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