blob: 87ae8c30ab41c142c19a0e4bd5d43eea3c2c4914 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
2 * Bridge multicast support.
3 *
4 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 */
12
13#include <linux/err.h>
14#include <linux/if_ether.h>
15#include <linux/igmp.h>
16#include <linux/jhash.h>
17#include <linux/kernel.h>
18#include <linux/log2.h>
19#include <linux/netdevice.h>
20#include <linux/netfilter_bridge.h>
21#include <linux/random.h>
22#include <linux/rculist.h>
23#include <linux/skbuff.h>
24#include <linux/slab.h>
25#include <linux/timer.h>
26#include <net/ip.h>
27#if IS_ENABLED(CONFIG_IPV6)
28#include <net/ipv6.h>
29#include <net/mld.h>
30#include <net/addrconf.h>
31#include <net/ip6_checksum.h>
32#endif
33
34#include "br_private.h"
35
36#define mlock_dereference(X, br) \
37 rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock))
38
39static void br_multicast_add_router(struct net_bridge *br,
40 struct net_bridge_port *port);
41
42#if IS_ENABLED(CONFIG_IPV6)
43static inline int ipv6_is_transient_multicast(const struct in6_addr *addr)
44{
45 if (ipv6_addr_is_multicast(addr) && IPV6_ADDR_MC_FLAG_TRANSIENT(addr))
46 return 1;
47 return 0;
48}
49#endif
50
51static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
52{
53 if (a->proto != b->proto)
54 return 0;
55 switch (a->proto) {
56 case htons(ETH_P_IP):
57 return a->u.ip4 == b->u.ip4;
58#if IS_ENABLED(CONFIG_IPV6)
59 case htons(ETH_P_IPV6):
60 return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
61#endif
62 }
63 return 0;
64}
65
66static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip)
67{
68 return jhash_1word(mdb->secret, (__force u32)ip) & (mdb->max - 1);
69}
70
71#if IS_ENABLED(CONFIG_IPV6)
72static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb,
73 const struct in6_addr *ip)
74{
75 return jhash2((__force u32 *)ip->s6_addr32, 4, mdb->secret) & (mdb->max - 1);
76}
77#endif
78
79static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb,
80 struct br_ip *ip)
81{
82 switch (ip->proto) {
83 case htons(ETH_P_IP):
84 return __br_ip4_hash(mdb, ip->u.ip4);
85#if IS_ENABLED(CONFIG_IPV6)
86 case htons(ETH_P_IPV6):
87 return __br_ip6_hash(mdb, &ip->u.ip6);
88#endif
89 }
90 return 0;
91}
92
93static struct net_bridge_mdb_entry *__br_mdb_ip_get(
94 struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash)
95{
96 struct net_bridge_mdb_entry *mp;
97 struct hlist_node *p;
98
99 hlist_for_each_entry_rcu(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) {
100 if (br_ip_equal(&mp->addr, dst))
101 return mp;
102 }
103
104 return NULL;
105}
106
107static struct net_bridge_mdb_entry *br_mdb_ip_get(
108 struct net_bridge_mdb_htable *mdb, struct br_ip *dst)
109{
110 if (!mdb)
111 return NULL;
112
113 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
114}
115
116static struct net_bridge_mdb_entry *br_mdb_ip4_get(
117 struct net_bridge_mdb_htable *mdb, __be32 dst)
118{
119 struct br_ip br_dst;
120
121 br_dst.u.ip4 = dst;
122 br_dst.proto = htons(ETH_P_IP);
123
124 return br_mdb_ip_get(mdb, &br_dst);
125}
126
127#if IS_ENABLED(CONFIG_IPV6)
128static struct net_bridge_mdb_entry *br_mdb_ip6_get(
129 struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst)
130{
131 struct br_ip br_dst;
132
133 br_dst.u.ip6 = *dst;
134 br_dst.proto = htons(ETH_P_IPV6);
135
136 return br_mdb_ip_get(mdb, &br_dst);
137}
138#endif
139
140struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
141 struct sk_buff *skb)
142{
143 struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb);
144 struct br_ip ip;
145
146 if (br->multicast_disabled)
147 return NULL;
148
149 if (BR_INPUT_SKB_CB(skb)->igmp)
150 return NULL;
151
152 ip.proto = skb->protocol;
153
154 switch (skb->protocol) {
155 case htons(ETH_P_IP):
156 ip.u.ip4 = ip_hdr(skb)->daddr;
157 break;
158#if IS_ENABLED(CONFIG_IPV6)
159 case htons(ETH_P_IPV6):
160 ip.u.ip6 = ipv6_hdr(skb)->daddr;
161 break;
162#endif
163 default:
164 return NULL;
165 }
166
167 return br_mdb_ip_get(mdb, &ip);
168}
169
170static void br_mdb_free(struct rcu_head *head)
171{
172 struct net_bridge_mdb_htable *mdb =
173 container_of(head, struct net_bridge_mdb_htable, rcu);
174 struct net_bridge_mdb_htable *old = mdb->old;
175
176 mdb->old = NULL;
177 kfree(old->mhash);
178 kfree(old);
179}
180
181static int br_mdb_copy(struct net_bridge_mdb_htable *new,
182 struct net_bridge_mdb_htable *old,
183 int elasticity)
184{
185 struct net_bridge_mdb_entry *mp;
186 struct hlist_node *p;
187 int maxlen;
188 int len;
189 int i;
190
191 for (i = 0; i < old->max; i++)
192 hlist_for_each_entry(mp, p, &old->mhash[i], hlist[old->ver])
193 hlist_add_head(&mp->hlist[new->ver],
194 &new->mhash[br_ip_hash(new, &mp->addr)]);
195
196 if (!elasticity)
197 return 0;
198
199 maxlen = 0;
200 for (i = 0; i < new->max; i++) {
201 len = 0;
202 hlist_for_each_entry(mp, p, &new->mhash[i], hlist[new->ver])
203 len++;
204 if (len > maxlen)
205 maxlen = len;
206 }
207
208 return maxlen > elasticity ? -EINVAL : 0;
209}
210
211static void br_multicast_free_pg(struct rcu_head *head)
212{
213 struct net_bridge_port_group *p =
214 container_of(head, struct net_bridge_port_group, rcu);
215
216 kfree(p);
217}
218
219static void br_multicast_free_group(struct rcu_head *head)
220{
221 struct net_bridge_mdb_entry *mp =
222 container_of(head, struct net_bridge_mdb_entry, rcu);
223
224 kfree(mp);
225}
226
227static void br_multicast_group_expired(unsigned long data)
228{
229 struct net_bridge_mdb_entry *mp = (void *)data;
230 struct net_bridge *br = mp->br;
231 struct net_bridge_mdb_htable *mdb;
232
233 spin_lock(&br->multicast_lock);
234 if (!netif_running(br->dev) || timer_pending(&mp->timer))
235 goto out;
236
237 mp->mglist = false;
238
239 if (mp->ports)
240 goto out;
241
242 mdb = mlock_dereference(br->mdb, br);
243
244 hlist_del_rcu(&mp->hlist[mdb->ver]);
245 mdb->size--;
246
247 call_rcu_bh(&mp->rcu, br_multicast_free_group);
248
249out:
250 spin_unlock(&br->multicast_lock);
251}
252
253static void br_multicast_del_pg(struct net_bridge *br,
254 struct net_bridge_port_group *pg)
255{
256 struct net_bridge_mdb_htable *mdb;
257 struct net_bridge_mdb_entry *mp;
258 struct net_bridge_port_group *p;
259 struct net_bridge_port_group __rcu **pp;
260
261 mdb = mlock_dereference(br->mdb, br);
262
263 mp = br_mdb_ip_get(mdb, &pg->addr);
264 if (WARN_ON(!mp))
265 return;
266
267 for (pp = &mp->ports;
268 (p = mlock_dereference(*pp, br)) != NULL;
269 pp = &p->next) {
270 if (p != pg)
271 continue;
272
273 rcu_assign_pointer(*pp, p->next);
274 hlist_del_init(&p->mglist);
275 del_timer(&p->timer);
276 call_rcu_bh(&p->rcu, br_multicast_free_pg);
277
278 if (!mp->ports && !mp->mglist &&
279 netif_running(br->dev))
280 mod_timer(&mp->timer, jiffies);
281
282 return;
283 }
284
285 WARN_ON(1);
286}
287
288static void br_multicast_port_group_expired(unsigned long data)
289{
290 struct net_bridge_port_group *pg = (void *)data;
291 struct net_bridge *br = pg->port->br;
292
293 spin_lock(&br->multicast_lock);
294 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
295 hlist_unhashed(&pg->mglist))
296 goto out;
297
298 br_multicast_del_pg(br, pg);
299
300out:
301 spin_unlock(&br->multicast_lock);
302}
303
304static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max,
305 int elasticity)
306{
307 struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1);
308 struct net_bridge_mdb_htable *mdb;
309 int err;
310
311 mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
312 if (!mdb)
313 return -ENOMEM;
314
315 mdb->max = max;
316 mdb->old = old;
317
318 mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
319 if (!mdb->mhash) {
320 kfree(mdb);
321 return -ENOMEM;
322 }
323
324 mdb->size = old ? old->size : 0;
325 mdb->ver = old ? old->ver ^ 1 : 0;
326
327 if (!old || elasticity)
328 get_random_bytes(&mdb->secret, sizeof(mdb->secret));
329 else
330 mdb->secret = old->secret;
331
332 if (!old)
333 goto out;
334
335 err = br_mdb_copy(mdb, old, elasticity);
336 if (err) {
337 kfree(mdb->mhash);
338 kfree(mdb);
339 return err;
340 }
341
342 call_rcu_bh(&mdb->rcu, br_mdb_free);
343
344out:
345 rcu_assign_pointer(*mdbp, mdb);
346
347 return 0;
348}
349
350static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
351 __be32 group)
352{
353 struct sk_buff *skb;
354 struct igmphdr *ih;
355 struct ethhdr *eth;
356 struct iphdr *iph;
357
358 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
359 sizeof(*ih) + 4);
360 if (!skb)
361 goto out;
362
363 skb->protocol = htons(ETH_P_IP);
364
365 skb_reset_mac_header(skb);
366 eth = eth_hdr(skb);
367
368 memcpy(eth->h_source, br->dev->dev_addr, 6);
369 eth->h_dest[0] = 1;
370 eth->h_dest[1] = 0;
371 eth->h_dest[2] = 0x5e;
372 eth->h_dest[3] = 0;
373 eth->h_dest[4] = 0;
374 eth->h_dest[5] = 1;
375 eth->h_proto = htons(ETH_P_IP);
376 skb_put(skb, sizeof(*eth));
377
378 skb_set_network_header(skb, skb->len);
379 iph = ip_hdr(skb);
380
381 iph->version = 4;
382 iph->ihl = 6;
383 iph->tos = 0xc0;
384 iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4);
385 iph->id = 0;
386 iph->frag_off = htons(IP_DF);
387 iph->ttl = 1;
388 iph->protocol = IPPROTO_IGMP;
389 iph->saddr = 0;
390 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
391 ((u8 *)&iph[1])[0] = IPOPT_RA;
392 ((u8 *)&iph[1])[1] = 4;
393 ((u8 *)&iph[1])[2] = 0;
394 ((u8 *)&iph[1])[3] = 0;
395 ip_send_check(iph);
396 skb_put(skb, 24);
397
398 skb_set_transport_header(skb, skb->len);
399 ih = igmp_hdr(skb);
400 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
401 ih->code = (group ? br->multicast_last_member_interval :
402 br->multicast_query_response_interval) /
403 (HZ / IGMP_TIMER_SCALE);
404 ih->group = group;
405 ih->csum = 0;
406 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
407 skb_put(skb, sizeof(*ih));
408
409 __skb_pull(skb, sizeof(*eth));
410
411out:
412 return skb;
413}
414
415#if IS_ENABLED(CONFIG_IPV6)
416static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
417 const struct in6_addr *group)
418{
419 struct sk_buff *skb;
420 struct ipv6hdr *ip6h;
421 struct mld_msg *mldq;
422 struct ethhdr *eth;
423 u8 *hopopt;
424 unsigned long interval;
425
426 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
427 8 + sizeof(*mldq));
428 if (!skb)
429 goto out;
430
431 skb->protocol = htons(ETH_P_IPV6);
432
433 /* Ethernet header */
434 skb_reset_mac_header(skb);
435 eth = eth_hdr(skb);
436
437 memcpy(eth->h_source, br->dev->dev_addr, 6);
438 eth->h_proto = htons(ETH_P_IPV6);
439 skb_put(skb, sizeof(*eth));
440
441 /* IPv6 header + HbH option */
442 skb_set_network_header(skb, skb->len);
443 ip6h = ipv6_hdr(skb);
444
445 *(__force __be32 *)ip6h = htonl(0x60000000);
446 ip6h->payload_len = htons(8 + sizeof(*mldq));
447 ip6h->nexthdr = IPPROTO_HOPOPTS;
448 ip6h->hop_limit = 1;
449 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
450 if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
451 &ip6h->saddr)) {
452 kfree_skb(skb);
453 return NULL;
454 }
455 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
456
457 hopopt = (u8 *)(ip6h + 1);
458 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
459 hopopt[1] = 0; /* length of HbH */
460 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
461 hopopt[3] = 2; /* Length of RA Option */
462 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
463 hopopt[5] = 0;
464 hopopt[6] = IPV6_TLV_PAD0; /* Pad0 */
465 hopopt[7] = IPV6_TLV_PAD0; /* Pad0 */
466
467 skb_put(skb, sizeof(*ip6h) + 8);
468
469 /* ICMPv6 */
470 skb_set_transport_header(skb, skb->len);
471 mldq = (struct mld_msg *) icmp6_hdr(skb);
472
473 interval = ipv6_addr_any(group) ?
474 br->multicast_query_response_interval :
475 br->multicast_last_member_interval;
476
477 mldq->mld_type = ICMPV6_MGM_QUERY;
478 mldq->mld_code = 0;
479 mldq->mld_cksum = 0;
480 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
481 mldq->mld_reserved = 0;
482 mldq->mld_mca = *group;
483
484 /* checksum */
485 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
486 sizeof(*mldq), IPPROTO_ICMPV6,
487 csum_partial(mldq,
488 sizeof(*mldq), 0));
489 skb_put(skb, sizeof(*mldq));
490
491 __skb_pull(skb, sizeof(*eth));
492
493out:
494 return skb;
495}
496#endif
497
498static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
499 struct br_ip *addr)
500{
501 switch (addr->proto) {
502 case htons(ETH_P_IP):
503 return br_ip4_multicast_alloc_query(br, addr->u.ip4);
504#if IS_ENABLED(CONFIG_IPV6)
505 case htons(ETH_P_IPV6):
506 return br_ip6_multicast_alloc_query(br, &addr->u.ip6);
507#endif
508 }
509 return NULL;
510}
511
512static struct net_bridge_mdb_entry *br_multicast_get_group(
513 struct net_bridge *br, struct net_bridge_port *port,
514 struct br_ip *group, int hash)
515{
516 struct net_bridge_mdb_htable *mdb;
517 struct net_bridge_mdb_entry *mp;
518 struct hlist_node *p;
519 unsigned count = 0;
520 unsigned max;
521 int elasticity;
522 int err;
523
524 mdb = rcu_dereference_protected(br->mdb, 1);
525 hlist_for_each_entry(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) {
526 count++;
527 if (unlikely(br_ip_equal(group, &mp->addr)))
528 return mp;
529 }
530
531 elasticity = 0;
532 max = mdb->max;
533
534 if (unlikely(count > br->hash_elasticity && count)) {
535 if (net_ratelimit())
536 br_info(br, "Multicast hash table "
537 "chain limit reached: %s\n",
538 port ? port->dev->name : br->dev->name);
539
540 elasticity = br->hash_elasticity;
541 }
542
543 if (mdb->size >= max) {
544 max *= 2;
545 if (unlikely(max >= br->hash_max)) {
546 br_warn(br, "Multicast hash table maximum "
547 "reached, disabling snooping: %s, %d\n",
548 port ? port->dev->name : br->dev->name, max);
549 err = -E2BIG;
550disable:
551 br->multicast_disabled = 1;
552 goto err;
553 }
554 }
555
556 if (max > mdb->max || elasticity) {
557 if (mdb->old) {
558 if (net_ratelimit())
559 br_info(br, "Multicast hash table "
560 "on fire: %s\n",
561 port ? port->dev->name : br->dev->name);
562 err = -EEXIST;
563 goto err;
564 }
565
566 err = br_mdb_rehash(&br->mdb, max, elasticity);
567 if (err) {
568 br_warn(br, "Cannot rehash multicast "
569 "hash table, disabling snooping: %s, %d, %d\n",
570 port ? port->dev->name : br->dev->name,
571 mdb->size, err);
572 goto disable;
573 }
574
575 err = -EAGAIN;
576 goto err;
577 }
578
579 return NULL;
580
581err:
582 mp = ERR_PTR(err);
583 return mp;
584}
585
586static struct net_bridge_mdb_entry *br_multicast_new_group(
587 struct net_bridge *br, struct net_bridge_port *port,
588 struct br_ip *group)
589{
590 struct net_bridge_mdb_htable *mdb;
591 struct net_bridge_mdb_entry *mp;
592 int hash;
593 int err;
594
595 mdb = rcu_dereference_protected(br->mdb, 1);
596 if (!mdb) {
597 err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0);
598 if (err)
599 return ERR_PTR(err);
600 goto rehash;
601 }
602
603 hash = br_ip_hash(mdb, group);
604 mp = br_multicast_get_group(br, port, group, hash);
605 switch (PTR_ERR(mp)) {
606 case 0:
607 break;
608
609 case -EAGAIN:
610rehash:
611 mdb = rcu_dereference_protected(br->mdb, 1);
612 hash = br_ip_hash(mdb, group);
613 break;
614
615 default:
616 goto out;
617 }
618
619 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
620 if (unlikely(!mp))
621 return ERR_PTR(-ENOMEM);
622
623 mp->br = br;
624 mp->addr = *group;
625 setup_timer(&mp->timer, br_multicast_group_expired,
626 (unsigned long)mp);
627
628 hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
629 mdb->size++;
630
631out:
632 return mp;
633}
634
635static int br_multicast_add_group(struct net_bridge *br,
636 struct net_bridge_port *port,
637 struct br_ip *group)
638{
639 struct net_bridge_mdb_entry *mp;
640 struct net_bridge_port_group *p;
641 struct net_bridge_port_group __rcu **pp;
642 unsigned long now = jiffies;
643 int err;
644
645 spin_lock(&br->multicast_lock);
646 if (!netif_running(br->dev) ||
647 (port && port->state == BR_STATE_DISABLED))
648 goto out;
649
650 mp = br_multicast_new_group(br, port, group);
651 err = PTR_ERR(mp);
652 if (IS_ERR(mp))
653 goto err;
654
655 if (!port) {
656 mp->mglist = true;
657 mod_timer(&mp->timer, now + br->multicast_membership_interval);
658 goto out;
659 }
660
661 for (pp = &mp->ports;
662 (p = mlock_dereference(*pp, br)) != NULL;
663 pp = &p->next) {
664 if (p->port == port)
665 goto found;
666 if ((unsigned long)p->port < (unsigned long)port)
667 break;
668 }
669
670 p = kzalloc(sizeof(*p), GFP_ATOMIC);
671 err = -ENOMEM;
672 if (unlikely(!p))
673 goto err;
674
675 p->addr = *group;
676 p->port = port;
677 p->next = *pp;
678 hlist_add_head(&p->mglist, &port->mglist);
679 setup_timer(&p->timer, br_multicast_port_group_expired,
680 (unsigned long)p);
681
682 rcu_assign_pointer(*pp, p);
683
684found:
685 mod_timer(&p->timer, now + br->multicast_membership_interval);
686out:
687 err = 0;
688
689err:
690 spin_unlock(&br->multicast_lock);
691 return err;
692}
693
694static int br_ip4_multicast_add_group(struct net_bridge *br,
695 struct net_bridge_port *port,
696 __be32 group)
697{
698 struct br_ip br_group;
699
700 if (ipv4_is_local_multicast(group))
701 return 0;
702
703 br_group.u.ip4 = group;
704 br_group.proto = htons(ETH_P_IP);
705
706 return br_multicast_add_group(br, port, &br_group);
707}
708
709#if IS_ENABLED(CONFIG_IPV6)
710static int br_ip6_multicast_add_group(struct net_bridge *br,
711 struct net_bridge_port *port,
712 const struct in6_addr *group)
713{
714 struct br_ip br_group;
715
716 if (!ipv6_is_transient_multicast(group))
717 return 0;
718
719 br_group.u.ip6 = *group;
720 br_group.proto = htons(ETH_P_IPV6);
721
722 return br_multicast_add_group(br, port, &br_group);
723}
724#endif
725
726static void br_multicast_router_expired(unsigned long data)
727{
728 struct net_bridge_port *port = (void *)data;
729 struct net_bridge *br = port->br;
730
731 spin_lock(&br->multicast_lock);
732 if (port->multicast_router != 1 ||
733 timer_pending(&port->multicast_router_timer) ||
734 hlist_unhashed(&port->rlist))
735 goto out;
736
737 hlist_del_init_rcu(&port->rlist);
738
739out:
740 spin_unlock(&br->multicast_lock);
741}
742
743static void br_multicast_local_router_expired(unsigned long data)
744{
745}
746
747static void __br_multicast_send_query(struct net_bridge *br,
748 struct net_bridge_port *port,
749 struct br_ip *ip)
750{
751 struct sk_buff *skb;
752
753 skb = br_multicast_alloc_query(br, ip);
754 if (!skb)
755 return;
756
757 if (port) {
758 __skb_push(skb, sizeof(struct ethhdr));
759 skb->dev = port->dev;
760 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
761 dev_queue_xmit);
762 } else
763 netif_rx(skb);
764}
765
766static void br_multicast_send_query(struct net_bridge *br,
767 struct net_bridge_port *port, u32 sent)
768{
769 unsigned long time;
770 struct br_ip br_group;
771
772 if (!netif_running(br->dev) || br->multicast_disabled ||
773 timer_pending(&br->multicast_querier_timer))
774 return;
775
776 memset(&br_group.u, 0, sizeof(br_group.u));
777
778 br_group.proto = htons(ETH_P_IP);
779 __br_multicast_send_query(br, port, &br_group);
780
781#if IS_ENABLED(CONFIG_IPV6)
782 br_group.proto = htons(ETH_P_IPV6);
783 __br_multicast_send_query(br, port, &br_group);
784#endif
785
786 time = jiffies;
787 time += sent < br->multicast_startup_query_count ?
788 br->multicast_startup_query_interval :
789 br->multicast_query_interval;
790 mod_timer(port ? &port->multicast_query_timer :
791 &br->multicast_query_timer, time);
792}
793
794static void br_multicast_port_query_expired(unsigned long data)
795{
796 struct net_bridge_port *port = (void *)data;
797 struct net_bridge *br = port->br;
798
799 spin_lock(&br->multicast_lock);
800 if (port->state == BR_STATE_DISABLED ||
801 port->state == BR_STATE_BLOCKING)
802 goto out;
803
804 if (port->multicast_startup_queries_sent <
805 br->multicast_startup_query_count)
806 port->multicast_startup_queries_sent++;
807
808 br_multicast_send_query(port->br, port,
809 port->multicast_startup_queries_sent);
810
811out:
812 spin_unlock(&br->multicast_lock);
813}
814
815void br_multicast_add_port(struct net_bridge_port *port)
816{
817 port->multicast_router = 1;
818
819 setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
820 (unsigned long)port);
821 setup_timer(&port->multicast_query_timer,
822 br_multicast_port_query_expired, (unsigned long)port);
823}
824
825void br_multicast_del_port(struct net_bridge_port *port)
826{
827 del_timer_sync(&port->multicast_router_timer);
828}
829
830static void __br_multicast_enable_port(struct net_bridge_port *port)
831{
832 port->multicast_startup_queries_sent = 0;
833
834 if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 ||
835 del_timer(&port->multicast_query_timer))
836 mod_timer(&port->multicast_query_timer, jiffies);
837}
838
839void br_multicast_enable_port(struct net_bridge_port *port)
840{
841 struct net_bridge *br = port->br;
842
843 spin_lock(&br->multicast_lock);
844 if (br->multicast_disabled || !netif_running(br->dev))
845 goto out;
846
847 __br_multicast_enable_port(port);
848 if (port->multicast_router == 2 && hlist_unhashed(&port->rlist))
849 br_multicast_add_router(br, port);
850
851out:
852 spin_unlock(&br->multicast_lock);
853}
854
855void br_multicast_disable_port(struct net_bridge_port *port)
856{
857 struct net_bridge *br = port->br;
858 struct net_bridge_port_group *pg;
859 struct hlist_node *p, *n;
860
861 spin_lock(&br->multicast_lock);
862 hlist_for_each_entry_safe(pg, p, n, &port->mglist, mglist)
863 br_multicast_del_pg(br, pg);
864
865 if (!hlist_unhashed(&port->rlist))
866 hlist_del_init_rcu(&port->rlist);
867 del_timer(&port->multicast_router_timer);
868 del_timer(&port->multicast_query_timer);
869 spin_unlock(&br->multicast_lock);
870}
871
872static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
873 struct net_bridge_port *port,
874 struct sk_buff *skb)
875{
876 struct igmpv3_report *ih;
877 struct igmpv3_grec *grec;
878 int i;
879 int len;
880 int num;
881 int type;
882 int err = 0;
883 __be32 group;
884
885 if (!pskb_may_pull(skb, sizeof(*ih)))
886 return -EINVAL;
887
888 ih = igmpv3_report_hdr(skb);
889 num = ntohs(ih->ngrec);
890 len = sizeof(*ih);
891
892 for (i = 0; i < num; i++) {
893 len += sizeof(*grec);
894 if (!pskb_may_pull(skb, len))
895 return -EINVAL;
896
897 grec = (void *)(skb->data + len - sizeof(*grec));
898 group = grec->grec_mca;
899 type = grec->grec_type;
900
901 len += ntohs(grec->grec_nsrcs) * 4;
902 if (!pskb_may_pull(skb, len))
903 return -EINVAL;
904
905 /* We treat this as an IGMPv2 report for now. */
906 switch (type) {
907 case IGMPV3_MODE_IS_INCLUDE:
908 case IGMPV3_MODE_IS_EXCLUDE:
909 case IGMPV3_CHANGE_TO_INCLUDE:
910 case IGMPV3_CHANGE_TO_EXCLUDE:
911 case IGMPV3_ALLOW_NEW_SOURCES:
912 case IGMPV3_BLOCK_OLD_SOURCES:
913 break;
914
915 default:
916 continue;
917 }
918
919 err = br_ip4_multicast_add_group(br, port, group);
920 if (err)
921 break;
922 }
923
924 return err;
925}
926
927#if IS_ENABLED(CONFIG_IPV6)
928static int br_ip6_multicast_mld2_report(struct net_bridge *br,
929 struct net_bridge_port *port,
930 struct sk_buff *skb)
931{
932 struct icmp6hdr *icmp6h;
933 struct mld2_grec *grec;
934 int i;
935 int len;
936 int num;
937 int err = 0;
938
939 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
940 return -EINVAL;
941
942 icmp6h = icmp6_hdr(skb);
943 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
944 len = sizeof(*icmp6h);
945
946 for (i = 0; i < num; i++) {
947 __be16 *nsrcs, _nsrcs;
948
949 nsrcs = skb_header_pointer(skb,
950 len + offsetof(struct mld2_grec,
951 grec_nsrcs),
952 sizeof(_nsrcs), &_nsrcs);
953 if (!nsrcs)
954 return -EINVAL;
955
956 if (!pskb_may_pull(skb,
957 len + sizeof(*grec) +
958 sizeof(struct in6_addr) * ntohs(*nsrcs)))
959 return -EINVAL;
960
961 grec = (struct mld2_grec *)(skb->data + len);
962 len += sizeof(*grec) +
963 sizeof(struct in6_addr) * ntohs(*nsrcs);
964
965 /* We treat these as MLDv1 reports for now. */
966 switch (grec->grec_type) {
967 case MLD2_MODE_IS_INCLUDE:
968 case MLD2_MODE_IS_EXCLUDE:
969 case MLD2_CHANGE_TO_INCLUDE:
970 case MLD2_CHANGE_TO_EXCLUDE:
971 case MLD2_ALLOW_NEW_SOURCES:
972 case MLD2_BLOCK_OLD_SOURCES:
973 break;
974
975 default:
976 continue;
977 }
978
979 err = br_ip6_multicast_add_group(br, port, &grec->grec_mca);
980 if (err)
981 break;
982 }
983
984 return err;
985}
986#endif
987
988/*
989 * Add port to rotuer_list
990 * list is maintained ordered by pointer value
991 * and locked by br->multicast_lock and RCU
992 */
993static void br_multicast_add_router(struct net_bridge *br,
994 struct net_bridge_port *port)
995{
996 struct net_bridge_port *p;
997 struct hlist_node *n, *slot = NULL;
998
999 if (!hlist_unhashed(&port->rlist))
1000 return;
1001
1002 hlist_for_each_entry(p, n, &br->router_list, rlist) {
1003 if ((unsigned long) port >= (unsigned long) p)
1004 break;
1005 slot = n;
1006 }
1007
1008 if (slot)
1009 hlist_add_after_rcu(slot, &port->rlist);
1010 else
1011 hlist_add_head_rcu(&port->rlist, &br->router_list);
1012}
1013
1014static void br_multicast_mark_router(struct net_bridge *br,
1015 struct net_bridge_port *port)
1016{
1017 unsigned long now = jiffies;
1018
1019 if (!port) {
1020 if (br->multicast_router == 1)
1021 mod_timer(&br->multicast_router_timer,
1022 now + br->multicast_querier_interval);
1023 return;
1024 }
1025
1026 if (port->multicast_router != 1)
1027 return;
1028
1029 br_multicast_add_router(br, port);
1030
1031 mod_timer(&port->multicast_router_timer,
1032 now + br->multicast_querier_interval);
1033}
1034
1035static void br_multicast_query_received(struct net_bridge *br,
1036 struct net_bridge_port *port,
1037 int saddr)
1038{
1039 if (saddr)
1040 mod_timer(&br->multicast_querier_timer,
1041 jiffies + br->multicast_querier_interval);
1042 else if (timer_pending(&br->multicast_querier_timer))
1043 return;
1044
1045 br_multicast_mark_router(br, port);
1046}
1047
1048static int br_ip4_multicast_query(struct net_bridge *br,
1049 struct net_bridge_port *port,
1050 struct sk_buff *skb)
1051{
1052 const struct iphdr *iph = ip_hdr(skb);
1053 struct igmphdr *ih = igmp_hdr(skb);
1054 struct net_bridge_mdb_entry *mp;
1055 struct igmpv3_query *ih3;
1056 struct net_bridge_port_group *p;
1057 struct net_bridge_port_group __rcu **pp;
1058 unsigned long max_delay;
1059 unsigned long now = jiffies;
1060 __be32 group;
1061 int err = 0;
1062
1063 spin_lock(&br->multicast_lock);
1064 if (!netif_running(br->dev) ||
1065 (port && port->state == BR_STATE_DISABLED))
1066 goto out;
1067
1068 br_multicast_query_received(br, port, !!iph->saddr);
1069
1070 group = ih->group;
1071
1072 if (skb->len == sizeof(*ih)) {
1073 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1074
1075 if (!max_delay) {
1076 max_delay = 10 * HZ;
1077 group = 0;
1078 }
1079 } else {
1080 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) {
1081 err = -EINVAL;
1082 goto out;
1083 }
1084
1085 ih3 = igmpv3_query_hdr(skb);
1086 if (ih3->nsrcs)
1087 goto out;
1088
1089 max_delay = ih3->code ?
1090 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
1091 }
1092
1093 if (!group)
1094 goto out;
1095
1096 mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group);
1097 if (!mp)
1098 goto out;
1099
1100 max_delay *= br->multicast_last_member_count;
1101
1102 if (mp->mglist &&
1103 (timer_pending(&mp->timer) ?
1104 time_after(mp->timer.expires, now + max_delay) :
1105 try_to_del_timer_sync(&mp->timer) >= 0))
1106 mod_timer(&mp->timer, now + max_delay);
1107
1108 for (pp = &mp->ports;
1109 (p = mlock_dereference(*pp, br)) != NULL;
1110 pp = &p->next) {
1111 if (timer_pending(&p->timer) ?
1112 time_after(p->timer.expires, now + max_delay) :
1113 try_to_del_timer_sync(&p->timer) >= 0)
1114 mod_timer(&p->timer, now + max_delay);
1115 }
1116
1117out:
1118 spin_unlock(&br->multicast_lock);
1119 return err;
1120}
1121
1122#if IS_ENABLED(CONFIG_IPV6)
1123static int br_ip6_multicast_query(struct net_bridge *br,
1124 struct net_bridge_port *port,
1125 struct sk_buff *skb)
1126{
1127 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
1128 struct mld_msg *mld = (struct mld_msg *) icmp6_hdr(skb);
1129 struct net_bridge_mdb_entry *mp;
1130 struct mld2_query *mld2q;
1131 struct net_bridge_port_group *p;
1132 struct net_bridge_port_group __rcu **pp;
1133 unsigned long max_delay;
1134 unsigned long now = jiffies;
1135 const struct in6_addr *group = NULL;
1136 int err = 0;
1137
1138 spin_lock(&br->multicast_lock);
1139 if (!netif_running(br->dev) ||
1140 (port && port->state == BR_STATE_DISABLED))
1141 goto out;
1142
1143 br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->saddr));
1144
1145 /* RFC2710+RFC3810 (MLDv1+MLDv2) require link-local source addresses */
1146 if (!(ipv6_addr_type(&ip6h->saddr) & IPV6_ADDR_LINKLOCAL)) {
1147 err = -EINVAL;
1148 goto out;
1149 }
1150
1151 if (skb->len == sizeof(*mld)) {
1152 if (!pskb_may_pull(skb, sizeof(*mld))) {
1153 err = -EINVAL;
1154 goto out;
1155 }
1156 mld = (struct mld_msg *) icmp6_hdr(skb);
1157 max_delay = msecs_to_jiffies(htons(mld->mld_maxdelay));
1158 if (max_delay)
1159 group = &mld->mld_mca;
1160 } else if (skb->len >= sizeof(*mld2q)) {
1161 if (!pskb_may_pull(skb, sizeof(*mld2q))) {
1162 err = -EINVAL;
1163 goto out;
1164 }
1165 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1166 if (!mld2q->mld2q_nsrcs)
1167 group = &mld2q->mld2q_mca;
1168
1169 max_delay = max(msecs_to_jiffies(MLDV2_MRC(ntohs(mld2q->mld2q_mrc))), 1UL);
1170 }
1171
1172 if (!group)
1173 goto out;
1174
1175 mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group);
1176 if (!mp)
1177 goto out;
1178
1179 max_delay *= br->multicast_last_member_count;
1180 if (mp->mglist &&
1181 (timer_pending(&mp->timer) ?
1182 time_after(mp->timer.expires, now + max_delay) :
1183 try_to_del_timer_sync(&mp->timer) >= 0))
1184 mod_timer(&mp->timer, now + max_delay);
1185
1186 for (pp = &mp->ports;
1187 (p = mlock_dereference(*pp, br)) != NULL;
1188 pp = &p->next) {
1189 if (timer_pending(&p->timer) ?
1190 time_after(p->timer.expires, now + max_delay) :
1191 try_to_del_timer_sync(&p->timer) >= 0)
1192 mod_timer(&p->timer, now + max_delay);
1193 }
1194
1195out:
1196 spin_unlock(&br->multicast_lock);
1197 return err;
1198}
1199#endif
1200
1201static void br_multicast_leave_group(struct net_bridge *br,
1202 struct net_bridge_port *port,
1203 struct br_ip *group)
1204{
1205 struct net_bridge_mdb_htable *mdb;
1206 struct net_bridge_mdb_entry *mp;
1207 struct net_bridge_port_group *p;
1208 unsigned long now;
1209 unsigned long time;
1210
1211 spin_lock(&br->multicast_lock);
1212 if (!netif_running(br->dev) ||
1213 (port && port->state == BR_STATE_DISABLED) ||
1214 timer_pending(&br->multicast_querier_timer))
1215 goto out;
1216
1217 mdb = mlock_dereference(br->mdb, br);
1218 mp = br_mdb_ip_get(mdb, group);
1219 if (!mp)
1220 goto out;
1221
1222 now = jiffies;
1223 time = now + br->multicast_last_member_count *
1224 br->multicast_last_member_interval;
1225
1226 if (!port) {
1227 if (mp->mglist &&
1228 (timer_pending(&mp->timer) ?
1229 time_after(mp->timer.expires, time) :
1230 try_to_del_timer_sync(&mp->timer) >= 0)) {
1231 mod_timer(&mp->timer, time);
1232 }
1233
1234 goto out;
1235 }
1236
1237 for (p = mlock_dereference(mp->ports, br);
1238 p != NULL;
1239 p = mlock_dereference(p->next, br)) {
1240 if (p->port != port)
1241 continue;
1242
1243 if (!hlist_unhashed(&p->mglist) &&
1244 (timer_pending(&p->timer) ?
1245 time_after(p->timer.expires, time) :
1246 try_to_del_timer_sync(&p->timer) >= 0)) {
1247 mod_timer(&p->timer, time);
1248 }
1249
1250 break;
1251 }
1252
1253out:
1254 spin_unlock(&br->multicast_lock);
1255}
1256
1257static void br_ip4_multicast_leave_group(struct net_bridge *br,
1258 struct net_bridge_port *port,
1259 __be32 group)
1260{
1261 struct br_ip br_group;
1262
1263 if (ipv4_is_local_multicast(group))
1264 return;
1265
1266 br_group.u.ip4 = group;
1267 br_group.proto = htons(ETH_P_IP);
1268
1269 br_multicast_leave_group(br, port, &br_group);
1270}
1271
1272#if IS_ENABLED(CONFIG_IPV6)
1273static void br_ip6_multicast_leave_group(struct net_bridge *br,
1274 struct net_bridge_port *port,
1275 const struct in6_addr *group)
1276{
1277 struct br_ip br_group;
1278
1279 if (!ipv6_is_transient_multicast(group))
1280 return;
1281
1282 br_group.u.ip6 = *group;
1283 br_group.proto = htons(ETH_P_IPV6);
1284
1285 br_multicast_leave_group(br, port, &br_group);
1286}
1287#endif
1288
1289static int br_multicast_ipv4_rcv(struct net_bridge *br,
1290 struct net_bridge_port *port,
1291 struct sk_buff *skb)
1292{
1293 struct sk_buff *skb2 = skb;
1294 const struct iphdr *iph;
1295 struct igmphdr *ih;
1296 unsigned len;
1297 unsigned offset;
1298 int err;
1299
1300 /* We treat OOM as packet loss for now. */
1301 if (!pskb_may_pull(skb, sizeof(*iph)))
1302 return -EINVAL;
1303
1304 iph = ip_hdr(skb);
1305
1306 if (iph->ihl < 5 || iph->version != 4)
1307 return -EINVAL;
1308
1309 if (!pskb_may_pull(skb, ip_hdrlen(skb)))
1310 return -EINVAL;
1311
1312 iph = ip_hdr(skb);
1313
1314 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1315 return -EINVAL;
1316
1317 if (iph->protocol != IPPROTO_IGMP) {
1318 if ((iph->daddr & IGMP_LOCAL_GROUP_MASK) != IGMP_LOCAL_GROUP)
1319 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1320 return 0;
1321 }
1322
1323 len = ntohs(iph->tot_len);
1324 if (skb->len < len || len < ip_hdrlen(skb))
1325 return -EINVAL;
1326
1327 if (skb->len > len) {
1328 skb2 = skb_clone(skb, GFP_ATOMIC);
1329 if (!skb2)
1330 return -ENOMEM;
1331
1332 err = pskb_trim_rcsum(skb2, len);
1333 if (err)
1334 goto err_out;
1335 }
1336
1337 len -= ip_hdrlen(skb2);
1338 offset = skb_network_offset(skb2) + ip_hdrlen(skb2);
1339 __skb_pull(skb2, offset);
1340 skb_reset_transport_header(skb2);
1341
1342 err = -EINVAL;
1343 if (!pskb_may_pull(skb2, sizeof(*ih)))
1344 goto out;
1345
1346 switch (skb2->ip_summed) {
1347 case CHECKSUM_COMPLETE:
1348 if (!csum_fold(skb2->csum))
1349 break;
1350 /* fall through */
1351 case CHECKSUM_NONE:
1352 skb2->csum = 0;
1353 if (skb_checksum_complete(skb2))
1354 goto out;
1355 }
1356
1357 err = 0;
1358
1359 BR_INPUT_SKB_CB(skb)->igmp = 1;
1360 ih = igmp_hdr(skb2);
1361
1362 switch (ih->type) {
1363 case IGMP_HOST_MEMBERSHIP_REPORT:
1364 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1365 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1366 err = br_ip4_multicast_add_group(br, port, ih->group);
1367 break;
1368 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1369 err = br_ip4_multicast_igmp3_report(br, port, skb2);
1370 break;
1371 case IGMP_HOST_MEMBERSHIP_QUERY:
1372 err = br_ip4_multicast_query(br, port, skb2);
1373 break;
1374 case IGMP_HOST_LEAVE_MESSAGE:
1375 br_ip4_multicast_leave_group(br, port, ih->group);
1376 break;
1377 }
1378
1379out:
1380 __skb_push(skb2, offset);
1381err_out:
1382 if (skb2 != skb)
1383 kfree_skb(skb2);
1384 return err;
1385}
1386
1387#if IS_ENABLED(CONFIG_IPV6)
1388static int br_multicast_ipv6_rcv(struct net_bridge *br,
1389 struct net_bridge_port *port,
1390 struct sk_buff *skb)
1391{
1392 struct sk_buff *skb2;
1393 const struct ipv6hdr *ip6h;
1394 u8 icmp6_type;
1395 u8 nexthdr;
1396 __be16 frag_off;
1397 unsigned len;
1398 int offset;
1399 int err;
1400
1401 if (!pskb_may_pull(skb, sizeof(*ip6h)))
1402 return -EINVAL;
1403
1404 ip6h = ipv6_hdr(skb);
1405
1406 /*
1407 * We're interested in MLD messages only.
1408 * - Version is 6
1409 * - MLD has always Router Alert hop-by-hop option
1410 * - But we do not support jumbrograms.
1411 */
1412 if (ip6h->version != 6 ||
1413 ip6h->nexthdr != IPPROTO_HOPOPTS ||
1414 ip6h->payload_len == 0)
1415 return 0;
1416
1417 len = ntohs(ip6h->payload_len) + sizeof(*ip6h);
1418 if (skb->len < len)
1419 return -EINVAL;
1420
1421 nexthdr = ip6h->nexthdr;
1422 offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr, &frag_off);
1423
1424 if (offset < 0 || nexthdr != IPPROTO_ICMPV6)
1425 return 0;
1426
1427 /* Okay, we found ICMPv6 header */
1428 skb2 = skb_clone(skb, GFP_ATOMIC);
1429 if (!skb2)
1430 return -ENOMEM;
1431
1432 err = -EINVAL;
1433 if (!pskb_may_pull(skb2, offset + sizeof(struct icmp6hdr)))
1434 goto out;
1435
1436 len -= offset - skb_network_offset(skb2);
1437
1438 __skb_pull(skb2, offset);
1439 skb_reset_transport_header(skb2);
1440 skb_postpull_rcsum(skb2, skb_network_header(skb2),
1441 skb_network_header_len(skb2));
1442
1443 icmp6_type = icmp6_hdr(skb2)->icmp6_type;
1444
1445 switch (icmp6_type) {
1446 case ICMPV6_MGM_QUERY:
1447 case ICMPV6_MGM_REPORT:
1448 case ICMPV6_MGM_REDUCTION:
1449 case ICMPV6_MLD2_REPORT:
1450 break;
1451 default:
1452 err = 0;
1453 goto out;
1454 }
1455
1456 /* Okay, we found MLD message. Check further. */
1457 if (skb2->len > len) {
1458 err = pskb_trim_rcsum(skb2, len);
1459 if (err)
1460 goto out;
1461 err = -EINVAL;
1462 }
1463
1464 ip6h = ipv6_hdr(skb2);
1465
1466 switch (skb2->ip_summed) {
1467 case CHECKSUM_COMPLETE:
1468 if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, skb2->len,
1469 IPPROTO_ICMPV6, skb2->csum))
1470 break;
1471 /*FALLTHROUGH*/
1472 case CHECKSUM_NONE:
1473 skb2->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr,
1474 &ip6h->daddr,
1475 skb2->len,
1476 IPPROTO_ICMPV6, 0));
1477 if (__skb_checksum_complete(skb2))
1478 goto out;
1479 }
1480
1481 err = 0;
1482
1483 BR_INPUT_SKB_CB(skb)->igmp = 1;
1484
1485 switch (icmp6_type) {
1486 case ICMPV6_MGM_REPORT:
1487 {
1488 struct mld_msg *mld;
1489 if (!pskb_may_pull(skb2, sizeof(*mld))) {
1490 err = -EINVAL;
1491 goto out;
1492 }
1493 mld = (struct mld_msg *)skb_transport_header(skb2);
1494 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1495 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca);
1496 break;
1497 }
1498 case ICMPV6_MLD2_REPORT:
1499 err = br_ip6_multicast_mld2_report(br, port, skb2);
1500 break;
1501 case ICMPV6_MGM_QUERY:
1502 err = br_ip6_multicast_query(br, port, skb2);
1503 break;
1504 case ICMPV6_MGM_REDUCTION:
1505 {
1506 struct mld_msg *mld;
1507 if (!pskb_may_pull(skb2, sizeof(*mld))) {
1508 err = -EINVAL;
1509 goto out;
1510 }
1511 mld = (struct mld_msg *)skb_transport_header(skb2);
1512 br_ip6_multicast_leave_group(br, port, &mld->mld_mca);
1513 }
1514 }
1515
1516out:
1517 kfree_skb(skb2);
1518 return err;
1519}
1520#endif
1521
1522int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1523 struct sk_buff *skb)
1524{
1525 BR_INPUT_SKB_CB(skb)->igmp = 0;
1526 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1527
1528 if (br->multicast_disabled)
1529 return 0;
1530
1531 switch (skb->protocol) {
1532 case htons(ETH_P_IP):
1533 return br_multicast_ipv4_rcv(br, port, skb);
1534#if IS_ENABLED(CONFIG_IPV6)
1535 case htons(ETH_P_IPV6):
1536 return br_multicast_ipv6_rcv(br, port, skb);
1537#endif
1538 }
1539
1540 return 0;
1541}
1542
1543static void br_multicast_query_expired(unsigned long data)
1544{
1545 struct net_bridge *br = (void *)data;
1546
1547 spin_lock(&br->multicast_lock);
1548 if (br->multicast_startup_queries_sent <
1549 br->multicast_startup_query_count)
1550 br->multicast_startup_queries_sent++;
1551
1552 br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent);
1553
1554 spin_unlock(&br->multicast_lock);
1555}
1556
1557void br_multicast_init(struct net_bridge *br)
1558{
1559 br->hash_elasticity = 4;
1560 br->hash_max = 512;
1561
1562 br->multicast_router = 1;
1563 br->multicast_last_member_count = 2;
1564 br->multicast_startup_query_count = 2;
1565
1566 br->multicast_last_member_interval = HZ;
1567 br->multicast_query_response_interval = 10 * HZ;
1568 br->multicast_startup_query_interval = 125 * HZ / 4;
1569 br->multicast_query_interval = 125 * HZ;
1570 br->multicast_querier_interval = 255 * HZ;
1571 br->multicast_membership_interval = 260 * HZ;
1572
1573 spin_lock_init(&br->multicast_lock);
1574 setup_timer(&br->multicast_router_timer,
1575 br_multicast_local_router_expired, 0);
1576 setup_timer(&br->multicast_querier_timer,
1577 br_multicast_local_router_expired, 0);
1578 setup_timer(&br->multicast_query_timer, br_multicast_query_expired,
1579 (unsigned long)br);
1580}
1581
1582void br_multicast_open(struct net_bridge *br)
1583{
1584 br->multicast_startup_queries_sent = 0;
1585
1586 if (br->multicast_disabled)
1587 return;
1588
1589 mod_timer(&br->multicast_query_timer, jiffies);
1590}
1591
1592void br_multicast_stop(struct net_bridge *br)
1593{
1594 struct net_bridge_mdb_htable *mdb;
1595 struct net_bridge_mdb_entry *mp;
1596 struct hlist_node *p, *n;
1597 u32 ver;
1598 int i;
1599
1600 del_timer_sync(&br->multicast_router_timer);
1601 del_timer_sync(&br->multicast_querier_timer);
1602 del_timer_sync(&br->multicast_query_timer);
1603
1604 spin_lock_bh(&br->multicast_lock);
1605 mdb = mlock_dereference(br->mdb, br);
1606 if (!mdb)
1607 goto out;
1608
1609 br->mdb = NULL;
1610
1611 ver = mdb->ver;
1612 for (i = 0; i < mdb->max; i++) {
1613 hlist_for_each_entry_safe(mp, p, n, &mdb->mhash[i],
1614 hlist[ver]) {
1615 del_timer(&mp->timer);
1616 call_rcu_bh(&mp->rcu, br_multicast_free_group);
1617 }
1618 }
1619
1620 if (mdb->old) {
1621 spin_unlock_bh(&br->multicast_lock);
1622 rcu_barrier_bh();
1623 spin_lock_bh(&br->multicast_lock);
1624 WARN_ON(mdb->old);
1625 }
1626
1627 mdb->old = mdb;
1628 call_rcu_bh(&mdb->rcu, br_mdb_free);
1629
1630out:
1631 spin_unlock_bh(&br->multicast_lock);
1632}
1633
1634int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1635{
1636 int err = -ENOENT;
1637
1638 spin_lock_bh(&br->multicast_lock);
1639 if (!netif_running(br->dev))
1640 goto unlock;
1641
1642 switch (val) {
1643 case 0:
1644 case 2:
1645 del_timer(&br->multicast_router_timer);
1646 /* fall through */
1647 case 1:
1648 br->multicast_router = val;
1649 err = 0;
1650 break;
1651
1652 default:
1653 err = -EINVAL;
1654 break;
1655 }
1656
1657unlock:
1658 spin_unlock_bh(&br->multicast_lock);
1659
1660 return err;
1661}
1662
1663int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1664{
1665 struct net_bridge *br = p->br;
1666 int err = -ENOENT;
1667
1668 spin_lock(&br->multicast_lock);
1669 if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED)
1670 goto unlock;
1671
1672 switch (val) {
1673 case 0:
1674 case 1:
1675 case 2:
1676 p->multicast_router = val;
1677 err = 0;
1678
1679 if (val < 2 && !hlist_unhashed(&p->rlist))
1680 hlist_del_init_rcu(&p->rlist);
1681
1682 if (val == 1)
1683 break;
1684
1685 del_timer(&p->multicast_router_timer);
1686
1687 if (val == 0)
1688 break;
1689
1690 br_multicast_add_router(br, p);
1691 break;
1692
1693 default:
1694 err = -EINVAL;
1695 break;
1696 }
1697
1698unlock:
1699 spin_unlock(&br->multicast_lock);
1700
1701 return err;
1702}
1703
1704int br_multicast_toggle(struct net_bridge *br, unsigned long val)
1705{
1706 struct net_bridge_port *port;
1707 int err = 0;
1708 struct net_bridge_mdb_htable *mdb;
1709
1710 spin_lock_bh(&br->multicast_lock);
1711 if (br->multicast_disabled == !val)
1712 goto unlock;
1713
1714 br->multicast_disabled = !val;
1715 if (br->multicast_disabled)
1716 goto unlock;
1717
1718 if (!netif_running(br->dev))
1719 goto unlock;
1720
1721 mdb = mlock_dereference(br->mdb, br);
1722 if (mdb) {
1723 if (mdb->old) {
1724 err = -EEXIST;
1725rollback:
1726 br->multicast_disabled = !!val;
1727 goto unlock;
1728 }
1729
1730 err = br_mdb_rehash(&br->mdb, mdb->max,
1731 br->hash_elasticity);
1732 if (err)
1733 goto rollback;
1734 }
1735
1736 br_multicast_open(br);
1737 list_for_each_entry(port, &br->port_list, list) {
1738 if (port->state == BR_STATE_DISABLED ||
1739 port->state == BR_STATE_BLOCKING)
1740 continue;
1741
1742 __br_multicast_enable_port(port);
1743 }
1744
1745unlock:
1746 spin_unlock_bh(&br->multicast_lock);
1747
1748 return err;
1749}
1750
1751int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
1752{
1753 int err = -ENOENT;
1754 u32 old;
1755 struct net_bridge_mdb_htable *mdb;
1756
1757 spin_lock_bh(&br->multicast_lock);
1758 if (!netif_running(br->dev))
1759 goto unlock;
1760
1761 err = -EINVAL;
1762 if (!is_power_of_2(val))
1763 goto unlock;
1764
1765 mdb = mlock_dereference(br->mdb, br);
1766 if (mdb && val < mdb->size)
1767 goto unlock;
1768
1769 err = 0;
1770
1771 old = br->hash_max;
1772 br->hash_max = val;
1773
1774 if (mdb) {
1775 if (mdb->old) {
1776 err = -EEXIST;
1777rollback:
1778 br->hash_max = old;
1779 goto unlock;
1780 }
1781
1782 err = br_mdb_rehash(&br->mdb, br->hash_max,
1783 br->hash_elasticity);
1784 if (err)
1785 goto rollback;
1786 }
1787
1788unlock:
1789 spin_unlock_bh(&br->multicast_lock);
1790
1791 return err;
1792}