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

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

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

~ [ 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