xref: /openbmc/linux/net/bridge/br_multicast.c (revision b19232effd09c2cb5e11b1b74547406a3c9adc5a)
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))
12541697291dSXu Wang 		return ERR_CAST(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 */
13038d21c882SJiapeng 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 =
1360ce6f7097SLinus Lüssing 			from_timer(port, t, ip4_mc_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 ||
1366ce6f7097SLinus Lüssing 	    timer_pending(&port->ip4_mc_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 
1384dcbdf135SVladimir Oltean 	switchdev_port_attr_set(p->dev, &attr, NULL);
138577041420SYotam Gigi }
138677041420SYotam Gigi 
138788c1f37fSAllen Pais static void br_multicast_local_router_expired(struct timer_list *t)
1388eb1d1641SHerbert Xu {
1389ce6f7097SLinus Lüssing 	struct net_bridge *br = from_timer(br, t, ip4_mc_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 ||
1394ce6f7097SLinus Lüssing 	    timer_pending(&br->ip4_mc_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 
1596ae1ea84bSFlorian Fainelli static int br_mc_disabled_update(struct net_device *dev, bool value,
1597ae1ea84bSFlorian Fainelli 				 struct netlink_ext_ack *extack)
1598147c1e9bSNogah Frankel {
1599147c1e9bSNogah Frankel 	struct switchdev_attr attr = {
1600147c1e9bSNogah Frankel 		.orig_dev = dev,
1601147c1e9bSNogah Frankel 		.id = SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED,
1602147c1e9bSNogah Frankel 		.flags = SWITCHDEV_F_DEFER,
160313cefad2SNikolay Aleksandrov 		.u.mc_disabled = !value,
1604147c1e9bSNogah Frankel 	};
1605147c1e9bSNogah Frankel 
1606ae1ea84bSFlorian Fainelli 	return switchdev_port_attr_set(dev, &attr, extack);
1607147c1e9bSNogah Frankel }
1608147c1e9bSNogah Frankel 
16091080ab95SNikolay Aleksandrov int br_multicast_add_port(struct net_bridge_port *port)
1610eb1d1641SHerbert Xu {
1611ae1ea84bSFlorian Fainelli 	int err;
1612ae1ea84bSFlorian Fainelli 
16137f0aec7aSNikolay Aleksandrov 	port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
161489268b05SNikolay Aleksandrov 	port->multicast_eht_hosts_limit = BR_MCAST_DEFAULT_EHT_HOSTS_LIMIT;
1615eb1d1641SHerbert Xu 
1616ce6f7097SLinus Lüssing 	timer_setup(&port->ip4_mc_router_timer,
161788c1f37fSAllen Pais 		    br_multicast_router_expired, 0);
161888c1f37fSAllen Pais 	timer_setup(&port->ip4_own_query.timer,
161988c1f37fSAllen Pais 		    br_ip4_multicast_port_query_expired, 0);
1620cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
162188c1f37fSAllen Pais 	timer_setup(&port->ip6_own_query.timer,
162288c1f37fSAllen Pais 		    br_ip6_multicast_port_query_expired, 0);
1623cc0fdd80SLinus Lüssing #endif
1624ae1ea84bSFlorian Fainelli 	err = br_mc_disabled_update(port->dev,
1625ae1ea84bSFlorian Fainelli 				    br_opt_get(port->br,
1626ae1ea84bSFlorian Fainelli 					       BROPT_MULTICAST_ENABLED),
1627ae1ea84bSFlorian Fainelli 				    NULL);
162868f5c12aSVladimir Oltean 	if (err && err != -EOPNOTSUPP)
1629ae1ea84bSFlorian Fainelli 		return err;
1630147c1e9bSNogah Frankel 
16311080ab95SNikolay Aleksandrov 	port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
16321080ab95SNikolay Aleksandrov 	if (!port->mcast_stats)
16331080ab95SNikolay Aleksandrov 		return -ENOMEM;
16341080ab95SNikolay Aleksandrov 
16351080ab95SNikolay Aleksandrov 	return 0;
1636eb1d1641SHerbert Xu }
1637eb1d1641SHerbert Xu 
1638eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port)
1639eb1d1641SHerbert Xu {
1640e10177abSSatish Ashok 	struct net_bridge *br = port->br;
1641e10177abSSatish Ashok 	struct net_bridge_port_group *pg;
1642e12cec65SNikolay Aleksandrov 	HLIST_HEAD(deleted_head);
1643e10177abSSatish Ashok 	struct hlist_node *n;
1644e10177abSSatish Ashok 
1645e10177abSSatish Ashok 	/* Take care of the remaining groups, only perm ones should be left */
1646e10177abSSatish Ashok 	spin_lock_bh(&br->multicast_lock);
1647e10177abSSatish Ashok 	hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
1648681590bdSNikolay Aleksandrov 		br_multicast_find_del_pg(br, pg);
1649e12cec65SNikolay Aleksandrov 	hlist_move_list(&br->mcast_gc_list, &deleted_head);
1650e10177abSSatish Ashok 	spin_unlock_bh(&br->multicast_lock);
1651e12cec65SNikolay Aleksandrov 	br_multicast_gc(&deleted_head);
1652ce6f7097SLinus Lüssing 	del_timer_sync(&port->ip4_mc_router_timer);
16531080ab95SNikolay Aleksandrov 	free_percpu(port->mcast_stats);
1654eb1d1641SHerbert Xu }
1655eb1d1641SHerbert Xu 
165690010b36SLinus Lüssing static void br_multicast_enable(struct bridge_mcast_own_query *query)
1657561f1103SHerbert Xu {
1658cc0fdd80SLinus Lüssing 	query->startup_sent = 0;
1659561f1103SHerbert Xu 
1660cc0fdd80SLinus Lüssing 	if (try_to_del_timer_sync(&query->timer) >= 0 ||
1661cc0fdd80SLinus Lüssing 	    del_timer(&query->timer))
1662cc0fdd80SLinus Lüssing 		mod_timer(&query->timer, jiffies);
1663561f1103SHerbert Xu }
1664561f1103SHerbert Xu 
16657cb3f921SNikolay Aleksandrov static void __br_multicast_enable_port(struct net_bridge_port *port)
1666eb1d1641SHerbert Xu {
1667eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
1668eb1d1641SHerbert Xu 
166913cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED) || !netif_running(br->dev))
16707cb3f921SNikolay Aleksandrov 		return;
1671eb1d1641SHerbert Xu 
167290010b36SLinus Lüssing 	br_multicast_enable(&port->ip4_own_query);
1673cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
167490010b36SLinus Lüssing 	br_multicast_enable(&port->ip6_own_query);
1675cc0fdd80SLinus Lüssing #endif
16767f0aec7aSNikolay Aleksandrov 	if (port->multicast_router == MDB_RTR_TYPE_PERM &&
1677ce6f7097SLinus Lüssing 	    hlist_unhashed(&port->ip4_rlist))
1678754bc547SSatish Ashok 		br_multicast_add_router(br, port);
16797cb3f921SNikolay Aleksandrov }
1680eb1d1641SHerbert Xu 
16817cb3f921SNikolay Aleksandrov void br_multicast_enable_port(struct net_bridge_port *port)
16827cb3f921SNikolay Aleksandrov {
16837cb3f921SNikolay Aleksandrov 	struct net_bridge *br = port->br;
16847cb3f921SNikolay Aleksandrov 
16857cb3f921SNikolay Aleksandrov 	spin_lock(&br->multicast_lock);
16867cb3f921SNikolay Aleksandrov 	__br_multicast_enable_port(port);
1687eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1688eb1d1641SHerbert Xu }
1689eb1d1641SHerbert Xu 
1690eb1d1641SHerbert Xu void br_multicast_disable_port(struct net_bridge_port *port)
1691eb1d1641SHerbert Xu {
1692eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
1693eb1d1641SHerbert Xu 	struct net_bridge_port_group *pg;
1694b67bfe0dSSasha Levin 	struct hlist_node *n;
1695eb1d1641SHerbert Xu 
1696eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
1697b67bfe0dSSasha Levin 	hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
16989d06b6d8SElad Raz 		if (!(pg->flags & MDB_PG_FLAGS_PERMANENT))
1699681590bdSNikolay Aleksandrov 			br_multicast_find_del_pg(br, pg);
1700eb1d1641SHerbert Xu 
1701f12e7d95SNogah Frankel 	__del_port_router(port);
1702f12e7d95SNogah Frankel 
1703ce6f7097SLinus Lüssing 	del_timer(&port->ip4_mc_router_timer);
170490010b36SLinus Lüssing 	del_timer(&port->ip4_own_query.timer);
1705cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
170690010b36SLinus Lüssing 	del_timer(&port->ip6_own_query.timer);
1707cc0fdd80SLinus Lüssing #endif
1708eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1709eb1d1641SHerbert Xu }
1710eb1d1641SHerbert Xu 
1711e6231bcaSNikolay Aleksandrov static int __grp_src_delete_marked(struct net_bridge_port_group *pg)
1712e6231bcaSNikolay Aleksandrov {
1713e6231bcaSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
1714e6231bcaSNikolay Aleksandrov 	struct hlist_node *tmp;
1715e6231bcaSNikolay Aleksandrov 	int deleted = 0;
1716e6231bcaSNikolay Aleksandrov 
1717e6231bcaSNikolay Aleksandrov 	hlist_for_each_entry_safe(ent, tmp, &pg->src_list, node)
1718e6231bcaSNikolay Aleksandrov 		if (ent->flags & BR_SGRP_F_DELETE) {
1719d5a10222SNikolay Aleksandrov 			br_multicast_del_group_src(ent, false);
1720e6231bcaSNikolay Aleksandrov 			deleted++;
1721e6231bcaSNikolay Aleksandrov 		}
1722e6231bcaSNikolay Aleksandrov 
1723e6231bcaSNikolay Aleksandrov 	return deleted;
1724e6231bcaSNikolay Aleksandrov }
1725e6231bcaSNikolay Aleksandrov 
1726b0812368SNikolay Aleksandrov static void __grp_src_mod_timer(struct net_bridge_group_src *src,
1727b0812368SNikolay Aleksandrov 				unsigned long expires)
1728b0812368SNikolay Aleksandrov {
1729b0812368SNikolay Aleksandrov 	mod_timer(&src->timer, expires);
1730b0812368SNikolay Aleksandrov 	br_multicast_fwd_src_handle(src);
1731b0812368SNikolay Aleksandrov }
1732b0812368SNikolay Aleksandrov 
17335bf1e00bSNikolay Aleksandrov static void __grp_src_query_marked_and_rexmit(struct net_bridge_port_group *pg)
17345bf1e00bSNikolay Aleksandrov {
17355bf1e00bSNikolay Aleksandrov 	struct bridge_mcast_other_query *other_query = NULL;
1736085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
17375bf1e00bSNikolay Aleksandrov 	u32 lmqc = br->multicast_last_member_count;
17385bf1e00bSNikolay Aleksandrov 	unsigned long lmqt, lmi, now = jiffies;
17395bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
17405bf1e00bSNikolay Aleksandrov 
17415bf1e00bSNikolay Aleksandrov 	if (!netif_running(br->dev) ||
17425bf1e00bSNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_ENABLED))
17435bf1e00bSNikolay Aleksandrov 		return;
17445bf1e00bSNikolay Aleksandrov 
1745085b53c8SNikolay Aleksandrov 	if (pg->key.addr.proto == htons(ETH_P_IP))
17465bf1e00bSNikolay Aleksandrov 		other_query = &br->ip4_other_query;
17475bf1e00bSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
17485bf1e00bSNikolay Aleksandrov 	else
17495bf1e00bSNikolay Aleksandrov 		other_query = &br->ip6_other_query;
17505bf1e00bSNikolay Aleksandrov #endif
17515bf1e00bSNikolay Aleksandrov 
17525bf1e00bSNikolay Aleksandrov 	lmqt = now + br_multicast_lmqt(br);
17535bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node) {
17545bf1e00bSNikolay Aleksandrov 		if (ent->flags & BR_SGRP_F_SEND) {
17555bf1e00bSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_SEND;
17565bf1e00bSNikolay Aleksandrov 			if (ent->timer.expires > lmqt) {
17575bf1e00bSNikolay Aleksandrov 				if (br_opt_get(br, BROPT_MULTICAST_QUERIER) &&
17585bf1e00bSNikolay Aleksandrov 				    other_query &&
17595bf1e00bSNikolay Aleksandrov 				    !timer_pending(&other_query->timer))
17605bf1e00bSNikolay Aleksandrov 					ent->src_query_rexmit_cnt = lmqc;
1761b0812368SNikolay Aleksandrov 				__grp_src_mod_timer(ent, lmqt);
17625bf1e00bSNikolay Aleksandrov 			}
17635bf1e00bSNikolay Aleksandrov 		}
17645bf1e00bSNikolay Aleksandrov 	}
17655bf1e00bSNikolay Aleksandrov 
17665bf1e00bSNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_QUERIER) ||
17675bf1e00bSNikolay Aleksandrov 	    !other_query || timer_pending(&other_query->timer))
17685bf1e00bSNikolay Aleksandrov 		return;
17695bf1e00bSNikolay Aleksandrov 
1770085b53c8SNikolay Aleksandrov 	__br_multicast_send_query(br, pg->key.port, pg, &pg->key.addr,
1771085b53c8SNikolay Aleksandrov 				  &pg->key.addr, true, 1, NULL);
17725bf1e00bSNikolay Aleksandrov 
17735bf1e00bSNikolay Aleksandrov 	lmi = now + br->multicast_last_member_interval;
17745bf1e00bSNikolay Aleksandrov 	if (!timer_pending(&pg->rexmit_timer) ||
17755bf1e00bSNikolay Aleksandrov 	    time_after(pg->rexmit_timer.expires, lmi))
17765bf1e00bSNikolay Aleksandrov 		mod_timer(&pg->rexmit_timer, lmi);
17775bf1e00bSNikolay Aleksandrov }
17785bf1e00bSNikolay Aleksandrov 
17795bf1e00bSNikolay Aleksandrov static void __grp_send_query_and_rexmit(struct net_bridge_port_group *pg)
17805bf1e00bSNikolay Aleksandrov {
17815bf1e00bSNikolay Aleksandrov 	struct bridge_mcast_other_query *other_query = NULL;
1782085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
17835bf1e00bSNikolay Aleksandrov 	unsigned long now = jiffies, lmi;
17845bf1e00bSNikolay Aleksandrov 
17855bf1e00bSNikolay Aleksandrov 	if (!netif_running(br->dev) ||
17865bf1e00bSNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_ENABLED))
17875bf1e00bSNikolay Aleksandrov 		return;
17885bf1e00bSNikolay Aleksandrov 
1789085b53c8SNikolay Aleksandrov 	if (pg->key.addr.proto == htons(ETH_P_IP))
17905bf1e00bSNikolay Aleksandrov 		other_query = &br->ip4_other_query;
17915bf1e00bSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
17925bf1e00bSNikolay Aleksandrov 	else
17935bf1e00bSNikolay Aleksandrov 		other_query = &br->ip6_other_query;
17945bf1e00bSNikolay Aleksandrov #endif
17955bf1e00bSNikolay Aleksandrov 
17965bf1e00bSNikolay Aleksandrov 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER) &&
17975bf1e00bSNikolay Aleksandrov 	    other_query && !timer_pending(&other_query->timer)) {
17985bf1e00bSNikolay Aleksandrov 		lmi = now + br->multicast_last_member_interval;
17995bf1e00bSNikolay Aleksandrov 		pg->grp_query_rexmit_cnt = br->multicast_last_member_count - 1;
1800085b53c8SNikolay Aleksandrov 		__br_multicast_send_query(br, pg->key.port, pg, &pg->key.addr,
1801085b53c8SNikolay Aleksandrov 					  &pg->key.addr, false, 0, NULL);
18025bf1e00bSNikolay Aleksandrov 		if (!timer_pending(&pg->rexmit_timer) ||
18035bf1e00bSNikolay Aleksandrov 		    time_after(pg->rexmit_timer.expires, lmi))
18045bf1e00bSNikolay Aleksandrov 			mod_timer(&pg->rexmit_timer, lmi);
18055bf1e00bSNikolay Aleksandrov 	}
18065bf1e00bSNikolay Aleksandrov 
18075bf1e00bSNikolay Aleksandrov 	if (pg->filter_mode == MCAST_EXCLUDE &&
18085bf1e00bSNikolay Aleksandrov 	    (!timer_pending(&pg->timer) ||
18095bf1e00bSNikolay Aleksandrov 	     time_after(pg->timer.expires, now + br_multicast_lmqt(br))))
18105bf1e00bSNikolay Aleksandrov 		mod_timer(&pg->timer, now + br_multicast_lmqt(br));
18115bf1e00bSNikolay Aleksandrov }
18125bf1e00bSNikolay Aleksandrov 
18130436862eSNikolay Aleksandrov /* State          Msg type      New state                Actions
18140436862eSNikolay Aleksandrov  * INCLUDE (A)    IS_IN (B)     INCLUDE (A+B)            (B)=GMI
18150436862eSNikolay Aleksandrov  * INCLUDE (A)    ALLOW (B)     INCLUDE (A+B)            (B)=GMI
18160436862eSNikolay Aleksandrov  * EXCLUDE (X,Y)  ALLOW (A)     EXCLUDE (X+A,Y-A)        (A)=GMI
18170436862eSNikolay Aleksandrov  */
181854bea721SNikolay Aleksandrov static bool br_multicast_isinc_allow(struct net_bridge_port_group *pg, void *h_addr,
1819474ddb37SNikolay Aleksandrov 				     void *srcs, u32 nsrcs, size_t addr_size,
1820474ddb37SNikolay Aleksandrov 				     int grec_type)
18210436862eSNikolay Aleksandrov {
1822085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
18230436862eSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
18240436862eSNikolay Aleksandrov 	unsigned long now = jiffies;
18250436862eSNikolay Aleksandrov 	bool changed = false;
18260436862eSNikolay Aleksandrov 	struct br_ip src_ip;
18270436862eSNikolay Aleksandrov 	u32 src_idx;
18280436862eSNikolay Aleksandrov 
18290436862eSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
1830085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
18310436862eSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
1832e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
18330436862eSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
18340436862eSNikolay Aleksandrov 		if (!ent) {
18350436862eSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
18360436862eSNikolay Aleksandrov 			if (ent)
18370436862eSNikolay Aleksandrov 				changed = true;
18380436862eSNikolay Aleksandrov 		}
18390436862eSNikolay Aleksandrov 
18400436862eSNikolay Aleksandrov 		if (ent)
1841b0812368SNikolay Aleksandrov 			__grp_src_mod_timer(ent, now + br_multicast_gmi(br));
18420436862eSNikolay Aleksandrov 	}
18430436862eSNikolay Aleksandrov 
1844474ddb37SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
1845474ddb37SNikolay Aleksandrov 		changed = true;
1846474ddb37SNikolay Aleksandrov 
18470436862eSNikolay Aleksandrov 	return changed;
18480436862eSNikolay Aleksandrov }
18490436862eSNikolay Aleksandrov 
1850e6231bcaSNikolay Aleksandrov /* State          Msg type      New state                Actions
1851e6231bcaSNikolay Aleksandrov  * INCLUDE (A)    IS_EX (B)     EXCLUDE (A*B,B-A)        (B-A)=0
1852e6231bcaSNikolay Aleksandrov  *                                                       Delete (A-B)
1853e6231bcaSNikolay Aleksandrov  *                                                       Group Timer=GMI
1854e6231bcaSNikolay Aleksandrov  */
185554bea721SNikolay Aleksandrov static void __grp_src_isexc_incl(struct net_bridge_port_group *pg, void *h_addr,
1856ddc255d9SNikolay Aleksandrov 				 void *srcs, u32 nsrcs, size_t addr_size,
1857ddc255d9SNikolay Aleksandrov 				 int grec_type)
1858e6231bcaSNikolay Aleksandrov {
1859e6231bcaSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
1860e6231bcaSNikolay Aleksandrov 	struct br_ip src_ip;
1861e6231bcaSNikolay Aleksandrov 	u32 src_idx;
1862e6231bcaSNikolay Aleksandrov 
1863e6231bcaSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
1864e6231bcaSNikolay Aleksandrov 		ent->flags |= BR_SGRP_F_DELETE;
1865e6231bcaSNikolay Aleksandrov 
1866e6231bcaSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
1867085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
1868e6231bcaSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
1869e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
1870e6231bcaSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
1871e6231bcaSNikolay Aleksandrov 		if (ent)
1872e6231bcaSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_DELETE;
1873e6231bcaSNikolay Aleksandrov 		else
1874b0812368SNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
1875b0812368SNikolay Aleksandrov 		if (ent)
1876b0812368SNikolay Aleksandrov 			br_multicast_fwd_src_handle(ent);
1877e6231bcaSNikolay Aleksandrov 	}
1878e6231bcaSNikolay Aleksandrov 
1879ddc255d9SNikolay Aleksandrov 	br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type);
1880ddc255d9SNikolay Aleksandrov 
1881e6231bcaSNikolay Aleksandrov 	__grp_src_delete_marked(pg);
1882e6231bcaSNikolay Aleksandrov }
1883e6231bcaSNikolay Aleksandrov 
1884e6231bcaSNikolay Aleksandrov /* State          Msg type      New state                Actions
1885e6231bcaSNikolay Aleksandrov  * EXCLUDE (X,Y)  IS_EX (A)     EXCLUDE (A-Y,Y*A)        (A-X-Y)=GMI
1886e6231bcaSNikolay Aleksandrov  *                                                       Delete (X-A)
1887e6231bcaSNikolay Aleksandrov  *                                                       Delete (Y-A)
1888e6231bcaSNikolay Aleksandrov  *                                                       Group Timer=GMI
1889e6231bcaSNikolay Aleksandrov  */
189054bea721SNikolay Aleksandrov static bool __grp_src_isexc_excl(struct net_bridge_port_group *pg, void *h_addr,
1891ddc255d9SNikolay Aleksandrov 				 void *srcs, u32 nsrcs, size_t addr_size,
1892ddc255d9SNikolay Aleksandrov 				 int grec_type)
1893e6231bcaSNikolay Aleksandrov {
1894085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
1895e6231bcaSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
1896e6231bcaSNikolay Aleksandrov 	unsigned long now = jiffies;
1897e6231bcaSNikolay Aleksandrov 	bool changed = false;
1898e6231bcaSNikolay Aleksandrov 	struct br_ip src_ip;
1899e6231bcaSNikolay Aleksandrov 	u32 src_idx;
1900e6231bcaSNikolay Aleksandrov 
1901e6231bcaSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
1902e6231bcaSNikolay Aleksandrov 		ent->flags |= BR_SGRP_F_DELETE;
1903e6231bcaSNikolay Aleksandrov 
1904e6231bcaSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
1905085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
1906e6231bcaSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
1907e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
1908e6231bcaSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
1909e6231bcaSNikolay Aleksandrov 		if (ent) {
1910e6231bcaSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_DELETE;
1911e6231bcaSNikolay Aleksandrov 		} else {
1912e6231bcaSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
1913e6231bcaSNikolay Aleksandrov 			if (ent) {
1914b0812368SNikolay Aleksandrov 				__grp_src_mod_timer(ent,
1915e6231bcaSNikolay Aleksandrov 						    now + br_multicast_gmi(br));
1916e6231bcaSNikolay Aleksandrov 				changed = true;
1917e6231bcaSNikolay Aleksandrov 			}
1918e6231bcaSNikolay Aleksandrov 		}
1919e6231bcaSNikolay Aleksandrov 	}
1920e6231bcaSNikolay Aleksandrov 
1921ddc255d9SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
1922ddc255d9SNikolay Aleksandrov 		changed = true;
1923ddc255d9SNikolay Aleksandrov 
1924e6231bcaSNikolay Aleksandrov 	if (__grp_src_delete_marked(pg))
1925e6231bcaSNikolay Aleksandrov 		changed = true;
1926e6231bcaSNikolay Aleksandrov 
1927e6231bcaSNikolay Aleksandrov 	return changed;
1928e6231bcaSNikolay Aleksandrov }
1929e6231bcaSNikolay Aleksandrov 
193054bea721SNikolay Aleksandrov static bool br_multicast_isexc(struct net_bridge_port_group *pg, void *h_addr,
1931ddc255d9SNikolay Aleksandrov 			       void *srcs, u32 nsrcs, size_t addr_size,
1932ddc255d9SNikolay Aleksandrov 			       int grec_type)
1933e6231bcaSNikolay Aleksandrov {
1934085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
1935e6231bcaSNikolay Aleksandrov 	bool changed = false;
1936e6231bcaSNikolay Aleksandrov 
1937e6231bcaSNikolay Aleksandrov 	switch (pg->filter_mode) {
1938e6231bcaSNikolay Aleksandrov 	case MCAST_INCLUDE:
1939ddc255d9SNikolay Aleksandrov 		__grp_src_isexc_incl(pg, h_addr, srcs, nsrcs, addr_size,
1940ddc255d9SNikolay Aleksandrov 				     grec_type);
19418266a049SNikolay Aleksandrov 		br_multicast_star_g_handle_mode(pg, MCAST_EXCLUDE);
1942e6231bcaSNikolay Aleksandrov 		changed = true;
1943e6231bcaSNikolay Aleksandrov 		break;
1944e6231bcaSNikolay Aleksandrov 	case MCAST_EXCLUDE:
1945ddc255d9SNikolay Aleksandrov 		changed = __grp_src_isexc_excl(pg, h_addr, srcs, nsrcs, addr_size,
1946ddc255d9SNikolay Aleksandrov 					       grec_type);
1947e6231bcaSNikolay Aleksandrov 		break;
1948e6231bcaSNikolay Aleksandrov 	}
1949e6231bcaSNikolay Aleksandrov 
1950e6231bcaSNikolay Aleksandrov 	pg->filter_mode = MCAST_EXCLUDE;
1951e6231bcaSNikolay Aleksandrov 	mod_timer(&pg->timer, jiffies + br_multicast_gmi(br));
1952e6231bcaSNikolay Aleksandrov 
1953e6231bcaSNikolay Aleksandrov 	return changed;
1954e6231bcaSNikolay Aleksandrov }
1955e6231bcaSNikolay Aleksandrov 
19565bf1e00bSNikolay Aleksandrov /* State          Msg type      New state                Actions
19575bf1e00bSNikolay Aleksandrov  * INCLUDE (A)    TO_IN (B)     INCLUDE (A+B)            (B)=GMI
19585bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G,A-B)
19595bf1e00bSNikolay Aleksandrov  */
196054bea721SNikolay Aleksandrov static bool __grp_src_toin_incl(struct net_bridge_port_group *pg, void *h_addr,
1961ddc255d9SNikolay Aleksandrov 				void *srcs, u32 nsrcs, size_t addr_size,
1962ddc255d9SNikolay Aleksandrov 				int grec_type)
19635bf1e00bSNikolay Aleksandrov {
1964085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
19655bf1e00bSNikolay Aleksandrov 	u32 src_idx, to_send = pg->src_ents;
19665bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
19675bf1e00bSNikolay Aleksandrov 	unsigned long now = jiffies;
19685bf1e00bSNikolay Aleksandrov 	bool changed = false;
19695bf1e00bSNikolay Aleksandrov 	struct br_ip src_ip;
19705bf1e00bSNikolay Aleksandrov 
19715bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
19725bf1e00bSNikolay Aleksandrov 		ent->flags |= BR_SGRP_F_SEND;
19735bf1e00bSNikolay Aleksandrov 
19745bf1e00bSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
1975085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
19765bf1e00bSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
1977e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
19785bf1e00bSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
19795bf1e00bSNikolay Aleksandrov 		if (ent) {
19805bf1e00bSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_SEND;
19815bf1e00bSNikolay Aleksandrov 			to_send--;
19825bf1e00bSNikolay Aleksandrov 		} else {
19835bf1e00bSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
19845bf1e00bSNikolay Aleksandrov 			if (ent)
19855bf1e00bSNikolay Aleksandrov 				changed = true;
19865bf1e00bSNikolay Aleksandrov 		}
19875bf1e00bSNikolay Aleksandrov 		if (ent)
1988b0812368SNikolay Aleksandrov 			__grp_src_mod_timer(ent, now + br_multicast_gmi(br));
19895bf1e00bSNikolay Aleksandrov 	}
19905bf1e00bSNikolay Aleksandrov 
1991ddc255d9SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
1992ddc255d9SNikolay Aleksandrov 		changed = true;
1993ddc255d9SNikolay Aleksandrov 
19945bf1e00bSNikolay Aleksandrov 	if (to_send)
19955bf1e00bSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
19965bf1e00bSNikolay Aleksandrov 
19975bf1e00bSNikolay Aleksandrov 	return changed;
19985bf1e00bSNikolay Aleksandrov }
19995bf1e00bSNikolay Aleksandrov 
20005bf1e00bSNikolay Aleksandrov /* State          Msg type      New state                Actions
20015bf1e00bSNikolay Aleksandrov  * EXCLUDE (X,Y)  TO_IN (A)     EXCLUDE (X+A,Y-A)        (A)=GMI
20025bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G,X-A)
20035bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G)
20045bf1e00bSNikolay Aleksandrov  */
200554bea721SNikolay Aleksandrov static bool __grp_src_toin_excl(struct net_bridge_port_group *pg, void *h_addr,
2006ddc255d9SNikolay Aleksandrov 				void *srcs, u32 nsrcs, size_t addr_size,
2007ddc255d9SNikolay Aleksandrov 				int grec_type)
20085bf1e00bSNikolay Aleksandrov {
2009085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
20105bf1e00bSNikolay Aleksandrov 	u32 src_idx, to_send = pg->src_ents;
20115bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
20125bf1e00bSNikolay Aleksandrov 	unsigned long now = jiffies;
20135bf1e00bSNikolay Aleksandrov 	bool changed = false;
20145bf1e00bSNikolay Aleksandrov 	struct br_ip src_ip;
20155bf1e00bSNikolay Aleksandrov 
20165bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
20175bf1e00bSNikolay Aleksandrov 		if (timer_pending(&ent->timer))
20185bf1e00bSNikolay Aleksandrov 			ent->flags |= BR_SGRP_F_SEND;
20195bf1e00bSNikolay Aleksandrov 
20205bf1e00bSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2021085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
20225bf1e00bSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2023e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
20245bf1e00bSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
20255bf1e00bSNikolay Aleksandrov 		if (ent) {
20265bf1e00bSNikolay Aleksandrov 			if (timer_pending(&ent->timer)) {
20275bf1e00bSNikolay Aleksandrov 				ent->flags &= ~BR_SGRP_F_SEND;
20285bf1e00bSNikolay Aleksandrov 				to_send--;
20295bf1e00bSNikolay Aleksandrov 			}
20305bf1e00bSNikolay Aleksandrov 		} else {
20315bf1e00bSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
20325bf1e00bSNikolay Aleksandrov 			if (ent)
20335bf1e00bSNikolay Aleksandrov 				changed = true;
20345bf1e00bSNikolay Aleksandrov 		}
20355bf1e00bSNikolay Aleksandrov 		if (ent)
2036b0812368SNikolay Aleksandrov 			__grp_src_mod_timer(ent, now + br_multicast_gmi(br));
20375bf1e00bSNikolay Aleksandrov 	}
20385bf1e00bSNikolay Aleksandrov 
2039ddc255d9SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
2040ddc255d9SNikolay Aleksandrov 		changed = true;
2041ddc255d9SNikolay Aleksandrov 
20425bf1e00bSNikolay Aleksandrov 	if (to_send)
20435bf1e00bSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
20445bf1e00bSNikolay Aleksandrov 
20455bf1e00bSNikolay Aleksandrov 	__grp_send_query_and_rexmit(pg);
20465bf1e00bSNikolay Aleksandrov 
20475bf1e00bSNikolay Aleksandrov 	return changed;
20485bf1e00bSNikolay Aleksandrov }
20495bf1e00bSNikolay Aleksandrov 
205054bea721SNikolay Aleksandrov static bool br_multicast_toin(struct net_bridge_port_group *pg, void *h_addr,
2051ddc255d9SNikolay Aleksandrov 			      void *srcs, u32 nsrcs, size_t addr_size,
2052ddc255d9SNikolay Aleksandrov 			      int grec_type)
20535bf1e00bSNikolay Aleksandrov {
20545bf1e00bSNikolay Aleksandrov 	bool changed = false;
20555bf1e00bSNikolay Aleksandrov 
20565bf1e00bSNikolay Aleksandrov 	switch (pg->filter_mode) {
20575bf1e00bSNikolay Aleksandrov 	case MCAST_INCLUDE:
2058ddc255d9SNikolay Aleksandrov 		changed = __grp_src_toin_incl(pg, h_addr, srcs, nsrcs, addr_size,
2059ddc255d9SNikolay Aleksandrov 					      grec_type);
20605bf1e00bSNikolay Aleksandrov 		break;
20615bf1e00bSNikolay Aleksandrov 	case MCAST_EXCLUDE:
2062ddc255d9SNikolay Aleksandrov 		changed = __grp_src_toin_excl(pg, h_addr, srcs, nsrcs, addr_size,
2063ddc255d9SNikolay Aleksandrov 					      grec_type);
20645bf1e00bSNikolay Aleksandrov 		break;
20655bf1e00bSNikolay Aleksandrov 	}
20665bf1e00bSNikolay Aleksandrov 
2067ddc255d9SNikolay Aleksandrov 	if (br_multicast_eht_should_del_pg(pg)) {
2068d5a10222SNikolay Aleksandrov 		pg->flags |= MDB_PG_FLAGS_FAST_LEAVE;
2069ddc255d9SNikolay Aleksandrov 		br_multicast_find_del_pg(pg->key.port->br, pg);
2070ddc255d9SNikolay Aleksandrov 		/* a notification has already been sent and we shouldn't
2071ddc255d9SNikolay Aleksandrov 		 * access pg after the delete so we have to return false
2072ddc255d9SNikolay Aleksandrov 		 */
2073ddc255d9SNikolay Aleksandrov 		changed = false;
2074ddc255d9SNikolay Aleksandrov 	}
2075ddc255d9SNikolay Aleksandrov 
20765bf1e00bSNikolay Aleksandrov 	return changed;
20775bf1e00bSNikolay Aleksandrov }
20785bf1e00bSNikolay Aleksandrov 
20795bf1e00bSNikolay Aleksandrov /* State          Msg type      New state                Actions
20805bf1e00bSNikolay Aleksandrov  * INCLUDE (A)    TO_EX (B)     EXCLUDE (A*B,B-A)        (B-A)=0
20815bf1e00bSNikolay Aleksandrov  *                                                       Delete (A-B)
20825bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G,A*B)
20835bf1e00bSNikolay Aleksandrov  *                                                       Group Timer=GMI
20845bf1e00bSNikolay Aleksandrov  */
208554bea721SNikolay Aleksandrov static void __grp_src_toex_incl(struct net_bridge_port_group *pg, void *h_addr,
2086ddc255d9SNikolay Aleksandrov 				void *srcs, u32 nsrcs, size_t addr_size,
2087ddc255d9SNikolay Aleksandrov 				int grec_type)
20885bf1e00bSNikolay Aleksandrov {
20895bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
20905bf1e00bSNikolay Aleksandrov 	u32 src_idx, to_send = 0;
20915bf1e00bSNikolay Aleksandrov 	struct br_ip src_ip;
20925bf1e00bSNikolay Aleksandrov 
20935bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
20945bf1e00bSNikolay Aleksandrov 		ent->flags = (ent->flags & ~BR_SGRP_F_SEND) | BR_SGRP_F_DELETE;
20955bf1e00bSNikolay Aleksandrov 
20965bf1e00bSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2097085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
20985bf1e00bSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2099e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
21005bf1e00bSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
21015bf1e00bSNikolay Aleksandrov 		if (ent) {
21025bf1e00bSNikolay Aleksandrov 			ent->flags = (ent->flags & ~BR_SGRP_F_DELETE) |
21035bf1e00bSNikolay Aleksandrov 				     BR_SGRP_F_SEND;
21045bf1e00bSNikolay Aleksandrov 			to_send++;
21055bf1e00bSNikolay Aleksandrov 		} else {
2106b0812368SNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
21075bf1e00bSNikolay Aleksandrov 		}
2108b0812368SNikolay Aleksandrov 		if (ent)
2109b0812368SNikolay Aleksandrov 			br_multicast_fwd_src_handle(ent);
21105bf1e00bSNikolay Aleksandrov 	}
21115bf1e00bSNikolay Aleksandrov 
2112ddc255d9SNikolay Aleksandrov 	br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type);
2113ddc255d9SNikolay Aleksandrov 
21145bf1e00bSNikolay Aleksandrov 	__grp_src_delete_marked(pg);
21155bf1e00bSNikolay Aleksandrov 	if (to_send)
21165bf1e00bSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
21175bf1e00bSNikolay Aleksandrov }
21185bf1e00bSNikolay Aleksandrov 
21195bf1e00bSNikolay Aleksandrov /* State          Msg type      New state                Actions
21205bf1e00bSNikolay Aleksandrov  * EXCLUDE (X,Y)  TO_EX (A)     EXCLUDE (A-Y,Y*A)        (A-X-Y)=Group Timer
21215bf1e00bSNikolay Aleksandrov  *                                                       Delete (X-A)
21225bf1e00bSNikolay Aleksandrov  *                                                       Delete (Y-A)
21235bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G,A-Y)
21245bf1e00bSNikolay Aleksandrov  *                                                       Group Timer=GMI
21255bf1e00bSNikolay Aleksandrov  */
212654bea721SNikolay Aleksandrov static bool __grp_src_toex_excl(struct net_bridge_port_group *pg, void *h_addr,
2127ddc255d9SNikolay Aleksandrov 				void *srcs, u32 nsrcs, size_t addr_size,
2128ddc255d9SNikolay Aleksandrov 				int grec_type)
21295bf1e00bSNikolay Aleksandrov {
21305bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
21315bf1e00bSNikolay Aleksandrov 	u32 src_idx, to_send = 0;
21325bf1e00bSNikolay Aleksandrov 	bool changed = false;
21335bf1e00bSNikolay Aleksandrov 	struct br_ip src_ip;
21345bf1e00bSNikolay Aleksandrov 
21355bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
21365bf1e00bSNikolay Aleksandrov 		ent->flags = (ent->flags & ~BR_SGRP_F_SEND) | BR_SGRP_F_DELETE;
21375bf1e00bSNikolay Aleksandrov 
21385bf1e00bSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2139085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
21405bf1e00bSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2141e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
21425bf1e00bSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
21435bf1e00bSNikolay Aleksandrov 		if (ent) {
21445bf1e00bSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_DELETE;
21455bf1e00bSNikolay Aleksandrov 		} else {
21465bf1e00bSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
21475bf1e00bSNikolay Aleksandrov 			if (ent) {
2148b0812368SNikolay Aleksandrov 				__grp_src_mod_timer(ent, pg->timer.expires);
21495bf1e00bSNikolay Aleksandrov 				changed = true;
21505bf1e00bSNikolay Aleksandrov 			}
21515bf1e00bSNikolay Aleksandrov 		}
21525bf1e00bSNikolay Aleksandrov 		if (ent && timer_pending(&ent->timer)) {
21535bf1e00bSNikolay Aleksandrov 			ent->flags |= BR_SGRP_F_SEND;
21545bf1e00bSNikolay Aleksandrov 			to_send++;
21555bf1e00bSNikolay Aleksandrov 		}
21565bf1e00bSNikolay Aleksandrov 	}
21575bf1e00bSNikolay Aleksandrov 
2158ddc255d9SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
2159ddc255d9SNikolay Aleksandrov 		changed = true;
2160ddc255d9SNikolay Aleksandrov 
21615bf1e00bSNikolay Aleksandrov 	if (__grp_src_delete_marked(pg))
21625bf1e00bSNikolay Aleksandrov 		changed = true;
21635bf1e00bSNikolay Aleksandrov 	if (to_send)
21645bf1e00bSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
21655bf1e00bSNikolay Aleksandrov 
21665bf1e00bSNikolay Aleksandrov 	return changed;
21675bf1e00bSNikolay Aleksandrov }
21685bf1e00bSNikolay Aleksandrov 
216954bea721SNikolay Aleksandrov static bool br_multicast_toex(struct net_bridge_port_group *pg, void *h_addr,
2170ddc255d9SNikolay Aleksandrov 			      void *srcs, u32 nsrcs, size_t addr_size,
2171ddc255d9SNikolay Aleksandrov 			      int grec_type)
21725bf1e00bSNikolay Aleksandrov {
2173085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
21745bf1e00bSNikolay Aleksandrov 	bool changed = false;
21755bf1e00bSNikolay Aleksandrov 
21765bf1e00bSNikolay Aleksandrov 	switch (pg->filter_mode) {
21775bf1e00bSNikolay Aleksandrov 	case MCAST_INCLUDE:
2178ddc255d9SNikolay Aleksandrov 		__grp_src_toex_incl(pg, h_addr, srcs, nsrcs, addr_size,
2179ddc255d9SNikolay Aleksandrov 				    grec_type);
21808266a049SNikolay Aleksandrov 		br_multicast_star_g_handle_mode(pg, MCAST_EXCLUDE);
21815bf1e00bSNikolay Aleksandrov 		changed = true;
21825bf1e00bSNikolay Aleksandrov 		break;
21835bf1e00bSNikolay Aleksandrov 	case MCAST_EXCLUDE:
2184ddc255d9SNikolay Aleksandrov 		changed = __grp_src_toex_excl(pg, h_addr, srcs, nsrcs, addr_size,
2185ddc255d9SNikolay Aleksandrov 					      grec_type);
21865bf1e00bSNikolay Aleksandrov 		break;
21875bf1e00bSNikolay Aleksandrov 	}
21885bf1e00bSNikolay Aleksandrov 
21895bf1e00bSNikolay Aleksandrov 	pg->filter_mode = MCAST_EXCLUDE;
21905bf1e00bSNikolay Aleksandrov 	mod_timer(&pg->timer, jiffies + br_multicast_gmi(br));
21915bf1e00bSNikolay Aleksandrov 
21925bf1e00bSNikolay Aleksandrov 	return changed;
21935bf1e00bSNikolay Aleksandrov }
21945bf1e00bSNikolay Aleksandrov 
2195109865feSNikolay Aleksandrov /* State          Msg type      New state                Actions
2196109865feSNikolay Aleksandrov  * INCLUDE (A)    BLOCK (B)     INCLUDE (A)              Send Q(G,A*B)
2197109865feSNikolay Aleksandrov  */
21980ad57c99SNikolay Aleksandrov static bool __grp_src_block_incl(struct net_bridge_port_group *pg, void *h_addr,
2199474ddb37SNikolay Aleksandrov 				 void *srcs, u32 nsrcs, size_t addr_size, int grec_type)
2200109865feSNikolay Aleksandrov {
2201109865feSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
2202109865feSNikolay Aleksandrov 	u32 src_idx, to_send = 0;
22030ad57c99SNikolay Aleksandrov 	bool changed = false;
2204109865feSNikolay Aleksandrov 	struct br_ip src_ip;
2205109865feSNikolay Aleksandrov 
2206109865feSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
2207109865feSNikolay Aleksandrov 		ent->flags &= ~BR_SGRP_F_SEND;
2208109865feSNikolay Aleksandrov 
2209109865feSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2210085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
2211109865feSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2212e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2213109865feSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
2214109865feSNikolay Aleksandrov 		if (ent) {
2215109865feSNikolay Aleksandrov 			ent->flags |= BR_SGRP_F_SEND;
2216109865feSNikolay Aleksandrov 			to_send++;
2217109865feSNikolay Aleksandrov 		}
2218109865feSNikolay Aleksandrov 	}
2219109865feSNikolay Aleksandrov 
2220474ddb37SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
2221474ddb37SNikolay Aleksandrov 		changed = true;
2222474ddb37SNikolay Aleksandrov 
2223109865feSNikolay Aleksandrov 	if (to_send)
2224109865feSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
2225109865feSNikolay Aleksandrov 
22260ad57c99SNikolay Aleksandrov 	return changed;
2227109865feSNikolay Aleksandrov }
2228109865feSNikolay Aleksandrov 
2229109865feSNikolay Aleksandrov /* State          Msg type      New state                Actions
2230109865feSNikolay Aleksandrov  * EXCLUDE (X,Y)  BLOCK (A)     EXCLUDE (X+(A-Y),Y)      (A-X-Y)=Group Timer
2231109865feSNikolay Aleksandrov  *                                                       Send Q(G,A-Y)
2232109865feSNikolay Aleksandrov  */
223354bea721SNikolay Aleksandrov static bool __grp_src_block_excl(struct net_bridge_port_group *pg, void *h_addr,
2234474ddb37SNikolay Aleksandrov 				 void *srcs, u32 nsrcs, size_t addr_size, int grec_type)
2235109865feSNikolay Aleksandrov {
2236109865feSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
2237109865feSNikolay Aleksandrov 	u32 src_idx, to_send = 0;
2238109865feSNikolay Aleksandrov 	bool changed = false;
2239109865feSNikolay Aleksandrov 	struct br_ip src_ip;
2240109865feSNikolay Aleksandrov 
2241109865feSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
2242109865feSNikolay Aleksandrov 		ent->flags &= ~BR_SGRP_F_SEND;
2243109865feSNikolay Aleksandrov 
2244109865feSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2245085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
2246109865feSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2247e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2248109865feSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
2249109865feSNikolay Aleksandrov 		if (!ent) {
2250109865feSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
2251109865feSNikolay Aleksandrov 			if (ent) {
2252b0812368SNikolay Aleksandrov 				__grp_src_mod_timer(ent, pg->timer.expires);
2253109865feSNikolay Aleksandrov 				changed = true;
2254109865feSNikolay Aleksandrov 			}
2255109865feSNikolay Aleksandrov 		}
2256109865feSNikolay Aleksandrov 		if (ent && timer_pending(&ent->timer)) {
2257109865feSNikolay Aleksandrov 			ent->flags |= BR_SGRP_F_SEND;
2258109865feSNikolay Aleksandrov 			to_send++;
2259109865feSNikolay Aleksandrov 		}
2260109865feSNikolay Aleksandrov 	}
2261109865feSNikolay Aleksandrov 
2262474ddb37SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
2263474ddb37SNikolay Aleksandrov 		changed = true;
2264474ddb37SNikolay Aleksandrov 
2265109865feSNikolay Aleksandrov 	if (to_send)
2266109865feSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
2267109865feSNikolay Aleksandrov 
2268109865feSNikolay Aleksandrov 	return changed;
2269109865feSNikolay Aleksandrov }
2270109865feSNikolay Aleksandrov 
227154bea721SNikolay Aleksandrov static bool br_multicast_block(struct net_bridge_port_group *pg, void *h_addr,
2272474ddb37SNikolay Aleksandrov 			       void *srcs, u32 nsrcs, size_t addr_size, int grec_type)
2273109865feSNikolay Aleksandrov {
2274109865feSNikolay Aleksandrov 	bool changed = false;
2275109865feSNikolay Aleksandrov 
2276109865feSNikolay Aleksandrov 	switch (pg->filter_mode) {
2277109865feSNikolay Aleksandrov 	case MCAST_INCLUDE:
2278474ddb37SNikolay Aleksandrov 		changed = __grp_src_block_incl(pg, h_addr, srcs, nsrcs, addr_size,
2279474ddb37SNikolay Aleksandrov 					       grec_type);
2280109865feSNikolay Aleksandrov 		break;
2281109865feSNikolay Aleksandrov 	case MCAST_EXCLUDE:
2282474ddb37SNikolay Aleksandrov 		changed = __grp_src_block_excl(pg, h_addr, srcs, nsrcs, addr_size,
2283474ddb37SNikolay Aleksandrov 					       grec_type);
2284109865feSNikolay Aleksandrov 		break;
2285109865feSNikolay Aleksandrov 	}
2286109865feSNikolay Aleksandrov 
2287e87e4b5cSNikolay Aleksandrov 	if ((pg->filter_mode == MCAST_INCLUDE && hlist_empty(&pg->src_list)) ||
2288e87e4b5cSNikolay Aleksandrov 	    br_multicast_eht_should_del_pg(pg)) {
2289d5a10222SNikolay Aleksandrov 		if (br_multicast_eht_should_del_pg(pg))
2290d5a10222SNikolay Aleksandrov 			pg->flags |= MDB_PG_FLAGS_FAST_LEAVE;
2291e87e4b5cSNikolay Aleksandrov 		br_multicast_find_del_pg(pg->key.port->br, pg);
2292e87e4b5cSNikolay Aleksandrov 		/* a notification has already been sent and we shouldn't
2293e87e4b5cSNikolay Aleksandrov 		 * access pg after the delete so we have to return false
2294e87e4b5cSNikolay Aleksandrov 		 */
2295e87e4b5cSNikolay Aleksandrov 		changed = false;
2296e87e4b5cSNikolay Aleksandrov 	}
2297e87e4b5cSNikolay Aleksandrov 
2298109865feSNikolay Aleksandrov 	return changed;
2299109865feSNikolay Aleksandrov }
2300109865feSNikolay Aleksandrov 
23010436862eSNikolay Aleksandrov static struct net_bridge_port_group *
23020436862eSNikolay Aleksandrov br_multicast_find_port(struct net_bridge_mdb_entry *mp,
23030436862eSNikolay Aleksandrov 		       struct net_bridge_port *p,
23040436862eSNikolay Aleksandrov 		       const unsigned char *src)
23050436862eSNikolay Aleksandrov {
2306071445c6SNikolay Aleksandrov 	struct net_bridge *br __maybe_unused = mp->br;
23070436862eSNikolay Aleksandrov 	struct net_bridge_port_group *pg;
23080436862eSNikolay Aleksandrov 
23090436862eSNikolay Aleksandrov 	for (pg = mlock_dereference(mp->ports, br);
23100436862eSNikolay Aleksandrov 	     pg;
23110436862eSNikolay Aleksandrov 	     pg = mlock_dereference(pg->next, br))
23120436862eSNikolay Aleksandrov 		if (br_port_group_equal(pg, p, src))
23130436862eSNikolay Aleksandrov 			return pg;
23140436862eSNikolay Aleksandrov 
23150436862eSNikolay Aleksandrov 	return NULL;
23160436862eSNikolay Aleksandrov }
23170436862eSNikolay Aleksandrov 
23188ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
2319eb1d1641SHerbert Xu 					 struct net_bridge_port *port,
232006499098SVlad Yasevich 					 struct sk_buff *skb,
232106499098SVlad Yasevich 					 u16 vid)
2322eb1d1641SHerbert Xu {
23230436862eSNikolay Aleksandrov 	bool igmpv2 = br->multicast_igmp_version == 2;
23240436862eSNikolay Aleksandrov 	struct net_bridge_mdb_entry *mdst;
23250436862eSNikolay Aleksandrov 	struct net_bridge_port_group *pg;
23266db6f0eaSFelix Fietkau 	const unsigned char *src;
2327eb1d1641SHerbert Xu 	struct igmpv3_report *ih;
2328eb1d1641SHerbert Xu 	struct igmpv3_grec *grec;
23290436862eSNikolay Aleksandrov 	int i, len, num, type;
233054bea721SNikolay Aleksandrov 	__be32 group, *h_addr;
23310436862eSNikolay Aleksandrov 	bool changed = false;
23320436862eSNikolay Aleksandrov 	int err = 0;
2333e57f6185SNikolay Aleksandrov 	u16 nsrcs;
2334eb1d1641SHerbert Xu 
2335eb1d1641SHerbert Xu 	ih = igmpv3_report_hdr(skb);
2336eb1d1641SHerbert Xu 	num = ntohs(ih->ngrec);
2337c2d4fbd2SLinus Lüssing 	len = skb_transport_offset(skb) + sizeof(*ih);
2338eb1d1641SHerbert Xu 
2339eb1d1641SHerbert Xu 	for (i = 0; i < num; i++) {
2340eb1d1641SHerbert Xu 		len += sizeof(*grec);
2341ba5ea614SLinus Lüssing 		if (!ip_mc_may_pull(skb, len))
2342eb1d1641SHerbert Xu 			return -EINVAL;
2343eb1d1641SHerbert Xu 
2344fd218cf9SHerbert Xu 		grec = (void *)(skb->data + len - sizeof(*grec));
2345eb1d1641SHerbert Xu 		group = grec->grec_mca;
2346eb1d1641SHerbert Xu 		type = grec->grec_type;
2347e57f6185SNikolay Aleksandrov 		nsrcs = ntohs(grec->grec_nsrcs);
2348eb1d1641SHerbert Xu 
2349e57f6185SNikolay Aleksandrov 		len += nsrcs * 4;
2350ba5ea614SLinus Lüssing 		if (!ip_mc_may_pull(skb, len))
2351eb1d1641SHerbert Xu 			return -EINVAL;
2352eb1d1641SHerbert Xu 
2353eb1d1641SHerbert Xu 		switch (type) {
2354eb1d1641SHerbert Xu 		case IGMPV3_MODE_IS_INCLUDE:
2355eb1d1641SHerbert Xu 		case IGMPV3_MODE_IS_EXCLUDE:
2356eb1d1641SHerbert Xu 		case IGMPV3_CHANGE_TO_INCLUDE:
2357eb1d1641SHerbert Xu 		case IGMPV3_CHANGE_TO_EXCLUDE:
2358eb1d1641SHerbert Xu 		case IGMPV3_ALLOW_NEW_SOURCES:
2359eb1d1641SHerbert Xu 		case IGMPV3_BLOCK_OLD_SOURCES:
2360eb1d1641SHerbert Xu 			break;
2361eb1d1641SHerbert Xu 
2362eb1d1641SHerbert Xu 		default:
2363eb1d1641SHerbert Xu 			continue;
2364eb1d1641SHerbert Xu 		}
2365eb1d1641SHerbert Xu 
23666db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
23670436862eSNikolay Aleksandrov 		if (nsrcs == 0 &&
23680436862eSNikolay Aleksandrov 		    (type == IGMPV3_CHANGE_TO_INCLUDE ||
23690436862eSNikolay Aleksandrov 		     type == IGMPV3_MODE_IS_INCLUDE)) {
23700436862eSNikolay Aleksandrov 			if (!port || igmpv2) {
23716db6f0eaSFelix Fietkau 				br_ip4_multicast_leave_group(br, port, group, vid, src);
23720436862eSNikolay Aleksandrov 				continue;
23730436862eSNikolay Aleksandrov 			}
2374bc8c20acSSatish Ashok 		} else {
23756db6f0eaSFelix Fietkau 			err = br_ip4_multicast_add_group(br, port, group, vid,
23760436862eSNikolay Aleksandrov 							 src, igmpv2);
2377eb1d1641SHerbert Xu 			if (err)
2378eb1d1641SHerbert Xu 				break;
2379eb1d1641SHerbert Xu 		}
23800436862eSNikolay Aleksandrov 
23810436862eSNikolay Aleksandrov 		if (!port || igmpv2)
23820436862eSNikolay Aleksandrov 			continue;
23830436862eSNikolay Aleksandrov 
23840436862eSNikolay Aleksandrov 		spin_lock_bh(&br->multicast_lock);
23850436862eSNikolay Aleksandrov 		mdst = br_mdb_ip4_get(br, group, vid);
23860436862eSNikolay Aleksandrov 		if (!mdst)
23870436862eSNikolay Aleksandrov 			goto unlock_continue;
23880436862eSNikolay Aleksandrov 		pg = br_multicast_find_port(mdst, port, src);
23890436862eSNikolay Aleksandrov 		if (!pg || (pg->flags & MDB_PG_FLAGS_PERMANENT))
23900436862eSNikolay Aleksandrov 			goto unlock_continue;
239154bea721SNikolay Aleksandrov 		/* reload grec and host addr */
23920436862eSNikolay Aleksandrov 		grec = (void *)(skb->data + len - sizeof(*grec) - (nsrcs * 4));
239354bea721SNikolay Aleksandrov 		h_addr = &ip_hdr(skb)->saddr;
23940436862eSNikolay Aleksandrov 		switch (type) {
23950436862eSNikolay Aleksandrov 		case IGMPV3_ALLOW_NEW_SOURCES:
239654bea721SNikolay Aleksandrov 			changed = br_multicast_isinc_allow(pg, h_addr, grec->grec_src,
2397474ddb37SNikolay Aleksandrov 							   nsrcs, sizeof(__be32), type);
23980436862eSNikolay Aleksandrov 			break;
2399e6231bcaSNikolay Aleksandrov 		case IGMPV3_MODE_IS_INCLUDE:
240054bea721SNikolay Aleksandrov 			changed = br_multicast_isinc_allow(pg, h_addr, grec->grec_src,
2401474ddb37SNikolay Aleksandrov 							   nsrcs, sizeof(__be32), type);
2402e6231bcaSNikolay Aleksandrov 			break;
2403e6231bcaSNikolay Aleksandrov 		case IGMPV3_MODE_IS_EXCLUDE:
240454bea721SNikolay Aleksandrov 			changed = br_multicast_isexc(pg, h_addr, grec->grec_src,
2405ddc255d9SNikolay Aleksandrov 						     nsrcs, sizeof(__be32), type);
2406e6231bcaSNikolay Aleksandrov 			break;
24075bf1e00bSNikolay Aleksandrov 		case IGMPV3_CHANGE_TO_INCLUDE:
240854bea721SNikolay Aleksandrov 			changed = br_multicast_toin(pg, h_addr, grec->grec_src,
2409ddc255d9SNikolay Aleksandrov 						    nsrcs, sizeof(__be32), type);
24105bf1e00bSNikolay Aleksandrov 			break;
24115bf1e00bSNikolay Aleksandrov 		case IGMPV3_CHANGE_TO_EXCLUDE:
241254bea721SNikolay Aleksandrov 			changed = br_multicast_toex(pg, h_addr, grec->grec_src,
2413ddc255d9SNikolay Aleksandrov 						    nsrcs, sizeof(__be32), type);
24145bf1e00bSNikolay Aleksandrov 			break;
2415109865feSNikolay Aleksandrov 		case IGMPV3_BLOCK_OLD_SOURCES:
241654bea721SNikolay Aleksandrov 			changed = br_multicast_block(pg, h_addr, grec->grec_src,
2417474ddb37SNikolay Aleksandrov 						     nsrcs, sizeof(__be32), type);
2418109865feSNikolay Aleksandrov 			break;
24190436862eSNikolay Aleksandrov 		}
24200436862eSNikolay Aleksandrov 		if (changed)
24210436862eSNikolay Aleksandrov 			br_mdb_notify(br->dev, mdst, pg, RTM_NEWMDB);
24220436862eSNikolay Aleksandrov unlock_continue:
24230436862eSNikolay Aleksandrov 		spin_unlock_bh(&br->multicast_lock);
2424bc8c20acSSatish Ashok 	}
2425eb1d1641SHerbert Xu 
2426eb1d1641SHerbert Xu 	return err;
2427eb1d1641SHerbert Xu }
2428eb1d1641SHerbert Xu 
2429dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
243008b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_mld2_report(struct net_bridge *br,
243108b202b6SYOSHIFUJI Hideaki 					struct net_bridge_port *port,
243206499098SVlad Yasevich 					struct sk_buff *skb,
243306499098SVlad Yasevich 					u16 vid)
243408b202b6SYOSHIFUJI Hideaki {
24350436862eSNikolay Aleksandrov 	bool mldv1 = br->multicast_mld_version == 1;
24360436862eSNikolay Aleksandrov 	struct net_bridge_mdb_entry *mdst;
24370436862eSNikolay Aleksandrov 	struct net_bridge_port_group *pg;
2438ba5ea614SLinus Lüssing 	unsigned int nsrcs_offset;
24396db6f0eaSFelix Fietkau 	const unsigned char *src;
244008b202b6SYOSHIFUJI Hideaki 	struct icmp6hdr *icmp6h;
244154bea721SNikolay Aleksandrov 	struct in6_addr *h_addr;
244208b202b6SYOSHIFUJI Hideaki 	struct mld2_grec *grec;
2443ba5ea614SLinus Lüssing 	unsigned int grec_len;
24440436862eSNikolay Aleksandrov 	bool changed = false;
24450436862eSNikolay Aleksandrov 	int i, len, num;
244608b202b6SYOSHIFUJI Hideaki 	int err = 0;
244708b202b6SYOSHIFUJI Hideaki 
2448ba5ea614SLinus Lüssing 	if (!ipv6_mc_may_pull(skb, sizeof(*icmp6h)))
244908b202b6SYOSHIFUJI Hideaki 		return -EINVAL;
245008b202b6SYOSHIFUJI Hideaki 
245108b202b6SYOSHIFUJI Hideaki 	icmp6h = icmp6_hdr(skb);
245208b202b6SYOSHIFUJI Hideaki 	num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
2453c2d4fbd2SLinus Lüssing 	len = skb_transport_offset(skb) + sizeof(*icmp6h);
245408b202b6SYOSHIFUJI Hideaki 
245508b202b6SYOSHIFUJI Hideaki 	for (i = 0; i < num; i++) {
2456e57f6185SNikolay Aleksandrov 		__be16 *_nsrcs, __nsrcs;
2457e57f6185SNikolay Aleksandrov 		u16 nsrcs;
245808b202b6SYOSHIFUJI Hideaki 
2459ba5ea614SLinus Lüssing 		nsrcs_offset = len + offsetof(struct mld2_grec, grec_nsrcs);
2460ba5ea614SLinus Lüssing 
2461ba5ea614SLinus Lüssing 		if (skb_transport_offset(skb) + ipv6_transport_len(skb) <
24625fc6266aSLinus Lüssing 		    nsrcs_offset + sizeof(__nsrcs))
2463ba5ea614SLinus Lüssing 			return -EINVAL;
2464ba5ea614SLinus Lüssing 
2465e57f6185SNikolay Aleksandrov 		_nsrcs = skb_header_pointer(skb, nsrcs_offset,
2466e57f6185SNikolay Aleksandrov 					    sizeof(__nsrcs), &__nsrcs);
2467e57f6185SNikolay Aleksandrov 		if (!_nsrcs)
246808b202b6SYOSHIFUJI Hideaki 			return -EINVAL;
246908b202b6SYOSHIFUJI Hideaki 
2470e57f6185SNikolay Aleksandrov 		nsrcs = ntohs(*_nsrcs);
2471e57f6185SNikolay Aleksandrov 		grec_len = struct_size(grec, grec_src, nsrcs);
2472ba5ea614SLinus Lüssing 
2473ba5ea614SLinus Lüssing 		if (!ipv6_mc_may_pull(skb, len + grec_len))
247408b202b6SYOSHIFUJI Hideaki 			return -EINVAL;
247508b202b6SYOSHIFUJI Hideaki 
247608b202b6SYOSHIFUJI Hideaki 		grec = (struct mld2_grec *)(skb->data + len);
2477ba5ea614SLinus Lüssing 		len += grec_len;
247808b202b6SYOSHIFUJI Hideaki 
247908b202b6SYOSHIFUJI Hideaki 		switch (grec->grec_type) {
248008b202b6SYOSHIFUJI Hideaki 		case MLD2_MODE_IS_INCLUDE:
248108b202b6SYOSHIFUJI Hideaki 		case MLD2_MODE_IS_EXCLUDE:
248208b202b6SYOSHIFUJI Hideaki 		case MLD2_CHANGE_TO_INCLUDE:
248308b202b6SYOSHIFUJI Hideaki 		case MLD2_CHANGE_TO_EXCLUDE:
248408b202b6SYOSHIFUJI Hideaki 		case MLD2_ALLOW_NEW_SOURCES:
248508b202b6SYOSHIFUJI Hideaki 		case MLD2_BLOCK_OLD_SOURCES:
248608b202b6SYOSHIFUJI Hideaki 			break;
248708b202b6SYOSHIFUJI Hideaki 
248808b202b6SYOSHIFUJI Hideaki 		default:
248908b202b6SYOSHIFUJI Hideaki 			continue;
249008b202b6SYOSHIFUJI Hideaki 		}
249108b202b6SYOSHIFUJI Hideaki 
24926db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
2493bc8c20acSSatish Ashok 		if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE ||
2494bc8c20acSSatish Ashok 		     grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
2495e57f6185SNikolay Aleksandrov 		    nsrcs == 0) {
24960436862eSNikolay Aleksandrov 			if (!port || mldv1) {
24970436862eSNikolay Aleksandrov 				br_ip6_multicast_leave_group(br, port,
24980436862eSNikolay Aleksandrov 							     &grec->grec_mca,
24996db6f0eaSFelix Fietkau 							     vid, src);
25000436862eSNikolay Aleksandrov 				continue;
25010436862eSNikolay Aleksandrov 			}
2502bc8c20acSSatish Ashok 		} else {
2503bc8c20acSSatish Ashok 			err = br_ip6_multicast_add_group(br, port,
25046db6f0eaSFelix Fietkau 							 &grec->grec_mca, vid,
25050436862eSNikolay Aleksandrov 							 src, mldv1);
25069264251eSDavide Caratti 			if (err)
250708b202b6SYOSHIFUJI Hideaki 				break;
250808b202b6SYOSHIFUJI Hideaki 		}
25090436862eSNikolay Aleksandrov 
25100436862eSNikolay Aleksandrov 		if (!port || mldv1)
25110436862eSNikolay Aleksandrov 			continue;
25120436862eSNikolay Aleksandrov 
25130436862eSNikolay Aleksandrov 		spin_lock_bh(&br->multicast_lock);
25140436862eSNikolay Aleksandrov 		mdst = br_mdb_ip6_get(br, &grec->grec_mca, vid);
25150436862eSNikolay Aleksandrov 		if (!mdst)
25160436862eSNikolay Aleksandrov 			goto unlock_continue;
25170436862eSNikolay Aleksandrov 		pg = br_multicast_find_port(mdst, port, src);
25180436862eSNikolay Aleksandrov 		if (!pg || (pg->flags & MDB_PG_FLAGS_PERMANENT))
25190436862eSNikolay Aleksandrov 			goto unlock_continue;
252054bea721SNikolay Aleksandrov 		h_addr = &ipv6_hdr(skb)->saddr;
25210436862eSNikolay Aleksandrov 		switch (grec->grec_type) {
25220436862eSNikolay Aleksandrov 		case MLD2_ALLOW_NEW_SOURCES:
252354bea721SNikolay Aleksandrov 			changed = br_multicast_isinc_allow(pg, h_addr,
252454bea721SNikolay Aleksandrov 							   grec->grec_src, nsrcs,
2525474ddb37SNikolay Aleksandrov 							   sizeof(struct in6_addr),
2526474ddb37SNikolay Aleksandrov 							   grec->grec_type);
25270436862eSNikolay Aleksandrov 			break;
2528e6231bcaSNikolay Aleksandrov 		case MLD2_MODE_IS_INCLUDE:
252954bea721SNikolay Aleksandrov 			changed = br_multicast_isinc_allow(pg, h_addr,
253054bea721SNikolay Aleksandrov 							   grec->grec_src, nsrcs,
2531474ddb37SNikolay Aleksandrov 							   sizeof(struct in6_addr),
2532474ddb37SNikolay Aleksandrov 							   grec->grec_type);
2533e6231bcaSNikolay Aleksandrov 			break;
2534e6231bcaSNikolay Aleksandrov 		case MLD2_MODE_IS_EXCLUDE:
253554bea721SNikolay Aleksandrov 			changed = br_multicast_isexc(pg, h_addr,
253654bea721SNikolay Aleksandrov 						     grec->grec_src, nsrcs,
2537ddc255d9SNikolay Aleksandrov 						     sizeof(struct in6_addr),
2538ddc255d9SNikolay Aleksandrov 						     grec->grec_type);
2539e6231bcaSNikolay Aleksandrov 			break;
25405bf1e00bSNikolay Aleksandrov 		case MLD2_CHANGE_TO_INCLUDE:
254154bea721SNikolay Aleksandrov 			changed = br_multicast_toin(pg, h_addr,
254254bea721SNikolay Aleksandrov 						    grec->grec_src, nsrcs,
2543ddc255d9SNikolay Aleksandrov 						    sizeof(struct in6_addr),
2544ddc255d9SNikolay Aleksandrov 						    grec->grec_type);
25455bf1e00bSNikolay Aleksandrov 			break;
25465bf1e00bSNikolay Aleksandrov 		case MLD2_CHANGE_TO_EXCLUDE:
254754bea721SNikolay Aleksandrov 			changed = br_multicast_toex(pg, h_addr,
254854bea721SNikolay Aleksandrov 						    grec->grec_src, nsrcs,
2549ddc255d9SNikolay Aleksandrov 						    sizeof(struct in6_addr),
2550ddc255d9SNikolay Aleksandrov 						    grec->grec_type);
25515bf1e00bSNikolay Aleksandrov 			break;
2552109865feSNikolay Aleksandrov 		case MLD2_BLOCK_OLD_SOURCES:
255354bea721SNikolay Aleksandrov 			changed = br_multicast_block(pg, h_addr,
255454bea721SNikolay Aleksandrov 						     grec->grec_src, nsrcs,
2555474ddb37SNikolay Aleksandrov 						     sizeof(struct in6_addr),
2556474ddb37SNikolay Aleksandrov 						     grec->grec_type);
2557109865feSNikolay Aleksandrov 			break;
25580436862eSNikolay Aleksandrov 		}
25590436862eSNikolay Aleksandrov 		if (changed)
25600436862eSNikolay Aleksandrov 			br_mdb_notify(br->dev, mdst, pg, RTM_NEWMDB);
25610436862eSNikolay Aleksandrov unlock_continue:
25620436862eSNikolay Aleksandrov 		spin_unlock_bh(&br->multicast_lock);
2563bc8c20acSSatish Ashok 	}
256408b202b6SYOSHIFUJI Hideaki 
256508b202b6SYOSHIFUJI Hideaki 	return err;
256608b202b6SYOSHIFUJI Hideaki }
256708b202b6SYOSHIFUJI Hideaki #endif
256808b202b6SYOSHIFUJI Hideaki 
2569dc4eb53aSLinus Lüssing static bool br_ip4_multicast_select_querier(struct net_bridge *br,
25702cd41431SLinus Lüssing 					    struct net_bridge_port *port,
2571dc4eb53aSLinus Lüssing 					    __be32 saddr)
2572dc4eb53aSLinus Lüssing {
2573dc4eb53aSLinus Lüssing 	if (!timer_pending(&br->ip4_own_query.timer) &&
2574dc4eb53aSLinus Lüssing 	    !timer_pending(&br->ip4_other_query.timer))
2575dc4eb53aSLinus Lüssing 		goto update;
2576dc4eb53aSLinus Lüssing 
2577deb96566SNikolay Aleksandrov 	if (!br->ip4_querier.addr.src.ip4)
2578dc4eb53aSLinus Lüssing 		goto update;
2579dc4eb53aSLinus Lüssing 
2580deb96566SNikolay Aleksandrov 	if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.src.ip4))
2581dc4eb53aSLinus Lüssing 		goto update;
2582dc4eb53aSLinus Lüssing 
2583dc4eb53aSLinus Lüssing 	return false;
2584dc4eb53aSLinus Lüssing 
2585dc4eb53aSLinus Lüssing update:
2586deb96566SNikolay Aleksandrov 	br->ip4_querier.addr.src.ip4 = saddr;
2587dc4eb53aSLinus Lüssing 
25882cd41431SLinus Lüssing 	/* update protected by general multicast_lock by caller */
25892cd41431SLinus Lüssing 	rcu_assign_pointer(br->ip4_querier.port, port);
25902cd41431SLinus Lüssing 
2591dc4eb53aSLinus Lüssing 	return true;
2592dc4eb53aSLinus Lüssing }
2593dc4eb53aSLinus Lüssing 
2594dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
2595dc4eb53aSLinus Lüssing static bool br_ip6_multicast_select_querier(struct net_bridge *br,
25962cd41431SLinus Lüssing 					    struct net_bridge_port *port,
2597dc4eb53aSLinus Lüssing 					    struct in6_addr *saddr)
2598dc4eb53aSLinus Lüssing {
2599dc4eb53aSLinus Lüssing 	if (!timer_pending(&br->ip6_own_query.timer) &&
2600dc4eb53aSLinus Lüssing 	    !timer_pending(&br->ip6_other_query.timer))
2601dc4eb53aSLinus Lüssing 		goto update;
2602dc4eb53aSLinus Lüssing 
2603deb96566SNikolay Aleksandrov 	if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.src.ip6) <= 0)
2604dc4eb53aSLinus Lüssing 		goto update;
2605dc4eb53aSLinus Lüssing 
2606dc4eb53aSLinus Lüssing 	return false;
2607dc4eb53aSLinus Lüssing 
2608dc4eb53aSLinus Lüssing update:
2609deb96566SNikolay Aleksandrov 	br->ip6_querier.addr.src.ip6 = *saddr;
2610dc4eb53aSLinus Lüssing 
26112cd41431SLinus Lüssing 	/* update protected by general multicast_lock by caller */
26122cd41431SLinus Lüssing 	rcu_assign_pointer(br->ip6_querier.port, port);
26132cd41431SLinus Lüssing 
2614dc4eb53aSLinus Lüssing 	return true;
2615dc4eb53aSLinus Lüssing }
2616dc4eb53aSLinus Lüssing #endif
2617dc4eb53aSLinus Lüssing 
2618cc0fdd80SLinus Lüssing static void
261990010b36SLinus Lüssing br_multicast_update_query_timer(struct net_bridge *br,
262090010b36SLinus Lüssing 				struct bridge_mcast_other_query *query,
2621b00589afSLinus Lüssing 				unsigned long max_delay)
2622b00589afSLinus Lüssing {
262390010b36SLinus Lüssing 	if (!timer_pending(&query->timer))
262490010b36SLinus Lüssing 		query->delay_time = jiffies + max_delay;
2625b00589afSLinus Lüssing 
262690010b36SLinus Lüssing 	mod_timer(&query->timer, jiffies + br->multicast_querier_interval);
2627b00589afSLinus Lüssing }
2628b00589afSLinus Lüssing 
26296d549648SNogah Frankel static void br_port_mc_router_state_change(struct net_bridge_port *p,
26306d549648SNogah Frankel 					   bool is_mc_router)
26316d549648SNogah Frankel {
26326d549648SNogah Frankel 	struct switchdev_attr attr = {
26336d549648SNogah Frankel 		.orig_dev = p->dev,
26346d549648SNogah Frankel 		.id = SWITCHDEV_ATTR_ID_PORT_MROUTER,
26356d549648SNogah Frankel 		.flags = SWITCHDEV_F_DEFER,
26366d549648SNogah Frankel 		.u.mrouter = is_mc_router,
26376d549648SNogah Frankel 	};
26386d549648SNogah Frankel 
2639dcbdf135SVladimir Oltean 	switchdev_port_attr_set(p->dev, &attr, NULL);
26406d549648SNogah Frankel }
26416d549648SNogah Frankel 
26427e80c124Sstephen hemminger /*
26437c77602fSCong Wang  * Add port to router_list
26447e80c124Sstephen hemminger  *  list is maintained ordered by pointer value
26457e80c124Sstephen hemminger  *  and locked by br->multicast_lock and RCU
26467e80c124Sstephen hemminger  */
26470909e117SHerbert Xu static void br_multicast_add_router(struct net_bridge *br,
26480909e117SHerbert Xu 				    struct net_bridge_port *port)
26490909e117SHerbert Xu {
2650dcdca2c4Sstephen hemminger 	struct net_bridge_port *p;
2651b67bfe0dSSasha Levin 	struct hlist_node *slot = NULL;
26520909e117SHerbert Xu 
2653ce6f7097SLinus Lüssing 	if (!hlist_unhashed(&port->ip4_rlist))
26541a040eacSNikolay Aleksandrov 		return;
26551a040eacSNikolay Aleksandrov 
2656ce6f7097SLinus Lüssing 	hlist_for_each_entry(p, &br->ip4_mc_router_list, ip4_rlist) {
26577e80c124Sstephen hemminger 		if ((unsigned long) port >= (unsigned long) p)
26587e80c124Sstephen hemminger 			break;
2659ce6f7097SLinus Lüssing 		slot = &p->ip4_rlist;
2660dcdca2c4Sstephen hemminger 	}
26610909e117SHerbert Xu 
26627e80c124Sstephen hemminger 	if (slot)
2663ce6f7097SLinus Lüssing 		hlist_add_behind_rcu(&port->ip4_rlist, slot);
2664dcdca2c4Sstephen hemminger 	else
2665ce6f7097SLinus Lüssing 		hlist_add_head_rcu(&port->ip4_rlist, &br->ip4_mc_router_list);
2666949f1e39SSatish Ashok 	br_rtr_notify(br->dev, port, RTM_NEWMDB);
26676d549648SNogah Frankel 	br_port_mc_router_state_change(port, true);
26680909e117SHerbert Xu }
26690909e117SHerbert Xu 
2670eb1d1641SHerbert Xu static void br_multicast_mark_router(struct net_bridge *br,
2671eb1d1641SHerbert Xu 				     struct net_bridge_port *port)
2672eb1d1641SHerbert Xu {
2673eb1d1641SHerbert Xu 	unsigned long now = jiffies;
2674eb1d1641SHerbert Xu 
2675eb1d1641SHerbert Xu 	if (!port) {
267677041420SYotam Gigi 		if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) {
2677ce6f7097SLinus Lüssing 			if (!timer_pending(&br->ip4_mc_router_timer))
267877041420SYotam Gigi 				br_mc_router_state_change(br, true);
2679ce6f7097SLinus Lüssing 			mod_timer(&br->ip4_mc_router_timer,
2680eb1d1641SHerbert Xu 				  now + br->multicast_querier_interval);
268177041420SYotam Gigi 		}
2682eb1d1641SHerbert Xu 		return;
2683eb1d1641SHerbert Xu 	}
2684eb1d1641SHerbert Xu 
2685a55d8246SNikolay Aleksandrov 	if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
2686a55d8246SNikolay Aleksandrov 	    port->multicast_router == MDB_RTR_TYPE_PERM)
2687eb1d1641SHerbert Xu 		return;
2688eb1d1641SHerbert Xu 
26890909e117SHerbert Xu 	br_multicast_add_router(br, port);
2690eb1d1641SHerbert Xu 
2691ce6f7097SLinus Lüssing 	mod_timer(&port->ip4_mc_router_timer,
2692eb1d1641SHerbert Xu 		  now + br->multicast_querier_interval);
2693eb1d1641SHerbert Xu }
2694eb1d1641SHerbert Xu 
2695*b19232efSLinus Lüssing static void
2696*b19232efSLinus Lüssing br_ip4_multicast_query_received(struct net_bridge *br,
2697eb1d1641SHerbert Xu 				struct net_bridge_port *port,
269890010b36SLinus Lüssing 				struct bridge_mcast_other_query *query,
2699dc4eb53aSLinus Lüssing 				struct br_ip *saddr,
2700b00589afSLinus Lüssing 				unsigned long max_delay)
2701eb1d1641SHerbert Xu {
2702*b19232efSLinus Lüssing 	if (!br_ip4_multicast_select_querier(br, port, saddr->src.ip4))
2703eb1d1641SHerbert Xu 		return;
2704eb1d1641SHerbert Xu 
2705dc4eb53aSLinus Lüssing 	br_multicast_update_query_timer(br, query, max_delay);
2706eb1d1641SHerbert Xu 	br_multicast_mark_router(br, port);
2707eb1d1641SHerbert Xu }
2708eb1d1641SHerbert Xu 
2709*b19232efSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
2710*b19232efSLinus Lüssing static void
2711*b19232efSLinus Lüssing br_ip6_multicast_query_received(struct net_bridge *br,
2712*b19232efSLinus Lüssing 				struct net_bridge_port *port,
2713*b19232efSLinus Lüssing 				struct bridge_mcast_other_query *query,
2714*b19232efSLinus Lüssing 				struct br_ip *saddr,
2715*b19232efSLinus Lüssing 				unsigned long max_delay)
2716*b19232efSLinus Lüssing {
2717*b19232efSLinus Lüssing 	if (!br_ip6_multicast_select_querier(br, port, &saddr->src.ip6))
2718*b19232efSLinus Lüssing 		return;
2719*b19232efSLinus Lüssing 
2720*b19232efSLinus Lüssing 	br_multicast_update_query_timer(br, query, max_delay);
2721*b19232efSLinus Lüssing 	br_multicast_mark_router(br, port);
2722*b19232efSLinus Lüssing }
2723*b19232efSLinus Lüssing #endif
2724*b19232efSLinus Lüssing 
27259c2e955cSzhong jiang static void br_ip4_multicast_query(struct net_bridge *br,
2726eb1d1641SHerbert Xu 				   struct net_bridge_port *port,
272706499098SVlad Yasevich 				   struct sk_buff *skb,
272806499098SVlad Yasevich 				   u16 vid)
2729eb1d1641SHerbert Xu {
2730ba5ea614SLinus Lüssing 	unsigned int transport_len = ip_transport_len(skb);
2731b71d1d42SEric Dumazet 	const struct iphdr *iph = ip_hdr(skb);
2732eb1d1641SHerbert Xu 	struct igmphdr *ih = igmp_hdr(skb);
2733eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
2734eb1d1641SHerbert Xu 	struct igmpv3_query *ih3;
2735eb1d1641SHerbert Xu 	struct net_bridge_port_group *p;
2736e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
2737dc4eb53aSLinus Lüssing 	struct br_ip saddr;
2738eb1d1641SHerbert Xu 	unsigned long max_delay;
2739eb1d1641SHerbert Xu 	unsigned long now = jiffies;
2740eb1d1641SHerbert Xu 	__be32 group;
2741eb1d1641SHerbert Xu 
2742eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
2743eb1d1641SHerbert Xu 	if (!netif_running(br->dev) ||
2744eb1d1641SHerbert Xu 	    (port && port->state == BR_STATE_DISABLED))
2745eb1d1641SHerbert Xu 		goto out;
2746eb1d1641SHerbert Xu 
2747eb1d1641SHerbert Xu 	group = ih->group;
2748eb1d1641SHerbert Xu 
2749ba5ea614SLinus Lüssing 	if (transport_len == sizeof(*ih)) {
2750eb1d1641SHerbert Xu 		max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
2751eb1d1641SHerbert Xu 
2752eb1d1641SHerbert Xu 		if (!max_delay) {
2753eb1d1641SHerbert Xu 			max_delay = 10 * HZ;
2754eb1d1641SHerbert Xu 			group = 0;
2755eb1d1641SHerbert Xu 		}
2756ba5ea614SLinus Lüssing 	} else if (transport_len >= sizeof(*ih3)) {
2757eb1d1641SHerbert Xu 		ih3 = igmpv3_query_hdr(skb);
275823550b83SNikolay Aleksandrov 		if (ih3->nsrcs ||
275923550b83SNikolay Aleksandrov 		    (br->multicast_igmp_version == 3 && group && ih3->suppress))
2760bec68ff1SYOSHIFUJI Hideaki 			goto out;
2761eb1d1641SHerbert Xu 
27620ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 		max_delay = ih3->code ?
27630ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 			    IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
27649afd85c9SLinus Lüssing 	} else {
27659ed973ccSLinus Lüssing 		goto out;
27669ed973ccSLinus Lüssing 	}
27679ed973ccSLinus Lüssing 
2768dc4eb53aSLinus Lüssing 	if (!group) {
2769dc4eb53aSLinus Lüssing 		saddr.proto = htons(ETH_P_IP);
2770deb96566SNikolay Aleksandrov 		saddr.src.ip4 = iph->saddr;
2771b00589afSLinus Lüssing 
2772*b19232efSLinus Lüssing 		br_ip4_multicast_query_received(br, port, &br->ip4_other_query,
2773dc4eb53aSLinus Lüssing 						&saddr, max_delay);
2774eb1d1641SHerbert Xu 		goto out;
2775dc4eb53aSLinus Lüssing 	}
2776eb1d1641SHerbert Xu 
277719e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip4_get(br, group, vid);
2778eb1d1641SHerbert Xu 	if (!mp)
2779eb1d1641SHerbert Xu 		goto out;
2780eb1d1641SHerbert Xu 
2781eb1d1641SHerbert Xu 	max_delay *= br->multicast_last_member_count;
2782eb1d1641SHerbert Xu 
2783ff0fd34eSAndrew Lunn 	if (mp->host_joined &&
2784eb1d1641SHerbert Xu 	    (timer_pending(&mp->timer) ?
2785eb1d1641SHerbert Xu 	     time_after(mp->timer.expires, now + max_delay) :
2786eb1d1641SHerbert Xu 	     try_to_del_timer_sync(&mp->timer) >= 0))
2787eb1d1641SHerbert Xu 		mod_timer(&mp->timer, now + max_delay);
2788eb1d1641SHerbert Xu 
2789e8051688SEric Dumazet 	for (pp = &mp->ports;
2790e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
2791e8051688SEric Dumazet 	     pp = &p->next) {
2792eb1d1641SHerbert Xu 		if (timer_pending(&p->timer) ?
2793eb1d1641SHerbert Xu 		    time_after(p->timer.expires, now + max_delay) :
279423550b83SNikolay Aleksandrov 		    try_to_del_timer_sync(&p->timer) >= 0 &&
279523550b83SNikolay Aleksandrov 		    (br->multicast_igmp_version == 2 ||
279623550b83SNikolay Aleksandrov 		     p->filter_mode == MCAST_EXCLUDE))
279724f9cdcbSHerbert Xu 			mod_timer(&p->timer, now + max_delay);
2798eb1d1641SHerbert Xu 	}
2799eb1d1641SHerbert Xu 
2800eb1d1641SHerbert Xu out:
2801eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
2802eb1d1641SHerbert Xu }
2803eb1d1641SHerbert Xu 
2804dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
280508b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_query(struct net_bridge *br,
280608b202b6SYOSHIFUJI Hideaki 				  struct net_bridge_port *port,
280706499098SVlad Yasevich 				  struct sk_buff *skb,
280806499098SVlad Yasevich 				  u16 vid)
280908b202b6SYOSHIFUJI Hideaki {
2810ba5ea614SLinus Lüssing 	unsigned int transport_len = ipv6_transport_len(skb);
2811eca2a43bSstephen hemminger 	struct mld_msg *mld;
281208b202b6SYOSHIFUJI Hideaki 	struct net_bridge_mdb_entry *mp;
281308b202b6SYOSHIFUJI Hideaki 	struct mld2_query *mld2q;
2814e8051688SEric Dumazet 	struct net_bridge_port_group *p;
2815e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
2816dc4eb53aSLinus Lüssing 	struct br_ip saddr;
281708b202b6SYOSHIFUJI Hideaki 	unsigned long max_delay;
281808b202b6SYOSHIFUJI Hideaki 	unsigned long now = jiffies;
2819856ce5d0SLinus Lüssing 	unsigned int offset = skb_transport_offset(skb);
2820b71d1d42SEric Dumazet 	const struct in6_addr *group = NULL;
28219ed973ccSLinus Lüssing 	bool is_general_query;
282208b202b6SYOSHIFUJI Hideaki 	int err = 0;
282308b202b6SYOSHIFUJI Hideaki 
282408b202b6SYOSHIFUJI Hideaki 	spin_lock(&br->multicast_lock);
282508b202b6SYOSHIFUJI Hideaki 	if (!netif_running(br->dev) ||
282608b202b6SYOSHIFUJI Hideaki 	    (port && port->state == BR_STATE_DISABLED))
282708b202b6SYOSHIFUJI Hideaki 		goto out;
282808b202b6SYOSHIFUJI Hideaki 
2829ba5ea614SLinus Lüssing 	if (transport_len == sizeof(*mld)) {
2830856ce5d0SLinus Lüssing 		if (!pskb_may_pull(skb, offset + sizeof(*mld))) {
283108b202b6SYOSHIFUJI Hideaki 			err = -EINVAL;
283208b202b6SYOSHIFUJI Hideaki 			goto out;
283308b202b6SYOSHIFUJI Hideaki 		}
283408b202b6SYOSHIFUJI Hideaki 		mld = (struct mld_msg *) icmp6_hdr(skb);
28354715213dSLi RongQing 		max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
283608b202b6SYOSHIFUJI Hideaki 		if (max_delay)
283708b202b6SYOSHIFUJI Hideaki 			group = &mld->mld_mca;
2838248ba8ecSLinus Lüssing 	} else {
2839856ce5d0SLinus Lüssing 		if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) {
284008b202b6SYOSHIFUJI Hideaki 			err = -EINVAL;
284108b202b6SYOSHIFUJI Hideaki 			goto out;
284208b202b6SYOSHIFUJI Hideaki 		}
284308b202b6SYOSHIFUJI Hideaki 		mld2q = (struct mld2_query *)icmp6_hdr(skb);
284408b202b6SYOSHIFUJI Hideaki 		if (!mld2q->mld2q_nsrcs)
284508b202b6SYOSHIFUJI Hideaki 			group = &mld2q->mld2q_mca;
284623550b83SNikolay Aleksandrov 		if (br->multicast_mld_version == 2 &&
284723550b83SNikolay Aleksandrov 		    !ipv6_addr_any(&mld2q->mld2q_mca) &&
284823550b83SNikolay Aleksandrov 		    mld2q->mld2q_suppress)
284923550b83SNikolay Aleksandrov 			goto out;
2850e3f5b170SDaniel Borkmann 
2851e3f5b170SDaniel Borkmann 		max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
285208b202b6SYOSHIFUJI Hideaki 	}
285308b202b6SYOSHIFUJI Hideaki 
28549ed973ccSLinus Lüssing 	is_general_query = group && ipv6_addr_any(group);
28559ed973ccSLinus Lüssing 
2856dc4eb53aSLinus Lüssing 	if (is_general_query) {
2857dc4eb53aSLinus Lüssing 		saddr.proto = htons(ETH_P_IPV6);
2858deb96566SNikolay Aleksandrov 		saddr.src.ip6 = ipv6_hdr(skb)->saddr;
2859b00589afSLinus Lüssing 
2860*b19232efSLinus Lüssing 		br_ip6_multicast_query_received(br, port, &br->ip6_other_query,
2861dc4eb53aSLinus Lüssing 						&saddr, max_delay);
286208b202b6SYOSHIFUJI Hideaki 		goto out;
28636c03ee8bSLinus Lüssing 	} else if (!group) {
28646c03ee8bSLinus Lüssing 		goto out;
2865dc4eb53aSLinus Lüssing 	}
286608b202b6SYOSHIFUJI Hideaki 
286719e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip6_get(br, group, vid);
286808b202b6SYOSHIFUJI Hideaki 	if (!mp)
286908b202b6SYOSHIFUJI Hideaki 		goto out;
287008b202b6SYOSHIFUJI Hideaki 
287108b202b6SYOSHIFUJI Hideaki 	max_delay *= br->multicast_last_member_count;
2872ff0fd34eSAndrew Lunn 	if (mp->host_joined &&
287308b202b6SYOSHIFUJI Hideaki 	    (timer_pending(&mp->timer) ?
287408b202b6SYOSHIFUJI Hideaki 	     time_after(mp->timer.expires, now + max_delay) :
287508b202b6SYOSHIFUJI Hideaki 	     try_to_del_timer_sync(&mp->timer) >= 0))
287608b202b6SYOSHIFUJI Hideaki 		mod_timer(&mp->timer, now + max_delay);
287708b202b6SYOSHIFUJI Hideaki 
2878e8051688SEric Dumazet 	for (pp = &mp->ports;
2879e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
2880e8051688SEric Dumazet 	     pp = &p->next) {
288108b202b6SYOSHIFUJI Hideaki 		if (timer_pending(&p->timer) ?
288208b202b6SYOSHIFUJI Hideaki 		    time_after(p->timer.expires, now + max_delay) :
288323550b83SNikolay Aleksandrov 		    try_to_del_timer_sync(&p->timer) >= 0 &&
288423550b83SNikolay Aleksandrov 		    (br->multicast_mld_version == 1 ||
288523550b83SNikolay Aleksandrov 		     p->filter_mode == MCAST_EXCLUDE))
288624f9cdcbSHerbert Xu 			mod_timer(&p->timer, now + max_delay);
288708b202b6SYOSHIFUJI Hideaki 	}
288808b202b6SYOSHIFUJI Hideaki 
288908b202b6SYOSHIFUJI Hideaki out:
289008b202b6SYOSHIFUJI Hideaki 	spin_unlock(&br->multicast_lock);
289108b202b6SYOSHIFUJI Hideaki 	return err;
289208b202b6SYOSHIFUJI Hideaki }
289308b202b6SYOSHIFUJI Hideaki #endif
289408b202b6SYOSHIFUJI Hideaki 
289590010b36SLinus Lüssing static void
289690010b36SLinus Lüssing br_multicast_leave_group(struct net_bridge *br,
2897eb1d1641SHerbert Xu 			 struct net_bridge_port *port,
2898cc0fdd80SLinus Lüssing 			 struct br_ip *group,
289990010b36SLinus Lüssing 			 struct bridge_mcast_other_query *other_query,
29006db6f0eaSFelix Fietkau 			 struct bridge_mcast_own_query *own_query,
29016db6f0eaSFelix Fietkau 			 const unsigned char *src)
2902eb1d1641SHerbert Xu {
2903eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
2904eb1d1641SHerbert Xu 	struct net_bridge_port_group *p;
2905eb1d1641SHerbert Xu 	unsigned long now;
2906eb1d1641SHerbert Xu 	unsigned long time;
2907eb1d1641SHerbert Xu 
2908eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
2909eb1d1641SHerbert Xu 	if (!netif_running(br->dev) ||
2910544586f7SSatish Ashok 	    (port && port->state == BR_STATE_DISABLED))
2911eb1d1641SHerbert Xu 		goto out;
2912eb1d1641SHerbert Xu 
291319e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip_get(br, group);
2914eb1d1641SHerbert Xu 	if (!mp)
2915eb1d1641SHerbert Xu 		goto out;
2916eb1d1641SHerbert Xu 
2917544586f7SSatish Ashok 	if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
2918544586f7SSatish Ashok 		struct net_bridge_port_group __rcu **pp;
2919544586f7SSatish Ashok 
2920544586f7SSatish Ashok 		for (pp = &mp->ports;
2921544586f7SSatish Ashok 		     (p = mlock_dereference(*pp, br)) != NULL;
2922544586f7SSatish Ashok 		     pp = &p->next) {
29236db6f0eaSFelix Fietkau 			if (!br_port_group_equal(p, port, src))
2924544586f7SSatish Ashok 				continue;
2925544586f7SSatish Ashok 
29265c725b6bSNikolay Aleksandrov 			if (p->flags & MDB_PG_FLAGS_PERMANENT)
29275c725b6bSNikolay Aleksandrov 				break;
29285c725b6bSNikolay Aleksandrov 
2929681590bdSNikolay Aleksandrov 			p->flags |= MDB_PG_FLAGS_FAST_LEAVE;
2930681590bdSNikolay Aleksandrov 			br_multicast_del_pg(mp, p, pp);
2931544586f7SSatish Ashok 		}
2932544586f7SSatish Ashok 		goto out;
2933544586f7SSatish Ashok 	}
2934544586f7SSatish Ashok 
2935544586f7SSatish Ashok 	if (timer_pending(&other_query->timer))
2936544586f7SSatish Ashok 		goto out;
2937544586f7SSatish Ashok 
2938675779adSNikolay Aleksandrov 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER)) {
2939438ef2d0SNikolay Aleksandrov 		__br_multicast_send_query(br, port, NULL, NULL, &mp->addr,
294042c11ccfSNikolay Aleksandrov 					  false, 0, NULL);
29416b7df111SCong Wang 
29426b7df111SCong Wang 		time = jiffies + br->multicast_last_member_count *
29436b7df111SCong Wang 				 br->multicast_last_member_interval;
2944cc0fdd80SLinus Lüssing 
294590010b36SLinus Lüssing 		mod_timer(&own_query->timer, time);
29466b7df111SCong Wang 
29476b7df111SCong Wang 		for (p = mlock_dereference(mp->ports, br);
29486b7df111SCong Wang 		     p != NULL;
29496b7df111SCong Wang 		     p = mlock_dereference(p->next, br)) {
29506db6f0eaSFelix Fietkau 			if (!br_port_group_equal(p, port, src))
29516b7df111SCong Wang 				continue;
29526b7df111SCong Wang 
29536b7df111SCong Wang 			if (!hlist_unhashed(&p->mglist) &&
29546b7df111SCong Wang 			    (timer_pending(&p->timer) ?
29556b7df111SCong Wang 			     time_after(p->timer.expires, time) :
29566b7df111SCong Wang 			     try_to_del_timer_sync(&p->timer) >= 0)) {
29576b7df111SCong Wang 				mod_timer(&p->timer, time);
29586b7df111SCong Wang 			}
29596b7df111SCong Wang 
29606b7df111SCong Wang 			break;
29616b7df111SCong Wang 		}
29626b7df111SCong Wang 	}
29636b7df111SCong Wang 
2964eb1d1641SHerbert Xu 	now = jiffies;
2965eb1d1641SHerbert Xu 	time = now + br->multicast_last_member_count *
2966eb1d1641SHerbert Xu 		     br->multicast_last_member_interval;
2967eb1d1641SHerbert Xu 
2968eb1d1641SHerbert Xu 	if (!port) {
2969ff0fd34eSAndrew Lunn 		if (mp->host_joined &&
2970eb1d1641SHerbert Xu 		    (timer_pending(&mp->timer) ?
2971eb1d1641SHerbert Xu 		     time_after(mp->timer.expires, time) :
2972eb1d1641SHerbert Xu 		     try_to_del_timer_sync(&mp->timer) >= 0)) {
2973eb1d1641SHerbert Xu 			mod_timer(&mp->timer, time);
2974eb1d1641SHerbert Xu 		}
2975454594f3SLinus Lüssing 
2976454594f3SLinus Lüssing 		goto out;
2977454594f3SLinus Lüssing 	}
2978454594f3SLinus Lüssing 
2979454594f3SLinus Lüssing 	for (p = mlock_dereference(mp->ports, br);
2980454594f3SLinus Lüssing 	     p != NULL;
2981454594f3SLinus Lüssing 	     p = mlock_dereference(p->next, br)) {
2982085b53c8SNikolay Aleksandrov 		if (p->key.port != port)
2983454594f3SLinus Lüssing 			continue;
2984454594f3SLinus Lüssing 
2985454594f3SLinus Lüssing 		if (!hlist_unhashed(&p->mglist) &&
2986454594f3SLinus Lüssing 		    (timer_pending(&p->timer) ?
2987454594f3SLinus Lüssing 		     time_after(p->timer.expires, time) :
2988454594f3SLinus Lüssing 		     try_to_del_timer_sync(&p->timer) >= 0)) {
2989454594f3SLinus Lüssing 			mod_timer(&p->timer, time);
2990454594f3SLinus Lüssing 		}
2991454594f3SLinus Lüssing 
2992454594f3SLinus Lüssing 		break;
2993eb1d1641SHerbert Xu 	}
2994eb1d1641SHerbert Xu out:
2995eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
2996eb1d1641SHerbert Xu }
2997eb1d1641SHerbert Xu 
29988ef2a9a5SYOSHIFUJI Hideaki static void br_ip4_multicast_leave_group(struct net_bridge *br,
29998ef2a9a5SYOSHIFUJI Hideaki 					 struct net_bridge_port *port,
3000b0e9a30dSVlad Yasevich 					 __be32 group,
30016db6f0eaSFelix Fietkau 					 __u16 vid,
30026db6f0eaSFelix Fietkau 					 const unsigned char *src)
30038ef2a9a5SYOSHIFUJI Hideaki {
30048ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip br_group;
300590010b36SLinus Lüssing 	struct bridge_mcast_own_query *own_query;
30068ef2a9a5SYOSHIFUJI Hideaki 
30078ef2a9a5SYOSHIFUJI Hideaki 	if (ipv4_is_local_multicast(group))
30088ef2a9a5SYOSHIFUJI Hideaki 		return;
30098ef2a9a5SYOSHIFUJI Hideaki 
301090010b36SLinus Lüssing 	own_query = port ? &port->ip4_own_query : &br->ip4_own_query;
301190010b36SLinus Lüssing 
30121515a63fSNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
3013eab3227bSNikolay Aleksandrov 	br_group.dst.ip4 = group;
30148ef2a9a5SYOSHIFUJI Hideaki 	br_group.proto = htons(ETH_P_IP);
3015b0e9a30dSVlad Yasevich 	br_group.vid = vid;
30168ef2a9a5SYOSHIFUJI Hideaki 
301790010b36SLinus Lüssing 	br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
30186db6f0eaSFelix Fietkau 				 own_query, src);
30198ef2a9a5SYOSHIFUJI Hideaki }
30208ef2a9a5SYOSHIFUJI Hideaki 
3021dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
302208b202b6SYOSHIFUJI Hideaki static void br_ip6_multicast_leave_group(struct net_bridge *br,
302308b202b6SYOSHIFUJI Hideaki 					 struct net_bridge_port *port,
3024b0e9a30dSVlad Yasevich 					 const struct in6_addr *group,
30256db6f0eaSFelix Fietkau 					 __u16 vid,
30266db6f0eaSFelix Fietkau 					 const unsigned char *src)
302708b202b6SYOSHIFUJI Hideaki {
302808b202b6SYOSHIFUJI Hideaki 	struct br_ip br_group;
302990010b36SLinus Lüssing 	struct bridge_mcast_own_query *own_query;
303008b202b6SYOSHIFUJI Hideaki 
30313c3769e6SLinus Lüssing 	if (ipv6_addr_is_ll_all_nodes(group))
303208b202b6SYOSHIFUJI Hideaki 		return;
303308b202b6SYOSHIFUJI Hideaki 
303490010b36SLinus Lüssing 	own_query = port ? &port->ip6_own_query : &br->ip6_own_query;
303590010b36SLinus Lüssing 
30361515a63fSNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
3037eab3227bSNikolay Aleksandrov 	br_group.dst.ip6 = *group;
303808b202b6SYOSHIFUJI Hideaki 	br_group.proto = htons(ETH_P_IPV6);
3039b0e9a30dSVlad Yasevich 	br_group.vid = vid;
304008b202b6SYOSHIFUJI Hideaki 
304190010b36SLinus Lüssing 	br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
30426db6f0eaSFelix Fietkau 				 own_query, src);
304308b202b6SYOSHIFUJI Hideaki }
304408b202b6SYOSHIFUJI Hideaki #endif
30458ef2a9a5SYOSHIFUJI Hideaki 
30461080ab95SNikolay Aleksandrov static void br_multicast_err_count(const struct net_bridge *br,
30471080ab95SNikolay Aleksandrov 				   const struct net_bridge_port *p,
30481080ab95SNikolay Aleksandrov 				   __be16 proto)
30491080ab95SNikolay Aleksandrov {
30501080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
30511080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats *pstats;
30521080ab95SNikolay Aleksandrov 
3053675779adSNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
30541080ab95SNikolay Aleksandrov 		return;
30551080ab95SNikolay Aleksandrov 
30561080ab95SNikolay Aleksandrov 	if (p)
30571080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
30581080ab95SNikolay Aleksandrov 	else
30591080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
30601080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
30611080ab95SNikolay Aleksandrov 		return;
30621080ab95SNikolay Aleksandrov 
30631080ab95SNikolay Aleksandrov 	pstats = this_cpu_ptr(stats);
30641080ab95SNikolay Aleksandrov 
30651080ab95SNikolay Aleksandrov 	u64_stats_update_begin(&pstats->syncp);
30661080ab95SNikolay Aleksandrov 	switch (proto) {
30671080ab95SNikolay Aleksandrov 	case htons(ETH_P_IP):
30681080ab95SNikolay Aleksandrov 		pstats->mstats.igmp_parse_errors++;
30691080ab95SNikolay Aleksandrov 		break;
30701080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
30711080ab95SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
30721080ab95SNikolay Aleksandrov 		pstats->mstats.mld_parse_errors++;
30731080ab95SNikolay Aleksandrov 		break;
30741080ab95SNikolay Aleksandrov #endif
30751080ab95SNikolay Aleksandrov 	}
30761080ab95SNikolay Aleksandrov 	u64_stats_update_end(&pstats->syncp);
30771080ab95SNikolay Aleksandrov }
30781080ab95SNikolay Aleksandrov 
307991b02d3dSNikolay Aleksandrov static void br_multicast_pim(struct net_bridge *br,
308091b02d3dSNikolay Aleksandrov 			     struct net_bridge_port *port,
308191b02d3dSNikolay Aleksandrov 			     const struct sk_buff *skb)
308291b02d3dSNikolay Aleksandrov {
308391b02d3dSNikolay Aleksandrov 	unsigned int offset = skb_transport_offset(skb);
308491b02d3dSNikolay Aleksandrov 	struct pimhdr *pimhdr, _pimhdr;
308591b02d3dSNikolay Aleksandrov 
308691b02d3dSNikolay Aleksandrov 	pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr);
308791b02d3dSNikolay Aleksandrov 	if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION ||
308891b02d3dSNikolay Aleksandrov 	    pim_hdr_type(pimhdr) != PIM_TYPE_HELLO)
308991b02d3dSNikolay Aleksandrov 		return;
309091b02d3dSNikolay Aleksandrov 
309191b02d3dSNikolay Aleksandrov 	br_multicast_mark_router(br, port);
309291b02d3dSNikolay Aleksandrov }
309391b02d3dSNikolay Aleksandrov 
30944b3087c7SLinus Lüssing static int br_ip4_multicast_mrd_rcv(struct net_bridge *br,
30954b3087c7SLinus Lüssing 				    struct net_bridge_port *port,
30964b3087c7SLinus Lüssing 				    struct sk_buff *skb)
30974b3087c7SLinus Lüssing {
30984b3087c7SLinus Lüssing 	if (ip_hdr(skb)->protocol != IPPROTO_IGMP ||
30994b3087c7SLinus Lüssing 	    igmp_hdr(skb)->type != IGMP_MRDISC_ADV)
31004b3087c7SLinus Lüssing 		return -ENOMSG;
31014b3087c7SLinus Lüssing 
31024b3087c7SLinus Lüssing 	br_multicast_mark_router(br, port);
31034b3087c7SLinus Lüssing 
31044b3087c7SLinus Lüssing 	return 0;
31054b3087c7SLinus Lüssing }
31064b3087c7SLinus Lüssing 
3107eb1d1641SHerbert Xu static int br_multicast_ipv4_rcv(struct net_bridge *br,
3108eb1d1641SHerbert Xu 				 struct net_bridge_port *port,
310906499098SVlad Yasevich 				 struct sk_buff *skb,
311006499098SVlad Yasevich 				 u16 vid)
3111eb1d1641SHerbert Xu {
31126db6f0eaSFelix Fietkau 	const unsigned char *src;
3113eb1d1641SHerbert Xu 	struct igmphdr *ih;
3114eb1d1641SHerbert Xu 	int err;
3115eb1d1641SHerbert Xu 
3116ba5ea614SLinus Lüssing 	err = ip_mc_check_igmp(skb);
3117eb1d1641SHerbert Xu 
31189afd85c9SLinus Lüssing 	if (err == -ENOMSG) {
311991b02d3dSNikolay Aleksandrov 		if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) {
3120bd4265feSHerbert Xu 			BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
312191b02d3dSNikolay Aleksandrov 		} else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) {
312291b02d3dSNikolay Aleksandrov 			if (ip_hdr(skb)->protocol == IPPROTO_PIM)
312391b02d3dSNikolay Aleksandrov 				br_multicast_pim(br, port, skb);
31244b3087c7SLinus Lüssing 		} else if (ipv4_is_all_snoopers(ip_hdr(skb)->daddr)) {
312508e71623SLi RongQing 			br_ip4_multicast_mrd_rcv(br, port, skb);
31264b3087c7SLinus Lüssing 		}
31274b3087c7SLinus Lüssing 
3128eb1d1641SHerbert Xu 		return 0;
31299afd85c9SLinus Lüssing 	} else if (err < 0) {
31301080ab95SNikolay Aleksandrov 		br_multicast_err_count(br, port, skb->protocol);
31319afd85c9SLinus Lüssing 		return err;
3132bd4265feSHerbert Xu 	}
3133eb1d1641SHerbert Xu 
31349afd85c9SLinus Lüssing 	ih = igmp_hdr(skb);
31356db6f0eaSFelix Fietkau 	src = eth_hdr(skb)->h_source;
31361080ab95SNikolay Aleksandrov 	BR_INPUT_SKB_CB(skb)->igmp = ih->type;
3137eb1d1641SHerbert Xu 
3138eb1d1641SHerbert Xu 	switch (ih->type) {
3139eb1d1641SHerbert Xu 	case IGMP_HOST_MEMBERSHIP_REPORT:
3140eb1d1641SHerbert Xu 	case IGMPV2_HOST_MEMBERSHIP_REPORT:
314162b2bcb4SFernando Luis Vázquez Cao 		BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
31428b671779SNikolay Aleksandrov 		err = br_ip4_multicast_add_group(br, port, ih->group, vid, src,
31438b671779SNikolay Aleksandrov 						 true);
3144eb1d1641SHerbert Xu 		break;
3145eb1d1641SHerbert Xu 	case IGMPV3_HOST_MEMBERSHIP_REPORT:
3146ba5ea614SLinus Lüssing 		err = br_ip4_multicast_igmp3_report(br, port, skb, vid);
3147eb1d1641SHerbert Xu 		break;
3148eb1d1641SHerbert Xu 	case IGMP_HOST_MEMBERSHIP_QUERY:
3149ba5ea614SLinus Lüssing 		br_ip4_multicast_query(br, port, skb, vid);
3150eb1d1641SHerbert Xu 		break;
3151eb1d1641SHerbert Xu 	case IGMP_HOST_LEAVE_MESSAGE:
31526db6f0eaSFelix Fietkau 		br_ip4_multicast_leave_group(br, port, ih->group, vid, src);
3153eb1d1641SHerbert Xu 		break;
3154eb1d1641SHerbert Xu 	}
3155eb1d1641SHerbert Xu 
3156a65056ecSNikolay Aleksandrov 	br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
31571080ab95SNikolay Aleksandrov 			   BR_MCAST_DIR_RX);
31581080ab95SNikolay Aleksandrov 
3159eb1d1641SHerbert Xu 	return err;
3160eb1d1641SHerbert Xu }
3161eb1d1641SHerbert Xu 
3162dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
316399014088SLinus Lüssing static void br_ip6_multicast_mrd_rcv(struct net_bridge *br,
31644b3087c7SLinus Lüssing 				     struct net_bridge_port *port,
31654b3087c7SLinus Lüssing 				     struct sk_buff *skb)
31664b3087c7SLinus Lüssing {
31674b3087c7SLinus Lüssing 	if (icmp6_hdr(skb)->icmp6_type != ICMPV6_MRDISC_ADV)
316899014088SLinus Lüssing 		return;
31694b3087c7SLinus Lüssing 
31704b3087c7SLinus Lüssing 	br_multicast_mark_router(br, port);
31714b3087c7SLinus Lüssing }
31724b3087c7SLinus Lüssing 
317308b202b6SYOSHIFUJI Hideaki static int br_multicast_ipv6_rcv(struct net_bridge *br,
317408b202b6SYOSHIFUJI Hideaki 				 struct net_bridge_port *port,
317506499098SVlad Yasevich 				 struct sk_buff *skb,
317606499098SVlad Yasevich 				 u16 vid)
317708b202b6SYOSHIFUJI Hideaki {
31786db6f0eaSFelix Fietkau 	const unsigned char *src;
31799afd85c9SLinus Lüssing 	struct mld_msg *mld;
318008b202b6SYOSHIFUJI Hideaki 	int err;
318108b202b6SYOSHIFUJI Hideaki 
3182ba5ea614SLinus Lüssing 	err = ipv6_mc_check_mld(skb);
318308b202b6SYOSHIFUJI Hideaki 
318499014088SLinus Lüssing 	if (err == -ENOMSG || err == -ENODATA) {
31859afd85c9SLinus Lüssing 		if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
31868fad9c39SLinus Lüssing 			BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
318799014088SLinus Lüssing 		if (err == -ENODATA &&
318899014088SLinus Lüssing 		    ipv6_addr_is_all_snoopers(&ipv6_hdr(skb)->daddr))
318999014088SLinus Lüssing 			br_ip6_multicast_mrd_rcv(br, port, skb);
31904b3087c7SLinus Lüssing 
319108b202b6SYOSHIFUJI Hideaki 		return 0;
31929afd85c9SLinus Lüssing 	} else if (err < 0) {
31931080ab95SNikolay Aleksandrov 		br_multicast_err_count(br, port, skb->protocol);
31949afd85c9SLinus Lüssing 		return err;
319508b202b6SYOSHIFUJI Hideaki 	}
319608b202b6SYOSHIFUJI Hideaki 
31979afd85c9SLinus Lüssing 	mld = (struct mld_msg *)skb_transport_header(skb);
31981080ab95SNikolay Aleksandrov 	BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type;
319908b202b6SYOSHIFUJI Hideaki 
32009afd85c9SLinus Lüssing 	switch (mld->mld_type) {
320108b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_REPORT:
32026db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
3203fc2af6c7SFernando Luis Vázquez Cao 		BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
32046db6f0eaSFelix Fietkau 		err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid,
32058b671779SNikolay Aleksandrov 						 src, true);
320608b202b6SYOSHIFUJI Hideaki 		break;
320708b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MLD2_REPORT:
3208ba5ea614SLinus Lüssing 		err = br_ip6_multicast_mld2_report(br, port, skb, vid);
320908b202b6SYOSHIFUJI Hideaki 		break;
321008b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_QUERY:
3211ba5ea614SLinus Lüssing 		err = br_ip6_multicast_query(br, port, skb, vid);
321208b202b6SYOSHIFUJI Hideaki 		break;
321308b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_REDUCTION:
32146db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
32156db6f0eaSFelix Fietkau 		br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src);
32169afd85c9SLinus Lüssing 		break;
321708b202b6SYOSHIFUJI Hideaki 	}
321808b202b6SYOSHIFUJI Hideaki 
3219a65056ecSNikolay Aleksandrov 	br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
32201080ab95SNikolay Aleksandrov 			   BR_MCAST_DIR_RX);
32211080ab95SNikolay Aleksandrov 
322208b202b6SYOSHIFUJI Hideaki 	return err;
322308b202b6SYOSHIFUJI Hideaki }
322408b202b6SYOSHIFUJI Hideaki #endif
322508b202b6SYOSHIFUJI Hideaki 
3226eb1d1641SHerbert Xu int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
322706499098SVlad Yasevich 		     struct sk_buff *skb, u16 vid)
3228eb1d1641SHerbert Xu {
32291080ab95SNikolay Aleksandrov 	int ret = 0;
32301080ab95SNikolay Aleksandrov 
32311fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 	BR_INPUT_SKB_CB(skb)->igmp = 0;
32321fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 	BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
32331fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 
323413cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
3235eb1d1641SHerbert Xu 		return 0;
3236eb1d1641SHerbert Xu 
3237eb1d1641SHerbert Xu 	switch (skb->protocol) {
3238eb1d1641SHerbert Xu 	case htons(ETH_P_IP):
32391080ab95SNikolay Aleksandrov 		ret = br_multicast_ipv4_rcv(br, port, skb, vid);
32401080ab95SNikolay Aleksandrov 		break;
3241dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
324208b202b6SYOSHIFUJI Hideaki 	case htons(ETH_P_IPV6):
32431080ab95SNikolay Aleksandrov 		ret = br_multicast_ipv6_rcv(br, port, skb, vid);
32441080ab95SNikolay Aleksandrov 		break;
324508b202b6SYOSHIFUJI Hideaki #endif
3246eb1d1641SHerbert Xu 	}
3247eb1d1641SHerbert Xu 
32481080ab95SNikolay Aleksandrov 	return ret;
3249eb1d1641SHerbert Xu }
3250eb1d1641SHerbert Xu 
3251cc0fdd80SLinus Lüssing static void br_multicast_query_expired(struct net_bridge *br,
32522cd41431SLinus Lüssing 				       struct bridge_mcast_own_query *query,
32532cd41431SLinus Lüssing 				       struct bridge_mcast_querier *querier)
3254cc0fdd80SLinus Lüssing {
3255cc0fdd80SLinus Lüssing 	spin_lock(&br->multicast_lock);
3256cc0fdd80SLinus Lüssing 	if (query->startup_sent < br->multicast_startup_query_count)
3257cc0fdd80SLinus Lüssing 		query->startup_sent++;
3258cc0fdd80SLinus Lüssing 
325971d9f614SEric Dumazet 	RCU_INIT_POINTER(querier->port, NULL);
3260cc0fdd80SLinus Lüssing 	br_multicast_send_query(br, NULL, query);
3261cc0fdd80SLinus Lüssing 	spin_unlock(&br->multicast_lock);
3262cc0fdd80SLinus Lüssing }
3263cc0fdd80SLinus Lüssing 
326488c1f37fSAllen Pais static void br_ip4_multicast_query_expired(struct timer_list *t)
3265eb1d1641SHerbert Xu {
326688c1f37fSAllen Pais 	struct net_bridge *br = from_timer(br, t, ip4_own_query.timer);
3267eb1d1641SHerbert Xu 
32682cd41431SLinus Lüssing 	br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier);
3269eb1d1641SHerbert Xu }
3270eb1d1641SHerbert Xu 
3271cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
327288c1f37fSAllen Pais static void br_ip6_multicast_query_expired(struct timer_list *t)
3273cc0fdd80SLinus Lüssing {
327488c1f37fSAllen Pais 	struct net_bridge *br = from_timer(br, t, ip6_own_query.timer);
3275cc0fdd80SLinus Lüssing 
32762cd41431SLinus Lüssing 	br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier);
3277cc0fdd80SLinus Lüssing }
3278cc0fdd80SLinus Lüssing #endif
3279cc0fdd80SLinus Lüssing 
3280e12cec65SNikolay Aleksandrov static void br_multicast_gc_work(struct work_struct *work)
32818b671779SNikolay Aleksandrov {
32828b671779SNikolay Aleksandrov 	struct net_bridge *br = container_of(work, struct net_bridge,
3283e12cec65SNikolay Aleksandrov 					     mcast_gc_work);
32848b671779SNikolay Aleksandrov 	HLIST_HEAD(deleted_head);
32858b671779SNikolay Aleksandrov 
32868b671779SNikolay Aleksandrov 	spin_lock_bh(&br->multicast_lock);
3287e12cec65SNikolay Aleksandrov 	hlist_move_list(&br->mcast_gc_list, &deleted_head);
32888b671779SNikolay Aleksandrov 	spin_unlock_bh(&br->multicast_lock);
32898b671779SNikolay Aleksandrov 
3290e12cec65SNikolay Aleksandrov 	br_multicast_gc(&deleted_head);
32918b671779SNikolay Aleksandrov }
32928b671779SNikolay Aleksandrov 
3293eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br)
3294eb1d1641SHerbert Xu {
3295d08c6bc0SNikolay Aleksandrov 	br->hash_max = BR_MULTICAST_DEFAULT_HASH_MAX;
3296eb1d1641SHerbert Xu 
32977f0aec7aSNikolay Aleksandrov 	br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
3298eb1d1641SHerbert Xu 	br->multicast_last_member_count = 2;
3299eb1d1641SHerbert Xu 	br->multicast_startup_query_count = 2;
3300eb1d1641SHerbert Xu 
3301eb1d1641SHerbert Xu 	br->multicast_last_member_interval = HZ;
3302eb1d1641SHerbert Xu 	br->multicast_query_response_interval = 10 * HZ;
3303eb1d1641SHerbert Xu 	br->multicast_startup_query_interval = 125 * HZ / 4;
3304eb1d1641SHerbert Xu 	br->multicast_query_interval = 125 * HZ;
3305eb1d1641SHerbert Xu 	br->multicast_querier_interval = 255 * HZ;
3306eb1d1641SHerbert Xu 	br->multicast_membership_interval = 260 * HZ;
3307eb1d1641SHerbert Xu 
330890010b36SLinus Lüssing 	br->ip4_other_query.delay_time = 0;
33092cd41431SLinus Lüssing 	br->ip4_querier.port = NULL;
3310aa2ae3e7SNikolay Aleksandrov 	br->multicast_igmp_version = 2;
3311cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3312aa2ae3e7SNikolay Aleksandrov 	br->multicast_mld_version = 1;
331390010b36SLinus Lüssing 	br->ip6_other_query.delay_time = 0;
33142cd41431SLinus Lüssing 	br->ip6_querier.port = NULL;
3315cc0fdd80SLinus Lüssing #endif
33166919622aSIdo Schimmel 	br_opt_toggle(br, BROPT_MULTICAST_ENABLED, true);
3317675779adSNikolay Aleksandrov 	br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true);
3318b00589afSLinus Lüssing 
3319eb1d1641SHerbert Xu 	spin_lock_init(&br->multicast_lock);
3320ce6f7097SLinus Lüssing 	timer_setup(&br->ip4_mc_router_timer,
332188c1f37fSAllen Pais 		    br_multicast_local_router_expired, 0);
332288c1f37fSAllen Pais 	timer_setup(&br->ip4_other_query.timer,
332388c1f37fSAllen Pais 		    br_ip4_multicast_querier_expired, 0);
332488c1f37fSAllen Pais 	timer_setup(&br->ip4_own_query.timer,
332588c1f37fSAllen Pais 		    br_ip4_multicast_query_expired, 0);
3326cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
332788c1f37fSAllen Pais 	timer_setup(&br->ip6_other_query.timer,
332888c1f37fSAllen Pais 		    br_ip6_multicast_querier_expired, 0);
332988c1f37fSAllen Pais 	timer_setup(&br->ip6_own_query.timer,
333088c1f37fSAllen Pais 		    br_ip6_multicast_query_expired, 0);
3331cc0fdd80SLinus Lüssing #endif
333219e3a9c9SNikolay Aleksandrov 	INIT_HLIST_HEAD(&br->mdb_list);
3333e12cec65SNikolay Aleksandrov 	INIT_HLIST_HEAD(&br->mcast_gc_list);
3334e12cec65SNikolay Aleksandrov 	INIT_WORK(&br->mcast_gc_work, br_multicast_gc_work);
3335eb1d1641SHerbert Xu }
3336eb1d1641SHerbert Xu 
33374effd28cSLinus Lüssing static void br_ip4_multicast_join_snoopers(struct net_bridge *br)
33384effd28cSLinus Lüssing {
33394effd28cSLinus Lüssing 	struct in_device *in_dev = in_dev_get(br->dev);
33404effd28cSLinus Lüssing 
33414effd28cSLinus Lüssing 	if (!in_dev)
33424effd28cSLinus Lüssing 		return;
33434effd28cSLinus Lüssing 
33449fb20801SFlorian Fainelli 	__ip_mc_inc_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
33454effd28cSLinus Lüssing 	in_dev_put(in_dev);
33464effd28cSLinus Lüssing }
33474effd28cSLinus Lüssing 
33484effd28cSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
33494effd28cSLinus Lüssing static void br_ip6_multicast_join_snoopers(struct net_bridge *br)
33504effd28cSLinus Lüssing {
33514effd28cSLinus Lüssing 	struct in6_addr addr;
33524effd28cSLinus Lüssing 
33534effd28cSLinus Lüssing 	ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
33544effd28cSLinus Lüssing 	ipv6_dev_mc_inc(br->dev, &addr);
33554effd28cSLinus Lüssing }
33564effd28cSLinus Lüssing #else
33574effd28cSLinus Lüssing static inline void br_ip6_multicast_join_snoopers(struct net_bridge *br)
33584effd28cSLinus Lüssing {
33594effd28cSLinus Lüssing }
33604effd28cSLinus Lüssing #endif
33614effd28cSLinus Lüssing 
3362851d0a73SJoseph Huang void br_multicast_join_snoopers(struct net_bridge *br)
33634effd28cSLinus Lüssing {
33644effd28cSLinus Lüssing 	br_ip4_multicast_join_snoopers(br);
33654effd28cSLinus Lüssing 	br_ip6_multicast_join_snoopers(br);
33664effd28cSLinus Lüssing }
33674effd28cSLinus Lüssing 
33684effd28cSLinus Lüssing static void br_ip4_multicast_leave_snoopers(struct net_bridge *br)
33694effd28cSLinus Lüssing {
33704effd28cSLinus Lüssing 	struct in_device *in_dev = in_dev_get(br->dev);
33714effd28cSLinus Lüssing 
33724effd28cSLinus Lüssing 	if (WARN_ON(!in_dev))
33734effd28cSLinus Lüssing 		return;
33744effd28cSLinus Lüssing 
33759fb20801SFlorian Fainelli 	__ip_mc_dec_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
33764effd28cSLinus Lüssing 	in_dev_put(in_dev);
33774effd28cSLinus Lüssing }
33784effd28cSLinus Lüssing 
33794effd28cSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
33804effd28cSLinus Lüssing static void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
33814effd28cSLinus Lüssing {
33824effd28cSLinus Lüssing 	struct in6_addr addr;
33834effd28cSLinus Lüssing 
33844effd28cSLinus Lüssing 	ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
33854effd28cSLinus Lüssing 	ipv6_dev_mc_dec(br->dev, &addr);
33864effd28cSLinus Lüssing }
33874effd28cSLinus Lüssing #else
33884effd28cSLinus Lüssing static inline void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
33894effd28cSLinus Lüssing {
33904effd28cSLinus Lüssing }
33914effd28cSLinus Lüssing #endif
33924effd28cSLinus Lüssing 
3393851d0a73SJoseph Huang void br_multicast_leave_snoopers(struct net_bridge *br)
33944effd28cSLinus Lüssing {
33954effd28cSLinus Lüssing 	br_ip4_multicast_leave_snoopers(br);
33964effd28cSLinus Lüssing 	br_ip6_multicast_leave_snoopers(br);
33974effd28cSLinus Lüssing }
33984effd28cSLinus Lüssing 
3399cc0fdd80SLinus Lüssing static void __br_multicast_open(struct net_bridge *br,
340090010b36SLinus Lüssing 				struct bridge_mcast_own_query *query)
3401eb1d1641SHerbert Xu {
3402cc0fdd80SLinus Lüssing 	query->startup_sent = 0;
3403eb1d1641SHerbert Xu 
340413cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
3405eb1d1641SHerbert Xu 		return;
3406eb1d1641SHerbert Xu 
3407cc0fdd80SLinus Lüssing 	mod_timer(&query->timer, jiffies);
3408cc0fdd80SLinus Lüssing }
3409cc0fdd80SLinus Lüssing 
3410cc0fdd80SLinus Lüssing void br_multicast_open(struct net_bridge *br)
3411cc0fdd80SLinus Lüssing {
341290010b36SLinus Lüssing 	__br_multicast_open(br, &br->ip4_own_query);
3413cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
341490010b36SLinus Lüssing 	__br_multicast_open(br, &br->ip6_own_query);
3415cc0fdd80SLinus Lüssing #endif
3416eb1d1641SHerbert Xu }
3417eb1d1641SHerbert Xu 
3418eb1d1641SHerbert Xu void br_multicast_stop(struct net_bridge *br)
3419eb1d1641SHerbert Xu {
3420ce6f7097SLinus Lüssing 	del_timer_sync(&br->ip4_mc_router_timer);
342190010b36SLinus Lüssing 	del_timer_sync(&br->ip4_other_query.timer);
342290010b36SLinus Lüssing 	del_timer_sync(&br->ip4_own_query.timer);
3423cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
342490010b36SLinus Lüssing 	del_timer_sync(&br->ip6_other_query.timer);
342590010b36SLinus Lüssing 	del_timer_sync(&br->ip6_own_query.timer);
3426cc0fdd80SLinus Lüssing #endif
3427e10177abSSatish Ashok }
3428e10177abSSatish Ashok 
3429e10177abSSatish Ashok void br_multicast_dev_del(struct net_bridge *br)
3430e10177abSSatish Ashok {
3431e10177abSSatish Ashok 	struct net_bridge_mdb_entry *mp;
34328b671779SNikolay Aleksandrov 	HLIST_HEAD(deleted_head);
343319e3a9c9SNikolay Aleksandrov 	struct hlist_node *tmp;
3434eb1d1641SHerbert Xu 
3435eb1d1641SHerbert Xu 	spin_lock_bh(&br->multicast_lock);
3436e12cec65SNikolay Aleksandrov 	hlist_for_each_entry_safe(mp, tmp, &br->mdb_list, mdb_node)
3437e12cec65SNikolay Aleksandrov 		br_multicast_del_mdb_entry(mp);
3438e12cec65SNikolay Aleksandrov 	hlist_move_list(&br->mcast_gc_list, &deleted_head);
3439eb1d1641SHerbert Xu 	spin_unlock_bh(&br->multicast_lock);
344019e3a9c9SNikolay Aleksandrov 
3441e12cec65SNikolay Aleksandrov 	br_multicast_gc(&deleted_head);
3442e12cec65SNikolay Aleksandrov 	cancel_work_sync(&br->mcast_gc_work);
34438b671779SNikolay Aleksandrov 
34444329596cSNikolay Aleksandrov 	rcu_barrier();
3445eb1d1641SHerbert Xu }
34460909e117SHerbert Xu 
34470909e117SHerbert Xu int br_multicast_set_router(struct net_bridge *br, unsigned long val)
34480909e117SHerbert Xu {
34496ae4ae8eSLinus Lüssing 	int err = -EINVAL;
34500909e117SHerbert Xu 
34510909e117SHerbert Xu 	spin_lock_bh(&br->multicast_lock);
34520909e117SHerbert Xu 
34530909e117SHerbert Xu 	switch (val) {
34547f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_DISABLED:
34557f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_PERM:
345677041420SYotam Gigi 		br_mc_router_state_change(br, val == MDB_RTR_TYPE_PERM);
3457ce6f7097SLinus Lüssing 		del_timer(&br->ip4_mc_router_timer);
345877041420SYotam Gigi 		br->multicast_router = val;
345977041420SYotam Gigi 		err = 0;
346077041420SYotam Gigi 		break;
34617f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP_QUERY:
346277041420SYotam Gigi 		if (br->multicast_router != MDB_RTR_TYPE_TEMP_QUERY)
346377041420SYotam Gigi 			br_mc_router_state_change(br, false);
34640909e117SHerbert Xu 		br->multicast_router = val;
34650909e117SHerbert Xu 		err = 0;
34660909e117SHerbert Xu 		break;
34670909e117SHerbert Xu 	}
34680909e117SHerbert Xu 
34690909e117SHerbert Xu 	spin_unlock_bh(&br->multicast_lock);
34700909e117SHerbert Xu 
34710909e117SHerbert Xu 	return err;
34720909e117SHerbert Xu }
34730909e117SHerbert Xu 
34747f0aec7aSNikolay Aleksandrov static void __del_port_router(struct net_bridge_port *p)
34757f0aec7aSNikolay Aleksandrov {
3476ce6f7097SLinus Lüssing 	if (hlist_unhashed(&p->ip4_rlist))
34777f0aec7aSNikolay Aleksandrov 		return;
3478ce6f7097SLinus Lüssing 	hlist_del_init_rcu(&p->ip4_rlist);
34797f0aec7aSNikolay Aleksandrov 	br_rtr_notify(p->br->dev, p, RTM_DELMDB);
34806d549648SNogah Frankel 	br_port_mc_router_state_change(p, false);
3481f12e7d95SNogah Frankel 
3482f12e7d95SNogah Frankel 	/* don't allow timer refresh */
3483f12e7d95SNogah Frankel 	if (p->multicast_router == MDB_RTR_TYPE_TEMP)
3484f12e7d95SNogah Frankel 		p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
34857f0aec7aSNikolay Aleksandrov }
34867f0aec7aSNikolay Aleksandrov 
34870909e117SHerbert Xu int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
34880909e117SHerbert Xu {
34890909e117SHerbert Xu 	struct net_bridge *br = p->br;
3490a55d8246SNikolay Aleksandrov 	unsigned long now = jiffies;
34916ae4ae8eSLinus Lüssing 	int err = -EINVAL;
34920909e117SHerbert Xu 
34930909e117SHerbert Xu 	spin_lock(&br->multicast_lock);
34944950cfd1SNikolay Aleksandrov 	if (p->multicast_router == val) {
3495a55d8246SNikolay Aleksandrov 		/* Refresh the temp router port timer */
3496a55d8246SNikolay Aleksandrov 		if (p->multicast_router == MDB_RTR_TYPE_TEMP)
3497ce6f7097SLinus Lüssing 			mod_timer(&p->ip4_mc_router_timer,
3498a55d8246SNikolay Aleksandrov 				  now + br->multicast_querier_interval);
34994950cfd1SNikolay Aleksandrov 		err = 0;
35004950cfd1SNikolay Aleksandrov 		goto unlock;
35014950cfd1SNikolay Aleksandrov 	}
35020909e117SHerbert Xu 	switch (val) {
35037f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_DISABLED:
35047f0aec7aSNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_DISABLED;
35057f0aec7aSNikolay Aleksandrov 		__del_port_router(p);
3506ce6f7097SLinus Lüssing 		del_timer(&p->ip4_mc_router_timer);
35070909e117SHerbert Xu 		break;
35087f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP_QUERY:
35097f0aec7aSNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
35107f0aec7aSNikolay Aleksandrov 		__del_port_router(p);
35117f0aec7aSNikolay Aleksandrov 		break;
35127f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_PERM:
35137f0aec7aSNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_PERM;
3514ce6f7097SLinus Lüssing 		del_timer(&p->ip4_mc_router_timer);
35150909e117SHerbert Xu 		br_multicast_add_router(br, p);
35160909e117SHerbert Xu 		break;
3517a55d8246SNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP:
3518a55d8246SNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_TEMP;
3519a55d8246SNikolay Aleksandrov 		br_multicast_mark_router(br, p);
3520a55d8246SNikolay Aleksandrov 		break;
35217f0aec7aSNikolay Aleksandrov 	default:
35227f0aec7aSNikolay Aleksandrov 		goto unlock;
35230909e117SHerbert Xu 	}
35247f0aec7aSNikolay Aleksandrov 	err = 0;
35257f0aec7aSNikolay Aleksandrov unlock:
35260909e117SHerbert Xu 	spin_unlock(&br->multicast_lock);
35270909e117SHerbert Xu 
35280909e117SHerbert Xu 	return err;
35290909e117SHerbert Xu }
3530561f1103SHerbert Xu 
3531cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br,
353290010b36SLinus Lüssing 				       struct bridge_mcast_own_query *query)
3533561f1103SHerbert Xu {
3534561f1103SHerbert Xu 	struct net_bridge_port *port;
353574857216SHerbert Xu 
3536cc0fdd80SLinus Lüssing 	__br_multicast_open(br, query);
353774857216SHerbert Xu 
3538c5b493ceSNikolay Aleksandrov 	rcu_read_lock();
3539c5b493ceSNikolay Aleksandrov 	list_for_each_entry_rcu(port, &br->port_list, list) {
354074857216SHerbert Xu 		if (port->state == BR_STATE_DISABLED ||
354174857216SHerbert Xu 		    port->state == BR_STATE_BLOCKING)
354274857216SHerbert Xu 			continue;
354374857216SHerbert Xu 
354490010b36SLinus Lüssing 		if (query == &br->ip4_own_query)
354590010b36SLinus Lüssing 			br_multicast_enable(&port->ip4_own_query);
3546cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3547cc0fdd80SLinus Lüssing 		else
354890010b36SLinus Lüssing 			br_multicast_enable(&port->ip6_own_query);
3549cc0fdd80SLinus Lüssing #endif
355074857216SHerbert Xu 	}
3551c5b493ceSNikolay Aleksandrov 	rcu_read_unlock();
355274857216SHerbert Xu }
355374857216SHerbert Xu 
3554ae1ea84bSFlorian Fainelli int br_multicast_toggle(struct net_bridge *br, unsigned long val,
3555ae1ea84bSFlorian Fainelli 			struct netlink_ext_ack *extack)
355674857216SHerbert Xu {
35577cb3f921SNikolay Aleksandrov 	struct net_bridge_port *port;
3558851d0a73SJoseph Huang 	bool change_snoopers = false;
3559ae1ea84bSFlorian Fainelli 	int err = 0;
3560561f1103SHerbert Xu 
3561ef5e0d82SAndrey Vagin 	spin_lock_bh(&br->multicast_lock);
356213cefad2SNikolay Aleksandrov 	if (!!br_opt_get(br, BROPT_MULTICAST_ENABLED) == !!val)
3563561f1103SHerbert Xu 		goto unlock;
3564561f1103SHerbert Xu 
3565ae1ea84bSFlorian Fainelli 	err = br_mc_disabled_update(br->dev, val, extack);
3566ae1ea84bSFlorian Fainelli 	if (err == -EOPNOTSUPP)
3567ae1ea84bSFlorian Fainelli 		err = 0;
3568ae1ea84bSFlorian Fainelli 	if (err)
3569ae1ea84bSFlorian Fainelli 		goto unlock;
3570ae1ea84bSFlorian Fainelli 
357113cefad2SNikolay Aleksandrov 	br_opt_toggle(br, BROPT_MULTICAST_ENABLED, !!val);
35724effd28cSLinus Lüssing 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) {
3573851d0a73SJoseph Huang 		change_snoopers = true;
3574561f1103SHerbert Xu 		goto unlock;
35754effd28cSLinus Lüssing 	}
3576561f1103SHerbert Xu 
35773a7fda06SHerbert Xu 	if (!netif_running(br->dev))
35783a7fda06SHerbert Xu 		goto unlock;
35793a7fda06SHerbert Xu 
35807cb3f921SNikolay Aleksandrov 	br_multicast_open(br);
35817cb3f921SNikolay Aleksandrov 	list_for_each_entry(port, &br->port_list, list)
35827cb3f921SNikolay Aleksandrov 		__br_multicast_enable_port(port);
3583561f1103SHerbert Xu 
3584851d0a73SJoseph Huang 	change_snoopers = true;
3585851d0a73SJoseph Huang 
3586561f1103SHerbert Xu unlock:
3587ef5e0d82SAndrey Vagin 	spin_unlock_bh(&br->multicast_lock);
3588561f1103SHerbert Xu 
3589851d0a73SJoseph Huang 	/* br_multicast_join_snoopers has the potential to cause
3590851d0a73SJoseph Huang 	 * an MLD Report/Leave to be delivered to br_multicast_rcv,
3591851d0a73SJoseph Huang 	 * which would in turn call br_multicast_add_group, which would
3592851d0a73SJoseph Huang 	 * attempt to acquire multicast_lock. This function should be
3593851d0a73SJoseph Huang 	 * called after the lock has been released to avoid deadlocks on
3594851d0a73SJoseph Huang 	 * multicast_lock.
3595851d0a73SJoseph Huang 	 *
3596851d0a73SJoseph Huang 	 * br_multicast_leave_snoopers does not have the problem since
3597851d0a73SJoseph Huang 	 * br_multicast_rcv first checks BROPT_MULTICAST_ENABLED, and
3598851d0a73SJoseph Huang 	 * returns without calling br_multicast_ipv4/6_rcv if it's not
3599851d0a73SJoseph Huang 	 * enabled. Moved both functions out just for symmetry.
3600851d0a73SJoseph Huang 	 */
3601851d0a73SJoseph Huang 	if (change_snoopers) {
3602851d0a73SJoseph Huang 		if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
3603851d0a73SJoseph Huang 			br_multicast_join_snoopers(br);
3604851d0a73SJoseph Huang 		else
3605851d0a73SJoseph Huang 			br_multicast_leave_snoopers(br);
3606851d0a73SJoseph Huang 	}
3607851d0a73SJoseph Huang 
3608ae1ea84bSFlorian Fainelli 	return err;
3609561f1103SHerbert Xu }
3610b195167fSHerbert Xu 
36119341b988SIdo Schimmel bool br_multicast_enabled(const struct net_device *dev)
36129341b988SIdo Schimmel {
36139341b988SIdo Schimmel 	struct net_bridge *br = netdev_priv(dev);
36149341b988SIdo Schimmel 
361513cefad2SNikolay Aleksandrov 	return !!br_opt_get(br, BROPT_MULTICAST_ENABLED);
36169341b988SIdo Schimmel }
36179341b988SIdo Schimmel EXPORT_SYMBOL_GPL(br_multicast_enabled);
36189341b988SIdo Schimmel 
36190912bda4SYotam Gigi bool br_multicast_router(const struct net_device *dev)
36200912bda4SYotam Gigi {
36210912bda4SYotam Gigi 	struct net_bridge *br = netdev_priv(dev);
36220912bda4SYotam Gigi 	bool is_router;
36230912bda4SYotam Gigi 
36240912bda4SYotam Gigi 	spin_lock_bh(&br->multicast_lock);
36250912bda4SYotam Gigi 	is_router = br_multicast_is_router(br);
36260912bda4SYotam Gigi 	spin_unlock_bh(&br->multicast_lock);
36270912bda4SYotam Gigi 	return is_router;
36280912bda4SYotam Gigi }
36290912bda4SYotam Gigi EXPORT_SYMBOL_GPL(br_multicast_router);
36300912bda4SYotam Gigi 
3631c5c23260SHerbert Xu int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
3632c5c23260SHerbert Xu {
3633b00589afSLinus Lüssing 	unsigned long max_delay;
3634b00589afSLinus Lüssing 
3635c5c23260SHerbert Xu 	val = !!val;
3636c5c23260SHerbert Xu 
3637c5c23260SHerbert Xu 	spin_lock_bh(&br->multicast_lock);
3638675779adSNikolay Aleksandrov 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER) == val)
3639c5c23260SHerbert Xu 		goto unlock;
3640c5c23260SHerbert Xu 
3641675779adSNikolay Aleksandrov 	br_opt_toggle(br, BROPT_MULTICAST_QUERIER, !!val);
3642b00589afSLinus Lüssing 	if (!val)
3643b00589afSLinus Lüssing 		goto unlock;
3644b00589afSLinus Lüssing 
3645b00589afSLinus Lüssing 	max_delay = br->multicast_query_response_interval;
3646b00589afSLinus Lüssing 
364790010b36SLinus Lüssing 	if (!timer_pending(&br->ip4_other_query.timer))
364890010b36SLinus Lüssing 		br->ip4_other_query.delay_time = jiffies + max_delay;
3649cc0fdd80SLinus Lüssing 
365090010b36SLinus Lüssing 	br_multicast_start_querier(br, &br->ip4_own_query);
3651cc0fdd80SLinus Lüssing 
3652cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
365390010b36SLinus Lüssing 	if (!timer_pending(&br->ip6_other_query.timer))
365490010b36SLinus Lüssing 		br->ip6_other_query.delay_time = jiffies + max_delay;
3655cc0fdd80SLinus Lüssing 
365690010b36SLinus Lüssing 	br_multicast_start_querier(br, &br->ip6_own_query);
3657cc0fdd80SLinus Lüssing #endif
3658c5c23260SHerbert Xu 
3659c5c23260SHerbert Xu unlock:
3660c5c23260SHerbert Xu 	spin_unlock_bh(&br->multicast_lock);
3661c5c23260SHerbert Xu 
3662c5c23260SHerbert Xu 	return 0;
3663c5c23260SHerbert Xu }
3664c5c23260SHerbert Xu 
36655e923585SNikolay Aleksandrov int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val)
36665e923585SNikolay Aleksandrov {
36675e923585SNikolay Aleksandrov 	/* Currently we support only version 2 and 3 */
36685e923585SNikolay Aleksandrov 	switch (val) {
36695e923585SNikolay Aleksandrov 	case 2:
36705e923585SNikolay Aleksandrov 	case 3:
36715e923585SNikolay Aleksandrov 		break;
36725e923585SNikolay Aleksandrov 	default:
36735e923585SNikolay Aleksandrov 		return -EINVAL;
36745e923585SNikolay Aleksandrov 	}
36755e923585SNikolay Aleksandrov 
36765e923585SNikolay Aleksandrov 	spin_lock_bh(&br->multicast_lock);
36775e923585SNikolay Aleksandrov 	br->multicast_igmp_version = val;
36785e923585SNikolay Aleksandrov 	spin_unlock_bh(&br->multicast_lock);
36795e923585SNikolay Aleksandrov 
36805e923585SNikolay Aleksandrov 	return 0;
36815e923585SNikolay Aleksandrov }
36825e923585SNikolay Aleksandrov 
3683aa2ae3e7SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
3684aa2ae3e7SNikolay Aleksandrov int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val)
3685aa2ae3e7SNikolay Aleksandrov {
3686aa2ae3e7SNikolay Aleksandrov 	/* Currently we support version 1 and 2 */
3687aa2ae3e7SNikolay Aleksandrov 	switch (val) {
3688aa2ae3e7SNikolay Aleksandrov 	case 1:
3689aa2ae3e7SNikolay Aleksandrov 	case 2:
3690aa2ae3e7SNikolay Aleksandrov 		break;
3691aa2ae3e7SNikolay Aleksandrov 	default:
3692aa2ae3e7SNikolay Aleksandrov 		return -EINVAL;
3693aa2ae3e7SNikolay Aleksandrov 	}
3694aa2ae3e7SNikolay Aleksandrov 
3695aa2ae3e7SNikolay Aleksandrov 	spin_lock_bh(&br->multicast_lock);
3696aa2ae3e7SNikolay Aleksandrov 	br->multicast_mld_version = val;
3697aa2ae3e7SNikolay Aleksandrov 	spin_unlock_bh(&br->multicast_lock);
3698aa2ae3e7SNikolay Aleksandrov 
3699aa2ae3e7SNikolay Aleksandrov 	return 0;
3700aa2ae3e7SNikolay Aleksandrov }
3701aa2ae3e7SNikolay Aleksandrov #endif
3702aa2ae3e7SNikolay Aleksandrov 
370307f8ac4aSLinus Lüssing /**
370407f8ac4aSLinus Lüssing  * br_multicast_list_adjacent - Returns snooped multicast addresses
370507f8ac4aSLinus Lüssing  * @dev:	The bridge port adjacent to which to retrieve addresses
370607f8ac4aSLinus Lüssing  * @br_ip_list:	The list to store found, snooped multicast IP addresses in
370707f8ac4aSLinus Lüssing  *
370807f8ac4aSLinus Lüssing  * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
370907f8ac4aSLinus Lüssing  * snooping feature on all bridge ports of dev's bridge device, excluding
371007f8ac4aSLinus Lüssing  * the addresses from dev itself.
371107f8ac4aSLinus Lüssing  *
371207f8ac4aSLinus Lüssing  * Returns the number of items added to br_ip_list.
371307f8ac4aSLinus Lüssing  *
371407f8ac4aSLinus Lüssing  * Notes:
371507f8ac4aSLinus Lüssing  * - br_ip_list needs to be initialized by caller
371607f8ac4aSLinus Lüssing  * - br_ip_list might contain duplicates in the end
371707f8ac4aSLinus Lüssing  *   (needs to be taken care of by caller)
371807f8ac4aSLinus Lüssing  * - br_ip_list needs to be freed by caller
371907f8ac4aSLinus Lüssing  */
372007f8ac4aSLinus Lüssing int br_multicast_list_adjacent(struct net_device *dev,
372107f8ac4aSLinus Lüssing 			       struct list_head *br_ip_list)
372207f8ac4aSLinus Lüssing {
372307f8ac4aSLinus Lüssing 	struct net_bridge *br;
372407f8ac4aSLinus Lüssing 	struct net_bridge_port *port;
372507f8ac4aSLinus Lüssing 	struct net_bridge_port_group *group;
372607f8ac4aSLinus Lüssing 	struct br_ip_list *entry;
372707f8ac4aSLinus Lüssing 	int count = 0;
372807f8ac4aSLinus Lüssing 
372907f8ac4aSLinus Lüssing 	rcu_read_lock();
373035f861e3SJulian Wiedmann 	if (!br_ip_list || !netif_is_bridge_port(dev))
373107f8ac4aSLinus Lüssing 		goto unlock;
373207f8ac4aSLinus Lüssing 
373307f8ac4aSLinus Lüssing 	port = br_port_get_rcu(dev);
373407f8ac4aSLinus Lüssing 	if (!port || !port->br)
373507f8ac4aSLinus Lüssing 		goto unlock;
373607f8ac4aSLinus Lüssing 
373707f8ac4aSLinus Lüssing 	br = port->br;
373807f8ac4aSLinus Lüssing 
373907f8ac4aSLinus Lüssing 	list_for_each_entry_rcu(port, &br->port_list, list) {
374007f8ac4aSLinus Lüssing 		if (!port->dev || port->dev == dev)
374107f8ac4aSLinus Lüssing 			continue;
374207f8ac4aSLinus Lüssing 
374307f8ac4aSLinus Lüssing 		hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
374407f8ac4aSLinus Lüssing 			entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
374507f8ac4aSLinus Lüssing 			if (!entry)
374607f8ac4aSLinus Lüssing 				goto unlock;
374707f8ac4aSLinus Lüssing 
3748085b53c8SNikolay Aleksandrov 			entry->addr = group->key.addr;
374907f8ac4aSLinus Lüssing 			list_add(&entry->list, br_ip_list);
375007f8ac4aSLinus Lüssing 			count++;
375107f8ac4aSLinus Lüssing 		}
375207f8ac4aSLinus Lüssing 	}
375307f8ac4aSLinus Lüssing 
375407f8ac4aSLinus Lüssing unlock:
375507f8ac4aSLinus Lüssing 	rcu_read_unlock();
375607f8ac4aSLinus Lüssing 	return count;
375707f8ac4aSLinus Lüssing }
375807f8ac4aSLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
37592cd41431SLinus Lüssing 
37602cd41431SLinus Lüssing /**
3761c34963e2SLinus Lüssing  * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
3762c34963e2SLinus Lüssing  * @dev: The bridge port providing the bridge on which to check for a querier
3763c34963e2SLinus Lüssing  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
3764c34963e2SLinus Lüssing  *
3765c34963e2SLinus Lüssing  * Checks whether the given interface has a bridge on top and if so returns
3766c34963e2SLinus Lüssing  * true if a valid querier exists anywhere on the bridged link layer.
3767c34963e2SLinus Lüssing  * Otherwise returns false.
3768c34963e2SLinus Lüssing  */
3769c34963e2SLinus Lüssing bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
3770c34963e2SLinus Lüssing {
3771c34963e2SLinus Lüssing 	struct net_bridge *br;
3772c34963e2SLinus Lüssing 	struct net_bridge_port *port;
3773c34963e2SLinus Lüssing 	struct ethhdr eth;
3774c34963e2SLinus Lüssing 	bool ret = false;
3775c34963e2SLinus Lüssing 
3776c34963e2SLinus Lüssing 	rcu_read_lock();
377735f861e3SJulian Wiedmann 	if (!netif_is_bridge_port(dev))
3778c34963e2SLinus Lüssing 		goto unlock;
3779c34963e2SLinus Lüssing 
3780c34963e2SLinus Lüssing 	port = br_port_get_rcu(dev);
3781c34963e2SLinus Lüssing 	if (!port || !port->br)
3782c34963e2SLinus Lüssing 		goto unlock;
3783c34963e2SLinus Lüssing 
3784c34963e2SLinus Lüssing 	br = port->br;
3785c34963e2SLinus Lüssing 
3786c34963e2SLinus Lüssing 	memset(&eth, 0, sizeof(eth));
3787c34963e2SLinus Lüssing 	eth.h_proto = htons(proto);
3788c34963e2SLinus Lüssing 
3789955062b0SNikolay Aleksandrov 	ret = br_multicast_querier_exists(br, &eth, NULL);
3790c34963e2SLinus Lüssing 
3791c34963e2SLinus Lüssing unlock:
3792c34963e2SLinus Lüssing 	rcu_read_unlock();
3793c34963e2SLinus Lüssing 	return ret;
3794c34963e2SLinus Lüssing }
3795c34963e2SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
3796c34963e2SLinus Lüssing 
3797c34963e2SLinus Lüssing /**
37982cd41431SLinus Lüssing  * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
37992cd41431SLinus Lüssing  * @dev: The bridge port adjacent to which to check for a querier
38002cd41431SLinus Lüssing  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
38012cd41431SLinus Lüssing  *
38022cd41431SLinus Lüssing  * Checks whether the given interface has a bridge on top and if so returns
38032cd41431SLinus Lüssing  * true if a selected querier is behind one of the other ports of this
38042cd41431SLinus Lüssing  * bridge. Otherwise returns false.
38052cd41431SLinus Lüssing  */
38062cd41431SLinus Lüssing bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
38072cd41431SLinus Lüssing {
38082cd41431SLinus Lüssing 	struct net_bridge *br;
38092cd41431SLinus Lüssing 	struct net_bridge_port *port;
38102cd41431SLinus Lüssing 	bool ret = false;
38112cd41431SLinus Lüssing 
38122cd41431SLinus Lüssing 	rcu_read_lock();
381335f861e3SJulian Wiedmann 	if (!netif_is_bridge_port(dev))
38142cd41431SLinus Lüssing 		goto unlock;
38152cd41431SLinus Lüssing 
38162cd41431SLinus Lüssing 	port = br_port_get_rcu(dev);
38172cd41431SLinus Lüssing 	if (!port || !port->br)
38182cd41431SLinus Lüssing 		goto unlock;
38192cd41431SLinus Lüssing 
38202cd41431SLinus Lüssing 	br = port->br;
38212cd41431SLinus Lüssing 
38222cd41431SLinus Lüssing 	switch (proto) {
38232cd41431SLinus Lüssing 	case ETH_P_IP:
38242cd41431SLinus Lüssing 		if (!timer_pending(&br->ip4_other_query.timer) ||
38252cd41431SLinus Lüssing 		    rcu_dereference(br->ip4_querier.port) == port)
38262cd41431SLinus Lüssing 			goto unlock;
38272cd41431SLinus Lüssing 		break;
38283993c4e1SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
38292cd41431SLinus Lüssing 	case ETH_P_IPV6:
38302cd41431SLinus Lüssing 		if (!timer_pending(&br->ip6_other_query.timer) ||
38312cd41431SLinus Lüssing 		    rcu_dereference(br->ip6_querier.port) == port)
38322cd41431SLinus Lüssing 			goto unlock;
38332cd41431SLinus Lüssing 		break;
38343993c4e1SLinus Lüssing #endif
38352cd41431SLinus Lüssing 	default:
38362cd41431SLinus Lüssing 		goto unlock;
38372cd41431SLinus Lüssing 	}
38382cd41431SLinus Lüssing 
38392cd41431SLinus Lüssing 	ret = true;
38402cd41431SLinus Lüssing unlock:
38412cd41431SLinus Lüssing 	rcu_read_unlock();
38422cd41431SLinus Lüssing 	return ret;
38432cd41431SLinus Lüssing }
38442cd41431SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
38451080ab95SNikolay Aleksandrov 
38461080ab95SNikolay Aleksandrov static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
3847a65056ecSNikolay Aleksandrov 			       const struct sk_buff *skb, u8 type, u8 dir)
38481080ab95SNikolay Aleksandrov {
38491080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
3850a65056ecSNikolay Aleksandrov 	__be16 proto = skb->protocol;
3851a65056ecSNikolay Aleksandrov 	unsigned int t_len;
38521080ab95SNikolay Aleksandrov 
38531080ab95SNikolay Aleksandrov 	u64_stats_update_begin(&pstats->syncp);
38541080ab95SNikolay Aleksandrov 	switch (proto) {
38551080ab95SNikolay Aleksandrov 	case htons(ETH_P_IP):
3856a65056ecSNikolay Aleksandrov 		t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
38571080ab95SNikolay Aleksandrov 		switch (type) {
38581080ab95SNikolay Aleksandrov 		case IGMP_HOST_MEMBERSHIP_REPORT:
38591080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v1reports[dir]++;
38601080ab95SNikolay Aleksandrov 			break;
38611080ab95SNikolay Aleksandrov 		case IGMPV2_HOST_MEMBERSHIP_REPORT:
38621080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v2reports[dir]++;
38631080ab95SNikolay Aleksandrov 			break;
38641080ab95SNikolay Aleksandrov 		case IGMPV3_HOST_MEMBERSHIP_REPORT:
38651080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v3reports[dir]++;
38661080ab95SNikolay Aleksandrov 			break;
38671080ab95SNikolay Aleksandrov 		case IGMP_HOST_MEMBERSHIP_QUERY:
3868a65056ecSNikolay Aleksandrov 			if (t_len != sizeof(struct igmphdr)) {
3869a65056ecSNikolay Aleksandrov 				pstats->mstats.igmp_v3queries[dir]++;
3870a65056ecSNikolay Aleksandrov 			} else {
3871a65056ecSNikolay Aleksandrov 				unsigned int offset = skb_transport_offset(skb);
3872a65056ecSNikolay Aleksandrov 				struct igmphdr *ih, _ihdr;
3873a65056ecSNikolay Aleksandrov 
3874a65056ecSNikolay Aleksandrov 				ih = skb_header_pointer(skb, offset,
3875a65056ecSNikolay Aleksandrov 							sizeof(_ihdr), &_ihdr);
3876a65056ecSNikolay Aleksandrov 				if (!ih)
3877a65056ecSNikolay Aleksandrov 					break;
3878a65056ecSNikolay Aleksandrov 				if (!ih->code)
3879a65056ecSNikolay Aleksandrov 					pstats->mstats.igmp_v1queries[dir]++;
3880a65056ecSNikolay Aleksandrov 				else
3881a65056ecSNikolay Aleksandrov 					pstats->mstats.igmp_v2queries[dir]++;
3882a65056ecSNikolay Aleksandrov 			}
38831080ab95SNikolay Aleksandrov 			break;
38841080ab95SNikolay Aleksandrov 		case IGMP_HOST_LEAVE_MESSAGE:
38851080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_leaves[dir]++;
38861080ab95SNikolay Aleksandrov 			break;
38871080ab95SNikolay Aleksandrov 		}
38881080ab95SNikolay Aleksandrov 		break;
38891080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
38901080ab95SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
3891a65056ecSNikolay Aleksandrov 		t_len = ntohs(ipv6_hdr(skb)->payload_len) +
3892a65056ecSNikolay Aleksandrov 			sizeof(struct ipv6hdr);
3893a65056ecSNikolay Aleksandrov 		t_len -= skb_network_header_len(skb);
38941080ab95SNikolay Aleksandrov 		switch (type) {
38951080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_REPORT:
38961080ab95SNikolay Aleksandrov 			pstats->mstats.mld_v1reports[dir]++;
38971080ab95SNikolay Aleksandrov 			break;
38981080ab95SNikolay Aleksandrov 		case ICMPV6_MLD2_REPORT:
38991080ab95SNikolay Aleksandrov 			pstats->mstats.mld_v2reports[dir]++;
39001080ab95SNikolay Aleksandrov 			break;
39011080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_QUERY:
3902a65056ecSNikolay Aleksandrov 			if (t_len != sizeof(struct mld_msg))
3903a65056ecSNikolay Aleksandrov 				pstats->mstats.mld_v2queries[dir]++;
3904a65056ecSNikolay Aleksandrov 			else
3905a65056ecSNikolay Aleksandrov 				pstats->mstats.mld_v1queries[dir]++;
39061080ab95SNikolay Aleksandrov 			break;
39071080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_REDUCTION:
39081080ab95SNikolay Aleksandrov 			pstats->mstats.mld_leaves[dir]++;
39091080ab95SNikolay Aleksandrov 			break;
39101080ab95SNikolay Aleksandrov 		}
39111080ab95SNikolay Aleksandrov 		break;
39121080ab95SNikolay Aleksandrov #endif /* CONFIG_IPV6 */
39131080ab95SNikolay Aleksandrov 	}
39141080ab95SNikolay Aleksandrov 	u64_stats_update_end(&pstats->syncp);
39151080ab95SNikolay Aleksandrov }
39161080ab95SNikolay Aleksandrov 
39171080ab95SNikolay Aleksandrov void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
3918a65056ecSNikolay Aleksandrov 			const struct sk_buff *skb, u8 type, u8 dir)
39191080ab95SNikolay Aleksandrov {
39201080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
39211080ab95SNikolay Aleksandrov 
39221080ab95SNikolay Aleksandrov 	/* if multicast_disabled is true then igmp type can't be set */
3923675779adSNikolay Aleksandrov 	if (!type || !br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
39241080ab95SNikolay Aleksandrov 		return;
39251080ab95SNikolay Aleksandrov 
39261080ab95SNikolay Aleksandrov 	if (p)
39271080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
39281080ab95SNikolay Aleksandrov 	else
39291080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
39301080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
39311080ab95SNikolay Aleksandrov 		return;
39321080ab95SNikolay Aleksandrov 
3933a65056ecSNikolay Aleksandrov 	br_mcast_stats_add(stats, skb, type, dir);
39341080ab95SNikolay Aleksandrov }
39351080ab95SNikolay Aleksandrov 
39361080ab95SNikolay Aleksandrov int br_multicast_init_stats(struct net_bridge *br)
39371080ab95SNikolay Aleksandrov {
39381080ab95SNikolay Aleksandrov 	br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
39391080ab95SNikolay Aleksandrov 	if (!br->mcast_stats)
39401080ab95SNikolay Aleksandrov 		return -ENOMEM;
39411080ab95SNikolay Aleksandrov 
39421080ab95SNikolay Aleksandrov 	return 0;
39431080ab95SNikolay Aleksandrov }
39441080ab95SNikolay Aleksandrov 
3945b6fe0440SIdo Schimmel void br_multicast_uninit_stats(struct net_bridge *br)
3946b6fe0440SIdo Schimmel {
3947b6fe0440SIdo Schimmel 	free_percpu(br->mcast_stats);
3948b6fe0440SIdo Schimmel }
3949b6fe0440SIdo Schimmel 
3950b3b6a84cSArnd Bergmann /* noinline for https://bugs.llvm.org/show_bug.cgi?id=45802#c9 */
3951b3b6a84cSArnd Bergmann static noinline_for_stack void mcast_stats_add_dir(u64 *dst, u64 *src)
39521080ab95SNikolay Aleksandrov {
39531080ab95SNikolay Aleksandrov 	dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
39541080ab95SNikolay Aleksandrov 	dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
39551080ab95SNikolay Aleksandrov }
39561080ab95SNikolay Aleksandrov 
39571080ab95SNikolay Aleksandrov void br_multicast_get_stats(const struct net_bridge *br,
39581080ab95SNikolay Aleksandrov 			    const struct net_bridge_port *p,
39591080ab95SNikolay Aleksandrov 			    struct br_mcast_stats *dest)
39601080ab95SNikolay Aleksandrov {
39611080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
39621080ab95SNikolay Aleksandrov 	struct br_mcast_stats tdst;
39631080ab95SNikolay Aleksandrov 	int i;
39641080ab95SNikolay Aleksandrov 
39651080ab95SNikolay Aleksandrov 	memset(dest, 0, sizeof(*dest));
39661080ab95SNikolay Aleksandrov 	if (p)
39671080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
39681080ab95SNikolay Aleksandrov 	else
39691080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
39701080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
39711080ab95SNikolay Aleksandrov 		return;
39721080ab95SNikolay Aleksandrov 
39731080ab95SNikolay Aleksandrov 	memset(&tdst, 0, sizeof(tdst));
39741080ab95SNikolay Aleksandrov 	for_each_possible_cpu(i) {
39751080ab95SNikolay Aleksandrov 		struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
39761080ab95SNikolay Aleksandrov 		struct br_mcast_stats temp;
39771080ab95SNikolay Aleksandrov 		unsigned int start;
39781080ab95SNikolay Aleksandrov 
39791080ab95SNikolay Aleksandrov 		do {
39801080ab95SNikolay Aleksandrov 			start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
39811080ab95SNikolay Aleksandrov 			memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
39821080ab95SNikolay Aleksandrov 		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
39831080ab95SNikolay Aleksandrov 
3984a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
3985a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
3986a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
39871080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
39881080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
39891080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
39901080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
39911080ab95SNikolay Aleksandrov 		tdst.igmp_parse_errors += temp.igmp_parse_errors;
39921080ab95SNikolay Aleksandrov 
3993a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
3994a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
39951080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
39961080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
39971080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
39981080ab95SNikolay Aleksandrov 		tdst.mld_parse_errors += temp.mld_parse_errors;
39991080ab95SNikolay Aleksandrov 	}
40001080ab95SNikolay Aleksandrov 	memcpy(dest, &tdst, sizeof(*dest));
40011080ab95SNikolay Aleksandrov }
400219e3a9c9SNikolay Aleksandrov 
400319e3a9c9SNikolay Aleksandrov int br_mdb_hash_init(struct net_bridge *br)
400419e3a9c9SNikolay Aleksandrov {
4005085b53c8SNikolay Aleksandrov 	int err;
4006085b53c8SNikolay Aleksandrov 
4007085b53c8SNikolay Aleksandrov 	err = rhashtable_init(&br->sg_port_tbl, &br_sg_port_rht_params);
4008085b53c8SNikolay Aleksandrov 	if (err)
4009085b53c8SNikolay Aleksandrov 		return err;
4010085b53c8SNikolay Aleksandrov 
4011085b53c8SNikolay Aleksandrov 	err = rhashtable_init(&br->mdb_hash_tbl, &br_mdb_rht_params);
4012085b53c8SNikolay Aleksandrov 	if (err) {
4013085b53c8SNikolay Aleksandrov 		rhashtable_destroy(&br->sg_port_tbl);
4014085b53c8SNikolay Aleksandrov 		return err;
4015085b53c8SNikolay Aleksandrov 	}
4016085b53c8SNikolay Aleksandrov 
4017085b53c8SNikolay Aleksandrov 	return 0;
401819e3a9c9SNikolay Aleksandrov }
401919e3a9c9SNikolay Aleksandrov 
402019e3a9c9SNikolay Aleksandrov void br_mdb_hash_fini(struct net_bridge *br)
402119e3a9c9SNikolay Aleksandrov {
4022085b53c8SNikolay Aleksandrov 	rhashtable_destroy(&br->sg_port_tbl);
402319e3a9c9SNikolay Aleksandrov 	rhashtable_destroy(&br->mdb_hash_tbl);
402419e3a9c9SNikolay Aleksandrov }
4025