blob: a1c231a04ac2dfa10088a0b2df558f62dbf32635 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Multicast support for IPv6
3 * Linux INET6 implementation
4 *
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 *
8 * $Id: mcast.c,v 1.40 2002/02/08 03:57:19 davem Exp $
9 *
10 * Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
11 *
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
52#include <net/sock.h>
53#include <net/snmp.h>
54
55#include <net/ipv6.h>
56#include <net/protocol.h>
57#include <net/if_inet6.h>
58#include <net/ndisc.h>
59#include <net/addrconf.h>
60#include <net/ip6_route.h>
61
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
128static struct socket *igmp6_socket;
129
130int __ipv6_dev_mc_dec(struct inet6_dev *idev, struct in6_addr *addr);
131
132static void igmp6_join_group(struct ifmcaddr6 *ma);
133static void igmp6_leave_group(struct ifmcaddr6 *ma);
134static void igmp6_timer_handler(unsigned long data);
135
136static void mld_gq_timer_expire(unsigned long data);
137static void mld_ifc_timer_expire(unsigned long data);
138static void mld_ifc_event(struct inet6_dev *idev);
139static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
140static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *addr);
141static void mld_clear_delrec(struct inet6_dev *idev);
142static int sf_setstate(struct ifmcaddr6 *pmc);
143static void sf_markstate(struct ifmcaddr6 *pmc);
144static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
145static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
146 int sfmode, int sfcount, struct in6_addr *psfsrc,
147 int delta);
148static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
149 int sfmode, int sfcount, struct in6_addr *psfsrc,
150 int delta);
151static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
152 struct inet6_dev *idev);
153
154
155#define IGMP6_UNSOLICITED_IVAL (10*HZ)
156#define MLD_QRV_DEFAULT 2
157
158#define MLD_V1_SEEN(idev) (ipv6_devconf.force_mld_version == 1 || \
159 (idev)->cnf.force_mld_version == 1 || \
160 ((idev)->mc_v1_seen && \
161 time_before(jiffies, (idev)->mc_v1_seen)))
162
163#define MLDV2_MASK(value, nb) ((nb)>=32 ? (value) : ((1<<(nb))-1) & (value))
164#define MLDV2_EXP(thresh, nbmant, nbexp, value) \
165 ((value) < (thresh) ? (value) : \
Yan Zhengf12baea2005-10-29 00:02:32 +0800166 ((MLDV2_MASK(value, nbmant) | (1<<(nbmant))) << \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 (MLDV2_MASK((value) >> (nbmant), nbexp) + (nbexp))))
168
169#define MLDV2_QQIC(value) MLDV2_EXP(0x80, 4, 3, value)
170#define MLDV2_MRC(value) MLDV2_EXP(0x8000, 12, 3, value)
171
David L Stevens6f4353d2005-12-26 17:03:46 -0800172#define IPV6_MLD_MAX_MSF 64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Brian Haleyab32ea52006-09-22 14:15:41 -0700174int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176/*
177 * socket join on multicast group
178 */
179
180int ipv6_sock_mc_join(struct sock *sk, int ifindex, struct in6_addr *addr)
181{
182 struct net_device *dev = NULL;
183 struct ipv6_mc_socklist *mc_lst;
184 struct ipv6_pinfo *np = inet6_sk(sk);
185 int err;
186
187 if (!ipv6_addr_is_multicast(addr))
188 return -EINVAL;
189
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700190 read_lock_bh(&ipv6_sk_mc_lock);
191 for (mc_lst=np->ipv6_mc_list; mc_lst; mc_lst=mc_lst->next) {
192 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
193 ipv6_addr_equal(&mc_lst->addr, addr)) {
194 read_unlock_bh(&ipv6_sk_mc_lock);
195 return -EADDRINUSE;
196 }
197 }
198 read_unlock_bh(&ipv6_sk_mc_lock);
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
201
202 if (mc_lst == NULL)
203 return -ENOMEM;
204
205 mc_lst->next = NULL;
206 ipv6_addr_copy(&mc_lst->addr, addr);
207
208 if (ifindex == 0) {
209 struct rt6_info *rt;
210 rt = rt6_lookup(addr, NULL, 0, 0);
211 if (rt) {
212 dev = rt->rt6i_dev;
213 dev_hold(dev);
214 dst_release(&rt->u.dst);
215 }
216 } else
217 dev = dev_get_by_index(ifindex);
218
219 if (dev == NULL) {
220 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
221 return -ENODEV;
222 }
223
224 mc_lst->ifindex = dev->ifindex;
225 mc_lst->sfmode = MCAST_EXCLUDE;
YOSHIFUJI Hideaki196433c2006-01-04 13:56:31 -0800226 rwlock_init(&mc_lst->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 mc_lst->sflist = NULL;
228
229 /*
230 * now add/increase the group membership on the device
231 */
232
233 err = ipv6_dev_mc_inc(dev, addr);
234
235 if (err) {
236 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
237 dev_put(dev);
238 return err;
239 }
240
241 write_lock_bh(&ipv6_sk_mc_lock);
242 mc_lst->next = np->ipv6_mc_list;
243 np->ipv6_mc_list = mc_lst;
244 write_unlock_bh(&ipv6_sk_mc_lock);
245
246 dev_put(dev);
247
248 return 0;
249}
250
251/*
252 * socket leave on multicast group
253 */
254int ipv6_sock_mc_drop(struct sock *sk, int ifindex, struct in6_addr *addr)
255{
256 struct ipv6_pinfo *np = inet6_sk(sk);
257 struct ipv6_mc_socklist *mc_lst, **lnk;
258
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
268 if ((dev = dev_get_by_index(mc_lst->ifindex)) != NULL) {
269 struct inet6_dev *idev = in6_dev_get(dev);
270
David L Stevensacd6e002006-08-17 16:27:39 -0700271 (void) ip6_mc_leave_src(sk, mc_lst, idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
274 in6_dev_put(idev);
275 }
276 dev_put(dev);
David L Stevensacd6e002006-08-17 16:27:39 -0700277 } else
278 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
280 return 0;
281 }
282 }
283 write_unlock_bh(&ipv6_sk_mc_lock);
284
David L Stevens9951f032005-07-08 17:47:28 -0700285 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288static struct inet6_dev *ip6_mc_find_dev(struct in6_addr *group, int ifindex)
289{
290 struct net_device *dev = NULL;
291 struct inet6_dev *idev = NULL;
292
293 if (ifindex == 0) {
294 struct rt6_info *rt;
295
296 rt = rt6_lookup(group, NULL, 0, 0);
297 if (rt) {
298 dev = rt->rt6i_dev;
299 dev_hold(dev);
300 dst_release(&rt->u.dst);
301 }
302 } else
303 dev = dev_get_by_index(ifindex);
304
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;
326
327 write_lock_bh(&ipv6_sk_mc_lock);
328 while ((mc_lst = np->ipv6_mc_list) != NULL) {
329 struct net_device *dev;
330
331 np->ipv6_mc_list = mc_lst->next;
332 write_unlock_bh(&ipv6_sk_mc_lock);
333
334 dev = dev_get_by_index(mc_lst->ifindex);
335 if (dev) {
336 struct inet6_dev *idev = in6_dev_get(dev);
337
David L Stevensacd6e002006-08-17 16:27:39 -0700338 (void) ip6_mc_leave_src(sk, mc_lst, idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
341 in6_dev_put(idev);
342 }
343 dev_put(dev);
David L Stevensacd6e002006-08-17 16:27:39 -0700344 } else
345 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
348
349 write_lock_bh(&ipv6_sk_mc_lock);
350 }
351 write_unlock_bh(&ipv6_sk_mc_lock);
352}
353
354int ip6_mc_source(int add, int omode, struct sock *sk,
355 struct group_source_req *pgsr)
356{
357 struct in6_addr *source, *group;
358 struct ipv6_mc_socklist *pmc;
359 struct net_device *dev;
360 struct inet6_dev *idev;
361 struct ipv6_pinfo *inet6 = inet6_sk(sk);
362 struct ip6_sf_socklist *psl;
363 int i, j, rv;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700364 int leavegroup = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800365 int pmclocked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 int err;
367
368 if (pgsr->gsr_group.ss_family != AF_INET6 ||
369 pgsr->gsr_source.ss_family != AF_INET6)
370 return -EINVAL;
371
372 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
373 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
374
375 if (!ipv6_addr_is_multicast(group))
376 return -EINVAL;
377
378 idev = ip6_mc_find_dev(group, pgsr->gsr_interface);
379 if (!idev)
380 return -ENODEV;
381 dev = idev->dev;
382
383 err = -EADDRNOTAVAIL;
384
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700385 read_lock_bh(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
387 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
388 continue;
389 if (ipv6_addr_equal(&pmc->addr, group))
390 break;
391 }
David L Stevens917f2f12005-07-08 17:45:16 -0700392 if (!pmc) { /* must have a prior join */
393 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 /* if a source filter was set, must be the same mode as before */
397 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -0700398 if (pmc->sfmode != omode) {
399 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 } else if (pmc->sfmode != omode) {
403 /* allow mode switches for empty-set filters */
404 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
405 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
406 pmc->sfmode = omode;
407 }
408
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800409 write_lock_bh(&pmc->sflock);
410 pmclocked = 1;
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 psl = pmc->sflist;
413 if (!add) {
414 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -0700415 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 rv = !0;
417 for (i=0; i<psl->sl_count; i++) {
418 rv = memcmp(&psl->sl_addr[i], source,
419 sizeof(struct in6_addr));
420 if (rv == 0)
421 break;
422 }
423 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -0700424 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700426 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
427 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
428 leavegroup = 1;
429 goto done;
430 }
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 /* update the interface filter */
433 ip6_mc_del_src(idev, group, omode, 1, source, 1);
434
435 for (j=i+1; j<psl->sl_count; j++)
436 psl->sl_addr[j-1] = psl->sl_addr[j];
437 psl->sl_count--;
438 err = 0;
439 goto done;
440 }
441 /* else, add a new source to the filter */
442
443 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
444 err = -ENOBUFS;
445 goto done;
446 }
447 if (!psl || psl->sl_count == psl->sl_max) {
448 struct ip6_sf_socklist *newpsl;
449 int count = IP6_SFBLOCK;
450
451 if (psl)
452 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800453 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (!newpsl) {
455 err = -ENOBUFS;
456 goto done;
457 }
458 newpsl->sl_max = count;
459 newpsl->sl_count = count - IP6_SFBLOCK;
460 if (psl) {
461 for (i=0; i<psl->sl_count; i++)
462 newpsl->sl_addr[i] = psl->sl_addr[i];
463 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
464 }
465 pmc->sflist = psl = newpsl;
466 }
467 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
468 for (i=0; i<psl->sl_count; i++) {
469 rv = memcmp(&psl->sl_addr[i], source, sizeof(struct in6_addr));
470 if (rv == 0)
471 break;
472 }
473 if (rv == 0) /* address already there is an error */
474 goto done;
475 for (j=psl->sl_count-1; j>=i; j--)
476 psl->sl_addr[j+1] = psl->sl_addr[j];
477 psl->sl_addr[i] = *source;
478 psl->sl_count++;
479 err = 0;
480 /* update the interface list */
481 ip6_mc_add_src(idev, group, omode, 1, source, 1);
482done:
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800483 if (pmclocked)
484 write_unlock_bh(&pmc->sflock);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700485 read_unlock_bh(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 read_unlock_bh(&idev->lock);
487 in6_dev_put(idev);
488 dev_put(dev);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700489 if (leavegroup)
490 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return err;
492}
493
494int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
495{
496 struct in6_addr *group;
497 struct ipv6_mc_socklist *pmc;
498 struct net_device *dev;
499 struct inet6_dev *idev;
500 struct ipv6_pinfo *inet6 = inet6_sk(sk);
501 struct ip6_sf_socklist *newpsl, *psl;
David L Stevens9951f032005-07-08 17:47:28 -0700502 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 int i, err;
504
505 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
506
507 if (!ipv6_addr_is_multicast(group))
508 return -EINVAL;
509 if (gsf->gf_fmode != MCAST_INCLUDE &&
510 gsf->gf_fmode != MCAST_EXCLUDE)
511 return -EINVAL;
512
513 idev = ip6_mc_find_dev(group, gsf->gf_interface);
514
515 if (!idev)
516 return -ENODEV;
517 dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
David S. Miller9c059892005-07-08 21:44:39 -0700519 err = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800520 read_lock_bh(&ipv6_sk_mc_lock);
521
David L Stevens9951f032005-07-08 17:47:28 -0700522 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
523 leavegroup = 1;
524 goto done;
525 }
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
528 if (pmc->ifindex != gsf->gf_interface)
529 continue;
530 if (ipv6_addr_equal(&pmc->addr, group))
531 break;
532 }
David L Stevens917f2f12005-07-08 17:45:16 -0700533 if (!pmc) { /* must have a prior join */
534 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (gsf->gf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800538 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
539 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (!newpsl) {
541 err = -ENOBUFS;
542 goto done;
543 }
544 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
545 for (i=0; i<newpsl->sl_count; ++i) {
546 struct sockaddr_in6 *psin6;
547
548 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
549 newpsl->sl_addr[i] = psin6->sin6_addr;
550 }
551 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
552 newpsl->sl_count, newpsl->sl_addr, 0);
553 if (err) {
554 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
555 goto done;
556 }
Yan Zheng8713dbf2005-10-28 08:02:08 +0800557 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +0800559 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
560 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800561
562 write_lock_bh(&pmc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 psl = pmc->sflist;
564 if (psl) {
565 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
566 psl->sl_count, psl->sl_addr, 0);
567 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
568 } else
569 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
570 pmc->sflist = newpsl;
571 pmc->sfmode = gsf->gf_fmode;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800572 write_unlock_bh(&pmc->sflock);
David L Stevens917f2f12005-07-08 17:45:16 -0700573 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574done:
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800575 read_unlock_bh(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 read_unlock_bh(&idev->lock);
577 in6_dev_put(idev);
578 dev_put(dev);
David L Stevens9951f032005-07-08 17:47:28 -0700579 if (leavegroup)
580 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return err;
582}
583
584int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
585 struct group_filter __user *optval, int __user *optlen)
586{
587 int err, i, count, copycount;
588 struct in6_addr *group;
589 struct ipv6_mc_socklist *pmc;
590 struct inet6_dev *idev;
591 struct net_device *dev;
592 struct ipv6_pinfo *inet6 = inet6_sk(sk);
593 struct ip6_sf_socklist *psl;
594
595 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
596
597 if (!ipv6_addr_is_multicast(group))
598 return -EINVAL;
599
600 idev = ip6_mc_find_dev(group, gsf->gf_interface);
601
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];
647 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
648 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
658int inet6_mc_check(struct sock *sk, struct in6_addr *mc_addr,
659 struct in6_addr *src_addr)
660{
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 */
865int ipv6_dev_mc_inc(struct net_device *dev, struct in6_addr *addr)
866{
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 init_timer(&mc->mca_timer);
906 mc->mca_timer.function = igmp6_timer_handler;
907 mc->mca_timer.data = (unsigned long) mc;
908
909 ipv6_addr_copy(&mc->mca_addr, addr);
910 mc->idev = idev;
911 mc->mca_users = 1;
912 /* mca_stamp should be updated upon changes */
913 mc->mca_cstamp = mc->mca_tstamp = jiffies;
914 atomic_set(&mc->mca_refcnt, 2);
915 spin_lock_init(&mc->mca_lock);
916
917 /* initial mode is (EX, empty) */
918 mc->mca_sfmode = MCAST_EXCLUDE;
919 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
920
921 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
922 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
923 mc->mca_flags |= MAF_NOREPORT;
924
925 mc->next = idev->mc_list;
926 idev->mc_list = mc;
927 write_unlock_bh(&idev->lock);
928
929 mld_del_delrec(idev, &mc->mca_addr);
930 igmp6_group_added(mc);
931 ma_put(mc);
932 return 0;
933}
934
935/*
936 * device multicast group del
937 */
938int __ipv6_dev_mc_dec(struct inet6_dev *idev, struct in6_addr *addr)
939{
940 struct ifmcaddr6 *ma, **map;
941
942 write_lock_bh(&idev->lock);
943 for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
944 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
945 if (--ma->mca_users == 0) {
946 *map = ma->next;
947 write_unlock_bh(&idev->lock);
948
949 igmp6_group_dropped(ma);
950
951 ma_put(ma);
952 return 0;
953 }
954 write_unlock_bh(&idev->lock);
955 return 0;
956 }
957 }
958 write_unlock_bh(&idev->lock);
959
960 return -ENOENT;
961}
962
963int ipv6_dev_mc_dec(struct net_device *dev, struct in6_addr *addr)
964{
965 struct inet6_dev *idev = in6_dev_get(dev);
966 int err;
967
968 if (!idev)
969 return -ENODEV;
970
971 err = __ipv6_dev_mc_dec(idev, addr);
972
973 in6_dev_put(idev);
974
975 return err;
976}
977
978/*
979 * identify MLD packets for MLD filter exceptions
980 */
981int ipv6_is_mld(struct sk_buff *skb, int nexthdr)
982{
983 struct icmp6hdr *pic;
984
985 if (nexthdr != IPPROTO_ICMPV6)
986 return 0;
987
988 if (!pskb_may_pull(skb, sizeof(struct icmp6hdr)))
989 return 0;
990
991 pic = (struct icmp6hdr *)skb->h.raw;
992
993 switch (pic->icmp6_type) {
994 case ICMPV6_MGM_QUERY:
995 case ICMPV6_MGM_REPORT:
996 case ICMPV6_MGM_REDUCTION:
997 case ICMPV6_MLD2_REPORT:
998 return 1;
999 default:
1000 break;
1001 }
1002 return 0;
1003}
1004
1005/*
1006 * check if the interface/address pair is valid
1007 */
1008int ipv6_chk_mcast_addr(struct net_device *dev, struct in6_addr *group,
1009 struct in6_addr *src_addr)
1010{
1011 struct inet6_dev *idev;
1012 struct ifmcaddr6 *mc;
1013 int rv = 0;
1014
1015 idev = in6_dev_get(dev);
1016 if (idev) {
1017 read_lock_bh(&idev->lock);
1018 for (mc = idev->mc_list; mc; mc=mc->next) {
1019 if (ipv6_addr_equal(&mc->mca_addr, group))
1020 break;
1021 }
1022 if (mc) {
1023 if (src_addr && !ipv6_addr_any(src_addr)) {
1024 struct ip6_sf_list *psf;
1025
1026 spin_lock_bh(&mc->mca_lock);
1027 for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
1028 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
1029 break;
1030 }
1031 if (psf)
1032 rv = psf->sf_count[MCAST_INCLUDE] ||
1033 psf->sf_count[MCAST_EXCLUDE] !=
1034 mc->mca_sfcount[MCAST_EXCLUDE];
1035 else
1036 rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
1037 spin_unlock_bh(&mc->mca_lock);
1038 } else
1039 rv = 1; /* don't filter unspecified source */
1040 }
1041 read_unlock_bh(&idev->lock);
1042 in6_dev_put(idev);
1043 }
1044 return rv;
1045}
1046
1047static void mld_gq_start_timer(struct inet6_dev *idev)
1048{
1049 int tv = net_random() % idev->mc_maxdelay;
1050
1051 idev->mc_gq_running = 1;
1052 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
1053 in6_dev_hold(idev);
1054}
1055
1056static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
1057{
1058 int tv = net_random() % delay;
1059
1060 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1061 in6_dev_hold(idev);
1062}
1063
1064/*
1065 * IGMP handling (alias multicast ICMPv6 messages)
1066 */
1067
1068static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1069{
1070 unsigned long delay = resptime;
1071
1072 /* Do not start timer for these addresses */
1073 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1074 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1075 return;
1076
1077 if (del_timer(&ma->mca_timer)) {
1078 atomic_dec(&ma->mca_refcnt);
1079 delay = ma->mca_timer.expires - jiffies;
1080 }
1081
1082 if (delay >= resptime) {
1083 if (resptime)
1084 delay = net_random() % resptime;
1085 else
1086 delay = 1;
1087 }
1088 ma->mca_timer.expires = jiffies + delay;
1089 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1090 atomic_inc(&ma->mca_refcnt);
1091 ma->mca_flags |= MAF_TIMER_RUNNING;
1092}
1093
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001094/* mark EXCLUDE-mode sources */
1095static int mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 struct in6_addr *srcs)
1097{
1098 struct ip6_sf_list *psf;
1099 int i, scount;
1100
1101 scount = 0;
1102 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1103 if (scount == nsrcs)
1104 break;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001105 for (i=0; i<nsrcs; i++) {
1106 /* skip inactive filters */
1107 if (pmc->mca_sfcount[MCAST_INCLUDE] ||
1108 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1109 psf->sf_count[MCAST_EXCLUDE])
1110 continue;
1111 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1112 scount++;
1113 break;
1114 }
1115 }
1116 }
1117 pmc->mca_flags &= ~MAF_GSQUERY;
1118 if (scount == nsrcs) /* all sources excluded */
1119 return 0;
1120 return 1;
1121}
1122
1123static int mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1124 struct in6_addr *srcs)
1125{
1126 struct ip6_sf_list *psf;
1127 int i, scount;
1128
1129 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1130 return mld_xmarksources(pmc, nsrcs, srcs);
1131
1132 /* mark INCLUDE-mode sources */
1133
1134 scount = 0;
1135 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1136 if (scount == nsrcs)
1137 break;
1138 for (i=0; i<nsrcs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1140 psf->sf_gsresp = 1;
1141 scount++;
1142 break;
1143 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001146 if (!scount) {
1147 pmc->mca_flags &= ~MAF_GSQUERY;
1148 return 0;
1149 }
1150 pmc->mca_flags |= MAF_GSQUERY;
1151 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
1154int igmp6_event_query(struct sk_buff *skb)
1155{
Yan Zheng97300b52005-10-31 20:09:45 +08001156 struct mld2_query *mlh2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 struct ifmcaddr6 *ma;
1158 struct in6_addr *group;
1159 unsigned long max_delay;
1160 struct inet6_dev *idev;
1161 struct icmp6hdr *hdr;
1162 int group_type;
1163 int mark = 0;
1164 int len;
1165
1166 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1167 return -EINVAL;
1168
1169 /* compute payload length excluding extension headers */
1170 len = ntohs(skb->nh.ipv6h->payload_len) + sizeof(struct ipv6hdr);
1171 len -= (char *)skb->h.raw - (char *)skb->nh.ipv6h;
1172
1173 /* Drop queries with not link local source */
1174 if (!(ipv6_addr_type(&skb->nh.ipv6h->saddr)&IPV6_ADDR_LINKLOCAL))
1175 return -EINVAL;
1176
1177 idev = in6_dev_get(skb->dev);
1178
1179 if (idev == NULL)
1180 return 0;
1181
1182 hdr = (struct icmp6hdr *) skb->h.raw;
1183 group = (struct in6_addr *) (hdr + 1);
1184 group_type = ipv6_addr_type(group);
1185
1186 if (group_type != IPV6_ADDR_ANY &&
1187 !(group_type&IPV6_ADDR_MULTICAST)) {
1188 in6_dev_put(idev);
1189 return -EINVAL;
1190 }
1191
1192 if (len == 24) {
1193 int switchback;
1194 /* MLDv1 router present */
1195
1196 /* Translate milliseconds to jiffies */
1197 max_delay = (ntohs(hdr->icmp6_maxdelay)*HZ)/1000;
1198
1199 switchback = (idev->mc_qrv + 1) * max_delay;
1200 idev->mc_v1_seen = jiffies + switchback;
1201
1202 /* cancel the interface change timer */
1203 idev->mc_ifc_count = 0;
1204 if (del_timer(&idev->mc_ifc_timer))
1205 __in6_dev_put(idev);
1206 /* clear deleted report items */
1207 mld_clear_delrec(idev);
1208 } else if (len >= 28) {
Yan Zheng97300b52005-10-31 20:09:45 +08001209 int srcs_offset = sizeof(struct mld2_query) -
1210 sizeof(struct icmp6hdr);
1211 if (!pskb_may_pull(skb, srcs_offset)) {
1212 in6_dev_put(idev);
1213 return -EINVAL;
1214 }
1215 mlh2 = (struct mld2_query *) skb->h.raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 max_delay = (MLDV2_MRC(ntohs(mlh2->mrc))*HZ)/1000;
1217 if (!max_delay)
1218 max_delay = 1;
1219 idev->mc_maxdelay = max_delay;
1220 if (mlh2->qrv)
1221 idev->mc_qrv = mlh2->qrv;
1222 if (group_type == IPV6_ADDR_ANY) { /* general query */
1223 if (mlh2->nsrcs) {
1224 in6_dev_put(idev);
1225 return -EINVAL; /* no sources allowed */
1226 }
1227 mld_gq_start_timer(idev);
1228 in6_dev_put(idev);
1229 return 0;
1230 }
1231 /* mark sources to include, if group & source-specific */
Yan Zheng97300b52005-10-31 20:09:45 +08001232 if (mlh2->nsrcs != 0) {
1233 if (!pskb_may_pull(skb, srcs_offset +
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001234 ntohs(mlh2->nsrcs) * sizeof(struct in6_addr))) {
Yan Zheng97300b52005-10-31 20:09:45 +08001235 in6_dev_put(idev);
1236 return -EINVAL;
1237 }
1238 mlh2 = (struct mld2_query *) skb->h.raw;
1239 mark = 1;
1240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 } else {
1242 in6_dev_put(idev);
1243 return -EINVAL;
1244 }
1245
1246 read_lock_bh(&idev->lock);
1247 if (group_type == IPV6_ADDR_ANY) {
1248 for (ma = idev->mc_list; ma; ma=ma->next) {
1249 spin_lock_bh(&ma->mca_lock);
1250 igmp6_group_queried(ma, max_delay);
1251 spin_unlock_bh(&ma->mca_lock);
1252 }
1253 } else {
1254 for (ma = idev->mc_list; ma; ma=ma->next) {
David L Stevens7add2a42006-01-24 13:06:39 -08001255 if (!ipv6_addr_equal(group, &ma->mca_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 continue;
1257 spin_lock_bh(&ma->mca_lock);
1258 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1259 /* gsquery <- gsquery && mark */
1260 if (!mark)
1261 ma->mca_flags &= ~MAF_GSQUERY;
1262 } else {
1263 /* gsquery <- mark */
1264 if (mark)
1265 ma->mca_flags |= MAF_GSQUERY;
1266 else
1267 ma->mca_flags &= ~MAF_GSQUERY;
1268 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001269 if (!(ma->mca_flags & MAF_GSQUERY) ||
David L Stevens7add2a42006-01-24 13:06:39 -08001270 mld_marksources(ma, ntohs(mlh2->nsrcs), mlh2->srcs))
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001271 igmp6_group_queried(ma, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 spin_unlock_bh(&ma->mca_lock);
David L Stevens7add2a42006-01-24 13:06:39 -08001273 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275 }
1276 read_unlock_bh(&idev->lock);
1277 in6_dev_put(idev);
1278
1279 return 0;
1280}
1281
1282
1283int igmp6_event_report(struct sk_buff *skb)
1284{
1285 struct ifmcaddr6 *ma;
1286 struct in6_addr *addrp;
1287 struct inet6_dev *idev;
1288 struct icmp6hdr *hdr;
1289 int addr_type;
1290
1291 /* Our own report looped back. Ignore it. */
1292 if (skb->pkt_type == PACKET_LOOPBACK)
1293 return 0;
1294
David Stevens24c69272005-12-02 20:32:59 -08001295 /* send our report if the MC router may not have heard this report */
1296 if (skb->pkt_type != PACKET_MULTICAST &&
1297 skb->pkt_type != PACKET_BROADCAST)
1298 return 0;
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1301 return -EINVAL;
1302
1303 hdr = (struct icmp6hdr*) skb->h.raw;
1304
1305 /* Drop reports with not link local source */
1306 addr_type = ipv6_addr_type(&skb->nh.ipv6h->saddr);
1307 if (addr_type != IPV6_ADDR_ANY &&
1308 !(addr_type&IPV6_ADDR_LINKLOCAL))
1309 return -EINVAL;
1310
1311 addrp = (struct in6_addr *) (hdr + 1);
1312
1313 idev = in6_dev_get(skb->dev);
1314 if (idev == NULL)
1315 return -ENODEV;
1316
1317 /*
1318 * Cancel the timer for this group
1319 */
1320
1321 read_lock_bh(&idev->lock);
1322 for (ma = idev->mc_list; ma; ma=ma->next) {
1323 if (ipv6_addr_equal(&ma->mca_addr, addrp)) {
1324 spin_lock(&ma->mca_lock);
1325 if (del_timer(&ma->mca_timer))
1326 atomic_dec(&ma->mca_refcnt);
1327 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1328 spin_unlock(&ma->mca_lock);
1329 break;
1330 }
1331 }
1332 read_unlock_bh(&idev->lock);
1333 in6_dev_put(idev);
1334 return 0;
1335}
1336
1337static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1338 int gdeleted, int sdeleted)
1339{
1340 switch (type) {
1341 case MLD2_MODE_IS_INCLUDE:
1342 case MLD2_MODE_IS_EXCLUDE:
1343 if (gdeleted || sdeleted)
1344 return 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001345 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1346 if (pmc->mca_sfmode == MCAST_INCLUDE)
1347 return 1;
1348 /* don't include if this source is excluded
1349 * in all filters
1350 */
1351 if (psf->sf_count[MCAST_INCLUDE])
David L Stevens7add2a42006-01-24 13:06:39 -08001352 return type == MLD2_MODE_IS_INCLUDE;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001353 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1354 psf->sf_count[MCAST_EXCLUDE];
1355 }
1356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 case MLD2_CHANGE_TO_INCLUDE:
1358 if (gdeleted || sdeleted)
1359 return 0;
1360 return psf->sf_count[MCAST_INCLUDE] != 0;
1361 case MLD2_CHANGE_TO_EXCLUDE:
1362 if (gdeleted || sdeleted)
1363 return 0;
1364 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1365 psf->sf_count[MCAST_INCLUDE])
1366 return 0;
1367 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1368 psf->sf_count[MCAST_EXCLUDE];
1369 case MLD2_ALLOW_NEW_SOURCES:
1370 if (gdeleted || !psf->sf_crcount)
1371 return 0;
1372 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1373 case MLD2_BLOCK_OLD_SOURCES:
1374 if (pmc->mca_sfmode == MCAST_INCLUDE)
1375 return gdeleted || (psf->sf_crcount && sdeleted);
1376 return psf->sf_crcount && !gdeleted && !sdeleted;
1377 }
1378 return 0;
1379}
1380
1381static int
1382mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1383{
1384 struct ip6_sf_list *psf;
1385 int scount = 0;
1386
1387 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1388 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1389 continue;
1390 scount++;
1391 }
1392 return scount;
1393}
1394
1395static struct sk_buff *mld_newpack(struct net_device *dev, int size)
1396{
1397 struct sock *sk = igmp6_socket->sk;
1398 struct sk_buff *skb;
1399 struct mld2_report *pmr;
1400 struct in6_addr addr_buf;
1401 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 */
1407 skb = sock_alloc_send_skb(sk, size + LL_RESERVED_SPACE(dev), 1, &err);
1408
1409 if (skb == 0)
1410 return NULL;
1411
1412 skb_reserve(skb, LL_RESERVED_SPACE(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 if (ipv6_get_lladdr(dev, &addr_buf)) {
1415 /* <draft-ietf-magma-mld-source-05.txt>:
1416 * use unspecified address as the source address
1417 * when a valid link-local address is not available.
1418 */
1419 memset(&addr_buf, 0, sizeof(addr_buf));
1420 }
1421
1422 ip6_nd_hdr(sk, skb, dev, &addr_buf, &mld2_all_mcr, NEXTHDR_HOP, 0);
1423
1424 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1425
1426 pmr =(struct mld2_report *)skb_put(skb, sizeof(*pmr));
1427 skb->h.raw = (unsigned char *)pmr;
1428 pmr->type = ICMPV6_MLD2_REPORT;
1429 pmr->resv1 = 0;
1430 pmr->csum = 0;
1431 pmr->resv2 = 0;
1432 pmr->ngrec = 0;
1433 return skb;
1434}
1435
Patrick McHardye9823182005-06-21 14:02:15 -07001436static inline int mld_dev_queue_xmit2(struct sk_buff *skb)
1437{
1438 struct net_device *dev = skb->dev;
1439
1440 if (dev->hard_header) {
1441 unsigned char ha[MAX_ADDR_LEN];
1442 int err;
1443
1444 ndisc_mc_map(&skb->nh.ipv6h->daddr, ha, dev, 1);
1445 err = dev->hard_header(skb, dev, ETH_P_IPV6, ha, NULL, skb->len);
1446 if (err < 0) {
1447 kfree_skb(skb);
1448 return err;
1449 }
1450 }
1451 return dev_queue_xmit(skb);
1452}
1453
1454static inline int mld_dev_queue_xmit(struct sk_buff *skb)
1455{
1456 return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL, skb->dev,
1457 mld_dev_queue_xmit2);
1458}
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460static void mld_sendpack(struct sk_buff *skb)
1461{
1462 struct ipv6hdr *pip6 = skb->nh.ipv6h;
1463 struct mld2_report *pmr = (struct mld2_report *)skb->h.raw;
1464 int payload_len, mldlen;
1465 struct inet6_dev *idev = in6_dev_get(skb->dev);
1466 int err;
1467
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001468 IP6_INC_STATS(idev, IPSTATS_MIB_OUTREQUESTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 payload_len = skb->tail - (unsigned char *)skb->nh.ipv6h -
1470 sizeof(struct ipv6hdr);
1471 mldlen = skb->tail - skb->h.raw;
1472 pip6->payload_len = htons(payload_len);
1473
1474 pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1475 IPPROTO_ICMPV6, csum_partial(skb->h.raw, mldlen, 0));
1476 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
Patrick McHardye9823182005-06-21 14:02:15 -07001477 mld_dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 if (!err) {
1479 ICMP6_INC_STATS(idev,ICMP6_MIB_OUTMSGS);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001480 IP6_INC_STATS(idev, IPSTATS_MIB_OUTMCASTPKTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 } else
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001482 IP6_INC_STATS(idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 if (likely(idev != NULL))
1485 in6_dev_put(idev);
1486}
1487
1488static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1489{
Yan Zhengfab10fe2005-10-05 12:08:13 -07001490 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
1492
1493static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1494 int type, struct mld2_grec **ppgr)
1495{
1496 struct net_device *dev = pmc->idev->dev;
1497 struct mld2_report *pmr;
1498 struct mld2_grec *pgr;
1499
1500 if (!skb)
1501 skb = mld_newpack(dev, dev->mtu);
1502 if (!skb)
1503 return NULL;
1504 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1505 pgr->grec_type = type;
1506 pgr->grec_auxwords = 0;
1507 pgr->grec_nsrcs = 0;
1508 pgr->grec_mca = pmc->mca_addr; /* structure copy */
1509 pmr = (struct mld2_report *)skb->h.raw;
1510 pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
1511 *ppgr = pgr;
1512 return skb;
1513}
1514
1515#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1516 skb_tailroom(skb)) : 0)
1517
1518static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1519 int type, int gdeleted, int sdeleted)
1520{
1521 struct net_device *dev = pmc->idev->dev;
1522 struct mld2_report *pmr;
1523 struct mld2_grec *pgr = NULL;
1524 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001525 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 if (pmc->mca_flags & MAF_NOREPORT)
1528 return skb;
1529
1530 isquery = type == MLD2_MODE_IS_INCLUDE ||
1531 type == MLD2_MODE_IS_EXCLUDE;
1532 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1533 type == MLD2_CHANGE_TO_EXCLUDE;
1534
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001535 stotal = scount = 0;
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1538
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001539 if (!*psf_list)
1540 goto empty_source;
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 pmr = skb ? (struct mld2_report *)skb->h.raw : NULL;
1543
1544 /* EX and TO_EX get a fresh packet, if needed */
1545 if (truncate) {
1546 if (pmr && pmr->ngrec &&
1547 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1548 if (skb)
1549 mld_sendpack(skb);
1550 skb = mld_newpack(dev, dev->mtu);
1551 }
1552 }
1553 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 psf_prev = NULL;
1555 for (psf=*psf_list; psf; psf=psf_next) {
1556 struct in6_addr *psrc;
1557
1558 psf_next = psf->sf_next;
1559
1560 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1561 psf_prev = psf;
1562 continue;
1563 }
1564
1565 /* clear marks on query responses */
1566 if (isquery)
1567 psf->sf_gsresp = 0;
1568
1569 if (AVAILABLE(skb) < sizeof(*psrc) +
1570 first*sizeof(struct mld2_grec)) {
1571 if (truncate && !first)
1572 break; /* truncate these */
1573 if (pgr)
1574 pgr->grec_nsrcs = htons(scount);
1575 if (skb)
1576 mld_sendpack(skb);
1577 skb = mld_newpack(dev, dev->mtu);
1578 first = 1;
1579 scount = 0;
1580 }
1581 if (first) {
1582 skb = add_grhead(skb, pmc, type, &pgr);
1583 first = 0;
1584 }
1585 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1586 *psrc = psf->sf_addr;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001587 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1589 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1590 psf->sf_crcount--;
1591 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1592 if (psf_prev)
1593 psf_prev->sf_next = psf->sf_next;
1594 else
1595 *psf_list = psf->sf_next;
1596 kfree(psf);
1597 continue;
1598 }
1599 }
1600 psf_prev = psf;
1601 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001602
1603empty_source:
1604 if (!stotal) {
1605 if (type == MLD2_ALLOW_NEW_SOURCES ||
1606 type == MLD2_BLOCK_OLD_SOURCES)
1607 return skb;
1608 if (pmc->mca_crcount || isquery) {
1609 /* make sure we have room for group header */
1610 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1611 mld_sendpack(skb);
1612 skb = NULL; /* add_grhead will get a new one */
1613 }
1614 skb = add_grhead(skb, pmc, type, &pgr);
1615 }
1616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 if (pgr)
1618 pgr->grec_nsrcs = htons(scount);
1619
1620 if (isquery)
1621 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1622 return skb;
1623}
1624
1625static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1626{
1627 struct sk_buff *skb = NULL;
1628 int type;
1629
1630 if (!pmc) {
1631 read_lock_bh(&idev->lock);
1632 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1633 if (pmc->mca_flags & MAF_NOREPORT)
1634 continue;
1635 spin_lock_bh(&pmc->mca_lock);
1636 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1637 type = MLD2_MODE_IS_EXCLUDE;
1638 else
1639 type = MLD2_MODE_IS_INCLUDE;
1640 skb = add_grec(skb, pmc, type, 0, 0);
1641 spin_unlock_bh(&pmc->mca_lock);
1642 }
1643 read_unlock_bh(&idev->lock);
1644 } else {
1645 spin_lock_bh(&pmc->mca_lock);
1646 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1647 type = MLD2_MODE_IS_EXCLUDE;
1648 else
1649 type = MLD2_MODE_IS_INCLUDE;
1650 skb = add_grec(skb, pmc, type, 0, 0);
1651 spin_unlock_bh(&pmc->mca_lock);
1652 }
1653 if (skb)
1654 mld_sendpack(skb);
1655}
1656
1657/*
1658 * remove zero-count source records from a source filter list
1659 */
1660static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1661{
1662 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1663
1664 psf_prev = NULL;
1665 for (psf=*ppsf; psf; psf = psf_next) {
1666 psf_next = psf->sf_next;
1667 if (psf->sf_crcount == 0) {
1668 if (psf_prev)
1669 psf_prev->sf_next = psf->sf_next;
1670 else
1671 *ppsf = psf->sf_next;
1672 kfree(psf);
1673 } else
1674 psf_prev = psf;
1675 }
1676}
1677
1678static void mld_send_cr(struct inet6_dev *idev)
1679{
1680 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1681 struct sk_buff *skb = NULL;
1682 int type, dtype;
1683
1684 read_lock_bh(&idev->lock);
1685 write_lock_bh(&idev->mc_lock);
1686
1687 /* deleted MCA's */
1688 pmc_prev = NULL;
1689 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1690 pmc_next = pmc->next;
1691 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1692 type = MLD2_BLOCK_OLD_SOURCES;
1693 dtype = MLD2_BLOCK_OLD_SOURCES;
1694 skb = add_grec(skb, pmc, type, 1, 0);
1695 skb = add_grec(skb, pmc, dtype, 1, 1);
1696 }
1697 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1699 type = MLD2_CHANGE_TO_INCLUDE;
1700 skb = add_grec(skb, pmc, type, 1, 0);
1701 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001702 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 if (pmc->mca_crcount == 0) {
1704 mld_clear_zeros(&pmc->mca_tomb);
1705 mld_clear_zeros(&pmc->mca_sources);
1706 }
1707 }
1708 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1709 !pmc->mca_sources) {
1710 if (pmc_prev)
1711 pmc_prev->next = pmc_next;
1712 else
1713 idev->mc_tomb = pmc_next;
1714 in6_dev_put(pmc->idev);
1715 kfree(pmc);
1716 } else
1717 pmc_prev = pmc;
1718 }
1719 write_unlock_bh(&idev->mc_lock);
1720
1721 /* change recs */
1722 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1723 spin_lock_bh(&pmc->mca_lock);
1724 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1725 type = MLD2_BLOCK_OLD_SOURCES;
1726 dtype = MLD2_ALLOW_NEW_SOURCES;
1727 } else {
1728 type = MLD2_ALLOW_NEW_SOURCES;
1729 dtype = MLD2_BLOCK_OLD_SOURCES;
1730 }
1731 skb = add_grec(skb, pmc, type, 0, 0);
1732 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1733
1734 /* filter mode changes */
1735 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1737 type = MLD2_CHANGE_TO_EXCLUDE;
1738 else
1739 type = MLD2_CHANGE_TO_INCLUDE;
1740 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001741 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 }
1743 spin_unlock_bh(&pmc->mca_lock);
1744 }
1745 read_unlock_bh(&idev->lock);
1746 if (!skb)
1747 return;
1748 (void) mld_sendpack(skb);
1749}
1750
1751static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1752{
1753 struct sock *sk = igmp6_socket->sk;
1754 struct inet6_dev *idev;
1755 struct sk_buff *skb;
1756 struct icmp6hdr *hdr;
1757 struct in6_addr *snd_addr;
1758 struct in6_addr *addrp;
1759 struct in6_addr addr_buf;
1760 struct in6_addr all_routers;
1761 int err, len, payload_len, full_len;
1762 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1763 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1764 IPV6_TLV_PADN, 0 };
1765
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001766 rcu_read_lock();
1767 IP6_INC_STATS(__in6_dev_get(dev),
1768 IPSTATS_MIB_OUTREQUESTS);
1769 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 snd_addr = addr;
1771 if (type == ICMPV6_MGM_REDUCTION) {
1772 snd_addr = &all_routers;
1773 ipv6_addr_all_routers(&all_routers);
1774 }
1775
1776 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1777 payload_len = len + sizeof(ra);
1778 full_len = sizeof(struct ipv6hdr) + payload_len;
1779
1780 skb = sock_alloc_send_skb(sk, LL_RESERVED_SPACE(dev) + full_len, 1, &err);
1781
1782 if (skb == NULL) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001783 rcu_read_lock();
1784 IP6_INC_STATS(__in6_dev_get(dev),
1785 IPSTATS_MIB_OUTDISCARDS);
1786 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 return;
1788 }
1789
1790 skb_reserve(skb, LL_RESERVED_SPACE(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792 if (ipv6_get_lladdr(dev, &addr_buf)) {
1793 /* <draft-ietf-magma-mld-source-05.txt>:
1794 * use unspecified address as the source address
1795 * when a valid link-local address is not available.
1796 */
1797 memset(&addr_buf, 0, sizeof(addr_buf));
1798 }
1799
1800 ip6_nd_hdr(sk, skb, dev, &addr_buf, snd_addr, NEXTHDR_HOP, payload_len);
1801
1802 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1803
1804 hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
1805 memset(hdr, 0, sizeof(struct icmp6hdr));
1806 hdr->icmp6_type = type;
1807
1808 addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
1809 ipv6_addr_copy(addrp, addr);
1810
1811 hdr->icmp6_cksum = csum_ipv6_magic(&addr_buf, snd_addr, len,
1812 IPPROTO_ICMPV6,
1813 csum_partial((__u8 *) hdr, len, 0));
1814
1815 idev = in6_dev_get(skb->dev);
1816
1817 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
Patrick McHardye9823182005-06-21 14:02:15 -07001818 mld_dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (!err) {
1820 if (type == ICMPV6_MGM_REDUCTION)
1821 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBREDUCTIONS);
1822 else
1823 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBRESPONSES);
1824 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTMSGS);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001825 IP6_INC_STATS(idev, IPSTATS_MIB_OUTMCASTPKTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 } else
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001827 IP6_INC_STATS(idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
1829 if (likely(idev != NULL))
1830 in6_dev_put(idev);
1831 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832}
1833
1834static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1835 struct in6_addr *psfsrc)
1836{
1837 struct ip6_sf_list *psf, *psf_prev;
1838 int rv = 0;
1839
1840 psf_prev = NULL;
1841 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1842 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1843 break;
1844 psf_prev = psf;
1845 }
1846 if (!psf || psf->sf_count[sfmode] == 0) {
1847 /* source filter not found, or count wrong => bug */
1848 return -ESRCH;
1849 }
1850 psf->sf_count[sfmode]--;
1851 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1852 struct inet6_dev *idev = pmc->idev;
1853
1854 /* no more filters for this source */
1855 if (psf_prev)
1856 psf_prev->sf_next = psf->sf_next;
1857 else
1858 pmc->mca_sources = psf->sf_next;
1859 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1860 !MLD_V1_SEEN(idev)) {
1861 psf->sf_crcount = idev->mc_qrv;
1862 psf->sf_next = pmc->mca_tomb;
1863 pmc->mca_tomb = psf;
1864 rv = 1;
1865 } else
1866 kfree(psf);
1867 }
1868 return rv;
1869}
1870
1871static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
1872 int sfmode, int sfcount, struct in6_addr *psfsrc,
1873 int delta)
1874{
1875 struct ifmcaddr6 *pmc;
1876 int changerec = 0;
1877 int i, err;
1878
1879 if (!idev)
1880 return -ENODEV;
1881 read_lock_bh(&idev->lock);
1882 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1883 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1884 break;
1885 }
1886 if (!pmc) {
1887 /* MCA not found?? bug */
1888 read_unlock_bh(&idev->lock);
1889 return -ESRCH;
1890 }
1891 spin_lock_bh(&pmc->mca_lock);
1892 sf_markstate(pmc);
1893 if (!delta) {
1894 if (!pmc->mca_sfcount[sfmode]) {
1895 spin_unlock_bh(&pmc->mca_lock);
1896 read_unlock_bh(&idev->lock);
1897 return -EINVAL;
1898 }
1899 pmc->mca_sfcount[sfmode]--;
1900 }
1901 err = 0;
1902 for (i=0; i<sfcount; i++) {
1903 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1904
1905 changerec |= rv > 0;
1906 if (!err && rv < 0)
1907 err = rv;
1908 }
1909 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1910 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1911 pmc->mca_sfcount[MCAST_INCLUDE]) {
1912 struct ip6_sf_list *psf;
1913
1914 /* filter mode change */
1915 pmc->mca_sfmode = MCAST_INCLUDE;
1916 pmc->mca_crcount = idev->mc_qrv;
1917 idev->mc_ifc_count = pmc->mca_crcount;
1918 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1919 psf->sf_crcount = 0;
1920 mld_ifc_event(pmc->idev);
1921 } else if (sf_setstate(pmc) || changerec)
1922 mld_ifc_event(pmc->idev);
1923 spin_unlock_bh(&pmc->mca_lock);
1924 read_unlock_bh(&idev->lock);
1925 return err;
1926}
1927
1928/*
1929 * Add multicast single-source filter to the interface list
1930 */
1931static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1932 struct in6_addr *psfsrc, int delta)
1933{
1934 struct ip6_sf_list *psf, *psf_prev;
1935
1936 psf_prev = NULL;
1937 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1938 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1939 break;
1940 psf_prev = psf;
1941 }
1942 if (!psf) {
Ingo Oeser0c600ed2006-03-20 23:01:32 -08001943 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (!psf)
1945 return -ENOBUFS;
Ingo Oeser0c600ed2006-03-20 23:01:32 -08001946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 psf->sf_addr = *psfsrc;
1948 if (psf_prev) {
1949 psf_prev->sf_next = psf;
1950 } else
1951 pmc->mca_sources = psf;
1952 }
1953 psf->sf_count[sfmode]++;
1954 return 0;
1955}
1956
1957static void sf_markstate(struct ifmcaddr6 *pmc)
1958{
1959 struct ip6_sf_list *psf;
1960 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1961
1962 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1963 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1964 psf->sf_oldin = mca_xcount ==
1965 psf->sf_count[MCAST_EXCLUDE] &&
1966 !psf->sf_count[MCAST_INCLUDE];
1967 } else
1968 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1969}
1970
1971static int sf_setstate(struct ifmcaddr6 *pmc)
1972{
David L Stevens7add2a42006-01-24 13:06:39 -08001973 struct ip6_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1975 int qrv = pmc->idev->mc_qrv;
1976 int new_in, rv;
1977
1978 rv = 0;
1979 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1980 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1981 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1982 !psf->sf_count[MCAST_INCLUDE];
1983 } else
1984 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevens7add2a42006-01-24 13:06:39 -08001985 if (new_in) {
1986 if (!psf->sf_oldin) {
Al Viroe80e28b2006-02-03 20:10:03 -05001987 struct ip6_sf_list *prev = NULL;
David L Stevens7add2a42006-01-24 13:06:39 -08001988
1989 for (dpsf=pmc->mca_tomb; dpsf;
1990 dpsf=dpsf->sf_next) {
1991 if (ipv6_addr_equal(&dpsf->sf_addr,
1992 &psf->sf_addr))
1993 break;
1994 prev = dpsf;
1995 }
1996 if (dpsf) {
1997 if (prev)
1998 prev->sf_next = dpsf->sf_next;
1999 else
2000 pmc->mca_tomb = dpsf->sf_next;
2001 kfree(dpsf);
2002 }
2003 psf->sf_crcount = qrv;
2004 rv++;
2005 }
2006 } else if (psf->sf_oldin) {
2007 psf->sf_crcount = 0;
2008 /*
2009 * add or update "delete" records if an active filter
2010 * is now inactive
2011 */
2012 for (dpsf=pmc->mca_tomb; dpsf; dpsf=dpsf->sf_next)
2013 if (ipv6_addr_equal(&dpsf->sf_addr,
2014 &psf->sf_addr))
2015 break;
2016 if (!dpsf) {
2017 dpsf = (struct ip6_sf_list *)
2018 kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2019 if (!dpsf)
2020 continue;
2021 *dpsf = *psf;
2022 /* pmc->mca_lock held by callers */
2023 dpsf->sf_next = pmc->mca_tomb;
2024 pmc->mca_tomb = dpsf;
2025 }
2026 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 rv++;
2028 }
2029 }
2030 return rv;
2031}
2032
2033/*
2034 * Add multicast source filter list to the interface list
2035 */
2036static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
2037 int sfmode, int sfcount, struct in6_addr *psfsrc,
2038 int delta)
2039{
2040 struct ifmcaddr6 *pmc;
2041 int isexclude;
2042 int i, err;
2043
2044 if (!idev)
2045 return -ENODEV;
2046 read_lock_bh(&idev->lock);
2047 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
2048 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2049 break;
2050 }
2051 if (!pmc) {
2052 /* MCA not found?? bug */
2053 read_unlock_bh(&idev->lock);
2054 return -ESRCH;
2055 }
2056 spin_lock_bh(&pmc->mca_lock);
2057
2058 sf_markstate(pmc);
2059 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2060 if (!delta)
2061 pmc->mca_sfcount[sfmode]++;
2062 err = 0;
2063 for (i=0; i<sfcount; i++) {
2064 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
2065 if (err)
2066 break;
2067 }
2068 if (err) {
2069 int j;
2070
2071 if (!delta)
2072 pmc->mca_sfcount[sfmode]--;
2073 for (j=0; j<i; j++)
2074 (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2075 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
2076 struct inet6_dev *idev = pmc->idev;
2077 struct ip6_sf_list *psf;
2078
2079 /* filter mode change */
2080 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2081 pmc->mca_sfmode = MCAST_EXCLUDE;
2082 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2083 pmc->mca_sfmode = MCAST_INCLUDE;
2084 /* else no filters; keep old mode for reports */
2085
2086 pmc->mca_crcount = idev->mc_qrv;
2087 idev->mc_ifc_count = pmc->mca_crcount;
2088 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
2089 psf->sf_crcount = 0;
2090 mld_ifc_event(idev);
2091 } else if (sf_setstate(pmc))
2092 mld_ifc_event(idev);
2093 spin_unlock_bh(&pmc->mca_lock);
2094 read_unlock_bh(&idev->lock);
2095 return err;
2096}
2097
2098static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2099{
2100 struct ip6_sf_list *psf, *nextpsf;
2101
2102 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
2103 nextpsf = psf->sf_next;
2104 kfree(psf);
2105 }
2106 pmc->mca_tomb = NULL;
2107 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
2108 nextpsf = psf->sf_next;
2109 kfree(psf);
2110 }
2111 pmc->mca_sources = NULL;
2112 pmc->mca_sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002113 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2115}
2116
2117
2118static void igmp6_join_group(struct ifmcaddr6 *ma)
2119{
2120 unsigned long delay;
2121
2122 if (ma->mca_flags & MAF_NOREPORT)
2123 return;
2124
2125 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2126
2127 delay = net_random() % IGMP6_UNSOLICITED_IVAL;
2128
2129 spin_lock_bh(&ma->mca_lock);
2130 if (del_timer(&ma->mca_timer)) {
2131 atomic_dec(&ma->mca_refcnt);
2132 delay = ma->mca_timer.expires - jiffies;
2133 }
2134
2135 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2136 atomic_inc(&ma->mca_refcnt);
2137 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2138 spin_unlock_bh(&ma->mca_lock);
2139}
2140
2141static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2142 struct inet6_dev *idev)
2143{
2144 int err;
2145
David L Stevens5ab4a6c2005-12-27 14:03:00 -08002146 /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
2147 * so no other readers or writers of iml or its sflist
2148 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (iml->sflist == 0) {
2150 /* any-source empty exclude case */
2151 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2152 }
2153 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2154 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2155 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2156 iml->sflist = NULL;
2157 return err;
2158}
2159
2160static void igmp6_leave_group(struct ifmcaddr6 *ma)
2161{
2162 if (MLD_V1_SEEN(ma->idev)) {
2163 if (ma->mca_flags & MAF_LAST_REPORTER)
2164 igmp6_send(&ma->mca_addr, ma->idev->dev,
2165 ICMPV6_MGM_REDUCTION);
2166 } else {
2167 mld_add_delrec(ma->idev, ma);
2168 mld_ifc_event(ma->idev);
2169 }
2170}
2171
2172static void mld_gq_timer_expire(unsigned long data)
2173{
2174 struct inet6_dev *idev = (struct inet6_dev *)data;
2175
2176 idev->mc_gq_running = 0;
2177 mld_send_report(idev, NULL);
2178 __in6_dev_put(idev);
2179}
2180
2181static void mld_ifc_timer_expire(unsigned long data)
2182{
2183 struct inet6_dev *idev = (struct inet6_dev *)data;
2184
2185 mld_send_cr(idev);
2186 if (idev->mc_ifc_count) {
2187 idev->mc_ifc_count--;
2188 if (idev->mc_ifc_count)
2189 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2190 }
2191 __in6_dev_put(idev);
2192}
2193
2194static void mld_ifc_event(struct inet6_dev *idev)
2195{
2196 if (MLD_V1_SEEN(idev))
2197 return;
2198 idev->mc_ifc_count = idev->mc_qrv;
2199 mld_ifc_start_timer(idev, 1);
2200}
2201
2202
2203static void igmp6_timer_handler(unsigned long data)
2204{
2205 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2206
2207 if (MLD_V1_SEEN(ma->idev))
2208 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2209 else
2210 mld_send_report(ma->idev, ma);
2211
2212 spin_lock(&ma->mca_lock);
2213 ma->mca_flags |= MAF_LAST_REPORTER;
2214 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2215 spin_unlock(&ma->mca_lock);
2216 ma_put(ma);
2217}
2218
2219/* Device going down */
2220
2221void ipv6_mc_down(struct inet6_dev *idev)
2222{
2223 struct ifmcaddr6 *i;
2224
2225 /* Withdraw multicast list */
2226
2227 read_lock_bh(&idev->lock);
2228 idev->mc_ifc_count = 0;
2229 if (del_timer(&idev->mc_ifc_timer))
2230 __in6_dev_put(idev);
2231 idev->mc_gq_running = 0;
2232 if (del_timer(&idev->mc_gq_timer))
2233 __in6_dev_put(idev);
2234
2235 for (i = idev->mc_list; i; i=i->next)
2236 igmp6_group_dropped(i);
2237 read_unlock_bh(&idev->lock);
2238
2239 mld_clear_delrec(idev);
2240}
2241
2242
2243/* Device going up */
2244
2245void ipv6_mc_up(struct inet6_dev *idev)
2246{
2247 struct ifmcaddr6 *i;
2248
2249 /* Install multicast list, except for all-nodes (already installed) */
2250
2251 read_lock_bh(&idev->lock);
2252 for (i = idev->mc_list; i; i=i->next)
2253 igmp6_group_added(i);
2254 read_unlock_bh(&idev->lock);
2255}
2256
2257/* IPv6 device initialization. */
2258
2259void ipv6_mc_init_dev(struct inet6_dev *idev)
2260{
2261 struct in6_addr maddr;
2262
2263 write_lock_bh(&idev->lock);
2264 rwlock_init(&idev->mc_lock);
2265 idev->mc_gq_running = 0;
2266 init_timer(&idev->mc_gq_timer);
2267 idev->mc_gq_timer.data = (unsigned long) idev;
2268 idev->mc_gq_timer.function = &mld_gq_timer_expire;
2269 idev->mc_tomb = NULL;
2270 idev->mc_ifc_count = 0;
2271 init_timer(&idev->mc_ifc_timer);
2272 idev->mc_ifc_timer.data = (unsigned long) idev;
2273 idev->mc_ifc_timer.function = &mld_ifc_timer_expire;
2274 idev->mc_qrv = MLD_QRV_DEFAULT;
2275 idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2276 idev->mc_v1_seen = 0;
2277 write_unlock_bh(&idev->lock);
2278
2279 /* Add all-nodes address. */
2280 ipv6_addr_all_nodes(&maddr);
2281 ipv6_dev_mc_inc(idev->dev, &maddr);
2282}
2283
2284/*
2285 * Device is about to be destroyed: clean up.
2286 */
2287
2288void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2289{
2290 struct ifmcaddr6 *i;
2291 struct in6_addr maddr;
2292
2293 /* Deactivate timers */
2294 ipv6_mc_down(idev);
2295
2296 /* Delete all-nodes address. */
2297 ipv6_addr_all_nodes(&maddr);
2298
2299 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2300 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2301 * fail.
2302 */
2303 __ipv6_dev_mc_dec(idev, &maddr);
2304
2305 if (idev->cnf.forwarding) {
2306 ipv6_addr_all_routers(&maddr);
2307 __ipv6_dev_mc_dec(idev, &maddr);
2308 }
2309
2310 write_lock_bh(&idev->lock);
2311 while ((i = idev->mc_list) != NULL) {
2312 idev->mc_list = i->next;
2313 write_unlock_bh(&idev->lock);
2314
2315 igmp6_group_dropped(i);
2316 ma_put(i);
2317
2318 write_lock_bh(&idev->lock);
2319 }
2320 write_unlock_bh(&idev->lock);
2321}
2322
2323#ifdef CONFIG_PROC_FS
2324struct igmp6_mc_iter_state {
2325 struct net_device *dev;
2326 struct inet6_dev *idev;
2327};
2328
2329#define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2330
2331static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2332{
2333 struct ifmcaddr6 *im = NULL;
2334 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2335
2336 for (state->dev = dev_base, state->idev = NULL;
2337 state->dev;
2338 state->dev = state->dev->next) {
2339 struct inet6_dev *idev;
2340 idev = in6_dev_get(state->dev);
2341 if (!idev)
2342 continue;
2343 read_lock_bh(&idev->lock);
2344 im = idev->mc_list;
2345 if (im) {
2346 state->idev = idev;
2347 break;
2348 }
2349 read_unlock_bh(&idev->lock);
2350 in6_dev_put(idev);
2351 }
2352 return im;
2353}
2354
2355static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2356{
2357 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2358
2359 im = im->next;
2360 while (!im) {
2361 if (likely(state->idev != NULL)) {
2362 read_unlock_bh(&state->idev->lock);
2363 in6_dev_put(state->idev);
2364 }
2365 state->dev = state->dev->next;
2366 if (!state->dev) {
2367 state->idev = NULL;
2368 break;
2369 }
2370 state->idev = in6_dev_get(state->dev);
2371 if (!state->idev)
2372 continue;
2373 read_lock_bh(&state->idev->lock);
2374 im = state->idev->mc_list;
2375 }
2376 return im;
2377}
2378
2379static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2380{
2381 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2382 if (im)
2383 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2384 --pos;
2385 return pos ? NULL : im;
2386}
2387
2388static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2389{
2390 read_lock(&dev_base_lock);
2391 return igmp6_mc_get_idx(seq, *pos);
2392}
2393
2394static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2395{
2396 struct ifmcaddr6 *im;
2397 im = igmp6_mc_get_next(seq, v);
2398 ++*pos;
2399 return im;
2400}
2401
2402static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2403{
2404 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2405 if (likely(state->idev != NULL)) {
2406 read_unlock_bh(&state->idev->lock);
2407 in6_dev_put(state->idev);
2408 state->idev = NULL;
2409 }
2410 state->dev = NULL;
2411 read_unlock(&dev_base_lock);
2412}
2413
2414static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2415{
2416 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2417 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2418
2419 seq_printf(seq,
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002420 "%-4d %-15s " NIP6_SEQFMT " %5d %08X %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 state->dev->ifindex, state->dev->name,
2422 NIP6(im->mca_addr),
2423 im->mca_users, im->mca_flags,
2424 (im->mca_flags&MAF_TIMER_RUNNING) ?
2425 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2426 return 0;
2427}
2428
2429static struct seq_operations igmp6_mc_seq_ops = {
2430 .start = igmp6_mc_seq_start,
2431 .next = igmp6_mc_seq_next,
2432 .stop = igmp6_mc_seq_stop,
2433 .show = igmp6_mc_seq_show,
2434};
2435
2436static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2437{
2438 struct seq_file *seq;
2439 int rc = -ENOMEM;
Ingo Oeser0c600ed2006-03-20 23:01:32 -08002440 struct igmp6_mc_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
2442 if (!s)
2443 goto out;
2444
2445 rc = seq_open(file, &igmp6_mc_seq_ops);
2446 if (rc)
2447 goto out_kfree;
2448
2449 seq = file->private_data;
2450 seq->private = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451out:
2452 return rc;
2453out_kfree:
2454 kfree(s);
2455 goto out;
2456}
2457
2458static struct file_operations igmp6_mc_seq_fops = {
2459 .owner = THIS_MODULE,
2460 .open = igmp6_mc_seq_open,
2461 .read = seq_read,
2462 .llseek = seq_lseek,
2463 .release = seq_release_private,
2464};
2465
2466struct igmp6_mcf_iter_state {
2467 struct net_device *dev;
2468 struct inet6_dev *idev;
2469 struct ifmcaddr6 *im;
2470};
2471
2472#define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2473
2474static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2475{
2476 struct ip6_sf_list *psf = NULL;
2477 struct ifmcaddr6 *im = NULL;
2478 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2479
2480 for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
2481 state->dev;
2482 state->dev = state->dev->next) {
2483 struct inet6_dev *idev;
2484 idev = in6_dev_get(state->dev);
2485 if (unlikely(idev == NULL))
2486 continue;
2487 read_lock_bh(&idev->lock);
2488 im = idev->mc_list;
2489 if (likely(im != NULL)) {
2490 spin_lock_bh(&im->mca_lock);
2491 psf = im->mca_sources;
2492 if (likely(psf != NULL)) {
2493 state->im = im;
2494 state->idev = idev;
2495 break;
2496 }
2497 spin_unlock_bh(&im->mca_lock);
2498 }
2499 read_unlock_bh(&idev->lock);
2500 in6_dev_put(idev);
2501 }
2502 return psf;
2503}
2504
2505static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2506{
2507 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2508
2509 psf = psf->sf_next;
2510 while (!psf) {
2511 spin_unlock_bh(&state->im->mca_lock);
2512 state->im = state->im->next;
2513 while (!state->im) {
2514 if (likely(state->idev != NULL)) {
2515 read_unlock_bh(&state->idev->lock);
2516 in6_dev_put(state->idev);
2517 }
2518 state->dev = state->dev->next;
2519 if (!state->dev) {
2520 state->idev = NULL;
2521 goto out;
2522 }
2523 state->idev = in6_dev_get(state->dev);
2524 if (!state->idev)
2525 continue;
2526 read_lock_bh(&state->idev->lock);
2527 state->im = state->idev->mc_list;
2528 }
2529 if (!state->im)
2530 break;
2531 spin_lock_bh(&state->im->mca_lock);
2532 psf = state->im->mca_sources;
2533 }
2534out:
2535 return psf;
2536}
2537
2538static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2539{
2540 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2541 if (psf)
2542 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2543 --pos;
2544 return pos ? NULL : psf;
2545}
2546
2547static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2548{
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)
2565{
2566 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2567 if (likely(state->im != NULL)) {
2568 spin_unlock_bh(&state->im->mca_lock);
2569 state->im = NULL;
2570 }
2571 if (likely(state->idev != NULL)) {
2572 read_unlock_bh(&state->idev->lock);
2573 in6_dev_put(state->idev);
2574 state->idev = NULL;
2575 }
2576 state->dev = NULL;
2577 read_unlock(&dev_base_lock);
2578}
2579
2580static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2581{
2582 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2583 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2584
2585 if (v == SEQ_START_TOKEN) {
2586 seq_printf(seq,
2587 "%3s %6s "
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002588 "%32s %32s %6s %6s\n", "Idx",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 "Device", "Multicast Address",
2590 "Source Address", "INC", "EXC");
2591 } else {
2592 seq_printf(seq,
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002593 "%3d %6.6s " NIP6_SEQFMT " " NIP6_SEQFMT " %6lu %6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 state->dev->ifindex, state->dev->name,
2595 NIP6(state->im->mca_addr),
2596 NIP6(psf->sf_addr),
2597 psf->sf_count[MCAST_INCLUDE],
2598 psf->sf_count[MCAST_EXCLUDE]);
2599 }
2600 return 0;
2601}
2602
2603static struct seq_operations igmp6_mcf_seq_ops = {
2604 .start = igmp6_mcf_seq_start,
2605 .next = igmp6_mcf_seq_next,
2606 .stop = igmp6_mcf_seq_stop,
2607 .show = igmp6_mcf_seq_show,
2608};
2609
2610static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2611{
2612 struct seq_file *seq;
2613 int rc = -ENOMEM;
Ingo Oeser0c600ed2006-03-20 23:01:32 -08002614 struct igmp6_mcf_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
2616 if (!s)
2617 goto out;
2618
2619 rc = seq_open(file, &igmp6_mcf_seq_ops);
2620 if (rc)
2621 goto out_kfree;
2622
2623 seq = file->private_data;
2624 seq->private = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625out:
2626 return rc;
2627out_kfree:
2628 kfree(s);
2629 goto out;
2630}
2631
2632static struct file_operations igmp6_mcf_seq_fops = {
2633 .owner = THIS_MODULE,
2634 .open = igmp6_mcf_seq_open,
2635 .read = seq_read,
2636 .llseek = seq_lseek,
2637 .release = seq_release_private,
2638};
2639#endif
2640
2641int __init igmp6_init(struct net_proto_family *ops)
2642{
2643 struct ipv6_pinfo *np;
2644 struct sock *sk;
2645 int err;
2646
2647 err = sock_create_kern(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6, &igmp6_socket);
2648 if (err < 0) {
2649 printk(KERN_ERR
2650 "Failed to initialize the IGMP6 control socket (err %d).\n",
2651 err);
2652 igmp6_socket = NULL; /* For safety. */
2653 return err;
2654 }
2655
2656 sk = igmp6_socket->sk;
2657 sk->sk_allocation = GFP_ATOMIC;
2658 sk->sk_prot->unhash(sk);
2659
2660 np = inet6_sk(sk);
2661 np->hop_limit = 1;
2662
2663#ifdef CONFIG_PROC_FS
2664 proc_net_fops_create("igmp6", S_IRUGO, &igmp6_mc_seq_fops);
2665 proc_net_fops_create("mcfilter6", S_IRUGO, &igmp6_mcf_seq_fops);
2666#endif
2667
2668 return 0;
2669}
2670
2671void igmp6_cleanup(void)
2672{
2673 sock_release(igmp6_socket);
2674 igmp6_socket = NULL; /* for safety */
2675
2676#ifdef CONFIG_PROC_FS
2677 proc_net_remove("mcfilter6");
2678 proc_net_remove("igmp6");
2679#endif
2680}