]> Pileus Git - ~andy/linux/blob - net/ipv6/mcast.c
[IPV4]: (INCLUDE,empty)/leave-group equivalence for full-state MSF APIs & errno fix
[~andy/linux] / 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         if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
513                 leavegroup = 1;
514                 goto done;
515         }
516
517         for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
518                 if (pmc->ifindex != gsf->gf_interface)
519                         continue;
520                 if (ipv6_addr_equal(&pmc->addr, group))
521                         break;
522         }
523         if (!pmc) {             /* must have a prior join */
524                 err = -EINVAL;
525                 goto done;
526         }
527         if (gsf->gf_numsrc) {
528                 newpsl = (struct ip6_sf_socklist *)sock_kmalloc(sk,
529                                 IP6_SFLSIZE(gsf->gf_numsrc), GFP_ATOMIC);
530                 if (!newpsl) {
531                         err = -ENOBUFS;
532                         goto done;
533                 }
534                 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
535                 for (i=0; i<newpsl->sl_count; ++i) {
536                         struct sockaddr_in6 *psin6;
537
538                         psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
539                         newpsl->sl_addr[i] = psin6->sin6_addr;
540                 }
541                 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
542                         newpsl->sl_count, newpsl->sl_addr, 0);
543                 if (err) {
544                         sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
545                         goto done;
546                 }
547         } else
548                 newpsl = NULL;
549         psl = pmc->sflist;
550         if (psl) {
551                 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
552                         psl->sl_count, psl->sl_addr, 0);
553                 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
554         } else
555                 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
556         pmc->sflist = newpsl;
557         pmc->sfmode = gsf->gf_fmode;
558         err = 0;
559 done:
560         read_unlock_bh(&idev->lock);
561         in6_dev_put(idev);
562         dev_put(dev);
563         if (leavegroup)
564                 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
565         return err;
566 }
567
568 int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
569         struct group_filter __user *optval, int __user *optlen)
570 {
571         int err, i, count, copycount;
572         struct in6_addr *group;
573         struct ipv6_mc_socklist *pmc;
574         struct inet6_dev *idev;
575         struct net_device *dev;
576         struct ipv6_pinfo *inet6 = inet6_sk(sk);
577         struct ip6_sf_socklist *psl;
578
579         group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
580
581         if (!ipv6_addr_is_multicast(group))
582                 return -EINVAL;
583
584         idev = ip6_mc_find_dev(group, gsf->gf_interface);
585
586         if (!idev)
587                 return -ENODEV;
588
589         dev = idev->dev;
590
591         err = -EADDRNOTAVAIL;
592
593         for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
594                 if (pmc->ifindex != gsf->gf_interface)
595                         continue;
596                 if (ipv6_addr_equal(group, &pmc->addr))
597                         break;
598         }
599         if (!pmc)               /* must have a prior join */
600                 goto done;
601         gsf->gf_fmode = pmc->sfmode;
602         psl = pmc->sflist;
603         count = psl ? psl->sl_count : 0;
604         read_unlock_bh(&idev->lock);
605         in6_dev_put(idev);
606         dev_put(dev);
607
608         copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
609         gsf->gf_numsrc = count;
610         if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
611             copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
612                 return -EFAULT;
613         }
614         for (i=0; i<copycount; i++) {
615                 struct sockaddr_in6 *psin6;
616                 struct sockaddr_storage ss;
617
618                 psin6 = (struct sockaddr_in6 *)&ss;
619                 memset(&ss, 0, sizeof(ss));
620                 psin6->sin6_family = AF_INET6;
621                 psin6->sin6_addr = psl->sl_addr[i];
622                 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
623                         return -EFAULT;
624         }
625         return 0;
626 done:
627         read_unlock_bh(&idev->lock);
628         in6_dev_put(idev);
629         dev_put(dev);
630         return err;
631 }
632
633 int inet6_mc_check(struct sock *sk, struct in6_addr *mc_addr,
634         struct in6_addr *src_addr)
635 {
636         struct ipv6_pinfo *np = inet6_sk(sk);
637         struct ipv6_mc_socklist *mc;
638         struct ip6_sf_socklist *psl;
639         int rv = 1;
640
641         read_lock(&ipv6_sk_mc_lock);
642         for (mc = np->ipv6_mc_list; mc; mc = mc->next) {
643                 if (ipv6_addr_equal(&mc->addr, mc_addr))
644                         break;
645         }
646         if (!mc) {
647                 read_unlock(&ipv6_sk_mc_lock);
648                 return 1;
649         }
650         psl = mc->sflist;
651         if (!psl) {
652                 rv = mc->sfmode == MCAST_EXCLUDE;
653         } else {
654                 int i;
655
656                 for (i=0; i<psl->sl_count; i++) {
657                         if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
658                                 break;
659                 }
660                 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
661                         rv = 0;
662                 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
663                         rv = 0;
664         }
665         read_unlock(&ipv6_sk_mc_lock);
666
667         return rv;
668 }
669
670 static void ma_put(struct ifmcaddr6 *mc)
671 {
672         if (atomic_dec_and_test(&mc->mca_refcnt)) {
673                 in6_dev_put(mc->idev);
674                 kfree(mc);
675         }
676 }
677
678 static void igmp6_group_added(struct ifmcaddr6 *mc)
679 {
680         struct net_device *dev = mc->idev->dev;
681         char buf[MAX_ADDR_LEN];
682
683         spin_lock_bh(&mc->mca_lock);
684         if (!(mc->mca_flags&MAF_LOADED)) {
685                 mc->mca_flags |= MAF_LOADED;
686                 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
687                         dev_mc_add(dev, buf, dev->addr_len, 0);
688         }
689         spin_unlock_bh(&mc->mca_lock);
690
691         if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
692                 return;
693
694         if (MLD_V1_SEEN(mc->idev)) {
695                 igmp6_join_group(mc);
696                 return;
697         }
698         /* else v2 */
699
700         mc->mca_crcount = mc->idev->mc_qrv;
701         mld_ifc_event(mc->idev);
702 }
703
704 static void igmp6_group_dropped(struct ifmcaddr6 *mc)
705 {
706         struct net_device *dev = mc->idev->dev;
707         char buf[MAX_ADDR_LEN];
708
709         spin_lock_bh(&mc->mca_lock);
710         if (mc->mca_flags&MAF_LOADED) {
711                 mc->mca_flags &= ~MAF_LOADED;
712                 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
713                         dev_mc_delete(dev, buf, dev->addr_len, 0);
714         }
715
716         if (mc->mca_flags & MAF_NOREPORT)
717                 goto done;
718         spin_unlock_bh(&mc->mca_lock);
719
720         if (!mc->idev->dead)
721                 igmp6_leave_group(mc);
722
723         spin_lock_bh(&mc->mca_lock);
724         if (del_timer(&mc->mca_timer))
725                 atomic_dec(&mc->mca_refcnt);
726 done:
727         ip6_mc_clear_src(mc);
728         spin_unlock_bh(&mc->mca_lock);
729 }
730
731 /*
732  * deleted ifmcaddr6 manipulation
733  */
734 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
735 {
736         struct ifmcaddr6 *pmc;
737
738         /* this is an "ifmcaddr6" for convenience; only the fields below
739          * are actually used. In particular, the refcnt and users are not
740          * used for management of the delete list. Using the same structure
741          * for deleted items allows change reports to use common code with
742          * non-deleted or query-response MCA's.
743          */
744         pmc = (struct ifmcaddr6 *)kmalloc(sizeof(*pmc), GFP_ATOMIC);
745         if (!pmc)
746                 return;
747         memset(pmc, 0, sizeof(*pmc));
748         spin_lock_bh(&im->mca_lock);
749         spin_lock_init(&pmc->mca_lock);
750         pmc->idev = im->idev;
751         in6_dev_hold(idev);
752         pmc->mca_addr = im->mca_addr;
753         pmc->mca_crcount = idev->mc_qrv;
754         pmc->mca_sfmode = im->mca_sfmode;
755         if (pmc->mca_sfmode == MCAST_INCLUDE) {
756                 struct ip6_sf_list *psf;
757
758                 pmc->mca_tomb = im->mca_tomb;
759                 pmc->mca_sources = im->mca_sources;
760                 im->mca_tomb = im->mca_sources = NULL;
761                 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
762                         psf->sf_crcount = pmc->mca_crcount;
763         }
764         spin_unlock_bh(&im->mca_lock);
765
766         write_lock_bh(&idev->mc_lock);
767         pmc->next = idev->mc_tomb;
768         idev->mc_tomb = pmc;
769         write_unlock_bh(&idev->mc_lock);
770 }
771
772 static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *pmca)
773 {
774         struct ifmcaddr6 *pmc, *pmc_prev;
775         struct ip6_sf_list *psf, *psf_next;
776
777         write_lock_bh(&idev->mc_lock);
778         pmc_prev = NULL;
779         for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
780                 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
781                         break;
782                 pmc_prev = pmc;
783         }
784         if (pmc) {
785                 if (pmc_prev)
786                         pmc_prev->next = pmc->next;
787                 else
788                         idev->mc_tomb = pmc->next;
789         }
790         write_unlock_bh(&idev->mc_lock);
791         if (pmc) {
792                 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
793                         psf_next = psf->sf_next;
794                         kfree(psf);
795                 }
796                 in6_dev_put(pmc->idev);
797                 kfree(pmc);
798         }
799 }
800
801 static void mld_clear_delrec(struct inet6_dev *idev)
802 {
803         struct ifmcaddr6 *pmc, *nextpmc;
804
805         write_lock_bh(&idev->mc_lock);
806         pmc = idev->mc_tomb;
807         idev->mc_tomb = NULL;
808         write_unlock_bh(&idev->mc_lock);
809
810         for (; pmc; pmc = nextpmc) {
811                 nextpmc = pmc->next;
812                 ip6_mc_clear_src(pmc);
813                 in6_dev_put(pmc->idev);
814                 kfree(pmc);
815         }
816
817         /* clear dead sources, too */
818         read_lock_bh(&idev->lock);
819         for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
820                 struct ip6_sf_list *psf, *psf_next;
821
822                 spin_lock_bh(&pmc->mca_lock);
823                 psf = pmc->mca_tomb;
824                 pmc->mca_tomb = NULL;
825                 spin_unlock_bh(&pmc->mca_lock);
826                 for (; psf; psf=psf_next) {
827                         psf_next = psf->sf_next;
828                         kfree(psf);
829                 }
830         }
831         read_unlock_bh(&idev->lock);
832 }
833
834
835 /*
836  *      device multicast group inc (add if not found)
837  */
838 int ipv6_dev_mc_inc(struct net_device *dev, struct in6_addr *addr)
839 {
840         struct ifmcaddr6 *mc;
841         struct inet6_dev *idev;
842
843         idev = in6_dev_get(dev);
844
845         if (idev == NULL)
846                 return -EINVAL;
847
848         write_lock_bh(&idev->lock);
849         if (idev->dead) {
850                 write_unlock_bh(&idev->lock);
851                 in6_dev_put(idev);
852                 return -ENODEV;
853         }
854
855         for (mc = idev->mc_list; mc; mc = mc->next) {
856                 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
857                         mc->mca_users++;
858                         write_unlock_bh(&idev->lock);
859                         ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
860                                 NULL, 0);
861                         in6_dev_put(idev);
862                         return 0;
863                 }
864         }
865
866         /*
867          *      not found: create a new one.
868          */
869
870         mc = kmalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
871
872         if (mc == NULL) {
873                 write_unlock_bh(&idev->lock);
874                 in6_dev_put(idev);
875                 return -ENOMEM;
876         }
877
878         memset(mc, 0, sizeof(struct ifmcaddr6));
879         init_timer(&mc->mca_timer);
880         mc->mca_timer.function = igmp6_timer_handler;
881         mc->mca_timer.data = (unsigned long) mc;
882
883         ipv6_addr_copy(&mc->mca_addr, addr);
884         mc->idev = idev;
885         mc->mca_users = 1;
886         /* mca_stamp should be updated upon changes */
887         mc->mca_cstamp = mc->mca_tstamp = jiffies;
888         atomic_set(&mc->mca_refcnt, 2);
889         spin_lock_init(&mc->mca_lock);
890
891         /* initial mode is (EX, empty) */
892         mc->mca_sfmode = MCAST_EXCLUDE;
893         mc->mca_sfcount[MCAST_EXCLUDE] = 1;
894
895         if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
896             IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
897                 mc->mca_flags |= MAF_NOREPORT;
898
899         mc->next = idev->mc_list;
900         idev->mc_list = mc;
901         write_unlock_bh(&idev->lock);
902
903         mld_del_delrec(idev, &mc->mca_addr);
904         igmp6_group_added(mc);
905         ma_put(mc);
906         return 0;
907 }
908
909 /*
910  *      device multicast group del
911  */
912 int __ipv6_dev_mc_dec(struct inet6_dev *idev, struct in6_addr *addr)
913 {
914         struct ifmcaddr6 *ma, **map;
915
916         write_lock_bh(&idev->lock);
917         for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
918                 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
919                         if (--ma->mca_users == 0) {
920                                 *map = ma->next;
921                                 write_unlock_bh(&idev->lock);
922
923                                 igmp6_group_dropped(ma);
924
925                                 ma_put(ma);
926                                 return 0;
927                         }
928                         write_unlock_bh(&idev->lock);
929                         return 0;
930                 }
931         }
932         write_unlock_bh(&idev->lock);
933
934         return -ENOENT;
935 }
936
937 int ipv6_dev_mc_dec(struct net_device *dev, struct in6_addr *addr)
938 {
939         struct inet6_dev *idev = in6_dev_get(dev);
940         int err;
941
942         if (!idev)
943                 return -ENODEV;
944
945         err = __ipv6_dev_mc_dec(idev, addr);
946
947         in6_dev_put(idev);
948
949         return err;
950 }
951
952 /*
953  * identify MLD packets for MLD filter exceptions
954  */
955 int ipv6_is_mld(struct sk_buff *skb, int nexthdr)
956 {
957         struct icmp6hdr *pic;
958
959         if (nexthdr != IPPROTO_ICMPV6)
960                 return 0;
961
962         if (!pskb_may_pull(skb, sizeof(struct icmp6hdr)))
963                 return 0;
964
965         pic = (struct icmp6hdr *)skb->h.raw;
966
967         switch (pic->icmp6_type) {
968         case ICMPV6_MGM_QUERY:
969         case ICMPV6_MGM_REPORT:
970         case ICMPV6_MGM_REDUCTION:
971         case ICMPV6_MLD2_REPORT:
972                 return 1;
973         default:
974                 break;
975         }
976         return 0;
977 }
978
979 /*
980  *      check if the interface/address pair is valid
981  */
982 int ipv6_chk_mcast_addr(struct net_device *dev, struct in6_addr *group,
983         struct in6_addr *src_addr)
984 {
985         struct inet6_dev *idev;
986         struct ifmcaddr6 *mc;
987         int rv = 0;
988
989         idev = in6_dev_get(dev);
990         if (idev) {
991                 read_lock_bh(&idev->lock);
992                 for (mc = idev->mc_list; mc; mc=mc->next) {
993                         if (ipv6_addr_equal(&mc->mca_addr, group))
994                                 break;
995                 }
996                 if (mc) {
997                         if (src_addr && !ipv6_addr_any(src_addr)) {
998                                 struct ip6_sf_list *psf;
999
1000                                 spin_lock_bh(&mc->mca_lock);
1001                                 for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
1002                                         if (ipv6_addr_equal(&psf->sf_addr, src_addr))
1003                                                 break;
1004                                 }
1005                                 if (psf)
1006                                         rv = psf->sf_count[MCAST_INCLUDE] ||
1007                                                 psf->sf_count[MCAST_EXCLUDE] !=
1008                                                 mc->mca_sfcount[MCAST_EXCLUDE];
1009                                 else
1010                                         rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
1011                                 spin_unlock_bh(&mc->mca_lock);
1012                         } else
1013                                 rv = 1; /* don't filter unspecified source */
1014                 }
1015                 read_unlock_bh(&idev->lock);
1016                 in6_dev_put(idev);
1017         }
1018         return rv;
1019 }
1020
1021 static void mld_gq_start_timer(struct inet6_dev *idev)
1022 {
1023         int tv = net_random() % idev->mc_maxdelay;
1024
1025         idev->mc_gq_running = 1;
1026         if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
1027                 in6_dev_hold(idev);
1028 }
1029
1030 static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
1031 {
1032         int tv = net_random() % delay;
1033
1034         if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1035                 in6_dev_hold(idev);
1036 }
1037
1038 /*
1039  *      IGMP handling (alias multicast ICMPv6 messages)
1040  */
1041
1042 static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1043 {
1044         unsigned long delay = resptime;
1045
1046         /* Do not start timer for these addresses */
1047         if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1048             IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1049                 return;
1050
1051         if (del_timer(&ma->mca_timer)) {
1052                 atomic_dec(&ma->mca_refcnt);
1053                 delay = ma->mca_timer.expires - jiffies;
1054         }
1055
1056         if (delay >= resptime) {
1057                 if (resptime)
1058                         delay = net_random() % resptime;
1059                 else
1060                         delay = 1;
1061         }
1062         ma->mca_timer.expires = jiffies + delay;
1063         if (!mod_timer(&ma->mca_timer, jiffies + delay))
1064                 atomic_inc(&ma->mca_refcnt);
1065         ma->mca_flags |= MAF_TIMER_RUNNING;
1066 }
1067
1068 static void mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1069         struct in6_addr *srcs)
1070 {
1071         struct ip6_sf_list *psf;
1072         int i, scount;
1073
1074         scount = 0;
1075         for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1076                 if (scount == nsrcs)
1077                         break;
1078                 for (i=0; i<nsrcs; i++)
1079                         if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1080                                 psf->sf_gsresp = 1;
1081                                 scount++;
1082                                 break;
1083                         }
1084         }
1085 }
1086
1087 int igmp6_event_query(struct sk_buff *skb)
1088 {
1089         struct mld2_query *mlh2 = (struct mld2_query *) skb->h.raw;
1090         struct ifmcaddr6 *ma;
1091         struct in6_addr *group;
1092         unsigned long max_delay;
1093         struct inet6_dev *idev;
1094         struct icmp6hdr *hdr;
1095         int group_type;
1096         int mark = 0;
1097         int len;
1098
1099         if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1100                 return -EINVAL;
1101
1102         /* compute payload length excluding extension headers */
1103         len = ntohs(skb->nh.ipv6h->payload_len) + sizeof(struct ipv6hdr);
1104         len -= (char *)skb->h.raw - (char *)skb->nh.ipv6h; 
1105
1106         /* Drop queries with not link local source */
1107         if (!(ipv6_addr_type(&skb->nh.ipv6h->saddr)&IPV6_ADDR_LINKLOCAL))
1108                 return -EINVAL;
1109
1110         idev = in6_dev_get(skb->dev);
1111
1112         if (idev == NULL)
1113                 return 0;
1114
1115         hdr = (struct icmp6hdr *) skb->h.raw;
1116         group = (struct in6_addr *) (hdr + 1);
1117         group_type = ipv6_addr_type(group);
1118
1119         if (group_type != IPV6_ADDR_ANY &&
1120             !(group_type&IPV6_ADDR_MULTICAST)) {
1121                 in6_dev_put(idev);
1122                 return -EINVAL;
1123         }
1124
1125         if (len == 24) {
1126                 int switchback;
1127                 /* MLDv1 router present */
1128
1129                 /* Translate milliseconds to jiffies */
1130                 max_delay = (ntohs(hdr->icmp6_maxdelay)*HZ)/1000;
1131
1132                 switchback = (idev->mc_qrv + 1) * max_delay;
1133                 idev->mc_v1_seen = jiffies + switchback;
1134
1135                 /* cancel the interface change timer */
1136                 idev->mc_ifc_count = 0;
1137                 if (del_timer(&idev->mc_ifc_timer))
1138                         __in6_dev_put(idev);
1139                 /* clear deleted report items */
1140                 mld_clear_delrec(idev);
1141         } else if (len >= 28) {
1142                 max_delay = (MLDV2_MRC(ntohs(mlh2->mrc))*HZ)/1000;
1143                 if (!max_delay)
1144                         max_delay = 1;
1145                 idev->mc_maxdelay = max_delay;
1146                 if (mlh2->qrv)
1147                         idev->mc_qrv = mlh2->qrv;
1148                 if (group_type == IPV6_ADDR_ANY) { /* general query */
1149                         if (mlh2->nsrcs) {
1150                                 in6_dev_put(idev);
1151                                 return -EINVAL; /* no sources allowed */
1152                         }
1153                         mld_gq_start_timer(idev);
1154                         in6_dev_put(idev);
1155                         return 0;
1156                 }
1157                 /* mark sources to include, if group & source-specific */
1158                 mark = mlh2->nsrcs != 0;
1159         } else {
1160                 in6_dev_put(idev);
1161                 return -EINVAL;
1162         }
1163
1164         read_lock_bh(&idev->lock);
1165         if (group_type == IPV6_ADDR_ANY) {
1166                 for (ma = idev->mc_list; ma; ma=ma->next) {
1167                         spin_lock_bh(&ma->mca_lock);
1168                         igmp6_group_queried(ma, max_delay);
1169                         spin_unlock_bh(&ma->mca_lock);
1170                 }
1171         } else {
1172                 for (ma = idev->mc_list; ma; ma=ma->next) {
1173                         if (group_type != IPV6_ADDR_ANY &&
1174                             !ipv6_addr_equal(group, &ma->mca_addr))
1175                                 continue;
1176                         spin_lock_bh(&ma->mca_lock);
1177                         if (ma->mca_flags & MAF_TIMER_RUNNING) {
1178                                 /* gsquery <- gsquery && mark */
1179                                 if (!mark)
1180                                         ma->mca_flags &= ~MAF_GSQUERY;
1181                         } else {
1182                                 /* gsquery <- mark */
1183                                 if (mark)
1184                                         ma->mca_flags |= MAF_GSQUERY;
1185                                 else
1186                                         ma->mca_flags &= ~MAF_GSQUERY;
1187                         }
1188                         if (ma->mca_flags & MAF_GSQUERY)
1189                                 mld_marksources(ma, ntohs(mlh2->nsrcs),
1190                                         mlh2->srcs);
1191                         igmp6_group_queried(ma, max_delay);
1192                         spin_unlock_bh(&ma->mca_lock);
1193                         if (group_type != IPV6_ADDR_ANY)
1194                                 break;
1195                 }
1196         }
1197         read_unlock_bh(&idev->lock);
1198         in6_dev_put(idev);
1199
1200         return 0;
1201 }
1202
1203
1204 int igmp6_event_report(struct sk_buff *skb)
1205 {
1206         struct ifmcaddr6 *ma;
1207         struct in6_addr *addrp;
1208         struct inet6_dev *idev;
1209         struct icmp6hdr *hdr;
1210         int addr_type;
1211
1212         /* Our own report looped back. Ignore it. */
1213         if (skb->pkt_type == PACKET_LOOPBACK)
1214                 return 0;
1215
1216         if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1217                 return -EINVAL;
1218
1219         hdr = (struct icmp6hdr*) skb->h.raw;
1220
1221         /* Drop reports with not link local source */
1222         addr_type = ipv6_addr_type(&skb->nh.ipv6h->saddr);
1223         if (addr_type != IPV6_ADDR_ANY && 
1224             !(addr_type&IPV6_ADDR_LINKLOCAL))
1225                 return -EINVAL;
1226
1227         addrp = (struct in6_addr *) (hdr + 1);
1228
1229         idev = in6_dev_get(skb->dev);
1230         if (idev == NULL)
1231                 return -ENODEV;
1232
1233         /*
1234          *      Cancel the timer for this group
1235          */
1236
1237         read_lock_bh(&idev->lock);
1238         for (ma = idev->mc_list; ma; ma=ma->next) {
1239                 if (ipv6_addr_equal(&ma->mca_addr, addrp)) {
1240                         spin_lock(&ma->mca_lock);
1241                         if (del_timer(&ma->mca_timer))
1242                                 atomic_dec(&ma->mca_refcnt);
1243                         ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1244                         spin_unlock(&ma->mca_lock);
1245                         break;
1246                 }
1247         }
1248         read_unlock_bh(&idev->lock);
1249         in6_dev_put(idev);
1250         return 0;
1251 }
1252
1253 static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1254         int gdeleted, int sdeleted)
1255 {
1256         switch (type) {
1257         case MLD2_MODE_IS_INCLUDE:
1258         case MLD2_MODE_IS_EXCLUDE:
1259                 if (gdeleted || sdeleted)
1260                         return 0;
1261                 return !((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp);
1262         case MLD2_CHANGE_TO_INCLUDE:
1263                 if (gdeleted || sdeleted)
1264                         return 0;
1265                 return psf->sf_count[MCAST_INCLUDE] != 0;
1266         case MLD2_CHANGE_TO_EXCLUDE:
1267                 if (gdeleted || sdeleted)
1268                         return 0;
1269                 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1270                     psf->sf_count[MCAST_INCLUDE])
1271                         return 0;
1272                 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1273                         psf->sf_count[MCAST_EXCLUDE];
1274         case MLD2_ALLOW_NEW_SOURCES:
1275                 if (gdeleted || !psf->sf_crcount)
1276                         return 0;
1277                 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1278         case MLD2_BLOCK_OLD_SOURCES:
1279                 if (pmc->mca_sfmode == MCAST_INCLUDE)
1280                         return gdeleted || (psf->sf_crcount && sdeleted);
1281                 return psf->sf_crcount && !gdeleted && !sdeleted;
1282         }
1283         return 0;
1284 }
1285
1286 static int
1287 mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1288 {
1289         struct ip6_sf_list *psf;
1290         int scount = 0;
1291
1292         for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1293                 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1294                         continue;
1295                 scount++;
1296         }
1297         return scount;
1298 }
1299
1300 static struct sk_buff *mld_newpack(struct net_device *dev, int size)
1301 {
1302         struct sock *sk = igmp6_socket->sk;
1303         struct sk_buff *skb;
1304         struct mld2_report *pmr;
1305         struct in6_addr addr_buf;
1306         int err;
1307         u8 ra[8] = { IPPROTO_ICMPV6, 0,
1308                      IPV6_TLV_ROUTERALERT, 2, 0, 0,
1309                      IPV6_TLV_PADN, 0 };
1310
1311         /* we assume size > sizeof(ra) here */
1312         skb = sock_alloc_send_skb(sk, size + LL_RESERVED_SPACE(dev), 1, &err);
1313
1314         if (skb == 0)
1315                 return NULL;
1316
1317         skb_reserve(skb, LL_RESERVED_SPACE(dev));
1318
1319         if (ipv6_get_lladdr(dev, &addr_buf)) {
1320                 /* <draft-ietf-magma-mld-source-05.txt>:
1321                  * use unspecified address as the source address 
1322                  * when a valid link-local address is not available.
1323                  */
1324                 memset(&addr_buf, 0, sizeof(addr_buf));
1325         }
1326
1327         ip6_nd_hdr(sk, skb, dev, &addr_buf, &mld2_all_mcr, NEXTHDR_HOP, 0);
1328
1329         memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1330
1331         pmr =(struct mld2_report *)skb_put(skb, sizeof(*pmr));
1332         skb->h.raw = (unsigned char *)pmr;
1333         pmr->type = ICMPV6_MLD2_REPORT;
1334         pmr->resv1 = 0;
1335         pmr->csum = 0;
1336         pmr->resv2 = 0;
1337         pmr->ngrec = 0;
1338         return skb;
1339 }
1340
1341 static inline int mld_dev_queue_xmit2(struct sk_buff *skb)
1342 {
1343         struct net_device *dev = skb->dev;
1344
1345         if (dev->hard_header) {
1346                 unsigned char ha[MAX_ADDR_LEN];
1347                 int err;
1348
1349                 ndisc_mc_map(&skb->nh.ipv6h->daddr, ha, dev, 1);
1350                 err = dev->hard_header(skb, dev, ETH_P_IPV6, ha, NULL, skb->len);
1351                 if (err < 0) {
1352                         kfree_skb(skb);
1353                         return err;
1354                 }
1355         }
1356         return dev_queue_xmit(skb);
1357 }
1358
1359 static inline int mld_dev_queue_xmit(struct sk_buff *skb)
1360 {
1361         return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL, skb->dev,
1362                        mld_dev_queue_xmit2);
1363 }
1364
1365 static void mld_sendpack(struct sk_buff *skb)
1366 {
1367         struct ipv6hdr *pip6 = skb->nh.ipv6h;
1368         struct mld2_report *pmr = (struct mld2_report *)skb->h.raw;
1369         int payload_len, mldlen;
1370         struct inet6_dev *idev = in6_dev_get(skb->dev);
1371         int err;
1372
1373         IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
1374         payload_len = skb->tail - (unsigned char *)skb->nh.ipv6h -
1375                 sizeof(struct ipv6hdr);
1376         mldlen = skb->tail - skb->h.raw;
1377         pip6->payload_len = htons(payload_len);
1378
1379         pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1380                 IPPROTO_ICMPV6, csum_partial(skb->h.raw, mldlen, 0));
1381         err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
1382                 mld_dev_queue_xmit);
1383         if (!err) {
1384                 ICMP6_INC_STATS(idev,ICMP6_MIB_OUTMSGS);
1385                 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
1386         } else
1387                 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1388
1389         if (likely(idev != NULL))
1390                 in6_dev_put(idev);
1391 }
1392
1393 static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1394 {
1395         return sizeof(struct mld2_grec) + 4*mld_scount(pmc,type,gdel,sdel);
1396 }
1397
1398 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1399         int type, struct mld2_grec **ppgr)
1400 {
1401         struct net_device *dev = pmc->idev->dev;
1402         struct mld2_report *pmr;
1403         struct mld2_grec *pgr;
1404
1405         if (!skb)
1406                 skb = mld_newpack(dev, dev->mtu);
1407         if (!skb)
1408                 return NULL;
1409         pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1410         pgr->grec_type = type;
1411         pgr->grec_auxwords = 0;
1412         pgr->grec_nsrcs = 0;
1413         pgr->grec_mca = pmc->mca_addr;  /* structure copy */
1414         pmr = (struct mld2_report *)skb->h.raw;
1415         pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
1416         *ppgr = pgr;
1417         return skb;
1418 }
1419
1420 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1421         skb_tailroom(skb)) : 0)
1422
1423 static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1424         int type, int gdeleted, int sdeleted)
1425 {
1426         struct net_device *dev = pmc->idev->dev;
1427         struct mld2_report *pmr;
1428         struct mld2_grec *pgr = NULL;
1429         struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
1430         int scount, first, isquery, truncate;
1431
1432         if (pmc->mca_flags & MAF_NOREPORT)
1433                 return skb;
1434
1435         isquery = type == MLD2_MODE_IS_INCLUDE ||
1436                   type == MLD2_MODE_IS_EXCLUDE;
1437         truncate = type == MLD2_MODE_IS_EXCLUDE ||
1438                     type == MLD2_CHANGE_TO_EXCLUDE;
1439
1440         psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1441
1442         if (!*psf_list) {
1443                 if (type == MLD2_ALLOW_NEW_SOURCES ||
1444                     type == MLD2_BLOCK_OLD_SOURCES)
1445                         return skb;
1446                 if (pmc->mca_crcount || isquery) {
1447                         /* make sure we have room for group header and at
1448                          * least one source.
1449                          */
1450                         if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)+
1451                             sizeof(struct in6_addr)) {
1452                                 mld_sendpack(skb);
1453                                 skb = NULL; /* add_grhead will get a new one */
1454                         }
1455                         skb = add_grhead(skb, pmc, type, &pgr);
1456                 }
1457                 return skb;
1458         }
1459         pmr = skb ? (struct mld2_report *)skb->h.raw : NULL;
1460
1461         /* EX and TO_EX get a fresh packet, if needed */
1462         if (truncate) {
1463                 if (pmr && pmr->ngrec &&
1464                     AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1465                         if (skb)
1466                                 mld_sendpack(skb);
1467                         skb = mld_newpack(dev, dev->mtu);
1468                 }
1469         }
1470         first = 1;
1471         scount = 0;
1472         psf_prev = NULL;
1473         for (psf=*psf_list; psf; psf=psf_next) {
1474                 struct in6_addr *psrc;
1475
1476                 psf_next = psf->sf_next;
1477
1478                 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1479                         psf_prev = psf;
1480                         continue;
1481                 }
1482
1483                 /* clear marks on query responses */
1484                 if (isquery)
1485                         psf->sf_gsresp = 0;
1486
1487                 if (AVAILABLE(skb) < sizeof(*psrc) +
1488                     first*sizeof(struct mld2_grec)) {
1489                         if (truncate && !first)
1490                                 break;   /* truncate these */
1491                         if (pgr)
1492                                 pgr->grec_nsrcs = htons(scount);
1493                         if (skb)
1494                                 mld_sendpack(skb);
1495                         skb = mld_newpack(dev, dev->mtu);
1496                         first = 1;
1497                         scount = 0;
1498                 }
1499                 if (first) {
1500                         skb = add_grhead(skb, pmc, type, &pgr);
1501                         first = 0;
1502                 }
1503                 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1504                 *psrc = psf->sf_addr;
1505                 scount++;
1506                 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1507                      type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1508                         psf->sf_crcount--;
1509                         if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1510                                 if (psf_prev)
1511                                         psf_prev->sf_next = psf->sf_next;
1512                                 else
1513                                         *psf_list = psf->sf_next;
1514                                 kfree(psf);
1515                                 continue;
1516                         }
1517                 }
1518                 psf_prev = psf;
1519         }
1520         if (pgr)
1521                 pgr->grec_nsrcs = htons(scount);
1522
1523         if (isquery)
1524                 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1525         return skb;
1526 }
1527
1528 static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1529 {
1530         struct sk_buff *skb = NULL;
1531         int type;
1532
1533         if (!pmc) {
1534                 read_lock_bh(&idev->lock);
1535                 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1536                         if (pmc->mca_flags & MAF_NOREPORT)
1537                                 continue;
1538                         spin_lock_bh(&pmc->mca_lock);
1539                         if (pmc->mca_sfcount[MCAST_EXCLUDE])
1540                                 type = MLD2_MODE_IS_EXCLUDE;
1541                         else
1542                                 type = MLD2_MODE_IS_INCLUDE;
1543                         skb = add_grec(skb, pmc, type, 0, 0);
1544                         spin_unlock_bh(&pmc->mca_lock);
1545                 }
1546                 read_unlock_bh(&idev->lock);
1547         } else {
1548                 spin_lock_bh(&pmc->mca_lock);
1549                 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1550                         type = MLD2_MODE_IS_EXCLUDE;
1551                 else
1552                         type = MLD2_MODE_IS_INCLUDE;
1553                 skb = add_grec(skb, pmc, type, 0, 0);
1554                 spin_unlock_bh(&pmc->mca_lock);
1555         }
1556         if (skb)
1557                 mld_sendpack(skb);
1558 }
1559
1560 /*
1561  * remove zero-count source records from a source filter list
1562  */
1563 static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1564 {
1565         struct ip6_sf_list *psf_prev, *psf_next, *psf;
1566
1567         psf_prev = NULL;
1568         for (psf=*ppsf; psf; psf = psf_next) {
1569                 psf_next = psf->sf_next;
1570                 if (psf->sf_crcount == 0) {
1571                         if (psf_prev)
1572                                 psf_prev->sf_next = psf->sf_next;
1573                         else
1574                                 *ppsf = psf->sf_next;
1575                         kfree(psf);
1576                 } else
1577                         psf_prev = psf;
1578         }
1579 }
1580
1581 static void mld_send_cr(struct inet6_dev *idev)
1582 {
1583         struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1584         struct sk_buff *skb = NULL;
1585         int type, dtype;
1586
1587         read_lock_bh(&idev->lock);
1588         write_lock_bh(&idev->mc_lock);
1589
1590         /* deleted MCA's */
1591         pmc_prev = NULL;
1592         for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1593                 pmc_next = pmc->next;
1594                 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1595                         type = MLD2_BLOCK_OLD_SOURCES;
1596                         dtype = MLD2_BLOCK_OLD_SOURCES;
1597                         skb = add_grec(skb, pmc, type, 1, 0);
1598                         skb = add_grec(skb, pmc, dtype, 1, 1);
1599                 }
1600                 if (pmc->mca_crcount) {
1601                         pmc->mca_crcount--;
1602                         if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1603                                 type = MLD2_CHANGE_TO_INCLUDE;
1604                                 skb = add_grec(skb, pmc, type, 1, 0);
1605                         }
1606                         if (pmc->mca_crcount == 0) {
1607                                 mld_clear_zeros(&pmc->mca_tomb);
1608                                 mld_clear_zeros(&pmc->mca_sources);
1609                         }
1610                 }
1611                 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1612                     !pmc->mca_sources) {
1613                         if (pmc_prev)
1614                                 pmc_prev->next = pmc_next;
1615                         else
1616                                 idev->mc_tomb = pmc_next;
1617                         in6_dev_put(pmc->idev);
1618                         kfree(pmc);
1619                 } else
1620                         pmc_prev = pmc;
1621         }
1622         write_unlock_bh(&idev->mc_lock);
1623
1624         /* change recs */
1625         for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1626                 spin_lock_bh(&pmc->mca_lock);
1627                 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1628                         type = MLD2_BLOCK_OLD_SOURCES;
1629                         dtype = MLD2_ALLOW_NEW_SOURCES;
1630                 } else {
1631                         type = MLD2_ALLOW_NEW_SOURCES;
1632                         dtype = MLD2_BLOCK_OLD_SOURCES;
1633                 }
1634                 skb = add_grec(skb, pmc, type, 0, 0);
1635                 skb = add_grec(skb, pmc, dtype, 0, 1);  /* deleted sources */
1636
1637                 /* filter mode changes */
1638                 if (pmc->mca_crcount) {
1639                         pmc->mca_crcount--;
1640                         if (pmc->mca_sfmode == MCAST_EXCLUDE)
1641                                 type = MLD2_CHANGE_TO_EXCLUDE;
1642                         else
1643                                 type = MLD2_CHANGE_TO_INCLUDE;
1644                         skb = add_grec(skb, pmc, type, 0, 0);
1645                 }
1646                 spin_unlock_bh(&pmc->mca_lock);
1647         }
1648         read_unlock_bh(&idev->lock);
1649         if (!skb)
1650                 return;
1651         (void) mld_sendpack(skb);
1652 }
1653
1654 static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1655 {
1656         struct sock *sk = igmp6_socket->sk;
1657         struct inet6_dev *idev;
1658         struct sk_buff *skb;
1659         struct icmp6hdr *hdr;
1660         struct in6_addr *snd_addr;
1661         struct in6_addr *addrp;
1662         struct in6_addr addr_buf;
1663         struct in6_addr all_routers;
1664         int err, len, payload_len, full_len;
1665         u8 ra[8] = { IPPROTO_ICMPV6, 0,
1666                      IPV6_TLV_ROUTERALERT, 2, 0, 0,
1667                      IPV6_TLV_PADN, 0 };
1668
1669         IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
1670         snd_addr = addr;
1671         if (type == ICMPV6_MGM_REDUCTION) {
1672                 snd_addr = &all_routers;
1673                 ipv6_addr_all_routers(&all_routers);
1674         }
1675
1676         len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1677         payload_len = len + sizeof(ra);
1678         full_len = sizeof(struct ipv6hdr) + payload_len;
1679
1680         skb = sock_alloc_send_skb(sk, LL_RESERVED_SPACE(dev) + full_len, 1, &err);
1681
1682         if (skb == NULL) {
1683                 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1684                 return;
1685         }
1686
1687         skb_reserve(skb, LL_RESERVED_SPACE(dev));
1688
1689         if (ipv6_get_lladdr(dev, &addr_buf)) {
1690                 /* <draft-ietf-magma-mld-source-05.txt>:
1691                  * use unspecified address as the source address 
1692                  * when a valid link-local address is not available.
1693                  */
1694                 memset(&addr_buf, 0, sizeof(addr_buf));
1695         }
1696
1697         ip6_nd_hdr(sk, skb, dev, &addr_buf, snd_addr, NEXTHDR_HOP, payload_len);
1698
1699         memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1700
1701         hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
1702         memset(hdr, 0, sizeof(struct icmp6hdr));
1703         hdr->icmp6_type = type;
1704
1705         addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
1706         ipv6_addr_copy(addrp, addr);
1707
1708         hdr->icmp6_cksum = csum_ipv6_magic(&addr_buf, snd_addr, len,
1709                                            IPPROTO_ICMPV6,
1710                                            csum_partial((__u8 *) hdr, len, 0));
1711
1712         idev = in6_dev_get(skb->dev);
1713
1714         err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
1715                 mld_dev_queue_xmit);
1716         if (!err) {
1717                 if (type == ICMPV6_MGM_REDUCTION)
1718                         ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBREDUCTIONS);
1719                 else
1720                         ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBRESPONSES);
1721                 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTMSGS);
1722                 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
1723         } else
1724                 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1725
1726         if (likely(idev != NULL))
1727                 in6_dev_put(idev);
1728         return;
1729 }
1730
1731 static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1732         struct in6_addr *psfsrc)
1733 {
1734         struct ip6_sf_list *psf, *psf_prev;
1735         int rv = 0;
1736
1737         psf_prev = NULL;
1738         for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1739                 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1740                         break;
1741                 psf_prev = psf;
1742         }
1743         if (!psf || psf->sf_count[sfmode] == 0) {
1744                 /* source filter not found, or count wrong =>  bug */
1745                 return -ESRCH;
1746         }
1747         psf->sf_count[sfmode]--;
1748         if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1749                 struct inet6_dev *idev = pmc->idev;
1750
1751                 /* no more filters for this source */
1752                 if (psf_prev)
1753                         psf_prev->sf_next = psf->sf_next;
1754                 else
1755                         pmc->mca_sources = psf->sf_next;
1756                 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1757                     !MLD_V1_SEEN(idev)) {
1758                         psf->sf_crcount = idev->mc_qrv;
1759                         psf->sf_next = pmc->mca_tomb;
1760                         pmc->mca_tomb = psf;
1761                         rv = 1;
1762                 } else
1763                         kfree(psf);
1764         }
1765         return rv;
1766 }
1767
1768 static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
1769                           int sfmode, int sfcount, struct in6_addr *psfsrc,
1770                           int delta)
1771 {
1772         struct ifmcaddr6 *pmc;
1773         int     changerec = 0;
1774         int     i, err;
1775
1776         if (!idev)
1777                 return -ENODEV;
1778         read_lock_bh(&idev->lock);
1779         for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1780                 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1781                         break;
1782         }
1783         if (!pmc) {
1784                 /* MCA not found?? bug */
1785                 read_unlock_bh(&idev->lock);
1786                 return -ESRCH;
1787         }
1788         spin_lock_bh(&pmc->mca_lock);
1789         sf_markstate(pmc);
1790         if (!delta) {
1791                 if (!pmc->mca_sfcount[sfmode]) {
1792                         spin_unlock_bh(&pmc->mca_lock);
1793                         read_unlock_bh(&idev->lock);
1794                         return -EINVAL;
1795                 }
1796                 pmc->mca_sfcount[sfmode]--;
1797         }
1798         err = 0;
1799         for (i=0; i<sfcount; i++) {
1800                 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1801
1802                 changerec |= rv > 0;
1803                 if (!err && rv < 0)
1804                         err = rv;
1805         }
1806         if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1807             pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1808             pmc->mca_sfcount[MCAST_INCLUDE]) {
1809                 struct ip6_sf_list *psf;
1810
1811                 /* filter mode change */
1812                 pmc->mca_sfmode = MCAST_INCLUDE;
1813                 pmc->mca_crcount = idev->mc_qrv;
1814                 idev->mc_ifc_count = pmc->mca_crcount;
1815                 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1816                         psf->sf_crcount = 0;
1817                 mld_ifc_event(pmc->idev);
1818         } else if (sf_setstate(pmc) || changerec)
1819                 mld_ifc_event(pmc->idev);
1820         spin_unlock_bh(&pmc->mca_lock);
1821         read_unlock_bh(&idev->lock);
1822         return err;
1823 }
1824
1825 /*
1826  * Add multicast single-source filter to the interface list
1827  */
1828 static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1829         struct in6_addr *psfsrc, int delta)
1830 {
1831         struct ip6_sf_list *psf, *psf_prev;
1832
1833         psf_prev = NULL;
1834         for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1835                 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1836                         break;
1837                 psf_prev = psf;
1838         }
1839         if (!psf) {
1840                 psf = (struct ip6_sf_list *)kmalloc(sizeof(*psf), GFP_ATOMIC);
1841                 if (!psf)
1842                         return -ENOBUFS;
1843                 memset(psf, 0, sizeof(*psf));
1844                 psf->sf_addr = *psfsrc;
1845                 if (psf_prev) {
1846                         psf_prev->sf_next = psf;
1847                 } else
1848                         pmc->mca_sources = psf;
1849         }
1850         psf->sf_count[sfmode]++;
1851         return 0;
1852 }
1853
1854 static void sf_markstate(struct ifmcaddr6 *pmc)
1855 {
1856         struct ip6_sf_list *psf;
1857         int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1858
1859         for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1860                 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1861                         psf->sf_oldin = mca_xcount ==
1862                                 psf->sf_count[MCAST_EXCLUDE] &&
1863                                 !psf->sf_count[MCAST_INCLUDE];
1864                 } else
1865                         psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1866 }
1867
1868 static int sf_setstate(struct ifmcaddr6 *pmc)
1869 {
1870         struct ip6_sf_list *psf;
1871         int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1872         int qrv = pmc->idev->mc_qrv;
1873         int new_in, rv;
1874
1875         rv = 0;
1876         for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1877                 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1878                         new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1879                                 !psf->sf_count[MCAST_INCLUDE];
1880                 } else
1881                         new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1882                 if (new_in != psf->sf_oldin) {
1883                         psf->sf_crcount = qrv;
1884                         rv++;
1885                 }
1886         }
1887         return rv;
1888 }
1889
1890 /*
1891  * Add multicast source filter list to the interface list
1892  */
1893 static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
1894                           int sfmode, int sfcount, struct in6_addr *psfsrc,
1895                           int delta)
1896 {
1897         struct ifmcaddr6 *pmc;
1898         int     isexclude;
1899         int     i, err;
1900
1901         if (!idev)
1902                 return -ENODEV;
1903         read_lock_bh(&idev->lock);
1904         for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1905                 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1906                         break;
1907         }
1908         if (!pmc) {
1909                 /* MCA not found?? bug */
1910                 read_unlock_bh(&idev->lock);
1911                 return -ESRCH;
1912         }
1913         spin_lock_bh(&pmc->mca_lock);
1914
1915         sf_markstate(pmc);
1916         isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
1917         if (!delta)
1918                 pmc->mca_sfcount[sfmode]++;
1919         err = 0;
1920         for (i=0; i<sfcount; i++) {
1921                 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1922                 if (err)
1923                         break;
1924         }
1925         if (err) {
1926                 int j;
1927
1928                 if (!delta)
1929                         pmc->mca_sfcount[sfmode]--;
1930                 for (j=0; j<i; j++)
1931                         (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1932         } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
1933                 struct inet6_dev *idev = pmc->idev;
1934                 struct ip6_sf_list *psf;
1935
1936                 /* filter mode change */
1937                 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1938                         pmc->mca_sfmode = MCAST_EXCLUDE;
1939                 else if (pmc->mca_sfcount[MCAST_INCLUDE])
1940                         pmc->mca_sfmode = MCAST_INCLUDE;
1941                 /* else no filters; keep old mode for reports */
1942
1943                 pmc->mca_crcount = idev->mc_qrv;
1944                 idev->mc_ifc_count = pmc->mca_crcount;
1945                 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1946                         psf->sf_crcount = 0;
1947                 mld_ifc_event(idev);
1948         } else if (sf_setstate(pmc))
1949                 mld_ifc_event(idev);
1950         spin_unlock_bh(&pmc->mca_lock);
1951         read_unlock_bh(&idev->lock);
1952         return err;
1953 }
1954
1955 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
1956 {
1957         struct ip6_sf_list *psf, *nextpsf;
1958
1959         for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
1960                 nextpsf = psf->sf_next;
1961                 kfree(psf);
1962         }
1963         pmc->mca_tomb = NULL;
1964         for (psf=pmc->mca_sources; psf; psf=nextpsf) {
1965                 nextpsf = psf->sf_next;
1966                 kfree(psf);
1967         }
1968         pmc->mca_sources = NULL;
1969         pmc->mca_sfmode = MCAST_EXCLUDE;
1970         pmc->mca_sfcount[MCAST_EXCLUDE] = 0;
1971         pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
1972 }
1973
1974
1975 static void igmp6_join_group(struct ifmcaddr6 *ma)
1976 {
1977         unsigned long delay;
1978
1979         if (ma->mca_flags & MAF_NOREPORT)
1980                 return;
1981
1982         igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
1983
1984         delay = net_random() % IGMP6_UNSOLICITED_IVAL;
1985
1986         spin_lock_bh(&ma->mca_lock);
1987         if (del_timer(&ma->mca_timer)) {
1988                 atomic_dec(&ma->mca_refcnt);
1989                 delay = ma->mca_timer.expires - jiffies;
1990         }
1991
1992         if (!mod_timer(&ma->mca_timer, jiffies + delay))
1993                 atomic_inc(&ma->mca_refcnt);
1994         ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
1995         spin_unlock_bh(&ma->mca_lock);
1996 }
1997
1998 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
1999                             struct inet6_dev *idev)
2000 {
2001         int err;
2002
2003         if (iml->sflist == 0) {
2004                 /* any-source empty exclude case */
2005                 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2006         }
2007         err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2008                 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2009         sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2010         iml->sflist = NULL;
2011         return err;
2012 }
2013
2014 static void igmp6_leave_group(struct ifmcaddr6 *ma)
2015 {
2016         if (MLD_V1_SEEN(ma->idev)) {
2017                 if (ma->mca_flags & MAF_LAST_REPORTER)
2018                         igmp6_send(&ma->mca_addr, ma->idev->dev,
2019                                 ICMPV6_MGM_REDUCTION);
2020         } else {
2021                 mld_add_delrec(ma->idev, ma);
2022                 mld_ifc_event(ma->idev);
2023         }
2024 }
2025
2026 static void mld_gq_timer_expire(unsigned long data)
2027 {
2028         struct inet6_dev *idev = (struct inet6_dev *)data;
2029
2030         idev->mc_gq_running = 0;
2031         mld_send_report(idev, NULL);
2032         __in6_dev_put(idev);
2033 }
2034
2035 static void mld_ifc_timer_expire(unsigned long data)
2036 {
2037         struct inet6_dev *idev = (struct inet6_dev *)data;
2038
2039         mld_send_cr(idev);
2040         if (idev->mc_ifc_count) {
2041                 idev->mc_ifc_count--;
2042                 if (idev->mc_ifc_count)
2043                         mld_ifc_start_timer(idev, idev->mc_maxdelay);
2044         }
2045         __in6_dev_put(idev);
2046 }
2047
2048 static void mld_ifc_event(struct inet6_dev *idev)
2049 {
2050         if (MLD_V1_SEEN(idev))
2051                 return;
2052         idev->mc_ifc_count = idev->mc_qrv;
2053         mld_ifc_start_timer(idev, 1);
2054 }
2055
2056
2057 static void igmp6_timer_handler(unsigned long data)
2058 {
2059         struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2060
2061         if (MLD_V1_SEEN(ma->idev))
2062                 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2063         else
2064                 mld_send_report(ma->idev, ma);
2065
2066         spin_lock(&ma->mca_lock);
2067         ma->mca_flags |=  MAF_LAST_REPORTER;
2068         ma->mca_flags &= ~MAF_TIMER_RUNNING;
2069         spin_unlock(&ma->mca_lock);
2070         ma_put(ma);
2071 }
2072
2073 /* Device going down */
2074
2075 void ipv6_mc_down(struct inet6_dev *idev)
2076 {
2077         struct ifmcaddr6 *i;
2078
2079         /* Withdraw multicast list */
2080
2081         read_lock_bh(&idev->lock);
2082         idev->mc_ifc_count = 0;
2083         if (del_timer(&idev->mc_ifc_timer))
2084                 __in6_dev_put(idev);
2085         idev->mc_gq_running = 0;
2086         if (del_timer(&idev->mc_gq_timer))
2087                 __in6_dev_put(idev);
2088
2089         for (i = idev->mc_list; i; i=i->next)
2090                 igmp6_group_dropped(i);
2091         read_unlock_bh(&idev->lock);
2092
2093         mld_clear_delrec(idev);
2094 }
2095
2096
2097 /* Device going up */
2098
2099 void ipv6_mc_up(struct inet6_dev *idev)
2100 {
2101         struct ifmcaddr6 *i;
2102
2103         /* Install multicast list, except for all-nodes (already installed) */
2104
2105         read_lock_bh(&idev->lock);
2106         for (i = idev->mc_list; i; i=i->next)
2107                 igmp6_group_added(i);
2108         read_unlock_bh(&idev->lock);
2109 }
2110
2111 /* IPv6 device initialization. */
2112
2113 void ipv6_mc_init_dev(struct inet6_dev *idev)
2114 {
2115         struct in6_addr maddr;
2116
2117         write_lock_bh(&idev->lock);
2118         rwlock_init(&idev->mc_lock);
2119         idev->mc_gq_running = 0;
2120         init_timer(&idev->mc_gq_timer);
2121         idev->mc_gq_timer.data = (unsigned long) idev;
2122         idev->mc_gq_timer.function = &mld_gq_timer_expire;
2123         idev->mc_tomb = NULL;
2124         idev->mc_ifc_count = 0;
2125         init_timer(&idev->mc_ifc_timer);
2126         idev->mc_ifc_timer.data = (unsigned long) idev;
2127         idev->mc_ifc_timer.function = &mld_ifc_timer_expire;
2128         idev->mc_qrv = MLD_QRV_DEFAULT;
2129         idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2130         idev->mc_v1_seen = 0;
2131         write_unlock_bh(&idev->lock);
2132
2133         /* Add all-nodes address. */
2134         ipv6_addr_all_nodes(&maddr);
2135         ipv6_dev_mc_inc(idev->dev, &maddr);
2136 }
2137
2138 /*
2139  *      Device is about to be destroyed: clean up.
2140  */
2141
2142 void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2143 {
2144         struct ifmcaddr6 *i;
2145         struct in6_addr maddr;
2146
2147         /* Deactivate timers */
2148         ipv6_mc_down(idev);
2149
2150         /* Delete all-nodes address. */
2151         ipv6_addr_all_nodes(&maddr);
2152
2153         /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2154          * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2155          * fail.
2156          */
2157         __ipv6_dev_mc_dec(idev, &maddr);
2158
2159         if (idev->cnf.forwarding) {
2160                 ipv6_addr_all_routers(&maddr);
2161                 __ipv6_dev_mc_dec(idev, &maddr);
2162         }
2163
2164         write_lock_bh(&idev->lock);
2165         while ((i = idev->mc_list) != NULL) {
2166                 idev->mc_list = i->next;
2167                 write_unlock_bh(&idev->lock);
2168
2169                 igmp6_group_dropped(i);
2170                 ma_put(i);
2171
2172                 write_lock_bh(&idev->lock);
2173         }
2174         write_unlock_bh(&idev->lock);
2175 }
2176
2177 #ifdef CONFIG_PROC_FS
2178 struct igmp6_mc_iter_state {
2179         struct net_device *dev;
2180         struct inet6_dev *idev;
2181 };
2182
2183 #define igmp6_mc_seq_private(seq)       ((struct igmp6_mc_iter_state *)(seq)->private)
2184
2185 static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2186 {
2187         struct ifmcaddr6 *im = NULL;
2188         struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2189
2190         for (state->dev = dev_base, state->idev = NULL;
2191              state->dev; 
2192              state->dev = state->dev->next) {
2193                 struct inet6_dev *idev;
2194                 idev = in6_dev_get(state->dev);
2195                 if (!idev)
2196                         continue;
2197                 read_lock_bh(&idev->lock);
2198                 im = idev->mc_list;
2199                 if (im) {
2200                         state->idev = idev;
2201                         break;
2202                 }
2203                 read_unlock_bh(&idev->lock);
2204                 in6_dev_put(idev);
2205         }
2206         return im;
2207 }
2208
2209 static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2210 {
2211         struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2212
2213         im = im->next;
2214         while (!im) {
2215                 if (likely(state->idev != NULL)) {
2216                         read_unlock_bh(&state->idev->lock);
2217                         in6_dev_put(state->idev);
2218                 }
2219                 state->dev = state->dev->next;
2220                 if (!state->dev) {
2221                         state->idev = NULL;
2222                         break;
2223                 }
2224                 state->idev = in6_dev_get(state->dev);
2225                 if (!state->idev)
2226                         continue;
2227                 read_lock_bh(&state->idev->lock);
2228                 im = state->idev->mc_list;
2229         }
2230         return im;
2231 }
2232
2233 static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2234 {
2235         struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2236         if (im)
2237                 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2238                         --pos;
2239         return pos ? NULL : im;
2240 }
2241
2242 static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2243 {
2244         read_lock(&dev_base_lock);
2245         return igmp6_mc_get_idx(seq, *pos);
2246 }
2247
2248 static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2249 {
2250         struct ifmcaddr6 *im;
2251         im = igmp6_mc_get_next(seq, v);
2252         ++*pos;
2253         return im;
2254 }
2255
2256 static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2257 {
2258         struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2259         if (likely(state->idev != NULL)) {
2260                 read_unlock_bh(&state->idev->lock);
2261                 in6_dev_put(state->idev);
2262                 state->idev = NULL;
2263         }
2264         state->dev = NULL;
2265         read_unlock(&dev_base_lock);
2266 }
2267
2268 static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2269 {
2270         struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2271         struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2272
2273         seq_printf(seq,
2274                    "%-4d %-15s %04x%04x%04x%04x%04x%04x%04x%04x %5d %08X %ld\n", 
2275                    state->dev->ifindex, state->dev->name,
2276                    NIP6(im->mca_addr),
2277                    im->mca_users, im->mca_flags,
2278                    (im->mca_flags&MAF_TIMER_RUNNING) ?
2279                    jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2280         return 0;
2281 }
2282
2283 static struct seq_operations igmp6_mc_seq_ops = {
2284         .start  =       igmp6_mc_seq_start,
2285         .next   =       igmp6_mc_seq_next,
2286         .stop   =       igmp6_mc_seq_stop,
2287         .show   =       igmp6_mc_seq_show,
2288 };
2289
2290 static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2291 {
2292         struct seq_file *seq;
2293         int rc = -ENOMEM;
2294         struct igmp6_mc_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2295
2296         if (!s)
2297                 goto out;
2298
2299         rc = seq_open(file, &igmp6_mc_seq_ops);
2300         if (rc)
2301                 goto out_kfree;
2302
2303         seq = file->private_data;
2304         seq->private = s;
2305         memset(s, 0, sizeof(*s));
2306 out:
2307         return rc;
2308 out_kfree:
2309         kfree(s);
2310         goto out;
2311 }
2312
2313 static struct file_operations igmp6_mc_seq_fops = {
2314         .owner          =       THIS_MODULE,
2315         .open           =       igmp6_mc_seq_open,
2316         .read           =       seq_read,
2317         .llseek         =       seq_lseek,
2318         .release        =       seq_release_private,
2319 };
2320
2321 struct igmp6_mcf_iter_state {
2322         struct net_device *dev;
2323         struct inet6_dev *idev;
2324         struct ifmcaddr6 *im;
2325 };
2326
2327 #define igmp6_mcf_seq_private(seq)      ((struct igmp6_mcf_iter_state *)(seq)->private)
2328
2329 static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2330 {
2331         struct ip6_sf_list *psf = NULL;
2332         struct ifmcaddr6 *im = NULL;
2333         struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2334
2335         for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
2336              state->dev; 
2337              state->dev = state->dev->next) {
2338                 struct inet6_dev *idev;
2339                 idev = in6_dev_get(state->dev);
2340                 if (unlikely(idev == NULL))
2341                         continue;
2342                 read_lock_bh(&idev->lock);
2343                 im = idev->mc_list;
2344                 if (likely(im != NULL)) {
2345                         spin_lock_bh(&im->mca_lock);
2346                         psf = im->mca_sources;
2347                         if (likely(psf != NULL)) {
2348                                 state->im = im;
2349                                 state->idev = idev;
2350                                 break;
2351                         }
2352                         spin_unlock_bh(&im->mca_lock);
2353                 }
2354                 read_unlock_bh(&idev->lock);
2355                 in6_dev_put(idev);
2356         }
2357         return psf;
2358 }
2359
2360 static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2361 {
2362         struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2363
2364         psf = psf->sf_next;
2365         while (!psf) {
2366                 spin_unlock_bh(&state->im->mca_lock);
2367                 state->im = state->im->next;
2368                 while (!state->im) {
2369                         if (likely(state->idev != NULL)) {
2370                                 read_unlock_bh(&state->idev->lock);
2371                                 in6_dev_put(state->idev);
2372                         }
2373                         state->dev = state->dev->next;
2374                         if (!state->dev) {
2375                                 state->idev = NULL;
2376                                 goto out;
2377                         }
2378                         state->idev = in6_dev_get(state->dev);
2379                         if (!state->idev)
2380                                 continue;
2381                         read_lock_bh(&state->idev->lock);
2382                         state->im = state->idev->mc_list;
2383                 }
2384                 if (!state->im)
2385                         break;
2386                 spin_lock_bh(&state->im->mca_lock);
2387                 psf = state->im->mca_sources;
2388         }
2389 out:
2390         return psf;
2391 }
2392
2393 static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2394 {
2395         struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2396         if (psf)
2397                 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2398                         --pos;
2399         return pos ? NULL : psf;
2400 }
2401
2402 static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2403 {
2404         read_lock(&dev_base_lock);
2405         return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2406 }
2407
2408 static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2409 {
2410         struct ip6_sf_list *psf;
2411         if (v == SEQ_START_TOKEN)
2412                 psf = igmp6_mcf_get_first(seq);
2413         else
2414                 psf = igmp6_mcf_get_next(seq, v);
2415         ++*pos;
2416         return psf;
2417 }
2418
2419 static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2420 {
2421         struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2422         if (likely(state->im != NULL)) {
2423                 spin_unlock_bh(&state->im->mca_lock);
2424                 state->im = NULL;
2425         }
2426         if (likely(state->idev != NULL)) {
2427                 read_unlock_bh(&state->idev->lock);
2428                 in6_dev_put(state->idev);
2429                 state->idev = NULL;
2430         }
2431         state->dev = NULL;
2432         read_unlock(&dev_base_lock);
2433 }
2434
2435 static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2436 {
2437         struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2438         struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2439
2440         if (v == SEQ_START_TOKEN) {
2441                 seq_printf(seq, 
2442                            "%3s %6s "
2443                            "%32s %32s %6s %6s\n", "Idx",
2444                            "Device", "Multicast Address",
2445                            "Source Address", "INC", "EXC");
2446         } else {
2447                 seq_printf(seq,
2448                            "%3d %6.6s "
2449                            "%04x%04x%04x%04x%04x%04x%04x%04x "
2450                            "%04x%04x%04x%04x%04x%04x%04x%04x "
2451                            "%6lu %6lu\n",
2452                            state->dev->ifindex, state->dev->name,
2453                            NIP6(state->im->mca_addr),
2454                            NIP6(psf->sf_addr),
2455                            psf->sf_count[MCAST_INCLUDE],
2456                            psf->sf_count[MCAST_EXCLUDE]);
2457         }
2458         return 0;
2459 }
2460
2461 static struct seq_operations igmp6_mcf_seq_ops = {
2462         .start  =       igmp6_mcf_seq_start,
2463         .next   =       igmp6_mcf_seq_next,
2464         .stop   =       igmp6_mcf_seq_stop,
2465         .show   =       igmp6_mcf_seq_show,
2466 };
2467
2468 static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2469 {
2470         struct seq_file *seq;
2471         int rc = -ENOMEM;
2472         struct igmp6_mcf_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2473         
2474         if (!s)
2475                 goto out;
2476
2477         rc = seq_open(file, &igmp6_mcf_seq_ops);
2478         if (rc)
2479                 goto out_kfree;
2480
2481         seq = file->private_data;
2482         seq->private = s;
2483         memset(s, 0, sizeof(*s));
2484 out:
2485         return rc;
2486 out_kfree:
2487         kfree(s);
2488         goto out;
2489 }
2490
2491 static struct file_operations igmp6_mcf_seq_fops = {
2492         .owner          =       THIS_MODULE,
2493         .open           =       igmp6_mcf_seq_open,
2494         .read           =       seq_read,
2495         .llseek         =       seq_lseek,
2496         .release        =       seq_release_private,
2497 };
2498 #endif
2499
2500 int __init igmp6_init(struct net_proto_family *ops)
2501 {
2502         struct ipv6_pinfo *np;
2503         struct sock *sk;
2504         int err;
2505
2506         err = sock_create_kern(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6, &igmp6_socket);
2507         if (err < 0) {
2508                 printk(KERN_ERR
2509                        "Failed to initialize the IGMP6 control socket (err %d).\n",
2510                        err);
2511                 igmp6_socket = NULL; /* For safety. */
2512                 return err;
2513         }
2514
2515         sk = igmp6_socket->sk;
2516         sk->sk_allocation = GFP_ATOMIC;
2517         sk->sk_prot->unhash(sk);
2518
2519         np = inet6_sk(sk);
2520         np->hop_limit = 1;
2521
2522 #ifdef CONFIG_PROC_FS
2523         proc_net_fops_create("igmp6", S_IRUGO, &igmp6_mc_seq_fops);
2524         proc_net_fops_create("mcfilter6", S_IRUGO, &igmp6_mcf_seq_fops);
2525 #endif
2526
2527         return 0;
2528 }
2529
2530 void igmp6_cleanup(void)
2531 {
2532         sock_release(igmp6_socket);
2533         igmp6_socket = NULL; /* for safety */
2534
2535 #ifdef CONFIG_PROC_FS
2536         proc_net_remove("mcfilter6");
2537         proc_net_remove("igmp6");
2538 #endif
2539 }