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