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