xref: /openbmc/linux/net/bridge/br_multicast.c (revision 109865fe12f98509224e29f7c19a11cd9a44110e)
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"
36eb1d1641SHerbert Xu 
3719e3a9c9SNikolay Aleksandrov static const struct rhashtable_params br_mdb_rht_params = {
3819e3a9c9SNikolay Aleksandrov 	.head_offset = offsetof(struct net_bridge_mdb_entry, rhnode),
3919e3a9c9SNikolay Aleksandrov 	.key_offset = offsetof(struct net_bridge_mdb_entry, addr),
4019e3a9c9SNikolay Aleksandrov 	.key_len = sizeof(struct br_ip),
4119e3a9c9SNikolay Aleksandrov 	.automatic_shrinking = true,
4219e3a9c9SNikolay Aleksandrov };
4319e3a9c9SNikolay Aleksandrov 
44cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br,
4590010b36SLinus Lüssing 				       struct bridge_mcast_own_query *query);
46754bc547SSatish Ashok static void br_multicast_add_router(struct net_bridge *br,
47754bc547SSatish Ashok 				    struct net_bridge_port *port);
48bc8c20acSSatish Ashok static void br_ip4_multicast_leave_group(struct net_bridge *br,
49bc8c20acSSatish Ashok 					 struct net_bridge_port *port,
50bc8c20acSSatish Ashok 					 __be32 group,
516db6f0eaSFelix Fietkau 					 __u16 vid,
526db6f0eaSFelix Fietkau 					 const unsigned char *src);
5342c11ccfSNikolay Aleksandrov static void br_multicast_port_group_rexmit(struct timer_list *t);
546db6f0eaSFelix Fietkau 
55f12e7d95SNogah Frankel static void __del_port_router(struct net_bridge_port *p);
56bc8c20acSSatish Ashok #if IS_ENABLED(CONFIG_IPV6)
57bc8c20acSSatish Ashok static void br_ip6_multicast_leave_group(struct net_bridge *br,
58bc8c20acSSatish Ashok 					 struct net_bridge_port *port,
59bc8c20acSSatish Ashok 					 const struct in6_addr *group,
606db6f0eaSFelix Fietkau 					 __u16 vid, const unsigned char *src);
61bc8c20acSSatish Ashok #endif
62c83b8fabSHerbert Xu 
6319e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip_get_rcu(struct net_bridge *br,
64cfd56754SCong Wang 						      struct br_ip *dst)
657f285fa7SHerbert Xu {
6619e3a9c9SNikolay Aleksandrov 	return rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params);
677f285fa7SHerbert Xu }
687f285fa7SHerbert Xu 
6919e3a9c9SNikolay Aleksandrov struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge *br,
7019e3a9c9SNikolay Aleksandrov 					   struct br_ip *dst)
7119e3a9c9SNikolay Aleksandrov {
7219e3a9c9SNikolay Aleksandrov 	struct net_bridge_mdb_entry *ent;
7319e3a9c9SNikolay Aleksandrov 
7419e3a9c9SNikolay Aleksandrov 	lockdep_assert_held_once(&br->multicast_lock);
7519e3a9c9SNikolay Aleksandrov 
7619e3a9c9SNikolay Aleksandrov 	rcu_read_lock();
7719e3a9c9SNikolay Aleksandrov 	ent = rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params);
7819e3a9c9SNikolay Aleksandrov 	rcu_read_unlock();
7919e3a9c9SNikolay Aleksandrov 
8019e3a9c9SNikolay Aleksandrov 	return ent;
8119e3a9c9SNikolay Aleksandrov }
8219e3a9c9SNikolay Aleksandrov 
8319e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip4_get(struct net_bridge *br,
8419e3a9c9SNikolay Aleksandrov 						   __be32 dst, __u16 vid)
85eb1d1641SHerbert Xu {
868ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip br_dst;
870821ec55SHerbert Xu 
8819e3a9c9SNikolay Aleksandrov 	memset(&br_dst, 0, sizeof(br_dst));
898ef2a9a5SYOSHIFUJI Hideaki 	br_dst.u.ip4 = dst;
908ef2a9a5SYOSHIFUJI Hideaki 	br_dst.proto = htons(ETH_P_IP);
91b0e9a30dSVlad Yasevich 	br_dst.vid = vid;
928ef2a9a5SYOSHIFUJI Hideaki 
9319e3a9c9SNikolay Aleksandrov 	return br_mdb_ip_get(br, &br_dst);
948ef2a9a5SYOSHIFUJI Hideaki }
958ef2a9a5SYOSHIFUJI Hideaki 
96dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
9719e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip6_get(struct net_bridge *br,
9819e3a9c9SNikolay Aleksandrov 						   const struct in6_addr *dst,
99b0e9a30dSVlad Yasevich 						   __u16 vid)
10008b202b6SYOSHIFUJI Hideaki {
10108b202b6SYOSHIFUJI Hideaki 	struct br_ip br_dst;
10208b202b6SYOSHIFUJI Hideaki 
10319e3a9c9SNikolay Aleksandrov 	memset(&br_dst, 0, sizeof(br_dst));
1044e3fd7a0SAlexey Dobriyan 	br_dst.u.ip6 = *dst;
10508b202b6SYOSHIFUJI Hideaki 	br_dst.proto = htons(ETH_P_IPV6);
106b0e9a30dSVlad Yasevich 	br_dst.vid = vid;
10708b202b6SYOSHIFUJI Hideaki 
10819e3a9c9SNikolay Aleksandrov 	return br_mdb_ip_get(br, &br_dst);
10908b202b6SYOSHIFUJI Hideaki }
11008b202b6SYOSHIFUJI Hideaki #endif
11108b202b6SYOSHIFUJI Hideaki 
112eb1d1641SHerbert Xu struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
113fbca58a2SCong Wang 					struct sk_buff *skb, u16 vid)
114eb1d1641SHerbert Xu {
1158ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip ip;
1168ef2a9a5SYOSHIFUJI Hideaki 
11713cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
118eb1d1641SHerbert Xu 		return NULL;
119eb1d1641SHerbert Xu 
1208ef2a9a5SYOSHIFUJI Hideaki 	if (BR_INPUT_SKB_CB(skb)->igmp)
1218ef2a9a5SYOSHIFUJI Hideaki 		return NULL;
1228ef2a9a5SYOSHIFUJI Hideaki 
12319e3a9c9SNikolay Aleksandrov 	memset(&ip, 0, sizeof(ip));
1248ef2a9a5SYOSHIFUJI Hideaki 	ip.proto = skb->protocol;
125fbca58a2SCong Wang 	ip.vid = vid;
1268ef2a9a5SYOSHIFUJI Hideaki 
127eb1d1641SHerbert Xu 	switch (skb->protocol) {
128eb1d1641SHerbert Xu 	case htons(ETH_P_IP):
1298ef2a9a5SYOSHIFUJI Hideaki 		ip.u.ip4 = ip_hdr(skb)->daddr;
130eb1d1641SHerbert Xu 		break;
131dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
13208b202b6SYOSHIFUJI Hideaki 	case htons(ETH_P_IPV6):
1334e3fd7a0SAlexey Dobriyan 		ip.u.ip6 = ipv6_hdr(skb)->daddr;
13408b202b6SYOSHIFUJI Hideaki 		break;
13508b202b6SYOSHIFUJI Hideaki #endif
1368ef2a9a5SYOSHIFUJI Hideaki 	default:
1378ef2a9a5SYOSHIFUJI Hideaki 		return NULL;
138eb1d1641SHerbert Xu 	}
139eb1d1641SHerbert Xu 
14019e3a9c9SNikolay Aleksandrov 	return br_mdb_ip_get_rcu(br, &ip);
141eb1d1641SHerbert Xu }
142eb1d1641SHerbert Xu 
14388c1f37fSAllen Pais static void br_multicast_group_expired(struct timer_list *t)
144eb1d1641SHerbert Xu {
14588c1f37fSAllen Pais 	struct net_bridge_mdb_entry *mp = from_timer(mp, t, timer);
146eb1d1641SHerbert Xu 	struct net_bridge *br = mp->br;
147eb1d1641SHerbert Xu 
148eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
149eb1d1641SHerbert Xu 	if (!netif_running(br->dev) || timer_pending(&mp->timer))
150eb1d1641SHerbert Xu 		goto out;
151eb1d1641SHerbert Xu 
1521bc844eeSNikolay Aleksandrov 	br_multicast_host_leave(mp, true);
153eb1d1641SHerbert Xu 
154eb1d1641SHerbert Xu 	if (mp->ports)
155eb1d1641SHerbert Xu 		goto out;
156eb1d1641SHerbert Xu 
15719e3a9c9SNikolay Aleksandrov 	rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode,
15819e3a9c9SNikolay Aleksandrov 			       br_mdb_rht_params);
15919e3a9c9SNikolay Aleksandrov 	hlist_del_rcu(&mp->mdb_node);
160eb1d1641SHerbert Xu 
1614329596cSNikolay Aleksandrov 	kfree_rcu(mp, rcu);
162eb1d1641SHerbert Xu 
163eb1d1641SHerbert Xu out:
164eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
165eb1d1641SHerbert Xu }
166eb1d1641SHerbert Xu 
1678b671779SNikolay Aleksandrov static void br_multicast_del_group_src(struct net_bridge_group_src *src)
1688b671779SNikolay Aleksandrov {
1698b671779SNikolay Aleksandrov 	struct net_bridge *br = src->pg->port->br;
1708b671779SNikolay Aleksandrov 
1718b671779SNikolay Aleksandrov 	hlist_del_init_rcu(&src->node);
1728b671779SNikolay Aleksandrov 	src->pg->src_ents--;
1738b671779SNikolay Aleksandrov 	hlist_add_head(&src->del_node, &br->src_gc_list);
1748b671779SNikolay Aleksandrov 	queue_work(system_long_wq, &br->src_gc_work);
1758b671779SNikolay Aleksandrov }
1768b671779SNikolay Aleksandrov 
177681590bdSNikolay Aleksandrov void br_multicast_del_pg(struct net_bridge_mdb_entry *mp,
178681590bdSNikolay Aleksandrov 			 struct net_bridge_port_group *pg,
179681590bdSNikolay Aleksandrov 			 struct net_bridge_port_group __rcu **pp)
180681590bdSNikolay Aleksandrov {
181681590bdSNikolay Aleksandrov 	struct net_bridge *br = pg->port->br;
1828b671779SNikolay Aleksandrov 	struct net_bridge_group_src *ent;
1838b671779SNikolay Aleksandrov 	struct hlist_node *tmp;
184681590bdSNikolay Aleksandrov 
185681590bdSNikolay Aleksandrov 	rcu_assign_pointer(*pp, pg->next);
186681590bdSNikolay Aleksandrov 	hlist_del_init(&pg->mglist);
187681590bdSNikolay Aleksandrov 	del_timer(&pg->timer);
18842c11ccfSNikolay Aleksandrov 	del_timer(&pg->rexmit_timer);
1898b671779SNikolay Aleksandrov 	hlist_for_each_entry_safe(ent, tmp, &pg->src_list, node)
1908b671779SNikolay Aleksandrov 		br_multicast_del_group_src(ent);
19181f19838SNikolay Aleksandrov 	br_mdb_notify(br->dev, mp, pg, RTM_DELMDB);
192681590bdSNikolay Aleksandrov 	kfree_rcu(pg, rcu);
193681590bdSNikolay Aleksandrov 
194681590bdSNikolay Aleksandrov 	if (!mp->ports && !mp->host_joined && netif_running(br->dev))
195681590bdSNikolay Aleksandrov 		mod_timer(&mp->timer, jiffies);
196681590bdSNikolay Aleksandrov }
197681590bdSNikolay Aleksandrov 
198681590bdSNikolay Aleksandrov static void br_multicast_find_del_pg(struct net_bridge *br,
199eb1d1641SHerbert Xu 				     struct net_bridge_port_group *pg)
200eb1d1641SHerbert Xu {
2018b671779SNikolay Aleksandrov 	struct net_bridge_port_group __rcu **pp;
202eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
203eb1d1641SHerbert Xu 	struct net_bridge_port_group *p;
204e8051688SEric Dumazet 
20519e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip_get(br, &pg->addr);
206eb1d1641SHerbert Xu 	if (WARN_ON(!mp))
207eb1d1641SHerbert Xu 		return;
208eb1d1641SHerbert Xu 
209e8051688SEric Dumazet 	for (pp = &mp->ports;
210e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
211e8051688SEric Dumazet 	     pp = &p->next) {
212eb1d1641SHerbert Xu 		if (p != pg)
213eb1d1641SHerbert Xu 			continue;
214eb1d1641SHerbert Xu 
215681590bdSNikolay Aleksandrov 		br_multicast_del_pg(mp, pg, pp);
216eb1d1641SHerbert Xu 		return;
217eb1d1641SHerbert Xu 	}
218eb1d1641SHerbert Xu 
219eb1d1641SHerbert Xu 	WARN_ON(1);
220eb1d1641SHerbert Xu }
221eb1d1641SHerbert Xu 
22288c1f37fSAllen Pais static void br_multicast_port_group_expired(struct timer_list *t)
223eb1d1641SHerbert Xu {
22488c1f37fSAllen Pais 	struct net_bridge_port_group *pg = from_timer(pg, t, timer);
225d6c33d67SNikolay Aleksandrov 	struct net_bridge_group_src *src_ent;
226eb1d1641SHerbert Xu 	struct net_bridge *br = pg->port->br;
227d6c33d67SNikolay Aleksandrov 	struct hlist_node *tmp;
228d6c33d67SNikolay Aleksandrov 	bool changed;
229eb1d1641SHerbert Xu 
230eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
231eb1d1641SHerbert Xu 	if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
2329d06b6d8SElad Raz 	    hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT)
233eb1d1641SHerbert Xu 		goto out;
234eb1d1641SHerbert Xu 
235d6c33d67SNikolay Aleksandrov 	changed = !!(pg->filter_mode == MCAST_EXCLUDE);
236d6c33d67SNikolay Aleksandrov 	pg->filter_mode = MCAST_INCLUDE;
237d6c33d67SNikolay Aleksandrov 	hlist_for_each_entry_safe(src_ent, tmp, &pg->src_list, node) {
238d6c33d67SNikolay Aleksandrov 		if (!timer_pending(&src_ent->timer)) {
239d6c33d67SNikolay Aleksandrov 			br_multicast_del_group_src(src_ent);
240d6c33d67SNikolay Aleksandrov 			changed = true;
241d6c33d67SNikolay Aleksandrov 		}
242d6c33d67SNikolay Aleksandrov 	}
243eb1d1641SHerbert Xu 
244d6c33d67SNikolay Aleksandrov 	if (hlist_empty(&pg->src_list)) {
245d6c33d67SNikolay Aleksandrov 		br_multicast_find_del_pg(br, pg);
246d6c33d67SNikolay Aleksandrov 	} else if (changed) {
247d6c33d67SNikolay Aleksandrov 		struct net_bridge_mdb_entry *mp = br_mdb_ip_get(br, &pg->addr);
248d6c33d67SNikolay Aleksandrov 
249d6c33d67SNikolay Aleksandrov 		if (WARN_ON(!mp))
250d6c33d67SNikolay Aleksandrov 			goto out;
251d6c33d67SNikolay Aleksandrov 		br_mdb_notify(br->dev, mp, pg, RTM_NEWMDB);
252d6c33d67SNikolay Aleksandrov 	}
253eb1d1641SHerbert Xu out:
254eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
255eb1d1641SHerbert Xu }
256eb1d1641SHerbert Xu 
2578ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
258438ef2d0SNikolay Aleksandrov 						    struct net_bridge_port_group *pg,
259438ef2d0SNikolay Aleksandrov 						    __be32 ip_dst, __be32 group,
260438ef2d0SNikolay Aleksandrov 						    bool with_srcs, bool over_lmqt,
26142c11ccfSNikolay Aleksandrov 						    u8 sflag, u8 *igmp_type,
26242c11ccfSNikolay Aleksandrov 						    bool *need_rexmit)
263eb1d1641SHerbert Xu {
264438ef2d0SNikolay Aleksandrov 	struct net_bridge_port *p = pg ? pg->port : NULL;
265438ef2d0SNikolay Aleksandrov 	struct net_bridge_group_src *ent;
266438ef2d0SNikolay Aleksandrov 	size_t pkt_size, igmp_hdr_size;
267438ef2d0SNikolay Aleksandrov 	unsigned long now = jiffies;
2685e923585SNikolay Aleksandrov 	struct igmpv3_query *ihv3;
269438ef2d0SNikolay Aleksandrov 	void *csum_start = NULL;
270438ef2d0SNikolay Aleksandrov 	__sum16 *csum = NULL;
271eb1d1641SHerbert Xu 	struct sk_buff *skb;
272eb1d1641SHerbert Xu 	struct igmphdr *ih;
273eb1d1641SHerbert Xu 	struct ethhdr *eth;
274438ef2d0SNikolay Aleksandrov 	unsigned long lmqt;
275eb1d1641SHerbert Xu 	struct iphdr *iph;
276438ef2d0SNikolay Aleksandrov 	u16 lmqt_srcs = 0;
277eb1d1641SHerbert Xu 
2785e923585SNikolay Aleksandrov 	igmp_hdr_size = sizeof(*ih);
279438ef2d0SNikolay Aleksandrov 	if (br->multicast_igmp_version == 3) {
2805e923585SNikolay Aleksandrov 		igmp_hdr_size = sizeof(*ihv3);
281438ef2d0SNikolay Aleksandrov 		if (pg && with_srcs) {
282438ef2d0SNikolay Aleksandrov 			lmqt = now + (br->multicast_last_member_interval *
283438ef2d0SNikolay Aleksandrov 				      br->multicast_last_member_count);
284438ef2d0SNikolay Aleksandrov 			hlist_for_each_entry(ent, &pg->src_list, node) {
285438ef2d0SNikolay Aleksandrov 				if (over_lmqt == time_after(ent->timer.expires,
286438ef2d0SNikolay Aleksandrov 							    lmqt) &&
287438ef2d0SNikolay Aleksandrov 				    ent->src_query_rexmit_cnt > 0)
288438ef2d0SNikolay Aleksandrov 					lmqt_srcs++;
289438ef2d0SNikolay Aleksandrov 			}
290438ef2d0SNikolay Aleksandrov 
291438ef2d0SNikolay Aleksandrov 			if (!lmqt_srcs)
292438ef2d0SNikolay Aleksandrov 				return NULL;
293438ef2d0SNikolay Aleksandrov 			igmp_hdr_size += lmqt_srcs * sizeof(__be32);
294438ef2d0SNikolay Aleksandrov 		}
295438ef2d0SNikolay Aleksandrov 	}
296438ef2d0SNikolay Aleksandrov 
297438ef2d0SNikolay Aleksandrov 	pkt_size = sizeof(*eth) + sizeof(*iph) + 4 + igmp_hdr_size;
298438ef2d0SNikolay Aleksandrov 	if ((p && pkt_size > p->dev->mtu) ||
299438ef2d0SNikolay Aleksandrov 	    pkt_size > br->dev->mtu)
300438ef2d0SNikolay Aleksandrov 		return NULL;
301438ef2d0SNikolay Aleksandrov 
302438ef2d0SNikolay Aleksandrov 	skb = netdev_alloc_skb_ip_align(br->dev, pkt_size);
303eb1d1641SHerbert Xu 	if (!skb)
304eb1d1641SHerbert Xu 		goto out;
305eb1d1641SHerbert Xu 
306eb1d1641SHerbert Xu 	skb->protocol = htons(ETH_P_IP);
307eb1d1641SHerbert Xu 
308eb1d1641SHerbert Xu 	skb_reset_mac_header(skb);
309eb1d1641SHerbert Xu 	eth = eth_hdr(skb);
310eb1d1641SHerbert Xu 
311e5a727f6SJoe Perches 	ether_addr_copy(eth->h_source, br->dev->dev_addr);
312438ef2d0SNikolay Aleksandrov 	ip_eth_mc_map(ip_dst, eth->h_dest);
313eb1d1641SHerbert Xu 	eth->h_proto = htons(ETH_P_IP);
314eb1d1641SHerbert Xu 	skb_put(skb, sizeof(*eth));
315eb1d1641SHerbert Xu 
316eb1d1641SHerbert Xu 	skb_set_network_header(skb, skb->len);
317eb1d1641SHerbert Xu 	iph = ip_hdr(skb);
318438ef2d0SNikolay Aleksandrov 	iph->tot_len = htons(pkt_size - sizeof(*eth));
319eb1d1641SHerbert Xu 
320eb1d1641SHerbert Xu 	iph->version = 4;
321eb1d1641SHerbert Xu 	iph->ihl = 6;
322eb1d1641SHerbert Xu 	iph->tos = 0xc0;
323eb1d1641SHerbert Xu 	iph->id = 0;
324eb1d1641SHerbert Xu 	iph->frag_off = htons(IP_DF);
325eb1d1641SHerbert Xu 	iph->ttl = 1;
326eb1d1641SHerbert Xu 	iph->protocol = IPPROTO_IGMP;
327675779adSNikolay Aleksandrov 	iph->saddr = br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR) ?
3281c8ad5bfSCong Wang 		     inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0;
329438ef2d0SNikolay Aleksandrov 	iph->daddr = ip_dst;
330eb1d1641SHerbert Xu 	((u8 *)&iph[1])[0] = IPOPT_RA;
331eb1d1641SHerbert Xu 	((u8 *)&iph[1])[1] = 4;
332eb1d1641SHerbert Xu 	((u8 *)&iph[1])[2] = 0;
333eb1d1641SHerbert Xu 	((u8 *)&iph[1])[3] = 0;
334eb1d1641SHerbert Xu 	ip_send_check(iph);
335eb1d1641SHerbert Xu 	skb_put(skb, 24);
336eb1d1641SHerbert Xu 
337eb1d1641SHerbert Xu 	skb_set_transport_header(skb, skb->len);
3381080ab95SNikolay Aleksandrov 	*igmp_type = IGMP_HOST_MEMBERSHIP_QUERY;
3395e923585SNikolay Aleksandrov 
3405e923585SNikolay Aleksandrov 	switch (br->multicast_igmp_version) {
3415e923585SNikolay Aleksandrov 	case 2:
3425e923585SNikolay Aleksandrov 		ih = igmp_hdr(skb);
343eb1d1641SHerbert Xu 		ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
344eb1d1641SHerbert Xu 		ih->code = (group ? br->multicast_last_member_interval :
345eb1d1641SHerbert Xu 				    br->multicast_query_response_interval) /
346eb1d1641SHerbert Xu 			   (HZ / IGMP_TIMER_SCALE);
347eb1d1641SHerbert Xu 		ih->group = group;
348eb1d1641SHerbert Xu 		ih->csum = 0;
349438ef2d0SNikolay Aleksandrov 		csum = &ih->csum;
350438ef2d0SNikolay Aleksandrov 		csum_start = (void *)ih;
3515e923585SNikolay Aleksandrov 		break;
3525e923585SNikolay Aleksandrov 	case 3:
3535e923585SNikolay Aleksandrov 		ihv3 = igmpv3_query_hdr(skb);
3545e923585SNikolay Aleksandrov 		ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY;
3555e923585SNikolay Aleksandrov 		ihv3->code = (group ? br->multicast_last_member_interval :
3565e923585SNikolay Aleksandrov 				      br->multicast_query_response_interval) /
3575e923585SNikolay Aleksandrov 			     (HZ / IGMP_TIMER_SCALE);
3585e923585SNikolay Aleksandrov 		ihv3->group = group;
3595e923585SNikolay Aleksandrov 		ihv3->qqic = br->multicast_query_interval / HZ;
360438ef2d0SNikolay Aleksandrov 		ihv3->nsrcs = htons(lmqt_srcs);
3615e923585SNikolay Aleksandrov 		ihv3->resv = 0;
362438ef2d0SNikolay Aleksandrov 		ihv3->suppress = sflag;
3635e923585SNikolay Aleksandrov 		ihv3->qrv = 2;
3645e923585SNikolay Aleksandrov 		ihv3->csum = 0;
365438ef2d0SNikolay Aleksandrov 		csum = &ihv3->csum;
366438ef2d0SNikolay Aleksandrov 		csum_start = (void *)ihv3;
367438ef2d0SNikolay Aleksandrov 		if (!pg || !with_srcs)
368438ef2d0SNikolay Aleksandrov 			break;
369438ef2d0SNikolay Aleksandrov 
370438ef2d0SNikolay Aleksandrov 		lmqt_srcs = 0;
371438ef2d0SNikolay Aleksandrov 		hlist_for_each_entry(ent, &pg->src_list, node) {
372438ef2d0SNikolay Aleksandrov 			if (over_lmqt == time_after(ent->timer.expires,
373438ef2d0SNikolay Aleksandrov 						    lmqt) &&
374438ef2d0SNikolay Aleksandrov 			    ent->src_query_rexmit_cnt > 0) {
375438ef2d0SNikolay Aleksandrov 				ihv3->srcs[lmqt_srcs++] = ent->addr.u.ip4;
376438ef2d0SNikolay Aleksandrov 				ent->src_query_rexmit_cnt--;
37742c11ccfSNikolay Aleksandrov 				if (need_rexmit && ent->src_query_rexmit_cnt)
37842c11ccfSNikolay Aleksandrov 					*need_rexmit = true;
379438ef2d0SNikolay Aleksandrov 			}
380438ef2d0SNikolay Aleksandrov 		}
381438ef2d0SNikolay Aleksandrov 		if (WARN_ON(lmqt_srcs != ntohs(ihv3->nsrcs))) {
382438ef2d0SNikolay Aleksandrov 			kfree_skb(skb);
383438ef2d0SNikolay Aleksandrov 			return NULL;
384438ef2d0SNikolay Aleksandrov 		}
3855e923585SNikolay Aleksandrov 		break;
3865e923585SNikolay Aleksandrov 	}
387eb1d1641SHerbert Xu 
388438ef2d0SNikolay Aleksandrov 	if (WARN_ON(!csum || !csum_start)) {
389438ef2d0SNikolay Aleksandrov 		kfree_skb(skb);
390438ef2d0SNikolay Aleksandrov 		return NULL;
391438ef2d0SNikolay Aleksandrov 	}
392438ef2d0SNikolay Aleksandrov 
393438ef2d0SNikolay Aleksandrov 	*csum = ip_compute_csum(csum_start, igmp_hdr_size);
3945e923585SNikolay Aleksandrov 	skb_put(skb, igmp_hdr_size);
395eb1d1641SHerbert Xu 	__skb_pull(skb, sizeof(*eth));
396eb1d1641SHerbert Xu 
397eb1d1641SHerbert Xu out:
398eb1d1641SHerbert Xu 	return skb;
399eb1d1641SHerbert Xu }
400eb1d1641SHerbert Xu 
401dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
40208b202b6SYOSHIFUJI Hideaki static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
403438ef2d0SNikolay Aleksandrov 						    struct net_bridge_port_group *pg,
404438ef2d0SNikolay Aleksandrov 						    const struct in6_addr *ip6_dst,
405438ef2d0SNikolay Aleksandrov 						    const struct in6_addr *group,
406438ef2d0SNikolay Aleksandrov 						    bool with_srcs, bool over_llqt,
40742c11ccfSNikolay Aleksandrov 						    u8 sflag, u8 *igmp_type,
40842c11ccfSNikolay Aleksandrov 						    bool *need_rexmit)
40908b202b6SYOSHIFUJI Hideaki {
410438ef2d0SNikolay Aleksandrov 	struct net_bridge_port *p = pg ? pg->port : NULL;
411438ef2d0SNikolay Aleksandrov 	struct net_bridge_group_src *ent;
412438ef2d0SNikolay Aleksandrov 	size_t pkt_size, mld_hdr_size;
413438ef2d0SNikolay Aleksandrov 	unsigned long now = jiffies;
414aa2ae3e7SNikolay Aleksandrov 	struct mld2_query *mld2q;
415438ef2d0SNikolay Aleksandrov 	void *csum_start = NULL;
416aa2ae3e7SNikolay Aleksandrov 	unsigned long interval;
417438ef2d0SNikolay Aleksandrov 	__sum16 *csum = NULL;
41808b202b6SYOSHIFUJI Hideaki 	struct ipv6hdr *ip6h;
41908b202b6SYOSHIFUJI Hideaki 	struct mld_msg *mldq;
420aa2ae3e7SNikolay Aleksandrov 	struct sk_buff *skb;
421438ef2d0SNikolay Aleksandrov 	unsigned long llqt;
42208b202b6SYOSHIFUJI Hideaki 	struct ethhdr *eth;
423438ef2d0SNikolay Aleksandrov 	u16 llqt_srcs = 0;
42408b202b6SYOSHIFUJI Hideaki 	u8 *hopopt;
42508b202b6SYOSHIFUJI Hideaki 
426aa2ae3e7SNikolay Aleksandrov 	mld_hdr_size = sizeof(*mldq);
427438ef2d0SNikolay Aleksandrov 	if (br->multicast_mld_version == 2) {
428aa2ae3e7SNikolay Aleksandrov 		mld_hdr_size = sizeof(*mld2q);
429438ef2d0SNikolay Aleksandrov 		if (pg && with_srcs) {
430438ef2d0SNikolay Aleksandrov 			llqt = now + (br->multicast_last_member_interval *
431438ef2d0SNikolay Aleksandrov 				      br->multicast_last_member_count);
432438ef2d0SNikolay Aleksandrov 			hlist_for_each_entry(ent, &pg->src_list, node) {
433438ef2d0SNikolay Aleksandrov 				if (over_llqt == time_after(ent->timer.expires,
434438ef2d0SNikolay Aleksandrov 							    llqt) &&
435438ef2d0SNikolay Aleksandrov 				    ent->src_query_rexmit_cnt > 0)
436438ef2d0SNikolay Aleksandrov 					llqt_srcs++;
437438ef2d0SNikolay Aleksandrov 			}
438438ef2d0SNikolay Aleksandrov 
439438ef2d0SNikolay Aleksandrov 			if (!llqt_srcs)
440438ef2d0SNikolay Aleksandrov 				return NULL;
441438ef2d0SNikolay Aleksandrov 			mld_hdr_size += llqt_srcs * sizeof(struct in6_addr);
442438ef2d0SNikolay Aleksandrov 		}
443438ef2d0SNikolay Aleksandrov 	}
444438ef2d0SNikolay Aleksandrov 
445438ef2d0SNikolay Aleksandrov 	pkt_size = sizeof(*eth) + sizeof(*ip6h) + 8 + mld_hdr_size;
446438ef2d0SNikolay Aleksandrov 	if ((p && pkt_size > p->dev->mtu) ||
447438ef2d0SNikolay Aleksandrov 	    pkt_size > br->dev->mtu)
448438ef2d0SNikolay Aleksandrov 		return NULL;
449438ef2d0SNikolay Aleksandrov 
450438ef2d0SNikolay Aleksandrov 	skb = netdev_alloc_skb_ip_align(br->dev, pkt_size);
45108b202b6SYOSHIFUJI Hideaki 	if (!skb)
45208b202b6SYOSHIFUJI Hideaki 		goto out;
45308b202b6SYOSHIFUJI Hideaki 
45408b202b6SYOSHIFUJI Hideaki 	skb->protocol = htons(ETH_P_IPV6);
45508b202b6SYOSHIFUJI Hideaki 
45608b202b6SYOSHIFUJI Hideaki 	/* Ethernet header */
45708b202b6SYOSHIFUJI Hideaki 	skb_reset_mac_header(skb);
45808b202b6SYOSHIFUJI Hideaki 	eth = eth_hdr(skb);
45908b202b6SYOSHIFUJI Hideaki 
460e5a727f6SJoe Perches 	ether_addr_copy(eth->h_source, br->dev->dev_addr);
46108b202b6SYOSHIFUJI Hideaki 	eth->h_proto = htons(ETH_P_IPV6);
46208b202b6SYOSHIFUJI Hideaki 	skb_put(skb, sizeof(*eth));
46308b202b6SYOSHIFUJI Hideaki 
46408b202b6SYOSHIFUJI Hideaki 	/* IPv6 header + HbH option */
46508b202b6SYOSHIFUJI Hideaki 	skb_set_network_header(skb, skb->len);
46608b202b6SYOSHIFUJI Hideaki 	ip6h = ipv6_hdr(skb);
46708b202b6SYOSHIFUJI Hideaki 
46808b202b6SYOSHIFUJI Hideaki 	*(__force __be32 *)ip6h = htonl(0x60000000);
469aa2ae3e7SNikolay Aleksandrov 	ip6h->payload_len = htons(8 + mld_hdr_size);
47008b202b6SYOSHIFUJI Hideaki 	ip6h->nexthdr = IPPROTO_HOPOPTS;
47108b202b6SYOSHIFUJI Hideaki 	ip6h->hop_limit = 1;
472438ef2d0SNikolay Aleksandrov 	ip6h->daddr = *ip6_dst;
473d1d81d4cSUlrich Weber 	if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
474d1d81d4cSUlrich Weber 			       &ip6h->saddr)) {
475d1d81d4cSUlrich Weber 		kfree_skb(skb);
476675779adSNikolay Aleksandrov 		br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, false);
477d1d81d4cSUlrich Weber 		return NULL;
478d1d81d4cSUlrich Weber 	}
4790888d5f3Sdaniel 
480675779adSNikolay Aleksandrov 	br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true);
48136cff5a1SLinus Lüssing 	ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
48208b202b6SYOSHIFUJI Hideaki 
48308b202b6SYOSHIFUJI Hideaki 	hopopt = (u8 *)(ip6h + 1);
48408b202b6SYOSHIFUJI Hideaki 	hopopt[0] = IPPROTO_ICMPV6;		/* next hdr */
48508b202b6SYOSHIFUJI Hideaki 	hopopt[1] = 0;				/* length of HbH */
48608b202b6SYOSHIFUJI Hideaki 	hopopt[2] = IPV6_TLV_ROUTERALERT;	/* Router Alert */
48708b202b6SYOSHIFUJI Hideaki 	hopopt[3] = 2;				/* Length of RA Option */
48808b202b6SYOSHIFUJI Hideaki 	hopopt[4] = 0;				/* Type = 0x0000 (MLD) */
48908b202b6SYOSHIFUJI Hideaki 	hopopt[5] = 0;
4901de5a71cSEldad Zack 	hopopt[6] = IPV6_TLV_PAD1;		/* Pad1 */
4911de5a71cSEldad Zack 	hopopt[7] = IPV6_TLV_PAD1;		/* Pad1 */
49208b202b6SYOSHIFUJI Hideaki 
49308b202b6SYOSHIFUJI Hideaki 	skb_put(skb, sizeof(*ip6h) + 8);
49408b202b6SYOSHIFUJI Hideaki 
49508b202b6SYOSHIFUJI Hideaki 	/* ICMPv6 */
49608b202b6SYOSHIFUJI Hideaki 	skb_set_transport_header(skb, skb->len);
497438ef2d0SNikolay Aleksandrov 	interval = ipv6_addr_any(group) ?
49832de868cSLinus Lüssing 			br->multicast_query_response_interval :
49932de868cSLinus Lüssing 			br->multicast_last_member_interval;
5001080ab95SNikolay Aleksandrov 	*igmp_type = ICMPV6_MGM_QUERY;
501aa2ae3e7SNikolay Aleksandrov 	switch (br->multicast_mld_version) {
502aa2ae3e7SNikolay Aleksandrov 	case 1:
503aa2ae3e7SNikolay Aleksandrov 		mldq = (struct mld_msg *)icmp6_hdr(skb);
50408b202b6SYOSHIFUJI Hideaki 		mldq->mld_type = ICMPV6_MGM_QUERY;
50508b202b6SYOSHIFUJI Hideaki 		mldq->mld_code = 0;
50608b202b6SYOSHIFUJI Hideaki 		mldq->mld_cksum = 0;
50708b202b6SYOSHIFUJI Hideaki 		mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
50808b202b6SYOSHIFUJI Hideaki 		mldq->mld_reserved = 0;
509438ef2d0SNikolay Aleksandrov 		mldq->mld_mca = *group;
510438ef2d0SNikolay Aleksandrov 		csum = &mldq->mld_cksum;
511438ef2d0SNikolay Aleksandrov 		csum_start = (void *)mldq;
512aa2ae3e7SNikolay Aleksandrov 		break;
513aa2ae3e7SNikolay Aleksandrov 	case 2:
514aa2ae3e7SNikolay Aleksandrov 		mld2q = (struct mld2_query *)icmp6_hdr(skb);
51553631a5fSLance Richardson 		mld2q->mld2q_mrc = htons((u16)jiffies_to_msecs(interval));
516aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_type = ICMPV6_MGM_QUERY;
517aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_code = 0;
518aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_cksum = 0;
519aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_resv1 = 0;
520aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_resv2 = 0;
521438ef2d0SNikolay Aleksandrov 		mld2q->mld2q_suppress = sflag;
522aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_qrv = 2;
523438ef2d0SNikolay Aleksandrov 		mld2q->mld2q_nsrcs = htons(llqt_srcs);
524aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_qqic = br->multicast_query_interval / HZ;
525438ef2d0SNikolay Aleksandrov 		mld2q->mld2q_mca = *group;
526438ef2d0SNikolay Aleksandrov 		csum = &mld2q->mld2q_cksum;
527438ef2d0SNikolay Aleksandrov 		csum_start = (void *)mld2q;
528438ef2d0SNikolay Aleksandrov 		if (!pg || !with_srcs)
529438ef2d0SNikolay Aleksandrov 			break;
530438ef2d0SNikolay Aleksandrov 
531438ef2d0SNikolay Aleksandrov 		llqt_srcs = 0;
532438ef2d0SNikolay Aleksandrov 		hlist_for_each_entry(ent, &pg->src_list, node) {
533438ef2d0SNikolay Aleksandrov 			if (over_llqt == time_after(ent->timer.expires,
534438ef2d0SNikolay Aleksandrov 						    llqt) &&
535438ef2d0SNikolay Aleksandrov 			    ent->src_query_rexmit_cnt > 0) {
536438ef2d0SNikolay Aleksandrov 				mld2q->mld2q_srcs[llqt_srcs++] = ent->addr.u.ip6;
537438ef2d0SNikolay Aleksandrov 				ent->src_query_rexmit_cnt--;
53842c11ccfSNikolay Aleksandrov 				if (need_rexmit && ent->src_query_rexmit_cnt)
53942c11ccfSNikolay Aleksandrov 					*need_rexmit = true;
540438ef2d0SNikolay Aleksandrov 			}
541438ef2d0SNikolay Aleksandrov 		}
542438ef2d0SNikolay Aleksandrov 		if (WARN_ON(llqt_srcs != ntohs(mld2q->mld2q_nsrcs))) {
543438ef2d0SNikolay Aleksandrov 			kfree_skb(skb);
544438ef2d0SNikolay Aleksandrov 			return NULL;
545438ef2d0SNikolay Aleksandrov 		}
546aa2ae3e7SNikolay Aleksandrov 		break;
547aa2ae3e7SNikolay Aleksandrov 	}
54808b202b6SYOSHIFUJI Hideaki 
549438ef2d0SNikolay Aleksandrov 	if (WARN_ON(!csum || !csum_start)) {
550438ef2d0SNikolay Aleksandrov 		kfree_skb(skb);
551438ef2d0SNikolay Aleksandrov 		return NULL;
552438ef2d0SNikolay Aleksandrov 	}
553438ef2d0SNikolay Aleksandrov 
554438ef2d0SNikolay Aleksandrov 	*csum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, mld_hdr_size,
555438ef2d0SNikolay Aleksandrov 				IPPROTO_ICMPV6,
556438ef2d0SNikolay Aleksandrov 				csum_partial(csum_start, mld_hdr_size, 0));
557438ef2d0SNikolay Aleksandrov 	skb_put(skb, mld_hdr_size);
55808b202b6SYOSHIFUJI Hideaki 	__skb_pull(skb, sizeof(*eth));
55908b202b6SYOSHIFUJI Hideaki 
56008b202b6SYOSHIFUJI Hideaki out:
56108b202b6SYOSHIFUJI Hideaki 	return skb;
56208b202b6SYOSHIFUJI Hideaki }
56308b202b6SYOSHIFUJI Hideaki #endif
56408b202b6SYOSHIFUJI Hideaki 
5658ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
566438ef2d0SNikolay Aleksandrov 						struct net_bridge_port_group *pg,
567438ef2d0SNikolay Aleksandrov 						struct br_ip *ip_dst,
568438ef2d0SNikolay Aleksandrov 						struct br_ip *group,
569438ef2d0SNikolay Aleksandrov 						bool with_srcs, bool over_lmqt,
57042c11ccfSNikolay Aleksandrov 						u8 sflag, u8 *igmp_type,
57142c11ccfSNikolay Aleksandrov 						bool *need_rexmit)
5728ef2a9a5SYOSHIFUJI Hideaki {
573438ef2d0SNikolay Aleksandrov 	__be32 ip4_dst;
574438ef2d0SNikolay Aleksandrov 
575438ef2d0SNikolay Aleksandrov 	switch (group->proto) {
5768ef2a9a5SYOSHIFUJI Hideaki 	case htons(ETH_P_IP):
577438ef2d0SNikolay Aleksandrov 		ip4_dst = ip_dst ? ip_dst->u.ip4 : htonl(INADDR_ALLHOSTS_GROUP);
578438ef2d0SNikolay Aleksandrov 		return br_ip4_multicast_alloc_query(br, pg,
579438ef2d0SNikolay Aleksandrov 						    ip4_dst, group->u.ip4,
580438ef2d0SNikolay Aleksandrov 						    with_srcs, over_lmqt,
58142c11ccfSNikolay Aleksandrov 						    sflag, igmp_type,
58242c11ccfSNikolay Aleksandrov 						    need_rexmit);
583dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
584438ef2d0SNikolay Aleksandrov 	case htons(ETH_P_IPV6): {
585438ef2d0SNikolay Aleksandrov 		struct in6_addr ip6_dst;
586438ef2d0SNikolay Aleksandrov 
587438ef2d0SNikolay Aleksandrov 		if (ip_dst)
588438ef2d0SNikolay Aleksandrov 			ip6_dst = ip_dst->u.ip6;
589438ef2d0SNikolay Aleksandrov 		else
590438ef2d0SNikolay Aleksandrov 			ipv6_addr_set(&ip6_dst, htonl(0xff020000), 0, 0,
591438ef2d0SNikolay Aleksandrov 				      htonl(1));
592438ef2d0SNikolay Aleksandrov 
593438ef2d0SNikolay Aleksandrov 		return br_ip6_multicast_alloc_query(br, pg,
594438ef2d0SNikolay Aleksandrov 						    &ip6_dst, &group->u.ip6,
595438ef2d0SNikolay Aleksandrov 						    with_srcs, over_lmqt,
59642c11ccfSNikolay Aleksandrov 						    sflag, igmp_type,
59742c11ccfSNikolay Aleksandrov 						    need_rexmit);
598438ef2d0SNikolay Aleksandrov 	}
59908b202b6SYOSHIFUJI Hideaki #endif
6008ef2a9a5SYOSHIFUJI Hideaki 	}
6018ef2a9a5SYOSHIFUJI Hideaki 	return NULL;
6028ef2a9a5SYOSHIFUJI Hideaki }
6038ef2a9a5SYOSHIFUJI Hideaki 
604cfd56754SCong Wang struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
6055e923585SNikolay Aleksandrov 						    struct br_ip *group)
606eb1d1641SHerbert Xu {
607eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
6084c0833bcSTobias Klauser 	int err;
609eb1d1641SHerbert Xu 
61019e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip_get(br, group);
61119e3a9c9SNikolay Aleksandrov 	if (mp)
61219e3a9c9SNikolay Aleksandrov 		return mp;
613eb1d1641SHerbert Xu 
61419e3a9c9SNikolay Aleksandrov 	if (atomic_read(&br->mdb_hash_tbl.nelems) >= br->hash_max) {
61519e3a9c9SNikolay Aleksandrov 		br_opt_toggle(br, BROPT_MULTICAST_ENABLED, false);
61619e3a9c9SNikolay Aleksandrov 		return ERR_PTR(-E2BIG);
617eb1d1641SHerbert Xu 	}
618eb1d1641SHerbert Xu 
619eb1d1641SHerbert Xu 	mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
620eb1d1641SHerbert Xu 	if (unlikely(!mp))
6214c0833bcSTobias Klauser 		return ERR_PTR(-ENOMEM);
622eb1d1641SHerbert Xu 
623eb1d1641SHerbert Xu 	mp->br = br;
6248ef2a9a5SYOSHIFUJI Hideaki 	mp->addr = *group;
62588c1f37fSAllen Pais 	timer_setup(&mp->timer, br_multicast_group_expired, 0);
62619e3a9c9SNikolay Aleksandrov 	err = rhashtable_lookup_insert_fast(&br->mdb_hash_tbl, &mp->rhnode,
62719e3a9c9SNikolay Aleksandrov 					    br_mdb_rht_params);
62819e3a9c9SNikolay Aleksandrov 	if (err) {
62919e3a9c9SNikolay Aleksandrov 		kfree(mp);
63019e3a9c9SNikolay Aleksandrov 		mp = ERR_PTR(err);
63119e3a9c9SNikolay Aleksandrov 	} else {
63219e3a9c9SNikolay Aleksandrov 		hlist_add_head_rcu(&mp->mdb_node, &br->mdb_list);
63319e3a9c9SNikolay Aleksandrov 	}
6341faabf2aSEric Dumazet 
635eb1d1641SHerbert Xu 	return mp;
636eb1d1641SHerbert Xu }
637eb1d1641SHerbert Xu 
6388b671779SNikolay Aleksandrov static void br_multicast_group_src_expired(struct timer_list *t)
6398b671779SNikolay Aleksandrov {
6408b671779SNikolay Aleksandrov 	struct net_bridge_group_src *src = from_timer(src, t, timer);
6418b671779SNikolay Aleksandrov 	struct net_bridge_port_group *pg;
6428b671779SNikolay Aleksandrov 	struct net_bridge *br = src->br;
6438b671779SNikolay Aleksandrov 
6448b671779SNikolay Aleksandrov 	spin_lock(&br->multicast_lock);
6458b671779SNikolay Aleksandrov 	if (hlist_unhashed(&src->node) || !netif_running(br->dev) ||
6468b671779SNikolay Aleksandrov 	    timer_pending(&src->timer))
6478b671779SNikolay Aleksandrov 		goto out;
6488b671779SNikolay Aleksandrov 
6498b671779SNikolay Aleksandrov 	pg = src->pg;
6508b671779SNikolay Aleksandrov 	if (pg->filter_mode == MCAST_INCLUDE) {
6518b671779SNikolay Aleksandrov 		br_multicast_del_group_src(src);
6528b671779SNikolay Aleksandrov 		if (!hlist_empty(&pg->src_list))
6538b671779SNikolay Aleksandrov 			goto out;
6548b671779SNikolay Aleksandrov 		br_multicast_find_del_pg(br, pg);
6558b671779SNikolay Aleksandrov 	}
6568b671779SNikolay Aleksandrov out:
6578b671779SNikolay Aleksandrov 	spin_unlock(&br->multicast_lock);
6588b671779SNikolay Aleksandrov }
6598b671779SNikolay Aleksandrov 
6608b671779SNikolay Aleksandrov static struct net_bridge_group_src *
6618b671779SNikolay Aleksandrov br_multicast_find_group_src(struct net_bridge_port_group *pg, struct br_ip *ip)
6628b671779SNikolay Aleksandrov {
6638b671779SNikolay Aleksandrov 	struct net_bridge_group_src *ent;
6648b671779SNikolay Aleksandrov 
6658b671779SNikolay Aleksandrov 	switch (ip->proto) {
6668b671779SNikolay Aleksandrov 	case htons(ETH_P_IP):
6678b671779SNikolay Aleksandrov 		hlist_for_each_entry(ent, &pg->src_list, node)
6688b671779SNikolay Aleksandrov 			if (ip->u.ip4 == ent->addr.u.ip4)
6698b671779SNikolay Aleksandrov 				return ent;
6708b671779SNikolay Aleksandrov 		break;
6718b671779SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
6728b671779SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
6738b671779SNikolay Aleksandrov 		hlist_for_each_entry(ent, &pg->src_list, node)
6748b671779SNikolay Aleksandrov 			if (!ipv6_addr_cmp(&ent->addr.u.ip6, &ip->u.ip6))
6758b671779SNikolay Aleksandrov 				return ent;
6768b671779SNikolay Aleksandrov 		break;
6778b671779SNikolay Aleksandrov #endif
6788b671779SNikolay Aleksandrov 	}
6798b671779SNikolay Aleksandrov 
6808b671779SNikolay Aleksandrov 	return NULL;
6818b671779SNikolay Aleksandrov }
6828b671779SNikolay Aleksandrov 
6838b671779SNikolay Aleksandrov static struct net_bridge_group_src *
6848b671779SNikolay Aleksandrov br_multicast_new_group_src(struct net_bridge_port_group *pg, struct br_ip *src_ip)
6858b671779SNikolay Aleksandrov {
6868b671779SNikolay Aleksandrov 	struct net_bridge_group_src *grp_src;
6878b671779SNikolay Aleksandrov 
6888b671779SNikolay Aleksandrov 	if (unlikely(pg->src_ents >= PG_SRC_ENT_LIMIT))
6898b671779SNikolay Aleksandrov 		return NULL;
6908b671779SNikolay Aleksandrov 
6918b671779SNikolay Aleksandrov 	switch (src_ip->proto) {
6928b671779SNikolay Aleksandrov 	case htons(ETH_P_IP):
6938b671779SNikolay Aleksandrov 		if (ipv4_is_zeronet(src_ip->u.ip4) ||
6948b671779SNikolay Aleksandrov 		    ipv4_is_multicast(src_ip->u.ip4))
6958b671779SNikolay Aleksandrov 			return NULL;
6968b671779SNikolay Aleksandrov 		break;
6978b671779SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
6988b671779SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
6998b671779SNikolay Aleksandrov 		if (ipv6_addr_any(&src_ip->u.ip6) ||
7008b671779SNikolay Aleksandrov 		    ipv6_addr_is_multicast(&src_ip->u.ip6))
7018b671779SNikolay Aleksandrov 			return NULL;
7028b671779SNikolay Aleksandrov 		break;
7038b671779SNikolay Aleksandrov #endif
7048b671779SNikolay Aleksandrov 	}
7058b671779SNikolay Aleksandrov 
7068b671779SNikolay Aleksandrov 	grp_src = kzalloc(sizeof(*grp_src), GFP_ATOMIC);
7078b671779SNikolay Aleksandrov 	if (unlikely(!grp_src))
7088b671779SNikolay Aleksandrov 		return NULL;
7098b671779SNikolay Aleksandrov 
7108b671779SNikolay Aleksandrov 	grp_src->pg = pg;
7118b671779SNikolay Aleksandrov 	grp_src->br = pg->port->br;
7128b671779SNikolay Aleksandrov 	grp_src->addr = *src_ip;
7138b671779SNikolay Aleksandrov 	timer_setup(&grp_src->timer, br_multicast_group_src_expired, 0);
7148b671779SNikolay Aleksandrov 
7158b671779SNikolay Aleksandrov 	hlist_add_head_rcu(&grp_src->node, &pg->src_list);
7168b671779SNikolay Aleksandrov 	pg->src_ents++;
7178b671779SNikolay Aleksandrov 
7188b671779SNikolay Aleksandrov 	return grp_src;
7198b671779SNikolay Aleksandrov }
7208b671779SNikolay Aleksandrov 
721cfd56754SCong Wang struct net_bridge_port_group *br_multicast_new_port_group(
722cfd56754SCong Wang 			struct net_bridge_port *port,
723cfd56754SCong Wang 			struct br_ip *group,
724ccb1c31aSAmerigo Wang 			struct net_bridge_port_group __rcu *next,
7256db6f0eaSFelix Fietkau 			unsigned char flags,
7268b671779SNikolay Aleksandrov 			const unsigned char *src,
7278b671779SNikolay Aleksandrov 			u8 filter_mode)
728cfd56754SCong Wang {
729cfd56754SCong Wang 	struct net_bridge_port_group *p;
730cfd56754SCong Wang 
731cfd56754SCong Wang 	p = kzalloc(sizeof(*p), GFP_ATOMIC);
732cfd56754SCong Wang 	if (unlikely(!p))
733cfd56754SCong Wang 		return NULL;
734cfd56754SCong Wang 
735cfd56754SCong Wang 	p->addr = *group;
736cfd56754SCong Wang 	p->port = port;
7379d06b6d8SElad Raz 	p->flags = flags;
7388b671779SNikolay Aleksandrov 	p->filter_mode = filter_mode;
7398b671779SNikolay Aleksandrov 	INIT_HLIST_HEAD(&p->src_list);
740eca2a43bSstephen hemminger 	rcu_assign_pointer(p->next, next);
74188c1f37fSAllen Pais 	timer_setup(&p->timer, br_multicast_port_group_expired, 0);
74242c11ccfSNikolay Aleksandrov 	timer_setup(&p->rexmit_timer, br_multicast_port_group_rexmit, 0);
74342c11ccfSNikolay Aleksandrov 	hlist_add_head(&p->mglist, &port->mglist);
7446db6f0eaSFelix Fietkau 
7456db6f0eaSFelix Fietkau 	if (src)
7466db6f0eaSFelix Fietkau 		memcpy(p->eth_addr, src, ETH_ALEN);
7476db6f0eaSFelix Fietkau 	else
7481bfe45f4SMao Wenan 		eth_broadcast_addr(p->eth_addr);
7496db6f0eaSFelix Fietkau 
750cfd56754SCong Wang 	return p;
751cfd56754SCong Wang }
752cfd56754SCong Wang 
7536db6f0eaSFelix Fietkau static bool br_port_group_equal(struct net_bridge_port_group *p,
7546db6f0eaSFelix Fietkau 				struct net_bridge_port *port,
7556db6f0eaSFelix Fietkau 				const unsigned char *src)
7566db6f0eaSFelix Fietkau {
7576db6f0eaSFelix Fietkau 	if (p->port != port)
7586db6f0eaSFelix Fietkau 		return false;
7596db6f0eaSFelix Fietkau 
7606db6f0eaSFelix Fietkau 	if (!(port->flags & BR_MULTICAST_TO_UNICAST))
7616db6f0eaSFelix Fietkau 		return true;
7626db6f0eaSFelix Fietkau 
7636db6f0eaSFelix Fietkau 	return ether_addr_equal(src, p->eth_addr);
7646db6f0eaSFelix Fietkau }
7656db6f0eaSFelix Fietkau 
7661bc844eeSNikolay Aleksandrov void br_multicast_host_join(struct net_bridge_mdb_entry *mp, bool notify)
7671bc844eeSNikolay Aleksandrov {
7681bc844eeSNikolay Aleksandrov 	if (!mp->host_joined) {
7691bc844eeSNikolay Aleksandrov 		mp->host_joined = true;
7701bc844eeSNikolay Aleksandrov 		if (notify)
77181f19838SNikolay Aleksandrov 			br_mdb_notify(mp->br->dev, mp, NULL, RTM_NEWMDB);
7721bc844eeSNikolay Aleksandrov 	}
7731bc844eeSNikolay Aleksandrov 	mod_timer(&mp->timer, jiffies + mp->br->multicast_membership_interval);
7741bc844eeSNikolay Aleksandrov }
7751bc844eeSNikolay Aleksandrov 
7761bc844eeSNikolay Aleksandrov void br_multicast_host_leave(struct net_bridge_mdb_entry *mp, bool notify)
7771bc844eeSNikolay Aleksandrov {
7781bc844eeSNikolay Aleksandrov 	if (!mp->host_joined)
7791bc844eeSNikolay Aleksandrov 		return;
7801bc844eeSNikolay Aleksandrov 
7811bc844eeSNikolay Aleksandrov 	mp->host_joined = false;
7821bc844eeSNikolay Aleksandrov 	if (notify)
78381f19838SNikolay Aleksandrov 		br_mdb_notify(mp->br->dev, mp, NULL, RTM_DELMDB);
7841bc844eeSNikolay Aleksandrov }
7851bc844eeSNikolay Aleksandrov 
786eb1d1641SHerbert Xu static int br_multicast_add_group(struct net_bridge *br,
7878ef2a9a5SYOSHIFUJI Hideaki 				  struct net_bridge_port *port,
7886db6f0eaSFelix Fietkau 				  struct br_ip *group,
7898b671779SNikolay Aleksandrov 				  const unsigned char *src,
7900436862eSNikolay Aleksandrov 				  u8 filter_mode,
7910436862eSNikolay Aleksandrov 				  bool igmpv2_mldv1)
792eb1d1641SHerbert Xu {
793e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
7945e923585SNikolay Aleksandrov 	struct net_bridge_port_group *p;
7955e923585SNikolay Aleksandrov 	struct net_bridge_mdb_entry *mp;
796454594f3SLinus Lüssing 	unsigned long now = jiffies;
797eb1d1641SHerbert Xu 	int err;
798eb1d1641SHerbert Xu 
799eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
800eb1d1641SHerbert Xu 	if (!netif_running(br->dev) ||
801eb1d1641SHerbert Xu 	    (port && port->state == BR_STATE_DISABLED))
802eb1d1641SHerbert Xu 		goto out;
803eb1d1641SHerbert Xu 
80419e3a9c9SNikolay Aleksandrov 	mp = br_multicast_new_group(br, group);
805eb1d1641SHerbert Xu 	err = PTR_ERR(mp);
8064c0833bcSTobias Klauser 	if (IS_ERR(mp))
807eb1d1641SHerbert Xu 		goto err;
808eb1d1641SHerbert Xu 
809eb1d1641SHerbert Xu 	if (!port) {
8101bc844eeSNikolay Aleksandrov 		br_multicast_host_join(mp, true);
811eb1d1641SHerbert Xu 		goto out;
812eb1d1641SHerbert Xu 	}
813eb1d1641SHerbert Xu 
814e8051688SEric Dumazet 	for (pp = &mp->ports;
815e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
816e8051688SEric Dumazet 	     pp = &p->next) {
8176db6f0eaSFelix Fietkau 		if (br_port_group_equal(p, port, src))
818454594f3SLinus Lüssing 			goto found;
819eb1d1641SHerbert Xu 		if ((unsigned long)p->port < (unsigned long)port)
820eb1d1641SHerbert Xu 			break;
821eb1d1641SHerbert Xu 	}
822eb1d1641SHerbert Xu 
8238b671779SNikolay Aleksandrov 	p = br_multicast_new_port_group(port, group, *pp, 0, src, filter_mode);
824eb1d1641SHerbert Xu 	if (unlikely(!p))
825eb1d1641SHerbert Xu 		goto err;
826eb1d1641SHerbert Xu 	rcu_assign_pointer(*pp, p);
82781f19838SNikolay Aleksandrov 	br_mdb_notify(br->dev, mp, p, RTM_NEWMDB);
828eb1d1641SHerbert Xu 
829454594f3SLinus Lüssing found:
8300436862eSNikolay Aleksandrov 	if (igmpv2_mldv1)
831454594f3SLinus Lüssing 		mod_timer(&p->timer, now + br->multicast_membership_interval);
83281f19838SNikolay Aleksandrov 
833eb1d1641SHerbert Xu out:
834eb1d1641SHerbert Xu 	err = 0;
835eb1d1641SHerbert Xu 
836eb1d1641SHerbert Xu err:
837eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
838eb1d1641SHerbert Xu 	return err;
839eb1d1641SHerbert Xu }
840eb1d1641SHerbert Xu 
8418ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_add_group(struct net_bridge *br,
8428ef2a9a5SYOSHIFUJI Hideaki 				      struct net_bridge_port *port,
843b0e9a30dSVlad Yasevich 				      __be32 group,
8446db6f0eaSFelix Fietkau 				      __u16 vid,
8458b671779SNikolay Aleksandrov 				      const unsigned char *src,
8468b671779SNikolay Aleksandrov 				      bool igmpv2)
8478ef2a9a5SYOSHIFUJI Hideaki {
8488ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip br_group;
8498b671779SNikolay Aleksandrov 	u8 filter_mode;
8508ef2a9a5SYOSHIFUJI Hideaki 
8518ef2a9a5SYOSHIFUJI Hideaki 	if (ipv4_is_local_multicast(group))
8528ef2a9a5SYOSHIFUJI Hideaki 		return 0;
8538ef2a9a5SYOSHIFUJI Hideaki 
8541515a63fSNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
8558ef2a9a5SYOSHIFUJI Hideaki 	br_group.u.ip4 = group;
8568ef2a9a5SYOSHIFUJI Hideaki 	br_group.proto = htons(ETH_P_IP);
857b0e9a30dSVlad Yasevich 	br_group.vid = vid;
8588b671779SNikolay Aleksandrov 	filter_mode = igmpv2 ? MCAST_EXCLUDE : MCAST_INCLUDE;
8598ef2a9a5SYOSHIFUJI Hideaki 
8600436862eSNikolay Aleksandrov 	return br_multicast_add_group(br, port, &br_group, src, filter_mode,
8610436862eSNikolay Aleksandrov 				      igmpv2);
8628ef2a9a5SYOSHIFUJI Hideaki }
8638ef2a9a5SYOSHIFUJI Hideaki 
864dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
86508b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_add_group(struct net_bridge *br,
86608b202b6SYOSHIFUJI Hideaki 				      struct net_bridge_port *port,
867b0e9a30dSVlad Yasevich 				      const struct in6_addr *group,
8686db6f0eaSFelix Fietkau 				      __u16 vid,
8698b671779SNikolay Aleksandrov 				      const unsigned char *src,
8708b671779SNikolay Aleksandrov 				      bool mldv1)
87108b202b6SYOSHIFUJI Hideaki {
87208b202b6SYOSHIFUJI Hideaki 	struct br_ip br_group;
8738b671779SNikolay Aleksandrov 	u8 filter_mode;
87408b202b6SYOSHIFUJI Hideaki 
8753c3769e6SLinus Lüssing 	if (ipv6_addr_is_ll_all_nodes(group))
87608b202b6SYOSHIFUJI Hideaki 		return 0;
87708b202b6SYOSHIFUJI Hideaki 
87819e3a9c9SNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
8794e3fd7a0SAlexey Dobriyan 	br_group.u.ip6 = *group;
8809cc6e0c4SLinus Lüssing 	br_group.proto = htons(ETH_P_IPV6);
881b0e9a30dSVlad Yasevich 	br_group.vid = vid;
8828b671779SNikolay Aleksandrov 	filter_mode = mldv1 ? MCAST_EXCLUDE : MCAST_INCLUDE;
88308b202b6SYOSHIFUJI Hideaki 
8840436862eSNikolay Aleksandrov 	return br_multicast_add_group(br, port, &br_group, src, filter_mode,
8850436862eSNikolay Aleksandrov 				      mldv1);
88608b202b6SYOSHIFUJI Hideaki }
88708b202b6SYOSHIFUJI Hideaki #endif
88808b202b6SYOSHIFUJI Hideaki 
88988c1f37fSAllen Pais static void br_multicast_router_expired(struct timer_list *t)
890eb1d1641SHerbert Xu {
89188c1f37fSAllen Pais 	struct net_bridge_port *port =
89288c1f37fSAllen Pais 			from_timer(port, t, multicast_router_timer);
893eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
894eb1d1641SHerbert Xu 
895eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
896a55d8246SNikolay Aleksandrov 	if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
897a55d8246SNikolay Aleksandrov 	    port->multicast_router == MDB_RTR_TYPE_PERM ||
898f12e7d95SNogah Frankel 	    timer_pending(&port->multicast_router_timer))
899eb1d1641SHerbert Xu 		goto out;
900eb1d1641SHerbert Xu 
901f12e7d95SNogah Frankel 	__del_port_router(port);
902eb1d1641SHerbert Xu out:
903eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
904eb1d1641SHerbert Xu }
905eb1d1641SHerbert Xu 
90677041420SYotam Gigi static void br_mc_router_state_change(struct net_bridge *p,
90777041420SYotam Gigi 				      bool is_mc_router)
90877041420SYotam Gigi {
90977041420SYotam Gigi 	struct switchdev_attr attr = {
91077041420SYotam Gigi 		.orig_dev = p->dev,
91177041420SYotam Gigi 		.id = SWITCHDEV_ATTR_ID_BRIDGE_MROUTER,
91277041420SYotam Gigi 		.flags = SWITCHDEV_F_DEFER,
91377041420SYotam Gigi 		.u.mrouter = is_mc_router,
91477041420SYotam Gigi 	};
91577041420SYotam Gigi 
91677041420SYotam Gigi 	switchdev_port_attr_set(p->dev, &attr);
91777041420SYotam Gigi }
91877041420SYotam Gigi 
91988c1f37fSAllen Pais static void br_multicast_local_router_expired(struct timer_list *t)
920eb1d1641SHerbert Xu {
92188c1f37fSAllen Pais 	struct net_bridge *br = from_timer(br, t, multicast_router_timer);
92277041420SYotam Gigi 
92377041420SYotam Gigi 	spin_lock(&br->multicast_lock);
92477041420SYotam Gigi 	if (br->multicast_router == MDB_RTR_TYPE_DISABLED ||
92577041420SYotam Gigi 	    br->multicast_router == MDB_RTR_TYPE_PERM ||
92677041420SYotam Gigi 	    timer_pending(&br->multicast_router_timer))
92777041420SYotam Gigi 		goto out;
92877041420SYotam Gigi 
92977041420SYotam Gigi 	br_mc_router_state_change(br, false);
93077041420SYotam Gigi out:
93177041420SYotam Gigi 	spin_unlock(&br->multicast_lock);
932eb1d1641SHerbert Xu }
933eb1d1641SHerbert Xu 
934cc0fdd80SLinus Lüssing static void br_multicast_querier_expired(struct net_bridge *br,
93590010b36SLinus Lüssing 					 struct bridge_mcast_own_query *query)
936c83b8fabSHerbert Xu {
937c83b8fabSHerbert Xu 	spin_lock(&br->multicast_lock);
93813cefad2SNikolay Aleksandrov 	if (!netif_running(br->dev) || !br_opt_get(br, BROPT_MULTICAST_ENABLED))
939c83b8fabSHerbert Xu 		goto out;
940c83b8fabSHerbert Xu 
941cc0fdd80SLinus Lüssing 	br_multicast_start_querier(br, query);
942c83b8fabSHerbert Xu 
943c83b8fabSHerbert Xu out:
944c83b8fabSHerbert Xu 	spin_unlock(&br->multicast_lock);
945c83b8fabSHerbert Xu }
946c83b8fabSHerbert Xu 
94788c1f37fSAllen Pais static void br_ip4_multicast_querier_expired(struct timer_list *t)
948cc0fdd80SLinus Lüssing {
94988c1f37fSAllen Pais 	struct net_bridge *br = from_timer(br, t, ip4_other_query.timer);
950cc0fdd80SLinus Lüssing 
95190010b36SLinus Lüssing 	br_multicast_querier_expired(br, &br->ip4_own_query);
952cc0fdd80SLinus Lüssing }
953cc0fdd80SLinus Lüssing 
954cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
95588c1f37fSAllen Pais static void br_ip6_multicast_querier_expired(struct timer_list *t)
956cc0fdd80SLinus Lüssing {
95788c1f37fSAllen Pais 	struct net_bridge *br = from_timer(br, t, ip6_other_query.timer);
958cc0fdd80SLinus Lüssing 
95990010b36SLinus Lüssing 	br_multicast_querier_expired(br, &br->ip6_own_query);
960cc0fdd80SLinus Lüssing }
961cc0fdd80SLinus Lüssing #endif
962cc0fdd80SLinus Lüssing 
963dc4eb53aSLinus Lüssing static void br_multicast_select_own_querier(struct net_bridge *br,
964dc4eb53aSLinus Lüssing 					    struct br_ip *ip,
965dc4eb53aSLinus Lüssing 					    struct sk_buff *skb)
966dc4eb53aSLinus Lüssing {
967dc4eb53aSLinus Lüssing 	if (ip->proto == htons(ETH_P_IP))
968dc4eb53aSLinus Lüssing 		br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr;
969dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
970dc4eb53aSLinus Lüssing 	else
971dc4eb53aSLinus Lüssing 		br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr;
972dc4eb53aSLinus Lüssing #endif
973dc4eb53aSLinus Lüssing }
974dc4eb53aSLinus Lüssing 
9758ef2a9a5SYOSHIFUJI Hideaki static void __br_multicast_send_query(struct net_bridge *br,
9768ef2a9a5SYOSHIFUJI Hideaki 				      struct net_bridge_port *port,
977438ef2d0SNikolay Aleksandrov 				      struct net_bridge_port_group *pg,
978438ef2d0SNikolay Aleksandrov 				      struct br_ip *ip_dst,
979438ef2d0SNikolay Aleksandrov 				      struct br_ip *group,
980438ef2d0SNikolay Aleksandrov 				      bool with_srcs,
98142c11ccfSNikolay Aleksandrov 				      u8 sflag,
98242c11ccfSNikolay Aleksandrov 				      bool *need_rexmit)
983eb1d1641SHerbert Xu {
984438ef2d0SNikolay Aleksandrov 	bool over_lmqt = !!sflag;
985eb1d1641SHerbert Xu 	struct sk_buff *skb;
9861080ab95SNikolay Aleksandrov 	u8 igmp_type;
987eb1d1641SHerbert Xu 
988438ef2d0SNikolay Aleksandrov again_under_lmqt:
989438ef2d0SNikolay Aleksandrov 	skb = br_multicast_alloc_query(br, pg, ip_dst, group, with_srcs,
99042c11ccfSNikolay Aleksandrov 				       over_lmqt, sflag, &igmp_type,
99142c11ccfSNikolay Aleksandrov 				       need_rexmit);
992eb1d1641SHerbert Xu 	if (!skb)
9938ef2a9a5SYOSHIFUJI Hideaki 		return;
994eb1d1641SHerbert Xu 
995eb1d1641SHerbert Xu 	if (port) {
996eb1d1641SHerbert Xu 		skb->dev = port->dev;
997a65056ecSNikolay Aleksandrov 		br_multicast_count(br, port, skb, igmp_type,
9981080ab95SNikolay Aleksandrov 				   BR_MCAST_DIR_TX);
99929a26a56SEric W. Biederman 		NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
100029a26a56SEric W. Biederman 			dev_net(port->dev), NULL, skb, NULL, skb->dev,
1001f0b4eeceSLinus Lüssing 			br_dev_queue_push_xmit);
1002438ef2d0SNikolay Aleksandrov 
1003438ef2d0SNikolay Aleksandrov 		if (over_lmqt && with_srcs && sflag) {
1004438ef2d0SNikolay Aleksandrov 			over_lmqt = false;
1005438ef2d0SNikolay Aleksandrov 			goto again_under_lmqt;
1006438ef2d0SNikolay Aleksandrov 		}
1007dc4eb53aSLinus Lüssing 	} else {
1008438ef2d0SNikolay Aleksandrov 		br_multicast_select_own_querier(br, group, skb);
1009a65056ecSNikolay Aleksandrov 		br_multicast_count(br, port, skb, igmp_type,
10101080ab95SNikolay Aleksandrov 				   BR_MCAST_DIR_RX);
1011eb1d1641SHerbert Xu 		netif_rx(skb);
10128ef2a9a5SYOSHIFUJI Hideaki 	}
1013dc4eb53aSLinus Lüssing }
1014eb1d1641SHerbert Xu 
10158ef2a9a5SYOSHIFUJI Hideaki static void br_multicast_send_query(struct net_bridge *br,
1016cc0fdd80SLinus Lüssing 				    struct net_bridge_port *port,
101790010b36SLinus Lüssing 				    struct bridge_mcast_own_query *own_query)
10188ef2a9a5SYOSHIFUJI Hideaki {
101990010b36SLinus Lüssing 	struct bridge_mcast_other_query *other_query = NULL;
10205e923585SNikolay Aleksandrov 	struct br_ip br_group;
10215e923585SNikolay Aleksandrov 	unsigned long time;
10228ef2a9a5SYOSHIFUJI Hideaki 
102313cefad2SNikolay Aleksandrov 	if (!netif_running(br->dev) ||
102413cefad2SNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_ENABLED) ||
1025675779adSNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_QUERIER))
10268ef2a9a5SYOSHIFUJI Hideaki 		return;
10278ef2a9a5SYOSHIFUJI Hideaki 
102808b202b6SYOSHIFUJI Hideaki 	memset(&br_group.u, 0, sizeof(br_group.u));
10298ef2a9a5SYOSHIFUJI Hideaki 
103090010b36SLinus Lüssing 	if (port ? (own_query == &port->ip4_own_query) :
103190010b36SLinus Lüssing 		   (own_query == &br->ip4_own_query)) {
103290010b36SLinus Lüssing 		other_query = &br->ip4_other_query;
103308b202b6SYOSHIFUJI Hideaki 		br_group.proto = htons(ETH_P_IP);
1034dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1035cc0fdd80SLinus Lüssing 	} else {
103690010b36SLinus Lüssing 		other_query = &br->ip6_other_query;
103708b202b6SYOSHIFUJI Hideaki 		br_group.proto = htons(ETH_P_IPV6);
103808b202b6SYOSHIFUJI Hideaki #endif
1039eb1d1641SHerbert Xu 	}
1040eb1d1641SHerbert Xu 
104190010b36SLinus Lüssing 	if (!other_query || timer_pending(&other_query->timer))
1042cc0fdd80SLinus Lüssing 		return;
1043cc0fdd80SLinus Lüssing 
104442c11ccfSNikolay Aleksandrov 	__br_multicast_send_query(br, port, NULL, NULL, &br_group, false, 0,
104542c11ccfSNikolay Aleksandrov 				  NULL);
1046cc0fdd80SLinus Lüssing 
1047cc0fdd80SLinus Lüssing 	time = jiffies;
104890010b36SLinus Lüssing 	time += own_query->startup_sent < br->multicast_startup_query_count ?
1049cc0fdd80SLinus Lüssing 		br->multicast_startup_query_interval :
1050cc0fdd80SLinus Lüssing 		br->multicast_query_interval;
105190010b36SLinus Lüssing 	mod_timer(&own_query->timer, time);
1052cc0fdd80SLinus Lüssing }
1053cc0fdd80SLinus Lüssing 
105490010b36SLinus Lüssing static void
105590010b36SLinus Lüssing br_multicast_port_query_expired(struct net_bridge_port *port,
105690010b36SLinus Lüssing 				struct bridge_mcast_own_query *query)
1057eb1d1641SHerbert Xu {
1058eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
1059eb1d1641SHerbert Xu 
1060eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
106102a780c0SDan Carpenter 	if (port->state == BR_STATE_DISABLED ||
106202a780c0SDan Carpenter 	    port->state == BR_STATE_BLOCKING)
1063eb1d1641SHerbert Xu 		goto out;
1064eb1d1641SHerbert Xu 
1065cc0fdd80SLinus Lüssing 	if (query->startup_sent < br->multicast_startup_query_count)
1066cc0fdd80SLinus Lüssing 		query->startup_sent++;
1067eb1d1641SHerbert Xu 
1068cc0fdd80SLinus Lüssing 	br_multicast_send_query(port->br, port, query);
1069eb1d1641SHerbert Xu 
1070eb1d1641SHerbert Xu out:
1071eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1072eb1d1641SHerbert Xu }
1073eb1d1641SHerbert Xu 
107488c1f37fSAllen Pais static void br_ip4_multicast_port_query_expired(struct timer_list *t)
1075cc0fdd80SLinus Lüssing {
107688c1f37fSAllen Pais 	struct net_bridge_port *port = from_timer(port, t, ip4_own_query.timer);
1077cc0fdd80SLinus Lüssing 
107890010b36SLinus Lüssing 	br_multicast_port_query_expired(port, &port->ip4_own_query);
1079cc0fdd80SLinus Lüssing }
1080cc0fdd80SLinus Lüssing 
1081cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
108288c1f37fSAllen Pais static void br_ip6_multicast_port_query_expired(struct timer_list *t)
1083cc0fdd80SLinus Lüssing {
108488c1f37fSAllen Pais 	struct net_bridge_port *port = from_timer(port, t, ip6_own_query.timer);
1085cc0fdd80SLinus Lüssing 
108690010b36SLinus Lüssing 	br_multicast_port_query_expired(port, &port->ip6_own_query);
1087cc0fdd80SLinus Lüssing }
1088cc0fdd80SLinus Lüssing #endif
1089cc0fdd80SLinus Lüssing 
109042c11ccfSNikolay Aleksandrov static void br_multicast_port_group_rexmit(struct timer_list *t)
109142c11ccfSNikolay Aleksandrov {
109242c11ccfSNikolay Aleksandrov 	struct net_bridge_port_group *pg = from_timer(pg, t, rexmit_timer);
109342c11ccfSNikolay Aleksandrov 	struct bridge_mcast_other_query *other_query = NULL;
109442c11ccfSNikolay Aleksandrov 	struct net_bridge *br = pg->port->br;
109542c11ccfSNikolay Aleksandrov 	bool need_rexmit = false;
109642c11ccfSNikolay Aleksandrov 
109742c11ccfSNikolay Aleksandrov 	spin_lock(&br->multicast_lock);
109842c11ccfSNikolay Aleksandrov 	if (!netif_running(br->dev) || hlist_unhashed(&pg->mglist) ||
109942c11ccfSNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_ENABLED) ||
110042c11ccfSNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_QUERIER))
110142c11ccfSNikolay Aleksandrov 		goto out;
110242c11ccfSNikolay Aleksandrov 
110342c11ccfSNikolay Aleksandrov 	if (pg->addr.proto == htons(ETH_P_IP))
110442c11ccfSNikolay Aleksandrov 		other_query = &br->ip4_other_query;
110542c11ccfSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
110642c11ccfSNikolay Aleksandrov 	else
110742c11ccfSNikolay Aleksandrov 		other_query = &br->ip6_other_query;
110842c11ccfSNikolay Aleksandrov #endif
110942c11ccfSNikolay Aleksandrov 
111042c11ccfSNikolay Aleksandrov 	if (!other_query || timer_pending(&other_query->timer))
111142c11ccfSNikolay Aleksandrov 		goto out;
111242c11ccfSNikolay Aleksandrov 
111342c11ccfSNikolay Aleksandrov 	if (pg->grp_query_rexmit_cnt) {
111442c11ccfSNikolay Aleksandrov 		pg->grp_query_rexmit_cnt--;
111542c11ccfSNikolay Aleksandrov 		__br_multicast_send_query(br, pg->port, pg, &pg->addr,
111642c11ccfSNikolay Aleksandrov 					  &pg->addr, false, 1, NULL);
111742c11ccfSNikolay Aleksandrov 	}
111842c11ccfSNikolay Aleksandrov 	__br_multicast_send_query(br, pg->port, pg, &pg->addr,
111942c11ccfSNikolay Aleksandrov 				  &pg->addr, true, 0, &need_rexmit);
112042c11ccfSNikolay Aleksandrov 
112142c11ccfSNikolay Aleksandrov 	if (pg->grp_query_rexmit_cnt || need_rexmit)
112242c11ccfSNikolay Aleksandrov 		mod_timer(&pg->rexmit_timer, jiffies +
112342c11ccfSNikolay Aleksandrov 					     br->multicast_last_member_interval);
112442c11ccfSNikolay Aleksandrov out:
112542c11ccfSNikolay Aleksandrov 	spin_unlock(&br->multicast_lock);
112642c11ccfSNikolay Aleksandrov }
112742c11ccfSNikolay Aleksandrov 
1128147c1e9bSNogah Frankel static void br_mc_disabled_update(struct net_device *dev, bool value)
1129147c1e9bSNogah Frankel {
1130147c1e9bSNogah Frankel 	struct switchdev_attr attr = {
1131147c1e9bSNogah Frankel 		.orig_dev = dev,
1132147c1e9bSNogah Frankel 		.id = SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED,
1133147c1e9bSNogah Frankel 		.flags = SWITCHDEV_F_DEFER,
113413cefad2SNikolay Aleksandrov 		.u.mc_disabled = !value,
1135147c1e9bSNogah Frankel 	};
1136147c1e9bSNogah Frankel 
1137147c1e9bSNogah Frankel 	switchdev_port_attr_set(dev, &attr);
1138147c1e9bSNogah Frankel }
1139147c1e9bSNogah Frankel 
11401080ab95SNikolay Aleksandrov int br_multicast_add_port(struct net_bridge_port *port)
1141eb1d1641SHerbert Xu {
11427f0aec7aSNikolay Aleksandrov 	port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
1143eb1d1641SHerbert Xu 
114488c1f37fSAllen Pais 	timer_setup(&port->multicast_router_timer,
114588c1f37fSAllen Pais 		    br_multicast_router_expired, 0);
114688c1f37fSAllen Pais 	timer_setup(&port->ip4_own_query.timer,
114788c1f37fSAllen Pais 		    br_ip4_multicast_port_query_expired, 0);
1148cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
114988c1f37fSAllen Pais 	timer_setup(&port->ip6_own_query.timer,
115088c1f37fSAllen Pais 		    br_ip6_multicast_port_query_expired, 0);
1151cc0fdd80SLinus Lüssing #endif
115213cefad2SNikolay Aleksandrov 	br_mc_disabled_update(port->dev,
115313cefad2SNikolay Aleksandrov 			      br_opt_get(port->br, BROPT_MULTICAST_ENABLED));
1154147c1e9bSNogah Frankel 
11551080ab95SNikolay Aleksandrov 	port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
11561080ab95SNikolay Aleksandrov 	if (!port->mcast_stats)
11571080ab95SNikolay Aleksandrov 		return -ENOMEM;
11581080ab95SNikolay Aleksandrov 
11591080ab95SNikolay Aleksandrov 	return 0;
1160eb1d1641SHerbert Xu }
1161eb1d1641SHerbert Xu 
1162eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port)
1163eb1d1641SHerbert Xu {
1164e10177abSSatish Ashok 	struct net_bridge *br = port->br;
1165e10177abSSatish Ashok 	struct net_bridge_port_group *pg;
1166e10177abSSatish Ashok 	struct hlist_node *n;
1167e10177abSSatish Ashok 
1168e10177abSSatish Ashok 	/* Take care of the remaining groups, only perm ones should be left */
1169e10177abSSatish Ashok 	spin_lock_bh(&br->multicast_lock);
1170e10177abSSatish Ashok 	hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
1171681590bdSNikolay Aleksandrov 		br_multicast_find_del_pg(br, pg);
1172e10177abSSatish Ashok 	spin_unlock_bh(&br->multicast_lock);
1173eb1d1641SHerbert Xu 	del_timer_sync(&port->multicast_router_timer);
11741080ab95SNikolay Aleksandrov 	free_percpu(port->mcast_stats);
1175eb1d1641SHerbert Xu }
1176eb1d1641SHerbert Xu 
117790010b36SLinus Lüssing static void br_multicast_enable(struct bridge_mcast_own_query *query)
1178561f1103SHerbert Xu {
1179cc0fdd80SLinus Lüssing 	query->startup_sent = 0;
1180561f1103SHerbert Xu 
1181cc0fdd80SLinus Lüssing 	if (try_to_del_timer_sync(&query->timer) >= 0 ||
1182cc0fdd80SLinus Lüssing 	    del_timer(&query->timer))
1183cc0fdd80SLinus Lüssing 		mod_timer(&query->timer, jiffies);
1184561f1103SHerbert Xu }
1185561f1103SHerbert Xu 
11867cb3f921SNikolay Aleksandrov static void __br_multicast_enable_port(struct net_bridge_port *port)
1187eb1d1641SHerbert Xu {
1188eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
1189eb1d1641SHerbert Xu 
119013cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED) || !netif_running(br->dev))
11917cb3f921SNikolay Aleksandrov 		return;
1192eb1d1641SHerbert Xu 
119390010b36SLinus Lüssing 	br_multicast_enable(&port->ip4_own_query);
1194cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
119590010b36SLinus Lüssing 	br_multicast_enable(&port->ip6_own_query);
1196cc0fdd80SLinus Lüssing #endif
11977f0aec7aSNikolay Aleksandrov 	if (port->multicast_router == MDB_RTR_TYPE_PERM &&
11987f0aec7aSNikolay Aleksandrov 	    hlist_unhashed(&port->rlist))
1199754bc547SSatish Ashok 		br_multicast_add_router(br, port);
12007cb3f921SNikolay Aleksandrov }
1201eb1d1641SHerbert Xu 
12027cb3f921SNikolay Aleksandrov void br_multicast_enable_port(struct net_bridge_port *port)
12037cb3f921SNikolay Aleksandrov {
12047cb3f921SNikolay Aleksandrov 	struct net_bridge *br = port->br;
12057cb3f921SNikolay Aleksandrov 
12067cb3f921SNikolay Aleksandrov 	spin_lock(&br->multicast_lock);
12077cb3f921SNikolay Aleksandrov 	__br_multicast_enable_port(port);
1208eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1209eb1d1641SHerbert Xu }
1210eb1d1641SHerbert Xu 
1211eb1d1641SHerbert Xu void br_multicast_disable_port(struct net_bridge_port *port)
1212eb1d1641SHerbert Xu {
1213eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
1214eb1d1641SHerbert Xu 	struct net_bridge_port_group *pg;
1215b67bfe0dSSasha Levin 	struct hlist_node *n;
1216eb1d1641SHerbert Xu 
1217eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
1218b67bfe0dSSasha Levin 	hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
12199d06b6d8SElad Raz 		if (!(pg->flags & MDB_PG_FLAGS_PERMANENT))
1220681590bdSNikolay Aleksandrov 			br_multicast_find_del_pg(br, pg);
1221eb1d1641SHerbert Xu 
1222f12e7d95SNogah Frankel 	__del_port_router(port);
1223f12e7d95SNogah Frankel 
1224eb1d1641SHerbert Xu 	del_timer(&port->multicast_router_timer);
122590010b36SLinus Lüssing 	del_timer(&port->ip4_own_query.timer);
1226cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
122790010b36SLinus Lüssing 	del_timer(&port->ip6_own_query.timer);
1228cc0fdd80SLinus Lüssing #endif
1229eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1230eb1d1641SHerbert Xu }
1231eb1d1641SHerbert Xu 
1232e6231bcaSNikolay Aleksandrov static int __grp_src_delete_marked(struct net_bridge_port_group *pg)
1233e6231bcaSNikolay Aleksandrov {
1234e6231bcaSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
1235e6231bcaSNikolay Aleksandrov 	struct hlist_node *tmp;
1236e6231bcaSNikolay Aleksandrov 	int deleted = 0;
1237e6231bcaSNikolay Aleksandrov 
1238e6231bcaSNikolay Aleksandrov 	hlist_for_each_entry_safe(ent, tmp, &pg->src_list, node)
1239e6231bcaSNikolay Aleksandrov 		if (ent->flags & BR_SGRP_F_DELETE) {
1240e6231bcaSNikolay Aleksandrov 			br_multicast_del_group_src(ent);
1241e6231bcaSNikolay Aleksandrov 			deleted++;
1242e6231bcaSNikolay Aleksandrov 		}
1243e6231bcaSNikolay Aleksandrov 
1244e6231bcaSNikolay Aleksandrov 	return deleted;
1245e6231bcaSNikolay Aleksandrov }
1246e6231bcaSNikolay Aleksandrov 
12475bf1e00bSNikolay Aleksandrov static void __grp_src_query_marked_and_rexmit(struct net_bridge_port_group *pg)
12485bf1e00bSNikolay Aleksandrov {
12495bf1e00bSNikolay Aleksandrov 	struct bridge_mcast_other_query *other_query = NULL;
12505bf1e00bSNikolay Aleksandrov 	struct net_bridge *br = pg->port->br;
12515bf1e00bSNikolay Aleksandrov 	u32 lmqc = br->multicast_last_member_count;
12525bf1e00bSNikolay Aleksandrov 	unsigned long lmqt, lmi, now = jiffies;
12535bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
12545bf1e00bSNikolay Aleksandrov 
12555bf1e00bSNikolay Aleksandrov 	if (!netif_running(br->dev) ||
12565bf1e00bSNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_ENABLED))
12575bf1e00bSNikolay Aleksandrov 		return;
12585bf1e00bSNikolay Aleksandrov 
12595bf1e00bSNikolay Aleksandrov 	if (pg->addr.proto == htons(ETH_P_IP))
12605bf1e00bSNikolay Aleksandrov 		other_query = &br->ip4_other_query;
12615bf1e00bSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
12625bf1e00bSNikolay Aleksandrov 	else
12635bf1e00bSNikolay Aleksandrov 		other_query = &br->ip6_other_query;
12645bf1e00bSNikolay Aleksandrov #endif
12655bf1e00bSNikolay Aleksandrov 
12665bf1e00bSNikolay Aleksandrov 	lmqt = now + br_multicast_lmqt(br);
12675bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node) {
12685bf1e00bSNikolay Aleksandrov 		if (ent->flags & BR_SGRP_F_SEND) {
12695bf1e00bSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_SEND;
12705bf1e00bSNikolay Aleksandrov 			if (ent->timer.expires > lmqt) {
12715bf1e00bSNikolay Aleksandrov 				if (br_opt_get(br, BROPT_MULTICAST_QUERIER) &&
12725bf1e00bSNikolay Aleksandrov 				    other_query &&
12735bf1e00bSNikolay Aleksandrov 				    !timer_pending(&other_query->timer))
12745bf1e00bSNikolay Aleksandrov 					ent->src_query_rexmit_cnt = lmqc;
12755bf1e00bSNikolay Aleksandrov 				mod_timer(&ent->timer, lmqt);
12765bf1e00bSNikolay Aleksandrov 			}
12775bf1e00bSNikolay Aleksandrov 		}
12785bf1e00bSNikolay Aleksandrov 	}
12795bf1e00bSNikolay Aleksandrov 
12805bf1e00bSNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_QUERIER) ||
12815bf1e00bSNikolay Aleksandrov 	    !other_query || timer_pending(&other_query->timer))
12825bf1e00bSNikolay Aleksandrov 		return;
12835bf1e00bSNikolay Aleksandrov 
12845bf1e00bSNikolay Aleksandrov 	__br_multicast_send_query(br, pg->port, pg, &pg->addr,
12855bf1e00bSNikolay Aleksandrov 				  &pg->addr, true, 1, NULL);
12865bf1e00bSNikolay Aleksandrov 
12875bf1e00bSNikolay Aleksandrov 	lmi = now + br->multicast_last_member_interval;
12885bf1e00bSNikolay Aleksandrov 	if (!timer_pending(&pg->rexmit_timer) ||
12895bf1e00bSNikolay Aleksandrov 	    time_after(pg->rexmit_timer.expires, lmi))
12905bf1e00bSNikolay Aleksandrov 		mod_timer(&pg->rexmit_timer, lmi);
12915bf1e00bSNikolay Aleksandrov }
12925bf1e00bSNikolay Aleksandrov 
12935bf1e00bSNikolay Aleksandrov static void __grp_send_query_and_rexmit(struct net_bridge_port_group *pg)
12945bf1e00bSNikolay Aleksandrov {
12955bf1e00bSNikolay Aleksandrov 	struct bridge_mcast_other_query *other_query = NULL;
12965bf1e00bSNikolay Aleksandrov 	struct net_bridge *br = pg->port->br;
12975bf1e00bSNikolay Aleksandrov 	unsigned long now = jiffies, lmi;
12985bf1e00bSNikolay Aleksandrov 
12995bf1e00bSNikolay Aleksandrov 	if (!netif_running(br->dev) ||
13005bf1e00bSNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_ENABLED))
13015bf1e00bSNikolay Aleksandrov 		return;
13025bf1e00bSNikolay Aleksandrov 
13035bf1e00bSNikolay Aleksandrov 	if (pg->addr.proto == htons(ETH_P_IP))
13045bf1e00bSNikolay Aleksandrov 		other_query = &br->ip4_other_query;
13055bf1e00bSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
13065bf1e00bSNikolay Aleksandrov 	else
13075bf1e00bSNikolay Aleksandrov 		other_query = &br->ip6_other_query;
13085bf1e00bSNikolay Aleksandrov #endif
13095bf1e00bSNikolay Aleksandrov 
13105bf1e00bSNikolay Aleksandrov 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER) &&
13115bf1e00bSNikolay Aleksandrov 	    other_query && !timer_pending(&other_query->timer)) {
13125bf1e00bSNikolay Aleksandrov 		lmi = now + br->multicast_last_member_interval;
13135bf1e00bSNikolay Aleksandrov 		pg->grp_query_rexmit_cnt = br->multicast_last_member_count - 1;
13145bf1e00bSNikolay Aleksandrov 		__br_multicast_send_query(br, pg->port, pg, &pg->addr,
13155bf1e00bSNikolay Aleksandrov 					  &pg->addr, false, 0, NULL);
13165bf1e00bSNikolay Aleksandrov 		if (!timer_pending(&pg->rexmit_timer) ||
13175bf1e00bSNikolay Aleksandrov 		    time_after(pg->rexmit_timer.expires, lmi))
13185bf1e00bSNikolay Aleksandrov 			mod_timer(&pg->rexmit_timer, lmi);
13195bf1e00bSNikolay Aleksandrov 	}
13205bf1e00bSNikolay Aleksandrov 
13215bf1e00bSNikolay Aleksandrov 	if (pg->filter_mode == MCAST_EXCLUDE &&
13225bf1e00bSNikolay Aleksandrov 	    (!timer_pending(&pg->timer) ||
13235bf1e00bSNikolay Aleksandrov 	     time_after(pg->timer.expires, now + br_multicast_lmqt(br))))
13245bf1e00bSNikolay Aleksandrov 		mod_timer(&pg->timer, now + br_multicast_lmqt(br));
13255bf1e00bSNikolay Aleksandrov }
13265bf1e00bSNikolay Aleksandrov 
13270436862eSNikolay Aleksandrov /* State          Msg type      New state                Actions
13280436862eSNikolay Aleksandrov  * INCLUDE (A)    IS_IN (B)     INCLUDE (A+B)            (B)=GMI
13290436862eSNikolay Aleksandrov  * INCLUDE (A)    ALLOW (B)     INCLUDE (A+B)            (B)=GMI
13300436862eSNikolay Aleksandrov  * EXCLUDE (X,Y)  ALLOW (A)     EXCLUDE (X+A,Y-A)        (A)=GMI
13310436862eSNikolay Aleksandrov  */
13320436862eSNikolay Aleksandrov static bool br_multicast_isinc_allow(struct net_bridge_port_group *pg,
13330436862eSNikolay Aleksandrov 				     void *srcs, u32 nsrcs, size_t src_size)
13340436862eSNikolay Aleksandrov {
13350436862eSNikolay Aleksandrov 	struct net_bridge *br = pg->port->br;
13360436862eSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
13370436862eSNikolay Aleksandrov 	unsigned long now = jiffies;
13380436862eSNikolay Aleksandrov 	bool changed = false;
13390436862eSNikolay Aleksandrov 	struct br_ip src_ip;
13400436862eSNikolay Aleksandrov 	u32 src_idx;
13410436862eSNikolay Aleksandrov 
13420436862eSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
13430436862eSNikolay Aleksandrov 	src_ip.proto = pg->addr.proto;
13440436862eSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
13450436862eSNikolay Aleksandrov 		memcpy(&src_ip.u, srcs, src_size);
13460436862eSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
13470436862eSNikolay Aleksandrov 		if (!ent) {
13480436862eSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
13490436862eSNikolay Aleksandrov 			if (ent)
13500436862eSNikolay Aleksandrov 				changed = true;
13510436862eSNikolay Aleksandrov 		}
13520436862eSNikolay Aleksandrov 
13530436862eSNikolay Aleksandrov 		if (ent)
13540436862eSNikolay Aleksandrov 			mod_timer(&ent->timer, now + br_multicast_gmi(br));
13550436862eSNikolay Aleksandrov 		srcs += src_size;
13560436862eSNikolay Aleksandrov 	}
13570436862eSNikolay Aleksandrov 
13580436862eSNikolay Aleksandrov 	return changed;
13590436862eSNikolay Aleksandrov }
13600436862eSNikolay Aleksandrov 
1361e6231bcaSNikolay Aleksandrov /* State          Msg type      New state                Actions
1362e6231bcaSNikolay Aleksandrov  * INCLUDE (A)    IS_EX (B)     EXCLUDE (A*B,B-A)        (B-A)=0
1363e6231bcaSNikolay Aleksandrov  *                                                       Delete (A-B)
1364e6231bcaSNikolay Aleksandrov  *                                                       Group Timer=GMI
1365e6231bcaSNikolay Aleksandrov  */
1366e6231bcaSNikolay Aleksandrov static void __grp_src_isexc_incl(struct net_bridge_port_group *pg,
1367e6231bcaSNikolay Aleksandrov 				 void *srcs, u32 nsrcs, size_t src_size)
1368e6231bcaSNikolay Aleksandrov {
1369e6231bcaSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
1370e6231bcaSNikolay Aleksandrov 	struct br_ip src_ip;
1371e6231bcaSNikolay Aleksandrov 	u32 src_idx;
1372e6231bcaSNikolay Aleksandrov 
1373e6231bcaSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
1374e6231bcaSNikolay Aleksandrov 		ent->flags |= BR_SGRP_F_DELETE;
1375e6231bcaSNikolay Aleksandrov 
1376e6231bcaSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
1377e6231bcaSNikolay Aleksandrov 	src_ip.proto = pg->addr.proto;
1378e6231bcaSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
1379e6231bcaSNikolay Aleksandrov 		memcpy(&src_ip.u, srcs, src_size);
1380e6231bcaSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
1381e6231bcaSNikolay Aleksandrov 		if (ent)
1382e6231bcaSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_DELETE;
1383e6231bcaSNikolay Aleksandrov 		else
1384e6231bcaSNikolay Aleksandrov 			br_multicast_new_group_src(pg, &src_ip);
1385e6231bcaSNikolay Aleksandrov 		srcs += src_size;
1386e6231bcaSNikolay Aleksandrov 	}
1387e6231bcaSNikolay Aleksandrov 
1388e6231bcaSNikolay Aleksandrov 	__grp_src_delete_marked(pg);
1389e6231bcaSNikolay Aleksandrov }
1390e6231bcaSNikolay Aleksandrov 
1391e6231bcaSNikolay Aleksandrov /* State          Msg type      New state                Actions
1392e6231bcaSNikolay Aleksandrov  * EXCLUDE (X,Y)  IS_EX (A)     EXCLUDE (A-Y,Y*A)        (A-X-Y)=GMI
1393e6231bcaSNikolay Aleksandrov  *                                                       Delete (X-A)
1394e6231bcaSNikolay Aleksandrov  *                                                       Delete (Y-A)
1395e6231bcaSNikolay Aleksandrov  *                                                       Group Timer=GMI
1396e6231bcaSNikolay Aleksandrov  */
1397e6231bcaSNikolay Aleksandrov static bool __grp_src_isexc_excl(struct net_bridge_port_group *pg,
1398e6231bcaSNikolay Aleksandrov 				 void *srcs, u32 nsrcs, size_t src_size)
1399e6231bcaSNikolay Aleksandrov {
1400e6231bcaSNikolay Aleksandrov 	struct net_bridge *br = pg->port->br;
1401e6231bcaSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
1402e6231bcaSNikolay Aleksandrov 	unsigned long now = jiffies;
1403e6231bcaSNikolay Aleksandrov 	bool changed = false;
1404e6231bcaSNikolay Aleksandrov 	struct br_ip src_ip;
1405e6231bcaSNikolay Aleksandrov 	u32 src_idx;
1406e6231bcaSNikolay Aleksandrov 
1407e6231bcaSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
1408e6231bcaSNikolay Aleksandrov 		ent->flags |= BR_SGRP_F_DELETE;
1409e6231bcaSNikolay Aleksandrov 
1410e6231bcaSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
1411e6231bcaSNikolay Aleksandrov 	src_ip.proto = pg->addr.proto;
1412e6231bcaSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
1413e6231bcaSNikolay Aleksandrov 		memcpy(&src_ip.u, srcs, src_size);
1414e6231bcaSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
1415e6231bcaSNikolay Aleksandrov 		if (ent) {
1416e6231bcaSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_DELETE;
1417e6231bcaSNikolay Aleksandrov 		} else {
1418e6231bcaSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
1419e6231bcaSNikolay Aleksandrov 			if (ent) {
1420e6231bcaSNikolay Aleksandrov 				mod_timer(&ent->timer,
1421e6231bcaSNikolay Aleksandrov 					  now + br_multicast_gmi(br));
1422e6231bcaSNikolay Aleksandrov 				changed = true;
1423e6231bcaSNikolay Aleksandrov 			}
1424e6231bcaSNikolay Aleksandrov 		}
1425e6231bcaSNikolay Aleksandrov 		srcs += src_size;
1426e6231bcaSNikolay Aleksandrov 	}
1427e6231bcaSNikolay Aleksandrov 
1428e6231bcaSNikolay Aleksandrov 	if (__grp_src_delete_marked(pg))
1429e6231bcaSNikolay Aleksandrov 		changed = true;
1430e6231bcaSNikolay Aleksandrov 
1431e6231bcaSNikolay Aleksandrov 	return changed;
1432e6231bcaSNikolay Aleksandrov }
1433e6231bcaSNikolay Aleksandrov 
1434e6231bcaSNikolay Aleksandrov static bool br_multicast_isexc(struct net_bridge_port_group *pg,
1435e6231bcaSNikolay Aleksandrov 			       void *srcs, u32 nsrcs, size_t src_size)
1436e6231bcaSNikolay Aleksandrov {
1437e6231bcaSNikolay Aleksandrov 	struct net_bridge *br = pg->port->br;
1438e6231bcaSNikolay Aleksandrov 	bool changed = false;
1439e6231bcaSNikolay Aleksandrov 
1440e6231bcaSNikolay Aleksandrov 	switch (pg->filter_mode) {
1441e6231bcaSNikolay Aleksandrov 	case MCAST_INCLUDE:
1442e6231bcaSNikolay Aleksandrov 		__grp_src_isexc_incl(pg, srcs, nsrcs, src_size);
1443e6231bcaSNikolay Aleksandrov 		changed = true;
1444e6231bcaSNikolay Aleksandrov 		break;
1445e6231bcaSNikolay Aleksandrov 	case MCAST_EXCLUDE:
1446e6231bcaSNikolay Aleksandrov 		changed = __grp_src_isexc_excl(pg, srcs, nsrcs, src_size);
1447e6231bcaSNikolay Aleksandrov 		break;
1448e6231bcaSNikolay Aleksandrov 	}
1449e6231bcaSNikolay Aleksandrov 
1450e6231bcaSNikolay Aleksandrov 	pg->filter_mode = MCAST_EXCLUDE;
1451e6231bcaSNikolay Aleksandrov 	mod_timer(&pg->timer, jiffies + br_multicast_gmi(br));
1452e6231bcaSNikolay Aleksandrov 
1453e6231bcaSNikolay Aleksandrov 	return changed;
1454e6231bcaSNikolay Aleksandrov }
1455e6231bcaSNikolay Aleksandrov 
14565bf1e00bSNikolay Aleksandrov /* State          Msg type      New state                Actions
14575bf1e00bSNikolay Aleksandrov  * INCLUDE (A)    TO_IN (B)     INCLUDE (A+B)            (B)=GMI
14585bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G,A-B)
14595bf1e00bSNikolay Aleksandrov  */
14605bf1e00bSNikolay Aleksandrov static bool __grp_src_toin_incl(struct net_bridge_port_group *pg,
14615bf1e00bSNikolay Aleksandrov 				void *srcs, u32 nsrcs, size_t src_size)
14625bf1e00bSNikolay Aleksandrov {
14635bf1e00bSNikolay Aleksandrov 	struct net_bridge *br = pg->port->br;
14645bf1e00bSNikolay Aleksandrov 	u32 src_idx, to_send = pg->src_ents;
14655bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
14665bf1e00bSNikolay Aleksandrov 	unsigned long now = jiffies;
14675bf1e00bSNikolay Aleksandrov 	bool changed = false;
14685bf1e00bSNikolay Aleksandrov 	struct br_ip src_ip;
14695bf1e00bSNikolay Aleksandrov 
14705bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
14715bf1e00bSNikolay Aleksandrov 		ent->flags |= BR_SGRP_F_SEND;
14725bf1e00bSNikolay Aleksandrov 
14735bf1e00bSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
14745bf1e00bSNikolay Aleksandrov 	src_ip.proto = pg->addr.proto;
14755bf1e00bSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
14765bf1e00bSNikolay Aleksandrov 		memcpy(&src_ip.u, srcs, src_size);
14775bf1e00bSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
14785bf1e00bSNikolay Aleksandrov 		if (ent) {
14795bf1e00bSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_SEND;
14805bf1e00bSNikolay Aleksandrov 			to_send--;
14815bf1e00bSNikolay Aleksandrov 		} else {
14825bf1e00bSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
14835bf1e00bSNikolay Aleksandrov 			if (ent)
14845bf1e00bSNikolay Aleksandrov 				changed = true;
14855bf1e00bSNikolay Aleksandrov 		}
14865bf1e00bSNikolay Aleksandrov 		if (ent)
14875bf1e00bSNikolay Aleksandrov 			mod_timer(&ent->timer, now + br_multicast_gmi(br));
14885bf1e00bSNikolay Aleksandrov 		srcs += src_size;
14895bf1e00bSNikolay Aleksandrov 	}
14905bf1e00bSNikolay Aleksandrov 
14915bf1e00bSNikolay Aleksandrov 	if (to_send)
14925bf1e00bSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
14935bf1e00bSNikolay Aleksandrov 
14945bf1e00bSNikolay Aleksandrov 	return changed;
14955bf1e00bSNikolay Aleksandrov }
14965bf1e00bSNikolay Aleksandrov 
14975bf1e00bSNikolay Aleksandrov /* State          Msg type      New state                Actions
14985bf1e00bSNikolay Aleksandrov  * EXCLUDE (X,Y)  TO_IN (A)     EXCLUDE (X+A,Y-A)        (A)=GMI
14995bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G,X-A)
15005bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G)
15015bf1e00bSNikolay Aleksandrov  */
15025bf1e00bSNikolay Aleksandrov static bool __grp_src_toin_excl(struct net_bridge_port_group *pg,
15035bf1e00bSNikolay Aleksandrov 				void *srcs, u32 nsrcs, size_t src_size)
15045bf1e00bSNikolay Aleksandrov {
15055bf1e00bSNikolay Aleksandrov 	struct net_bridge *br = pg->port->br;
15065bf1e00bSNikolay Aleksandrov 	u32 src_idx, to_send = pg->src_ents;
15075bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
15085bf1e00bSNikolay Aleksandrov 	unsigned long now = jiffies;
15095bf1e00bSNikolay Aleksandrov 	bool changed = false;
15105bf1e00bSNikolay Aleksandrov 	struct br_ip src_ip;
15115bf1e00bSNikolay Aleksandrov 
15125bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
15135bf1e00bSNikolay Aleksandrov 		if (timer_pending(&ent->timer))
15145bf1e00bSNikolay Aleksandrov 			ent->flags |= BR_SGRP_F_SEND;
15155bf1e00bSNikolay Aleksandrov 
15165bf1e00bSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
15175bf1e00bSNikolay Aleksandrov 	src_ip.proto = pg->addr.proto;
15185bf1e00bSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
15195bf1e00bSNikolay Aleksandrov 		memcpy(&src_ip.u, srcs, src_size);
15205bf1e00bSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
15215bf1e00bSNikolay Aleksandrov 		if (ent) {
15225bf1e00bSNikolay Aleksandrov 			if (timer_pending(&ent->timer)) {
15235bf1e00bSNikolay Aleksandrov 				ent->flags &= ~BR_SGRP_F_SEND;
15245bf1e00bSNikolay Aleksandrov 				to_send--;
15255bf1e00bSNikolay Aleksandrov 			}
15265bf1e00bSNikolay Aleksandrov 		} else {
15275bf1e00bSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
15285bf1e00bSNikolay Aleksandrov 			if (ent)
15295bf1e00bSNikolay Aleksandrov 				changed = true;
15305bf1e00bSNikolay Aleksandrov 		}
15315bf1e00bSNikolay Aleksandrov 		if (ent)
15325bf1e00bSNikolay Aleksandrov 			mod_timer(&ent->timer, now + br_multicast_gmi(br));
15335bf1e00bSNikolay Aleksandrov 		srcs += src_size;
15345bf1e00bSNikolay Aleksandrov 	}
15355bf1e00bSNikolay Aleksandrov 
15365bf1e00bSNikolay Aleksandrov 	if (to_send)
15375bf1e00bSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
15385bf1e00bSNikolay Aleksandrov 
15395bf1e00bSNikolay Aleksandrov 	__grp_send_query_and_rexmit(pg);
15405bf1e00bSNikolay Aleksandrov 
15415bf1e00bSNikolay Aleksandrov 	return changed;
15425bf1e00bSNikolay Aleksandrov }
15435bf1e00bSNikolay Aleksandrov 
15445bf1e00bSNikolay Aleksandrov static bool br_multicast_toin(struct net_bridge_port_group *pg,
15455bf1e00bSNikolay Aleksandrov 			      void *srcs, u32 nsrcs, size_t src_size)
15465bf1e00bSNikolay Aleksandrov {
15475bf1e00bSNikolay Aleksandrov 	bool changed = false;
15485bf1e00bSNikolay Aleksandrov 
15495bf1e00bSNikolay Aleksandrov 	switch (pg->filter_mode) {
15505bf1e00bSNikolay Aleksandrov 	case MCAST_INCLUDE:
15515bf1e00bSNikolay Aleksandrov 		changed = __grp_src_toin_incl(pg, srcs, nsrcs, src_size);
15525bf1e00bSNikolay Aleksandrov 		break;
15535bf1e00bSNikolay Aleksandrov 	case MCAST_EXCLUDE:
15545bf1e00bSNikolay Aleksandrov 		changed = __grp_src_toin_excl(pg, srcs, nsrcs, src_size);
15555bf1e00bSNikolay Aleksandrov 		break;
15565bf1e00bSNikolay Aleksandrov 	}
15575bf1e00bSNikolay Aleksandrov 
15585bf1e00bSNikolay Aleksandrov 	return changed;
15595bf1e00bSNikolay Aleksandrov }
15605bf1e00bSNikolay Aleksandrov 
15615bf1e00bSNikolay Aleksandrov /* State          Msg type      New state                Actions
15625bf1e00bSNikolay Aleksandrov  * INCLUDE (A)    TO_EX (B)     EXCLUDE (A*B,B-A)        (B-A)=0
15635bf1e00bSNikolay Aleksandrov  *                                                       Delete (A-B)
15645bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G,A*B)
15655bf1e00bSNikolay Aleksandrov  *                                                       Group Timer=GMI
15665bf1e00bSNikolay Aleksandrov  */
15675bf1e00bSNikolay Aleksandrov static void __grp_src_toex_incl(struct net_bridge_port_group *pg,
15685bf1e00bSNikolay Aleksandrov 				void *srcs, u32 nsrcs, size_t src_size)
15695bf1e00bSNikolay Aleksandrov {
15705bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
15715bf1e00bSNikolay Aleksandrov 	u32 src_idx, to_send = 0;
15725bf1e00bSNikolay Aleksandrov 	struct br_ip src_ip;
15735bf1e00bSNikolay Aleksandrov 
15745bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
15755bf1e00bSNikolay Aleksandrov 		ent->flags = (ent->flags & ~BR_SGRP_F_SEND) | BR_SGRP_F_DELETE;
15765bf1e00bSNikolay Aleksandrov 
15775bf1e00bSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
15785bf1e00bSNikolay Aleksandrov 	src_ip.proto = pg->addr.proto;
15795bf1e00bSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
15805bf1e00bSNikolay Aleksandrov 		memcpy(&src_ip.u, srcs, src_size);
15815bf1e00bSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
15825bf1e00bSNikolay Aleksandrov 		if (ent) {
15835bf1e00bSNikolay Aleksandrov 			ent->flags = (ent->flags & ~BR_SGRP_F_DELETE) |
15845bf1e00bSNikolay Aleksandrov 				     BR_SGRP_F_SEND;
15855bf1e00bSNikolay Aleksandrov 			to_send++;
15865bf1e00bSNikolay Aleksandrov 		} else {
15875bf1e00bSNikolay Aleksandrov 			br_multicast_new_group_src(pg, &src_ip);
15885bf1e00bSNikolay Aleksandrov 		}
15895bf1e00bSNikolay Aleksandrov 		srcs += src_size;
15905bf1e00bSNikolay Aleksandrov 	}
15915bf1e00bSNikolay Aleksandrov 
15925bf1e00bSNikolay Aleksandrov 	__grp_src_delete_marked(pg);
15935bf1e00bSNikolay Aleksandrov 	if (to_send)
15945bf1e00bSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
15955bf1e00bSNikolay Aleksandrov }
15965bf1e00bSNikolay Aleksandrov 
15975bf1e00bSNikolay Aleksandrov /* State          Msg type      New state                Actions
15985bf1e00bSNikolay Aleksandrov  * EXCLUDE (X,Y)  TO_EX (A)     EXCLUDE (A-Y,Y*A)        (A-X-Y)=Group Timer
15995bf1e00bSNikolay Aleksandrov  *                                                       Delete (X-A)
16005bf1e00bSNikolay Aleksandrov  *                                                       Delete (Y-A)
16015bf1e00bSNikolay Aleksandrov  *                                                       Send Q(G,A-Y)
16025bf1e00bSNikolay Aleksandrov  *                                                       Group Timer=GMI
16035bf1e00bSNikolay Aleksandrov  */
16045bf1e00bSNikolay Aleksandrov static bool __grp_src_toex_excl(struct net_bridge_port_group *pg,
16055bf1e00bSNikolay Aleksandrov 				void *srcs, u32 nsrcs, size_t src_size)
16065bf1e00bSNikolay Aleksandrov {
16075bf1e00bSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
16085bf1e00bSNikolay Aleksandrov 	u32 src_idx, to_send = 0;
16095bf1e00bSNikolay Aleksandrov 	bool changed = false;
16105bf1e00bSNikolay Aleksandrov 	struct br_ip src_ip;
16115bf1e00bSNikolay Aleksandrov 
16125bf1e00bSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
16135bf1e00bSNikolay Aleksandrov 		ent->flags = (ent->flags & ~BR_SGRP_F_SEND) | BR_SGRP_F_DELETE;
16145bf1e00bSNikolay Aleksandrov 
16155bf1e00bSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
16165bf1e00bSNikolay Aleksandrov 	src_ip.proto = pg->addr.proto;
16175bf1e00bSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
16185bf1e00bSNikolay Aleksandrov 		memcpy(&src_ip.u, srcs, src_size);
16195bf1e00bSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
16205bf1e00bSNikolay Aleksandrov 		if (ent) {
16215bf1e00bSNikolay Aleksandrov 			ent->flags &= ~BR_SGRP_F_DELETE;
16225bf1e00bSNikolay Aleksandrov 		} else {
16235bf1e00bSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
16245bf1e00bSNikolay Aleksandrov 			if (ent) {
16255bf1e00bSNikolay Aleksandrov 				mod_timer(&ent->timer, pg->timer.expires);
16265bf1e00bSNikolay Aleksandrov 				changed = true;
16275bf1e00bSNikolay Aleksandrov 			}
16285bf1e00bSNikolay Aleksandrov 		}
16295bf1e00bSNikolay Aleksandrov 		if (ent && timer_pending(&ent->timer)) {
16305bf1e00bSNikolay Aleksandrov 			ent->flags |= BR_SGRP_F_SEND;
16315bf1e00bSNikolay Aleksandrov 			to_send++;
16325bf1e00bSNikolay Aleksandrov 		}
16335bf1e00bSNikolay Aleksandrov 		srcs += src_size;
16345bf1e00bSNikolay Aleksandrov 	}
16355bf1e00bSNikolay Aleksandrov 
16365bf1e00bSNikolay Aleksandrov 	if (__grp_src_delete_marked(pg))
16375bf1e00bSNikolay Aleksandrov 		changed = true;
16385bf1e00bSNikolay Aleksandrov 	if (to_send)
16395bf1e00bSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
16405bf1e00bSNikolay Aleksandrov 
16415bf1e00bSNikolay Aleksandrov 	return changed;
16425bf1e00bSNikolay Aleksandrov }
16435bf1e00bSNikolay Aleksandrov 
16445bf1e00bSNikolay Aleksandrov static bool br_multicast_toex(struct net_bridge_port_group *pg,
16455bf1e00bSNikolay Aleksandrov 			      void *srcs, u32 nsrcs, size_t src_size)
16465bf1e00bSNikolay Aleksandrov {
16475bf1e00bSNikolay Aleksandrov 	struct net_bridge *br = pg->port->br;
16485bf1e00bSNikolay Aleksandrov 	bool changed = false;
16495bf1e00bSNikolay Aleksandrov 
16505bf1e00bSNikolay Aleksandrov 	switch (pg->filter_mode) {
16515bf1e00bSNikolay Aleksandrov 	case MCAST_INCLUDE:
16525bf1e00bSNikolay Aleksandrov 		__grp_src_toex_incl(pg, srcs, nsrcs, src_size);
16535bf1e00bSNikolay Aleksandrov 		changed = true;
16545bf1e00bSNikolay Aleksandrov 		break;
16555bf1e00bSNikolay Aleksandrov 	case MCAST_EXCLUDE:
16565bf1e00bSNikolay Aleksandrov 		__grp_src_toex_excl(pg, srcs, nsrcs, src_size);
16575bf1e00bSNikolay Aleksandrov 		break;
16585bf1e00bSNikolay Aleksandrov 	}
16595bf1e00bSNikolay Aleksandrov 
16605bf1e00bSNikolay Aleksandrov 	pg->filter_mode = MCAST_EXCLUDE;
16615bf1e00bSNikolay Aleksandrov 	mod_timer(&pg->timer, jiffies + br_multicast_gmi(br));
16625bf1e00bSNikolay Aleksandrov 
16635bf1e00bSNikolay Aleksandrov 	return changed;
16645bf1e00bSNikolay Aleksandrov }
16655bf1e00bSNikolay Aleksandrov 
1666*109865feSNikolay Aleksandrov /* State          Msg type      New state                Actions
1667*109865feSNikolay Aleksandrov  * INCLUDE (A)    BLOCK (B)     INCLUDE (A)              Send Q(G,A*B)
1668*109865feSNikolay Aleksandrov  */
1669*109865feSNikolay Aleksandrov static void __grp_src_block_incl(struct net_bridge_port_group *pg,
1670*109865feSNikolay Aleksandrov 				 void *srcs, u32 nsrcs, size_t src_size)
1671*109865feSNikolay Aleksandrov {
1672*109865feSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
1673*109865feSNikolay Aleksandrov 	u32 src_idx, to_send = 0;
1674*109865feSNikolay Aleksandrov 	struct br_ip src_ip;
1675*109865feSNikolay Aleksandrov 
1676*109865feSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
1677*109865feSNikolay Aleksandrov 		ent->flags &= ~BR_SGRP_F_SEND;
1678*109865feSNikolay Aleksandrov 
1679*109865feSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
1680*109865feSNikolay Aleksandrov 	src_ip.proto = pg->addr.proto;
1681*109865feSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
1682*109865feSNikolay Aleksandrov 		memcpy(&src_ip.u, srcs, src_size);
1683*109865feSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
1684*109865feSNikolay Aleksandrov 		if (ent) {
1685*109865feSNikolay Aleksandrov 			ent->flags |= BR_SGRP_F_SEND;
1686*109865feSNikolay Aleksandrov 			to_send++;
1687*109865feSNikolay Aleksandrov 		}
1688*109865feSNikolay Aleksandrov 		srcs += src_size;
1689*109865feSNikolay Aleksandrov 	}
1690*109865feSNikolay Aleksandrov 
1691*109865feSNikolay Aleksandrov 	if (to_send)
1692*109865feSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
1693*109865feSNikolay Aleksandrov 
1694*109865feSNikolay Aleksandrov 	if (pg->filter_mode == MCAST_INCLUDE && hlist_empty(&pg->src_list))
1695*109865feSNikolay Aleksandrov 		br_multicast_find_del_pg(pg->port->br, pg);
1696*109865feSNikolay Aleksandrov }
1697*109865feSNikolay Aleksandrov 
1698*109865feSNikolay Aleksandrov /* State          Msg type      New state                Actions
1699*109865feSNikolay Aleksandrov  * EXCLUDE (X,Y)  BLOCK (A)     EXCLUDE (X+(A-Y),Y)      (A-X-Y)=Group Timer
1700*109865feSNikolay Aleksandrov  *                                                       Send Q(G,A-Y)
1701*109865feSNikolay Aleksandrov  */
1702*109865feSNikolay Aleksandrov static bool __grp_src_block_excl(struct net_bridge_port_group *pg,
1703*109865feSNikolay Aleksandrov 				 void *srcs, u32 nsrcs, size_t src_size)
1704*109865feSNikolay Aleksandrov {
1705*109865feSNikolay Aleksandrov 	struct net_bridge_group_src *ent;
1706*109865feSNikolay Aleksandrov 	u32 src_idx, to_send = 0;
1707*109865feSNikolay Aleksandrov 	bool changed = false;
1708*109865feSNikolay Aleksandrov 	struct br_ip src_ip;
1709*109865feSNikolay Aleksandrov 
1710*109865feSNikolay Aleksandrov 	hlist_for_each_entry(ent, &pg->src_list, node)
1711*109865feSNikolay Aleksandrov 		ent->flags &= ~BR_SGRP_F_SEND;
1712*109865feSNikolay Aleksandrov 
1713*109865feSNikolay Aleksandrov 	memset(&src_ip, 0, sizeof(src_ip));
1714*109865feSNikolay Aleksandrov 	src_ip.proto = pg->addr.proto;
1715*109865feSNikolay Aleksandrov 	for (src_idx = 0; src_idx < nsrcs; src_idx++) {
1716*109865feSNikolay Aleksandrov 		memcpy(&src_ip.u, srcs, src_size);
1717*109865feSNikolay Aleksandrov 		ent = br_multicast_find_group_src(pg, &src_ip);
1718*109865feSNikolay Aleksandrov 		if (!ent) {
1719*109865feSNikolay Aleksandrov 			ent = br_multicast_new_group_src(pg, &src_ip);
1720*109865feSNikolay Aleksandrov 			if (ent) {
1721*109865feSNikolay Aleksandrov 				mod_timer(&ent->timer, pg->timer.expires);
1722*109865feSNikolay Aleksandrov 				changed = true;
1723*109865feSNikolay Aleksandrov 			}
1724*109865feSNikolay Aleksandrov 		}
1725*109865feSNikolay Aleksandrov 		if (ent && timer_pending(&ent->timer)) {
1726*109865feSNikolay Aleksandrov 			ent->flags |= BR_SGRP_F_SEND;
1727*109865feSNikolay Aleksandrov 			to_send++;
1728*109865feSNikolay Aleksandrov 		}
1729*109865feSNikolay Aleksandrov 		srcs += src_size;
1730*109865feSNikolay Aleksandrov 	}
1731*109865feSNikolay Aleksandrov 
1732*109865feSNikolay Aleksandrov 	if (to_send)
1733*109865feSNikolay Aleksandrov 		__grp_src_query_marked_and_rexmit(pg);
1734*109865feSNikolay Aleksandrov 
1735*109865feSNikolay Aleksandrov 	return changed;
1736*109865feSNikolay Aleksandrov }
1737*109865feSNikolay Aleksandrov 
1738*109865feSNikolay Aleksandrov static bool br_multicast_block(struct net_bridge_port_group *pg,
1739*109865feSNikolay Aleksandrov 			       void *srcs, u32 nsrcs, size_t src_size)
1740*109865feSNikolay Aleksandrov {
1741*109865feSNikolay Aleksandrov 	bool changed = false;
1742*109865feSNikolay Aleksandrov 
1743*109865feSNikolay Aleksandrov 	switch (pg->filter_mode) {
1744*109865feSNikolay Aleksandrov 	case MCAST_INCLUDE:
1745*109865feSNikolay Aleksandrov 		__grp_src_block_incl(pg, srcs, nsrcs, src_size);
1746*109865feSNikolay Aleksandrov 		break;
1747*109865feSNikolay Aleksandrov 	case MCAST_EXCLUDE:
1748*109865feSNikolay Aleksandrov 		changed = __grp_src_block_excl(pg, srcs, nsrcs, src_size);
1749*109865feSNikolay Aleksandrov 		break;
1750*109865feSNikolay Aleksandrov 	}
1751*109865feSNikolay Aleksandrov 
1752*109865feSNikolay Aleksandrov 	return changed;
1753*109865feSNikolay Aleksandrov }
1754*109865feSNikolay Aleksandrov 
17550436862eSNikolay Aleksandrov static struct net_bridge_port_group *
17560436862eSNikolay Aleksandrov br_multicast_find_port(struct net_bridge_mdb_entry *mp,
17570436862eSNikolay Aleksandrov 		       struct net_bridge_port *p,
17580436862eSNikolay Aleksandrov 		       const unsigned char *src)
17590436862eSNikolay Aleksandrov {
17600436862eSNikolay Aleksandrov 	struct net_bridge_port_group *pg;
17610436862eSNikolay Aleksandrov 	struct net_bridge *br = mp->br;
17620436862eSNikolay Aleksandrov 
17630436862eSNikolay Aleksandrov 	for (pg = mlock_dereference(mp->ports, br);
17640436862eSNikolay Aleksandrov 	     pg;
17650436862eSNikolay Aleksandrov 	     pg = mlock_dereference(pg->next, br))
17660436862eSNikolay Aleksandrov 		if (br_port_group_equal(pg, p, src))
17670436862eSNikolay Aleksandrov 			return pg;
17680436862eSNikolay Aleksandrov 
17690436862eSNikolay Aleksandrov 	return NULL;
17700436862eSNikolay Aleksandrov }
17710436862eSNikolay Aleksandrov 
17728ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
1773eb1d1641SHerbert Xu 					 struct net_bridge_port *port,
177406499098SVlad Yasevich 					 struct sk_buff *skb,
177506499098SVlad Yasevich 					 u16 vid)
1776eb1d1641SHerbert Xu {
17770436862eSNikolay Aleksandrov 	bool igmpv2 = br->multicast_igmp_version == 2;
17780436862eSNikolay Aleksandrov 	struct net_bridge_mdb_entry *mdst;
17790436862eSNikolay Aleksandrov 	struct net_bridge_port_group *pg;
17806db6f0eaSFelix Fietkau 	const unsigned char *src;
1781eb1d1641SHerbert Xu 	struct igmpv3_report *ih;
1782eb1d1641SHerbert Xu 	struct igmpv3_grec *grec;
17830436862eSNikolay Aleksandrov 	int i, len, num, type;
17840436862eSNikolay Aleksandrov 	bool changed = false;
1785eb1d1641SHerbert Xu 	__be32 group;
17860436862eSNikolay Aleksandrov 	int err = 0;
1787e57f6185SNikolay Aleksandrov 	u16 nsrcs;
1788eb1d1641SHerbert Xu 
1789eb1d1641SHerbert Xu 	ih = igmpv3_report_hdr(skb);
1790eb1d1641SHerbert Xu 	num = ntohs(ih->ngrec);
1791c2d4fbd2SLinus Lüssing 	len = skb_transport_offset(skb) + sizeof(*ih);
1792eb1d1641SHerbert Xu 
1793eb1d1641SHerbert Xu 	for (i = 0; i < num; i++) {
1794eb1d1641SHerbert Xu 		len += sizeof(*grec);
1795ba5ea614SLinus Lüssing 		if (!ip_mc_may_pull(skb, len))
1796eb1d1641SHerbert Xu 			return -EINVAL;
1797eb1d1641SHerbert Xu 
1798fd218cf9SHerbert Xu 		grec = (void *)(skb->data + len - sizeof(*grec));
1799eb1d1641SHerbert Xu 		group = grec->grec_mca;
1800eb1d1641SHerbert Xu 		type = grec->grec_type;
1801e57f6185SNikolay Aleksandrov 		nsrcs = ntohs(grec->grec_nsrcs);
1802eb1d1641SHerbert Xu 
1803e57f6185SNikolay Aleksandrov 		len += nsrcs * 4;
1804ba5ea614SLinus Lüssing 		if (!ip_mc_may_pull(skb, len))
1805eb1d1641SHerbert Xu 			return -EINVAL;
1806eb1d1641SHerbert Xu 
1807eb1d1641SHerbert Xu 		switch (type) {
1808eb1d1641SHerbert Xu 		case IGMPV3_MODE_IS_INCLUDE:
1809eb1d1641SHerbert Xu 		case IGMPV3_MODE_IS_EXCLUDE:
1810eb1d1641SHerbert Xu 		case IGMPV3_CHANGE_TO_INCLUDE:
1811eb1d1641SHerbert Xu 		case IGMPV3_CHANGE_TO_EXCLUDE:
1812eb1d1641SHerbert Xu 		case IGMPV3_ALLOW_NEW_SOURCES:
1813eb1d1641SHerbert Xu 		case IGMPV3_BLOCK_OLD_SOURCES:
1814eb1d1641SHerbert Xu 			break;
1815eb1d1641SHerbert Xu 
1816eb1d1641SHerbert Xu 		default:
1817eb1d1641SHerbert Xu 			continue;
1818eb1d1641SHerbert Xu 		}
1819eb1d1641SHerbert Xu 
18206db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
18210436862eSNikolay Aleksandrov 		if (nsrcs == 0 &&
18220436862eSNikolay Aleksandrov 		    (type == IGMPV3_CHANGE_TO_INCLUDE ||
18230436862eSNikolay Aleksandrov 		     type == IGMPV3_MODE_IS_INCLUDE)) {
18240436862eSNikolay Aleksandrov 			if (!port || igmpv2) {
18256db6f0eaSFelix Fietkau 				br_ip4_multicast_leave_group(br, port, group, vid, src);
18260436862eSNikolay Aleksandrov 				continue;
18270436862eSNikolay Aleksandrov 			}
1828bc8c20acSSatish Ashok 		} else {
18296db6f0eaSFelix Fietkau 			err = br_ip4_multicast_add_group(br, port, group, vid,
18300436862eSNikolay Aleksandrov 							 src, igmpv2);
1831eb1d1641SHerbert Xu 			if (err)
1832eb1d1641SHerbert Xu 				break;
1833eb1d1641SHerbert Xu 		}
18340436862eSNikolay Aleksandrov 
18350436862eSNikolay Aleksandrov 		if (!port || igmpv2)
18360436862eSNikolay Aleksandrov 			continue;
18370436862eSNikolay Aleksandrov 
18380436862eSNikolay Aleksandrov 		spin_lock_bh(&br->multicast_lock);
18390436862eSNikolay Aleksandrov 		mdst = br_mdb_ip4_get(br, group, vid);
18400436862eSNikolay Aleksandrov 		if (!mdst)
18410436862eSNikolay Aleksandrov 			goto unlock_continue;
18420436862eSNikolay Aleksandrov 		pg = br_multicast_find_port(mdst, port, src);
18430436862eSNikolay Aleksandrov 		if (!pg || (pg->flags & MDB_PG_FLAGS_PERMANENT))
18440436862eSNikolay Aleksandrov 			goto unlock_continue;
18450436862eSNikolay Aleksandrov 		/* reload grec */
18460436862eSNikolay Aleksandrov 		grec = (void *)(skb->data + len - sizeof(*grec) - (nsrcs * 4));
18470436862eSNikolay Aleksandrov 		switch (type) {
18480436862eSNikolay Aleksandrov 		case IGMPV3_ALLOW_NEW_SOURCES:
18490436862eSNikolay Aleksandrov 			changed = br_multicast_isinc_allow(pg, grec->grec_src,
18500436862eSNikolay Aleksandrov 							   nsrcs, sizeof(__be32));
18510436862eSNikolay Aleksandrov 			break;
1852e6231bcaSNikolay Aleksandrov 		case IGMPV3_MODE_IS_INCLUDE:
1853e6231bcaSNikolay Aleksandrov 			changed = br_multicast_isinc_allow(pg, grec->grec_src, nsrcs,
1854e6231bcaSNikolay Aleksandrov 							   sizeof(__be32));
1855e6231bcaSNikolay Aleksandrov 			break;
1856e6231bcaSNikolay Aleksandrov 		case IGMPV3_MODE_IS_EXCLUDE:
1857e6231bcaSNikolay Aleksandrov 			changed = br_multicast_isexc(pg, grec->grec_src, nsrcs,
1858e6231bcaSNikolay Aleksandrov 						     sizeof(__be32));
1859e6231bcaSNikolay Aleksandrov 			break;
18605bf1e00bSNikolay Aleksandrov 		case IGMPV3_CHANGE_TO_INCLUDE:
18615bf1e00bSNikolay Aleksandrov 			changed = br_multicast_toin(pg, grec->grec_src, nsrcs,
18625bf1e00bSNikolay Aleksandrov 						    sizeof(__be32));
18635bf1e00bSNikolay Aleksandrov 			break;
18645bf1e00bSNikolay Aleksandrov 		case IGMPV3_CHANGE_TO_EXCLUDE:
18655bf1e00bSNikolay Aleksandrov 			changed = br_multicast_toex(pg, grec->grec_src, nsrcs,
18665bf1e00bSNikolay Aleksandrov 						    sizeof(__be32));
18675bf1e00bSNikolay Aleksandrov 			break;
1868*109865feSNikolay Aleksandrov 		case IGMPV3_BLOCK_OLD_SOURCES:
1869*109865feSNikolay Aleksandrov 			changed = br_multicast_block(pg, grec->grec_src, nsrcs,
1870*109865feSNikolay Aleksandrov 						     sizeof(__be32));
1871*109865feSNikolay Aleksandrov 			break;
18720436862eSNikolay Aleksandrov 		}
18730436862eSNikolay Aleksandrov 		if (changed)
18740436862eSNikolay Aleksandrov 			br_mdb_notify(br->dev, mdst, pg, RTM_NEWMDB);
18750436862eSNikolay Aleksandrov unlock_continue:
18760436862eSNikolay Aleksandrov 		spin_unlock_bh(&br->multicast_lock);
1877bc8c20acSSatish Ashok 	}
1878eb1d1641SHerbert Xu 
1879eb1d1641SHerbert Xu 	return err;
1880eb1d1641SHerbert Xu }
1881eb1d1641SHerbert Xu 
1882dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
188308b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_mld2_report(struct net_bridge *br,
188408b202b6SYOSHIFUJI Hideaki 					struct net_bridge_port *port,
188506499098SVlad Yasevich 					struct sk_buff *skb,
188606499098SVlad Yasevich 					u16 vid)
188708b202b6SYOSHIFUJI Hideaki {
18880436862eSNikolay Aleksandrov 	bool mldv1 = br->multicast_mld_version == 1;
18890436862eSNikolay Aleksandrov 	struct net_bridge_mdb_entry *mdst;
18900436862eSNikolay Aleksandrov 	struct net_bridge_port_group *pg;
1891ba5ea614SLinus Lüssing 	unsigned int nsrcs_offset;
18926db6f0eaSFelix Fietkau 	const unsigned char *src;
189308b202b6SYOSHIFUJI Hideaki 	struct icmp6hdr *icmp6h;
189408b202b6SYOSHIFUJI Hideaki 	struct mld2_grec *grec;
1895ba5ea614SLinus Lüssing 	unsigned int grec_len;
18960436862eSNikolay Aleksandrov 	bool changed = false;
18970436862eSNikolay Aleksandrov 	int i, len, num;
189808b202b6SYOSHIFUJI Hideaki 	int err = 0;
189908b202b6SYOSHIFUJI Hideaki 
1900ba5ea614SLinus Lüssing 	if (!ipv6_mc_may_pull(skb, sizeof(*icmp6h)))
190108b202b6SYOSHIFUJI Hideaki 		return -EINVAL;
190208b202b6SYOSHIFUJI Hideaki 
190308b202b6SYOSHIFUJI Hideaki 	icmp6h = icmp6_hdr(skb);
190408b202b6SYOSHIFUJI Hideaki 	num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
1905c2d4fbd2SLinus Lüssing 	len = skb_transport_offset(skb) + sizeof(*icmp6h);
190608b202b6SYOSHIFUJI Hideaki 
190708b202b6SYOSHIFUJI Hideaki 	for (i = 0; i < num; i++) {
1908e57f6185SNikolay Aleksandrov 		__be16 *_nsrcs, __nsrcs;
1909e57f6185SNikolay Aleksandrov 		u16 nsrcs;
191008b202b6SYOSHIFUJI Hideaki 
1911ba5ea614SLinus Lüssing 		nsrcs_offset = len + offsetof(struct mld2_grec, grec_nsrcs);
1912ba5ea614SLinus Lüssing 
1913ba5ea614SLinus Lüssing 		if (skb_transport_offset(skb) + ipv6_transport_len(skb) <
19145fc6266aSLinus Lüssing 		    nsrcs_offset + sizeof(__nsrcs))
1915ba5ea614SLinus Lüssing 			return -EINVAL;
1916ba5ea614SLinus Lüssing 
1917e57f6185SNikolay Aleksandrov 		_nsrcs = skb_header_pointer(skb, nsrcs_offset,
1918e57f6185SNikolay Aleksandrov 					    sizeof(__nsrcs), &__nsrcs);
1919e57f6185SNikolay Aleksandrov 		if (!_nsrcs)
192008b202b6SYOSHIFUJI Hideaki 			return -EINVAL;
192108b202b6SYOSHIFUJI Hideaki 
1922e57f6185SNikolay Aleksandrov 		nsrcs = ntohs(*_nsrcs);
1923e57f6185SNikolay Aleksandrov 		grec_len = struct_size(grec, grec_src, nsrcs);
1924ba5ea614SLinus Lüssing 
1925ba5ea614SLinus Lüssing 		if (!ipv6_mc_may_pull(skb, len + grec_len))
192608b202b6SYOSHIFUJI Hideaki 			return -EINVAL;
192708b202b6SYOSHIFUJI Hideaki 
192808b202b6SYOSHIFUJI Hideaki 		grec = (struct mld2_grec *)(skb->data + len);
1929ba5ea614SLinus Lüssing 		len += grec_len;
193008b202b6SYOSHIFUJI Hideaki 
193108b202b6SYOSHIFUJI Hideaki 		switch (grec->grec_type) {
193208b202b6SYOSHIFUJI Hideaki 		case MLD2_MODE_IS_INCLUDE:
193308b202b6SYOSHIFUJI Hideaki 		case MLD2_MODE_IS_EXCLUDE:
193408b202b6SYOSHIFUJI Hideaki 		case MLD2_CHANGE_TO_INCLUDE:
193508b202b6SYOSHIFUJI Hideaki 		case MLD2_CHANGE_TO_EXCLUDE:
193608b202b6SYOSHIFUJI Hideaki 		case MLD2_ALLOW_NEW_SOURCES:
193708b202b6SYOSHIFUJI Hideaki 		case MLD2_BLOCK_OLD_SOURCES:
193808b202b6SYOSHIFUJI Hideaki 			break;
193908b202b6SYOSHIFUJI Hideaki 
194008b202b6SYOSHIFUJI Hideaki 		default:
194108b202b6SYOSHIFUJI Hideaki 			continue;
194208b202b6SYOSHIFUJI Hideaki 		}
194308b202b6SYOSHIFUJI Hideaki 
19446db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
1945bc8c20acSSatish Ashok 		if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE ||
1946bc8c20acSSatish Ashok 		     grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
1947e57f6185SNikolay Aleksandrov 		    nsrcs == 0) {
19480436862eSNikolay Aleksandrov 			if (!port || mldv1) {
19490436862eSNikolay Aleksandrov 				br_ip6_multicast_leave_group(br, port,
19500436862eSNikolay Aleksandrov 							     &grec->grec_mca,
19516db6f0eaSFelix Fietkau 							     vid, src);
19520436862eSNikolay Aleksandrov 				continue;
19530436862eSNikolay Aleksandrov 			}
1954bc8c20acSSatish Ashok 		} else {
1955bc8c20acSSatish Ashok 			err = br_ip6_multicast_add_group(br, port,
19566db6f0eaSFelix Fietkau 							 &grec->grec_mca, vid,
19570436862eSNikolay Aleksandrov 							 src, mldv1);
19589264251eSDavide Caratti 			if (err)
195908b202b6SYOSHIFUJI Hideaki 				break;
196008b202b6SYOSHIFUJI Hideaki 		}
19610436862eSNikolay Aleksandrov 
19620436862eSNikolay Aleksandrov 		if (!port || mldv1)
19630436862eSNikolay Aleksandrov 			continue;
19640436862eSNikolay Aleksandrov 
19650436862eSNikolay Aleksandrov 		spin_lock_bh(&br->multicast_lock);
19660436862eSNikolay Aleksandrov 		mdst = br_mdb_ip6_get(br, &grec->grec_mca, vid);
19670436862eSNikolay Aleksandrov 		if (!mdst)
19680436862eSNikolay Aleksandrov 			goto unlock_continue;
19690436862eSNikolay Aleksandrov 		pg = br_multicast_find_port(mdst, port, src);
19700436862eSNikolay Aleksandrov 		if (!pg || (pg->flags & MDB_PG_FLAGS_PERMANENT))
19710436862eSNikolay Aleksandrov 			goto unlock_continue;
19720436862eSNikolay Aleksandrov 		switch (grec->grec_type) {
19730436862eSNikolay Aleksandrov 		case MLD2_ALLOW_NEW_SOURCES:
19740436862eSNikolay Aleksandrov 			changed = br_multicast_isinc_allow(pg, grec->grec_src,
19750436862eSNikolay Aleksandrov 							   nsrcs,
19760436862eSNikolay Aleksandrov 							   sizeof(struct in6_addr));
19770436862eSNikolay Aleksandrov 			break;
1978e6231bcaSNikolay Aleksandrov 		case MLD2_MODE_IS_INCLUDE:
1979e6231bcaSNikolay Aleksandrov 			changed = br_multicast_isinc_allow(pg, grec->grec_src, nsrcs,
1980e6231bcaSNikolay Aleksandrov 							   sizeof(struct in6_addr));
1981e6231bcaSNikolay Aleksandrov 			break;
1982e6231bcaSNikolay Aleksandrov 		case MLD2_MODE_IS_EXCLUDE:
1983e6231bcaSNikolay Aleksandrov 			changed = br_multicast_isexc(pg, grec->grec_src, nsrcs,
1984e6231bcaSNikolay Aleksandrov 						     sizeof(struct in6_addr));
1985e6231bcaSNikolay Aleksandrov 			break;
19865bf1e00bSNikolay Aleksandrov 		case MLD2_CHANGE_TO_INCLUDE:
19875bf1e00bSNikolay Aleksandrov 			changed = br_multicast_toin(pg, grec->grec_src, nsrcs,
19885bf1e00bSNikolay Aleksandrov 						    sizeof(struct in6_addr));
19895bf1e00bSNikolay Aleksandrov 			break;
19905bf1e00bSNikolay Aleksandrov 		case MLD2_CHANGE_TO_EXCLUDE:
19915bf1e00bSNikolay Aleksandrov 			changed = br_multicast_toex(pg, grec->grec_src, nsrcs,
19925bf1e00bSNikolay Aleksandrov 						    sizeof(struct in6_addr));
19935bf1e00bSNikolay Aleksandrov 			break;
1994*109865feSNikolay Aleksandrov 		case MLD2_BLOCK_OLD_SOURCES:
1995*109865feSNikolay Aleksandrov 			changed = br_multicast_block(pg, grec->grec_src, nsrcs,
1996*109865feSNikolay Aleksandrov 						     sizeof(struct in6_addr));
1997*109865feSNikolay Aleksandrov 			break;
19980436862eSNikolay Aleksandrov 		}
19990436862eSNikolay Aleksandrov 		if (changed)
20000436862eSNikolay Aleksandrov 			br_mdb_notify(br->dev, mdst, pg, RTM_NEWMDB);
20010436862eSNikolay Aleksandrov unlock_continue:
20020436862eSNikolay Aleksandrov 		spin_unlock_bh(&br->multicast_lock);
2003bc8c20acSSatish Ashok 	}
200408b202b6SYOSHIFUJI Hideaki 
200508b202b6SYOSHIFUJI Hideaki 	return err;
200608b202b6SYOSHIFUJI Hideaki }
200708b202b6SYOSHIFUJI Hideaki #endif
200808b202b6SYOSHIFUJI Hideaki 
2009dc4eb53aSLinus Lüssing static bool br_ip4_multicast_select_querier(struct net_bridge *br,
20102cd41431SLinus Lüssing 					    struct net_bridge_port *port,
2011dc4eb53aSLinus Lüssing 					    __be32 saddr)
2012dc4eb53aSLinus Lüssing {
2013dc4eb53aSLinus Lüssing 	if (!timer_pending(&br->ip4_own_query.timer) &&
2014dc4eb53aSLinus Lüssing 	    !timer_pending(&br->ip4_other_query.timer))
2015dc4eb53aSLinus Lüssing 		goto update;
2016dc4eb53aSLinus Lüssing 
2017dc4eb53aSLinus Lüssing 	if (!br->ip4_querier.addr.u.ip4)
2018dc4eb53aSLinus Lüssing 		goto update;
2019dc4eb53aSLinus Lüssing 
2020dc4eb53aSLinus Lüssing 	if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4))
2021dc4eb53aSLinus Lüssing 		goto update;
2022dc4eb53aSLinus Lüssing 
2023dc4eb53aSLinus Lüssing 	return false;
2024dc4eb53aSLinus Lüssing 
2025dc4eb53aSLinus Lüssing update:
2026dc4eb53aSLinus Lüssing 	br->ip4_querier.addr.u.ip4 = saddr;
2027dc4eb53aSLinus Lüssing 
20282cd41431SLinus Lüssing 	/* update protected by general multicast_lock by caller */
20292cd41431SLinus Lüssing 	rcu_assign_pointer(br->ip4_querier.port, port);
20302cd41431SLinus Lüssing 
2031dc4eb53aSLinus Lüssing 	return true;
2032dc4eb53aSLinus Lüssing }
2033dc4eb53aSLinus Lüssing 
2034dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
2035dc4eb53aSLinus Lüssing static bool br_ip6_multicast_select_querier(struct net_bridge *br,
20362cd41431SLinus Lüssing 					    struct net_bridge_port *port,
2037dc4eb53aSLinus Lüssing 					    struct in6_addr *saddr)
2038dc4eb53aSLinus Lüssing {
2039dc4eb53aSLinus Lüssing 	if (!timer_pending(&br->ip6_own_query.timer) &&
2040dc4eb53aSLinus Lüssing 	    !timer_pending(&br->ip6_other_query.timer))
2041dc4eb53aSLinus Lüssing 		goto update;
2042dc4eb53aSLinus Lüssing 
2043dc4eb53aSLinus Lüssing 	if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0)
2044dc4eb53aSLinus Lüssing 		goto update;
2045dc4eb53aSLinus Lüssing 
2046dc4eb53aSLinus Lüssing 	return false;
2047dc4eb53aSLinus Lüssing 
2048dc4eb53aSLinus Lüssing update:
2049dc4eb53aSLinus Lüssing 	br->ip6_querier.addr.u.ip6 = *saddr;
2050dc4eb53aSLinus Lüssing 
20512cd41431SLinus Lüssing 	/* update protected by general multicast_lock by caller */
20522cd41431SLinus Lüssing 	rcu_assign_pointer(br->ip6_querier.port, port);
20532cd41431SLinus Lüssing 
2054dc4eb53aSLinus Lüssing 	return true;
2055dc4eb53aSLinus Lüssing }
2056dc4eb53aSLinus Lüssing #endif
2057dc4eb53aSLinus Lüssing 
2058dc4eb53aSLinus Lüssing static bool br_multicast_select_querier(struct net_bridge *br,
20592cd41431SLinus Lüssing 					struct net_bridge_port *port,
2060dc4eb53aSLinus Lüssing 					struct br_ip *saddr)
2061dc4eb53aSLinus Lüssing {
2062dc4eb53aSLinus Lüssing 	switch (saddr->proto) {
2063dc4eb53aSLinus Lüssing 	case htons(ETH_P_IP):
20642cd41431SLinus Lüssing 		return br_ip4_multicast_select_querier(br, port, saddr->u.ip4);
2065dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
2066dc4eb53aSLinus Lüssing 	case htons(ETH_P_IPV6):
20672cd41431SLinus Lüssing 		return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6);
2068dc4eb53aSLinus Lüssing #endif
2069dc4eb53aSLinus Lüssing 	}
2070dc4eb53aSLinus Lüssing 
2071dc4eb53aSLinus Lüssing 	return false;
2072dc4eb53aSLinus Lüssing }
2073dc4eb53aSLinus Lüssing 
2074cc0fdd80SLinus Lüssing static void
207590010b36SLinus Lüssing br_multicast_update_query_timer(struct net_bridge *br,
207690010b36SLinus Lüssing 				struct bridge_mcast_other_query *query,
2077b00589afSLinus Lüssing 				unsigned long max_delay)
2078b00589afSLinus Lüssing {
207990010b36SLinus Lüssing 	if (!timer_pending(&query->timer))
208090010b36SLinus Lüssing 		query->delay_time = jiffies + max_delay;
2081b00589afSLinus Lüssing 
208290010b36SLinus Lüssing 	mod_timer(&query->timer, jiffies + br->multicast_querier_interval);
2083b00589afSLinus Lüssing }
2084b00589afSLinus Lüssing 
20856d549648SNogah Frankel static void br_port_mc_router_state_change(struct net_bridge_port *p,
20866d549648SNogah Frankel 					   bool is_mc_router)
20876d549648SNogah Frankel {
20886d549648SNogah Frankel 	struct switchdev_attr attr = {
20896d549648SNogah Frankel 		.orig_dev = p->dev,
20906d549648SNogah Frankel 		.id = SWITCHDEV_ATTR_ID_PORT_MROUTER,
20916d549648SNogah Frankel 		.flags = SWITCHDEV_F_DEFER,
20926d549648SNogah Frankel 		.u.mrouter = is_mc_router,
20936d549648SNogah Frankel 	};
20946d549648SNogah Frankel 
20956d549648SNogah Frankel 	switchdev_port_attr_set(p->dev, &attr);
20966d549648SNogah Frankel }
20976d549648SNogah Frankel 
20987e80c124Sstephen hemminger /*
20997c77602fSCong Wang  * Add port to router_list
21007e80c124Sstephen hemminger  *  list is maintained ordered by pointer value
21017e80c124Sstephen hemminger  *  and locked by br->multicast_lock and RCU
21027e80c124Sstephen hemminger  */
21030909e117SHerbert Xu static void br_multicast_add_router(struct net_bridge *br,
21040909e117SHerbert Xu 				    struct net_bridge_port *port)
21050909e117SHerbert Xu {
2106dcdca2c4Sstephen hemminger 	struct net_bridge_port *p;
2107b67bfe0dSSasha Levin 	struct hlist_node *slot = NULL;
21080909e117SHerbert Xu 
21091a040eacSNikolay Aleksandrov 	if (!hlist_unhashed(&port->rlist))
21101a040eacSNikolay Aleksandrov 		return;
21111a040eacSNikolay Aleksandrov 
2112b67bfe0dSSasha Levin 	hlist_for_each_entry(p, &br->router_list, rlist) {
21137e80c124Sstephen hemminger 		if ((unsigned long) port >= (unsigned long) p)
21147e80c124Sstephen hemminger 			break;
2115b67bfe0dSSasha Levin 		slot = &p->rlist;
2116dcdca2c4Sstephen hemminger 	}
21170909e117SHerbert Xu 
21187e80c124Sstephen hemminger 	if (slot)
21191d023284SKen Helias 		hlist_add_behind_rcu(&port->rlist, slot);
2120dcdca2c4Sstephen hemminger 	else
2121dcdca2c4Sstephen hemminger 		hlist_add_head_rcu(&port->rlist, &br->router_list);
2122949f1e39SSatish Ashok 	br_rtr_notify(br->dev, port, RTM_NEWMDB);
21236d549648SNogah Frankel 	br_port_mc_router_state_change(port, true);
21240909e117SHerbert Xu }
21250909e117SHerbert Xu 
2126eb1d1641SHerbert Xu static void br_multicast_mark_router(struct net_bridge *br,
2127eb1d1641SHerbert Xu 				     struct net_bridge_port *port)
2128eb1d1641SHerbert Xu {
2129eb1d1641SHerbert Xu 	unsigned long now = jiffies;
2130eb1d1641SHerbert Xu 
2131eb1d1641SHerbert Xu 	if (!port) {
213277041420SYotam Gigi 		if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) {
213377041420SYotam Gigi 			if (!timer_pending(&br->multicast_router_timer))
213477041420SYotam Gigi 				br_mc_router_state_change(br, true);
2135eb1d1641SHerbert Xu 			mod_timer(&br->multicast_router_timer,
2136eb1d1641SHerbert Xu 				  now + br->multicast_querier_interval);
213777041420SYotam Gigi 		}
2138eb1d1641SHerbert Xu 		return;
2139eb1d1641SHerbert Xu 	}
2140eb1d1641SHerbert Xu 
2141a55d8246SNikolay Aleksandrov 	if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
2142a55d8246SNikolay Aleksandrov 	    port->multicast_router == MDB_RTR_TYPE_PERM)
2143eb1d1641SHerbert Xu 		return;
2144eb1d1641SHerbert Xu 
21450909e117SHerbert Xu 	br_multicast_add_router(br, port);
2146eb1d1641SHerbert Xu 
2147eb1d1641SHerbert Xu 	mod_timer(&port->multicast_router_timer,
2148eb1d1641SHerbert Xu 		  now + br->multicast_querier_interval);
2149eb1d1641SHerbert Xu }
2150eb1d1641SHerbert Xu 
2151eb1d1641SHerbert Xu static void br_multicast_query_received(struct net_bridge *br,
2152eb1d1641SHerbert Xu 					struct net_bridge_port *port,
215390010b36SLinus Lüssing 					struct bridge_mcast_other_query *query,
2154dc4eb53aSLinus Lüssing 					struct br_ip *saddr,
2155b00589afSLinus Lüssing 					unsigned long max_delay)
2156eb1d1641SHerbert Xu {
21572cd41431SLinus Lüssing 	if (!br_multicast_select_querier(br, port, saddr))
2158eb1d1641SHerbert Xu 		return;
2159eb1d1641SHerbert Xu 
2160dc4eb53aSLinus Lüssing 	br_multicast_update_query_timer(br, query, max_delay);
2161eb1d1641SHerbert Xu 	br_multicast_mark_router(br, port);
2162eb1d1641SHerbert Xu }
2163eb1d1641SHerbert Xu 
21649c2e955cSzhong jiang static void br_ip4_multicast_query(struct net_bridge *br,
2165eb1d1641SHerbert Xu 				   struct net_bridge_port *port,
216606499098SVlad Yasevich 				   struct sk_buff *skb,
216706499098SVlad Yasevich 				   u16 vid)
2168eb1d1641SHerbert Xu {
2169ba5ea614SLinus Lüssing 	unsigned int transport_len = ip_transport_len(skb);
2170b71d1d42SEric Dumazet 	const struct iphdr *iph = ip_hdr(skb);
2171eb1d1641SHerbert Xu 	struct igmphdr *ih = igmp_hdr(skb);
2172eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
2173eb1d1641SHerbert Xu 	struct igmpv3_query *ih3;
2174eb1d1641SHerbert Xu 	struct net_bridge_port_group *p;
2175e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
2176dc4eb53aSLinus Lüssing 	struct br_ip saddr;
2177eb1d1641SHerbert Xu 	unsigned long max_delay;
2178eb1d1641SHerbert Xu 	unsigned long now = jiffies;
2179eb1d1641SHerbert Xu 	__be32 group;
2180eb1d1641SHerbert Xu 
2181eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
2182eb1d1641SHerbert Xu 	if (!netif_running(br->dev) ||
2183eb1d1641SHerbert Xu 	    (port && port->state == BR_STATE_DISABLED))
2184eb1d1641SHerbert Xu 		goto out;
2185eb1d1641SHerbert Xu 
2186eb1d1641SHerbert Xu 	group = ih->group;
2187eb1d1641SHerbert Xu 
2188ba5ea614SLinus Lüssing 	if (transport_len == sizeof(*ih)) {
2189eb1d1641SHerbert Xu 		max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
2190eb1d1641SHerbert Xu 
2191eb1d1641SHerbert Xu 		if (!max_delay) {
2192eb1d1641SHerbert Xu 			max_delay = 10 * HZ;
2193eb1d1641SHerbert Xu 			group = 0;
2194eb1d1641SHerbert Xu 		}
2195ba5ea614SLinus Lüssing 	} else if (transport_len >= sizeof(*ih3)) {
2196eb1d1641SHerbert Xu 		ih3 = igmpv3_query_hdr(skb);
2197eb1d1641SHerbert Xu 		if (ih3->nsrcs)
2198bec68ff1SYOSHIFUJI Hideaki 			goto out;
2199eb1d1641SHerbert Xu 
22000ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 		max_delay = ih3->code ?
22010ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 			    IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
22029afd85c9SLinus Lüssing 	} else {
22039ed973ccSLinus Lüssing 		goto out;
22049ed973ccSLinus Lüssing 	}
22059ed973ccSLinus Lüssing 
2206dc4eb53aSLinus Lüssing 	if (!group) {
2207dc4eb53aSLinus Lüssing 		saddr.proto = htons(ETH_P_IP);
2208dc4eb53aSLinus Lüssing 		saddr.u.ip4 = iph->saddr;
2209b00589afSLinus Lüssing 
2210dc4eb53aSLinus Lüssing 		br_multicast_query_received(br, port, &br->ip4_other_query,
2211dc4eb53aSLinus Lüssing 					    &saddr, max_delay);
2212eb1d1641SHerbert Xu 		goto out;
2213dc4eb53aSLinus Lüssing 	}
2214eb1d1641SHerbert Xu 
221519e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip4_get(br, group, vid);
2216eb1d1641SHerbert Xu 	if (!mp)
2217eb1d1641SHerbert Xu 		goto out;
2218eb1d1641SHerbert Xu 
2219eb1d1641SHerbert Xu 	max_delay *= br->multicast_last_member_count;
2220eb1d1641SHerbert Xu 
2221ff0fd34eSAndrew Lunn 	if (mp->host_joined &&
2222eb1d1641SHerbert Xu 	    (timer_pending(&mp->timer) ?
2223eb1d1641SHerbert Xu 	     time_after(mp->timer.expires, now + max_delay) :
2224eb1d1641SHerbert Xu 	     try_to_del_timer_sync(&mp->timer) >= 0))
2225eb1d1641SHerbert Xu 		mod_timer(&mp->timer, now + max_delay);
2226eb1d1641SHerbert Xu 
2227e8051688SEric Dumazet 	for (pp = &mp->ports;
2228e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
2229e8051688SEric Dumazet 	     pp = &p->next) {
2230eb1d1641SHerbert Xu 		if (timer_pending(&p->timer) ?
2231eb1d1641SHerbert Xu 		    time_after(p->timer.expires, now + max_delay) :
2232eb1d1641SHerbert Xu 		    try_to_del_timer_sync(&p->timer) >= 0)
223324f9cdcbSHerbert Xu 			mod_timer(&p->timer, now + max_delay);
2234eb1d1641SHerbert Xu 	}
2235eb1d1641SHerbert Xu 
2236eb1d1641SHerbert Xu out:
2237eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
2238eb1d1641SHerbert Xu }
2239eb1d1641SHerbert Xu 
2240dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
224108b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_query(struct net_bridge *br,
224208b202b6SYOSHIFUJI Hideaki 				  struct net_bridge_port *port,
224306499098SVlad Yasevich 				  struct sk_buff *skb,
224406499098SVlad Yasevich 				  u16 vid)
224508b202b6SYOSHIFUJI Hideaki {
2246ba5ea614SLinus Lüssing 	unsigned int transport_len = ipv6_transport_len(skb);
2247eca2a43bSstephen hemminger 	struct mld_msg *mld;
224808b202b6SYOSHIFUJI Hideaki 	struct net_bridge_mdb_entry *mp;
224908b202b6SYOSHIFUJI Hideaki 	struct mld2_query *mld2q;
2250e8051688SEric Dumazet 	struct net_bridge_port_group *p;
2251e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
2252dc4eb53aSLinus Lüssing 	struct br_ip saddr;
225308b202b6SYOSHIFUJI Hideaki 	unsigned long max_delay;
225408b202b6SYOSHIFUJI Hideaki 	unsigned long now = jiffies;
2255856ce5d0SLinus Lüssing 	unsigned int offset = skb_transport_offset(skb);
2256b71d1d42SEric Dumazet 	const struct in6_addr *group = NULL;
22579ed973ccSLinus Lüssing 	bool is_general_query;
225808b202b6SYOSHIFUJI Hideaki 	int err = 0;
225908b202b6SYOSHIFUJI Hideaki 
226008b202b6SYOSHIFUJI Hideaki 	spin_lock(&br->multicast_lock);
226108b202b6SYOSHIFUJI Hideaki 	if (!netif_running(br->dev) ||
226208b202b6SYOSHIFUJI Hideaki 	    (port && port->state == BR_STATE_DISABLED))
226308b202b6SYOSHIFUJI Hideaki 		goto out;
226408b202b6SYOSHIFUJI Hideaki 
2265ba5ea614SLinus Lüssing 	if (transport_len == sizeof(*mld)) {
2266856ce5d0SLinus Lüssing 		if (!pskb_may_pull(skb, offset + sizeof(*mld))) {
226708b202b6SYOSHIFUJI Hideaki 			err = -EINVAL;
226808b202b6SYOSHIFUJI Hideaki 			goto out;
226908b202b6SYOSHIFUJI Hideaki 		}
227008b202b6SYOSHIFUJI Hideaki 		mld = (struct mld_msg *) icmp6_hdr(skb);
22714715213dSLi RongQing 		max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
227208b202b6SYOSHIFUJI Hideaki 		if (max_delay)
227308b202b6SYOSHIFUJI Hideaki 			group = &mld->mld_mca;
2274248ba8ecSLinus Lüssing 	} else {
2275856ce5d0SLinus Lüssing 		if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) {
227608b202b6SYOSHIFUJI Hideaki 			err = -EINVAL;
227708b202b6SYOSHIFUJI Hideaki 			goto out;
227808b202b6SYOSHIFUJI Hideaki 		}
227908b202b6SYOSHIFUJI Hideaki 		mld2q = (struct mld2_query *)icmp6_hdr(skb);
228008b202b6SYOSHIFUJI Hideaki 		if (!mld2q->mld2q_nsrcs)
228108b202b6SYOSHIFUJI Hideaki 			group = &mld2q->mld2q_mca;
2282e3f5b170SDaniel Borkmann 
2283e3f5b170SDaniel Borkmann 		max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
228408b202b6SYOSHIFUJI Hideaki 	}
228508b202b6SYOSHIFUJI Hideaki 
22869ed973ccSLinus Lüssing 	is_general_query = group && ipv6_addr_any(group);
22879ed973ccSLinus Lüssing 
2288dc4eb53aSLinus Lüssing 	if (is_general_query) {
2289dc4eb53aSLinus Lüssing 		saddr.proto = htons(ETH_P_IPV6);
22903b26a5d0SNikolay Aleksandrov 		saddr.u.ip6 = ipv6_hdr(skb)->saddr;
2291b00589afSLinus Lüssing 
2292dc4eb53aSLinus Lüssing 		br_multicast_query_received(br, port, &br->ip6_other_query,
2293dc4eb53aSLinus Lüssing 					    &saddr, max_delay);
229408b202b6SYOSHIFUJI Hideaki 		goto out;
22956c03ee8bSLinus Lüssing 	} else if (!group) {
22966c03ee8bSLinus Lüssing 		goto out;
2297dc4eb53aSLinus Lüssing 	}
229808b202b6SYOSHIFUJI Hideaki 
229919e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip6_get(br, group, vid);
230008b202b6SYOSHIFUJI Hideaki 	if (!mp)
230108b202b6SYOSHIFUJI Hideaki 		goto out;
230208b202b6SYOSHIFUJI Hideaki 
230308b202b6SYOSHIFUJI Hideaki 	max_delay *= br->multicast_last_member_count;
2304ff0fd34eSAndrew Lunn 	if (mp->host_joined &&
230508b202b6SYOSHIFUJI Hideaki 	    (timer_pending(&mp->timer) ?
230608b202b6SYOSHIFUJI Hideaki 	     time_after(mp->timer.expires, now + max_delay) :
230708b202b6SYOSHIFUJI Hideaki 	     try_to_del_timer_sync(&mp->timer) >= 0))
230808b202b6SYOSHIFUJI Hideaki 		mod_timer(&mp->timer, now + max_delay);
230908b202b6SYOSHIFUJI Hideaki 
2310e8051688SEric Dumazet 	for (pp = &mp->ports;
2311e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
2312e8051688SEric Dumazet 	     pp = &p->next) {
231308b202b6SYOSHIFUJI Hideaki 		if (timer_pending(&p->timer) ?
231408b202b6SYOSHIFUJI Hideaki 		    time_after(p->timer.expires, now + max_delay) :
231508b202b6SYOSHIFUJI Hideaki 		    try_to_del_timer_sync(&p->timer) >= 0)
231624f9cdcbSHerbert Xu 			mod_timer(&p->timer, now + max_delay);
231708b202b6SYOSHIFUJI Hideaki 	}
231808b202b6SYOSHIFUJI Hideaki 
231908b202b6SYOSHIFUJI Hideaki out:
232008b202b6SYOSHIFUJI Hideaki 	spin_unlock(&br->multicast_lock);
232108b202b6SYOSHIFUJI Hideaki 	return err;
232208b202b6SYOSHIFUJI Hideaki }
232308b202b6SYOSHIFUJI Hideaki #endif
232408b202b6SYOSHIFUJI Hideaki 
232590010b36SLinus Lüssing static void
232690010b36SLinus Lüssing br_multicast_leave_group(struct net_bridge *br,
2327eb1d1641SHerbert Xu 			 struct net_bridge_port *port,
2328cc0fdd80SLinus Lüssing 			 struct br_ip *group,
232990010b36SLinus Lüssing 			 struct bridge_mcast_other_query *other_query,
23306db6f0eaSFelix Fietkau 			 struct bridge_mcast_own_query *own_query,
23316db6f0eaSFelix Fietkau 			 const unsigned char *src)
2332eb1d1641SHerbert Xu {
2333eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
2334eb1d1641SHerbert Xu 	struct net_bridge_port_group *p;
2335eb1d1641SHerbert Xu 	unsigned long now;
2336eb1d1641SHerbert Xu 	unsigned long time;
2337eb1d1641SHerbert Xu 
2338eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
2339eb1d1641SHerbert Xu 	if (!netif_running(br->dev) ||
2340544586f7SSatish Ashok 	    (port && port->state == BR_STATE_DISABLED))
2341eb1d1641SHerbert Xu 		goto out;
2342eb1d1641SHerbert Xu 
234319e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip_get(br, group);
2344eb1d1641SHerbert Xu 	if (!mp)
2345eb1d1641SHerbert Xu 		goto out;
2346eb1d1641SHerbert Xu 
2347544586f7SSatish Ashok 	if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
2348544586f7SSatish Ashok 		struct net_bridge_port_group __rcu **pp;
2349544586f7SSatish Ashok 
2350544586f7SSatish Ashok 		for (pp = &mp->ports;
2351544586f7SSatish Ashok 		     (p = mlock_dereference(*pp, br)) != NULL;
2352544586f7SSatish Ashok 		     pp = &p->next) {
23536db6f0eaSFelix Fietkau 			if (!br_port_group_equal(p, port, src))
2354544586f7SSatish Ashok 				continue;
2355544586f7SSatish Ashok 
23565c725b6bSNikolay Aleksandrov 			if (p->flags & MDB_PG_FLAGS_PERMANENT)
23575c725b6bSNikolay Aleksandrov 				break;
23585c725b6bSNikolay Aleksandrov 
2359681590bdSNikolay Aleksandrov 			p->flags |= MDB_PG_FLAGS_FAST_LEAVE;
2360681590bdSNikolay Aleksandrov 			br_multicast_del_pg(mp, p, pp);
2361544586f7SSatish Ashok 		}
2362544586f7SSatish Ashok 		goto out;
2363544586f7SSatish Ashok 	}
2364544586f7SSatish Ashok 
2365544586f7SSatish Ashok 	if (timer_pending(&other_query->timer))
2366544586f7SSatish Ashok 		goto out;
2367544586f7SSatish Ashok 
2368675779adSNikolay Aleksandrov 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER)) {
2369438ef2d0SNikolay Aleksandrov 		__br_multicast_send_query(br, port, NULL, NULL, &mp->addr,
237042c11ccfSNikolay Aleksandrov 					  false, 0, NULL);
23716b7df111SCong Wang 
23726b7df111SCong Wang 		time = jiffies + br->multicast_last_member_count *
23736b7df111SCong Wang 				 br->multicast_last_member_interval;
2374cc0fdd80SLinus Lüssing 
237590010b36SLinus Lüssing 		mod_timer(&own_query->timer, time);
23766b7df111SCong Wang 
23776b7df111SCong Wang 		for (p = mlock_dereference(mp->ports, br);
23786b7df111SCong Wang 		     p != NULL;
23796b7df111SCong Wang 		     p = mlock_dereference(p->next, br)) {
23806db6f0eaSFelix Fietkau 			if (!br_port_group_equal(p, port, src))
23816b7df111SCong Wang 				continue;
23826b7df111SCong Wang 
23836b7df111SCong Wang 			if (!hlist_unhashed(&p->mglist) &&
23846b7df111SCong Wang 			    (timer_pending(&p->timer) ?
23856b7df111SCong Wang 			     time_after(p->timer.expires, time) :
23866b7df111SCong Wang 			     try_to_del_timer_sync(&p->timer) >= 0)) {
23876b7df111SCong Wang 				mod_timer(&p->timer, time);
23886b7df111SCong Wang 			}
23896b7df111SCong Wang 
23906b7df111SCong Wang 			break;
23916b7df111SCong Wang 		}
23926b7df111SCong Wang 	}
23936b7df111SCong Wang 
2394eb1d1641SHerbert Xu 	now = jiffies;
2395eb1d1641SHerbert Xu 	time = now + br->multicast_last_member_count *
2396eb1d1641SHerbert Xu 		     br->multicast_last_member_interval;
2397eb1d1641SHerbert Xu 
2398eb1d1641SHerbert Xu 	if (!port) {
2399ff0fd34eSAndrew Lunn 		if (mp->host_joined &&
2400eb1d1641SHerbert Xu 		    (timer_pending(&mp->timer) ?
2401eb1d1641SHerbert Xu 		     time_after(mp->timer.expires, time) :
2402eb1d1641SHerbert Xu 		     try_to_del_timer_sync(&mp->timer) >= 0)) {
2403eb1d1641SHerbert Xu 			mod_timer(&mp->timer, time);
2404eb1d1641SHerbert Xu 		}
2405454594f3SLinus Lüssing 
2406454594f3SLinus Lüssing 		goto out;
2407454594f3SLinus Lüssing 	}
2408454594f3SLinus Lüssing 
2409454594f3SLinus Lüssing 	for (p = mlock_dereference(mp->ports, br);
2410454594f3SLinus Lüssing 	     p != NULL;
2411454594f3SLinus Lüssing 	     p = mlock_dereference(p->next, br)) {
2412454594f3SLinus Lüssing 		if (p->port != port)
2413454594f3SLinus Lüssing 			continue;
2414454594f3SLinus Lüssing 
2415454594f3SLinus Lüssing 		if (!hlist_unhashed(&p->mglist) &&
2416454594f3SLinus Lüssing 		    (timer_pending(&p->timer) ?
2417454594f3SLinus Lüssing 		     time_after(p->timer.expires, time) :
2418454594f3SLinus Lüssing 		     try_to_del_timer_sync(&p->timer) >= 0)) {
2419454594f3SLinus Lüssing 			mod_timer(&p->timer, time);
2420454594f3SLinus Lüssing 		}
2421454594f3SLinus Lüssing 
2422454594f3SLinus Lüssing 		break;
2423eb1d1641SHerbert Xu 	}
2424eb1d1641SHerbert Xu out:
2425eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
2426eb1d1641SHerbert Xu }
2427eb1d1641SHerbert Xu 
24288ef2a9a5SYOSHIFUJI Hideaki static void br_ip4_multicast_leave_group(struct net_bridge *br,
24298ef2a9a5SYOSHIFUJI Hideaki 					 struct net_bridge_port *port,
2430b0e9a30dSVlad Yasevich 					 __be32 group,
24316db6f0eaSFelix Fietkau 					 __u16 vid,
24326db6f0eaSFelix Fietkau 					 const unsigned char *src)
24338ef2a9a5SYOSHIFUJI Hideaki {
24348ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip br_group;
243590010b36SLinus Lüssing 	struct bridge_mcast_own_query *own_query;
24368ef2a9a5SYOSHIFUJI Hideaki 
24378ef2a9a5SYOSHIFUJI Hideaki 	if (ipv4_is_local_multicast(group))
24388ef2a9a5SYOSHIFUJI Hideaki 		return;
24398ef2a9a5SYOSHIFUJI Hideaki 
244090010b36SLinus Lüssing 	own_query = port ? &port->ip4_own_query : &br->ip4_own_query;
244190010b36SLinus Lüssing 
24421515a63fSNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
24438ef2a9a5SYOSHIFUJI Hideaki 	br_group.u.ip4 = group;
24448ef2a9a5SYOSHIFUJI Hideaki 	br_group.proto = htons(ETH_P_IP);
2445b0e9a30dSVlad Yasevich 	br_group.vid = vid;
24468ef2a9a5SYOSHIFUJI Hideaki 
244790010b36SLinus Lüssing 	br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
24486db6f0eaSFelix Fietkau 				 own_query, src);
24498ef2a9a5SYOSHIFUJI Hideaki }
24508ef2a9a5SYOSHIFUJI Hideaki 
2451dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
245208b202b6SYOSHIFUJI Hideaki static void br_ip6_multicast_leave_group(struct net_bridge *br,
245308b202b6SYOSHIFUJI Hideaki 					 struct net_bridge_port *port,
2454b0e9a30dSVlad Yasevich 					 const struct in6_addr *group,
24556db6f0eaSFelix Fietkau 					 __u16 vid,
24566db6f0eaSFelix Fietkau 					 const unsigned char *src)
245708b202b6SYOSHIFUJI Hideaki {
245808b202b6SYOSHIFUJI Hideaki 	struct br_ip br_group;
245990010b36SLinus Lüssing 	struct bridge_mcast_own_query *own_query;
246008b202b6SYOSHIFUJI Hideaki 
24613c3769e6SLinus Lüssing 	if (ipv6_addr_is_ll_all_nodes(group))
246208b202b6SYOSHIFUJI Hideaki 		return;
246308b202b6SYOSHIFUJI Hideaki 
246490010b36SLinus Lüssing 	own_query = port ? &port->ip6_own_query : &br->ip6_own_query;
246590010b36SLinus Lüssing 
24661515a63fSNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
24674e3fd7a0SAlexey Dobriyan 	br_group.u.ip6 = *group;
246808b202b6SYOSHIFUJI Hideaki 	br_group.proto = htons(ETH_P_IPV6);
2469b0e9a30dSVlad Yasevich 	br_group.vid = vid;
247008b202b6SYOSHIFUJI Hideaki 
247190010b36SLinus Lüssing 	br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
24726db6f0eaSFelix Fietkau 				 own_query, src);
247308b202b6SYOSHIFUJI Hideaki }
247408b202b6SYOSHIFUJI Hideaki #endif
24758ef2a9a5SYOSHIFUJI Hideaki 
24761080ab95SNikolay Aleksandrov static void br_multicast_err_count(const struct net_bridge *br,
24771080ab95SNikolay Aleksandrov 				   const struct net_bridge_port *p,
24781080ab95SNikolay Aleksandrov 				   __be16 proto)
24791080ab95SNikolay Aleksandrov {
24801080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
24811080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats *pstats;
24821080ab95SNikolay Aleksandrov 
2483675779adSNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
24841080ab95SNikolay Aleksandrov 		return;
24851080ab95SNikolay Aleksandrov 
24861080ab95SNikolay Aleksandrov 	if (p)
24871080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
24881080ab95SNikolay Aleksandrov 	else
24891080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
24901080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
24911080ab95SNikolay Aleksandrov 		return;
24921080ab95SNikolay Aleksandrov 
24931080ab95SNikolay Aleksandrov 	pstats = this_cpu_ptr(stats);
24941080ab95SNikolay Aleksandrov 
24951080ab95SNikolay Aleksandrov 	u64_stats_update_begin(&pstats->syncp);
24961080ab95SNikolay Aleksandrov 	switch (proto) {
24971080ab95SNikolay Aleksandrov 	case htons(ETH_P_IP):
24981080ab95SNikolay Aleksandrov 		pstats->mstats.igmp_parse_errors++;
24991080ab95SNikolay Aleksandrov 		break;
25001080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
25011080ab95SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
25021080ab95SNikolay Aleksandrov 		pstats->mstats.mld_parse_errors++;
25031080ab95SNikolay Aleksandrov 		break;
25041080ab95SNikolay Aleksandrov #endif
25051080ab95SNikolay Aleksandrov 	}
25061080ab95SNikolay Aleksandrov 	u64_stats_update_end(&pstats->syncp);
25071080ab95SNikolay Aleksandrov }
25081080ab95SNikolay Aleksandrov 
250991b02d3dSNikolay Aleksandrov static void br_multicast_pim(struct net_bridge *br,
251091b02d3dSNikolay Aleksandrov 			     struct net_bridge_port *port,
251191b02d3dSNikolay Aleksandrov 			     const struct sk_buff *skb)
251291b02d3dSNikolay Aleksandrov {
251391b02d3dSNikolay Aleksandrov 	unsigned int offset = skb_transport_offset(skb);
251491b02d3dSNikolay Aleksandrov 	struct pimhdr *pimhdr, _pimhdr;
251591b02d3dSNikolay Aleksandrov 
251691b02d3dSNikolay Aleksandrov 	pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr);
251791b02d3dSNikolay Aleksandrov 	if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION ||
251891b02d3dSNikolay Aleksandrov 	    pim_hdr_type(pimhdr) != PIM_TYPE_HELLO)
251991b02d3dSNikolay Aleksandrov 		return;
252091b02d3dSNikolay Aleksandrov 
252191b02d3dSNikolay Aleksandrov 	br_multicast_mark_router(br, port);
252291b02d3dSNikolay Aleksandrov }
252391b02d3dSNikolay Aleksandrov 
25244b3087c7SLinus Lüssing static int br_ip4_multicast_mrd_rcv(struct net_bridge *br,
25254b3087c7SLinus Lüssing 				    struct net_bridge_port *port,
25264b3087c7SLinus Lüssing 				    struct sk_buff *skb)
25274b3087c7SLinus Lüssing {
25284b3087c7SLinus Lüssing 	if (ip_hdr(skb)->protocol != IPPROTO_IGMP ||
25294b3087c7SLinus Lüssing 	    igmp_hdr(skb)->type != IGMP_MRDISC_ADV)
25304b3087c7SLinus Lüssing 		return -ENOMSG;
25314b3087c7SLinus Lüssing 
25324b3087c7SLinus Lüssing 	br_multicast_mark_router(br, port);
25334b3087c7SLinus Lüssing 
25344b3087c7SLinus Lüssing 	return 0;
25354b3087c7SLinus Lüssing }
25364b3087c7SLinus Lüssing 
2537eb1d1641SHerbert Xu static int br_multicast_ipv4_rcv(struct net_bridge *br,
2538eb1d1641SHerbert Xu 				 struct net_bridge_port *port,
253906499098SVlad Yasevich 				 struct sk_buff *skb,
254006499098SVlad Yasevich 				 u16 vid)
2541eb1d1641SHerbert Xu {
25426db6f0eaSFelix Fietkau 	const unsigned char *src;
2543eb1d1641SHerbert Xu 	struct igmphdr *ih;
2544eb1d1641SHerbert Xu 	int err;
2545eb1d1641SHerbert Xu 
2546ba5ea614SLinus Lüssing 	err = ip_mc_check_igmp(skb);
2547eb1d1641SHerbert Xu 
25489afd85c9SLinus Lüssing 	if (err == -ENOMSG) {
254991b02d3dSNikolay Aleksandrov 		if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) {
2550bd4265feSHerbert Xu 			BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
255191b02d3dSNikolay Aleksandrov 		} else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) {
255291b02d3dSNikolay Aleksandrov 			if (ip_hdr(skb)->protocol == IPPROTO_PIM)
255391b02d3dSNikolay Aleksandrov 				br_multicast_pim(br, port, skb);
25544b3087c7SLinus Lüssing 		} else if (ipv4_is_all_snoopers(ip_hdr(skb)->daddr)) {
255508e71623SLi RongQing 			br_ip4_multicast_mrd_rcv(br, port, skb);
25564b3087c7SLinus Lüssing 		}
25574b3087c7SLinus Lüssing 
2558eb1d1641SHerbert Xu 		return 0;
25599afd85c9SLinus Lüssing 	} else if (err < 0) {
25601080ab95SNikolay Aleksandrov 		br_multicast_err_count(br, port, skb->protocol);
25619afd85c9SLinus Lüssing 		return err;
2562bd4265feSHerbert Xu 	}
2563eb1d1641SHerbert Xu 
25649afd85c9SLinus Lüssing 	ih = igmp_hdr(skb);
25656db6f0eaSFelix Fietkau 	src = eth_hdr(skb)->h_source;
25661080ab95SNikolay Aleksandrov 	BR_INPUT_SKB_CB(skb)->igmp = ih->type;
2567eb1d1641SHerbert Xu 
2568eb1d1641SHerbert Xu 	switch (ih->type) {
2569eb1d1641SHerbert Xu 	case IGMP_HOST_MEMBERSHIP_REPORT:
2570eb1d1641SHerbert Xu 	case IGMPV2_HOST_MEMBERSHIP_REPORT:
257162b2bcb4SFernando Luis Vázquez Cao 		BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
25728b671779SNikolay Aleksandrov 		err = br_ip4_multicast_add_group(br, port, ih->group, vid, src,
25738b671779SNikolay Aleksandrov 						 true);
2574eb1d1641SHerbert Xu 		break;
2575eb1d1641SHerbert Xu 	case IGMPV3_HOST_MEMBERSHIP_REPORT:
2576ba5ea614SLinus Lüssing 		err = br_ip4_multicast_igmp3_report(br, port, skb, vid);
2577eb1d1641SHerbert Xu 		break;
2578eb1d1641SHerbert Xu 	case IGMP_HOST_MEMBERSHIP_QUERY:
2579ba5ea614SLinus Lüssing 		br_ip4_multicast_query(br, port, skb, vid);
2580eb1d1641SHerbert Xu 		break;
2581eb1d1641SHerbert Xu 	case IGMP_HOST_LEAVE_MESSAGE:
25826db6f0eaSFelix Fietkau 		br_ip4_multicast_leave_group(br, port, ih->group, vid, src);
2583eb1d1641SHerbert Xu 		break;
2584eb1d1641SHerbert Xu 	}
2585eb1d1641SHerbert Xu 
2586a65056ecSNikolay Aleksandrov 	br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
25871080ab95SNikolay Aleksandrov 			   BR_MCAST_DIR_RX);
25881080ab95SNikolay Aleksandrov 
2589eb1d1641SHerbert Xu 	return err;
2590eb1d1641SHerbert Xu }
2591eb1d1641SHerbert Xu 
2592dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
25934b3087c7SLinus Lüssing static int br_ip6_multicast_mrd_rcv(struct net_bridge *br,
25944b3087c7SLinus Lüssing 				    struct net_bridge_port *port,
25954b3087c7SLinus Lüssing 				    struct sk_buff *skb)
25964b3087c7SLinus Lüssing {
25974b3087c7SLinus Lüssing 	int ret;
25984b3087c7SLinus Lüssing 
25994b3087c7SLinus Lüssing 	if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6)
26004b3087c7SLinus Lüssing 		return -ENOMSG;
26014b3087c7SLinus Lüssing 
26024b3087c7SLinus Lüssing 	ret = ipv6_mc_check_icmpv6(skb);
26034b3087c7SLinus Lüssing 	if (ret < 0)
26044b3087c7SLinus Lüssing 		return ret;
26054b3087c7SLinus Lüssing 
26064b3087c7SLinus Lüssing 	if (icmp6_hdr(skb)->icmp6_type != ICMPV6_MRDISC_ADV)
26074b3087c7SLinus Lüssing 		return -ENOMSG;
26084b3087c7SLinus Lüssing 
26094b3087c7SLinus Lüssing 	br_multicast_mark_router(br, port);
26104b3087c7SLinus Lüssing 
26114b3087c7SLinus Lüssing 	return 0;
26124b3087c7SLinus Lüssing }
26134b3087c7SLinus Lüssing 
261408b202b6SYOSHIFUJI Hideaki static int br_multicast_ipv6_rcv(struct net_bridge *br,
261508b202b6SYOSHIFUJI Hideaki 				 struct net_bridge_port *port,
261606499098SVlad Yasevich 				 struct sk_buff *skb,
261706499098SVlad Yasevich 				 u16 vid)
261808b202b6SYOSHIFUJI Hideaki {
26196db6f0eaSFelix Fietkau 	const unsigned char *src;
26209afd85c9SLinus Lüssing 	struct mld_msg *mld;
262108b202b6SYOSHIFUJI Hideaki 	int err;
262208b202b6SYOSHIFUJI Hideaki 
2623ba5ea614SLinus Lüssing 	err = ipv6_mc_check_mld(skb);
262408b202b6SYOSHIFUJI Hideaki 
26259afd85c9SLinus Lüssing 	if (err == -ENOMSG) {
26269afd85c9SLinus Lüssing 		if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
26278fad9c39SLinus Lüssing 			BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
26284b3087c7SLinus Lüssing 
26294b3087c7SLinus Lüssing 		if (ipv6_addr_is_all_snoopers(&ipv6_hdr(skb)->daddr)) {
26304b3087c7SLinus Lüssing 			err = br_ip6_multicast_mrd_rcv(br, port, skb);
26314b3087c7SLinus Lüssing 
26324b3087c7SLinus Lüssing 			if (err < 0 && err != -ENOMSG) {
26334b3087c7SLinus Lüssing 				br_multicast_err_count(br, port, skb->protocol);
26344b3087c7SLinus Lüssing 				return err;
26354b3087c7SLinus Lüssing 			}
26364b3087c7SLinus Lüssing 		}
26374b3087c7SLinus Lüssing 
263808b202b6SYOSHIFUJI Hideaki 		return 0;
26399afd85c9SLinus Lüssing 	} else if (err < 0) {
26401080ab95SNikolay Aleksandrov 		br_multicast_err_count(br, port, skb->protocol);
26419afd85c9SLinus Lüssing 		return err;
264208b202b6SYOSHIFUJI Hideaki 	}
264308b202b6SYOSHIFUJI Hideaki 
26449afd85c9SLinus Lüssing 	mld = (struct mld_msg *)skb_transport_header(skb);
26451080ab95SNikolay Aleksandrov 	BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type;
264608b202b6SYOSHIFUJI Hideaki 
26479afd85c9SLinus Lüssing 	switch (mld->mld_type) {
264808b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_REPORT:
26496db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
2650fc2af6c7SFernando Luis Vázquez Cao 		BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
26516db6f0eaSFelix Fietkau 		err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid,
26528b671779SNikolay Aleksandrov 						 src, true);
265308b202b6SYOSHIFUJI Hideaki 		break;
265408b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MLD2_REPORT:
2655ba5ea614SLinus Lüssing 		err = br_ip6_multicast_mld2_report(br, port, skb, vid);
265608b202b6SYOSHIFUJI Hideaki 		break;
265708b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_QUERY:
2658ba5ea614SLinus Lüssing 		err = br_ip6_multicast_query(br, port, skb, vid);
265908b202b6SYOSHIFUJI Hideaki 		break;
266008b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_REDUCTION:
26616db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
26626db6f0eaSFelix Fietkau 		br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src);
26639afd85c9SLinus Lüssing 		break;
266408b202b6SYOSHIFUJI Hideaki 	}
266508b202b6SYOSHIFUJI Hideaki 
2666a65056ecSNikolay Aleksandrov 	br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
26671080ab95SNikolay Aleksandrov 			   BR_MCAST_DIR_RX);
26681080ab95SNikolay Aleksandrov 
266908b202b6SYOSHIFUJI Hideaki 	return err;
267008b202b6SYOSHIFUJI Hideaki }
267108b202b6SYOSHIFUJI Hideaki #endif
267208b202b6SYOSHIFUJI Hideaki 
2673eb1d1641SHerbert Xu int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
267406499098SVlad Yasevich 		     struct sk_buff *skb, u16 vid)
2675eb1d1641SHerbert Xu {
26761080ab95SNikolay Aleksandrov 	int ret = 0;
26771080ab95SNikolay Aleksandrov 
26781fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 	BR_INPUT_SKB_CB(skb)->igmp = 0;
26791fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 	BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
26801fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 
268113cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
2682eb1d1641SHerbert Xu 		return 0;
2683eb1d1641SHerbert Xu 
2684eb1d1641SHerbert Xu 	switch (skb->protocol) {
2685eb1d1641SHerbert Xu 	case htons(ETH_P_IP):
26861080ab95SNikolay Aleksandrov 		ret = br_multicast_ipv4_rcv(br, port, skb, vid);
26871080ab95SNikolay Aleksandrov 		break;
2688dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
268908b202b6SYOSHIFUJI Hideaki 	case htons(ETH_P_IPV6):
26901080ab95SNikolay Aleksandrov 		ret = br_multicast_ipv6_rcv(br, port, skb, vid);
26911080ab95SNikolay Aleksandrov 		break;
269208b202b6SYOSHIFUJI Hideaki #endif
2693eb1d1641SHerbert Xu 	}
2694eb1d1641SHerbert Xu 
26951080ab95SNikolay Aleksandrov 	return ret;
2696eb1d1641SHerbert Xu }
2697eb1d1641SHerbert Xu 
2698cc0fdd80SLinus Lüssing static void br_multicast_query_expired(struct net_bridge *br,
26992cd41431SLinus Lüssing 				       struct bridge_mcast_own_query *query,
27002cd41431SLinus Lüssing 				       struct bridge_mcast_querier *querier)
2701cc0fdd80SLinus Lüssing {
2702cc0fdd80SLinus Lüssing 	spin_lock(&br->multicast_lock);
2703cc0fdd80SLinus Lüssing 	if (query->startup_sent < br->multicast_startup_query_count)
2704cc0fdd80SLinus Lüssing 		query->startup_sent++;
2705cc0fdd80SLinus Lüssing 
270671d9f614SEric Dumazet 	RCU_INIT_POINTER(querier->port, NULL);
2707cc0fdd80SLinus Lüssing 	br_multicast_send_query(br, NULL, query);
2708cc0fdd80SLinus Lüssing 	spin_unlock(&br->multicast_lock);
2709cc0fdd80SLinus Lüssing }
2710cc0fdd80SLinus Lüssing 
271188c1f37fSAllen Pais static void br_ip4_multicast_query_expired(struct timer_list *t)
2712eb1d1641SHerbert Xu {
271388c1f37fSAllen Pais 	struct net_bridge *br = from_timer(br, t, ip4_own_query.timer);
2714eb1d1641SHerbert Xu 
27152cd41431SLinus Lüssing 	br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier);
2716eb1d1641SHerbert Xu }
2717eb1d1641SHerbert Xu 
2718cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
271988c1f37fSAllen Pais static void br_ip6_multicast_query_expired(struct timer_list *t)
2720cc0fdd80SLinus Lüssing {
272188c1f37fSAllen Pais 	struct net_bridge *br = from_timer(br, t, ip6_own_query.timer);
2722cc0fdd80SLinus Lüssing 
27232cd41431SLinus Lüssing 	br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier);
2724cc0fdd80SLinus Lüssing }
2725cc0fdd80SLinus Lüssing #endif
2726cc0fdd80SLinus Lüssing 
27278b671779SNikolay Aleksandrov static void __grp_src_gc(struct hlist_head *head)
27288b671779SNikolay Aleksandrov {
27298b671779SNikolay Aleksandrov 	struct net_bridge_group_src *ent;
27308b671779SNikolay Aleksandrov 	struct hlist_node *tmp;
27318b671779SNikolay Aleksandrov 
27328b671779SNikolay Aleksandrov 	hlist_for_each_entry_safe(ent, tmp, head, del_node) {
27338b671779SNikolay Aleksandrov 		hlist_del_init(&ent->del_node);
27348b671779SNikolay Aleksandrov 		del_timer_sync(&ent->timer);
27358b671779SNikolay Aleksandrov 		kfree_rcu(ent, rcu);
27368b671779SNikolay Aleksandrov 	}
27378b671779SNikolay Aleksandrov }
27388b671779SNikolay Aleksandrov 
27398b671779SNikolay Aleksandrov static void br_multicast_src_gc(struct work_struct *work)
27408b671779SNikolay Aleksandrov {
27418b671779SNikolay Aleksandrov 	struct net_bridge *br = container_of(work, struct net_bridge,
27428b671779SNikolay Aleksandrov 					     src_gc_work);
27438b671779SNikolay Aleksandrov 	HLIST_HEAD(deleted_head);
27448b671779SNikolay Aleksandrov 
27458b671779SNikolay Aleksandrov 	spin_lock_bh(&br->multicast_lock);
27468b671779SNikolay Aleksandrov 	hlist_move_list(&br->src_gc_list, &deleted_head);
27478b671779SNikolay Aleksandrov 	spin_unlock_bh(&br->multicast_lock);
27488b671779SNikolay Aleksandrov 
27498b671779SNikolay Aleksandrov 	__grp_src_gc(&deleted_head);
27508b671779SNikolay Aleksandrov }
27518b671779SNikolay Aleksandrov 
2752eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br)
2753eb1d1641SHerbert Xu {
2754d08c6bc0SNikolay Aleksandrov 	br->hash_max = BR_MULTICAST_DEFAULT_HASH_MAX;
2755eb1d1641SHerbert Xu 
27567f0aec7aSNikolay Aleksandrov 	br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
2757eb1d1641SHerbert Xu 	br->multicast_last_member_count = 2;
2758eb1d1641SHerbert Xu 	br->multicast_startup_query_count = 2;
2759eb1d1641SHerbert Xu 
2760eb1d1641SHerbert Xu 	br->multicast_last_member_interval = HZ;
2761eb1d1641SHerbert Xu 	br->multicast_query_response_interval = 10 * HZ;
2762eb1d1641SHerbert Xu 	br->multicast_startup_query_interval = 125 * HZ / 4;
2763eb1d1641SHerbert Xu 	br->multicast_query_interval = 125 * HZ;
2764eb1d1641SHerbert Xu 	br->multicast_querier_interval = 255 * HZ;
2765eb1d1641SHerbert Xu 	br->multicast_membership_interval = 260 * HZ;
2766eb1d1641SHerbert Xu 
276790010b36SLinus Lüssing 	br->ip4_other_query.delay_time = 0;
27682cd41431SLinus Lüssing 	br->ip4_querier.port = NULL;
2769aa2ae3e7SNikolay Aleksandrov 	br->multicast_igmp_version = 2;
2770cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
2771aa2ae3e7SNikolay Aleksandrov 	br->multicast_mld_version = 1;
277290010b36SLinus Lüssing 	br->ip6_other_query.delay_time = 0;
27732cd41431SLinus Lüssing 	br->ip6_querier.port = NULL;
2774cc0fdd80SLinus Lüssing #endif
27756919622aSIdo Schimmel 	br_opt_toggle(br, BROPT_MULTICAST_ENABLED, true);
2776675779adSNikolay Aleksandrov 	br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true);
2777b00589afSLinus Lüssing 
2778eb1d1641SHerbert Xu 	spin_lock_init(&br->multicast_lock);
277988c1f37fSAllen Pais 	timer_setup(&br->multicast_router_timer,
278088c1f37fSAllen Pais 		    br_multicast_local_router_expired, 0);
278188c1f37fSAllen Pais 	timer_setup(&br->ip4_other_query.timer,
278288c1f37fSAllen Pais 		    br_ip4_multicast_querier_expired, 0);
278388c1f37fSAllen Pais 	timer_setup(&br->ip4_own_query.timer,
278488c1f37fSAllen Pais 		    br_ip4_multicast_query_expired, 0);
2785cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
278688c1f37fSAllen Pais 	timer_setup(&br->ip6_other_query.timer,
278788c1f37fSAllen Pais 		    br_ip6_multicast_querier_expired, 0);
278888c1f37fSAllen Pais 	timer_setup(&br->ip6_own_query.timer,
278988c1f37fSAllen Pais 		    br_ip6_multicast_query_expired, 0);
2790cc0fdd80SLinus Lüssing #endif
279119e3a9c9SNikolay Aleksandrov 	INIT_HLIST_HEAD(&br->mdb_list);
27928b671779SNikolay Aleksandrov 	INIT_HLIST_HEAD(&br->src_gc_list);
27938b671779SNikolay Aleksandrov 	INIT_WORK(&br->src_gc_work, br_multicast_src_gc);
2794eb1d1641SHerbert Xu }
2795eb1d1641SHerbert Xu 
27964effd28cSLinus Lüssing static void br_ip4_multicast_join_snoopers(struct net_bridge *br)
27974effd28cSLinus Lüssing {
27984effd28cSLinus Lüssing 	struct in_device *in_dev = in_dev_get(br->dev);
27994effd28cSLinus Lüssing 
28004effd28cSLinus Lüssing 	if (!in_dev)
28014effd28cSLinus Lüssing 		return;
28024effd28cSLinus Lüssing 
28039fb20801SFlorian Fainelli 	__ip_mc_inc_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
28044effd28cSLinus Lüssing 	in_dev_put(in_dev);
28054effd28cSLinus Lüssing }
28064effd28cSLinus Lüssing 
28074effd28cSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
28084effd28cSLinus Lüssing static void br_ip6_multicast_join_snoopers(struct net_bridge *br)
28094effd28cSLinus Lüssing {
28104effd28cSLinus Lüssing 	struct in6_addr addr;
28114effd28cSLinus Lüssing 
28124effd28cSLinus Lüssing 	ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
28134effd28cSLinus Lüssing 	ipv6_dev_mc_inc(br->dev, &addr);
28144effd28cSLinus Lüssing }
28154effd28cSLinus Lüssing #else
28164effd28cSLinus Lüssing static inline void br_ip6_multicast_join_snoopers(struct net_bridge *br)
28174effd28cSLinus Lüssing {
28184effd28cSLinus Lüssing }
28194effd28cSLinus Lüssing #endif
28204effd28cSLinus Lüssing 
28214effd28cSLinus Lüssing static void br_multicast_join_snoopers(struct net_bridge *br)
28224effd28cSLinus Lüssing {
28234effd28cSLinus Lüssing 	br_ip4_multicast_join_snoopers(br);
28244effd28cSLinus Lüssing 	br_ip6_multicast_join_snoopers(br);
28254effd28cSLinus Lüssing }
28264effd28cSLinus Lüssing 
28274effd28cSLinus Lüssing static void br_ip4_multicast_leave_snoopers(struct net_bridge *br)
28284effd28cSLinus Lüssing {
28294effd28cSLinus Lüssing 	struct in_device *in_dev = in_dev_get(br->dev);
28304effd28cSLinus Lüssing 
28314effd28cSLinus Lüssing 	if (WARN_ON(!in_dev))
28324effd28cSLinus Lüssing 		return;
28334effd28cSLinus Lüssing 
28349fb20801SFlorian Fainelli 	__ip_mc_dec_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
28354effd28cSLinus Lüssing 	in_dev_put(in_dev);
28364effd28cSLinus Lüssing }
28374effd28cSLinus Lüssing 
28384effd28cSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
28394effd28cSLinus Lüssing static void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
28404effd28cSLinus Lüssing {
28414effd28cSLinus Lüssing 	struct in6_addr addr;
28424effd28cSLinus Lüssing 
28434effd28cSLinus Lüssing 	ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
28444effd28cSLinus Lüssing 	ipv6_dev_mc_dec(br->dev, &addr);
28454effd28cSLinus Lüssing }
28464effd28cSLinus Lüssing #else
28474effd28cSLinus Lüssing static inline void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
28484effd28cSLinus Lüssing {
28494effd28cSLinus Lüssing }
28504effd28cSLinus Lüssing #endif
28514effd28cSLinus Lüssing 
28524effd28cSLinus Lüssing static void br_multicast_leave_snoopers(struct net_bridge *br)
28534effd28cSLinus Lüssing {
28544effd28cSLinus Lüssing 	br_ip4_multicast_leave_snoopers(br);
28554effd28cSLinus Lüssing 	br_ip6_multicast_leave_snoopers(br);
28564effd28cSLinus Lüssing }
28574effd28cSLinus Lüssing 
2858cc0fdd80SLinus Lüssing static void __br_multicast_open(struct net_bridge *br,
285990010b36SLinus Lüssing 				struct bridge_mcast_own_query *query)
2860eb1d1641SHerbert Xu {
2861cc0fdd80SLinus Lüssing 	query->startup_sent = 0;
2862eb1d1641SHerbert Xu 
286313cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
2864eb1d1641SHerbert Xu 		return;
2865eb1d1641SHerbert Xu 
2866cc0fdd80SLinus Lüssing 	mod_timer(&query->timer, jiffies);
2867cc0fdd80SLinus Lüssing }
2868cc0fdd80SLinus Lüssing 
2869cc0fdd80SLinus Lüssing void br_multicast_open(struct net_bridge *br)
2870cc0fdd80SLinus Lüssing {
28714effd28cSLinus Lüssing 	if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
28724effd28cSLinus Lüssing 		br_multicast_join_snoopers(br);
28734effd28cSLinus Lüssing 
287490010b36SLinus Lüssing 	__br_multicast_open(br, &br->ip4_own_query);
2875cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
287690010b36SLinus Lüssing 	__br_multicast_open(br, &br->ip6_own_query);
2877cc0fdd80SLinus Lüssing #endif
2878eb1d1641SHerbert Xu }
2879eb1d1641SHerbert Xu 
2880eb1d1641SHerbert Xu void br_multicast_stop(struct net_bridge *br)
2881eb1d1641SHerbert Xu {
2882eb1d1641SHerbert Xu 	del_timer_sync(&br->multicast_router_timer);
288390010b36SLinus Lüssing 	del_timer_sync(&br->ip4_other_query.timer);
288490010b36SLinus Lüssing 	del_timer_sync(&br->ip4_own_query.timer);
2885cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
288690010b36SLinus Lüssing 	del_timer_sync(&br->ip6_other_query.timer);
288790010b36SLinus Lüssing 	del_timer_sync(&br->ip6_own_query.timer);
2888cc0fdd80SLinus Lüssing #endif
28894effd28cSLinus Lüssing 
28904effd28cSLinus Lüssing 	if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
28914effd28cSLinus Lüssing 		br_multicast_leave_snoopers(br);
2892e10177abSSatish Ashok }
2893e10177abSSatish Ashok 
2894e10177abSSatish Ashok void br_multicast_dev_del(struct net_bridge *br)
2895e10177abSSatish Ashok {
2896e10177abSSatish Ashok 	struct net_bridge_mdb_entry *mp;
28978b671779SNikolay Aleksandrov 	HLIST_HEAD(deleted_head);
289819e3a9c9SNikolay Aleksandrov 	struct hlist_node *tmp;
2899eb1d1641SHerbert Xu 
2900eb1d1641SHerbert Xu 	spin_lock_bh(&br->multicast_lock);
290119e3a9c9SNikolay Aleksandrov 	hlist_for_each_entry_safe(mp, tmp, &br->mdb_list, mdb_node) {
2902eb1d1641SHerbert Xu 		del_timer(&mp->timer);
290319e3a9c9SNikolay Aleksandrov 		rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode,
290419e3a9c9SNikolay Aleksandrov 				       br_mdb_rht_params);
290519e3a9c9SNikolay Aleksandrov 		hlist_del_rcu(&mp->mdb_node);
29064329596cSNikolay Aleksandrov 		kfree_rcu(mp, rcu);
2907eb1d1641SHerbert Xu 	}
29088b671779SNikolay Aleksandrov 	hlist_move_list(&br->src_gc_list, &deleted_head);
2909eb1d1641SHerbert Xu 	spin_unlock_bh(&br->multicast_lock);
291019e3a9c9SNikolay Aleksandrov 
29118b671779SNikolay Aleksandrov 	__grp_src_gc(&deleted_head);
29128b671779SNikolay Aleksandrov 	cancel_work_sync(&br->src_gc_work);
29138b671779SNikolay Aleksandrov 
29144329596cSNikolay Aleksandrov 	rcu_barrier();
2915eb1d1641SHerbert Xu }
29160909e117SHerbert Xu 
29170909e117SHerbert Xu int br_multicast_set_router(struct net_bridge *br, unsigned long val)
29180909e117SHerbert Xu {
29196ae4ae8eSLinus Lüssing 	int err = -EINVAL;
29200909e117SHerbert Xu 
29210909e117SHerbert Xu 	spin_lock_bh(&br->multicast_lock);
29220909e117SHerbert Xu 
29230909e117SHerbert Xu 	switch (val) {
29247f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_DISABLED:
29257f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_PERM:
292677041420SYotam Gigi 		br_mc_router_state_change(br, val == MDB_RTR_TYPE_PERM);
29270909e117SHerbert Xu 		del_timer(&br->multicast_router_timer);
292877041420SYotam Gigi 		br->multicast_router = val;
292977041420SYotam Gigi 		err = 0;
293077041420SYotam Gigi 		break;
29317f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP_QUERY:
293277041420SYotam Gigi 		if (br->multicast_router != MDB_RTR_TYPE_TEMP_QUERY)
293377041420SYotam Gigi 			br_mc_router_state_change(br, false);
29340909e117SHerbert Xu 		br->multicast_router = val;
29350909e117SHerbert Xu 		err = 0;
29360909e117SHerbert Xu 		break;
29370909e117SHerbert Xu 	}
29380909e117SHerbert Xu 
29390909e117SHerbert Xu 	spin_unlock_bh(&br->multicast_lock);
29400909e117SHerbert Xu 
29410909e117SHerbert Xu 	return err;
29420909e117SHerbert Xu }
29430909e117SHerbert Xu 
29447f0aec7aSNikolay Aleksandrov static void __del_port_router(struct net_bridge_port *p)
29457f0aec7aSNikolay Aleksandrov {
29467f0aec7aSNikolay Aleksandrov 	if (hlist_unhashed(&p->rlist))
29477f0aec7aSNikolay Aleksandrov 		return;
29487f0aec7aSNikolay Aleksandrov 	hlist_del_init_rcu(&p->rlist);
29497f0aec7aSNikolay Aleksandrov 	br_rtr_notify(p->br->dev, p, RTM_DELMDB);
29506d549648SNogah Frankel 	br_port_mc_router_state_change(p, false);
2951f12e7d95SNogah Frankel 
2952f12e7d95SNogah Frankel 	/* don't allow timer refresh */
2953f12e7d95SNogah Frankel 	if (p->multicast_router == MDB_RTR_TYPE_TEMP)
2954f12e7d95SNogah Frankel 		p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
29557f0aec7aSNikolay Aleksandrov }
29567f0aec7aSNikolay Aleksandrov 
29570909e117SHerbert Xu int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
29580909e117SHerbert Xu {
29590909e117SHerbert Xu 	struct net_bridge *br = p->br;
2960a55d8246SNikolay Aleksandrov 	unsigned long now = jiffies;
29616ae4ae8eSLinus Lüssing 	int err = -EINVAL;
29620909e117SHerbert Xu 
29630909e117SHerbert Xu 	spin_lock(&br->multicast_lock);
29644950cfd1SNikolay Aleksandrov 	if (p->multicast_router == val) {
2965a55d8246SNikolay Aleksandrov 		/* Refresh the temp router port timer */
2966a55d8246SNikolay Aleksandrov 		if (p->multicast_router == MDB_RTR_TYPE_TEMP)
2967a55d8246SNikolay Aleksandrov 			mod_timer(&p->multicast_router_timer,
2968a55d8246SNikolay Aleksandrov 				  now + br->multicast_querier_interval);
29694950cfd1SNikolay Aleksandrov 		err = 0;
29704950cfd1SNikolay Aleksandrov 		goto unlock;
29714950cfd1SNikolay Aleksandrov 	}
29720909e117SHerbert Xu 	switch (val) {
29737f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_DISABLED:
29747f0aec7aSNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_DISABLED;
29757f0aec7aSNikolay Aleksandrov 		__del_port_router(p);
29760909e117SHerbert Xu 		del_timer(&p->multicast_router_timer);
29770909e117SHerbert Xu 		break;
29787f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP_QUERY:
29797f0aec7aSNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
29807f0aec7aSNikolay Aleksandrov 		__del_port_router(p);
29817f0aec7aSNikolay Aleksandrov 		break;
29827f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_PERM:
29837f0aec7aSNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_PERM;
29847f0aec7aSNikolay Aleksandrov 		del_timer(&p->multicast_router_timer);
29850909e117SHerbert Xu 		br_multicast_add_router(br, p);
29860909e117SHerbert Xu 		break;
2987a55d8246SNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP:
2988a55d8246SNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_TEMP;
2989a55d8246SNikolay Aleksandrov 		br_multicast_mark_router(br, p);
2990a55d8246SNikolay Aleksandrov 		break;
29917f0aec7aSNikolay Aleksandrov 	default:
29927f0aec7aSNikolay Aleksandrov 		goto unlock;
29930909e117SHerbert Xu 	}
29947f0aec7aSNikolay Aleksandrov 	err = 0;
29957f0aec7aSNikolay Aleksandrov unlock:
29960909e117SHerbert Xu 	spin_unlock(&br->multicast_lock);
29970909e117SHerbert Xu 
29980909e117SHerbert Xu 	return err;
29990909e117SHerbert Xu }
3000561f1103SHerbert Xu 
3001cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br,
300290010b36SLinus Lüssing 				       struct bridge_mcast_own_query *query)
3003561f1103SHerbert Xu {
3004561f1103SHerbert Xu 	struct net_bridge_port *port;
300574857216SHerbert Xu 
3006cc0fdd80SLinus Lüssing 	__br_multicast_open(br, query);
300774857216SHerbert Xu 
3008c5b493ceSNikolay Aleksandrov 	rcu_read_lock();
3009c5b493ceSNikolay Aleksandrov 	list_for_each_entry_rcu(port, &br->port_list, list) {
301074857216SHerbert Xu 		if (port->state == BR_STATE_DISABLED ||
301174857216SHerbert Xu 		    port->state == BR_STATE_BLOCKING)
301274857216SHerbert Xu 			continue;
301374857216SHerbert Xu 
301490010b36SLinus Lüssing 		if (query == &br->ip4_own_query)
301590010b36SLinus Lüssing 			br_multicast_enable(&port->ip4_own_query);
3016cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
3017cc0fdd80SLinus Lüssing 		else
301890010b36SLinus Lüssing 			br_multicast_enable(&port->ip6_own_query);
3019cc0fdd80SLinus Lüssing #endif
302074857216SHerbert Xu 	}
3021c5b493ceSNikolay Aleksandrov 	rcu_read_unlock();
302274857216SHerbert Xu }
302374857216SHerbert Xu 
302474857216SHerbert Xu int br_multicast_toggle(struct net_bridge *br, unsigned long val)
302574857216SHerbert Xu {
30267cb3f921SNikolay Aleksandrov 	struct net_bridge_port *port;
3027561f1103SHerbert Xu 
3028ef5e0d82SAndrey Vagin 	spin_lock_bh(&br->multicast_lock);
302913cefad2SNikolay Aleksandrov 	if (!!br_opt_get(br, BROPT_MULTICAST_ENABLED) == !!val)
3030561f1103SHerbert Xu 		goto unlock;
3031561f1103SHerbert Xu 
303213cefad2SNikolay Aleksandrov 	br_mc_disabled_update(br->dev, val);
303313cefad2SNikolay Aleksandrov 	br_opt_toggle(br, BROPT_MULTICAST_ENABLED, !!val);
30344effd28cSLinus Lüssing 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) {
30354effd28cSLinus Lüssing 		br_multicast_leave_snoopers(br);
3036561f1103SHerbert Xu 		goto unlock;
30374effd28cSLinus Lüssing 	}
3038561f1103SHerbert Xu 
30393a7fda06SHerbert Xu 	if (!netif_running(br->dev))
30403a7fda06SHerbert Xu 		goto unlock;
30413a7fda06SHerbert Xu 
30427cb3f921SNikolay Aleksandrov 	br_multicast_open(br);
30437cb3f921SNikolay Aleksandrov 	list_for_each_entry(port, &br->port_list, list)
30447cb3f921SNikolay Aleksandrov 		__br_multicast_enable_port(port);
3045561f1103SHerbert Xu 
3046561f1103SHerbert Xu unlock:
3047ef5e0d82SAndrey Vagin 	spin_unlock_bh(&br->multicast_lock);
3048561f1103SHerbert Xu 
3049a26d94bfSYueHaibing 	return 0;
3050561f1103SHerbert Xu }
3051b195167fSHerbert Xu 
30529341b988SIdo Schimmel bool br_multicast_enabled(const struct net_device *dev)
30539341b988SIdo Schimmel {
30549341b988SIdo Schimmel 	struct net_bridge *br = netdev_priv(dev);
30559341b988SIdo Schimmel 
305613cefad2SNikolay Aleksandrov 	return !!br_opt_get(br, BROPT_MULTICAST_ENABLED);
30579341b988SIdo Schimmel }
30589341b988SIdo Schimmel EXPORT_SYMBOL_GPL(br_multicast_enabled);
30599341b988SIdo Schimmel 
30600912bda4SYotam Gigi bool br_multicast_router(const struct net_device *dev)
30610912bda4SYotam Gigi {
30620912bda4SYotam Gigi 	struct net_bridge *br = netdev_priv(dev);
30630912bda4SYotam Gigi 	bool is_router;
30640912bda4SYotam Gigi 
30650912bda4SYotam Gigi 	spin_lock_bh(&br->multicast_lock);
30660912bda4SYotam Gigi 	is_router = br_multicast_is_router(br);
30670912bda4SYotam Gigi 	spin_unlock_bh(&br->multicast_lock);
30680912bda4SYotam Gigi 	return is_router;
30690912bda4SYotam Gigi }
30700912bda4SYotam Gigi EXPORT_SYMBOL_GPL(br_multicast_router);
30710912bda4SYotam Gigi 
3072c5c23260SHerbert Xu int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
3073c5c23260SHerbert Xu {
3074b00589afSLinus Lüssing 	unsigned long max_delay;
3075b00589afSLinus Lüssing 
3076c5c23260SHerbert Xu 	val = !!val;
3077c5c23260SHerbert Xu 
3078c5c23260SHerbert Xu 	spin_lock_bh(&br->multicast_lock);
3079675779adSNikolay Aleksandrov 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER) == val)
3080c5c23260SHerbert Xu 		goto unlock;
3081c5c23260SHerbert Xu 
3082675779adSNikolay Aleksandrov 	br_opt_toggle(br, BROPT_MULTICAST_QUERIER, !!val);
3083b00589afSLinus Lüssing 	if (!val)
3084b00589afSLinus Lüssing 		goto unlock;
3085b00589afSLinus Lüssing 
3086b00589afSLinus Lüssing 	max_delay = br->multicast_query_response_interval;
3087b00589afSLinus Lüssing 
308890010b36SLinus Lüssing 	if (!timer_pending(&br->ip4_other_query.timer))
308990010b36SLinus Lüssing 		br->ip4_other_query.delay_time = jiffies + max_delay;
3090cc0fdd80SLinus Lüssing 
309190010b36SLinus Lüssing 	br_multicast_start_querier(br, &br->ip4_own_query);
3092cc0fdd80SLinus Lüssing 
3093cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
309490010b36SLinus Lüssing 	if (!timer_pending(&br->ip6_other_query.timer))
309590010b36SLinus Lüssing 		br->ip6_other_query.delay_time = jiffies + max_delay;
3096cc0fdd80SLinus Lüssing 
309790010b36SLinus Lüssing 	br_multicast_start_querier(br, &br->ip6_own_query);
3098cc0fdd80SLinus Lüssing #endif
3099c5c23260SHerbert Xu 
3100c5c23260SHerbert Xu unlock:
3101c5c23260SHerbert Xu 	spin_unlock_bh(&br->multicast_lock);
3102c5c23260SHerbert Xu 
3103c5c23260SHerbert Xu 	return 0;
3104c5c23260SHerbert Xu }
3105c5c23260SHerbert Xu 
31065e923585SNikolay Aleksandrov int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val)
31075e923585SNikolay Aleksandrov {
31085e923585SNikolay Aleksandrov 	/* Currently we support only version 2 and 3 */
31095e923585SNikolay Aleksandrov 	switch (val) {
31105e923585SNikolay Aleksandrov 	case 2:
31115e923585SNikolay Aleksandrov 	case 3:
31125e923585SNikolay Aleksandrov 		break;
31135e923585SNikolay Aleksandrov 	default:
31145e923585SNikolay Aleksandrov 		return -EINVAL;
31155e923585SNikolay Aleksandrov 	}
31165e923585SNikolay Aleksandrov 
31175e923585SNikolay Aleksandrov 	spin_lock_bh(&br->multicast_lock);
31185e923585SNikolay Aleksandrov 	br->multicast_igmp_version = val;
31195e923585SNikolay Aleksandrov 	spin_unlock_bh(&br->multicast_lock);
31205e923585SNikolay Aleksandrov 
31215e923585SNikolay Aleksandrov 	return 0;
31225e923585SNikolay Aleksandrov }
31235e923585SNikolay Aleksandrov 
3124aa2ae3e7SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
3125aa2ae3e7SNikolay Aleksandrov int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val)
3126aa2ae3e7SNikolay Aleksandrov {
3127aa2ae3e7SNikolay Aleksandrov 	/* Currently we support version 1 and 2 */
3128aa2ae3e7SNikolay Aleksandrov 	switch (val) {
3129aa2ae3e7SNikolay Aleksandrov 	case 1:
3130aa2ae3e7SNikolay Aleksandrov 	case 2:
3131aa2ae3e7SNikolay Aleksandrov 		break;
3132aa2ae3e7SNikolay Aleksandrov 	default:
3133aa2ae3e7SNikolay Aleksandrov 		return -EINVAL;
3134aa2ae3e7SNikolay Aleksandrov 	}
3135aa2ae3e7SNikolay Aleksandrov 
3136aa2ae3e7SNikolay Aleksandrov 	spin_lock_bh(&br->multicast_lock);
3137aa2ae3e7SNikolay Aleksandrov 	br->multicast_mld_version = val;
3138aa2ae3e7SNikolay Aleksandrov 	spin_unlock_bh(&br->multicast_lock);
3139aa2ae3e7SNikolay Aleksandrov 
3140aa2ae3e7SNikolay Aleksandrov 	return 0;
3141aa2ae3e7SNikolay Aleksandrov }
3142aa2ae3e7SNikolay Aleksandrov #endif
3143aa2ae3e7SNikolay Aleksandrov 
314407f8ac4aSLinus Lüssing /**
314507f8ac4aSLinus Lüssing  * br_multicast_list_adjacent - Returns snooped multicast addresses
314607f8ac4aSLinus Lüssing  * @dev:	The bridge port adjacent to which to retrieve addresses
314707f8ac4aSLinus Lüssing  * @br_ip_list:	The list to store found, snooped multicast IP addresses in
314807f8ac4aSLinus Lüssing  *
314907f8ac4aSLinus Lüssing  * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
315007f8ac4aSLinus Lüssing  * snooping feature on all bridge ports of dev's bridge device, excluding
315107f8ac4aSLinus Lüssing  * the addresses from dev itself.
315207f8ac4aSLinus Lüssing  *
315307f8ac4aSLinus Lüssing  * Returns the number of items added to br_ip_list.
315407f8ac4aSLinus Lüssing  *
315507f8ac4aSLinus Lüssing  * Notes:
315607f8ac4aSLinus Lüssing  * - br_ip_list needs to be initialized by caller
315707f8ac4aSLinus Lüssing  * - br_ip_list might contain duplicates in the end
315807f8ac4aSLinus Lüssing  *   (needs to be taken care of by caller)
315907f8ac4aSLinus Lüssing  * - br_ip_list needs to be freed by caller
316007f8ac4aSLinus Lüssing  */
316107f8ac4aSLinus Lüssing int br_multicast_list_adjacent(struct net_device *dev,
316207f8ac4aSLinus Lüssing 			       struct list_head *br_ip_list)
316307f8ac4aSLinus Lüssing {
316407f8ac4aSLinus Lüssing 	struct net_bridge *br;
316507f8ac4aSLinus Lüssing 	struct net_bridge_port *port;
316607f8ac4aSLinus Lüssing 	struct net_bridge_port_group *group;
316707f8ac4aSLinus Lüssing 	struct br_ip_list *entry;
316807f8ac4aSLinus Lüssing 	int count = 0;
316907f8ac4aSLinus Lüssing 
317007f8ac4aSLinus Lüssing 	rcu_read_lock();
317135f861e3SJulian Wiedmann 	if (!br_ip_list || !netif_is_bridge_port(dev))
317207f8ac4aSLinus Lüssing 		goto unlock;
317307f8ac4aSLinus Lüssing 
317407f8ac4aSLinus Lüssing 	port = br_port_get_rcu(dev);
317507f8ac4aSLinus Lüssing 	if (!port || !port->br)
317607f8ac4aSLinus Lüssing 		goto unlock;
317707f8ac4aSLinus Lüssing 
317807f8ac4aSLinus Lüssing 	br = port->br;
317907f8ac4aSLinus Lüssing 
318007f8ac4aSLinus Lüssing 	list_for_each_entry_rcu(port, &br->port_list, list) {
318107f8ac4aSLinus Lüssing 		if (!port->dev || port->dev == dev)
318207f8ac4aSLinus Lüssing 			continue;
318307f8ac4aSLinus Lüssing 
318407f8ac4aSLinus Lüssing 		hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
318507f8ac4aSLinus Lüssing 			entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
318607f8ac4aSLinus Lüssing 			if (!entry)
318707f8ac4aSLinus Lüssing 				goto unlock;
318807f8ac4aSLinus Lüssing 
318907f8ac4aSLinus Lüssing 			entry->addr = group->addr;
319007f8ac4aSLinus Lüssing 			list_add(&entry->list, br_ip_list);
319107f8ac4aSLinus Lüssing 			count++;
319207f8ac4aSLinus Lüssing 		}
319307f8ac4aSLinus Lüssing 	}
319407f8ac4aSLinus Lüssing 
319507f8ac4aSLinus Lüssing unlock:
319607f8ac4aSLinus Lüssing 	rcu_read_unlock();
319707f8ac4aSLinus Lüssing 	return count;
319807f8ac4aSLinus Lüssing }
319907f8ac4aSLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
32002cd41431SLinus Lüssing 
32012cd41431SLinus Lüssing /**
3202c34963e2SLinus Lüssing  * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
3203c34963e2SLinus Lüssing  * @dev: The bridge port providing the bridge on which to check for a querier
3204c34963e2SLinus Lüssing  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
3205c34963e2SLinus Lüssing  *
3206c34963e2SLinus Lüssing  * Checks whether the given interface has a bridge on top and if so returns
3207c34963e2SLinus Lüssing  * true if a valid querier exists anywhere on the bridged link layer.
3208c34963e2SLinus Lüssing  * Otherwise returns false.
3209c34963e2SLinus Lüssing  */
3210c34963e2SLinus Lüssing bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
3211c34963e2SLinus Lüssing {
3212c34963e2SLinus Lüssing 	struct net_bridge *br;
3213c34963e2SLinus Lüssing 	struct net_bridge_port *port;
3214c34963e2SLinus Lüssing 	struct ethhdr eth;
3215c34963e2SLinus Lüssing 	bool ret = false;
3216c34963e2SLinus Lüssing 
3217c34963e2SLinus Lüssing 	rcu_read_lock();
321835f861e3SJulian Wiedmann 	if (!netif_is_bridge_port(dev))
3219c34963e2SLinus Lüssing 		goto unlock;
3220c34963e2SLinus Lüssing 
3221c34963e2SLinus Lüssing 	port = br_port_get_rcu(dev);
3222c34963e2SLinus Lüssing 	if (!port || !port->br)
3223c34963e2SLinus Lüssing 		goto unlock;
3224c34963e2SLinus Lüssing 
3225c34963e2SLinus Lüssing 	br = port->br;
3226c34963e2SLinus Lüssing 
3227c34963e2SLinus Lüssing 	memset(&eth, 0, sizeof(eth));
3228c34963e2SLinus Lüssing 	eth.h_proto = htons(proto);
3229c34963e2SLinus Lüssing 
3230c34963e2SLinus Lüssing 	ret = br_multicast_querier_exists(br, &eth);
3231c34963e2SLinus Lüssing 
3232c34963e2SLinus Lüssing unlock:
3233c34963e2SLinus Lüssing 	rcu_read_unlock();
3234c34963e2SLinus Lüssing 	return ret;
3235c34963e2SLinus Lüssing }
3236c34963e2SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
3237c34963e2SLinus Lüssing 
3238c34963e2SLinus Lüssing /**
32392cd41431SLinus Lüssing  * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
32402cd41431SLinus Lüssing  * @dev: The bridge port adjacent to which to check for a querier
32412cd41431SLinus Lüssing  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
32422cd41431SLinus Lüssing  *
32432cd41431SLinus Lüssing  * Checks whether the given interface has a bridge on top and if so returns
32442cd41431SLinus Lüssing  * true if a selected querier is behind one of the other ports of this
32452cd41431SLinus Lüssing  * bridge. Otherwise returns false.
32462cd41431SLinus Lüssing  */
32472cd41431SLinus Lüssing bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
32482cd41431SLinus Lüssing {
32492cd41431SLinus Lüssing 	struct net_bridge *br;
32502cd41431SLinus Lüssing 	struct net_bridge_port *port;
32512cd41431SLinus Lüssing 	bool ret = false;
32522cd41431SLinus Lüssing 
32532cd41431SLinus Lüssing 	rcu_read_lock();
325435f861e3SJulian Wiedmann 	if (!netif_is_bridge_port(dev))
32552cd41431SLinus Lüssing 		goto unlock;
32562cd41431SLinus Lüssing 
32572cd41431SLinus Lüssing 	port = br_port_get_rcu(dev);
32582cd41431SLinus Lüssing 	if (!port || !port->br)
32592cd41431SLinus Lüssing 		goto unlock;
32602cd41431SLinus Lüssing 
32612cd41431SLinus Lüssing 	br = port->br;
32622cd41431SLinus Lüssing 
32632cd41431SLinus Lüssing 	switch (proto) {
32642cd41431SLinus Lüssing 	case ETH_P_IP:
32652cd41431SLinus Lüssing 		if (!timer_pending(&br->ip4_other_query.timer) ||
32662cd41431SLinus Lüssing 		    rcu_dereference(br->ip4_querier.port) == port)
32672cd41431SLinus Lüssing 			goto unlock;
32682cd41431SLinus Lüssing 		break;
32693993c4e1SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
32702cd41431SLinus Lüssing 	case ETH_P_IPV6:
32712cd41431SLinus Lüssing 		if (!timer_pending(&br->ip6_other_query.timer) ||
32722cd41431SLinus Lüssing 		    rcu_dereference(br->ip6_querier.port) == port)
32732cd41431SLinus Lüssing 			goto unlock;
32742cd41431SLinus Lüssing 		break;
32753993c4e1SLinus Lüssing #endif
32762cd41431SLinus Lüssing 	default:
32772cd41431SLinus Lüssing 		goto unlock;
32782cd41431SLinus Lüssing 	}
32792cd41431SLinus Lüssing 
32802cd41431SLinus Lüssing 	ret = true;
32812cd41431SLinus Lüssing unlock:
32822cd41431SLinus Lüssing 	rcu_read_unlock();
32832cd41431SLinus Lüssing 	return ret;
32842cd41431SLinus Lüssing }
32852cd41431SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
32861080ab95SNikolay Aleksandrov 
32871080ab95SNikolay Aleksandrov static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
3288a65056ecSNikolay Aleksandrov 			       const struct sk_buff *skb, u8 type, u8 dir)
32891080ab95SNikolay Aleksandrov {
32901080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
3291a65056ecSNikolay Aleksandrov 	__be16 proto = skb->protocol;
3292a65056ecSNikolay Aleksandrov 	unsigned int t_len;
32931080ab95SNikolay Aleksandrov 
32941080ab95SNikolay Aleksandrov 	u64_stats_update_begin(&pstats->syncp);
32951080ab95SNikolay Aleksandrov 	switch (proto) {
32961080ab95SNikolay Aleksandrov 	case htons(ETH_P_IP):
3297a65056ecSNikolay Aleksandrov 		t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
32981080ab95SNikolay Aleksandrov 		switch (type) {
32991080ab95SNikolay Aleksandrov 		case IGMP_HOST_MEMBERSHIP_REPORT:
33001080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v1reports[dir]++;
33011080ab95SNikolay Aleksandrov 			break;
33021080ab95SNikolay Aleksandrov 		case IGMPV2_HOST_MEMBERSHIP_REPORT:
33031080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v2reports[dir]++;
33041080ab95SNikolay Aleksandrov 			break;
33051080ab95SNikolay Aleksandrov 		case IGMPV3_HOST_MEMBERSHIP_REPORT:
33061080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v3reports[dir]++;
33071080ab95SNikolay Aleksandrov 			break;
33081080ab95SNikolay Aleksandrov 		case IGMP_HOST_MEMBERSHIP_QUERY:
3309a65056ecSNikolay Aleksandrov 			if (t_len != sizeof(struct igmphdr)) {
3310a65056ecSNikolay Aleksandrov 				pstats->mstats.igmp_v3queries[dir]++;
3311a65056ecSNikolay Aleksandrov 			} else {
3312a65056ecSNikolay Aleksandrov 				unsigned int offset = skb_transport_offset(skb);
3313a65056ecSNikolay Aleksandrov 				struct igmphdr *ih, _ihdr;
3314a65056ecSNikolay Aleksandrov 
3315a65056ecSNikolay Aleksandrov 				ih = skb_header_pointer(skb, offset,
3316a65056ecSNikolay Aleksandrov 							sizeof(_ihdr), &_ihdr);
3317a65056ecSNikolay Aleksandrov 				if (!ih)
3318a65056ecSNikolay Aleksandrov 					break;
3319a65056ecSNikolay Aleksandrov 				if (!ih->code)
3320a65056ecSNikolay Aleksandrov 					pstats->mstats.igmp_v1queries[dir]++;
3321a65056ecSNikolay Aleksandrov 				else
3322a65056ecSNikolay Aleksandrov 					pstats->mstats.igmp_v2queries[dir]++;
3323a65056ecSNikolay Aleksandrov 			}
33241080ab95SNikolay Aleksandrov 			break;
33251080ab95SNikolay Aleksandrov 		case IGMP_HOST_LEAVE_MESSAGE:
33261080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_leaves[dir]++;
33271080ab95SNikolay Aleksandrov 			break;
33281080ab95SNikolay Aleksandrov 		}
33291080ab95SNikolay Aleksandrov 		break;
33301080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
33311080ab95SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
3332a65056ecSNikolay Aleksandrov 		t_len = ntohs(ipv6_hdr(skb)->payload_len) +
3333a65056ecSNikolay Aleksandrov 			sizeof(struct ipv6hdr);
3334a65056ecSNikolay Aleksandrov 		t_len -= skb_network_header_len(skb);
33351080ab95SNikolay Aleksandrov 		switch (type) {
33361080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_REPORT:
33371080ab95SNikolay Aleksandrov 			pstats->mstats.mld_v1reports[dir]++;
33381080ab95SNikolay Aleksandrov 			break;
33391080ab95SNikolay Aleksandrov 		case ICMPV6_MLD2_REPORT:
33401080ab95SNikolay Aleksandrov 			pstats->mstats.mld_v2reports[dir]++;
33411080ab95SNikolay Aleksandrov 			break;
33421080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_QUERY:
3343a65056ecSNikolay Aleksandrov 			if (t_len != sizeof(struct mld_msg))
3344a65056ecSNikolay Aleksandrov 				pstats->mstats.mld_v2queries[dir]++;
3345a65056ecSNikolay Aleksandrov 			else
3346a65056ecSNikolay Aleksandrov 				pstats->mstats.mld_v1queries[dir]++;
33471080ab95SNikolay Aleksandrov 			break;
33481080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_REDUCTION:
33491080ab95SNikolay Aleksandrov 			pstats->mstats.mld_leaves[dir]++;
33501080ab95SNikolay Aleksandrov 			break;
33511080ab95SNikolay Aleksandrov 		}
33521080ab95SNikolay Aleksandrov 		break;
33531080ab95SNikolay Aleksandrov #endif /* CONFIG_IPV6 */
33541080ab95SNikolay Aleksandrov 	}
33551080ab95SNikolay Aleksandrov 	u64_stats_update_end(&pstats->syncp);
33561080ab95SNikolay Aleksandrov }
33571080ab95SNikolay Aleksandrov 
33581080ab95SNikolay Aleksandrov void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
3359a65056ecSNikolay Aleksandrov 			const struct sk_buff *skb, u8 type, u8 dir)
33601080ab95SNikolay Aleksandrov {
33611080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
33621080ab95SNikolay Aleksandrov 
33631080ab95SNikolay Aleksandrov 	/* if multicast_disabled is true then igmp type can't be set */
3364675779adSNikolay Aleksandrov 	if (!type || !br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
33651080ab95SNikolay Aleksandrov 		return;
33661080ab95SNikolay Aleksandrov 
33671080ab95SNikolay Aleksandrov 	if (p)
33681080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
33691080ab95SNikolay Aleksandrov 	else
33701080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
33711080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
33721080ab95SNikolay Aleksandrov 		return;
33731080ab95SNikolay Aleksandrov 
3374a65056ecSNikolay Aleksandrov 	br_mcast_stats_add(stats, skb, type, dir);
33751080ab95SNikolay Aleksandrov }
33761080ab95SNikolay Aleksandrov 
33771080ab95SNikolay Aleksandrov int br_multicast_init_stats(struct net_bridge *br)
33781080ab95SNikolay Aleksandrov {
33791080ab95SNikolay Aleksandrov 	br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
33801080ab95SNikolay Aleksandrov 	if (!br->mcast_stats)
33811080ab95SNikolay Aleksandrov 		return -ENOMEM;
33821080ab95SNikolay Aleksandrov 
33831080ab95SNikolay Aleksandrov 	return 0;
33841080ab95SNikolay Aleksandrov }
33851080ab95SNikolay Aleksandrov 
3386b6fe0440SIdo Schimmel void br_multicast_uninit_stats(struct net_bridge *br)
3387b6fe0440SIdo Schimmel {
3388b6fe0440SIdo Schimmel 	free_percpu(br->mcast_stats);
3389b6fe0440SIdo Schimmel }
3390b6fe0440SIdo Schimmel 
3391b3b6a84cSArnd Bergmann /* noinline for https://bugs.llvm.org/show_bug.cgi?id=45802#c9 */
3392b3b6a84cSArnd Bergmann static noinline_for_stack void mcast_stats_add_dir(u64 *dst, u64 *src)
33931080ab95SNikolay Aleksandrov {
33941080ab95SNikolay Aleksandrov 	dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
33951080ab95SNikolay Aleksandrov 	dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
33961080ab95SNikolay Aleksandrov }
33971080ab95SNikolay Aleksandrov 
33981080ab95SNikolay Aleksandrov void br_multicast_get_stats(const struct net_bridge *br,
33991080ab95SNikolay Aleksandrov 			    const struct net_bridge_port *p,
34001080ab95SNikolay Aleksandrov 			    struct br_mcast_stats *dest)
34011080ab95SNikolay Aleksandrov {
34021080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
34031080ab95SNikolay Aleksandrov 	struct br_mcast_stats tdst;
34041080ab95SNikolay Aleksandrov 	int i;
34051080ab95SNikolay Aleksandrov 
34061080ab95SNikolay Aleksandrov 	memset(dest, 0, sizeof(*dest));
34071080ab95SNikolay Aleksandrov 	if (p)
34081080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
34091080ab95SNikolay Aleksandrov 	else
34101080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
34111080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
34121080ab95SNikolay Aleksandrov 		return;
34131080ab95SNikolay Aleksandrov 
34141080ab95SNikolay Aleksandrov 	memset(&tdst, 0, sizeof(tdst));
34151080ab95SNikolay Aleksandrov 	for_each_possible_cpu(i) {
34161080ab95SNikolay Aleksandrov 		struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
34171080ab95SNikolay Aleksandrov 		struct br_mcast_stats temp;
34181080ab95SNikolay Aleksandrov 		unsigned int start;
34191080ab95SNikolay Aleksandrov 
34201080ab95SNikolay Aleksandrov 		do {
34211080ab95SNikolay Aleksandrov 			start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
34221080ab95SNikolay Aleksandrov 			memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
34231080ab95SNikolay Aleksandrov 		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
34241080ab95SNikolay Aleksandrov 
3425a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
3426a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
3427a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
34281080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
34291080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
34301080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
34311080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
34321080ab95SNikolay Aleksandrov 		tdst.igmp_parse_errors += temp.igmp_parse_errors;
34331080ab95SNikolay Aleksandrov 
3434a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
3435a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
34361080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
34371080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
34381080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
34391080ab95SNikolay Aleksandrov 		tdst.mld_parse_errors += temp.mld_parse_errors;
34401080ab95SNikolay Aleksandrov 	}
34411080ab95SNikolay Aleksandrov 	memcpy(dest, &tdst, sizeof(*dest));
34421080ab95SNikolay Aleksandrov }
344319e3a9c9SNikolay Aleksandrov 
344419e3a9c9SNikolay Aleksandrov int br_mdb_hash_init(struct net_bridge *br)
344519e3a9c9SNikolay Aleksandrov {
344619e3a9c9SNikolay Aleksandrov 	return rhashtable_init(&br->mdb_hash_tbl, &br_mdb_rht_params);
344719e3a9c9SNikolay Aleksandrov }
344819e3a9c9SNikolay Aleksandrov 
344919e3a9c9SNikolay Aleksandrov void br_mdb_hash_fini(struct net_bridge *br)
345019e3a9c9SNikolay Aleksandrov {
345119e3a9c9SNikolay Aleksandrov 	rhashtable_destroy(&br->mdb_hash_tbl);
345219e3a9c9SNikolay Aleksandrov }
3453