xref: /openbmc/linux/net/bridge/br_multicast.c (revision 30515832e987597eae354f6ffcdb3374bdfde16d)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2eb1d1641SHerbert Xu /*
3eb1d1641SHerbert Xu  * Bridge multicast support.
4eb1d1641SHerbert Xu  *
5eb1d1641SHerbert Xu  * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
6eb1d1641SHerbert Xu  */
7eb1d1641SHerbert Xu 
8eb1d1641SHerbert Xu #include <linux/err.h>
907f8ac4aSLinus Lüssing #include <linux/export.h>
10eb1d1641SHerbert Xu #include <linux/if_ether.h>
11eb1d1641SHerbert Xu #include <linux/igmp.h>
124b3087c7SLinus Lüssing #include <linux/in.h>
13eb1d1641SHerbert Xu #include <linux/jhash.h>
14eb1d1641SHerbert Xu #include <linux/kernel.h>
15b195167fSHerbert Xu #include <linux/log2.h>
16eb1d1641SHerbert Xu #include <linux/netdevice.h>
17eb1d1641SHerbert Xu #include <linux/netfilter_bridge.h>
18eb1d1641SHerbert Xu #include <linux/random.h>
19eb1d1641SHerbert Xu #include <linux/rculist.h>
20eb1d1641SHerbert Xu #include <linux/skbuff.h>
21eb1d1641SHerbert Xu #include <linux/slab.h>
22eb1d1641SHerbert Xu #include <linux/timer.h>
231c8ad5bfSCong Wang #include <linux/inetdevice.h>
2491b02d3dSNikolay Aleksandrov #include <linux/mroute.h>
25eb1d1641SHerbert Xu #include <net/ip.h>
26147c1e9bSNogah Frankel #include <net/switchdev.h>
27dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
284b3087c7SLinus Lüssing #include <linux/icmpv6.h>
2908b202b6SYOSHIFUJI Hideaki #include <net/ipv6.h>
3008b202b6SYOSHIFUJI Hideaki #include <net/mld.h>
31d4c4f07dSDavid S. Miller #include <net/ip6_checksum.h>
323c3769e6SLinus Lüssing #include <net/addrconf.h>
3308b202b6SYOSHIFUJI Hideaki #endif
34eb1d1641SHerbert Xu 
35eb1d1641SHerbert Xu #include "br_private.h"
368f07b831SNikolay Aleksandrov #include "br_private_mcast_eht.h"
37eb1d1641SHerbert Xu 
3819e3a9c9SNikolay Aleksandrov static const struct rhashtable_params br_mdb_rht_params = {
3919e3a9c9SNikolay Aleksandrov 	.head_offset = offsetof(struct net_bridge_mdb_entry, rhnode),
4019e3a9c9SNikolay Aleksandrov 	.key_offset = offsetof(struct net_bridge_mdb_entry, addr),
4119e3a9c9SNikolay Aleksandrov 	.key_len = sizeof(struct br_ip),
4219e3a9c9SNikolay Aleksandrov 	.automatic_shrinking = true,
4319e3a9c9SNikolay Aleksandrov };
4419e3a9c9SNikolay Aleksandrov 
45085b53c8SNikolay Aleksandrov static const struct rhashtable_params br_sg_port_rht_params = {
46085b53c8SNikolay Aleksandrov 	.head_offset = offsetof(struct net_bridge_port_group, rhnode),
47085b53c8SNikolay Aleksandrov 	.key_offset = offsetof(struct net_bridge_port_group, key),
48085b53c8SNikolay Aleksandrov 	.key_len = sizeof(struct net_bridge_port_group_sg_key),
49085b53c8SNikolay Aleksandrov 	.automatic_shrinking = true,
50085b53c8SNikolay Aleksandrov };
51085b53c8SNikolay Aleksandrov 
52cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br,
5390010b36SLinus Lüssing 				       struct bridge_mcast_own_query *query);
54d9b8c4d8SLinus Lüssing static void br_ip4_multicast_add_router(struct net_bridge *br,
55754bc547SSatish Ashok 					struct net_bridge_port *port);
56bc8c20acSSatish Ashok static void br_ip4_multicast_leave_group(struct net_bridge *br,
57bc8c20acSSatish Ashok 					 struct net_bridge_port *port,
58bc8c20acSSatish Ashok 					 __be32 group,
596db6f0eaSFelix Fietkau 					 __u16 vid,
606db6f0eaSFelix Fietkau 					 const unsigned char *src);
6142c11ccfSNikolay Aleksandrov static void br_multicast_port_group_rexmit(struct timer_list *t);
626db6f0eaSFelix Fietkau 
63ed2d3597SLinus Lüssing static void
64ed2d3597SLinus Lüssing br_multicast_rport_del_notify(struct net_bridge_port *p, bool deleted);
65a3c02e76SLinus Lüssing static void br_ip6_multicast_add_router(struct net_bridge *br,
66a3c02e76SLinus Lüssing 					struct net_bridge_port *port);
67*30515832SMatteo Croce #if IS_ENABLED(CONFIG_IPV6)
68bc8c20acSSatish Ashok static void br_ip6_multicast_leave_group(struct net_bridge *br,
69bc8c20acSSatish Ashok 					 struct net_bridge_port *port,
70bc8c20acSSatish Ashok 					 const struct in6_addr *group,
716db6f0eaSFelix Fietkau 					 __u16 vid, const unsigned char *src);
72bc8c20acSSatish Ashok #endif
73b0812368SNikolay Aleksandrov static struct net_bridge_port_group *
74b0812368SNikolay Aleksandrov __br_multicast_add_group(struct net_bridge *br,
75b0812368SNikolay Aleksandrov 			 struct net_bridge_port *port,
76b0812368SNikolay Aleksandrov 			 struct br_ip *group,
77b0812368SNikolay Aleksandrov 			 const unsigned char *src,
78b0812368SNikolay Aleksandrov 			 u8 filter_mode,
799116ffbfSNikolay Aleksandrov 			 bool igmpv2_mldv1,
809116ffbfSNikolay Aleksandrov 			 bool blocked);
818266a049SNikolay Aleksandrov static void br_multicast_find_del_pg(struct net_bridge *br,
828266a049SNikolay Aleksandrov 				     struct net_bridge_port_group *pg);
83c83b8fabSHerbert Xu 
84085b53c8SNikolay Aleksandrov static struct net_bridge_port_group *
85085b53c8SNikolay Aleksandrov br_sg_port_find(struct net_bridge *br,
86085b53c8SNikolay Aleksandrov 		struct net_bridge_port_group_sg_key *sg_p)
87085b53c8SNikolay Aleksandrov {
88085b53c8SNikolay Aleksandrov 	lockdep_assert_held_once(&br->multicast_lock);
89085b53c8SNikolay Aleksandrov 
90085b53c8SNikolay Aleksandrov 	return rhashtable_lookup_fast(&br->sg_port_tbl, sg_p,
91085b53c8SNikolay Aleksandrov 				      br_sg_port_rht_params);
92085b53c8SNikolay Aleksandrov }
93085b53c8SNikolay Aleksandrov 
9419e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip_get_rcu(struct net_bridge *br,
95cfd56754SCong Wang 						      struct br_ip *dst)
967f285fa7SHerbert Xu {
9719e3a9c9SNikolay Aleksandrov 	return rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params);
987f285fa7SHerbert Xu }
997f285fa7SHerbert Xu 
10019e3a9c9SNikolay Aleksandrov struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge *br,
10119e3a9c9SNikolay Aleksandrov 					   struct br_ip *dst)
10219e3a9c9SNikolay Aleksandrov {
10319e3a9c9SNikolay Aleksandrov 	struct net_bridge_mdb_entry *ent;
10419e3a9c9SNikolay Aleksandrov 
10519e3a9c9SNikolay Aleksandrov 	lockdep_assert_held_once(&br->multicast_lock);
10619e3a9c9SNikolay Aleksandrov 
10719e3a9c9SNikolay Aleksandrov 	rcu_read_lock();
10819e3a9c9SNikolay Aleksandrov 	ent = rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params);
10919e3a9c9SNikolay Aleksandrov 	rcu_read_unlock();
11019e3a9c9SNikolay Aleksandrov 
11119e3a9c9SNikolay Aleksandrov 	return ent;
11219e3a9c9SNikolay Aleksandrov }
11319e3a9c9SNikolay Aleksandrov 
11419e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip4_get(struct net_bridge *br,
11519e3a9c9SNikolay Aleksandrov 						   __be32 dst, __u16 vid)
116eb1d1641SHerbert Xu {
1178ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip br_dst;
1180821ec55SHerbert Xu 
11919e3a9c9SNikolay Aleksandrov 	memset(&br_dst, 0, sizeof(br_dst));
120eab3227bSNikolay Aleksandrov 	br_dst.dst.ip4 = dst;
1218ef2a9a5SYOSHIFUJI Hideaki 	br_dst.proto = htons(ETH_P_IP);
122b0e9a30dSVlad Yasevich 	br_dst.vid = vid;
1238ef2a9a5SYOSHIFUJI Hideaki 
12419e3a9c9SNikolay Aleksandrov 	return br_mdb_ip_get(br, &br_dst);
1258ef2a9a5SYOSHIFUJI Hideaki }
1268ef2a9a5SYOSHIFUJI Hideaki 
127dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
12819e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip6_get(struct net_bridge *br,
12919e3a9c9SNikolay Aleksandrov 						   const struct in6_addr *dst,
130b0e9a30dSVlad Yasevich 						   __u16 vid)
13108b202b6SYOSHIFUJI Hideaki {
13208b202b6SYOSHIFUJI Hideaki 	struct br_ip br_dst;
13308b202b6SYOSHIFUJI Hideaki 
13419e3a9c9SNikolay Aleksandrov 	memset(&br_dst, 0, sizeof(br_dst));
135eab3227bSNikolay Aleksandrov 	br_dst.dst.ip6 = *dst;
13608b202b6SYOSHIFUJI Hideaki 	br_dst.proto = htons(ETH_P_IPV6);
137b0e9a30dSVlad Yasevich 	br_dst.vid = vid;
13808b202b6SYOSHIFUJI Hideaki 
13919e3a9c9SNikolay Aleksandrov 	return br_mdb_ip_get(br, &br_dst);
14008b202b6SYOSHIFUJI Hideaki }
14108b202b6SYOSHIFUJI Hideaki #endif
14208b202b6SYOSHIFUJI Hideaki 
143eb1d1641SHerbert Xu struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
144fbca58a2SCong Wang 					struct sk_buff *skb, u16 vid)
145eb1d1641SHerbert Xu {
1468ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip ip;
1478ef2a9a5SYOSHIFUJI Hideaki 
14813cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
149eb1d1641SHerbert Xu 		return NULL;
150eb1d1641SHerbert Xu 
1518ef2a9a5SYOSHIFUJI Hideaki 	if (BR_INPUT_SKB_CB(skb)->igmp)
1528ef2a9a5SYOSHIFUJI Hideaki 		return NULL;
1538ef2a9a5SYOSHIFUJI Hideaki 
15419e3a9c9SNikolay Aleksandrov 	memset(&ip, 0, sizeof(ip));
1558ef2a9a5SYOSHIFUJI Hideaki 	ip.proto = skb->protocol;
156fbca58a2SCong Wang 	ip.vid = vid;
1578ef2a9a5SYOSHIFUJI Hideaki 
158eb1d1641SHerbert Xu 	switch (skb->protocol) {
159eb1d1641SHerbert Xu 	case htons(ETH_P_IP):
160eab3227bSNikolay Aleksandrov 		ip.dst.ip4 = ip_hdr(skb)->daddr;
1617d07a68cSNikolay Aleksandrov 		if (br->multicast_igmp_version == 3) {
1627d07a68cSNikolay Aleksandrov 			struct net_bridge_mdb_entry *mdb;
1637d07a68cSNikolay Aleksandrov 
1647d07a68cSNikolay Aleksandrov 			ip.src.ip4 = ip_hdr(skb)->saddr;
1657d07a68cSNikolay Aleksandrov 			mdb = br_mdb_ip_get_rcu(br, &ip);
1667d07a68cSNikolay Aleksandrov 			if (mdb)
1677d07a68cSNikolay Aleksandrov 				return mdb;
1687d07a68cSNikolay Aleksandrov 			ip.src.ip4 = 0;
1697d07a68cSNikolay Aleksandrov 		}
170eb1d1641SHerbert Xu 		break;
171dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
17208b202b6SYOSHIFUJI Hideaki 	case htons(ETH_P_IPV6):
173eab3227bSNikolay Aleksandrov 		ip.dst.ip6 = ipv6_hdr(skb)->daddr;
1747d07a68cSNikolay Aleksandrov 		if (br->multicast_mld_version == 2) {
1757d07a68cSNikolay Aleksandrov 			struct net_bridge_mdb_entry *mdb;
1767d07a68cSNikolay Aleksandrov 
1777d07a68cSNikolay Aleksandrov 			ip.src.ip6 = ipv6_hdr(skb)->saddr;
1787d07a68cSNikolay Aleksandrov 			mdb = br_mdb_ip_get_rcu(br, &ip);
1797d07a68cSNikolay Aleksandrov 			if (mdb)
1807d07a68cSNikolay Aleksandrov 				return mdb;
1817d07a68cSNikolay Aleksandrov 			memset(&ip.src.ip6, 0, sizeof(ip.src.ip6));
1827d07a68cSNikolay Aleksandrov 		}
18308b202b6SYOSHIFUJI Hideaki 		break;
18408b202b6SYOSHIFUJI Hideaki #endif
1858ef2a9a5SYOSHIFUJI Hideaki 	default:
186955062b0SNikolay Aleksandrov 		ip.proto = 0;
187955062b0SNikolay Aleksandrov 		ether_addr_copy(ip.dst.mac_addr, eth_hdr(skb)->h_dest);
188eb1d1641SHerbert Xu 	}
189eb1d1641SHerbert Xu 
19019e3a9c9SNikolay Aleksandrov 	return br_mdb_ip_get_rcu(br, &ip);
191eb1d1641SHerbert Xu }
192eb1d1641SHerbert Xu 
193b0812368SNikolay Aleksandrov static bool br_port_group_equal(struct net_bridge_port_group *p,
194b0812368SNikolay Aleksandrov 				struct net_bridge_port *port,
195b0812368SNikolay Aleksandrov 				const unsigned char *src)
196b0812368SNikolay Aleksandrov {
197b0812368SNikolay Aleksandrov 	if (p->key.port != port)
198b0812368SNikolay Aleksandrov 		return false;
199b0812368SNikolay Aleksandrov 
200b0812368SNikolay Aleksandrov 	if (!(port->flags & BR_MULTICAST_TO_UNICAST))
201b0812368SNikolay Aleksandrov 		return true;
202b0812368SNikolay Aleksandrov 
203b0812368SNikolay Aleksandrov 	return ether_addr_equal(src, p->eth_addr);
204b0812368SNikolay Aleksandrov }
205b0812368SNikolay Aleksandrov 
2068266a049SNikolay Aleksandrov static void __fwd_add_star_excl(struct net_bridge_port_group *pg,
2078266a049SNikolay Aleksandrov 				struct br_ip *sg_ip)
2088266a049SNikolay Aleksandrov {
2098266a049SNikolay Aleksandrov 	struct net_bridge_port_group_sg_key sg_key;
2108266a049SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
2118266a049SNikolay Aleksandrov 	struct net_bridge_port_group *src_pg;
2128266a049SNikolay Aleksandrov 
2138266a049SNikolay Aleksandrov 	memset(&sg_key, 0, sizeof(sg_key));
2148266a049SNikolay Aleksandrov 	sg_key.port = pg->key.port;
2158266a049SNikolay Aleksandrov 	sg_key.addr = *sg_ip;
2168266a049SNikolay Aleksandrov 	if (br_sg_port_find(br, &sg_key))
2178266a049SNikolay Aleksandrov 		return;
2188266a049SNikolay Aleksandrov 
2198266a049SNikolay Aleksandrov 	src_pg = __br_multicast_add_group(br, pg->key.port, sg_ip, pg->eth_addr,
2209116ffbfSNikolay Aleksandrov 					  MCAST_INCLUDE, false, false);
2218266a049SNikolay Aleksandrov 	if (IS_ERR_OR_NULL(src_pg) ||
2228266a049SNikolay Aleksandrov 	    src_pg->rt_protocol != RTPROT_KERNEL)
2238266a049SNikolay Aleksandrov 		return;
2248266a049SNikolay Aleksandrov 
2258266a049SNikolay Aleksandrov 	src_pg->flags |= MDB_PG_FLAGS_STAR_EXCL;
2268266a049SNikolay Aleksandrov }
2278266a049SNikolay Aleksandrov 
2288266a049SNikolay Aleksandrov static void __fwd_del_star_excl(struct net_bridge_port_group *pg,
2298266a049SNikolay Aleksandrov 				struct br_ip *sg_ip)
2308266a049SNikolay Aleksandrov {
2318266a049SNikolay Aleksandrov 	struct net_bridge_port_group_sg_key sg_key;
2328266a049SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
2338266a049SNikolay Aleksandrov 	struct net_bridge_port_group *src_pg;
2348266a049SNikolay Aleksandrov 
2358266a049SNikolay Aleksandrov 	memset(&sg_key, 0, sizeof(sg_key));
2368266a049SNikolay Aleksandrov 	sg_key.port = pg->key.port;
2378266a049SNikolay Aleksandrov 	sg_key.addr = *sg_ip;
2388266a049SNikolay Aleksandrov 	src_pg = br_sg_port_find(br, &sg_key);
2398266a049SNikolay Aleksandrov 	if (!src_pg || !(src_pg->flags & MDB_PG_FLAGS_STAR_EXCL) ||
2408266a049SNikolay Aleksandrov 	    src_pg->rt_protocol != RTPROT_KERNEL)
2418266a049SNikolay Aleksandrov 		return;
2428266a049SNikolay Aleksandrov 
2438266a049SNikolay Aleksandrov 	br_multicast_find_del_pg(br, src_pg);
2448266a049SNikolay Aleksandrov }
2458266a049SNikolay Aleksandrov 
2468266a049SNikolay Aleksandrov /* When a port group transitions to (or is added as) EXCLUDE we need to add it
2478266a049SNikolay Aleksandrov  * to all other ports' S,G entries which are not blocked by the current group
2488266a049SNikolay Aleksandrov  * for proper replication, the assumption is that any S,G blocked entries
2498266a049SNikolay Aleksandrov  * are already added so the S,G,port lookup should skip them.
2508266a049SNikolay Aleksandrov  * When a port group transitions from EXCLUDE -> INCLUDE mode or is being
2518266a049SNikolay Aleksandrov  * deleted we need to remove it from all ports' S,G entries where it was
2528266a049SNikolay Aleksandrov  * automatically installed before (i.e. where it's MDB_PG_FLAGS_STAR_EXCL).
2538266a049SNikolay Aleksandrov  */
2548266a049SNikolay Aleksandrov void br_multicast_star_g_handle_mode(struct net_bridge_port_group *pg,
2558266a049SNikolay Aleksandrov 				     u8 filter_mode)
2568266a049SNikolay Aleksandrov {
2578266a049SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
2588266a049SNikolay Aleksandrov 	struct net_bridge_port_group *pg_lst;
2598266a049SNikolay Aleksandrov 	struct net_bridge_mdb_entry *mp;
2608266a049SNikolay Aleksandrov 	struct br_ip sg_ip;
2618266a049SNikolay Aleksandrov 
2628266a049SNikolay Aleksandrov 	if (WARN_ON(!br_multicast_is_star_g(&pg->key.addr)))
2638266a049SNikolay Aleksandrov 		return;
2648266a049SNikolay Aleksandrov 
2658266a049SNikolay Aleksandrov 	mp = br_mdb_ip_get(br, &pg->key.addr);
2668266a049SNikolay Aleksandrov 	if (!mp)
2678266a049SNikolay Aleksandrov 		return;
2688266a049SNikolay Aleksandrov 
2698266a049SNikolay Aleksandrov 	memset(&sg_ip, 0, sizeof(sg_ip));
2708266a049SNikolay Aleksandrov 	sg_ip = pg->key.addr;
2718266a049SNikolay Aleksandrov 	for (pg_lst = mlock_dereference(mp->ports, br);
2728266a049SNikolay Aleksandrov 	     pg_lst;
2738266a049SNikolay Aleksandrov 	     pg_lst = mlock_dereference(pg_lst->next, br)) {
2748266a049SNikolay Aleksandrov 		struct net_bridge_group_src *src_ent;
2758266a049SNikolay Aleksandrov 
2768266a049SNikolay Aleksandrov 		if (pg_lst == pg)
2778266a049SNikolay Aleksandrov 			continue;
2788266a049SNikolay Aleksandrov 		hlist_for_each_entry(src_ent, &pg_lst->src_list, node) {
2798266a049SNikolay Aleksandrov 			if (!(src_ent->flags & BR_SGRP_F_INSTALLED))
2808266a049SNikolay Aleksandrov 				continue;
2818266a049SNikolay Aleksandrov 			sg_ip.src = src_ent->addr.src;
2828266a049SNikolay Aleksandrov 			switch (filter_mode) {
2838266a049SNikolay Aleksandrov 			case MCAST_INCLUDE:
2848266a049SNikolay Aleksandrov 				__fwd_del_star_excl(pg, &sg_ip);
2858266a049SNikolay Aleksandrov 				break;
2868266a049SNikolay Aleksandrov 			case MCAST_EXCLUDE:
2878266a049SNikolay Aleksandrov 				__fwd_add_star_excl(pg, &sg_ip);
2888266a049SNikolay Aleksandrov 				break;
2898266a049SNikolay Aleksandrov 			}
2908266a049SNikolay Aleksandrov 		}
2918266a049SNikolay Aleksandrov 	}
2928266a049SNikolay Aleksandrov }
2938266a049SNikolay Aleksandrov 
294094b82fdSNikolay Aleksandrov /* called when adding a new S,G with host_joined == false by default */
295094b82fdSNikolay Aleksandrov static void br_multicast_sg_host_state(struct net_bridge_mdb_entry *star_mp,
296094b82fdSNikolay Aleksandrov 				       struct net_bridge_port_group *sg)
297094b82fdSNikolay Aleksandrov {
298094b82fdSNikolay Aleksandrov 	struct net_bridge_mdb_entry *sg_mp;
299094b82fdSNikolay Aleksandrov 
300094b82fdSNikolay Aleksandrov 	if (WARN_ON(!br_multicast_is_star_g(&star_mp->addr)))
301094b82fdSNikolay Aleksandrov 		return;
302094b82fdSNikolay Aleksandrov 	if (!star_mp->host_joined)
303094b82fdSNikolay Aleksandrov 		return;
304094b82fdSNikolay Aleksandrov 
305094b82fdSNikolay Aleksandrov 	sg_mp = br_mdb_ip_get(star_mp->br, &sg->key.addr);
306094b82fdSNikolay Aleksandrov 	if (!sg_mp)
307094b82fdSNikolay Aleksandrov 		return;
308094b82fdSNikolay Aleksandrov 	sg_mp->host_joined = true;
309094b82fdSNikolay Aleksandrov }
310094b82fdSNikolay Aleksandrov 
311094b82fdSNikolay Aleksandrov /* set the host_joined state of all of *,G's S,G entries */
312094b82fdSNikolay Aleksandrov static void br_multicast_star_g_host_state(struct net_bridge_mdb_entry *star_mp)
313094b82fdSNikolay Aleksandrov {
314094b82fdSNikolay Aleksandrov 	struct net_bridge *br = star_mp->br;
315094b82fdSNikolay Aleksandrov 	struct net_bridge_mdb_entry *sg_mp;
316094b82fdSNikolay Aleksandrov 	struct net_bridge_port_group *pg;
317094b82fdSNikolay Aleksandrov 	struct br_ip sg_ip;
318094b82fdSNikolay Aleksandrov 
319094b82fdSNikolay Aleksandrov 	if (WARN_ON(!br_multicast_is_star_g(&star_mp->addr)))
320094b82fdSNikolay Aleksandrov 		return;
321094b82fdSNikolay Aleksandrov 
322094b82fdSNikolay Aleksandrov 	memset(&sg_ip, 0, sizeof(sg_ip));
323094b82fdSNikolay Aleksandrov 	sg_ip = star_mp->addr;
324094b82fdSNikolay Aleksandrov 	for (pg = mlock_dereference(star_mp->ports, br);
325094b82fdSNikolay Aleksandrov 	     pg;
326094b82fdSNikolay Aleksandrov 	     pg = mlock_dereference(pg->next, br)) {
327094b82fdSNikolay Aleksandrov 		struct net_bridge_group_src *src_ent;
328094b82fdSNikolay Aleksandrov 
329094b82fdSNikolay Aleksandrov 		hlist_for_each_entry(src_ent, &pg->src_list, node) {
330094b82fdSNikolay Aleksandrov 			if (!(src_ent->flags & BR_SGRP_F_INSTALLED))
331094b82fdSNikolay Aleksandrov 				continue;
332094b82fdSNikolay Aleksandrov 			sg_ip.src = src_ent->addr.src;
333094b82fdSNikolay Aleksandrov 			sg_mp = br_mdb_ip_get(br, &sg_ip);
334094b82fdSNikolay Aleksandrov 			if (!sg_mp)
335094b82fdSNikolay Aleksandrov 				continue;
336094b82fdSNikolay Aleksandrov 			sg_mp->host_joined = star_mp->host_joined;
337094b82fdSNikolay Aleksandrov 		}
338094b82fdSNikolay Aleksandrov 	}
339094b82fdSNikolay Aleksandrov }
340094b82fdSNikolay Aleksandrov 
3418266a049SNikolay Aleksandrov static void br_multicast_sg_del_exclude_ports(struct net_bridge_mdb_entry *sgmp)
3428266a049SNikolay Aleksandrov {
3438266a049SNikolay Aleksandrov 	struct net_bridge_port_group __rcu **pp;
3448266a049SNikolay Aleksandrov 	struct net_bridge_port_group *p;
3458266a049SNikolay Aleksandrov 
3468266a049SNikolay Aleksandrov 	/* *,G exclude ports are only added to S,G entries */
3478266a049SNikolay Aleksandrov 	if (WARN_ON(br_multicast_is_star_g(&sgmp->addr)))
3488266a049SNikolay Aleksandrov 		return;
3498266a049SNikolay Aleksandrov 
3508266a049SNikolay Aleksandrov 	/* we need the STAR_EXCLUDE ports if there are non-STAR_EXCLUDE ports
3518266a049SNikolay Aleksandrov 	 * we should ignore perm entries since they're managed by user-space
3528266a049SNikolay Aleksandrov 	 */
3538266a049SNikolay Aleksandrov 	for (pp = &sgmp->ports;
3548266a049SNikolay Aleksandrov 	     (p = mlock_dereference(*pp, sgmp->br)) != NULL;
3558266a049SNikolay Aleksandrov 	     pp = &p->next)
3568266a049SNikolay Aleksandrov 		if (!(p->flags & (MDB_PG_FLAGS_STAR_EXCL |
3578266a049SNikolay Aleksandrov 				  MDB_PG_FLAGS_PERMANENT)))
3588266a049SNikolay Aleksandrov 			return;
3598266a049SNikolay Aleksandrov 
360094b82fdSNikolay Aleksandrov 	/* currently the host can only have joined the *,G which means
361094b82fdSNikolay Aleksandrov 	 * we treat it as EXCLUDE {}, so for an S,G it's considered a
362094b82fdSNikolay Aleksandrov 	 * STAR_EXCLUDE entry and we can safely leave it
363094b82fdSNikolay Aleksandrov 	 */
364094b82fdSNikolay Aleksandrov 	sgmp->host_joined = false;
365094b82fdSNikolay Aleksandrov 
3668266a049SNikolay Aleksandrov 	for (pp = &sgmp->ports;
3678266a049SNikolay Aleksandrov 	     (p = mlock_dereference(*pp, sgmp->br)) != NULL;) {
3688266a049SNikolay Aleksandrov 		if (!(p->flags & MDB_PG_FLAGS_PERMANENT))
3698266a049SNikolay Aleksandrov 			br_multicast_del_pg(sgmp, p, pp);
3708266a049SNikolay Aleksandrov 		else
3718266a049SNikolay Aleksandrov 			pp = &p->next;
3728266a049SNikolay Aleksandrov 	}
3738266a049SNikolay Aleksandrov }
3748266a049SNikolay Aleksandrov 
3758266a049SNikolay Aleksandrov void br_multicast_sg_add_exclude_ports(struct net_bridge_mdb_entry *star_mp,
3768266a049SNikolay Aleksandrov 				       struct net_bridge_port_group *sg)
3778266a049SNikolay Aleksandrov {
3788266a049SNikolay Aleksandrov 	struct net_bridge_port_group_sg_key sg_key;
3798266a049SNikolay Aleksandrov 	struct net_bridge *br = star_mp->br;
3808266a049SNikolay Aleksandrov 	struct net_bridge_port_group *pg;
3818266a049SNikolay Aleksandrov 
3828266a049SNikolay Aleksandrov 	if (WARN_ON(br_multicast_is_star_g(&sg->key.addr)))
3838266a049SNikolay Aleksandrov 		return;
3848266a049SNikolay Aleksandrov 	if (WARN_ON(!br_multicast_is_star_g(&star_mp->addr)))
3858266a049SNikolay Aleksandrov 		return;
3868266a049SNikolay Aleksandrov 
387094b82fdSNikolay Aleksandrov 	br_multicast_sg_host_state(star_mp, sg);
3888266a049SNikolay Aleksandrov 	memset(&sg_key, 0, sizeof(sg_key));
3898266a049SNikolay Aleksandrov 	sg_key.addr = sg->key.addr;
3908266a049SNikolay Aleksandrov 	/* we need to add all exclude ports to the S,G */
3918266a049SNikolay Aleksandrov 	for (pg = mlock_dereference(star_mp->ports, br);
3928266a049SNikolay Aleksandrov 	     pg;
3938266a049SNikolay Aleksandrov 	     pg = mlock_dereference(pg->next, br)) {
3948266a049SNikolay Aleksandrov 		struct net_bridge_port_group *src_pg;
3958266a049SNikolay Aleksandrov 
3968266a049SNikolay Aleksandrov 		if (pg == sg || pg->filter_mode == MCAST_INCLUDE)
3978266a049SNikolay Aleksandrov 			continue;
3988266a049SNikolay Aleksandrov 
3998266a049SNikolay Aleksandrov 		sg_key.port = pg->key.port;
4008266a049SNikolay Aleksandrov 		if (br_sg_port_find(br, &sg_key))
4018266a049SNikolay Aleksandrov 			continue;
4028266a049SNikolay Aleksandrov 
4038266a049SNikolay Aleksandrov 		src_pg = __br_multicast_add_group(br, pg->key.port,
4048266a049SNikolay Aleksandrov 						  &sg->key.addr,
4058266a049SNikolay Aleksandrov 						  sg->eth_addr,
4069116ffbfSNikolay Aleksandrov 						  MCAST_INCLUDE, false, false);
4078266a049SNikolay Aleksandrov 		if (IS_ERR_OR_NULL(src_pg) ||
4088266a049SNikolay Aleksandrov 		    src_pg->rt_protocol != RTPROT_KERNEL)
4098266a049SNikolay Aleksandrov 			continue;
4108266a049SNikolay Aleksandrov 		src_pg->flags |= MDB_PG_FLAGS_STAR_EXCL;
4118266a049SNikolay Aleksandrov 	}
4128266a049SNikolay Aleksandrov }
4138266a049SNikolay Aleksandrov 
414b0812368SNikolay Aleksandrov static void br_multicast_fwd_src_add(struct net_bridge_group_src *src)
415b0812368SNikolay Aleksandrov {
4168266a049SNikolay Aleksandrov 	struct net_bridge_mdb_entry *star_mp;
417b0812368SNikolay Aleksandrov 	struct net_bridge_port_group *sg;
418b0812368SNikolay Aleksandrov 	struct br_ip sg_ip;
419b0812368SNikolay Aleksandrov 
420b0812368SNikolay Aleksandrov 	if (src->flags & BR_SGRP_F_INSTALLED)
421b0812368SNikolay Aleksandrov 		return;
422b0812368SNikolay Aleksandrov 
423b0812368SNikolay Aleksandrov 	memset(&sg_ip, 0, sizeof(sg_ip));
424b0812368SNikolay Aleksandrov 	sg_ip = src->pg->key.addr;
425b0812368SNikolay Aleksandrov 	sg_ip.src = src->addr.src;
426b0812368SNikolay Aleksandrov 	sg = __br_multicast_add_group(src->br, src->pg->key.port, &sg_ip,
4279116ffbfSNikolay Aleksandrov 				      src->pg->eth_addr, MCAST_INCLUDE, false,
4289116ffbfSNikolay Aleksandrov 				      !timer_pending(&src->timer));
429b0812368SNikolay Aleksandrov 	if (IS_ERR_OR_NULL(sg))
430b0812368SNikolay Aleksandrov 		return;
431b0812368SNikolay Aleksandrov 	src->flags |= BR_SGRP_F_INSTALLED;
4328266a049SNikolay Aleksandrov 	sg->flags &= ~MDB_PG_FLAGS_STAR_EXCL;
433b0812368SNikolay Aleksandrov 
434b0812368SNikolay Aleksandrov 	/* if it was added by user-space as perm we can skip next steps */
435b0812368SNikolay Aleksandrov 	if (sg->rt_protocol != RTPROT_KERNEL &&
436b0812368SNikolay Aleksandrov 	    (sg->flags & MDB_PG_FLAGS_PERMANENT))
437b0812368SNikolay Aleksandrov 		return;
438b0812368SNikolay Aleksandrov 
439b0812368SNikolay Aleksandrov 	/* the kernel is now responsible for removing this S,G */
440b0812368SNikolay Aleksandrov 	del_timer(&sg->timer);
4418266a049SNikolay Aleksandrov 	star_mp = br_mdb_ip_get(src->br, &src->pg->key.addr);
4428266a049SNikolay Aleksandrov 	if (!star_mp)
4438266a049SNikolay Aleksandrov 		return;
4448266a049SNikolay Aleksandrov 
4458266a049SNikolay Aleksandrov 	br_multicast_sg_add_exclude_ports(star_mp, sg);
446b0812368SNikolay Aleksandrov }
447b0812368SNikolay Aleksandrov 
448d5a10222SNikolay Aleksandrov static void br_multicast_fwd_src_remove(struct net_bridge_group_src *src,
449d5a10222SNikolay Aleksandrov 					bool fastleave)
450b0812368SNikolay Aleksandrov {
451b0812368SNikolay Aleksandrov 	struct net_bridge_port_group *p, *pg = src->pg;
452b0812368SNikolay Aleksandrov 	struct net_bridge_port_group __rcu **pp;
453b0812368SNikolay Aleksandrov 	struct net_bridge_mdb_entry *mp;
454b0812368SNikolay Aleksandrov 	struct br_ip sg_ip;
455b0812368SNikolay Aleksandrov 
456b0812368SNikolay Aleksandrov 	memset(&sg_ip, 0, sizeof(sg_ip));
457b0812368SNikolay Aleksandrov 	sg_ip = pg->key.addr;
458b0812368SNikolay Aleksandrov 	sg_ip.src = src->addr.src;
459b0812368SNikolay Aleksandrov 
460b0812368SNikolay Aleksandrov 	mp = br_mdb_ip_get(src->br, &sg_ip);
461b0812368SNikolay Aleksandrov 	if (!mp)
462b0812368SNikolay Aleksandrov 		return;
463b0812368SNikolay Aleksandrov 
464b0812368SNikolay Aleksandrov 	for (pp = &mp->ports;
465b0812368SNikolay Aleksandrov 	     (p = mlock_dereference(*pp, src->br)) != NULL;
466b0812368SNikolay Aleksandrov 	     pp = &p->next) {
467b0812368SNikolay Aleksandrov 		if (!br_port_group_equal(p, pg->key.port, pg->eth_addr))
468b0812368SNikolay Aleksandrov 			continue;
469b0812368SNikolay Aleksandrov 
470b0812368SNikolay Aleksandrov 		if (p->rt_protocol != RTPROT_KERNEL &&
471b0812368SNikolay Aleksandrov 		    (p->flags & MDB_PG_FLAGS_PERMANENT))
472b0812368SNikolay Aleksandrov 			break;
473b0812368SNikolay Aleksandrov 
474d5a10222SNikolay Aleksandrov 		if (fastleave)
475d5a10222SNikolay Aleksandrov 			p->flags |= MDB_PG_FLAGS_FAST_LEAVE;
476b0812368SNikolay Aleksandrov 		br_multicast_del_pg(mp, p, pp);
477b0812368SNikolay Aleksandrov 		break;
478b0812368SNikolay Aleksandrov 	}
479b0812368SNikolay Aleksandrov 	src->flags &= ~BR_SGRP_F_INSTALLED;
480b0812368SNikolay Aleksandrov }
481b0812368SNikolay Aleksandrov 
4829116ffbfSNikolay Aleksandrov /* install S,G and based on src's timer enable or disable forwarding */
483b0812368SNikolay Aleksandrov static void br_multicast_fwd_src_handle(struct net_bridge_group_src *src)
484b0812368SNikolay Aleksandrov {
4859116ffbfSNikolay Aleksandrov 	struct net_bridge_port_group_sg_key sg_key;
4869116ffbfSNikolay Aleksandrov 	struct net_bridge_port_group *sg;
4879116ffbfSNikolay Aleksandrov 	u8 old_flags;
4889116ffbfSNikolay Aleksandrov 
489b0812368SNikolay Aleksandrov 	br_multicast_fwd_src_add(src);
4909116ffbfSNikolay Aleksandrov 
4919116ffbfSNikolay Aleksandrov 	memset(&sg_key, 0, sizeof(sg_key));
4929116ffbfSNikolay Aleksandrov 	sg_key.addr = src->pg->key.addr;
4939116ffbfSNikolay Aleksandrov 	sg_key.addr.src = src->addr.src;
4949116ffbfSNikolay Aleksandrov 	sg_key.port = src->pg->key.port;
4959116ffbfSNikolay Aleksandrov 
4969116ffbfSNikolay Aleksandrov 	sg = br_sg_port_find(src->br, &sg_key);
4979116ffbfSNikolay Aleksandrov 	if (!sg || (sg->flags & MDB_PG_FLAGS_PERMANENT))
4989116ffbfSNikolay Aleksandrov 		return;
4999116ffbfSNikolay Aleksandrov 
5009116ffbfSNikolay Aleksandrov 	old_flags = sg->flags;
5019116ffbfSNikolay Aleksandrov 	if (timer_pending(&src->timer))
5029116ffbfSNikolay Aleksandrov 		sg->flags &= ~MDB_PG_FLAGS_BLOCKED;
5039116ffbfSNikolay Aleksandrov 	else
5049116ffbfSNikolay Aleksandrov 		sg->flags |= MDB_PG_FLAGS_BLOCKED;
5059116ffbfSNikolay Aleksandrov 
5069116ffbfSNikolay Aleksandrov 	if (old_flags != sg->flags) {
5079116ffbfSNikolay Aleksandrov 		struct net_bridge_mdb_entry *sg_mp;
5089116ffbfSNikolay Aleksandrov 
5099116ffbfSNikolay Aleksandrov 		sg_mp = br_mdb_ip_get(src->br, &sg_key.addr);
5109116ffbfSNikolay Aleksandrov 		if (!sg_mp)
5119116ffbfSNikolay Aleksandrov 			return;
5129116ffbfSNikolay Aleksandrov 		br_mdb_notify(src->br->dev, sg_mp, sg, RTM_NEWMDB);
5139116ffbfSNikolay Aleksandrov 	}
514b0812368SNikolay Aleksandrov }
515b0812368SNikolay Aleksandrov 
516e12cec65SNikolay Aleksandrov static void br_multicast_destroy_mdb_entry(struct net_bridge_mcast_gc *gc)
517e12cec65SNikolay Aleksandrov {
518e12cec65SNikolay Aleksandrov 	struct net_bridge_mdb_entry *mp;
519e12cec65SNikolay Aleksandrov 
520e12cec65SNikolay Aleksandrov 	mp = container_of(gc, struct net_bridge_mdb_entry, mcast_gc);
521e12cec65SNikolay Aleksandrov 	WARN_ON(!hlist_unhashed(&mp->mdb_node));
522e12cec65SNikolay Aleksandrov 	WARN_ON(mp->ports);
523e12cec65SNikolay Aleksandrov 
524e12cec65SNikolay Aleksandrov 	del_timer_sync(&mp->timer);
525e12cec65SNikolay Aleksandrov 	kfree_rcu(mp, rcu);
526e12cec65SNikolay Aleksandrov }
527e12cec65SNikolay Aleksandrov 
528e12cec65SNikolay Aleksandrov static void br_multicast_del_mdb_entry(struct net_bridge_mdb_entry *mp)
529e12cec65SNikolay Aleksandrov {
530e12cec65SNikolay Aleksandrov 	struct net_bridge *br = mp->br;
531e12cec65SNikolay Aleksandrov 
532e12cec65SNikolay Aleksandrov 	rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode,
533e12cec65SNikolay Aleksandrov 			       br_mdb_rht_params);
534e12cec65SNikolay Aleksandrov 	hlist_del_init_rcu(&mp->mdb_node);
535e12cec65SNikolay Aleksandrov 	hlist_add_head(&mp->mcast_gc.gc_node, &br->mcast_gc_list);
536e12cec65SNikolay Aleksandrov 	queue_work(system_long_wq, &br->mcast_gc_work);
537e12cec65SNikolay Aleksandrov }
538e12cec65SNikolay Aleksandrov 
53988c1f37fSAllen Pais static void br_multicast_group_expired(struct timer_list *t)
540eb1d1641SHerbert Xu {
54188c1f37fSAllen Pais 	struct net_bridge_mdb_entry *mp = from_timer(mp, t, timer);
542eb1d1641SHerbert Xu 	struct net_bridge *br = mp->br;
543eb1d1641SHerbert Xu 
544eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
545b0812368SNikolay Aleksandrov 	if (hlist_unhashed(&mp->mdb_node) || !netif_running(br->dev) ||
546b0812368SNikolay Aleksandrov 	    timer_pending(&mp->timer))
547eb1d1641SHerbert Xu 		goto out;
548eb1d1641SHerbert Xu 
5491bc844eeSNikolay Aleksandrov 	br_multicast_host_leave(mp, true);
550eb1d1641SHerbert Xu 
551eb1d1641SHerbert Xu 	if (mp->ports)
552eb1d1641SHerbert Xu 		goto out;
553e12cec65SNikolay Aleksandrov 	br_multicast_del_mdb_entry(mp);
554eb1d1641SHerbert Xu out:
555eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
556eb1d1641SHerbert Xu }
557eb1d1641SHerbert Xu 
558e12cec65SNikolay Aleksandrov static void br_multicast_destroy_group_src(struct net_bridge_mcast_gc *gc)
559e12cec65SNikolay Aleksandrov {
560e12cec65SNikolay Aleksandrov 	struct net_bridge_group_src *src;
561e12cec65SNikolay Aleksandrov 
562e12cec65SNikolay Aleksandrov 	src = container_of(gc, struct net_bridge_group_src, mcast_gc);
563e12cec65SNikolay Aleksandrov 	WARN_ON(!hlist_unhashed(&src->node));
564e12cec65SNikolay Aleksandrov 
565e12cec65SNikolay Aleksandrov 	del_timer_sync(&src->timer);
566e12cec65SNikolay Aleksandrov 	kfree_rcu(src, rcu);
567e12cec65SNikolay Aleksandrov }
568e12cec65SNikolay Aleksandrov 
569d5a10222SNikolay Aleksandrov void br_multicast_del_group_src(struct net_bridge_group_src *src,
570d5a10222SNikolay Aleksandrov 				bool fastleave)
5718b671779SNikolay Aleksandrov {
572085b53c8SNikolay Aleksandrov 	struct net_bridge *br = src->pg->key.port->br;
5738b671779SNikolay Aleksandrov 
574d5a10222SNikolay Aleksandrov 	br_multicast_fwd_src_remove(src, fastleave);
5758b671779SNikolay Aleksandrov 	hlist_del_init_rcu(&src->node);
5768b671779SNikolay Aleksandrov 	src->pg->src_ents--;
577e12cec65SNikolay Aleksandrov 	hlist_add_head(&src->mcast_gc.gc_node, &br->mcast_gc_list);
578e12cec65SNikolay Aleksandrov 	queue_work(system_long_wq, &br->mcast_gc_work);
579e12cec65SNikolay Aleksandrov }
580e12cec65SNikolay Aleksandrov 
581e12cec65SNikolay Aleksandrov static void br_multicast_destroy_port_group(struct net_bridge_mcast_gc *gc)
582e12cec65SNikolay Aleksandrov {
583e12cec65SNikolay Aleksandrov 	struct net_bridge_port_group *pg;
584e12cec65SNikolay Aleksandrov 
585e12cec65SNikolay Aleksandrov 	pg = container_of(gc, struct net_bridge_port_group, mcast_gc);
586e12cec65SNikolay Aleksandrov 	WARN_ON(!hlist_unhashed(&pg->mglist));
587e12cec65SNikolay Aleksandrov 	WARN_ON(!hlist_empty(&pg->src_list));
588e12cec65SNikolay Aleksandrov 
589e12cec65SNikolay Aleksandrov 	del_timer_sync(&pg->rexmit_timer);
590e12cec65SNikolay Aleksandrov 	del_timer_sync(&pg->timer);
591e12cec65SNikolay Aleksandrov 	kfree_rcu(pg, rcu);
5928b671779SNikolay Aleksandrov }
5938b671779SNikolay Aleksandrov 
594681590bdSNikolay Aleksandrov void br_multicast_del_pg(struct net_bridge_mdb_entry *mp,
595681590bdSNikolay Aleksandrov 			 struct net_bridge_port_group *pg,
596681590bdSNikolay Aleksandrov 			 struct net_bridge_port_group __rcu **pp)
597681590bdSNikolay Aleksandrov {
598085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
5998b671779SNikolay Aleksandrov 	struct net_bridge_group_src *ent;
6008b671779SNikolay Aleksandrov 	struct hlist_node *tmp;
601681590bdSNikolay Aleksandrov 
602681590bdSNikolay Aleksandrov 	rcu_assign_pointer(*pp, pg->next);
603681590bdSNikolay Aleksandrov 	hlist_del_init(&pg->mglist);
604baa74d39SNikolay Aleksandrov 	br_multicast_eht_clean_sets(pg);
6058b671779SNikolay Aleksandrov 	hlist_for_each_entry_safe(ent, tmp, &pg->src_list, node)
606d5a10222SNikolay Aleksandrov 		br_multicast_del_group_src(ent, false);
60781f19838SNikolay Aleksandrov 	br_mdb_notify(br->dev, mp, pg, RTM_DELMDB);
60874705582SNikolay Aleksandrov 	if (!br_multicast_is_star_g(&mp->addr)) {
60974705582SNikolay Aleksandrov 		rhashtable_remove_fast(&br->sg_port_tbl, &pg->rhnode,
61074705582SNikolay Aleksandrov 				       br_sg_port_rht_params);
6118266a049SNikolay Aleksandrov 		br_multicast_sg_del_exclude_ports(mp);
61274705582SNikolay Aleksandrov 	} else {
6138266a049SNikolay Aleksandrov 		br_multicast_star_g_handle_mode(pg, MCAST_INCLUDE);
61474705582SNikolay Aleksandrov 	}
615e12cec65SNikolay Aleksandrov 	hlist_add_head(&pg->mcast_gc.gc_node, &br->mcast_gc_list);
616e12cec65SNikolay Aleksandrov 	queue_work(system_long_wq, &br->mcast_gc_work);
617681590bdSNikolay Aleksandrov 
618681590bdSNikolay Aleksandrov 	if (!mp->ports && !mp->host_joined && netif_running(br->dev))
619681590bdSNikolay Aleksandrov 		mod_timer(&mp->timer, jiffies);
620681590bdSNikolay Aleksandrov }
621681590bdSNikolay Aleksandrov 
622681590bdSNikolay Aleksandrov static void br_multicast_find_del_pg(struct net_bridge *br,
623eb1d1641SHerbert Xu 				     struct net_bridge_port_group *pg)
624eb1d1641SHerbert Xu {
6258b671779SNikolay Aleksandrov 	struct net_bridge_port_group __rcu **pp;
626eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
627eb1d1641SHerbert Xu 	struct net_bridge_port_group *p;
628e8051688SEric Dumazet 
629085b53c8SNikolay Aleksandrov 	mp = br_mdb_ip_get(br, &pg->key.addr);
630eb1d1641SHerbert Xu 	if (WARN_ON(!mp))
631eb1d1641SHerbert Xu 		return;
632eb1d1641SHerbert Xu 
633e8051688SEric Dumazet 	for (pp = &mp->ports;
634e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
635e8051688SEric Dumazet 	     pp = &p->next) {
636eb1d1641SHerbert Xu 		if (p != pg)
637eb1d1641SHerbert Xu 			continue;
638eb1d1641SHerbert Xu 
639681590bdSNikolay Aleksandrov 		br_multicast_del_pg(mp, pg, pp);
640eb1d1641SHerbert Xu 		return;
641eb1d1641SHerbert Xu 	}
642eb1d1641SHerbert Xu 
643eb1d1641SHerbert Xu 	WARN_ON(1);
644eb1d1641SHerbert Xu }
645eb1d1641SHerbert Xu 
64688c1f37fSAllen Pais static void br_multicast_port_group_expired(struct timer_list *t)
647eb1d1641SHerbert Xu {
64888c1f37fSAllen Pais 	struct net_bridge_port_group *pg = from_timer(pg, t, timer);
649d6c33d67SNikolay Aleksandrov 	struct net_bridge_group_src *src_ent;
650085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
651d6c33d67SNikolay Aleksandrov 	struct hlist_node *tmp;
652d6c33d67SNikolay Aleksandrov 	bool changed;
653eb1d1641SHerbert Xu 
654eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
655eb1d1641SHerbert Xu 	if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
6569d06b6d8SElad Raz 	    hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT)
657eb1d1641SHerbert Xu 		goto out;
658eb1d1641SHerbert Xu 
659d6c33d67SNikolay Aleksandrov 	changed = !!(pg->filter_mode == MCAST_EXCLUDE);
660d6c33d67SNikolay Aleksandrov 	pg->filter_mode = MCAST_INCLUDE;
661d6c33d67SNikolay Aleksandrov 	hlist_for_each_entry_safe(src_ent, tmp, &pg->src_list, node) {
662d6c33d67SNikolay Aleksandrov 		if (!timer_pending(&src_ent->timer)) {
663d5a10222SNikolay Aleksandrov 			br_multicast_del_group_src(src_ent, false);
664d6c33d67SNikolay Aleksandrov 			changed = true;
665d6c33d67SNikolay Aleksandrov 		}
666d6c33d67SNikolay Aleksandrov 	}
667eb1d1641SHerbert Xu 
668d6c33d67SNikolay Aleksandrov 	if (hlist_empty(&pg->src_list)) {
669d6c33d67SNikolay Aleksandrov 		br_multicast_find_del_pg(br, pg);
670d6c33d67SNikolay Aleksandrov 	} else if (changed) {
671085b53c8SNikolay Aleksandrov 		struct net_bridge_mdb_entry *mp = br_mdb_ip_get(br, &pg->key.addr);
672d6c33d67SNikolay Aleksandrov 
6738266a049SNikolay Aleksandrov 		if (changed && br_multicast_is_star_g(&pg->key.addr))
6748266a049SNikolay Aleksandrov 			br_multicast_star_g_handle_mode(pg, MCAST_INCLUDE);
6758266a049SNikolay Aleksandrov 
676d6c33d67SNikolay Aleksandrov 		if (WARN_ON(!mp))
677d6c33d67SNikolay Aleksandrov 			goto out;
678d6c33d67SNikolay Aleksandrov 		br_mdb_notify(br->dev, mp, pg, RTM_NEWMDB);
679d6c33d67SNikolay Aleksandrov 	}
680eb1d1641SHerbert Xu out:
681eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
682eb1d1641SHerbert Xu }
683eb1d1641SHerbert Xu 
684e12cec65SNikolay Aleksandrov static void br_multicast_gc(struct hlist_head *head)
685e12cec65SNikolay Aleksandrov {
686e12cec65SNikolay Aleksandrov 	struct net_bridge_mcast_gc *gcent;
687e12cec65SNikolay Aleksandrov 	struct hlist_node *tmp;
688e12cec65SNikolay Aleksandrov 
689e12cec65SNikolay Aleksandrov 	hlist_for_each_entry_safe(gcent, tmp, head, gc_node) {
690e12cec65SNikolay Aleksandrov 		hlist_del_init(&gcent->gc_node);
691e12cec65SNikolay Aleksandrov 		gcent->destroy(gcent);
692e12cec65SNikolay Aleksandrov 	}
693e12cec65SNikolay Aleksandrov }
694e12cec65SNikolay Aleksandrov 
6958ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
696438ef2d0SNikolay Aleksandrov 						    struct net_bridge_port_group *pg,
697438ef2d0SNikolay Aleksandrov 						    __be32 ip_dst, __be32 group,
698438ef2d0SNikolay Aleksandrov 						    bool with_srcs, bool over_lmqt,
69942c11ccfSNikolay Aleksandrov 						    u8 sflag, u8 *igmp_type,
70042c11ccfSNikolay Aleksandrov 						    bool *need_rexmit)
701eb1d1641SHerbert Xu {
702085b53c8SNikolay Aleksandrov 	struct net_bridge_port *p = pg ? pg->key.port : NULL;
703438ef2d0SNikolay Aleksandrov 	struct net_bridge_group_src *ent;
704438ef2d0SNikolay Aleksandrov 	size_t pkt_size, igmp_hdr_size;
705438ef2d0SNikolay Aleksandrov 	unsigned long now = jiffies;
7065e923585SNikolay Aleksandrov 	struct igmpv3_query *ihv3;
707438ef2d0SNikolay Aleksandrov 	void *csum_start = NULL;
708438ef2d0SNikolay Aleksandrov 	__sum16 *csum = NULL;
709eb1d1641SHerbert Xu 	struct sk_buff *skb;
710eb1d1641SHerbert Xu 	struct igmphdr *ih;
711eb1d1641SHerbert Xu 	struct ethhdr *eth;
712438ef2d0SNikolay Aleksandrov 	unsigned long lmqt;
713eb1d1641SHerbert Xu 	struct iphdr *iph;
714438ef2d0SNikolay Aleksandrov 	u16 lmqt_srcs = 0;
715eb1d1641SHerbert Xu 
7165e923585SNikolay Aleksandrov 	igmp_hdr_size = sizeof(*ih);
717438ef2d0SNikolay Aleksandrov 	if (br->multicast_igmp_version == 3) {
7185e923585SNikolay Aleksandrov 		igmp_hdr_size = sizeof(*ihv3);
719438ef2d0SNikolay Aleksandrov 		if (pg && with_srcs) {
720438ef2d0SNikolay Aleksandrov 			lmqt = now + (br->multicast_last_member_interval *
721438ef2d0SNikolay Aleksandrov 				      br->multicast_last_member_count);
722438ef2d0SNikolay Aleksandrov 			hlist_for_each_entry(ent, &pg->src_list, node) {
723438ef2d0SNikolay Aleksandrov 				if (over_lmqt == time_after(ent->timer.expires,
724438ef2d0SNikolay Aleksandrov 							    lmqt) &&
725438ef2d0SNikolay Aleksandrov 				    ent->src_query_rexmit_cnt > 0)
726438ef2d0SNikolay Aleksandrov 					lmqt_srcs++;
727438ef2d0SNikolay Aleksandrov 			}
728438ef2d0SNikolay Aleksandrov 
729438ef2d0SNikolay Aleksandrov 			if (!lmqt_srcs)
730438ef2d0SNikolay Aleksandrov 				return NULL;
731438ef2d0SNikolay Aleksandrov 			igmp_hdr_size += lmqt_srcs * sizeof(__be32);
732438ef2d0SNikolay Aleksandrov 		}
733438ef2d0SNikolay Aleksandrov 	}
734438ef2d0SNikolay Aleksandrov 
735438ef2d0SNikolay Aleksandrov 	pkt_size = sizeof(*eth) + sizeof(*iph) + 4 + igmp_hdr_size;
736438ef2d0SNikolay Aleksandrov 	if ((p && pkt_size > p->dev->mtu) ||
737438ef2d0SNikolay Aleksandrov 	    pkt_size > br->dev->mtu)
738438ef2d0SNikolay Aleksandrov 		return NULL;
739438ef2d0SNikolay Aleksandrov 
740438ef2d0SNikolay Aleksandrov 	skb = netdev_alloc_skb_ip_align(br->dev, pkt_size);
741eb1d1641SHerbert Xu 	if (!skb)
742eb1d1641SHerbert Xu 		goto out;
743eb1d1641SHerbert Xu 
744eb1d1641SHerbert Xu 	skb->protocol = htons(ETH_P_IP);
745eb1d1641SHerbert Xu 
746eb1d1641SHerbert Xu 	skb_reset_mac_header(skb);
747eb1d1641SHerbert Xu 	eth = eth_hdr(skb);
748eb1d1641SHerbert Xu 
749e5a727f6SJoe Perches 	ether_addr_copy(eth->h_source, br->dev->dev_addr);
750438ef2d0SNikolay Aleksandrov 	ip_eth_mc_map(ip_dst, eth->h_dest);
751eb1d1641SHerbert Xu 	eth->h_proto = htons(ETH_P_IP);
752eb1d1641SHerbert Xu 	skb_put(skb, sizeof(*eth));
753eb1d1641SHerbert Xu 
754eb1d1641SHerbert Xu 	skb_set_network_header(skb, skb->len);
755eb1d1641SHerbert Xu 	iph = ip_hdr(skb);
756438ef2d0SNikolay Aleksandrov 	iph->tot_len = htons(pkt_size - sizeof(*eth));
757eb1d1641SHerbert Xu 
758eb1d1641SHerbert Xu 	iph->version = 4;
759eb1d1641SHerbert Xu 	iph->ihl = 6;
760eb1d1641SHerbert Xu 	iph->tos = 0xc0;
761eb1d1641SHerbert Xu 	iph->id = 0;
762eb1d1641SHerbert Xu 	iph->frag_off = htons(IP_DF);
763eb1d1641SHerbert Xu 	iph->ttl = 1;
764eb1d1641SHerbert Xu 	iph->protocol = IPPROTO_IGMP;
765675779adSNikolay Aleksandrov 	iph->saddr = br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR) ?
7661c8ad5bfSCong Wang 		     inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0;
767438ef2d0SNikolay Aleksandrov 	iph->daddr = ip_dst;
768eb1d1641SHerbert Xu 	((u8 *)&iph[1])[0] = IPOPT_RA;
769eb1d1641SHerbert Xu 	((u8 *)&iph[1])[1] = 4;
770eb1d1641SHerbert Xu 	((u8 *)&iph[1])[2] = 0;
771eb1d1641SHerbert Xu 	((u8 *)&iph[1])[3] = 0;
772eb1d1641SHerbert Xu 	ip_send_check(iph);
773eb1d1641SHerbert Xu 	skb_put(skb, 24);
774eb1d1641SHerbert Xu 
775eb1d1641SHerbert Xu 	skb_set_transport_header(skb, skb->len);
7761080ab95SNikolay Aleksandrov 	*igmp_type = IGMP_HOST_MEMBERSHIP_QUERY;
7775e923585SNikolay Aleksandrov 
7785e923585SNikolay Aleksandrov 	switch (br->multicast_igmp_version) {
7795e923585SNikolay Aleksandrov 	case 2:
7805e923585SNikolay Aleksandrov 		ih = igmp_hdr(skb);
781eb1d1641SHerbert Xu 		ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
782eb1d1641SHerbert Xu 		ih->code = (group ? br->multicast_last_member_interval :
783eb1d1641SHerbert Xu 				    br->multicast_query_response_interval) /
784eb1d1641SHerbert Xu 			   (HZ / IGMP_TIMER_SCALE);
785eb1d1641SHerbert Xu 		ih->group = group;
786eb1d1641SHerbert Xu 		ih->csum = 0;
787438ef2d0SNikolay Aleksandrov 		csum = &ih->csum;
788438ef2d0SNikolay Aleksandrov 		csum_start = (void *)ih;
7895e923585SNikolay Aleksandrov 		break;
7905e923585SNikolay Aleksandrov 	case 3:
7915e923585SNikolay Aleksandrov 		ihv3 = igmpv3_query_hdr(skb);
7925e923585SNikolay Aleksandrov 		ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY;
7935e923585SNikolay Aleksandrov 		ihv3->code = (group ? br->multicast_last_member_interval :
7945e923585SNikolay Aleksandrov 				      br->multicast_query_response_interval) /
7955e923585SNikolay Aleksandrov 			     (HZ / IGMP_TIMER_SCALE);
7965e923585SNikolay Aleksandrov 		ihv3->group = group;
7975e923585SNikolay Aleksandrov 		ihv3->qqic = br->multicast_query_interval / HZ;
798438ef2d0SNikolay Aleksandrov 		ihv3->nsrcs = htons(lmqt_srcs);
7995e923585SNikolay Aleksandrov 		ihv3->resv = 0;
800438ef2d0SNikolay Aleksandrov 		ihv3->suppress = sflag;
8015e923585SNikolay Aleksandrov 		ihv3->qrv = 2;
8025e923585SNikolay Aleksandrov 		ihv3->csum = 0;
803438ef2d0SNikolay Aleksandrov 		csum = &ihv3->csum;
804438ef2d0SNikolay Aleksandrov 		csum_start = (void *)ihv3;
805438ef2d0SNikolay Aleksandrov 		if (!pg || !with_srcs)
806438ef2d0SNikolay Aleksandrov 			break;
807438ef2d0SNikolay Aleksandrov 
808438ef2d0SNikolay Aleksandrov 		lmqt_srcs = 0;
809438ef2d0SNikolay Aleksandrov 		hlist_for_each_entry(ent, &pg->src_list, node) {
810438ef2d0SNikolay Aleksandrov 			if (over_lmqt == time_after(ent->timer.expires,
811438ef2d0SNikolay Aleksandrov 						    lmqt) &&
812438ef2d0SNikolay Aleksandrov 			    ent->src_query_rexmit_cnt > 0) {
813deb96566SNikolay Aleksandrov 				ihv3->srcs[lmqt_srcs++] = ent->addr.src.ip4;
814438ef2d0SNikolay Aleksandrov 				ent->src_query_rexmit_cnt--;
81542c11ccfSNikolay Aleksandrov 				if (need_rexmit && ent->src_query_rexmit_cnt)
81642c11ccfSNikolay Aleksandrov 					*need_rexmit = true;
817438ef2d0SNikolay Aleksandrov 			}
818438ef2d0SNikolay Aleksandrov 		}
819438ef2d0SNikolay Aleksandrov 		if (WARN_ON(lmqt_srcs != ntohs(ihv3->nsrcs))) {
820438ef2d0SNikolay Aleksandrov 			kfree_skb(skb);
821438ef2d0SNikolay Aleksandrov 			return NULL;
822438ef2d0SNikolay Aleksandrov 		}
8235e923585SNikolay Aleksandrov 		break;
8245e923585SNikolay Aleksandrov 	}
825eb1d1641SHerbert Xu 
826438ef2d0SNikolay Aleksandrov 	if (WARN_ON(!csum || !csum_start)) {
827438ef2d0SNikolay Aleksandrov 		kfree_skb(skb);
828438ef2d0SNikolay Aleksandrov 		return NULL;
829438ef2d0SNikolay Aleksandrov 	}
830438ef2d0SNikolay Aleksandrov 
831438ef2d0SNikolay Aleksandrov 	*csum = ip_compute_csum(csum_start, igmp_hdr_size);
8325e923585SNikolay Aleksandrov 	skb_put(skb, igmp_hdr_size);
833eb1d1641SHerbert Xu 	__skb_pull(skb, sizeof(*eth));
834eb1d1641SHerbert Xu 
835eb1d1641SHerbert Xu out:
836eb1d1641SHerbert Xu 	return skb;
837eb1d1641SHerbert Xu }
838eb1d1641SHerbert Xu 
839dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
84008b202b6SYOSHIFUJI Hideaki static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
841438ef2d0SNikolay Aleksandrov 						    struct net_bridge_port_group *pg,
842438ef2d0SNikolay Aleksandrov 						    const struct in6_addr *ip6_dst,
843438ef2d0SNikolay Aleksandrov 						    const struct in6_addr *group,
844438ef2d0SNikolay Aleksandrov 						    bool with_srcs, bool over_llqt,
84542c11ccfSNikolay Aleksandrov 						    u8 sflag, u8 *igmp_type,
84642c11ccfSNikolay Aleksandrov 						    bool *need_rexmit)
84708b202b6SYOSHIFUJI Hideaki {
848085b53c8SNikolay Aleksandrov 	struct net_bridge_port *p = pg ? pg->key.port : NULL;
849438ef2d0SNikolay Aleksandrov 	struct net_bridge_group_src *ent;
850438ef2d0SNikolay Aleksandrov 	size_t pkt_size, mld_hdr_size;
851438ef2d0SNikolay Aleksandrov 	unsigned long now = jiffies;
852aa2ae3e7SNikolay Aleksandrov 	struct mld2_query *mld2q;
853438ef2d0SNikolay Aleksandrov 	void *csum_start = NULL;
854aa2ae3e7SNikolay Aleksandrov 	unsigned long interval;
855438ef2d0SNikolay Aleksandrov 	__sum16 *csum = NULL;
85608b202b6SYOSHIFUJI Hideaki 	struct ipv6hdr *ip6h;
85708b202b6SYOSHIFUJI Hideaki 	struct mld_msg *mldq;
858aa2ae3e7SNikolay Aleksandrov 	struct sk_buff *skb;
859438ef2d0SNikolay Aleksandrov 	unsigned long llqt;
86008b202b6SYOSHIFUJI Hideaki 	struct ethhdr *eth;
861438ef2d0SNikolay Aleksandrov 	u16 llqt_srcs = 0;
86208b202b6SYOSHIFUJI Hideaki 	u8 *hopopt;
86308b202b6SYOSHIFUJI Hideaki 
864aa2ae3e7SNikolay Aleksandrov 	mld_hdr_size = sizeof(*mldq);
865438ef2d0SNikolay Aleksandrov 	if (br->multicast_mld_version == 2) {
866aa2ae3e7SNikolay Aleksandrov 		mld_hdr_size = sizeof(*mld2q);
867438ef2d0SNikolay Aleksandrov 		if (pg && with_srcs) {
868438ef2d0SNikolay Aleksandrov 			llqt = now + (br->multicast_last_member_interval *
869438ef2d0SNikolay Aleksandrov 				      br->multicast_last_member_count);
870438ef2d0SNikolay Aleksandrov 			hlist_for_each_entry(ent, &pg->src_list, node) {
871438ef2d0SNikolay Aleksandrov 				if (over_llqt == time_after(ent->timer.expires,
872438ef2d0SNikolay Aleksandrov 							    llqt) &&
873438ef2d0SNikolay Aleksandrov 				    ent->src_query_rexmit_cnt > 0)
874438ef2d0SNikolay Aleksandrov 					llqt_srcs++;
875438ef2d0SNikolay Aleksandrov 			}
876438ef2d0SNikolay Aleksandrov 
877438ef2d0SNikolay Aleksandrov 			if (!llqt_srcs)
878438ef2d0SNikolay Aleksandrov 				return NULL;
879438ef2d0SNikolay Aleksandrov 			mld_hdr_size += llqt_srcs * sizeof(struct in6_addr);
880438ef2d0SNikolay Aleksandrov 		}
881438ef2d0SNikolay Aleksandrov 	}
882438ef2d0SNikolay Aleksandrov 
883438ef2d0SNikolay Aleksandrov 	pkt_size = sizeof(*eth) + sizeof(*ip6h) + 8 + mld_hdr_size;
884438ef2d0SNikolay Aleksandrov 	if ((p && pkt_size > p->dev->mtu) ||
885438ef2d0SNikolay Aleksandrov 	    pkt_size > br->dev->mtu)
886438ef2d0SNikolay Aleksandrov 		return NULL;
887438ef2d0SNikolay Aleksandrov 
888438ef2d0SNikolay Aleksandrov 	skb = netdev_alloc_skb_ip_align(br->dev, pkt_size);
88908b202b6SYOSHIFUJI Hideaki 	if (!skb)
89008b202b6SYOSHIFUJI Hideaki 		goto out;
89108b202b6SYOSHIFUJI Hideaki 
89208b202b6SYOSHIFUJI Hideaki 	skb->protocol = htons(ETH_P_IPV6);
89308b202b6SYOSHIFUJI Hideaki 
89408b202b6SYOSHIFUJI Hideaki 	/* Ethernet header */
89508b202b6SYOSHIFUJI Hideaki 	skb_reset_mac_header(skb);
89608b202b6SYOSHIFUJI Hideaki 	eth = eth_hdr(skb);
89708b202b6SYOSHIFUJI Hideaki 
898e5a727f6SJoe Perches 	ether_addr_copy(eth->h_source, br->dev->dev_addr);
89908b202b6SYOSHIFUJI Hideaki 	eth->h_proto = htons(ETH_P_IPV6);
90008b202b6SYOSHIFUJI Hideaki 	skb_put(skb, sizeof(*eth));
90108b202b6SYOSHIFUJI Hideaki 
90208b202b6SYOSHIFUJI Hideaki 	/* IPv6 header + HbH option */
90308b202b6SYOSHIFUJI Hideaki 	skb_set_network_header(skb, skb->len);
90408b202b6SYOSHIFUJI Hideaki 	ip6h = ipv6_hdr(skb);
90508b202b6SYOSHIFUJI Hideaki 
90608b202b6SYOSHIFUJI Hideaki 	*(__force __be32 *)ip6h = htonl(0x60000000);
907aa2ae3e7SNikolay Aleksandrov 	ip6h->payload_len = htons(8 + mld_hdr_size);
90808b202b6SYOSHIFUJI Hideaki 	ip6h->nexthdr = IPPROTO_HOPOPTS;
90908b202b6SYOSHIFUJI Hideaki 	ip6h->hop_limit = 1;
910438ef2d0SNikolay Aleksandrov 	ip6h->daddr = *ip6_dst;
911d1d81d4cSUlrich Weber 	if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
912d1d81d4cSUlrich Weber 			       &ip6h->saddr)) {
913d1d81d4cSUlrich Weber 		kfree_skb(skb);
914675779adSNikolay Aleksandrov 		br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, false);
915d1d81d4cSUlrich Weber 		return NULL;
916d1d81d4cSUlrich Weber 	}
9170888d5f3Sdaniel 
918675779adSNikolay Aleksandrov 	br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true);
91936cff5a1SLinus Lüssing 	ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
92008b202b6SYOSHIFUJI Hideaki 
92108b202b6SYOSHIFUJI Hideaki 	hopopt = (u8 *)(ip6h + 1);
92208b202b6SYOSHIFUJI Hideaki 	hopopt[0] = IPPROTO_ICMPV6;		/* next hdr */
92308b202b6SYOSHIFUJI Hideaki 	hopopt[1] = 0;				/* length of HbH */
92408b202b6SYOSHIFUJI Hideaki 	hopopt[2] = IPV6_TLV_ROUTERALERT;	/* Router Alert */
92508b202b6SYOSHIFUJI Hideaki 	hopopt[3] = 2;				/* Length of RA Option */
92608b202b6SYOSHIFUJI Hideaki 	hopopt[4] = 0;				/* Type = 0x0000 (MLD) */
92708b202b6SYOSHIFUJI Hideaki 	hopopt[5] = 0;
9281de5a71cSEldad Zack 	hopopt[6] = IPV6_TLV_PAD1;		/* Pad1 */
9291de5a71cSEldad Zack 	hopopt[7] = IPV6_TLV_PAD1;		/* Pad1 */
93008b202b6SYOSHIFUJI Hideaki 
93108b202b6SYOSHIFUJI Hideaki 	skb_put(skb, sizeof(*ip6h) + 8);
93208b202b6SYOSHIFUJI Hideaki 
93308b202b6SYOSHIFUJI Hideaki 	/* ICMPv6 */
93408b202b6SYOSHIFUJI Hideaki 	skb_set_transport_header(skb, skb->len);
935438ef2d0SNikolay Aleksandrov 	interval = ipv6_addr_any(group) ?
93632de868cSLinus Lüssing 			br->multicast_query_response_interval :
93732de868cSLinus Lüssing 			br->multicast_last_member_interval;
9381080ab95SNikolay Aleksandrov 	*igmp_type = ICMPV6_MGM_QUERY;
939aa2ae3e7SNikolay Aleksandrov 	switch (br->multicast_mld_version) {
940aa2ae3e7SNikolay Aleksandrov 	case 1:
941aa2ae3e7SNikolay Aleksandrov 		mldq = (struct mld_msg *)icmp6_hdr(skb);
94208b202b6SYOSHIFUJI Hideaki 		mldq->mld_type = ICMPV6_MGM_QUERY;
94308b202b6SYOSHIFUJI Hideaki 		mldq->mld_code = 0;
94408b202b6SYOSHIFUJI Hideaki 		mldq->mld_cksum = 0;
94508b202b6SYOSHIFUJI Hideaki 		mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
94608b202b6SYOSHIFUJI Hideaki 		mldq->mld_reserved = 0;
947438ef2d0SNikolay Aleksandrov 		mldq->mld_mca = *group;
948438ef2d0SNikolay Aleksandrov 		csum = &mldq->mld_cksum;
949438ef2d0SNikolay Aleksandrov 		csum_start = (void *)mldq;
950aa2ae3e7SNikolay Aleksandrov 		break;
951aa2ae3e7SNikolay Aleksandrov 	case 2:
952aa2ae3e7SNikolay Aleksandrov 		mld2q = (struct mld2_query *)icmp6_hdr(skb);
95353631a5fSLance Richardson 		mld2q->mld2q_mrc = htons((u16)jiffies_to_msecs(interval));
954aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_type = ICMPV6_MGM_QUERY;
955aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_code = 0;
956aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_cksum = 0;
957aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_resv1 = 0;
958aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_resv2 = 0;
959438ef2d0SNikolay Aleksandrov 		mld2q->mld2q_suppress = sflag;
960aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_qrv = 2;
961438ef2d0SNikolay Aleksandrov 		mld2q->mld2q_nsrcs = htons(llqt_srcs);
962aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_qqic = br->multicast_query_interval / HZ;
963438ef2d0SNikolay Aleksandrov 		mld2q->mld2q_mca = *group;
964438ef2d0SNikolay Aleksandrov 		csum = &mld2q->mld2q_cksum;
965438ef2d0SNikolay Aleksandrov 		csum_start = (void *)mld2q;
966438ef2d0SNikolay Aleksandrov 		if (!pg || !with_srcs)
967438ef2d0SNikolay Aleksandrov 			break;
968438ef2d0SNikolay Aleksandrov 
969438ef2d0SNikolay Aleksandrov 		llqt_srcs = 0;
970438ef2d0SNikolay Aleksandrov 		hlist_for_each_entry(ent, &pg->src_list, node) {
971438ef2d0SNikolay Aleksandrov 			if (over_llqt == time_after(ent->timer.expires,
972438ef2d0SNikolay Aleksandrov 						    llqt) &&
973438ef2d0SNikolay Aleksandrov 			    ent->src_query_rexmit_cnt > 0) {
974deb96566SNikolay Aleksandrov 				mld2q->mld2q_srcs[llqt_srcs++] = ent->addr.src.ip6;
975438ef2d0SNikolay Aleksandrov 				ent->src_query_rexmit_cnt--;
97642c11ccfSNikolay Aleksandrov 				if (need_rexmit && ent->src_query_rexmit_cnt)
97742c11ccfSNikolay Aleksandrov 					*need_rexmit = true;
978438ef2d0SNikolay Aleksandrov 			}
979438ef2d0SNikolay Aleksandrov 		}
980438ef2d0SNikolay Aleksandrov 		if (WARN_ON(llqt_srcs != ntohs(mld2q->mld2q_nsrcs))) {
981438ef2d0SNikolay Aleksandrov 			kfree_skb(skb);
982438ef2d0SNikolay Aleksandrov 			return NULL;
983438ef2d0SNikolay Aleksandrov 		}
984aa2ae3e7SNikolay Aleksandrov 		break;
985aa2ae3e7SNikolay Aleksandrov 	}
98608b202b6SYOSHIFUJI Hideaki 
987438ef2d0SNikolay Aleksandrov 	if (WARN_ON(!csum || !csum_start)) {
988438ef2d0SNikolay Aleksandrov 		kfree_skb(skb);
989438ef2d0SNikolay Aleksandrov 		return NULL;
990438ef2d0SNikolay Aleksandrov 	}
991438ef2d0SNikolay Aleksandrov 
992438ef2d0SNikolay Aleksandrov 	*csum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, mld_hdr_size,
993438ef2d0SNikolay Aleksandrov 				IPPROTO_ICMPV6,
994438ef2d0SNikolay Aleksandrov 				csum_partial(csum_start, mld_hdr_size, 0));
995438ef2d0SNikolay Aleksandrov 	skb_put(skb, mld_hdr_size);
99608b202b6SYOSHIFUJI Hideaki 	__skb_pull(skb, sizeof(*eth));
99708b202b6SYOSHIFUJI Hideaki 
99808b202b6SYOSHIFUJI Hideaki out:
99908b202b6SYOSHIFUJI Hideaki 	return skb;
100008b202b6SYOSHIFUJI Hideaki }
100108b202b6SYOSHIFUJI Hideaki #endif
100208b202b6SYOSHIFUJI Hideaki 
10038ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
1004438ef2d0SNikolay Aleksandrov 						struct net_bridge_port_group *pg,
1005438ef2d0SNikolay Aleksandrov 						struct br_ip *ip_dst,
1006438ef2d0SNikolay Aleksandrov 						struct br_ip *group,
1007438ef2d0SNikolay Aleksandrov 						bool with_srcs, bool over_lmqt,
100842c11ccfSNikolay Aleksandrov 						u8 sflag, u8 *igmp_type,
100942c11ccfSNikolay Aleksandrov 						bool *need_rexmit)
10108ef2a9a5SYOSHIFUJI Hideaki {
1011438ef2d0SNikolay Aleksandrov 	__be32 ip4_dst;
1012438ef2d0SNikolay Aleksandrov 
1013438ef2d0SNikolay Aleksandrov 	switch (group->proto) {
10148ef2a9a5SYOSHIFUJI Hideaki 	case htons(ETH_P_IP):
1015eab3227bSNikolay Aleksandrov 		ip4_dst = ip_dst ? ip_dst->dst.ip4 : htonl(INADDR_ALLHOSTS_GROUP);
1016438ef2d0SNikolay Aleksandrov 		return br_ip4_multicast_alloc_query(br, pg,
1017eab3227bSNikolay Aleksandrov 						    ip4_dst, group->dst.ip4,
1018438ef2d0SNikolay Aleksandrov 						    with_srcs, over_lmqt,
101942c11ccfSNikolay Aleksandrov 						    sflag, igmp_type,
102042c11ccfSNikolay Aleksandrov 						    need_rexmit);
1021dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1022438ef2d0SNikolay Aleksandrov 	case htons(ETH_P_IPV6): {
1023438ef2d0SNikolay Aleksandrov 		struct in6_addr ip6_dst;
1024438ef2d0SNikolay Aleksandrov 
1025438ef2d0SNikolay Aleksandrov 		if (ip_dst)
1026eab3227bSNikolay Aleksandrov 			ip6_dst = ip_dst->dst.ip6;
1027438ef2d0SNikolay Aleksandrov 		else
1028438ef2d0SNikolay Aleksandrov 			ipv6_addr_set(&ip6_dst, htonl(0xff020000), 0, 0,
1029438ef2d0SNikolay Aleksandrov 				      htonl(1));
1030438ef2d0SNikolay Aleksandrov 
1031438ef2d0SNikolay Aleksandrov 		return br_ip6_multicast_alloc_query(br, pg,
1032eab3227bSNikolay Aleksandrov 						    &ip6_dst, &group->dst.ip6,
1033438ef2d0SNikolay Aleksandrov 						    with_srcs, over_lmqt,
103442c11ccfSNikolay Aleksandrov 						    sflag, igmp_type,
103542c11ccfSNikolay Aleksandrov 						    need_rexmit);
1036438ef2d0SNikolay Aleksandrov 	}
103708b202b6SYOSHIFUJI Hideaki #endif
10388ef2a9a5SYOSHIFUJI Hideaki 	}
10398ef2a9a5SYOSHIFUJI Hideaki 	return NULL;
10408ef2a9a5SYOSHIFUJI Hideaki }
10418ef2a9a5SYOSHIFUJI Hideaki 
1042cfd56754SCong Wang struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
10435e923585SNikolay Aleksandrov 						    struct br_ip *group)
1044eb1d1641SHerbert Xu {
1045eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
10464c0833bcSTobias Klauser 	int err;
1047eb1d1641SHerbert Xu 
104819e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip_get(br, group);
104919e3a9c9SNikolay Aleksandrov 	if (mp)
105019e3a9c9SNikolay Aleksandrov 		return mp;
1051eb1d1641SHerbert Xu 
105219e3a9c9SNikolay Aleksandrov 	if (atomic_read(&br->mdb_hash_tbl.nelems) >= br->hash_max) {
105319e3a9c9SNikolay Aleksandrov 		br_opt_toggle(br, BROPT_MULTICAST_ENABLED, false);
105419e3a9c9SNikolay Aleksandrov 		return ERR_PTR(-E2BIG);
1055eb1d1641SHerbert Xu 	}
1056eb1d1641SHerbert Xu 
1057eb1d1641SHerbert Xu 	mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
1058eb1d1641SHerbert Xu 	if (unlikely(!mp))
10594c0833bcSTobias Klauser 		return ERR_PTR(-ENOMEM);
1060eb1d1641SHerbert Xu 
1061eb1d1641SHerbert Xu 	mp->br = br;
10628ef2a9a5SYOSHIFUJI Hideaki 	mp->addr = *group;
1063e12cec65SNikolay Aleksandrov 	mp->mcast_gc.destroy = br_multicast_destroy_mdb_entry;
106488c1f37fSAllen Pais 	timer_setup(&mp->timer, br_multicast_group_expired, 0);
106519e3a9c9SNikolay Aleksandrov 	err = rhashtable_lookup_insert_fast(&br->mdb_hash_tbl, &mp->rhnode,
106619e3a9c9SNikolay Aleksandrov 					    br_mdb_rht_params);
106719e3a9c9SNikolay Aleksandrov 	if (err) {
106819e3a9c9SNikolay Aleksandrov 		kfree(mp);
106919e3a9c9SNikolay Aleksandrov 		mp = ERR_PTR(err);
107019e3a9c9SNikolay Aleksandrov 	} else {
107119e3a9c9SNikolay Aleksandrov 		hlist_add_head_rcu(&mp->mdb_node, &br->mdb_list);
107219e3a9c9SNikolay Aleksandrov 	}
10731faabf2aSEric Dumazet 
1074eb1d1641SHerbert Xu 	return mp;
1075eb1d1641SHerbert Xu }
1076eb1d1641SHerbert Xu 
10778b671779SNikolay Aleksandrov static void br_multicast_group_src_expired(struct timer_list *t)
10788b671779SNikolay Aleksandrov {
10798b671779SNikolay Aleksandrov 	struct net_bridge_group_src *src = from_timer(src, t, timer);
10808b671779SNikolay Aleksandrov 	struct net_bridge_port_group *pg;
10818b671779SNikolay Aleksandrov 	struct net_bridge *br = src->br;
10828b671779SNikolay Aleksandrov 
10838b671779SNikolay Aleksandrov 	spin_lock(&br->multicast_lock);
10848b671779SNikolay Aleksandrov 	if (hlist_unhashed(&src->node) || !netif_running(br->dev) ||
10858b671779SNikolay Aleksandrov 	    timer_pending(&src->timer))
10868b671779SNikolay Aleksandrov 		goto out;
10878b671779SNikolay Aleksandrov 
10888b671779SNikolay Aleksandrov 	pg = src->pg;
10898b671779SNikolay Aleksandrov 	if (pg->filter_mode == MCAST_INCLUDE) {
1090d5a10222SNikolay Aleksandrov 		br_multicast_del_group_src(src, false);
10918b671779SNikolay Aleksandrov 		if (!hlist_empty(&pg->src_list))
10928b671779SNikolay Aleksandrov 			goto out;
10938b671779SNikolay Aleksandrov 		br_multicast_find_del_pg(br, pg);
10949116ffbfSNikolay Aleksandrov 	} else {
10959116ffbfSNikolay Aleksandrov 		br_multicast_fwd_src_handle(src);
10968b671779SNikolay Aleksandrov 	}
10979116ffbfSNikolay Aleksandrov 
10988b671779SNikolay Aleksandrov out:
10998b671779SNikolay Aleksandrov 	spin_unlock(&br->multicast_lock);
11008b671779SNikolay Aleksandrov }
11018b671779SNikolay Aleksandrov 
1102474ddb37SNikolay Aleksandrov struct net_bridge_group_src *
11038b671779SNikolay Aleksandrov br_multicast_find_group_src(struct net_bridge_port_group *pg, struct br_ip *ip)
11048b671779SNikolay Aleksandrov {
11058b671779SNikolay Aleksandrov 	struct net_bridge_group_src *ent;
11068b671779SNikolay Aleksandrov 
11078b671779SNikolay Aleksandrov 	switch (ip->proto) {
11088b671779SNikolay Aleksandrov 	case htons(ETH_P_IP):
11098b671779SNikolay Aleksandrov 		hlist_for_each_entry(ent, &pg->src_list, node)
1110deb96566SNikolay Aleksandrov 			if (ip->src.ip4 == ent->addr.src.ip4)
11118b671779SNikolay Aleksandrov 				return ent;
11128b671779SNikolay Aleksandrov 		break;
11138b671779SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
11148b671779SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
11158b671779SNikolay Aleksandrov 		hlist_for_each_entry(ent, &pg->src_list, node)
1116deb96566SNikolay Aleksandrov 			if (!ipv6_addr_cmp(&ent->addr.src.ip6, &ip->src.ip6))
11178b671779SNikolay Aleksandrov 				return ent;
11188b671779SNikolay Aleksandrov 		break;
11198b671779SNikolay Aleksandrov #endif
11208b671779SNikolay Aleksandrov 	}
11218b671779SNikolay Aleksandrov 
11228b671779SNikolay Aleksandrov 	return NULL;
11238b671779SNikolay Aleksandrov }
11248b671779SNikolay Aleksandrov 
11258b671779SNikolay Aleksandrov static struct net_bridge_group_src *
11268b671779SNikolay Aleksandrov br_multicast_new_group_src(struct net_bridge_port_group *pg, struct br_ip *src_ip)
11278b671779SNikolay Aleksandrov {
11288b671779SNikolay Aleksandrov 	struct net_bridge_group_src *grp_src;
11298b671779SNikolay Aleksandrov 
11308b671779SNikolay Aleksandrov 	if (unlikely(pg->src_ents >= PG_SRC_ENT_LIMIT))
11318b671779SNikolay Aleksandrov 		return NULL;
11328b671779SNikolay Aleksandrov 
11338b671779SNikolay Aleksandrov 	switch (src_ip->proto) {
11348b671779SNikolay Aleksandrov 	case htons(ETH_P_IP):
1135deb96566SNikolay Aleksandrov 		if (ipv4_is_zeronet(src_ip->src.ip4) ||
1136deb96566SNikolay Aleksandrov 		    ipv4_is_multicast(src_ip->src.ip4))
11378b671779SNikolay Aleksandrov 			return NULL;
11388b671779SNikolay Aleksandrov 		break;
11398b671779SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
11408b671779SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
1141deb96566SNikolay Aleksandrov 		if (ipv6_addr_any(&src_ip->src.ip6) ||
1142deb96566SNikolay Aleksandrov 		    ipv6_addr_is_multicast(&src_ip->src.ip6))
11438b671779SNikolay Aleksandrov 			return NULL;
11448b671779SNikolay Aleksandrov 		break;
11458b671779SNikolay Aleksandrov #endif
11468b671779SNikolay Aleksandrov 	}
11478b671779SNikolay Aleksandrov 
11488b671779SNikolay Aleksandrov 	grp_src = kzalloc(sizeof(*grp_src), GFP_ATOMIC);
11498b671779SNikolay Aleksandrov 	if (unlikely(!grp_src))
11508b671779SNikolay Aleksandrov 		return NULL;
11518b671779SNikolay Aleksandrov 
11528b671779SNikolay Aleksandrov 	grp_src->pg = pg;
1153085b53c8SNikolay Aleksandrov 	grp_src->br = pg->key.port->br;
11548b671779SNikolay Aleksandrov 	grp_src->addr = *src_ip;
1155e12cec65SNikolay Aleksandrov 	grp_src->mcast_gc.destroy = br_multicast_destroy_group_src;
11568b671779SNikolay Aleksandrov 	timer_setup(&grp_src->timer, br_multicast_group_src_expired, 0);
11578b671779SNikolay Aleksandrov 
11588b671779SNikolay Aleksandrov 	hlist_add_head_rcu(&grp_src->node, &pg->src_list);
11598b671779SNikolay Aleksandrov 	pg->src_ents++;
11608b671779SNikolay Aleksandrov 
11618b671779SNikolay Aleksandrov 	return grp_src;
11628b671779SNikolay Aleksandrov }
11638b671779SNikolay Aleksandrov 
1164cfd56754SCong Wang struct net_bridge_port_group *br_multicast_new_port_group(
1165cfd56754SCong Wang 			struct net_bridge_port *port,
1166cfd56754SCong Wang 			struct br_ip *group,
1167ccb1c31aSAmerigo Wang 			struct net_bridge_port_group __rcu *next,
11686db6f0eaSFelix Fietkau 			unsigned char flags,
11698b671779SNikolay Aleksandrov 			const unsigned char *src,
11708f8cb77eSNikolay Aleksandrov 			u8 filter_mode,
11718f8cb77eSNikolay Aleksandrov 			u8 rt_protocol)
1172cfd56754SCong Wang {
1173cfd56754SCong Wang 	struct net_bridge_port_group *p;
1174cfd56754SCong Wang 
1175cfd56754SCong Wang 	p = kzalloc(sizeof(*p), GFP_ATOMIC);
1176cfd56754SCong Wang 	if (unlikely(!p))
1177cfd56754SCong Wang 		return NULL;
1178cfd56754SCong Wang 
1179085b53c8SNikolay Aleksandrov 	p->key.addr = *group;
1180085b53c8SNikolay Aleksandrov 	p->key.port = port;
11819d06b6d8SElad Raz 	p->flags = flags;
11828b671779SNikolay Aleksandrov 	p->filter_mode = filter_mode;
11838f8cb77eSNikolay Aleksandrov 	p->rt_protocol = rt_protocol;
11845b163288SNikolay Aleksandrov 	p->eht_host_tree = RB_ROOT;
1185baa74d39SNikolay Aleksandrov 	p->eht_set_tree = RB_ROOT;
1186e12cec65SNikolay Aleksandrov 	p->mcast_gc.destroy = br_multicast_destroy_port_group;
11878b671779SNikolay Aleksandrov 	INIT_HLIST_HEAD(&p->src_list);
1188085b53c8SNikolay Aleksandrov 
1189085b53c8SNikolay Aleksandrov 	if (!br_multicast_is_star_g(group) &&
1190085b53c8SNikolay Aleksandrov 	    rhashtable_lookup_insert_fast(&port->br->sg_port_tbl, &p->rhnode,
1191085b53c8SNikolay Aleksandrov 					  br_sg_port_rht_params)) {
1192085b53c8SNikolay Aleksandrov 		kfree(p);
1193085b53c8SNikolay Aleksandrov 		return NULL;
1194085b53c8SNikolay Aleksandrov 	}
1195085b53c8SNikolay Aleksandrov 
1196eca2a43bSstephen hemminger 	rcu_assign_pointer(p->next, next);
119788c1f37fSAllen Pais 	timer_setup(&p->timer, br_multicast_port_group_expired, 0);
119842c11ccfSNikolay Aleksandrov 	timer_setup(&p->rexmit_timer, br_multicast_port_group_rexmit, 0);
119942c11ccfSNikolay Aleksandrov 	hlist_add_head(&p->mglist, &port->mglist);
12006db6f0eaSFelix Fietkau 
12016db6f0eaSFelix Fietkau 	if (src)
12026db6f0eaSFelix Fietkau 		memcpy(p->eth_addr, src, ETH_ALEN);
12036db6f0eaSFelix Fietkau 	else
12041bfe45f4SMao Wenan 		eth_broadcast_addr(p->eth_addr);
12056db6f0eaSFelix Fietkau 
1206cfd56754SCong Wang 	return p;
1207cfd56754SCong Wang }
1208cfd56754SCong Wang 
12091bc844eeSNikolay Aleksandrov void br_multicast_host_join(struct net_bridge_mdb_entry *mp, bool notify)
12101bc844eeSNikolay Aleksandrov {
12111bc844eeSNikolay Aleksandrov 	if (!mp->host_joined) {
12121bc844eeSNikolay Aleksandrov 		mp->host_joined = true;
1213094b82fdSNikolay Aleksandrov 		if (br_multicast_is_star_g(&mp->addr))
1214094b82fdSNikolay Aleksandrov 			br_multicast_star_g_host_state(mp);
12151bc844eeSNikolay Aleksandrov 		if (notify)
121681f19838SNikolay Aleksandrov 			br_mdb_notify(mp->br->dev, mp, NULL, RTM_NEWMDB);
12171bc844eeSNikolay Aleksandrov 	}
1218955062b0SNikolay Aleksandrov 
1219955062b0SNikolay Aleksandrov 	if (br_group_is_l2(&mp->addr))
1220955062b0SNikolay Aleksandrov 		return;
1221955062b0SNikolay Aleksandrov 
12221bc844eeSNikolay Aleksandrov 	mod_timer(&mp->timer, jiffies + mp->br->multicast_membership_interval);
12231bc844eeSNikolay Aleksandrov }
12241bc844eeSNikolay Aleksandrov 
12251bc844eeSNikolay Aleksandrov void br_multicast_host_leave(struct net_bridge_mdb_entry *mp, bool notify)
12261bc844eeSNikolay Aleksandrov {
12271bc844eeSNikolay Aleksandrov 	if (!mp->host_joined)
12281bc844eeSNikolay Aleksandrov 		return;
12291bc844eeSNikolay Aleksandrov 
12301bc844eeSNikolay Aleksandrov 	mp->host_joined = false;
1231094b82fdSNikolay Aleksandrov 	if (br_multicast_is_star_g(&mp->addr))
1232094b82fdSNikolay Aleksandrov 		br_multicast_star_g_host_state(mp);
12331bc844eeSNikolay Aleksandrov 	if (notify)
123481f19838SNikolay Aleksandrov 		br_mdb_notify(mp->br->dev, mp, NULL, RTM_DELMDB);
12351bc844eeSNikolay Aleksandrov }
12361bc844eeSNikolay Aleksandrov 
1237b0812368SNikolay Aleksandrov static struct net_bridge_port_group *
1238b0812368SNikolay Aleksandrov __br_multicast_add_group(struct net_bridge *br,
12398ef2a9a5SYOSHIFUJI Hideaki 			 struct net_bridge_port *port,
12406db6f0eaSFelix Fietkau 			 struct br_ip *group,
12418b671779SNikolay Aleksandrov 			 const unsigned char *src,
12420436862eSNikolay Aleksandrov 			 u8 filter_mode,
12439116ffbfSNikolay Aleksandrov 			 bool igmpv2_mldv1,
12449116ffbfSNikolay Aleksandrov 			 bool blocked)
1245eb1d1641SHerbert Xu {
1246e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
1247b0812368SNikolay Aleksandrov 	struct net_bridge_port_group *p = NULL;
12485e923585SNikolay Aleksandrov 	struct net_bridge_mdb_entry *mp;
1249454594f3SLinus Lüssing 	unsigned long now = jiffies;
1250eb1d1641SHerbert Xu 
1251eb1d1641SHerbert Xu 	if (!netif_running(br->dev) ||
1252eb1d1641SHerbert Xu 	    (port && port->state == BR_STATE_DISABLED))
1253eb1d1641SHerbert Xu 		goto out;
1254eb1d1641SHerbert Xu 
125519e3a9c9SNikolay Aleksandrov 	mp = br_multicast_new_group(br, group);
12564c0833bcSTobias Klauser 	if (IS_ERR(mp))
12571697291dSXu Wang 		return ERR_CAST(mp);
1258eb1d1641SHerbert Xu 
1259eb1d1641SHerbert Xu 	if (!port) {
12601bc844eeSNikolay Aleksandrov 		br_multicast_host_join(mp, true);
1261eb1d1641SHerbert Xu 		goto out;
1262eb1d1641SHerbert Xu 	}
1263eb1d1641SHerbert Xu 
1264e8051688SEric Dumazet 	for (pp = &mp->ports;
1265e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
1266e8051688SEric Dumazet 	     pp = &p->next) {
12676db6f0eaSFelix Fietkau 		if (br_port_group_equal(p, port, src))
1268454594f3SLinus Lüssing 			goto found;
1269085b53c8SNikolay Aleksandrov 		if ((unsigned long)p->key.port < (unsigned long)port)
1270eb1d1641SHerbert Xu 			break;
1271eb1d1641SHerbert Xu 	}
1272eb1d1641SHerbert Xu 
1273955062b0SNikolay Aleksandrov 	p = br_multicast_new_port_group(port, group, *pp, 0, src,
1274955062b0SNikolay Aleksandrov 					filter_mode, RTPROT_KERNEL);
1275b0812368SNikolay Aleksandrov 	if (unlikely(!p)) {
1276b0812368SNikolay Aleksandrov 		p = ERR_PTR(-ENOMEM);
1277b0812368SNikolay Aleksandrov 		goto out;
1278b0812368SNikolay Aleksandrov 	}
1279eb1d1641SHerbert Xu 	rcu_assign_pointer(*pp, p);
12809116ffbfSNikolay Aleksandrov 	if (blocked)
12819116ffbfSNikolay Aleksandrov 		p->flags |= MDB_PG_FLAGS_BLOCKED;
128281f19838SNikolay Aleksandrov 	br_mdb_notify(br->dev, mp, p, RTM_NEWMDB);
1283eb1d1641SHerbert Xu 
1284454594f3SLinus Lüssing found:
12850436862eSNikolay Aleksandrov 	if (igmpv2_mldv1)
1286454594f3SLinus Lüssing 		mod_timer(&p->timer, now + br->multicast_membership_interval);
128781f19838SNikolay Aleksandrov 
1288eb1d1641SHerbert Xu out:
1289b0812368SNikolay Aleksandrov 	return p;
1290b0812368SNikolay Aleksandrov }
1291eb1d1641SHerbert Xu 
1292b0812368SNikolay Aleksandrov static int br_multicast_add_group(struct net_bridge *br,
1293b0812368SNikolay Aleksandrov 				  struct net_bridge_port *port,
1294b0812368SNikolay Aleksandrov 				  struct br_ip *group,
1295b0812368SNikolay Aleksandrov 				  const unsigned char *src,
1296b0812368SNikolay Aleksandrov 				  u8 filter_mode,
1297b0812368SNikolay Aleksandrov 				  bool igmpv2_mldv1)
1298b0812368SNikolay Aleksandrov {
1299b0812368SNikolay Aleksandrov 	struct net_bridge_port_group *pg;
1300b0812368SNikolay Aleksandrov 	int err;
1301b0812368SNikolay Aleksandrov 
1302b0812368SNikolay Aleksandrov 	spin_lock(&br->multicast_lock);
1303b0812368SNikolay Aleksandrov 	pg = __br_multicast_add_group(br, port, group, src, filter_mode,
13049116ffbfSNikolay Aleksandrov 				      igmpv2_mldv1, false);
1305b0812368SNikolay Aleksandrov 	/* NULL is considered valid for host joined groups */
13068d21c882SJiapeng Zhong 	err = PTR_ERR_OR_ZERO(pg);
1307eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1308b0812368SNikolay Aleksandrov 
1309eb1d1641SHerbert Xu 	return err;
1310eb1d1641SHerbert Xu }
1311eb1d1641SHerbert Xu 
13128ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_add_group(struct net_bridge *br,
13138ef2a9a5SYOSHIFUJI Hideaki 				      struct net_bridge_port *port,
1314b0e9a30dSVlad Yasevich 				      __be32 group,
13156db6f0eaSFelix Fietkau 				      __u16 vid,
13168b671779SNikolay Aleksandrov 				      const unsigned char *src,
13178b671779SNikolay Aleksandrov 				      bool igmpv2)
13188ef2a9a5SYOSHIFUJI Hideaki {
13198ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip br_group;
13208b671779SNikolay Aleksandrov 	u8 filter_mode;
13218ef2a9a5SYOSHIFUJI Hideaki 
13228ef2a9a5SYOSHIFUJI Hideaki 	if (ipv4_is_local_multicast(group))
13238ef2a9a5SYOSHIFUJI Hideaki 		return 0;
13248ef2a9a5SYOSHIFUJI Hideaki 
13251515a63fSNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
1326eab3227bSNikolay Aleksandrov 	br_group.dst.ip4 = group;
13278ef2a9a5SYOSHIFUJI Hideaki 	br_group.proto = htons(ETH_P_IP);
1328b0e9a30dSVlad Yasevich 	br_group.vid = vid;
13298b671779SNikolay Aleksandrov 	filter_mode = igmpv2 ? MCAST_EXCLUDE : MCAST_INCLUDE;
13308ef2a9a5SYOSHIFUJI Hideaki 
13310436862eSNikolay Aleksandrov 	return br_multicast_add_group(br, port, &br_group, src, filter_mode,
13320436862eSNikolay Aleksandrov 				      igmpv2);
13338ef2a9a5SYOSHIFUJI Hideaki }
13348ef2a9a5SYOSHIFUJI Hideaki 
1335dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
133608b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_add_group(struct net_bridge *br,
133708b202b6SYOSHIFUJI Hideaki 				      struct net_bridge_port *port,
1338b0e9a30dSVlad Yasevich 				      const struct in6_addr *group,
13396db6f0eaSFelix Fietkau 				      __u16 vid,
13408b671779SNikolay Aleksandrov 				      const unsigned char *src,
13418b671779SNikolay Aleksandrov 				      bool mldv1)
134208b202b6SYOSHIFUJI Hideaki {
134308b202b6SYOSHIFUJI Hideaki 	struct br_ip br_group;
13448b671779SNikolay Aleksandrov 	u8 filter_mode;
134508b202b6SYOSHIFUJI Hideaki 
13463c3769e6SLinus Lüssing 	if (ipv6_addr_is_ll_all_nodes(group))
134708b202b6SYOSHIFUJI Hideaki 		return 0;
134808b202b6SYOSHIFUJI Hideaki 
134919e3a9c9SNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
1350eab3227bSNikolay Aleksandrov 	br_group.dst.ip6 = *group;
13519cc6e0c4SLinus Lüssing 	br_group.proto = htons(ETH_P_IPV6);
1352b0e9a30dSVlad Yasevich 	br_group.vid = vid;
13538b671779SNikolay Aleksandrov 	filter_mode = mldv1 ? MCAST_EXCLUDE : MCAST_INCLUDE;
135408b202b6SYOSHIFUJI Hideaki 
13550436862eSNikolay Aleksandrov 	return br_multicast_add_group(br, port, &br_group, src, filter_mode,
13560436862eSNikolay Aleksandrov 				      mldv1);
135708b202b6SYOSHIFUJI Hideaki }
135808b202b6SYOSHIFUJI Hideaki #endif
135908b202b6SYOSHIFUJI Hideaki 
1360ed2d3597SLinus Lüssing static bool br_multicast_rport_del(struct hlist_node *rlist)
1361ed2d3597SLinus Lüssing {
1362ed2d3597SLinus Lüssing 	if (hlist_unhashed(rlist))
1363ed2d3597SLinus Lüssing 		return false;
1364ed2d3597SLinus Lüssing 
1365ed2d3597SLinus Lüssing 	hlist_del_init_rcu(rlist);
1366ed2d3597SLinus Lüssing 	return true;
1367ed2d3597SLinus Lüssing }
1368ed2d3597SLinus Lüssing 
1369ed2d3597SLinus Lüssing static bool br_ip4_multicast_rport_del(struct net_bridge_port *p)
1370ed2d3597SLinus Lüssing {
1371ed2d3597SLinus Lüssing 	return br_multicast_rport_del(&p->ip4_rlist);
1372ed2d3597SLinus Lüssing }
1373ed2d3597SLinus Lüssing 
1374a3c02e76SLinus Lüssing static bool br_ip6_multicast_rport_del(struct net_bridge_port *p)
1375a3c02e76SLinus Lüssing {
1376a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
1377a3c02e76SLinus Lüssing 	return br_multicast_rport_del(&p->ip6_rlist);
1378a3c02e76SLinus Lüssing #else
1379a3c02e76SLinus Lüssing 	return false;
1380a3c02e76SLinus Lüssing #endif
1381a3c02e76SLinus Lüssing }
1382a3c02e76SLinus Lüssing 
1383ee5fb222SLinus Lüssing static void br_multicast_router_expired(struct net_bridge_port *port,
1384ee5fb222SLinus Lüssing 					struct timer_list *t,
1385ee5fb222SLinus Lüssing 					struct hlist_node *rlist)
1386eb1d1641SHerbert Xu {
1387eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
1388ed2d3597SLinus Lüssing 	bool del;
1389eb1d1641SHerbert Xu 
1390eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
1391a55d8246SNikolay Aleksandrov 	if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
1392a55d8246SNikolay Aleksandrov 	    port->multicast_router == MDB_RTR_TYPE_PERM ||
1393ee5fb222SLinus Lüssing 	    timer_pending(t))
1394eb1d1641SHerbert Xu 		goto out;
1395eb1d1641SHerbert Xu 
1396ed2d3597SLinus Lüssing 	del = br_multicast_rport_del(rlist);
1397ed2d3597SLinus Lüssing 	br_multicast_rport_del_notify(port, del);
1398eb1d1641SHerbert Xu out:
1399eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1400eb1d1641SHerbert Xu }
1401eb1d1641SHerbert Xu 
1402ee5fb222SLinus Lüssing static void br_ip4_multicast_router_expired(struct timer_list *t)
1403ee5fb222SLinus Lüssing {
1404ee5fb222SLinus Lüssing 	struct net_bridge_port *port = from_timer(port, t, ip4_mc_router_timer);
1405ee5fb222SLinus Lüssing 
1406ee5fb222SLinus Lüssing 	br_multicast_router_expired(port, t, &port->ip4_rlist);
1407ee5fb222SLinus Lüssing }
1408ee5fb222SLinus Lüssing 
1409a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
1410a3c02e76SLinus Lüssing static void br_ip6_multicast_router_expired(struct timer_list *t)
1411a3c02e76SLinus Lüssing {
1412a3c02e76SLinus Lüssing 	struct net_bridge_port *port = from_timer(port, t, ip6_mc_router_timer);
1413a3c02e76SLinus Lüssing 
1414a3c02e76SLinus Lüssing 	br_multicast_router_expired(port, t, &port->ip6_rlist);
1415a3c02e76SLinus Lüssing }
1416a3c02e76SLinus Lüssing #endif
1417a3c02e76SLinus Lüssing 
141877041420SYotam Gigi static void br_mc_router_state_change(struct net_bridge *p,
141977041420SYotam Gigi 				      bool is_mc_router)
142077041420SYotam Gigi {
142177041420SYotam Gigi 	struct switchdev_attr attr = {
142277041420SYotam Gigi 		.orig_dev = p->dev,
142377041420SYotam Gigi 		.id = SWITCHDEV_ATTR_ID_BRIDGE_MROUTER,
142477041420SYotam Gigi 		.flags = SWITCHDEV_F_DEFER,
142577041420SYotam Gigi 		.u.mrouter = is_mc_router,
142677041420SYotam Gigi 	};
142777041420SYotam Gigi 
1428dcbdf135SVladimir Oltean 	switchdev_port_attr_set(p->dev, &attr, NULL);
142977041420SYotam Gigi }
143077041420SYotam Gigi 
1431ee5fb222SLinus Lüssing static void br_multicast_local_router_expired(struct net_bridge *br,
1432ee5fb222SLinus Lüssing 					      struct timer_list *timer)
1433eb1d1641SHerbert Xu {
143477041420SYotam Gigi 	spin_lock(&br->multicast_lock);
143577041420SYotam Gigi 	if (br->multicast_router == MDB_RTR_TYPE_DISABLED ||
143677041420SYotam Gigi 	    br->multicast_router == MDB_RTR_TYPE_PERM ||
14371a3065a2SLinus Lüssing 	    br_ip4_multicast_is_router(br) ||
14381a3065a2SLinus Lüssing 	    br_ip6_multicast_is_router(br))
143977041420SYotam Gigi 		goto out;
144077041420SYotam Gigi 
144177041420SYotam Gigi 	br_mc_router_state_change(br, false);
144277041420SYotam Gigi out:
144377041420SYotam Gigi 	spin_unlock(&br->multicast_lock);
1444eb1d1641SHerbert Xu }
1445eb1d1641SHerbert Xu 
1446ee5fb222SLinus Lüssing static void br_ip4_multicast_local_router_expired(struct timer_list *t)
1447ee5fb222SLinus Lüssing {
1448ee5fb222SLinus Lüssing 	struct net_bridge *br = from_timer(br, t, ip4_mc_router_timer);
1449ee5fb222SLinus Lüssing 
1450ee5fb222SLinus Lüssing 	br_multicast_local_router_expired(br, t);
1451ee5fb222SLinus Lüssing }
1452ee5fb222SLinus Lüssing 
1453a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
1454a3c02e76SLinus Lüssing static void br_ip6_multicast_local_router_expired(struct timer_list *t)
1455a3c02e76SLinus Lüssing {
1456a3c02e76SLinus Lüssing 	struct net_bridge *br = from_timer(br, t, ip6_mc_router_timer);
1457a3c02e76SLinus Lüssing 
1458a3c02e76SLinus Lüssing 	br_multicast_local_router_expired(br, t);
1459a3c02e76SLinus Lüssing }
1460a3c02e76SLinus Lüssing #endif
1461a3c02e76SLinus Lüssing 
1462cc0fdd80SLinus Lüssing static void br_multicast_querier_expired(struct net_bridge *br,
146390010b36SLinus Lüssing 					 struct bridge_mcast_own_query *query)
1464c83b8fabSHerbert Xu {
1465c83b8fabSHerbert Xu 	spin_lock(&br->multicast_lock);
146613cefad2SNikolay Aleksandrov 	if (!netif_running(br->dev) || !br_opt_get(br, BROPT_MULTICAST_ENABLED))
1467c83b8fabSHerbert Xu 		goto out;
1468c83b8fabSHerbert Xu 
1469cc0fdd80SLinus Lüssing 	br_multicast_start_querier(br, query);
1470c83b8fabSHerbert Xu 
1471c83b8fabSHerbert Xu out:
1472c83b8fabSHerbert Xu 	spin_unlock(&br->multicast_lock);
1473c83b8fabSHerbert Xu }
1474c83b8fabSHerbert Xu 
147588c1f37fSAllen Pais static void br_ip4_multicast_querier_expired(struct timer_list *t)
1476cc0fdd80SLinus Lüssing {
147788c1f37fSAllen Pais 	struct net_bridge *br = from_timer(br, t, ip4_other_query.timer);
1478cc0fdd80SLinus Lüssing 
147990010b36SLinus Lüssing 	br_multicast_querier_expired(br, &br->ip4_own_query);
1480cc0fdd80SLinus Lüssing }
1481cc0fdd80SLinus Lüssing 
1482cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
148388c1f37fSAllen Pais static void br_ip6_multicast_querier_expired(struct timer_list *t)
1484cc0fdd80SLinus Lüssing {
148588c1f37fSAllen Pais 	struct net_bridge *br = from_timer(br, t, ip6_other_query.timer);
1486cc0fdd80SLinus Lüssing 
148790010b36SLinus Lüssing 	br_multicast_querier_expired(br, &br->ip6_own_query);
1488cc0fdd80SLinus Lüssing }
1489cc0fdd80SLinus Lüssing #endif
1490cc0fdd80SLinus Lüssing 
1491dc4eb53aSLinus Lüssing static void br_multicast_select_own_querier(struct net_bridge *br,
1492dc4eb53aSLinus Lüssing 					    struct br_ip *ip,
1493dc4eb53aSLinus Lüssing 					    struct sk_buff *skb)
1494dc4eb53aSLinus Lüssing {
1495dc4eb53aSLinus Lüssing 	if (ip->proto == htons(ETH_P_IP))
1496deb96566SNikolay Aleksandrov 		br->ip4_querier.addr.src.ip4 = ip_hdr(skb)->saddr;
1497dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
1498dc4eb53aSLinus Lüssing 	else
1499deb96566SNikolay Aleksandrov 		br->ip6_querier.addr.src.ip6 = ipv6_hdr(skb)->saddr;
1500dc4eb53aSLinus Lüssing #endif
1501dc4eb53aSLinus Lüssing }
1502dc4eb53aSLinus Lüssing 
15038ef2a9a5SYOSHIFUJI Hideaki static void __br_multicast_send_query(struct net_bridge *br,
15048ef2a9a5SYOSHIFUJI Hideaki 				      struct net_bridge_port *port,
1505438ef2d0SNikolay Aleksandrov 				      struct net_bridge_port_group *pg,
1506438ef2d0SNikolay Aleksandrov 				      struct br_ip *ip_dst,
1507438ef2d0SNikolay Aleksandrov 				      struct br_ip *group,
1508438ef2d0SNikolay Aleksandrov 				      bool with_srcs,
150942c11ccfSNikolay Aleksandrov 				      u8 sflag,
151042c11ccfSNikolay Aleksandrov 				      bool *need_rexmit)
1511eb1d1641SHerbert Xu {
1512438ef2d0SNikolay Aleksandrov 	bool over_lmqt = !!sflag;
1513eb1d1641SHerbert Xu 	struct sk_buff *skb;
15141080ab95SNikolay Aleksandrov 	u8 igmp_type;
1515eb1d1641SHerbert Xu 
1516438ef2d0SNikolay Aleksandrov again_under_lmqt:
1517438ef2d0SNikolay Aleksandrov 	skb = br_multicast_alloc_query(br, pg, ip_dst, group, with_srcs,
151842c11ccfSNikolay Aleksandrov 				       over_lmqt, sflag, &igmp_type,
151942c11ccfSNikolay Aleksandrov 				       need_rexmit);
1520eb1d1641SHerbert Xu 	if (!skb)
15218ef2a9a5SYOSHIFUJI Hideaki 		return;
1522eb1d1641SHerbert Xu 
1523eb1d1641SHerbert Xu 	if (port) {
1524eb1d1641SHerbert Xu 		skb->dev = port->dev;
1525a65056ecSNikolay Aleksandrov 		br_multicast_count(br, port, skb, igmp_type,
15261080ab95SNikolay Aleksandrov 				   BR_MCAST_DIR_TX);
152729a26a56SEric W. Biederman 		NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
152829a26a56SEric W. Biederman 			dev_net(port->dev), NULL, skb, NULL, skb->dev,
1529f0b4eeceSLinus Lüssing 			br_dev_queue_push_xmit);
1530438ef2d0SNikolay Aleksandrov 
1531438ef2d0SNikolay Aleksandrov 		if (over_lmqt && with_srcs && sflag) {
1532438ef2d0SNikolay Aleksandrov 			over_lmqt = false;
1533438ef2d0SNikolay Aleksandrov 			goto again_under_lmqt;
1534438ef2d0SNikolay Aleksandrov 		}
1535dc4eb53aSLinus Lüssing 	} else {
1536438ef2d0SNikolay Aleksandrov 		br_multicast_select_own_querier(br, group, skb);
1537a65056ecSNikolay Aleksandrov 		br_multicast_count(br, port, skb, igmp_type,
15381080ab95SNikolay Aleksandrov 				   BR_MCAST_DIR_RX);
1539eb1d1641SHerbert Xu 		netif_rx(skb);
15408ef2a9a5SYOSHIFUJI Hideaki 	}
1541dc4eb53aSLinus Lüssing }
1542eb1d1641SHerbert Xu 
15438ef2a9a5SYOSHIFUJI Hideaki static void br_multicast_send_query(struct net_bridge *br,
1544cc0fdd80SLinus Lüssing 				    struct net_bridge_port *port,
154590010b36SLinus Lüssing 				    struct bridge_mcast_own_query *own_query)
15468ef2a9a5SYOSHIFUJI Hideaki {
154790010b36SLinus Lüssing 	struct bridge_mcast_other_query *other_query = NULL;
15485e923585SNikolay Aleksandrov 	struct br_ip br_group;
15495e923585SNikolay Aleksandrov 	unsigned long time;
15508ef2a9a5SYOSHIFUJI Hideaki 
155113cefad2SNikolay Aleksandrov 	if (!netif_running(br->dev) ||
155213cefad2SNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_ENABLED) ||
1553675779adSNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_QUERIER))
15548ef2a9a5SYOSHIFUJI Hideaki 		return;
15558ef2a9a5SYOSHIFUJI Hideaki 
1556eab3227bSNikolay Aleksandrov 	memset(&br_group.dst, 0, sizeof(br_group.dst));
15578ef2a9a5SYOSHIFUJI Hideaki 
155890010b36SLinus Lüssing 	if (port ? (own_query == &port->ip4_own_query) :
155990010b36SLinus Lüssing 		   (own_query == &br->ip4_own_query)) {
156090010b36SLinus Lüssing 		other_query = &br->ip4_other_query;
156108b202b6SYOSHIFUJI Hideaki 		br_group.proto = htons(ETH_P_IP);
1562dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1563cc0fdd80SLinus Lüssing 	} else {
156490010b36SLinus Lüssing 		other_query = &br->ip6_other_query;
156508b202b6SYOSHIFUJI Hideaki 		br_group.proto = htons(ETH_P_IPV6);
156608b202b6SYOSHIFUJI Hideaki #endif
1567eb1d1641SHerbert Xu 	}
1568eb1d1641SHerbert Xu 
156990010b36SLinus Lüssing 	if (!other_query || timer_pending(&other_query->timer))
1570cc0fdd80SLinus Lüssing 		return;
1571cc0fdd80SLinus Lüssing 
157242c11ccfSNikolay Aleksandrov 	__br_multicast_send_query(br, port, NULL, NULL, &br_group, false, 0,
157342c11ccfSNikolay Aleksandrov 				  NULL);
1574cc0fdd80SLinus Lüssing 
1575cc0fdd80SLinus Lüssing 	time = jiffies;
157690010b36SLinus Lüssing 	time += own_query->startup_sent < br->multicast_startup_query_count ?
1577cc0fdd80SLinus Lüssing 		br->multicast_startup_query_interval :
1578cc0fdd80SLinus Lüssing 		br->multicast_query_interval;
157990010b36SLinus Lüssing 	mod_timer(&own_query->timer, time);
1580cc0fdd80SLinus Lüssing }
1581cc0fdd80SLinus Lüssing 
158290010b36SLinus Lüssing static void
158390010b36SLinus Lüssing br_multicast_port_query_expired(struct net_bridge_port *port,
158490010b36SLinus Lüssing 				struct bridge_mcast_own_query *query)
1585eb1d1641SHerbert Xu {
1586eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
1587eb1d1641SHerbert Xu 
1588eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
158902a780c0SDan Carpenter 	if (port->state == BR_STATE_DISABLED ||
159002a780c0SDan Carpenter 	    port->state == BR_STATE_BLOCKING)
1591eb1d1641SHerbert Xu 		goto out;
1592eb1d1641SHerbert Xu 
1593cc0fdd80SLinus Lüssing 	if (query->startup_sent < br->multicast_startup_query_count)
1594cc0fdd80SLinus Lüssing 		query->startup_sent++;
1595eb1d1641SHerbert Xu 
1596cc0fdd80SLinus Lüssing 	br_multicast_send_query(port->br, port, query);
1597eb1d1641SHerbert Xu 
1598eb1d1641SHerbert Xu out:
1599eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1600eb1d1641SHerbert Xu }
1601eb1d1641SHerbert Xu 
160288c1f37fSAllen Pais static void br_ip4_multicast_port_query_expired(struct timer_list *t)
1603cc0fdd80SLinus Lüssing {
160488c1f37fSAllen Pais 	struct net_bridge_port *port = from_timer(port, t, ip4_own_query.timer);
1605cc0fdd80SLinus Lüssing 
160690010b36SLinus Lüssing 	br_multicast_port_query_expired(port, &port->ip4_own_query);
1607cc0fdd80SLinus Lüssing }
1608cc0fdd80SLinus Lüssing 
1609cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
161088c1f37fSAllen Pais static void br_ip6_multicast_port_query_expired(struct timer_list *t)
1611cc0fdd80SLinus Lüssing {
161288c1f37fSAllen Pais 	struct net_bridge_port *port = from_timer(port, t, ip6_own_query.timer);
1613cc0fdd80SLinus Lüssing 
161490010b36SLinus Lüssing 	br_multicast_port_query_expired(port, &port->ip6_own_query);
1615cc0fdd80SLinus Lüssing }
1616cc0fdd80SLinus Lüssing #endif
1617cc0fdd80SLinus Lüssing 
161842c11ccfSNikolay Aleksandrov static void br_multicast_port_group_rexmit(struct timer_list *t)
161942c11ccfSNikolay Aleksandrov {
162042c11ccfSNikolay Aleksandrov 	struct net_bridge_port_group *pg = from_timer(pg, t, rexmit_timer);
162142c11ccfSNikolay Aleksandrov 	struct bridge_mcast_other_query *other_query = NULL;
1622085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
162342c11ccfSNikolay Aleksandrov 	bool need_rexmit = false;
162442c11ccfSNikolay Aleksandrov 
162542c11ccfSNikolay Aleksandrov 	spin_lock(&br->multicast_lock);
162642c11ccfSNikolay Aleksandrov 	if (!netif_running(br->dev) || hlist_unhashed(&pg->mglist) ||
162742c11ccfSNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_ENABLED) ||
162842c11ccfSNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_QUERIER))
162942c11ccfSNikolay Aleksandrov 		goto out;
163042c11ccfSNikolay Aleksandrov 
1631085b53c8SNikolay Aleksandrov 	if (pg->key.addr.proto == htons(ETH_P_IP))
163242c11ccfSNikolay Aleksandrov 		other_query = &br->ip4_other_query;
163342c11ccfSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
163442c11ccfSNikolay Aleksandrov 	else
163542c11ccfSNikolay Aleksandrov 		other_query = &br->ip6_other_query;
163642c11ccfSNikolay Aleksandrov #endif
163742c11ccfSNikolay Aleksandrov 
163842c11ccfSNikolay Aleksandrov 	if (!other_query || timer_pending(&other_query->timer))
163942c11ccfSNikolay Aleksandrov 		goto out;
164042c11ccfSNikolay Aleksandrov 
164142c11ccfSNikolay Aleksandrov 	if (pg->grp_query_rexmit_cnt) {
164242c11ccfSNikolay Aleksandrov 		pg->grp_query_rexmit_cnt--;
1643085b53c8SNikolay Aleksandrov 		__br_multicast_send_query(br, pg->key.port, pg, &pg->key.addr,
1644085b53c8SNikolay Aleksandrov 					  &pg->key.addr, false, 1, NULL);
164542c11ccfSNikolay Aleksandrov 	}
1646085b53c8SNikolay Aleksandrov 	__br_multicast_send_query(br, pg->key.port, pg, &pg->key.addr,
1647085b53c8SNikolay Aleksandrov 				  &pg->key.addr, true, 0, &need_rexmit);
164842c11ccfSNikolay Aleksandrov 
164942c11ccfSNikolay Aleksandrov 	if (pg->grp_query_rexmit_cnt || need_rexmit)
165042c11ccfSNikolay Aleksandrov 		mod_timer(&pg->rexmit_timer, jiffies +
165142c11ccfSNikolay Aleksandrov 					     br->multicast_last_member_interval);
165242c11ccfSNikolay Aleksandrov out:
165342c11ccfSNikolay Aleksandrov 	spin_unlock(&br->multicast_lock);
165442c11ccfSNikolay Aleksandrov }
165542c11ccfSNikolay Aleksandrov 
1656ae1ea84bSFlorian Fainelli static int br_mc_disabled_update(struct net_device *dev, bool value,
1657ae1ea84bSFlorian Fainelli 				 struct netlink_ext_ack *extack)
1658147c1e9bSNogah Frankel {
1659147c1e9bSNogah Frankel 	struct switchdev_attr attr = {
1660147c1e9bSNogah Frankel 		.orig_dev = dev,
1661147c1e9bSNogah Frankel 		.id = SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED,
1662147c1e9bSNogah Frankel 		.flags = SWITCHDEV_F_DEFER,
166313cefad2SNikolay Aleksandrov 		.u.mc_disabled = !value,
1664147c1e9bSNogah Frankel 	};
1665147c1e9bSNogah Frankel 
1666ae1ea84bSFlorian Fainelli 	return switchdev_port_attr_set(dev, &attr, extack);
1667147c1e9bSNogah Frankel }
1668147c1e9bSNogah Frankel 
16691080ab95SNikolay Aleksandrov int br_multicast_add_port(struct net_bridge_port *port)
1670eb1d1641SHerbert Xu {
1671ae1ea84bSFlorian Fainelli 	int err;
1672ae1ea84bSFlorian Fainelli 
16737f0aec7aSNikolay Aleksandrov 	port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
167489268b05SNikolay Aleksandrov 	port->multicast_eht_hosts_limit = BR_MCAST_DEFAULT_EHT_HOSTS_LIMIT;
1675eb1d1641SHerbert Xu 
1676ce6f7097SLinus Lüssing 	timer_setup(&port->ip4_mc_router_timer,
1677ee5fb222SLinus Lüssing 		    br_ip4_multicast_router_expired, 0);
167888c1f37fSAllen Pais 	timer_setup(&port->ip4_own_query.timer,
167988c1f37fSAllen Pais 		    br_ip4_multicast_port_query_expired, 0);
1680cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
1681a3c02e76SLinus Lüssing 	timer_setup(&port->ip6_mc_router_timer,
1682a3c02e76SLinus Lüssing 		    br_ip6_multicast_router_expired, 0);
168388c1f37fSAllen Pais 	timer_setup(&port->ip6_own_query.timer,
168488c1f37fSAllen Pais 		    br_ip6_multicast_port_query_expired, 0);
1685cc0fdd80SLinus Lüssing #endif
1686ae1ea84bSFlorian Fainelli 	err = br_mc_disabled_update(port->dev,
1687ae1ea84bSFlorian Fainelli 				    br_opt_get(port->br,
1688ae1ea84bSFlorian Fainelli 					       BROPT_MULTICAST_ENABLED),
1689ae1ea84bSFlorian Fainelli 				    NULL);
169068f5c12aSVladimir Oltean 	if (err && err != -EOPNOTSUPP)
1691ae1ea84bSFlorian Fainelli 		return err;
1692147c1e9bSNogah Frankel 
16931080ab95SNikolay Aleksandrov 	port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
16941080ab95SNikolay Aleksandrov 	if (!port->mcast_stats)
16951080ab95SNikolay Aleksandrov 		return -ENOMEM;
16961080ab95SNikolay Aleksandrov 
16971080ab95SNikolay Aleksandrov 	return 0;
1698eb1d1641SHerbert Xu }
1699eb1d1641SHerbert Xu 
1700eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port)
1701eb1d1641SHerbert Xu {
1702e10177abSSatish Ashok 	struct net_bridge *br = port->br;
1703e10177abSSatish Ashok 	struct net_bridge_port_group *pg;
1704e12cec65SNikolay Aleksandrov 	HLIST_HEAD(deleted_head);
1705e10177abSSatish Ashok 	struct hlist_node *n;
1706e10177abSSatish Ashok 
1707e10177abSSatish Ashok 	/* Take care of the remaining groups, only perm ones should be left */
1708e10177abSSatish Ashok 	spin_lock_bh(&br->multicast_lock);
1709e10177abSSatish Ashok 	hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
1710681590bdSNikolay Aleksandrov 		br_multicast_find_del_pg(br, pg);
1711e12cec65SNikolay Aleksandrov 	hlist_move_list(&br->mcast_gc_list, &deleted_head);
1712e10177abSSatish Ashok 	spin_unlock_bh(&br->multicast_lock);
1713e12cec65SNikolay Aleksandrov 	br_multicast_gc(&deleted_head);
1714ce6f7097SLinus Lüssing 	del_timer_sync(&port->ip4_mc_router_timer);
1715a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
1716a3c02e76SLinus Lüssing 	del_timer_sync(&port->ip6_mc_router_timer);
1717a3c02e76SLinus Lüssing #endif
17181080ab95SNikolay Aleksandrov 	free_percpu(port->mcast_stats);
1719eb1d1641SHerbert Xu }
1720eb1d1641SHerbert Xu 
172190010b36SLinus Lüssing static void br_multicast_enable(struct bridge_mcast_own_query *query)
1722561f1103SHerbert Xu {
1723cc0fdd80SLinus Lüssing 	query->startup_sent = 0;
1724561f1103SHerbert Xu 
1725cc0fdd80SLinus Lüssing 	if (try_to_del_timer_sync(&query->timer) >= 0 ||
1726cc0fdd80SLinus Lüssing 	    del_timer(&query->timer))
1727cc0fdd80SLinus Lüssing 		mod_timer(&query->timer, jiffies);
1728561f1103SHerbert Xu }
1729561f1103SHerbert Xu 
17307cb3f921SNikolay Aleksandrov static void __br_multicast_enable_port(struct net_bridge_port *port)
1731eb1d1641SHerbert Xu {
1732eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
1733eb1d1641SHerbert Xu 
173413cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED) || !netif_running(br->dev))
17357cb3f921SNikolay Aleksandrov 		return;
1736eb1d1641SHerbert Xu 
173790010b36SLinus Lüssing 	br_multicast_enable(&port->ip4_own_query);
1738cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
173990010b36SLinus Lüssing 	br_multicast_enable(&port->ip6_own_query);
1740cc0fdd80SLinus Lüssing #endif
1741a3c02e76SLinus Lüssing 	if (port->multicast_router == MDB_RTR_TYPE_PERM) {
1742d9b8c4d8SLinus Lüssing 		br_ip4_multicast_add_router(br, port);
1743a3c02e76SLinus Lüssing 		br_ip6_multicast_add_router(br, port);
1744a3c02e76SLinus Lüssing 	}
17457cb3f921SNikolay Aleksandrov }
1746eb1d1641SHerbert Xu 
17477cb3f921SNikolay Aleksandrov void br_multicast_enable_port(struct net_bridge_port *port)
17487cb3f921SNikolay Aleksandrov {
17497cb3f921SNikolay Aleksandrov 	struct net_bridge *br = port->br;
17507cb3f921SNikolay Aleksandrov 
17517cb3f921SNikolay Aleksandrov 	spin_lock(&br->multicast_lock);
17527cb3f921SNikolay Aleksandrov 	__br_multicast_enable_port(port);
1753eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1754eb1d1641SHerbert Xu }
1755eb1d1641SHerbert Xu 
1756eb1d1641SHerbert Xu void br_multicast_disable_port(struct net_bridge_port *port)
1757eb1d1641SHerbert Xu {
1758eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
1759eb1d1641SHerbert Xu 	struct net_bridge_port_group *pg;
1760b67bfe0dSSasha Levin 	struct hlist_node *n;
1761ed2d3597SLinus Lüssing 	bool del = false;
1762eb1d1641SHerbert Xu 
1763eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
1764b67bfe0dSSasha Levin 	hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
17659d06b6d8SElad Raz 		if (!(pg->flags & MDB_PG_FLAGS_PERMANENT))
1766681590bdSNikolay Aleksandrov 			br_multicast_find_del_pg(br, pg);
1767eb1d1641SHerbert Xu 
1768ed2d3597SLinus Lüssing 	del |= br_ip4_multicast_rport_del(port);
1769ce6f7097SLinus Lüssing 	del_timer(&port->ip4_mc_router_timer);
177090010b36SLinus Lüssing 	del_timer(&port->ip4_own_query.timer);
1771a3c02e76SLinus Lüssing 	del |= br_ip6_multicast_rport_del(port);
1772cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
1773a3c02e76SLinus Lüssing 	del_timer(&port->ip6_mc_router_timer);
177490010b36SLinus Lüssing 	del_timer(&port->ip6_own_query.timer);
1775cc0fdd80SLinus Lüssing #endif
1776ed2d3597SLinus Lüssing 	br_multicast_rport_del_notify(port, del);
1777eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1778eb1d1641SHerbert Xu }
1779eb1d1641SHerbert Xu 
1780e6231bcaSNikolay Aleksandrov static int __grp_src_delete_marked(struct net_bridge_port_group *pg)
1781e6231bcaSNikolay Aleksandrov {
1782e6231bcaSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
1783e6231bcaSNikolay Aleksandrov 	struct hlist_node *tmp;
1784e6231bcaSNikolay Aleksandrov 	int deleted = 0;
1785e6231bcaSNikolay Aleksandrov 
1786e6231bcaSNikolay Aleksandrov 	hlist_for_each_entry_safe(ent, tmp, &pg->src_list, node)
1787e6231bcaSNikolay Aleksandrov 		if (ent->flags & BR_SGRP_F_DELETE) {
1788d5a10222SNikolay Aleksandrov 			br_multicast_del_group_src(ent, false);
1789e6231bcaSNikolay Aleksandrov 			deleted++;
1790e6231bcaSNikolay Aleksandrov 		}
1791e6231bcaSNikolay Aleksandrov 
1792e6231bcaSNikolay Aleksandrov 	return deleted;
1793e6231bcaSNikolay Aleksandrov }
1794e6231bcaSNikolay Aleksandrov 
1795b0812368SNikolay Aleksandrov static void __grp_src_mod_timer(struct net_bridge_group_src *src,
1796b0812368SNikolay Aleksandrov 				unsigned long expires)
1797b0812368SNikolay Aleksandrov {
1798b0812368SNikolay Aleksandrov 	mod_timer(&src->timer, expires);
1799b0812368SNikolay Aleksandrov 	br_multicast_fwd_src_handle(src);
1800b0812368SNikolay Aleksandrov }
1801b0812368SNikolay Aleksandrov 
18025bf1e00bSNikolay Aleksandrov static void __grp_src_query_marked_and_rexmit(struct net_bridge_port_group *pg)
18035bf1e00bSNikolay Aleksandrov {
18045bf1e00bSNikolay Aleksandrov 	struct bridge_mcast_other_query *other_query = NULL;
1805085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
18065bf1e00bSNikolay Aleksandrov 	u32 lmqc = br->multicast_last_member_count;
18075bf1e00bSNikolay Aleksandrov 	unsigned long lmqt, lmi, now = jiffies;
18085bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
18095bf1e00bSNikolay Aleksandrov 
18105bf1e00bSNikolay Aleksandrov 	if (!netif_running(br->dev) ||
18115bf1e00bSNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_ENABLED))
18125bf1e00bSNikolay Aleksandrov 		return;
18135bf1e00bSNikolay Aleksandrov 
1814085b53c8SNikolay Aleksandrov 	if (pg->key.addr.proto == htons(ETH_P_IP))
18155bf1e00bSNikolay Aleksandrov 		other_query = &br->ip4_other_query;
18165bf1e00bSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
18175bf1e00bSNikolay Aleksandrov 	else
18185bf1e00bSNikolay Aleksandrov 		other_query = &br->ip6_other_query;
18195bf1e00bSNikolay Aleksandrov #endif
18205bf1e00bSNikolay Aleksandrov 
18215bf1e00bSNikolay Aleksandrov 	lmqt = now + br_multicast_lmqt(br);
18225bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node) {
18235bf1e00bSNikolay Aleksandrov 		if (ent->flags & BR_SGRP_F_SEND) {
18245bf1e00bSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_SEND;
18255bf1e00bSNikolay Aleksandrov 			if (ent->timer.expires > lmqt) {
18265bf1e00bSNikolay Aleksandrov 				if (br_opt_get(br, BROPT_MULTICAST_QUERIER) &&
18275bf1e00bSNikolay Aleksandrov 				    other_query &&
18285bf1e00bSNikolay Aleksandrov 				    !timer_pending(&other_query->timer))
18295bf1e00bSNikolay Aleksandrov 					ent->src_query_rexmit_cnt = lmqc;
1830b0812368SNikolay Aleksandrov 				__grp_src_mod_timer(ent, lmqt);
18315bf1e00bSNikolay Aleksandrov 			}
18325bf1e00bSNikolay Aleksandrov 		}
18335bf1e00bSNikolay Aleksandrov 	}
18345bf1e00bSNikolay Aleksandrov 
18355bf1e00bSNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_QUERIER) ||
18365bf1e00bSNikolay Aleksandrov 	    !other_query || timer_pending(&other_query->timer))
18375bf1e00bSNikolay Aleksandrov 		return;
18385bf1e00bSNikolay Aleksandrov 
1839085b53c8SNikolay Aleksandrov 	__br_multicast_send_query(br, pg->key.port, pg, &pg->key.addr,
1840085b53c8SNikolay Aleksandrov 				  &pg->key.addr, true, 1, NULL);
18415bf1e00bSNikolay Aleksandrov 
18425bf1e00bSNikolay Aleksandrov 	lmi = now + br->multicast_last_member_interval;
18435bf1e00bSNikolay Aleksandrov 	if (!timer_pending(&pg->rexmit_timer) ||
18445bf1e00bSNikolay Aleksandrov 	    time_after(pg->rexmit_timer.expires, lmi))
18455bf1e00bSNikolay Aleksandrov 		mod_timer(&pg->rexmit_timer, lmi);
18465bf1e00bSNikolay Aleksandrov }
18475bf1e00bSNikolay Aleksandrov 
18485bf1e00bSNikolay Aleksandrov static void __grp_send_query_and_rexmit(struct net_bridge_port_group *pg)
18495bf1e00bSNikolay Aleksandrov {
18505bf1e00bSNikolay Aleksandrov 	struct bridge_mcast_other_query *other_query = NULL;
1851085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
18525bf1e00bSNikolay Aleksandrov 	unsigned long now = jiffies, lmi;
18535bf1e00bSNikolay Aleksandrov 
18545bf1e00bSNikolay Aleksandrov 	if (!netif_running(br->dev) ||
18555bf1e00bSNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_ENABLED))
18565bf1e00bSNikolay Aleksandrov 		return;
18575bf1e00bSNikolay Aleksandrov 
1858085b53c8SNikolay Aleksandrov 	if (pg->key.addr.proto == htons(ETH_P_IP))
18595bf1e00bSNikolay Aleksandrov 		other_query = &br->ip4_other_query;
18605bf1e00bSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
18615bf1e00bSNikolay Aleksandrov 	else
18625bf1e00bSNikolay Aleksandrov 		other_query = &br->ip6_other_query;
18635bf1e00bSNikolay Aleksandrov #endif
18645bf1e00bSNikolay Aleksandrov 
18655bf1e00bSNikolay Aleksandrov 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER) &&
18665bf1e00bSNikolay Aleksandrov 	    other_query && !timer_pending(&other_query->timer)) {
18675bf1e00bSNikolay Aleksandrov 		lmi = now + br->multicast_last_member_interval;
18685bf1e00bSNikolay Aleksandrov 		pg->grp_query_rexmit_cnt = br->multicast_last_member_count - 1;
1869085b53c8SNikolay Aleksandrov 		__br_multicast_send_query(br, pg->key.port, pg, &pg->key.addr,
1870085b53c8SNikolay Aleksandrov 					  &pg->key.addr, false, 0, NULL);
18715bf1e00bSNikolay Aleksandrov 		if (!timer_pending(&pg->rexmit_timer) ||
18725bf1e00bSNikolay Aleksandrov 		    time_after(pg->rexmit_timer.expires, lmi))
18735bf1e00bSNikolay Aleksandrov 			mod_timer(&pg->rexmit_timer, lmi);
18745bf1e00bSNikolay Aleksandrov 	}
18755bf1e00bSNikolay Aleksandrov 
18765bf1e00bSNikolay Aleksandrov 	if (pg->filter_mode == MCAST_EXCLUDE &&
18775bf1e00bSNikolay Aleksandrov 	    (!timer_pending(&pg->timer) ||
18785bf1e00bSNikolay Aleksandrov 	     time_after(pg->timer.expires, now + br_multicast_lmqt(br))))
18795bf1e00bSNikolay Aleksandrov 		mod_timer(&pg->timer, now + br_multicast_lmqt(br));
18805bf1e00bSNikolay Aleksandrov }
18815bf1e00bSNikolay Aleksandrov 
18820436862eSNikolay Aleksandrov /* State          Msg type      New state                Actions
18830436862eSNikolay Aleksandrov  * INCLUDE (A)    IS_IN (B)     INCLUDE (A+B)            (B)=GMI
18840436862eSNikolay Aleksandrov  * INCLUDE (A)    ALLOW (B)     INCLUDE (A+B)            (B)=GMI
18850436862eSNikolay Aleksandrov  * EXCLUDE (X,Y)  ALLOW (A)     EXCLUDE (X+A,Y-A)        (A)=GMI
18860436862eSNikolay Aleksandrov  */
188754bea721SNikolay Aleksandrov static bool br_multicast_isinc_allow(struct net_bridge_port_group *pg, void *h_addr,
1888474ddb37SNikolay Aleksandrov 				     void *srcs, u32 nsrcs, size_t addr_size,
1889474ddb37SNikolay Aleksandrov 				     int grec_type)
18900436862eSNikolay Aleksandrov {
1891085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
18920436862eSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
18930436862eSNikolay Aleksandrov 	unsigned long now = jiffies;
18940436862eSNikolay Aleksandrov 	bool changed = false;
18950436862eSNikolay Aleksandrov 	struct br_ip src_ip;
18960436862eSNikolay Aleksandrov 	u32 src_idx;
18970436862eSNikolay Aleksandrov 
18980436862eSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
1899085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
19000436862eSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
1901e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
19020436862eSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
19030436862eSNikolay Aleksandrov 		if (!ent) {
19040436862eSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
19050436862eSNikolay Aleksandrov 			if (ent)
19060436862eSNikolay Aleksandrov 				changed = true;
19070436862eSNikolay Aleksandrov 		}
19080436862eSNikolay Aleksandrov 
19090436862eSNikolay Aleksandrov 		if (ent)
1910b0812368SNikolay Aleksandrov 			__grp_src_mod_timer(ent, now + br_multicast_gmi(br));
19110436862eSNikolay Aleksandrov 	}
19120436862eSNikolay Aleksandrov 
1913474ddb37SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
1914474ddb37SNikolay Aleksandrov 		changed = true;
1915474ddb37SNikolay Aleksandrov 
19160436862eSNikolay Aleksandrov 	return changed;
19170436862eSNikolay Aleksandrov }
19180436862eSNikolay Aleksandrov 
1919e6231bcaSNikolay Aleksandrov /* State          Msg type      New state                Actions
1920e6231bcaSNikolay Aleksandrov  * INCLUDE (A)    IS_EX (B)     EXCLUDE (A*B,B-A)        (B-A)=0
1921e6231bcaSNikolay Aleksandrov  *                                                       Delete (A-B)
1922e6231bcaSNikolay Aleksandrov  *                                                       Group Timer=GMI
1923e6231bcaSNikolay Aleksandrov  */
192454bea721SNikolay Aleksandrov static void __grp_src_isexc_incl(struct net_bridge_port_group *pg, void *h_addr,
1925ddc255d9SNikolay Aleksandrov 				 void *srcs, u32 nsrcs, size_t addr_size,
1926ddc255d9SNikolay Aleksandrov 				 int grec_type)
1927e6231bcaSNikolay Aleksandrov {
1928e6231bcaSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
1929e6231bcaSNikolay Aleksandrov 	struct br_ip src_ip;
1930e6231bcaSNikolay Aleksandrov 	u32 src_idx;
1931e6231bcaSNikolay Aleksandrov 
1932e6231bcaSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
1933e6231bcaSNikolay Aleksandrov 		ent->flags |= BR_SGRP_F_DELETE;
1934e6231bcaSNikolay Aleksandrov 
1935e6231bcaSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
1936085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
1937e6231bcaSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
1938e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
1939e6231bcaSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
1940e6231bcaSNikolay Aleksandrov 		if (ent)
1941e6231bcaSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_DELETE;
1942e6231bcaSNikolay Aleksandrov 		else
1943b0812368SNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
1944b0812368SNikolay Aleksandrov 		if (ent)
1945b0812368SNikolay Aleksandrov 			br_multicast_fwd_src_handle(ent);
1946e6231bcaSNikolay Aleksandrov 	}
1947e6231bcaSNikolay Aleksandrov 
1948ddc255d9SNikolay Aleksandrov 	br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type);
1949ddc255d9SNikolay Aleksandrov 
1950e6231bcaSNikolay Aleksandrov 	__grp_src_delete_marked(pg);
1951e6231bcaSNikolay Aleksandrov }
1952e6231bcaSNikolay Aleksandrov 
1953e6231bcaSNikolay Aleksandrov /* State          Msg type      New state                Actions
1954e6231bcaSNikolay Aleksandrov  * EXCLUDE (X,Y)  IS_EX (A)     EXCLUDE (A-Y,Y*A)        (A-X-Y)=GMI
1955e6231bcaSNikolay Aleksandrov  *                                                       Delete (X-A)
1956e6231bcaSNikolay Aleksandrov  *                                                       Delete (Y-A)
1957e6231bcaSNikolay Aleksandrov  *                                                       Group Timer=GMI
1958e6231bcaSNikolay Aleksandrov  */
195954bea721SNikolay Aleksandrov static bool __grp_src_isexc_excl(struct net_bridge_port_group *pg, void *h_addr,
1960ddc255d9SNikolay Aleksandrov 				 void *srcs, u32 nsrcs, size_t addr_size,
1961ddc255d9SNikolay Aleksandrov 				 int grec_type)
1962e6231bcaSNikolay Aleksandrov {
1963085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
1964e6231bcaSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
1965e6231bcaSNikolay Aleksandrov 	unsigned long now = jiffies;
1966e6231bcaSNikolay Aleksandrov 	bool changed = false;
1967e6231bcaSNikolay Aleksandrov 	struct br_ip src_ip;
1968e6231bcaSNikolay Aleksandrov 	u32 src_idx;
1969e6231bcaSNikolay Aleksandrov 
1970e6231bcaSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
1971e6231bcaSNikolay Aleksandrov 		ent->flags |= BR_SGRP_F_DELETE;
1972e6231bcaSNikolay Aleksandrov 
1973e6231bcaSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
1974085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
1975e6231bcaSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
1976e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
1977e6231bcaSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
1978e6231bcaSNikolay Aleksandrov 		if (ent) {
1979e6231bcaSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_DELETE;
1980e6231bcaSNikolay Aleksandrov 		} else {
1981e6231bcaSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
1982e6231bcaSNikolay Aleksandrov 			if (ent) {
1983b0812368SNikolay Aleksandrov 				__grp_src_mod_timer(ent,
1984e6231bcaSNikolay Aleksandrov 						    now + br_multicast_gmi(br));
1985e6231bcaSNikolay Aleksandrov 				changed = true;
1986e6231bcaSNikolay Aleksandrov 			}
1987e6231bcaSNikolay Aleksandrov 		}
1988e6231bcaSNikolay Aleksandrov 	}
1989e6231bcaSNikolay Aleksandrov 
1990ddc255d9SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
1991ddc255d9SNikolay Aleksandrov 		changed = true;
1992ddc255d9SNikolay Aleksandrov 
1993e6231bcaSNikolay Aleksandrov 	if (__grp_src_delete_marked(pg))
1994e6231bcaSNikolay Aleksandrov 		changed = true;
1995e6231bcaSNikolay Aleksandrov 
1996e6231bcaSNikolay Aleksandrov 	return changed;
1997e6231bcaSNikolay Aleksandrov }
1998e6231bcaSNikolay Aleksandrov 
199954bea721SNikolay Aleksandrov static bool br_multicast_isexc(struct net_bridge_port_group *pg, void *h_addr,
2000ddc255d9SNikolay Aleksandrov 			       void *srcs, u32 nsrcs, size_t addr_size,
2001ddc255d9SNikolay Aleksandrov 			       int grec_type)
2002e6231bcaSNikolay Aleksandrov {
2003085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
2004e6231bcaSNikolay Aleksandrov 	bool changed = false;
2005e6231bcaSNikolay Aleksandrov 
2006e6231bcaSNikolay Aleksandrov 	switch (pg->filter_mode) {
2007e6231bcaSNikolay Aleksandrov 	case MCAST_INCLUDE:
2008ddc255d9SNikolay Aleksandrov 		__grp_src_isexc_incl(pg, h_addr, srcs, nsrcs, addr_size,
2009ddc255d9SNikolay Aleksandrov 				     grec_type);
20108266a049SNikolay Aleksandrov 		br_multicast_star_g_handle_mode(pg, MCAST_EXCLUDE);
2011e6231bcaSNikolay Aleksandrov 		changed = true;
2012e6231bcaSNikolay Aleksandrov 		break;
2013e6231bcaSNikolay Aleksandrov 	case MCAST_EXCLUDE:
2014ddc255d9SNikolay Aleksandrov 		changed = __grp_src_isexc_excl(pg, h_addr, srcs, nsrcs, addr_size,
2015ddc255d9SNikolay Aleksandrov 					       grec_type);
2016e6231bcaSNikolay Aleksandrov 		break;
2017e6231bcaSNikolay Aleksandrov 	}
2018e6231bcaSNikolay Aleksandrov 
2019e6231bcaSNikolay Aleksandrov 	pg->filter_mode = MCAST_EXCLUDE;
2020e6231bcaSNikolay Aleksandrov 	mod_timer(&pg->timer, jiffies + br_multicast_gmi(br));
2021e6231bcaSNikolay Aleksandrov 
2022e6231bcaSNikolay Aleksandrov 	return changed;
2023e6231bcaSNikolay Aleksandrov }
2024e6231bcaSNikolay Aleksandrov 
20255bf1e00bSNikolay Aleksandrov /* State          Msg type      New state                Actions
20265bf1e00bSNikolay Aleksandrov  * INCLUDE (A)    TO_IN (B)     INCLUDE (A+B)            (B)=GMI
20275bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G,A-B)
20285bf1e00bSNikolay Aleksandrov  */
202954bea721SNikolay Aleksandrov static bool __grp_src_toin_incl(struct net_bridge_port_group *pg, void *h_addr,
2030ddc255d9SNikolay Aleksandrov 				void *srcs, u32 nsrcs, size_t addr_size,
2031ddc255d9SNikolay Aleksandrov 				int grec_type)
20325bf1e00bSNikolay Aleksandrov {
2033085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
20345bf1e00bSNikolay Aleksandrov 	u32 src_idx, to_send = pg->src_ents;
20355bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
20365bf1e00bSNikolay Aleksandrov 	unsigned long now = jiffies;
20375bf1e00bSNikolay Aleksandrov 	bool changed = false;
20385bf1e00bSNikolay Aleksandrov 	struct br_ip src_ip;
20395bf1e00bSNikolay Aleksandrov 
20405bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
20415bf1e00bSNikolay Aleksandrov 		ent->flags |= BR_SGRP_F_SEND;
20425bf1e00bSNikolay Aleksandrov 
20435bf1e00bSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2044085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
20455bf1e00bSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2046e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
20475bf1e00bSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
20485bf1e00bSNikolay Aleksandrov 		if (ent) {
20495bf1e00bSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_SEND;
20505bf1e00bSNikolay Aleksandrov 			to_send--;
20515bf1e00bSNikolay Aleksandrov 		} else {
20525bf1e00bSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
20535bf1e00bSNikolay Aleksandrov 			if (ent)
20545bf1e00bSNikolay Aleksandrov 				changed = true;
20555bf1e00bSNikolay Aleksandrov 		}
20565bf1e00bSNikolay Aleksandrov 		if (ent)
2057b0812368SNikolay Aleksandrov 			__grp_src_mod_timer(ent, now + br_multicast_gmi(br));
20585bf1e00bSNikolay Aleksandrov 	}
20595bf1e00bSNikolay Aleksandrov 
2060ddc255d9SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
2061ddc255d9SNikolay Aleksandrov 		changed = true;
2062ddc255d9SNikolay Aleksandrov 
20635bf1e00bSNikolay Aleksandrov 	if (to_send)
20645bf1e00bSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
20655bf1e00bSNikolay Aleksandrov 
20665bf1e00bSNikolay Aleksandrov 	return changed;
20675bf1e00bSNikolay Aleksandrov }
20685bf1e00bSNikolay Aleksandrov 
20695bf1e00bSNikolay Aleksandrov /* State          Msg type      New state                Actions
20705bf1e00bSNikolay Aleksandrov  * EXCLUDE (X,Y)  TO_IN (A)     EXCLUDE (X+A,Y-A)        (A)=GMI
20715bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G,X-A)
20725bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G)
20735bf1e00bSNikolay Aleksandrov  */
207454bea721SNikolay Aleksandrov static bool __grp_src_toin_excl(struct net_bridge_port_group *pg, void *h_addr,
2075ddc255d9SNikolay Aleksandrov 				void *srcs, u32 nsrcs, size_t addr_size,
2076ddc255d9SNikolay Aleksandrov 				int grec_type)
20775bf1e00bSNikolay Aleksandrov {
2078085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
20795bf1e00bSNikolay Aleksandrov 	u32 src_idx, to_send = pg->src_ents;
20805bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
20815bf1e00bSNikolay Aleksandrov 	unsigned long now = jiffies;
20825bf1e00bSNikolay Aleksandrov 	bool changed = false;
20835bf1e00bSNikolay Aleksandrov 	struct br_ip src_ip;
20845bf1e00bSNikolay Aleksandrov 
20855bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
20865bf1e00bSNikolay Aleksandrov 		if (timer_pending(&ent->timer))
20875bf1e00bSNikolay Aleksandrov 			ent->flags |= BR_SGRP_F_SEND;
20885bf1e00bSNikolay Aleksandrov 
20895bf1e00bSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2090085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
20915bf1e00bSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2092e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
20935bf1e00bSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
20945bf1e00bSNikolay Aleksandrov 		if (ent) {
20955bf1e00bSNikolay Aleksandrov 			if (timer_pending(&ent->timer)) {
20965bf1e00bSNikolay Aleksandrov 				ent->flags &= ~BR_SGRP_F_SEND;
20975bf1e00bSNikolay Aleksandrov 				to_send--;
20985bf1e00bSNikolay Aleksandrov 			}
20995bf1e00bSNikolay Aleksandrov 		} else {
21005bf1e00bSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
21015bf1e00bSNikolay Aleksandrov 			if (ent)
21025bf1e00bSNikolay Aleksandrov 				changed = true;
21035bf1e00bSNikolay Aleksandrov 		}
21045bf1e00bSNikolay Aleksandrov 		if (ent)
2105b0812368SNikolay Aleksandrov 			__grp_src_mod_timer(ent, now + br_multicast_gmi(br));
21065bf1e00bSNikolay Aleksandrov 	}
21075bf1e00bSNikolay Aleksandrov 
2108ddc255d9SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
2109ddc255d9SNikolay Aleksandrov 		changed = true;
2110ddc255d9SNikolay Aleksandrov 
21115bf1e00bSNikolay Aleksandrov 	if (to_send)
21125bf1e00bSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
21135bf1e00bSNikolay Aleksandrov 
21145bf1e00bSNikolay Aleksandrov 	__grp_send_query_and_rexmit(pg);
21155bf1e00bSNikolay Aleksandrov 
21165bf1e00bSNikolay Aleksandrov 	return changed;
21175bf1e00bSNikolay Aleksandrov }
21185bf1e00bSNikolay Aleksandrov 
211954bea721SNikolay Aleksandrov static bool br_multicast_toin(struct net_bridge_port_group *pg, void *h_addr,
2120ddc255d9SNikolay Aleksandrov 			      void *srcs, u32 nsrcs, size_t addr_size,
2121ddc255d9SNikolay Aleksandrov 			      int grec_type)
21225bf1e00bSNikolay Aleksandrov {
21235bf1e00bSNikolay Aleksandrov 	bool changed = false;
21245bf1e00bSNikolay Aleksandrov 
21255bf1e00bSNikolay Aleksandrov 	switch (pg->filter_mode) {
21265bf1e00bSNikolay Aleksandrov 	case MCAST_INCLUDE:
2127ddc255d9SNikolay Aleksandrov 		changed = __grp_src_toin_incl(pg, h_addr, srcs, nsrcs, addr_size,
2128ddc255d9SNikolay Aleksandrov 					      grec_type);
21295bf1e00bSNikolay Aleksandrov 		break;
21305bf1e00bSNikolay Aleksandrov 	case MCAST_EXCLUDE:
2131ddc255d9SNikolay Aleksandrov 		changed = __grp_src_toin_excl(pg, h_addr, srcs, nsrcs, addr_size,
2132ddc255d9SNikolay Aleksandrov 					      grec_type);
21335bf1e00bSNikolay Aleksandrov 		break;
21345bf1e00bSNikolay Aleksandrov 	}
21355bf1e00bSNikolay Aleksandrov 
2136ddc255d9SNikolay Aleksandrov 	if (br_multicast_eht_should_del_pg(pg)) {
2137d5a10222SNikolay Aleksandrov 		pg->flags |= MDB_PG_FLAGS_FAST_LEAVE;
2138ddc255d9SNikolay Aleksandrov 		br_multicast_find_del_pg(pg->key.port->br, pg);
2139ddc255d9SNikolay Aleksandrov 		/* a notification has already been sent and we shouldn't
2140ddc255d9SNikolay Aleksandrov 		 * access pg after the delete so we have to return false
2141ddc255d9SNikolay Aleksandrov 		 */
2142ddc255d9SNikolay Aleksandrov 		changed = false;
2143ddc255d9SNikolay Aleksandrov 	}
2144ddc255d9SNikolay Aleksandrov 
21455bf1e00bSNikolay Aleksandrov 	return changed;
21465bf1e00bSNikolay Aleksandrov }
21475bf1e00bSNikolay Aleksandrov 
21485bf1e00bSNikolay Aleksandrov /* State          Msg type      New state                Actions
21495bf1e00bSNikolay Aleksandrov  * INCLUDE (A)    TO_EX (B)     EXCLUDE (A*B,B-A)        (B-A)=0
21505bf1e00bSNikolay Aleksandrov  *                                                       Delete (A-B)
21515bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G,A*B)
21525bf1e00bSNikolay Aleksandrov  *                                                       Group Timer=GMI
21535bf1e00bSNikolay Aleksandrov  */
215454bea721SNikolay Aleksandrov static void __grp_src_toex_incl(struct net_bridge_port_group *pg, void *h_addr,
2155ddc255d9SNikolay Aleksandrov 				void *srcs, u32 nsrcs, size_t addr_size,
2156ddc255d9SNikolay Aleksandrov 				int grec_type)
21575bf1e00bSNikolay Aleksandrov {
21585bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
21595bf1e00bSNikolay Aleksandrov 	u32 src_idx, to_send = 0;
21605bf1e00bSNikolay Aleksandrov 	struct br_ip src_ip;
21615bf1e00bSNikolay Aleksandrov 
21625bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
21635bf1e00bSNikolay Aleksandrov 		ent->flags = (ent->flags & ~BR_SGRP_F_SEND) | BR_SGRP_F_DELETE;
21645bf1e00bSNikolay Aleksandrov 
21655bf1e00bSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2166085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
21675bf1e00bSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2168e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
21695bf1e00bSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
21705bf1e00bSNikolay Aleksandrov 		if (ent) {
21715bf1e00bSNikolay Aleksandrov 			ent->flags = (ent->flags & ~BR_SGRP_F_DELETE) |
21725bf1e00bSNikolay Aleksandrov 				     BR_SGRP_F_SEND;
21735bf1e00bSNikolay Aleksandrov 			to_send++;
21745bf1e00bSNikolay Aleksandrov 		} else {
2175b0812368SNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
21765bf1e00bSNikolay Aleksandrov 		}
2177b0812368SNikolay Aleksandrov 		if (ent)
2178b0812368SNikolay Aleksandrov 			br_multicast_fwd_src_handle(ent);
21795bf1e00bSNikolay Aleksandrov 	}
21805bf1e00bSNikolay Aleksandrov 
2181ddc255d9SNikolay Aleksandrov 	br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type);
2182ddc255d9SNikolay Aleksandrov 
21835bf1e00bSNikolay Aleksandrov 	__grp_src_delete_marked(pg);
21845bf1e00bSNikolay Aleksandrov 	if (to_send)
21855bf1e00bSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
21865bf1e00bSNikolay Aleksandrov }
21875bf1e00bSNikolay Aleksandrov 
21885bf1e00bSNikolay Aleksandrov /* State          Msg type      New state                Actions
21895bf1e00bSNikolay Aleksandrov  * EXCLUDE (X,Y)  TO_EX (A)     EXCLUDE (A-Y,Y*A)        (A-X-Y)=Group Timer
21905bf1e00bSNikolay Aleksandrov  *                                                       Delete (X-A)
21915bf1e00bSNikolay Aleksandrov  *                                                       Delete (Y-A)
21925bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G,A-Y)
21935bf1e00bSNikolay Aleksandrov  *                                                       Group Timer=GMI
21945bf1e00bSNikolay Aleksandrov  */
219554bea721SNikolay Aleksandrov static bool __grp_src_toex_excl(struct net_bridge_port_group *pg, void *h_addr,
2196ddc255d9SNikolay Aleksandrov 				void *srcs, u32 nsrcs, size_t addr_size,
2197ddc255d9SNikolay Aleksandrov 				int grec_type)
21985bf1e00bSNikolay Aleksandrov {
21995bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
22005bf1e00bSNikolay Aleksandrov 	u32 src_idx, to_send = 0;
22015bf1e00bSNikolay Aleksandrov 	bool changed = false;
22025bf1e00bSNikolay Aleksandrov 	struct br_ip src_ip;
22035bf1e00bSNikolay Aleksandrov 
22045bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
22055bf1e00bSNikolay Aleksandrov 		ent->flags = (ent->flags & ~BR_SGRP_F_SEND) | BR_SGRP_F_DELETE;
22065bf1e00bSNikolay Aleksandrov 
22075bf1e00bSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2208085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
22095bf1e00bSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2210e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
22115bf1e00bSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
22125bf1e00bSNikolay Aleksandrov 		if (ent) {
22135bf1e00bSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_DELETE;
22145bf1e00bSNikolay Aleksandrov 		} else {
22155bf1e00bSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
22165bf1e00bSNikolay Aleksandrov 			if (ent) {
2217b0812368SNikolay Aleksandrov 				__grp_src_mod_timer(ent, pg->timer.expires);
22185bf1e00bSNikolay Aleksandrov 				changed = true;
22195bf1e00bSNikolay Aleksandrov 			}
22205bf1e00bSNikolay Aleksandrov 		}
22215bf1e00bSNikolay Aleksandrov 		if (ent && timer_pending(&ent->timer)) {
22225bf1e00bSNikolay Aleksandrov 			ent->flags |= BR_SGRP_F_SEND;
22235bf1e00bSNikolay Aleksandrov 			to_send++;
22245bf1e00bSNikolay Aleksandrov 		}
22255bf1e00bSNikolay Aleksandrov 	}
22265bf1e00bSNikolay Aleksandrov 
2227ddc255d9SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
2228ddc255d9SNikolay Aleksandrov 		changed = true;
2229ddc255d9SNikolay Aleksandrov 
22305bf1e00bSNikolay Aleksandrov 	if (__grp_src_delete_marked(pg))
22315bf1e00bSNikolay Aleksandrov 		changed = true;
22325bf1e00bSNikolay Aleksandrov 	if (to_send)
22335bf1e00bSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
22345bf1e00bSNikolay Aleksandrov 
22355bf1e00bSNikolay Aleksandrov 	return changed;
22365bf1e00bSNikolay Aleksandrov }
22375bf1e00bSNikolay Aleksandrov 
223854bea721SNikolay Aleksandrov static bool br_multicast_toex(struct net_bridge_port_group *pg, void *h_addr,
2239ddc255d9SNikolay Aleksandrov 			      void *srcs, u32 nsrcs, size_t addr_size,
2240ddc255d9SNikolay Aleksandrov 			      int grec_type)
22415bf1e00bSNikolay Aleksandrov {
2242085b53c8SNikolay Aleksandrov 	struct net_bridge *br = pg->key.port->br;
22435bf1e00bSNikolay Aleksandrov 	bool changed = false;
22445bf1e00bSNikolay Aleksandrov 
22455bf1e00bSNikolay Aleksandrov 	switch (pg->filter_mode) {
22465bf1e00bSNikolay Aleksandrov 	case MCAST_INCLUDE:
2247ddc255d9SNikolay Aleksandrov 		__grp_src_toex_incl(pg, h_addr, srcs, nsrcs, addr_size,
2248ddc255d9SNikolay Aleksandrov 				    grec_type);
22498266a049SNikolay Aleksandrov 		br_multicast_star_g_handle_mode(pg, MCAST_EXCLUDE);
22505bf1e00bSNikolay Aleksandrov 		changed = true;
22515bf1e00bSNikolay Aleksandrov 		break;
22525bf1e00bSNikolay Aleksandrov 	case MCAST_EXCLUDE:
2253ddc255d9SNikolay Aleksandrov 		changed = __grp_src_toex_excl(pg, h_addr, srcs, nsrcs, addr_size,
2254ddc255d9SNikolay Aleksandrov 					      grec_type);
22555bf1e00bSNikolay Aleksandrov 		break;
22565bf1e00bSNikolay Aleksandrov 	}
22575bf1e00bSNikolay Aleksandrov 
22585bf1e00bSNikolay Aleksandrov 	pg->filter_mode = MCAST_EXCLUDE;
22595bf1e00bSNikolay Aleksandrov 	mod_timer(&pg->timer, jiffies + br_multicast_gmi(br));
22605bf1e00bSNikolay Aleksandrov 
22615bf1e00bSNikolay Aleksandrov 	return changed;
22625bf1e00bSNikolay Aleksandrov }
22635bf1e00bSNikolay Aleksandrov 
2264109865feSNikolay Aleksandrov /* State          Msg type      New state                Actions
2265109865feSNikolay Aleksandrov  * INCLUDE (A)    BLOCK (B)     INCLUDE (A)              Send Q(G,A*B)
2266109865feSNikolay Aleksandrov  */
22670ad57c99SNikolay Aleksandrov static bool __grp_src_block_incl(struct net_bridge_port_group *pg, void *h_addr,
2268474ddb37SNikolay Aleksandrov 				 void *srcs, u32 nsrcs, size_t addr_size, int grec_type)
2269109865feSNikolay Aleksandrov {
2270109865feSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
2271109865feSNikolay Aleksandrov 	u32 src_idx, to_send = 0;
22720ad57c99SNikolay Aleksandrov 	bool changed = false;
2273109865feSNikolay Aleksandrov 	struct br_ip src_ip;
2274109865feSNikolay Aleksandrov 
2275109865feSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
2276109865feSNikolay Aleksandrov 		ent->flags &= ~BR_SGRP_F_SEND;
2277109865feSNikolay Aleksandrov 
2278109865feSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2279085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
2280109865feSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2281e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2282109865feSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
2283109865feSNikolay Aleksandrov 		if (ent) {
2284109865feSNikolay Aleksandrov 			ent->flags |= BR_SGRP_F_SEND;
2285109865feSNikolay Aleksandrov 			to_send++;
2286109865feSNikolay Aleksandrov 		}
2287109865feSNikolay Aleksandrov 	}
2288109865feSNikolay Aleksandrov 
2289474ddb37SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
2290474ddb37SNikolay Aleksandrov 		changed = true;
2291474ddb37SNikolay Aleksandrov 
2292109865feSNikolay Aleksandrov 	if (to_send)
2293109865feSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
2294109865feSNikolay Aleksandrov 
22950ad57c99SNikolay Aleksandrov 	return changed;
2296109865feSNikolay Aleksandrov }
2297109865feSNikolay Aleksandrov 
2298109865feSNikolay Aleksandrov /* State          Msg type      New state                Actions
2299109865feSNikolay Aleksandrov  * EXCLUDE (X,Y)  BLOCK (A)     EXCLUDE (X+(A-Y),Y)      (A-X-Y)=Group Timer
2300109865feSNikolay Aleksandrov  *                                                       Send Q(G,A-Y)
2301109865feSNikolay Aleksandrov  */
230254bea721SNikolay Aleksandrov static bool __grp_src_block_excl(struct net_bridge_port_group *pg, void *h_addr,
2303474ddb37SNikolay Aleksandrov 				 void *srcs, u32 nsrcs, size_t addr_size, int grec_type)
2304109865feSNikolay Aleksandrov {
2305109865feSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
2306109865feSNikolay Aleksandrov 	u32 src_idx, to_send = 0;
2307109865feSNikolay Aleksandrov 	bool changed = false;
2308109865feSNikolay Aleksandrov 	struct br_ip src_ip;
2309109865feSNikolay Aleksandrov 
2310109865feSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
2311109865feSNikolay Aleksandrov 		ent->flags &= ~BR_SGRP_F_SEND;
2312109865feSNikolay Aleksandrov 
2313109865feSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
2314085b53c8SNikolay Aleksandrov 	src_ip.proto = pg->key.addr.proto;
2315109865feSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
2316e7cfcf2cSNikolay Aleksandrov 		memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size);
2317109865feSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
2318109865feSNikolay Aleksandrov 		if (!ent) {
2319109865feSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
2320109865feSNikolay Aleksandrov 			if (ent) {
2321b0812368SNikolay Aleksandrov 				__grp_src_mod_timer(ent, pg->timer.expires);
2322109865feSNikolay Aleksandrov 				changed = true;
2323109865feSNikolay Aleksandrov 			}
2324109865feSNikolay Aleksandrov 		}
2325109865feSNikolay Aleksandrov 		if (ent && timer_pending(&ent->timer)) {
2326109865feSNikolay Aleksandrov 			ent->flags |= BR_SGRP_F_SEND;
2327109865feSNikolay Aleksandrov 			to_send++;
2328109865feSNikolay Aleksandrov 		}
2329109865feSNikolay Aleksandrov 	}
2330109865feSNikolay Aleksandrov 
2331474ddb37SNikolay Aleksandrov 	if (br_multicast_eht_handle(pg, h_addr, srcs, nsrcs, addr_size, grec_type))
2332474ddb37SNikolay Aleksandrov 		changed = true;
2333474ddb37SNikolay Aleksandrov 
2334109865feSNikolay Aleksandrov 	if (to_send)
2335109865feSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
2336109865feSNikolay Aleksandrov 
2337109865feSNikolay Aleksandrov 	return changed;
2338109865feSNikolay Aleksandrov }
2339109865feSNikolay Aleksandrov 
234054bea721SNikolay Aleksandrov static bool br_multicast_block(struct net_bridge_port_group *pg, void *h_addr,
2341474ddb37SNikolay Aleksandrov 			       void *srcs, u32 nsrcs, size_t addr_size, int grec_type)
2342109865feSNikolay Aleksandrov {
2343109865feSNikolay Aleksandrov 	bool changed = false;
2344109865feSNikolay Aleksandrov 
2345109865feSNikolay Aleksandrov 	switch (pg->filter_mode) {
2346109865feSNikolay Aleksandrov 	case MCAST_INCLUDE:
2347474ddb37SNikolay Aleksandrov 		changed = __grp_src_block_incl(pg, h_addr, srcs, nsrcs, addr_size,
2348474ddb37SNikolay Aleksandrov 					       grec_type);
2349109865feSNikolay Aleksandrov 		break;
2350109865feSNikolay Aleksandrov 	case MCAST_EXCLUDE:
2351474ddb37SNikolay Aleksandrov 		changed = __grp_src_block_excl(pg, h_addr, srcs, nsrcs, addr_size,
2352474ddb37SNikolay Aleksandrov 					       grec_type);
2353109865feSNikolay Aleksandrov 		break;
2354109865feSNikolay Aleksandrov 	}
2355109865feSNikolay Aleksandrov 
2356e87e4b5cSNikolay Aleksandrov 	if ((pg->filter_mode == MCAST_INCLUDE && hlist_empty(&pg->src_list)) ||
2357e87e4b5cSNikolay Aleksandrov 	    br_multicast_eht_should_del_pg(pg)) {
2358d5a10222SNikolay Aleksandrov 		if (br_multicast_eht_should_del_pg(pg))
2359d5a10222SNikolay Aleksandrov 			pg->flags |= MDB_PG_FLAGS_FAST_LEAVE;
2360e87e4b5cSNikolay Aleksandrov 		br_multicast_find_del_pg(pg->key.port->br, pg);
2361e87e4b5cSNikolay Aleksandrov 		/* a notification has already been sent and we shouldn't
2362e87e4b5cSNikolay Aleksandrov 		 * access pg after the delete so we have to return false
2363e87e4b5cSNikolay Aleksandrov 		 */
2364e87e4b5cSNikolay Aleksandrov 		changed = false;
2365e87e4b5cSNikolay Aleksandrov 	}
2366e87e4b5cSNikolay Aleksandrov 
2367109865feSNikolay Aleksandrov 	return changed;
2368109865feSNikolay Aleksandrov }
2369109865feSNikolay Aleksandrov 
23700436862eSNikolay Aleksandrov static struct net_bridge_port_group *
23710436862eSNikolay Aleksandrov br_multicast_find_port(struct net_bridge_mdb_entry *mp,
23720436862eSNikolay Aleksandrov 		       struct net_bridge_port *p,
23730436862eSNikolay Aleksandrov 		       const unsigned char *src)
23740436862eSNikolay Aleksandrov {
2375071445c6SNikolay Aleksandrov 	struct net_bridge *br __maybe_unused = mp->br;
23760436862eSNikolay Aleksandrov 	struct net_bridge_port_group *pg;
23770436862eSNikolay Aleksandrov 
23780436862eSNikolay Aleksandrov 	for (pg = mlock_dereference(mp->ports, br);
23790436862eSNikolay Aleksandrov 	     pg;
23800436862eSNikolay Aleksandrov 	     pg = mlock_dereference(pg->next, br))
23810436862eSNikolay Aleksandrov 		if (br_port_group_equal(pg, p, src))
23820436862eSNikolay Aleksandrov 			return pg;
23830436862eSNikolay Aleksandrov 
23840436862eSNikolay Aleksandrov 	return NULL;
23850436862eSNikolay Aleksandrov }
23860436862eSNikolay Aleksandrov 
23878ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
2388eb1d1641SHerbert Xu 					 struct net_bridge_port *port,
238906499098SVlad Yasevich 					 struct sk_buff *skb,
239006499098SVlad Yasevich 					 u16 vid)
2391eb1d1641SHerbert Xu {
23920436862eSNikolay Aleksandrov 	bool igmpv2 = br->multicast_igmp_version == 2;
23930436862eSNikolay Aleksandrov 	struct net_bridge_mdb_entry *mdst;
23940436862eSNikolay Aleksandrov 	struct net_bridge_port_group *pg;
23956db6f0eaSFelix Fietkau 	const unsigned char *src;
2396eb1d1641SHerbert Xu 	struct igmpv3_report *ih;
2397eb1d1641SHerbert Xu 	struct igmpv3_grec *grec;
23980436862eSNikolay Aleksandrov 	int i, len, num, type;
239954bea721SNikolay Aleksandrov 	__be32 group, *h_addr;
24000436862eSNikolay Aleksandrov 	bool changed = false;
24010436862eSNikolay Aleksandrov 	int err = 0;
2402e57f6185SNikolay Aleksandrov 	u16 nsrcs;
2403eb1d1641SHerbert Xu 
2404eb1d1641SHerbert Xu 	ih = igmpv3_report_hdr(skb);
2405eb1d1641SHerbert Xu 	num = ntohs(ih->ngrec);
2406c2d4fbd2SLinus Lüssing 	len = skb_transport_offset(skb) + sizeof(*ih);
2407eb1d1641SHerbert Xu 
2408eb1d1641SHerbert Xu 	for (i = 0; i < num; i++) {
2409eb1d1641SHerbert Xu 		len += sizeof(*grec);
2410ba5ea614SLinus Lüssing 		if (!ip_mc_may_pull(skb, len))
2411eb1d1641SHerbert Xu 			return -EINVAL;
2412eb1d1641SHerbert Xu 
2413fd218cf9SHerbert Xu 		grec = (void *)(skb->data + len - sizeof(*grec));
2414eb1d1641SHerbert Xu 		group = grec->grec_mca;
2415eb1d1641SHerbert Xu 		type = grec->grec_type;
2416e57f6185SNikolay Aleksandrov 		nsrcs = ntohs(grec->grec_nsrcs);
2417eb1d1641SHerbert Xu 
2418e57f6185SNikolay Aleksandrov 		len += nsrcs * 4;
2419ba5ea614SLinus Lüssing 		if (!ip_mc_may_pull(skb, len))
2420eb1d1641SHerbert Xu 			return -EINVAL;
2421eb1d1641SHerbert Xu 
2422eb1d1641SHerbert Xu 		switch (type) {
2423eb1d1641SHerbert Xu 		case IGMPV3_MODE_IS_INCLUDE:
2424eb1d1641SHerbert Xu 		case IGMPV3_MODE_IS_EXCLUDE:
2425eb1d1641SHerbert Xu 		case IGMPV3_CHANGE_TO_INCLUDE:
2426eb1d1641SHerbert Xu 		case IGMPV3_CHANGE_TO_EXCLUDE:
2427eb1d1641SHerbert Xu 		case IGMPV3_ALLOW_NEW_SOURCES:
2428eb1d1641SHerbert Xu 		case IGMPV3_BLOCK_OLD_SOURCES:
2429eb1d1641SHerbert Xu 			break;
2430eb1d1641SHerbert Xu 
2431eb1d1641SHerbert Xu 		default:
2432eb1d1641SHerbert Xu 			continue;
2433eb1d1641SHerbert Xu 		}
2434eb1d1641SHerbert Xu 
24356db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
24360436862eSNikolay Aleksandrov 		if (nsrcs == 0 &&
24370436862eSNikolay Aleksandrov 		    (type == IGMPV3_CHANGE_TO_INCLUDE ||
24380436862eSNikolay Aleksandrov 		     type == IGMPV3_MODE_IS_INCLUDE)) {
24390436862eSNikolay Aleksandrov 			if (!port || igmpv2) {
24406db6f0eaSFelix Fietkau 				br_ip4_multicast_leave_group(br, port, group, vid, src);
24410436862eSNikolay Aleksandrov 				continue;
24420436862eSNikolay Aleksandrov 			}
2443bc8c20acSSatish Ashok 		} else {
24446db6f0eaSFelix Fietkau 			err = br_ip4_multicast_add_group(br, port, group, vid,
24450436862eSNikolay Aleksandrov 							 src, igmpv2);
2446eb1d1641SHerbert Xu 			if (err)
2447eb1d1641SHerbert Xu 				break;
2448eb1d1641SHerbert Xu 		}
24490436862eSNikolay Aleksandrov 
24500436862eSNikolay Aleksandrov 		if (!port || igmpv2)
24510436862eSNikolay Aleksandrov 			continue;
24520436862eSNikolay Aleksandrov 
24530436862eSNikolay Aleksandrov 		spin_lock_bh(&br->multicast_lock);
24540436862eSNikolay Aleksandrov 		mdst = br_mdb_ip4_get(br, group, vid);
24550436862eSNikolay Aleksandrov 		if (!mdst)
24560436862eSNikolay Aleksandrov 			goto unlock_continue;
24570436862eSNikolay Aleksandrov 		pg = br_multicast_find_port(mdst, port, src);
24580436862eSNikolay Aleksandrov 		if (!pg || (pg->flags & MDB_PG_FLAGS_PERMANENT))
24590436862eSNikolay Aleksandrov 			goto unlock_continue;
246054bea721SNikolay Aleksandrov 		/* reload grec and host addr */
24610436862eSNikolay Aleksandrov 		grec = (void *)(skb->data + len - sizeof(*grec) - (nsrcs * 4));
246254bea721SNikolay Aleksandrov 		h_addr = &ip_hdr(skb)->saddr;
24630436862eSNikolay Aleksandrov 		switch (type) {
24640436862eSNikolay Aleksandrov 		case IGMPV3_ALLOW_NEW_SOURCES:
246554bea721SNikolay Aleksandrov 			changed = br_multicast_isinc_allow(pg, h_addr, grec->grec_src,
2466474ddb37SNikolay Aleksandrov 							   nsrcs, sizeof(__be32), type);
24670436862eSNikolay Aleksandrov 			break;
2468e6231bcaSNikolay Aleksandrov 		case IGMPV3_MODE_IS_INCLUDE:
246954bea721SNikolay Aleksandrov 			changed = br_multicast_isinc_allow(pg, h_addr, grec->grec_src,
2470474ddb37SNikolay Aleksandrov 							   nsrcs, sizeof(__be32), type);
2471e6231bcaSNikolay Aleksandrov 			break;
2472e6231bcaSNikolay Aleksandrov 		case IGMPV3_MODE_IS_EXCLUDE:
247354bea721SNikolay Aleksandrov 			changed = br_multicast_isexc(pg, h_addr, grec->grec_src,
2474ddc255d9SNikolay Aleksandrov 						     nsrcs, sizeof(__be32), type);
2475e6231bcaSNikolay Aleksandrov 			break;
24765bf1e00bSNikolay Aleksandrov 		case IGMPV3_CHANGE_TO_INCLUDE:
247754bea721SNikolay Aleksandrov 			changed = br_multicast_toin(pg, h_addr, grec->grec_src,
2478ddc255d9SNikolay Aleksandrov 						    nsrcs, sizeof(__be32), type);
24795bf1e00bSNikolay Aleksandrov 			break;
24805bf1e00bSNikolay Aleksandrov 		case IGMPV3_CHANGE_TO_EXCLUDE:
248154bea721SNikolay Aleksandrov 			changed = br_multicast_toex(pg, h_addr, grec->grec_src,
2482ddc255d9SNikolay Aleksandrov 						    nsrcs, sizeof(__be32), type);
24835bf1e00bSNikolay Aleksandrov 			break;
2484109865feSNikolay Aleksandrov 		case IGMPV3_BLOCK_OLD_SOURCES:
248554bea721SNikolay Aleksandrov 			changed = br_multicast_block(pg, h_addr, grec->grec_src,
2486474ddb37SNikolay Aleksandrov 						     nsrcs, sizeof(__be32), type);
2487109865feSNikolay Aleksandrov 			break;
24880436862eSNikolay Aleksandrov 		}
24890436862eSNikolay Aleksandrov 		if (changed)
24900436862eSNikolay Aleksandrov 			br_mdb_notify(br->dev, mdst, pg, RTM_NEWMDB);
24910436862eSNikolay Aleksandrov unlock_continue:
24920436862eSNikolay Aleksandrov 		spin_unlock_bh(&br->multicast_lock);
2493bc8c20acSSatish Ashok 	}
2494eb1d1641SHerbert Xu 
2495eb1d1641SHerbert Xu 	return err;
2496eb1d1641SHerbert Xu }
2497eb1d1641SHerbert Xu 
2498dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
249908b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_mld2_report(struct net_bridge *br,
250008b202b6SYOSHIFUJI Hideaki 					struct net_bridge_port *port,
250106499098SVlad Yasevich 					struct sk_buff *skb,
250206499098SVlad Yasevich 					u16 vid)
250308b202b6SYOSHIFUJI Hideaki {
25040436862eSNikolay Aleksandrov 	bool mldv1 = br->multicast_mld_version == 1;
25050436862eSNikolay Aleksandrov 	struct net_bridge_mdb_entry *mdst;
25060436862eSNikolay Aleksandrov 	struct net_bridge_port_group *pg;
2507ba5ea614SLinus Lüssing 	unsigned int nsrcs_offset;
25086db6f0eaSFelix Fietkau 	const unsigned char *src;
250908b202b6SYOSHIFUJI Hideaki 	struct icmp6hdr *icmp6h;
251054bea721SNikolay Aleksandrov 	struct in6_addr *h_addr;
251108b202b6SYOSHIFUJI Hideaki 	struct mld2_grec *grec;
2512ba5ea614SLinus Lüssing 	unsigned int grec_len;
25130436862eSNikolay Aleksandrov 	bool changed = false;
25140436862eSNikolay Aleksandrov 	int i, len, num;
251508b202b6SYOSHIFUJI Hideaki 	int err = 0;
251608b202b6SYOSHIFUJI Hideaki 
2517ba5ea614SLinus Lüssing 	if (!ipv6_mc_may_pull(skb, sizeof(*icmp6h)))
251808b202b6SYOSHIFUJI Hideaki 		return -EINVAL;
251908b202b6SYOSHIFUJI Hideaki 
252008b202b6SYOSHIFUJI Hideaki 	icmp6h = icmp6_hdr(skb);
252108b202b6SYOSHIFUJI Hideaki 	num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
2522c2d4fbd2SLinus Lüssing 	len = skb_transport_offset(skb) + sizeof(*icmp6h);
252308b202b6SYOSHIFUJI Hideaki 
252408b202b6SYOSHIFUJI Hideaki 	for (i = 0; i < num; i++) {
2525e57f6185SNikolay Aleksandrov 		__be16 *_nsrcs, __nsrcs;
2526e57f6185SNikolay Aleksandrov 		u16 nsrcs;
252708b202b6SYOSHIFUJI Hideaki 
2528ba5ea614SLinus Lüssing 		nsrcs_offset = len + offsetof(struct mld2_grec, grec_nsrcs);
2529ba5ea614SLinus Lüssing 
2530ba5ea614SLinus Lüssing 		if (skb_transport_offset(skb) + ipv6_transport_len(skb) <
25315fc6266aSLinus Lüssing 		    nsrcs_offset + sizeof(__nsrcs))
2532ba5ea614SLinus Lüssing 			return -EINVAL;
2533ba5ea614SLinus Lüssing 
2534e57f6185SNikolay Aleksandrov 		_nsrcs = skb_header_pointer(skb, nsrcs_offset,
2535e57f6185SNikolay Aleksandrov 					    sizeof(__nsrcs), &__nsrcs);
2536e57f6185SNikolay Aleksandrov 		if (!_nsrcs)
253708b202b6SYOSHIFUJI Hideaki 			return -EINVAL;
253808b202b6SYOSHIFUJI Hideaki 
2539e57f6185SNikolay Aleksandrov 		nsrcs = ntohs(*_nsrcs);
2540e57f6185SNikolay Aleksandrov 		grec_len = struct_size(grec, grec_src, nsrcs);
2541ba5ea614SLinus Lüssing 
2542ba5ea614SLinus Lüssing 		if (!ipv6_mc_may_pull(skb, len + grec_len))
254308b202b6SYOSHIFUJI Hideaki 			return -EINVAL;
254408b202b6SYOSHIFUJI Hideaki 
254508b202b6SYOSHIFUJI Hideaki 		grec = (struct mld2_grec *)(skb->data + len);
2546ba5ea614SLinus Lüssing 		len += grec_len;
254708b202b6SYOSHIFUJI Hideaki 
254808b202b6SYOSHIFUJI Hideaki 		switch (grec->grec_type) {
254908b202b6SYOSHIFUJI Hideaki 		case MLD2_MODE_IS_INCLUDE:
255008b202b6SYOSHIFUJI Hideaki 		case MLD2_MODE_IS_EXCLUDE:
255108b202b6SYOSHIFUJI Hideaki 		case MLD2_CHANGE_TO_INCLUDE:
255208b202b6SYOSHIFUJI Hideaki 		case MLD2_CHANGE_TO_EXCLUDE:
255308b202b6SYOSHIFUJI Hideaki 		case MLD2_ALLOW_NEW_SOURCES:
255408b202b6SYOSHIFUJI Hideaki 		case MLD2_BLOCK_OLD_SOURCES:
255508b202b6SYOSHIFUJI Hideaki 			break;
255608b202b6SYOSHIFUJI Hideaki 
255708b202b6SYOSHIFUJI Hideaki 		default:
255808b202b6SYOSHIFUJI Hideaki 			continue;
255908b202b6SYOSHIFUJI Hideaki 		}
256008b202b6SYOSHIFUJI Hideaki 
25616db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
2562bc8c20acSSatish Ashok 		if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE ||
2563bc8c20acSSatish Ashok 		     grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
2564e57f6185SNikolay Aleksandrov 		    nsrcs == 0) {
25650436862eSNikolay Aleksandrov 			if (!port || mldv1) {
25660436862eSNikolay Aleksandrov 				br_ip6_multicast_leave_group(br, port,
25670436862eSNikolay Aleksandrov 							     &grec->grec_mca,
25686db6f0eaSFelix Fietkau 							     vid, src);
25690436862eSNikolay Aleksandrov 				continue;
25700436862eSNikolay Aleksandrov 			}
2571bc8c20acSSatish Ashok 		} else {
2572bc8c20acSSatish Ashok 			err = br_ip6_multicast_add_group(br, port,
25736db6f0eaSFelix Fietkau 							 &grec->grec_mca, vid,
25740436862eSNikolay Aleksandrov 							 src, mldv1);
25759264251eSDavide Caratti 			if (err)
257608b202b6SYOSHIFUJI Hideaki 				break;
257708b202b6SYOSHIFUJI Hideaki 		}
25780436862eSNikolay Aleksandrov 
25790436862eSNikolay Aleksandrov 		if (!port || mldv1)
25800436862eSNikolay Aleksandrov 			continue;
25810436862eSNikolay Aleksandrov 
25820436862eSNikolay Aleksandrov 		spin_lock_bh(&br->multicast_lock);
25830436862eSNikolay Aleksandrov 		mdst = br_mdb_ip6_get(br, &grec->grec_mca, vid);
25840436862eSNikolay Aleksandrov 		if (!mdst)
25850436862eSNikolay Aleksandrov 			goto unlock_continue;
25860436862eSNikolay Aleksandrov 		pg = br_multicast_find_port(mdst, port, src);
25870436862eSNikolay Aleksandrov 		if (!pg || (pg->flags & MDB_PG_FLAGS_PERMANENT))
25880436862eSNikolay Aleksandrov 			goto unlock_continue;
258954bea721SNikolay Aleksandrov 		h_addr = &ipv6_hdr(skb)->saddr;
25900436862eSNikolay Aleksandrov 		switch (grec->grec_type) {
25910436862eSNikolay Aleksandrov 		case MLD2_ALLOW_NEW_SOURCES:
259254bea721SNikolay Aleksandrov 			changed = br_multicast_isinc_allow(pg, h_addr,
259354bea721SNikolay Aleksandrov 							   grec->grec_src, nsrcs,
2594474ddb37SNikolay Aleksandrov 							   sizeof(struct in6_addr),
2595474ddb37SNikolay Aleksandrov 							   grec->grec_type);
25960436862eSNikolay Aleksandrov 			break;
2597e6231bcaSNikolay Aleksandrov 		case MLD2_MODE_IS_INCLUDE:
259854bea721SNikolay Aleksandrov 			changed = br_multicast_isinc_allow(pg, h_addr,
259954bea721SNikolay Aleksandrov 							   grec->grec_src, nsrcs,
2600474ddb37SNikolay Aleksandrov 							   sizeof(struct in6_addr),
2601474ddb37SNikolay Aleksandrov 							   grec->grec_type);
2602e6231bcaSNikolay Aleksandrov 			break;
2603e6231bcaSNikolay Aleksandrov 		case MLD2_MODE_IS_EXCLUDE:
260454bea721SNikolay Aleksandrov 			changed = br_multicast_isexc(pg, h_addr,
260554bea721SNikolay Aleksandrov 						     grec->grec_src, nsrcs,
2606ddc255d9SNikolay Aleksandrov 						     sizeof(struct in6_addr),
2607ddc255d9SNikolay Aleksandrov 						     grec->grec_type);
2608e6231bcaSNikolay Aleksandrov 			break;
26095bf1e00bSNikolay Aleksandrov 		case MLD2_CHANGE_TO_INCLUDE:
261054bea721SNikolay Aleksandrov 			changed = br_multicast_toin(pg, h_addr,
261154bea721SNikolay Aleksandrov 						    grec->grec_src, nsrcs,
2612ddc255d9SNikolay Aleksandrov 						    sizeof(struct in6_addr),
2613ddc255d9SNikolay Aleksandrov 						    grec->grec_type);
26145bf1e00bSNikolay Aleksandrov 			break;
26155bf1e00bSNikolay Aleksandrov 		case MLD2_CHANGE_TO_EXCLUDE:
261654bea721SNikolay Aleksandrov 			changed = br_multicast_toex(pg, h_addr,
261754bea721SNikolay Aleksandrov 						    grec->grec_src, nsrcs,
2618ddc255d9SNikolay Aleksandrov 						    sizeof(struct in6_addr),
2619ddc255d9SNikolay Aleksandrov 						    grec->grec_type);
26205bf1e00bSNikolay Aleksandrov 			break;
2621109865feSNikolay Aleksandrov 		case MLD2_BLOCK_OLD_SOURCES:
262254bea721SNikolay Aleksandrov 			changed = br_multicast_block(pg, h_addr,
262354bea721SNikolay Aleksandrov 						     grec->grec_src, nsrcs,
2624474ddb37SNikolay Aleksandrov 						     sizeof(struct in6_addr),
2625474ddb37SNikolay Aleksandrov 						     grec->grec_type);
2626109865feSNikolay Aleksandrov 			break;
26270436862eSNikolay Aleksandrov 		}
26280436862eSNikolay Aleksandrov 		if (changed)
26290436862eSNikolay Aleksandrov 			br_mdb_notify(br->dev, mdst, pg, RTM_NEWMDB);
26300436862eSNikolay Aleksandrov unlock_continue:
26310436862eSNikolay Aleksandrov 		spin_unlock_bh(&br->multicast_lock);
2632bc8c20acSSatish Ashok 	}
263308b202b6SYOSHIFUJI Hideaki 
263408b202b6SYOSHIFUJI Hideaki 	return err;
263508b202b6SYOSHIFUJI Hideaki }
263608b202b6SYOSHIFUJI Hideaki #endif
263708b202b6SYOSHIFUJI Hideaki 
2638dc4eb53aSLinus Lüssing static bool br_ip4_multicast_select_querier(struct net_bridge *br,
26392cd41431SLinus Lüssing 					    struct net_bridge_port *port,
2640dc4eb53aSLinus Lüssing 					    __be32 saddr)
2641dc4eb53aSLinus Lüssing {
2642dc4eb53aSLinus Lüssing 	if (!timer_pending(&br->ip4_own_query.timer) &&
2643dc4eb53aSLinus Lüssing 	    !timer_pending(&br->ip4_other_query.timer))
2644dc4eb53aSLinus Lüssing 		goto update;
2645dc4eb53aSLinus Lüssing 
2646deb96566SNikolay Aleksandrov 	if (!br->ip4_querier.addr.src.ip4)
2647dc4eb53aSLinus Lüssing 		goto update;
2648dc4eb53aSLinus Lüssing 
2649deb96566SNikolay Aleksandrov 	if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.src.ip4))
2650dc4eb53aSLinus Lüssing 		goto update;
2651dc4eb53aSLinus Lüssing 
2652dc4eb53aSLinus Lüssing 	return false;
2653dc4eb53aSLinus Lüssing 
2654dc4eb53aSLinus Lüssing update:
2655deb96566SNikolay Aleksandrov 	br->ip4_querier.addr.src.ip4 = saddr;
2656dc4eb53aSLinus Lüssing 
26572cd41431SLinus Lüssing 	/* update protected by general multicast_lock by caller */
26582cd41431SLinus Lüssing 	rcu_assign_pointer(br->ip4_querier.port, port);
26592cd41431SLinus Lüssing 
2660dc4eb53aSLinus Lüssing 	return true;
2661dc4eb53aSLinus Lüssing }
2662dc4eb53aSLinus Lüssing 
2663dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
2664dc4eb53aSLinus Lüssing static bool br_ip6_multicast_select_querier(struct net_bridge *br,
26652cd41431SLinus Lüssing 					    struct net_bridge_port *port,
2666dc4eb53aSLinus Lüssing 					    struct in6_addr *saddr)
2667dc4eb53aSLinus Lüssing {
2668dc4eb53aSLinus Lüssing 	if (!timer_pending(&br->ip6_own_query.timer) &&
2669dc4eb53aSLinus Lüssing 	    !timer_pending(&br->ip6_other_query.timer))
2670dc4eb53aSLinus Lüssing 		goto update;
2671dc4eb53aSLinus Lüssing 
2672deb96566SNikolay Aleksandrov 	if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.src.ip6) <= 0)
2673dc4eb53aSLinus Lüssing 		goto update;
2674dc4eb53aSLinus Lüssing 
2675dc4eb53aSLinus Lüssing 	return false;
2676dc4eb53aSLinus Lüssing 
2677dc4eb53aSLinus Lüssing update:
2678deb96566SNikolay Aleksandrov 	br->ip6_querier.addr.src.ip6 = *saddr;
2679dc4eb53aSLinus Lüssing 
26802cd41431SLinus Lüssing 	/* update protected by general multicast_lock by caller */
26812cd41431SLinus Lüssing 	rcu_assign_pointer(br->ip6_querier.port, port);
26822cd41431SLinus Lüssing 
2683dc4eb53aSLinus Lüssing 	return true;
2684dc4eb53aSLinus Lüssing }
2685dc4eb53aSLinus Lüssing #endif
2686dc4eb53aSLinus Lüssing 
2687cc0fdd80SLinus Lüssing static void
268890010b36SLinus Lüssing br_multicast_update_query_timer(struct net_bridge *br,
268990010b36SLinus Lüssing 				struct bridge_mcast_other_query *query,
2690b00589afSLinus Lüssing 				unsigned long max_delay)
2691b00589afSLinus Lüssing {
269290010b36SLinus Lüssing 	if (!timer_pending(&query->timer))
269390010b36SLinus Lüssing 		query->delay_time = jiffies + max_delay;
2694b00589afSLinus Lüssing 
269590010b36SLinus Lüssing 	mod_timer(&query->timer, jiffies + br->multicast_querier_interval);
2696b00589afSLinus Lüssing }
2697b00589afSLinus Lüssing 
26986d549648SNogah Frankel static void br_port_mc_router_state_change(struct net_bridge_port *p,
26996d549648SNogah Frankel 					   bool is_mc_router)
27006d549648SNogah Frankel {
27016d549648SNogah Frankel 	struct switchdev_attr attr = {
27026d549648SNogah Frankel 		.orig_dev = p->dev,
27036d549648SNogah Frankel 		.id = SWITCHDEV_ATTR_ID_PORT_MROUTER,
27046d549648SNogah Frankel 		.flags = SWITCHDEV_F_DEFER,
27056d549648SNogah Frankel 		.u.mrouter = is_mc_router,
27066d549648SNogah Frankel 	};
27076d549648SNogah Frankel 
2708dcbdf135SVladimir Oltean 	switchdev_port_attr_set(p->dev, &attr, NULL);
27096d549648SNogah Frankel }
27106d549648SNogah Frankel 
2711d9b8c4d8SLinus Lüssing static struct net_bridge_port *
2712d9b8c4d8SLinus Lüssing br_multicast_rport_from_node(struct net_bridge *br,
2713d9b8c4d8SLinus Lüssing 			     struct hlist_head *mc_router_list,
2714d9b8c4d8SLinus Lüssing 			     struct hlist_node *rlist)
2715d9b8c4d8SLinus Lüssing {
2716a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
2717a3c02e76SLinus Lüssing 	if (mc_router_list == &br->ip6_mc_router_list)
2718a3c02e76SLinus Lüssing 		return hlist_entry(rlist, struct net_bridge_port, ip6_rlist);
2719a3c02e76SLinus Lüssing #endif
2720d9b8c4d8SLinus Lüssing 	return hlist_entry(rlist, struct net_bridge_port, ip4_rlist);
2721d9b8c4d8SLinus Lüssing }
2722d9b8c4d8SLinus Lüssing 
2723d9b8c4d8SLinus Lüssing static struct hlist_node *
2724d9b8c4d8SLinus Lüssing br_multicast_get_rport_slot(struct net_bridge *br,
2725d9b8c4d8SLinus Lüssing 			    struct net_bridge_port *port,
2726d9b8c4d8SLinus Lüssing 			    struct hlist_head *mc_router_list)
2727d9b8c4d8SLinus Lüssing 
2728d9b8c4d8SLinus Lüssing {
2729d9b8c4d8SLinus Lüssing 	struct hlist_node *slot = NULL;
2730d9b8c4d8SLinus Lüssing 	struct net_bridge_port *p;
2731d9b8c4d8SLinus Lüssing 	struct hlist_node *rlist;
2732d9b8c4d8SLinus Lüssing 
2733d9b8c4d8SLinus Lüssing 	hlist_for_each(rlist, mc_router_list) {
2734d9b8c4d8SLinus Lüssing 		p = br_multicast_rport_from_node(br, mc_router_list, rlist);
2735d9b8c4d8SLinus Lüssing 
2736d9b8c4d8SLinus Lüssing 		if ((unsigned long)port >= (unsigned long)p)
2737d9b8c4d8SLinus Lüssing 			break;
2738d9b8c4d8SLinus Lüssing 
2739d9b8c4d8SLinus Lüssing 		slot = rlist;
2740d9b8c4d8SLinus Lüssing 	}
2741d9b8c4d8SLinus Lüssing 
2742d9b8c4d8SLinus Lüssing 	return slot;
2743d9b8c4d8SLinus Lüssing }
2744d9b8c4d8SLinus Lüssing 
2745a3c02e76SLinus Lüssing static bool br_multicast_no_router_otherpf(struct net_bridge_port *port,
2746a3c02e76SLinus Lüssing 					   struct hlist_node *rnode)
2747a3c02e76SLinus Lüssing {
2748a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
2749a3c02e76SLinus Lüssing 	if (rnode != &port->ip6_rlist)
2750a3c02e76SLinus Lüssing 		return hlist_unhashed(&port->ip6_rlist);
2751a3c02e76SLinus Lüssing 	else
2752a3c02e76SLinus Lüssing 		return hlist_unhashed(&port->ip4_rlist);
2753a3c02e76SLinus Lüssing #else
2754a3c02e76SLinus Lüssing 	return true;
2755a3c02e76SLinus Lüssing #endif
2756a3c02e76SLinus Lüssing }
2757a3c02e76SLinus Lüssing 
2758d9b8c4d8SLinus Lüssing /* Add port to router_list
27597e80c124Sstephen hemminger  *  list is maintained ordered by pointer value
27607e80c124Sstephen hemminger  *  and locked by br->multicast_lock and RCU
27617e80c124Sstephen hemminger  */
27620909e117SHerbert Xu static void br_multicast_add_router(struct net_bridge *br,
2763d9b8c4d8SLinus Lüssing 				    struct net_bridge_port *port,
2764d9b8c4d8SLinus Lüssing 				    struct hlist_node *rlist,
2765d9b8c4d8SLinus Lüssing 				    struct hlist_head *mc_router_list)
27660909e117SHerbert Xu {
2767d9b8c4d8SLinus Lüssing 	struct hlist_node *slot;
27680909e117SHerbert Xu 
2769d9b8c4d8SLinus Lüssing 	if (!hlist_unhashed(rlist))
27701a040eacSNikolay Aleksandrov 		return;
27711a040eacSNikolay Aleksandrov 
2772d9b8c4d8SLinus Lüssing 	slot = br_multicast_get_rport_slot(br, port, mc_router_list);
27730909e117SHerbert Xu 
27747e80c124Sstephen hemminger 	if (slot)
2775d9b8c4d8SLinus Lüssing 		hlist_add_behind_rcu(rlist, slot);
2776dcdca2c4Sstephen hemminger 	else
2777d9b8c4d8SLinus Lüssing 		hlist_add_head_rcu(rlist, mc_router_list);
2778d9b8c4d8SLinus Lüssing 
2779a3c02e76SLinus Lüssing 	/* For backwards compatibility for now, only notify if we
2780a3c02e76SLinus Lüssing 	 * switched from no IPv4/IPv6 multicast router to a new
2781a3c02e76SLinus Lüssing 	 * IPv4 or IPv6 multicast router.
2782a3c02e76SLinus Lüssing 	 */
2783a3c02e76SLinus Lüssing 	if (br_multicast_no_router_otherpf(port, rlist)) {
2784949f1e39SSatish Ashok 		br_rtr_notify(br->dev, port, RTM_NEWMDB);
27856d549648SNogah Frankel 		br_port_mc_router_state_change(port, true);
27860909e117SHerbert Xu 	}
2787a3c02e76SLinus Lüssing }
27880909e117SHerbert Xu 
2789d9b8c4d8SLinus Lüssing /* Add port to router_list
2790d9b8c4d8SLinus Lüssing  *  list is maintained ordered by pointer value
2791d9b8c4d8SLinus Lüssing  *  and locked by br->multicast_lock and RCU
2792d9b8c4d8SLinus Lüssing  */
2793d9b8c4d8SLinus Lüssing static void br_ip4_multicast_add_router(struct net_bridge *br,
2794eb1d1641SHerbert Xu 					struct net_bridge_port *port)
2795eb1d1641SHerbert Xu {
2796d9b8c4d8SLinus Lüssing 	br_multicast_add_router(br, port, &port->ip4_rlist,
2797d9b8c4d8SLinus Lüssing 				&br->ip4_mc_router_list);
2798d9b8c4d8SLinus Lüssing }
2799d9b8c4d8SLinus Lüssing 
2800a3c02e76SLinus Lüssing /* Add port to router_list
2801a3c02e76SLinus Lüssing  *  list is maintained ordered by pointer value
2802a3c02e76SLinus Lüssing  *  and locked by br->multicast_lock and RCU
2803a3c02e76SLinus Lüssing  */
2804a3c02e76SLinus Lüssing static void br_ip6_multicast_add_router(struct net_bridge *br,
2805a3c02e76SLinus Lüssing 					struct net_bridge_port *port)
2806a3c02e76SLinus Lüssing {
2807a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
2808a3c02e76SLinus Lüssing 	br_multicast_add_router(br, port, &port->ip6_rlist,
2809a3c02e76SLinus Lüssing 				&br->ip6_mc_router_list);
2810a3c02e76SLinus Lüssing #endif
2811a3c02e76SLinus Lüssing }
2812a3c02e76SLinus Lüssing 
2813d9b8c4d8SLinus Lüssing static void br_multicast_mark_router(struct net_bridge *br,
2814d9b8c4d8SLinus Lüssing 				     struct net_bridge_port *port,
2815d9b8c4d8SLinus Lüssing 				     struct timer_list *timer,
2816d9b8c4d8SLinus Lüssing 				     struct hlist_node *rlist,
2817d9b8c4d8SLinus Lüssing 				     struct hlist_head *mc_router_list)
2818d9b8c4d8SLinus Lüssing {
2819eb1d1641SHerbert Xu 	unsigned long now = jiffies;
2820eb1d1641SHerbert Xu 
2821eb1d1641SHerbert Xu 	if (!port) {
282277041420SYotam Gigi 		if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) {
2823d9b8c4d8SLinus Lüssing 			if (!br_ip4_multicast_is_router(br) &&
2824d9b8c4d8SLinus Lüssing 			    !br_ip6_multicast_is_router(br))
282577041420SYotam Gigi 				br_mc_router_state_change(br, true);
2826d9b8c4d8SLinus Lüssing 			mod_timer(timer, now + br->multicast_querier_interval);
282777041420SYotam Gigi 		}
2828eb1d1641SHerbert Xu 		return;
2829eb1d1641SHerbert Xu 	}
2830eb1d1641SHerbert Xu 
2831a55d8246SNikolay Aleksandrov 	if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
2832a55d8246SNikolay Aleksandrov 	    port->multicast_router == MDB_RTR_TYPE_PERM)
2833eb1d1641SHerbert Xu 		return;
2834eb1d1641SHerbert Xu 
2835d9b8c4d8SLinus Lüssing 	br_multicast_add_router(br, port, rlist, mc_router_list);
2836d9b8c4d8SLinus Lüssing 	mod_timer(timer, now + br->multicast_querier_interval);
2837d9b8c4d8SLinus Lüssing }
2838eb1d1641SHerbert Xu 
2839d9b8c4d8SLinus Lüssing static void br_ip4_multicast_mark_router(struct net_bridge *br,
2840d9b8c4d8SLinus Lüssing 					 struct net_bridge_port *port)
2841d9b8c4d8SLinus Lüssing {
2842d9b8c4d8SLinus Lüssing 	struct timer_list *timer = &br->ip4_mc_router_timer;
2843d9b8c4d8SLinus Lüssing 	struct hlist_node *rlist = NULL;
2844d9b8c4d8SLinus Lüssing 
2845d9b8c4d8SLinus Lüssing 	if (port) {
2846d9b8c4d8SLinus Lüssing 		timer = &port->ip4_mc_router_timer;
2847d9b8c4d8SLinus Lüssing 		rlist = &port->ip4_rlist;
2848d9b8c4d8SLinus Lüssing 	}
2849d9b8c4d8SLinus Lüssing 
2850d9b8c4d8SLinus Lüssing 	br_multicast_mark_router(br, port, timer, rlist,
2851d9b8c4d8SLinus Lüssing 				 &br->ip4_mc_router_list);
2852eb1d1641SHerbert Xu }
2853eb1d1641SHerbert Xu 
2854a3c02e76SLinus Lüssing static void br_ip6_multicast_mark_router(struct net_bridge *br,
2855a3c02e76SLinus Lüssing 					 struct net_bridge_port *port)
2856a3c02e76SLinus Lüssing {
2857a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
2858a3c02e76SLinus Lüssing 	struct timer_list *timer = &br->ip6_mc_router_timer;
2859a3c02e76SLinus Lüssing 	struct hlist_node *rlist = NULL;
2860a3c02e76SLinus Lüssing 
2861a3c02e76SLinus Lüssing 	if (port) {
2862a3c02e76SLinus Lüssing 		timer = &port->ip6_mc_router_timer;
2863a3c02e76SLinus Lüssing 		rlist = &port->ip6_rlist;
2864a3c02e76SLinus Lüssing 	}
2865a3c02e76SLinus Lüssing 
2866a3c02e76SLinus Lüssing 	br_multicast_mark_router(br, port, timer, rlist,
2867a3c02e76SLinus Lüssing 				 &br->ip6_mc_router_list);
2868a3c02e76SLinus Lüssing #endif
2869a3c02e76SLinus Lüssing }
2870a3c02e76SLinus Lüssing 
2871b19232efSLinus Lüssing static void
2872b19232efSLinus Lüssing br_ip4_multicast_query_received(struct net_bridge *br,
2873eb1d1641SHerbert Xu 				struct net_bridge_port *port,
287490010b36SLinus Lüssing 				struct bridge_mcast_other_query *query,
2875dc4eb53aSLinus Lüssing 				struct br_ip *saddr,
2876b00589afSLinus Lüssing 				unsigned long max_delay)
2877eb1d1641SHerbert Xu {
2878b19232efSLinus Lüssing 	if (!br_ip4_multicast_select_querier(br, port, saddr->src.ip4))
2879eb1d1641SHerbert Xu 		return;
2880eb1d1641SHerbert Xu 
2881dc4eb53aSLinus Lüssing 	br_multicast_update_query_timer(br, query, max_delay);
2882d9b8c4d8SLinus Lüssing 	br_ip4_multicast_mark_router(br, port);
2883eb1d1641SHerbert Xu }
2884eb1d1641SHerbert Xu 
2885b19232efSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
2886b19232efSLinus Lüssing static void
2887b19232efSLinus Lüssing br_ip6_multicast_query_received(struct net_bridge *br,
2888b19232efSLinus Lüssing 				struct net_bridge_port *port,
2889b19232efSLinus Lüssing 				struct bridge_mcast_other_query *query,
2890b19232efSLinus Lüssing 				struct br_ip *saddr,
2891b19232efSLinus Lüssing 				unsigned long max_delay)
2892b19232efSLinus Lüssing {
2893b19232efSLinus Lüssing 	if (!br_ip6_multicast_select_querier(br, port, &saddr->src.ip6))
2894b19232efSLinus Lüssing 		return;
2895b19232efSLinus Lüssing 
2896b19232efSLinus Lüssing 	br_multicast_update_query_timer(br, query, max_delay);
2897a3c02e76SLinus Lüssing 	br_ip6_multicast_mark_router(br, port);
2898b19232efSLinus Lüssing }
2899b19232efSLinus Lüssing #endif
2900b19232efSLinus Lüssing 
29019c2e955cSzhong jiang static void br_ip4_multicast_query(struct net_bridge *br,
2902eb1d1641SHerbert Xu 				   struct net_bridge_port *port,
290306499098SVlad Yasevich 				   struct sk_buff *skb,
290406499098SVlad Yasevich 				   u16 vid)
2905eb1d1641SHerbert Xu {
2906ba5ea614SLinus Lüssing 	unsigned int transport_len = ip_transport_len(skb);
2907b71d1d42SEric Dumazet 	const struct iphdr *iph = ip_hdr(skb);
2908eb1d1641SHerbert Xu 	struct igmphdr *ih = igmp_hdr(skb);
2909eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
2910eb1d1641SHerbert Xu 	struct igmpv3_query *ih3;
2911eb1d1641SHerbert Xu 	struct net_bridge_port_group *p;
2912e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
2913dc4eb53aSLinus Lüssing 	struct br_ip saddr;
2914eb1d1641SHerbert Xu 	unsigned long max_delay;
2915eb1d1641SHerbert Xu 	unsigned long now = jiffies;
2916eb1d1641SHerbert Xu 	__be32 group;
2917eb1d1641SHerbert Xu 
2918eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
2919eb1d1641SHerbert Xu 	if (!netif_running(br->dev) ||
2920eb1d1641SHerbert Xu 	    (port && port->state == BR_STATE_DISABLED))
2921eb1d1641SHerbert Xu 		goto out;
2922eb1d1641SHerbert Xu 
2923eb1d1641SHerbert Xu 	group = ih->group;
2924eb1d1641SHerbert Xu 
2925ba5ea614SLinus Lüssing 	if (transport_len == sizeof(*ih)) {
2926eb1d1641SHerbert Xu 		max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
2927eb1d1641SHerbert Xu 
2928eb1d1641SHerbert Xu 		if (!max_delay) {
2929eb1d1641SHerbert Xu 			max_delay = 10 * HZ;
2930eb1d1641SHerbert Xu 			group = 0;
2931eb1d1641SHerbert Xu 		}
2932ba5ea614SLinus Lüssing 	} else if (transport_len >= sizeof(*ih3)) {
2933eb1d1641SHerbert Xu 		ih3 = igmpv3_query_hdr(skb);
293423550b83SNikolay Aleksandrov 		if (ih3->nsrcs ||
293523550b83SNikolay Aleksandrov 		    (br->multicast_igmp_version == 3 && group && ih3->suppress))
2936bec68ff1SYOSHIFUJI Hideaki 			goto out;
2937eb1d1641SHerbert Xu 
29380ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 		max_delay = ih3->code ?
29390ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 			    IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
29409afd85c9SLinus Lüssing 	} else {
29419ed973ccSLinus Lüssing 		goto out;
29429ed973ccSLinus Lüssing 	}
29439ed973ccSLinus Lüssing 
2944dc4eb53aSLinus Lüssing 	if (!group) {
2945dc4eb53aSLinus Lüssing 		saddr.proto = htons(ETH_P_IP);
2946deb96566SNikolay Aleksandrov 		saddr.src.ip4 = iph->saddr;
2947b00589afSLinus Lüssing 
2948b19232efSLinus Lüssing 		br_ip4_multicast_query_received(br, port, &br->ip4_other_query,
2949dc4eb53aSLinus Lüssing 						&saddr, max_delay);
2950eb1d1641SHerbert Xu 		goto out;
2951dc4eb53aSLinus Lüssing 	}
2952eb1d1641SHerbert Xu 
295319e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip4_get(br, group, vid);
2954eb1d1641SHerbert Xu 	if (!mp)
2955eb1d1641SHerbert Xu 		goto out;
2956eb1d1641SHerbert Xu 
2957eb1d1641SHerbert Xu 	max_delay *= br->multicast_last_member_count;
2958eb1d1641SHerbert Xu 
2959ff0fd34eSAndrew Lunn 	if (mp->host_joined &&
2960eb1d1641SHerbert Xu 	    (timer_pending(&mp->timer) ?
2961eb1d1641SHerbert Xu 	     time_after(mp->timer.expires, now + max_delay) :
2962eb1d1641SHerbert Xu 	     try_to_del_timer_sync(&mp->timer) >= 0))
2963eb1d1641SHerbert Xu 		mod_timer(&mp->timer, now + max_delay);
2964eb1d1641SHerbert Xu 
2965e8051688SEric Dumazet 	for (pp = &mp->ports;
2966e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
2967e8051688SEric Dumazet 	     pp = &p->next) {
2968eb1d1641SHerbert Xu 		if (timer_pending(&p->timer) ?
2969eb1d1641SHerbert Xu 		    time_after(p->timer.expires, now + max_delay) :
297023550b83SNikolay Aleksandrov 		    try_to_del_timer_sync(&p->timer) >= 0 &&
297123550b83SNikolay Aleksandrov 		    (br->multicast_igmp_version == 2 ||
297223550b83SNikolay Aleksandrov 		     p->filter_mode == MCAST_EXCLUDE))
297324f9cdcbSHerbert Xu 			mod_timer(&p->timer, now + max_delay);
2974eb1d1641SHerbert Xu 	}
2975eb1d1641SHerbert Xu 
2976eb1d1641SHerbert Xu out:
2977eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
2978eb1d1641SHerbert Xu }
2979eb1d1641SHerbert Xu 
2980dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
298108b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_query(struct net_bridge *br,
298208b202b6SYOSHIFUJI Hideaki 				  struct net_bridge_port *port,
298306499098SVlad Yasevich 				  struct sk_buff *skb,
298406499098SVlad Yasevich 				  u16 vid)
298508b202b6SYOSHIFUJI Hideaki {
2986ba5ea614SLinus Lüssing 	unsigned int transport_len = ipv6_transport_len(skb);
2987eca2a43bSstephen hemminger 	struct mld_msg *mld;
298808b202b6SYOSHIFUJI Hideaki 	struct net_bridge_mdb_entry *mp;
298908b202b6SYOSHIFUJI Hideaki 	struct mld2_query *mld2q;
2990e8051688SEric Dumazet 	struct net_bridge_port_group *p;
2991e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
2992dc4eb53aSLinus Lüssing 	struct br_ip saddr;
299308b202b6SYOSHIFUJI Hideaki 	unsigned long max_delay;
299408b202b6SYOSHIFUJI Hideaki 	unsigned long now = jiffies;
2995856ce5d0SLinus Lüssing 	unsigned int offset = skb_transport_offset(skb);
2996b71d1d42SEric Dumazet 	const struct in6_addr *group = NULL;
29979ed973ccSLinus Lüssing 	bool is_general_query;
299808b202b6SYOSHIFUJI Hideaki 	int err = 0;
299908b202b6SYOSHIFUJI Hideaki 
300008b202b6SYOSHIFUJI Hideaki 	spin_lock(&br->multicast_lock);
300108b202b6SYOSHIFUJI Hideaki 	if (!netif_running(br->dev) ||
300208b202b6SYOSHIFUJI Hideaki 	    (port && port->state == BR_STATE_DISABLED))
300308b202b6SYOSHIFUJI Hideaki 		goto out;
300408b202b6SYOSHIFUJI Hideaki 
3005ba5ea614SLinus Lüssing 	if (transport_len == sizeof(*mld)) {
3006856ce5d0SLinus Lüssing 		if (!pskb_may_pull(skb, offset + sizeof(*mld))) {
300708b202b6SYOSHIFUJI Hideaki 			err = -EINVAL;
300808b202b6SYOSHIFUJI Hideaki 			goto out;
300908b202b6SYOSHIFUJI Hideaki 		}
301008b202b6SYOSHIFUJI Hideaki 		mld = (struct mld_msg *) icmp6_hdr(skb);
30114715213dSLi RongQing 		max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
301208b202b6SYOSHIFUJI Hideaki 		if (max_delay)
301308b202b6SYOSHIFUJI Hideaki 			group = &mld->mld_mca;
3014248ba8ecSLinus Lüssing 	} else {
3015856ce5d0SLinus Lüssing 		if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) {
301608b202b6SYOSHIFUJI Hideaki 			err = -EINVAL;
301708b202b6SYOSHIFUJI Hideaki 			goto out;
301808b202b6SYOSHIFUJI Hideaki 		}
301908b202b6SYOSHIFUJI Hideaki 		mld2q = (struct mld2_query *)icmp6_hdr(skb);
302008b202b6SYOSHIFUJI Hideaki 		if (!mld2q->mld2q_nsrcs)
302108b202b6SYOSHIFUJI Hideaki 			group = &mld2q->mld2q_mca;
302223550b83SNikolay Aleksandrov 		if (br->multicast_mld_version == 2 &&
302323550b83SNikolay Aleksandrov 		    !ipv6_addr_any(&mld2q->mld2q_mca) &&
302423550b83SNikolay Aleksandrov 		    mld2q->mld2q_suppress)
302523550b83SNikolay Aleksandrov 			goto out;
3026e3f5b170SDaniel Borkmann 
3027e3f5b170SDaniel Borkmann 		max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
302808b202b6SYOSHIFUJI Hideaki 	}
302908b202b6SYOSHIFUJI Hideaki 
30309ed973ccSLinus Lüssing 	is_general_query = group && ipv6_addr_any(group);
30319ed973ccSLinus Lüssing 
3032dc4eb53aSLinus Lüssing 	if (is_general_query) {
3033dc4eb53aSLinus Lüssing 		saddr.proto = htons(ETH_P_IPV6);
3034deb96566SNikolay Aleksandrov 		saddr.src.ip6 = ipv6_hdr(skb)->saddr;
3035b00589afSLinus Lüssing 
3036b19232efSLinus Lüssing 		br_ip6_multicast_query_received(br, port, &br->ip6_other_query,
3037dc4eb53aSLinus Lüssing 						&saddr, max_delay);
303808b202b6SYOSHIFUJI Hideaki 		goto out;
30396c03ee8bSLinus Lüssing 	} else if (!group) {
30406c03ee8bSLinus Lüssing 		goto out;
3041dc4eb53aSLinus Lüssing 	}
304208b202b6SYOSHIFUJI Hideaki 
304319e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip6_get(br, group, vid);
304408b202b6SYOSHIFUJI Hideaki 	if (!mp)
304508b202b6SYOSHIFUJI Hideaki 		goto out;
304608b202b6SYOSHIFUJI Hideaki 
304708b202b6SYOSHIFUJI Hideaki 	max_delay *= br->multicast_last_member_count;
3048ff0fd34eSAndrew Lunn 	if (mp->host_joined &&
304908b202b6SYOSHIFUJI Hideaki 	    (timer_pending(&mp->timer) ?
305008b202b6SYOSHIFUJI Hideaki 	     time_after(mp->timer.expires, now + max_delay) :
305108b202b6SYOSHIFUJI Hideaki 	     try_to_del_timer_sync(&mp->timer) >= 0))
305208b202b6SYOSHIFUJI Hideaki 		mod_timer(&mp->timer, now + max_delay);
305308b202b6SYOSHIFUJI Hideaki 
3054e8051688SEric Dumazet 	for (pp = &mp->ports;
3055e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
3056e8051688SEric Dumazet 	     pp = &p->next) {
305708b202b6SYOSHIFUJI Hideaki 		if (timer_pending(&p->timer) ?
305808b202b6SYOSHIFUJI Hideaki 		    time_after(p->timer.expires, now + max_delay) :
305923550b83SNikolay Aleksandrov 		    try_to_del_timer_sync(&p->timer) >= 0 &&
306023550b83SNikolay Aleksandrov 		    (br->multicast_mld_version == 1 ||
306123550b83SNikolay Aleksandrov 		     p->filter_mode == MCAST_EXCLUDE))
306224f9cdcbSHerbert Xu 			mod_timer(&p->timer, now + max_delay);
306308b202b6SYOSHIFUJI Hideaki 	}
306408b202b6SYOSHIFUJI Hideaki 
306508b202b6SYOSHIFUJI Hideaki out:
306608b202b6SYOSHIFUJI Hideaki 	spin_unlock(&br->multicast_lock);
306708b202b6SYOSHIFUJI Hideaki 	return err;
306808b202b6SYOSHIFUJI Hideaki }
306908b202b6SYOSHIFUJI Hideaki #endif
307008b202b6SYOSHIFUJI Hideaki 
307190010b36SLinus Lüssing static void
307290010b36SLinus Lüssing br_multicast_leave_group(struct net_bridge *br,
3073eb1d1641SHerbert Xu 			 struct net_bridge_port *port,
3074cc0fdd80SLinus Lüssing 			 struct br_ip *group,
307590010b36SLinus Lüssing 			 struct bridge_mcast_other_query *other_query,
30766db6f0eaSFelix Fietkau 			 struct bridge_mcast_own_query *own_query,
30776db6f0eaSFelix Fietkau 			 const unsigned char *src)
3078eb1d1641SHerbert Xu {
3079eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
3080eb1d1641SHerbert Xu 	struct net_bridge_port_group *p;
3081eb1d1641SHerbert Xu 	unsigned long now;
3082eb1d1641SHerbert Xu 	unsigned long time;
3083eb1d1641SHerbert Xu 
3084eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
3085eb1d1641SHerbert Xu 	if (!netif_running(br->dev) ||
3086544586f7SSatish Ashok 	    (port && port->state == BR_STATE_DISABLED))
3087eb1d1641SHerbert Xu 		goto out;
3088eb1d1641SHerbert Xu 
308919e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip_get(br, group);
3090eb1d1641SHerbert Xu 	if (!mp)
3091eb1d1641SHerbert Xu 		goto out;
3092eb1d1641SHerbert Xu 
3093544586f7SSatish Ashok 	if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
3094544586f7SSatish Ashok 		struct net_bridge_port_group __rcu **pp;
3095544586f7SSatish Ashok 
3096544586f7SSatish Ashok 		for (pp = &mp->ports;
3097544586f7SSatish Ashok 		     (p = mlock_dereference(*pp, br)) != NULL;
3098544586f7SSatish Ashok 		     pp = &p->next) {
30996db6f0eaSFelix Fietkau 			if (!br_port_group_equal(p, port, src))
3100544586f7SSatish Ashok 				continue;
3101544586f7SSatish Ashok 
31025c725b6bSNikolay Aleksandrov 			if (p->flags & MDB_PG_FLAGS_PERMANENT)
31035c725b6bSNikolay Aleksandrov 				break;
31045c725b6bSNikolay Aleksandrov 
3105681590bdSNikolay Aleksandrov 			p->flags |= MDB_PG_FLAGS_FAST_LEAVE;
3106681590bdSNikolay Aleksandrov 			br_multicast_del_pg(mp, p, pp);
3107544586f7SSatish Ashok 		}
3108544586f7SSatish Ashok 		goto out;
3109544586f7SSatish Ashok 	}
3110544586f7SSatish Ashok 
3111544586f7SSatish Ashok 	if (timer_pending(&other_query->timer))
3112544586f7SSatish Ashok 		goto out;
3113544586f7SSatish Ashok 
3114675779adSNikolay Aleksandrov 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER)) {
3115438ef2d0SNikolay Aleksandrov 		__br_multicast_send_query(br, port, NULL, NULL, &mp->addr,
311642c11ccfSNikolay Aleksandrov 					  false, 0, NULL);
31176b7df111SCong Wang 
31186b7df111SCong Wang 		time = jiffies + br->multicast_last_member_count *
31196b7df111SCong Wang 				 br->multicast_last_member_interval;
3120cc0fdd80SLinus Lüssing 
312190010b36SLinus Lüssing 		mod_timer(&own_query->timer, time);
31226b7df111SCong Wang 
31236b7df111SCong Wang 		for (p = mlock_dereference(mp->ports, br);
31246b7df111SCong Wang 		     p != NULL;
31256b7df111SCong Wang 		     p = mlock_dereference(p->next, br)) {
31266db6f0eaSFelix Fietkau 			if (!br_port_group_equal(p, port, src))
31276b7df111SCong Wang 				continue;
31286b7df111SCong Wang 
31296b7df111SCong Wang 			if (!hlist_unhashed(&p->mglist) &&
31306b7df111SCong Wang 			    (timer_pending(&p->timer) ?
31316b7df111SCong Wang 			     time_after(p->timer.expires, time) :
31326b7df111SCong Wang 			     try_to_del_timer_sync(&p->timer) >= 0)) {
31336b7df111SCong Wang 				mod_timer(&p->timer, time);
31346b7df111SCong Wang 			}
31356b7df111SCong Wang 
31366b7df111SCong Wang 			break;
31376b7df111SCong Wang 		}
31386b7df111SCong Wang 	}
31396b7df111SCong Wang 
3140eb1d1641SHerbert Xu 	now = jiffies;
3141eb1d1641SHerbert Xu 	time = now + br->multicast_last_member_count *
3142eb1d1641SHerbert Xu 		     br->multicast_last_member_interval;
3143eb1d1641SHerbert Xu 
3144eb1d1641SHerbert Xu 	if (!port) {
3145ff0fd34eSAndrew Lunn 		if (mp->host_joined &&
3146eb1d1641SHerbert Xu 		    (timer_pending(&mp->timer) ?
3147eb1d1641SHerbert Xu 		     time_after(mp->timer.expires, time) :
3148eb1d1641SHerbert Xu 		     try_to_del_timer_sync(&mp->timer) >= 0)) {
3149eb1d1641SHerbert Xu 			mod_timer(&mp->timer, time);
3150eb1d1641SHerbert Xu 		}
3151454594f3SLinus Lüssing 
3152454594f3SLinus Lüssing 		goto out;
3153454594f3SLinus Lüssing 	}
3154454594f3SLinus Lüssing 
3155454594f3SLinus Lüssing 	for (p = mlock_dereference(mp->ports, br);
3156454594f3SLinus Lüssing 	     p != NULL;
3157454594f3SLinus Lüssing 	     p = mlock_dereference(p->next, br)) {
3158085b53c8SNikolay Aleksandrov 		if (p->key.port != port)
3159454594f3SLinus Lüssing 			continue;
3160454594f3SLinus Lüssing 
3161454594f3SLinus Lüssing 		if (!hlist_unhashed(&p->mglist) &&
3162454594f3SLinus Lüssing 		    (timer_pending(&p->timer) ?
3163454594f3SLinus Lüssing 		     time_after(p->timer.expires, time) :
3164454594f3SLinus Lüssing 		     try_to_del_timer_sync(&p->timer) >= 0)) {
3165454594f3SLinus Lüssing 			mod_timer(&p->timer, time);
3166454594f3SLinus Lüssing 		}
3167454594f3SLinus Lüssing 
3168454594f3SLinus Lüssing 		break;
3169eb1d1641SHerbert Xu 	}
3170eb1d1641SHerbert Xu out:
3171eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
3172eb1d1641SHerbert Xu }
3173eb1d1641SHerbert Xu 
31748ef2a9a5SYOSHIFUJI Hideaki static void br_ip4_multicast_leave_group(struct net_bridge *br,
31758ef2a9a5SYOSHIFUJI Hideaki 					 struct net_bridge_port *port,
3176b0e9a30dSVlad Yasevich 					 __be32 group,
31776db6f0eaSFelix Fietkau 					 __u16 vid,
31786db6f0eaSFelix Fietkau 					 const unsigned char *src)
31798ef2a9a5SYOSHIFUJI Hideaki {
31808ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip br_group;
318190010b36SLinus Lüssing 	struct bridge_mcast_own_query *own_query;
31828ef2a9a5SYOSHIFUJI Hideaki 
31838ef2a9a5SYOSHIFUJI Hideaki 	if (ipv4_is_local_multicast(group))
31848ef2a9a5SYOSHIFUJI Hideaki 		return;
31858ef2a9a5SYOSHIFUJI Hideaki 
318690010b36SLinus Lüssing 	own_query = port ? &port->ip4_own_query : &br->ip4_own_query;
318790010b36SLinus Lüssing 
31881515a63fSNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
3189eab3227bSNikolay Aleksandrov 	br_group.dst.ip4 = group;
31908ef2a9a5SYOSHIFUJI Hideaki 	br_group.proto = htons(ETH_P_IP);
3191b0e9a30dSVlad Yasevich 	br_group.vid = vid;
31928ef2a9a5SYOSHIFUJI Hideaki 
319390010b36SLinus Lüssing 	br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
31946db6f0eaSFelix Fietkau 				 own_query, src);
31958ef2a9a5SYOSHIFUJI Hideaki }
31968ef2a9a5SYOSHIFUJI Hideaki 
3197dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
319808b202b6SYOSHIFUJI Hideaki static void br_ip6_multicast_leave_group(struct net_bridge *br,
319908b202b6SYOSHIFUJI Hideaki 					 struct net_bridge_port *port,
3200b0e9a30dSVlad Yasevich 					 const struct in6_addr *group,
32016db6f0eaSFelix Fietkau 					 __u16 vid,
32026db6f0eaSFelix Fietkau 					 const unsigned char *src)
320308b202b6SYOSHIFUJI Hideaki {
320408b202b6SYOSHIFUJI Hideaki 	struct br_ip br_group;
320590010b36SLinus Lüssing 	struct bridge_mcast_own_query *own_query;
320608b202b6SYOSHIFUJI Hideaki 
32073c3769e6SLinus Lüssing 	if (ipv6_addr_is_ll_all_nodes(group))
320808b202b6SYOSHIFUJI Hideaki 		return;
320908b202b6SYOSHIFUJI Hideaki 
321090010b36SLinus Lüssing 	own_query = port ? &port->ip6_own_query : &br->ip6_own_query;
321190010b36SLinus Lüssing 
32121515a63fSNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
3213eab3227bSNikolay Aleksandrov 	br_group.dst.ip6 = *group;
321408b202b6SYOSHIFUJI Hideaki 	br_group.proto = htons(ETH_P_IPV6);
3215b0e9a30dSVlad Yasevich 	br_group.vid = vid;
321608b202b6SYOSHIFUJI Hideaki 
321790010b36SLinus Lüssing 	br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
32186db6f0eaSFelix Fietkau 				 own_query, src);
321908b202b6SYOSHIFUJI Hideaki }
322008b202b6SYOSHIFUJI Hideaki #endif
32218ef2a9a5SYOSHIFUJI Hideaki 
32221080ab95SNikolay Aleksandrov static void br_multicast_err_count(const struct net_bridge *br,
32231080ab95SNikolay Aleksandrov 				   const struct net_bridge_port *p,
32241080ab95SNikolay Aleksandrov 				   __be16 proto)
32251080ab95SNikolay Aleksandrov {
32261080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
32271080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats *pstats;
32281080ab95SNikolay Aleksandrov 
3229675779adSNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
32301080ab95SNikolay Aleksandrov 		return;
32311080ab95SNikolay Aleksandrov 
32321080ab95SNikolay Aleksandrov 	if (p)
32331080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
32341080ab95SNikolay Aleksandrov 	else
32351080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
32361080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
32371080ab95SNikolay Aleksandrov 		return;
32381080ab95SNikolay Aleksandrov 
32391080ab95SNikolay Aleksandrov 	pstats = this_cpu_ptr(stats);
32401080ab95SNikolay Aleksandrov 
32411080ab95SNikolay Aleksandrov 	u64_stats_update_begin(&pstats->syncp);
32421080ab95SNikolay Aleksandrov 	switch (proto) {
32431080ab95SNikolay Aleksandrov 	case htons(ETH_P_IP):
32441080ab95SNikolay Aleksandrov 		pstats->mstats.igmp_parse_errors++;
32451080ab95SNikolay Aleksandrov 		break;
32461080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
32471080ab95SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
32481080ab95SNikolay Aleksandrov 		pstats->mstats.mld_parse_errors++;
32491080ab95SNikolay Aleksandrov 		break;
32501080ab95SNikolay Aleksandrov #endif
32511080ab95SNikolay Aleksandrov 	}
32521080ab95SNikolay Aleksandrov 	u64_stats_update_end(&pstats->syncp);
32531080ab95SNikolay Aleksandrov }
32541080ab95SNikolay Aleksandrov 
325591b02d3dSNikolay Aleksandrov static void br_multicast_pim(struct net_bridge *br,
325691b02d3dSNikolay Aleksandrov 			     struct net_bridge_port *port,
325791b02d3dSNikolay Aleksandrov 			     const struct sk_buff *skb)
325891b02d3dSNikolay Aleksandrov {
325991b02d3dSNikolay Aleksandrov 	unsigned int offset = skb_transport_offset(skb);
326091b02d3dSNikolay Aleksandrov 	struct pimhdr *pimhdr, _pimhdr;
326191b02d3dSNikolay Aleksandrov 
326291b02d3dSNikolay Aleksandrov 	pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr);
326391b02d3dSNikolay Aleksandrov 	if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION ||
326491b02d3dSNikolay Aleksandrov 	    pim_hdr_type(pimhdr) != PIM_TYPE_HELLO)
326591b02d3dSNikolay Aleksandrov 		return;
326691b02d3dSNikolay Aleksandrov 
3267d9b8c4d8SLinus Lüssing 	br_ip4_multicast_mark_router(br, port);
326891b02d3dSNikolay Aleksandrov }
326991b02d3dSNikolay Aleksandrov 
32704b3087c7SLinus Lüssing static int br_ip4_multicast_mrd_rcv(struct net_bridge *br,
32714b3087c7SLinus Lüssing 				    struct net_bridge_port *port,
32724b3087c7SLinus Lüssing 				    struct sk_buff *skb)
32734b3087c7SLinus Lüssing {
32744b3087c7SLinus Lüssing 	if (ip_hdr(skb)->protocol != IPPROTO_IGMP ||
32754b3087c7SLinus Lüssing 	    igmp_hdr(skb)->type != IGMP_MRDISC_ADV)
32764b3087c7SLinus Lüssing 		return -ENOMSG;
32774b3087c7SLinus Lüssing 
3278d9b8c4d8SLinus Lüssing 	br_ip4_multicast_mark_router(br, port);
32794b3087c7SLinus Lüssing 
32804b3087c7SLinus Lüssing 	return 0;
32814b3087c7SLinus Lüssing }
32824b3087c7SLinus Lüssing 
3283eb1d1641SHerbert Xu static int br_multicast_ipv4_rcv(struct net_bridge *br,
3284eb1d1641SHerbert Xu 				 struct net_bridge_port *port,
328506499098SVlad Yasevich 				 struct sk_buff *skb,
328606499098SVlad Yasevich 				 u16 vid)
3287eb1d1641SHerbert Xu {
32886db6f0eaSFelix Fietkau 	const unsigned char *src;
3289eb1d1641SHerbert Xu 	struct igmphdr *ih;
3290eb1d1641SHerbert Xu 	int err;
3291eb1d1641SHerbert Xu 
3292ba5ea614SLinus Lüssing 	err = ip_mc_check_igmp(skb);
3293eb1d1641SHerbert Xu 
32949afd85c9SLinus Lüssing 	if (err == -ENOMSG) {
329591b02d3dSNikolay Aleksandrov 		if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) {
3296bd4265feSHerbert Xu 			BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
329791b02d3dSNikolay Aleksandrov 		} else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) {
329891b02d3dSNikolay Aleksandrov 			if (ip_hdr(skb)->protocol == IPPROTO_PIM)
329991b02d3dSNikolay Aleksandrov 				br_multicast_pim(br, port, skb);
33004b3087c7SLinus Lüssing 		} else if (ipv4_is_all_snoopers(ip_hdr(skb)->daddr)) {
330108e71623SLi RongQing 			br_ip4_multicast_mrd_rcv(br, port, skb);
33024b3087c7SLinus Lüssing 		}
33034b3087c7SLinus Lüssing 
3304eb1d1641SHerbert Xu 		return 0;
33059afd85c9SLinus Lüssing 	} else if (err < 0) {
33061080ab95SNikolay Aleksandrov 		br_multicast_err_count(br, port, skb->protocol);
33079afd85c9SLinus Lüssing 		return err;
3308bd4265feSHerbert Xu 	}
3309eb1d1641SHerbert Xu 
33109afd85c9SLinus Lüssing 	ih = igmp_hdr(skb);
33116db6f0eaSFelix Fietkau 	src = eth_hdr(skb)->h_source;
33121080ab95SNikolay Aleksandrov 	BR_INPUT_SKB_CB(skb)->igmp = ih->type;
3313eb1d1641SHerbert Xu 
3314eb1d1641SHerbert Xu 	switch (ih->type) {
3315eb1d1641SHerbert Xu 	case IGMP_HOST_MEMBERSHIP_REPORT:
3316eb1d1641SHerbert Xu 	case IGMPV2_HOST_MEMBERSHIP_REPORT:
331762b2bcb4SFernando Luis Vázquez Cao 		BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
33188b671779SNikolay Aleksandrov 		err = br_ip4_multicast_add_group(br, port, ih->group, vid, src,
33198b671779SNikolay Aleksandrov 						 true);
3320eb1d1641SHerbert Xu 		break;
3321eb1d1641SHerbert Xu 	case IGMPV3_HOST_MEMBERSHIP_REPORT:
3322ba5ea614SLinus Lüssing 		err = br_ip4_multicast_igmp3_report(br, port, skb, vid);
3323eb1d1641SHerbert Xu 		break;
3324eb1d1641SHerbert Xu 	case IGMP_HOST_MEMBERSHIP_QUERY:
3325ba5ea614SLinus Lüssing 		br_ip4_multicast_query(br, port, skb, vid);
3326eb1d1641SHerbert Xu 		break;
3327eb1d1641SHerbert Xu 	case IGMP_HOST_LEAVE_MESSAGE:
33286db6f0eaSFelix Fietkau 		br_ip4_multicast_leave_group(br, port, ih->group, vid, src);
3329eb1d1641SHerbert Xu 		break;
3330eb1d1641SHerbert Xu 	}
3331eb1d1641SHerbert Xu 
3332a65056ecSNikolay Aleksandrov 	br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
33331080ab95SNikolay Aleksandrov 			   BR_MCAST_DIR_RX);
33341080ab95SNikolay Aleksandrov 
3335eb1d1641SHerbert Xu 	return err;
3336eb1d1641SHerbert Xu }
3337eb1d1641SHerbert Xu 
3338dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
333999014088SLinus Lüssing static void br_ip6_multicast_mrd_rcv(struct net_bridge *br,
33404b3087c7SLinus Lüssing 				     struct net_bridge_port *port,
33414b3087c7SLinus Lüssing 				     struct sk_buff *skb)
33424b3087c7SLinus Lüssing {
33434b3087c7SLinus Lüssing 	if (icmp6_hdr(skb)->icmp6_type != ICMPV6_MRDISC_ADV)
334499014088SLinus Lüssing 		return;
33454b3087c7SLinus Lüssing 
3346a3c02e76SLinus Lüssing 	br_ip6_multicast_mark_router(br, port);
33474b3087c7SLinus Lüssing }
33484b3087c7SLinus Lüssing 
334908b202b6SYOSHIFUJI Hideaki static int br_multicast_ipv6_rcv(struct net_bridge *br,
335008b202b6SYOSHIFUJI Hideaki 				 struct net_bridge_port *port,
335106499098SVlad Yasevich 				 struct sk_buff *skb,
335206499098SVlad Yasevich 				 u16 vid)
335308b202b6SYOSHIFUJI Hideaki {
33546db6f0eaSFelix Fietkau 	const unsigned char *src;
33559afd85c9SLinus Lüssing 	struct mld_msg *mld;
335608b202b6SYOSHIFUJI Hideaki 	int err;
335708b202b6SYOSHIFUJI Hideaki 
3358ba5ea614SLinus Lüssing 	err = ipv6_mc_check_mld(skb);
335908b202b6SYOSHIFUJI Hideaki 
336099014088SLinus Lüssing 	if (err == -ENOMSG || err == -ENODATA) {
33619afd85c9SLinus Lüssing 		if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
33628fad9c39SLinus Lüssing 			BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
336399014088SLinus Lüssing 		if (err == -ENODATA &&
336499014088SLinus Lüssing 		    ipv6_addr_is_all_snoopers(&ipv6_hdr(skb)->daddr))
336599014088SLinus Lüssing 			br_ip6_multicast_mrd_rcv(br, port, skb);
33664b3087c7SLinus Lüssing 
336708b202b6SYOSHIFUJI Hideaki 		return 0;
33689afd85c9SLinus Lüssing 	} else if (err < 0) {
33691080ab95SNikolay Aleksandrov 		br_multicast_err_count(br, port, skb->protocol);
33709afd85c9SLinus Lüssing 		return err;
337108b202b6SYOSHIFUJI Hideaki 	}
337208b202b6SYOSHIFUJI Hideaki 
33739afd85c9SLinus Lüssing 	mld = (struct mld_msg *)skb_transport_header(skb);
33741080ab95SNikolay Aleksandrov 	BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type;
337508b202b6SYOSHIFUJI Hideaki 
33769afd85c9SLinus Lüssing 	switch (mld->mld_type) {
337708b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_REPORT:
33786db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
3379fc2af6c7SFernando Luis Vázquez Cao 		BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
33806db6f0eaSFelix Fietkau 		err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid,
33818b671779SNikolay Aleksandrov 						 src, true);
338208b202b6SYOSHIFUJI Hideaki 		break;
338308b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MLD2_REPORT:
3384ba5ea614SLinus Lüssing 		err = br_ip6_multicast_mld2_report(br, port, skb, vid);
338508b202b6SYOSHIFUJI Hideaki 		break;
338608b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_QUERY:
3387ba5ea614SLinus Lüssing 		err = br_ip6_multicast_query(br, port, skb, vid);
338808b202b6SYOSHIFUJI Hideaki 		break;
338908b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_REDUCTION:
33906db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
33916db6f0eaSFelix Fietkau 		br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src);
33929afd85c9SLinus Lüssing 		break;
339308b202b6SYOSHIFUJI Hideaki 	}
339408b202b6SYOSHIFUJI Hideaki 
3395a65056ecSNikolay Aleksandrov 	br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
33961080ab95SNikolay Aleksandrov 			   BR_MCAST_DIR_RX);
33971080ab95SNikolay Aleksandrov 
339808b202b6SYOSHIFUJI Hideaki 	return err;
339908b202b6SYOSHIFUJI Hideaki }
340008b202b6SYOSHIFUJI Hideaki #endif
340108b202b6SYOSHIFUJI Hideaki 
3402eb1d1641SHerbert Xu int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
340306499098SVlad Yasevich 		     struct sk_buff *skb, u16 vid)
3404eb1d1641SHerbert Xu {
34051080ab95SNikolay Aleksandrov 	int ret = 0;
34061080ab95SNikolay Aleksandrov 
34071fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 	BR_INPUT_SKB_CB(skb)->igmp = 0;
34081fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 	BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
34091fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 
341013cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
3411eb1d1641SHerbert Xu 		return 0;
3412eb1d1641SHerbert Xu 
3413eb1d1641SHerbert Xu 	switch (skb->protocol) {
3414eb1d1641SHerbert Xu 	case htons(ETH_P_IP):
34151080ab95SNikolay Aleksandrov 		ret = br_multicast_ipv4_rcv(br, port, skb, vid);
34161080ab95SNikolay Aleksandrov 		break;
3417dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
341808b202b6SYOSHIFUJI Hideaki 	case htons(ETH_P_IPV6):
34191080ab95SNikolay Aleksandrov 		ret = br_multicast_ipv6_rcv(br, port, skb, vid);
34201080ab95SNikolay Aleksandrov 		break;
342108b202b6SYOSHIFUJI Hideaki #endif
3422eb1d1641SHerbert Xu 	}
3423eb1d1641SHerbert Xu 
34241080ab95SNikolay Aleksandrov 	return ret;
3425eb1d1641SHerbert Xu }
3426eb1d1641SHerbert Xu 
3427cc0fdd80SLinus Lüssing static void br_multicast_query_expired(struct net_bridge *br,
34282cd41431SLinus Lüssing 				       struct bridge_mcast_own_query *query,
34292cd41431SLinus Lüssing 				       struct bridge_mcast_querier *querier)
3430cc0fdd80SLinus Lüssing {
3431cc0fdd80SLinus Lüssing 	spin_lock(&br->multicast_lock);
3432cc0fdd80SLinus Lüssing 	if (query->startup_sent < br->multicast_startup_query_count)
3433cc0fdd80SLinus Lüssing 		query->startup_sent++;
3434cc0fdd80SLinus Lüssing 
343571d9f614SEric Dumazet 	RCU_INIT_POINTER(querier->port, NULL);
3436cc0fdd80SLinus Lüssing 	br_multicast_send_query(br, NULL, query);
3437cc0fdd80SLinus Lüssing 	spin_unlock(&br->multicast_lock);
3438cc0fdd80SLinus Lüssing }
3439cc0fdd80SLinus Lüssing 
344088c1f37fSAllen Pais static void br_ip4_multicast_query_expired(struct timer_list *t)
3441eb1d1641SHerbert Xu {
344288c1f37fSAllen Pais 	struct net_bridge *br = from_timer(br, t, ip4_own_query.timer);
3443eb1d1641SHerbert Xu 
34442cd41431SLinus Lüssing 	br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier);
3445eb1d1641SHerbert Xu }
3446eb1d1641SHerbert Xu 
3447cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
344888c1f37fSAllen Pais static void br_ip6_multicast_query_expired(struct timer_list *t)
3449cc0fdd80SLinus Lüssing {
345088c1f37fSAllen Pais 	struct net_bridge *br = from_timer(br, t, ip6_own_query.timer);
3451cc0fdd80SLinus Lüssing 
34522cd41431SLinus Lüssing 	br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier);
3453cc0fdd80SLinus Lüssing }
3454cc0fdd80SLinus Lüssing #endif
3455cc0fdd80SLinus Lüssing 
3456e12cec65SNikolay Aleksandrov static void br_multicast_gc_work(struct work_struct *work)
34578b671779SNikolay Aleksandrov {
34588b671779SNikolay Aleksandrov 	struct net_bridge *br = container_of(work, struct net_bridge,
3459e12cec65SNikolay Aleksandrov 					     mcast_gc_work);
34608b671779SNikolay Aleksandrov 	HLIST_HEAD(deleted_head);
34618b671779SNikolay Aleksandrov 
34628b671779SNikolay Aleksandrov 	spin_lock_bh(&br->multicast_lock);
3463e12cec65SNikolay Aleksandrov 	hlist_move_list(&br->mcast_gc_list, &deleted_head);
34648b671779SNikolay Aleksandrov 	spin_unlock_bh(&br->multicast_lock);
34658b671779SNikolay Aleksandrov 
3466e12cec65SNikolay Aleksandrov 	br_multicast_gc(&deleted_head);
34678b671779SNikolay Aleksandrov }
34688b671779SNikolay Aleksandrov 
3469eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br)
3470eb1d1641SHerbert Xu {
3471d08c6bc0SNikolay Aleksandrov 	br->hash_max = BR_MULTICAST_DEFAULT_HASH_MAX;
3472eb1d1641SHerbert Xu 
34737f0aec7aSNikolay Aleksandrov 	br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
3474eb1d1641SHerbert Xu 	br->multicast_last_member_count = 2;
3475eb1d1641SHerbert Xu 	br->multicast_startup_query_count = 2;
3476eb1d1641SHerbert Xu 
3477eb1d1641SHerbert Xu 	br->multicast_last_member_interval = HZ;
3478eb1d1641SHerbert Xu 	br->multicast_query_response_interval = 10 * HZ;
3479eb1d1641SHerbert Xu 	br->multicast_startup_query_interval = 125 * HZ / 4;
3480eb1d1641SHerbert Xu 	br->multicast_query_interval = 125 * HZ;
3481eb1d1641SHerbert Xu 	br->multicast_querier_interval = 255 * HZ;
3482eb1d1641SHerbert Xu 	br->multicast_membership_interval = 260 * HZ;
3483eb1d1641SHerbert Xu 
348490010b36SLinus Lüssing 	br->ip4_other_query.delay_time = 0;
34852cd41431SLinus Lüssing 	br->ip4_querier.port = NULL;
3486aa2ae3e7SNikolay Aleksandrov 	br->multicast_igmp_version = 2;
3487cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3488aa2ae3e7SNikolay Aleksandrov 	br->multicast_mld_version = 1;
348990010b36SLinus Lüssing 	br->ip6_other_query.delay_time = 0;
34902cd41431SLinus Lüssing 	br->ip6_querier.port = NULL;
3491cc0fdd80SLinus Lüssing #endif
34926919622aSIdo Schimmel 	br_opt_toggle(br, BROPT_MULTICAST_ENABLED, true);
3493675779adSNikolay Aleksandrov 	br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true);
3494b00589afSLinus Lüssing 
3495eb1d1641SHerbert Xu 	spin_lock_init(&br->multicast_lock);
3496ce6f7097SLinus Lüssing 	timer_setup(&br->ip4_mc_router_timer,
3497ee5fb222SLinus Lüssing 		    br_ip4_multicast_local_router_expired, 0);
349888c1f37fSAllen Pais 	timer_setup(&br->ip4_other_query.timer,
349988c1f37fSAllen Pais 		    br_ip4_multicast_querier_expired, 0);
350088c1f37fSAllen Pais 	timer_setup(&br->ip4_own_query.timer,
350188c1f37fSAllen Pais 		    br_ip4_multicast_query_expired, 0);
3502cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3503a3c02e76SLinus Lüssing 	timer_setup(&br->ip6_mc_router_timer,
3504a3c02e76SLinus Lüssing 		    br_ip6_multicast_local_router_expired, 0);
350588c1f37fSAllen Pais 	timer_setup(&br->ip6_other_query.timer,
350688c1f37fSAllen Pais 		    br_ip6_multicast_querier_expired, 0);
350788c1f37fSAllen Pais 	timer_setup(&br->ip6_own_query.timer,
350888c1f37fSAllen Pais 		    br_ip6_multicast_query_expired, 0);
3509cc0fdd80SLinus Lüssing #endif
351019e3a9c9SNikolay Aleksandrov 	INIT_HLIST_HEAD(&br->mdb_list);
3511e12cec65SNikolay Aleksandrov 	INIT_HLIST_HEAD(&br->mcast_gc_list);
3512e12cec65SNikolay Aleksandrov 	INIT_WORK(&br->mcast_gc_work, br_multicast_gc_work);
3513eb1d1641SHerbert Xu }
3514eb1d1641SHerbert Xu 
35154effd28cSLinus Lüssing static void br_ip4_multicast_join_snoopers(struct net_bridge *br)
35164effd28cSLinus Lüssing {
35174effd28cSLinus Lüssing 	struct in_device *in_dev = in_dev_get(br->dev);
35184effd28cSLinus Lüssing 
35194effd28cSLinus Lüssing 	if (!in_dev)
35204effd28cSLinus Lüssing 		return;
35214effd28cSLinus Lüssing 
35229fb20801SFlorian Fainelli 	__ip_mc_inc_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
35234effd28cSLinus Lüssing 	in_dev_put(in_dev);
35244effd28cSLinus Lüssing }
35254effd28cSLinus Lüssing 
35264effd28cSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
35274effd28cSLinus Lüssing static void br_ip6_multicast_join_snoopers(struct net_bridge *br)
35284effd28cSLinus Lüssing {
35294effd28cSLinus Lüssing 	struct in6_addr addr;
35304effd28cSLinus Lüssing 
35314effd28cSLinus Lüssing 	ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
35324effd28cSLinus Lüssing 	ipv6_dev_mc_inc(br->dev, &addr);
35334effd28cSLinus Lüssing }
35344effd28cSLinus Lüssing #else
35354effd28cSLinus Lüssing static inline void br_ip6_multicast_join_snoopers(struct net_bridge *br)
35364effd28cSLinus Lüssing {
35374effd28cSLinus Lüssing }
35384effd28cSLinus Lüssing #endif
35394effd28cSLinus Lüssing 
3540851d0a73SJoseph Huang void br_multicast_join_snoopers(struct net_bridge *br)
35414effd28cSLinus Lüssing {
35424effd28cSLinus Lüssing 	br_ip4_multicast_join_snoopers(br);
35434effd28cSLinus Lüssing 	br_ip6_multicast_join_snoopers(br);
35444effd28cSLinus Lüssing }
35454effd28cSLinus Lüssing 
35464effd28cSLinus Lüssing static void br_ip4_multicast_leave_snoopers(struct net_bridge *br)
35474effd28cSLinus Lüssing {
35484effd28cSLinus Lüssing 	struct in_device *in_dev = in_dev_get(br->dev);
35494effd28cSLinus Lüssing 
35504effd28cSLinus Lüssing 	if (WARN_ON(!in_dev))
35514effd28cSLinus Lüssing 		return;
35524effd28cSLinus Lüssing 
35539fb20801SFlorian Fainelli 	__ip_mc_dec_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
35544effd28cSLinus Lüssing 	in_dev_put(in_dev);
35554effd28cSLinus Lüssing }
35564effd28cSLinus Lüssing 
35574effd28cSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
35584effd28cSLinus Lüssing static void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
35594effd28cSLinus Lüssing {
35604effd28cSLinus Lüssing 	struct in6_addr addr;
35614effd28cSLinus Lüssing 
35624effd28cSLinus Lüssing 	ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
35634effd28cSLinus Lüssing 	ipv6_dev_mc_dec(br->dev, &addr);
35644effd28cSLinus Lüssing }
35654effd28cSLinus Lüssing #else
35664effd28cSLinus Lüssing static inline void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
35674effd28cSLinus Lüssing {
35684effd28cSLinus Lüssing }
35694effd28cSLinus Lüssing #endif
35704effd28cSLinus Lüssing 
3571851d0a73SJoseph Huang void br_multicast_leave_snoopers(struct net_bridge *br)
35724effd28cSLinus Lüssing {
35734effd28cSLinus Lüssing 	br_ip4_multicast_leave_snoopers(br);
35744effd28cSLinus Lüssing 	br_ip6_multicast_leave_snoopers(br);
35754effd28cSLinus Lüssing }
35764effd28cSLinus Lüssing 
3577cc0fdd80SLinus Lüssing static void __br_multicast_open(struct net_bridge *br,
357890010b36SLinus Lüssing 				struct bridge_mcast_own_query *query)
3579eb1d1641SHerbert Xu {
3580cc0fdd80SLinus Lüssing 	query->startup_sent = 0;
3581eb1d1641SHerbert Xu 
358213cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
3583eb1d1641SHerbert Xu 		return;
3584eb1d1641SHerbert Xu 
3585cc0fdd80SLinus Lüssing 	mod_timer(&query->timer, jiffies);
3586cc0fdd80SLinus Lüssing }
3587cc0fdd80SLinus Lüssing 
3588cc0fdd80SLinus Lüssing void br_multicast_open(struct net_bridge *br)
3589cc0fdd80SLinus Lüssing {
359090010b36SLinus Lüssing 	__br_multicast_open(br, &br->ip4_own_query);
3591cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
359290010b36SLinus Lüssing 	__br_multicast_open(br, &br->ip6_own_query);
3593cc0fdd80SLinus Lüssing #endif
3594eb1d1641SHerbert Xu }
3595eb1d1641SHerbert Xu 
3596eb1d1641SHerbert Xu void br_multicast_stop(struct net_bridge *br)
3597eb1d1641SHerbert Xu {
3598ce6f7097SLinus Lüssing 	del_timer_sync(&br->ip4_mc_router_timer);
359990010b36SLinus Lüssing 	del_timer_sync(&br->ip4_other_query.timer);
360090010b36SLinus Lüssing 	del_timer_sync(&br->ip4_own_query.timer);
3601cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3602a3c02e76SLinus Lüssing 	del_timer_sync(&br->ip6_mc_router_timer);
360390010b36SLinus Lüssing 	del_timer_sync(&br->ip6_other_query.timer);
360490010b36SLinus Lüssing 	del_timer_sync(&br->ip6_own_query.timer);
3605cc0fdd80SLinus Lüssing #endif
3606e10177abSSatish Ashok }
3607e10177abSSatish Ashok 
3608e10177abSSatish Ashok void br_multicast_dev_del(struct net_bridge *br)
3609e10177abSSatish Ashok {
3610e10177abSSatish Ashok 	struct net_bridge_mdb_entry *mp;
36118b671779SNikolay Aleksandrov 	HLIST_HEAD(deleted_head);
361219e3a9c9SNikolay Aleksandrov 	struct hlist_node *tmp;
3613eb1d1641SHerbert Xu 
3614eb1d1641SHerbert Xu 	spin_lock_bh(&br->multicast_lock);
3615e12cec65SNikolay Aleksandrov 	hlist_for_each_entry_safe(mp, tmp, &br->mdb_list, mdb_node)
3616e12cec65SNikolay Aleksandrov 		br_multicast_del_mdb_entry(mp);
3617e12cec65SNikolay Aleksandrov 	hlist_move_list(&br->mcast_gc_list, &deleted_head);
3618eb1d1641SHerbert Xu 	spin_unlock_bh(&br->multicast_lock);
361919e3a9c9SNikolay Aleksandrov 
3620e12cec65SNikolay Aleksandrov 	br_multicast_gc(&deleted_head);
3621e12cec65SNikolay Aleksandrov 	cancel_work_sync(&br->mcast_gc_work);
36228b671779SNikolay Aleksandrov 
36234329596cSNikolay Aleksandrov 	rcu_barrier();
3624eb1d1641SHerbert Xu }
36250909e117SHerbert Xu 
36260909e117SHerbert Xu int br_multicast_set_router(struct net_bridge *br, unsigned long val)
36270909e117SHerbert Xu {
36286ae4ae8eSLinus Lüssing 	int err = -EINVAL;
36290909e117SHerbert Xu 
36300909e117SHerbert Xu 	spin_lock_bh(&br->multicast_lock);
36310909e117SHerbert Xu 
36320909e117SHerbert Xu 	switch (val) {
36337f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_DISABLED:
36347f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_PERM:
363577041420SYotam Gigi 		br_mc_router_state_change(br, val == MDB_RTR_TYPE_PERM);
3636ce6f7097SLinus Lüssing 		del_timer(&br->ip4_mc_router_timer);
3637a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3638a3c02e76SLinus Lüssing 		del_timer(&br->ip6_mc_router_timer);
3639a3c02e76SLinus Lüssing #endif
364077041420SYotam Gigi 		br->multicast_router = val;
364177041420SYotam Gigi 		err = 0;
364277041420SYotam Gigi 		break;
36437f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP_QUERY:
364477041420SYotam Gigi 		if (br->multicast_router != MDB_RTR_TYPE_TEMP_QUERY)
364577041420SYotam Gigi 			br_mc_router_state_change(br, false);
36460909e117SHerbert Xu 		br->multicast_router = val;
36470909e117SHerbert Xu 		err = 0;
36480909e117SHerbert Xu 		break;
36490909e117SHerbert Xu 	}
36500909e117SHerbert Xu 
36510909e117SHerbert Xu 	spin_unlock_bh(&br->multicast_lock);
36520909e117SHerbert Xu 
36530909e117SHerbert Xu 	return err;
36540909e117SHerbert Xu }
36550909e117SHerbert Xu 
3656ed2d3597SLinus Lüssing static void
3657ed2d3597SLinus Lüssing br_multicast_rport_del_notify(struct net_bridge_port *p, bool deleted)
36587f0aec7aSNikolay Aleksandrov {
3659ed2d3597SLinus Lüssing 	if (!deleted)
36607f0aec7aSNikolay Aleksandrov 		return;
3661ed2d3597SLinus Lüssing 
3662a3c02e76SLinus Lüssing 	/* For backwards compatibility for now, only notify if there is
3663a3c02e76SLinus Lüssing 	 * no multicast router anymore for both IPv4 and IPv6.
3664a3c02e76SLinus Lüssing 	 */
3665a3c02e76SLinus Lüssing 	if (!hlist_unhashed(&p->ip4_rlist))
3666a3c02e76SLinus Lüssing 		return;
3667a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3668a3c02e76SLinus Lüssing 	if (!hlist_unhashed(&p->ip6_rlist))
3669a3c02e76SLinus Lüssing 		return;
3670a3c02e76SLinus Lüssing #endif
3671a3c02e76SLinus Lüssing 
36727f0aec7aSNikolay Aleksandrov 	br_rtr_notify(p->br->dev, p, RTM_DELMDB);
36736d549648SNogah Frankel 	br_port_mc_router_state_change(p, false);
3674f12e7d95SNogah Frankel 
3675f12e7d95SNogah Frankel 	/* don't allow timer refresh */
3676f12e7d95SNogah Frankel 	if (p->multicast_router == MDB_RTR_TYPE_TEMP)
3677f12e7d95SNogah Frankel 		p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
36787f0aec7aSNikolay Aleksandrov }
36797f0aec7aSNikolay Aleksandrov 
36800909e117SHerbert Xu int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
36810909e117SHerbert Xu {
36820909e117SHerbert Xu 	struct net_bridge *br = p->br;
3683a55d8246SNikolay Aleksandrov 	unsigned long now = jiffies;
36846ae4ae8eSLinus Lüssing 	int err = -EINVAL;
3685ed2d3597SLinus Lüssing 	bool del = false;
36860909e117SHerbert Xu 
36870909e117SHerbert Xu 	spin_lock(&br->multicast_lock);
36884950cfd1SNikolay Aleksandrov 	if (p->multicast_router == val) {
3689a55d8246SNikolay Aleksandrov 		/* Refresh the temp router port timer */
3690a3c02e76SLinus Lüssing 		if (p->multicast_router == MDB_RTR_TYPE_TEMP) {
3691ce6f7097SLinus Lüssing 			mod_timer(&p->ip4_mc_router_timer,
3692a55d8246SNikolay Aleksandrov 				  now + br->multicast_querier_interval);
3693a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3694a3c02e76SLinus Lüssing 			mod_timer(&p->ip6_mc_router_timer,
3695a3c02e76SLinus Lüssing 				  now + br->multicast_querier_interval);
3696a3c02e76SLinus Lüssing #endif
3697a3c02e76SLinus Lüssing 		}
36984950cfd1SNikolay Aleksandrov 		err = 0;
36994950cfd1SNikolay Aleksandrov 		goto unlock;
37004950cfd1SNikolay Aleksandrov 	}
37010909e117SHerbert Xu 	switch (val) {
37027f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_DISABLED:
37037f0aec7aSNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_DISABLED;
3704ed2d3597SLinus Lüssing 		del |= br_ip4_multicast_rport_del(p);
3705ce6f7097SLinus Lüssing 		del_timer(&p->ip4_mc_router_timer);
3706a3c02e76SLinus Lüssing 		del |= br_ip6_multicast_rport_del(p);
3707a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3708a3c02e76SLinus Lüssing 		del_timer(&p->ip6_mc_router_timer);
3709a3c02e76SLinus Lüssing #endif
3710ed2d3597SLinus Lüssing 		br_multicast_rport_del_notify(p, del);
37110909e117SHerbert Xu 		break;
37127f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP_QUERY:
37137f0aec7aSNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
3714ed2d3597SLinus Lüssing 		del |= br_ip4_multicast_rport_del(p);
3715a3c02e76SLinus Lüssing 		del |= br_ip6_multicast_rport_del(p);
3716ed2d3597SLinus Lüssing 		br_multicast_rport_del_notify(p, del);
37177f0aec7aSNikolay Aleksandrov 		break;
37187f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_PERM:
37197f0aec7aSNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_PERM;
3720ce6f7097SLinus Lüssing 		del_timer(&p->ip4_mc_router_timer);
3721d9b8c4d8SLinus Lüssing 		br_ip4_multicast_add_router(br, p);
3722a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3723a3c02e76SLinus Lüssing 		del_timer(&p->ip6_mc_router_timer);
3724a3c02e76SLinus Lüssing #endif
3725a3c02e76SLinus Lüssing 		br_ip6_multicast_add_router(br, p);
37260909e117SHerbert Xu 		break;
3727a55d8246SNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP:
3728a55d8246SNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_TEMP;
3729d9b8c4d8SLinus Lüssing 		br_ip4_multicast_mark_router(br, p);
3730a3c02e76SLinus Lüssing 		br_ip6_multicast_mark_router(br, p);
3731a55d8246SNikolay Aleksandrov 		break;
37327f0aec7aSNikolay Aleksandrov 	default:
37337f0aec7aSNikolay Aleksandrov 		goto unlock;
37340909e117SHerbert Xu 	}
37357f0aec7aSNikolay Aleksandrov 	err = 0;
37367f0aec7aSNikolay Aleksandrov unlock:
37370909e117SHerbert Xu 	spin_unlock(&br->multicast_lock);
37380909e117SHerbert Xu 
37390909e117SHerbert Xu 	return err;
37400909e117SHerbert Xu }
3741561f1103SHerbert Xu 
3742cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br,
374390010b36SLinus Lüssing 				       struct bridge_mcast_own_query *query)
3744561f1103SHerbert Xu {
3745561f1103SHerbert Xu 	struct net_bridge_port *port;
374674857216SHerbert Xu 
3747cc0fdd80SLinus Lüssing 	__br_multicast_open(br, query);
374874857216SHerbert Xu 
3749c5b493ceSNikolay Aleksandrov 	rcu_read_lock();
3750c5b493ceSNikolay Aleksandrov 	list_for_each_entry_rcu(port, &br->port_list, list) {
375174857216SHerbert Xu 		if (port->state == BR_STATE_DISABLED ||
375274857216SHerbert Xu 		    port->state == BR_STATE_BLOCKING)
375374857216SHerbert Xu 			continue;
375474857216SHerbert Xu 
375590010b36SLinus Lüssing 		if (query == &br->ip4_own_query)
375690010b36SLinus Lüssing 			br_multicast_enable(&port->ip4_own_query);
3757cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3758cc0fdd80SLinus Lüssing 		else
375990010b36SLinus Lüssing 			br_multicast_enable(&port->ip6_own_query);
3760cc0fdd80SLinus Lüssing #endif
376174857216SHerbert Xu 	}
3762c5b493ceSNikolay Aleksandrov 	rcu_read_unlock();
376374857216SHerbert Xu }
376474857216SHerbert Xu 
3765ae1ea84bSFlorian Fainelli int br_multicast_toggle(struct net_bridge *br, unsigned long val,
3766ae1ea84bSFlorian Fainelli 			struct netlink_ext_ack *extack)
376774857216SHerbert Xu {
37687cb3f921SNikolay Aleksandrov 	struct net_bridge_port *port;
3769851d0a73SJoseph Huang 	bool change_snoopers = false;
3770ae1ea84bSFlorian Fainelli 	int err = 0;
3771561f1103SHerbert Xu 
3772ef5e0d82SAndrey Vagin 	spin_lock_bh(&br->multicast_lock);
377313cefad2SNikolay Aleksandrov 	if (!!br_opt_get(br, BROPT_MULTICAST_ENABLED) == !!val)
3774561f1103SHerbert Xu 		goto unlock;
3775561f1103SHerbert Xu 
3776ae1ea84bSFlorian Fainelli 	err = br_mc_disabled_update(br->dev, val, extack);
3777ae1ea84bSFlorian Fainelli 	if (err == -EOPNOTSUPP)
3778ae1ea84bSFlorian Fainelli 		err = 0;
3779ae1ea84bSFlorian Fainelli 	if (err)
3780ae1ea84bSFlorian Fainelli 		goto unlock;
3781ae1ea84bSFlorian Fainelli 
378213cefad2SNikolay Aleksandrov 	br_opt_toggle(br, BROPT_MULTICAST_ENABLED, !!val);
37834effd28cSLinus Lüssing 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) {
3784851d0a73SJoseph Huang 		change_snoopers = true;
3785561f1103SHerbert Xu 		goto unlock;
37864effd28cSLinus Lüssing 	}
3787561f1103SHerbert Xu 
37883a7fda06SHerbert Xu 	if (!netif_running(br->dev))
37893a7fda06SHerbert Xu 		goto unlock;
37903a7fda06SHerbert Xu 
37917cb3f921SNikolay Aleksandrov 	br_multicast_open(br);
37927cb3f921SNikolay Aleksandrov 	list_for_each_entry(port, &br->port_list, list)
37937cb3f921SNikolay Aleksandrov 		__br_multicast_enable_port(port);
3794561f1103SHerbert Xu 
3795851d0a73SJoseph Huang 	change_snoopers = true;
3796851d0a73SJoseph Huang 
3797561f1103SHerbert Xu unlock:
3798ef5e0d82SAndrey Vagin 	spin_unlock_bh(&br->multicast_lock);
3799561f1103SHerbert Xu 
3800851d0a73SJoseph Huang 	/* br_multicast_join_snoopers has the potential to cause
3801851d0a73SJoseph Huang 	 * an MLD Report/Leave to be delivered to br_multicast_rcv,
3802851d0a73SJoseph Huang 	 * which would in turn call br_multicast_add_group, which would
3803851d0a73SJoseph Huang 	 * attempt to acquire multicast_lock. This function should be
3804851d0a73SJoseph Huang 	 * called after the lock has been released to avoid deadlocks on
3805851d0a73SJoseph Huang 	 * multicast_lock.
3806851d0a73SJoseph Huang 	 *
3807851d0a73SJoseph Huang 	 * br_multicast_leave_snoopers does not have the problem since
3808851d0a73SJoseph Huang 	 * br_multicast_rcv first checks BROPT_MULTICAST_ENABLED, and
3809851d0a73SJoseph Huang 	 * returns without calling br_multicast_ipv4/6_rcv if it's not
3810851d0a73SJoseph Huang 	 * enabled. Moved both functions out just for symmetry.
3811851d0a73SJoseph Huang 	 */
3812851d0a73SJoseph Huang 	if (change_snoopers) {
3813851d0a73SJoseph Huang 		if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
3814851d0a73SJoseph Huang 			br_multicast_join_snoopers(br);
3815851d0a73SJoseph Huang 		else
3816851d0a73SJoseph Huang 			br_multicast_leave_snoopers(br);
3817851d0a73SJoseph Huang 	}
3818851d0a73SJoseph Huang 
3819ae1ea84bSFlorian Fainelli 	return err;
3820561f1103SHerbert Xu }
3821b195167fSHerbert Xu 
38229341b988SIdo Schimmel bool br_multicast_enabled(const struct net_device *dev)
38239341b988SIdo Schimmel {
38249341b988SIdo Schimmel 	struct net_bridge *br = netdev_priv(dev);
38259341b988SIdo Schimmel 
382613cefad2SNikolay Aleksandrov 	return !!br_opt_get(br, BROPT_MULTICAST_ENABLED);
38279341b988SIdo Schimmel }
38289341b988SIdo Schimmel EXPORT_SYMBOL_GPL(br_multicast_enabled);
38299341b988SIdo Schimmel 
38300912bda4SYotam Gigi bool br_multicast_router(const struct net_device *dev)
38310912bda4SYotam Gigi {
38320912bda4SYotam Gigi 	struct net_bridge *br = netdev_priv(dev);
38330912bda4SYotam Gigi 	bool is_router;
38340912bda4SYotam Gigi 
38350912bda4SYotam Gigi 	spin_lock_bh(&br->multicast_lock);
38361a3065a2SLinus Lüssing 	is_router = br_multicast_is_router(br, NULL);
38370912bda4SYotam Gigi 	spin_unlock_bh(&br->multicast_lock);
38380912bda4SYotam Gigi 	return is_router;
38390912bda4SYotam Gigi }
38400912bda4SYotam Gigi EXPORT_SYMBOL_GPL(br_multicast_router);
38410912bda4SYotam Gigi 
3842c5c23260SHerbert Xu int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
3843c5c23260SHerbert Xu {
3844b00589afSLinus Lüssing 	unsigned long max_delay;
3845b00589afSLinus Lüssing 
3846c5c23260SHerbert Xu 	val = !!val;
3847c5c23260SHerbert Xu 
3848c5c23260SHerbert Xu 	spin_lock_bh(&br->multicast_lock);
3849675779adSNikolay Aleksandrov 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER) == val)
3850c5c23260SHerbert Xu 		goto unlock;
3851c5c23260SHerbert Xu 
3852675779adSNikolay Aleksandrov 	br_opt_toggle(br, BROPT_MULTICAST_QUERIER, !!val);
3853b00589afSLinus Lüssing 	if (!val)
3854b00589afSLinus Lüssing 		goto unlock;
3855b00589afSLinus Lüssing 
3856b00589afSLinus Lüssing 	max_delay = br->multicast_query_response_interval;
3857b00589afSLinus Lüssing 
385890010b36SLinus Lüssing 	if (!timer_pending(&br->ip4_other_query.timer))
385990010b36SLinus Lüssing 		br->ip4_other_query.delay_time = jiffies + max_delay;
3860cc0fdd80SLinus Lüssing 
386190010b36SLinus Lüssing 	br_multicast_start_querier(br, &br->ip4_own_query);
3862cc0fdd80SLinus Lüssing 
3863cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
386490010b36SLinus Lüssing 	if (!timer_pending(&br->ip6_other_query.timer))
386590010b36SLinus Lüssing 		br->ip6_other_query.delay_time = jiffies + max_delay;
3866cc0fdd80SLinus Lüssing 
386790010b36SLinus Lüssing 	br_multicast_start_querier(br, &br->ip6_own_query);
3868cc0fdd80SLinus Lüssing #endif
3869c5c23260SHerbert Xu 
3870c5c23260SHerbert Xu unlock:
3871c5c23260SHerbert Xu 	spin_unlock_bh(&br->multicast_lock);
3872c5c23260SHerbert Xu 
3873c5c23260SHerbert Xu 	return 0;
3874c5c23260SHerbert Xu }
3875c5c23260SHerbert Xu 
38765e923585SNikolay Aleksandrov int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val)
38775e923585SNikolay Aleksandrov {
38785e923585SNikolay Aleksandrov 	/* Currently we support only version 2 and 3 */
38795e923585SNikolay Aleksandrov 	switch (val) {
38805e923585SNikolay Aleksandrov 	case 2:
38815e923585SNikolay Aleksandrov 	case 3:
38825e923585SNikolay Aleksandrov 		break;
38835e923585SNikolay Aleksandrov 	default:
38845e923585SNikolay Aleksandrov 		return -EINVAL;
38855e923585SNikolay Aleksandrov 	}
38865e923585SNikolay Aleksandrov 
38875e923585SNikolay Aleksandrov 	spin_lock_bh(&br->multicast_lock);
38885e923585SNikolay Aleksandrov 	br->multicast_igmp_version = val;
38895e923585SNikolay Aleksandrov 	spin_unlock_bh(&br->multicast_lock);
38905e923585SNikolay Aleksandrov 
38915e923585SNikolay Aleksandrov 	return 0;
38925e923585SNikolay Aleksandrov }
38935e923585SNikolay Aleksandrov 
3894aa2ae3e7SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
3895aa2ae3e7SNikolay Aleksandrov int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val)
3896aa2ae3e7SNikolay Aleksandrov {
3897aa2ae3e7SNikolay Aleksandrov 	/* Currently we support version 1 and 2 */
3898aa2ae3e7SNikolay Aleksandrov 	switch (val) {
3899aa2ae3e7SNikolay Aleksandrov 	case 1:
3900aa2ae3e7SNikolay Aleksandrov 	case 2:
3901aa2ae3e7SNikolay Aleksandrov 		break;
3902aa2ae3e7SNikolay Aleksandrov 	default:
3903aa2ae3e7SNikolay Aleksandrov 		return -EINVAL;
3904aa2ae3e7SNikolay Aleksandrov 	}
3905aa2ae3e7SNikolay Aleksandrov 
3906aa2ae3e7SNikolay Aleksandrov 	spin_lock_bh(&br->multicast_lock);
3907aa2ae3e7SNikolay Aleksandrov 	br->multicast_mld_version = val;
3908aa2ae3e7SNikolay Aleksandrov 	spin_unlock_bh(&br->multicast_lock);
3909aa2ae3e7SNikolay Aleksandrov 
3910aa2ae3e7SNikolay Aleksandrov 	return 0;
3911aa2ae3e7SNikolay Aleksandrov }
3912aa2ae3e7SNikolay Aleksandrov #endif
3913aa2ae3e7SNikolay Aleksandrov 
391407f8ac4aSLinus Lüssing /**
391507f8ac4aSLinus Lüssing  * br_multicast_list_adjacent - Returns snooped multicast addresses
391607f8ac4aSLinus Lüssing  * @dev:	The bridge port adjacent to which to retrieve addresses
391707f8ac4aSLinus Lüssing  * @br_ip_list:	The list to store found, snooped multicast IP addresses in
391807f8ac4aSLinus Lüssing  *
391907f8ac4aSLinus Lüssing  * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
392007f8ac4aSLinus Lüssing  * snooping feature on all bridge ports of dev's bridge device, excluding
392107f8ac4aSLinus Lüssing  * the addresses from dev itself.
392207f8ac4aSLinus Lüssing  *
392307f8ac4aSLinus Lüssing  * Returns the number of items added to br_ip_list.
392407f8ac4aSLinus Lüssing  *
392507f8ac4aSLinus Lüssing  * Notes:
392607f8ac4aSLinus Lüssing  * - br_ip_list needs to be initialized by caller
392707f8ac4aSLinus Lüssing  * - br_ip_list might contain duplicates in the end
392807f8ac4aSLinus Lüssing  *   (needs to be taken care of by caller)
392907f8ac4aSLinus Lüssing  * - br_ip_list needs to be freed by caller
393007f8ac4aSLinus Lüssing  */
393107f8ac4aSLinus Lüssing int br_multicast_list_adjacent(struct net_device *dev,
393207f8ac4aSLinus Lüssing 			       struct list_head *br_ip_list)
393307f8ac4aSLinus Lüssing {
393407f8ac4aSLinus Lüssing 	struct net_bridge *br;
393507f8ac4aSLinus Lüssing 	struct net_bridge_port *port;
393607f8ac4aSLinus Lüssing 	struct net_bridge_port_group *group;
393707f8ac4aSLinus Lüssing 	struct br_ip_list *entry;
393807f8ac4aSLinus Lüssing 	int count = 0;
393907f8ac4aSLinus Lüssing 
394007f8ac4aSLinus Lüssing 	rcu_read_lock();
394135f861e3SJulian Wiedmann 	if (!br_ip_list || !netif_is_bridge_port(dev))
394207f8ac4aSLinus Lüssing 		goto unlock;
394307f8ac4aSLinus Lüssing 
394407f8ac4aSLinus Lüssing 	port = br_port_get_rcu(dev);
394507f8ac4aSLinus Lüssing 	if (!port || !port->br)
394607f8ac4aSLinus Lüssing 		goto unlock;
394707f8ac4aSLinus Lüssing 
394807f8ac4aSLinus Lüssing 	br = port->br;
394907f8ac4aSLinus Lüssing 
395007f8ac4aSLinus Lüssing 	list_for_each_entry_rcu(port, &br->port_list, list) {
395107f8ac4aSLinus Lüssing 		if (!port->dev || port->dev == dev)
395207f8ac4aSLinus Lüssing 			continue;
395307f8ac4aSLinus Lüssing 
395407f8ac4aSLinus Lüssing 		hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
395507f8ac4aSLinus Lüssing 			entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
395607f8ac4aSLinus Lüssing 			if (!entry)
395707f8ac4aSLinus Lüssing 				goto unlock;
395807f8ac4aSLinus Lüssing 
3959085b53c8SNikolay Aleksandrov 			entry->addr = group->key.addr;
396007f8ac4aSLinus Lüssing 			list_add(&entry->list, br_ip_list);
396107f8ac4aSLinus Lüssing 			count++;
396207f8ac4aSLinus Lüssing 		}
396307f8ac4aSLinus Lüssing 	}
396407f8ac4aSLinus Lüssing 
396507f8ac4aSLinus Lüssing unlock:
396607f8ac4aSLinus Lüssing 	rcu_read_unlock();
396707f8ac4aSLinus Lüssing 	return count;
396807f8ac4aSLinus Lüssing }
396907f8ac4aSLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
39702cd41431SLinus Lüssing 
39712cd41431SLinus Lüssing /**
3972c34963e2SLinus Lüssing  * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
3973c34963e2SLinus Lüssing  * @dev: The bridge port providing the bridge on which to check for a querier
3974c34963e2SLinus Lüssing  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
3975c34963e2SLinus Lüssing  *
3976c34963e2SLinus Lüssing  * Checks whether the given interface has a bridge on top and if so returns
3977c34963e2SLinus Lüssing  * true if a valid querier exists anywhere on the bridged link layer.
3978c34963e2SLinus Lüssing  * Otherwise returns false.
3979c34963e2SLinus Lüssing  */
3980c34963e2SLinus Lüssing bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
3981c34963e2SLinus Lüssing {
3982c34963e2SLinus Lüssing 	struct net_bridge *br;
3983c34963e2SLinus Lüssing 	struct net_bridge_port *port;
3984c34963e2SLinus Lüssing 	struct ethhdr eth;
3985c34963e2SLinus Lüssing 	bool ret = false;
3986c34963e2SLinus Lüssing 
3987c34963e2SLinus Lüssing 	rcu_read_lock();
398835f861e3SJulian Wiedmann 	if (!netif_is_bridge_port(dev))
3989c34963e2SLinus Lüssing 		goto unlock;
3990c34963e2SLinus Lüssing 
3991c34963e2SLinus Lüssing 	port = br_port_get_rcu(dev);
3992c34963e2SLinus Lüssing 	if (!port || !port->br)
3993c34963e2SLinus Lüssing 		goto unlock;
3994c34963e2SLinus Lüssing 
3995c34963e2SLinus Lüssing 	br = port->br;
3996c34963e2SLinus Lüssing 
3997c34963e2SLinus Lüssing 	memset(&eth, 0, sizeof(eth));
3998c34963e2SLinus Lüssing 	eth.h_proto = htons(proto);
3999c34963e2SLinus Lüssing 
4000955062b0SNikolay Aleksandrov 	ret = br_multicast_querier_exists(br, &eth, NULL);
4001c34963e2SLinus Lüssing 
4002c34963e2SLinus Lüssing unlock:
4003c34963e2SLinus Lüssing 	rcu_read_unlock();
4004c34963e2SLinus Lüssing 	return ret;
4005c34963e2SLinus Lüssing }
4006c34963e2SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
4007c34963e2SLinus Lüssing 
4008c34963e2SLinus Lüssing /**
40092cd41431SLinus Lüssing  * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
40102cd41431SLinus Lüssing  * @dev: The bridge port adjacent to which to check for a querier
40112cd41431SLinus Lüssing  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
40122cd41431SLinus Lüssing  *
40132cd41431SLinus Lüssing  * Checks whether the given interface has a bridge on top and if so returns
40142cd41431SLinus Lüssing  * true if a selected querier is behind one of the other ports of this
40152cd41431SLinus Lüssing  * bridge. Otherwise returns false.
40162cd41431SLinus Lüssing  */
40172cd41431SLinus Lüssing bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
40182cd41431SLinus Lüssing {
40192cd41431SLinus Lüssing 	struct net_bridge *br;
40202cd41431SLinus Lüssing 	struct net_bridge_port *port;
40212cd41431SLinus Lüssing 	bool ret = false;
40222cd41431SLinus Lüssing 
40232cd41431SLinus Lüssing 	rcu_read_lock();
402435f861e3SJulian Wiedmann 	if (!netif_is_bridge_port(dev))
40252cd41431SLinus Lüssing 		goto unlock;
40262cd41431SLinus Lüssing 
40272cd41431SLinus Lüssing 	port = br_port_get_rcu(dev);
40282cd41431SLinus Lüssing 	if (!port || !port->br)
40292cd41431SLinus Lüssing 		goto unlock;
40302cd41431SLinus Lüssing 
40312cd41431SLinus Lüssing 	br = port->br;
40322cd41431SLinus Lüssing 
40332cd41431SLinus Lüssing 	switch (proto) {
40342cd41431SLinus Lüssing 	case ETH_P_IP:
40352cd41431SLinus Lüssing 		if (!timer_pending(&br->ip4_other_query.timer) ||
40362cd41431SLinus Lüssing 		    rcu_dereference(br->ip4_querier.port) == port)
40372cd41431SLinus Lüssing 			goto unlock;
40382cd41431SLinus Lüssing 		break;
40393993c4e1SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
40402cd41431SLinus Lüssing 	case ETH_P_IPV6:
40412cd41431SLinus Lüssing 		if (!timer_pending(&br->ip6_other_query.timer) ||
40422cd41431SLinus Lüssing 		    rcu_dereference(br->ip6_querier.port) == port)
40432cd41431SLinus Lüssing 			goto unlock;
40442cd41431SLinus Lüssing 		break;
40453993c4e1SLinus Lüssing #endif
40462cd41431SLinus Lüssing 	default:
40472cd41431SLinus Lüssing 		goto unlock;
40482cd41431SLinus Lüssing 	}
40492cd41431SLinus Lüssing 
40502cd41431SLinus Lüssing 	ret = true;
40512cd41431SLinus Lüssing unlock:
40522cd41431SLinus Lüssing 	rcu_read_unlock();
40532cd41431SLinus Lüssing 	return ret;
40542cd41431SLinus Lüssing }
40552cd41431SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
40561080ab95SNikolay Aleksandrov 
40573b85f9baSLinus Lüssing /**
40583b85f9baSLinus Lüssing  * br_multicast_has_router_adjacent - Checks for a router behind a bridge port
40593b85f9baSLinus Lüssing  * @dev: The bridge port adjacent to which to check for a multicast router
40603b85f9baSLinus Lüssing  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
40613b85f9baSLinus Lüssing  *
40623b85f9baSLinus Lüssing  * Checks whether the given interface has a bridge on top and if so returns
40633b85f9baSLinus Lüssing  * true if a multicast router is behind one of the other ports of this
40643b85f9baSLinus Lüssing  * bridge. Otherwise returns false.
40653b85f9baSLinus Lüssing  */
40663b85f9baSLinus Lüssing bool br_multicast_has_router_adjacent(struct net_device *dev, int proto)
40673b85f9baSLinus Lüssing {
40683b85f9baSLinus Lüssing 	struct net_bridge_port *port, *p;
40693b85f9baSLinus Lüssing 	bool ret = false;
40703b85f9baSLinus Lüssing 
40713b85f9baSLinus Lüssing 	rcu_read_lock();
40723b85f9baSLinus Lüssing 	port = br_port_get_check_rcu(dev);
40733b85f9baSLinus Lüssing 	if (!port)
40743b85f9baSLinus Lüssing 		goto unlock;
40753b85f9baSLinus Lüssing 
40763b85f9baSLinus Lüssing 	switch (proto) {
40773b85f9baSLinus Lüssing 	case ETH_P_IP:
40783b85f9baSLinus Lüssing 		hlist_for_each_entry_rcu(p, &port->br->ip4_mc_router_list,
40793b85f9baSLinus Lüssing 					 ip4_rlist) {
40803b85f9baSLinus Lüssing 			if (p == port)
40813b85f9baSLinus Lüssing 				continue;
40823b85f9baSLinus Lüssing 
40833b85f9baSLinus Lüssing 			ret = true;
40843b85f9baSLinus Lüssing 			goto unlock;
40853b85f9baSLinus Lüssing 		}
40863b85f9baSLinus Lüssing 		break;
40873b85f9baSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
40883b85f9baSLinus Lüssing 	case ETH_P_IPV6:
40893b85f9baSLinus Lüssing 		hlist_for_each_entry_rcu(p, &port->br->ip6_mc_router_list,
40903b85f9baSLinus Lüssing 					 ip6_rlist) {
40913b85f9baSLinus Lüssing 			if (p == port)
40923b85f9baSLinus Lüssing 				continue;
40933b85f9baSLinus Lüssing 
40943b85f9baSLinus Lüssing 			ret = true;
40953b85f9baSLinus Lüssing 			goto unlock;
40963b85f9baSLinus Lüssing 		}
40973b85f9baSLinus Lüssing 		break;
40983b85f9baSLinus Lüssing #endif
40993b85f9baSLinus Lüssing 	default:
41003b85f9baSLinus Lüssing 		/* when compiled without IPv6 support, be conservative and
41013b85f9baSLinus Lüssing 		 * always assume presence of an IPv6 multicast router
41023b85f9baSLinus Lüssing 		 */
41033b85f9baSLinus Lüssing 		ret = true;
41043b85f9baSLinus Lüssing 	}
41053b85f9baSLinus Lüssing 
41063b85f9baSLinus Lüssing unlock:
41073b85f9baSLinus Lüssing 	rcu_read_unlock();
41083b85f9baSLinus Lüssing 	return ret;
41093b85f9baSLinus Lüssing }
41103b85f9baSLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_router_adjacent);
41113b85f9baSLinus Lüssing 
41121080ab95SNikolay Aleksandrov static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
4113a65056ecSNikolay Aleksandrov 			       const struct sk_buff *skb, u8 type, u8 dir)
41141080ab95SNikolay Aleksandrov {
41151080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
4116a65056ecSNikolay Aleksandrov 	__be16 proto = skb->protocol;
4117a65056ecSNikolay Aleksandrov 	unsigned int t_len;
41181080ab95SNikolay Aleksandrov 
41191080ab95SNikolay Aleksandrov 	u64_stats_update_begin(&pstats->syncp);
41201080ab95SNikolay Aleksandrov 	switch (proto) {
41211080ab95SNikolay Aleksandrov 	case htons(ETH_P_IP):
4122a65056ecSNikolay Aleksandrov 		t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
41231080ab95SNikolay Aleksandrov 		switch (type) {
41241080ab95SNikolay Aleksandrov 		case IGMP_HOST_MEMBERSHIP_REPORT:
41251080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v1reports[dir]++;
41261080ab95SNikolay Aleksandrov 			break;
41271080ab95SNikolay Aleksandrov 		case IGMPV2_HOST_MEMBERSHIP_REPORT:
41281080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v2reports[dir]++;
41291080ab95SNikolay Aleksandrov 			break;
41301080ab95SNikolay Aleksandrov 		case IGMPV3_HOST_MEMBERSHIP_REPORT:
41311080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v3reports[dir]++;
41321080ab95SNikolay Aleksandrov 			break;
41331080ab95SNikolay Aleksandrov 		case IGMP_HOST_MEMBERSHIP_QUERY:
4134a65056ecSNikolay Aleksandrov 			if (t_len != sizeof(struct igmphdr)) {
4135a65056ecSNikolay Aleksandrov 				pstats->mstats.igmp_v3queries[dir]++;
4136a65056ecSNikolay Aleksandrov 			} else {
4137a65056ecSNikolay Aleksandrov 				unsigned int offset = skb_transport_offset(skb);
4138a65056ecSNikolay Aleksandrov 				struct igmphdr *ih, _ihdr;
4139a65056ecSNikolay Aleksandrov 
4140a65056ecSNikolay Aleksandrov 				ih = skb_header_pointer(skb, offset,
4141a65056ecSNikolay Aleksandrov 							sizeof(_ihdr), &_ihdr);
4142a65056ecSNikolay Aleksandrov 				if (!ih)
4143a65056ecSNikolay Aleksandrov 					break;
4144a65056ecSNikolay Aleksandrov 				if (!ih->code)
4145a65056ecSNikolay Aleksandrov 					pstats->mstats.igmp_v1queries[dir]++;
4146a65056ecSNikolay Aleksandrov 				else
4147a65056ecSNikolay Aleksandrov 					pstats->mstats.igmp_v2queries[dir]++;
4148a65056ecSNikolay Aleksandrov 			}
41491080ab95SNikolay Aleksandrov 			break;
41501080ab95SNikolay Aleksandrov 		case IGMP_HOST_LEAVE_MESSAGE:
41511080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_leaves[dir]++;
41521080ab95SNikolay Aleksandrov 			break;
41531080ab95SNikolay Aleksandrov 		}
41541080ab95SNikolay Aleksandrov 		break;
41551080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
41561080ab95SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
4157a65056ecSNikolay Aleksandrov 		t_len = ntohs(ipv6_hdr(skb)->payload_len) +
4158a65056ecSNikolay Aleksandrov 			sizeof(struct ipv6hdr);
4159a65056ecSNikolay Aleksandrov 		t_len -= skb_network_header_len(skb);
41601080ab95SNikolay Aleksandrov 		switch (type) {
41611080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_REPORT:
41621080ab95SNikolay Aleksandrov 			pstats->mstats.mld_v1reports[dir]++;
41631080ab95SNikolay Aleksandrov 			break;
41641080ab95SNikolay Aleksandrov 		case ICMPV6_MLD2_REPORT:
41651080ab95SNikolay Aleksandrov 			pstats->mstats.mld_v2reports[dir]++;
41661080ab95SNikolay Aleksandrov 			break;
41671080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_QUERY:
4168a65056ecSNikolay Aleksandrov 			if (t_len != sizeof(struct mld_msg))
4169a65056ecSNikolay Aleksandrov 				pstats->mstats.mld_v2queries[dir]++;
4170a65056ecSNikolay Aleksandrov 			else
4171a65056ecSNikolay Aleksandrov 				pstats->mstats.mld_v1queries[dir]++;
41721080ab95SNikolay Aleksandrov 			break;
41731080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_REDUCTION:
41741080ab95SNikolay Aleksandrov 			pstats->mstats.mld_leaves[dir]++;
41751080ab95SNikolay Aleksandrov 			break;
41761080ab95SNikolay Aleksandrov 		}
41771080ab95SNikolay Aleksandrov 		break;
41781080ab95SNikolay Aleksandrov #endif /* CONFIG_IPV6 */
41791080ab95SNikolay Aleksandrov 	}
41801080ab95SNikolay Aleksandrov 	u64_stats_update_end(&pstats->syncp);
41811080ab95SNikolay Aleksandrov }
41821080ab95SNikolay Aleksandrov 
41831080ab95SNikolay Aleksandrov void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
4184a65056ecSNikolay Aleksandrov 			const struct sk_buff *skb, u8 type, u8 dir)
41851080ab95SNikolay Aleksandrov {
41861080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
41871080ab95SNikolay Aleksandrov 
41881080ab95SNikolay Aleksandrov 	/* if multicast_disabled is true then igmp type can't be set */
4189675779adSNikolay Aleksandrov 	if (!type || !br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
41901080ab95SNikolay Aleksandrov 		return;
41911080ab95SNikolay Aleksandrov 
41921080ab95SNikolay Aleksandrov 	if (p)
41931080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
41941080ab95SNikolay Aleksandrov 	else
41951080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
41961080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
41971080ab95SNikolay Aleksandrov 		return;
41981080ab95SNikolay Aleksandrov 
4199a65056ecSNikolay Aleksandrov 	br_mcast_stats_add(stats, skb, type, dir);
42001080ab95SNikolay Aleksandrov }
42011080ab95SNikolay Aleksandrov 
42021080ab95SNikolay Aleksandrov int br_multicast_init_stats(struct net_bridge *br)
42031080ab95SNikolay Aleksandrov {
42041080ab95SNikolay Aleksandrov 	br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
42051080ab95SNikolay Aleksandrov 	if (!br->mcast_stats)
42061080ab95SNikolay Aleksandrov 		return -ENOMEM;
42071080ab95SNikolay Aleksandrov 
42081080ab95SNikolay Aleksandrov 	return 0;
42091080ab95SNikolay Aleksandrov }
42101080ab95SNikolay Aleksandrov 
4211b6fe0440SIdo Schimmel void br_multicast_uninit_stats(struct net_bridge *br)
4212b6fe0440SIdo Schimmel {
4213b6fe0440SIdo Schimmel 	free_percpu(br->mcast_stats);
4214b6fe0440SIdo Schimmel }
4215b6fe0440SIdo Schimmel 
4216b3b6a84cSArnd Bergmann /* noinline for https://bugs.llvm.org/show_bug.cgi?id=45802#c9 */
4217b3b6a84cSArnd Bergmann static noinline_for_stack void mcast_stats_add_dir(u64 *dst, u64 *src)
42181080ab95SNikolay Aleksandrov {
42191080ab95SNikolay Aleksandrov 	dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
42201080ab95SNikolay Aleksandrov 	dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
42211080ab95SNikolay Aleksandrov }
42221080ab95SNikolay Aleksandrov 
42231080ab95SNikolay Aleksandrov void br_multicast_get_stats(const struct net_bridge *br,
42241080ab95SNikolay Aleksandrov 			    const struct net_bridge_port *p,
42251080ab95SNikolay Aleksandrov 			    struct br_mcast_stats *dest)
42261080ab95SNikolay Aleksandrov {
42271080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
42281080ab95SNikolay Aleksandrov 	struct br_mcast_stats tdst;
42291080ab95SNikolay Aleksandrov 	int i;
42301080ab95SNikolay Aleksandrov 
42311080ab95SNikolay Aleksandrov 	memset(dest, 0, sizeof(*dest));
42321080ab95SNikolay Aleksandrov 	if (p)
42331080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
42341080ab95SNikolay Aleksandrov 	else
42351080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
42361080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
42371080ab95SNikolay Aleksandrov 		return;
42381080ab95SNikolay Aleksandrov 
42391080ab95SNikolay Aleksandrov 	memset(&tdst, 0, sizeof(tdst));
42401080ab95SNikolay Aleksandrov 	for_each_possible_cpu(i) {
42411080ab95SNikolay Aleksandrov 		struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
42421080ab95SNikolay Aleksandrov 		struct br_mcast_stats temp;
42431080ab95SNikolay Aleksandrov 		unsigned int start;
42441080ab95SNikolay Aleksandrov 
42451080ab95SNikolay Aleksandrov 		do {
42461080ab95SNikolay Aleksandrov 			start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
42471080ab95SNikolay Aleksandrov 			memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
42481080ab95SNikolay Aleksandrov 		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
42491080ab95SNikolay Aleksandrov 
4250a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
4251a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
4252a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
42531080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
42541080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
42551080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
42561080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
42571080ab95SNikolay Aleksandrov 		tdst.igmp_parse_errors += temp.igmp_parse_errors;
42581080ab95SNikolay Aleksandrov 
4259a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
4260a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
42611080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
42621080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
42631080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
42641080ab95SNikolay Aleksandrov 		tdst.mld_parse_errors += temp.mld_parse_errors;
42651080ab95SNikolay Aleksandrov 	}
42661080ab95SNikolay Aleksandrov 	memcpy(dest, &tdst, sizeof(*dest));
42671080ab95SNikolay Aleksandrov }
426819e3a9c9SNikolay Aleksandrov 
426919e3a9c9SNikolay Aleksandrov int br_mdb_hash_init(struct net_bridge *br)
427019e3a9c9SNikolay Aleksandrov {
4271085b53c8SNikolay Aleksandrov 	int err;
4272085b53c8SNikolay Aleksandrov 
4273085b53c8SNikolay Aleksandrov 	err = rhashtable_init(&br->sg_port_tbl, &br_sg_port_rht_params);
4274085b53c8SNikolay Aleksandrov 	if (err)
4275085b53c8SNikolay Aleksandrov 		return err;
4276085b53c8SNikolay Aleksandrov 
4277085b53c8SNikolay Aleksandrov 	err = rhashtable_init(&br->mdb_hash_tbl, &br_mdb_rht_params);
4278085b53c8SNikolay Aleksandrov 	if (err) {
4279085b53c8SNikolay Aleksandrov 		rhashtable_destroy(&br->sg_port_tbl);
4280085b53c8SNikolay Aleksandrov 		return err;
4281085b53c8SNikolay Aleksandrov 	}
4282085b53c8SNikolay Aleksandrov 
4283085b53c8SNikolay Aleksandrov 	return 0;
428419e3a9c9SNikolay Aleksandrov }
428519e3a9c9SNikolay Aleksandrov 
428619e3a9c9SNikolay Aleksandrov void br_mdb_hash_fini(struct net_bridge *br)
428719e3a9c9SNikolay Aleksandrov {
4288085b53c8SNikolay Aleksandrov 	rhashtable_destroy(&br->sg_port_tbl);
428919e3a9c9SNikolay Aleksandrov 	rhashtable_destroy(&br->mdb_hash_tbl);
429019e3a9c9SNikolay Aleksandrov }
4291