~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv6/mcast.c

Version: ~ [ linux-5.8 ] ~ [ linux-5.7.12 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.55 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.136 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.191 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.232 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.232 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp