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