blob: 54f91efdae58cdcded9923675b34ea0bec580f04 [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 *
8 * $Id: mcast.c,v 1.40 2002/02/08 03:57:19 davem Exp $
9 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090010 * Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 */
17
18/* Changes:
19 *
20 * yoshfuji : fix format of router-alert option
21 * YOSHIFUJI Hideaki @USAGI:
22 * Fixed source address for MLD message based on
23 * <draft-ietf-magma-mld-source-05.txt>.
24 * YOSHIFUJI Hideaki @USAGI:
25 * - Ignore Queries for invalid addresses.
26 * - MLD for link-local addresses.
27 * David L Stevens <dlstevens@us.ibm.com>:
28 * - MLDv2 support
29 */
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/module.h>
32#include <linux/errno.h>
33#include <linux/types.h>
34#include <linux/string.h>
35#include <linux/socket.h>
36#include <linux/sockios.h>
37#include <linux/jiffies.h>
38#include <linux/times.h>
39#include <linux/net.h>
40#include <linux/in.h>
41#include <linux/in6.h>
42#include <linux/netdevice.h>
43#include <linux/if_arp.h>
44#include <linux/route.h>
45#include <linux/init.h>
46#include <linux/proc_fs.h>
47#include <linux/seq_file.h>
48
49#include <linux/netfilter.h>
50#include <linux/netfilter_ipv6.h>
51
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020052#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <net/sock.h>
54#include <net/snmp.h>
55
56#include <net/ipv6.h>
57#include <net/protocol.h>
58#include <net/if_inet6.h>
59#include <net/ndisc.h>
60#include <net/addrconf.h>
61#include <net/ip6_route.h>
Denis V. Lunev1ed85162008-04-03 14:31:03 -070062#include <net/inet_common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64#include <net/ip6_checksum.h>
65
66/* Set to 3 to get tracing... */
67#define MCAST_DEBUG 2
68
69#if MCAST_DEBUG >= 3
70#define MDBG(x) printk x
71#else
72#define MDBG(x)
73#endif
74
75/*
76 * These header formats should be in a separate include file, but icmpv6.h
77 * doesn't have in6_addr defined in all cases, there is no __u128, and no
78 * other files reference these.
79 *
80 * +-DLS 4/14/03
81 */
82
83/* Multicast Listener Discovery version 2 headers */
84
85struct mld2_grec {
86 __u8 grec_type;
87 __u8 grec_auxwords;
Al Viro8a74ff72006-11-08 00:22:08 -080088 __be16 grec_nsrcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 struct in6_addr grec_mca;
90 struct in6_addr grec_src[0];
91};
92
93struct mld2_report {
94 __u8 type;
95 __u8 resv1;
Al Viro868c86b2006-11-14 21:35:48 -080096 __sum16 csum;
Al Viro8a74ff72006-11-08 00:22:08 -080097 __be16 resv2;
98 __be16 ngrec;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 struct mld2_grec grec[0];
100};
101
102struct mld2_query {
103 __u8 type;
104 __u8 code;
Al Viro868c86b2006-11-14 21:35:48 -0800105 __sum16 csum;
Al Viro8a74ff72006-11-08 00:22:08 -0800106 __be16 mrc;
107 __be16 resv1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 struct in6_addr mca;
109#if defined(__LITTLE_ENDIAN_BITFIELD)
110 __u8 qrv:3,
111 suppress:1,
112 resv2:4;
113#elif defined(__BIG_ENDIAN_BITFIELD)
114 __u8 resv2:4,
115 suppress:1,
116 qrv:3;
117#else
118#error "Please fix <asm/byteorder.h>"
119#endif
120 __u8 qqic;
Al Viro8a74ff72006-11-08 00:22:08 -0800121 __be16 nsrcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 struct in6_addr srcs[0];
123};
124
125static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;
126
127/* Big mc list lock for all the sockets */
128static DEFINE_RWLOCK(ipv6_sk_mc_lock);
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static void igmp6_join_group(struct ifmcaddr6 *ma);
131static void igmp6_leave_group(struct ifmcaddr6 *ma);
132static void igmp6_timer_handler(unsigned long data);
133
134static void mld_gq_timer_expire(unsigned long data);
135static void mld_ifc_timer_expire(unsigned long data);
136static void mld_ifc_event(struct inet6_dev *idev);
137static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
138static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *addr);
139static void mld_clear_delrec(struct inet6_dev *idev);
140static int sf_setstate(struct ifmcaddr6 *pmc);
141static void sf_markstate(struct ifmcaddr6 *pmc);
142static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
143static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
144 int sfmode, int sfcount, struct in6_addr *psfsrc,
145 int delta);
146static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
147 int sfmode, int sfcount, struct in6_addr *psfsrc,
148 int delta);
149static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
150 struct inet6_dev *idev);
151
152
153#define IGMP6_UNSOLICITED_IVAL (10*HZ)
154#define MLD_QRV_DEFAULT 2
155
156#define MLD_V1_SEEN(idev) (ipv6_devconf.force_mld_version == 1 || \
157 (idev)->cnf.force_mld_version == 1 || \
158 ((idev)->mc_v1_seen && \
159 time_before(jiffies, (idev)->mc_v1_seen)))
160
161#define MLDV2_MASK(value, nb) ((nb)>=32 ? (value) : ((1<<(nb))-1) & (value))
162#define MLDV2_EXP(thresh, nbmant, nbexp, value) \
163 ((value) < (thresh) ? (value) : \
Yan Zhengf12baea2005-10-29 00:02:32 +0800164 ((MLDV2_MASK(value, nbmant) | (1<<(nbmant))) << \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 (MLDV2_MASK((value) >> (nbmant), nbexp) + (nbexp))))
166
167#define MLDV2_QQIC(value) MLDV2_EXP(0x80, 4, 3, value)
168#define MLDV2_MRC(value) MLDV2_EXP(0x8000, 12, 3, value)
169
David L Stevens6f4353d2005-12-26 17:03:46 -0800170#define IPV6_MLD_MAX_MSF 64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Brian Haleyab32ea52006-09-22 14:15:41 -0700172int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174/*
175 * socket join on multicast group
176 */
177
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900178int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 struct net_device *dev = NULL;
181 struct ipv6_mc_socklist *mc_lst;
182 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900183 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 int err;
185
186 if (!ipv6_addr_is_multicast(addr))
187 return -EINVAL;
188
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700189 read_lock_bh(&ipv6_sk_mc_lock);
190 for (mc_lst=np->ipv6_mc_list; mc_lst; mc_lst=mc_lst->next) {
191 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
192 ipv6_addr_equal(&mc_lst->addr, addr)) {
193 read_unlock_bh(&ipv6_sk_mc_lock);
194 return -EADDRINUSE;
195 }
196 }
197 read_unlock_bh(&ipv6_sk_mc_lock);
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
200
201 if (mc_lst == NULL)
202 return -ENOMEM;
203
204 mc_lst->next = NULL;
205 ipv6_addr_copy(&mc_lst->addr, addr);
206
207 if (ifindex == 0) {
208 struct rt6_info *rt;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800209 rt = rt6_lookup(net, addr, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (rt) {
211 dev = rt->rt6i_dev;
212 dev_hold(dev);
213 dst_release(&rt->u.dst);
214 }
215 } else
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800216 dev = dev_get_by_index(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 if (dev == NULL) {
219 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
220 return -ENODEV;
221 }
222
223 mc_lst->ifindex = dev->ifindex;
224 mc_lst->sfmode = MCAST_EXCLUDE;
YOSHIFUJI Hideaki196433c2006-01-04 13:56:31 -0800225 rwlock_init(&mc_lst->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 mc_lst->sflist = NULL;
227
228 /*
229 * now add/increase the group membership on the device
230 */
231
232 err = ipv6_dev_mc_inc(dev, addr);
233
234 if (err) {
235 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
236 dev_put(dev);
237 return err;
238 }
239
240 write_lock_bh(&ipv6_sk_mc_lock);
241 mc_lst->next = np->ipv6_mc_list;
242 np->ipv6_mc_list = mc_lst;
243 write_unlock_bh(&ipv6_sk_mc_lock);
244
245 dev_put(dev);
246
247 return 0;
248}
249
250/*
251 * socket leave on multicast group
252 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900253int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 struct ipv6_pinfo *np = inet6_sk(sk);
256 struct ipv6_mc_socklist *mc_lst, **lnk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900257 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 write_lock_bh(&ipv6_sk_mc_lock);
260 for (lnk = &np->ipv6_mc_list; (mc_lst = *lnk) !=NULL ; lnk = &mc_lst->next) {
261 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
262 ipv6_addr_equal(&mc_lst->addr, addr)) {
263 struct net_device *dev;
264
265 *lnk = mc_lst->next;
266 write_unlock_bh(&ipv6_sk_mc_lock);
267
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800268 dev = dev_get_by_index(net, mc_lst->ifindex);
269 if (dev != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 struct inet6_dev *idev = in6_dev_get(dev);
271
David L Stevensacd6e002006-08-17 16:27:39 -0700272 (void) ip6_mc_leave_src(sk, mc_lst, idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
275 in6_dev_put(idev);
276 }
277 dev_put(dev);
David L Stevensacd6e002006-08-17 16:27:39 -0700278 } else
279 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
281 return 0;
282 }
283 }
284 write_unlock_bh(&ipv6_sk_mc_lock);
285
David L Stevens9951f032005-07-08 17:47:28 -0700286 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800289static struct inet6_dev *ip6_mc_find_dev(struct net *net,
290 struct in6_addr *group,
291 int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 struct net_device *dev = NULL;
294 struct inet6_dev *idev = NULL;
295
296 if (ifindex == 0) {
297 struct rt6_info *rt;
298
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800299 rt = rt6_lookup(net, group, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (rt) {
301 dev = rt->rt6i_dev;
302 dev_hold(dev);
303 dst_release(&rt->u.dst);
304 }
305 } else
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800306 dev = dev_get_by_index(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 if (!dev)
309 return NULL;
310 idev = in6_dev_get(dev);
311 if (!idev) {
312 dev_put(dev);
313 return NULL;
314 }
315 read_lock_bh(&idev->lock);
316 if (idev->dead) {
317 read_unlock_bh(&idev->lock);
318 in6_dev_put(idev);
319 dev_put(dev);
320 return NULL;
321 }
322 return idev;
323}
324
325void ipv6_sock_mc_close(struct sock *sk)
326{
327 struct ipv6_pinfo *np = inet6_sk(sk);
328 struct ipv6_mc_socklist *mc_lst;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900329 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 write_lock_bh(&ipv6_sk_mc_lock);
332 while ((mc_lst = np->ipv6_mc_list) != NULL) {
333 struct net_device *dev;
334
335 np->ipv6_mc_list = mc_lst->next;
336 write_unlock_bh(&ipv6_sk_mc_lock);
337
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800338 dev = dev_get_by_index(net, mc_lst->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (dev) {
340 struct inet6_dev *idev = in6_dev_get(dev);
341
David L Stevensacd6e002006-08-17 16:27:39 -0700342 (void) ip6_mc_leave_src(sk, mc_lst, idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
345 in6_dev_put(idev);
346 }
347 dev_put(dev);
David L Stevensacd6e002006-08-17 16:27:39 -0700348 } else
349 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
352
353 write_lock_bh(&ipv6_sk_mc_lock);
354 }
355 write_unlock_bh(&ipv6_sk_mc_lock);
356}
357
358int ip6_mc_source(int add, int omode, struct sock *sk,
359 struct group_source_req *pgsr)
360{
361 struct in6_addr *source, *group;
362 struct ipv6_mc_socklist *pmc;
363 struct net_device *dev;
364 struct inet6_dev *idev;
365 struct ipv6_pinfo *inet6 = inet6_sk(sk);
366 struct ip6_sf_socklist *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900367 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 int i, j, rv;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700369 int leavegroup = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800370 int pmclocked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 int err;
372
373 if (pgsr->gsr_group.ss_family != AF_INET6 ||
374 pgsr->gsr_source.ss_family != AF_INET6)
375 return -EINVAL;
376
377 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
378 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
379
380 if (!ipv6_addr_is_multicast(group))
381 return -EINVAL;
382
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800383 idev = ip6_mc_find_dev(net, group, pgsr->gsr_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (!idev)
385 return -ENODEV;
386 dev = idev->dev;
387
388 err = -EADDRNOTAVAIL;
389
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700390 read_lock_bh(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
392 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
393 continue;
394 if (ipv6_addr_equal(&pmc->addr, group))
395 break;
396 }
David L Stevens917f2f12005-07-08 17:45:16 -0700397 if (!pmc) { /* must have a prior join */
398 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 /* if a source filter was set, must be the same mode as before */
402 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -0700403 if (pmc->sfmode != omode) {
404 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 } else if (pmc->sfmode != omode) {
408 /* allow mode switches for empty-set filters */
409 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
410 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
411 pmc->sfmode = omode;
412 }
413
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800414 write_lock_bh(&pmc->sflock);
415 pmclocked = 1;
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 psl = pmc->sflist;
418 if (!add) {
419 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -0700420 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 rv = !0;
422 for (i=0; i<psl->sl_count; i++) {
423 rv = memcmp(&psl->sl_addr[i], source,
424 sizeof(struct in6_addr));
425 if (rv == 0)
426 break;
427 }
428 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -0700429 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700431 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
432 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
433 leavegroup = 1;
434 goto done;
435 }
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 /* update the interface filter */
438 ip6_mc_del_src(idev, group, omode, 1, source, 1);
439
440 for (j=i+1; j<psl->sl_count; j++)
441 psl->sl_addr[j-1] = psl->sl_addr[j];
442 psl->sl_count--;
443 err = 0;
444 goto done;
445 }
446 /* else, add a new source to the filter */
447
448 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
449 err = -ENOBUFS;
450 goto done;
451 }
452 if (!psl || psl->sl_count == psl->sl_max) {
453 struct ip6_sf_socklist *newpsl;
454 int count = IP6_SFBLOCK;
455
456 if (psl)
457 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800458 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (!newpsl) {
460 err = -ENOBUFS;
461 goto done;
462 }
463 newpsl->sl_max = count;
464 newpsl->sl_count = count - IP6_SFBLOCK;
465 if (psl) {
466 for (i=0; i<psl->sl_count; i++)
467 newpsl->sl_addr[i] = psl->sl_addr[i];
468 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
469 }
470 pmc->sflist = psl = newpsl;
471 }
472 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
473 for (i=0; i<psl->sl_count; i++) {
474 rv = memcmp(&psl->sl_addr[i], source, sizeof(struct in6_addr));
475 if (rv == 0)
476 break;
477 }
478 if (rv == 0) /* address already there is an error */
479 goto done;
480 for (j=psl->sl_count-1; j>=i; j--)
481 psl->sl_addr[j+1] = psl->sl_addr[j];
482 psl->sl_addr[i] = *source;
483 psl->sl_count++;
484 err = 0;
485 /* update the interface list */
486 ip6_mc_add_src(idev, group, omode, 1, source, 1);
487done:
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800488 if (pmclocked)
489 write_unlock_bh(&pmc->sflock);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700490 read_unlock_bh(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 read_unlock_bh(&idev->lock);
492 in6_dev_put(idev);
493 dev_put(dev);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700494 if (leavegroup)
495 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return err;
497}
498
499int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
500{
501 struct in6_addr *group;
502 struct ipv6_mc_socklist *pmc;
503 struct net_device *dev;
504 struct inet6_dev *idev;
505 struct ipv6_pinfo *inet6 = inet6_sk(sk);
506 struct ip6_sf_socklist *newpsl, *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900507 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -0700508 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 int i, err;
510
511 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
512
513 if (!ipv6_addr_is_multicast(group))
514 return -EINVAL;
515 if (gsf->gf_fmode != MCAST_INCLUDE &&
516 gsf->gf_fmode != MCAST_EXCLUDE)
517 return -EINVAL;
518
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800519 idev = ip6_mc_find_dev(net, group, gsf->gf_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 if (!idev)
522 return -ENODEV;
523 dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
David S. Miller9c059892005-07-08 21:44:39 -0700525 err = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800526 read_lock_bh(&ipv6_sk_mc_lock);
527
David L Stevens9951f032005-07-08 17:47:28 -0700528 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
529 leavegroup = 1;
530 goto done;
531 }
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
534 if (pmc->ifindex != gsf->gf_interface)
535 continue;
536 if (ipv6_addr_equal(&pmc->addr, group))
537 break;
538 }
David L Stevens917f2f12005-07-08 17:45:16 -0700539 if (!pmc) { /* must have a prior join */
540 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (gsf->gf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800544 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
545 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (!newpsl) {
547 err = -ENOBUFS;
548 goto done;
549 }
550 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
551 for (i=0; i<newpsl->sl_count; ++i) {
552 struct sockaddr_in6 *psin6;
553
554 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
555 newpsl->sl_addr[i] = psin6->sin6_addr;
556 }
557 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
558 newpsl->sl_count, newpsl->sl_addr, 0);
559 if (err) {
560 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
561 goto done;
562 }
Yan Zheng8713dbf2005-10-28 08:02:08 +0800563 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +0800565 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
566 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800567
568 write_lock_bh(&pmc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 psl = pmc->sflist;
570 if (psl) {
571 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
572 psl->sl_count, psl->sl_addr, 0);
573 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
574 } else
575 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
576 pmc->sflist = newpsl;
577 pmc->sfmode = gsf->gf_fmode;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800578 write_unlock_bh(&pmc->sflock);
David L Stevens917f2f12005-07-08 17:45:16 -0700579 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580done:
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800581 read_unlock_bh(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 read_unlock_bh(&idev->lock);
583 in6_dev_put(idev);
584 dev_put(dev);
David L Stevens9951f032005-07-08 17:47:28 -0700585 if (leavegroup)
586 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return err;
588}
589
590int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
591 struct group_filter __user *optval, int __user *optlen)
592{
593 int err, i, count, copycount;
594 struct in6_addr *group;
595 struct ipv6_mc_socklist *pmc;
596 struct inet6_dev *idev;
597 struct net_device *dev;
598 struct ipv6_pinfo *inet6 = inet6_sk(sk);
599 struct ip6_sf_socklist *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900600 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
603
604 if (!ipv6_addr_is_multicast(group))
605 return -EINVAL;
606
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800607 idev = ip6_mc_find_dev(net, group, gsf->gf_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 if (!idev)
610 return -ENODEV;
611
612 dev = idev->dev;
613
614 err = -EADDRNOTAVAIL;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800615 /*
616 * changes to the ipv6_mc_list require the socket lock and
617 * a read lock on ip6_sk_mc_lock. We have the socket lock,
618 * so reading the list is safe.
619 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
622 if (pmc->ifindex != gsf->gf_interface)
623 continue;
624 if (ipv6_addr_equal(group, &pmc->addr))
625 break;
626 }
627 if (!pmc) /* must have a prior join */
628 goto done;
629 gsf->gf_fmode = pmc->sfmode;
630 psl = pmc->sflist;
631 count = psl ? psl->sl_count : 0;
632 read_unlock_bh(&idev->lock);
633 in6_dev_put(idev);
634 dev_put(dev);
635
636 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
637 gsf->gf_numsrc = count;
638 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
639 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
640 return -EFAULT;
641 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800642 /* changes to psl require the socket lock, a read lock on
643 * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
644 * have the socket lock, so reading here is safe.
645 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 for (i=0; i<copycount; i++) {
647 struct sockaddr_in6 *psin6;
648 struct sockaddr_storage ss;
649
650 psin6 = (struct sockaddr_in6 *)&ss;
651 memset(&ss, 0, sizeof(ss));
652 psin6->sin6_family = AF_INET6;
653 psin6->sin6_addr = psl->sl_addr[i];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900654 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return -EFAULT;
656 }
657 return 0;
658done:
659 read_unlock_bh(&idev->lock);
660 in6_dev_put(idev);
661 dev_put(dev);
662 return err;
663}
664
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900665int inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
666 const struct in6_addr *src_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 struct ipv6_pinfo *np = inet6_sk(sk);
669 struct ipv6_mc_socklist *mc;
670 struct ip6_sf_socklist *psl;
671 int rv = 1;
672
673 read_lock(&ipv6_sk_mc_lock);
674 for (mc = np->ipv6_mc_list; mc; mc = mc->next) {
675 if (ipv6_addr_equal(&mc->addr, mc_addr))
676 break;
677 }
678 if (!mc) {
679 read_unlock(&ipv6_sk_mc_lock);
680 return 1;
681 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800682 read_lock(&mc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 psl = mc->sflist;
684 if (!psl) {
685 rv = mc->sfmode == MCAST_EXCLUDE;
686 } else {
687 int i;
688
689 for (i=0; i<psl->sl_count; i++) {
690 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
691 break;
692 }
693 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
694 rv = 0;
695 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
696 rv = 0;
697 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800698 read_unlock(&mc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 read_unlock(&ipv6_sk_mc_lock);
700
701 return rv;
702}
703
704static void ma_put(struct ifmcaddr6 *mc)
705{
706 if (atomic_dec_and_test(&mc->mca_refcnt)) {
707 in6_dev_put(mc->idev);
708 kfree(mc);
709 }
710}
711
712static void igmp6_group_added(struct ifmcaddr6 *mc)
713{
714 struct net_device *dev = mc->idev->dev;
715 char buf[MAX_ADDR_LEN];
716
717 spin_lock_bh(&mc->mca_lock);
718 if (!(mc->mca_flags&MAF_LOADED)) {
719 mc->mca_flags |= MAF_LOADED;
720 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
721 dev_mc_add(dev, buf, dev->addr_len, 0);
722 }
723 spin_unlock_bh(&mc->mca_lock);
724
725 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
726 return;
727
728 if (MLD_V1_SEEN(mc->idev)) {
729 igmp6_join_group(mc);
730 return;
731 }
732 /* else v2 */
733
734 mc->mca_crcount = mc->idev->mc_qrv;
735 mld_ifc_event(mc->idev);
736}
737
738static void igmp6_group_dropped(struct ifmcaddr6 *mc)
739{
740 struct net_device *dev = mc->idev->dev;
741 char buf[MAX_ADDR_LEN];
742
743 spin_lock_bh(&mc->mca_lock);
744 if (mc->mca_flags&MAF_LOADED) {
745 mc->mca_flags &= ~MAF_LOADED;
746 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
747 dev_mc_delete(dev, buf, dev->addr_len, 0);
748 }
749
750 if (mc->mca_flags & MAF_NOREPORT)
751 goto done;
752 spin_unlock_bh(&mc->mca_lock);
753
754 if (!mc->idev->dead)
755 igmp6_leave_group(mc);
756
757 spin_lock_bh(&mc->mca_lock);
758 if (del_timer(&mc->mca_timer))
759 atomic_dec(&mc->mca_refcnt);
760done:
761 ip6_mc_clear_src(mc);
762 spin_unlock_bh(&mc->mca_lock);
763}
764
765/*
766 * deleted ifmcaddr6 manipulation
767 */
768static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
769{
770 struct ifmcaddr6 *pmc;
771
772 /* this is an "ifmcaddr6" for convenience; only the fields below
773 * are actually used. In particular, the refcnt and users are not
774 * used for management of the delete list. Using the same structure
775 * for deleted items allows change reports to use common code with
776 * non-deleted or query-response MCA's.
777 */
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800778 pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (!pmc)
780 return;
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 spin_lock_bh(&im->mca_lock);
783 spin_lock_init(&pmc->mca_lock);
784 pmc->idev = im->idev;
785 in6_dev_hold(idev);
786 pmc->mca_addr = im->mca_addr;
787 pmc->mca_crcount = idev->mc_qrv;
788 pmc->mca_sfmode = im->mca_sfmode;
789 if (pmc->mca_sfmode == MCAST_INCLUDE) {
790 struct ip6_sf_list *psf;
791
792 pmc->mca_tomb = im->mca_tomb;
793 pmc->mca_sources = im->mca_sources;
794 im->mca_tomb = im->mca_sources = NULL;
795 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
796 psf->sf_crcount = pmc->mca_crcount;
797 }
798 spin_unlock_bh(&im->mca_lock);
799
800 write_lock_bh(&idev->mc_lock);
801 pmc->next = idev->mc_tomb;
802 idev->mc_tomb = pmc;
803 write_unlock_bh(&idev->mc_lock);
804}
805
806static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *pmca)
807{
808 struct ifmcaddr6 *pmc, *pmc_prev;
809 struct ip6_sf_list *psf, *psf_next;
810
811 write_lock_bh(&idev->mc_lock);
812 pmc_prev = NULL;
813 for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
814 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
815 break;
816 pmc_prev = pmc;
817 }
818 if (pmc) {
819 if (pmc_prev)
820 pmc_prev->next = pmc->next;
821 else
822 idev->mc_tomb = pmc->next;
823 }
824 write_unlock_bh(&idev->mc_lock);
825 if (pmc) {
826 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
827 psf_next = psf->sf_next;
828 kfree(psf);
829 }
830 in6_dev_put(pmc->idev);
831 kfree(pmc);
832 }
833}
834
835static void mld_clear_delrec(struct inet6_dev *idev)
836{
837 struct ifmcaddr6 *pmc, *nextpmc;
838
839 write_lock_bh(&idev->mc_lock);
840 pmc = idev->mc_tomb;
841 idev->mc_tomb = NULL;
842 write_unlock_bh(&idev->mc_lock);
843
844 for (; pmc; pmc = nextpmc) {
845 nextpmc = pmc->next;
846 ip6_mc_clear_src(pmc);
847 in6_dev_put(pmc->idev);
848 kfree(pmc);
849 }
850
851 /* clear dead sources, too */
852 read_lock_bh(&idev->lock);
853 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
854 struct ip6_sf_list *psf, *psf_next;
855
856 spin_lock_bh(&pmc->mca_lock);
857 psf = pmc->mca_tomb;
858 pmc->mca_tomb = NULL;
859 spin_unlock_bh(&pmc->mca_lock);
860 for (; psf; psf=psf_next) {
861 psf_next = psf->sf_next;
862 kfree(psf);
863 }
864 }
865 read_unlock_bh(&idev->lock);
866}
867
868
869/*
870 * device multicast group inc (add if not found)
871 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900872int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874 struct ifmcaddr6 *mc;
875 struct inet6_dev *idev;
876
877 idev = in6_dev_get(dev);
878
879 if (idev == NULL)
880 return -EINVAL;
881
882 write_lock_bh(&idev->lock);
883 if (idev->dead) {
884 write_unlock_bh(&idev->lock);
885 in6_dev_put(idev);
886 return -ENODEV;
887 }
888
889 for (mc = idev->mc_list; mc; mc = mc->next) {
890 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
891 mc->mca_users++;
892 write_unlock_bh(&idev->lock);
893 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
894 NULL, 0);
895 in6_dev_put(idev);
896 return 0;
897 }
898 }
899
900 /*
901 * not found: create a new one.
902 */
903
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800904 mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 if (mc == NULL) {
907 write_unlock_bh(&idev->lock);
908 in6_dev_put(idev);
909 return -ENOMEM;
910 }
911
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800912 setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 ipv6_addr_copy(&mc->mca_addr, addr);
915 mc->idev = idev;
916 mc->mca_users = 1;
917 /* mca_stamp should be updated upon changes */
918 mc->mca_cstamp = mc->mca_tstamp = jiffies;
919 atomic_set(&mc->mca_refcnt, 2);
920 spin_lock_init(&mc->mca_lock);
921
922 /* initial mode is (EX, empty) */
923 mc->mca_sfmode = MCAST_EXCLUDE;
924 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
925
926 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
927 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
928 mc->mca_flags |= MAF_NOREPORT;
929
930 mc->next = idev->mc_list;
931 idev->mc_list = mc;
932 write_unlock_bh(&idev->lock);
933
934 mld_del_delrec(idev, &mc->mca_addr);
935 igmp6_group_added(mc);
936 ma_put(mc);
937 return 0;
938}
939
940/*
941 * device multicast group del
942 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900943int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
945 struct ifmcaddr6 *ma, **map;
946
947 write_lock_bh(&idev->lock);
948 for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
949 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
950 if (--ma->mca_users == 0) {
951 *map = ma->next;
952 write_unlock_bh(&idev->lock);
953
954 igmp6_group_dropped(ma);
955
956 ma_put(ma);
957 return 0;
958 }
959 write_unlock_bh(&idev->lock);
960 return 0;
961 }
962 }
963 write_unlock_bh(&idev->lock);
964
965 return -ENOENT;
966}
967
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900968int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
970 struct inet6_dev *idev = in6_dev_get(dev);
971 int err;
972
973 if (!idev)
974 return -ENODEV;
975
976 err = __ipv6_dev_mc_dec(idev, addr);
977
978 in6_dev_put(idev);
979
980 return err;
981}
982
983/*
984 * identify MLD packets for MLD filter exceptions
985 */
986int ipv6_is_mld(struct sk_buff *skb, int nexthdr)
987{
988 struct icmp6hdr *pic;
989
990 if (nexthdr != IPPROTO_ICMPV6)
991 return 0;
992
993 if (!pskb_may_pull(skb, sizeof(struct icmp6hdr)))
994 return 0;
995
Arnaldo Carvalho de Melocc70ab22007-03-13 14:03:22 -0300996 pic = icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 switch (pic->icmp6_type) {
999 case ICMPV6_MGM_QUERY:
1000 case ICMPV6_MGM_REPORT:
1001 case ICMPV6_MGM_REDUCTION:
1002 case ICMPV6_MLD2_REPORT:
1003 return 1;
1004 default:
1005 break;
1006 }
1007 return 0;
1008}
1009
1010/*
1011 * check if the interface/address pair is valid
1012 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001013int ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
1014 const struct in6_addr *src_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
1016 struct inet6_dev *idev;
1017 struct ifmcaddr6 *mc;
1018 int rv = 0;
1019
1020 idev = in6_dev_get(dev);
1021 if (idev) {
1022 read_lock_bh(&idev->lock);
1023 for (mc = idev->mc_list; mc; mc=mc->next) {
1024 if (ipv6_addr_equal(&mc->mca_addr, group))
1025 break;
1026 }
1027 if (mc) {
1028 if (src_addr && !ipv6_addr_any(src_addr)) {
1029 struct ip6_sf_list *psf;
1030
1031 spin_lock_bh(&mc->mca_lock);
1032 for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
1033 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
1034 break;
1035 }
1036 if (psf)
1037 rv = psf->sf_count[MCAST_INCLUDE] ||
1038 psf->sf_count[MCAST_EXCLUDE] !=
1039 mc->mca_sfcount[MCAST_EXCLUDE];
1040 else
1041 rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
1042 spin_unlock_bh(&mc->mca_lock);
1043 } else
1044 rv = 1; /* don't filter unspecified source */
1045 }
1046 read_unlock_bh(&idev->lock);
1047 in6_dev_put(idev);
1048 }
1049 return rv;
1050}
1051
1052static void mld_gq_start_timer(struct inet6_dev *idev)
1053{
1054 int tv = net_random() % idev->mc_maxdelay;
1055
1056 idev->mc_gq_running = 1;
1057 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
1058 in6_dev_hold(idev);
1059}
1060
1061static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
1062{
1063 int tv = net_random() % delay;
1064
1065 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1066 in6_dev_hold(idev);
1067}
1068
1069/*
1070 * IGMP handling (alias multicast ICMPv6 messages)
1071 */
1072
1073static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1074{
1075 unsigned long delay = resptime;
1076
1077 /* Do not start timer for these addresses */
1078 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1079 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1080 return;
1081
1082 if (del_timer(&ma->mca_timer)) {
1083 atomic_dec(&ma->mca_refcnt);
1084 delay = ma->mca_timer.expires - jiffies;
1085 }
1086
1087 if (delay >= resptime) {
1088 if (resptime)
1089 delay = net_random() % resptime;
1090 else
1091 delay = 1;
1092 }
1093 ma->mca_timer.expires = jiffies + delay;
1094 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1095 atomic_inc(&ma->mca_refcnt);
1096 ma->mca_flags |= MAF_TIMER_RUNNING;
1097}
1098
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001099/* mark EXCLUDE-mode sources */
1100static int mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 struct in6_addr *srcs)
1102{
1103 struct ip6_sf_list *psf;
1104 int i, scount;
1105
1106 scount = 0;
1107 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1108 if (scount == nsrcs)
1109 break;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001110 for (i=0; i<nsrcs; i++) {
1111 /* skip inactive filters */
1112 if (pmc->mca_sfcount[MCAST_INCLUDE] ||
1113 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1114 psf->sf_count[MCAST_EXCLUDE])
1115 continue;
1116 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1117 scount++;
1118 break;
1119 }
1120 }
1121 }
1122 pmc->mca_flags &= ~MAF_GSQUERY;
1123 if (scount == nsrcs) /* all sources excluded */
1124 return 0;
1125 return 1;
1126}
1127
1128static int mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1129 struct in6_addr *srcs)
1130{
1131 struct ip6_sf_list *psf;
1132 int i, scount;
1133
1134 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1135 return mld_xmarksources(pmc, nsrcs, srcs);
1136
1137 /* mark INCLUDE-mode sources */
1138
1139 scount = 0;
1140 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1141 if (scount == nsrcs)
1142 break;
1143 for (i=0; i<nsrcs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1145 psf->sf_gsresp = 1;
1146 scount++;
1147 break;
1148 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001151 if (!scount) {
1152 pmc->mca_flags &= ~MAF_GSQUERY;
1153 return 0;
1154 }
1155 pmc->mca_flags |= MAF_GSQUERY;
1156 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
1159int igmp6_event_query(struct sk_buff *skb)
1160{
Yan Zheng97300b52005-10-31 20:09:45 +08001161 struct mld2_query *mlh2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 struct ifmcaddr6 *ma;
1163 struct in6_addr *group;
1164 unsigned long max_delay;
1165 struct inet6_dev *idev;
1166 struct icmp6hdr *hdr;
1167 int group_type;
1168 int mark = 0;
1169 int len;
1170
1171 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1172 return -EINVAL;
1173
1174 /* compute payload length excluding extension headers */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001175 len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001176 len -= skb_network_header_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 /* Drop queries with not link local source */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001179 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 return -EINVAL;
1181
1182 idev = in6_dev_get(skb->dev);
1183
1184 if (idev == NULL)
1185 return 0;
1186
Arnaldo Carvalho de Melocc70ab22007-03-13 14:03:22 -03001187 hdr = icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 group = (struct in6_addr *) (hdr + 1);
1189 group_type = ipv6_addr_type(group);
1190
1191 if (group_type != IPV6_ADDR_ANY &&
1192 !(group_type&IPV6_ADDR_MULTICAST)) {
1193 in6_dev_put(idev);
1194 return -EINVAL;
1195 }
1196
1197 if (len == 24) {
1198 int switchback;
1199 /* MLDv1 router present */
1200
1201 /* Translate milliseconds to jiffies */
1202 max_delay = (ntohs(hdr->icmp6_maxdelay)*HZ)/1000;
1203
1204 switchback = (idev->mc_qrv + 1) * max_delay;
1205 idev->mc_v1_seen = jiffies + switchback;
1206
1207 /* cancel the interface change timer */
1208 idev->mc_ifc_count = 0;
1209 if (del_timer(&idev->mc_ifc_timer))
1210 __in6_dev_put(idev);
1211 /* clear deleted report items */
1212 mld_clear_delrec(idev);
1213 } else if (len >= 28) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001214 int srcs_offset = sizeof(struct mld2_query) -
Yan Zheng97300b52005-10-31 20:09:45 +08001215 sizeof(struct icmp6hdr);
1216 if (!pskb_may_pull(skb, srcs_offset)) {
1217 in6_dev_put(idev);
1218 return -EINVAL;
1219 }
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001220 mlh2 = (struct mld2_query *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 max_delay = (MLDV2_MRC(ntohs(mlh2->mrc))*HZ)/1000;
1222 if (!max_delay)
1223 max_delay = 1;
1224 idev->mc_maxdelay = max_delay;
1225 if (mlh2->qrv)
1226 idev->mc_qrv = mlh2->qrv;
1227 if (group_type == IPV6_ADDR_ANY) { /* general query */
1228 if (mlh2->nsrcs) {
1229 in6_dev_put(idev);
1230 return -EINVAL; /* no sources allowed */
1231 }
1232 mld_gq_start_timer(idev);
1233 in6_dev_put(idev);
1234 return 0;
1235 }
1236 /* mark sources to include, if group & source-specific */
Yan Zheng97300b52005-10-31 20:09:45 +08001237 if (mlh2->nsrcs != 0) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001238 if (!pskb_may_pull(skb, srcs_offset +
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001239 ntohs(mlh2->nsrcs) * sizeof(struct in6_addr))) {
Yan Zheng97300b52005-10-31 20:09:45 +08001240 in6_dev_put(idev);
1241 return -EINVAL;
1242 }
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001243 mlh2 = (struct mld2_query *)skb_transport_header(skb);
Yan Zheng97300b52005-10-31 20:09:45 +08001244 mark = 1;
1245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 } else {
1247 in6_dev_put(idev);
1248 return -EINVAL;
1249 }
1250
1251 read_lock_bh(&idev->lock);
1252 if (group_type == IPV6_ADDR_ANY) {
1253 for (ma = idev->mc_list; ma; ma=ma->next) {
1254 spin_lock_bh(&ma->mca_lock);
1255 igmp6_group_queried(ma, max_delay);
1256 spin_unlock_bh(&ma->mca_lock);
1257 }
1258 } else {
1259 for (ma = idev->mc_list; ma; ma=ma->next) {
David L Stevens7add2a42006-01-24 13:06:39 -08001260 if (!ipv6_addr_equal(group, &ma->mca_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 continue;
1262 spin_lock_bh(&ma->mca_lock);
1263 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1264 /* gsquery <- gsquery && mark */
1265 if (!mark)
1266 ma->mca_flags &= ~MAF_GSQUERY;
1267 } else {
1268 /* gsquery <- mark */
1269 if (mark)
1270 ma->mca_flags |= MAF_GSQUERY;
1271 else
1272 ma->mca_flags &= ~MAF_GSQUERY;
1273 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001274 if (!(ma->mca_flags & MAF_GSQUERY) ||
David L Stevens7add2a42006-01-24 13:06:39 -08001275 mld_marksources(ma, ntohs(mlh2->nsrcs), mlh2->srcs))
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001276 igmp6_group_queried(ma, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 spin_unlock_bh(&ma->mca_lock);
David L Stevens7add2a42006-01-24 13:06:39 -08001278 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280 }
1281 read_unlock_bh(&idev->lock);
1282 in6_dev_put(idev);
1283
1284 return 0;
1285}
1286
1287
1288int igmp6_event_report(struct sk_buff *skb)
1289{
1290 struct ifmcaddr6 *ma;
1291 struct in6_addr *addrp;
1292 struct inet6_dev *idev;
1293 struct icmp6hdr *hdr;
1294 int addr_type;
1295
1296 /* Our own report looped back. Ignore it. */
1297 if (skb->pkt_type == PACKET_LOOPBACK)
1298 return 0;
1299
David Stevens24c69272005-12-02 20:32:59 -08001300 /* send our report if the MC router may not have heard this report */
1301 if (skb->pkt_type != PACKET_MULTICAST &&
1302 skb->pkt_type != PACKET_BROADCAST)
1303 return 0;
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1306 return -EINVAL;
1307
Arnaldo Carvalho de Melocc70ab22007-03-13 14:03:22 -03001308 hdr = icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 /* Drop reports with not link local source */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001311 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001312 if (addr_type != IPV6_ADDR_ANY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 !(addr_type&IPV6_ADDR_LINKLOCAL))
1314 return -EINVAL;
1315
1316 addrp = (struct in6_addr *) (hdr + 1);
1317
1318 idev = in6_dev_get(skb->dev);
1319 if (idev == NULL)
1320 return -ENODEV;
1321
1322 /*
1323 * Cancel the timer for this group
1324 */
1325
1326 read_lock_bh(&idev->lock);
1327 for (ma = idev->mc_list; ma; ma=ma->next) {
1328 if (ipv6_addr_equal(&ma->mca_addr, addrp)) {
1329 spin_lock(&ma->mca_lock);
1330 if (del_timer(&ma->mca_timer))
1331 atomic_dec(&ma->mca_refcnt);
1332 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1333 spin_unlock(&ma->mca_lock);
1334 break;
1335 }
1336 }
1337 read_unlock_bh(&idev->lock);
1338 in6_dev_put(idev);
1339 return 0;
1340}
1341
1342static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1343 int gdeleted, int sdeleted)
1344{
1345 switch (type) {
1346 case MLD2_MODE_IS_INCLUDE:
1347 case MLD2_MODE_IS_EXCLUDE:
1348 if (gdeleted || sdeleted)
1349 return 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001350 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1351 if (pmc->mca_sfmode == MCAST_INCLUDE)
1352 return 1;
1353 /* don't include if this source is excluded
1354 * in all filters
1355 */
1356 if (psf->sf_count[MCAST_INCLUDE])
David L Stevens7add2a42006-01-24 13:06:39 -08001357 return type == MLD2_MODE_IS_INCLUDE;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001358 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1359 psf->sf_count[MCAST_EXCLUDE];
1360 }
1361 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 case MLD2_CHANGE_TO_INCLUDE:
1363 if (gdeleted || sdeleted)
1364 return 0;
1365 return psf->sf_count[MCAST_INCLUDE] != 0;
1366 case MLD2_CHANGE_TO_EXCLUDE:
1367 if (gdeleted || sdeleted)
1368 return 0;
1369 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1370 psf->sf_count[MCAST_INCLUDE])
1371 return 0;
1372 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1373 psf->sf_count[MCAST_EXCLUDE];
1374 case MLD2_ALLOW_NEW_SOURCES:
1375 if (gdeleted || !psf->sf_crcount)
1376 return 0;
1377 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1378 case MLD2_BLOCK_OLD_SOURCES:
1379 if (pmc->mca_sfmode == MCAST_INCLUDE)
1380 return gdeleted || (psf->sf_crcount && sdeleted);
1381 return psf->sf_crcount && !gdeleted && !sdeleted;
1382 }
1383 return 0;
1384}
1385
1386static int
1387mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1388{
1389 struct ip6_sf_list *psf;
1390 int scount = 0;
1391
1392 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1393 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1394 continue;
1395 scount++;
1396 }
1397 return scount;
1398}
1399
1400static struct sk_buff *mld_newpack(struct net_device *dev, int size)
1401{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001402 struct net *net = dev_net(dev);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001403 struct sock *sk = net->ipv6.igmp_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 struct sk_buff *skb;
1405 struct mld2_report *pmr;
1406 struct in6_addr addr_buf;
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001407 const struct in6_addr *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 int err;
1409 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1410 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1411 IPV6_TLV_PADN, 0 };
1412
1413 /* we assume size > sizeof(ra) here */
1414 skb = sock_alloc_send_skb(sk, size + LL_RESERVED_SPACE(dev), 1, &err);
1415
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001416 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return NULL;
1418
1419 skb_reserve(skb, LL_RESERVED_SPACE(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Neil Horman95c385b2007-04-25 17:08:10 -07001421 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 /* <draft-ietf-magma-mld-source-05.txt>:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001423 * use unspecified address as the source address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 * when a valid link-local address is not available.
1425 */
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001426 saddr = &in6addr_any;
1427 } else
1428 saddr = &addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001430 ip6_nd_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1433
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001434 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -03001435 skb_put(skb, sizeof(*pmr));
1436 pmr = (struct mld2_report *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 pmr->type = ICMPV6_MLD2_REPORT;
1438 pmr->resv1 = 0;
1439 pmr->csum = 0;
1440 pmr->resv2 = 0;
1441 pmr->ngrec = 0;
1442 return skb;
1443}
1444
1445static void mld_sendpack(struct sk_buff *skb)
1446{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001447 struct ipv6hdr *pip6 = ipv6_hdr(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001448 struct mld2_report *pmr =
1449 (struct mld2_report *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 int payload_len, mldlen;
1451 struct inet6_dev *idev = in6_dev_get(skb->dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001452 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 int err;
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001454 struct flowi fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001456 IP6_INC_STATS(idev, IPSTATS_MIB_OUTREQUESTS);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001457 payload_len = (skb->tail - skb->network_header) - sizeof(*pip6);
1458 mldlen = skb->tail - skb->transport_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 pip6->payload_len = htons(payload_len);
1460
1461 pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001462 IPPROTO_ICMPV6, csum_partial(skb_transport_header(skb),
1463 mldlen, 0));
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001464
1465 skb->dst = icmp6_dst_alloc(skb->dev, NULL, &ipv6_hdr(skb)->daddr);
1466
1467 if (!skb->dst) {
1468 err = -ENOMEM;
1469 goto err_out;
1470 }
1471
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001472 icmpv6_flow_init(net->ipv6.igmp_sk, &fl, ICMPV6_MLD2_REPORT,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001473 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1474 skb->dev->ifindex);
1475
1476 err = xfrm_lookup(&skb->dst, &fl, NULL, 0);
1477 if (err)
1478 goto err_out;
1479
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001480 err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001481 dst_output);
1482out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 if (!err) {
David L Stevens14878f72007-09-16 16:52:35 -07001484 ICMP6MSGOUT_INC_STATS_BH(idev, ICMPV6_MLD2_REPORT);
1485 ICMP6_INC_STATS_BH(idev, ICMP6_MIB_OUTMSGS);
1486 IP6_INC_STATS_BH(idev, IPSTATS_MIB_OUTMCASTPKTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 } else
David L Stevens14878f72007-09-16 16:52:35 -07001488 IP6_INC_STATS_BH(idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 if (likely(idev != NULL))
1491 in6_dev_put(idev);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001492 return;
1493
1494err_out:
1495 kfree_skb(skb);
1496 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
1498
1499static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1500{
Yan Zhengfab10fe2005-10-05 12:08:13 -07001501 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
1504static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1505 int type, struct mld2_grec **ppgr)
1506{
1507 struct net_device *dev = pmc->idev->dev;
1508 struct mld2_report *pmr;
1509 struct mld2_grec *pgr;
1510
1511 if (!skb)
1512 skb = mld_newpack(dev, dev->mtu);
1513 if (!skb)
1514 return NULL;
1515 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1516 pgr->grec_type = type;
1517 pgr->grec_auxwords = 0;
1518 pgr->grec_nsrcs = 0;
1519 pgr->grec_mca = pmc->mca_addr; /* structure copy */
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001520 pmr = (struct mld2_report *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
1522 *ppgr = pgr;
1523 return skb;
1524}
1525
1526#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1527 skb_tailroom(skb)) : 0)
1528
1529static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1530 int type, int gdeleted, int sdeleted)
1531{
1532 struct net_device *dev = pmc->idev->dev;
1533 struct mld2_report *pmr;
1534 struct mld2_grec *pgr = NULL;
1535 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001536 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538 if (pmc->mca_flags & MAF_NOREPORT)
1539 return skb;
1540
1541 isquery = type == MLD2_MODE_IS_INCLUDE ||
1542 type == MLD2_MODE_IS_EXCLUDE;
1543 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1544 type == MLD2_CHANGE_TO_EXCLUDE;
1545
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001546 stotal = scount = 0;
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1549
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001550 if (!*psf_list)
1551 goto empty_source;
1552
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001553 pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555 /* EX and TO_EX get a fresh packet, if needed */
1556 if (truncate) {
1557 if (pmr && pmr->ngrec &&
1558 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1559 if (skb)
1560 mld_sendpack(skb);
1561 skb = mld_newpack(dev, dev->mtu);
1562 }
1563 }
1564 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 psf_prev = NULL;
1566 for (psf=*psf_list; psf; psf=psf_next) {
1567 struct in6_addr *psrc;
1568
1569 psf_next = psf->sf_next;
1570
1571 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1572 psf_prev = psf;
1573 continue;
1574 }
1575
1576 /* clear marks on query responses */
1577 if (isquery)
1578 psf->sf_gsresp = 0;
1579
1580 if (AVAILABLE(skb) < sizeof(*psrc) +
1581 first*sizeof(struct mld2_grec)) {
1582 if (truncate && !first)
1583 break; /* truncate these */
1584 if (pgr)
1585 pgr->grec_nsrcs = htons(scount);
1586 if (skb)
1587 mld_sendpack(skb);
1588 skb = mld_newpack(dev, dev->mtu);
1589 first = 1;
1590 scount = 0;
1591 }
1592 if (first) {
1593 skb = add_grhead(skb, pmc, type, &pgr);
1594 first = 0;
1595 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -08001596 if (!skb)
1597 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1599 *psrc = psf->sf_addr;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001600 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1602 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1603 psf->sf_crcount--;
1604 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1605 if (psf_prev)
1606 psf_prev->sf_next = psf->sf_next;
1607 else
1608 *psf_list = psf->sf_next;
1609 kfree(psf);
1610 continue;
1611 }
1612 }
1613 psf_prev = psf;
1614 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001615
1616empty_source:
1617 if (!stotal) {
1618 if (type == MLD2_ALLOW_NEW_SOURCES ||
1619 type == MLD2_BLOCK_OLD_SOURCES)
1620 return skb;
1621 if (pmc->mca_crcount || isquery) {
1622 /* make sure we have room for group header */
1623 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1624 mld_sendpack(skb);
1625 skb = NULL; /* add_grhead will get a new one */
1626 }
1627 skb = add_grhead(skb, pmc, type, &pgr);
1628 }
1629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 if (pgr)
1631 pgr->grec_nsrcs = htons(scount);
1632
1633 if (isquery)
1634 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1635 return skb;
1636}
1637
1638static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1639{
1640 struct sk_buff *skb = NULL;
1641 int type;
1642
1643 if (!pmc) {
1644 read_lock_bh(&idev->lock);
1645 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1646 if (pmc->mca_flags & MAF_NOREPORT)
1647 continue;
1648 spin_lock_bh(&pmc->mca_lock);
1649 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1650 type = MLD2_MODE_IS_EXCLUDE;
1651 else
1652 type = MLD2_MODE_IS_INCLUDE;
1653 skb = add_grec(skb, pmc, type, 0, 0);
1654 spin_unlock_bh(&pmc->mca_lock);
1655 }
1656 read_unlock_bh(&idev->lock);
1657 } else {
1658 spin_lock_bh(&pmc->mca_lock);
1659 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1660 type = MLD2_MODE_IS_EXCLUDE;
1661 else
1662 type = MLD2_MODE_IS_INCLUDE;
1663 skb = add_grec(skb, pmc, type, 0, 0);
1664 spin_unlock_bh(&pmc->mca_lock);
1665 }
1666 if (skb)
1667 mld_sendpack(skb);
1668}
1669
1670/*
1671 * remove zero-count source records from a source filter list
1672 */
1673static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1674{
1675 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1676
1677 psf_prev = NULL;
1678 for (psf=*ppsf; psf; psf = psf_next) {
1679 psf_next = psf->sf_next;
1680 if (psf->sf_crcount == 0) {
1681 if (psf_prev)
1682 psf_prev->sf_next = psf->sf_next;
1683 else
1684 *ppsf = psf->sf_next;
1685 kfree(psf);
1686 } else
1687 psf_prev = psf;
1688 }
1689}
1690
1691static void mld_send_cr(struct inet6_dev *idev)
1692{
1693 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1694 struct sk_buff *skb = NULL;
1695 int type, dtype;
1696
1697 read_lock_bh(&idev->lock);
1698 write_lock_bh(&idev->mc_lock);
1699
1700 /* deleted MCA's */
1701 pmc_prev = NULL;
1702 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1703 pmc_next = pmc->next;
1704 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1705 type = MLD2_BLOCK_OLD_SOURCES;
1706 dtype = MLD2_BLOCK_OLD_SOURCES;
1707 skb = add_grec(skb, pmc, type, 1, 0);
1708 skb = add_grec(skb, pmc, dtype, 1, 1);
1709 }
1710 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1712 type = MLD2_CHANGE_TO_INCLUDE;
1713 skb = add_grec(skb, pmc, type, 1, 0);
1714 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001715 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 if (pmc->mca_crcount == 0) {
1717 mld_clear_zeros(&pmc->mca_tomb);
1718 mld_clear_zeros(&pmc->mca_sources);
1719 }
1720 }
1721 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1722 !pmc->mca_sources) {
1723 if (pmc_prev)
1724 pmc_prev->next = pmc_next;
1725 else
1726 idev->mc_tomb = pmc_next;
1727 in6_dev_put(pmc->idev);
1728 kfree(pmc);
1729 } else
1730 pmc_prev = pmc;
1731 }
1732 write_unlock_bh(&idev->mc_lock);
1733
1734 /* change recs */
1735 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1736 spin_lock_bh(&pmc->mca_lock);
1737 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1738 type = MLD2_BLOCK_OLD_SOURCES;
1739 dtype = MLD2_ALLOW_NEW_SOURCES;
1740 } else {
1741 type = MLD2_ALLOW_NEW_SOURCES;
1742 dtype = MLD2_BLOCK_OLD_SOURCES;
1743 }
1744 skb = add_grec(skb, pmc, type, 0, 0);
1745 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1746
1747 /* filter mode changes */
1748 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1750 type = MLD2_CHANGE_TO_EXCLUDE;
1751 else
1752 type = MLD2_CHANGE_TO_INCLUDE;
1753 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001754 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 }
1756 spin_unlock_bh(&pmc->mca_lock);
1757 }
1758 read_unlock_bh(&idev->lock);
1759 if (!skb)
1760 return;
1761 (void) mld_sendpack(skb);
1762}
1763
1764static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1765{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001766 struct net *net = dev_net(dev);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001767 struct sock *sk = net->ipv6.igmp_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 struct inet6_dev *idev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001769 struct sk_buff *skb;
1770 struct icmp6hdr *hdr;
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001771 const struct in6_addr *snd_addr, *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 struct in6_addr *addrp;
1773 struct in6_addr addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 int err, len, payload_len, full_len;
1775 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1776 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1777 IPV6_TLV_PADN, 0 };
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001778 struct flowi fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001780 rcu_read_lock();
1781 IP6_INC_STATS(__in6_dev_get(dev),
1782 IPSTATS_MIB_OUTREQUESTS);
1783 rcu_read_unlock();
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09001784 if (type == ICMPV6_MGM_REDUCTION)
1785 snd_addr = &in6addr_linklocal_allrouters;
1786 else
1787 snd_addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1790 payload_len = len + sizeof(ra);
1791 full_len = sizeof(struct ipv6hdr) + payload_len;
1792
1793 skb = sock_alloc_send_skb(sk, LL_RESERVED_SPACE(dev) + full_len, 1, &err);
1794
1795 if (skb == NULL) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001796 rcu_read_lock();
1797 IP6_INC_STATS(__in6_dev_get(dev),
1798 IPSTATS_MIB_OUTDISCARDS);
1799 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return;
1801 }
1802
1803 skb_reserve(skb, LL_RESERVED_SPACE(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Neil Horman95c385b2007-04-25 17:08:10 -07001805 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 /* <draft-ietf-magma-mld-source-05.txt>:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001807 * use unspecified address as the source address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 * when a valid link-local address is not available.
1809 */
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001810 saddr = &in6addr_any;
1811 } else
1812 saddr = &addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001814 ip6_nd_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1817
1818 hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
1819 memset(hdr, 0, sizeof(struct icmp6hdr));
1820 hdr->icmp6_type = type;
1821
1822 addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
1823 ipv6_addr_copy(addrp, addr);
1824
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001825 hdr->icmp6_cksum = csum_ipv6_magic(saddr, snd_addr, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 IPPROTO_ICMPV6,
1827 csum_partial((__u8 *) hdr, len, 0));
1828
1829 idev = in6_dev_get(skb->dev);
1830
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001831 skb->dst = icmp6_dst_alloc(skb->dev, NULL, &ipv6_hdr(skb)->daddr);
1832 if (!skb->dst) {
1833 err = -ENOMEM;
1834 goto err_out;
1835 }
1836
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001837 icmpv6_flow_init(sk, &fl, type,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001838 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1839 skb->dev->ifindex);
1840
1841 err = xfrm_lookup(&skb->dst, &fl, NULL, 0);
1842 if (err)
1843 goto err_out;
1844
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001845 err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001846 dst_output);
1847out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (!err) {
David L Stevens14878f72007-09-16 16:52:35 -07001849 ICMP6MSGOUT_INC_STATS(idev, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTMSGS);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001851 IP6_INC_STATS(idev, IPSTATS_MIB_OUTMCASTPKTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 } else
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001853 IP6_INC_STATS(idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 if (likely(idev != NULL))
1856 in6_dev_put(idev);
1857 return;
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001858
1859err_out:
1860 kfree_skb(skb);
1861 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862}
1863
1864static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1865 struct in6_addr *psfsrc)
1866{
1867 struct ip6_sf_list *psf, *psf_prev;
1868 int rv = 0;
1869
1870 psf_prev = NULL;
1871 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1872 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1873 break;
1874 psf_prev = psf;
1875 }
1876 if (!psf || psf->sf_count[sfmode] == 0) {
1877 /* source filter not found, or count wrong => bug */
1878 return -ESRCH;
1879 }
1880 psf->sf_count[sfmode]--;
1881 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1882 struct inet6_dev *idev = pmc->idev;
1883
1884 /* no more filters for this source */
1885 if (psf_prev)
1886 psf_prev->sf_next = psf->sf_next;
1887 else
1888 pmc->mca_sources = psf->sf_next;
1889 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1890 !MLD_V1_SEEN(idev)) {
1891 psf->sf_crcount = idev->mc_qrv;
1892 psf->sf_next = pmc->mca_tomb;
1893 pmc->mca_tomb = psf;
1894 rv = 1;
1895 } else
1896 kfree(psf);
1897 }
1898 return rv;
1899}
1900
1901static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
1902 int sfmode, int sfcount, struct in6_addr *psfsrc,
1903 int delta)
1904{
1905 struct ifmcaddr6 *pmc;
1906 int changerec = 0;
1907 int i, err;
1908
1909 if (!idev)
1910 return -ENODEV;
1911 read_lock_bh(&idev->lock);
1912 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1913 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1914 break;
1915 }
1916 if (!pmc) {
1917 /* MCA not found?? bug */
1918 read_unlock_bh(&idev->lock);
1919 return -ESRCH;
1920 }
1921 spin_lock_bh(&pmc->mca_lock);
1922 sf_markstate(pmc);
1923 if (!delta) {
1924 if (!pmc->mca_sfcount[sfmode]) {
1925 spin_unlock_bh(&pmc->mca_lock);
1926 read_unlock_bh(&idev->lock);
1927 return -EINVAL;
1928 }
1929 pmc->mca_sfcount[sfmode]--;
1930 }
1931 err = 0;
1932 for (i=0; i<sfcount; i++) {
1933 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1934
1935 changerec |= rv > 0;
1936 if (!err && rv < 0)
1937 err = rv;
1938 }
1939 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1940 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1941 pmc->mca_sfcount[MCAST_INCLUDE]) {
1942 struct ip6_sf_list *psf;
1943
1944 /* filter mode change */
1945 pmc->mca_sfmode = MCAST_INCLUDE;
1946 pmc->mca_crcount = idev->mc_qrv;
1947 idev->mc_ifc_count = pmc->mca_crcount;
1948 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1949 psf->sf_crcount = 0;
1950 mld_ifc_event(pmc->idev);
1951 } else if (sf_setstate(pmc) || changerec)
1952 mld_ifc_event(pmc->idev);
1953 spin_unlock_bh(&pmc->mca_lock);
1954 read_unlock_bh(&idev->lock);
1955 return err;
1956}
1957
1958/*
1959 * Add multicast single-source filter to the interface list
1960 */
1961static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1962 struct in6_addr *psfsrc, int delta)
1963{
1964 struct ip6_sf_list *psf, *psf_prev;
1965
1966 psf_prev = NULL;
1967 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1968 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1969 break;
1970 psf_prev = psf;
1971 }
1972 if (!psf) {
Ingo Oeser0c600ed2006-03-20 23:01:32 -08001973 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if (!psf)
1975 return -ENOBUFS;
Ingo Oeser0c600ed2006-03-20 23:01:32 -08001976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 psf->sf_addr = *psfsrc;
1978 if (psf_prev) {
1979 psf_prev->sf_next = psf;
1980 } else
1981 pmc->mca_sources = psf;
1982 }
1983 psf->sf_count[sfmode]++;
1984 return 0;
1985}
1986
1987static void sf_markstate(struct ifmcaddr6 *pmc)
1988{
1989 struct ip6_sf_list *psf;
1990 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1991
1992 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1993 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1994 psf->sf_oldin = mca_xcount ==
1995 psf->sf_count[MCAST_EXCLUDE] &&
1996 !psf->sf_count[MCAST_INCLUDE];
1997 } else
1998 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1999}
2000
2001static int sf_setstate(struct ifmcaddr6 *pmc)
2002{
David L Stevens7add2a42006-01-24 13:06:39 -08002003 struct ip6_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2005 int qrv = pmc->idev->mc_qrv;
2006 int new_in, rv;
2007
2008 rv = 0;
2009 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
2010 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2011 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2012 !psf->sf_count[MCAST_INCLUDE];
2013 } else
2014 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevens7add2a42006-01-24 13:06:39 -08002015 if (new_in) {
2016 if (!psf->sf_oldin) {
Al Viroe80e28b2006-02-03 20:10:03 -05002017 struct ip6_sf_list *prev = NULL;
David L Stevens7add2a42006-01-24 13:06:39 -08002018
2019 for (dpsf=pmc->mca_tomb; dpsf;
2020 dpsf=dpsf->sf_next) {
2021 if (ipv6_addr_equal(&dpsf->sf_addr,
2022 &psf->sf_addr))
2023 break;
2024 prev = dpsf;
2025 }
2026 if (dpsf) {
2027 if (prev)
2028 prev->sf_next = dpsf->sf_next;
2029 else
2030 pmc->mca_tomb = dpsf->sf_next;
2031 kfree(dpsf);
2032 }
2033 psf->sf_crcount = qrv;
2034 rv++;
2035 }
2036 } else if (psf->sf_oldin) {
2037 psf->sf_crcount = 0;
2038 /*
2039 * add or update "delete" records if an active filter
2040 * is now inactive
2041 */
2042 for (dpsf=pmc->mca_tomb; dpsf; dpsf=dpsf->sf_next)
2043 if (ipv6_addr_equal(&dpsf->sf_addr,
2044 &psf->sf_addr))
2045 break;
2046 if (!dpsf) {
2047 dpsf = (struct ip6_sf_list *)
2048 kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2049 if (!dpsf)
2050 continue;
2051 *dpsf = *psf;
2052 /* pmc->mca_lock held by callers */
2053 dpsf->sf_next = pmc->mca_tomb;
2054 pmc->mca_tomb = dpsf;
2055 }
2056 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 rv++;
2058 }
2059 }
2060 return rv;
2061}
2062
2063/*
2064 * Add multicast source filter list to the interface list
2065 */
2066static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
2067 int sfmode, int sfcount, struct in6_addr *psfsrc,
2068 int delta)
2069{
2070 struct ifmcaddr6 *pmc;
2071 int isexclude;
2072 int i, err;
2073
2074 if (!idev)
2075 return -ENODEV;
2076 read_lock_bh(&idev->lock);
2077 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
2078 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2079 break;
2080 }
2081 if (!pmc) {
2082 /* MCA not found?? bug */
2083 read_unlock_bh(&idev->lock);
2084 return -ESRCH;
2085 }
2086 spin_lock_bh(&pmc->mca_lock);
2087
2088 sf_markstate(pmc);
2089 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2090 if (!delta)
2091 pmc->mca_sfcount[sfmode]++;
2092 err = 0;
2093 for (i=0; i<sfcount; i++) {
2094 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
2095 if (err)
2096 break;
2097 }
2098 if (err) {
2099 int j;
2100
2101 if (!delta)
2102 pmc->mca_sfcount[sfmode]--;
2103 for (j=0; j<i; j++)
2104 (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2105 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
2106 struct inet6_dev *idev = pmc->idev;
2107 struct ip6_sf_list *psf;
2108
2109 /* filter mode change */
2110 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2111 pmc->mca_sfmode = MCAST_EXCLUDE;
2112 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2113 pmc->mca_sfmode = MCAST_INCLUDE;
2114 /* else no filters; keep old mode for reports */
2115
2116 pmc->mca_crcount = idev->mc_qrv;
2117 idev->mc_ifc_count = pmc->mca_crcount;
2118 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
2119 psf->sf_crcount = 0;
2120 mld_ifc_event(idev);
2121 } else if (sf_setstate(pmc))
2122 mld_ifc_event(idev);
2123 spin_unlock_bh(&pmc->mca_lock);
2124 read_unlock_bh(&idev->lock);
2125 return err;
2126}
2127
2128static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2129{
2130 struct ip6_sf_list *psf, *nextpsf;
2131
2132 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
2133 nextpsf = psf->sf_next;
2134 kfree(psf);
2135 }
2136 pmc->mca_tomb = NULL;
2137 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
2138 nextpsf = psf->sf_next;
2139 kfree(psf);
2140 }
2141 pmc->mca_sources = NULL;
2142 pmc->mca_sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002143 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2145}
2146
2147
2148static void igmp6_join_group(struct ifmcaddr6 *ma)
2149{
2150 unsigned long delay;
2151
2152 if (ma->mca_flags & MAF_NOREPORT)
2153 return;
2154
2155 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2156
2157 delay = net_random() % IGMP6_UNSOLICITED_IVAL;
2158
2159 spin_lock_bh(&ma->mca_lock);
2160 if (del_timer(&ma->mca_timer)) {
2161 atomic_dec(&ma->mca_refcnt);
2162 delay = ma->mca_timer.expires - jiffies;
2163 }
2164
2165 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2166 atomic_inc(&ma->mca_refcnt);
2167 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2168 spin_unlock_bh(&ma->mca_lock);
2169}
2170
2171static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2172 struct inet6_dev *idev)
2173{
2174 int err;
2175
David L Stevens5ab4a6c2005-12-27 14:03:00 -08002176 /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
2177 * so no other readers or writers of iml or its sflist
2178 */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002179 if (!iml->sflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 /* any-source empty exclude case */
2181 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2182 }
2183 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2184 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2185 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2186 iml->sflist = NULL;
2187 return err;
2188}
2189
2190static void igmp6_leave_group(struct ifmcaddr6 *ma)
2191{
2192 if (MLD_V1_SEEN(ma->idev)) {
2193 if (ma->mca_flags & MAF_LAST_REPORTER)
2194 igmp6_send(&ma->mca_addr, ma->idev->dev,
2195 ICMPV6_MGM_REDUCTION);
2196 } else {
2197 mld_add_delrec(ma->idev, ma);
2198 mld_ifc_event(ma->idev);
2199 }
2200}
2201
2202static void mld_gq_timer_expire(unsigned long data)
2203{
2204 struct inet6_dev *idev = (struct inet6_dev *)data;
2205
2206 idev->mc_gq_running = 0;
2207 mld_send_report(idev, NULL);
2208 __in6_dev_put(idev);
2209}
2210
2211static void mld_ifc_timer_expire(unsigned long data)
2212{
2213 struct inet6_dev *idev = (struct inet6_dev *)data;
2214
2215 mld_send_cr(idev);
2216 if (idev->mc_ifc_count) {
2217 idev->mc_ifc_count--;
2218 if (idev->mc_ifc_count)
2219 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2220 }
2221 __in6_dev_put(idev);
2222}
2223
2224static void mld_ifc_event(struct inet6_dev *idev)
2225{
2226 if (MLD_V1_SEEN(idev))
2227 return;
2228 idev->mc_ifc_count = idev->mc_qrv;
2229 mld_ifc_start_timer(idev, 1);
2230}
2231
2232
2233static void igmp6_timer_handler(unsigned long data)
2234{
2235 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2236
2237 if (MLD_V1_SEEN(ma->idev))
2238 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2239 else
2240 mld_send_report(ma->idev, ma);
2241
2242 spin_lock(&ma->mca_lock);
2243 ma->mca_flags |= MAF_LAST_REPORTER;
2244 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2245 spin_unlock(&ma->mca_lock);
2246 ma_put(ma);
2247}
2248
2249/* Device going down */
2250
2251void ipv6_mc_down(struct inet6_dev *idev)
2252{
2253 struct ifmcaddr6 *i;
2254
2255 /* Withdraw multicast list */
2256
2257 read_lock_bh(&idev->lock);
2258 idev->mc_ifc_count = 0;
2259 if (del_timer(&idev->mc_ifc_timer))
2260 __in6_dev_put(idev);
2261 idev->mc_gq_running = 0;
2262 if (del_timer(&idev->mc_gq_timer))
2263 __in6_dev_put(idev);
2264
2265 for (i = idev->mc_list; i; i=i->next)
2266 igmp6_group_dropped(i);
2267 read_unlock_bh(&idev->lock);
2268
2269 mld_clear_delrec(idev);
2270}
2271
2272
2273/* Device going up */
2274
2275void ipv6_mc_up(struct inet6_dev *idev)
2276{
2277 struct ifmcaddr6 *i;
2278
2279 /* Install multicast list, except for all-nodes (already installed) */
2280
2281 read_lock_bh(&idev->lock);
2282 for (i = idev->mc_list; i; i=i->next)
2283 igmp6_group_added(i);
2284 read_unlock_bh(&idev->lock);
2285}
2286
2287/* IPv6 device initialization. */
2288
2289void ipv6_mc_init_dev(struct inet6_dev *idev)
2290{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 write_lock_bh(&idev->lock);
2292 rwlock_init(&idev->mc_lock);
2293 idev->mc_gq_running = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08002294 setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
2295 (unsigned long)idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 idev->mc_tomb = NULL;
2297 idev->mc_ifc_count = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08002298 setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
2299 (unsigned long)idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 idev->mc_qrv = MLD_QRV_DEFAULT;
2301 idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2302 idev->mc_v1_seen = 0;
2303 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
2306/*
2307 * Device is about to be destroyed: clean up.
2308 */
2309
2310void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2311{
2312 struct ifmcaddr6 *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
2314 /* Deactivate timers */
2315 ipv6_mc_down(idev);
2316
2317 /* Delete all-nodes address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2319 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2320 * fail.
2321 */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002322 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002324 if (idev->cnf.forwarding)
2325 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 write_lock_bh(&idev->lock);
2328 while ((i = idev->mc_list) != NULL) {
2329 idev->mc_list = i->next;
2330 write_unlock_bh(&idev->lock);
2331
2332 igmp6_group_dropped(i);
2333 ma_put(i);
2334
2335 write_lock_bh(&idev->lock);
2336 }
2337 write_unlock_bh(&idev->lock);
2338}
2339
2340#ifdef CONFIG_PROC_FS
2341struct igmp6_mc_iter_state {
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002342 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 struct net_device *dev;
2344 struct inet6_dev *idev;
2345};
2346
2347#define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2348
2349static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2350{
2351 struct ifmcaddr6 *im = NULL;
2352 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002353 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Pavel Emelianov7562f872007-05-03 15:13:45 -07002355 state->idev = NULL;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002356 for_each_netdev(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 struct inet6_dev *idev;
2358 idev = in6_dev_get(state->dev);
2359 if (!idev)
2360 continue;
2361 read_lock_bh(&idev->lock);
2362 im = idev->mc_list;
2363 if (im) {
2364 state->idev = idev;
2365 break;
2366 }
2367 read_unlock_bh(&idev->lock);
2368 in6_dev_put(idev);
2369 }
2370 return im;
2371}
2372
2373static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2374{
2375 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2376
2377 im = im->next;
2378 while (!im) {
2379 if (likely(state->idev != NULL)) {
2380 read_unlock_bh(&state->idev->lock);
2381 in6_dev_put(state->idev);
2382 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07002383 state->dev = next_net_device(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 if (!state->dev) {
2385 state->idev = NULL;
2386 break;
2387 }
2388 state->idev = in6_dev_get(state->dev);
2389 if (!state->idev)
2390 continue;
2391 read_lock_bh(&state->idev->lock);
2392 im = state->idev->mc_list;
2393 }
2394 return im;
2395}
2396
2397static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2398{
2399 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2400 if (im)
2401 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2402 --pos;
2403 return pos ? NULL : im;
2404}
2405
2406static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002407 __acquires(dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408{
2409 read_lock(&dev_base_lock);
2410 return igmp6_mc_get_idx(seq, *pos);
2411}
2412
2413static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2414{
2415 struct ifmcaddr6 *im;
2416 im = igmp6_mc_get_next(seq, v);
2417 ++*pos;
2418 return im;
2419}
2420
2421static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002422 __releases(dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423{
2424 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2425 if (likely(state->idev != NULL)) {
2426 read_unlock_bh(&state->idev->lock);
2427 in6_dev_put(state->idev);
2428 state->idev = NULL;
2429 }
2430 state->dev = NULL;
2431 read_unlock(&dev_base_lock);
2432}
2433
2434static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2435{
2436 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2437 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2438
2439 seq_printf(seq,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002440 "%-4d %-15s " NIP6_SEQFMT " %5d %08X %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 state->dev->ifindex, state->dev->name,
2442 NIP6(im->mca_addr),
2443 im->mca_users, im->mca_flags,
2444 (im->mca_flags&MAF_TIMER_RUNNING) ?
2445 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2446 return 0;
2447}
2448
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002449static const struct seq_operations igmp6_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 .start = igmp6_mc_seq_start,
2451 .next = igmp6_mc_seq_next,
2452 .stop = igmp6_mc_seq_stop,
2453 .show = igmp6_mc_seq_show,
2454};
2455
2456static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2457{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002458 return seq_open_net(inode, file, &igmp6_mc_seq_ops,
2459 sizeof(struct igmp6_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460}
2461
Arjan van de Ven9a321442007-02-12 00:55:35 -08002462static const struct file_operations igmp6_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 .owner = THIS_MODULE,
2464 .open = igmp6_mc_seq_open,
2465 .read = seq_read,
2466 .llseek = seq_lseek,
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002467 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468};
2469
2470struct igmp6_mcf_iter_state {
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002471 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 struct net_device *dev;
2473 struct inet6_dev *idev;
2474 struct ifmcaddr6 *im;
2475};
2476
2477#define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2478
2479static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2480{
2481 struct ip6_sf_list *psf = NULL;
2482 struct ifmcaddr6 *im = NULL;
2483 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002484 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Pavel Emelianov7562f872007-05-03 15:13:45 -07002486 state->idev = NULL;
2487 state->im = NULL;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002488 for_each_netdev(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 struct inet6_dev *idev;
2490 idev = in6_dev_get(state->dev);
2491 if (unlikely(idev == NULL))
2492 continue;
2493 read_lock_bh(&idev->lock);
2494 im = idev->mc_list;
2495 if (likely(im != NULL)) {
2496 spin_lock_bh(&im->mca_lock);
2497 psf = im->mca_sources;
2498 if (likely(psf != NULL)) {
2499 state->im = im;
2500 state->idev = idev;
2501 break;
2502 }
2503 spin_unlock_bh(&im->mca_lock);
2504 }
2505 read_unlock_bh(&idev->lock);
2506 in6_dev_put(idev);
2507 }
2508 return psf;
2509}
2510
2511static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2512{
2513 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2514
2515 psf = psf->sf_next;
2516 while (!psf) {
2517 spin_unlock_bh(&state->im->mca_lock);
2518 state->im = state->im->next;
2519 while (!state->im) {
2520 if (likely(state->idev != NULL)) {
2521 read_unlock_bh(&state->idev->lock);
2522 in6_dev_put(state->idev);
2523 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07002524 state->dev = next_net_device(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 if (!state->dev) {
2526 state->idev = NULL;
2527 goto out;
2528 }
2529 state->idev = in6_dev_get(state->dev);
2530 if (!state->idev)
2531 continue;
2532 read_lock_bh(&state->idev->lock);
2533 state->im = state->idev->mc_list;
2534 }
2535 if (!state->im)
2536 break;
2537 spin_lock_bh(&state->im->mca_lock);
2538 psf = state->im->mca_sources;
2539 }
2540out:
2541 return psf;
2542}
2543
2544static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2545{
2546 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2547 if (psf)
2548 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2549 --pos;
2550 return pos ? NULL : psf;
2551}
2552
2553static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002554 __acquires(dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555{
2556 read_lock(&dev_base_lock);
2557 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2558}
2559
2560static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2561{
2562 struct ip6_sf_list *psf;
2563 if (v == SEQ_START_TOKEN)
2564 psf = igmp6_mcf_get_first(seq);
2565 else
2566 psf = igmp6_mcf_get_next(seq, v);
2567 ++*pos;
2568 return psf;
2569}
2570
2571static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002572 __releases(dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573{
2574 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2575 if (likely(state->im != NULL)) {
2576 spin_unlock_bh(&state->im->mca_lock);
2577 state->im = NULL;
2578 }
2579 if (likely(state->idev != NULL)) {
2580 read_unlock_bh(&state->idev->lock);
2581 in6_dev_put(state->idev);
2582 state->idev = NULL;
2583 }
2584 state->dev = NULL;
2585 read_unlock(&dev_base_lock);
2586}
2587
2588static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2589{
2590 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2591 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2592
2593 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002594 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 "%3s %6s "
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002596 "%32s %32s %6s %6s\n", "Idx",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 "Device", "Multicast Address",
2598 "Source Address", "INC", "EXC");
2599 } else {
2600 seq_printf(seq,
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002601 "%3d %6.6s " NIP6_SEQFMT " " NIP6_SEQFMT " %6lu %6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 state->dev->ifindex, state->dev->name,
2603 NIP6(state->im->mca_addr),
2604 NIP6(psf->sf_addr),
2605 psf->sf_count[MCAST_INCLUDE],
2606 psf->sf_count[MCAST_EXCLUDE]);
2607 }
2608 return 0;
2609}
2610
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002611static const struct seq_operations igmp6_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 .start = igmp6_mcf_seq_start,
2613 .next = igmp6_mcf_seq_next,
2614 .stop = igmp6_mcf_seq_stop,
2615 .show = igmp6_mcf_seq_show,
2616};
2617
2618static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2619{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002620 return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
2621 sizeof(struct igmp6_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622}
2623
Arjan van de Ven9a321442007-02-12 00:55:35 -08002624static const struct file_operations igmp6_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 .owner = THIS_MODULE,
2626 .open = igmp6_mcf_seq_open,
2627 .read = seq_read,
2628 .llseek = seq_lseek,
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002629 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630};
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002631
2632static int igmp6_proc_init(struct net *net)
2633{
2634 int err;
2635
2636 err = -ENOMEM;
2637 if (!proc_net_fops_create(net, "igmp6", S_IRUGO, &igmp6_mc_seq_fops))
2638 goto out;
2639 if (!proc_net_fops_create(net, "mcfilter6", S_IRUGO,
2640 &igmp6_mcf_seq_fops))
2641 goto out_proc_net_igmp6;
2642
2643 err = 0;
2644out:
2645 return err;
2646
2647out_proc_net_igmp6:
2648 proc_net_remove(net, "igmp6");
2649 goto out;
2650}
2651
2652static void igmp6_proc_exit(struct net *net)
2653{
2654 proc_net_remove(net, "mcfilter6");
2655 proc_net_remove(net, "igmp6");
2656}
2657#else
2658static int igmp6_proc_init(struct net *net)
2659{
2660 return 0;
2661}
2662static void igmp6_proc_exit(struct net *net)
2663{
2664 ;
2665}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666#endif
2667
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002668static int igmp6_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 int err;
2671
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002672 err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
2673 SOCK_RAW, IPPROTO_ICMPV6, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 if (err < 0) {
2675 printk(KERN_ERR
2676 "Failed to initialize the IGMP6 control socket (err %d).\n",
2677 err);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002678 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 }
2680
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002681 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002683 err = igmp6_proc_init(net);
2684 if (err)
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002685 goto out_sock_create;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002686out:
2687 return err;
2688
2689out_sock_create:
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002690 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002691 goto out;
2692}
2693
2694static void igmp6_net_exit(struct net *net)
2695{
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002696 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002697 igmp6_proc_exit(net);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002698}
2699
2700static struct pernet_operations igmp6_net_ops = {
2701 .init = igmp6_net_init,
2702 .exit = igmp6_net_exit,
2703};
2704
2705int __init igmp6_init(void)
2706{
2707 return register_pernet_subsys(&igmp6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708}
2709
2710void igmp6_cleanup(void)
2711{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002712 unregister_pernet_subsys(&igmp6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713}