]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/ipv6/mcast.c
[NETFILTER]: Kill nf_debug
[net-next-2.6.git] / net / ipv6 / mcast.c
CommitLineData
1da177e4
LT
1/*
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
31#include <linux/config.h>
32#include <linux/module.h>
33#include <linux/errno.h>
34#include <linux/types.h>
35#include <linux/string.h>
36#include <linux/socket.h>
37#include <linux/sockios.h>
38#include <linux/jiffies.h>
39#include <linux/times.h>
40#include <linux/net.h>
41#include <linux/in.h>
42#include <linux/in6.h>
43#include <linux/netdevice.h>
44#include <linux/if_arp.h>
45#include <linux/route.h>
46#include <linux/init.h>
47#include <linux/proc_fs.h>
48#include <linux/seq_file.h>
49
50#include <linux/netfilter.h>
51#include <linux/netfilter_ipv6.h>
52
53#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>
62
63#include <net/ip6_checksum.h>
64
65/* Set to 3 to get tracing... */
66#define MCAST_DEBUG 2
67
68#if MCAST_DEBUG >= 3
69#define MDBG(x) printk x
70#else
71#define MDBG(x)
72#endif
73
74/*
75 * These header formats should be in a separate include file, but icmpv6.h
76 * doesn't have in6_addr defined in all cases, there is no __u128, and no
77 * other files reference these.
78 *
79 * +-DLS 4/14/03
80 */
81
82/* Multicast Listener Discovery version 2 headers */
83
84struct mld2_grec {
85 __u8 grec_type;
86 __u8 grec_auxwords;
87 __u16 grec_nsrcs;
88 struct in6_addr grec_mca;
89 struct in6_addr grec_src[0];
90};
91
92struct mld2_report {
93 __u8 type;
94 __u8 resv1;
95 __u16 csum;
96 __u16 resv2;
97 __u16 ngrec;
98 struct mld2_grec grec[0];
99};
100
101struct mld2_query {
102 __u8 type;
103 __u8 code;
104 __u16 csum;
105 __u16 mrc;
106 __u16 resv1;
107 struct in6_addr mca;
108#if defined(__LITTLE_ENDIAN_BITFIELD)
109 __u8 qrv:3,
110 suppress:1,
111 resv2:4;
112#elif defined(__BIG_ENDIAN_BITFIELD)
113 __u8 resv2:4,
114 suppress:1,
115 qrv:3;
116#else
117#error "Please fix <asm/byteorder.h>"
118#endif
119 __u8 qqic;
120 __u16 nsrcs;
121 struct in6_addr srcs[0];
122};
123
124static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;
125
126/* Big mc list lock for all the sockets */
127static DEFINE_RWLOCK(ipv6_sk_mc_lock);
128
129static struct socket *igmp6_socket;
130
131int __ipv6_dev_mc_dec(struct inet6_dev *idev, struct in6_addr *addr);
132
133static void igmp6_join_group(struct ifmcaddr6 *ma);
134static void igmp6_leave_group(struct ifmcaddr6 *ma);
135static void igmp6_timer_handler(unsigned long data);
136
137static void mld_gq_timer_expire(unsigned long data);
138static void mld_ifc_timer_expire(unsigned long data);
139static void mld_ifc_event(struct inet6_dev *idev);
140static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
141static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *addr);
142static void mld_clear_delrec(struct inet6_dev *idev);
143static int sf_setstate(struct ifmcaddr6 *pmc);
144static void sf_markstate(struct ifmcaddr6 *pmc);
145static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
146static int ip6_mc_del_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_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
150 int sfmode, int sfcount, struct in6_addr *psfsrc,
151 int delta);
152static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
153 struct inet6_dev *idev);
154
155
156#define IGMP6_UNSOLICITED_IVAL (10*HZ)
157#define MLD_QRV_DEFAULT 2
158
159#define MLD_V1_SEEN(idev) (ipv6_devconf.force_mld_version == 1 || \
160 (idev)->cnf.force_mld_version == 1 || \
161 ((idev)->mc_v1_seen && \
162 time_before(jiffies, (idev)->mc_v1_seen)))
163
164#define MLDV2_MASK(value, nb) ((nb)>=32 ? (value) : ((1<<(nb))-1) & (value))
165#define MLDV2_EXP(thresh, nbmant, nbexp, value) \
166 ((value) < (thresh) ? (value) : \
167 ((MLDV2_MASK(value, nbmant) | (1<<(nbmant+nbexp))) << \
168 (MLDV2_MASK((value) >> (nbmant), nbexp) + (nbexp))))
169
170#define MLDV2_QQIC(value) MLDV2_EXP(0x80, 4, 3, value)
171#define MLDV2_MRC(value) MLDV2_EXP(0x8000, 12, 3, value)
172
173#define IPV6_MLD_MAX_MSF 10
174
175int sysctl_mld_max_msf = IPV6_MLD_MAX_MSF;
176
177/*
178 * socket join on multicast group
179 */
180
181int ipv6_sock_mc_join(struct sock *sk, int ifindex, struct in6_addr *addr)
182{
183 struct net_device *dev = NULL;
184 struct ipv6_mc_socklist *mc_lst;
185 struct ipv6_pinfo *np = inet6_sk(sk);
186 int err;
187
188 if (!ipv6_addr_is_multicast(addr))
189 return -EINVAL;
190
c9e3e8b6
DS
191 read_lock_bh(&ipv6_sk_mc_lock);
192 for (mc_lst=np->ipv6_mc_list; mc_lst; mc_lst=mc_lst->next) {
193 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
194 ipv6_addr_equal(&mc_lst->addr, addr)) {
195 read_unlock_bh(&ipv6_sk_mc_lock);
196 return -EADDRINUSE;
197 }
198 }
199 read_unlock_bh(&ipv6_sk_mc_lock);
200
1da177e4
LT
201 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
202
203 if (mc_lst == NULL)
204 return -ENOMEM;
205
206 mc_lst->next = NULL;
207 ipv6_addr_copy(&mc_lst->addr, addr);
208
209 if (ifindex == 0) {
210 struct rt6_info *rt;
211 rt = rt6_lookup(addr, NULL, 0, 0);
212 if (rt) {
213 dev = rt->rt6i_dev;
214 dev_hold(dev);
215 dst_release(&rt->u.dst);
216 }
217 } else
218 dev = dev_get_by_index(ifindex);
219
220 if (dev == NULL) {
221 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
222 return -ENODEV;
223 }
224
225 mc_lst->ifindex = dev->ifindex;
226 mc_lst->sfmode = MCAST_EXCLUDE;
227 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
271 if (idev) {
272 (void) ip6_mc_leave_src(sk,mc_lst,idev);
273 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
274 in6_dev_put(idev);
275 }
276 dev_put(dev);
277 }
278 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
279 return 0;
280 }
281 }
282 write_unlock_bh(&ipv6_sk_mc_lock);
283
284 return -ENOENT;
285}
286
287static struct inet6_dev *ip6_mc_find_dev(struct in6_addr *group, int ifindex)
288{
289 struct net_device *dev = NULL;
290 struct inet6_dev *idev = NULL;
291
292 if (ifindex == 0) {
293 struct rt6_info *rt;
294
295 rt = rt6_lookup(group, NULL, 0, 0);
296 if (rt) {
297 dev = rt->rt6i_dev;
298 dev_hold(dev);
299 dst_release(&rt->u.dst);
300 }
301 } else
302 dev = dev_get_by_index(ifindex);
303
304 if (!dev)
305 return NULL;
306 idev = in6_dev_get(dev);
307 if (!idev) {
308 dev_put(dev);
309 return NULL;
310 }
311 read_lock_bh(&idev->lock);
312 if (idev->dead) {
313 read_unlock_bh(&idev->lock);
314 in6_dev_put(idev);
315 dev_put(dev);
316 return NULL;
317 }
318 return idev;
319}
320
321void ipv6_sock_mc_close(struct sock *sk)
322{
323 struct ipv6_pinfo *np = inet6_sk(sk);
324 struct ipv6_mc_socklist *mc_lst;
325
326 write_lock_bh(&ipv6_sk_mc_lock);
327 while ((mc_lst = np->ipv6_mc_list) != NULL) {
328 struct net_device *dev;
329
330 np->ipv6_mc_list = mc_lst->next;
331 write_unlock_bh(&ipv6_sk_mc_lock);
332
333 dev = dev_get_by_index(mc_lst->ifindex);
334 if (dev) {
335 struct inet6_dev *idev = in6_dev_get(dev);
336
337 if (idev) {
338 (void) ip6_mc_leave_src(sk, mc_lst, idev);
339 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
340 in6_dev_put(idev);
341 }
342 dev_put(dev);
343 }
344
345 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
346
347 write_lock_bh(&ipv6_sk_mc_lock);
348 }
349 write_unlock_bh(&ipv6_sk_mc_lock);
350}
351
352int ip6_mc_source(int add, int omode, struct sock *sk,
353 struct group_source_req *pgsr)
354{
355 struct in6_addr *source, *group;
356 struct ipv6_mc_socklist *pmc;
357 struct net_device *dev;
358 struct inet6_dev *idev;
359 struct ipv6_pinfo *inet6 = inet6_sk(sk);
360 struct ip6_sf_socklist *psl;
361 int i, j, rv;
c9e3e8b6 362 int leavegroup = 0;
1da177e4
LT
363 int err;
364
365 if (pgsr->gsr_group.ss_family != AF_INET6 ||
366 pgsr->gsr_source.ss_family != AF_INET6)
367 return -EINVAL;
368
369 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
370 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
371
372 if (!ipv6_addr_is_multicast(group))
373 return -EINVAL;
374
375 idev = ip6_mc_find_dev(group, pgsr->gsr_interface);
376 if (!idev)
377 return -ENODEV;
378 dev = idev->dev;
379
380 err = -EADDRNOTAVAIL;
381
c9e3e8b6 382 read_lock_bh(&ipv6_sk_mc_lock);
1da177e4
LT
383 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
384 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
385 continue;
386 if (ipv6_addr_equal(&pmc->addr, group))
387 break;
388 }
389 if (!pmc) /* must have a prior join */
390 goto done;
391 /* if a source filter was set, must be the same mode as before */
392 if (pmc->sflist) {
393 if (pmc->sfmode != omode)
394 goto done;
395 } else if (pmc->sfmode != omode) {
396 /* allow mode switches for empty-set filters */
397 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
398 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
399 pmc->sfmode = omode;
400 }
401
402 psl = pmc->sflist;
403 if (!add) {
404 if (!psl)
405 goto done;
406 rv = !0;
407 for (i=0; i<psl->sl_count; i++) {
408 rv = memcmp(&psl->sl_addr[i], source,
409 sizeof(struct in6_addr));
410 if (rv == 0)
411 break;
412 }
413 if (rv) /* source not found */
414 goto done;
415
c9e3e8b6
DS
416 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
417 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
418 leavegroup = 1;
419 goto done;
420 }
421
1da177e4
LT
422 /* update the interface filter */
423 ip6_mc_del_src(idev, group, omode, 1, source, 1);
424
425 for (j=i+1; j<psl->sl_count; j++)
426 psl->sl_addr[j-1] = psl->sl_addr[j];
427 psl->sl_count--;
428 err = 0;
429 goto done;
430 }
431 /* else, add a new source to the filter */
432
433 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
434 err = -ENOBUFS;
435 goto done;
436 }
437 if (!psl || psl->sl_count == psl->sl_max) {
438 struct ip6_sf_socklist *newpsl;
439 int count = IP6_SFBLOCK;
440
441 if (psl)
442 count += psl->sl_max;
443 newpsl = (struct ip6_sf_socklist *)sock_kmalloc(sk,
444 IP6_SFLSIZE(count), GFP_ATOMIC);
445 if (!newpsl) {
446 err = -ENOBUFS;
447 goto done;
448 }
449 newpsl->sl_max = count;
450 newpsl->sl_count = count - IP6_SFBLOCK;
451 if (psl) {
452 for (i=0; i<psl->sl_count; i++)
453 newpsl->sl_addr[i] = psl->sl_addr[i];
454 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
455 }
456 pmc->sflist = psl = newpsl;
457 }
458 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
459 for (i=0; i<psl->sl_count; i++) {
460 rv = memcmp(&psl->sl_addr[i], source, sizeof(struct in6_addr));
461 if (rv == 0)
462 break;
463 }
464 if (rv == 0) /* address already there is an error */
465 goto done;
466 for (j=psl->sl_count-1; j>=i; j--)
467 psl->sl_addr[j+1] = psl->sl_addr[j];
468 psl->sl_addr[i] = *source;
469 psl->sl_count++;
470 err = 0;
471 /* update the interface list */
472 ip6_mc_add_src(idev, group, omode, 1, source, 1);
473done:
c9e3e8b6 474 read_unlock_bh(&ipv6_sk_mc_lock);
1da177e4
LT
475 read_unlock_bh(&idev->lock);
476 in6_dev_put(idev);
477 dev_put(dev);
c9e3e8b6
DS
478 if (leavegroup)
479 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
1da177e4
LT
480 return err;
481}
482
483int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
484{
485 struct in6_addr *group;
486 struct ipv6_mc_socklist *pmc;
487 struct net_device *dev;
488 struct inet6_dev *idev;
489 struct ipv6_pinfo *inet6 = inet6_sk(sk);
490 struct ip6_sf_socklist *newpsl, *psl;
491 int i, err;
492
493 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
494
495 if (!ipv6_addr_is_multicast(group))
496 return -EINVAL;
497 if (gsf->gf_fmode != MCAST_INCLUDE &&
498 gsf->gf_fmode != MCAST_EXCLUDE)
499 return -EINVAL;
500
501 idev = ip6_mc_find_dev(group, gsf->gf_interface);
502
503 if (!idev)
504 return -ENODEV;
505 dev = idev->dev;
506 err = -EADDRNOTAVAIL;
507
508 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
509 if (pmc->ifindex != gsf->gf_interface)
510 continue;
511 if (ipv6_addr_equal(&pmc->addr, group))
512 break;
513 }
514 if (!pmc) /* must have a prior join */
515 goto done;
516 if (gsf->gf_numsrc) {
517 newpsl = (struct ip6_sf_socklist *)sock_kmalloc(sk,
518 IP6_SFLSIZE(gsf->gf_numsrc), GFP_ATOMIC);
519 if (!newpsl) {
520 err = -ENOBUFS;
521 goto done;
522 }
523 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
524 for (i=0; i<newpsl->sl_count; ++i) {
525 struct sockaddr_in6 *psin6;
526
527 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
528 newpsl->sl_addr[i] = psin6->sin6_addr;
529 }
530 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
531 newpsl->sl_count, newpsl->sl_addr, 0);
532 if (err) {
533 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
534 goto done;
535 }
536 } else
537 newpsl = NULL;
538 psl = pmc->sflist;
539 if (psl) {
540 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
541 psl->sl_count, psl->sl_addr, 0);
542 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
543 } else
544 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
545 pmc->sflist = newpsl;
546 pmc->sfmode = gsf->gf_fmode;
547done:
548 read_unlock_bh(&idev->lock);
549 in6_dev_put(idev);
550 dev_put(dev);
551 return err;
552}
553
554int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
555 struct group_filter __user *optval, int __user *optlen)
556{
557 int err, i, count, copycount;
558 struct in6_addr *group;
559 struct ipv6_mc_socklist *pmc;
560 struct inet6_dev *idev;
561 struct net_device *dev;
562 struct ipv6_pinfo *inet6 = inet6_sk(sk);
563 struct ip6_sf_socklist *psl;
564
565 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
566
567 if (!ipv6_addr_is_multicast(group))
568 return -EINVAL;
569
570 idev = ip6_mc_find_dev(group, gsf->gf_interface);
571
572 if (!idev)
573 return -ENODEV;
574
575 dev = idev->dev;
576
577 err = -EADDRNOTAVAIL;
578
579 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
580 if (pmc->ifindex != gsf->gf_interface)
581 continue;
582 if (ipv6_addr_equal(group, &pmc->addr))
583 break;
584 }
585 if (!pmc) /* must have a prior join */
586 goto done;
587 gsf->gf_fmode = pmc->sfmode;
588 psl = pmc->sflist;
589 count = psl ? psl->sl_count : 0;
590 read_unlock_bh(&idev->lock);
591 in6_dev_put(idev);
592 dev_put(dev);
593
594 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
595 gsf->gf_numsrc = count;
596 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
597 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
598 return -EFAULT;
599 }
600 for (i=0; i<copycount; i++) {
601 struct sockaddr_in6 *psin6;
602 struct sockaddr_storage ss;
603
604 psin6 = (struct sockaddr_in6 *)&ss;
605 memset(&ss, 0, sizeof(ss));
606 psin6->sin6_family = AF_INET6;
607 psin6->sin6_addr = psl->sl_addr[i];
608 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
609 return -EFAULT;
610 }
611 return 0;
612done:
613 read_unlock_bh(&idev->lock);
614 in6_dev_put(idev);
615 dev_put(dev);
616 return err;
617}
618
619int inet6_mc_check(struct sock *sk, struct in6_addr *mc_addr,
620 struct in6_addr *src_addr)
621{
622 struct ipv6_pinfo *np = inet6_sk(sk);
623 struct ipv6_mc_socklist *mc;
624 struct ip6_sf_socklist *psl;
625 int rv = 1;
626
627 read_lock(&ipv6_sk_mc_lock);
628 for (mc = np->ipv6_mc_list; mc; mc = mc->next) {
629 if (ipv6_addr_equal(&mc->addr, mc_addr))
630 break;
631 }
632 if (!mc) {
633 read_unlock(&ipv6_sk_mc_lock);
634 return 1;
635 }
636 psl = mc->sflist;
637 if (!psl) {
638 rv = mc->sfmode == MCAST_EXCLUDE;
639 } else {
640 int i;
641
642 for (i=0; i<psl->sl_count; i++) {
643 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
644 break;
645 }
646 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
647 rv = 0;
648 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
649 rv = 0;
650 }
651 read_unlock(&ipv6_sk_mc_lock);
652
653 return rv;
654}
655
656static void ma_put(struct ifmcaddr6 *mc)
657{
658 if (atomic_dec_and_test(&mc->mca_refcnt)) {
659 in6_dev_put(mc->idev);
660 kfree(mc);
661 }
662}
663
664static void igmp6_group_added(struct ifmcaddr6 *mc)
665{
666 struct net_device *dev = mc->idev->dev;
667 char buf[MAX_ADDR_LEN];
668
669 spin_lock_bh(&mc->mca_lock);
670 if (!(mc->mca_flags&MAF_LOADED)) {
671 mc->mca_flags |= MAF_LOADED;
672 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
673 dev_mc_add(dev, buf, dev->addr_len, 0);
674 }
675 spin_unlock_bh(&mc->mca_lock);
676
677 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
678 return;
679
680 if (MLD_V1_SEEN(mc->idev)) {
681 igmp6_join_group(mc);
682 return;
683 }
684 /* else v2 */
685
686 mc->mca_crcount = mc->idev->mc_qrv;
687 mld_ifc_event(mc->idev);
688}
689
690static void igmp6_group_dropped(struct ifmcaddr6 *mc)
691{
692 struct net_device *dev = mc->idev->dev;
693 char buf[MAX_ADDR_LEN];
694
695 spin_lock_bh(&mc->mca_lock);
696 if (mc->mca_flags&MAF_LOADED) {
697 mc->mca_flags &= ~MAF_LOADED;
698 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
699 dev_mc_delete(dev, buf, dev->addr_len, 0);
700 }
701
702 if (mc->mca_flags & MAF_NOREPORT)
703 goto done;
704 spin_unlock_bh(&mc->mca_lock);
705
706 if (!mc->idev->dead)
707 igmp6_leave_group(mc);
708
709 spin_lock_bh(&mc->mca_lock);
710 if (del_timer(&mc->mca_timer))
711 atomic_dec(&mc->mca_refcnt);
712done:
713 ip6_mc_clear_src(mc);
714 spin_unlock_bh(&mc->mca_lock);
715}
716
717/*
718 * deleted ifmcaddr6 manipulation
719 */
720static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
721{
722 struct ifmcaddr6 *pmc;
723
724 /* this is an "ifmcaddr6" for convenience; only the fields below
725 * are actually used. In particular, the refcnt and users are not
726 * used for management of the delete list. Using the same structure
727 * for deleted items allows change reports to use common code with
728 * non-deleted or query-response MCA's.
729 */
730 pmc = (struct ifmcaddr6 *)kmalloc(sizeof(*pmc), GFP_ATOMIC);
731 if (!pmc)
732 return;
733 memset(pmc, 0, sizeof(*pmc));
734 spin_lock_bh(&im->mca_lock);
735 spin_lock_init(&pmc->mca_lock);
736 pmc->idev = im->idev;
737 in6_dev_hold(idev);
738 pmc->mca_addr = im->mca_addr;
739 pmc->mca_crcount = idev->mc_qrv;
740 pmc->mca_sfmode = im->mca_sfmode;
741 if (pmc->mca_sfmode == MCAST_INCLUDE) {
742 struct ip6_sf_list *psf;
743
744 pmc->mca_tomb = im->mca_tomb;
745 pmc->mca_sources = im->mca_sources;
746 im->mca_tomb = im->mca_sources = NULL;
747 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
748 psf->sf_crcount = pmc->mca_crcount;
749 }
750 spin_unlock_bh(&im->mca_lock);
751
752 write_lock_bh(&idev->mc_lock);
753 pmc->next = idev->mc_tomb;
754 idev->mc_tomb = pmc;
755 write_unlock_bh(&idev->mc_lock);
756}
757
758static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *pmca)
759{
760 struct ifmcaddr6 *pmc, *pmc_prev;
761 struct ip6_sf_list *psf, *psf_next;
762
763 write_lock_bh(&idev->mc_lock);
764 pmc_prev = NULL;
765 for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
766 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
767 break;
768 pmc_prev = pmc;
769 }
770 if (pmc) {
771 if (pmc_prev)
772 pmc_prev->next = pmc->next;
773 else
774 idev->mc_tomb = pmc->next;
775 }
776 write_unlock_bh(&idev->mc_lock);
777 if (pmc) {
778 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
779 psf_next = psf->sf_next;
780 kfree(psf);
781 }
782 in6_dev_put(pmc->idev);
783 kfree(pmc);
784 }
785}
786
787static void mld_clear_delrec(struct inet6_dev *idev)
788{
789 struct ifmcaddr6 *pmc, *nextpmc;
790
791 write_lock_bh(&idev->mc_lock);
792 pmc = idev->mc_tomb;
793 idev->mc_tomb = NULL;
794 write_unlock_bh(&idev->mc_lock);
795
796 for (; pmc; pmc = nextpmc) {
797 nextpmc = pmc->next;
798 ip6_mc_clear_src(pmc);
799 in6_dev_put(pmc->idev);
800 kfree(pmc);
801 }
802
803 /* clear dead sources, too */
804 read_lock_bh(&idev->lock);
805 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
806 struct ip6_sf_list *psf, *psf_next;
807
808 spin_lock_bh(&pmc->mca_lock);
809 psf = pmc->mca_tomb;
810 pmc->mca_tomb = NULL;
811 spin_unlock_bh(&pmc->mca_lock);
812 for (; psf; psf=psf_next) {
813 psf_next = psf->sf_next;
814 kfree(psf);
815 }
816 }
817 read_unlock_bh(&idev->lock);
818}
819
820
821/*
822 * device multicast group inc (add if not found)
823 */
824int ipv6_dev_mc_inc(struct net_device *dev, struct in6_addr *addr)
825{
826 struct ifmcaddr6 *mc;
827 struct inet6_dev *idev;
828
829 idev = in6_dev_get(dev);
830
831 if (idev == NULL)
832 return -EINVAL;
833
834 write_lock_bh(&idev->lock);
835 if (idev->dead) {
836 write_unlock_bh(&idev->lock);
837 in6_dev_put(idev);
838 return -ENODEV;
839 }
840
841 for (mc = idev->mc_list; mc; mc = mc->next) {
842 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
843 mc->mca_users++;
844 write_unlock_bh(&idev->lock);
845 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
846 NULL, 0);
847 in6_dev_put(idev);
848 return 0;
849 }
850 }
851
852 /*
853 * not found: create a new one.
854 */
855
856 mc = kmalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
857
858 if (mc == NULL) {
859 write_unlock_bh(&idev->lock);
860 in6_dev_put(idev);
861 return -ENOMEM;
862 }
863
864 memset(mc, 0, sizeof(struct ifmcaddr6));
865 init_timer(&mc->mca_timer);
866 mc->mca_timer.function = igmp6_timer_handler;
867 mc->mca_timer.data = (unsigned long) mc;
868
869 ipv6_addr_copy(&mc->mca_addr, addr);
870 mc->idev = idev;
871 mc->mca_users = 1;
872 /* mca_stamp should be updated upon changes */
873 mc->mca_cstamp = mc->mca_tstamp = jiffies;
874 atomic_set(&mc->mca_refcnt, 2);
875 spin_lock_init(&mc->mca_lock);
876
877 /* initial mode is (EX, empty) */
878 mc->mca_sfmode = MCAST_EXCLUDE;
879 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
880
881 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
882 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
883 mc->mca_flags |= MAF_NOREPORT;
884
885 mc->next = idev->mc_list;
886 idev->mc_list = mc;
887 write_unlock_bh(&idev->lock);
888
889 mld_del_delrec(idev, &mc->mca_addr);
890 igmp6_group_added(mc);
891 ma_put(mc);
892 return 0;
893}
894
895/*
896 * device multicast group del
897 */
898int __ipv6_dev_mc_dec(struct inet6_dev *idev, struct in6_addr *addr)
899{
900 struct ifmcaddr6 *ma, **map;
901
902 write_lock_bh(&idev->lock);
903 for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
904 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
905 if (--ma->mca_users == 0) {
906 *map = ma->next;
907 write_unlock_bh(&idev->lock);
908
909 igmp6_group_dropped(ma);
910
911 ma_put(ma);
912 return 0;
913 }
914 write_unlock_bh(&idev->lock);
915 return 0;
916 }
917 }
918 write_unlock_bh(&idev->lock);
919
920 return -ENOENT;
921}
922
923int ipv6_dev_mc_dec(struct net_device *dev, struct in6_addr *addr)
924{
925 struct inet6_dev *idev = in6_dev_get(dev);
926 int err;
927
928 if (!idev)
929 return -ENODEV;
930
931 err = __ipv6_dev_mc_dec(idev, addr);
932
933 in6_dev_put(idev);
934
935 return err;
936}
937
938/*
939 * identify MLD packets for MLD filter exceptions
940 */
941int ipv6_is_mld(struct sk_buff *skb, int nexthdr)
942{
943 struct icmp6hdr *pic;
944
945 if (nexthdr != IPPROTO_ICMPV6)
946 return 0;
947
948 if (!pskb_may_pull(skb, sizeof(struct icmp6hdr)))
949 return 0;
950
951 pic = (struct icmp6hdr *)skb->h.raw;
952
953 switch (pic->icmp6_type) {
954 case ICMPV6_MGM_QUERY:
955 case ICMPV6_MGM_REPORT:
956 case ICMPV6_MGM_REDUCTION:
957 case ICMPV6_MLD2_REPORT:
958 return 1;
959 default:
960 break;
961 }
962 return 0;
963}
964
965/*
966 * check if the interface/address pair is valid
967 */
968int ipv6_chk_mcast_addr(struct net_device *dev, struct in6_addr *group,
969 struct in6_addr *src_addr)
970{
971 struct inet6_dev *idev;
972 struct ifmcaddr6 *mc;
973 int rv = 0;
974
975 idev = in6_dev_get(dev);
976 if (idev) {
977 read_lock_bh(&idev->lock);
978 for (mc = idev->mc_list; mc; mc=mc->next) {
979 if (ipv6_addr_equal(&mc->mca_addr, group))
980 break;
981 }
982 if (mc) {
983 if (src_addr && !ipv6_addr_any(src_addr)) {
984 struct ip6_sf_list *psf;
985
986 spin_lock_bh(&mc->mca_lock);
987 for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
988 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
989 break;
990 }
991 if (psf)
992 rv = psf->sf_count[MCAST_INCLUDE] ||
993 psf->sf_count[MCAST_EXCLUDE] !=
994 mc->mca_sfcount[MCAST_EXCLUDE];
995 else
996 rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
997 spin_unlock_bh(&mc->mca_lock);
998 } else
999 rv = 1; /* don't filter unspecified source */
1000 }
1001 read_unlock_bh(&idev->lock);
1002 in6_dev_put(idev);
1003 }
1004 return rv;
1005}
1006
1007static void mld_gq_start_timer(struct inet6_dev *idev)
1008{
1009 int tv = net_random() % idev->mc_maxdelay;
1010
1011 idev->mc_gq_running = 1;
1012 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
1013 in6_dev_hold(idev);
1014}
1015
1016static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
1017{
1018 int tv = net_random() % delay;
1019
1020 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1021 in6_dev_hold(idev);
1022}
1023
1024/*
1025 * IGMP handling (alias multicast ICMPv6 messages)
1026 */
1027
1028static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1029{
1030 unsigned long delay = resptime;
1031
1032 /* Do not start timer for these addresses */
1033 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1034 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1035 return;
1036
1037 if (del_timer(&ma->mca_timer)) {
1038 atomic_dec(&ma->mca_refcnt);
1039 delay = ma->mca_timer.expires - jiffies;
1040 }
1041
1042 if (delay >= resptime) {
1043 if (resptime)
1044 delay = net_random() % resptime;
1045 else
1046 delay = 1;
1047 }
1048 ma->mca_timer.expires = jiffies + delay;
1049 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1050 atomic_inc(&ma->mca_refcnt);
1051 ma->mca_flags |= MAF_TIMER_RUNNING;
1052}
1053
1054static void mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1055 struct in6_addr *srcs)
1056{
1057 struct ip6_sf_list *psf;
1058 int i, scount;
1059
1060 scount = 0;
1061 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1062 if (scount == nsrcs)
1063 break;
1064 for (i=0; i<nsrcs; i++)
1065 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1066 psf->sf_gsresp = 1;
1067 scount++;
1068 break;
1069 }
1070 }
1071}
1072
1073int igmp6_event_query(struct sk_buff *skb)
1074{
1075 struct mld2_query *mlh2 = (struct mld2_query *) skb->h.raw;
1076 struct ifmcaddr6 *ma;
1077 struct in6_addr *group;
1078 unsigned long max_delay;
1079 struct inet6_dev *idev;
1080 struct icmp6hdr *hdr;
1081 int group_type;
1082 int mark = 0;
1083 int len;
1084
1085 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1086 return -EINVAL;
1087
1088 /* compute payload length excluding extension headers */
1089 len = ntohs(skb->nh.ipv6h->payload_len) + sizeof(struct ipv6hdr);
1090 len -= (char *)skb->h.raw - (char *)skb->nh.ipv6h;
1091
1092 /* Drop queries with not link local source */
1093 if (!(ipv6_addr_type(&skb->nh.ipv6h->saddr)&IPV6_ADDR_LINKLOCAL))
1094 return -EINVAL;
1095
1096 idev = in6_dev_get(skb->dev);
1097
1098 if (idev == NULL)
1099 return 0;
1100
1101 hdr = (struct icmp6hdr *) skb->h.raw;
1102 group = (struct in6_addr *) (hdr + 1);
1103 group_type = ipv6_addr_type(group);
1104
1105 if (group_type != IPV6_ADDR_ANY &&
1106 !(group_type&IPV6_ADDR_MULTICAST)) {
1107 in6_dev_put(idev);
1108 return -EINVAL;
1109 }
1110
1111 if (len == 24) {
1112 int switchback;
1113 /* MLDv1 router present */
1114
1115 /* Translate milliseconds to jiffies */
1116 max_delay = (ntohs(hdr->icmp6_maxdelay)*HZ)/1000;
1117
1118 switchback = (idev->mc_qrv + 1) * max_delay;
1119 idev->mc_v1_seen = jiffies + switchback;
1120
1121 /* cancel the interface change timer */
1122 idev->mc_ifc_count = 0;
1123 if (del_timer(&idev->mc_ifc_timer))
1124 __in6_dev_put(idev);
1125 /* clear deleted report items */
1126 mld_clear_delrec(idev);
1127 } else if (len >= 28) {
1128 max_delay = (MLDV2_MRC(ntohs(mlh2->mrc))*HZ)/1000;
1129 if (!max_delay)
1130 max_delay = 1;
1131 idev->mc_maxdelay = max_delay;
1132 if (mlh2->qrv)
1133 idev->mc_qrv = mlh2->qrv;
1134 if (group_type == IPV6_ADDR_ANY) { /* general query */
1135 if (mlh2->nsrcs) {
1136 in6_dev_put(idev);
1137 return -EINVAL; /* no sources allowed */
1138 }
1139 mld_gq_start_timer(idev);
1140 in6_dev_put(idev);
1141 return 0;
1142 }
1143 /* mark sources to include, if group & source-specific */
1144 mark = mlh2->nsrcs != 0;
1145 } else {
1146 in6_dev_put(idev);
1147 return -EINVAL;
1148 }
1149
1150 read_lock_bh(&idev->lock);
1151 if (group_type == IPV6_ADDR_ANY) {
1152 for (ma = idev->mc_list; ma; ma=ma->next) {
1153 spin_lock_bh(&ma->mca_lock);
1154 igmp6_group_queried(ma, max_delay);
1155 spin_unlock_bh(&ma->mca_lock);
1156 }
1157 } else {
1158 for (ma = idev->mc_list; ma; ma=ma->next) {
1159 if (group_type != IPV6_ADDR_ANY &&
1160 !ipv6_addr_equal(group, &ma->mca_addr))
1161 continue;
1162 spin_lock_bh(&ma->mca_lock);
1163 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1164 /* gsquery <- gsquery && mark */
1165 if (!mark)
1166 ma->mca_flags &= ~MAF_GSQUERY;
1167 } else {
1168 /* gsquery <- mark */
1169 if (mark)
1170 ma->mca_flags |= MAF_GSQUERY;
1171 else
1172 ma->mca_flags &= ~MAF_GSQUERY;
1173 }
1174 if (ma->mca_flags & MAF_GSQUERY)
1175 mld_marksources(ma, ntohs(mlh2->nsrcs),
1176 mlh2->srcs);
1177 igmp6_group_queried(ma, max_delay);
1178 spin_unlock_bh(&ma->mca_lock);
1179 if (group_type != IPV6_ADDR_ANY)
1180 break;
1181 }
1182 }
1183 read_unlock_bh(&idev->lock);
1184 in6_dev_put(idev);
1185
1186 return 0;
1187}
1188
1189
1190int igmp6_event_report(struct sk_buff *skb)
1191{
1192 struct ifmcaddr6 *ma;
1193 struct in6_addr *addrp;
1194 struct inet6_dev *idev;
1195 struct icmp6hdr *hdr;
1196 int addr_type;
1197
1198 /* Our own report looped back. Ignore it. */
1199 if (skb->pkt_type == PACKET_LOOPBACK)
1200 return 0;
1201
1202 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1203 return -EINVAL;
1204
1205 hdr = (struct icmp6hdr*) skb->h.raw;
1206
1207 /* Drop reports with not link local source */
1208 addr_type = ipv6_addr_type(&skb->nh.ipv6h->saddr);
1209 if (addr_type != IPV6_ADDR_ANY &&
1210 !(addr_type&IPV6_ADDR_LINKLOCAL))
1211 return -EINVAL;
1212
1213 addrp = (struct in6_addr *) (hdr + 1);
1214
1215 idev = in6_dev_get(skb->dev);
1216 if (idev == NULL)
1217 return -ENODEV;
1218
1219 /*
1220 * Cancel the timer for this group
1221 */
1222
1223 read_lock_bh(&idev->lock);
1224 for (ma = idev->mc_list; ma; ma=ma->next) {
1225 if (ipv6_addr_equal(&ma->mca_addr, addrp)) {
1226 spin_lock(&ma->mca_lock);
1227 if (del_timer(&ma->mca_timer))
1228 atomic_dec(&ma->mca_refcnt);
1229 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1230 spin_unlock(&ma->mca_lock);
1231 break;
1232 }
1233 }
1234 read_unlock_bh(&idev->lock);
1235 in6_dev_put(idev);
1236 return 0;
1237}
1238
1239static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1240 int gdeleted, int sdeleted)
1241{
1242 switch (type) {
1243 case MLD2_MODE_IS_INCLUDE:
1244 case MLD2_MODE_IS_EXCLUDE:
1245 if (gdeleted || sdeleted)
1246 return 0;
1247 return !((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp);
1248 case MLD2_CHANGE_TO_INCLUDE:
1249 if (gdeleted || sdeleted)
1250 return 0;
1251 return psf->sf_count[MCAST_INCLUDE] != 0;
1252 case MLD2_CHANGE_TO_EXCLUDE:
1253 if (gdeleted || sdeleted)
1254 return 0;
1255 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1256 psf->sf_count[MCAST_INCLUDE])
1257 return 0;
1258 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1259 psf->sf_count[MCAST_EXCLUDE];
1260 case MLD2_ALLOW_NEW_SOURCES:
1261 if (gdeleted || !psf->sf_crcount)
1262 return 0;
1263 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1264 case MLD2_BLOCK_OLD_SOURCES:
1265 if (pmc->mca_sfmode == MCAST_INCLUDE)
1266 return gdeleted || (psf->sf_crcount && sdeleted);
1267 return psf->sf_crcount && !gdeleted && !sdeleted;
1268 }
1269 return 0;
1270}
1271
1272static int
1273mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1274{
1275 struct ip6_sf_list *psf;
1276 int scount = 0;
1277
1278 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1279 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1280 continue;
1281 scount++;
1282 }
1283 return scount;
1284}
1285
1286static struct sk_buff *mld_newpack(struct net_device *dev, int size)
1287{
1288 struct sock *sk = igmp6_socket->sk;
1289 struct sk_buff *skb;
1290 struct mld2_report *pmr;
1291 struct in6_addr addr_buf;
1292 int err;
1293 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1294 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1295 IPV6_TLV_PADN, 0 };
1296
1297 /* we assume size > sizeof(ra) here */
1298 skb = sock_alloc_send_skb(sk, size + LL_RESERVED_SPACE(dev), 1, &err);
1299
1300 if (skb == 0)
1301 return NULL;
1302
1303 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1304 if (dev->hard_header) {
1305 unsigned char ha[MAX_ADDR_LEN];
1306
1307 ndisc_mc_map(&mld2_all_mcr, ha, dev, 1);
1308 if (dev->hard_header(skb, dev, ETH_P_IPV6,ha,NULL,size) < 0) {
1309 kfree_skb(skb);
1310 return NULL;
1311 }
1312 }
1313
1314 if (ipv6_get_lladdr(dev, &addr_buf)) {
1315 /* <draft-ietf-magma-mld-source-05.txt>:
1316 * use unspecified address as the source address
1317 * when a valid link-local address is not available.
1318 */
1319 memset(&addr_buf, 0, sizeof(addr_buf));
1320 }
1321
1322 ip6_nd_hdr(sk, skb, dev, &addr_buf, &mld2_all_mcr, NEXTHDR_HOP, 0);
1323
1324 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1325
1326 pmr =(struct mld2_report *)skb_put(skb, sizeof(*pmr));
1327 skb->h.raw = (unsigned char *)pmr;
1328 pmr->type = ICMPV6_MLD2_REPORT;
1329 pmr->resv1 = 0;
1330 pmr->csum = 0;
1331 pmr->resv2 = 0;
1332 pmr->ngrec = 0;
1333 return skb;
1334}
1335
1336static void mld_sendpack(struct sk_buff *skb)
1337{
1338 struct ipv6hdr *pip6 = skb->nh.ipv6h;
1339 struct mld2_report *pmr = (struct mld2_report *)skb->h.raw;
1340 int payload_len, mldlen;
1341 struct inet6_dev *idev = in6_dev_get(skb->dev);
1342 int err;
1343
1344 IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
1345 payload_len = skb->tail - (unsigned char *)skb->nh.ipv6h -
1346 sizeof(struct ipv6hdr);
1347 mldlen = skb->tail - skb->h.raw;
1348 pip6->payload_len = htons(payload_len);
1349
1350 pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1351 IPPROTO_ICMPV6, csum_partial(skb->h.raw, mldlen, 0));
1352 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
1353 dev_queue_xmit);
1354 if (!err) {
1355 ICMP6_INC_STATS(idev,ICMP6_MIB_OUTMSGS);
1356 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
1357 } else
1358 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1359
1360 if (likely(idev != NULL))
1361 in6_dev_put(idev);
1362}
1363
1364static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1365{
1366 return sizeof(struct mld2_grec) + 4*mld_scount(pmc,type,gdel,sdel);
1367}
1368
1369static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1370 int type, struct mld2_grec **ppgr)
1371{
1372 struct net_device *dev = pmc->idev->dev;
1373 struct mld2_report *pmr;
1374 struct mld2_grec *pgr;
1375
1376 if (!skb)
1377 skb = mld_newpack(dev, dev->mtu);
1378 if (!skb)
1379 return NULL;
1380 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1381 pgr->grec_type = type;
1382 pgr->grec_auxwords = 0;
1383 pgr->grec_nsrcs = 0;
1384 pgr->grec_mca = pmc->mca_addr; /* structure copy */
1385 pmr = (struct mld2_report *)skb->h.raw;
1386 pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
1387 *ppgr = pgr;
1388 return skb;
1389}
1390
1391#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1392 skb_tailroom(skb)) : 0)
1393
1394static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1395 int type, int gdeleted, int sdeleted)
1396{
1397 struct net_device *dev = pmc->idev->dev;
1398 struct mld2_report *pmr;
1399 struct mld2_grec *pgr = NULL;
1400 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
1401 int scount, first, isquery, truncate;
1402
1403 if (pmc->mca_flags & MAF_NOREPORT)
1404 return skb;
1405
1406 isquery = type == MLD2_MODE_IS_INCLUDE ||
1407 type == MLD2_MODE_IS_EXCLUDE;
1408 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1409 type == MLD2_CHANGE_TO_EXCLUDE;
1410
1411 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1412
1413 if (!*psf_list) {
1414 if (type == MLD2_ALLOW_NEW_SOURCES ||
1415 type == MLD2_BLOCK_OLD_SOURCES)
1416 return skb;
1417 if (pmc->mca_crcount || isquery) {
1418 /* make sure we have room for group header and at
1419 * least one source.
1420 */
1421 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)+
1422 sizeof(struct in6_addr)) {
1423 mld_sendpack(skb);
1424 skb = NULL; /* add_grhead will get a new one */
1425 }
1426 skb = add_grhead(skb, pmc, type, &pgr);
1427 }
1428 return skb;
1429 }
1430 pmr = skb ? (struct mld2_report *)skb->h.raw : NULL;
1431
1432 /* EX and TO_EX get a fresh packet, if needed */
1433 if (truncate) {
1434 if (pmr && pmr->ngrec &&
1435 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1436 if (skb)
1437 mld_sendpack(skb);
1438 skb = mld_newpack(dev, dev->mtu);
1439 }
1440 }
1441 first = 1;
1442 scount = 0;
1443 psf_prev = NULL;
1444 for (psf=*psf_list; psf; psf=psf_next) {
1445 struct in6_addr *psrc;
1446
1447 psf_next = psf->sf_next;
1448
1449 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1450 psf_prev = psf;
1451 continue;
1452 }
1453
1454 /* clear marks on query responses */
1455 if (isquery)
1456 psf->sf_gsresp = 0;
1457
1458 if (AVAILABLE(skb) < sizeof(*psrc) +
1459 first*sizeof(struct mld2_grec)) {
1460 if (truncate && !first)
1461 break; /* truncate these */
1462 if (pgr)
1463 pgr->grec_nsrcs = htons(scount);
1464 if (skb)
1465 mld_sendpack(skb);
1466 skb = mld_newpack(dev, dev->mtu);
1467 first = 1;
1468 scount = 0;
1469 }
1470 if (first) {
1471 skb = add_grhead(skb, pmc, type, &pgr);
1472 first = 0;
1473 }
1474 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1475 *psrc = psf->sf_addr;
1476 scount++;
1477 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1478 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1479 psf->sf_crcount--;
1480 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1481 if (psf_prev)
1482 psf_prev->sf_next = psf->sf_next;
1483 else
1484 *psf_list = psf->sf_next;
1485 kfree(psf);
1486 continue;
1487 }
1488 }
1489 psf_prev = psf;
1490 }
1491 if (pgr)
1492 pgr->grec_nsrcs = htons(scount);
1493
1494 if (isquery)
1495 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1496 return skb;
1497}
1498
1499static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1500{
1501 struct sk_buff *skb = NULL;
1502 int type;
1503
1504 if (!pmc) {
1505 read_lock_bh(&idev->lock);
1506 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1507 if (pmc->mca_flags & MAF_NOREPORT)
1508 continue;
1509 spin_lock_bh(&pmc->mca_lock);
1510 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1511 type = MLD2_MODE_IS_EXCLUDE;
1512 else
1513 type = MLD2_MODE_IS_INCLUDE;
1514 skb = add_grec(skb, pmc, type, 0, 0);
1515 spin_unlock_bh(&pmc->mca_lock);
1516 }
1517 read_unlock_bh(&idev->lock);
1518 } else {
1519 spin_lock_bh(&pmc->mca_lock);
1520 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1521 type = MLD2_MODE_IS_EXCLUDE;
1522 else
1523 type = MLD2_MODE_IS_INCLUDE;
1524 skb = add_grec(skb, pmc, type, 0, 0);
1525 spin_unlock_bh(&pmc->mca_lock);
1526 }
1527 if (skb)
1528 mld_sendpack(skb);
1529}
1530
1531/*
1532 * remove zero-count source records from a source filter list
1533 */
1534static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1535{
1536 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1537
1538 psf_prev = NULL;
1539 for (psf=*ppsf; psf; psf = psf_next) {
1540 psf_next = psf->sf_next;
1541 if (psf->sf_crcount == 0) {
1542 if (psf_prev)
1543 psf_prev->sf_next = psf->sf_next;
1544 else
1545 *ppsf = psf->sf_next;
1546 kfree(psf);
1547 } else
1548 psf_prev = psf;
1549 }
1550}
1551
1552static void mld_send_cr(struct inet6_dev *idev)
1553{
1554 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1555 struct sk_buff *skb = NULL;
1556 int type, dtype;
1557
1558 read_lock_bh(&idev->lock);
1559 write_lock_bh(&idev->mc_lock);
1560
1561 /* deleted MCA's */
1562 pmc_prev = NULL;
1563 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1564 pmc_next = pmc->next;
1565 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1566 type = MLD2_BLOCK_OLD_SOURCES;
1567 dtype = MLD2_BLOCK_OLD_SOURCES;
1568 skb = add_grec(skb, pmc, type, 1, 0);
1569 skb = add_grec(skb, pmc, dtype, 1, 1);
1570 }
1571 if (pmc->mca_crcount) {
1572 pmc->mca_crcount--;
1573 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1574 type = MLD2_CHANGE_TO_INCLUDE;
1575 skb = add_grec(skb, pmc, type, 1, 0);
1576 }
1577 if (pmc->mca_crcount == 0) {
1578 mld_clear_zeros(&pmc->mca_tomb);
1579 mld_clear_zeros(&pmc->mca_sources);
1580 }
1581 }
1582 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1583 !pmc->mca_sources) {
1584 if (pmc_prev)
1585 pmc_prev->next = pmc_next;
1586 else
1587 idev->mc_tomb = pmc_next;
1588 in6_dev_put(pmc->idev);
1589 kfree(pmc);
1590 } else
1591 pmc_prev = pmc;
1592 }
1593 write_unlock_bh(&idev->mc_lock);
1594
1595 /* change recs */
1596 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1597 spin_lock_bh(&pmc->mca_lock);
1598 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1599 type = MLD2_BLOCK_OLD_SOURCES;
1600 dtype = MLD2_ALLOW_NEW_SOURCES;
1601 } else {
1602 type = MLD2_ALLOW_NEW_SOURCES;
1603 dtype = MLD2_BLOCK_OLD_SOURCES;
1604 }
1605 skb = add_grec(skb, pmc, type, 0, 0);
1606 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1607
1608 /* filter mode changes */
1609 if (pmc->mca_crcount) {
1610 pmc->mca_crcount--;
1611 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1612 type = MLD2_CHANGE_TO_EXCLUDE;
1613 else
1614 type = MLD2_CHANGE_TO_INCLUDE;
1615 skb = add_grec(skb, pmc, type, 0, 0);
1616 }
1617 spin_unlock_bh(&pmc->mca_lock);
1618 }
1619 read_unlock_bh(&idev->lock);
1620 if (!skb)
1621 return;
1622 (void) mld_sendpack(skb);
1623}
1624
1625static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1626{
1627 struct sock *sk = igmp6_socket->sk;
1628 struct inet6_dev *idev;
1629 struct sk_buff *skb;
1630 struct icmp6hdr *hdr;
1631 struct in6_addr *snd_addr;
1632 struct in6_addr *addrp;
1633 struct in6_addr addr_buf;
1634 struct in6_addr all_routers;
1635 int err, len, payload_len, full_len;
1636 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1637 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1638 IPV6_TLV_PADN, 0 };
1639
1640 IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
1641 snd_addr = addr;
1642 if (type == ICMPV6_MGM_REDUCTION) {
1643 snd_addr = &all_routers;
1644 ipv6_addr_all_routers(&all_routers);
1645 }
1646
1647 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1648 payload_len = len + sizeof(ra);
1649 full_len = sizeof(struct ipv6hdr) + payload_len;
1650
1651 skb = sock_alloc_send_skb(sk, LL_RESERVED_SPACE(dev) + full_len, 1, &err);
1652
1653 if (skb == NULL) {
1654 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1655 return;
1656 }
1657
1658 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1659 if (dev->hard_header) {
1660 unsigned char ha[MAX_ADDR_LEN];
1661 ndisc_mc_map(snd_addr, ha, dev, 1);
1662 if (dev->hard_header(skb, dev, ETH_P_IPV6, ha, NULL, full_len) < 0)
1663 goto out;
1664 }
1665
1666 if (ipv6_get_lladdr(dev, &addr_buf)) {
1667 /* <draft-ietf-magma-mld-source-05.txt>:
1668 * use unspecified address as the source address
1669 * when a valid link-local address is not available.
1670 */
1671 memset(&addr_buf, 0, sizeof(addr_buf));
1672 }
1673
1674 ip6_nd_hdr(sk, skb, dev, &addr_buf, snd_addr, NEXTHDR_HOP, payload_len);
1675
1676 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1677
1678 hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
1679 memset(hdr, 0, sizeof(struct icmp6hdr));
1680 hdr->icmp6_type = type;
1681
1682 addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
1683 ipv6_addr_copy(addrp, addr);
1684
1685 hdr->icmp6_cksum = csum_ipv6_magic(&addr_buf, snd_addr, len,
1686 IPPROTO_ICMPV6,
1687 csum_partial((__u8 *) hdr, len, 0));
1688
1689 idev = in6_dev_get(skb->dev);
1690
1691 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
1692 dev_queue_xmit);
1693 if (!err) {
1694 if (type == ICMPV6_MGM_REDUCTION)
1695 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBREDUCTIONS);
1696 else
1697 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBRESPONSES);
1698 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTMSGS);
1699 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
1700 } else
1701 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1702
1703 if (likely(idev != NULL))
1704 in6_dev_put(idev);
1705 return;
1706
1707out:
1708 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1709 kfree_skb(skb);
1710}
1711
1712static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1713 struct in6_addr *psfsrc)
1714{
1715 struct ip6_sf_list *psf, *psf_prev;
1716 int rv = 0;
1717
1718 psf_prev = NULL;
1719 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1720 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1721 break;
1722 psf_prev = psf;
1723 }
1724 if (!psf || psf->sf_count[sfmode] == 0) {
1725 /* source filter not found, or count wrong => bug */
1726 return -ESRCH;
1727 }
1728 psf->sf_count[sfmode]--;
1729 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1730 struct inet6_dev *idev = pmc->idev;
1731
1732 /* no more filters for this source */
1733 if (psf_prev)
1734 psf_prev->sf_next = psf->sf_next;
1735 else
1736 pmc->mca_sources = psf->sf_next;
1737 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1738 !MLD_V1_SEEN(idev)) {
1739 psf->sf_crcount = idev->mc_qrv;
1740 psf->sf_next = pmc->mca_tomb;
1741 pmc->mca_tomb = psf;
1742 rv = 1;
1743 } else
1744 kfree(psf);
1745 }
1746 return rv;
1747}
1748
1749static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
1750 int sfmode, int sfcount, struct in6_addr *psfsrc,
1751 int delta)
1752{
1753 struct ifmcaddr6 *pmc;
1754 int changerec = 0;
1755 int i, err;
1756
1757 if (!idev)
1758 return -ENODEV;
1759 read_lock_bh(&idev->lock);
1760 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1761 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1762 break;
1763 }
1764 if (!pmc) {
1765 /* MCA not found?? bug */
1766 read_unlock_bh(&idev->lock);
1767 return -ESRCH;
1768 }
1769 spin_lock_bh(&pmc->mca_lock);
1770 sf_markstate(pmc);
1771 if (!delta) {
1772 if (!pmc->mca_sfcount[sfmode]) {
1773 spin_unlock_bh(&pmc->mca_lock);
1774 read_unlock_bh(&idev->lock);
1775 return -EINVAL;
1776 }
1777 pmc->mca_sfcount[sfmode]--;
1778 }
1779 err = 0;
1780 for (i=0; i<sfcount; i++) {
1781 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1782
1783 changerec |= rv > 0;
1784 if (!err && rv < 0)
1785 err = rv;
1786 }
1787 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1788 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1789 pmc->mca_sfcount[MCAST_INCLUDE]) {
1790 struct ip6_sf_list *psf;
1791
1792 /* filter mode change */
1793 pmc->mca_sfmode = MCAST_INCLUDE;
1794 pmc->mca_crcount = idev->mc_qrv;
1795 idev->mc_ifc_count = pmc->mca_crcount;
1796 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1797 psf->sf_crcount = 0;
1798 mld_ifc_event(pmc->idev);
1799 } else if (sf_setstate(pmc) || changerec)
1800 mld_ifc_event(pmc->idev);
1801 spin_unlock_bh(&pmc->mca_lock);
1802 read_unlock_bh(&idev->lock);
1803 return err;
1804}
1805
1806/*
1807 * Add multicast single-source filter to the interface list
1808 */
1809static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1810 struct in6_addr *psfsrc, int delta)
1811{
1812 struct ip6_sf_list *psf, *psf_prev;
1813
1814 psf_prev = NULL;
1815 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1816 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1817 break;
1818 psf_prev = psf;
1819 }
1820 if (!psf) {
1821 psf = (struct ip6_sf_list *)kmalloc(sizeof(*psf), GFP_ATOMIC);
1822 if (!psf)
1823 return -ENOBUFS;
1824 memset(psf, 0, sizeof(*psf));
1825 psf->sf_addr = *psfsrc;
1826 if (psf_prev) {
1827 psf_prev->sf_next = psf;
1828 } else
1829 pmc->mca_sources = psf;
1830 }
1831 psf->sf_count[sfmode]++;
1832 return 0;
1833}
1834
1835static void sf_markstate(struct ifmcaddr6 *pmc)
1836{
1837 struct ip6_sf_list *psf;
1838 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1839
1840 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1841 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1842 psf->sf_oldin = mca_xcount ==
1843 psf->sf_count[MCAST_EXCLUDE] &&
1844 !psf->sf_count[MCAST_INCLUDE];
1845 } else
1846 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1847}
1848
1849static int sf_setstate(struct ifmcaddr6 *pmc)
1850{
1851 struct ip6_sf_list *psf;
1852 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1853 int qrv = pmc->idev->mc_qrv;
1854 int new_in, rv;
1855
1856 rv = 0;
1857 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1858 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1859 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1860 !psf->sf_count[MCAST_INCLUDE];
1861 } else
1862 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1863 if (new_in != psf->sf_oldin) {
1864 psf->sf_crcount = qrv;
1865 rv++;
1866 }
1867 }
1868 return rv;
1869}
1870
1871/*
1872 * Add multicast source filter list to the interface list
1873 */
1874static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
1875 int sfmode, int sfcount, struct in6_addr *psfsrc,
1876 int delta)
1877{
1878 struct ifmcaddr6 *pmc;
1879 int isexclude;
1880 int i, err;
1881
1882 if (!idev)
1883 return -ENODEV;
1884 read_lock_bh(&idev->lock);
1885 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1886 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1887 break;
1888 }
1889 if (!pmc) {
1890 /* MCA not found?? bug */
1891 read_unlock_bh(&idev->lock);
1892 return -ESRCH;
1893 }
1894 spin_lock_bh(&pmc->mca_lock);
1895
1896 sf_markstate(pmc);
1897 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
1898 if (!delta)
1899 pmc->mca_sfcount[sfmode]++;
1900 err = 0;
1901 for (i=0; i<sfcount; i++) {
1902 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1903 if (err)
1904 break;
1905 }
1906 if (err) {
1907 int j;
1908
1909 if (!delta)
1910 pmc->mca_sfcount[sfmode]--;
1911 for (j=0; j<i; j++)
1912 (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1913 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
1914 struct inet6_dev *idev = pmc->idev;
1915 struct ip6_sf_list *psf;
1916
1917 /* filter mode change */
1918 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1919 pmc->mca_sfmode = MCAST_EXCLUDE;
1920 else if (pmc->mca_sfcount[MCAST_INCLUDE])
1921 pmc->mca_sfmode = MCAST_INCLUDE;
1922 /* else no filters; keep old mode for reports */
1923
1924 pmc->mca_crcount = idev->mc_qrv;
1925 idev->mc_ifc_count = pmc->mca_crcount;
1926 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1927 psf->sf_crcount = 0;
1928 mld_ifc_event(idev);
1929 } else if (sf_setstate(pmc))
1930 mld_ifc_event(idev);
1931 spin_unlock_bh(&pmc->mca_lock);
1932 read_unlock_bh(&idev->lock);
1933 return err;
1934}
1935
1936static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
1937{
1938 struct ip6_sf_list *psf, *nextpsf;
1939
1940 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
1941 nextpsf = psf->sf_next;
1942 kfree(psf);
1943 }
1944 pmc->mca_tomb = NULL;
1945 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
1946 nextpsf = psf->sf_next;
1947 kfree(psf);
1948 }
1949 pmc->mca_sources = NULL;
1950 pmc->mca_sfmode = MCAST_EXCLUDE;
1951 pmc->mca_sfcount[MCAST_EXCLUDE] = 0;
1952 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
1953}
1954
1955
1956static void igmp6_join_group(struct ifmcaddr6 *ma)
1957{
1958 unsigned long delay;
1959
1960 if (ma->mca_flags & MAF_NOREPORT)
1961 return;
1962
1963 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
1964
1965 delay = net_random() % IGMP6_UNSOLICITED_IVAL;
1966
1967 spin_lock_bh(&ma->mca_lock);
1968 if (del_timer(&ma->mca_timer)) {
1969 atomic_dec(&ma->mca_refcnt);
1970 delay = ma->mca_timer.expires - jiffies;
1971 }
1972
1973 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1974 atomic_inc(&ma->mca_refcnt);
1975 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
1976 spin_unlock_bh(&ma->mca_lock);
1977}
1978
1979static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
1980 struct inet6_dev *idev)
1981{
1982 int err;
1983
1984 if (iml->sflist == 0) {
1985 /* any-source empty exclude case */
1986 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
1987 }
1988 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
1989 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
1990 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
1991 iml->sflist = NULL;
1992 return err;
1993}
1994
1995static void igmp6_leave_group(struct ifmcaddr6 *ma)
1996{
1997 if (MLD_V1_SEEN(ma->idev)) {
1998 if (ma->mca_flags & MAF_LAST_REPORTER)
1999 igmp6_send(&ma->mca_addr, ma->idev->dev,
2000 ICMPV6_MGM_REDUCTION);
2001 } else {
2002 mld_add_delrec(ma->idev, ma);
2003 mld_ifc_event(ma->idev);
2004 }
2005}
2006
2007static void mld_gq_timer_expire(unsigned long data)
2008{
2009 struct inet6_dev *idev = (struct inet6_dev *)data;
2010
2011 idev->mc_gq_running = 0;
2012 mld_send_report(idev, NULL);
2013 __in6_dev_put(idev);
2014}
2015
2016static void mld_ifc_timer_expire(unsigned long data)
2017{
2018 struct inet6_dev *idev = (struct inet6_dev *)data;
2019
2020 mld_send_cr(idev);
2021 if (idev->mc_ifc_count) {
2022 idev->mc_ifc_count--;
2023 if (idev->mc_ifc_count)
2024 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2025 }
2026 __in6_dev_put(idev);
2027}
2028
2029static void mld_ifc_event(struct inet6_dev *idev)
2030{
2031 if (MLD_V1_SEEN(idev))
2032 return;
2033 idev->mc_ifc_count = idev->mc_qrv;
2034 mld_ifc_start_timer(idev, 1);
2035}
2036
2037
2038static void igmp6_timer_handler(unsigned long data)
2039{
2040 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2041
2042 if (MLD_V1_SEEN(ma->idev))
2043 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2044 else
2045 mld_send_report(ma->idev, ma);
2046
2047 spin_lock(&ma->mca_lock);
2048 ma->mca_flags |= MAF_LAST_REPORTER;
2049 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2050 spin_unlock(&ma->mca_lock);
2051 ma_put(ma);
2052}
2053
2054/* Device going down */
2055
2056void ipv6_mc_down(struct inet6_dev *idev)
2057{
2058 struct ifmcaddr6 *i;
2059
2060 /* Withdraw multicast list */
2061
2062 read_lock_bh(&idev->lock);
2063 idev->mc_ifc_count = 0;
2064 if (del_timer(&idev->mc_ifc_timer))
2065 __in6_dev_put(idev);
2066 idev->mc_gq_running = 0;
2067 if (del_timer(&idev->mc_gq_timer))
2068 __in6_dev_put(idev);
2069
2070 for (i = idev->mc_list; i; i=i->next)
2071 igmp6_group_dropped(i);
2072 read_unlock_bh(&idev->lock);
2073
2074 mld_clear_delrec(idev);
2075}
2076
2077
2078/* Device going up */
2079
2080void ipv6_mc_up(struct inet6_dev *idev)
2081{
2082 struct ifmcaddr6 *i;
2083
2084 /* Install multicast list, except for all-nodes (already installed) */
2085
2086 read_lock_bh(&idev->lock);
2087 for (i = idev->mc_list; i; i=i->next)
2088 igmp6_group_added(i);
2089 read_unlock_bh(&idev->lock);
2090}
2091
2092/* IPv6 device initialization. */
2093
2094void ipv6_mc_init_dev(struct inet6_dev *idev)
2095{
2096 struct in6_addr maddr;
2097
2098 write_lock_bh(&idev->lock);
2099 rwlock_init(&idev->mc_lock);
2100 idev->mc_gq_running = 0;
2101 init_timer(&idev->mc_gq_timer);
2102 idev->mc_gq_timer.data = (unsigned long) idev;
2103 idev->mc_gq_timer.function = &mld_gq_timer_expire;
2104 idev->mc_tomb = NULL;
2105 idev->mc_ifc_count = 0;
2106 init_timer(&idev->mc_ifc_timer);
2107 idev->mc_ifc_timer.data = (unsigned long) idev;
2108 idev->mc_ifc_timer.function = &mld_ifc_timer_expire;
2109 idev->mc_qrv = MLD_QRV_DEFAULT;
2110 idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2111 idev->mc_v1_seen = 0;
2112 write_unlock_bh(&idev->lock);
2113
2114 /* Add all-nodes address. */
2115 ipv6_addr_all_nodes(&maddr);
2116 ipv6_dev_mc_inc(idev->dev, &maddr);
2117}
2118
2119/*
2120 * Device is about to be destroyed: clean up.
2121 */
2122
2123void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2124{
2125 struct ifmcaddr6 *i;
2126 struct in6_addr maddr;
2127
2128 /* Deactivate timers */
2129 ipv6_mc_down(idev);
2130
2131 /* Delete all-nodes address. */
2132 ipv6_addr_all_nodes(&maddr);
2133
2134 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2135 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2136 * fail.
2137 */
2138 __ipv6_dev_mc_dec(idev, &maddr);
2139
2140 if (idev->cnf.forwarding) {
2141 ipv6_addr_all_routers(&maddr);
2142 __ipv6_dev_mc_dec(idev, &maddr);
2143 }
2144
2145 write_lock_bh(&idev->lock);
2146 while ((i = idev->mc_list) != NULL) {
2147 idev->mc_list = i->next;
2148 write_unlock_bh(&idev->lock);
2149
2150 igmp6_group_dropped(i);
2151 ma_put(i);
2152
2153 write_lock_bh(&idev->lock);
2154 }
2155 write_unlock_bh(&idev->lock);
2156}
2157
2158#ifdef CONFIG_PROC_FS
2159struct igmp6_mc_iter_state {
2160 struct net_device *dev;
2161 struct inet6_dev *idev;
2162};
2163
2164#define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2165
2166static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2167{
2168 struct ifmcaddr6 *im = NULL;
2169 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2170
2171 for (state->dev = dev_base, state->idev = NULL;
2172 state->dev;
2173 state->dev = state->dev->next) {
2174 struct inet6_dev *idev;
2175 idev = in6_dev_get(state->dev);
2176 if (!idev)
2177 continue;
2178 read_lock_bh(&idev->lock);
2179 im = idev->mc_list;
2180 if (im) {
2181 state->idev = idev;
2182 break;
2183 }
2184 read_unlock_bh(&idev->lock);
2185 in6_dev_put(idev);
2186 }
2187 return im;
2188}
2189
2190static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2191{
2192 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2193
2194 im = im->next;
2195 while (!im) {
2196 if (likely(state->idev != NULL)) {
2197 read_unlock_bh(&state->idev->lock);
2198 in6_dev_put(state->idev);
2199 }
2200 state->dev = state->dev->next;
2201 if (!state->dev) {
2202 state->idev = NULL;
2203 break;
2204 }
2205 state->idev = in6_dev_get(state->dev);
2206 if (!state->idev)
2207 continue;
2208 read_lock_bh(&state->idev->lock);
2209 im = state->idev->mc_list;
2210 }
2211 return im;
2212}
2213
2214static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2215{
2216 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2217 if (im)
2218 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2219 --pos;
2220 return pos ? NULL : im;
2221}
2222
2223static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2224{
2225 read_lock(&dev_base_lock);
2226 return igmp6_mc_get_idx(seq, *pos);
2227}
2228
2229static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2230{
2231 struct ifmcaddr6 *im;
2232 im = igmp6_mc_get_next(seq, v);
2233 ++*pos;
2234 return im;
2235}
2236
2237static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2238{
2239 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2240 if (likely(state->idev != NULL)) {
2241 read_unlock_bh(&state->idev->lock);
2242 in6_dev_put(state->idev);
2243 state->idev = NULL;
2244 }
2245 state->dev = NULL;
2246 read_unlock(&dev_base_lock);
2247}
2248
2249static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2250{
2251 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2252 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2253
2254 seq_printf(seq,
2255 "%-4d %-15s %04x%04x%04x%04x%04x%04x%04x%04x %5d %08X %ld\n",
2256 state->dev->ifindex, state->dev->name,
2257 NIP6(im->mca_addr),
2258 im->mca_users, im->mca_flags,
2259 (im->mca_flags&MAF_TIMER_RUNNING) ?
2260 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2261 return 0;
2262}
2263
2264static struct seq_operations igmp6_mc_seq_ops = {
2265 .start = igmp6_mc_seq_start,
2266 .next = igmp6_mc_seq_next,
2267 .stop = igmp6_mc_seq_stop,
2268 .show = igmp6_mc_seq_show,
2269};
2270
2271static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2272{
2273 struct seq_file *seq;
2274 int rc = -ENOMEM;
2275 struct igmp6_mc_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2276
2277 if (!s)
2278 goto out;
2279
2280 rc = seq_open(file, &igmp6_mc_seq_ops);
2281 if (rc)
2282 goto out_kfree;
2283
2284 seq = file->private_data;
2285 seq->private = s;
2286 memset(s, 0, sizeof(*s));
2287out:
2288 return rc;
2289out_kfree:
2290 kfree(s);
2291 goto out;
2292}
2293
2294static struct file_operations igmp6_mc_seq_fops = {
2295 .owner = THIS_MODULE,
2296 .open = igmp6_mc_seq_open,
2297 .read = seq_read,
2298 .llseek = seq_lseek,
2299 .release = seq_release_private,
2300};
2301
2302struct igmp6_mcf_iter_state {
2303 struct net_device *dev;
2304 struct inet6_dev *idev;
2305 struct ifmcaddr6 *im;
2306};
2307
2308#define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2309
2310static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2311{
2312 struct ip6_sf_list *psf = NULL;
2313 struct ifmcaddr6 *im = NULL;
2314 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2315
2316 for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
2317 state->dev;
2318 state->dev = state->dev->next) {
2319 struct inet6_dev *idev;
2320 idev = in6_dev_get(state->dev);
2321 if (unlikely(idev == NULL))
2322 continue;
2323 read_lock_bh(&idev->lock);
2324 im = idev->mc_list;
2325 if (likely(im != NULL)) {
2326 spin_lock_bh(&im->mca_lock);
2327 psf = im->mca_sources;
2328 if (likely(psf != NULL)) {
2329 state->im = im;
2330 state->idev = idev;
2331 break;
2332 }
2333 spin_unlock_bh(&im->mca_lock);
2334 }
2335 read_unlock_bh(&idev->lock);
2336 in6_dev_put(idev);
2337 }
2338 return psf;
2339}
2340
2341static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2342{
2343 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2344
2345 psf = psf->sf_next;
2346 while (!psf) {
2347 spin_unlock_bh(&state->im->mca_lock);
2348 state->im = state->im->next;
2349 while (!state->im) {
2350 if (likely(state->idev != NULL)) {
2351 read_unlock_bh(&state->idev->lock);
2352 in6_dev_put(state->idev);
2353 }
2354 state->dev = state->dev->next;
2355 if (!state->dev) {
2356 state->idev = NULL;
2357 goto out;
2358 }
2359 state->idev = in6_dev_get(state->dev);
2360 if (!state->idev)
2361 continue;
2362 read_lock_bh(&state->idev->lock);
2363 state->im = state->idev->mc_list;
2364 }
2365 if (!state->im)
2366 break;
2367 spin_lock_bh(&state->im->mca_lock);
2368 psf = state->im->mca_sources;
2369 }
2370out:
2371 return psf;
2372}
2373
2374static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2375{
2376 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2377 if (psf)
2378 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2379 --pos;
2380 return pos ? NULL : psf;
2381}
2382
2383static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2384{
2385 read_lock(&dev_base_lock);
2386 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2387}
2388
2389static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2390{
2391 struct ip6_sf_list *psf;
2392 if (v == SEQ_START_TOKEN)
2393 psf = igmp6_mcf_get_first(seq);
2394 else
2395 psf = igmp6_mcf_get_next(seq, v);
2396 ++*pos;
2397 return psf;
2398}
2399
2400static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2401{
2402 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2403 if (likely(state->im != NULL)) {
2404 spin_unlock_bh(&state->im->mca_lock);
2405 state->im = NULL;
2406 }
2407 if (likely(state->idev != NULL)) {
2408 read_unlock_bh(&state->idev->lock);
2409 in6_dev_put(state->idev);
2410 state->idev = NULL;
2411 }
2412 state->dev = NULL;
2413 read_unlock(&dev_base_lock);
2414}
2415
2416static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2417{
2418 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2419 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2420
2421 if (v == SEQ_START_TOKEN) {
2422 seq_printf(seq,
2423 "%3s %6s "
2424 "%32s %32s %6s %6s\n", "Idx",
2425 "Device", "Multicast Address",
2426 "Source Address", "INC", "EXC");
2427 } else {
2428 seq_printf(seq,
2429 "%3d %6.6s "
2430 "%04x%04x%04x%04x%04x%04x%04x%04x "
2431 "%04x%04x%04x%04x%04x%04x%04x%04x "
2432 "%6lu %6lu\n",
2433 state->dev->ifindex, state->dev->name,
2434 NIP6(state->im->mca_addr),
2435 NIP6(psf->sf_addr),
2436 psf->sf_count[MCAST_INCLUDE],
2437 psf->sf_count[MCAST_EXCLUDE]);
2438 }
2439 return 0;
2440}
2441
2442static struct seq_operations igmp6_mcf_seq_ops = {
2443 .start = igmp6_mcf_seq_start,
2444 .next = igmp6_mcf_seq_next,
2445 .stop = igmp6_mcf_seq_stop,
2446 .show = igmp6_mcf_seq_show,
2447};
2448
2449static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2450{
2451 struct seq_file *seq;
2452 int rc = -ENOMEM;
2453 struct igmp6_mcf_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2454
2455 if (!s)
2456 goto out;
2457
2458 rc = seq_open(file, &igmp6_mcf_seq_ops);
2459 if (rc)
2460 goto out_kfree;
2461
2462 seq = file->private_data;
2463 seq->private = s;
2464 memset(s, 0, sizeof(*s));
2465out:
2466 return rc;
2467out_kfree:
2468 kfree(s);
2469 goto out;
2470}
2471
2472static struct file_operations igmp6_mcf_seq_fops = {
2473 .owner = THIS_MODULE,
2474 .open = igmp6_mcf_seq_open,
2475 .read = seq_read,
2476 .llseek = seq_lseek,
2477 .release = seq_release_private,
2478};
2479#endif
2480
2481int __init igmp6_init(struct net_proto_family *ops)
2482{
2483 struct ipv6_pinfo *np;
2484 struct sock *sk;
2485 int err;
2486
2487 err = sock_create_kern(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6, &igmp6_socket);
2488 if (err < 0) {
2489 printk(KERN_ERR
2490 "Failed to initialize the IGMP6 control socket (err %d).\n",
2491 err);
2492 igmp6_socket = NULL; /* For safety. */
2493 return err;
2494 }
2495
2496 sk = igmp6_socket->sk;
2497 sk->sk_allocation = GFP_ATOMIC;
2498 sk->sk_prot->unhash(sk);
2499
2500 np = inet6_sk(sk);
2501 np->hop_limit = 1;
2502
2503#ifdef CONFIG_PROC_FS
2504 proc_net_fops_create("igmp6", S_IRUGO, &igmp6_mc_seq_fops);
2505 proc_net_fops_create("mcfilter6", S_IRUGO, &igmp6_mcf_seq_fops);
2506#endif
2507
2508 return 0;
2509}
2510
2511void igmp6_cleanup(void)
2512{
2513 sock_release(igmp6_socket);
2514 igmp6_socket = NULL; /* for safety */
2515
2516#ifdef CONFIG_PROC_FS
2517 proc_net_remove("mcfilter6");
2518 proc_net_remove("igmp6");
2519#endif
2520}