blob: 6e871afbb2c7154bbb72723ade6a212773b00440 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Multicast support for IPv6
3 * Linux INET6 implementation
4 *
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 *
8 * $Id: mcast.c,v 1.40 2002/02/08 03:57:19 davem Exp $
9 *
10 * Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 */
17
18/* Changes:
19 *
20 * yoshfuji : fix format of router-alert option
21 * YOSHIFUJI Hideaki @USAGI:
22 * Fixed source address for MLD message based on
23 * <draft-ietf-magma-mld-source-05.txt>.
24 * YOSHIFUJI Hideaki @USAGI:
25 * - Ignore Queries for invalid addresses.
26 * - MLD for link-local addresses.
27 * David L Stevens <dlstevens@us.ibm.com>:
28 * - MLDv2 support
29 */
30
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) : \
Yan Zhengf12baea2005-10-29 00:02:32 +0800167 ((MLDV2_MASK(value, nbmant) | (1<<(nbmant))) << \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 (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
David L Stevens6f4353d2005-12-26 17:03:46 -0800173#define IPV6_MLD_MAX_MSF 64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
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
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700191 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 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;
YOSHIFUJI Hideaki196433c2006-01-04 13:56:31 -0800227 rwlock_init(&mc_lst->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 mc_lst->sflist = NULL;
229
230 /*
231 * now add/increase the group membership on the device
232 */
233
234 err = ipv6_dev_mc_inc(dev, addr);
235
236 if (err) {
237 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
238 dev_put(dev);
239 return err;
240 }
241
242 write_lock_bh(&ipv6_sk_mc_lock);
243 mc_lst->next = np->ipv6_mc_list;
244 np->ipv6_mc_list = mc_lst;
245 write_unlock_bh(&ipv6_sk_mc_lock);
246
247 dev_put(dev);
248
249 return 0;
250}
251
252/*
253 * socket leave on multicast group
254 */
255int ipv6_sock_mc_drop(struct sock *sk, int ifindex, struct in6_addr *addr)
256{
257 struct ipv6_pinfo *np = inet6_sk(sk);
258 struct ipv6_mc_socklist *mc_lst, **lnk;
259
260 write_lock_bh(&ipv6_sk_mc_lock);
261 for (lnk = &np->ipv6_mc_list; (mc_lst = *lnk) !=NULL ; lnk = &mc_lst->next) {
262 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
263 ipv6_addr_equal(&mc_lst->addr, addr)) {
264 struct net_device *dev;
265
266 *lnk = mc_lst->next;
267 write_unlock_bh(&ipv6_sk_mc_lock);
268
269 if ((dev = dev_get_by_index(mc_lst->ifindex)) != NULL) {
270 struct inet6_dev *idev = in6_dev_get(dev);
271
272 if (idev) {
273 (void) ip6_mc_leave_src(sk,mc_lst,idev);
274 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
275 in6_dev_put(idev);
276 }
277 dev_put(dev);
278 }
279 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
280 return 0;
281 }
282 }
283 write_unlock_bh(&ipv6_sk_mc_lock);
284
David L Stevens9951f032005-07-08 17:47:28 -0700285 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288static struct inet6_dev *ip6_mc_find_dev(struct in6_addr *group, int ifindex)
289{
290 struct net_device *dev = NULL;
291 struct inet6_dev *idev = NULL;
292
293 if (ifindex == 0) {
294 struct rt6_info *rt;
295
296 rt = rt6_lookup(group, NULL, 0, 0);
297 if (rt) {
298 dev = rt->rt6i_dev;
299 dev_hold(dev);
300 dst_release(&rt->u.dst);
301 }
302 } else
303 dev = dev_get_by_index(ifindex);
304
305 if (!dev)
306 return NULL;
307 idev = in6_dev_get(dev);
308 if (!idev) {
309 dev_put(dev);
310 return NULL;
311 }
312 read_lock_bh(&idev->lock);
313 if (idev->dead) {
314 read_unlock_bh(&idev->lock);
315 in6_dev_put(idev);
316 dev_put(dev);
317 return NULL;
318 }
319 return idev;
320}
321
322void ipv6_sock_mc_close(struct sock *sk)
323{
324 struct ipv6_pinfo *np = inet6_sk(sk);
325 struct ipv6_mc_socklist *mc_lst;
326
327 write_lock_bh(&ipv6_sk_mc_lock);
328 while ((mc_lst = np->ipv6_mc_list) != NULL) {
329 struct net_device *dev;
330
331 np->ipv6_mc_list = mc_lst->next;
332 write_unlock_bh(&ipv6_sk_mc_lock);
333
334 dev = dev_get_by_index(mc_lst->ifindex);
335 if (dev) {
336 struct inet6_dev *idev = in6_dev_get(dev);
337
338 if (idev) {
339 (void) ip6_mc_leave_src(sk, mc_lst, idev);
340 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
341 in6_dev_put(idev);
342 }
343 dev_put(dev);
344 }
345
346 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
347
348 write_lock_bh(&ipv6_sk_mc_lock);
349 }
350 write_unlock_bh(&ipv6_sk_mc_lock);
351}
352
353int ip6_mc_source(int add, int omode, struct sock *sk,
354 struct group_source_req *pgsr)
355{
356 struct in6_addr *source, *group;
357 struct ipv6_mc_socklist *pmc;
358 struct net_device *dev;
359 struct inet6_dev *idev;
360 struct ipv6_pinfo *inet6 = inet6_sk(sk);
361 struct ip6_sf_socklist *psl;
362 int i, j, rv;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700363 int leavegroup = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800364 int pmclocked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 int err;
366
367 if (pgsr->gsr_group.ss_family != AF_INET6 ||
368 pgsr->gsr_source.ss_family != AF_INET6)
369 return -EINVAL;
370
371 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
372 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
373
374 if (!ipv6_addr_is_multicast(group))
375 return -EINVAL;
376
377 idev = ip6_mc_find_dev(group, pgsr->gsr_interface);
378 if (!idev)
379 return -ENODEV;
380 dev = idev->dev;
381
382 err = -EADDRNOTAVAIL;
383
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700384 read_lock_bh(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
386 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
387 continue;
388 if (ipv6_addr_equal(&pmc->addr, group))
389 break;
390 }
David L Stevens917f2f12005-07-08 17:45:16 -0700391 if (!pmc) { /* must have a prior join */
392 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /* if a source filter was set, must be the same mode as before */
396 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -0700397 if (pmc->sfmode != omode) {
398 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 } else if (pmc->sfmode != omode) {
402 /* allow mode switches for empty-set filters */
403 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
404 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
405 pmc->sfmode = omode;
406 }
407
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800408 write_lock_bh(&pmc->sflock);
409 pmclocked = 1;
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 psl = pmc->sflist;
412 if (!add) {
413 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -0700414 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 rv = !0;
416 for (i=0; i<psl->sl_count; i++) {
417 rv = memcmp(&psl->sl_addr[i], source,
418 sizeof(struct in6_addr));
419 if (rv == 0)
420 break;
421 }
422 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -0700423 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700425 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
426 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
427 leavegroup = 1;
428 goto done;
429 }
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /* update the interface filter */
432 ip6_mc_del_src(idev, group, omode, 1, source, 1);
433
434 for (j=i+1; j<psl->sl_count; j++)
435 psl->sl_addr[j-1] = psl->sl_addr[j];
436 psl->sl_count--;
437 err = 0;
438 goto done;
439 }
440 /* else, add a new source to the filter */
441
442 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
443 err = -ENOBUFS;
444 goto done;
445 }
446 if (!psl || psl->sl_count == psl->sl_max) {
447 struct ip6_sf_socklist *newpsl;
448 int count = IP6_SFBLOCK;
449
450 if (psl)
451 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800452 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (!newpsl) {
454 err = -ENOBUFS;
455 goto done;
456 }
457 newpsl->sl_max = count;
458 newpsl->sl_count = count - IP6_SFBLOCK;
459 if (psl) {
460 for (i=0; i<psl->sl_count; i++)
461 newpsl->sl_addr[i] = psl->sl_addr[i];
462 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
463 }
464 pmc->sflist = psl = newpsl;
465 }
466 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
467 for (i=0; i<psl->sl_count; i++) {
468 rv = memcmp(&psl->sl_addr[i], source, sizeof(struct in6_addr));
469 if (rv == 0)
470 break;
471 }
472 if (rv == 0) /* address already there is an error */
473 goto done;
474 for (j=psl->sl_count-1; j>=i; j--)
475 psl->sl_addr[j+1] = psl->sl_addr[j];
476 psl->sl_addr[i] = *source;
477 psl->sl_count++;
478 err = 0;
479 /* update the interface list */
480 ip6_mc_add_src(idev, group, omode, 1, source, 1);
481done:
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800482 if (pmclocked)
483 write_unlock_bh(&pmc->sflock);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700484 read_unlock_bh(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 read_unlock_bh(&idev->lock);
486 in6_dev_put(idev);
487 dev_put(dev);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700488 if (leavegroup)
489 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return err;
491}
492
493int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
494{
495 struct in6_addr *group;
496 struct ipv6_mc_socklist *pmc;
497 struct net_device *dev;
498 struct inet6_dev *idev;
499 struct ipv6_pinfo *inet6 = inet6_sk(sk);
500 struct ip6_sf_socklist *newpsl, *psl;
David L Stevens9951f032005-07-08 17:47:28 -0700501 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 int i, err;
503
504 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
505
506 if (!ipv6_addr_is_multicast(group))
507 return -EINVAL;
508 if (gsf->gf_fmode != MCAST_INCLUDE &&
509 gsf->gf_fmode != MCAST_EXCLUDE)
510 return -EINVAL;
511
512 idev = ip6_mc_find_dev(group, gsf->gf_interface);
513
514 if (!idev)
515 return -ENODEV;
516 dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
David S. Miller9c059892005-07-08 21:44:39 -0700518 err = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800519 read_lock_bh(&ipv6_sk_mc_lock);
520
David L Stevens9951f032005-07-08 17:47:28 -0700521 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
522 leavegroup = 1;
523 goto done;
524 }
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
527 if (pmc->ifindex != gsf->gf_interface)
528 continue;
529 if (ipv6_addr_equal(&pmc->addr, group))
530 break;
531 }
David L Stevens917f2f12005-07-08 17:45:16 -0700532 if (!pmc) { /* must have a prior join */
533 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (gsf->gf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800537 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
538 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (!newpsl) {
540 err = -ENOBUFS;
541 goto done;
542 }
543 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
544 for (i=0; i<newpsl->sl_count; ++i) {
545 struct sockaddr_in6 *psin6;
546
547 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
548 newpsl->sl_addr[i] = psin6->sin6_addr;
549 }
550 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
551 newpsl->sl_count, newpsl->sl_addr, 0);
552 if (err) {
553 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
554 goto done;
555 }
Yan Zheng8713dbf2005-10-28 08:02:08 +0800556 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +0800558 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
559 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800560
561 write_lock_bh(&pmc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 psl = pmc->sflist;
563 if (psl) {
564 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
565 psl->sl_count, psl->sl_addr, 0);
566 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
567 } else
568 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
569 pmc->sflist = newpsl;
570 pmc->sfmode = gsf->gf_fmode;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800571 write_unlock_bh(&pmc->sflock);
David L Stevens917f2f12005-07-08 17:45:16 -0700572 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573done:
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800574 read_unlock_bh(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 read_unlock_bh(&idev->lock);
576 in6_dev_put(idev);
577 dev_put(dev);
David L Stevens9951f032005-07-08 17:47:28 -0700578 if (leavegroup)
579 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return err;
581}
582
583int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
584 struct group_filter __user *optval, int __user *optlen)
585{
586 int err, i, count, copycount;
587 struct in6_addr *group;
588 struct ipv6_mc_socklist *pmc;
589 struct inet6_dev *idev;
590 struct net_device *dev;
591 struct ipv6_pinfo *inet6 = inet6_sk(sk);
592 struct ip6_sf_socklist *psl;
593
594 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
595
596 if (!ipv6_addr_is_multicast(group))
597 return -EINVAL;
598
599 idev = ip6_mc_find_dev(group, gsf->gf_interface);
600
601 if (!idev)
602 return -ENODEV;
603
604 dev = idev->dev;
605
606 err = -EADDRNOTAVAIL;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800607 /*
608 * changes to the ipv6_mc_list require the socket lock and
609 * a read lock on ip6_sk_mc_lock. We have the socket lock,
610 * so reading the list is safe.
611 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
614 if (pmc->ifindex != gsf->gf_interface)
615 continue;
616 if (ipv6_addr_equal(group, &pmc->addr))
617 break;
618 }
619 if (!pmc) /* must have a prior join */
620 goto done;
621 gsf->gf_fmode = pmc->sfmode;
622 psl = pmc->sflist;
623 count = psl ? psl->sl_count : 0;
624 read_unlock_bh(&idev->lock);
625 in6_dev_put(idev);
626 dev_put(dev);
627
628 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
629 gsf->gf_numsrc = count;
630 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
631 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
632 return -EFAULT;
633 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800634 /* changes to psl require the socket lock, a read lock on
635 * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
636 * have the socket lock, so reading here is safe.
637 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 for (i=0; i<copycount; i++) {
639 struct sockaddr_in6 *psin6;
640 struct sockaddr_storage ss;
641
642 psin6 = (struct sockaddr_in6 *)&ss;
643 memset(&ss, 0, sizeof(ss));
644 psin6->sin6_family = AF_INET6;
645 psin6->sin6_addr = psl->sl_addr[i];
646 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
647 return -EFAULT;
648 }
649 return 0;
650done:
651 read_unlock_bh(&idev->lock);
652 in6_dev_put(idev);
653 dev_put(dev);
654 return err;
655}
656
657int inet6_mc_check(struct sock *sk, struct in6_addr *mc_addr,
658 struct in6_addr *src_addr)
659{
660 struct ipv6_pinfo *np = inet6_sk(sk);
661 struct ipv6_mc_socklist *mc;
662 struct ip6_sf_socklist *psl;
663 int rv = 1;
664
665 read_lock(&ipv6_sk_mc_lock);
666 for (mc = np->ipv6_mc_list; mc; mc = mc->next) {
667 if (ipv6_addr_equal(&mc->addr, mc_addr))
668 break;
669 }
670 if (!mc) {
671 read_unlock(&ipv6_sk_mc_lock);
672 return 1;
673 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800674 read_lock(&mc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 psl = mc->sflist;
676 if (!psl) {
677 rv = mc->sfmode == MCAST_EXCLUDE;
678 } else {
679 int i;
680
681 for (i=0; i<psl->sl_count; i++) {
682 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
683 break;
684 }
685 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
686 rv = 0;
687 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
688 rv = 0;
689 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800690 read_unlock(&mc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 read_unlock(&ipv6_sk_mc_lock);
692
693 return rv;
694}
695
696static void ma_put(struct ifmcaddr6 *mc)
697{
698 if (atomic_dec_and_test(&mc->mca_refcnt)) {
699 in6_dev_put(mc->idev);
700 kfree(mc);
701 }
702}
703
704static void igmp6_group_added(struct ifmcaddr6 *mc)
705{
706 struct net_device *dev = mc->idev->dev;
707 char buf[MAX_ADDR_LEN];
708
709 spin_lock_bh(&mc->mca_lock);
710 if (!(mc->mca_flags&MAF_LOADED)) {
711 mc->mca_flags |= MAF_LOADED;
712 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
713 dev_mc_add(dev, buf, dev->addr_len, 0);
714 }
715 spin_unlock_bh(&mc->mca_lock);
716
717 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
718 return;
719
720 if (MLD_V1_SEEN(mc->idev)) {
721 igmp6_join_group(mc);
722 return;
723 }
724 /* else v2 */
725
726 mc->mca_crcount = mc->idev->mc_qrv;
727 mld_ifc_event(mc->idev);
728}
729
730static void igmp6_group_dropped(struct ifmcaddr6 *mc)
731{
732 struct net_device *dev = mc->idev->dev;
733 char buf[MAX_ADDR_LEN];
734
735 spin_lock_bh(&mc->mca_lock);
736 if (mc->mca_flags&MAF_LOADED) {
737 mc->mca_flags &= ~MAF_LOADED;
738 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
739 dev_mc_delete(dev, buf, dev->addr_len, 0);
740 }
741
742 if (mc->mca_flags & MAF_NOREPORT)
743 goto done;
744 spin_unlock_bh(&mc->mca_lock);
745
746 if (!mc->idev->dead)
747 igmp6_leave_group(mc);
748
749 spin_lock_bh(&mc->mca_lock);
750 if (del_timer(&mc->mca_timer))
751 atomic_dec(&mc->mca_refcnt);
752done:
753 ip6_mc_clear_src(mc);
754 spin_unlock_bh(&mc->mca_lock);
755}
756
757/*
758 * deleted ifmcaddr6 manipulation
759 */
760static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
761{
762 struct ifmcaddr6 *pmc;
763
764 /* this is an "ifmcaddr6" for convenience; only the fields below
765 * are actually used. In particular, the refcnt and users are not
766 * used for management of the delete list. Using the same structure
767 * for deleted items allows change reports to use common code with
768 * non-deleted or query-response MCA's.
769 */
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800770 pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (!pmc)
772 return;
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 spin_lock_bh(&im->mca_lock);
775 spin_lock_init(&pmc->mca_lock);
776 pmc->idev = im->idev;
777 in6_dev_hold(idev);
778 pmc->mca_addr = im->mca_addr;
779 pmc->mca_crcount = idev->mc_qrv;
780 pmc->mca_sfmode = im->mca_sfmode;
781 if (pmc->mca_sfmode == MCAST_INCLUDE) {
782 struct ip6_sf_list *psf;
783
784 pmc->mca_tomb = im->mca_tomb;
785 pmc->mca_sources = im->mca_sources;
786 im->mca_tomb = im->mca_sources = NULL;
787 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
788 psf->sf_crcount = pmc->mca_crcount;
789 }
790 spin_unlock_bh(&im->mca_lock);
791
792 write_lock_bh(&idev->mc_lock);
793 pmc->next = idev->mc_tomb;
794 idev->mc_tomb = pmc;
795 write_unlock_bh(&idev->mc_lock);
796}
797
798static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *pmca)
799{
800 struct ifmcaddr6 *pmc, *pmc_prev;
801 struct ip6_sf_list *psf, *psf_next;
802
803 write_lock_bh(&idev->mc_lock);
804 pmc_prev = NULL;
805 for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
806 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
807 break;
808 pmc_prev = pmc;
809 }
810 if (pmc) {
811 if (pmc_prev)
812 pmc_prev->next = pmc->next;
813 else
814 idev->mc_tomb = pmc->next;
815 }
816 write_unlock_bh(&idev->mc_lock);
817 if (pmc) {
818 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
819 psf_next = psf->sf_next;
820 kfree(psf);
821 }
822 in6_dev_put(pmc->idev);
823 kfree(pmc);
824 }
825}
826
827static void mld_clear_delrec(struct inet6_dev *idev)
828{
829 struct ifmcaddr6 *pmc, *nextpmc;
830
831 write_lock_bh(&idev->mc_lock);
832 pmc = idev->mc_tomb;
833 idev->mc_tomb = NULL;
834 write_unlock_bh(&idev->mc_lock);
835
836 for (; pmc; pmc = nextpmc) {
837 nextpmc = pmc->next;
838 ip6_mc_clear_src(pmc);
839 in6_dev_put(pmc->idev);
840 kfree(pmc);
841 }
842
843 /* clear dead sources, too */
844 read_lock_bh(&idev->lock);
845 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
846 struct ip6_sf_list *psf, *psf_next;
847
848 spin_lock_bh(&pmc->mca_lock);
849 psf = pmc->mca_tomb;
850 pmc->mca_tomb = NULL;
851 spin_unlock_bh(&pmc->mca_lock);
852 for (; psf; psf=psf_next) {
853 psf_next = psf->sf_next;
854 kfree(psf);
855 }
856 }
857 read_unlock_bh(&idev->lock);
858}
859
860
861/*
862 * device multicast group inc (add if not found)
863 */
864int ipv6_dev_mc_inc(struct net_device *dev, struct in6_addr *addr)
865{
866 struct ifmcaddr6 *mc;
867 struct inet6_dev *idev;
868
869 idev = in6_dev_get(dev);
870
871 if (idev == NULL)
872 return -EINVAL;
873
874 write_lock_bh(&idev->lock);
875 if (idev->dead) {
876 write_unlock_bh(&idev->lock);
877 in6_dev_put(idev);
878 return -ENODEV;
879 }
880
881 for (mc = idev->mc_list; mc; mc = mc->next) {
882 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
883 mc->mca_users++;
884 write_unlock_bh(&idev->lock);
885 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
886 NULL, 0);
887 in6_dev_put(idev);
888 return 0;
889 }
890 }
891
892 /*
893 * not found: create a new one.
894 */
895
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800896 mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 if (mc == NULL) {
899 write_unlock_bh(&idev->lock);
900 in6_dev_put(idev);
901 return -ENOMEM;
902 }
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 init_timer(&mc->mca_timer);
905 mc->mca_timer.function = igmp6_timer_handler;
906 mc->mca_timer.data = (unsigned long) mc;
907
908 ipv6_addr_copy(&mc->mca_addr, addr);
909 mc->idev = idev;
910 mc->mca_users = 1;
911 /* mca_stamp should be updated upon changes */
912 mc->mca_cstamp = mc->mca_tstamp = jiffies;
913 atomic_set(&mc->mca_refcnt, 2);
914 spin_lock_init(&mc->mca_lock);
915
916 /* initial mode is (EX, empty) */
917 mc->mca_sfmode = MCAST_EXCLUDE;
918 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
919
920 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
921 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
922 mc->mca_flags |= MAF_NOREPORT;
923
924 mc->next = idev->mc_list;
925 idev->mc_list = mc;
926 write_unlock_bh(&idev->lock);
927
928 mld_del_delrec(idev, &mc->mca_addr);
929 igmp6_group_added(mc);
930 ma_put(mc);
931 return 0;
932}
933
934/*
935 * device multicast group del
936 */
937int __ipv6_dev_mc_dec(struct inet6_dev *idev, struct in6_addr *addr)
938{
939 struct ifmcaddr6 *ma, **map;
940
941 write_lock_bh(&idev->lock);
942 for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
943 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
944 if (--ma->mca_users == 0) {
945 *map = ma->next;
946 write_unlock_bh(&idev->lock);
947
948 igmp6_group_dropped(ma);
949
950 ma_put(ma);
951 return 0;
952 }
953 write_unlock_bh(&idev->lock);
954 return 0;
955 }
956 }
957 write_unlock_bh(&idev->lock);
958
959 return -ENOENT;
960}
961
962int ipv6_dev_mc_dec(struct net_device *dev, struct in6_addr *addr)
963{
964 struct inet6_dev *idev = in6_dev_get(dev);
965 int err;
966
967 if (!idev)
968 return -ENODEV;
969
970 err = __ipv6_dev_mc_dec(idev, addr);
971
972 in6_dev_put(idev);
973
974 return err;
975}
976
977/*
978 * identify MLD packets for MLD filter exceptions
979 */
980int ipv6_is_mld(struct sk_buff *skb, int nexthdr)
981{
982 struct icmp6hdr *pic;
983
984 if (nexthdr != IPPROTO_ICMPV6)
985 return 0;
986
987 if (!pskb_may_pull(skb, sizeof(struct icmp6hdr)))
988 return 0;
989
990 pic = (struct icmp6hdr *)skb->h.raw;
991
992 switch (pic->icmp6_type) {
993 case ICMPV6_MGM_QUERY:
994 case ICMPV6_MGM_REPORT:
995 case ICMPV6_MGM_REDUCTION:
996 case ICMPV6_MLD2_REPORT:
997 return 1;
998 default:
999 break;
1000 }
1001 return 0;
1002}
1003
1004/*
1005 * check if the interface/address pair is valid
1006 */
1007int ipv6_chk_mcast_addr(struct net_device *dev, struct in6_addr *group,
1008 struct in6_addr *src_addr)
1009{
1010 struct inet6_dev *idev;
1011 struct ifmcaddr6 *mc;
1012 int rv = 0;
1013
1014 idev = in6_dev_get(dev);
1015 if (idev) {
1016 read_lock_bh(&idev->lock);
1017 for (mc = idev->mc_list; mc; mc=mc->next) {
1018 if (ipv6_addr_equal(&mc->mca_addr, group))
1019 break;
1020 }
1021 if (mc) {
1022 if (src_addr && !ipv6_addr_any(src_addr)) {
1023 struct ip6_sf_list *psf;
1024
1025 spin_lock_bh(&mc->mca_lock);
1026 for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
1027 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
1028 break;
1029 }
1030 if (psf)
1031 rv = psf->sf_count[MCAST_INCLUDE] ||
1032 psf->sf_count[MCAST_EXCLUDE] !=
1033 mc->mca_sfcount[MCAST_EXCLUDE];
1034 else
1035 rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
1036 spin_unlock_bh(&mc->mca_lock);
1037 } else
1038 rv = 1; /* don't filter unspecified source */
1039 }
1040 read_unlock_bh(&idev->lock);
1041 in6_dev_put(idev);
1042 }
1043 return rv;
1044}
1045
1046static void mld_gq_start_timer(struct inet6_dev *idev)
1047{
1048 int tv = net_random() % idev->mc_maxdelay;
1049
1050 idev->mc_gq_running = 1;
1051 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
1052 in6_dev_hold(idev);
1053}
1054
1055static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
1056{
1057 int tv = net_random() % delay;
1058
1059 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1060 in6_dev_hold(idev);
1061}
1062
1063/*
1064 * IGMP handling (alias multicast ICMPv6 messages)
1065 */
1066
1067static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1068{
1069 unsigned long delay = resptime;
1070
1071 /* Do not start timer for these addresses */
1072 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1073 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1074 return;
1075
1076 if (del_timer(&ma->mca_timer)) {
1077 atomic_dec(&ma->mca_refcnt);
1078 delay = ma->mca_timer.expires - jiffies;
1079 }
1080
1081 if (delay >= resptime) {
1082 if (resptime)
1083 delay = net_random() % resptime;
1084 else
1085 delay = 1;
1086 }
1087 ma->mca_timer.expires = jiffies + delay;
1088 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1089 atomic_inc(&ma->mca_refcnt);
1090 ma->mca_flags |= MAF_TIMER_RUNNING;
1091}
1092
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001093/* mark EXCLUDE-mode sources */
1094static int mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 struct in6_addr *srcs)
1096{
1097 struct ip6_sf_list *psf;
1098 int i, scount;
1099
1100 scount = 0;
1101 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1102 if (scount == nsrcs)
1103 break;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001104 for (i=0; i<nsrcs; i++) {
1105 /* skip inactive filters */
1106 if (pmc->mca_sfcount[MCAST_INCLUDE] ||
1107 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1108 psf->sf_count[MCAST_EXCLUDE])
1109 continue;
1110 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1111 scount++;
1112 break;
1113 }
1114 }
1115 }
1116 pmc->mca_flags &= ~MAF_GSQUERY;
1117 if (scount == nsrcs) /* all sources excluded */
1118 return 0;
1119 return 1;
1120}
1121
1122static int mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1123 struct in6_addr *srcs)
1124{
1125 struct ip6_sf_list *psf;
1126 int i, scount;
1127
1128 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1129 return mld_xmarksources(pmc, nsrcs, srcs);
1130
1131 /* mark INCLUDE-mode sources */
1132
1133 scount = 0;
1134 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1135 if (scount == nsrcs)
1136 break;
1137 for (i=0; i<nsrcs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1139 psf->sf_gsresp = 1;
1140 scount++;
1141 break;
1142 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001145 if (!scount) {
1146 pmc->mca_flags &= ~MAF_GSQUERY;
1147 return 0;
1148 }
1149 pmc->mca_flags |= MAF_GSQUERY;
1150 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151}
1152
1153int igmp6_event_query(struct sk_buff *skb)
1154{
Yan Zheng97300b52005-10-31 20:09:45 +08001155 struct mld2_query *mlh2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 struct ifmcaddr6 *ma;
1157 struct in6_addr *group;
1158 unsigned long max_delay;
1159 struct inet6_dev *idev;
1160 struct icmp6hdr *hdr;
1161 int group_type;
1162 int mark = 0;
1163 int len;
1164
1165 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1166 return -EINVAL;
1167
1168 /* compute payload length excluding extension headers */
1169 len = ntohs(skb->nh.ipv6h->payload_len) + sizeof(struct ipv6hdr);
1170 len -= (char *)skb->h.raw - (char *)skb->nh.ipv6h;
1171
1172 /* Drop queries with not link local source */
1173 if (!(ipv6_addr_type(&skb->nh.ipv6h->saddr)&IPV6_ADDR_LINKLOCAL))
1174 return -EINVAL;
1175
1176 idev = in6_dev_get(skb->dev);
1177
1178 if (idev == NULL)
1179 return 0;
1180
1181 hdr = (struct icmp6hdr *) skb->h.raw;
1182 group = (struct in6_addr *) (hdr + 1);
1183 group_type = ipv6_addr_type(group);
1184
1185 if (group_type != IPV6_ADDR_ANY &&
1186 !(group_type&IPV6_ADDR_MULTICAST)) {
1187 in6_dev_put(idev);
1188 return -EINVAL;
1189 }
1190
1191 if (len == 24) {
1192 int switchback;
1193 /* MLDv1 router present */
1194
1195 /* Translate milliseconds to jiffies */
1196 max_delay = (ntohs(hdr->icmp6_maxdelay)*HZ)/1000;
1197
1198 switchback = (idev->mc_qrv + 1) * max_delay;
1199 idev->mc_v1_seen = jiffies + switchback;
1200
1201 /* cancel the interface change timer */
1202 idev->mc_ifc_count = 0;
1203 if (del_timer(&idev->mc_ifc_timer))
1204 __in6_dev_put(idev);
1205 /* clear deleted report items */
1206 mld_clear_delrec(idev);
1207 } else if (len >= 28) {
Yan Zheng97300b52005-10-31 20:09:45 +08001208 int srcs_offset = sizeof(struct mld2_query) -
1209 sizeof(struct icmp6hdr);
1210 if (!pskb_may_pull(skb, srcs_offset)) {
1211 in6_dev_put(idev);
1212 return -EINVAL;
1213 }
1214 mlh2 = (struct mld2_query *) skb->h.raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 max_delay = (MLDV2_MRC(ntohs(mlh2->mrc))*HZ)/1000;
1216 if (!max_delay)
1217 max_delay = 1;
1218 idev->mc_maxdelay = max_delay;
1219 if (mlh2->qrv)
1220 idev->mc_qrv = mlh2->qrv;
1221 if (group_type == IPV6_ADDR_ANY) { /* general query */
1222 if (mlh2->nsrcs) {
1223 in6_dev_put(idev);
1224 return -EINVAL; /* no sources allowed */
1225 }
1226 mld_gq_start_timer(idev);
1227 in6_dev_put(idev);
1228 return 0;
1229 }
1230 /* mark sources to include, if group & source-specific */
Yan Zheng97300b52005-10-31 20:09:45 +08001231 if (mlh2->nsrcs != 0) {
1232 if (!pskb_may_pull(skb, srcs_offset +
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001233 ntohs(mlh2->nsrcs) * sizeof(struct in6_addr))) {
Yan Zheng97300b52005-10-31 20:09:45 +08001234 in6_dev_put(idev);
1235 return -EINVAL;
1236 }
1237 mlh2 = (struct mld2_query *) skb->h.raw;
1238 mark = 1;
1239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 } else {
1241 in6_dev_put(idev);
1242 return -EINVAL;
1243 }
1244
1245 read_lock_bh(&idev->lock);
1246 if (group_type == IPV6_ADDR_ANY) {
1247 for (ma = idev->mc_list; ma; ma=ma->next) {
1248 spin_lock_bh(&ma->mca_lock);
1249 igmp6_group_queried(ma, max_delay);
1250 spin_unlock_bh(&ma->mca_lock);
1251 }
1252 } else {
1253 for (ma = idev->mc_list; ma; ma=ma->next) {
David L Stevens7add2a42006-01-24 13:06:39 -08001254 if (!ipv6_addr_equal(group, &ma->mca_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 continue;
1256 spin_lock_bh(&ma->mca_lock);
1257 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1258 /* gsquery <- gsquery && mark */
1259 if (!mark)
1260 ma->mca_flags &= ~MAF_GSQUERY;
1261 } else {
1262 /* gsquery <- mark */
1263 if (mark)
1264 ma->mca_flags |= MAF_GSQUERY;
1265 else
1266 ma->mca_flags &= ~MAF_GSQUERY;
1267 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001268 if (!(ma->mca_flags & MAF_GSQUERY) ||
David L Stevens7add2a42006-01-24 13:06:39 -08001269 mld_marksources(ma, ntohs(mlh2->nsrcs), mlh2->srcs))
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001270 igmp6_group_queried(ma, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 spin_unlock_bh(&ma->mca_lock);
David L Stevens7add2a42006-01-24 13:06:39 -08001272 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
1274 }
1275 read_unlock_bh(&idev->lock);
1276 in6_dev_put(idev);
1277
1278 return 0;
1279}
1280
1281
1282int igmp6_event_report(struct sk_buff *skb)
1283{
1284 struct ifmcaddr6 *ma;
1285 struct in6_addr *addrp;
1286 struct inet6_dev *idev;
1287 struct icmp6hdr *hdr;
1288 int addr_type;
1289
1290 /* Our own report looped back. Ignore it. */
1291 if (skb->pkt_type == PACKET_LOOPBACK)
1292 return 0;
1293
David Stevens24c69272005-12-02 20:32:59 -08001294 /* send our report if the MC router may not have heard this report */
1295 if (skb->pkt_type != PACKET_MULTICAST &&
1296 skb->pkt_type != PACKET_BROADCAST)
1297 return 0;
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1300 return -EINVAL;
1301
1302 hdr = (struct icmp6hdr*) skb->h.raw;
1303
1304 /* Drop reports with not link local source */
1305 addr_type = ipv6_addr_type(&skb->nh.ipv6h->saddr);
1306 if (addr_type != IPV6_ADDR_ANY &&
1307 !(addr_type&IPV6_ADDR_LINKLOCAL))
1308 return -EINVAL;
1309
1310 addrp = (struct in6_addr *) (hdr + 1);
1311
1312 idev = in6_dev_get(skb->dev);
1313 if (idev == NULL)
1314 return -ENODEV;
1315
1316 /*
1317 * Cancel the timer for this group
1318 */
1319
1320 read_lock_bh(&idev->lock);
1321 for (ma = idev->mc_list; ma; ma=ma->next) {
1322 if (ipv6_addr_equal(&ma->mca_addr, addrp)) {
1323 spin_lock(&ma->mca_lock);
1324 if (del_timer(&ma->mca_timer))
1325 atomic_dec(&ma->mca_refcnt);
1326 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1327 spin_unlock(&ma->mca_lock);
1328 break;
1329 }
1330 }
1331 read_unlock_bh(&idev->lock);
1332 in6_dev_put(idev);
1333 return 0;
1334}
1335
1336static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1337 int gdeleted, int sdeleted)
1338{
1339 switch (type) {
1340 case MLD2_MODE_IS_INCLUDE:
1341 case MLD2_MODE_IS_EXCLUDE:
1342 if (gdeleted || sdeleted)
1343 return 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001344 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1345 if (pmc->mca_sfmode == MCAST_INCLUDE)
1346 return 1;
1347 /* don't include if this source is excluded
1348 * in all filters
1349 */
1350 if (psf->sf_count[MCAST_INCLUDE])
David L Stevens7add2a42006-01-24 13:06:39 -08001351 return type == MLD2_MODE_IS_INCLUDE;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001352 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1353 psf->sf_count[MCAST_EXCLUDE];
1354 }
1355 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 case MLD2_CHANGE_TO_INCLUDE:
1357 if (gdeleted || sdeleted)
1358 return 0;
1359 return psf->sf_count[MCAST_INCLUDE] != 0;
1360 case MLD2_CHANGE_TO_EXCLUDE:
1361 if (gdeleted || sdeleted)
1362 return 0;
1363 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1364 psf->sf_count[MCAST_INCLUDE])
1365 return 0;
1366 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1367 psf->sf_count[MCAST_EXCLUDE];
1368 case MLD2_ALLOW_NEW_SOURCES:
1369 if (gdeleted || !psf->sf_crcount)
1370 return 0;
1371 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1372 case MLD2_BLOCK_OLD_SOURCES:
1373 if (pmc->mca_sfmode == MCAST_INCLUDE)
1374 return gdeleted || (psf->sf_crcount && sdeleted);
1375 return psf->sf_crcount && !gdeleted && !sdeleted;
1376 }
1377 return 0;
1378}
1379
1380static int
1381mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1382{
1383 struct ip6_sf_list *psf;
1384 int scount = 0;
1385
1386 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1387 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1388 continue;
1389 scount++;
1390 }
1391 return scount;
1392}
1393
1394static struct sk_buff *mld_newpack(struct net_device *dev, int size)
1395{
1396 struct sock *sk = igmp6_socket->sk;
1397 struct sk_buff *skb;
1398 struct mld2_report *pmr;
1399 struct in6_addr addr_buf;
1400 int err;
1401 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1402 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1403 IPV6_TLV_PADN, 0 };
1404
1405 /* we assume size > sizeof(ra) here */
1406 skb = sock_alloc_send_skb(sk, size + LL_RESERVED_SPACE(dev), 1, &err);
1407
1408 if (skb == 0)
1409 return NULL;
1410
1411 skb_reserve(skb, LL_RESERVED_SPACE(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 if (ipv6_get_lladdr(dev, &addr_buf)) {
1414 /* <draft-ietf-magma-mld-source-05.txt>:
1415 * use unspecified address as the source address
1416 * when a valid link-local address is not available.
1417 */
1418 memset(&addr_buf, 0, sizeof(addr_buf));
1419 }
1420
1421 ip6_nd_hdr(sk, skb, dev, &addr_buf, &mld2_all_mcr, NEXTHDR_HOP, 0);
1422
1423 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1424
1425 pmr =(struct mld2_report *)skb_put(skb, sizeof(*pmr));
1426 skb->h.raw = (unsigned char *)pmr;
1427 pmr->type = ICMPV6_MLD2_REPORT;
1428 pmr->resv1 = 0;
1429 pmr->csum = 0;
1430 pmr->resv2 = 0;
1431 pmr->ngrec = 0;
1432 return skb;
1433}
1434
Patrick McHardye9823182005-06-21 14:02:15 -07001435static inline int mld_dev_queue_xmit2(struct sk_buff *skb)
1436{
1437 struct net_device *dev = skb->dev;
1438
1439 if (dev->hard_header) {
1440 unsigned char ha[MAX_ADDR_LEN];
1441 int err;
1442
1443 ndisc_mc_map(&skb->nh.ipv6h->daddr, ha, dev, 1);
1444 err = dev->hard_header(skb, dev, ETH_P_IPV6, ha, NULL, skb->len);
1445 if (err < 0) {
1446 kfree_skb(skb);
1447 return err;
1448 }
1449 }
1450 return dev_queue_xmit(skb);
1451}
1452
1453static inline int mld_dev_queue_xmit(struct sk_buff *skb)
1454{
1455 return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL, skb->dev,
1456 mld_dev_queue_xmit2);
1457}
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459static void mld_sendpack(struct sk_buff *skb)
1460{
1461 struct ipv6hdr *pip6 = skb->nh.ipv6h;
1462 struct mld2_report *pmr = (struct mld2_report *)skb->h.raw;
1463 int payload_len, mldlen;
1464 struct inet6_dev *idev = in6_dev_get(skb->dev);
1465 int err;
1466
1467 IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
1468 payload_len = skb->tail - (unsigned char *)skb->nh.ipv6h -
1469 sizeof(struct ipv6hdr);
1470 mldlen = skb->tail - skb->h.raw;
1471 pip6->payload_len = htons(payload_len);
1472
1473 pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1474 IPPROTO_ICMPV6, csum_partial(skb->h.raw, mldlen, 0));
1475 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
Patrick McHardye9823182005-06-21 14:02:15 -07001476 mld_dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 if (!err) {
1478 ICMP6_INC_STATS(idev,ICMP6_MIB_OUTMSGS);
1479 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
1480 } else
1481 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1482
1483 if (likely(idev != NULL))
1484 in6_dev_put(idev);
1485}
1486
1487static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1488{
Yan Zhengfab10fe2005-10-05 12:08:13 -07001489 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
1492static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1493 int type, struct mld2_grec **ppgr)
1494{
1495 struct net_device *dev = pmc->idev->dev;
1496 struct mld2_report *pmr;
1497 struct mld2_grec *pgr;
1498
1499 if (!skb)
1500 skb = mld_newpack(dev, dev->mtu);
1501 if (!skb)
1502 return NULL;
1503 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1504 pgr->grec_type = type;
1505 pgr->grec_auxwords = 0;
1506 pgr->grec_nsrcs = 0;
1507 pgr->grec_mca = pmc->mca_addr; /* structure copy */
1508 pmr = (struct mld2_report *)skb->h.raw;
1509 pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
1510 *ppgr = pgr;
1511 return skb;
1512}
1513
1514#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1515 skb_tailroom(skb)) : 0)
1516
1517static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1518 int type, int gdeleted, int sdeleted)
1519{
1520 struct net_device *dev = pmc->idev->dev;
1521 struct mld2_report *pmr;
1522 struct mld2_grec *pgr = NULL;
1523 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001524 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 if (pmc->mca_flags & MAF_NOREPORT)
1527 return skb;
1528
1529 isquery = type == MLD2_MODE_IS_INCLUDE ||
1530 type == MLD2_MODE_IS_EXCLUDE;
1531 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1532 type == MLD2_CHANGE_TO_EXCLUDE;
1533
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001534 stotal = scount = 0;
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1537
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001538 if (!*psf_list)
1539 goto empty_source;
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 pmr = skb ? (struct mld2_report *)skb->h.raw : NULL;
1542
1543 /* EX and TO_EX get a fresh packet, if needed */
1544 if (truncate) {
1545 if (pmr && pmr->ngrec &&
1546 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1547 if (skb)
1548 mld_sendpack(skb);
1549 skb = mld_newpack(dev, dev->mtu);
1550 }
1551 }
1552 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 psf_prev = NULL;
1554 for (psf=*psf_list; psf; psf=psf_next) {
1555 struct in6_addr *psrc;
1556
1557 psf_next = psf->sf_next;
1558
1559 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1560 psf_prev = psf;
1561 continue;
1562 }
1563
1564 /* clear marks on query responses */
1565 if (isquery)
1566 psf->sf_gsresp = 0;
1567
1568 if (AVAILABLE(skb) < sizeof(*psrc) +
1569 first*sizeof(struct mld2_grec)) {
1570 if (truncate && !first)
1571 break; /* truncate these */
1572 if (pgr)
1573 pgr->grec_nsrcs = htons(scount);
1574 if (skb)
1575 mld_sendpack(skb);
1576 skb = mld_newpack(dev, dev->mtu);
1577 first = 1;
1578 scount = 0;
1579 }
1580 if (first) {
1581 skb = add_grhead(skb, pmc, type, &pgr);
1582 first = 0;
1583 }
1584 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1585 *psrc = psf->sf_addr;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001586 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1588 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1589 psf->sf_crcount--;
1590 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1591 if (psf_prev)
1592 psf_prev->sf_next = psf->sf_next;
1593 else
1594 *psf_list = psf->sf_next;
1595 kfree(psf);
1596 continue;
1597 }
1598 }
1599 psf_prev = psf;
1600 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001601
1602empty_source:
1603 if (!stotal) {
1604 if (type == MLD2_ALLOW_NEW_SOURCES ||
1605 type == MLD2_BLOCK_OLD_SOURCES)
1606 return skb;
1607 if (pmc->mca_crcount || isquery) {
1608 /* make sure we have room for group header */
1609 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1610 mld_sendpack(skb);
1611 skb = NULL; /* add_grhead will get a new one */
1612 }
1613 skb = add_grhead(skb, pmc, type, &pgr);
1614 }
1615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 if (pgr)
1617 pgr->grec_nsrcs = htons(scount);
1618
1619 if (isquery)
1620 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1621 return skb;
1622}
1623
1624static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1625{
1626 struct sk_buff *skb = NULL;
1627 int type;
1628
1629 if (!pmc) {
1630 read_lock_bh(&idev->lock);
1631 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1632 if (pmc->mca_flags & MAF_NOREPORT)
1633 continue;
1634 spin_lock_bh(&pmc->mca_lock);
1635 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1636 type = MLD2_MODE_IS_EXCLUDE;
1637 else
1638 type = MLD2_MODE_IS_INCLUDE;
1639 skb = add_grec(skb, pmc, type, 0, 0);
1640 spin_unlock_bh(&pmc->mca_lock);
1641 }
1642 read_unlock_bh(&idev->lock);
1643 } else {
1644 spin_lock_bh(&pmc->mca_lock);
1645 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1646 type = MLD2_MODE_IS_EXCLUDE;
1647 else
1648 type = MLD2_MODE_IS_INCLUDE;
1649 skb = add_grec(skb, pmc, type, 0, 0);
1650 spin_unlock_bh(&pmc->mca_lock);
1651 }
1652 if (skb)
1653 mld_sendpack(skb);
1654}
1655
1656/*
1657 * remove zero-count source records from a source filter list
1658 */
1659static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1660{
1661 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1662
1663 psf_prev = NULL;
1664 for (psf=*ppsf; psf; psf = psf_next) {
1665 psf_next = psf->sf_next;
1666 if (psf->sf_crcount == 0) {
1667 if (psf_prev)
1668 psf_prev->sf_next = psf->sf_next;
1669 else
1670 *ppsf = psf->sf_next;
1671 kfree(psf);
1672 } else
1673 psf_prev = psf;
1674 }
1675}
1676
1677static void mld_send_cr(struct inet6_dev *idev)
1678{
1679 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1680 struct sk_buff *skb = NULL;
1681 int type, dtype;
1682
1683 read_lock_bh(&idev->lock);
1684 write_lock_bh(&idev->mc_lock);
1685
1686 /* deleted MCA's */
1687 pmc_prev = NULL;
1688 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1689 pmc_next = pmc->next;
1690 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1691 type = MLD2_BLOCK_OLD_SOURCES;
1692 dtype = MLD2_BLOCK_OLD_SOURCES;
1693 skb = add_grec(skb, pmc, type, 1, 0);
1694 skb = add_grec(skb, pmc, dtype, 1, 1);
1695 }
1696 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1698 type = MLD2_CHANGE_TO_INCLUDE;
1699 skb = add_grec(skb, pmc, type, 1, 0);
1700 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001701 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 if (pmc->mca_crcount == 0) {
1703 mld_clear_zeros(&pmc->mca_tomb);
1704 mld_clear_zeros(&pmc->mca_sources);
1705 }
1706 }
1707 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1708 !pmc->mca_sources) {
1709 if (pmc_prev)
1710 pmc_prev->next = pmc_next;
1711 else
1712 idev->mc_tomb = pmc_next;
1713 in6_dev_put(pmc->idev);
1714 kfree(pmc);
1715 } else
1716 pmc_prev = pmc;
1717 }
1718 write_unlock_bh(&idev->mc_lock);
1719
1720 /* change recs */
1721 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1722 spin_lock_bh(&pmc->mca_lock);
1723 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1724 type = MLD2_BLOCK_OLD_SOURCES;
1725 dtype = MLD2_ALLOW_NEW_SOURCES;
1726 } else {
1727 type = MLD2_ALLOW_NEW_SOURCES;
1728 dtype = MLD2_BLOCK_OLD_SOURCES;
1729 }
1730 skb = add_grec(skb, pmc, type, 0, 0);
1731 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1732
1733 /* filter mode changes */
1734 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1736 type = MLD2_CHANGE_TO_EXCLUDE;
1737 else
1738 type = MLD2_CHANGE_TO_INCLUDE;
1739 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001740 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
1742 spin_unlock_bh(&pmc->mca_lock);
1743 }
1744 read_unlock_bh(&idev->lock);
1745 if (!skb)
1746 return;
1747 (void) mld_sendpack(skb);
1748}
1749
1750static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1751{
1752 struct sock *sk = igmp6_socket->sk;
1753 struct inet6_dev *idev;
1754 struct sk_buff *skb;
1755 struct icmp6hdr *hdr;
1756 struct in6_addr *snd_addr;
1757 struct in6_addr *addrp;
1758 struct in6_addr addr_buf;
1759 struct in6_addr all_routers;
1760 int err, len, payload_len, full_len;
1761 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1762 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1763 IPV6_TLV_PADN, 0 };
1764
1765 IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
1766 snd_addr = addr;
1767 if (type == ICMPV6_MGM_REDUCTION) {
1768 snd_addr = &all_routers;
1769 ipv6_addr_all_routers(&all_routers);
1770 }
1771
1772 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1773 payload_len = len + sizeof(ra);
1774 full_len = sizeof(struct ipv6hdr) + payload_len;
1775
1776 skb = sock_alloc_send_skb(sk, LL_RESERVED_SPACE(dev) + full_len, 1, &err);
1777
1778 if (skb == NULL) {
1779 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1780 return;
1781 }
1782
1783 skb_reserve(skb, LL_RESERVED_SPACE(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
1785 if (ipv6_get_lladdr(dev, &addr_buf)) {
1786 /* <draft-ietf-magma-mld-source-05.txt>:
1787 * use unspecified address as the source address
1788 * when a valid link-local address is not available.
1789 */
1790 memset(&addr_buf, 0, sizeof(addr_buf));
1791 }
1792
1793 ip6_nd_hdr(sk, skb, dev, &addr_buf, snd_addr, NEXTHDR_HOP, payload_len);
1794
1795 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1796
1797 hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
1798 memset(hdr, 0, sizeof(struct icmp6hdr));
1799 hdr->icmp6_type = type;
1800
1801 addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
1802 ipv6_addr_copy(addrp, addr);
1803
1804 hdr->icmp6_cksum = csum_ipv6_magic(&addr_buf, snd_addr, len,
1805 IPPROTO_ICMPV6,
1806 csum_partial((__u8 *) hdr, len, 0));
1807
1808 idev = in6_dev_get(skb->dev);
1809
1810 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
Patrick McHardye9823182005-06-21 14:02:15 -07001811 mld_dev_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 if (!err) {
1813 if (type == ICMPV6_MGM_REDUCTION)
1814 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBREDUCTIONS);
1815 else
1816 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBRESPONSES);
1817 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTMSGS);
1818 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
1819 } else
1820 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1821
1822 if (likely(idev != NULL))
1823 in6_dev_put(idev);
1824 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
1827static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1828 struct in6_addr *psfsrc)
1829{
1830 struct ip6_sf_list *psf, *psf_prev;
1831 int rv = 0;
1832
1833 psf_prev = NULL;
1834 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1835 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1836 break;
1837 psf_prev = psf;
1838 }
1839 if (!psf || psf->sf_count[sfmode] == 0) {
1840 /* source filter not found, or count wrong => bug */
1841 return -ESRCH;
1842 }
1843 psf->sf_count[sfmode]--;
1844 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1845 struct inet6_dev *idev = pmc->idev;
1846
1847 /* no more filters for this source */
1848 if (psf_prev)
1849 psf_prev->sf_next = psf->sf_next;
1850 else
1851 pmc->mca_sources = psf->sf_next;
1852 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1853 !MLD_V1_SEEN(idev)) {
1854 psf->sf_crcount = idev->mc_qrv;
1855 psf->sf_next = pmc->mca_tomb;
1856 pmc->mca_tomb = psf;
1857 rv = 1;
1858 } else
1859 kfree(psf);
1860 }
1861 return rv;
1862}
1863
1864static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
1865 int sfmode, int sfcount, struct in6_addr *psfsrc,
1866 int delta)
1867{
1868 struct ifmcaddr6 *pmc;
1869 int changerec = 0;
1870 int i, err;
1871
1872 if (!idev)
1873 return -ENODEV;
1874 read_lock_bh(&idev->lock);
1875 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1876 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1877 break;
1878 }
1879 if (!pmc) {
1880 /* MCA not found?? bug */
1881 read_unlock_bh(&idev->lock);
1882 return -ESRCH;
1883 }
1884 spin_lock_bh(&pmc->mca_lock);
1885 sf_markstate(pmc);
1886 if (!delta) {
1887 if (!pmc->mca_sfcount[sfmode]) {
1888 spin_unlock_bh(&pmc->mca_lock);
1889 read_unlock_bh(&idev->lock);
1890 return -EINVAL;
1891 }
1892 pmc->mca_sfcount[sfmode]--;
1893 }
1894 err = 0;
1895 for (i=0; i<sfcount; i++) {
1896 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1897
1898 changerec |= rv > 0;
1899 if (!err && rv < 0)
1900 err = rv;
1901 }
1902 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1903 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1904 pmc->mca_sfcount[MCAST_INCLUDE]) {
1905 struct ip6_sf_list *psf;
1906
1907 /* filter mode change */
1908 pmc->mca_sfmode = MCAST_INCLUDE;
1909 pmc->mca_crcount = idev->mc_qrv;
1910 idev->mc_ifc_count = pmc->mca_crcount;
1911 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1912 psf->sf_crcount = 0;
1913 mld_ifc_event(pmc->idev);
1914 } else if (sf_setstate(pmc) || changerec)
1915 mld_ifc_event(pmc->idev);
1916 spin_unlock_bh(&pmc->mca_lock);
1917 read_unlock_bh(&idev->lock);
1918 return err;
1919}
1920
1921/*
1922 * Add multicast single-source filter to the interface list
1923 */
1924static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1925 struct in6_addr *psfsrc, int delta)
1926{
1927 struct ip6_sf_list *psf, *psf_prev;
1928
1929 psf_prev = NULL;
1930 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1931 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1932 break;
1933 psf_prev = psf;
1934 }
1935 if (!psf) {
Ingo Oeser0c600ed2006-03-20 23:01:32 -08001936 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 if (!psf)
1938 return -ENOBUFS;
Ingo Oeser0c600ed2006-03-20 23:01:32 -08001939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 psf->sf_addr = *psfsrc;
1941 if (psf_prev) {
1942 psf_prev->sf_next = psf;
1943 } else
1944 pmc->mca_sources = psf;
1945 }
1946 psf->sf_count[sfmode]++;
1947 return 0;
1948}
1949
1950static void sf_markstate(struct ifmcaddr6 *pmc)
1951{
1952 struct ip6_sf_list *psf;
1953 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1954
1955 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1956 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1957 psf->sf_oldin = mca_xcount ==
1958 psf->sf_count[MCAST_EXCLUDE] &&
1959 !psf->sf_count[MCAST_INCLUDE];
1960 } else
1961 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1962}
1963
1964static int sf_setstate(struct ifmcaddr6 *pmc)
1965{
David L Stevens7add2a42006-01-24 13:06:39 -08001966 struct ip6_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1968 int qrv = pmc->idev->mc_qrv;
1969 int new_in, rv;
1970
1971 rv = 0;
1972 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1973 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1974 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1975 !psf->sf_count[MCAST_INCLUDE];
1976 } else
1977 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevens7add2a42006-01-24 13:06:39 -08001978 if (new_in) {
1979 if (!psf->sf_oldin) {
Al Viroe80e28b2006-02-03 20:10:03 -05001980 struct ip6_sf_list *prev = NULL;
David L Stevens7add2a42006-01-24 13:06:39 -08001981
1982 for (dpsf=pmc->mca_tomb; dpsf;
1983 dpsf=dpsf->sf_next) {
1984 if (ipv6_addr_equal(&dpsf->sf_addr,
1985 &psf->sf_addr))
1986 break;
1987 prev = dpsf;
1988 }
1989 if (dpsf) {
1990 if (prev)
1991 prev->sf_next = dpsf->sf_next;
1992 else
1993 pmc->mca_tomb = dpsf->sf_next;
1994 kfree(dpsf);
1995 }
1996 psf->sf_crcount = qrv;
1997 rv++;
1998 }
1999 } else if (psf->sf_oldin) {
2000 psf->sf_crcount = 0;
2001 /*
2002 * add or update "delete" records if an active filter
2003 * is now inactive
2004 */
2005 for (dpsf=pmc->mca_tomb; dpsf; dpsf=dpsf->sf_next)
2006 if (ipv6_addr_equal(&dpsf->sf_addr,
2007 &psf->sf_addr))
2008 break;
2009 if (!dpsf) {
2010 dpsf = (struct ip6_sf_list *)
2011 kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2012 if (!dpsf)
2013 continue;
2014 *dpsf = *psf;
2015 /* pmc->mca_lock held by callers */
2016 dpsf->sf_next = pmc->mca_tomb;
2017 pmc->mca_tomb = dpsf;
2018 }
2019 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 rv++;
2021 }
2022 }
2023 return rv;
2024}
2025
2026/*
2027 * Add multicast source filter list to the interface list
2028 */
2029static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
2030 int sfmode, int sfcount, struct in6_addr *psfsrc,
2031 int delta)
2032{
2033 struct ifmcaddr6 *pmc;
2034 int isexclude;
2035 int i, err;
2036
2037 if (!idev)
2038 return -ENODEV;
2039 read_lock_bh(&idev->lock);
2040 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
2041 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2042 break;
2043 }
2044 if (!pmc) {
2045 /* MCA not found?? bug */
2046 read_unlock_bh(&idev->lock);
2047 return -ESRCH;
2048 }
2049 spin_lock_bh(&pmc->mca_lock);
2050
2051 sf_markstate(pmc);
2052 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2053 if (!delta)
2054 pmc->mca_sfcount[sfmode]++;
2055 err = 0;
2056 for (i=0; i<sfcount; i++) {
2057 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
2058 if (err)
2059 break;
2060 }
2061 if (err) {
2062 int j;
2063
2064 if (!delta)
2065 pmc->mca_sfcount[sfmode]--;
2066 for (j=0; j<i; j++)
2067 (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2068 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
2069 struct inet6_dev *idev = pmc->idev;
2070 struct ip6_sf_list *psf;
2071
2072 /* filter mode change */
2073 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2074 pmc->mca_sfmode = MCAST_EXCLUDE;
2075 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2076 pmc->mca_sfmode = MCAST_INCLUDE;
2077 /* else no filters; keep old mode for reports */
2078
2079 pmc->mca_crcount = idev->mc_qrv;
2080 idev->mc_ifc_count = pmc->mca_crcount;
2081 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
2082 psf->sf_crcount = 0;
2083 mld_ifc_event(idev);
2084 } else if (sf_setstate(pmc))
2085 mld_ifc_event(idev);
2086 spin_unlock_bh(&pmc->mca_lock);
2087 read_unlock_bh(&idev->lock);
2088 return err;
2089}
2090
2091static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2092{
2093 struct ip6_sf_list *psf, *nextpsf;
2094
2095 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
2096 nextpsf = psf->sf_next;
2097 kfree(psf);
2098 }
2099 pmc->mca_tomb = NULL;
2100 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
2101 nextpsf = psf->sf_next;
2102 kfree(psf);
2103 }
2104 pmc->mca_sources = NULL;
2105 pmc->mca_sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002106 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2108}
2109
2110
2111static void igmp6_join_group(struct ifmcaddr6 *ma)
2112{
2113 unsigned long delay;
2114
2115 if (ma->mca_flags & MAF_NOREPORT)
2116 return;
2117
2118 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2119
2120 delay = net_random() % IGMP6_UNSOLICITED_IVAL;
2121
2122 spin_lock_bh(&ma->mca_lock);
2123 if (del_timer(&ma->mca_timer)) {
2124 atomic_dec(&ma->mca_refcnt);
2125 delay = ma->mca_timer.expires - jiffies;
2126 }
2127
2128 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2129 atomic_inc(&ma->mca_refcnt);
2130 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2131 spin_unlock_bh(&ma->mca_lock);
2132}
2133
2134static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2135 struct inet6_dev *idev)
2136{
2137 int err;
2138
David L Stevens5ab4a6c2005-12-27 14:03:00 -08002139 /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
2140 * so no other readers or writers of iml or its sflist
2141 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 if (iml->sflist == 0) {
2143 /* any-source empty exclude case */
2144 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2145 }
2146 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2147 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2148 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2149 iml->sflist = NULL;
2150 return err;
2151}
2152
2153static void igmp6_leave_group(struct ifmcaddr6 *ma)
2154{
2155 if (MLD_V1_SEEN(ma->idev)) {
2156 if (ma->mca_flags & MAF_LAST_REPORTER)
2157 igmp6_send(&ma->mca_addr, ma->idev->dev,
2158 ICMPV6_MGM_REDUCTION);
2159 } else {
2160 mld_add_delrec(ma->idev, ma);
2161 mld_ifc_event(ma->idev);
2162 }
2163}
2164
2165static void mld_gq_timer_expire(unsigned long data)
2166{
2167 struct inet6_dev *idev = (struct inet6_dev *)data;
2168
2169 idev->mc_gq_running = 0;
2170 mld_send_report(idev, NULL);
2171 __in6_dev_put(idev);
2172}
2173
2174static void mld_ifc_timer_expire(unsigned long data)
2175{
2176 struct inet6_dev *idev = (struct inet6_dev *)data;
2177
2178 mld_send_cr(idev);
2179 if (idev->mc_ifc_count) {
2180 idev->mc_ifc_count--;
2181 if (idev->mc_ifc_count)
2182 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2183 }
2184 __in6_dev_put(idev);
2185}
2186
2187static void mld_ifc_event(struct inet6_dev *idev)
2188{
2189 if (MLD_V1_SEEN(idev))
2190 return;
2191 idev->mc_ifc_count = idev->mc_qrv;
2192 mld_ifc_start_timer(idev, 1);
2193}
2194
2195
2196static void igmp6_timer_handler(unsigned long data)
2197{
2198 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2199
2200 if (MLD_V1_SEEN(ma->idev))
2201 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2202 else
2203 mld_send_report(ma->idev, ma);
2204
2205 spin_lock(&ma->mca_lock);
2206 ma->mca_flags |= MAF_LAST_REPORTER;
2207 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2208 spin_unlock(&ma->mca_lock);
2209 ma_put(ma);
2210}
2211
2212/* Device going down */
2213
2214void ipv6_mc_down(struct inet6_dev *idev)
2215{
2216 struct ifmcaddr6 *i;
2217
2218 /* Withdraw multicast list */
2219
2220 read_lock_bh(&idev->lock);
2221 idev->mc_ifc_count = 0;
2222 if (del_timer(&idev->mc_ifc_timer))
2223 __in6_dev_put(idev);
2224 idev->mc_gq_running = 0;
2225 if (del_timer(&idev->mc_gq_timer))
2226 __in6_dev_put(idev);
2227
2228 for (i = idev->mc_list; i; i=i->next)
2229 igmp6_group_dropped(i);
2230 read_unlock_bh(&idev->lock);
2231
2232 mld_clear_delrec(idev);
2233}
2234
2235
2236/* Device going up */
2237
2238void ipv6_mc_up(struct inet6_dev *idev)
2239{
2240 struct ifmcaddr6 *i;
2241
2242 /* Install multicast list, except for all-nodes (already installed) */
2243
2244 read_lock_bh(&idev->lock);
2245 for (i = idev->mc_list; i; i=i->next)
2246 igmp6_group_added(i);
2247 read_unlock_bh(&idev->lock);
2248}
2249
2250/* IPv6 device initialization. */
2251
2252void ipv6_mc_init_dev(struct inet6_dev *idev)
2253{
2254 struct in6_addr maddr;
2255
2256 write_lock_bh(&idev->lock);
2257 rwlock_init(&idev->mc_lock);
2258 idev->mc_gq_running = 0;
2259 init_timer(&idev->mc_gq_timer);
2260 idev->mc_gq_timer.data = (unsigned long) idev;
2261 idev->mc_gq_timer.function = &mld_gq_timer_expire;
2262 idev->mc_tomb = NULL;
2263 idev->mc_ifc_count = 0;
2264 init_timer(&idev->mc_ifc_timer);
2265 idev->mc_ifc_timer.data = (unsigned long) idev;
2266 idev->mc_ifc_timer.function = &mld_ifc_timer_expire;
2267 idev->mc_qrv = MLD_QRV_DEFAULT;
2268 idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2269 idev->mc_v1_seen = 0;
2270 write_unlock_bh(&idev->lock);
2271
2272 /* Add all-nodes address. */
2273 ipv6_addr_all_nodes(&maddr);
2274 ipv6_dev_mc_inc(idev->dev, &maddr);
2275}
2276
2277/*
2278 * Device is about to be destroyed: clean up.
2279 */
2280
2281void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2282{
2283 struct ifmcaddr6 *i;
2284 struct in6_addr maddr;
2285
2286 /* Deactivate timers */
2287 ipv6_mc_down(idev);
2288
2289 /* Delete all-nodes address. */
2290 ipv6_addr_all_nodes(&maddr);
2291
2292 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2293 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2294 * fail.
2295 */
2296 __ipv6_dev_mc_dec(idev, &maddr);
2297
2298 if (idev->cnf.forwarding) {
2299 ipv6_addr_all_routers(&maddr);
2300 __ipv6_dev_mc_dec(idev, &maddr);
2301 }
2302
2303 write_lock_bh(&idev->lock);
2304 while ((i = idev->mc_list) != NULL) {
2305 idev->mc_list = i->next;
2306 write_unlock_bh(&idev->lock);
2307
2308 igmp6_group_dropped(i);
2309 ma_put(i);
2310
2311 write_lock_bh(&idev->lock);
2312 }
2313 write_unlock_bh(&idev->lock);
2314}
2315
2316#ifdef CONFIG_PROC_FS
2317struct igmp6_mc_iter_state {
2318 struct net_device *dev;
2319 struct inet6_dev *idev;
2320};
2321
2322#define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2323
2324static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2325{
2326 struct ifmcaddr6 *im = NULL;
2327 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2328
2329 for (state->dev = dev_base, state->idev = NULL;
2330 state->dev;
2331 state->dev = state->dev->next) {
2332 struct inet6_dev *idev;
2333 idev = in6_dev_get(state->dev);
2334 if (!idev)
2335 continue;
2336 read_lock_bh(&idev->lock);
2337 im = idev->mc_list;
2338 if (im) {
2339 state->idev = idev;
2340 break;
2341 }
2342 read_unlock_bh(&idev->lock);
2343 in6_dev_put(idev);
2344 }
2345 return im;
2346}
2347
2348static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2349{
2350 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2351
2352 im = im->next;
2353 while (!im) {
2354 if (likely(state->idev != NULL)) {
2355 read_unlock_bh(&state->idev->lock);
2356 in6_dev_put(state->idev);
2357 }
2358 state->dev = state->dev->next;
2359 if (!state->dev) {
2360 state->idev = NULL;
2361 break;
2362 }
2363 state->idev = in6_dev_get(state->dev);
2364 if (!state->idev)
2365 continue;
2366 read_lock_bh(&state->idev->lock);
2367 im = state->idev->mc_list;
2368 }
2369 return im;
2370}
2371
2372static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2373{
2374 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2375 if (im)
2376 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2377 --pos;
2378 return pos ? NULL : im;
2379}
2380
2381static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2382{
2383 read_lock(&dev_base_lock);
2384 return igmp6_mc_get_idx(seq, *pos);
2385}
2386
2387static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2388{
2389 struct ifmcaddr6 *im;
2390 im = igmp6_mc_get_next(seq, v);
2391 ++*pos;
2392 return im;
2393}
2394
2395static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2396{
2397 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2398 if (likely(state->idev != NULL)) {
2399 read_unlock_bh(&state->idev->lock);
2400 in6_dev_put(state->idev);
2401 state->idev = NULL;
2402 }
2403 state->dev = NULL;
2404 read_unlock(&dev_base_lock);
2405}
2406
2407static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2408{
2409 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2410 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2411
2412 seq_printf(seq,
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002413 "%-4d %-15s " NIP6_SEQFMT " %5d %08X %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 state->dev->ifindex, state->dev->name,
2415 NIP6(im->mca_addr),
2416 im->mca_users, im->mca_flags,
2417 (im->mca_flags&MAF_TIMER_RUNNING) ?
2418 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2419 return 0;
2420}
2421
2422static struct seq_operations igmp6_mc_seq_ops = {
2423 .start = igmp6_mc_seq_start,
2424 .next = igmp6_mc_seq_next,
2425 .stop = igmp6_mc_seq_stop,
2426 .show = igmp6_mc_seq_show,
2427};
2428
2429static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2430{
2431 struct seq_file *seq;
2432 int rc = -ENOMEM;
Ingo Oeser0c600ed2006-03-20 23:01:32 -08002433 struct igmp6_mc_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
2435 if (!s)
2436 goto out;
2437
2438 rc = seq_open(file, &igmp6_mc_seq_ops);
2439 if (rc)
2440 goto out_kfree;
2441
2442 seq = file->private_data;
2443 seq->private = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444out:
2445 return rc;
2446out_kfree:
2447 kfree(s);
2448 goto out;
2449}
2450
2451static struct file_operations igmp6_mc_seq_fops = {
2452 .owner = THIS_MODULE,
2453 .open = igmp6_mc_seq_open,
2454 .read = seq_read,
2455 .llseek = seq_lseek,
2456 .release = seq_release_private,
2457};
2458
2459struct igmp6_mcf_iter_state {
2460 struct net_device *dev;
2461 struct inet6_dev *idev;
2462 struct ifmcaddr6 *im;
2463};
2464
2465#define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2466
2467static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2468{
2469 struct ip6_sf_list *psf = NULL;
2470 struct ifmcaddr6 *im = NULL;
2471 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2472
2473 for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
2474 state->dev;
2475 state->dev = state->dev->next) {
2476 struct inet6_dev *idev;
2477 idev = in6_dev_get(state->dev);
2478 if (unlikely(idev == NULL))
2479 continue;
2480 read_lock_bh(&idev->lock);
2481 im = idev->mc_list;
2482 if (likely(im != NULL)) {
2483 spin_lock_bh(&im->mca_lock);
2484 psf = im->mca_sources;
2485 if (likely(psf != NULL)) {
2486 state->im = im;
2487 state->idev = idev;
2488 break;
2489 }
2490 spin_unlock_bh(&im->mca_lock);
2491 }
2492 read_unlock_bh(&idev->lock);
2493 in6_dev_put(idev);
2494 }
2495 return psf;
2496}
2497
2498static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2499{
2500 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2501
2502 psf = psf->sf_next;
2503 while (!psf) {
2504 spin_unlock_bh(&state->im->mca_lock);
2505 state->im = state->im->next;
2506 while (!state->im) {
2507 if (likely(state->idev != NULL)) {
2508 read_unlock_bh(&state->idev->lock);
2509 in6_dev_put(state->idev);
2510 }
2511 state->dev = state->dev->next;
2512 if (!state->dev) {
2513 state->idev = NULL;
2514 goto out;
2515 }
2516 state->idev = in6_dev_get(state->dev);
2517 if (!state->idev)
2518 continue;
2519 read_lock_bh(&state->idev->lock);
2520 state->im = state->idev->mc_list;
2521 }
2522 if (!state->im)
2523 break;
2524 spin_lock_bh(&state->im->mca_lock);
2525 psf = state->im->mca_sources;
2526 }
2527out:
2528 return psf;
2529}
2530
2531static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2532{
2533 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2534 if (psf)
2535 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2536 --pos;
2537 return pos ? NULL : psf;
2538}
2539
2540static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2541{
2542 read_lock(&dev_base_lock);
2543 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2544}
2545
2546static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2547{
2548 struct ip6_sf_list *psf;
2549 if (v == SEQ_START_TOKEN)
2550 psf = igmp6_mcf_get_first(seq);
2551 else
2552 psf = igmp6_mcf_get_next(seq, v);
2553 ++*pos;
2554 return psf;
2555}
2556
2557static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2558{
2559 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2560 if (likely(state->im != NULL)) {
2561 spin_unlock_bh(&state->im->mca_lock);
2562 state->im = NULL;
2563 }
2564 if (likely(state->idev != NULL)) {
2565 read_unlock_bh(&state->idev->lock);
2566 in6_dev_put(state->idev);
2567 state->idev = NULL;
2568 }
2569 state->dev = NULL;
2570 read_unlock(&dev_base_lock);
2571}
2572
2573static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2574{
2575 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2576 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2577
2578 if (v == SEQ_START_TOKEN) {
2579 seq_printf(seq,
2580 "%3s %6s "
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002581 "%32s %32s %6s %6s\n", "Idx",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 "Device", "Multicast Address",
2583 "Source Address", "INC", "EXC");
2584 } else {
2585 seq_printf(seq,
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002586 "%3d %6.6s " NIP6_SEQFMT " " NIP6_SEQFMT " %6lu %6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 state->dev->ifindex, state->dev->name,
2588 NIP6(state->im->mca_addr),
2589 NIP6(psf->sf_addr),
2590 psf->sf_count[MCAST_INCLUDE],
2591 psf->sf_count[MCAST_EXCLUDE]);
2592 }
2593 return 0;
2594}
2595
2596static struct seq_operations igmp6_mcf_seq_ops = {
2597 .start = igmp6_mcf_seq_start,
2598 .next = igmp6_mcf_seq_next,
2599 .stop = igmp6_mcf_seq_stop,
2600 .show = igmp6_mcf_seq_show,
2601};
2602
2603static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2604{
2605 struct seq_file *seq;
2606 int rc = -ENOMEM;
Ingo Oeser0c600ed2006-03-20 23:01:32 -08002607 struct igmp6_mcf_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
2609 if (!s)
2610 goto out;
2611
2612 rc = seq_open(file, &igmp6_mcf_seq_ops);
2613 if (rc)
2614 goto out_kfree;
2615
2616 seq = file->private_data;
2617 seq->private = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618out:
2619 return rc;
2620out_kfree:
2621 kfree(s);
2622 goto out;
2623}
2624
2625static struct file_operations igmp6_mcf_seq_fops = {
2626 .owner = THIS_MODULE,
2627 .open = igmp6_mcf_seq_open,
2628 .read = seq_read,
2629 .llseek = seq_lseek,
2630 .release = seq_release_private,
2631};
2632#endif
2633
2634int __init igmp6_init(struct net_proto_family *ops)
2635{
2636 struct ipv6_pinfo *np;
2637 struct sock *sk;
2638 int err;
2639
2640 err = sock_create_kern(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6, &igmp6_socket);
2641 if (err < 0) {
2642 printk(KERN_ERR
2643 "Failed to initialize the IGMP6 control socket (err %d).\n",
2644 err);
2645 igmp6_socket = NULL; /* For safety. */
2646 return err;
2647 }
2648
2649 sk = igmp6_socket->sk;
2650 sk->sk_allocation = GFP_ATOMIC;
2651 sk->sk_prot->unhash(sk);
2652
2653 np = inet6_sk(sk);
2654 np->hop_limit = 1;
2655
2656#ifdef CONFIG_PROC_FS
2657 proc_net_fops_create("igmp6", S_IRUGO, &igmp6_mc_seq_fops);
2658 proc_net_fops_create("mcfilter6", S_IRUGO, &igmp6_mcf_seq_fops);
2659#endif
2660
2661 return 0;
2662}
2663
2664void igmp6_cleanup(void)
2665{
2666 sock_release(igmp6_socket);
2667 igmp6_socket = NULL; /* for safety */
2668
2669#ifdef CONFIG_PROC_FS
2670 proc_net_remove("mcfilter6");
2671 proc_net_remove("igmp6");
2672#endif
2673}