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(unsigned long data
)
244 struct net_bridge_mdb_entry
*mp
= (void *)data
;
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
))
257 mdb
= mlock_dereference(br
->mdb
, br
);
259 hlist_del_rcu(&mp
->hlist
[mdb
->ver
]);
262 call_rcu_bh(&mp
->rcu
, br_multicast_free_group
);
265 spin_unlock(&br
->multicast_lock
);
268 static void br_multicast_del_pg(struct net_bridge
*br
,
269 struct net_bridge_port_group
*pg
)
271 struct net_bridge_mdb_htable
*mdb
;
272 struct net_bridge_mdb_entry
*mp
;
273 struct net_bridge_port_group
*p
;
274 struct net_bridge_port_group __rcu
**pp
;
276 mdb
= mlock_dereference(br
->mdb
, br
);
278 mp
= br_mdb_ip_get(mdb
, &pg
->addr
);
282 for (pp
= &mp
->ports
;
283 (p
= mlock_dereference(*pp
, br
)) != NULL
;
288 rcu_assign_pointer(*pp
, p
->next
);
289 hlist_del_init(&p
->mglist
);
290 del_timer(&p
->timer
);
291 br_mdb_notify(br
->dev
, p
->port
, &pg
->addr
, RTM_DELMDB
,
293 call_rcu_bh(&p
->rcu
, br_multicast_free_pg
);
295 if (!mp
->ports
&& !mp
->mglist
&&
296 netif_running(br
->dev
))
297 mod_timer(&mp
->timer
, jiffies
);
305 static void br_multicast_port_group_expired(unsigned long data
)
307 struct net_bridge_port_group
*pg
= (void *)data
;
308 struct net_bridge
*br
= pg
->port
->br
;
310 spin_lock(&br
->multicast_lock
);
311 if (!netif_running(br
->dev
) || timer_pending(&pg
->timer
) ||
312 hlist_unhashed(&pg
->mglist
) || pg
->flags
& MDB_PG_FLAGS_PERMANENT
)
315 br_multicast_del_pg(br
, pg
);
318 spin_unlock(&br
->multicast_lock
);
321 static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu
**mdbp
, int max
,
324 struct net_bridge_mdb_htable
*old
= rcu_dereference_protected(*mdbp
, 1);
325 struct net_bridge_mdb_htable
*mdb
;
328 mdb
= kmalloc(sizeof(*mdb
), GFP_ATOMIC
);
335 mdb
->mhash
= kzalloc(max
* sizeof(*mdb
->mhash
), GFP_ATOMIC
);
341 mdb
->size
= old
? old
->size
: 0;
342 mdb
->ver
= old
? old
->ver
^ 1 : 0;
344 if (!old
|| elasticity
)
345 get_random_bytes(&mdb
->secret
, sizeof(mdb
->secret
));
347 mdb
->secret
= old
->secret
;
352 err
= br_mdb_copy(mdb
, old
, elasticity
);
360 call_rcu_bh(&mdb
->rcu
, br_mdb_free
);
363 rcu_assign_pointer(*mdbp
, mdb
);
368 static struct sk_buff
*br_ip4_multicast_alloc_query(struct net_bridge
*br
,
372 struct igmpv3_query
*ihv3
;
373 size_t igmp_hdr_size
;
379 igmp_hdr_size
= sizeof(*ih
);
380 if (br
->multicast_igmp_version
== 3)
381 igmp_hdr_size
= sizeof(*ihv3
);
382 skb
= netdev_alloc_skb_ip_align(br
->dev
, sizeof(*eth
) + sizeof(*iph
) +
387 skb
->protocol
= htons(ETH_P_IP
);
389 skb_reset_mac_header(skb
);
392 ether_addr_copy(eth
->h_source
, br
->dev
->dev_addr
);
395 eth
->h_dest
[2] = 0x5e;
399 eth
->h_proto
= htons(ETH_P_IP
);
400 skb_put(skb
, sizeof(*eth
));
402 skb_set_network_header(skb
, skb
->len
);
408 iph
->tot_len
= htons(sizeof(*iph
) + igmp_hdr_size
+ 4);
410 iph
->frag_off
= htons(IP_DF
);
412 iph
->protocol
= IPPROTO_IGMP
;
413 iph
->saddr
= br
->multicast_query_use_ifaddr
?
414 inet_select_addr(br
->dev
, 0, RT_SCOPE_LINK
) : 0;
415 iph
->daddr
= htonl(INADDR_ALLHOSTS_GROUP
);
416 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
417 ((u8
*)&iph
[1])[1] = 4;
418 ((u8
*)&iph
[1])[2] = 0;
419 ((u8
*)&iph
[1])[3] = 0;
423 skb_set_transport_header(skb
, skb
->len
);
424 *igmp_type
= IGMP_HOST_MEMBERSHIP_QUERY
;
426 switch (br
->multicast_igmp_version
) {
429 ih
->type
= IGMP_HOST_MEMBERSHIP_QUERY
;
430 ih
->code
= (group
? br
->multicast_last_member_interval
:
431 br
->multicast_query_response_interval
) /
432 (HZ
/ IGMP_TIMER_SCALE
);
435 ih
->csum
= ip_compute_csum((void *)ih
, sizeof(*ih
));
438 ihv3
= igmpv3_query_hdr(skb
);
439 ihv3
->type
= IGMP_HOST_MEMBERSHIP_QUERY
;
440 ihv3
->code
= (group
? br
->multicast_last_member_interval
:
441 br
->multicast_query_response_interval
) /
442 (HZ
/ IGMP_TIMER_SCALE
);
444 ihv3
->qqic
= br
->multicast_query_interval
/ HZ
;
450 ihv3
->csum
= ip_compute_csum((void *)ihv3
, sizeof(*ihv3
));
454 skb_put(skb
, igmp_hdr_size
);
455 __skb_pull(skb
, sizeof(*eth
));
461 #if IS_ENABLED(CONFIG_IPV6)
462 static struct sk_buff
*br_ip6_multicast_alloc_query(struct net_bridge
*br
,
463 const struct in6_addr
*grp
,
466 struct mld2_query
*mld2q
;
467 unsigned long interval
;
468 struct ipv6hdr
*ip6h
;
469 struct mld_msg
*mldq
;
475 mld_hdr_size
= sizeof(*mldq
);
476 if (br
->multicast_mld_version
== 2)
477 mld_hdr_size
= sizeof(*mld2q
);
478 skb
= netdev_alloc_skb_ip_align(br
->dev
, sizeof(*eth
) + sizeof(*ip6h
) +
483 skb
->protocol
= htons(ETH_P_IPV6
);
485 /* Ethernet header */
486 skb_reset_mac_header(skb
);
489 ether_addr_copy(eth
->h_source
, br
->dev
->dev_addr
);
490 eth
->h_proto
= htons(ETH_P_IPV6
);
491 skb_put(skb
, sizeof(*eth
));
493 /* IPv6 header + HbH option */
494 skb_set_network_header(skb
, skb
->len
);
495 ip6h
= ipv6_hdr(skb
);
497 *(__force __be32
*)ip6h
= htonl(0x60000000);
498 ip6h
->payload_len
= htons(8 + mld_hdr_size
);
499 ip6h
->nexthdr
= IPPROTO_HOPOPTS
;
501 ipv6_addr_set(&ip6h
->daddr
, htonl(0xff020000), 0, 0, htonl(1));
502 if (ipv6_dev_get_saddr(dev_net(br
->dev
), br
->dev
, &ip6h
->daddr
, 0,
505 br
->has_ipv6_addr
= 0;
509 br
->has_ipv6_addr
= 1;
510 ipv6_eth_mc_map(&ip6h
->daddr
, eth
->h_dest
);
512 hopopt
= (u8
*)(ip6h
+ 1);
513 hopopt
[0] = IPPROTO_ICMPV6
; /* next hdr */
514 hopopt
[1] = 0; /* length of HbH */
515 hopopt
[2] = IPV6_TLV_ROUTERALERT
; /* Router Alert */
516 hopopt
[3] = 2; /* Length of RA Option */
517 hopopt
[4] = 0; /* Type = 0x0000 (MLD) */
519 hopopt
[6] = IPV6_TLV_PAD1
; /* Pad1 */
520 hopopt
[7] = IPV6_TLV_PAD1
; /* Pad1 */
522 skb_put(skb
, sizeof(*ip6h
) + 8);
525 skb_set_transport_header(skb
, skb
->len
);
526 interval
= ipv6_addr_any(grp
) ?
527 br
->multicast_query_response_interval
:
528 br
->multicast_last_member_interval
;
529 *igmp_type
= ICMPV6_MGM_QUERY
;
530 switch (br
->multicast_mld_version
) {
532 mldq
= (struct mld_msg
*)icmp6_hdr(skb
);
533 mldq
->mld_type
= ICMPV6_MGM_QUERY
;
536 mldq
->mld_maxdelay
= htons((u16
)jiffies_to_msecs(interval
));
537 mldq
->mld_reserved
= 0;
538 mldq
->mld_mca
= *grp
;
539 mldq
->mld_cksum
= csum_ipv6_magic(&ip6h
->saddr
, &ip6h
->daddr
,
540 sizeof(*mldq
), IPPROTO_ICMPV6
,
546 mld2q
= (struct mld2_query
*)icmp6_hdr(skb
);
547 mld2q
->mld2q_mrc
= htons((u16
)jiffies_to_msecs(interval
));
548 mld2q
->mld2q_type
= ICMPV6_MGM_QUERY
;
549 mld2q
->mld2q_code
= 0;
550 mld2q
->mld2q_cksum
= 0;
551 mld2q
->mld2q_resv1
= 0;
552 mld2q
->mld2q_resv2
= 0;
553 mld2q
->mld2q_suppress
= 0;
554 mld2q
->mld2q_qrv
= 2;
555 mld2q
->mld2q_nsrcs
= 0;
556 mld2q
->mld2q_qqic
= br
->multicast_query_interval
/ HZ
;
557 mld2q
->mld2q_mca
= *grp
;
558 mld2q
->mld2q_cksum
= csum_ipv6_magic(&ip6h
->saddr
, &ip6h
->daddr
,
566 skb_put(skb
, mld_hdr_size
);
568 __skb_pull(skb
, sizeof(*eth
));
575 static struct sk_buff
*br_multicast_alloc_query(struct net_bridge
*br
,
579 switch (addr
->proto
) {
580 case htons(ETH_P_IP
):
581 return br_ip4_multicast_alloc_query(br
, addr
->u
.ip4
, igmp_type
);
582 #if IS_ENABLED(CONFIG_IPV6)
583 case htons(ETH_P_IPV6
):
584 return br_ip6_multicast_alloc_query(br
, &addr
->u
.ip6
,
591 static struct net_bridge_mdb_entry
*br_multicast_get_group(
592 struct net_bridge
*br
, struct net_bridge_port
*port
,
593 struct br_ip
*group
, int hash
)
595 struct net_bridge_mdb_htable
*mdb
;
596 struct net_bridge_mdb_entry
*mp
;
597 unsigned int count
= 0;
602 mdb
= rcu_dereference_protected(br
->mdb
, 1);
603 hlist_for_each_entry(mp
, &mdb
->mhash
[hash
], hlist
[mdb
->ver
]) {
605 if (unlikely(br_ip_equal(group
, &mp
->addr
)))
612 if (unlikely(count
> br
->hash_elasticity
&& count
)) {
614 br_info(br
, "Multicast hash table "
615 "chain limit reached: %s\n",
616 port
? port
->dev
->name
: br
->dev
->name
);
618 elasticity
= br
->hash_elasticity
;
621 if (mdb
->size
>= max
) {
623 if (unlikely(max
> br
->hash_max
)) {
624 br_warn(br
, "Multicast hash table maximum of %d "
625 "reached, disabling snooping: %s\n",
627 port
? port
->dev
->name
: br
->dev
->name
);
630 br
->multicast_disabled
= 1;
635 if (max
> mdb
->max
|| elasticity
) {
638 br_info(br
, "Multicast hash table "
640 port
? port
->dev
->name
: br
->dev
->name
);
645 err
= br_mdb_rehash(&br
->mdb
, max
, elasticity
);
647 br_warn(br
, "Cannot rehash multicast "
648 "hash table, disabling snooping: %s, %d, %d\n",
649 port
? port
->dev
->name
: br
->dev
->name
,
665 struct net_bridge_mdb_entry
*br_multicast_new_group(struct net_bridge
*br
,
666 struct net_bridge_port
*p
,
669 struct net_bridge_mdb_htable
*mdb
;
670 struct net_bridge_mdb_entry
*mp
;
674 mdb
= rcu_dereference_protected(br
->mdb
, 1);
676 err
= br_mdb_rehash(&br
->mdb
, BR_HASH_SIZE
, 0);
682 hash
= br_ip_hash(mdb
, group
);
683 mp
= br_multicast_get_group(br
, p
, group
, hash
);
684 switch (PTR_ERR(mp
)) {
690 mdb
= rcu_dereference_protected(br
->mdb
, 1);
691 hash
= br_ip_hash(mdb
, group
);
698 mp
= kzalloc(sizeof(*mp
), GFP_ATOMIC
);
700 return ERR_PTR(-ENOMEM
);
704 setup_timer(&mp
->timer
, br_multicast_group_expired
,
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 setup_timer(&p
->timer
, br_multicast_port_group_expired
,
736 memcpy(p
->eth_addr
, src
, ETH_ALEN
);
738 memset(p
->eth_addr
, 0xff, ETH_ALEN
);
743 static bool br_port_group_equal(struct net_bridge_port_group
*p
,
744 struct net_bridge_port
*port
,
745 const unsigned char *src
)
750 if (!(port
->flags
& BR_MULTICAST_TO_UNICAST
))
753 return ether_addr_equal(src
, p
->eth_addr
);
756 static int br_multicast_add_group(struct net_bridge
*br
,
757 struct net_bridge_port
*port
,
759 const unsigned char *src
)
761 struct net_bridge_port_group __rcu
**pp
;
762 struct net_bridge_port_group
*p
;
763 struct net_bridge_mdb_entry
*mp
;
764 unsigned long now
= jiffies
;
767 spin_lock(&br
->multicast_lock
);
768 if (!netif_running(br
->dev
) ||
769 (port
&& port
->state
== BR_STATE_DISABLED
))
772 mp
= br_multicast_new_group(br
, port
, group
);
779 mod_timer(&mp
->timer
, now
+ br
->multicast_membership_interval
);
783 for (pp
= &mp
->ports
;
784 (p
= mlock_dereference(*pp
, br
)) != NULL
;
786 if (br_port_group_equal(p
, port
, src
))
788 if ((unsigned long)p
->port
< (unsigned long)port
)
792 p
= br_multicast_new_port_group(port
, group
, *pp
, 0, src
);
795 rcu_assign_pointer(*pp
, p
);
796 br_mdb_notify(br
->dev
, port
, group
, RTM_NEWMDB
, 0);
799 mod_timer(&p
->timer
, now
+ br
->multicast_membership_interval
);
804 spin_unlock(&br
->multicast_lock
);
808 static int br_ip4_multicast_add_group(struct net_bridge
*br
,
809 struct net_bridge_port
*port
,
812 const unsigned char *src
)
814 struct br_ip br_group
;
816 if (ipv4_is_local_multicast(group
))
819 br_group
.u
.ip4
= group
;
820 br_group
.proto
= htons(ETH_P_IP
);
823 return br_multicast_add_group(br
, port
, &br_group
, src
);
826 #if IS_ENABLED(CONFIG_IPV6)
827 static int br_ip6_multicast_add_group(struct net_bridge
*br
,
828 struct net_bridge_port
*port
,
829 const struct in6_addr
*group
,
831 const unsigned char *src
)
833 struct br_ip br_group
;
835 if (ipv6_addr_is_ll_all_nodes(group
))
838 br_group
.u
.ip6
= *group
;
839 br_group
.proto
= htons(ETH_P_IPV6
);
842 return br_multicast_add_group(br
, port
, &br_group
, src
);
846 static void br_multicast_router_expired(unsigned long data
)
848 struct net_bridge_port
*port
= (void *)data
;
849 struct net_bridge
*br
= port
->br
;
851 spin_lock(&br
->multicast_lock
);
852 if (port
->multicast_router
== MDB_RTR_TYPE_DISABLED
||
853 port
->multicast_router
== MDB_RTR_TYPE_PERM
||
854 timer_pending(&port
->multicast_router_timer
))
857 __del_port_router(port
);
859 spin_unlock(&br
->multicast_lock
);
862 static void br_multicast_local_router_expired(unsigned long data
)
866 static void br_multicast_querier_expired(struct net_bridge
*br
,
867 struct bridge_mcast_own_query
*query
)
869 spin_lock(&br
->multicast_lock
);
870 if (!netif_running(br
->dev
) || br
->multicast_disabled
)
873 br_multicast_start_querier(br
, query
);
876 spin_unlock(&br
->multicast_lock
);
879 static void br_ip4_multicast_querier_expired(unsigned long data
)
881 struct net_bridge
*br
= (void *)data
;
883 br_multicast_querier_expired(br
, &br
->ip4_own_query
);
886 #if IS_ENABLED(CONFIG_IPV6)
887 static void br_ip6_multicast_querier_expired(unsigned long data
)
889 struct net_bridge
*br
= (void *)data
;
891 br_multicast_querier_expired(br
, &br
->ip6_own_query
);
895 static void br_multicast_select_own_querier(struct net_bridge
*br
,
899 if (ip
->proto
== htons(ETH_P_IP
))
900 br
->ip4_querier
.addr
.u
.ip4
= ip_hdr(skb
)->saddr
;
901 #if IS_ENABLED(CONFIG_IPV6)
903 br
->ip6_querier
.addr
.u
.ip6
= ipv6_hdr(skb
)->saddr
;
907 static void __br_multicast_send_query(struct net_bridge
*br
,
908 struct net_bridge_port
*port
,
914 skb
= br_multicast_alloc_query(br
, ip
, &igmp_type
);
919 skb
->dev
= port
->dev
;
920 br_multicast_count(br
, port
, skb
, igmp_type
,
922 NF_HOOK(NFPROTO_BRIDGE
, NF_BR_LOCAL_OUT
,
923 dev_net(port
->dev
), NULL
, skb
, NULL
, skb
->dev
,
924 br_dev_queue_push_xmit
);
926 br_multicast_select_own_querier(br
, ip
, skb
);
927 br_multicast_count(br
, port
, skb
, igmp_type
,
933 static void br_multicast_send_query(struct net_bridge
*br
,
934 struct net_bridge_port
*port
,
935 struct bridge_mcast_own_query
*own_query
)
937 struct bridge_mcast_other_query
*other_query
= NULL
;
938 struct br_ip br_group
;
941 if (!netif_running(br
->dev
) || br
->multicast_disabled
||
942 !br
->multicast_querier
)
945 memset(&br_group
.u
, 0, sizeof(br_group
.u
));
947 if (port
? (own_query
== &port
->ip4_own_query
) :
948 (own_query
== &br
->ip4_own_query
)) {
949 other_query
= &br
->ip4_other_query
;
950 br_group
.proto
= htons(ETH_P_IP
);
951 #if IS_ENABLED(CONFIG_IPV6)
953 other_query
= &br
->ip6_other_query
;
954 br_group
.proto
= htons(ETH_P_IPV6
);
958 if (!other_query
|| timer_pending(&other_query
->timer
))
961 __br_multicast_send_query(br
, port
, &br_group
);
964 time
+= own_query
->startup_sent
< br
->multicast_startup_query_count
?
965 br
->multicast_startup_query_interval
:
966 br
->multicast_query_interval
;
967 mod_timer(&own_query
->timer
, time
);
971 br_multicast_port_query_expired(struct net_bridge_port
*port
,
972 struct bridge_mcast_own_query
*query
)
974 struct net_bridge
*br
= port
->br
;
976 spin_lock(&br
->multicast_lock
);
977 if (port
->state
== BR_STATE_DISABLED
||
978 port
->state
== BR_STATE_BLOCKING
)
981 if (query
->startup_sent
< br
->multicast_startup_query_count
)
982 query
->startup_sent
++;
984 br_multicast_send_query(port
->br
, port
, query
);
987 spin_unlock(&br
->multicast_lock
);
990 static void br_ip4_multicast_port_query_expired(unsigned long data
)
992 struct net_bridge_port
*port
= (void *)data
;
994 br_multicast_port_query_expired(port
, &port
->ip4_own_query
);
997 #if IS_ENABLED(CONFIG_IPV6)
998 static void br_ip6_multicast_port_query_expired(unsigned long data
)
1000 struct net_bridge_port
*port
= (void *)data
;
1002 br_multicast_port_query_expired(port
, &port
->ip6_own_query
);
1006 static void br_mc_disabled_update(struct net_device
*dev
, bool value
)
1008 struct switchdev_attr attr
= {
1010 .id
= SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED
,
1011 .flags
= SWITCHDEV_F_DEFER
,
1012 .u
.mc_disabled
= value
,
1015 switchdev_port_attr_set(dev
, &attr
);
1018 int br_multicast_add_port(struct net_bridge_port
*port
)
1020 port
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
1022 setup_timer(&port
->multicast_router_timer
, br_multicast_router_expired
,
1023 (unsigned long)port
);
1024 setup_timer(&port
->ip4_own_query
.timer
,
1025 br_ip4_multicast_port_query_expired
, (unsigned long)port
);
1026 #if IS_ENABLED(CONFIG_IPV6)
1027 setup_timer(&port
->ip6_own_query
.timer
,
1028 br_ip6_multicast_port_query_expired
, (unsigned long)port
);
1030 br_mc_disabled_update(port
->dev
, port
->br
->multicast_disabled
);
1032 port
->mcast_stats
= netdev_alloc_pcpu_stats(struct bridge_mcast_stats
);
1033 if (!port
->mcast_stats
)
1039 void br_multicast_del_port(struct net_bridge_port
*port
)
1041 struct net_bridge
*br
= port
->br
;
1042 struct net_bridge_port_group
*pg
;
1043 struct hlist_node
*n
;
1045 /* Take care of the remaining groups, only perm ones should be left */
1046 spin_lock_bh(&br
->multicast_lock
);
1047 hlist_for_each_entry_safe(pg
, n
, &port
->mglist
, mglist
)
1048 br_multicast_del_pg(br
, pg
);
1049 spin_unlock_bh(&br
->multicast_lock
);
1050 del_timer_sync(&port
->multicast_router_timer
);
1051 free_percpu(port
->mcast_stats
);
1054 static void br_multicast_enable(struct bridge_mcast_own_query
*query
)
1056 query
->startup_sent
= 0;
1058 if (try_to_del_timer_sync(&query
->timer
) >= 0 ||
1059 del_timer(&query
->timer
))
1060 mod_timer(&query
->timer
, jiffies
);
1063 static void __br_multicast_enable_port(struct net_bridge_port
*port
)
1065 struct net_bridge
*br
= port
->br
;
1067 if (br
->multicast_disabled
|| !netif_running(br
->dev
))
1070 br_multicast_enable(&port
->ip4_own_query
);
1071 #if IS_ENABLED(CONFIG_IPV6)
1072 br_multicast_enable(&port
->ip6_own_query
);
1074 if (port
->multicast_router
== MDB_RTR_TYPE_PERM
&&
1075 hlist_unhashed(&port
->rlist
))
1076 br_multicast_add_router(br
, port
);
1079 void br_multicast_enable_port(struct net_bridge_port
*port
)
1081 struct net_bridge
*br
= port
->br
;
1083 spin_lock(&br
->multicast_lock
);
1084 __br_multicast_enable_port(port
);
1085 spin_unlock(&br
->multicast_lock
);
1088 void br_multicast_disable_port(struct net_bridge_port
*port
)
1090 struct net_bridge
*br
= port
->br
;
1091 struct net_bridge_port_group
*pg
;
1092 struct hlist_node
*n
;
1094 spin_lock(&br
->multicast_lock
);
1095 hlist_for_each_entry_safe(pg
, n
, &port
->mglist
, mglist
)
1096 if (!(pg
->flags
& MDB_PG_FLAGS_PERMANENT
))
1097 br_multicast_del_pg(br
, pg
);
1099 __del_port_router(port
);
1101 del_timer(&port
->multicast_router_timer
);
1102 del_timer(&port
->ip4_own_query
.timer
);
1103 #if IS_ENABLED(CONFIG_IPV6)
1104 del_timer(&port
->ip6_own_query
.timer
);
1106 spin_unlock(&br
->multicast_lock
);
1109 static int br_ip4_multicast_igmp3_report(struct net_bridge
*br
,
1110 struct net_bridge_port
*port
,
1111 struct sk_buff
*skb
,
1114 const unsigned char *src
;
1115 struct igmpv3_report
*ih
;
1116 struct igmpv3_grec
*grec
;
1124 ih
= igmpv3_report_hdr(skb
);
1125 num
= ntohs(ih
->ngrec
);
1126 len
= skb_transport_offset(skb
) + sizeof(*ih
);
1128 for (i
= 0; i
< num
; i
++) {
1129 len
+= sizeof(*grec
);
1130 if (!pskb_may_pull(skb
, len
))
1133 grec
= (void *)(skb
->data
+ len
- sizeof(*grec
));
1134 group
= grec
->grec_mca
;
1135 type
= grec
->grec_type
;
1137 len
+= ntohs(grec
->grec_nsrcs
) * 4;
1138 if (!pskb_may_pull(skb
, len
))
1141 /* We treat this as an IGMPv2 report for now. */
1143 case IGMPV3_MODE_IS_INCLUDE
:
1144 case IGMPV3_MODE_IS_EXCLUDE
:
1145 case IGMPV3_CHANGE_TO_INCLUDE
:
1146 case IGMPV3_CHANGE_TO_EXCLUDE
:
1147 case IGMPV3_ALLOW_NEW_SOURCES
:
1148 case IGMPV3_BLOCK_OLD_SOURCES
:
1155 src
= eth_hdr(skb
)->h_source
;
1156 if ((type
== IGMPV3_CHANGE_TO_INCLUDE
||
1157 type
== IGMPV3_MODE_IS_INCLUDE
) &&
1158 ntohs(grec
->grec_nsrcs
) == 0) {
1159 br_ip4_multicast_leave_group(br
, port
, group
, vid
, src
);
1161 err
= br_ip4_multicast_add_group(br
, port
, group
, vid
,
1171 #if IS_ENABLED(CONFIG_IPV6)
1172 static int br_ip6_multicast_mld2_report(struct net_bridge
*br
,
1173 struct net_bridge_port
*port
,
1174 struct sk_buff
*skb
,
1177 const unsigned char *src
;
1178 struct icmp6hdr
*icmp6h
;
1179 struct mld2_grec
*grec
;
1185 if (!pskb_may_pull(skb
, sizeof(*icmp6h
)))
1188 icmp6h
= icmp6_hdr(skb
);
1189 num
= ntohs(icmp6h
->icmp6_dataun
.un_data16
[1]);
1190 len
= skb_transport_offset(skb
) + sizeof(*icmp6h
);
1192 for (i
= 0; i
< num
; i
++) {
1193 __be16
*nsrcs
, _nsrcs
;
1195 nsrcs
= skb_header_pointer(skb
,
1196 len
+ offsetof(struct mld2_grec
,
1198 sizeof(_nsrcs
), &_nsrcs
);
1202 if (!pskb_may_pull(skb
,
1203 len
+ sizeof(*grec
) +
1204 sizeof(struct in6_addr
) * ntohs(*nsrcs
)))
1207 grec
= (struct mld2_grec
*)(skb
->data
+ len
);
1208 len
+= sizeof(*grec
) +
1209 sizeof(struct in6_addr
) * ntohs(*nsrcs
);
1211 /* We treat these as MLDv1 reports for now. */
1212 switch (grec
->grec_type
) {
1213 case MLD2_MODE_IS_INCLUDE
:
1214 case MLD2_MODE_IS_EXCLUDE
:
1215 case MLD2_CHANGE_TO_INCLUDE
:
1216 case MLD2_CHANGE_TO_EXCLUDE
:
1217 case MLD2_ALLOW_NEW_SOURCES
:
1218 case MLD2_BLOCK_OLD_SOURCES
:
1225 src
= eth_hdr(skb
)->h_source
;
1226 if ((grec
->grec_type
== MLD2_CHANGE_TO_INCLUDE
||
1227 grec
->grec_type
== MLD2_MODE_IS_INCLUDE
) &&
1228 ntohs(*nsrcs
) == 0) {
1229 br_ip6_multicast_leave_group(br
, port
, &grec
->grec_mca
,
1232 err
= br_ip6_multicast_add_group(br
, port
,
1233 &grec
->grec_mca
, vid
,
1244 static bool br_ip4_multicast_select_querier(struct net_bridge
*br
,
1245 struct net_bridge_port
*port
,
1248 if (!timer_pending(&br
->ip4_own_query
.timer
) &&
1249 !timer_pending(&br
->ip4_other_query
.timer
))
1252 if (!br
->ip4_querier
.addr
.u
.ip4
)
1255 if (ntohl(saddr
) <= ntohl(br
->ip4_querier
.addr
.u
.ip4
))
1261 br
->ip4_querier
.addr
.u
.ip4
= saddr
;
1263 /* update protected by general multicast_lock by caller */
1264 rcu_assign_pointer(br
->ip4_querier
.port
, port
);
1269 #if IS_ENABLED(CONFIG_IPV6)
1270 static bool br_ip6_multicast_select_querier(struct net_bridge
*br
,
1271 struct net_bridge_port
*port
,
1272 struct in6_addr
*saddr
)
1274 if (!timer_pending(&br
->ip6_own_query
.timer
) &&
1275 !timer_pending(&br
->ip6_other_query
.timer
))
1278 if (ipv6_addr_cmp(saddr
, &br
->ip6_querier
.addr
.u
.ip6
) <= 0)
1284 br
->ip6_querier
.addr
.u
.ip6
= *saddr
;
1286 /* update protected by general multicast_lock by caller */
1287 rcu_assign_pointer(br
->ip6_querier
.port
, port
);
1293 static bool br_multicast_select_querier(struct net_bridge
*br
,
1294 struct net_bridge_port
*port
,
1295 struct br_ip
*saddr
)
1297 switch (saddr
->proto
) {
1298 case htons(ETH_P_IP
):
1299 return br_ip4_multicast_select_querier(br
, port
, saddr
->u
.ip4
);
1300 #if IS_ENABLED(CONFIG_IPV6)
1301 case htons(ETH_P_IPV6
):
1302 return br_ip6_multicast_select_querier(br
, port
, &saddr
->u
.ip6
);
1310 br_multicast_update_query_timer(struct net_bridge
*br
,
1311 struct bridge_mcast_other_query
*query
,
1312 unsigned long max_delay
)
1314 if (!timer_pending(&query
->timer
))
1315 query
->delay_time
= jiffies
+ max_delay
;
1317 mod_timer(&query
->timer
, jiffies
+ br
->multicast_querier_interval
);
1320 static void br_port_mc_router_state_change(struct net_bridge_port
*p
,
1323 struct switchdev_attr attr
= {
1325 .id
= SWITCHDEV_ATTR_ID_PORT_MROUTER
,
1326 .flags
= SWITCHDEV_F_DEFER
,
1327 .u
.mrouter
= is_mc_router
,
1330 switchdev_port_attr_set(p
->dev
, &attr
);
1334 * Add port to router_list
1335 * list is maintained ordered by pointer value
1336 * and locked by br->multicast_lock and RCU
1338 static void br_multicast_add_router(struct net_bridge
*br
,
1339 struct net_bridge_port
*port
)
1341 struct net_bridge_port
*p
;
1342 struct hlist_node
*slot
= NULL
;
1344 if (!hlist_unhashed(&port
->rlist
))
1347 hlist_for_each_entry(p
, &br
->router_list
, rlist
) {
1348 if ((unsigned long) port
>= (unsigned long) p
)
1354 hlist_add_behind_rcu(&port
->rlist
, slot
);
1356 hlist_add_head_rcu(&port
->rlist
, &br
->router_list
);
1357 br_rtr_notify(br
->dev
, port
, RTM_NEWMDB
);
1358 br_port_mc_router_state_change(port
, true);
1361 static void br_multicast_mark_router(struct net_bridge
*br
,
1362 struct net_bridge_port
*port
)
1364 unsigned long now
= jiffies
;
1367 if (br
->multicast_router
== MDB_RTR_TYPE_TEMP_QUERY
)
1368 mod_timer(&br
->multicast_router_timer
,
1369 now
+ br
->multicast_querier_interval
);
1373 if (port
->multicast_router
== MDB_RTR_TYPE_DISABLED
||
1374 port
->multicast_router
== MDB_RTR_TYPE_PERM
)
1377 br_multicast_add_router(br
, port
);
1379 mod_timer(&port
->multicast_router_timer
,
1380 now
+ br
->multicast_querier_interval
);
1383 static void br_multicast_query_received(struct net_bridge
*br
,
1384 struct net_bridge_port
*port
,
1385 struct bridge_mcast_other_query
*query
,
1386 struct br_ip
*saddr
,
1387 unsigned long max_delay
)
1389 if (!br_multicast_select_querier(br
, port
, saddr
))
1392 br_multicast_update_query_timer(br
, query
, max_delay
);
1393 br_multicast_mark_router(br
, port
);
1396 static int br_ip4_multicast_query(struct net_bridge
*br
,
1397 struct net_bridge_port
*port
,
1398 struct sk_buff
*skb
,
1401 const struct iphdr
*iph
= ip_hdr(skb
);
1402 struct igmphdr
*ih
= igmp_hdr(skb
);
1403 struct net_bridge_mdb_entry
*mp
;
1404 struct igmpv3_query
*ih3
;
1405 struct net_bridge_port_group
*p
;
1406 struct net_bridge_port_group __rcu
**pp
;
1408 unsigned long max_delay
;
1409 unsigned long now
= jiffies
;
1410 unsigned int offset
= skb_transport_offset(skb
);
1414 spin_lock(&br
->multicast_lock
);
1415 if (!netif_running(br
->dev
) ||
1416 (port
&& port
->state
== BR_STATE_DISABLED
))
1421 if (skb
->len
== offset
+ sizeof(*ih
)) {
1422 max_delay
= ih
->code
* (HZ
/ IGMP_TIMER_SCALE
);
1425 max_delay
= 10 * HZ
;
1428 } else if (skb
->len
>= offset
+ sizeof(*ih3
)) {
1429 ih3
= igmpv3_query_hdr(skb
);
1433 max_delay
= ih3
->code
?
1434 IGMPV3_MRC(ih3
->code
) * (HZ
/ IGMP_TIMER_SCALE
) : 1;
1440 saddr
.proto
= htons(ETH_P_IP
);
1441 saddr
.u
.ip4
= iph
->saddr
;
1443 br_multicast_query_received(br
, port
, &br
->ip4_other_query
,
1448 mp
= br_mdb_ip4_get(mlock_dereference(br
->mdb
, br
), group
, vid
);
1452 max_delay
*= br
->multicast_last_member_count
;
1455 (timer_pending(&mp
->timer
) ?
1456 time_after(mp
->timer
.expires
, now
+ max_delay
) :
1457 try_to_del_timer_sync(&mp
->timer
) >= 0))
1458 mod_timer(&mp
->timer
, now
+ max_delay
);
1460 for (pp
= &mp
->ports
;
1461 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1463 if (timer_pending(&p
->timer
) ?
1464 time_after(p
->timer
.expires
, now
+ max_delay
) :
1465 try_to_del_timer_sync(&p
->timer
) >= 0)
1466 mod_timer(&p
->timer
, now
+ max_delay
);
1470 spin_unlock(&br
->multicast_lock
);
1474 #if IS_ENABLED(CONFIG_IPV6)
1475 static int br_ip6_multicast_query(struct net_bridge
*br
,
1476 struct net_bridge_port
*port
,
1477 struct sk_buff
*skb
,
1480 const struct ipv6hdr
*ip6h
= ipv6_hdr(skb
);
1481 struct mld_msg
*mld
;
1482 struct net_bridge_mdb_entry
*mp
;
1483 struct mld2_query
*mld2q
;
1484 struct net_bridge_port_group
*p
;
1485 struct net_bridge_port_group __rcu
**pp
;
1487 unsigned long max_delay
;
1488 unsigned long now
= jiffies
;
1489 unsigned int offset
= skb_transport_offset(skb
);
1490 const struct in6_addr
*group
= NULL
;
1491 bool is_general_query
;
1494 spin_lock(&br
->multicast_lock
);
1495 if (!netif_running(br
->dev
) ||
1496 (port
&& port
->state
== BR_STATE_DISABLED
))
1499 if (skb
->len
== offset
+ sizeof(*mld
)) {
1500 if (!pskb_may_pull(skb
, offset
+ sizeof(*mld
))) {
1504 mld
= (struct mld_msg
*) icmp6_hdr(skb
);
1505 max_delay
= msecs_to_jiffies(ntohs(mld
->mld_maxdelay
));
1507 group
= &mld
->mld_mca
;
1509 if (!pskb_may_pull(skb
, offset
+ sizeof(*mld2q
))) {
1513 mld2q
= (struct mld2_query
*)icmp6_hdr(skb
);
1514 if (!mld2q
->mld2q_nsrcs
)
1515 group
= &mld2q
->mld2q_mca
;
1517 max_delay
= max(msecs_to_jiffies(mldv2_mrc(mld2q
)), 1UL);
1520 is_general_query
= group
&& ipv6_addr_any(group
);
1522 if (is_general_query
) {
1523 saddr
.proto
= htons(ETH_P_IPV6
);
1524 saddr
.u
.ip6
= ip6h
->saddr
;
1526 br_multicast_query_received(br
, port
, &br
->ip6_other_query
,
1529 } else if (!group
) {
1533 mp
= br_mdb_ip6_get(mlock_dereference(br
->mdb
, br
), group
, vid
);
1537 max_delay
*= br
->multicast_last_member_count
;
1539 (timer_pending(&mp
->timer
) ?
1540 time_after(mp
->timer
.expires
, now
+ max_delay
) :
1541 try_to_del_timer_sync(&mp
->timer
) >= 0))
1542 mod_timer(&mp
->timer
, now
+ max_delay
);
1544 for (pp
= &mp
->ports
;
1545 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1547 if (timer_pending(&p
->timer
) ?
1548 time_after(p
->timer
.expires
, now
+ max_delay
) :
1549 try_to_del_timer_sync(&p
->timer
) >= 0)
1550 mod_timer(&p
->timer
, now
+ max_delay
);
1554 spin_unlock(&br
->multicast_lock
);
1560 br_multicast_leave_group(struct net_bridge
*br
,
1561 struct net_bridge_port
*port
,
1562 struct br_ip
*group
,
1563 struct bridge_mcast_other_query
*other_query
,
1564 struct bridge_mcast_own_query
*own_query
,
1565 const unsigned char *src
)
1567 struct net_bridge_mdb_htable
*mdb
;
1568 struct net_bridge_mdb_entry
*mp
;
1569 struct net_bridge_port_group
*p
;
1573 spin_lock(&br
->multicast_lock
);
1574 if (!netif_running(br
->dev
) ||
1575 (port
&& port
->state
== BR_STATE_DISABLED
))
1578 mdb
= mlock_dereference(br
->mdb
, br
);
1579 mp
= br_mdb_ip_get(mdb
, group
);
1583 if (port
&& (port
->flags
& BR_MULTICAST_FAST_LEAVE
)) {
1584 struct net_bridge_port_group __rcu
**pp
;
1586 for (pp
= &mp
->ports
;
1587 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1589 if (!br_port_group_equal(p
, port
, src
))
1592 rcu_assign_pointer(*pp
, p
->next
);
1593 hlist_del_init(&p
->mglist
);
1594 del_timer(&p
->timer
);
1595 call_rcu_bh(&p
->rcu
, br_multicast_free_pg
);
1596 br_mdb_notify(br
->dev
, port
, group
, RTM_DELMDB
,
1599 if (!mp
->ports
&& !mp
->mglist
&&
1600 netif_running(br
->dev
))
1601 mod_timer(&mp
->timer
, jiffies
);
1606 if (timer_pending(&other_query
->timer
))
1609 if (br
->multicast_querier
) {
1610 __br_multicast_send_query(br
, port
, &mp
->addr
);
1612 time
= jiffies
+ br
->multicast_last_member_count
*
1613 br
->multicast_last_member_interval
;
1615 mod_timer(&own_query
->timer
, time
);
1617 for (p
= mlock_dereference(mp
->ports
, br
);
1619 p
= mlock_dereference(p
->next
, br
)) {
1620 if (!br_port_group_equal(p
, port
, src
))
1623 if (!hlist_unhashed(&p
->mglist
) &&
1624 (timer_pending(&p
->timer
) ?
1625 time_after(p
->timer
.expires
, time
) :
1626 try_to_del_timer_sync(&p
->timer
) >= 0)) {
1627 mod_timer(&p
->timer
, time
);
1635 time
= now
+ br
->multicast_last_member_count
*
1636 br
->multicast_last_member_interval
;
1640 (timer_pending(&mp
->timer
) ?
1641 time_after(mp
->timer
.expires
, time
) :
1642 try_to_del_timer_sync(&mp
->timer
) >= 0)) {
1643 mod_timer(&mp
->timer
, time
);
1649 for (p
= mlock_dereference(mp
->ports
, br
);
1651 p
= mlock_dereference(p
->next
, br
)) {
1652 if (p
->port
!= port
)
1655 if (!hlist_unhashed(&p
->mglist
) &&
1656 (timer_pending(&p
->timer
) ?
1657 time_after(p
->timer
.expires
, time
) :
1658 try_to_del_timer_sync(&p
->timer
) >= 0)) {
1659 mod_timer(&p
->timer
, time
);
1665 spin_unlock(&br
->multicast_lock
);
1668 static void br_ip4_multicast_leave_group(struct net_bridge
*br
,
1669 struct net_bridge_port
*port
,
1672 const unsigned char *src
)
1674 struct br_ip br_group
;
1675 struct bridge_mcast_own_query
*own_query
;
1677 if (ipv4_is_local_multicast(group
))
1680 own_query
= port
? &port
->ip4_own_query
: &br
->ip4_own_query
;
1682 br_group
.u
.ip4
= group
;
1683 br_group
.proto
= htons(ETH_P_IP
);
1686 br_multicast_leave_group(br
, port
, &br_group
, &br
->ip4_other_query
,
1690 #if IS_ENABLED(CONFIG_IPV6)
1691 static void br_ip6_multicast_leave_group(struct net_bridge
*br
,
1692 struct net_bridge_port
*port
,
1693 const struct in6_addr
*group
,
1695 const unsigned char *src
)
1697 struct br_ip br_group
;
1698 struct bridge_mcast_own_query
*own_query
;
1700 if (ipv6_addr_is_ll_all_nodes(group
))
1703 own_query
= port
? &port
->ip6_own_query
: &br
->ip6_own_query
;
1705 br_group
.u
.ip6
= *group
;
1706 br_group
.proto
= htons(ETH_P_IPV6
);
1709 br_multicast_leave_group(br
, port
, &br_group
, &br
->ip6_other_query
,
1714 static void br_multicast_err_count(const struct net_bridge
*br
,
1715 const struct net_bridge_port
*p
,
1718 struct bridge_mcast_stats __percpu
*stats
;
1719 struct bridge_mcast_stats
*pstats
;
1721 if (!br
->multicast_stats_enabled
)
1725 stats
= p
->mcast_stats
;
1727 stats
= br
->mcast_stats
;
1728 if (WARN_ON(!stats
))
1731 pstats
= this_cpu_ptr(stats
);
1733 u64_stats_update_begin(&pstats
->syncp
);
1735 case htons(ETH_P_IP
):
1736 pstats
->mstats
.igmp_parse_errors
++;
1738 #if IS_ENABLED(CONFIG_IPV6)
1739 case htons(ETH_P_IPV6
):
1740 pstats
->mstats
.mld_parse_errors
++;
1744 u64_stats_update_end(&pstats
->syncp
);
1747 static void br_multicast_pim(struct net_bridge
*br
,
1748 struct net_bridge_port
*port
,
1749 const struct sk_buff
*skb
)
1751 unsigned int offset
= skb_transport_offset(skb
);
1752 struct pimhdr
*pimhdr
, _pimhdr
;
1754 pimhdr
= skb_header_pointer(skb
, offset
, sizeof(_pimhdr
), &_pimhdr
);
1755 if (!pimhdr
|| pim_hdr_version(pimhdr
) != PIM_VERSION
||
1756 pim_hdr_type(pimhdr
) != PIM_TYPE_HELLO
)
1759 br_multicast_mark_router(br
, port
);
1762 static int br_multicast_ipv4_rcv(struct net_bridge
*br
,
1763 struct net_bridge_port
*port
,
1764 struct sk_buff
*skb
,
1767 struct sk_buff
*skb_trimmed
= NULL
;
1768 const unsigned char *src
;
1772 err
= ip_mc_check_igmp(skb
, &skb_trimmed
);
1774 if (err
== -ENOMSG
) {
1775 if (!ipv4_is_local_multicast(ip_hdr(skb
)->daddr
)) {
1776 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1777 } else if (pim_ipv4_all_pim_routers(ip_hdr(skb
)->daddr
)) {
1778 if (ip_hdr(skb
)->protocol
== IPPROTO_PIM
)
1779 br_multicast_pim(br
, port
, skb
);
1782 } else if (err
< 0) {
1783 br_multicast_err_count(br
, port
, skb
->protocol
);
1788 src
= eth_hdr(skb
)->h_source
;
1789 BR_INPUT_SKB_CB(skb
)->igmp
= ih
->type
;
1792 case IGMP_HOST_MEMBERSHIP_REPORT
:
1793 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1794 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1795 err
= br_ip4_multicast_add_group(br
, port
, ih
->group
, vid
, src
);
1797 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1798 err
= br_ip4_multicast_igmp3_report(br
, port
, skb_trimmed
, vid
);
1800 case IGMP_HOST_MEMBERSHIP_QUERY
:
1801 err
= br_ip4_multicast_query(br
, port
, skb_trimmed
, vid
);
1803 case IGMP_HOST_LEAVE_MESSAGE
:
1804 br_ip4_multicast_leave_group(br
, port
, ih
->group
, vid
, src
);
1808 if (skb_trimmed
&& skb_trimmed
!= skb
)
1809 kfree_skb(skb_trimmed
);
1811 br_multicast_count(br
, port
, skb
, BR_INPUT_SKB_CB(skb
)->igmp
,
1817 #if IS_ENABLED(CONFIG_IPV6)
1818 static int br_multicast_ipv6_rcv(struct net_bridge
*br
,
1819 struct net_bridge_port
*port
,
1820 struct sk_buff
*skb
,
1823 struct sk_buff
*skb_trimmed
= NULL
;
1824 const unsigned char *src
;
1825 struct mld_msg
*mld
;
1828 err
= ipv6_mc_check_mld(skb
, &skb_trimmed
);
1830 if (err
== -ENOMSG
) {
1831 if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb
)->daddr
))
1832 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1834 } else if (err
< 0) {
1835 br_multicast_err_count(br
, port
, skb
->protocol
);
1839 mld
= (struct mld_msg
*)skb_transport_header(skb
);
1840 BR_INPUT_SKB_CB(skb
)->igmp
= mld
->mld_type
;
1842 switch (mld
->mld_type
) {
1843 case ICMPV6_MGM_REPORT
:
1844 src
= eth_hdr(skb
)->h_source
;
1845 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1846 err
= br_ip6_multicast_add_group(br
, port
, &mld
->mld_mca
, vid
,
1849 case ICMPV6_MLD2_REPORT
:
1850 err
= br_ip6_multicast_mld2_report(br
, port
, skb_trimmed
, vid
);
1852 case ICMPV6_MGM_QUERY
:
1853 err
= br_ip6_multicast_query(br
, port
, skb_trimmed
, vid
);
1855 case ICMPV6_MGM_REDUCTION
:
1856 src
= eth_hdr(skb
)->h_source
;
1857 br_ip6_multicast_leave_group(br
, port
, &mld
->mld_mca
, vid
, src
);
1861 if (skb_trimmed
&& skb_trimmed
!= skb
)
1862 kfree_skb(skb_trimmed
);
1864 br_multicast_count(br
, port
, skb
, BR_INPUT_SKB_CB(skb
)->igmp
,
1871 int br_multicast_rcv(struct net_bridge
*br
, struct net_bridge_port
*port
,
1872 struct sk_buff
*skb
, u16 vid
)
1876 BR_INPUT_SKB_CB(skb
)->igmp
= 0;
1877 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 0;
1879 if (br
->multicast_disabled
)
1882 switch (skb
->protocol
) {
1883 case htons(ETH_P_IP
):
1884 ret
= br_multicast_ipv4_rcv(br
, port
, skb
, vid
);
1886 #if IS_ENABLED(CONFIG_IPV6)
1887 case htons(ETH_P_IPV6
):
1888 ret
= br_multicast_ipv6_rcv(br
, port
, skb
, vid
);
1896 static void br_multicast_query_expired(struct net_bridge
*br
,
1897 struct bridge_mcast_own_query
*query
,
1898 struct bridge_mcast_querier
*querier
)
1900 spin_lock(&br
->multicast_lock
);
1901 if (query
->startup_sent
< br
->multicast_startup_query_count
)
1902 query
->startup_sent
++;
1904 RCU_INIT_POINTER(querier
->port
, NULL
);
1905 br_multicast_send_query(br
, NULL
, query
);
1906 spin_unlock(&br
->multicast_lock
);
1909 static void br_ip4_multicast_query_expired(unsigned long data
)
1911 struct net_bridge
*br
= (void *)data
;
1913 br_multicast_query_expired(br
, &br
->ip4_own_query
, &br
->ip4_querier
);
1916 #if IS_ENABLED(CONFIG_IPV6)
1917 static void br_ip6_multicast_query_expired(unsigned long data
)
1919 struct net_bridge
*br
= (void *)data
;
1921 br_multicast_query_expired(br
, &br
->ip6_own_query
, &br
->ip6_querier
);
1925 void br_multicast_init(struct net_bridge
*br
)
1927 br
->hash_elasticity
= 4;
1930 br
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
1931 br
->multicast_querier
= 0;
1932 br
->multicast_query_use_ifaddr
= 0;
1933 br
->multicast_last_member_count
= 2;
1934 br
->multicast_startup_query_count
= 2;
1936 br
->multicast_last_member_interval
= HZ
;
1937 br
->multicast_query_response_interval
= 10 * HZ
;
1938 br
->multicast_startup_query_interval
= 125 * HZ
/ 4;
1939 br
->multicast_query_interval
= 125 * HZ
;
1940 br
->multicast_querier_interval
= 255 * HZ
;
1941 br
->multicast_membership_interval
= 260 * HZ
;
1943 br
->ip4_other_query
.delay_time
= 0;
1944 br
->ip4_querier
.port
= NULL
;
1945 br
->multicast_igmp_version
= 2;
1946 #if IS_ENABLED(CONFIG_IPV6)
1947 br
->multicast_mld_version
= 1;
1948 br
->ip6_other_query
.delay_time
= 0;
1949 br
->ip6_querier
.port
= NULL
;
1951 br
->has_ipv6_addr
= 1;
1953 spin_lock_init(&br
->multicast_lock
);
1954 setup_timer(&br
->multicast_router_timer
,
1955 br_multicast_local_router_expired
, 0);
1956 setup_timer(&br
->ip4_other_query
.timer
,
1957 br_ip4_multicast_querier_expired
, (unsigned long)br
);
1958 setup_timer(&br
->ip4_own_query
.timer
, br_ip4_multicast_query_expired
,
1960 #if IS_ENABLED(CONFIG_IPV6)
1961 setup_timer(&br
->ip6_other_query
.timer
,
1962 br_ip6_multicast_querier_expired
, (unsigned long)br
);
1963 setup_timer(&br
->ip6_own_query
.timer
, br_ip6_multicast_query_expired
,
1968 static void __br_multicast_open(struct net_bridge
*br
,
1969 struct bridge_mcast_own_query
*query
)
1971 query
->startup_sent
= 0;
1973 if (br
->multicast_disabled
)
1976 mod_timer(&query
->timer
, jiffies
);
1979 void br_multicast_open(struct net_bridge
*br
)
1981 __br_multicast_open(br
, &br
->ip4_own_query
);
1982 #if IS_ENABLED(CONFIG_IPV6)
1983 __br_multicast_open(br
, &br
->ip6_own_query
);
1987 void br_multicast_stop(struct net_bridge
*br
)
1989 del_timer_sync(&br
->multicast_router_timer
);
1990 del_timer_sync(&br
->ip4_other_query
.timer
);
1991 del_timer_sync(&br
->ip4_own_query
.timer
);
1992 #if IS_ENABLED(CONFIG_IPV6)
1993 del_timer_sync(&br
->ip6_other_query
.timer
);
1994 del_timer_sync(&br
->ip6_own_query
.timer
);
1998 void br_multicast_dev_del(struct net_bridge
*br
)
2000 struct net_bridge_mdb_htable
*mdb
;
2001 struct net_bridge_mdb_entry
*mp
;
2002 struct hlist_node
*n
;
2006 spin_lock_bh(&br
->multicast_lock
);
2007 mdb
= mlock_dereference(br
->mdb
, br
);
2014 for (i
= 0; i
< mdb
->max
; i
++) {
2015 hlist_for_each_entry_safe(mp
, n
, &mdb
->mhash
[i
],
2017 del_timer(&mp
->timer
);
2018 call_rcu_bh(&mp
->rcu
, br_multicast_free_group
);
2023 spin_unlock_bh(&br
->multicast_lock
);
2025 spin_lock_bh(&br
->multicast_lock
);
2030 call_rcu_bh(&mdb
->rcu
, br_mdb_free
);
2033 spin_unlock_bh(&br
->multicast_lock
);
2035 free_percpu(br
->mcast_stats
);
2038 int br_multicast_set_router(struct net_bridge
*br
, unsigned long val
)
2042 spin_lock_bh(&br
->multicast_lock
);
2045 case MDB_RTR_TYPE_DISABLED
:
2046 case MDB_RTR_TYPE_PERM
:
2047 del_timer(&br
->multicast_router_timer
);
2049 case MDB_RTR_TYPE_TEMP_QUERY
:
2050 br
->multicast_router
= val
;
2055 spin_unlock_bh(&br
->multicast_lock
);
2060 static void __del_port_router(struct net_bridge_port
*p
)
2062 if (hlist_unhashed(&p
->rlist
))
2064 hlist_del_init_rcu(&p
->rlist
);
2065 br_rtr_notify(p
->br
->dev
, p
, RTM_DELMDB
);
2066 br_port_mc_router_state_change(p
, false);
2068 /* don't allow timer refresh */
2069 if (p
->multicast_router
== MDB_RTR_TYPE_TEMP
)
2070 p
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
2073 int br_multicast_set_port_router(struct net_bridge_port
*p
, unsigned long val
)
2075 struct net_bridge
*br
= p
->br
;
2076 unsigned long now
= jiffies
;
2079 spin_lock(&br
->multicast_lock
);
2080 if (p
->multicast_router
== val
) {
2081 /* Refresh the temp router port timer */
2082 if (p
->multicast_router
== MDB_RTR_TYPE_TEMP
)
2083 mod_timer(&p
->multicast_router_timer
,
2084 now
+ br
->multicast_querier_interval
);
2089 case MDB_RTR_TYPE_DISABLED
:
2090 p
->multicast_router
= MDB_RTR_TYPE_DISABLED
;
2091 __del_port_router(p
);
2092 del_timer(&p
->multicast_router_timer
);
2094 case MDB_RTR_TYPE_TEMP_QUERY
:
2095 p
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
2096 __del_port_router(p
);
2098 case MDB_RTR_TYPE_PERM
:
2099 p
->multicast_router
= MDB_RTR_TYPE_PERM
;
2100 del_timer(&p
->multicast_router_timer
);
2101 br_multicast_add_router(br
, p
);
2103 case MDB_RTR_TYPE_TEMP
:
2104 p
->multicast_router
= MDB_RTR_TYPE_TEMP
;
2105 br_multicast_mark_router(br
, p
);
2112 spin_unlock(&br
->multicast_lock
);
2117 static void br_multicast_start_querier(struct net_bridge
*br
,
2118 struct bridge_mcast_own_query
*query
)
2120 struct net_bridge_port
*port
;
2122 __br_multicast_open(br
, query
);
2124 list_for_each_entry(port
, &br
->port_list
, list
) {
2125 if (port
->state
== BR_STATE_DISABLED
||
2126 port
->state
== BR_STATE_BLOCKING
)
2129 if (query
== &br
->ip4_own_query
)
2130 br_multicast_enable(&port
->ip4_own_query
);
2131 #if IS_ENABLED(CONFIG_IPV6)
2133 br_multicast_enable(&port
->ip6_own_query
);
2138 int br_multicast_toggle(struct net_bridge
*br
, unsigned long val
)
2140 struct net_bridge_mdb_htable
*mdb
;
2141 struct net_bridge_port
*port
;
2144 spin_lock_bh(&br
->multicast_lock
);
2145 if (br
->multicast_disabled
== !val
)
2148 br_mc_disabled_update(br
->dev
, !val
);
2149 br
->multicast_disabled
= !val
;
2150 if (br
->multicast_disabled
)
2153 if (!netif_running(br
->dev
))
2156 mdb
= mlock_dereference(br
->mdb
, br
);
2161 br
->multicast_disabled
= !!val
;
2165 err
= br_mdb_rehash(&br
->mdb
, mdb
->max
,
2166 br
->hash_elasticity
);
2171 br_multicast_open(br
);
2172 list_for_each_entry(port
, &br
->port_list
, list
)
2173 __br_multicast_enable_port(port
);
2176 spin_unlock_bh(&br
->multicast_lock
);
2181 int br_multicast_set_querier(struct net_bridge
*br
, unsigned long val
)
2183 unsigned long max_delay
;
2187 spin_lock_bh(&br
->multicast_lock
);
2188 if (br
->multicast_querier
== val
)
2191 br
->multicast_querier
= val
;
2195 max_delay
= br
->multicast_query_response_interval
;
2197 if (!timer_pending(&br
->ip4_other_query
.timer
))
2198 br
->ip4_other_query
.delay_time
= jiffies
+ max_delay
;
2200 br_multicast_start_querier(br
, &br
->ip4_own_query
);
2202 #if IS_ENABLED(CONFIG_IPV6)
2203 if (!timer_pending(&br
->ip6_other_query
.timer
))
2204 br
->ip6_other_query
.delay_time
= jiffies
+ max_delay
;
2206 br_multicast_start_querier(br
, &br
->ip6_own_query
);
2210 spin_unlock_bh(&br
->multicast_lock
);
2215 int br_multicast_set_hash_max(struct net_bridge
*br
, unsigned long val
)
2219 struct net_bridge_mdb_htable
*mdb
;
2221 spin_lock_bh(&br
->multicast_lock
);
2222 if (!is_power_of_2(val
))
2225 mdb
= mlock_dereference(br
->mdb
, br
);
2226 if (mdb
&& val
< mdb
->size
)
2242 err
= br_mdb_rehash(&br
->mdb
, br
->hash_max
,
2243 br
->hash_elasticity
);
2249 spin_unlock_bh(&br
->multicast_lock
);
2254 int br_multicast_set_igmp_version(struct net_bridge
*br
, unsigned long val
)
2256 /* Currently we support only version 2 and 3 */
2265 spin_lock_bh(&br
->multicast_lock
);
2266 br
->multicast_igmp_version
= val
;
2267 spin_unlock_bh(&br
->multicast_lock
);
2272 #if IS_ENABLED(CONFIG_IPV6)
2273 int br_multicast_set_mld_version(struct net_bridge
*br
, unsigned long val
)
2275 /* Currently we support version 1 and 2 */
2284 spin_lock_bh(&br
->multicast_lock
);
2285 br
->multicast_mld_version
= val
;
2286 spin_unlock_bh(&br
->multicast_lock
);
2293 * br_multicast_list_adjacent - Returns snooped multicast addresses
2294 * @dev: The bridge port adjacent to which to retrieve addresses
2295 * @br_ip_list: The list to store found, snooped multicast IP addresses in
2297 * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
2298 * snooping feature on all bridge ports of dev's bridge device, excluding
2299 * the addresses from dev itself.
2301 * Returns the number of items added to br_ip_list.
2304 * - br_ip_list needs to be initialized by caller
2305 * - br_ip_list might contain duplicates in the end
2306 * (needs to be taken care of by caller)
2307 * - br_ip_list needs to be freed by caller
2309 int br_multicast_list_adjacent(struct net_device
*dev
,
2310 struct list_head
*br_ip_list
)
2312 struct net_bridge
*br
;
2313 struct net_bridge_port
*port
;
2314 struct net_bridge_port_group
*group
;
2315 struct br_ip_list
*entry
;
2319 if (!br_ip_list
|| !br_port_exists(dev
))
2322 port
= br_port_get_rcu(dev
);
2323 if (!port
|| !port
->br
)
2328 list_for_each_entry_rcu(port
, &br
->port_list
, list
) {
2329 if (!port
->dev
|| port
->dev
== dev
)
2332 hlist_for_each_entry_rcu(group
, &port
->mglist
, mglist
) {
2333 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
2337 entry
->addr
= group
->addr
;
2338 list_add(&entry
->list
, br_ip_list
);
2347 EXPORT_SYMBOL_GPL(br_multicast_list_adjacent
);
2350 * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2351 * @dev: The bridge port providing the bridge on which to check for a querier
2352 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2354 * Checks whether the given interface has a bridge on top and if so returns
2355 * true if a valid querier exists anywhere on the bridged link layer.
2356 * Otherwise returns false.
2358 bool br_multicast_has_querier_anywhere(struct net_device
*dev
, int proto
)
2360 struct net_bridge
*br
;
2361 struct net_bridge_port
*port
;
2366 if (!br_port_exists(dev
))
2369 port
= br_port_get_rcu(dev
);
2370 if (!port
|| !port
->br
)
2375 memset(ð
, 0, sizeof(eth
));
2376 eth
.h_proto
= htons(proto
);
2378 ret
= br_multicast_querier_exists(br
, ð
);
2384 EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere
);
2387 * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
2388 * @dev: The bridge port adjacent to which to check for a querier
2389 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2391 * Checks whether the given interface has a bridge on top and if so returns
2392 * true if a selected querier is behind one of the other ports of this
2393 * bridge. Otherwise returns false.
2395 bool br_multicast_has_querier_adjacent(struct net_device
*dev
, int proto
)
2397 struct net_bridge
*br
;
2398 struct net_bridge_port
*port
;
2402 if (!br_port_exists(dev
))
2405 port
= br_port_get_rcu(dev
);
2406 if (!port
|| !port
->br
)
2413 if (!timer_pending(&br
->ip4_other_query
.timer
) ||
2414 rcu_dereference(br
->ip4_querier
.port
) == port
)
2417 #if IS_ENABLED(CONFIG_IPV6)
2419 if (!timer_pending(&br
->ip6_other_query
.timer
) ||
2420 rcu_dereference(br
->ip6_querier
.port
) == port
)
2433 EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent
);
2435 static void br_mcast_stats_add(struct bridge_mcast_stats __percpu
*stats
,
2436 const struct sk_buff
*skb
, u8 type
, u8 dir
)
2438 struct bridge_mcast_stats
*pstats
= this_cpu_ptr(stats
);
2439 __be16 proto
= skb
->protocol
;
2442 u64_stats_update_begin(&pstats
->syncp
);
2444 case htons(ETH_P_IP
):
2445 t_len
= ntohs(ip_hdr(skb
)->tot_len
) - ip_hdrlen(skb
);
2447 case IGMP_HOST_MEMBERSHIP_REPORT
:
2448 pstats
->mstats
.igmp_v1reports
[dir
]++;
2450 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
2451 pstats
->mstats
.igmp_v2reports
[dir
]++;
2453 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
2454 pstats
->mstats
.igmp_v3reports
[dir
]++;
2456 case IGMP_HOST_MEMBERSHIP_QUERY
:
2457 if (t_len
!= sizeof(struct igmphdr
)) {
2458 pstats
->mstats
.igmp_v3queries
[dir
]++;
2460 unsigned int offset
= skb_transport_offset(skb
);
2461 struct igmphdr
*ih
, _ihdr
;
2463 ih
= skb_header_pointer(skb
, offset
,
2464 sizeof(_ihdr
), &_ihdr
);
2468 pstats
->mstats
.igmp_v1queries
[dir
]++;
2470 pstats
->mstats
.igmp_v2queries
[dir
]++;
2473 case IGMP_HOST_LEAVE_MESSAGE
:
2474 pstats
->mstats
.igmp_leaves
[dir
]++;
2478 #if IS_ENABLED(CONFIG_IPV6)
2479 case htons(ETH_P_IPV6
):
2480 t_len
= ntohs(ipv6_hdr(skb
)->payload_len
) +
2481 sizeof(struct ipv6hdr
);
2482 t_len
-= skb_network_header_len(skb
);
2484 case ICMPV6_MGM_REPORT
:
2485 pstats
->mstats
.mld_v1reports
[dir
]++;
2487 case ICMPV6_MLD2_REPORT
:
2488 pstats
->mstats
.mld_v2reports
[dir
]++;
2490 case ICMPV6_MGM_QUERY
:
2491 if (t_len
!= sizeof(struct mld_msg
))
2492 pstats
->mstats
.mld_v2queries
[dir
]++;
2494 pstats
->mstats
.mld_v1queries
[dir
]++;
2496 case ICMPV6_MGM_REDUCTION
:
2497 pstats
->mstats
.mld_leaves
[dir
]++;
2501 #endif /* CONFIG_IPV6 */
2503 u64_stats_update_end(&pstats
->syncp
);
2506 void br_multicast_count(struct net_bridge
*br
, const struct net_bridge_port
*p
,
2507 const struct sk_buff
*skb
, u8 type
, u8 dir
)
2509 struct bridge_mcast_stats __percpu
*stats
;
2511 /* if multicast_disabled is true then igmp type can't be set */
2512 if (!type
|| !br
->multicast_stats_enabled
)
2516 stats
= p
->mcast_stats
;
2518 stats
= br
->mcast_stats
;
2519 if (WARN_ON(!stats
))
2522 br_mcast_stats_add(stats
, skb
, type
, dir
);
2525 int br_multicast_init_stats(struct net_bridge
*br
)
2527 br
->mcast_stats
= netdev_alloc_pcpu_stats(struct bridge_mcast_stats
);
2528 if (!br
->mcast_stats
)
2534 static void mcast_stats_add_dir(u64
*dst
, u64
*src
)
2536 dst
[BR_MCAST_DIR_RX
] += src
[BR_MCAST_DIR_RX
];
2537 dst
[BR_MCAST_DIR_TX
] += src
[BR_MCAST_DIR_TX
];
2540 void br_multicast_get_stats(const struct net_bridge
*br
,
2541 const struct net_bridge_port
*p
,
2542 struct br_mcast_stats
*dest
)
2544 struct bridge_mcast_stats __percpu
*stats
;
2545 struct br_mcast_stats tdst
;
2548 memset(dest
, 0, sizeof(*dest
));
2550 stats
= p
->mcast_stats
;
2552 stats
= br
->mcast_stats
;
2553 if (WARN_ON(!stats
))
2556 memset(&tdst
, 0, sizeof(tdst
));
2557 for_each_possible_cpu(i
) {
2558 struct bridge_mcast_stats
*cpu_stats
= per_cpu_ptr(stats
, i
);
2559 struct br_mcast_stats temp
;
2563 start
= u64_stats_fetch_begin_irq(&cpu_stats
->syncp
);
2564 memcpy(&temp
, &cpu_stats
->mstats
, sizeof(temp
));
2565 } while (u64_stats_fetch_retry_irq(&cpu_stats
->syncp
, start
));
2567 mcast_stats_add_dir(tdst
.igmp_v1queries
, temp
.igmp_v1queries
);
2568 mcast_stats_add_dir(tdst
.igmp_v2queries
, temp
.igmp_v2queries
);
2569 mcast_stats_add_dir(tdst
.igmp_v3queries
, temp
.igmp_v3queries
);
2570 mcast_stats_add_dir(tdst
.igmp_leaves
, temp
.igmp_leaves
);
2571 mcast_stats_add_dir(tdst
.igmp_v1reports
, temp
.igmp_v1reports
);
2572 mcast_stats_add_dir(tdst
.igmp_v2reports
, temp
.igmp_v2reports
);
2573 mcast_stats_add_dir(tdst
.igmp_v3reports
, temp
.igmp_v3reports
);
2574 tdst
.igmp_parse_errors
+= temp
.igmp_parse_errors
;
2576 mcast_stats_add_dir(tdst
.mld_v1queries
, temp
.mld_v1queries
);
2577 mcast_stats_add_dir(tdst
.mld_v2queries
, temp
.mld_v2queries
);
2578 mcast_stats_add_dir(tdst
.mld_leaves
, temp
.mld_leaves
);
2579 mcast_stats_add_dir(tdst
.mld_v1reports
, temp
.mld_v1reports
);
2580 mcast_stats_add_dir(tdst
.mld_v2reports
, temp
.mld_v2reports
);
2581 tdst
.mld_parse_errors
+= temp
.mld_parse_errors
;
2583 memcpy(dest
, &tdst
, sizeof(*dest
));