Linux 4.19.133
[linux/fpc-iii.git] / net / bridge / br_multicast.c
blob6a362da211e1b45aa5e7b94a8085217ab707023f
1 /*
2 * Bridge multicast support.
4 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
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.
13 #include <linux/err.h>
14 #include <linux/export.h>
15 #include <linux/if_ether.h>
16 #include <linux/igmp.h>
17 #include <linux/jhash.h>
18 #include <linux/kernel.h>
19 #include <linux/log2.h>
20 #include <linux/netdevice.h>
21 #include <linux/netfilter_bridge.h>
22 #include <linux/random.h>
23 #include <linux/rculist.h>
24 #include <linux/skbuff.h>
25 #include <linux/slab.h>
26 #include <linux/timer.h>
27 #include <linux/inetdevice.h>
28 #include <linux/mroute.h>
29 #include <net/ip.h>
30 #include <net/switchdev.h>
31 #if IS_ENABLED(CONFIG_IPV6)
32 #include <net/ipv6.h>
33 #include <net/mld.h>
34 #include <net/ip6_checksum.h>
35 #include <net/addrconf.h>
36 #endif
38 #include "br_private.h"
40 static void br_multicast_start_querier(struct net_bridge *br,
41 struct bridge_mcast_own_query *query);
42 static void br_multicast_add_router(struct net_bridge *br,
43 struct net_bridge_port *port);
44 static void br_ip4_multicast_leave_group(struct net_bridge *br,
45 struct net_bridge_port *port,
46 __be32 group,
47 __u16 vid,
48 const unsigned char *src);
50 static void __del_port_router(struct net_bridge_port *p);
51 #if IS_ENABLED(CONFIG_IPV6)
52 static void br_ip6_multicast_leave_group(struct net_bridge *br,
53 struct net_bridge_port *port,
54 const struct in6_addr *group,
55 __u16 vid, const unsigned char *src);
56 #endif
57 unsigned int br_mdb_rehash_seq;
59 static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
61 if (a->proto != b->proto)
62 return 0;
63 if (a->vid != b->vid)
64 return 0;
65 switch (a->proto) {
66 case htons(ETH_P_IP):
67 return a->u.ip4 == b->u.ip4;
68 #if IS_ENABLED(CONFIG_IPV6)
69 case htons(ETH_P_IPV6):
70 return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
71 #endif
73 return 0;
76 static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip,
77 __u16 vid)
79 return jhash_2words((__force u32)ip, vid, mdb->secret) & (mdb->max - 1);
82 #if IS_ENABLED(CONFIG_IPV6)
83 static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb,
84 const struct in6_addr *ip,
85 __u16 vid)
87 return jhash_2words(ipv6_addr_hash(ip), vid,
88 mdb->secret) & (mdb->max - 1);
90 #endif
92 static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb,
93 struct br_ip *ip)
95 switch (ip->proto) {
96 case htons(ETH_P_IP):
97 return __br_ip4_hash(mdb, ip->u.ip4, ip->vid);
98 #if IS_ENABLED(CONFIG_IPV6)
99 case htons(ETH_P_IPV6):
100 return __br_ip6_hash(mdb, &ip->u.ip6, ip->vid);
101 #endif
103 return 0;
106 static struct net_bridge_mdb_entry *__br_mdb_ip_get(
107 struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash)
109 struct net_bridge_mdb_entry *mp;
111 hlist_for_each_entry_rcu(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
112 if (br_ip_equal(&mp->addr, dst))
113 return mp;
116 return NULL;
119 struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge_mdb_htable *mdb,
120 struct br_ip *dst)
122 if (!mdb)
123 return NULL;
125 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
128 static struct net_bridge_mdb_entry *br_mdb_ip4_get(
129 struct net_bridge_mdb_htable *mdb, __be32 dst, __u16 vid)
131 struct br_ip br_dst;
133 br_dst.u.ip4 = dst;
134 br_dst.proto = htons(ETH_P_IP);
135 br_dst.vid = vid;
137 return br_mdb_ip_get(mdb, &br_dst);
140 #if IS_ENABLED(CONFIG_IPV6)
141 static struct net_bridge_mdb_entry *br_mdb_ip6_get(
142 struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst,
143 __u16 vid)
145 struct br_ip br_dst;
147 br_dst.u.ip6 = *dst;
148 br_dst.proto = htons(ETH_P_IPV6);
149 br_dst.vid = vid;
151 return br_mdb_ip_get(mdb, &br_dst);
153 #endif
155 struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
156 struct sk_buff *skb, u16 vid)
158 struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb);
159 struct br_ip ip;
161 if (br->multicast_disabled)
162 return NULL;
164 if (BR_INPUT_SKB_CB(skb)->igmp)
165 return NULL;
167 ip.proto = skb->protocol;
168 ip.vid = vid;
170 switch (skb->protocol) {
171 case htons(ETH_P_IP):
172 ip.u.ip4 = ip_hdr(skb)->daddr;
173 break;
174 #if IS_ENABLED(CONFIG_IPV6)
175 case htons(ETH_P_IPV6):
176 ip.u.ip6 = ipv6_hdr(skb)->daddr;
177 break;
178 #endif
179 default:
180 return NULL;
183 return br_mdb_ip_get(mdb, &ip);
186 static void br_mdb_free(struct rcu_head *head)
188 struct net_bridge_mdb_htable *mdb =
189 container_of(head, struct net_bridge_mdb_htable, rcu);
190 struct net_bridge_mdb_htable *old = mdb->old;
192 mdb->old = NULL;
193 kfree(old->mhash);
194 kfree(old);
197 static int br_mdb_copy(struct net_bridge_mdb_htable *new,
198 struct net_bridge_mdb_htable *old,
199 int elasticity)
201 struct net_bridge_mdb_entry *mp;
202 int maxlen;
203 int len;
204 int i;
206 for (i = 0; i < old->max; i++)
207 hlist_for_each_entry(mp, &old->mhash[i], hlist[old->ver])
208 hlist_add_head(&mp->hlist[new->ver],
209 &new->mhash[br_ip_hash(new, &mp->addr)]);
211 if (!elasticity)
212 return 0;
214 maxlen = 0;
215 for (i = 0; i < new->max; i++) {
216 len = 0;
217 hlist_for_each_entry(mp, &new->mhash[i], hlist[new->ver])
218 len++;
219 if (len > maxlen)
220 maxlen = len;
223 return maxlen > elasticity ? -EINVAL : 0;
226 void br_multicast_free_pg(struct rcu_head *head)
228 struct net_bridge_port_group *p =
229 container_of(head, struct net_bridge_port_group, rcu);
231 kfree(p);
234 static void br_multicast_free_group(struct rcu_head *head)
236 struct net_bridge_mdb_entry *mp =
237 container_of(head, struct net_bridge_mdb_entry, rcu);
239 kfree(mp);
242 static void br_multicast_group_expired(struct timer_list *t)
244 struct net_bridge_mdb_entry *mp = from_timer(mp, t, timer);
245 struct net_bridge *br = mp->br;
246 struct net_bridge_mdb_htable *mdb;
248 spin_lock(&br->multicast_lock);
249 if (!netif_running(br->dev) || timer_pending(&mp->timer))
250 goto out;
252 mp->host_joined = false;
253 br_mdb_notify(br->dev, NULL, &mp->addr, RTM_DELMDB, 0);
255 if (mp->ports)
256 goto out;
258 mdb = mlock_dereference(br->mdb, br);
260 hlist_del_rcu(&mp->hlist[mdb->ver]);
261 mdb->size--;
263 call_rcu_bh(&mp->rcu, br_multicast_free_group);
265 out:
266 spin_unlock(&br->multicast_lock);
269 static void br_multicast_del_pg(struct net_bridge *br,
270 struct net_bridge_port_group *pg)
272 struct net_bridge_mdb_htable *mdb;
273 struct net_bridge_mdb_entry *mp;
274 struct net_bridge_port_group *p;
275 struct net_bridge_port_group __rcu **pp;
277 mdb = mlock_dereference(br->mdb, br);
279 mp = br_mdb_ip_get(mdb, &pg->addr);
280 if (WARN_ON(!mp))
281 return;
283 for (pp = &mp->ports;
284 (p = mlock_dereference(*pp, br)) != NULL;
285 pp = &p->next) {
286 if (p != pg)
287 continue;
289 rcu_assign_pointer(*pp, p->next);
290 hlist_del_init(&p->mglist);
291 del_timer(&p->timer);
292 br_mdb_notify(br->dev, p->port, &pg->addr, RTM_DELMDB,
293 p->flags);
294 call_rcu_bh(&p->rcu, br_multicast_free_pg);
296 if (!mp->ports && !mp->host_joined &&
297 netif_running(br->dev))
298 mod_timer(&mp->timer, jiffies);
300 return;
303 WARN_ON(1);
306 static void br_multicast_port_group_expired(struct timer_list *t)
308 struct net_bridge_port_group *pg = from_timer(pg, t, timer);
309 struct net_bridge *br = pg->port->br;
311 spin_lock(&br->multicast_lock);
312 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
313 hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT)
314 goto out;
316 br_multicast_del_pg(br, pg);
318 out:
319 spin_unlock(&br->multicast_lock);
322 static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max,
323 int elasticity)
325 struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1);
326 struct net_bridge_mdb_htable *mdb;
327 int err;
329 mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
330 if (!mdb)
331 return -ENOMEM;
333 mdb->max = max;
334 mdb->old = old;
336 mdb->mhash = kcalloc(max, sizeof(*mdb->mhash), GFP_ATOMIC);
337 if (!mdb->mhash) {
338 kfree(mdb);
339 return -ENOMEM;
342 mdb->size = old ? old->size : 0;
343 mdb->ver = old ? old->ver ^ 1 : 0;
345 if (!old || elasticity)
346 get_random_bytes(&mdb->secret, sizeof(mdb->secret));
347 else
348 mdb->secret = old->secret;
350 if (!old)
351 goto out;
353 err = br_mdb_copy(mdb, old, elasticity);
354 if (err) {
355 kfree(mdb->mhash);
356 kfree(mdb);
357 return err;
360 br_mdb_rehash_seq++;
361 call_rcu_bh(&mdb->rcu, br_mdb_free);
363 out:
364 rcu_assign_pointer(*mdbp, mdb);
366 return 0;
369 static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
370 __be32 group,
371 u8 *igmp_type)
373 struct igmpv3_query *ihv3;
374 size_t igmp_hdr_size;
375 struct sk_buff *skb;
376 struct igmphdr *ih;
377 struct ethhdr *eth;
378 struct iphdr *iph;
380 igmp_hdr_size = sizeof(*ih);
381 if (br->multicast_igmp_version == 3)
382 igmp_hdr_size = sizeof(*ihv3);
383 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
384 igmp_hdr_size + 4);
385 if (!skb)
386 goto out;
388 skb->protocol = htons(ETH_P_IP);
390 skb_reset_mac_header(skb);
391 eth = eth_hdr(skb);
393 ether_addr_copy(eth->h_source, br->dev->dev_addr);
394 eth->h_dest[0] = 1;
395 eth->h_dest[1] = 0;
396 eth->h_dest[2] = 0x5e;
397 eth->h_dest[3] = 0;
398 eth->h_dest[4] = 0;
399 eth->h_dest[5] = 1;
400 eth->h_proto = htons(ETH_P_IP);
401 skb_put(skb, sizeof(*eth));
403 skb_set_network_header(skb, skb->len);
404 iph = ip_hdr(skb);
406 iph->version = 4;
407 iph->ihl = 6;
408 iph->tos = 0xc0;
409 iph->tot_len = htons(sizeof(*iph) + igmp_hdr_size + 4);
410 iph->id = 0;
411 iph->frag_off = htons(IP_DF);
412 iph->ttl = 1;
413 iph->protocol = IPPROTO_IGMP;
414 iph->saddr = br->multicast_query_use_ifaddr ?
415 inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0;
416 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
417 ((u8 *)&iph[1])[0] = IPOPT_RA;
418 ((u8 *)&iph[1])[1] = 4;
419 ((u8 *)&iph[1])[2] = 0;
420 ((u8 *)&iph[1])[3] = 0;
421 ip_send_check(iph);
422 skb_put(skb, 24);
424 skb_set_transport_header(skb, skb->len);
425 *igmp_type = IGMP_HOST_MEMBERSHIP_QUERY;
427 switch (br->multicast_igmp_version) {
428 case 2:
429 ih = igmp_hdr(skb);
430 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
431 ih->code = (group ? br->multicast_last_member_interval :
432 br->multicast_query_response_interval) /
433 (HZ / IGMP_TIMER_SCALE);
434 ih->group = group;
435 ih->csum = 0;
436 ih->csum = ip_compute_csum((void *)ih, sizeof(*ih));
437 break;
438 case 3:
439 ihv3 = igmpv3_query_hdr(skb);
440 ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY;
441 ihv3->code = (group ? br->multicast_last_member_interval :
442 br->multicast_query_response_interval) /
443 (HZ / IGMP_TIMER_SCALE);
444 ihv3->group = group;
445 ihv3->qqic = br->multicast_query_interval / HZ;
446 ihv3->nsrcs = 0;
447 ihv3->resv = 0;
448 ihv3->suppress = 0;
449 ihv3->qrv = 2;
450 ihv3->csum = 0;
451 ihv3->csum = ip_compute_csum((void *)ihv3, sizeof(*ihv3));
452 break;
455 skb_put(skb, igmp_hdr_size);
456 __skb_pull(skb, sizeof(*eth));
458 out:
459 return skb;
462 #if IS_ENABLED(CONFIG_IPV6)
463 static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
464 const struct in6_addr *grp,
465 u8 *igmp_type)
467 struct mld2_query *mld2q;
468 unsigned long interval;
469 struct ipv6hdr *ip6h;
470 struct mld_msg *mldq;
471 size_t mld_hdr_size;
472 struct sk_buff *skb;
473 struct ethhdr *eth;
474 u8 *hopopt;
476 mld_hdr_size = sizeof(*mldq);
477 if (br->multicast_mld_version == 2)
478 mld_hdr_size = sizeof(*mld2q);
479 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
480 8 + mld_hdr_size);
481 if (!skb)
482 goto out;
484 skb->protocol = htons(ETH_P_IPV6);
486 /* Ethernet header */
487 skb_reset_mac_header(skb);
488 eth = eth_hdr(skb);
490 ether_addr_copy(eth->h_source, br->dev->dev_addr);
491 eth->h_proto = htons(ETH_P_IPV6);
492 skb_put(skb, sizeof(*eth));
494 /* IPv6 header + HbH option */
495 skb_set_network_header(skb, skb->len);
496 ip6h = ipv6_hdr(skb);
498 *(__force __be32 *)ip6h = htonl(0x60000000);
499 ip6h->payload_len = htons(8 + mld_hdr_size);
500 ip6h->nexthdr = IPPROTO_HOPOPTS;
501 ip6h->hop_limit = 1;
502 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
503 if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
504 &ip6h->saddr)) {
505 kfree_skb(skb);
506 br->has_ipv6_addr = 0;
507 return NULL;
510 br->has_ipv6_addr = 1;
511 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
513 hopopt = (u8 *)(ip6h + 1);
514 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
515 hopopt[1] = 0; /* length of HbH */
516 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
517 hopopt[3] = 2; /* Length of RA Option */
518 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
519 hopopt[5] = 0;
520 hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */
521 hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */
523 skb_put(skb, sizeof(*ip6h) + 8);
525 /* ICMPv6 */
526 skb_set_transport_header(skb, skb->len);
527 interval = ipv6_addr_any(grp) ?
528 br->multicast_query_response_interval :
529 br->multicast_last_member_interval;
530 *igmp_type = ICMPV6_MGM_QUERY;
531 switch (br->multicast_mld_version) {
532 case 1:
533 mldq = (struct mld_msg *)icmp6_hdr(skb);
534 mldq->mld_type = ICMPV6_MGM_QUERY;
535 mldq->mld_code = 0;
536 mldq->mld_cksum = 0;
537 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
538 mldq->mld_reserved = 0;
539 mldq->mld_mca = *grp;
540 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
541 sizeof(*mldq), IPPROTO_ICMPV6,
542 csum_partial(mldq,
543 sizeof(*mldq),
544 0));
545 break;
546 case 2:
547 mld2q = (struct mld2_query *)icmp6_hdr(skb);
548 mld2q->mld2q_mrc = htons((u16)jiffies_to_msecs(interval));
549 mld2q->mld2q_type = ICMPV6_MGM_QUERY;
550 mld2q->mld2q_code = 0;
551 mld2q->mld2q_cksum = 0;
552 mld2q->mld2q_resv1 = 0;
553 mld2q->mld2q_resv2 = 0;
554 mld2q->mld2q_suppress = 0;
555 mld2q->mld2q_qrv = 2;
556 mld2q->mld2q_nsrcs = 0;
557 mld2q->mld2q_qqic = br->multicast_query_interval / HZ;
558 mld2q->mld2q_mca = *grp;
559 mld2q->mld2q_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
560 sizeof(*mld2q),
561 IPPROTO_ICMPV6,
562 csum_partial(mld2q,
563 sizeof(*mld2q),
564 0));
565 break;
567 skb_put(skb, mld_hdr_size);
569 __skb_pull(skb, sizeof(*eth));
571 out:
572 return skb;
574 #endif
576 static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
577 struct br_ip *addr,
578 u8 *igmp_type)
580 switch (addr->proto) {
581 case htons(ETH_P_IP):
582 return br_ip4_multicast_alloc_query(br, addr->u.ip4, igmp_type);
583 #if IS_ENABLED(CONFIG_IPV6)
584 case htons(ETH_P_IPV6):
585 return br_ip6_multicast_alloc_query(br, &addr->u.ip6,
586 igmp_type);
587 #endif
589 return NULL;
592 static struct net_bridge_mdb_entry *br_multicast_get_group(
593 struct net_bridge *br, struct net_bridge_port *port,
594 struct br_ip *group, int hash)
596 struct net_bridge_mdb_htable *mdb;
597 struct net_bridge_mdb_entry *mp;
598 unsigned int count = 0;
599 unsigned int max;
600 int elasticity;
601 int err;
603 mdb = rcu_dereference_protected(br->mdb, 1);
604 hlist_for_each_entry(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
605 count++;
606 if (unlikely(br_ip_equal(group, &mp->addr)))
607 return mp;
610 elasticity = 0;
611 max = mdb->max;
613 if (unlikely(count > br->hash_elasticity && count)) {
614 if (net_ratelimit())
615 br_info(br, "Multicast hash table "
616 "chain limit reached: %s\n",
617 port ? port->dev->name : br->dev->name);
619 elasticity = br->hash_elasticity;
622 if (mdb->size >= max) {
623 max *= 2;
624 if (unlikely(max > br->hash_max)) {
625 br_warn(br, "Multicast hash table maximum of %d "
626 "reached, disabling snooping: %s\n",
627 br->hash_max,
628 port ? port->dev->name : br->dev->name);
629 err = -E2BIG;
630 disable:
631 br->multicast_disabled = 1;
632 goto err;
636 if (max > mdb->max || elasticity) {
637 if (mdb->old) {
638 if (net_ratelimit())
639 br_info(br, "Multicast hash table "
640 "on fire: %s\n",
641 port ? port->dev->name : br->dev->name);
642 err = -EEXIST;
643 goto err;
646 err = br_mdb_rehash(&br->mdb, max, elasticity);
647 if (err) {
648 br_warn(br, "Cannot rehash multicast "
649 "hash table, disabling snooping: %s, %d, %d\n",
650 port ? port->dev->name : br->dev->name,
651 mdb->size, err);
652 goto disable;
655 err = -EAGAIN;
656 goto err;
659 return NULL;
661 err:
662 mp = ERR_PTR(err);
663 return mp;
666 struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
667 struct net_bridge_port *p,
668 struct br_ip *group)
670 struct net_bridge_mdb_htable *mdb;
671 struct net_bridge_mdb_entry *mp;
672 int hash;
673 int err;
675 mdb = rcu_dereference_protected(br->mdb, 1);
676 if (!mdb) {
677 err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0);
678 if (err)
679 return ERR_PTR(err);
680 goto rehash;
683 hash = br_ip_hash(mdb, group);
684 mp = br_multicast_get_group(br, p, group, hash);
685 switch (PTR_ERR(mp)) {
686 case 0:
687 break;
689 case -EAGAIN:
690 rehash:
691 mdb = rcu_dereference_protected(br->mdb, 1);
692 hash = br_ip_hash(mdb, group);
693 break;
695 default:
696 goto out;
699 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
700 if (unlikely(!mp))
701 return ERR_PTR(-ENOMEM);
703 mp->br = br;
704 mp->addr = *group;
705 timer_setup(&mp->timer, br_multicast_group_expired, 0);
707 hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
708 mdb->size++;
710 out:
711 return mp;
714 struct net_bridge_port_group *br_multicast_new_port_group(
715 struct net_bridge_port *port,
716 struct br_ip *group,
717 struct net_bridge_port_group __rcu *next,
718 unsigned char flags,
719 const unsigned char *src)
721 struct net_bridge_port_group *p;
723 p = kzalloc(sizeof(*p), GFP_ATOMIC);
724 if (unlikely(!p))
725 return NULL;
727 p->addr = *group;
728 p->port = port;
729 p->flags = flags;
730 rcu_assign_pointer(p->next, next);
731 hlist_add_head(&p->mglist, &port->mglist);
732 timer_setup(&p->timer, br_multicast_port_group_expired, 0);
734 if (src)
735 memcpy(p->eth_addr, src, ETH_ALEN);
736 else
737 memset(p->eth_addr, 0xff, ETH_ALEN);
739 return p;
742 static bool br_port_group_equal(struct net_bridge_port_group *p,
743 struct net_bridge_port *port,
744 const unsigned char *src)
746 if (p->port != port)
747 return false;
749 if (!(port->flags & BR_MULTICAST_TO_UNICAST))
750 return true;
752 return ether_addr_equal(src, p->eth_addr);
755 static int br_multicast_add_group(struct net_bridge *br,
756 struct net_bridge_port *port,
757 struct br_ip *group,
758 const unsigned char *src)
760 struct net_bridge_port_group __rcu **pp;
761 struct net_bridge_port_group *p;
762 struct net_bridge_mdb_entry *mp;
763 unsigned long now = jiffies;
764 int err;
766 spin_lock(&br->multicast_lock);
767 if (!netif_running(br->dev) ||
768 (port && port->state == BR_STATE_DISABLED))
769 goto out;
771 mp = br_multicast_new_group(br, port, group);
772 err = PTR_ERR(mp);
773 if (IS_ERR(mp))
774 goto err;
776 if (!port) {
777 if (!mp->host_joined) {
778 mp->host_joined = true;
779 br_mdb_notify(br->dev, NULL, &mp->addr, RTM_NEWMDB, 0);
781 mod_timer(&mp->timer, now + br->multicast_membership_interval);
782 goto out;
785 for (pp = &mp->ports;
786 (p = mlock_dereference(*pp, br)) != NULL;
787 pp = &p->next) {
788 if (br_port_group_equal(p, port, src))
789 goto found;
790 if ((unsigned long)p->port < (unsigned long)port)
791 break;
794 p = br_multicast_new_port_group(port, group, *pp, 0, src);
795 if (unlikely(!p))
796 goto err;
797 rcu_assign_pointer(*pp, p);
798 br_mdb_notify(br->dev, port, group, RTM_NEWMDB, 0);
800 found:
801 mod_timer(&p->timer, now + br->multicast_membership_interval);
802 out:
803 err = 0;
805 err:
806 spin_unlock(&br->multicast_lock);
807 return err;
810 static int br_ip4_multicast_add_group(struct net_bridge *br,
811 struct net_bridge_port *port,
812 __be32 group,
813 __u16 vid,
814 const unsigned char *src)
816 struct br_ip br_group;
818 if (ipv4_is_local_multicast(group))
819 return 0;
821 br_group.u.ip4 = group;
822 br_group.proto = htons(ETH_P_IP);
823 br_group.vid = vid;
825 return br_multicast_add_group(br, port, &br_group, src);
828 #if IS_ENABLED(CONFIG_IPV6)
829 static int br_ip6_multicast_add_group(struct net_bridge *br,
830 struct net_bridge_port *port,
831 const struct in6_addr *group,
832 __u16 vid,
833 const unsigned char *src)
835 struct br_ip br_group;
837 if (ipv6_addr_is_ll_all_nodes(group))
838 return 0;
840 br_group.u.ip6 = *group;
841 br_group.proto = htons(ETH_P_IPV6);
842 br_group.vid = vid;
844 return br_multicast_add_group(br, port, &br_group, src);
846 #endif
848 static void br_multicast_router_expired(struct timer_list *t)
850 struct net_bridge_port *port =
851 from_timer(port, t, multicast_router_timer);
852 struct net_bridge *br = port->br;
854 spin_lock(&br->multicast_lock);
855 if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
856 port->multicast_router == MDB_RTR_TYPE_PERM ||
857 timer_pending(&port->multicast_router_timer))
858 goto out;
860 __del_port_router(port);
861 out:
862 spin_unlock(&br->multicast_lock);
865 static void br_mc_router_state_change(struct net_bridge *p,
866 bool is_mc_router)
868 struct switchdev_attr attr = {
869 .orig_dev = p->dev,
870 .id = SWITCHDEV_ATTR_ID_BRIDGE_MROUTER,
871 .flags = SWITCHDEV_F_DEFER,
872 .u.mrouter = is_mc_router,
875 switchdev_port_attr_set(p->dev, &attr);
878 static void br_multicast_local_router_expired(struct timer_list *t)
880 struct net_bridge *br = from_timer(br, t, multicast_router_timer);
882 spin_lock(&br->multicast_lock);
883 if (br->multicast_router == MDB_RTR_TYPE_DISABLED ||
884 br->multicast_router == MDB_RTR_TYPE_PERM ||
885 timer_pending(&br->multicast_router_timer))
886 goto out;
888 br_mc_router_state_change(br, false);
889 out:
890 spin_unlock(&br->multicast_lock);
893 static void br_multicast_querier_expired(struct net_bridge *br,
894 struct bridge_mcast_own_query *query)
896 spin_lock(&br->multicast_lock);
897 if (!netif_running(br->dev) || br->multicast_disabled)
898 goto out;
900 br_multicast_start_querier(br, query);
902 out:
903 spin_unlock(&br->multicast_lock);
906 static void br_ip4_multicast_querier_expired(struct timer_list *t)
908 struct net_bridge *br = from_timer(br, t, ip4_other_query.timer);
910 br_multicast_querier_expired(br, &br->ip4_own_query);
913 #if IS_ENABLED(CONFIG_IPV6)
914 static void br_ip6_multicast_querier_expired(struct timer_list *t)
916 struct net_bridge *br = from_timer(br, t, ip6_other_query.timer);
918 br_multicast_querier_expired(br, &br->ip6_own_query);
920 #endif
922 static void br_multicast_select_own_querier(struct net_bridge *br,
923 struct br_ip *ip,
924 struct sk_buff *skb)
926 if (ip->proto == htons(ETH_P_IP))
927 br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr;
928 #if IS_ENABLED(CONFIG_IPV6)
929 else
930 br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr;
931 #endif
934 static void __br_multicast_send_query(struct net_bridge *br,
935 struct net_bridge_port *port,
936 struct br_ip *ip)
938 struct sk_buff *skb;
939 u8 igmp_type;
941 skb = br_multicast_alloc_query(br, ip, &igmp_type);
942 if (!skb)
943 return;
945 if (port) {
946 skb->dev = port->dev;
947 br_multicast_count(br, port, skb, igmp_type,
948 BR_MCAST_DIR_TX);
949 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
950 dev_net(port->dev), NULL, skb, NULL, skb->dev,
951 br_dev_queue_push_xmit);
952 } else {
953 br_multicast_select_own_querier(br, ip, skb);
954 br_multicast_count(br, port, skb, igmp_type,
955 BR_MCAST_DIR_RX);
956 netif_rx(skb);
960 static void br_multicast_send_query(struct net_bridge *br,
961 struct net_bridge_port *port,
962 struct bridge_mcast_own_query *own_query)
964 struct bridge_mcast_other_query *other_query = NULL;
965 struct br_ip br_group;
966 unsigned long time;
968 if (!netif_running(br->dev) || br->multicast_disabled ||
969 !br->multicast_querier)
970 return;
972 memset(&br_group.u, 0, sizeof(br_group.u));
974 if (port ? (own_query == &port->ip4_own_query) :
975 (own_query == &br->ip4_own_query)) {
976 other_query = &br->ip4_other_query;
977 br_group.proto = htons(ETH_P_IP);
978 #if IS_ENABLED(CONFIG_IPV6)
979 } else {
980 other_query = &br->ip6_other_query;
981 br_group.proto = htons(ETH_P_IPV6);
982 #endif
985 if (!other_query || timer_pending(&other_query->timer))
986 return;
988 __br_multicast_send_query(br, port, &br_group);
990 time = jiffies;
991 time += own_query->startup_sent < br->multicast_startup_query_count ?
992 br->multicast_startup_query_interval :
993 br->multicast_query_interval;
994 mod_timer(&own_query->timer, time);
997 static void
998 br_multicast_port_query_expired(struct net_bridge_port *port,
999 struct bridge_mcast_own_query *query)
1001 struct net_bridge *br = port->br;
1003 spin_lock(&br->multicast_lock);
1004 if (port->state == BR_STATE_DISABLED ||
1005 port->state == BR_STATE_BLOCKING)
1006 goto out;
1008 if (query->startup_sent < br->multicast_startup_query_count)
1009 query->startup_sent++;
1011 br_multicast_send_query(port->br, port, query);
1013 out:
1014 spin_unlock(&br->multicast_lock);
1017 static void br_ip4_multicast_port_query_expired(struct timer_list *t)
1019 struct net_bridge_port *port = from_timer(port, t, ip4_own_query.timer);
1021 br_multicast_port_query_expired(port, &port->ip4_own_query);
1024 #if IS_ENABLED(CONFIG_IPV6)
1025 static void br_ip6_multicast_port_query_expired(struct timer_list *t)
1027 struct net_bridge_port *port = from_timer(port, t, ip6_own_query.timer);
1029 br_multicast_port_query_expired(port, &port->ip6_own_query);
1031 #endif
1033 static void br_mc_disabled_update(struct net_device *dev, bool value)
1035 struct switchdev_attr attr = {
1036 .orig_dev = dev,
1037 .id = SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED,
1038 .flags = SWITCHDEV_F_DEFER,
1039 .u.mc_disabled = value,
1042 switchdev_port_attr_set(dev, &attr);
1045 int br_multicast_add_port(struct net_bridge_port *port)
1047 port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
1049 timer_setup(&port->multicast_router_timer,
1050 br_multicast_router_expired, 0);
1051 timer_setup(&port->ip4_own_query.timer,
1052 br_ip4_multicast_port_query_expired, 0);
1053 #if IS_ENABLED(CONFIG_IPV6)
1054 timer_setup(&port->ip6_own_query.timer,
1055 br_ip6_multicast_port_query_expired, 0);
1056 #endif
1057 br_mc_disabled_update(port->dev, port->br->multicast_disabled);
1059 port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
1060 if (!port->mcast_stats)
1061 return -ENOMEM;
1063 return 0;
1066 void br_multicast_del_port(struct net_bridge_port *port)
1068 struct net_bridge *br = port->br;
1069 struct net_bridge_port_group *pg;
1070 struct hlist_node *n;
1072 /* Take care of the remaining groups, only perm ones should be left */
1073 spin_lock_bh(&br->multicast_lock);
1074 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
1075 br_multicast_del_pg(br, pg);
1076 spin_unlock_bh(&br->multicast_lock);
1077 del_timer_sync(&port->multicast_router_timer);
1078 free_percpu(port->mcast_stats);
1081 static void br_multicast_enable(struct bridge_mcast_own_query *query)
1083 query->startup_sent = 0;
1085 if (try_to_del_timer_sync(&query->timer) >= 0 ||
1086 del_timer(&query->timer))
1087 mod_timer(&query->timer, jiffies);
1090 static void __br_multicast_enable_port(struct net_bridge_port *port)
1092 struct net_bridge *br = port->br;
1094 if (br->multicast_disabled || !netif_running(br->dev))
1095 return;
1097 br_multicast_enable(&port->ip4_own_query);
1098 #if IS_ENABLED(CONFIG_IPV6)
1099 br_multicast_enable(&port->ip6_own_query);
1100 #endif
1101 if (port->multicast_router == MDB_RTR_TYPE_PERM &&
1102 hlist_unhashed(&port->rlist))
1103 br_multicast_add_router(br, port);
1106 void br_multicast_enable_port(struct net_bridge_port *port)
1108 struct net_bridge *br = port->br;
1110 spin_lock(&br->multicast_lock);
1111 __br_multicast_enable_port(port);
1112 spin_unlock(&br->multicast_lock);
1115 void br_multicast_disable_port(struct net_bridge_port *port)
1117 struct net_bridge *br = port->br;
1118 struct net_bridge_port_group *pg;
1119 struct hlist_node *n;
1121 spin_lock(&br->multicast_lock);
1122 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
1123 if (!(pg->flags & MDB_PG_FLAGS_PERMANENT))
1124 br_multicast_del_pg(br, pg);
1126 __del_port_router(port);
1128 del_timer(&port->multicast_router_timer);
1129 del_timer(&port->ip4_own_query.timer);
1130 #if IS_ENABLED(CONFIG_IPV6)
1131 del_timer(&port->ip6_own_query.timer);
1132 #endif
1133 spin_unlock(&br->multicast_lock);
1136 static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
1137 struct net_bridge_port *port,
1138 struct sk_buff *skb,
1139 u16 vid)
1141 const unsigned char *src;
1142 struct igmpv3_report *ih;
1143 struct igmpv3_grec *grec;
1144 int i;
1145 int len;
1146 int num;
1147 int type;
1148 int err = 0;
1149 __be32 group;
1150 u16 nsrcs;
1152 ih = igmpv3_report_hdr(skb);
1153 num = ntohs(ih->ngrec);
1154 len = skb_transport_offset(skb) + sizeof(*ih);
1156 for (i = 0; i < num; i++) {
1157 len += sizeof(*grec);
1158 if (!pskb_may_pull(skb, len))
1159 return -EINVAL;
1161 grec = (void *)(skb->data + len - sizeof(*grec));
1162 group = grec->grec_mca;
1163 type = grec->grec_type;
1164 nsrcs = ntohs(grec->grec_nsrcs);
1166 len += nsrcs * 4;
1167 if (!pskb_may_pull(skb, len))
1168 return -EINVAL;
1170 /* We treat this as an IGMPv2 report for now. */
1171 switch (type) {
1172 case IGMPV3_MODE_IS_INCLUDE:
1173 case IGMPV3_MODE_IS_EXCLUDE:
1174 case IGMPV3_CHANGE_TO_INCLUDE:
1175 case IGMPV3_CHANGE_TO_EXCLUDE:
1176 case IGMPV3_ALLOW_NEW_SOURCES:
1177 case IGMPV3_BLOCK_OLD_SOURCES:
1178 break;
1180 default:
1181 continue;
1184 src = eth_hdr(skb)->h_source;
1185 if ((type == IGMPV3_CHANGE_TO_INCLUDE ||
1186 type == IGMPV3_MODE_IS_INCLUDE) &&
1187 nsrcs == 0) {
1188 br_ip4_multicast_leave_group(br, port, group, vid, src);
1189 } else {
1190 err = br_ip4_multicast_add_group(br, port, group, vid,
1191 src);
1192 if (err)
1193 break;
1197 return err;
1200 #if IS_ENABLED(CONFIG_IPV6)
1201 static int br_ip6_multicast_mld2_report(struct net_bridge *br,
1202 struct net_bridge_port *port,
1203 struct sk_buff *skb,
1204 u16 vid)
1206 const unsigned char *src;
1207 struct icmp6hdr *icmp6h;
1208 struct mld2_grec *grec;
1209 int i;
1210 int len;
1211 int num;
1212 int err = 0;
1214 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
1215 return -EINVAL;
1217 icmp6h = icmp6_hdr(skb);
1218 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
1219 len = skb_transport_offset(skb) + sizeof(*icmp6h);
1221 for (i = 0; i < num; i++) {
1222 __be16 *_nsrcs, __nsrcs;
1223 u16 nsrcs;
1225 _nsrcs = skb_header_pointer(skb,
1226 len + offsetof(struct mld2_grec,
1227 grec_nsrcs),
1228 sizeof(__nsrcs), &__nsrcs);
1229 if (!_nsrcs)
1230 return -EINVAL;
1232 nsrcs = ntohs(*_nsrcs);
1234 if (!pskb_may_pull(skb,
1235 len + sizeof(*grec) +
1236 sizeof(struct in6_addr) * nsrcs))
1237 return -EINVAL;
1239 grec = (struct mld2_grec *)(skb->data + len);
1240 len += sizeof(*grec) +
1241 sizeof(struct in6_addr) * nsrcs;
1243 /* We treat these as MLDv1 reports for now. */
1244 switch (grec->grec_type) {
1245 case MLD2_MODE_IS_INCLUDE:
1246 case MLD2_MODE_IS_EXCLUDE:
1247 case MLD2_CHANGE_TO_INCLUDE:
1248 case MLD2_CHANGE_TO_EXCLUDE:
1249 case MLD2_ALLOW_NEW_SOURCES:
1250 case MLD2_BLOCK_OLD_SOURCES:
1251 break;
1253 default:
1254 continue;
1257 src = eth_hdr(skb)->h_source;
1258 if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE ||
1259 grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
1260 nsrcs == 0) {
1261 br_ip6_multicast_leave_group(br, port, &grec->grec_mca,
1262 vid, src);
1263 } else {
1264 err = br_ip6_multicast_add_group(br, port,
1265 &grec->grec_mca, vid,
1266 src);
1267 if (err)
1268 break;
1272 return err;
1274 #endif
1276 static bool br_ip4_multicast_select_querier(struct net_bridge *br,
1277 struct net_bridge_port *port,
1278 __be32 saddr)
1280 if (!timer_pending(&br->ip4_own_query.timer) &&
1281 !timer_pending(&br->ip4_other_query.timer))
1282 goto update;
1284 if (!br->ip4_querier.addr.u.ip4)
1285 goto update;
1287 if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4))
1288 goto update;
1290 return false;
1292 update:
1293 br->ip4_querier.addr.u.ip4 = saddr;
1295 /* update protected by general multicast_lock by caller */
1296 rcu_assign_pointer(br->ip4_querier.port, port);
1298 return true;
1301 #if IS_ENABLED(CONFIG_IPV6)
1302 static bool br_ip6_multicast_select_querier(struct net_bridge *br,
1303 struct net_bridge_port *port,
1304 struct in6_addr *saddr)
1306 if (!timer_pending(&br->ip6_own_query.timer) &&
1307 !timer_pending(&br->ip6_other_query.timer))
1308 goto update;
1310 if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0)
1311 goto update;
1313 return false;
1315 update:
1316 br->ip6_querier.addr.u.ip6 = *saddr;
1318 /* update protected by general multicast_lock by caller */
1319 rcu_assign_pointer(br->ip6_querier.port, port);
1321 return true;
1323 #endif
1325 static bool br_multicast_select_querier(struct net_bridge *br,
1326 struct net_bridge_port *port,
1327 struct br_ip *saddr)
1329 switch (saddr->proto) {
1330 case htons(ETH_P_IP):
1331 return br_ip4_multicast_select_querier(br, port, saddr->u.ip4);
1332 #if IS_ENABLED(CONFIG_IPV6)
1333 case htons(ETH_P_IPV6):
1334 return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6);
1335 #endif
1338 return false;
1341 static void
1342 br_multicast_update_query_timer(struct net_bridge *br,
1343 struct bridge_mcast_other_query *query,
1344 unsigned long max_delay)
1346 if (!timer_pending(&query->timer))
1347 query->delay_time = jiffies + max_delay;
1349 mod_timer(&query->timer, jiffies + br->multicast_querier_interval);
1352 static void br_port_mc_router_state_change(struct net_bridge_port *p,
1353 bool is_mc_router)
1355 struct switchdev_attr attr = {
1356 .orig_dev = p->dev,
1357 .id = SWITCHDEV_ATTR_ID_PORT_MROUTER,
1358 .flags = SWITCHDEV_F_DEFER,
1359 .u.mrouter = is_mc_router,
1362 switchdev_port_attr_set(p->dev, &attr);
1366 * Add port to router_list
1367 * list is maintained ordered by pointer value
1368 * and locked by br->multicast_lock and RCU
1370 static void br_multicast_add_router(struct net_bridge *br,
1371 struct net_bridge_port *port)
1373 struct net_bridge_port *p;
1374 struct hlist_node *slot = NULL;
1376 if (!hlist_unhashed(&port->rlist))
1377 return;
1379 hlist_for_each_entry(p, &br->router_list, rlist) {
1380 if ((unsigned long) port >= (unsigned long) p)
1381 break;
1382 slot = &p->rlist;
1385 if (slot)
1386 hlist_add_behind_rcu(&port->rlist, slot);
1387 else
1388 hlist_add_head_rcu(&port->rlist, &br->router_list);
1389 br_rtr_notify(br->dev, port, RTM_NEWMDB);
1390 br_port_mc_router_state_change(port, true);
1393 static void br_multicast_mark_router(struct net_bridge *br,
1394 struct net_bridge_port *port)
1396 unsigned long now = jiffies;
1398 if (!port) {
1399 if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) {
1400 if (!timer_pending(&br->multicast_router_timer))
1401 br_mc_router_state_change(br, true);
1402 mod_timer(&br->multicast_router_timer,
1403 now + br->multicast_querier_interval);
1405 return;
1408 if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
1409 port->multicast_router == MDB_RTR_TYPE_PERM)
1410 return;
1412 br_multicast_add_router(br, port);
1414 mod_timer(&port->multicast_router_timer,
1415 now + br->multicast_querier_interval);
1418 static void br_multicast_query_received(struct net_bridge *br,
1419 struct net_bridge_port *port,
1420 struct bridge_mcast_other_query *query,
1421 struct br_ip *saddr,
1422 unsigned long max_delay)
1424 if (!br_multicast_select_querier(br, port, saddr))
1425 return;
1427 br_multicast_update_query_timer(br, query, max_delay);
1428 br_multicast_mark_router(br, port);
1431 static void br_ip4_multicast_query(struct net_bridge *br,
1432 struct net_bridge_port *port,
1433 struct sk_buff *skb,
1434 u16 vid)
1436 const struct iphdr *iph = ip_hdr(skb);
1437 struct igmphdr *ih = igmp_hdr(skb);
1438 struct net_bridge_mdb_entry *mp;
1439 struct igmpv3_query *ih3;
1440 struct net_bridge_port_group *p;
1441 struct net_bridge_port_group __rcu **pp;
1442 struct br_ip saddr;
1443 unsigned long max_delay;
1444 unsigned long now = jiffies;
1445 unsigned int offset = skb_transport_offset(skb);
1446 __be32 group;
1448 spin_lock(&br->multicast_lock);
1449 if (!netif_running(br->dev) ||
1450 (port && port->state == BR_STATE_DISABLED))
1451 goto out;
1453 group = ih->group;
1455 if (skb->len == offset + sizeof(*ih)) {
1456 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1458 if (!max_delay) {
1459 max_delay = 10 * HZ;
1460 group = 0;
1462 } else if (skb->len >= offset + sizeof(*ih3)) {
1463 ih3 = igmpv3_query_hdr(skb);
1464 if (ih3->nsrcs)
1465 goto out;
1467 max_delay = ih3->code ?
1468 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
1469 } else {
1470 goto out;
1473 if (!group) {
1474 saddr.proto = htons(ETH_P_IP);
1475 saddr.u.ip4 = iph->saddr;
1477 br_multicast_query_received(br, port, &br->ip4_other_query,
1478 &saddr, max_delay);
1479 goto out;
1482 mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid);
1483 if (!mp)
1484 goto out;
1486 max_delay *= br->multicast_last_member_count;
1488 if (mp->host_joined &&
1489 (timer_pending(&mp->timer) ?
1490 time_after(mp->timer.expires, now + max_delay) :
1491 try_to_del_timer_sync(&mp->timer) >= 0))
1492 mod_timer(&mp->timer, now + max_delay);
1494 for (pp = &mp->ports;
1495 (p = mlock_dereference(*pp, br)) != NULL;
1496 pp = &p->next) {
1497 if (timer_pending(&p->timer) ?
1498 time_after(p->timer.expires, now + max_delay) :
1499 try_to_del_timer_sync(&p->timer) >= 0)
1500 mod_timer(&p->timer, now + max_delay);
1503 out:
1504 spin_unlock(&br->multicast_lock);
1507 #if IS_ENABLED(CONFIG_IPV6)
1508 static int br_ip6_multicast_query(struct net_bridge *br,
1509 struct net_bridge_port *port,
1510 struct sk_buff *skb,
1511 u16 vid)
1513 struct mld_msg *mld;
1514 struct net_bridge_mdb_entry *mp;
1515 struct mld2_query *mld2q;
1516 struct net_bridge_port_group *p;
1517 struct net_bridge_port_group __rcu **pp;
1518 struct br_ip saddr;
1519 unsigned long max_delay;
1520 unsigned long now = jiffies;
1521 unsigned int offset = skb_transport_offset(skb);
1522 const struct in6_addr *group = NULL;
1523 bool is_general_query;
1524 int err = 0;
1526 spin_lock(&br->multicast_lock);
1527 if (!netif_running(br->dev) ||
1528 (port && port->state == BR_STATE_DISABLED))
1529 goto out;
1531 if (skb->len == offset + sizeof(*mld)) {
1532 if (!pskb_may_pull(skb, offset + sizeof(*mld))) {
1533 err = -EINVAL;
1534 goto out;
1536 mld = (struct mld_msg *) icmp6_hdr(skb);
1537 max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
1538 if (max_delay)
1539 group = &mld->mld_mca;
1540 } else {
1541 if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) {
1542 err = -EINVAL;
1543 goto out;
1545 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1546 if (!mld2q->mld2q_nsrcs)
1547 group = &mld2q->mld2q_mca;
1549 max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
1552 is_general_query = group && ipv6_addr_any(group);
1554 if (is_general_query) {
1555 saddr.proto = htons(ETH_P_IPV6);
1556 saddr.u.ip6 = ipv6_hdr(skb)->saddr;
1558 br_multicast_query_received(br, port, &br->ip6_other_query,
1559 &saddr, max_delay);
1560 goto out;
1561 } else if (!group) {
1562 goto out;
1565 mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid);
1566 if (!mp)
1567 goto out;
1569 max_delay *= br->multicast_last_member_count;
1570 if (mp->host_joined &&
1571 (timer_pending(&mp->timer) ?
1572 time_after(mp->timer.expires, now + max_delay) :
1573 try_to_del_timer_sync(&mp->timer) >= 0))
1574 mod_timer(&mp->timer, now + max_delay);
1576 for (pp = &mp->ports;
1577 (p = mlock_dereference(*pp, br)) != NULL;
1578 pp = &p->next) {
1579 if (timer_pending(&p->timer) ?
1580 time_after(p->timer.expires, now + max_delay) :
1581 try_to_del_timer_sync(&p->timer) >= 0)
1582 mod_timer(&p->timer, now + max_delay);
1585 out:
1586 spin_unlock(&br->multicast_lock);
1587 return err;
1589 #endif
1591 static void
1592 br_multicast_leave_group(struct net_bridge *br,
1593 struct net_bridge_port *port,
1594 struct br_ip *group,
1595 struct bridge_mcast_other_query *other_query,
1596 struct bridge_mcast_own_query *own_query,
1597 const unsigned char *src)
1599 struct net_bridge_mdb_htable *mdb;
1600 struct net_bridge_mdb_entry *mp;
1601 struct net_bridge_port_group *p;
1602 unsigned long now;
1603 unsigned long time;
1605 spin_lock(&br->multicast_lock);
1606 if (!netif_running(br->dev) ||
1607 (port && port->state == BR_STATE_DISABLED))
1608 goto out;
1610 mdb = mlock_dereference(br->mdb, br);
1611 mp = br_mdb_ip_get(mdb, group);
1612 if (!mp)
1613 goto out;
1615 if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
1616 struct net_bridge_port_group __rcu **pp;
1618 for (pp = &mp->ports;
1619 (p = mlock_dereference(*pp, br)) != NULL;
1620 pp = &p->next) {
1621 if (!br_port_group_equal(p, port, src))
1622 continue;
1624 if (p->flags & MDB_PG_FLAGS_PERMANENT)
1625 break;
1627 rcu_assign_pointer(*pp, p->next);
1628 hlist_del_init(&p->mglist);
1629 del_timer(&p->timer);
1630 call_rcu_bh(&p->rcu, br_multicast_free_pg);
1631 br_mdb_notify(br->dev, port, group, RTM_DELMDB,
1632 p->flags);
1634 if (!mp->ports && !mp->host_joined &&
1635 netif_running(br->dev))
1636 mod_timer(&mp->timer, jiffies);
1638 goto out;
1641 if (timer_pending(&other_query->timer))
1642 goto out;
1644 if (br->multicast_querier) {
1645 __br_multicast_send_query(br, port, &mp->addr);
1647 time = jiffies + br->multicast_last_member_count *
1648 br->multicast_last_member_interval;
1650 mod_timer(&own_query->timer, time);
1652 for (p = mlock_dereference(mp->ports, br);
1653 p != NULL;
1654 p = mlock_dereference(p->next, br)) {
1655 if (!br_port_group_equal(p, port, src))
1656 continue;
1658 if (!hlist_unhashed(&p->mglist) &&
1659 (timer_pending(&p->timer) ?
1660 time_after(p->timer.expires, time) :
1661 try_to_del_timer_sync(&p->timer) >= 0)) {
1662 mod_timer(&p->timer, time);
1665 break;
1669 now = jiffies;
1670 time = now + br->multicast_last_member_count *
1671 br->multicast_last_member_interval;
1673 if (!port) {
1674 if (mp->host_joined &&
1675 (timer_pending(&mp->timer) ?
1676 time_after(mp->timer.expires, time) :
1677 try_to_del_timer_sync(&mp->timer) >= 0)) {
1678 mod_timer(&mp->timer, time);
1681 goto out;
1684 for (p = mlock_dereference(mp->ports, br);
1685 p != NULL;
1686 p = mlock_dereference(p->next, br)) {
1687 if (p->port != port)
1688 continue;
1690 if (!hlist_unhashed(&p->mglist) &&
1691 (timer_pending(&p->timer) ?
1692 time_after(p->timer.expires, time) :
1693 try_to_del_timer_sync(&p->timer) >= 0)) {
1694 mod_timer(&p->timer, time);
1697 break;
1699 out:
1700 spin_unlock(&br->multicast_lock);
1703 static void br_ip4_multicast_leave_group(struct net_bridge *br,
1704 struct net_bridge_port *port,
1705 __be32 group,
1706 __u16 vid,
1707 const unsigned char *src)
1709 struct br_ip br_group;
1710 struct bridge_mcast_own_query *own_query;
1712 if (ipv4_is_local_multicast(group))
1713 return;
1715 own_query = port ? &port->ip4_own_query : &br->ip4_own_query;
1717 br_group.u.ip4 = group;
1718 br_group.proto = htons(ETH_P_IP);
1719 br_group.vid = vid;
1721 br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
1722 own_query, src);
1725 #if IS_ENABLED(CONFIG_IPV6)
1726 static void br_ip6_multicast_leave_group(struct net_bridge *br,
1727 struct net_bridge_port *port,
1728 const struct in6_addr *group,
1729 __u16 vid,
1730 const unsigned char *src)
1732 struct br_ip br_group;
1733 struct bridge_mcast_own_query *own_query;
1735 if (ipv6_addr_is_ll_all_nodes(group))
1736 return;
1738 own_query = port ? &port->ip6_own_query : &br->ip6_own_query;
1740 br_group.u.ip6 = *group;
1741 br_group.proto = htons(ETH_P_IPV6);
1742 br_group.vid = vid;
1744 br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
1745 own_query, src);
1747 #endif
1749 static void br_multicast_err_count(const struct net_bridge *br,
1750 const struct net_bridge_port *p,
1751 __be16 proto)
1753 struct bridge_mcast_stats __percpu *stats;
1754 struct bridge_mcast_stats *pstats;
1756 if (!br->multicast_stats_enabled)
1757 return;
1759 if (p)
1760 stats = p->mcast_stats;
1761 else
1762 stats = br->mcast_stats;
1763 if (WARN_ON(!stats))
1764 return;
1766 pstats = this_cpu_ptr(stats);
1768 u64_stats_update_begin(&pstats->syncp);
1769 switch (proto) {
1770 case htons(ETH_P_IP):
1771 pstats->mstats.igmp_parse_errors++;
1772 break;
1773 #if IS_ENABLED(CONFIG_IPV6)
1774 case htons(ETH_P_IPV6):
1775 pstats->mstats.mld_parse_errors++;
1776 break;
1777 #endif
1779 u64_stats_update_end(&pstats->syncp);
1782 static void br_multicast_pim(struct net_bridge *br,
1783 struct net_bridge_port *port,
1784 const struct sk_buff *skb)
1786 unsigned int offset = skb_transport_offset(skb);
1787 struct pimhdr *pimhdr, _pimhdr;
1789 pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr);
1790 if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION ||
1791 pim_hdr_type(pimhdr) != PIM_TYPE_HELLO)
1792 return;
1794 br_multicast_mark_router(br, port);
1797 static int br_multicast_ipv4_rcv(struct net_bridge *br,
1798 struct net_bridge_port *port,
1799 struct sk_buff *skb,
1800 u16 vid)
1802 struct sk_buff *skb_trimmed = NULL;
1803 const unsigned char *src;
1804 struct igmphdr *ih;
1805 int err;
1807 err = ip_mc_check_igmp(skb, &skb_trimmed);
1809 if (err == -ENOMSG) {
1810 if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) {
1811 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1812 } else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) {
1813 if (ip_hdr(skb)->protocol == IPPROTO_PIM)
1814 br_multicast_pim(br, port, skb);
1816 return 0;
1817 } else if (err < 0) {
1818 br_multicast_err_count(br, port, skb->protocol);
1819 return err;
1822 ih = igmp_hdr(skb);
1823 src = eth_hdr(skb)->h_source;
1824 BR_INPUT_SKB_CB(skb)->igmp = ih->type;
1826 switch (ih->type) {
1827 case IGMP_HOST_MEMBERSHIP_REPORT:
1828 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1829 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1830 err = br_ip4_multicast_add_group(br, port, ih->group, vid, src);
1831 break;
1832 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1833 err = br_ip4_multicast_igmp3_report(br, port, skb_trimmed, vid);
1834 break;
1835 case IGMP_HOST_MEMBERSHIP_QUERY:
1836 br_ip4_multicast_query(br, port, skb_trimmed, vid);
1837 break;
1838 case IGMP_HOST_LEAVE_MESSAGE:
1839 br_ip4_multicast_leave_group(br, port, ih->group, vid, src);
1840 break;
1843 if (skb_trimmed && skb_trimmed != skb)
1844 kfree_skb(skb_trimmed);
1846 br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
1847 BR_MCAST_DIR_RX);
1849 return err;
1852 #if IS_ENABLED(CONFIG_IPV6)
1853 static int br_multicast_ipv6_rcv(struct net_bridge *br,
1854 struct net_bridge_port *port,
1855 struct sk_buff *skb,
1856 u16 vid)
1858 struct sk_buff *skb_trimmed = NULL;
1859 const unsigned char *src;
1860 struct mld_msg *mld;
1861 int err;
1863 err = ipv6_mc_check_mld(skb, &skb_trimmed);
1865 if (err == -ENOMSG) {
1866 if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
1867 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1868 return 0;
1869 } else if (err < 0) {
1870 br_multicast_err_count(br, port, skb->protocol);
1871 return err;
1874 mld = (struct mld_msg *)skb_transport_header(skb);
1875 BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type;
1877 switch (mld->mld_type) {
1878 case ICMPV6_MGM_REPORT:
1879 src = eth_hdr(skb)->h_source;
1880 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1881 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid,
1882 src);
1883 break;
1884 case ICMPV6_MLD2_REPORT:
1885 err = br_ip6_multicast_mld2_report(br, port, skb_trimmed, vid);
1886 break;
1887 case ICMPV6_MGM_QUERY:
1888 err = br_ip6_multicast_query(br, port, skb_trimmed, vid);
1889 break;
1890 case ICMPV6_MGM_REDUCTION:
1891 src = eth_hdr(skb)->h_source;
1892 br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src);
1893 break;
1896 if (skb_trimmed && skb_trimmed != skb)
1897 kfree_skb(skb_trimmed);
1899 br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
1900 BR_MCAST_DIR_RX);
1902 return err;
1904 #endif
1906 int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1907 struct sk_buff *skb, u16 vid)
1909 int ret = 0;
1911 BR_INPUT_SKB_CB(skb)->igmp = 0;
1912 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1914 if (br->multicast_disabled)
1915 return 0;
1917 switch (skb->protocol) {
1918 case htons(ETH_P_IP):
1919 ret = br_multicast_ipv4_rcv(br, port, skb, vid);
1920 break;
1921 #if IS_ENABLED(CONFIG_IPV6)
1922 case htons(ETH_P_IPV6):
1923 ret = br_multicast_ipv6_rcv(br, port, skb, vid);
1924 break;
1925 #endif
1928 return ret;
1931 static void br_multicast_query_expired(struct net_bridge *br,
1932 struct bridge_mcast_own_query *query,
1933 struct bridge_mcast_querier *querier)
1935 spin_lock(&br->multicast_lock);
1936 if (query->startup_sent < br->multicast_startup_query_count)
1937 query->startup_sent++;
1939 RCU_INIT_POINTER(querier->port, NULL);
1940 br_multicast_send_query(br, NULL, query);
1941 spin_unlock(&br->multicast_lock);
1944 static void br_ip4_multicast_query_expired(struct timer_list *t)
1946 struct net_bridge *br = from_timer(br, t, ip4_own_query.timer);
1948 br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier);
1951 #if IS_ENABLED(CONFIG_IPV6)
1952 static void br_ip6_multicast_query_expired(struct timer_list *t)
1954 struct net_bridge *br = from_timer(br, t, ip6_own_query.timer);
1956 br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier);
1958 #endif
1960 void br_multicast_init(struct net_bridge *br)
1962 br->hash_elasticity = 4;
1963 br->hash_max = 512;
1965 br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
1966 br->multicast_querier = 0;
1967 br->multicast_query_use_ifaddr = 0;
1968 br->multicast_last_member_count = 2;
1969 br->multicast_startup_query_count = 2;
1971 br->multicast_last_member_interval = HZ;
1972 br->multicast_query_response_interval = 10 * HZ;
1973 br->multicast_startup_query_interval = 125 * HZ / 4;
1974 br->multicast_query_interval = 125 * HZ;
1975 br->multicast_querier_interval = 255 * HZ;
1976 br->multicast_membership_interval = 260 * HZ;
1978 br->ip4_other_query.delay_time = 0;
1979 br->ip4_querier.port = NULL;
1980 br->multicast_igmp_version = 2;
1981 #if IS_ENABLED(CONFIG_IPV6)
1982 br->multicast_mld_version = 1;
1983 br->ip6_other_query.delay_time = 0;
1984 br->ip6_querier.port = NULL;
1985 #endif
1986 br->has_ipv6_addr = 1;
1988 spin_lock_init(&br->multicast_lock);
1989 timer_setup(&br->multicast_router_timer,
1990 br_multicast_local_router_expired, 0);
1991 timer_setup(&br->ip4_other_query.timer,
1992 br_ip4_multicast_querier_expired, 0);
1993 timer_setup(&br->ip4_own_query.timer,
1994 br_ip4_multicast_query_expired, 0);
1995 #if IS_ENABLED(CONFIG_IPV6)
1996 timer_setup(&br->ip6_other_query.timer,
1997 br_ip6_multicast_querier_expired, 0);
1998 timer_setup(&br->ip6_own_query.timer,
1999 br_ip6_multicast_query_expired, 0);
2000 #endif
2003 static void __br_multicast_open(struct net_bridge *br,
2004 struct bridge_mcast_own_query *query)
2006 query->startup_sent = 0;
2008 if (br->multicast_disabled)
2009 return;
2011 mod_timer(&query->timer, jiffies);
2014 void br_multicast_open(struct net_bridge *br)
2016 __br_multicast_open(br, &br->ip4_own_query);
2017 #if IS_ENABLED(CONFIG_IPV6)
2018 __br_multicast_open(br, &br->ip6_own_query);
2019 #endif
2022 void br_multicast_stop(struct net_bridge *br)
2024 del_timer_sync(&br->multicast_router_timer);
2025 del_timer_sync(&br->ip4_other_query.timer);
2026 del_timer_sync(&br->ip4_own_query.timer);
2027 #if IS_ENABLED(CONFIG_IPV6)
2028 del_timer_sync(&br->ip6_other_query.timer);
2029 del_timer_sync(&br->ip6_own_query.timer);
2030 #endif
2033 void br_multicast_dev_del(struct net_bridge *br)
2035 struct net_bridge_mdb_htable *mdb;
2036 struct net_bridge_mdb_entry *mp;
2037 struct hlist_node *n;
2038 u32 ver;
2039 int i;
2041 spin_lock_bh(&br->multicast_lock);
2042 mdb = mlock_dereference(br->mdb, br);
2043 if (!mdb)
2044 goto out;
2046 br->mdb = NULL;
2048 ver = mdb->ver;
2049 for (i = 0; i < mdb->max; i++) {
2050 hlist_for_each_entry_safe(mp, n, &mdb->mhash[i],
2051 hlist[ver]) {
2052 del_timer(&mp->timer);
2053 call_rcu_bh(&mp->rcu, br_multicast_free_group);
2057 if (mdb->old) {
2058 spin_unlock_bh(&br->multicast_lock);
2059 rcu_barrier_bh();
2060 spin_lock_bh(&br->multicast_lock);
2061 WARN_ON(mdb->old);
2064 mdb->old = mdb;
2065 call_rcu_bh(&mdb->rcu, br_mdb_free);
2067 out:
2068 spin_unlock_bh(&br->multicast_lock);
2071 int br_multicast_set_router(struct net_bridge *br, unsigned long val)
2073 int err = -EINVAL;
2075 spin_lock_bh(&br->multicast_lock);
2077 switch (val) {
2078 case MDB_RTR_TYPE_DISABLED:
2079 case MDB_RTR_TYPE_PERM:
2080 br_mc_router_state_change(br, val == MDB_RTR_TYPE_PERM);
2081 del_timer(&br->multicast_router_timer);
2082 br->multicast_router = val;
2083 err = 0;
2084 break;
2085 case MDB_RTR_TYPE_TEMP_QUERY:
2086 if (br->multicast_router != MDB_RTR_TYPE_TEMP_QUERY)
2087 br_mc_router_state_change(br, false);
2088 br->multicast_router = val;
2089 err = 0;
2090 break;
2093 spin_unlock_bh(&br->multicast_lock);
2095 return err;
2098 static void __del_port_router(struct net_bridge_port *p)
2100 if (hlist_unhashed(&p->rlist))
2101 return;
2102 hlist_del_init_rcu(&p->rlist);
2103 br_rtr_notify(p->br->dev, p, RTM_DELMDB);
2104 br_port_mc_router_state_change(p, false);
2106 /* don't allow timer refresh */
2107 if (p->multicast_router == MDB_RTR_TYPE_TEMP)
2108 p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
2111 int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
2113 struct net_bridge *br = p->br;
2114 unsigned long now = jiffies;
2115 int err = -EINVAL;
2117 spin_lock(&br->multicast_lock);
2118 if (p->multicast_router == val) {
2119 /* Refresh the temp router port timer */
2120 if (p->multicast_router == MDB_RTR_TYPE_TEMP)
2121 mod_timer(&p->multicast_router_timer,
2122 now + br->multicast_querier_interval);
2123 err = 0;
2124 goto unlock;
2126 switch (val) {
2127 case MDB_RTR_TYPE_DISABLED:
2128 p->multicast_router = MDB_RTR_TYPE_DISABLED;
2129 __del_port_router(p);
2130 del_timer(&p->multicast_router_timer);
2131 break;
2132 case MDB_RTR_TYPE_TEMP_QUERY:
2133 p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
2134 __del_port_router(p);
2135 break;
2136 case MDB_RTR_TYPE_PERM:
2137 p->multicast_router = MDB_RTR_TYPE_PERM;
2138 del_timer(&p->multicast_router_timer);
2139 br_multicast_add_router(br, p);
2140 break;
2141 case MDB_RTR_TYPE_TEMP:
2142 p->multicast_router = MDB_RTR_TYPE_TEMP;
2143 br_multicast_mark_router(br, p);
2144 break;
2145 default:
2146 goto unlock;
2148 err = 0;
2149 unlock:
2150 spin_unlock(&br->multicast_lock);
2152 return err;
2155 static void br_multicast_start_querier(struct net_bridge *br,
2156 struct bridge_mcast_own_query *query)
2158 struct net_bridge_port *port;
2160 __br_multicast_open(br, query);
2162 rcu_read_lock();
2163 list_for_each_entry_rcu(port, &br->port_list, list) {
2164 if (port->state == BR_STATE_DISABLED ||
2165 port->state == BR_STATE_BLOCKING)
2166 continue;
2168 if (query == &br->ip4_own_query)
2169 br_multicast_enable(&port->ip4_own_query);
2170 #if IS_ENABLED(CONFIG_IPV6)
2171 else
2172 br_multicast_enable(&port->ip6_own_query);
2173 #endif
2175 rcu_read_unlock();
2178 int br_multicast_toggle(struct net_bridge *br, unsigned long val)
2180 struct net_bridge_mdb_htable *mdb;
2181 struct net_bridge_port *port;
2182 int err = 0;
2184 spin_lock_bh(&br->multicast_lock);
2185 if (br->multicast_disabled == !val)
2186 goto unlock;
2188 br_mc_disabled_update(br->dev, !val);
2189 br->multicast_disabled = !val;
2190 if (br->multicast_disabled)
2191 goto unlock;
2193 if (!netif_running(br->dev))
2194 goto unlock;
2196 mdb = mlock_dereference(br->mdb, br);
2197 if (mdb) {
2198 if (mdb->old) {
2199 err = -EEXIST;
2200 rollback:
2201 br->multicast_disabled = !!val;
2202 goto unlock;
2205 err = br_mdb_rehash(&br->mdb, mdb->max,
2206 br->hash_elasticity);
2207 if (err)
2208 goto rollback;
2211 br_multicast_open(br);
2212 list_for_each_entry(port, &br->port_list, list)
2213 __br_multicast_enable_port(port);
2215 unlock:
2216 spin_unlock_bh(&br->multicast_lock);
2218 return err;
2221 bool br_multicast_enabled(const struct net_device *dev)
2223 struct net_bridge *br = netdev_priv(dev);
2225 return !br->multicast_disabled;
2227 EXPORT_SYMBOL_GPL(br_multicast_enabled);
2229 bool br_multicast_router(const struct net_device *dev)
2231 struct net_bridge *br = netdev_priv(dev);
2232 bool is_router;
2234 spin_lock_bh(&br->multicast_lock);
2235 is_router = br_multicast_is_router(br);
2236 spin_unlock_bh(&br->multicast_lock);
2237 return is_router;
2239 EXPORT_SYMBOL_GPL(br_multicast_router);
2241 int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
2243 unsigned long max_delay;
2245 val = !!val;
2247 spin_lock_bh(&br->multicast_lock);
2248 if (br->multicast_querier == val)
2249 goto unlock;
2251 br->multicast_querier = val;
2252 if (!val)
2253 goto unlock;
2255 max_delay = br->multicast_query_response_interval;
2257 if (!timer_pending(&br->ip4_other_query.timer))
2258 br->ip4_other_query.delay_time = jiffies + max_delay;
2260 br_multicast_start_querier(br, &br->ip4_own_query);
2262 #if IS_ENABLED(CONFIG_IPV6)
2263 if (!timer_pending(&br->ip6_other_query.timer))
2264 br->ip6_other_query.delay_time = jiffies + max_delay;
2266 br_multicast_start_querier(br, &br->ip6_own_query);
2267 #endif
2269 unlock:
2270 spin_unlock_bh(&br->multicast_lock);
2272 return 0;
2275 int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
2277 int err = -EINVAL;
2278 u32 old;
2279 struct net_bridge_mdb_htable *mdb;
2281 spin_lock_bh(&br->multicast_lock);
2282 if (!is_power_of_2(val))
2283 goto unlock;
2285 mdb = mlock_dereference(br->mdb, br);
2286 if (mdb && val < mdb->size)
2287 goto unlock;
2289 err = 0;
2291 old = br->hash_max;
2292 br->hash_max = val;
2294 if (mdb) {
2295 if (mdb->old) {
2296 err = -EEXIST;
2297 rollback:
2298 br->hash_max = old;
2299 goto unlock;
2302 err = br_mdb_rehash(&br->mdb, br->hash_max,
2303 br->hash_elasticity);
2304 if (err)
2305 goto rollback;
2308 unlock:
2309 spin_unlock_bh(&br->multicast_lock);
2311 return err;
2314 int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val)
2316 /* Currently we support only version 2 and 3 */
2317 switch (val) {
2318 case 2:
2319 case 3:
2320 break;
2321 default:
2322 return -EINVAL;
2325 spin_lock_bh(&br->multicast_lock);
2326 br->multicast_igmp_version = val;
2327 spin_unlock_bh(&br->multicast_lock);
2329 return 0;
2332 #if IS_ENABLED(CONFIG_IPV6)
2333 int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val)
2335 /* Currently we support version 1 and 2 */
2336 switch (val) {
2337 case 1:
2338 case 2:
2339 break;
2340 default:
2341 return -EINVAL;
2344 spin_lock_bh(&br->multicast_lock);
2345 br->multicast_mld_version = val;
2346 spin_unlock_bh(&br->multicast_lock);
2348 return 0;
2350 #endif
2353 * br_multicast_list_adjacent - Returns snooped multicast addresses
2354 * @dev: The bridge port adjacent to which to retrieve addresses
2355 * @br_ip_list: The list to store found, snooped multicast IP addresses in
2357 * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
2358 * snooping feature on all bridge ports of dev's bridge device, excluding
2359 * the addresses from dev itself.
2361 * Returns the number of items added to br_ip_list.
2363 * Notes:
2364 * - br_ip_list needs to be initialized by caller
2365 * - br_ip_list might contain duplicates in the end
2366 * (needs to be taken care of by caller)
2367 * - br_ip_list needs to be freed by caller
2369 int br_multicast_list_adjacent(struct net_device *dev,
2370 struct list_head *br_ip_list)
2372 struct net_bridge *br;
2373 struct net_bridge_port *port;
2374 struct net_bridge_port_group *group;
2375 struct br_ip_list *entry;
2376 int count = 0;
2378 rcu_read_lock();
2379 if (!br_ip_list || !br_port_exists(dev))
2380 goto unlock;
2382 port = br_port_get_rcu(dev);
2383 if (!port || !port->br)
2384 goto unlock;
2386 br = port->br;
2388 list_for_each_entry_rcu(port, &br->port_list, list) {
2389 if (!port->dev || port->dev == dev)
2390 continue;
2392 hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
2393 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
2394 if (!entry)
2395 goto unlock;
2397 entry->addr = group->addr;
2398 list_add(&entry->list, br_ip_list);
2399 count++;
2403 unlock:
2404 rcu_read_unlock();
2405 return count;
2407 EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
2410 * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2411 * @dev: The bridge port providing the bridge on which to check for a querier
2412 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2414 * Checks whether the given interface has a bridge on top and if so returns
2415 * true if a valid querier exists anywhere on the bridged link layer.
2416 * Otherwise returns false.
2418 bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
2420 struct net_bridge *br;
2421 struct net_bridge_port *port;
2422 struct ethhdr eth;
2423 bool ret = false;
2425 rcu_read_lock();
2426 if (!br_port_exists(dev))
2427 goto unlock;
2429 port = br_port_get_rcu(dev);
2430 if (!port || !port->br)
2431 goto unlock;
2433 br = port->br;
2435 memset(&eth, 0, sizeof(eth));
2436 eth.h_proto = htons(proto);
2438 ret = br_multicast_querier_exists(br, &eth);
2440 unlock:
2441 rcu_read_unlock();
2442 return ret;
2444 EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
2447 * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
2448 * @dev: The bridge port adjacent to which to check for a querier
2449 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2451 * Checks whether the given interface has a bridge on top and if so returns
2452 * true if a selected querier is behind one of the other ports of this
2453 * bridge. Otherwise returns false.
2455 bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
2457 struct net_bridge *br;
2458 struct net_bridge_port *port;
2459 bool ret = false;
2461 rcu_read_lock();
2462 if (!br_port_exists(dev))
2463 goto unlock;
2465 port = br_port_get_rcu(dev);
2466 if (!port || !port->br)
2467 goto unlock;
2469 br = port->br;
2471 switch (proto) {
2472 case ETH_P_IP:
2473 if (!timer_pending(&br->ip4_other_query.timer) ||
2474 rcu_dereference(br->ip4_querier.port) == port)
2475 goto unlock;
2476 break;
2477 #if IS_ENABLED(CONFIG_IPV6)
2478 case ETH_P_IPV6:
2479 if (!timer_pending(&br->ip6_other_query.timer) ||
2480 rcu_dereference(br->ip6_querier.port) == port)
2481 goto unlock;
2482 break;
2483 #endif
2484 default:
2485 goto unlock;
2488 ret = true;
2489 unlock:
2490 rcu_read_unlock();
2491 return ret;
2493 EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
2495 static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
2496 const struct sk_buff *skb, u8 type, u8 dir)
2498 struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
2499 __be16 proto = skb->protocol;
2500 unsigned int t_len;
2502 u64_stats_update_begin(&pstats->syncp);
2503 switch (proto) {
2504 case htons(ETH_P_IP):
2505 t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
2506 switch (type) {
2507 case IGMP_HOST_MEMBERSHIP_REPORT:
2508 pstats->mstats.igmp_v1reports[dir]++;
2509 break;
2510 case IGMPV2_HOST_MEMBERSHIP_REPORT:
2511 pstats->mstats.igmp_v2reports[dir]++;
2512 break;
2513 case IGMPV3_HOST_MEMBERSHIP_REPORT:
2514 pstats->mstats.igmp_v3reports[dir]++;
2515 break;
2516 case IGMP_HOST_MEMBERSHIP_QUERY:
2517 if (t_len != sizeof(struct igmphdr)) {
2518 pstats->mstats.igmp_v3queries[dir]++;
2519 } else {
2520 unsigned int offset = skb_transport_offset(skb);
2521 struct igmphdr *ih, _ihdr;
2523 ih = skb_header_pointer(skb, offset,
2524 sizeof(_ihdr), &_ihdr);
2525 if (!ih)
2526 break;
2527 if (!ih->code)
2528 pstats->mstats.igmp_v1queries[dir]++;
2529 else
2530 pstats->mstats.igmp_v2queries[dir]++;
2532 break;
2533 case IGMP_HOST_LEAVE_MESSAGE:
2534 pstats->mstats.igmp_leaves[dir]++;
2535 break;
2537 break;
2538 #if IS_ENABLED(CONFIG_IPV6)
2539 case htons(ETH_P_IPV6):
2540 t_len = ntohs(ipv6_hdr(skb)->payload_len) +
2541 sizeof(struct ipv6hdr);
2542 t_len -= skb_network_header_len(skb);
2543 switch (type) {
2544 case ICMPV6_MGM_REPORT:
2545 pstats->mstats.mld_v1reports[dir]++;
2546 break;
2547 case ICMPV6_MLD2_REPORT:
2548 pstats->mstats.mld_v2reports[dir]++;
2549 break;
2550 case ICMPV6_MGM_QUERY:
2551 if (t_len != sizeof(struct mld_msg))
2552 pstats->mstats.mld_v2queries[dir]++;
2553 else
2554 pstats->mstats.mld_v1queries[dir]++;
2555 break;
2556 case ICMPV6_MGM_REDUCTION:
2557 pstats->mstats.mld_leaves[dir]++;
2558 break;
2560 break;
2561 #endif /* CONFIG_IPV6 */
2563 u64_stats_update_end(&pstats->syncp);
2566 void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
2567 const struct sk_buff *skb, u8 type, u8 dir)
2569 struct bridge_mcast_stats __percpu *stats;
2571 /* if multicast_disabled is true then igmp type can't be set */
2572 if (!type || !br->multicast_stats_enabled)
2573 return;
2575 if (p)
2576 stats = p->mcast_stats;
2577 else
2578 stats = br->mcast_stats;
2579 if (WARN_ON(!stats))
2580 return;
2582 br_mcast_stats_add(stats, skb, type, dir);
2585 int br_multicast_init_stats(struct net_bridge *br)
2587 br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
2588 if (!br->mcast_stats)
2589 return -ENOMEM;
2591 return 0;
2594 void br_multicast_uninit_stats(struct net_bridge *br)
2596 free_percpu(br->mcast_stats);
2599 static void mcast_stats_add_dir(u64 *dst, u64 *src)
2601 dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
2602 dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
2605 void br_multicast_get_stats(const struct net_bridge *br,
2606 const struct net_bridge_port *p,
2607 struct br_mcast_stats *dest)
2609 struct bridge_mcast_stats __percpu *stats;
2610 struct br_mcast_stats tdst;
2611 int i;
2613 memset(dest, 0, sizeof(*dest));
2614 if (p)
2615 stats = p->mcast_stats;
2616 else
2617 stats = br->mcast_stats;
2618 if (WARN_ON(!stats))
2619 return;
2621 memset(&tdst, 0, sizeof(tdst));
2622 for_each_possible_cpu(i) {
2623 struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
2624 struct br_mcast_stats temp;
2625 unsigned int start;
2627 do {
2628 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
2629 memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
2630 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
2632 mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
2633 mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
2634 mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
2635 mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
2636 mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
2637 mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
2638 mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
2639 tdst.igmp_parse_errors += temp.igmp_parse_errors;
2641 mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
2642 mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
2643 mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
2644 mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
2645 mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
2646 tdst.mld_parse_errors += temp.mld_parse_errors;
2648 memcpy(dest, &tdst, sizeof(*dest));