xref: /openbmc/linux/net/bridge/br_multicast.c (revision 8d21c882aba8004bb996270589c17ba71e100a16)
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);
54754bc547SSatish Ashok static void br_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 
63f12e7d95SNogah Frankel static void __del_port_router(struct net_bridge_port *p);
64bc8c20acSSatish Ashok #if IS_ENABLED(CONFIG_IPV6)
65bc8c20acSSatish Ashok static void br_ip6_multicast_leave_group(struct net_bridge *br,
66bc8c20acSSatish Ashok 					 struct net_bridge_port *port,
67bc8c20acSSatish Ashok 					 const struct in6_addr *group,
686db6f0eaSFelix Fietkau 					 __u16 vid, const unsigned char *src);
69bc8c20acSSatish Ashok #endif
70b0812368SNikolay Aleksandrov static struct net_bridge_port_group *
71b0812368SNikolay Aleksandrov __br_multicast_add_group(struct net_bridge *br,
72b0812368SNikolay Aleksandrov 			 struct net_bridge_port *port,
73b0812368SNikolay Aleksandrov 			 struct br_ip *group,
74b0812368SNikolay Aleksandrov 			 const unsigned char *src,
75b0812368SNikolay Aleksandrov 			 u8 filter_mode,
769116ffbfSNikolay Aleksandrov 			 bool igmpv2_mldv1,
779116ffbfSNikolay Aleksandrov 			 bool blocked);
788266a049SNikolay Aleksandrov static void br_multicast_find_del_pg(struct net_bridge *br,
798266a049SNikolay Aleksandrov 				     struct net_bridge_port_group *pg);
80c83b8fabSHerbert Xu 
81085b53c8SNikolay Aleksandrov static struct net_bridge_port_group *
82085b53c8SNikolay Aleksandrov br_sg_port_find(struct net_bridge *br,
83085b53c8SNikolay Aleksandrov 		struct net_bridge_port_group_sg_key *sg_p)
84085b53c8SNikolay Aleksandrov {
85085b53c8SNikolay Aleksandrov 	lockdep_assert_held_once(&br->multicast_lock);
86085b53c8SNikolay Aleksandrov 
87085b53c8SNikolay Aleksandrov 	return rhashtable_lookup_fast(&br->sg_port_tbl, sg_p,
88085b53c8SNikolay Aleksandrov 				      br_sg_port_rht_params);
89085b53c8SNikolay Aleksandrov }
90085b53c8SNikolay Aleksandrov 
9119e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip_get_rcu(struct net_bridge *br,
92cfd56754SCong Wang 						      struct br_ip *dst)
937f285fa7SHerbert Xu {
9419e3a9c9SNikolay Aleksandrov 	return rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params);
957f285fa7SHerbert Xu }
967f285fa7SHerbert Xu 
9719e3a9c9SNikolay Aleksandrov struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge *br,
9819e3a9c9SNikolay Aleksandrov 					   struct br_ip *dst)
9919e3a9c9SNikolay Aleksandrov {
10019e3a9c9SNikolay Aleksandrov 	struct net_bridge_mdb_entry *ent;
10119e3a9c9SNikolay Aleksandrov 
10219e3a9c9SNikolay Aleksandrov 	lockdep_assert_held_once(&br->multicast_lock);
10319e3a9c9SNikolay Aleksandrov 
10419e3a9c9SNikolay Aleksandrov 	rcu_read_lock();
10519e3a9c9SNikolay Aleksandrov 	ent = rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params);
10619e3a9c9SNikolay Aleksandrov 	rcu_read_unlock();
10719e3a9c9SNikolay Aleksandrov 
10819e3a9c9SNikolay Aleksandrov 	return ent;
10919e3a9c9SNikolay Aleksandrov }
11019e3a9c9SNikolay Aleksandrov 
11119e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip4_get(struct net_bridge *br,
11219e3a9c9SNikolay Aleksandrov 						   __be32 dst, __u16 vid)
113eb1d1641SHerbert Xu {
1148ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip br_dst;
1150821ec55SHerbert Xu 
11619e3a9c9SNikolay Aleksandrov 	memset(&br_dst, 0, sizeof(br_dst));
117eab3227bSNikolay Aleksandrov 	br_dst.dst.ip4 = dst;
1188ef2a9a5SYOSHIFUJI Hideaki 	br_dst.proto = htons(ETH_P_IP);
119b0e9a30dSVlad Yasevich 	br_dst.vid = vid;
1208ef2a9a5SYOSHIFUJI Hideaki 
12119e3a9c9SNikolay Aleksandrov 	return br_mdb_ip_get(br, &br_dst);
1228ef2a9a5SYOSHIFUJI Hideaki }
1238ef2a9a5SYOSHIFUJI Hideaki 
124dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
12519e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip6_get(struct net_bridge *br,
12619e3a9c9SNikolay Aleksandrov 						   const struct in6_addr *dst,
127b0e9a30dSVlad Yasevich 						   __u16 vid)
12808b202b6SYOSHIFUJI Hideaki {
12908b202b6SYOSHIFUJI Hideaki 	struct br_ip br_dst;
13008b202b6SYOSHIFUJI Hideaki 
13119e3a9c9SNikolay Aleksandrov 	memset(&br_dst, 0, sizeof(br_dst));
132eab3227bSNikolay Aleksandrov 	br_dst.dst.ip6 = *dst;
13308b202b6SYOSHIFUJI Hideaki 	br_dst.proto = htons(ETH_P_IPV6);
134b0e9a30dSVlad Yasevich 	br_dst.vid = vid;
13508b202b6SYOSHIFUJI Hideaki 
13619e3a9c9SNikolay Aleksandrov 	return br_mdb_ip_get(br, &br_dst);
13708b202b6SYOSHIFUJI Hideaki }
13808b202b6SYOSHIFUJI Hideaki #endif
13908b202b6SYOSHIFUJI Hideaki 
140eb1d1641SHerbert Xu struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
141fbca58a2SCong Wang 					struct sk_buff *skb, u16 vid)
142eb1d1641SHerbert Xu {
1438ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip ip;
1448ef2a9a5SYOSHIFUJI Hideaki 
14513cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
146eb1d1641SHerbert Xu 		return NULL;
147eb1d1641SHerbert Xu 
1488ef2a9a5SYOSHIFUJI Hideaki 	if (BR_INPUT_SKB_CB(skb)->igmp)
1498ef2a9a5SYOSHIFUJI Hideaki 		return NULL;
1508ef2a9a5SYOSHIFUJI Hideaki 
15119e3a9c9SNikolay Aleksandrov 	memset(&ip, 0, sizeof(ip));
1528ef2a9a5SYOSHIFUJI Hideaki 	ip.proto = skb->protocol;
153fbca58a2SCong Wang 	ip.vid = vid;
1548ef2a9a5SYOSHIFUJI Hideaki 
155eb1d1641SHerbert Xu 	switch (skb->protocol) {
156eb1d1641SHerbert Xu 	case htons(ETH_P_IP):
157eab3227bSNikolay Aleksandrov 		ip.dst.ip4 = ip_hdr(skb)->daddr;
1587d07a68cSNikolay Aleksandrov 		if (br->multicast_igmp_version == 3) {
1597d07a68cSNikolay Aleksandrov 			struct net_bridge_mdb_entry *mdb;
1607d07a68cSNikolay Aleksandrov 
1617d07a68cSNikolay Aleksandrov 			ip.src.ip4 = ip_hdr(skb)->saddr;
1627d07a68cSNikolay Aleksandrov 			mdb = br_mdb_ip_get_rcu(br, &ip);
1637d07a68cSNikolay Aleksandrov 			if (mdb)
1647d07a68cSNikolay Aleksandrov 				return mdb;
1657d07a68cSNikolay Aleksandrov 			ip.src.ip4 = 0;
1667d07a68cSNikolay Aleksandrov 		}
167eb1d1641SHerbert Xu 		break;
168dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
16908b202b6SYOSHIFUJI Hideaki 	case htons(ETH_P_IPV6):
170eab3227bSNikolay Aleksandrov 		ip.dst.ip6 = ipv6_hdr(skb)->daddr;
1717d07a68cSNikolay Aleksandrov 		if (br->multicast_mld_version == 2) {
1727d07a68cSNikolay Aleksandrov 			struct net_bridge_mdb_entry *mdb;
1737d07a68cSNikolay Aleksandrov 
1747d07a68cSNikolay Aleksandrov 			ip.src.ip6 = ipv6_hdr(skb)->saddr;
1757d07a68cSNikolay Aleksandrov 			mdb = br_mdb_ip_get_rcu(br, &ip);
1767d07a68cSNikolay Aleksandrov 			if (mdb)
1777d07a68cSNikolay Aleksandrov 				return mdb;
1787d07a68cSNikolay Aleksandrov 			memset(&ip.src.ip6, 0, sizeof(ip.src.ip6));
1797d07a68cSNikolay Aleksandrov 		}
18008b202b6SYOSHIFUJI Hideaki 		break;
18108b202b6SYOSHIFUJI Hideaki #endif
1828ef2a9a5SYOSHIFUJI Hideaki 	default:
183955062b0SNikolay Aleksandrov 		ip.proto = 0;
184955062b0SNikolay Aleksandrov 		ether_addr_copy(ip.dst.mac_addr, eth_hdr(skb)->h_dest);
185eb1d1641SHerbert Xu 	}
186eb1d1641SHerbert Xu 
18719e3a9c9SNikolay Aleksandrov 	return br_mdb_ip_get_rcu(br, &ip);
188eb1d1641SHerbert Xu }
189eb1d1641SHerbert Xu 
190b0812368SNikolay Aleksandrov static bool br_port_group_equal(struct net_bridge_port_group *p,
191b0812368SNikolay Aleksandrov 				struct net_bridge_port *port,
192b0812368SNikolay Aleksandrov 				const unsigned char *src)
193b0812368SNikolay Aleksandrov {
194b0812368SNikolay Aleksandrov 	if (p->key.port != port)
195b0812368SNikolay Aleksandrov 		return false;
196b0812368SNikolay Aleksandrov 
197b0812368SNikolay Aleksandrov 	if (!(port->flags & BR_MULTICAST_TO_UNICAST))
198b0812368SNikolay Aleksandrov 		return true;
199b0812368SNikolay Aleksandrov 
200b0812368SNikolay Aleksandrov 	return ether_addr_equal(src, p->eth_addr);
201b0812368SNikolay Aleksandrov }
202b0812368SNikolay Aleksandrov 
2038266a049SNikolay Aleksandrov static void __fwd_add_star_excl(struct net_bridge_port_group *pg,
2048266a049SNikolay Aleksandrov 				struct br_ip *sg_ip)
2058266a049SNikolay Aleksandrov {
2068266a049SNikolay Aleksandrov 	struct net_bridge_port_group_sg_key sg_key;
2078266a049SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
2088266a049SNikolay Aleksandrov 	struct net_bridge_port_group *src_pg;
2098266a049SNikolay Aleksandrov 
2108266a049SNikolay Aleksandrov 	memset(&sg_key, 0, sizeof(sg_key));
2118266a049SNikolay Aleksandrov 	sg_key.port = pg->key.port;
2128266a049SNikolay Aleksandrov 	sg_key.addr = *sg_ip;
2138266a049SNikolay Aleksandrov 	if (br_sg_port_find(br, &sg_key))
2148266a049SNikolay Aleksandrov 		return;
2158266a049SNikolay Aleksandrov 
2168266a049SNikolay Aleksandrov 	src_pg = __br_multicast_add_group(br, pg->key.port, sg_ip, pg->eth_addr,
2179116ffbfSNikolay Aleksandrov 					  MCAST_INCLUDE, false, false);
2188266a049SNikolay Aleksandrov 	if (IS_ERR_OR_NULL(src_pg) ||
2198266a049SNikolay Aleksandrov 	    src_pg->rt_protocol != RTPROT_KERNEL)
2208266a049SNikolay Aleksandrov 		return;
2218266a049SNikolay Aleksandrov 
2228266a049SNikolay Aleksandrov 	src_pg->flags |= MDB_PG_FLAGS_STAR_EXCL;
2238266a049SNikolay Aleksandrov }
2248266a049SNikolay Aleksandrov 
2258266a049SNikolay Aleksandrov static void __fwd_del_star_excl(struct net_bridge_port_group *pg,
2268266a049SNikolay Aleksandrov 				struct br_ip *sg_ip)
2278266a049SNikolay Aleksandrov {
2288266a049SNikolay Aleksandrov 	struct net_bridge_port_group_sg_key sg_key;
2298266a049SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
2308266a049SNikolay Aleksandrov 	struct net_bridge_port_group *src_pg;
2318266a049SNikolay Aleksandrov 
2328266a049SNikolay Aleksandrov 	memset(&sg_key, 0, sizeof(sg_key));
2338266a049SNikolay Aleksandrov 	sg_key.port = pg->key.port;
2348266a049SNikolay Aleksandrov 	sg_key.addr = *sg_ip;
2358266a049SNikolay Aleksandrov 	src_pg = br_sg_port_find(br, &sg_key);
2368266a049SNikolay Aleksandrov 	if (!src_pg || !(src_pg->flags & MDB_PG_FLAGS_STAR_EXCL) ||
2378266a049SNikolay Aleksandrov 	    src_pg->rt_protocol != RTPROT_KERNEL)
2388266a049SNikolay Aleksandrov 		return;
2398266a049SNikolay Aleksandrov 
2408266a049SNikolay Aleksandrov 	br_multicast_find_del_pg(br, src_pg);
2418266a049SNikolay Aleksandrov }
2428266a049SNikolay Aleksandrov 
2438266a049SNikolay Aleksandrov /* When a port group transitions to (or is added as) EXCLUDE we need to add it
2448266a049SNikolay Aleksandrov  * to all other ports' S,G entries which are not blocked by the current group
2458266a049SNikolay Aleksandrov  * for proper replication, the assumption is that any S,G blocked entries
2468266a049SNikolay Aleksandrov  * are already added so the S,G,port lookup should skip them.
2478266a049SNikolay Aleksandrov  * When a port group transitions from EXCLUDE -> INCLUDE mode or is being
2488266a049SNikolay Aleksandrov  * deleted we need to remove it from all ports' S,G entries where it was
2498266a049SNikolay Aleksandrov  * automatically installed before (i.e. where it's MDB_PG_FLAGS_STAR_EXCL).
2508266a049SNikolay Aleksandrov  */
2518266a049SNikolay Aleksandrov void br_multicast_star_g_handle_mode(struct net_bridge_port_group *pg,
2528266a049SNikolay Aleksandrov 				     u8 filter_mode)
2538266a049SNikolay Aleksandrov {
2548266a049SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
2558266a049SNikolay Aleksandrov 	struct net_bridge_port_group *pg_lst;
2568266a049SNikolay Aleksandrov 	struct net_bridge_mdb_entry *mp;
2578266a049SNikolay Aleksandrov 	struct br_ip sg_ip;
2588266a049SNikolay Aleksandrov 
2598266a049SNikolay Aleksandrov 	if (WARN_ON(!br_multicast_is_star_g(&pg->key.addr)))
2608266a049SNikolay Aleksandrov 		return;
2618266a049SNikolay Aleksandrov 
2628266a049SNikolay Aleksandrov 	mp = br_mdb_ip_get(br, &pg->key.addr);
2638266a049SNikolay Aleksandrov 	if (!mp)
2648266a049SNikolay Aleksandrov 		return;
2658266a049SNikolay Aleksandrov 
2668266a049SNikolay Aleksandrov 	memset(&sg_ip, 0, sizeof(sg_ip));
2678266a049SNikolay Aleksandrov 	sg_ip = pg->key.addr;
2688266a049SNikolay Aleksandrov 	for (pg_lst = mlock_dereference(mp->ports, br);
2698266a049SNikolay Aleksandrov 	     pg_lst;
2708266a049SNikolay Aleksandrov 	     pg_lst = mlock_dereference(pg_lst->next, br)) {
2718266a049SNikolay Aleksandrov 		struct net_bridge_group_src *src_ent;
2728266a049SNikolay Aleksandrov 
2738266a049SNikolay Aleksandrov 		if (pg_lst == pg)
2748266a049SNikolay Aleksandrov 			continue;
2758266a049SNikolay Aleksandrov 		hlist_for_each_entry(src_ent, &pg_lst->src_list, node) {
2768266a049SNikolay Aleksandrov 			if (!(src_ent->flags & BR_SGRP_F_INSTALLED))
2778266a049SNikolay Aleksandrov 				continue;
2788266a049SNikolay Aleksandrov 			sg_ip.src = src_ent->addr.src;
2798266a049SNikolay Aleksandrov 			switch (filter_mode) {
2808266a049SNikolay Aleksandrov 			case MCAST_INCLUDE:
2818266a049SNikolay Aleksandrov 				__fwd_del_star_excl(pg, &sg_ip);
2828266a049SNikolay Aleksandrov 				break;
2838266a049SNikolay Aleksandrov 			case MCAST_EXCLUDE:
2848266a049SNikolay Aleksandrov 				__fwd_add_star_excl(pg, &sg_ip);
2858266a049SNikolay Aleksandrov 				break;
2868266a049SNikolay Aleksandrov 			}
2878266a049SNikolay Aleksandrov 		}
2888266a049SNikolay Aleksandrov 	}
2898266a049SNikolay Aleksandrov }
2908266a049SNikolay Aleksandrov 
291094b82fdSNikolay Aleksandrov /* called when adding a new S,G with host_joined == false by default */
292094b82fdSNikolay Aleksandrov static void br_multicast_sg_host_state(struct net_bridge_mdb_entry *star_mp,
293094b82fdSNikolay Aleksandrov 				       struct net_bridge_port_group *sg)
294094b82fdSNikolay Aleksandrov {
295094b82fdSNikolay Aleksandrov 	struct net_bridge_mdb_entry *sg_mp;
296094b82fdSNikolay Aleksandrov 
297094b82fdSNikolay Aleksandrov 	if (WARN_ON(!br_multicast_is_star_g(&star_mp->addr)))
298094b82fdSNikolay Aleksandrov 		return;
299094b82fdSNikolay Aleksandrov 	if (!star_mp->host_joined)
300094b82fdSNikolay Aleksandrov 		return;
301094b82fdSNikolay Aleksandrov 
302094b82fdSNikolay Aleksandrov 	sg_mp = br_mdb_ip_get(star_mp->br, &sg->key.addr);
303094b82fdSNikolay Aleksandrov 	if (!sg_mp)
304094b82fdSNikolay Aleksandrov 		return;
305094b82fdSNikolay Aleksandrov 	sg_mp->host_joined = true;
306094b82fdSNikolay Aleksandrov }
307094b82fdSNikolay Aleksandrov 
308094b82fdSNikolay Aleksandrov /* set the host_joined state of all of *,G's S,G entries */
309094b82fdSNikolay Aleksandrov static void br_multicast_star_g_host_state(struct net_bridge_mdb_entry *star_mp)
310094b82fdSNikolay Aleksandrov {
311094b82fdSNikolay Aleksandrov 	struct net_bridge *br = star_mp->br;
312094b82fdSNikolay Aleksandrov 	struct net_bridge_mdb_entry *sg_mp;
313094b82fdSNikolay Aleksandrov 	struct net_bridge_port_group *pg;
314094b82fdSNikolay Aleksandrov 	struct br_ip sg_ip;
315094b82fdSNikolay Aleksandrov 
316094b82fdSNikolay Aleksandrov 	if (WARN_ON(!br_multicast_is_star_g(&star_mp->addr)))
317094b82fdSNikolay Aleksandrov 		return;
318094b82fdSNikolay Aleksandrov 
319094b82fdSNikolay Aleksandrov 	memset(&sg_ip, 0, sizeof(sg_ip));
320094b82fdSNikolay Aleksandrov 	sg_ip = star_mp->addr;
321094b82fdSNikolay Aleksandrov 	for (pg = mlock_dereference(star_mp->ports, br);
322094b82fdSNikolay Aleksandrov 	     pg;
323094b82fdSNikolay Aleksandrov 	     pg = mlock_dereference(pg->next, br)) {
324094b82fdSNikolay Aleksandrov 		struct net_bridge_group_src *src_ent;
325094b82fdSNikolay Aleksandrov 
326094b82fdSNikolay Aleksandrov 		hlist_for_each_entry(src_ent, &pg->src_list, node) {
327094b82fdSNikolay Aleksandrov 			if (!(src_ent->flags & BR_SGRP_F_INSTALLED))
328094b82fdSNikolay Aleksandrov 				continue;
329094b82fdSNikolay Aleksandrov 			sg_ip.src = src_ent->addr.src;
330094b82fdSNikolay Aleksandrov 			sg_mp = br_mdb_ip_get(br, &sg_ip);
331094b82fdSNikolay Aleksandrov 			if (!sg_mp)
332094b82fdSNikolay Aleksandrov 				continue;
333094b82fdSNikolay Aleksandrov 			sg_mp->host_joined = star_mp->host_joined;
334094b82fdSNikolay Aleksandrov 		}
335094b82fdSNikolay Aleksandrov 	}
336094b82fdSNikolay Aleksandrov }
337094b82fdSNikolay Aleksandrov 
3388266a049SNikolay Aleksandrov static void br_multicast_sg_del_exclude_ports(struct net_bridge_mdb_entry *sgmp)
3398266a049SNikolay Aleksandrov {
3408266a049SNikolay Aleksandrov 	struct net_bridge_port_group __rcu **pp;
3418266a049SNikolay Aleksandrov 	struct net_bridge_port_group *p;
3428266a049SNikolay Aleksandrov 
3438266a049SNikolay Aleksandrov 	/* *,G exclude ports are only added to S,G entries */
3448266a049SNikolay Aleksandrov 	if (WARN_ON(br_multicast_is_star_g(&sgmp->addr)))
3458266a049SNikolay Aleksandrov 		return;
3468266a049SNikolay Aleksandrov 
3478266a049SNikolay Aleksandrov 	/* we need the STAR_EXCLUDE ports if there are non-STAR_EXCLUDE ports
3488266a049SNikolay Aleksandrov 	 * we should ignore perm entries since they're managed by user-space
3498266a049SNikolay Aleksandrov 	 */
3508266a049SNikolay Aleksandrov 	for (pp = &sgmp->ports;
3518266a049SNikolay Aleksandrov 	     (p = mlock_dereference(*pp, sgmp->br)) != NULL;
3528266a049SNikolay Aleksandrov 	     pp = &p->next)
3538266a049SNikolay Aleksandrov 		if (!(p->flags & (MDB_PG_FLAGS_STAR_EXCL |
3548266a049SNikolay Aleksandrov 				  MDB_PG_FLAGS_PERMANENT)))
3558266a049SNikolay Aleksandrov 			return;
3568266a049SNikolay Aleksandrov 
357094b82fdSNikolay Aleksandrov 	/* currently the host can only have joined the *,G which means
358094b82fdSNikolay Aleksandrov 	 * we treat it as EXCLUDE {}, so for an S,G it's considered a
359094b82fdSNikolay Aleksandrov 	 * STAR_EXCLUDE entry and we can safely leave it
360094b82fdSNikolay Aleksandrov 	 */
361094b82fdSNikolay Aleksandrov 	sgmp->host_joined = false;
362094b82fdSNikolay Aleksandrov 
3638266a049SNikolay Aleksandrov 	for (pp = &sgmp->ports;
3648266a049SNikolay Aleksandrov 	     (p = mlock_dereference(*pp, sgmp->br)) != NULL;) {
3658266a049SNikolay Aleksandrov 		if (!(p->flags & MDB_PG_FLAGS_PERMANENT))
3668266a049SNikolay Aleksandrov 			br_multicast_del_pg(sgmp, p, pp);
3678266a049SNikolay Aleksandrov 		else
3688266a049SNikolay Aleksandrov 			pp = &p->next;
3698266a049SNikolay Aleksandrov 	}
3708266a049SNikolay Aleksandrov }
3718266a049SNikolay Aleksandrov 
3728266a049SNikolay Aleksandrov void br_multicast_sg_add_exclude_ports(struct net_bridge_mdb_entry *star_mp,
3738266a049SNikolay Aleksandrov 				       struct net_bridge_port_group *sg)
3748266a049SNikolay Aleksandrov {
3758266a049SNikolay Aleksandrov 	struct net_bridge_port_group_sg_key sg_key;
3768266a049SNikolay Aleksandrov 	struct net_bridge *br = star_mp->br;
3778266a049SNikolay Aleksandrov 	struct net_bridge_port_group *pg;
3788266a049SNikolay Aleksandrov 
3798266a049SNikolay Aleksandrov 	if (WARN_ON(br_multicast_is_star_g(&sg->key.addr)))
3808266a049SNikolay Aleksandrov 		return;
3818266a049SNikolay Aleksandrov 	if (WARN_ON(!br_multicast_is_star_g(&star_mp->addr)))
3828266a049SNikolay Aleksandrov 		return;
3838266a049SNikolay Aleksandrov 
384094b82fdSNikolay Aleksandrov 	br_multicast_sg_host_state(star_mp, sg);
3858266a049SNikolay Aleksandrov 	memset(&sg_key, 0, sizeof(sg_key));
3868266a049SNikolay Aleksandrov 	sg_key.addr = sg->key.addr;
3878266a049SNikolay Aleksandrov 	/* we need to add all exclude ports to the S,G */
3888266a049SNikolay Aleksandrov 	for (pg = mlock_dereference(star_mp->ports, br);
3898266a049SNikolay Aleksandrov 	     pg;
3908266a049SNikolay Aleksandrov 	     pg = mlock_dereference(pg->next, br)) {
3918266a049SNikolay Aleksandrov 		struct net_bridge_port_group *src_pg;
3928266a049SNikolay Aleksandrov 
3938266a049SNikolay Aleksandrov 		if (pg == sg || pg->filter_mode == MCAST_INCLUDE)
3948266a049SNikolay Aleksandrov 			continue;
3958266a049SNikolay Aleksandrov 
3968266a049SNikolay Aleksandrov 		sg_key.port = pg->key.port;
3978266a049SNikolay Aleksandrov 		if (br_sg_port_find(br, &sg_key))
3988266a049SNikolay Aleksandrov 			continue;
3998266a049SNikolay Aleksandrov 
4008266a049SNikolay Aleksandrov 		src_pg = __br_multicast_add_group(br, pg->key.port,
4018266a049SNikolay Aleksandrov 						  &sg->key.addr,
4028266a049SNikolay Aleksandrov 						  sg->eth_addr,
4039116ffbfSNikolay Aleksandrov 						  MCAST_INCLUDE, false, false);
4048266a049SNikolay Aleksandrov 		if (IS_ERR_OR_NULL(src_pg) ||
4058266a049SNikolay Aleksandrov 		    src_pg->rt_protocol != RTPROT_KERNEL)
4068266a049SNikolay Aleksandrov 			continue;
4078266a049SNikolay Aleksandrov 		src_pg->flags |= MDB_PG_FLAGS_STAR_EXCL;
4088266a049SNikolay Aleksandrov 	}
4098266a049SNikolay Aleksandrov }
4108266a049SNikolay Aleksandrov 
411b0812368SNikolay Aleksandrov static void br_multicast_fwd_src_add(struct net_bridge_group_src *src)
412b0812368SNikolay Aleksandrov {
4138266a049SNikolay Aleksandrov 	struct net_bridge_mdb_entry *star_mp;
414b0812368SNikolay Aleksandrov 	struct net_bridge_port_group *sg;
415b0812368SNikolay Aleksandrov 	struct br_ip sg_ip;
416b0812368SNikolay Aleksandrov 
417b0812368SNikolay Aleksandrov 	if (src->flags & BR_SGRP_F_INSTALLED)
418b0812368SNikolay Aleksandrov 		return;
419b0812368SNikolay Aleksandrov 
420b0812368SNikolay Aleksandrov 	memset(&sg_ip, 0, sizeof(sg_ip));
421b0812368SNikolay Aleksandrov 	sg_ip = src->pg->key.addr;
422b0812368SNikolay Aleksandrov 	sg_ip.src = src->addr.src;
423b0812368SNikolay Aleksandrov 	sg = __br_multicast_add_group(src->br, src->pg->key.port, &sg_ip,
4249116ffbfSNikolay Aleksandrov 				      src->pg->eth_addr, MCAST_INCLUDE, false,
4259116ffbfSNikolay Aleksandrov 				      !timer_pending(&src->timer));
426b0812368SNikolay Aleksandrov 	if (IS_ERR_OR_NULL(sg))
427b0812368SNikolay Aleksandrov 		return;
428b0812368SNikolay Aleksandrov 	src->flags |= BR_SGRP_F_INSTALLED;
4298266a049SNikolay Aleksandrov 	sg->flags &= ~MDB_PG_FLAGS_STAR_EXCL;
430b0812368SNikolay Aleksandrov 
431b0812368SNikolay Aleksandrov 	/* if it was added by user-space as perm we can skip next steps */
432b0812368SNikolay Aleksandrov 	if (sg->rt_protocol != RTPROT_KERNEL &&
433b0812368SNikolay Aleksandrov 	    (sg->flags & MDB_PG_FLAGS_PERMANENT))
434b0812368SNikolay Aleksandrov 		return;
435b0812368SNikolay Aleksandrov 
436b0812368SNikolay Aleksandrov 	/* the kernel is now responsible for removing this S,G */
437b0812368SNikolay Aleksandrov 	del_timer(&sg->timer);
4388266a049SNikolay Aleksandrov 	star_mp = br_mdb_ip_get(src->br, &src->pg->key.addr);
4398266a049SNikolay Aleksandrov 	if (!star_mp)
4408266a049SNikolay Aleksandrov 		return;
4418266a049SNikolay Aleksandrov 
4428266a049SNikolay Aleksandrov 	br_multicast_sg_add_exclude_ports(star_mp, sg);
443b0812368SNikolay Aleksandrov }
444b0812368SNikolay Aleksandrov 
445d5a10222SNikolay Aleksandrov static void br_multicast_fwd_src_remove(struct net_bridge_group_src *src,
446d5a10222SNikolay Aleksandrov 					bool fastleave)
447b0812368SNikolay Aleksandrov {
448b0812368SNikolay Aleksandrov 	struct net_bridge_port_group *p, *pg = src->pg;
449b0812368SNikolay Aleksandrov 	struct net_bridge_port_group __rcu **pp;
450b0812368SNikolay Aleksandrov 	struct net_bridge_mdb_entry *mp;
451b0812368SNikolay Aleksandrov 	struct br_ip sg_ip;
452b0812368SNikolay Aleksandrov 
453b0812368SNikolay Aleksandrov 	memset(&sg_ip, 0, sizeof(sg_ip));
454b0812368SNikolay Aleksandrov 	sg_ip = pg->key.addr;
455b0812368SNikolay Aleksandrov 	sg_ip.src = src->addr.src;
456b0812368SNikolay Aleksandrov 
457b0812368SNikolay Aleksandrov 	mp = br_mdb_ip_get(src->br, &sg_ip);
458b0812368SNikolay Aleksandrov 	if (!mp)
459b0812368SNikolay Aleksandrov 		return;
460b0812368SNikolay Aleksandrov 
461b0812368SNikolay Aleksandrov 	for (pp = &mp->ports;
462b0812368SNikolay Aleksandrov 	     (p = mlock_dereference(*pp, src->br)) != NULL;
463b0812368SNikolay Aleksandrov 	     pp = &p->next) {
464b0812368SNikolay Aleksandrov 		if (!br_port_group_equal(p, pg->key.port, pg->eth_addr))
465b0812368SNikolay Aleksandrov 			continue;
466b0812368SNikolay Aleksandrov 
467b0812368SNikolay Aleksandrov 		if (p->rt_protocol != RTPROT_KERNEL &&
468b0812368SNikolay Aleksandrov 		    (p->flags & MDB_PG_FLAGS_PERMANENT))
469b0812368SNikolay Aleksandrov 			break;
470b0812368SNikolay Aleksandrov 
471d5a10222SNikolay Aleksandrov 		if (fastleave)
472d5a10222SNikolay Aleksandrov 			p->flags |= MDB_PG_FLAGS_FAST_LEAVE;
473b0812368SNikolay Aleksandrov 		br_multicast_del_pg(mp, p, pp);
474b0812368SNikolay Aleksandrov 		break;
475b0812368SNikolay Aleksandrov 	}
476b0812368SNikolay Aleksandrov 	src->flags &= ~BR_SGRP_F_INSTALLED;
477b0812368SNikolay Aleksandrov }
478b0812368SNikolay Aleksandrov 
4799116ffbfSNikolay Aleksandrov /* install S,G and based on src's timer enable or disable forwarding */
480b0812368SNikolay Aleksandrov static void br_multicast_fwd_src_handle(struct net_bridge_group_src *src)
481b0812368SNikolay Aleksandrov {
4829116ffbfSNikolay Aleksandrov 	struct net_bridge_port_group_sg_key sg_key;
4839116ffbfSNikolay Aleksandrov 	struct net_bridge_port_group *sg;
4849116ffbfSNikolay Aleksandrov 	u8 old_flags;
4859116ffbfSNikolay Aleksandrov 
486b0812368SNikolay Aleksandrov 	br_multicast_fwd_src_add(src);
4879116ffbfSNikolay Aleksandrov 
4889116ffbfSNikolay Aleksandrov 	memset(&sg_key, 0, sizeof(sg_key));
4899116ffbfSNikolay Aleksandrov 	sg_key.addr = src->pg->key.addr;
4909116ffbfSNikolay Aleksandrov 	sg_key.addr.src = src->addr.src;
4919116ffbfSNikolay Aleksandrov 	sg_key.port = src->pg->key.port;
4929116ffbfSNikolay Aleksandrov 
4939116ffbfSNikolay Aleksandrov 	sg = br_sg_port_find(src->br, &sg_key);
4949116ffbfSNikolay Aleksandrov 	if (!sg || (sg->flags & MDB_PG_FLAGS_PERMANENT))
4959116ffbfSNikolay Aleksandrov 		return;
4969116ffbfSNikolay Aleksandrov 
4979116ffbfSNikolay Aleksandrov 	old_flags = sg->flags;
4989116ffbfSNikolay Aleksandrov 	if (timer_pending(&src->timer))
4999116ffbfSNikolay Aleksandrov 		sg->flags &= ~MDB_PG_FLAGS_BLOCKED;
5009116ffbfSNikolay Aleksandrov 	else
5019116ffbfSNikolay Aleksandrov 		sg->flags |= MDB_PG_FLAGS_BLOCKED;
5029116ffbfSNikolay Aleksandrov 
5039116ffbfSNikolay Aleksandrov 	if (old_flags != sg->flags) {
5049116ffbfSNikolay Aleksandrov 		struct net_bridge_mdb_entry *sg_mp;
5059116ffbfSNikolay Aleksandrov 
5069116ffbfSNikolay Aleksandrov 		sg_mp = br_mdb_ip_get(src->br, &sg_key.addr);
5079116ffbfSNikolay Aleksandrov 		if (!sg_mp)
5089116ffbfSNikolay Aleksandrov 			return;
5099116ffbfSNikolay Aleksandrov 		br_mdb_notify(src->br->dev, sg_mp, sg, RTM_NEWMDB);
5109116ffbfSNikolay Aleksandrov 	}
511b0812368SNikolay Aleksandrov }
512b0812368SNikolay Aleksandrov 
513e12cec65SNikolay Aleksandrov static void br_multicast_destroy_mdb_entry(struct net_bridge_mcast_gc *gc)
514e12cec65SNikolay Aleksandrov {
515e12cec65SNikolay Aleksandrov 	struct net_bridge_mdb_entry *mp;
516e12cec65SNikolay Aleksandrov 
517e12cec65SNikolay Aleksandrov 	mp = container_of(gc, struct net_bridge_mdb_entry, mcast_gc);
518e12cec65SNikolay Aleksandrov 	WARN_ON(!hlist_unhashed(&mp->mdb_node));
519e12cec65SNikolay Aleksandrov 	WARN_ON(mp->ports);
520e12cec65SNikolay Aleksandrov 
521e12cec65SNikolay Aleksandrov 	del_timer_sync(&mp->timer);
522e12cec65SNikolay Aleksandrov 	kfree_rcu(mp, rcu);
523e12cec65SNikolay Aleksandrov }
524e12cec65SNikolay Aleksandrov 
525e12cec65SNikolay Aleksandrov static void br_multicast_del_mdb_entry(struct net_bridge_mdb_entry *mp)
526e12cec65SNikolay Aleksandrov {
527e12cec65SNikolay Aleksandrov 	struct net_bridge *br = mp->br;
528e12cec65SNikolay Aleksandrov 
529e12cec65SNikolay Aleksandrov 	rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode,
530e12cec65SNikolay Aleksandrov 			       br_mdb_rht_params);
531e12cec65SNikolay Aleksandrov 	hlist_del_init_rcu(&mp->mdb_node);
532e12cec65SNikolay Aleksandrov 	hlist_add_head(&mp->mcast_gc.gc_node, &br->mcast_gc_list);
533e12cec65SNikolay Aleksandrov 	queue_work(system_long_wq, &br->mcast_gc_work);
534e12cec65SNikolay Aleksandrov }
535e12cec65SNikolay Aleksandrov 
53688c1f37fSAllen Pais static void br_multicast_group_expired(struct timer_list *t)
537eb1d1641SHerbert Xu {
53888c1f37fSAllen Pais 	struct net_bridge_mdb_entry *mp = from_timer(mp, t, timer);
539eb1d1641SHerbert Xu 	struct net_bridge *br = mp->br;
540eb1d1641SHerbert Xu 
541eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
542b0812368SNikolay Aleksandrov 	if (hlist_unhashed(&mp->mdb_node) || !netif_running(br->dev) ||
543b0812368SNikolay Aleksandrov 	    timer_pending(&mp->timer))
544eb1d1641SHerbert Xu 		goto out;
545eb1d1641SHerbert Xu 
5461bc844eeSNikolay Aleksandrov 	br_multicast_host_leave(mp, true);
547eb1d1641SHerbert Xu 
548eb1d1641SHerbert Xu 	if (mp->ports)
549eb1d1641SHerbert Xu 		goto out;
550e12cec65SNikolay Aleksandrov 	br_multicast_del_mdb_entry(mp);
551eb1d1641SHerbert Xu out:
552eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
553eb1d1641SHerbert Xu }
554eb1d1641SHerbert Xu 
555e12cec65SNikolay Aleksandrov static void br_multicast_destroy_group_src(struct net_bridge_mcast_gc *gc)
556e12cec65SNikolay Aleksandrov {
557e12cec65SNikolay Aleksandrov 	struct net_bridge_group_src *src;
558e12cec65SNikolay Aleksandrov 
559e12cec65SNikolay Aleksandrov 	src = container_of(gc, struct net_bridge_group_src, mcast_gc);
560e12cec65SNikolay Aleksandrov 	WARN_ON(!hlist_unhashed(&src->node));
561e12cec65SNikolay Aleksandrov 
562e12cec65SNikolay Aleksandrov 	del_timer_sync(&src->timer);
563e12cec65SNikolay Aleksandrov 	kfree_rcu(src, rcu);
564e12cec65SNikolay Aleksandrov }
565e12cec65SNikolay Aleksandrov 
566d5a10222SNikolay Aleksandrov void br_multicast_del_group_src(struct net_bridge_group_src *src,
567d5a10222SNikolay Aleksandrov 				bool fastleave)
5688b671779SNikolay Aleksandrov {
569085b53c8SNikolay Aleksandrov 	struct net_bridge *br = src->pg->key.port->br;
5708b671779SNikolay Aleksandrov 
571d5a10222SNikolay Aleksandrov 	br_multicast_fwd_src_remove(src, fastleave);
5728b671779SNikolay Aleksandrov 	hlist_del_init_rcu(&src->node);
5738b671779SNikolay Aleksandrov 	src->pg->src_ents--;
574e12cec65SNikolay Aleksandrov 	hlist_add_head(&src->mcast_gc.gc_node, &br->mcast_gc_list);
575e12cec65SNikolay Aleksandrov 	queue_work(system_long_wq, &br->mcast_gc_work);
576e12cec65SNikolay Aleksandrov }
577e12cec65SNikolay Aleksandrov 
578e12cec65SNikolay Aleksandrov static void br_multicast_destroy_port_group(struct net_bridge_mcast_gc *gc)
579e12cec65SNikolay Aleksandrov {
580e12cec65SNikolay Aleksandrov 	struct net_bridge_port_group *pg;
581e12cec65SNikolay Aleksandrov 
582e12cec65SNikolay Aleksandrov 	pg = container_of(gc, struct net_bridge_port_group, mcast_gc);
583e12cec65SNikolay Aleksandrov 	WARN_ON(!hlist_unhashed(&pg->mglist));
584e12cec65SNikolay Aleksandrov 	WARN_ON(!hlist_empty(&pg->src_list));
585e12cec65SNikolay Aleksandrov 
586e12cec65SNikolay Aleksandrov 	del_timer_sync(&pg->rexmit_timer);
587e12cec65SNikolay Aleksandrov 	del_timer_sync(&pg->timer);
588e12cec65SNikolay Aleksandrov 	kfree_rcu(pg, rcu);
5898b671779SNikolay Aleksandrov }
5908b671779SNikolay Aleksandrov 
591681590bdSNikolay Aleksandrov void br_multicast_del_pg(struct net_bridge_mdb_entry *mp,
592681590bdSNikolay Aleksandrov 			 struct net_bridge_port_group *pg,
593681590bdSNikolay Aleksandrov 			 struct net_bridge_port_group __rcu **pp)
594681590bdSNikolay Aleksandrov {
595085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
5968b671779SNikolay Aleksandrov 	struct net_bridge_group_src *ent;
5978b671779SNikolay Aleksandrov 	struct hlist_node *tmp;
598681590bdSNikolay Aleksandrov 
599681590bdSNikolay Aleksandrov 	rcu_assign_pointer(*pp, pg->next);
600681590bdSNikolay Aleksandrov 	hlist_del_init(&pg->mglist);
601baa74d39SNikolay Aleksandrov 	br_multicast_eht_clean_sets(pg);
6028b671779SNikolay Aleksandrov 	hlist_for_each_entry_safe(ent, tmp, &pg->src_list, node)
603d5a10222SNikolay Aleksandrov 		br_multicast_del_group_src(ent, false);
60481f19838SNikolay Aleksandrov 	br_mdb_notify(br->dev, mp, pg, RTM_DELMDB);
60574705582SNikolay Aleksandrov 	if (!br_multicast_is_star_g(&mp->addr)) {
60674705582SNikolay Aleksandrov 		rhashtable_remove_fast(&br->sg_port_tbl, &pg->rhnode,
60774705582SNikolay Aleksandrov 				       br_sg_port_rht_params);
6088266a049SNikolay Aleksandrov 		br_multicast_sg_del_exclude_ports(mp);
60974705582SNikolay Aleksandrov 	} else {
6108266a049SNikolay Aleksandrov 		br_multicast_star_g_handle_mode(pg, MCAST_INCLUDE);
61174705582SNikolay Aleksandrov 	}
612e12cec65SNikolay Aleksandrov 	hlist_add_head(&pg->mcast_gc.gc_node, &br->mcast_gc_list);
613e12cec65SNikolay Aleksandrov 	queue_work(system_long_wq, &br->mcast_gc_work);
614681590bdSNikolay Aleksandrov 
615681590bdSNikolay Aleksandrov 	if (!mp->ports && !mp->host_joined && netif_running(br->dev))
616681590bdSNikolay Aleksandrov 		mod_timer(&mp->timer, jiffies);
617681590bdSNikolay Aleksandrov }
618681590bdSNikolay Aleksandrov 
619681590bdSNikolay Aleksandrov static void br_multicast_find_del_pg(struct net_bridge *br,
620eb1d1641SHerbert Xu 				     struct net_bridge_port_group *pg)
621eb1d1641SHerbert Xu {
6228b671779SNikolay Aleksandrov 	struct net_bridge_port_group __rcu **pp;
623eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
624eb1d1641SHerbert Xu 	struct net_bridge_port_group *p;
625e8051688SEric Dumazet 
626085b53c8SNikolay Aleksandrov 	mp = br_mdb_ip_get(br, &pg->key.addr);
627eb1d1641SHerbert Xu 	if (WARN_ON(!mp))
628eb1d1641SHerbert Xu 		return;
629eb1d1641SHerbert Xu 
630e8051688SEric Dumazet 	for (pp = &mp->ports;
631e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
632e8051688SEric Dumazet 	     pp = &p->next) {
633eb1d1641SHerbert Xu 		if (p != pg)
634eb1d1641SHerbert Xu 			continue;
635eb1d1641SHerbert Xu 
636681590bdSNikolay Aleksandrov 		br_multicast_del_pg(mp, pg, pp);
637eb1d1641SHerbert Xu 		return;
638eb1d1641SHerbert Xu 	}
639eb1d1641SHerbert Xu 
640eb1d1641SHerbert Xu 	WARN_ON(1);
641eb1d1641SHerbert Xu }
642eb1d1641SHerbert Xu 
64388c1f37fSAllen Pais static void br_multicast_port_group_expired(struct timer_list *t)
644eb1d1641SHerbert Xu {
64588c1f37fSAllen Pais 	struct net_bridge_port_group *pg = from_timer(pg, t, timer);
646d6c33d67SNikolay Aleksandrov 	struct net_bridge_group_src *src_ent;
647085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
648d6c33d67SNikolay Aleksandrov 	struct hlist_node *tmp;
649d6c33d67SNikolay Aleksandrov 	bool changed;
650eb1d1641SHerbert Xu 
651eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
652eb1d1641SHerbert Xu 	if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
6539d06b6d8SElad Raz 	    hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT)
654eb1d1641SHerbert Xu 		goto out;
655eb1d1641SHerbert Xu 
656d6c33d67SNikolay Aleksandrov 	changed = !!(pg->filter_mode == MCAST_EXCLUDE);
657d6c33d67SNikolay Aleksandrov 	pg->filter_mode = MCAST_INCLUDE;
658d6c33d67SNikolay Aleksandrov 	hlist_for_each_entry_safe(src_ent, tmp, &pg->src_list, node) {
659d6c33d67SNikolay Aleksandrov 		if (!timer_pending(&src_ent->timer)) {
660d5a10222SNikolay Aleksandrov 			br_multicast_del_group_src(src_ent, false);
661d6c33d67SNikolay Aleksandrov 			changed = true;
662d6c33d67SNikolay Aleksandrov 		}
663d6c33d67SNikolay Aleksandrov 	}
664eb1d1641SHerbert Xu 
665d6c33d67SNikolay Aleksandrov 	if (hlist_empty(&pg->src_list)) {
666d6c33d67SNikolay Aleksandrov 		br_multicast_find_del_pg(br, pg);
667d6c33d67SNikolay Aleksandrov 	} else if (changed) {
668085b53c8SNikolay Aleksandrov 		struct net_bridge_mdb_entry *mp = br_mdb_ip_get(br, &pg->key.addr);
669d6c33d67SNikolay Aleksandrov 
6708266a049SNikolay Aleksandrov 		if (changed && br_multicast_is_star_g(&pg->key.addr))
6718266a049SNikolay Aleksandrov 			br_multicast_star_g_handle_mode(pg, MCAST_INCLUDE);
6728266a049SNikolay Aleksandrov 
673d6c33d67SNikolay Aleksandrov 		if (WARN_ON(!mp))
674d6c33d67SNikolay Aleksandrov 			goto out;
675d6c33d67SNikolay Aleksandrov 		br_mdb_notify(br->dev, mp, pg, RTM_NEWMDB);
676d6c33d67SNikolay Aleksandrov 	}
677eb1d1641SHerbert Xu out:
678eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
679eb1d1641SHerbert Xu }
680eb1d1641SHerbert Xu 
681e12cec65SNikolay Aleksandrov static void br_multicast_gc(struct hlist_head *head)
682e12cec65SNikolay Aleksandrov {
683e12cec65SNikolay Aleksandrov 	struct net_bridge_mcast_gc *gcent;
684e12cec65SNikolay Aleksandrov 	struct hlist_node *tmp;
685e12cec65SNikolay Aleksandrov 
686e12cec65SNikolay Aleksandrov 	hlist_for_each_entry_safe(gcent, tmp, head, gc_node) {
687e12cec65SNikolay Aleksandrov 		hlist_del_init(&gcent->gc_node);
688e12cec65SNikolay Aleksandrov 		gcent->destroy(gcent);
689e12cec65SNikolay Aleksandrov 	}
690e12cec65SNikolay Aleksandrov }
691e12cec65SNikolay Aleksandrov 
6928ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
693438ef2d0SNikolay Aleksandrov 						    struct net_bridge_port_group *pg,
694438ef2d0SNikolay Aleksandrov 						    __be32 ip_dst, __be32 group,
695438ef2d0SNikolay Aleksandrov 						    bool with_srcs, bool over_lmqt,
69642c11ccfSNikolay Aleksandrov 						    u8 sflag, u8 *igmp_type,
69742c11ccfSNikolay Aleksandrov 						    bool *need_rexmit)
698eb1d1641SHerbert Xu {
699085b53c8SNikolay Aleksandrov 	struct net_bridge_port *p = pg ? pg->key.port : NULL;
700438ef2d0SNikolay Aleksandrov 	struct net_bridge_group_src *ent;
701438ef2d0SNikolay Aleksandrov 	size_t pkt_size, igmp_hdr_size;
702438ef2d0SNikolay Aleksandrov 	unsigned long now = jiffies;
7035e923585SNikolay Aleksandrov 	struct igmpv3_query *ihv3;
704438ef2d0SNikolay Aleksandrov 	void *csum_start = NULL;
705438ef2d0SNikolay Aleksandrov 	__sum16 *csum = NULL;
706eb1d1641SHerbert Xu 	struct sk_buff *skb;
707eb1d1641SHerbert Xu 	struct igmphdr *ih;
708eb1d1641SHerbert Xu 	struct ethhdr *eth;
709438ef2d0SNikolay Aleksandrov 	unsigned long lmqt;
710eb1d1641SHerbert Xu 	struct iphdr *iph;
711438ef2d0SNikolay Aleksandrov 	u16 lmqt_srcs = 0;
712eb1d1641SHerbert Xu 
7135e923585SNikolay Aleksandrov 	igmp_hdr_size = sizeof(*ih);
714438ef2d0SNikolay Aleksandrov 	if (br->multicast_igmp_version == 3) {
7155e923585SNikolay Aleksandrov 		igmp_hdr_size = sizeof(*ihv3);
716438ef2d0SNikolay Aleksandrov 		if (pg && with_srcs) {
717438ef2d0SNikolay Aleksandrov 			lmqt = now + (br->multicast_last_member_interval *
718438ef2d0SNikolay Aleksandrov 				      br->multicast_last_member_count);
719438ef2d0SNikolay Aleksandrov 			hlist_for_each_entry(ent, &pg->src_list, node) {
720438ef2d0SNikolay Aleksandrov 				if (over_lmqt == time_after(ent->timer.expires,
721438ef2d0SNikolay Aleksandrov 							    lmqt) &&
722438ef2d0SNikolay Aleksandrov 				    ent->src_query_rexmit_cnt > 0)
723438ef2d0SNikolay Aleksandrov 					lmqt_srcs++;
724438ef2d0SNikolay Aleksandrov 			}
725438ef2d0SNikolay Aleksandrov 
726438ef2d0SNikolay Aleksandrov 			if (!lmqt_srcs)
727438ef2d0SNikolay Aleksandrov 				return NULL;
728438ef2d0SNikolay Aleksandrov 			igmp_hdr_size += lmqt_srcs * sizeof(__be32);
729438ef2d0SNikolay Aleksandrov 		}
730438ef2d0SNikolay Aleksandrov 	}
731438ef2d0SNikolay Aleksandrov 
732438ef2d0SNikolay Aleksandrov 	pkt_size = sizeof(*eth) + sizeof(*iph) + 4 + igmp_hdr_size;
733438ef2d0SNikolay Aleksandrov 	if ((p && pkt_size > p->dev->mtu) ||
734438ef2d0SNikolay Aleksandrov 	    pkt_size > br->dev->mtu)
735438ef2d0SNikolay Aleksandrov 		return NULL;
736438ef2d0SNikolay Aleksandrov 
737438ef2d0SNikolay Aleksandrov 	skb = netdev_alloc_skb_ip_align(br->dev, pkt_size);
738eb1d1641SHerbert Xu 	if (!skb)
739eb1d1641SHerbert Xu 		goto out;
740eb1d1641SHerbert Xu 
741eb1d1641SHerbert Xu 	skb->protocol = htons(ETH_P_IP);
742eb1d1641SHerbert Xu 
743eb1d1641SHerbert Xu 	skb_reset_mac_header(skb);
744eb1d1641SHerbert Xu 	eth = eth_hdr(skb);
745eb1d1641SHerbert Xu 
746e5a727f6SJoe Perches 	ether_addr_copy(eth->h_source, br->dev->dev_addr);
747438ef2d0SNikolay Aleksandrov 	ip_eth_mc_map(ip_dst, eth->h_dest);
748eb1d1641SHerbert Xu 	eth->h_proto = htons(ETH_P_IP);
749eb1d1641SHerbert Xu 	skb_put(skb, sizeof(*eth));
750eb1d1641SHerbert Xu 
751eb1d1641SHerbert Xu 	skb_set_network_header(skb, skb->len);
752eb1d1641SHerbert Xu 	iph = ip_hdr(skb);
753438ef2d0SNikolay Aleksandrov 	iph->tot_len = htons(pkt_size - sizeof(*eth));
754eb1d1641SHerbert Xu 
755eb1d1641SHerbert Xu 	iph->version = 4;
756eb1d1641SHerbert Xu 	iph->ihl = 6;
757eb1d1641SHerbert Xu 	iph->tos = 0xc0;
758eb1d1641SHerbert Xu 	iph->id = 0;
759eb1d1641SHerbert Xu 	iph->frag_off = htons(IP_DF);
760eb1d1641SHerbert Xu 	iph->ttl = 1;
761eb1d1641SHerbert Xu 	iph->protocol = IPPROTO_IGMP;
762675779adSNikolay Aleksandrov 	iph->saddr = br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR) ?
7631c8ad5bfSCong Wang 		     inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0;
764438ef2d0SNikolay Aleksandrov 	iph->daddr = ip_dst;
765eb1d1641SHerbert Xu 	((u8 *)&iph[1])[0] = IPOPT_RA;
766eb1d1641SHerbert Xu 	((u8 *)&iph[1])[1] = 4;
767eb1d1641SHerbert Xu 	((u8 *)&iph[1])[2] = 0;
768eb1d1641SHerbert Xu 	((u8 *)&iph[1])[3] = 0;
769eb1d1641SHerbert Xu 	ip_send_check(iph);
770eb1d1641SHerbert Xu 	skb_put(skb, 24);
771eb1d1641SHerbert Xu 
772eb1d1641SHerbert Xu 	skb_set_transport_header(skb, skb->len);
7731080ab95SNikolay Aleksandrov 	*igmp_type = IGMP_HOST_MEMBERSHIP_QUERY;
7745e923585SNikolay Aleksandrov 
7755e923585SNikolay Aleksandrov 	switch (br->multicast_igmp_version) {
7765e923585SNikolay Aleksandrov 	case 2:
7775e923585SNikolay Aleksandrov 		ih = igmp_hdr(skb);
778eb1d1641SHerbert Xu 		ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
779eb1d1641SHerbert Xu 		ih->code = (group ? br->multicast_last_member_interval :
780eb1d1641SHerbert Xu 				    br->multicast_query_response_interval) /
781eb1d1641SHerbert Xu 			   (HZ / IGMP_TIMER_SCALE);
782eb1d1641SHerbert Xu 		ih->group = group;
783eb1d1641SHerbert Xu 		ih->csum = 0;
784438ef2d0SNikolay Aleksandrov 		csum = &ih->csum;
785438ef2d0SNikolay Aleksandrov 		csum_start = (void *)ih;
7865e923585SNikolay Aleksandrov 		break;
7875e923585SNikolay Aleksandrov 	case 3:
7885e923585SNikolay Aleksandrov 		ihv3 = igmpv3_query_hdr(skb);
7895e923585SNikolay Aleksandrov 		ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY;
7905e923585SNikolay Aleksandrov 		ihv3->code = (group ? br->multicast_last_member_interval :
7915e923585SNikolay Aleksandrov 				      br->multicast_query_response_interval) /
7925e923585SNikolay Aleksandrov 			     (HZ / IGMP_TIMER_SCALE);
7935e923585SNikolay Aleksandrov 		ihv3->group = group;
7945e923585SNikolay Aleksandrov 		ihv3->qqic = br->multicast_query_interval / HZ;
795438ef2d0SNikolay Aleksandrov 		ihv3->nsrcs = htons(lmqt_srcs);
7965e923585SNikolay Aleksandrov 		ihv3->resv = 0;
797438ef2d0SNikolay Aleksandrov 		ihv3->suppress = sflag;
7985e923585SNikolay Aleksandrov 		ihv3->qrv = 2;
7995e923585SNikolay Aleksandrov 		ihv3->csum = 0;
800438ef2d0SNikolay Aleksandrov 		csum = &ihv3->csum;
801438ef2d0SNikolay Aleksandrov 		csum_start = (void *)ihv3;
802438ef2d0SNikolay Aleksandrov 		if (!pg || !with_srcs)
803438ef2d0SNikolay Aleksandrov 			break;
804438ef2d0SNikolay Aleksandrov 
805438ef2d0SNikolay Aleksandrov 		lmqt_srcs = 0;
806438ef2d0SNikolay Aleksandrov 		hlist_for_each_entry(ent, &pg->src_list, node) {
807438ef2d0SNikolay Aleksandrov 			if (over_lmqt == time_after(ent->timer.expires,
808438ef2d0SNikolay Aleksandrov 						    lmqt) &&
809438ef2d0SNikolay Aleksandrov 			    ent->src_query_rexmit_cnt > 0) {
810deb96566SNikolay Aleksandrov 				ihv3->srcs[lmqt_srcs++] = ent->addr.src.ip4;
811438ef2d0SNikolay Aleksandrov 				ent->src_query_rexmit_cnt--;
81242c11ccfSNikolay Aleksandrov 				if (need_rexmit && ent->src_query_rexmit_cnt)
81342c11ccfSNikolay Aleksandrov 					*need_rexmit = true;
814438ef2d0SNikolay Aleksandrov 			}
815438ef2d0SNikolay Aleksandrov 		}
816438ef2d0SNikolay Aleksandrov 		if (WARN_ON(lmqt_srcs != ntohs(ihv3->nsrcs))) {
817438ef2d0SNikolay Aleksandrov 			kfree_skb(skb);
818438ef2d0SNikolay Aleksandrov 			return NULL;
819438ef2d0SNikolay Aleksandrov 		}
8205e923585SNikolay Aleksandrov 		break;
8215e923585SNikolay Aleksandrov 	}
822eb1d1641SHerbert Xu 
823438ef2d0SNikolay Aleksandrov 	if (WARN_ON(!csum || !csum_start)) {
824438ef2d0SNikolay Aleksandrov 		kfree_skb(skb);
825438ef2d0SNikolay Aleksandrov 		return NULL;
826438ef2d0SNikolay Aleksandrov 	}
827438ef2d0SNikolay Aleksandrov 
828438ef2d0SNikolay Aleksandrov 	*csum = ip_compute_csum(csum_start, igmp_hdr_size);
8295e923585SNikolay Aleksandrov 	skb_put(skb, igmp_hdr_size);
830eb1d1641SHerbert Xu 	__skb_pull(skb, sizeof(*eth));
831eb1d1641SHerbert Xu 
832eb1d1641SHerbert Xu out:
833eb1d1641SHerbert Xu 	return skb;
834eb1d1641SHerbert Xu }
835eb1d1641SHerbert Xu 
836dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
83708b202b6SYOSHIFUJI Hideaki static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
838438ef2d0SNikolay Aleksandrov 						    struct net_bridge_port_group *pg,
839438ef2d0SNikolay Aleksandrov 						    const struct in6_addr *ip6_dst,
840438ef2d0SNikolay Aleksandrov 						    const struct in6_addr *group,
841438ef2d0SNikolay Aleksandrov 						    bool with_srcs, bool over_llqt,
84242c11ccfSNikolay Aleksandrov 						    u8 sflag, u8 *igmp_type,
84342c11ccfSNikolay Aleksandrov 						    bool *need_rexmit)
84408b202b6SYOSHIFUJI Hideaki {
845085b53c8SNikolay Aleksandrov 	struct net_bridge_port *p = pg ? pg->key.port : NULL;
846438ef2d0SNikolay Aleksandrov 	struct net_bridge_group_src *ent;
847438ef2d0SNikolay Aleksandrov 	size_t pkt_size, mld_hdr_size;
848438ef2d0SNikolay Aleksandrov 	unsigned long now = jiffies;
849aa2ae3e7SNikolay Aleksandrov 	struct mld2_query *mld2q;
850438ef2d0SNikolay Aleksandrov 	void *csum_start = NULL;
851aa2ae3e7SNikolay Aleksandrov 	unsigned long interval;
852438ef2d0SNikolay Aleksandrov 	__sum16 *csum = NULL;
85308b202b6SYOSHIFUJI Hideaki 	struct ipv6hdr *ip6h;
85408b202b6SYOSHIFUJI Hideaki 	struct mld_msg *mldq;
855aa2ae3e7SNikolay Aleksandrov 	struct sk_buff *skb;
856438ef2d0SNikolay Aleksandrov 	unsigned long llqt;
85708b202b6SYOSHIFUJI Hideaki 	struct ethhdr *eth;
858438ef2d0SNikolay Aleksandrov 	u16 llqt_srcs = 0;
85908b202b6SYOSHIFUJI Hideaki 	u8 *hopopt;
86008b202b6SYOSHIFUJI Hideaki 
861aa2ae3e7SNikolay Aleksandrov 	mld_hdr_size = sizeof(*mldq);
862438ef2d0SNikolay Aleksandrov 	if (br->multicast_mld_version == 2) {
863aa2ae3e7SNikolay Aleksandrov 		mld_hdr_size = sizeof(*mld2q);
864438ef2d0SNikolay Aleksandrov 		if (pg && with_srcs) {
865438ef2d0SNikolay Aleksandrov 			llqt = now + (br->multicast_last_member_interval *
866438ef2d0SNikolay Aleksandrov 				      br->multicast_last_member_count);
867438ef2d0SNikolay Aleksandrov 			hlist_for_each_entry(ent, &pg->src_list, node) {
868438ef2d0SNikolay Aleksandrov 				if (over_llqt == time_after(ent->timer.expires,
869438ef2d0SNikolay Aleksandrov 							    llqt) &&
870438ef2d0SNikolay Aleksandrov 				    ent->src_query_rexmit_cnt > 0)
871438ef2d0SNikolay Aleksandrov 					llqt_srcs++;
872438ef2d0SNikolay Aleksandrov 			}
873438ef2d0SNikolay Aleksandrov 
874438ef2d0SNikolay Aleksandrov 			if (!llqt_srcs)
875438ef2d0SNikolay Aleksandrov 				return NULL;
876438ef2d0SNikolay Aleksandrov 			mld_hdr_size += llqt_srcs * sizeof(struct in6_addr);
877438ef2d0SNikolay Aleksandrov 		}
878438ef2d0SNikolay Aleksandrov 	}
879438ef2d0SNikolay Aleksandrov 
880438ef2d0SNikolay Aleksandrov 	pkt_size = sizeof(*eth) + sizeof(*ip6h) + 8 + mld_hdr_size;
881438ef2d0SNikolay Aleksandrov 	if ((p && pkt_size > p->dev->mtu) ||
882438ef2d0SNikolay Aleksandrov 	    pkt_size > br->dev->mtu)
883438ef2d0SNikolay Aleksandrov 		return NULL;
884438ef2d0SNikolay Aleksandrov 
885438ef2d0SNikolay Aleksandrov 	skb = netdev_alloc_skb_ip_align(br->dev, pkt_size);
88608b202b6SYOSHIFUJI Hideaki 	if (!skb)
88708b202b6SYOSHIFUJI Hideaki 		goto out;
88808b202b6SYOSHIFUJI Hideaki 
88908b202b6SYOSHIFUJI Hideaki 	skb->protocol = htons(ETH_P_IPV6);
89008b202b6SYOSHIFUJI Hideaki 
89108b202b6SYOSHIFUJI Hideaki 	/* Ethernet header */
89208b202b6SYOSHIFUJI Hideaki 	skb_reset_mac_header(skb);
89308b202b6SYOSHIFUJI Hideaki 	eth = eth_hdr(skb);
89408b202b6SYOSHIFUJI Hideaki 
895e5a727f6SJoe Perches 	ether_addr_copy(eth->h_source, br->dev->dev_addr);
89608b202b6SYOSHIFUJI Hideaki 	eth->h_proto = htons(ETH_P_IPV6);
89708b202b6SYOSHIFUJI Hideaki 	skb_put(skb, sizeof(*eth));
89808b202b6SYOSHIFUJI Hideaki 
89908b202b6SYOSHIFUJI Hideaki 	/* IPv6 header + HbH option */
90008b202b6SYOSHIFUJI Hideaki 	skb_set_network_header(skb, skb->len);
90108b202b6SYOSHIFUJI Hideaki 	ip6h = ipv6_hdr(skb);
90208b202b6SYOSHIFUJI Hideaki 
90308b202b6SYOSHIFUJI Hideaki 	*(__force __be32 *)ip6h = htonl(0x60000000);
904aa2ae3e7SNikolay Aleksandrov 	ip6h->payload_len = htons(8 + mld_hdr_size);
90508b202b6SYOSHIFUJI Hideaki 	ip6h->nexthdr = IPPROTO_HOPOPTS;
90608b202b6SYOSHIFUJI Hideaki 	ip6h->hop_limit = 1;
907438ef2d0SNikolay Aleksandrov 	ip6h->daddr = *ip6_dst;
908d1d81d4cSUlrich Weber 	if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
909d1d81d4cSUlrich Weber 			       &ip6h->saddr)) {
910d1d81d4cSUlrich Weber 		kfree_skb(skb);
911675779adSNikolay Aleksandrov 		br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, false);
912d1d81d4cSUlrich Weber 		return NULL;
913d1d81d4cSUlrich Weber 	}
9140888d5f3Sdaniel 
915675779adSNikolay Aleksandrov 	br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true);
91636cff5a1SLinus Lüssing 	ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
91708b202b6SYOSHIFUJI Hideaki 
91808b202b6SYOSHIFUJI Hideaki 	hopopt = (u8 *)(ip6h + 1);
91908b202b6SYOSHIFUJI Hideaki 	hopopt[0] = IPPROTO_ICMPV6;		/* next hdr */
92008b202b6SYOSHIFUJI Hideaki 	hopopt[1] = 0;				/* length of HbH */
92108b202b6SYOSHIFUJI Hideaki 	hopopt[2] = IPV6_TLV_ROUTERALERT;	/* Router Alert */
92208b202b6SYOSHIFUJI Hideaki 	hopopt[3] = 2;				/* Length of RA Option */
92308b202b6SYOSHIFUJI Hideaki 	hopopt[4] = 0;				/* Type = 0x0000 (MLD) */
92408b202b6SYOSHIFUJI Hideaki 	hopopt[5] = 0;
9251de5a71cSEldad Zack 	hopopt[6] = IPV6_TLV_PAD1;		/* Pad1 */
9261de5a71cSEldad Zack 	hopopt[7] = IPV6_TLV_PAD1;		/* Pad1 */
92708b202b6SYOSHIFUJI Hideaki 
92808b202b6SYOSHIFUJI Hideaki 	skb_put(skb, sizeof(*ip6h) + 8);
92908b202b6SYOSHIFUJI Hideaki 
93008b202b6SYOSHIFUJI Hideaki 	/* ICMPv6 */
93108b202b6SYOSHIFUJI Hideaki 	skb_set_transport_header(skb, skb->len);
932438ef2d0SNikolay Aleksandrov 	interval = ipv6_addr_any(group) ?
93332de868cSLinus Lüssing 			br->multicast_query_response_interval :
93432de868cSLinus Lüssing 			br->multicast_last_member_interval;
9351080ab95SNikolay Aleksandrov 	*igmp_type = ICMPV6_MGM_QUERY;
936aa2ae3e7SNikolay Aleksandrov 	switch (br->multicast_mld_version) {
937aa2ae3e7SNikolay Aleksandrov 	case 1:
938aa2ae3e7SNikolay Aleksandrov 		mldq = (struct mld_msg *)icmp6_hdr(skb);
93908b202b6SYOSHIFUJI Hideaki 		mldq->mld_type = ICMPV6_MGM_QUERY;
94008b202b6SYOSHIFUJI Hideaki 		mldq->mld_code = 0;
94108b202b6SYOSHIFUJI Hideaki 		mldq->mld_cksum = 0;
94208b202b6SYOSHIFUJI Hideaki 		mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
94308b202b6SYOSHIFUJI Hideaki 		mldq->mld_reserved = 0;
944438ef2d0SNikolay Aleksandrov 		mldq->mld_mca = *group;
945438ef2d0SNikolay Aleksandrov 		csum = &mldq->mld_cksum;
946438ef2d0SNikolay Aleksandrov 		csum_start = (void *)mldq;
947aa2ae3e7SNikolay Aleksandrov 		break;
948aa2ae3e7SNikolay Aleksandrov 	case 2:
949aa2ae3e7SNikolay Aleksandrov 		mld2q = (struct mld2_query *)icmp6_hdr(skb);
95053631a5fSLance Richardson 		mld2q->mld2q_mrc = htons((u16)jiffies_to_msecs(interval));
951aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_type = ICMPV6_MGM_QUERY;
952aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_code = 0;
953aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_cksum = 0;
954aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_resv1 = 0;
955aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_resv2 = 0;
956438ef2d0SNikolay Aleksandrov 		mld2q->mld2q_suppress = sflag;
957aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_qrv = 2;
958438ef2d0SNikolay Aleksandrov 		mld2q->mld2q_nsrcs = htons(llqt_srcs);
959aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_qqic = br->multicast_query_interval / HZ;
960438ef2d0SNikolay Aleksandrov 		mld2q->mld2q_mca = *group;
961438ef2d0SNikolay Aleksandrov 		csum = &mld2q->mld2q_cksum;
962438ef2d0SNikolay Aleksandrov 		csum_start = (void *)mld2q;
963438ef2d0SNikolay Aleksandrov 		if (!pg || !with_srcs)
964438ef2d0SNikolay Aleksandrov 			break;
965438ef2d0SNikolay Aleksandrov 
966438ef2d0SNikolay Aleksandrov 		llqt_srcs = 0;
967438ef2d0SNikolay Aleksandrov 		hlist_for_each_entry(ent, &pg->src_list, node) {
968438ef2d0SNikolay Aleksandrov 			if (over_llqt == time_after(ent->timer.expires,
969438ef2d0SNikolay Aleksandrov 						    llqt) &&
970438ef2d0SNikolay Aleksandrov 			    ent->src_query_rexmit_cnt > 0) {
971deb96566SNikolay Aleksandrov 				mld2q->mld2q_srcs[llqt_srcs++] = ent->addr.src.ip6;
972438ef2d0SNikolay Aleksandrov 				ent->src_query_rexmit_cnt--;
97342c11ccfSNikolay Aleksandrov 				if (need_rexmit && ent->src_query_rexmit_cnt)
97442c11ccfSNikolay Aleksandrov 					*need_rexmit = true;
975438ef2d0SNikolay Aleksandrov 			}
976438ef2d0SNikolay Aleksandrov 		}
977438ef2d0SNikolay Aleksandrov 		if (WARN_ON(llqt_srcs != ntohs(mld2q->mld2q_nsrcs))) {
978438ef2d0SNikolay Aleksandrov 			kfree_skb(skb);
979438ef2d0SNikolay Aleksandrov 			return NULL;
980438ef2d0SNikolay Aleksandrov 		}
981aa2ae3e7SNikolay Aleksandrov 		break;
982aa2ae3e7SNikolay Aleksandrov 	}
98308b202b6SYOSHIFUJI Hideaki 
984438ef2d0SNikolay Aleksandrov 	if (WARN_ON(!csum || !csum_start)) {
985438ef2d0SNikolay Aleksandrov 		kfree_skb(skb);
986438ef2d0SNikolay Aleksandrov 		return NULL;
987438ef2d0SNikolay Aleksandrov 	}
988438ef2d0SNikolay Aleksandrov 
989438ef2d0SNikolay Aleksandrov 	*csum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, mld_hdr_size,
990438ef2d0SNikolay Aleksandrov 				IPPROTO_ICMPV6,
991438ef2d0SNikolay Aleksandrov 				csum_partial(csum_start, mld_hdr_size, 0));
992438ef2d0SNikolay Aleksandrov 	skb_put(skb, mld_hdr_size);
99308b202b6SYOSHIFUJI Hideaki 	__skb_pull(skb, sizeof(*eth));
99408b202b6SYOSHIFUJI Hideaki 
99508b202b6SYOSHIFUJI Hideaki out:
99608b202b6SYOSHIFUJI Hideaki 	return skb;
99708b202b6SYOSHIFUJI Hideaki }
99808b202b6SYOSHIFUJI Hideaki #endif
99908b202b6SYOSHIFUJI Hideaki 
10008ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
1001438ef2d0SNikolay Aleksandrov 						struct net_bridge_port_group *pg,
1002438ef2d0SNikolay Aleksandrov 						struct br_ip *ip_dst,
1003438ef2d0SNikolay Aleksandrov 						struct br_ip *group,
1004438ef2d0SNikolay Aleksandrov 						bool with_srcs, bool over_lmqt,
100542c11ccfSNikolay Aleksandrov 						u8 sflag, u8 *igmp_type,
100642c11ccfSNikolay Aleksandrov 						bool *need_rexmit)
10078ef2a9a5SYOSHIFUJI Hideaki {
1008438ef2d0SNikolay Aleksandrov 	__be32 ip4_dst;
1009438ef2d0SNikolay Aleksandrov 
1010438ef2d0SNikolay Aleksandrov 	switch (group->proto) {
10118ef2a9a5SYOSHIFUJI Hideaki 	case htons(ETH_P_IP):
1012eab3227bSNikolay Aleksandrov 		ip4_dst = ip_dst ? ip_dst->dst.ip4 : htonl(INADDR_ALLHOSTS_GROUP);
1013438ef2d0SNikolay Aleksandrov 		return br_ip4_multicast_alloc_query(br, pg,
1014eab3227bSNikolay Aleksandrov 						    ip4_dst, group->dst.ip4,
1015438ef2d0SNikolay Aleksandrov 						    with_srcs, over_lmqt,
101642c11ccfSNikolay Aleksandrov 						    sflag, igmp_type,
101742c11ccfSNikolay Aleksandrov 						    need_rexmit);
1018dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1019438ef2d0SNikolay Aleksandrov 	case htons(ETH_P_IPV6): {
1020438ef2d0SNikolay Aleksandrov 		struct in6_addr ip6_dst;
1021438ef2d0SNikolay Aleksandrov 
1022438ef2d0SNikolay Aleksandrov 		if (ip_dst)
1023eab3227bSNikolay Aleksandrov 			ip6_dst = ip_dst->dst.ip6;
1024438ef2d0SNikolay Aleksandrov 		else
1025438ef2d0SNikolay Aleksandrov 			ipv6_addr_set(&ip6_dst, htonl(0xff020000), 0, 0,
1026438ef2d0SNikolay Aleksandrov 				      htonl(1));
1027438ef2d0SNikolay Aleksandrov 
1028438ef2d0SNikolay Aleksandrov 		return br_ip6_multicast_alloc_query(br, pg,
1029eab3227bSNikolay Aleksandrov 						    &ip6_dst, &group->dst.ip6,
1030438ef2d0SNikolay Aleksandrov 						    with_srcs, over_lmqt,
103142c11ccfSNikolay Aleksandrov 						    sflag, igmp_type,
103242c11ccfSNikolay Aleksandrov 						    need_rexmit);
1033438ef2d0SNikolay Aleksandrov 	}
103408b202b6SYOSHIFUJI Hideaki #endif
10358ef2a9a5SYOSHIFUJI Hideaki 	}
10368ef2a9a5SYOSHIFUJI Hideaki 	return NULL;
10378ef2a9a5SYOSHIFUJI Hideaki }
10388ef2a9a5SYOSHIFUJI Hideaki 
1039cfd56754SCong Wang struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
10405e923585SNikolay Aleksandrov 						    struct br_ip *group)
1041eb1d1641SHerbert Xu {
1042eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
10434c0833bcSTobias Klauser 	int err;
1044eb1d1641SHerbert Xu 
104519e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip_get(br, group);
104619e3a9c9SNikolay Aleksandrov 	if (mp)
104719e3a9c9SNikolay Aleksandrov 		return mp;
1048eb1d1641SHerbert Xu 
104919e3a9c9SNikolay Aleksandrov 	if (atomic_read(&br->mdb_hash_tbl.nelems) >= br->hash_max) {
105019e3a9c9SNikolay Aleksandrov 		br_opt_toggle(br, BROPT_MULTICAST_ENABLED, false);
105119e3a9c9SNikolay Aleksandrov 		return ERR_PTR(-E2BIG);
1052eb1d1641SHerbert Xu 	}
1053eb1d1641SHerbert Xu 
1054eb1d1641SHerbert Xu 	mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
1055eb1d1641SHerbert Xu 	if (unlikely(!mp))
10564c0833bcSTobias Klauser 		return ERR_PTR(-ENOMEM);
1057eb1d1641SHerbert Xu 
1058eb1d1641SHerbert Xu 	mp->br = br;
10598ef2a9a5SYOSHIFUJI Hideaki 	mp->addr = *group;
1060e12cec65SNikolay Aleksandrov 	mp->mcast_gc.destroy = br_multicast_destroy_mdb_entry;
106188c1f37fSAllen Pais 	timer_setup(&mp->timer, br_multicast_group_expired, 0);
106219e3a9c9SNikolay Aleksandrov 	err = rhashtable_lookup_insert_fast(&br->mdb_hash_tbl, &mp->rhnode,
106319e3a9c9SNikolay Aleksandrov 					    br_mdb_rht_params);
106419e3a9c9SNikolay Aleksandrov 	if (err) {
106519e3a9c9SNikolay Aleksandrov 		kfree(mp);
106619e3a9c9SNikolay Aleksandrov 		mp = ERR_PTR(err);
106719e3a9c9SNikolay Aleksandrov 	} else {
106819e3a9c9SNikolay Aleksandrov 		hlist_add_head_rcu(&mp->mdb_node, &br->mdb_list);
106919e3a9c9SNikolay Aleksandrov 	}
10701faabf2aSEric Dumazet 
1071eb1d1641SHerbert Xu 	return mp;
1072eb1d1641SHerbert Xu }
1073eb1d1641SHerbert Xu 
10748b671779SNikolay Aleksandrov static void br_multicast_group_src_expired(struct timer_list *t)
10758b671779SNikolay Aleksandrov {
10768b671779SNikolay Aleksandrov 	struct net_bridge_group_src *src = from_timer(src, t, timer);
10778b671779SNikolay Aleksandrov 	struct net_bridge_port_group *pg;
10788b671779SNikolay Aleksandrov 	struct net_bridge *br = src->br;
10798b671779SNikolay Aleksandrov 
10808b671779SNikolay Aleksandrov 	spin_lock(&br->multicast_lock);
10818b671779SNikolay Aleksandrov 	if (hlist_unhashed(&src->node) || !netif_running(br->dev) ||
10828b671779SNikolay Aleksandrov 	    timer_pending(&src->timer))
10838b671779SNikolay Aleksandrov 		goto out;
10848b671779SNikolay Aleksandrov 
10858b671779SNikolay Aleksandrov 	pg = src->pg;
10868b671779SNikolay Aleksandrov 	if (pg->filter_mode == MCAST_INCLUDE) {
1087d5a10222SNikolay Aleksandrov 		br_multicast_del_group_src(src, false);
10888b671779SNikolay Aleksandrov 		if (!hlist_empty(&pg->src_list))
10898b671779SNikolay Aleksandrov 			goto out;
10908b671779SNikolay Aleksandrov 		br_multicast_find_del_pg(br, pg);
10919116ffbfSNikolay Aleksandrov 	} else {
10929116ffbfSNikolay Aleksandrov 		br_multicast_fwd_src_handle(src);
10938b671779SNikolay Aleksandrov 	}
10949116ffbfSNikolay Aleksandrov 
10958b671779SNikolay Aleksandrov out:
10968b671779SNikolay Aleksandrov 	spin_unlock(&br->multicast_lock);
10978b671779SNikolay Aleksandrov }
10988b671779SNikolay Aleksandrov 
1099474ddb37SNikolay Aleksandrov struct net_bridge_group_src *
11008b671779SNikolay Aleksandrov br_multicast_find_group_src(struct net_bridge_port_group *pg, struct br_ip *ip)
11018b671779SNikolay Aleksandrov {
11028b671779SNikolay Aleksandrov 	struct net_bridge_group_src *ent;
11038b671779SNikolay Aleksandrov 
11048b671779SNikolay Aleksandrov 	switch (ip->proto) {
11058b671779SNikolay Aleksandrov 	case htons(ETH_P_IP):
11068b671779SNikolay Aleksandrov 		hlist_for_each_entry(ent, &pg->src_list, node)
1107deb96566SNikolay Aleksandrov 			if (ip->src.ip4 == ent->addr.src.ip4)
11088b671779SNikolay Aleksandrov 				return ent;
11098b671779SNikolay Aleksandrov 		break;
11108b671779SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
11118b671779SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
11128b671779SNikolay Aleksandrov 		hlist_for_each_entry(ent, &pg->src_list, node)
1113deb96566SNikolay Aleksandrov 			if (!ipv6_addr_cmp(&ent->addr.src.ip6, &ip->src.ip6))
11148b671779SNikolay Aleksandrov 				return ent;
11158b671779SNikolay Aleksandrov 		break;
11168b671779SNikolay Aleksandrov #endif
11178b671779SNikolay Aleksandrov 	}
11188b671779SNikolay Aleksandrov 
11198b671779SNikolay Aleksandrov 	return NULL;
11208b671779SNikolay Aleksandrov }
11218b671779SNikolay Aleksandrov 
11228b671779SNikolay Aleksandrov static struct net_bridge_group_src *
11238b671779SNikolay Aleksandrov br_multicast_new_group_src(struct net_bridge_port_group *pg, struct br_ip *src_ip)
11248b671779SNikolay Aleksandrov {
11258b671779SNikolay Aleksandrov 	struct net_bridge_group_src *grp_src;
11268b671779SNikolay Aleksandrov 
11278b671779SNikolay Aleksandrov 	if (unlikely(pg->src_ents >= PG_SRC_ENT_LIMIT))
11288b671779SNikolay Aleksandrov 		return NULL;
11298b671779SNikolay Aleksandrov 
11308b671779SNikolay Aleksandrov 	switch (src_ip->proto) {
11318b671779SNikolay Aleksandrov 	case htons(ETH_P_IP):
1132deb96566SNikolay Aleksandrov 		if (ipv4_is_zeronet(src_ip->src.ip4) ||
1133deb96566SNikolay Aleksandrov 		    ipv4_is_multicast(src_ip->src.ip4))
11348b671779SNikolay Aleksandrov 			return NULL;
11358b671779SNikolay Aleksandrov 		break;
11368b671779SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
11378b671779SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
1138deb96566SNikolay Aleksandrov 		if (ipv6_addr_any(&src_ip->src.ip6) ||
1139deb96566SNikolay Aleksandrov 		    ipv6_addr_is_multicast(&src_ip->src.ip6))
11408b671779SNikolay Aleksandrov 			return NULL;
11418b671779SNikolay Aleksandrov 		break;
11428b671779SNikolay Aleksandrov #endif
11438b671779SNikolay Aleksandrov 	}
11448b671779SNikolay Aleksandrov 
11458b671779SNikolay Aleksandrov 	grp_src = kzalloc(sizeof(*grp_src), GFP_ATOMIC);
11468b671779SNikolay Aleksandrov 	if (unlikely(!grp_src))
11478b671779SNikolay Aleksandrov 		return NULL;
11488b671779SNikolay Aleksandrov 
11498b671779SNikolay Aleksandrov 	grp_src->pg = pg;
1150085b53c8SNikolay Aleksandrov 	grp_src->br = pg->key.port->br;
11518b671779SNikolay Aleksandrov 	grp_src->addr = *src_ip;
1152e12cec65SNikolay Aleksandrov 	grp_src->mcast_gc.destroy = br_multicast_destroy_group_src;
11538b671779SNikolay Aleksandrov 	timer_setup(&grp_src->timer, br_multicast_group_src_expired, 0);
11548b671779SNikolay Aleksandrov 
11558b671779SNikolay Aleksandrov 	hlist_add_head_rcu(&grp_src->node, &pg->src_list);
11568b671779SNikolay Aleksandrov 	pg->src_ents++;
11578b671779SNikolay Aleksandrov 
11588b671779SNikolay Aleksandrov 	return grp_src;
11598b671779SNikolay Aleksandrov }
11608b671779SNikolay Aleksandrov 
1161cfd56754SCong Wang struct net_bridge_port_group *br_multicast_new_port_group(
1162cfd56754SCong Wang 			struct net_bridge_port *port,
1163cfd56754SCong Wang 			struct br_ip *group,
1164ccb1c31aSAmerigo Wang 			struct net_bridge_port_group __rcu *next,
11656db6f0eaSFelix Fietkau 			unsigned char flags,
11668b671779SNikolay Aleksandrov 			const unsigned char *src,
11678f8cb77eSNikolay Aleksandrov 			u8 filter_mode,
11688f8cb77eSNikolay Aleksandrov 			u8 rt_protocol)
1169cfd56754SCong Wang {
1170cfd56754SCong Wang 	struct net_bridge_port_group *p;
1171cfd56754SCong Wang 
1172cfd56754SCong Wang 	p = kzalloc(sizeof(*p), GFP_ATOMIC);
1173cfd56754SCong Wang 	if (unlikely(!p))
1174cfd56754SCong Wang 		return NULL;
1175cfd56754SCong Wang 
1176085b53c8SNikolay Aleksandrov 	p->key.addr = *group;
1177085b53c8SNikolay Aleksandrov 	p->key.port = port;
11789d06b6d8SElad Raz 	p->flags = flags;
11798b671779SNikolay Aleksandrov 	p->filter_mode = filter_mode;
11808f8cb77eSNikolay Aleksandrov 	p->rt_protocol = rt_protocol;
11815b163288SNikolay Aleksandrov 	p->eht_host_tree = RB_ROOT;
1182baa74d39SNikolay Aleksandrov 	p->eht_set_tree = RB_ROOT;
1183e12cec65SNikolay Aleksandrov 	p->mcast_gc.destroy = br_multicast_destroy_port_group;
11848b671779SNikolay Aleksandrov 	INIT_HLIST_HEAD(&p->src_list);
1185085b53c8SNikolay Aleksandrov 
1186085b53c8SNikolay Aleksandrov 	if (!br_multicast_is_star_g(group) &&
1187085b53c8SNikolay Aleksandrov 	    rhashtable_lookup_insert_fast(&port->br->sg_port_tbl, &p->rhnode,
1188085b53c8SNikolay Aleksandrov 					  br_sg_port_rht_params)) {
1189085b53c8SNikolay Aleksandrov 		kfree(p);
1190085b53c8SNikolay Aleksandrov 		return NULL;
1191085b53c8SNikolay Aleksandrov 	}
1192085b53c8SNikolay Aleksandrov 
1193eca2a43bSstephen hemminger 	rcu_assign_pointer(p->next, next);
119488c1f37fSAllen Pais 	timer_setup(&p->timer, br_multicast_port_group_expired, 0);
119542c11ccfSNikolay Aleksandrov 	timer_setup(&p->rexmit_timer, br_multicast_port_group_rexmit, 0);
119642c11ccfSNikolay Aleksandrov 	hlist_add_head(&p->mglist, &port->mglist);
11976db6f0eaSFelix Fietkau 
11986db6f0eaSFelix Fietkau 	if (src)
11996db6f0eaSFelix Fietkau 		memcpy(p->eth_addr, src, ETH_ALEN);
12006db6f0eaSFelix Fietkau 	else
12011bfe45f4SMao Wenan 		eth_broadcast_addr(p->eth_addr);
12026db6f0eaSFelix Fietkau 
1203cfd56754SCong Wang 	return p;
1204cfd56754SCong Wang }
1205cfd56754SCong Wang 
12061bc844eeSNikolay Aleksandrov void br_multicast_host_join(struct net_bridge_mdb_entry *mp, bool notify)
12071bc844eeSNikolay Aleksandrov {
12081bc844eeSNikolay Aleksandrov 	if (!mp->host_joined) {
12091bc844eeSNikolay Aleksandrov 		mp->host_joined = true;
1210094b82fdSNikolay Aleksandrov 		if (br_multicast_is_star_g(&mp->addr))
1211094b82fdSNikolay Aleksandrov 			br_multicast_star_g_host_state(mp);
12121bc844eeSNikolay Aleksandrov 		if (notify)
121381f19838SNikolay Aleksandrov 			br_mdb_notify(mp->br->dev, mp, NULL, RTM_NEWMDB);
12141bc844eeSNikolay Aleksandrov 	}
1215955062b0SNikolay Aleksandrov 
1216955062b0SNikolay Aleksandrov 	if (br_group_is_l2(&mp->addr))
1217955062b0SNikolay Aleksandrov 		return;
1218955062b0SNikolay Aleksandrov 
12191bc844eeSNikolay Aleksandrov 	mod_timer(&mp->timer, jiffies + mp->br->multicast_membership_interval);
12201bc844eeSNikolay Aleksandrov }
12211bc844eeSNikolay Aleksandrov 
12221bc844eeSNikolay Aleksandrov void br_multicast_host_leave(struct net_bridge_mdb_entry *mp, bool notify)
12231bc844eeSNikolay Aleksandrov {
12241bc844eeSNikolay Aleksandrov 	if (!mp->host_joined)
12251bc844eeSNikolay Aleksandrov 		return;
12261bc844eeSNikolay Aleksandrov 
12271bc844eeSNikolay Aleksandrov 	mp->host_joined = false;
1228094b82fdSNikolay Aleksandrov 	if (br_multicast_is_star_g(&mp->addr))
1229094b82fdSNikolay Aleksandrov 		br_multicast_star_g_host_state(mp);
12301bc844eeSNikolay Aleksandrov 	if (notify)
123181f19838SNikolay Aleksandrov 		br_mdb_notify(mp->br->dev, mp, NULL, RTM_DELMDB);
12321bc844eeSNikolay Aleksandrov }
12331bc844eeSNikolay Aleksandrov 
1234b0812368SNikolay Aleksandrov static struct net_bridge_port_group *
1235b0812368SNikolay Aleksandrov __br_multicast_add_group(struct net_bridge *br,
12368ef2a9a5SYOSHIFUJI Hideaki 			 struct net_bridge_port *port,
12376db6f0eaSFelix Fietkau 			 struct br_ip *group,
12388b671779SNikolay Aleksandrov 			 const unsigned char *src,
12390436862eSNikolay Aleksandrov 			 u8 filter_mode,
12409116ffbfSNikolay Aleksandrov 			 bool igmpv2_mldv1,
12419116ffbfSNikolay Aleksandrov 			 bool blocked)
1242eb1d1641SHerbert Xu {
1243e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
1244b0812368SNikolay Aleksandrov 	struct net_bridge_port_group *p = NULL;
12455e923585SNikolay Aleksandrov 	struct net_bridge_mdb_entry *mp;
1246454594f3SLinus Lüssing 	unsigned long now = jiffies;
1247eb1d1641SHerbert Xu 
1248eb1d1641SHerbert Xu 	if (!netif_running(br->dev) ||
1249eb1d1641SHerbert Xu 	    (port && port->state == BR_STATE_DISABLED))
1250eb1d1641SHerbert Xu 		goto out;
1251eb1d1641SHerbert Xu 
125219e3a9c9SNikolay Aleksandrov 	mp = br_multicast_new_group(br, group);
12534c0833bcSTobias Klauser 	if (IS_ERR(mp))
1254b0812368SNikolay Aleksandrov 		return ERR_PTR(PTR_ERR(mp));
1255eb1d1641SHerbert Xu 
1256eb1d1641SHerbert Xu 	if (!port) {
12571bc844eeSNikolay Aleksandrov 		br_multicast_host_join(mp, true);
1258eb1d1641SHerbert Xu 		goto out;
1259eb1d1641SHerbert Xu 	}
1260eb1d1641SHerbert Xu 
1261e8051688SEric Dumazet 	for (pp = &mp->ports;
1262e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
1263e8051688SEric Dumazet 	     pp = &p->next) {
12646db6f0eaSFelix Fietkau 		if (br_port_group_equal(p, port, src))
1265454594f3SLinus Lüssing 			goto found;
1266085b53c8SNikolay Aleksandrov 		if ((unsigned long)p->key.port < (unsigned long)port)
1267eb1d1641SHerbert Xu 			break;
1268eb1d1641SHerbert Xu 	}
1269eb1d1641SHerbert Xu 
1270955062b0SNikolay Aleksandrov 	p = br_multicast_new_port_group(port, group, *pp, 0, src,
1271955062b0SNikolay Aleksandrov 					filter_mode, RTPROT_KERNEL);
1272b0812368SNikolay Aleksandrov 	if (unlikely(!p)) {
1273b0812368SNikolay Aleksandrov 		p = ERR_PTR(-ENOMEM);
1274b0812368SNikolay Aleksandrov 		goto out;
1275b0812368SNikolay Aleksandrov 	}
1276eb1d1641SHerbert Xu 	rcu_assign_pointer(*pp, p);
12779116ffbfSNikolay Aleksandrov 	if (blocked)
12789116ffbfSNikolay Aleksandrov 		p->flags |= MDB_PG_FLAGS_BLOCKED;
127981f19838SNikolay Aleksandrov 	br_mdb_notify(br->dev, mp, p, RTM_NEWMDB);
1280eb1d1641SHerbert Xu 
1281454594f3SLinus Lüssing found:
12820436862eSNikolay Aleksandrov 	if (igmpv2_mldv1)
1283454594f3SLinus Lüssing 		mod_timer(&p->timer, now + br->multicast_membership_interval);
128481f19838SNikolay Aleksandrov 
1285eb1d1641SHerbert Xu out:
1286b0812368SNikolay Aleksandrov 	return p;
1287b0812368SNikolay Aleksandrov }
1288eb1d1641SHerbert Xu 
1289b0812368SNikolay Aleksandrov static int br_multicast_add_group(struct net_bridge *br,
1290b0812368SNikolay Aleksandrov 				  struct net_bridge_port *port,
1291b0812368SNikolay Aleksandrov 				  struct br_ip *group,
1292b0812368SNikolay Aleksandrov 				  const unsigned char *src,
1293b0812368SNikolay Aleksandrov 				  u8 filter_mode,
1294b0812368SNikolay Aleksandrov 				  bool igmpv2_mldv1)
1295b0812368SNikolay Aleksandrov {
1296b0812368SNikolay Aleksandrov 	struct net_bridge_port_group *pg;
1297b0812368SNikolay Aleksandrov 	int err;
1298b0812368SNikolay Aleksandrov 
1299b0812368SNikolay Aleksandrov 	spin_lock(&br->multicast_lock);
1300b0812368SNikolay Aleksandrov 	pg = __br_multicast_add_group(br, port, group, src, filter_mode,
13019116ffbfSNikolay Aleksandrov 				      igmpv2_mldv1, false);
1302b0812368SNikolay Aleksandrov 	/* NULL is considered valid for host joined groups */
1303*8d21c882SJiapeng Zhong 	err = PTR_ERR_OR_ZERO(pg);
1304eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1305b0812368SNikolay Aleksandrov 
1306eb1d1641SHerbert Xu 	return err;
1307eb1d1641SHerbert Xu }
1308eb1d1641SHerbert Xu 
13098ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_add_group(struct net_bridge *br,
13108ef2a9a5SYOSHIFUJI Hideaki 				      struct net_bridge_port *port,
1311b0e9a30dSVlad Yasevich 				      __be32 group,
13126db6f0eaSFelix Fietkau 				      __u16 vid,
13138b671779SNikolay Aleksandrov 				      const unsigned char *src,
13148b671779SNikolay Aleksandrov 				      bool igmpv2)
13158ef2a9a5SYOSHIFUJI Hideaki {
13168ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip br_group;
13178b671779SNikolay Aleksandrov 	u8 filter_mode;
13188ef2a9a5SYOSHIFUJI Hideaki 
13198ef2a9a5SYOSHIFUJI Hideaki 	if (ipv4_is_local_multicast(group))
13208ef2a9a5SYOSHIFUJI Hideaki 		return 0;
13218ef2a9a5SYOSHIFUJI Hideaki 
13221515a63fSNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
1323eab3227bSNikolay Aleksandrov 	br_group.dst.ip4 = group;
13248ef2a9a5SYOSHIFUJI Hideaki 	br_group.proto = htons(ETH_P_IP);
1325b0e9a30dSVlad Yasevich 	br_group.vid = vid;
13268b671779SNikolay Aleksandrov 	filter_mode = igmpv2 ? MCAST_EXCLUDE : MCAST_INCLUDE;
13278ef2a9a5SYOSHIFUJI Hideaki 
13280436862eSNikolay Aleksandrov 	return br_multicast_add_group(br, port, &br_group, src, filter_mode,
13290436862eSNikolay Aleksandrov 				      igmpv2);
13308ef2a9a5SYOSHIFUJI Hideaki }
13318ef2a9a5SYOSHIFUJI Hideaki 
1332dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
133308b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_add_group(struct net_bridge *br,
133408b202b6SYOSHIFUJI Hideaki 				      struct net_bridge_port *port,
1335b0e9a30dSVlad Yasevich 				      const struct in6_addr *group,
13366db6f0eaSFelix Fietkau 				      __u16 vid,
13378b671779SNikolay Aleksandrov 				      const unsigned char *src,
13388b671779SNikolay Aleksandrov 				      bool mldv1)
133908b202b6SYOSHIFUJI Hideaki {
134008b202b6SYOSHIFUJI Hideaki 	struct br_ip br_group;
13418b671779SNikolay Aleksandrov 	u8 filter_mode;
134208b202b6SYOSHIFUJI Hideaki 
13433c3769e6SLinus Lüssing 	if (ipv6_addr_is_ll_all_nodes(group))
134408b202b6SYOSHIFUJI Hideaki 		return 0;
134508b202b6SYOSHIFUJI Hideaki 
134619e3a9c9SNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
1347eab3227bSNikolay Aleksandrov 	br_group.dst.ip6 = *group;
13489cc6e0c4SLinus Lüssing 	br_group.proto = htons(ETH_P_IPV6);
1349b0e9a30dSVlad Yasevich 	br_group.vid = vid;
13508b671779SNikolay Aleksandrov 	filter_mode = mldv1 ? MCAST_EXCLUDE : MCAST_INCLUDE;
135108b202b6SYOSHIFUJI Hideaki 
13520436862eSNikolay Aleksandrov 	return br_multicast_add_group(br, port, &br_group, src, filter_mode,
13530436862eSNikolay Aleksandrov 				      mldv1);
135408b202b6SYOSHIFUJI Hideaki }
135508b202b6SYOSHIFUJI Hideaki #endif
135608b202b6SYOSHIFUJI Hideaki 
135788c1f37fSAllen Pais static void br_multicast_router_expired(struct timer_list *t)
1358eb1d1641SHerbert Xu {
135988c1f37fSAllen Pais 	struct net_bridge_port *port =
136088c1f37fSAllen Pais 			from_timer(port, t, multicast_router_timer);
1361eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
1362eb1d1641SHerbert Xu 
1363eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
1364a55d8246SNikolay Aleksandrov 	if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
1365a55d8246SNikolay Aleksandrov 	    port->multicast_router == MDB_RTR_TYPE_PERM ||
1366f12e7d95SNogah Frankel 	    timer_pending(&port->multicast_router_timer))
1367eb1d1641SHerbert Xu 		goto out;
1368eb1d1641SHerbert Xu 
1369f12e7d95SNogah Frankel 	__del_port_router(port);
1370eb1d1641SHerbert Xu out:
1371eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1372eb1d1641SHerbert Xu }
1373eb1d1641SHerbert Xu 
137477041420SYotam Gigi static void br_mc_router_state_change(struct net_bridge *p,
137577041420SYotam Gigi 				      bool is_mc_router)
137677041420SYotam Gigi {
137777041420SYotam Gigi 	struct switchdev_attr attr = {
137877041420SYotam Gigi 		.orig_dev = p->dev,
137977041420SYotam Gigi 		.id = SWITCHDEV_ATTR_ID_BRIDGE_MROUTER,
138077041420SYotam Gigi 		.flags = SWITCHDEV_F_DEFER,
138177041420SYotam Gigi 		.u.mrouter = is_mc_router,
138277041420SYotam Gigi 	};
138377041420SYotam Gigi 
138477041420SYotam Gigi 	switchdev_port_attr_set(p->dev, &attr);
138577041420SYotam Gigi }
138677041420SYotam Gigi 
138788c1f37fSAllen Pais static void br_multicast_local_router_expired(struct timer_list *t)
1388eb1d1641SHerbert Xu {
138988c1f37fSAllen Pais 	struct net_bridge *br = from_timer(br, t, multicast_router_timer);
139077041420SYotam Gigi 
139177041420SYotam Gigi 	spin_lock(&br->multicast_lock);
139277041420SYotam Gigi 	if (br->multicast_router == MDB_RTR_TYPE_DISABLED ||
139377041420SYotam Gigi 	    br->multicast_router == MDB_RTR_TYPE_PERM ||
139477041420SYotam Gigi 	    timer_pending(&br->multicast_router_timer))
139577041420SYotam Gigi 		goto out;
139677041420SYotam Gigi 
139777041420SYotam Gigi 	br_mc_router_state_change(br, false);
139877041420SYotam Gigi out:
139977041420SYotam Gigi 	spin_unlock(&br->multicast_lock);
1400eb1d1641SHerbert Xu }
1401eb1d1641SHerbert Xu 
1402cc0fdd80SLinus Lüssing static void br_multicast_querier_expired(struct net_bridge *br,
140390010b36SLinus Lüssing 					 struct bridge_mcast_own_query *query)
1404c83b8fabSHerbert Xu {
1405c83b8fabSHerbert Xu 	spin_lock(&br->multicast_lock);
140613cefad2SNikolay Aleksandrov 	if (!netif_running(br->dev) || !br_opt_get(br, BROPT_MULTICAST_ENABLED))
1407c83b8fabSHerbert Xu 		goto out;
1408c83b8fabSHerbert Xu 
1409cc0fdd80SLinus Lüssing 	br_multicast_start_querier(br, query);
1410c83b8fabSHerbert Xu 
1411c83b8fabSHerbert Xu out:
1412c83b8fabSHerbert Xu 	spin_unlock(&br->multicast_lock);
1413c83b8fabSHerbert Xu }
1414c83b8fabSHerbert Xu 
141588c1f37fSAllen Pais static void br_ip4_multicast_querier_expired(struct timer_list *t)
1416cc0fdd80SLinus Lüssing {
141788c1f37fSAllen Pais 	struct net_bridge *br = from_timer(br, t, ip4_other_query.timer);
1418cc0fdd80SLinus Lüssing 
141990010b36SLinus Lüssing 	br_multicast_querier_expired(br, &br->ip4_own_query);
1420cc0fdd80SLinus Lüssing }
1421cc0fdd80SLinus Lüssing 
1422cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
142388c1f37fSAllen Pais static void br_ip6_multicast_querier_expired(struct timer_list *t)
1424cc0fdd80SLinus Lüssing {
142588c1f37fSAllen Pais 	struct net_bridge *br = from_timer(br, t, ip6_other_query.timer);
1426cc0fdd80SLinus Lüssing 
142790010b36SLinus Lüssing 	br_multicast_querier_expired(br, &br->ip6_own_query);
1428cc0fdd80SLinus Lüssing }
1429cc0fdd80SLinus Lüssing #endif
1430cc0fdd80SLinus Lüssing 
1431dc4eb53aSLinus Lüssing static void br_multicast_select_own_querier(struct net_bridge *br,
1432dc4eb53aSLinus Lüssing 					    struct br_ip *ip,
1433dc4eb53aSLinus Lüssing 					    struct sk_buff *skb)
1434dc4eb53aSLinus Lüssing {
1435dc4eb53aSLinus Lüssing 	if (ip->proto == htons(ETH_P_IP))
1436deb96566SNikolay Aleksandrov 		br->ip4_querier.addr.src.ip4 = ip_hdr(skb)->saddr;
1437dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
1438dc4eb53aSLinus Lüssing 	else
1439deb96566SNikolay Aleksandrov 		br->ip6_querier.addr.src.ip6 = ipv6_hdr(skb)->saddr;
1440dc4eb53aSLinus Lüssing #endif
1441dc4eb53aSLinus Lüssing }
1442dc4eb53aSLinus Lüssing 
14438ef2a9a5SYOSHIFUJI Hideaki static void __br_multicast_send_query(struct net_bridge *br,
14448ef2a9a5SYOSHIFUJI Hideaki 				      struct net_bridge_port *port,
1445438ef2d0SNikolay Aleksandrov 				      struct net_bridge_port_group *pg,
1446438ef2d0SNikolay Aleksandrov 				      struct br_ip *ip_dst,
1447438ef2d0SNikolay Aleksandrov 				      struct br_ip *group,
1448438ef2d0SNikolay Aleksandrov 				      bool with_srcs,
144942c11ccfSNikolay Aleksandrov 				      u8 sflag,
145042c11ccfSNikolay Aleksandrov 				      bool *need_rexmit)
1451eb1d1641SHerbert Xu {
1452438ef2d0SNikolay Aleksandrov 	bool over_lmqt = !!sflag;
1453eb1d1641SHerbert Xu 	struct sk_buff *skb;
14541080ab95SNikolay Aleksandrov 	u8 igmp_type;
1455eb1d1641SHerbert Xu 
1456438ef2d0SNikolay Aleksandrov again_under_lmqt:
1457438ef2d0SNikolay Aleksandrov 	skb = br_multicast_alloc_query(br, pg, ip_dst, group, with_srcs,
145842c11ccfSNikolay Aleksandrov 				       over_lmqt, sflag, &igmp_type,
145942c11ccfSNikolay Aleksandrov 				       need_rexmit);
1460eb1d1641SHerbert Xu 	if (!skb)
14618ef2a9a5SYOSHIFUJI Hideaki 		return;
1462eb1d1641SHerbert Xu 
1463eb1d1641SHerbert Xu 	if (port) {
1464eb1d1641SHerbert Xu 		skb->dev = port->dev;
1465a65056ecSNikolay Aleksandrov 		br_multicast_count(br, port, skb, igmp_type,
14661080ab95SNikolay Aleksandrov 				   BR_MCAST_DIR_TX);
146729a26a56SEric W. Biederman 		NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
146829a26a56SEric W. Biederman 			dev_net(port->dev), NULL, skb, NULL, skb->dev,
1469f0b4eeceSLinus Lüssing 			br_dev_queue_push_xmit);
1470438ef2d0SNikolay Aleksandrov 
1471438ef2d0SNikolay Aleksandrov 		if (over_lmqt && with_srcs && sflag) {
1472438ef2d0SNikolay Aleksandrov 			over_lmqt = false;
1473438ef2d0SNikolay Aleksandrov 			goto again_under_lmqt;
1474438ef2d0SNikolay Aleksandrov 		}
1475dc4eb53aSLinus Lüssing 	} else {
1476438ef2d0SNikolay Aleksandrov 		br_multicast_select_own_querier(br, group, skb);
1477a65056ecSNikolay Aleksandrov 		br_multicast_count(br, port, skb, igmp_type,
14781080ab95SNikolay Aleksandrov 				   BR_MCAST_DIR_RX);
1479eb1d1641SHerbert Xu 		netif_rx(skb);
14808ef2a9a5SYOSHIFUJI Hideaki 	}
1481dc4eb53aSLinus Lüssing }
1482eb1d1641SHerbert Xu 
14838ef2a9a5SYOSHIFUJI Hideaki static void br_multicast_send_query(struct net_bridge *br,
1484cc0fdd80SLinus Lüssing 				    struct net_bridge_port *port,
148590010b36SLinus Lüssing 				    struct bridge_mcast_own_query *own_query)
14868ef2a9a5SYOSHIFUJI Hideaki {
148790010b36SLinus Lüssing 	struct bridge_mcast_other_query *other_query = NULL;
14885e923585SNikolay Aleksandrov 	struct br_ip br_group;
14895e923585SNikolay Aleksandrov 	unsigned long time;
14908ef2a9a5SYOSHIFUJI Hideaki 
149113cefad2SNikolay Aleksandrov 	if (!netif_running(br->dev) ||
149213cefad2SNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_ENABLED) ||
1493675779adSNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_QUERIER))
14948ef2a9a5SYOSHIFUJI Hideaki 		return;
14958ef2a9a5SYOSHIFUJI Hideaki 
1496eab3227bSNikolay Aleksandrov 	memset(&br_group.dst, 0, sizeof(br_group.dst));
14978ef2a9a5SYOSHIFUJI Hideaki 
149890010b36SLinus Lüssing 	if (port ? (own_query == &port->ip4_own_query) :
149990010b36SLinus Lüssing 		   (own_query == &br->ip4_own_query)) {
150090010b36SLinus Lüssing 		other_query = &br->ip4_other_query;
150108b202b6SYOSHIFUJI Hideaki 		br_group.proto = htons(ETH_P_IP);
1502dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1503cc0fdd80SLinus Lüssing 	} else {
150490010b36SLinus Lüssing 		other_query = &br->ip6_other_query;
150508b202b6SYOSHIFUJI Hideaki 		br_group.proto = htons(ETH_P_IPV6);
150608b202b6SYOSHIFUJI Hideaki #endif
1507eb1d1641SHerbert Xu 	}
1508eb1d1641SHerbert Xu 
150990010b36SLinus Lüssing 	if (!other_query || timer_pending(&other_query->timer))
1510cc0fdd80SLinus Lüssing 		return;
1511cc0fdd80SLinus Lüssing 
151242c11ccfSNikolay Aleksandrov 	__br_multicast_send_query(br, port, NULL, NULL, &br_group, false, 0,
151342c11ccfSNikolay Aleksandrov 				  NULL);
1514cc0fdd80SLinus Lüssing 
1515cc0fdd80SLinus Lüssing 	time = jiffies;
151690010b36SLinus Lüssing 	time += own_query->startup_sent < br->multicast_startup_query_count ?
1517cc0fdd80SLinus Lüssing 		br->multicast_startup_query_interval :
1518cc0fdd80SLinus Lüssing 		br->multicast_query_interval;
151990010b36SLinus Lüssing 	mod_timer(&own_query->timer, time);
1520cc0fdd80SLinus Lüssing }
1521cc0fdd80SLinus Lüssing 
152290010b36SLinus Lüssing static void
152390010b36SLinus Lüssing br_multicast_port_query_expired(struct net_bridge_port *port,
152490010b36SLinus Lüssing 				struct bridge_mcast_own_query *query)
1525eb1d1641SHerbert Xu {
1526eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
1527eb1d1641SHerbert Xu 
1528eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
152902a780c0SDan Carpenter 	if (port->state == BR_STATE_DISABLED ||
153002a780c0SDan Carpenter 	    port->state == BR_STATE_BLOCKING)
1531eb1d1641SHerbert Xu 		goto out;
1532eb1d1641SHerbert Xu 
1533cc0fdd80SLinus Lüssing 	if (query->startup_sent < br->multicast_startup_query_count)
1534cc0fdd80SLinus Lüssing 		query->startup_sent++;
1535eb1d1641SHerbert Xu 
1536cc0fdd80SLinus Lüssing 	br_multicast_send_query(port->br, port, query);
1537eb1d1641SHerbert Xu 
1538eb1d1641SHerbert Xu out:
1539eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1540eb1d1641SHerbert Xu }
1541eb1d1641SHerbert Xu 
154288c1f37fSAllen Pais static void br_ip4_multicast_port_query_expired(struct timer_list *t)
1543cc0fdd80SLinus Lüssing {
154488c1f37fSAllen Pais 	struct net_bridge_port *port = from_timer(port, t, ip4_own_query.timer);
1545cc0fdd80SLinus Lüssing 
154690010b36SLinus Lüssing 	br_multicast_port_query_expired(port, &port->ip4_own_query);
1547cc0fdd80SLinus Lüssing }
1548cc0fdd80SLinus Lüssing 
1549cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
155088c1f37fSAllen Pais static void br_ip6_multicast_port_query_expired(struct timer_list *t)
1551cc0fdd80SLinus Lüssing {
155288c1f37fSAllen Pais 	struct net_bridge_port *port = from_timer(port, t, ip6_own_query.timer);
1553cc0fdd80SLinus Lüssing 
155490010b36SLinus Lüssing 	br_multicast_port_query_expired(port, &port->ip6_own_query);
1555cc0fdd80SLinus Lüssing }
1556cc0fdd80SLinus Lüssing #endif
1557cc0fdd80SLinus Lüssing 
155842c11ccfSNikolay Aleksandrov static void br_multicast_port_group_rexmit(struct timer_list *t)
155942c11ccfSNikolay Aleksandrov {
156042c11ccfSNikolay Aleksandrov 	struct net_bridge_port_group *pg = from_timer(pg, t, rexmit_timer);
156142c11ccfSNikolay Aleksandrov 	struct bridge_mcast_other_query *other_query = NULL;
1562085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
156342c11ccfSNikolay Aleksandrov 	bool need_rexmit = false;
156442c11ccfSNikolay Aleksandrov 
156542c11ccfSNikolay Aleksandrov 	spin_lock(&br->multicast_lock);
156642c11ccfSNikolay Aleksandrov 	if (!netif_running(br->dev) || hlist_unhashed(&pg->mglist) ||
156742c11ccfSNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_ENABLED) ||
156842c11ccfSNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_QUERIER))
156942c11ccfSNikolay Aleksandrov 		goto out;
157042c11ccfSNikolay Aleksandrov 
1571085b53c8SNikolay Aleksandrov 	if (pg->key.addr.proto == htons(ETH_P_IP))
157242c11ccfSNikolay Aleksandrov 		other_query = &br->ip4_other_query;
157342c11ccfSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
157442c11ccfSNikolay Aleksandrov 	else
157542c11ccfSNikolay Aleksandrov 		other_query = &br->ip6_other_query;
157642c11ccfSNikolay Aleksandrov #endif
157742c11ccfSNikolay Aleksandrov 
157842c11ccfSNikolay Aleksandrov 	if (!other_query || timer_pending(&other_query->timer))
157942c11ccfSNikolay Aleksandrov 		goto out;
158042c11ccfSNikolay Aleksandrov 
158142c11ccfSNikolay Aleksandrov 	if (pg->grp_query_rexmit_cnt) {
158242c11ccfSNikolay Aleksandrov 		pg->grp_query_rexmit_cnt--;
1583085b53c8SNikolay Aleksandrov 		__br_multicast_send_query(br, pg->key.port, pg, &pg->key.addr,
1584085b53c8SNikolay Aleksandrov 					  &pg->key.addr, false, 1, NULL);
158542c11ccfSNikolay Aleksandrov 	}
1586085b53c8SNikolay Aleksandrov 	__br_multicast_send_query(br, pg->key.port, pg, &pg->key.addr,
1587085b53c8SNikolay Aleksandrov 				  &pg->key.addr, true, 0, &need_rexmit);
158842c11ccfSNikolay Aleksandrov 
158942c11ccfSNikolay Aleksandrov 	if (pg->grp_query_rexmit_cnt || need_rexmit)
159042c11ccfSNikolay Aleksandrov 		mod_timer(&pg->rexmit_timer, jiffies +
159142c11ccfSNikolay Aleksandrov 					     br->multicast_last_member_interval);
159242c11ccfSNikolay Aleksandrov out:
159342c11ccfSNikolay Aleksandrov 	spin_unlock(&br->multicast_lock);
159442c11ccfSNikolay Aleksandrov }
159542c11ccfSNikolay Aleksandrov 
1596147c1e9bSNogah Frankel static void br_mc_disabled_update(struct net_device *dev, bool value)
1597147c1e9bSNogah Frankel {
1598147c1e9bSNogah Frankel 	struct switchdev_attr attr = {
1599147c1e9bSNogah Frankel 		.orig_dev = dev,
1600147c1e9bSNogah Frankel 		.id = SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED,
1601147c1e9bSNogah Frankel 		.flags = SWITCHDEV_F_DEFER,
160213cefad2SNikolay Aleksandrov 		.u.mc_disabled = !value,
1603147c1e9bSNogah Frankel 	};
1604147c1e9bSNogah Frankel 
1605147c1e9bSNogah Frankel 	switchdev_port_attr_set(dev, &attr);
1606147c1e9bSNogah Frankel }
1607147c1e9bSNogah Frankel 
16081080ab95SNikolay Aleksandrov int br_multicast_add_port(struct net_bridge_port *port)
1609eb1d1641SHerbert Xu {
16107f0aec7aSNikolay Aleksandrov 	port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
1611eb1d1641SHerbert Xu 
161288c1f37fSAllen Pais 	timer_setup(&port->multicast_router_timer,
161388c1f37fSAllen Pais 		    br_multicast_router_expired, 0);
161488c1f37fSAllen Pais 	timer_setup(&port->ip4_own_query.timer,
161588c1f37fSAllen Pais 		    br_ip4_multicast_port_query_expired, 0);
1616cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
161788c1f37fSAllen Pais 	timer_setup(&port->ip6_own_query.timer,
161888c1f37fSAllen Pais 		    br_ip6_multicast_port_query_expired, 0);
1619cc0fdd80SLinus Lüssing #endif
162013cefad2SNikolay Aleksandrov 	br_mc_disabled_update(port->dev,
162113cefad2SNikolay Aleksandrov 			      br_opt_get(port->br, BROPT_MULTICAST_ENABLED));
1622147c1e9bSNogah Frankel 
16231080ab95SNikolay Aleksandrov 	port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
16241080ab95SNikolay Aleksandrov 	if (!port->mcast_stats)
16251080ab95SNikolay Aleksandrov 		return -ENOMEM;
16261080ab95SNikolay Aleksandrov 
16271080ab95SNikolay Aleksandrov 	return 0;
1628eb1d1641SHerbert Xu }
1629eb1d1641SHerbert Xu 
1630eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port)
1631eb1d1641SHerbert Xu {
1632e10177abSSatish Ashok 	struct net_bridge *br = port->br;
1633e10177abSSatish Ashok 	struct net_bridge_port_group *pg;
1634e12cec65SNikolay Aleksandrov 	HLIST_HEAD(deleted_head);
1635e10177abSSatish Ashok 	struct hlist_node *n;
1636e10177abSSatish Ashok 
1637e10177abSSatish Ashok 	/* Take care of the remaining groups, only perm ones should be left */
1638e10177abSSatish Ashok 	spin_lock_bh(&br->multicast_lock);
1639e10177abSSatish Ashok 	hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
1640681590bdSNikolay Aleksandrov 		br_multicast_find_del_pg(br, pg);
1641e12cec65SNikolay Aleksandrov 	hlist_move_list(&br->mcast_gc_list, &deleted_head);
1642e10177abSSatish Ashok 	spin_unlock_bh(&br->multicast_lock);
1643e12cec65SNikolay Aleksandrov 	br_multicast_gc(&deleted_head);
1644eb1d1641SHerbert Xu 	del_timer_sync(&port->multicast_router_timer);
16451080ab95SNikolay Aleksandrov 	free_percpu(port->mcast_stats);
1646eb1d1641SHerbert Xu }
1647eb1d1641SHerbert Xu 
164890010b36SLinus Lüssing static void br_multicast_enable(struct bridge_mcast_own_query *query)
1649561f1103SHerbert Xu {
1650cc0fdd80SLinus Lüssing 	query->startup_sent = 0;
1651561f1103SHerbert Xu 
1652cc0fdd80SLinus Lüssing 	if (try_to_del_timer_sync(&query->timer) >= 0 ||
1653cc0fdd80SLinus Lüssing 	    del_timer(&query->timer))
1654cc0fdd80SLinus Lüssing 		mod_timer(&query->timer, jiffies);
1655561f1103SHerbert Xu }
1656561f1103SHerbert Xu 
16577cb3f921SNikolay Aleksandrov static void __br_multicast_enable_port(struct net_bridge_port *port)
1658eb1d1641SHerbert Xu {
1659eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
1660eb1d1641SHerbert Xu 
166113cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED) || !netif_running(br->dev))
16627cb3f921SNikolay Aleksandrov 		return;
1663eb1d1641SHerbert Xu 
166490010b36SLinus Lüssing 	br_multicast_enable(&port->ip4_own_query);
1665cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
166690010b36SLinus Lüssing 	br_multicast_enable(&port->ip6_own_query);
1667cc0fdd80SLinus Lüssing #endif
16687f0aec7aSNikolay Aleksandrov 	if (port->multicast_router == MDB_RTR_TYPE_PERM &&
16697f0aec7aSNikolay Aleksandrov 	    hlist_unhashed(&port->rlist))
1670754bc547SSatish Ashok 		br_multicast_add_router(br, port);
16717cb3f921SNikolay Aleksandrov }
1672eb1d1641SHerbert Xu 
16737cb3f921SNikolay Aleksandrov void br_multicast_enable_port(struct net_bridge_port *port)
16747cb3f921SNikolay Aleksandrov {
16757cb3f921SNikolay Aleksandrov 	struct net_bridge *br = port->br;
16767cb3f921SNikolay Aleksandrov 
16777cb3f921SNikolay Aleksandrov 	spin_lock(&br->multicast_lock);
16787cb3f921SNikolay Aleksandrov 	__br_multicast_enable_port(port);
1679eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1680eb1d1641SHerbert Xu }
1681eb1d1641SHerbert Xu 
1682eb1d1641SHerbert Xu void br_multicast_disable_port(struct net_bridge_port *port)
1683eb1d1641SHerbert Xu {
1684eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
1685eb1d1641SHerbert Xu 	struct net_bridge_port_group *pg;
1686b67bfe0dSSasha Levin 	struct hlist_node *n;
1687eb1d1641SHerbert Xu 
1688eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
1689b67bfe0dSSasha Levin 	hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
16909d06b6d8SElad Raz 		if (!(pg->flags & MDB_PG_FLAGS_PERMANENT))
1691681590bdSNikolay Aleksandrov 			br_multicast_find_del_pg(br, pg);
1692eb1d1641SHerbert Xu 
1693f12e7d95SNogah Frankel 	__del_port_router(port);
1694f12e7d95SNogah Frankel 
1695eb1d1641SHerbert Xu 	del_timer(&port->multicast_router_timer);
169690010b36SLinus Lüssing 	del_timer(&port->ip4_own_query.timer);
1697cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
169890010b36SLinus Lüssing 	del_timer(&port->ip6_own_query.timer);
1699cc0fdd80SLinus Lüssing #endif
1700eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1701eb1d1641SHerbert Xu }
1702eb1d1641SHerbert Xu 
1703e6231bcaSNikolay Aleksandrov static int __grp_src_delete_marked(struct net_bridge_port_group *pg)
1704e6231bcaSNikolay Aleksandrov {
1705e6231bcaSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
1706e6231bcaSNikolay Aleksandrov 	struct hlist_node *tmp;
1707e6231bcaSNikolay Aleksandrov 	int deleted = 0;
1708e6231bcaSNikolay Aleksandrov 
1709e6231bcaSNikolay Aleksandrov 	hlist_for_each_entry_safe(ent, tmp, &pg->src_list, node)
1710e6231bcaSNikolay Aleksandrov 		if (ent->flags & BR_SGRP_F_DELETE) {
1711d5a10222SNikolay Aleksandrov 			br_multicast_del_group_src(ent, false);
1712e6231bcaSNikolay Aleksandrov 			deleted++;
1713e6231bcaSNikolay Aleksandrov 		}
1714e6231bcaSNikolay Aleksandrov 
1715e6231bcaSNikolay Aleksandrov 	return deleted;
1716e6231bcaSNikolay Aleksandrov }
1717e6231bcaSNikolay Aleksandrov 
1718b0812368SNikolay Aleksandrov static void __grp_src_mod_timer(struct net_bridge_group_src *src,
1719b0812368SNikolay Aleksandrov 				unsigned long expires)
1720b0812368SNikolay Aleksandrov {
1721b0812368SNikolay Aleksandrov 	mod_timer(&src->timer, expires);
1722b0812368SNikolay Aleksandrov 	br_multicast_fwd_src_handle(src);
1723b0812368SNikolay Aleksandrov }
1724b0812368SNikolay Aleksandrov 
17255bf1e00bSNikolay Aleksandrov static void __grp_src_query_marked_and_rexmit(struct net_bridge_port_group *pg)
17265bf1e00bSNikolay Aleksandrov {
17275bf1e00bSNikolay Aleksandrov 	struct bridge_mcast_other_query *other_query = NULL;
1728085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
17295bf1e00bSNikolay Aleksandrov 	u32 lmqc = br->multicast_last_member_count;
17305bf1e00bSNikolay Aleksandrov 	unsigned long lmqt, lmi, now = jiffies;
17315bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
17325bf1e00bSNikolay Aleksandrov 
17335bf1e00bSNikolay Aleksandrov 	if (!netif_running(br->dev) ||
17345bf1e00bSNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_ENABLED))
17355bf1e00bSNikolay Aleksandrov 		return;
17365bf1e00bSNikolay Aleksandrov 
1737085b53c8SNikolay Aleksandrov 	if (pg->key.addr.proto == htons(ETH_P_IP))
17385bf1e00bSNikolay Aleksandrov 		other_query = &br->ip4_other_query;
17395bf1e00bSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
17405bf1e00bSNikolay Aleksandrov 	else
17415bf1e00bSNikolay Aleksandrov 		other_query = &br->ip6_other_query;
17425bf1e00bSNikolay Aleksandrov #endif
17435bf1e00bSNikolay Aleksandrov 
17445bf1e00bSNikolay Aleksandrov 	lmqt = now + br_multicast_lmqt(br);
17455bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node) {
17465bf1e00bSNikolay Aleksandrov 		if (ent->flags & BR_SGRP_F_SEND) {
17475bf1e00bSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_SEND;
17485bf1e00bSNikolay Aleksandrov 			if (ent->timer.expires > lmqt) {
17495bf1e00bSNikolay Aleksandrov 				if (br_opt_get(br, BROPT_MULTICAST_QUERIER) &&
17505bf1e00bSNikolay Aleksandrov 				    other_query &&
17515bf1e00bSNikolay Aleksandrov 				    !timer_pending(&other_query->timer))
17525bf1e00bSNikolay Aleksandrov 					ent->src_query_rexmit_cnt = lmqc;
1753b0812368SNikolay Aleksandrov 				__grp_src_mod_timer(ent, lmqt);
17545bf1e00bSNikolay Aleksandrov 			}
17555bf1e00bSNikolay Aleksandrov 		}
17565bf1e00bSNikolay Aleksandrov 	}
17575bf1e00bSNikolay Aleksandrov 
17585bf1e00bSNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_QUERIER) ||
17595bf1e00bSNikolay Aleksandrov 	    !other_query || timer_pending(&other_query->timer))
17605bf1e00bSNikolay Aleksandrov 		return;
17615bf1e00bSNikolay Aleksandrov 
1762085b53c8SNikolay Aleksandrov 	__br_multicast_send_query(br, pg->key.port, pg, &pg->key.addr,
1763085b53c8SNikolay Aleksandrov 				  &pg->key.addr, true, 1, NULL);
17645bf1e00bSNikolay Aleksandrov 
17655bf1e00bSNikolay Aleksandrov 	lmi = now + br->multicast_last_member_interval;
17665bf1e00bSNikolay Aleksandrov 	if (!timer_pending(&pg->rexmit_timer) ||
17675bf1e00bSNikolay Aleksandrov 	    time_after(pg->rexmit_timer.expires, lmi))
17685bf1e00bSNikolay Aleksandrov 		mod_timer(&pg->rexmit_timer, lmi);
17695bf1e00bSNikolay Aleksandrov }
17705bf1e00bSNikolay Aleksandrov 
17715bf1e00bSNikolay Aleksandrov static void __grp_send_query_and_rexmit(struct net_bridge_port_group *pg)
17725bf1e00bSNikolay Aleksandrov {
17735bf1e00bSNikolay Aleksandrov 	struct bridge_mcast_other_query *other_query = NULL;
1774085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
17755bf1e00bSNikolay Aleksandrov 	unsigned long now = jiffies, lmi;
17765bf1e00bSNikolay Aleksandrov 
17775bf1e00bSNikolay Aleksandrov 	if (!netif_running(br->dev) ||
17785bf1e00bSNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_ENABLED))
17795bf1e00bSNikolay Aleksandrov 		return;
17805bf1e00bSNikolay Aleksandrov 
1781085b53c8SNikolay Aleksandrov 	if (pg->key.addr.proto == htons(ETH_P_IP))
17825bf1e00bSNikolay Aleksandrov 		other_query = &br->ip4_other_query;
17835bf1e00bSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
17845bf1e00bSNikolay Aleksandrov 	else
17855bf1e00bSNikolay Aleksandrov 		other_query = &br->ip6_other_query;
17865bf1e00bSNikolay Aleksandrov #endif
17875bf1e00bSNikolay Aleksandrov 
17885bf1e00bSNikolay Aleksandrov 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER) &&
17895bf1e00bSNikolay Aleksandrov 	    other_query && !timer_pending(&other_query->timer)) {
17905bf1e00bSNikolay Aleksandrov 		lmi = now + br->multicast_last_member_interval;
17915bf1e00bSNikolay Aleksandrov 		pg->grp_query_rexmit_cnt = br->multicast_last_member_count - 1;
1792085b53c8SNikolay Aleksandrov 		__br_multicast_send_query(br, pg->key.port, pg, &pg->key.addr,
1793085b53c8SNikolay Aleksandrov 					  &pg->key.addr, false, 0, NULL);
17945bf1e00bSNikolay Aleksandrov 		if (!timer_pending(&pg->rexmit_timer) ||
17955bf1e00bSNikolay Aleksandrov 		    time_after(pg->rexmit_timer.expires, lmi))
17965bf1e00bSNikolay Aleksandrov 			mod_timer(&pg->rexmit_timer, lmi);
17975bf1e00bSNikolay Aleksandrov 	}
17985bf1e00bSNikolay Aleksandrov 
17995bf1e00bSNikolay Aleksandrov 	if (pg->filter_mode == MCAST_EXCLUDE &&
18005bf1e00bSNikolay Aleksandrov 	    (!timer_pending(&pg->timer) ||
18015bf1e00bSNikolay Aleksandrov 	     time_after(pg->timer.expires, now + br_multicast_lmqt(br))))
18025bf1e00bSNikolay Aleksandrov 		mod_timer(&pg->timer, now + br_multicast_lmqt(br));
18035bf1e00bSNikolay Aleksandrov }
18045bf1e00bSNikolay Aleksandrov 
18050436862eSNikolay Aleksandrov /* State          Msg type      New state                Actions
18060436862eSNikolay Aleksandrov  * INCLUDE (A)    IS_IN (B)     INCLUDE (A+B)            (B)=GMI
18070436862eSNikolay Aleksandrov  * INCLUDE (A)    ALLOW (B)     INCLUDE (A+B)            (B)=GMI
18080436862eSNikolay Aleksandrov  * EXCLUDE (X,Y)  ALLOW (A)     EXCLUDE (X+A,Y-A)        (A)=GMI
18090436862eSNikolay Aleksandrov  */
181054bea721SNikolay Aleksandrov static bool br_multicast_isinc_allow(struct net_bridge_port_group *pg, void *h_addr,
1811474ddb37SNikolay Aleksandrov 				     void *srcs, u32 nsrcs, size_t addr_size,
1812474ddb37SNikolay Aleksandrov 				     int grec_type)
18130436862eSNikolay Aleksandrov {
1814085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
18150436862eSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
18160436862eSNikolay Aleksandrov 	unsigned long now = jiffies;
18170436862eSNikolay Aleksandrov 	bool changed = false;
18180436862eSNikolay Aleksandrov 	struct br_ip src_ip;
18190436862eSNikolay Aleksandrov 	u32 src_idx;
18200436862eSNikolay Aleksandrov 
18210436862eSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
1822085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
18230436862eSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
1824e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
18250436862eSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
18260436862eSNikolay Aleksandrov 		if (!ent) {
18270436862eSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
18280436862eSNikolay Aleksandrov 			if (ent)
18290436862eSNikolay Aleksandrov 				changed = true;
18300436862eSNikolay Aleksandrov 		}
18310436862eSNikolay Aleksandrov 
18320436862eSNikolay Aleksandrov 		if (ent)
1833b0812368SNikolay Aleksandrov 			__grp_src_mod_timer(ent, now + br_multicast_gmi(br));
18340436862eSNikolay Aleksandrov 	}
18350436862eSNikolay Aleksandrov 
1836474ddb37SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
1837474ddb37SNikolay Aleksandrov 		changed = true;
1838474ddb37SNikolay Aleksandrov 
18390436862eSNikolay Aleksandrov 	return changed;
18400436862eSNikolay Aleksandrov }
18410436862eSNikolay Aleksandrov 
1842e6231bcaSNikolay Aleksandrov /* State          Msg type      New state                Actions
1843e6231bcaSNikolay Aleksandrov  * INCLUDE (A)    IS_EX (B)     EXCLUDE (A*B,B-A)        (B-A)=0
1844e6231bcaSNikolay Aleksandrov  *                                                       Delete (A-B)
1845e6231bcaSNikolay Aleksandrov  *                                                       Group Timer=GMI
1846e6231bcaSNikolay Aleksandrov  */
184754bea721SNikolay Aleksandrov static void __grp_src_isexc_incl(struct net_bridge_port_group *pg, void *h_addr,
1848ddc255d9SNikolay Aleksandrov 				 void *srcs, u32 nsrcs, size_t addr_size,
1849ddc255d9SNikolay Aleksandrov 				 int grec_type)
1850e6231bcaSNikolay Aleksandrov {
1851e6231bcaSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
1852e6231bcaSNikolay Aleksandrov 	struct br_ip src_ip;
1853e6231bcaSNikolay Aleksandrov 	u32 src_idx;
1854e6231bcaSNikolay Aleksandrov 
1855e6231bcaSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
1856e6231bcaSNikolay Aleksandrov 		ent->flags |= BR_SGRP_F_DELETE;
1857e6231bcaSNikolay Aleksandrov 
1858e6231bcaSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
1859085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
1860e6231bcaSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
1861e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
1862e6231bcaSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
1863e6231bcaSNikolay Aleksandrov 		if (ent)
1864e6231bcaSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_DELETE;
1865e6231bcaSNikolay Aleksandrov 		else
1866b0812368SNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
1867b0812368SNikolay Aleksandrov 		if (ent)
1868b0812368SNikolay Aleksandrov 			br_multicast_fwd_src_handle(ent);
1869e6231bcaSNikolay Aleksandrov 	}
1870e6231bcaSNikolay Aleksandrov 
1871ddc255d9SNikolay Aleksandrov 	br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type);
1872ddc255d9SNikolay Aleksandrov 
1873e6231bcaSNikolay Aleksandrov 	__grp_src_delete_marked(pg);
1874e6231bcaSNikolay Aleksandrov }
1875e6231bcaSNikolay Aleksandrov 
1876e6231bcaSNikolay Aleksandrov /* State          Msg type      New state                Actions
1877e6231bcaSNikolay Aleksandrov  * EXCLUDE (X,Y)  IS_EX (A)     EXCLUDE (A-Y,Y*A)        (A-X-Y)=GMI
1878e6231bcaSNikolay Aleksandrov  *                                                       Delete (X-A)
1879e6231bcaSNikolay Aleksandrov  *                                                       Delete (Y-A)
1880e6231bcaSNikolay Aleksandrov  *                                                       Group Timer=GMI
1881e6231bcaSNikolay Aleksandrov  */
188254bea721SNikolay Aleksandrov static bool __grp_src_isexc_excl(struct net_bridge_port_group *pg, void *h_addr,
1883ddc255d9SNikolay Aleksandrov 				 void *srcs, u32 nsrcs, size_t addr_size,
1884ddc255d9SNikolay Aleksandrov 				 int grec_type)
1885e6231bcaSNikolay Aleksandrov {
1886085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
1887e6231bcaSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
1888e6231bcaSNikolay Aleksandrov 	unsigned long now = jiffies;
1889e6231bcaSNikolay Aleksandrov 	bool changed = false;
1890e6231bcaSNikolay Aleksandrov 	struct br_ip src_ip;
1891e6231bcaSNikolay Aleksandrov 	u32 src_idx;
1892e6231bcaSNikolay Aleksandrov 
1893e6231bcaSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
1894e6231bcaSNikolay Aleksandrov 		ent->flags |= BR_SGRP_F_DELETE;
1895e6231bcaSNikolay Aleksandrov 
1896e6231bcaSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
1897085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
1898e6231bcaSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
1899e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
1900e6231bcaSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
1901e6231bcaSNikolay Aleksandrov 		if (ent) {
1902e6231bcaSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_DELETE;
1903e6231bcaSNikolay Aleksandrov 		} else {
1904e6231bcaSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
1905e6231bcaSNikolay Aleksandrov 			if (ent) {
1906b0812368SNikolay Aleksandrov 				__grp_src_mod_timer(ent,
1907e6231bcaSNikolay Aleksandrov 						    now + br_multicast_gmi(br));
1908e6231bcaSNikolay Aleksandrov 				changed = true;
1909e6231bcaSNikolay Aleksandrov 			}
1910e6231bcaSNikolay Aleksandrov 		}
1911e6231bcaSNikolay Aleksandrov 	}
1912e6231bcaSNikolay Aleksandrov 
1913ddc255d9SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
1914ddc255d9SNikolay Aleksandrov 		changed = true;
1915ddc255d9SNikolay Aleksandrov 
1916e6231bcaSNikolay Aleksandrov 	if (__grp_src_delete_marked(pg))
1917e6231bcaSNikolay Aleksandrov 		changed = true;
1918e6231bcaSNikolay Aleksandrov 
1919e6231bcaSNikolay Aleksandrov 	return changed;
1920e6231bcaSNikolay Aleksandrov }
1921e6231bcaSNikolay Aleksandrov 
192254bea721SNikolay Aleksandrov static bool br_multicast_isexc(struct net_bridge_port_group *pg, void *h_addr,
1923ddc255d9SNikolay Aleksandrov 			       void *srcs, u32 nsrcs, size_t addr_size,
1924ddc255d9SNikolay Aleksandrov 			       int grec_type)
1925e6231bcaSNikolay Aleksandrov {
1926085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
1927e6231bcaSNikolay Aleksandrov 	bool changed = false;
1928e6231bcaSNikolay Aleksandrov 
1929e6231bcaSNikolay Aleksandrov 	switch (pg->filter_mode) {
1930e6231bcaSNikolay Aleksandrov 	case MCAST_INCLUDE:
1931ddc255d9SNikolay Aleksandrov 		__grp_src_isexc_incl(pg, h_addr, srcs, nsrcs, addr_size,
1932ddc255d9SNikolay Aleksandrov 				     grec_type);
19338266a049SNikolay Aleksandrov 		br_multicast_star_g_handle_mode(pg, MCAST_EXCLUDE);
1934e6231bcaSNikolay Aleksandrov 		changed = true;
1935e6231bcaSNikolay Aleksandrov 		break;
1936e6231bcaSNikolay Aleksandrov 	case MCAST_EXCLUDE:
1937ddc255d9SNikolay Aleksandrov 		changed = __grp_src_isexc_excl(pg, h_addr, srcs, nsrcs, addr_size,
1938ddc255d9SNikolay Aleksandrov 					       grec_type);
1939e6231bcaSNikolay Aleksandrov 		break;
1940e6231bcaSNikolay Aleksandrov 	}
1941e6231bcaSNikolay Aleksandrov 
1942e6231bcaSNikolay Aleksandrov 	pg->filter_mode = MCAST_EXCLUDE;
1943e6231bcaSNikolay Aleksandrov 	mod_timer(&pg->timer, jiffies + br_multicast_gmi(br));
1944e6231bcaSNikolay Aleksandrov 
1945e6231bcaSNikolay Aleksandrov 	return changed;
1946e6231bcaSNikolay Aleksandrov }
1947e6231bcaSNikolay Aleksandrov 
19485bf1e00bSNikolay Aleksandrov /* State          Msg type      New state                Actions
19495bf1e00bSNikolay Aleksandrov  * INCLUDE (A)    TO_IN (B)     INCLUDE (A+B)            (B)=GMI
19505bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G,A-B)
19515bf1e00bSNikolay Aleksandrov  */
195254bea721SNikolay Aleksandrov static bool __grp_src_toin_incl(struct net_bridge_port_group *pg, void *h_addr,
1953ddc255d9SNikolay Aleksandrov 				void *srcs, u32 nsrcs, size_t addr_size,
1954ddc255d9SNikolay Aleksandrov 				int grec_type)
19555bf1e00bSNikolay Aleksandrov {
1956085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
19575bf1e00bSNikolay Aleksandrov 	u32 src_idx, to_send = pg->src_ents;
19585bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
19595bf1e00bSNikolay Aleksandrov 	unsigned long now = jiffies;
19605bf1e00bSNikolay Aleksandrov 	bool changed = false;
19615bf1e00bSNikolay Aleksandrov 	struct br_ip src_ip;
19625bf1e00bSNikolay Aleksandrov 
19635bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
19645bf1e00bSNikolay Aleksandrov 		ent->flags |= BR_SGRP_F_SEND;
19655bf1e00bSNikolay Aleksandrov 
19665bf1e00bSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
1967085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
19685bf1e00bSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
1969e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
19705bf1e00bSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
19715bf1e00bSNikolay Aleksandrov 		if (ent) {
19725bf1e00bSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_SEND;
19735bf1e00bSNikolay Aleksandrov 			to_send--;
19745bf1e00bSNikolay Aleksandrov 		} else {
19755bf1e00bSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
19765bf1e00bSNikolay Aleksandrov 			if (ent)
19775bf1e00bSNikolay Aleksandrov 				changed = true;
19785bf1e00bSNikolay Aleksandrov 		}
19795bf1e00bSNikolay Aleksandrov 		if (ent)
1980b0812368SNikolay Aleksandrov 			__grp_src_mod_timer(ent, now + br_multicast_gmi(br));
19815bf1e00bSNikolay Aleksandrov 	}
19825bf1e00bSNikolay Aleksandrov 
1983ddc255d9SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
1984ddc255d9SNikolay Aleksandrov 		changed = true;
1985ddc255d9SNikolay Aleksandrov 
19865bf1e00bSNikolay Aleksandrov 	if (to_send)
19875bf1e00bSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
19885bf1e00bSNikolay Aleksandrov 
19895bf1e00bSNikolay Aleksandrov 	return changed;
19905bf1e00bSNikolay Aleksandrov }
19915bf1e00bSNikolay Aleksandrov 
19925bf1e00bSNikolay Aleksandrov /* State          Msg type      New state                Actions
19935bf1e00bSNikolay Aleksandrov  * EXCLUDE (X,Y)  TO_IN (A)     EXCLUDE (X+A,Y-A)        (A)=GMI
19945bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G,X-A)
19955bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G)
19965bf1e00bSNikolay Aleksandrov  */
199754bea721SNikolay Aleksandrov static bool __grp_src_toin_excl(struct net_bridge_port_group *pg, void *h_addr,
1998ddc255d9SNikolay Aleksandrov 				void *srcs, u32 nsrcs, size_t addr_size,
1999ddc255d9SNikolay Aleksandrov 				int grec_type)
20005bf1e00bSNikolay Aleksandrov {
2001085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
20025bf1e00bSNikolay Aleksandrov 	u32 src_idx, to_send = pg->src_ents;
20035bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
20045bf1e00bSNikolay Aleksandrov 	unsigned long now = jiffies;
20055bf1e00bSNikolay Aleksandrov 	bool changed = false;
20065bf1e00bSNikolay Aleksandrov 	struct br_ip src_ip;
20075bf1e00bSNikolay Aleksandrov 
20085bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
20095bf1e00bSNikolay Aleksandrov 		if (timer_pending(&ent->timer))
20105bf1e00bSNikolay Aleksandrov 			ent->flags |= BR_SGRP_F_SEND;
20115bf1e00bSNikolay Aleksandrov 
20125bf1e00bSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2013085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
20145bf1e00bSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2015e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
20165bf1e00bSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
20175bf1e00bSNikolay Aleksandrov 		if (ent) {
20185bf1e00bSNikolay Aleksandrov 			if (timer_pending(&ent->timer)) {
20195bf1e00bSNikolay Aleksandrov 				ent->flags &= ~BR_SGRP_F_SEND;
20205bf1e00bSNikolay Aleksandrov 				to_send--;
20215bf1e00bSNikolay Aleksandrov 			}
20225bf1e00bSNikolay Aleksandrov 		} else {
20235bf1e00bSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
20245bf1e00bSNikolay Aleksandrov 			if (ent)
20255bf1e00bSNikolay Aleksandrov 				changed = true;
20265bf1e00bSNikolay Aleksandrov 		}
20275bf1e00bSNikolay Aleksandrov 		if (ent)
2028b0812368SNikolay Aleksandrov 			__grp_src_mod_timer(ent, now + br_multicast_gmi(br));
20295bf1e00bSNikolay Aleksandrov 	}
20305bf1e00bSNikolay Aleksandrov 
2031ddc255d9SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
2032ddc255d9SNikolay Aleksandrov 		changed = true;
2033ddc255d9SNikolay Aleksandrov 
20345bf1e00bSNikolay Aleksandrov 	if (to_send)
20355bf1e00bSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
20365bf1e00bSNikolay Aleksandrov 
20375bf1e00bSNikolay Aleksandrov 	__grp_send_query_and_rexmit(pg);
20385bf1e00bSNikolay Aleksandrov 
20395bf1e00bSNikolay Aleksandrov 	return changed;
20405bf1e00bSNikolay Aleksandrov }
20415bf1e00bSNikolay Aleksandrov 
204254bea721SNikolay Aleksandrov static bool br_multicast_toin(struct net_bridge_port_group *pg, void *h_addr,
2043ddc255d9SNikolay Aleksandrov 			      void *srcs, u32 nsrcs, size_t addr_size,
2044ddc255d9SNikolay Aleksandrov 			      int grec_type)
20455bf1e00bSNikolay Aleksandrov {
20465bf1e00bSNikolay Aleksandrov 	bool changed = false;
20475bf1e00bSNikolay Aleksandrov 
20485bf1e00bSNikolay Aleksandrov 	switch (pg->filter_mode) {
20495bf1e00bSNikolay Aleksandrov 	case MCAST_INCLUDE:
2050ddc255d9SNikolay Aleksandrov 		changed = __grp_src_toin_incl(pg, h_addr, srcs, nsrcs, addr_size,
2051ddc255d9SNikolay Aleksandrov 					      grec_type);
20525bf1e00bSNikolay Aleksandrov 		break;
20535bf1e00bSNikolay Aleksandrov 	case MCAST_EXCLUDE:
2054ddc255d9SNikolay Aleksandrov 		changed = __grp_src_toin_excl(pg, h_addr, srcs, nsrcs, addr_size,
2055ddc255d9SNikolay Aleksandrov 					      grec_type);
20565bf1e00bSNikolay Aleksandrov 		break;
20575bf1e00bSNikolay Aleksandrov 	}
20585bf1e00bSNikolay Aleksandrov 
2059ddc255d9SNikolay Aleksandrov 	if (br_multicast_eht_should_del_pg(pg)) {
2060d5a10222SNikolay Aleksandrov 		pg->flags |= MDB_PG_FLAGS_FAST_LEAVE;
2061ddc255d9SNikolay Aleksandrov 		br_multicast_find_del_pg(pg->key.port->br, pg);
2062ddc255d9SNikolay Aleksandrov 		/* a notification has already been sent and we shouldn't
2063ddc255d9SNikolay Aleksandrov 		 * access pg after the delete so we have to return false
2064ddc255d9SNikolay Aleksandrov 		 */
2065ddc255d9SNikolay Aleksandrov 		changed = false;
2066ddc255d9SNikolay Aleksandrov 	}
2067ddc255d9SNikolay Aleksandrov 
20685bf1e00bSNikolay Aleksandrov 	return changed;
20695bf1e00bSNikolay Aleksandrov }
20705bf1e00bSNikolay Aleksandrov 
20715bf1e00bSNikolay Aleksandrov /* State          Msg type      New state                Actions
20725bf1e00bSNikolay Aleksandrov  * INCLUDE (A)    TO_EX (B)     EXCLUDE (A*B,B-A)        (B-A)=0
20735bf1e00bSNikolay Aleksandrov  *                                                       Delete (A-B)
20745bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G,A*B)
20755bf1e00bSNikolay Aleksandrov  *                                                       Group Timer=GMI
20765bf1e00bSNikolay Aleksandrov  */
207754bea721SNikolay Aleksandrov static void __grp_src_toex_incl(struct net_bridge_port_group *pg, void *h_addr,
2078ddc255d9SNikolay Aleksandrov 				void *srcs, u32 nsrcs, size_t addr_size,
2079ddc255d9SNikolay Aleksandrov 				int grec_type)
20805bf1e00bSNikolay Aleksandrov {
20815bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
20825bf1e00bSNikolay Aleksandrov 	u32 src_idx, to_send = 0;
20835bf1e00bSNikolay Aleksandrov 	struct br_ip src_ip;
20845bf1e00bSNikolay Aleksandrov 
20855bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
20865bf1e00bSNikolay Aleksandrov 		ent->flags = (ent->flags & ~BR_SGRP_F_SEND) | BR_SGRP_F_DELETE;
20875bf1e00bSNikolay Aleksandrov 
20885bf1e00bSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2089085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
20905bf1e00bSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2091e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
20925bf1e00bSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
20935bf1e00bSNikolay Aleksandrov 		if (ent) {
20945bf1e00bSNikolay Aleksandrov 			ent->flags = (ent->flags & ~BR_SGRP_F_DELETE) |
20955bf1e00bSNikolay Aleksandrov 				     BR_SGRP_F_SEND;
20965bf1e00bSNikolay Aleksandrov 			to_send++;
20975bf1e00bSNikolay Aleksandrov 		} else {
2098b0812368SNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
20995bf1e00bSNikolay Aleksandrov 		}
2100b0812368SNikolay Aleksandrov 		if (ent)
2101b0812368SNikolay Aleksandrov 			br_multicast_fwd_src_handle(ent);
21025bf1e00bSNikolay Aleksandrov 	}
21035bf1e00bSNikolay Aleksandrov 
2104ddc255d9SNikolay Aleksandrov 	br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type);
2105ddc255d9SNikolay Aleksandrov 
21065bf1e00bSNikolay Aleksandrov 	__grp_src_delete_marked(pg);
21075bf1e00bSNikolay Aleksandrov 	if (to_send)
21085bf1e00bSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
21095bf1e00bSNikolay Aleksandrov }
21105bf1e00bSNikolay Aleksandrov 
21115bf1e00bSNikolay Aleksandrov /* State          Msg type      New state                Actions
21125bf1e00bSNikolay Aleksandrov  * EXCLUDE (X,Y)  TO_EX (A)     EXCLUDE (A-Y,Y*A)        (A-X-Y)=Group Timer
21135bf1e00bSNikolay Aleksandrov  *                                                       Delete (X-A)
21145bf1e00bSNikolay Aleksandrov  *                                                       Delete (Y-A)
21155bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G,A-Y)
21165bf1e00bSNikolay Aleksandrov  *                                                       Group Timer=GMI
21175bf1e00bSNikolay Aleksandrov  */
211854bea721SNikolay Aleksandrov static bool __grp_src_toex_excl(struct net_bridge_port_group *pg, void *h_addr,
2119ddc255d9SNikolay Aleksandrov 				void *srcs, u32 nsrcs, size_t addr_size,
2120ddc255d9SNikolay Aleksandrov 				int grec_type)
21215bf1e00bSNikolay Aleksandrov {
21225bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
21235bf1e00bSNikolay Aleksandrov 	u32 src_idx, to_send = 0;
21245bf1e00bSNikolay Aleksandrov 	bool changed = false;
21255bf1e00bSNikolay Aleksandrov 	struct br_ip src_ip;
21265bf1e00bSNikolay Aleksandrov 
21275bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
21285bf1e00bSNikolay Aleksandrov 		ent->flags = (ent->flags & ~BR_SGRP_F_SEND) | BR_SGRP_F_DELETE;
21295bf1e00bSNikolay Aleksandrov 
21305bf1e00bSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2131085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
21325bf1e00bSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2133e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
21345bf1e00bSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
21355bf1e00bSNikolay Aleksandrov 		if (ent) {
21365bf1e00bSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_DELETE;
21375bf1e00bSNikolay Aleksandrov 		} else {
21385bf1e00bSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
21395bf1e00bSNikolay Aleksandrov 			if (ent) {
2140b0812368SNikolay Aleksandrov 				__grp_src_mod_timer(ent, pg->timer.expires);
21415bf1e00bSNikolay Aleksandrov 				changed = true;
21425bf1e00bSNikolay Aleksandrov 			}
21435bf1e00bSNikolay Aleksandrov 		}
21445bf1e00bSNikolay Aleksandrov 		if (ent && timer_pending(&ent->timer)) {
21455bf1e00bSNikolay Aleksandrov 			ent->flags |= BR_SGRP_F_SEND;
21465bf1e00bSNikolay Aleksandrov 			to_send++;
21475bf1e00bSNikolay Aleksandrov 		}
21485bf1e00bSNikolay Aleksandrov 	}
21495bf1e00bSNikolay Aleksandrov 
2150ddc255d9SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
2151ddc255d9SNikolay Aleksandrov 		changed = true;
2152ddc255d9SNikolay Aleksandrov 
21535bf1e00bSNikolay Aleksandrov 	if (__grp_src_delete_marked(pg))
21545bf1e00bSNikolay Aleksandrov 		changed = true;
21555bf1e00bSNikolay Aleksandrov 	if (to_send)
21565bf1e00bSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
21575bf1e00bSNikolay Aleksandrov 
21585bf1e00bSNikolay Aleksandrov 	return changed;
21595bf1e00bSNikolay Aleksandrov }
21605bf1e00bSNikolay Aleksandrov 
216154bea721SNikolay Aleksandrov static bool br_multicast_toex(struct net_bridge_port_group *pg, void *h_addr,
2162ddc255d9SNikolay Aleksandrov 			      void *srcs, u32 nsrcs, size_t addr_size,
2163ddc255d9SNikolay Aleksandrov 			      int grec_type)
21645bf1e00bSNikolay Aleksandrov {
2165085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
21665bf1e00bSNikolay Aleksandrov 	bool changed = false;
21675bf1e00bSNikolay Aleksandrov 
21685bf1e00bSNikolay Aleksandrov 	switch (pg->filter_mode) {
21695bf1e00bSNikolay Aleksandrov 	case MCAST_INCLUDE:
2170ddc255d9SNikolay Aleksandrov 		__grp_src_toex_incl(pg, h_addr, srcs, nsrcs, addr_size,
2171ddc255d9SNikolay Aleksandrov 				    grec_type);
21728266a049SNikolay Aleksandrov 		br_multicast_star_g_handle_mode(pg, MCAST_EXCLUDE);
21735bf1e00bSNikolay Aleksandrov 		changed = true;
21745bf1e00bSNikolay Aleksandrov 		break;
21755bf1e00bSNikolay Aleksandrov 	case MCAST_EXCLUDE:
2176ddc255d9SNikolay Aleksandrov 		changed = __grp_src_toex_excl(pg, h_addr, srcs, nsrcs, addr_size,
2177ddc255d9SNikolay Aleksandrov 					      grec_type);
21785bf1e00bSNikolay Aleksandrov 		break;
21795bf1e00bSNikolay Aleksandrov 	}
21805bf1e00bSNikolay Aleksandrov 
21815bf1e00bSNikolay Aleksandrov 	pg->filter_mode = MCAST_EXCLUDE;
21825bf1e00bSNikolay Aleksandrov 	mod_timer(&pg->timer, jiffies + br_multicast_gmi(br));
21835bf1e00bSNikolay Aleksandrov 
21845bf1e00bSNikolay Aleksandrov 	return changed;
21855bf1e00bSNikolay Aleksandrov }
21865bf1e00bSNikolay Aleksandrov 
2187109865feSNikolay Aleksandrov /* State          Msg type      New state                Actions
2188109865feSNikolay Aleksandrov  * INCLUDE (A)    BLOCK (B)     INCLUDE (A)              Send Q(G,A*B)
2189109865feSNikolay Aleksandrov  */
21900ad57c99SNikolay Aleksandrov static bool __grp_src_block_incl(struct net_bridge_port_group *pg, void *h_addr,
2191474ddb37SNikolay Aleksandrov 				 void *srcs, u32 nsrcs, size_t addr_size, int grec_type)
2192109865feSNikolay Aleksandrov {
2193109865feSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
2194109865feSNikolay Aleksandrov 	u32 src_idx, to_send = 0;
21950ad57c99SNikolay Aleksandrov 	bool changed = false;
2196109865feSNikolay Aleksandrov 	struct br_ip src_ip;
2197109865feSNikolay Aleksandrov 
2198109865feSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
2199109865feSNikolay Aleksandrov 		ent->flags &= ~BR_SGRP_F_SEND;
2200109865feSNikolay Aleksandrov 
2201109865feSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2202085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
2203109865feSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2204e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2205109865feSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
2206109865feSNikolay Aleksandrov 		if (ent) {
2207109865feSNikolay Aleksandrov 			ent->flags |= BR_SGRP_F_SEND;
2208109865feSNikolay Aleksandrov 			to_send++;
2209109865feSNikolay Aleksandrov 		}
2210109865feSNikolay Aleksandrov 	}
2211109865feSNikolay Aleksandrov 
2212474ddb37SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
2213474ddb37SNikolay Aleksandrov 		changed = true;
2214474ddb37SNikolay Aleksandrov 
2215109865feSNikolay Aleksandrov 	if (to_send)
2216109865feSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
2217109865feSNikolay Aleksandrov 
22180ad57c99SNikolay Aleksandrov 	return changed;
2219109865feSNikolay Aleksandrov }
2220109865feSNikolay Aleksandrov 
2221109865feSNikolay Aleksandrov /* State          Msg type      New state                Actions
2222109865feSNikolay Aleksandrov  * EXCLUDE (X,Y)  BLOCK (A)     EXCLUDE (X+(A-Y),Y)      (A-X-Y)=Group Timer
2223109865feSNikolay Aleksandrov  *                                                       Send Q(G,A-Y)
2224109865feSNikolay Aleksandrov  */
222554bea721SNikolay Aleksandrov static bool __grp_src_block_excl(struct net_bridge_port_group *pg, void *h_addr,
2226474ddb37SNikolay Aleksandrov 				 void *srcs, u32 nsrcs, size_t addr_size, int grec_type)
2227109865feSNikolay Aleksandrov {
2228109865feSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
2229109865feSNikolay Aleksandrov 	u32 src_idx, to_send = 0;
2230109865feSNikolay Aleksandrov 	bool changed = false;
2231109865feSNikolay Aleksandrov 	struct br_ip src_ip;
2232109865feSNikolay Aleksandrov 
2233109865feSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
2234109865feSNikolay Aleksandrov 		ent->flags &= ~BR_SGRP_F_SEND;
2235109865feSNikolay Aleksandrov 
2236109865feSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2237085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
2238109865feSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2239e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2240109865feSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
2241109865feSNikolay Aleksandrov 		if (!ent) {
2242109865feSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
2243109865feSNikolay Aleksandrov 			if (ent) {
2244b0812368SNikolay Aleksandrov 				__grp_src_mod_timer(ent, pg->timer.expires);
2245109865feSNikolay Aleksandrov 				changed = true;
2246109865feSNikolay Aleksandrov 			}
2247109865feSNikolay Aleksandrov 		}
2248109865feSNikolay Aleksandrov 		if (ent && timer_pending(&ent->timer)) {
2249109865feSNikolay Aleksandrov 			ent->flags |= BR_SGRP_F_SEND;
2250109865feSNikolay Aleksandrov 			to_send++;
2251109865feSNikolay Aleksandrov 		}
2252109865feSNikolay Aleksandrov 	}
2253109865feSNikolay Aleksandrov 
2254474ddb37SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
2255474ddb37SNikolay Aleksandrov 		changed = true;
2256474ddb37SNikolay Aleksandrov 
2257109865feSNikolay Aleksandrov 	if (to_send)
2258109865feSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
2259109865feSNikolay Aleksandrov 
2260109865feSNikolay Aleksandrov 	return changed;
2261109865feSNikolay Aleksandrov }
2262109865feSNikolay Aleksandrov 
226354bea721SNikolay Aleksandrov static bool br_multicast_block(struct net_bridge_port_group *pg, void *h_addr,
2264474ddb37SNikolay Aleksandrov 			       void *srcs, u32 nsrcs, size_t addr_size, int grec_type)
2265109865feSNikolay Aleksandrov {
2266109865feSNikolay Aleksandrov 	bool changed = false;
2267109865feSNikolay Aleksandrov 
2268109865feSNikolay Aleksandrov 	switch (pg->filter_mode) {
2269109865feSNikolay Aleksandrov 	case MCAST_INCLUDE:
2270474ddb37SNikolay Aleksandrov 		changed = __grp_src_block_incl(pg, h_addr, srcs, nsrcs, addr_size,
2271474ddb37SNikolay Aleksandrov 					       grec_type);
2272109865feSNikolay Aleksandrov 		break;
2273109865feSNikolay Aleksandrov 	case MCAST_EXCLUDE:
2274474ddb37SNikolay Aleksandrov 		changed = __grp_src_block_excl(pg, h_addr, srcs, nsrcs, addr_size,
2275474ddb37SNikolay Aleksandrov 					       grec_type);
2276109865feSNikolay Aleksandrov 		break;
2277109865feSNikolay Aleksandrov 	}
2278109865feSNikolay Aleksandrov 
2279e87e4b5cSNikolay Aleksandrov 	if ((pg->filter_mode == MCAST_INCLUDE && hlist_empty(&pg->src_list)) ||
2280e87e4b5cSNikolay Aleksandrov 	    br_multicast_eht_should_del_pg(pg)) {
2281d5a10222SNikolay Aleksandrov 		if (br_multicast_eht_should_del_pg(pg))
2282d5a10222SNikolay Aleksandrov 			pg->flags |= MDB_PG_FLAGS_FAST_LEAVE;
2283e87e4b5cSNikolay Aleksandrov 		br_multicast_find_del_pg(pg->key.port->br, pg);
2284e87e4b5cSNikolay Aleksandrov 		/* a notification has already been sent and we shouldn't
2285e87e4b5cSNikolay Aleksandrov 		 * access pg after the delete so we have to return false
2286e87e4b5cSNikolay Aleksandrov 		 */
2287e87e4b5cSNikolay Aleksandrov 		changed = false;
2288e87e4b5cSNikolay Aleksandrov 	}
2289e87e4b5cSNikolay Aleksandrov 
2290109865feSNikolay Aleksandrov 	return changed;
2291109865feSNikolay Aleksandrov }
2292109865feSNikolay Aleksandrov 
22930436862eSNikolay Aleksandrov static struct net_bridge_port_group *
22940436862eSNikolay Aleksandrov br_multicast_find_port(struct net_bridge_mdb_entry *mp,
22950436862eSNikolay Aleksandrov 		       struct net_bridge_port *p,
22960436862eSNikolay Aleksandrov 		       const unsigned char *src)
22970436862eSNikolay Aleksandrov {
2298071445c6SNikolay Aleksandrov 	struct net_bridge *br __maybe_unused = mp->br;
22990436862eSNikolay Aleksandrov 	struct net_bridge_port_group *pg;
23000436862eSNikolay Aleksandrov 
23010436862eSNikolay Aleksandrov 	for (pg = mlock_dereference(mp->ports, br);
23020436862eSNikolay Aleksandrov 	     pg;
23030436862eSNikolay Aleksandrov 	     pg = mlock_dereference(pg->next, br))
23040436862eSNikolay Aleksandrov 		if (br_port_group_equal(pg, p, src))
23050436862eSNikolay Aleksandrov 			return pg;
23060436862eSNikolay Aleksandrov 
23070436862eSNikolay Aleksandrov 	return NULL;
23080436862eSNikolay Aleksandrov }
23090436862eSNikolay Aleksandrov 
23108ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
2311eb1d1641SHerbert Xu 					 struct net_bridge_port *port,
231206499098SVlad Yasevich 					 struct sk_buff *skb,
231306499098SVlad Yasevich 					 u16 vid)
2314eb1d1641SHerbert Xu {
23150436862eSNikolay Aleksandrov 	bool igmpv2 = br->multicast_igmp_version == 2;
23160436862eSNikolay Aleksandrov 	struct net_bridge_mdb_entry *mdst;
23170436862eSNikolay Aleksandrov 	struct net_bridge_port_group *pg;
23186db6f0eaSFelix Fietkau 	const unsigned char *src;
2319eb1d1641SHerbert Xu 	struct igmpv3_report *ih;
2320eb1d1641SHerbert Xu 	struct igmpv3_grec *grec;
23210436862eSNikolay Aleksandrov 	int i, len, num, type;
232254bea721SNikolay Aleksandrov 	__be32 group, *h_addr;
23230436862eSNikolay Aleksandrov 	bool changed = false;
23240436862eSNikolay Aleksandrov 	int err = 0;
2325e57f6185SNikolay Aleksandrov 	u16 nsrcs;
2326eb1d1641SHerbert Xu 
2327eb1d1641SHerbert Xu 	ih = igmpv3_report_hdr(skb);
2328eb1d1641SHerbert Xu 	num = ntohs(ih->ngrec);
2329c2d4fbd2SLinus Lüssing 	len = skb_transport_offset(skb) + sizeof(*ih);
2330eb1d1641SHerbert Xu 
2331eb1d1641SHerbert Xu 	for (i = 0; i < num; i++) {
2332eb1d1641SHerbert Xu 		len += sizeof(*grec);
2333ba5ea614SLinus Lüssing 		if (!ip_mc_may_pull(skb, len))
2334eb1d1641SHerbert Xu 			return -EINVAL;
2335eb1d1641SHerbert Xu 
2336fd218cf9SHerbert Xu 		grec = (void *)(skb->data + len - sizeof(*grec));
2337eb1d1641SHerbert Xu 		group = grec->grec_mca;
2338eb1d1641SHerbert Xu 		type = grec->grec_type;
2339e57f6185SNikolay Aleksandrov 		nsrcs = ntohs(grec->grec_nsrcs);
2340eb1d1641SHerbert Xu 
2341e57f6185SNikolay Aleksandrov 		len += nsrcs * 4;
2342ba5ea614SLinus Lüssing 		if (!ip_mc_may_pull(skb, len))
2343eb1d1641SHerbert Xu 			return -EINVAL;
2344eb1d1641SHerbert Xu 
2345eb1d1641SHerbert Xu 		switch (type) {
2346eb1d1641SHerbert Xu 		case IGMPV3_MODE_IS_INCLUDE:
2347eb1d1641SHerbert Xu 		case IGMPV3_MODE_IS_EXCLUDE:
2348eb1d1641SHerbert Xu 		case IGMPV3_CHANGE_TO_INCLUDE:
2349eb1d1641SHerbert Xu 		case IGMPV3_CHANGE_TO_EXCLUDE:
2350eb1d1641SHerbert Xu 		case IGMPV3_ALLOW_NEW_SOURCES:
2351eb1d1641SHerbert Xu 		case IGMPV3_BLOCK_OLD_SOURCES:
2352eb1d1641SHerbert Xu 			break;
2353eb1d1641SHerbert Xu 
2354eb1d1641SHerbert Xu 		default:
2355eb1d1641SHerbert Xu 			continue;
2356eb1d1641SHerbert Xu 		}
2357eb1d1641SHerbert Xu 
23586db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
23590436862eSNikolay Aleksandrov 		if (nsrcs == 0 &&
23600436862eSNikolay Aleksandrov 		    (type == IGMPV3_CHANGE_TO_INCLUDE ||
23610436862eSNikolay Aleksandrov 		     type == IGMPV3_MODE_IS_INCLUDE)) {
23620436862eSNikolay Aleksandrov 			if (!port || igmpv2) {
23636db6f0eaSFelix Fietkau 				br_ip4_multicast_leave_group(br, port, group, vid, src);
23640436862eSNikolay Aleksandrov 				continue;
23650436862eSNikolay Aleksandrov 			}
2366bc8c20acSSatish Ashok 		} else {
23676db6f0eaSFelix Fietkau 			err = br_ip4_multicast_add_group(br, port, group, vid,
23680436862eSNikolay Aleksandrov 							 src, igmpv2);
2369eb1d1641SHerbert Xu 			if (err)
2370eb1d1641SHerbert Xu 				break;
2371eb1d1641SHerbert Xu 		}
23720436862eSNikolay Aleksandrov 
23730436862eSNikolay Aleksandrov 		if (!port || igmpv2)
23740436862eSNikolay Aleksandrov 			continue;
23750436862eSNikolay Aleksandrov 
23760436862eSNikolay Aleksandrov 		spin_lock_bh(&br->multicast_lock);
23770436862eSNikolay Aleksandrov 		mdst = br_mdb_ip4_get(br, group, vid);
23780436862eSNikolay Aleksandrov 		if (!mdst)
23790436862eSNikolay Aleksandrov 			goto unlock_continue;
23800436862eSNikolay Aleksandrov 		pg = br_multicast_find_port(mdst, port, src);
23810436862eSNikolay Aleksandrov 		if (!pg || (pg->flags & MDB_PG_FLAGS_PERMANENT))
23820436862eSNikolay Aleksandrov 			goto unlock_continue;
238354bea721SNikolay Aleksandrov 		/* reload grec and host addr */
23840436862eSNikolay Aleksandrov 		grec = (void *)(skb->data + len - sizeof(*grec) - (nsrcs * 4));
238554bea721SNikolay Aleksandrov 		h_addr = &ip_hdr(skb)->saddr;
23860436862eSNikolay Aleksandrov 		switch (type) {
23870436862eSNikolay Aleksandrov 		case IGMPV3_ALLOW_NEW_SOURCES:
238854bea721SNikolay Aleksandrov 			changed = br_multicast_isinc_allow(pg, h_addr, grec->grec_src,
2389474ddb37SNikolay Aleksandrov 							   nsrcs, sizeof(__be32), type);
23900436862eSNikolay Aleksandrov 			break;
2391e6231bcaSNikolay Aleksandrov 		case IGMPV3_MODE_IS_INCLUDE:
239254bea721SNikolay Aleksandrov 			changed = br_multicast_isinc_allow(pg, h_addr, grec->grec_src,
2393474ddb37SNikolay Aleksandrov 							   nsrcs, sizeof(__be32), type);
2394e6231bcaSNikolay Aleksandrov 			break;
2395e6231bcaSNikolay Aleksandrov 		case IGMPV3_MODE_IS_EXCLUDE:
239654bea721SNikolay Aleksandrov 			changed = br_multicast_isexc(pg, h_addr, grec->grec_src,
2397ddc255d9SNikolay Aleksandrov 						     nsrcs, sizeof(__be32), type);
2398e6231bcaSNikolay Aleksandrov 			break;
23995bf1e00bSNikolay Aleksandrov 		case IGMPV3_CHANGE_TO_INCLUDE:
240054bea721SNikolay Aleksandrov 			changed = br_multicast_toin(pg, h_addr, grec->grec_src,
2401ddc255d9SNikolay Aleksandrov 						    nsrcs, sizeof(__be32), type);
24025bf1e00bSNikolay Aleksandrov 			break;
24035bf1e00bSNikolay Aleksandrov 		case IGMPV3_CHANGE_TO_EXCLUDE:
240454bea721SNikolay Aleksandrov 			changed = br_multicast_toex(pg, h_addr, grec->grec_src,
2405ddc255d9SNikolay Aleksandrov 						    nsrcs, sizeof(__be32), type);
24065bf1e00bSNikolay Aleksandrov 			break;
2407109865feSNikolay Aleksandrov 		case IGMPV3_BLOCK_OLD_SOURCES:
240854bea721SNikolay Aleksandrov 			changed = br_multicast_block(pg, h_addr, grec->grec_src,
2409474ddb37SNikolay Aleksandrov 						     nsrcs, sizeof(__be32), type);
2410109865feSNikolay Aleksandrov 			break;
24110436862eSNikolay Aleksandrov 		}
24120436862eSNikolay Aleksandrov 		if (changed)
24130436862eSNikolay Aleksandrov 			br_mdb_notify(br->dev, mdst, pg, RTM_NEWMDB);
24140436862eSNikolay Aleksandrov unlock_continue:
24150436862eSNikolay Aleksandrov 		spin_unlock_bh(&br->multicast_lock);
2416bc8c20acSSatish Ashok 	}
2417eb1d1641SHerbert Xu 
2418eb1d1641SHerbert Xu 	return err;
2419eb1d1641SHerbert Xu }
2420eb1d1641SHerbert Xu 
2421dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
242208b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_mld2_report(struct net_bridge *br,
242308b202b6SYOSHIFUJI Hideaki 					struct net_bridge_port *port,
242406499098SVlad Yasevich 					struct sk_buff *skb,
242506499098SVlad Yasevich 					u16 vid)
242608b202b6SYOSHIFUJI Hideaki {
24270436862eSNikolay Aleksandrov 	bool mldv1 = br->multicast_mld_version == 1;
24280436862eSNikolay Aleksandrov 	struct net_bridge_mdb_entry *mdst;
24290436862eSNikolay Aleksandrov 	struct net_bridge_port_group *pg;
2430ba5ea614SLinus Lüssing 	unsigned int nsrcs_offset;
24316db6f0eaSFelix Fietkau 	const unsigned char *src;
243208b202b6SYOSHIFUJI Hideaki 	struct icmp6hdr *icmp6h;
243354bea721SNikolay Aleksandrov 	struct in6_addr *h_addr;
243408b202b6SYOSHIFUJI Hideaki 	struct mld2_grec *grec;
2435ba5ea614SLinus Lüssing 	unsigned int grec_len;
24360436862eSNikolay Aleksandrov 	bool changed = false;
24370436862eSNikolay Aleksandrov 	int i, len, num;
243808b202b6SYOSHIFUJI Hideaki 	int err = 0;
243908b202b6SYOSHIFUJI Hideaki 
2440ba5ea614SLinus Lüssing 	if (!ipv6_mc_may_pull(skb, sizeof(*icmp6h)))
244108b202b6SYOSHIFUJI Hideaki 		return -EINVAL;
244208b202b6SYOSHIFUJI Hideaki 
244308b202b6SYOSHIFUJI Hideaki 	icmp6h = icmp6_hdr(skb);
244408b202b6SYOSHIFUJI Hideaki 	num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
2445c2d4fbd2SLinus Lüssing 	len = skb_transport_offset(skb) + sizeof(*icmp6h);
244608b202b6SYOSHIFUJI Hideaki 
244708b202b6SYOSHIFUJI Hideaki 	for (i = 0; i < num; i++) {
2448e57f6185SNikolay Aleksandrov 		__be16 *_nsrcs, __nsrcs;
2449e57f6185SNikolay Aleksandrov 		u16 nsrcs;
245008b202b6SYOSHIFUJI Hideaki 
2451ba5ea614SLinus Lüssing 		nsrcs_offset = len + offsetof(struct mld2_grec, grec_nsrcs);
2452ba5ea614SLinus Lüssing 
2453ba5ea614SLinus Lüssing 		if (skb_transport_offset(skb) + ipv6_transport_len(skb) <
24545fc6266aSLinus Lüssing 		    nsrcs_offset + sizeof(__nsrcs))
2455ba5ea614SLinus Lüssing 			return -EINVAL;
2456ba5ea614SLinus Lüssing 
2457e57f6185SNikolay Aleksandrov 		_nsrcs = skb_header_pointer(skb, nsrcs_offset,
2458e57f6185SNikolay Aleksandrov 					    sizeof(__nsrcs), &__nsrcs);
2459e57f6185SNikolay Aleksandrov 		if (!_nsrcs)
246008b202b6SYOSHIFUJI Hideaki 			return -EINVAL;
246108b202b6SYOSHIFUJI Hideaki 
2462e57f6185SNikolay Aleksandrov 		nsrcs = ntohs(*_nsrcs);
2463e57f6185SNikolay Aleksandrov 		grec_len = struct_size(grec, grec_src, nsrcs);
2464ba5ea614SLinus Lüssing 
2465ba5ea614SLinus Lüssing 		if (!ipv6_mc_may_pull(skb, len + grec_len))
246608b202b6SYOSHIFUJI Hideaki 			return -EINVAL;
246708b202b6SYOSHIFUJI Hideaki 
246808b202b6SYOSHIFUJI Hideaki 		grec = (struct mld2_grec *)(skb->data + len);
2469ba5ea614SLinus Lüssing 		len += grec_len;
247008b202b6SYOSHIFUJI Hideaki 
247108b202b6SYOSHIFUJI Hideaki 		switch (grec->grec_type) {
247208b202b6SYOSHIFUJI Hideaki 		case MLD2_MODE_IS_INCLUDE:
247308b202b6SYOSHIFUJI Hideaki 		case MLD2_MODE_IS_EXCLUDE:
247408b202b6SYOSHIFUJI Hideaki 		case MLD2_CHANGE_TO_INCLUDE:
247508b202b6SYOSHIFUJI Hideaki 		case MLD2_CHANGE_TO_EXCLUDE:
247608b202b6SYOSHIFUJI Hideaki 		case MLD2_ALLOW_NEW_SOURCES:
247708b202b6SYOSHIFUJI Hideaki 		case MLD2_BLOCK_OLD_SOURCES:
247808b202b6SYOSHIFUJI Hideaki 			break;
247908b202b6SYOSHIFUJI Hideaki 
248008b202b6SYOSHIFUJI Hideaki 		default:
248108b202b6SYOSHIFUJI Hideaki 			continue;
248208b202b6SYOSHIFUJI Hideaki 		}
248308b202b6SYOSHIFUJI Hideaki 
24846db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
2485bc8c20acSSatish Ashok 		if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE ||
2486bc8c20acSSatish Ashok 		     grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
2487e57f6185SNikolay Aleksandrov 		    nsrcs == 0) {
24880436862eSNikolay Aleksandrov 			if (!port || mldv1) {
24890436862eSNikolay Aleksandrov 				br_ip6_multicast_leave_group(br, port,
24900436862eSNikolay Aleksandrov 							     &grec->grec_mca,
24916db6f0eaSFelix Fietkau 							     vid, src);
24920436862eSNikolay Aleksandrov 				continue;
24930436862eSNikolay Aleksandrov 			}
2494bc8c20acSSatish Ashok 		} else {
2495bc8c20acSSatish Ashok 			err = br_ip6_multicast_add_group(br, port,
24966db6f0eaSFelix Fietkau 							 &grec->grec_mca, vid,
24970436862eSNikolay Aleksandrov 							 src, mldv1);
24989264251eSDavide Caratti 			if (err)
249908b202b6SYOSHIFUJI Hideaki 				break;
250008b202b6SYOSHIFUJI Hideaki 		}
25010436862eSNikolay Aleksandrov 
25020436862eSNikolay Aleksandrov 		if (!port || mldv1)
25030436862eSNikolay Aleksandrov 			continue;
25040436862eSNikolay Aleksandrov 
25050436862eSNikolay Aleksandrov 		spin_lock_bh(&br->multicast_lock);
25060436862eSNikolay Aleksandrov 		mdst = br_mdb_ip6_get(br, &grec->grec_mca, vid);
25070436862eSNikolay Aleksandrov 		if (!mdst)
25080436862eSNikolay Aleksandrov 			goto unlock_continue;
25090436862eSNikolay Aleksandrov 		pg = br_multicast_find_port(mdst, port, src);
25100436862eSNikolay Aleksandrov 		if (!pg || (pg->flags & MDB_PG_FLAGS_PERMANENT))
25110436862eSNikolay Aleksandrov 			goto unlock_continue;
251254bea721SNikolay Aleksandrov 		h_addr = &ipv6_hdr(skb)->saddr;
25130436862eSNikolay Aleksandrov 		switch (grec->grec_type) {
25140436862eSNikolay Aleksandrov 		case MLD2_ALLOW_NEW_SOURCES:
251554bea721SNikolay Aleksandrov 			changed = br_multicast_isinc_allow(pg, h_addr,
251654bea721SNikolay Aleksandrov 							   grec->grec_src, nsrcs,
2517474ddb37SNikolay Aleksandrov 							   sizeof(struct in6_addr),
2518474ddb37SNikolay Aleksandrov 							   grec->grec_type);
25190436862eSNikolay Aleksandrov 			break;
2520e6231bcaSNikolay Aleksandrov 		case MLD2_MODE_IS_INCLUDE:
252154bea721SNikolay Aleksandrov 			changed = br_multicast_isinc_allow(pg, h_addr,
252254bea721SNikolay Aleksandrov 							   grec->grec_src, nsrcs,
2523474ddb37SNikolay Aleksandrov 							   sizeof(struct in6_addr),
2524474ddb37SNikolay Aleksandrov 							   grec->grec_type);
2525e6231bcaSNikolay Aleksandrov 			break;
2526e6231bcaSNikolay Aleksandrov 		case MLD2_MODE_IS_EXCLUDE:
252754bea721SNikolay Aleksandrov 			changed = br_multicast_isexc(pg, h_addr,
252854bea721SNikolay Aleksandrov 						     grec->grec_src, nsrcs,
2529ddc255d9SNikolay Aleksandrov 						     sizeof(struct in6_addr),
2530ddc255d9SNikolay Aleksandrov 						     grec->grec_type);
2531e6231bcaSNikolay Aleksandrov 			break;
25325bf1e00bSNikolay Aleksandrov 		case MLD2_CHANGE_TO_INCLUDE:
253354bea721SNikolay Aleksandrov 			changed = br_multicast_toin(pg, h_addr,
253454bea721SNikolay Aleksandrov 						    grec->grec_src, nsrcs,
2535ddc255d9SNikolay Aleksandrov 						    sizeof(struct in6_addr),
2536ddc255d9SNikolay Aleksandrov 						    grec->grec_type);
25375bf1e00bSNikolay Aleksandrov 			break;
25385bf1e00bSNikolay Aleksandrov 		case MLD2_CHANGE_TO_EXCLUDE:
253954bea721SNikolay Aleksandrov 			changed = br_multicast_toex(pg, h_addr,
254054bea721SNikolay Aleksandrov 						    grec->grec_src, nsrcs,
2541ddc255d9SNikolay Aleksandrov 						    sizeof(struct in6_addr),
2542ddc255d9SNikolay Aleksandrov 						    grec->grec_type);
25435bf1e00bSNikolay Aleksandrov 			break;
2544109865feSNikolay Aleksandrov 		case MLD2_BLOCK_OLD_SOURCES:
254554bea721SNikolay Aleksandrov 			changed = br_multicast_block(pg, h_addr,
254654bea721SNikolay Aleksandrov 						     grec->grec_src, nsrcs,
2547474ddb37SNikolay Aleksandrov 						     sizeof(struct in6_addr),
2548474ddb37SNikolay Aleksandrov 						     grec->grec_type);
2549109865feSNikolay Aleksandrov 			break;
25500436862eSNikolay Aleksandrov 		}
25510436862eSNikolay Aleksandrov 		if (changed)
25520436862eSNikolay Aleksandrov 			br_mdb_notify(br->dev, mdst, pg, RTM_NEWMDB);
25530436862eSNikolay Aleksandrov unlock_continue:
25540436862eSNikolay Aleksandrov 		spin_unlock_bh(&br->multicast_lock);
2555bc8c20acSSatish Ashok 	}
255608b202b6SYOSHIFUJI Hideaki 
255708b202b6SYOSHIFUJI Hideaki 	return err;
255808b202b6SYOSHIFUJI Hideaki }
255908b202b6SYOSHIFUJI Hideaki #endif
256008b202b6SYOSHIFUJI Hideaki 
2561dc4eb53aSLinus Lüssing static bool br_ip4_multicast_select_querier(struct net_bridge *br,
25622cd41431SLinus Lüssing 					    struct net_bridge_port *port,
2563dc4eb53aSLinus Lüssing 					    __be32 saddr)
2564dc4eb53aSLinus Lüssing {
2565dc4eb53aSLinus Lüssing 	if (!timer_pending(&br->ip4_own_query.timer) &&
2566dc4eb53aSLinus Lüssing 	    !timer_pending(&br->ip4_other_query.timer))
2567dc4eb53aSLinus Lüssing 		goto update;
2568dc4eb53aSLinus Lüssing 
2569deb96566SNikolay Aleksandrov 	if (!br->ip4_querier.addr.src.ip4)
2570dc4eb53aSLinus Lüssing 		goto update;
2571dc4eb53aSLinus Lüssing 
2572deb96566SNikolay Aleksandrov 	if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.src.ip4))
2573dc4eb53aSLinus Lüssing 		goto update;
2574dc4eb53aSLinus Lüssing 
2575dc4eb53aSLinus Lüssing 	return false;
2576dc4eb53aSLinus Lüssing 
2577dc4eb53aSLinus Lüssing update:
2578deb96566SNikolay Aleksandrov 	br->ip4_querier.addr.src.ip4 = saddr;
2579dc4eb53aSLinus Lüssing 
25802cd41431SLinus Lüssing 	/* update protected by general multicast_lock by caller */
25812cd41431SLinus Lüssing 	rcu_assign_pointer(br->ip4_querier.port, port);
25822cd41431SLinus Lüssing 
2583dc4eb53aSLinus Lüssing 	return true;
2584dc4eb53aSLinus Lüssing }
2585dc4eb53aSLinus Lüssing 
2586dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
2587dc4eb53aSLinus Lüssing static bool br_ip6_multicast_select_querier(struct net_bridge *br,
25882cd41431SLinus Lüssing 					    struct net_bridge_port *port,
2589dc4eb53aSLinus Lüssing 					    struct in6_addr *saddr)
2590dc4eb53aSLinus Lüssing {
2591dc4eb53aSLinus Lüssing 	if (!timer_pending(&br->ip6_own_query.timer) &&
2592dc4eb53aSLinus Lüssing 	    !timer_pending(&br->ip6_other_query.timer))
2593dc4eb53aSLinus Lüssing 		goto update;
2594dc4eb53aSLinus Lüssing 
2595deb96566SNikolay Aleksandrov 	if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.src.ip6) <= 0)
2596dc4eb53aSLinus Lüssing 		goto update;
2597dc4eb53aSLinus Lüssing 
2598dc4eb53aSLinus Lüssing 	return false;
2599dc4eb53aSLinus Lüssing 
2600dc4eb53aSLinus Lüssing update:
2601deb96566SNikolay Aleksandrov 	br->ip6_querier.addr.src.ip6 = *saddr;
2602dc4eb53aSLinus Lüssing 
26032cd41431SLinus Lüssing 	/* update protected by general multicast_lock by caller */
26042cd41431SLinus Lüssing 	rcu_assign_pointer(br->ip6_querier.port, port);
26052cd41431SLinus Lüssing 
2606dc4eb53aSLinus Lüssing 	return true;
2607dc4eb53aSLinus Lüssing }
2608dc4eb53aSLinus Lüssing #endif
2609dc4eb53aSLinus Lüssing 
2610dc4eb53aSLinus Lüssing static bool br_multicast_select_querier(struct net_bridge *br,
26112cd41431SLinus Lüssing 					struct net_bridge_port *port,
2612dc4eb53aSLinus Lüssing 					struct br_ip *saddr)
2613dc4eb53aSLinus Lüssing {
2614dc4eb53aSLinus Lüssing 	switch (saddr->proto) {
2615dc4eb53aSLinus Lüssing 	case htons(ETH_P_IP):
2616deb96566SNikolay Aleksandrov 		return br_ip4_multicast_select_querier(br, port, saddr->src.ip4);
2617dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
2618dc4eb53aSLinus Lüssing 	case htons(ETH_P_IPV6):
2619deb96566SNikolay Aleksandrov 		return br_ip6_multicast_select_querier(br, port, &saddr->src.ip6);
2620dc4eb53aSLinus Lüssing #endif
2621dc4eb53aSLinus Lüssing 	}
2622dc4eb53aSLinus Lüssing 
2623dc4eb53aSLinus Lüssing 	return false;
2624dc4eb53aSLinus Lüssing }
2625dc4eb53aSLinus Lüssing 
2626cc0fdd80SLinus Lüssing static void
262790010b36SLinus Lüssing br_multicast_update_query_timer(struct net_bridge *br,
262890010b36SLinus Lüssing 				struct bridge_mcast_other_query *query,
2629b00589afSLinus Lüssing 				unsigned long max_delay)
2630b00589afSLinus Lüssing {
263190010b36SLinus Lüssing 	if (!timer_pending(&query->timer))
263290010b36SLinus Lüssing 		query->delay_time = jiffies + max_delay;
2633b00589afSLinus Lüssing 
263490010b36SLinus Lüssing 	mod_timer(&query->timer, jiffies + br->multicast_querier_interval);
2635b00589afSLinus Lüssing }
2636b00589afSLinus Lüssing 
26376d549648SNogah Frankel static void br_port_mc_router_state_change(struct net_bridge_port *p,
26386d549648SNogah Frankel 					   bool is_mc_router)
26396d549648SNogah Frankel {
26406d549648SNogah Frankel 	struct switchdev_attr attr = {
26416d549648SNogah Frankel 		.orig_dev = p->dev,
26426d549648SNogah Frankel 		.id = SWITCHDEV_ATTR_ID_PORT_MROUTER,
26436d549648SNogah Frankel 		.flags = SWITCHDEV_F_DEFER,
26446d549648SNogah Frankel 		.u.mrouter = is_mc_router,
26456d549648SNogah Frankel 	};
26466d549648SNogah Frankel 
26476d549648SNogah Frankel 	switchdev_port_attr_set(p->dev, &attr);
26486d549648SNogah Frankel }
26496d549648SNogah Frankel 
26507e80c124Sstephen hemminger /*
26517c77602fSCong Wang  * Add port to router_list
26527e80c124Sstephen hemminger  *  list is maintained ordered by pointer value
26537e80c124Sstephen hemminger  *  and locked by br->multicast_lock and RCU
26547e80c124Sstephen hemminger  */
26550909e117SHerbert Xu static void br_multicast_add_router(struct net_bridge *br,
26560909e117SHerbert Xu 				    struct net_bridge_port *port)
26570909e117SHerbert Xu {
2658dcdca2c4Sstephen hemminger 	struct net_bridge_port *p;
2659b67bfe0dSSasha Levin 	struct hlist_node *slot = NULL;
26600909e117SHerbert Xu 
26611a040eacSNikolay Aleksandrov 	if (!hlist_unhashed(&port->rlist))
26621a040eacSNikolay Aleksandrov 		return;
26631a040eacSNikolay Aleksandrov 
2664b67bfe0dSSasha Levin 	hlist_for_each_entry(p, &br->router_list, rlist) {
26657e80c124Sstephen hemminger 		if ((unsigned long) port >= (unsigned long) p)
26667e80c124Sstephen hemminger 			break;
2667b67bfe0dSSasha Levin 		slot = &p->rlist;
2668dcdca2c4Sstephen hemminger 	}
26690909e117SHerbert Xu 
26707e80c124Sstephen hemminger 	if (slot)
26711d023284SKen Helias 		hlist_add_behind_rcu(&port->rlist, slot);
2672dcdca2c4Sstephen hemminger 	else
2673dcdca2c4Sstephen hemminger 		hlist_add_head_rcu(&port->rlist, &br->router_list);
2674949f1e39SSatish Ashok 	br_rtr_notify(br->dev, port, RTM_NEWMDB);
26756d549648SNogah Frankel 	br_port_mc_router_state_change(port, true);
26760909e117SHerbert Xu }
26770909e117SHerbert Xu 
2678eb1d1641SHerbert Xu static void br_multicast_mark_router(struct net_bridge *br,
2679eb1d1641SHerbert Xu 				     struct net_bridge_port *port)
2680eb1d1641SHerbert Xu {
2681eb1d1641SHerbert Xu 	unsigned long now = jiffies;
2682eb1d1641SHerbert Xu 
2683eb1d1641SHerbert Xu 	if (!port) {
268477041420SYotam Gigi 		if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) {
268577041420SYotam Gigi 			if (!timer_pending(&br->multicast_router_timer))
268677041420SYotam Gigi 				br_mc_router_state_change(br, true);
2687eb1d1641SHerbert Xu 			mod_timer(&br->multicast_router_timer,
2688eb1d1641SHerbert Xu 				  now + br->multicast_querier_interval);
268977041420SYotam Gigi 		}
2690eb1d1641SHerbert Xu 		return;
2691eb1d1641SHerbert Xu 	}
2692eb1d1641SHerbert Xu 
2693a55d8246SNikolay Aleksandrov 	if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
2694a55d8246SNikolay Aleksandrov 	    port->multicast_router == MDB_RTR_TYPE_PERM)
2695eb1d1641SHerbert Xu 		return;
2696eb1d1641SHerbert Xu 
26970909e117SHerbert Xu 	br_multicast_add_router(br, port);
2698eb1d1641SHerbert Xu 
2699eb1d1641SHerbert Xu 	mod_timer(&port->multicast_router_timer,
2700eb1d1641SHerbert Xu 		  now + br->multicast_querier_interval);
2701eb1d1641SHerbert Xu }
2702eb1d1641SHerbert Xu 
2703eb1d1641SHerbert Xu static void br_multicast_query_received(struct net_bridge *br,
2704eb1d1641SHerbert Xu 					struct net_bridge_port *port,
270590010b36SLinus Lüssing 					struct bridge_mcast_other_query *query,
2706dc4eb53aSLinus Lüssing 					struct br_ip *saddr,
2707b00589afSLinus Lüssing 					unsigned long max_delay)
2708eb1d1641SHerbert Xu {
27092cd41431SLinus Lüssing 	if (!br_multicast_select_querier(br, port, saddr))
2710eb1d1641SHerbert Xu 		return;
2711eb1d1641SHerbert Xu 
2712dc4eb53aSLinus Lüssing 	br_multicast_update_query_timer(br, query, max_delay);
2713eb1d1641SHerbert Xu 	br_multicast_mark_router(br, port);
2714eb1d1641SHerbert Xu }
2715eb1d1641SHerbert Xu 
27169c2e955cSzhong jiang static void br_ip4_multicast_query(struct net_bridge *br,
2717eb1d1641SHerbert Xu 				   struct net_bridge_port *port,
271806499098SVlad Yasevich 				   struct sk_buff *skb,
271906499098SVlad Yasevich 				   u16 vid)
2720eb1d1641SHerbert Xu {
2721ba5ea614SLinus Lüssing 	unsigned int transport_len = ip_transport_len(skb);
2722b71d1d42SEric Dumazet 	const struct iphdr *iph = ip_hdr(skb);
2723eb1d1641SHerbert Xu 	struct igmphdr *ih = igmp_hdr(skb);
2724eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
2725eb1d1641SHerbert Xu 	struct igmpv3_query *ih3;
2726eb1d1641SHerbert Xu 	struct net_bridge_port_group *p;
2727e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
2728dc4eb53aSLinus Lüssing 	struct br_ip saddr;
2729eb1d1641SHerbert Xu 	unsigned long max_delay;
2730eb1d1641SHerbert Xu 	unsigned long now = jiffies;
2731eb1d1641SHerbert Xu 	__be32 group;
2732eb1d1641SHerbert Xu 
2733eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
2734eb1d1641SHerbert Xu 	if (!netif_running(br->dev) ||
2735eb1d1641SHerbert Xu 	    (port && port->state == BR_STATE_DISABLED))
2736eb1d1641SHerbert Xu 		goto out;
2737eb1d1641SHerbert Xu 
2738eb1d1641SHerbert Xu 	group = ih->group;
2739eb1d1641SHerbert Xu 
2740ba5ea614SLinus Lüssing 	if (transport_len == sizeof(*ih)) {
2741eb1d1641SHerbert Xu 		max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
2742eb1d1641SHerbert Xu 
2743eb1d1641SHerbert Xu 		if (!max_delay) {
2744eb1d1641SHerbert Xu 			max_delay = 10 * HZ;
2745eb1d1641SHerbert Xu 			group = 0;
2746eb1d1641SHerbert Xu 		}
2747ba5ea614SLinus Lüssing 	} else if (transport_len >= sizeof(*ih3)) {
2748eb1d1641SHerbert Xu 		ih3 = igmpv3_query_hdr(skb);
274923550b83SNikolay Aleksandrov 		if (ih3->nsrcs ||
275023550b83SNikolay Aleksandrov 		    (br->multicast_igmp_version == 3 && group && ih3->suppress))
2751bec68ff1SYOSHIFUJI Hideaki 			goto out;
2752eb1d1641SHerbert Xu 
27530ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 		max_delay = ih3->code ?
27540ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 			    IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
27559afd85c9SLinus Lüssing 	} else {
27569ed973ccSLinus Lüssing 		goto out;
27579ed973ccSLinus Lüssing 	}
27589ed973ccSLinus Lüssing 
2759dc4eb53aSLinus Lüssing 	if (!group) {
2760dc4eb53aSLinus Lüssing 		saddr.proto = htons(ETH_P_IP);
2761deb96566SNikolay Aleksandrov 		saddr.src.ip4 = iph->saddr;
2762b00589afSLinus Lüssing 
2763dc4eb53aSLinus Lüssing 		br_multicast_query_received(br, port, &br->ip4_other_query,
2764dc4eb53aSLinus Lüssing 					    &saddr, max_delay);
2765eb1d1641SHerbert Xu 		goto out;
2766dc4eb53aSLinus Lüssing 	}
2767eb1d1641SHerbert Xu 
276819e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip4_get(br, group, vid);
2769eb1d1641SHerbert Xu 	if (!mp)
2770eb1d1641SHerbert Xu 		goto out;
2771eb1d1641SHerbert Xu 
2772eb1d1641SHerbert Xu 	max_delay *= br->multicast_last_member_count;
2773eb1d1641SHerbert Xu 
2774ff0fd34eSAndrew Lunn 	if (mp->host_joined &&
2775eb1d1641SHerbert Xu 	    (timer_pending(&mp->timer) ?
2776eb1d1641SHerbert Xu 	     time_after(mp->timer.expires, now + max_delay) :
2777eb1d1641SHerbert Xu 	     try_to_del_timer_sync(&mp->timer) >= 0))
2778eb1d1641SHerbert Xu 		mod_timer(&mp->timer, now + max_delay);
2779eb1d1641SHerbert Xu 
2780e8051688SEric Dumazet 	for (pp = &mp->ports;
2781e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
2782e8051688SEric Dumazet 	     pp = &p->next) {
2783eb1d1641SHerbert Xu 		if (timer_pending(&p->timer) ?
2784eb1d1641SHerbert Xu 		    time_after(p->timer.expires, now + max_delay) :
278523550b83SNikolay Aleksandrov 		    try_to_del_timer_sync(&p->timer) >= 0 &&
278623550b83SNikolay Aleksandrov 		    (br->multicast_igmp_version == 2 ||
278723550b83SNikolay Aleksandrov 		     p->filter_mode == MCAST_EXCLUDE))
278824f9cdcbSHerbert Xu 			mod_timer(&p->timer, now + max_delay);
2789eb1d1641SHerbert Xu 	}
2790eb1d1641SHerbert Xu 
2791eb1d1641SHerbert Xu out:
2792eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
2793eb1d1641SHerbert Xu }
2794eb1d1641SHerbert Xu 
2795dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
279608b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_query(struct net_bridge *br,
279708b202b6SYOSHIFUJI Hideaki 				  struct net_bridge_port *port,
279806499098SVlad Yasevich 				  struct sk_buff *skb,
279906499098SVlad Yasevich 				  u16 vid)
280008b202b6SYOSHIFUJI Hideaki {
2801ba5ea614SLinus Lüssing 	unsigned int transport_len = ipv6_transport_len(skb);
2802eca2a43bSstephen hemminger 	struct mld_msg *mld;
280308b202b6SYOSHIFUJI Hideaki 	struct net_bridge_mdb_entry *mp;
280408b202b6SYOSHIFUJI Hideaki 	struct mld2_query *mld2q;
2805e8051688SEric Dumazet 	struct net_bridge_port_group *p;
2806e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
2807dc4eb53aSLinus Lüssing 	struct br_ip saddr;
280808b202b6SYOSHIFUJI Hideaki 	unsigned long max_delay;
280908b202b6SYOSHIFUJI Hideaki 	unsigned long now = jiffies;
2810856ce5d0SLinus Lüssing 	unsigned int offset = skb_transport_offset(skb);
2811b71d1d42SEric Dumazet 	const struct in6_addr *group = NULL;
28129ed973ccSLinus Lüssing 	bool is_general_query;
281308b202b6SYOSHIFUJI Hideaki 	int err = 0;
281408b202b6SYOSHIFUJI Hideaki 
281508b202b6SYOSHIFUJI Hideaki 	spin_lock(&br->multicast_lock);
281608b202b6SYOSHIFUJI Hideaki 	if (!netif_running(br->dev) ||
281708b202b6SYOSHIFUJI Hideaki 	    (port && port->state == BR_STATE_DISABLED))
281808b202b6SYOSHIFUJI Hideaki 		goto out;
281908b202b6SYOSHIFUJI Hideaki 
2820ba5ea614SLinus Lüssing 	if (transport_len == sizeof(*mld)) {
2821856ce5d0SLinus Lüssing 		if (!pskb_may_pull(skb, offset + sizeof(*mld))) {
282208b202b6SYOSHIFUJI Hideaki 			err = -EINVAL;
282308b202b6SYOSHIFUJI Hideaki 			goto out;
282408b202b6SYOSHIFUJI Hideaki 		}
282508b202b6SYOSHIFUJI Hideaki 		mld = (struct mld_msg *) icmp6_hdr(skb);
28264715213dSLi RongQing 		max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
282708b202b6SYOSHIFUJI Hideaki 		if (max_delay)
282808b202b6SYOSHIFUJI Hideaki 			group = &mld->mld_mca;
2829248ba8ecSLinus Lüssing 	} else {
2830856ce5d0SLinus Lüssing 		if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) {
283108b202b6SYOSHIFUJI Hideaki 			err = -EINVAL;
283208b202b6SYOSHIFUJI Hideaki 			goto out;
283308b202b6SYOSHIFUJI Hideaki 		}
283408b202b6SYOSHIFUJI Hideaki 		mld2q = (struct mld2_query *)icmp6_hdr(skb);
283508b202b6SYOSHIFUJI Hideaki 		if (!mld2q->mld2q_nsrcs)
283608b202b6SYOSHIFUJI Hideaki 			group = &mld2q->mld2q_mca;
283723550b83SNikolay Aleksandrov 		if (br->multicast_mld_version == 2 &&
283823550b83SNikolay Aleksandrov 		    !ipv6_addr_any(&mld2q->mld2q_mca) &&
283923550b83SNikolay Aleksandrov 		    mld2q->mld2q_suppress)
284023550b83SNikolay Aleksandrov 			goto out;
2841e3f5b170SDaniel Borkmann 
2842e3f5b170SDaniel Borkmann 		max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
284308b202b6SYOSHIFUJI Hideaki 	}
284408b202b6SYOSHIFUJI Hideaki 
28459ed973ccSLinus Lüssing 	is_general_query = group && ipv6_addr_any(group);
28469ed973ccSLinus Lüssing 
2847dc4eb53aSLinus Lüssing 	if (is_general_query) {
2848dc4eb53aSLinus Lüssing 		saddr.proto = htons(ETH_P_IPV6);
2849deb96566SNikolay Aleksandrov 		saddr.src.ip6 = ipv6_hdr(skb)->saddr;
2850b00589afSLinus Lüssing 
2851dc4eb53aSLinus Lüssing 		br_multicast_query_received(br, port, &br->ip6_other_query,
2852dc4eb53aSLinus Lüssing 					    &saddr, max_delay);
285308b202b6SYOSHIFUJI Hideaki 		goto out;
28546c03ee8bSLinus Lüssing 	} else if (!group) {
28556c03ee8bSLinus Lüssing 		goto out;
2856dc4eb53aSLinus Lüssing 	}
285708b202b6SYOSHIFUJI Hideaki 
285819e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip6_get(br, group, vid);
285908b202b6SYOSHIFUJI Hideaki 	if (!mp)
286008b202b6SYOSHIFUJI Hideaki 		goto out;
286108b202b6SYOSHIFUJI Hideaki 
286208b202b6SYOSHIFUJI Hideaki 	max_delay *= br->multicast_last_member_count;
2863ff0fd34eSAndrew Lunn 	if (mp->host_joined &&
286408b202b6SYOSHIFUJI Hideaki 	    (timer_pending(&mp->timer) ?
286508b202b6SYOSHIFUJI Hideaki 	     time_after(mp->timer.expires, now + max_delay) :
286608b202b6SYOSHIFUJI Hideaki 	     try_to_del_timer_sync(&mp->timer) >= 0))
286708b202b6SYOSHIFUJI Hideaki 		mod_timer(&mp->timer, now + max_delay);
286808b202b6SYOSHIFUJI Hideaki 
2869e8051688SEric Dumazet 	for (pp = &mp->ports;
2870e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
2871e8051688SEric Dumazet 	     pp = &p->next) {
287208b202b6SYOSHIFUJI Hideaki 		if (timer_pending(&p->timer) ?
287308b202b6SYOSHIFUJI Hideaki 		    time_after(p->timer.expires, now + max_delay) :
287423550b83SNikolay Aleksandrov 		    try_to_del_timer_sync(&p->timer) >= 0 &&
287523550b83SNikolay Aleksandrov 		    (br->multicast_mld_version == 1 ||
287623550b83SNikolay Aleksandrov 		     p->filter_mode == MCAST_EXCLUDE))
287724f9cdcbSHerbert Xu 			mod_timer(&p->timer, now + max_delay);
287808b202b6SYOSHIFUJI Hideaki 	}
287908b202b6SYOSHIFUJI Hideaki 
288008b202b6SYOSHIFUJI Hideaki out:
288108b202b6SYOSHIFUJI Hideaki 	spin_unlock(&br->multicast_lock);
288208b202b6SYOSHIFUJI Hideaki 	return err;
288308b202b6SYOSHIFUJI Hideaki }
288408b202b6SYOSHIFUJI Hideaki #endif
288508b202b6SYOSHIFUJI Hideaki 
288690010b36SLinus Lüssing static void
288790010b36SLinus Lüssing br_multicast_leave_group(struct net_bridge *br,
2888eb1d1641SHerbert Xu 			 struct net_bridge_port *port,
2889cc0fdd80SLinus Lüssing 			 struct br_ip *group,
289090010b36SLinus Lüssing 			 struct bridge_mcast_other_query *other_query,
28916db6f0eaSFelix Fietkau 			 struct bridge_mcast_own_query *own_query,
28926db6f0eaSFelix Fietkau 			 const unsigned char *src)
2893eb1d1641SHerbert Xu {
2894eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
2895eb1d1641SHerbert Xu 	struct net_bridge_port_group *p;
2896eb1d1641SHerbert Xu 	unsigned long now;
2897eb1d1641SHerbert Xu 	unsigned long time;
2898eb1d1641SHerbert Xu 
2899eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
2900eb1d1641SHerbert Xu 	if (!netif_running(br->dev) ||
2901544586f7SSatish Ashok 	    (port && port->state == BR_STATE_DISABLED))
2902eb1d1641SHerbert Xu 		goto out;
2903eb1d1641SHerbert Xu 
290419e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip_get(br, group);
2905eb1d1641SHerbert Xu 	if (!mp)
2906eb1d1641SHerbert Xu 		goto out;
2907eb1d1641SHerbert Xu 
2908544586f7SSatish Ashok 	if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
2909544586f7SSatish Ashok 		struct net_bridge_port_group __rcu **pp;
2910544586f7SSatish Ashok 
2911544586f7SSatish Ashok 		for (pp = &mp->ports;
2912544586f7SSatish Ashok 		     (p = mlock_dereference(*pp, br)) != NULL;
2913544586f7SSatish Ashok 		     pp = &p->next) {
29146db6f0eaSFelix Fietkau 			if (!br_port_group_equal(p, port, src))
2915544586f7SSatish Ashok 				continue;
2916544586f7SSatish Ashok 
29175c725b6bSNikolay Aleksandrov 			if (p->flags & MDB_PG_FLAGS_PERMANENT)
29185c725b6bSNikolay Aleksandrov 				break;
29195c725b6bSNikolay Aleksandrov 
2920681590bdSNikolay Aleksandrov 			p->flags |= MDB_PG_FLAGS_FAST_LEAVE;
2921681590bdSNikolay Aleksandrov 			br_multicast_del_pg(mp, p, pp);
2922544586f7SSatish Ashok 		}
2923544586f7SSatish Ashok 		goto out;
2924544586f7SSatish Ashok 	}
2925544586f7SSatish Ashok 
2926544586f7SSatish Ashok 	if (timer_pending(&other_query->timer))
2927544586f7SSatish Ashok 		goto out;
2928544586f7SSatish Ashok 
2929675779adSNikolay Aleksandrov 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER)) {
2930438ef2d0SNikolay Aleksandrov 		__br_multicast_send_query(br, port, NULL, NULL, &mp->addr,
293142c11ccfSNikolay Aleksandrov 					  false, 0, NULL);
29326b7df111SCong Wang 
29336b7df111SCong Wang 		time = jiffies + br->multicast_last_member_count *
29346b7df111SCong Wang 				 br->multicast_last_member_interval;
2935cc0fdd80SLinus Lüssing 
293690010b36SLinus Lüssing 		mod_timer(&own_query->timer, time);
29376b7df111SCong Wang 
29386b7df111SCong Wang 		for (p = mlock_dereference(mp->ports, br);
29396b7df111SCong Wang 		     p != NULL;
29406b7df111SCong Wang 		     p = mlock_dereference(p->next, br)) {
29416db6f0eaSFelix Fietkau 			if (!br_port_group_equal(p, port, src))
29426b7df111SCong Wang 				continue;
29436b7df111SCong Wang 
29446b7df111SCong Wang 			if (!hlist_unhashed(&p->mglist) &&
29456b7df111SCong Wang 			    (timer_pending(&p->timer) ?
29466b7df111SCong Wang 			     time_after(p->timer.expires, time) :
29476b7df111SCong Wang 			     try_to_del_timer_sync(&p->timer) >= 0)) {
29486b7df111SCong Wang 				mod_timer(&p->timer, time);
29496b7df111SCong Wang 			}
29506b7df111SCong Wang 
29516b7df111SCong Wang 			break;
29526b7df111SCong Wang 		}
29536b7df111SCong Wang 	}
29546b7df111SCong Wang 
2955eb1d1641SHerbert Xu 	now = jiffies;
2956eb1d1641SHerbert Xu 	time = now + br->multicast_last_member_count *
2957eb1d1641SHerbert Xu 		     br->multicast_last_member_interval;
2958eb1d1641SHerbert Xu 
2959eb1d1641SHerbert Xu 	if (!port) {
2960ff0fd34eSAndrew Lunn 		if (mp->host_joined &&
2961eb1d1641SHerbert Xu 		    (timer_pending(&mp->timer) ?
2962eb1d1641SHerbert Xu 		     time_after(mp->timer.expires, time) :
2963eb1d1641SHerbert Xu 		     try_to_del_timer_sync(&mp->timer) >= 0)) {
2964eb1d1641SHerbert Xu 			mod_timer(&mp->timer, time);
2965eb1d1641SHerbert Xu 		}
2966454594f3SLinus Lüssing 
2967454594f3SLinus Lüssing 		goto out;
2968454594f3SLinus Lüssing 	}
2969454594f3SLinus Lüssing 
2970454594f3SLinus Lüssing 	for (p = mlock_dereference(mp->ports, br);
2971454594f3SLinus Lüssing 	     p != NULL;
2972454594f3SLinus Lüssing 	     p = mlock_dereference(p->next, br)) {
2973085b53c8SNikolay Aleksandrov 		if (p->key.port != port)
2974454594f3SLinus Lüssing 			continue;
2975454594f3SLinus Lüssing 
2976454594f3SLinus Lüssing 		if (!hlist_unhashed(&p->mglist) &&
2977454594f3SLinus Lüssing 		    (timer_pending(&p->timer) ?
2978454594f3SLinus Lüssing 		     time_after(p->timer.expires, time) :
2979454594f3SLinus Lüssing 		     try_to_del_timer_sync(&p->timer) >= 0)) {
2980454594f3SLinus Lüssing 			mod_timer(&p->timer, time);
2981454594f3SLinus Lüssing 		}
2982454594f3SLinus Lüssing 
2983454594f3SLinus Lüssing 		break;
2984eb1d1641SHerbert Xu 	}
2985eb1d1641SHerbert Xu out:
2986eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
2987eb1d1641SHerbert Xu }
2988eb1d1641SHerbert Xu 
29898ef2a9a5SYOSHIFUJI Hideaki static void br_ip4_multicast_leave_group(struct net_bridge *br,
29908ef2a9a5SYOSHIFUJI Hideaki 					 struct net_bridge_port *port,
2991b0e9a30dSVlad Yasevich 					 __be32 group,
29926db6f0eaSFelix Fietkau 					 __u16 vid,
29936db6f0eaSFelix Fietkau 					 const unsigned char *src)
29948ef2a9a5SYOSHIFUJI Hideaki {
29958ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip br_group;
299690010b36SLinus Lüssing 	struct bridge_mcast_own_query *own_query;
29978ef2a9a5SYOSHIFUJI Hideaki 
29988ef2a9a5SYOSHIFUJI Hideaki 	if (ipv4_is_local_multicast(group))
29998ef2a9a5SYOSHIFUJI Hideaki 		return;
30008ef2a9a5SYOSHIFUJI Hideaki 
300190010b36SLinus Lüssing 	own_query = port ? &port->ip4_own_query : &br->ip4_own_query;
300290010b36SLinus Lüssing 
30031515a63fSNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
3004eab3227bSNikolay Aleksandrov 	br_group.dst.ip4 = group;
30058ef2a9a5SYOSHIFUJI Hideaki 	br_group.proto = htons(ETH_P_IP);
3006b0e9a30dSVlad Yasevich 	br_group.vid = vid;
30078ef2a9a5SYOSHIFUJI Hideaki 
300890010b36SLinus Lüssing 	br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
30096db6f0eaSFelix Fietkau 				 own_query, src);
30108ef2a9a5SYOSHIFUJI Hideaki }
30118ef2a9a5SYOSHIFUJI Hideaki 
3012dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
301308b202b6SYOSHIFUJI Hideaki static void br_ip6_multicast_leave_group(struct net_bridge *br,
301408b202b6SYOSHIFUJI Hideaki 					 struct net_bridge_port *port,
3015b0e9a30dSVlad Yasevich 					 const struct in6_addr *group,
30166db6f0eaSFelix Fietkau 					 __u16 vid,
30176db6f0eaSFelix Fietkau 					 const unsigned char *src)
301808b202b6SYOSHIFUJI Hideaki {
301908b202b6SYOSHIFUJI Hideaki 	struct br_ip br_group;
302090010b36SLinus Lüssing 	struct bridge_mcast_own_query *own_query;
302108b202b6SYOSHIFUJI Hideaki 
30223c3769e6SLinus Lüssing 	if (ipv6_addr_is_ll_all_nodes(group))
302308b202b6SYOSHIFUJI Hideaki 		return;
302408b202b6SYOSHIFUJI Hideaki 
302590010b36SLinus Lüssing 	own_query = port ? &port->ip6_own_query : &br->ip6_own_query;
302690010b36SLinus Lüssing 
30271515a63fSNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
3028eab3227bSNikolay Aleksandrov 	br_group.dst.ip6 = *group;
302908b202b6SYOSHIFUJI Hideaki 	br_group.proto = htons(ETH_P_IPV6);
3030b0e9a30dSVlad Yasevich 	br_group.vid = vid;
303108b202b6SYOSHIFUJI Hideaki 
303290010b36SLinus Lüssing 	br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
30336db6f0eaSFelix Fietkau 				 own_query, src);
303408b202b6SYOSHIFUJI Hideaki }
303508b202b6SYOSHIFUJI Hideaki #endif
30368ef2a9a5SYOSHIFUJI Hideaki 
30371080ab95SNikolay Aleksandrov static void br_multicast_err_count(const struct net_bridge *br,
30381080ab95SNikolay Aleksandrov 				   const struct net_bridge_port *p,
30391080ab95SNikolay Aleksandrov 				   __be16 proto)
30401080ab95SNikolay Aleksandrov {
30411080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
30421080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats *pstats;
30431080ab95SNikolay Aleksandrov 
3044675779adSNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
30451080ab95SNikolay Aleksandrov 		return;
30461080ab95SNikolay Aleksandrov 
30471080ab95SNikolay Aleksandrov 	if (p)
30481080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
30491080ab95SNikolay Aleksandrov 	else
30501080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
30511080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
30521080ab95SNikolay Aleksandrov 		return;
30531080ab95SNikolay Aleksandrov 
30541080ab95SNikolay Aleksandrov 	pstats = this_cpu_ptr(stats);
30551080ab95SNikolay Aleksandrov 
30561080ab95SNikolay Aleksandrov 	u64_stats_update_begin(&pstats->syncp);
30571080ab95SNikolay Aleksandrov 	switch (proto) {
30581080ab95SNikolay Aleksandrov 	case htons(ETH_P_IP):
30591080ab95SNikolay Aleksandrov 		pstats->mstats.igmp_parse_errors++;
30601080ab95SNikolay Aleksandrov 		break;
30611080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
30621080ab95SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
30631080ab95SNikolay Aleksandrov 		pstats->mstats.mld_parse_errors++;
30641080ab95SNikolay Aleksandrov 		break;
30651080ab95SNikolay Aleksandrov #endif
30661080ab95SNikolay Aleksandrov 	}
30671080ab95SNikolay Aleksandrov 	u64_stats_update_end(&pstats->syncp);
30681080ab95SNikolay Aleksandrov }
30691080ab95SNikolay Aleksandrov 
307091b02d3dSNikolay Aleksandrov static void br_multicast_pim(struct net_bridge *br,
307191b02d3dSNikolay Aleksandrov 			     struct net_bridge_port *port,
307291b02d3dSNikolay Aleksandrov 			     const struct sk_buff *skb)
307391b02d3dSNikolay Aleksandrov {
307491b02d3dSNikolay Aleksandrov 	unsigned int offset = skb_transport_offset(skb);
307591b02d3dSNikolay Aleksandrov 	struct pimhdr *pimhdr, _pimhdr;
307691b02d3dSNikolay Aleksandrov 
307791b02d3dSNikolay Aleksandrov 	pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr);
307891b02d3dSNikolay Aleksandrov 	if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION ||
307991b02d3dSNikolay Aleksandrov 	    pim_hdr_type(pimhdr) != PIM_TYPE_HELLO)
308091b02d3dSNikolay Aleksandrov 		return;
308191b02d3dSNikolay Aleksandrov 
308291b02d3dSNikolay Aleksandrov 	br_multicast_mark_router(br, port);
308391b02d3dSNikolay Aleksandrov }
308491b02d3dSNikolay Aleksandrov 
30854b3087c7SLinus Lüssing static int br_ip4_multicast_mrd_rcv(struct net_bridge *br,
30864b3087c7SLinus Lüssing 				    struct net_bridge_port *port,
30874b3087c7SLinus Lüssing 				    struct sk_buff *skb)
30884b3087c7SLinus Lüssing {
30894b3087c7SLinus Lüssing 	if (ip_hdr(skb)->protocol != IPPROTO_IGMP ||
30904b3087c7SLinus Lüssing 	    igmp_hdr(skb)->type != IGMP_MRDISC_ADV)
30914b3087c7SLinus Lüssing 		return -ENOMSG;
30924b3087c7SLinus Lüssing 
30934b3087c7SLinus Lüssing 	br_multicast_mark_router(br, port);
30944b3087c7SLinus Lüssing 
30954b3087c7SLinus Lüssing 	return 0;
30964b3087c7SLinus Lüssing }
30974b3087c7SLinus Lüssing 
3098eb1d1641SHerbert Xu static int br_multicast_ipv4_rcv(struct net_bridge *br,
3099eb1d1641SHerbert Xu 				 struct net_bridge_port *port,
310006499098SVlad Yasevich 				 struct sk_buff *skb,
310106499098SVlad Yasevich 				 u16 vid)
3102eb1d1641SHerbert Xu {
31036db6f0eaSFelix Fietkau 	const unsigned char *src;
3104eb1d1641SHerbert Xu 	struct igmphdr *ih;
3105eb1d1641SHerbert Xu 	int err;
3106eb1d1641SHerbert Xu 
3107ba5ea614SLinus Lüssing 	err = ip_mc_check_igmp(skb);
3108eb1d1641SHerbert Xu 
31099afd85c9SLinus Lüssing 	if (err == -ENOMSG) {
311091b02d3dSNikolay Aleksandrov 		if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) {
3111bd4265feSHerbert Xu 			BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
311291b02d3dSNikolay Aleksandrov 		} else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) {
311391b02d3dSNikolay Aleksandrov 			if (ip_hdr(skb)->protocol == IPPROTO_PIM)
311491b02d3dSNikolay Aleksandrov 				br_multicast_pim(br, port, skb);
31154b3087c7SLinus Lüssing 		} else if (ipv4_is_all_snoopers(ip_hdr(skb)->daddr)) {
311608e71623SLi RongQing 			br_ip4_multicast_mrd_rcv(br, port, skb);
31174b3087c7SLinus Lüssing 		}
31184b3087c7SLinus Lüssing 
3119eb1d1641SHerbert Xu 		return 0;
31209afd85c9SLinus Lüssing 	} else if (err < 0) {
31211080ab95SNikolay Aleksandrov 		br_multicast_err_count(br, port, skb->protocol);
31229afd85c9SLinus Lüssing 		return err;
3123bd4265feSHerbert Xu 	}
3124eb1d1641SHerbert Xu 
31259afd85c9SLinus Lüssing 	ih = igmp_hdr(skb);
31266db6f0eaSFelix Fietkau 	src = eth_hdr(skb)->h_source;
31271080ab95SNikolay Aleksandrov 	BR_INPUT_SKB_CB(skb)->igmp = ih->type;
3128eb1d1641SHerbert Xu 
3129eb1d1641SHerbert Xu 	switch (ih->type) {
3130eb1d1641SHerbert Xu 	case IGMP_HOST_MEMBERSHIP_REPORT:
3131eb1d1641SHerbert Xu 	case IGMPV2_HOST_MEMBERSHIP_REPORT:
313262b2bcb4SFernando Luis Vázquez Cao 		BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
31338b671779SNikolay Aleksandrov 		err = br_ip4_multicast_add_group(br, port, ih->group, vid, src,
31348b671779SNikolay Aleksandrov 						 true);
3135eb1d1641SHerbert Xu 		break;
3136eb1d1641SHerbert Xu 	case IGMPV3_HOST_MEMBERSHIP_REPORT:
3137ba5ea614SLinus Lüssing 		err = br_ip4_multicast_igmp3_report(br, port, skb, vid);
3138eb1d1641SHerbert Xu 		break;
3139eb1d1641SHerbert Xu 	case IGMP_HOST_MEMBERSHIP_QUERY:
3140ba5ea614SLinus Lüssing 		br_ip4_multicast_query(br, port, skb, vid);
3141eb1d1641SHerbert Xu 		break;
3142eb1d1641SHerbert Xu 	case IGMP_HOST_LEAVE_MESSAGE:
31436db6f0eaSFelix Fietkau 		br_ip4_multicast_leave_group(br, port, ih->group, vid, src);
3144eb1d1641SHerbert Xu 		break;
3145eb1d1641SHerbert Xu 	}
3146eb1d1641SHerbert Xu 
3147a65056ecSNikolay Aleksandrov 	br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
31481080ab95SNikolay Aleksandrov 			   BR_MCAST_DIR_RX);
31491080ab95SNikolay Aleksandrov 
3150eb1d1641SHerbert Xu 	return err;
3151eb1d1641SHerbert Xu }
3152eb1d1641SHerbert Xu 
3153dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
31544b3087c7SLinus Lüssing static int br_ip6_multicast_mrd_rcv(struct net_bridge *br,
31554b3087c7SLinus Lüssing 				    struct net_bridge_port *port,
31564b3087c7SLinus Lüssing 				    struct sk_buff *skb)
31574b3087c7SLinus Lüssing {
31584b3087c7SLinus Lüssing 	int ret;
31594b3087c7SLinus Lüssing 
31604b3087c7SLinus Lüssing 	if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6)
31614b3087c7SLinus Lüssing 		return -ENOMSG;
31624b3087c7SLinus Lüssing 
31634b3087c7SLinus Lüssing 	ret = ipv6_mc_check_icmpv6(skb);
31644b3087c7SLinus Lüssing 	if (ret < 0)
31654b3087c7SLinus Lüssing 		return ret;
31664b3087c7SLinus Lüssing 
31674b3087c7SLinus Lüssing 	if (icmp6_hdr(skb)->icmp6_type != ICMPV6_MRDISC_ADV)
31684b3087c7SLinus Lüssing 		return -ENOMSG;
31694b3087c7SLinus Lüssing 
31704b3087c7SLinus Lüssing 	br_multicast_mark_router(br, port);
31714b3087c7SLinus Lüssing 
31724b3087c7SLinus Lüssing 	return 0;
31734b3087c7SLinus Lüssing }
31744b3087c7SLinus Lüssing 
317508b202b6SYOSHIFUJI Hideaki static int br_multicast_ipv6_rcv(struct net_bridge *br,
317608b202b6SYOSHIFUJI Hideaki 				 struct net_bridge_port *port,
317706499098SVlad Yasevich 				 struct sk_buff *skb,
317806499098SVlad Yasevich 				 u16 vid)
317908b202b6SYOSHIFUJI Hideaki {
31806db6f0eaSFelix Fietkau 	const unsigned char *src;
31819afd85c9SLinus Lüssing 	struct mld_msg *mld;
318208b202b6SYOSHIFUJI Hideaki 	int err;
318308b202b6SYOSHIFUJI Hideaki 
3184ba5ea614SLinus Lüssing 	err = ipv6_mc_check_mld(skb);
318508b202b6SYOSHIFUJI Hideaki 
31869afd85c9SLinus Lüssing 	if (err == -ENOMSG) {
31879afd85c9SLinus Lüssing 		if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
31888fad9c39SLinus Lüssing 			BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
31894b3087c7SLinus Lüssing 
31904b3087c7SLinus Lüssing 		if (ipv6_addr_is_all_snoopers(&ipv6_hdr(skb)->daddr)) {
31914b3087c7SLinus Lüssing 			err = br_ip6_multicast_mrd_rcv(br, port, skb);
31924b3087c7SLinus Lüssing 
31934b3087c7SLinus Lüssing 			if (err < 0 && err != -ENOMSG) {
31944b3087c7SLinus Lüssing 				br_multicast_err_count(br, port, skb->protocol);
31954b3087c7SLinus Lüssing 				return err;
31964b3087c7SLinus Lüssing 			}
31974b3087c7SLinus Lüssing 		}
31984b3087c7SLinus Lüssing 
319908b202b6SYOSHIFUJI Hideaki 		return 0;
32009afd85c9SLinus Lüssing 	} else if (err < 0) {
32011080ab95SNikolay Aleksandrov 		br_multicast_err_count(br, port, skb->protocol);
32029afd85c9SLinus Lüssing 		return err;
320308b202b6SYOSHIFUJI Hideaki 	}
320408b202b6SYOSHIFUJI Hideaki 
32059afd85c9SLinus Lüssing 	mld = (struct mld_msg *)skb_transport_header(skb);
32061080ab95SNikolay Aleksandrov 	BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type;
320708b202b6SYOSHIFUJI Hideaki 
32089afd85c9SLinus Lüssing 	switch (mld->mld_type) {
320908b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_REPORT:
32106db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
3211fc2af6c7SFernando Luis Vázquez Cao 		BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
32126db6f0eaSFelix Fietkau 		err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid,
32138b671779SNikolay Aleksandrov 						 src, true);
321408b202b6SYOSHIFUJI Hideaki 		break;
321508b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MLD2_REPORT:
3216ba5ea614SLinus Lüssing 		err = br_ip6_multicast_mld2_report(br, port, skb, vid);
321708b202b6SYOSHIFUJI Hideaki 		break;
321808b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_QUERY:
3219ba5ea614SLinus Lüssing 		err = br_ip6_multicast_query(br, port, skb, vid);
322008b202b6SYOSHIFUJI Hideaki 		break;
322108b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_REDUCTION:
32226db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
32236db6f0eaSFelix Fietkau 		br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src);
32249afd85c9SLinus Lüssing 		break;
322508b202b6SYOSHIFUJI Hideaki 	}
322608b202b6SYOSHIFUJI Hideaki 
3227a65056ecSNikolay Aleksandrov 	br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
32281080ab95SNikolay Aleksandrov 			   BR_MCAST_DIR_RX);
32291080ab95SNikolay Aleksandrov 
323008b202b6SYOSHIFUJI Hideaki 	return err;
323108b202b6SYOSHIFUJI Hideaki }
323208b202b6SYOSHIFUJI Hideaki #endif
323308b202b6SYOSHIFUJI Hideaki 
3234eb1d1641SHerbert Xu int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
323506499098SVlad Yasevich 		     struct sk_buff *skb, u16 vid)
3236eb1d1641SHerbert Xu {
32371080ab95SNikolay Aleksandrov 	int ret = 0;
32381080ab95SNikolay Aleksandrov 
32391fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 	BR_INPUT_SKB_CB(skb)->igmp = 0;
32401fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 	BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
32411fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 
324213cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
3243eb1d1641SHerbert Xu 		return 0;
3244eb1d1641SHerbert Xu 
3245eb1d1641SHerbert Xu 	switch (skb->protocol) {
3246eb1d1641SHerbert Xu 	case htons(ETH_P_IP):
32471080ab95SNikolay Aleksandrov 		ret = br_multicast_ipv4_rcv(br, port, skb, vid);
32481080ab95SNikolay Aleksandrov 		break;
3249dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
325008b202b6SYOSHIFUJI Hideaki 	case htons(ETH_P_IPV6):
32511080ab95SNikolay Aleksandrov 		ret = br_multicast_ipv6_rcv(br, port, skb, vid);
32521080ab95SNikolay Aleksandrov 		break;
325308b202b6SYOSHIFUJI Hideaki #endif
3254eb1d1641SHerbert Xu 	}
3255eb1d1641SHerbert Xu 
32561080ab95SNikolay Aleksandrov 	return ret;
3257eb1d1641SHerbert Xu }
3258eb1d1641SHerbert Xu 
3259cc0fdd80SLinus Lüssing static void br_multicast_query_expired(struct net_bridge *br,
32602cd41431SLinus Lüssing 				       struct bridge_mcast_own_query *query,
32612cd41431SLinus Lüssing 				       struct bridge_mcast_querier *querier)
3262cc0fdd80SLinus Lüssing {
3263cc0fdd80SLinus Lüssing 	spin_lock(&br->multicast_lock);
3264cc0fdd80SLinus Lüssing 	if (query->startup_sent < br->multicast_startup_query_count)
3265cc0fdd80SLinus Lüssing 		query->startup_sent++;
3266cc0fdd80SLinus Lüssing 
326771d9f614SEric Dumazet 	RCU_INIT_POINTER(querier->port, NULL);
3268cc0fdd80SLinus Lüssing 	br_multicast_send_query(br, NULL, query);
3269cc0fdd80SLinus Lüssing 	spin_unlock(&br->multicast_lock);
3270cc0fdd80SLinus Lüssing }
3271cc0fdd80SLinus Lüssing 
327288c1f37fSAllen Pais static void br_ip4_multicast_query_expired(struct timer_list *t)
3273eb1d1641SHerbert Xu {
327488c1f37fSAllen Pais 	struct net_bridge *br = from_timer(br, t, ip4_own_query.timer);
3275eb1d1641SHerbert Xu 
32762cd41431SLinus Lüssing 	br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier);
3277eb1d1641SHerbert Xu }
3278eb1d1641SHerbert Xu 
3279cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
328088c1f37fSAllen Pais static void br_ip6_multicast_query_expired(struct timer_list *t)
3281cc0fdd80SLinus Lüssing {
328288c1f37fSAllen Pais 	struct net_bridge *br = from_timer(br, t, ip6_own_query.timer);
3283cc0fdd80SLinus Lüssing 
32842cd41431SLinus Lüssing 	br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier);
3285cc0fdd80SLinus Lüssing }
3286cc0fdd80SLinus Lüssing #endif
3287cc0fdd80SLinus Lüssing 
3288e12cec65SNikolay Aleksandrov static void br_multicast_gc_work(struct work_struct *work)
32898b671779SNikolay Aleksandrov {
32908b671779SNikolay Aleksandrov 	struct net_bridge *br = container_of(work, struct net_bridge,
3291e12cec65SNikolay Aleksandrov 					     mcast_gc_work);
32928b671779SNikolay Aleksandrov 	HLIST_HEAD(deleted_head);
32938b671779SNikolay Aleksandrov 
32948b671779SNikolay Aleksandrov 	spin_lock_bh(&br->multicast_lock);
3295e12cec65SNikolay Aleksandrov 	hlist_move_list(&br->mcast_gc_list, &deleted_head);
32968b671779SNikolay Aleksandrov 	spin_unlock_bh(&br->multicast_lock);
32978b671779SNikolay Aleksandrov 
3298e12cec65SNikolay Aleksandrov 	br_multicast_gc(&deleted_head);
32998b671779SNikolay Aleksandrov }
33008b671779SNikolay Aleksandrov 
3301eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br)
3302eb1d1641SHerbert Xu {
3303d08c6bc0SNikolay Aleksandrov 	br->hash_max = BR_MULTICAST_DEFAULT_HASH_MAX;
3304eb1d1641SHerbert Xu 
33057f0aec7aSNikolay Aleksandrov 	br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
3306eb1d1641SHerbert Xu 	br->multicast_last_member_count = 2;
3307eb1d1641SHerbert Xu 	br->multicast_startup_query_count = 2;
3308eb1d1641SHerbert Xu 
3309eb1d1641SHerbert Xu 	br->multicast_last_member_interval = HZ;
3310eb1d1641SHerbert Xu 	br->multicast_query_response_interval = 10 * HZ;
3311eb1d1641SHerbert Xu 	br->multicast_startup_query_interval = 125 * HZ / 4;
3312eb1d1641SHerbert Xu 	br->multicast_query_interval = 125 * HZ;
3313eb1d1641SHerbert Xu 	br->multicast_querier_interval = 255 * HZ;
3314eb1d1641SHerbert Xu 	br->multicast_membership_interval = 260 * HZ;
3315eb1d1641SHerbert Xu 
331690010b36SLinus Lüssing 	br->ip4_other_query.delay_time = 0;
33172cd41431SLinus Lüssing 	br->ip4_querier.port = NULL;
3318aa2ae3e7SNikolay Aleksandrov 	br->multicast_igmp_version = 2;
3319cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3320aa2ae3e7SNikolay Aleksandrov 	br->multicast_mld_version = 1;
332190010b36SLinus Lüssing 	br->ip6_other_query.delay_time = 0;
33222cd41431SLinus Lüssing 	br->ip6_querier.port = NULL;
3323cc0fdd80SLinus Lüssing #endif
33246919622aSIdo Schimmel 	br_opt_toggle(br, BROPT_MULTICAST_ENABLED, true);
3325675779adSNikolay Aleksandrov 	br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true);
3326b00589afSLinus Lüssing 
3327eb1d1641SHerbert Xu 	spin_lock_init(&br->multicast_lock);
332888c1f37fSAllen Pais 	timer_setup(&br->multicast_router_timer,
332988c1f37fSAllen Pais 		    br_multicast_local_router_expired, 0);
333088c1f37fSAllen Pais 	timer_setup(&br->ip4_other_query.timer,
333188c1f37fSAllen Pais 		    br_ip4_multicast_querier_expired, 0);
333288c1f37fSAllen Pais 	timer_setup(&br->ip4_own_query.timer,
333388c1f37fSAllen Pais 		    br_ip4_multicast_query_expired, 0);
3334cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
333588c1f37fSAllen Pais 	timer_setup(&br->ip6_other_query.timer,
333688c1f37fSAllen Pais 		    br_ip6_multicast_querier_expired, 0);
333788c1f37fSAllen Pais 	timer_setup(&br->ip6_own_query.timer,
333888c1f37fSAllen Pais 		    br_ip6_multicast_query_expired, 0);
3339cc0fdd80SLinus Lüssing #endif
334019e3a9c9SNikolay Aleksandrov 	INIT_HLIST_HEAD(&br->mdb_list);
3341e12cec65SNikolay Aleksandrov 	INIT_HLIST_HEAD(&br->mcast_gc_list);
3342e12cec65SNikolay Aleksandrov 	INIT_WORK(&br->mcast_gc_work, br_multicast_gc_work);
3343eb1d1641SHerbert Xu }
3344eb1d1641SHerbert Xu 
33454effd28cSLinus Lüssing static void br_ip4_multicast_join_snoopers(struct net_bridge *br)
33464effd28cSLinus Lüssing {
33474effd28cSLinus Lüssing 	struct in_device *in_dev = in_dev_get(br->dev);
33484effd28cSLinus Lüssing 
33494effd28cSLinus Lüssing 	if (!in_dev)
33504effd28cSLinus Lüssing 		return;
33514effd28cSLinus Lüssing 
33529fb20801SFlorian Fainelli 	__ip_mc_inc_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
33534effd28cSLinus Lüssing 	in_dev_put(in_dev);
33544effd28cSLinus Lüssing }
33554effd28cSLinus Lüssing 
33564effd28cSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
33574effd28cSLinus Lüssing static void br_ip6_multicast_join_snoopers(struct net_bridge *br)
33584effd28cSLinus Lüssing {
33594effd28cSLinus Lüssing 	struct in6_addr addr;
33604effd28cSLinus Lüssing 
33614effd28cSLinus Lüssing 	ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
33624effd28cSLinus Lüssing 	ipv6_dev_mc_inc(br->dev, &addr);
33634effd28cSLinus Lüssing }
33644effd28cSLinus Lüssing #else
33654effd28cSLinus Lüssing static inline void br_ip6_multicast_join_snoopers(struct net_bridge *br)
33664effd28cSLinus Lüssing {
33674effd28cSLinus Lüssing }
33684effd28cSLinus Lüssing #endif
33694effd28cSLinus Lüssing 
3370851d0a73SJoseph Huang void br_multicast_join_snoopers(struct net_bridge *br)
33714effd28cSLinus Lüssing {
33724effd28cSLinus Lüssing 	br_ip4_multicast_join_snoopers(br);
33734effd28cSLinus Lüssing 	br_ip6_multicast_join_snoopers(br);
33744effd28cSLinus Lüssing }
33754effd28cSLinus Lüssing 
33764effd28cSLinus Lüssing static void br_ip4_multicast_leave_snoopers(struct net_bridge *br)
33774effd28cSLinus Lüssing {
33784effd28cSLinus Lüssing 	struct in_device *in_dev = in_dev_get(br->dev);
33794effd28cSLinus Lüssing 
33804effd28cSLinus Lüssing 	if (WARN_ON(!in_dev))
33814effd28cSLinus Lüssing 		return;
33824effd28cSLinus Lüssing 
33839fb20801SFlorian Fainelli 	__ip_mc_dec_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
33844effd28cSLinus Lüssing 	in_dev_put(in_dev);
33854effd28cSLinus Lüssing }
33864effd28cSLinus Lüssing 
33874effd28cSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
33884effd28cSLinus Lüssing static void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
33894effd28cSLinus Lüssing {
33904effd28cSLinus Lüssing 	struct in6_addr addr;
33914effd28cSLinus Lüssing 
33924effd28cSLinus Lüssing 	ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
33934effd28cSLinus Lüssing 	ipv6_dev_mc_dec(br->dev, &addr);
33944effd28cSLinus Lüssing }
33954effd28cSLinus Lüssing #else
33964effd28cSLinus Lüssing static inline void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
33974effd28cSLinus Lüssing {
33984effd28cSLinus Lüssing }
33994effd28cSLinus Lüssing #endif
34004effd28cSLinus Lüssing 
3401851d0a73SJoseph Huang void br_multicast_leave_snoopers(struct net_bridge *br)
34024effd28cSLinus Lüssing {
34034effd28cSLinus Lüssing 	br_ip4_multicast_leave_snoopers(br);
34044effd28cSLinus Lüssing 	br_ip6_multicast_leave_snoopers(br);
34054effd28cSLinus Lüssing }
34064effd28cSLinus Lüssing 
3407cc0fdd80SLinus Lüssing static void __br_multicast_open(struct net_bridge *br,
340890010b36SLinus Lüssing 				struct bridge_mcast_own_query *query)
3409eb1d1641SHerbert Xu {
3410cc0fdd80SLinus Lüssing 	query->startup_sent = 0;
3411eb1d1641SHerbert Xu 
341213cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
3413eb1d1641SHerbert Xu 		return;
3414eb1d1641SHerbert Xu 
3415cc0fdd80SLinus Lüssing 	mod_timer(&query->timer, jiffies);
3416cc0fdd80SLinus Lüssing }
3417cc0fdd80SLinus Lüssing 
3418cc0fdd80SLinus Lüssing void br_multicast_open(struct net_bridge *br)
3419cc0fdd80SLinus Lüssing {
342090010b36SLinus Lüssing 	__br_multicast_open(br, &br->ip4_own_query);
3421cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
342290010b36SLinus Lüssing 	__br_multicast_open(br, &br->ip6_own_query);
3423cc0fdd80SLinus Lüssing #endif
3424eb1d1641SHerbert Xu }
3425eb1d1641SHerbert Xu 
3426eb1d1641SHerbert Xu void br_multicast_stop(struct net_bridge *br)
3427eb1d1641SHerbert Xu {
3428eb1d1641SHerbert Xu 	del_timer_sync(&br->multicast_router_timer);
342990010b36SLinus Lüssing 	del_timer_sync(&br->ip4_other_query.timer);
343090010b36SLinus Lüssing 	del_timer_sync(&br->ip4_own_query.timer);
3431cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
343290010b36SLinus Lüssing 	del_timer_sync(&br->ip6_other_query.timer);
343390010b36SLinus Lüssing 	del_timer_sync(&br->ip6_own_query.timer);
3434cc0fdd80SLinus Lüssing #endif
3435e10177abSSatish Ashok }
3436e10177abSSatish Ashok 
3437e10177abSSatish Ashok void br_multicast_dev_del(struct net_bridge *br)
3438e10177abSSatish Ashok {
3439e10177abSSatish Ashok 	struct net_bridge_mdb_entry *mp;
34408b671779SNikolay Aleksandrov 	HLIST_HEAD(deleted_head);
344119e3a9c9SNikolay Aleksandrov 	struct hlist_node *tmp;
3442eb1d1641SHerbert Xu 
3443eb1d1641SHerbert Xu 	spin_lock_bh(&br->multicast_lock);
3444e12cec65SNikolay Aleksandrov 	hlist_for_each_entry_safe(mp, tmp, &br->mdb_list, mdb_node)
3445e12cec65SNikolay Aleksandrov 		br_multicast_del_mdb_entry(mp);
3446e12cec65SNikolay Aleksandrov 	hlist_move_list(&br->mcast_gc_list, &deleted_head);
3447eb1d1641SHerbert Xu 	spin_unlock_bh(&br->multicast_lock);
344819e3a9c9SNikolay Aleksandrov 
3449e12cec65SNikolay Aleksandrov 	br_multicast_gc(&deleted_head);
3450e12cec65SNikolay Aleksandrov 	cancel_work_sync(&br->mcast_gc_work);
34518b671779SNikolay Aleksandrov 
34524329596cSNikolay Aleksandrov 	rcu_barrier();
3453eb1d1641SHerbert Xu }
34540909e117SHerbert Xu 
34550909e117SHerbert Xu int br_multicast_set_router(struct net_bridge *br, unsigned long val)
34560909e117SHerbert Xu {
34576ae4ae8eSLinus Lüssing 	int err = -EINVAL;
34580909e117SHerbert Xu 
34590909e117SHerbert Xu 	spin_lock_bh(&br->multicast_lock);
34600909e117SHerbert Xu 
34610909e117SHerbert Xu 	switch (val) {
34627f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_DISABLED:
34637f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_PERM:
346477041420SYotam Gigi 		br_mc_router_state_change(br, val == MDB_RTR_TYPE_PERM);
34650909e117SHerbert Xu 		del_timer(&br->multicast_router_timer);
346677041420SYotam Gigi 		br->multicast_router = val;
346777041420SYotam Gigi 		err = 0;
346877041420SYotam Gigi 		break;
34697f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP_QUERY:
347077041420SYotam Gigi 		if (br->multicast_router != MDB_RTR_TYPE_TEMP_QUERY)
347177041420SYotam Gigi 			br_mc_router_state_change(br, false);
34720909e117SHerbert Xu 		br->multicast_router = val;
34730909e117SHerbert Xu 		err = 0;
34740909e117SHerbert Xu 		break;
34750909e117SHerbert Xu 	}
34760909e117SHerbert Xu 
34770909e117SHerbert Xu 	spin_unlock_bh(&br->multicast_lock);
34780909e117SHerbert Xu 
34790909e117SHerbert Xu 	return err;
34800909e117SHerbert Xu }
34810909e117SHerbert Xu 
34827f0aec7aSNikolay Aleksandrov static void __del_port_router(struct net_bridge_port *p)
34837f0aec7aSNikolay Aleksandrov {
34847f0aec7aSNikolay Aleksandrov 	if (hlist_unhashed(&p->rlist))
34857f0aec7aSNikolay Aleksandrov 		return;
34867f0aec7aSNikolay Aleksandrov 	hlist_del_init_rcu(&p->rlist);
34877f0aec7aSNikolay Aleksandrov 	br_rtr_notify(p->br->dev, p, RTM_DELMDB);
34886d549648SNogah Frankel 	br_port_mc_router_state_change(p, false);
3489f12e7d95SNogah Frankel 
3490f12e7d95SNogah Frankel 	/* don't allow timer refresh */
3491f12e7d95SNogah Frankel 	if (p->multicast_router == MDB_RTR_TYPE_TEMP)
3492f12e7d95SNogah Frankel 		p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
34937f0aec7aSNikolay Aleksandrov }
34947f0aec7aSNikolay Aleksandrov 
34950909e117SHerbert Xu int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
34960909e117SHerbert Xu {
34970909e117SHerbert Xu 	struct net_bridge *br = p->br;
3498a55d8246SNikolay Aleksandrov 	unsigned long now = jiffies;
34996ae4ae8eSLinus Lüssing 	int err = -EINVAL;
35000909e117SHerbert Xu 
35010909e117SHerbert Xu 	spin_lock(&br->multicast_lock);
35024950cfd1SNikolay Aleksandrov 	if (p->multicast_router == val) {
3503a55d8246SNikolay Aleksandrov 		/* Refresh the temp router port timer */
3504a55d8246SNikolay Aleksandrov 		if (p->multicast_router == MDB_RTR_TYPE_TEMP)
3505a55d8246SNikolay Aleksandrov 			mod_timer(&p->multicast_router_timer,
3506a55d8246SNikolay Aleksandrov 				  now + br->multicast_querier_interval);
35074950cfd1SNikolay Aleksandrov 		err = 0;
35084950cfd1SNikolay Aleksandrov 		goto unlock;
35094950cfd1SNikolay Aleksandrov 	}
35100909e117SHerbert Xu 	switch (val) {
35117f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_DISABLED:
35127f0aec7aSNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_DISABLED;
35137f0aec7aSNikolay Aleksandrov 		__del_port_router(p);
35140909e117SHerbert Xu 		del_timer(&p->multicast_router_timer);
35150909e117SHerbert Xu 		break;
35167f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP_QUERY:
35177f0aec7aSNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
35187f0aec7aSNikolay Aleksandrov 		__del_port_router(p);
35197f0aec7aSNikolay Aleksandrov 		break;
35207f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_PERM:
35217f0aec7aSNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_PERM;
35227f0aec7aSNikolay Aleksandrov 		del_timer(&p->multicast_router_timer);
35230909e117SHerbert Xu 		br_multicast_add_router(br, p);
35240909e117SHerbert Xu 		break;
3525a55d8246SNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP:
3526a55d8246SNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_TEMP;
3527a55d8246SNikolay Aleksandrov 		br_multicast_mark_router(br, p);
3528a55d8246SNikolay Aleksandrov 		break;
35297f0aec7aSNikolay Aleksandrov 	default:
35307f0aec7aSNikolay Aleksandrov 		goto unlock;
35310909e117SHerbert Xu 	}
35327f0aec7aSNikolay Aleksandrov 	err = 0;
35337f0aec7aSNikolay Aleksandrov unlock:
35340909e117SHerbert Xu 	spin_unlock(&br->multicast_lock);
35350909e117SHerbert Xu 
35360909e117SHerbert Xu 	return err;
35370909e117SHerbert Xu }
3538561f1103SHerbert Xu 
3539cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br,
354090010b36SLinus Lüssing 				       struct bridge_mcast_own_query *query)
3541561f1103SHerbert Xu {
3542561f1103SHerbert Xu 	struct net_bridge_port *port;
354374857216SHerbert Xu 
3544cc0fdd80SLinus Lüssing 	__br_multicast_open(br, query);
354574857216SHerbert Xu 
3546c5b493ceSNikolay Aleksandrov 	rcu_read_lock();
3547c5b493ceSNikolay Aleksandrov 	list_for_each_entry_rcu(port, &br->port_list, list) {
354874857216SHerbert Xu 		if (port->state == BR_STATE_DISABLED ||
354974857216SHerbert Xu 		    port->state == BR_STATE_BLOCKING)
355074857216SHerbert Xu 			continue;
355174857216SHerbert Xu 
355290010b36SLinus Lüssing 		if (query == &br->ip4_own_query)
355390010b36SLinus Lüssing 			br_multicast_enable(&port->ip4_own_query);
3554cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3555cc0fdd80SLinus Lüssing 		else
355690010b36SLinus Lüssing 			br_multicast_enable(&port->ip6_own_query);
3557cc0fdd80SLinus Lüssing #endif
355874857216SHerbert Xu 	}
3559c5b493ceSNikolay Aleksandrov 	rcu_read_unlock();
356074857216SHerbert Xu }
356174857216SHerbert Xu 
356274857216SHerbert Xu int br_multicast_toggle(struct net_bridge *br, unsigned long val)
356374857216SHerbert Xu {
35647cb3f921SNikolay Aleksandrov 	struct net_bridge_port *port;
3565851d0a73SJoseph Huang 	bool change_snoopers = false;
3566561f1103SHerbert Xu 
3567ef5e0d82SAndrey Vagin 	spin_lock_bh(&br->multicast_lock);
356813cefad2SNikolay Aleksandrov 	if (!!br_opt_get(br, BROPT_MULTICAST_ENABLED) == !!val)
3569561f1103SHerbert Xu 		goto unlock;
3570561f1103SHerbert Xu 
357113cefad2SNikolay Aleksandrov 	br_mc_disabled_update(br->dev, val);
357213cefad2SNikolay Aleksandrov 	br_opt_toggle(br, BROPT_MULTICAST_ENABLED, !!val);
35734effd28cSLinus Lüssing 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) {
3574851d0a73SJoseph Huang 		change_snoopers = true;
3575561f1103SHerbert Xu 		goto unlock;
35764effd28cSLinus Lüssing 	}
3577561f1103SHerbert Xu 
35783a7fda06SHerbert Xu 	if (!netif_running(br->dev))
35793a7fda06SHerbert Xu 		goto unlock;
35803a7fda06SHerbert Xu 
35817cb3f921SNikolay Aleksandrov 	br_multicast_open(br);
35827cb3f921SNikolay Aleksandrov 	list_for_each_entry(port, &br->port_list, list)
35837cb3f921SNikolay Aleksandrov 		__br_multicast_enable_port(port);
3584561f1103SHerbert Xu 
3585851d0a73SJoseph Huang 	change_snoopers = true;
3586851d0a73SJoseph Huang 
3587561f1103SHerbert Xu unlock:
3588ef5e0d82SAndrey Vagin 	spin_unlock_bh(&br->multicast_lock);
3589561f1103SHerbert Xu 
3590851d0a73SJoseph Huang 	/* br_multicast_join_snoopers has the potential to cause
3591851d0a73SJoseph Huang 	 * an MLD Report/Leave to be delivered to br_multicast_rcv,
3592851d0a73SJoseph Huang 	 * which would in turn call br_multicast_add_group, which would
3593851d0a73SJoseph Huang 	 * attempt to acquire multicast_lock. This function should be
3594851d0a73SJoseph Huang 	 * called after the lock has been released to avoid deadlocks on
3595851d0a73SJoseph Huang 	 * multicast_lock.
3596851d0a73SJoseph Huang 	 *
3597851d0a73SJoseph Huang 	 * br_multicast_leave_snoopers does not have the problem since
3598851d0a73SJoseph Huang 	 * br_multicast_rcv first checks BROPT_MULTICAST_ENABLED, and
3599851d0a73SJoseph Huang 	 * returns without calling br_multicast_ipv4/6_rcv if it's not
3600851d0a73SJoseph Huang 	 * enabled. Moved both functions out just for symmetry.
3601851d0a73SJoseph Huang 	 */
3602851d0a73SJoseph Huang 	if (change_snoopers) {
3603851d0a73SJoseph Huang 		if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
3604851d0a73SJoseph Huang 			br_multicast_join_snoopers(br);
3605851d0a73SJoseph Huang 		else
3606851d0a73SJoseph Huang 			br_multicast_leave_snoopers(br);
3607851d0a73SJoseph Huang 	}
3608851d0a73SJoseph Huang 
3609a26d94bfSYueHaibing 	return 0;
3610561f1103SHerbert Xu }
3611b195167fSHerbert Xu 
36129341b988SIdo Schimmel bool br_multicast_enabled(const struct net_device *dev)
36139341b988SIdo Schimmel {
36149341b988SIdo Schimmel 	struct net_bridge *br = netdev_priv(dev);
36159341b988SIdo Schimmel 
361613cefad2SNikolay Aleksandrov 	return !!br_opt_get(br, BROPT_MULTICAST_ENABLED);
36179341b988SIdo Schimmel }
36189341b988SIdo Schimmel EXPORT_SYMBOL_GPL(br_multicast_enabled);
36199341b988SIdo Schimmel 
36200912bda4SYotam Gigi bool br_multicast_router(const struct net_device *dev)
36210912bda4SYotam Gigi {
36220912bda4SYotam Gigi 	struct net_bridge *br = netdev_priv(dev);
36230912bda4SYotam Gigi 	bool is_router;
36240912bda4SYotam Gigi 
36250912bda4SYotam Gigi 	spin_lock_bh(&br->multicast_lock);
36260912bda4SYotam Gigi 	is_router = br_multicast_is_router(br);
36270912bda4SYotam Gigi 	spin_unlock_bh(&br->multicast_lock);
36280912bda4SYotam Gigi 	return is_router;
36290912bda4SYotam Gigi }
36300912bda4SYotam Gigi EXPORT_SYMBOL_GPL(br_multicast_router);
36310912bda4SYotam Gigi 
3632c5c23260SHerbert Xu int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
3633c5c23260SHerbert Xu {
3634b00589afSLinus Lüssing 	unsigned long max_delay;
3635b00589afSLinus Lüssing 
3636c5c23260SHerbert Xu 	val = !!val;
3637c5c23260SHerbert Xu 
3638c5c23260SHerbert Xu 	spin_lock_bh(&br->multicast_lock);
3639675779adSNikolay Aleksandrov 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER) == val)
3640c5c23260SHerbert Xu 		goto unlock;
3641c5c23260SHerbert Xu 
3642675779adSNikolay Aleksandrov 	br_opt_toggle(br, BROPT_MULTICAST_QUERIER, !!val);
3643b00589afSLinus Lüssing 	if (!val)
3644b00589afSLinus Lüssing 		goto unlock;
3645b00589afSLinus Lüssing 
3646b00589afSLinus Lüssing 	max_delay = br->multicast_query_response_interval;
3647b00589afSLinus Lüssing 
364890010b36SLinus Lüssing 	if (!timer_pending(&br->ip4_other_query.timer))
364990010b36SLinus Lüssing 		br->ip4_other_query.delay_time = jiffies + max_delay;
3650cc0fdd80SLinus Lüssing 
365190010b36SLinus Lüssing 	br_multicast_start_querier(br, &br->ip4_own_query);
3652cc0fdd80SLinus Lüssing 
3653cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
365490010b36SLinus Lüssing 	if (!timer_pending(&br->ip6_other_query.timer))
365590010b36SLinus Lüssing 		br->ip6_other_query.delay_time = jiffies + max_delay;
3656cc0fdd80SLinus Lüssing 
365790010b36SLinus Lüssing 	br_multicast_start_querier(br, &br->ip6_own_query);
3658cc0fdd80SLinus Lüssing #endif
3659c5c23260SHerbert Xu 
3660c5c23260SHerbert Xu unlock:
3661c5c23260SHerbert Xu 	spin_unlock_bh(&br->multicast_lock);
3662c5c23260SHerbert Xu 
3663c5c23260SHerbert Xu 	return 0;
3664c5c23260SHerbert Xu }
3665c5c23260SHerbert Xu 
36665e923585SNikolay Aleksandrov int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val)
36675e923585SNikolay Aleksandrov {
36685e923585SNikolay Aleksandrov 	/* Currently we support only version 2 and 3 */
36695e923585SNikolay Aleksandrov 	switch (val) {
36705e923585SNikolay Aleksandrov 	case 2:
36715e923585SNikolay Aleksandrov 	case 3:
36725e923585SNikolay Aleksandrov 		break;
36735e923585SNikolay Aleksandrov 	default:
36745e923585SNikolay Aleksandrov 		return -EINVAL;
36755e923585SNikolay Aleksandrov 	}
36765e923585SNikolay Aleksandrov 
36775e923585SNikolay Aleksandrov 	spin_lock_bh(&br->multicast_lock);
36785e923585SNikolay Aleksandrov 	br->multicast_igmp_version = val;
36795e923585SNikolay Aleksandrov 	spin_unlock_bh(&br->multicast_lock);
36805e923585SNikolay Aleksandrov 
36815e923585SNikolay Aleksandrov 	return 0;
36825e923585SNikolay Aleksandrov }
36835e923585SNikolay Aleksandrov 
3684aa2ae3e7SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
3685aa2ae3e7SNikolay Aleksandrov int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val)
3686aa2ae3e7SNikolay Aleksandrov {
3687aa2ae3e7SNikolay Aleksandrov 	/* Currently we support version 1 and 2 */
3688aa2ae3e7SNikolay Aleksandrov 	switch (val) {
3689aa2ae3e7SNikolay Aleksandrov 	case 1:
3690aa2ae3e7SNikolay Aleksandrov 	case 2:
3691aa2ae3e7SNikolay Aleksandrov 		break;
3692aa2ae3e7SNikolay Aleksandrov 	default:
3693aa2ae3e7SNikolay Aleksandrov 		return -EINVAL;
3694aa2ae3e7SNikolay Aleksandrov 	}
3695aa2ae3e7SNikolay Aleksandrov 
3696aa2ae3e7SNikolay Aleksandrov 	spin_lock_bh(&br->multicast_lock);
3697aa2ae3e7SNikolay Aleksandrov 	br->multicast_mld_version = val;
3698aa2ae3e7SNikolay Aleksandrov 	spin_unlock_bh(&br->multicast_lock);
3699aa2ae3e7SNikolay Aleksandrov 
3700aa2ae3e7SNikolay Aleksandrov 	return 0;
3701aa2ae3e7SNikolay Aleksandrov }
3702aa2ae3e7SNikolay Aleksandrov #endif
3703aa2ae3e7SNikolay Aleksandrov 
370407f8ac4aSLinus Lüssing /**
370507f8ac4aSLinus Lüssing  * br_multicast_list_adjacent - Returns snooped multicast addresses
370607f8ac4aSLinus Lüssing  * @dev:	The bridge port adjacent to which to retrieve addresses
370707f8ac4aSLinus Lüssing  * @br_ip_list:	The list to store found, snooped multicast IP addresses in
370807f8ac4aSLinus Lüssing  *
370907f8ac4aSLinus Lüssing  * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
371007f8ac4aSLinus Lüssing  * snooping feature on all bridge ports of dev's bridge device, excluding
371107f8ac4aSLinus Lüssing  * the addresses from dev itself.
371207f8ac4aSLinus Lüssing  *
371307f8ac4aSLinus Lüssing  * Returns the number of items added to br_ip_list.
371407f8ac4aSLinus Lüssing  *
371507f8ac4aSLinus Lüssing  * Notes:
371607f8ac4aSLinus Lüssing  * - br_ip_list needs to be initialized by caller
371707f8ac4aSLinus Lüssing  * - br_ip_list might contain duplicates in the end
371807f8ac4aSLinus Lüssing  *   (needs to be taken care of by caller)
371907f8ac4aSLinus Lüssing  * - br_ip_list needs to be freed by caller
372007f8ac4aSLinus Lüssing  */
372107f8ac4aSLinus Lüssing int br_multicast_list_adjacent(struct net_device *dev,
372207f8ac4aSLinus Lüssing 			       struct list_head *br_ip_list)
372307f8ac4aSLinus Lüssing {
372407f8ac4aSLinus Lüssing 	struct net_bridge *br;
372507f8ac4aSLinus Lüssing 	struct net_bridge_port *port;
372607f8ac4aSLinus Lüssing 	struct net_bridge_port_group *group;
372707f8ac4aSLinus Lüssing 	struct br_ip_list *entry;
372807f8ac4aSLinus Lüssing 	int count = 0;
372907f8ac4aSLinus Lüssing 
373007f8ac4aSLinus Lüssing 	rcu_read_lock();
373135f861e3SJulian Wiedmann 	if (!br_ip_list || !netif_is_bridge_port(dev))
373207f8ac4aSLinus Lüssing 		goto unlock;
373307f8ac4aSLinus Lüssing 
373407f8ac4aSLinus Lüssing 	port = br_port_get_rcu(dev);
373507f8ac4aSLinus Lüssing 	if (!port || !port->br)
373607f8ac4aSLinus Lüssing 		goto unlock;
373707f8ac4aSLinus Lüssing 
373807f8ac4aSLinus Lüssing 	br = port->br;
373907f8ac4aSLinus Lüssing 
374007f8ac4aSLinus Lüssing 	list_for_each_entry_rcu(port, &br->port_list, list) {
374107f8ac4aSLinus Lüssing 		if (!port->dev || port->dev == dev)
374207f8ac4aSLinus Lüssing 			continue;
374307f8ac4aSLinus Lüssing 
374407f8ac4aSLinus Lüssing 		hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
374507f8ac4aSLinus Lüssing 			entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
374607f8ac4aSLinus Lüssing 			if (!entry)
374707f8ac4aSLinus Lüssing 				goto unlock;
374807f8ac4aSLinus Lüssing 
3749085b53c8SNikolay Aleksandrov 			entry->addr = group->key.addr;
375007f8ac4aSLinus Lüssing 			list_add(&entry->list, br_ip_list);
375107f8ac4aSLinus Lüssing 			count++;
375207f8ac4aSLinus Lüssing 		}
375307f8ac4aSLinus Lüssing 	}
375407f8ac4aSLinus Lüssing 
375507f8ac4aSLinus Lüssing unlock:
375607f8ac4aSLinus Lüssing 	rcu_read_unlock();
375707f8ac4aSLinus Lüssing 	return count;
375807f8ac4aSLinus Lüssing }
375907f8ac4aSLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
37602cd41431SLinus Lüssing 
37612cd41431SLinus Lüssing /**
3762c34963e2SLinus Lüssing  * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
3763c34963e2SLinus Lüssing  * @dev: The bridge port providing the bridge on which to check for a querier
3764c34963e2SLinus Lüssing  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
3765c34963e2SLinus Lüssing  *
3766c34963e2SLinus Lüssing  * Checks whether the given interface has a bridge on top and if so returns
3767c34963e2SLinus Lüssing  * true if a valid querier exists anywhere on the bridged link layer.
3768c34963e2SLinus Lüssing  * Otherwise returns false.
3769c34963e2SLinus Lüssing  */
3770c34963e2SLinus Lüssing bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
3771c34963e2SLinus Lüssing {
3772c34963e2SLinus Lüssing 	struct net_bridge *br;
3773c34963e2SLinus Lüssing 	struct net_bridge_port *port;
3774c34963e2SLinus Lüssing 	struct ethhdr eth;
3775c34963e2SLinus Lüssing 	bool ret = false;
3776c34963e2SLinus Lüssing 
3777c34963e2SLinus Lüssing 	rcu_read_lock();
377835f861e3SJulian Wiedmann 	if (!netif_is_bridge_port(dev))
3779c34963e2SLinus Lüssing 		goto unlock;
3780c34963e2SLinus Lüssing 
3781c34963e2SLinus Lüssing 	port = br_port_get_rcu(dev);
3782c34963e2SLinus Lüssing 	if (!port || !port->br)
3783c34963e2SLinus Lüssing 		goto unlock;
3784c34963e2SLinus Lüssing 
3785c34963e2SLinus Lüssing 	br = port->br;
3786c34963e2SLinus Lüssing 
3787c34963e2SLinus Lüssing 	memset(&eth, 0, sizeof(eth));
3788c34963e2SLinus Lüssing 	eth.h_proto = htons(proto);
3789c34963e2SLinus Lüssing 
3790955062b0SNikolay Aleksandrov 	ret = br_multicast_querier_exists(br, &eth, NULL);
3791c34963e2SLinus Lüssing 
3792c34963e2SLinus Lüssing unlock:
3793c34963e2SLinus Lüssing 	rcu_read_unlock();
3794c34963e2SLinus Lüssing 	return ret;
3795c34963e2SLinus Lüssing }
3796c34963e2SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
3797c34963e2SLinus Lüssing 
3798c34963e2SLinus Lüssing /**
37992cd41431SLinus Lüssing  * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
38002cd41431SLinus Lüssing  * @dev: The bridge port adjacent to which to check for a querier
38012cd41431SLinus Lüssing  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
38022cd41431SLinus Lüssing  *
38032cd41431SLinus Lüssing  * Checks whether the given interface has a bridge on top and if so returns
38042cd41431SLinus Lüssing  * true if a selected querier is behind one of the other ports of this
38052cd41431SLinus Lüssing  * bridge. Otherwise returns false.
38062cd41431SLinus Lüssing  */
38072cd41431SLinus Lüssing bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
38082cd41431SLinus Lüssing {
38092cd41431SLinus Lüssing 	struct net_bridge *br;
38102cd41431SLinus Lüssing 	struct net_bridge_port *port;
38112cd41431SLinus Lüssing 	bool ret = false;
38122cd41431SLinus Lüssing 
38132cd41431SLinus Lüssing 	rcu_read_lock();
381435f861e3SJulian Wiedmann 	if (!netif_is_bridge_port(dev))
38152cd41431SLinus Lüssing 		goto unlock;
38162cd41431SLinus Lüssing 
38172cd41431SLinus Lüssing 	port = br_port_get_rcu(dev);
38182cd41431SLinus Lüssing 	if (!port || !port->br)
38192cd41431SLinus Lüssing 		goto unlock;
38202cd41431SLinus Lüssing 
38212cd41431SLinus Lüssing 	br = port->br;
38222cd41431SLinus Lüssing 
38232cd41431SLinus Lüssing 	switch (proto) {
38242cd41431SLinus Lüssing 	case ETH_P_IP:
38252cd41431SLinus Lüssing 		if (!timer_pending(&br->ip4_other_query.timer) ||
38262cd41431SLinus Lüssing 		    rcu_dereference(br->ip4_querier.port) == port)
38272cd41431SLinus Lüssing 			goto unlock;
38282cd41431SLinus Lüssing 		break;
38293993c4e1SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
38302cd41431SLinus Lüssing 	case ETH_P_IPV6:
38312cd41431SLinus Lüssing 		if (!timer_pending(&br->ip6_other_query.timer) ||
38322cd41431SLinus Lüssing 		    rcu_dereference(br->ip6_querier.port) == port)
38332cd41431SLinus Lüssing 			goto unlock;
38342cd41431SLinus Lüssing 		break;
38353993c4e1SLinus Lüssing #endif
38362cd41431SLinus Lüssing 	default:
38372cd41431SLinus Lüssing 		goto unlock;
38382cd41431SLinus Lüssing 	}
38392cd41431SLinus Lüssing 
38402cd41431SLinus Lüssing 	ret = true;
38412cd41431SLinus Lüssing unlock:
38422cd41431SLinus Lüssing 	rcu_read_unlock();
38432cd41431SLinus Lüssing 	return ret;
38442cd41431SLinus Lüssing }
38452cd41431SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
38461080ab95SNikolay Aleksandrov 
38471080ab95SNikolay Aleksandrov static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
3848a65056ecSNikolay Aleksandrov 			       const struct sk_buff *skb, u8 type, u8 dir)
38491080ab95SNikolay Aleksandrov {
38501080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
3851a65056ecSNikolay Aleksandrov 	__be16 proto = skb->protocol;
3852a65056ecSNikolay Aleksandrov 	unsigned int t_len;
38531080ab95SNikolay Aleksandrov 
38541080ab95SNikolay Aleksandrov 	u64_stats_update_begin(&pstats->syncp);
38551080ab95SNikolay Aleksandrov 	switch (proto) {
38561080ab95SNikolay Aleksandrov 	case htons(ETH_P_IP):
3857a65056ecSNikolay Aleksandrov 		t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
38581080ab95SNikolay Aleksandrov 		switch (type) {
38591080ab95SNikolay Aleksandrov 		case IGMP_HOST_MEMBERSHIP_REPORT:
38601080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v1reports[dir]++;
38611080ab95SNikolay Aleksandrov 			break;
38621080ab95SNikolay Aleksandrov 		case IGMPV2_HOST_MEMBERSHIP_REPORT:
38631080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v2reports[dir]++;
38641080ab95SNikolay Aleksandrov 			break;
38651080ab95SNikolay Aleksandrov 		case IGMPV3_HOST_MEMBERSHIP_REPORT:
38661080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v3reports[dir]++;
38671080ab95SNikolay Aleksandrov 			break;
38681080ab95SNikolay Aleksandrov 		case IGMP_HOST_MEMBERSHIP_QUERY:
3869a65056ecSNikolay Aleksandrov 			if (t_len != sizeof(struct igmphdr)) {
3870a65056ecSNikolay Aleksandrov 				pstats->mstats.igmp_v3queries[dir]++;
3871a65056ecSNikolay Aleksandrov 			} else {
3872a65056ecSNikolay Aleksandrov 				unsigned int offset = skb_transport_offset(skb);
3873a65056ecSNikolay Aleksandrov 				struct igmphdr *ih, _ihdr;
3874a65056ecSNikolay Aleksandrov 
3875a65056ecSNikolay Aleksandrov 				ih = skb_header_pointer(skb, offset,
3876a65056ecSNikolay Aleksandrov 							sizeof(_ihdr), &_ihdr);
3877a65056ecSNikolay Aleksandrov 				if (!ih)
3878a65056ecSNikolay Aleksandrov 					break;
3879a65056ecSNikolay Aleksandrov 				if (!ih->code)
3880a65056ecSNikolay Aleksandrov 					pstats->mstats.igmp_v1queries[dir]++;
3881a65056ecSNikolay Aleksandrov 				else
3882a65056ecSNikolay Aleksandrov 					pstats->mstats.igmp_v2queries[dir]++;
3883a65056ecSNikolay Aleksandrov 			}
38841080ab95SNikolay Aleksandrov 			break;
38851080ab95SNikolay Aleksandrov 		case IGMP_HOST_LEAVE_MESSAGE:
38861080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_leaves[dir]++;
38871080ab95SNikolay Aleksandrov 			break;
38881080ab95SNikolay Aleksandrov 		}
38891080ab95SNikolay Aleksandrov 		break;
38901080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
38911080ab95SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
3892a65056ecSNikolay Aleksandrov 		t_len = ntohs(ipv6_hdr(skb)->payload_len) +
3893a65056ecSNikolay Aleksandrov 			sizeof(struct ipv6hdr);
3894a65056ecSNikolay Aleksandrov 		t_len -= skb_network_header_len(skb);
38951080ab95SNikolay Aleksandrov 		switch (type) {
38961080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_REPORT:
38971080ab95SNikolay Aleksandrov 			pstats->mstats.mld_v1reports[dir]++;
38981080ab95SNikolay Aleksandrov 			break;
38991080ab95SNikolay Aleksandrov 		case ICMPV6_MLD2_REPORT:
39001080ab95SNikolay Aleksandrov 			pstats->mstats.mld_v2reports[dir]++;
39011080ab95SNikolay Aleksandrov 			break;
39021080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_QUERY:
3903a65056ecSNikolay Aleksandrov 			if (t_len != sizeof(struct mld_msg))
3904a65056ecSNikolay Aleksandrov 				pstats->mstats.mld_v2queries[dir]++;
3905a65056ecSNikolay Aleksandrov 			else
3906a65056ecSNikolay Aleksandrov 				pstats->mstats.mld_v1queries[dir]++;
39071080ab95SNikolay Aleksandrov 			break;
39081080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_REDUCTION:
39091080ab95SNikolay Aleksandrov 			pstats->mstats.mld_leaves[dir]++;
39101080ab95SNikolay Aleksandrov 			break;
39111080ab95SNikolay Aleksandrov 		}
39121080ab95SNikolay Aleksandrov 		break;
39131080ab95SNikolay Aleksandrov #endif /* CONFIG_IPV6 */
39141080ab95SNikolay Aleksandrov 	}
39151080ab95SNikolay Aleksandrov 	u64_stats_update_end(&pstats->syncp);
39161080ab95SNikolay Aleksandrov }
39171080ab95SNikolay Aleksandrov 
39181080ab95SNikolay Aleksandrov void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
3919a65056ecSNikolay Aleksandrov 			const struct sk_buff *skb, u8 type, u8 dir)
39201080ab95SNikolay Aleksandrov {
39211080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
39221080ab95SNikolay Aleksandrov 
39231080ab95SNikolay Aleksandrov 	/* if multicast_disabled is true then igmp type can't be set */
3924675779adSNikolay Aleksandrov 	if (!type || !br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
39251080ab95SNikolay Aleksandrov 		return;
39261080ab95SNikolay Aleksandrov 
39271080ab95SNikolay Aleksandrov 	if (p)
39281080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
39291080ab95SNikolay Aleksandrov 	else
39301080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
39311080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
39321080ab95SNikolay Aleksandrov 		return;
39331080ab95SNikolay Aleksandrov 
3934a65056ecSNikolay Aleksandrov 	br_mcast_stats_add(stats, skb, type, dir);
39351080ab95SNikolay Aleksandrov }
39361080ab95SNikolay Aleksandrov 
39371080ab95SNikolay Aleksandrov int br_multicast_init_stats(struct net_bridge *br)
39381080ab95SNikolay Aleksandrov {
39391080ab95SNikolay Aleksandrov 	br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
39401080ab95SNikolay Aleksandrov 	if (!br->mcast_stats)
39411080ab95SNikolay Aleksandrov 		return -ENOMEM;
39421080ab95SNikolay Aleksandrov 
39431080ab95SNikolay Aleksandrov 	return 0;
39441080ab95SNikolay Aleksandrov }
39451080ab95SNikolay Aleksandrov 
3946b6fe0440SIdo Schimmel void br_multicast_uninit_stats(struct net_bridge *br)
3947b6fe0440SIdo Schimmel {
3948b6fe0440SIdo Schimmel 	free_percpu(br->mcast_stats);
3949b6fe0440SIdo Schimmel }
3950b6fe0440SIdo Schimmel 
3951b3b6a84cSArnd Bergmann /* noinline for https://bugs.llvm.org/show_bug.cgi?id=45802#c9 */
3952b3b6a84cSArnd Bergmann static noinline_for_stack void mcast_stats_add_dir(u64 *dst, u64 *src)
39531080ab95SNikolay Aleksandrov {
39541080ab95SNikolay Aleksandrov 	dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
39551080ab95SNikolay Aleksandrov 	dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
39561080ab95SNikolay Aleksandrov }
39571080ab95SNikolay Aleksandrov 
39581080ab95SNikolay Aleksandrov void br_multicast_get_stats(const struct net_bridge *br,
39591080ab95SNikolay Aleksandrov 			    const struct net_bridge_port *p,
39601080ab95SNikolay Aleksandrov 			    struct br_mcast_stats *dest)
39611080ab95SNikolay Aleksandrov {
39621080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
39631080ab95SNikolay Aleksandrov 	struct br_mcast_stats tdst;
39641080ab95SNikolay Aleksandrov 	int i;
39651080ab95SNikolay Aleksandrov 
39661080ab95SNikolay Aleksandrov 	memset(dest, 0, sizeof(*dest));
39671080ab95SNikolay Aleksandrov 	if (p)
39681080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
39691080ab95SNikolay Aleksandrov 	else
39701080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
39711080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
39721080ab95SNikolay Aleksandrov 		return;
39731080ab95SNikolay Aleksandrov 
39741080ab95SNikolay Aleksandrov 	memset(&tdst, 0, sizeof(tdst));
39751080ab95SNikolay Aleksandrov 	for_each_possible_cpu(i) {
39761080ab95SNikolay Aleksandrov 		struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
39771080ab95SNikolay Aleksandrov 		struct br_mcast_stats temp;
39781080ab95SNikolay Aleksandrov 		unsigned int start;
39791080ab95SNikolay Aleksandrov 
39801080ab95SNikolay Aleksandrov 		do {
39811080ab95SNikolay Aleksandrov 			start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
39821080ab95SNikolay Aleksandrov 			memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
39831080ab95SNikolay Aleksandrov 		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
39841080ab95SNikolay Aleksandrov 
3985a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
3986a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
3987a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
39881080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
39891080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
39901080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
39911080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
39921080ab95SNikolay Aleksandrov 		tdst.igmp_parse_errors += temp.igmp_parse_errors;
39931080ab95SNikolay Aleksandrov 
3994a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
3995a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
39961080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
39971080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
39981080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
39991080ab95SNikolay Aleksandrov 		tdst.mld_parse_errors += temp.mld_parse_errors;
40001080ab95SNikolay Aleksandrov 	}
40011080ab95SNikolay Aleksandrov 	memcpy(dest, &tdst, sizeof(*dest));
40021080ab95SNikolay Aleksandrov }
400319e3a9c9SNikolay Aleksandrov 
400419e3a9c9SNikolay Aleksandrov int br_mdb_hash_init(struct net_bridge *br)
400519e3a9c9SNikolay Aleksandrov {
4006085b53c8SNikolay Aleksandrov 	int err;
4007085b53c8SNikolay Aleksandrov 
4008085b53c8SNikolay Aleksandrov 	err = rhashtable_init(&br->sg_port_tbl, &br_sg_port_rht_params);
4009085b53c8SNikolay Aleksandrov 	if (err)
4010085b53c8SNikolay Aleksandrov 		return err;
4011085b53c8SNikolay Aleksandrov 
4012085b53c8SNikolay Aleksandrov 	err = rhashtable_init(&br->mdb_hash_tbl, &br_mdb_rht_params);
4013085b53c8SNikolay Aleksandrov 	if (err) {
4014085b53c8SNikolay Aleksandrov 		rhashtable_destroy(&br->sg_port_tbl);
4015085b53c8SNikolay Aleksandrov 		return err;
4016085b53c8SNikolay Aleksandrov 	}
4017085b53c8SNikolay Aleksandrov 
4018085b53c8SNikolay Aleksandrov 	return 0;
401919e3a9c9SNikolay Aleksandrov }
402019e3a9c9SNikolay Aleksandrov 
402119e3a9c9SNikolay Aleksandrov void br_mdb_hash_fini(struct net_bridge *br)
402219e3a9c9SNikolay Aleksandrov {
4023085b53c8SNikolay Aleksandrov 	rhashtable_destroy(&br->sg_port_tbl);
402419e3a9c9SNikolay Aleksandrov 	rhashtable_destroy(&br->mdb_hash_tbl);
402519e3a9c9SNikolay Aleksandrov }
4026