blob: fbb2d12c41bc25f5b97f00209c71e8943d99f5e5 [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
154#define MLD_V1_SEEN(idev) (ipv6_devconf.force_mld_version == 1 || \
155 (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
165#define MLDV2_QQIC(value) MLDV2_EXP(0x80, 4, 3, value)
166#define MLDV2_MRC(value) MLDV2_EXP(0x8000, 12, 3, value)
167
David L Stevens6f4353d2005-12-26 17:03:46 -0800168#define IPV6_MLD_MAX_MSF 64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Brian Haleyab32ea52006-09-22 14:15:41 -0700170int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172/*
173 * socket join on multicast group
174 */
175
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900176int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 struct net_device *dev = NULL;
179 struct ipv6_mc_socklist *mc_lst;
180 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900181 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 int err;
183
184 if (!ipv6_addr_is_multicast(addr))
185 return -EINVAL;
186
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700187 read_lock_bh(&ipv6_sk_mc_lock);
188 for (mc_lst=np->ipv6_mc_list; mc_lst; mc_lst=mc_lst->next) {
189 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
190 ipv6_addr_equal(&mc_lst->addr, addr)) {
191 read_unlock_bh(&ipv6_sk_mc_lock);
192 return -EADDRINUSE;
193 }
194 }
195 read_unlock_bh(&ipv6_sk_mc_lock);
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
198
199 if (mc_lst == NULL)
200 return -ENOMEM;
201
202 mc_lst->next = NULL;
203 ipv6_addr_copy(&mc_lst->addr, addr);
204
205 if (ifindex == 0) {
206 struct rt6_info *rt;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800207 rt = rt6_lookup(net, addr, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (rt) {
209 dev = rt->rt6i_dev;
210 dev_hold(dev);
211 dst_release(&rt->u.dst);
212 }
213 } else
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800214 dev = dev_get_by_index(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 if (dev == NULL) {
217 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
218 return -ENODEV;
219 }
220
221 mc_lst->ifindex = dev->ifindex;
222 mc_lst->sfmode = MCAST_EXCLUDE;
YOSHIFUJI Hideaki196433c2006-01-04 13:56:31 -0800223 rwlock_init(&mc_lst->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 mc_lst->sflist = NULL;
225
226 /*
227 * now add/increase the group membership on the device
228 */
229
230 err = ipv6_dev_mc_inc(dev, addr);
231
232 if (err) {
233 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
234 dev_put(dev);
235 return err;
236 }
237
238 write_lock_bh(&ipv6_sk_mc_lock);
239 mc_lst->next = np->ipv6_mc_list;
240 np->ipv6_mc_list = mc_lst;
241 write_unlock_bh(&ipv6_sk_mc_lock);
242
243 dev_put(dev);
244
245 return 0;
246}
247
248/*
249 * socket leave on multicast group
250 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900251int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 struct ipv6_pinfo *np = inet6_sk(sk);
254 struct ipv6_mc_socklist *mc_lst, **lnk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900255 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 write_lock_bh(&ipv6_sk_mc_lock);
258 for (lnk = &np->ipv6_mc_list; (mc_lst = *lnk) !=NULL ; lnk = &mc_lst->next) {
259 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
260 ipv6_addr_equal(&mc_lst->addr, addr)) {
261 struct net_device *dev;
262
263 *lnk = mc_lst->next;
264 write_unlock_bh(&ipv6_sk_mc_lock);
265
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800266 dev = dev_get_by_index(net, mc_lst->ifindex);
267 if (dev != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 struct inet6_dev *idev = in6_dev_get(dev);
269
David L Stevensacd6e002006-08-17 16:27:39 -0700270 (void) ip6_mc_leave_src(sk, mc_lst, idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
273 in6_dev_put(idev);
274 }
275 dev_put(dev);
David L Stevensacd6e002006-08-17 16:27:39 -0700276 } else
277 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
279 return 0;
280 }
281 }
282 write_unlock_bh(&ipv6_sk_mc_lock);
283
David L Stevens9951f032005-07-08 17:47:28 -0700284 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800287static struct inet6_dev *ip6_mc_find_dev(struct net *net,
288 struct in6_addr *group,
289 int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 struct net_device *dev = NULL;
292 struct inet6_dev *idev = NULL;
293
294 if (ifindex == 0) {
295 struct rt6_info *rt;
296
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800297 rt = rt6_lookup(net, group, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (rt) {
299 dev = rt->rt6i_dev;
300 dev_hold(dev);
301 dst_release(&rt->u.dst);
302 }
303 } else
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800304 dev = dev_get_by_index(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 if (!dev)
307 return NULL;
308 idev = in6_dev_get(dev);
309 if (!idev) {
310 dev_put(dev);
311 return NULL;
312 }
313 read_lock_bh(&idev->lock);
314 if (idev->dead) {
315 read_unlock_bh(&idev->lock);
316 in6_dev_put(idev);
317 dev_put(dev);
318 return NULL;
319 }
320 return idev;
321}
322
323void ipv6_sock_mc_close(struct sock *sk)
324{
325 struct ipv6_pinfo *np = inet6_sk(sk);
326 struct ipv6_mc_socklist *mc_lst;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900327 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 write_lock_bh(&ipv6_sk_mc_lock);
330 while ((mc_lst = np->ipv6_mc_list) != NULL) {
331 struct net_device *dev;
332
333 np->ipv6_mc_list = mc_lst->next;
334 write_unlock_bh(&ipv6_sk_mc_lock);
335
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800336 dev = dev_get_by_index(net, mc_lst->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (dev) {
338 struct inet6_dev *idev = in6_dev_get(dev);
339
David L Stevensacd6e002006-08-17 16:27:39 -0700340 (void) ip6_mc_leave_src(sk, mc_lst, idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
343 in6_dev_put(idev);
344 }
345 dev_put(dev);
David L Stevensacd6e002006-08-17 16:27:39 -0700346 } else
347 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
350
351 write_lock_bh(&ipv6_sk_mc_lock);
352 }
353 write_unlock_bh(&ipv6_sk_mc_lock);
354}
355
356int ip6_mc_source(int add, int omode, struct sock *sk,
357 struct group_source_req *pgsr)
358{
359 struct in6_addr *source, *group;
360 struct ipv6_mc_socklist *pmc;
361 struct net_device *dev;
362 struct inet6_dev *idev;
363 struct ipv6_pinfo *inet6 = inet6_sk(sk);
364 struct ip6_sf_socklist *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900365 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 int i, j, rv;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700367 int leavegroup = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800368 int pmclocked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 int err;
370
371 if (pgsr->gsr_group.ss_family != AF_INET6 ||
372 pgsr->gsr_source.ss_family != AF_INET6)
373 return -EINVAL;
374
375 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
376 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
377
378 if (!ipv6_addr_is_multicast(group))
379 return -EINVAL;
380
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800381 idev = ip6_mc_find_dev(net, group, pgsr->gsr_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (!idev)
383 return -ENODEV;
384 dev = idev->dev;
385
386 err = -EADDRNOTAVAIL;
387
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700388 read_lock_bh(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
390 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
391 continue;
392 if (ipv6_addr_equal(&pmc->addr, group))
393 break;
394 }
David L Stevens917f2f12005-07-08 17:45:16 -0700395 if (!pmc) { /* must have a prior join */
396 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 /* if a source filter was set, must be the same mode as before */
400 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -0700401 if (pmc->sfmode != omode) {
402 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 } else if (pmc->sfmode != omode) {
406 /* allow mode switches for empty-set filters */
407 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
408 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
409 pmc->sfmode = omode;
410 }
411
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800412 write_lock_bh(&pmc->sflock);
413 pmclocked = 1;
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 psl = pmc->sflist;
416 if (!add) {
417 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -0700418 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 rv = !0;
420 for (i=0; i<psl->sl_count; i++) {
421 rv = memcmp(&psl->sl_addr[i], source,
422 sizeof(struct in6_addr));
423 if (rv == 0)
424 break;
425 }
426 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -0700427 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700429 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
430 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
431 leavegroup = 1;
432 goto done;
433 }
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 /* update the interface filter */
436 ip6_mc_del_src(idev, group, omode, 1, source, 1);
437
438 for (j=i+1; j<psl->sl_count; j++)
439 psl->sl_addr[j-1] = psl->sl_addr[j];
440 psl->sl_count--;
441 err = 0;
442 goto done;
443 }
444 /* else, add a new source to the filter */
445
446 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
447 err = -ENOBUFS;
448 goto done;
449 }
450 if (!psl || psl->sl_count == psl->sl_max) {
451 struct ip6_sf_socklist *newpsl;
452 int count = IP6_SFBLOCK;
453
454 if (psl)
455 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800456 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (!newpsl) {
458 err = -ENOBUFS;
459 goto done;
460 }
461 newpsl->sl_max = count;
462 newpsl->sl_count = count - IP6_SFBLOCK;
463 if (psl) {
464 for (i=0; i<psl->sl_count; i++)
465 newpsl->sl_addr[i] = psl->sl_addr[i];
466 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
467 }
468 pmc->sflist = psl = newpsl;
469 }
470 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
471 for (i=0; i<psl->sl_count; i++) {
472 rv = memcmp(&psl->sl_addr[i], source, sizeof(struct in6_addr));
473 if (rv == 0)
474 break;
475 }
476 if (rv == 0) /* address already there is an error */
477 goto done;
478 for (j=psl->sl_count-1; j>=i; j--)
479 psl->sl_addr[j+1] = psl->sl_addr[j];
480 psl->sl_addr[i] = *source;
481 psl->sl_count++;
482 err = 0;
483 /* update the interface list */
484 ip6_mc_add_src(idev, group, omode, 1, source, 1);
485done:
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800486 if (pmclocked)
487 write_unlock_bh(&pmc->sflock);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700488 read_unlock_bh(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 read_unlock_bh(&idev->lock);
490 in6_dev_put(idev);
491 dev_put(dev);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700492 if (leavegroup)
493 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return err;
495}
496
497int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
498{
499 struct in6_addr *group;
500 struct ipv6_mc_socklist *pmc;
501 struct net_device *dev;
502 struct inet6_dev *idev;
503 struct ipv6_pinfo *inet6 = inet6_sk(sk);
504 struct ip6_sf_socklist *newpsl, *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900505 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -0700506 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 int i, err;
508
509 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
510
511 if (!ipv6_addr_is_multicast(group))
512 return -EINVAL;
513 if (gsf->gf_fmode != MCAST_INCLUDE &&
514 gsf->gf_fmode != MCAST_EXCLUDE)
515 return -EINVAL;
516
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800517 idev = ip6_mc_find_dev(net, group, gsf->gf_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 if (!idev)
520 return -ENODEV;
521 dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
David S. Miller9c059892005-07-08 21:44:39 -0700523 err = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800524 read_lock_bh(&ipv6_sk_mc_lock);
525
David L Stevens9951f032005-07-08 17:47:28 -0700526 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
527 leavegroup = 1;
528 goto done;
529 }
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
532 if (pmc->ifindex != gsf->gf_interface)
533 continue;
534 if (ipv6_addr_equal(&pmc->addr, group))
535 break;
536 }
David L Stevens917f2f12005-07-08 17:45:16 -0700537 if (!pmc) { /* must have a prior join */
538 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (gsf->gf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800542 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
543 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (!newpsl) {
545 err = -ENOBUFS;
546 goto done;
547 }
548 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
549 for (i=0; i<newpsl->sl_count; ++i) {
550 struct sockaddr_in6 *psin6;
551
552 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
553 newpsl->sl_addr[i] = psin6->sin6_addr;
554 }
555 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
556 newpsl->sl_count, newpsl->sl_addr, 0);
557 if (err) {
558 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
559 goto done;
560 }
Yan Zheng8713dbf2005-10-28 08:02:08 +0800561 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +0800563 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
564 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800565
566 write_lock_bh(&pmc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 psl = pmc->sflist;
568 if (psl) {
569 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
570 psl->sl_count, psl->sl_addr, 0);
571 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
572 } else
573 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
574 pmc->sflist = newpsl;
575 pmc->sfmode = gsf->gf_fmode;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800576 write_unlock_bh(&pmc->sflock);
David L Stevens917f2f12005-07-08 17:45:16 -0700577 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578done:
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800579 read_unlock_bh(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 read_unlock_bh(&idev->lock);
581 in6_dev_put(idev);
582 dev_put(dev);
David L Stevens9951f032005-07-08 17:47:28 -0700583 if (leavegroup)
584 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return err;
586}
587
588int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
589 struct group_filter __user *optval, int __user *optlen)
590{
591 int err, i, count, copycount;
592 struct in6_addr *group;
593 struct ipv6_mc_socklist *pmc;
594 struct inet6_dev *idev;
595 struct net_device *dev;
596 struct ipv6_pinfo *inet6 = inet6_sk(sk);
597 struct ip6_sf_socklist *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900598 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
601
602 if (!ipv6_addr_is_multicast(group))
603 return -EINVAL;
604
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800605 idev = ip6_mc_find_dev(net, group, gsf->gf_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 if (!idev)
608 return -ENODEV;
609
610 dev = idev->dev;
611
612 err = -EADDRNOTAVAIL;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800613 /*
614 * changes to the ipv6_mc_list require the socket lock and
615 * a read lock on ip6_sk_mc_lock. We have the socket lock,
616 * so reading the list is safe.
617 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
620 if (pmc->ifindex != gsf->gf_interface)
621 continue;
622 if (ipv6_addr_equal(group, &pmc->addr))
623 break;
624 }
625 if (!pmc) /* must have a prior join */
626 goto done;
627 gsf->gf_fmode = pmc->sfmode;
628 psl = pmc->sflist;
629 count = psl ? psl->sl_count : 0;
630 read_unlock_bh(&idev->lock);
631 in6_dev_put(idev);
632 dev_put(dev);
633
634 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
635 gsf->gf_numsrc = count;
636 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
637 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
638 return -EFAULT;
639 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800640 /* changes to psl require the socket lock, a read lock on
641 * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
642 * have the socket lock, so reading here is safe.
643 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 for (i=0; i<copycount; i++) {
645 struct sockaddr_in6 *psin6;
646 struct sockaddr_storage ss;
647
648 psin6 = (struct sockaddr_in6 *)&ss;
649 memset(&ss, 0, sizeof(ss));
650 psin6->sin6_family = AF_INET6;
651 psin6->sin6_addr = psl->sl_addr[i];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900652 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return -EFAULT;
654 }
655 return 0;
656done:
657 read_unlock_bh(&idev->lock);
658 in6_dev_put(idev);
659 dev_put(dev);
660 return err;
661}
662
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900663int inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
664 const struct in6_addr *src_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 struct ipv6_pinfo *np = inet6_sk(sk);
667 struct ipv6_mc_socklist *mc;
668 struct ip6_sf_socklist *psl;
669 int rv = 1;
670
671 read_lock(&ipv6_sk_mc_lock);
672 for (mc = np->ipv6_mc_list; mc; mc = mc->next) {
673 if (ipv6_addr_equal(&mc->addr, mc_addr))
674 break;
675 }
676 if (!mc) {
677 read_unlock(&ipv6_sk_mc_lock);
678 return 1;
679 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800680 read_lock(&mc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 psl = mc->sflist;
682 if (!psl) {
683 rv = mc->sfmode == MCAST_EXCLUDE;
684 } else {
685 int i;
686
687 for (i=0; i<psl->sl_count; i++) {
688 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
689 break;
690 }
691 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
692 rv = 0;
693 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
694 rv = 0;
695 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800696 read_unlock(&mc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 read_unlock(&ipv6_sk_mc_lock);
698
699 return rv;
700}
701
702static void ma_put(struct ifmcaddr6 *mc)
703{
704 if (atomic_dec_and_test(&mc->mca_refcnt)) {
705 in6_dev_put(mc->idev);
706 kfree(mc);
707 }
708}
709
710static void igmp6_group_added(struct ifmcaddr6 *mc)
711{
712 struct net_device *dev = mc->idev->dev;
713 char buf[MAX_ADDR_LEN];
714
715 spin_lock_bh(&mc->mca_lock);
716 if (!(mc->mca_flags&MAF_LOADED)) {
717 mc->mca_flags |= MAF_LOADED;
718 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
719 dev_mc_add(dev, buf, dev->addr_len, 0);
720 }
721 spin_unlock_bh(&mc->mca_lock);
722
723 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
724 return;
725
726 if (MLD_V1_SEEN(mc->idev)) {
727 igmp6_join_group(mc);
728 return;
729 }
730 /* else v2 */
731
732 mc->mca_crcount = mc->idev->mc_qrv;
733 mld_ifc_event(mc->idev);
734}
735
736static void igmp6_group_dropped(struct ifmcaddr6 *mc)
737{
738 struct net_device *dev = mc->idev->dev;
739 char buf[MAX_ADDR_LEN];
740
741 spin_lock_bh(&mc->mca_lock);
742 if (mc->mca_flags&MAF_LOADED) {
743 mc->mca_flags &= ~MAF_LOADED;
744 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
745 dev_mc_delete(dev, buf, dev->addr_len, 0);
746 }
747
748 if (mc->mca_flags & MAF_NOREPORT)
749 goto done;
750 spin_unlock_bh(&mc->mca_lock);
751
752 if (!mc->idev->dead)
753 igmp6_leave_group(mc);
754
755 spin_lock_bh(&mc->mca_lock);
756 if (del_timer(&mc->mca_timer))
757 atomic_dec(&mc->mca_refcnt);
758done:
759 ip6_mc_clear_src(mc);
760 spin_unlock_bh(&mc->mca_lock);
761}
762
763/*
764 * deleted ifmcaddr6 manipulation
765 */
766static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
767{
768 struct ifmcaddr6 *pmc;
769
770 /* this is an "ifmcaddr6" for convenience; only the fields below
771 * are actually used. In particular, the refcnt and users are not
772 * used for management of the delete list. Using the same structure
773 * for deleted items allows change reports to use common code with
774 * non-deleted or query-response MCA's.
775 */
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800776 pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (!pmc)
778 return;
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 spin_lock_bh(&im->mca_lock);
781 spin_lock_init(&pmc->mca_lock);
782 pmc->idev = im->idev;
783 in6_dev_hold(idev);
784 pmc->mca_addr = im->mca_addr;
785 pmc->mca_crcount = idev->mc_qrv;
786 pmc->mca_sfmode = im->mca_sfmode;
787 if (pmc->mca_sfmode == MCAST_INCLUDE) {
788 struct ip6_sf_list *psf;
789
790 pmc->mca_tomb = im->mca_tomb;
791 pmc->mca_sources = im->mca_sources;
792 im->mca_tomb = im->mca_sources = NULL;
793 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
794 psf->sf_crcount = pmc->mca_crcount;
795 }
796 spin_unlock_bh(&im->mca_lock);
797
798 write_lock_bh(&idev->mc_lock);
799 pmc->next = idev->mc_tomb;
800 idev->mc_tomb = pmc;
801 write_unlock_bh(&idev->mc_lock);
802}
803
804static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *pmca)
805{
806 struct ifmcaddr6 *pmc, *pmc_prev;
807 struct ip6_sf_list *psf, *psf_next;
808
809 write_lock_bh(&idev->mc_lock);
810 pmc_prev = NULL;
811 for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
812 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
813 break;
814 pmc_prev = pmc;
815 }
816 if (pmc) {
817 if (pmc_prev)
818 pmc_prev->next = pmc->next;
819 else
820 idev->mc_tomb = pmc->next;
821 }
822 write_unlock_bh(&idev->mc_lock);
823 if (pmc) {
824 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
825 psf_next = psf->sf_next;
826 kfree(psf);
827 }
828 in6_dev_put(pmc->idev);
829 kfree(pmc);
830 }
831}
832
833static void mld_clear_delrec(struct inet6_dev *idev)
834{
835 struct ifmcaddr6 *pmc, *nextpmc;
836
837 write_lock_bh(&idev->mc_lock);
838 pmc = idev->mc_tomb;
839 idev->mc_tomb = NULL;
840 write_unlock_bh(&idev->mc_lock);
841
842 for (; pmc; pmc = nextpmc) {
843 nextpmc = pmc->next;
844 ip6_mc_clear_src(pmc);
845 in6_dev_put(pmc->idev);
846 kfree(pmc);
847 }
848
849 /* clear dead sources, too */
850 read_lock_bh(&idev->lock);
851 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
852 struct ip6_sf_list *psf, *psf_next;
853
854 spin_lock_bh(&pmc->mca_lock);
855 psf = pmc->mca_tomb;
856 pmc->mca_tomb = NULL;
857 spin_unlock_bh(&pmc->mca_lock);
858 for (; psf; psf=psf_next) {
859 psf_next = psf->sf_next;
860 kfree(psf);
861 }
862 }
863 read_unlock_bh(&idev->lock);
864}
865
866
867/*
868 * device multicast group inc (add if not found)
869 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900870int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 struct ifmcaddr6 *mc;
873 struct inet6_dev *idev;
874
875 idev = in6_dev_get(dev);
876
877 if (idev == NULL)
878 return -EINVAL;
879
880 write_lock_bh(&idev->lock);
881 if (idev->dead) {
882 write_unlock_bh(&idev->lock);
883 in6_dev_put(idev);
884 return -ENODEV;
885 }
886
887 for (mc = idev->mc_list; mc; mc = mc->next) {
888 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
889 mc->mca_users++;
890 write_unlock_bh(&idev->lock);
891 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
892 NULL, 0);
893 in6_dev_put(idev);
894 return 0;
895 }
896 }
897
898 /*
899 * not found: create a new one.
900 */
901
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800902 mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 if (mc == NULL) {
905 write_unlock_bh(&idev->lock);
906 in6_dev_put(idev);
907 return -ENOMEM;
908 }
909
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800910 setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 ipv6_addr_copy(&mc->mca_addr, addr);
913 mc->idev = idev;
914 mc->mca_users = 1;
915 /* mca_stamp should be updated upon changes */
916 mc->mca_cstamp = mc->mca_tstamp = jiffies;
917 atomic_set(&mc->mca_refcnt, 2);
918 spin_lock_init(&mc->mca_lock);
919
920 /* initial mode is (EX, empty) */
921 mc->mca_sfmode = MCAST_EXCLUDE;
922 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
923
924 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
925 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
926 mc->mca_flags |= MAF_NOREPORT;
927
928 mc->next = idev->mc_list;
929 idev->mc_list = mc;
930 write_unlock_bh(&idev->lock);
931
932 mld_del_delrec(idev, &mc->mca_addr);
933 igmp6_group_added(mc);
934 ma_put(mc);
935 return 0;
936}
937
938/*
939 * device multicast group del
940 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900941int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
943 struct ifmcaddr6 *ma, **map;
944
945 write_lock_bh(&idev->lock);
946 for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
947 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
948 if (--ma->mca_users == 0) {
949 *map = ma->next;
950 write_unlock_bh(&idev->lock);
951
952 igmp6_group_dropped(ma);
953
954 ma_put(ma);
955 return 0;
956 }
957 write_unlock_bh(&idev->lock);
958 return 0;
959 }
960 }
961 write_unlock_bh(&idev->lock);
962
963 return -ENOENT;
964}
965
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900966int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
968 struct inet6_dev *idev = in6_dev_get(dev);
969 int err;
970
971 if (!idev)
972 return -ENODEV;
973
974 err = __ipv6_dev_mc_dec(idev, addr);
975
976 in6_dev_put(idev);
977
978 return err;
979}
980
981/*
982 * identify MLD packets for MLD filter exceptions
983 */
984int ipv6_is_mld(struct sk_buff *skb, int nexthdr)
985{
986 struct icmp6hdr *pic;
987
988 if (nexthdr != IPPROTO_ICMPV6)
989 return 0;
990
991 if (!pskb_may_pull(skb, sizeof(struct icmp6hdr)))
992 return 0;
993
Arnaldo Carvalho de Melocc70ab22007-03-13 14:03:22 -0300994 pic = icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 switch (pic->icmp6_type) {
997 case ICMPV6_MGM_QUERY:
998 case ICMPV6_MGM_REPORT:
999 case ICMPV6_MGM_REDUCTION:
1000 case ICMPV6_MLD2_REPORT:
1001 return 1;
1002 default:
1003 break;
1004 }
1005 return 0;
1006}
1007
1008/*
1009 * check if the interface/address pair is valid
1010 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001011int ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
1012 const struct in6_addr *src_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 struct inet6_dev *idev;
1015 struct ifmcaddr6 *mc;
1016 int rv = 0;
1017
1018 idev = in6_dev_get(dev);
1019 if (idev) {
1020 read_lock_bh(&idev->lock);
1021 for (mc = idev->mc_list; mc; mc=mc->next) {
1022 if (ipv6_addr_equal(&mc->mca_addr, group))
1023 break;
1024 }
1025 if (mc) {
1026 if (src_addr && !ipv6_addr_any(src_addr)) {
1027 struct ip6_sf_list *psf;
1028
1029 spin_lock_bh(&mc->mca_lock);
1030 for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
1031 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
1032 break;
1033 }
1034 if (psf)
1035 rv = psf->sf_count[MCAST_INCLUDE] ||
1036 psf->sf_count[MCAST_EXCLUDE] !=
1037 mc->mca_sfcount[MCAST_EXCLUDE];
1038 else
1039 rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
1040 spin_unlock_bh(&mc->mca_lock);
1041 } else
1042 rv = 1; /* don't filter unspecified source */
1043 }
1044 read_unlock_bh(&idev->lock);
1045 in6_dev_put(idev);
1046 }
1047 return rv;
1048}
1049
1050static void mld_gq_start_timer(struct inet6_dev *idev)
1051{
1052 int tv = net_random() % idev->mc_maxdelay;
1053
1054 idev->mc_gq_running = 1;
1055 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
1056 in6_dev_hold(idev);
1057}
1058
1059static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
1060{
1061 int tv = net_random() % delay;
1062
1063 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1064 in6_dev_hold(idev);
1065}
1066
1067/*
1068 * IGMP handling (alias multicast ICMPv6 messages)
1069 */
1070
1071static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1072{
1073 unsigned long delay = resptime;
1074
1075 /* Do not start timer for these addresses */
1076 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1077 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1078 return;
1079
1080 if (del_timer(&ma->mca_timer)) {
1081 atomic_dec(&ma->mca_refcnt);
1082 delay = ma->mca_timer.expires - jiffies;
1083 }
1084
1085 if (delay >= resptime) {
1086 if (resptime)
1087 delay = net_random() % resptime;
1088 else
1089 delay = 1;
1090 }
1091 ma->mca_timer.expires = jiffies + delay;
1092 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1093 atomic_inc(&ma->mca_refcnt);
1094 ma->mca_flags |= MAF_TIMER_RUNNING;
1095}
1096
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001097/* mark EXCLUDE-mode sources */
1098static int mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 struct in6_addr *srcs)
1100{
1101 struct ip6_sf_list *psf;
1102 int i, scount;
1103
1104 scount = 0;
1105 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1106 if (scount == nsrcs)
1107 break;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001108 for (i=0; i<nsrcs; i++) {
1109 /* skip inactive filters */
1110 if (pmc->mca_sfcount[MCAST_INCLUDE] ||
1111 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1112 psf->sf_count[MCAST_EXCLUDE])
1113 continue;
1114 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1115 scount++;
1116 break;
1117 }
1118 }
1119 }
1120 pmc->mca_flags &= ~MAF_GSQUERY;
1121 if (scount == nsrcs) /* all sources excluded */
1122 return 0;
1123 return 1;
1124}
1125
1126static int mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1127 struct in6_addr *srcs)
1128{
1129 struct ip6_sf_list *psf;
1130 int i, scount;
1131
1132 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1133 return mld_xmarksources(pmc, nsrcs, srcs);
1134
1135 /* mark INCLUDE-mode sources */
1136
1137 scount = 0;
1138 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1139 if (scount == nsrcs)
1140 break;
1141 for (i=0; i<nsrcs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1143 psf->sf_gsresp = 1;
1144 scount++;
1145 break;
1146 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001149 if (!scount) {
1150 pmc->mca_flags &= ~MAF_GSQUERY;
1151 return 0;
1152 }
1153 pmc->mca_flags |= MAF_GSQUERY;
1154 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155}
1156
1157int igmp6_event_query(struct sk_buff *skb)
1158{
Yan Zheng97300b52005-10-31 20:09:45 +08001159 struct mld2_query *mlh2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 struct ifmcaddr6 *ma;
1161 struct in6_addr *group;
1162 unsigned long max_delay;
1163 struct inet6_dev *idev;
1164 struct icmp6hdr *hdr;
1165 int group_type;
1166 int mark = 0;
1167 int len;
1168
1169 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1170 return -EINVAL;
1171
1172 /* compute payload length excluding extension headers */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001173 len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001174 len -= skb_network_header_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 /* Drop queries with not link local source */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001177 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 return -EINVAL;
1179
1180 idev = in6_dev_get(skb->dev);
1181
1182 if (idev == NULL)
1183 return 0;
1184
Arnaldo Carvalho de Melocc70ab22007-03-13 14:03:22 -03001185 hdr = icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 group = (struct in6_addr *) (hdr + 1);
1187 group_type = ipv6_addr_type(group);
1188
1189 if (group_type != IPV6_ADDR_ANY &&
1190 !(group_type&IPV6_ADDR_MULTICAST)) {
1191 in6_dev_put(idev);
1192 return -EINVAL;
1193 }
1194
1195 if (len == 24) {
1196 int switchback;
1197 /* MLDv1 router present */
1198
1199 /* Translate milliseconds to jiffies */
1200 max_delay = (ntohs(hdr->icmp6_maxdelay)*HZ)/1000;
1201
1202 switchback = (idev->mc_qrv + 1) * max_delay;
1203 idev->mc_v1_seen = jiffies + switchback;
1204
1205 /* cancel the interface change timer */
1206 idev->mc_ifc_count = 0;
1207 if (del_timer(&idev->mc_ifc_timer))
1208 __in6_dev_put(idev);
1209 /* clear deleted report items */
1210 mld_clear_delrec(idev);
1211 } else if (len >= 28) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001212 int srcs_offset = sizeof(struct mld2_query) -
Yan Zheng97300b52005-10-31 20:09:45 +08001213 sizeof(struct icmp6hdr);
1214 if (!pskb_may_pull(skb, srcs_offset)) {
1215 in6_dev_put(idev);
1216 return -EINVAL;
1217 }
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001218 mlh2 = (struct mld2_query *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 max_delay = (MLDV2_MRC(ntohs(mlh2->mrc))*HZ)/1000;
1220 if (!max_delay)
1221 max_delay = 1;
1222 idev->mc_maxdelay = max_delay;
1223 if (mlh2->qrv)
1224 idev->mc_qrv = mlh2->qrv;
1225 if (group_type == IPV6_ADDR_ANY) { /* general query */
1226 if (mlh2->nsrcs) {
1227 in6_dev_put(idev);
1228 return -EINVAL; /* no sources allowed */
1229 }
1230 mld_gq_start_timer(idev);
1231 in6_dev_put(idev);
1232 return 0;
1233 }
1234 /* mark sources to include, if group & source-specific */
Yan Zheng97300b52005-10-31 20:09:45 +08001235 if (mlh2->nsrcs != 0) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001236 if (!pskb_may_pull(skb, srcs_offset +
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001237 ntohs(mlh2->nsrcs) * sizeof(struct in6_addr))) {
Yan Zheng97300b52005-10-31 20:09:45 +08001238 in6_dev_put(idev);
1239 return -EINVAL;
1240 }
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001241 mlh2 = (struct mld2_query *)skb_transport_header(skb);
Yan Zheng97300b52005-10-31 20:09:45 +08001242 mark = 1;
1243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 } else {
1245 in6_dev_put(idev);
1246 return -EINVAL;
1247 }
1248
1249 read_lock_bh(&idev->lock);
1250 if (group_type == IPV6_ADDR_ANY) {
1251 for (ma = idev->mc_list; ma; ma=ma->next) {
1252 spin_lock_bh(&ma->mca_lock);
1253 igmp6_group_queried(ma, max_delay);
1254 spin_unlock_bh(&ma->mca_lock);
1255 }
1256 } else {
1257 for (ma = idev->mc_list; ma; ma=ma->next) {
David L Stevens7add2a42006-01-24 13:06:39 -08001258 if (!ipv6_addr_equal(group, &ma->mca_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 continue;
1260 spin_lock_bh(&ma->mca_lock);
1261 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1262 /* gsquery <- gsquery && mark */
1263 if (!mark)
1264 ma->mca_flags &= ~MAF_GSQUERY;
1265 } else {
1266 /* gsquery <- mark */
1267 if (mark)
1268 ma->mca_flags |= MAF_GSQUERY;
1269 else
1270 ma->mca_flags &= ~MAF_GSQUERY;
1271 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001272 if (!(ma->mca_flags & MAF_GSQUERY) ||
David L Stevens7add2a42006-01-24 13:06:39 -08001273 mld_marksources(ma, ntohs(mlh2->nsrcs), mlh2->srcs))
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001274 igmp6_group_queried(ma, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 spin_unlock_bh(&ma->mca_lock);
David L Stevens7add2a42006-01-24 13:06:39 -08001276 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278 }
1279 read_unlock_bh(&idev->lock);
1280 in6_dev_put(idev);
1281
1282 return 0;
1283}
1284
1285
1286int igmp6_event_report(struct sk_buff *skb)
1287{
1288 struct ifmcaddr6 *ma;
1289 struct in6_addr *addrp;
1290 struct inet6_dev *idev;
1291 struct icmp6hdr *hdr;
1292 int addr_type;
1293
1294 /* Our own report looped back. Ignore it. */
1295 if (skb->pkt_type == PACKET_LOOPBACK)
1296 return 0;
1297
David Stevens24c69272005-12-02 20:32:59 -08001298 /* send our report if the MC router may not have heard this report */
1299 if (skb->pkt_type != PACKET_MULTICAST &&
1300 skb->pkt_type != PACKET_BROADCAST)
1301 return 0;
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1304 return -EINVAL;
1305
Arnaldo Carvalho de Melocc70ab22007-03-13 14:03:22 -03001306 hdr = icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 /* Drop reports with not link local source */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001309 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001310 if (addr_type != IPV6_ADDR_ANY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 !(addr_type&IPV6_ADDR_LINKLOCAL))
1312 return -EINVAL;
1313
1314 addrp = (struct in6_addr *) (hdr + 1);
1315
1316 idev = in6_dev_get(skb->dev);
1317 if (idev == NULL)
1318 return -ENODEV;
1319
1320 /*
1321 * Cancel the timer for this group
1322 */
1323
1324 read_lock_bh(&idev->lock);
1325 for (ma = idev->mc_list; ma; ma=ma->next) {
1326 if (ipv6_addr_equal(&ma->mca_addr, addrp)) {
1327 spin_lock(&ma->mca_lock);
1328 if (del_timer(&ma->mca_timer))
1329 atomic_dec(&ma->mca_refcnt);
1330 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1331 spin_unlock(&ma->mca_lock);
1332 break;
1333 }
1334 }
1335 read_unlock_bh(&idev->lock);
1336 in6_dev_put(idev);
1337 return 0;
1338}
1339
1340static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1341 int gdeleted, int sdeleted)
1342{
1343 switch (type) {
1344 case MLD2_MODE_IS_INCLUDE:
1345 case MLD2_MODE_IS_EXCLUDE:
1346 if (gdeleted || sdeleted)
1347 return 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001348 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1349 if (pmc->mca_sfmode == MCAST_INCLUDE)
1350 return 1;
1351 /* don't include if this source is excluded
1352 * in all filters
1353 */
1354 if (psf->sf_count[MCAST_INCLUDE])
David L Stevens7add2a42006-01-24 13:06:39 -08001355 return type == MLD2_MODE_IS_INCLUDE;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001356 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1357 psf->sf_count[MCAST_EXCLUDE];
1358 }
1359 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 case MLD2_CHANGE_TO_INCLUDE:
1361 if (gdeleted || sdeleted)
1362 return 0;
1363 return psf->sf_count[MCAST_INCLUDE] != 0;
1364 case MLD2_CHANGE_TO_EXCLUDE:
1365 if (gdeleted || sdeleted)
1366 return 0;
1367 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1368 psf->sf_count[MCAST_INCLUDE])
1369 return 0;
1370 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1371 psf->sf_count[MCAST_EXCLUDE];
1372 case MLD2_ALLOW_NEW_SOURCES:
1373 if (gdeleted || !psf->sf_crcount)
1374 return 0;
1375 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1376 case MLD2_BLOCK_OLD_SOURCES:
1377 if (pmc->mca_sfmode == MCAST_INCLUDE)
1378 return gdeleted || (psf->sf_crcount && sdeleted);
1379 return psf->sf_crcount && !gdeleted && !sdeleted;
1380 }
1381 return 0;
1382}
1383
1384static int
1385mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1386{
1387 struct ip6_sf_list *psf;
1388 int scount = 0;
1389
1390 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1391 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1392 continue;
1393 scount++;
1394 }
1395 return scount;
1396}
1397
1398static struct sk_buff *mld_newpack(struct net_device *dev, int size)
1399{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001400 struct net *net = dev_net(dev);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001401 struct sock *sk = net->ipv6.igmp_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 struct sk_buff *skb;
1403 struct mld2_report *pmr;
1404 struct in6_addr addr_buf;
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001405 const struct in6_addr *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 int err;
1407 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1408 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1409 IPV6_TLV_PADN, 0 };
1410
1411 /* we assume size > sizeof(ra) here */
Johannes Bergf5184d22008-05-12 20:48:31 -07001412 skb = sock_alloc_send_skb(sk, size + LL_ALLOCATED_SPACE(dev), 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001414 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return NULL;
1416
1417 skb_reserve(skb, LL_RESERVED_SPACE(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Neil Horman95c385b2007-04-25 17:08:10 -07001419 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 /* <draft-ietf-magma-mld-source-05.txt>:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001421 * use unspecified address as the source address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 * when a valid link-local address is not available.
1423 */
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001424 saddr = &in6addr_any;
1425 } else
1426 saddr = &addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001428 ip6_nd_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1431
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001432 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -03001433 skb_put(skb, sizeof(*pmr));
1434 pmr = (struct mld2_report *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 pmr->type = ICMPV6_MLD2_REPORT;
1436 pmr->resv1 = 0;
1437 pmr->csum = 0;
1438 pmr->resv2 = 0;
1439 pmr->ngrec = 0;
1440 return skb;
1441}
1442
1443static void mld_sendpack(struct sk_buff *skb)
1444{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001445 struct ipv6hdr *pip6 = ipv6_hdr(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001446 struct mld2_report *pmr =
1447 (struct mld2_report *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 int payload_len, mldlen;
1449 struct inet6_dev *idev = in6_dev_get(skb->dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001450 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 int err;
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001452 struct flowi fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001454 IP6_INC_STATS(idev, IPSTATS_MIB_OUTREQUESTS);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001455 payload_len = (skb->tail - skb->network_header) - sizeof(*pip6);
1456 mldlen = skb->tail - skb->transport_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 pip6->payload_len = htons(payload_len);
1458
1459 pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001460 IPPROTO_ICMPV6, csum_partial(skb_transport_header(skb),
1461 mldlen, 0));
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001462
1463 skb->dst = icmp6_dst_alloc(skb->dev, NULL, &ipv6_hdr(skb)->daddr);
1464
1465 if (!skb->dst) {
1466 err = -ENOMEM;
1467 goto err_out;
1468 }
1469
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001470 icmpv6_flow_init(net->ipv6.igmp_sk, &fl, ICMPV6_MLD2_REPORT,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001471 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1472 skb->dev->ifindex);
1473
1474 err = xfrm_lookup(&skb->dst, &fl, NULL, 0);
1475 if (err)
1476 goto err_out;
1477
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001478 err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001479 dst_output);
1480out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 if (!err) {
David L Stevens14878f72007-09-16 16:52:35 -07001482 ICMP6MSGOUT_INC_STATS_BH(idev, ICMPV6_MLD2_REPORT);
1483 ICMP6_INC_STATS_BH(idev, ICMP6_MIB_OUTMSGS);
1484 IP6_INC_STATS_BH(idev, IPSTATS_MIB_OUTMCASTPKTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 } else
David L Stevens14878f72007-09-16 16:52:35 -07001486 IP6_INC_STATS_BH(idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 if (likely(idev != NULL))
1489 in6_dev_put(idev);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001490 return;
1491
1492err_out:
1493 kfree_skb(skb);
1494 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
1497static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1498{
Yan Zhengfab10fe2005-10-05 12:08:13 -07001499 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
1502static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1503 int type, struct mld2_grec **ppgr)
1504{
1505 struct net_device *dev = pmc->idev->dev;
1506 struct mld2_report *pmr;
1507 struct mld2_grec *pgr;
1508
1509 if (!skb)
1510 skb = mld_newpack(dev, dev->mtu);
1511 if (!skb)
1512 return NULL;
1513 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1514 pgr->grec_type = type;
1515 pgr->grec_auxwords = 0;
1516 pgr->grec_nsrcs = 0;
1517 pgr->grec_mca = pmc->mca_addr; /* structure copy */
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001518 pmr = (struct mld2_report *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
1520 *ppgr = pgr;
1521 return skb;
1522}
1523
1524#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1525 skb_tailroom(skb)) : 0)
1526
1527static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1528 int type, int gdeleted, int sdeleted)
1529{
1530 struct net_device *dev = pmc->idev->dev;
1531 struct mld2_report *pmr;
1532 struct mld2_grec *pgr = NULL;
1533 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001534 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 if (pmc->mca_flags & MAF_NOREPORT)
1537 return skb;
1538
1539 isquery = type == MLD2_MODE_IS_INCLUDE ||
1540 type == MLD2_MODE_IS_EXCLUDE;
1541 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1542 type == MLD2_CHANGE_TO_EXCLUDE;
1543
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001544 stotal = scount = 0;
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1547
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001548 if (!*psf_list)
1549 goto empty_source;
1550
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001551 pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 /* EX and TO_EX get a fresh packet, if needed */
1554 if (truncate) {
1555 if (pmr && pmr->ngrec &&
1556 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1557 if (skb)
1558 mld_sendpack(skb);
1559 skb = mld_newpack(dev, dev->mtu);
1560 }
1561 }
1562 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 psf_prev = NULL;
1564 for (psf=*psf_list; psf; psf=psf_next) {
1565 struct in6_addr *psrc;
1566
1567 psf_next = psf->sf_next;
1568
1569 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1570 psf_prev = psf;
1571 continue;
1572 }
1573
1574 /* clear marks on query responses */
1575 if (isquery)
1576 psf->sf_gsresp = 0;
1577
1578 if (AVAILABLE(skb) < sizeof(*psrc) +
1579 first*sizeof(struct mld2_grec)) {
1580 if (truncate && !first)
1581 break; /* truncate these */
1582 if (pgr)
1583 pgr->grec_nsrcs = htons(scount);
1584 if (skb)
1585 mld_sendpack(skb);
1586 skb = mld_newpack(dev, dev->mtu);
1587 first = 1;
1588 scount = 0;
1589 }
1590 if (first) {
1591 skb = add_grhead(skb, pmc, type, &pgr);
1592 first = 0;
1593 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -08001594 if (!skb)
1595 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1597 *psrc = psf->sf_addr;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001598 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1600 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1601 psf->sf_crcount--;
1602 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1603 if (psf_prev)
1604 psf_prev->sf_next = psf->sf_next;
1605 else
1606 *psf_list = psf->sf_next;
1607 kfree(psf);
1608 continue;
1609 }
1610 }
1611 psf_prev = psf;
1612 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001613
1614empty_source:
1615 if (!stotal) {
1616 if (type == MLD2_ALLOW_NEW_SOURCES ||
1617 type == MLD2_BLOCK_OLD_SOURCES)
1618 return skb;
1619 if (pmc->mca_crcount || isquery) {
1620 /* make sure we have room for group header */
1621 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1622 mld_sendpack(skb);
1623 skb = NULL; /* add_grhead will get a new one */
1624 }
1625 skb = add_grhead(skb, pmc, type, &pgr);
1626 }
1627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 if (pgr)
1629 pgr->grec_nsrcs = htons(scount);
1630
1631 if (isquery)
1632 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1633 return skb;
1634}
1635
1636static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1637{
1638 struct sk_buff *skb = NULL;
1639 int type;
1640
1641 if (!pmc) {
1642 read_lock_bh(&idev->lock);
1643 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1644 if (pmc->mca_flags & MAF_NOREPORT)
1645 continue;
1646 spin_lock_bh(&pmc->mca_lock);
1647 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1648 type = MLD2_MODE_IS_EXCLUDE;
1649 else
1650 type = MLD2_MODE_IS_INCLUDE;
1651 skb = add_grec(skb, pmc, type, 0, 0);
1652 spin_unlock_bh(&pmc->mca_lock);
1653 }
1654 read_unlock_bh(&idev->lock);
1655 } else {
1656 spin_lock_bh(&pmc->mca_lock);
1657 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1658 type = MLD2_MODE_IS_EXCLUDE;
1659 else
1660 type = MLD2_MODE_IS_INCLUDE;
1661 skb = add_grec(skb, pmc, type, 0, 0);
1662 spin_unlock_bh(&pmc->mca_lock);
1663 }
1664 if (skb)
1665 mld_sendpack(skb);
1666}
1667
1668/*
1669 * remove zero-count source records from a source filter list
1670 */
1671static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1672{
1673 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1674
1675 psf_prev = NULL;
1676 for (psf=*ppsf; psf; psf = psf_next) {
1677 psf_next = psf->sf_next;
1678 if (psf->sf_crcount == 0) {
1679 if (psf_prev)
1680 psf_prev->sf_next = psf->sf_next;
1681 else
1682 *ppsf = psf->sf_next;
1683 kfree(psf);
1684 } else
1685 psf_prev = psf;
1686 }
1687}
1688
1689static void mld_send_cr(struct inet6_dev *idev)
1690{
1691 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1692 struct sk_buff *skb = NULL;
1693 int type, dtype;
1694
1695 read_lock_bh(&idev->lock);
1696 write_lock_bh(&idev->mc_lock);
1697
1698 /* deleted MCA's */
1699 pmc_prev = NULL;
1700 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1701 pmc_next = pmc->next;
1702 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1703 type = MLD2_BLOCK_OLD_SOURCES;
1704 dtype = MLD2_BLOCK_OLD_SOURCES;
1705 skb = add_grec(skb, pmc, type, 1, 0);
1706 skb = add_grec(skb, pmc, dtype, 1, 1);
1707 }
1708 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1710 type = MLD2_CHANGE_TO_INCLUDE;
1711 skb = add_grec(skb, pmc, type, 1, 0);
1712 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001713 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 if (pmc->mca_crcount == 0) {
1715 mld_clear_zeros(&pmc->mca_tomb);
1716 mld_clear_zeros(&pmc->mca_sources);
1717 }
1718 }
1719 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1720 !pmc->mca_sources) {
1721 if (pmc_prev)
1722 pmc_prev->next = pmc_next;
1723 else
1724 idev->mc_tomb = pmc_next;
1725 in6_dev_put(pmc->idev);
1726 kfree(pmc);
1727 } else
1728 pmc_prev = pmc;
1729 }
1730 write_unlock_bh(&idev->mc_lock);
1731
1732 /* change recs */
1733 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1734 spin_lock_bh(&pmc->mca_lock);
1735 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1736 type = MLD2_BLOCK_OLD_SOURCES;
1737 dtype = MLD2_ALLOW_NEW_SOURCES;
1738 } else {
1739 type = MLD2_ALLOW_NEW_SOURCES;
1740 dtype = MLD2_BLOCK_OLD_SOURCES;
1741 }
1742 skb = add_grec(skb, pmc, type, 0, 0);
1743 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1744
1745 /* filter mode changes */
1746 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1748 type = MLD2_CHANGE_TO_EXCLUDE;
1749 else
1750 type = MLD2_CHANGE_TO_INCLUDE;
1751 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001752 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
1754 spin_unlock_bh(&pmc->mca_lock);
1755 }
1756 read_unlock_bh(&idev->lock);
1757 if (!skb)
1758 return;
1759 (void) mld_sendpack(skb);
1760}
1761
1762static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1763{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001764 struct net *net = dev_net(dev);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001765 struct sock *sk = net->ipv6.igmp_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 struct inet6_dev *idev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001767 struct sk_buff *skb;
1768 struct icmp6hdr *hdr;
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001769 const struct in6_addr *snd_addr, *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 struct in6_addr *addrp;
1771 struct in6_addr addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 int err, len, payload_len, full_len;
1773 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1774 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1775 IPV6_TLV_PADN, 0 };
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001776 struct flowi fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001778 rcu_read_lock();
1779 IP6_INC_STATS(__in6_dev_get(dev),
1780 IPSTATS_MIB_OUTREQUESTS);
1781 rcu_read_unlock();
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09001782 if (type == ICMPV6_MGM_REDUCTION)
1783 snd_addr = &in6addr_linklocal_allrouters;
1784 else
1785 snd_addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1788 payload_len = len + sizeof(ra);
1789 full_len = sizeof(struct ipv6hdr) + payload_len;
1790
Johannes Bergf5184d22008-05-12 20:48:31 -07001791 skb = sock_alloc_send_skb(sk, LL_ALLOCATED_SPACE(dev) + full_len, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 if (skb == NULL) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001794 rcu_read_lock();
1795 IP6_INC_STATS(__in6_dev_get(dev),
1796 IPSTATS_MIB_OUTDISCARDS);
1797 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 return;
1799 }
1800
1801 skb_reserve(skb, LL_RESERVED_SPACE(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Neil Horman95c385b2007-04-25 17:08:10 -07001803 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 /* <draft-ietf-magma-mld-source-05.txt>:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001805 * use unspecified address as the source address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 * when a valid link-local address is not available.
1807 */
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001808 saddr = &in6addr_any;
1809 } else
1810 saddr = &addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001812 ip6_nd_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1815
1816 hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
1817 memset(hdr, 0, sizeof(struct icmp6hdr));
1818 hdr->icmp6_type = type;
1819
1820 addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
1821 ipv6_addr_copy(addrp, addr);
1822
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001823 hdr->icmp6_cksum = csum_ipv6_magic(saddr, snd_addr, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 IPPROTO_ICMPV6,
1825 csum_partial((__u8 *) hdr, len, 0));
1826
1827 idev = in6_dev_get(skb->dev);
1828
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001829 skb->dst = icmp6_dst_alloc(skb->dev, NULL, &ipv6_hdr(skb)->daddr);
1830 if (!skb->dst) {
1831 err = -ENOMEM;
1832 goto err_out;
1833 }
1834
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001835 icmpv6_flow_init(sk, &fl, type,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001836 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1837 skb->dev->ifindex);
1838
1839 err = xfrm_lookup(&skb->dst, &fl, NULL, 0);
1840 if (err)
1841 goto err_out;
1842
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001843 err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001844 dst_output);
1845out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 if (!err) {
David L Stevens14878f72007-09-16 16:52:35 -07001847 ICMP6MSGOUT_INC_STATS(idev, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTMSGS);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001849 IP6_INC_STATS(idev, IPSTATS_MIB_OUTMCASTPKTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 } else
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001851 IP6_INC_STATS(idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853 if (likely(idev != NULL))
1854 in6_dev_put(idev);
1855 return;
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001856
1857err_out:
1858 kfree_skb(skb);
1859 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860}
1861
1862static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1863 struct in6_addr *psfsrc)
1864{
1865 struct ip6_sf_list *psf, *psf_prev;
1866 int rv = 0;
1867
1868 psf_prev = NULL;
1869 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1870 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1871 break;
1872 psf_prev = psf;
1873 }
1874 if (!psf || psf->sf_count[sfmode] == 0) {
1875 /* source filter not found, or count wrong => bug */
1876 return -ESRCH;
1877 }
1878 psf->sf_count[sfmode]--;
1879 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1880 struct inet6_dev *idev = pmc->idev;
1881
1882 /* no more filters for this source */
1883 if (psf_prev)
1884 psf_prev->sf_next = psf->sf_next;
1885 else
1886 pmc->mca_sources = psf->sf_next;
1887 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1888 !MLD_V1_SEEN(idev)) {
1889 psf->sf_crcount = idev->mc_qrv;
1890 psf->sf_next = pmc->mca_tomb;
1891 pmc->mca_tomb = psf;
1892 rv = 1;
1893 } else
1894 kfree(psf);
1895 }
1896 return rv;
1897}
1898
1899static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
1900 int sfmode, int sfcount, struct in6_addr *psfsrc,
1901 int delta)
1902{
1903 struct ifmcaddr6 *pmc;
1904 int changerec = 0;
1905 int i, err;
1906
1907 if (!idev)
1908 return -ENODEV;
1909 read_lock_bh(&idev->lock);
1910 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1911 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1912 break;
1913 }
1914 if (!pmc) {
1915 /* MCA not found?? bug */
1916 read_unlock_bh(&idev->lock);
1917 return -ESRCH;
1918 }
1919 spin_lock_bh(&pmc->mca_lock);
1920 sf_markstate(pmc);
1921 if (!delta) {
1922 if (!pmc->mca_sfcount[sfmode]) {
1923 spin_unlock_bh(&pmc->mca_lock);
1924 read_unlock_bh(&idev->lock);
1925 return -EINVAL;
1926 }
1927 pmc->mca_sfcount[sfmode]--;
1928 }
1929 err = 0;
1930 for (i=0; i<sfcount; i++) {
1931 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1932
1933 changerec |= rv > 0;
1934 if (!err && rv < 0)
1935 err = rv;
1936 }
1937 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1938 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1939 pmc->mca_sfcount[MCAST_INCLUDE]) {
1940 struct ip6_sf_list *psf;
1941
1942 /* filter mode change */
1943 pmc->mca_sfmode = MCAST_INCLUDE;
1944 pmc->mca_crcount = idev->mc_qrv;
1945 idev->mc_ifc_count = pmc->mca_crcount;
1946 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1947 psf->sf_crcount = 0;
1948 mld_ifc_event(pmc->idev);
1949 } else if (sf_setstate(pmc) || changerec)
1950 mld_ifc_event(pmc->idev);
1951 spin_unlock_bh(&pmc->mca_lock);
1952 read_unlock_bh(&idev->lock);
1953 return err;
1954}
1955
1956/*
1957 * Add multicast single-source filter to the interface list
1958 */
1959static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1960 struct in6_addr *psfsrc, int delta)
1961{
1962 struct ip6_sf_list *psf, *psf_prev;
1963
1964 psf_prev = NULL;
1965 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1966 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1967 break;
1968 psf_prev = psf;
1969 }
1970 if (!psf) {
Ingo Oeser0c600ed2006-03-20 23:01:32 -08001971 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 if (!psf)
1973 return -ENOBUFS;
Ingo Oeser0c600ed2006-03-20 23:01:32 -08001974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 psf->sf_addr = *psfsrc;
1976 if (psf_prev) {
1977 psf_prev->sf_next = psf;
1978 } else
1979 pmc->mca_sources = psf;
1980 }
1981 psf->sf_count[sfmode]++;
1982 return 0;
1983}
1984
1985static void sf_markstate(struct ifmcaddr6 *pmc)
1986{
1987 struct ip6_sf_list *psf;
1988 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1989
1990 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1991 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1992 psf->sf_oldin = mca_xcount ==
1993 psf->sf_count[MCAST_EXCLUDE] &&
1994 !psf->sf_count[MCAST_INCLUDE];
1995 } else
1996 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1997}
1998
1999static int sf_setstate(struct ifmcaddr6 *pmc)
2000{
David L Stevens7add2a42006-01-24 13:06:39 -08002001 struct ip6_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2003 int qrv = pmc->idev->mc_qrv;
2004 int new_in, rv;
2005
2006 rv = 0;
2007 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
2008 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2009 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2010 !psf->sf_count[MCAST_INCLUDE];
2011 } else
2012 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevens7add2a42006-01-24 13:06:39 -08002013 if (new_in) {
2014 if (!psf->sf_oldin) {
Al Viroe80e28b2006-02-03 20:10:03 -05002015 struct ip6_sf_list *prev = NULL;
David L Stevens7add2a42006-01-24 13:06:39 -08002016
2017 for (dpsf=pmc->mca_tomb; dpsf;
2018 dpsf=dpsf->sf_next) {
2019 if (ipv6_addr_equal(&dpsf->sf_addr,
2020 &psf->sf_addr))
2021 break;
2022 prev = dpsf;
2023 }
2024 if (dpsf) {
2025 if (prev)
2026 prev->sf_next = dpsf->sf_next;
2027 else
2028 pmc->mca_tomb = dpsf->sf_next;
2029 kfree(dpsf);
2030 }
2031 psf->sf_crcount = qrv;
2032 rv++;
2033 }
2034 } else if (psf->sf_oldin) {
2035 psf->sf_crcount = 0;
2036 /*
2037 * add or update "delete" records if an active filter
2038 * is now inactive
2039 */
2040 for (dpsf=pmc->mca_tomb; dpsf; dpsf=dpsf->sf_next)
2041 if (ipv6_addr_equal(&dpsf->sf_addr,
2042 &psf->sf_addr))
2043 break;
2044 if (!dpsf) {
2045 dpsf = (struct ip6_sf_list *)
2046 kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2047 if (!dpsf)
2048 continue;
2049 *dpsf = *psf;
2050 /* pmc->mca_lock held by callers */
2051 dpsf->sf_next = pmc->mca_tomb;
2052 pmc->mca_tomb = dpsf;
2053 }
2054 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 rv++;
2056 }
2057 }
2058 return rv;
2059}
2060
2061/*
2062 * Add multicast source filter list to the interface list
2063 */
2064static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
2065 int sfmode, int sfcount, struct in6_addr *psfsrc,
2066 int delta)
2067{
2068 struct ifmcaddr6 *pmc;
2069 int isexclude;
2070 int i, err;
2071
2072 if (!idev)
2073 return -ENODEV;
2074 read_lock_bh(&idev->lock);
2075 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
2076 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2077 break;
2078 }
2079 if (!pmc) {
2080 /* MCA not found?? bug */
2081 read_unlock_bh(&idev->lock);
2082 return -ESRCH;
2083 }
2084 spin_lock_bh(&pmc->mca_lock);
2085
2086 sf_markstate(pmc);
2087 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2088 if (!delta)
2089 pmc->mca_sfcount[sfmode]++;
2090 err = 0;
2091 for (i=0; i<sfcount; i++) {
2092 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
2093 if (err)
2094 break;
2095 }
2096 if (err) {
2097 int j;
2098
2099 if (!delta)
2100 pmc->mca_sfcount[sfmode]--;
2101 for (j=0; j<i; j++)
2102 (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2103 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
2104 struct inet6_dev *idev = pmc->idev;
2105 struct ip6_sf_list *psf;
2106
2107 /* filter mode change */
2108 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2109 pmc->mca_sfmode = MCAST_EXCLUDE;
2110 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2111 pmc->mca_sfmode = MCAST_INCLUDE;
2112 /* else no filters; keep old mode for reports */
2113
2114 pmc->mca_crcount = idev->mc_qrv;
2115 idev->mc_ifc_count = pmc->mca_crcount;
2116 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
2117 psf->sf_crcount = 0;
2118 mld_ifc_event(idev);
2119 } else if (sf_setstate(pmc))
2120 mld_ifc_event(idev);
2121 spin_unlock_bh(&pmc->mca_lock);
2122 read_unlock_bh(&idev->lock);
2123 return err;
2124}
2125
2126static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2127{
2128 struct ip6_sf_list *psf, *nextpsf;
2129
2130 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
2131 nextpsf = psf->sf_next;
2132 kfree(psf);
2133 }
2134 pmc->mca_tomb = NULL;
2135 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
2136 nextpsf = psf->sf_next;
2137 kfree(psf);
2138 }
2139 pmc->mca_sources = NULL;
2140 pmc->mca_sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002141 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2143}
2144
2145
2146static void igmp6_join_group(struct ifmcaddr6 *ma)
2147{
2148 unsigned long delay;
2149
2150 if (ma->mca_flags & MAF_NOREPORT)
2151 return;
2152
2153 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2154
2155 delay = net_random() % IGMP6_UNSOLICITED_IVAL;
2156
2157 spin_lock_bh(&ma->mca_lock);
2158 if (del_timer(&ma->mca_timer)) {
2159 atomic_dec(&ma->mca_refcnt);
2160 delay = ma->mca_timer.expires - jiffies;
2161 }
2162
2163 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2164 atomic_inc(&ma->mca_refcnt);
2165 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2166 spin_unlock_bh(&ma->mca_lock);
2167}
2168
2169static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2170 struct inet6_dev *idev)
2171{
2172 int err;
2173
David L Stevens5ab4a6c2005-12-27 14:03:00 -08002174 /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
2175 * so no other readers or writers of iml or its sflist
2176 */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002177 if (!iml->sflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 /* any-source empty exclude case */
2179 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2180 }
2181 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2182 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2183 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2184 iml->sflist = NULL;
2185 return err;
2186}
2187
2188static void igmp6_leave_group(struct ifmcaddr6 *ma)
2189{
2190 if (MLD_V1_SEEN(ma->idev)) {
2191 if (ma->mca_flags & MAF_LAST_REPORTER)
2192 igmp6_send(&ma->mca_addr, ma->idev->dev,
2193 ICMPV6_MGM_REDUCTION);
2194 } else {
2195 mld_add_delrec(ma->idev, ma);
2196 mld_ifc_event(ma->idev);
2197 }
2198}
2199
2200static void mld_gq_timer_expire(unsigned long data)
2201{
2202 struct inet6_dev *idev = (struct inet6_dev *)data;
2203
2204 idev->mc_gq_running = 0;
2205 mld_send_report(idev, NULL);
2206 __in6_dev_put(idev);
2207}
2208
2209static void mld_ifc_timer_expire(unsigned long data)
2210{
2211 struct inet6_dev *idev = (struct inet6_dev *)data;
2212
2213 mld_send_cr(idev);
2214 if (idev->mc_ifc_count) {
2215 idev->mc_ifc_count--;
2216 if (idev->mc_ifc_count)
2217 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2218 }
2219 __in6_dev_put(idev);
2220}
2221
2222static void mld_ifc_event(struct inet6_dev *idev)
2223{
2224 if (MLD_V1_SEEN(idev))
2225 return;
2226 idev->mc_ifc_count = idev->mc_qrv;
2227 mld_ifc_start_timer(idev, 1);
2228}
2229
2230
2231static void igmp6_timer_handler(unsigned long data)
2232{
2233 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2234
2235 if (MLD_V1_SEEN(ma->idev))
2236 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2237 else
2238 mld_send_report(ma->idev, ma);
2239
2240 spin_lock(&ma->mca_lock);
2241 ma->mca_flags |= MAF_LAST_REPORTER;
2242 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2243 spin_unlock(&ma->mca_lock);
2244 ma_put(ma);
2245}
2246
2247/* Device going down */
2248
2249void ipv6_mc_down(struct inet6_dev *idev)
2250{
2251 struct ifmcaddr6 *i;
2252
2253 /* Withdraw multicast list */
2254
2255 read_lock_bh(&idev->lock);
2256 idev->mc_ifc_count = 0;
2257 if (del_timer(&idev->mc_ifc_timer))
2258 __in6_dev_put(idev);
2259 idev->mc_gq_running = 0;
2260 if (del_timer(&idev->mc_gq_timer))
2261 __in6_dev_put(idev);
2262
2263 for (i = idev->mc_list; i; i=i->next)
2264 igmp6_group_dropped(i);
2265 read_unlock_bh(&idev->lock);
2266
2267 mld_clear_delrec(idev);
2268}
2269
2270
2271/* Device going up */
2272
2273void ipv6_mc_up(struct inet6_dev *idev)
2274{
2275 struct ifmcaddr6 *i;
2276
2277 /* Install multicast list, except for all-nodes (already installed) */
2278
2279 read_lock_bh(&idev->lock);
2280 for (i = idev->mc_list; i; i=i->next)
2281 igmp6_group_added(i);
2282 read_unlock_bh(&idev->lock);
2283}
2284
2285/* IPv6 device initialization. */
2286
2287void ipv6_mc_init_dev(struct inet6_dev *idev)
2288{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 write_lock_bh(&idev->lock);
2290 rwlock_init(&idev->mc_lock);
2291 idev->mc_gq_running = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08002292 setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
2293 (unsigned long)idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 idev->mc_tomb = NULL;
2295 idev->mc_ifc_count = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08002296 setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
2297 (unsigned long)idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 idev->mc_qrv = MLD_QRV_DEFAULT;
2299 idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2300 idev->mc_v1_seen = 0;
2301 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302}
2303
2304/*
2305 * Device is about to be destroyed: clean up.
2306 */
2307
2308void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2309{
2310 struct ifmcaddr6 *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
2312 /* Deactivate timers */
2313 ipv6_mc_down(idev);
2314
2315 /* Delete all-nodes address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2317 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2318 * fail.
2319 */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002320 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002322 if (idev->cnf.forwarding)
2323 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
2325 write_lock_bh(&idev->lock);
2326 while ((i = idev->mc_list) != NULL) {
2327 idev->mc_list = i->next;
2328 write_unlock_bh(&idev->lock);
2329
2330 igmp6_group_dropped(i);
2331 ma_put(i);
2332
2333 write_lock_bh(&idev->lock);
2334 }
2335 write_unlock_bh(&idev->lock);
2336}
2337
2338#ifdef CONFIG_PROC_FS
2339struct igmp6_mc_iter_state {
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002340 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 struct net_device *dev;
2342 struct inet6_dev *idev;
2343};
2344
2345#define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2346
2347static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2348{
2349 struct ifmcaddr6 *im = NULL;
2350 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002351 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Pavel Emelianov7562f872007-05-03 15:13:45 -07002353 state->idev = NULL;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002354 for_each_netdev(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 struct inet6_dev *idev;
2356 idev = in6_dev_get(state->dev);
2357 if (!idev)
2358 continue;
2359 read_lock_bh(&idev->lock);
2360 im = idev->mc_list;
2361 if (im) {
2362 state->idev = idev;
2363 break;
2364 }
2365 read_unlock_bh(&idev->lock);
2366 in6_dev_put(idev);
2367 }
2368 return im;
2369}
2370
2371static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2372{
2373 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2374
2375 im = im->next;
2376 while (!im) {
2377 if (likely(state->idev != NULL)) {
2378 read_unlock_bh(&state->idev->lock);
2379 in6_dev_put(state->idev);
2380 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07002381 state->dev = next_net_device(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 if (!state->dev) {
2383 state->idev = NULL;
2384 break;
2385 }
2386 state->idev = in6_dev_get(state->dev);
2387 if (!state->idev)
2388 continue;
2389 read_lock_bh(&state->idev->lock);
2390 im = state->idev->mc_list;
2391 }
2392 return im;
2393}
2394
2395static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2396{
2397 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2398 if (im)
2399 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2400 --pos;
2401 return pos ? NULL : im;
2402}
2403
2404static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002405 __acquires(dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406{
2407 read_lock(&dev_base_lock);
2408 return igmp6_mc_get_idx(seq, *pos);
2409}
2410
2411static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2412{
2413 struct ifmcaddr6 *im;
2414 im = igmp6_mc_get_next(seq, v);
2415 ++*pos;
2416 return im;
2417}
2418
2419static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002420 __releases(dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421{
2422 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2423 if (likely(state->idev != NULL)) {
2424 read_unlock_bh(&state->idev->lock);
2425 in6_dev_put(state->idev);
2426 state->idev = NULL;
2427 }
2428 state->dev = NULL;
2429 read_unlock(&dev_base_lock);
2430}
2431
2432static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2433{
2434 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2435 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2436
2437 seq_printf(seq,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002438 "%-4d %-15s " NIP6_SEQFMT " %5d %08X %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 state->dev->ifindex, state->dev->name,
2440 NIP6(im->mca_addr),
2441 im->mca_users, im->mca_flags,
2442 (im->mca_flags&MAF_TIMER_RUNNING) ?
2443 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2444 return 0;
2445}
2446
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002447static const struct seq_operations igmp6_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 .start = igmp6_mc_seq_start,
2449 .next = igmp6_mc_seq_next,
2450 .stop = igmp6_mc_seq_stop,
2451 .show = igmp6_mc_seq_show,
2452};
2453
2454static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2455{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002456 return seq_open_net(inode, file, &igmp6_mc_seq_ops,
2457 sizeof(struct igmp6_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458}
2459
Arjan van de Ven9a321442007-02-12 00:55:35 -08002460static const struct file_operations igmp6_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 .owner = THIS_MODULE,
2462 .open = igmp6_mc_seq_open,
2463 .read = seq_read,
2464 .llseek = seq_lseek,
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002465 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466};
2467
2468struct igmp6_mcf_iter_state {
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002469 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 struct net_device *dev;
2471 struct inet6_dev *idev;
2472 struct ifmcaddr6 *im;
2473};
2474
2475#define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2476
2477static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2478{
2479 struct ip6_sf_list *psf = NULL;
2480 struct ifmcaddr6 *im = NULL;
2481 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002482 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Pavel Emelianov7562f872007-05-03 15:13:45 -07002484 state->idev = NULL;
2485 state->im = NULL;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002486 for_each_netdev(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 struct inet6_dev *idev;
2488 idev = in6_dev_get(state->dev);
2489 if (unlikely(idev == NULL))
2490 continue;
2491 read_lock_bh(&idev->lock);
2492 im = idev->mc_list;
2493 if (likely(im != NULL)) {
2494 spin_lock_bh(&im->mca_lock);
2495 psf = im->mca_sources;
2496 if (likely(psf != NULL)) {
2497 state->im = im;
2498 state->idev = idev;
2499 break;
2500 }
2501 spin_unlock_bh(&im->mca_lock);
2502 }
2503 read_unlock_bh(&idev->lock);
2504 in6_dev_put(idev);
2505 }
2506 return psf;
2507}
2508
2509static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2510{
2511 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2512
2513 psf = psf->sf_next;
2514 while (!psf) {
2515 spin_unlock_bh(&state->im->mca_lock);
2516 state->im = state->im->next;
2517 while (!state->im) {
2518 if (likely(state->idev != NULL)) {
2519 read_unlock_bh(&state->idev->lock);
2520 in6_dev_put(state->idev);
2521 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07002522 state->dev = next_net_device(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 if (!state->dev) {
2524 state->idev = NULL;
2525 goto out;
2526 }
2527 state->idev = in6_dev_get(state->dev);
2528 if (!state->idev)
2529 continue;
2530 read_lock_bh(&state->idev->lock);
2531 state->im = state->idev->mc_list;
2532 }
2533 if (!state->im)
2534 break;
2535 spin_lock_bh(&state->im->mca_lock);
2536 psf = state->im->mca_sources;
2537 }
2538out:
2539 return psf;
2540}
2541
2542static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2543{
2544 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2545 if (psf)
2546 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2547 --pos;
2548 return pos ? NULL : psf;
2549}
2550
2551static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002552 __acquires(dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553{
2554 read_lock(&dev_base_lock);
2555 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2556}
2557
2558static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2559{
2560 struct ip6_sf_list *psf;
2561 if (v == SEQ_START_TOKEN)
2562 psf = igmp6_mcf_get_first(seq);
2563 else
2564 psf = igmp6_mcf_get_next(seq, v);
2565 ++*pos;
2566 return psf;
2567}
2568
2569static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002570 __releases(dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
2572 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2573 if (likely(state->im != NULL)) {
2574 spin_unlock_bh(&state->im->mca_lock);
2575 state->im = NULL;
2576 }
2577 if (likely(state->idev != NULL)) {
2578 read_unlock_bh(&state->idev->lock);
2579 in6_dev_put(state->idev);
2580 state->idev = NULL;
2581 }
2582 state->dev = NULL;
2583 read_unlock(&dev_base_lock);
2584}
2585
2586static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2587{
2588 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2589 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2590
2591 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002592 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 "%3s %6s "
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002594 "%32s %32s %6s %6s\n", "Idx",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 "Device", "Multicast Address",
2596 "Source Address", "INC", "EXC");
2597 } else {
2598 seq_printf(seq,
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002599 "%3d %6.6s " NIP6_SEQFMT " " NIP6_SEQFMT " %6lu %6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 state->dev->ifindex, state->dev->name,
2601 NIP6(state->im->mca_addr),
2602 NIP6(psf->sf_addr),
2603 psf->sf_count[MCAST_INCLUDE],
2604 psf->sf_count[MCAST_EXCLUDE]);
2605 }
2606 return 0;
2607}
2608
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002609static const struct seq_operations igmp6_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 .start = igmp6_mcf_seq_start,
2611 .next = igmp6_mcf_seq_next,
2612 .stop = igmp6_mcf_seq_stop,
2613 .show = igmp6_mcf_seq_show,
2614};
2615
2616static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2617{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002618 return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
2619 sizeof(struct igmp6_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620}
2621
Arjan van de Ven9a321442007-02-12 00:55:35 -08002622static const struct file_operations igmp6_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 .owner = THIS_MODULE,
2624 .open = igmp6_mcf_seq_open,
2625 .read = seq_read,
2626 .llseek = seq_lseek,
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002627 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628};
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002629
2630static int igmp6_proc_init(struct net *net)
2631{
2632 int err;
2633
2634 err = -ENOMEM;
2635 if (!proc_net_fops_create(net, "igmp6", S_IRUGO, &igmp6_mc_seq_fops))
2636 goto out;
2637 if (!proc_net_fops_create(net, "mcfilter6", S_IRUGO,
2638 &igmp6_mcf_seq_fops))
2639 goto out_proc_net_igmp6;
2640
2641 err = 0;
2642out:
2643 return err;
2644
2645out_proc_net_igmp6:
2646 proc_net_remove(net, "igmp6");
2647 goto out;
2648}
2649
2650static void igmp6_proc_exit(struct net *net)
2651{
2652 proc_net_remove(net, "mcfilter6");
2653 proc_net_remove(net, "igmp6");
2654}
2655#else
2656static int igmp6_proc_init(struct net *net)
2657{
2658 return 0;
2659}
2660static void igmp6_proc_exit(struct net *net)
2661{
2662 ;
2663}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664#endif
2665
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002666static int igmp6_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 int err;
2669
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002670 err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
2671 SOCK_RAW, IPPROTO_ICMPV6, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 if (err < 0) {
2673 printk(KERN_ERR
2674 "Failed to initialize the IGMP6 control socket (err %d).\n",
2675 err);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002676 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 }
2678
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002679 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002681 err = igmp6_proc_init(net);
2682 if (err)
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002683 goto out_sock_create;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002684out:
2685 return err;
2686
2687out_sock_create:
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002688 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002689 goto out;
2690}
2691
2692static void igmp6_net_exit(struct net *net)
2693{
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002694 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002695 igmp6_proc_exit(net);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002696}
2697
2698static struct pernet_operations igmp6_net_ops = {
2699 .init = igmp6_net_init,
2700 .exit = igmp6_net_exit,
2701};
2702
2703int __init igmp6_init(void)
2704{
2705 return register_pernet_subsys(&igmp6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706}
2707
2708void igmp6_cleanup(void)
2709{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002710 unregister_pernet_subsys(&igmp6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711}