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