]> Pileus Git - ~andy/linux/blob - net/ipv6/mcast.c
f15e04ad026effec3f907f6eb2db1eb140e54c3b
[~andy/linux] / 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         if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1235                 return -EINVAL;
1236
1237         hdr = (struct icmp6hdr*) skb->h.raw;
1238
1239         /* Drop reports with not link local source */
1240         addr_type = ipv6_addr_type(&skb->nh.ipv6h->saddr);
1241         if (addr_type != IPV6_ADDR_ANY && 
1242             !(addr_type&IPV6_ADDR_LINKLOCAL))
1243                 return -EINVAL;
1244
1245         addrp = (struct in6_addr *) (hdr + 1);
1246
1247         idev = in6_dev_get(skb->dev);
1248         if (idev == NULL)
1249                 return -ENODEV;
1250
1251         /*
1252          *      Cancel the timer for this group
1253          */
1254
1255         read_lock_bh(&idev->lock);
1256         for (ma = idev->mc_list; ma; ma=ma->next) {
1257                 if (ipv6_addr_equal(&ma->mca_addr, addrp)) {
1258                         spin_lock(&ma->mca_lock);
1259                         if (del_timer(&ma->mca_timer))
1260                                 atomic_dec(&ma->mca_refcnt);
1261                         ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1262                         spin_unlock(&ma->mca_lock);
1263                         break;
1264                 }
1265         }
1266         read_unlock_bh(&idev->lock);
1267         in6_dev_put(idev);
1268         return 0;
1269 }
1270
1271 static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1272         int gdeleted, int sdeleted)
1273 {
1274         switch (type) {
1275         case MLD2_MODE_IS_INCLUDE:
1276         case MLD2_MODE_IS_EXCLUDE:
1277                 if (gdeleted || sdeleted)
1278                         return 0;
1279                 return !((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp);
1280         case MLD2_CHANGE_TO_INCLUDE:
1281                 if (gdeleted || sdeleted)
1282                         return 0;
1283                 return psf->sf_count[MCAST_INCLUDE] != 0;
1284         case MLD2_CHANGE_TO_EXCLUDE:
1285                 if (gdeleted || sdeleted)
1286                         return 0;
1287                 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1288                     psf->sf_count[MCAST_INCLUDE])
1289                         return 0;
1290                 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1291                         psf->sf_count[MCAST_EXCLUDE];
1292         case MLD2_ALLOW_NEW_SOURCES:
1293                 if (gdeleted || !psf->sf_crcount)
1294                         return 0;
1295                 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1296         case MLD2_BLOCK_OLD_SOURCES:
1297                 if (pmc->mca_sfmode == MCAST_INCLUDE)
1298                         return gdeleted || (psf->sf_crcount && sdeleted);
1299                 return psf->sf_crcount && !gdeleted && !sdeleted;
1300         }
1301         return 0;
1302 }
1303
1304 static int
1305 mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1306 {
1307         struct ip6_sf_list *psf;
1308         int scount = 0;
1309
1310         for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1311                 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1312                         continue;
1313                 scount++;
1314         }
1315         return scount;
1316 }
1317
1318 static struct sk_buff *mld_newpack(struct net_device *dev, int size)
1319 {
1320         struct sock *sk = igmp6_socket->sk;
1321         struct sk_buff *skb;
1322         struct mld2_report *pmr;
1323         struct in6_addr addr_buf;
1324         int err;
1325         u8 ra[8] = { IPPROTO_ICMPV6, 0,
1326                      IPV6_TLV_ROUTERALERT, 2, 0, 0,
1327                      IPV6_TLV_PADN, 0 };
1328
1329         /* we assume size > sizeof(ra) here */
1330         skb = sock_alloc_send_skb(sk, size + LL_RESERVED_SPACE(dev), 1, &err);
1331
1332         if (skb == 0)
1333                 return NULL;
1334
1335         skb_reserve(skb, LL_RESERVED_SPACE(dev));
1336
1337         if (ipv6_get_lladdr(dev, &addr_buf)) {
1338                 /* <draft-ietf-magma-mld-source-05.txt>:
1339                  * use unspecified address as the source address 
1340                  * when a valid link-local address is not available.
1341                  */
1342                 memset(&addr_buf, 0, sizeof(addr_buf));
1343         }
1344
1345         ip6_nd_hdr(sk, skb, dev, &addr_buf, &mld2_all_mcr, NEXTHDR_HOP, 0);
1346
1347         memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1348
1349         pmr =(struct mld2_report *)skb_put(skb, sizeof(*pmr));
1350         skb->h.raw = (unsigned char *)pmr;
1351         pmr->type = ICMPV6_MLD2_REPORT;
1352         pmr->resv1 = 0;
1353         pmr->csum = 0;
1354         pmr->resv2 = 0;
1355         pmr->ngrec = 0;
1356         return skb;
1357 }
1358
1359 static inline int mld_dev_queue_xmit2(struct sk_buff *skb)
1360 {
1361         struct net_device *dev = skb->dev;
1362
1363         if (dev->hard_header) {
1364                 unsigned char ha[MAX_ADDR_LEN];
1365                 int err;
1366
1367                 ndisc_mc_map(&skb->nh.ipv6h->daddr, ha, dev, 1);
1368                 err = dev->hard_header(skb, dev, ETH_P_IPV6, ha, NULL, skb->len);
1369                 if (err < 0) {
1370                         kfree_skb(skb);
1371                         return err;
1372                 }
1373         }
1374         return dev_queue_xmit(skb);
1375 }
1376
1377 static inline int mld_dev_queue_xmit(struct sk_buff *skb)
1378 {
1379         return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL, skb->dev,
1380                        mld_dev_queue_xmit2);
1381 }
1382
1383 static void mld_sendpack(struct sk_buff *skb)
1384 {
1385         struct ipv6hdr *pip6 = skb->nh.ipv6h;
1386         struct mld2_report *pmr = (struct mld2_report *)skb->h.raw;
1387         int payload_len, mldlen;
1388         struct inet6_dev *idev = in6_dev_get(skb->dev);
1389         int err;
1390
1391         IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
1392         payload_len = skb->tail - (unsigned char *)skb->nh.ipv6h -
1393                 sizeof(struct ipv6hdr);
1394         mldlen = skb->tail - skb->h.raw;
1395         pip6->payload_len = htons(payload_len);
1396
1397         pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1398                 IPPROTO_ICMPV6, csum_partial(skb->h.raw, mldlen, 0));
1399         err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
1400                 mld_dev_queue_xmit);
1401         if (!err) {
1402                 ICMP6_INC_STATS(idev,ICMP6_MIB_OUTMSGS);
1403                 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
1404         } else
1405                 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1406
1407         if (likely(idev != NULL))
1408                 in6_dev_put(idev);
1409 }
1410
1411 static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1412 {
1413         return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
1414 }
1415
1416 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1417         int type, struct mld2_grec **ppgr)
1418 {
1419         struct net_device *dev = pmc->idev->dev;
1420         struct mld2_report *pmr;
1421         struct mld2_grec *pgr;
1422
1423         if (!skb)
1424                 skb = mld_newpack(dev, dev->mtu);
1425         if (!skb)
1426                 return NULL;
1427         pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1428         pgr->grec_type = type;
1429         pgr->grec_auxwords = 0;
1430         pgr->grec_nsrcs = 0;
1431         pgr->grec_mca = pmc->mca_addr;  /* structure copy */
1432         pmr = (struct mld2_report *)skb->h.raw;
1433         pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
1434         *ppgr = pgr;
1435         return skb;
1436 }
1437
1438 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1439         skb_tailroom(skb)) : 0)
1440
1441 static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1442         int type, int gdeleted, int sdeleted)
1443 {
1444         struct net_device *dev = pmc->idev->dev;
1445         struct mld2_report *pmr;
1446         struct mld2_grec *pgr = NULL;
1447         struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
1448         int scount, first, isquery, truncate;
1449
1450         if (pmc->mca_flags & MAF_NOREPORT)
1451                 return skb;
1452
1453         isquery = type == MLD2_MODE_IS_INCLUDE ||
1454                   type == MLD2_MODE_IS_EXCLUDE;
1455         truncate = type == MLD2_MODE_IS_EXCLUDE ||
1456                     type == MLD2_CHANGE_TO_EXCLUDE;
1457
1458         psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1459
1460         if (!*psf_list) {
1461                 if (type == MLD2_ALLOW_NEW_SOURCES ||
1462                     type == MLD2_BLOCK_OLD_SOURCES)
1463                         return skb;
1464                 if (pmc->mca_crcount || isquery) {
1465                         /* make sure we have room for group header and at
1466                          * least one source.
1467                          */
1468                         if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)+
1469                             sizeof(struct in6_addr)) {
1470                                 mld_sendpack(skb);
1471                                 skb = NULL; /* add_grhead will get a new one */
1472                         }
1473                         skb = add_grhead(skb, pmc, type, &pgr);
1474                 }
1475                 return skb;
1476         }
1477         pmr = skb ? (struct mld2_report *)skb->h.raw : NULL;
1478
1479         /* EX and TO_EX get a fresh packet, if needed */
1480         if (truncate) {
1481                 if (pmr && pmr->ngrec &&
1482                     AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1483                         if (skb)
1484                                 mld_sendpack(skb);
1485                         skb = mld_newpack(dev, dev->mtu);
1486                 }
1487         }
1488         first = 1;
1489         scount = 0;
1490         psf_prev = NULL;
1491         for (psf=*psf_list; psf; psf=psf_next) {
1492                 struct in6_addr *psrc;
1493
1494                 psf_next = psf->sf_next;
1495
1496                 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1497                         psf_prev = psf;
1498                         continue;
1499                 }
1500
1501                 /* clear marks on query responses */
1502                 if (isquery)
1503                         psf->sf_gsresp = 0;
1504
1505                 if (AVAILABLE(skb) < sizeof(*psrc) +
1506                     first*sizeof(struct mld2_grec)) {
1507                         if (truncate && !first)
1508                                 break;   /* truncate these */
1509                         if (pgr)
1510                                 pgr->grec_nsrcs = htons(scount);
1511                         if (skb)
1512                                 mld_sendpack(skb);
1513                         skb = mld_newpack(dev, dev->mtu);
1514                         first = 1;
1515                         scount = 0;
1516                 }
1517                 if (first) {
1518                         skb = add_grhead(skb, pmc, type, &pgr);
1519                         first = 0;
1520                 }
1521                 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1522                 *psrc = psf->sf_addr;
1523                 scount++;
1524                 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1525                      type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1526                         psf->sf_crcount--;
1527                         if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1528                                 if (psf_prev)
1529                                         psf_prev->sf_next = psf->sf_next;
1530                                 else
1531                                         *psf_list = psf->sf_next;
1532                                 kfree(psf);
1533                                 continue;
1534                         }
1535                 }
1536                 psf_prev = psf;
1537         }
1538         if (pgr)
1539                 pgr->grec_nsrcs = htons(scount);
1540
1541         if (isquery)
1542                 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1543         return skb;
1544 }
1545
1546 static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1547 {
1548         struct sk_buff *skb = NULL;
1549         int type;
1550
1551         if (!pmc) {
1552                 read_lock_bh(&idev->lock);
1553                 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1554                         if (pmc->mca_flags & MAF_NOREPORT)
1555                                 continue;
1556                         spin_lock_bh(&pmc->mca_lock);
1557                         if (pmc->mca_sfcount[MCAST_EXCLUDE])
1558                                 type = MLD2_MODE_IS_EXCLUDE;
1559                         else
1560                                 type = MLD2_MODE_IS_INCLUDE;
1561                         skb = add_grec(skb, pmc, type, 0, 0);
1562                         spin_unlock_bh(&pmc->mca_lock);
1563                 }
1564                 read_unlock_bh(&idev->lock);
1565         } else {
1566                 spin_lock_bh(&pmc->mca_lock);
1567                 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1568                         type = MLD2_MODE_IS_EXCLUDE;
1569                 else
1570                         type = MLD2_MODE_IS_INCLUDE;
1571                 skb = add_grec(skb, pmc, type, 0, 0);
1572                 spin_unlock_bh(&pmc->mca_lock);
1573         }
1574         if (skb)
1575                 mld_sendpack(skb);
1576 }
1577
1578 /*
1579  * remove zero-count source records from a source filter list
1580  */
1581 static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1582 {
1583         struct ip6_sf_list *psf_prev, *psf_next, *psf;
1584
1585         psf_prev = NULL;
1586         for (psf=*ppsf; psf; psf = psf_next) {
1587                 psf_next = psf->sf_next;
1588                 if (psf->sf_crcount == 0) {
1589                         if (psf_prev)
1590                                 psf_prev->sf_next = psf->sf_next;
1591                         else
1592                                 *ppsf = psf->sf_next;
1593                         kfree(psf);
1594                 } else
1595                         psf_prev = psf;
1596         }
1597 }
1598
1599 static void mld_send_cr(struct inet6_dev *idev)
1600 {
1601         struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1602         struct sk_buff *skb = NULL;
1603         int type, dtype;
1604
1605         read_lock_bh(&idev->lock);
1606         write_lock_bh(&idev->mc_lock);
1607
1608         /* deleted MCA's */
1609         pmc_prev = NULL;
1610         for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1611                 pmc_next = pmc->next;
1612                 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1613                         type = MLD2_BLOCK_OLD_SOURCES;
1614                         dtype = MLD2_BLOCK_OLD_SOURCES;
1615                         skb = add_grec(skb, pmc, type, 1, 0);
1616                         skb = add_grec(skb, pmc, dtype, 1, 1);
1617                 }
1618                 if (pmc->mca_crcount) {
1619                         pmc->mca_crcount--;
1620                         if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1621                                 type = MLD2_CHANGE_TO_INCLUDE;
1622                                 skb = add_grec(skb, pmc, type, 1, 0);
1623                         }
1624                         if (pmc->mca_crcount == 0) {
1625                                 mld_clear_zeros(&pmc->mca_tomb);
1626                                 mld_clear_zeros(&pmc->mca_sources);
1627                         }
1628                 }
1629                 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1630                     !pmc->mca_sources) {
1631                         if (pmc_prev)
1632                                 pmc_prev->next = pmc_next;
1633                         else
1634                                 idev->mc_tomb = pmc_next;
1635                         in6_dev_put(pmc->idev);
1636                         kfree(pmc);
1637                 } else
1638                         pmc_prev = pmc;
1639         }
1640         write_unlock_bh(&idev->mc_lock);
1641
1642         /* change recs */
1643         for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1644                 spin_lock_bh(&pmc->mca_lock);
1645                 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1646                         type = MLD2_BLOCK_OLD_SOURCES;
1647                         dtype = MLD2_ALLOW_NEW_SOURCES;
1648                 } else {
1649                         type = MLD2_ALLOW_NEW_SOURCES;
1650                         dtype = MLD2_BLOCK_OLD_SOURCES;
1651                 }
1652                 skb = add_grec(skb, pmc, type, 0, 0);
1653                 skb = add_grec(skb, pmc, dtype, 0, 1);  /* deleted sources */
1654
1655                 /* filter mode changes */
1656                 if (pmc->mca_crcount) {
1657                         pmc->mca_crcount--;
1658                         if (pmc->mca_sfmode == MCAST_EXCLUDE)
1659                                 type = MLD2_CHANGE_TO_EXCLUDE;
1660                         else
1661                                 type = MLD2_CHANGE_TO_INCLUDE;
1662                         skb = add_grec(skb, pmc, type, 0, 0);
1663                 }
1664                 spin_unlock_bh(&pmc->mca_lock);
1665         }
1666         read_unlock_bh(&idev->lock);
1667         if (!skb)
1668                 return;
1669         (void) mld_sendpack(skb);
1670 }
1671
1672 static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1673 {
1674         struct sock *sk = igmp6_socket->sk;
1675         struct inet6_dev *idev;
1676         struct sk_buff *skb;
1677         struct icmp6hdr *hdr;
1678         struct in6_addr *snd_addr;
1679         struct in6_addr *addrp;
1680         struct in6_addr addr_buf;
1681         struct in6_addr all_routers;
1682         int err, len, payload_len, full_len;
1683         u8 ra[8] = { IPPROTO_ICMPV6, 0,
1684                      IPV6_TLV_ROUTERALERT, 2, 0, 0,
1685                      IPV6_TLV_PADN, 0 };
1686
1687         IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
1688         snd_addr = addr;
1689         if (type == ICMPV6_MGM_REDUCTION) {
1690                 snd_addr = &all_routers;
1691                 ipv6_addr_all_routers(&all_routers);
1692         }
1693
1694         len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1695         payload_len = len + sizeof(ra);
1696         full_len = sizeof(struct ipv6hdr) + payload_len;
1697
1698         skb = sock_alloc_send_skb(sk, LL_RESERVED_SPACE(dev) + full_len, 1, &err);
1699
1700         if (skb == NULL) {
1701                 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1702                 return;
1703         }
1704
1705         skb_reserve(skb, LL_RESERVED_SPACE(dev));
1706
1707         if (ipv6_get_lladdr(dev, &addr_buf)) {
1708                 /* <draft-ietf-magma-mld-source-05.txt>:
1709                  * use unspecified address as the source address 
1710                  * when a valid link-local address is not available.
1711                  */
1712                 memset(&addr_buf, 0, sizeof(addr_buf));
1713         }
1714
1715         ip6_nd_hdr(sk, skb, dev, &addr_buf, snd_addr, NEXTHDR_HOP, payload_len);
1716
1717         memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1718
1719         hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
1720         memset(hdr, 0, sizeof(struct icmp6hdr));
1721         hdr->icmp6_type = type;
1722
1723         addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
1724         ipv6_addr_copy(addrp, addr);
1725
1726         hdr->icmp6_cksum = csum_ipv6_magic(&addr_buf, snd_addr, len,
1727                                            IPPROTO_ICMPV6,
1728                                            csum_partial((__u8 *) hdr, len, 0));
1729
1730         idev = in6_dev_get(skb->dev);
1731
1732         err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
1733                 mld_dev_queue_xmit);
1734         if (!err) {
1735                 if (type == ICMPV6_MGM_REDUCTION)
1736                         ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBREDUCTIONS);
1737                 else
1738                         ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBRESPONSES);
1739                 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTMSGS);
1740                 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
1741         } else
1742                 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1743
1744         if (likely(idev != NULL))
1745                 in6_dev_put(idev);
1746         return;
1747 }
1748
1749 static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1750         struct in6_addr *psfsrc)
1751 {
1752         struct ip6_sf_list *psf, *psf_prev;
1753         int rv = 0;
1754
1755         psf_prev = NULL;
1756         for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1757                 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1758                         break;
1759                 psf_prev = psf;
1760         }
1761         if (!psf || psf->sf_count[sfmode] == 0) {
1762                 /* source filter not found, or count wrong =>  bug */
1763                 return -ESRCH;
1764         }
1765         psf->sf_count[sfmode]--;
1766         if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1767                 struct inet6_dev *idev = pmc->idev;
1768
1769                 /* no more filters for this source */
1770                 if (psf_prev)
1771                         psf_prev->sf_next = psf->sf_next;
1772                 else
1773                         pmc->mca_sources = psf->sf_next;
1774                 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1775                     !MLD_V1_SEEN(idev)) {
1776                         psf->sf_crcount = idev->mc_qrv;
1777                         psf->sf_next = pmc->mca_tomb;
1778                         pmc->mca_tomb = psf;
1779                         rv = 1;
1780                 } else
1781                         kfree(psf);
1782         }
1783         return rv;
1784 }
1785
1786 static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
1787                           int sfmode, int sfcount, struct in6_addr *psfsrc,
1788                           int delta)
1789 {
1790         struct ifmcaddr6 *pmc;
1791         int     changerec = 0;
1792         int     i, err;
1793
1794         if (!idev)
1795                 return -ENODEV;
1796         read_lock_bh(&idev->lock);
1797         for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1798                 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1799                         break;
1800         }
1801         if (!pmc) {
1802                 /* MCA not found?? bug */
1803                 read_unlock_bh(&idev->lock);
1804                 return -ESRCH;
1805         }
1806         spin_lock_bh(&pmc->mca_lock);
1807         sf_markstate(pmc);
1808         if (!delta) {
1809                 if (!pmc->mca_sfcount[sfmode]) {
1810                         spin_unlock_bh(&pmc->mca_lock);
1811                         read_unlock_bh(&idev->lock);
1812                         return -EINVAL;
1813                 }
1814                 pmc->mca_sfcount[sfmode]--;
1815         }
1816         err = 0;
1817         for (i=0; i<sfcount; i++) {
1818                 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1819
1820                 changerec |= rv > 0;
1821                 if (!err && rv < 0)
1822                         err = rv;
1823         }
1824         if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1825             pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1826             pmc->mca_sfcount[MCAST_INCLUDE]) {
1827                 struct ip6_sf_list *psf;
1828
1829                 /* filter mode change */
1830                 pmc->mca_sfmode = MCAST_INCLUDE;
1831                 pmc->mca_crcount = idev->mc_qrv;
1832                 idev->mc_ifc_count = pmc->mca_crcount;
1833                 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1834                         psf->sf_crcount = 0;
1835                 mld_ifc_event(pmc->idev);
1836         } else if (sf_setstate(pmc) || changerec)
1837                 mld_ifc_event(pmc->idev);
1838         spin_unlock_bh(&pmc->mca_lock);
1839         read_unlock_bh(&idev->lock);
1840         return err;
1841 }
1842
1843 /*
1844  * Add multicast single-source filter to the interface list
1845  */
1846 static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1847         struct in6_addr *psfsrc, int delta)
1848 {
1849         struct ip6_sf_list *psf, *psf_prev;
1850
1851         psf_prev = NULL;
1852         for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1853                 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1854                         break;
1855                 psf_prev = psf;
1856         }
1857         if (!psf) {
1858                 psf = (struct ip6_sf_list *)kmalloc(sizeof(*psf), GFP_ATOMIC);
1859                 if (!psf)
1860                         return -ENOBUFS;
1861                 memset(psf, 0, sizeof(*psf));
1862                 psf->sf_addr = *psfsrc;
1863                 if (psf_prev) {
1864                         psf_prev->sf_next = psf;
1865                 } else
1866                         pmc->mca_sources = psf;
1867         }
1868         psf->sf_count[sfmode]++;
1869         return 0;
1870 }
1871
1872 static void sf_markstate(struct ifmcaddr6 *pmc)
1873 {
1874         struct ip6_sf_list *psf;
1875         int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1876
1877         for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1878                 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1879                         psf->sf_oldin = mca_xcount ==
1880                                 psf->sf_count[MCAST_EXCLUDE] &&
1881                                 !psf->sf_count[MCAST_INCLUDE];
1882                 } else
1883                         psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1884 }
1885
1886 static int sf_setstate(struct ifmcaddr6 *pmc)
1887 {
1888         struct ip6_sf_list *psf;
1889         int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1890         int qrv = pmc->idev->mc_qrv;
1891         int new_in, rv;
1892
1893         rv = 0;
1894         for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1895                 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1896                         new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1897                                 !psf->sf_count[MCAST_INCLUDE];
1898                 } else
1899                         new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1900                 if (new_in != psf->sf_oldin) {
1901                         psf->sf_crcount = qrv;
1902                         rv++;
1903                 }
1904         }
1905         return rv;
1906 }
1907
1908 /*
1909  * Add multicast source filter list to the interface list
1910  */
1911 static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
1912                           int sfmode, int sfcount, struct in6_addr *psfsrc,
1913                           int delta)
1914 {
1915         struct ifmcaddr6 *pmc;
1916         int     isexclude;
1917         int     i, err;
1918
1919         if (!idev)
1920                 return -ENODEV;
1921         read_lock_bh(&idev->lock);
1922         for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1923                 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1924                         break;
1925         }
1926         if (!pmc) {
1927                 /* MCA not found?? bug */
1928                 read_unlock_bh(&idev->lock);
1929                 return -ESRCH;
1930         }
1931         spin_lock_bh(&pmc->mca_lock);
1932
1933         sf_markstate(pmc);
1934         isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
1935         if (!delta)
1936                 pmc->mca_sfcount[sfmode]++;
1937         err = 0;
1938         for (i=0; i<sfcount; i++) {
1939                 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1940                 if (err)
1941                         break;
1942         }
1943         if (err) {
1944                 int j;
1945
1946                 if (!delta)
1947                         pmc->mca_sfcount[sfmode]--;
1948                 for (j=0; j<i; j++)
1949                         (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1950         } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
1951                 struct inet6_dev *idev = pmc->idev;
1952                 struct ip6_sf_list *psf;
1953
1954                 /* filter mode change */
1955                 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1956                         pmc->mca_sfmode = MCAST_EXCLUDE;
1957                 else if (pmc->mca_sfcount[MCAST_INCLUDE])
1958                         pmc->mca_sfmode = MCAST_INCLUDE;
1959                 /* else no filters; keep old mode for reports */
1960
1961                 pmc->mca_crcount = idev->mc_qrv;
1962                 idev->mc_ifc_count = pmc->mca_crcount;
1963                 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1964                         psf->sf_crcount = 0;
1965                 mld_ifc_event(idev);
1966         } else if (sf_setstate(pmc))
1967                 mld_ifc_event(idev);
1968         spin_unlock_bh(&pmc->mca_lock);
1969         read_unlock_bh(&idev->lock);
1970         return err;
1971 }
1972
1973 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
1974 {
1975         struct ip6_sf_list *psf, *nextpsf;
1976
1977         for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
1978                 nextpsf = psf->sf_next;
1979                 kfree(psf);
1980         }
1981         pmc->mca_tomb = NULL;
1982         for (psf=pmc->mca_sources; psf; psf=nextpsf) {
1983                 nextpsf = psf->sf_next;
1984                 kfree(psf);
1985         }
1986         pmc->mca_sources = NULL;
1987         pmc->mca_sfmode = MCAST_EXCLUDE;
1988         pmc->mca_sfcount[MCAST_INCLUDE] = 0;
1989         pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
1990 }
1991
1992
1993 static void igmp6_join_group(struct ifmcaddr6 *ma)
1994 {
1995         unsigned long delay;
1996
1997         if (ma->mca_flags & MAF_NOREPORT)
1998                 return;
1999
2000         igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2001
2002         delay = net_random() % IGMP6_UNSOLICITED_IVAL;
2003
2004         spin_lock_bh(&ma->mca_lock);
2005         if (del_timer(&ma->mca_timer)) {
2006                 atomic_dec(&ma->mca_refcnt);
2007                 delay = ma->mca_timer.expires - jiffies;
2008         }
2009
2010         if (!mod_timer(&ma->mca_timer, jiffies + delay))
2011                 atomic_inc(&ma->mca_refcnt);
2012         ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2013         spin_unlock_bh(&ma->mca_lock);
2014 }
2015
2016 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2017                             struct inet6_dev *idev)
2018 {
2019         int err;
2020
2021         if (iml->sflist == 0) {
2022                 /* any-source empty exclude case */
2023                 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2024         }
2025         err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2026                 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2027         sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2028         iml->sflist = NULL;
2029         return err;
2030 }
2031
2032 static void igmp6_leave_group(struct ifmcaddr6 *ma)
2033 {
2034         if (MLD_V1_SEEN(ma->idev)) {
2035                 if (ma->mca_flags & MAF_LAST_REPORTER)
2036                         igmp6_send(&ma->mca_addr, ma->idev->dev,
2037                                 ICMPV6_MGM_REDUCTION);
2038         } else {
2039                 mld_add_delrec(ma->idev, ma);
2040                 mld_ifc_event(ma->idev);
2041         }
2042 }
2043
2044 static void mld_gq_timer_expire(unsigned long data)
2045 {
2046         struct inet6_dev *idev = (struct inet6_dev *)data;
2047
2048         idev->mc_gq_running = 0;
2049         mld_send_report(idev, NULL);
2050         __in6_dev_put(idev);
2051 }
2052
2053 static void mld_ifc_timer_expire(unsigned long data)
2054 {
2055         struct inet6_dev *idev = (struct inet6_dev *)data;
2056
2057         mld_send_cr(idev);
2058         if (idev->mc_ifc_count) {
2059                 idev->mc_ifc_count--;
2060                 if (idev->mc_ifc_count)
2061                         mld_ifc_start_timer(idev, idev->mc_maxdelay);
2062         }
2063         __in6_dev_put(idev);
2064 }
2065
2066 static void mld_ifc_event(struct inet6_dev *idev)
2067 {
2068         if (MLD_V1_SEEN(idev))
2069                 return;
2070         idev->mc_ifc_count = idev->mc_qrv;
2071         mld_ifc_start_timer(idev, 1);
2072 }
2073
2074
2075 static void igmp6_timer_handler(unsigned long data)
2076 {
2077         struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2078
2079         if (MLD_V1_SEEN(ma->idev))
2080                 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2081         else
2082                 mld_send_report(ma->idev, ma);
2083
2084         spin_lock(&ma->mca_lock);
2085         ma->mca_flags |=  MAF_LAST_REPORTER;
2086         ma->mca_flags &= ~MAF_TIMER_RUNNING;
2087         spin_unlock(&ma->mca_lock);
2088         ma_put(ma);
2089 }
2090
2091 /* Device going down */
2092
2093 void ipv6_mc_down(struct inet6_dev *idev)
2094 {
2095         struct ifmcaddr6 *i;
2096
2097         /* Withdraw multicast list */
2098
2099         read_lock_bh(&idev->lock);
2100         idev->mc_ifc_count = 0;
2101         if (del_timer(&idev->mc_ifc_timer))
2102                 __in6_dev_put(idev);
2103         idev->mc_gq_running = 0;
2104         if (del_timer(&idev->mc_gq_timer))
2105                 __in6_dev_put(idev);
2106
2107         for (i = idev->mc_list; i; i=i->next)
2108                 igmp6_group_dropped(i);
2109         read_unlock_bh(&idev->lock);
2110
2111         mld_clear_delrec(idev);
2112 }
2113
2114
2115 /* Device going up */
2116
2117 void ipv6_mc_up(struct inet6_dev *idev)
2118 {
2119         struct ifmcaddr6 *i;
2120
2121         /* Install multicast list, except for all-nodes (already installed) */
2122
2123         read_lock_bh(&idev->lock);
2124         for (i = idev->mc_list; i; i=i->next)
2125                 igmp6_group_added(i);
2126         read_unlock_bh(&idev->lock);
2127 }
2128
2129 /* IPv6 device initialization. */
2130
2131 void ipv6_mc_init_dev(struct inet6_dev *idev)
2132 {
2133         struct in6_addr maddr;
2134
2135         write_lock_bh(&idev->lock);
2136         rwlock_init(&idev->mc_lock);
2137         idev->mc_gq_running = 0;
2138         init_timer(&idev->mc_gq_timer);
2139         idev->mc_gq_timer.data = (unsigned long) idev;
2140         idev->mc_gq_timer.function = &mld_gq_timer_expire;
2141         idev->mc_tomb = NULL;
2142         idev->mc_ifc_count = 0;
2143         init_timer(&idev->mc_ifc_timer);
2144         idev->mc_ifc_timer.data = (unsigned long) idev;
2145         idev->mc_ifc_timer.function = &mld_ifc_timer_expire;
2146         idev->mc_qrv = MLD_QRV_DEFAULT;
2147         idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2148         idev->mc_v1_seen = 0;
2149         write_unlock_bh(&idev->lock);
2150
2151         /* Add all-nodes address. */
2152         ipv6_addr_all_nodes(&maddr);
2153         ipv6_dev_mc_inc(idev->dev, &maddr);
2154 }
2155
2156 /*
2157  *      Device is about to be destroyed: clean up.
2158  */
2159
2160 void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2161 {
2162         struct ifmcaddr6 *i;
2163         struct in6_addr maddr;
2164
2165         /* Deactivate timers */
2166         ipv6_mc_down(idev);
2167
2168         /* Delete all-nodes address. */
2169         ipv6_addr_all_nodes(&maddr);
2170
2171         /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2172          * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2173          * fail.
2174          */
2175         __ipv6_dev_mc_dec(idev, &maddr);
2176
2177         if (idev->cnf.forwarding) {
2178                 ipv6_addr_all_routers(&maddr);
2179                 __ipv6_dev_mc_dec(idev, &maddr);
2180         }
2181
2182         write_lock_bh(&idev->lock);
2183         while ((i = idev->mc_list) != NULL) {
2184                 idev->mc_list = i->next;
2185                 write_unlock_bh(&idev->lock);
2186
2187                 igmp6_group_dropped(i);
2188                 ma_put(i);
2189
2190                 write_lock_bh(&idev->lock);
2191         }
2192         write_unlock_bh(&idev->lock);
2193 }
2194
2195 #ifdef CONFIG_PROC_FS
2196 struct igmp6_mc_iter_state {
2197         struct net_device *dev;
2198         struct inet6_dev *idev;
2199 };
2200
2201 #define igmp6_mc_seq_private(seq)       ((struct igmp6_mc_iter_state *)(seq)->private)
2202
2203 static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2204 {
2205         struct ifmcaddr6 *im = NULL;
2206         struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2207
2208         for (state->dev = dev_base, state->idev = NULL;
2209              state->dev; 
2210              state->dev = state->dev->next) {
2211                 struct inet6_dev *idev;
2212                 idev = in6_dev_get(state->dev);
2213                 if (!idev)
2214                         continue;
2215                 read_lock_bh(&idev->lock);
2216                 im = idev->mc_list;
2217                 if (im) {
2218                         state->idev = idev;
2219                         break;
2220                 }
2221                 read_unlock_bh(&idev->lock);
2222                 in6_dev_put(idev);
2223         }
2224         return im;
2225 }
2226
2227 static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2228 {
2229         struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2230
2231         im = im->next;
2232         while (!im) {
2233                 if (likely(state->idev != NULL)) {
2234                         read_unlock_bh(&state->idev->lock);
2235                         in6_dev_put(state->idev);
2236                 }
2237                 state->dev = state->dev->next;
2238                 if (!state->dev) {
2239                         state->idev = NULL;
2240                         break;
2241                 }
2242                 state->idev = in6_dev_get(state->dev);
2243                 if (!state->idev)
2244                         continue;
2245                 read_lock_bh(&state->idev->lock);
2246                 im = state->idev->mc_list;
2247         }
2248         return im;
2249 }
2250
2251 static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2252 {
2253         struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2254         if (im)
2255                 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2256                         --pos;
2257         return pos ? NULL : im;
2258 }
2259
2260 static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2261 {
2262         read_lock(&dev_base_lock);
2263         return igmp6_mc_get_idx(seq, *pos);
2264 }
2265
2266 static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2267 {
2268         struct ifmcaddr6 *im;
2269         im = igmp6_mc_get_next(seq, v);
2270         ++*pos;
2271         return im;
2272 }
2273
2274 static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2275 {
2276         struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2277         if (likely(state->idev != NULL)) {
2278                 read_unlock_bh(&state->idev->lock);
2279                 in6_dev_put(state->idev);
2280                 state->idev = NULL;
2281         }
2282         state->dev = NULL;
2283         read_unlock(&dev_base_lock);
2284 }
2285
2286 static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2287 {
2288         struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2289         struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2290
2291         seq_printf(seq,
2292                    "%-4d %-15s %04x%04x%04x%04x%04x%04x%04x%04x %5d %08X %ld\n", 
2293                    state->dev->ifindex, state->dev->name,
2294                    NIP6(im->mca_addr),
2295                    im->mca_users, im->mca_flags,
2296                    (im->mca_flags&MAF_TIMER_RUNNING) ?
2297                    jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2298         return 0;
2299 }
2300
2301 static struct seq_operations igmp6_mc_seq_ops = {
2302         .start  =       igmp6_mc_seq_start,
2303         .next   =       igmp6_mc_seq_next,
2304         .stop   =       igmp6_mc_seq_stop,
2305         .show   =       igmp6_mc_seq_show,
2306 };
2307
2308 static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2309 {
2310         struct seq_file *seq;
2311         int rc = -ENOMEM;
2312         struct igmp6_mc_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2313
2314         if (!s)
2315                 goto out;
2316
2317         rc = seq_open(file, &igmp6_mc_seq_ops);
2318         if (rc)
2319                 goto out_kfree;
2320
2321         seq = file->private_data;
2322         seq->private = s;
2323         memset(s, 0, sizeof(*s));
2324 out:
2325         return rc;
2326 out_kfree:
2327         kfree(s);
2328         goto out;
2329 }
2330
2331 static struct file_operations igmp6_mc_seq_fops = {
2332         .owner          =       THIS_MODULE,
2333         .open           =       igmp6_mc_seq_open,
2334         .read           =       seq_read,
2335         .llseek         =       seq_lseek,
2336         .release        =       seq_release_private,
2337 };
2338
2339 struct igmp6_mcf_iter_state {
2340         struct net_device *dev;
2341         struct inet6_dev *idev;
2342         struct ifmcaddr6 *im;
2343 };
2344
2345 #define igmp6_mcf_seq_private(seq)      ((struct igmp6_mcf_iter_state *)(seq)->private)
2346
2347 static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2348 {
2349         struct ip6_sf_list *psf = NULL;
2350         struct ifmcaddr6 *im = NULL;
2351         struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2352
2353         for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
2354              state->dev; 
2355              state->dev = state->dev->next) {
2356                 struct inet6_dev *idev;
2357                 idev = in6_dev_get(state->dev);
2358                 if (unlikely(idev == NULL))
2359                         continue;
2360                 read_lock_bh(&idev->lock);
2361                 im = idev->mc_list;
2362                 if (likely(im != NULL)) {
2363                         spin_lock_bh(&im->mca_lock);
2364                         psf = im->mca_sources;
2365                         if (likely(psf != NULL)) {
2366                                 state->im = im;
2367                                 state->idev = idev;
2368                                 break;
2369                         }
2370                         spin_unlock_bh(&im->mca_lock);
2371                 }
2372                 read_unlock_bh(&idev->lock);
2373                 in6_dev_put(idev);
2374         }
2375         return psf;
2376 }
2377
2378 static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2379 {
2380         struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2381
2382         psf = psf->sf_next;
2383         while (!psf) {
2384                 spin_unlock_bh(&state->im->mca_lock);
2385                 state->im = state->im->next;
2386                 while (!state->im) {
2387                         if (likely(state->idev != NULL)) {
2388                                 read_unlock_bh(&state->idev->lock);
2389                                 in6_dev_put(state->idev);
2390                         }
2391                         state->dev = state->dev->next;
2392                         if (!state->dev) {
2393                                 state->idev = NULL;
2394                                 goto out;
2395                         }
2396                         state->idev = in6_dev_get(state->dev);
2397                         if (!state->idev)
2398                                 continue;
2399                         read_lock_bh(&state->idev->lock);
2400                         state->im = state->idev->mc_list;
2401                 }
2402                 if (!state->im)
2403                         break;
2404                 spin_lock_bh(&state->im->mca_lock);
2405                 psf = state->im->mca_sources;
2406         }
2407 out:
2408         return psf;
2409 }
2410
2411 static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2412 {
2413         struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2414         if (psf)
2415                 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2416                         --pos;
2417         return pos ? NULL : psf;
2418 }
2419
2420 static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2421 {
2422         read_lock(&dev_base_lock);
2423         return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2424 }
2425
2426 static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2427 {
2428         struct ip6_sf_list *psf;
2429         if (v == SEQ_START_TOKEN)
2430                 psf = igmp6_mcf_get_first(seq);
2431         else
2432                 psf = igmp6_mcf_get_next(seq, v);
2433         ++*pos;
2434         return psf;
2435 }
2436
2437 static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2438 {
2439         struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2440         if (likely(state->im != NULL)) {
2441                 spin_unlock_bh(&state->im->mca_lock);
2442                 state->im = NULL;
2443         }
2444         if (likely(state->idev != NULL)) {
2445                 read_unlock_bh(&state->idev->lock);
2446                 in6_dev_put(state->idev);
2447                 state->idev = NULL;
2448         }
2449         state->dev = NULL;
2450         read_unlock(&dev_base_lock);
2451 }
2452
2453 static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2454 {
2455         struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2456         struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2457
2458         if (v == SEQ_START_TOKEN) {
2459                 seq_printf(seq, 
2460                            "%3s %6s "
2461                            "%32s %32s %6s %6s\n", "Idx",
2462                            "Device", "Multicast Address",
2463                            "Source Address", "INC", "EXC");
2464         } else {
2465                 seq_printf(seq,
2466                            "%3d %6.6s "
2467                            "%04x%04x%04x%04x%04x%04x%04x%04x "
2468                            "%04x%04x%04x%04x%04x%04x%04x%04x "
2469                            "%6lu %6lu\n",
2470                            state->dev->ifindex, state->dev->name,
2471                            NIP6(state->im->mca_addr),
2472                            NIP6(psf->sf_addr),
2473                            psf->sf_count[MCAST_INCLUDE],
2474                            psf->sf_count[MCAST_EXCLUDE]);
2475         }
2476         return 0;
2477 }
2478
2479 static struct seq_operations igmp6_mcf_seq_ops = {
2480         .start  =       igmp6_mcf_seq_start,
2481         .next   =       igmp6_mcf_seq_next,
2482         .stop   =       igmp6_mcf_seq_stop,
2483         .show   =       igmp6_mcf_seq_show,
2484 };
2485
2486 static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2487 {
2488         struct seq_file *seq;
2489         int rc = -ENOMEM;
2490         struct igmp6_mcf_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2491         
2492         if (!s)
2493                 goto out;
2494
2495         rc = seq_open(file, &igmp6_mcf_seq_ops);
2496         if (rc)
2497                 goto out_kfree;
2498
2499         seq = file->private_data;
2500         seq->private = s;
2501         memset(s, 0, sizeof(*s));
2502 out:
2503         return rc;
2504 out_kfree:
2505         kfree(s);
2506         goto out;
2507 }
2508
2509 static struct file_operations igmp6_mcf_seq_fops = {
2510         .owner          =       THIS_MODULE,
2511         .open           =       igmp6_mcf_seq_open,
2512         .read           =       seq_read,
2513         .llseek         =       seq_lseek,
2514         .release        =       seq_release_private,
2515 };
2516 #endif
2517
2518 int __init igmp6_init(struct net_proto_family *ops)
2519 {
2520         struct ipv6_pinfo *np;
2521         struct sock *sk;
2522         int err;
2523
2524         err = sock_create_kern(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6, &igmp6_socket);
2525         if (err < 0) {
2526                 printk(KERN_ERR
2527                        "Failed to initialize the IGMP6 control socket (err %d).\n",
2528                        err);
2529                 igmp6_socket = NULL; /* For safety. */
2530                 return err;
2531         }
2532
2533         sk = igmp6_socket->sk;
2534         sk->sk_allocation = GFP_ATOMIC;
2535         sk->sk_prot->unhash(sk);
2536
2537         np = inet6_sk(sk);
2538         np->hop_limit = 1;
2539
2540 #ifdef CONFIG_PROC_FS
2541         proc_net_fops_create("igmp6", S_IRUGO, &igmp6_mc_seq_fops);
2542         proc_net_fops_create("mcfilter6", S_IRUGO, &igmp6_mcf_seq_fops);
2543 #endif
2544
2545         return 0;
2546 }
2547
2548 void igmp6_cleanup(void)
2549 {
2550         sock_release(igmp6_socket);
2551         igmp6_socket = NULL; /* for safety */
2552
2553 #ifdef CONFIG_PROC_FS
2554         proc_net_remove("mcfilter6");
2555         proc_net_remove("igmp6");
2556 #endif
2557 }