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
= kzalloc(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
;
1151 ih
= igmpv3_report_hdr(skb
);
1152 num
= ntohs(ih
->ngrec
);
1153 len
= skb_transport_offset(skb
) + sizeof(*ih
);
1155 for (i
= 0; i
< num
; i
++) {
1156 len
+= sizeof(*grec
);
1157 if (!pskb_may_pull(skb
, len
))
1160 grec
= (void *)(skb
->data
+ len
- sizeof(*grec
));
1161 group
= grec
->grec_mca
;
1162 type
= grec
->grec_type
;
1164 len
+= ntohs(grec
->grec_nsrcs
) * 4;
1165 if (!pskb_may_pull(skb
, len
))
1168 /* We treat this as an IGMPv2 report for now. */
1170 case IGMPV3_MODE_IS_INCLUDE
:
1171 case IGMPV3_MODE_IS_EXCLUDE
:
1172 case IGMPV3_CHANGE_TO_INCLUDE
:
1173 case IGMPV3_CHANGE_TO_EXCLUDE
:
1174 case IGMPV3_ALLOW_NEW_SOURCES
:
1175 case IGMPV3_BLOCK_OLD_SOURCES
:
1182 src
= eth_hdr(skb
)->h_source
;
1183 if ((type
== IGMPV3_CHANGE_TO_INCLUDE
||
1184 type
== IGMPV3_MODE_IS_INCLUDE
) &&
1185 ntohs(grec
->grec_nsrcs
) == 0) {
1186 br_ip4_multicast_leave_group(br
, port
, group
, vid
, src
);
1188 err
= br_ip4_multicast_add_group(br
, port
, group
, vid
,
1198 #if IS_ENABLED(CONFIG_IPV6)
1199 static int br_ip6_multicast_mld2_report(struct net_bridge
*br
,
1200 struct net_bridge_port
*port
,
1201 struct sk_buff
*skb
,
1204 const unsigned char *src
;
1205 struct icmp6hdr
*icmp6h
;
1206 struct mld2_grec
*grec
;
1212 if (!pskb_may_pull(skb
, sizeof(*icmp6h
)))
1215 icmp6h
= icmp6_hdr(skb
);
1216 num
= ntohs(icmp6h
->icmp6_dataun
.un_data16
[1]);
1217 len
= skb_transport_offset(skb
) + sizeof(*icmp6h
);
1219 for (i
= 0; i
< num
; i
++) {
1220 __be16
*nsrcs
, _nsrcs
;
1222 nsrcs
= skb_header_pointer(skb
,
1223 len
+ offsetof(struct mld2_grec
,
1225 sizeof(_nsrcs
), &_nsrcs
);
1229 if (!pskb_may_pull(skb
,
1230 len
+ sizeof(*grec
) +
1231 sizeof(struct in6_addr
) * ntohs(*nsrcs
)))
1234 grec
= (struct mld2_grec
*)(skb
->data
+ len
);
1235 len
+= sizeof(*grec
) +
1236 sizeof(struct in6_addr
) * ntohs(*nsrcs
);
1238 /* We treat these as MLDv1 reports for now. */
1239 switch (grec
->grec_type
) {
1240 case MLD2_MODE_IS_INCLUDE
:
1241 case MLD2_MODE_IS_EXCLUDE
:
1242 case MLD2_CHANGE_TO_INCLUDE
:
1243 case MLD2_CHANGE_TO_EXCLUDE
:
1244 case MLD2_ALLOW_NEW_SOURCES
:
1245 case MLD2_BLOCK_OLD_SOURCES
:
1252 src
= eth_hdr(skb
)->h_source
;
1253 if ((grec
->grec_type
== MLD2_CHANGE_TO_INCLUDE
||
1254 grec
->grec_type
== MLD2_MODE_IS_INCLUDE
) &&
1255 ntohs(*nsrcs
) == 0) {
1256 br_ip6_multicast_leave_group(br
, port
, &grec
->grec_mca
,
1259 err
= br_ip6_multicast_add_group(br
, port
,
1260 &grec
->grec_mca
, vid
,
1271 static bool br_ip4_multicast_select_querier(struct net_bridge
*br
,
1272 struct net_bridge_port
*port
,
1275 if (!timer_pending(&br
->ip4_own_query
.timer
) &&
1276 !timer_pending(&br
->ip4_other_query
.timer
))
1279 if (!br
->ip4_querier
.addr
.u
.ip4
)
1282 if (ntohl(saddr
) <= ntohl(br
->ip4_querier
.addr
.u
.ip4
))
1288 br
->ip4_querier
.addr
.u
.ip4
= saddr
;
1290 /* update protected by general multicast_lock by caller */
1291 rcu_assign_pointer(br
->ip4_querier
.port
, port
);
1296 #if IS_ENABLED(CONFIG_IPV6)
1297 static bool br_ip6_multicast_select_querier(struct net_bridge
*br
,
1298 struct net_bridge_port
*port
,
1299 struct in6_addr
*saddr
)
1301 if (!timer_pending(&br
->ip6_own_query
.timer
) &&
1302 !timer_pending(&br
->ip6_other_query
.timer
))
1305 if (ipv6_addr_cmp(saddr
, &br
->ip6_querier
.addr
.u
.ip6
) <= 0)
1311 br
->ip6_querier
.addr
.u
.ip6
= *saddr
;
1313 /* update protected by general multicast_lock by caller */
1314 rcu_assign_pointer(br
->ip6_querier
.port
, port
);
1320 static bool br_multicast_select_querier(struct net_bridge
*br
,
1321 struct net_bridge_port
*port
,
1322 struct br_ip
*saddr
)
1324 switch (saddr
->proto
) {
1325 case htons(ETH_P_IP
):
1326 return br_ip4_multicast_select_querier(br
, port
, saddr
->u
.ip4
);
1327 #if IS_ENABLED(CONFIG_IPV6)
1328 case htons(ETH_P_IPV6
):
1329 return br_ip6_multicast_select_querier(br
, port
, &saddr
->u
.ip6
);
1337 br_multicast_update_query_timer(struct net_bridge
*br
,
1338 struct bridge_mcast_other_query
*query
,
1339 unsigned long max_delay
)
1341 if (!timer_pending(&query
->timer
))
1342 query
->delay_time
= jiffies
+ max_delay
;
1344 mod_timer(&query
->timer
, jiffies
+ br
->multicast_querier_interval
);
1347 static void br_port_mc_router_state_change(struct net_bridge_port
*p
,
1350 struct switchdev_attr attr
= {
1352 .id
= SWITCHDEV_ATTR_ID_PORT_MROUTER
,
1353 .flags
= SWITCHDEV_F_DEFER
,
1354 .u
.mrouter
= is_mc_router
,
1357 switchdev_port_attr_set(p
->dev
, &attr
);
1361 * Add port to router_list
1362 * list is maintained ordered by pointer value
1363 * and locked by br->multicast_lock and RCU
1365 static void br_multicast_add_router(struct net_bridge
*br
,
1366 struct net_bridge_port
*port
)
1368 struct net_bridge_port
*p
;
1369 struct hlist_node
*slot
= NULL
;
1371 if (!hlist_unhashed(&port
->rlist
))
1374 hlist_for_each_entry(p
, &br
->router_list
, rlist
) {
1375 if ((unsigned long) port
>= (unsigned long) p
)
1381 hlist_add_behind_rcu(&port
->rlist
, slot
);
1383 hlist_add_head_rcu(&port
->rlist
, &br
->router_list
);
1384 br_rtr_notify(br
->dev
, port
, RTM_NEWMDB
);
1385 br_port_mc_router_state_change(port
, true);
1388 static void br_multicast_mark_router(struct net_bridge
*br
,
1389 struct net_bridge_port
*port
)
1391 unsigned long now
= jiffies
;
1394 if (br
->multicast_router
== MDB_RTR_TYPE_TEMP_QUERY
) {
1395 if (!timer_pending(&br
->multicast_router_timer
))
1396 br_mc_router_state_change(br
, true);
1397 mod_timer(&br
->multicast_router_timer
,
1398 now
+ br
->multicast_querier_interval
);
1403 if (port
->multicast_router
== MDB_RTR_TYPE_DISABLED
||
1404 port
->multicast_router
== MDB_RTR_TYPE_PERM
)
1407 br_multicast_add_router(br
, port
);
1409 mod_timer(&port
->multicast_router_timer
,
1410 now
+ br
->multicast_querier_interval
);
1413 static void br_multicast_query_received(struct net_bridge
*br
,
1414 struct net_bridge_port
*port
,
1415 struct bridge_mcast_other_query
*query
,
1416 struct br_ip
*saddr
,
1417 unsigned long max_delay
)
1419 if (!br_multicast_select_querier(br
, port
, saddr
))
1422 br_multicast_update_query_timer(br
, query
, max_delay
);
1423 br_multicast_mark_router(br
, port
);
1426 static int br_ip4_multicast_query(struct net_bridge
*br
,
1427 struct net_bridge_port
*port
,
1428 struct sk_buff
*skb
,
1431 const struct iphdr
*iph
= ip_hdr(skb
);
1432 struct igmphdr
*ih
= igmp_hdr(skb
);
1433 struct net_bridge_mdb_entry
*mp
;
1434 struct igmpv3_query
*ih3
;
1435 struct net_bridge_port_group
*p
;
1436 struct net_bridge_port_group __rcu
**pp
;
1438 unsigned long max_delay
;
1439 unsigned long now
= jiffies
;
1440 unsigned int offset
= skb_transport_offset(skb
);
1444 spin_lock(&br
->multicast_lock
);
1445 if (!netif_running(br
->dev
) ||
1446 (port
&& port
->state
== BR_STATE_DISABLED
))
1451 if (skb
->len
== offset
+ sizeof(*ih
)) {
1452 max_delay
= ih
->code
* (HZ
/ IGMP_TIMER_SCALE
);
1455 max_delay
= 10 * HZ
;
1458 } else if (skb
->len
>= offset
+ sizeof(*ih3
)) {
1459 ih3
= igmpv3_query_hdr(skb
);
1463 max_delay
= ih3
->code
?
1464 IGMPV3_MRC(ih3
->code
) * (HZ
/ IGMP_TIMER_SCALE
) : 1;
1470 saddr
.proto
= htons(ETH_P_IP
);
1471 saddr
.u
.ip4
= iph
->saddr
;
1473 br_multicast_query_received(br
, port
, &br
->ip4_other_query
,
1478 mp
= br_mdb_ip4_get(mlock_dereference(br
->mdb
, br
), group
, vid
);
1482 max_delay
*= br
->multicast_last_member_count
;
1484 if (mp
->host_joined
&&
1485 (timer_pending(&mp
->timer
) ?
1486 time_after(mp
->timer
.expires
, now
+ max_delay
) :
1487 try_to_del_timer_sync(&mp
->timer
) >= 0))
1488 mod_timer(&mp
->timer
, now
+ max_delay
);
1490 for (pp
= &mp
->ports
;
1491 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1493 if (timer_pending(&p
->timer
) ?
1494 time_after(p
->timer
.expires
, now
+ max_delay
) :
1495 try_to_del_timer_sync(&p
->timer
) >= 0)
1496 mod_timer(&p
->timer
, now
+ max_delay
);
1500 spin_unlock(&br
->multicast_lock
);
1504 #if IS_ENABLED(CONFIG_IPV6)
1505 static int br_ip6_multicast_query(struct net_bridge
*br
,
1506 struct net_bridge_port
*port
,
1507 struct sk_buff
*skb
,
1510 const struct ipv6hdr
*ip6h
= ipv6_hdr(skb
);
1511 struct mld_msg
*mld
;
1512 struct net_bridge_mdb_entry
*mp
;
1513 struct mld2_query
*mld2q
;
1514 struct net_bridge_port_group
*p
;
1515 struct net_bridge_port_group __rcu
**pp
;
1517 unsigned long max_delay
;
1518 unsigned long now
= jiffies
;
1519 unsigned int offset
= skb_transport_offset(skb
);
1520 const struct in6_addr
*group
= NULL
;
1521 bool is_general_query
;
1524 spin_lock(&br
->multicast_lock
);
1525 if (!netif_running(br
->dev
) ||
1526 (port
&& port
->state
== BR_STATE_DISABLED
))
1529 if (skb
->len
== offset
+ sizeof(*mld
)) {
1530 if (!pskb_may_pull(skb
, offset
+ sizeof(*mld
))) {
1534 mld
= (struct mld_msg
*) icmp6_hdr(skb
);
1535 max_delay
= msecs_to_jiffies(ntohs(mld
->mld_maxdelay
));
1537 group
= &mld
->mld_mca
;
1539 if (!pskb_may_pull(skb
, offset
+ sizeof(*mld2q
))) {
1543 mld2q
= (struct mld2_query
*)icmp6_hdr(skb
);
1544 if (!mld2q
->mld2q_nsrcs
)
1545 group
= &mld2q
->mld2q_mca
;
1547 max_delay
= max(msecs_to_jiffies(mldv2_mrc(mld2q
)), 1UL);
1550 is_general_query
= group
&& ipv6_addr_any(group
);
1552 if (is_general_query
) {
1553 saddr
.proto
= htons(ETH_P_IPV6
);
1554 saddr
.u
.ip6
= ip6h
->saddr
;
1556 br_multicast_query_received(br
, port
, &br
->ip6_other_query
,
1559 } else if (!group
) {
1563 mp
= br_mdb_ip6_get(mlock_dereference(br
->mdb
, br
), group
, vid
);
1567 max_delay
*= br
->multicast_last_member_count
;
1568 if (mp
->host_joined
&&
1569 (timer_pending(&mp
->timer
) ?
1570 time_after(mp
->timer
.expires
, now
+ max_delay
) :
1571 try_to_del_timer_sync(&mp
->timer
) >= 0))
1572 mod_timer(&mp
->timer
, now
+ max_delay
);
1574 for (pp
= &mp
->ports
;
1575 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1577 if (timer_pending(&p
->timer
) ?
1578 time_after(p
->timer
.expires
, now
+ max_delay
) :
1579 try_to_del_timer_sync(&p
->timer
) >= 0)
1580 mod_timer(&p
->timer
, now
+ max_delay
);
1584 spin_unlock(&br
->multicast_lock
);
1590 br_multicast_leave_group(struct net_bridge
*br
,
1591 struct net_bridge_port
*port
,
1592 struct br_ip
*group
,
1593 struct bridge_mcast_other_query
*other_query
,
1594 struct bridge_mcast_own_query
*own_query
,
1595 const unsigned char *src
)
1597 struct net_bridge_mdb_htable
*mdb
;
1598 struct net_bridge_mdb_entry
*mp
;
1599 struct net_bridge_port_group
*p
;
1603 spin_lock(&br
->multicast_lock
);
1604 if (!netif_running(br
->dev
) ||
1605 (port
&& port
->state
== BR_STATE_DISABLED
))
1608 mdb
= mlock_dereference(br
->mdb
, br
);
1609 mp
= br_mdb_ip_get(mdb
, group
);
1613 if (port
&& (port
->flags
& BR_MULTICAST_FAST_LEAVE
)) {
1614 struct net_bridge_port_group __rcu
**pp
;
1616 for (pp
= &mp
->ports
;
1617 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1619 if (!br_port_group_equal(p
, port
, src
))
1622 rcu_assign_pointer(*pp
, p
->next
);
1623 hlist_del_init(&p
->mglist
);
1624 del_timer(&p
->timer
);
1625 call_rcu_bh(&p
->rcu
, br_multicast_free_pg
);
1626 br_mdb_notify(br
->dev
, port
, group
, RTM_DELMDB
,
1629 if (!mp
->ports
&& !mp
->host_joined
&&
1630 netif_running(br
->dev
))
1631 mod_timer(&mp
->timer
, jiffies
);
1636 if (timer_pending(&other_query
->timer
))
1639 if (br
->multicast_querier
) {
1640 __br_multicast_send_query(br
, port
, &mp
->addr
);
1642 time
= jiffies
+ br
->multicast_last_member_count
*
1643 br
->multicast_last_member_interval
;
1645 mod_timer(&own_query
->timer
, time
);
1647 for (p
= mlock_dereference(mp
->ports
, br
);
1649 p
= mlock_dereference(p
->next
, br
)) {
1650 if (!br_port_group_equal(p
, port
, src
))
1653 if (!hlist_unhashed(&p
->mglist
) &&
1654 (timer_pending(&p
->timer
) ?
1655 time_after(p
->timer
.expires
, time
) :
1656 try_to_del_timer_sync(&p
->timer
) >= 0)) {
1657 mod_timer(&p
->timer
, time
);
1665 time
= now
+ br
->multicast_last_member_count
*
1666 br
->multicast_last_member_interval
;
1669 if (mp
->host_joined
&&
1670 (timer_pending(&mp
->timer
) ?
1671 time_after(mp
->timer
.expires
, time
) :
1672 try_to_del_timer_sync(&mp
->timer
) >= 0)) {
1673 mod_timer(&mp
->timer
, time
);
1679 for (p
= mlock_dereference(mp
->ports
, br
);
1681 p
= mlock_dereference(p
->next
, br
)) {
1682 if (p
->port
!= port
)
1685 if (!hlist_unhashed(&p
->mglist
) &&
1686 (timer_pending(&p
->timer
) ?
1687 time_after(p
->timer
.expires
, time
) :
1688 try_to_del_timer_sync(&p
->timer
) >= 0)) {
1689 mod_timer(&p
->timer
, time
);
1695 spin_unlock(&br
->multicast_lock
);
1698 static void br_ip4_multicast_leave_group(struct net_bridge
*br
,
1699 struct net_bridge_port
*port
,
1702 const unsigned char *src
)
1704 struct br_ip br_group
;
1705 struct bridge_mcast_own_query
*own_query
;
1707 if (ipv4_is_local_multicast(group
))
1710 own_query
= port
? &port
->ip4_own_query
: &br
->ip4_own_query
;
1712 br_group
.u
.ip4
= group
;
1713 br_group
.proto
= htons(ETH_P_IP
);
1716 br_multicast_leave_group(br
, port
, &br_group
, &br
->ip4_other_query
,
1720 #if IS_ENABLED(CONFIG_IPV6)
1721 static void br_ip6_multicast_leave_group(struct net_bridge
*br
,
1722 struct net_bridge_port
*port
,
1723 const struct in6_addr
*group
,
1725 const unsigned char *src
)
1727 struct br_ip br_group
;
1728 struct bridge_mcast_own_query
*own_query
;
1730 if (ipv6_addr_is_ll_all_nodes(group
))
1733 own_query
= port
? &port
->ip6_own_query
: &br
->ip6_own_query
;
1735 br_group
.u
.ip6
= *group
;
1736 br_group
.proto
= htons(ETH_P_IPV6
);
1739 br_multicast_leave_group(br
, port
, &br_group
, &br
->ip6_other_query
,
1744 static void br_multicast_err_count(const struct net_bridge
*br
,
1745 const struct net_bridge_port
*p
,
1748 struct bridge_mcast_stats __percpu
*stats
;
1749 struct bridge_mcast_stats
*pstats
;
1751 if (!br
->multicast_stats_enabled
)
1755 stats
= p
->mcast_stats
;
1757 stats
= br
->mcast_stats
;
1758 if (WARN_ON(!stats
))
1761 pstats
= this_cpu_ptr(stats
);
1763 u64_stats_update_begin(&pstats
->syncp
);
1765 case htons(ETH_P_IP
):
1766 pstats
->mstats
.igmp_parse_errors
++;
1768 #if IS_ENABLED(CONFIG_IPV6)
1769 case htons(ETH_P_IPV6
):
1770 pstats
->mstats
.mld_parse_errors
++;
1774 u64_stats_update_end(&pstats
->syncp
);
1777 static void br_multicast_pim(struct net_bridge
*br
,
1778 struct net_bridge_port
*port
,
1779 const struct sk_buff
*skb
)
1781 unsigned int offset
= skb_transport_offset(skb
);
1782 struct pimhdr
*pimhdr
, _pimhdr
;
1784 pimhdr
= skb_header_pointer(skb
, offset
, sizeof(_pimhdr
), &_pimhdr
);
1785 if (!pimhdr
|| pim_hdr_version(pimhdr
) != PIM_VERSION
||
1786 pim_hdr_type(pimhdr
) != PIM_TYPE_HELLO
)
1789 br_multicast_mark_router(br
, port
);
1792 static int br_multicast_ipv4_rcv(struct net_bridge
*br
,
1793 struct net_bridge_port
*port
,
1794 struct sk_buff
*skb
,
1797 struct sk_buff
*skb_trimmed
= NULL
;
1798 const unsigned char *src
;
1802 err
= ip_mc_check_igmp(skb
, &skb_trimmed
);
1804 if (err
== -ENOMSG
) {
1805 if (!ipv4_is_local_multicast(ip_hdr(skb
)->daddr
)) {
1806 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1807 } else if (pim_ipv4_all_pim_routers(ip_hdr(skb
)->daddr
)) {
1808 if (ip_hdr(skb
)->protocol
== IPPROTO_PIM
)
1809 br_multicast_pim(br
, port
, skb
);
1812 } else if (err
< 0) {
1813 br_multicast_err_count(br
, port
, skb
->protocol
);
1818 src
= eth_hdr(skb
)->h_source
;
1819 BR_INPUT_SKB_CB(skb
)->igmp
= ih
->type
;
1822 case IGMP_HOST_MEMBERSHIP_REPORT
:
1823 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1824 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1825 err
= br_ip4_multicast_add_group(br
, port
, ih
->group
, vid
, src
);
1827 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1828 err
= br_ip4_multicast_igmp3_report(br
, port
, skb_trimmed
, vid
);
1830 case IGMP_HOST_MEMBERSHIP_QUERY
:
1831 err
= br_ip4_multicast_query(br
, port
, skb_trimmed
, vid
);
1833 case IGMP_HOST_LEAVE_MESSAGE
:
1834 br_ip4_multicast_leave_group(br
, port
, ih
->group
, vid
, src
);
1838 if (skb_trimmed
&& skb_trimmed
!= skb
)
1839 kfree_skb(skb_trimmed
);
1841 br_multicast_count(br
, port
, skb
, BR_INPUT_SKB_CB(skb
)->igmp
,
1847 #if IS_ENABLED(CONFIG_IPV6)
1848 static int br_multicast_ipv6_rcv(struct net_bridge
*br
,
1849 struct net_bridge_port
*port
,
1850 struct sk_buff
*skb
,
1853 struct sk_buff
*skb_trimmed
= NULL
;
1854 const unsigned char *src
;
1855 struct mld_msg
*mld
;
1858 err
= ipv6_mc_check_mld(skb
, &skb_trimmed
);
1860 if (err
== -ENOMSG
) {
1861 if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb
)->daddr
))
1862 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1864 } else if (err
< 0) {
1865 br_multicast_err_count(br
, port
, skb
->protocol
);
1869 mld
= (struct mld_msg
*)skb_transport_header(skb
);
1870 BR_INPUT_SKB_CB(skb
)->igmp
= mld
->mld_type
;
1872 switch (mld
->mld_type
) {
1873 case ICMPV6_MGM_REPORT
:
1874 src
= eth_hdr(skb
)->h_source
;
1875 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1876 err
= br_ip6_multicast_add_group(br
, port
, &mld
->mld_mca
, vid
,
1879 case ICMPV6_MLD2_REPORT
:
1880 err
= br_ip6_multicast_mld2_report(br
, port
, skb_trimmed
, vid
);
1882 case ICMPV6_MGM_QUERY
:
1883 err
= br_ip6_multicast_query(br
, port
, skb_trimmed
, vid
);
1885 case ICMPV6_MGM_REDUCTION
:
1886 src
= eth_hdr(skb
)->h_source
;
1887 br_ip6_multicast_leave_group(br
, port
, &mld
->mld_mca
, vid
, src
);
1891 if (skb_trimmed
&& skb_trimmed
!= skb
)
1892 kfree_skb(skb_trimmed
);
1894 br_multicast_count(br
, port
, skb
, BR_INPUT_SKB_CB(skb
)->igmp
,
1901 int br_multicast_rcv(struct net_bridge
*br
, struct net_bridge_port
*port
,
1902 struct sk_buff
*skb
, u16 vid
)
1906 BR_INPUT_SKB_CB(skb
)->igmp
= 0;
1907 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 0;
1909 if (br
->multicast_disabled
)
1912 switch (skb
->protocol
) {
1913 case htons(ETH_P_IP
):
1914 ret
= br_multicast_ipv4_rcv(br
, port
, skb
, vid
);
1916 #if IS_ENABLED(CONFIG_IPV6)
1917 case htons(ETH_P_IPV6
):
1918 ret
= br_multicast_ipv6_rcv(br
, port
, skb
, vid
);
1926 static void br_multicast_query_expired(struct net_bridge
*br
,
1927 struct bridge_mcast_own_query
*query
,
1928 struct bridge_mcast_querier
*querier
)
1930 spin_lock(&br
->multicast_lock
);
1931 if (query
->startup_sent
< br
->multicast_startup_query_count
)
1932 query
->startup_sent
++;
1934 RCU_INIT_POINTER(querier
->port
, NULL
);
1935 br_multicast_send_query(br
, NULL
, query
);
1936 spin_unlock(&br
->multicast_lock
);
1939 static void br_ip4_multicast_query_expired(struct timer_list
*t
)
1941 struct net_bridge
*br
= from_timer(br
, t
, ip4_own_query
.timer
);
1943 br_multicast_query_expired(br
, &br
->ip4_own_query
, &br
->ip4_querier
);
1946 #if IS_ENABLED(CONFIG_IPV6)
1947 static void br_ip6_multicast_query_expired(struct timer_list
*t
)
1949 struct net_bridge
*br
= from_timer(br
, t
, ip6_own_query
.timer
);
1951 br_multicast_query_expired(br
, &br
->ip6_own_query
, &br
->ip6_querier
);
1955 void br_multicast_init(struct net_bridge
*br
)
1957 br
->hash_elasticity
= 4;
1960 br
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
1961 br
->multicast_querier
= 0;
1962 br
->multicast_query_use_ifaddr
= 0;
1963 br
->multicast_last_member_count
= 2;
1964 br
->multicast_startup_query_count
= 2;
1966 br
->multicast_last_member_interval
= HZ
;
1967 br
->multicast_query_response_interval
= 10 * HZ
;
1968 br
->multicast_startup_query_interval
= 125 * HZ
/ 4;
1969 br
->multicast_query_interval
= 125 * HZ
;
1970 br
->multicast_querier_interval
= 255 * HZ
;
1971 br
->multicast_membership_interval
= 260 * HZ
;
1973 br
->ip4_other_query
.delay_time
= 0;
1974 br
->ip4_querier
.port
= NULL
;
1975 br
->multicast_igmp_version
= 2;
1976 #if IS_ENABLED(CONFIG_IPV6)
1977 br
->multicast_mld_version
= 1;
1978 br
->ip6_other_query
.delay_time
= 0;
1979 br
->ip6_querier
.port
= NULL
;
1981 br
->has_ipv6_addr
= 1;
1983 spin_lock_init(&br
->multicast_lock
);
1984 timer_setup(&br
->multicast_router_timer
,
1985 br_multicast_local_router_expired
, 0);
1986 timer_setup(&br
->ip4_other_query
.timer
,
1987 br_ip4_multicast_querier_expired
, 0);
1988 timer_setup(&br
->ip4_own_query
.timer
,
1989 br_ip4_multicast_query_expired
, 0);
1990 #if IS_ENABLED(CONFIG_IPV6)
1991 timer_setup(&br
->ip6_other_query
.timer
,
1992 br_ip6_multicast_querier_expired
, 0);
1993 timer_setup(&br
->ip6_own_query
.timer
,
1994 br_ip6_multicast_query_expired
, 0);
1998 static void __br_multicast_open(struct net_bridge
*br
,
1999 struct bridge_mcast_own_query
*query
)
2001 query
->startup_sent
= 0;
2003 if (br
->multicast_disabled
)
2006 mod_timer(&query
->timer
, jiffies
);
2009 void br_multicast_open(struct net_bridge
*br
)
2011 __br_multicast_open(br
, &br
->ip4_own_query
);
2012 #if IS_ENABLED(CONFIG_IPV6)
2013 __br_multicast_open(br
, &br
->ip6_own_query
);
2017 void br_multicast_stop(struct net_bridge
*br
)
2019 del_timer_sync(&br
->multicast_router_timer
);
2020 del_timer_sync(&br
->ip4_other_query
.timer
);
2021 del_timer_sync(&br
->ip4_own_query
.timer
);
2022 #if IS_ENABLED(CONFIG_IPV6)
2023 del_timer_sync(&br
->ip6_other_query
.timer
);
2024 del_timer_sync(&br
->ip6_own_query
.timer
);
2028 void br_multicast_dev_del(struct net_bridge
*br
)
2030 struct net_bridge_mdb_htable
*mdb
;
2031 struct net_bridge_mdb_entry
*mp
;
2032 struct hlist_node
*n
;
2036 spin_lock_bh(&br
->multicast_lock
);
2037 mdb
= mlock_dereference(br
->mdb
, br
);
2044 for (i
= 0; i
< mdb
->max
; i
++) {
2045 hlist_for_each_entry_safe(mp
, n
, &mdb
->mhash
[i
],
2047 del_timer(&mp
->timer
);
2048 call_rcu_bh(&mp
->rcu
, br_multicast_free_group
);
2053 spin_unlock_bh(&br
->multicast_lock
);
2055 spin_lock_bh(&br
->multicast_lock
);
2060 call_rcu_bh(&mdb
->rcu
, br_mdb_free
);
2063 spin_unlock_bh(&br
->multicast_lock
);
2066 int br_multicast_set_router(struct net_bridge
*br
, unsigned long val
)
2070 spin_lock_bh(&br
->multicast_lock
);
2073 case MDB_RTR_TYPE_DISABLED
:
2074 case MDB_RTR_TYPE_PERM
:
2075 br_mc_router_state_change(br
, val
== MDB_RTR_TYPE_PERM
);
2076 del_timer(&br
->multicast_router_timer
);
2077 br
->multicast_router
= val
;
2080 case MDB_RTR_TYPE_TEMP_QUERY
:
2081 if (br
->multicast_router
!= MDB_RTR_TYPE_TEMP_QUERY
)
2082 br_mc_router_state_change(br
, false);
2083 br
->multicast_router
= val
;
2088 spin_unlock_bh(&br
->multicast_lock
);
2093 static void __del_port_router(struct net_bridge_port
*p
)
2095 if (hlist_unhashed(&p
->rlist
))
2097 hlist_del_init_rcu(&p
->rlist
);
2098 br_rtr_notify(p
->br
->dev
, p
, RTM_DELMDB
);
2099 br_port_mc_router_state_change(p
, false);
2101 /* don't allow timer refresh */
2102 if (p
->multicast_router
== MDB_RTR_TYPE_TEMP
)
2103 p
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
2106 int br_multicast_set_port_router(struct net_bridge_port
*p
, unsigned long val
)
2108 struct net_bridge
*br
= p
->br
;
2109 unsigned long now
= jiffies
;
2112 spin_lock(&br
->multicast_lock
);
2113 if (p
->multicast_router
== val
) {
2114 /* Refresh the temp router port timer */
2115 if (p
->multicast_router
== MDB_RTR_TYPE_TEMP
)
2116 mod_timer(&p
->multicast_router_timer
,
2117 now
+ br
->multicast_querier_interval
);
2122 case MDB_RTR_TYPE_DISABLED
:
2123 p
->multicast_router
= MDB_RTR_TYPE_DISABLED
;
2124 __del_port_router(p
);
2125 del_timer(&p
->multicast_router_timer
);
2127 case MDB_RTR_TYPE_TEMP_QUERY
:
2128 p
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
2129 __del_port_router(p
);
2131 case MDB_RTR_TYPE_PERM
:
2132 p
->multicast_router
= MDB_RTR_TYPE_PERM
;
2133 del_timer(&p
->multicast_router_timer
);
2134 br_multicast_add_router(br
, p
);
2136 case MDB_RTR_TYPE_TEMP
:
2137 p
->multicast_router
= MDB_RTR_TYPE_TEMP
;
2138 br_multicast_mark_router(br
, p
);
2145 spin_unlock(&br
->multicast_lock
);
2150 static void br_multicast_start_querier(struct net_bridge
*br
,
2151 struct bridge_mcast_own_query
*query
)
2153 struct net_bridge_port
*port
;
2155 __br_multicast_open(br
, query
);
2157 list_for_each_entry(port
, &br
->port_list
, list
) {
2158 if (port
->state
== BR_STATE_DISABLED
||
2159 port
->state
== BR_STATE_BLOCKING
)
2162 if (query
== &br
->ip4_own_query
)
2163 br_multicast_enable(&port
->ip4_own_query
);
2164 #if IS_ENABLED(CONFIG_IPV6)
2166 br_multicast_enable(&port
->ip6_own_query
);
2171 int br_multicast_toggle(struct net_bridge
*br
, unsigned long val
)
2173 struct net_bridge_mdb_htable
*mdb
;
2174 struct net_bridge_port
*port
;
2177 spin_lock_bh(&br
->multicast_lock
);
2178 if (br
->multicast_disabled
== !val
)
2181 br_mc_disabled_update(br
->dev
, !val
);
2182 br
->multicast_disabled
= !val
;
2183 if (br
->multicast_disabled
)
2186 if (!netif_running(br
->dev
))
2189 mdb
= mlock_dereference(br
->mdb
, br
);
2194 br
->multicast_disabled
= !!val
;
2198 err
= br_mdb_rehash(&br
->mdb
, mdb
->max
,
2199 br
->hash_elasticity
);
2204 br_multicast_open(br
);
2205 list_for_each_entry(port
, &br
->port_list
, list
)
2206 __br_multicast_enable_port(port
);
2209 spin_unlock_bh(&br
->multicast_lock
);
2214 bool br_multicast_enabled(const struct net_device
*dev
)
2216 struct net_bridge
*br
= netdev_priv(dev
);
2218 return !br
->multicast_disabled
;
2220 EXPORT_SYMBOL_GPL(br_multicast_enabled
);
2222 bool br_multicast_router(const struct net_device
*dev
)
2224 struct net_bridge
*br
= netdev_priv(dev
);
2227 spin_lock_bh(&br
->multicast_lock
);
2228 is_router
= br_multicast_is_router(br
);
2229 spin_unlock_bh(&br
->multicast_lock
);
2232 EXPORT_SYMBOL_GPL(br_multicast_router
);
2234 int br_multicast_set_querier(struct net_bridge
*br
, unsigned long val
)
2236 unsigned long max_delay
;
2240 spin_lock_bh(&br
->multicast_lock
);
2241 if (br
->multicast_querier
== val
)
2244 br
->multicast_querier
= val
;
2248 max_delay
= br
->multicast_query_response_interval
;
2250 if (!timer_pending(&br
->ip4_other_query
.timer
))
2251 br
->ip4_other_query
.delay_time
= jiffies
+ max_delay
;
2253 br_multicast_start_querier(br
, &br
->ip4_own_query
);
2255 #if IS_ENABLED(CONFIG_IPV6)
2256 if (!timer_pending(&br
->ip6_other_query
.timer
))
2257 br
->ip6_other_query
.delay_time
= jiffies
+ max_delay
;
2259 br_multicast_start_querier(br
, &br
->ip6_own_query
);
2263 spin_unlock_bh(&br
->multicast_lock
);
2268 int br_multicast_set_hash_max(struct net_bridge
*br
, unsigned long val
)
2272 struct net_bridge_mdb_htable
*mdb
;
2274 spin_lock_bh(&br
->multicast_lock
);
2275 if (!is_power_of_2(val
))
2278 mdb
= mlock_dereference(br
->mdb
, br
);
2279 if (mdb
&& val
< mdb
->size
)
2295 err
= br_mdb_rehash(&br
->mdb
, br
->hash_max
,
2296 br
->hash_elasticity
);
2302 spin_unlock_bh(&br
->multicast_lock
);
2307 int br_multicast_set_igmp_version(struct net_bridge
*br
, unsigned long val
)
2309 /* Currently we support only version 2 and 3 */
2318 spin_lock_bh(&br
->multicast_lock
);
2319 br
->multicast_igmp_version
= val
;
2320 spin_unlock_bh(&br
->multicast_lock
);
2325 #if IS_ENABLED(CONFIG_IPV6)
2326 int br_multicast_set_mld_version(struct net_bridge
*br
, unsigned long val
)
2328 /* Currently we support version 1 and 2 */
2337 spin_lock_bh(&br
->multicast_lock
);
2338 br
->multicast_mld_version
= val
;
2339 spin_unlock_bh(&br
->multicast_lock
);
2346 * br_multicast_list_adjacent - Returns snooped multicast addresses
2347 * @dev: The bridge port adjacent to which to retrieve addresses
2348 * @br_ip_list: The list to store found, snooped multicast IP addresses in
2350 * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
2351 * snooping feature on all bridge ports of dev's bridge device, excluding
2352 * the addresses from dev itself.
2354 * Returns the number of items added to br_ip_list.
2357 * - br_ip_list needs to be initialized by caller
2358 * - br_ip_list might contain duplicates in the end
2359 * (needs to be taken care of by caller)
2360 * - br_ip_list needs to be freed by caller
2362 int br_multicast_list_adjacent(struct net_device
*dev
,
2363 struct list_head
*br_ip_list
)
2365 struct net_bridge
*br
;
2366 struct net_bridge_port
*port
;
2367 struct net_bridge_port_group
*group
;
2368 struct br_ip_list
*entry
;
2372 if (!br_ip_list
|| !br_port_exists(dev
))
2375 port
= br_port_get_rcu(dev
);
2376 if (!port
|| !port
->br
)
2381 list_for_each_entry_rcu(port
, &br
->port_list
, list
) {
2382 if (!port
->dev
|| port
->dev
== dev
)
2385 hlist_for_each_entry_rcu(group
, &port
->mglist
, mglist
) {
2386 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
2390 entry
->addr
= group
->addr
;
2391 list_add(&entry
->list
, br_ip_list
);
2400 EXPORT_SYMBOL_GPL(br_multicast_list_adjacent
);
2403 * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2404 * @dev: The bridge port providing the bridge on which to check for a querier
2405 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2407 * Checks whether the given interface has a bridge on top and if so returns
2408 * true if a valid querier exists anywhere on the bridged link layer.
2409 * Otherwise returns false.
2411 bool br_multicast_has_querier_anywhere(struct net_device
*dev
, int proto
)
2413 struct net_bridge
*br
;
2414 struct net_bridge_port
*port
;
2419 if (!br_port_exists(dev
))
2422 port
= br_port_get_rcu(dev
);
2423 if (!port
|| !port
->br
)
2428 memset(ð
, 0, sizeof(eth
));
2429 eth
.h_proto
= htons(proto
);
2431 ret
= br_multicast_querier_exists(br
, ð
);
2437 EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere
);
2440 * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
2441 * @dev: The bridge port adjacent to which to check for a querier
2442 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2444 * Checks whether the given interface has a bridge on top and if so returns
2445 * true if a selected querier is behind one of the other ports of this
2446 * bridge. Otherwise returns false.
2448 bool br_multicast_has_querier_adjacent(struct net_device
*dev
, int proto
)
2450 struct net_bridge
*br
;
2451 struct net_bridge_port
*port
;
2455 if (!br_port_exists(dev
))
2458 port
= br_port_get_rcu(dev
);
2459 if (!port
|| !port
->br
)
2466 if (!timer_pending(&br
->ip4_other_query
.timer
) ||
2467 rcu_dereference(br
->ip4_querier
.port
) == port
)
2470 #if IS_ENABLED(CONFIG_IPV6)
2472 if (!timer_pending(&br
->ip6_other_query
.timer
) ||
2473 rcu_dereference(br
->ip6_querier
.port
) == port
)
2486 EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent
);
2488 static void br_mcast_stats_add(struct bridge_mcast_stats __percpu
*stats
,
2489 const struct sk_buff
*skb
, u8 type
, u8 dir
)
2491 struct bridge_mcast_stats
*pstats
= this_cpu_ptr(stats
);
2492 __be16 proto
= skb
->protocol
;
2495 u64_stats_update_begin(&pstats
->syncp
);
2497 case htons(ETH_P_IP
):
2498 t_len
= ntohs(ip_hdr(skb
)->tot_len
) - ip_hdrlen(skb
);
2500 case IGMP_HOST_MEMBERSHIP_REPORT
:
2501 pstats
->mstats
.igmp_v1reports
[dir
]++;
2503 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
2504 pstats
->mstats
.igmp_v2reports
[dir
]++;
2506 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
2507 pstats
->mstats
.igmp_v3reports
[dir
]++;
2509 case IGMP_HOST_MEMBERSHIP_QUERY
:
2510 if (t_len
!= sizeof(struct igmphdr
)) {
2511 pstats
->mstats
.igmp_v3queries
[dir
]++;
2513 unsigned int offset
= skb_transport_offset(skb
);
2514 struct igmphdr
*ih
, _ihdr
;
2516 ih
= skb_header_pointer(skb
, offset
,
2517 sizeof(_ihdr
), &_ihdr
);
2521 pstats
->mstats
.igmp_v1queries
[dir
]++;
2523 pstats
->mstats
.igmp_v2queries
[dir
]++;
2526 case IGMP_HOST_LEAVE_MESSAGE
:
2527 pstats
->mstats
.igmp_leaves
[dir
]++;
2531 #if IS_ENABLED(CONFIG_IPV6)
2532 case htons(ETH_P_IPV6
):
2533 t_len
= ntohs(ipv6_hdr(skb
)->payload_len
) +
2534 sizeof(struct ipv6hdr
);
2535 t_len
-= skb_network_header_len(skb
);
2537 case ICMPV6_MGM_REPORT
:
2538 pstats
->mstats
.mld_v1reports
[dir
]++;
2540 case ICMPV6_MLD2_REPORT
:
2541 pstats
->mstats
.mld_v2reports
[dir
]++;
2543 case ICMPV6_MGM_QUERY
:
2544 if (t_len
!= sizeof(struct mld_msg
))
2545 pstats
->mstats
.mld_v2queries
[dir
]++;
2547 pstats
->mstats
.mld_v1queries
[dir
]++;
2549 case ICMPV6_MGM_REDUCTION
:
2550 pstats
->mstats
.mld_leaves
[dir
]++;
2554 #endif /* CONFIG_IPV6 */
2556 u64_stats_update_end(&pstats
->syncp
);
2559 void br_multicast_count(struct net_bridge
*br
, const struct net_bridge_port
*p
,
2560 const struct sk_buff
*skb
, u8 type
, u8 dir
)
2562 struct bridge_mcast_stats __percpu
*stats
;
2564 /* if multicast_disabled is true then igmp type can't be set */
2565 if (!type
|| !br
->multicast_stats_enabled
)
2569 stats
= p
->mcast_stats
;
2571 stats
= br
->mcast_stats
;
2572 if (WARN_ON(!stats
))
2575 br_mcast_stats_add(stats
, skb
, type
, dir
);
2578 int br_multicast_init_stats(struct net_bridge
*br
)
2580 br
->mcast_stats
= netdev_alloc_pcpu_stats(struct bridge_mcast_stats
);
2581 if (!br
->mcast_stats
)
2587 void br_multicast_uninit_stats(struct net_bridge
*br
)
2589 free_percpu(br
->mcast_stats
);
2592 static void mcast_stats_add_dir(u64
*dst
, u64
*src
)
2594 dst
[BR_MCAST_DIR_RX
] += src
[BR_MCAST_DIR_RX
];
2595 dst
[BR_MCAST_DIR_TX
] += src
[BR_MCAST_DIR_TX
];
2598 void br_multicast_get_stats(const struct net_bridge
*br
,
2599 const struct net_bridge_port
*p
,
2600 struct br_mcast_stats
*dest
)
2602 struct bridge_mcast_stats __percpu
*stats
;
2603 struct br_mcast_stats tdst
;
2606 memset(dest
, 0, sizeof(*dest
));
2608 stats
= p
->mcast_stats
;
2610 stats
= br
->mcast_stats
;
2611 if (WARN_ON(!stats
))
2614 memset(&tdst
, 0, sizeof(tdst
));
2615 for_each_possible_cpu(i
) {
2616 struct bridge_mcast_stats
*cpu_stats
= per_cpu_ptr(stats
, i
);
2617 struct br_mcast_stats temp
;
2621 start
= u64_stats_fetch_begin_irq(&cpu_stats
->syncp
);
2622 memcpy(&temp
, &cpu_stats
->mstats
, sizeof(temp
));
2623 } while (u64_stats_fetch_retry_irq(&cpu_stats
->syncp
, start
));
2625 mcast_stats_add_dir(tdst
.igmp_v1queries
, temp
.igmp_v1queries
);
2626 mcast_stats_add_dir(tdst
.igmp_v2queries
, temp
.igmp_v2queries
);
2627 mcast_stats_add_dir(tdst
.igmp_v3queries
, temp
.igmp_v3queries
);
2628 mcast_stats_add_dir(tdst
.igmp_leaves
, temp
.igmp_leaves
);
2629 mcast_stats_add_dir(tdst
.igmp_v1reports
, temp
.igmp_v1reports
);
2630 mcast_stats_add_dir(tdst
.igmp_v2reports
, temp
.igmp_v2reports
);
2631 mcast_stats_add_dir(tdst
.igmp_v3reports
, temp
.igmp_v3reports
);
2632 tdst
.igmp_parse_errors
+= temp
.igmp_parse_errors
;
2634 mcast_stats_add_dir(tdst
.mld_v1queries
, temp
.mld_v1queries
);
2635 mcast_stats_add_dir(tdst
.mld_v2queries
, temp
.mld_v2queries
);
2636 mcast_stats_add_dir(tdst
.mld_leaves
, temp
.mld_leaves
);
2637 mcast_stats_add_dir(tdst
.mld_v1reports
, temp
.mld_v1reports
);
2638 mcast_stats_add_dir(tdst
.mld_v2reports
, temp
.mld_v2reports
);
2639 tdst
.mld_parse_errors
+= temp
.mld_parse_errors
;
2641 memcpy(dest
, &tdst
, sizeof(*dest
));