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 #if IS_ENABLED(CONFIG_IPV6)
33 #include <net/ip6_checksum.h>
34 #include <net/addrconf.h>
37 #include "br_private.h"
39 static void br_multicast_start_querier(struct net_bridge
*br
,
40 struct bridge_mcast_own_query
*query
);
41 static void br_multicast_add_router(struct net_bridge
*br
,
42 struct net_bridge_port
*port
);
43 static void br_ip4_multicast_leave_group(struct net_bridge
*br
,
44 struct net_bridge_port
*port
,
47 #if IS_ENABLED(CONFIG_IPV6)
48 static void br_ip6_multicast_leave_group(struct net_bridge
*br
,
49 struct net_bridge_port
*port
,
50 const struct in6_addr
*group
,
53 unsigned int br_mdb_rehash_seq
;
55 static inline int br_ip_equal(const struct br_ip
*a
, const struct br_ip
*b
)
57 if (a
->proto
!= b
->proto
)
63 return a
->u
.ip4
== b
->u
.ip4
;
64 #if IS_ENABLED(CONFIG_IPV6)
65 case htons(ETH_P_IPV6
):
66 return ipv6_addr_equal(&a
->u
.ip6
, &b
->u
.ip6
);
72 static inline int __br_ip4_hash(struct net_bridge_mdb_htable
*mdb
, __be32 ip
,
75 return jhash_2words((__force u32
)ip
, vid
, mdb
->secret
) & (mdb
->max
- 1);
78 #if IS_ENABLED(CONFIG_IPV6)
79 static inline int __br_ip6_hash(struct net_bridge_mdb_htable
*mdb
,
80 const struct in6_addr
*ip
,
83 return jhash_2words(ipv6_addr_hash(ip
), vid
,
84 mdb
->secret
) & (mdb
->max
- 1);
88 static inline int br_ip_hash(struct net_bridge_mdb_htable
*mdb
,
93 return __br_ip4_hash(mdb
, ip
->u
.ip4
, ip
->vid
);
94 #if IS_ENABLED(CONFIG_IPV6)
95 case htons(ETH_P_IPV6
):
96 return __br_ip6_hash(mdb
, &ip
->u
.ip6
, ip
->vid
);
102 static struct net_bridge_mdb_entry
*__br_mdb_ip_get(
103 struct net_bridge_mdb_htable
*mdb
, struct br_ip
*dst
, int hash
)
105 struct net_bridge_mdb_entry
*mp
;
107 hlist_for_each_entry_rcu(mp
, &mdb
->mhash
[hash
], hlist
[mdb
->ver
]) {
108 if (br_ip_equal(&mp
->addr
, dst
))
115 struct net_bridge_mdb_entry
*br_mdb_ip_get(struct net_bridge_mdb_htable
*mdb
,
121 return __br_mdb_ip_get(mdb
, dst
, br_ip_hash(mdb
, dst
));
124 static struct net_bridge_mdb_entry
*br_mdb_ip4_get(
125 struct net_bridge_mdb_htable
*mdb
, __be32 dst
, __u16 vid
)
130 br_dst
.proto
= htons(ETH_P_IP
);
133 return br_mdb_ip_get(mdb
, &br_dst
);
136 #if IS_ENABLED(CONFIG_IPV6)
137 static struct net_bridge_mdb_entry
*br_mdb_ip6_get(
138 struct net_bridge_mdb_htable
*mdb
, const struct in6_addr
*dst
,
144 br_dst
.proto
= htons(ETH_P_IPV6
);
147 return br_mdb_ip_get(mdb
, &br_dst
);
151 struct net_bridge_mdb_entry
*br_mdb_get(struct net_bridge
*br
,
152 struct sk_buff
*skb
, u16 vid
)
154 struct net_bridge_mdb_htable
*mdb
= rcu_dereference(br
->mdb
);
157 if (br
->multicast_disabled
)
160 if (BR_INPUT_SKB_CB(skb
)->igmp
)
163 ip
.proto
= skb
->protocol
;
166 switch (skb
->protocol
) {
167 case htons(ETH_P_IP
):
168 ip
.u
.ip4
= ip_hdr(skb
)->daddr
;
170 #if IS_ENABLED(CONFIG_IPV6)
171 case htons(ETH_P_IPV6
):
172 ip
.u
.ip6
= ipv6_hdr(skb
)->daddr
;
179 return br_mdb_ip_get(mdb
, &ip
);
182 static void br_mdb_free(struct rcu_head
*head
)
184 struct net_bridge_mdb_htable
*mdb
=
185 container_of(head
, struct net_bridge_mdb_htable
, rcu
);
186 struct net_bridge_mdb_htable
*old
= mdb
->old
;
193 static int br_mdb_copy(struct net_bridge_mdb_htable
*new,
194 struct net_bridge_mdb_htable
*old
,
197 struct net_bridge_mdb_entry
*mp
;
202 for (i
= 0; i
< old
->max
; i
++)
203 hlist_for_each_entry(mp
, &old
->mhash
[i
], hlist
[old
->ver
])
204 hlist_add_head(&mp
->hlist
[new->ver
],
205 &new->mhash
[br_ip_hash(new, &mp
->addr
)]);
211 for (i
= 0; i
< new->max
; i
++) {
213 hlist_for_each_entry(mp
, &new->mhash
[i
], hlist
[new->ver
])
219 return maxlen
> elasticity
? -EINVAL
: 0;
222 void br_multicast_free_pg(struct rcu_head
*head
)
224 struct net_bridge_port_group
*p
=
225 container_of(head
, struct net_bridge_port_group
, rcu
);
230 static void br_multicast_free_group(struct rcu_head
*head
)
232 struct net_bridge_mdb_entry
*mp
=
233 container_of(head
, struct net_bridge_mdb_entry
, rcu
);
238 static void br_multicast_group_expired(unsigned long data
)
240 struct net_bridge_mdb_entry
*mp
= (void *)data
;
241 struct net_bridge
*br
= mp
->br
;
242 struct net_bridge_mdb_htable
*mdb
;
244 spin_lock(&br
->multicast_lock
);
245 if (!netif_running(br
->dev
) || timer_pending(&mp
->timer
))
253 mdb
= mlock_dereference(br
->mdb
, br
);
255 hlist_del_rcu(&mp
->hlist
[mdb
->ver
]);
258 call_rcu_bh(&mp
->rcu
, br_multicast_free_group
);
261 spin_unlock(&br
->multicast_lock
);
264 static void br_multicast_del_pg(struct net_bridge
*br
,
265 struct net_bridge_port_group
*pg
)
267 struct net_bridge_mdb_htable
*mdb
;
268 struct net_bridge_mdb_entry
*mp
;
269 struct net_bridge_port_group
*p
;
270 struct net_bridge_port_group __rcu
**pp
;
272 mdb
= mlock_dereference(br
->mdb
, br
);
274 mp
= br_mdb_ip_get(mdb
, &pg
->addr
);
278 for (pp
= &mp
->ports
;
279 (p
= mlock_dereference(*pp
, br
)) != NULL
;
284 rcu_assign_pointer(*pp
, p
->next
);
285 hlist_del_init(&p
->mglist
);
286 del_timer(&p
->timer
);
287 br_mdb_notify(br
->dev
, p
->port
, &pg
->addr
, RTM_DELMDB
,
289 call_rcu_bh(&p
->rcu
, br_multicast_free_pg
);
291 if (!mp
->ports
&& !mp
->mglist
&&
292 netif_running(br
->dev
))
293 mod_timer(&mp
->timer
, jiffies
);
301 static void br_multicast_port_group_expired(unsigned long data
)
303 struct net_bridge_port_group
*pg
= (void *)data
;
304 struct net_bridge
*br
= pg
->port
->br
;
306 spin_lock(&br
->multicast_lock
);
307 if (!netif_running(br
->dev
) || timer_pending(&pg
->timer
) ||
308 hlist_unhashed(&pg
->mglist
) || pg
->flags
& MDB_PG_FLAGS_PERMANENT
)
311 br_multicast_del_pg(br
, pg
);
314 spin_unlock(&br
->multicast_lock
);
317 static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu
**mdbp
, int max
,
320 struct net_bridge_mdb_htable
*old
= rcu_dereference_protected(*mdbp
, 1);
321 struct net_bridge_mdb_htable
*mdb
;
324 mdb
= kmalloc(sizeof(*mdb
), GFP_ATOMIC
);
331 mdb
->mhash
= kzalloc(max
* sizeof(*mdb
->mhash
), GFP_ATOMIC
);
337 mdb
->size
= old
? old
->size
: 0;
338 mdb
->ver
= old
? old
->ver
^ 1 : 0;
340 if (!old
|| elasticity
)
341 get_random_bytes(&mdb
->secret
, sizeof(mdb
->secret
));
343 mdb
->secret
= old
->secret
;
348 err
= br_mdb_copy(mdb
, old
, elasticity
);
356 call_rcu_bh(&mdb
->rcu
, br_mdb_free
);
359 rcu_assign_pointer(*mdbp
, mdb
);
364 static struct sk_buff
*br_ip4_multicast_alloc_query(struct net_bridge
*br
,
368 struct igmpv3_query
*ihv3
;
369 size_t igmp_hdr_size
;
375 igmp_hdr_size
= sizeof(*ih
);
376 if (br
->multicast_igmp_version
== 3)
377 igmp_hdr_size
= sizeof(*ihv3
);
378 skb
= netdev_alloc_skb_ip_align(br
->dev
, sizeof(*eth
) + sizeof(*iph
) +
383 skb
->protocol
= htons(ETH_P_IP
);
385 skb_reset_mac_header(skb
);
388 ether_addr_copy(eth
->h_source
, br
->dev
->dev_addr
);
391 eth
->h_dest
[2] = 0x5e;
395 eth
->h_proto
= htons(ETH_P_IP
);
396 skb_put(skb
, sizeof(*eth
));
398 skb_set_network_header(skb
, skb
->len
);
404 iph
->tot_len
= htons(sizeof(*iph
) + igmp_hdr_size
+ 4);
406 iph
->frag_off
= htons(IP_DF
);
408 iph
->protocol
= IPPROTO_IGMP
;
409 iph
->saddr
= br
->multicast_query_use_ifaddr
?
410 inet_select_addr(br
->dev
, 0, RT_SCOPE_LINK
) : 0;
411 iph
->daddr
= htonl(INADDR_ALLHOSTS_GROUP
);
412 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
413 ((u8
*)&iph
[1])[1] = 4;
414 ((u8
*)&iph
[1])[2] = 0;
415 ((u8
*)&iph
[1])[3] = 0;
419 skb_set_transport_header(skb
, skb
->len
);
420 *igmp_type
= IGMP_HOST_MEMBERSHIP_QUERY
;
422 switch (br
->multicast_igmp_version
) {
425 ih
->type
= IGMP_HOST_MEMBERSHIP_QUERY
;
426 ih
->code
= (group
? br
->multicast_last_member_interval
:
427 br
->multicast_query_response_interval
) /
428 (HZ
/ IGMP_TIMER_SCALE
);
431 ih
->csum
= ip_compute_csum((void *)ih
, sizeof(*ih
));
434 ihv3
= igmpv3_query_hdr(skb
);
435 ihv3
->type
= IGMP_HOST_MEMBERSHIP_QUERY
;
436 ihv3
->code
= (group
? br
->multicast_last_member_interval
:
437 br
->multicast_query_response_interval
) /
438 (HZ
/ IGMP_TIMER_SCALE
);
440 ihv3
->qqic
= br
->multicast_query_interval
/ HZ
;
446 ihv3
->csum
= ip_compute_csum((void *)ihv3
, sizeof(*ihv3
));
450 skb_put(skb
, igmp_hdr_size
);
451 __skb_pull(skb
, sizeof(*eth
));
457 #if IS_ENABLED(CONFIG_IPV6)
458 static struct sk_buff
*br_ip6_multicast_alloc_query(struct net_bridge
*br
,
459 const struct in6_addr
*grp
,
462 struct mld2_query
*mld2q
;
463 unsigned long interval
;
464 struct ipv6hdr
*ip6h
;
465 struct mld_msg
*mldq
;
471 mld_hdr_size
= sizeof(*mldq
);
472 if (br
->multicast_mld_version
== 2)
473 mld_hdr_size
= sizeof(*mld2q
);
474 skb
= netdev_alloc_skb_ip_align(br
->dev
, sizeof(*eth
) + sizeof(*ip6h
) +
479 skb
->protocol
= htons(ETH_P_IPV6
);
481 /* Ethernet header */
482 skb_reset_mac_header(skb
);
485 ether_addr_copy(eth
->h_source
, br
->dev
->dev_addr
);
486 eth
->h_proto
= htons(ETH_P_IPV6
);
487 skb_put(skb
, sizeof(*eth
));
489 /* IPv6 header + HbH option */
490 skb_set_network_header(skb
, skb
->len
);
491 ip6h
= ipv6_hdr(skb
);
493 *(__force __be32
*)ip6h
= htonl(0x60000000);
494 ip6h
->payload_len
= htons(8 + mld_hdr_size
);
495 ip6h
->nexthdr
= IPPROTO_HOPOPTS
;
497 ipv6_addr_set(&ip6h
->daddr
, htonl(0xff020000), 0, 0, htonl(1));
498 if (ipv6_dev_get_saddr(dev_net(br
->dev
), br
->dev
, &ip6h
->daddr
, 0,
501 br
->has_ipv6_addr
= 0;
505 br
->has_ipv6_addr
= 1;
506 ipv6_eth_mc_map(&ip6h
->daddr
, eth
->h_dest
);
508 hopopt
= (u8
*)(ip6h
+ 1);
509 hopopt
[0] = IPPROTO_ICMPV6
; /* next hdr */
510 hopopt
[1] = 0; /* length of HbH */
511 hopopt
[2] = IPV6_TLV_ROUTERALERT
; /* Router Alert */
512 hopopt
[3] = 2; /* Length of RA Option */
513 hopopt
[4] = 0; /* Type = 0x0000 (MLD) */
515 hopopt
[6] = IPV6_TLV_PAD1
; /* Pad1 */
516 hopopt
[7] = IPV6_TLV_PAD1
; /* Pad1 */
518 skb_put(skb
, sizeof(*ip6h
) + 8);
521 skb_set_transport_header(skb
, skb
->len
);
522 interval
= ipv6_addr_any(grp
) ?
523 br
->multicast_query_response_interval
:
524 br
->multicast_last_member_interval
;
525 *igmp_type
= ICMPV6_MGM_QUERY
;
526 switch (br
->multicast_mld_version
) {
528 mldq
= (struct mld_msg
*)icmp6_hdr(skb
);
529 mldq
->mld_type
= ICMPV6_MGM_QUERY
;
532 mldq
->mld_maxdelay
= htons((u16
)jiffies_to_msecs(interval
));
533 mldq
->mld_reserved
= 0;
534 mldq
->mld_mca
= *grp
;
535 mldq
->mld_cksum
= csum_ipv6_magic(&ip6h
->saddr
, &ip6h
->daddr
,
536 sizeof(*mldq
), IPPROTO_ICMPV6
,
542 mld2q
= (struct mld2_query
*)icmp6_hdr(skb
);
543 mld2q
->mld2q_mrc
= ntohs((u16
)jiffies_to_msecs(interval
));
544 mld2q
->mld2q_type
= ICMPV6_MGM_QUERY
;
545 mld2q
->mld2q_code
= 0;
546 mld2q
->mld2q_cksum
= 0;
547 mld2q
->mld2q_resv1
= 0;
548 mld2q
->mld2q_resv2
= 0;
549 mld2q
->mld2q_suppress
= 0;
550 mld2q
->mld2q_qrv
= 2;
551 mld2q
->mld2q_nsrcs
= 0;
552 mld2q
->mld2q_qqic
= br
->multicast_query_interval
/ HZ
;
553 mld2q
->mld2q_mca
= *grp
;
554 mld2q
->mld2q_cksum
= csum_ipv6_magic(&ip6h
->saddr
, &ip6h
->daddr
,
562 skb_put(skb
, mld_hdr_size
);
564 __skb_pull(skb
, sizeof(*eth
));
571 static struct sk_buff
*br_multicast_alloc_query(struct net_bridge
*br
,
575 switch (addr
->proto
) {
576 case htons(ETH_P_IP
):
577 return br_ip4_multicast_alloc_query(br
, addr
->u
.ip4
, igmp_type
);
578 #if IS_ENABLED(CONFIG_IPV6)
579 case htons(ETH_P_IPV6
):
580 return br_ip6_multicast_alloc_query(br
, &addr
->u
.ip6
,
587 static struct net_bridge_mdb_entry
*br_multicast_get_group(
588 struct net_bridge
*br
, struct net_bridge_port
*port
,
589 struct br_ip
*group
, int hash
)
591 struct net_bridge_mdb_htable
*mdb
;
592 struct net_bridge_mdb_entry
*mp
;
593 unsigned int count
= 0;
598 mdb
= rcu_dereference_protected(br
->mdb
, 1);
599 hlist_for_each_entry(mp
, &mdb
->mhash
[hash
], hlist
[mdb
->ver
]) {
601 if (unlikely(br_ip_equal(group
, &mp
->addr
)))
608 if (unlikely(count
> br
->hash_elasticity
&& count
)) {
610 br_info(br
, "Multicast hash table "
611 "chain limit reached: %s\n",
612 port
? port
->dev
->name
: br
->dev
->name
);
614 elasticity
= br
->hash_elasticity
;
617 if (mdb
->size
>= max
) {
619 if (unlikely(max
> br
->hash_max
)) {
620 br_warn(br
, "Multicast hash table maximum of %d "
621 "reached, disabling snooping: %s\n",
623 port
? port
->dev
->name
: br
->dev
->name
);
626 br
->multicast_disabled
= 1;
631 if (max
> mdb
->max
|| elasticity
) {
634 br_info(br
, "Multicast hash table "
636 port
? port
->dev
->name
: br
->dev
->name
);
641 err
= br_mdb_rehash(&br
->mdb
, max
, elasticity
);
643 br_warn(br
, "Cannot rehash multicast "
644 "hash table, disabling snooping: %s, %d, %d\n",
645 port
? port
->dev
->name
: br
->dev
->name
,
661 struct net_bridge_mdb_entry
*br_multicast_new_group(struct net_bridge
*br
,
662 struct net_bridge_port
*p
,
665 struct net_bridge_mdb_htable
*mdb
;
666 struct net_bridge_mdb_entry
*mp
;
670 mdb
= rcu_dereference_protected(br
->mdb
, 1);
672 err
= br_mdb_rehash(&br
->mdb
, BR_HASH_SIZE
, 0);
678 hash
= br_ip_hash(mdb
, group
);
679 mp
= br_multicast_get_group(br
, p
, group
, hash
);
680 switch (PTR_ERR(mp
)) {
686 mdb
= rcu_dereference_protected(br
->mdb
, 1);
687 hash
= br_ip_hash(mdb
, group
);
694 mp
= kzalloc(sizeof(*mp
), GFP_ATOMIC
);
696 return ERR_PTR(-ENOMEM
);
700 setup_timer(&mp
->timer
, br_multicast_group_expired
,
703 hlist_add_head_rcu(&mp
->hlist
[mdb
->ver
], &mdb
->mhash
[hash
]);
710 struct net_bridge_port_group
*br_multicast_new_port_group(
711 struct net_bridge_port
*port
,
713 struct net_bridge_port_group __rcu
*next
,
716 struct net_bridge_port_group
*p
;
718 p
= kzalloc(sizeof(*p
), GFP_ATOMIC
);
725 rcu_assign_pointer(p
->next
, next
);
726 hlist_add_head(&p
->mglist
, &port
->mglist
);
727 setup_timer(&p
->timer
, br_multicast_port_group_expired
,
732 static int br_multicast_add_group(struct net_bridge
*br
,
733 struct net_bridge_port
*port
,
736 struct net_bridge_port_group __rcu
**pp
;
737 struct net_bridge_port_group
*p
;
738 struct net_bridge_mdb_entry
*mp
;
739 unsigned long now
= jiffies
;
742 spin_lock(&br
->multicast_lock
);
743 if (!netif_running(br
->dev
) ||
744 (port
&& port
->state
== BR_STATE_DISABLED
))
747 mp
= br_multicast_new_group(br
, port
, group
);
754 mod_timer(&mp
->timer
, now
+ br
->multicast_membership_interval
);
758 for (pp
= &mp
->ports
;
759 (p
= mlock_dereference(*pp
, br
)) != NULL
;
763 if ((unsigned long)p
->port
< (unsigned long)port
)
767 p
= br_multicast_new_port_group(port
, group
, *pp
, 0);
770 rcu_assign_pointer(*pp
, p
);
771 br_mdb_notify(br
->dev
, port
, group
, RTM_NEWMDB
, 0);
774 mod_timer(&p
->timer
, now
+ br
->multicast_membership_interval
);
779 spin_unlock(&br
->multicast_lock
);
783 static int br_ip4_multicast_add_group(struct net_bridge
*br
,
784 struct net_bridge_port
*port
,
788 struct br_ip br_group
;
790 if (ipv4_is_local_multicast(group
))
793 br_group
.u
.ip4
= group
;
794 br_group
.proto
= htons(ETH_P_IP
);
797 return br_multicast_add_group(br
, port
, &br_group
);
800 #if IS_ENABLED(CONFIG_IPV6)
801 static int br_ip6_multicast_add_group(struct net_bridge
*br
,
802 struct net_bridge_port
*port
,
803 const struct in6_addr
*group
,
806 struct br_ip br_group
;
808 if (ipv6_addr_is_ll_all_nodes(group
))
811 br_group
.u
.ip6
= *group
;
812 br_group
.proto
= htons(ETH_P_IPV6
);
815 return br_multicast_add_group(br
, port
, &br_group
);
819 static void br_multicast_router_expired(unsigned long data
)
821 struct net_bridge_port
*port
= (void *)data
;
822 struct net_bridge
*br
= port
->br
;
824 spin_lock(&br
->multicast_lock
);
825 if (port
->multicast_router
== MDB_RTR_TYPE_DISABLED
||
826 port
->multicast_router
== MDB_RTR_TYPE_PERM
||
827 timer_pending(&port
->multicast_router_timer
) ||
828 hlist_unhashed(&port
->rlist
))
831 hlist_del_init_rcu(&port
->rlist
);
832 br_rtr_notify(br
->dev
, port
, RTM_DELMDB
);
833 /* Don't allow timer refresh if the router expired */
834 if (port
->multicast_router
== MDB_RTR_TYPE_TEMP
)
835 port
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
838 spin_unlock(&br
->multicast_lock
);
841 static void br_multicast_local_router_expired(unsigned long data
)
845 static void br_multicast_querier_expired(struct net_bridge
*br
,
846 struct bridge_mcast_own_query
*query
)
848 spin_lock(&br
->multicast_lock
);
849 if (!netif_running(br
->dev
) || br
->multicast_disabled
)
852 br_multicast_start_querier(br
, query
);
855 spin_unlock(&br
->multicast_lock
);
858 static void br_ip4_multicast_querier_expired(unsigned long data
)
860 struct net_bridge
*br
= (void *)data
;
862 br_multicast_querier_expired(br
, &br
->ip4_own_query
);
865 #if IS_ENABLED(CONFIG_IPV6)
866 static void br_ip6_multicast_querier_expired(unsigned long data
)
868 struct net_bridge
*br
= (void *)data
;
870 br_multicast_querier_expired(br
, &br
->ip6_own_query
);
874 static void br_multicast_select_own_querier(struct net_bridge
*br
,
878 if (ip
->proto
== htons(ETH_P_IP
))
879 br
->ip4_querier
.addr
.u
.ip4
= ip_hdr(skb
)->saddr
;
880 #if IS_ENABLED(CONFIG_IPV6)
882 br
->ip6_querier
.addr
.u
.ip6
= ipv6_hdr(skb
)->saddr
;
886 static void __br_multicast_send_query(struct net_bridge
*br
,
887 struct net_bridge_port
*port
,
893 skb
= br_multicast_alloc_query(br
, ip
, &igmp_type
);
898 skb
->dev
= port
->dev
;
899 br_multicast_count(br
, port
, skb
, igmp_type
,
901 NF_HOOK(NFPROTO_BRIDGE
, NF_BR_LOCAL_OUT
,
902 dev_net(port
->dev
), NULL
, skb
, NULL
, skb
->dev
,
903 br_dev_queue_push_xmit
);
905 br_multicast_select_own_querier(br
, ip
, skb
);
906 br_multicast_count(br
, port
, skb
, igmp_type
,
912 static void br_multicast_send_query(struct net_bridge
*br
,
913 struct net_bridge_port
*port
,
914 struct bridge_mcast_own_query
*own_query
)
916 struct bridge_mcast_other_query
*other_query
= NULL
;
917 struct br_ip br_group
;
920 if (!netif_running(br
->dev
) || br
->multicast_disabled
||
921 !br
->multicast_querier
)
924 memset(&br_group
.u
, 0, sizeof(br_group
.u
));
926 if (port
? (own_query
== &port
->ip4_own_query
) :
927 (own_query
== &br
->ip4_own_query
)) {
928 other_query
= &br
->ip4_other_query
;
929 br_group
.proto
= htons(ETH_P_IP
);
930 #if IS_ENABLED(CONFIG_IPV6)
932 other_query
= &br
->ip6_other_query
;
933 br_group
.proto
= htons(ETH_P_IPV6
);
937 if (!other_query
|| timer_pending(&other_query
->timer
))
940 __br_multicast_send_query(br
, port
, &br_group
);
943 time
+= own_query
->startup_sent
< br
->multicast_startup_query_count
?
944 br
->multicast_startup_query_interval
:
945 br
->multicast_query_interval
;
946 mod_timer(&own_query
->timer
, time
);
950 br_multicast_port_query_expired(struct net_bridge_port
*port
,
951 struct bridge_mcast_own_query
*query
)
953 struct net_bridge
*br
= port
->br
;
955 spin_lock(&br
->multicast_lock
);
956 if (port
->state
== BR_STATE_DISABLED
||
957 port
->state
== BR_STATE_BLOCKING
)
960 if (query
->startup_sent
< br
->multicast_startup_query_count
)
961 query
->startup_sent
++;
963 br_multicast_send_query(port
->br
, port
, query
);
966 spin_unlock(&br
->multicast_lock
);
969 static void br_ip4_multicast_port_query_expired(unsigned long data
)
971 struct net_bridge_port
*port
= (void *)data
;
973 br_multicast_port_query_expired(port
, &port
->ip4_own_query
);
976 #if IS_ENABLED(CONFIG_IPV6)
977 static void br_ip6_multicast_port_query_expired(unsigned long data
)
979 struct net_bridge_port
*port
= (void *)data
;
981 br_multicast_port_query_expired(port
, &port
->ip6_own_query
);
985 int br_multicast_add_port(struct net_bridge_port
*port
)
987 port
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
989 setup_timer(&port
->multicast_router_timer
, br_multicast_router_expired
,
990 (unsigned long)port
);
991 setup_timer(&port
->ip4_own_query
.timer
,
992 br_ip4_multicast_port_query_expired
, (unsigned long)port
);
993 #if IS_ENABLED(CONFIG_IPV6)
994 setup_timer(&port
->ip6_own_query
.timer
,
995 br_ip6_multicast_port_query_expired
, (unsigned long)port
);
997 port
->mcast_stats
= netdev_alloc_pcpu_stats(struct bridge_mcast_stats
);
998 if (!port
->mcast_stats
)
1004 void br_multicast_del_port(struct net_bridge_port
*port
)
1006 struct net_bridge
*br
= port
->br
;
1007 struct net_bridge_port_group
*pg
;
1008 struct hlist_node
*n
;
1010 /* Take care of the remaining groups, only perm ones should be left */
1011 spin_lock_bh(&br
->multicast_lock
);
1012 hlist_for_each_entry_safe(pg
, n
, &port
->mglist
, mglist
)
1013 br_multicast_del_pg(br
, pg
);
1014 spin_unlock_bh(&br
->multicast_lock
);
1015 del_timer_sync(&port
->multicast_router_timer
);
1016 free_percpu(port
->mcast_stats
);
1019 static void br_multicast_enable(struct bridge_mcast_own_query
*query
)
1021 query
->startup_sent
= 0;
1023 if (try_to_del_timer_sync(&query
->timer
) >= 0 ||
1024 del_timer(&query
->timer
))
1025 mod_timer(&query
->timer
, jiffies
);
1028 static void __br_multicast_enable_port(struct net_bridge_port
*port
)
1030 struct net_bridge
*br
= port
->br
;
1032 if (br
->multicast_disabled
|| !netif_running(br
->dev
))
1035 br_multicast_enable(&port
->ip4_own_query
);
1036 #if IS_ENABLED(CONFIG_IPV6)
1037 br_multicast_enable(&port
->ip6_own_query
);
1039 if (port
->multicast_router
== MDB_RTR_TYPE_PERM
&&
1040 hlist_unhashed(&port
->rlist
))
1041 br_multicast_add_router(br
, port
);
1044 void br_multicast_enable_port(struct net_bridge_port
*port
)
1046 struct net_bridge
*br
= port
->br
;
1048 spin_lock(&br
->multicast_lock
);
1049 __br_multicast_enable_port(port
);
1050 spin_unlock(&br
->multicast_lock
);
1053 void br_multicast_disable_port(struct net_bridge_port
*port
)
1055 struct net_bridge
*br
= port
->br
;
1056 struct net_bridge_port_group
*pg
;
1057 struct hlist_node
*n
;
1059 spin_lock(&br
->multicast_lock
);
1060 hlist_for_each_entry_safe(pg
, n
, &port
->mglist
, mglist
)
1061 if (!(pg
->flags
& MDB_PG_FLAGS_PERMANENT
))
1062 br_multicast_del_pg(br
, pg
);
1064 if (!hlist_unhashed(&port
->rlist
)) {
1065 hlist_del_init_rcu(&port
->rlist
);
1066 br_rtr_notify(br
->dev
, port
, RTM_DELMDB
);
1067 /* Don't allow timer refresh if disabling */
1068 if (port
->multicast_router
== MDB_RTR_TYPE_TEMP
)
1069 port
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
1071 del_timer(&port
->multicast_router_timer
);
1072 del_timer(&port
->ip4_own_query
.timer
);
1073 #if IS_ENABLED(CONFIG_IPV6)
1074 del_timer(&port
->ip6_own_query
.timer
);
1076 spin_unlock(&br
->multicast_lock
);
1079 static int br_ip4_multicast_igmp3_report(struct net_bridge
*br
,
1080 struct net_bridge_port
*port
,
1081 struct sk_buff
*skb
,
1084 struct igmpv3_report
*ih
;
1085 struct igmpv3_grec
*grec
;
1093 ih
= igmpv3_report_hdr(skb
);
1094 num
= ntohs(ih
->ngrec
);
1095 len
= skb_transport_offset(skb
) + sizeof(*ih
);
1097 for (i
= 0; i
< num
; i
++) {
1098 len
+= sizeof(*grec
);
1099 if (!pskb_may_pull(skb
, len
))
1102 grec
= (void *)(skb
->data
+ len
- sizeof(*grec
));
1103 group
= grec
->grec_mca
;
1104 type
= grec
->grec_type
;
1106 len
+= ntohs(grec
->grec_nsrcs
) * 4;
1107 if (!pskb_may_pull(skb
, len
))
1110 /* We treat this as an IGMPv2 report for now. */
1112 case IGMPV3_MODE_IS_INCLUDE
:
1113 case IGMPV3_MODE_IS_EXCLUDE
:
1114 case IGMPV3_CHANGE_TO_INCLUDE
:
1115 case IGMPV3_CHANGE_TO_EXCLUDE
:
1116 case IGMPV3_ALLOW_NEW_SOURCES
:
1117 case IGMPV3_BLOCK_OLD_SOURCES
:
1124 if ((type
== IGMPV3_CHANGE_TO_INCLUDE
||
1125 type
== IGMPV3_MODE_IS_INCLUDE
) &&
1126 ntohs(grec
->grec_nsrcs
) == 0) {
1127 br_ip4_multicast_leave_group(br
, port
, group
, vid
);
1129 err
= br_ip4_multicast_add_group(br
, port
, group
, vid
);
1138 #if IS_ENABLED(CONFIG_IPV6)
1139 static int br_ip6_multicast_mld2_report(struct net_bridge
*br
,
1140 struct net_bridge_port
*port
,
1141 struct sk_buff
*skb
,
1144 struct icmp6hdr
*icmp6h
;
1145 struct mld2_grec
*grec
;
1151 if (!pskb_may_pull(skb
, sizeof(*icmp6h
)))
1154 icmp6h
= icmp6_hdr(skb
);
1155 num
= ntohs(icmp6h
->icmp6_dataun
.un_data16
[1]);
1156 len
= skb_transport_offset(skb
) + sizeof(*icmp6h
);
1158 for (i
= 0; i
< num
; i
++) {
1159 __be16
*nsrcs
, _nsrcs
;
1161 nsrcs
= skb_header_pointer(skb
,
1162 len
+ offsetof(struct mld2_grec
,
1164 sizeof(_nsrcs
), &_nsrcs
);
1168 if (!pskb_may_pull(skb
,
1169 len
+ sizeof(*grec
) +
1170 sizeof(struct in6_addr
) * ntohs(*nsrcs
)))
1173 grec
= (struct mld2_grec
*)(skb
->data
+ len
);
1174 len
+= sizeof(*grec
) +
1175 sizeof(struct in6_addr
) * ntohs(*nsrcs
);
1177 /* We treat these as MLDv1 reports for now. */
1178 switch (grec
->grec_type
) {
1179 case MLD2_MODE_IS_INCLUDE
:
1180 case MLD2_MODE_IS_EXCLUDE
:
1181 case MLD2_CHANGE_TO_INCLUDE
:
1182 case MLD2_CHANGE_TO_EXCLUDE
:
1183 case MLD2_ALLOW_NEW_SOURCES
:
1184 case MLD2_BLOCK_OLD_SOURCES
:
1191 if ((grec
->grec_type
== MLD2_CHANGE_TO_INCLUDE
||
1192 grec
->grec_type
== MLD2_MODE_IS_INCLUDE
) &&
1193 ntohs(*nsrcs
) == 0) {
1194 br_ip6_multicast_leave_group(br
, port
, &grec
->grec_mca
,
1197 err
= br_ip6_multicast_add_group(br
, port
,
1198 &grec
->grec_mca
, vid
);
1208 static bool br_ip4_multicast_select_querier(struct net_bridge
*br
,
1209 struct net_bridge_port
*port
,
1212 if (!timer_pending(&br
->ip4_own_query
.timer
) &&
1213 !timer_pending(&br
->ip4_other_query
.timer
))
1216 if (!br
->ip4_querier
.addr
.u
.ip4
)
1219 if (ntohl(saddr
) <= ntohl(br
->ip4_querier
.addr
.u
.ip4
))
1225 br
->ip4_querier
.addr
.u
.ip4
= saddr
;
1227 /* update protected by general multicast_lock by caller */
1228 rcu_assign_pointer(br
->ip4_querier
.port
, port
);
1233 #if IS_ENABLED(CONFIG_IPV6)
1234 static bool br_ip6_multicast_select_querier(struct net_bridge
*br
,
1235 struct net_bridge_port
*port
,
1236 struct in6_addr
*saddr
)
1238 if (!timer_pending(&br
->ip6_own_query
.timer
) &&
1239 !timer_pending(&br
->ip6_other_query
.timer
))
1242 if (ipv6_addr_cmp(saddr
, &br
->ip6_querier
.addr
.u
.ip6
) <= 0)
1248 br
->ip6_querier
.addr
.u
.ip6
= *saddr
;
1250 /* update protected by general multicast_lock by caller */
1251 rcu_assign_pointer(br
->ip6_querier
.port
, port
);
1257 static bool br_multicast_select_querier(struct net_bridge
*br
,
1258 struct net_bridge_port
*port
,
1259 struct br_ip
*saddr
)
1261 switch (saddr
->proto
) {
1262 case htons(ETH_P_IP
):
1263 return br_ip4_multicast_select_querier(br
, port
, saddr
->u
.ip4
);
1264 #if IS_ENABLED(CONFIG_IPV6)
1265 case htons(ETH_P_IPV6
):
1266 return br_ip6_multicast_select_querier(br
, port
, &saddr
->u
.ip6
);
1274 br_multicast_update_query_timer(struct net_bridge
*br
,
1275 struct bridge_mcast_other_query
*query
,
1276 unsigned long max_delay
)
1278 if (!timer_pending(&query
->timer
))
1279 query
->delay_time
= jiffies
+ max_delay
;
1281 mod_timer(&query
->timer
, jiffies
+ br
->multicast_querier_interval
);
1285 * Add port to router_list
1286 * list is maintained ordered by pointer value
1287 * and locked by br->multicast_lock and RCU
1289 static void br_multicast_add_router(struct net_bridge
*br
,
1290 struct net_bridge_port
*port
)
1292 struct net_bridge_port
*p
;
1293 struct hlist_node
*slot
= NULL
;
1295 if (!hlist_unhashed(&port
->rlist
))
1298 hlist_for_each_entry(p
, &br
->router_list
, rlist
) {
1299 if ((unsigned long) port
>= (unsigned long) p
)
1305 hlist_add_behind_rcu(&port
->rlist
, slot
);
1307 hlist_add_head_rcu(&port
->rlist
, &br
->router_list
);
1308 br_rtr_notify(br
->dev
, port
, RTM_NEWMDB
);
1311 static void br_multicast_mark_router(struct net_bridge
*br
,
1312 struct net_bridge_port
*port
)
1314 unsigned long now
= jiffies
;
1317 if (br
->multicast_router
== MDB_RTR_TYPE_TEMP_QUERY
)
1318 mod_timer(&br
->multicast_router_timer
,
1319 now
+ br
->multicast_querier_interval
);
1323 if (port
->multicast_router
== MDB_RTR_TYPE_DISABLED
||
1324 port
->multicast_router
== MDB_RTR_TYPE_PERM
)
1327 br_multicast_add_router(br
, port
);
1329 mod_timer(&port
->multicast_router_timer
,
1330 now
+ br
->multicast_querier_interval
);
1333 static void br_multicast_query_received(struct net_bridge
*br
,
1334 struct net_bridge_port
*port
,
1335 struct bridge_mcast_other_query
*query
,
1336 struct br_ip
*saddr
,
1337 unsigned long max_delay
)
1339 if (!br_multicast_select_querier(br
, port
, saddr
))
1342 br_multicast_update_query_timer(br
, query
, max_delay
);
1343 br_multicast_mark_router(br
, port
);
1346 static int br_ip4_multicast_query(struct net_bridge
*br
,
1347 struct net_bridge_port
*port
,
1348 struct sk_buff
*skb
,
1351 const struct iphdr
*iph
= ip_hdr(skb
);
1352 struct igmphdr
*ih
= igmp_hdr(skb
);
1353 struct net_bridge_mdb_entry
*mp
;
1354 struct igmpv3_query
*ih3
;
1355 struct net_bridge_port_group
*p
;
1356 struct net_bridge_port_group __rcu
**pp
;
1358 unsigned long max_delay
;
1359 unsigned long now
= jiffies
;
1360 unsigned int offset
= skb_transport_offset(skb
);
1364 spin_lock(&br
->multicast_lock
);
1365 if (!netif_running(br
->dev
) ||
1366 (port
&& port
->state
== BR_STATE_DISABLED
))
1371 if (skb
->len
== offset
+ sizeof(*ih
)) {
1372 max_delay
= ih
->code
* (HZ
/ IGMP_TIMER_SCALE
);
1375 max_delay
= 10 * HZ
;
1378 } else if (skb
->len
>= offset
+ sizeof(*ih3
)) {
1379 ih3
= igmpv3_query_hdr(skb
);
1383 max_delay
= ih3
->code
?
1384 IGMPV3_MRC(ih3
->code
) * (HZ
/ IGMP_TIMER_SCALE
) : 1;
1390 saddr
.proto
= htons(ETH_P_IP
);
1391 saddr
.u
.ip4
= iph
->saddr
;
1393 br_multicast_query_received(br
, port
, &br
->ip4_other_query
,
1398 mp
= br_mdb_ip4_get(mlock_dereference(br
->mdb
, br
), group
, vid
);
1402 max_delay
*= br
->multicast_last_member_count
;
1405 (timer_pending(&mp
->timer
) ?
1406 time_after(mp
->timer
.expires
, now
+ max_delay
) :
1407 try_to_del_timer_sync(&mp
->timer
) >= 0))
1408 mod_timer(&mp
->timer
, now
+ max_delay
);
1410 for (pp
= &mp
->ports
;
1411 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1413 if (timer_pending(&p
->timer
) ?
1414 time_after(p
->timer
.expires
, now
+ max_delay
) :
1415 try_to_del_timer_sync(&p
->timer
) >= 0)
1416 mod_timer(&p
->timer
, now
+ max_delay
);
1420 spin_unlock(&br
->multicast_lock
);
1424 #if IS_ENABLED(CONFIG_IPV6)
1425 static int br_ip6_multicast_query(struct net_bridge
*br
,
1426 struct net_bridge_port
*port
,
1427 struct sk_buff
*skb
,
1430 const struct ipv6hdr
*ip6h
= ipv6_hdr(skb
);
1431 struct mld_msg
*mld
;
1432 struct net_bridge_mdb_entry
*mp
;
1433 struct mld2_query
*mld2q
;
1434 struct net_bridge_port_group
*p
;
1435 struct net_bridge_port_group __rcu
**pp
;
1437 unsigned long max_delay
;
1438 unsigned long now
= jiffies
;
1439 unsigned int offset
= skb_transport_offset(skb
);
1440 const struct in6_addr
*group
= NULL
;
1441 bool is_general_query
;
1444 spin_lock(&br
->multicast_lock
);
1445 if (!netif_running(br
->dev
) ||
1446 (port
&& port
->state
== BR_STATE_DISABLED
))
1449 if (skb
->len
== offset
+ sizeof(*mld
)) {
1450 if (!pskb_may_pull(skb
, offset
+ sizeof(*mld
))) {
1454 mld
= (struct mld_msg
*) icmp6_hdr(skb
);
1455 max_delay
= msecs_to_jiffies(ntohs(mld
->mld_maxdelay
));
1457 group
= &mld
->mld_mca
;
1459 if (!pskb_may_pull(skb
, offset
+ sizeof(*mld2q
))) {
1463 mld2q
= (struct mld2_query
*)icmp6_hdr(skb
);
1464 if (!mld2q
->mld2q_nsrcs
)
1465 group
= &mld2q
->mld2q_mca
;
1467 max_delay
= max(msecs_to_jiffies(mldv2_mrc(mld2q
)), 1UL);
1470 is_general_query
= group
&& ipv6_addr_any(group
);
1472 if (is_general_query
) {
1473 saddr
.proto
= htons(ETH_P_IPV6
);
1474 saddr
.u
.ip6
= ip6h
->saddr
;
1476 br_multicast_query_received(br
, port
, &br
->ip6_other_query
,
1479 } else if (!group
) {
1483 mp
= br_mdb_ip6_get(mlock_dereference(br
->mdb
, br
), group
, vid
);
1487 max_delay
*= br
->multicast_last_member_count
;
1489 (timer_pending(&mp
->timer
) ?
1490 time_after(mp
->timer
.expires
, now
+ max_delay
) :
1491 try_to_del_timer_sync(&mp
->timer
) >= 0))
1492 mod_timer(&mp
->timer
, now
+ max_delay
);
1494 for (pp
= &mp
->ports
;
1495 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1497 if (timer_pending(&p
->timer
) ?
1498 time_after(p
->timer
.expires
, now
+ max_delay
) :
1499 try_to_del_timer_sync(&p
->timer
) >= 0)
1500 mod_timer(&p
->timer
, now
+ max_delay
);
1504 spin_unlock(&br
->multicast_lock
);
1510 br_multicast_leave_group(struct net_bridge
*br
,
1511 struct net_bridge_port
*port
,
1512 struct br_ip
*group
,
1513 struct bridge_mcast_other_query
*other_query
,
1514 struct bridge_mcast_own_query
*own_query
)
1516 struct net_bridge_mdb_htable
*mdb
;
1517 struct net_bridge_mdb_entry
*mp
;
1518 struct net_bridge_port_group
*p
;
1522 spin_lock(&br
->multicast_lock
);
1523 if (!netif_running(br
->dev
) ||
1524 (port
&& port
->state
== BR_STATE_DISABLED
))
1527 mdb
= mlock_dereference(br
->mdb
, br
);
1528 mp
= br_mdb_ip_get(mdb
, group
);
1532 if (port
&& (port
->flags
& BR_MULTICAST_FAST_LEAVE
)) {
1533 struct net_bridge_port_group __rcu
**pp
;
1535 for (pp
= &mp
->ports
;
1536 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1538 if (p
->port
!= port
)
1541 rcu_assign_pointer(*pp
, p
->next
);
1542 hlist_del_init(&p
->mglist
);
1543 del_timer(&p
->timer
);
1544 call_rcu_bh(&p
->rcu
, br_multicast_free_pg
);
1545 br_mdb_notify(br
->dev
, port
, group
, RTM_DELMDB
,
1548 if (!mp
->ports
&& !mp
->mglist
&&
1549 netif_running(br
->dev
))
1550 mod_timer(&mp
->timer
, jiffies
);
1555 if (timer_pending(&other_query
->timer
))
1558 if (br
->multicast_querier
) {
1559 __br_multicast_send_query(br
, port
, &mp
->addr
);
1561 time
= jiffies
+ br
->multicast_last_member_count
*
1562 br
->multicast_last_member_interval
;
1564 mod_timer(&own_query
->timer
, time
);
1566 for (p
= mlock_dereference(mp
->ports
, br
);
1568 p
= mlock_dereference(p
->next
, br
)) {
1569 if (p
->port
!= port
)
1572 if (!hlist_unhashed(&p
->mglist
) &&
1573 (timer_pending(&p
->timer
) ?
1574 time_after(p
->timer
.expires
, time
) :
1575 try_to_del_timer_sync(&p
->timer
) >= 0)) {
1576 mod_timer(&p
->timer
, time
);
1584 time
= now
+ br
->multicast_last_member_count
*
1585 br
->multicast_last_member_interval
;
1589 (timer_pending(&mp
->timer
) ?
1590 time_after(mp
->timer
.expires
, time
) :
1591 try_to_del_timer_sync(&mp
->timer
) >= 0)) {
1592 mod_timer(&mp
->timer
, time
);
1598 for (p
= mlock_dereference(mp
->ports
, br
);
1600 p
= mlock_dereference(p
->next
, br
)) {
1601 if (p
->port
!= port
)
1604 if (!hlist_unhashed(&p
->mglist
) &&
1605 (timer_pending(&p
->timer
) ?
1606 time_after(p
->timer
.expires
, time
) :
1607 try_to_del_timer_sync(&p
->timer
) >= 0)) {
1608 mod_timer(&p
->timer
, time
);
1614 spin_unlock(&br
->multicast_lock
);
1617 static void br_ip4_multicast_leave_group(struct net_bridge
*br
,
1618 struct net_bridge_port
*port
,
1622 struct br_ip br_group
;
1623 struct bridge_mcast_own_query
*own_query
;
1625 if (ipv4_is_local_multicast(group
))
1628 own_query
= port
? &port
->ip4_own_query
: &br
->ip4_own_query
;
1630 br_group
.u
.ip4
= group
;
1631 br_group
.proto
= htons(ETH_P_IP
);
1634 br_multicast_leave_group(br
, port
, &br_group
, &br
->ip4_other_query
,
1638 #if IS_ENABLED(CONFIG_IPV6)
1639 static void br_ip6_multicast_leave_group(struct net_bridge
*br
,
1640 struct net_bridge_port
*port
,
1641 const struct in6_addr
*group
,
1644 struct br_ip br_group
;
1645 struct bridge_mcast_own_query
*own_query
;
1647 if (ipv6_addr_is_ll_all_nodes(group
))
1650 own_query
= port
? &port
->ip6_own_query
: &br
->ip6_own_query
;
1652 br_group
.u
.ip6
= *group
;
1653 br_group
.proto
= htons(ETH_P_IPV6
);
1656 br_multicast_leave_group(br
, port
, &br_group
, &br
->ip6_other_query
,
1661 static void br_multicast_err_count(const struct net_bridge
*br
,
1662 const struct net_bridge_port
*p
,
1665 struct bridge_mcast_stats __percpu
*stats
;
1666 struct bridge_mcast_stats
*pstats
;
1668 if (!br
->multicast_stats_enabled
)
1672 stats
= p
->mcast_stats
;
1674 stats
= br
->mcast_stats
;
1675 if (WARN_ON(!stats
))
1678 pstats
= this_cpu_ptr(stats
);
1680 u64_stats_update_begin(&pstats
->syncp
);
1682 case htons(ETH_P_IP
):
1683 pstats
->mstats
.igmp_parse_errors
++;
1685 #if IS_ENABLED(CONFIG_IPV6)
1686 case htons(ETH_P_IPV6
):
1687 pstats
->mstats
.mld_parse_errors
++;
1691 u64_stats_update_end(&pstats
->syncp
);
1694 static void br_multicast_pim(struct net_bridge
*br
,
1695 struct net_bridge_port
*port
,
1696 const struct sk_buff
*skb
)
1698 unsigned int offset
= skb_transport_offset(skb
);
1699 struct pimhdr
*pimhdr
, _pimhdr
;
1701 pimhdr
= skb_header_pointer(skb
, offset
, sizeof(_pimhdr
), &_pimhdr
);
1702 if (!pimhdr
|| pim_hdr_version(pimhdr
) != PIM_VERSION
||
1703 pim_hdr_type(pimhdr
) != PIM_TYPE_HELLO
)
1706 br_multicast_mark_router(br
, port
);
1709 static int br_multicast_ipv4_rcv(struct net_bridge
*br
,
1710 struct net_bridge_port
*port
,
1711 struct sk_buff
*skb
,
1714 struct sk_buff
*skb_trimmed
= NULL
;
1718 err
= ip_mc_check_igmp(skb
, &skb_trimmed
);
1720 if (err
== -ENOMSG
) {
1721 if (!ipv4_is_local_multicast(ip_hdr(skb
)->daddr
)) {
1722 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1723 } else if (pim_ipv4_all_pim_routers(ip_hdr(skb
)->daddr
)) {
1724 if (ip_hdr(skb
)->protocol
== IPPROTO_PIM
)
1725 br_multicast_pim(br
, port
, skb
);
1728 } else if (err
< 0) {
1729 br_multicast_err_count(br
, port
, skb
->protocol
);
1734 BR_INPUT_SKB_CB(skb
)->igmp
= ih
->type
;
1737 case IGMP_HOST_MEMBERSHIP_REPORT
:
1738 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1739 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1740 err
= br_ip4_multicast_add_group(br
, port
, ih
->group
, vid
);
1742 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1743 err
= br_ip4_multicast_igmp3_report(br
, port
, skb_trimmed
, vid
);
1745 case IGMP_HOST_MEMBERSHIP_QUERY
:
1746 err
= br_ip4_multicast_query(br
, port
, skb_trimmed
, vid
);
1748 case IGMP_HOST_LEAVE_MESSAGE
:
1749 br_ip4_multicast_leave_group(br
, port
, ih
->group
, vid
);
1753 if (skb_trimmed
&& skb_trimmed
!= skb
)
1754 kfree_skb(skb_trimmed
);
1756 br_multicast_count(br
, port
, skb
, BR_INPUT_SKB_CB(skb
)->igmp
,
1762 #if IS_ENABLED(CONFIG_IPV6)
1763 static int br_multicast_ipv6_rcv(struct net_bridge
*br
,
1764 struct net_bridge_port
*port
,
1765 struct sk_buff
*skb
,
1768 struct sk_buff
*skb_trimmed
= NULL
;
1769 struct mld_msg
*mld
;
1772 err
= ipv6_mc_check_mld(skb
, &skb_trimmed
);
1774 if (err
== -ENOMSG
) {
1775 if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb
)->daddr
))
1776 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1778 } else if (err
< 0) {
1779 br_multicast_err_count(br
, port
, skb
->protocol
);
1783 mld
= (struct mld_msg
*)skb_transport_header(skb
);
1784 BR_INPUT_SKB_CB(skb
)->igmp
= mld
->mld_type
;
1786 switch (mld
->mld_type
) {
1787 case ICMPV6_MGM_REPORT
:
1788 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1789 err
= br_ip6_multicast_add_group(br
, port
, &mld
->mld_mca
, vid
);
1791 case ICMPV6_MLD2_REPORT
:
1792 err
= br_ip6_multicast_mld2_report(br
, port
, skb_trimmed
, vid
);
1794 case ICMPV6_MGM_QUERY
:
1795 err
= br_ip6_multicast_query(br
, port
, skb_trimmed
, vid
);
1797 case ICMPV6_MGM_REDUCTION
:
1798 br_ip6_multicast_leave_group(br
, port
, &mld
->mld_mca
, vid
);
1802 if (skb_trimmed
&& skb_trimmed
!= skb
)
1803 kfree_skb(skb_trimmed
);
1805 br_multicast_count(br
, port
, skb
, BR_INPUT_SKB_CB(skb
)->igmp
,
1812 int br_multicast_rcv(struct net_bridge
*br
, struct net_bridge_port
*port
,
1813 struct sk_buff
*skb
, u16 vid
)
1817 BR_INPUT_SKB_CB(skb
)->igmp
= 0;
1818 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 0;
1820 if (br
->multicast_disabled
)
1823 switch (skb
->protocol
) {
1824 case htons(ETH_P_IP
):
1825 ret
= br_multicast_ipv4_rcv(br
, port
, skb
, vid
);
1827 #if IS_ENABLED(CONFIG_IPV6)
1828 case htons(ETH_P_IPV6
):
1829 ret
= br_multicast_ipv6_rcv(br
, port
, skb
, vid
);
1837 static void br_multicast_query_expired(struct net_bridge
*br
,
1838 struct bridge_mcast_own_query
*query
,
1839 struct bridge_mcast_querier
*querier
)
1841 spin_lock(&br
->multicast_lock
);
1842 if (query
->startup_sent
< br
->multicast_startup_query_count
)
1843 query
->startup_sent
++;
1845 RCU_INIT_POINTER(querier
->port
, NULL
);
1846 br_multicast_send_query(br
, NULL
, query
);
1847 spin_unlock(&br
->multicast_lock
);
1850 static void br_ip4_multicast_query_expired(unsigned long data
)
1852 struct net_bridge
*br
= (void *)data
;
1854 br_multicast_query_expired(br
, &br
->ip4_own_query
, &br
->ip4_querier
);
1857 #if IS_ENABLED(CONFIG_IPV6)
1858 static void br_ip6_multicast_query_expired(unsigned long data
)
1860 struct net_bridge
*br
= (void *)data
;
1862 br_multicast_query_expired(br
, &br
->ip6_own_query
, &br
->ip6_querier
);
1866 void br_multicast_init(struct net_bridge
*br
)
1868 br
->hash_elasticity
= 4;
1871 br
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
1872 br
->multicast_querier
= 0;
1873 br
->multicast_query_use_ifaddr
= 0;
1874 br
->multicast_last_member_count
= 2;
1875 br
->multicast_startup_query_count
= 2;
1877 br
->multicast_last_member_interval
= HZ
;
1878 br
->multicast_query_response_interval
= 10 * HZ
;
1879 br
->multicast_startup_query_interval
= 125 * HZ
/ 4;
1880 br
->multicast_query_interval
= 125 * HZ
;
1881 br
->multicast_querier_interval
= 255 * HZ
;
1882 br
->multicast_membership_interval
= 260 * HZ
;
1884 br
->ip4_other_query
.delay_time
= 0;
1885 br
->ip4_querier
.port
= NULL
;
1886 br
->multicast_igmp_version
= 2;
1887 #if IS_ENABLED(CONFIG_IPV6)
1888 br
->multicast_mld_version
= 1;
1889 br
->ip6_other_query
.delay_time
= 0;
1890 br
->ip6_querier
.port
= NULL
;
1892 br
->has_ipv6_addr
= 1;
1894 spin_lock_init(&br
->multicast_lock
);
1895 setup_timer(&br
->multicast_router_timer
,
1896 br_multicast_local_router_expired
, 0);
1897 setup_timer(&br
->ip4_other_query
.timer
,
1898 br_ip4_multicast_querier_expired
, (unsigned long)br
);
1899 setup_timer(&br
->ip4_own_query
.timer
, br_ip4_multicast_query_expired
,
1901 #if IS_ENABLED(CONFIG_IPV6)
1902 setup_timer(&br
->ip6_other_query
.timer
,
1903 br_ip6_multicast_querier_expired
, (unsigned long)br
);
1904 setup_timer(&br
->ip6_own_query
.timer
, br_ip6_multicast_query_expired
,
1909 static void __br_multicast_open(struct net_bridge
*br
,
1910 struct bridge_mcast_own_query
*query
)
1912 query
->startup_sent
= 0;
1914 if (br
->multicast_disabled
)
1917 mod_timer(&query
->timer
, jiffies
);
1920 void br_multicast_open(struct net_bridge
*br
)
1922 __br_multicast_open(br
, &br
->ip4_own_query
);
1923 #if IS_ENABLED(CONFIG_IPV6)
1924 __br_multicast_open(br
, &br
->ip6_own_query
);
1928 void br_multicast_stop(struct net_bridge
*br
)
1930 del_timer_sync(&br
->multicast_router_timer
);
1931 del_timer_sync(&br
->ip4_other_query
.timer
);
1932 del_timer_sync(&br
->ip4_own_query
.timer
);
1933 #if IS_ENABLED(CONFIG_IPV6)
1934 del_timer_sync(&br
->ip6_other_query
.timer
);
1935 del_timer_sync(&br
->ip6_own_query
.timer
);
1939 void br_multicast_dev_del(struct net_bridge
*br
)
1941 struct net_bridge_mdb_htable
*mdb
;
1942 struct net_bridge_mdb_entry
*mp
;
1943 struct hlist_node
*n
;
1947 spin_lock_bh(&br
->multicast_lock
);
1948 mdb
= mlock_dereference(br
->mdb
, br
);
1955 for (i
= 0; i
< mdb
->max
; i
++) {
1956 hlist_for_each_entry_safe(mp
, n
, &mdb
->mhash
[i
],
1958 del_timer(&mp
->timer
);
1959 call_rcu_bh(&mp
->rcu
, br_multicast_free_group
);
1964 spin_unlock_bh(&br
->multicast_lock
);
1966 spin_lock_bh(&br
->multicast_lock
);
1971 call_rcu_bh(&mdb
->rcu
, br_mdb_free
);
1974 spin_unlock_bh(&br
->multicast_lock
);
1976 free_percpu(br
->mcast_stats
);
1979 int br_multicast_set_router(struct net_bridge
*br
, unsigned long val
)
1983 spin_lock_bh(&br
->multicast_lock
);
1986 case MDB_RTR_TYPE_DISABLED
:
1987 case MDB_RTR_TYPE_PERM
:
1988 del_timer(&br
->multicast_router_timer
);
1990 case MDB_RTR_TYPE_TEMP_QUERY
:
1991 br
->multicast_router
= val
;
1996 spin_unlock_bh(&br
->multicast_lock
);
2001 static void __del_port_router(struct net_bridge_port
*p
)
2003 if (hlist_unhashed(&p
->rlist
))
2005 hlist_del_init_rcu(&p
->rlist
);
2006 br_rtr_notify(p
->br
->dev
, p
, RTM_DELMDB
);
2009 int br_multicast_set_port_router(struct net_bridge_port
*p
, unsigned long val
)
2011 struct net_bridge
*br
= p
->br
;
2012 unsigned long now
= jiffies
;
2015 spin_lock(&br
->multicast_lock
);
2016 if (p
->multicast_router
== val
) {
2017 /* Refresh the temp router port timer */
2018 if (p
->multicast_router
== MDB_RTR_TYPE_TEMP
)
2019 mod_timer(&p
->multicast_router_timer
,
2020 now
+ br
->multicast_querier_interval
);
2025 case MDB_RTR_TYPE_DISABLED
:
2026 p
->multicast_router
= MDB_RTR_TYPE_DISABLED
;
2027 __del_port_router(p
);
2028 del_timer(&p
->multicast_router_timer
);
2030 case MDB_RTR_TYPE_TEMP_QUERY
:
2031 p
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
2032 __del_port_router(p
);
2034 case MDB_RTR_TYPE_PERM
:
2035 p
->multicast_router
= MDB_RTR_TYPE_PERM
;
2036 del_timer(&p
->multicast_router_timer
);
2037 br_multicast_add_router(br
, p
);
2039 case MDB_RTR_TYPE_TEMP
:
2040 p
->multicast_router
= MDB_RTR_TYPE_TEMP
;
2041 br_multicast_mark_router(br
, p
);
2048 spin_unlock(&br
->multicast_lock
);
2053 static void br_multicast_start_querier(struct net_bridge
*br
,
2054 struct bridge_mcast_own_query
*query
)
2056 struct net_bridge_port
*port
;
2058 __br_multicast_open(br
, query
);
2060 list_for_each_entry(port
, &br
->port_list
, list
) {
2061 if (port
->state
== BR_STATE_DISABLED
||
2062 port
->state
== BR_STATE_BLOCKING
)
2065 if (query
== &br
->ip4_own_query
)
2066 br_multicast_enable(&port
->ip4_own_query
);
2067 #if IS_ENABLED(CONFIG_IPV6)
2069 br_multicast_enable(&port
->ip6_own_query
);
2074 int br_multicast_toggle(struct net_bridge
*br
, unsigned long val
)
2076 struct net_bridge_mdb_htable
*mdb
;
2077 struct net_bridge_port
*port
;
2080 spin_lock_bh(&br
->multicast_lock
);
2081 if (br
->multicast_disabled
== !val
)
2084 br
->multicast_disabled
= !val
;
2085 if (br
->multicast_disabled
)
2088 if (!netif_running(br
->dev
))
2091 mdb
= mlock_dereference(br
->mdb
, br
);
2096 br
->multicast_disabled
= !!val
;
2100 err
= br_mdb_rehash(&br
->mdb
, mdb
->max
,
2101 br
->hash_elasticity
);
2106 br_multicast_open(br
);
2107 list_for_each_entry(port
, &br
->port_list
, list
)
2108 __br_multicast_enable_port(port
);
2111 spin_unlock_bh(&br
->multicast_lock
);
2116 int br_multicast_set_querier(struct net_bridge
*br
, unsigned long val
)
2118 unsigned long max_delay
;
2122 spin_lock_bh(&br
->multicast_lock
);
2123 if (br
->multicast_querier
== val
)
2126 br
->multicast_querier
= val
;
2130 max_delay
= br
->multicast_query_response_interval
;
2132 if (!timer_pending(&br
->ip4_other_query
.timer
))
2133 br
->ip4_other_query
.delay_time
= jiffies
+ max_delay
;
2135 br_multicast_start_querier(br
, &br
->ip4_own_query
);
2137 #if IS_ENABLED(CONFIG_IPV6)
2138 if (!timer_pending(&br
->ip6_other_query
.timer
))
2139 br
->ip6_other_query
.delay_time
= jiffies
+ max_delay
;
2141 br_multicast_start_querier(br
, &br
->ip6_own_query
);
2145 spin_unlock_bh(&br
->multicast_lock
);
2150 int br_multicast_set_hash_max(struct net_bridge
*br
, unsigned long val
)
2154 struct net_bridge_mdb_htable
*mdb
;
2156 spin_lock_bh(&br
->multicast_lock
);
2157 if (!is_power_of_2(val
))
2160 mdb
= mlock_dereference(br
->mdb
, br
);
2161 if (mdb
&& val
< mdb
->size
)
2177 err
= br_mdb_rehash(&br
->mdb
, br
->hash_max
,
2178 br
->hash_elasticity
);
2184 spin_unlock_bh(&br
->multicast_lock
);
2189 int br_multicast_set_igmp_version(struct net_bridge
*br
, unsigned long val
)
2191 /* Currently we support only version 2 and 3 */
2200 spin_lock_bh(&br
->multicast_lock
);
2201 br
->multicast_igmp_version
= val
;
2202 spin_unlock_bh(&br
->multicast_lock
);
2207 #if IS_ENABLED(CONFIG_IPV6)
2208 int br_multicast_set_mld_version(struct net_bridge
*br
, unsigned long val
)
2210 /* Currently we support version 1 and 2 */
2219 spin_lock_bh(&br
->multicast_lock
);
2220 br
->multicast_mld_version
= val
;
2221 spin_unlock_bh(&br
->multicast_lock
);
2228 * br_multicast_list_adjacent - Returns snooped multicast addresses
2229 * @dev: The bridge port adjacent to which to retrieve addresses
2230 * @br_ip_list: The list to store found, snooped multicast IP addresses in
2232 * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
2233 * snooping feature on all bridge ports of dev's bridge device, excluding
2234 * the addresses from dev itself.
2236 * Returns the number of items added to br_ip_list.
2239 * - br_ip_list needs to be initialized by caller
2240 * - br_ip_list might contain duplicates in the end
2241 * (needs to be taken care of by caller)
2242 * - br_ip_list needs to be freed by caller
2244 int br_multicast_list_adjacent(struct net_device
*dev
,
2245 struct list_head
*br_ip_list
)
2247 struct net_bridge
*br
;
2248 struct net_bridge_port
*port
;
2249 struct net_bridge_port_group
*group
;
2250 struct br_ip_list
*entry
;
2254 if (!br_ip_list
|| !br_port_exists(dev
))
2257 port
= br_port_get_rcu(dev
);
2258 if (!port
|| !port
->br
)
2263 list_for_each_entry_rcu(port
, &br
->port_list
, list
) {
2264 if (!port
->dev
|| port
->dev
== dev
)
2267 hlist_for_each_entry_rcu(group
, &port
->mglist
, mglist
) {
2268 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
2272 entry
->addr
= group
->addr
;
2273 list_add(&entry
->list
, br_ip_list
);
2282 EXPORT_SYMBOL_GPL(br_multicast_list_adjacent
);
2285 * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2286 * @dev: The bridge port providing the bridge on which to check for a querier
2287 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2289 * Checks whether the given interface has a bridge on top and if so returns
2290 * true if a valid querier exists anywhere on the bridged link layer.
2291 * Otherwise returns false.
2293 bool br_multicast_has_querier_anywhere(struct net_device
*dev
, int proto
)
2295 struct net_bridge
*br
;
2296 struct net_bridge_port
*port
;
2301 if (!br_port_exists(dev
))
2304 port
= br_port_get_rcu(dev
);
2305 if (!port
|| !port
->br
)
2310 memset(ð
, 0, sizeof(eth
));
2311 eth
.h_proto
= htons(proto
);
2313 ret
= br_multicast_querier_exists(br
, ð
);
2319 EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere
);
2322 * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
2323 * @dev: The bridge port adjacent to which to check for a querier
2324 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2326 * Checks whether the given interface has a bridge on top and if so returns
2327 * true if a selected querier is behind one of the other ports of this
2328 * bridge. Otherwise returns false.
2330 bool br_multicast_has_querier_adjacent(struct net_device
*dev
, int proto
)
2332 struct net_bridge
*br
;
2333 struct net_bridge_port
*port
;
2337 if (!br_port_exists(dev
))
2340 port
= br_port_get_rcu(dev
);
2341 if (!port
|| !port
->br
)
2348 if (!timer_pending(&br
->ip4_other_query
.timer
) ||
2349 rcu_dereference(br
->ip4_querier
.port
) == port
)
2352 #if IS_ENABLED(CONFIG_IPV6)
2354 if (!timer_pending(&br
->ip6_other_query
.timer
) ||
2355 rcu_dereference(br
->ip6_querier
.port
) == port
)
2368 EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent
);
2370 static void br_mcast_stats_add(struct bridge_mcast_stats __percpu
*stats
,
2371 const struct sk_buff
*skb
, u8 type
, u8 dir
)
2373 struct bridge_mcast_stats
*pstats
= this_cpu_ptr(stats
);
2374 __be16 proto
= skb
->protocol
;
2377 u64_stats_update_begin(&pstats
->syncp
);
2379 case htons(ETH_P_IP
):
2380 t_len
= ntohs(ip_hdr(skb
)->tot_len
) - ip_hdrlen(skb
);
2382 case IGMP_HOST_MEMBERSHIP_REPORT
:
2383 pstats
->mstats
.igmp_v1reports
[dir
]++;
2385 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
2386 pstats
->mstats
.igmp_v2reports
[dir
]++;
2388 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
2389 pstats
->mstats
.igmp_v3reports
[dir
]++;
2391 case IGMP_HOST_MEMBERSHIP_QUERY
:
2392 if (t_len
!= sizeof(struct igmphdr
)) {
2393 pstats
->mstats
.igmp_v3queries
[dir
]++;
2395 unsigned int offset
= skb_transport_offset(skb
);
2396 struct igmphdr
*ih
, _ihdr
;
2398 ih
= skb_header_pointer(skb
, offset
,
2399 sizeof(_ihdr
), &_ihdr
);
2403 pstats
->mstats
.igmp_v1queries
[dir
]++;
2405 pstats
->mstats
.igmp_v2queries
[dir
]++;
2408 case IGMP_HOST_LEAVE_MESSAGE
:
2409 pstats
->mstats
.igmp_leaves
[dir
]++;
2413 #if IS_ENABLED(CONFIG_IPV6)
2414 case htons(ETH_P_IPV6
):
2415 t_len
= ntohs(ipv6_hdr(skb
)->payload_len
) +
2416 sizeof(struct ipv6hdr
);
2417 t_len
-= skb_network_header_len(skb
);
2419 case ICMPV6_MGM_REPORT
:
2420 pstats
->mstats
.mld_v1reports
[dir
]++;
2422 case ICMPV6_MLD2_REPORT
:
2423 pstats
->mstats
.mld_v2reports
[dir
]++;
2425 case ICMPV6_MGM_QUERY
:
2426 if (t_len
!= sizeof(struct mld_msg
))
2427 pstats
->mstats
.mld_v2queries
[dir
]++;
2429 pstats
->mstats
.mld_v1queries
[dir
]++;
2431 case ICMPV6_MGM_REDUCTION
:
2432 pstats
->mstats
.mld_leaves
[dir
]++;
2436 #endif /* CONFIG_IPV6 */
2438 u64_stats_update_end(&pstats
->syncp
);
2441 void br_multicast_count(struct net_bridge
*br
, const struct net_bridge_port
*p
,
2442 const struct sk_buff
*skb
, u8 type
, u8 dir
)
2444 struct bridge_mcast_stats __percpu
*stats
;
2446 /* if multicast_disabled is true then igmp type can't be set */
2447 if (!type
|| !br
->multicast_stats_enabled
)
2451 stats
= p
->mcast_stats
;
2453 stats
= br
->mcast_stats
;
2454 if (WARN_ON(!stats
))
2457 br_mcast_stats_add(stats
, skb
, type
, dir
);
2460 int br_multicast_init_stats(struct net_bridge
*br
)
2462 br
->mcast_stats
= netdev_alloc_pcpu_stats(struct bridge_mcast_stats
);
2463 if (!br
->mcast_stats
)
2469 static void mcast_stats_add_dir(u64
*dst
, u64
*src
)
2471 dst
[BR_MCAST_DIR_RX
] += src
[BR_MCAST_DIR_RX
];
2472 dst
[BR_MCAST_DIR_TX
] += src
[BR_MCAST_DIR_TX
];
2475 void br_multicast_get_stats(const struct net_bridge
*br
,
2476 const struct net_bridge_port
*p
,
2477 struct br_mcast_stats
*dest
)
2479 struct bridge_mcast_stats __percpu
*stats
;
2480 struct br_mcast_stats tdst
;
2483 memset(dest
, 0, sizeof(*dest
));
2485 stats
= p
->mcast_stats
;
2487 stats
= br
->mcast_stats
;
2488 if (WARN_ON(!stats
))
2491 memset(&tdst
, 0, sizeof(tdst
));
2492 for_each_possible_cpu(i
) {
2493 struct bridge_mcast_stats
*cpu_stats
= per_cpu_ptr(stats
, i
);
2494 struct br_mcast_stats temp
;
2498 start
= u64_stats_fetch_begin_irq(&cpu_stats
->syncp
);
2499 memcpy(&temp
, &cpu_stats
->mstats
, sizeof(temp
));
2500 } while (u64_stats_fetch_retry_irq(&cpu_stats
->syncp
, start
));
2502 mcast_stats_add_dir(tdst
.igmp_v1queries
, temp
.igmp_v1queries
);
2503 mcast_stats_add_dir(tdst
.igmp_v2queries
, temp
.igmp_v2queries
);
2504 mcast_stats_add_dir(tdst
.igmp_v3queries
, temp
.igmp_v3queries
);
2505 mcast_stats_add_dir(tdst
.igmp_leaves
, temp
.igmp_leaves
);
2506 mcast_stats_add_dir(tdst
.igmp_v1reports
, temp
.igmp_v1reports
);
2507 mcast_stats_add_dir(tdst
.igmp_v2reports
, temp
.igmp_v2reports
);
2508 mcast_stats_add_dir(tdst
.igmp_v3reports
, temp
.igmp_v3reports
);
2509 tdst
.igmp_parse_errors
+= temp
.igmp_parse_errors
;
2511 mcast_stats_add_dir(tdst
.mld_v1queries
, temp
.mld_v1queries
);
2512 mcast_stats_add_dir(tdst
.mld_v2queries
, temp
.mld_v2queries
);
2513 mcast_stats_add_dir(tdst
.mld_leaves
, temp
.mld_leaves
);
2514 mcast_stats_add_dir(tdst
.mld_v1reports
, temp
.mld_v1reports
);
2515 mcast_stats_add_dir(tdst
.mld_v2reports
, temp
.mld_v2reports
);
2516 tdst
.mld_parse_errors
+= temp
.mld_parse_errors
;
2518 memcpy(dest
, &tdst
, sizeof(*dest
));