blob: f06ceea1ffa0f9371dce16459313db00a07ace4e [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)
306 return NULL;
307 idev = in6_dev_get(dev);
308 if (!idev) {
309 dev_put(dev);
310 return NULL;
311 }
312 read_lock_bh(&idev->lock);
313 if (idev->dead) {
314 read_unlock_bh(&idev->lock);
315 in6_dev_put(idev);
316 dev_put(dev);
317 return NULL;
318 }
319 return idev;
320}
321
322void ipv6_sock_mc_close(struct sock *sk)
323{
324 struct ipv6_pinfo *np = inet6_sk(sk);
325 struct ipv6_mc_socklist *mc_lst;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900326 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 write_lock_bh(&ipv6_sk_mc_lock);
329 while ((mc_lst = np->ipv6_mc_list) != NULL) {
330 struct net_device *dev;
331
332 np->ipv6_mc_list = mc_lst->next;
333 write_unlock_bh(&ipv6_sk_mc_lock);
334
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800335 dev = dev_get_by_index(net, mc_lst->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (dev) {
337 struct inet6_dev *idev = in6_dev_get(dev);
338
David L Stevensacd6e002006-08-17 16:27:39 -0700339 (void) ip6_mc_leave_src(sk, mc_lst, idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
342 in6_dev_put(idev);
343 }
344 dev_put(dev);
David L Stevensacd6e002006-08-17 16:27:39 -0700345 } else
346 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
349
350 write_lock_bh(&ipv6_sk_mc_lock);
351 }
352 write_unlock_bh(&ipv6_sk_mc_lock);
353}
354
355int ip6_mc_source(int add, int omode, struct sock *sk,
356 struct group_source_req *pgsr)
357{
358 struct in6_addr *source, *group;
359 struct ipv6_mc_socklist *pmc;
360 struct net_device *dev;
361 struct inet6_dev *idev;
362 struct ipv6_pinfo *inet6 = inet6_sk(sk);
363 struct ip6_sf_socklist *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900364 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 int i, j, rv;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700366 int leavegroup = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800367 int pmclocked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 int err;
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
371 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
372
373 if (!ipv6_addr_is_multicast(group))
374 return -EINVAL;
375
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800376 idev = ip6_mc_find_dev(net, group, pgsr->gsr_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (!idev)
378 return -ENODEV;
379 dev = idev->dev;
380
381 err = -EADDRNOTAVAIL;
382
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700383 read_lock_bh(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
385 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
386 continue;
387 if (ipv6_addr_equal(&pmc->addr, group))
388 break;
389 }
David L Stevens917f2f12005-07-08 17:45:16 -0700390 if (!pmc) { /* must have a prior join */
391 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /* if a source filter was set, must be the same mode as before */
395 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -0700396 if (pmc->sfmode != omode) {
397 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 } else if (pmc->sfmode != omode) {
401 /* allow mode switches for empty-set filters */
402 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
403 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
404 pmc->sfmode = omode;
405 }
406
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800407 write_lock_bh(&pmc->sflock);
408 pmclocked = 1;
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 psl = pmc->sflist;
411 if (!add) {
412 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -0700413 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 rv = !0;
415 for (i=0; i<psl->sl_count; i++) {
416 rv = memcmp(&psl->sl_addr[i], source,
417 sizeof(struct in6_addr));
418 if (rv == 0)
419 break;
420 }
421 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -0700422 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700424 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
425 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
426 leavegroup = 1;
427 goto done;
428 }
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /* update the interface filter */
431 ip6_mc_del_src(idev, group, omode, 1, source, 1);
432
433 for (j=i+1; j<psl->sl_count; j++)
434 psl->sl_addr[j-1] = psl->sl_addr[j];
435 psl->sl_count--;
436 err = 0;
437 goto done;
438 }
439 /* else, add a new source to the filter */
440
441 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
442 err = -ENOBUFS;
443 goto done;
444 }
445 if (!psl || psl->sl_count == psl->sl_max) {
446 struct ip6_sf_socklist *newpsl;
447 int count = IP6_SFBLOCK;
448
449 if (psl)
450 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800451 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (!newpsl) {
453 err = -ENOBUFS;
454 goto done;
455 }
456 newpsl->sl_max = count;
457 newpsl->sl_count = count - IP6_SFBLOCK;
458 if (psl) {
459 for (i=0; i<psl->sl_count; i++)
460 newpsl->sl_addr[i] = psl->sl_addr[i];
461 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
462 }
463 pmc->sflist = psl = newpsl;
464 }
465 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
466 for (i=0; i<psl->sl_count; i++) {
467 rv = memcmp(&psl->sl_addr[i], source, sizeof(struct in6_addr));
468 if (rv == 0)
469 break;
470 }
471 if (rv == 0) /* address already there is an error */
472 goto done;
473 for (j=psl->sl_count-1; j>=i; j--)
474 psl->sl_addr[j+1] = psl->sl_addr[j];
475 psl->sl_addr[i] = *source;
476 psl->sl_count++;
477 err = 0;
478 /* update the interface list */
479 ip6_mc_add_src(idev, group, omode, 1, source, 1);
480done:
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800481 if (pmclocked)
482 write_unlock_bh(&pmc->sflock);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700483 read_unlock_bh(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 read_unlock_bh(&idev->lock);
485 in6_dev_put(idev);
486 dev_put(dev);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700487 if (leavegroup)
488 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return err;
490}
491
492int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
493{
494 struct in6_addr *group;
495 struct ipv6_mc_socklist *pmc;
496 struct net_device *dev;
497 struct inet6_dev *idev;
498 struct ipv6_pinfo *inet6 = inet6_sk(sk);
499 struct ip6_sf_socklist *newpsl, *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900500 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -0700501 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 int i, err;
503
504 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
505
506 if (!ipv6_addr_is_multicast(group))
507 return -EINVAL;
508 if (gsf->gf_fmode != MCAST_INCLUDE &&
509 gsf->gf_fmode != MCAST_EXCLUDE)
510 return -EINVAL;
511
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800512 idev = ip6_mc_find_dev(net, group, gsf->gf_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 if (!idev)
515 return -ENODEV;
516 dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
David S. Miller9c059892005-07-08 21:44:39 -0700518 err = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800519 read_lock_bh(&ipv6_sk_mc_lock);
520
David L Stevens9951f032005-07-08 17:47:28 -0700521 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
522 leavegroup = 1;
523 goto done;
524 }
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
527 if (pmc->ifindex != gsf->gf_interface)
528 continue;
529 if (ipv6_addr_equal(&pmc->addr, group))
530 break;
531 }
David L Stevens917f2f12005-07-08 17:45:16 -0700532 if (!pmc) { /* must have a prior join */
533 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (gsf->gf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800537 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
538 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (!newpsl) {
540 err = -ENOBUFS;
541 goto done;
542 }
543 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
544 for (i=0; i<newpsl->sl_count; ++i) {
545 struct sockaddr_in6 *psin6;
546
547 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
548 newpsl->sl_addr[i] = psin6->sin6_addr;
549 }
550 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
551 newpsl->sl_count, newpsl->sl_addr, 0);
552 if (err) {
553 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
554 goto done;
555 }
Yan Zheng8713dbf2005-10-28 08:02:08 +0800556 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +0800558 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
559 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800560
561 write_lock_bh(&pmc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 psl = pmc->sflist;
563 if (psl) {
564 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
565 psl->sl_count, psl->sl_addr, 0);
566 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
567 } else
568 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
569 pmc->sflist = newpsl;
570 pmc->sfmode = gsf->gf_fmode;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800571 write_unlock_bh(&pmc->sflock);
David L Stevens917f2f12005-07-08 17:45:16 -0700572 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573done:
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800574 read_unlock_bh(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 read_unlock_bh(&idev->lock);
576 in6_dev_put(idev);
577 dev_put(dev);
David L Stevens9951f032005-07-08 17:47:28 -0700578 if (leavegroup)
579 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return err;
581}
582
583int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
584 struct group_filter __user *optval, int __user *optlen)
585{
586 int err, i, count, copycount;
587 struct in6_addr *group;
588 struct ipv6_mc_socklist *pmc;
589 struct inet6_dev *idev;
590 struct net_device *dev;
591 struct ipv6_pinfo *inet6 = inet6_sk(sk);
592 struct ip6_sf_socklist *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900593 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
596
597 if (!ipv6_addr_is_multicast(group))
598 return -EINVAL;
599
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800600 idev = ip6_mc_find_dev(net, group, gsf->gf_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 if (!idev)
603 return -ENODEV;
604
605 dev = idev->dev;
606
607 err = -EADDRNOTAVAIL;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800608 /*
609 * changes to the ipv6_mc_list require the socket lock and
610 * a read lock on ip6_sk_mc_lock. We have the socket lock,
611 * so reading the list is safe.
612 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
615 if (pmc->ifindex != gsf->gf_interface)
616 continue;
617 if (ipv6_addr_equal(group, &pmc->addr))
618 break;
619 }
620 if (!pmc) /* must have a prior join */
621 goto done;
622 gsf->gf_fmode = pmc->sfmode;
623 psl = pmc->sflist;
624 count = psl ? psl->sl_count : 0;
625 read_unlock_bh(&idev->lock);
626 in6_dev_put(idev);
627 dev_put(dev);
628
629 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
630 gsf->gf_numsrc = count;
631 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
632 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
633 return -EFAULT;
634 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800635 /* changes to psl require the socket lock, a read lock on
636 * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
637 * have the socket lock, so reading here is safe.
638 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 for (i=0; i<copycount; i++) {
640 struct sockaddr_in6 *psin6;
641 struct sockaddr_storage ss;
642
643 psin6 = (struct sockaddr_in6 *)&ss;
644 memset(&ss, 0, sizeof(ss));
645 psin6->sin6_family = AF_INET6;
646 psin6->sin6_addr = psl->sl_addr[i];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900647 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return -EFAULT;
649 }
650 return 0;
651done:
652 read_unlock_bh(&idev->lock);
653 in6_dev_put(idev);
654 dev_put(dev);
655 return err;
656}
657
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900658int inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
659 const struct in6_addr *src_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 struct ipv6_pinfo *np = inet6_sk(sk);
662 struct ipv6_mc_socklist *mc;
663 struct ip6_sf_socklist *psl;
664 int rv = 1;
665
666 read_lock(&ipv6_sk_mc_lock);
667 for (mc = np->ipv6_mc_list; mc; mc = mc->next) {
668 if (ipv6_addr_equal(&mc->addr, mc_addr))
669 break;
670 }
671 if (!mc) {
672 read_unlock(&ipv6_sk_mc_lock);
673 return 1;
674 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800675 read_lock(&mc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 psl = mc->sflist;
677 if (!psl) {
678 rv = mc->sfmode == MCAST_EXCLUDE;
679 } else {
680 int i;
681
682 for (i=0; i<psl->sl_count; i++) {
683 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
684 break;
685 }
686 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
687 rv = 0;
688 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
689 rv = 0;
690 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800691 read_unlock(&mc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 read_unlock(&ipv6_sk_mc_lock);
693
694 return rv;
695}
696
697static void ma_put(struct ifmcaddr6 *mc)
698{
699 if (atomic_dec_and_test(&mc->mca_refcnt)) {
700 in6_dev_put(mc->idev);
701 kfree(mc);
702 }
703}
704
705static void igmp6_group_added(struct ifmcaddr6 *mc)
706{
707 struct net_device *dev = mc->idev->dev;
708 char buf[MAX_ADDR_LEN];
709
710 spin_lock_bh(&mc->mca_lock);
711 if (!(mc->mca_flags&MAF_LOADED)) {
712 mc->mca_flags |= MAF_LOADED;
713 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
714 dev_mc_add(dev, buf, dev->addr_len, 0);
715 }
716 spin_unlock_bh(&mc->mca_lock);
717
718 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
719 return;
720
721 if (MLD_V1_SEEN(mc->idev)) {
722 igmp6_join_group(mc);
723 return;
724 }
725 /* else v2 */
726
727 mc->mca_crcount = mc->idev->mc_qrv;
728 mld_ifc_event(mc->idev);
729}
730
731static void igmp6_group_dropped(struct ifmcaddr6 *mc)
732{
733 struct net_device *dev = mc->idev->dev;
734 char buf[MAX_ADDR_LEN];
735
736 spin_lock_bh(&mc->mca_lock);
737 if (mc->mca_flags&MAF_LOADED) {
738 mc->mca_flags &= ~MAF_LOADED;
739 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
740 dev_mc_delete(dev, buf, dev->addr_len, 0);
741 }
742
743 if (mc->mca_flags & MAF_NOREPORT)
744 goto done;
745 spin_unlock_bh(&mc->mca_lock);
746
747 if (!mc->idev->dead)
748 igmp6_leave_group(mc);
749
750 spin_lock_bh(&mc->mca_lock);
751 if (del_timer(&mc->mca_timer))
752 atomic_dec(&mc->mca_refcnt);
753done:
754 ip6_mc_clear_src(mc);
755 spin_unlock_bh(&mc->mca_lock);
756}
757
758/*
759 * deleted ifmcaddr6 manipulation
760 */
761static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
762{
763 struct ifmcaddr6 *pmc;
764
765 /* this is an "ifmcaddr6" for convenience; only the fields below
766 * are actually used. In particular, the refcnt and users are not
767 * used for management of the delete list. Using the same structure
768 * for deleted items allows change reports to use common code with
769 * non-deleted or query-response MCA's.
770 */
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800771 pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (!pmc)
773 return;
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 spin_lock_bh(&im->mca_lock);
776 spin_lock_init(&pmc->mca_lock);
777 pmc->idev = im->idev;
778 in6_dev_hold(idev);
779 pmc->mca_addr = im->mca_addr;
780 pmc->mca_crcount = idev->mc_qrv;
781 pmc->mca_sfmode = im->mca_sfmode;
782 if (pmc->mca_sfmode == MCAST_INCLUDE) {
783 struct ip6_sf_list *psf;
784
785 pmc->mca_tomb = im->mca_tomb;
786 pmc->mca_sources = im->mca_sources;
787 im->mca_tomb = im->mca_sources = NULL;
788 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
789 psf->sf_crcount = pmc->mca_crcount;
790 }
791 spin_unlock_bh(&im->mca_lock);
792
793 write_lock_bh(&idev->mc_lock);
794 pmc->next = idev->mc_tomb;
795 idev->mc_tomb = pmc;
796 write_unlock_bh(&idev->mc_lock);
797}
798
799static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *pmca)
800{
801 struct ifmcaddr6 *pmc, *pmc_prev;
802 struct ip6_sf_list *psf, *psf_next;
803
804 write_lock_bh(&idev->mc_lock);
805 pmc_prev = NULL;
806 for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
807 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
808 break;
809 pmc_prev = pmc;
810 }
811 if (pmc) {
812 if (pmc_prev)
813 pmc_prev->next = pmc->next;
814 else
815 idev->mc_tomb = pmc->next;
816 }
817 write_unlock_bh(&idev->mc_lock);
818 if (pmc) {
819 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
820 psf_next = psf->sf_next;
821 kfree(psf);
822 }
823 in6_dev_put(pmc->idev);
824 kfree(pmc);
825 }
826}
827
828static void mld_clear_delrec(struct inet6_dev *idev)
829{
830 struct ifmcaddr6 *pmc, *nextpmc;
831
832 write_lock_bh(&idev->mc_lock);
833 pmc = idev->mc_tomb;
834 idev->mc_tomb = NULL;
835 write_unlock_bh(&idev->mc_lock);
836
837 for (; pmc; pmc = nextpmc) {
838 nextpmc = pmc->next;
839 ip6_mc_clear_src(pmc);
840 in6_dev_put(pmc->idev);
841 kfree(pmc);
842 }
843
844 /* clear dead sources, too */
845 read_lock_bh(&idev->lock);
846 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
847 struct ip6_sf_list *psf, *psf_next;
848
849 spin_lock_bh(&pmc->mca_lock);
850 psf = pmc->mca_tomb;
851 pmc->mca_tomb = NULL;
852 spin_unlock_bh(&pmc->mca_lock);
853 for (; psf; psf=psf_next) {
854 psf_next = psf->sf_next;
855 kfree(psf);
856 }
857 }
858 read_unlock_bh(&idev->lock);
859}
860
861
862/*
863 * device multicast group inc (add if not found)
864 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900865int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
867 struct ifmcaddr6 *mc;
868 struct inet6_dev *idev;
869
870 idev = in6_dev_get(dev);
871
872 if (idev == NULL)
873 return -EINVAL;
874
875 write_lock_bh(&idev->lock);
876 if (idev->dead) {
877 write_unlock_bh(&idev->lock);
878 in6_dev_put(idev);
879 return -ENODEV;
880 }
881
882 for (mc = idev->mc_list; mc; mc = mc->next) {
883 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
884 mc->mca_users++;
885 write_unlock_bh(&idev->lock);
886 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
887 NULL, 0);
888 in6_dev_put(idev);
889 return 0;
890 }
891 }
892
893 /*
894 * not found: create a new one.
895 */
896
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800897 mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 if (mc == NULL) {
900 write_unlock_bh(&idev->lock);
901 in6_dev_put(idev);
902 return -ENOMEM;
903 }
904
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800905 setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 ipv6_addr_copy(&mc->mca_addr, addr);
908 mc->idev = idev;
909 mc->mca_users = 1;
910 /* mca_stamp should be updated upon changes */
911 mc->mca_cstamp = mc->mca_tstamp = jiffies;
912 atomic_set(&mc->mca_refcnt, 2);
913 spin_lock_init(&mc->mca_lock);
914
915 /* initial mode is (EX, empty) */
916 mc->mca_sfmode = MCAST_EXCLUDE;
917 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
918
919 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
920 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
921 mc->mca_flags |= MAF_NOREPORT;
922
923 mc->next = idev->mc_list;
924 idev->mc_list = mc;
925 write_unlock_bh(&idev->lock);
926
927 mld_del_delrec(idev, &mc->mca_addr);
928 igmp6_group_added(mc);
929 ma_put(mc);
930 return 0;
931}
932
933/*
934 * device multicast group del
935 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900936int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
938 struct ifmcaddr6 *ma, **map;
939
940 write_lock_bh(&idev->lock);
941 for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
942 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
943 if (--ma->mca_users == 0) {
944 *map = ma->next;
945 write_unlock_bh(&idev->lock);
946
947 igmp6_group_dropped(ma);
948
949 ma_put(ma);
950 return 0;
951 }
952 write_unlock_bh(&idev->lock);
953 return 0;
954 }
955 }
956 write_unlock_bh(&idev->lock);
957
958 return -ENOENT;
959}
960
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900961int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
963 struct inet6_dev *idev = in6_dev_get(dev);
964 int err;
965
966 if (!idev)
967 return -ENODEV;
968
969 err = __ipv6_dev_mc_dec(idev, addr);
970
971 in6_dev_put(idev);
972
973 return err;
974}
975
976/*
977 * identify MLD packets for MLD filter exceptions
978 */
979int ipv6_is_mld(struct sk_buff *skb, int nexthdr)
980{
981 struct icmp6hdr *pic;
982
983 if (nexthdr != IPPROTO_ICMPV6)
984 return 0;
985
986 if (!pskb_may_pull(skb, sizeof(struct icmp6hdr)))
987 return 0;
988
Arnaldo Carvalho de Melocc70ab22007-03-13 14:03:22 -0300989 pic = icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 switch (pic->icmp6_type) {
992 case ICMPV6_MGM_QUERY:
993 case ICMPV6_MGM_REPORT:
994 case ICMPV6_MGM_REDUCTION:
995 case ICMPV6_MLD2_REPORT:
996 return 1;
997 default:
998 break;
999 }
1000 return 0;
1001}
1002
1003/*
1004 * check if the interface/address pair is valid
1005 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001006int ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
1007 const struct in6_addr *src_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
1009 struct inet6_dev *idev;
1010 struct ifmcaddr6 *mc;
1011 int rv = 0;
1012
1013 idev = in6_dev_get(dev);
1014 if (idev) {
1015 read_lock_bh(&idev->lock);
1016 for (mc = idev->mc_list; mc; mc=mc->next) {
1017 if (ipv6_addr_equal(&mc->mca_addr, group))
1018 break;
1019 }
1020 if (mc) {
1021 if (src_addr && !ipv6_addr_any(src_addr)) {
1022 struct ip6_sf_list *psf;
1023
1024 spin_lock_bh(&mc->mca_lock);
1025 for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
1026 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
1027 break;
1028 }
1029 if (psf)
1030 rv = psf->sf_count[MCAST_INCLUDE] ||
1031 psf->sf_count[MCAST_EXCLUDE] !=
1032 mc->mca_sfcount[MCAST_EXCLUDE];
1033 else
1034 rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
1035 spin_unlock_bh(&mc->mca_lock);
1036 } else
1037 rv = 1; /* don't filter unspecified source */
1038 }
1039 read_unlock_bh(&idev->lock);
1040 in6_dev_put(idev);
1041 }
1042 return rv;
1043}
1044
1045static void mld_gq_start_timer(struct inet6_dev *idev)
1046{
1047 int tv = net_random() % idev->mc_maxdelay;
1048
1049 idev->mc_gq_running = 1;
1050 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
1051 in6_dev_hold(idev);
1052}
1053
1054static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
1055{
1056 int tv = net_random() % delay;
1057
1058 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1059 in6_dev_hold(idev);
1060}
1061
1062/*
1063 * IGMP handling (alias multicast ICMPv6 messages)
1064 */
1065
1066static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1067{
1068 unsigned long delay = resptime;
1069
1070 /* Do not start timer for these addresses */
1071 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1072 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1073 return;
1074
1075 if (del_timer(&ma->mca_timer)) {
1076 atomic_dec(&ma->mca_refcnt);
1077 delay = ma->mca_timer.expires - jiffies;
1078 }
1079
1080 if (delay >= resptime) {
1081 if (resptime)
1082 delay = net_random() % resptime;
1083 else
1084 delay = 1;
1085 }
1086 ma->mca_timer.expires = jiffies + delay;
1087 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1088 atomic_inc(&ma->mca_refcnt);
1089 ma->mca_flags |= MAF_TIMER_RUNNING;
1090}
1091
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001092/* mark EXCLUDE-mode sources */
1093static int mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 struct in6_addr *srcs)
1095{
1096 struct ip6_sf_list *psf;
1097 int i, scount;
1098
1099 scount = 0;
1100 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1101 if (scount == nsrcs)
1102 break;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001103 for (i=0; i<nsrcs; i++) {
1104 /* skip inactive filters */
1105 if (pmc->mca_sfcount[MCAST_INCLUDE] ||
1106 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1107 psf->sf_count[MCAST_EXCLUDE])
1108 continue;
1109 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1110 scount++;
1111 break;
1112 }
1113 }
1114 }
1115 pmc->mca_flags &= ~MAF_GSQUERY;
1116 if (scount == nsrcs) /* all sources excluded */
1117 return 0;
1118 return 1;
1119}
1120
1121static int mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1122 struct in6_addr *srcs)
1123{
1124 struct ip6_sf_list *psf;
1125 int i, scount;
1126
1127 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1128 return mld_xmarksources(pmc, nsrcs, srcs);
1129
1130 /* mark INCLUDE-mode sources */
1131
1132 scount = 0;
1133 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1134 if (scount == nsrcs)
1135 break;
1136 for (i=0; i<nsrcs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1138 psf->sf_gsresp = 1;
1139 scount++;
1140 break;
1141 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001144 if (!scount) {
1145 pmc->mca_flags &= ~MAF_GSQUERY;
1146 return 0;
1147 }
1148 pmc->mca_flags |= MAF_GSQUERY;
1149 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
1151
1152int igmp6_event_query(struct sk_buff *skb)
1153{
Yan Zheng97300b52005-10-31 20:09:45 +08001154 struct mld2_query *mlh2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 struct ifmcaddr6 *ma;
1156 struct in6_addr *group;
1157 unsigned long max_delay;
1158 struct inet6_dev *idev;
1159 struct icmp6hdr *hdr;
1160 int group_type;
1161 int mark = 0;
1162 int len;
1163
1164 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1165 return -EINVAL;
1166
1167 /* compute payload length excluding extension headers */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001168 len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001169 len -= skb_network_header_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 /* Drop queries with not link local source */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001172 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 return -EINVAL;
1174
1175 idev = in6_dev_get(skb->dev);
1176
1177 if (idev == NULL)
1178 return 0;
1179
Arnaldo Carvalho de Melocc70ab22007-03-13 14:03:22 -03001180 hdr = icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 group = (struct in6_addr *) (hdr + 1);
1182 group_type = ipv6_addr_type(group);
1183
1184 if (group_type != IPV6_ADDR_ANY &&
1185 !(group_type&IPV6_ADDR_MULTICAST)) {
1186 in6_dev_put(idev);
1187 return -EINVAL;
1188 }
1189
1190 if (len == 24) {
1191 int switchback;
1192 /* MLDv1 router present */
1193
1194 /* Translate milliseconds to jiffies */
1195 max_delay = (ntohs(hdr->icmp6_maxdelay)*HZ)/1000;
1196
1197 switchback = (idev->mc_qrv + 1) * max_delay;
1198 idev->mc_v1_seen = jiffies + switchback;
1199
1200 /* cancel the interface change timer */
1201 idev->mc_ifc_count = 0;
1202 if (del_timer(&idev->mc_ifc_timer))
1203 __in6_dev_put(idev);
1204 /* clear deleted report items */
1205 mld_clear_delrec(idev);
1206 } else if (len >= 28) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001207 int srcs_offset = sizeof(struct mld2_query) -
Yan Zheng97300b52005-10-31 20:09:45 +08001208 sizeof(struct icmp6hdr);
1209 if (!pskb_may_pull(skb, srcs_offset)) {
1210 in6_dev_put(idev);
1211 return -EINVAL;
1212 }
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001213 mlh2 = (struct mld2_query *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 max_delay = (MLDV2_MRC(ntohs(mlh2->mrc))*HZ)/1000;
1215 if (!max_delay)
1216 max_delay = 1;
1217 idev->mc_maxdelay = max_delay;
1218 if (mlh2->qrv)
1219 idev->mc_qrv = mlh2->qrv;
1220 if (group_type == IPV6_ADDR_ANY) { /* general query */
1221 if (mlh2->nsrcs) {
1222 in6_dev_put(idev);
1223 return -EINVAL; /* no sources allowed */
1224 }
1225 mld_gq_start_timer(idev);
1226 in6_dev_put(idev);
1227 return 0;
1228 }
1229 /* mark sources to include, if group & source-specific */
Yan Zheng97300b52005-10-31 20:09:45 +08001230 if (mlh2->nsrcs != 0) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001231 if (!pskb_may_pull(skb, srcs_offset +
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001232 ntohs(mlh2->nsrcs) * sizeof(struct in6_addr))) {
Yan Zheng97300b52005-10-31 20:09:45 +08001233 in6_dev_put(idev);
1234 return -EINVAL;
1235 }
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001236 mlh2 = (struct mld2_query *)skb_transport_header(skb);
Yan Zheng97300b52005-10-31 20:09:45 +08001237 mark = 1;
1238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 } else {
1240 in6_dev_put(idev);
1241 return -EINVAL;
1242 }
1243
1244 read_lock_bh(&idev->lock);
1245 if (group_type == IPV6_ADDR_ANY) {
1246 for (ma = idev->mc_list; ma; ma=ma->next) {
1247 spin_lock_bh(&ma->mca_lock);
1248 igmp6_group_queried(ma, max_delay);
1249 spin_unlock_bh(&ma->mca_lock);
1250 }
1251 } else {
1252 for (ma = idev->mc_list; ma; ma=ma->next) {
David L Stevens7add2a42006-01-24 13:06:39 -08001253 if (!ipv6_addr_equal(group, &ma->mca_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 continue;
1255 spin_lock_bh(&ma->mca_lock);
1256 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1257 /* gsquery <- gsquery && mark */
1258 if (!mark)
1259 ma->mca_flags &= ~MAF_GSQUERY;
1260 } else {
1261 /* gsquery <- mark */
1262 if (mark)
1263 ma->mca_flags |= MAF_GSQUERY;
1264 else
1265 ma->mca_flags &= ~MAF_GSQUERY;
1266 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001267 if (!(ma->mca_flags & MAF_GSQUERY) ||
David L Stevens7add2a42006-01-24 13:06:39 -08001268 mld_marksources(ma, ntohs(mlh2->nsrcs), mlh2->srcs))
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001269 igmp6_group_queried(ma, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 spin_unlock_bh(&ma->mca_lock);
David L Stevens7add2a42006-01-24 13:06:39 -08001271 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 }
1273 }
1274 read_unlock_bh(&idev->lock);
1275 in6_dev_put(idev);
1276
1277 return 0;
1278}
1279
1280
1281int igmp6_event_report(struct sk_buff *skb)
1282{
1283 struct ifmcaddr6 *ma;
1284 struct in6_addr *addrp;
1285 struct inet6_dev *idev;
1286 struct icmp6hdr *hdr;
1287 int addr_type;
1288
1289 /* Our own report looped back. Ignore it. */
1290 if (skb->pkt_type == PACKET_LOOPBACK)
1291 return 0;
1292
David Stevens24c69272005-12-02 20:32:59 -08001293 /* send our report if the MC router may not have heard this report */
1294 if (skb->pkt_type != PACKET_MULTICAST &&
1295 skb->pkt_type != PACKET_BROADCAST)
1296 return 0;
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1299 return -EINVAL;
1300
Arnaldo Carvalho de Melocc70ab22007-03-13 14:03:22 -03001301 hdr = icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 /* Drop reports with not link local source */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001304 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001305 if (addr_type != IPV6_ADDR_ANY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 !(addr_type&IPV6_ADDR_LINKLOCAL))
1307 return -EINVAL;
1308
1309 addrp = (struct in6_addr *) (hdr + 1);
1310
1311 idev = in6_dev_get(skb->dev);
1312 if (idev == NULL)
1313 return -ENODEV;
1314
1315 /*
1316 * Cancel the timer for this group
1317 */
1318
1319 read_lock_bh(&idev->lock);
1320 for (ma = idev->mc_list; ma; ma=ma->next) {
1321 if (ipv6_addr_equal(&ma->mca_addr, addrp)) {
1322 spin_lock(&ma->mca_lock);
1323 if (del_timer(&ma->mca_timer))
1324 atomic_dec(&ma->mca_refcnt);
1325 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1326 spin_unlock(&ma->mca_lock);
1327 break;
1328 }
1329 }
1330 read_unlock_bh(&idev->lock);
1331 in6_dev_put(idev);
1332 return 0;
1333}
1334
1335static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1336 int gdeleted, int sdeleted)
1337{
1338 switch (type) {
1339 case MLD2_MODE_IS_INCLUDE:
1340 case MLD2_MODE_IS_EXCLUDE:
1341 if (gdeleted || sdeleted)
1342 return 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001343 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1344 if (pmc->mca_sfmode == MCAST_INCLUDE)
1345 return 1;
1346 /* don't include if this source is excluded
1347 * in all filters
1348 */
1349 if (psf->sf_count[MCAST_INCLUDE])
David L Stevens7add2a42006-01-24 13:06:39 -08001350 return type == MLD2_MODE_IS_INCLUDE;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001351 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1352 psf->sf_count[MCAST_EXCLUDE];
1353 }
1354 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 case MLD2_CHANGE_TO_INCLUDE:
1356 if (gdeleted || sdeleted)
1357 return 0;
1358 return psf->sf_count[MCAST_INCLUDE] != 0;
1359 case MLD2_CHANGE_TO_EXCLUDE:
1360 if (gdeleted || sdeleted)
1361 return 0;
1362 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1363 psf->sf_count[MCAST_INCLUDE])
1364 return 0;
1365 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1366 psf->sf_count[MCAST_EXCLUDE];
1367 case MLD2_ALLOW_NEW_SOURCES:
1368 if (gdeleted || !psf->sf_crcount)
1369 return 0;
1370 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1371 case MLD2_BLOCK_OLD_SOURCES:
1372 if (pmc->mca_sfmode == MCAST_INCLUDE)
1373 return gdeleted || (psf->sf_crcount && sdeleted);
1374 return psf->sf_crcount && !gdeleted && !sdeleted;
1375 }
1376 return 0;
1377}
1378
1379static int
1380mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1381{
1382 struct ip6_sf_list *psf;
1383 int scount = 0;
1384
1385 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1386 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1387 continue;
1388 scount++;
1389 }
1390 return scount;
1391}
1392
1393static struct sk_buff *mld_newpack(struct net_device *dev, int size)
1394{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001395 struct net *net = dev_net(dev);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001396 struct sock *sk = net->ipv6.igmp_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 struct sk_buff *skb;
1398 struct mld2_report *pmr;
1399 struct in6_addr addr_buf;
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001400 const struct in6_addr *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 int err;
1402 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1403 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1404 IPV6_TLV_PADN, 0 };
1405
1406 /* we assume size > sizeof(ra) here */
Johannes Bergf5184d22008-05-12 20:48:31 -07001407 skb = sock_alloc_send_skb(sk, size + LL_ALLOCATED_SPACE(dev), 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001409 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return NULL;
1411
1412 skb_reserve(skb, LL_RESERVED_SPACE(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Neil Horman95c385b2007-04-25 17:08:10 -07001414 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 /* <draft-ietf-magma-mld-source-05.txt>:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001416 * use unspecified address as the source address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 * when a valid link-local address is not available.
1418 */
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001419 saddr = &in6addr_any;
1420 } else
1421 saddr = &addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001423 ip6_nd_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1426
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001427 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -03001428 skb_put(skb, sizeof(*pmr));
1429 pmr = (struct mld2_report *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 pmr->type = ICMPV6_MLD2_REPORT;
1431 pmr->resv1 = 0;
1432 pmr->csum = 0;
1433 pmr->resv2 = 0;
1434 pmr->ngrec = 0;
1435 return skb;
1436}
1437
1438static void mld_sendpack(struct sk_buff *skb)
1439{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001440 struct ipv6hdr *pip6 = ipv6_hdr(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001441 struct mld2_report *pmr =
1442 (struct mld2_report *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 int payload_len, mldlen;
1444 struct inet6_dev *idev = in6_dev_get(skb->dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001445 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 int err;
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001447 struct flowi fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001449 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTREQUESTS);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001450 payload_len = (skb->tail - skb->network_header) - sizeof(*pip6);
1451 mldlen = skb->tail - skb->transport_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 pip6->payload_len = htons(payload_len);
1453
1454 pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001455 IPPROTO_ICMPV6, csum_partial(skb_transport_header(skb),
1456 mldlen, 0));
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001457
1458 skb->dst = icmp6_dst_alloc(skb->dev, NULL, &ipv6_hdr(skb)->daddr);
1459
1460 if (!skb->dst) {
1461 err = -ENOMEM;
1462 goto err_out;
1463 }
1464
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001465 icmpv6_flow_init(net->ipv6.igmp_sk, &fl, ICMPV6_MLD2_REPORT,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001466 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1467 skb->dev->ifindex);
1468
1469 err = xfrm_lookup(&skb->dst, &fl, NULL, 0);
1470 if (err)
1471 goto err_out;
1472
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001473 err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001474 dst_output);
1475out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 if (!err) {
David L Stevens14878f72007-09-16 16:52:35 -07001477 ICMP6MSGOUT_INC_STATS_BH(idev, ICMPV6_MLD2_REPORT);
Denis V. Luneve41b5362008-10-08 10:33:26 -07001478 ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTMSGS);
Denis V. Lunev483a47d2008-10-08 11:09:27 -07001479 IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_OUTMCASTPKTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 } else
Denis V. Lunev483a47d2008-10-08 11:09:27 -07001481 IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 if (likely(idev != NULL))
1484 in6_dev_put(idev);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001485 return;
1486
1487err_out:
1488 kfree_skb(skb);
1489 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
1492static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1493{
Yan Zhengfab10fe2005-10-05 12:08:13 -07001494 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
1497static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1498 int type, struct mld2_grec **ppgr)
1499{
1500 struct net_device *dev = pmc->idev->dev;
1501 struct mld2_report *pmr;
1502 struct mld2_grec *pgr;
1503
1504 if (!skb)
1505 skb = mld_newpack(dev, dev->mtu);
1506 if (!skb)
1507 return NULL;
1508 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1509 pgr->grec_type = type;
1510 pgr->grec_auxwords = 0;
1511 pgr->grec_nsrcs = 0;
1512 pgr->grec_mca = pmc->mca_addr; /* structure copy */
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001513 pmr = (struct mld2_report *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
1515 *ppgr = pgr;
1516 return skb;
1517}
1518
1519#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1520 skb_tailroom(skb)) : 0)
1521
1522static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1523 int type, int gdeleted, int sdeleted)
1524{
1525 struct net_device *dev = pmc->idev->dev;
1526 struct mld2_report *pmr;
1527 struct mld2_grec *pgr = NULL;
1528 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001529 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 if (pmc->mca_flags & MAF_NOREPORT)
1532 return skb;
1533
1534 isquery = type == MLD2_MODE_IS_INCLUDE ||
1535 type == MLD2_MODE_IS_EXCLUDE;
1536 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1537 type == MLD2_CHANGE_TO_EXCLUDE;
1538
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001539 stotal = scount = 0;
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1542
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001543 if (!*psf_list)
1544 goto empty_source;
1545
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001546 pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548 /* EX and TO_EX get a fresh packet, if needed */
1549 if (truncate) {
1550 if (pmr && pmr->ngrec &&
1551 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1552 if (skb)
1553 mld_sendpack(skb);
1554 skb = mld_newpack(dev, dev->mtu);
1555 }
1556 }
1557 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 psf_prev = NULL;
1559 for (psf=*psf_list; psf; psf=psf_next) {
1560 struct in6_addr *psrc;
1561
1562 psf_next = psf->sf_next;
1563
1564 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1565 psf_prev = psf;
1566 continue;
1567 }
1568
1569 /* clear marks on query responses */
1570 if (isquery)
1571 psf->sf_gsresp = 0;
1572
1573 if (AVAILABLE(skb) < sizeof(*psrc) +
1574 first*sizeof(struct mld2_grec)) {
1575 if (truncate && !first)
1576 break; /* truncate these */
1577 if (pgr)
1578 pgr->grec_nsrcs = htons(scount);
1579 if (skb)
1580 mld_sendpack(skb);
1581 skb = mld_newpack(dev, dev->mtu);
1582 first = 1;
1583 scount = 0;
1584 }
1585 if (first) {
1586 skb = add_grhead(skb, pmc, type, &pgr);
1587 first = 0;
1588 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -08001589 if (!skb)
1590 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1592 *psrc = psf->sf_addr;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001593 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1595 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1596 psf->sf_crcount--;
1597 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1598 if (psf_prev)
1599 psf_prev->sf_next = psf->sf_next;
1600 else
1601 *psf_list = psf->sf_next;
1602 kfree(psf);
1603 continue;
1604 }
1605 }
1606 psf_prev = psf;
1607 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001608
1609empty_source:
1610 if (!stotal) {
1611 if (type == MLD2_ALLOW_NEW_SOURCES ||
1612 type == MLD2_BLOCK_OLD_SOURCES)
1613 return skb;
1614 if (pmc->mca_crcount || isquery) {
1615 /* make sure we have room for group header */
1616 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1617 mld_sendpack(skb);
1618 skb = NULL; /* add_grhead will get a new one */
1619 }
1620 skb = add_grhead(skb, pmc, type, &pgr);
1621 }
1622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (pgr)
1624 pgr->grec_nsrcs = htons(scount);
1625
1626 if (isquery)
1627 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1628 return skb;
1629}
1630
1631static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1632{
1633 struct sk_buff *skb = NULL;
1634 int type;
1635
1636 if (!pmc) {
1637 read_lock_bh(&idev->lock);
1638 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1639 if (pmc->mca_flags & MAF_NOREPORT)
1640 continue;
1641 spin_lock_bh(&pmc->mca_lock);
1642 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1643 type = MLD2_MODE_IS_EXCLUDE;
1644 else
1645 type = MLD2_MODE_IS_INCLUDE;
1646 skb = add_grec(skb, pmc, type, 0, 0);
1647 spin_unlock_bh(&pmc->mca_lock);
1648 }
1649 read_unlock_bh(&idev->lock);
1650 } else {
1651 spin_lock_bh(&pmc->mca_lock);
1652 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1653 type = MLD2_MODE_IS_EXCLUDE;
1654 else
1655 type = MLD2_MODE_IS_INCLUDE;
1656 skb = add_grec(skb, pmc, type, 0, 0);
1657 spin_unlock_bh(&pmc->mca_lock);
1658 }
1659 if (skb)
1660 mld_sendpack(skb);
1661}
1662
1663/*
1664 * remove zero-count source records from a source filter list
1665 */
1666static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1667{
1668 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1669
1670 psf_prev = NULL;
1671 for (psf=*ppsf; psf; psf = psf_next) {
1672 psf_next = psf->sf_next;
1673 if (psf->sf_crcount == 0) {
1674 if (psf_prev)
1675 psf_prev->sf_next = psf->sf_next;
1676 else
1677 *ppsf = psf->sf_next;
1678 kfree(psf);
1679 } else
1680 psf_prev = psf;
1681 }
1682}
1683
1684static void mld_send_cr(struct inet6_dev *idev)
1685{
1686 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1687 struct sk_buff *skb = NULL;
1688 int type, dtype;
1689
1690 read_lock_bh(&idev->lock);
1691 write_lock_bh(&idev->mc_lock);
1692
1693 /* deleted MCA's */
1694 pmc_prev = NULL;
1695 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1696 pmc_next = pmc->next;
1697 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1698 type = MLD2_BLOCK_OLD_SOURCES;
1699 dtype = MLD2_BLOCK_OLD_SOURCES;
1700 skb = add_grec(skb, pmc, type, 1, 0);
1701 skb = add_grec(skb, pmc, dtype, 1, 1);
1702 }
1703 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1705 type = MLD2_CHANGE_TO_INCLUDE;
1706 skb = add_grec(skb, pmc, type, 1, 0);
1707 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001708 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 if (pmc->mca_crcount == 0) {
1710 mld_clear_zeros(&pmc->mca_tomb);
1711 mld_clear_zeros(&pmc->mca_sources);
1712 }
1713 }
1714 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1715 !pmc->mca_sources) {
1716 if (pmc_prev)
1717 pmc_prev->next = pmc_next;
1718 else
1719 idev->mc_tomb = pmc_next;
1720 in6_dev_put(pmc->idev);
1721 kfree(pmc);
1722 } else
1723 pmc_prev = pmc;
1724 }
1725 write_unlock_bh(&idev->mc_lock);
1726
1727 /* change recs */
1728 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1729 spin_lock_bh(&pmc->mca_lock);
1730 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1731 type = MLD2_BLOCK_OLD_SOURCES;
1732 dtype = MLD2_ALLOW_NEW_SOURCES;
1733 } else {
1734 type = MLD2_ALLOW_NEW_SOURCES;
1735 dtype = MLD2_BLOCK_OLD_SOURCES;
1736 }
1737 skb = add_grec(skb, pmc, type, 0, 0);
1738 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1739
1740 /* filter mode changes */
1741 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1743 type = MLD2_CHANGE_TO_EXCLUDE;
1744 else
1745 type = MLD2_CHANGE_TO_INCLUDE;
1746 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001747 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
1749 spin_unlock_bh(&pmc->mca_lock);
1750 }
1751 read_unlock_bh(&idev->lock);
1752 if (!skb)
1753 return;
1754 (void) mld_sendpack(skb);
1755}
1756
1757static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1758{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001759 struct net *net = dev_net(dev);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001760 struct sock *sk = net->ipv6.igmp_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 struct inet6_dev *idev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001762 struct sk_buff *skb;
1763 struct icmp6hdr *hdr;
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001764 const struct in6_addr *snd_addr, *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 struct in6_addr *addrp;
1766 struct in6_addr addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 int err, len, payload_len, full_len;
1768 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1769 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1770 IPV6_TLV_PADN, 0 };
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001771 struct flowi fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001773 rcu_read_lock();
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001774 IP6_INC_STATS(net, __in6_dev_get(dev),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001775 IPSTATS_MIB_OUTREQUESTS);
1776 rcu_read_unlock();
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09001777 if (type == ICMPV6_MGM_REDUCTION)
1778 snd_addr = &in6addr_linklocal_allrouters;
1779 else
1780 snd_addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1783 payload_len = len + sizeof(ra);
1784 full_len = sizeof(struct ipv6hdr) + payload_len;
1785
Johannes Bergf5184d22008-05-12 20:48:31 -07001786 skb = sock_alloc_send_skb(sk, LL_ALLOCATED_SPACE(dev) + full_len, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788 if (skb == NULL) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001789 rcu_read_lock();
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001790 IP6_INC_STATS(net, __in6_dev_get(dev),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001791 IPSTATS_MIB_OUTDISCARDS);
1792 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 return;
1794 }
1795
1796 skb_reserve(skb, LL_RESERVED_SPACE(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Neil Horman95c385b2007-04-25 17:08:10 -07001798 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 /* <draft-ietf-magma-mld-source-05.txt>:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001800 * use unspecified address as the source address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 * when a valid link-local address is not available.
1802 */
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001803 saddr = &in6addr_any;
1804 } else
1805 saddr = &addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001807 ip6_nd_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1810
1811 hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
1812 memset(hdr, 0, sizeof(struct icmp6hdr));
1813 hdr->icmp6_type = type;
1814
1815 addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
1816 ipv6_addr_copy(addrp, addr);
1817
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001818 hdr->icmp6_cksum = csum_ipv6_magic(saddr, snd_addr, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 IPPROTO_ICMPV6,
1820 csum_partial((__u8 *) hdr, len, 0));
1821
1822 idev = in6_dev_get(skb->dev);
1823
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001824 skb->dst = icmp6_dst_alloc(skb->dev, NULL, &ipv6_hdr(skb)->daddr);
1825 if (!skb->dst) {
1826 err = -ENOMEM;
1827 goto err_out;
1828 }
1829
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001830 icmpv6_flow_init(sk, &fl, type,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001831 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1832 skb->dev->ifindex);
1833
1834 err = xfrm_lookup(&skb->dst, &fl, NULL, 0);
1835 if (err)
1836 goto err_out;
1837
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001838 err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001839 dst_output);
1840out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 if (!err) {
David L Stevens14878f72007-09-16 16:52:35 -07001842 ICMP6MSGOUT_INC_STATS(idev, type);
Denis V. Luneva862f6a2008-10-08 10:33:06 -07001843 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001844 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTMCASTPKTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 } else
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001846 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
1848 if (likely(idev != NULL))
1849 in6_dev_put(idev);
1850 return;
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001851
1852err_out:
1853 kfree_skb(skb);
1854 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855}
1856
1857static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1858 struct in6_addr *psfsrc)
1859{
1860 struct ip6_sf_list *psf, *psf_prev;
1861 int rv = 0;
1862
1863 psf_prev = NULL;
1864 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1865 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1866 break;
1867 psf_prev = psf;
1868 }
1869 if (!psf || psf->sf_count[sfmode] == 0) {
1870 /* source filter not found, or count wrong => bug */
1871 return -ESRCH;
1872 }
1873 psf->sf_count[sfmode]--;
1874 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1875 struct inet6_dev *idev = pmc->idev;
1876
1877 /* no more filters for this source */
1878 if (psf_prev)
1879 psf_prev->sf_next = psf->sf_next;
1880 else
1881 pmc->mca_sources = psf->sf_next;
1882 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1883 !MLD_V1_SEEN(idev)) {
1884 psf->sf_crcount = idev->mc_qrv;
1885 psf->sf_next = pmc->mca_tomb;
1886 pmc->mca_tomb = psf;
1887 rv = 1;
1888 } else
1889 kfree(psf);
1890 }
1891 return rv;
1892}
1893
1894static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
1895 int sfmode, int sfcount, struct in6_addr *psfsrc,
1896 int delta)
1897{
1898 struct ifmcaddr6 *pmc;
1899 int changerec = 0;
1900 int i, err;
1901
1902 if (!idev)
1903 return -ENODEV;
1904 read_lock_bh(&idev->lock);
1905 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1906 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1907 break;
1908 }
1909 if (!pmc) {
1910 /* MCA not found?? bug */
1911 read_unlock_bh(&idev->lock);
1912 return -ESRCH;
1913 }
1914 spin_lock_bh(&pmc->mca_lock);
1915 sf_markstate(pmc);
1916 if (!delta) {
1917 if (!pmc->mca_sfcount[sfmode]) {
1918 spin_unlock_bh(&pmc->mca_lock);
1919 read_unlock_bh(&idev->lock);
1920 return -EINVAL;
1921 }
1922 pmc->mca_sfcount[sfmode]--;
1923 }
1924 err = 0;
1925 for (i=0; i<sfcount; i++) {
1926 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1927
1928 changerec |= rv > 0;
1929 if (!err && rv < 0)
1930 err = rv;
1931 }
1932 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1933 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1934 pmc->mca_sfcount[MCAST_INCLUDE]) {
1935 struct ip6_sf_list *psf;
1936
1937 /* filter mode change */
1938 pmc->mca_sfmode = MCAST_INCLUDE;
1939 pmc->mca_crcount = idev->mc_qrv;
1940 idev->mc_ifc_count = pmc->mca_crcount;
1941 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1942 psf->sf_crcount = 0;
1943 mld_ifc_event(pmc->idev);
1944 } else if (sf_setstate(pmc) || changerec)
1945 mld_ifc_event(pmc->idev);
1946 spin_unlock_bh(&pmc->mca_lock);
1947 read_unlock_bh(&idev->lock);
1948 return err;
1949}
1950
1951/*
1952 * Add multicast single-source filter to the interface list
1953 */
1954static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1955 struct in6_addr *psfsrc, int delta)
1956{
1957 struct ip6_sf_list *psf, *psf_prev;
1958
1959 psf_prev = NULL;
1960 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1961 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1962 break;
1963 psf_prev = psf;
1964 }
1965 if (!psf) {
Ingo Oeser0c600ed2006-03-20 23:01:32 -08001966 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (!psf)
1968 return -ENOBUFS;
Ingo Oeser0c600ed2006-03-20 23:01:32 -08001969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 psf->sf_addr = *psfsrc;
1971 if (psf_prev) {
1972 psf_prev->sf_next = psf;
1973 } else
1974 pmc->mca_sources = psf;
1975 }
1976 psf->sf_count[sfmode]++;
1977 return 0;
1978}
1979
1980static void sf_markstate(struct ifmcaddr6 *pmc)
1981{
1982 struct ip6_sf_list *psf;
1983 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1984
1985 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1986 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1987 psf->sf_oldin = mca_xcount ==
1988 psf->sf_count[MCAST_EXCLUDE] &&
1989 !psf->sf_count[MCAST_INCLUDE];
1990 } else
1991 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1992}
1993
1994static int sf_setstate(struct ifmcaddr6 *pmc)
1995{
David L Stevens7add2a42006-01-24 13:06:39 -08001996 struct ip6_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1998 int qrv = pmc->idev->mc_qrv;
1999 int new_in, rv;
2000
2001 rv = 0;
2002 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
2003 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2004 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2005 !psf->sf_count[MCAST_INCLUDE];
2006 } else
2007 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevens7add2a42006-01-24 13:06:39 -08002008 if (new_in) {
2009 if (!psf->sf_oldin) {
Al Viroe80e28b2006-02-03 20:10:03 -05002010 struct ip6_sf_list *prev = NULL;
David L Stevens7add2a42006-01-24 13:06:39 -08002011
2012 for (dpsf=pmc->mca_tomb; dpsf;
2013 dpsf=dpsf->sf_next) {
2014 if (ipv6_addr_equal(&dpsf->sf_addr,
2015 &psf->sf_addr))
2016 break;
2017 prev = dpsf;
2018 }
2019 if (dpsf) {
2020 if (prev)
2021 prev->sf_next = dpsf->sf_next;
2022 else
2023 pmc->mca_tomb = dpsf->sf_next;
2024 kfree(dpsf);
2025 }
2026 psf->sf_crcount = qrv;
2027 rv++;
2028 }
2029 } else if (psf->sf_oldin) {
2030 psf->sf_crcount = 0;
2031 /*
2032 * add or update "delete" records if an active filter
2033 * is now inactive
2034 */
2035 for (dpsf=pmc->mca_tomb; dpsf; dpsf=dpsf->sf_next)
2036 if (ipv6_addr_equal(&dpsf->sf_addr,
2037 &psf->sf_addr))
2038 break;
2039 if (!dpsf) {
2040 dpsf = (struct ip6_sf_list *)
2041 kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2042 if (!dpsf)
2043 continue;
2044 *dpsf = *psf;
2045 /* pmc->mca_lock held by callers */
2046 dpsf->sf_next = pmc->mca_tomb;
2047 pmc->mca_tomb = dpsf;
2048 }
2049 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 rv++;
2051 }
2052 }
2053 return rv;
2054}
2055
2056/*
2057 * Add multicast source filter list to the interface list
2058 */
2059static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
2060 int sfmode, int sfcount, struct in6_addr *psfsrc,
2061 int delta)
2062{
2063 struct ifmcaddr6 *pmc;
2064 int isexclude;
2065 int i, err;
2066
2067 if (!idev)
2068 return -ENODEV;
2069 read_lock_bh(&idev->lock);
2070 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
2071 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2072 break;
2073 }
2074 if (!pmc) {
2075 /* MCA not found?? bug */
2076 read_unlock_bh(&idev->lock);
2077 return -ESRCH;
2078 }
2079 spin_lock_bh(&pmc->mca_lock);
2080
2081 sf_markstate(pmc);
2082 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2083 if (!delta)
2084 pmc->mca_sfcount[sfmode]++;
2085 err = 0;
2086 for (i=0; i<sfcount; i++) {
2087 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
2088 if (err)
2089 break;
2090 }
2091 if (err) {
2092 int j;
2093
2094 if (!delta)
2095 pmc->mca_sfcount[sfmode]--;
2096 for (j=0; j<i; j++)
2097 (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2098 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
2099 struct inet6_dev *idev = pmc->idev;
2100 struct ip6_sf_list *psf;
2101
2102 /* filter mode change */
2103 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2104 pmc->mca_sfmode = MCAST_EXCLUDE;
2105 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2106 pmc->mca_sfmode = MCAST_INCLUDE;
2107 /* else no filters; keep old mode for reports */
2108
2109 pmc->mca_crcount = idev->mc_qrv;
2110 idev->mc_ifc_count = pmc->mca_crcount;
2111 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
2112 psf->sf_crcount = 0;
2113 mld_ifc_event(idev);
2114 } else if (sf_setstate(pmc))
2115 mld_ifc_event(idev);
2116 spin_unlock_bh(&pmc->mca_lock);
2117 read_unlock_bh(&idev->lock);
2118 return err;
2119}
2120
2121static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2122{
2123 struct ip6_sf_list *psf, *nextpsf;
2124
2125 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
2126 nextpsf = psf->sf_next;
2127 kfree(psf);
2128 }
2129 pmc->mca_tomb = NULL;
2130 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
2131 nextpsf = psf->sf_next;
2132 kfree(psf);
2133 }
2134 pmc->mca_sources = NULL;
2135 pmc->mca_sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002136 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2138}
2139
2140
2141static void igmp6_join_group(struct ifmcaddr6 *ma)
2142{
2143 unsigned long delay;
2144
2145 if (ma->mca_flags & MAF_NOREPORT)
2146 return;
2147
2148 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2149
2150 delay = net_random() % IGMP6_UNSOLICITED_IVAL;
2151
2152 spin_lock_bh(&ma->mca_lock);
2153 if (del_timer(&ma->mca_timer)) {
2154 atomic_dec(&ma->mca_refcnt);
2155 delay = ma->mca_timer.expires - jiffies;
2156 }
2157
2158 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2159 atomic_inc(&ma->mca_refcnt);
2160 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2161 spin_unlock_bh(&ma->mca_lock);
2162}
2163
2164static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2165 struct inet6_dev *idev)
2166{
2167 int err;
2168
David L Stevens5ab4a6c2005-12-27 14:03:00 -08002169 /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
2170 * so no other readers or writers of iml or its sflist
2171 */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002172 if (!iml->sflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 /* any-source empty exclude case */
2174 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2175 }
2176 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2177 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2178 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2179 iml->sflist = NULL;
2180 return err;
2181}
2182
2183static void igmp6_leave_group(struct ifmcaddr6 *ma)
2184{
2185 if (MLD_V1_SEEN(ma->idev)) {
2186 if (ma->mca_flags & MAF_LAST_REPORTER)
2187 igmp6_send(&ma->mca_addr, ma->idev->dev,
2188 ICMPV6_MGM_REDUCTION);
2189 } else {
2190 mld_add_delrec(ma->idev, ma);
2191 mld_ifc_event(ma->idev);
2192 }
2193}
2194
2195static void mld_gq_timer_expire(unsigned long data)
2196{
2197 struct inet6_dev *idev = (struct inet6_dev *)data;
2198
2199 idev->mc_gq_running = 0;
2200 mld_send_report(idev, NULL);
2201 __in6_dev_put(idev);
2202}
2203
2204static void mld_ifc_timer_expire(unsigned long data)
2205{
2206 struct inet6_dev *idev = (struct inet6_dev *)data;
2207
2208 mld_send_cr(idev);
2209 if (idev->mc_ifc_count) {
2210 idev->mc_ifc_count--;
2211 if (idev->mc_ifc_count)
2212 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2213 }
2214 __in6_dev_put(idev);
2215}
2216
2217static void mld_ifc_event(struct inet6_dev *idev)
2218{
2219 if (MLD_V1_SEEN(idev))
2220 return;
2221 idev->mc_ifc_count = idev->mc_qrv;
2222 mld_ifc_start_timer(idev, 1);
2223}
2224
2225
2226static void igmp6_timer_handler(unsigned long data)
2227{
2228 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2229
2230 if (MLD_V1_SEEN(ma->idev))
2231 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2232 else
2233 mld_send_report(ma->idev, ma);
2234
2235 spin_lock(&ma->mca_lock);
2236 ma->mca_flags |= MAF_LAST_REPORTER;
2237 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2238 spin_unlock(&ma->mca_lock);
2239 ma_put(ma);
2240}
2241
2242/* Device going down */
2243
2244void ipv6_mc_down(struct inet6_dev *idev)
2245{
2246 struct ifmcaddr6 *i;
2247
2248 /* Withdraw multicast list */
2249
2250 read_lock_bh(&idev->lock);
2251 idev->mc_ifc_count = 0;
2252 if (del_timer(&idev->mc_ifc_timer))
2253 __in6_dev_put(idev);
2254 idev->mc_gq_running = 0;
2255 if (del_timer(&idev->mc_gq_timer))
2256 __in6_dev_put(idev);
2257
2258 for (i = idev->mc_list; i; i=i->next)
2259 igmp6_group_dropped(i);
2260 read_unlock_bh(&idev->lock);
2261
2262 mld_clear_delrec(idev);
2263}
2264
2265
2266/* Device going up */
2267
2268void ipv6_mc_up(struct inet6_dev *idev)
2269{
2270 struct ifmcaddr6 *i;
2271
2272 /* Install multicast list, except for all-nodes (already installed) */
2273
2274 read_lock_bh(&idev->lock);
2275 for (i = idev->mc_list; i; i=i->next)
2276 igmp6_group_added(i);
2277 read_unlock_bh(&idev->lock);
2278}
2279
2280/* IPv6 device initialization. */
2281
2282void ipv6_mc_init_dev(struct inet6_dev *idev)
2283{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 write_lock_bh(&idev->lock);
2285 rwlock_init(&idev->mc_lock);
2286 idev->mc_gq_running = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08002287 setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
2288 (unsigned long)idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 idev->mc_tomb = NULL;
2290 idev->mc_ifc_count = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08002291 setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
2292 (unsigned long)idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 idev->mc_qrv = MLD_QRV_DEFAULT;
2294 idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2295 idev->mc_v1_seen = 0;
2296 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297}
2298
2299/*
2300 * Device is about to be destroyed: clean up.
2301 */
2302
2303void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2304{
2305 struct ifmcaddr6 *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
2307 /* Deactivate timers */
2308 ipv6_mc_down(idev);
2309
2310 /* Delete all-nodes address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2312 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2313 * fail.
2314 */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002315 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002317 if (idev->cnf.forwarding)
2318 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
2320 write_lock_bh(&idev->lock);
2321 while ((i = idev->mc_list) != NULL) {
2322 idev->mc_list = i->next;
2323 write_unlock_bh(&idev->lock);
2324
2325 igmp6_group_dropped(i);
2326 ma_put(i);
2327
2328 write_lock_bh(&idev->lock);
2329 }
2330 write_unlock_bh(&idev->lock);
2331}
2332
2333#ifdef CONFIG_PROC_FS
2334struct igmp6_mc_iter_state {
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002335 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 struct net_device *dev;
2337 struct inet6_dev *idev;
2338};
2339
2340#define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2341
2342static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2343{
2344 struct ifmcaddr6 *im = NULL;
2345 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002346 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Pavel Emelianov7562f872007-05-03 15:13:45 -07002348 state->idev = NULL;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002349 for_each_netdev(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 struct inet6_dev *idev;
2351 idev = in6_dev_get(state->dev);
2352 if (!idev)
2353 continue;
2354 read_lock_bh(&idev->lock);
2355 im = idev->mc_list;
2356 if (im) {
2357 state->idev = idev;
2358 break;
2359 }
2360 read_unlock_bh(&idev->lock);
2361 in6_dev_put(idev);
2362 }
2363 return im;
2364}
2365
2366static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2367{
2368 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2369
2370 im = im->next;
2371 while (!im) {
2372 if (likely(state->idev != NULL)) {
2373 read_unlock_bh(&state->idev->lock);
2374 in6_dev_put(state->idev);
2375 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07002376 state->dev = next_net_device(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 if (!state->dev) {
2378 state->idev = NULL;
2379 break;
2380 }
2381 state->idev = in6_dev_get(state->dev);
2382 if (!state->idev)
2383 continue;
2384 read_lock_bh(&state->idev->lock);
2385 im = state->idev->mc_list;
2386 }
2387 return im;
2388}
2389
2390static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2391{
2392 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2393 if (im)
2394 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2395 --pos;
2396 return pos ? NULL : im;
2397}
2398
2399static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002400 __acquires(dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401{
2402 read_lock(&dev_base_lock);
2403 return igmp6_mc_get_idx(seq, *pos);
2404}
2405
2406static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2407{
2408 struct ifmcaddr6 *im;
2409 im = igmp6_mc_get_next(seq, v);
2410 ++*pos;
2411 return im;
2412}
2413
2414static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002415 __releases(dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
2417 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2418 if (likely(state->idev != NULL)) {
2419 read_unlock_bh(&state->idev->lock);
2420 in6_dev_put(state->idev);
2421 state->idev = NULL;
2422 }
2423 state->dev = NULL;
2424 read_unlock(&dev_base_lock);
2425}
2426
2427static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2428{
2429 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2430 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2431
2432 seq_printf(seq,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002433 "%-4d %-15s " NIP6_SEQFMT " %5d %08X %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 state->dev->ifindex, state->dev->name,
2435 NIP6(im->mca_addr),
2436 im->mca_users, im->mca_flags,
2437 (im->mca_flags&MAF_TIMER_RUNNING) ?
2438 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2439 return 0;
2440}
2441
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002442static const struct seq_operations igmp6_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 .start = igmp6_mc_seq_start,
2444 .next = igmp6_mc_seq_next,
2445 .stop = igmp6_mc_seq_stop,
2446 .show = igmp6_mc_seq_show,
2447};
2448
2449static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2450{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002451 return seq_open_net(inode, file, &igmp6_mc_seq_ops,
2452 sizeof(struct igmp6_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453}
2454
Arjan van de Ven9a321442007-02-12 00:55:35 -08002455static const struct file_operations igmp6_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 .owner = THIS_MODULE,
2457 .open = igmp6_mc_seq_open,
2458 .read = seq_read,
2459 .llseek = seq_lseek,
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002460 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461};
2462
2463struct igmp6_mcf_iter_state {
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002464 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 struct net_device *dev;
2466 struct inet6_dev *idev;
2467 struct ifmcaddr6 *im;
2468};
2469
2470#define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2471
2472static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2473{
2474 struct ip6_sf_list *psf = NULL;
2475 struct ifmcaddr6 *im = NULL;
2476 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002477 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Pavel Emelianov7562f872007-05-03 15:13:45 -07002479 state->idev = NULL;
2480 state->im = NULL;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002481 for_each_netdev(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 struct inet6_dev *idev;
2483 idev = in6_dev_get(state->dev);
2484 if (unlikely(idev == NULL))
2485 continue;
2486 read_lock_bh(&idev->lock);
2487 im = idev->mc_list;
2488 if (likely(im != NULL)) {
2489 spin_lock_bh(&im->mca_lock);
2490 psf = im->mca_sources;
2491 if (likely(psf != NULL)) {
2492 state->im = im;
2493 state->idev = idev;
2494 break;
2495 }
2496 spin_unlock_bh(&im->mca_lock);
2497 }
2498 read_unlock_bh(&idev->lock);
2499 in6_dev_put(idev);
2500 }
2501 return psf;
2502}
2503
2504static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2505{
2506 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2507
2508 psf = psf->sf_next;
2509 while (!psf) {
2510 spin_unlock_bh(&state->im->mca_lock);
2511 state->im = state->im->next;
2512 while (!state->im) {
2513 if (likely(state->idev != NULL)) {
2514 read_unlock_bh(&state->idev->lock);
2515 in6_dev_put(state->idev);
2516 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07002517 state->dev = next_net_device(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 if (!state->dev) {
2519 state->idev = NULL;
2520 goto out;
2521 }
2522 state->idev = in6_dev_get(state->dev);
2523 if (!state->idev)
2524 continue;
2525 read_lock_bh(&state->idev->lock);
2526 state->im = state->idev->mc_list;
2527 }
2528 if (!state->im)
2529 break;
2530 spin_lock_bh(&state->im->mca_lock);
2531 psf = state->im->mca_sources;
2532 }
2533out:
2534 return psf;
2535}
2536
2537static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2538{
2539 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2540 if (psf)
2541 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2542 --pos;
2543 return pos ? NULL : psf;
2544}
2545
2546static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002547 __acquires(dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548{
2549 read_lock(&dev_base_lock);
2550 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2551}
2552
2553static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2554{
2555 struct ip6_sf_list *psf;
2556 if (v == SEQ_START_TOKEN)
2557 psf = igmp6_mcf_get_first(seq);
2558 else
2559 psf = igmp6_mcf_get_next(seq, v);
2560 ++*pos;
2561 return psf;
2562}
2563
2564static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002565 __releases(dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566{
2567 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2568 if (likely(state->im != NULL)) {
2569 spin_unlock_bh(&state->im->mca_lock);
2570 state->im = NULL;
2571 }
2572 if (likely(state->idev != NULL)) {
2573 read_unlock_bh(&state->idev->lock);
2574 in6_dev_put(state->idev);
2575 state->idev = NULL;
2576 }
2577 state->dev = NULL;
2578 read_unlock(&dev_base_lock);
2579}
2580
2581static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2582{
2583 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2584 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2585
2586 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002587 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 "%3s %6s "
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002589 "%32s %32s %6s %6s\n", "Idx",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 "Device", "Multicast Address",
2591 "Source Address", "INC", "EXC");
2592 } else {
2593 seq_printf(seq,
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002594 "%3d %6.6s " NIP6_SEQFMT " " NIP6_SEQFMT " %6lu %6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 state->dev->ifindex, state->dev->name,
2596 NIP6(state->im->mca_addr),
2597 NIP6(psf->sf_addr),
2598 psf->sf_count[MCAST_INCLUDE],
2599 psf->sf_count[MCAST_EXCLUDE]);
2600 }
2601 return 0;
2602}
2603
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002604static const struct seq_operations igmp6_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 .start = igmp6_mcf_seq_start,
2606 .next = igmp6_mcf_seq_next,
2607 .stop = igmp6_mcf_seq_stop,
2608 .show = igmp6_mcf_seq_show,
2609};
2610
2611static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2612{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002613 return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
2614 sizeof(struct igmp6_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615}
2616
Arjan van de Ven9a321442007-02-12 00:55:35 -08002617static const struct file_operations igmp6_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 .owner = THIS_MODULE,
2619 .open = igmp6_mcf_seq_open,
2620 .read = seq_read,
2621 .llseek = seq_lseek,
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002622 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623};
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002624
2625static int igmp6_proc_init(struct net *net)
2626{
2627 int err;
2628
2629 err = -ENOMEM;
2630 if (!proc_net_fops_create(net, "igmp6", S_IRUGO, &igmp6_mc_seq_fops))
2631 goto out;
2632 if (!proc_net_fops_create(net, "mcfilter6", S_IRUGO,
2633 &igmp6_mcf_seq_fops))
2634 goto out_proc_net_igmp6;
2635
2636 err = 0;
2637out:
2638 return err;
2639
2640out_proc_net_igmp6:
2641 proc_net_remove(net, "igmp6");
2642 goto out;
2643}
2644
2645static void igmp6_proc_exit(struct net *net)
2646{
2647 proc_net_remove(net, "mcfilter6");
2648 proc_net_remove(net, "igmp6");
2649}
2650#else
2651static int igmp6_proc_init(struct net *net)
2652{
2653 return 0;
2654}
2655static void igmp6_proc_exit(struct net *net)
2656{
2657 ;
2658}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659#endif
2660
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002661static int igmp6_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 int err;
2664
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002665 err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
2666 SOCK_RAW, IPPROTO_ICMPV6, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 if (err < 0) {
2668 printk(KERN_ERR
2669 "Failed to initialize the IGMP6 control socket (err %d).\n",
2670 err);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002671 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 }
2673
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002674 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002676 err = igmp6_proc_init(net);
2677 if (err)
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002678 goto out_sock_create;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002679out:
2680 return err;
2681
2682out_sock_create:
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002683 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002684 goto out;
2685}
2686
2687static void igmp6_net_exit(struct net *net)
2688{
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002689 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002690 igmp6_proc_exit(net);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002691}
2692
2693static struct pernet_operations igmp6_net_ops = {
2694 .init = igmp6_net_init,
2695 .exit = igmp6_net_exit,
2696};
2697
2698int __init igmp6_init(void)
2699{
2700 return register_pernet_subsys(&igmp6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701}
2702
2703void igmp6_cleanup(void)
2704{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002705 unregister_pernet_subsys(&igmp6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706}