Merge branch 'master'
[linux-2.6] / net / ipv6 / mcast.c
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
84 struct 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
92 struct 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
101 struct 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
124 static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;
125
126 /* Big mc list lock for all the sockets */
127 static DEFINE_RWLOCK(ipv6_sk_mc_lock);
128
129 static struct socket *igmp6_socket;
130
131 int __ipv6_dev_mc_dec(struct inet6_dev *idev, struct in6_addr *addr);
132
133 static void igmp6_join_group(struct ifmcaddr6 *ma);
134 static void igmp6_leave_group(struct ifmcaddr6 *ma);
135 static void igmp6_timer_handler(unsigned long data);
136
137 static void mld_gq_timer_expire(unsigned long data);
138 static void mld_ifc_timer_expire(unsigned long data);
139 static void mld_ifc_event(struct inet6_dev *idev);
140 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
141 static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *addr);
142 static void mld_clear_delrec(struct inet6_dev *idev);
143 static int sf_setstate(struct ifmcaddr6 *pmc);
144 static void sf_markstate(struct ifmcaddr6 *pmc);
145 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
146 static 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);
149 static 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);
152 static 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))) << \
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        64
174
175 int sysctl_mld_max_msf = IPV6_MLD_MAX_MSF;
176
177 /*
178  *      socket join on multicast group
179  */
180
181 int 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
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
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         rwlock_init(&mc_lst->sflock);
228         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  */
255 int 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
285         return -EADDRNOTAVAIL;
286 }
287
288 static 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
322 void 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
353 int 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;
363         int leavegroup = 0;
364         int pmclocked = 0;
365         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
384         read_lock_bh(&ipv6_sk_mc_lock);
385         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         }
391         if (!pmc) {             /* must have a prior join */
392                 err = -EINVAL;
393                 goto done;
394         }
395         /* if a source filter was set, must be the same mode as before */
396         if (pmc->sflist) {
397                 if (pmc->sfmode != omode) {
398                         err = -EINVAL;
399                         goto done;
400                 }
401         } 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
408         write_lock_bh(&pmc->sflock);
409         pmclocked = 1;
410
411         psl = pmc->sflist;
412         if (!add) {
413                 if (!psl)
414                         goto done;      /* err = -EADDRNOTAVAIL */
415                 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 */
423                         goto done;      /* err = -EADDRNOTAVAIL */
424
425                 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
426                 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
427                         leavegroup = 1;
428                         goto done;
429                 }
430
431                 /* 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;
452                 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
453                 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);
481 done:
482         if (pmclocked)
483                 write_unlock_bh(&pmc->sflock);
484         read_unlock_bh(&ipv6_sk_mc_lock);
485         read_unlock_bh(&idev->lock);
486         in6_dev_put(idev);
487         dev_put(dev);
488         if (leavegroup)
489                 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
490         return err;
491 }
492
493 int 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;
501         int leavegroup = 0;
502         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;
517
518         err = 0;
519         read_lock_bh(&ipv6_sk_mc_lock);
520
521         if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
522                 leavegroup = 1;
523                 goto done;
524         }
525
526         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         }
532         if (!pmc) {             /* must have a prior join */
533                 err = -EINVAL;
534                 goto done;
535         }
536         if (gsf->gf_numsrc) {
537                 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
538                                                           GFP_ATOMIC);
539                 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                 }
556         } else {
557                 newpsl = NULL;
558                 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
559         }
560
561         write_lock_bh(&pmc->sflock);
562         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;
571         write_unlock_bh(&pmc->sflock);
572         err = 0;
573 done:
574         read_unlock_bh(&ipv6_sk_mc_lock);
575         read_unlock_bh(&idev->lock);
576         in6_dev_put(idev);
577         dev_put(dev);
578         if (leavegroup)
579                 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
580         return err;
581 }
582
583 int 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;
607         /*
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          */
612
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         }
634         /* 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          */
638         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;
650 done:
651         read_unlock_bh(&idev->lock);
652         in6_dev_put(idev);
653         dev_put(dev);
654         return err;
655 }
656
657 int 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         }
674         read_lock(&mc->sflock);
675         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         }
690         read_unlock(&mc->sflock);
691         read_unlock(&ipv6_sk_mc_lock);
692
693         return rv;
694 }
695
696 static 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
704 static 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
730 static 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);
752 done:
753         ip6_mc_clear_src(mc);
754         spin_unlock_bh(&mc->mca_lock);
755 }
756
757 /*
758  * deleted ifmcaddr6 manipulation
759  */
760 static 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          */
770         pmc = kmalloc(sizeof(*pmc), GFP_ATOMIC);
771         if (!pmc)
772                 return;
773         memset(pmc, 0, sizeof(*pmc));
774         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
798 static 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
827 static 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  */
864 int 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
896         mc = kmalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
897
898         if (mc == NULL) {
899                 write_unlock_bh(&idev->lock);
900                 in6_dev_put(idev);
901                 return -ENOMEM;
902         }
903
904         memset(mc, 0, sizeof(struct ifmcaddr6));
905         init_timer(&mc->mca_timer);
906         mc->mca_timer.function = igmp6_timer_handler;
907         mc->mca_timer.data = (unsigned long) mc;
908
909         ipv6_addr_copy(&mc->mca_addr, addr);
910         mc->idev = idev;
911         mc->mca_users = 1;
912         /* mca_stamp should be updated upon changes */
913         mc->mca_cstamp = mc->mca_tstamp = jiffies;
914         atomic_set(&mc->mca_refcnt, 2);
915         spin_lock_init(&mc->mca_lock);
916
917         /* initial mode is (EX, empty) */
918         mc->mca_sfmode = MCAST_EXCLUDE;
919         mc->mca_sfcount[MCAST_EXCLUDE] = 1;
920
921         if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
922             IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
923                 mc->mca_flags |= MAF_NOREPORT;
924
925         mc->next = idev->mc_list;
926         idev->mc_list = mc;
927         write_unlock_bh(&idev->lock);
928
929         mld_del_delrec(idev, &mc->mca_addr);
930         igmp6_group_added(mc);
931         ma_put(mc);
932         return 0;
933 }
934
935 /*
936  *      device multicast group del
937  */
938 int __ipv6_dev_mc_dec(struct inet6_dev *idev, struct in6_addr *addr)
939 {
940         struct ifmcaddr6 *ma, **map;
941
942         write_lock_bh(&idev->lock);
943         for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
944                 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
945                         if (--ma->mca_users == 0) {
946                                 *map = ma->next;
947                                 write_unlock_bh(&idev->lock);
948
949                                 igmp6_group_dropped(ma);
950
951                                 ma_put(ma);
952                                 return 0;
953                         }
954                         write_unlock_bh(&idev->lock);
955                         return 0;
956                 }
957         }
958         write_unlock_bh(&idev->lock);
959
960         return -ENOENT;
961 }
962
963 int ipv6_dev_mc_dec(struct net_device *dev, struct in6_addr *addr)
964 {
965         struct inet6_dev *idev = in6_dev_get(dev);
966         int err;
967
968         if (!idev)
969                 return -ENODEV;
970
971         err = __ipv6_dev_mc_dec(idev, addr);
972
973         in6_dev_put(idev);
974
975         return err;
976 }
977
978 /*
979  * identify MLD packets for MLD filter exceptions
980  */
981 int ipv6_is_mld(struct sk_buff *skb, int nexthdr)
982 {
983         struct icmp6hdr *pic;
984
985         if (nexthdr != IPPROTO_ICMPV6)
986                 return 0;
987
988         if (!pskb_may_pull(skb, sizeof(struct icmp6hdr)))
989                 return 0;
990
991         pic = (struct icmp6hdr *)skb->h.raw;
992
993         switch (pic->icmp6_type) {
994         case ICMPV6_MGM_QUERY:
995         case ICMPV6_MGM_REPORT:
996         case ICMPV6_MGM_REDUCTION:
997         case ICMPV6_MLD2_REPORT:
998                 return 1;
999         default:
1000                 break;
1001         }
1002         return 0;
1003 }
1004
1005 /*
1006  *      check if the interface/address pair is valid
1007  */
1008 int ipv6_chk_mcast_addr(struct net_device *dev, struct in6_addr *group,
1009         struct in6_addr *src_addr)
1010 {
1011         struct inet6_dev *idev;
1012         struct ifmcaddr6 *mc;
1013         int rv = 0;
1014
1015         idev = in6_dev_get(dev);
1016         if (idev) {
1017                 read_lock_bh(&idev->lock);
1018                 for (mc = idev->mc_list; mc; mc=mc->next) {
1019                         if (ipv6_addr_equal(&mc->mca_addr, group))
1020                                 break;
1021                 }
1022                 if (mc) {
1023                         if (src_addr && !ipv6_addr_any(src_addr)) {
1024                                 struct ip6_sf_list *psf;
1025
1026                                 spin_lock_bh(&mc->mca_lock);
1027                                 for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
1028                                         if (ipv6_addr_equal(&psf->sf_addr, src_addr))
1029                                                 break;
1030                                 }
1031                                 if (psf)
1032                                         rv = psf->sf_count[MCAST_INCLUDE] ||
1033                                                 psf->sf_count[MCAST_EXCLUDE] !=
1034                                                 mc->mca_sfcount[MCAST_EXCLUDE];
1035                                 else
1036                                         rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
1037                                 spin_unlock_bh(&mc->mca_lock);
1038                         } else
1039                                 rv = 1; /* don't filter unspecified source */
1040                 }
1041                 read_unlock_bh(&idev->lock);
1042                 in6_dev_put(idev);
1043         }
1044         return rv;
1045 }
1046
1047 static void mld_gq_start_timer(struct inet6_dev *idev)
1048 {
1049         int tv = net_random() % idev->mc_maxdelay;
1050
1051         idev->mc_gq_running = 1;
1052         if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
1053                 in6_dev_hold(idev);
1054 }
1055
1056 static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
1057 {
1058         int tv = net_random() % delay;
1059
1060         if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1061                 in6_dev_hold(idev);
1062 }
1063
1064 /*
1065  *      IGMP handling (alias multicast ICMPv6 messages)
1066  */
1067
1068 static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1069 {
1070         unsigned long delay = resptime;
1071
1072         /* Do not start timer for these addresses */
1073         if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1074             IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1075                 return;
1076
1077         if (del_timer(&ma->mca_timer)) {
1078                 atomic_dec(&ma->mca_refcnt);
1079                 delay = ma->mca_timer.expires - jiffies;
1080         }
1081
1082         if (delay >= resptime) {
1083                 if (resptime)
1084                         delay = net_random() % resptime;
1085                 else
1086                         delay = 1;
1087         }
1088         ma->mca_timer.expires = jiffies + delay;
1089         if (!mod_timer(&ma->mca_timer, jiffies + delay))
1090                 atomic_inc(&ma->mca_refcnt);
1091         ma->mca_flags |= MAF_TIMER_RUNNING;
1092 }
1093
1094 /* mark EXCLUDE-mode sources */
1095 static int mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
1096         struct in6_addr *srcs)
1097 {
1098         struct ip6_sf_list *psf;
1099         int i, scount;
1100
1101         scount = 0;
1102         for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1103                 if (scount == nsrcs)
1104                         break;
1105                 for (i=0; i<nsrcs; i++) {
1106                         /* skip inactive filters */
1107                         if (pmc->mca_sfcount[MCAST_INCLUDE] ||
1108                             pmc->mca_sfcount[MCAST_EXCLUDE] !=
1109                             psf->sf_count[MCAST_EXCLUDE])
1110                                 continue;
1111                         if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1112                                 scount++;
1113                                 break;
1114                         }
1115                 }
1116         }
1117         pmc->mca_flags &= ~MAF_GSQUERY;
1118         if (scount == nsrcs)    /* all sources excluded */
1119                 return 0;
1120         return 1;
1121 }
1122
1123 static int mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1124         struct in6_addr *srcs)
1125 {
1126         struct ip6_sf_list *psf;
1127         int i, scount;
1128
1129         if (pmc->mca_sfmode == MCAST_EXCLUDE)
1130                 return mld_xmarksources(pmc, nsrcs, srcs);
1131
1132         /* mark INCLUDE-mode sources */
1133
1134         scount = 0;
1135         for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1136                 if (scount == nsrcs)
1137                         break;
1138                 for (i=0; i<nsrcs; i++) {
1139                         if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1140                                 psf->sf_gsresp = 1;
1141                                 scount++;
1142                                 break;
1143                         }
1144                 }
1145         }
1146         if (!scount) {
1147                 pmc->mca_flags &= ~MAF_GSQUERY;
1148                 return 0;
1149         }
1150         pmc->mca_flags |= MAF_GSQUERY;
1151         return 1;
1152 }
1153
1154 int igmp6_event_query(struct sk_buff *skb)
1155 {
1156         struct mld2_query *mlh2 = NULL;
1157         struct ifmcaddr6 *ma;
1158         struct in6_addr *group;
1159         unsigned long max_delay;
1160         struct inet6_dev *idev;
1161         struct icmp6hdr *hdr;
1162         int group_type;
1163         int mark = 0;
1164         int len;
1165
1166         if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1167                 return -EINVAL;
1168
1169         /* compute payload length excluding extension headers */
1170         len = ntohs(skb->nh.ipv6h->payload_len) + sizeof(struct ipv6hdr);
1171         len -= (char *)skb->h.raw - (char *)skb->nh.ipv6h; 
1172
1173         /* Drop queries with not link local source */
1174         if (!(ipv6_addr_type(&skb->nh.ipv6h->saddr)&IPV6_ADDR_LINKLOCAL))
1175                 return -EINVAL;
1176
1177         idev = in6_dev_get(skb->dev);
1178
1179         if (idev == NULL)
1180                 return 0;
1181
1182         hdr = (struct icmp6hdr *) skb->h.raw;
1183         group = (struct in6_addr *) (hdr + 1);
1184         group_type = ipv6_addr_type(group);
1185
1186         if (group_type != IPV6_ADDR_ANY &&
1187             !(group_type&IPV6_ADDR_MULTICAST)) {
1188                 in6_dev_put(idev);
1189                 return -EINVAL;
1190         }
1191
1192         if (len == 24) {
1193                 int switchback;
1194                 /* MLDv1 router present */
1195
1196                 /* Translate milliseconds to jiffies */
1197                 max_delay = (ntohs(hdr->icmp6_maxdelay)*HZ)/1000;
1198
1199                 switchback = (idev->mc_qrv + 1) * max_delay;
1200                 idev->mc_v1_seen = jiffies + switchback;
1201
1202                 /* cancel the interface change timer */
1203                 idev->mc_ifc_count = 0;
1204                 if (del_timer(&idev->mc_ifc_timer))
1205                         __in6_dev_put(idev);
1206                 /* clear deleted report items */
1207                 mld_clear_delrec(idev);
1208         } else if (len >= 28) {
1209                 int srcs_offset = sizeof(struct mld2_query) - 
1210                                   sizeof(struct icmp6hdr);
1211                 if (!pskb_may_pull(skb, srcs_offset)) {
1212                         in6_dev_put(idev);
1213                         return -EINVAL;
1214                 }
1215                 mlh2 = (struct mld2_query *) skb->h.raw;
1216                 max_delay = (MLDV2_MRC(ntohs(mlh2->mrc))*HZ)/1000;
1217                 if (!max_delay)
1218                         max_delay = 1;
1219                 idev->mc_maxdelay = max_delay;
1220                 if (mlh2->qrv)
1221                         idev->mc_qrv = mlh2->qrv;
1222                 if (group_type == IPV6_ADDR_ANY) { /* general query */
1223                         if (mlh2->nsrcs) {
1224                                 in6_dev_put(idev);
1225                                 return -EINVAL; /* no sources allowed */
1226                         }
1227                         mld_gq_start_timer(idev);
1228                         in6_dev_put(idev);
1229                         return 0;
1230                 }
1231                 /* mark sources to include, if group & source-specific */
1232                 if (mlh2->nsrcs != 0) {
1233                         if (!pskb_may_pull(skb, srcs_offset + 
1234                             ntohs(mlh2->nsrcs) * sizeof(struct in6_addr))) {
1235                                 in6_dev_put(idev);
1236                                 return -EINVAL;
1237                         }
1238                         mlh2 = (struct mld2_query *) skb->h.raw;
1239                         mark = 1;
1240                 }
1241         } else {
1242                 in6_dev_put(idev);
1243                 return -EINVAL;
1244         }
1245
1246         read_lock_bh(&idev->lock);
1247         if (group_type == IPV6_ADDR_ANY) {
1248                 for (ma = idev->mc_list; ma; ma=ma->next) {
1249                         spin_lock_bh(&ma->mca_lock);
1250                         igmp6_group_queried(ma, max_delay);
1251                         spin_unlock_bh(&ma->mca_lock);
1252                 }
1253         } else {
1254                 for (ma = idev->mc_list; ma; ma=ma->next) {
1255                         if (group_type != IPV6_ADDR_ANY &&
1256                             !ipv6_addr_equal(group, &ma->mca_addr))
1257                                 continue;
1258                         spin_lock_bh(&ma->mca_lock);
1259                         if (ma->mca_flags & MAF_TIMER_RUNNING) {
1260                                 /* gsquery <- gsquery && mark */
1261                                 if (!mark)
1262                                         ma->mca_flags &= ~MAF_GSQUERY;
1263                         } else {
1264                                 /* gsquery <- mark */
1265                                 if (mark)
1266                                         ma->mca_flags |= MAF_GSQUERY;
1267                                 else
1268                                         ma->mca_flags &= ~MAF_GSQUERY;
1269                         }
1270                         if (!(ma->mca_flags & MAF_GSQUERY) ||
1271                            mld_marksources(ma, ntohs(mlh2->nsrcs), mlh2->srcs))
1272                                 igmp6_group_queried(ma, max_delay);
1273                         spin_unlock_bh(&ma->mca_lock);
1274                         if (group_type != IPV6_ADDR_ANY)
1275                                 break;
1276                 }
1277         }
1278         read_unlock_bh(&idev->lock);
1279         in6_dev_put(idev);
1280
1281         return 0;
1282 }
1283
1284
1285 int igmp6_event_report(struct sk_buff *skb)
1286 {
1287         struct ifmcaddr6 *ma;
1288         struct in6_addr *addrp;
1289         struct inet6_dev *idev;
1290         struct icmp6hdr *hdr;
1291         int addr_type;
1292
1293         /* Our own report looped back. Ignore it. */
1294         if (skb->pkt_type == PACKET_LOOPBACK)
1295                 return 0;
1296
1297         /* send our report if the MC router may not have heard this report */
1298         if (skb->pkt_type != PACKET_MULTICAST &&
1299             skb->pkt_type != PACKET_BROADCAST)
1300                 return 0;
1301
1302         if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1303                 return -EINVAL;
1304
1305         hdr = (struct icmp6hdr*) skb->h.raw;
1306
1307         /* Drop reports with not link local source */
1308         addr_type = ipv6_addr_type(&skb->nh.ipv6h->saddr);
1309         if (addr_type != IPV6_ADDR_ANY && 
1310             !(addr_type&IPV6_ADDR_LINKLOCAL))
1311                 return -EINVAL;
1312
1313         addrp = (struct in6_addr *) (hdr + 1);
1314
1315         idev = in6_dev_get(skb->dev);
1316         if (idev == NULL)
1317                 return -ENODEV;
1318
1319         /*
1320          *      Cancel the timer for this group
1321          */
1322
1323         read_lock_bh(&idev->lock);
1324         for (ma = idev->mc_list; ma; ma=ma->next) {
1325                 if (ipv6_addr_equal(&ma->mca_addr, addrp)) {
1326                         spin_lock(&ma->mca_lock);
1327                         if (del_timer(&ma->mca_timer))
1328                                 atomic_dec(&ma->mca_refcnt);
1329                         ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1330                         spin_unlock(&ma->mca_lock);
1331                         break;
1332                 }
1333         }
1334         read_unlock_bh(&idev->lock);
1335         in6_dev_put(idev);
1336         return 0;
1337 }
1338
1339 static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1340         int gdeleted, int sdeleted)
1341 {
1342         switch (type) {
1343         case MLD2_MODE_IS_INCLUDE:
1344         case MLD2_MODE_IS_EXCLUDE:
1345                 if (gdeleted || sdeleted)
1346                         return 0;
1347                 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1348                         if (pmc->mca_sfmode == MCAST_INCLUDE)
1349                                 return 1;
1350                         /* don't include if this source is excluded
1351                          * in all filters
1352                          */
1353                         if (psf->sf_count[MCAST_INCLUDE])
1354                                 return 0;
1355                         return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1356                                 psf->sf_count[MCAST_EXCLUDE];
1357                 }
1358                 return 0;
1359         case MLD2_CHANGE_TO_INCLUDE:
1360                 if (gdeleted || sdeleted)
1361                         return 0;
1362                 return psf->sf_count[MCAST_INCLUDE] != 0;
1363         case MLD2_CHANGE_TO_EXCLUDE:
1364                 if (gdeleted || sdeleted)
1365                         return 0;
1366                 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1367                     psf->sf_count[MCAST_INCLUDE])
1368                         return 0;
1369                 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1370                         psf->sf_count[MCAST_EXCLUDE];
1371         case MLD2_ALLOW_NEW_SOURCES:
1372                 if (gdeleted || !psf->sf_crcount)
1373                         return 0;
1374                 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1375         case MLD2_BLOCK_OLD_SOURCES:
1376                 if (pmc->mca_sfmode == MCAST_INCLUDE)
1377                         return gdeleted || (psf->sf_crcount && sdeleted);
1378                 return psf->sf_crcount && !gdeleted && !sdeleted;
1379         }
1380         return 0;
1381 }
1382
1383 static int
1384 mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1385 {
1386         struct ip6_sf_list *psf;
1387         int scount = 0;
1388
1389         for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1390                 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1391                         continue;
1392                 scount++;
1393         }
1394         return scount;
1395 }
1396
1397 static struct sk_buff *mld_newpack(struct net_device *dev, int size)
1398 {
1399         struct sock *sk = igmp6_socket->sk;
1400         struct sk_buff *skb;
1401         struct mld2_report *pmr;
1402         struct in6_addr addr_buf;
1403         int err;
1404         u8 ra[8] = { IPPROTO_ICMPV6, 0,
1405                      IPV6_TLV_ROUTERALERT, 2, 0, 0,
1406                      IPV6_TLV_PADN, 0 };
1407
1408         /* we assume size > sizeof(ra) here */
1409         skb = sock_alloc_send_skb(sk, size + LL_RESERVED_SPACE(dev), 1, &err);
1410
1411         if (skb == 0)
1412                 return NULL;
1413
1414         skb_reserve(skb, LL_RESERVED_SPACE(dev));
1415
1416         if (ipv6_get_lladdr(dev, &addr_buf)) {
1417                 /* <draft-ietf-magma-mld-source-05.txt>:
1418                  * use unspecified address as the source address 
1419                  * when a valid link-local address is not available.
1420                  */
1421                 memset(&addr_buf, 0, sizeof(addr_buf));
1422         }
1423
1424         ip6_nd_hdr(sk, skb, dev, &addr_buf, &mld2_all_mcr, NEXTHDR_HOP, 0);
1425
1426         memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1427
1428         pmr =(struct mld2_report *)skb_put(skb, sizeof(*pmr));
1429         skb->h.raw = (unsigned char *)pmr;
1430         pmr->type = ICMPV6_MLD2_REPORT;
1431         pmr->resv1 = 0;
1432         pmr->csum = 0;
1433         pmr->resv2 = 0;
1434         pmr->ngrec = 0;
1435         return skb;
1436 }
1437
1438 static inline int mld_dev_queue_xmit2(struct sk_buff *skb)
1439 {
1440         struct net_device *dev = skb->dev;
1441
1442         if (dev->hard_header) {
1443                 unsigned char ha[MAX_ADDR_LEN];
1444                 int err;
1445
1446                 ndisc_mc_map(&skb->nh.ipv6h->daddr, ha, dev, 1);
1447                 err = dev->hard_header(skb, dev, ETH_P_IPV6, ha, NULL, skb->len);
1448                 if (err < 0) {
1449                         kfree_skb(skb);
1450                         return err;
1451                 }
1452         }
1453         return dev_queue_xmit(skb);
1454 }
1455
1456 static inline int mld_dev_queue_xmit(struct sk_buff *skb)
1457 {
1458         return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL, skb->dev,
1459                        mld_dev_queue_xmit2);
1460 }
1461
1462 static void mld_sendpack(struct sk_buff *skb)
1463 {
1464         struct ipv6hdr *pip6 = skb->nh.ipv6h;
1465         struct mld2_report *pmr = (struct mld2_report *)skb->h.raw;
1466         int payload_len, mldlen;
1467         struct inet6_dev *idev = in6_dev_get(skb->dev);
1468         int err;
1469
1470         IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
1471         payload_len = skb->tail - (unsigned char *)skb->nh.ipv6h -
1472                 sizeof(struct ipv6hdr);
1473         mldlen = skb->tail - skb->h.raw;
1474         pip6->payload_len = htons(payload_len);
1475
1476         pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1477                 IPPROTO_ICMPV6, csum_partial(skb->h.raw, mldlen, 0));
1478         err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
1479                 mld_dev_queue_xmit);
1480         if (!err) {
1481                 ICMP6_INC_STATS(idev,ICMP6_MIB_OUTMSGS);
1482                 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
1483         } else
1484                 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1485
1486         if (likely(idev != NULL))
1487                 in6_dev_put(idev);
1488 }
1489
1490 static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1491 {
1492         return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
1493 }
1494
1495 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1496         int type, struct mld2_grec **ppgr)
1497 {
1498         struct net_device *dev = pmc->idev->dev;
1499         struct mld2_report *pmr;
1500         struct mld2_grec *pgr;
1501
1502         if (!skb)
1503                 skb = mld_newpack(dev, dev->mtu);
1504         if (!skb)
1505                 return NULL;
1506         pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1507         pgr->grec_type = type;
1508         pgr->grec_auxwords = 0;
1509         pgr->grec_nsrcs = 0;
1510         pgr->grec_mca = pmc->mca_addr;  /* structure copy */
1511         pmr = (struct mld2_report *)skb->h.raw;
1512         pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
1513         *ppgr = pgr;
1514         return skb;
1515 }
1516
1517 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1518         skb_tailroom(skb)) : 0)
1519
1520 static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1521         int type, int gdeleted, int sdeleted)
1522 {
1523         struct net_device *dev = pmc->idev->dev;
1524         struct mld2_report *pmr;
1525         struct mld2_grec *pgr = NULL;
1526         struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
1527         int scount, stotal, first, isquery, truncate;
1528
1529         if (pmc->mca_flags & MAF_NOREPORT)
1530                 return skb;
1531
1532         isquery = type == MLD2_MODE_IS_INCLUDE ||
1533                   type == MLD2_MODE_IS_EXCLUDE;
1534         truncate = type == MLD2_MODE_IS_EXCLUDE ||
1535                     type == MLD2_CHANGE_TO_EXCLUDE;
1536
1537         stotal = scount = 0;
1538
1539         psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1540
1541         if (!*psf_list)
1542                 goto empty_source;
1543
1544         pmr = skb ? (struct mld2_report *)skb->h.raw : NULL;
1545
1546         /* EX and TO_EX get a fresh packet, if needed */
1547         if (truncate) {
1548                 if (pmr && pmr->ngrec &&
1549                     AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1550                         if (skb)
1551                                 mld_sendpack(skb);
1552                         skb = mld_newpack(dev, dev->mtu);
1553                 }
1554         }
1555         first = 1;
1556         psf_prev = NULL;
1557         for (psf=*psf_list; psf; psf=psf_next) {
1558                 struct in6_addr *psrc;
1559
1560                 psf_next = psf->sf_next;
1561
1562                 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1563                         psf_prev = psf;
1564                         continue;
1565                 }
1566
1567                 /* clear marks on query responses */
1568                 if (isquery)
1569                         psf->sf_gsresp = 0;
1570
1571                 if (AVAILABLE(skb) < sizeof(*psrc) +
1572                     first*sizeof(struct mld2_grec)) {
1573                         if (truncate && !first)
1574                                 break;   /* truncate these */
1575                         if (pgr)
1576                                 pgr->grec_nsrcs = htons(scount);
1577                         if (skb)
1578                                 mld_sendpack(skb);
1579                         skb = mld_newpack(dev, dev->mtu);
1580                         first = 1;
1581                         scount = 0;
1582                 }
1583                 if (first) {
1584                         skb = add_grhead(skb, pmc, type, &pgr);
1585                         first = 0;
1586                 }
1587                 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1588                 *psrc = psf->sf_addr;
1589                 scount++; stotal++;
1590                 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1591                      type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1592                         psf->sf_crcount--;
1593                         if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1594                                 if (psf_prev)
1595                                         psf_prev->sf_next = psf->sf_next;
1596                                 else
1597                                         *psf_list = psf->sf_next;
1598                                 kfree(psf);
1599                                 continue;
1600                         }
1601                 }
1602                 psf_prev = psf;
1603         }
1604
1605 empty_source:
1606         if (!stotal) {
1607                 if (type == MLD2_ALLOW_NEW_SOURCES ||
1608                     type == MLD2_BLOCK_OLD_SOURCES)
1609                         return skb;
1610                 if (pmc->mca_crcount || isquery) {
1611                         /* make sure we have room for group header */
1612                         if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1613                                 mld_sendpack(skb);
1614                                 skb = NULL; /* add_grhead will get a new one */
1615                         }
1616                         skb = add_grhead(skb, pmc, type, &pgr);
1617                 }
1618         }
1619         if (pgr)
1620                 pgr->grec_nsrcs = htons(scount);
1621
1622         if (isquery)
1623                 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1624         return skb;
1625 }
1626
1627 static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1628 {
1629         struct sk_buff *skb = NULL;
1630         int type;
1631
1632         if (!pmc) {
1633                 read_lock_bh(&idev->lock);
1634                 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1635                         if (pmc->mca_flags & MAF_NOREPORT)
1636                                 continue;
1637                         spin_lock_bh(&pmc->mca_lock);
1638                         if (pmc->mca_sfcount[MCAST_EXCLUDE])
1639                                 type = MLD2_MODE_IS_EXCLUDE;
1640                         else
1641                                 type = MLD2_MODE_IS_INCLUDE;
1642                         skb = add_grec(skb, pmc, type, 0, 0);
1643                         spin_unlock_bh(&pmc->mca_lock);
1644                 }
1645                 read_unlock_bh(&idev->lock);
1646         } else {
1647                 spin_lock_bh(&pmc->mca_lock);
1648                 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1649                         type = MLD2_MODE_IS_EXCLUDE;
1650                 else
1651                         type = MLD2_MODE_IS_INCLUDE;
1652                 skb = add_grec(skb, pmc, type, 0, 0);
1653                 spin_unlock_bh(&pmc->mca_lock);
1654         }
1655         if (skb)
1656                 mld_sendpack(skb);
1657 }
1658
1659 /*
1660  * remove zero-count source records from a source filter list
1661  */
1662 static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1663 {
1664         struct ip6_sf_list *psf_prev, *psf_next, *psf;
1665
1666         psf_prev = NULL;
1667         for (psf=*ppsf; psf; psf = psf_next) {
1668                 psf_next = psf->sf_next;
1669                 if (psf->sf_crcount == 0) {
1670                         if (psf_prev)
1671                                 psf_prev->sf_next = psf->sf_next;
1672                         else
1673                                 *ppsf = psf->sf_next;
1674                         kfree(psf);
1675                 } else
1676                         psf_prev = psf;
1677         }
1678 }
1679
1680 static void mld_send_cr(struct inet6_dev *idev)
1681 {
1682         struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1683         struct sk_buff *skb = NULL;
1684         int type, dtype;
1685
1686         read_lock_bh(&idev->lock);
1687         write_lock_bh(&idev->mc_lock);
1688
1689         /* deleted MCA's */
1690         pmc_prev = NULL;
1691         for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1692                 pmc_next = pmc->next;
1693                 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1694                         type = MLD2_BLOCK_OLD_SOURCES;
1695                         dtype = MLD2_BLOCK_OLD_SOURCES;
1696                         skb = add_grec(skb, pmc, type, 1, 0);
1697                         skb = add_grec(skb, pmc, dtype, 1, 1);
1698                 }
1699                 if (pmc->mca_crcount) {
1700                         if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1701                                 type = MLD2_CHANGE_TO_INCLUDE;
1702                                 skb = add_grec(skb, pmc, type, 1, 0);
1703                         }
1704                         pmc->mca_crcount--;
1705                         if (pmc->mca_crcount == 0) {
1706                                 mld_clear_zeros(&pmc->mca_tomb);
1707                                 mld_clear_zeros(&pmc->mca_sources);
1708                         }
1709                 }
1710                 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1711                     !pmc->mca_sources) {
1712                         if (pmc_prev)
1713                                 pmc_prev->next = pmc_next;
1714                         else
1715                                 idev->mc_tomb = pmc_next;
1716                         in6_dev_put(pmc->idev);
1717                         kfree(pmc);
1718                 } else
1719                         pmc_prev = pmc;
1720         }
1721         write_unlock_bh(&idev->mc_lock);
1722
1723         /* change recs */
1724         for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1725                 spin_lock_bh(&pmc->mca_lock);
1726                 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1727                         type = MLD2_BLOCK_OLD_SOURCES;
1728                         dtype = MLD2_ALLOW_NEW_SOURCES;
1729                 } else {
1730                         type = MLD2_ALLOW_NEW_SOURCES;
1731                         dtype = MLD2_BLOCK_OLD_SOURCES;
1732                 }
1733                 skb = add_grec(skb, pmc, type, 0, 0);
1734                 skb = add_grec(skb, pmc, dtype, 0, 1);  /* deleted sources */
1735
1736                 /* filter mode changes */
1737                 if (pmc->mca_crcount) {
1738                         if (pmc->mca_sfmode == MCAST_EXCLUDE)
1739                                 type = MLD2_CHANGE_TO_EXCLUDE;
1740                         else
1741                                 type = MLD2_CHANGE_TO_INCLUDE;
1742                         skb = add_grec(skb, pmc, type, 0, 0);
1743                         pmc->mca_crcount--;
1744                 }
1745                 spin_unlock_bh(&pmc->mca_lock);
1746         }
1747         read_unlock_bh(&idev->lock);
1748         if (!skb)
1749                 return;
1750         (void) mld_sendpack(skb);
1751 }
1752
1753 static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1754 {
1755         struct sock *sk = igmp6_socket->sk;
1756         struct inet6_dev *idev;
1757         struct sk_buff *skb;
1758         struct icmp6hdr *hdr;
1759         struct in6_addr *snd_addr;
1760         struct in6_addr *addrp;
1761         struct in6_addr addr_buf;
1762         struct in6_addr all_routers;
1763         int err, len, payload_len, full_len;
1764         u8 ra[8] = { IPPROTO_ICMPV6, 0,
1765                      IPV6_TLV_ROUTERALERT, 2, 0, 0,
1766                      IPV6_TLV_PADN, 0 };
1767
1768         IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
1769         snd_addr = addr;
1770         if (type == ICMPV6_MGM_REDUCTION) {
1771                 snd_addr = &all_routers;
1772                 ipv6_addr_all_routers(&all_routers);
1773         }
1774
1775         len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1776         payload_len = len + sizeof(ra);
1777         full_len = sizeof(struct ipv6hdr) + payload_len;
1778
1779         skb = sock_alloc_send_skb(sk, LL_RESERVED_SPACE(dev) + full_len, 1, &err);
1780
1781         if (skb == NULL) {
1782                 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1783                 return;
1784         }
1785
1786         skb_reserve(skb, LL_RESERVED_SPACE(dev));
1787
1788         if (ipv6_get_lladdr(dev, &addr_buf)) {
1789                 /* <draft-ietf-magma-mld-source-05.txt>:
1790                  * use unspecified address as the source address 
1791                  * when a valid link-local address is not available.
1792                  */
1793                 memset(&addr_buf, 0, sizeof(addr_buf));
1794         }
1795
1796         ip6_nd_hdr(sk, skb, dev, &addr_buf, snd_addr, NEXTHDR_HOP, payload_len);
1797
1798         memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1799
1800         hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
1801         memset(hdr, 0, sizeof(struct icmp6hdr));
1802         hdr->icmp6_type = type;
1803
1804         addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
1805         ipv6_addr_copy(addrp, addr);
1806
1807         hdr->icmp6_cksum = csum_ipv6_magic(&addr_buf, snd_addr, len,
1808                                            IPPROTO_ICMPV6,
1809                                            csum_partial((__u8 *) hdr, len, 0));
1810
1811         idev = in6_dev_get(skb->dev);
1812
1813         err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
1814                 mld_dev_queue_xmit);
1815         if (!err) {
1816                 if (type == ICMPV6_MGM_REDUCTION)
1817                         ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBREDUCTIONS);
1818                 else
1819                         ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBRESPONSES);
1820                 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTMSGS);
1821                 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
1822         } else
1823                 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1824
1825         if (likely(idev != NULL))
1826                 in6_dev_put(idev);
1827         return;
1828 }
1829
1830 static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1831         struct in6_addr *psfsrc)
1832 {
1833         struct ip6_sf_list *psf, *psf_prev;
1834         int rv = 0;
1835
1836         psf_prev = NULL;
1837         for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1838                 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1839                         break;
1840                 psf_prev = psf;
1841         }
1842         if (!psf || psf->sf_count[sfmode] == 0) {
1843                 /* source filter not found, or count wrong =>  bug */
1844                 return -ESRCH;
1845         }
1846         psf->sf_count[sfmode]--;
1847         if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1848                 struct inet6_dev *idev = pmc->idev;
1849
1850                 /* no more filters for this source */
1851                 if (psf_prev)
1852                         psf_prev->sf_next = psf->sf_next;
1853                 else
1854                         pmc->mca_sources = psf->sf_next;
1855                 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1856                     !MLD_V1_SEEN(idev)) {
1857                         psf->sf_crcount = idev->mc_qrv;
1858                         psf->sf_next = pmc->mca_tomb;
1859                         pmc->mca_tomb = psf;
1860                         rv = 1;
1861                 } else
1862                         kfree(psf);
1863         }
1864         return rv;
1865 }
1866
1867 static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
1868                           int sfmode, int sfcount, struct in6_addr *psfsrc,
1869                           int delta)
1870 {
1871         struct ifmcaddr6 *pmc;
1872         int     changerec = 0;
1873         int     i, err;
1874
1875         if (!idev)
1876                 return -ENODEV;
1877         read_lock_bh(&idev->lock);
1878         for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1879                 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1880                         break;
1881         }
1882         if (!pmc) {
1883                 /* MCA not found?? bug */
1884                 read_unlock_bh(&idev->lock);
1885                 return -ESRCH;
1886         }
1887         spin_lock_bh(&pmc->mca_lock);
1888         sf_markstate(pmc);
1889         if (!delta) {
1890                 if (!pmc->mca_sfcount[sfmode]) {
1891                         spin_unlock_bh(&pmc->mca_lock);
1892                         read_unlock_bh(&idev->lock);
1893                         return -EINVAL;
1894                 }
1895                 pmc->mca_sfcount[sfmode]--;
1896         }
1897         err = 0;
1898         for (i=0; i<sfcount; i++) {
1899                 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1900
1901                 changerec |= rv > 0;
1902                 if (!err && rv < 0)
1903                         err = rv;
1904         }
1905         if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1906             pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1907             pmc->mca_sfcount[MCAST_INCLUDE]) {
1908                 struct ip6_sf_list *psf;
1909
1910                 /* filter mode change */
1911                 pmc->mca_sfmode = MCAST_INCLUDE;
1912                 pmc->mca_crcount = idev->mc_qrv;
1913                 idev->mc_ifc_count = pmc->mca_crcount;
1914                 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1915                         psf->sf_crcount = 0;
1916                 mld_ifc_event(pmc->idev);
1917         } else if (sf_setstate(pmc) || changerec)
1918                 mld_ifc_event(pmc->idev);
1919         spin_unlock_bh(&pmc->mca_lock);
1920         read_unlock_bh(&idev->lock);
1921         return err;
1922 }
1923
1924 /*
1925  * Add multicast single-source filter to the interface list
1926  */
1927 static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1928         struct in6_addr *psfsrc, int delta)
1929 {
1930         struct ip6_sf_list *psf, *psf_prev;
1931
1932         psf_prev = NULL;
1933         for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1934                 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1935                         break;
1936                 psf_prev = psf;
1937         }
1938         if (!psf) {
1939                 psf = kmalloc(sizeof(*psf), GFP_ATOMIC);
1940                 if (!psf)
1941                         return -ENOBUFS;
1942                 memset(psf, 0, sizeof(*psf));
1943                 psf->sf_addr = *psfsrc;
1944                 if (psf_prev) {
1945                         psf_prev->sf_next = psf;
1946                 } else
1947                         pmc->mca_sources = psf;
1948         }
1949         psf->sf_count[sfmode]++;
1950         return 0;
1951 }
1952
1953 static void sf_markstate(struct ifmcaddr6 *pmc)
1954 {
1955         struct ip6_sf_list *psf;
1956         int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1957
1958         for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1959                 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1960                         psf->sf_oldin = mca_xcount ==
1961                                 psf->sf_count[MCAST_EXCLUDE] &&
1962                                 !psf->sf_count[MCAST_INCLUDE];
1963                 } else
1964                         psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1965 }
1966
1967 static int sf_setstate(struct ifmcaddr6 *pmc)
1968 {
1969         struct ip6_sf_list *psf;
1970         int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1971         int qrv = pmc->idev->mc_qrv;
1972         int new_in, rv;
1973
1974         rv = 0;
1975         for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1976                 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1977                         new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1978                                 !psf->sf_count[MCAST_INCLUDE];
1979                 } else
1980                         new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1981                 if (new_in != psf->sf_oldin) {
1982                         psf->sf_crcount = qrv;
1983                         rv++;
1984                 }
1985         }
1986         return rv;
1987 }
1988
1989 /*
1990  * Add multicast source filter list to the interface list
1991  */
1992 static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
1993                           int sfmode, int sfcount, struct in6_addr *psfsrc,
1994                           int delta)
1995 {
1996         struct ifmcaddr6 *pmc;
1997         int     isexclude;
1998         int     i, err;
1999
2000         if (!idev)
2001                 return -ENODEV;
2002         read_lock_bh(&idev->lock);
2003         for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
2004                 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2005                         break;
2006         }
2007         if (!pmc) {
2008                 /* MCA not found?? bug */
2009                 read_unlock_bh(&idev->lock);
2010                 return -ESRCH;
2011         }
2012         spin_lock_bh(&pmc->mca_lock);
2013
2014         sf_markstate(pmc);
2015         isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2016         if (!delta)
2017                 pmc->mca_sfcount[sfmode]++;
2018         err = 0;
2019         for (i=0; i<sfcount; i++) {
2020                 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
2021                 if (err)
2022                         break;
2023         }
2024         if (err) {
2025                 int j;
2026
2027                 if (!delta)
2028                         pmc->mca_sfcount[sfmode]--;
2029                 for (j=0; j<i; j++)
2030                         (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2031         } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
2032                 struct inet6_dev *idev = pmc->idev;
2033                 struct ip6_sf_list *psf;
2034
2035                 /* filter mode change */
2036                 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2037                         pmc->mca_sfmode = MCAST_EXCLUDE;
2038                 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2039                         pmc->mca_sfmode = MCAST_INCLUDE;
2040                 /* else no filters; keep old mode for reports */
2041
2042                 pmc->mca_crcount = idev->mc_qrv;
2043                 idev->mc_ifc_count = pmc->mca_crcount;
2044                 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
2045                         psf->sf_crcount = 0;
2046                 mld_ifc_event(idev);
2047         } else if (sf_setstate(pmc))
2048                 mld_ifc_event(idev);
2049         spin_unlock_bh(&pmc->mca_lock);
2050         read_unlock_bh(&idev->lock);
2051         return err;
2052 }
2053
2054 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2055 {
2056         struct ip6_sf_list *psf, *nextpsf;
2057
2058         for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
2059                 nextpsf = psf->sf_next;
2060                 kfree(psf);
2061         }
2062         pmc->mca_tomb = NULL;
2063         for (psf=pmc->mca_sources; psf; psf=nextpsf) {
2064                 nextpsf = psf->sf_next;
2065                 kfree(psf);
2066         }
2067         pmc->mca_sources = NULL;
2068         pmc->mca_sfmode = MCAST_EXCLUDE;
2069         pmc->mca_sfcount[MCAST_INCLUDE] = 0;
2070         pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2071 }
2072
2073
2074 static void igmp6_join_group(struct ifmcaddr6 *ma)
2075 {
2076         unsigned long delay;
2077
2078         if (ma->mca_flags & MAF_NOREPORT)
2079                 return;
2080
2081         igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2082
2083         delay = net_random() % IGMP6_UNSOLICITED_IVAL;
2084
2085         spin_lock_bh(&ma->mca_lock);
2086         if (del_timer(&ma->mca_timer)) {
2087                 atomic_dec(&ma->mca_refcnt);
2088                 delay = ma->mca_timer.expires - jiffies;
2089         }
2090
2091         if (!mod_timer(&ma->mca_timer, jiffies + delay))
2092                 atomic_inc(&ma->mca_refcnt);
2093         ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2094         spin_unlock_bh(&ma->mca_lock);
2095 }
2096
2097 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2098                             struct inet6_dev *idev)
2099 {
2100         int err;
2101
2102         /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
2103          * so no other readers or writers of iml or its sflist
2104          */
2105         if (iml->sflist == 0) {
2106                 /* any-source empty exclude case */
2107                 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2108         }
2109         err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2110                 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2111         sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2112         iml->sflist = NULL;
2113         return err;
2114 }
2115
2116 static void igmp6_leave_group(struct ifmcaddr6 *ma)
2117 {
2118         if (MLD_V1_SEEN(ma->idev)) {
2119                 if (ma->mca_flags & MAF_LAST_REPORTER)
2120                         igmp6_send(&ma->mca_addr, ma->idev->dev,
2121                                 ICMPV6_MGM_REDUCTION);
2122         } else {
2123                 mld_add_delrec(ma->idev, ma);
2124                 mld_ifc_event(ma->idev);
2125         }
2126 }
2127
2128 static void mld_gq_timer_expire(unsigned long data)
2129 {
2130         struct inet6_dev *idev = (struct inet6_dev *)data;
2131
2132         idev->mc_gq_running = 0;
2133         mld_send_report(idev, NULL);
2134         __in6_dev_put(idev);
2135 }
2136
2137 static void mld_ifc_timer_expire(unsigned long data)
2138 {
2139         struct inet6_dev *idev = (struct inet6_dev *)data;
2140
2141         mld_send_cr(idev);
2142         if (idev->mc_ifc_count) {
2143                 idev->mc_ifc_count--;
2144                 if (idev->mc_ifc_count)
2145                         mld_ifc_start_timer(idev, idev->mc_maxdelay);
2146         }
2147         __in6_dev_put(idev);
2148 }
2149
2150 static void mld_ifc_event(struct inet6_dev *idev)
2151 {
2152         if (MLD_V1_SEEN(idev))
2153                 return;
2154         idev->mc_ifc_count = idev->mc_qrv;
2155         mld_ifc_start_timer(idev, 1);
2156 }
2157
2158
2159 static void igmp6_timer_handler(unsigned long data)
2160 {
2161         struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2162
2163         if (MLD_V1_SEEN(ma->idev))
2164                 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2165         else
2166                 mld_send_report(ma->idev, ma);
2167
2168         spin_lock(&ma->mca_lock);
2169         ma->mca_flags |=  MAF_LAST_REPORTER;
2170         ma->mca_flags &= ~MAF_TIMER_RUNNING;
2171         spin_unlock(&ma->mca_lock);
2172         ma_put(ma);
2173 }
2174
2175 /* Device going down */
2176
2177 void ipv6_mc_down(struct inet6_dev *idev)
2178 {
2179         struct ifmcaddr6 *i;
2180
2181         /* Withdraw multicast list */
2182
2183         read_lock_bh(&idev->lock);
2184         idev->mc_ifc_count = 0;
2185         if (del_timer(&idev->mc_ifc_timer))
2186                 __in6_dev_put(idev);
2187         idev->mc_gq_running = 0;
2188         if (del_timer(&idev->mc_gq_timer))
2189                 __in6_dev_put(idev);
2190
2191         for (i = idev->mc_list; i; i=i->next)
2192                 igmp6_group_dropped(i);
2193         read_unlock_bh(&idev->lock);
2194
2195         mld_clear_delrec(idev);
2196 }
2197
2198
2199 /* Device going up */
2200
2201 void ipv6_mc_up(struct inet6_dev *idev)
2202 {
2203         struct ifmcaddr6 *i;
2204
2205         /* Install multicast list, except for all-nodes (already installed) */
2206
2207         read_lock_bh(&idev->lock);
2208         for (i = idev->mc_list; i; i=i->next)
2209                 igmp6_group_added(i);
2210         read_unlock_bh(&idev->lock);
2211 }
2212
2213 /* IPv6 device initialization. */
2214
2215 void ipv6_mc_init_dev(struct inet6_dev *idev)
2216 {
2217         struct in6_addr maddr;
2218
2219         write_lock_bh(&idev->lock);
2220         rwlock_init(&idev->mc_lock);
2221         idev->mc_gq_running = 0;
2222         init_timer(&idev->mc_gq_timer);
2223         idev->mc_gq_timer.data = (unsigned long) idev;
2224         idev->mc_gq_timer.function = &mld_gq_timer_expire;
2225         idev->mc_tomb = NULL;
2226         idev->mc_ifc_count = 0;
2227         init_timer(&idev->mc_ifc_timer);
2228         idev->mc_ifc_timer.data = (unsigned long) idev;
2229         idev->mc_ifc_timer.function = &mld_ifc_timer_expire;
2230         idev->mc_qrv = MLD_QRV_DEFAULT;
2231         idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2232         idev->mc_v1_seen = 0;
2233         write_unlock_bh(&idev->lock);
2234
2235         /* Add all-nodes address. */
2236         ipv6_addr_all_nodes(&maddr);
2237         ipv6_dev_mc_inc(idev->dev, &maddr);
2238 }
2239
2240 /*
2241  *      Device is about to be destroyed: clean up.
2242  */
2243
2244 void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2245 {
2246         struct ifmcaddr6 *i;
2247         struct in6_addr maddr;
2248
2249         /* Deactivate timers */
2250         ipv6_mc_down(idev);
2251
2252         /* Delete all-nodes address. */
2253         ipv6_addr_all_nodes(&maddr);
2254
2255         /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2256          * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2257          * fail.
2258          */
2259         __ipv6_dev_mc_dec(idev, &maddr);
2260
2261         if (idev->cnf.forwarding) {
2262                 ipv6_addr_all_routers(&maddr);
2263                 __ipv6_dev_mc_dec(idev, &maddr);
2264         }
2265
2266         write_lock_bh(&idev->lock);
2267         while ((i = idev->mc_list) != NULL) {
2268                 idev->mc_list = i->next;
2269                 write_unlock_bh(&idev->lock);
2270
2271                 igmp6_group_dropped(i);
2272                 ma_put(i);
2273
2274                 write_lock_bh(&idev->lock);
2275         }
2276         write_unlock_bh(&idev->lock);
2277 }
2278
2279 #ifdef CONFIG_PROC_FS
2280 struct igmp6_mc_iter_state {
2281         struct net_device *dev;
2282         struct inet6_dev *idev;
2283 };
2284
2285 #define igmp6_mc_seq_private(seq)       ((struct igmp6_mc_iter_state *)(seq)->private)
2286
2287 static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2288 {
2289         struct ifmcaddr6 *im = NULL;
2290         struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2291
2292         for (state->dev = dev_base, state->idev = NULL;
2293              state->dev; 
2294              state->dev = state->dev->next) {
2295                 struct inet6_dev *idev;
2296                 idev = in6_dev_get(state->dev);
2297                 if (!idev)
2298                         continue;
2299                 read_lock_bh(&idev->lock);
2300                 im = idev->mc_list;
2301                 if (im) {
2302                         state->idev = idev;
2303                         break;
2304                 }
2305                 read_unlock_bh(&idev->lock);
2306                 in6_dev_put(idev);
2307         }
2308         return im;
2309 }
2310
2311 static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2312 {
2313         struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2314
2315         im = im->next;
2316         while (!im) {
2317                 if (likely(state->idev != NULL)) {
2318                         read_unlock_bh(&state->idev->lock);
2319                         in6_dev_put(state->idev);
2320                 }
2321                 state->dev = state->dev->next;
2322                 if (!state->dev) {
2323                         state->idev = NULL;
2324                         break;
2325                 }
2326                 state->idev = in6_dev_get(state->dev);
2327                 if (!state->idev)
2328                         continue;
2329                 read_lock_bh(&state->idev->lock);
2330                 im = state->idev->mc_list;
2331         }
2332         return im;
2333 }
2334
2335 static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2336 {
2337         struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2338         if (im)
2339                 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2340                         --pos;
2341         return pos ? NULL : im;
2342 }
2343
2344 static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2345 {
2346         read_lock(&dev_base_lock);
2347         return igmp6_mc_get_idx(seq, *pos);
2348 }
2349
2350 static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2351 {
2352         struct ifmcaddr6 *im;
2353         im = igmp6_mc_get_next(seq, v);
2354         ++*pos;
2355         return im;
2356 }
2357
2358 static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2359 {
2360         struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2361         if (likely(state->idev != NULL)) {
2362                 read_unlock_bh(&state->idev->lock);
2363                 in6_dev_put(state->idev);
2364                 state->idev = NULL;
2365         }
2366         state->dev = NULL;
2367         read_unlock(&dev_base_lock);
2368 }
2369
2370 static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2371 {
2372         struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2373         struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2374
2375         seq_printf(seq,
2376                    "%-4d %-15s " NIP6_SEQFMT " %5d %08X %ld\n", 
2377                    state->dev->ifindex, state->dev->name,
2378                    NIP6(im->mca_addr),
2379                    im->mca_users, im->mca_flags,
2380                    (im->mca_flags&MAF_TIMER_RUNNING) ?
2381                    jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2382         return 0;
2383 }
2384
2385 static struct seq_operations igmp6_mc_seq_ops = {
2386         .start  =       igmp6_mc_seq_start,
2387         .next   =       igmp6_mc_seq_next,
2388         .stop   =       igmp6_mc_seq_stop,
2389         .show   =       igmp6_mc_seq_show,
2390 };
2391
2392 static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2393 {
2394         struct seq_file *seq;
2395         int rc = -ENOMEM;
2396         struct igmp6_mc_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2397
2398         if (!s)
2399                 goto out;
2400
2401         rc = seq_open(file, &igmp6_mc_seq_ops);
2402         if (rc)
2403                 goto out_kfree;
2404
2405         seq = file->private_data;
2406         seq->private = s;
2407         memset(s, 0, sizeof(*s));
2408 out:
2409         return rc;
2410 out_kfree:
2411         kfree(s);
2412         goto out;
2413 }
2414
2415 static struct file_operations igmp6_mc_seq_fops = {
2416         .owner          =       THIS_MODULE,
2417         .open           =       igmp6_mc_seq_open,
2418         .read           =       seq_read,
2419         .llseek         =       seq_lseek,
2420         .release        =       seq_release_private,
2421 };
2422
2423 struct igmp6_mcf_iter_state {
2424         struct net_device *dev;
2425         struct inet6_dev *idev;
2426         struct ifmcaddr6 *im;
2427 };
2428
2429 #define igmp6_mcf_seq_private(seq)      ((struct igmp6_mcf_iter_state *)(seq)->private)
2430
2431 static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2432 {
2433         struct ip6_sf_list *psf = NULL;
2434         struct ifmcaddr6 *im = NULL;
2435         struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2436
2437         for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
2438              state->dev; 
2439              state->dev = state->dev->next) {
2440                 struct inet6_dev *idev;
2441                 idev = in6_dev_get(state->dev);
2442                 if (unlikely(idev == NULL))
2443                         continue;
2444                 read_lock_bh(&idev->lock);
2445                 im = idev->mc_list;
2446                 if (likely(im != NULL)) {
2447                         spin_lock_bh(&im->mca_lock);
2448                         psf = im->mca_sources;
2449                         if (likely(psf != NULL)) {
2450                                 state->im = im;
2451                                 state->idev = idev;
2452                                 break;
2453                         }
2454                         spin_unlock_bh(&im->mca_lock);
2455                 }
2456                 read_unlock_bh(&idev->lock);
2457                 in6_dev_put(idev);
2458         }
2459         return psf;
2460 }
2461
2462 static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2463 {
2464         struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2465
2466         psf = psf->sf_next;
2467         while (!psf) {
2468                 spin_unlock_bh(&state->im->mca_lock);
2469                 state->im = state->im->next;
2470                 while (!state->im) {
2471                         if (likely(state->idev != NULL)) {
2472                                 read_unlock_bh(&state->idev->lock);
2473                                 in6_dev_put(state->idev);
2474                         }
2475                         state->dev = state->dev->next;
2476                         if (!state->dev) {
2477                                 state->idev = NULL;
2478                                 goto out;
2479                         }
2480                         state->idev = in6_dev_get(state->dev);
2481                         if (!state->idev)
2482                                 continue;
2483                         read_lock_bh(&state->idev->lock);
2484                         state->im = state->idev->mc_list;
2485                 }
2486                 if (!state->im)
2487                         break;
2488                 spin_lock_bh(&state->im->mca_lock);
2489                 psf = state->im->mca_sources;
2490         }
2491 out:
2492         return psf;
2493 }
2494
2495 static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2496 {
2497         struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2498         if (psf)
2499                 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2500                         --pos;
2501         return pos ? NULL : psf;
2502 }
2503
2504 static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2505 {
2506         read_lock(&dev_base_lock);
2507         return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2508 }
2509
2510 static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2511 {
2512         struct ip6_sf_list *psf;
2513         if (v == SEQ_START_TOKEN)
2514                 psf = igmp6_mcf_get_first(seq);
2515         else
2516                 psf = igmp6_mcf_get_next(seq, v);
2517         ++*pos;
2518         return psf;
2519 }
2520
2521 static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2522 {
2523         struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2524         if (likely(state->im != NULL)) {
2525                 spin_unlock_bh(&state->im->mca_lock);
2526                 state->im = NULL;
2527         }
2528         if (likely(state->idev != NULL)) {
2529                 read_unlock_bh(&state->idev->lock);
2530                 in6_dev_put(state->idev);
2531                 state->idev = NULL;
2532         }
2533         state->dev = NULL;
2534         read_unlock(&dev_base_lock);
2535 }
2536
2537 static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2538 {
2539         struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2540         struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2541
2542         if (v == SEQ_START_TOKEN) {
2543                 seq_printf(seq, 
2544                            "%3s %6s "
2545                            "%32s %32s %6s %6s\n", "Idx",
2546                            "Device", "Multicast Address",
2547                            "Source Address", "INC", "EXC");
2548         } else {
2549                 seq_printf(seq,
2550                            "%3d %6.6s " NIP6_SEQFMT " " NIP6_SEQFMT " %6lu %6lu\n",
2551                            state->dev->ifindex, state->dev->name,
2552                            NIP6(state->im->mca_addr),
2553                            NIP6(psf->sf_addr),
2554                            psf->sf_count[MCAST_INCLUDE],
2555                            psf->sf_count[MCAST_EXCLUDE]);
2556         }
2557         return 0;
2558 }
2559
2560 static struct seq_operations igmp6_mcf_seq_ops = {
2561         .start  =       igmp6_mcf_seq_start,
2562         .next   =       igmp6_mcf_seq_next,
2563         .stop   =       igmp6_mcf_seq_stop,
2564         .show   =       igmp6_mcf_seq_show,
2565 };
2566
2567 static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2568 {
2569         struct seq_file *seq;
2570         int rc = -ENOMEM;
2571         struct igmp6_mcf_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2572         
2573         if (!s)
2574                 goto out;
2575
2576         rc = seq_open(file, &igmp6_mcf_seq_ops);
2577         if (rc)
2578                 goto out_kfree;
2579
2580         seq = file->private_data;
2581         seq->private = s;
2582         memset(s, 0, sizeof(*s));
2583 out:
2584         return rc;
2585 out_kfree:
2586         kfree(s);
2587         goto out;
2588 }
2589
2590 static struct file_operations igmp6_mcf_seq_fops = {
2591         .owner          =       THIS_MODULE,
2592         .open           =       igmp6_mcf_seq_open,
2593         .read           =       seq_read,
2594         .llseek         =       seq_lseek,
2595         .release        =       seq_release_private,
2596 };
2597 #endif
2598
2599 int __init igmp6_init(struct net_proto_family *ops)
2600 {
2601         struct ipv6_pinfo *np;
2602         struct sock *sk;
2603         int err;
2604
2605         err = sock_create_kern(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6, &igmp6_socket);
2606         if (err < 0) {
2607                 printk(KERN_ERR
2608                        "Failed to initialize the IGMP6 control socket (err %d).\n",
2609                        err);
2610                 igmp6_socket = NULL; /* For safety. */
2611                 return err;
2612         }
2613
2614         sk = igmp6_socket->sk;
2615         sk->sk_allocation = GFP_ATOMIC;
2616         sk->sk_prot->unhash(sk);
2617
2618         np = inet6_sk(sk);
2619         np->hop_limit = 1;
2620
2621 #ifdef CONFIG_PROC_FS
2622         proc_net_fops_create("igmp6", S_IRUGO, &igmp6_mc_seq_fops);
2623         proc_net_fops_create("mcfilter6", S_IRUGO, &igmp6_mcf_seq_fops);
2624 #endif
2625
2626         return 0;
2627 }
2628
2629 void igmp6_cleanup(void)
2630 {
2631         sock_release(igmp6_socket);
2632         igmp6_socket = NULL; /* for safety */
2633
2634 #ifdef CONFIG_PROC_FS
2635         proc_net_remove("mcfilter6");
2636         proc_net_remove("igmp6");
2637 #endif
2638 }