[NET]: Make the device list and device lookups per namespace.
[pandora-kernel.git] / net / ipv6 / mcast.c
1 /*
2  *      Multicast support for IPv6
3  *      Linux INET6 implementation
4  *
5  *      Authors:
6  *      Pedro Roque             <roque@di.fc.ul.pt>
7  *
8  *      $Id: mcast.c,v 1.40 2002/02/08 03:57:19 davem Exp $
9  *
10  *      Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
11  *
12  *      This program is free software; you can redistribute it and/or
13  *      modify it under the terms of the GNU General Public License
14  *      as published by the Free Software Foundation; either version
15  *      2 of the License, or (at your option) any later version.
16  */
17
18 /* Changes:
19  *
20  *      yoshfuji        : fix format of router-alert option
21  *      YOSHIFUJI Hideaki @USAGI:
22  *              Fixed source address for MLD message based on
23  *              <draft-ietf-magma-mld-source-05.txt>.
24  *      YOSHIFUJI Hideaki @USAGI:
25  *              - Ignore Queries for invalid addresses.
26  *              - MLD for link-local addresses.
27  *      David L Stevens <dlstevens@us.ibm.com>:
28  *              - MLDv2 support
29  */
30
31 #include <linux/module.h>
32 #include <linux/errno.h>
33 #include <linux/types.h>
34 #include <linux/string.h>
35 #include <linux/socket.h>
36 #include <linux/sockios.h>
37 #include <linux/jiffies.h>
38 #include <linux/times.h>
39 #include <linux/net.h>
40 #include <linux/in.h>
41 #include <linux/in6.h>
42 #include <linux/netdevice.h>
43 #include <linux/if_arp.h>
44 #include <linux/route.h>
45 #include <linux/init.h>
46 #include <linux/proc_fs.h>
47 #include <linux/seq_file.h>
48
49 #include <linux/netfilter.h>
50 #include <linux/netfilter_ipv6.h>
51
52 #include <net/net_namespace.h>
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         __be16          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         __sum16 csum;
96         __be16  resv2;
97         __be16  ngrec;
98         struct mld2_grec grec[0];
99 };
100
101 struct mld2_query {
102         __u8 type;
103         __u8 code;
104         __sum16 csum;
105         __be16 mrc;
106         __be16 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         __be16 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 __read_mostly = 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(&init_net, ifindex);
219
220         if (dev == NULL) {
221                 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
222                 return -ENODEV;
223         }
224
225         mc_lst->ifindex = dev->ifindex;
226         mc_lst->sfmode = MCAST_EXCLUDE;
227         rwlock_init(&mc_lst->sflock);
228         mc_lst->sflist = NULL;
229
230         /*
231          *      now add/increase the group membership on the device
232          */
233
234         err = ipv6_dev_mc_inc(dev, addr);
235
236         if (err) {
237                 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
238                 dev_put(dev);
239                 return err;
240         }
241
242         write_lock_bh(&ipv6_sk_mc_lock);
243         mc_lst->next = np->ipv6_mc_list;
244         np->ipv6_mc_list = mc_lst;
245         write_unlock_bh(&ipv6_sk_mc_lock);
246
247         dev_put(dev);
248
249         return 0;
250 }
251
252 /*
253  *      socket leave on multicast group
254  */
255 int ipv6_sock_mc_drop(struct sock *sk, int ifindex, struct in6_addr *addr)
256 {
257         struct ipv6_pinfo *np = inet6_sk(sk);
258         struct ipv6_mc_socklist *mc_lst, **lnk;
259
260         write_lock_bh(&ipv6_sk_mc_lock);
261         for (lnk = &np->ipv6_mc_list; (mc_lst = *lnk) !=NULL ; lnk = &mc_lst->next) {
262                 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
263                     ipv6_addr_equal(&mc_lst->addr, addr)) {
264                         struct net_device *dev;
265
266                         *lnk = mc_lst->next;
267                         write_unlock_bh(&ipv6_sk_mc_lock);
268
269                         if ((dev = dev_get_by_index(&init_net, mc_lst->ifindex)) != NULL) {
270                                 struct inet6_dev *idev = in6_dev_get(dev);
271
272                                 (void) ip6_mc_leave_src(sk, mc_lst, idev);
273                                 if (idev) {
274                                         __ipv6_dev_mc_dec(idev, &mc_lst->addr);
275                                         in6_dev_put(idev);
276                                 }
277                                 dev_put(dev);
278                         } else
279                                 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
280                         sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
281                         return 0;
282                 }
283         }
284         write_unlock_bh(&ipv6_sk_mc_lock);
285
286         return -EADDRNOTAVAIL;
287 }
288
289 static struct inet6_dev *ip6_mc_find_dev(struct in6_addr *group, int ifindex)
290 {
291         struct net_device *dev = NULL;
292         struct inet6_dev *idev = NULL;
293
294         if (ifindex == 0) {
295                 struct rt6_info *rt;
296
297                 rt = rt6_lookup(group, NULL, 0, 0);
298                 if (rt) {
299                         dev = rt->rt6i_dev;
300                         dev_hold(dev);
301                         dst_release(&rt->u.dst);
302                 }
303         } else
304                 dev = dev_get_by_index(&init_net, ifindex);
305
306         if (!dev)
307                 return NULL;
308         idev = in6_dev_get(dev);
309         if (!idev) {
310                 dev_put(dev);
311                 return NULL;
312         }
313         read_lock_bh(&idev->lock);
314         if (idev->dead) {
315                 read_unlock_bh(&idev->lock);
316                 in6_dev_put(idev);
317                 dev_put(dev);
318                 return NULL;
319         }
320         return idev;
321 }
322
323 void ipv6_sock_mc_close(struct sock *sk)
324 {
325         struct ipv6_pinfo *np = inet6_sk(sk);
326         struct ipv6_mc_socklist *mc_lst;
327
328         write_lock_bh(&ipv6_sk_mc_lock);
329         while ((mc_lst = np->ipv6_mc_list) != NULL) {
330                 struct net_device *dev;
331
332                 np->ipv6_mc_list = mc_lst->next;
333                 write_unlock_bh(&ipv6_sk_mc_lock);
334
335                 dev = dev_get_by_index(&init_net, mc_lst->ifindex);
336                 if (dev) {
337                         struct inet6_dev *idev = in6_dev_get(dev);
338
339                         (void) ip6_mc_leave_src(sk, mc_lst, idev);
340                         if (idev) {
341                                 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
342                                 in6_dev_put(idev);
343                         }
344                         dev_put(dev);
345                 } else
346                         (void) ip6_mc_leave_src(sk, mc_lst, NULL);
347
348                 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
349
350                 write_lock_bh(&ipv6_sk_mc_lock);
351         }
352         write_unlock_bh(&ipv6_sk_mc_lock);
353 }
354
355 int ip6_mc_source(int add, int omode, struct sock *sk,
356         struct group_source_req *pgsr)
357 {
358         struct in6_addr *source, *group;
359         struct ipv6_mc_socklist *pmc;
360         struct net_device *dev;
361         struct inet6_dev *idev;
362         struct ipv6_pinfo *inet6 = inet6_sk(sk);
363         struct ip6_sf_socklist *psl;
364         int i, j, rv;
365         int leavegroup = 0;
366         int pmclocked = 0;
367         int err;
368
369         if (pgsr->gsr_group.ss_family != AF_INET6 ||
370             pgsr->gsr_source.ss_family != AF_INET6)
371                 return -EINVAL;
372
373         source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
374         group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
375
376         if (!ipv6_addr_is_multicast(group))
377                 return -EINVAL;
378
379         idev = ip6_mc_find_dev(group, pgsr->gsr_interface);
380         if (!idev)
381                 return -ENODEV;
382         dev = idev->dev;
383
384         err = -EADDRNOTAVAIL;
385
386         read_lock_bh(&ipv6_sk_mc_lock);
387         for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
388                 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
389                         continue;
390                 if (ipv6_addr_equal(&pmc->addr, group))
391                         break;
392         }
393         if (!pmc) {             /* must have a prior join */
394                 err = -EINVAL;
395                 goto done;
396         }
397         /* if a source filter was set, must be the same mode as before */
398         if (pmc->sflist) {
399                 if (pmc->sfmode != omode) {
400                         err = -EINVAL;
401                         goto done;
402                 }
403         } else if (pmc->sfmode != omode) {
404                 /* allow mode switches for empty-set filters */
405                 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
406                 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
407                 pmc->sfmode = omode;
408         }
409
410         write_lock_bh(&pmc->sflock);
411         pmclocked = 1;
412
413         psl = pmc->sflist;
414         if (!add) {
415                 if (!psl)
416                         goto done;      /* err = -EADDRNOTAVAIL */
417                 rv = !0;
418                 for (i=0; i<psl->sl_count; i++) {
419                         rv = memcmp(&psl->sl_addr[i], source,
420                                 sizeof(struct in6_addr));
421                         if (rv == 0)
422                                 break;
423                 }
424                 if (rv)         /* source not found */
425                         goto done;      /* err = -EADDRNOTAVAIL */
426
427                 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
428                 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
429                         leavegroup = 1;
430                         goto done;
431                 }
432
433                 /* update the interface filter */
434                 ip6_mc_del_src(idev, group, omode, 1, source, 1);
435
436                 for (j=i+1; j<psl->sl_count; j++)
437                         psl->sl_addr[j-1] = psl->sl_addr[j];
438                 psl->sl_count--;
439                 err = 0;
440                 goto done;
441         }
442         /* else, add a new source to the filter */
443
444         if (psl && psl->sl_count >= sysctl_mld_max_msf) {
445                 err = -ENOBUFS;
446                 goto done;
447         }
448         if (!psl || psl->sl_count == psl->sl_max) {
449                 struct ip6_sf_socklist *newpsl;
450                 int count = IP6_SFBLOCK;
451
452                 if (psl)
453                         count += psl->sl_max;
454                 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
455                 if (!newpsl) {
456                         err = -ENOBUFS;
457                         goto done;
458                 }
459                 newpsl->sl_max = count;
460                 newpsl->sl_count = count - IP6_SFBLOCK;
461                 if (psl) {
462                         for (i=0; i<psl->sl_count; i++)
463                                 newpsl->sl_addr[i] = psl->sl_addr[i];
464                         sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
465                 }
466                 pmc->sflist = psl = newpsl;
467         }
468         rv = 1; /* > 0 for insert logic below if sl_count is 0 */
469         for (i=0; i<psl->sl_count; i++) {
470                 rv = memcmp(&psl->sl_addr[i], source, sizeof(struct in6_addr));
471                 if (rv == 0)
472                         break;
473         }
474         if (rv == 0)            /* address already there is an error */
475                 goto done;
476         for (j=psl->sl_count-1; j>=i; j--)
477                 psl->sl_addr[j+1] = psl->sl_addr[j];
478         psl->sl_addr[i] = *source;
479         psl->sl_count++;
480         err = 0;
481         /* update the interface list */
482         ip6_mc_add_src(idev, group, omode, 1, source, 1);
483 done:
484         if (pmclocked)
485                 write_unlock_bh(&pmc->sflock);
486         read_unlock_bh(&ipv6_sk_mc_lock);
487         read_unlock_bh(&idev->lock);
488         in6_dev_put(idev);
489         dev_put(dev);
490         if (leavegroup)
491                 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
492         return err;
493 }
494
495 int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
496 {
497         struct in6_addr *group;
498         struct ipv6_mc_socklist *pmc;
499         struct net_device *dev;
500         struct inet6_dev *idev;
501         struct ipv6_pinfo *inet6 = inet6_sk(sk);
502         struct ip6_sf_socklist *newpsl, *psl;
503         int leavegroup = 0;
504         int i, err;
505
506         group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
507
508         if (!ipv6_addr_is_multicast(group))
509                 return -EINVAL;
510         if (gsf->gf_fmode != MCAST_INCLUDE &&
511             gsf->gf_fmode != MCAST_EXCLUDE)
512                 return -EINVAL;
513
514         idev = ip6_mc_find_dev(group, gsf->gf_interface);
515
516         if (!idev)
517                 return -ENODEV;
518         dev = idev->dev;
519
520         err = 0;
521         read_lock_bh(&ipv6_sk_mc_lock);
522
523         if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
524                 leavegroup = 1;
525                 goto done;
526         }
527
528         for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
529                 if (pmc->ifindex != gsf->gf_interface)
530                         continue;
531                 if (ipv6_addr_equal(&pmc->addr, group))
532                         break;
533         }
534         if (!pmc) {             /* must have a prior join */
535                 err = -EINVAL;
536                 goto done;
537         }
538         if (gsf->gf_numsrc) {
539                 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
540                                                           GFP_ATOMIC);
541                 if (!newpsl) {
542                         err = -ENOBUFS;
543                         goto done;
544                 }
545                 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
546                 for (i=0; i<newpsl->sl_count; ++i) {
547                         struct sockaddr_in6 *psin6;
548
549                         psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
550                         newpsl->sl_addr[i] = psin6->sin6_addr;
551                 }
552                 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
553                         newpsl->sl_count, newpsl->sl_addr, 0);
554                 if (err) {
555                         sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
556                         goto done;
557                 }
558         } else {
559                 newpsl = NULL;
560                 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
561         }
562
563         write_lock_bh(&pmc->sflock);
564         psl = pmc->sflist;
565         if (psl) {
566                 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
567                         psl->sl_count, psl->sl_addr, 0);
568                 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
569         } else
570                 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
571         pmc->sflist = newpsl;
572         pmc->sfmode = gsf->gf_fmode;
573         write_unlock_bh(&pmc->sflock);
574         err = 0;
575 done:
576         read_unlock_bh(&ipv6_sk_mc_lock);
577         read_unlock_bh(&idev->lock);
578         in6_dev_put(idev);
579         dev_put(dev);
580         if (leavegroup)
581                 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
582         return err;
583 }
584
585 int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
586         struct group_filter __user *optval, int __user *optlen)
587 {
588         int err, i, count, copycount;
589         struct in6_addr *group;
590         struct ipv6_mc_socklist *pmc;
591         struct inet6_dev *idev;
592         struct net_device *dev;
593         struct ipv6_pinfo *inet6 = inet6_sk(sk);
594         struct ip6_sf_socklist *psl;
595
596         group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
597
598         if (!ipv6_addr_is_multicast(group))
599                 return -EINVAL;
600
601         idev = ip6_mc_find_dev(group, gsf->gf_interface);
602
603         if (!idev)
604                 return -ENODEV;
605
606         dev = idev->dev;
607
608         err = -EADDRNOTAVAIL;
609         /*
610          * changes to the ipv6_mc_list require the socket lock and
611          * a read lock on ip6_sk_mc_lock. We have the socket lock,
612          * so reading the list is safe.
613          */
614
615         for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
616                 if (pmc->ifindex != gsf->gf_interface)
617                         continue;
618                 if (ipv6_addr_equal(group, &pmc->addr))
619                         break;
620         }
621         if (!pmc)               /* must have a prior join */
622                 goto done;
623         gsf->gf_fmode = pmc->sfmode;
624         psl = pmc->sflist;
625         count = psl ? psl->sl_count : 0;
626         read_unlock_bh(&idev->lock);
627         in6_dev_put(idev);
628         dev_put(dev);
629
630         copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
631         gsf->gf_numsrc = count;
632         if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
633             copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
634                 return -EFAULT;
635         }
636         /* changes to psl require the socket lock, a read lock on
637          * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
638          * have the socket lock, so reading here is safe.
639          */
640         for (i=0; i<copycount; i++) {
641                 struct sockaddr_in6 *psin6;
642                 struct sockaddr_storage ss;
643
644                 psin6 = (struct sockaddr_in6 *)&ss;
645                 memset(&ss, 0, sizeof(ss));
646                 psin6->sin6_family = AF_INET6;
647                 psin6->sin6_addr = psl->sl_addr[i];
648                 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
649                         return -EFAULT;
650         }
651         return 0;
652 done:
653         read_unlock_bh(&idev->lock);
654         in6_dev_put(idev);
655         dev_put(dev);
656         return err;
657 }
658
659 int inet6_mc_check(struct sock *sk, struct in6_addr *mc_addr,
660         struct in6_addr *src_addr)
661 {
662         struct ipv6_pinfo *np = inet6_sk(sk);
663         struct ipv6_mc_socklist *mc;
664         struct ip6_sf_socklist *psl;
665         int rv = 1;
666
667         read_lock(&ipv6_sk_mc_lock);
668         for (mc = np->ipv6_mc_list; mc; mc = mc->next) {
669                 if (ipv6_addr_equal(&mc->addr, mc_addr))
670                         break;
671         }
672         if (!mc) {
673                 read_unlock(&ipv6_sk_mc_lock);
674                 return 1;
675         }
676         read_lock(&mc->sflock);
677         psl = mc->sflist;
678         if (!psl) {
679                 rv = mc->sfmode == MCAST_EXCLUDE;
680         } else {
681                 int i;
682
683                 for (i=0; i<psl->sl_count; i++) {
684                         if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
685                                 break;
686                 }
687                 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
688                         rv = 0;
689                 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
690                         rv = 0;
691         }
692         read_unlock(&mc->sflock);
693         read_unlock(&ipv6_sk_mc_lock);
694
695         return rv;
696 }
697
698 static void ma_put(struct ifmcaddr6 *mc)
699 {
700         if (atomic_dec_and_test(&mc->mca_refcnt)) {
701                 in6_dev_put(mc->idev);
702                 kfree(mc);
703         }
704 }
705
706 static void igmp6_group_added(struct ifmcaddr6 *mc)
707 {
708         struct net_device *dev = mc->idev->dev;
709         char buf[MAX_ADDR_LEN];
710
711         spin_lock_bh(&mc->mca_lock);
712         if (!(mc->mca_flags&MAF_LOADED)) {
713                 mc->mca_flags |= MAF_LOADED;
714                 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
715                         dev_mc_add(dev, buf, dev->addr_len, 0);
716         }
717         spin_unlock_bh(&mc->mca_lock);
718
719         if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
720                 return;
721
722         if (MLD_V1_SEEN(mc->idev)) {
723                 igmp6_join_group(mc);
724                 return;
725         }
726         /* else v2 */
727
728         mc->mca_crcount = mc->idev->mc_qrv;
729         mld_ifc_event(mc->idev);
730 }
731
732 static void igmp6_group_dropped(struct ifmcaddr6 *mc)
733 {
734         struct net_device *dev = mc->idev->dev;
735         char buf[MAX_ADDR_LEN];
736
737         spin_lock_bh(&mc->mca_lock);
738         if (mc->mca_flags&MAF_LOADED) {
739                 mc->mca_flags &= ~MAF_LOADED;
740                 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
741                         dev_mc_delete(dev, buf, dev->addr_len, 0);
742         }
743
744         if (mc->mca_flags & MAF_NOREPORT)
745                 goto done;
746         spin_unlock_bh(&mc->mca_lock);
747
748         if (!mc->idev->dead)
749                 igmp6_leave_group(mc);
750
751         spin_lock_bh(&mc->mca_lock);
752         if (del_timer(&mc->mca_timer))
753                 atomic_dec(&mc->mca_refcnt);
754 done:
755         ip6_mc_clear_src(mc);
756         spin_unlock_bh(&mc->mca_lock);
757 }
758
759 /*
760  * deleted ifmcaddr6 manipulation
761  */
762 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
763 {
764         struct ifmcaddr6 *pmc;
765
766         /* this is an "ifmcaddr6" for convenience; only the fields below
767          * are actually used. In particular, the refcnt and users are not
768          * used for management of the delete list. Using the same structure
769          * for deleted items allows change reports to use common code with
770          * non-deleted or query-response MCA's.
771          */
772         pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
773         if (!pmc)
774                 return;
775
776         spin_lock_bh(&im->mca_lock);
777         spin_lock_init(&pmc->mca_lock);
778         pmc->idev = im->idev;
779         in6_dev_hold(idev);
780         pmc->mca_addr = im->mca_addr;
781         pmc->mca_crcount = idev->mc_qrv;
782         pmc->mca_sfmode = im->mca_sfmode;
783         if (pmc->mca_sfmode == MCAST_INCLUDE) {
784                 struct ip6_sf_list *psf;
785
786                 pmc->mca_tomb = im->mca_tomb;
787                 pmc->mca_sources = im->mca_sources;
788                 im->mca_tomb = im->mca_sources = NULL;
789                 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
790                         psf->sf_crcount = pmc->mca_crcount;
791         }
792         spin_unlock_bh(&im->mca_lock);
793
794         write_lock_bh(&idev->mc_lock);
795         pmc->next = idev->mc_tomb;
796         idev->mc_tomb = pmc;
797         write_unlock_bh(&idev->mc_lock);
798 }
799
800 static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *pmca)
801 {
802         struct ifmcaddr6 *pmc, *pmc_prev;
803         struct ip6_sf_list *psf, *psf_next;
804
805         write_lock_bh(&idev->mc_lock);
806         pmc_prev = NULL;
807         for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
808                 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
809                         break;
810                 pmc_prev = pmc;
811         }
812         if (pmc) {
813                 if (pmc_prev)
814                         pmc_prev->next = pmc->next;
815                 else
816                         idev->mc_tomb = pmc->next;
817         }
818         write_unlock_bh(&idev->mc_lock);
819         if (pmc) {
820                 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
821                         psf_next = psf->sf_next;
822                         kfree(psf);
823                 }
824                 in6_dev_put(pmc->idev);
825                 kfree(pmc);
826         }
827 }
828
829 static void mld_clear_delrec(struct inet6_dev *idev)
830 {
831         struct ifmcaddr6 *pmc, *nextpmc;
832
833         write_lock_bh(&idev->mc_lock);
834         pmc = idev->mc_tomb;
835         idev->mc_tomb = NULL;
836         write_unlock_bh(&idev->mc_lock);
837
838         for (; pmc; pmc = nextpmc) {
839                 nextpmc = pmc->next;
840                 ip6_mc_clear_src(pmc);
841                 in6_dev_put(pmc->idev);
842                 kfree(pmc);
843         }
844
845         /* clear dead sources, too */
846         read_lock_bh(&idev->lock);
847         for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
848                 struct ip6_sf_list *psf, *psf_next;
849
850                 spin_lock_bh(&pmc->mca_lock);
851                 psf = pmc->mca_tomb;
852                 pmc->mca_tomb = NULL;
853                 spin_unlock_bh(&pmc->mca_lock);
854                 for (; psf; psf=psf_next) {
855                         psf_next = psf->sf_next;
856                         kfree(psf);
857                 }
858         }
859         read_unlock_bh(&idev->lock);
860 }
861
862
863 /*
864  *      device multicast group inc (add if not found)
865  */
866 int ipv6_dev_mc_inc(struct net_device *dev, struct in6_addr *addr)
867 {
868         struct ifmcaddr6 *mc;
869         struct inet6_dev *idev;
870
871         idev = in6_dev_get(dev);
872
873         if (idev == NULL)
874                 return -EINVAL;
875
876         write_lock_bh(&idev->lock);
877         if (idev->dead) {
878                 write_unlock_bh(&idev->lock);
879                 in6_dev_put(idev);
880                 return -ENODEV;
881         }
882
883         for (mc = idev->mc_list; mc; mc = mc->next) {
884                 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
885                         mc->mca_users++;
886                         write_unlock_bh(&idev->lock);
887                         ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
888                                 NULL, 0);
889                         in6_dev_put(idev);
890                         return 0;
891                 }
892         }
893
894         /*
895          *      not found: create a new one.
896          */
897
898         mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
899
900         if (mc == NULL) {
901                 write_unlock_bh(&idev->lock);
902                 in6_dev_put(idev);
903                 return -ENOMEM;
904         }
905
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 = icmp6_hdr(skb);
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(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
1172         len -= skb_network_header_len(skb);
1173
1174         /* Drop queries with not link local source */
1175         if (!(ipv6_addr_type(&ipv6_hdr(skb)->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 = icmp6_hdr(skb);
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_transport_header(skb);
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_transport_header(skb);
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 (!ipv6_addr_equal(group, &ma->mca_addr))
1257                                 continue;
1258                         spin_lock_bh(&ma->mca_lock);
1259                         if (ma->mca_flags & MAF_TIMER_RUNNING) {
1260                                 /* gsquery <- gsquery && mark */
1261                                 if (!mark)
1262                                         ma->mca_flags &= ~MAF_GSQUERY;
1263                         } else {
1264                                 /* gsquery <- mark */
1265                                 if (mark)
1266                                         ma->mca_flags |= MAF_GSQUERY;
1267                                 else
1268                                         ma->mca_flags &= ~MAF_GSQUERY;
1269                         }
1270                         if (!(ma->mca_flags & MAF_GSQUERY) ||
1271                             mld_marksources(ma, ntohs(mlh2->nsrcs), mlh2->srcs))
1272                                 igmp6_group_queried(ma, max_delay);
1273                         spin_unlock_bh(&ma->mca_lock);
1274                         break;
1275                 }
1276         }
1277         read_unlock_bh(&idev->lock);
1278         in6_dev_put(idev);
1279
1280         return 0;
1281 }
1282
1283
1284 int igmp6_event_report(struct sk_buff *skb)
1285 {
1286         struct ifmcaddr6 *ma;
1287         struct in6_addr *addrp;
1288         struct inet6_dev *idev;
1289         struct icmp6hdr *hdr;
1290         int addr_type;
1291
1292         /* Our own report looped back. Ignore it. */
1293         if (skb->pkt_type == PACKET_LOOPBACK)
1294                 return 0;
1295
1296         /* send our report if the MC router may not have heard this report */
1297         if (skb->pkt_type != PACKET_MULTICAST &&
1298             skb->pkt_type != PACKET_BROADCAST)
1299                 return 0;
1300
1301         if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1302                 return -EINVAL;
1303
1304         hdr = icmp6_hdr(skb);
1305
1306         /* Drop reports with not link local source */
1307         addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
1308         if (addr_type != IPV6_ADDR_ANY &&
1309             !(addr_type&IPV6_ADDR_LINKLOCAL))
1310                 return -EINVAL;
1311
1312         addrp = (struct in6_addr *) (hdr + 1);
1313
1314         idev = in6_dev_get(skb->dev);
1315         if (idev == NULL)
1316                 return -ENODEV;
1317
1318         /*
1319          *      Cancel the timer for this group
1320          */
1321
1322         read_lock_bh(&idev->lock);
1323         for (ma = idev->mc_list; ma; ma=ma->next) {
1324                 if (ipv6_addr_equal(&ma->mca_addr, addrp)) {
1325                         spin_lock(&ma->mca_lock);
1326                         if (del_timer(&ma->mca_timer))
1327                                 atomic_dec(&ma->mca_refcnt);
1328                         ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1329                         spin_unlock(&ma->mca_lock);
1330                         break;
1331                 }
1332         }
1333         read_unlock_bh(&idev->lock);
1334         in6_dev_put(idev);
1335         return 0;
1336 }
1337
1338 static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1339         int gdeleted, int sdeleted)
1340 {
1341         switch (type) {
1342         case MLD2_MODE_IS_INCLUDE:
1343         case MLD2_MODE_IS_EXCLUDE:
1344                 if (gdeleted || sdeleted)
1345                         return 0;
1346                 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1347                         if (pmc->mca_sfmode == MCAST_INCLUDE)
1348                                 return 1;
1349                         /* don't include if this source is excluded
1350                          * in all filters
1351                          */
1352                         if (psf->sf_count[MCAST_INCLUDE])
1353                                 return type == MLD2_MODE_IS_INCLUDE;
1354                         return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1355                                 psf->sf_count[MCAST_EXCLUDE];
1356                 }
1357                 return 0;
1358         case MLD2_CHANGE_TO_INCLUDE:
1359                 if (gdeleted || sdeleted)
1360                         return 0;
1361                 return psf->sf_count[MCAST_INCLUDE] != 0;
1362         case MLD2_CHANGE_TO_EXCLUDE:
1363                 if (gdeleted || sdeleted)
1364                         return 0;
1365                 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1366                     psf->sf_count[MCAST_INCLUDE])
1367                         return 0;
1368                 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1369                         psf->sf_count[MCAST_EXCLUDE];
1370         case MLD2_ALLOW_NEW_SOURCES:
1371                 if (gdeleted || !psf->sf_crcount)
1372                         return 0;
1373                 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1374         case MLD2_BLOCK_OLD_SOURCES:
1375                 if (pmc->mca_sfmode == MCAST_INCLUDE)
1376                         return gdeleted || (psf->sf_crcount && sdeleted);
1377                 return psf->sf_crcount && !gdeleted && !sdeleted;
1378         }
1379         return 0;
1380 }
1381
1382 static int
1383 mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1384 {
1385         struct ip6_sf_list *psf;
1386         int scount = 0;
1387
1388         for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1389                 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1390                         continue;
1391                 scount++;
1392         }
1393         return scount;
1394 }
1395
1396 static struct sk_buff *mld_newpack(struct net_device *dev, int size)
1397 {
1398         struct sock *sk = igmp6_socket->sk;
1399         struct sk_buff *skb;
1400         struct mld2_report *pmr;
1401         struct in6_addr addr_buf;
1402         int err;
1403         u8 ra[8] = { IPPROTO_ICMPV6, 0,
1404                      IPV6_TLV_ROUTERALERT, 2, 0, 0,
1405                      IPV6_TLV_PADN, 0 };
1406
1407         /* we assume size > sizeof(ra) here */
1408         skb = sock_alloc_send_skb(sk, size + LL_RESERVED_SPACE(dev), 1, &err);
1409
1410         if (skb == 0)
1411                 return NULL;
1412
1413         skb_reserve(skb, LL_RESERVED_SPACE(dev));
1414
1415         if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
1416                 /* <draft-ietf-magma-mld-source-05.txt>:
1417                  * use unspecified address as the source address
1418                  * when a valid link-local address is not available.
1419                  */
1420                 memset(&addr_buf, 0, sizeof(addr_buf));
1421         }
1422
1423         ip6_nd_hdr(sk, skb, dev, &addr_buf, &mld2_all_mcr, NEXTHDR_HOP, 0);
1424
1425         memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1426
1427         skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
1428         skb_put(skb, sizeof(*pmr));
1429         pmr = (struct mld2_report *)skb_transport_header(skb);
1430         pmr->type = ICMPV6_MLD2_REPORT;
1431         pmr->resv1 = 0;
1432         pmr->csum = 0;
1433         pmr->resv2 = 0;
1434         pmr->ngrec = 0;
1435         return skb;
1436 }
1437
1438 static inline int mld_dev_queue_xmit2(struct sk_buff *skb)
1439 {
1440         struct net_device *dev = skb->dev;
1441
1442         if (dev->hard_header) {
1443                 unsigned char ha[MAX_ADDR_LEN];
1444                 int err;
1445
1446                 ndisc_mc_map(&ipv6_hdr(skb)->daddr, ha, dev, 1);
1447                 err = dev->hard_header(skb, dev, ETH_P_IPV6, ha, NULL, skb->len);
1448                 if (err < 0) {
1449                         kfree_skb(skb);
1450                         return err;
1451                 }
1452         }
1453         return dev_queue_xmit(skb);
1454 }
1455
1456 static inline int mld_dev_queue_xmit(struct sk_buff *skb)
1457 {
1458         return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL, skb->dev,
1459                        mld_dev_queue_xmit2);
1460 }
1461
1462 static void mld_sendpack(struct sk_buff *skb)
1463 {
1464         struct ipv6hdr *pip6 = ipv6_hdr(skb);
1465         struct mld2_report *pmr =
1466                               (struct mld2_report *)skb_transport_header(skb);
1467         int payload_len, mldlen;
1468         struct inet6_dev *idev = in6_dev_get(skb->dev);
1469         int err;
1470
1471         IP6_INC_STATS(idev, IPSTATS_MIB_OUTREQUESTS);
1472         payload_len = (skb->tail - skb->network_header) - sizeof(*pip6);
1473         mldlen = skb->tail - skb->transport_header;
1474         pip6->payload_len = htons(payload_len);
1475
1476         pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1477                 IPPROTO_ICMPV6, csum_partial(skb_transport_header(skb),
1478                                              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(idev, IPSTATS_MIB_OUTMCASTPKTS);
1484         } else
1485                 IP6_INC_STATS(idev, 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_transport_header(skb);
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_transport_header(skb) : 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                 if (!skb)
1589                         return NULL;
1590                 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1591                 *psrc = psf->sf_addr;
1592                 scount++; stotal++;
1593                 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1594                      type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1595                         psf->sf_crcount--;
1596                         if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1597                                 if (psf_prev)
1598                                         psf_prev->sf_next = psf->sf_next;
1599                                 else
1600                                         *psf_list = psf->sf_next;
1601                                 kfree(psf);
1602                                 continue;
1603                         }
1604                 }
1605                 psf_prev = psf;
1606         }
1607
1608 empty_source:
1609         if (!stotal) {
1610                 if (type == MLD2_ALLOW_NEW_SOURCES ||
1611                     type == MLD2_BLOCK_OLD_SOURCES)
1612                         return skb;
1613                 if (pmc->mca_crcount || isquery) {
1614                         /* make sure we have room for group header */
1615                         if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1616                                 mld_sendpack(skb);
1617                                 skb = NULL; /* add_grhead will get a new one */
1618                         }
1619                         skb = add_grhead(skb, pmc, type, &pgr);
1620                 }
1621         }
1622         if (pgr)
1623                 pgr->grec_nsrcs = htons(scount);
1624
1625         if (isquery)
1626                 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1627         return skb;
1628 }
1629
1630 static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1631 {
1632         struct sk_buff *skb = NULL;
1633         int type;
1634
1635         if (!pmc) {
1636                 read_lock_bh(&idev->lock);
1637                 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1638                         if (pmc->mca_flags & MAF_NOREPORT)
1639                                 continue;
1640                         spin_lock_bh(&pmc->mca_lock);
1641                         if (pmc->mca_sfcount[MCAST_EXCLUDE])
1642                                 type = MLD2_MODE_IS_EXCLUDE;
1643                         else
1644                                 type = MLD2_MODE_IS_INCLUDE;
1645                         skb = add_grec(skb, pmc, type, 0, 0);
1646                         spin_unlock_bh(&pmc->mca_lock);
1647                 }
1648                 read_unlock_bh(&idev->lock);
1649         } else {
1650                 spin_lock_bh(&pmc->mca_lock);
1651                 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1652                         type = MLD2_MODE_IS_EXCLUDE;
1653                 else
1654                         type = MLD2_MODE_IS_INCLUDE;
1655                 skb = add_grec(skb, pmc, type, 0, 0);
1656                 spin_unlock_bh(&pmc->mca_lock);
1657         }
1658         if (skb)
1659                 mld_sendpack(skb);
1660 }
1661
1662 /*
1663  * remove zero-count source records from a source filter list
1664  */
1665 static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1666 {
1667         struct ip6_sf_list *psf_prev, *psf_next, *psf;
1668
1669         psf_prev = NULL;
1670         for (psf=*ppsf; psf; psf = psf_next) {
1671                 psf_next = psf->sf_next;
1672                 if (psf->sf_crcount == 0) {
1673                         if (psf_prev)
1674                                 psf_prev->sf_next = psf->sf_next;
1675                         else
1676                                 *ppsf = psf->sf_next;
1677                         kfree(psf);
1678                 } else
1679                         psf_prev = psf;
1680         }
1681 }
1682
1683 static void mld_send_cr(struct inet6_dev *idev)
1684 {
1685         struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1686         struct sk_buff *skb = NULL;
1687         int type, dtype;
1688
1689         read_lock_bh(&idev->lock);
1690         write_lock_bh(&idev->mc_lock);
1691
1692         /* deleted MCA's */
1693         pmc_prev = NULL;
1694         for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1695                 pmc_next = pmc->next;
1696                 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1697                         type = MLD2_BLOCK_OLD_SOURCES;
1698                         dtype = MLD2_BLOCK_OLD_SOURCES;
1699                         skb = add_grec(skb, pmc, type, 1, 0);
1700                         skb = add_grec(skb, pmc, dtype, 1, 1);
1701                 }
1702                 if (pmc->mca_crcount) {
1703                         if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1704                                 type = MLD2_CHANGE_TO_INCLUDE;
1705                                 skb = add_grec(skb, pmc, type, 1, 0);
1706                         }
1707                         pmc->mca_crcount--;
1708                         if (pmc->mca_crcount == 0) {
1709                                 mld_clear_zeros(&pmc->mca_tomb);
1710                                 mld_clear_zeros(&pmc->mca_sources);
1711                         }
1712                 }
1713                 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1714                     !pmc->mca_sources) {
1715                         if (pmc_prev)
1716                                 pmc_prev->next = pmc_next;
1717                         else
1718                                 idev->mc_tomb = pmc_next;
1719                         in6_dev_put(pmc->idev);
1720                         kfree(pmc);
1721                 } else
1722                         pmc_prev = pmc;
1723         }
1724         write_unlock_bh(&idev->mc_lock);
1725
1726         /* change recs */
1727         for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1728                 spin_lock_bh(&pmc->mca_lock);
1729                 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1730                         type = MLD2_BLOCK_OLD_SOURCES;
1731                         dtype = MLD2_ALLOW_NEW_SOURCES;
1732                 } else {
1733                         type = MLD2_ALLOW_NEW_SOURCES;
1734                         dtype = MLD2_BLOCK_OLD_SOURCES;
1735                 }
1736                 skb = add_grec(skb, pmc, type, 0, 0);
1737                 skb = add_grec(skb, pmc, dtype, 0, 1);  /* deleted sources */
1738
1739                 /* filter mode changes */
1740                 if (pmc->mca_crcount) {
1741                         if (pmc->mca_sfmode == MCAST_EXCLUDE)
1742                                 type = MLD2_CHANGE_TO_EXCLUDE;
1743                         else
1744                                 type = MLD2_CHANGE_TO_INCLUDE;
1745                         skb = add_grec(skb, pmc, type, 0, 0);
1746                         pmc->mca_crcount--;
1747                 }
1748                 spin_unlock_bh(&pmc->mca_lock);
1749         }
1750         read_unlock_bh(&idev->lock);
1751         if (!skb)
1752                 return;
1753         (void) mld_sendpack(skb);
1754 }
1755
1756 static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1757 {
1758         struct sock *sk = igmp6_socket->sk;
1759         struct inet6_dev *idev;
1760         struct sk_buff *skb;
1761         struct icmp6hdr *hdr;
1762         struct in6_addr *snd_addr;
1763         struct in6_addr *addrp;
1764         struct in6_addr addr_buf;
1765         struct in6_addr all_routers;
1766         int err, len, payload_len, full_len;
1767         u8 ra[8] = { IPPROTO_ICMPV6, 0,
1768                      IPV6_TLV_ROUTERALERT, 2, 0, 0,
1769                      IPV6_TLV_PADN, 0 };
1770
1771         rcu_read_lock();
1772         IP6_INC_STATS(__in6_dev_get(dev),
1773                       IPSTATS_MIB_OUTREQUESTS);
1774         rcu_read_unlock();
1775         snd_addr = addr;
1776         if (type == ICMPV6_MGM_REDUCTION) {
1777                 snd_addr = &all_routers;
1778                 ipv6_addr_all_routers(&all_routers);
1779         }
1780
1781         len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1782         payload_len = len + sizeof(ra);
1783         full_len = sizeof(struct ipv6hdr) + payload_len;
1784
1785         skb = sock_alloc_send_skb(sk, LL_RESERVED_SPACE(dev) + full_len, 1, &err);
1786
1787         if (skb == NULL) {
1788                 rcu_read_lock();
1789                 IP6_INC_STATS(__in6_dev_get(dev),
1790                               IPSTATS_MIB_OUTDISCARDS);
1791                 rcu_read_unlock();
1792                 return;
1793         }
1794
1795         skb_reserve(skb, LL_RESERVED_SPACE(dev));
1796
1797         if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
1798                 /* <draft-ietf-magma-mld-source-05.txt>:
1799                  * use unspecified address as the source address
1800                  * when a valid link-local address is not available.
1801                  */
1802                 memset(&addr_buf, 0, sizeof(addr_buf));
1803         }
1804
1805         ip6_nd_hdr(sk, skb, dev, &addr_buf, snd_addr, NEXTHDR_HOP, payload_len);
1806
1807         memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1808
1809         hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
1810         memset(hdr, 0, sizeof(struct icmp6hdr));
1811         hdr->icmp6_type = type;
1812
1813         addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
1814         ipv6_addr_copy(addrp, addr);
1815
1816         hdr->icmp6_cksum = csum_ipv6_magic(&addr_buf, snd_addr, len,
1817                                            IPPROTO_ICMPV6,
1818                                            csum_partial((__u8 *) hdr, len, 0));
1819
1820         idev = in6_dev_get(skb->dev);
1821
1822         err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
1823                 mld_dev_queue_xmit);
1824         if (!err) {
1825                 if (type == ICMPV6_MGM_REDUCTION)
1826                         ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBREDUCTIONS);
1827                 else
1828                         ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBRESPONSES);
1829                 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTMSGS);
1830                 IP6_INC_STATS(idev, IPSTATS_MIB_OUTMCASTPKTS);
1831         } else
1832                 IP6_INC_STATS(idev, IPSTATS_MIB_OUTDISCARDS);
1833
1834         if (likely(idev != NULL))
1835                 in6_dev_put(idev);
1836         return;
1837 }
1838
1839 static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1840         struct in6_addr *psfsrc)
1841 {
1842         struct ip6_sf_list *psf, *psf_prev;
1843         int rv = 0;
1844
1845         psf_prev = NULL;
1846         for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1847                 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1848                         break;
1849                 psf_prev = psf;
1850         }
1851         if (!psf || psf->sf_count[sfmode] == 0) {
1852                 /* source filter not found, or count wrong =>  bug */
1853                 return -ESRCH;
1854         }
1855         psf->sf_count[sfmode]--;
1856         if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1857                 struct inet6_dev *idev = pmc->idev;
1858
1859                 /* no more filters for this source */
1860                 if (psf_prev)
1861                         psf_prev->sf_next = psf->sf_next;
1862                 else
1863                         pmc->mca_sources = psf->sf_next;
1864                 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1865                     !MLD_V1_SEEN(idev)) {
1866                         psf->sf_crcount = idev->mc_qrv;
1867                         psf->sf_next = pmc->mca_tomb;
1868                         pmc->mca_tomb = psf;
1869                         rv = 1;
1870                 } else
1871                         kfree(psf);
1872         }
1873         return rv;
1874 }
1875
1876 static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
1877                           int sfmode, int sfcount, struct in6_addr *psfsrc,
1878                           int delta)
1879 {
1880         struct ifmcaddr6 *pmc;
1881         int     changerec = 0;
1882         int     i, err;
1883
1884         if (!idev)
1885                 return -ENODEV;
1886         read_lock_bh(&idev->lock);
1887         for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1888                 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1889                         break;
1890         }
1891         if (!pmc) {
1892                 /* MCA not found?? bug */
1893                 read_unlock_bh(&idev->lock);
1894                 return -ESRCH;
1895         }
1896         spin_lock_bh(&pmc->mca_lock);
1897         sf_markstate(pmc);
1898         if (!delta) {
1899                 if (!pmc->mca_sfcount[sfmode]) {
1900                         spin_unlock_bh(&pmc->mca_lock);
1901                         read_unlock_bh(&idev->lock);
1902                         return -EINVAL;
1903                 }
1904                 pmc->mca_sfcount[sfmode]--;
1905         }
1906         err = 0;
1907         for (i=0; i<sfcount; i++) {
1908                 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1909
1910                 changerec |= rv > 0;
1911                 if (!err && rv < 0)
1912                         err = rv;
1913         }
1914         if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1915             pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1916             pmc->mca_sfcount[MCAST_INCLUDE]) {
1917                 struct ip6_sf_list *psf;
1918
1919                 /* filter mode change */
1920                 pmc->mca_sfmode = MCAST_INCLUDE;
1921                 pmc->mca_crcount = idev->mc_qrv;
1922                 idev->mc_ifc_count = pmc->mca_crcount;
1923                 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1924                         psf->sf_crcount = 0;
1925                 mld_ifc_event(pmc->idev);
1926         } else if (sf_setstate(pmc) || changerec)
1927                 mld_ifc_event(pmc->idev);
1928         spin_unlock_bh(&pmc->mca_lock);
1929         read_unlock_bh(&idev->lock);
1930         return err;
1931 }
1932
1933 /*
1934  * Add multicast single-source filter to the interface list
1935  */
1936 static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1937         struct in6_addr *psfsrc, int delta)
1938 {
1939         struct ip6_sf_list *psf, *psf_prev;
1940
1941         psf_prev = NULL;
1942         for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1943                 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1944                         break;
1945                 psf_prev = psf;
1946         }
1947         if (!psf) {
1948                 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1949                 if (!psf)
1950                         return -ENOBUFS;
1951
1952                 psf->sf_addr = *psfsrc;
1953                 if (psf_prev) {
1954                         psf_prev->sf_next = psf;
1955                 } else
1956                         pmc->mca_sources = psf;
1957         }
1958         psf->sf_count[sfmode]++;
1959         return 0;
1960 }
1961
1962 static void sf_markstate(struct ifmcaddr6 *pmc)
1963 {
1964         struct ip6_sf_list *psf;
1965         int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1966
1967         for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1968                 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1969                         psf->sf_oldin = mca_xcount ==
1970                                 psf->sf_count[MCAST_EXCLUDE] &&
1971                                 !psf->sf_count[MCAST_INCLUDE];
1972                 } else
1973                         psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1974 }
1975
1976 static int sf_setstate(struct ifmcaddr6 *pmc)
1977 {
1978         struct ip6_sf_list *psf, *dpsf;
1979         int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1980         int qrv = pmc->idev->mc_qrv;
1981         int new_in, rv;
1982
1983         rv = 0;
1984         for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1985                 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1986                         new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1987                                 !psf->sf_count[MCAST_INCLUDE];
1988                 } else
1989                         new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1990                 if (new_in) {
1991                         if (!psf->sf_oldin) {
1992                                 struct ip6_sf_list *prev = NULL;
1993
1994                                 for (dpsf=pmc->mca_tomb; dpsf;
1995                                      dpsf=dpsf->sf_next) {
1996                                         if (ipv6_addr_equal(&dpsf->sf_addr,
1997                                             &psf->sf_addr))
1998                                                 break;
1999                                         prev = dpsf;
2000                                 }
2001                                 if (dpsf) {
2002                                         if (prev)
2003                                                 prev->sf_next = dpsf->sf_next;
2004                                         else
2005                                                 pmc->mca_tomb = dpsf->sf_next;
2006                                         kfree(dpsf);
2007                                 }
2008                                 psf->sf_crcount = qrv;
2009                                 rv++;
2010                         }
2011                 } else if (psf->sf_oldin) {
2012                         psf->sf_crcount = 0;
2013                         /*
2014                          * add or update "delete" records if an active filter
2015                          * is now inactive
2016                          */
2017                         for (dpsf=pmc->mca_tomb; dpsf; dpsf=dpsf->sf_next)
2018                                 if (ipv6_addr_equal(&dpsf->sf_addr,
2019                                     &psf->sf_addr))
2020                                         break;
2021                         if (!dpsf) {
2022                                 dpsf = (struct ip6_sf_list *)
2023                                         kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2024                                 if (!dpsf)
2025                                         continue;
2026                                 *dpsf = *psf;
2027                                 /* pmc->mca_lock held by callers */
2028                                 dpsf->sf_next = pmc->mca_tomb;
2029                                 pmc->mca_tomb = dpsf;
2030                         }
2031                         dpsf->sf_crcount = qrv;
2032                         rv++;
2033                 }
2034         }
2035         return rv;
2036 }
2037
2038 /*
2039  * Add multicast source filter list to the interface list
2040  */
2041 static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
2042                           int sfmode, int sfcount, struct in6_addr *psfsrc,
2043                           int delta)
2044 {
2045         struct ifmcaddr6 *pmc;
2046         int     isexclude;
2047         int     i, err;
2048
2049         if (!idev)
2050                 return -ENODEV;
2051         read_lock_bh(&idev->lock);
2052         for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
2053                 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2054                         break;
2055         }
2056         if (!pmc) {
2057                 /* MCA not found?? bug */
2058                 read_unlock_bh(&idev->lock);
2059                 return -ESRCH;
2060         }
2061         spin_lock_bh(&pmc->mca_lock);
2062
2063         sf_markstate(pmc);
2064         isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2065         if (!delta)
2066                 pmc->mca_sfcount[sfmode]++;
2067         err = 0;
2068         for (i=0; i<sfcount; i++) {
2069                 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
2070                 if (err)
2071                         break;
2072         }
2073         if (err) {
2074                 int j;
2075
2076                 if (!delta)
2077                         pmc->mca_sfcount[sfmode]--;
2078                 for (j=0; j<i; j++)
2079                         (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2080         } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
2081                 struct inet6_dev *idev = pmc->idev;
2082                 struct ip6_sf_list *psf;
2083
2084                 /* filter mode change */
2085                 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2086                         pmc->mca_sfmode = MCAST_EXCLUDE;
2087                 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2088                         pmc->mca_sfmode = MCAST_INCLUDE;
2089                 /* else no filters; keep old mode for reports */
2090
2091                 pmc->mca_crcount = idev->mc_qrv;
2092                 idev->mc_ifc_count = pmc->mca_crcount;
2093                 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
2094                         psf->sf_crcount = 0;
2095                 mld_ifc_event(idev);
2096         } else if (sf_setstate(pmc))
2097                 mld_ifc_event(idev);
2098         spin_unlock_bh(&pmc->mca_lock);
2099         read_unlock_bh(&idev->lock);
2100         return err;
2101 }
2102
2103 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2104 {
2105         struct ip6_sf_list *psf, *nextpsf;
2106
2107         for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
2108                 nextpsf = psf->sf_next;
2109                 kfree(psf);
2110         }
2111         pmc->mca_tomb = NULL;
2112         for (psf=pmc->mca_sources; psf; psf=nextpsf) {
2113                 nextpsf = psf->sf_next;
2114                 kfree(psf);
2115         }
2116         pmc->mca_sources = NULL;
2117         pmc->mca_sfmode = MCAST_EXCLUDE;
2118         pmc->mca_sfcount[MCAST_INCLUDE] = 0;
2119         pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2120 }
2121
2122
2123 static void igmp6_join_group(struct ifmcaddr6 *ma)
2124 {
2125         unsigned long delay;
2126
2127         if (ma->mca_flags & MAF_NOREPORT)
2128                 return;
2129
2130         igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2131
2132         delay = net_random() % IGMP6_UNSOLICITED_IVAL;
2133
2134         spin_lock_bh(&ma->mca_lock);
2135         if (del_timer(&ma->mca_timer)) {
2136                 atomic_dec(&ma->mca_refcnt);
2137                 delay = ma->mca_timer.expires - jiffies;
2138         }
2139
2140         if (!mod_timer(&ma->mca_timer, jiffies + delay))
2141                 atomic_inc(&ma->mca_refcnt);
2142         ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2143         spin_unlock_bh(&ma->mca_lock);
2144 }
2145
2146 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2147                             struct inet6_dev *idev)
2148 {
2149         int err;
2150
2151         /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
2152          * so no other readers or writers of iml or its sflist
2153          */
2154         if (iml->sflist == 0) {
2155                 /* any-source empty exclude case */
2156                 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2157         }
2158         err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2159                 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2160         sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2161         iml->sflist = NULL;
2162         return err;
2163 }
2164
2165 static void igmp6_leave_group(struct ifmcaddr6 *ma)
2166 {
2167         if (MLD_V1_SEEN(ma->idev)) {
2168                 if (ma->mca_flags & MAF_LAST_REPORTER)
2169                         igmp6_send(&ma->mca_addr, ma->idev->dev,
2170                                 ICMPV6_MGM_REDUCTION);
2171         } else {
2172                 mld_add_delrec(ma->idev, ma);
2173                 mld_ifc_event(ma->idev);
2174         }
2175 }
2176
2177 static void mld_gq_timer_expire(unsigned long data)
2178 {
2179         struct inet6_dev *idev = (struct inet6_dev *)data;
2180
2181         idev->mc_gq_running = 0;
2182         mld_send_report(idev, NULL);
2183         __in6_dev_put(idev);
2184 }
2185
2186 static void mld_ifc_timer_expire(unsigned long data)
2187 {
2188         struct inet6_dev *idev = (struct inet6_dev *)data;
2189
2190         mld_send_cr(idev);
2191         if (idev->mc_ifc_count) {
2192                 idev->mc_ifc_count--;
2193                 if (idev->mc_ifc_count)
2194                         mld_ifc_start_timer(idev, idev->mc_maxdelay);
2195         }
2196         __in6_dev_put(idev);
2197 }
2198
2199 static void mld_ifc_event(struct inet6_dev *idev)
2200 {
2201         if (MLD_V1_SEEN(idev))
2202                 return;
2203         idev->mc_ifc_count = idev->mc_qrv;
2204         mld_ifc_start_timer(idev, 1);
2205 }
2206
2207
2208 static void igmp6_timer_handler(unsigned long data)
2209 {
2210         struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2211
2212         if (MLD_V1_SEEN(ma->idev))
2213                 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2214         else
2215                 mld_send_report(ma->idev, ma);
2216
2217         spin_lock(&ma->mca_lock);
2218         ma->mca_flags |=  MAF_LAST_REPORTER;
2219         ma->mca_flags &= ~MAF_TIMER_RUNNING;
2220         spin_unlock(&ma->mca_lock);
2221         ma_put(ma);
2222 }
2223
2224 /* Device going down */
2225
2226 void ipv6_mc_down(struct inet6_dev *idev)
2227 {
2228         struct ifmcaddr6 *i;
2229
2230         /* Withdraw multicast list */
2231
2232         read_lock_bh(&idev->lock);
2233         idev->mc_ifc_count = 0;
2234         if (del_timer(&idev->mc_ifc_timer))
2235                 __in6_dev_put(idev);
2236         idev->mc_gq_running = 0;
2237         if (del_timer(&idev->mc_gq_timer))
2238                 __in6_dev_put(idev);
2239
2240         for (i = idev->mc_list; i; i=i->next)
2241                 igmp6_group_dropped(i);
2242         read_unlock_bh(&idev->lock);
2243
2244         mld_clear_delrec(idev);
2245 }
2246
2247
2248 /* Device going up */
2249
2250 void ipv6_mc_up(struct inet6_dev *idev)
2251 {
2252         struct ifmcaddr6 *i;
2253
2254         /* Install multicast list, except for all-nodes (already installed) */
2255
2256         read_lock_bh(&idev->lock);
2257         for (i = idev->mc_list; i; i=i->next)
2258                 igmp6_group_added(i);
2259         read_unlock_bh(&idev->lock);
2260 }
2261
2262 /* IPv6 device initialization. */
2263
2264 void ipv6_mc_init_dev(struct inet6_dev *idev)
2265 {
2266         write_lock_bh(&idev->lock);
2267         rwlock_init(&idev->mc_lock);
2268         idev->mc_gq_running = 0;
2269         init_timer(&idev->mc_gq_timer);
2270         idev->mc_gq_timer.data = (unsigned long) idev;
2271         idev->mc_gq_timer.function = &mld_gq_timer_expire;
2272         idev->mc_tomb = NULL;
2273         idev->mc_ifc_count = 0;
2274         init_timer(&idev->mc_ifc_timer);
2275         idev->mc_ifc_timer.data = (unsigned long) idev;
2276         idev->mc_ifc_timer.function = &mld_ifc_timer_expire;
2277         idev->mc_qrv = MLD_QRV_DEFAULT;
2278         idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2279         idev->mc_v1_seen = 0;
2280         write_unlock_bh(&idev->lock);
2281 }
2282
2283 /*
2284  *      Device is about to be destroyed: clean up.
2285  */
2286
2287 void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2288 {
2289         struct ifmcaddr6 *i;
2290         struct in6_addr maddr;
2291
2292         /* Deactivate timers */
2293         ipv6_mc_down(idev);
2294
2295         /* Delete all-nodes address. */
2296         ipv6_addr_all_nodes(&maddr);
2297
2298         /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2299          * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2300          * fail.
2301          */
2302         __ipv6_dev_mc_dec(idev, &maddr);
2303
2304         if (idev->cnf.forwarding) {
2305                 ipv6_addr_all_routers(&maddr);
2306                 __ipv6_dev_mc_dec(idev, &maddr);
2307         }
2308
2309         write_lock_bh(&idev->lock);
2310         while ((i = idev->mc_list) != NULL) {
2311                 idev->mc_list = i->next;
2312                 write_unlock_bh(&idev->lock);
2313
2314                 igmp6_group_dropped(i);
2315                 ma_put(i);
2316
2317                 write_lock_bh(&idev->lock);
2318         }
2319         write_unlock_bh(&idev->lock);
2320 }
2321
2322 #ifdef CONFIG_PROC_FS
2323 struct igmp6_mc_iter_state {
2324         struct net_device *dev;
2325         struct inet6_dev *idev;
2326 };
2327
2328 #define igmp6_mc_seq_private(seq)       ((struct igmp6_mc_iter_state *)(seq)->private)
2329
2330 static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2331 {
2332         struct ifmcaddr6 *im = NULL;
2333         struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2334
2335         state->idev = NULL;
2336         for_each_netdev(&init_net, state->dev) {
2337                 struct inet6_dev *idev;
2338                 idev = in6_dev_get(state->dev);
2339                 if (!idev)
2340                         continue;
2341                 read_lock_bh(&idev->lock);
2342                 im = idev->mc_list;
2343                 if (im) {
2344                         state->idev = idev;
2345                         break;
2346                 }
2347                 read_unlock_bh(&idev->lock);
2348                 in6_dev_put(idev);
2349         }
2350         return im;
2351 }
2352
2353 static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2354 {
2355         struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2356
2357         im = im->next;
2358         while (!im) {
2359                 if (likely(state->idev != NULL)) {
2360                         read_unlock_bh(&state->idev->lock);
2361                         in6_dev_put(state->idev);
2362                 }
2363                 state->dev = next_net_device(state->dev);
2364                 if (!state->dev) {
2365                         state->idev = NULL;
2366                         break;
2367                 }
2368                 state->idev = in6_dev_get(state->dev);
2369                 if (!state->idev)
2370                         continue;
2371                 read_lock_bh(&state->idev->lock);
2372                 im = state->idev->mc_list;
2373         }
2374         return im;
2375 }
2376
2377 static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2378 {
2379         struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2380         if (im)
2381                 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2382                         --pos;
2383         return pos ? NULL : im;
2384 }
2385
2386 static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2387 {
2388         read_lock(&dev_base_lock);
2389         return igmp6_mc_get_idx(seq, *pos);
2390 }
2391
2392 static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2393 {
2394         struct ifmcaddr6 *im;
2395         im = igmp6_mc_get_next(seq, v);
2396         ++*pos;
2397         return im;
2398 }
2399
2400 static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2401 {
2402         struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2403         if (likely(state->idev != NULL)) {
2404                 read_unlock_bh(&state->idev->lock);
2405                 in6_dev_put(state->idev);
2406                 state->idev = NULL;
2407         }
2408         state->dev = NULL;
2409         read_unlock(&dev_base_lock);
2410 }
2411
2412 static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2413 {
2414         struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2415         struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2416
2417         seq_printf(seq,
2418                    "%-4d %-15s " NIP6_SEQFMT " %5d %08X %ld\n",
2419                    state->dev->ifindex, state->dev->name,
2420                    NIP6(im->mca_addr),
2421                    im->mca_users, im->mca_flags,
2422                    (im->mca_flags&MAF_TIMER_RUNNING) ?
2423                    jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2424         return 0;
2425 }
2426
2427 static const struct seq_operations igmp6_mc_seq_ops = {
2428         .start  =       igmp6_mc_seq_start,
2429         .next   =       igmp6_mc_seq_next,
2430         .stop   =       igmp6_mc_seq_stop,
2431         .show   =       igmp6_mc_seq_show,
2432 };
2433
2434 static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2435 {
2436         struct seq_file *seq;
2437         int rc = -ENOMEM;
2438         struct igmp6_mc_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
2439
2440         if (!s)
2441                 goto out;
2442
2443         rc = seq_open(file, &igmp6_mc_seq_ops);
2444         if (rc)
2445                 goto out_kfree;
2446
2447         seq = file->private_data;
2448         seq->private = s;
2449 out:
2450         return rc;
2451 out_kfree:
2452         kfree(s);
2453         goto out;
2454 }
2455
2456 static const struct file_operations igmp6_mc_seq_fops = {
2457         .owner          =       THIS_MODULE,
2458         .open           =       igmp6_mc_seq_open,
2459         .read           =       seq_read,
2460         .llseek         =       seq_lseek,
2461         .release        =       seq_release_private,
2462 };
2463
2464 struct igmp6_mcf_iter_state {
2465         struct net_device *dev;
2466         struct inet6_dev *idev;
2467         struct ifmcaddr6 *im;
2468 };
2469
2470 #define igmp6_mcf_seq_private(seq)      ((struct igmp6_mcf_iter_state *)(seq)->private)
2471
2472 static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2473 {
2474         struct ip6_sf_list *psf = NULL;
2475         struct ifmcaddr6 *im = NULL;
2476         struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2477
2478         state->idev = NULL;
2479         state->im = NULL;
2480         for_each_netdev(&init_net, state->dev) {
2481                 struct inet6_dev *idev;
2482                 idev = in6_dev_get(state->dev);
2483                 if (unlikely(idev == NULL))
2484                         continue;
2485                 read_lock_bh(&idev->lock);
2486                 im = idev->mc_list;
2487                 if (likely(im != NULL)) {
2488                         spin_lock_bh(&im->mca_lock);
2489                         psf = im->mca_sources;
2490                         if (likely(psf != NULL)) {
2491                                 state->im = im;
2492                                 state->idev = idev;
2493                                 break;
2494                         }
2495                         spin_unlock_bh(&im->mca_lock);
2496                 }
2497                 read_unlock_bh(&idev->lock);
2498                 in6_dev_put(idev);
2499         }
2500         return psf;
2501 }
2502
2503 static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2504 {
2505         struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2506
2507         psf = psf->sf_next;
2508         while (!psf) {
2509                 spin_unlock_bh(&state->im->mca_lock);
2510                 state->im = state->im->next;
2511                 while (!state->im) {
2512                         if (likely(state->idev != NULL)) {
2513                                 read_unlock_bh(&state->idev->lock);
2514                                 in6_dev_put(state->idev);
2515                         }
2516                         state->dev = next_net_device(state->dev);
2517                         if (!state->dev) {
2518                                 state->idev = NULL;
2519                                 goto out;
2520                         }
2521                         state->idev = in6_dev_get(state->dev);
2522                         if (!state->idev)
2523                                 continue;
2524                         read_lock_bh(&state->idev->lock);
2525                         state->im = state->idev->mc_list;
2526                 }
2527                 if (!state->im)
2528                         break;
2529                 spin_lock_bh(&state->im->mca_lock);
2530                 psf = state->im->mca_sources;
2531         }
2532 out:
2533         return psf;
2534 }
2535
2536 static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2537 {
2538         struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2539         if (psf)
2540                 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2541                         --pos;
2542         return pos ? NULL : psf;
2543 }
2544
2545 static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2546 {
2547         read_lock(&dev_base_lock);
2548         return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2549 }
2550
2551 static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2552 {
2553         struct ip6_sf_list *psf;
2554         if (v == SEQ_START_TOKEN)
2555                 psf = igmp6_mcf_get_first(seq);
2556         else
2557                 psf = igmp6_mcf_get_next(seq, v);
2558         ++*pos;
2559         return psf;
2560 }
2561
2562 static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2563 {
2564         struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2565         if (likely(state->im != NULL)) {
2566                 spin_unlock_bh(&state->im->mca_lock);
2567                 state->im = NULL;
2568         }
2569         if (likely(state->idev != NULL)) {
2570                 read_unlock_bh(&state->idev->lock);
2571                 in6_dev_put(state->idev);
2572                 state->idev = NULL;
2573         }
2574         state->dev = NULL;
2575         read_unlock(&dev_base_lock);
2576 }
2577
2578 static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2579 {
2580         struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2581         struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2582
2583         if (v == SEQ_START_TOKEN) {
2584                 seq_printf(seq,
2585                            "%3s %6s "
2586                            "%32s %32s %6s %6s\n", "Idx",
2587                            "Device", "Multicast Address",
2588                            "Source Address", "INC", "EXC");
2589         } else {
2590                 seq_printf(seq,
2591                            "%3d %6.6s " NIP6_SEQFMT " " NIP6_SEQFMT " %6lu %6lu\n",
2592                            state->dev->ifindex, state->dev->name,
2593                            NIP6(state->im->mca_addr),
2594                            NIP6(psf->sf_addr),
2595                            psf->sf_count[MCAST_INCLUDE],
2596                            psf->sf_count[MCAST_EXCLUDE]);
2597         }
2598         return 0;
2599 }
2600
2601 static const struct seq_operations igmp6_mcf_seq_ops = {
2602         .start  =       igmp6_mcf_seq_start,
2603         .next   =       igmp6_mcf_seq_next,
2604         .stop   =       igmp6_mcf_seq_stop,
2605         .show   =       igmp6_mcf_seq_show,
2606 };
2607
2608 static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2609 {
2610         struct seq_file *seq;
2611         int rc = -ENOMEM;
2612         struct igmp6_mcf_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
2613
2614         if (!s)
2615                 goto out;
2616
2617         rc = seq_open(file, &igmp6_mcf_seq_ops);
2618         if (rc)
2619                 goto out_kfree;
2620
2621         seq = file->private_data;
2622         seq->private = s;
2623 out:
2624         return rc;
2625 out_kfree:
2626         kfree(s);
2627         goto out;
2628 }
2629
2630 static const struct file_operations igmp6_mcf_seq_fops = {
2631         .owner          =       THIS_MODULE,
2632         .open           =       igmp6_mcf_seq_open,
2633         .read           =       seq_read,
2634         .llseek         =       seq_lseek,
2635         .release        =       seq_release_private,
2636 };
2637 #endif
2638
2639 int __init igmp6_init(struct net_proto_family *ops)
2640 {
2641         struct ipv6_pinfo *np;
2642         struct sock *sk;
2643         int err;
2644
2645         err = sock_create_kern(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6, &igmp6_socket);
2646         if (err < 0) {
2647                 printk(KERN_ERR
2648                        "Failed to initialize the IGMP6 control socket (err %d).\n",
2649                        err);
2650                 igmp6_socket = NULL; /* For safety. */
2651                 return err;
2652         }
2653
2654         sk = igmp6_socket->sk;
2655         sk->sk_allocation = GFP_ATOMIC;
2656         sk->sk_prot->unhash(sk);
2657
2658         np = inet6_sk(sk);
2659         np->hop_limit = 1;
2660
2661 #ifdef CONFIG_PROC_FS
2662         proc_net_fops_create(&init_net, "igmp6", S_IRUGO, &igmp6_mc_seq_fops);
2663         proc_net_fops_create(&init_net, "mcfilter6", S_IRUGO, &igmp6_mcf_seq_fops);
2664 #endif
2665
2666         return 0;
2667 }
2668
2669 void igmp6_cleanup(void)
2670 {
2671         sock_release(igmp6_socket);
2672         igmp6_socket = NULL; /* for safety */
2673
2674 #ifdef CONFIG_PROC_FS
2675         proc_net_remove(&init_net, "mcfilter6");
2676         proc_net_remove(&init_net, "igmp6");
2677 #endif
2678 }