xref: /openbmc/linux/net/bridge/br_multicast.c (revision d3d065c0032bf7043401e34cec2114fc553f919e)
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
34eb1d1641SHerbert Xu 
35eb1d1641SHerbert Xu #include "br_private.h"
368f07b831SNikolay Aleksandrov #include "br_private_mcast_eht.h"
37eb1d1641SHerbert Xu 
3819e3a9c9SNikolay Aleksandrov static const struct rhashtable_params br_mdb_rht_params = {
3919e3a9c9SNikolay Aleksandrov 	.head_offset = offsetof(struct net_bridge_mdb_entry, rhnode),
4019e3a9c9SNikolay Aleksandrov 	.key_offset = offsetof(struct net_bridge_mdb_entry, addr),
4119e3a9c9SNikolay Aleksandrov 	.key_len = sizeof(struct br_ip),
4219e3a9c9SNikolay Aleksandrov 	.automatic_shrinking = true,
4319e3a9c9SNikolay Aleksandrov };
4419e3a9c9SNikolay Aleksandrov 
45085b53c8SNikolay Aleksandrov static const struct rhashtable_params br_sg_port_rht_params = {
46085b53c8SNikolay Aleksandrov 	.head_offset = offsetof(struct net_bridge_port_group, rhnode),
47085b53c8SNikolay Aleksandrov 	.key_offset = offsetof(struct net_bridge_port_group, key),
48085b53c8SNikolay Aleksandrov 	.key_len = sizeof(struct net_bridge_port_group_sg_key),
49085b53c8SNikolay Aleksandrov 	.automatic_shrinking = true,
50085b53c8SNikolay Aleksandrov };
51085b53c8SNikolay Aleksandrov 
52cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br,
5390010b36SLinus Lüssing 				       struct bridge_mcast_own_query *query);
54d9b8c4d8SLinus Lüssing static void br_ip4_multicast_add_router(struct net_bridge *br,
55754bc547SSatish Ashok 					struct net_bridge_port *port);
56bc8c20acSSatish Ashok static void br_ip4_multicast_leave_group(struct net_bridge *br,
57bc8c20acSSatish Ashok 					 struct net_bridge_port *port,
58bc8c20acSSatish Ashok 					 __be32 group,
596db6f0eaSFelix Fietkau 					 __u16 vid,
606db6f0eaSFelix Fietkau 					 const unsigned char *src);
6142c11ccfSNikolay Aleksandrov static void br_multicast_port_group_rexmit(struct timer_list *t);
626db6f0eaSFelix Fietkau 
63ed2d3597SLinus Lüssing static void
64ed2d3597SLinus Lüssing br_multicast_rport_del_notify(struct net_bridge_port *p, bool deleted);
65a3c02e76SLinus Lüssing static void br_ip6_multicast_add_router(struct net_bridge *br,
66a3c02e76SLinus Lüssing 					struct net_bridge_port *port);
6730515832SMatteo Croce #if IS_ENABLED(CONFIG_IPV6)
68bc8c20acSSatish Ashok static void br_ip6_multicast_leave_group(struct net_bridge *br,
69bc8c20acSSatish Ashok 					 struct net_bridge_port *port,
70bc8c20acSSatish Ashok 					 const struct in6_addr *group,
716db6f0eaSFelix Fietkau 					 __u16 vid, const unsigned char *src);
72bc8c20acSSatish Ashok #endif
73b0812368SNikolay Aleksandrov static struct net_bridge_port_group *
74b0812368SNikolay Aleksandrov __br_multicast_add_group(struct net_bridge *br,
75b0812368SNikolay Aleksandrov 			 struct net_bridge_port *port,
76b0812368SNikolay Aleksandrov 			 struct br_ip *group,
77b0812368SNikolay Aleksandrov 			 const unsigned char *src,
78b0812368SNikolay Aleksandrov 			 u8 filter_mode,
799116ffbfSNikolay Aleksandrov 			 bool igmpv2_mldv1,
809116ffbfSNikolay Aleksandrov 			 bool blocked);
818266a049SNikolay Aleksandrov static void br_multicast_find_del_pg(struct net_bridge *br,
828266a049SNikolay Aleksandrov 				     struct net_bridge_port_group *pg);
83c83b8fabSHerbert Xu 
84085b53c8SNikolay Aleksandrov static struct net_bridge_port_group *
85085b53c8SNikolay Aleksandrov br_sg_port_find(struct net_bridge *br,
86085b53c8SNikolay Aleksandrov 		struct net_bridge_port_group_sg_key *sg_p)
87085b53c8SNikolay Aleksandrov {
88085b53c8SNikolay Aleksandrov 	lockdep_assert_held_once(&br->multicast_lock);
89085b53c8SNikolay Aleksandrov 
90085b53c8SNikolay Aleksandrov 	return rhashtable_lookup_fast(&br->sg_port_tbl, sg_p,
91085b53c8SNikolay Aleksandrov 				      br_sg_port_rht_params);
92085b53c8SNikolay Aleksandrov }
93085b53c8SNikolay Aleksandrov 
9419e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip_get_rcu(struct net_bridge *br,
95cfd56754SCong Wang 						      struct br_ip *dst)
967f285fa7SHerbert Xu {
9719e3a9c9SNikolay Aleksandrov 	return rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params);
987f285fa7SHerbert Xu }
997f285fa7SHerbert Xu 
10019e3a9c9SNikolay Aleksandrov struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge *br,
10119e3a9c9SNikolay Aleksandrov 					   struct br_ip *dst)
10219e3a9c9SNikolay Aleksandrov {
10319e3a9c9SNikolay Aleksandrov 	struct net_bridge_mdb_entry *ent;
10419e3a9c9SNikolay Aleksandrov 
10519e3a9c9SNikolay Aleksandrov 	lockdep_assert_held_once(&br->multicast_lock);
10619e3a9c9SNikolay Aleksandrov 
10719e3a9c9SNikolay Aleksandrov 	rcu_read_lock();
10819e3a9c9SNikolay Aleksandrov 	ent = rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params);
10919e3a9c9SNikolay Aleksandrov 	rcu_read_unlock();
11019e3a9c9SNikolay Aleksandrov 
11119e3a9c9SNikolay Aleksandrov 	return ent;
11219e3a9c9SNikolay Aleksandrov }
11319e3a9c9SNikolay Aleksandrov 
11419e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip4_get(struct net_bridge *br,
11519e3a9c9SNikolay Aleksandrov 						   __be32 dst, __u16 vid)
116eb1d1641SHerbert Xu {
1178ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip br_dst;
1180821ec55SHerbert Xu 
11919e3a9c9SNikolay Aleksandrov 	memset(&br_dst, 0, sizeof(br_dst));
120eab3227bSNikolay Aleksandrov 	br_dst.dst.ip4 = dst;
1218ef2a9a5SYOSHIFUJI Hideaki 	br_dst.proto = htons(ETH_P_IP);
122b0e9a30dSVlad Yasevich 	br_dst.vid = vid;
1238ef2a9a5SYOSHIFUJI Hideaki 
12419e3a9c9SNikolay Aleksandrov 	return br_mdb_ip_get(br, &br_dst);
1258ef2a9a5SYOSHIFUJI Hideaki }
1268ef2a9a5SYOSHIFUJI Hideaki 
127dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
12819e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip6_get(struct net_bridge *br,
12919e3a9c9SNikolay Aleksandrov 						   const struct in6_addr *dst,
130b0e9a30dSVlad Yasevich 						   __u16 vid)
13108b202b6SYOSHIFUJI Hideaki {
13208b202b6SYOSHIFUJI Hideaki 	struct br_ip br_dst;
13308b202b6SYOSHIFUJI Hideaki 
13419e3a9c9SNikolay Aleksandrov 	memset(&br_dst, 0, sizeof(br_dst));
135eab3227bSNikolay Aleksandrov 	br_dst.dst.ip6 = *dst;
13608b202b6SYOSHIFUJI Hideaki 	br_dst.proto = htons(ETH_P_IPV6);
137b0e9a30dSVlad Yasevich 	br_dst.vid = vid;
13808b202b6SYOSHIFUJI Hideaki 
13919e3a9c9SNikolay Aleksandrov 	return br_mdb_ip_get(br, &br_dst);
14008b202b6SYOSHIFUJI Hideaki }
14108b202b6SYOSHIFUJI Hideaki #endif
14208b202b6SYOSHIFUJI Hideaki 
143eb1d1641SHerbert Xu struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
144fbca58a2SCong Wang 					struct sk_buff *skb, u16 vid)
145eb1d1641SHerbert Xu {
1468ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip ip;
1478ef2a9a5SYOSHIFUJI Hideaki 
14813cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
149eb1d1641SHerbert Xu 		return NULL;
150eb1d1641SHerbert Xu 
1518ef2a9a5SYOSHIFUJI Hideaki 	if (BR_INPUT_SKB_CB(skb)->igmp)
1528ef2a9a5SYOSHIFUJI Hideaki 		return NULL;
1538ef2a9a5SYOSHIFUJI Hideaki 
15419e3a9c9SNikolay Aleksandrov 	memset(&ip, 0, sizeof(ip));
1558ef2a9a5SYOSHIFUJI Hideaki 	ip.proto = skb->protocol;
156fbca58a2SCong Wang 	ip.vid = vid;
1578ef2a9a5SYOSHIFUJI Hideaki 
158eb1d1641SHerbert Xu 	switch (skb->protocol) {
159eb1d1641SHerbert Xu 	case htons(ETH_P_IP):
160eab3227bSNikolay Aleksandrov 		ip.dst.ip4 = ip_hdr(skb)->daddr;
161*d3d065c0SNikolay Aleksandrov 		if (br->multicast_ctx.multicast_igmp_version == 3) {
1627d07a68cSNikolay Aleksandrov 			struct net_bridge_mdb_entry *mdb;
1637d07a68cSNikolay Aleksandrov 
1647d07a68cSNikolay Aleksandrov 			ip.src.ip4 = ip_hdr(skb)->saddr;
1657d07a68cSNikolay Aleksandrov 			mdb = br_mdb_ip_get_rcu(br, &ip);
1667d07a68cSNikolay Aleksandrov 			if (mdb)
1677d07a68cSNikolay Aleksandrov 				return mdb;
1687d07a68cSNikolay Aleksandrov 			ip.src.ip4 = 0;
1697d07a68cSNikolay Aleksandrov 		}
170eb1d1641SHerbert Xu 		break;
171dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
17208b202b6SYOSHIFUJI Hideaki 	case htons(ETH_P_IPV6):
173eab3227bSNikolay Aleksandrov 		ip.dst.ip6 = ipv6_hdr(skb)->daddr;
174*d3d065c0SNikolay Aleksandrov 		if (br->multicast_ctx.multicast_mld_version == 2) {
1757d07a68cSNikolay Aleksandrov 			struct net_bridge_mdb_entry *mdb;
1767d07a68cSNikolay Aleksandrov 
1777d07a68cSNikolay Aleksandrov 			ip.src.ip6 = ipv6_hdr(skb)->saddr;
1787d07a68cSNikolay Aleksandrov 			mdb = br_mdb_ip_get_rcu(br, &ip);
1797d07a68cSNikolay Aleksandrov 			if (mdb)
1807d07a68cSNikolay Aleksandrov 				return mdb;
1817d07a68cSNikolay Aleksandrov 			memset(&ip.src.ip6, 0, sizeof(ip.src.ip6));
1827d07a68cSNikolay Aleksandrov 		}
18308b202b6SYOSHIFUJI Hideaki 		break;
18408b202b6SYOSHIFUJI Hideaki #endif
1858ef2a9a5SYOSHIFUJI Hideaki 	default:
186955062b0SNikolay Aleksandrov 		ip.proto = 0;
187955062b0SNikolay Aleksandrov 		ether_addr_copy(ip.dst.mac_addr, eth_hdr(skb)->h_dest);
188eb1d1641SHerbert Xu 	}
189eb1d1641SHerbert Xu 
19019e3a9c9SNikolay Aleksandrov 	return br_mdb_ip_get_rcu(br, &ip);
191eb1d1641SHerbert Xu }
192eb1d1641SHerbert Xu 
193b0812368SNikolay Aleksandrov static bool br_port_group_equal(struct net_bridge_port_group *p,
194b0812368SNikolay Aleksandrov 				struct net_bridge_port *port,
195b0812368SNikolay Aleksandrov 				const unsigned char *src)
196b0812368SNikolay Aleksandrov {
197b0812368SNikolay Aleksandrov 	if (p->key.port != port)
198b0812368SNikolay Aleksandrov 		return false;
199b0812368SNikolay Aleksandrov 
200b0812368SNikolay Aleksandrov 	if (!(port->flags & BR_MULTICAST_TO_UNICAST))
201b0812368SNikolay Aleksandrov 		return true;
202b0812368SNikolay Aleksandrov 
203b0812368SNikolay Aleksandrov 	return ether_addr_equal(src, p->eth_addr);
204b0812368SNikolay Aleksandrov }
205b0812368SNikolay Aleksandrov 
2068266a049SNikolay Aleksandrov static void __fwd_add_star_excl(struct net_bridge_port_group *pg,
2078266a049SNikolay Aleksandrov 				struct br_ip *sg_ip)
2088266a049SNikolay Aleksandrov {
2098266a049SNikolay Aleksandrov 	struct net_bridge_port_group_sg_key sg_key;
2108266a049SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
2118266a049SNikolay Aleksandrov 	struct net_bridge_port_group *src_pg;
2128266a049SNikolay Aleksandrov 
2138266a049SNikolay Aleksandrov 	memset(&sg_key, 0, sizeof(sg_key));
2148266a049SNikolay Aleksandrov 	sg_key.port = pg->key.port;
2158266a049SNikolay Aleksandrov 	sg_key.addr = *sg_ip;
2168266a049SNikolay Aleksandrov 	if (br_sg_port_find(br, &sg_key))
2178266a049SNikolay Aleksandrov 		return;
2188266a049SNikolay Aleksandrov 
2198266a049SNikolay Aleksandrov 	src_pg = __br_multicast_add_group(br, pg->key.port, sg_ip, pg->eth_addr,
2209116ffbfSNikolay Aleksandrov 					  MCAST_INCLUDE, false, false);
2218266a049SNikolay Aleksandrov 	if (IS_ERR_OR_NULL(src_pg) ||
2228266a049SNikolay Aleksandrov 	    src_pg->rt_protocol != RTPROT_KERNEL)
2238266a049SNikolay Aleksandrov 		return;
2248266a049SNikolay Aleksandrov 
2258266a049SNikolay Aleksandrov 	src_pg->flags |= MDB_PG_FLAGS_STAR_EXCL;
2268266a049SNikolay Aleksandrov }
2278266a049SNikolay Aleksandrov 
2288266a049SNikolay Aleksandrov static void __fwd_del_star_excl(struct net_bridge_port_group *pg,
2298266a049SNikolay Aleksandrov 				struct br_ip *sg_ip)
2308266a049SNikolay Aleksandrov {
2318266a049SNikolay Aleksandrov 	struct net_bridge_port_group_sg_key sg_key;
2328266a049SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
2338266a049SNikolay Aleksandrov 	struct net_bridge_port_group *src_pg;
2348266a049SNikolay Aleksandrov 
2358266a049SNikolay Aleksandrov 	memset(&sg_key, 0, sizeof(sg_key));
2368266a049SNikolay Aleksandrov 	sg_key.port = pg->key.port;
2378266a049SNikolay Aleksandrov 	sg_key.addr = *sg_ip;
2388266a049SNikolay Aleksandrov 	src_pg = br_sg_port_find(br, &sg_key);
2398266a049SNikolay Aleksandrov 	if (!src_pg || !(src_pg->flags & MDB_PG_FLAGS_STAR_EXCL) ||
2408266a049SNikolay Aleksandrov 	    src_pg->rt_protocol != RTPROT_KERNEL)
2418266a049SNikolay Aleksandrov 		return;
2428266a049SNikolay Aleksandrov 
2438266a049SNikolay Aleksandrov 	br_multicast_find_del_pg(br, src_pg);
2448266a049SNikolay Aleksandrov }
2458266a049SNikolay Aleksandrov 
2468266a049SNikolay Aleksandrov /* When a port group transitions to (or is added as) EXCLUDE we need to add it
2478266a049SNikolay Aleksandrov  * to all other ports' S,G entries which are not blocked by the current group
2488266a049SNikolay Aleksandrov  * for proper replication, the assumption is that any S,G blocked entries
2498266a049SNikolay Aleksandrov  * are already added so the S,G,port lookup should skip them.
2508266a049SNikolay Aleksandrov  * When a port group transitions from EXCLUDE -> INCLUDE mode or is being
2518266a049SNikolay Aleksandrov  * deleted we need to remove it from all ports' S,G entries where it was
2528266a049SNikolay Aleksandrov  * automatically installed before (i.e. where it's MDB_PG_FLAGS_STAR_EXCL).
2538266a049SNikolay Aleksandrov  */
2548266a049SNikolay Aleksandrov void br_multicast_star_g_handle_mode(struct net_bridge_port_group *pg,
2558266a049SNikolay Aleksandrov 				     u8 filter_mode)
2568266a049SNikolay Aleksandrov {
2578266a049SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
2588266a049SNikolay Aleksandrov 	struct net_bridge_port_group *pg_lst;
2598266a049SNikolay Aleksandrov 	struct net_bridge_mdb_entry *mp;
2608266a049SNikolay Aleksandrov 	struct br_ip sg_ip;
2618266a049SNikolay Aleksandrov 
2628266a049SNikolay Aleksandrov 	if (WARN_ON(!br_multicast_is_star_g(&pg->key.addr)))
2638266a049SNikolay Aleksandrov 		return;
2648266a049SNikolay Aleksandrov 
2658266a049SNikolay Aleksandrov 	mp = br_mdb_ip_get(br, &pg->key.addr);
2668266a049SNikolay Aleksandrov 	if (!mp)
2678266a049SNikolay Aleksandrov 		return;
2688266a049SNikolay Aleksandrov 
2698266a049SNikolay Aleksandrov 	memset(&sg_ip, 0, sizeof(sg_ip));
2708266a049SNikolay Aleksandrov 	sg_ip = pg->key.addr;
2718266a049SNikolay Aleksandrov 	for (pg_lst = mlock_dereference(mp->ports, br);
2728266a049SNikolay Aleksandrov 	     pg_lst;
2738266a049SNikolay Aleksandrov 	     pg_lst = mlock_dereference(pg_lst->next, br)) {
2748266a049SNikolay Aleksandrov 		struct net_bridge_group_src *src_ent;
2758266a049SNikolay Aleksandrov 
2768266a049SNikolay Aleksandrov 		if (pg_lst == pg)
2778266a049SNikolay Aleksandrov 			continue;
2788266a049SNikolay Aleksandrov 		hlist_for_each_entry(src_ent, &pg_lst->src_list, node) {
2798266a049SNikolay Aleksandrov 			if (!(src_ent->flags & BR_SGRP_F_INSTALLED))
2808266a049SNikolay Aleksandrov 				continue;
2818266a049SNikolay Aleksandrov 			sg_ip.src = src_ent->addr.src;
2828266a049SNikolay Aleksandrov 			switch (filter_mode) {
2838266a049SNikolay Aleksandrov 			case MCAST_INCLUDE:
2848266a049SNikolay Aleksandrov 				__fwd_del_star_excl(pg, &sg_ip);
2858266a049SNikolay Aleksandrov 				break;
2868266a049SNikolay Aleksandrov 			case MCAST_EXCLUDE:
2878266a049SNikolay Aleksandrov 				__fwd_add_star_excl(pg, &sg_ip);
2888266a049SNikolay Aleksandrov 				break;
2898266a049SNikolay Aleksandrov 			}
2908266a049SNikolay Aleksandrov 		}
2918266a049SNikolay Aleksandrov 	}
2928266a049SNikolay Aleksandrov }
2938266a049SNikolay Aleksandrov 
294094b82fdSNikolay Aleksandrov /* called when adding a new S,G with host_joined == false by default */
295094b82fdSNikolay Aleksandrov static void br_multicast_sg_host_state(struct net_bridge_mdb_entry *star_mp,
296094b82fdSNikolay Aleksandrov 				       struct net_bridge_port_group *sg)
297094b82fdSNikolay Aleksandrov {
298094b82fdSNikolay Aleksandrov 	struct net_bridge_mdb_entry *sg_mp;
299094b82fdSNikolay Aleksandrov 
300094b82fdSNikolay Aleksandrov 	if (WARN_ON(!br_multicast_is_star_g(&star_mp->addr)))
301094b82fdSNikolay Aleksandrov 		return;
302094b82fdSNikolay Aleksandrov 	if (!star_mp->host_joined)
303094b82fdSNikolay Aleksandrov 		return;
304094b82fdSNikolay Aleksandrov 
305094b82fdSNikolay Aleksandrov 	sg_mp = br_mdb_ip_get(star_mp->br, &sg->key.addr);
306094b82fdSNikolay Aleksandrov 	if (!sg_mp)
307094b82fdSNikolay Aleksandrov 		return;
308094b82fdSNikolay Aleksandrov 	sg_mp->host_joined = true;
309094b82fdSNikolay Aleksandrov }
310094b82fdSNikolay Aleksandrov 
311094b82fdSNikolay Aleksandrov /* set the host_joined state of all of *,G's S,G entries */
312094b82fdSNikolay Aleksandrov static void br_multicast_star_g_host_state(struct net_bridge_mdb_entry *star_mp)
313094b82fdSNikolay Aleksandrov {
314094b82fdSNikolay Aleksandrov 	struct net_bridge *br = star_mp->br;
315094b82fdSNikolay Aleksandrov 	struct net_bridge_mdb_entry *sg_mp;
316094b82fdSNikolay Aleksandrov 	struct net_bridge_port_group *pg;
317094b82fdSNikolay Aleksandrov 	struct br_ip sg_ip;
318094b82fdSNikolay Aleksandrov 
319094b82fdSNikolay Aleksandrov 	if (WARN_ON(!br_multicast_is_star_g(&star_mp->addr)))
320094b82fdSNikolay Aleksandrov 		return;
321094b82fdSNikolay Aleksandrov 
322094b82fdSNikolay Aleksandrov 	memset(&sg_ip, 0, sizeof(sg_ip));
323094b82fdSNikolay Aleksandrov 	sg_ip = star_mp->addr;
324094b82fdSNikolay Aleksandrov 	for (pg = mlock_dereference(star_mp->ports, br);
325094b82fdSNikolay Aleksandrov 	     pg;
326094b82fdSNikolay Aleksandrov 	     pg = mlock_dereference(pg->next, br)) {
327094b82fdSNikolay Aleksandrov 		struct net_bridge_group_src *src_ent;
328094b82fdSNikolay Aleksandrov 
329094b82fdSNikolay Aleksandrov 		hlist_for_each_entry(src_ent, &pg->src_list, node) {
330094b82fdSNikolay Aleksandrov 			if (!(src_ent->flags & BR_SGRP_F_INSTALLED))
331094b82fdSNikolay Aleksandrov 				continue;
332094b82fdSNikolay Aleksandrov 			sg_ip.src = src_ent->addr.src;
333094b82fdSNikolay Aleksandrov 			sg_mp = br_mdb_ip_get(br, &sg_ip);
334094b82fdSNikolay Aleksandrov 			if (!sg_mp)
335094b82fdSNikolay Aleksandrov 				continue;
336094b82fdSNikolay Aleksandrov 			sg_mp->host_joined = star_mp->host_joined;
337094b82fdSNikolay Aleksandrov 		}
338094b82fdSNikolay Aleksandrov 	}
339094b82fdSNikolay Aleksandrov }
340094b82fdSNikolay Aleksandrov 
3418266a049SNikolay Aleksandrov static void br_multicast_sg_del_exclude_ports(struct net_bridge_mdb_entry *sgmp)
3428266a049SNikolay Aleksandrov {
3438266a049SNikolay Aleksandrov 	struct net_bridge_port_group __rcu **pp;
3448266a049SNikolay Aleksandrov 	struct net_bridge_port_group *p;
3458266a049SNikolay Aleksandrov 
3468266a049SNikolay Aleksandrov 	/* *,G exclude ports are only added to S,G entries */
3478266a049SNikolay Aleksandrov 	if (WARN_ON(br_multicast_is_star_g(&sgmp->addr)))
3488266a049SNikolay Aleksandrov 		return;
3498266a049SNikolay Aleksandrov 
3508266a049SNikolay Aleksandrov 	/* we need the STAR_EXCLUDE ports if there are non-STAR_EXCLUDE ports
3518266a049SNikolay Aleksandrov 	 * we should ignore perm entries since they're managed by user-space
3528266a049SNikolay Aleksandrov 	 */
3538266a049SNikolay Aleksandrov 	for (pp = &sgmp->ports;
3548266a049SNikolay Aleksandrov 	     (p = mlock_dereference(*pp, sgmp->br)) != NULL;
3558266a049SNikolay Aleksandrov 	     pp = &p->next)
3568266a049SNikolay Aleksandrov 		if (!(p->flags & (MDB_PG_FLAGS_STAR_EXCL |
3578266a049SNikolay Aleksandrov 				  MDB_PG_FLAGS_PERMANENT)))
3588266a049SNikolay Aleksandrov 			return;
3598266a049SNikolay Aleksandrov 
360094b82fdSNikolay Aleksandrov 	/* currently the host can only have joined the *,G which means
361094b82fdSNikolay Aleksandrov 	 * we treat it as EXCLUDE {}, so for an S,G it's considered a
362094b82fdSNikolay Aleksandrov 	 * STAR_EXCLUDE entry and we can safely leave it
363094b82fdSNikolay Aleksandrov 	 */
364094b82fdSNikolay Aleksandrov 	sgmp->host_joined = false;
365094b82fdSNikolay Aleksandrov 
3668266a049SNikolay Aleksandrov 	for (pp = &sgmp->ports;
3678266a049SNikolay Aleksandrov 	     (p = mlock_dereference(*pp, sgmp->br)) != NULL;) {
3688266a049SNikolay Aleksandrov 		if (!(p->flags & MDB_PG_FLAGS_PERMANENT))
3698266a049SNikolay Aleksandrov 			br_multicast_del_pg(sgmp, p, pp);
3708266a049SNikolay Aleksandrov 		else
3718266a049SNikolay Aleksandrov 			pp = &p->next;
3728266a049SNikolay Aleksandrov 	}
3738266a049SNikolay Aleksandrov }
3748266a049SNikolay Aleksandrov 
3758266a049SNikolay Aleksandrov void br_multicast_sg_add_exclude_ports(struct net_bridge_mdb_entry *star_mp,
3768266a049SNikolay Aleksandrov 				       struct net_bridge_port_group *sg)
3778266a049SNikolay Aleksandrov {
3788266a049SNikolay Aleksandrov 	struct net_bridge_port_group_sg_key sg_key;
3798266a049SNikolay Aleksandrov 	struct net_bridge *br = star_mp->br;
3808266a049SNikolay Aleksandrov 	struct net_bridge_port_group *pg;
3818266a049SNikolay Aleksandrov 
3828266a049SNikolay Aleksandrov 	if (WARN_ON(br_multicast_is_star_g(&sg->key.addr)))
3838266a049SNikolay Aleksandrov 		return;
3848266a049SNikolay Aleksandrov 	if (WARN_ON(!br_multicast_is_star_g(&star_mp->addr)))
3858266a049SNikolay Aleksandrov 		return;
3868266a049SNikolay Aleksandrov 
387094b82fdSNikolay Aleksandrov 	br_multicast_sg_host_state(star_mp, sg);
3888266a049SNikolay Aleksandrov 	memset(&sg_key, 0, sizeof(sg_key));
3898266a049SNikolay Aleksandrov 	sg_key.addr = sg->key.addr;
3908266a049SNikolay Aleksandrov 	/* we need to add all exclude ports to the S,G */
3918266a049SNikolay Aleksandrov 	for (pg = mlock_dereference(star_mp->ports, br);
3928266a049SNikolay Aleksandrov 	     pg;
3938266a049SNikolay Aleksandrov 	     pg = mlock_dereference(pg->next, br)) {
3948266a049SNikolay Aleksandrov 		struct net_bridge_port_group *src_pg;
3958266a049SNikolay Aleksandrov 
3968266a049SNikolay Aleksandrov 		if (pg == sg || pg->filter_mode == MCAST_INCLUDE)
3978266a049SNikolay Aleksandrov 			continue;
3988266a049SNikolay Aleksandrov 
3998266a049SNikolay Aleksandrov 		sg_key.port = pg->key.port;
4008266a049SNikolay Aleksandrov 		if (br_sg_port_find(br, &sg_key))
4018266a049SNikolay Aleksandrov 			continue;
4028266a049SNikolay Aleksandrov 
4038266a049SNikolay Aleksandrov 		src_pg = __br_multicast_add_group(br, pg->key.port,
4048266a049SNikolay Aleksandrov 						  &sg->key.addr,
4058266a049SNikolay Aleksandrov 						  sg->eth_addr,
4069116ffbfSNikolay Aleksandrov 						  MCAST_INCLUDE, false, false);
4078266a049SNikolay Aleksandrov 		if (IS_ERR_OR_NULL(src_pg) ||
4088266a049SNikolay Aleksandrov 		    src_pg->rt_protocol != RTPROT_KERNEL)
4098266a049SNikolay Aleksandrov 			continue;
4108266a049SNikolay Aleksandrov 		src_pg->flags |= MDB_PG_FLAGS_STAR_EXCL;
4118266a049SNikolay Aleksandrov 	}
4128266a049SNikolay Aleksandrov }
4138266a049SNikolay Aleksandrov 
414b0812368SNikolay Aleksandrov static void br_multicast_fwd_src_add(struct net_bridge_group_src *src)
415b0812368SNikolay Aleksandrov {
4168266a049SNikolay Aleksandrov 	struct net_bridge_mdb_entry *star_mp;
417b0812368SNikolay Aleksandrov 	struct net_bridge_port_group *sg;
418b0812368SNikolay Aleksandrov 	struct br_ip sg_ip;
419b0812368SNikolay Aleksandrov 
420b0812368SNikolay Aleksandrov 	if (src->flags & BR_SGRP_F_INSTALLED)
421b0812368SNikolay Aleksandrov 		return;
422b0812368SNikolay Aleksandrov 
423b0812368SNikolay Aleksandrov 	memset(&sg_ip, 0, sizeof(sg_ip));
424b0812368SNikolay Aleksandrov 	sg_ip = src->pg->key.addr;
425b0812368SNikolay Aleksandrov 	sg_ip.src = src->addr.src;
426b0812368SNikolay Aleksandrov 	sg = __br_multicast_add_group(src->br, src->pg->key.port, &sg_ip,
4279116ffbfSNikolay Aleksandrov 				      src->pg->eth_addr, MCAST_INCLUDE, false,
4289116ffbfSNikolay Aleksandrov 				      !timer_pending(&src->timer));
429b0812368SNikolay Aleksandrov 	if (IS_ERR_OR_NULL(sg))
430b0812368SNikolay Aleksandrov 		return;
431b0812368SNikolay Aleksandrov 	src->flags |= BR_SGRP_F_INSTALLED;
4328266a049SNikolay Aleksandrov 	sg->flags &= ~MDB_PG_FLAGS_STAR_EXCL;
433b0812368SNikolay Aleksandrov 
434b0812368SNikolay Aleksandrov 	/* if it was added by user-space as perm we can skip next steps */
435b0812368SNikolay Aleksandrov 	if (sg->rt_protocol != RTPROT_KERNEL &&
436b0812368SNikolay Aleksandrov 	    (sg->flags & MDB_PG_FLAGS_PERMANENT))
437b0812368SNikolay Aleksandrov 		return;
438b0812368SNikolay Aleksandrov 
439b0812368SNikolay Aleksandrov 	/* the kernel is now responsible for removing this S,G */
440b0812368SNikolay Aleksandrov 	del_timer(&sg->timer);
4418266a049SNikolay Aleksandrov 	star_mp = br_mdb_ip_get(src->br, &src->pg->key.addr);
4428266a049SNikolay Aleksandrov 	if (!star_mp)
4438266a049SNikolay Aleksandrov 		return;
4448266a049SNikolay Aleksandrov 
4458266a049SNikolay Aleksandrov 	br_multicast_sg_add_exclude_ports(star_mp, sg);
446b0812368SNikolay Aleksandrov }
447b0812368SNikolay Aleksandrov 
448d5a10222SNikolay Aleksandrov static void br_multicast_fwd_src_remove(struct net_bridge_group_src *src,
449d5a10222SNikolay Aleksandrov 					bool fastleave)
450b0812368SNikolay Aleksandrov {
451b0812368SNikolay Aleksandrov 	struct net_bridge_port_group *p, *pg = src->pg;
452b0812368SNikolay Aleksandrov 	struct net_bridge_port_group __rcu **pp;
453b0812368SNikolay Aleksandrov 	struct net_bridge_mdb_entry *mp;
454b0812368SNikolay Aleksandrov 	struct br_ip sg_ip;
455b0812368SNikolay Aleksandrov 
456b0812368SNikolay Aleksandrov 	memset(&sg_ip, 0, sizeof(sg_ip));
457b0812368SNikolay Aleksandrov 	sg_ip = pg->key.addr;
458b0812368SNikolay Aleksandrov 	sg_ip.src = src->addr.src;
459b0812368SNikolay Aleksandrov 
460b0812368SNikolay Aleksandrov 	mp = br_mdb_ip_get(src->br, &sg_ip);
461b0812368SNikolay Aleksandrov 	if (!mp)
462b0812368SNikolay Aleksandrov 		return;
463b0812368SNikolay Aleksandrov 
464b0812368SNikolay Aleksandrov 	for (pp = &mp->ports;
465b0812368SNikolay Aleksandrov 	     (p = mlock_dereference(*pp, src->br)) != NULL;
466b0812368SNikolay Aleksandrov 	     pp = &p->next) {
467b0812368SNikolay Aleksandrov 		if (!br_port_group_equal(p, pg->key.port, pg->eth_addr))
468b0812368SNikolay Aleksandrov 			continue;
469b0812368SNikolay Aleksandrov 
470b0812368SNikolay Aleksandrov 		if (p->rt_protocol != RTPROT_KERNEL &&
471b0812368SNikolay Aleksandrov 		    (p->flags & MDB_PG_FLAGS_PERMANENT))
472b0812368SNikolay Aleksandrov 			break;
473b0812368SNikolay Aleksandrov 
474d5a10222SNikolay Aleksandrov 		if (fastleave)
475d5a10222SNikolay Aleksandrov 			p->flags |= MDB_PG_FLAGS_FAST_LEAVE;
476b0812368SNikolay Aleksandrov 		br_multicast_del_pg(mp, p, pp);
477b0812368SNikolay Aleksandrov 		break;
478b0812368SNikolay Aleksandrov 	}
479b0812368SNikolay Aleksandrov 	src->flags &= ~BR_SGRP_F_INSTALLED;
480b0812368SNikolay Aleksandrov }
481b0812368SNikolay Aleksandrov 
4829116ffbfSNikolay Aleksandrov /* install S,G and based on src's timer enable or disable forwarding */
483b0812368SNikolay Aleksandrov static void br_multicast_fwd_src_handle(struct net_bridge_group_src *src)
484b0812368SNikolay Aleksandrov {
4859116ffbfSNikolay Aleksandrov 	struct net_bridge_port_group_sg_key sg_key;
4869116ffbfSNikolay Aleksandrov 	struct net_bridge_port_group *sg;
4879116ffbfSNikolay Aleksandrov 	u8 old_flags;
4889116ffbfSNikolay Aleksandrov 
489b0812368SNikolay Aleksandrov 	br_multicast_fwd_src_add(src);
4909116ffbfSNikolay Aleksandrov 
4919116ffbfSNikolay Aleksandrov 	memset(&sg_key, 0, sizeof(sg_key));
4929116ffbfSNikolay Aleksandrov 	sg_key.addr = src->pg->key.addr;
4939116ffbfSNikolay Aleksandrov 	sg_key.addr.src = src->addr.src;
4949116ffbfSNikolay Aleksandrov 	sg_key.port = src->pg->key.port;
4959116ffbfSNikolay Aleksandrov 
4969116ffbfSNikolay Aleksandrov 	sg = br_sg_port_find(src->br, &sg_key);
4979116ffbfSNikolay Aleksandrov 	if (!sg || (sg->flags & MDB_PG_FLAGS_PERMANENT))
4989116ffbfSNikolay Aleksandrov 		return;
4999116ffbfSNikolay Aleksandrov 
5009116ffbfSNikolay Aleksandrov 	old_flags = sg->flags;
5019116ffbfSNikolay Aleksandrov 	if (timer_pending(&src->timer))
5029116ffbfSNikolay Aleksandrov 		sg->flags &= ~MDB_PG_FLAGS_BLOCKED;
5039116ffbfSNikolay Aleksandrov 	else
5049116ffbfSNikolay Aleksandrov 		sg->flags |= MDB_PG_FLAGS_BLOCKED;
5059116ffbfSNikolay Aleksandrov 
5069116ffbfSNikolay Aleksandrov 	if (old_flags != sg->flags) {
5079116ffbfSNikolay Aleksandrov 		struct net_bridge_mdb_entry *sg_mp;
5089116ffbfSNikolay Aleksandrov 
5099116ffbfSNikolay Aleksandrov 		sg_mp = br_mdb_ip_get(src->br, &sg_key.addr);
5109116ffbfSNikolay Aleksandrov 		if (!sg_mp)
5119116ffbfSNikolay Aleksandrov 			return;
5129116ffbfSNikolay Aleksandrov 		br_mdb_notify(src->br->dev, sg_mp, sg, RTM_NEWMDB);
5139116ffbfSNikolay Aleksandrov 	}
514b0812368SNikolay Aleksandrov }
515b0812368SNikolay Aleksandrov 
516e12cec65SNikolay Aleksandrov static void br_multicast_destroy_mdb_entry(struct net_bridge_mcast_gc *gc)
517e12cec65SNikolay Aleksandrov {
518e12cec65SNikolay Aleksandrov 	struct net_bridge_mdb_entry *mp;
519e12cec65SNikolay Aleksandrov 
520e12cec65SNikolay Aleksandrov 	mp = container_of(gc, struct net_bridge_mdb_entry, mcast_gc);
521e12cec65SNikolay Aleksandrov 	WARN_ON(!hlist_unhashed(&mp->mdb_node));
522e12cec65SNikolay Aleksandrov 	WARN_ON(mp->ports);
523e12cec65SNikolay Aleksandrov 
524e12cec65SNikolay Aleksandrov 	del_timer_sync(&mp->timer);
525e12cec65SNikolay Aleksandrov 	kfree_rcu(mp, rcu);
526e12cec65SNikolay Aleksandrov }
527e12cec65SNikolay Aleksandrov 
528e12cec65SNikolay Aleksandrov static void br_multicast_del_mdb_entry(struct net_bridge_mdb_entry *mp)
529e12cec65SNikolay Aleksandrov {
530e12cec65SNikolay Aleksandrov 	struct net_bridge *br = mp->br;
531e12cec65SNikolay Aleksandrov 
532e12cec65SNikolay Aleksandrov 	rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode,
533e12cec65SNikolay Aleksandrov 			       br_mdb_rht_params);
534e12cec65SNikolay Aleksandrov 	hlist_del_init_rcu(&mp->mdb_node);
535e12cec65SNikolay Aleksandrov 	hlist_add_head(&mp->mcast_gc.gc_node, &br->mcast_gc_list);
536e12cec65SNikolay Aleksandrov 	queue_work(system_long_wq, &br->mcast_gc_work);
537e12cec65SNikolay Aleksandrov }
538e12cec65SNikolay Aleksandrov 
53988c1f37fSAllen Pais static void br_multicast_group_expired(struct timer_list *t)
540eb1d1641SHerbert Xu {
54188c1f37fSAllen Pais 	struct net_bridge_mdb_entry *mp = from_timer(mp, t, timer);
542eb1d1641SHerbert Xu 	struct net_bridge *br = mp->br;
543eb1d1641SHerbert Xu 
544eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
545b0812368SNikolay Aleksandrov 	if (hlist_unhashed(&mp->mdb_node) || !netif_running(br->dev) ||
546b0812368SNikolay Aleksandrov 	    timer_pending(&mp->timer))
547eb1d1641SHerbert Xu 		goto out;
548eb1d1641SHerbert Xu 
5491bc844eeSNikolay Aleksandrov 	br_multicast_host_leave(mp, true);
550eb1d1641SHerbert Xu 
551eb1d1641SHerbert Xu 	if (mp->ports)
552eb1d1641SHerbert Xu 		goto out;
553e12cec65SNikolay Aleksandrov 	br_multicast_del_mdb_entry(mp);
554eb1d1641SHerbert Xu out:
555eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
556eb1d1641SHerbert Xu }
557eb1d1641SHerbert Xu 
558e12cec65SNikolay Aleksandrov static void br_multicast_destroy_group_src(struct net_bridge_mcast_gc *gc)
559e12cec65SNikolay Aleksandrov {
560e12cec65SNikolay Aleksandrov 	struct net_bridge_group_src *src;
561e12cec65SNikolay Aleksandrov 
562e12cec65SNikolay Aleksandrov 	src = container_of(gc, struct net_bridge_group_src, mcast_gc);
563e12cec65SNikolay Aleksandrov 	WARN_ON(!hlist_unhashed(&src->node));
564e12cec65SNikolay Aleksandrov 
565e12cec65SNikolay Aleksandrov 	del_timer_sync(&src->timer);
566e12cec65SNikolay Aleksandrov 	kfree_rcu(src, rcu);
567e12cec65SNikolay Aleksandrov }
568e12cec65SNikolay Aleksandrov 
569d5a10222SNikolay Aleksandrov void br_multicast_del_group_src(struct net_bridge_group_src *src,
570d5a10222SNikolay Aleksandrov 				bool fastleave)
5718b671779SNikolay Aleksandrov {
572085b53c8SNikolay Aleksandrov 	struct net_bridge *br = src->pg->key.port->br;
5738b671779SNikolay Aleksandrov 
574d5a10222SNikolay Aleksandrov 	br_multicast_fwd_src_remove(src, fastleave);
5758b671779SNikolay Aleksandrov 	hlist_del_init_rcu(&src->node);
5768b671779SNikolay Aleksandrov 	src->pg->src_ents--;
577e12cec65SNikolay Aleksandrov 	hlist_add_head(&src->mcast_gc.gc_node, &br->mcast_gc_list);
578e12cec65SNikolay Aleksandrov 	queue_work(system_long_wq, &br->mcast_gc_work);
579e12cec65SNikolay Aleksandrov }
580e12cec65SNikolay Aleksandrov 
581e12cec65SNikolay Aleksandrov static void br_multicast_destroy_port_group(struct net_bridge_mcast_gc *gc)
582e12cec65SNikolay Aleksandrov {
583e12cec65SNikolay Aleksandrov 	struct net_bridge_port_group *pg;
584e12cec65SNikolay Aleksandrov 
585e12cec65SNikolay Aleksandrov 	pg = container_of(gc, struct net_bridge_port_group, mcast_gc);
586e12cec65SNikolay Aleksandrov 	WARN_ON(!hlist_unhashed(&pg->mglist));
587e12cec65SNikolay Aleksandrov 	WARN_ON(!hlist_empty(&pg->src_list));
588e12cec65SNikolay Aleksandrov 
589e12cec65SNikolay Aleksandrov 	del_timer_sync(&pg->rexmit_timer);
590e12cec65SNikolay Aleksandrov 	del_timer_sync(&pg->timer);
591e12cec65SNikolay Aleksandrov 	kfree_rcu(pg, rcu);
5928b671779SNikolay Aleksandrov }
5938b671779SNikolay Aleksandrov 
594681590bdSNikolay Aleksandrov void br_multicast_del_pg(struct net_bridge_mdb_entry *mp,
595681590bdSNikolay Aleksandrov 			 struct net_bridge_port_group *pg,
596681590bdSNikolay Aleksandrov 			 struct net_bridge_port_group __rcu **pp)
597681590bdSNikolay Aleksandrov {
598085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
5998b671779SNikolay Aleksandrov 	struct net_bridge_group_src *ent;
6008b671779SNikolay Aleksandrov 	struct hlist_node *tmp;
601681590bdSNikolay Aleksandrov 
602681590bdSNikolay Aleksandrov 	rcu_assign_pointer(*pp, pg->next);
603681590bdSNikolay Aleksandrov 	hlist_del_init(&pg->mglist);
604baa74d39SNikolay Aleksandrov 	br_multicast_eht_clean_sets(pg);
6058b671779SNikolay Aleksandrov 	hlist_for_each_entry_safe(ent, tmp, &pg->src_list, node)
606d5a10222SNikolay Aleksandrov 		br_multicast_del_group_src(ent, false);
60781f19838SNikolay Aleksandrov 	br_mdb_notify(br->dev, mp, pg, RTM_DELMDB);
60874705582SNikolay Aleksandrov 	if (!br_multicast_is_star_g(&mp->addr)) {
60974705582SNikolay Aleksandrov 		rhashtable_remove_fast(&br->sg_port_tbl, &pg->rhnode,
61074705582SNikolay Aleksandrov 				       br_sg_port_rht_params);
6118266a049SNikolay Aleksandrov 		br_multicast_sg_del_exclude_ports(mp);
61274705582SNikolay Aleksandrov 	} else {
6138266a049SNikolay Aleksandrov 		br_multicast_star_g_handle_mode(pg, MCAST_INCLUDE);
61474705582SNikolay Aleksandrov 	}
615e12cec65SNikolay Aleksandrov 	hlist_add_head(&pg->mcast_gc.gc_node, &br->mcast_gc_list);
616e12cec65SNikolay Aleksandrov 	queue_work(system_long_wq, &br->mcast_gc_work);
617681590bdSNikolay Aleksandrov 
618681590bdSNikolay Aleksandrov 	if (!mp->ports && !mp->host_joined && netif_running(br->dev))
619681590bdSNikolay Aleksandrov 		mod_timer(&mp->timer, jiffies);
620681590bdSNikolay Aleksandrov }
621681590bdSNikolay Aleksandrov 
622681590bdSNikolay Aleksandrov static void br_multicast_find_del_pg(struct net_bridge *br,
623eb1d1641SHerbert Xu 				     struct net_bridge_port_group *pg)
624eb1d1641SHerbert Xu {
6258b671779SNikolay Aleksandrov 	struct net_bridge_port_group __rcu **pp;
626eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
627eb1d1641SHerbert Xu 	struct net_bridge_port_group *p;
628e8051688SEric Dumazet 
629085b53c8SNikolay Aleksandrov 	mp = br_mdb_ip_get(br, &pg->key.addr);
630eb1d1641SHerbert Xu 	if (WARN_ON(!mp))
631eb1d1641SHerbert Xu 		return;
632eb1d1641SHerbert Xu 
633e8051688SEric Dumazet 	for (pp = &mp->ports;
634e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
635e8051688SEric Dumazet 	     pp = &p->next) {
636eb1d1641SHerbert Xu 		if (p != pg)
637eb1d1641SHerbert Xu 			continue;
638eb1d1641SHerbert Xu 
639681590bdSNikolay Aleksandrov 		br_multicast_del_pg(mp, pg, pp);
640eb1d1641SHerbert Xu 		return;
641eb1d1641SHerbert Xu 	}
642eb1d1641SHerbert Xu 
643eb1d1641SHerbert Xu 	WARN_ON(1);
644eb1d1641SHerbert Xu }
645eb1d1641SHerbert Xu 
64688c1f37fSAllen Pais static void br_multicast_port_group_expired(struct timer_list *t)
647eb1d1641SHerbert Xu {
64888c1f37fSAllen Pais 	struct net_bridge_port_group *pg = from_timer(pg, t, timer);
649d6c33d67SNikolay Aleksandrov 	struct net_bridge_group_src *src_ent;
650085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
651d6c33d67SNikolay Aleksandrov 	struct hlist_node *tmp;
652d6c33d67SNikolay Aleksandrov 	bool changed;
653eb1d1641SHerbert Xu 
654eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
655eb1d1641SHerbert Xu 	if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
6569d06b6d8SElad Raz 	    hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT)
657eb1d1641SHerbert Xu 		goto out;
658eb1d1641SHerbert Xu 
659d6c33d67SNikolay Aleksandrov 	changed = !!(pg->filter_mode == MCAST_EXCLUDE);
660d6c33d67SNikolay Aleksandrov 	pg->filter_mode = MCAST_INCLUDE;
661d6c33d67SNikolay Aleksandrov 	hlist_for_each_entry_safe(src_ent, tmp, &pg->src_list, node) {
662d6c33d67SNikolay Aleksandrov 		if (!timer_pending(&src_ent->timer)) {
663d5a10222SNikolay Aleksandrov 			br_multicast_del_group_src(src_ent, false);
664d6c33d67SNikolay Aleksandrov 			changed = true;
665d6c33d67SNikolay Aleksandrov 		}
666d6c33d67SNikolay Aleksandrov 	}
667eb1d1641SHerbert Xu 
668d6c33d67SNikolay Aleksandrov 	if (hlist_empty(&pg->src_list)) {
669d6c33d67SNikolay Aleksandrov 		br_multicast_find_del_pg(br, pg);
670d6c33d67SNikolay Aleksandrov 	} else if (changed) {
671085b53c8SNikolay Aleksandrov 		struct net_bridge_mdb_entry *mp = br_mdb_ip_get(br, &pg->key.addr);
672d6c33d67SNikolay Aleksandrov 
6738266a049SNikolay Aleksandrov 		if (changed && br_multicast_is_star_g(&pg->key.addr))
6748266a049SNikolay Aleksandrov 			br_multicast_star_g_handle_mode(pg, MCAST_INCLUDE);
6758266a049SNikolay Aleksandrov 
676d6c33d67SNikolay Aleksandrov 		if (WARN_ON(!mp))
677d6c33d67SNikolay Aleksandrov 			goto out;
678d6c33d67SNikolay Aleksandrov 		br_mdb_notify(br->dev, mp, pg, RTM_NEWMDB);
679d6c33d67SNikolay Aleksandrov 	}
680eb1d1641SHerbert Xu out:
681eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
682eb1d1641SHerbert Xu }
683eb1d1641SHerbert Xu 
684e12cec65SNikolay Aleksandrov static void br_multicast_gc(struct hlist_head *head)
685e12cec65SNikolay Aleksandrov {
686e12cec65SNikolay Aleksandrov 	struct net_bridge_mcast_gc *gcent;
687e12cec65SNikolay Aleksandrov 	struct hlist_node *tmp;
688e12cec65SNikolay Aleksandrov 
689e12cec65SNikolay Aleksandrov 	hlist_for_each_entry_safe(gcent, tmp, head, gc_node) {
690e12cec65SNikolay Aleksandrov 		hlist_del_init(&gcent->gc_node);
691e12cec65SNikolay Aleksandrov 		gcent->destroy(gcent);
692e12cec65SNikolay Aleksandrov 	}
693e12cec65SNikolay Aleksandrov }
694e12cec65SNikolay Aleksandrov 
6958ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
696438ef2d0SNikolay Aleksandrov 						    struct net_bridge_port_group *pg,
697438ef2d0SNikolay Aleksandrov 						    __be32 ip_dst, __be32 group,
698438ef2d0SNikolay Aleksandrov 						    bool with_srcs, bool over_lmqt,
69942c11ccfSNikolay Aleksandrov 						    u8 sflag, u8 *igmp_type,
70042c11ccfSNikolay Aleksandrov 						    bool *need_rexmit)
701eb1d1641SHerbert Xu {
702*d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = &br->multicast_ctx;
703085b53c8SNikolay Aleksandrov 	struct net_bridge_port *p = pg ? pg->key.port : NULL;
704438ef2d0SNikolay Aleksandrov 	struct net_bridge_group_src *ent;
705438ef2d0SNikolay Aleksandrov 	size_t pkt_size, igmp_hdr_size;
706438ef2d0SNikolay Aleksandrov 	unsigned long now = jiffies;
7075e923585SNikolay Aleksandrov 	struct igmpv3_query *ihv3;
708438ef2d0SNikolay Aleksandrov 	void *csum_start = NULL;
709438ef2d0SNikolay Aleksandrov 	__sum16 *csum = NULL;
710eb1d1641SHerbert Xu 	struct sk_buff *skb;
711eb1d1641SHerbert Xu 	struct igmphdr *ih;
712eb1d1641SHerbert Xu 	struct ethhdr *eth;
713438ef2d0SNikolay Aleksandrov 	unsigned long lmqt;
714eb1d1641SHerbert Xu 	struct iphdr *iph;
715438ef2d0SNikolay Aleksandrov 	u16 lmqt_srcs = 0;
716eb1d1641SHerbert Xu 
7175e923585SNikolay Aleksandrov 	igmp_hdr_size = sizeof(*ih);
718*d3d065c0SNikolay Aleksandrov 	if (brmctx->multicast_igmp_version == 3) {
7195e923585SNikolay Aleksandrov 		igmp_hdr_size = sizeof(*ihv3);
720438ef2d0SNikolay Aleksandrov 		if (pg && with_srcs) {
721*d3d065c0SNikolay Aleksandrov 			lmqt = now + (brmctx->multicast_last_member_interval *
722*d3d065c0SNikolay Aleksandrov 				      brmctx->multicast_last_member_count);
723438ef2d0SNikolay Aleksandrov 			hlist_for_each_entry(ent, &pg->src_list, node) {
724438ef2d0SNikolay Aleksandrov 				if (over_lmqt == time_after(ent->timer.expires,
725438ef2d0SNikolay Aleksandrov 							    lmqt) &&
726438ef2d0SNikolay Aleksandrov 				    ent->src_query_rexmit_cnt > 0)
727438ef2d0SNikolay Aleksandrov 					lmqt_srcs++;
728438ef2d0SNikolay Aleksandrov 			}
729438ef2d0SNikolay Aleksandrov 
730438ef2d0SNikolay Aleksandrov 			if (!lmqt_srcs)
731438ef2d0SNikolay Aleksandrov 				return NULL;
732438ef2d0SNikolay Aleksandrov 			igmp_hdr_size += lmqt_srcs * sizeof(__be32);
733438ef2d0SNikolay Aleksandrov 		}
734438ef2d0SNikolay Aleksandrov 	}
735438ef2d0SNikolay Aleksandrov 
736438ef2d0SNikolay Aleksandrov 	pkt_size = sizeof(*eth) + sizeof(*iph) + 4 + igmp_hdr_size;
737438ef2d0SNikolay Aleksandrov 	if ((p && pkt_size > p->dev->mtu) ||
738438ef2d0SNikolay Aleksandrov 	    pkt_size > br->dev->mtu)
739438ef2d0SNikolay Aleksandrov 		return NULL;
740438ef2d0SNikolay Aleksandrov 
741438ef2d0SNikolay Aleksandrov 	skb = netdev_alloc_skb_ip_align(br->dev, pkt_size);
742eb1d1641SHerbert Xu 	if (!skb)
743eb1d1641SHerbert Xu 		goto out;
744eb1d1641SHerbert Xu 
745eb1d1641SHerbert Xu 	skb->protocol = htons(ETH_P_IP);
746eb1d1641SHerbert Xu 
747eb1d1641SHerbert Xu 	skb_reset_mac_header(skb);
748eb1d1641SHerbert Xu 	eth = eth_hdr(skb);
749eb1d1641SHerbert Xu 
750e5a727f6SJoe Perches 	ether_addr_copy(eth->h_source, br->dev->dev_addr);
751438ef2d0SNikolay Aleksandrov 	ip_eth_mc_map(ip_dst, eth->h_dest);
752eb1d1641SHerbert Xu 	eth->h_proto = htons(ETH_P_IP);
753eb1d1641SHerbert Xu 	skb_put(skb, sizeof(*eth));
754eb1d1641SHerbert Xu 
755eb1d1641SHerbert Xu 	skb_set_network_header(skb, skb->len);
756eb1d1641SHerbert Xu 	iph = ip_hdr(skb);
757438ef2d0SNikolay Aleksandrov 	iph->tot_len = htons(pkt_size - sizeof(*eth));
758eb1d1641SHerbert Xu 
759eb1d1641SHerbert Xu 	iph->version = 4;
760eb1d1641SHerbert Xu 	iph->ihl = 6;
761eb1d1641SHerbert Xu 	iph->tos = 0xc0;
762eb1d1641SHerbert Xu 	iph->id = 0;
763eb1d1641SHerbert Xu 	iph->frag_off = htons(IP_DF);
764eb1d1641SHerbert Xu 	iph->ttl = 1;
765eb1d1641SHerbert Xu 	iph->protocol = IPPROTO_IGMP;
766675779adSNikolay Aleksandrov 	iph->saddr = br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR) ?
7671c8ad5bfSCong Wang 		     inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0;
768438ef2d0SNikolay Aleksandrov 	iph->daddr = ip_dst;
769eb1d1641SHerbert Xu 	((u8 *)&iph[1])[0] = IPOPT_RA;
770eb1d1641SHerbert Xu 	((u8 *)&iph[1])[1] = 4;
771eb1d1641SHerbert Xu 	((u8 *)&iph[1])[2] = 0;
772eb1d1641SHerbert Xu 	((u8 *)&iph[1])[3] = 0;
773eb1d1641SHerbert Xu 	ip_send_check(iph);
774eb1d1641SHerbert Xu 	skb_put(skb, 24);
775eb1d1641SHerbert Xu 
776eb1d1641SHerbert Xu 	skb_set_transport_header(skb, skb->len);
7771080ab95SNikolay Aleksandrov 	*igmp_type = IGMP_HOST_MEMBERSHIP_QUERY;
7785e923585SNikolay Aleksandrov 
779*d3d065c0SNikolay Aleksandrov 	switch (brmctx->multicast_igmp_version) {
7805e923585SNikolay Aleksandrov 	case 2:
7815e923585SNikolay Aleksandrov 		ih = igmp_hdr(skb);
782eb1d1641SHerbert Xu 		ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
783*d3d065c0SNikolay Aleksandrov 		ih->code = (group ? brmctx->multicast_last_member_interval :
784*d3d065c0SNikolay Aleksandrov 				    brmctx->multicast_query_response_interval) /
785eb1d1641SHerbert Xu 			   (HZ / IGMP_TIMER_SCALE);
786eb1d1641SHerbert Xu 		ih->group = group;
787eb1d1641SHerbert Xu 		ih->csum = 0;
788438ef2d0SNikolay Aleksandrov 		csum = &ih->csum;
789438ef2d0SNikolay Aleksandrov 		csum_start = (void *)ih;
7905e923585SNikolay Aleksandrov 		break;
7915e923585SNikolay Aleksandrov 	case 3:
7925e923585SNikolay Aleksandrov 		ihv3 = igmpv3_query_hdr(skb);
7935e923585SNikolay Aleksandrov 		ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY;
794*d3d065c0SNikolay Aleksandrov 		ihv3->code = (group ? brmctx->multicast_last_member_interval :
795*d3d065c0SNikolay Aleksandrov 				      brmctx->multicast_query_response_interval) /
7965e923585SNikolay Aleksandrov 			     (HZ / IGMP_TIMER_SCALE);
7975e923585SNikolay Aleksandrov 		ihv3->group = group;
798*d3d065c0SNikolay Aleksandrov 		ihv3->qqic = brmctx->multicast_query_interval / HZ;
799438ef2d0SNikolay Aleksandrov 		ihv3->nsrcs = htons(lmqt_srcs);
8005e923585SNikolay Aleksandrov 		ihv3->resv = 0;
801438ef2d0SNikolay Aleksandrov 		ihv3->suppress = sflag;
8025e923585SNikolay Aleksandrov 		ihv3->qrv = 2;
8035e923585SNikolay Aleksandrov 		ihv3->csum = 0;
804438ef2d0SNikolay Aleksandrov 		csum = &ihv3->csum;
805438ef2d0SNikolay Aleksandrov 		csum_start = (void *)ihv3;
806438ef2d0SNikolay Aleksandrov 		if (!pg || !with_srcs)
807438ef2d0SNikolay Aleksandrov 			break;
808438ef2d0SNikolay Aleksandrov 
809438ef2d0SNikolay Aleksandrov 		lmqt_srcs = 0;
810438ef2d0SNikolay Aleksandrov 		hlist_for_each_entry(ent, &pg->src_list, node) {
811438ef2d0SNikolay Aleksandrov 			if (over_lmqt == time_after(ent->timer.expires,
812438ef2d0SNikolay Aleksandrov 						    lmqt) &&
813438ef2d0SNikolay Aleksandrov 			    ent->src_query_rexmit_cnt > 0) {
814deb96566SNikolay Aleksandrov 				ihv3->srcs[lmqt_srcs++] = ent->addr.src.ip4;
815438ef2d0SNikolay Aleksandrov 				ent->src_query_rexmit_cnt--;
81642c11ccfSNikolay Aleksandrov 				if (need_rexmit && ent->src_query_rexmit_cnt)
81742c11ccfSNikolay Aleksandrov 					*need_rexmit = true;
818438ef2d0SNikolay Aleksandrov 			}
819438ef2d0SNikolay Aleksandrov 		}
820438ef2d0SNikolay Aleksandrov 		if (WARN_ON(lmqt_srcs != ntohs(ihv3->nsrcs))) {
821438ef2d0SNikolay Aleksandrov 			kfree_skb(skb);
822438ef2d0SNikolay Aleksandrov 			return NULL;
823438ef2d0SNikolay Aleksandrov 		}
8245e923585SNikolay Aleksandrov 		break;
8255e923585SNikolay Aleksandrov 	}
826eb1d1641SHerbert Xu 
827438ef2d0SNikolay Aleksandrov 	if (WARN_ON(!csum || !csum_start)) {
828438ef2d0SNikolay Aleksandrov 		kfree_skb(skb);
829438ef2d0SNikolay Aleksandrov 		return NULL;
830438ef2d0SNikolay Aleksandrov 	}
831438ef2d0SNikolay Aleksandrov 
832438ef2d0SNikolay Aleksandrov 	*csum = ip_compute_csum(csum_start, igmp_hdr_size);
8335e923585SNikolay Aleksandrov 	skb_put(skb, igmp_hdr_size);
834eb1d1641SHerbert Xu 	__skb_pull(skb, sizeof(*eth));
835eb1d1641SHerbert Xu 
836eb1d1641SHerbert Xu out:
837eb1d1641SHerbert Xu 	return skb;
838eb1d1641SHerbert Xu }
839eb1d1641SHerbert Xu 
840dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
84108b202b6SYOSHIFUJI Hideaki static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
842438ef2d0SNikolay Aleksandrov 						    struct net_bridge_port_group *pg,
843438ef2d0SNikolay Aleksandrov 						    const struct in6_addr *ip6_dst,
844438ef2d0SNikolay Aleksandrov 						    const struct in6_addr *group,
845438ef2d0SNikolay Aleksandrov 						    bool with_srcs, bool over_llqt,
84642c11ccfSNikolay Aleksandrov 						    u8 sflag, u8 *igmp_type,
84742c11ccfSNikolay Aleksandrov 						    bool *need_rexmit)
84808b202b6SYOSHIFUJI Hideaki {
849*d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = &br->multicast_ctx;
850085b53c8SNikolay Aleksandrov 	struct net_bridge_port *p = pg ? pg->key.port : NULL;
851438ef2d0SNikolay Aleksandrov 	struct net_bridge_group_src *ent;
852438ef2d0SNikolay Aleksandrov 	size_t pkt_size, mld_hdr_size;
853438ef2d0SNikolay Aleksandrov 	unsigned long now = jiffies;
854aa2ae3e7SNikolay Aleksandrov 	struct mld2_query *mld2q;
855438ef2d0SNikolay Aleksandrov 	void *csum_start = NULL;
856aa2ae3e7SNikolay Aleksandrov 	unsigned long interval;
857438ef2d0SNikolay Aleksandrov 	__sum16 *csum = NULL;
85808b202b6SYOSHIFUJI Hideaki 	struct ipv6hdr *ip6h;
85908b202b6SYOSHIFUJI Hideaki 	struct mld_msg *mldq;
860aa2ae3e7SNikolay Aleksandrov 	struct sk_buff *skb;
861438ef2d0SNikolay Aleksandrov 	unsigned long llqt;
86208b202b6SYOSHIFUJI Hideaki 	struct ethhdr *eth;
863438ef2d0SNikolay Aleksandrov 	u16 llqt_srcs = 0;
86408b202b6SYOSHIFUJI Hideaki 	u8 *hopopt;
86508b202b6SYOSHIFUJI Hideaki 
866aa2ae3e7SNikolay Aleksandrov 	mld_hdr_size = sizeof(*mldq);
867*d3d065c0SNikolay Aleksandrov 	if (brmctx->multicast_mld_version == 2) {
868aa2ae3e7SNikolay Aleksandrov 		mld_hdr_size = sizeof(*mld2q);
869438ef2d0SNikolay Aleksandrov 		if (pg && with_srcs) {
870*d3d065c0SNikolay Aleksandrov 			llqt = now + (brmctx->multicast_last_member_interval *
871*d3d065c0SNikolay Aleksandrov 				      brmctx->multicast_last_member_count);
872438ef2d0SNikolay Aleksandrov 			hlist_for_each_entry(ent, &pg->src_list, node) {
873438ef2d0SNikolay Aleksandrov 				if (over_llqt == time_after(ent->timer.expires,
874438ef2d0SNikolay Aleksandrov 							    llqt) &&
875438ef2d0SNikolay Aleksandrov 				    ent->src_query_rexmit_cnt > 0)
876438ef2d0SNikolay Aleksandrov 					llqt_srcs++;
877438ef2d0SNikolay Aleksandrov 			}
878438ef2d0SNikolay Aleksandrov 
879438ef2d0SNikolay Aleksandrov 			if (!llqt_srcs)
880438ef2d0SNikolay Aleksandrov 				return NULL;
881438ef2d0SNikolay Aleksandrov 			mld_hdr_size += llqt_srcs * sizeof(struct in6_addr);
882438ef2d0SNikolay Aleksandrov 		}
883438ef2d0SNikolay Aleksandrov 	}
884438ef2d0SNikolay Aleksandrov 
885438ef2d0SNikolay Aleksandrov 	pkt_size = sizeof(*eth) + sizeof(*ip6h) + 8 + mld_hdr_size;
886438ef2d0SNikolay Aleksandrov 	if ((p && pkt_size > p->dev->mtu) ||
887438ef2d0SNikolay Aleksandrov 	    pkt_size > br->dev->mtu)
888438ef2d0SNikolay Aleksandrov 		return NULL;
889438ef2d0SNikolay Aleksandrov 
890438ef2d0SNikolay Aleksandrov 	skb = netdev_alloc_skb_ip_align(br->dev, pkt_size);
89108b202b6SYOSHIFUJI Hideaki 	if (!skb)
89208b202b6SYOSHIFUJI Hideaki 		goto out;
89308b202b6SYOSHIFUJI Hideaki 
89408b202b6SYOSHIFUJI Hideaki 	skb->protocol = htons(ETH_P_IPV6);
89508b202b6SYOSHIFUJI Hideaki 
89608b202b6SYOSHIFUJI Hideaki 	/* Ethernet header */
89708b202b6SYOSHIFUJI Hideaki 	skb_reset_mac_header(skb);
89808b202b6SYOSHIFUJI Hideaki 	eth = eth_hdr(skb);
89908b202b6SYOSHIFUJI Hideaki 
900e5a727f6SJoe Perches 	ether_addr_copy(eth->h_source, br->dev->dev_addr);
90108b202b6SYOSHIFUJI Hideaki 	eth->h_proto = htons(ETH_P_IPV6);
90208b202b6SYOSHIFUJI Hideaki 	skb_put(skb, sizeof(*eth));
90308b202b6SYOSHIFUJI Hideaki 
90408b202b6SYOSHIFUJI Hideaki 	/* IPv6 header + HbH option */
90508b202b6SYOSHIFUJI Hideaki 	skb_set_network_header(skb, skb->len);
90608b202b6SYOSHIFUJI Hideaki 	ip6h = ipv6_hdr(skb);
90708b202b6SYOSHIFUJI Hideaki 
90808b202b6SYOSHIFUJI Hideaki 	*(__force __be32 *)ip6h = htonl(0x60000000);
909aa2ae3e7SNikolay Aleksandrov 	ip6h->payload_len = htons(8 + mld_hdr_size);
91008b202b6SYOSHIFUJI Hideaki 	ip6h->nexthdr = IPPROTO_HOPOPTS;
91108b202b6SYOSHIFUJI Hideaki 	ip6h->hop_limit = 1;
912438ef2d0SNikolay Aleksandrov 	ip6h->daddr = *ip6_dst;
913d1d81d4cSUlrich Weber 	if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
914d1d81d4cSUlrich Weber 			       &ip6h->saddr)) {
915d1d81d4cSUlrich Weber 		kfree_skb(skb);
916675779adSNikolay Aleksandrov 		br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, false);
917d1d81d4cSUlrich Weber 		return NULL;
918d1d81d4cSUlrich Weber 	}
9190888d5f3Sdaniel 
920675779adSNikolay Aleksandrov 	br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true);
92136cff5a1SLinus Lüssing 	ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
92208b202b6SYOSHIFUJI Hideaki 
92308b202b6SYOSHIFUJI Hideaki 	hopopt = (u8 *)(ip6h + 1);
92408b202b6SYOSHIFUJI Hideaki 	hopopt[0] = IPPROTO_ICMPV6;		/* next hdr */
92508b202b6SYOSHIFUJI Hideaki 	hopopt[1] = 0;				/* length of HbH */
92608b202b6SYOSHIFUJI Hideaki 	hopopt[2] = IPV6_TLV_ROUTERALERT;	/* Router Alert */
92708b202b6SYOSHIFUJI Hideaki 	hopopt[3] = 2;				/* Length of RA Option */
92808b202b6SYOSHIFUJI Hideaki 	hopopt[4] = 0;				/* Type = 0x0000 (MLD) */
92908b202b6SYOSHIFUJI Hideaki 	hopopt[5] = 0;
9301de5a71cSEldad Zack 	hopopt[6] = IPV6_TLV_PAD1;		/* Pad1 */
9311de5a71cSEldad Zack 	hopopt[7] = IPV6_TLV_PAD1;		/* Pad1 */
93208b202b6SYOSHIFUJI Hideaki 
93308b202b6SYOSHIFUJI Hideaki 	skb_put(skb, sizeof(*ip6h) + 8);
93408b202b6SYOSHIFUJI Hideaki 
93508b202b6SYOSHIFUJI Hideaki 	/* ICMPv6 */
93608b202b6SYOSHIFUJI Hideaki 	skb_set_transport_header(skb, skb->len);
937438ef2d0SNikolay Aleksandrov 	interval = ipv6_addr_any(group) ?
938*d3d065c0SNikolay Aleksandrov 			brmctx->multicast_query_response_interval :
939*d3d065c0SNikolay Aleksandrov 			brmctx->multicast_last_member_interval;
9401080ab95SNikolay Aleksandrov 	*igmp_type = ICMPV6_MGM_QUERY;
941*d3d065c0SNikolay Aleksandrov 	switch (brmctx->multicast_mld_version) {
942aa2ae3e7SNikolay Aleksandrov 	case 1:
943aa2ae3e7SNikolay Aleksandrov 		mldq = (struct mld_msg *)icmp6_hdr(skb);
94408b202b6SYOSHIFUJI Hideaki 		mldq->mld_type = ICMPV6_MGM_QUERY;
94508b202b6SYOSHIFUJI Hideaki 		mldq->mld_code = 0;
94608b202b6SYOSHIFUJI Hideaki 		mldq->mld_cksum = 0;
94708b202b6SYOSHIFUJI Hideaki 		mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
94808b202b6SYOSHIFUJI Hideaki 		mldq->mld_reserved = 0;
949438ef2d0SNikolay Aleksandrov 		mldq->mld_mca = *group;
950438ef2d0SNikolay Aleksandrov 		csum = &mldq->mld_cksum;
951438ef2d0SNikolay Aleksandrov 		csum_start = (void *)mldq;
952aa2ae3e7SNikolay Aleksandrov 		break;
953aa2ae3e7SNikolay Aleksandrov 	case 2:
954aa2ae3e7SNikolay Aleksandrov 		mld2q = (struct mld2_query *)icmp6_hdr(skb);
95553631a5fSLance Richardson 		mld2q->mld2q_mrc = htons((u16)jiffies_to_msecs(interval));
956aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_type = ICMPV6_MGM_QUERY;
957aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_code = 0;
958aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_cksum = 0;
959aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_resv1 = 0;
960aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_resv2 = 0;
961438ef2d0SNikolay Aleksandrov 		mld2q->mld2q_suppress = sflag;
962aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_qrv = 2;
963438ef2d0SNikolay Aleksandrov 		mld2q->mld2q_nsrcs = htons(llqt_srcs);
964*d3d065c0SNikolay Aleksandrov 		mld2q->mld2q_qqic = brmctx->multicast_query_interval / HZ;
965438ef2d0SNikolay Aleksandrov 		mld2q->mld2q_mca = *group;
966438ef2d0SNikolay Aleksandrov 		csum = &mld2q->mld2q_cksum;
967438ef2d0SNikolay Aleksandrov 		csum_start = (void *)mld2q;
968438ef2d0SNikolay Aleksandrov 		if (!pg || !with_srcs)
969438ef2d0SNikolay Aleksandrov 			break;
970438ef2d0SNikolay Aleksandrov 
971438ef2d0SNikolay Aleksandrov 		llqt_srcs = 0;
972438ef2d0SNikolay Aleksandrov 		hlist_for_each_entry(ent, &pg->src_list, node) {
973438ef2d0SNikolay Aleksandrov 			if (over_llqt == time_after(ent->timer.expires,
974438ef2d0SNikolay Aleksandrov 						    llqt) &&
975438ef2d0SNikolay Aleksandrov 			    ent->src_query_rexmit_cnt > 0) {
976deb96566SNikolay Aleksandrov 				mld2q->mld2q_srcs[llqt_srcs++] = ent->addr.src.ip6;
977438ef2d0SNikolay Aleksandrov 				ent->src_query_rexmit_cnt--;
97842c11ccfSNikolay Aleksandrov 				if (need_rexmit && ent->src_query_rexmit_cnt)
97942c11ccfSNikolay Aleksandrov 					*need_rexmit = true;
980438ef2d0SNikolay Aleksandrov 			}
981438ef2d0SNikolay Aleksandrov 		}
982438ef2d0SNikolay Aleksandrov 		if (WARN_ON(llqt_srcs != ntohs(mld2q->mld2q_nsrcs))) {
983438ef2d0SNikolay Aleksandrov 			kfree_skb(skb);
984438ef2d0SNikolay Aleksandrov 			return NULL;
985438ef2d0SNikolay Aleksandrov 		}
986aa2ae3e7SNikolay Aleksandrov 		break;
987aa2ae3e7SNikolay Aleksandrov 	}
98808b202b6SYOSHIFUJI Hideaki 
989438ef2d0SNikolay Aleksandrov 	if (WARN_ON(!csum || !csum_start)) {
990438ef2d0SNikolay Aleksandrov 		kfree_skb(skb);
991438ef2d0SNikolay Aleksandrov 		return NULL;
992438ef2d0SNikolay Aleksandrov 	}
993438ef2d0SNikolay Aleksandrov 
994438ef2d0SNikolay Aleksandrov 	*csum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, mld_hdr_size,
995438ef2d0SNikolay Aleksandrov 				IPPROTO_ICMPV6,
996438ef2d0SNikolay Aleksandrov 				csum_partial(csum_start, mld_hdr_size, 0));
997438ef2d0SNikolay Aleksandrov 	skb_put(skb, mld_hdr_size);
99808b202b6SYOSHIFUJI Hideaki 	__skb_pull(skb, sizeof(*eth));
99908b202b6SYOSHIFUJI Hideaki 
100008b202b6SYOSHIFUJI Hideaki out:
100108b202b6SYOSHIFUJI Hideaki 	return skb;
100208b202b6SYOSHIFUJI Hideaki }
100308b202b6SYOSHIFUJI Hideaki #endif
100408b202b6SYOSHIFUJI Hideaki 
10058ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
1006438ef2d0SNikolay Aleksandrov 						struct net_bridge_port_group *pg,
1007438ef2d0SNikolay Aleksandrov 						struct br_ip *ip_dst,
1008438ef2d0SNikolay Aleksandrov 						struct br_ip *group,
1009438ef2d0SNikolay Aleksandrov 						bool with_srcs, bool over_lmqt,
101042c11ccfSNikolay Aleksandrov 						u8 sflag, u8 *igmp_type,
101142c11ccfSNikolay Aleksandrov 						bool *need_rexmit)
10128ef2a9a5SYOSHIFUJI Hideaki {
1013438ef2d0SNikolay Aleksandrov 	__be32 ip4_dst;
1014438ef2d0SNikolay Aleksandrov 
1015438ef2d0SNikolay Aleksandrov 	switch (group->proto) {
10168ef2a9a5SYOSHIFUJI Hideaki 	case htons(ETH_P_IP):
1017eab3227bSNikolay Aleksandrov 		ip4_dst = ip_dst ? ip_dst->dst.ip4 : htonl(INADDR_ALLHOSTS_GROUP);
1018438ef2d0SNikolay Aleksandrov 		return br_ip4_multicast_alloc_query(br, pg,
1019eab3227bSNikolay Aleksandrov 						    ip4_dst, group->dst.ip4,
1020438ef2d0SNikolay Aleksandrov 						    with_srcs, over_lmqt,
102142c11ccfSNikolay Aleksandrov 						    sflag, igmp_type,
102242c11ccfSNikolay Aleksandrov 						    need_rexmit);
1023dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1024438ef2d0SNikolay Aleksandrov 	case htons(ETH_P_IPV6): {
1025438ef2d0SNikolay Aleksandrov 		struct in6_addr ip6_dst;
1026438ef2d0SNikolay Aleksandrov 
1027438ef2d0SNikolay Aleksandrov 		if (ip_dst)
1028eab3227bSNikolay Aleksandrov 			ip6_dst = ip_dst->dst.ip6;
1029438ef2d0SNikolay Aleksandrov 		else
1030438ef2d0SNikolay Aleksandrov 			ipv6_addr_set(&ip6_dst, htonl(0xff020000), 0, 0,
1031438ef2d0SNikolay Aleksandrov 				      htonl(1));
1032438ef2d0SNikolay Aleksandrov 
1033438ef2d0SNikolay Aleksandrov 		return br_ip6_multicast_alloc_query(br, pg,
1034eab3227bSNikolay Aleksandrov 						    &ip6_dst, &group->dst.ip6,
1035438ef2d0SNikolay Aleksandrov 						    with_srcs, over_lmqt,
103642c11ccfSNikolay Aleksandrov 						    sflag, igmp_type,
103742c11ccfSNikolay Aleksandrov 						    need_rexmit);
1038438ef2d0SNikolay Aleksandrov 	}
103908b202b6SYOSHIFUJI Hideaki #endif
10408ef2a9a5SYOSHIFUJI Hideaki 	}
10418ef2a9a5SYOSHIFUJI Hideaki 	return NULL;
10428ef2a9a5SYOSHIFUJI Hideaki }
10438ef2a9a5SYOSHIFUJI Hideaki 
1044cfd56754SCong Wang struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
10455e923585SNikolay Aleksandrov 						    struct br_ip *group)
1046eb1d1641SHerbert Xu {
1047eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
10484c0833bcSTobias Klauser 	int err;
1049eb1d1641SHerbert Xu 
105019e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip_get(br, group);
105119e3a9c9SNikolay Aleksandrov 	if (mp)
105219e3a9c9SNikolay Aleksandrov 		return mp;
1053eb1d1641SHerbert Xu 
105419e3a9c9SNikolay Aleksandrov 	if (atomic_read(&br->mdb_hash_tbl.nelems) >= br->hash_max) {
105519e3a9c9SNikolay Aleksandrov 		br_opt_toggle(br, BROPT_MULTICAST_ENABLED, false);
105619e3a9c9SNikolay Aleksandrov 		return ERR_PTR(-E2BIG);
1057eb1d1641SHerbert Xu 	}
1058eb1d1641SHerbert Xu 
1059eb1d1641SHerbert Xu 	mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
1060eb1d1641SHerbert Xu 	if (unlikely(!mp))
10614c0833bcSTobias Klauser 		return ERR_PTR(-ENOMEM);
1062eb1d1641SHerbert Xu 
1063eb1d1641SHerbert Xu 	mp->br = br;
10648ef2a9a5SYOSHIFUJI Hideaki 	mp->addr = *group;
1065e12cec65SNikolay Aleksandrov 	mp->mcast_gc.destroy = br_multicast_destroy_mdb_entry;
106688c1f37fSAllen Pais 	timer_setup(&mp->timer, br_multicast_group_expired, 0);
106719e3a9c9SNikolay Aleksandrov 	err = rhashtable_lookup_insert_fast(&br->mdb_hash_tbl, &mp->rhnode,
106819e3a9c9SNikolay Aleksandrov 					    br_mdb_rht_params);
106919e3a9c9SNikolay Aleksandrov 	if (err) {
107019e3a9c9SNikolay Aleksandrov 		kfree(mp);
107119e3a9c9SNikolay Aleksandrov 		mp = ERR_PTR(err);
107219e3a9c9SNikolay Aleksandrov 	} else {
107319e3a9c9SNikolay Aleksandrov 		hlist_add_head_rcu(&mp->mdb_node, &br->mdb_list);
107419e3a9c9SNikolay Aleksandrov 	}
10751faabf2aSEric Dumazet 
1076eb1d1641SHerbert Xu 	return mp;
1077eb1d1641SHerbert Xu }
1078eb1d1641SHerbert Xu 
10798b671779SNikolay Aleksandrov static void br_multicast_group_src_expired(struct timer_list *t)
10808b671779SNikolay Aleksandrov {
10818b671779SNikolay Aleksandrov 	struct net_bridge_group_src *src = from_timer(src, t, timer);
10828b671779SNikolay Aleksandrov 	struct net_bridge_port_group *pg;
10838b671779SNikolay Aleksandrov 	struct net_bridge *br = src->br;
10848b671779SNikolay Aleksandrov 
10858b671779SNikolay Aleksandrov 	spin_lock(&br->multicast_lock);
10868b671779SNikolay Aleksandrov 	if (hlist_unhashed(&src->node) || !netif_running(br->dev) ||
10878b671779SNikolay Aleksandrov 	    timer_pending(&src->timer))
10888b671779SNikolay Aleksandrov 		goto out;
10898b671779SNikolay Aleksandrov 
10908b671779SNikolay Aleksandrov 	pg = src->pg;
10918b671779SNikolay Aleksandrov 	if (pg->filter_mode == MCAST_INCLUDE) {
1092d5a10222SNikolay Aleksandrov 		br_multicast_del_group_src(src, false);
10938b671779SNikolay Aleksandrov 		if (!hlist_empty(&pg->src_list))
10948b671779SNikolay Aleksandrov 			goto out;
10958b671779SNikolay Aleksandrov 		br_multicast_find_del_pg(br, pg);
10969116ffbfSNikolay Aleksandrov 	} else {
10979116ffbfSNikolay Aleksandrov 		br_multicast_fwd_src_handle(src);
10988b671779SNikolay Aleksandrov 	}
10999116ffbfSNikolay Aleksandrov 
11008b671779SNikolay Aleksandrov out:
11018b671779SNikolay Aleksandrov 	spin_unlock(&br->multicast_lock);
11028b671779SNikolay Aleksandrov }
11038b671779SNikolay Aleksandrov 
1104474ddb37SNikolay Aleksandrov struct net_bridge_group_src *
11058b671779SNikolay Aleksandrov br_multicast_find_group_src(struct net_bridge_port_group *pg, struct br_ip *ip)
11068b671779SNikolay Aleksandrov {
11078b671779SNikolay Aleksandrov 	struct net_bridge_group_src *ent;
11088b671779SNikolay Aleksandrov 
11098b671779SNikolay Aleksandrov 	switch (ip->proto) {
11108b671779SNikolay Aleksandrov 	case htons(ETH_P_IP):
11118b671779SNikolay Aleksandrov 		hlist_for_each_entry(ent, &pg->src_list, node)
1112deb96566SNikolay Aleksandrov 			if (ip->src.ip4 == ent->addr.src.ip4)
11138b671779SNikolay Aleksandrov 				return ent;
11148b671779SNikolay Aleksandrov 		break;
11158b671779SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
11168b671779SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
11178b671779SNikolay Aleksandrov 		hlist_for_each_entry(ent, &pg->src_list, node)
1118deb96566SNikolay Aleksandrov 			if (!ipv6_addr_cmp(&ent->addr.src.ip6, &ip->src.ip6))
11198b671779SNikolay Aleksandrov 				return ent;
11208b671779SNikolay Aleksandrov 		break;
11218b671779SNikolay Aleksandrov #endif
11228b671779SNikolay Aleksandrov 	}
11238b671779SNikolay Aleksandrov 
11248b671779SNikolay Aleksandrov 	return NULL;
11258b671779SNikolay Aleksandrov }
11268b671779SNikolay Aleksandrov 
11278b671779SNikolay Aleksandrov static struct net_bridge_group_src *
11288b671779SNikolay Aleksandrov br_multicast_new_group_src(struct net_bridge_port_group *pg, struct br_ip *src_ip)
11298b671779SNikolay Aleksandrov {
11308b671779SNikolay Aleksandrov 	struct net_bridge_group_src *grp_src;
11318b671779SNikolay Aleksandrov 
11328b671779SNikolay Aleksandrov 	if (unlikely(pg->src_ents >= PG_SRC_ENT_LIMIT))
11338b671779SNikolay Aleksandrov 		return NULL;
11348b671779SNikolay Aleksandrov 
11358b671779SNikolay Aleksandrov 	switch (src_ip->proto) {
11368b671779SNikolay Aleksandrov 	case htons(ETH_P_IP):
1137deb96566SNikolay Aleksandrov 		if (ipv4_is_zeronet(src_ip->src.ip4) ||
1138deb96566SNikolay Aleksandrov 		    ipv4_is_multicast(src_ip->src.ip4))
11398b671779SNikolay Aleksandrov 			return NULL;
11408b671779SNikolay Aleksandrov 		break;
11418b671779SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
11428b671779SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
1143deb96566SNikolay Aleksandrov 		if (ipv6_addr_any(&src_ip->src.ip6) ||
1144deb96566SNikolay Aleksandrov 		    ipv6_addr_is_multicast(&src_ip->src.ip6))
11458b671779SNikolay Aleksandrov 			return NULL;
11468b671779SNikolay Aleksandrov 		break;
11478b671779SNikolay Aleksandrov #endif
11488b671779SNikolay Aleksandrov 	}
11498b671779SNikolay Aleksandrov 
11508b671779SNikolay Aleksandrov 	grp_src = kzalloc(sizeof(*grp_src), GFP_ATOMIC);
11518b671779SNikolay Aleksandrov 	if (unlikely(!grp_src))
11528b671779SNikolay Aleksandrov 		return NULL;
11538b671779SNikolay Aleksandrov 
11548b671779SNikolay Aleksandrov 	grp_src->pg = pg;
1155085b53c8SNikolay Aleksandrov 	grp_src->br = pg->key.port->br;
11568b671779SNikolay Aleksandrov 	grp_src->addr = *src_ip;
1157e12cec65SNikolay Aleksandrov 	grp_src->mcast_gc.destroy = br_multicast_destroy_group_src;
11588b671779SNikolay Aleksandrov 	timer_setup(&grp_src->timer, br_multicast_group_src_expired, 0);
11598b671779SNikolay Aleksandrov 
11608b671779SNikolay Aleksandrov 	hlist_add_head_rcu(&grp_src->node, &pg->src_list);
11618b671779SNikolay Aleksandrov 	pg->src_ents++;
11628b671779SNikolay Aleksandrov 
11638b671779SNikolay Aleksandrov 	return grp_src;
11648b671779SNikolay Aleksandrov }
11658b671779SNikolay Aleksandrov 
1166cfd56754SCong Wang struct net_bridge_port_group *br_multicast_new_port_group(
1167cfd56754SCong Wang 			struct net_bridge_port *port,
1168cfd56754SCong Wang 			struct br_ip *group,
1169ccb1c31aSAmerigo Wang 			struct net_bridge_port_group __rcu *next,
11706db6f0eaSFelix Fietkau 			unsigned char flags,
11718b671779SNikolay Aleksandrov 			const unsigned char *src,
11728f8cb77eSNikolay Aleksandrov 			u8 filter_mode,
11738f8cb77eSNikolay Aleksandrov 			u8 rt_protocol)
1174cfd56754SCong Wang {
1175cfd56754SCong Wang 	struct net_bridge_port_group *p;
1176cfd56754SCong Wang 
1177cfd56754SCong Wang 	p = kzalloc(sizeof(*p), GFP_ATOMIC);
1178cfd56754SCong Wang 	if (unlikely(!p))
1179cfd56754SCong Wang 		return NULL;
1180cfd56754SCong Wang 
1181085b53c8SNikolay Aleksandrov 	p->key.addr = *group;
1182085b53c8SNikolay Aleksandrov 	p->key.port = port;
11839d06b6d8SElad Raz 	p->flags = flags;
11848b671779SNikolay Aleksandrov 	p->filter_mode = filter_mode;
11858f8cb77eSNikolay Aleksandrov 	p->rt_protocol = rt_protocol;
11865b163288SNikolay Aleksandrov 	p->eht_host_tree = RB_ROOT;
1187baa74d39SNikolay Aleksandrov 	p->eht_set_tree = RB_ROOT;
1188e12cec65SNikolay Aleksandrov 	p->mcast_gc.destroy = br_multicast_destroy_port_group;
11898b671779SNikolay Aleksandrov 	INIT_HLIST_HEAD(&p->src_list);
1190085b53c8SNikolay Aleksandrov 
1191085b53c8SNikolay Aleksandrov 	if (!br_multicast_is_star_g(group) &&
1192085b53c8SNikolay Aleksandrov 	    rhashtable_lookup_insert_fast(&port->br->sg_port_tbl, &p->rhnode,
1193085b53c8SNikolay Aleksandrov 					  br_sg_port_rht_params)) {
1194085b53c8SNikolay Aleksandrov 		kfree(p);
1195085b53c8SNikolay Aleksandrov 		return NULL;
1196085b53c8SNikolay Aleksandrov 	}
1197085b53c8SNikolay Aleksandrov 
1198eca2a43bSstephen hemminger 	rcu_assign_pointer(p->next, next);
119988c1f37fSAllen Pais 	timer_setup(&p->timer, br_multicast_port_group_expired, 0);
120042c11ccfSNikolay Aleksandrov 	timer_setup(&p->rexmit_timer, br_multicast_port_group_rexmit, 0);
120142c11ccfSNikolay Aleksandrov 	hlist_add_head(&p->mglist, &port->mglist);
12026db6f0eaSFelix Fietkau 
12036db6f0eaSFelix Fietkau 	if (src)
12046db6f0eaSFelix Fietkau 		memcpy(p->eth_addr, src, ETH_ALEN);
12056db6f0eaSFelix Fietkau 	else
12061bfe45f4SMao Wenan 		eth_broadcast_addr(p->eth_addr);
12076db6f0eaSFelix Fietkau 
1208cfd56754SCong Wang 	return p;
1209cfd56754SCong Wang }
1210cfd56754SCong Wang 
12111bc844eeSNikolay Aleksandrov void br_multicast_host_join(struct net_bridge_mdb_entry *mp, bool notify)
12121bc844eeSNikolay Aleksandrov {
12131bc844eeSNikolay Aleksandrov 	if (!mp->host_joined) {
12141bc844eeSNikolay Aleksandrov 		mp->host_joined = true;
1215094b82fdSNikolay Aleksandrov 		if (br_multicast_is_star_g(&mp->addr))
1216094b82fdSNikolay Aleksandrov 			br_multicast_star_g_host_state(mp);
12171bc844eeSNikolay Aleksandrov 		if (notify)
121881f19838SNikolay Aleksandrov 			br_mdb_notify(mp->br->dev, mp, NULL, RTM_NEWMDB);
12191bc844eeSNikolay Aleksandrov 	}
1220955062b0SNikolay Aleksandrov 
1221955062b0SNikolay Aleksandrov 	if (br_group_is_l2(&mp->addr))
1222955062b0SNikolay Aleksandrov 		return;
1223955062b0SNikolay Aleksandrov 
1224*d3d065c0SNikolay Aleksandrov 	mod_timer(&mp->timer,
1225*d3d065c0SNikolay Aleksandrov 		  jiffies + mp->br->multicast_ctx.multicast_membership_interval);
12261bc844eeSNikolay Aleksandrov }
12271bc844eeSNikolay Aleksandrov 
12281bc844eeSNikolay Aleksandrov void br_multicast_host_leave(struct net_bridge_mdb_entry *mp, bool notify)
12291bc844eeSNikolay Aleksandrov {
12301bc844eeSNikolay Aleksandrov 	if (!mp->host_joined)
12311bc844eeSNikolay Aleksandrov 		return;
12321bc844eeSNikolay Aleksandrov 
12331bc844eeSNikolay Aleksandrov 	mp->host_joined = false;
1234094b82fdSNikolay Aleksandrov 	if (br_multicast_is_star_g(&mp->addr))
1235094b82fdSNikolay Aleksandrov 		br_multicast_star_g_host_state(mp);
12361bc844eeSNikolay Aleksandrov 	if (notify)
123781f19838SNikolay Aleksandrov 		br_mdb_notify(mp->br->dev, mp, NULL, RTM_DELMDB);
12381bc844eeSNikolay Aleksandrov }
12391bc844eeSNikolay Aleksandrov 
1240b0812368SNikolay Aleksandrov static struct net_bridge_port_group *
1241b0812368SNikolay Aleksandrov __br_multicast_add_group(struct net_bridge *br,
12428ef2a9a5SYOSHIFUJI Hideaki 			 struct net_bridge_port *port,
12436db6f0eaSFelix Fietkau 			 struct br_ip *group,
12448b671779SNikolay Aleksandrov 			 const unsigned char *src,
12450436862eSNikolay Aleksandrov 			 u8 filter_mode,
12469116ffbfSNikolay Aleksandrov 			 bool igmpv2_mldv1,
12479116ffbfSNikolay Aleksandrov 			 bool blocked)
1248eb1d1641SHerbert Xu {
1249e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
1250b0812368SNikolay Aleksandrov 	struct net_bridge_port_group *p = NULL;
12515e923585SNikolay Aleksandrov 	struct net_bridge_mdb_entry *mp;
1252454594f3SLinus Lüssing 	unsigned long now = jiffies;
1253eb1d1641SHerbert Xu 
1254eb1d1641SHerbert Xu 	if (!netif_running(br->dev) ||
1255eb1d1641SHerbert Xu 	    (port && port->state == BR_STATE_DISABLED))
1256eb1d1641SHerbert Xu 		goto out;
1257eb1d1641SHerbert Xu 
125819e3a9c9SNikolay Aleksandrov 	mp = br_multicast_new_group(br, group);
12594c0833bcSTobias Klauser 	if (IS_ERR(mp))
12601697291dSXu Wang 		return ERR_CAST(mp);
1261eb1d1641SHerbert Xu 
1262eb1d1641SHerbert Xu 	if (!port) {
12631bc844eeSNikolay Aleksandrov 		br_multicast_host_join(mp, true);
1264eb1d1641SHerbert Xu 		goto out;
1265eb1d1641SHerbert Xu 	}
1266eb1d1641SHerbert Xu 
1267e8051688SEric Dumazet 	for (pp = &mp->ports;
1268e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
1269e8051688SEric Dumazet 	     pp = &p->next) {
12706db6f0eaSFelix Fietkau 		if (br_port_group_equal(p, port, src))
1271454594f3SLinus Lüssing 			goto found;
1272085b53c8SNikolay Aleksandrov 		if ((unsigned long)p->key.port < (unsigned long)port)
1273eb1d1641SHerbert Xu 			break;
1274eb1d1641SHerbert Xu 	}
1275eb1d1641SHerbert Xu 
1276955062b0SNikolay Aleksandrov 	p = br_multicast_new_port_group(port, group, *pp, 0, src,
1277955062b0SNikolay Aleksandrov 					filter_mode, RTPROT_KERNEL);
1278b0812368SNikolay Aleksandrov 	if (unlikely(!p)) {
1279b0812368SNikolay Aleksandrov 		p = ERR_PTR(-ENOMEM);
1280b0812368SNikolay Aleksandrov 		goto out;
1281b0812368SNikolay Aleksandrov 	}
1282eb1d1641SHerbert Xu 	rcu_assign_pointer(*pp, p);
12839116ffbfSNikolay Aleksandrov 	if (blocked)
12849116ffbfSNikolay Aleksandrov 		p->flags |= MDB_PG_FLAGS_BLOCKED;
128581f19838SNikolay Aleksandrov 	br_mdb_notify(br->dev, mp, p, RTM_NEWMDB);
1286eb1d1641SHerbert Xu 
1287454594f3SLinus Lüssing found:
12880436862eSNikolay Aleksandrov 	if (igmpv2_mldv1)
1289*d3d065c0SNikolay Aleksandrov 		mod_timer(&p->timer,
1290*d3d065c0SNikolay Aleksandrov 			  now + br->multicast_ctx.multicast_membership_interval);
129181f19838SNikolay Aleksandrov 
1292eb1d1641SHerbert Xu out:
1293b0812368SNikolay Aleksandrov 	return p;
1294b0812368SNikolay Aleksandrov }
1295eb1d1641SHerbert Xu 
1296b0812368SNikolay Aleksandrov static int br_multicast_add_group(struct net_bridge *br,
1297b0812368SNikolay Aleksandrov 				  struct net_bridge_port *port,
1298b0812368SNikolay Aleksandrov 				  struct br_ip *group,
1299b0812368SNikolay Aleksandrov 				  const unsigned char *src,
1300b0812368SNikolay Aleksandrov 				  u8 filter_mode,
1301b0812368SNikolay Aleksandrov 				  bool igmpv2_mldv1)
1302b0812368SNikolay Aleksandrov {
1303b0812368SNikolay Aleksandrov 	struct net_bridge_port_group *pg;
1304b0812368SNikolay Aleksandrov 	int err;
1305b0812368SNikolay Aleksandrov 
1306b0812368SNikolay Aleksandrov 	spin_lock(&br->multicast_lock);
1307b0812368SNikolay Aleksandrov 	pg = __br_multicast_add_group(br, port, group, src, filter_mode,
13089116ffbfSNikolay Aleksandrov 				      igmpv2_mldv1, false);
1309b0812368SNikolay Aleksandrov 	/* NULL is considered valid for host joined groups */
13108d21c882SJiapeng Zhong 	err = PTR_ERR_OR_ZERO(pg);
1311eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1312b0812368SNikolay Aleksandrov 
1313eb1d1641SHerbert Xu 	return err;
1314eb1d1641SHerbert Xu }
1315eb1d1641SHerbert Xu 
13168ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_add_group(struct net_bridge *br,
13178ef2a9a5SYOSHIFUJI Hideaki 				      struct net_bridge_port *port,
1318b0e9a30dSVlad Yasevich 				      __be32 group,
13196db6f0eaSFelix Fietkau 				      __u16 vid,
13208b671779SNikolay Aleksandrov 				      const unsigned char *src,
13218b671779SNikolay Aleksandrov 				      bool igmpv2)
13228ef2a9a5SYOSHIFUJI Hideaki {
13238ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip br_group;
13248b671779SNikolay Aleksandrov 	u8 filter_mode;
13258ef2a9a5SYOSHIFUJI Hideaki 
13268ef2a9a5SYOSHIFUJI Hideaki 	if (ipv4_is_local_multicast(group))
13278ef2a9a5SYOSHIFUJI Hideaki 		return 0;
13288ef2a9a5SYOSHIFUJI Hideaki 
13291515a63fSNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
1330eab3227bSNikolay Aleksandrov 	br_group.dst.ip4 = group;
13318ef2a9a5SYOSHIFUJI Hideaki 	br_group.proto = htons(ETH_P_IP);
1332b0e9a30dSVlad Yasevich 	br_group.vid = vid;
13338b671779SNikolay Aleksandrov 	filter_mode = igmpv2 ? MCAST_EXCLUDE : MCAST_INCLUDE;
13348ef2a9a5SYOSHIFUJI Hideaki 
13350436862eSNikolay Aleksandrov 	return br_multicast_add_group(br, port, &br_group, src, filter_mode,
13360436862eSNikolay Aleksandrov 				      igmpv2);
13378ef2a9a5SYOSHIFUJI Hideaki }
13388ef2a9a5SYOSHIFUJI Hideaki 
1339dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
134008b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_add_group(struct net_bridge *br,
134108b202b6SYOSHIFUJI Hideaki 				      struct net_bridge_port *port,
1342b0e9a30dSVlad Yasevich 				      const struct in6_addr *group,
13436db6f0eaSFelix Fietkau 				      __u16 vid,
13448b671779SNikolay Aleksandrov 				      const unsigned char *src,
13458b671779SNikolay Aleksandrov 				      bool mldv1)
134608b202b6SYOSHIFUJI Hideaki {
134708b202b6SYOSHIFUJI Hideaki 	struct br_ip br_group;
13488b671779SNikolay Aleksandrov 	u8 filter_mode;
134908b202b6SYOSHIFUJI Hideaki 
13503c3769e6SLinus Lüssing 	if (ipv6_addr_is_ll_all_nodes(group))
135108b202b6SYOSHIFUJI Hideaki 		return 0;
135208b202b6SYOSHIFUJI Hideaki 
135319e3a9c9SNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
1354eab3227bSNikolay Aleksandrov 	br_group.dst.ip6 = *group;
13559cc6e0c4SLinus Lüssing 	br_group.proto = htons(ETH_P_IPV6);
1356b0e9a30dSVlad Yasevich 	br_group.vid = vid;
13578b671779SNikolay Aleksandrov 	filter_mode = mldv1 ? MCAST_EXCLUDE : MCAST_INCLUDE;
135808b202b6SYOSHIFUJI Hideaki 
13590436862eSNikolay Aleksandrov 	return br_multicast_add_group(br, port, &br_group, src, filter_mode,
13600436862eSNikolay Aleksandrov 				      mldv1);
136108b202b6SYOSHIFUJI Hideaki }
136208b202b6SYOSHIFUJI Hideaki #endif
136308b202b6SYOSHIFUJI Hideaki 
1364ed2d3597SLinus Lüssing static bool br_multicast_rport_del(struct hlist_node *rlist)
1365ed2d3597SLinus Lüssing {
1366ed2d3597SLinus Lüssing 	if (hlist_unhashed(rlist))
1367ed2d3597SLinus Lüssing 		return false;
1368ed2d3597SLinus Lüssing 
1369ed2d3597SLinus Lüssing 	hlist_del_init_rcu(rlist);
1370ed2d3597SLinus Lüssing 	return true;
1371ed2d3597SLinus Lüssing }
1372ed2d3597SLinus Lüssing 
1373ed2d3597SLinus Lüssing static bool br_ip4_multicast_rport_del(struct net_bridge_port *p)
1374ed2d3597SLinus Lüssing {
13759632233eSNikolay Aleksandrov 	return br_multicast_rport_del(&p->multicast_ctx.ip4_rlist);
1376ed2d3597SLinus Lüssing }
1377ed2d3597SLinus Lüssing 
1378a3c02e76SLinus Lüssing static bool br_ip6_multicast_rport_del(struct net_bridge_port *p)
1379a3c02e76SLinus Lüssing {
1380a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
13819632233eSNikolay Aleksandrov 	return br_multicast_rport_del(&p->multicast_ctx.ip6_rlist);
1382a3c02e76SLinus Lüssing #else
1383a3c02e76SLinus Lüssing 	return false;
1384a3c02e76SLinus Lüssing #endif
1385a3c02e76SLinus Lüssing }
1386a3c02e76SLinus Lüssing 
13879632233eSNikolay Aleksandrov static void br_multicast_router_expired(struct net_bridge_mcast_port *pmctx,
1388ee5fb222SLinus Lüssing 					struct timer_list *t,
1389ee5fb222SLinus Lüssing 					struct hlist_node *rlist)
1390eb1d1641SHerbert Xu {
13919632233eSNikolay Aleksandrov 	struct net_bridge *br = pmctx->port->br;
1392ed2d3597SLinus Lüssing 	bool del;
1393eb1d1641SHerbert Xu 
1394eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
13959632233eSNikolay Aleksandrov 	if (pmctx->multicast_router == MDB_RTR_TYPE_DISABLED ||
13969632233eSNikolay Aleksandrov 	    pmctx->multicast_router == MDB_RTR_TYPE_PERM ||
1397ee5fb222SLinus Lüssing 	    timer_pending(t))
1398eb1d1641SHerbert Xu 		goto out;
1399eb1d1641SHerbert Xu 
1400ed2d3597SLinus Lüssing 	del = br_multicast_rport_del(rlist);
14019632233eSNikolay Aleksandrov 	br_multicast_rport_del_notify(pmctx->port, del);
1402eb1d1641SHerbert Xu out:
1403eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1404eb1d1641SHerbert Xu }
1405eb1d1641SHerbert Xu 
1406ee5fb222SLinus Lüssing static void br_ip4_multicast_router_expired(struct timer_list *t)
1407ee5fb222SLinus Lüssing {
14089632233eSNikolay Aleksandrov 	struct net_bridge_mcast_port *pmctx = from_timer(pmctx, t,
14099632233eSNikolay Aleksandrov 							 ip4_mc_router_timer);
1410ee5fb222SLinus Lüssing 
14119632233eSNikolay Aleksandrov 	br_multicast_router_expired(pmctx, t, &pmctx->ip4_rlist);
1412ee5fb222SLinus Lüssing }
1413ee5fb222SLinus Lüssing 
1414a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
1415a3c02e76SLinus Lüssing static void br_ip6_multicast_router_expired(struct timer_list *t)
1416a3c02e76SLinus Lüssing {
14179632233eSNikolay Aleksandrov 	struct net_bridge_mcast_port *pmctx = from_timer(pmctx, t,
14189632233eSNikolay Aleksandrov 							 ip6_mc_router_timer);
1419a3c02e76SLinus Lüssing 
14209632233eSNikolay Aleksandrov 	br_multicast_router_expired(pmctx, t, &pmctx->ip6_rlist);
1421a3c02e76SLinus Lüssing }
1422a3c02e76SLinus Lüssing #endif
1423a3c02e76SLinus Lüssing 
142477041420SYotam Gigi static void br_mc_router_state_change(struct net_bridge *p,
142577041420SYotam Gigi 				      bool is_mc_router)
142677041420SYotam Gigi {
142777041420SYotam Gigi 	struct switchdev_attr attr = {
142877041420SYotam Gigi 		.orig_dev = p->dev,
142977041420SYotam Gigi 		.id = SWITCHDEV_ATTR_ID_BRIDGE_MROUTER,
143077041420SYotam Gigi 		.flags = SWITCHDEV_F_DEFER,
143177041420SYotam Gigi 		.u.mrouter = is_mc_router,
143277041420SYotam Gigi 	};
143377041420SYotam Gigi 
1434dcbdf135SVladimir Oltean 	switchdev_port_attr_set(p->dev, &attr, NULL);
143577041420SYotam Gigi }
143677041420SYotam Gigi 
1437*d3d065c0SNikolay Aleksandrov static void br_multicast_local_router_expired(struct net_bridge_mcast *brmctx,
1438ee5fb222SLinus Lüssing 					      struct timer_list *timer)
1439eb1d1641SHerbert Xu {
1440*d3d065c0SNikolay Aleksandrov 	spin_lock(&brmctx->br->multicast_lock);
1441*d3d065c0SNikolay Aleksandrov 	if (brmctx->multicast_router == MDB_RTR_TYPE_DISABLED ||
1442*d3d065c0SNikolay Aleksandrov 	    brmctx->multicast_router == MDB_RTR_TYPE_PERM ||
1443*d3d065c0SNikolay Aleksandrov 	    br_ip4_multicast_is_router(brmctx) ||
1444*d3d065c0SNikolay Aleksandrov 	    br_ip6_multicast_is_router(brmctx))
144577041420SYotam Gigi 		goto out;
144677041420SYotam Gigi 
1447*d3d065c0SNikolay Aleksandrov 	br_mc_router_state_change(brmctx->br, false);
144877041420SYotam Gigi out:
1449*d3d065c0SNikolay Aleksandrov 	spin_unlock(&brmctx->br->multicast_lock);
1450eb1d1641SHerbert Xu }
1451eb1d1641SHerbert Xu 
1452ee5fb222SLinus Lüssing static void br_ip4_multicast_local_router_expired(struct timer_list *t)
1453ee5fb222SLinus Lüssing {
1454*d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = from_timer(brmctx, t,
1455*d3d065c0SNikolay Aleksandrov 						     ip4_mc_router_timer);
1456ee5fb222SLinus Lüssing 
1457*d3d065c0SNikolay Aleksandrov 	br_multicast_local_router_expired(brmctx, t);
1458ee5fb222SLinus Lüssing }
1459ee5fb222SLinus Lüssing 
1460a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
1461a3c02e76SLinus Lüssing static void br_ip6_multicast_local_router_expired(struct timer_list *t)
1462a3c02e76SLinus Lüssing {
1463*d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = from_timer(brmctx, t,
1464*d3d065c0SNikolay Aleksandrov 						     ip6_mc_router_timer);
1465a3c02e76SLinus Lüssing 
1466*d3d065c0SNikolay Aleksandrov 	br_multicast_local_router_expired(brmctx, t);
1467a3c02e76SLinus Lüssing }
1468a3c02e76SLinus Lüssing #endif
1469a3c02e76SLinus Lüssing 
1470*d3d065c0SNikolay Aleksandrov static void br_multicast_querier_expired(struct net_bridge_mcast *brmctx,
147190010b36SLinus Lüssing 					 struct bridge_mcast_own_query *query)
1472c83b8fabSHerbert Xu {
1473*d3d065c0SNikolay Aleksandrov 	spin_lock(&brmctx->br->multicast_lock);
1474*d3d065c0SNikolay Aleksandrov 	if (!netif_running(brmctx->br->dev) ||
1475*d3d065c0SNikolay Aleksandrov 	    !br_opt_get(brmctx->br, BROPT_MULTICAST_ENABLED))
1476c83b8fabSHerbert Xu 		goto out;
1477c83b8fabSHerbert Xu 
1478*d3d065c0SNikolay Aleksandrov 	br_multicast_start_querier(brmctx->br, query);
1479c83b8fabSHerbert Xu 
1480c83b8fabSHerbert Xu out:
1481*d3d065c0SNikolay Aleksandrov 	spin_unlock(&brmctx->br->multicast_lock);
1482c83b8fabSHerbert Xu }
1483c83b8fabSHerbert Xu 
148488c1f37fSAllen Pais static void br_ip4_multicast_querier_expired(struct timer_list *t)
1485cc0fdd80SLinus Lüssing {
1486*d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = from_timer(brmctx, t,
1487*d3d065c0SNikolay Aleksandrov 						     ip4_other_query.timer);
1488cc0fdd80SLinus Lüssing 
1489*d3d065c0SNikolay Aleksandrov 	br_multicast_querier_expired(brmctx, &brmctx->ip4_own_query);
1490cc0fdd80SLinus Lüssing }
1491cc0fdd80SLinus Lüssing 
1492cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
149388c1f37fSAllen Pais static void br_ip6_multicast_querier_expired(struct timer_list *t)
1494cc0fdd80SLinus Lüssing {
1495*d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = from_timer(brmctx, t,
1496*d3d065c0SNikolay Aleksandrov 						     ip6_other_query.timer);
1497cc0fdd80SLinus Lüssing 
1498*d3d065c0SNikolay Aleksandrov 	br_multicast_querier_expired(brmctx, &brmctx->ip6_own_query);
1499cc0fdd80SLinus Lüssing }
1500cc0fdd80SLinus Lüssing #endif
1501cc0fdd80SLinus Lüssing 
1502dc4eb53aSLinus Lüssing static void br_multicast_select_own_querier(struct net_bridge *br,
1503dc4eb53aSLinus Lüssing 					    struct br_ip *ip,
1504dc4eb53aSLinus Lüssing 					    struct sk_buff *skb)
1505dc4eb53aSLinus Lüssing {
1506*d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = &br->multicast_ctx;
1507*d3d065c0SNikolay Aleksandrov 
1508dc4eb53aSLinus Lüssing 	if (ip->proto == htons(ETH_P_IP))
1509*d3d065c0SNikolay Aleksandrov 		brmctx->ip4_querier.addr.src.ip4 = ip_hdr(skb)->saddr;
1510dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
1511dc4eb53aSLinus Lüssing 	else
1512*d3d065c0SNikolay Aleksandrov 		brmctx->ip6_querier.addr.src.ip6 = ipv6_hdr(skb)->saddr;
1513dc4eb53aSLinus Lüssing #endif
1514dc4eb53aSLinus Lüssing }
1515dc4eb53aSLinus Lüssing 
15168ef2a9a5SYOSHIFUJI Hideaki static void __br_multicast_send_query(struct net_bridge *br,
15178ef2a9a5SYOSHIFUJI Hideaki 				      struct net_bridge_port *port,
1518438ef2d0SNikolay Aleksandrov 				      struct net_bridge_port_group *pg,
1519438ef2d0SNikolay Aleksandrov 				      struct br_ip *ip_dst,
1520438ef2d0SNikolay Aleksandrov 				      struct br_ip *group,
1521438ef2d0SNikolay Aleksandrov 				      bool with_srcs,
152242c11ccfSNikolay Aleksandrov 				      u8 sflag,
152342c11ccfSNikolay Aleksandrov 				      bool *need_rexmit)
1524eb1d1641SHerbert Xu {
1525438ef2d0SNikolay Aleksandrov 	bool over_lmqt = !!sflag;
1526eb1d1641SHerbert Xu 	struct sk_buff *skb;
15271080ab95SNikolay Aleksandrov 	u8 igmp_type;
1528eb1d1641SHerbert Xu 
1529438ef2d0SNikolay Aleksandrov again_under_lmqt:
1530438ef2d0SNikolay Aleksandrov 	skb = br_multicast_alloc_query(br, pg, ip_dst, group, with_srcs,
153142c11ccfSNikolay Aleksandrov 				       over_lmqt, sflag, &igmp_type,
153242c11ccfSNikolay Aleksandrov 				       need_rexmit);
1533eb1d1641SHerbert Xu 	if (!skb)
15348ef2a9a5SYOSHIFUJI Hideaki 		return;
1535eb1d1641SHerbert Xu 
1536eb1d1641SHerbert Xu 	if (port) {
1537eb1d1641SHerbert Xu 		skb->dev = port->dev;
1538a65056ecSNikolay Aleksandrov 		br_multicast_count(br, port, skb, igmp_type,
15391080ab95SNikolay Aleksandrov 				   BR_MCAST_DIR_TX);
154029a26a56SEric W. Biederman 		NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
154129a26a56SEric W. Biederman 			dev_net(port->dev), NULL, skb, NULL, skb->dev,
1542f0b4eeceSLinus Lüssing 			br_dev_queue_push_xmit);
1543438ef2d0SNikolay Aleksandrov 
1544438ef2d0SNikolay Aleksandrov 		if (over_lmqt && with_srcs && sflag) {
1545438ef2d0SNikolay Aleksandrov 			over_lmqt = false;
1546438ef2d0SNikolay Aleksandrov 			goto again_under_lmqt;
1547438ef2d0SNikolay Aleksandrov 		}
1548dc4eb53aSLinus Lüssing 	} else {
1549438ef2d0SNikolay Aleksandrov 		br_multicast_select_own_querier(br, group, skb);
1550a65056ecSNikolay Aleksandrov 		br_multicast_count(br, port, skb, igmp_type,
15511080ab95SNikolay Aleksandrov 				   BR_MCAST_DIR_RX);
1552eb1d1641SHerbert Xu 		netif_rx(skb);
15538ef2a9a5SYOSHIFUJI Hideaki 	}
1554dc4eb53aSLinus Lüssing }
1555eb1d1641SHerbert Xu 
15568ef2a9a5SYOSHIFUJI Hideaki static void br_multicast_send_query(struct net_bridge *br,
1557cc0fdd80SLinus Lüssing 				    struct net_bridge_port *port,
155890010b36SLinus Lüssing 				    struct bridge_mcast_own_query *own_query)
15598ef2a9a5SYOSHIFUJI Hideaki {
1560*d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = &br->multicast_ctx;
156190010b36SLinus Lüssing 	struct bridge_mcast_other_query *other_query = NULL;
15625e923585SNikolay Aleksandrov 	struct br_ip br_group;
15635e923585SNikolay Aleksandrov 	unsigned long time;
15648ef2a9a5SYOSHIFUJI Hideaki 
156513cefad2SNikolay Aleksandrov 	if (!netif_running(br->dev) ||
156613cefad2SNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_ENABLED) ||
1567675779adSNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_QUERIER))
15688ef2a9a5SYOSHIFUJI Hideaki 		return;
15698ef2a9a5SYOSHIFUJI Hideaki 
1570eab3227bSNikolay Aleksandrov 	memset(&br_group.dst, 0, sizeof(br_group.dst));
15718ef2a9a5SYOSHIFUJI Hideaki 
15729632233eSNikolay Aleksandrov 	if (port ? (own_query == &port->multicast_ctx.ip4_own_query) :
1573*d3d065c0SNikolay Aleksandrov 		   (own_query == &brmctx->ip4_own_query)) {
1574*d3d065c0SNikolay Aleksandrov 		other_query = &brmctx->ip4_other_query;
157508b202b6SYOSHIFUJI Hideaki 		br_group.proto = htons(ETH_P_IP);
1576dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1577cc0fdd80SLinus Lüssing 	} else {
1578*d3d065c0SNikolay Aleksandrov 		other_query = &brmctx->ip6_other_query;
157908b202b6SYOSHIFUJI Hideaki 		br_group.proto = htons(ETH_P_IPV6);
158008b202b6SYOSHIFUJI Hideaki #endif
1581eb1d1641SHerbert Xu 	}
1582eb1d1641SHerbert Xu 
158390010b36SLinus Lüssing 	if (!other_query || timer_pending(&other_query->timer))
1584cc0fdd80SLinus Lüssing 		return;
1585cc0fdd80SLinus Lüssing 
158642c11ccfSNikolay Aleksandrov 	__br_multicast_send_query(br, port, NULL, NULL, &br_group, false, 0,
158742c11ccfSNikolay Aleksandrov 				  NULL);
1588cc0fdd80SLinus Lüssing 
1589cc0fdd80SLinus Lüssing 	time = jiffies;
1590*d3d065c0SNikolay Aleksandrov 	time += own_query->startup_sent < brmctx->multicast_startup_query_count ?
1591*d3d065c0SNikolay Aleksandrov 		brmctx->multicast_startup_query_interval :
1592*d3d065c0SNikolay Aleksandrov 		brmctx->multicast_query_interval;
159390010b36SLinus Lüssing 	mod_timer(&own_query->timer, time);
1594cc0fdd80SLinus Lüssing }
1595cc0fdd80SLinus Lüssing 
159690010b36SLinus Lüssing static void
15979632233eSNikolay Aleksandrov br_multicast_port_query_expired(struct net_bridge_mcast_port *pmctx,
159890010b36SLinus Lüssing 				struct bridge_mcast_own_query *query)
1599eb1d1641SHerbert Xu {
16009632233eSNikolay Aleksandrov 	struct net_bridge *br = pmctx->port->br;
1601eb1d1641SHerbert Xu 
1602eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
16039632233eSNikolay Aleksandrov 	if (pmctx->port->state == BR_STATE_DISABLED ||
16049632233eSNikolay Aleksandrov 	    pmctx->port->state == BR_STATE_BLOCKING)
1605eb1d1641SHerbert Xu 		goto out;
1606eb1d1641SHerbert Xu 
1607*d3d065c0SNikolay Aleksandrov 	if (query->startup_sent < br->multicast_ctx.multicast_startup_query_count)
1608cc0fdd80SLinus Lüssing 		query->startup_sent++;
1609eb1d1641SHerbert Xu 
16109632233eSNikolay Aleksandrov 	br_multicast_send_query(pmctx->port->br, pmctx->port, query);
1611eb1d1641SHerbert Xu 
1612eb1d1641SHerbert Xu out:
1613eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1614eb1d1641SHerbert Xu }
1615eb1d1641SHerbert Xu 
161688c1f37fSAllen Pais static void br_ip4_multicast_port_query_expired(struct timer_list *t)
1617cc0fdd80SLinus Lüssing {
16189632233eSNikolay Aleksandrov 	struct net_bridge_mcast_port *pmctx = from_timer(pmctx, t,
16199632233eSNikolay Aleksandrov 							 ip4_own_query.timer);
1620cc0fdd80SLinus Lüssing 
16219632233eSNikolay Aleksandrov 	br_multicast_port_query_expired(pmctx, &pmctx->ip4_own_query);
1622cc0fdd80SLinus Lüssing }
1623cc0fdd80SLinus Lüssing 
1624cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
162588c1f37fSAllen Pais static void br_ip6_multicast_port_query_expired(struct timer_list *t)
1626cc0fdd80SLinus Lüssing {
16279632233eSNikolay Aleksandrov 	struct net_bridge_mcast_port *pmctx = from_timer(pmctx, t,
16289632233eSNikolay Aleksandrov 							 ip6_own_query.timer);
1629cc0fdd80SLinus Lüssing 
16309632233eSNikolay Aleksandrov 	br_multicast_port_query_expired(pmctx, &pmctx->ip6_own_query);
1631cc0fdd80SLinus Lüssing }
1632cc0fdd80SLinus Lüssing #endif
1633cc0fdd80SLinus Lüssing 
163442c11ccfSNikolay Aleksandrov static void br_multicast_port_group_rexmit(struct timer_list *t)
163542c11ccfSNikolay Aleksandrov {
163642c11ccfSNikolay Aleksandrov 	struct net_bridge_port_group *pg = from_timer(pg, t, rexmit_timer);
163742c11ccfSNikolay Aleksandrov 	struct bridge_mcast_other_query *other_query = NULL;
1638085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
1639*d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = &br->multicast_ctx;
164042c11ccfSNikolay Aleksandrov 	bool need_rexmit = false;
164142c11ccfSNikolay Aleksandrov 
164242c11ccfSNikolay Aleksandrov 	spin_lock(&br->multicast_lock);
164342c11ccfSNikolay Aleksandrov 	if (!netif_running(br->dev) || hlist_unhashed(&pg->mglist) ||
164442c11ccfSNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_ENABLED) ||
164542c11ccfSNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_QUERIER))
164642c11ccfSNikolay Aleksandrov 		goto out;
164742c11ccfSNikolay Aleksandrov 
1648085b53c8SNikolay Aleksandrov 	if (pg->key.addr.proto == htons(ETH_P_IP))
1649*d3d065c0SNikolay Aleksandrov 		other_query = &brmctx->ip4_other_query;
165042c11ccfSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
165142c11ccfSNikolay Aleksandrov 	else
1652*d3d065c0SNikolay Aleksandrov 		other_query = &brmctx->ip6_other_query;
165342c11ccfSNikolay Aleksandrov #endif
165442c11ccfSNikolay Aleksandrov 
165542c11ccfSNikolay Aleksandrov 	if (!other_query || timer_pending(&other_query->timer))
165642c11ccfSNikolay Aleksandrov 		goto out;
165742c11ccfSNikolay Aleksandrov 
165842c11ccfSNikolay Aleksandrov 	if (pg->grp_query_rexmit_cnt) {
165942c11ccfSNikolay Aleksandrov 		pg->grp_query_rexmit_cnt--;
1660085b53c8SNikolay Aleksandrov 		__br_multicast_send_query(br, pg->key.port, pg, &pg->key.addr,
1661085b53c8SNikolay Aleksandrov 					  &pg->key.addr, false, 1, NULL);
166242c11ccfSNikolay Aleksandrov 	}
1663085b53c8SNikolay Aleksandrov 	__br_multicast_send_query(br, pg->key.port, pg, &pg->key.addr,
1664085b53c8SNikolay Aleksandrov 				  &pg->key.addr, true, 0, &need_rexmit);
166542c11ccfSNikolay Aleksandrov 
166642c11ccfSNikolay Aleksandrov 	if (pg->grp_query_rexmit_cnt || need_rexmit)
166742c11ccfSNikolay Aleksandrov 		mod_timer(&pg->rexmit_timer, jiffies +
1668*d3d065c0SNikolay Aleksandrov 					     brmctx->multicast_last_member_interval);
166942c11ccfSNikolay Aleksandrov out:
167042c11ccfSNikolay Aleksandrov 	spin_unlock(&br->multicast_lock);
167142c11ccfSNikolay Aleksandrov }
167242c11ccfSNikolay Aleksandrov 
1673ae1ea84bSFlorian Fainelli static int br_mc_disabled_update(struct net_device *dev, bool value,
1674ae1ea84bSFlorian Fainelli 				 struct netlink_ext_ack *extack)
1675147c1e9bSNogah Frankel {
1676147c1e9bSNogah Frankel 	struct switchdev_attr attr = {
1677147c1e9bSNogah Frankel 		.orig_dev = dev,
1678147c1e9bSNogah Frankel 		.id = SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED,
1679147c1e9bSNogah Frankel 		.flags = SWITCHDEV_F_DEFER,
168013cefad2SNikolay Aleksandrov 		.u.mc_disabled = !value,
1681147c1e9bSNogah Frankel 	};
1682147c1e9bSNogah Frankel 
1683ae1ea84bSFlorian Fainelli 	return switchdev_port_attr_set(dev, &attr, extack);
1684147c1e9bSNogah Frankel }
1685147c1e9bSNogah Frankel 
16869632233eSNikolay Aleksandrov static void br_multicast_port_ctx_init(struct net_bridge_port *port,
16879632233eSNikolay Aleksandrov 				       struct net_bridge_mcast_port *pmctx)
16889632233eSNikolay Aleksandrov {
16899632233eSNikolay Aleksandrov 	pmctx->port = port;
16909632233eSNikolay Aleksandrov 	pmctx->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
16919632233eSNikolay Aleksandrov 	timer_setup(&pmctx->ip4_mc_router_timer,
16929632233eSNikolay Aleksandrov 		    br_ip4_multicast_router_expired, 0);
16939632233eSNikolay Aleksandrov 	timer_setup(&pmctx->ip4_own_query.timer,
16949632233eSNikolay Aleksandrov 		    br_ip4_multicast_port_query_expired, 0);
16959632233eSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
16969632233eSNikolay Aleksandrov 	timer_setup(&pmctx->ip6_mc_router_timer,
16979632233eSNikolay Aleksandrov 		    br_ip6_multicast_router_expired, 0);
16989632233eSNikolay Aleksandrov 	timer_setup(&pmctx->ip6_own_query.timer,
16999632233eSNikolay Aleksandrov 		    br_ip6_multicast_port_query_expired, 0);
17009632233eSNikolay Aleksandrov #endif
17019632233eSNikolay Aleksandrov }
17029632233eSNikolay Aleksandrov 
17039632233eSNikolay Aleksandrov static void br_multicast_port_ctx_deinit(struct net_bridge_mcast_port *pmctx)
17049632233eSNikolay Aleksandrov {
17059632233eSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
17069632233eSNikolay Aleksandrov 	del_timer_sync(&pmctx->ip6_mc_router_timer);
17079632233eSNikolay Aleksandrov #endif
17089632233eSNikolay Aleksandrov 	del_timer_sync(&pmctx->ip4_mc_router_timer);
17099632233eSNikolay Aleksandrov }
17109632233eSNikolay Aleksandrov 
17111080ab95SNikolay Aleksandrov int br_multicast_add_port(struct net_bridge_port *port)
1712eb1d1641SHerbert Xu {
1713ae1ea84bSFlorian Fainelli 	int err;
1714ae1ea84bSFlorian Fainelli 
171589268b05SNikolay Aleksandrov 	port->multicast_eht_hosts_limit = BR_MCAST_DEFAULT_EHT_HOSTS_LIMIT;
17169632233eSNikolay Aleksandrov 	br_multicast_port_ctx_init(port, &port->multicast_ctx);
1717eb1d1641SHerbert Xu 
1718ae1ea84bSFlorian Fainelli 	err = br_mc_disabled_update(port->dev,
1719ae1ea84bSFlorian Fainelli 				    br_opt_get(port->br,
1720ae1ea84bSFlorian Fainelli 					       BROPT_MULTICAST_ENABLED),
1721ae1ea84bSFlorian Fainelli 				    NULL);
172268f5c12aSVladimir Oltean 	if (err && err != -EOPNOTSUPP)
1723ae1ea84bSFlorian Fainelli 		return err;
1724147c1e9bSNogah Frankel 
17251080ab95SNikolay Aleksandrov 	port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
17261080ab95SNikolay Aleksandrov 	if (!port->mcast_stats)
17271080ab95SNikolay Aleksandrov 		return -ENOMEM;
17281080ab95SNikolay Aleksandrov 
17291080ab95SNikolay Aleksandrov 	return 0;
1730eb1d1641SHerbert Xu }
1731eb1d1641SHerbert Xu 
1732eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port)
1733eb1d1641SHerbert Xu {
1734e10177abSSatish Ashok 	struct net_bridge *br = port->br;
1735e10177abSSatish Ashok 	struct net_bridge_port_group *pg;
1736e12cec65SNikolay Aleksandrov 	HLIST_HEAD(deleted_head);
1737e10177abSSatish Ashok 	struct hlist_node *n;
1738e10177abSSatish Ashok 
1739e10177abSSatish Ashok 	/* Take care of the remaining groups, only perm ones should be left */
1740e10177abSSatish Ashok 	spin_lock_bh(&br->multicast_lock);
1741e10177abSSatish Ashok 	hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
1742681590bdSNikolay Aleksandrov 		br_multicast_find_del_pg(br, pg);
1743e12cec65SNikolay Aleksandrov 	hlist_move_list(&br->mcast_gc_list, &deleted_head);
1744e10177abSSatish Ashok 	spin_unlock_bh(&br->multicast_lock);
1745e12cec65SNikolay Aleksandrov 	br_multicast_gc(&deleted_head);
17469632233eSNikolay Aleksandrov 	br_multicast_port_ctx_deinit(&port->multicast_ctx);
17471080ab95SNikolay Aleksandrov 	free_percpu(port->mcast_stats);
1748eb1d1641SHerbert Xu }
1749eb1d1641SHerbert Xu 
175090010b36SLinus Lüssing static void br_multicast_enable(struct bridge_mcast_own_query *query)
1751561f1103SHerbert Xu {
1752cc0fdd80SLinus Lüssing 	query->startup_sent = 0;
1753561f1103SHerbert Xu 
1754cc0fdd80SLinus Lüssing 	if (try_to_del_timer_sync(&query->timer) >= 0 ||
1755cc0fdd80SLinus Lüssing 	    del_timer(&query->timer))
1756cc0fdd80SLinus Lüssing 		mod_timer(&query->timer, jiffies);
1757561f1103SHerbert Xu }
1758561f1103SHerbert Xu 
17597cb3f921SNikolay Aleksandrov static void __br_multicast_enable_port(struct net_bridge_port *port)
1760eb1d1641SHerbert Xu {
1761eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
1762eb1d1641SHerbert Xu 
176313cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED) || !netif_running(br->dev))
17647cb3f921SNikolay Aleksandrov 		return;
1765eb1d1641SHerbert Xu 
17669632233eSNikolay Aleksandrov 	br_multicast_enable(&port->multicast_ctx.ip4_own_query);
1767cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
17689632233eSNikolay Aleksandrov 	br_multicast_enable(&port->multicast_ctx.ip6_own_query);
1769cc0fdd80SLinus Lüssing #endif
17709632233eSNikolay Aleksandrov 	if (port->multicast_ctx.multicast_router == MDB_RTR_TYPE_PERM) {
1771d9b8c4d8SLinus Lüssing 		br_ip4_multicast_add_router(br, port);
1772a3c02e76SLinus Lüssing 		br_ip6_multicast_add_router(br, port);
1773a3c02e76SLinus Lüssing 	}
17747cb3f921SNikolay Aleksandrov }
1775eb1d1641SHerbert Xu 
17767cb3f921SNikolay Aleksandrov void br_multicast_enable_port(struct net_bridge_port *port)
17777cb3f921SNikolay Aleksandrov {
17787cb3f921SNikolay Aleksandrov 	struct net_bridge *br = port->br;
17797cb3f921SNikolay Aleksandrov 
17807cb3f921SNikolay Aleksandrov 	spin_lock(&br->multicast_lock);
17817cb3f921SNikolay Aleksandrov 	__br_multicast_enable_port(port);
1782eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1783eb1d1641SHerbert Xu }
1784eb1d1641SHerbert Xu 
1785eb1d1641SHerbert Xu void br_multicast_disable_port(struct net_bridge_port *port)
1786eb1d1641SHerbert Xu {
1787eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
1788eb1d1641SHerbert Xu 	struct net_bridge_port_group *pg;
1789b67bfe0dSSasha Levin 	struct hlist_node *n;
1790ed2d3597SLinus Lüssing 	bool del = false;
1791eb1d1641SHerbert Xu 
1792eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
1793b67bfe0dSSasha Levin 	hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
17949d06b6d8SElad Raz 		if (!(pg->flags & MDB_PG_FLAGS_PERMANENT))
1795681590bdSNikolay Aleksandrov 			br_multicast_find_del_pg(br, pg);
1796eb1d1641SHerbert Xu 
1797ed2d3597SLinus Lüssing 	del |= br_ip4_multicast_rport_del(port);
17989632233eSNikolay Aleksandrov 	del_timer(&port->multicast_ctx.ip4_mc_router_timer);
17999632233eSNikolay Aleksandrov 	del_timer(&port->multicast_ctx.ip4_own_query.timer);
1800a3c02e76SLinus Lüssing 	del |= br_ip6_multicast_rport_del(port);
1801cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
18029632233eSNikolay Aleksandrov 	del_timer(&port->multicast_ctx.ip6_mc_router_timer);
18039632233eSNikolay Aleksandrov 	del_timer(&port->multicast_ctx.ip6_own_query.timer);
1804cc0fdd80SLinus Lüssing #endif
1805ed2d3597SLinus Lüssing 	br_multicast_rport_del_notify(port, del);
1806eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1807eb1d1641SHerbert Xu }
1808eb1d1641SHerbert Xu 
1809e6231bcaSNikolay Aleksandrov static int __grp_src_delete_marked(struct net_bridge_port_group *pg)
1810e6231bcaSNikolay Aleksandrov {
1811e6231bcaSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
1812e6231bcaSNikolay Aleksandrov 	struct hlist_node *tmp;
1813e6231bcaSNikolay Aleksandrov 	int deleted = 0;
1814e6231bcaSNikolay Aleksandrov 
1815e6231bcaSNikolay Aleksandrov 	hlist_for_each_entry_safe(ent, tmp, &pg->src_list, node)
1816e6231bcaSNikolay Aleksandrov 		if (ent->flags & BR_SGRP_F_DELETE) {
1817d5a10222SNikolay Aleksandrov 			br_multicast_del_group_src(ent, false);
1818e6231bcaSNikolay Aleksandrov 			deleted++;
1819e6231bcaSNikolay Aleksandrov 		}
1820e6231bcaSNikolay Aleksandrov 
1821e6231bcaSNikolay Aleksandrov 	return deleted;
1822e6231bcaSNikolay Aleksandrov }
1823e6231bcaSNikolay Aleksandrov 
1824b0812368SNikolay Aleksandrov static void __grp_src_mod_timer(struct net_bridge_group_src *src,
1825b0812368SNikolay Aleksandrov 				unsigned long expires)
1826b0812368SNikolay Aleksandrov {
1827b0812368SNikolay Aleksandrov 	mod_timer(&src->timer, expires);
1828b0812368SNikolay Aleksandrov 	br_multicast_fwd_src_handle(src);
1829b0812368SNikolay Aleksandrov }
1830b0812368SNikolay Aleksandrov 
18315bf1e00bSNikolay Aleksandrov static void __grp_src_query_marked_and_rexmit(struct net_bridge_port_group *pg)
18325bf1e00bSNikolay Aleksandrov {
18335bf1e00bSNikolay Aleksandrov 	struct bridge_mcast_other_query *other_query = NULL;
1834085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
1835*d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = &br->multicast_ctx;
1836*d3d065c0SNikolay Aleksandrov 	u32 lmqc = brmctx->multicast_last_member_count;
18375bf1e00bSNikolay Aleksandrov 	unsigned long lmqt, lmi, now = jiffies;
18385bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
18395bf1e00bSNikolay Aleksandrov 
18405bf1e00bSNikolay Aleksandrov 	if (!netif_running(br->dev) ||
18415bf1e00bSNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_ENABLED))
18425bf1e00bSNikolay Aleksandrov 		return;
18435bf1e00bSNikolay Aleksandrov 
1844085b53c8SNikolay Aleksandrov 	if (pg->key.addr.proto == htons(ETH_P_IP))
1845*d3d065c0SNikolay Aleksandrov 		other_query = &brmctx->ip4_other_query;
18465bf1e00bSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
18475bf1e00bSNikolay Aleksandrov 	else
1848*d3d065c0SNikolay Aleksandrov 		other_query = &brmctx->ip6_other_query;
18495bf1e00bSNikolay Aleksandrov #endif
18505bf1e00bSNikolay Aleksandrov 
18515bf1e00bSNikolay Aleksandrov 	lmqt = now + br_multicast_lmqt(br);
18525bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node) {
18535bf1e00bSNikolay Aleksandrov 		if (ent->flags & BR_SGRP_F_SEND) {
18545bf1e00bSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_SEND;
18555bf1e00bSNikolay Aleksandrov 			if (ent->timer.expires > lmqt) {
18565bf1e00bSNikolay Aleksandrov 				if (br_opt_get(br, BROPT_MULTICAST_QUERIER) &&
18575bf1e00bSNikolay Aleksandrov 				    other_query &&
18585bf1e00bSNikolay Aleksandrov 				    !timer_pending(&other_query->timer))
18595bf1e00bSNikolay Aleksandrov 					ent->src_query_rexmit_cnt = lmqc;
1860b0812368SNikolay Aleksandrov 				__grp_src_mod_timer(ent, lmqt);
18615bf1e00bSNikolay Aleksandrov 			}
18625bf1e00bSNikolay Aleksandrov 		}
18635bf1e00bSNikolay Aleksandrov 	}
18645bf1e00bSNikolay Aleksandrov 
18655bf1e00bSNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_QUERIER) ||
18665bf1e00bSNikolay Aleksandrov 	    !other_query || timer_pending(&other_query->timer))
18675bf1e00bSNikolay Aleksandrov 		return;
18685bf1e00bSNikolay Aleksandrov 
1869085b53c8SNikolay Aleksandrov 	__br_multicast_send_query(br, pg->key.port, pg, &pg->key.addr,
1870085b53c8SNikolay Aleksandrov 				  &pg->key.addr, true, 1, NULL);
18715bf1e00bSNikolay Aleksandrov 
1872*d3d065c0SNikolay Aleksandrov 	lmi = now + brmctx->multicast_last_member_interval;
18735bf1e00bSNikolay Aleksandrov 	if (!timer_pending(&pg->rexmit_timer) ||
18745bf1e00bSNikolay Aleksandrov 	    time_after(pg->rexmit_timer.expires, lmi))
18755bf1e00bSNikolay Aleksandrov 		mod_timer(&pg->rexmit_timer, lmi);
18765bf1e00bSNikolay Aleksandrov }
18775bf1e00bSNikolay Aleksandrov 
18785bf1e00bSNikolay Aleksandrov static void __grp_send_query_and_rexmit(struct net_bridge_port_group *pg)
18795bf1e00bSNikolay Aleksandrov {
18805bf1e00bSNikolay Aleksandrov 	struct bridge_mcast_other_query *other_query = NULL;
1881085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
1882*d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = &br->multicast_ctx;
18835bf1e00bSNikolay Aleksandrov 	unsigned long now = jiffies, lmi;
18845bf1e00bSNikolay Aleksandrov 
18855bf1e00bSNikolay Aleksandrov 	if (!netif_running(br->dev) ||
18865bf1e00bSNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_ENABLED))
18875bf1e00bSNikolay Aleksandrov 		return;
18885bf1e00bSNikolay Aleksandrov 
1889085b53c8SNikolay Aleksandrov 	if (pg->key.addr.proto == htons(ETH_P_IP))
1890*d3d065c0SNikolay Aleksandrov 		other_query = &brmctx->ip4_other_query;
18915bf1e00bSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
18925bf1e00bSNikolay Aleksandrov 	else
1893*d3d065c0SNikolay Aleksandrov 		other_query = &brmctx->ip6_other_query;
18945bf1e00bSNikolay Aleksandrov #endif
18955bf1e00bSNikolay Aleksandrov 
18965bf1e00bSNikolay Aleksandrov 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER) &&
18975bf1e00bSNikolay Aleksandrov 	    other_query && !timer_pending(&other_query->timer)) {
1898*d3d065c0SNikolay Aleksandrov 		lmi = now + brmctx->multicast_last_member_interval;
1899*d3d065c0SNikolay Aleksandrov 		pg->grp_query_rexmit_cnt = brmctx->multicast_last_member_count - 1;
1900085b53c8SNikolay Aleksandrov 		__br_multicast_send_query(br, pg->key.port, pg, &pg->key.addr,
1901085b53c8SNikolay Aleksandrov 					  &pg->key.addr, false, 0, NULL);
19025bf1e00bSNikolay Aleksandrov 		if (!timer_pending(&pg->rexmit_timer) ||
19035bf1e00bSNikolay Aleksandrov 		    time_after(pg->rexmit_timer.expires, lmi))
19045bf1e00bSNikolay Aleksandrov 			mod_timer(&pg->rexmit_timer, lmi);
19055bf1e00bSNikolay Aleksandrov 	}
19065bf1e00bSNikolay Aleksandrov 
19075bf1e00bSNikolay Aleksandrov 	if (pg->filter_mode == MCAST_EXCLUDE &&
19085bf1e00bSNikolay Aleksandrov 	    (!timer_pending(&pg->timer) ||
19095bf1e00bSNikolay Aleksandrov 	     time_after(pg->timer.expires, now + br_multicast_lmqt(br))))
19105bf1e00bSNikolay Aleksandrov 		mod_timer(&pg->timer, now + br_multicast_lmqt(br));
19115bf1e00bSNikolay Aleksandrov }
19125bf1e00bSNikolay Aleksandrov 
19130436862eSNikolay Aleksandrov /* State          Msg type      New state                Actions
19140436862eSNikolay Aleksandrov  * INCLUDE (A)    IS_IN (B)     INCLUDE (A+B)            (B)=GMI
19150436862eSNikolay Aleksandrov  * INCLUDE (A)    ALLOW (B)     INCLUDE (A+B)            (B)=GMI
19160436862eSNikolay Aleksandrov  * EXCLUDE (X,Y)  ALLOW (A)     EXCLUDE (X+A,Y-A)        (A)=GMI
19170436862eSNikolay Aleksandrov  */
191854bea721SNikolay Aleksandrov static bool br_multicast_isinc_allow(struct net_bridge_port_group *pg, void *h_addr,
1919474ddb37SNikolay Aleksandrov 				     void *srcs, u32 nsrcs, size_t addr_size,
1920474ddb37SNikolay Aleksandrov 				     int grec_type)
19210436862eSNikolay Aleksandrov {
1922085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
19230436862eSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
19240436862eSNikolay Aleksandrov 	unsigned long now = jiffies;
19250436862eSNikolay Aleksandrov 	bool changed = false;
19260436862eSNikolay Aleksandrov 	struct br_ip src_ip;
19270436862eSNikolay Aleksandrov 	u32 src_idx;
19280436862eSNikolay Aleksandrov 
19290436862eSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
1930085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
19310436862eSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
1932e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
19330436862eSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
19340436862eSNikolay Aleksandrov 		if (!ent) {
19350436862eSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
19360436862eSNikolay Aleksandrov 			if (ent)
19370436862eSNikolay Aleksandrov 				changed = true;
19380436862eSNikolay Aleksandrov 		}
19390436862eSNikolay Aleksandrov 
19400436862eSNikolay Aleksandrov 		if (ent)
1941b0812368SNikolay Aleksandrov 			__grp_src_mod_timer(ent, now + br_multicast_gmi(br));
19420436862eSNikolay Aleksandrov 	}
19430436862eSNikolay Aleksandrov 
1944474ddb37SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
1945474ddb37SNikolay Aleksandrov 		changed = true;
1946474ddb37SNikolay Aleksandrov 
19470436862eSNikolay Aleksandrov 	return changed;
19480436862eSNikolay Aleksandrov }
19490436862eSNikolay Aleksandrov 
1950e6231bcaSNikolay Aleksandrov /* State          Msg type      New state                Actions
1951e6231bcaSNikolay Aleksandrov  * INCLUDE (A)    IS_EX (B)     EXCLUDE (A*B,B-A)        (B-A)=0
1952e6231bcaSNikolay Aleksandrov  *                                                       Delete (A-B)
1953e6231bcaSNikolay Aleksandrov  *                                                       Group Timer=GMI
1954e6231bcaSNikolay Aleksandrov  */
195554bea721SNikolay Aleksandrov static void __grp_src_isexc_incl(struct net_bridge_port_group *pg, void *h_addr,
1956ddc255d9SNikolay Aleksandrov 				 void *srcs, u32 nsrcs, size_t addr_size,
1957ddc255d9SNikolay Aleksandrov 				 int grec_type)
1958e6231bcaSNikolay Aleksandrov {
1959e6231bcaSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
1960e6231bcaSNikolay Aleksandrov 	struct br_ip src_ip;
1961e6231bcaSNikolay Aleksandrov 	u32 src_idx;
1962e6231bcaSNikolay Aleksandrov 
1963e6231bcaSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
1964e6231bcaSNikolay Aleksandrov 		ent->flags |= BR_SGRP_F_DELETE;
1965e6231bcaSNikolay Aleksandrov 
1966e6231bcaSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
1967085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
1968e6231bcaSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
1969e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
1970e6231bcaSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
1971e6231bcaSNikolay Aleksandrov 		if (ent)
1972e6231bcaSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_DELETE;
1973e6231bcaSNikolay Aleksandrov 		else
1974b0812368SNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
1975b0812368SNikolay Aleksandrov 		if (ent)
1976b0812368SNikolay Aleksandrov 			br_multicast_fwd_src_handle(ent);
1977e6231bcaSNikolay Aleksandrov 	}
1978e6231bcaSNikolay Aleksandrov 
1979ddc255d9SNikolay Aleksandrov 	br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type);
1980ddc255d9SNikolay Aleksandrov 
1981e6231bcaSNikolay Aleksandrov 	__grp_src_delete_marked(pg);
1982e6231bcaSNikolay Aleksandrov }
1983e6231bcaSNikolay Aleksandrov 
1984e6231bcaSNikolay Aleksandrov /* State          Msg type      New state                Actions
1985e6231bcaSNikolay Aleksandrov  * EXCLUDE (X,Y)  IS_EX (A)     EXCLUDE (A-Y,Y*A)        (A-X-Y)=GMI
1986e6231bcaSNikolay Aleksandrov  *                                                       Delete (X-A)
1987e6231bcaSNikolay Aleksandrov  *                                                       Delete (Y-A)
1988e6231bcaSNikolay Aleksandrov  *                                                       Group Timer=GMI
1989e6231bcaSNikolay Aleksandrov  */
199054bea721SNikolay Aleksandrov static bool __grp_src_isexc_excl(struct net_bridge_port_group *pg, void *h_addr,
1991ddc255d9SNikolay Aleksandrov 				 void *srcs, u32 nsrcs, size_t addr_size,
1992ddc255d9SNikolay Aleksandrov 				 int grec_type)
1993e6231bcaSNikolay Aleksandrov {
1994085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
1995e6231bcaSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
1996e6231bcaSNikolay Aleksandrov 	unsigned long now = jiffies;
1997e6231bcaSNikolay Aleksandrov 	bool changed = false;
1998e6231bcaSNikolay Aleksandrov 	struct br_ip src_ip;
1999e6231bcaSNikolay Aleksandrov 	u32 src_idx;
2000e6231bcaSNikolay Aleksandrov 
2001e6231bcaSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
2002e6231bcaSNikolay Aleksandrov 		ent->flags |= BR_SGRP_F_DELETE;
2003e6231bcaSNikolay Aleksandrov 
2004e6231bcaSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2005085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
2006e6231bcaSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2007e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2008e6231bcaSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
2009e6231bcaSNikolay Aleksandrov 		if (ent) {
2010e6231bcaSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_DELETE;
2011e6231bcaSNikolay Aleksandrov 		} else {
2012e6231bcaSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
2013e6231bcaSNikolay Aleksandrov 			if (ent) {
2014b0812368SNikolay Aleksandrov 				__grp_src_mod_timer(ent,
2015e6231bcaSNikolay Aleksandrov 						    now + br_multicast_gmi(br));
2016e6231bcaSNikolay Aleksandrov 				changed = true;
2017e6231bcaSNikolay Aleksandrov 			}
2018e6231bcaSNikolay Aleksandrov 		}
2019e6231bcaSNikolay Aleksandrov 	}
2020e6231bcaSNikolay Aleksandrov 
2021ddc255d9SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
2022ddc255d9SNikolay Aleksandrov 		changed = true;
2023ddc255d9SNikolay Aleksandrov 
2024e6231bcaSNikolay Aleksandrov 	if (__grp_src_delete_marked(pg))
2025e6231bcaSNikolay Aleksandrov 		changed = true;
2026e6231bcaSNikolay Aleksandrov 
2027e6231bcaSNikolay Aleksandrov 	return changed;
2028e6231bcaSNikolay Aleksandrov }
2029e6231bcaSNikolay Aleksandrov 
203054bea721SNikolay Aleksandrov static bool br_multicast_isexc(struct net_bridge_port_group *pg, void *h_addr,
2031ddc255d9SNikolay Aleksandrov 			       void *srcs, u32 nsrcs, size_t addr_size,
2032ddc255d9SNikolay Aleksandrov 			       int grec_type)
2033e6231bcaSNikolay Aleksandrov {
2034085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
2035e6231bcaSNikolay Aleksandrov 	bool changed = false;
2036e6231bcaSNikolay Aleksandrov 
2037e6231bcaSNikolay Aleksandrov 	switch (pg->filter_mode) {
2038e6231bcaSNikolay Aleksandrov 	case MCAST_INCLUDE:
2039ddc255d9SNikolay Aleksandrov 		__grp_src_isexc_incl(pg, h_addr, srcs, nsrcs, addr_size,
2040ddc255d9SNikolay Aleksandrov 				     grec_type);
20418266a049SNikolay Aleksandrov 		br_multicast_star_g_handle_mode(pg, MCAST_EXCLUDE);
2042e6231bcaSNikolay Aleksandrov 		changed = true;
2043e6231bcaSNikolay Aleksandrov 		break;
2044e6231bcaSNikolay Aleksandrov 	case MCAST_EXCLUDE:
2045ddc255d9SNikolay Aleksandrov 		changed = __grp_src_isexc_excl(pg, h_addr, srcs, nsrcs, addr_size,
2046ddc255d9SNikolay Aleksandrov 					       grec_type);
2047e6231bcaSNikolay Aleksandrov 		break;
2048e6231bcaSNikolay Aleksandrov 	}
2049e6231bcaSNikolay Aleksandrov 
2050e6231bcaSNikolay Aleksandrov 	pg->filter_mode = MCAST_EXCLUDE;
2051e6231bcaSNikolay Aleksandrov 	mod_timer(&pg->timer, jiffies + br_multicast_gmi(br));
2052e6231bcaSNikolay Aleksandrov 
2053e6231bcaSNikolay Aleksandrov 	return changed;
2054e6231bcaSNikolay Aleksandrov }
2055e6231bcaSNikolay Aleksandrov 
20565bf1e00bSNikolay Aleksandrov /* State          Msg type      New state                Actions
20575bf1e00bSNikolay Aleksandrov  * INCLUDE (A)    TO_IN (B)     INCLUDE (A+B)            (B)=GMI
20585bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G,A-B)
20595bf1e00bSNikolay Aleksandrov  */
206054bea721SNikolay Aleksandrov static bool __grp_src_toin_incl(struct net_bridge_port_group *pg, void *h_addr,
2061ddc255d9SNikolay Aleksandrov 				void *srcs, u32 nsrcs, size_t addr_size,
2062ddc255d9SNikolay Aleksandrov 				int grec_type)
20635bf1e00bSNikolay Aleksandrov {
2064085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
20655bf1e00bSNikolay Aleksandrov 	u32 src_idx, to_send = pg->src_ents;
20665bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
20675bf1e00bSNikolay Aleksandrov 	unsigned long now = jiffies;
20685bf1e00bSNikolay Aleksandrov 	bool changed = false;
20695bf1e00bSNikolay Aleksandrov 	struct br_ip src_ip;
20705bf1e00bSNikolay Aleksandrov 
20715bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
20725bf1e00bSNikolay Aleksandrov 		ent->flags |= BR_SGRP_F_SEND;
20735bf1e00bSNikolay Aleksandrov 
20745bf1e00bSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2075085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
20765bf1e00bSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2077e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
20785bf1e00bSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
20795bf1e00bSNikolay Aleksandrov 		if (ent) {
20805bf1e00bSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_SEND;
20815bf1e00bSNikolay Aleksandrov 			to_send--;
20825bf1e00bSNikolay Aleksandrov 		} else {
20835bf1e00bSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
20845bf1e00bSNikolay Aleksandrov 			if (ent)
20855bf1e00bSNikolay Aleksandrov 				changed = true;
20865bf1e00bSNikolay Aleksandrov 		}
20875bf1e00bSNikolay Aleksandrov 		if (ent)
2088b0812368SNikolay Aleksandrov 			__grp_src_mod_timer(ent, now + br_multicast_gmi(br));
20895bf1e00bSNikolay Aleksandrov 	}
20905bf1e00bSNikolay Aleksandrov 
2091ddc255d9SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
2092ddc255d9SNikolay Aleksandrov 		changed = true;
2093ddc255d9SNikolay Aleksandrov 
20945bf1e00bSNikolay Aleksandrov 	if (to_send)
20955bf1e00bSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
20965bf1e00bSNikolay Aleksandrov 
20975bf1e00bSNikolay Aleksandrov 	return changed;
20985bf1e00bSNikolay Aleksandrov }
20995bf1e00bSNikolay Aleksandrov 
21005bf1e00bSNikolay Aleksandrov /* State          Msg type      New state                Actions
21015bf1e00bSNikolay Aleksandrov  * EXCLUDE (X,Y)  TO_IN (A)     EXCLUDE (X+A,Y-A)        (A)=GMI
21025bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G,X-A)
21035bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G)
21045bf1e00bSNikolay Aleksandrov  */
210554bea721SNikolay Aleksandrov static bool __grp_src_toin_excl(struct net_bridge_port_group *pg, void *h_addr,
2106ddc255d9SNikolay Aleksandrov 				void *srcs, u32 nsrcs, size_t addr_size,
2107ddc255d9SNikolay Aleksandrov 				int grec_type)
21085bf1e00bSNikolay Aleksandrov {
2109085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
21105bf1e00bSNikolay Aleksandrov 	u32 src_idx, to_send = pg->src_ents;
21115bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
21125bf1e00bSNikolay Aleksandrov 	unsigned long now = jiffies;
21135bf1e00bSNikolay Aleksandrov 	bool changed = false;
21145bf1e00bSNikolay Aleksandrov 	struct br_ip src_ip;
21155bf1e00bSNikolay Aleksandrov 
21165bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
21175bf1e00bSNikolay Aleksandrov 		if (timer_pending(&ent->timer))
21185bf1e00bSNikolay Aleksandrov 			ent->flags |= BR_SGRP_F_SEND;
21195bf1e00bSNikolay Aleksandrov 
21205bf1e00bSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2121085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
21225bf1e00bSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2123e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
21245bf1e00bSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
21255bf1e00bSNikolay Aleksandrov 		if (ent) {
21265bf1e00bSNikolay Aleksandrov 			if (timer_pending(&ent->timer)) {
21275bf1e00bSNikolay Aleksandrov 				ent->flags &= ~BR_SGRP_F_SEND;
21285bf1e00bSNikolay Aleksandrov 				to_send--;
21295bf1e00bSNikolay Aleksandrov 			}
21305bf1e00bSNikolay Aleksandrov 		} else {
21315bf1e00bSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
21325bf1e00bSNikolay Aleksandrov 			if (ent)
21335bf1e00bSNikolay Aleksandrov 				changed = true;
21345bf1e00bSNikolay Aleksandrov 		}
21355bf1e00bSNikolay Aleksandrov 		if (ent)
2136b0812368SNikolay Aleksandrov 			__grp_src_mod_timer(ent, now + br_multicast_gmi(br));
21375bf1e00bSNikolay Aleksandrov 	}
21385bf1e00bSNikolay Aleksandrov 
2139ddc255d9SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
2140ddc255d9SNikolay Aleksandrov 		changed = true;
2141ddc255d9SNikolay Aleksandrov 
21425bf1e00bSNikolay Aleksandrov 	if (to_send)
21435bf1e00bSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
21445bf1e00bSNikolay Aleksandrov 
21455bf1e00bSNikolay Aleksandrov 	__grp_send_query_and_rexmit(pg);
21465bf1e00bSNikolay Aleksandrov 
21475bf1e00bSNikolay Aleksandrov 	return changed;
21485bf1e00bSNikolay Aleksandrov }
21495bf1e00bSNikolay Aleksandrov 
215054bea721SNikolay Aleksandrov static bool br_multicast_toin(struct net_bridge_port_group *pg, void *h_addr,
2151ddc255d9SNikolay Aleksandrov 			      void *srcs, u32 nsrcs, size_t addr_size,
2152ddc255d9SNikolay Aleksandrov 			      int grec_type)
21535bf1e00bSNikolay Aleksandrov {
21545bf1e00bSNikolay Aleksandrov 	bool changed = false;
21555bf1e00bSNikolay Aleksandrov 
21565bf1e00bSNikolay Aleksandrov 	switch (pg->filter_mode) {
21575bf1e00bSNikolay Aleksandrov 	case MCAST_INCLUDE:
2158ddc255d9SNikolay Aleksandrov 		changed = __grp_src_toin_incl(pg, h_addr, srcs, nsrcs, addr_size,
2159ddc255d9SNikolay Aleksandrov 					      grec_type);
21605bf1e00bSNikolay Aleksandrov 		break;
21615bf1e00bSNikolay Aleksandrov 	case MCAST_EXCLUDE:
2162ddc255d9SNikolay Aleksandrov 		changed = __grp_src_toin_excl(pg, h_addr, srcs, nsrcs, addr_size,
2163ddc255d9SNikolay Aleksandrov 					      grec_type);
21645bf1e00bSNikolay Aleksandrov 		break;
21655bf1e00bSNikolay Aleksandrov 	}
21665bf1e00bSNikolay Aleksandrov 
2167ddc255d9SNikolay Aleksandrov 	if (br_multicast_eht_should_del_pg(pg)) {
2168d5a10222SNikolay Aleksandrov 		pg->flags |= MDB_PG_FLAGS_FAST_LEAVE;
2169ddc255d9SNikolay Aleksandrov 		br_multicast_find_del_pg(pg->key.port->br, pg);
2170ddc255d9SNikolay Aleksandrov 		/* a notification has already been sent and we shouldn't
2171ddc255d9SNikolay Aleksandrov 		 * access pg after the delete so we have to return false
2172ddc255d9SNikolay Aleksandrov 		 */
2173ddc255d9SNikolay Aleksandrov 		changed = false;
2174ddc255d9SNikolay Aleksandrov 	}
2175ddc255d9SNikolay Aleksandrov 
21765bf1e00bSNikolay Aleksandrov 	return changed;
21775bf1e00bSNikolay Aleksandrov }
21785bf1e00bSNikolay Aleksandrov 
21795bf1e00bSNikolay Aleksandrov /* State          Msg type      New state                Actions
21805bf1e00bSNikolay Aleksandrov  * INCLUDE (A)    TO_EX (B)     EXCLUDE (A*B,B-A)        (B-A)=0
21815bf1e00bSNikolay Aleksandrov  *                                                       Delete (A-B)
21825bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G,A*B)
21835bf1e00bSNikolay Aleksandrov  *                                                       Group Timer=GMI
21845bf1e00bSNikolay Aleksandrov  */
218554bea721SNikolay Aleksandrov static void __grp_src_toex_incl(struct net_bridge_port_group *pg, void *h_addr,
2186ddc255d9SNikolay Aleksandrov 				void *srcs, u32 nsrcs, size_t addr_size,
2187ddc255d9SNikolay Aleksandrov 				int grec_type)
21885bf1e00bSNikolay Aleksandrov {
21895bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
21905bf1e00bSNikolay Aleksandrov 	u32 src_idx, to_send = 0;
21915bf1e00bSNikolay Aleksandrov 	struct br_ip src_ip;
21925bf1e00bSNikolay Aleksandrov 
21935bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
21945bf1e00bSNikolay Aleksandrov 		ent->flags = (ent->flags & ~BR_SGRP_F_SEND) | BR_SGRP_F_DELETE;
21955bf1e00bSNikolay Aleksandrov 
21965bf1e00bSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2197085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
21985bf1e00bSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2199e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
22005bf1e00bSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
22015bf1e00bSNikolay Aleksandrov 		if (ent) {
22025bf1e00bSNikolay Aleksandrov 			ent->flags = (ent->flags & ~BR_SGRP_F_DELETE) |
22035bf1e00bSNikolay Aleksandrov 				     BR_SGRP_F_SEND;
22045bf1e00bSNikolay Aleksandrov 			to_send++;
22055bf1e00bSNikolay Aleksandrov 		} else {
2206b0812368SNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
22075bf1e00bSNikolay Aleksandrov 		}
2208b0812368SNikolay Aleksandrov 		if (ent)
2209b0812368SNikolay Aleksandrov 			br_multicast_fwd_src_handle(ent);
22105bf1e00bSNikolay Aleksandrov 	}
22115bf1e00bSNikolay Aleksandrov 
2212ddc255d9SNikolay Aleksandrov 	br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type);
2213ddc255d9SNikolay Aleksandrov 
22145bf1e00bSNikolay Aleksandrov 	__grp_src_delete_marked(pg);
22155bf1e00bSNikolay Aleksandrov 	if (to_send)
22165bf1e00bSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
22175bf1e00bSNikolay Aleksandrov }
22185bf1e00bSNikolay Aleksandrov 
22195bf1e00bSNikolay Aleksandrov /* State          Msg type      New state                Actions
22205bf1e00bSNikolay Aleksandrov  * EXCLUDE (X,Y)  TO_EX (A)     EXCLUDE (A-Y,Y*A)        (A-X-Y)=Group Timer
22215bf1e00bSNikolay Aleksandrov  *                                                       Delete (X-A)
22225bf1e00bSNikolay Aleksandrov  *                                                       Delete (Y-A)
22235bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G,A-Y)
22245bf1e00bSNikolay Aleksandrov  *                                                       Group Timer=GMI
22255bf1e00bSNikolay Aleksandrov  */
222654bea721SNikolay Aleksandrov static bool __grp_src_toex_excl(struct net_bridge_port_group *pg, void *h_addr,
2227ddc255d9SNikolay Aleksandrov 				void *srcs, u32 nsrcs, size_t addr_size,
2228ddc255d9SNikolay Aleksandrov 				int grec_type)
22295bf1e00bSNikolay Aleksandrov {
22305bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
22315bf1e00bSNikolay Aleksandrov 	u32 src_idx, to_send = 0;
22325bf1e00bSNikolay Aleksandrov 	bool changed = false;
22335bf1e00bSNikolay Aleksandrov 	struct br_ip src_ip;
22345bf1e00bSNikolay Aleksandrov 
22355bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
22365bf1e00bSNikolay Aleksandrov 		ent->flags = (ent->flags & ~BR_SGRP_F_SEND) | BR_SGRP_F_DELETE;
22375bf1e00bSNikolay Aleksandrov 
22385bf1e00bSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2239085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
22405bf1e00bSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2241e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
22425bf1e00bSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
22435bf1e00bSNikolay Aleksandrov 		if (ent) {
22445bf1e00bSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_DELETE;
22455bf1e00bSNikolay Aleksandrov 		} else {
22465bf1e00bSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
22475bf1e00bSNikolay Aleksandrov 			if (ent) {
2248b0812368SNikolay Aleksandrov 				__grp_src_mod_timer(ent, pg->timer.expires);
22495bf1e00bSNikolay Aleksandrov 				changed = true;
22505bf1e00bSNikolay Aleksandrov 			}
22515bf1e00bSNikolay Aleksandrov 		}
22525bf1e00bSNikolay Aleksandrov 		if (ent && timer_pending(&ent->timer)) {
22535bf1e00bSNikolay Aleksandrov 			ent->flags |= BR_SGRP_F_SEND;
22545bf1e00bSNikolay Aleksandrov 			to_send++;
22555bf1e00bSNikolay Aleksandrov 		}
22565bf1e00bSNikolay Aleksandrov 	}
22575bf1e00bSNikolay Aleksandrov 
2258ddc255d9SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
2259ddc255d9SNikolay Aleksandrov 		changed = true;
2260ddc255d9SNikolay Aleksandrov 
22615bf1e00bSNikolay Aleksandrov 	if (__grp_src_delete_marked(pg))
22625bf1e00bSNikolay Aleksandrov 		changed = true;
22635bf1e00bSNikolay Aleksandrov 	if (to_send)
22645bf1e00bSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
22655bf1e00bSNikolay Aleksandrov 
22665bf1e00bSNikolay Aleksandrov 	return changed;
22675bf1e00bSNikolay Aleksandrov }
22685bf1e00bSNikolay Aleksandrov 
226954bea721SNikolay Aleksandrov static bool br_multicast_toex(struct net_bridge_port_group *pg, void *h_addr,
2270ddc255d9SNikolay Aleksandrov 			      void *srcs, u32 nsrcs, size_t addr_size,
2271ddc255d9SNikolay Aleksandrov 			      int grec_type)
22725bf1e00bSNikolay Aleksandrov {
2273085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
22745bf1e00bSNikolay Aleksandrov 	bool changed = false;
22755bf1e00bSNikolay Aleksandrov 
22765bf1e00bSNikolay Aleksandrov 	switch (pg->filter_mode) {
22775bf1e00bSNikolay Aleksandrov 	case MCAST_INCLUDE:
2278ddc255d9SNikolay Aleksandrov 		__grp_src_toex_incl(pg, h_addr, srcs, nsrcs, addr_size,
2279ddc255d9SNikolay Aleksandrov 				    grec_type);
22808266a049SNikolay Aleksandrov 		br_multicast_star_g_handle_mode(pg, MCAST_EXCLUDE);
22815bf1e00bSNikolay Aleksandrov 		changed = true;
22825bf1e00bSNikolay Aleksandrov 		break;
22835bf1e00bSNikolay Aleksandrov 	case MCAST_EXCLUDE:
2284ddc255d9SNikolay Aleksandrov 		changed = __grp_src_toex_excl(pg, h_addr, srcs, nsrcs, addr_size,
2285ddc255d9SNikolay Aleksandrov 					      grec_type);
22865bf1e00bSNikolay Aleksandrov 		break;
22875bf1e00bSNikolay Aleksandrov 	}
22885bf1e00bSNikolay Aleksandrov 
22895bf1e00bSNikolay Aleksandrov 	pg->filter_mode = MCAST_EXCLUDE;
22905bf1e00bSNikolay Aleksandrov 	mod_timer(&pg->timer, jiffies + br_multicast_gmi(br));
22915bf1e00bSNikolay Aleksandrov 
22925bf1e00bSNikolay Aleksandrov 	return changed;
22935bf1e00bSNikolay Aleksandrov }
22945bf1e00bSNikolay Aleksandrov 
2295109865feSNikolay Aleksandrov /* State          Msg type      New state                Actions
2296109865feSNikolay Aleksandrov  * INCLUDE (A)    BLOCK (B)     INCLUDE (A)              Send Q(G,A*B)
2297109865feSNikolay Aleksandrov  */
22980ad57c99SNikolay Aleksandrov static bool __grp_src_block_incl(struct net_bridge_port_group *pg, void *h_addr,
2299474ddb37SNikolay Aleksandrov 				 void *srcs, u32 nsrcs, size_t addr_size, int grec_type)
2300109865feSNikolay Aleksandrov {
2301109865feSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
2302109865feSNikolay Aleksandrov 	u32 src_idx, to_send = 0;
23030ad57c99SNikolay Aleksandrov 	bool changed = false;
2304109865feSNikolay Aleksandrov 	struct br_ip src_ip;
2305109865feSNikolay Aleksandrov 
2306109865feSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
2307109865feSNikolay Aleksandrov 		ent->flags &= ~BR_SGRP_F_SEND;
2308109865feSNikolay Aleksandrov 
2309109865feSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2310085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
2311109865feSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2312e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2313109865feSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
2314109865feSNikolay Aleksandrov 		if (ent) {
2315109865feSNikolay Aleksandrov 			ent->flags |= BR_SGRP_F_SEND;
2316109865feSNikolay Aleksandrov 			to_send++;
2317109865feSNikolay Aleksandrov 		}
2318109865feSNikolay Aleksandrov 	}
2319109865feSNikolay Aleksandrov 
2320474ddb37SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
2321474ddb37SNikolay Aleksandrov 		changed = true;
2322474ddb37SNikolay Aleksandrov 
2323109865feSNikolay Aleksandrov 	if (to_send)
2324109865feSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
2325109865feSNikolay Aleksandrov 
23260ad57c99SNikolay Aleksandrov 	return changed;
2327109865feSNikolay Aleksandrov }
2328109865feSNikolay Aleksandrov 
2329109865feSNikolay Aleksandrov /* State          Msg type      New state                Actions
2330109865feSNikolay Aleksandrov  * EXCLUDE (X,Y)  BLOCK (A)     EXCLUDE (X+(A-Y),Y)      (A-X-Y)=Group Timer
2331109865feSNikolay Aleksandrov  *                                                       Send Q(G,A-Y)
2332109865feSNikolay Aleksandrov  */
233354bea721SNikolay Aleksandrov static bool __grp_src_block_excl(struct net_bridge_port_group *pg, void *h_addr,
2334474ddb37SNikolay Aleksandrov 				 void *srcs, u32 nsrcs, size_t addr_size, int grec_type)
2335109865feSNikolay Aleksandrov {
2336109865feSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
2337109865feSNikolay Aleksandrov 	u32 src_idx, to_send = 0;
2338109865feSNikolay Aleksandrov 	bool changed = false;
2339109865feSNikolay Aleksandrov 	struct br_ip src_ip;
2340109865feSNikolay Aleksandrov 
2341109865feSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
2342109865feSNikolay Aleksandrov 		ent->flags &= ~BR_SGRP_F_SEND;
2343109865feSNikolay Aleksandrov 
2344109865feSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2345085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
2346109865feSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2347e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2348109865feSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
2349109865feSNikolay Aleksandrov 		if (!ent) {
2350109865feSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
2351109865feSNikolay Aleksandrov 			if (ent) {
2352b0812368SNikolay Aleksandrov 				__grp_src_mod_timer(ent, pg->timer.expires);
2353109865feSNikolay Aleksandrov 				changed = true;
2354109865feSNikolay Aleksandrov 			}
2355109865feSNikolay Aleksandrov 		}
2356109865feSNikolay Aleksandrov 		if (ent && timer_pending(&ent->timer)) {
2357109865feSNikolay Aleksandrov 			ent->flags |= BR_SGRP_F_SEND;
2358109865feSNikolay Aleksandrov 			to_send++;
2359109865feSNikolay Aleksandrov 		}
2360109865feSNikolay Aleksandrov 	}
2361109865feSNikolay Aleksandrov 
2362474ddb37SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
2363474ddb37SNikolay Aleksandrov 		changed = true;
2364474ddb37SNikolay Aleksandrov 
2365109865feSNikolay Aleksandrov 	if (to_send)
2366109865feSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
2367109865feSNikolay Aleksandrov 
2368109865feSNikolay Aleksandrov 	return changed;
2369109865feSNikolay Aleksandrov }
2370109865feSNikolay Aleksandrov 
237154bea721SNikolay Aleksandrov static bool br_multicast_block(struct net_bridge_port_group *pg, void *h_addr,
2372474ddb37SNikolay Aleksandrov 			       void *srcs, u32 nsrcs, size_t addr_size, int grec_type)
2373109865feSNikolay Aleksandrov {
2374109865feSNikolay Aleksandrov 	bool changed = false;
2375109865feSNikolay Aleksandrov 
2376109865feSNikolay Aleksandrov 	switch (pg->filter_mode) {
2377109865feSNikolay Aleksandrov 	case MCAST_INCLUDE:
2378474ddb37SNikolay Aleksandrov 		changed = __grp_src_block_incl(pg, h_addr, srcs, nsrcs, addr_size,
2379474ddb37SNikolay Aleksandrov 					       grec_type);
2380109865feSNikolay Aleksandrov 		break;
2381109865feSNikolay Aleksandrov 	case MCAST_EXCLUDE:
2382474ddb37SNikolay Aleksandrov 		changed = __grp_src_block_excl(pg, h_addr, srcs, nsrcs, addr_size,
2383474ddb37SNikolay Aleksandrov 					       grec_type);
2384109865feSNikolay Aleksandrov 		break;
2385109865feSNikolay Aleksandrov 	}
2386109865feSNikolay Aleksandrov 
2387e87e4b5cSNikolay Aleksandrov 	if ((pg->filter_mode == MCAST_INCLUDE && hlist_empty(&pg->src_list)) ||
2388e87e4b5cSNikolay Aleksandrov 	    br_multicast_eht_should_del_pg(pg)) {
2389d5a10222SNikolay Aleksandrov 		if (br_multicast_eht_should_del_pg(pg))
2390d5a10222SNikolay Aleksandrov 			pg->flags |= MDB_PG_FLAGS_FAST_LEAVE;
2391e87e4b5cSNikolay Aleksandrov 		br_multicast_find_del_pg(pg->key.port->br, pg);
2392e87e4b5cSNikolay Aleksandrov 		/* a notification has already been sent and we shouldn't
2393e87e4b5cSNikolay Aleksandrov 		 * access pg after the delete so we have to return false
2394e87e4b5cSNikolay Aleksandrov 		 */
2395e87e4b5cSNikolay Aleksandrov 		changed = false;
2396e87e4b5cSNikolay Aleksandrov 	}
2397e87e4b5cSNikolay Aleksandrov 
2398109865feSNikolay Aleksandrov 	return changed;
2399109865feSNikolay Aleksandrov }
2400109865feSNikolay Aleksandrov 
24010436862eSNikolay Aleksandrov static struct net_bridge_port_group *
24020436862eSNikolay Aleksandrov br_multicast_find_port(struct net_bridge_mdb_entry *mp,
24030436862eSNikolay Aleksandrov 		       struct net_bridge_port *p,
24040436862eSNikolay Aleksandrov 		       const unsigned char *src)
24050436862eSNikolay Aleksandrov {
2406071445c6SNikolay Aleksandrov 	struct net_bridge *br __maybe_unused = mp->br;
24070436862eSNikolay Aleksandrov 	struct net_bridge_port_group *pg;
24080436862eSNikolay Aleksandrov 
24090436862eSNikolay Aleksandrov 	for (pg = mlock_dereference(mp->ports, br);
24100436862eSNikolay Aleksandrov 	     pg;
24110436862eSNikolay Aleksandrov 	     pg = mlock_dereference(pg->next, br))
24120436862eSNikolay Aleksandrov 		if (br_port_group_equal(pg, p, src))
24130436862eSNikolay Aleksandrov 			return pg;
24140436862eSNikolay Aleksandrov 
24150436862eSNikolay Aleksandrov 	return NULL;
24160436862eSNikolay Aleksandrov }
24170436862eSNikolay Aleksandrov 
24188ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
2419eb1d1641SHerbert Xu 					 struct net_bridge_port *port,
242006499098SVlad Yasevich 					 struct sk_buff *skb,
242106499098SVlad Yasevich 					 u16 vid)
2422eb1d1641SHerbert Xu {
2423*d3d065c0SNikolay Aleksandrov 	bool igmpv2 = br->multicast_ctx.multicast_igmp_version == 2;
24240436862eSNikolay Aleksandrov 	struct net_bridge_mdb_entry *mdst;
24250436862eSNikolay Aleksandrov 	struct net_bridge_port_group *pg;
24266db6f0eaSFelix Fietkau 	const unsigned char *src;
2427eb1d1641SHerbert Xu 	struct igmpv3_report *ih;
2428eb1d1641SHerbert Xu 	struct igmpv3_grec *grec;
24290436862eSNikolay Aleksandrov 	int i, len, num, type;
243054bea721SNikolay Aleksandrov 	__be32 group, *h_addr;
24310436862eSNikolay Aleksandrov 	bool changed = false;
24320436862eSNikolay Aleksandrov 	int err = 0;
2433e57f6185SNikolay Aleksandrov 	u16 nsrcs;
2434eb1d1641SHerbert Xu 
2435eb1d1641SHerbert Xu 	ih = igmpv3_report_hdr(skb);
2436eb1d1641SHerbert Xu 	num = ntohs(ih->ngrec);
2437c2d4fbd2SLinus Lüssing 	len = skb_transport_offset(skb) + sizeof(*ih);
2438eb1d1641SHerbert Xu 
2439eb1d1641SHerbert Xu 	for (i = 0; i < num; i++) {
2440eb1d1641SHerbert Xu 		len += sizeof(*grec);
2441ba5ea614SLinus Lüssing 		if (!ip_mc_may_pull(skb, len))
2442eb1d1641SHerbert Xu 			return -EINVAL;
2443eb1d1641SHerbert Xu 
2444fd218cf9SHerbert Xu 		grec = (void *)(skb->data + len - sizeof(*grec));
2445eb1d1641SHerbert Xu 		group = grec->grec_mca;
2446eb1d1641SHerbert Xu 		type = grec->grec_type;
2447e57f6185SNikolay Aleksandrov 		nsrcs = ntohs(grec->grec_nsrcs);
2448eb1d1641SHerbert Xu 
2449e57f6185SNikolay Aleksandrov 		len += nsrcs * 4;
2450ba5ea614SLinus Lüssing 		if (!ip_mc_may_pull(skb, len))
2451eb1d1641SHerbert Xu 			return -EINVAL;
2452eb1d1641SHerbert Xu 
2453eb1d1641SHerbert Xu 		switch (type) {
2454eb1d1641SHerbert Xu 		case IGMPV3_MODE_IS_INCLUDE:
2455eb1d1641SHerbert Xu 		case IGMPV3_MODE_IS_EXCLUDE:
2456eb1d1641SHerbert Xu 		case IGMPV3_CHANGE_TO_INCLUDE:
2457eb1d1641SHerbert Xu 		case IGMPV3_CHANGE_TO_EXCLUDE:
2458eb1d1641SHerbert Xu 		case IGMPV3_ALLOW_NEW_SOURCES:
2459eb1d1641SHerbert Xu 		case IGMPV3_BLOCK_OLD_SOURCES:
2460eb1d1641SHerbert Xu 			break;
2461eb1d1641SHerbert Xu 
2462eb1d1641SHerbert Xu 		default:
2463eb1d1641SHerbert Xu 			continue;
2464eb1d1641SHerbert Xu 		}
2465eb1d1641SHerbert Xu 
24666db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
24670436862eSNikolay Aleksandrov 		if (nsrcs == 0 &&
24680436862eSNikolay Aleksandrov 		    (type == IGMPV3_CHANGE_TO_INCLUDE ||
24690436862eSNikolay Aleksandrov 		     type == IGMPV3_MODE_IS_INCLUDE)) {
24700436862eSNikolay Aleksandrov 			if (!port || igmpv2) {
24716db6f0eaSFelix Fietkau 				br_ip4_multicast_leave_group(br, port, group, vid, src);
24720436862eSNikolay Aleksandrov 				continue;
24730436862eSNikolay Aleksandrov 			}
2474bc8c20acSSatish Ashok 		} else {
24756db6f0eaSFelix Fietkau 			err = br_ip4_multicast_add_group(br, port, group, vid,
24760436862eSNikolay Aleksandrov 							 src, igmpv2);
2477eb1d1641SHerbert Xu 			if (err)
2478eb1d1641SHerbert Xu 				break;
2479eb1d1641SHerbert Xu 		}
24800436862eSNikolay Aleksandrov 
24810436862eSNikolay Aleksandrov 		if (!port || igmpv2)
24820436862eSNikolay Aleksandrov 			continue;
24830436862eSNikolay Aleksandrov 
24840436862eSNikolay Aleksandrov 		spin_lock_bh(&br->multicast_lock);
24850436862eSNikolay Aleksandrov 		mdst = br_mdb_ip4_get(br, group, vid);
24860436862eSNikolay Aleksandrov 		if (!mdst)
24870436862eSNikolay Aleksandrov 			goto unlock_continue;
24880436862eSNikolay Aleksandrov 		pg = br_multicast_find_port(mdst, port, src);
24890436862eSNikolay Aleksandrov 		if (!pg || (pg->flags & MDB_PG_FLAGS_PERMANENT))
24900436862eSNikolay Aleksandrov 			goto unlock_continue;
249154bea721SNikolay Aleksandrov 		/* reload grec and host addr */
24920436862eSNikolay Aleksandrov 		grec = (void *)(skb->data + len - sizeof(*grec) - (nsrcs * 4));
249354bea721SNikolay Aleksandrov 		h_addr = &ip_hdr(skb)->saddr;
24940436862eSNikolay Aleksandrov 		switch (type) {
24950436862eSNikolay Aleksandrov 		case IGMPV3_ALLOW_NEW_SOURCES:
249654bea721SNikolay Aleksandrov 			changed = br_multicast_isinc_allow(pg, h_addr, grec->grec_src,
2497474ddb37SNikolay Aleksandrov 							   nsrcs, sizeof(__be32), type);
24980436862eSNikolay Aleksandrov 			break;
2499e6231bcaSNikolay Aleksandrov 		case IGMPV3_MODE_IS_INCLUDE:
250054bea721SNikolay Aleksandrov 			changed = br_multicast_isinc_allow(pg, h_addr, grec->grec_src,
2501474ddb37SNikolay Aleksandrov 							   nsrcs, sizeof(__be32), type);
2502e6231bcaSNikolay Aleksandrov 			break;
2503e6231bcaSNikolay Aleksandrov 		case IGMPV3_MODE_IS_EXCLUDE:
250454bea721SNikolay Aleksandrov 			changed = br_multicast_isexc(pg, h_addr, grec->grec_src,
2505ddc255d9SNikolay Aleksandrov 						     nsrcs, sizeof(__be32), type);
2506e6231bcaSNikolay Aleksandrov 			break;
25075bf1e00bSNikolay Aleksandrov 		case IGMPV3_CHANGE_TO_INCLUDE:
250854bea721SNikolay Aleksandrov 			changed = br_multicast_toin(pg, h_addr, grec->grec_src,
2509ddc255d9SNikolay Aleksandrov 						    nsrcs, sizeof(__be32), type);
25105bf1e00bSNikolay Aleksandrov 			break;
25115bf1e00bSNikolay Aleksandrov 		case IGMPV3_CHANGE_TO_EXCLUDE:
251254bea721SNikolay Aleksandrov 			changed = br_multicast_toex(pg, h_addr, grec->grec_src,
2513ddc255d9SNikolay Aleksandrov 						    nsrcs, sizeof(__be32), type);
25145bf1e00bSNikolay Aleksandrov 			break;
2515109865feSNikolay Aleksandrov 		case IGMPV3_BLOCK_OLD_SOURCES:
251654bea721SNikolay Aleksandrov 			changed = br_multicast_block(pg, h_addr, grec->grec_src,
2517474ddb37SNikolay Aleksandrov 						     nsrcs, sizeof(__be32), type);
2518109865feSNikolay Aleksandrov 			break;
25190436862eSNikolay Aleksandrov 		}
25200436862eSNikolay Aleksandrov 		if (changed)
25210436862eSNikolay Aleksandrov 			br_mdb_notify(br->dev, mdst, pg, RTM_NEWMDB);
25220436862eSNikolay Aleksandrov unlock_continue:
25230436862eSNikolay Aleksandrov 		spin_unlock_bh(&br->multicast_lock);
2524bc8c20acSSatish Ashok 	}
2525eb1d1641SHerbert Xu 
2526eb1d1641SHerbert Xu 	return err;
2527eb1d1641SHerbert Xu }
2528eb1d1641SHerbert Xu 
2529dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
253008b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_mld2_report(struct net_bridge *br,
253108b202b6SYOSHIFUJI Hideaki 					struct net_bridge_port *port,
253206499098SVlad Yasevich 					struct sk_buff *skb,
253306499098SVlad Yasevich 					u16 vid)
253408b202b6SYOSHIFUJI Hideaki {
2535*d3d065c0SNikolay Aleksandrov 	bool mldv1 = br->multicast_ctx.multicast_mld_version == 1;
25360436862eSNikolay Aleksandrov 	struct net_bridge_mdb_entry *mdst;
25370436862eSNikolay Aleksandrov 	struct net_bridge_port_group *pg;
2538ba5ea614SLinus Lüssing 	unsigned int nsrcs_offset;
25396db6f0eaSFelix Fietkau 	const unsigned char *src;
254008b202b6SYOSHIFUJI Hideaki 	struct icmp6hdr *icmp6h;
254154bea721SNikolay Aleksandrov 	struct in6_addr *h_addr;
254208b202b6SYOSHIFUJI Hideaki 	struct mld2_grec *grec;
2543ba5ea614SLinus Lüssing 	unsigned int grec_len;
25440436862eSNikolay Aleksandrov 	bool changed = false;
25450436862eSNikolay Aleksandrov 	int i, len, num;
254608b202b6SYOSHIFUJI Hideaki 	int err = 0;
254708b202b6SYOSHIFUJI Hideaki 
2548ba5ea614SLinus Lüssing 	if (!ipv6_mc_may_pull(skb, sizeof(*icmp6h)))
254908b202b6SYOSHIFUJI Hideaki 		return -EINVAL;
255008b202b6SYOSHIFUJI Hideaki 
255108b202b6SYOSHIFUJI Hideaki 	icmp6h = icmp6_hdr(skb);
255208b202b6SYOSHIFUJI Hideaki 	num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
2553c2d4fbd2SLinus Lüssing 	len = skb_transport_offset(skb) + sizeof(*icmp6h);
255408b202b6SYOSHIFUJI Hideaki 
255508b202b6SYOSHIFUJI Hideaki 	for (i = 0; i < num; i++) {
2556e57f6185SNikolay Aleksandrov 		__be16 *_nsrcs, __nsrcs;
2557e57f6185SNikolay Aleksandrov 		u16 nsrcs;
255808b202b6SYOSHIFUJI Hideaki 
2559ba5ea614SLinus Lüssing 		nsrcs_offset = len + offsetof(struct mld2_grec, grec_nsrcs);
2560ba5ea614SLinus Lüssing 
2561ba5ea614SLinus Lüssing 		if (skb_transport_offset(skb) + ipv6_transport_len(skb) <
25625fc6266aSLinus Lüssing 		    nsrcs_offset + sizeof(__nsrcs))
2563ba5ea614SLinus Lüssing 			return -EINVAL;
2564ba5ea614SLinus Lüssing 
2565e57f6185SNikolay Aleksandrov 		_nsrcs = skb_header_pointer(skb, nsrcs_offset,
2566e57f6185SNikolay Aleksandrov 					    sizeof(__nsrcs), &__nsrcs);
2567e57f6185SNikolay Aleksandrov 		if (!_nsrcs)
256808b202b6SYOSHIFUJI Hideaki 			return -EINVAL;
256908b202b6SYOSHIFUJI Hideaki 
2570e57f6185SNikolay Aleksandrov 		nsrcs = ntohs(*_nsrcs);
2571e57f6185SNikolay Aleksandrov 		grec_len = struct_size(grec, grec_src, nsrcs);
2572ba5ea614SLinus Lüssing 
2573ba5ea614SLinus Lüssing 		if (!ipv6_mc_may_pull(skb, len + grec_len))
257408b202b6SYOSHIFUJI Hideaki 			return -EINVAL;
257508b202b6SYOSHIFUJI Hideaki 
257608b202b6SYOSHIFUJI Hideaki 		grec = (struct mld2_grec *)(skb->data + len);
2577ba5ea614SLinus Lüssing 		len += grec_len;
257808b202b6SYOSHIFUJI Hideaki 
257908b202b6SYOSHIFUJI Hideaki 		switch (grec->grec_type) {
258008b202b6SYOSHIFUJI Hideaki 		case MLD2_MODE_IS_INCLUDE:
258108b202b6SYOSHIFUJI Hideaki 		case MLD2_MODE_IS_EXCLUDE:
258208b202b6SYOSHIFUJI Hideaki 		case MLD2_CHANGE_TO_INCLUDE:
258308b202b6SYOSHIFUJI Hideaki 		case MLD2_CHANGE_TO_EXCLUDE:
258408b202b6SYOSHIFUJI Hideaki 		case MLD2_ALLOW_NEW_SOURCES:
258508b202b6SYOSHIFUJI Hideaki 		case MLD2_BLOCK_OLD_SOURCES:
258608b202b6SYOSHIFUJI Hideaki 			break;
258708b202b6SYOSHIFUJI Hideaki 
258808b202b6SYOSHIFUJI Hideaki 		default:
258908b202b6SYOSHIFUJI Hideaki 			continue;
259008b202b6SYOSHIFUJI Hideaki 		}
259108b202b6SYOSHIFUJI Hideaki 
25926db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
2593bc8c20acSSatish Ashok 		if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE ||
2594bc8c20acSSatish Ashok 		     grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
2595e57f6185SNikolay Aleksandrov 		    nsrcs == 0) {
25960436862eSNikolay Aleksandrov 			if (!port || mldv1) {
25970436862eSNikolay Aleksandrov 				br_ip6_multicast_leave_group(br, port,
25980436862eSNikolay Aleksandrov 							     &grec->grec_mca,
25996db6f0eaSFelix Fietkau 							     vid, src);
26000436862eSNikolay Aleksandrov 				continue;
26010436862eSNikolay Aleksandrov 			}
2602bc8c20acSSatish Ashok 		} else {
2603bc8c20acSSatish Ashok 			err = br_ip6_multicast_add_group(br, port,
26046db6f0eaSFelix Fietkau 							 &grec->grec_mca, vid,
26050436862eSNikolay Aleksandrov 							 src, mldv1);
26069264251eSDavide Caratti 			if (err)
260708b202b6SYOSHIFUJI Hideaki 				break;
260808b202b6SYOSHIFUJI Hideaki 		}
26090436862eSNikolay Aleksandrov 
26100436862eSNikolay Aleksandrov 		if (!port || mldv1)
26110436862eSNikolay Aleksandrov 			continue;
26120436862eSNikolay Aleksandrov 
26130436862eSNikolay Aleksandrov 		spin_lock_bh(&br->multicast_lock);
26140436862eSNikolay Aleksandrov 		mdst = br_mdb_ip6_get(br, &grec->grec_mca, vid);
26150436862eSNikolay Aleksandrov 		if (!mdst)
26160436862eSNikolay Aleksandrov 			goto unlock_continue;
26170436862eSNikolay Aleksandrov 		pg = br_multicast_find_port(mdst, port, src);
26180436862eSNikolay Aleksandrov 		if (!pg || (pg->flags & MDB_PG_FLAGS_PERMANENT))
26190436862eSNikolay Aleksandrov 			goto unlock_continue;
262054bea721SNikolay Aleksandrov 		h_addr = &ipv6_hdr(skb)->saddr;
26210436862eSNikolay Aleksandrov 		switch (grec->grec_type) {
26220436862eSNikolay Aleksandrov 		case MLD2_ALLOW_NEW_SOURCES:
262354bea721SNikolay Aleksandrov 			changed = br_multicast_isinc_allow(pg, h_addr,
262454bea721SNikolay Aleksandrov 							   grec->grec_src, nsrcs,
2625474ddb37SNikolay Aleksandrov 							   sizeof(struct in6_addr),
2626474ddb37SNikolay Aleksandrov 							   grec->grec_type);
26270436862eSNikolay Aleksandrov 			break;
2628e6231bcaSNikolay Aleksandrov 		case MLD2_MODE_IS_INCLUDE:
262954bea721SNikolay Aleksandrov 			changed = br_multicast_isinc_allow(pg, h_addr,
263054bea721SNikolay Aleksandrov 							   grec->grec_src, nsrcs,
2631474ddb37SNikolay Aleksandrov 							   sizeof(struct in6_addr),
2632474ddb37SNikolay Aleksandrov 							   grec->grec_type);
2633e6231bcaSNikolay Aleksandrov 			break;
2634e6231bcaSNikolay Aleksandrov 		case MLD2_MODE_IS_EXCLUDE:
263554bea721SNikolay Aleksandrov 			changed = br_multicast_isexc(pg, h_addr,
263654bea721SNikolay Aleksandrov 						     grec->grec_src, nsrcs,
2637ddc255d9SNikolay Aleksandrov 						     sizeof(struct in6_addr),
2638ddc255d9SNikolay Aleksandrov 						     grec->grec_type);
2639e6231bcaSNikolay Aleksandrov 			break;
26405bf1e00bSNikolay Aleksandrov 		case MLD2_CHANGE_TO_INCLUDE:
264154bea721SNikolay Aleksandrov 			changed = br_multicast_toin(pg, h_addr,
264254bea721SNikolay Aleksandrov 						    grec->grec_src, nsrcs,
2643ddc255d9SNikolay Aleksandrov 						    sizeof(struct in6_addr),
2644ddc255d9SNikolay Aleksandrov 						    grec->grec_type);
26455bf1e00bSNikolay Aleksandrov 			break;
26465bf1e00bSNikolay Aleksandrov 		case MLD2_CHANGE_TO_EXCLUDE:
264754bea721SNikolay Aleksandrov 			changed = br_multicast_toex(pg, h_addr,
264854bea721SNikolay Aleksandrov 						    grec->grec_src, nsrcs,
2649ddc255d9SNikolay Aleksandrov 						    sizeof(struct in6_addr),
2650ddc255d9SNikolay Aleksandrov 						    grec->grec_type);
26515bf1e00bSNikolay Aleksandrov 			break;
2652109865feSNikolay Aleksandrov 		case MLD2_BLOCK_OLD_SOURCES:
265354bea721SNikolay Aleksandrov 			changed = br_multicast_block(pg, h_addr,
265454bea721SNikolay Aleksandrov 						     grec->grec_src, nsrcs,
2655474ddb37SNikolay Aleksandrov 						     sizeof(struct in6_addr),
2656474ddb37SNikolay Aleksandrov 						     grec->grec_type);
2657109865feSNikolay Aleksandrov 			break;
26580436862eSNikolay Aleksandrov 		}
26590436862eSNikolay Aleksandrov 		if (changed)
26600436862eSNikolay Aleksandrov 			br_mdb_notify(br->dev, mdst, pg, RTM_NEWMDB);
26610436862eSNikolay Aleksandrov unlock_continue:
26620436862eSNikolay Aleksandrov 		spin_unlock_bh(&br->multicast_lock);
2663bc8c20acSSatish Ashok 	}
266408b202b6SYOSHIFUJI Hideaki 
266508b202b6SYOSHIFUJI Hideaki 	return err;
266608b202b6SYOSHIFUJI Hideaki }
266708b202b6SYOSHIFUJI Hideaki #endif
266808b202b6SYOSHIFUJI Hideaki 
2669dc4eb53aSLinus Lüssing static bool br_ip4_multicast_select_querier(struct net_bridge *br,
26702cd41431SLinus Lüssing 					    struct net_bridge_port *port,
2671dc4eb53aSLinus Lüssing 					    __be32 saddr)
2672dc4eb53aSLinus Lüssing {
2673*d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = &br->multicast_ctx;
2674*d3d065c0SNikolay Aleksandrov 
2675*d3d065c0SNikolay Aleksandrov 	if (!timer_pending(&brmctx->ip4_own_query.timer) &&
2676*d3d065c0SNikolay Aleksandrov 	    !timer_pending(&brmctx->ip4_other_query.timer))
2677dc4eb53aSLinus Lüssing 		goto update;
2678dc4eb53aSLinus Lüssing 
2679*d3d065c0SNikolay Aleksandrov 	if (!brmctx->ip4_querier.addr.src.ip4)
2680dc4eb53aSLinus Lüssing 		goto update;
2681dc4eb53aSLinus Lüssing 
2682*d3d065c0SNikolay Aleksandrov 	if (ntohl(saddr) <= ntohl(brmctx->ip4_querier.addr.src.ip4))
2683dc4eb53aSLinus Lüssing 		goto update;
2684dc4eb53aSLinus Lüssing 
2685dc4eb53aSLinus Lüssing 	return false;
2686dc4eb53aSLinus Lüssing 
2687dc4eb53aSLinus Lüssing update:
2688*d3d065c0SNikolay Aleksandrov 	brmctx->ip4_querier.addr.src.ip4 = saddr;
2689dc4eb53aSLinus Lüssing 
26902cd41431SLinus Lüssing 	/* update protected by general multicast_lock by caller */
2691*d3d065c0SNikolay Aleksandrov 	rcu_assign_pointer(brmctx->ip4_querier.port, port);
26922cd41431SLinus Lüssing 
2693dc4eb53aSLinus Lüssing 	return true;
2694dc4eb53aSLinus Lüssing }
2695dc4eb53aSLinus Lüssing 
2696dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
2697dc4eb53aSLinus Lüssing static bool br_ip6_multicast_select_querier(struct net_bridge *br,
26982cd41431SLinus Lüssing 					    struct net_bridge_port *port,
2699dc4eb53aSLinus Lüssing 					    struct in6_addr *saddr)
2700dc4eb53aSLinus Lüssing {
2701*d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = &br->multicast_ctx;
2702*d3d065c0SNikolay Aleksandrov 
2703*d3d065c0SNikolay Aleksandrov 	if (!timer_pending(&brmctx->ip6_own_query.timer) &&
2704*d3d065c0SNikolay Aleksandrov 	    !timer_pending(&brmctx->ip6_other_query.timer))
2705dc4eb53aSLinus Lüssing 		goto update;
2706dc4eb53aSLinus Lüssing 
2707*d3d065c0SNikolay Aleksandrov 	if (ipv6_addr_cmp(saddr, &brmctx->ip6_querier.addr.src.ip6) <= 0)
2708dc4eb53aSLinus Lüssing 		goto update;
2709dc4eb53aSLinus Lüssing 
2710dc4eb53aSLinus Lüssing 	return false;
2711dc4eb53aSLinus Lüssing 
2712dc4eb53aSLinus Lüssing update:
2713*d3d065c0SNikolay Aleksandrov 	brmctx->ip6_querier.addr.src.ip6 = *saddr;
2714dc4eb53aSLinus Lüssing 
27152cd41431SLinus Lüssing 	/* update protected by general multicast_lock by caller */
2716*d3d065c0SNikolay Aleksandrov 	rcu_assign_pointer(brmctx->ip6_querier.port, port);
27172cd41431SLinus Lüssing 
2718dc4eb53aSLinus Lüssing 	return true;
2719dc4eb53aSLinus Lüssing }
2720dc4eb53aSLinus Lüssing #endif
2721dc4eb53aSLinus Lüssing 
2722cc0fdd80SLinus Lüssing static void
272390010b36SLinus Lüssing br_multicast_update_query_timer(struct net_bridge *br,
272490010b36SLinus Lüssing 				struct bridge_mcast_other_query *query,
2725b00589afSLinus Lüssing 				unsigned long max_delay)
2726b00589afSLinus Lüssing {
272790010b36SLinus Lüssing 	if (!timer_pending(&query->timer))
272890010b36SLinus Lüssing 		query->delay_time = jiffies + max_delay;
2729b00589afSLinus Lüssing 
2730*d3d065c0SNikolay Aleksandrov 	mod_timer(&query->timer, jiffies +
2731*d3d065c0SNikolay Aleksandrov 				 br->multicast_ctx.multicast_querier_interval);
2732b00589afSLinus Lüssing }
2733b00589afSLinus Lüssing 
27346d549648SNogah Frankel static void br_port_mc_router_state_change(struct net_bridge_port *p,
27356d549648SNogah Frankel 					   bool is_mc_router)
27366d549648SNogah Frankel {
27376d549648SNogah Frankel 	struct switchdev_attr attr = {
27386d549648SNogah Frankel 		.orig_dev = p->dev,
27396d549648SNogah Frankel 		.id = SWITCHDEV_ATTR_ID_PORT_MROUTER,
27406d549648SNogah Frankel 		.flags = SWITCHDEV_F_DEFER,
27416d549648SNogah Frankel 		.u.mrouter = is_mc_router,
27426d549648SNogah Frankel 	};
27436d549648SNogah Frankel 
2744dcbdf135SVladimir Oltean 	switchdev_port_attr_set(p->dev, &attr, NULL);
27456d549648SNogah Frankel }
27466d549648SNogah Frankel 
2747d9b8c4d8SLinus Lüssing static struct net_bridge_port *
2748*d3d065c0SNikolay Aleksandrov br_multicast_rport_from_node(struct net_bridge_mcast *brmctx,
2749d9b8c4d8SLinus Lüssing 			     struct hlist_head *mc_router_list,
2750d9b8c4d8SLinus Lüssing 			     struct hlist_node *rlist)
2751d9b8c4d8SLinus Lüssing {
27529632233eSNikolay Aleksandrov 	struct net_bridge_mcast_port *pmctx;
27539632233eSNikolay Aleksandrov 
2754a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
2755*d3d065c0SNikolay Aleksandrov 	if (mc_router_list == &brmctx->ip6_mc_router_list)
27569632233eSNikolay Aleksandrov 		pmctx = hlist_entry(rlist, struct net_bridge_mcast_port,
27579632233eSNikolay Aleksandrov 				    ip6_rlist);
27589632233eSNikolay Aleksandrov 	else
2759a3c02e76SLinus Lüssing #endif
27609632233eSNikolay Aleksandrov 		pmctx = hlist_entry(rlist, struct net_bridge_mcast_port,
27619632233eSNikolay Aleksandrov 				    ip4_rlist);
27629632233eSNikolay Aleksandrov 
27639632233eSNikolay Aleksandrov 	return pmctx->port;
2764d9b8c4d8SLinus Lüssing }
2765d9b8c4d8SLinus Lüssing 
2766d9b8c4d8SLinus Lüssing static struct hlist_node *
2767*d3d065c0SNikolay Aleksandrov br_multicast_get_rport_slot(struct net_bridge_mcast *brmctx,
2768d9b8c4d8SLinus Lüssing 			    struct net_bridge_port *port,
2769d9b8c4d8SLinus Lüssing 			    struct hlist_head *mc_router_list)
2770d9b8c4d8SLinus Lüssing 
2771d9b8c4d8SLinus Lüssing {
2772d9b8c4d8SLinus Lüssing 	struct hlist_node *slot = NULL;
2773d9b8c4d8SLinus Lüssing 	struct net_bridge_port *p;
2774d9b8c4d8SLinus Lüssing 	struct hlist_node *rlist;
2775d9b8c4d8SLinus Lüssing 
2776d9b8c4d8SLinus Lüssing 	hlist_for_each(rlist, mc_router_list) {
2777*d3d065c0SNikolay Aleksandrov 		p = br_multicast_rport_from_node(brmctx, mc_router_list, rlist);
2778d9b8c4d8SLinus Lüssing 
2779d9b8c4d8SLinus Lüssing 		if ((unsigned long)port >= (unsigned long)p)
2780d9b8c4d8SLinus Lüssing 			break;
2781d9b8c4d8SLinus Lüssing 
2782d9b8c4d8SLinus Lüssing 		slot = rlist;
2783d9b8c4d8SLinus Lüssing 	}
2784d9b8c4d8SLinus Lüssing 
2785d9b8c4d8SLinus Lüssing 	return slot;
2786d9b8c4d8SLinus Lüssing }
2787d9b8c4d8SLinus Lüssing 
2788a3c02e76SLinus Lüssing static bool br_multicast_no_router_otherpf(struct net_bridge_port *port,
2789a3c02e76SLinus Lüssing 					   struct hlist_node *rnode)
2790a3c02e76SLinus Lüssing {
2791a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
27929632233eSNikolay Aleksandrov 	if (rnode != &port->multicast_ctx.ip6_rlist)
27939632233eSNikolay Aleksandrov 		return hlist_unhashed(&port->multicast_ctx.ip6_rlist);
2794a3c02e76SLinus Lüssing 	else
27959632233eSNikolay Aleksandrov 		return hlist_unhashed(&port->multicast_ctx.ip4_rlist);
2796a3c02e76SLinus Lüssing #else
2797a3c02e76SLinus Lüssing 	return true;
2798a3c02e76SLinus Lüssing #endif
2799a3c02e76SLinus Lüssing }
2800a3c02e76SLinus Lüssing 
2801d9b8c4d8SLinus Lüssing /* Add port to router_list
28027e80c124Sstephen hemminger  *  list is maintained ordered by pointer value
28037e80c124Sstephen hemminger  *  and locked by br->multicast_lock and RCU
28047e80c124Sstephen hemminger  */
2805*d3d065c0SNikolay Aleksandrov static void br_multicast_add_router(struct net_bridge_mcast *brmctx,
2806d9b8c4d8SLinus Lüssing 				    struct net_bridge_port *port,
2807d9b8c4d8SLinus Lüssing 				    struct hlist_node *rlist,
2808d9b8c4d8SLinus Lüssing 				    struct hlist_head *mc_router_list)
28090909e117SHerbert Xu {
2810d9b8c4d8SLinus Lüssing 	struct hlist_node *slot;
28110909e117SHerbert Xu 
2812d9b8c4d8SLinus Lüssing 	if (!hlist_unhashed(rlist))
28131a040eacSNikolay Aleksandrov 		return;
28141a040eacSNikolay Aleksandrov 
2815*d3d065c0SNikolay Aleksandrov 	slot = br_multicast_get_rport_slot(brmctx, port, mc_router_list);
28160909e117SHerbert Xu 
28177e80c124Sstephen hemminger 	if (slot)
2818d9b8c4d8SLinus Lüssing 		hlist_add_behind_rcu(rlist, slot);
2819dcdca2c4Sstephen hemminger 	else
2820d9b8c4d8SLinus Lüssing 		hlist_add_head_rcu(rlist, mc_router_list);
2821d9b8c4d8SLinus Lüssing 
2822a3c02e76SLinus Lüssing 	/* For backwards compatibility for now, only notify if we
2823a3c02e76SLinus Lüssing 	 * switched from no IPv4/IPv6 multicast router to a new
2824a3c02e76SLinus Lüssing 	 * IPv4 or IPv6 multicast router.
2825a3c02e76SLinus Lüssing 	 */
2826a3c02e76SLinus Lüssing 	if (br_multicast_no_router_otherpf(port, rlist)) {
2827*d3d065c0SNikolay Aleksandrov 		br_rtr_notify(port->br->dev, port, RTM_NEWMDB);
28286d549648SNogah Frankel 		br_port_mc_router_state_change(port, true);
28290909e117SHerbert Xu 	}
2830a3c02e76SLinus Lüssing }
28310909e117SHerbert Xu 
2832d9b8c4d8SLinus Lüssing /* Add port to router_list
2833d9b8c4d8SLinus Lüssing  *  list is maintained ordered by pointer value
2834d9b8c4d8SLinus Lüssing  *  and locked by br->multicast_lock and RCU
2835d9b8c4d8SLinus Lüssing  */
2836d9b8c4d8SLinus Lüssing static void br_ip4_multicast_add_router(struct net_bridge *br,
2837eb1d1641SHerbert Xu 					struct net_bridge_port *port)
2838eb1d1641SHerbert Xu {
2839*d3d065c0SNikolay Aleksandrov 	br_multicast_add_router(&br->multicast_ctx, port,
2840*d3d065c0SNikolay Aleksandrov 				&port->multicast_ctx.ip4_rlist,
2841*d3d065c0SNikolay Aleksandrov 				&br->multicast_ctx.ip4_mc_router_list);
2842d9b8c4d8SLinus Lüssing }
2843d9b8c4d8SLinus Lüssing 
2844a3c02e76SLinus Lüssing /* Add port to router_list
2845a3c02e76SLinus Lüssing  *  list is maintained ordered by pointer value
2846a3c02e76SLinus Lüssing  *  and locked by br->multicast_lock and RCU
2847a3c02e76SLinus Lüssing  */
2848a3c02e76SLinus Lüssing static void br_ip6_multicast_add_router(struct net_bridge *br,
2849a3c02e76SLinus Lüssing 					struct net_bridge_port *port)
2850a3c02e76SLinus Lüssing {
2851a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
2852*d3d065c0SNikolay Aleksandrov 	br_multicast_add_router(&br->multicast_ctx, port,
2853*d3d065c0SNikolay Aleksandrov 				&port->multicast_ctx.ip6_rlist,
2854*d3d065c0SNikolay Aleksandrov 				&br->multicast_ctx.ip6_mc_router_list);
2855a3c02e76SLinus Lüssing #endif
2856a3c02e76SLinus Lüssing }
2857a3c02e76SLinus Lüssing 
2858d9b8c4d8SLinus Lüssing static void br_multicast_mark_router(struct net_bridge *br,
2859d9b8c4d8SLinus Lüssing 				     struct net_bridge_port *port,
2860d9b8c4d8SLinus Lüssing 				     struct timer_list *timer,
2861d9b8c4d8SLinus Lüssing 				     struct hlist_node *rlist,
2862d9b8c4d8SLinus Lüssing 				     struct hlist_head *mc_router_list)
2863d9b8c4d8SLinus Lüssing {
2864*d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = &br->multicast_ctx;
2865eb1d1641SHerbert Xu 	unsigned long now = jiffies;
2866eb1d1641SHerbert Xu 
2867eb1d1641SHerbert Xu 	if (!port) {
2868*d3d065c0SNikolay Aleksandrov 		if (brmctx->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) {
2869*d3d065c0SNikolay Aleksandrov 			if (!br_ip4_multicast_is_router(brmctx) &&
2870*d3d065c0SNikolay Aleksandrov 			    !br_ip6_multicast_is_router(brmctx))
287177041420SYotam Gigi 				br_mc_router_state_change(br, true);
2872*d3d065c0SNikolay Aleksandrov 			mod_timer(timer, now + brmctx->multicast_querier_interval);
287377041420SYotam Gigi 		}
2874eb1d1641SHerbert Xu 		return;
2875eb1d1641SHerbert Xu 	}
2876eb1d1641SHerbert Xu 
28779632233eSNikolay Aleksandrov 	if (port->multicast_ctx.multicast_router == MDB_RTR_TYPE_DISABLED ||
28789632233eSNikolay Aleksandrov 	    port->multicast_ctx.multicast_router == MDB_RTR_TYPE_PERM)
2879eb1d1641SHerbert Xu 		return;
2880eb1d1641SHerbert Xu 
2881*d3d065c0SNikolay Aleksandrov 	br_multicast_add_router(brmctx, port, rlist, mc_router_list);
2882*d3d065c0SNikolay Aleksandrov 	mod_timer(timer, now + brmctx->multicast_querier_interval);
2883d9b8c4d8SLinus Lüssing }
2884eb1d1641SHerbert Xu 
2885d9b8c4d8SLinus Lüssing static void br_ip4_multicast_mark_router(struct net_bridge *br,
2886d9b8c4d8SLinus Lüssing 					 struct net_bridge_port *port)
2887d9b8c4d8SLinus Lüssing {
2888*d3d065c0SNikolay Aleksandrov 	struct timer_list *timer = &br->multicast_ctx.ip4_mc_router_timer;
2889d9b8c4d8SLinus Lüssing 	struct hlist_node *rlist = NULL;
2890d9b8c4d8SLinus Lüssing 
2891d9b8c4d8SLinus Lüssing 	if (port) {
28929632233eSNikolay Aleksandrov 		timer = &port->multicast_ctx.ip4_mc_router_timer;
28939632233eSNikolay Aleksandrov 		rlist = &port->multicast_ctx.ip4_rlist;
2894d9b8c4d8SLinus Lüssing 	}
2895d9b8c4d8SLinus Lüssing 
2896d9b8c4d8SLinus Lüssing 	br_multicast_mark_router(br, port, timer, rlist,
2897*d3d065c0SNikolay Aleksandrov 				 &br->multicast_ctx.ip4_mc_router_list);
2898eb1d1641SHerbert Xu }
2899eb1d1641SHerbert Xu 
2900a3c02e76SLinus Lüssing static void br_ip6_multicast_mark_router(struct net_bridge *br,
2901a3c02e76SLinus Lüssing 					 struct net_bridge_port *port)
2902a3c02e76SLinus Lüssing {
2903a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
2904*d3d065c0SNikolay Aleksandrov 	struct timer_list *timer = &br->multicast_ctx.ip6_mc_router_timer;
2905a3c02e76SLinus Lüssing 	struct hlist_node *rlist = NULL;
2906a3c02e76SLinus Lüssing 
2907a3c02e76SLinus Lüssing 	if (port) {
29089632233eSNikolay Aleksandrov 		timer = &port->multicast_ctx.ip6_mc_router_timer;
29099632233eSNikolay Aleksandrov 		rlist = &port->multicast_ctx.ip6_rlist;
2910a3c02e76SLinus Lüssing 	}
2911a3c02e76SLinus Lüssing 
2912a3c02e76SLinus Lüssing 	br_multicast_mark_router(br, port, timer, rlist,
2913*d3d065c0SNikolay Aleksandrov 				 &br->multicast_ctx.ip6_mc_router_list);
2914a3c02e76SLinus Lüssing #endif
2915a3c02e76SLinus Lüssing }
2916a3c02e76SLinus Lüssing 
2917b19232efSLinus Lüssing static void
2918b19232efSLinus Lüssing br_ip4_multicast_query_received(struct net_bridge *br,
2919eb1d1641SHerbert Xu 				struct net_bridge_port *port,
292090010b36SLinus Lüssing 				struct bridge_mcast_other_query *query,
2921dc4eb53aSLinus Lüssing 				struct br_ip *saddr,
2922b00589afSLinus Lüssing 				unsigned long max_delay)
2923eb1d1641SHerbert Xu {
2924b19232efSLinus Lüssing 	if (!br_ip4_multicast_select_querier(br, port, saddr->src.ip4))
2925eb1d1641SHerbert Xu 		return;
2926eb1d1641SHerbert Xu 
2927dc4eb53aSLinus Lüssing 	br_multicast_update_query_timer(br, query, max_delay);
2928d9b8c4d8SLinus Lüssing 	br_ip4_multicast_mark_router(br, port);
2929eb1d1641SHerbert Xu }
2930eb1d1641SHerbert Xu 
2931b19232efSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
2932b19232efSLinus Lüssing static void
2933b19232efSLinus Lüssing br_ip6_multicast_query_received(struct net_bridge *br,
2934b19232efSLinus Lüssing 				struct net_bridge_port *port,
2935b19232efSLinus Lüssing 				struct bridge_mcast_other_query *query,
2936b19232efSLinus Lüssing 				struct br_ip *saddr,
2937b19232efSLinus Lüssing 				unsigned long max_delay)
2938b19232efSLinus Lüssing {
2939b19232efSLinus Lüssing 	if (!br_ip6_multicast_select_querier(br, port, &saddr->src.ip6))
2940b19232efSLinus Lüssing 		return;
2941b19232efSLinus Lüssing 
2942b19232efSLinus Lüssing 	br_multicast_update_query_timer(br, query, max_delay);
2943a3c02e76SLinus Lüssing 	br_ip6_multicast_mark_router(br, port);
2944b19232efSLinus Lüssing }
2945b19232efSLinus Lüssing #endif
2946b19232efSLinus Lüssing 
29479c2e955cSzhong jiang static void br_ip4_multicast_query(struct net_bridge *br,
2948eb1d1641SHerbert Xu 				   struct net_bridge_port *port,
294906499098SVlad Yasevich 				   struct sk_buff *skb,
295006499098SVlad Yasevich 				   u16 vid)
2951eb1d1641SHerbert Xu {
2952*d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = &br->multicast_ctx;
2953ba5ea614SLinus Lüssing 	unsigned int transport_len = ip_transport_len(skb);
2954b71d1d42SEric Dumazet 	const struct iphdr *iph = ip_hdr(skb);
2955eb1d1641SHerbert Xu 	struct igmphdr *ih = igmp_hdr(skb);
2956eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
2957eb1d1641SHerbert Xu 	struct igmpv3_query *ih3;
2958eb1d1641SHerbert Xu 	struct net_bridge_port_group *p;
2959e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
2960dc4eb53aSLinus Lüssing 	struct br_ip saddr;
2961eb1d1641SHerbert Xu 	unsigned long max_delay;
2962eb1d1641SHerbert Xu 	unsigned long now = jiffies;
2963eb1d1641SHerbert Xu 	__be32 group;
2964eb1d1641SHerbert Xu 
2965eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
2966eb1d1641SHerbert Xu 	if (!netif_running(br->dev) ||
2967eb1d1641SHerbert Xu 	    (port && port->state == BR_STATE_DISABLED))
2968eb1d1641SHerbert Xu 		goto out;
2969eb1d1641SHerbert Xu 
2970eb1d1641SHerbert Xu 	group = ih->group;
2971eb1d1641SHerbert Xu 
2972ba5ea614SLinus Lüssing 	if (transport_len == sizeof(*ih)) {
2973eb1d1641SHerbert Xu 		max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
2974eb1d1641SHerbert Xu 
2975eb1d1641SHerbert Xu 		if (!max_delay) {
2976eb1d1641SHerbert Xu 			max_delay = 10 * HZ;
2977eb1d1641SHerbert Xu 			group = 0;
2978eb1d1641SHerbert Xu 		}
2979ba5ea614SLinus Lüssing 	} else if (transport_len >= sizeof(*ih3)) {
2980eb1d1641SHerbert Xu 		ih3 = igmpv3_query_hdr(skb);
298123550b83SNikolay Aleksandrov 		if (ih3->nsrcs ||
2982*d3d065c0SNikolay Aleksandrov 		    (brmctx->multicast_igmp_version == 3 && group &&
2983*d3d065c0SNikolay Aleksandrov 		     ih3->suppress))
2984bec68ff1SYOSHIFUJI Hideaki 			goto out;
2985eb1d1641SHerbert Xu 
29860ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 		max_delay = ih3->code ?
29870ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 			    IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
29889afd85c9SLinus Lüssing 	} else {
29899ed973ccSLinus Lüssing 		goto out;
29909ed973ccSLinus Lüssing 	}
29919ed973ccSLinus Lüssing 
2992dc4eb53aSLinus Lüssing 	if (!group) {
2993dc4eb53aSLinus Lüssing 		saddr.proto = htons(ETH_P_IP);
2994deb96566SNikolay Aleksandrov 		saddr.src.ip4 = iph->saddr;
2995b00589afSLinus Lüssing 
2996*d3d065c0SNikolay Aleksandrov 		br_ip4_multicast_query_received(br, port,
2997*d3d065c0SNikolay Aleksandrov 						&brmctx->ip4_other_query,
2998dc4eb53aSLinus Lüssing 						&saddr, max_delay);
2999eb1d1641SHerbert Xu 		goto out;
3000dc4eb53aSLinus Lüssing 	}
3001eb1d1641SHerbert Xu 
300219e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip4_get(br, group, vid);
3003eb1d1641SHerbert Xu 	if (!mp)
3004eb1d1641SHerbert Xu 		goto out;
3005eb1d1641SHerbert Xu 
3006*d3d065c0SNikolay Aleksandrov 	max_delay *= brmctx->multicast_last_member_count;
3007eb1d1641SHerbert Xu 
3008ff0fd34eSAndrew Lunn 	if (mp->host_joined &&
3009eb1d1641SHerbert Xu 	    (timer_pending(&mp->timer) ?
3010eb1d1641SHerbert Xu 	     time_after(mp->timer.expires, now + max_delay) :
3011eb1d1641SHerbert Xu 	     try_to_del_timer_sync(&mp->timer) >= 0))
3012eb1d1641SHerbert Xu 		mod_timer(&mp->timer, now + max_delay);
3013eb1d1641SHerbert Xu 
3014e8051688SEric Dumazet 	for (pp = &mp->ports;
3015e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
3016e8051688SEric Dumazet 	     pp = &p->next) {
3017eb1d1641SHerbert Xu 		if (timer_pending(&p->timer) ?
3018eb1d1641SHerbert Xu 		    time_after(p->timer.expires, now + max_delay) :
301923550b83SNikolay Aleksandrov 		    try_to_del_timer_sync(&p->timer) >= 0 &&
3020*d3d065c0SNikolay Aleksandrov 		    (brmctx->multicast_igmp_version == 2 ||
302123550b83SNikolay Aleksandrov 		     p->filter_mode == MCAST_EXCLUDE))
302224f9cdcbSHerbert Xu 			mod_timer(&p->timer, now + max_delay);
3023eb1d1641SHerbert Xu 	}
3024eb1d1641SHerbert Xu 
3025eb1d1641SHerbert Xu out:
3026eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
3027eb1d1641SHerbert Xu }
3028eb1d1641SHerbert Xu 
3029dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
303008b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_query(struct net_bridge *br,
303108b202b6SYOSHIFUJI Hideaki 				  struct net_bridge_port *port,
303206499098SVlad Yasevich 				  struct sk_buff *skb,
303306499098SVlad Yasevich 				  u16 vid)
303408b202b6SYOSHIFUJI Hideaki {
3035*d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = &br->multicast_ctx;
3036ba5ea614SLinus Lüssing 	unsigned int transport_len = ipv6_transport_len(skb);
3037eca2a43bSstephen hemminger 	struct mld_msg *mld;
303808b202b6SYOSHIFUJI Hideaki 	struct net_bridge_mdb_entry *mp;
303908b202b6SYOSHIFUJI Hideaki 	struct mld2_query *mld2q;
3040e8051688SEric Dumazet 	struct net_bridge_port_group *p;
3041e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
3042dc4eb53aSLinus Lüssing 	struct br_ip saddr;
304308b202b6SYOSHIFUJI Hideaki 	unsigned long max_delay;
304408b202b6SYOSHIFUJI Hideaki 	unsigned long now = jiffies;
3045856ce5d0SLinus Lüssing 	unsigned int offset = skb_transport_offset(skb);
3046b71d1d42SEric Dumazet 	const struct in6_addr *group = NULL;
30479ed973ccSLinus Lüssing 	bool is_general_query;
304808b202b6SYOSHIFUJI Hideaki 	int err = 0;
304908b202b6SYOSHIFUJI Hideaki 
305008b202b6SYOSHIFUJI Hideaki 	spin_lock(&br->multicast_lock);
305108b202b6SYOSHIFUJI Hideaki 	if (!netif_running(br->dev) ||
305208b202b6SYOSHIFUJI Hideaki 	    (port && port->state == BR_STATE_DISABLED))
305308b202b6SYOSHIFUJI Hideaki 		goto out;
305408b202b6SYOSHIFUJI Hideaki 
3055ba5ea614SLinus Lüssing 	if (transport_len == sizeof(*mld)) {
3056856ce5d0SLinus Lüssing 		if (!pskb_may_pull(skb, offset + sizeof(*mld))) {
305708b202b6SYOSHIFUJI Hideaki 			err = -EINVAL;
305808b202b6SYOSHIFUJI Hideaki 			goto out;
305908b202b6SYOSHIFUJI Hideaki 		}
306008b202b6SYOSHIFUJI Hideaki 		mld = (struct mld_msg *) icmp6_hdr(skb);
30614715213dSLi RongQing 		max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
306208b202b6SYOSHIFUJI Hideaki 		if (max_delay)
306308b202b6SYOSHIFUJI Hideaki 			group = &mld->mld_mca;
3064248ba8ecSLinus Lüssing 	} else {
3065856ce5d0SLinus Lüssing 		if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) {
306608b202b6SYOSHIFUJI Hideaki 			err = -EINVAL;
306708b202b6SYOSHIFUJI Hideaki 			goto out;
306808b202b6SYOSHIFUJI Hideaki 		}
306908b202b6SYOSHIFUJI Hideaki 		mld2q = (struct mld2_query *)icmp6_hdr(skb);
307008b202b6SYOSHIFUJI Hideaki 		if (!mld2q->mld2q_nsrcs)
307108b202b6SYOSHIFUJI Hideaki 			group = &mld2q->mld2q_mca;
3072*d3d065c0SNikolay Aleksandrov 		if (brmctx->multicast_mld_version == 2 &&
307323550b83SNikolay Aleksandrov 		    !ipv6_addr_any(&mld2q->mld2q_mca) &&
307423550b83SNikolay Aleksandrov 		    mld2q->mld2q_suppress)
307523550b83SNikolay Aleksandrov 			goto out;
3076e3f5b170SDaniel Borkmann 
3077e3f5b170SDaniel Borkmann 		max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
307808b202b6SYOSHIFUJI Hideaki 	}
307908b202b6SYOSHIFUJI Hideaki 
30809ed973ccSLinus Lüssing 	is_general_query = group && ipv6_addr_any(group);
30819ed973ccSLinus Lüssing 
3082dc4eb53aSLinus Lüssing 	if (is_general_query) {
3083dc4eb53aSLinus Lüssing 		saddr.proto = htons(ETH_P_IPV6);
3084deb96566SNikolay Aleksandrov 		saddr.src.ip6 = ipv6_hdr(skb)->saddr;
3085b00589afSLinus Lüssing 
3086*d3d065c0SNikolay Aleksandrov 		br_ip6_multicast_query_received(br, port,
3087*d3d065c0SNikolay Aleksandrov 						&brmctx->ip6_other_query,
3088dc4eb53aSLinus Lüssing 						&saddr, max_delay);
308908b202b6SYOSHIFUJI Hideaki 		goto out;
30906c03ee8bSLinus Lüssing 	} else if (!group) {
30916c03ee8bSLinus Lüssing 		goto out;
3092dc4eb53aSLinus Lüssing 	}
309308b202b6SYOSHIFUJI Hideaki 
309419e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip6_get(br, group, vid);
309508b202b6SYOSHIFUJI Hideaki 	if (!mp)
309608b202b6SYOSHIFUJI Hideaki 		goto out;
309708b202b6SYOSHIFUJI Hideaki 
3098*d3d065c0SNikolay Aleksandrov 	max_delay *= brmctx->multicast_last_member_count;
3099ff0fd34eSAndrew Lunn 	if (mp->host_joined &&
310008b202b6SYOSHIFUJI Hideaki 	    (timer_pending(&mp->timer) ?
310108b202b6SYOSHIFUJI Hideaki 	     time_after(mp->timer.expires, now + max_delay) :
310208b202b6SYOSHIFUJI Hideaki 	     try_to_del_timer_sync(&mp->timer) >= 0))
310308b202b6SYOSHIFUJI Hideaki 		mod_timer(&mp->timer, now + max_delay);
310408b202b6SYOSHIFUJI Hideaki 
3105e8051688SEric Dumazet 	for (pp = &mp->ports;
3106e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
3107e8051688SEric Dumazet 	     pp = &p->next) {
310808b202b6SYOSHIFUJI Hideaki 		if (timer_pending(&p->timer) ?
310908b202b6SYOSHIFUJI Hideaki 		    time_after(p->timer.expires, now + max_delay) :
311023550b83SNikolay Aleksandrov 		    try_to_del_timer_sync(&p->timer) >= 0 &&
3111*d3d065c0SNikolay Aleksandrov 		    (brmctx->multicast_mld_version == 1 ||
311223550b83SNikolay Aleksandrov 		     p->filter_mode == MCAST_EXCLUDE))
311324f9cdcbSHerbert Xu 			mod_timer(&p->timer, now + max_delay);
311408b202b6SYOSHIFUJI Hideaki 	}
311508b202b6SYOSHIFUJI Hideaki 
311608b202b6SYOSHIFUJI Hideaki out:
311708b202b6SYOSHIFUJI Hideaki 	spin_unlock(&br->multicast_lock);
311808b202b6SYOSHIFUJI Hideaki 	return err;
311908b202b6SYOSHIFUJI Hideaki }
312008b202b6SYOSHIFUJI Hideaki #endif
312108b202b6SYOSHIFUJI Hideaki 
312290010b36SLinus Lüssing static void
312390010b36SLinus Lüssing br_multicast_leave_group(struct net_bridge *br,
3124eb1d1641SHerbert Xu 			 struct net_bridge_port *port,
3125cc0fdd80SLinus Lüssing 			 struct br_ip *group,
312690010b36SLinus Lüssing 			 struct bridge_mcast_other_query *other_query,
31276db6f0eaSFelix Fietkau 			 struct bridge_mcast_own_query *own_query,
31286db6f0eaSFelix Fietkau 			 const unsigned char *src)
3129eb1d1641SHerbert Xu {
3130*d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = &br->multicast_ctx;
3131eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
3132eb1d1641SHerbert Xu 	struct net_bridge_port_group *p;
3133eb1d1641SHerbert Xu 	unsigned long now;
3134eb1d1641SHerbert Xu 	unsigned long time;
3135eb1d1641SHerbert Xu 
3136eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
3137eb1d1641SHerbert Xu 	if (!netif_running(br->dev) ||
3138544586f7SSatish Ashok 	    (port && port->state == BR_STATE_DISABLED))
3139eb1d1641SHerbert Xu 		goto out;
3140eb1d1641SHerbert Xu 
314119e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip_get(br, group);
3142eb1d1641SHerbert Xu 	if (!mp)
3143eb1d1641SHerbert Xu 		goto out;
3144eb1d1641SHerbert Xu 
3145544586f7SSatish Ashok 	if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
3146544586f7SSatish Ashok 		struct net_bridge_port_group __rcu **pp;
3147544586f7SSatish Ashok 
3148544586f7SSatish Ashok 		for (pp = &mp->ports;
3149544586f7SSatish Ashok 		     (p = mlock_dereference(*pp, br)) != NULL;
3150544586f7SSatish Ashok 		     pp = &p->next) {
31516db6f0eaSFelix Fietkau 			if (!br_port_group_equal(p, port, src))
3152544586f7SSatish Ashok 				continue;
3153544586f7SSatish Ashok 
31545c725b6bSNikolay Aleksandrov 			if (p->flags & MDB_PG_FLAGS_PERMANENT)
31555c725b6bSNikolay Aleksandrov 				break;
31565c725b6bSNikolay Aleksandrov 
3157681590bdSNikolay Aleksandrov 			p->flags |= MDB_PG_FLAGS_FAST_LEAVE;
3158681590bdSNikolay Aleksandrov 			br_multicast_del_pg(mp, p, pp);
3159544586f7SSatish Ashok 		}
3160544586f7SSatish Ashok 		goto out;
3161544586f7SSatish Ashok 	}
3162544586f7SSatish Ashok 
3163544586f7SSatish Ashok 	if (timer_pending(&other_query->timer))
3164544586f7SSatish Ashok 		goto out;
3165544586f7SSatish Ashok 
3166675779adSNikolay Aleksandrov 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER)) {
3167438ef2d0SNikolay Aleksandrov 		__br_multicast_send_query(br, port, NULL, NULL, &mp->addr,
316842c11ccfSNikolay Aleksandrov 					  false, 0, NULL);
31696b7df111SCong Wang 
3170*d3d065c0SNikolay Aleksandrov 		time = jiffies + brmctx->multicast_last_member_count *
3171*d3d065c0SNikolay Aleksandrov 				 brmctx->multicast_last_member_interval;
3172cc0fdd80SLinus Lüssing 
317390010b36SLinus Lüssing 		mod_timer(&own_query->timer, time);
31746b7df111SCong Wang 
31756b7df111SCong Wang 		for (p = mlock_dereference(mp->ports, br);
31766b7df111SCong Wang 		     p != NULL;
31776b7df111SCong Wang 		     p = mlock_dereference(p->next, br)) {
31786db6f0eaSFelix Fietkau 			if (!br_port_group_equal(p, port, src))
31796b7df111SCong Wang 				continue;
31806b7df111SCong Wang 
31816b7df111SCong Wang 			if (!hlist_unhashed(&p->mglist) &&
31826b7df111SCong Wang 			    (timer_pending(&p->timer) ?
31836b7df111SCong Wang 			     time_after(p->timer.expires, time) :
31846b7df111SCong Wang 			     try_to_del_timer_sync(&p->timer) >= 0)) {
31856b7df111SCong Wang 				mod_timer(&p->timer, time);
31866b7df111SCong Wang 			}
31876b7df111SCong Wang 
31886b7df111SCong Wang 			break;
31896b7df111SCong Wang 		}
31906b7df111SCong Wang 	}
31916b7df111SCong Wang 
3192eb1d1641SHerbert Xu 	now = jiffies;
3193*d3d065c0SNikolay Aleksandrov 	time = now + brmctx->multicast_last_member_count *
3194*d3d065c0SNikolay Aleksandrov 		     brmctx->multicast_last_member_interval;
3195eb1d1641SHerbert Xu 
3196eb1d1641SHerbert Xu 	if (!port) {
3197ff0fd34eSAndrew Lunn 		if (mp->host_joined &&
3198eb1d1641SHerbert Xu 		    (timer_pending(&mp->timer) ?
3199eb1d1641SHerbert Xu 		     time_after(mp->timer.expires, time) :
3200eb1d1641SHerbert Xu 		     try_to_del_timer_sync(&mp->timer) >= 0)) {
3201eb1d1641SHerbert Xu 			mod_timer(&mp->timer, time);
3202eb1d1641SHerbert Xu 		}
3203454594f3SLinus Lüssing 
3204454594f3SLinus Lüssing 		goto out;
3205454594f3SLinus Lüssing 	}
3206454594f3SLinus Lüssing 
3207454594f3SLinus Lüssing 	for (p = mlock_dereference(mp->ports, br);
3208454594f3SLinus Lüssing 	     p != NULL;
3209454594f3SLinus Lüssing 	     p = mlock_dereference(p->next, br)) {
3210085b53c8SNikolay Aleksandrov 		if (p->key.port != port)
3211454594f3SLinus Lüssing 			continue;
3212454594f3SLinus Lüssing 
3213454594f3SLinus Lüssing 		if (!hlist_unhashed(&p->mglist) &&
3214454594f3SLinus Lüssing 		    (timer_pending(&p->timer) ?
3215454594f3SLinus Lüssing 		     time_after(p->timer.expires, time) :
3216454594f3SLinus Lüssing 		     try_to_del_timer_sync(&p->timer) >= 0)) {
3217454594f3SLinus Lüssing 			mod_timer(&p->timer, time);
3218454594f3SLinus Lüssing 		}
3219454594f3SLinus Lüssing 
3220454594f3SLinus Lüssing 		break;
3221eb1d1641SHerbert Xu 	}
3222eb1d1641SHerbert Xu out:
3223eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
3224eb1d1641SHerbert Xu }
3225eb1d1641SHerbert Xu 
32268ef2a9a5SYOSHIFUJI Hideaki static void br_ip4_multicast_leave_group(struct net_bridge *br,
32278ef2a9a5SYOSHIFUJI Hideaki 					 struct net_bridge_port *port,
3228b0e9a30dSVlad Yasevich 					 __be32 group,
32296db6f0eaSFelix Fietkau 					 __u16 vid,
32306db6f0eaSFelix Fietkau 					 const unsigned char *src)
32318ef2a9a5SYOSHIFUJI Hideaki {
32328ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip br_group;
323390010b36SLinus Lüssing 	struct bridge_mcast_own_query *own_query;
32348ef2a9a5SYOSHIFUJI Hideaki 
32358ef2a9a5SYOSHIFUJI Hideaki 	if (ipv4_is_local_multicast(group))
32368ef2a9a5SYOSHIFUJI Hideaki 		return;
32378ef2a9a5SYOSHIFUJI Hideaki 
32389632233eSNikolay Aleksandrov 	own_query = port ? &port->multicast_ctx.ip4_own_query :
3239*d3d065c0SNikolay Aleksandrov 			   &br->multicast_ctx.ip4_own_query;
324090010b36SLinus Lüssing 
32411515a63fSNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
3242eab3227bSNikolay Aleksandrov 	br_group.dst.ip4 = group;
32438ef2a9a5SYOSHIFUJI Hideaki 	br_group.proto = htons(ETH_P_IP);
3244b0e9a30dSVlad Yasevich 	br_group.vid = vid;
32458ef2a9a5SYOSHIFUJI Hideaki 
3246*d3d065c0SNikolay Aleksandrov 	br_multicast_leave_group(br, port, &br_group,
3247*d3d065c0SNikolay Aleksandrov 				 &br->multicast_ctx.ip4_other_query,
32486db6f0eaSFelix Fietkau 				 own_query, src);
32498ef2a9a5SYOSHIFUJI Hideaki }
32508ef2a9a5SYOSHIFUJI Hideaki 
3251dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
325208b202b6SYOSHIFUJI Hideaki static void br_ip6_multicast_leave_group(struct net_bridge *br,
325308b202b6SYOSHIFUJI Hideaki 					 struct net_bridge_port *port,
3254b0e9a30dSVlad Yasevich 					 const struct in6_addr *group,
32556db6f0eaSFelix Fietkau 					 __u16 vid,
32566db6f0eaSFelix Fietkau 					 const unsigned char *src)
325708b202b6SYOSHIFUJI Hideaki {
325808b202b6SYOSHIFUJI Hideaki 	struct br_ip br_group;
325990010b36SLinus Lüssing 	struct bridge_mcast_own_query *own_query;
326008b202b6SYOSHIFUJI Hideaki 
32613c3769e6SLinus Lüssing 	if (ipv6_addr_is_ll_all_nodes(group))
326208b202b6SYOSHIFUJI Hideaki 		return;
326308b202b6SYOSHIFUJI Hideaki 
32649632233eSNikolay Aleksandrov 	own_query = port ? &port->multicast_ctx.ip6_own_query :
3265*d3d065c0SNikolay Aleksandrov 			   &br->multicast_ctx.ip6_own_query;
326690010b36SLinus Lüssing 
32671515a63fSNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
3268eab3227bSNikolay Aleksandrov 	br_group.dst.ip6 = *group;
326908b202b6SYOSHIFUJI Hideaki 	br_group.proto = htons(ETH_P_IPV6);
3270b0e9a30dSVlad Yasevich 	br_group.vid = vid;
327108b202b6SYOSHIFUJI Hideaki 
3272*d3d065c0SNikolay Aleksandrov 	br_multicast_leave_group(br, port, &br_group,
3273*d3d065c0SNikolay Aleksandrov 				 &br->multicast_ctx.ip6_other_query,
32746db6f0eaSFelix Fietkau 				 own_query, src);
327508b202b6SYOSHIFUJI Hideaki }
327608b202b6SYOSHIFUJI Hideaki #endif
32778ef2a9a5SYOSHIFUJI Hideaki 
32781080ab95SNikolay Aleksandrov static void br_multicast_err_count(const struct net_bridge *br,
32791080ab95SNikolay Aleksandrov 				   const struct net_bridge_port *p,
32801080ab95SNikolay Aleksandrov 				   __be16 proto)
32811080ab95SNikolay Aleksandrov {
32821080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
32831080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats *pstats;
32841080ab95SNikolay Aleksandrov 
3285675779adSNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
32861080ab95SNikolay Aleksandrov 		return;
32871080ab95SNikolay Aleksandrov 
32881080ab95SNikolay Aleksandrov 	if (p)
32891080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
32901080ab95SNikolay Aleksandrov 	else
32911080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
32921080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
32931080ab95SNikolay Aleksandrov 		return;
32941080ab95SNikolay Aleksandrov 
32951080ab95SNikolay Aleksandrov 	pstats = this_cpu_ptr(stats);
32961080ab95SNikolay Aleksandrov 
32971080ab95SNikolay Aleksandrov 	u64_stats_update_begin(&pstats->syncp);
32981080ab95SNikolay Aleksandrov 	switch (proto) {
32991080ab95SNikolay Aleksandrov 	case htons(ETH_P_IP):
33001080ab95SNikolay Aleksandrov 		pstats->mstats.igmp_parse_errors++;
33011080ab95SNikolay Aleksandrov 		break;
33021080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
33031080ab95SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
33041080ab95SNikolay Aleksandrov 		pstats->mstats.mld_parse_errors++;
33051080ab95SNikolay Aleksandrov 		break;
33061080ab95SNikolay Aleksandrov #endif
33071080ab95SNikolay Aleksandrov 	}
33081080ab95SNikolay Aleksandrov 	u64_stats_update_end(&pstats->syncp);
33091080ab95SNikolay Aleksandrov }
33101080ab95SNikolay Aleksandrov 
331191b02d3dSNikolay Aleksandrov static void br_multicast_pim(struct net_bridge *br,
331291b02d3dSNikolay Aleksandrov 			     struct net_bridge_port *port,
331391b02d3dSNikolay Aleksandrov 			     const struct sk_buff *skb)
331491b02d3dSNikolay Aleksandrov {
331591b02d3dSNikolay Aleksandrov 	unsigned int offset = skb_transport_offset(skb);
331691b02d3dSNikolay Aleksandrov 	struct pimhdr *pimhdr, _pimhdr;
331791b02d3dSNikolay Aleksandrov 
331891b02d3dSNikolay Aleksandrov 	pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr);
331991b02d3dSNikolay Aleksandrov 	if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION ||
332091b02d3dSNikolay Aleksandrov 	    pim_hdr_type(pimhdr) != PIM_TYPE_HELLO)
332191b02d3dSNikolay Aleksandrov 		return;
332291b02d3dSNikolay Aleksandrov 
332304bef83aSNikolay Aleksandrov 	spin_lock(&br->multicast_lock);
3324d9b8c4d8SLinus Lüssing 	br_ip4_multicast_mark_router(br, port);
332504bef83aSNikolay Aleksandrov 	spin_unlock(&br->multicast_lock);
332691b02d3dSNikolay Aleksandrov }
332791b02d3dSNikolay Aleksandrov 
33284b3087c7SLinus Lüssing static int br_ip4_multicast_mrd_rcv(struct net_bridge *br,
33294b3087c7SLinus Lüssing 				    struct net_bridge_port *port,
33304b3087c7SLinus Lüssing 				    struct sk_buff *skb)
33314b3087c7SLinus Lüssing {
33324b3087c7SLinus Lüssing 	if (ip_hdr(skb)->protocol != IPPROTO_IGMP ||
33334b3087c7SLinus Lüssing 	    igmp_hdr(skb)->type != IGMP_MRDISC_ADV)
33344b3087c7SLinus Lüssing 		return -ENOMSG;
33354b3087c7SLinus Lüssing 
3336000b7287SNikolay Aleksandrov 	spin_lock(&br->multicast_lock);
3337d9b8c4d8SLinus Lüssing 	br_ip4_multicast_mark_router(br, port);
3338000b7287SNikolay Aleksandrov 	spin_unlock(&br->multicast_lock);
33394b3087c7SLinus Lüssing 
33404b3087c7SLinus Lüssing 	return 0;
33414b3087c7SLinus Lüssing }
33424b3087c7SLinus Lüssing 
3343eb1d1641SHerbert Xu static int br_multicast_ipv4_rcv(struct net_bridge *br,
3344eb1d1641SHerbert Xu 				 struct net_bridge_port *port,
334506499098SVlad Yasevich 				 struct sk_buff *skb,
334606499098SVlad Yasevich 				 u16 vid)
3347eb1d1641SHerbert Xu {
33486db6f0eaSFelix Fietkau 	const unsigned char *src;
3349eb1d1641SHerbert Xu 	struct igmphdr *ih;
3350eb1d1641SHerbert Xu 	int err;
3351eb1d1641SHerbert Xu 
3352ba5ea614SLinus Lüssing 	err = ip_mc_check_igmp(skb);
3353eb1d1641SHerbert Xu 
33549afd85c9SLinus Lüssing 	if (err == -ENOMSG) {
335591b02d3dSNikolay Aleksandrov 		if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) {
3356bd4265feSHerbert Xu 			BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
335791b02d3dSNikolay Aleksandrov 		} else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) {
335891b02d3dSNikolay Aleksandrov 			if (ip_hdr(skb)->protocol == IPPROTO_PIM)
335991b02d3dSNikolay Aleksandrov 				br_multicast_pim(br, port, skb);
33604b3087c7SLinus Lüssing 		} else if (ipv4_is_all_snoopers(ip_hdr(skb)->daddr)) {
336108e71623SLi RongQing 			br_ip4_multicast_mrd_rcv(br, port, skb);
33624b3087c7SLinus Lüssing 		}
33634b3087c7SLinus Lüssing 
3364eb1d1641SHerbert Xu 		return 0;
33659afd85c9SLinus Lüssing 	} else if (err < 0) {
33661080ab95SNikolay Aleksandrov 		br_multicast_err_count(br, port, skb->protocol);
33679afd85c9SLinus Lüssing 		return err;
3368bd4265feSHerbert Xu 	}
3369eb1d1641SHerbert Xu 
33709afd85c9SLinus Lüssing 	ih = igmp_hdr(skb);
33716db6f0eaSFelix Fietkau 	src = eth_hdr(skb)->h_source;
33721080ab95SNikolay Aleksandrov 	BR_INPUT_SKB_CB(skb)->igmp = ih->type;
3373eb1d1641SHerbert Xu 
3374eb1d1641SHerbert Xu 	switch (ih->type) {
3375eb1d1641SHerbert Xu 	case IGMP_HOST_MEMBERSHIP_REPORT:
3376eb1d1641SHerbert Xu 	case IGMPV2_HOST_MEMBERSHIP_REPORT:
337762b2bcb4SFernando Luis Vázquez Cao 		BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
33788b671779SNikolay Aleksandrov 		err = br_ip4_multicast_add_group(br, port, ih->group, vid, src,
33798b671779SNikolay Aleksandrov 						 true);
3380eb1d1641SHerbert Xu 		break;
3381eb1d1641SHerbert Xu 	case IGMPV3_HOST_MEMBERSHIP_REPORT:
3382ba5ea614SLinus Lüssing 		err = br_ip4_multicast_igmp3_report(br, port, skb, vid);
3383eb1d1641SHerbert Xu 		break;
3384eb1d1641SHerbert Xu 	case IGMP_HOST_MEMBERSHIP_QUERY:
3385ba5ea614SLinus Lüssing 		br_ip4_multicast_query(br, port, skb, vid);
3386eb1d1641SHerbert Xu 		break;
3387eb1d1641SHerbert Xu 	case IGMP_HOST_LEAVE_MESSAGE:
33886db6f0eaSFelix Fietkau 		br_ip4_multicast_leave_group(br, port, ih->group, vid, src);
3389eb1d1641SHerbert Xu 		break;
3390eb1d1641SHerbert Xu 	}
3391eb1d1641SHerbert Xu 
3392a65056ecSNikolay Aleksandrov 	br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
33931080ab95SNikolay Aleksandrov 			   BR_MCAST_DIR_RX);
33941080ab95SNikolay Aleksandrov 
3395eb1d1641SHerbert Xu 	return err;
3396eb1d1641SHerbert Xu }
3397eb1d1641SHerbert Xu 
3398dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
339999014088SLinus Lüssing static void br_ip6_multicast_mrd_rcv(struct net_bridge *br,
34004b3087c7SLinus Lüssing 				     struct net_bridge_port *port,
34014b3087c7SLinus Lüssing 				     struct sk_buff *skb)
34024b3087c7SLinus Lüssing {
34034b3087c7SLinus Lüssing 	if (icmp6_hdr(skb)->icmp6_type != ICMPV6_MRDISC_ADV)
340499014088SLinus Lüssing 		return;
34054b3087c7SLinus Lüssing 
3406000b7287SNikolay Aleksandrov 	spin_lock(&br->multicast_lock);
3407a3c02e76SLinus Lüssing 	br_ip6_multicast_mark_router(br, port);
3408000b7287SNikolay Aleksandrov 	spin_unlock(&br->multicast_lock);
34094b3087c7SLinus Lüssing }
34104b3087c7SLinus Lüssing 
341108b202b6SYOSHIFUJI Hideaki static int br_multicast_ipv6_rcv(struct net_bridge *br,
341208b202b6SYOSHIFUJI Hideaki 				 struct net_bridge_port *port,
341306499098SVlad Yasevich 				 struct sk_buff *skb,
341406499098SVlad Yasevich 				 u16 vid)
341508b202b6SYOSHIFUJI Hideaki {
34166db6f0eaSFelix Fietkau 	const unsigned char *src;
34179afd85c9SLinus Lüssing 	struct mld_msg *mld;
341808b202b6SYOSHIFUJI Hideaki 	int err;
341908b202b6SYOSHIFUJI Hideaki 
3420ba5ea614SLinus Lüssing 	err = ipv6_mc_check_mld(skb);
342108b202b6SYOSHIFUJI Hideaki 
342299014088SLinus Lüssing 	if (err == -ENOMSG || err == -ENODATA) {
34239afd85c9SLinus Lüssing 		if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
34248fad9c39SLinus Lüssing 			BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
342599014088SLinus Lüssing 		if (err == -ENODATA &&
342699014088SLinus Lüssing 		    ipv6_addr_is_all_snoopers(&ipv6_hdr(skb)->daddr))
342799014088SLinus Lüssing 			br_ip6_multicast_mrd_rcv(br, port, skb);
34284b3087c7SLinus Lüssing 
342908b202b6SYOSHIFUJI Hideaki 		return 0;
34309afd85c9SLinus Lüssing 	} else if (err < 0) {
34311080ab95SNikolay Aleksandrov 		br_multicast_err_count(br, port, skb->protocol);
34329afd85c9SLinus Lüssing 		return err;
343308b202b6SYOSHIFUJI Hideaki 	}
343408b202b6SYOSHIFUJI Hideaki 
34359afd85c9SLinus Lüssing 	mld = (struct mld_msg *)skb_transport_header(skb);
34361080ab95SNikolay Aleksandrov 	BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type;
343708b202b6SYOSHIFUJI Hideaki 
34389afd85c9SLinus Lüssing 	switch (mld->mld_type) {
343908b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_REPORT:
34406db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
3441fc2af6c7SFernando Luis Vázquez Cao 		BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
34426db6f0eaSFelix Fietkau 		err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid,
34438b671779SNikolay Aleksandrov 						 src, true);
344408b202b6SYOSHIFUJI Hideaki 		break;
344508b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MLD2_REPORT:
3446ba5ea614SLinus Lüssing 		err = br_ip6_multicast_mld2_report(br, port, skb, vid);
344708b202b6SYOSHIFUJI Hideaki 		break;
344808b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_QUERY:
3449ba5ea614SLinus Lüssing 		err = br_ip6_multicast_query(br, port, skb, vid);
345008b202b6SYOSHIFUJI Hideaki 		break;
345108b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_REDUCTION:
34526db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
34536db6f0eaSFelix Fietkau 		br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src);
34549afd85c9SLinus Lüssing 		break;
345508b202b6SYOSHIFUJI Hideaki 	}
345608b202b6SYOSHIFUJI Hideaki 
3457a65056ecSNikolay Aleksandrov 	br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
34581080ab95SNikolay Aleksandrov 			   BR_MCAST_DIR_RX);
34591080ab95SNikolay Aleksandrov 
346008b202b6SYOSHIFUJI Hideaki 	return err;
346108b202b6SYOSHIFUJI Hideaki }
346208b202b6SYOSHIFUJI Hideaki #endif
346308b202b6SYOSHIFUJI Hideaki 
3464eb1d1641SHerbert Xu int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
346506499098SVlad Yasevich 		     struct sk_buff *skb, u16 vid)
3466eb1d1641SHerbert Xu {
34671080ab95SNikolay Aleksandrov 	int ret = 0;
34681080ab95SNikolay Aleksandrov 
34691fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 	BR_INPUT_SKB_CB(skb)->igmp = 0;
34701fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 	BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
34711fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 
347213cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
3473eb1d1641SHerbert Xu 		return 0;
3474eb1d1641SHerbert Xu 
3475eb1d1641SHerbert Xu 	switch (skb->protocol) {
3476eb1d1641SHerbert Xu 	case htons(ETH_P_IP):
34771080ab95SNikolay Aleksandrov 		ret = br_multicast_ipv4_rcv(br, port, skb, vid);
34781080ab95SNikolay Aleksandrov 		break;
3479dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
348008b202b6SYOSHIFUJI Hideaki 	case htons(ETH_P_IPV6):
34811080ab95SNikolay Aleksandrov 		ret = br_multicast_ipv6_rcv(br, port, skb, vid);
34821080ab95SNikolay Aleksandrov 		break;
348308b202b6SYOSHIFUJI Hideaki #endif
3484eb1d1641SHerbert Xu 	}
3485eb1d1641SHerbert Xu 
34861080ab95SNikolay Aleksandrov 	return ret;
3487eb1d1641SHerbert Xu }
3488eb1d1641SHerbert Xu 
3489cc0fdd80SLinus Lüssing static void br_multicast_query_expired(struct net_bridge *br,
34902cd41431SLinus Lüssing 				       struct bridge_mcast_own_query *query,
34912cd41431SLinus Lüssing 				       struct bridge_mcast_querier *querier)
3492cc0fdd80SLinus Lüssing {
3493cc0fdd80SLinus Lüssing 	spin_lock(&br->multicast_lock);
3494*d3d065c0SNikolay Aleksandrov 	if (query->startup_sent < br->multicast_ctx.multicast_startup_query_count)
3495cc0fdd80SLinus Lüssing 		query->startup_sent++;
3496cc0fdd80SLinus Lüssing 
349771d9f614SEric Dumazet 	RCU_INIT_POINTER(querier->port, NULL);
3498cc0fdd80SLinus Lüssing 	br_multicast_send_query(br, NULL, query);
3499cc0fdd80SLinus Lüssing 	spin_unlock(&br->multicast_lock);
3500cc0fdd80SLinus Lüssing }
3501cc0fdd80SLinus Lüssing 
350288c1f37fSAllen Pais static void br_ip4_multicast_query_expired(struct timer_list *t)
3503eb1d1641SHerbert Xu {
3504*d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = from_timer(brmctx, t,
3505*d3d065c0SNikolay Aleksandrov 						     ip4_own_query.timer);
3506eb1d1641SHerbert Xu 
3507*d3d065c0SNikolay Aleksandrov 	br_multicast_query_expired(brmctx->br, &brmctx->ip4_own_query,
3508*d3d065c0SNikolay Aleksandrov 				   &brmctx->ip4_querier);
3509eb1d1641SHerbert Xu }
3510eb1d1641SHerbert Xu 
3511cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
351288c1f37fSAllen Pais static void br_ip6_multicast_query_expired(struct timer_list *t)
3513cc0fdd80SLinus Lüssing {
3514*d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = from_timer(brmctx, t,
3515*d3d065c0SNikolay Aleksandrov 						     ip6_own_query.timer);
3516cc0fdd80SLinus Lüssing 
3517*d3d065c0SNikolay Aleksandrov 	br_multicast_query_expired(brmctx->br, &brmctx->ip6_own_query,
3518*d3d065c0SNikolay Aleksandrov 				   &brmctx->ip6_querier);
3519cc0fdd80SLinus Lüssing }
3520cc0fdd80SLinus Lüssing #endif
3521cc0fdd80SLinus Lüssing 
3522e12cec65SNikolay Aleksandrov static void br_multicast_gc_work(struct work_struct *work)
35238b671779SNikolay Aleksandrov {
35248b671779SNikolay Aleksandrov 	struct net_bridge *br = container_of(work, struct net_bridge,
3525e12cec65SNikolay Aleksandrov 					     mcast_gc_work);
35268b671779SNikolay Aleksandrov 	HLIST_HEAD(deleted_head);
35278b671779SNikolay Aleksandrov 
35288b671779SNikolay Aleksandrov 	spin_lock_bh(&br->multicast_lock);
3529e12cec65SNikolay Aleksandrov 	hlist_move_list(&br->mcast_gc_list, &deleted_head);
35308b671779SNikolay Aleksandrov 	spin_unlock_bh(&br->multicast_lock);
35318b671779SNikolay Aleksandrov 
3532e12cec65SNikolay Aleksandrov 	br_multicast_gc(&deleted_head);
35338b671779SNikolay Aleksandrov }
35348b671779SNikolay Aleksandrov 
3535eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br)
3536eb1d1641SHerbert Xu {
3537d08c6bc0SNikolay Aleksandrov 	br->hash_max = BR_MULTICAST_DEFAULT_HASH_MAX;
3538eb1d1641SHerbert Xu 
3539*d3d065c0SNikolay Aleksandrov 	br->multicast_ctx.br = br;
3540*d3d065c0SNikolay Aleksandrov 	br->multicast_ctx.multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
3541*d3d065c0SNikolay Aleksandrov 	br->multicast_ctx.multicast_last_member_count = 2;
3542*d3d065c0SNikolay Aleksandrov 	br->multicast_ctx.multicast_startup_query_count = 2;
3543eb1d1641SHerbert Xu 
3544*d3d065c0SNikolay Aleksandrov 	br->multicast_ctx.multicast_last_member_interval = HZ;
3545*d3d065c0SNikolay Aleksandrov 	br->multicast_ctx.multicast_query_response_interval = 10 * HZ;
3546*d3d065c0SNikolay Aleksandrov 	br->multicast_ctx.multicast_startup_query_interval = 125 * HZ / 4;
3547*d3d065c0SNikolay Aleksandrov 	br->multicast_ctx.multicast_query_interval = 125 * HZ;
3548*d3d065c0SNikolay Aleksandrov 	br->multicast_ctx.multicast_querier_interval = 255 * HZ;
3549*d3d065c0SNikolay Aleksandrov 	br->multicast_ctx.multicast_membership_interval = 260 * HZ;
3550eb1d1641SHerbert Xu 
3551*d3d065c0SNikolay Aleksandrov 	br->multicast_ctx.ip4_other_query.delay_time = 0;
3552*d3d065c0SNikolay Aleksandrov 	br->multicast_ctx.ip4_querier.port = NULL;
3553*d3d065c0SNikolay Aleksandrov 	br->multicast_ctx.multicast_igmp_version = 2;
3554cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3555*d3d065c0SNikolay Aleksandrov 	br->multicast_ctx.multicast_mld_version = 1;
3556*d3d065c0SNikolay Aleksandrov 	br->multicast_ctx.ip6_other_query.delay_time = 0;
3557*d3d065c0SNikolay Aleksandrov 	br->multicast_ctx.ip6_querier.port = NULL;
3558cc0fdd80SLinus Lüssing #endif
35596919622aSIdo Schimmel 	br_opt_toggle(br, BROPT_MULTICAST_ENABLED, true);
3560675779adSNikolay Aleksandrov 	br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true);
3561b00589afSLinus Lüssing 
3562eb1d1641SHerbert Xu 	spin_lock_init(&br->multicast_lock);
3563*d3d065c0SNikolay Aleksandrov 	timer_setup(&br->multicast_ctx.ip4_mc_router_timer,
3564ee5fb222SLinus Lüssing 		    br_ip4_multicast_local_router_expired, 0);
3565*d3d065c0SNikolay Aleksandrov 	timer_setup(&br->multicast_ctx.ip4_other_query.timer,
356688c1f37fSAllen Pais 		    br_ip4_multicast_querier_expired, 0);
3567*d3d065c0SNikolay Aleksandrov 	timer_setup(&br->multicast_ctx.ip4_own_query.timer,
356888c1f37fSAllen Pais 		    br_ip4_multicast_query_expired, 0);
3569cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3570*d3d065c0SNikolay Aleksandrov 	timer_setup(&br->multicast_ctx.ip6_mc_router_timer,
3571a3c02e76SLinus Lüssing 		    br_ip6_multicast_local_router_expired, 0);
3572*d3d065c0SNikolay Aleksandrov 	timer_setup(&br->multicast_ctx.ip6_other_query.timer,
357388c1f37fSAllen Pais 		    br_ip6_multicast_querier_expired, 0);
3574*d3d065c0SNikolay Aleksandrov 	timer_setup(&br->multicast_ctx.ip6_own_query.timer,
357588c1f37fSAllen Pais 		    br_ip6_multicast_query_expired, 0);
3576cc0fdd80SLinus Lüssing #endif
357719e3a9c9SNikolay Aleksandrov 	INIT_HLIST_HEAD(&br->mdb_list);
3578e12cec65SNikolay Aleksandrov 	INIT_HLIST_HEAD(&br->mcast_gc_list);
3579e12cec65SNikolay Aleksandrov 	INIT_WORK(&br->mcast_gc_work, br_multicast_gc_work);
3580eb1d1641SHerbert Xu }
3581eb1d1641SHerbert Xu 
35824effd28cSLinus Lüssing static void br_ip4_multicast_join_snoopers(struct net_bridge *br)
35834effd28cSLinus Lüssing {
35844effd28cSLinus Lüssing 	struct in_device *in_dev = in_dev_get(br->dev);
35854effd28cSLinus Lüssing 
35864effd28cSLinus Lüssing 	if (!in_dev)
35874effd28cSLinus Lüssing 		return;
35884effd28cSLinus Lüssing 
35899fb20801SFlorian Fainelli 	__ip_mc_inc_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
35904effd28cSLinus Lüssing 	in_dev_put(in_dev);
35914effd28cSLinus Lüssing }
35924effd28cSLinus Lüssing 
35934effd28cSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
35944effd28cSLinus Lüssing static void br_ip6_multicast_join_snoopers(struct net_bridge *br)
35954effd28cSLinus Lüssing {
35964effd28cSLinus Lüssing 	struct in6_addr addr;
35974effd28cSLinus Lüssing 
35984effd28cSLinus Lüssing 	ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
35994effd28cSLinus Lüssing 	ipv6_dev_mc_inc(br->dev, &addr);
36004effd28cSLinus Lüssing }
36014effd28cSLinus Lüssing #else
36024effd28cSLinus Lüssing static inline void br_ip6_multicast_join_snoopers(struct net_bridge *br)
36034effd28cSLinus Lüssing {
36044effd28cSLinus Lüssing }
36054effd28cSLinus Lüssing #endif
36064effd28cSLinus Lüssing 
3607851d0a73SJoseph Huang void br_multicast_join_snoopers(struct net_bridge *br)
36084effd28cSLinus Lüssing {
36094effd28cSLinus Lüssing 	br_ip4_multicast_join_snoopers(br);
36104effd28cSLinus Lüssing 	br_ip6_multicast_join_snoopers(br);
36114effd28cSLinus Lüssing }
36124effd28cSLinus Lüssing 
36134effd28cSLinus Lüssing static void br_ip4_multicast_leave_snoopers(struct net_bridge *br)
36144effd28cSLinus Lüssing {
36154effd28cSLinus Lüssing 	struct in_device *in_dev = in_dev_get(br->dev);
36164effd28cSLinus Lüssing 
36174effd28cSLinus Lüssing 	if (WARN_ON(!in_dev))
36184effd28cSLinus Lüssing 		return;
36194effd28cSLinus Lüssing 
36209fb20801SFlorian Fainelli 	__ip_mc_dec_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
36214effd28cSLinus Lüssing 	in_dev_put(in_dev);
36224effd28cSLinus Lüssing }
36234effd28cSLinus Lüssing 
36244effd28cSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
36254effd28cSLinus Lüssing static void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
36264effd28cSLinus Lüssing {
36274effd28cSLinus Lüssing 	struct in6_addr addr;
36284effd28cSLinus Lüssing 
36294effd28cSLinus Lüssing 	ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
36304effd28cSLinus Lüssing 	ipv6_dev_mc_dec(br->dev, &addr);
36314effd28cSLinus Lüssing }
36324effd28cSLinus Lüssing #else
36334effd28cSLinus Lüssing static inline void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
36344effd28cSLinus Lüssing {
36354effd28cSLinus Lüssing }
36364effd28cSLinus Lüssing #endif
36374effd28cSLinus Lüssing 
3638851d0a73SJoseph Huang void br_multicast_leave_snoopers(struct net_bridge *br)
36394effd28cSLinus Lüssing {
36404effd28cSLinus Lüssing 	br_ip4_multicast_leave_snoopers(br);
36414effd28cSLinus Lüssing 	br_ip6_multicast_leave_snoopers(br);
36424effd28cSLinus Lüssing }
36434effd28cSLinus Lüssing 
3644cc0fdd80SLinus Lüssing static void __br_multicast_open(struct net_bridge *br,
364590010b36SLinus Lüssing 				struct bridge_mcast_own_query *query)
3646eb1d1641SHerbert Xu {
3647cc0fdd80SLinus Lüssing 	query->startup_sent = 0;
3648eb1d1641SHerbert Xu 
364913cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
3650eb1d1641SHerbert Xu 		return;
3651eb1d1641SHerbert Xu 
3652cc0fdd80SLinus Lüssing 	mod_timer(&query->timer, jiffies);
3653cc0fdd80SLinus Lüssing }
3654cc0fdd80SLinus Lüssing 
3655cc0fdd80SLinus Lüssing void br_multicast_open(struct net_bridge *br)
3656cc0fdd80SLinus Lüssing {
3657*d3d065c0SNikolay Aleksandrov 	__br_multicast_open(br, &br->multicast_ctx.ip4_own_query);
3658cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3659*d3d065c0SNikolay Aleksandrov 	__br_multicast_open(br, &br->multicast_ctx.ip6_own_query);
3660cc0fdd80SLinus Lüssing #endif
3661eb1d1641SHerbert Xu }
3662eb1d1641SHerbert Xu 
3663eb1d1641SHerbert Xu void br_multicast_stop(struct net_bridge *br)
3664eb1d1641SHerbert Xu {
3665*d3d065c0SNikolay Aleksandrov 	del_timer_sync(&br->multicast_ctx.ip4_mc_router_timer);
3666*d3d065c0SNikolay Aleksandrov 	del_timer_sync(&br->multicast_ctx.ip4_other_query.timer);
3667*d3d065c0SNikolay Aleksandrov 	del_timer_sync(&br->multicast_ctx.ip4_own_query.timer);
3668cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3669*d3d065c0SNikolay Aleksandrov 	del_timer_sync(&br->multicast_ctx.ip6_mc_router_timer);
3670*d3d065c0SNikolay Aleksandrov 	del_timer_sync(&br->multicast_ctx.ip6_other_query.timer);
3671*d3d065c0SNikolay Aleksandrov 	del_timer_sync(&br->multicast_ctx.ip6_own_query.timer);
3672cc0fdd80SLinus Lüssing #endif
3673e10177abSSatish Ashok }
3674e10177abSSatish Ashok 
3675e10177abSSatish Ashok void br_multicast_dev_del(struct net_bridge *br)
3676e10177abSSatish Ashok {
3677e10177abSSatish Ashok 	struct net_bridge_mdb_entry *mp;
36788b671779SNikolay Aleksandrov 	HLIST_HEAD(deleted_head);
367919e3a9c9SNikolay Aleksandrov 	struct hlist_node *tmp;
3680eb1d1641SHerbert Xu 
3681eb1d1641SHerbert Xu 	spin_lock_bh(&br->multicast_lock);
3682e12cec65SNikolay Aleksandrov 	hlist_for_each_entry_safe(mp, tmp, &br->mdb_list, mdb_node)
3683e12cec65SNikolay Aleksandrov 		br_multicast_del_mdb_entry(mp);
3684e12cec65SNikolay Aleksandrov 	hlist_move_list(&br->mcast_gc_list, &deleted_head);
3685eb1d1641SHerbert Xu 	spin_unlock_bh(&br->multicast_lock);
368619e3a9c9SNikolay Aleksandrov 
3687e12cec65SNikolay Aleksandrov 	br_multicast_gc(&deleted_head);
3688e12cec65SNikolay Aleksandrov 	cancel_work_sync(&br->mcast_gc_work);
36898b671779SNikolay Aleksandrov 
36904329596cSNikolay Aleksandrov 	rcu_barrier();
3691eb1d1641SHerbert Xu }
36920909e117SHerbert Xu 
36930909e117SHerbert Xu int br_multicast_set_router(struct net_bridge *br, unsigned long val)
36940909e117SHerbert Xu {
3695*d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = &br->multicast_ctx;
36966ae4ae8eSLinus Lüssing 	int err = -EINVAL;
36970909e117SHerbert Xu 
36980909e117SHerbert Xu 	spin_lock_bh(&br->multicast_lock);
36990909e117SHerbert Xu 
37000909e117SHerbert Xu 	switch (val) {
37017f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_DISABLED:
37027f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_PERM:
370377041420SYotam Gigi 		br_mc_router_state_change(br, val == MDB_RTR_TYPE_PERM);
3704*d3d065c0SNikolay Aleksandrov 		del_timer(&brmctx->ip4_mc_router_timer);
3705a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3706*d3d065c0SNikolay Aleksandrov 		del_timer(&brmctx->ip6_mc_router_timer);
3707a3c02e76SLinus Lüssing #endif
3708*d3d065c0SNikolay Aleksandrov 		brmctx->multicast_router = val;
370977041420SYotam Gigi 		err = 0;
371077041420SYotam Gigi 		break;
37117f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP_QUERY:
3712*d3d065c0SNikolay Aleksandrov 		if (brmctx->multicast_router != MDB_RTR_TYPE_TEMP_QUERY)
371377041420SYotam Gigi 			br_mc_router_state_change(br, false);
3714*d3d065c0SNikolay Aleksandrov 		brmctx->multicast_router = val;
37150909e117SHerbert Xu 		err = 0;
37160909e117SHerbert Xu 		break;
37170909e117SHerbert Xu 	}
37180909e117SHerbert Xu 
37190909e117SHerbert Xu 	spin_unlock_bh(&br->multicast_lock);
37200909e117SHerbert Xu 
37210909e117SHerbert Xu 	return err;
37220909e117SHerbert Xu }
37230909e117SHerbert Xu 
3724ed2d3597SLinus Lüssing static void
3725ed2d3597SLinus Lüssing br_multicast_rport_del_notify(struct net_bridge_port *p, bool deleted)
37267f0aec7aSNikolay Aleksandrov {
3727ed2d3597SLinus Lüssing 	if (!deleted)
37287f0aec7aSNikolay Aleksandrov 		return;
3729ed2d3597SLinus Lüssing 
3730a3c02e76SLinus Lüssing 	/* For backwards compatibility for now, only notify if there is
3731a3c02e76SLinus Lüssing 	 * no multicast router anymore for both IPv4 and IPv6.
3732a3c02e76SLinus Lüssing 	 */
37339632233eSNikolay Aleksandrov 	if (!hlist_unhashed(&p->multicast_ctx.ip4_rlist))
3734a3c02e76SLinus Lüssing 		return;
3735a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
37369632233eSNikolay Aleksandrov 	if (!hlist_unhashed(&p->multicast_ctx.ip6_rlist))
3737a3c02e76SLinus Lüssing 		return;
3738a3c02e76SLinus Lüssing #endif
3739a3c02e76SLinus Lüssing 
37407f0aec7aSNikolay Aleksandrov 	br_rtr_notify(p->br->dev, p, RTM_DELMDB);
37416d549648SNogah Frankel 	br_port_mc_router_state_change(p, false);
3742f12e7d95SNogah Frankel 
3743f12e7d95SNogah Frankel 	/* don't allow timer refresh */
37449632233eSNikolay Aleksandrov 	if (p->multicast_ctx.multicast_router == MDB_RTR_TYPE_TEMP)
37459632233eSNikolay Aleksandrov 		p->multicast_ctx.multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
37467f0aec7aSNikolay Aleksandrov }
37477f0aec7aSNikolay Aleksandrov 
37480909e117SHerbert Xu int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
37490909e117SHerbert Xu {
3750*d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = &p->br->multicast_ctx;
3751a55d8246SNikolay Aleksandrov 	unsigned long now = jiffies;
37526ae4ae8eSLinus Lüssing 	int err = -EINVAL;
3753ed2d3597SLinus Lüssing 	bool del = false;
37540909e117SHerbert Xu 
3755*d3d065c0SNikolay Aleksandrov 	spin_lock(&p->br->multicast_lock);
37569632233eSNikolay Aleksandrov 	if (p->multicast_ctx.multicast_router == val) {
3757a55d8246SNikolay Aleksandrov 		/* Refresh the temp router port timer */
37589632233eSNikolay Aleksandrov 		if (p->multicast_ctx.multicast_router == MDB_RTR_TYPE_TEMP) {
37599632233eSNikolay Aleksandrov 			mod_timer(&p->multicast_ctx.ip4_mc_router_timer,
3760*d3d065c0SNikolay Aleksandrov 				  now + brmctx->multicast_querier_interval);
3761a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
37629632233eSNikolay Aleksandrov 			mod_timer(&p->multicast_ctx.ip6_mc_router_timer,
3763*d3d065c0SNikolay Aleksandrov 				  now + brmctx->multicast_querier_interval);
3764a3c02e76SLinus Lüssing #endif
3765a3c02e76SLinus Lüssing 		}
37664950cfd1SNikolay Aleksandrov 		err = 0;
37674950cfd1SNikolay Aleksandrov 		goto unlock;
37684950cfd1SNikolay Aleksandrov 	}
37690909e117SHerbert Xu 	switch (val) {
37707f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_DISABLED:
37719632233eSNikolay Aleksandrov 		p->multicast_ctx.multicast_router = MDB_RTR_TYPE_DISABLED;
3772ed2d3597SLinus Lüssing 		del |= br_ip4_multicast_rport_del(p);
37739632233eSNikolay Aleksandrov 		del_timer(&p->multicast_ctx.ip4_mc_router_timer);
3774a3c02e76SLinus Lüssing 		del |= br_ip6_multicast_rport_del(p);
3775a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
37769632233eSNikolay Aleksandrov 		del_timer(&p->multicast_ctx.ip6_mc_router_timer);
3777a3c02e76SLinus Lüssing #endif
3778ed2d3597SLinus Lüssing 		br_multicast_rport_del_notify(p, del);
37790909e117SHerbert Xu 		break;
37807f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP_QUERY:
37819632233eSNikolay Aleksandrov 		p->multicast_ctx.multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
3782ed2d3597SLinus Lüssing 		del |= br_ip4_multicast_rport_del(p);
3783a3c02e76SLinus Lüssing 		del |= br_ip6_multicast_rport_del(p);
3784ed2d3597SLinus Lüssing 		br_multicast_rport_del_notify(p, del);
37857f0aec7aSNikolay Aleksandrov 		break;
37867f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_PERM:
37879632233eSNikolay Aleksandrov 		p->multicast_ctx.multicast_router = MDB_RTR_TYPE_PERM;
37889632233eSNikolay Aleksandrov 		del_timer(&p->multicast_ctx.ip4_mc_router_timer);
3789*d3d065c0SNikolay Aleksandrov 		br_ip4_multicast_add_router(p->br, p);
3790a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
37919632233eSNikolay Aleksandrov 		del_timer(&p->multicast_ctx.ip6_mc_router_timer);
3792a3c02e76SLinus Lüssing #endif
3793*d3d065c0SNikolay Aleksandrov 		br_ip6_multicast_add_router(p->br, p);
37940909e117SHerbert Xu 		break;
3795a55d8246SNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP:
37969632233eSNikolay Aleksandrov 		p->multicast_ctx.multicast_router = MDB_RTR_TYPE_TEMP;
3797*d3d065c0SNikolay Aleksandrov 		br_ip4_multicast_mark_router(p->br, p);
3798*d3d065c0SNikolay Aleksandrov 		br_ip6_multicast_mark_router(p->br, p);
3799a55d8246SNikolay Aleksandrov 		break;
38007f0aec7aSNikolay Aleksandrov 	default:
38017f0aec7aSNikolay Aleksandrov 		goto unlock;
38020909e117SHerbert Xu 	}
38037f0aec7aSNikolay Aleksandrov 	err = 0;
38047f0aec7aSNikolay Aleksandrov unlock:
3805*d3d065c0SNikolay Aleksandrov 	spin_unlock(&p->br->multicast_lock);
38060909e117SHerbert Xu 
38070909e117SHerbert Xu 	return err;
38080909e117SHerbert Xu }
3809561f1103SHerbert Xu 
3810cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br,
381190010b36SLinus Lüssing 				       struct bridge_mcast_own_query *query)
3812561f1103SHerbert Xu {
3813561f1103SHerbert Xu 	struct net_bridge_port *port;
381474857216SHerbert Xu 
3815cc0fdd80SLinus Lüssing 	__br_multicast_open(br, query);
381674857216SHerbert Xu 
3817c5b493ceSNikolay Aleksandrov 	rcu_read_lock();
3818c5b493ceSNikolay Aleksandrov 	list_for_each_entry_rcu(port, &br->port_list, list) {
381974857216SHerbert Xu 		if (port->state == BR_STATE_DISABLED ||
382074857216SHerbert Xu 		    port->state == BR_STATE_BLOCKING)
382174857216SHerbert Xu 			continue;
382274857216SHerbert Xu 
3823*d3d065c0SNikolay Aleksandrov 		if (query == &br->multicast_ctx.ip4_own_query)
38249632233eSNikolay Aleksandrov 			br_multicast_enable(&port->multicast_ctx.ip4_own_query);
3825cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3826cc0fdd80SLinus Lüssing 		else
38279632233eSNikolay Aleksandrov 			br_multicast_enable(&port->multicast_ctx.ip6_own_query);
3828cc0fdd80SLinus Lüssing #endif
382974857216SHerbert Xu 	}
3830c5b493ceSNikolay Aleksandrov 	rcu_read_unlock();
383174857216SHerbert Xu }
383274857216SHerbert Xu 
3833ae1ea84bSFlorian Fainelli int br_multicast_toggle(struct net_bridge *br, unsigned long val,
3834ae1ea84bSFlorian Fainelli 			struct netlink_ext_ack *extack)
383574857216SHerbert Xu {
38367cb3f921SNikolay Aleksandrov 	struct net_bridge_port *port;
3837851d0a73SJoseph Huang 	bool change_snoopers = false;
3838ae1ea84bSFlorian Fainelli 	int err = 0;
3839561f1103SHerbert Xu 
3840ef5e0d82SAndrey Vagin 	spin_lock_bh(&br->multicast_lock);
384113cefad2SNikolay Aleksandrov 	if (!!br_opt_get(br, BROPT_MULTICAST_ENABLED) == !!val)
3842561f1103SHerbert Xu 		goto unlock;
3843561f1103SHerbert Xu 
3844ae1ea84bSFlorian Fainelli 	err = br_mc_disabled_update(br->dev, val, extack);
3845ae1ea84bSFlorian Fainelli 	if (err == -EOPNOTSUPP)
3846ae1ea84bSFlorian Fainelli 		err = 0;
3847ae1ea84bSFlorian Fainelli 	if (err)
3848ae1ea84bSFlorian Fainelli 		goto unlock;
3849ae1ea84bSFlorian Fainelli 
385013cefad2SNikolay Aleksandrov 	br_opt_toggle(br, BROPT_MULTICAST_ENABLED, !!val);
38514effd28cSLinus Lüssing 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) {
3852851d0a73SJoseph Huang 		change_snoopers = true;
3853561f1103SHerbert Xu 		goto unlock;
38544effd28cSLinus Lüssing 	}
3855561f1103SHerbert Xu 
38563a7fda06SHerbert Xu 	if (!netif_running(br->dev))
38573a7fda06SHerbert Xu 		goto unlock;
38583a7fda06SHerbert Xu 
38597cb3f921SNikolay Aleksandrov 	br_multicast_open(br);
38607cb3f921SNikolay Aleksandrov 	list_for_each_entry(port, &br->port_list, list)
38617cb3f921SNikolay Aleksandrov 		__br_multicast_enable_port(port);
3862561f1103SHerbert Xu 
3863851d0a73SJoseph Huang 	change_snoopers = true;
3864851d0a73SJoseph Huang 
3865561f1103SHerbert Xu unlock:
3866ef5e0d82SAndrey Vagin 	spin_unlock_bh(&br->multicast_lock);
3867561f1103SHerbert Xu 
3868851d0a73SJoseph Huang 	/* br_multicast_join_snoopers has the potential to cause
3869851d0a73SJoseph Huang 	 * an MLD Report/Leave to be delivered to br_multicast_rcv,
3870851d0a73SJoseph Huang 	 * which would in turn call br_multicast_add_group, which would
3871851d0a73SJoseph Huang 	 * attempt to acquire multicast_lock. This function should be
3872851d0a73SJoseph Huang 	 * called after the lock has been released to avoid deadlocks on
3873851d0a73SJoseph Huang 	 * multicast_lock.
3874851d0a73SJoseph Huang 	 *
3875851d0a73SJoseph Huang 	 * br_multicast_leave_snoopers does not have the problem since
3876851d0a73SJoseph Huang 	 * br_multicast_rcv first checks BROPT_MULTICAST_ENABLED, and
3877851d0a73SJoseph Huang 	 * returns without calling br_multicast_ipv4/6_rcv if it's not
3878851d0a73SJoseph Huang 	 * enabled. Moved both functions out just for symmetry.
3879851d0a73SJoseph Huang 	 */
3880851d0a73SJoseph Huang 	if (change_snoopers) {
3881851d0a73SJoseph Huang 		if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
3882851d0a73SJoseph Huang 			br_multicast_join_snoopers(br);
3883851d0a73SJoseph Huang 		else
3884851d0a73SJoseph Huang 			br_multicast_leave_snoopers(br);
3885851d0a73SJoseph Huang 	}
3886851d0a73SJoseph Huang 
3887ae1ea84bSFlorian Fainelli 	return err;
3888561f1103SHerbert Xu }
3889b195167fSHerbert Xu 
38909341b988SIdo Schimmel bool br_multicast_enabled(const struct net_device *dev)
38919341b988SIdo Schimmel {
38929341b988SIdo Schimmel 	struct net_bridge *br = netdev_priv(dev);
38939341b988SIdo Schimmel 
389413cefad2SNikolay Aleksandrov 	return !!br_opt_get(br, BROPT_MULTICAST_ENABLED);
38959341b988SIdo Schimmel }
38969341b988SIdo Schimmel EXPORT_SYMBOL_GPL(br_multicast_enabled);
38979341b988SIdo Schimmel 
38980912bda4SYotam Gigi bool br_multicast_router(const struct net_device *dev)
38990912bda4SYotam Gigi {
39000912bda4SYotam Gigi 	struct net_bridge *br = netdev_priv(dev);
39010912bda4SYotam Gigi 	bool is_router;
39020912bda4SYotam Gigi 
39030912bda4SYotam Gigi 	spin_lock_bh(&br->multicast_lock);
39041a3065a2SLinus Lüssing 	is_router = br_multicast_is_router(br, NULL);
39050912bda4SYotam Gigi 	spin_unlock_bh(&br->multicast_lock);
39060912bda4SYotam Gigi 	return is_router;
39070912bda4SYotam Gigi }
39080912bda4SYotam Gigi EXPORT_SYMBOL_GPL(br_multicast_router);
39090912bda4SYotam Gigi 
3910c5c23260SHerbert Xu int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
3911c5c23260SHerbert Xu {
3912*d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx = &br->multicast_ctx;
3913b00589afSLinus Lüssing 	unsigned long max_delay;
3914b00589afSLinus Lüssing 
3915c5c23260SHerbert Xu 	val = !!val;
3916c5c23260SHerbert Xu 
3917c5c23260SHerbert Xu 	spin_lock_bh(&br->multicast_lock);
3918675779adSNikolay Aleksandrov 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER) == val)
3919c5c23260SHerbert Xu 		goto unlock;
3920c5c23260SHerbert Xu 
3921675779adSNikolay Aleksandrov 	br_opt_toggle(br, BROPT_MULTICAST_QUERIER, !!val);
3922b00589afSLinus Lüssing 	if (!val)
3923b00589afSLinus Lüssing 		goto unlock;
3924b00589afSLinus Lüssing 
3925*d3d065c0SNikolay Aleksandrov 	max_delay = brmctx->multicast_query_response_interval;
3926b00589afSLinus Lüssing 
3927*d3d065c0SNikolay Aleksandrov 	if (!timer_pending(&brmctx->ip4_other_query.timer))
3928*d3d065c0SNikolay Aleksandrov 		brmctx->ip4_other_query.delay_time = jiffies + max_delay;
3929cc0fdd80SLinus Lüssing 
3930*d3d065c0SNikolay Aleksandrov 	br_multicast_start_querier(br, &brmctx->ip4_own_query);
3931cc0fdd80SLinus Lüssing 
3932cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3933*d3d065c0SNikolay Aleksandrov 	if (!timer_pending(&brmctx->ip6_other_query.timer))
3934*d3d065c0SNikolay Aleksandrov 		brmctx->ip6_other_query.delay_time = jiffies + max_delay;
3935cc0fdd80SLinus Lüssing 
3936*d3d065c0SNikolay Aleksandrov 	br_multicast_start_querier(br, &brmctx->ip6_own_query);
3937cc0fdd80SLinus Lüssing #endif
3938c5c23260SHerbert Xu 
3939c5c23260SHerbert Xu unlock:
3940c5c23260SHerbert Xu 	spin_unlock_bh(&br->multicast_lock);
3941c5c23260SHerbert Xu 
3942c5c23260SHerbert Xu 	return 0;
3943c5c23260SHerbert Xu }
3944c5c23260SHerbert Xu 
39455e923585SNikolay Aleksandrov int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val)
39465e923585SNikolay Aleksandrov {
39475e923585SNikolay Aleksandrov 	/* Currently we support only version 2 and 3 */
39485e923585SNikolay Aleksandrov 	switch (val) {
39495e923585SNikolay Aleksandrov 	case 2:
39505e923585SNikolay Aleksandrov 	case 3:
39515e923585SNikolay Aleksandrov 		break;
39525e923585SNikolay Aleksandrov 	default:
39535e923585SNikolay Aleksandrov 		return -EINVAL;
39545e923585SNikolay Aleksandrov 	}
39555e923585SNikolay Aleksandrov 
39565e923585SNikolay Aleksandrov 	spin_lock_bh(&br->multicast_lock);
3957*d3d065c0SNikolay Aleksandrov 	br->multicast_ctx.multicast_igmp_version = val;
39585e923585SNikolay Aleksandrov 	spin_unlock_bh(&br->multicast_lock);
39595e923585SNikolay Aleksandrov 
39605e923585SNikolay Aleksandrov 	return 0;
39615e923585SNikolay Aleksandrov }
39625e923585SNikolay Aleksandrov 
3963aa2ae3e7SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
3964aa2ae3e7SNikolay Aleksandrov int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val)
3965aa2ae3e7SNikolay Aleksandrov {
3966aa2ae3e7SNikolay Aleksandrov 	/* Currently we support version 1 and 2 */
3967aa2ae3e7SNikolay Aleksandrov 	switch (val) {
3968aa2ae3e7SNikolay Aleksandrov 	case 1:
3969aa2ae3e7SNikolay Aleksandrov 	case 2:
3970aa2ae3e7SNikolay Aleksandrov 		break;
3971aa2ae3e7SNikolay Aleksandrov 	default:
3972aa2ae3e7SNikolay Aleksandrov 		return -EINVAL;
3973aa2ae3e7SNikolay Aleksandrov 	}
3974aa2ae3e7SNikolay Aleksandrov 
3975aa2ae3e7SNikolay Aleksandrov 	spin_lock_bh(&br->multicast_lock);
3976*d3d065c0SNikolay Aleksandrov 	br->multicast_ctx.multicast_mld_version = val;
3977aa2ae3e7SNikolay Aleksandrov 	spin_unlock_bh(&br->multicast_lock);
3978aa2ae3e7SNikolay Aleksandrov 
3979aa2ae3e7SNikolay Aleksandrov 	return 0;
3980aa2ae3e7SNikolay Aleksandrov }
3981aa2ae3e7SNikolay Aleksandrov #endif
3982aa2ae3e7SNikolay Aleksandrov 
398307f8ac4aSLinus Lüssing /**
398407f8ac4aSLinus Lüssing  * br_multicast_list_adjacent - Returns snooped multicast addresses
398507f8ac4aSLinus Lüssing  * @dev:	The bridge port adjacent to which to retrieve addresses
398607f8ac4aSLinus Lüssing  * @br_ip_list:	The list to store found, snooped multicast IP addresses in
398707f8ac4aSLinus Lüssing  *
398807f8ac4aSLinus Lüssing  * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
398907f8ac4aSLinus Lüssing  * snooping feature on all bridge ports of dev's bridge device, excluding
399007f8ac4aSLinus Lüssing  * the addresses from dev itself.
399107f8ac4aSLinus Lüssing  *
399207f8ac4aSLinus Lüssing  * Returns the number of items added to br_ip_list.
399307f8ac4aSLinus Lüssing  *
399407f8ac4aSLinus Lüssing  * Notes:
399507f8ac4aSLinus Lüssing  * - br_ip_list needs to be initialized by caller
399607f8ac4aSLinus Lüssing  * - br_ip_list might contain duplicates in the end
399707f8ac4aSLinus Lüssing  *   (needs to be taken care of by caller)
399807f8ac4aSLinus Lüssing  * - br_ip_list needs to be freed by caller
399907f8ac4aSLinus Lüssing  */
400007f8ac4aSLinus Lüssing int br_multicast_list_adjacent(struct net_device *dev,
400107f8ac4aSLinus Lüssing 			       struct list_head *br_ip_list)
400207f8ac4aSLinus Lüssing {
400307f8ac4aSLinus Lüssing 	struct net_bridge *br;
400407f8ac4aSLinus Lüssing 	struct net_bridge_port *port;
400507f8ac4aSLinus Lüssing 	struct net_bridge_port_group *group;
400607f8ac4aSLinus Lüssing 	struct br_ip_list *entry;
400707f8ac4aSLinus Lüssing 	int count = 0;
400807f8ac4aSLinus Lüssing 
400907f8ac4aSLinus Lüssing 	rcu_read_lock();
401035f861e3SJulian Wiedmann 	if (!br_ip_list || !netif_is_bridge_port(dev))
401107f8ac4aSLinus Lüssing 		goto unlock;
401207f8ac4aSLinus Lüssing 
401307f8ac4aSLinus Lüssing 	port = br_port_get_rcu(dev);
401407f8ac4aSLinus Lüssing 	if (!port || !port->br)
401507f8ac4aSLinus Lüssing 		goto unlock;
401607f8ac4aSLinus Lüssing 
401707f8ac4aSLinus Lüssing 	br = port->br;
401807f8ac4aSLinus Lüssing 
401907f8ac4aSLinus Lüssing 	list_for_each_entry_rcu(port, &br->port_list, list) {
402007f8ac4aSLinus Lüssing 		if (!port->dev || port->dev == dev)
402107f8ac4aSLinus Lüssing 			continue;
402207f8ac4aSLinus Lüssing 
402307f8ac4aSLinus Lüssing 		hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
402407f8ac4aSLinus Lüssing 			entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
402507f8ac4aSLinus Lüssing 			if (!entry)
402607f8ac4aSLinus Lüssing 				goto unlock;
402707f8ac4aSLinus Lüssing 
4028085b53c8SNikolay Aleksandrov 			entry->addr = group->key.addr;
402907f8ac4aSLinus Lüssing 			list_add(&entry->list, br_ip_list);
403007f8ac4aSLinus Lüssing 			count++;
403107f8ac4aSLinus Lüssing 		}
403207f8ac4aSLinus Lüssing 	}
403307f8ac4aSLinus Lüssing 
403407f8ac4aSLinus Lüssing unlock:
403507f8ac4aSLinus Lüssing 	rcu_read_unlock();
403607f8ac4aSLinus Lüssing 	return count;
403707f8ac4aSLinus Lüssing }
403807f8ac4aSLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
40392cd41431SLinus Lüssing 
40402cd41431SLinus Lüssing /**
4041c34963e2SLinus Lüssing  * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
4042c34963e2SLinus Lüssing  * @dev: The bridge port providing the bridge on which to check for a querier
4043c34963e2SLinus Lüssing  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
4044c34963e2SLinus Lüssing  *
4045c34963e2SLinus Lüssing  * Checks whether the given interface has a bridge on top and if so returns
4046c34963e2SLinus Lüssing  * true if a valid querier exists anywhere on the bridged link layer.
4047c34963e2SLinus Lüssing  * Otherwise returns false.
4048c34963e2SLinus Lüssing  */
4049c34963e2SLinus Lüssing bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
4050c34963e2SLinus Lüssing {
4051c34963e2SLinus Lüssing 	struct net_bridge *br;
4052c34963e2SLinus Lüssing 	struct net_bridge_port *port;
4053c34963e2SLinus Lüssing 	struct ethhdr eth;
4054c34963e2SLinus Lüssing 	bool ret = false;
4055c34963e2SLinus Lüssing 
4056c34963e2SLinus Lüssing 	rcu_read_lock();
405735f861e3SJulian Wiedmann 	if (!netif_is_bridge_port(dev))
4058c34963e2SLinus Lüssing 		goto unlock;
4059c34963e2SLinus Lüssing 
4060c34963e2SLinus Lüssing 	port = br_port_get_rcu(dev);
4061c34963e2SLinus Lüssing 	if (!port || !port->br)
4062c34963e2SLinus Lüssing 		goto unlock;
4063c34963e2SLinus Lüssing 
4064c34963e2SLinus Lüssing 	br = port->br;
4065c34963e2SLinus Lüssing 
4066c34963e2SLinus Lüssing 	memset(&eth, 0, sizeof(eth));
4067c34963e2SLinus Lüssing 	eth.h_proto = htons(proto);
4068c34963e2SLinus Lüssing 
4069955062b0SNikolay Aleksandrov 	ret = br_multicast_querier_exists(br, &eth, NULL);
4070c34963e2SLinus Lüssing 
4071c34963e2SLinus Lüssing unlock:
4072c34963e2SLinus Lüssing 	rcu_read_unlock();
4073c34963e2SLinus Lüssing 	return ret;
4074c34963e2SLinus Lüssing }
4075c34963e2SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
4076c34963e2SLinus Lüssing 
4077c34963e2SLinus Lüssing /**
40782cd41431SLinus Lüssing  * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
40792cd41431SLinus Lüssing  * @dev: The bridge port adjacent to which to check for a querier
40802cd41431SLinus Lüssing  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
40812cd41431SLinus Lüssing  *
40822cd41431SLinus Lüssing  * Checks whether the given interface has a bridge on top and if so returns
40832cd41431SLinus Lüssing  * true if a selected querier is behind one of the other ports of this
40842cd41431SLinus Lüssing  * bridge. Otherwise returns false.
40852cd41431SLinus Lüssing  */
40862cd41431SLinus Lüssing bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
40872cd41431SLinus Lüssing {
4088*d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx;
40892cd41431SLinus Lüssing 	struct net_bridge *br;
40902cd41431SLinus Lüssing 	struct net_bridge_port *port;
40912cd41431SLinus Lüssing 	bool ret = false;
40922cd41431SLinus Lüssing 
40932cd41431SLinus Lüssing 	rcu_read_lock();
409435f861e3SJulian Wiedmann 	if (!netif_is_bridge_port(dev))
40952cd41431SLinus Lüssing 		goto unlock;
40962cd41431SLinus Lüssing 
40972cd41431SLinus Lüssing 	port = br_port_get_rcu(dev);
40982cd41431SLinus Lüssing 	if (!port || !port->br)
40992cd41431SLinus Lüssing 		goto unlock;
41002cd41431SLinus Lüssing 
41012cd41431SLinus Lüssing 	br = port->br;
4102*d3d065c0SNikolay Aleksandrov 	brmctx = &br->multicast_ctx;
41032cd41431SLinus Lüssing 
41042cd41431SLinus Lüssing 	switch (proto) {
41052cd41431SLinus Lüssing 	case ETH_P_IP:
4106*d3d065c0SNikolay Aleksandrov 		if (!timer_pending(&brmctx->ip4_other_query.timer) ||
4107*d3d065c0SNikolay Aleksandrov 		    rcu_dereference(brmctx->ip4_querier.port) == port)
41082cd41431SLinus Lüssing 			goto unlock;
41092cd41431SLinus Lüssing 		break;
41103993c4e1SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
41112cd41431SLinus Lüssing 	case ETH_P_IPV6:
4112*d3d065c0SNikolay Aleksandrov 		if (!timer_pending(&brmctx->ip6_other_query.timer) ||
4113*d3d065c0SNikolay Aleksandrov 		    rcu_dereference(brmctx->ip6_querier.port) == port)
41142cd41431SLinus Lüssing 			goto unlock;
41152cd41431SLinus Lüssing 		break;
41163993c4e1SLinus Lüssing #endif
41172cd41431SLinus Lüssing 	default:
41182cd41431SLinus Lüssing 		goto unlock;
41192cd41431SLinus Lüssing 	}
41202cd41431SLinus Lüssing 
41212cd41431SLinus Lüssing 	ret = true;
41222cd41431SLinus Lüssing unlock:
41232cd41431SLinus Lüssing 	rcu_read_unlock();
41242cd41431SLinus Lüssing 	return ret;
41252cd41431SLinus Lüssing }
41262cd41431SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
41271080ab95SNikolay Aleksandrov 
41283b85f9baSLinus Lüssing /**
41293b85f9baSLinus Lüssing  * br_multicast_has_router_adjacent - Checks for a router behind a bridge port
41303b85f9baSLinus Lüssing  * @dev: The bridge port adjacent to which to check for a multicast router
41313b85f9baSLinus Lüssing  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
41323b85f9baSLinus Lüssing  *
41333b85f9baSLinus Lüssing  * Checks whether the given interface has a bridge on top and if so returns
41343b85f9baSLinus Lüssing  * true if a multicast router is behind one of the other ports of this
41353b85f9baSLinus Lüssing  * bridge. Otherwise returns false.
41363b85f9baSLinus Lüssing  */
41373b85f9baSLinus Lüssing bool br_multicast_has_router_adjacent(struct net_device *dev, int proto)
41383b85f9baSLinus Lüssing {
41399632233eSNikolay Aleksandrov 	struct net_bridge_mcast_port *pmctx;
4140*d3d065c0SNikolay Aleksandrov 	struct net_bridge_mcast *brmctx;
41419632233eSNikolay Aleksandrov 	struct net_bridge_port *port;
41423b85f9baSLinus Lüssing 	bool ret = false;
41433b85f9baSLinus Lüssing 
41443b85f9baSLinus Lüssing 	rcu_read_lock();
41453b85f9baSLinus Lüssing 	port = br_port_get_check_rcu(dev);
41463b85f9baSLinus Lüssing 	if (!port)
41473b85f9baSLinus Lüssing 		goto unlock;
41483b85f9baSLinus Lüssing 
4149*d3d065c0SNikolay Aleksandrov 	brmctx = &port->br->multicast_ctx;
41503b85f9baSLinus Lüssing 	switch (proto) {
41513b85f9baSLinus Lüssing 	case ETH_P_IP:
4152*d3d065c0SNikolay Aleksandrov 		hlist_for_each_entry_rcu(pmctx, &brmctx->ip4_mc_router_list,
41533b85f9baSLinus Lüssing 					 ip4_rlist) {
41549632233eSNikolay Aleksandrov 			if (pmctx->port == port)
41553b85f9baSLinus Lüssing 				continue;
41563b85f9baSLinus Lüssing 
41573b85f9baSLinus Lüssing 			ret = true;
41583b85f9baSLinus Lüssing 			goto unlock;
41593b85f9baSLinus Lüssing 		}
41603b85f9baSLinus Lüssing 		break;
41613b85f9baSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
41623b85f9baSLinus Lüssing 	case ETH_P_IPV6:
4163*d3d065c0SNikolay Aleksandrov 		hlist_for_each_entry_rcu(pmctx, &brmctx->ip6_mc_router_list,
41643b85f9baSLinus Lüssing 					 ip6_rlist) {
41659632233eSNikolay Aleksandrov 			if (pmctx->port == port)
41663b85f9baSLinus Lüssing 				continue;
41673b85f9baSLinus Lüssing 
41683b85f9baSLinus Lüssing 			ret = true;
41693b85f9baSLinus Lüssing 			goto unlock;
41703b85f9baSLinus Lüssing 		}
41713b85f9baSLinus Lüssing 		break;
41723b85f9baSLinus Lüssing #endif
41733b85f9baSLinus Lüssing 	default:
41743b85f9baSLinus Lüssing 		/* when compiled without IPv6 support, be conservative and
41753b85f9baSLinus Lüssing 		 * always assume presence of an IPv6 multicast router
41763b85f9baSLinus Lüssing 		 */
41773b85f9baSLinus Lüssing 		ret = true;
41783b85f9baSLinus Lüssing 	}
41793b85f9baSLinus Lüssing 
41803b85f9baSLinus Lüssing unlock:
41813b85f9baSLinus Lüssing 	rcu_read_unlock();
41823b85f9baSLinus Lüssing 	return ret;
41833b85f9baSLinus Lüssing }
41843b85f9baSLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_router_adjacent);
41853b85f9baSLinus Lüssing 
41861080ab95SNikolay Aleksandrov static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
4187a65056ecSNikolay Aleksandrov 			       const struct sk_buff *skb, u8 type, u8 dir)
41881080ab95SNikolay Aleksandrov {
41891080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
4190a65056ecSNikolay Aleksandrov 	__be16 proto = skb->protocol;
4191a65056ecSNikolay Aleksandrov 	unsigned int t_len;
41921080ab95SNikolay Aleksandrov 
41931080ab95SNikolay Aleksandrov 	u64_stats_update_begin(&pstats->syncp);
41941080ab95SNikolay Aleksandrov 	switch (proto) {
41951080ab95SNikolay Aleksandrov 	case htons(ETH_P_IP):
4196a65056ecSNikolay Aleksandrov 		t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
41971080ab95SNikolay Aleksandrov 		switch (type) {
41981080ab95SNikolay Aleksandrov 		case IGMP_HOST_MEMBERSHIP_REPORT:
41991080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v1reports[dir]++;
42001080ab95SNikolay Aleksandrov 			break;
42011080ab95SNikolay Aleksandrov 		case IGMPV2_HOST_MEMBERSHIP_REPORT:
42021080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v2reports[dir]++;
42031080ab95SNikolay Aleksandrov 			break;
42041080ab95SNikolay Aleksandrov 		case IGMPV3_HOST_MEMBERSHIP_REPORT:
42051080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v3reports[dir]++;
42061080ab95SNikolay Aleksandrov 			break;
42071080ab95SNikolay Aleksandrov 		case IGMP_HOST_MEMBERSHIP_QUERY:
4208a65056ecSNikolay Aleksandrov 			if (t_len != sizeof(struct igmphdr)) {
4209a65056ecSNikolay Aleksandrov 				pstats->mstats.igmp_v3queries[dir]++;
4210a65056ecSNikolay Aleksandrov 			} else {
4211a65056ecSNikolay Aleksandrov 				unsigned int offset = skb_transport_offset(skb);
4212a65056ecSNikolay Aleksandrov 				struct igmphdr *ih, _ihdr;
4213a65056ecSNikolay Aleksandrov 
4214a65056ecSNikolay Aleksandrov 				ih = skb_header_pointer(skb, offset,
4215a65056ecSNikolay Aleksandrov 							sizeof(_ihdr), &_ihdr);
4216a65056ecSNikolay Aleksandrov 				if (!ih)
4217a65056ecSNikolay Aleksandrov 					break;
4218a65056ecSNikolay Aleksandrov 				if (!ih->code)
4219a65056ecSNikolay Aleksandrov 					pstats->mstats.igmp_v1queries[dir]++;
4220a65056ecSNikolay Aleksandrov 				else
4221a65056ecSNikolay Aleksandrov 					pstats->mstats.igmp_v2queries[dir]++;
4222a65056ecSNikolay Aleksandrov 			}
42231080ab95SNikolay Aleksandrov 			break;
42241080ab95SNikolay Aleksandrov 		case IGMP_HOST_LEAVE_MESSAGE:
42251080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_leaves[dir]++;
42261080ab95SNikolay Aleksandrov 			break;
42271080ab95SNikolay Aleksandrov 		}
42281080ab95SNikolay Aleksandrov 		break;
42291080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
42301080ab95SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
4231a65056ecSNikolay Aleksandrov 		t_len = ntohs(ipv6_hdr(skb)->payload_len) +
4232a65056ecSNikolay Aleksandrov 			sizeof(struct ipv6hdr);
4233a65056ecSNikolay Aleksandrov 		t_len -= skb_network_header_len(skb);
42341080ab95SNikolay Aleksandrov 		switch (type) {
42351080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_REPORT:
42361080ab95SNikolay Aleksandrov 			pstats->mstats.mld_v1reports[dir]++;
42371080ab95SNikolay Aleksandrov 			break;
42381080ab95SNikolay Aleksandrov 		case ICMPV6_MLD2_REPORT:
42391080ab95SNikolay Aleksandrov 			pstats->mstats.mld_v2reports[dir]++;
42401080ab95SNikolay Aleksandrov 			break;
42411080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_QUERY:
4242a65056ecSNikolay Aleksandrov 			if (t_len != sizeof(struct mld_msg))
4243a65056ecSNikolay Aleksandrov 				pstats->mstats.mld_v2queries[dir]++;
4244a65056ecSNikolay Aleksandrov 			else
4245a65056ecSNikolay Aleksandrov 				pstats->mstats.mld_v1queries[dir]++;
42461080ab95SNikolay Aleksandrov 			break;
42471080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_REDUCTION:
42481080ab95SNikolay Aleksandrov 			pstats->mstats.mld_leaves[dir]++;
42491080ab95SNikolay Aleksandrov 			break;
42501080ab95SNikolay Aleksandrov 		}
42511080ab95SNikolay Aleksandrov 		break;
42521080ab95SNikolay Aleksandrov #endif /* CONFIG_IPV6 */
42531080ab95SNikolay Aleksandrov 	}
42541080ab95SNikolay Aleksandrov 	u64_stats_update_end(&pstats->syncp);
42551080ab95SNikolay Aleksandrov }
42561080ab95SNikolay Aleksandrov 
42571080ab95SNikolay Aleksandrov void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
4258a65056ecSNikolay Aleksandrov 			const struct sk_buff *skb, u8 type, u8 dir)
42591080ab95SNikolay Aleksandrov {
42601080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
42611080ab95SNikolay Aleksandrov 
42621080ab95SNikolay Aleksandrov 	/* if multicast_disabled is true then igmp type can't be set */
4263675779adSNikolay Aleksandrov 	if (!type || !br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
42641080ab95SNikolay Aleksandrov 		return;
42651080ab95SNikolay Aleksandrov 
42661080ab95SNikolay Aleksandrov 	if (p)
42671080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
42681080ab95SNikolay Aleksandrov 	else
42691080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
42701080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
42711080ab95SNikolay Aleksandrov 		return;
42721080ab95SNikolay Aleksandrov 
4273a65056ecSNikolay Aleksandrov 	br_mcast_stats_add(stats, skb, type, dir);
42741080ab95SNikolay Aleksandrov }
42751080ab95SNikolay Aleksandrov 
42761080ab95SNikolay Aleksandrov int br_multicast_init_stats(struct net_bridge *br)
42771080ab95SNikolay Aleksandrov {
42781080ab95SNikolay Aleksandrov 	br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
42791080ab95SNikolay Aleksandrov 	if (!br->mcast_stats)
42801080ab95SNikolay Aleksandrov 		return -ENOMEM;
42811080ab95SNikolay Aleksandrov 
42821080ab95SNikolay Aleksandrov 	return 0;
42831080ab95SNikolay Aleksandrov }
42841080ab95SNikolay Aleksandrov 
4285b6fe0440SIdo Schimmel void br_multicast_uninit_stats(struct net_bridge *br)
4286b6fe0440SIdo Schimmel {
4287b6fe0440SIdo Schimmel 	free_percpu(br->mcast_stats);
4288b6fe0440SIdo Schimmel }
4289b6fe0440SIdo Schimmel 
4290b3b6a84cSArnd Bergmann /* noinline for https://bugs.llvm.org/show_bug.cgi?id=45802#c9 */
4291b3b6a84cSArnd Bergmann static noinline_for_stack void mcast_stats_add_dir(u64 *dst, u64 *src)
42921080ab95SNikolay Aleksandrov {
42931080ab95SNikolay Aleksandrov 	dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
42941080ab95SNikolay Aleksandrov 	dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
42951080ab95SNikolay Aleksandrov }
42961080ab95SNikolay Aleksandrov 
42971080ab95SNikolay Aleksandrov void br_multicast_get_stats(const struct net_bridge *br,
42981080ab95SNikolay Aleksandrov 			    const struct net_bridge_port *p,
42991080ab95SNikolay Aleksandrov 			    struct br_mcast_stats *dest)
43001080ab95SNikolay Aleksandrov {
43011080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
43021080ab95SNikolay Aleksandrov 	struct br_mcast_stats tdst;
43031080ab95SNikolay Aleksandrov 	int i;
43041080ab95SNikolay Aleksandrov 
43051080ab95SNikolay Aleksandrov 	memset(dest, 0, sizeof(*dest));
43061080ab95SNikolay Aleksandrov 	if (p)
43071080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
43081080ab95SNikolay Aleksandrov 	else
43091080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
43101080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
43111080ab95SNikolay Aleksandrov 		return;
43121080ab95SNikolay Aleksandrov 
43131080ab95SNikolay Aleksandrov 	memset(&tdst, 0, sizeof(tdst));
43141080ab95SNikolay Aleksandrov 	for_each_possible_cpu(i) {
43151080ab95SNikolay Aleksandrov 		struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
43161080ab95SNikolay Aleksandrov 		struct br_mcast_stats temp;
43171080ab95SNikolay Aleksandrov 		unsigned int start;
43181080ab95SNikolay Aleksandrov 
43191080ab95SNikolay Aleksandrov 		do {
43201080ab95SNikolay Aleksandrov 			start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
43211080ab95SNikolay Aleksandrov 			memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
43221080ab95SNikolay Aleksandrov 		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
43231080ab95SNikolay Aleksandrov 
4324a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
4325a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
4326a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
43271080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
43281080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
43291080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
43301080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
43311080ab95SNikolay Aleksandrov 		tdst.igmp_parse_errors += temp.igmp_parse_errors;
43321080ab95SNikolay Aleksandrov 
4333a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
4334a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
43351080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
43361080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
43371080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
43381080ab95SNikolay Aleksandrov 		tdst.mld_parse_errors += temp.mld_parse_errors;
43391080ab95SNikolay Aleksandrov 	}
43401080ab95SNikolay Aleksandrov 	memcpy(dest, &tdst, sizeof(*dest));
43411080ab95SNikolay Aleksandrov }
434219e3a9c9SNikolay Aleksandrov 
434319e3a9c9SNikolay Aleksandrov int br_mdb_hash_init(struct net_bridge *br)
434419e3a9c9SNikolay Aleksandrov {
4345085b53c8SNikolay Aleksandrov 	int err;
4346085b53c8SNikolay Aleksandrov 
4347085b53c8SNikolay Aleksandrov 	err = rhashtable_init(&br->sg_port_tbl, &br_sg_port_rht_params);
4348085b53c8SNikolay Aleksandrov 	if (err)
4349085b53c8SNikolay Aleksandrov 		return err;
4350085b53c8SNikolay Aleksandrov 
4351085b53c8SNikolay Aleksandrov 	err = rhashtable_init(&br->mdb_hash_tbl, &br_mdb_rht_params);
4352085b53c8SNikolay Aleksandrov 	if (err) {
4353085b53c8SNikolay Aleksandrov 		rhashtable_destroy(&br->sg_port_tbl);
4354085b53c8SNikolay Aleksandrov 		return err;
4355085b53c8SNikolay Aleksandrov 	}
4356085b53c8SNikolay Aleksandrov 
4357085b53c8SNikolay Aleksandrov 	return 0;
435819e3a9c9SNikolay Aleksandrov }
435919e3a9c9SNikolay Aleksandrov 
436019e3a9c9SNikolay Aleksandrov void br_mdb_hash_fini(struct net_bridge *br)
436119e3a9c9SNikolay Aleksandrov {
4362085b53c8SNikolay Aleksandrov 	rhashtable_destroy(&br->sg_port_tbl);
436319e3a9c9SNikolay Aleksandrov 	rhashtable_destroy(&br->mdb_hash_tbl);
436419e3a9c9SNikolay Aleksandrov }
4365