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