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