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