xref: /openbmc/linux/net/bridge/br_multicast.c (revision c005e2f62f8421b13b9a31adb9db7281f1a19e68)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2eb1d1641SHerbert Xu /*
3eb1d1641SHerbert Xu  * Bridge multicast support.
4eb1d1641SHerbert Xu  *
5eb1d1641SHerbert Xu  * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
6eb1d1641SHerbert Xu  */
7eb1d1641SHerbert Xu 
8eb1d1641SHerbert Xu #include <linux/err.h>
907f8ac4aSLinus Lüssing #include <linux/export.h>
10eb1d1641SHerbert Xu #include <linux/if_ether.h>
11eb1d1641SHerbert Xu #include <linux/igmp.h>
124b3087c7SLinus Lüssing #include <linux/in.h>
13eb1d1641SHerbert Xu #include <linux/jhash.h>
14eb1d1641SHerbert Xu #include <linux/kernel.h>
15b195167fSHerbert Xu #include <linux/log2.h>
16eb1d1641SHerbert Xu #include <linux/netdevice.h>
17eb1d1641SHerbert Xu #include <linux/netfilter_bridge.h>
18eb1d1641SHerbert Xu #include <linux/random.h>
19eb1d1641SHerbert Xu #include <linux/rculist.h>
20eb1d1641SHerbert Xu #include <linux/skbuff.h>
21eb1d1641SHerbert Xu #include <linux/slab.h>
22eb1d1641SHerbert Xu #include <linux/timer.h>
231c8ad5bfSCong Wang #include <linux/inetdevice.h>
2491b02d3dSNikolay Aleksandrov #include <linux/mroute.h>
25eb1d1641SHerbert Xu #include <net/ip.h>
26147c1e9bSNogah Frankel #include <net/switchdev.h>
27dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
284b3087c7SLinus Lüssing #include <linux/icmpv6.h>
2908b202b6SYOSHIFUJI Hideaki #include <net/ipv6.h>
3008b202b6SYOSHIFUJI Hideaki #include <net/mld.h>
31d4c4f07dSDavid S. Miller #include <net/ip6_checksum.h>
323c3769e6SLinus Lüssing #include <net/addrconf.h>
3308b202b6SYOSHIFUJI Hideaki #endif
34b57e8d87SPetr Machata #include <trace/events/bridge.h>
35eb1d1641SHerbert Xu 
36eb1d1641SHerbert Xu #include "br_private.h"
378f07b831SNikolay Aleksandrov #include "br_private_mcast_eht.h"
38eb1d1641SHerbert Xu 
3919e3a9c9SNikolay Aleksandrov static const struct rhashtable_params br_mdb_rht_params = {
4019e3a9c9SNikolay Aleksandrov 	.head_offset = offsetof(struct net_bridge_mdb_entry, rhnode),
4119e3a9c9SNikolay Aleksandrov 	.key_offset = offsetof(struct net_bridge_mdb_entry, addr),
4219e3a9c9SNikolay Aleksandrov 	.key_len = sizeof(struct br_ip),
4319e3a9c9SNikolay Aleksandrov 	.automatic_shrinking = true,
4419e3a9c9SNikolay Aleksandrov };
4519e3a9c9SNikolay Aleksandrov 
46085b53c8SNikolay Aleksandrov static const struct rhashtable_params br_sg_port_rht_params = {
47085b53c8SNikolay Aleksandrov 	.head_offset = offsetof(struct net_bridge_port_group, rhnode),
48085b53c8SNikolay Aleksandrov 	.key_offset = offsetof(struct net_bridge_port_group, key),
49085b53c8SNikolay Aleksandrov 	.key_len = sizeof(struct net_bridge_port_group_sg_key),
50085b53c8SNikolay Aleksandrov 	.automatic_shrinking = true,
51085b53c8SNikolay Aleksandrov };
52085b53c8SNikolay Aleksandrov 
53adc47037SNikolay Aleksandrov static void br_multicast_start_querier(struct net_bridge_mcast *brmctx,
5490010b36SLinus Lüssing 				       struct bridge_mcast_own_query *query);
55adc47037SNikolay Aleksandrov static void br_ip4_multicast_add_router(struct net_bridge_mcast *brmctx,
56adc47037SNikolay Aleksandrov 					struct net_bridge_mcast_port *pmctx);
57adc47037SNikolay Aleksandrov static void br_ip4_multicast_leave_group(struct net_bridge_mcast *brmctx,
58adc47037SNikolay Aleksandrov 					 struct net_bridge_mcast_port *pmctx,
59bc8c20acSSatish Ashok 					 __be32 group,
606db6f0eaSFelix Fietkau 					 __u16 vid,
616db6f0eaSFelix Fietkau 					 const unsigned char *src);
6242c11ccfSNikolay Aleksandrov static void br_multicast_port_group_rexmit(struct timer_list *t);
636db6f0eaSFelix Fietkau 
64ed2d3597SLinus Lüssing static void
65adc47037SNikolay Aleksandrov br_multicast_rport_del_notify(struct net_bridge_mcast_port *pmctx, bool deleted);
66adc47037SNikolay Aleksandrov static void br_ip6_multicast_add_router(struct net_bridge_mcast *brmctx,
67adc47037SNikolay Aleksandrov 					struct net_bridge_mcast_port *pmctx);
6830515832SMatteo Croce #if IS_ENABLED(CONFIG_IPV6)
69adc47037SNikolay Aleksandrov static void br_ip6_multicast_leave_group(struct net_bridge_mcast *brmctx,
70adc47037SNikolay Aleksandrov 					 struct net_bridge_mcast_port *pmctx,
71bc8c20acSSatish Ashok 					 const struct in6_addr *group,
726db6f0eaSFelix Fietkau 					 __u16 vid, const unsigned char *src);
73bc8c20acSSatish Ashok #endif
74b0812368SNikolay Aleksandrov static struct net_bridge_port_group *
75adc47037SNikolay Aleksandrov __br_multicast_add_group(struct net_bridge_mcast *brmctx,
76adc47037SNikolay Aleksandrov 			 struct net_bridge_mcast_port *pmctx,
77b0812368SNikolay Aleksandrov 			 struct br_ip *group,
78b0812368SNikolay Aleksandrov 			 const unsigned char *src,
79b0812368SNikolay Aleksandrov 			 u8 filter_mode,
809116ffbfSNikolay Aleksandrov 			 bool igmpv2_mldv1,
819116ffbfSNikolay Aleksandrov 			 bool blocked);
828266a049SNikolay Aleksandrov static void br_multicast_find_del_pg(struct net_bridge *br,
838266a049SNikolay Aleksandrov 				     struct net_bridge_port_group *pg);
84613d61dbSNikolay Aleksandrov static void __br_multicast_stop(struct net_bridge_mcast *brmctx);
85c83b8fabSHerbert Xu 
86c832962aSOleksandr Mazur static int br_mc_disabled_update(struct net_device *dev, bool value,
87c832962aSOleksandr Mazur 				 struct netlink_ext_ack *extack);
88c832962aSOleksandr Mazur 
89085b53c8SNikolay Aleksandrov static struct net_bridge_port_group *
br_sg_port_find(struct net_bridge * br,struct net_bridge_port_group_sg_key * sg_p)90085b53c8SNikolay Aleksandrov br_sg_port_find(struct net_bridge *br,
91085b53c8SNikolay Aleksandrov 		struct net_bridge_port_group_sg_key *sg_p)
92085b53c8SNikolay Aleksandrov {
93085b53c8SNikolay Aleksandrov 	lockdep_assert_held_once(&br->multicast_lock);
94085b53c8SNikolay Aleksandrov 
95085b53c8SNikolay Aleksandrov 	return rhashtable_lookup_fast(&br->sg_port_tbl, sg_p,
96085b53c8SNikolay Aleksandrov 				      br_sg_port_rht_params);
97085b53c8SNikolay Aleksandrov }
98085b53c8SNikolay Aleksandrov 
br_mdb_ip_get_rcu(struct net_bridge * br,struct br_ip * dst)9919e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip_get_rcu(struct net_bridge *br,
100cfd56754SCong Wang 						      struct br_ip *dst)
1017f285fa7SHerbert Xu {
10219e3a9c9SNikolay Aleksandrov 	return rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params);
1037f285fa7SHerbert Xu }
1047f285fa7SHerbert Xu 
br_mdb_ip_get(struct net_bridge * br,struct br_ip * dst)10519e3a9c9SNikolay Aleksandrov struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge *br,
10619e3a9c9SNikolay Aleksandrov 					   struct br_ip *dst)
10719e3a9c9SNikolay Aleksandrov {
10819e3a9c9SNikolay Aleksandrov 	struct net_bridge_mdb_entry *ent;
10919e3a9c9SNikolay Aleksandrov 
11019e3a9c9SNikolay Aleksandrov 	lockdep_assert_held_once(&br->multicast_lock);
11119e3a9c9SNikolay Aleksandrov 
11219e3a9c9SNikolay Aleksandrov 	rcu_read_lock();
11319e3a9c9SNikolay Aleksandrov 	ent = rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params);
11419e3a9c9SNikolay Aleksandrov 	rcu_read_unlock();
11519e3a9c9SNikolay Aleksandrov 
11619e3a9c9SNikolay Aleksandrov 	return ent;
11719e3a9c9SNikolay Aleksandrov }
11819e3a9c9SNikolay Aleksandrov 
br_mdb_ip4_get(struct net_bridge * br,__be32 dst,__u16 vid)11919e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip4_get(struct net_bridge *br,
12019e3a9c9SNikolay Aleksandrov 						   __be32 dst, __u16 vid)
121eb1d1641SHerbert Xu {
1228ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip br_dst;
1230821ec55SHerbert Xu 
12419e3a9c9SNikolay Aleksandrov 	memset(&br_dst, 0, sizeof(br_dst));
125eab3227bSNikolay Aleksandrov 	br_dst.dst.ip4 = dst;
1268ef2a9a5SYOSHIFUJI Hideaki 	br_dst.proto = htons(ETH_P_IP);
127b0e9a30dSVlad Yasevich 	br_dst.vid = vid;
1288ef2a9a5SYOSHIFUJI Hideaki 
12919e3a9c9SNikolay Aleksandrov 	return br_mdb_ip_get(br, &br_dst);
1308ef2a9a5SYOSHIFUJI Hideaki }
1318ef2a9a5SYOSHIFUJI Hideaki 
132dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
br_mdb_ip6_get(struct net_bridge * br,const struct in6_addr * dst,__u16 vid)13319e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip6_get(struct net_bridge *br,
13419e3a9c9SNikolay Aleksandrov 						   const struct in6_addr *dst,
135b0e9a30dSVlad Yasevich 						   __u16 vid)
13608b202b6SYOSHIFUJI Hideaki {
13708b202b6SYOSHIFUJI Hideaki 	struct br_ip br_dst;
13808b202b6SYOSHIFUJI Hideaki 
13919e3a9c9SNikolay Aleksandrov 	memset(&br_dst, 0, sizeof(br_dst));
140eab3227bSNikolay Aleksandrov 	br_dst.dst.ip6 = *dst;
14108b202b6SYOSHIFUJI Hideaki 	br_dst.proto = htons(ETH_P_IPV6);
142b0e9a30dSVlad Yasevich 	br_dst.vid = vid;
14308b202b6SYOSHIFUJI Hideaki 
14419e3a9c9SNikolay Aleksandrov 	return br_mdb_ip_get(br, &br_dst);
14508b202b6SYOSHIFUJI Hideaki }
14608b202b6SYOSHIFUJI Hideaki #endif
14708b202b6SYOSHIFUJI Hideaki 
br_mdb_get(struct net_bridge_mcast * brmctx,struct sk_buff * skb,u16 vid)148adc47037SNikolay Aleksandrov struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge_mcast *brmctx,
149fbca58a2SCong Wang 					struct sk_buff *skb, u16 vid)
150eb1d1641SHerbert Xu {
151adc47037SNikolay Aleksandrov 	struct net_bridge *br = brmctx->br;
1528ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip ip;
1538ef2a9a5SYOSHIFUJI Hideaki 
1544cdd0d10SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED) ||
1554cdd0d10SNikolay Aleksandrov 	    br_multicast_ctx_vlan_global_disabled(brmctx))
156eb1d1641SHerbert Xu 		return NULL;
157eb1d1641SHerbert Xu 
1588ef2a9a5SYOSHIFUJI Hideaki 	if (BR_INPUT_SKB_CB(skb)->igmp)
1598ef2a9a5SYOSHIFUJI Hideaki 		return NULL;
1608ef2a9a5SYOSHIFUJI Hideaki 
16119e3a9c9SNikolay Aleksandrov 	memset(&ip, 0, sizeof(ip));
1628ef2a9a5SYOSHIFUJI Hideaki 	ip.proto = skb->protocol;
163fbca58a2SCong Wang 	ip.vid = vid;
1648ef2a9a5SYOSHIFUJI Hideaki 
165eb1d1641SHerbert Xu 	switch (skb->protocol) {
166eb1d1641SHerbert Xu 	case htons(ETH_P_IP):
167eab3227bSNikolay Aleksandrov 		ip.dst.ip4 = ip_hdr(skb)->daddr;
168adc47037SNikolay Aleksandrov 		if (brmctx->multicast_igmp_version == 3) {
1697d07a68cSNikolay Aleksandrov 			struct net_bridge_mdb_entry *mdb;
1707d07a68cSNikolay Aleksandrov 
1717d07a68cSNikolay Aleksandrov 			ip.src.ip4 = ip_hdr(skb)->saddr;
1727d07a68cSNikolay Aleksandrov 			mdb = br_mdb_ip_get_rcu(br, &ip);
1737d07a68cSNikolay Aleksandrov 			if (mdb)
1747d07a68cSNikolay Aleksandrov 				return mdb;
1757d07a68cSNikolay Aleksandrov 			ip.src.ip4 = 0;
1767d07a68cSNikolay Aleksandrov 		}
177eb1d1641SHerbert Xu 		break;
178dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
17908b202b6SYOSHIFUJI Hideaki 	case htons(ETH_P_IPV6):
180eab3227bSNikolay Aleksandrov 		ip.dst.ip6 = ipv6_hdr(skb)->daddr;
181adc47037SNikolay Aleksandrov 		if (brmctx->multicast_mld_version == 2) {
1827d07a68cSNikolay Aleksandrov 			struct net_bridge_mdb_entry *mdb;
1837d07a68cSNikolay Aleksandrov 
1847d07a68cSNikolay Aleksandrov 			ip.src.ip6 = ipv6_hdr(skb)->saddr;
1857d07a68cSNikolay Aleksandrov 			mdb = br_mdb_ip_get_rcu(br, &ip);
1867d07a68cSNikolay Aleksandrov 			if (mdb)
1877d07a68cSNikolay Aleksandrov 				return mdb;
1887d07a68cSNikolay Aleksandrov 			memset(&ip.src.ip6, 0, sizeof(ip.src.ip6));
1897d07a68cSNikolay Aleksandrov 		}
19008b202b6SYOSHIFUJI Hideaki 		break;
19108b202b6SYOSHIFUJI Hideaki #endif
1928ef2a9a5SYOSHIFUJI Hideaki 	default:
193955062b0SNikolay Aleksandrov 		ip.proto = 0;
194955062b0SNikolay Aleksandrov 		ether_addr_copy(ip.dst.mac_addr, eth_hdr(skb)->h_dest);
195eb1d1641SHerbert Xu 	}
196eb1d1641SHerbert Xu 
19719e3a9c9SNikolay Aleksandrov 	return br_mdb_ip_get_rcu(br, &ip);
198eb1d1641SHerbert Xu }
199eb1d1641SHerbert Xu 
20074edfd48SNikolay Aleksandrov /* IMPORTANT: this function must be used only when the contexts cannot be
20174edfd48SNikolay Aleksandrov  * passed down (e.g. timer) and must be used for read-only purposes because
20274edfd48SNikolay Aleksandrov  * the vlan snooping option can change, so it can return any context
20374edfd48SNikolay Aleksandrov  * (non-vlan or vlan). Its initial intended purpose is to read timer values
20474edfd48SNikolay Aleksandrov  * from the *current* context based on the option. At worst that could lead
20574edfd48SNikolay Aleksandrov  * to inconsistent timers when the contexts are changed, i.e. src timer
20674edfd48SNikolay Aleksandrov  * which needs to re-arm with a specific delay taken from the old context
20774edfd48SNikolay Aleksandrov  */
20874edfd48SNikolay Aleksandrov static struct net_bridge_mcast_port *
br_multicast_pg_to_port_ctx(const struct net_bridge_port_group * pg)20974edfd48SNikolay Aleksandrov br_multicast_pg_to_port_ctx(const struct net_bridge_port_group *pg)
21074edfd48SNikolay Aleksandrov {
21174edfd48SNikolay Aleksandrov 	struct net_bridge_mcast_port *pmctx = &pg->key.port->multicast_ctx;
21274edfd48SNikolay Aleksandrov 	struct net_bridge_vlan *vlan;
21374edfd48SNikolay Aleksandrov 
21474edfd48SNikolay Aleksandrov 	lockdep_assert_held_once(&pg->key.port->br->multicast_lock);
21574edfd48SNikolay Aleksandrov 
21674edfd48SNikolay Aleksandrov 	/* if vlan snooping is disabled use the port's multicast context */
21774edfd48SNikolay Aleksandrov 	if (!pg->key.addr.vid ||
21874edfd48SNikolay Aleksandrov 	    !br_opt_get(pg->key.port->br, BROPT_MCAST_VLAN_SNOOPING_ENABLED))
21974edfd48SNikolay Aleksandrov 		goto out;
22074edfd48SNikolay Aleksandrov 
22174edfd48SNikolay Aleksandrov 	/* locking is tricky here, due to different rules for multicast and
22274edfd48SNikolay Aleksandrov 	 * vlans we need to take rcu to find the vlan and make sure it has
22374edfd48SNikolay Aleksandrov 	 * the BR_VLFLAG_MCAST_ENABLED flag set, it can only change under
22474edfd48SNikolay Aleksandrov 	 * multicast_lock which must be already held here, so the vlan's pmctx
22574edfd48SNikolay Aleksandrov 	 * can safely be used on return
22674edfd48SNikolay Aleksandrov 	 */
22774edfd48SNikolay Aleksandrov 	rcu_read_lock();
2283f0d14efSNikolay Aleksandrov 	vlan = br_vlan_find(nbp_vlan_group_rcu(pg->key.port), pg->key.addr.vid);
22974edfd48SNikolay Aleksandrov 	if (vlan && !br_multicast_port_ctx_vlan_disabled(&vlan->port_mcast_ctx))
23074edfd48SNikolay Aleksandrov 		pmctx = &vlan->port_mcast_ctx;
23174edfd48SNikolay Aleksandrov 	else
23274edfd48SNikolay Aleksandrov 		pmctx = NULL;
23374edfd48SNikolay Aleksandrov 	rcu_read_unlock();
23474edfd48SNikolay Aleksandrov out:
23574edfd48SNikolay Aleksandrov 	return pmctx;
23674edfd48SNikolay Aleksandrov }
23774edfd48SNikolay Aleksandrov 
238b57e8d87SPetr Machata static struct net_bridge_mcast_port *
br_multicast_port_vid_to_port_ctx(struct net_bridge_port * port,u16 vid)239b57e8d87SPetr Machata br_multicast_port_vid_to_port_ctx(struct net_bridge_port *port, u16 vid)
240b57e8d87SPetr Machata {
241b57e8d87SPetr Machata 	struct net_bridge_mcast_port *pmctx = NULL;
242b57e8d87SPetr Machata 	struct net_bridge_vlan *vlan;
243b57e8d87SPetr Machata 
244b57e8d87SPetr Machata 	lockdep_assert_held_once(&port->br->multicast_lock);
245b57e8d87SPetr Machata 
246b57e8d87SPetr Machata 	if (!br_opt_get(port->br, BROPT_MCAST_VLAN_SNOOPING_ENABLED))
247b57e8d87SPetr Machata 		return NULL;
248b57e8d87SPetr Machata 
249b57e8d87SPetr Machata 	/* Take RCU to access the vlan. */
250b57e8d87SPetr Machata 	rcu_read_lock();
251b57e8d87SPetr Machata 
252b57e8d87SPetr Machata 	vlan = br_vlan_find(nbp_vlan_group_rcu(port), vid);
253b57e8d87SPetr Machata 	if (vlan && !br_multicast_port_ctx_vlan_disabled(&vlan->port_mcast_ctx))
254b57e8d87SPetr Machata 		pmctx = &vlan->port_mcast_ctx;
255b57e8d87SPetr Machata 
256b57e8d87SPetr Machata 	rcu_read_unlock();
257b57e8d87SPetr Machata 
258b57e8d87SPetr Machata 	return pmctx;
259b57e8d87SPetr Machata }
260b57e8d87SPetr Machata 
2614cdd0d10SNikolay Aleksandrov /* when snooping we need to check if the contexts should be used
2624cdd0d10SNikolay Aleksandrov  * in the following order:
2634cdd0d10SNikolay Aleksandrov  * - if pmctx is non-NULL (port), check if it should be used
2644cdd0d10SNikolay Aleksandrov  * - if pmctx is NULL (bridge), check if brmctx should be used
2654cdd0d10SNikolay Aleksandrov  */
2664cdd0d10SNikolay Aleksandrov static bool
br_multicast_ctx_should_use(const struct net_bridge_mcast * brmctx,const struct net_bridge_mcast_port * pmctx)2674cdd0d10SNikolay Aleksandrov br_multicast_ctx_should_use(const struct net_bridge_mcast *brmctx,
2684cdd0d10SNikolay Aleksandrov 			    const struct net_bridge_mcast_port *pmctx)
2694cdd0d10SNikolay Aleksandrov {
2704cdd0d10SNikolay Aleksandrov 	if (!netif_running(brmctx->br->dev))
2714cdd0d10SNikolay Aleksandrov 		return false;
2724cdd0d10SNikolay Aleksandrov 
2734cdd0d10SNikolay Aleksandrov 	if (pmctx)
2744cdd0d10SNikolay Aleksandrov 		return !br_multicast_port_ctx_state_disabled(pmctx);
2754cdd0d10SNikolay Aleksandrov 	else
2764cdd0d10SNikolay Aleksandrov 		return !br_multicast_ctx_vlan_disabled(brmctx);
2774cdd0d10SNikolay Aleksandrov }
2784cdd0d10SNikolay Aleksandrov 
br_port_group_equal(struct net_bridge_port_group * p,struct net_bridge_port * port,const unsigned char * src)279b0812368SNikolay Aleksandrov static bool br_port_group_equal(struct net_bridge_port_group *p,
280b0812368SNikolay Aleksandrov 				struct net_bridge_port *port,
281b0812368SNikolay Aleksandrov 				const unsigned char *src)
282b0812368SNikolay Aleksandrov {
283b0812368SNikolay Aleksandrov 	if (p->key.port != port)
284b0812368SNikolay Aleksandrov 		return false;
285b0812368SNikolay Aleksandrov 
286b0812368SNikolay Aleksandrov 	if (!(port->flags & BR_MULTICAST_TO_UNICAST))
287b0812368SNikolay Aleksandrov 		return true;
288b0812368SNikolay Aleksandrov 
289b0812368SNikolay Aleksandrov 	return ether_addr_equal(src, p->eth_addr);
290b0812368SNikolay Aleksandrov }
291b0812368SNikolay Aleksandrov 
__fwd_add_star_excl(struct net_bridge_mcast_port * pmctx,struct net_bridge_port_group * pg,struct br_ip * sg_ip)292adc47037SNikolay Aleksandrov static void __fwd_add_star_excl(struct net_bridge_mcast_port *pmctx,
293adc47037SNikolay Aleksandrov 				struct net_bridge_port_group *pg,
2948266a049SNikolay Aleksandrov 				struct br_ip *sg_ip)
2958266a049SNikolay Aleksandrov {
2968266a049SNikolay Aleksandrov 	struct net_bridge_port_group_sg_key sg_key;
2978266a049SNikolay Aleksandrov 	struct net_bridge_port_group *src_pg;
298adc47037SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx;
2998266a049SNikolay Aleksandrov 
3008266a049SNikolay Aleksandrov 	memset(&sg_key, 0, sizeof(sg_key));
3017b54aaafSNikolay Aleksandrov 	brmctx = br_multicast_port_ctx_get_global(pmctx);
3028266a049SNikolay Aleksandrov 	sg_key.port = pg->key.port;
3038266a049SNikolay Aleksandrov 	sg_key.addr = *sg_ip;
304adc47037SNikolay Aleksandrov 	if (br_sg_port_find(brmctx->br, &sg_key))
3058266a049SNikolay Aleksandrov 		return;
3068266a049SNikolay Aleksandrov 
307adc47037SNikolay Aleksandrov 	src_pg = __br_multicast_add_group(brmctx, pmctx,
308adc47037SNikolay Aleksandrov 					  sg_ip, pg->eth_addr,
3099116ffbfSNikolay Aleksandrov 					  MCAST_INCLUDE, false, false);
3108266a049SNikolay Aleksandrov 	if (IS_ERR_OR_NULL(src_pg) ||
3118266a049SNikolay Aleksandrov 	    src_pg->rt_protocol != RTPROT_KERNEL)
3128266a049SNikolay Aleksandrov 		return;
3138266a049SNikolay Aleksandrov 
3148266a049SNikolay Aleksandrov 	src_pg->flags |= MDB_PG_FLAGS_STAR_EXCL;
3158266a049SNikolay Aleksandrov }
3168266a049SNikolay Aleksandrov 
__fwd_del_star_excl(struct net_bridge_port_group * pg,struct br_ip * sg_ip)3178266a049SNikolay Aleksandrov static void __fwd_del_star_excl(struct net_bridge_port_group *pg,
3188266a049SNikolay Aleksandrov 				struct br_ip *sg_ip)
3198266a049SNikolay Aleksandrov {
3208266a049SNikolay Aleksandrov 	struct net_bridge_port_group_sg_key sg_key;
3218266a049SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
3228266a049SNikolay Aleksandrov 	struct net_bridge_port_group *src_pg;
3238266a049SNikolay Aleksandrov 
3248266a049SNikolay Aleksandrov 	memset(&sg_key, 0, sizeof(sg_key));
3258266a049SNikolay Aleksandrov 	sg_key.port = pg->key.port;
3268266a049SNikolay Aleksandrov 	sg_key.addr = *sg_ip;
3278266a049SNikolay Aleksandrov 	src_pg = br_sg_port_find(br, &sg_key);
3288266a049SNikolay Aleksandrov 	if (!src_pg || !(src_pg->flags & MDB_PG_FLAGS_STAR_EXCL) ||
3298266a049SNikolay Aleksandrov 	    src_pg->rt_protocol != RTPROT_KERNEL)
3308266a049SNikolay Aleksandrov 		return;
3318266a049SNikolay Aleksandrov 
3328266a049SNikolay Aleksandrov 	br_multicast_find_del_pg(br, src_pg);
3338266a049SNikolay Aleksandrov }
3348266a049SNikolay Aleksandrov 
3358266a049SNikolay Aleksandrov /* When a port group transitions to (or is added as) EXCLUDE we need to add it
3368266a049SNikolay Aleksandrov  * to all other ports' S,G entries which are not blocked by the current group
3378266a049SNikolay Aleksandrov  * for proper replication, the assumption is that any S,G blocked entries
3388266a049SNikolay Aleksandrov  * are already added so the S,G,port lookup should skip them.
3398266a049SNikolay Aleksandrov  * When a port group transitions from EXCLUDE -> INCLUDE mode or is being
3408266a049SNikolay Aleksandrov  * deleted we need to remove it from all ports' S,G entries where it was
3418266a049SNikolay Aleksandrov  * automatically installed before (i.e. where it's MDB_PG_FLAGS_STAR_EXCL).
3428266a049SNikolay Aleksandrov  */
br_multicast_star_g_handle_mode(struct net_bridge_port_group * pg,u8 filter_mode)3438266a049SNikolay Aleksandrov void br_multicast_star_g_handle_mode(struct net_bridge_port_group *pg,
3448266a049SNikolay Aleksandrov 				     u8 filter_mode)
3458266a049SNikolay Aleksandrov {
3468266a049SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
3478266a049SNikolay Aleksandrov 	struct net_bridge_port_group *pg_lst;
348adc47037SNikolay Aleksandrov 	struct net_bridge_mcast_port *pmctx;
3498266a049SNikolay Aleksandrov 	struct net_bridge_mdb_entry *mp;
3508266a049SNikolay Aleksandrov 	struct br_ip sg_ip;
3518266a049SNikolay Aleksandrov 
3528266a049SNikolay Aleksandrov 	if (WARN_ON(!br_multicast_is_star_g(&pg->key.addr)))
3538266a049SNikolay Aleksandrov 		return;
3548266a049SNikolay Aleksandrov 
3558266a049SNikolay Aleksandrov 	mp = br_mdb_ip_get(br, &pg->key.addr);
3568266a049SNikolay Aleksandrov 	if (!mp)
3578266a049SNikolay Aleksandrov 		return;
358eb1593a0SNikolay Aleksandrov 	pmctx = br_multicast_pg_to_port_ctx(pg);
359eb1593a0SNikolay Aleksandrov 	if (!pmctx)
360eb1593a0SNikolay Aleksandrov 		return;
3618266a049SNikolay Aleksandrov 
3628266a049SNikolay Aleksandrov 	memset(&sg_ip, 0, sizeof(sg_ip));
3638266a049SNikolay Aleksandrov 	sg_ip = pg->key.addr;
3647b54aaafSNikolay Aleksandrov 
3658266a049SNikolay Aleksandrov 	for (pg_lst = mlock_dereference(mp->ports, br);
3668266a049SNikolay Aleksandrov 	     pg_lst;
3678266a049SNikolay Aleksandrov 	     pg_lst = mlock_dereference(pg_lst->next, br)) {
3688266a049SNikolay Aleksandrov 		struct net_bridge_group_src *src_ent;
3698266a049SNikolay Aleksandrov 
3708266a049SNikolay Aleksandrov 		if (pg_lst == pg)
3718266a049SNikolay Aleksandrov 			continue;
3728266a049SNikolay Aleksandrov 		hlist_for_each_entry(src_ent, &pg_lst->src_list, node) {
3738266a049SNikolay Aleksandrov 			if (!(src_ent->flags & BR_SGRP_F_INSTALLED))
3748266a049SNikolay Aleksandrov 				continue;
3758266a049SNikolay Aleksandrov 			sg_ip.src = src_ent->addr.src;
3768266a049SNikolay Aleksandrov 			switch (filter_mode) {
3778266a049SNikolay Aleksandrov 			case MCAST_INCLUDE:
3788266a049SNikolay Aleksandrov 				__fwd_del_star_excl(pg, &sg_ip);
3798266a049SNikolay Aleksandrov 				break;
3808266a049SNikolay Aleksandrov 			case MCAST_EXCLUDE:
381adc47037SNikolay Aleksandrov 				__fwd_add_star_excl(pmctx, pg, &sg_ip);
3828266a049SNikolay Aleksandrov 				break;
3838266a049SNikolay Aleksandrov 			}
3848266a049SNikolay Aleksandrov 		}
3858266a049SNikolay Aleksandrov 	}
3868266a049SNikolay Aleksandrov }
3878266a049SNikolay Aleksandrov 
388094b82fdSNikolay Aleksandrov /* called when adding a new S,G with host_joined == false by default */
br_multicast_sg_host_state(struct net_bridge_mdb_entry * star_mp,struct net_bridge_port_group * sg)389094b82fdSNikolay Aleksandrov static void br_multicast_sg_host_state(struct net_bridge_mdb_entry *star_mp,
390094b82fdSNikolay Aleksandrov 				       struct net_bridge_port_group *sg)
391094b82fdSNikolay Aleksandrov {
392094b82fdSNikolay Aleksandrov 	struct net_bridge_mdb_entry *sg_mp;
393094b82fdSNikolay Aleksandrov 
394094b82fdSNikolay Aleksandrov 	if (WARN_ON(!br_multicast_is_star_g(&star_mp->addr)))
395094b82fdSNikolay Aleksandrov 		return;
396094b82fdSNikolay Aleksandrov 	if (!star_mp->host_joined)
397094b82fdSNikolay Aleksandrov 		return;
398094b82fdSNikolay Aleksandrov 
399094b82fdSNikolay Aleksandrov 	sg_mp = br_mdb_ip_get(star_mp->br, &sg->key.addr);
400094b82fdSNikolay Aleksandrov 	if (!sg_mp)
401094b82fdSNikolay Aleksandrov 		return;
402094b82fdSNikolay Aleksandrov 	sg_mp->host_joined = true;
403094b82fdSNikolay Aleksandrov }
404094b82fdSNikolay Aleksandrov 
405094b82fdSNikolay Aleksandrov /* set the host_joined state of all of *,G's S,G entries */
br_multicast_star_g_host_state(struct net_bridge_mdb_entry * star_mp)406094b82fdSNikolay Aleksandrov static void br_multicast_star_g_host_state(struct net_bridge_mdb_entry *star_mp)
407094b82fdSNikolay Aleksandrov {
408094b82fdSNikolay Aleksandrov 	struct net_bridge *br = star_mp->br;
409094b82fdSNikolay Aleksandrov 	struct net_bridge_mdb_entry *sg_mp;
410094b82fdSNikolay Aleksandrov 	struct net_bridge_port_group *pg;
411094b82fdSNikolay Aleksandrov 	struct br_ip sg_ip;
412094b82fdSNikolay Aleksandrov 
413094b82fdSNikolay Aleksandrov 	if (WARN_ON(!br_multicast_is_star_g(&star_mp->addr)))
414094b82fdSNikolay Aleksandrov 		return;
415094b82fdSNikolay Aleksandrov 
416094b82fdSNikolay Aleksandrov 	memset(&sg_ip, 0, sizeof(sg_ip));
417094b82fdSNikolay Aleksandrov 	sg_ip = star_mp->addr;
418094b82fdSNikolay Aleksandrov 	for (pg = mlock_dereference(star_mp->ports, br);
419094b82fdSNikolay Aleksandrov 	     pg;
420094b82fdSNikolay Aleksandrov 	     pg = mlock_dereference(pg->next, br)) {
421094b82fdSNikolay Aleksandrov 		struct net_bridge_group_src *src_ent;
422094b82fdSNikolay Aleksandrov 
423094b82fdSNikolay Aleksandrov 		hlist_for_each_entry(src_ent, &pg->src_list, node) {
424094b82fdSNikolay Aleksandrov 			if (!(src_ent->flags & BR_SGRP_F_INSTALLED))
425094b82fdSNikolay Aleksandrov 				continue;
426094b82fdSNikolay Aleksandrov 			sg_ip.src = src_ent->addr.src;
427094b82fdSNikolay Aleksandrov 			sg_mp = br_mdb_ip_get(br, &sg_ip);
428094b82fdSNikolay Aleksandrov 			if (!sg_mp)
429094b82fdSNikolay Aleksandrov 				continue;
430094b82fdSNikolay Aleksandrov 			sg_mp->host_joined = star_mp->host_joined;
431094b82fdSNikolay Aleksandrov 		}
432094b82fdSNikolay Aleksandrov 	}
433094b82fdSNikolay Aleksandrov }
434094b82fdSNikolay Aleksandrov 
br_multicast_sg_del_exclude_ports(struct net_bridge_mdb_entry * sgmp)4358266a049SNikolay Aleksandrov static void br_multicast_sg_del_exclude_ports(struct net_bridge_mdb_entry *sgmp)
4368266a049SNikolay Aleksandrov {
4378266a049SNikolay Aleksandrov 	struct net_bridge_port_group __rcu **pp;
4388266a049SNikolay Aleksandrov 	struct net_bridge_port_group *p;
4398266a049SNikolay Aleksandrov 
4408266a049SNikolay Aleksandrov 	/* *,G exclude ports are only added to S,G entries */
4418266a049SNikolay Aleksandrov 	if (WARN_ON(br_multicast_is_star_g(&sgmp->addr)))
4428266a049SNikolay Aleksandrov 		return;
4438266a049SNikolay Aleksandrov 
4448266a049SNikolay Aleksandrov 	/* we need the STAR_EXCLUDE ports if there are non-STAR_EXCLUDE ports
4458266a049SNikolay Aleksandrov 	 * we should ignore perm entries since they're managed by user-space
4468266a049SNikolay Aleksandrov 	 */
4478266a049SNikolay Aleksandrov 	for (pp = &sgmp->ports;
4488266a049SNikolay Aleksandrov 	     (p = mlock_dereference(*pp, sgmp->br)) != NULL;
4498266a049SNikolay Aleksandrov 	     pp = &p->next)
4508266a049SNikolay Aleksandrov 		if (!(p->flags & (MDB_PG_FLAGS_STAR_EXCL |
4518266a049SNikolay Aleksandrov 				  MDB_PG_FLAGS_PERMANENT)))
4528266a049SNikolay Aleksandrov 			return;
4538266a049SNikolay Aleksandrov 
454094b82fdSNikolay Aleksandrov 	/* currently the host can only have joined the *,G which means
455094b82fdSNikolay Aleksandrov 	 * we treat it as EXCLUDE {}, so for an S,G it's considered a
456094b82fdSNikolay Aleksandrov 	 * STAR_EXCLUDE entry and we can safely leave it
457094b82fdSNikolay Aleksandrov 	 */
458094b82fdSNikolay Aleksandrov 	sgmp->host_joined = false;
459094b82fdSNikolay Aleksandrov 
4608266a049SNikolay Aleksandrov 	for (pp = &sgmp->ports;
4618266a049SNikolay Aleksandrov 	     (p = mlock_dereference(*pp, sgmp->br)) != NULL;) {
4628266a049SNikolay Aleksandrov 		if (!(p->flags & MDB_PG_FLAGS_PERMANENT))
4638266a049SNikolay Aleksandrov 			br_multicast_del_pg(sgmp, p, pp);
4648266a049SNikolay Aleksandrov 		else
4658266a049SNikolay Aleksandrov 			pp = &p->next;
4668266a049SNikolay Aleksandrov 	}
4678266a049SNikolay Aleksandrov }
4688266a049SNikolay Aleksandrov 
br_multicast_sg_add_exclude_ports(struct net_bridge_mdb_entry * star_mp,struct net_bridge_port_group * sg)4698266a049SNikolay Aleksandrov void br_multicast_sg_add_exclude_ports(struct net_bridge_mdb_entry *star_mp,
4708266a049SNikolay Aleksandrov 				       struct net_bridge_port_group *sg)
4718266a049SNikolay Aleksandrov {
4728266a049SNikolay Aleksandrov 	struct net_bridge_port_group_sg_key sg_key;
4738266a049SNikolay Aleksandrov 	struct net_bridge *br = star_mp->br;
474adc47037SNikolay Aleksandrov 	struct net_bridge_mcast_port *pmctx;
4758266a049SNikolay Aleksandrov 	struct net_bridge_port_group *pg;
476adc47037SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx;
4778266a049SNikolay Aleksandrov 
4788266a049SNikolay Aleksandrov 	if (WARN_ON(br_multicast_is_star_g(&sg->key.addr)))
4798266a049SNikolay Aleksandrov 		return;
4808266a049SNikolay Aleksandrov 	if (WARN_ON(!br_multicast_is_star_g(&star_mp->addr)))
4818266a049SNikolay Aleksandrov 		return;
4828266a049SNikolay Aleksandrov 
483094b82fdSNikolay Aleksandrov 	br_multicast_sg_host_state(star_mp, sg);
4848266a049SNikolay Aleksandrov 	memset(&sg_key, 0, sizeof(sg_key));
4858266a049SNikolay Aleksandrov 	sg_key.addr = sg->key.addr;
4868266a049SNikolay Aleksandrov 	/* we need to add all exclude ports to the S,G */
4878266a049SNikolay Aleksandrov 	for (pg = mlock_dereference(star_mp->ports, br);
4888266a049SNikolay Aleksandrov 	     pg;
4898266a049SNikolay Aleksandrov 	     pg = mlock_dereference(pg->next, br)) {
4908266a049SNikolay Aleksandrov 		struct net_bridge_port_group *src_pg;
4918266a049SNikolay Aleksandrov 
4928266a049SNikolay Aleksandrov 		if (pg == sg || pg->filter_mode == MCAST_INCLUDE)
4938266a049SNikolay Aleksandrov 			continue;
4948266a049SNikolay Aleksandrov 
4958266a049SNikolay Aleksandrov 		sg_key.port = pg->key.port;
4968266a049SNikolay Aleksandrov 		if (br_sg_port_find(br, &sg_key))
4978266a049SNikolay Aleksandrov 			continue;
4988266a049SNikolay Aleksandrov 
499eb1593a0SNikolay Aleksandrov 		pmctx = br_multicast_pg_to_port_ctx(pg);
500eb1593a0SNikolay Aleksandrov 		if (!pmctx)
501eb1593a0SNikolay Aleksandrov 			continue;
502eb1593a0SNikolay Aleksandrov 		brmctx = br_multicast_port_ctx_get_global(pmctx);
503eb1593a0SNikolay Aleksandrov 
504adc47037SNikolay Aleksandrov 		src_pg = __br_multicast_add_group(brmctx, pmctx,
5058266a049SNikolay Aleksandrov 						  &sg->key.addr,
5068266a049SNikolay Aleksandrov 						  sg->eth_addr,
5079116ffbfSNikolay Aleksandrov 						  MCAST_INCLUDE, false, false);
5088266a049SNikolay Aleksandrov 		if (IS_ERR_OR_NULL(src_pg) ||
5098266a049SNikolay Aleksandrov 		    src_pg->rt_protocol != RTPROT_KERNEL)
5108266a049SNikolay Aleksandrov 			continue;
5118266a049SNikolay Aleksandrov 		src_pg->flags |= MDB_PG_FLAGS_STAR_EXCL;
5128266a049SNikolay Aleksandrov 	}
5138266a049SNikolay Aleksandrov }
5148266a049SNikolay Aleksandrov 
br_multicast_fwd_src_add(struct net_bridge_group_src * src)515b0812368SNikolay Aleksandrov static void br_multicast_fwd_src_add(struct net_bridge_group_src *src)
516b0812368SNikolay Aleksandrov {
5178266a049SNikolay Aleksandrov 	struct net_bridge_mdb_entry *star_mp;
518adc47037SNikolay Aleksandrov 	struct net_bridge_mcast_port *pmctx;
519b0812368SNikolay Aleksandrov 	struct net_bridge_port_group *sg;
520adc47037SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx;
521b0812368SNikolay Aleksandrov 	struct br_ip sg_ip;
522b0812368SNikolay Aleksandrov 
523b0812368SNikolay Aleksandrov 	if (src->flags & BR_SGRP_F_INSTALLED)
524b0812368SNikolay Aleksandrov 		return;
525b0812368SNikolay Aleksandrov 
526b0812368SNikolay Aleksandrov 	memset(&sg_ip, 0, sizeof(sg_ip));
527eb1593a0SNikolay Aleksandrov 	pmctx = br_multicast_pg_to_port_ctx(src->pg);
528eb1593a0SNikolay Aleksandrov 	if (!pmctx)
529eb1593a0SNikolay Aleksandrov 		return;
5307b54aaafSNikolay Aleksandrov 	brmctx = br_multicast_port_ctx_get_global(pmctx);
531b0812368SNikolay Aleksandrov 	sg_ip = src->pg->key.addr;
532b0812368SNikolay Aleksandrov 	sg_ip.src = src->addr.src;
533adc47037SNikolay Aleksandrov 
534adc47037SNikolay Aleksandrov 	sg = __br_multicast_add_group(brmctx, pmctx, &sg_ip,
5359116ffbfSNikolay Aleksandrov 				      src->pg->eth_addr, MCAST_INCLUDE, false,
5369116ffbfSNikolay Aleksandrov 				      !timer_pending(&src->timer));
537b0812368SNikolay Aleksandrov 	if (IS_ERR_OR_NULL(sg))
538b0812368SNikolay Aleksandrov 		return;
539b0812368SNikolay Aleksandrov 	src->flags |= BR_SGRP_F_INSTALLED;
5408266a049SNikolay Aleksandrov 	sg->flags &= ~MDB_PG_FLAGS_STAR_EXCL;
541b0812368SNikolay Aleksandrov 
542b0812368SNikolay Aleksandrov 	/* if it was added by user-space as perm we can skip next steps */
543b0812368SNikolay Aleksandrov 	if (sg->rt_protocol != RTPROT_KERNEL &&
544b0812368SNikolay Aleksandrov 	    (sg->flags & MDB_PG_FLAGS_PERMANENT))
545b0812368SNikolay Aleksandrov 		return;
546b0812368SNikolay Aleksandrov 
547b0812368SNikolay Aleksandrov 	/* the kernel is now responsible for removing this S,G */
548b0812368SNikolay Aleksandrov 	del_timer(&sg->timer);
5498266a049SNikolay Aleksandrov 	star_mp = br_mdb_ip_get(src->br, &src->pg->key.addr);
5508266a049SNikolay Aleksandrov 	if (!star_mp)
5518266a049SNikolay Aleksandrov 		return;
5528266a049SNikolay Aleksandrov 
5538266a049SNikolay Aleksandrov 	br_multicast_sg_add_exclude_ports(star_mp, sg);
554b0812368SNikolay Aleksandrov }
555b0812368SNikolay Aleksandrov 
br_multicast_fwd_src_remove(struct net_bridge_group_src * src,bool fastleave)556d5a10222SNikolay Aleksandrov static void br_multicast_fwd_src_remove(struct net_bridge_group_src *src,
557d5a10222SNikolay Aleksandrov 					bool fastleave)
558b0812368SNikolay Aleksandrov {
559b0812368SNikolay Aleksandrov 	struct net_bridge_port_group *p, *pg = src->pg;
560b0812368SNikolay Aleksandrov 	struct net_bridge_port_group __rcu **pp;
561b0812368SNikolay Aleksandrov 	struct net_bridge_mdb_entry *mp;
562b0812368SNikolay Aleksandrov 	struct br_ip sg_ip;
563b0812368SNikolay Aleksandrov 
564b0812368SNikolay Aleksandrov 	memset(&sg_ip, 0, sizeof(sg_ip));
565b0812368SNikolay Aleksandrov 	sg_ip = pg->key.addr;
566b0812368SNikolay Aleksandrov 	sg_ip.src = src->addr.src;
567b0812368SNikolay Aleksandrov 
568b0812368SNikolay Aleksandrov 	mp = br_mdb_ip_get(src->br, &sg_ip);
569b0812368SNikolay Aleksandrov 	if (!mp)
570b0812368SNikolay Aleksandrov 		return;
571b0812368SNikolay Aleksandrov 
572b0812368SNikolay Aleksandrov 	for (pp = &mp->ports;
573b0812368SNikolay Aleksandrov 	     (p = mlock_dereference(*pp, src->br)) != NULL;
574b0812368SNikolay Aleksandrov 	     pp = &p->next) {
575b0812368SNikolay Aleksandrov 		if (!br_port_group_equal(p, pg->key.port, pg->eth_addr))
576b0812368SNikolay Aleksandrov 			continue;
577b0812368SNikolay Aleksandrov 
578b0812368SNikolay Aleksandrov 		if (p->rt_protocol != RTPROT_KERNEL &&
579a01ecb17SIdo Schimmel 		    (p->flags & MDB_PG_FLAGS_PERMANENT) &&
580a01ecb17SIdo Schimmel 		    !(src->flags & BR_SGRP_F_USER_ADDED))
581b0812368SNikolay Aleksandrov 			break;
582b0812368SNikolay Aleksandrov 
583d5a10222SNikolay Aleksandrov 		if (fastleave)
584d5a10222SNikolay Aleksandrov 			p->flags |= MDB_PG_FLAGS_FAST_LEAVE;
585b0812368SNikolay Aleksandrov 		br_multicast_del_pg(mp, p, pp);
586b0812368SNikolay Aleksandrov 		break;
587b0812368SNikolay Aleksandrov 	}
588b0812368SNikolay Aleksandrov 	src->flags &= ~BR_SGRP_F_INSTALLED;
589b0812368SNikolay Aleksandrov }
590b0812368SNikolay Aleksandrov 
5919116ffbfSNikolay Aleksandrov /* install S,G and based on src's timer enable or disable forwarding */
br_multicast_fwd_src_handle(struct net_bridge_group_src * src)592b0812368SNikolay Aleksandrov static void br_multicast_fwd_src_handle(struct net_bridge_group_src *src)
593b0812368SNikolay Aleksandrov {
5949116ffbfSNikolay Aleksandrov 	struct net_bridge_port_group_sg_key sg_key;
5959116ffbfSNikolay Aleksandrov 	struct net_bridge_port_group *sg;
5969116ffbfSNikolay Aleksandrov 	u8 old_flags;
5979116ffbfSNikolay Aleksandrov 
598b0812368SNikolay Aleksandrov 	br_multicast_fwd_src_add(src);
5999116ffbfSNikolay Aleksandrov 
6009116ffbfSNikolay Aleksandrov 	memset(&sg_key, 0, sizeof(sg_key));
6019116ffbfSNikolay Aleksandrov 	sg_key.addr = src->pg->key.addr;
6029116ffbfSNikolay Aleksandrov 	sg_key.addr.src = src->addr.src;
6039116ffbfSNikolay Aleksandrov 	sg_key.port = src->pg->key.port;
6049116ffbfSNikolay Aleksandrov 
6059116ffbfSNikolay Aleksandrov 	sg = br_sg_port_find(src->br, &sg_key);
6069116ffbfSNikolay Aleksandrov 	if (!sg || (sg->flags & MDB_PG_FLAGS_PERMANENT))
6079116ffbfSNikolay Aleksandrov 		return;
6089116ffbfSNikolay Aleksandrov 
6099116ffbfSNikolay Aleksandrov 	old_flags = sg->flags;
6109116ffbfSNikolay Aleksandrov 	if (timer_pending(&src->timer))
6119116ffbfSNikolay Aleksandrov 		sg->flags &= ~MDB_PG_FLAGS_BLOCKED;
6129116ffbfSNikolay Aleksandrov 	else
6139116ffbfSNikolay Aleksandrov 		sg->flags |= MDB_PG_FLAGS_BLOCKED;
6149116ffbfSNikolay Aleksandrov 
6159116ffbfSNikolay Aleksandrov 	if (old_flags != sg->flags) {
6169116ffbfSNikolay Aleksandrov 		struct net_bridge_mdb_entry *sg_mp;
6179116ffbfSNikolay Aleksandrov 
6189116ffbfSNikolay Aleksandrov 		sg_mp = br_mdb_ip_get(src->br, &sg_key.addr);
6199116ffbfSNikolay Aleksandrov 		if (!sg_mp)
6209116ffbfSNikolay Aleksandrov 			return;
6219116ffbfSNikolay Aleksandrov 		br_mdb_notify(src->br->dev, sg_mp, sg, RTM_NEWMDB);
6229116ffbfSNikolay Aleksandrov 	}
623b0812368SNikolay Aleksandrov }
624b0812368SNikolay Aleksandrov 
br_multicast_destroy_mdb_entry(struct net_bridge_mcast_gc * gc)625e12cec65SNikolay Aleksandrov static void br_multicast_destroy_mdb_entry(struct net_bridge_mcast_gc *gc)
626e12cec65SNikolay Aleksandrov {
627e12cec65SNikolay Aleksandrov 	struct net_bridge_mdb_entry *mp;
628e12cec65SNikolay Aleksandrov 
629e12cec65SNikolay Aleksandrov 	mp = container_of(gc, struct net_bridge_mdb_entry, mcast_gc);
630e12cec65SNikolay Aleksandrov 	WARN_ON(!hlist_unhashed(&mp->mdb_node));
631e12cec65SNikolay Aleksandrov 	WARN_ON(mp->ports);
632e12cec65SNikolay Aleksandrov 
633292a089dSSteven Rostedt (Google) 	timer_shutdown_sync(&mp->timer);
634e12cec65SNikolay Aleksandrov 	kfree_rcu(mp, rcu);
635e12cec65SNikolay Aleksandrov }
636e12cec65SNikolay Aleksandrov 
br_multicast_del_mdb_entry(struct net_bridge_mdb_entry * mp)637e12cec65SNikolay Aleksandrov static void br_multicast_del_mdb_entry(struct net_bridge_mdb_entry *mp)
638e12cec65SNikolay Aleksandrov {
639e12cec65SNikolay Aleksandrov 	struct net_bridge *br = mp->br;
640e12cec65SNikolay Aleksandrov 
641e12cec65SNikolay Aleksandrov 	rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode,
642e12cec65SNikolay Aleksandrov 			       br_mdb_rht_params);
643e12cec65SNikolay Aleksandrov 	hlist_del_init_rcu(&mp->mdb_node);
644e12cec65SNikolay Aleksandrov 	hlist_add_head(&mp->mcast_gc.gc_node, &br->mcast_gc_list);
645e12cec65SNikolay Aleksandrov 	queue_work(system_long_wq, &br->mcast_gc_work);
646e12cec65SNikolay Aleksandrov }
647e12cec65SNikolay Aleksandrov 
br_multicast_group_expired(struct timer_list * t)64888c1f37fSAllen Pais static void br_multicast_group_expired(struct timer_list *t)
649eb1d1641SHerbert Xu {
65088c1f37fSAllen Pais 	struct net_bridge_mdb_entry *mp = from_timer(mp, t, timer);
651eb1d1641SHerbert Xu 	struct net_bridge *br = mp->br;
652eb1d1641SHerbert Xu 
653eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
654b0812368SNikolay Aleksandrov 	if (hlist_unhashed(&mp->mdb_node) || !netif_running(br->dev) ||
655b0812368SNikolay Aleksandrov 	    timer_pending(&mp->timer))
656eb1d1641SHerbert Xu 		goto out;
657eb1d1641SHerbert Xu 
6581bc844eeSNikolay Aleksandrov 	br_multicast_host_leave(mp, true);
659eb1d1641SHerbert Xu 
660eb1d1641SHerbert Xu 	if (mp->ports)
661eb1d1641SHerbert Xu 		goto out;
662e12cec65SNikolay Aleksandrov 	br_multicast_del_mdb_entry(mp);
663eb1d1641SHerbert Xu out:
664eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
665eb1d1641SHerbert Xu }
666eb1d1641SHerbert Xu 
br_multicast_destroy_group_src(struct net_bridge_mcast_gc * gc)667e12cec65SNikolay Aleksandrov static void br_multicast_destroy_group_src(struct net_bridge_mcast_gc *gc)
668e12cec65SNikolay Aleksandrov {
669e12cec65SNikolay Aleksandrov 	struct net_bridge_group_src *src;
670e12cec65SNikolay Aleksandrov 
671e12cec65SNikolay Aleksandrov 	src = container_of(gc, struct net_bridge_group_src, mcast_gc);
672e12cec65SNikolay Aleksandrov 	WARN_ON(!hlist_unhashed(&src->node));
673e12cec65SNikolay Aleksandrov 
674292a089dSSteven Rostedt (Google) 	timer_shutdown_sync(&src->timer);
675e12cec65SNikolay Aleksandrov 	kfree_rcu(src, rcu);
676e12cec65SNikolay Aleksandrov }
677e12cec65SNikolay Aleksandrov 
__br_multicast_del_group_src(struct net_bridge_group_src * src)678083e3534SIdo Schimmel void __br_multicast_del_group_src(struct net_bridge_group_src *src)
6798b671779SNikolay Aleksandrov {
680085b53c8SNikolay Aleksandrov 	struct net_bridge *br = src->pg->key.port->br;
6818b671779SNikolay Aleksandrov 
6828b671779SNikolay Aleksandrov 	hlist_del_init_rcu(&src->node);
6838b671779SNikolay Aleksandrov 	src->pg->src_ents--;
684e12cec65SNikolay Aleksandrov 	hlist_add_head(&src->mcast_gc.gc_node, &br->mcast_gc_list);
685e12cec65SNikolay Aleksandrov 	queue_work(system_long_wq, &br->mcast_gc_work);
686e12cec65SNikolay Aleksandrov }
687e12cec65SNikolay Aleksandrov 
br_multicast_del_group_src(struct net_bridge_group_src * src,bool fastleave)688083e3534SIdo Schimmel void br_multicast_del_group_src(struct net_bridge_group_src *src,
689083e3534SIdo Schimmel 				bool fastleave)
690083e3534SIdo Schimmel {
691083e3534SIdo Schimmel 	br_multicast_fwd_src_remove(src, fastleave);
692083e3534SIdo Schimmel 	__br_multicast_del_group_src(src);
693083e3534SIdo Schimmel }
694083e3534SIdo Schimmel 
695b57e8d87SPetr Machata static int
br_multicast_port_ngroups_inc_one(struct net_bridge_mcast_port * pmctx,struct netlink_ext_ack * extack,const char * what)696b57e8d87SPetr Machata br_multicast_port_ngroups_inc_one(struct net_bridge_mcast_port *pmctx,
697b57e8d87SPetr Machata 				  struct netlink_ext_ack *extack,
698b57e8d87SPetr Machata 				  const char *what)
699b57e8d87SPetr Machata {
700b57e8d87SPetr Machata 	u32 max = READ_ONCE(pmctx->mdb_max_entries);
701b57e8d87SPetr Machata 	u32 n = READ_ONCE(pmctx->mdb_n_entries);
702b57e8d87SPetr Machata 
703b57e8d87SPetr Machata 	if (max && n >= max) {
704b57e8d87SPetr Machata 		NL_SET_ERR_MSG_FMT_MOD(extack, "%s is already in %u groups, and mcast_max_groups=%u",
705b57e8d87SPetr Machata 				       what, n, max);
706b57e8d87SPetr Machata 		return -E2BIG;
707b57e8d87SPetr Machata 	}
708b57e8d87SPetr Machata 
709b57e8d87SPetr Machata 	WRITE_ONCE(pmctx->mdb_n_entries, n + 1);
710b57e8d87SPetr Machata 	return 0;
711b57e8d87SPetr Machata }
712b57e8d87SPetr Machata 
br_multicast_port_ngroups_dec_one(struct net_bridge_mcast_port * pmctx)713b57e8d87SPetr Machata static void br_multicast_port_ngroups_dec_one(struct net_bridge_mcast_port *pmctx)
714b57e8d87SPetr Machata {
715b57e8d87SPetr Machata 	u32 n = READ_ONCE(pmctx->mdb_n_entries);
716b57e8d87SPetr Machata 
717b57e8d87SPetr Machata 	WARN_ON_ONCE(n == 0);
718b57e8d87SPetr Machata 	WRITE_ONCE(pmctx->mdb_n_entries, n - 1);
719b57e8d87SPetr Machata }
720b57e8d87SPetr Machata 
br_multicast_port_ngroups_inc(struct net_bridge_port * port,const struct br_ip * group,struct netlink_ext_ack * extack)721b57e8d87SPetr Machata static int br_multicast_port_ngroups_inc(struct net_bridge_port *port,
722b57e8d87SPetr Machata 					 const struct br_ip *group,
723b57e8d87SPetr Machata 					 struct netlink_ext_ack *extack)
724b57e8d87SPetr Machata {
725b57e8d87SPetr Machata 	struct net_bridge_mcast_port *pmctx;
726b57e8d87SPetr Machata 	int err;
727b57e8d87SPetr Machata 
728b57e8d87SPetr Machata 	lockdep_assert_held_once(&port->br->multicast_lock);
729b57e8d87SPetr Machata 
730b57e8d87SPetr Machata 	/* Always count on the port context. */
731b57e8d87SPetr Machata 	err = br_multicast_port_ngroups_inc_one(&port->multicast_ctx, extack,
732b57e8d87SPetr Machata 						"Port");
733b57e8d87SPetr Machata 	if (err) {
734b57e8d87SPetr Machata 		trace_br_mdb_full(port->dev, group);
735b57e8d87SPetr Machata 		return err;
736b57e8d87SPetr Machata 	}
737b57e8d87SPetr Machata 
738b57e8d87SPetr Machata 	/* Only count on the VLAN context if VID is given, and if snooping on
739b57e8d87SPetr Machata 	 * that VLAN is enabled.
740b57e8d87SPetr Machata 	 */
741b57e8d87SPetr Machata 	if (!group->vid)
742b57e8d87SPetr Machata 		return 0;
743b57e8d87SPetr Machata 
744b57e8d87SPetr Machata 	pmctx = br_multicast_port_vid_to_port_ctx(port, group->vid);
745b57e8d87SPetr Machata 	if (!pmctx)
746b57e8d87SPetr Machata 		return 0;
747b57e8d87SPetr Machata 
748b57e8d87SPetr Machata 	err = br_multicast_port_ngroups_inc_one(pmctx, extack, "Port-VLAN");
749b57e8d87SPetr Machata 	if (err) {
750b57e8d87SPetr Machata 		trace_br_mdb_full(port->dev, group);
751b57e8d87SPetr Machata 		goto dec_one_out;
752b57e8d87SPetr Machata 	}
753b57e8d87SPetr Machata 
754b57e8d87SPetr Machata 	return 0;
755b57e8d87SPetr Machata 
756b57e8d87SPetr Machata dec_one_out:
757b57e8d87SPetr Machata 	br_multicast_port_ngroups_dec_one(&port->multicast_ctx);
758b57e8d87SPetr Machata 	return err;
759b57e8d87SPetr Machata }
760b57e8d87SPetr Machata 
br_multicast_port_ngroups_dec(struct net_bridge_port * port,u16 vid)761b57e8d87SPetr Machata static void br_multicast_port_ngroups_dec(struct net_bridge_port *port, u16 vid)
762b57e8d87SPetr Machata {
763b57e8d87SPetr Machata 	struct net_bridge_mcast_port *pmctx;
764b57e8d87SPetr Machata 
765b57e8d87SPetr Machata 	lockdep_assert_held_once(&port->br->multicast_lock);
766b57e8d87SPetr Machata 
767b57e8d87SPetr Machata 	if (vid) {
768b57e8d87SPetr Machata 		pmctx = br_multicast_port_vid_to_port_ctx(port, vid);
769b57e8d87SPetr Machata 		if (pmctx)
770b57e8d87SPetr Machata 			br_multicast_port_ngroups_dec_one(pmctx);
771b57e8d87SPetr Machata 	}
772b57e8d87SPetr Machata 	br_multicast_port_ngroups_dec_one(&port->multicast_ctx);
773b57e8d87SPetr Machata }
774b57e8d87SPetr Machata 
br_multicast_ngroups_get(const struct net_bridge_mcast_port * pmctx)775a1aee20dSPetr Machata u32 br_multicast_ngroups_get(const struct net_bridge_mcast_port *pmctx)
776a1aee20dSPetr Machata {
777a1aee20dSPetr Machata 	return READ_ONCE(pmctx->mdb_n_entries);
778a1aee20dSPetr Machata }
779a1aee20dSPetr Machata 
br_multicast_ngroups_set_max(struct net_bridge_mcast_port * pmctx,u32 max)780a1aee20dSPetr Machata void br_multicast_ngroups_set_max(struct net_bridge_mcast_port *pmctx, u32 max)
781a1aee20dSPetr Machata {
782a1aee20dSPetr Machata 	WRITE_ONCE(pmctx->mdb_max_entries, max);
783a1aee20dSPetr Machata }
784a1aee20dSPetr Machata 
br_multicast_ngroups_get_max(const struct net_bridge_mcast_port * pmctx)785a1aee20dSPetr Machata u32 br_multicast_ngroups_get_max(const struct net_bridge_mcast_port *pmctx)
786a1aee20dSPetr Machata {
787a1aee20dSPetr Machata 	return READ_ONCE(pmctx->mdb_max_entries);
788a1aee20dSPetr Machata }
789a1aee20dSPetr Machata 
br_multicast_destroy_port_group(struct net_bridge_mcast_gc * gc)790e12cec65SNikolay Aleksandrov static void br_multicast_destroy_port_group(struct net_bridge_mcast_gc *gc)
791e12cec65SNikolay Aleksandrov {
792e12cec65SNikolay Aleksandrov 	struct net_bridge_port_group *pg;
793e12cec65SNikolay Aleksandrov 
794e12cec65SNikolay Aleksandrov 	pg = container_of(gc, struct net_bridge_port_group, mcast_gc);
795e12cec65SNikolay Aleksandrov 	WARN_ON(!hlist_unhashed(&pg->mglist));
796e12cec65SNikolay Aleksandrov 	WARN_ON(!hlist_empty(&pg->src_list));
797e12cec65SNikolay Aleksandrov 
798292a089dSSteven Rostedt (Google) 	timer_shutdown_sync(&pg->rexmit_timer);
799292a089dSSteven Rostedt (Google) 	timer_shutdown_sync(&pg->timer);
800e12cec65SNikolay Aleksandrov 	kfree_rcu(pg, rcu);
8018b671779SNikolay Aleksandrov }
8028b671779SNikolay Aleksandrov 
br_multicast_del_pg(struct net_bridge_mdb_entry * mp,struct net_bridge_port_group * pg,struct net_bridge_port_group __rcu ** pp)803681590bdSNikolay Aleksandrov void br_multicast_del_pg(struct net_bridge_mdb_entry *mp,
804681590bdSNikolay Aleksandrov 			 struct net_bridge_port_group *pg,
805681590bdSNikolay Aleksandrov 			 struct net_bridge_port_group __rcu **pp)
806681590bdSNikolay Aleksandrov {
807085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
8088b671779SNikolay Aleksandrov 	struct net_bridge_group_src *ent;
8098b671779SNikolay Aleksandrov 	struct hlist_node *tmp;
810681590bdSNikolay Aleksandrov 
811681590bdSNikolay Aleksandrov 	rcu_assign_pointer(*pp, pg->next);
812681590bdSNikolay Aleksandrov 	hlist_del_init(&pg->mglist);
813baa74d39SNikolay Aleksandrov 	br_multicast_eht_clean_sets(pg);
8148b671779SNikolay Aleksandrov 	hlist_for_each_entry_safe(ent, tmp, &pg->src_list, node)
815d5a10222SNikolay Aleksandrov 		br_multicast_del_group_src(ent, false);
81681f19838SNikolay Aleksandrov 	br_mdb_notify(br->dev, mp, pg, RTM_DELMDB);
81774705582SNikolay Aleksandrov 	if (!br_multicast_is_star_g(&mp->addr)) {
81874705582SNikolay Aleksandrov 		rhashtable_remove_fast(&br->sg_port_tbl, &pg->rhnode,
81974705582SNikolay Aleksandrov 				       br_sg_port_rht_params);
8208266a049SNikolay Aleksandrov 		br_multicast_sg_del_exclude_ports(mp);
82174705582SNikolay Aleksandrov 	} else {
8228266a049SNikolay Aleksandrov 		br_multicast_star_g_handle_mode(pg, MCAST_INCLUDE);
82374705582SNikolay Aleksandrov 	}
824b57e8d87SPetr Machata 	br_multicast_port_ngroups_dec(pg->key.port, pg->key.addr.vid);
825e12cec65SNikolay Aleksandrov 	hlist_add_head(&pg->mcast_gc.gc_node, &br->mcast_gc_list);
826e12cec65SNikolay Aleksandrov 	queue_work(system_long_wq, &br->mcast_gc_work);
827681590bdSNikolay Aleksandrov 
828681590bdSNikolay Aleksandrov 	if (!mp->ports && !mp->host_joined && netif_running(br->dev))
829681590bdSNikolay Aleksandrov 		mod_timer(&mp->timer, jiffies);
830681590bdSNikolay Aleksandrov }
831681590bdSNikolay Aleksandrov 
br_multicast_find_del_pg(struct net_bridge * br,struct net_bridge_port_group * pg)832681590bdSNikolay Aleksandrov static void br_multicast_find_del_pg(struct net_bridge *br,
833eb1d1641SHerbert Xu 				     struct net_bridge_port_group *pg)
834eb1d1641SHerbert Xu {
8358b671779SNikolay Aleksandrov 	struct net_bridge_port_group __rcu **pp;
836eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
837eb1d1641SHerbert Xu 	struct net_bridge_port_group *p;
838e8051688SEric Dumazet 
839085b53c8SNikolay Aleksandrov 	mp = br_mdb_ip_get(br, &pg->key.addr);
840eb1d1641SHerbert Xu 	if (WARN_ON(!mp))
841eb1d1641SHerbert Xu 		return;
842eb1d1641SHerbert Xu 
843e8051688SEric Dumazet 	for (pp = &mp->ports;
844e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
845e8051688SEric Dumazet 	     pp = &p->next) {
846eb1d1641SHerbert Xu 		if (p != pg)
847eb1d1641SHerbert Xu 			continue;
848eb1d1641SHerbert Xu 
849681590bdSNikolay Aleksandrov 		br_multicast_del_pg(mp, pg, pp);
850eb1d1641SHerbert Xu 		return;
851eb1d1641SHerbert Xu 	}
852eb1d1641SHerbert Xu 
853eb1d1641SHerbert Xu 	WARN_ON(1);
854eb1d1641SHerbert Xu }
855eb1d1641SHerbert Xu 
br_multicast_port_group_expired(struct timer_list * t)85688c1f37fSAllen Pais static void br_multicast_port_group_expired(struct timer_list *t)
857eb1d1641SHerbert Xu {
85888c1f37fSAllen Pais 	struct net_bridge_port_group *pg = from_timer(pg, t, timer);
859d6c33d67SNikolay Aleksandrov 	struct net_bridge_group_src *src_ent;
860085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
861d6c33d67SNikolay Aleksandrov 	struct hlist_node *tmp;
862d6c33d67SNikolay Aleksandrov 	bool changed;
863eb1d1641SHerbert Xu 
864eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
865eb1d1641SHerbert Xu 	if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
8669d06b6d8SElad Raz 	    hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT)
867eb1d1641SHerbert Xu 		goto out;
868eb1d1641SHerbert Xu 
869d6c33d67SNikolay Aleksandrov 	changed = !!(pg->filter_mode == MCAST_EXCLUDE);
870d6c33d67SNikolay Aleksandrov 	pg->filter_mode = MCAST_INCLUDE;
871d6c33d67SNikolay Aleksandrov 	hlist_for_each_entry_safe(src_ent, tmp, &pg->src_list, node) {
872d6c33d67SNikolay Aleksandrov 		if (!timer_pending(&src_ent->timer)) {
873d5a10222SNikolay Aleksandrov 			br_multicast_del_group_src(src_ent, false);
874d6c33d67SNikolay Aleksandrov 			changed = true;
875d6c33d67SNikolay Aleksandrov 		}
876d6c33d67SNikolay Aleksandrov 	}
877eb1d1641SHerbert Xu 
878d6c33d67SNikolay Aleksandrov 	if (hlist_empty(&pg->src_list)) {
879d6c33d67SNikolay Aleksandrov 		br_multicast_find_del_pg(br, pg);
880d6c33d67SNikolay Aleksandrov 	} else if (changed) {
881085b53c8SNikolay Aleksandrov 		struct net_bridge_mdb_entry *mp = br_mdb_ip_get(br, &pg->key.addr);
882d6c33d67SNikolay Aleksandrov 
8838266a049SNikolay Aleksandrov 		if (changed && br_multicast_is_star_g(&pg->key.addr))
8848266a049SNikolay Aleksandrov 			br_multicast_star_g_handle_mode(pg, MCAST_INCLUDE);
8858266a049SNikolay Aleksandrov 
886d6c33d67SNikolay Aleksandrov 		if (WARN_ON(!mp))
887d6c33d67SNikolay Aleksandrov 			goto out;
888d6c33d67SNikolay Aleksandrov 		br_mdb_notify(br->dev, mp, pg, RTM_NEWMDB);
889d6c33d67SNikolay Aleksandrov 	}
890eb1d1641SHerbert Xu out:
891eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
892eb1d1641SHerbert Xu }
893eb1d1641SHerbert Xu 
br_multicast_gc(struct hlist_head * head)894e12cec65SNikolay Aleksandrov static void br_multicast_gc(struct hlist_head *head)
895e12cec65SNikolay Aleksandrov {
896e12cec65SNikolay Aleksandrov 	struct net_bridge_mcast_gc *gcent;
897e12cec65SNikolay Aleksandrov 	struct hlist_node *tmp;
898e12cec65SNikolay Aleksandrov 
899e12cec65SNikolay Aleksandrov 	hlist_for_each_entry_safe(gcent, tmp, head, gc_node) {
900e12cec65SNikolay Aleksandrov 		hlist_del_init(&gcent->gc_node);
901e12cec65SNikolay Aleksandrov 		gcent->destroy(gcent);
902e12cec65SNikolay Aleksandrov 	}
903e12cec65SNikolay Aleksandrov }
904e12cec65SNikolay Aleksandrov 
__br_multicast_query_handle_vlan(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct sk_buff * skb)905615cc23eSNikolay Aleksandrov static void __br_multicast_query_handle_vlan(struct net_bridge_mcast *brmctx,
906615cc23eSNikolay Aleksandrov 					     struct net_bridge_mcast_port *pmctx,
907615cc23eSNikolay Aleksandrov 					     struct sk_buff *skb)
908615cc23eSNikolay Aleksandrov {
909615cc23eSNikolay Aleksandrov 	struct net_bridge_vlan *vlan = NULL;
910615cc23eSNikolay Aleksandrov 
911615cc23eSNikolay Aleksandrov 	if (pmctx && br_multicast_port_ctx_is_vlan(pmctx))
912615cc23eSNikolay Aleksandrov 		vlan = pmctx->vlan;
913615cc23eSNikolay Aleksandrov 	else if (br_multicast_ctx_is_vlan(brmctx))
914615cc23eSNikolay Aleksandrov 		vlan = brmctx->vlan;
915615cc23eSNikolay Aleksandrov 
916615cc23eSNikolay Aleksandrov 	if (vlan && !(vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED)) {
917615cc23eSNikolay Aleksandrov 		u16 vlan_proto;
918615cc23eSNikolay Aleksandrov 
919615cc23eSNikolay Aleksandrov 		if (br_vlan_get_proto(brmctx->br->dev, &vlan_proto) != 0)
920615cc23eSNikolay Aleksandrov 			return;
921615cc23eSNikolay Aleksandrov 		__vlan_hwaccel_put_tag(skb, htons(vlan_proto), vlan->vid);
922615cc23eSNikolay Aleksandrov 	}
923615cc23eSNikolay Aleksandrov }
924615cc23eSNikolay Aleksandrov 
br_ip4_multicast_alloc_query(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct net_bridge_port_group * pg,__be32 ip_dst,__be32 group,bool with_srcs,bool over_lmqt,u8 sflag,u8 * igmp_type,bool * need_rexmit)925adc47037SNikolay Aleksandrov static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge_mcast *brmctx,
926615cc23eSNikolay Aleksandrov 						    struct net_bridge_mcast_port *pmctx,
927438ef2d0SNikolay Aleksandrov 						    struct net_bridge_port_group *pg,
928438ef2d0SNikolay Aleksandrov 						    __be32 ip_dst, __be32 group,
929438ef2d0SNikolay Aleksandrov 						    bool with_srcs, bool over_lmqt,
93042c11ccfSNikolay Aleksandrov 						    u8 sflag, u8 *igmp_type,
93142c11ccfSNikolay Aleksandrov 						    bool *need_rexmit)
932eb1d1641SHerbert Xu {
933085b53c8SNikolay Aleksandrov 	struct net_bridge_port *p = pg ? pg->key.port : NULL;
934438ef2d0SNikolay Aleksandrov 	struct net_bridge_group_src *ent;
935438ef2d0SNikolay Aleksandrov 	size_t pkt_size, igmp_hdr_size;
936438ef2d0SNikolay Aleksandrov 	unsigned long now = jiffies;
9375e923585SNikolay Aleksandrov 	struct igmpv3_query *ihv3;
938438ef2d0SNikolay Aleksandrov 	void *csum_start = NULL;
939438ef2d0SNikolay Aleksandrov 	__sum16 *csum = NULL;
940eb1d1641SHerbert Xu 	struct sk_buff *skb;
941eb1d1641SHerbert Xu 	struct igmphdr *ih;
942eb1d1641SHerbert Xu 	struct ethhdr *eth;
943438ef2d0SNikolay Aleksandrov 	unsigned long lmqt;
944eb1d1641SHerbert Xu 	struct iphdr *iph;
945438ef2d0SNikolay Aleksandrov 	u16 lmqt_srcs = 0;
946eb1d1641SHerbert Xu 
9475e923585SNikolay Aleksandrov 	igmp_hdr_size = sizeof(*ih);
948d3d065c0SNikolay Aleksandrov 	if (brmctx->multicast_igmp_version == 3) {
9495e923585SNikolay Aleksandrov 		igmp_hdr_size = sizeof(*ihv3);
950438ef2d0SNikolay Aleksandrov 		if (pg && with_srcs) {
951d3d065c0SNikolay Aleksandrov 			lmqt = now + (brmctx->multicast_last_member_interval *
952d3d065c0SNikolay Aleksandrov 				      brmctx->multicast_last_member_count);
953438ef2d0SNikolay Aleksandrov 			hlist_for_each_entry(ent, &pg->src_list, node) {
954438ef2d0SNikolay Aleksandrov 				if (over_lmqt == time_after(ent->timer.expires,
955438ef2d0SNikolay Aleksandrov 							    lmqt) &&
956438ef2d0SNikolay Aleksandrov 				    ent->src_query_rexmit_cnt > 0)
957438ef2d0SNikolay Aleksandrov 					lmqt_srcs++;
958438ef2d0SNikolay Aleksandrov 			}
959438ef2d0SNikolay Aleksandrov 
960438ef2d0SNikolay Aleksandrov 			if (!lmqt_srcs)
961438ef2d0SNikolay Aleksandrov 				return NULL;
962438ef2d0SNikolay Aleksandrov 			igmp_hdr_size += lmqt_srcs * sizeof(__be32);
963438ef2d0SNikolay Aleksandrov 		}
964438ef2d0SNikolay Aleksandrov 	}
965438ef2d0SNikolay Aleksandrov 
966438ef2d0SNikolay Aleksandrov 	pkt_size = sizeof(*eth) + sizeof(*iph) + 4 + igmp_hdr_size;
967438ef2d0SNikolay Aleksandrov 	if ((p && pkt_size > p->dev->mtu) ||
968adc47037SNikolay Aleksandrov 	    pkt_size > brmctx->br->dev->mtu)
969438ef2d0SNikolay Aleksandrov 		return NULL;
970438ef2d0SNikolay Aleksandrov 
971adc47037SNikolay Aleksandrov 	skb = netdev_alloc_skb_ip_align(brmctx->br->dev, pkt_size);
972eb1d1641SHerbert Xu 	if (!skb)
973eb1d1641SHerbert Xu 		goto out;
974eb1d1641SHerbert Xu 
975615cc23eSNikolay Aleksandrov 	__br_multicast_query_handle_vlan(brmctx, pmctx, skb);
976eb1d1641SHerbert Xu 	skb->protocol = htons(ETH_P_IP);
977eb1d1641SHerbert Xu 
978eb1d1641SHerbert Xu 	skb_reset_mac_header(skb);
979eb1d1641SHerbert Xu 	eth = eth_hdr(skb);
980eb1d1641SHerbert Xu 
981adc47037SNikolay Aleksandrov 	ether_addr_copy(eth->h_source, brmctx->br->dev->dev_addr);
982438ef2d0SNikolay Aleksandrov 	ip_eth_mc_map(ip_dst, eth->h_dest);
983eb1d1641SHerbert Xu 	eth->h_proto = htons(ETH_P_IP);
984eb1d1641SHerbert Xu 	skb_put(skb, sizeof(*eth));
985eb1d1641SHerbert Xu 
986eb1d1641SHerbert Xu 	skb_set_network_header(skb, skb->len);
987eb1d1641SHerbert Xu 	iph = ip_hdr(skb);
988438ef2d0SNikolay Aleksandrov 	iph->tot_len = htons(pkt_size - sizeof(*eth));
989eb1d1641SHerbert Xu 
990eb1d1641SHerbert Xu 	iph->version = 4;
991eb1d1641SHerbert Xu 	iph->ihl = 6;
992eb1d1641SHerbert Xu 	iph->tos = 0xc0;
993eb1d1641SHerbert Xu 	iph->id = 0;
994eb1d1641SHerbert Xu 	iph->frag_off = htons(IP_DF);
995eb1d1641SHerbert Xu 	iph->ttl = 1;
996eb1d1641SHerbert Xu 	iph->protocol = IPPROTO_IGMP;
997adc47037SNikolay Aleksandrov 	iph->saddr = br_opt_get(brmctx->br, BROPT_MULTICAST_QUERY_USE_IFADDR) ?
998adc47037SNikolay Aleksandrov 		     inet_select_addr(brmctx->br->dev, 0, RT_SCOPE_LINK) : 0;
999438ef2d0SNikolay Aleksandrov 	iph->daddr = ip_dst;
1000eb1d1641SHerbert Xu 	((u8 *)&iph[1])[0] = IPOPT_RA;
1001eb1d1641SHerbert Xu 	((u8 *)&iph[1])[1] = 4;
1002eb1d1641SHerbert Xu 	((u8 *)&iph[1])[2] = 0;
1003eb1d1641SHerbert Xu 	((u8 *)&iph[1])[3] = 0;
1004eb1d1641SHerbert Xu 	ip_send_check(iph);
1005eb1d1641SHerbert Xu 	skb_put(skb, 24);
1006eb1d1641SHerbert Xu 
1007eb1d1641SHerbert Xu 	skb_set_transport_header(skb, skb->len);
10081080ab95SNikolay Aleksandrov 	*igmp_type = IGMP_HOST_MEMBERSHIP_QUERY;
10095e923585SNikolay Aleksandrov 
1010d3d065c0SNikolay Aleksandrov 	switch (brmctx->multicast_igmp_version) {
10115e923585SNikolay Aleksandrov 	case 2:
10125e923585SNikolay Aleksandrov 		ih = igmp_hdr(skb);
1013eb1d1641SHerbert Xu 		ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
1014d3d065c0SNikolay Aleksandrov 		ih->code = (group ? brmctx->multicast_last_member_interval :
1015d3d065c0SNikolay Aleksandrov 				    brmctx->multicast_query_response_interval) /
1016eb1d1641SHerbert Xu 			   (HZ / IGMP_TIMER_SCALE);
1017eb1d1641SHerbert Xu 		ih->group = group;
1018eb1d1641SHerbert Xu 		ih->csum = 0;
1019438ef2d0SNikolay Aleksandrov 		csum = &ih->csum;
1020438ef2d0SNikolay Aleksandrov 		csum_start = (void *)ih;
10215e923585SNikolay Aleksandrov 		break;
10225e923585SNikolay Aleksandrov 	case 3:
10235e923585SNikolay Aleksandrov 		ihv3 = igmpv3_query_hdr(skb);
10245e923585SNikolay Aleksandrov 		ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY;
1025d3d065c0SNikolay Aleksandrov 		ihv3->code = (group ? brmctx->multicast_last_member_interval :
1026d3d065c0SNikolay Aleksandrov 				      brmctx->multicast_query_response_interval) /
10275e923585SNikolay Aleksandrov 			     (HZ / IGMP_TIMER_SCALE);
10285e923585SNikolay Aleksandrov 		ihv3->group = group;
1029d3d065c0SNikolay Aleksandrov 		ihv3->qqic = brmctx->multicast_query_interval / HZ;
1030438ef2d0SNikolay Aleksandrov 		ihv3->nsrcs = htons(lmqt_srcs);
10315e923585SNikolay Aleksandrov 		ihv3->resv = 0;
1032438ef2d0SNikolay Aleksandrov 		ihv3->suppress = sflag;
10335e923585SNikolay Aleksandrov 		ihv3->qrv = 2;
10345e923585SNikolay Aleksandrov 		ihv3->csum = 0;
1035438ef2d0SNikolay Aleksandrov 		csum = &ihv3->csum;
1036438ef2d0SNikolay Aleksandrov 		csum_start = (void *)ihv3;
1037438ef2d0SNikolay Aleksandrov 		if (!pg || !with_srcs)
1038438ef2d0SNikolay Aleksandrov 			break;
1039438ef2d0SNikolay Aleksandrov 
1040438ef2d0SNikolay Aleksandrov 		lmqt_srcs = 0;
1041438ef2d0SNikolay Aleksandrov 		hlist_for_each_entry(ent, &pg->src_list, node) {
1042438ef2d0SNikolay Aleksandrov 			if (over_lmqt == time_after(ent->timer.expires,
1043438ef2d0SNikolay Aleksandrov 						    lmqt) &&
1044438ef2d0SNikolay Aleksandrov 			    ent->src_query_rexmit_cnt > 0) {
1045deb96566SNikolay Aleksandrov 				ihv3->srcs[lmqt_srcs++] = ent->addr.src.ip4;
1046438ef2d0SNikolay Aleksandrov 				ent->src_query_rexmit_cnt--;
104742c11ccfSNikolay Aleksandrov 				if (need_rexmit && ent->src_query_rexmit_cnt)
104842c11ccfSNikolay Aleksandrov 					*need_rexmit = true;
1049438ef2d0SNikolay Aleksandrov 			}
1050438ef2d0SNikolay Aleksandrov 		}
1051438ef2d0SNikolay Aleksandrov 		if (WARN_ON(lmqt_srcs != ntohs(ihv3->nsrcs))) {
1052438ef2d0SNikolay Aleksandrov 			kfree_skb(skb);
1053438ef2d0SNikolay Aleksandrov 			return NULL;
1054438ef2d0SNikolay Aleksandrov 		}
10555e923585SNikolay Aleksandrov 		break;
10565e923585SNikolay Aleksandrov 	}
1057eb1d1641SHerbert Xu 
1058438ef2d0SNikolay Aleksandrov 	if (WARN_ON(!csum || !csum_start)) {
1059438ef2d0SNikolay Aleksandrov 		kfree_skb(skb);
1060438ef2d0SNikolay Aleksandrov 		return NULL;
1061438ef2d0SNikolay Aleksandrov 	}
1062438ef2d0SNikolay Aleksandrov 
1063438ef2d0SNikolay Aleksandrov 	*csum = ip_compute_csum(csum_start, igmp_hdr_size);
10645e923585SNikolay Aleksandrov 	skb_put(skb, igmp_hdr_size);
1065eb1d1641SHerbert Xu 	__skb_pull(skb, sizeof(*eth));
1066eb1d1641SHerbert Xu 
1067eb1d1641SHerbert Xu out:
1068eb1d1641SHerbert Xu 	return skb;
1069eb1d1641SHerbert Xu }
1070eb1d1641SHerbert Xu 
1071dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
br_ip6_multicast_alloc_query(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct net_bridge_port_group * pg,const struct in6_addr * ip6_dst,const struct in6_addr * group,bool with_srcs,bool over_llqt,u8 sflag,u8 * igmp_type,bool * need_rexmit)1072adc47037SNikolay Aleksandrov static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge_mcast *brmctx,
1073615cc23eSNikolay Aleksandrov 						    struct net_bridge_mcast_port *pmctx,
1074438ef2d0SNikolay Aleksandrov 						    struct net_bridge_port_group *pg,
1075438ef2d0SNikolay Aleksandrov 						    const struct in6_addr *ip6_dst,
1076438ef2d0SNikolay Aleksandrov 						    const struct in6_addr *group,
1077438ef2d0SNikolay Aleksandrov 						    bool with_srcs, bool over_llqt,
107842c11ccfSNikolay Aleksandrov 						    u8 sflag, u8 *igmp_type,
107942c11ccfSNikolay Aleksandrov 						    bool *need_rexmit)
108008b202b6SYOSHIFUJI Hideaki {
1081085b53c8SNikolay Aleksandrov 	struct net_bridge_port *p = pg ? pg->key.port : NULL;
1082438ef2d0SNikolay Aleksandrov 	struct net_bridge_group_src *ent;
1083438ef2d0SNikolay Aleksandrov 	size_t pkt_size, mld_hdr_size;
1084438ef2d0SNikolay Aleksandrov 	unsigned long now = jiffies;
1085aa2ae3e7SNikolay Aleksandrov 	struct mld2_query *mld2q;
1086438ef2d0SNikolay Aleksandrov 	void *csum_start = NULL;
1087aa2ae3e7SNikolay Aleksandrov 	unsigned long interval;
1088438ef2d0SNikolay Aleksandrov 	__sum16 *csum = NULL;
108908b202b6SYOSHIFUJI Hideaki 	struct ipv6hdr *ip6h;
109008b202b6SYOSHIFUJI Hideaki 	struct mld_msg *mldq;
1091aa2ae3e7SNikolay Aleksandrov 	struct sk_buff *skb;
1092438ef2d0SNikolay Aleksandrov 	unsigned long llqt;
109308b202b6SYOSHIFUJI Hideaki 	struct ethhdr *eth;
1094438ef2d0SNikolay Aleksandrov 	u16 llqt_srcs = 0;
109508b202b6SYOSHIFUJI Hideaki 	u8 *hopopt;
109608b202b6SYOSHIFUJI Hideaki 
1097aa2ae3e7SNikolay Aleksandrov 	mld_hdr_size = sizeof(*mldq);
1098d3d065c0SNikolay Aleksandrov 	if (brmctx->multicast_mld_version == 2) {
1099aa2ae3e7SNikolay Aleksandrov 		mld_hdr_size = sizeof(*mld2q);
1100438ef2d0SNikolay Aleksandrov 		if (pg && with_srcs) {
1101d3d065c0SNikolay Aleksandrov 			llqt = now + (brmctx->multicast_last_member_interval *
1102d3d065c0SNikolay Aleksandrov 				      brmctx->multicast_last_member_count);
1103438ef2d0SNikolay Aleksandrov 			hlist_for_each_entry(ent, &pg->src_list, node) {
1104438ef2d0SNikolay Aleksandrov 				if (over_llqt == time_after(ent->timer.expires,
1105438ef2d0SNikolay Aleksandrov 							    llqt) &&
1106438ef2d0SNikolay Aleksandrov 				    ent->src_query_rexmit_cnt > 0)
1107438ef2d0SNikolay Aleksandrov 					llqt_srcs++;
1108438ef2d0SNikolay Aleksandrov 			}
1109438ef2d0SNikolay Aleksandrov 
1110438ef2d0SNikolay Aleksandrov 			if (!llqt_srcs)
1111438ef2d0SNikolay Aleksandrov 				return NULL;
1112438ef2d0SNikolay Aleksandrov 			mld_hdr_size += llqt_srcs * sizeof(struct in6_addr);
1113438ef2d0SNikolay Aleksandrov 		}
1114438ef2d0SNikolay Aleksandrov 	}
1115438ef2d0SNikolay Aleksandrov 
1116438ef2d0SNikolay Aleksandrov 	pkt_size = sizeof(*eth) + sizeof(*ip6h) + 8 + mld_hdr_size;
1117438ef2d0SNikolay Aleksandrov 	if ((p && pkt_size > p->dev->mtu) ||
1118adc47037SNikolay Aleksandrov 	    pkt_size > brmctx->br->dev->mtu)
1119438ef2d0SNikolay Aleksandrov 		return NULL;
1120438ef2d0SNikolay Aleksandrov 
1121adc47037SNikolay Aleksandrov 	skb = netdev_alloc_skb_ip_align(brmctx->br->dev, pkt_size);
112208b202b6SYOSHIFUJI Hideaki 	if (!skb)
112308b202b6SYOSHIFUJI Hideaki 		goto out;
112408b202b6SYOSHIFUJI Hideaki 
1125615cc23eSNikolay Aleksandrov 	__br_multicast_query_handle_vlan(brmctx, pmctx, skb);
112608b202b6SYOSHIFUJI Hideaki 	skb->protocol = htons(ETH_P_IPV6);
112708b202b6SYOSHIFUJI Hideaki 
112808b202b6SYOSHIFUJI Hideaki 	/* Ethernet header */
112908b202b6SYOSHIFUJI Hideaki 	skb_reset_mac_header(skb);
113008b202b6SYOSHIFUJI Hideaki 	eth = eth_hdr(skb);
113108b202b6SYOSHIFUJI Hideaki 
1132adc47037SNikolay Aleksandrov 	ether_addr_copy(eth->h_source, brmctx->br->dev->dev_addr);
113308b202b6SYOSHIFUJI Hideaki 	eth->h_proto = htons(ETH_P_IPV6);
113408b202b6SYOSHIFUJI Hideaki 	skb_put(skb, sizeof(*eth));
113508b202b6SYOSHIFUJI Hideaki 
113608b202b6SYOSHIFUJI Hideaki 	/* IPv6 header + HbH option */
113708b202b6SYOSHIFUJI Hideaki 	skb_set_network_header(skb, skb->len);
113808b202b6SYOSHIFUJI Hideaki 	ip6h = ipv6_hdr(skb);
113908b202b6SYOSHIFUJI Hideaki 
114008b202b6SYOSHIFUJI Hideaki 	*(__force __be32 *)ip6h = htonl(0x60000000);
1141aa2ae3e7SNikolay Aleksandrov 	ip6h->payload_len = htons(8 + mld_hdr_size);
114208b202b6SYOSHIFUJI Hideaki 	ip6h->nexthdr = IPPROTO_HOPOPTS;
114308b202b6SYOSHIFUJI Hideaki 	ip6h->hop_limit = 1;
1144438ef2d0SNikolay Aleksandrov 	ip6h->daddr = *ip6_dst;
1145adc47037SNikolay Aleksandrov 	if (ipv6_dev_get_saddr(dev_net(brmctx->br->dev), brmctx->br->dev,
1146adc47037SNikolay Aleksandrov 			       &ip6h->daddr, 0, &ip6h->saddr)) {
1147d1d81d4cSUlrich Weber 		kfree_skb(skb);
1148adc47037SNikolay Aleksandrov 		br_opt_toggle(brmctx->br, BROPT_HAS_IPV6_ADDR, false);
1149d1d81d4cSUlrich Weber 		return NULL;
1150d1d81d4cSUlrich Weber 	}
11510888d5f3Sdaniel 
1152adc47037SNikolay Aleksandrov 	br_opt_toggle(brmctx->br, BROPT_HAS_IPV6_ADDR, true);
115336cff5a1SLinus Lüssing 	ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
115408b202b6SYOSHIFUJI Hideaki 
115508b202b6SYOSHIFUJI Hideaki 	hopopt = (u8 *)(ip6h + 1);
115608b202b6SYOSHIFUJI Hideaki 	hopopt[0] = IPPROTO_ICMPV6;		/* next hdr */
115708b202b6SYOSHIFUJI Hideaki 	hopopt[1] = 0;				/* length of HbH */
115808b202b6SYOSHIFUJI Hideaki 	hopopt[2] = IPV6_TLV_ROUTERALERT;	/* Router Alert */
115908b202b6SYOSHIFUJI Hideaki 	hopopt[3] = 2;				/* Length of RA Option */
116008b202b6SYOSHIFUJI Hideaki 	hopopt[4] = 0;				/* Type = 0x0000 (MLD) */
116108b202b6SYOSHIFUJI Hideaki 	hopopt[5] = 0;
11621de5a71cSEldad Zack 	hopopt[6] = IPV6_TLV_PAD1;		/* Pad1 */
11631de5a71cSEldad Zack 	hopopt[7] = IPV6_TLV_PAD1;		/* Pad1 */
116408b202b6SYOSHIFUJI Hideaki 
116508b202b6SYOSHIFUJI Hideaki 	skb_put(skb, sizeof(*ip6h) + 8);
116608b202b6SYOSHIFUJI Hideaki 
116708b202b6SYOSHIFUJI Hideaki 	/* ICMPv6 */
116808b202b6SYOSHIFUJI Hideaki 	skb_set_transport_header(skb, skb->len);
1169438ef2d0SNikolay Aleksandrov 	interval = ipv6_addr_any(group) ?
1170d3d065c0SNikolay Aleksandrov 			brmctx->multicast_query_response_interval :
1171d3d065c0SNikolay Aleksandrov 			brmctx->multicast_last_member_interval;
11721080ab95SNikolay Aleksandrov 	*igmp_type = ICMPV6_MGM_QUERY;
1173d3d065c0SNikolay Aleksandrov 	switch (brmctx->multicast_mld_version) {
1174aa2ae3e7SNikolay Aleksandrov 	case 1:
1175aa2ae3e7SNikolay Aleksandrov 		mldq = (struct mld_msg *)icmp6_hdr(skb);
117608b202b6SYOSHIFUJI Hideaki 		mldq->mld_type = ICMPV6_MGM_QUERY;
117708b202b6SYOSHIFUJI Hideaki 		mldq->mld_code = 0;
117808b202b6SYOSHIFUJI Hideaki 		mldq->mld_cksum = 0;
117908b202b6SYOSHIFUJI Hideaki 		mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
118008b202b6SYOSHIFUJI Hideaki 		mldq->mld_reserved = 0;
1181438ef2d0SNikolay Aleksandrov 		mldq->mld_mca = *group;
1182438ef2d0SNikolay Aleksandrov 		csum = &mldq->mld_cksum;
1183438ef2d0SNikolay Aleksandrov 		csum_start = (void *)mldq;
1184aa2ae3e7SNikolay Aleksandrov 		break;
1185aa2ae3e7SNikolay Aleksandrov 	case 2:
1186aa2ae3e7SNikolay Aleksandrov 		mld2q = (struct mld2_query *)icmp6_hdr(skb);
118753631a5fSLance Richardson 		mld2q->mld2q_mrc = htons((u16)jiffies_to_msecs(interval));
1188aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_type = ICMPV6_MGM_QUERY;
1189aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_code = 0;
1190aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_cksum = 0;
1191aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_resv1 = 0;
1192aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_resv2 = 0;
1193438ef2d0SNikolay Aleksandrov 		mld2q->mld2q_suppress = sflag;
1194aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_qrv = 2;
1195438ef2d0SNikolay Aleksandrov 		mld2q->mld2q_nsrcs = htons(llqt_srcs);
1196d3d065c0SNikolay Aleksandrov 		mld2q->mld2q_qqic = brmctx->multicast_query_interval / HZ;
1197438ef2d0SNikolay Aleksandrov 		mld2q->mld2q_mca = *group;
1198438ef2d0SNikolay Aleksandrov 		csum = &mld2q->mld2q_cksum;
1199438ef2d0SNikolay Aleksandrov 		csum_start = (void *)mld2q;
1200438ef2d0SNikolay Aleksandrov 		if (!pg || !with_srcs)
1201438ef2d0SNikolay Aleksandrov 			break;
1202438ef2d0SNikolay Aleksandrov 
1203438ef2d0SNikolay Aleksandrov 		llqt_srcs = 0;
1204438ef2d0SNikolay Aleksandrov 		hlist_for_each_entry(ent, &pg->src_list, node) {
1205438ef2d0SNikolay Aleksandrov 			if (over_llqt == time_after(ent->timer.expires,
1206438ef2d0SNikolay Aleksandrov 						    llqt) &&
1207438ef2d0SNikolay Aleksandrov 			    ent->src_query_rexmit_cnt > 0) {
1208deb96566SNikolay Aleksandrov 				mld2q->mld2q_srcs[llqt_srcs++] = ent->addr.src.ip6;
1209438ef2d0SNikolay Aleksandrov 				ent->src_query_rexmit_cnt--;
121042c11ccfSNikolay Aleksandrov 				if (need_rexmit && ent->src_query_rexmit_cnt)
121142c11ccfSNikolay Aleksandrov 					*need_rexmit = true;
1212438ef2d0SNikolay Aleksandrov 			}
1213438ef2d0SNikolay Aleksandrov 		}
1214438ef2d0SNikolay Aleksandrov 		if (WARN_ON(llqt_srcs != ntohs(mld2q->mld2q_nsrcs))) {
1215438ef2d0SNikolay Aleksandrov 			kfree_skb(skb);
1216438ef2d0SNikolay Aleksandrov 			return NULL;
1217438ef2d0SNikolay Aleksandrov 		}
1218aa2ae3e7SNikolay Aleksandrov 		break;
1219aa2ae3e7SNikolay Aleksandrov 	}
122008b202b6SYOSHIFUJI Hideaki 
1221438ef2d0SNikolay Aleksandrov 	if (WARN_ON(!csum || !csum_start)) {
1222438ef2d0SNikolay Aleksandrov 		kfree_skb(skb);
1223438ef2d0SNikolay Aleksandrov 		return NULL;
1224438ef2d0SNikolay Aleksandrov 	}
1225438ef2d0SNikolay Aleksandrov 
1226438ef2d0SNikolay Aleksandrov 	*csum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, mld_hdr_size,
1227438ef2d0SNikolay Aleksandrov 				IPPROTO_ICMPV6,
1228438ef2d0SNikolay Aleksandrov 				csum_partial(csum_start, mld_hdr_size, 0));
1229438ef2d0SNikolay Aleksandrov 	skb_put(skb, mld_hdr_size);
123008b202b6SYOSHIFUJI Hideaki 	__skb_pull(skb, sizeof(*eth));
123108b202b6SYOSHIFUJI Hideaki 
123208b202b6SYOSHIFUJI Hideaki out:
123308b202b6SYOSHIFUJI Hideaki 	return skb;
123408b202b6SYOSHIFUJI Hideaki }
123508b202b6SYOSHIFUJI Hideaki #endif
123608b202b6SYOSHIFUJI Hideaki 
br_multicast_alloc_query(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct net_bridge_port_group * pg,struct br_ip * ip_dst,struct br_ip * group,bool with_srcs,bool over_lmqt,u8 sflag,u8 * igmp_type,bool * need_rexmit)1237adc47037SNikolay Aleksandrov static struct sk_buff *br_multicast_alloc_query(struct net_bridge_mcast *brmctx,
1238615cc23eSNikolay Aleksandrov 						struct net_bridge_mcast_port *pmctx,
1239438ef2d0SNikolay Aleksandrov 						struct net_bridge_port_group *pg,
1240438ef2d0SNikolay Aleksandrov 						struct br_ip *ip_dst,
1241438ef2d0SNikolay Aleksandrov 						struct br_ip *group,
1242438ef2d0SNikolay Aleksandrov 						bool with_srcs, bool over_lmqt,
124342c11ccfSNikolay Aleksandrov 						u8 sflag, u8 *igmp_type,
124442c11ccfSNikolay Aleksandrov 						bool *need_rexmit)
12458ef2a9a5SYOSHIFUJI Hideaki {
1246438ef2d0SNikolay Aleksandrov 	__be32 ip4_dst;
1247438ef2d0SNikolay Aleksandrov 
1248438ef2d0SNikolay Aleksandrov 	switch (group->proto) {
12498ef2a9a5SYOSHIFUJI Hideaki 	case htons(ETH_P_IP):
1250eab3227bSNikolay Aleksandrov 		ip4_dst = ip_dst ? ip_dst->dst.ip4 : htonl(INADDR_ALLHOSTS_GROUP);
1251615cc23eSNikolay Aleksandrov 		return br_ip4_multicast_alloc_query(brmctx, pmctx, pg,
1252eab3227bSNikolay Aleksandrov 						    ip4_dst, group->dst.ip4,
1253438ef2d0SNikolay Aleksandrov 						    with_srcs, over_lmqt,
125442c11ccfSNikolay Aleksandrov 						    sflag, igmp_type,
125542c11ccfSNikolay Aleksandrov 						    need_rexmit);
1256dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1257438ef2d0SNikolay Aleksandrov 	case htons(ETH_P_IPV6): {
1258438ef2d0SNikolay Aleksandrov 		struct in6_addr ip6_dst;
1259438ef2d0SNikolay Aleksandrov 
1260438ef2d0SNikolay Aleksandrov 		if (ip_dst)
1261eab3227bSNikolay Aleksandrov 			ip6_dst = ip_dst->dst.ip6;
1262438ef2d0SNikolay Aleksandrov 		else
1263438ef2d0SNikolay Aleksandrov 			ipv6_addr_set(&ip6_dst, htonl(0xff020000), 0, 0,
1264438ef2d0SNikolay Aleksandrov 				      htonl(1));
1265438ef2d0SNikolay Aleksandrov 
1266615cc23eSNikolay Aleksandrov 		return br_ip6_multicast_alloc_query(brmctx, pmctx, pg,
1267eab3227bSNikolay Aleksandrov 						    &ip6_dst, &group->dst.ip6,
1268438ef2d0SNikolay Aleksandrov 						    with_srcs, over_lmqt,
126942c11ccfSNikolay Aleksandrov 						    sflag, igmp_type,
127042c11ccfSNikolay Aleksandrov 						    need_rexmit);
1271438ef2d0SNikolay Aleksandrov 	}
127208b202b6SYOSHIFUJI Hideaki #endif
12738ef2a9a5SYOSHIFUJI Hideaki 	}
12748ef2a9a5SYOSHIFUJI Hideaki 	return NULL;
12758ef2a9a5SYOSHIFUJI Hideaki }
12768ef2a9a5SYOSHIFUJI Hideaki 
br_multicast_new_group(struct net_bridge * br,struct br_ip * group)1277cfd56754SCong Wang struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
12785e923585SNikolay Aleksandrov 						    struct br_ip *group)
1279eb1d1641SHerbert Xu {
1280eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
12814c0833bcSTobias Klauser 	int err;
1282eb1d1641SHerbert Xu 
128319e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip_get(br, group);
128419e3a9c9SNikolay Aleksandrov 	if (mp)
128519e3a9c9SNikolay Aleksandrov 		return mp;
1286eb1d1641SHerbert Xu 
128719e3a9c9SNikolay Aleksandrov 	if (atomic_read(&br->mdb_hash_tbl.nelems) >= br->hash_max) {
1288b57e8d87SPetr Machata 		trace_br_mdb_full(br->dev, group);
1289c832962aSOleksandr Mazur 		br_mc_disabled_update(br->dev, false, NULL);
129019e3a9c9SNikolay Aleksandrov 		br_opt_toggle(br, BROPT_MULTICAST_ENABLED, false);
129119e3a9c9SNikolay Aleksandrov 		return ERR_PTR(-E2BIG);
1292eb1d1641SHerbert Xu 	}
1293eb1d1641SHerbert Xu 
1294eb1d1641SHerbert Xu 	mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
1295eb1d1641SHerbert Xu 	if (unlikely(!mp))
12964c0833bcSTobias Klauser 		return ERR_PTR(-ENOMEM);
1297eb1d1641SHerbert Xu 
1298eb1d1641SHerbert Xu 	mp->br = br;
12998ef2a9a5SYOSHIFUJI Hideaki 	mp->addr = *group;
1300e12cec65SNikolay Aleksandrov 	mp->mcast_gc.destroy = br_multicast_destroy_mdb_entry;
130188c1f37fSAllen Pais 	timer_setup(&mp->timer, br_multicast_group_expired, 0);
130219e3a9c9SNikolay Aleksandrov 	err = rhashtable_lookup_insert_fast(&br->mdb_hash_tbl, &mp->rhnode,
130319e3a9c9SNikolay Aleksandrov 					    br_mdb_rht_params);
130419e3a9c9SNikolay Aleksandrov 	if (err) {
130519e3a9c9SNikolay Aleksandrov 		kfree(mp);
130619e3a9c9SNikolay Aleksandrov 		mp = ERR_PTR(err);
130719e3a9c9SNikolay Aleksandrov 	} else {
130819e3a9c9SNikolay Aleksandrov 		hlist_add_head_rcu(&mp->mdb_node, &br->mdb_list);
130919e3a9c9SNikolay Aleksandrov 	}
13101faabf2aSEric Dumazet 
1311eb1d1641SHerbert Xu 	return mp;
1312eb1d1641SHerbert Xu }
1313eb1d1641SHerbert Xu 
br_multicast_group_src_expired(struct timer_list * t)13148b671779SNikolay Aleksandrov static void br_multicast_group_src_expired(struct timer_list *t)
13158b671779SNikolay Aleksandrov {
13168b671779SNikolay Aleksandrov 	struct net_bridge_group_src *src = from_timer(src, t, timer);
13178b671779SNikolay Aleksandrov 	struct net_bridge_port_group *pg;
13188b671779SNikolay Aleksandrov 	struct net_bridge *br = src->br;
13198b671779SNikolay Aleksandrov 
13208b671779SNikolay Aleksandrov 	spin_lock(&br->multicast_lock);
13218b671779SNikolay Aleksandrov 	if (hlist_unhashed(&src->node) || !netif_running(br->dev) ||
13228b671779SNikolay Aleksandrov 	    timer_pending(&src->timer))
13238b671779SNikolay Aleksandrov 		goto out;
13248b671779SNikolay Aleksandrov 
13258b671779SNikolay Aleksandrov 	pg = src->pg;
13268b671779SNikolay Aleksandrov 	if (pg->filter_mode == MCAST_INCLUDE) {
1327d5a10222SNikolay Aleksandrov 		br_multicast_del_group_src(src, false);
13288b671779SNikolay Aleksandrov 		if (!hlist_empty(&pg->src_list))
13298b671779SNikolay Aleksandrov 			goto out;
13308b671779SNikolay Aleksandrov 		br_multicast_find_del_pg(br, pg);
13319116ffbfSNikolay Aleksandrov 	} else {
13329116ffbfSNikolay Aleksandrov 		br_multicast_fwd_src_handle(src);
13338b671779SNikolay Aleksandrov 	}
13349116ffbfSNikolay Aleksandrov 
13358b671779SNikolay Aleksandrov out:
13368b671779SNikolay Aleksandrov 	spin_unlock(&br->multicast_lock);
13378b671779SNikolay Aleksandrov }
13388b671779SNikolay Aleksandrov 
1339474ddb37SNikolay Aleksandrov struct net_bridge_group_src *
br_multicast_find_group_src(struct net_bridge_port_group * pg,struct br_ip * ip)13408b671779SNikolay Aleksandrov br_multicast_find_group_src(struct net_bridge_port_group *pg, struct br_ip *ip)
13418b671779SNikolay Aleksandrov {
13428b671779SNikolay Aleksandrov 	struct net_bridge_group_src *ent;
13438b671779SNikolay Aleksandrov 
13448b671779SNikolay Aleksandrov 	switch (ip->proto) {
13458b671779SNikolay Aleksandrov 	case htons(ETH_P_IP):
13468b671779SNikolay Aleksandrov 		hlist_for_each_entry(ent, &pg->src_list, node)
1347deb96566SNikolay Aleksandrov 			if (ip->src.ip4 == ent->addr.src.ip4)
13488b671779SNikolay Aleksandrov 				return ent;
13498b671779SNikolay Aleksandrov 		break;
13508b671779SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
13518b671779SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
13528b671779SNikolay Aleksandrov 		hlist_for_each_entry(ent, &pg->src_list, node)
1353deb96566SNikolay Aleksandrov 			if (!ipv6_addr_cmp(&ent->addr.src.ip6, &ip->src.ip6))
13548b671779SNikolay Aleksandrov 				return ent;
13558b671779SNikolay Aleksandrov 		break;
13568b671779SNikolay Aleksandrov #endif
13578b671779SNikolay Aleksandrov 	}
13588b671779SNikolay Aleksandrov 
13598b671779SNikolay Aleksandrov 	return NULL;
13608b671779SNikolay Aleksandrov }
13618b671779SNikolay Aleksandrov 
1362fd0c6961SIdo Schimmel struct net_bridge_group_src *
br_multicast_new_group_src(struct net_bridge_port_group * pg,struct br_ip * src_ip)13638b671779SNikolay Aleksandrov br_multicast_new_group_src(struct net_bridge_port_group *pg, struct br_ip *src_ip)
13648b671779SNikolay Aleksandrov {
13658b671779SNikolay Aleksandrov 	struct net_bridge_group_src *grp_src;
13668b671779SNikolay Aleksandrov 
13678b671779SNikolay Aleksandrov 	if (unlikely(pg->src_ents >= PG_SRC_ENT_LIMIT))
13688b671779SNikolay Aleksandrov 		return NULL;
13698b671779SNikolay Aleksandrov 
13708b671779SNikolay Aleksandrov 	switch (src_ip->proto) {
13718b671779SNikolay Aleksandrov 	case htons(ETH_P_IP):
1372deb96566SNikolay Aleksandrov 		if (ipv4_is_zeronet(src_ip->src.ip4) ||
1373deb96566SNikolay Aleksandrov 		    ipv4_is_multicast(src_ip->src.ip4))
13748b671779SNikolay Aleksandrov 			return NULL;
13758b671779SNikolay Aleksandrov 		break;
13768b671779SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
13778b671779SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
1378deb96566SNikolay Aleksandrov 		if (ipv6_addr_any(&src_ip->src.ip6) ||
1379deb96566SNikolay Aleksandrov 		    ipv6_addr_is_multicast(&src_ip->src.ip6))
13808b671779SNikolay Aleksandrov 			return NULL;
13818b671779SNikolay Aleksandrov 		break;
13828b671779SNikolay Aleksandrov #endif
13838b671779SNikolay Aleksandrov 	}
13848b671779SNikolay Aleksandrov 
13858b671779SNikolay Aleksandrov 	grp_src = kzalloc(sizeof(*grp_src), GFP_ATOMIC);
13868b671779SNikolay Aleksandrov 	if (unlikely(!grp_src))
13878b671779SNikolay Aleksandrov 		return NULL;
13888b671779SNikolay Aleksandrov 
13898b671779SNikolay Aleksandrov 	grp_src->pg = pg;
1390085b53c8SNikolay Aleksandrov 	grp_src->br = pg->key.port->br;
13918b671779SNikolay Aleksandrov 	grp_src->addr = *src_ip;
1392e12cec65SNikolay Aleksandrov 	grp_src->mcast_gc.destroy = br_multicast_destroy_group_src;
13938b671779SNikolay Aleksandrov 	timer_setup(&grp_src->timer, br_multicast_group_src_expired, 0);
13948b671779SNikolay Aleksandrov 
13958b671779SNikolay Aleksandrov 	hlist_add_head_rcu(&grp_src->node, &pg->src_list);
13968b671779SNikolay Aleksandrov 	pg->src_ents++;
13978b671779SNikolay Aleksandrov 
13988b671779SNikolay Aleksandrov 	return grp_src;
13998b671779SNikolay Aleksandrov }
14008b671779SNikolay Aleksandrov 
br_multicast_new_port_group(struct net_bridge_port * port,const struct br_ip * group,struct net_bridge_port_group __rcu * next,unsigned char flags,const unsigned char * src,u8 filter_mode,u8 rt_protocol,struct netlink_ext_ack * extack)1401cfd56754SCong Wang struct net_bridge_port_group *br_multicast_new_port_group(
1402cfd56754SCong Wang 			struct net_bridge_port *port,
1403f86c3e2cSIdo Schimmel 			const struct br_ip *group,
1404ccb1c31aSAmerigo Wang 			struct net_bridge_port_group __rcu *next,
14056db6f0eaSFelix Fietkau 			unsigned char flags,
14068b671779SNikolay Aleksandrov 			const unsigned char *src,
14078f8cb77eSNikolay Aleksandrov 			u8 filter_mode,
140860977a0cSPetr Machata 			u8 rt_protocol,
140960977a0cSPetr Machata 			struct netlink_ext_ack *extack)
1410cfd56754SCong Wang {
1411cfd56754SCong Wang 	struct net_bridge_port_group *p;
1412b57e8d87SPetr Machata 	int err;
1413b57e8d87SPetr Machata 
1414b57e8d87SPetr Machata 	err = br_multicast_port_ngroups_inc(port, group, extack);
1415b57e8d87SPetr Machata 	if (err)
1416b57e8d87SPetr Machata 		return NULL;
1417cfd56754SCong Wang 
1418cfd56754SCong Wang 	p = kzalloc(sizeof(*p), GFP_ATOMIC);
14191c85b80bSPetr Machata 	if (unlikely(!p)) {
14201c85b80bSPetr Machata 		NL_SET_ERR_MSG_MOD(extack, "Couldn't allocate new port group");
1421b57e8d87SPetr Machata 		goto dec_out;
14221c85b80bSPetr Machata 	}
1423cfd56754SCong Wang 
1424085b53c8SNikolay Aleksandrov 	p->key.addr = *group;
1425085b53c8SNikolay Aleksandrov 	p->key.port = port;
14269d06b6d8SElad Raz 	p->flags = flags;
14278b671779SNikolay Aleksandrov 	p->filter_mode = filter_mode;
14288f8cb77eSNikolay Aleksandrov 	p->rt_protocol = rt_protocol;
14295b163288SNikolay Aleksandrov 	p->eht_host_tree = RB_ROOT;
1430baa74d39SNikolay Aleksandrov 	p->eht_set_tree = RB_ROOT;
1431e12cec65SNikolay Aleksandrov 	p->mcast_gc.destroy = br_multicast_destroy_port_group;
14328b671779SNikolay Aleksandrov 	INIT_HLIST_HEAD(&p->src_list);
1433085b53c8SNikolay Aleksandrov 
1434085b53c8SNikolay Aleksandrov 	if (!br_multicast_is_star_g(group) &&
1435085b53c8SNikolay Aleksandrov 	    rhashtable_lookup_insert_fast(&port->br->sg_port_tbl, &p->rhnode,
1436085b53c8SNikolay Aleksandrov 					  br_sg_port_rht_params)) {
14371c85b80bSPetr Machata 		NL_SET_ERR_MSG_MOD(extack, "Couldn't insert new port group");
1438eceb3085SPetr Machata 		goto free_out;
1439085b53c8SNikolay Aleksandrov 	}
1440085b53c8SNikolay Aleksandrov 
1441eca2a43bSstephen hemminger 	rcu_assign_pointer(p->next, next);
144288c1f37fSAllen Pais 	timer_setup(&p->timer, br_multicast_port_group_expired, 0);
144342c11ccfSNikolay Aleksandrov 	timer_setup(&p->rexmit_timer, br_multicast_port_group_rexmit, 0);
144442c11ccfSNikolay Aleksandrov 	hlist_add_head(&p->mglist, &port->mglist);
14456db6f0eaSFelix Fietkau 
14466db6f0eaSFelix Fietkau 	if (src)
14476db6f0eaSFelix Fietkau 		memcpy(p->eth_addr, src, ETH_ALEN);
14486db6f0eaSFelix Fietkau 	else
14491bfe45f4SMao Wenan 		eth_broadcast_addr(p->eth_addr);
14506db6f0eaSFelix Fietkau 
1451cfd56754SCong Wang 	return p;
1452eceb3085SPetr Machata 
1453eceb3085SPetr Machata free_out:
1454eceb3085SPetr Machata 	kfree(p);
1455b57e8d87SPetr Machata dec_out:
1456b57e8d87SPetr Machata 	br_multicast_port_ngroups_dec(port, group->vid);
1457eceb3085SPetr Machata 	return NULL;
1458cfd56754SCong Wang }
1459cfd56754SCong Wang 
br_multicast_del_port_group(struct net_bridge_port_group * p)1460976b3858SPetr Machata void br_multicast_del_port_group(struct net_bridge_port_group *p)
1461976b3858SPetr Machata {
1462976b3858SPetr Machata 	struct net_bridge_port *port = p->key.port;
1463b57e8d87SPetr Machata 	__u16 vid = p->key.addr.vid;
1464976b3858SPetr Machata 
1465976b3858SPetr Machata 	hlist_del_init(&p->mglist);
1466976b3858SPetr Machata 	if (!br_multicast_is_star_g(&p->key.addr))
1467976b3858SPetr Machata 		rhashtable_remove_fast(&port->br->sg_port_tbl, &p->rhnode,
1468976b3858SPetr Machata 				       br_sg_port_rht_params);
1469976b3858SPetr Machata 	kfree(p);
1470b57e8d87SPetr Machata 	br_multicast_port_ngroups_dec(port, vid);
1471976b3858SPetr Machata }
1472976b3858SPetr Machata 
br_multicast_host_join(const struct net_bridge_mcast * brmctx,struct net_bridge_mdb_entry * mp,bool notify)147358d913a3SNikolay Aleksandrov void br_multicast_host_join(const struct net_bridge_mcast *brmctx,
147458d913a3SNikolay Aleksandrov 			    struct net_bridge_mdb_entry *mp, bool notify)
14751bc844eeSNikolay Aleksandrov {
14761bc844eeSNikolay Aleksandrov 	if (!mp->host_joined) {
14771bc844eeSNikolay Aleksandrov 		mp->host_joined = true;
1478094b82fdSNikolay Aleksandrov 		if (br_multicast_is_star_g(&mp->addr))
1479094b82fdSNikolay Aleksandrov 			br_multicast_star_g_host_state(mp);
14801bc844eeSNikolay Aleksandrov 		if (notify)
148181f19838SNikolay Aleksandrov 			br_mdb_notify(mp->br->dev, mp, NULL, RTM_NEWMDB);
14821bc844eeSNikolay Aleksandrov 	}
1483955062b0SNikolay Aleksandrov 
1484955062b0SNikolay Aleksandrov 	if (br_group_is_l2(&mp->addr))
1485955062b0SNikolay Aleksandrov 		return;
1486955062b0SNikolay Aleksandrov 
148758d913a3SNikolay Aleksandrov 	mod_timer(&mp->timer, jiffies + brmctx->multicast_membership_interval);
14881bc844eeSNikolay Aleksandrov }
14891bc844eeSNikolay Aleksandrov 
br_multicast_host_leave(struct net_bridge_mdb_entry * mp,bool notify)14901bc844eeSNikolay Aleksandrov void br_multicast_host_leave(struct net_bridge_mdb_entry *mp, bool notify)
14911bc844eeSNikolay Aleksandrov {
14921bc844eeSNikolay Aleksandrov 	if (!mp->host_joined)
14931bc844eeSNikolay Aleksandrov 		return;
14941bc844eeSNikolay Aleksandrov 
14951bc844eeSNikolay Aleksandrov 	mp->host_joined = false;
1496094b82fdSNikolay Aleksandrov 	if (br_multicast_is_star_g(&mp->addr))
1497094b82fdSNikolay Aleksandrov 		br_multicast_star_g_host_state(mp);
14981bc844eeSNikolay Aleksandrov 	if (notify)
149981f19838SNikolay Aleksandrov 		br_mdb_notify(mp->br->dev, mp, NULL, RTM_DELMDB);
15001bc844eeSNikolay Aleksandrov }
15011bc844eeSNikolay Aleksandrov 
1502b0812368SNikolay Aleksandrov static struct net_bridge_port_group *
__br_multicast_add_group(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct br_ip * group,const unsigned char * src,u8 filter_mode,bool igmpv2_mldv1,bool blocked)1503adc47037SNikolay Aleksandrov __br_multicast_add_group(struct net_bridge_mcast *brmctx,
1504adc47037SNikolay Aleksandrov 			 struct net_bridge_mcast_port *pmctx,
15056db6f0eaSFelix Fietkau 			 struct br_ip *group,
15068b671779SNikolay Aleksandrov 			 const unsigned char *src,
15070436862eSNikolay Aleksandrov 			 u8 filter_mode,
15089116ffbfSNikolay Aleksandrov 			 bool igmpv2_mldv1,
15099116ffbfSNikolay Aleksandrov 			 bool blocked)
1510eb1d1641SHerbert Xu {
1511e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
1512b0812368SNikolay Aleksandrov 	struct net_bridge_port_group *p = NULL;
15135e923585SNikolay Aleksandrov 	struct net_bridge_mdb_entry *mp;
1514454594f3SLinus Lüssing 	unsigned long now = jiffies;
1515eb1d1641SHerbert Xu 
15164cdd0d10SNikolay Aleksandrov 	if (!br_multicast_ctx_should_use(brmctx, pmctx))
1517eb1d1641SHerbert Xu 		goto out;
1518eb1d1641SHerbert Xu 
1519adc47037SNikolay Aleksandrov 	mp = br_multicast_new_group(brmctx->br, group);
15204c0833bcSTobias Klauser 	if (IS_ERR(mp))
15211697291dSXu Wang 		return ERR_CAST(mp);
1522eb1d1641SHerbert Xu 
1523adc47037SNikolay Aleksandrov 	if (!pmctx) {
152458d913a3SNikolay Aleksandrov 		br_multicast_host_join(brmctx, mp, true);
1525eb1d1641SHerbert Xu 		goto out;
1526eb1d1641SHerbert Xu 	}
1527eb1d1641SHerbert Xu 
1528e8051688SEric Dumazet 	for (pp = &mp->ports;
1529adc47037SNikolay Aleksandrov 	     (p = mlock_dereference(*pp, brmctx->br)) != NULL;
1530e8051688SEric Dumazet 	     pp = &p->next) {
1531adc47037SNikolay Aleksandrov 		if (br_port_group_equal(p, pmctx->port, src))
1532454594f3SLinus Lüssing 			goto found;
1533adc47037SNikolay Aleksandrov 		if ((unsigned long)p->key.port < (unsigned long)pmctx->port)
1534eb1d1641SHerbert Xu 			break;
1535eb1d1641SHerbert Xu 	}
1536eb1d1641SHerbert Xu 
1537adc47037SNikolay Aleksandrov 	p = br_multicast_new_port_group(pmctx->port, group, *pp, 0, src,
153860977a0cSPetr Machata 					filter_mode, RTPROT_KERNEL, NULL);
1539b0812368SNikolay Aleksandrov 	if (unlikely(!p)) {
1540b0812368SNikolay Aleksandrov 		p = ERR_PTR(-ENOMEM);
1541b0812368SNikolay Aleksandrov 		goto out;
1542b0812368SNikolay Aleksandrov 	}
1543eb1d1641SHerbert Xu 	rcu_assign_pointer(*pp, p);
15449116ffbfSNikolay Aleksandrov 	if (blocked)
15459116ffbfSNikolay Aleksandrov 		p->flags |= MDB_PG_FLAGS_BLOCKED;
1546adc47037SNikolay Aleksandrov 	br_mdb_notify(brmctx->br->dev, mp, p, RTM_NEWMDB);
1547eb1d1641SHerbert Xu 
1548454594f3SLinus Lüssing found:
15490436862eSNikolay Aleksandrov 	if (igmpv2_mldv1)
1550d3d065c0SNikolay Aleksandrov 		mod_timer(&p->timer,
1551adc47037SNikolay Aleksandrov 			  now + brmctx->multicast_membership_interval);
155281f19838SNikolay Aleksandrov 
1553eb1d1641SHerbert Xu out:
1554b0812368SNikolay Aleksandrov 	return p;
1555b0812368SNikolay Aleksandrov }
1556eb1d1641SHerbert Xu 
br_multicast_add_group(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct br_ip * group,const unsigned char * src,u8 filter_mode,bool igmpv2_mldv1)1557adc47037SNikolay Aleksandrov static int br_multicast_add_group(struct net_bridge_mcast *brmctx,
1558adc47037SNikolay Aleksandrov 				  struct net_bridge_mcast_port *pmctx,
1559b0812368SNikolay Aleksandrov 				  struct br_ip *group,
1560b0812368SNikolay Aleksandrov 				  const unsigned char *src,
1561b0812368SNikolay Aleksandrov 				  u8 filter_mode,
1562b0812368SNikolay Aleksandrov 				  bool igmpv2_mldv1)
1563b0812368SNikolay Aleksandrov {
1564b0812368SNikolay Aleksandrov 	struct net_bridge_port_group *pg;
1565b0812368SNikolay Aleksandrov 	int err;
1566b0812368SNikolay Aleksandrov 
1567adc47037SNikolay Aleksandrov 	spin_lock(&brmctx->br->multicast_lock);
1568adc47037SNikolay Aleksandrov 	pg = __br_multicast_add_group(brmctx, pmctx, group, src, filter_mode,
15699116ffbfSNikolay Aleksandrov 				      igmpv2_mldv1, false);
1570b0812368SNikolay Aleksandrov 	/* NULL is considered valid for host joined groups */
15718d21c882SJiapeng Zhong 	err = PTR_ERR_OR_ZERO(pg);
1572adc47037SNikolay Aleksandrov 	spin_unlock(&brmctx->br->multicast_lock);
1573b0812368SNikolay Aleksandrov 
1574eb1d1641SHerbert Xu 	return err;
1575eb1d1641SHerbert Xu }
1576eb1d1641SHerbert Xu 
br_ip4_multicast_add_group(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,__be32 group,__u16 vid,const unsigned char * src,bool igmpv2)1577adc47037SNikolay Aleksandrov static int br_ip4_multicast_add_group(struct net_bridge_mcast *brmctx,
1578adc47037SNikolay Aleksandrov 				      struct net_bridge_mcast_port *pmctx,
1579b0e9a30dSVlad Yasevich 				      __be32 group,
15806db6f0eaSFelix Fietkau 				      __u16 vid,
15818b671779SNikolay Aleksandrov 				      const unsigned char *src,
15828b671779SNikolay Aleksandrov 				      bool igmpv2)
15838ef2a9a5SYOSHIFUJI Hideaki {
15848ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip br_group;
15858b671779SNikolay Aleksandrov 	u8 filter_mode;
15868ef2a9a5SYOSHIFUJI Hideaki 
15878ef2a9a5SYOSHIFUJI Hideaki 	if (ipv4_is_local_multicast(group))
15888ef2a9a5SYOSHIFUJI Hideaki 		return 0;
15898ef2a9a5SYOSHIFUJI Hideaki 
15901515a63fSNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
1591eab3227bSNikolay Aleksandrov 	br_group.dst.ip4 = group;
15928ef2a9a5SYOSHIFUJI Hideaki 	br_group.proto = htons(ETH_P_IP);
1593b0e9a30dSVlad Yasevich 	br_group.vid = vid;
15948b671779SNikolay Aleksandrov 	filter_mode = igmpv2 ? MCAST_EXCLUDE : MCAST_INCLUDE;
15958ef2a9a5SYOSHIFUJI Hideaki 
1596adc47037SNikolay Aleksandrov 	return br_multicast_add_group(brmctx, pmctx, &br_group, src,
1597adc47037SNikolay Aleksandrov 				      filter_mode, igmpv2);
15988ef2a9a5SYOSHIFUJI Hideaki }
15998ef2a9a5SYOSHIFUJI Hideaki 
1600dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
br_ip6_multicast_add_group(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,const struct in6_addr * group,__u16 vid,const unsigned char * src,bool mldv1)1601adc47037SNikolay Aleksandrov static int br_ip6_multicast_add_group(struct net_bridge_mcast *brmctx,
1602adc47037SNikolay Aleksandrov 				      struct net_bridge_mcast_port *pmctx,
1603b0e9a30dSVlad Yasevich 				      const struct in6_addr *group,
16046db6f0eaSFelix Fietkau 				      __u16 vid,
16058b671779SNikolay Aleksandrov 				      const unsigned char *src,
16068b671779SNikolay Aleksandrov 				      bool mldv1)
160708b202b6SYOSHIFUJI Hideaki {
160808b202b6SYOSHIFUJI Hideaki 	struct br_ip br_group;
16098b671779SNikolay Aleksandrov 	u8 filter_mode;
161008b202b6SYOSHIFUJI Hideaki 
16113c3769e6SLinus Lüssing 	if (ipv6_addr_is_ll_all_nodes(group))
161208b202b6SYOSHIFUJI Hideaki 		return 0;
161308b202b6SYOSHIFUJI Hideaki 
161419e3a9c9SNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
1615eab3227bSNikolay Aleksandrov 	br_group.dst.ip6 = *group;
16169cc6e0c4SLinus Lüssing 	br_group.proto = htons(ETH_P_IPV6);
1617b0e9a30dSVlad Yasevich 	br_group.vid = vid;
16188b671779SNikolay Aleksandrov 	filter_mode = mldv1 ? MCAST_EXCLUDE : MCAST_INCLUDE;
161908b202b6SYOSHIFUJI Hideaki 
1620adc47037SNikolay Aleksandrov 	return br_multicast_add_group(brmctx, pmctx, &br_group, src,
1621adc47037SNikolay Aleksandrov 				      filter_mode, mldv1);
162208b202b6SYOSHIFUJI Hideaki }
162308b202b6SYOSHIFUJI Hideaki #endif
162408b202b6SYOSHIFUJI Hideaki 
br_multicast_rport_del(struct hlist_node * rlist)1625ed2d3597SLinus Lüssing static bool br_multicast_rport_del(struct hlist_node *rlist)
1626ed2d3597SLinus Lüssing {
1627ed2d3597SLinus Lüssing 	if (hlist_unhashed(rlist))
1628ed2d3597SLinus Lüssing 		return false;
1629ed2d3597SLinus Lüssing 
1630ed2d3597SLinus Lüssing 	hlist_del_init_rcu(rlist);
1631ed2d3597SLinus Lüssing 	return true;
1632ed2d3597SLinus Lüssing }
1633ed2d3597SLinus Lüssing 
br_ip4_multicast_rport_del(struct net_bridge_mcast_port * pmctx)1634adc47037SNikolay Aleksandrov static bool br_ip4_multicast_rport_del(struct net_bridge_mcast_port *pmctx)
1635ed2d3597SLinus Lüssing {
1636adc47037SNikolay Aleksandrov 	return br_multicast_rport_del(&pmctx->ip4_rlist);
1637ed2d3597SLinus Lüssing }
1638ed2d3597SLinus Lüssing 
br_ip6_multicast_rport_del(struct net_bridge_mcast_port * pmctx)1639adc47037SNikolay Aleksandrov static bool br_ip6_multicast_rport_del(struct net_bridge_mcast_port *pmctx)
1640a3c02e76SLinus Lüssing {
1641a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
1642adc47037SNikolay Aleksandrov 	return br_multicast_rport_del(&pmctx->ip6_rlist);
1643a3c02e76SLinus Lüssing #else
1644a3c02e76SLinus Lüssing 	return false;
1645a3c02e76SLinus Lüssing #endif
1646a3c02e76SLinus Lüssing }
1647a3c02e76SLinus Lüssing 
br_multicast_router_expired(struct net_bridge_mcast_port * pmctx,struct timer_list * t,struct hlist_node * rlist)16489632233eSNikolay Aleksandrov static void br_multicast_router_expired(struct net_bridge_mcast_port *pmctx,
1649ee5fb222SLinus Lüssing 					struct timer_list *t,
1650ee5fb222SLinus Lüssing 					struct hlist_node *rlist)
1651eb1d1641SHerbert Xu {
16529632233eSNikolay Aleksandrov 	struct net_bridge *br = pmctx->port->br;
1653ed2d3597SLinus Lüssing 	bool del;
1654eb1d1641SHerbert Xu 
1655eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
16569632233eSNikolay Aleksandrov 	if (pmctx->multicast_router == MDB_RTR_TYPE_DISABLED ||
16579632233eSNikolay Aleksandrov 	    pmctx->multicast_router == MDB_RTR_TYPE_PERM ||
1658ee5fb222SLinus Lüssing 	    timer_pending(t))
1659eb1d1641SHerbert Xu 		goto out;
1660eb1d1641SHerbert Xu 
1661ed2d3597SLinus Lüssing 	del = br_multicast_rport_del(rlist);
1662adc47037SNikolay Aleksandrov 	br_multicast_rport_del_notify(pmctx, del);
1663eb1d1641SHerbert Xu out:
1664eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1665eb1d1641SHerbert Xu }
1666eb1d1641SHerbert Xu 
br_ip4_multicast_router_expired(struct timer_list * t)1667ee5fb222SLinus Lüssing static void br_ip4_multicast_router_expired(struct timer_list *t)
1668ee5fb222SLinus Lüssing {
16699632233eSNikolay Aleksandrov 	struct net_bridge_mcast_port *pmctx = from_timer(pmctx, t,
16709632233eSNikolay Aleksandrov 							 ip4_mc_router_timer);
1671ee5fb222SLinus Lüssing 
16729632233eSNikolay Aleksandrov 	br_multicast_router_expired(pmctx, t, &pmctx->ip4_rlist);
1673ee5fb222SLinus Lüssing }
1674ee5fb222SLinus Lüssing 
1675a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
br_ip6_multicast_router_expired(struct timer_list * t)1676a3c02e76SLinus Lüssing static void br_ip6_multicast_router_expired(struct timer_list *t)
1677a3c02e76SLinus Lüssing {
16789632233eSNikolay Aleksandrov 	struct net_bridge_mcast_port *pmctx = from_timer(pmctx, t,
16799632233eSNikolay Aleksandrov 							 ip6_mc_router_timer);
1680a3c02e76SLinus Lüssing 
16819632233eSNikolay Aleksandrov 	br_multicast_router_expired(pmctx, t, &pmctx->ip6_rlist);
1682a3c02e76SLinus Lüssing }
1683a3c02e76SLinus Lüssing #endif
1684a3c02e76SLinus Lüssing 
br_mc_router_state_change(struct net_bridge * p,bool is_mc_router)168577041420SYotam Gigi static void br_mc_router_state_change(struct net_bridge *p,
168677041420SYotam Gigi 				      bool is_mc_router)
168777041420SYotam Gigi {
168877041420SYotam Gigi 	struct switchdev_attr attr = {
168977041420SYotam Gigi 		.orig_dev = p->dev,
169077041420SYotam Gigi 		.id = SWITCHDEV_ATTR_ID_BRIDGE_MROUTER,
169177041420SYotam Gigi 		.flags = SWITCHDEV_F_DEFER,
169277041420SYotam Gigi 		.u.mrouter = is_mc_router,
169377041420SYotam Gigi 	};
169477041420SYotam Gigi 
1695dcbdf135SVladimir Oltean 	switchdev_port_attr_set(p->dev, &attr, NULL);
169677041420SYotam Gigi }
169777041420SYotam Gigi 
br_multicast_local_router_expired(struct net_bridge_mcast * brmctx,struct timer_list * timer)1698d3d065c0SNikolay Aleksandrov static void br_multicast_local_router_expired(struct net_bridge_mcast *brmctx,
1699ee5fb222SLinus Lüssing 					      struct timer_list *timer)
1700eb1d1641SHerbert Xu {
1701d3d065c0SNikolay Aleksandrov 	spin_lock(&brmctx->br->multicast_lock);
1702d3d065c0SNikolay Aleksandrov 	if (brmctx->multicast_router == MDB_RTR_TYPE_DISABLED ||
1703d3d065c0SNikolay Aleksandrov 	    brmctx->multicast_router == MDB_RTR_TYPE_PERM ||
1704d3d065c0SNikolay Aleksandrov 	    br_ip4_multicast_is_router(brmctx) ||
1705d3d065c0SNikolay Aleksandrov 	    br_ip6_multicast_is_router(brmctx))
170677041420SYotam Gigi 		goto out;
170777041420SYotam Gigi 
1708d3d065c0SNikolay Aleksandrov 	br_mc_router_state_change(brmctx->br, false);
170977041420SYotam Gigi out:
1710d3d065c0SNikolay Aleksandrov 	spin_unlock(&brmctx->br->multicast_lock);
1711eb1d1641SHerbert Xu }
1712eb1d1641SHerbert Xu 
br_ip4_multicast_local_router_expired(struct timer_list * t)1713ee5fb222SLinus Lüssing static void br_ip4_multicast_local_router_expired(struct timer_list *t)
1714ee5fb222SLinus Lüssing {
1715d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = from_timer(brmctx, t,
1716d3d065c0SNikolay Aleksandrov 						     ip4_mc_router_timer);
1717ee5fb222SLinus Lüssing 
1718d3d065c0SNikolay Aleksandrov 	br_multicast_local_router_expired(brmctx, t);
1719ee5fb222SLinus Lüssing }
1720ee5fb222SLinus Lüssing 
1721a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
br_ip6_multicast_local_router_expired(struct timer_list * t)1722a3c02e76SLinus Lüssing static void br_ip6_multicast_local_router_expired(struct timer_list *t)
1723a3c02e76SLinus Lüssing {
1724d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = from_timer(brmctx, t,
1725d3d065c0SNikolay Aleksandrov 						     ip6_mc_router_timer);
1726a3c02e76SLinus Lüssing 
1727d3d065c0SNikolay Aleksandrov 	br_multicast_local_router_expired(brmctx, t);
1728a3c02e76SLinus Lüssing }
1729a3c02e76SLinus Lüssing #endif
1730a3c02e76SLinus Lüssing 
br_multicast_querier_expired(struct net_bridge_mcast * brmctx,struct bridge_mcast_own_query * query)1731d3d065c0SNikolay Aleksandrov static void br_multicast_querier_expired(struct net_bridge_mcast *brmctx,
173290010b36SLinus Lüssing 					 struct bridge_mcast_own_query *query)
1733c83b8fabSHerbert Xu {
1734d3d065c0SNikolay Aleksandrov 	spin_lock(&brmctx->br->multicast_lock);
1735d3d065c0SNikolay Aleksandrov 	if (!netif_running(brmctx->br->dev) ||
17364cdd0d10SNikolay Aleksandrov 	    br_multicast_ctx_vlan_global_disabled(brmctx) ||
1737d3d065c0SNikolay Aleksandrov 	    !br_opt_get(brmctx->br, BROPT_MULTICAST_ENABLED))
1738c83b8fabSHerbert Xu 		goto out;
1739c83b8fabSHerbert Xu 
1740adc47037SNikolay Aleksandrov 	br_multicast_start_querier(brmctx, query);
1741c83b8fabSHerbert Xu 
1742c83b8fabSHerbert Xu out:
1743d3d065c0SNikolay Aleksandrov 	spin_unlock(&brmctx->br->multicast_lock);
1744c83b8fabSHerbert Xu }
1745c83b8fabSHerbert Xu 
br_ip4_multicast_querier_expired(struct timer_list * t)174688c1f37fSAllen Pais static void br_ip4_multicast_querier_expired(struct timer_list *t)
1747cc0fdd80SLinus Lüssing {
1748d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = from_timer(brmctx, t,
1749d3d065c0SNikolay Aleksandrov 						     ip4_other_query.timer);
1750cc0fdd80SLinus Lüssing 
1751d3d065c0SNikolay Aleksandrov 	br_multicast_querier_expired(brmctx, &brmctx->ip4_own_query);
1752cc0fdd80SLinus Lüssing }
1753cc0fdd80SLinus Lüssing 
1754cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
br_ip6_multicast_querier_expired(struct timer_list * t)175588c1f37fSAllen Pais static void br_ip6_multicast_querier_expired(struct timer_list *t)
1756cc0fdd80SLinus Lüssing {
1757d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = from_timer(brmctx, t,
1758d3d065c0SNikolay Aleksandrov 						     ip6_other_query.timer);
1759cc0fdd80SLinus Lüssing 
1760d3d065c0SNikolay Aleksandrov 	br_multicast_querier_expired(brmctx, &brmctx->ip6_own_query);
1761cc0fdd80SLinus Lüssing }
1762cc0fdd80SLinus Lüssing #endif
1763cc0fdd80SLinus Lüssing 
br_multicast_query_delay_expired(struct timer_list * t)1764d99971ecSLinus Lüssing static void br_multicast_query_delay_expired(struct timer_list *t)
1765d99971ecSLinus Lüssing {
1766d99971ecSLinus Lüssing }
1767d99971ecSLinus Lüssing 
br_multicast_select_own_querier(struct net_bridge_mcast * brmctx,struct br_ip * ip,struct sk_buff * skb)1768adc47037SNikolay Aleksandrov static void br_multicast_select_own_querier(struct net_bridge_mcast *brmctx,
1769dc4eb53aSLinus Lüssing 					    struct br_ip *ip,
1770dc4eb53aSLinus Lüssing 					    struct sk_buff *skb)
1771dc4eb53aSLinus Lüssing {
1772dc4eb53aSLinus Lüssing 	if (ip->proto == htons(ETH_P_IP))
1773d3d065c0SNikolay Aleksandrov 		brmctx->ip4_querier.addr.src.ip4 = ip_hdr(skb)->saddr;
1774dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
1775dc4eb53aSLinus Lüssing 	else
1776d3d065c0SNikolay Aleksandrov 		brmctx->ip6_querier.addr.src.ip6 = ipv6_hdr(skb)->saddr;
1777dc4eb53aSLinus Lüssing #endif
1778dc4eb53aSLinus Lüssing }
1779dc4eb53aSLinus Lüssing 
__br_multicast_send_query(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct net_bridge_port_group * pg,struct br_ip * ip_dst,struct br_ip * group,bool with_srcs,u8 sflag,bool * need_rexmit)1780adc47037SNikolay Aleksandrov static void __br_multicast_send_query(struct net_bridge_mcast *brmctx,
1781adc47037SNikolay Aleksandrov 				      struct net_bridge_mcast_port *pmctx,
1782438ef2d0SNikolay Aleksandrov 				      struct net_bridge_port_group *pg,
1783438ef2d0SNikolay Aleksandrov 				      struct br_ip *ip_dst,
1784438ef2d0SNikolay Aleksandrov 				      struct br_ip *group,
1785438ef2d0SNikolay Aleksandrov 				      bool with_srcs,
178642c11ccfSNikolay Aleksandrov 				      u8 sflag,
178742c11ccfSNikolay Aleksandrov 				      bool *need_rexmit)
1788eb1d1641SHerbert Xu {
1789438ef2d0SNikolay Aleksandrov 	bool over_lmqt = !!sflag;
1790eb1d1641SHerbert Xu 	struct sk_buff *skb;
17911080ab95SNikolay Aleksandrov 	u8 igmp_type;
1792eb1d1641SHerbert Xu 
1793cb486ce9SNikolay Aleksandrov 	if (!br_multicast_ctx_should_use(brmctx, pmctx) ||
1794cb486ce9SNikolay Aleksandrov 	    !br_multicast_ctx_matches_vlan_snooping(brmctx))
1795615cc23eSNikolay Aleksandrov 		return;
1796615cc23eSNikolay Aleksandrov 
1797438ef2d0SNikolay Aleksandrov again_under_lmqt:
1798615cc23eSNikolay Aleksandrov 	skb = br_multicast_alloc_query(brmctx, pmctx, pg, ip_dst, group,
1799615cc23eSNikolay Aleksandrov 				       with_srcs, over_lmqt, sflag, &igmp_type,
180042c11ccfSNikolay Aleksandrov 				       need_rexmit);
1801eb1d1641SHerbert Xu 	if (!skb)
18028ef2a9a5SYOSHIFUJI Hideaki 		return;
1803eb1d1641SHerbert Xu 
1804adc47037SNikolay Aleksandrov 	if (pmctx) {
1805adc47037SNikolay Aleksandrov 		skb->dev = pmctx->port->dev;
1806adc47037SNikolay Aleksandrov 		br_multicast_count(brmctx->br, pmctx->port, skb, igmp_type,
18071080ab95SNikolay Aleksandrov 				   BR_MCAST_DIR_TX);
180829a26a56SEric W. Biederman 		NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
1809adc47037SNikolay Aleksandrov 			dev_net(pmctx->port->dev), NULL, skb, NULL, skb->dev,
1810f0b4eeceSLinus Lüssing 			br_dev_queue_push_xmit);
1811438ef2d0SNikolay Aleksandrov 
1812438ef2d0SNikolay Aleksandrov 		if (over_lmqt && with_srcs && sflag) {
1813438ef2d0SNikolay Aleksandrov 			over_lmqt = false;
1814438ef2d0SNikolay Aleksandrov 			goto again_under_lmqt;
1815438ef2d0SNikolay Aleksandrov 		}
1816dc4eb53aSLinus Lüssing 	} else {
1817adc47037SNikolay Aleksandrov 		br_multicast_select_own_querier(brmctx, group, skb);
1818adc47037SNikolay Aleksandrov 		br_multicast_count(brmctx->br, NULL, skb, igmp_type,
18191080ab95SNikolay Aleksandrov 				   BR_MCAST_DIR_RX);
1820eb1d1641SHerbert Xu 		netif_rx(skb);
18218ef2a9a5SYOSHIFUJI Hideaki 	}
1822dc4eb53aSLinus Lüssing }
1823eb1d1641SHerbert Xu 
br_multicast_read_querier(const struct bridge_mcast_querier * querier,struct bridge_mcast_querier * dest)182467b746f9SNikolay Aleksandrov static void br_multicast_read_querier(const struct bridge_mcast_querier *querier,
182567b746f9SNikolay Aleksandrov 				      struct bridge_mcast_querier *dest)
182667b746f9SNikolay Aleksandrov {
182767b746f9SNikolay Aleksandrov 	unsigned int seq;
182867b746f9SNikolay Aleksandrov 
182967b746f9SNikolay Aleksandrov 	memset(dest, 0, sizeof(*dest));
183067b746f9SNikolay Aleksandrov 	do {
183167b746f9SNikolay Aleksandrov 		seq = read_seqcount_begin(&querier->seq);
183267b746f9SNikolay Aleksandrov 		dest->port_ifidx = querier->port_ifidx;
183367b746f9SNikolay Aleksandrov 		memcpy(&dest->addr, &querier->addr, sizeof(struct br_ip));
183467b746f9SNikolay Aleksandrov 	} while (read_seqcount_retry(&querier->seq, seq));
183567b746f9SNikolay Aleksandrov }
183667b746f9SNikolay Aleksandrov 
br_multicast_update_querier(struct net_bridge_mcast * brmctx,struct bridge_mcast_querier * querier,int ifindex,struct br_ip * saddr)183767b746f9SNikolay Aleksandrov static void br_multicast_update_querier(struct net_bridge_mcast *brmctx,
183867b746f9SNikolay Aleksandrov 					struct bridge_mcast_querier *querier,
183967b746f9SNikolay Aleksandrov 					int ifindex,
184067b746f9SNikolay Aleksandrov 					struct br_ip *saddr)
184167b746f9SNikolay Aleksandrov {
184267b746f9SNikolay Aleksandrov 	write_seqcount_begin(&querier->seq);
184367b746f9SNikolay Aleksandrov 	querier->port_ifidx = ifindex;
184467b746f9SNikolay Aleksandrov 	memcpy(&querier->addr, saddr, sizeof(*saddr));
184567b746f9SNikolay Aleksandrov 	write_seqcount_end(&querier->seq);
184667b746f9SNikolay Aleksandrov }
184767b746f9SNikolay Aleksandrov 
br_multicast_send_query(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct bridge_mcast_own_query * own_query)1848adc47037SNikolay Aleksandrov static void br_multicast_send_query(struct net_bridge_mcast *brmctx,
1849adc47037SNikolay Aleksandrov 				    struct net_bridge_mcast_port *pmctx,
185090010b36SLinus Lüssing 				    struct bridge_mcast_own_query *own_query)
18518ef2a9a5SYOSHIFUJI Hideaki {
185290010b36SLinus Lüssing 	struct bridge_mcast_other_query *other_query = NULL;
185367b746f9SNikolay Aleksandrov 	struct bridge_mcast_querier *querier;
18545e923585SNikolay Aleksandrov 	struct br_ip br_group;
18555e923585SNikolay Aleksandrov 	unsigned long time;
18568ef2a9a5SYOSHIFUJI Hideaki 
18574cdd0d10SNikolay Aleksandrov 	if (!br_multicast_ctx_should_use(brmctx, pmctx) ||
1858adc47037SNikolay Aleksandrov 	    !br_opt_get(brmctx->br, BROPT_MULTICAST_ENABLED) ||
18594d5b4e84SNikolay Aleksandrov 	    !brmctx->multicast_querier)
18608ef2a9a5SYOSHIFUJI Hideaki 		return;
18618ef2a9a5SYOSHIFUJI Hideaki 
1862eab3227bSNikolay Aleksandrov 	memset(&br_group.dst, 0, sizeof(br_group.dst));
18638ef2a9a5SYOSHIFUJI Hideaki 
1864adc47037SNikolay Aleksandrov 	if (pmctx ? (own_query == &pmctx->ip4_own_query) :
1865d3d065c0SNikolay Aleksandrov 		    (own_query == &brmctx->ip4_own_query)) {
186667b746f9SNikolay Aleksandrov 		querier = &brmctx->ip4_querier;
1867d3d065c0SNikolay Aleksandrov 		other_query = &brmctx->ip4_other_query;
186808b202b6SYOSHIFUJI Hideaki 		br_group.proto = htons(ETH_P_IP);
1869dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1870cc0fdd80SLinus Lüssing 	} else {
187167b746f9SNikolay Aleksandrov 		querier = &brmctx->ip6_querier;
1872d3d065c0SNikolay Aleksandrov 		other_query = &brmctx->ip6_other_query;
187308b202b6SYOSHIFUJI Hideaki 		br_group.proto = htons(ETH_P_IPV6);
187408b202b6SYOSHIFUJI Hideaki #endif
1875eb1d1641SHerbert Xu 	}
1876eb1d1641SHerbert Xu 
187790010b36SLinus Lüssing 	if (!other_query || timer_pending(&other_query->timer))
1878cc0fdd80SLinus Lüssing 		return;
1879cc0fdd80SLinus Lüssing 
188067b746f9SNikolay Aleksandrov 	/* we're about to select ourselves as querier */
188167b746f9SNikolay Aleksandrov 	if (!pmctx && querier->port_ifidx) {
188267b746f9SNikolay Aleksandrov 		struct br_ip zeroip = {};
188367b746f9SNikolay Aleksandrov 
188467b746f9SNikolay Aleksandrov 		br_multicast_update_querier(brmctx, querier, 0, &zeroip);
188567b746f9SNikolay Aleksandrov 	}
188667b746f9SNikolay Aleksandrov 
1887adc47037SNikolay Aleksandrov 	__br_multicast_send_query(brmctx, pmctx, NULL, NULL, &br_group, false,
1888adc47037SNikolay Aleksandrov 				  0, NULL);
1889cc0fdd80SLinus Lüssing 
1890cc0fdd80SLinus Lüssing 	time = jiffies;
1891d3d065c0SNikolay Aleksandrov 	time += own_query->startup_sent < brmctx->multicast_startup_query_count ?
1892d3d065c0SNikolay Aleksandrov 		brmctx->multicast_startup_query_interval :
1893d3d065c0SNikolay Aleksandrov 		brmctx->multicast_query_interval;
189490010b36SLinus Lüssing 	mod_timer(&own_query->timer, time);
1895cc0fdd80SLinus Lüssing }
1896cc0fdd80SLinus Lüssing 
189790010b36SLinus Lüssing static void
br_multicast_port_query_expired(struct net_bridge_mcast_port * pmctx,struct bridge_mcast_own_query * query)18989632233eSNikolay Aleksandrov br_multicast_port_query_expired(struct net_bridge_mcast_port *pmctx,
189990010b36SLinus Lüssing 				struct bridge_mcast_own_query *query)
1900eb1d1641SHerbert Xu {
19019632233eSNikolay Aleksandrov 	struct net_bridge *br = pmctx->port->br;
19024cdd0d10SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx;
1903eb1d1641SHerbert Xu 
1904eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
19054cdd0d10SNikolay Aleksandrov 	if (br_multicast_port_ctx_state_stopped(pmctx))
1906eb1d1641SHerbert Xu 		goto out;
1907615cc23eSNikolay Aleksandrov 
19084cdd0d10SNikolay Aleksandrov 	brmctx = br_multicast_port_ctx_get_global(pmctx);
19094cdd0d10SNikolay Aleksandrov 	if (query->startup_sent < brmctx->multicast_startup_query_count)
1910cc0fdd80SLinus Lüssing 		query->startup_sent++;
1911eb1d1641SHerbert Xu 
19124cdd0d10SNikolay Aleksandrov 	br_multicast_send_query(brmctx, pmctx, query);
1913eb1d1641SHerbert Xu 
1914eb1d1641SHerbert Xu out:
1915eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1916eb1d1641SHerbert Xu }
1917eb1d1641SHerbert Xu 
br_ip4_multicast_port_query_expired(struct timer_list * t)191888c1f37fSAllen Pais static void br_ip4_multicast_port_query_expired(struct timer_list *t)
1919cc0fdd80SLinus Lüssing {
19209632233eSNikolay Aleksandrov 	struct net_bridge_mcast_port *pmctx = from_timer(pmctx, t,
19219632233eSNikolay Aleksandrov 							 ip4_own_query.timer);
1922cc0fdd80SLinus Lüssing 
19239632233eSNikolay Aleksandrov 	br_multicast_port_query_expired(pmctx, &pmctx->ip4_own_query);
1924cc0fdd80SLinus Lüssing }
1925cc0fdd80SLinus Lüssing 
1926cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
br_ip6_multicast_port_query_expired(struct timer_list * t)192788c1f37fSAllen Pais static void br_ip6_multicast_port_query_expired(struct timer_list *t)
1928cc0fdd80SLinus Lüssing {
19299632233eSNikolay Aleksandrov 	struct net_bridge_mcast_port *pmctx = from_timer(pmctx, t,
19309632233eSNikolay Aleksandrov 							 ip6_own_query.timer);
1931cc0fdd80SLinus Lüssing 
19329632233eSNikolay Aleksandrov 	br_multicast_port_query_expired(pmctx, &pmctx->ip6_own_query);
1933cc0fdd80SLinus Lüssing }
1934cc0fdd80SLinus Lüssing #endif
1935cc0fdd80SLinus Lüssing 
br_multicast_port_group_rexmit(struct timer_list * t)193642c11ccfSNikolay Aleksandrov static void br_multicast_port_group_rexmit(struct timer_list *t)
193742c11ccfSNikolay Aleksandrov {
193842c11ccfSNikolay Aleksandrov 	struct net_bridge_port_group *pg = from_timer(pg, t, rexmit_timer);
193942c11ccfSNikolay Aleksandrov 	struct bridge_mcast_other_query *other_query = NULL;
1940085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
1941adc47037SNikolay Aleksandrov 	struct net_bridge_mcast_port *pmctx;
1942adc47037SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx;
194342c11ccfSNikolay Aleksandrov 	bool need_rexmit = false;
194442c11ccfSNikolay Aleksandrov 
194542c11ccfSNikolay Aleksandrov 	spin_lock(&br->multicast_lock);
194642c11ccfSNikolay Aleksandrov 	if (!netif_running(br->dev) || hlist_unhashed(&pg->mglist) ||
19474d5b4e84SNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_ENABLED))
194842c11ccfSNikolay Aleksandrov 		goto out;
194942c11ccfSNikolay Aleksandrov 
1950eb1593a0SNikolay Aleksandrov 	pmctx = br_multicast_pg_to_port_ctx(pg);
1951eb1593a0SNikolay Aleksandrov 	if (!pmctx)
1952eb1593a0SNikolay Aleksandrov 		goto out;
1953eb1593a0SNikolay Aleksandrov 	brmctx = br_multicast_port_ctx_get_global(pmctx);
19544d5b4e84SNikolay Aleksandrov 	if (!brmctx->multicast_querier)
19554d5b4e84SNikolay Aleksandrov 		goto out;
19564d5b4e84SNikolay Aleksandrov 
1957085b53c8SNikolay Aleksandrov 	if (pg->key.addr.proto == htons(ETH_P_IP))
1958d3d065c0SNikolay Aleksandrov 		other_query = &brmctx->ip4_other_query;
195942c11ccfSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
196042c11ccfSNikolay Aleksandrov 	else
1961d3d065c0SNikolay Aleksandrov 		other_query = &brmctx->ip6_other_query;
196242c11ccfSNikolay Aleksandrov #endif
196342c11ccfSNikolay Aleksandrov 
196442c11ccfSNikolay Aleksandrov 	if (!other_query || timer_pending(&other_query->timer))
196542c11ccfSNikolay Aleksandrov 		goto out;
196642c11ccfSNikolay Aleksandrov 
196742c11ccfSNikolay Aleksandrov 	if (pg->grp_query_rexmit_cnt) {
196842c11ccfSNikolay Aleksandrov 		pg->grp_query_rexmit_cnt--;
1969adc47037SNikolay Aleksandrov 		__br_multicast_send_query(brmctx, pmctx, pg, &pg->key.addr,
1970085b53c8SNikolay Aleksandrov 					  &pg->key.addr, false, 1, NULL);
197142c11ccfSNikolay Aleksandrov 	}
1972adc47037SNikolay Aleksandrov 	__br_multicast_send_query(brmctx, pmctx, pg, &pg->key.addr,
1973085b53c8SNikolay Aleksandrov 				  &pg->key.addr, true, 0, &need_rexmit);
197442c11ccfSNikolay Aleksandrov 
197542c11ccfSNikolay Aleksandrov 	if (pg->grp_query_rexmit_cnt || need_rexmit)
197642c11ccfSNikolay Aleksandrov 		mod_timer(&pg->rexmit_timer, jiffies +
1977d3d065c0SNikolay Aleksandrov 					     brmctx->multicast_last_member_interval);
197842c11ccfSNikolay Aleksandrov out:
197942c11ccfSNikolay Aleksandrov 	spin_unlock(&br->multicast_lock);
198042c11ccfSNikolay Aleksandrov }
198142c11ccfSNikolay Aleksandrov 
br_mc_disabled_update(struct net_device * dev,bool value,struct netlink_ext_ack * extack)1982ae1ea84bSFlorian Fainelli static int br_mc_disabled_update(struct net_device *dev, bool value,
1983ae1ea84bSFlorian Fainelli 				 struct netlink_ext_ack *extack)
1984147c1e9bSNogah Frankel {
1985147c1e9bSNogah Frankel 	struct switchdev_attr attr = {
1986147c1e9bSNogah Frankel 		.orig_dev = dev,
1987147c1e9bSNogah Frankel 		.id = SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED,
1988147c1e9bSNogah Frankel 		.flags = SWITCHDEV_F_DEFER,
198913cefad2SNikolay Aleksandrov 		.u.mc_disabled = !value,
1990147c1e9bSNogah Frankel 	};
1991147c1e9bSNogah Frankel 
1992ae1ea84bSFlorian Fainelli 	return switchdev_port_attr_set(dev, &attr, extack);
1993147c1e9bSNogah Frankel }
1994147c1e9bSNogah Frankel 
br_multicast_port_ctx_init(struct net_bridge_port * port,struct net_bridge_vlan * vlan,struct net_bridge_mcast_port * pmctx)1995613d61dbSNikolay Aleksandrov void br_multicast_port_ctx_init(struct net_bridge_port *port,
1996613d61dbSNikolay Aleksandrov 				struct net_bridge_vlan *vlan,
19979632233eSNikolay Aleksandrov 				struct net_bridge_mcast_port *pmctx)
19989632233eSNikolay Aleksandrov {
19999632233eSNikolay Aleksandrov 	pmctx->port = port;
2000613d61dbSNikolay Aleksandrov 	pmctx->vlan = vlan;
20019632233eSNikolay Aleksandrov 	pmctx->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
20029632233eSNikolay Aleksandrov 	timer_setup(&pmctx->ip4_mc_router_timer,
20039632233eSNikolay Aleksandrov 		    br_ip4_multicast_router_expired, 0);
20049632233eSNikolay Aleksandrov 	timer_setup(&pmctx->ip4_own_query.timer,
20059632233eSNikolay Aleksandrov 		    br_ip4_multicast_port_query_expired, 0);
20069632233eSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
20079632233eSNikolay Aleksandrov 	timer_setup(&pmctx->ip6_mc_router_timer,
20089632233eSNikolay Aleksandrov 		    br_ip6_multicast_router_expired, 0);
20099632233eSNikolay Aleksandrov 	timer_setup(&pmctx->ip6_own_query.timer,
20109632233eSNikolay Aleksandrov 		    br_ip6_multicast_port_query_expired, 0);
20119632233eSNikolay Aleksandrov #endif
20129632233eSNikolay Aleksandrov }
20139632233eSNikolay Aleksandrov 
br_multicast_port_ctx_deinit(struct net_bridge_mcast_port * pmctx)2014613d61dbSNikolay Aleksandrov void br_multicast_port_ctx_deinit(struct net_bridge_mcast_port *pmctx)
20159632233eSNikolay Aleksandrov {
20169632233eSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
20179632233eSNikolay Aleksandrov 	del_timer_sync(&pmctx->ip6_mc_router_timer);
20189632233eSNikolay Aleksandrov #endif
20199632233eSNikolay Aleksandrov 	del_timer_sync(&pmctx->ip4_mc_router_timer);
20209632233eSNikolay Aleksandrov }
20219632233eSNikolay Aleksandrov 
br_multicast_add_port(struct net_bridge_port * port)20221080ab95SNikolay Aleksandrov int br_multicast_add_port(struct net_bridge_port *port)
2023eb1d1641SHerbert Xu {
2024ae1ea84bSFlorian Fainelli 	int err;
2025ae1ea84bSFlorian Fainelli 
202689268b05SNikolay Aleksandrov 	port->multicast_eht_hosts_limit = BR_MCAST_DEFAULT_EHT_HOSTS_LIMIT;
2027613d61dbSNikolay Aleksandrov 	br_multicast_port_ctx_init(port, NULL, &port->multicast_ctx);
2028eb1d1641SHerbert Xu 
2029ae1ea84bSFlorian Fainelli 	err = br_mc_disabled_update(port->dev,
2030ae1ea84bSFlorian Fainelli 				    br_opt_get(port->br,
2031ae1ea84bSFlorian Fainelli 					       BROPT_MULTICAST_ENABLED),
2032ae1ea84bSFlorian Fainelli 				    NULL);
203368f5c12aSVladimir Oltean 	if (err && err != -EOPNOTSUPP)
2034ae1ea84bSFlorian Fainelli 		return err;
2035147c1e9bSNogah Frankel 
20361080ab95SNikolay Aleksandrov 	port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
20371080ab95SNikolay Aleksandrov 	if (!port->mcast_stats)
20381080ab95SNikolay Aleksandrov 		return -ENOMEM;
20391080ab95SNikolay Aleksandrov 
20401080ab95SNikolay Aleksandrov 	return 0;
2041eb1d1641SHerbert Xu }
2042eb1d1641SHerbert Xu 
br_multicast_del_port(struct net_bridge_port * port)2043eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port)
2044eb1d1641SHerbert Xu {
2045e10177abSSatish Ashok 	struct net_bridge *br = port->br;
2046e10177abSSatish Ashok 	struct net_bridge_port_group *pg;
2047e10177abSSatish Ashok 	struct hlist_node *n;
2048e10177abSSatish Ashok 
2049e10177abSSatish Ashok 	/* Take care of the remaining groups, only perm ones should be left */
2050e10177abSSatish Ashok 	spin_lock_bh(&br->multicast_lock);
2051e10177abSSatish Ashok 	hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
2052681590bdSNikolay Aleksandrov 		br_multicast_find_del_pg(br, pg);
2053e10177abSSatish Ashok 	spin_unlock_bh(&br->multicast_lock);
2054*e3145ca9SNikolay Aleksandrov 	flush_work(&br->mcast_gc_work);
20559632233eSNikolay Aleksandrov 	br_multicast_port_ctx_deinit(&port->multicast_ctx);
20561080ab95SNikolay Aleksandrov 	free_percpu(port->mcast_stats);
2057eb1d1641SHerbert Xu }
2058eb1d1641SHerbert Xu 
br_multicast_enable(struct bridge_mcast_own_query * query)205990010b36SLinus Lüssing static void br_multicast_enable(struct bridge_mcast_own_query *query)
2060561f1103SHerbert Xu {
2061cc0fdd80SLinus Lüssing 	query->startup_sent = 0;
2062561f1103SHerbert Xu 
2063cc0fdd80SLinus Lüssing 	if (try_to_del_timer_sync(&query->timer) >= 0 ||
2064cc0fdd80SLinus Lüssing 	    del_timer(&query->timer))
2065cc0fdd80SLinus Lüssing 		mod_timer(&query->timer, jiffies);
2066561f1103SHerbert Xu }
2067561f1103SHerbert Xu 
__br_multicast_enable_port_ctx(struct net_bridge_mcast_port * pmctx)2068adc47037SNikolay Aleksandrov static void __br_multicast_enable_port_ctx(struct net_bridge_mcast_port *pmctx)
2069eb1d1641SHerbert Xu {
2070adc47037SNikolay Aleksandrov 	struct net_bridge *br = pmctx->port->br;
20717b54aaafSNikolay Aleksandrov 	struct net_bridge_mcast *brmctx;
2072eb1d1641SHerbert Xu 
20737b54aaafSNikolay Aleksandrov 	brmctx = br_multicast_port_ctx_get_global(pmctx);
20747b54aaafSNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED) ||
20757b54aaafSNikolay Aleksandrov 	    !netif_running(br->dev))
20767cb3f921SNikolay Aleksandrov 		return;
2077eb1d1641SHerbert Xu 
2078adc47037SNikolay Aleksandrov 	br_multicast_enable(&pmctx->ip4_own_query);
2079cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
2080adc47037SNikolay Aleksandrov 	br_multicast_enable(&pmctx->ip6_own_query);
2081cc0fdd80SLinus Lüssing #endif
2082adc47037SNikolay Aleksandrov 	if (pmctx->multicast_router == MDB_RTR_TYPE_PERM) {
2083adc47037SNikolay Aleksandrov 		br_ip4_multicast_add_router(brmctx, pmctx);
2084adc47037SNikolay Aleksandrov 		br_ip6_multicast_add_router(brmctx, pmctx);
2085a3c02e76SLinus Lüssing 	}
2086b57e8d87SPetr Machata 
2087b57e8d87SPetr Machata 	if (br_multicast_port_ctx_is_vlan(pmctx)) {
2088b57e8d87SPetr Machata 		struct net_bridge_port_group *pg;
2089b57e8d87SPetr Machata 		u32 n = 0;
2090b57e8d87SPetr Machata 
2091b57e8d87SPetr Machata 		/* The mcast_n_groups counter might be wrong. First,
2092b57e8d87SPetr Machata 		 * BR_VLFLAG_MCAST_ENABLED is toggled before temporary entries
2093b57e8d87SPetr Machata 		 * are flushed, thus mcast_n_groups after the toggle does not
2094b57e8d87SPetr Machata 		 * reflect the true values. And second, permanent entries added
2095b57e8d87SPetr Machata 		 * while BR_VLFLAG_MCAST_ENABLED was disabled, are not reflected
2096b57e8d87SPetr Machata 		 * either. Thus we have to refresh the counter.
2097b57e8d87SPetr Machata 		 */
2098b57e8d87SPetr Machata 
2099b57e8d87SPetr Machata 		hlist_for_each_entry(pg, &pmctx->port->mglist, mglist) {
2100b57e8d87SPetr Machata 			if (pg->key.addr.vid == pmctx->vlan->vid)
2101b57e8d87SPetr Machata 				n++;
2102b57e8d87SPetr Machata 		}
2103b57e8d87SPetr Machata 		WRITE_ONCE(pmctx->mdb_n_entries, n);
2104b57e8d87SPetr Machata 	}
21057cb3f921SNikolay Aleksandrov }
2106eb1d1641SHerbert Xu 
br_multicast_enable_port(struct net_bridge_port * port)21077cb3f921SNikolay Aleksandrov void br_multicast_enable_port(struct net_bridge_port *port)
21087cb3f921SNikolay Aleksandrov {
21097cb3f921SNikolay Aleksandrov 	struct net_bridge *br = port->br;
21107cb3f921SNikolay Aleksandrov 
2111f4b7002aSNikolay Aleksandrov 	spin_lock_bh(&br->multicast_lock);
2112adc47037SNikolay Aleksandrov 	__br_multicast_enable_port_ctx(&port->multicast_ctx);
2113f4b7002aSNikolay Aleksandrov 	spin_unlock_bh(&br->multicast_lock);
2114eb1d1641SHerbert Xu }
2115eb1d1641SHerbert Xu 
__br_multicast_disable_port_ctx(struct net_bridge_mcast_port * pmctx)21167b54aaafSNikolay Aleksandrov static void __br_multicast_disable_port_ctx(struct net_bridge_mcast_port *pmctx)
2117eb1d1641SHerbert Xu {
2118eb1d1641SHerbert Xu 	struct net_bridge_port_group *pg;
2119b67bfe0dSSasha Levin 	struct hlist_node *n;
2120ed2d3597SLinus Lüssing 	bool del = false;
2121eb1d1641SHerbert Xu 
21227b54aaafSNikolay Aleksandrov 	hlist_for_each_entry_safe(pg, n, &pmctx->port->mglist, mglist)
21237b54aaafSNikolay Aleksandrov 		if (!(pg->flags & MDB_PG_FLAGS_PERMANENT) &&
21247b54aaafSNikolay Aleksandrov 		    (!br_multicast_port_ctx_is_vlan(pmctx) ||
21257b54aaafSNikolay Aleksandrov 		     pg->key.addr.vid == pmctx->vlan->vid))
21267b54aaafSNikolay Aleksandrov 			br_multicast_find_del_pg(pmctx->port->br, pg);
2127eb1d1641SHerbert Xu 
2128adc47037SNikolay Aleksandrov 	del |= br_ip4_multicast_rport_del(pmctx);
2129adc47037SNikolay Aleksandrov 	del_timer(&pmctx->ip4_mc_router_timer);
2130adc47037SNikolay Aleksandrov 	del_timer(&pmctx->ip4_own_query.timer);
2131adc47037SNikolay Aleksandrov 	del |= br_ip6_multicast_rport_del(pmctx);
2132cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
2133adc47037SNikolay Aleksandrov 	del_timer(&pmctx->ip6_mc_router_timer);
2134adc47037SNikolay Aleksandrov 	del_timer(&pmctx->ip6_own_query.timer);
2135cc0fdd80SLinus Lüssing #endif
2136adc47037SNikolay Aleksandrov 	br_multicast_rport_del_notify(pmctx, del);
21377b54aaafSNikolay Aleksandrov }
21387b54aaafSNikolay Aleksandrov 
br_multicast_disable_port(struct net_bridge_port * port)21397b54aaafSNikolay Aleksandrov void br_multicast_disable_port(struct net_bridge_port *port)
21407b54aaafSNikolay Aleksandrov {
2141f4b7002aSNikolay Aleksandrov 	spin_lock_bh(&port->br->multicast_lock);
21427b54aaafSNikolay Aleksandrov 	__br_multicast_disable_port_ctx(&port->multicast_ctx);
2143f4b7002aSNikolay Aleksandrov 	spin_unlock_bh(&port->br->multicast_lock);
2144eb1d1641SHerbert Xu }
2145eb1d1641SHerbert Xu 
__grp_src_delete_marked(struct net_bridge_port_group * pg)2146e6231bcaSNikolay Aleksandrov static int __grp_src_delete_marked(struct net_bridge_port_group *pg)
2147e6231bcaSNikolay Aleksandrov {
2148e6231bcaSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
2149e6231bcaSNikolay Aleksandrov 	struct hlist_node *tmp;
2150e6231bcaSNikolay Aleksandrov 	int deleted = 0;
2151e6231bcaSNikolay Aleksandrov 
2152e6231bcaSNikolay Aleksandrov 	hlist_for_each_entry_safe(ent, tmp, &pg->src_list, node)
2153e6231bcaSNikolay Aleksandrov 		if (ent->flags & BR_SGRP_F_DELETE) {
2154d5a10222SNikolay Aleksandrov 			br_multicast_del_group_src(ent, false);
2155e6231bcaSNikolay Aleksandrov 			deleted++;
2156e6231bcaSNikolay Aleksandrov 		}
2157e6231bcaSNikolay Aleksandrov 
2158e6231bcaSNikolay Aleksandrov 	return deleted;
2159e6231bcaSNikolay Aleksandrov }
2160e6231bcaSNikolay Aleksandrov 
__grp_src_mod_timer(struct net_bridge_group_src * src,unsigned long expires)2161b0812368SNikolay Aleksandrov static void __grp_src_mod_timer(struct net_bridge_group_src *src,
2162b0812368SNikolay Aleksandrov 				unsigned long expires)
2163b0812368SNikolay Aleksandrov {
2164b0812368SNikolay Aleksandrov 	mod_timer(&src->timer, expires);
2165b0812368SNikolay Aleksandrov 	br_multicast_fwd_src_handle(src);
2166b0812368SNikolay Aleksandrov }
2167b0812368SNikolay Aleksandrov 
__grp_src_query_marked_and_rexmit(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct net_bridge_port_group * pg)2168adc47037SNikolay Aleksandrov static void __grp_src_query_marked_and_rexmit(struct net_bridge_mcast *brmctx,
2169adc47037SNikolay Aleksandrov 					      struct net_bridge_mcast_port *pmctx,
2170adc47037SNikolay Aleksandrov 					      struct net_bridge_port_group *pg)
21715bf1e00bSNikolay Aleksandrov {
21725bf1e00bSNikolay Aleksandrov 	struct bridge_mcast_other_query *other_query = NULL;
2173d3d065c0SNikolay Aleksandrov 	u32 lmqc = brmctx->multicast_last_member_count;
21745bf1e00bSNikolay Aleksandrov 	unsigned long lmqt, lmi, now = jiffies;
21755bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
21765bf1e00bSNikolay Aleksandrov 
2177adc47037SNikolay Aleksandrov 	if (!netif_running(brmctx->br->dev) ||
2178adc47037SNikolay Aleksandrov 	    !br_opt_get(brmctx->br, BROPT_MULTICAST_ENABLED))
21795bf1e00bSNikolay Aleksandrov 		return;
21805bf1e00bSNikolay Aleksandrov 
2181085b53c8SNikolay Aleksandrov 	if (pg->key.addr.proto == htons(ETH_P_IP))
2182d3d065c0SNikolay Aleksandrov 		other_query = &brmctx->ip4_other_query;
21835bf1e00bSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
21845bf1e00bSNikolay Aleksandrov 	else
2185d3d065c0SNikolay Aleksandrov 		other_query = &brmctx->ip6_other_query;
21865bf1e00bSNikolay Aleksandrov #endif
21875bf1e00bSNikolay Aleksandrov 
2188adc47037SNikolay Aleksandrov 	lmqt = now + br_multicast_lmqt(brmctx);
21895bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node) {
21905bf1e00bSNikolay Aleksandrov 		if (ent->flags & BR_SGRP_F_SEND) {
21915bf1e00bSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_SEND;
21925bf1e00bSNikolay Aleksandrov 			if (ent->timer.expires > lmqt) {
21934d5b4e84SNikolay Aleksandrov 				if (brmctx->multicast_querier &&
21945bf1e00bSNikolay Aleksandrov 				    other_query &&
21955bf1e00bSNikolay Aleksandrov 				    !timer_pending(&other_query->timer))
21965bf1e00bSNikolay Aleksandrov 					ent->src_query_rexmit_cnt = lmqc;
2197b0812368SNikolay Aleksandrov 				__grp_src_mod_timer(ent, lmqt);
21985bf1e00bSNikolay Aleksandrov 			}
21995bf1e00bSNikolay Aleksandrov 		}
22005bf1e00bSNikolay Aleksandrov 	}
22015bf1e00bSNikolay Aleksandrov 
22024d5b4e84SNikolay Aleksandrov 	if (!brmctx->multicast_querier ||
22035bf1e00bSNikolay Aleksandrov 	    !other_query || timer_pending(&other_query->timer))
22045bf1e00bSNikolay Aleksandrov 		return;
22055bf1e00bSNikolay Aleksandrov 
2206adc47037SNikolay Aleksandrov 	__br_multicast_send_query(brmctx, pmctx, pg, &pg->key.addr,
2207085b53c8SNikolay Aleksandrov 				  &pg->key.addr, true, 1, NULL);
22085bf1e00bSNikolay Aleksandrov 
2209d3d065c0SNikolay Aleksandrov 	lmi = now + brmctx->multicast_last_member_interval;
22105bf1e00bSNikolay Aleksandrov 	if (!timer_pending(&pg->rexmit_timer) ||
22115bf1e00bSNikolay Aleksandrov 	    time_after(pg->rexmit_timer.expires, lmi))
22125bf1e00bSNikolay Aleksandrov 		mod_timer(&pg->rexmit_timer, lmi);
22135bf1e00bSNikolay Aleksandrov }
22145bf1e00bSNikolay Aleksandrov 
__grp_send_query_and_rexmit(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct net_bridge_port_group * pg)2215adc47037SNikolay Aleksandrov static void __grp_send_query_and_rexmit(struct net_bridge_mcast *brmctx,
2216adc47037SNikolay Aleksandrov 					struct net_bridge_mcast_port *pmctx,
2217adc47037SNikolay Aleksandrov 					struct net_bridge_port_group *pg)
22185bf1e00bSNikolay Aleksandrov {
22195bf1e00bSNikolay Aleksandrov 	struct bridge_mcast_other_query *other_query = NULL;
22205bf1e00bSNikolay Aleksandrov 	unsigned long now = jiffies, lmi;
22215bf1e00bSNikolay Aleksandrov 
2222adc47037SNikolay Aleksandrov 	if (!netif_running(brmctx->br->dev) ||
2223adc47037SNikolay Aleksandrov 	    !br_opt_get(brmctx->br, BROPT_MULTICAST_ENABLED))
22245bf1e00bSNikolay Aleksandrov 		return;
22255bf1e00bSNikolay Aleksandrov 
2226085b53c8SNikolay Aleksandrov 	if (pg->key.addr.proto == htons(ETH_P_IP))
2227d3d065c0SNikolay Aleksandrov 		other_query = &brmctx->ip4_other_query;
22285bf1e00bSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
22295bf1e00bSNikolay Aleksandrov 	else
2230d3d065c0SNikolay Aleksandrov 		other_query = &brmctx->ip6_other_query;
22315bf1e00bSNikolay Aleksandrov #endif
22325bf1e00bSNikolay Aleksandrov 
22334d5b4e84SNikolay Aleksandrov 	if (brmctx->multicast_querier &&
22345bf1e00bSNikolay Aleksandrov 	    other_query && !timer_pending(&other_query->timer)) {
2235d3d065c0SNikolay Aleksandrov 		lmi = now + brmctx->multicast_last_member_interval;
2236d3d065c0SNikolay Aleksandrov 		pg->grp_query_rexmit_cnt = brmctx->multicast_last_member_count - 1;
2237adc47037SNikolay Aleksandrov 		__br_multicast_send_query(brmctx, pmctx, pg, &pg->key.addr,
2238085b53c8SNikolay Aleksandrov 					  &pg->key.addr, false, 0, NULL);
22395bf1e00bSNikolay Aleksandrov 		if (!timer_pending(&pg->rexmit_timer) ||
22405bf1e00bSNikolay Aleksandrov 		    time_after(pg->rexmit_timer.expires, lmi))
22415bf1e00bSNikolay Aleksandrov 			mod_timer(&pg->rexmit_timer, lmi);
22425bf1e00bSNikolay Aleksandrov 	}
22435bf1e00bSNikolay Aleksandrov 
22445bf1e00bSNikolay Aleksandrov 	if (pg->filter_mode == MCAST_EXCLUDE &&
22455bf1e00bSNikolay Aleksandrov 	    (!timer_pending(&pg->timer) ||
2246adc47037SNikolay Aleksandrov 	     time_after(pg->timer.expires, now + br_multicast_lmqt(brmctx))))
2247adc47037SNikolay Aleksandrov 		mod_timer(&pg->timer, now + br_multicast_lmqt(brmctx));
22485bf1e00bSNikolay Aleksandrov }
22495bf1e00bSNikolay Aleksandrov 
22500436862eSNikolay Aleksandrov /* State          Msg type      New state                Actions
22510436862eSNikolay Aleksandrov  * INCLUDE (A)    IS_IN (B)     INCLUDE (A+B)            (B)=GMI
22520436862eSNikolay Aleksandrov  * INCLUDE (A)    ALLOW (B)     INCLUDE (A+B)            (B)=GMI
22530436862eSNikolay Aleksandrov  * EXCLUDE (X,Y)  ALLOW (A)     EXCLUDE (X+A,Y-A)        (A)=GMI
22540436862eSNikolay Aleksandrov  */
br_multicast_isinc_allow(const struct net_bridge_mcast * brmctx,struct net_bridge_port_group * pg,void * h_addr,void * srcs,u32 nsrcs,size_t addr_size,int grec_type)2255adc47037SNikolay Aleksandrov static bool br_multicast_isinc_allow(const struct net_bridge_mcast *brmctx,
2256adc47037SNikolay Aleksandrov 				     struct net_bridge_port_group *pg, void *h_addr,
2257474ddb37SNikolay Aleksandrov 				     void *srcs, u32 nsrcs, size_t addr_size,
2258474ddb37SNikolay Aleksandrov 				     int grec_type)
22590436862eSNikolay Aleksandrov {
22600436862eSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
22610436862eSNikolay Aleksandrov 	unsigned long now = jiffies;
22620436862eSNikolay Aleksandrov 	bool changed = false;
22630436862eSNikolay Aleksandrov 	struct br_ip src_ip;
22640436862eSNikolay Aleksandrov 	u32 src_idx;
22650436862eSNikolay Aleksandrov 
22660436862eSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2267085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
22680436862eSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2269e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
22700436862eSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
22710436862eSNikolay Aleksandrov 		if (!ent) {
22720436862eSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
22730436862eSNikolay Aleksandrov 			if (ent)
22740436862eSNikolay Aleksandrov 				changed = true;
22750436862eSNikolay Aleksandrov 		}
22760436862eSNikolay Aleksandrov 
22770436862eSNikolay Aleksandrov 		if (ent)
2278adc47037SNikolay Aleksandrov 			__grp_src_mod_timer(ent, now + br_multicast_gmi(brmctx));
22790436862eSNikolay Aleksandrov 	}
22800436862eSNikolay Aleksandrov 
2281adc47037SNikolay Aleksandrov 	if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2282adc47037SNikolay Aleksandrov 				    grec_type))
2283474ddb37SNikolay Aleksandrov 		changed = true;
2284474ddb37SNikolay Aleksandrov 
22850436862eSNikolay Aleksandrov 	return changed;
22860436862eSNikolay Aleksandrov }
22870436862eSNikolay Aleksandrov 
2288e6231bcaSNikolay Aleksandrov /* State          Msg type      New state                Actions
2289e6231bcaSNikolay Aleksandrov  * INCLUDE (A)    IS_EX (B)     EXCLUDE (A*B,B-A)        (B-A)=0
2290e6231bcaSNikolay Aleksandrov  *                                                       Delete (A-B)
2291e6231bcaSNikolay Aleksandrov  *                                                       Group Timer=GMI
2292e6231bcaSNikolay Aleksandrov  */
__grp_src_isexc_incl(const struct net_bridge_mcast * brmctx,struct net_bridge_port_group * pg,void * h_addr,void * srcs,u32 nsrcs,size_t addr_size,int grec_type)2293adc47037SNikolay Aleksandrov static void __grp_src_isexc_incl(const struct net_bridge_mcast *brmctx,
2294adc47037SNikolay Aleksandrov 				 struct net_bridge_port_group *pg, void *h_addr,
2295ddc255d9SNikolay Aleksandrov 				 void *srcs, u32 nsrcs, size_t addr_size,
2296ddc255d9SNikolay Aleksandrov 				 int grec_type)
2297e6231bcaSNikolay Aleksandrov {
2298e6231bcaSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
2299e6231bcaSNikolay Aleksandrov 	struct br_ip src_ip;
2300e6231bcaSNikolay Aleksandrov 	u32 src_idx;
2301e6231bcaSNikolay Aleksandrov 
2302e6231bcaSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
2303e6231bcaSNikolay Aleksandrov 		ent->flags |= BR_SGRP_F_DELETE;
2304e6231bcaSNikolay Aleksandrov 
2305e6231bcaSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2306085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
2307e6231bcaSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2308e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2309e6231bcaSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
2310e6231bcaSNikolay Aleksandrov 		if (ent)
2311e6231bcaSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_DELETE;
2312e6231bcaSNikolay Aleksandrov 		else
2313b0812368SNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
2314b0812368SNikolay Aleksandrov 		if (ent)
2315b0812368SNikolay Aleksandrov 			br_multicast_fwd_src_handle(ent);
2316e6231bcaSNikolay Aleksandrov 	}
2317e6231bcaSNikolay Aleksandrov 
2318adc47037SNikolay Aleksandrov 	br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2319adc47037SNikolay Aleksandrov 				grec_type);
2320ddc255d9SNikolay Aleksandrov 
2321e6231bcaSNikolay Aleksandrov 	__grp_src_delete_marked(pg);
2322e6231bcaSNikolay Aleksandrov }
2323e6231bcaSNikolay Aleksandrov 
2324e6231bcaSNikolay Aleksandrov /* State          Msg type      New state                Actions
2325e6231bcaSNikolay Aleksandrov  * EXCLUDE (X,Y)  IS_EX (A)     EXCLUDE (A-Y,Y*A)        (A-X-Y)=GMI
2326e6231bcaSNikolay Aleksandrov  *                                                       Delete (X-A)
2327e6231bcaSNikolay Aleksandrov  *                                                       Delete (Y-A)
2328e6231bcaSNikolay Aleksandrov  *                                                       Group Timer=GMI
2329e6231bcaSNikolay Aleksandrov  */
__grp_src_isexc_excl(const struct net_bridge_mcast * brmctx,struct net_bridge_port_group * pg,void * h_addr,void * srcs,u32 nsrcs,size_t addr_size,int grec_type)2330adc47037SNikolay Aleksandrov static bool __grp_src_isexc_excl(const struct net_bridge_mcast *brmctx,
2331adc47037SNikolay Aleksandrov 				 struct net_bridge_port_group *pg, void *h_addr,
2332ddc255d9SNikolay Aleksandrov 				 void *srcs, u32 nsrcs, size_t addr_size,
2333ddc255d9SNikolay Aleksandrov 				 int grec_type)
2334e6231bcaSNikolay Aleksandrov {
2335e6231bcaSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
2336e6231bcaSNikolay Aleksandrov 	unsigned long now = jiffies;
2337e6231bcaSNikolay Aleksandrov 	bool changed = false;
2338e6231bcaSNikolay Aleksandrov 	struct br_ip src_ip;
2339e6231bcaSNikolay Aleksandrov 	u32 src_idx;
2340e6231bcaSNikolay Aleksandrov 
2341e6231bcaSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
2342e6231bcaSNikolay Aleksandrov 		ent->flags |= BR_SGRP_F_DELETE;
2343e6231bcaSNikolay Aleksandrov 
2344e6231bcaSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2345085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
2346e6231bcaSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2347e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2348e6231bcaSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
2349e6231bcaSNikolay Aleksandrov 		if (ent) {
2350e6231bcaSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_DELETE;
2351e6231bcaSNikolay Aleksandrov 		} else {
2352e6231bcaSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
2353e6231bcaSNikolay Aleksandrov 			if (ent) {
2354b0812368SNikolay Aleksandrov 				__grp_src_mod_timer(ent,
2355adc47037SNikolay Aleksandrov 						    now + br_multicast_gmi(brmctx));
2356e6231bcaSNikolay Aleksandrov 				changed = true;
2357e6231bcaSNikolay Aleksandrov 			}
2358e6231bcaSNikolay Aleksandrov 		}
2359e6231bcaSNikolay Aleksandrov 	}
2360e6231bcaSNikolay Aleksandrov 
2361adc47037SNikolay Aleksandrov 	if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2362adc47037SNikolay Aleksandrov 				    grec_type))
2363ddc255d9SNikolay Aleksandrov 		changed = true;
2364ddc255d9SNikolay Aleksandrov 
2365e6231bcaSNikolay Aleksandrov 	if (__grp_src_delete_marked(pg))
2366e6231bcaSNikolay Aleksandrov 		changed = true;
2367e6231bcaSNikolay Aleksandrov 
2368e6231bcaSNikolay Aleksandrov 	return changed;
2369e6231bcaSNikolay Aleksandrov }
2370e6231bcaSNikolay Aleksandrov 
br_multicast_isexc(const struct net_bridge_mcast * brmctx,struct net_bridge_port_group * pg,void * h_addr,void * srcs,u32 nsrcs,size_t addr_size,int grec_type)2371adc47037SNikolay Aleksandrov static bool br_multicast_isexc(const struct net_bridge_mcast *brmctx,
2372adc47037SNikolay Aleksandrov 			       struct net_bridge_port_group *pg, void *h_addr,
2373ddc255d9SNikolay Aleksandrov 			       void *srcs, u32 nsrcs, size_t addr_size,
2374ddc255d9SNikolay Aleksandrov 			       int grec_type)
2375e6231bcaSNikolay Aleksandrov {
2376e6231bcaSNikolay Aleksandrov 	bool changed = false;
2377e6231bcaSNikolay Aleksandrov 
2378e6231bcaSNikolay Aleksandrov 	switch (pg->filter_mode) {
2379e6231bcaSNikolay Aleksandrov 	case MCAST_INCLUDE:
2380adc47037SNikolay Aleksandrov 		__grp_src_isexc_incl(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2381ddc255d9SNikolay Aleksandrov 				     grec_type);
23828266a049SNikolay Aleksandrov 		br_multicast_star_g_handle_mode(pg, MCAST_EXCLUDE);
2383e6231bcaSNikolay Aleksandrov 		changed = true;
2384e6231bcaSNikolay Aleksandrov 		break;
2385e6231bcaSNikolay Aleksandrov 	case MCAST_EXCLUDE:
2386adc47037SNikolay Aleksandrov 		changed = __grp_src_isexc_excl(brmctx, pg, h_addr, srcs, nsrcs,
2387adc47037SNikolay Aleksandrov 					       addr_size, grec_type);
2388e6231bcaSNikolay Aleksandrov 		break;
2389e6231bcaSNikolay Aleksandrov 	}
2390e6231bcaSNikolay Aleksandrov 
2391e6231bcaSNikolay Aleksandrov 	pg->filter_mode = MCAST_EXCLUDE;
2392adc47037SNikolay Aleksandrov 	mod_timer(&pg->timer, jiffies + br_multicast_gmi(brmctx));
2393e6231bcaSNikolay Aleksandrov 
2394e6231bcaSNikolay Aleksandrov 	return changed;
2395e6231bcaSNikolay Aleksandrov }
2396e6231bcaSNikolay Aleksandrov 
23975bf1e00bSNikolay Aleksandrov /* State          Msg type      New state                Actions
23985bf1e00bSNikolay Aleksandrov  * INCLUDE (A)    TO_IN (B)     INCLUDE (A+B)            (B)=GMI
23995bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G,A-B)
24005bf1e00bSNikolay Aleksandrov  */
__grp_src_toin_incl(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct net_bridge_port_group * pg,void * h_addr,void * srcs,u32 nsrcs,size_t addr_size,int grec_type)2401adc47037SNikolay Aleksandrov static bool __grp_src_toin_incl(struct net_bridge_mcast *brmctx,
2402adc47037SNikolay Aleksandrov 				struct net_bridge_mcast_port *pmctx,
2403adc47037SNikolay Aleksandrov 				struct net_bridge_port_group *pg, void *h_addr,
2404ddc255d9SNikolay Aleksandrov 				void *srcs, u32 nsrcs, size_t addr_size,
2405ddc255d9SNikolay Aleksandrov 				int grec_type)
24065bf1e00bSNikolay Aleksandrov {
24075bf1e00bSNikolay Aleksandrov 	u32 src_idx, to_send = pg->src_ents;
24085bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
24095bf1e00bSNikolay Aleksandrov 	unsigned long now = jiffies;
24105bf1e00bSNikolay Aleksandrov 	bool changed = false;
24115bf1e00bSNikolay Aleksandrov 	struct br_ip src_ip;
24125bf1e00bSNikolay Aleksandrov 
24135bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
24145bf1e00bSNikolay Aleksandrov 		ent->flags |= BR_SGRP_F_SEND;
24155bf1e00bSNikolay Aleksandrov 
24165bf1e00bSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2417085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
24185bf1e00bSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2419e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
24205bf1e00bSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
24215bf1e00bSNikolay Aleksandrov 		if (ent) {
24225bf1e00bSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_SEND;
24235bf1e00bSNikolay Aleksandrov 			to_send--;
24245bf1e00bSNikolay Aleksandrov 		} else {
24255bf1e00bSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
24265bf1e00bSNikolay Aleksandrov 			if (ent)
24275bf1e00bSNikolay Aleksandrov 				changed = true;
24285bf1e00bSNikolay Aleksandrov 		}
24295bf1e00bSNikolay Aleksandrov 		if (ent)
2430adc47037SNikolay Aleksandrov 			__grp_src_mod_timer(ent, now + br_multicast_gmi(brmctx));
24315bf1e00bSNikolay Aleksandrov 	}
24325bf1e00bSNikolay Aleksandrov 
2433adc47037SNikolay Aleksandrov 	if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2434adc47037SNikolay Aleksandrov 				    grec_type))
2435ddc255d9SNikolay Aleksandrov 		changed = true;
2436ddc255d9SNikolay Aleksandrov 
24375bf1e00bSNikolay Aleksandrov 	if (to_send)
2438adc47037SNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(brmctx, pmctx, pg);
24395bf1e00bSNikolay Aleksandrov 
24405bf1e00bSNikolay Aleksandrov 	return changed;
24415bf1e00bSNikolay Aleksandrov }
24425bf1e00bSNikolay Aleksandrov 
24435bf1e00bSNikolay Aleksandrov /* State          Msg type      New state                Actions
24445bf1e00bSNikolay Aleksandrov  * EXCLUDE (X,Y)  TO_IN (A)     EXCLUDE (X+A,Y-A)        (A)=GMI
24455bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G,X-A)
24465bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G)
24475bf1e00bSNikolay Aleksandrov  */
__grp_src_toin_excl(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct net_bridge_port_group * pg,void * h_addr,void * srcs,u32 nsrcs,size_t addr_size,int grec_type)2448adc47037SNikolay Aleksandrov static bool __grp_src_toin_excl(struct net_bridge_mcast *brmctx,
2449adc47037SNikolay Aleksandrov 				struct net_bridge_mcast_port *pmctx,
2450adc47037SNikolay Aleksandrov 				struct net_bridge_port_group *pg, void *h_addr,
2451ddc255d9SNikolay Aleksandrov 				void *srcs, u32 nsrcs, size_t addr_size,
2452ddc255d9SNikolay Aleksandrov 				int grec_type)
24535bf1e00bSNikolay Aleksandrov {
24545bf1e00bSNikolay Aleksandrov 	u32 src_idx, to_send = pg->src_ents;
24555bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
24565bf1e00bSNikolay Aleksandrov 	unsigned long now = jiffies;
24575bf1e00bSNikolay Aleksandrov 	bool changed = false;
24585bf1e00bSNikolay Aleksandrov 	struct br_ip src_ip;
24595bf1e00bSNikolay Aleksandrov 
24605bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
24615bf1e00bSNikolay Aleksandrov 		if (timer_pending(&ent->timer))
24625bf1e00bSNikolay Aleksandrov 			ent->flags |= BR_SGRP_F_SEND;
24635bf1e00bSNikolay Aleksandrov 
24645bf1e00bSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2465085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
24665bf1e00bSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2467e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
24685bf1e00bSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
24695bf1e00bSNikolay Aleksandrov 		if (ent) {
24705bf1e00bSNikolay Aleksandrov 			if (timer_pending(&ent->timer)) {
24715bf1e00bSNikolay Aleksandrov 				ent->flags &= ~BR_SGRP_F_SEND;
24725bf1e00bSNikolay Aleksandrov 				to_send--;
24735bf1e00bSNikolay Aleksandrov 			}
24745bf1e00bSNikolay Aleksandrov 		} else {
24755bf1e00bSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
24765bf1e00bSNikolay Aleksandrov 			if (ent)
24775bf1e00bSNikolay Aleksandrov 				changed = true;
24785bf1e00bSNikolay Aleksandrov 		}
24795bf1e00bSNikolay Aleksandrov 		if (ent)
2480adc47037SNikolay Aleksandrov 			__grp_src_mod_timer(ent, now + br_multicast_gmi(brmctx));
24815bf1e00bSNikolay Aleksandrov 	}
24825bf1e00bSNikolay Aleksandrov 
2483adc47037SNikolay Aleksandrov 	if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2484adc47037SNikolay Aleksandrov 				    grec_type))
2485ddc255d9SNikolay Aleksandrov 		changed = true;
2486ddc255d9SNikolay Aleksandrov 
24875bf1e00bSNikolay Aleksandrov 	if (to_send)
2488adc47037SNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(brmctx, pmctx, pg);
24895bf1e00bSNikolay Aleksandrov 
2490adc47037SNikolay Aleksandrov 	__grp_send_query_and_rexmit(brmctx, pmctx, pg);
24915bf1e00bSNikolay Aleksandrov 
24925bf1e00bSNikolay Aleksandrov 	return changed;
24935bf1e00bSNikolay Aleksandrov }
24945bf1e00bSNikolay Aleksandrov 
br_multicast_toin(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct net_bridge_port_group * pg,void * h_addr,void * srcs,u32 nsrcs,size_t addr_size,int grec_type)2495adc47037SNikolay Aleksandrov static bool br_multicast_toin(struct net_bridge_mcast *brmctx,
2496adc47037SNikolay Aleksandrov 			      struct net_bridge_mcast_port *pmctx,
2497adc47037SNikolay Aleksandrov 			      struct net_bridge_port_group *pg, void *h_addr,
2498ddc255d9SNikolay Aleksandrov 			      void *srcs, u32 nsrcs, size_t addr_size,
2499ddc255d9SNikolay Aleksandrov 			      int grec_type)
25005bf1e00bSNikolay Aleksandrov {
25015bf1e00bSNikolay Aleksandrov 	bool changed = false;
25025bf1e00bSNikolay Aleksandrov 
25035bf1e00bSNikolay Aleksandrov 	switch (pg->filter_mode) {
25045bf1e00bSNikolay Aleksandrov 	case MCAST_INCLUDE:
2505adc47037SNikolay Aleksandrov 		changed = __grp_src_toin_incl(brmctx, pmctx, pg, h_addr, srcs,
2506adc47037SNikolay Aleksandrov 					      nsrcs, addr_size, grec_type);
25075bf1e00bSNikolay Aleksandrov 		break;
25085bf1e00bSNikolay Aleksandrov 	case MCAST_EXCLUDE:
2509adc47037SNikolay Aleksandrov 		changed = __grp_src_toin_excl(brmctx, pmctx, pg, h_addr, srcs,
2510adc47037SNikolay Aleksandrov 					      nsrcs, addr_size, grec_type);
25115bf1e00bSNikolay Aleksandrov 		break;
25125bf1e00bSNikolay Aleksandrov 	}
25135bf1e00bSNikolay Aleksandrov 
2514ddc255d9SNikolay Aleksandrov 	if (br_multicast_eht_should_del_pg(pg)) {
2515d5a10222SNikolay Aleksandrov 		pg->flags |= MDB_PG_FLAGS_FAST_LEAVE;
2516ddc255d9SNikolay Aleksandrov 		br_multicast_find_del_pg(pg->key.port->br, pg);
2517ddc255d9SNikolay Aleksandrov 		/* a notification has already been sent and we shouldn't
2518ddc255d9SNikolay Aleksandrov 		 * access pg after the delete so we have to return false
2519ddc255d9SNikolay Aleksandrov 		 */
2520ddc255d9SNikolay Aleksandrov 		changed = false;
2521ddc255d9SNikolay Aleksandrov 	}
2522ddc255d9SNikolay Aleksandrov 
25235bf1e00bSNikolay Aleksandrov 	return changed;
25245bf1e00bSNikolay Aleksandrov }
25255bf1e00bSNikolay Aleksandrov 
25265bf1e00bSNikolay Aleksandrov /* State          Msg type      New state                Actions
25275bf1e00bSNikolay Aleksandrov  * INCLUDE (A)    TO_EX (B)     EXCLUDE (A*B,B-A)        (B-A)=0
25285bf1e00bSNikolay Aleksandrov  *                                                       Delete (A-B)
25295bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G,A*B)
25305bf1e00bSNikolay Aleksandrov  *                                                       Group Timer=GMI
25315bf1e00bSNikolay Aleksandrov  */
__grp_src_toex_incl(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct net_bridge_port_group * pg,void * h_addr,void * srcs,u32 nsrcs,size_t addr_size,int grec_type)2532adc47037SNikolay Aleksandrov static void __grp_src_toex_incl(struct net_bridge_mcast *brmctx,
2533adc47037SNikolay Aleksandrov 				struct net_bridge_mcast_port *pmctx,
2534adc47037SNikolay Aleksandrov 				struct net_bridge_port_group *pg, void *h_addr,
2535ddc255d9SNikolay Aleksandrov 				void *srcs, u32 nsrcs, size_t addr_size,
2536ddc255d9SNikolay Aleksandrov 				int grec_type)
25375bf1e00bSNikolay Aleksandrov {
25385bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
25395bf1e00bSNikolay Aleksandrov 	u32 src_idx, to_send = 0;
25405bf1e00bSNikolay Aleksandrov 	struct br_ip src_ip;
25415bf1e00bSNikolay Aleksandrov 
25425bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
25435bf1e00bSNikolay Aleksandrov 		ent->flags = (ent->flags & ~BR_SGRP_F_SEND) | BR_SGRP_F_DELETE;
25445bf1e00bSNikolay Aleksandrov 
25455bf1e00bSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2546085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
25475bf1e00bSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2548e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
25495bf1e00bSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
25505bf1e00bSNikolay Aleksandrov 		if (ent) {
25515bf1e00bSNikolay Aleksandrov 			ent->flags = (ent->flags & ~BR_SGRP_F_DELETE) |
25525bf1e00bSNikolay Aleksandrov 				     BR_SGRP_F_SEND;
25535bf1e00bSNikolay Aleksandrov 			to_send++;
25545bf1e00bSNikolay Aleksandrov 		} else {
2555b0812368SNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
25565bf1e00bSNikolay Aleksandrov 		}
2557b0812368SNikolay Aleksandrov 		if (ent)
2558b0812368SNikolay Aleksandrov 			br_multicast_fwd_src_handle(ent);
25595bf1e00bSNikolay Aleksandrov 	}
25605bf1e00bSNikolay Aleksandrov 
2561adc47037SNikolay Aleksandrov 	br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2562adc47037SNikolay Aleksandrov 				grec_type);
2563ddc255d9SNikolay Aleksandrov 
25645bf1e00bSNikolay Aleksandrov 	__grp_src_delete_marked(pg);
25655bf1e00bSNikolay Aleksandrov 	if (to_send)
2566adc47037SNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(brmctx, pmctx, pg);
25675bf1e00bSNikolay Aleksandrov }
25685bf1e00bSNikolay Aleksandrov 
25695bf1e00bSNikolay Aleksandrov /* State          Msg type      New state                Actions
25705bf1e00bSNikolay Aleksandrov  * EXCLUDE (X,Y)  TO_EX (A)     EXCLUDE (A-Y,Y*A)        (A-X-Y)=Group Timer
25715bf1e00bSNikolay Aleksandrov  *                                                       Delete (X-A)
25725bf1e00bSNikolay Aleksandrov  *                                                       Delete (Y-A)
25735bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G,A-Y)
25745bf1e00bSNikolay Aleksandrov  *                                                       Group Timer=GMI
25755bf1e00bSNikolay Aleksandrov  */
__grp_src_toex_excl(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct net_bridge_port_group * pg,void * h_addr,void * srcs,u32 nsrcs,size_t addr_size,int grec_type)2576adc47037SNikolay Aleksandrov static bool __grp_src_toex_excl(struct net_bridge_mcast *brmctx,
2577adc47037SNikolay Aleksandrov 				struct net_bridge_mcast_port *pmctx,
2578adc47037SNikolay Aleksandrov 				struct net_bridge_port_group *pg, void *h_addr,
2579ddc255d9SNikolay Aleksandrov 				void *srcs, u32 nsrcs, size_t addr_size,
2580ddc255d9SNikolay Aleksandrov 				int grec_type)
25815bf1e00bSNikolay Aleksandrov {
25825bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
25835bf1e00bSNikolay Aleksandrov 	u32 src_idx, to_send = 0;
25845bf1e00bSNikolay Aleksandrov 	bool changed = false;
25855bf1e00bSNikolay Aleksandrov 	struct br_ip src_ip;
25865bf1e00bSNikolay Aleksandrov 
25875bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
25885bf1e00bSNikolay Aleksandrov 		ent->flags = (ent->flags & ~BR_SGRP_F_SEND) | BR_SGRP_F_DELETE;
25895bf1e00bSNikolay Aleksandrov 
25905bf1e00bSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2591085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
25925bf1e00bSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2593e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
25945bf1e00bSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
25955bf1e00bSNikolay Aleksandrov 		if (ent) {
25965bf1e00bSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_DELETE;
25975bf1e00bSNikolay Aleksandrov 		} else {
25985bf1e00bSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
25995bf1e00bSNikolay Aleksandrov 			if (ent) {
2600b0812368SNikolay Aleksandrov 				__grp_src_mod_timer(ent, pg->timer.expires);
26015bf1e00bSNikolay Aleksandrov 				changed = true;
26025bf1e00bSNikolay Aleksandrov 			}
26035bf1e00bSNikolay Aleksandrov 		}
26045bf1e00bSNikolay Aleksandrov 		if (ent && timer_pending(&ent->timer)) {
26055bf1e00bSNikolay Aleksandrov 			ent->flags |= BR_SGRP_F_SEND;
26065bf1e00bSNikolay Aleksandrov 			to_send++;
26075bf1e00bSNikolay Aleksandrov 		}
26085bf1e00bSNikolay Aleksandrov 	}
26095bf1e00bSNikolay Aleksandrov 
2610adc47037SNikolay Aleksandrov 	if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2611adc47037SNikolay Aleksandrov 				    grec_type))
2612ddc255d9SNikolay Aleksandrov 		changed = true;
2613ddc255d9SNikolay Aleksandrov 
26145bf1e00bSNikolay Aleksandrov 	if (__grp_src_delete_marked(pg))
26155bf1e00bSNikolay Aleksandrov 		changed = true;
26165bf1e00bSNikolay Aleksandrov 	if (to_send)
2617adc47037SNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(brmctx, pmctx, pg);
26185bf1e00bSNikolay Aleksandrov 
26195bf1e00bSNikolay Aleksandrov 	return changed;
26205bf1e00bSNikolay Aleksandrov }
26215bf1e00bSNikolay Aleksandrov 
br_multicast_toex(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct net_bridge_port_group * pg,void * h_addr,void * srcs,u32 nsrcs,size_t addr_size,int grec_type)2622adc47037SNikolay Aleksandrov static bool br_multicast_toex(struct net_bridge_mcast *brmctx,
2623adc47037SNikolay Aleksandrov 			      struct net_bridge_mcast_port *pmctx,
2624adc47037SNikolay Aleksandrov 			      struct net_bridge_port_group *pg, void *h_addr,
2625ddc255d9SNikolay Aleksandrov 			      void *srcs, u32 nsrcs, size_t addr_size,
2626ddc255d9SNikolay Aleksandrov 			      int grec_type)
26275bf1e00bSNikolay Aleksandrov {
26285bf1e00bSNikolay Aleksandrov 	bool changed = false;
26295bf1e00bSNikolay Aleksandrov 
26305bf1e00bSNikolay Aleksandrov 	switch (pg->filter_mode) {
26315bf1e00bSNikolay Aleksandrov 	case MCAST_INCLUDE:
2632adc47037SNikolay Aleksandrov 		__grp_src_toex_incl(brmctx, pmctx, pg, h_addr, srcs, nsrcs,
2633adc47037SNikolay Aleksandrov 				    addr_size, grec_type);
26348266a049SNikolay Aleksandrov 		br_multicast_star_g_handle_mode(pg, MCAST_EXCLUDE);
26355bf1e00bSNikolay Aleksandrov 		changed = true;
26365bf1e00bSNikolay Aleksandrov 		break;
26375bf1e00bSNikolay Aleksandrov 	case MCAST_EXCLUDE:
2638adc47037SNikolay Aleksandrov 		changed = __grp_src_toex_excl(brmctx, pmctx, pg, h_addr, srcs,
2639adc47037SNikolay Aleksandrov 					      nsrcs, addr_size, grec_type);
26405bf1e00bSNikolay Aleksandrov 		break;
26415bf1e00bSNikolay Aleksandrov 	}
26425bf1e00bSNikolay Aleksandrov 
26435bf1e00bSNikolay Aleksandrov 	pg->filter_mode = MCAST_EXCLUDE;
2644adc47037SNikolay Aleksandrov 	mod_timer(&pg->timer, jiffies + br_multicast_gmi(brmctx));
26455bf1e00bSNikolay Aleksandrov 
26465bf1e00bSNikolay Aleksandrov 	return changed;
26475bf1e00bSNikolay Aleksandrov }
26485bf1e00bSNikolay Aleksandrov 
2649109865feSNikolay Aleksandrov /* State          Msg type      New state                Actions
2650109865feSNikolay Aleksandrov  * INCLUDE (A)    BLOCK (B)     INCLUDE (A)              Send Q(G,A*B)
2651109865feSNikolay Aleksandrov  */
__grp_src_block_incl(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct net_bridge_port_group * pg,void * h_addr,void * srcs,u32 nsrcs,size_t addr_size,int grec_type)2652adc47037SNikolay Aleksandrov static bool __grp_src_block_incl(struct net_bridge_mcast *brmctx,
2653adc47037SNikolay Aleksandrov 				 struct net_bridge_mcast_port *pmctx,
2654adc47037SNikolay Aleksandrov 				 struct net_bridge_port_group *pg, void *h_addr,
2655474ddb37SNikolay Aleksandrov 				 void *srcs, u32 nsrcs, size_t addr_size, int grec_type)
2656109865feSNikolay Aleksandrov {
2657109865feSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
2658109865feSNikolay Aleksandrov 	u32 src_idx, to_send = 0;
26590ad57c99SNikolay Aleksandrov 	bool changed = false;
2660109865feSNikolay Aleksandrov 	struct br_ip src_ip;
2661109865feSNikolay Aleksandrov 
2662109865feSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
2663109865feSNikolay Aleksandrov 		ent->flags &= ~BR_SGRP_F_SEND;
2664109865feSNikolay Aleksandrov 
2665109865feSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2666085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
2667109865feSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2668e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2669109865feSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
2670109865feSNikolay Aleksandrov 		if (ent) {
2671109865feSNikolay Aleksandrov 			ent->flags |= BR_SGRP_F_SEND;
2672109865feSNikolay Aleksandrov 			to_send++;
2673109865feSNikolay Aleksandrov 		}
2674109865feSNikolay Aleksandrov 	}
2675109865feSNikolay Aleksandrov 
2676adc47037SNikolay Aleksandrov 	if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2677adc47037SNikolay Aleksandrov 				    grec_type))
2678474ddb37SNikolay Aleksandrov 		changed = true;
2679474ddb37SNikolay Aleksandrov 
2680109865feSNikolay Aleksandrov 	if (to_send)
2681adc47037SNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(brmctx, pmctx, pg);
2682109865feSNikolay Aleksandrov 
26830ad57c99SNikolay Aleksandrov 	return changed;
2684109865feSNikolay Aleksandrov }
2685109865feSNikolay Aleksandrov 
2686109865feSNikolay Aleksandrov /* State          Msg type      New state                Actions
2687109865feSNikolay Aleksandrov  * EXCLUDE (X,Y)  BLOCK (A)     EXCLUDE (X+(A-Y),Y)      (A-X-Y)=Group Timer
2688109865feSNikolay Aleksandrov  *                                                       Send Q(G,A-Y)
2689109865feSNikolay Aleksandrov  */
__grp_src_block_excl(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct net_bridge_port_group * pg,void * h_addr,void * srcs,u32 nsrcs,size_t addr_size,int grec_type)2690adc47037SNikolay Aleksandrov static bool __grp_src_block_excl(struct net_bridge_mcast *brmctx,
2691adc47037SNikolay Aleksandrov 				 struct net_bridge_mcast_port *pmctx,
2692adc47037SNikolay Aleksandrov 				 struct net_bridge_port_group *pg, void *h_addr,
2693474ddb37SNikolay Aleksandrov 				 void *srcs, u32 nsrcs, size_t addr_size, int grec_type)
2694109865feSNikolay Aleksandrov {
2695109865feSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
2696109865feSNikolay Aleksandrov 	u32 src_idx, to_send = 0;
2697109865feSNikolay Aleksandrov 	bool changed = false;
2698109865feSNikolay Aleksandrov 	struct br_ip src_ip;
2699109865feSNikolay Aleksandrov 
2700109865feSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
2701109865feSNikolay Aleksandrov 		ent->flags &= ~BR_SGRP_F_SEND;
2702109865feSNikolay Aleksandrov 
2703109865feSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2704085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
2705109865feSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2706e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2707109865feSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
2708109865feSNikolay Aleksandrov 		if (!ent) {
2709109865feSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
2710109865feSNikolay Aleksandrov 			if (ent) {
2711b0812368SNikolay Aleksandrov 				__grp_src_mod_timer(ent, pg->timer.expires);
2712109865feSNikolay Aleksandrov 				changed = true;
2713109865feSNikolay Aleksandrov 			}
2714109865feSNikolay Aleksandrov 		}
2715109865feSNikolay Aleksandrov 		if (ent && timer_pending(&ent->timer)) {
2716109865feSNikolay Aleksandrov 			ent->flags |= BR_SGRP_F_SEND;
2717109865feSNikolay Aleksandrov 			to_send++;
2718109865feSNikolay Aleksandrov 		}
2719109865feSNikolay Aleksandrov 	}
2720109865feSNikolay Aleksandrov 
2721adc47037SNikolay Aleksandrov 	if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size,
2722adc47037SNikolay Aleksandrov 				    grec_type))
2723474ddb37SNikolay Aleksandrov 		changed = true;
2724474ddb37SNikolay Aleksandrov 
2725109865feSNikolay Aleksandrov 	if (to_send)
2726adc47037SNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(brmctx, pmctx, pg);
2727109865feSNikolay Aleksandrov 
2728109865feSNikolay Aleksandrov 	return changed;
2729109865feSNikolay Aleksandrov }
2730109865feSNikolay Aleksandrov 
br_multicast_block(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct net_bridge_port_group * pg,void * h_addr,void * srcs,u32 nsrcs,size_t addr_size,int grec_type)2731adc47037SNikolay Aleksandrov static bool br_multicast_block(struct net_bridge_mcast *brmctx,
2732adc47037SNikolay Aleksandrov 			       struct net_bridge_mcast_port *pmctx,
2733adc47037SNikolay Aleksandrov 			       struct net_bridge_port_group *pg, void *h_addr,
2734474ddb37SNikolay Aleksandrov 			       void *srcs, u32 nsrcs, size_t addr_size, int grec_type)
2735109865feSNikolay Aleksandrov {
2736109865feSNikolay Aleksandrov 	bool changed = false;
2737109865feSNikolay Aleksandrov 
2738109865feSNikolay Aleksandrov 	switch (pg->filter_mode) {
2739109865feSNikolay Aleksandrov 	case MCAST_INCLUDE:
2740adc47037SNikolay Aleksandrov 		changed = __grp_src_block_incl(brmctx, pmctx, pg, h_addr, srcs,
2741adc47037SNikolay Aleksandrov 					       nsrcs, addr_size, grec_type);
2742109865feSNikolay Aleksandrov 		break;
2743109865feSNikolay Aleksandrov 	case MCAST_EXCLUDE:
2744adc47037SNikolay Aleksandrov 		changed = __grp_src_block_excl(brmctx, pmctx, pg, h_addr, srcs,
2745adc47037SNikolay Aleksandrov 					       nsrcs, addr_size, grec_type);
2746109865feSNikolay Aleksandrov 		break;
2747109865feSNikolay Aleksandrov 	}
2748109865feSNikolay Aleksandrov 
2749e87e4b5cSNikolay Aleksandrov 	if ((pg->filter_mode == MCAST_INCLUDE && hlist_empty(&pg->src_list)) ||
2750e87e4b5cSNikolay Aleksandrov 	    br_multicast_eht_should_del_pg(pg)) {
2751d5a10222SNikolay Aleksandrov 		if (br_multicast_eht_should_del_pg(pg))
2752d5a10222SNikolay Aleksandrov 			pg->flags |= MDB_PG_FLAGS_FAST_LEAVE;
2753e87e4b5cSNikolay Aleksandrov 		br_multicast_find_del_pg(pg->key.port->br, pg);
2754e87e4b5cSNikolay Aleksandrov 		/* a notification has already been sent and we shouldn't
2755e87e4b5cSNikolay Aleksandrov 		 * access pg after the delete so we have to return false
2756e87e4b5cSNikolay Aleksandrov 		 */
2757e87e4b5cSNikolay Aleksandrov 		changed = false;
2758e87e4b5cSNikolay Aleksandrov 	}
2759e87e4b5cSNikolay Aleksandrov 
2760109865feSNikolay Aleksandrov 	return changed;
2761109865feSNikolay Aleksandrov }
2762109865feSNikolay Aleksandrov 
27630436862eSNikolay Aleksandrov static struct net_bridge_port_group *
br_multicast_find_port(struct net_bridge_mdb_entry * mp,struct net_bridge_port * p,const unsigned char * src)27640436862eSNikolay Aleksandrov br_multicast_find_port(struct net_bridge_mdb_entry *mp,
27650436862eSNikolay Aleksandrov 		       struct net_bridge_port *p,
27660436862eSNikolay Aleksandrov 		       const unsigned char *src)
27670436862eSNikolay Aleksandrov {
2768071445c6SNikolay Aleksandrov 	struct net_bridge *br __maybe_unused = mp->br;
27690436862eSNikolay Aleksandrov 	struct net_bridge_port_group *pg;
27700436862eSNikolay Aleksandrov 
27710436862eSNikolay Aleksandrov 	for (pg = mlock_dereference(mp->ports, br);
27720436862eSNikolay Aleksandrov 	     pg;
27730436862eSNikolay Aleksandrov 	     pg = mlock_dereference(pg->next, br))
27740436862eSNikolay Aleksandrov 		if (br_port_group_equal(pg, p, src))
27750436862eSNikolay Aleksandrov 			return pg;
27760436862eSNikolay Aleksandrov 
27770436862eSNikolay Aleksandrov 	return NULL;
27780436862eSNikolay Aleksandrov }
27790436862eSNikolay Aleksandrov 
br_ip4_multicast_igmp3_report(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct sk_buff * skb,u16 vid)2780adc47037SNikolay Aleksandrov static int br_ip4_multicast_igmp3_report(struct net_bridge_mcast *brmctx,
2781adc47037SNikolay Aleksandrov 					 struct net_bridge_mcast_port *pmctx,
278206499098SVlad Yasevich 					 struct sk_buff *skb,
278306499098SVlad Yasevich 					 u16 vid)
2784eb1d1641SHerbert Xu {
2785adc47037SNikolay Aleksandrov 	bool igmpv2 = brmctx->multicast_igmp_version == 2;
27860436862eSNikolay Aleksandrov 	struct net_bridge_mdb_entry *mdst;
27870436862eSNikolay Aleksandrov 	struct net_bridge_port_group *pg;
27886db6f0eaSFelix Fietkau 	const unsigned char *src;
2789eb1d1641SHerbert Xu 	struct igmpv3_report *ih;
2790eb1d1641SHerbert Xu 	struct igmpv3_grec *grec;
27910436862eSNikolay Aleksandrov 	int i, len, num, type;
279254bea721SNikolay Aleksandrov 	__be32 group, *h_addr;
27930436862eSNikolay Aleksandrov 	bool changed = false;
27940436862eSNikolay Aleksandrov 	int err = 0;
2795e57f6185SNikolay Aleksandrov 	u16 nsrcs;
2796eb1d1641SHerbert Xu 
2797eb1d1641SHerbert Xu 	ih = igmpv3_report_hdr(skb);
2798eb1d1641SHerbert Xu 	num = ntohs(ih->ngrec);
2799c2d4fbd2SLinus Lüssing 	len = skb_transport_offset(skb) + sizeof(*ih);
2800eb1d1641SHerbert Xu 
2801eb1d1641SHerbert Xu 	for (i = 0; i < num; i++) {
2802eb1d1641SHerbert Xu 		len += sizeof(*grec);
2803ba5ea614SLinus Lüssing 		if (!ip_mc_may_pull(skb, len))
2804eb1d1641SHerbert Xu 			return -EINVAL;
2805eb1d1641SHerbert Xu 
2806fd218cf9SHerbert Xu 		grec = (void *)(skb->data + len - sizeof(*grec));
2807eb1d1641SHerbert Xu 		group = grec->grec_mca;
2808eb1d1641SHerbert Xu 		type = grec->grec_type;
2809e57f6185SNikolay Aleksandrov 		nsrcs = ntohs(grec->grec_nsrcs);
2810eb1d1641SHerbert Xu 
2811e57f6185SNikolay Aleksandrov 		len += nsrcs * 4;
2812ba5ea614SLinus Lüssing 		if (!ip_mc_may_pull(skb, len))
2813eb1d1641SHerbert Xu 			return -EINVAL;
2814eb1d1641SHerbert Xu 
2815eb1d1641SHerbert Xu 		switch (type) {
2816eb1d1641SHerbert Xu 		case IGMPV3_MODE_IS_INCLUDE:
2817eb1d1641SHerbert Xu 		case IGMPV3_MODE_IS_EXCLUDE:
2818eb1d1641SHerbert Xu 		case IGMPV3_CHANGE_TO_INCLUDE:
2819eb1d1641SHerbert Xu 		case IGMPV3_CHANGE_TO_EXCLUDE:
2820eb1d1641SHerbert Xu 		case IGMPV3_ALLOW_NEW_SOURCES:
2821eb1d1641SHerbert Xu 		case IGMPV3_BLOCK_OLD_SOURCES:
2822eb1d1641SHerbert Xu 			break;
2823eb1d1641SHerbert Xu 
2824eb1d1641SHerbert Xu 		default:
2825eb1d1641SHerbert Xu 			continue;
2826eb1d1641SHerbert Xu 		}
2827eb1d1641SHerbert Xu 
28286db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
28290436862eSNikolay Aleksandrov 		if (nsrcs == 0 &&
28300436862eSNikolay Aleksandrov 		    (type == IGMPV3_CHANGE_TO_INCLUDE ||
28310436862eSNikolay Aleksandrov 		     type == IGMPV3_MODE_IS_INCLUDE)) {
2832adc47037SNikolay Aleksandrov 			if (!pmctx || igmpv2) {
2833adc47037SNikolay Aleksandrov 				br_ip4_multicast_leave_group(brmctx, pmctx,
2834adc47037SNikolay Aleksandrov 							     group, vid, src);
28350436862eSNikolay Aleksandrov 				continue;
28360436862eSNikolay Aleksandrov 			}
2837bc8c20acSSatish Ashok 		} else {
2838adc47037SNikolay Aleksandrov 			err = br_ip4_multicast_add_group(brmctx, pmctx, group,
2839adc47037SNikolay Aleksandrov 							 vid, src, igmpv2);
2840eb1d1641SHerbert Xu 			if (err)
2841eb1d1641SHerbert Xu 				break;
2842eb1d1641SHerbert Xu 		}
28430436862eSNikolay Aleksandrov 
2844adc47037SNikolay Aleksandrov 		if (!pmctx || igmpv2)
28450436862eSNikolay Aleksandrov 			continue;
28460436862eSNikolay Aleksandrov 
2847262985faSIdo Schimmel 		spin_lock(&brmctx->br->multicast_lock);
28484cdd0d10SNikolay Aleksandrov 		if (!br_multicast_ctx_should_use(brmctx, pmctx))
28494cdd0d10SNikolay Aleksandrov 			goto unlock_continue;
28504cdd0d10SNikolay Aleksandrov 
2851adc47037SNikolay Aleksandrov 		mdst = br_mdb_ip4_get(brmctx->br, group, vid);
28520436862eSNikolay Aleksandrov 		if (!mdst)
28530436862eSNikolay Aleksandrov 			goto unlock_continue;
2854adc47037SNikolay Aleksandrov 		pg = br_multicast_find_port(mdst, pmctx->port, src);
28550436862eSNikolay Aleksandrov 		if (!pg || (pg->flags & MDB_PG_FLAGS_PERMANENT))
28560436862eSNikolay Aleksandrov 			goto unlock_continue;
285754bea721SNikolay Aleksandrov 		/* reload grec and host addr */
28580436862eSNikolay Aleksandrov 		grec = (void *)(skb->data + len - sizeof(*grec) - (nsrcs * 4));
285954bea721SNikolay Aleksandrov 		h_addr = &ip_hdr(skb)->saddr;
28600436862eSNikolay Aleksandrov 		switch (type) {
28610436862eSNikolay Aleksandrov 		case IGMPV3_ALLOW_NEW_SOURCES:
2862adc47037SNikolay Aleksandrov 			changed = br_multicast_isinc_allow(brmctx, pg, h_addr,
2863adc47037SNikolay Aleksandrov 							   grec->grec_src,
2864474ddb37SNikolay Aleksandrov 							   nsrcs, sizeof(__be32), type);
28650436862eSNikolay Aleksandrov 			break;
2866e6231bcaSNikolay Aleksandrov 		case IGMPV3_MODE_IS_INCLUDE:
2867adc47037SNikolay Aleksandrov 			changed = br_multicast_isinc_allow(brmctx, pg, h_addr,
2868adc47037SNikolay Aleksandrov 							   grec->grec_src,
2869474ddb37SNikolay Aleksandrov 							   nsrcs, sizeof(__be32), type);
2870e6231bcaSNikolay Aleksandrov 			break;
2871e6231bcaSNikolay Aleksandrov 		case IGMPV3_MODE_IS_EXCLUDE:
2872adc47037SNikolay Aleksandrov 			changed = br_multicast_isexc(brmctx, pg, h_addr,
2873adc47037SNikolay Aleksandrov 						     grec->grec_src,
2874ddc255d9SNikolay Aleksandrov 						     nsrcs, sizeof(__be32), type);
2875e6231bcaSNikolay Aleksandrov 			break;
28765bf1e00bSNikolay Aleksandrov 		case IGMPV3_CHANGE_TO_INCLUDE:
2877adc47037SNikolay Aleksandrov 			changed = br_multicast_toin(brmctx, pmctx, pg, h_addr,
2878adc47037SNikolay Aleksandrov 						    grec->grec_src,
2879ddc255d9SNikolay Aleksandrov 						    nsrcs, sizeof(__be32), type);
28805bf1e00bSNikolay Aleksandrov 			break;
28815bf1e00bSNikolay Aleksandrov 		case IGMPV3_CHANGE_TO_EXCLUDE:
2882adc47037SNikolay Aleksandrov 			changed = br_multicast_toex(brmctx, pmctx, pg, h_addr,
2883adc47037SNikolay Aleksandrov 						    grec->grec_src,
2884ddc255d9SNikolay Aleksandrov 						    nsrcs, sizeof(__be32), type);
28855bf1e00bSNikolay Aleksandrov 			break;
2886109865feSNikolay Aleksandrov 		case IGMPV3_BLOCK_OLD_SOURCES:
2887adc47037SNikolay Aleksandrov 			changed = br_multicast_block(brmctx, pmctx, pg, h_addr,
2888adc47037SNikolay Aleksandrov 						     grec->grec_src,
2889474ddb37SNikolay Aleksandrov 						     nsrcs, sizeof(__be32), type);
2890109865feSNikolay Aleksandrov 			break;
28910436862eSNikolay Aleksandrov 		}
28920436862eSNikolay Aleksandrov 		if (changed)
2893adc47037SNikolay Aleksandrov 			br_mdb_notify(brmctx->br->dev, mdst, pg, RTM_NEWMDB);
28940436862eSNikolay Aleksandrov unlock_continue:
2895262985faSIdo Schimmel 		spin_unlock(&brmctx->br->multicast_lock);
2896bc8c20acSSatish Ashok 	}
2897eb1d1641SHerbert Xu 
2898eb1d1641SHerbert Xu 	return err;
2899eb1d1641SHerbert Xu }
2900eb1d1641SHerbert Xu 
2901dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
br_ip6_multicast_mld2_report(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct sk_buff * skb,u16 vid)2902adc47037SNikolay Aleksandrov static int br_ip6_multicast_mld2_report(struct net_bridge_mcast *brmctx,
2903adc47037SNikolay Aleksandrov 					struct net_bridge_mcast_port *pmctx,
290406499098SVlad Yasevich 					struct sk_buff *skb,
290506499098SVlad Yasevich 					u16 vid)
290608b202b6SYOSHIFUJI Hideaki {
2907adc47037SNikolay Aleksandrov 	bool mldv1 = brmctx->multicast_mld_version == 1;
29080436862eSNikolay Aleksandrov 	struct net_bridge_mdb_entry *mdst;
29090436862eSNikolay Aleksandrov 	struct net_bridge_port_group *pg;
2910ba5ea614SLinus Lüssing 	unsigned int nsrcs_offset;
29116baeb395SMichelleJin 	struct mld2_report *mld2r;
29126db6f0eaSFelix Fietkau 	const unsigned char *src;
291354bea721SNikolay Aleksandrov 	struct in6_addr *h_addr;
291408b202b6SYOSHIFUJI Hideaki 	struct mld2_grec *grec;
2915ba5ea614SLinus Lüssing 	unsigned int grec_len;
29160436862eSNikolay Aleksandrov 	bool changed = false;
29170436862eSNikolay Aleksandrov 	int i, len, num;
291808b202b6SYOSHIFUJI Hideaki 	int err = 0;
291908b202b6SYOSHIFUJI Hideaki 
29206baeb395SMichelleJin 	if (!ipv6_mc_may_pull(skb, sizeof(*mld2r)))
292108b202b6SYOSHIFUJI Hideaki 		return -EINVAL;
292208b202b6SYOSHIFUJI Hideaki 
29236baeb395SMichelleJin 	mld2r = (struct mld2_report *)icmp6_hdr(skb);
29246baeb395SMichelleJin 	num = ntohs(mld2r->mld2r_ngrec);
29256baeb395SMichelleJin 	len = skb_transport_offset(skb) + sizeof(*mld2r);
292608b202b6SYOSHIFUJI Hideaki 
292708b202b6SYOSHIFUJI Hideaki 	for (i = 0; i < num; i++) {
2928e57f6185SNikolay Aleksandrov 		__be16 *_nsrcs, __nsrcs;
2929e57f6185SNikolay Aleksandrov 		u16 nsrcs;
293008b202b6SYOSHIFUJI Hideaki 
2931ba5ea614SLinus Lüssing 		nsrcs_offset = len + offsetof(struct mld2_grec, grec_nsrcs);
2932ba5ea614SLinus Lüssing 
2933ba5ea614SLinus Lüssing 		if (skb_transport_offset(skb) + ipv6_transport_len(skb) <
29345fc6266aSLinus Lüssing 		    nsrcs_offset + sizeof(__nsrcs))
2935ba5ea614SLinus Lüssing 			return -EINVAL;
2936ba5ea614SLinus Lüssing 
2937e57f6185SNikolay Aleksandrov 		_nsrcs = skb_header_pointer(skb, nsrcs_offset,
2938e57f6185SNikolay Aleksandrov 					    sizeof(__nsrcs), &__nsrcs);
2939e57f6185SNikolay Aleksandrov 		if (!_nsrcs)
294008b202b6SYOSHIFUJI Hideaki 			return -EINVAL;
294108b202b6SYOSHIFUJI Hideaki 
2942e57f6185SNikolay Aleksandrov 		nsrcs = ntohs(*_nsrcs);
2943e57f6185SNikolay Aleksandrov 		grec_len = struct_size(grec, grec_src, nsrcs);
2944ba5ea614SLinus Lüssing 
2945ba5ea614SLinus Lüssing 		if (!ipv6_mc_may_pull(skb, len + grec_len))
294608b202b6SYOSHIFUJI Hideaki 			return -EINVAL;
294708b202b6SYOSHIFUJI Hideaki 
294808b202b6SYOSHIFUJI Hideaki 		grec = (struct mld2_grec *)(skb->data + len);
2949ba5ea614SLinus Lüssing 		len += grec_len;
295008b202b6SYOSHIFUJI Hideaki 
295108b202b6SYOSHIFUJI Hideaki 		switch (grec->grec_type) {
295208b202b6SYOSHIFUJI Hideaki 		case MLD2_MODE_IS_INCLUDE:
295308b202b6SYOSHIFUJI Hideaki 		case MLD2_MODE_IS_EXCLUDE:
295408b202b6SYOSHIFUJI Hideaki 		case MLD2_CHANGE_TO_INCLUDE:
295508b202b6SYOSHIFUJI Hideaki 		case MLD2_CHANGE_TO_EXCLUDE:
295608b202b6SYOSHIFUJI Hideaki 		case MLD2_ALLOW_NEW_SOURCES:
295708b202b6SYOSHIFUJI Hideaki 		case MLD2_BLOCK_OLD_SOURCES:
295808b202b6SYOSHIFUJI Hideaki 			break;
295908b202b6SYOSHIFUJI Hideaki 
296008b202b6SYOSHIFUJI Hideaki 		default:
296108b202b6SYOSHIFUJI Hideaki 			continue;
296208b202b6SYOSHIFUJI Hideaki 		}
296308b202b6SYOSHIFUJI Hideaki 
29646db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
2965bc8c20acSSatish Ashok 		if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE ||
2966bc8c20acSSatish Ashok 		     grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
2967e57f6185SNikolay Aleksandrov 		    nsrcs == 0) {
2968adc47037SNikolay Aleksandrov 			if (!pmctx || mldv1) {
2969adc47037SNikolay Aleksandrov 				br_ip6_multicast_leave_group(brmctx, pmctx,
29700436862eSNikolay Aleksandrov 							     &grec->grec_mca,
29716db6f0eaSFelix Fietkau 							     vid, src);
29720436862eSNikolay Aleksandrov 				continue;
29730436862eSNikolay Aleksandrov 			}
2974bc8c20acSSatish Ashok 		} else {
2975adc47037SNikolay Aleksandrov 			err = br_ip6_multicast_add_group(brmctx, pmctx,
29766db6f0eaSFelix Fietkau 							 &grec->grec_mca, vid,
29770436862eSNikolay Aleksandrov 							 src, mldv1);
29789264251eSDavide Caratti 			if (err)
297908b202b6SYOSHIFUJI Hideaki 				break;
298008b202b6SYOSHIFUJI Hideaki 		}
29810436862eSNikolay Aleksandrov 
2982adc47037SNikolay Aleksandrov 		if (!pmctx || mldv1)
29830436862eSNikolay Aleksandrov 			continue;
29840436862eSNikolay Aleksandrov 
2985262985faSIdo Schimmel 		spin_lock(&brmctx->br->multicast_lock);
29864cdd0d10SNikolay Aleksandrov 		if (!br_multicast_ctx_should_use(brmctx, pmctx))
29874cdd0d10SNikolay Aleksandrov 			goto unlock_continue;
29884cdd0d10SNikolay Aleksandrov 
2989adc47037SNikolay Aleksandrov 		mdst = br_mdb_ip6_get(brmctx->br, &grec->grec_mca, vid);
29900436862eSNikolay Aleksandrov 		if (!mdst)
29910436862eSNikolay Aleksandrov 			goto unlock_continue;
2992adc47037SNikolay Aleksandrov 		pg = br_multicast_find_port(mdst, pmctx->port, src);
29930436862eSNikolay Aleksandrov 		if (!pg || (pg->flags & MDB_PG_FLAGS_PERMANENT))
29940436862eSNikolay Aleksandrov 			goto unlock_continue;
299554bea721SNikolay Aleksandrov 		h_addr = &ipv6_hdr(skb)->saddr;
29960436862eSNikolay Aleksandrov 		switch (grec->grec_type) {
29970436862eSNikolay Aleksandrov 		case MLD2_ALLOW_NEW_SOURCES:
2998adc47037SNikolay Aleksandrov 			changed = br_multicast_isinc_allow(brmctx, pg, h_addr,
299954bea721SNikolay Aleksandrov 							   grec->grec_src, nsrcs,
3000474ddb37SNikolay Aleksandrov 							   sizeof(struct in6_addr),
3001474ddb37SNikolay Aleksandrov 							   grec->grec_type);
30020436862eSNikolay Aleksandrov 			break;
3003e6231bcaSNikolay Aleksandrov 		case MLD2_MODE_IS_INCLUDE:
3004adc47037SNikolay Aleksandrov 			changed = br_multicast_isinc_allow(brmctx, pg, h_addr,
300554bea721SNikolay Aleksandrov 							   grec->grec_src, nsrcs,
3006474ddb37SNikolay Aleksandrov 							   sizeof(struct in6_addr),
3007474ddb37SNikolay Aleksandrov 							   grec->grec_type);
3008e6231bcaSNikolay Aleksandrov 			break;
3009e6231bcaSNikolay Aleksandrov 		case MLD2_MODE_IS_EXCLUDE:
3010adc47037SNikolay Aleksandrov 			changed = br_multicast_isexc(brmctx, pg, h_addr,
301154bea721SNikolay Aleksandrov 						     grec->grec_src, nsrcs,
3012ddc255d9SNikolay Aleksandrov 						     sizeof(struct in6_addr),
3013ddc255d9SNikolay Aleksandrov 						     grec->grec_type);
3014e6231bcaSNikolay Aleksandrov 			break;
30155bf1e00bSNikolay Aleksandrov 		case MLD2_CHANGE_TO_INCLUDE:
3016adc47037SNikolay Aleksandrov 			changed = br_multicast_toin(brmctx, pmctx, pg, h_addr,
301754bea721SNikolay Aleksandrov 						    grec->grec_src, nsrcs,
3018ddc255d9SNikolay Aleksandrov 						    sizeof(struct in6_addr),
3019ddc255d9SNikolay Aleksandrov 						    grec->grec_type);
30205bf1e00bSNikolay Aleksandrov 			break;
30215bf1e00bSNikolay Aleksandrov 		case MLD2_CHANGE_TO_EXCLUDE:
3022adc47037SNikolay Aleksandrov 			changed = br_multicast_toex(brmctx, pmctx, pg, h_addr,
302354bea721SNikolay Aleksandrov 						    grec->grec_src, nsrcs,
3024ddc255d9SNikolay Aleksandrov 						    sizeof(struct in6_addr),
3025ddc255d9SNikolay Aleksandrov 						    grec->grec_type);
30265bf1e00bSNikolay Aleksandrov 			break;
3027109865feSNikolay Aleksandrov 		case MLD2_BLOCK_OLD_SOURCES:
3028adc47037SNikolay Aleksandrov 			changed = br_multicast_block(brmctx, pmctx, pg, h_addr,
302954bea721SNikolay Aleksandrov 						     grec->grec_src, nsrcs,
3030474ddb37SNikolay Aleksandrov 						     sizeof(struct in6_addr),
3031474ddb37SNikolay Aleksandrov 						     grec->grec_type);
3032109865feSNikolay Aleksandrov 			break;
30330436862eSNikolay Aleksandrov 		}
30340436862eSNikolay Aleksandrov 		if (changed)
3035adc47037SNikolay Aleksandrov 			br_mdb_notify(brmctx->br->dev, mdst, pg, RTM_NEWMDB);
30360436862eSNikolay Aleksandrov unlock_continue:
3037262985faSIdo Schimmel 		spin_unlock(&brmctx->br->multicast_lock);
3038bc8c20acSSatish Ashok 	}
303908b202b6SYOSHIFUJI Hideaki 
304008b202b6SYOSHIFUJI Hideaki 	return err;
304108b202b6SYOSHIFUJI Hideaki }
304208b202b6SYOSHIFUJI Hideaki #endif
304308b202b6SYOSHIFUJI Hideaki 
br_multicast_select_querier(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct br_ip * saddr)3044c3fb3698SNikolay Aleksandrov static bool br_multicast_select_querier(struct net_bridge_mcast *brmctx,
304554cb4319SNikolay Aleksandrov 					struct net_bridge_mcast_port *pmctx,
304667b746f9SNikolay Aleksandrov 					struct br_ip *saddr)
3047dc4eb53aSLinus Lüssing {
304867b746f9SNikolay Aleksandrov 	int port_ifidx = pmctx ? pmctx->port->dev->ifindex : 0;
3049c3fb3698SNikolay Aleksandrov 	struct timer_list *own_timer, *other_timer;
3050c3fb3698SNikolay Aleksandrov 	struct bridge_mcast_querier *querier;
305154cb4319SNikolay Aleksandrov 
3052c3fb3698SNikolay Aleksandrov 	switch (saddr->proto) {
3053c3fb3698SNikolay Aleksandrov 	case htons(ETH_P_IP):
3054c3fb3698SNikolay Aleksandrov 		querier = &brmctx->ip4_querier;
3055c3fb3698SNikolay Aleksandrov 		own_timer = &brmctx->ip4_own_query.timer;
3056c3fb3698SNikolay Aleksandrov 		other_timer = &brmctx->ip4_other_query.timer;
3057c3fb3698SNikolay Aleksandrov 		if (!querier->addr.src.ip4 ||
3058c3fb3698SNikolay Aleksandrov 		    ntohl(saddr->src.ip4) <= ntohl(querier->addr.src.ip4))
3059dc4eb53aSLinus Lüssing 			goto update;
3060c3fb3698SNikolay Aleksandrov 		break;
3061dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3062c3fb3698SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
3063c3fb3698SNikolay Aleksandrov 		querier = &brmctx->ip6_querier;
3064c3fb3698SNikolay Aleksandrov 		own_timer = &brmctx->ip6_own_query.timer;
3065c3fb3698SNikolay Aleksandrov 		other_timer = &brmctx->ip6_other_query.timer;
3066c3fb3698SNikolay Aleksandrov 		if (ipv6_addr_cmp(&saddr->src.ip6, &querier->addr.src.ip6) <= 0)
3067dc4eb53aSLinus Lüssing 			goto update;
3068c3fb3698SNikolay Aleksandrov 		break;
3069c3fb3698SNikolay Aleksandrov #endif
3070c3fb3698SNikolay Aleksandrov 	default:
3071c3fb3698SNikolay Aleksandrov 		return false;
3072c3fb3698SNikolay Aleksandrov 	}
3073dc4eb53aSLinus Lüssing 
3074c3fb3698SNikolay Aleksandrov 	if (!timer_pending(own_timer) && !timer_pending(other_timer))
3075dc4eb53aSLinus Lüssing 		goto update;
3076dc4eb53aSLinus Lüssing 
3077dc4eb53aSLinus Lüssing 	return false;
3078dc4eb53aSLinus Lüssing 
3079dc4eb53aSLinus Lüssing update:
3080c3fb3698SNikolay Aleksandrov 	br_multicast_update_querier(brmctx, querier, port_ifidx, saddr);
30812cd41431SLinus Lüssing 
3082dc4eb53aSLinus Lüssing 	return true;
3083dc4eb53aSLinus Lüssing }
3084dc4eb53aSLinus Lüssing 
3085c7fa1d9bSNikolay Aleksandrov static struct net_bridge_port *
__br_multicast_get_querier_port(struct net_bridge * br,const struct bridge_mcast_querier * querier)3086c7fa1d9bSNikolay Aleksandrov __br_multicast_get_querier_port(struct net_bridge *br,
3087c7fa1d9bSNikolay Aleksandrov 				const struct bridge_mcast_querier *querier)
3088c7fa1d9bSNikolay Aleksandrov {
3089c7fa1d9bSNikolay Aleksandrov 	int port_ifidx = READ_ONCE(querier->port_ifidx);
3090c7fa1d9bSNikolay Aleksandrov 	struct net_bridge_port *p;
3091c7fa1d9bSNikolay Aleksandrov 	struct net_device *dev;
3092c7fa1d9bSNikolay Aleksandrov 
3093c7fa1d9bSNikolay Aleksandrov 	if (port_ifidx == 0)
3094c7fa1d9bSNikolay Aleksandrov 		return NULL;
3095c7fa1d9bSNikolay Aleksandrov 
3096c7fa1d9bSNikolay Aleksandrov 	dev = dev_get_by_index_rcu(dev_net(br->dev), port_ifidx);
3097c7fa1d9bSNikolay Aleksandrov 	if (!dev)
3098c7fa1d9bSNikolay Aleksandrov 		return NULL;
3099c7fa1d9bSNikolay Aleksandrov 	p = br_port_get_rtnl_rcu(dev);
3100c7fa1d9bSNikolay Aleksandrov 	if (!p || p->br != br)
3101c7fa1d9bSNikolay Aleksandrov 		return NULL;
3102c7fa1d9bSNikolay Aleksandrov 
3103c7fa1d9bSNikolay Aleksandrov 	return p;
3104c7fa1d9bSNikolay Aleksandrov }
3105c7fa1d9bSNikolay Aleksandrov 
br_multicast_querier_state_size(void)3106c7fa1d9bSNikolay Aleksandrov size_t br_multicast_querier_state_size(void)
3107c7fa1d9bSNikolay Aleksandrov {
3108cdda378bSNikolay Aleksandrov 	return nla_total_size(0) +		/* nest attribute */
3109c7fa1d9bSNikolay Aleksandrov 	       nla_total_size(sizeof(__be32)) + /* BRIDGE_QUERIER_IP_ADDRESS */
3110c7fa1d9bSNikolay Aleksandrov 	       nla_total_size(sizeof(int)) +    /* BRIDGE_QUERIER_IP_PORT */
3111175e6692SNikolay Aleksandrov 	       nla_total_size_64bit(sizeof(u64)) + /* BRIDGE_QUERIER_IP_OTHER_TIMER */
3112175e6692SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
3113175e6692SNikolay Aleksandrov 	       nla_total_size(sizeof(struct in6_addr)) + /* BRIDGE_QUERIER_IPV6_ADDRESS */
3114175e6692SNikolay Aleksandrov 	       nla_total_size(sizeof(int)) +		 /* BRIDGE_QUERIER_IPV6_PORT */
3115175e6692SNikolay Aleksandrov 	       nla_total_size_64bit(sizeof(u64)) +	 /* BRIDGE_QUERIER_IPV6_OTHER_TIMER */
3116175e6692SNikolay Aleksandrov #endif
3117175e6692SNikolay Aleksandrov 	       0;
3118c7fa1d9bSNikolay Aleksandrov }
3119c7fa1d9bSNikolay Aleksandrov 
3120c7fa1d9bSNikolay Aleksandrov /* protected by rtnl or rcu */
br_multicast_dump_querier_state(struct sk_buff * skb,const struct net_bridge_mcast * brmctx,int nest_attr)3121c7fa1d9bSNikolay Aleksandrov int br_multicast_dump_querier_state(struct sk_buff *skb,
3122c7fa1d9bSNikolay Aleksandrov 				    const struct net_bridge_mcast *brmctx,
3123c7fa1d9bSNikolay Aleksandrov 				    int nest_attr)
3124c7fa1d9bSNikolay Aleksandrov {
3125c7fa1d9bSNikolay Aleksandrov 	struct bridge_mcast_querier querier = {};
3126c7fa1d9bSNikolay Aleksandrov 	struct net_bridge_port *p;
3127c7fa1d9bSNikolay Aleksandrov 	struct nlattr *nest;
3128c7fa1d9bSNikolay Aleksandrov 
3129f137b7d4SNikolay Aleksandrov 	if (!br_opt_get(brmctx->br, BROPT_MULTICAST_ENABLED) ||
3130f137b7d4SNikolay Aleksandrov 	    br_multicast_ctx_vlan_global_disabled(brmctx))
3131f137b7d4SNikolay Aleksandrov 		return 0;
3132f137b7d4SNikolay Aleksandrov 
3133c7fa1d9bSNikolay Aleksandrov 	nest = nla_nest_start(skb, nest_attr);
3134c7fa1d9bSNikolay Aleksandrov 	if (!nest)
3135c7fa1d9bSNikolay Aleksandrov 		return -EMSGSIZE;
3136c7fa1d9bSNikolay Aleksandrov 
3137c7fa1d9bSNikolay Aleksandrov 	rcu_read_lock();
313885b41082SNikolay Aleksandrov 	if (!brmctx->multicast_querier &&
313985b41082SNikolay Aleksandrov 	    !timer_pending(&brmctx->ip4_other_query.timer))
314085b41082SNikolay Aleksandrov 		goto out_v6;
314185b41082SNikolay Aleksandrov 
3142c7fa1d9bSNikolay Aleksandrov 	br_multicast_read_querier(&brmctx->ip4_querier, &querier);
3143c7fa1d9bSNikolay Aleksandrov 	if (nla_put_in_addr(skb, BRIDGE_QUERIER_IP_ADDRESS,
3144c7fa1d9bSNikolay Aleksandrov 			    querier.addr.src.ip4)) {
3145c7fa1d9bSNikolay Aleksandrov 		rcu_read_unlock();
3146c7fa1d9bSNikolay Aleksandrov 		goto out_err;
3147c7fa1d9bSNikolay Aleksandrov 	}
3148c7fa1d9bSNikolay Aleksandrov 
3149c7fa1d9bSNikolay Aleksandrov 	p = __br_multicast_get_querier_port(brmctx->br, &querier);
3150c7fa1d9bSNikolay Aleksandrov 	if (timer_pending(&brmctx->ip4_other_query.timer) &&
3151c7fa1d9bSNikolay Aleksandrov 	    (nla_put_u64_64bit(skb, BRIDGE_QUERIER_IP_OTHER_TIMER,
3152c7fa1d9bSNikolay Aleksandrov 			       br_timer_value(&brmctx->ip4_other_query.timer),
3153c7fa1d9bSNikolay Aleksandrov 			       BRIDGE_QUERIER_PAD) ||
3154c7fa1d9bSNikolay Aleksandrov 	     (p && nla_put_u32(skb, BRIDGE_QUERIER_IP_PORT, p->dev->ifindex)))) {
3155c7fa1d9bSNikolay Aleksandrov 		rcu_read_unlock();
3156c7fa1d9bSNikolay Aleksandrov 		goto out_err;
3157c7fa1d9bSNikolay Aleksandrov 	}
315885b41082SNikolay Aleksandrov 
315985b41082SNikolay Aleksandrov out_v6:
316085b41082SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
316185b41082SNikolay Aleksandrov 	if (!brmctx->multicast_querier &&
316285b41082SNikolay Aleksandrov 	    !timer_pending(&brmctx->ip6_other_query.timer))
316385b41082SNikolay Aleksandrov 		goto out;
316485b41082SNikolay Aleksandrov 
316585b41082SNikolay Aleksandrov 	br_multicast_read_querier(&brmctx->ip6_querier, &querier);
316685b41082SNikolay Aleksandrov 	if (nla_put_in6_addr(skb, BRIDGE_QUERIER_IPV6_ADDRESS,
316785b41082SNikolay Aleksandrov 			     &querier.addr.src.ip6)) {
316885b41082SNikolay Aleksandrov 		rcu_read_unlock();
316985b41082SNikolay Aleksandrov 		goto out_err;
317085b41082SNikolay Aleksandrov 	}
317185b41082SNikolay Aleksandrov 
317285b41082SNikolay Aleksandrov 	p = __br_multicast_get_querier_port(brmctx->br, &querier);
317385b41082SNikolay Aleksandrov 	if (timer_pending(&brmctx->ip6_other_query.timer) &&
317485b41082SNikolay Aleksandrov 	    (nla_put_u64_64bit(skb, BRIDGE_QUERIER_IPV6_OTHER_TIMER,
317585b41082SNikolay Aleksandrov 			       br_timer_value(&brmctx->ip6_other_query.timer),
317685b41082SNikolay Aleksandrov 			       BRIDGE_QUERIER_PAD) ||
317785b41082SNikolay Aleksandrov 	     (p && nla_put_u32(skb, BRIDGE_QUERIER_IPV6_PORT,
317885b41082SNikolay Aleksandrov 			       p->dev->ifindex)))) {
317985b41082SNikolay Aleksandrov 		rcu_read_unlock();
318085b41082SNikolay Aleksandrov 		goto out_err;
318185b41082SNikolay Aleksandrov 	}
318285b41082SNikolay Aleksandrov out:
318385b41082SNikolay Aleksandrov #endif
3184c7fa1d9bSNikolay Aleksandrov 	rcu_read_unlock();
3185c7fa1d9bSNikolay Aleksandrov 	nla_nest_end(skb, nest);
318685b41082SNikolay Aleksandrov 	if (!nla_len(nest))
318785b41082SNikolay Aleksandrov 		nla_nest_cancel(skb, nest);
3188c7fa1d9bSNikolay Aleksandrov 
3189c7fa1d9bSNikolay Aleksandrov 	return 0;
3190c7fa1d9bSNikolay Aleksandrov 
3191c7fa1d9bSNikolay Aleksandrov out_err:
3192c7fa1d9bSNikolay Aleksandrov 	nla_nest_cancel(skb, nest);
3193c7fa1d9bSNikolay Aleksandrov 	return -EMSGSIZE;
3194c7fa1d9bSNikolay Aleksandrov }
3195c7fa1d9bSNikolay Aleksandrov 
3196cc0fdd80SLinus Lüssing static void
br_multicast_update_query_timer(struct net_bridge_mcast * brmctx,struct bridge_mcast_other_query * query,unsigned long max_delay)3197adc47037SNikolay Aleksandrov br_multicast_update_query_timer(struct net_bridge_mcast *brmctx,
319890010b36SLinus Lüssing 				struct bridge_mcast_other_query *query,
3199b00589afSLinus Lüssing 				unsigned long max_delay)
3200b00589afSLinus Lüssing {
320190010b36SLinus Lüssing 	if (!timer_pending(&query->timer))
3202d99971ecSLinus Lüssing 		mod_timer(&query->delay_timer, jiffies + max_delay);
3203b00589afSLinus Lüssing 
3204adc47037SNikolay Aleksandrov 	mod_timer(&query->timer, jiffies + brmctx->multicast_querier_interval);
3205b00589afSLinus Lüssing }
3206b00589afSLinus Lüssing 
br_port_mc_router_state_change(struct net_bridge_port * p,bool is_mc_router)32076d549648SNogah Frankel static void br_port_mc_router_state_change(struct net_bridge_port *p,
32086d549648SNogah Frankel 					   bool is_mc_router)
32096d549648SNogah Frankel {
32106d549648SNogah Frankel 	struct switchdev_attr attr = {
32116d549648SNogah Frankel 		.orig_dev = p->dev,
32126d549648SNogah Frankel 		.id = SWITCHDEV_ATTR_ID_PORT_MROUTER,
32136d549648SNogah Frankel 		.flags = SWITCHDEV_F_DEFER,
32146d549648SNogah Frankel 		.u.mrouter = is_mc_router,
32156d549648SNogah Frankel 	};
32166d549648SNogah Frankel 
3217dcbdf135SVladimir Oltean 	switchdev_port_attr_set(p->dev, &attr, NULL);
32186d549648SNogah Frankel }
32196d549648SNogah Frankel 
3220d9b8c4d8SLinus Lüssing static struct net_bridge_port *
br_multicast_rport_from_node(struct net_bridge_mcast * brmctx,struct hlist_head * mc_router_list,struct hlist_node * rlist)3221d3d065c0SNikolay Aleksandrov br_multicast_rport_from_node(struct net_bridge_mcast *brmctx,
3222d9b8c4d8SLinus Lüssing 			     struct hlist_head *mc_router_list,
3223d9b8c4d8SLinus Lüssing 			     struct hlist_node *rlist)
3224d9b8c4d8SLinus Lüssing {
32259632233eSNikolay Aleksandrov 	struct net_bridge_mcast_port *pmctx;
32269632233eSNikolay Aleksandrov 
3227a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3228d3d065c0SNikolay Aleksandrov 	if (mc_router_list == &brmctx->ip6_mc_router_list)
32299632233eSNikolay Aleksandrov 		pmctx = hlist_entry(rlist, struct net_bridge_mcast_port,
32309632233eSNikolay Aleksandrov 				    ip6_rlist);
32319632233eSNikolay Aleksandrov 	else
3232a3c02e76SLinus Lüssing #endif
32339632233eSNikolay Aleksandrov 		pmctx = hlist_entry(rlist, struct net_bridge_mcast_port,
32349632233eSNikolay Aleksandrov 				    ip4_rlist);
32359632233eSNikolay Aleksandrov 
32369632233eSNikolay Aleksandrov 	return pmctx->port;
3237d9b8c4d8SLinus Lüssing }
3238d9b8c4d8SLinus Lüssing 
3239d9b8c4d8SLinus Lüssing static struct hlist_node *
br_multicast_get_rport_slot(struct net_bridge_mcast * brmctx,struct net_bridge_port * port,struct hlist_head * mc_router_list)3240d3d065c0SNikolay Aleksandrov br_multicast_get_rport_slot(struct net_bridge_mcast *brmctx,
3241d9b8c4d8SLinus Lüssing 			    struct net_bridge_port *port,
3242d9b8c4d8SLinus Lüssing 			    struct hlist_head *mc_router_list)
3243d9b8c4d8SLinus Lüssing 
3244d9b8c4d8SLinus Lüssing {
3245d9b8c4d8SLinus Lüssing 	struct hlist_node *slot = NULL;
3246d9b8c4d8SLinus Lüssing 	struct net_bridge_port *p;
3247d9b8c4d8SLinus Lüssing 	struct hlist_node *rlist;
3248d9b8c4d8SLinus Lüssing 
3249d9b8c4d8SLinus Lüssing 	hlist_for_each(rlist, mc_router_list) {
3250d3d065c0SNikolay Aleksandrov 		p = br_multicast_rport_from_node(brmctx, mc_router_list, rlist);
3251d9b8c4d8SLinus Lüssing 
3252d9b8c4d8SLinus Lüssing 		if ((unsigned long)port >= (unsigned long)p)
3253d9b8c4d8SLinus Lüssing 			break;
3254d9b8c4d8SLinus Lüssing 
3255d9b8c4d8SLinus Lüssing 		slot = rlist;
3256d9b8c4d8SLinus Lüssing 	}
3257d9b8c4d8SLinus Lüssing 
3258d9b8c4d8SLinus Lüssing 	return slot;
3259d9b8c4d8SLinus Lüssing }
3260d9b8c4d8SLinus Lüssing 
br_multicast_no_router_otherpf(struct net_bridge_mcast_port * pmctx,struct hlist_node * rnode)3261adc47037SNikolay Aleksandrov static bool br_multicast_no_router_otherpf(struct net_bridge_mcast_port *pmctx,
3262a3c02e76SLinus Lüssing 					   struct hlist_node *rnode)
3263a3c02e76SLinus Lüssing {
3264a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3265adc47037SNikolay Aleksandrov 	if (rnode != &pmctx->ip6_rlist)
3266adc47037SNikolay Aleksandrov 		return hlist_unhashed(&pmctx->ip6_rlist);
3267a3c02e76SLinus Lüssing 	else
3268adc47037SNikolay Aleksandrov 		return hlist_unhashed(&pmctx->ip4_rlist);
3269a3c02e76SLinus Lüssing #else
3270a3c02e76SLinus Lüssing 	return true;
3271a3c02e76SLinus Lüssing #endif
3272a3c02e76SLinus Lüssing }
3273a3c02e76SLinus Lüssing 
3274d9b8c4d8SLinus Lüssing /* Add port to router_list
32757e80c124Sstephen hemminger  *  list is maintained ordered by pointer value
32767e80c124Sstephen hemminger  *  and locked by br->multicast_lock and RCU
32777e80c124Sstephen hemminger  */
br_multicast_add_router(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct hlist_node * rlist,struct hlist_head * mc_router_list)3278d3d065c0SNikolay Aleksandrov static void br_multicast_add_router(struct net_bridge_mcast *brmctx,
3279adc47037SNikolay Aleksandrov 				    struct net_bridge_mcast_port *pmctx,
3280d9b8c4d8SLinus Lüssing 				    struct hlist_node *rlist,
3281d9b8c4d8SLinus Lüssing 				    struct hlist_head *mc_router_list)
32820909e117SHerbert Xu {
3283d9b8c4d8SLinus Lüssing 	struct hlist_node *slot;
32840909e117SHerbert Xu 
3285d9b8c4d8SLinus Lüssing 	if (!hlist_unhashed(rlist))
32861a040eacSNikolay Aleksandrov 		return;
32871a040eacSNikolay Aleksandrov 
3288adc47037SNikolay Aleksandrov 	slot = br_multicast_get_rport_slot(brmctx, pmctx->port, mc_router_list);
32890909e117SHerbert Xu 
32907e80c124Sstephen hemminger 	if (slot)
3291d9b8c4d8SLinus Lüssing 		hlist_add_behind_rcu(rlist, slot);
3292dcdca2c4Sstephen hemminger 	else
3293d9b8c4d8SLinus Lüssing 		hlist_add_head_rcu(rlist, mc_router_list);
3294d9b8c4d8SLinus Lüssing 
3295a3c02e76SLinus Lüssing 	/* For backwards compatibility for now, only notify if we
3296a3c02e76SLinus Lüssing 	 * switched from no IPv4/IPv6 multicast router to a new
3297a3c02e76SLinus Lüssing 	 * IPv4 or IPv6 multicast router.
3298a3c02e76SLinus Lüssing 	 */
3299adc47037SNikolay Aleksandrov 	if (br_multicast_no_router_otherpf(pmctx, rlist)) {
33001e9ca456SNikolay Aleksandrov 		br_rtr_notify(pmctx->port->br->dev, pmctx, RTM_NEWMDB);
3301adc47037SNikolay Aleksandrov 		br_port_mc_router_state_change(pmctx->port, true);
33020909e117SHerbert Xu 	}
3303a3c02e76SLinus Lüssing }
33040909e117SHerbert Xu 
3305d9b8c4d8SLinus Lüssing /* Add port to router_list
3306d9b8c4d8SLinus Lüssing  *  list is maintained ordered by pointer value
3307d9b8c4d8SLinus Lüssing  *  and locked by br->multicast_lock and RCU
3308d9b8c4d8SLinus Lüssing  */
br_ip4_multicast_add_router(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx)3309adc47037SNikolay Aleksandrov static void br_ip4_multicast_add_router(struct net_bridge_mcast *brmctx,
3310adc47037SNikolay Aleksandrov 					struct net_bridge_mcast_port *pmctx)
3311eb1d1641SHerbert Xu {
3312adc47037SNikolay Aleksandrov 	br_multicast_add_router(brmctx, pmctx, &pmctx->ip4_rlist,
3313adc47037SNikolay Aleksandrov 				&brmctx->ip4_mc_router_list);
3314d9b8c4d8SLinus Lüssing }
3315d9b8c4d8SLinus Lüssing 
3316a3c02e76SLinus Lüssing /* Add port to router_list
3317a3c02e76SLinus Lüssing  *  list is maintained ordered by pointer value
3318a3c02e76SLinus Lüssing  *  and locked by br->multicast_lock and RCU
3319a3c02e76SLinus Lüssing  */
br_ip6_multicast_add_router(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx)3320adc47037SNikolay Aleksandrov static void br_ip6_multicast_add_router(struct net_bridge_mcast *brmctx,
3321adc47037SNikolay Aleksandrov 					struct net_bridge_mcast_port *pmctx)
3322a3c02e76SLinus Lüssing {
3323a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3324adc47037SNikolay Aleksandrov 	br_multicast_add_router(brmctx, pmctx, &pmctx->ip6_rlist,
3325adc47037SNikolay Aleksandrov 				&brmctx->ip6_mc_router_list);
3326a3c02e76SLinus Lüssing #endif
3327a3c02e76SLinus Lüssing }
3328a3c02e76SLinus Lüssing 
br_multicast_mark_router(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct timer_list * timer,struct hlist_node * rlist,struct hlist_head * mc_router_list)3329adc47037SNikolay Aleksandrov static void br_multicast_mark_router(struct net_bridge_mcast *brmctx,
3330adc47037SNikolay Aleksandrov 				     struct net_bridge_mcast_port *pmctx,
3331d9b8c4d8SLinus Lüssing 				     struct timer_list *timer,
3332d9b8c4d8SLinus Lüssing 				     struct hlist_node *rlist,
3333d9b8c4d8SLinus Lüssing 				     struct hlist_head *mc_router_list)
3334d9b8c4d8SLinus Lüssing {
3335eb1d1641SHerbert Xu 	unsigned long now = jiffies;
3336eb1d1641SHerbert Xu 
33374cdd0d10SNikolay Aleksandrov 	if (!br_multicast_ctx_should_use(brmctx, pmctx))
33384cdd0d10SNikolay Aleksandrov 		return;
33394cdd0d10SNikolay Aleksandrov 
3340adc47037SNikolay Aleksandrov 	if (!pmctx) {
3341d3d065c0SNikolay Aleksandrov 		if (brmctx->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) {
3342d3d065c0SNikolay Aleksandrov 			if (!br_ip4_multicast_is_router(brmctx) &&
3343d3d065c0SNikolay Aleksandrov 			    !br_ip6_multicast_is_router(brmctx))
3344adc47037SNikolay Aleksandrov 				br_mc_router_state_change(brmctx->br, true);
3345d3d065c0SNikolay Aleksandrov 			mod_timer(timer, now + brmctx->multicast_querier_interval);
334677041420SYotam Gigi 		}
3347eb1d1641SHerbert Xu 		return;
3348eb1d1641SHerbert Xu 	}
3349eb1d1641SHerbert Xu 
3350adc47037SNikolay Aleksandrov 	if (pmctx->multicast_router == MDB_RTR_TYPE_DISABLED ||
3351adc47037SNikolay Aleksandrov 	    pmctx->multicast_router == MDB_RTR_TYPE_PERM)
3352eb1d1641SHerbert Xu 		return;
3353eb1d1641SHerbert Xu 
3354adc47037SNikolay Aleksandrov 	br_multicast_add_router(brmctx, pmctx, rlist, mc_router_list);
3355d3d065c0SNikolay Aleksandrov 	mod_timer(timer, now + brmctx->multicast_querier_interval);
3356d9b8c4d8SLinus Lüssing }
3357eb1d1641SHerbert Xu 
br_ip4_multicast_mark_router(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx)3358adc47037SNikolay Aleksandrov static void br_ip4_multicast_mark_router(struct net_bridge_mcast *brmctx,
3359adc47037SNikolay Aleksandrov 					 struct net_bridge_mcast_port *pmctx)
3360d9b8c4d8SLinus Lüssing {
3361adc47037SNikolay Aleksandrov 	struct timer_list *timer = &brmctx->ip4_mc_router_timer;
3362d9b8c4d8SLinus Lüssing 	struct hlist_node *rlist = NULL;
3363d9b8c4d8SLinus Lüssing 
3364adc47037SNikolay Aleksandrov 	if (pmctx) {
3365adc47037SNikolay Aleksandrov 		timer = &pmctx->ip4_mc_router_timer;
3366adc47037SNikolay Aleksandrov 		rlist = &pmctx->ip4_rlist;
3367d9b8c4d8SLinus Lüssing 	}
3368d9b8c4d8SLinus Lüssing 
3369adc47037SNikolay Aleksandrov 	br_multicast_mark_router(brmctx, pmctx, timer, rlist,
3370adc47037SNikolay Aleksandrov 				 &brmctx->ip4_mc_router_list);
3371eb1d1641SHerbert Xu }
3372eb1d1641SHerbert Xu 
br_ip6_multicast_mark_router(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx)3373adc47037SNikolay Aleksandrov static void br_ip6_multicast_mark_router(struct net_bridge_mcast *brmctx,
3374adc47037SNikolay Aleksandrov 					 struct net_bridge_mcast_port *pmctx)
3375a3c02e76SLinus Lüssing {
3376a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3377adc47037SNikolay Aleksandrov 	struct timer_list *timer = &brmctx->ip6_mc_router_timer;
3378a3c02e76SLinus Lüssing 	struct hlist_node *rlist = NULL;
3379a3c02e76SLinus Lüssing 
3380adc47037SNikolay Aleksandrov 	if (pmctx) {
3381adc47037SNikolay Aleksandrov 		timer = &pmctx->ip6_mc_router_timer;
3382adc47037SNikolay Aleksandrov 		rlist = &pmctx->ip6_rlist;
3383a3c02e76SLinus Lüssing 	}
3384a3c02e76SLinus Lüssing 
3385adc47037SNikolay Aleksandrov 	br_multicast_mark_router(brmctx, pmctx, timer, rlist,
3386adc47037SNikolay Aleksandrov 				 &brmctx->ip6_mc_router_list);
3387a3c02e76SLinus Lüssing #endif
3388a3c02e76SLinus Lüssing }
3389a3c02e76SLinus Lüssing 
3390b19232efSLinus Lüssing static void
br_ip4_multicast_query_received(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct bridge_mcast_other_query * query,struct br_ip * saddr,unsigned long max_delay)3391adc47037SNikolay Aleksandrov br_ip4_multicast_query_received(struct net_bridge_mcast *brmctx,
3392adc47037SNikolay Aleksandrov 				struct net_bridge_mcast_port *pmctx,
339390010b36SLinus Lüssing 				struct bridge_mcast_other_query *query,
3394dc4eb53aSLinus Lüssing 				struct br_ip *saddr,
3395b00589afSLinus Lüssing 				unsigned long max_delay)
3396eb1d1641SHerbert Xu {
3397c3fb3698SNikolay Aleksandrov 	if (!br_multicast_select_querier(brmctx, pmctx, saddr))
3398eb1d1641SHerbert Xu 		return;
3399eb1d1641SHerbert Xu 
3400adc47037SNikolay Aleksandrov 	br_multicast_update_query_timer(brmctx, query, max_delay);
3401adc47037SNikolay Aleksandrov 	br_ip4_multicast_mark_router(brmctx, pmctx);
3402eb1d1641SHerbert Xu }
3403eb1d1641SHerbert Xu 
3404b19232efSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3405b19232efSLinus Lüssing static void
br_ip6_multicast_query_received(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct bridge_mcast_other_query * query,struct br_ip * saddr,unsigned long max_delay)3406adc47037SNikolay Aleksandrov br_ip6_multicast_query_received(struct net_bridge_mcast *brmctx,
3407adc47037SNikolay Aleksandrov 				struct net_bridge_mcast_port *pmctx,
3408b19232efSLinus Lüssing 				struct bridge_mcast_other_query *query,
3409b19232efSLinus Lüssing 				struct br_ip *saddr,
3410b19232efSLinus Lüssing 				unsigned long max_delay)
3411b19232efSLinus Lüssing {
3412c3fb3698SNikolay Aleksandrov 	if (!br_multicast_select_querier(brmctx, pmctx, saddr))
3413b19232efSLinus Lüssing 		return;
3414b19232efSLinus Lüssing 
3415adc47037SNikolay Aleksandrov 	br_multicast_update_query_timer(brmctx, query, max_delay);
3416adc47037SNikolay Aleksandrov 	br_ip6_multicast_mark_router(brmctx, pmctx);
3417b19232efSLinus Lüssing }
3418b19232efSLinus Lüssing #endif
3419b19232efSLinus Lüssing 
br_ip4_multicast_query(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct sk_buff * skb,u16 vid)3420adc47037SNikolay Aleksandrov static void br_ip4_multicast_query(struct net_bridge_mcast *brmctx,
3421adc47037SNikolay Aleksandrov 				   struct net_bridge_mcast_port *pmctx,
342206499098SVlad Yasevich 				   struct sk_buff *skb,
342306499098SVlad Yasevich 				   u16 vid)
3424eb1d1641SHerbert Xu {
3425ba5ea614SLinus Lüssing 	unsigned int transport_len = ip_transport_len(skb);
3426b71d1d42SEric Dumazet 	const struct iphdr *iph = ip_hdr(skb);
3427eb1d1641SHerbert Xu 	struct igmphdr *ih = igmp_hdr(skb);
3428eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
3429eb1d1641SHerbert Xu 	struct igmpv3_query *ih3;
3430eb1d1641SHerbert Xu 	struct net_bridge_port_group *p;
3431e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
343267b746f9SNikolay Aleksandrov 	struct br_ip saddr = {};
3433eb1d1641SHerbert Xu 	unsigned long max_delay;
3434eb1d1641SHerbert Xu 	unsigned long now = jiffies;
3435eb1d1641SHerbert Xu 	__be32 group;
3436eb1d1641SHerbert Xu 
3437adc47037SNikolay Aleksandrov 	spin_lock(&brmctx->br->multicast_lock);
34384cdd0d10SNikolay Aleksandrov 	if (!br_multicast_ctx_should_use(brmctx, pmctx))
3439eb1d1641SHerbert Xu 		goto out;
3440eb1d1641SHerbert Xu 
3441eb1d1641SHerbert Xu 	group = ih->group;
3442eb1d1641SHerbert Xu 
3443ba5ea614SLinus Lüssing 	if (transport_len == sizeof(*ih)) {
3444eb1d1641SHerbert Xu 		max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
3445eb1d1641SHerbert Xu 
3446eb1d1641SHerbert Xu 		if (!max_delay) {
3447eb1d1641SHerbert Xu 			max_delay = 10 * HZ;
3448eb1d1641SHerbert Xu 			group = 0;
3449eb1d1641SHerbert Xu 		}
3450ba5ea614SLinus Lüssing 	} else if (transport_len >= sizeof(*ih3)) {
3451eb1d1641SHerbert Xu 		ih3 = igmpv3_query_hdr(skb);
345223550b83SNikolay Aleksandrov 		if (ih3->nsrcs ||
3453d3d065c0SNikolay Aleksandrov 		    (brmctx->multicast_igmp_version == 3 && group &&
3454d3d065c0SNikolay Aleksandrov 		     ih3->suppress))
3455bec68ff1SYOSHIFUJI Hideaki 			goto out;
3456eb1d1641SHerbert Xu 
34570ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 		max_delay = ih3->code ?
34580ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 			    IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
34599afd85c9SLinus Lüssing 	} else {
34609ed973ccSLinus Lüssing 		goto out;
34619ed973ccSLinus Lüssing 	}
34629ed973ccSLinus Lüssing 
3463dc4eb53aSLinus Lüssing 	if (!group) {
3464dc4eb53aSLinus Lüssing 		saddr.proto = htons(ETH_P_IP);
3465deb96566SNikolay Aleksandrov 		saddr.src.ip4 = iph->saddr;
3466b00589afSLinus Lüssing 
3467adc47037SNikolay Aleksandrov 		br_ip4_multicast_query_received(brmctx, pmctx,
3468d3d065c0SNikolay Aleksandrov 						&brmctx->ip4_other_query,
3469dc4eb53aSLinus Lüssing 						&saddr, max_delay);
3470eb1d1641SHerbert Xu 		goto out;
3471dc4eb53aSLinus Lüssing 	}
3472eb1d1641SHerbert Xu 
3473adc47037SNikolay Aleksandrov 	mp = br_mdb_ip4_get(brmctx->br, group, vid);
3474eb1d1641SHerbert Xu 	if (!mp)
3475eb1d1641SHerbert Xu 		goto out;
3476eb1d1641SHerbert Xu 
3477d3d065c0SNikolay Aleksandrov 	max_delay *= brmctx->multicast_last_member_count;
3478eb1d1641SHerbert Xu 
3479ff0fd34eSAndrew Lunn 	if (mp->host_joined &&
3480eb1d1641SHerbert Xu 	    (timer_pending(&mp->timer) ?
3481eb1d1641SHerbert Xu 	     time_after(mp->timer.expires, now + max_delay) :
3482eb1d1641SHerbert Xu 	     try_to_del_timer_sync(&mp->timer) >= 0))
3483eb1d1641SHerbert Xu 		mod_timer(&mp->timer, now + max_delay);
3484eb1d1641SHerbert Xu 
3485e8051688SEric Dumazet 	for (pp = &mp->ports;
3486adc47037SNikolay Aleksandrov 	     (p = mlock_dereference(*pp, brmctx->br)) != NULL;
3487e8051688SEric Dumazet 	     pp = &p->next) {
3488eb1d1641SHerbert Xu 		if (timer_pending(&p->timer) ?
3489eb1d1641SHerbert Xu 		    time_after(p->timer.expires, now + max_delay) :
349023550b83SNikolay Aleksandrov 		    try_to_del_timer_sync(&p->timer) >= 0 &&
3491d3d065c0SNikolay Aleksandrov 		    (brmctx->multicast_igmp_version == 2 ||
349223550b83SNikolay Aleksandrov 		     p->filter_mode == MCAST_EXCLUDE))
349324f9cdcbSHerbert Xu 			mod_timer(&p->timer, now + max_delay);
3494eb1d1641SHerbert Xu 	}
3495eb1d1641SHerbert Xu 
3496eb1d1641SHerbert Xu out:
3497adc47037SNikolay Aleksandrov 	spin_unlock(&brmctx->br->multicast_lock);
3498eb1d1641SHerbert Xu }
3499eb1d1641SHerbert Xu 
3500dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
br_ip6_multicast_query(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct sk_buff * skb,u16 vid)3501adc47037SNikolay Aleksandrov static int br_ip6_multicast_query(struct net_bridge_mcast *brmctx,
3502adc47037SNikolay Aleksandrov 				  struct net_bridge_mcast_port *pmctx,
350306499098SVlad Yasevich 				  struct sk_buff *skb,
350406499098SVlad Yasevich 				  u16 vid)
350508b202b6SYOSHIFUJI Hideaki {
3506ba5ea614SLinus Lüssing 	unsigned int transport_len = ipv6_transport_len(skb);
3507eca2a43bSstephen hemminger 	struct mld_msg *mld;
350808b202b6SYOSHIFUJI Hideaki 	struct net_bridge_mdb_entry *mp;
350908b202b6SYOSHIFUJI Hideaki 	struct mld2_query *mld2q;
3510e8051688SEric Dumazet 	struct net_bridge_port_group *p;
3511e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
351267b746f9SNikolay Aleksandrov 	struct br_ip saddr = {};
351308b202b6SYOSHIFUJI Hideaki 	unsigned long max_delay;
351408b202b6SYOSHIFUJI Hideaki 	unsigned long now = jiffies;
3515856ce5d0SLinus Lüssing 	unsigned int offset = skb_transport_offset(skb);
3516b71d1d42SEric Dumazet 	const struct in6_addr *group = NULL;
35179ed973ccSLinus Lüssing 	bool is_general_query;
351808b202b6SYOSHIFUJI Hideaki 	int err = 0;
351908b202b6SYOSHIFUJI Hideaki 
3520adc47037SNikolay Aleksandrov 	spin_lock(&brmctx->br->multicast_lock);
35214cdd0d10SNikolay Aleksandrov 	if (!br_multicast_ctx_should_use(brmctx, pmctx))
352208b202b6SYOSHIFUJI Hideaki 		goto out;
352308b202b6SYOSHIFUJI Hideaki 
3524ba5ea614SLinus Lüssing 	if (transport_len == sizeof(*mld)) {
3525856ce5d0SLinus Lüssing 		if (!pskb_may_pull(skb, offset + sizeof(*mld))) {
352608b202b6SYOSHIFUJI Hideaki 			err = -EINVAL;
352708b202b6SYOSHIFUJI Hideaki 			goto out;
352808b202b6SYOSHIFUJI Hideaki 		}
352908b202b6SYOSHIFUJI Hideaki 		mld = (struct mld_msg *) icmp6_hdr(skb);
35304715213dSLi RongQing 		max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
353108b202b6SYOSHIFUJI Hideaki 		if (max_delay)
353208b202b6SYOSHIFUJI Hideaki 			group = &mld->mld_mca;
3533248ba8ecSLinus Lüssing 	} else {
3534856ce5d0SLinus Lüssing 		if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) {
353508b202b6SYOSHIFUJI Hideaki 			err = -EINVAL;
353608b202b6SYOSHIFUJI Hideaki 			goto out;
353708b202b6SYOSHIFUJI Hideaki 		}
353808b202b6SYOSHIFUJI Hideaki 		mld2q = (struct mld2_query *)icmp6_hdr(skb);
353908b202b6SYOSHIFUJI Hideaki 		if (!mld2q->mld2q_nsrcs)
354008b202b6SYOSHIFUJI Hideaki 			group = &mld2q->mld2q_mca;
3541d3d065c0SNikolay Aleksandrov 		if (brmctx->multicast_mld_version == 2 &&
354223550b83SNikolay Aleksandrov 		    !ipv6_addr_any(&mld2q->mld2q_mca) &&
354323550b83SNikolay Aleksandrov 		    mld2q->mld2q_suppress)
354423550b83SNikolay Aleksandrov 			goto out;
3545e3f5b170SDaniel Borkmann 
3546e3f5b170SDaniel Borkmann 		max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
354708b202b6SYOSHIFUJI Hideaki 	}
354808b202b6SYOSHIFUJI Hideaki 
35499ed973ccSLinus Lüssing 	is_general_query = group && ipv6_addr_any(group);
35509ed973ccSLinus Lüssing 
3551dc4eb53aSLinus Lüssing 	if (is_general_query) {
3552dc4eb53aSLinus Lüssing 		saddr.proto = htons(ETH_P_IPV6);
3553deb96566SNikolay Aleksandrov 		saddr.src.ip6 = ipv6_hdr(skb)->saddr;
3554b00589afSLinus Lüssing 
3555adc47037SNikolay Aleksandrov 		br_ip6_multicast_query_received(brmctx, pmctx,
3556d3d065c0SNikolay Aleksandrov 						&brmctx->ip6_other_query,
3557dc4eb53aSLinus Lüssing 						&saddr, max_delay);
355808b202b6SYOSHIFUJI Hideaki 		goto out;
35596c03ee8bSLinus Lüssing 	} else if (!group) {
35606c03ee8bSLinus Lüssing 		goto out;
3561dc4eb53aSLinus Lüssing 	}
356208b202b6SYOSHIFUJI Hideaki 
3563adc47037SNikolay Aleksandrov 	mp = br_mdb_ip6_get(brmctx->br, group, vid);
356408b202b6SYOSHIFUJI Hideaki 	if (!mp)
356508b202b6SYOSHIFUJI Hideaki 		goto out;
356608b202b6SYOSHIFUJI Hideaki 
3567d3d065c0SNikolay Aleksandrov 	max_delay *= brmctx->multicast_last_member_count;
3568ff0fd34eSAndrew Lunn 	if (mp->host_joined &&
356908b202b6SYOSHIFUJI Hideaki 	    (timer_pending(&mp->timer) ?
357008b202b6SYOSHIFUJI Hideaki 	     time_after(mp->timer.expires, now + max_delay) :
357108b202b6SYOSHIFUJI Hideaki 	     try_to_del_timer_sync(&mp->timer) >= 0))
357208b202b6SYOSHIFUJI Hideaki 		mod_timer(&mp->timer, now + max_delay);
357308b202b6SYOSHIFUJI Hideaki 
3574e8051688SEric Dumazet 	for (pp = &mp->ports;
3575adc47037SNikolay Aleksandrov 	     (p = mlock_dereference(*pp, brmctx->br)) != NULL;
3576e8051688SEric Dumazet 	     pp = &p->next) {
357708b202b6SYOSHIFUJI Hideaki 		if (timer_pending(&p->timer) ?
357808b202b6SYOSHIFUJI Hideaki 		    time_after(p->timer.expires, now + max_delay) :
357923550b83SNikolay Aleksandrov 		    try_to_del_timer_sync(&p->timer) >= 0 &&
3580d3d065c0SNikolay Aleksandrov 		    (brmctx->multicast_mld_version == 1 ||
358123550b83SNikolay Aleksandrov 		     p->filter_mode == MCAST_EXCLUDE))
358224f9cdcbSHerbert Xu 			mod_timer(&p->timer, now + max_delay);
358308b202b6SYOSHIFUJI Hideaki 	}
358408b202b6SYOSHIFUJI Hideaki 
358508b202b6SYOSHIFUJI Hideaki out:
3586adc47037SNikolay Aleksandrov 	spin_unlock(&brmctx->br->multicast_lock);
358708b202b6SYOSHIFUJI Hideaki 	return err;
358808b202b6SYOSHIFUJI Hideaki }
358908b202b6SYOSHIFUJI Hideaki #endif
359008b202b6SYOSHIFUJI Hideaki 
359190010b36SLinus Lüssing static void
br_multicast_leave_group(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct br_ip * group,struct bridge_mcast_other_query * other_query,struct bridge_mcast_own_query * own_query,const unsigned char * src)3592adc47037SNikolay Aleksandrov br_multicast_leave_group(struct net_bridge_mcast *brmctx,
3593adc47037SNikolay Aleksandrov 			 struct net_bridge_mcast_port *pmctx,
3594cc0fdd80SLinus Lüssing 			 struct br_ip *group,
359590010b36SLinus Lüssing 			 struct bridge_mcast_other_query *other_query,
35966db6f0eaSFelix Fietkau 			 struct bridge_mcast_own_query *own_query,
35976db6f0eaSFelix Fietkau 			 const unsigned char *src)
3598eb1d1641SHerbert Xu {
3599eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
3600eb1d1641SHerbert Xu 	struct net_bridge_port_group *p;
3601eb1d1641SHerbert Xu 	unsigned long now;
3602eb1d1641SHerbert Xu 	unsigned long time;
3603eb1d1641SHerbert Xu 
3604adc47037SNikolay Aleksandrov 	spin_lock(&brmctx->br->multicast_lock);
36054cdd0d10SNikolay Aleksandrov 	if (!br_multicast_ctx_should_use(brmctx, pmctx))
3606eb1d1641SHerbert Xu 		goto out;
3607eb1d1641SHerbert Xu 
3608adc47037SNikolay Aleksandrov 	mp = br_mdb_ip_get(brmctx->br, group);
3609eb1d1641SHerbert Xu 	if (!mp)
3610eb1d1641SHerbert Xu 		goto out;
3611eb1d1641SHerbert Xu 
3612adc47037SNikolay Aleksandrov 	if (pmctx && (pmctx->port->flags & BR_MULTICAST_FAST_LEAVE)) {
3613544586f7SSatish Ashok 		struct net_bridge_port_group __rcu **pp;
3614544586f7SSatish Ashok 
3615544586f7SSatish Ashok 		for (pp = &mp->ports;
3616adc47037SNikolay Aleksandrov 		     (p = mlock_dereference(*pp, brmctx->br)) != NULL;
3617544586f7SSatish Ashok 		     pp = &p->next) {
3618adc47037SNikolay Aleksandrov 			if (!br_port_group_equal(p, pmctx->port, src))
3619544586f7SSatish Ashok 				continue;
3620544586f7SSatish Ashok 
36215c725b6bSNikolay Aleksandrov 			if (p->flags & MDB_PG_FLAGS_PERMANENT)
36225c725b6bSNikolay Aleksandrov 				break;
36235c725b6bSNikolay Aleksandrov 
3624681590bdSNikolay Aleksandrov 			p->flags |= MDB_PG_FLAGS_FAST_LEAVE;
3625681590bdSNikolay Aleksandrov 			br_multicast_del_pg(mp, p, pp);
3626544586f7SSatish Ashok 		}
3627544586f7SSatish Ashok 		goto out;
3628544586f7SSatish Ashok 	}
3629544586f7SSatish Ashok 
3630544586f7SSatish Ashok 	if (timer_pending(&other_query->timer))
3631544586f7SSatish Ashok 		goto out;
3632544586f7SSatish Ashok 
36334d5b4e84SNikolay Aleksandrov 	if (brmctx->multicast_querier) {
3634adc47037SNikolay Aleksandrov 		__br_multicast_send_query(brmctx, pmctx, NULL, NULL, &mp->addr,
363542c11ccfSNikolay Aleksandrov 					  false, 0, NULL);
36366b7df111SCong Wang 
3637d3d065c0SNikolay Aleksandrov 		time = jiffies + brmctx->multicast_last_member_count *
3638d3d065c0SNikolay Aleksandrov 				 brmctx->multicast_last_member_interval;
3639cc0fdd80SLinus Lüssing 
364090010b36SLinus Lüssing 		mod_timer(&own_query->timer, time);
36416b7df111SCong Wang 
3642adc47037SNikolay Aleksandrov 		for (p = mlock_dereference(mp->ports, brmctx->br);
364354cb4319SNikolay Aleksandrov 		     p != NULL && pmctx != NULL;
3644adc47037SNikolay Aleksandrov 		     p = mlock_dereference(p->next, brmctx->br)) {
3645adc47037SNikolay Aleksandrov 			if (!br_port_group_equal(p, pmctx->port, src))
36466b7df111SCong Wang 				continue;
36476b7df111SCong Wang 
36486b7df111SCong Wang 			if (!hlist_unhashed(&p->mglist) &&
36496b7df111SCong Wang 			    (timer_pending(&p->timer) ?
36506b7df111SCong Wang 			     time_after(p->timer.expires, time) :
36516b7df111SCong Wang 			     try_to_del_timer_sync(&p->timer) >= 0)) {
36526b7df111SCong Wang 				mod_timer(&p->timer, time);
36536b7df111SCong Wang 			}
36546b7df111SCong Wang 
36556b7df111SCong Wang 			break;
36566b7df111SCong Wang 		}
36576b7df111SCong Wang 	}
36586b7df111SCong Wang 
3659eb1d1641SHerbert Xu 	now = jiffies;
3660d3d065c0SNikolay Aleksandrov 	time = now + brmctx->multicast_last_member_count *
3661d3d065c0SNikolay Aleksandrov 		     brmctx->multicast_last_member_interval;
3662eb1d1641SHerbert Xu 
3663adc47037SNikolay Aleksandrov 	if (!pmctx) {
3664ff0fd34eSAndrew Lunn 		if (mp->host_joined &&
3665eb1d1641SHerbert Xu 		    (timer_pending(&mp->timer) ?
3666eb1d1641SHerbert Xu 		     time_after(mp->timer.expires, time) :
3667eb1d1641SHerbert Xu 		     try_to_del_timer_sync(&mp->timer) >= 0)) {
3668eb1d1641SHerbert Xu 			mod_timer(&mp->timer, time);
3669eb1d1641SHerbert Xu 		}
3670454594f3SLinus Lüssing 
3671454594f3SLinus Lüssing 		goto out;
3672454594f3SLinus Lüssing 	}
3673454594f3SLinus Lüssing 
3674adc47037SNikolay Aleksandrov 	for (p = mlock_dereference(mp->ports, brmctx->br);
3675454594f3SLinus Lüssing 	     p != NULL;
3676adc47037SNikolay Aleksandrov 	     p = mlock_dereference(p->next, brmctx->br)) {
3677adc47037SNikolay Aleksandrov 		if (p->key.port != pmctx->port)
3678454594f3SLinus Lüssing 			continue;
3679454594f3SLinus Lüssing 
3680454594f3SLinus Lüssing 		if (!hlist_unhashed(&p->mglist) &&
3681454594f3SLinus Lüssing 		    (timer_pending(&p->timer) ?
3682454594f3SLinus Lüssing 		     time_after(p->timer.expires, time) :
3683454594f3SLinus Lüssing 		     try_to_del_timer_sync(&p->timer) >= 0)) {
3684454594f3SLinus Lüssing 			mod_timer(&p->timer, time);
3685454594f3SLinus Lüssing 		}
3686454594f3SLinus Lüssing 
3687454594f3SLinus Lüssing 		break;
3688eb1d1641SHerbert Xu 	}
3689eb1d1641SHerbert Xu out:
3690adc47037SNikolay Aleksandrov 	spin_unlock(&brmctx->br->multicast_lock);
3691eb1d1641SHerbert Xu }
3692eb1d1641SHerbert Xu 
br_ip4_multicast_leave_group(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,__be32 group,__u16 vid,const unsigned char * src)3693adc47037SNikolay Aleksandrov static void br_ip4_multicast_leave_group(struct net_bridge_mcast *brmctx,
3694adc47037SNikolay Aleksandrov 					 struct net_bridge_mcast_port *pmctx,
3695b0e9a30dSVlad Yasevich 					 __be32 group,
36966db6f0eaSFelix Fietkau 					 __u16 vid,
36976db6f0eaSFelix Fietkau 					 const unsigned char *src)
36988ef2a9a5SYOSHIFUJI Hideaki {
36998ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip br_group;
370090010b36SLinus Lüssing 	struct bridge_mcast_own_query *own_query;
37018ef2a9a5SYOSHIFUJI Hideaki 
37028ef2a9a5SYOSHIFUJI Hideaki 	if (ipv4_is_local_multicast(group))
37038ef2a9a5SYOSHIFUJI Hideaki 		return;
37048ef2a9a5SYOSHIFUJI Hideaki 
3705adc47037SNikolay Aleksandrov 	own_query = pmctx ? &pmctx->ip4_own_query : &brmctx->ip4_own_query;
370690010b36SLinus Lüssing 
37071515a63fSNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
3708eab3227bSNikolay Aleksandrov 	br_group.dst.ip4 = group;
37098ef2a9a5SYOSHIFUJI Hideaki 	br_group.proto = htons(ETH_P_IP);
3710b0e9a30dSVlad Yasevich 	br_group.vid = vid;
37118ef2a9a5SYOSHIFUJI Hideaki 
3712adc47037SNikolay Aleksandrov 	br_multicast_leave_group(brmctx, pmctx, &br_group,
3713adc47037SNikolay Aleksandrov 				 &brmctx->ip4_other_query,
37146db6f0eaSFelix Fietkau 				 own_query, src);
37158ef2a9a5SYOSHIFUJI Hideaki }
37168ef2a9a5SYOSHIFUJI Hideaki 
3717dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
br_ip6_multicast_leave_group(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,const struct in6_addr * group,__u16 vid,const unsigned char * src)3718adc47037SNikolay Aleksandrov static void br_ip6_multicast_leave_group(struct net_bridge_mcast *brmctx,
3719adc47037SNikolay Aleksandrov 					 struct net_bridge_mcast_port *pmctx,
3720b0e9a30dSVlad Yasevich 					 const struct in6_addr *group,
37216db6f0eaSFelix Fietkau 					 __u16 vid,
37226db6f0eaSFelix Fietkau 					 const unsigned char *src)
372308b202b6SYOSHIFUJI Hideaki {
372408b202b6SYOSHIFUJI Hideaki 	struct br_ip br_group;
372590010b36SLinus Lüssing 	struct bridge_mcast_own_query *own_query;
372608b202b6SYOSHIFUJI Hideaki 
37273c3769e6SLinus Lüssing 	if (ipv6_addr_is_ll_all_nodes(group))
372808b202b6SYOSHIFUJI Hideaki 		return;
372908b202b6SYOSHIFUJI Hideaki 
3730adc47037SNikolay Aleksandrov 	own_query = pmctx ? &pmctx->ip6_own_query : &brmctx->ip6_own_query;
373190010b36SLinus Lüssing 
37321515a63fSNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
3733eab3227bSNikolay Aleksandrov 	br_group.dst.ip6 = *group;
373408b202b6SYOSHIFUJI Hideaki 	br_group.proto = htons(ETH_P_IPV6);
3735b0e9a30dSVlad Yasevich 	br_group.vid = vid;
373608b202b6SYOSHIFUJI Hideaki 
3737adc47037SNikolay Aleksandrov 	br_multicast_leave_group(brmctx, pmctx, &br_group,
3738adc47037SNikolay Aleksandrov 				 &brmctx->ip6_other_query,
37396db6f0eaSFelix Fietkau 				 own_query, src);
374008b202b6SYOSHIFUJI Hideaki }
374108b202b6SYOSHIFUJI Hideaki #endif
37428ef2a9a5SYOSHIFUJI Hideaki 
br_multicast_err_count(const struct net_bridge * br,const struct net_bridge_port * p,__be16 proto)37431080ab95SNikolay Aleksandrov static void br_multicast_err_count(const struct net_bridge *br,
37441080ab95SNikolay Aleksandrov 				   const struct net_bridge_port *p,
37451080ab95SNikolay Aleksandrov 				   __be16 proto)
37461080ab95SNikolay Aleksandrov {
37471080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
37481080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats *pstats;
37491080ab95SNikolay Aleksandrov 
3750675779adSNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
37511080ab95SNikolay Aleksandrov 		return;
37521080ab95SNikolay Aleksandrov 
37531080ab95SNikolay Aleksandrov 	if (p)
37541080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
37551080ab95SNikolay Aleksandrov 	else
37561080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
37571080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
37581080ab95SNikolay Aleksandrov 		return;
37591080ab95SNikolay Aleksandrov 
37601080ab95SNikolay Aleksandrov 	pstats = this_cpu_ptr(stats);
37611080ab95SNikolay Aleksandrov 
37621080ab95SNikolay Aleksandrov 	u64_stats_update_begin(&pstats->syncp);
37631080ab95SNikolay Aleksandrov 	switch (proto) {
37641080ab95SNikolay Aleksandrov 	case htons(ETH_P_IP):
37651080ab95SNikolay Aleksandrov 		pstats->mstats.igmp_parse_errors++;
37661080ab95SNikolay Aleksandrov 		break;
37671080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
37681080ab95SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
37691080ab95SNikolay Aleksandrov 		pstats->mstats.mld_parse_errors++;
37701080ab95SNikolay Aleksandrov 		break;
37711080ab95SNikolay Aleksandrov #endif
37721080ab95SNikolay Aleksandrov 	}
37731080ab95SNikolay Aleksandrov 	u64_stats_update_end(&pstats->syncp);
37741080ab95SNikolay Aleksandrov }
37751080ab95SNikolay Aleksandrov 
br_multicast_pim(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,const struct sk_buff * skb)3776adc47037SNikolay Aleksandrov static void br_multicast_pim(struct net_bridge_mcast *brmctx,
3777adc47037SNikolay Aleksandrov 			     struct net_bridge_mcast_port *pmctx,
377891b02d3dSNikolay Aleksandrov 			     const struct sk_buff *skb)
377991b02d3dSNikolay Aleksandrov {
378091b02d3dSNikolay Aleksandrov 	unsigned int offset = skb_transport_offset(skb);
378191b02d3dSNikolay Aleksandrov 	struct pimhdr *pimhdr, _pimhdr;
378291b02d3dSNikolay Aleksandrov 
378391b02d3dSNikolay Aleksandrov 	pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr);
378491b02d3dSNikolay Aleksandrov 	if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION ||
378591b02d3dSNikolay Aleksandrov 	    pim_hdr_type(pimhdr) != PIM_TYPE_HELLO)
378691b02d3dSNikolay Aleksandrov 		return;
378791b02d3dSNikolay Aleksandrov 
3788adc47037SNikolay Aleksandrov 	spin_lock(&brmctx->br->multicast_lock);
3789adc47037SNikolay Aleksandrov 	br_ip4_multicast_mark_router(brmctx, pmctx);
3790adc47037SNikolay Aleksandrov 	spin_unlock(&brmctx->br->multicast_lock);
379191b02d3dSNikolay Aleksandrov }
379291b02d3dSNikolay Aleksandrov 
br_ip4_multicast_mrd_rcv(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct sk_buff * skb)3793adc47037SNikolay Aleksandrov static int br_ip4_multicast_mrd_rcv(struct net_bridge_mcast *brmctx,
3794adc47037SNikolay Aleksandrov 				    struct net_bridge_mcast_port *pmctx,
37954b3087c7SLinus Lüssing 				    struct sk_buff *skb)
37964b3087c7SLinus Lüssing {
37974b3087c7SLinus Lüssing 	if (ip_hdr(skb)->protocol != IPPROTO_IGMP ||
37984b3087c7SLinus Lüssing 	    igmp_hdr(skb)->type != IGMP_MRDISC_ADV)
37994b3087c7SLinus Lüssing 		return -ENOMSG;
38004b3087c7SLinus Lüssing 
3801adc47037SNikolay Aleksandrov 	spin_lock(&brmctx->br->multicast_lock);
3802adc47037SNikolay Aleksandrov 	br_ip4_multicast_mark_router(brmctx, pmctx);
3803adc47037SNikolay Aleksandrov 	spin_unlock(&brmctx->br->multicast_lock);
38044b3087c7SLinus Lüssing 
38054b3087c7SLinus Lüssing 	return 0;
38064b3087c7SLinus Lüssing }
38074b3087c7SLinus Lüssing 
br_multicast_ipv4_rcv(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct sk_buff * skb,u16 vid)3808adc47037SNikolay Aleksandrov static int br_multicast_ipv4_rcv(struct net_bridge_mcast *brmctx,
3809adc47037SNikolay Aleksandrov 				 struct net_bridge_mcast_port *pmctx,
381006499098SVlad Yasevich 				 struct sk_buff *skb,
381106499098SVlad Yasevich 				 u16 vid)
3812eb1d1641SHerbert Xu {
3813adc47037SNikolay Aleksandrov 	struct net_bridge_port *p = pmctx ? pmctx->port : NULL;
38146db6f0eaSFelix Fietkau 	const unsigned char *src;
3815eb1d1641SHerbert Xu 	struct igmphdr *ih;
3816eb1d1641SHerbert Xu 	int err;
3817eb1d1641SHerbert Xu 
3818ba5ea614SLinus Lüssing 	err = ip_mc_check_igmp(skb);
3819eb1d1641SHerbert Xu 
38209afd85c9SLinus Lüssing 	if (err == -ENOMSG) {
382191b02d3dSNikolay Aleksandrov 		if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) {
3822bd4265feSHerbert Xu 			BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
382391b02d3dSNikolay Aleksandrov 		} else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) {
382491b02d3dSNikolay Aleksandrov 			if (ip_hdr(skb)->protocol == IPPROTO_PIM)
3825adc47037SNikolay Aleksandrov 				br_multicast_pim(brmctx, pmctx, skb);
38264b3087c7SLinus Lüssing 		} else if (ipv4_is_all_snoopers(ip_hdr(skb)->daddr)) {
3827adc47037SNikolay Aleksandrov 			br_ip4_multicast_mrd_rcv(brmctx, pmctx, skb);
38284b3087c7SLinus Lüssing 		}
38294b3087c7SLinus Lüssing 
3830eb1d1641SHerbert Xu 		return 0;
38319afd85c9SLinus Lüssing 	} else if (err < 0) {
3832adc47037SNikolay Aleksandrov 		br_multicast_err_count(brmctx->br, p, skb->protocol);
38339afd85c9SLinus Lüssing 		return err;
3834bd4265feSHerbert Xu 	}
3835eb1d1641SHerbert Xu 
38369afd85c9SLinus Lüssing 	ih = igmp_hdr(skb);
38376db6f0eaSFelix Fietkau 	src = eth_hdr(skb)->h_source;
38381080ab95SNikolay Aleksandrov 	BR_INPUT_SKB_CB(skb)->igmp = ih->type;
3839eb1d1641SHerbert Xu 
3840eb1d1641SHerbert Xu 	switch (ih->type) {
3841eb1d1641SHerbert Xu 	case IGMP_HOST_MEMBERSHIP_REPORT:
3842eb1d1641SHerbert Xu 	case IGMPV2_HOST_MEMBERSHIP_REPORT:
384362b2bcb4SFernando Luis Vázquez Cao 		BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
3844adc47037SNikolay Aleksandrov 		err = br_ip4_multicast_add_group(brmctx, pmctx, ih->group, vid,
3845adc47037SNikolay Aleksandrov 						 src, true);
3846eb1d1641SHerbert Xu 		break;
3847eb1d1641SHerbert Xu 	case IGMPV3_HOST_MEMBERSHIP_REPORT:
3848adc47037SNikolay Aleksandrov 		err = br_ip4_multicast_igmp3_report(brmctx, pmctx, skb, vid);
3849eb1d1641SHerbert Xu 		break;
3850eb1d1641SHerbert Xu 	case IGMP_HOST_MEMBERSHIP_QUERY:
3851adc47037SNikolay Aleksandrov 		br_ip4_multicast_query(brmctx, pmctx, skb, vid);
3852eb1d1641SHerbert Xu 		break;
3853eb1d1641SHerbert Xu 	case IGMP_HOST_LEAVE_MESSAGE:
3854adc47037SNikolay Aleksandrov 		br_ip4_multicast_leave_group(brmctx, pmctx, ih->group, vid, src);
3855eb1d1641SHerbert Xu 		break;
3856eb1d1641SHerbert Xu 	}
3857eb1d1641SHerbert Xu 
3858adc47037SNikolay Aleksandrov 	br_multicast_count(brmctx->br, p, skb, BR_INPUT_SKB_CB(skb)->igmp,
38591080ab95SNikolay Aleksandrov 			   BR_MCAST_DIR_RX);
38601080ab95SNikolay Aleksandrov 
3861eb1d1641SHerbert Xu 	return err;
3862eb1d1641SHerbert Xu }
3863eb1d1641SHerbert Xu 
3864dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
br_ip6_multicast_mrd_rcv(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct sk_buff * skb)3865adc47037SNikolay Aleksandrov static void br_ip6_multicast_mrd_rcv(struct net_bridge_mcast *brmctx,
3866adc47037SNikolay Aleksandrov 				     struct net_bridge_mcast_port *pmctx,
38674b3087c7SLinus Lüssing 				     struct sk_buff *skb)
38684b3087c7SLinus Lüssing {
38694b3087c7SLinus Lüssing 	if (icmp6_hdr(skb)->icmp6_type != ICMPV6_MRDISC_ADV)
387099014088SLinus Lüssing 		return;
38714b3087c7SLinus Lüssing 
3872adc47037SNikolay Aleksandrov 	spin_lock(&brmctx->br->multicast_lock);
3873adc47037SNikolay Aleksandrov 	br_ip6_multicast_mark_router(brmctx, pmctx);
3874adc47037SNikolay Aleksandrov 	spin_unlock(&brmctx->br->multicast_lock);
38754b3087c7SLinus Lüssing }
38764b3087c7SLinus Lüssing 
br_multicast_ipv6_rcv(struct net_bridge_mcast * brmctx,struct net_bridge_mcast_port * pmctx,struct sk_buff * skb,u16 vid)3877adc47037SNikolay Aleksandrov static int br_multicast_ipv6_rcv(struct net_bridge_mcast *brmctx,
3878adc47037SNikolay Aleksandrov 				 struct net_bridge_mcast_port *pmctx,
387906499098SVlad Yasevich 				 struct sk_buff *skb,
388006499098SVlad Yasevich 				 u16 vid)
388108b202b6SYOSHIFUJI Hideaki {
3882adc47037SNikolay Aleksandrov 	struct net_bridge_port *p = pmctx ? pmctx->port : NULL;
38836db6f0eaSFelix Fietkau 	const unsigned char *src;
38849afd85c9SLinus Lüssing 	struct mld_msg *mld;
388508b202b6SYOSHIFUJI Hideaki 	int err;
388608b202b6SYOSHIFUJI Hideaki 
3887ba5ea614SLinus Lüssing 	err = ipv6_mc_check_mld(skb);
388808b202b6SYOSHIFUJI Hideaki 
388999014088SLinus Lüssing 	if (err == -ENOMSG || err == -ENODATA) {
38909afd85c9SLinus Lüssing 		if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
38918fad9c39SLinus Lüssing 			BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
389299014088SLinus Lüssing 		if (err == -ENODATA &&
389399014088SLinus Lüssing 		    ipv6_addr_is_all_snoopers(&ipv6_hdr(skb)->daddr))
3894adc47037SNikolay Aleksandrov 			br_ip6_multicast_mrd_rcv(brmctx, pmctx, skb);
38954b3087c7SLinus Lüssing 
389608b202b6SYOSHIFUJI Hideaki 		return 0;
38979afd85c9SLinus Lüssing 	} else if (err < 0) {
3898adc47037SNikolay Aleksandrov 		br_multicast_err_count(brmctx->br, p, skb->protocol);
38999afd85c9SLinus Lüssing 		return err;
390008b202b6SYOSHIFUJI Hideaki 	}
390108b202b6SYOSHIFUJI Hideaki 
39029afd85c9SLinus Lüssing 	mld = (struct mld_msg *)skb_transport_header(skb);
39031080ab95SNikolay Aleksandrov 	BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type;
390408b202b6SYOSHIFUJI Hideaki 
39059afd85c9SLinus Lüssing 	switch (mld->mld_type) {
390608b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_REPORT:
39076db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
3908fc2af6c7SFernando Luis Vázquez Cao 		BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
3909adc47037SNikolay Aleksandrov 		err = br_ip6_multicast_add_group(brmctx, pmctx, &mld->mld_mca,
3910adc47037SNikolay Aleksandrov 						 vid, src, true);
391108b202b6SYOSHIFUJI Hideaki 		break;
391208b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MLD2_REPORT:
3913adc47037SNikolay Aleksandrov 		err = br_ip6_multicast_mld2_report(brmctx, pmctx, skb, vid);
391408b202b6SYOSHIFUJI Hideaki 		break;
391508b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_QUERY:
3916adc47037SNikolay Aleksandrov 		err = br_ip6_multicast_query(brmctx, pmctx, skb, vid);
391708b202b6SYOSHIFUJI Hideaki 		break;
391808b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_REDUCTION:
39196db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
3920adc47037SNikolay Aleksandrov 		br_ip6_multicast_leave_group(brmctx, pmctx, &mld->mld_mca, vid,
3921adc47037SNikolay Aleksandrov 					     src);
39229afd85c9SLinus Lüssing 		break;
392308b202b6SYOSHIFUJI Hideaki 	}
392408b202b6SYOSHIFUJI Hideaki 
3925adc47037SNikolay Aleksandrov 	br_multicast_count(brmctx->br, p, skb, BR_INPUT_SKB_CB(skb)->igmp,
39261080ab95SNikolay Aleksandrov 			   BR_MCAST_DIR_RX);
39271080ab95SNikolay Aleksandrov 
392808b202b6SYOSHIFUJI Hideaki 	return err;
392908b202b6SYOSHIFUJI Hideaki }
393008b202b6SYOSHIFUJI Hideaki #endif
393108b202b6SYOSHIFUJI Hideaki 
br_multicast_rcv(struct net_bridge_mcast ** brmctx,struct net_bridge_mcast_port ** pmctx,struct net_bridge_vlan * vlan,struct sk_buff * skb,u16 vid)3932f4b7002aSNikolay Aleksandrov int br_multicast_rcv(struct net_bridge_mcast **brmctx,
3933f4b7002aSNikolay Aleksandrov 		     struct net_bridge_mcast_port **pmctx,
3934f4b7002aSNikolay Aleksandrov 		     struct net_bridge_vlan *vlan,
393506499098SVlad Yasevich 		     struct sk_buff *skb, u16 vid)
3936eb1d1641SHerbert Xu {
39371080ab95SNikolay Aleksandrov 	int ret = 0;
39381080ab95SNikolay Aleksandrov 
39391fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 	BR_INPUT_SKB_CB(skb)->igmp = 0;
39401fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 	BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
39411fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 
3942f4b7002aSNikolay Aleksandrov 	if (!br_opt_get((*brmctx)->br, BROPT_MULTICAST_ENABLED))
3943eb1d1641SHerbert Xu 		return 0;
3944eb1d1641SHerbert Xu 
3945f4b7002aSNikolay Aleksandrov 	if (br_opt_get((*brmctx)->br, BROPT_MCAST_VLAN_SNOOPING_ENABLED) && vlan) {
3946f4b7002aSNikolay Aleksandrov 		const struct net_bridge_vlan *masterv;
3947f4b7002aSNikolay Aleksandrov 
3948f4b7002aSNikolay Aleksandrov 		/* the vlan has the master flag set only when transmitting
3949f4b7002aSNikolay Aleksandrov 		 * through the bridge device
3950f4b7002aSNikolay Aleksandrov 		 */
3951f4b7002aSNikolay Aleksandrov 		if (br_vlan_is_master(vlan)) {
3952f4b7002aSNikolay Aleksandrov 			masterv = vlan;
3953f4b7002aSNikolay Aleksandrov 			*brmctx = &vlan->br_mcast_ctx;
3954f4b7002aSNikolay Aleksandrov 			*pmctx = NULL;
3955f4b7002aSNikolay Aleksandrov 		} else {
3956f4b7002aSNikolay Aleksandrov 			masterv = vlan->brvlan;
3957f4b7002aSNikolay Aleksandrov 			*brmctx = &vlan->brvlan->br_mcast_ctx;
3958f4b7002aSNikolay Aleksandrov 			*pmctx = &vlan->port_mcast_ctx;
3959f4b7002aSNikolay Aleksandrov 		}
3960f4b7002aSNikolay Aleksandrov 
3961f4b7002aSNikolay Aleksandrov 		if (!(masterv->priv_flags & BR_VLFLAG_GLOBAL_MCAST_ENABLED))
3962f4b7002aSNikolay Aleksandrov 			return 0;
3963f4b7002aSNikolay Aleksandrov 	}
3964f4b7002aSNikolay Aleksandrov 
3965eb1d1641SHerbert Xu 	switch (skb->protocol) {
3966eb1d1641SHerbert Xu 	case htons(ETH_P_IP):
3967f4b7002aSNikolay Aleksandrov 		ret = br_multicast_ipv4_rcv(*brmctx, *pmctx, skb, vid);
39681080ab95SNikolay Aleksandrov 		break;
3969dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
397008b202b6SYOSHIFUJI Hideaki 	case htons(ETH_P_IPV6):
3971f4b7002aSNikolay Aleksandrov 		ret = br_multicast_ipv6_rcv(*brmctx, *pmctx, skb, vid);
39721080ab95SNikolay Aleksandrov 		break;
397308b202b6SYOSHIFUJI Hideaki #endif
3974eb1d1641SHerbert Xu 	}
3975eb1d1641SHerbert Xu 
39761080ab95SNikolay Aleksandrov 	return ret;
3977eb1d1641SHerbert Xu }
3978eb1d1641SHerbert Xu 
br_multicast_query_expired(struct net_bridge_mcast * brmctx,struct bridge_mcast_own_query * query,struct bridge_mcast_querier * querier)3979adc47037SNikolay Aleksandrov static void br_multicast_query_expired(struct net_bridge_mcast *brmctx,
39802cd41431SLinus Lüssing 				       struct bridge_mcast_own_query *query,
39812cd41431SLinus Lüssing 				       struct bridge_mcast_querier *querier)
3982cc0fdd80SLinus Lüssing {
3983adc47037SNikolay Aleksandrov 	spin_lock(&brmctx->br->multicast_lock);
39844cdd0d10SNikolay Aleksandrov 	if (br_multicast_ctx_vlan_disabled(brmctx))
39854cdd0d10SNikolay Aleksandrov 		goto out;
39864cdd0d10SNikolay Aleksandrov 
3987adc47037SNikolay Aleksandrov 	if (query->startup_sent < brmctx->multicast_startup_query_count)
3988cc0fdd80SLinus Lüssing 		query->startup_sent++;
3989cc0fdd80SLinus Lüssing 
3990adc47037SNikolay Aleksandrov 	br_multicast_send_query(brmctx, NULL, query);
39914cdd0d10SNikolay Aleksandrov out:
3992adc47037SNikolay Aleksandrov 	spin_unlock(&brmctx->br->multicast_lock);
3993cc0fdd80SLinus Lüssing }
3994cc0fdd80SLinus Lüssing 
br_ip4_multicast_query_expired(struct timer_list * t)399588c1f37fSAllen Pais static void br_ip4_multicast_query_expired(struct timer_list *t)
3996eb1d1641SHerbert Xu {
3997d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = from_timer(brmctx, t,
3998d3d065c0SNikolay Aleksandrov 						     ip4_own_query.timer);
3999eb1d1641SHerbert Xu 
4000adc47037SNikolay Aleksandrov 	br_multicast_query_expired(brmctx, &brmctx->ip4_own_query,
4001d3d065c0SNikolay Aleksandrov 				   &brmctx->ip4_querier);
4002eb1d1641SHerbert Xu }
4003eb1d1641SHerbert Xu 
4004cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
br_ip6_multicast_query_expired(struct timer_list * t)400588c1f37fSAllen Pais static void br_ip6_multicast_query_expired(struct timer_list *t)
4006cc0fdd80SLinus Lüssing {
4007d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = from_timer(brmctx, t,
4008d3d065c0SNikolay Aleksandrov 						     ip6_own_query.timer);
4009cc0fdd80SLinus Lüssing 
4010adc47037SNikolay Aleksandrov 	br_multicast_query_expired(brmctx, &brmctx->ip6_own_query,
4011d3d065c0SNikolay Aleksandrov 				   &brmctx->ip6_querier);
4012cc0fdd80SLinus Lüssing }
4013cc0fdd80SLinus Lüssing #endif
4014cc0fdd80SLinus Lüssing 
br_multicast_gc_work(struct work_struct * work)4015e12cec65SNikolay Aleksandrov static void br_multicast_gc_work(struct work_struct *work)
40168b671779SNikolay Aleksandrov {
40178b671779SNikolay Aleksandrov 	struct net_bridge *br = container_of(work, struct net_bridge,
4018e12cec65SNikolay Aleksandrov 					     mcast_gc_work);
40198b671779SNikolay Aleksandrov 	HLIST_HEAD(deleted_head);
40208b671779SNikolay Aleksandrov 
40218b671779SNikolay Aleksandrov 	spin_lock_bh(&br->multicast_lock);
4022e12cec65SNikolay Aleksandrov 	hlist_move_list(&br->mcast_gc_list, &deleted_head);
40238b671779SNikolay Aleksandrov 	spin_unlock_bh(&br->multicast_lock);
40248b671779SNikolay Aleksandrov 
4025e12cec65SNikolay Aleksandrov 	br_multicast_gc(&deleted_head);
40268b671779SNikolay Aleksandrov }
40278b671779SNikolay Aleksandrov 
br_multicast_ctx_init(struct net_bridge * br,struct net_bridge_vlan * vlan,struct net_bridge_mcast * brmctx)4028613d61dbSNikolay Aleksandrov void br_multicast_ctx_init(struct net_bridge *br,
4029613d61dbSNikolay Aleksandrov 			   struct net_bridge_vlan *vlan,
4030613d61dbSNikolay Aleksandrov 			   struct net_bridge_mcast *brmctx)
4031613d61dbSNikolay Aleksandrov {
4032613d61dbSNikolay Aleksandrov 	brmctx->br = br;
4033613d61dbSNikolay Aleksandrov 	brmctx->vlan = vlan;
4034613d61dbSNikolay Aleksandrov 	brmctx->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
4035613d61dbSNikolay Aleksandrov 	brmctx->multicast_last_member_count = 2;
4036613d61dbSNikolay Aleksandrov 	brmctx->multicast_startup_query_count = 2;
4037613d61dbSNikolay Aleksandrov 
4038613d61dbSNikolay Aleksandrov 	brmctx->multicast_last_member_interval = HZ;
4039613d61dbSNikolay Aleksandrov 	brmctx->multicast_query_response_interval = 10 * HZ;
4040613d61dbSNikolay Aleksandrov 	brmctx->multicast_startup_query_interval = 125 * HZ / 4;
4041613d61dbSNikolay Aleksandrov 	brmctx->multicast_query_interval = 125 * HZ;
4042613d61dbSNikolay Aleksandrov 	brmctx->multicast_querier_interval = 255 * HZ;
4043613d61dbSNikolay Aleksandrov 	brmctx->multicast_membership_interval = 260 * HZ;
4044613d61dbSNikolay Aleksandrov 
4045bb18ef8eSNikolay Aleksandrov 	brmctx->ip4_querier.port_ifidx = 0;
4046f936bb42SThomas Gleixner 	seqcount_spinlock_init(&brmctx->ip4_querier.seq, &br->multicast_lock);
4047613d61dbSNikolay Aleksandrov 	brmctx->multicast_igmp_version = 2;
4048613d61dbSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
4049613d61dbSNikolay Aleksandrov 	brmctx->multicast_mld_version = 1;
4050bb18ef8eSNikolay Aleksandrov 	brmctx->ip6_querier.port_ifidx = 0;
4051f936bb42SThomas Gleixner 	seqcount_spinlock_init(&brmctx->ip6_querier.seq, &br->multicast_lock);
4052613d61dbSNikolay Aleksandrov #endif
4053613d61dbSNikolay Aleksandrov 
4054613d61dbSNikolay Aleksandrov 	timer_setup(&brmctx->ip4_mc_router_timer,
4055613d61dbSNikolay Aleksandrov 		    br_ip4_multicast_local_router_expired, 0);
4056613d61dbSNikolay Aleksandrov 	timer_setup(&brmctx->ip4_other_query.timer,
4057613d61dbSNikolay Aleksandrov 		    br_ip4_multicast_querier_expired, 0);
4058d99971ecSLinus Lüssing 	timer_setup(&brmctx->ip4_other_query.delay_timer,
4059d99971ecSLinus Lüssing 		    br_multicast_query_delay_expired, 0);
4060613d61dbSNikolay Aleksandrov 	timer_setup(&brmctx->ip4_own_query.timer,
4061613d61dbSNikolay Aleksandrov 		    br_ip4_multicast_query_expired, 0);
4062613d61dbSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
4063613d61dbSNikolay Aleksandrov 	timer_setup(&brmctx->ip6_mc_router_timer,
4064613d61dbSNikolay Aleksandrov 		    br_ip6_multicast_local_router_expired, 0);
4065613d61dbSNikolay Aleksandrov 	timer_setup(&brmctx->ip6_other_query.timer,
4066613d61dbSNikolay Aleksandrov 		    br_ip6_multicast_querier_expired, 0);
4067d99971ecSLinus Lüssing 	timer_setup(&brmctx->ip6_other_query.delay_timer,
4068d99971ecSLinus Lüssing 		    br_multicast_query_delay_expired, 0);
4069613d61dbSNikolay Aleksandrov 	timer_setup(&brmctx->ip6_own_query.timer,
4070613d61dbSNikolay Aleksandrov 		    br_ip6_multicast_query_expired, 0);
4071613d61dbSNikolay Aleksandrov #endif
4072613d61dbSNikolay Aleksandrov }
4073613d61dbSNikolay Aleksandrov 
br_multicast_ctx_deinit(struct net_bridge_mcast * brmctx)4074613d61dbSNikolay Aleksandrov void br_multicast_ctx_deinit(struct net_bridge_mcast *brmctx)
4075613d61dbSNikolay Aleksandrov {
4076613d61dbSNikolay Aleksandrov 	__br_multicast_stop(brmctx);
4077613d61dbSNikolay Aleksandrov }
4078613d61dbSNikolay Aleksandrov 
br_multicast_init(struct net_bridge * br)4079eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br)
4080eb1d1641SHerbert Xu {
4081d08c6bc0SNikolay Aleksandrov 	br->hash_max = BR_MULTICAST_DEFAULT_HASH_MAX;
4082eb1d1641SHerbert Xu 
4083613d61dbSNikolay Aleksandrov 	br_multicast_ctx_init(br, NULL, &br->multicast_ctx);
4084eb1d1641SHerbert Xu 
40856919622aSIdo Schimmel 	br_opt_toggle(br, BROPT_MULTICAST_ENABLED, true);
4086675779adSNikolay Aleksandrov 	br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true);
4087b00589afSLinus Lüssing 
4088eb1d1641SHerbert Xu 	spin_lock_init(&br->multicast_lock);
408919e3a9c9SNikolay Aleksandrov 	INIT_HLIST_HEAD(&br->mdb_list);
4090e12cec65SNikolay Aleksandrov 	INIT_HLIST_HEAD(&br->mcast_gc_list);
4091e12cec65SNikolay Aleksandrov 	INIT_WORK(&br->mcast_gc_work, br_multicast_gc_work);
4092eb1d1641SHerbert Xu }
4093eb1d1641SHerbert Xu 
br_ip4_multicast_join_snoopers(struct net_bridge * br)40944effd28cSLinus Lüssing static void br_ip4_multicast_join_snoopers(struct net_bridge *br)
40954effd28cSLinus Lüssing {
40964effd28cSLinus Lüssing 	struct in_device *in_dev = in_dev_get(br->dev);
40974effd28cSLinus Lüssing 
40984effd28cSLinus Lüssing 	if (!in_dev)
40994effd28cSLinus Lüssing 		return;
41004effd28cSLinus Lüssing 
41019fb20801SFlorian Fainelli 	__ip_mc_inc_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
41024effd28cSLinus Lüssing 	in_dev_put(in_dev);
41034effd28cSLinus Lüssing }
41044effd28cSLinus Lüssing 
41054effd28cSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
br_ip6_multicast_join_snoopers(struct net_bridge * br)41064effd28cSLinus Lüssing static void br_ip6_multicast_join_snoopers(struct net_bridge *br)
41074effd28cSLinus Lüssing {
41084effd28cSLinus Lüssing 	struct in6_addr addr;
41094effd28cSLinus Lüssing 
41104effd28cSLinus Lüssing 	ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
41114effd28cSLinus Lüssing 	ipv6_dev_mc_inc(br->dev, &addr);
41124effd28cSLinus Lüssing }
41134effd28cSLinus Lüssing #else
br_ip6_multicast_join_snoopers(struct net_bridge * br)41144effd28cSLinus Lüssing static inline void br_ip6_multicast_join_snoopers(struct net_bridge *br)
41154effd28cSLinus Lüssing {
41164effd28cSLinus Lüssing }
41174effd28cSLinus Lüssing #endif
41184effd28cSLinus Lüssing 
br_multicast_join_snoopers(struct net_bridge * br)4119851d0a73SJoseph Huang void br_multicast_join_snoopers(struct net_bridge *br)
41204effd28cSLinus Lüssing {
41214effd28cSLinus Lüssing 	br_ip4_multicast_join_snoopers(br);
41224effd28cSLinus Lüssing 	br_ip6_multicast_join_snoopers(br);
41234effd28cSLinus Lüssing }
41244effd28cSLinus Lüssing 
br_ip4_multicast_leave_snoopers(struct net_bridge * br)41254effd28cSLinus Lüssing static void br_ip4_multicast_leave_snoopers(struct net_bridge *br)
41264effd28cSLinus Lüssing {
41274effd28cSLinus Lüssing 	struct in_device *in_dev = in_dev_get(br->dev);
41284effd28cSLinus Lüssing 
41294effd28cSLinus Lüssing 	if (WARN_ON(!in_dev))
41304effd28cSLinus Lüssing 		return;
41314effd28cSLinus Lüssing 
41329fb20801SFlorian Fainelli 	__ip_mc_dec_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
41334effd28cSLinus Lüssing 	in_dev_put(in_dev);
41344effd28cSLinus Lüssing }
41354effd28cSLinus Lüssing 
41364effd28cSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
br_ip6_multicast_leave_snoopers(struct net_bridge * br)41374effd28cSLinus Lüssing static void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
41384effd28cSLinus Lüssing {
41394effd28cSLinus Lüssing 	struct in6_addr addr;
41404effd28cSLinus Lüssing 
41414effd28cSLinus Lüssing 	ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
41424effd28cSLinus Lüssing 	ipv6_dev_mc_dec(br->dev, &addr);
41434effd28cSLinus Lüssing }
41444effd28cSLinus Lüssing #else
br_ip6_multicast_leave_snoopers(struct net_bridge * br)41454effd28cSLinus Lüssing static inline void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
41464effd28cSLinus Lüssing {
41474effd28cSLinus Lüssing }
41484effd28cSLinus Lüssing #endif
41494effd28cSLinus Lüssing 
br_multicast_leave_snoopers(struct net_bridge * br)4150851d0a73SJoseph Huang void br_multicast_leave_snoopers(struct net_bridge *br)
41514effd28cSLinus Lüssing {
41524effd28cSLinus Lüssing 	br_ip4_multicast_leave_snoopers(br);
41534effd28cSLinus Lüssing 	br_ip6_multicast_leave_snoopers(br);
41544effd28cSLinus Lüssing }
41554effd28cSLinus Lüssing 
__br_multicast_open_query(struct net_bridge * br,struct bridge_mcast_own_query * query)41567b54aaafSNikolay Aleksandrov static void __br_multicast_open_query(struct net_bridge *br,
415790010b36SLinus Lüssing 				      struct bridge_mcast_own_query *query)
4158eb1d1641SHerbert Xu {
4159cc0fdd80SLinus Lüssing 	query->startup_sent = 0;
4160eb1d1641SHerbert Xu 
416113cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
4162eb1d1641SHerbert Xu 		return;
4163eb1d1641SHerbert Xu 
4164cc0fdd80SLinus Lüssing 	mod_timer(&query->timer, jiffies);
4165cc0fdd80SLinus Lüssing }
4166cc0fdd80SLinus Lüssing 
__br_multicast_open(struct net_bridge_mcast * brmctx)41677b54aaafSNikolay Aleksandrov static void __br_multicast_open(struct net_bridge_mcast *brmctx)
41687b54aaafSNikolay Aleksandrov {
41697b54aaafSNikolay Aleksandrov 	__br_multicast_open_query(brmctx->br, &brmctx->ip4_own_query);
41707b54aaafSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
41717b54aaafSNikolay Aleksandrov 	__br_multicast_open_query(brmctx->br, &brmctx->ip6_own_query);
41727b54aaafSNikolay Aleksandrov #endif
41737b54aaafSNikolay Aleksandrov }
41747b54aaafSNikolay Aleksandrov 
br_multicast_open(struct net_bridge * br)4175cc0fdd80SLinus Lüssing void br_multicast_open(struct net_bridge *br)
4176cc0fdd80SLinus Lüssing {
4177f4b7002aSNikolay Aleksandrov 	ASSERT_RTNL();
4178f4b7002aSNikolay Aleksandrov 
4179f4b7002aSNikolay Aleksandrov 	if (br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED)) {
41807b54aaafSNikolay Aleksandrov 		struct net_bridge_vlan_group *vg;
41817b54aaafSNikolay Aleksandrov 		struct net_bridge_vlan *vlan;
41827b54aaafSNikolay Aleksandrov 
41837b54aaafSNikolay Aleksandrov 		vg = br_vlan_group(br);
41847b54aaafSNikolay Aleksandrov 		if (vg) {
41857b54aaafSNikolay Aleksandrov 			list_for_each_entry(vlan, &vg->vlan_list, vlist) {
41867b54aaafSNikolay Aleksandrov 				struct net_bridge_mcast *brmctx;
41877b54aaafSNikolay Aleksandrov 
41887b54aaafSNikolay Aleksandrov 				brmctx = &vlan->br_mcast_ctx;
41897b54aaafSNikolay Aleksandrov 				if (br_vlan_is_brentry(vlan) &&
41907b54aaafSNikolay Aleksandrov 				    !br_multicast_ctx_vlan_disabled(brmctx))
41917b54aaafSNikolay Aleksandrov 					__br_multicast_open(&vlan->br_mcast_ctx);
41927b54aaafSNikolay Aleksandrov 			}
41937b54aaafSNikolay Aleksandrov 		}
4194cb486ce9SNikolay Aleksandrov 	} else {
41957b54aaafSNikolay Aleksandrov 		__br_multicast_open(&br->multicast_ctx);
4196eb1d1641SHerbert Xu 	}
4197cb486ce9SNikolay Aleksandrov }
4198eb1d1641SHerbert Xu 
__br_multicast_stop(struct net_bridge_mcast * brmctx)4199613d61dbSNikolay Aleksandrov static void __br_multicast_stop(struct net_bridge_mcast *brmctx)
4200613d61dbSNikolay Aleksandrov {
4201613d61dbSNikolay Aleksandrov 	del_timer_sync(&brmctx->ip4_mc_router_timer);
4202613d61dbSNikolay Aleksandrov 	del_timer_sync(&brmctx->ip4_other_query.timer);
4203d99971ecSLinus Lüssing 	del_timer_sync(&brmctx->ip4_other_query.delay_timer);
4204613d61dbSNikolay Aleksandrov 	del_timer_sync(&brmctx->ip4_own_query.timer);
4205613d61dbSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
4206613d61dbSNikolay Aleksandrov 	del_timer_sync(&brmctx->ip6_mc_router_timer);
4207613d61dbSNikolay Aleksandrov 	del_timer_sync(&brmctx->ip6_other_query.timer);
4208d99971ecSLinus Lüssing 	del_timer_sync(&brmctx->ip6_other_query.delay_timer);
4209613d61dbSNikolay Aleksandrov 	del_timer_sync(&brmctx->ip6_own_query.timer);
4210613d61dbSNikolay Aleksandrov #endif
4211613d61dbSNikolay Aleksandrov }
4212613d61dbSNikolay Aleksandrov 
br_multicast_toggle_one_vlan(struct net_bridge_vlan * vlan,bool on)42137b54aaafSNikolay Aleksandrov void br_multicast_toggle_one_vlan(struct net_bridge_vlan *vlan, bool on)
42147b54aaafSNikolay Aleksandrov {
42157b54aaafSNikolay Aleksandrov 	struct net_bridge *br;
42167b54aaafSNikolay Aleksandrov 
42177b54aaafSNikolay Aleksandrov 	/* it's okay to check for the flag without the multicast lock because it
42187b54aaafSNikolay Aleksandrov 	 * can only change under RTNL -> multicast_lock, we need the latter to
42197b54aaafSNikolay Aleksandrov 	 * sync with timers and packets
42207b54aaafSNikolay Aleksandrov 	 */
42217b54aaafSNikolay Aleksandrov 	if (on == !!(vlan->priv_flags & BR_VLFLAG_MCAST_ENABLED))
42227b54aaafSNikolay Aleksandrov 		return;
42237b54aaafSNikolay Aleksandrov 
42247b54aaafSNikolay Aleksandrov 	if (br_vlan_is_master(vlan)) {
42257b54aaafSNikolay Aleksandrov 		br = vlan->br;
42267b54aaafSNikolay Aleksandrov 
42277b54aaafSNikolay Aleksandrov 		if (!br_vlan_is_brentry(vlan) ||
42287b54aaafSNikolay Aleksandrov 		    (on &&
42297b54aaafSNikolay Aleksandrov 		     br_multicast_ctx_vlan_global_disabled(&vlan->br_mcast_ctx)))
42307b54aaafSNikolay Aleksandrov 			return;
42317b54aaafSNikolay Aleksandrov 
42327b54aaafSNikolay Aleksandrov 		spin_lock_bh(&br->multicast_lock);
42337b54aaafSNikolay Aleksandrov 		vlan->priv_flags ^= BR_VLFLAG_MCAST_ENABLED;
42347b54aaafSNikolay Aleksandrov 		spin_unlock_bh(&br->multicast_lock);
42357b54aaafSNikolay Aleksandrov 
42367b54aaafSNikolay Aleksandrov 		if (on)
42377b54aaafSNikolay Aleksandrov 			__br_multicast_open(&vlan->br_mcast_ctx);
42387b54aaafSNikolay Aleksandrov 		else
42397b54aaafSNikolay Aleksandrov 			__br_multicast_stop(&vlan->br_mcast_ctx);
42407b54aaafSNikolay Aleksandrov 	} else {
42417b54aaafSNikolay Aleksandrov 		struct net_bridge_mcast *brmctx;
42427b54aaafSNikolay Aleksandrov 
42437b54aaafSNikolay Aleksandrov 		brmctx = br_multicast_port_ctx_get_global(&vlan->port_mcast_ctx);
42447b54aaafSNikolay Aleksandrov 		if (on && br_multicast_ctx_vlan_global_disabled(brmctx))
42457b54aaafSNikolay Aleksandrov 			return;
42467b54aaafSNikolay Aleksandrov 
42477b54aaafSNikolay Aleksandrov 		br = vlan->port->br;
42487b54aaafSNikolay Aleksandrov 		spin_lock_bh(&br->multicast_lock);
42497b54aaafSNikolay Aleksandrov 		vlan->priv_flags ^= BR_VLFLAG_MCAST_ENABLED;
42507b54aaafSNikolay Aleksandrov 		if (on)
42517b54aaafSNikolay Aleksandrov 			__br_multicast_enable_port_ctx(&vlan->port_mcast_ctx);
42527b54aaafSNikolay Aleksandrov 		else
42537b54aaafSNikolay Aleksandrov 			__br_multicast_disable_port_ctx(&vlan->port_mcast_ctx);
42547b54aaafSNikolay Aleksandrov 		spin_unlock_bh(&br->multicast_lock);
42557b54aaafSNikolay Aleksandrov 	}
42567b54aaafSNikolay Aleksandrov }
42577b54aaafSNikolay Aleksandrov 
br_multicast_toggle_vlan(struct net_bridge_vlan * vlan,bool on)4258affce9a7SNikolay Aleksandrov static void br_multicast_toggle_vlan(struct net_bridge_vlan *vlan, bool on)
4259f4b7002aSNikolay Aleksandrov {
4260f4b7002aSNikolay Aleksandrov 	struct net_bridge_port *p;
4261f4b7002aSNikolay Aleksandrov 
4262f4b7002aSNikolay Aleksandrov 	if (WARN_ON_ONCE(!br_vlan_is_master(vlan)))
4263f4b7002aSNikolay Aleksandrov 		return;
4264f4b7002aSNikolay Aleksandrov 
4265f4b7002aSNikolay Aleksandrov 	list_for_each_entry(p, &vlan->br->port_list, list) {
4266f4b7002aSNikolay Aleksandrov 		struct net_bridge_vlan *vport;
4267f4b7002aSNikolay Aleksandrov 
4268f4b7002aSNikolay Aleksandrov 		vport = br_vlan_find(nbp_vlan_group(p), vlan->vid);
4269f4b7002aSNikolay Aleksandrov 		if (!vport)
4270f4b7002aSNikolay Aleksandrov 			continue;
4271f4b7002aSNikolay Aleksandrov 		br_multicast_toggle_one_vlan(vport, on);
4272f4b7002aSNikolay Aleksandrov 	}
4273affce9a7SNikolay Aleksandrov 
4274affce9a7SNikolay Aleksandrov 	if (br_vlan_is_brentry(vlan))
4275affce9a7SNikolay Aleksandrov 		br_multicast_toggle_one_vlan(vlan, on);
4276f4b7002aSNikolay Aleksandrov }
4277f4b7002aSNikolay Aleksandrov 
br_multicast_toggle_vlan_snooping(struct net_bridge * br,bool on,struct netlink_ext_ack * extack)4278f4b7002aSNikolay Aleksandrov int br_multicast_toggle_vlan_snooping(struct net_bridge *br, bool on,
4279f4b7002aSNikolay Aleksandrov 				      struct netlink_ext_ack *extack)
4280eb1d1641SHerbert Xu {
42817b54aaafSNikolay Aleksandrov 	struct net_bridge_vlan_group *vg;
42827b54aaafSNikolay Aleksandrov 	struct net_bridge_vlan *vlan;
4283f4b7002aSNikolay Aleksandrov 	struct net_bridge_port *p;
42847b54aaafSNikolay Aleksandrov 
4285f4b7002aSNikolay Aleksandrov 	if (br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED) == on)
4286f4b7002aSNikolay Aleksandrov 		return 0;
4287f4b7002aSNikolay Aleksandrov 
4288f4b7002aSNikolay Aleksandrov 	if (on && !br_opt_get(br, BROPT_VLAN_ENABLED)) {
4289f4b7002aSNikolay Aleksandrov 		NL_SET_ERR_MSG_MOD(extack, "Cannot enable multicast vlan snooping with vlan filtering disabled");
4290f4b7002aSNikolay Aleksandrov 		return -EINVAL;
4291f4b7002aSNikolay Aleksandrov 	}
4292f4b7002aSNikolay Aleksandrov 
4293f4b7002aSNikolay Aleksandrov 	vg = br_vlan_group(br);
4294f4b7002aSNikolay Aleksandrov 	if (!vg)
4295f4b7002aSNikolay Aleksandrov 		return 0;
4296f4b7002aSNikolay Aleksandrov 
4297f4b7002aSNikolay Aleksandrov 	br_opt_toggle(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED, on);
4298f4b7002aSNikolay Aleksandrov 
4299f4b7002aSNikolay Aleksandrov 	/* disable/enable non-vlan mcast contexts based on vlan snooping */
4300f4b7002aSNikolay Aleksandrov 	if (on)
4301f4b7002aSNikolay Aleksandrov 		__br_multicast_stop(&br->multicast_ctx);
4302f4b7002aSNikolay Aleksandrov 	else
4303f4b7002aSNikolay Aleksandrov 		__br_multicast_open(&br->multicast_ctx);
4304f4b7002aSNikolay Aleksandrov 	list_for_each_entry(p, &br->port_list, list) {
4305f4b7002aSNikolay Aleksandrov 		if (on)
4306f4b7002aSNikolay Aleksandrov 			br_multicast_disable_port(p);
4307f4b7002aSNikolay Aleksandrov 		else
4308f4b7002aSNikolay Aleksandrov 			br_multicast_enable_port(p);
4309f4b7002aSNikolay Aleksandrov 	}
4310f4b7002aSNikolay Aleksandrov 
4311f4b7002aSNikolay Aleksandrov 	list_for_each_entry(vlan, &vg->vlan_list, vlist)
4312f4b7002aSNikolay Aleksandrov 		br_multicast_toggle_vlan(vlan, on);
4313f4b7002aSNikolay Aleksandrov 
4314f4b7002aSNikolay Aleksandrov 	return 0;
4315f4b7002aSNikolay Aleksandrov }
4316f4b7002aSNikolay Aleksandrov 
br_multicast_toggle_global_vlan(struct net_bridge_vlan * vlan,bool on)43179dee572cSNikolay Aleksandrov bool br_multicast_toggle_global_vlan(struct net_bridge_vlan *vlan, bool on)
43189dee572cSNikolay Aleksandrov {
43199dee572cSNikolay Aleksandrov 	ASSERT_RTNL();
43209dee572cSNikolay Aleksandrov 
43219dee572cSNikolay Aleksandrov 	/* BR_VLFLAG_GLOBAL_MCAST_ENABLED relies on eventual consistency and
43229dee572cSNikolay Aleksandrov 	 * requires only RTNL to change
43239dee572cSNikolay Aleksandrov 	 */
43249dee572cSNikolay Aleksandrov 	if (on == !!(vlan->priv_flags & BR_VLFLAG_GLOBAL_MCAST_ENABLED))
43259dee572cSNikolay Aleksandrov 		return false;
43269dee572cSNikolay Aleksandrov 
43279dee572cSNikolay Aleksandrov 	vlan->priv_flags ^= BR_VLFLAG_GLOBAL_MCAST_ENABLED;
43289dee572cSNikolay Aleksandrov 	br_multicast_toggle_vlan(vlan, on);
43299dee572cSNikolay Aleksandrov 
43309dee572cSNikolay Aleksandrov 	return true;
43319dee572cSNikolay Aleksandrov }
43329dee572cSNikolay Aleksandrov 
br_multicast_stop(struct net_bridge * br)4333f4b7002aSNikolay Aleksandrov void br_multicast_stop(struct net_bridge *br)
4334f4b7002aSNikolay Aleksandrov {
43357b54aaafSNikolay Aleksandrov 	ASSERT_RTNL();
43367b54aaafSNikolay Aleksandrov 
4337f4b7002aSNikolay Aleksandrov 	if (br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED)) {
4338f4b7002aSNikolay Aleksandrov 		struct net_bridge_vlan_group *vg;
4339f4b7002aSNikolay Aleksandrov 		struct net_bridge_vlan *vlan;
4340f4b7002aSNikolay Aleksandrov 
43417b54aaafSNikolay Aleksandrov 		vg = br_vlan_group(br);
43427b54aaafSNikolay Aleksandrov 		if (vg) {
43437b54aaafSNikolay Aleksandrov 			list_for_each_entry(vlan, &vg->vlan_list, vlist) {
43447b54aaafSNikolay Aleksandrov 				struct net_bridge_mcast *brmctx;
43457b54aaafSNikolay Aleksandrov 
43467b54aaafSNikolay Aleksandrov 				brmctx = &vlan->br_mcast_ctx;
43477b54aaafSNikolay Aleksandrov 				if (br_vlan_is_brentry(vlan) &&
43487b54aaafSNikolay Aleksandrov 				    !br_multicast_ctx_vlan_disabled(brmctx))
43497b54aaafSNikolay Aleksandrov 					__br_multicast_stop(&vlan->br_mcast_ctx);
43507b54aaafSNikolay Aleksandrov 			}
43517b54aaafSNikolay Aleksandrov 		}
4352cb486ce9SNikolay Aleksandrov 	} else {
4353613d61dbSNikolay Aleksandrov 		__br_multicast_stop(&br->multicast_ctx);
4354e10177abSSatish Ashok 	}
4355cb486ce9SNikolay Aleksandrov }
4356e10177abSSatish Ashok 
br_multicast_dev_del(struct net_bridge * br)4357e10177abSSatish Ashok void br_multicast_dev_del(struct net_bridge *br)
4358e10177abSSatish Ashok {
4359e10177abSSatish Ashok 	struct net_bridge_mdb_entry *mp;
43608b671779SNikolay Aleksandrov 	HLIST_HEAD(deleted_head);
436119e3a9c9SNikolay Aleksandrov 	struct hlist_node *tmp;
4362eb1d1641SHerbert Xu 
4363eb1d1641SHerbert Xu 	spin_lock_bh(&br->multicast_lock);
4364e12cec65SNikolay Aleksandrov 	hlist_for_each_entry_safe(mp, tmp, &br->mdb_list, mdb_node)
4365e12cec65SNikolay Aleksandrov 		br_multicast_del_mdb_entry(mp);
4366e12cec65SNikolay Aleksandrov 	hlist_move_list(&br->mcast_gc_list, &deleted_head);
4367eb1d1641SHerbert Xu 	spin_unlock_bh(&br->multicast_lock);
436819e3a9c9SNikolay Aleksandrov 
4369613d61dbSNikolay Aleksandrov 	br_multicast_ctx_deinit(&br->multicast_ctx);
4370e12cec65SNikolay Aleksandrov 	br_multicast_gc(&deleted_head);
4371e12cec65SNikolay Aleksandrov 	cancel_work_sync(&br->mcast_gc_work);
43728b671779SNikolay Aleksandrov 
43734329596cSNikolay Aleksandrov 	rcu_barrier();
4374eb1d1641SHerbert Xu }
43750909e117SHerbert Xu 
br_multicast_set_router(struct net_bridge_mcast * brmctx,unsigned long val)4376a97df080SNikolay Aleksandrov int br_multicast_set_router(struct net_bridge_mcast *brmctx, unsigned long val)
43770909e117SHerbert Xu {
43786ae4ae8eSLinus Lüssing 	int err = -EINVAL;
43790909e117SHerbert Xu 
4380a97df080SNikolay Aleksandrov 	spin_lock_bh(&brmctx->br->multicast_lock);
43810909e117SHerbert Xu 
43820909e117SHerbert Xu 	switch (val) {
43837f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_DISABLED:
43847f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_PERM:
4385a97df080SNikolay Aleksandrov 		br_mc_router_state_change(brmctx->br, val == MDB_RTR_TYPE_PERM);
4386d3d065c0SNikolay Aleksandrov 		del_timer(&brmctx->ip4_mc_router_timer);
4387a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
4388d3d065c0SNikolay Aleksandrov 		del_timer(&brmctx->ip6_mc_router_timer);
4389a3c02e76SLinus Lüssing #endif
4390d3d065c0SNikolay Aleksandrov 		brmctx->multicast_router = val;
439177041420SYotam Gigi 		err = 0;
439277041420SYotam Gigi 		break;
43937f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP_QUERY:
4394d3d065c0SNikolay Aleksandrov 		if (brmctx->multicast_router != MDB_RTR_TYPE_TEMP_QUERY)
4395a97df080SNikolay Aleksandrov 			br_mc_router_state_change(brmctx->br, false);
4396d3d065c0SNikolay Aleksandrov 		brmctx->multicast_router = val;
43970909e117SHerbert Xu 		err = 0;
43980909e117SHerbert Xu 		break;
43990909e117SHerbert Xu 	}
44000909e117SHerbert Xu 
4401a97df080SNikolay Aleksandrov 	spin_unlock_bh(&brmctx->br->multicast_lock);
44020909e117SHerbert Xu 
44030909e117SHerbert Xu 	return err;
44040909e117SHerbert Xu }
44050909e117SHerbert Xu 
4406ed2d3597SLinus Lüssing static void
br_multicast_rport_del_notify(struct net_bridge_mcast_port * pmctx,bool deleted)4407adc47037SNikolay Aleksandrov br_multicast_rport_del_notify(struct net_bridge_mcast_port *pmctx, bool deleted)
44087f0aec7aSNikolay Aleksandrov {
4409ed2d3597SLinus Lüssing 	if (!deleted)
44107f0aec7aSNikolay Aleksandrov 		return;
4411ed2d3597SLinus Lüssing 
4412a3c02e76SLinus Lüssing 	/* For backwards compatibility for now, only notify if there is
4413a3c02e76SLinus Lüssing 	 * no multicast router anymore for both IPv4 and IPv6.
4414a3c02e76SLinus Lüssing 	 */
4415adc47037SNikolay Aleksandrov 	if (!hlist_unhashed(&pmctx->ip4_rlist))
4416a3c02e76SLinus Lüssing 		return;
4417a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
4418adc47037SNikolay Aleksandrov 	if (!hlist_unhashed(&pmctx->ip6_rlist))
4419a3c02e76SLinus Lüssing 		return;
4420a3c02e76SLinus Lüssing #endif
4421a3c02e76SLinus Lüssing 
44221e9ca456SNikolay Aleksandrov 	br_rtr_notify(pmctx->port->br->dev, pmctx, RTM_DELMDB);
4423adc47037SNikolay Aleksandrov 	br_port_mc_router_state_change(pmctx->port, false);
4424f12e7d95SNogah Frankel 
4425f12e7d95SNogah Frankel 	/* don't allow timer refresh */
4426adc47037SNikolay Aleksandrov 	if (pmctx->multicast_router == MDB_RTR_TYPE_TEMP)
4427adc47037SNikolay Aleksandrov 		pmctx->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
44287f0aec7aSNikolay Aleksandrov }
44297f0aec7aSNikolay Aleksandrov 
br_multicast_set_port_router(struct net_bridge_mcast_port * pmctx,unsigned long val)4430a53581d5SNikolay Aleksandrov int br_multicast_set_port_router(struct net_bridge_mcast_port *pmctx,
4431a53581d5SNikolay Aleksandrov 				 unsigned long val)
44320909e117SHerbert Xu {
4433a53581d5SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx;
4434a55d8246SNikolay Aleksandrov 	unsigned long now = jiffies;
44356ae4ae8eSLinus Lüssing 	int err = -EINVAL;
4436ed2d3597SLinus Lüssing 	bool del = false;
44370909e117SHerbert Xu 
4438a53581d5SNikolay Aleksandrov 	brmctx = br_multicast_port_ctx_get_global(pmctx);
4439ddd0d529SNikolay Aleksandrov 	spin_lock_bh(&brmctx->br->multicast_lock);
4440adc47037SNikolay Aleksandrov 	if (pmctx->multicast_router == val) {
4441a55d8246SNikolay Aleksandrov 		/* Refresh the temp router port timer */
4442adc47037SNikolay Aleksandrov 		if (pmctx->multicast_router == MDB_RTR_TYPE_TEMP) {
4443adc47037SNikolay Aleksandrov 			mod_timer(&pmctx->ip4_mc_router_timer,
4444d3d065c0SNikolay Aleksandrov 				  now + brmctx->multicast_querier_interval);
4445a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
4446adc47037SNikolay Aleksandrov 			mod_timer(&pmctx->ip6_mc_router_timer,
4447d3d065c0SNikolay Aleksandrov 				  now + brmctx->multicast_querier_interval);
4448a3c02e76SLinus Lüssing #endif
4449a3c02e76SLinus Lüssing 		}
44504950cfd1SNikolay Aleksandrov 		err = 0;
44514950cfd1SNikolay Aleksandrov 		goto unlock;
44524950cfd1SNikolay Aleksandrov 	}
44530909e117SHerbert Xu 	switch (val) {
44547f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_DISABLED:
4455adc47037SNikolay Aleksandrov 		pmctx->multicast_router = MDB_RTR_TYPE_DISABLED;
4456adc47037SNikolay Aleksandrov 		del |= br_ip4_multicast_rport_del(pmctx);
4457adc47037SNikolay Aleksandrov 		del_timer(&pmctx->ip4_mc_router_timer);
4458adc47037SNikolay Aleksandrov 		del |= br_ip6_multicast_rport_del(pmctx);
4459a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
4460adc47037SNikolay Aleksandrov 		del_timer(&pmctx->ip6_mc_router_timer);
4461a3c02e76SLinus Lüssing #endif
4462adc47037SNikolay Aleksandrov 		br_multicast_rport_del_notify(pmctx, del);
44630909e117SHerbert Xu 		break;
44647f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP_QUERY:
4465adc47037SNikolay Aleksandrov 		pmctx->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
4466adc47037SNikolay Aleksandrov 		del |= br_ip4_multicast_rport_del(pmctx);
4467adc47037SNikolay Aleksandrov 		del |= br_ip6_multicast_rport_del(pmctx);
4468adc47037SNikolay Aleksandrov 		br_multicast_rport_del_notify(pmctx, del);
44697f0aec7aSNikolay Aleksandrov 		break;
44707f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_PERM:
4471adc47037SNikolay Aleksandrov 		pmctx->multicast_router = MDB_RTR_TYPE_PERM;
4472adc47037SNikolay Aleksandrov 		del_timer(&pmctx->ip4_mc_router_timer);
4473adc47037SNikolay Aleksandrov 		br_ip4_multicast_add_router(brmctx, pmctx);
4474a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
4475adc47037SNikolay Aleksandrov 		del_timer(&pmctx->ip6_mc_router_timer);
4476a3c02e76SLinus Lüssing #endif
4477adc47037SNikolay Aleksandrov 		br_ip6_multicast_add_router(brmctx, pmctx);
44780909e117SHerbert Xu 		break;
4479a55d8246SNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP:
4480adc47037SNikolay Aleksandrov 		pmctx->multicast_router = MDB_RTR_TYPE_TEMP;
4481adc47037SNikolay Aleksandrov 		br_ip4_multicast_mark_router(brmctx, pmctx);
4482adc47037SNikolay Aleksandrov 		br_ip6_multicast_mark_router(brmctx, pmctx);
4483a55d8246SNikolay Aleksandrov 		break;
44847f0aec7aSNikolay Aleksandrov 	default:
44857f0aec7aSNikolay Aleksandrov 		goto unlock;
44860909e117SHerbert Xu 	}
44877f0aec7aSNikolay Aleksandrov 	err = 0;
44887f0aec7aSNikolay Aleksandrov unlock:
4489ddd0d529SNikolay Aleksandrov 	spin_unlock_bh(&brmctx->br->multicast_lock);
44900909e117SHerbert Xu 
44910909e117SHerbert Xu 	return err;
44920909e117SHerbert Xu }
4493561f1103SHerbert Xu 
br_multicast_set_vlan_router(struct net_bridge_vlan * v,u8 mcast_router)44942796d846SNikolay Aleksandrov int br_multicast_set_vlan_router(struct net_bridge_vlan *v, u8 mcast_router)
44952796d846SNikolay Aleksandrov {
44962796d846SNikolay Aleksandrov 	int err;
44972796d846SNikolay Aleksandrov 
44982796d846SNikolay Aleksandrov 	if (br_vlan_is_master(v))
44992796d846SNikolay Aleksandrov 		err = br_multicast_set_router(&v->br_mcast_ctx, mcast_router);
45002796d846SNikolay Aleksandrov 	else
45012796d846SNikolay Aleksandrov 		err = br_multicast_set_port_router(&v->port_mcast_ctx,
45022796d846SNikolay Aleksandrov 						   mcast_router);
45032796d846SNikolay Aleksandrov 
45042796d846SNikolay Aleksandrov 	return err;
45052796d846SNikolay Aleksandrov }
45062796d846SNikolay Aleksandrov 
br_multicast_start_querier(struct net_bridge_mcast * brmctx,struct bridge_mcast_own_query * query)4507adc47037SNikolay Aleksandrov static void br_multicast_start_querier(struct net_bridge_mcast *brmctx,
450890010b36SLinus Lüssing 				       struct bridge_mcast_own_query *query)
4509561f1103SHerbert Xu {
4510561f1103SHerbert Xu 	struct net_bridge_port *port;
451174857216SHerbert Xu 
4512cb486ce9SNikolay Aleksandrov 	if (!br_multicast_ctx_matches_vlan_snooping(brmctx))
4513cb486ce9SNikolay Aleksandrov 		return;
4514cb486ce9SNikolay Aleksandrov 
45157b54aaafSNikolay Aleksandrov 	__br_multicast_open_query(brmctx->br, query);
451674857216SHerbert Xu 
4517c5b493ceSNikolay Aleksandrov 	rcu_read_lock();
4518adc47037SNikolay Aleksandrov 	list_for_each_entry_rcu(port, &brmctx->br->port_list, list) {
4519615cc23eSNikolay Aleksandrov 		struct bridge_mcast_own_query *ip4_own_query;
4520615cc23eSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
4521615cc23eSNikolay Aleksandrov 		struct bridge_mcast_own_query *ip6_own_query;
4522615cc23eSNikolay Aleksandrov #endif
4523615cc23eSNikolay Aleksandrov 
4524615cc23eSNikolay Aleksandrov 		if (br_multicast_port_ctx_state_stopped(&port->multicast_ctx))
452574857216SHerbert Xu 			continue;
452674857216SHerbert Xu 
4527615cc23eSNikolay Aleksandrov 		if (br_multicast_ctx_is_vlan(brmctx)) {
4528615cc23eSNikolay Aleksandrov 			struct net_bridge_vlan *vlan;
4529615cc23eSNikolay Aleksandrov 
45303f0d14efSNikolay Aleksandrov 			vlan = br_vlan_find(nbp_vlan_group_rcu(port),
45313f0d14efSNikolay Aleksandrov 					    brmctx->vlan->vid);
4532615cc23eSNikolay Aleksandrov 			if (!vlan ||
4533615cc23eSNikolay Aleksandrov 			    br_multicast_port_ctx_state_stopped(&vlan->port_mcast_ctx))
4534615cc23eSNikolay Aleksandrov 				continue;
4535615cc23eSNikolay Aleksandrov 
4536615cc23eSNikolay Aleksandrov 			ip4_own_query = &vlan->port_mcast_ctx.ip4_own_query;
4537615cc23eSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
4538615cc23eSNikolay Aleksandrov 			ip6_own_query = &vlan->port_mcast_ctx.ip6_own_query;
4539615cc23eSNikolay Aleksandrov #endif
4540615cc23eSNikolay Aleksandrov 		} else {
4541615cc23eSNikolay Aleksandrov 			ip4_own_query = &port->multicast_ctx.ip4_own_query;
4542615cc23eSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
4543615cc23eSNikolay Aleksandrov 			ip6_own_query = &port->multicast_ctx.ip6_own_query;
4544615cc23eSNikolay Aleksandrov #endif
4545615cc23eSNikolay Aleksandrov 		}
4546615cc23eSNikolay Aleksandrov 
4547adc47037SNikolay Aleksandrov 		if (query == &brmctx->ip4_own_query)
4548615cc23eSNikolay Aleksandrov 			br_multicast_enable(ip4_own_query);
4549cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
4550cc0fdd80SLinus Lüssing 		else
4551615cc23eSNikolay Aleksandrov 			br_multicast_enable(ip6_own_query);
4552cc0fdd80SLinus Lüssing #endif
455374857216SHerbert Xu 	}
4554c5b493ceSNikolay Aleksandrov 	rcu_read_unlock();
455574857216SHerbert Xu }
455674857216SHerbert Xu 
br_multicast_toggle(struct net_bridge * br,unsigned long val,struct netlink_ext_ack * extack)4557ae1ea84bSFlorian Fainelli int br_multicast_toggle(struct net_bridge *br, unsigned long val,
4558ae1ea84bSFlorian Fainelli 			struct netlink_ext_ack *extack)
455974857216SHerbert Xu {
45607cb3f921SNikolay Aleksandrov 	struct net_bridge_port *port;
4561851d0a73SJoseph Huang 	bool change_snoopers = false;
4562ae1ea84bSFlorian Fainelli 	int err = 0;
4563561f1103SHerbert Xu 
4564ef5e0d82SAndrey Vagin 	spin_lock_bh(&br->multicast_lock);
456513cefad2SNikolay Aleksandrov 	if (!!br_opt_get(br, BROPT_MULTICAST_ENABLED) == !!val)
4566561f1103SHerbert Xu 		goto unlock;
4567561f1103SHerbert Xu 
4568ae1ea84bSFlorian Fainelli 	err = br_mc_disabled_update(br->dev, val, extack);
4569ae1ea84bSFlorian Fainelli 	if (err == -EOPNOTSUPP)
4570ae1ea84bSFlorian Fainelli 		err = 0;
4571ae1ea84bSFlorian Fainelli 	if (err)
4572ae1ea84bSFlorian Fainelli 		goto unlock;
4573ae1ea84bSFlorian Fainelli 
457413cefad2SNikolay Aleksandrov 	br_opt_toggle(br, BROPT_MULTICAST_ENABLED, !!val);
45754effd28cSLinus Lüssing 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) {
4576851d0a73SJoseph Huang 		change_snoopers = true;
4577561f1103SHerbert Xu 		goto unlock;
45784effd28cSLinus Lüssing 	}
4579561f1103SHerbert Xu 
45803a7fda06SHerbert Xu 	if (!netif_running(br->dev))
45813a7fda06SHerbert Xu 		goto unlock;
45823a7fda06SHerbert Xu 
45837cb3f921SNikolay Aleksandrov 	br_multicast_open(br);
45847cb3f921SNikolay Aleksandrov 	list_for_each_entry(port, &br->port_list, list)
4585adc47037SNikolay Aleksandrov 		__br_multicast_enable_port_ctx(&port->multicast_ctx);
4586561f1103SHerbert Xu 
4587851d0a73SJoseph Huang 	change_snoopers = true;
4588851d0a73SJoseph Huang 
4589561f1103SHerbert Xu unlock:
4590ef5e0d82SAndrey Vagin 	spin_unlock_bh(&br->multicast_lock);
4591561f1103SHerbert Xu 
4592851d0a73SJoseph Huang 	/* br_multicast_join_snoopers has the potential to cause
4593851d0a73SJoseph Huang 	 * an MLD Report/Leave to be delivered to br_multicast_rcv,
4594851d0a73SJoseph Huang 	 * which would in turn call br_multicast_add_group, which would
4595851d0a73SJoseph Huang 	 * attempt to acquire multicast_lock. This function should be
4596851d0a73SJoseph Huang 	 * called after the lock has been released to avoid deadlocks on
4597851d0a73SJoseph Huang 	 * multicast_lock.
4598851d0a73SJoseph Huang 	 *
4599851d0a73SJoseph Huang 	 * br_multicast_leave_snoopers does not have the problem since
4600851d0a73SJoseph Huang 	 * br_multicast_rcv first checks BROPT_MULTICAST_ENABLED, and
4601851d0a73SJoseph Huang 	 * returns without calling br_multicast_ipv4/6_rcv if it's not
4602851d0a73SJoseph Huang 	 * enabled. Moved both functions out just for symmetry.
4603851d0a73SJoseph Huang 	 */
4604851d0a73SJoseph Huang 	if (change_snoopers) {
4605851d0a73SJoseph Huang 		if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
4606851d0a73SJoseph Huang 			br_multicast_join_snoopers(br);
4607851d0a73SJoseph Huang 		else
4608851d0a73SJoseph Huang 			br_multicast_leave_snoopers(br);
4609851d0a73SJoseph Huang 	}
4610851d0a73SJoseph Huang 
4611ae1ea84bSFlorian Fainelli 	return err;
4612561f1103SHerbert Xu }
4613b195167fSHerbert Xu 
br_multicast_enabled(const struct net_device * dev)46149341b988SIdo Schimmel bool br_multicast_enabled(const struct net_device *dev)
46159341b988SIdo Schimmel {
46169341b988SIdo Schimmel 	struct net_bridge *br = netdev_priv(dev);
46179341b988SIdo Schimmel 
461813cefad2SNikolay Aleksandrov 	return !!br_opt_get(br, BROPT_MULTICAST_ENABLED);
46199341b988SIdo Schimmel }
46209341b988SIdo Schimmel EXPORT_SYMBOL_GPL(br_multicast_enabled);
46219341b988SIdo Schimmel 
br_multicast_router(const struct net_device * dev)46220912bda4SYotam Gigi bool br_multicast_router(const struct net_device *dev)
46230912bda4SYotam Gigi {
46240912bda4SYotam Gigi 	struct net_bridge *br = netdev_priv(dev);
46250912bda4SYotam Gigi 	bool is_router;
46260912bda4SYotam Gigi 
46270912bda4SYotam Gigi 	spin_lock_bh(&br->multicast_lock);
4628adc47037SNikolay Aleksandrov 	is_router = br_multicast_is_router(&br->multicast_ctx, NULL);
46290912bda4SYotam Gigi 	spin_unlock_bh(&br->multicast_lock);
46300912bda4SYotam Gigi 	return is_router;
46310912bda4SYotam Gigi }
46320912bda4SYotam Gigi EXPORT_SYMBOL_GPL(br_multicast_router);
46330912bda4SYotam Gigi 
br_multicast_set_querier(struct net_bridge_mcast * brmctx,unsigned long val)463462938182SNikolay Aleksandrov int br_multicast_set_querier(struct net_bridge_mcast *brmctx, unsigned long val)
4635c5c23260SHerbert Xu {
4636b00589afSLinus Lüssing 	unsigned long max_delay;
4637b00589afSLinus Lüssing 
4638c5c23260SHerbert Xu 	val = !!val;
4639c5c23260SHerbert Xu 
464062938182SNikolay Aleksandrov 	spin_lock_bh(&brmctx->br->multicast_lock);
46414d5b4e84SNikolay Aleksandrov 	if (brmctx->multicast_querier == val)
4642c5c23260SHerbert Xu 		goto unlock;
4643c5c23260SHerbert Xu 
46444d5b4e84SNikolay Aleksandrov 	WRITE_ONCE(brmctx->multicast_querier, val);
4645b00589afSLinus Lüssing 	if (!val)
4646b00589afSLinus Lüssing 		goto unlock;
4647b00589afSLinus Lüssing 
4648d3d065c0SNikolay Aleksandrov 	max_delay = brmctx->multicast_query_response_interval;
4649b00589afSLinus Lüssing 
4650d3d065c0SNikolay Aleksandrov 	if (!timer_pending(&brmctx->ip4_other_query.timer))
4651d99971ecSLinus Lüssing 		mod_timer(&brmctx->ip4_other_query.delay_timer,
4652d99971ecSLinus Lüssing 			  jiffies + max_delay);
4653cc0fdd80SLinus Lüssing 
4654adc47037SNikolay Aleksandrov 	br_multicast_start_querier(brmctx, &brmctx->ip4_own_query);
4655cc0fdd80SLinus Lüssing 
4656cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
4657d3d065c0SNikolay Aleksandrov 	if (!timer_pending(&brmctx->ip6_other_query.timer))
4658d99971ecSLinus Lüssing 		mod_timer(&brmctx->ip6_other_query.delay_timer,
4659d99971ecSLinus Lüssing 			  jiffies + max_delay);
4660cc0fdd80SLinus Lüssing 
4661adc47037SNikolay Aleksandrov 	br_multicast_start_querier(brmctx, &brmctx->ip6_own_query);
4662cc0fdd80SLinus Lüssing #endif
4663c5c23260SHerbert Xu 
4664c5c23260SHerbert Xu unlock:
466562938182SNikolay Aleksandrov 	spin_unlock_bh(&brmctx->br->multicast_lock);
4666c5c23260SHerbert Xu 
4667c5c23260SHerbert Xu 	return 0;
4668c5c23260SHerbert Xu }
4669c5c23260SHerbert Xu 
br_multicast_set_igmp_version(struct net_bridge_mcast * brmctx,unsigned long val)4670df271cd6SNikolay Aleksandrov int br_multicast_set_igmp_version(struct net_bridge_mcast *brmctx,
4671df271cd6SNikolay Aleksandrov 				  unsigned long val)
46725e923585SNikolay Aleksandrov {
46735e923585SNikolay Aleksandrov 	/* Currently we support only version 2 and 3 */
46745e923585SNikolay Aleksandrov 	switch (val) {
46755e923585SNikolay Aleksandrov 	case 2:
46765e923585SNikolay Aleksandrov 	case 3:
46775e923585SNikolay Aleksandrov 		break;
46785e923585SNikolay Aleksandrov 	default:
46795e923585SNikolay Aleksandrov 		return -EINVAL;
46805e923585SNikolay Aleksandrov 	}
46815e923585SNikolay Aleksandrov 
4682df271cd6SNikolay Aleksandrov 	spin_lock_bh(&brmctx->br->multicast_lock);
4683df271cd6SNikolay Aleksandrov 	brmctx->multicast_igmp_version = val;
4684df271cd6SNikolay Aleksandrov 	spin_unlock_bh(&brmctx->br->multicast_lock);
46855e923585SNikolay Aleksandrov 
46865e923585SNikolay Aleksandrov 	return 0;
46875e923585SNikolay Aleksandrov }
46885e923585SNikolay Aleksandrov 
4689aa2ae3e7SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
br_multicast_set_mld_version(struct net_bridge_mcast * brmctx,unsigned long val)4690df271cd6SNikolay Aleksandrov int br_multicast_set_mld_version(struct net_bridge_mcast *brmctx,
4691df271cd6SNikolay Aleksandrov 				 unsigned long val)
4692aa2ae3e7SNikolay Aleksandrov {
4693aa2ae3e7SNikolay Aleksandrov 	/* Currently we support version 1 and 2 */
4694aa2ae3e7SNikolay Aleksandrov 	switch (val) {
4695aa2ae3e7SNikolay Aleksandrov 	case 1:
4696aa2ae3e7SNikolay Aleksandrov 	case 2:
4697aa2ae3e7SNikolay Aleksandrov 		break;
4698aa2ae3e7SNikolay Aleksandrov 	default:
4699aa2ae3e7SNikolay Aleksandrov 		return -EINVAL;
4700aa2ae3e7SNikolay Aleksandrov 	}
4701aa2ae3e7SNikolay Aleksandrov 
4702df271cd6SNikolay Aleksandrov 	spin_lock_bh(&brmctx->br->multicast_lock);
4703df271cd6SNikolay Aleksandrov 	brmctx->multicast_mld_version = val;
4704df271cd6SNikolay Aleksandrov 	spin_unlock_bh(&brmctx->br->multicast_lock);
4705aa2ae3e7SNikolay Aleksandrov 
4706aa2ae3e7SNikolay Aleksandrov 	return 0;
4707aa2ae3e7SNikolay Aleksandrov }
4708aa2ae3e7SNikolay Aleksandrov #endif
4709aa2ae3e7SNikolay Aleksandrov 
br_multicast_set_query_intvl(struct net_bridge_mcast * brmctx,unsigned long val)471099b40610SNikolay Aleksandrov void br_multicast_set_query_intvl(struct net_bridge_mcast *brmctx,
471199b40610SNikolay Aleksandrov 				  unsigned long val)
471299b40610SNikolay Aleksandrov {
471399b40610SNikolay Aleksandrov 	unsigned long intvl_jiffies = clock_t_to_jiffies(val);
471499b40610SNikolay Aleksandrov 
471599b40610SNikolay Aleksandrov 	if (intvl_jiffies < BR_MULTICAST_QUERY_INTVL_MIN) {
471699b40610SNikolay Aleksandrov 		br_info(brmctx->br,
471799b40610SNikolay Aleksandrov 			"trying to set multicast query interval below minimum, setting to %lu (%ums)\n",
471899b40610SNikolay Aleksandrov 			jiffies_to_clock_t(BR_MULTICAST_QUERY_INTVL_MIN),
471999b40610SNikolay Aleksandrov 			jiffies_to_msecs(BR_MULTICAST_QUERY_INTVL_MIN));
472099b40610SNikolay Aleksandrov 		intvl_jiffies = BR_MULTICAST_QUERY_INTVL_MIN;
472199b40610SNikolay Aleksandrov 	}
472299b40610SNikolay Aleksandrov 
472399b40610SNikolay Aleksandrov 	brmctx->multicast_query_interval = intvl_jiffies;
472499b40610SNikolay Aleksandrov }
472599b40610SNikolay Aleksandrov 
br_multicast_set_startup_query_intvl(struct net_bridge_mcast * brmctx,unsigned long val)4726f83a112bSNikolay Aleksandrov void br_multicast_set_startup_query_intvl(struct net_bridge_mcast *brmctx,
4727f83a112bSNikolay Aleksandrov 					  unsigned long val)
4728f83a112bSNikolay Aleksandrov {
4729f83a112bSNikolay Aleksandrov 	unsigned long intvl_jiffies = clock_t_to_jiffies(val);
4730f83a112bSNikolay Aleksandrov 
4731f83a112bSNikolay Aleksandrov 	if (intvl_jiffies < BR_MULTICAST_STARTUP_QUERY_INTVL_MIN) {
4732f83a112bSNikolay Aleksandrov 		br_info(brmctx->br,
4733f83a112bSNikolay Aleksandrov 			"trying to set multicast startup query interval below minimum, setting to %lu (%ums)\n",
4734f83a112bSNikolay Aleksandrov 			jiffies_to_clock_t(BR_MULTICAST_STARTUP_QUERY_INTVL_MIN),
4735f83a112bSNikolay Aleksandrov 			jiffies_to_msecs(BR_MULTICAST_STARTUP_QUERY_INTVL_MIN));
4736f83a112bSNikolay Aleksandrov 		intvl_jiffies = BR_MULTICAST_STARTUP_QUERY_INTVL_MIN;
4737f83a112bSNikolay Aleksandrov 	}
4738f83a112bSNikolay Aleksandrov 
4739f83a112bSNikolay Aleksandrov 	brmctx->multicast_startup_query_interval = intvl_jiffies;
4740f83a112bSNikolay Aleksandrov }
4741f83a112bSNikolay Aleksandrov 
474207f8ac4aSLinus Lüssing /**
474307f8ac4aSLinus Lüssing  * br_multicast_list_adjacent - Returns snooped multicast addresses
474407f8ac4aSLinus Lüssing  * @dev:	The bridge port adjacent to which to retrieve addresses
474507f8ac4aSLinus Lüssing  * @br_ip_list:	The list to store found, snooped multicast IP addresses in
474607f8ac4aSLinus Lüssing  *
474707f8ac4aSLinus Lüssing  * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
474807f8ac4aSLinus Lüssing  * snooping feature on all bridge ports of dev's bridge device, excluding
474907f8ac4aSLinus Lüssing  * the addresses from dev itself.
475007f8ac4aSLinus Lüssing  *
475107f8ac4aSLinus Lüssing  * Returns the number of items added to br_ip_list.
475207f8ac4aSLinus Lüssing  *
475307f8ac4aSLinus Lüssing  * Notes:
475407f8ac4aSLinus Lüssing  * - br_ip_list needs to be initialized by caller
475507f8ac4aSLinus Lüssing  * - br_ip_list might contain duplicates in the end
475607f8ac4aSLinus Lüssing  *   (needs to be taken care of by caller)
475707f8ac4aSLinus Lüssing  * - br_ip_list needs to be freed by caller
475807f8ac4aSLinus Lüssing  */
br_multicast_list_adjacent(struct net_device * dev,struct list_head * br_ip_list)475907f8ac4aSLinus Lüssing int br_multicast_list_adjacent(struct net_device *dev,
476007f8ac4aSLinus Lüssing 			       struct list_head *br_ip_list)
476107f8ac4aSLinus Lüssing {
476207f8ac4aSLinus Lüssing 	struct net_bridge *br;
476307f8ac4aSLinus Lüssing 	struct net_bridge_port *port;
476407f8ac4aSLinus Lüssing 	struct net_bridge_port_group *group;
476507f8ac4aSLinus Lüssing 	struct br_ip_list *entry;
476607f8ac4aSLinus Lüssing 	int count = 0;
476707f8ac4aSLinus Lüssing 
476807f8ac4aSLinus Lüssing 	rcu_read_lock();
476935f861e3SJulian Wiedmann 	if (!br_ip_list || !netif_is_bridge_port(dev))
477007f8ac4aSLinus Lüssing 		goto unlock;
477107f8ac4aSLinus Lüssing 
477207f8ac4aSLinus Lüssing 	port = br_port_get_rcu(dev);
477307f8ac4aSLinus Lüssing 	if (!port || !port->br)
477407f8ac4aSLinus Lüssing 		goto unlock;
477507f8ac4aSLinus Lüssing 
477607f8ac4aSLinus Lüssing 	br = port->br;
477707f8ac4aSLinus Lüssing 
477807f8ac4aSLinus Lüssing 	list_for_each_entry_rcu(port, &br->port_list, list) {
477907f8ac4aSLinus Lüssing 		if (!port->dev || port->dev == dev)
478007f8ac4aSLinus Lüssing 			continue;
478107f8ac4aSLinus Lüssing 
478207f8ac4aSLinus Lüssing 		hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
478307f8ac4aSLinus Lüssing 			entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
478407f8ac4aSLinus Lüssing 			if (!entry)
478507f8ac4aSLinus Lüssing 				goto unlock;
478607f8ac4aSLinus Lüssing 
4787085b53c8SNikolay Aleksandrov 			entry->addr = group->key.addr;
478807f8ac4aSLinus Lüssing 			list_add(&entry->list, br_ip_list);
478907f8ac4aSLinus Lüssing 			count++;
479007f8ac4aSLinus Lüssing 		}
479107f8ac4aSLinus Lüssing 	}
479207f8ac4aSLinus Lüssing 
479307f8ac4aSLinus Lüssing unlock:
479407f8ac4aSLinus Lüssing 	rcu_read_unlock();
479507f8ac4aSLinus Lüssing 	return count;
479607f8ac4aSLinus Lüssing }
479707f8ac4aSLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
47982cd41431SLinus Lüssing 
47992cd41431SLinus Lüssing /**
4800c34963e2SLinus Lüssing  * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
4801c34963e2SLinus Lüssing  * @dev: The bridge port providing the bridge on which to check for a querier
4802c34963e2SLinus Lüssing  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
4803c34963e2SLinus Lüssing  *
4804c34963e2SLinus Lüssing  * Checks whether the given interface has a bridge on top and if so returns
4805c34963e2SLinus Lüssing  * true if a valid querier exists anywhere on the bridged link layer.
4806c34963e2SLinus Lüssing  * Otherwise returns false.
4807c34963e2SLinus Lüssing  */
br_multicast_has_querier_anywhere(struct net_device * dev,int proto)4808c34963e2SLinus Lüssing bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
4809c34963e2SLinus Lüssing {
4810c34963e2SLinus Lüssing 	struct net_bridge *br;
4811c34963e2SLinus Lüssing 	struct net_bridge_port *port;
4812c34963e2SLinus Lüssing 	struct ethhdr eth;
4813c34963e2SLinus Lüssing 	bool ret = false;
4814c34963e2SLinus Lüssing 
4815c34963e2SLinus Lüssing 	rcu_read_lock();
481635f861e3SJulian Wiedmann 	if (!netif_is_bridge_port(dev))
4817c34963e2SLinus Lüssing 		goto unlock;
4818c34963e2SLinus Lüssing 
4819c34963e2SLinus Lüssing 	port = br_port_get_rcu(dev);
4820c34963e2SLinus Lüssing 	if (!port || !port->br)
4821c34963e2SLinus Lüssing 		goto unlock;
4822c34963e2SLinus Lüssing 
4823c34963e2SLinus Lüssing 	br = port->br;
4824c34963e2SLinus Lüssing 
4825c34963e2SLinus Lüssing 	memset(&eth, 0, sizeof(eth));
4826c34963e2SLinus Lüssing 	eth.h_proto = htons(proto);
4827c34963e2SLinus Lüssing 
4828adc47037SNikolay Aleksandrov 	ret = br_multicast_querier_exists(&br->multicast_ctx, &eth, NULL);
4829c34963e2SLinus Lüssing 
4830c34963e2SLinus Lüssing unlock:
4831c34963e2SLinus Lüssing 	rcu_read_unlock();
4832c34963e2SLinus Lüssing 	return ret;
4833c34963e2SLinus Lüssing }
4834c34963e2SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
4835c34963e2SLinus Lüssing 
4836c34963e2SLinus Lüssing /**
48372cd41431SLinus Lüssing  * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
48382cd41431SLinus Lüssing  * @dev: The bridge port adjacent to which to check for a querier
48392cd41431SLinus Lüssing  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
48402cd41431SLinus Lüssing  *
48412cd41431SLinus Lüssing  * Checks whether the given interface has a bridge on top and if so returns
48422cd41431SLinus Lüssing  * true if a selected querier is behind one of the other ports of this
48432cd41431SLinus Lüssing  * bridge. Otherwise returns false.
48442cd41431SLinus Lüssing  */
br_multicast_has_querier_adjacent(struct net_device * dev,int proto)48452cd41431SLinus Lüssing bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
48462cd41431SLinus Lüssing {
4847d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx;
48482cd41431SLinus Lüssing 	struct net_bridge *br;
48492cd41431SLinus Lüssing 	struct net_bridge_port *port;
48502cd41431SLinus Lüssing 	bool ret = false;
4851bb18ef8eSNikolay Aleksandrov 	int port_ifidx;
48522cd41431SLinus Lüssing 
48532cd41431SLinus Lüssing 	rcu_read_lock();
485435f861e3SJulian Wiedmann 	if (!netif_is_bridge_port(dev))
48552cd41431SLinus Lüssing 		goto unlock;
48562cd41431SLinus Lüssing 
48572cd41431SLinus Lüssing 	port = br_port_get_rcu(dev);
48582cd41431SLinus Lüssing 	if (!port || !port->br)
48592cd41431SLinus Lüssing 		goto unlock;
48602cd41431SLinus Lüssing 
48612cd41431SLinus Lüssing 	br = port->br;
4862d3d065c0SNikolay Aleksandrov 	brmctx = &br->multicast_ctx;
48632cd41431SLinus Lüssing 
48642cd41431SLinus Lüssing 	switch (proto) {
48652cd41431SLinus Lüssing 	case ETH_P_IP:
4866bb18ef8eSNikolay Aleksandrov 		port_ifidx = brmctx->ip4_querier.port_ifidx;
4867d3d065c0SNikolay Aleksandrov 		if (!timer_pending(&brmctx->ip4_other_query.timer) ||
4868bb18ef8eSNikolay Aleksandrov 		    port_ifidx == port->dev->ifindex)
48692cd41431SLinus Lüssing 			goto unlock;
48702cd41431SLinus Lüssing 		break;
48713993c4e1SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
48722cd41431SLinus Lüssing 	case ETH_P_IPV6:
4873bb18ef8eSNikolay Aleksandrov 		port_ifidx = brmctx->ip6_querier.port_ifidx;
4874d3d065c0SNikolay Aleksandrov 		if (!timer_pending(&brmctx->ip6_other_query.timer) ||
4875bb18ef8eSNikolay Aleksandrov 		    port_ifidx == port->dev->ifindex)
48762cd41431SLinus Lüssing 			goto unlock;
48772cd41431SLinus Lüssing 		break;
48783993c4e1SLinus Lüssing #endif
48792cd41431SLinus Lüssing 	default:
48802cd41431SLinus Lüssing 		goto unlock;
48812cd41431SLinus Lüssing 	}
48822cd41431SLinus Lüssing 
48832cd41431SLinus Lüssing 	ret = true;
48842cd41431SLinus Lüssing unlock:
48852cd41431SLinus Lüssing 	rcu_read_unlock();
48862cd41431SLinus Lüssing 	return ret;
48872cd41431SLinus Lüssing }
48882cd41431SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
48891080ab95SNikolay Aleksandrov 
48903b85f9baSLinus Lüssing /**
48913b85f9baSLinus Lüssing  * br_multicast_has_router_adjacent - Checks for a router behind a bridge port
48923b85f9baSLinus Lüssing  * @dev: The bridge port adjacent to which to check for a multicast router
48933b85f9baSLinus Lüssing  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
48943b85f9baSLinus Lüssing  *
48953b85f9baSLinus Lüssing  * Checks whether the given interface has a bridge on top and if so returns
48963b85f9baSLinus Lüssing  * true if a multicast router is behind one of the other ports of this
48973b85f9baSLinus Lüssing  * bridge. Otherwise returns false.
48983b85f9baSLinus Lüssing  */
br_multicast_has_router_adjacent(struct net_device * dev,int proto)48993b85f9baSLinus Lüssing bool br_multicast_has_router_adjacent(struct net_device *dev, int proto)
49003b85f9baSLinus Lüssing {
49019632233eSNikolay Aleksandrov 	struct net_bridge_mcast_port *pmctx;
4902d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx;
49039632233eSNikolay Aleksandrov 	struct net_bridge_port *port;
49043b85f9baSLinus Lüssing 	bool ret = false;
49053b85f9baSLinus Lüssing 
49063b85f9baSLinus Lüssing 	rcu_read_lock();
49073b85f9baSLinus Lüssing 	port = br_port_get_check_rcu(dev);
49083b85f9baSLinus Lüssing 	if (!port)
49093b85f9baSLinus Lüssing 		goto unlock;
49103b85f9baSLinus Lüssing 
4911d3d065c0SNikolay Aleksandrov 	brmctx = &port->br->multicast_ctx;
49123b85f9baSLinus Lüssing 	switch (proto) {
49133b85f9baSLinus Lüssing 	case ETH_P_IP:
4914d3d065c0SNikolay Aleksandrov 		hlist_for_each_entry_rcu(pmctx, &brmctx->ip4_mc_router_list,
49153b85f9baSLinus Lüssing 					 ip4_rlist) {
49169632233eSNikolay Aleksandrov 			if (pmctx->port == port)
49173b85f9baSLinus Lüssing 				continue;
49183b85f9baSLinus Lüssing 
49193b85f9baSLinus Lüssing 			ret = true;
49203b85f9baSLinus Lüssing 			goto unlock;
49213b85f9baSLinus Lüssing 		}
49223b85f9baSLinus Lüssing 		break;
49233b85f9baSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
49243b85f9baSLinus Lüssing 	case ETH_P_IPV6:
4925d3d065c0SNikolay Aleksandrov 		hlist_for_each_entry_rcu(pmctx, &brmctx->ip6_mc_router_list,
49263b85f9baSLinus Lüssing 					 ip6_rlist) {
49279632233eSNikolay Aleksandrov 			if (pmctx->port == port)
49283b85f9baSLinus Lüssing 				continue;
49293b85f9baSLinus Lüssing 
49303b85f9baSLinus Lüssing 			ret = true;
49313b85f9baSLinus Lüssing 			goto unlock;
49323b85f9baSLinus Lüssing 		}
49333b85f9baSLinus Lüssing 		break;
49343b85f9baSLinus Lüssing #endif
49353b85f9baSLinus Lüssing 	default:
49363b85f9baSLinus Lüssing 		/* when compiled without IPv6 support, be conservative and
49373b85f9baSLinus Lüssing 		 * always assume presence of an IPv6 multicast router
49383b85f9baSLinus Lüssing 		 */
49393b85f9baSLinus Lüssing 		ret = true;
49403b85f9baSLinus Lüssing 	}
49413b85f9baSLinus Lüssing 
49423b85f9baSLinus Lüssing unlock:
49433b85f9baSLinus Lüssing 	rcu_read_unlock();
49443b85f9baSLinus Lüssing 	return ret;
49453b85f9baSLinus Lüssing }
49463b85f9baSLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_router_adjacent);
49473b85f9baSLinus Lüssing 
br_mcast_stats_add(struct bridge_mcast_stats __percpu * stats,const struct sk_buff * skb,u8 type,u8 dir)49481080ab95SNikolay Aleksandrov static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
4949a65056ecSNikolay Aleksandrov 			       const struct sk_buff *skb, u8 type, u8 dir)
49501080ab95SNikolay Aleksandrov {
49511080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
4952a65056ecSNikolay Aleksandrov 	__be16 proto = skb->protocol;
4953a65056ecSNikolay Aleksandrov 	unsigned int t_len;
49541080ab95SNikolay Aleksandrov 
49551080ab95SNikolay Aleksandrov 	u64_stats_update_begin(&pstats->syncp);
49561080ab95SNikolay Aleksandrov 	switch (proto) {
49571080ab95SNikolay Aleksandrov 	case htons(ETH_P_IP):
4958a65056ecSNikolay Aleksandrov 		t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
49591080ab95SNikolay Aleksandrov 		switch (type) {
49601080ab95SNikolay Aleksandrov 		case IGMP_HOST_MEMBERSHIP_REPORT:
49611080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v1reports[dir]++;
49621080ab95SNikolay Aleksandrov 			break;
49631080ab95SNikolay Aleksandrov 		case IGMPV2_HOST_MEMBERSHIP_REPORT:
49641080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v2reports[dir]++;
49651080ab95SNikolay Aleksandrov 			break;
49661080ab95SNikolay Aleksandrov 		case IGMPV3_HOST_MEMBERSHIP_REPORT:
49671080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v3reports[dir]++;
49681080ab95SNikolay Aleksandrov 			break;
49691080ab95SNikolay Aleksandrov 		case IGMP_HOST_MEMBERSHIP_QUERY:
4970a65056ecSNikolay Aleksandrov 			if (t_len != sizeof(struct igmphdr)) {
4971a65056ecSNikolay Aleksandrov 				pstats->mstats.igmp_v3queries[dir]++;
4972a65056ecSNikolay Aleksandrov 			} else {
4973a65056ecSNikolay Aleksandrov 				unsigned int offset = skb_transport_offset(skb);
4974a65056ecSNikolay Aleksandrov 				struct igmphdr *ih, _ihdr;
4975a65056ecSNikolay Aleksandrov 
4976a65056ecSNikolay Aleksandrov 				ih = skb_header_pointer(skb, offset,
4977a65056ecSNikolay Aleksandrov 							sizeof(_ihdr), &_ihdr);
4978a65056ecSNikolay Aleksandrov 				if (!ih)
4979a65056ecSNikolay Aleksandrov 					break;
4980a65056ecSNikolay Aleksandrov 				if (!ih->code)
4981a65056ecSNikolay Aleksandrov 					pstats->mstats.igmp_v1queries[dir]++;
4982a65056ecSNikolay Aleksandrov 				else
4983a65056ecSNikolay Aleksandrov 					pstats->mstats.igmp_v2queries[dir]++;
4984a65056ecSNikolay Aleksandrov 			}
49851080ab95SNikolay Aleksandrov 			break;
49861080ab95SNikolay Aleksandrov 		case IGMP_HOST_LEAVE_MESSAGE:
49871080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_leaves[dir]++;
49881080ab95SNikolay Aleksandrov 			break;
49891080ab95SNikolay Aleksandrov 		}
49901080ab95SNikolay Aleksandrov 		break;
49911080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
49921080ab95SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
4993a65056ecSNikolay Aleksandrov 		t_len = ntohs(ipv6_hdr(skb)->payload_len) +
4994a65056ecSNikolay Aleksandrov 			sizeof(struct ipv6hdr);
4995a65056ecSNikolay Aleksandrov 		t_len -= skb_network_header_len(skb);
49961080ab95SNikolay Aleksandrov 		switch (type) {
49971080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_REPORT:
49981080ab95SNikolay Aleksandrov 			pstats->mstats.mld_v1reports[dir]++;
49991080ab95SNikolay Aleksandrov 			break;
50001080ab95SNikolay Aleksandrov 		case ICMPV6_MLD2_REPORT:
50011080ab95SNikolay Aleksandrov 			pstats->mstats.mld_v2reports[dir]++;
50021080ab95SNikolay Aleksandrov 			break;
50031080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_QUERY:
5004a65056ecSNikolay Aleksandrov 			if (t_len != sizeof(struct mld_msg))
5005a65056ecSNikolay Aleksandrov 				pstats->mstats.mld_v2queries[dir]++;
5006a65056ecSNikolay Aleksandrov 			else
5007a65056ecSNikolay Aleksandrov 				pstats->mstats.mld_v1queries[dir]++;
50081080ab95SNikolay Aleksandrov 			break;
50091080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_REDUCTION:
50101080ab95SNikolay Aleksandrov 			pstats->mstats.mld_leaves[dir]++;
50111080ab95SNikolay Aleksandrov 			break;
50121080ab95SNikolay Aleksandrov 		}
50131080ab95SNikolay Aleksandrov 		break;
50141080ab95SNikolay Aleksandrov #endif /* CONFIG_IPV6 */
50151080ab95SNikolay Aleksandrov 	}
50161080ab95SNikolay Aleksandrov 	u64_stats_update_end(&pstats->syncp);
50171080ab95SNikolay Aleksandrov }
50181080ab95SNikolay Aleksandrov 
br_multicast_count(struct net_bridge * br,const struct net_bridge_port * p,const struct sk_buff * skb,u8 type,u8 dir)5019adc47037SNikolay Aleksandrov void br_multicast_count(struct net_bridge *br,
5020adc47037SNikolay Aleksandrov 			const struct net_bridge_port *p,
5021a65056ecSNikolay Aleksandrov 			const struct sk_buff *skb, u8 type, u8 dir)
50221080ab95SNikolay Aleksandrov {
50231080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
50241080ab95SNikolay Aleksandrov 
50251080ab95SNikolay Aleksandrov 	/* if multicast_disabled is true then igmp type can't be set */
5026675779adSNikolay Aleksandrov 	if (!type || !br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
50271080ab95SNikolay Aleksandrov 		return;
50281080ab95SNikolay Aleksandrov 
50291080ab95SNikolay Aleksandrov 	if (p)
50301080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
50311080ab95SNikolay Aleksandrov 	else
50321080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
50331080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
50341080ab95SNikolay Aleksandrov 		return;
50351080ab95SNikolay Aleksandrov 
5036a65056ecSNikolay Aleksandrov 	br_mcast_stats_add(stats, skb, type, dir);
50371080ab95SNikolay Aleksandrov }
50381080ab95SNikolay Aleksandrov 
br_multicast_init_stats(struct net_bridge * br)50391080ab95SNikolay Aleksandrov int br_multicast_init_stats(struct net_bridge *br)
50401080ab95SNikolay Aleksandrov {
50411080ab95SNikolay Aleksandrov 	br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
50421080ab95SNikolay Aleksandrov 	if (!br->mcast_stats)
50431080ab95SNikolay Aleksandrov 		return -ENOMEM;
50441080ab95SNikolay Aleksandrov 
50451080ab95SNikolay Aleksandrov 	return 0;
50461080ab95SNikolay Aleksandrov }
50471080ab95SNikolay Aleksandrov 
br_multicast_uninit_stats(struct net_bridge * br)5048b6fe0440SIdo Schimmel void br_multicast_uninit_stats(struct net_bridge *br)
5049b6fe0440SIdo Schimmel {
5050b6fe0440SIdo Schimmel 	free_percpu(br->mcast_stats);
5051b6fe0440SIdo Schimmel }
5052b6fe0440SIdo Schimmel 
5053b3b6a84cSArnd Bergmann /* noinline for https://bugs.llvm.org/show_bug.cgi?id=45802#c9 */
mcast_stats_add_dir(u64 * dst,u64 * src)5054b3b6a84cSArnd Bergmann static noinline_for_stack void mcast_stats_add_dir(u64 *dst, u64 *src)
50551080ab95SNikolay Aleksandrov {
50561080ab95SNikolay Aleksandrov 	dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
50571080ab95SNikolay Aleksandrov 	dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
50581080ab95SNikolay Aleksandrov }
50591080ab95SNikolay Aleksandrov 
br_multicast_get_stats(const struct net_bridge * br,const struct net_bridge_port * p,struct br_mcast_stats * dest)50601080ab95SNikolay Aleksandrov void br_multicast_get_stats(const struct net_bridge *br,
50611080ab95SNikolay Aleksandrov 			    const struct net_bridge_port *p,
50621080ab95SNikolay Aleksandrov 			    struct br_mcast_stats *dest)
50631080ab95SNikolay Aleksandrov {
50641080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
50651080ab95SNikolay Aleksandrov 	struct br_mcast_stats tdst;
50661080ab95SNikolay Aleksandrov 	int i;
50671080ab95SNikolay Aleksandrov 
50681080ab95SNikolay Aleksandrov 	memset(dest, 0, sizeof(*dest));
50691080ab95SNikolay Aleksandrov 	if (p)
50701080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
50711080ab95SNikolay Aleksandrov 	else
50721080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
50731080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
50741080ab95SNikolay Aleksandrov 		return;
50751080ab95SNikolay Aleksandrov 
50761080ab95SNikolay Aleksandrov 	memset(&tdst, 0, sizeof(tdst));
50771080ab95SNikolay Aleksandrov 	for_each_possible_cpu(i) {
50781080ab95SNikolay Aleksandrov 		struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
50791080ab95SNikolay Aleksandrov 		struct br_mcast_stats temp;
50801080ab95SNikolay Aleksandrov 		unsigned int start;
50811080ab95SNikolay Aleksandrov 
50821080ab95SNikolay Aleksandrov 		do {
5083d120d1a6SThomas Gleixner 			start = u64_stats_fetch_begin(&cpu_stats->syncp);
50841080ab95SNikolay Aleksandrov 			memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
5085d120d1a6SThomas Gleixner 		} while (u64_stats_fetch_retry(&cpu_stats->syncp, start));
50861080ab95SNikolay Aleksandrov 
5087a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
5088a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
5089a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
50901080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
50911080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
50921080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
50931080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
50941080ab95SNikolay Aleksandrov 		tdst.igmp_parse_errors += temp.igmp_parse_errors;
50951080ab95SNikolay Aleksandrov 
5096a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
5097a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
50981080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
50991080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
51001080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
51011080ab95SNikolay Aleksandrov 		tdst.mld_parse_errors += temp.mld_parse_errors;
51021080ab95SNikolay Aleksandrov 	}
51031080ab95SNikolay Aleksandrov 	memcpy(dest, &tdst, sizeof(*dest));
51041080ab95SNikolay Aleksandrov }
510519e3a9c9SNikolay Aleksandrov 
br_mdb_hash_init(struct net_bridge * br)510619e3a9c9SNikolay Aleksandrov int br_mdb_hash_init(struct net_bridge *br)
510719e3a9c9SNikolay Aleksandrov {
5108085b53c8SNikolay Aleksandrov 	int err;
5109085b53c8SNikolay Aleksandrov 
5110085b53c8SNikolay Aleksandrov 	err = rhashtable_init(&br->sg_port_tbl, &br_sg_port_rht_params);
5111085b53c8SNikolay Aleksandrov 	if (err)
5112085b53c8SNikolay Aleksandrov 		return err;
5113085b53c8SNikolay Aleksandrov 
5114085b53c8SNikolay Aleksandrov 	err = rhashtable_init(&br->mdb_hash_tbl, &br_mdb_rht_params);
5115085b53c8SNikolay Aleksandrov 	if (err) {
5116085b53c8SNikolay Aleksandrov 		rhashtable_destroy(&br->sg_port_tbl);
5117085b53c8SNikolay Aleksandrov 		return err;
5118085b53c8SNikolay Aleksandrov 	}
5119085b53c8SNikolay Aleksandrov 
5120085b53c8SNikolay Aleksandrov 	return 0;
512119e3a9c9SNikolay Aleksandrov }
512219e3a9c9SNikolay Aleksandrov 
br_mdb_hash_fini(struct net_bridge * br)512319e3a9c9SNikolay Aleksandrov void br_mdb_hash_fini(struct net_bridge *br)
512419e3a9c9SNikolay Aleksandrov {
5125085b53c8SNikolay Aleksandrov 	rhashtable_destroy(&br->sg_port_tbl);
512619e3a9c9SNikolay Aleksandrov 	rhashtable_destroy(&br->mdb_hash_tbl);
512719e3a9c9SNikolay Aleksandrov }
5128