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)
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>
30 #include <net/switchdev.h>
31 #if IS_ENABLED(CONFIG_IPV6)
34 #include <net/ip6_checksum.h>
35 #include <net/addrconf.h>
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
,
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
);
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
)
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
);
76 static inline int __br_ip4_hash(struct net_bridge_mdb_htable
*mdb
, __be32 ip
,
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
,
87 return jhash_2words(ipv6_addr_hash(ip
), vid
,
88 mdb
->secret
) & (mdb
->max
- 1);
92 static inline int br_ip_hash(struct net_bridge_mdb_htable
*mdb
,
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
);
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
))
119 struct net_bridge_mdb_entry
*br_mdb_ip_get(struct net_bridge_mdb_htable
*mdb
,
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
)
134 br_dst
.proto
= htons(ETH_P_IP
);
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
,
148 br_dst
.proto
= htons(ETH_P_IPV6
);
151 return br_mdb_ip_get(mdb
, &br_dst
);
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
);
161 if (br
->multicast_disabled
)
164 if (BR_INPUT_SKB_CB(skb
)->igmp
)
167 ip
.proto
= skb
->protocol
;
170 switch (skb
->protocol
) {
171 case htons(ETH_P_IP
):
172 ip
.u
.ip4
= ip_hdr(skb
)->daddr
;
174 #if IS_ENABLED(CONFIG_IPV6)
175 case htons(ETH_P_IPV6
):
176 ip
.u
.ip6
= ipv6_hdr(skb
)->daddr
;
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
;
197 static int br_mdb_copy(struct net_bridge_mdb_htable
*new,
198 struct net_bridge_mdb_htable
*old
,
201 struct net_bridge_mdb_entry
*mp
;
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
)]);
215 for (i
= 0; i
< new->max
; i
++) {
217 hlist_for_each_entry(mp
, &new->mhash
[i
], hlist
[new->ver
])
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
);
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
);
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
))
252 mp
->host_joined
= false;
253 br_mdb_notify(br
->dev
, NULL
, &mp
->addr
, RTM_DELMDB
, 0);
258 mdb
= mlock_dereference(br
->mdb
, br
);
260 hlist_del_rcu(&mp
->hlist
[mdb
->ver
]);
263 call_rcu_bh(&mp
->rcu
, br_multicast_free_group
);
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
);
283 for (pp
= &mp
->ports
;
284 (p
= mlock_dereference(*pp
, br
)) != NULL
;
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
,
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
);
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
)
316 br_multicast_del_pg(br
, pg
);
319 spin_unlock(&br
->multicast_lock
);
322 static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu
**mdbp
, int max
,
325 struct net_bridge_mdb_htable
*old
= rcu_dereference_protected(*mdbp
, 1);
326 struct net_bridge_mdb_htable
*mdb
;
329 mdb
= kmalloc(sizeof(*mdb
), GFP_ATOMIC
);
336 mdb
->mhash
= kcalloc(max
, sizeof(*mdb
->mhash
), GFP_ATOMIC
);
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
));
348 mdb
->secret
= old
->secret
;
353 err
= br_mdb_copy(mdb
, old
, elasticity
);
361 call_rcu_bh(&mdb
->rcu
, br_mdb_free
);
364 rcu_assign_pointer(*mdbp
, mdb
);
369 static struct sk_buff
*br_ip4_multicast_alloc_query(struct net_bridge
*br
,
373 struct igmpv3_query
*ihv3
;
374 size_t igmp_hdr_size
;
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
) +
388 skb
->protocol
= htons(ETH_P_IP
);
390 skb_reset_mac_header(skb
);
393 ether_addr_copy(eth
->h_source
, br
->dev
->dev_addr
);
396 eth
->h_dest
[2] = 0x5e;
400 eth
->h_proto
= htons(ETH_P_IP
);
401 skb_put(skb
, sizeof(*eth
));
403 skb_set_network_header(skb
, skb
->len
);
409 iph
->tot_len
= htons(sizeof(*iph
) + igmp_hdr_size
+ 4);
411 iph
->frag_off
= htons(IP_DF
);
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;
424 skb_set_transport_header(skb
, skb
->len
);
425 *igmp_type
= IGMP_HOST_MEMBERSHIP_QUERY
;
427 switch (br
->multicast_igmp_version
) {
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
);
436 ih
->csum
= ip_compute_csum((void *)ih
, sizeof(*ih
));
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
);
445 ihv3
->qqic
= br
->multicast_query_interval
/ HZ
;
451 ihv3
->csum
= ip_compute_csum((void *)ihv3
, sizeof(*ihv3
));
455 skb_put(skb
, igmp_hdr_size
);
456 __skb_pull(skb
, sizeof(*eth
));
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
,
467 struct mld2_query
*mld2q
;
468 unsigned long interval
;
469 struct ipv6hdr
*ip6h
;
470 struct mld_msg
*mldq
;
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
) +
484 skb
->protocol
= htons(ETH_P_IPV6
);
486 /* Ethernet header */
487 skb_reset_mac_header(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
;
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,
506 br
->has_ipv6_addr
= 0;
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) */
520 hopopt
[6] = IPV6_TLV_PAD1
; /* Pad1 */
521 hopopt
[7] = IPV6_TLV_PAD1
; /* Pad1 */
523 skb_put(skb
, sizeof(*ip6h
) + 8);
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
) {
533 mldq
= (struct mld_msg
*)icmp6_hdr(skb
);
534 mldq
->mld_type
= ICMPV6_MGM_QUERY
;
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
,
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
,
567 skb_put(skb
, mld_hdr_size
);
569 __skb_pull(skb
, sizeof(*eth
));
576 static struct sk_buff
*br_multicast_alloc_query(struct net_bridge
*br
,
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
,
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;
603 mdb
= rcu_dereference_protected(br
->mdb
, 1);
604 hlist_for_each_entry(mp
, &mdb
->mhash
[hash
], hlist
[mdb
->ver
]) {
606 if (unlikely(br_ip_equal(group
, &mp
->addr
)))
613 if (unlikely(count
> br
->hash_elasticity
&& count
)) {
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
) {
624 if (unlikely(max
> br
->hash_max
)) {
625 br_warn(br
, "Multicast hash table maximum of %d "
626 "reached, disabling snooping: %s\n",
628 port
? port
->dev
->name
: br
->dev
->name
);
631 br
->multicast_disabled
= 1;
636 if (max
> mdb
->max
|| elasticity
) {
639 br_info(br
, "Multicast hash table "
641 port
? port
->dev
->name
: br
->dev
->name
);
646 err
= br_mdb_rehash(&br
->mdb
, max
, elasticity
);
648 br_warn(br
, "Cannot rehash multicast "
649 "hash table, disabling snooping: %s, %d, %d\n",
650 port
? port
->dev
->name
: br
->dev
->name
,
666 struct net_bridge_mdb_entry
*br_multicast_new_group(struct net_bridge
*br
,
667 struct net_bridge_port
*p
,
670 struct net_bridge_mdb_htable
*mdb
;
671 struct net_bridge_mdb_entry
*mp
;
675 mdb
= rcu_dereference_protected(br
->mdb
, 1);
677 err
= br_mdb_rehash(&br
->mdb
, BR_HASH_SIZE
, 0);
683 hash
= br_ip_hash(mdb
, group
);
684 mp
= br_multicast_get_group(br
, p
, group
, hash
);
685 switch (PTR_ERR(mp
)) {
691 mdb
= rcu_dereference_protected(br
->mdb
, 1);
692 hash
= br_ip_hash(mdb
, group
);
699 mp
= kzalloc(sizeof(*mp
), GFP_ATOMIC
);
701 return ERR_PTR(-ENOMEM
);
705 timer_setup(&mp
->timer
, br_multicast_group_expired
, 0);
707 hlist_add_head_rcu(&mp
->hlist
[mdb
->ver
], &mdb
->mhash
[hash
]);
714 struct net_bridge_port_group
*br_multicast_new_port_group(
715 struct net_bridge_port
*port
,
717 struct net_bridge_port_group __rcu
*next
,
719 const unsigned char *src
)
721 struct net_bridge_port_group
*p
;
723 p
= kzalloc(sizeof(*p
), GFP_ATOMIC
);
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);
735 memcpy(p
->eth_addr
, src
, ETH_ALEN
);
737 memset(p
->eth_addr
, 0xff, ETH_ALEN
);
742 static bool br_port_group_equal(struct net_bridge_port_group
*p
,
743 struct net_bridge_port
*port
,
744 const unsigned char *src
)
749 if (!(port
->flags
& BR_MULTICAST_TO_UNICAST
))
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
,
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
;
766 spin_lock(&br
->multicast_lock
);
767 if (!netif_running(br
->dev
) ||
768 (port
&& port
->state
== BR_STATE_DISABLED
))
771 mp
= br_multicast_new_group(br
, port
, group
);
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
);
785 for (pp
= &mp
->ports
;
786 (p
= mlock_dereference(*pp
, br
)) != NULL
;
788 if (br_port_group_equal(p
, port
, src
))
790 if ((unsigned long)p
->port
< (unsigned long)port
)
794 p
= br_multicast_new_port_group(port
, group
, *pp
, 0, src
);
797 rcu_assign_pointer(*pp
, p
);
798 br_mdb_notify(br
->dev
, port
, group
, RTM_NEWMDB
, 0);
801 mod_timer(&p
->timer
, now
+ br
->multicast_membership_interval
);
806 spin_unlock(&br
->multicast_lock
);
810 static int br_ip4_multicast_add_group(struct net_bridge
*br
,
811 struct net_bridge_port
*port
,
814 const unsigned char *src
)
816 struct br_ip br_group
;
818 if (ipv4_is_local_multicast(group
))
821 br_group
.u
.ip4
= group
;
822 br_group
.proto
= htons(ETH_P_IP
);
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
,
833 const unsigned char *src
)
835 struct br_ip br_group
;
837 if (ipv6_addr_is_ll_all_nodes(group
))
840 br_group
.u
.ip6
= *group
;
841 br_group
.proto
= htons(ETH_P_IPV6
);
844 return br_multicast_add_group(br
, port
, &br_group
, src
);
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
))
860 __del_port_router(port
);
862 spin_unlock(&br
->multicast_lock
);
865 static void br_mc_router_state_change(struct net_bridge
*p
,
868 struct switchdev_attr attr
= {
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
))
888 br_mc_router_state_change(br
, false);
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
)
900 br_multicast_start_querier(br
, query
);
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
);
922 static void br_multicast_select_own_querier(struct net_bridge
*br
,
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)
930 br
->ip6_querier
.addr
.u
.ip6
= ipv6_hdr(skb
)->saddr
;
934 static void __br_multicast_send_query(struct net_bridge
*br
,
935 struct net_bridge_port
*port
,
941 skb
= br_multicast_alloc_query(br
, ip
, &igmp_type
);
946 skb
->dev
= port
->dev
;
947 br_multicast_count(br
, port
, skb
, igmp_type
,
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
);
953 br_multicast_select_own_querier(br
, ip
, skb
);
954 br_multicast_count(br
, port
, skb
, igmp_type
,
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
;
968 if (!netif_running(br
->dev
) || br
->multicast_disabled
||
969 !br
->multicast_querier
)
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)
980 other_query
= &br
->ip6_other_query
;
981 br_group
.proto
= htons(ETH_P_IPV6
);
985 if (!other_query
|| timer_pending(&other_query
->timer
))
988 __br_multicast_send_query(br
, port
, &br_group
);
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
);
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
)
1008 if (query
->startup_sent
< br
->multicast_startup_query_count
)
1009 query
->startup_sent
++;
1011 br_multicast_send_query(port
->br
, port
, query
);
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
);
1033 static void br_mc_disabled_update(struct net_device
*dev
, bool value
)
1035 struct switchdev_attr attr
= {
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);
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
)
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
))
1097 br_multicast_enable(&port
->ip4_own_query
);
1098 #if IS_ENABLED(CONFIG_IPV6)
1099 br_multicast_enable(&port
->ip6_own_query
);
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
);
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
,
1141 const unsigned char *src
;
1142 struct igmpv3_report
*ih
;
1143 struct igmpv3_grec
*grec
;
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
))
1161 grec
= (void *)(skb
->data
+ len
- sizeof(*grec
));
1162 group
= grec
->grec_mca
;
1163 type
= grec
->grec_type
;
1164 nsrcs
= ntohs(grec
->grec_nsrcs
);
1167 if (!pskb_may_pull(skb
, len
))
1170 /* We treat this as an IGMPv2 report for now. */
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
:
1184 src
= eth_hdr(skb
)->h_source
;
1185 if ((type
== IGMPV3_CHANGE_TO_INCLUDE
||
1186 type
== IGMPV3_MODE_IS_INCLUDE
) &&
1188 br_ip4_multicast_leave_group(br
, port
, group
, vid
, src
);
1190 err
= br_ip4_multicast_add_group(br
, port
, group
, vid
,
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
,
1206 const unsigned char *src
;
1207 struct icmp6hdr
*icmp6h
;
1208 struct mld2_grec
*grec
;
1214 if (!pskb_may_pull(skb
, sizeof(*icmp6h
)))
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
;
1225 _nsrcs
= skb_header_pointer(skb
,
1226 len
+ offsetof(struct mld2_grec
,
1228 sizeof(__nsrcs
), &__nsrcs
);
1232 nsrcs
= ntohs(*_nsrcs
);
1234 if (!pskb_may_pull(skb
,
1235 len
+ sizeof(*grec
) +
1236 sizeof(struct in6_addr
) * nsrcs
))
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
:
1257 src
= eth_hdr(skb
)->h_source
;
1258 if ((grec
->grec_type
== MLD2_CHANGE_TO_INCLUDE
||
1259 grec
->grec_type
== MLD2_MODE_IS_INCLUDE
) &&
1261 br_ip6_multicast_leave_group(br
, port
, &grec
->grec_mca
,
1264 err
= br_ip6_multicast_add_group(br
, port
,
1265 &grec
->grec_mca
, vid
,
1276 static bool br_ip4_multicast_select_querier(struct net_bridge
*br
,
1277 struct net_bridge_port
*port
,
1280 if (!timer_pending(&br
->ip4_own_query
.timer
) &&
1281 !timer_pending(&br
->ip4_other_query
.timer
))
1284 if (!br
->ip4_querier
.addr
.u
.ip4
)
1287 if (ntohl(saddr
) <= ntohl(br
->ip4_querier
.addr
.u
.ip4
))
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
);
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
))
1310 if (ipv6_addr_cmp(saddr
, &br
->ip6_querier
.addr
.u
.ip6
) <= 0)
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
);
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
);
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
,
1355 struct switchdev_attr attr
= {
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
))
1379 hlist_for_each_entry(p
, &br
->router_list
, rlist
) {
1380 if ((unsigned long) port
>= (unsigned long) p
)
1386 hlist_add_behind_rcu(&port
->rlist
, slot
);
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
;
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
);
1408 if (port
->multicast_router
== MDB_RTR_TYPE_DISABLED
||
1409 port
->multicast_router
== MDB_RTR_TYPE_PERM
)
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
))
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
,
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
;
1443 unsigned long max_delay
;
1444 unsigned long now
= jiffies
;
1445 unsigned int offset
= skb_transport_offset(skb
);
1448 spin_lock(&br
->multicast_lock
);
1449 if (!netif_running(br
->dev
) ||
1450 (port
&& port
->state
== BR_STATE_DISABLED
))
1455 if (skb
->len
== offset
+ sizeof(*ih
)) {
1456 max_delay
= ih
->code
* (HZ
/ IGMP_TIMER_SCALE
);
1459 max_delay
= 10 * HZ
;
1462 } else if (skb
->len
>= offset
+ sizeof(*ih3
)) {
1463 ih3
= igmpv3_query_hdr(skb
);
1467 max_delay
= ih3
->code
?
1468 IGMPV3_MRC(ih3
->code
) * (HZ
/ IGMP_TIMER_SCALE
) : 1;
1474 saddr
.proto
= htons(ETH_P_IP
);
1475 saddr
.u
.ip4
= iph
->saddr
;
1477 br_multicast_query_received(br
, port
, &br
->ip4_other_query
,
1482 mp
= br_mdb_ip4_get(mlock_dereference(br
->mdb
, br
), group
, vid
);
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
;
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
);
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
,
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
;
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
;
1526 spin_lock(&br
->multicast_lock
);
1527 if (!netif_running(br
->dev
) ||
1528 (port
&& port
->state
== BR_STATE_DISABLED
))
1531 if (skb
->len
== offset
+ sizeof(*mld
)) {
1532 if (!pskb_may_pull(skb
, offset
+ sizeof(*mld
))) {
1536 mld
= (struct mld_msg
*) icmp6_hdr(skb
);
1537 max_delay
= msecs_to_jiffies(ntohs(mld
->mld_maxdelay
));
1539 group
= &mld
->mld_mca
;
1541 if (!pskb_may_pull(skb
, offset
+ sizeof(*mld2q
))) {
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
,
1561 } else if (!group
) {
1565 mp
= br_mdb_ip6_get(mlock_dereference(br
->mdb
, br
), group
, vid
);
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
;
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
);
1586 spin_unlock(&br
->multicast_lock
);
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
;
1605 spin_lock(&br
->multicast_lock
);
1606 if (!netif_running(br
->dev
) ||
1607 (port
&& port
->state
== BR_STATE_DISABLED
))
1610 mdb
= mlock_dereference(br
->mdb
, br
);
1611 mp
= br_mdb_ip_get(mdb
, group
);
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
;
1621 if (!br_port_group_equal(p
, port
, src
))
1624 if (p
->flags
& MDB_PG_FLAGS_PERMANENT
)
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
,
1634 if (!mp
->ports
&& !mp
->host_joined
&&
1635 netif_running(br
->dev
))
1636 mod_timer(&mp
->timer
, jiffies
);
1641 if (timer_pending(&other_query
->timer
))
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
);
1654 p
= mlock_dereference(p
->next
, br
)) {
1655 if (!br_port_group_equal(p
, port
, src
))
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
);
1670 time
= now
+ br
->multicast_last_member_count
*
1671 br
->multicast_last_member_interval
;
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
);
1684 for (p
= mlock_dereference(mp
->ports
, br
);
1686 p
= mlock_dereference(p
->next
, br
)) {
1687 if (p
->port
!= port
)
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
);
1700 spin_unlock(&br
->multicast_lock
);
1703 static void br_ip4_multicast_leave_group(struct net_bridge
*br
,
1704 struct net_bridge_port
*port
,
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
))
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
);
1721 br_multicast_leave_group(br
, port
, &br_group
, &br
->ip4_other_query
,
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
,
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
))
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
);
1744 br_multicast_leave_group(br
, port
, &br_group
, &br
->ip6_other_query
,
1749 static void br_multicast_err_count(const struct net_bridge
*br
,
1750 const struct net_bridge_port
*p
,
1753 struct bridge_mcast_stats __percpu
*stats
;
1754 struct bridge_mcast_stats
*pstats
;
1756 if (!br
->multicast_stats_enabled
)
1760 stats
= p
->mcast_stats
;
1762 stats
= br
->mcast_stats
;
1763 if (WARN_ON(!stats
))
1766 pstats
= this_cpu_ptr(stats
);
1768 u64_stats_update_begin(&pstats
->syncp
);
1770 case htons(ETH_P_IP
):
1771 pstats
->mstats
.igmp_parse_errors
++;
1773 #if IS_ENABLED(CONFIG_IPV6)
1774 case htons(ETH_P_IPV6
):
1775 pstats
->mstats
.mld_parse_errors
++;
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
)
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
,
1802 struct sk_buff
*skb_trimmed
= NULL
;
1803 const unsigned char *src
;
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
);
1817 } else if (err
< 0) {
1818 br_multicast_err_count(br
, port
, skb
->protocol
);
1823 src
= eth_hdr(skb
)->h_source
;
1824 BR_INPUT_SKB_CB(skb
)->igmp
= 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
);
1832 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1833 err
= br_ip4_multicast_igmp3_report(br
, port
, skb_trimmed
, vid
);
1835 case IGMP_HOST_MEMBERSHIP_QUERY
:
1836 br_ip4_multicast_query(br
, port
, skb_trimmed
, vid
);
1838 case IGMP_HOST_LEAVE_MESSAGE
:
1839 br_ip4_multicast_leave_group(br
, port
, ih
->group
, vid
, src
);
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
,
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
,
1858 struct sk_buff
*skb_trimmed
= NULL
;
1859 const unsigned char *src
;
1860 struct mld_msg
*mld
;
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;
1869 } else if (err
< 0) {
1870 br_multicast_err_count(br
, port
, skb
->protocol
);
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
,
1884 case ICMPV6_MLD2_REPORT
:
1885 err
= br_ip6_multicast_mld2_report(br
, port
, skb_trimmed
, vid
);
1887 case ICMPV6_MGM_QUERY
:
1888 err
= br_ip6_multicast_query(br
, port
, skb_trimmed
, vid
);
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
);
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
,
1906 int br_multicast_rcv(struct net_bridge
*br
, struct net_bridge_port
*port
,
1907 struct sk_buff
*skb
, u16 vid
)
1911 BR_INPUT_SKB_CB(skb
)->igmp
= 0;
1912 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 0;
1914 if (br
->multicast_disabled
)
1917 switch (skb
->protocol
) {
1918 case htons(ETH_P_IP
):
1919 ret
= br_multicast_ipv4_rcv(br
, port
, skb
, vid
);
1921 #if IS_ENABLED(CONFIG_IPV6)
1922 case htons(ETH_P_IPV6
):
1923 ret
= br_multicast_ipv6_rcv(br
, port
, skb
, vid
);
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
);
1960 void br_multicast_init(struct net_bridge
*br
)
1962 br
->hash_elasticity
= 4;
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
;
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);
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
)
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
);
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
);
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
;
2041 spin_lock_bh(&br
->multicast_lock
);
2042 mdb
= mlock_dereference(br
->mdb
, br
);
2049 for (i
= 0; i
< mdb
->max
; i
++) {
2050 hlist_for_each_entry_safe(mp
, n
, &mdb
->mhash
[i
],
2052 del_timer(&mp
->timer
);
2053 call_rcu_bh(&mp
->rcu
, br_multicast_free_group
);
2058 spin_unlock_bh(&br
->multicast_lock
);
2060 spin_lock_bh(&br
->multicast_lock
);
2065 call_rcu_bh(&mdb
->rcu
, br_mdb_free
);
2068 spin_unlock_bh(&br
->multicast_lock
);
2071 int br_multicast_set_router(struct net_bridge
*br
, unsigned long val
)
2075 spin_lock_bh(&br
->multicast_lock
);
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
;
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
;
2093 spin_unlock_bh(&br
->multicast_lock
);
2098 static void __del_port_router(struct net_bridge_port
*p
)
2100 if (hlist_unhashed(&p
->rlist
))
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
;
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
);
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
);
2132 case MDB_RTR_TYPE_TEMP_QUERY
:
2133 p
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
2134 __del_port_router(p
);
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
);
2141 case MDB_RTR_TYPE_TEMP
:
2142 p
->multicast_router
= MDB_RTR_TYPE_TEMP
;
2143 br_multicast_mark_router(br
, p
);
2150 spin_unlock(&br
->multicast_lock
);
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
);
2163 list_for_each_entry_rcu(port
, &br
->port_list
, list
) {
2164 if (port
->state
== BR_STATE_DISABLED
||
2165 port
->state
== BR_STATE_BLOCKING
)
2168 if (query
== &br
->ip4_own_query
)
2169 br_multicast_enable(&port
->ip4_own_query
);
2170 #if IS_ENABLED(CONFIG_IPV6)
2172 br_multicast_enable(&port
->ip6_own_query
);
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
;
2184 spin_lock_bh(&br
->multicast_lock
);
2185 if (br
->multicast_disabled
== !val
)
2188 br_mc_disabled_update(br
->dev
, !val
);
2189 br
->multicast_disabled
= !val
;
2190 if (br
->multicast_disabled
)
2193 if (!netif_running(br
->dev
))
2196 mdb
= mlock_dereference(br
->mdb
, br
);
2201 br
->multicast_disabled
= !!val
;
2205 err
= br_mdb_rehash(&br
->mdb
, mdb
->max
,
2206 br
->hash_elasticity
);
2211 br_multicast_open(br
);
2212 list_for_each_entry(port
, &br
->port_list
, list
)
2213 __br_multicast_enable_port(port
);
2216 spin_unlock_bh(&br
->multicast_lock
);
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
);
2234 spin_lock_bh(&br
->multicast_lock
);
2235 is_router
= br_multicast_is_router(br
);
2236 spin_unlock_bh(&br
->multicast_lock
);
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
;
2247 spin_lock_bh(&br
->multicast_lock
);
2248 if (br
->multicast_querier
== val
)
2251 br
->multicast_querier
= val
;
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
);
2270 spin_unlock_bh(&br
->multicast_lock
);
2275 int br_multicast_set_hash_max(struct net_bridge
*br
, unsigned long val
)
2279 struct net_bridge_mdb_htable
*mdb
;
2281 spin_lock_bh(&br
->multicast_lock
);
2282 if (!is_power_of_2(val
))
2285 mdb
= mlock_dereference(br
->mdb
, br
);
2286 if (mdb
&& val
< mdb
->size
)
2302 err
= br_mdb_rehash(&br
->mdb
, br
->hash_max
,
2303 br
->hash_elasticity
);
2309 spin_unlock_bh(&br
->multicast_lock
);
2314 int br_multicast_set_igmp_version(struct net_bridge
*br
, unsigned long val
)
2316 /* Currently we support only version 2 and 3 */
2325 spin_lock_bh(&br
->multicast_lock
);
2326 br
->multicast_igmp_version
= val
;
2327 spin_unlock_bh(&br
->multicast_lock
);
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 */
2344 spin_lock_bh(&br
->multicast_lock
);
2345 br
->multicast_mld_version
= val
;
2346 spin_unlock_bh(&br
->multicast_lock
);
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.
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
;
2379 if (!br_ip_list
|| !br_port_exists(dev
))
2382 port
= br_port_get_rcu(dev
);
2383 if (!port
|| !port
->br
)
2388 list_for_each_entry_rcu(port
, &br
->port_list
, list
) {
2389 if (!port
->dev
|| port
->dev
== dev
)
2392 hlist_for_each_entry_rcu(group
, &port
->mglist
, mglist
) {
2393 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
2397 entry
->addr
= group
->addr
;
2398 list_add(&entry
->list
, br_ip_list
);
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
;
2426 if (!br_port_exists(dev
))
2429 port
= br_port_get_rcu(dev
);
2430 if (!port
|| !port
->br
)
2435 memset(ð
, 0, sizeof(eth
));
2436 eth
.h_proto
= htons(proto
);
2438 ret
= br_multicast_querier_exists(br
, ð
);
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
;
2462 if (!br_port_exists(dev
))
2465 port
= br_port_get_rcu(dev
);
2466 if (!port
|| !port
->br
)
2473 if (!timer_pending(&br
->ip4_other_query
.timer
) ||
2474 rcu_dereference(br
->ip4_querier
.port
) == port
)
2477 #if IS_ENABLED(CONFIG_IPV6)
2479 if (!timer_pending(&br
->ip6_other_query
.timer
) ||
2480 rcu_dereference(br
->ip6_querier
.port
) == port
)
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
;
2502 u64_stats_update_begin(&pstats
->syncp
);
2504 case htons(ETH_P_IP
):
2505 t_len
= ntohs(ip_hdr(skb
)->tot_len
) - ip_hdrlen(skb
);
2507 case IGMP_HOST_MEMBERSHIP_REPORT
:
2508 pstats
->mstats
.igmp_v1reports
[dir
]++;
2510 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
2511 pstats
->mstats
.igmp_v2reports
[dir
]++;
2513 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
2514 pstats
->mstats
.igmp_v3reports
[dir
]++;
2516 case IGMP_HOST_MEMBERSHIP_QUERY
:
2517 if (t_len
!= sizeof(struct igmphdr
)) {
2518 pstats
->mstats
.igmp_v3queries
[dir
]++;
2520 unsigned int offset
= skb_transport_offset(skb
);
2521 struct igmphdr
*ih
, _ihdr
;
2523 ih
= skb_header_pointer(skb
, offset
,
2524 sizeof(_ihdr
), &_ihdr
);
2528 pstats
->mstats
.igmp_v1queries
[dir
]++;
2530 pstats
->mstats
.igmp_v2queries
[dir
]++;
2533 case IGMP_HOST_LEAVE_MESSAGE
:
2534 pstats
->mstats
.igmp_leaves
[dir
]++;
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
);
2544 case ICMPV6_MGM_REPORT
:
2545 pstats
->mstats
.mld_v1reports
[dir
]++;
2547 case ICMPV6_MLD2_REPORT
:
2548 pstats
->mstats
.mld_v2reports
[dir
]++;
2550 case ICMPV6_MGM_QUERY
:
2551 if (t_len
!= sizeof(struct mld_msg
))
2552 pstats
->mstats
.mld_v2queries
[dir
]++;
2554 pstats
->mstats
.mld_v1queries
[dir
]++;
2556 case ICMPV6_MGM_REDUCTION
:
2557 pstats
->mstats
.mld_leaves
[dir
]++;
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
)
2576 stats
= p
->mcast_stats
;
2578 stats
= br
->mcast_stats
;
2579 if (WARN_ON(!stats
))
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
)
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
;
2613 memset(dest
, 0, sizeof(*dest
));
2615 stats
= p
->mcast_stats
;
2617 stats
= br
->mcast_stats
;
2618 if (WARN_ON(!stats
))
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
;
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
));