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