xref: /openbmc/linux/net/bridge/br_multicast.c (revision 42c11ccfe844c1df91599428d87101e07e389901)
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);
53*42c11ccfSNikolay 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);
188*42c11ccfSNikolay 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);
191681590bdSNikolay Aleksandrov 	br_mdb_notify(br->dev, pg->port, &pg->addr, RTM_DELMDB, pg->flags);
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);
225eb1d1641SHerbert Xu 	struct net_bridge *br = pg->port->br;
226eb1d1641SHerbert Xu 
227eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
228eb1d1641SHerbert Xu 	if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
2299d06b6d8SElad Raz 	    hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT)
230eb1d1641SHerbert Xu 		goto out;
231eb1d1641SHerbert Xu 
232681590bdSNikolay Aleksandrov 	br_multicast_find_del_pg(br, pg);
233eb1d1641SHerbert Xu 
234eb1d1641SHerbert Xu out:
235eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
236eb1d1641SHerbert Xu }
237eb1d1641SHerbert Xu 
2388ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
239438ef2d0SNikolay Aleksandrov 						    struct net_bridge_port_group *pg,
240438ef2d0SNikolay Aleksandrov 						    __be32 ip_dst, __be32 group,
241438ef2d0SNikolay Aleksandrov 						    bool with_srcs, bool over_lmqt,
242*42c11ccfSNikolay Aleksandrov 						    u8 sflag, u8 *igmp_type,
243*42c11ccfSNikolay Aleksandrov 						    bool *need_rexmit)
244eb1d1641SHerbert Xu {
245438ef2d0SNikolay Aleksandrov 	struct net_bridge_port *p = pg ? pg->port : NULL;
246438ef2d0SNikolay Aleksandrov 	struct net_bridge_group_src *ent;
247438ef2d0SNikolay Aleksandrov 	size_t pkt_size, igmp_hdr_size;
248438ef2d0SNikolay Aleksandrov 	unsigned long now = jiffies;
2495e923585SNikolay Aleksandrov 	struct igmpv3_query *ihv3;
250438ef2d0SNikolay Aleksandrov 	void *csum_start = NULL;
251438ef2d0SNikolay Aleksandrov 	__sum16 *csum = NULL;
252eb1d1641SHerbert Xu 	struct sk_buff *skb;
253eb1d1641SHerbert Xu 	struct igmphdr *ih;
254eb1d1641SHerbert Xu 	struct ethhdr *eth;
255438ef2d0SNikolay Aleksandrov 	unsigned long lmqt;
256eb1d1641SHerbert Xu 	struct iphdr *iph;
257438ef2d0SNikolay Aleksandrov 	u16 lmqt_srcs = 0;
258eb1d1641SHerbert Xu 
2595e923585SNikolay Aleksandrov 	igmp_hdr_size = sizeof(*ih);
260438ef2d0SNikolay Aleksandrov 	if (br->multicast_igmp_version == 3) {
2615e923585SNikolay Aleksandrov 		igmp_hdr_size = sizeof(*ihv3);
262438ef2d0SNikolay Aleksandrov 		if (pg && with_srcs) {
263438ef2d0SNikolay Aleksandrov 			lmqt = now + (br->multicast_last_member_interval *
264438ef2d0SNikolay Aleksandrov 				      br->multicast_last_member_count);
265438ef2d0SNikolay Aleksandrov 			hlist_for_each_entry(ent, &pg->src_list, node) {
266438ef2d0SNikolay Aleksandrov 				if (over_lmqt == time_after(ent->timer.expires,
267438ef2d0SNikolay Aleksandrov 							    lmqt) &&
268438ef2d0SNikolay Aleksandrov 				    ent->src_query_rexmit_cnt > 0)
269438ef2d0SNikolay Aleksandrov 					lmqt_srcs++;
270438ef2d0SNikolay Aleksandrov 			}
271438ef2d0SNikolay Aleksandrov 
272438ef2d0SNikolay Aleksandrov 			if (!lmqt_srcs)
273438ef2d0SNikolay Aleksandrov 				return NULL;
274438ef2d0SNikolay Aleksandrov 			igmp_hdr_size += lmqt_srcs * sizeof(__be32);
275438ef2d0SNikolay Aleksandrov 		}
276438ef2d0SNikolay Aleksandrov 	}
277438ef2d0SNikolay Aleksandrov 
278438ef2d0SNikolay Aleksandrov 	pkt_size = sizeof(*eth) + sizeof(*iph) + 4 + igmp_hdr_size;
279438ef2d0SNikolay Aleksandrov 	if ((p && pkt_size > p->dev->mtu) ||
280438ef2d0SNikolay Aleksandrov 	    pkt_size > br->dev->mtu)
281438ef2d0SNikolay Aleksandrov 		return NULL;
282438ef2d0SNikolay Aleksandrov 
283438ef2d0SNikolay Aleksandrov 	skb = netdev_alloc_skb_ip_align(br->dev, pkt_size);
284eb1d1641SHerbert Xu 	if (!skb)
285eb1d1641SHerbert Xu 		goto out;
286eb1d1641SHerbert Xu 
287eb1d1641SHerbert Xu 	skb->protocol = htons(ETH_P_IP);
288eb1d1641SHerbert Xu 
289eb1d1641SHerbert Xu 	skb_reset_mac_header(skb);
290eb1d1641SHerbert Xu 	eth = eth_hdr(skb);
291eb1d1641SHerbert Xu 
292e5a727f6SJoe Perches 	ether_addr_copy(eth->h_source, br->dev->dev_addr);
293438ef2d0SNikolay Aleksandrov 	ip_eth_mc_map(ip_dst, eth->h_dest);
294eb1d1641SHerbert Xu 	eth->h_proto = htons(ETH_P_IP);
295eb1d1641SHerbert Xu 	skb_put(skb, sizeof(*eth));
296eb1d1641SHerbert Xu 
297eb1d1641SHerbert Xu 	skb_set_network_header(skb, skb->len);
298eb1d1641SHerbert Xu 	iph = ip_hdr(skb);
299438ef2d0SNikolay Aleksandrov 	iph->tot_len = htons(pkt_size - sizeof(*eth));
300eb1d1641SHerbert Xu 
301eb1d1641SHerbert Xu 	iph->version = 4;
302eb1d1641SHerbert Xu 	iph->ihl = 6;
303eb1d1641SHerbert Xu 	iph->tos = 0xc0;
304eb1d1641SHerbert Xu 	iph->id = 0;
305eb1d1641SHerbert Xu 	iph->frag_off = htons(IP_DF);
306eb1d1641SHerbert Xu 	iph->ttl = 1;
307eb1d1641SHerbert Xu 	iph->protocol = IPPROTO_IGMP;
308675779adSNikolay Aleksandrov 	iph->saddr = br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR) ?
3091c8ad5bfSCong Wang 		     inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0;
310438ef2d0SNikolay Aleksandrov 	iph->daddr = ip_dst;
311eb1d1641SHerbert Xu 	((u8 *)&iph[1])[0] = IPOPT_RA;
312eb1d1641SHerbert Xu 	((u8 *)&iph[1])[1] = 4;
313eb1d1641SHerbert Xu 	((u8 *)&iph[1])[2] = 0;
314eb1d1641SHerbert Xu 	((u8 *)&iph[1])[3] = 0;
315eb1d1641SHerbert Xu 	ip_send_check(iph);
316eb1d1641SHerbert Xu 	skb_put(skb, 24);
317eb1d1641SHerbert Xu 
318eb1d1641SHerbert Xu 	skb_set_transport_header(skb, skb->len);
3191080ab95SNikolay Aleksandrov 	*igmp_type = IGMP_HOST_MEMBERSHIP_QUERY;
3205e923585SNikolay Aleksandrov 
3215e923585SNikolay Aleksandrov 	switch (br->multicast_igmp_version) {
3225e923585SNikolay Aleksandrov 	case 2:
3235e923585SNikolay Aleksandrov 		ih = igmp_hdr(skb);
324eb1d1641SHerbert Xu 		ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
325eb1d1641SHerbert Xu 		ih->code = (group ? br->multicast_last_member_interval :
326eb1d1641SHerbert Xu 				    br->multicast_query_response_interval) /
327eb1d1641SHerbert Xu 			   (HZ / IGMP_TIMER_SCALE);
328eb1d1641SHerbert Xu 		ih->group = group;
329eb1d1641SHerbert Xu 		ih->csum = 0;
330438ef2d0SNikolay Aleksandrov 		csum = &ih->csum;
331438ef2d0SNikolay Aleksandrov 		csum_start = (void *)ih;
3325e923585SNikolay Aleksandrov 		break;
3335e923585SNikolay Aleksandrov 	case 3:
3345e923585SNikolay Aleksandrov 		ihv3 = igmpv3_query_hdr(skb);
3355e923585SNikolay Aleksandrov 		ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY;
3365e923585SNikolay Aleksandrov 		ihv3->code = (group ? br->multicast_last_member_interval :
3375e923585SNikolay Aleksandrov 				      br->multicast_query_response_interval) /
3385e923585SNikolay Aleksandrov 			     (HZ / IGMP_TIMER_SCALE);
3395e923585SNikolay Aleksandrov 		ihv3->group = group;
3405e923585SNikolay Aleksandrov 		ihv3->qqic = br->multicast_query_interval / HZ;
341438ef2d0SNikolay Aleksandrov 		ihv3->nsrcs = htons(lmqt_srcs);
3425e923585SNikolay Aleksandrov 		ihv3->resv = 0;
343438ef2d0SNikolay Aleksandrov 		ihv3->suppress = sflag;
3445e923585SNikolay Aleksandrov 		ihv3->qrv = 2;
3455e923585SNikolay Aleksandrov 		ihv3->csum = 0;
346438ef2d0SNikolay Aleksandrov 		csum = &ihv3->csum;
347438ef2d0SNikolay Aleksandrov 		csum_start = (void *)ihv3;
348438ef2d0SNikolay Aleksandrov 		if (!pg || !with_srcs)
349438ef2d0SNikolay Aleksandrov 			break;
350438ef2d0SNikolay Aleksandrov 
351438ef2d0SNikolay Aleksandrov 		lmqt_srcs = 0;
352438ef2d0SNikolay Aleksandrov 		hlist_for_each_entry(ent, &pg->src_list, node) {
353438ef2d0SNikolay Aleksandrov 			if (over_lmqt == time_after(ent->timer.expires,
354438ef2d0SNikolay Aleksandrov 						    lmqt) &&
355438ef2d0SNikolay Aleksandrov 			    ent->src_query_rexmit_cnt > 0) {
356438ef2d0SNikolay Aleksandrov 				ihv3->srcs[lmqt_srcs++] = ent->addr.u.ip4;
357438ef2d0SNikolay Aleksandrov 				ent->src_query_rexmit_cnt--;
358*42c11ccfSNikolay Aleksandrov 				if (need_rexmit && ent->src_query_rexmit_cnt)
359*42c11ccfSNikolay Aleksandrov 					*need_rexmit = true;
360438ef2d0SNikolay Aleksandrov 			}
361438ef2d0SNikolay Aleksandrov 		}
362438ef2d0SNikolay Aleksandrov 		if (WARN_ON(lmqt_srcs != ntohs(ihv3->nsrcs))) {
363438ef2d0SNikolay Aleksandrov 			kfree_skb(skb);
364438ef2d0SNikolay Aleksandrov 			return NULL;
365438ef2d0SNikolay Aleksandrov 		}
3665e923585SNikolay Aleksandrov 		break;
3675e923585SNikolay Aleksandrov 	}
368eb1d1641SHerbert Xu 
369438ef2d0SNikolay Aleksandrov 	if (WARN_ON(!csum || !csum_start)) {
370438ef2d0SNikolay Aleksandrov 		kfree_skb(skb);
371438ef2d0SNikolay Aleksandrov 		return NULL;
372438ef2d0SNikolay Aleksandrov 	}
373438ef2d0SNikolay Aleksandrov 
374438ef2d0SNikolay Aleksandrov 	*csum = ip_compute_csum(csum_start, igmp_hdr_size);
3755e923585SNikolay Aleksandrov 	skb_put(skb, igmp_hdr_size);
376eb1d1641SHerbert Xu 	__skb_pull(skb, sizeof(*eth));
377eb1d1641SHerbert Xu 
378eb1d1641SHerbert Xu out:
379eb1d1641SHerbert Xu 	return skb;
380eb1d1641SHerbert Xu }
381eb1d1641SHerbert Xu 
382dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
38308b202b6SYOSHIFUJI Hideaki static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
384438ef2d0SNikolay Aleksandrov 						    struct net_bridge_port_group *pg,
385438ef2d0SNikolay Aleksandrov 						    const struct in6_addr *ip6_dst,
386438ef2d0SNikolay Aleksandrov 						    const struct in6_addr *group,
387438ef2d0SNikolay Aleksandrov 						    bool with_srcs, bool over_llqt,
388*42c11ccfSNikolay Aleksandrov 						    u8 sflag, u8 *igmp_type,
389*42c11ccfSNikolay Aleksandrov 						    bool *need_rexmit)
39008b202b6SYOSHIFUJI Hideaki {
391438ef2d0SNikolay Aleksandrov 	struct net_bridge_port *p = pg ? pg->port : NULL;
392438ef2d0SNikolay Aleksandrov 	struct net_bridge_group_src *ent;
393438ef2d0SNikolay Aleksandrov 	size_t pkt_size, mld_hdr_size;
394438ef2d0SNikolay Aleksandrov 	unsigned long now = jiffies;
395aa2ae3e7SNikolay Aleksandrov 	struct mld2_query *mld2q;
396438ef2d0SNikolay Aleksandrov 	void *csum_start = NULL;
397aa2ae3e7SNikolay Aleksandrov 	unsigned long interval;
398438ef2d0SNikolay Aleksandrov 	__sum16 *csum = NULL;
39908b202b6SYOSHIFUJI Hideaki 	struct ipv6hdr *ip6h;
40008b202b6SYOSHIFUJI Hideaki 	struct mld_msg *mldq;
401aa2ae3e7SNikolay Aleksandrov 	struct sk_buff *skb;
402438ef2d0SNikolay Aleksandrov 	unsigned long llqt;
40308b202b6SYOSHIFUJI Hideaki 	struct ethhdr *eth;
404438ef2d0SNikolay Aleksandrov 	u16 llqt_srcs = 0;
40508b202b6SYOSHIFUJI Hideaki 	u8 *hopopt;
40608b202b6SYOSHIFUJI Hideaki 
407aa2ae3e7SNikolay Aleksandrov 	mld_hdr_size = sizeof(*mldq);
408438ef2d0SNikolay Aleksandrov 	if (br->multicast_mld_version == 2) {
409aa2ae3e7SNikolay Aleksandrov 		mld_hdr_size = sizeof(*mld2q);
410438ef2d0SNikolay Aleksandrov 		if (pg && with_srcs) {
411438ef2d0SNikolay Aleksandrov 			llqt = now + (br->multicast_last_member_interval *
412438ef2d0SNikolay Aleksandrov 				      br->multicast_last_member_count);
413438ef2d0SNikolay Aleksandrov 			hlist_for_each_entry(ent, &pg->src_list, node) {
414438ef2d0SNikolay Aleksandrov 				if (over_llqt == time_after(ent->timer.expires,
415438ef2d0SNikolay Aleksandrov 							    llqt) &&
416438ef2d0SNikolay Aleksandrov 				    ent->src_query_rexmit_cnt > 0)
417438ef2d0SNikolay Aleksandrov 					llqt_srcs++;
418438ef2d0SNikolay Aleksandrov 			}
419438ef2d0SNikolay Aleksandrov 
420438ef2d0SNikolay Aleksandrov 			if (!llqt_srcs)
421438ef2d0SNikolay Aleksandrov 				return NULL;
422438ef2d0SNikolay Aleksandrov 			mld_hdr_size += llqt_srcs * sizeof(struct in6_addr);
423438ef2d0SNikolay Aleksandrov 		}
424438ef2d0SNikolay Aleksandrov 	}
425438ef2d0SNikolay Aleksandrov 
426438ef2d0SNikolay Aleksandrov 	pkt_size = sizeof(*eth) + sizeof(*ip6h) + 8 + mld_hdr_size;
427438ef2d0SNikolay Aleksandrov 	if ((p && pkt_size > p->dev->mtu) ||
428438ef2d0SNikolay Aleksandrov 	    pkt_size > br->dev->mtu)
429438ef2d0SNikolay Aleksandrov 		return NULL;
430438ef2d0SNikolay Aleksandrov 
431438ef2d0SNikolay Aleksandrov 	skb = netdev_alloc_skb_ip_align(br->dev, pkt_size);
43208b202b6SYOSHIFUJI Hideaki 	if (!skb)
43308b202b6SYOSHIFUJI Hideaki 		goto out;
43408b202b6SYOSHIFUJI Hideaki 
43508b202b6SYOSHIFUJI Hideaki 	skb->protocol = htons(ETH_P_IPV6);
43608b202b6SYOSHIFUJI Hideaki 
43708b202b6SYOSHIFUJI Hideaki 	/* Ethernet header */
43808b202b6SYOSHIFUJI Hideaki 	skb_reset_mac_header(skb);
43908b202b6SYOSHIFUJI Hideaki 	eth = eth_hdr(skb);
44008b202b6SYOSHIFUJI Hideaki 
441e5a727f6SJoe Perches 	ether_addr_copy(eth->h_source, br->dev->dev_addr);
44208b202b6SYOSHIFUJI Hideaki 	eth->h_proto = htons(ETH_P_IPV6);
44308b202b6SYOSHIFUJI Hideaki 	skb_put(skb, sizeof(*eth));
44408b202b6SYOSHIFUJI Hideaki 
44508b202b6SYOSHIFUJI Hideaki 	/* IPv6 header + HbH option */
44608b202b6SYOSHIFUJI Hideaki 	skb_set_network_header(skb, skb->len);
44708b202b6SYOSHIFUJI Hideaki 	ip6h = ipv6_hdr(skb);
44808b202b6SYOSHIFUJI Hideaki 
44908b202b6SYOSHIFUJI Hideaki 	*(__force __be32 *)ip6h = htonl(0x60000000);
450aa2ae3e7SNikolay Aleksandrov 	ip6h->payload_len = htons(8 + mld_hdr_size);
45108b202b6SYOSHIFUJI Hideaki 	ip6h->nexthdr = IPPROTO_HOPOPTS;
45208b202b6SYOSHIFUJI Hideaki 	ip6h->hop_limit = 1;
453438ef2d0SNikolay Aleksandrov 	ip6h->daddr = *ip6_dst;
454d1d81d4cSUlrich Weber 	if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
455d1d81d4cSUlrich Weber 			       &ip6h->saddr)) {
456d1d81d4cSUlrich Weber 		kfree_skb(skb);
457675779adSNikolay Aleksandrov 		br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, false);
458d1d81d4cSUlrich Weber 		return NULL;
459d1d81d4cSUlrich Weber 	}
4600888d5f3Sdaniel 
461675779adSNikolay Aleksandrov 	br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true);
46236cff5a1SLinus Lüssing 	ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
46308b202b6SYOSHIFUJI Hideaki 
46408b202b6SYOSHIFUJI Hideaki 	hopopt = (u8 *)(ip6h + 1);
46508b202b6SYOSHIFUJI Hideaki 	hopopt[0] = IPPROTO_ICMPV6;		/* next hdr */
46608b202b6SYOSHIFUJI Hideaki 	hopopt[1] = 0;				/* length of HbH */
46708b202b6SYOSHIFUJI Hideaki 	hopopt[2] = IPV6_TLV_ROUTERALERT;	/* Router Alert */
46808b202b6SYOSHIFUJI Hideaki 	hopopt[3] = 2;				/* Length of RA Option */
46908b202b6SYOSHIFUJI Hideaki 	hopopt[4] = 0;				/* Type = 0x0000 (MLD) */
47008b202b6SYOSHIFUJI Hideaki 	hopopt[5] = 0;
4711de5a71cSEldad Zack 	hopopt[6] = IPV6_TLV_PAD1;		/* Pad1 */
4721de5a71cSEldad Zack 	hopopt[7] = IPV6_TLV_PAD1;		/* Pad1 */
47308b202b6SYOSHIFUJI Hideaki 
47408b202b6SYOSHIFUJI Hideaki 	skb_put(skb, sizeof(*ip6h) + 8);
47508b202b6SYOSHIFUJI Hideaki 
47608b202b6SYOSHIFUJI Hideaki 	/* ICMPv6 */
47708b202b6SYOSHIFUJI Hideaki 	skb_set_transport_header(skb, skb->len);
478438ef2d0SNikolay Aleksandrov 	interval = ipv6_addr_any(group) ?
47932de868cSLinus Lüssing 			br->multicast_query_response_interval :
48032de868cSLinus Lüssing 			br->multicast_last_member_interval;
4811080ab95SNikolay Aleksandrov 	*igmp_type = ICMPV6_MGM_QUERY;
482aa2ae3e7SNikolay Aleksandrov 	switch (br->multicast_mld_version) {
483aa2ae3e7SNikolay Aleksandrov 	case 1:
484aa2ae3e7SNikolay Aleksandrov 		mldq = (struct mld_msg *)icmp6_hdr(skb);
48508b202b6SYOSHIFUJI Hideaki 		mldq->mld_type = ICMPV6_MGM_QUERY;
48608b202b6SYOSHIFUJI Hideaki 		mldq->mld_code = 0;
48708b202b6SYOSHIFUJI Hideaki 		mldq->mld_cksum = 0;
48808b202b6SYOSHIFUJI Hideaki 		mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
48908b202b6SYOSHIFUJI Hideaki 		mldq->mld_reserved = 0;
490438ef2d0SNikolay Aleksandrov 		mldq->mld_mca = *group;
491438ef2d0SNikolay Aleksandrov 		csum = &mldq->mld_cksum;
492438ef2d0SNikolay Aleksandrov 		csum_start = (void *)mldq;
493aa2ae3e7SNikolay Aleksandrov 		break;
494aa2ae3e7SNikolay Aleksandrov 	case 2:
495aa2ae3e7SNikolay Aleksandrov 		mld2q = (struct mld2_query *)icmp6_hdr(skb);
49653631a5fSLance Richardson 		mld2q->mld2q_mrc = htons((u16)jiffies_to_msecs(interval));
497aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_type = ICMPV6_MGM_QUERY;
498aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_code = 0;
499aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_cksum = 0;
500aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_resv1 = 0;
501aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_resv2 = 0;
502438ef2d0SNikolay Aleksandrov 		mld2q->mld2q_suppress = sflag;
503aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_qrv = 2;
504438ef2d0SNikolay Aleksandrov 		mld2q->mld2q_nsrcs = htons(llqt_srcs);
505aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_qqic = br->multicast_query_interval / HZ;
506438ef2d0SNikolay Aleksandrov 		mld2q->mld2q_mca = *group;
507438ef2d0SNikolay Aleksandrov 		csum = &mld2q->mld2q_cksum;
508438ef2d0SNikolay Aleksandrov 		csum_start = (void *)mld2q;
509438ef2d0SNikolay Aleksandrov 		if (!pg || !with_srcs)
510438ef2d0SNikolay Aleksandrov 			break;
511438ef2d0SNikolay Aleksandrov 
512438ef2d0SNikolay Aleksandrov 		llqt_srcs = 0;
513438ef2d0SNikolay Aleksandrov 		hlist_for_each_entry(ent, &pg->src_list, node) {
514438ef2d0SNikolay Aleksandrov 			if (over_llqt == time_after(ent->timer.expires,
515438ef2d0SNikolay Aleksandrov 						    llqt) &&
516438ef2d0SNikolay Aleksandrov 			    ent->src_query_rexmit_cnt > 0) {
517438ef2d0SNikolay Aleksandrov 				mld2q->mld2q_srcs[llqt_srcs++] = ent->addr.u.ip6;
518438ef2d0SNikolay Aleksandrov 				ent->src_query_rexmit_cnt--;
519*42c11ccfSNikolay Aleksandrov 				if (need_rexmit && ent->src_query_rexmit_cnt)
520*42c11ccfSNikolay Aleksandrov 					*need_rexmit = true;
521438ef2d0SNikolay Aleksandrov 			}
522438ef2d0SNikolay Aleksandrov 		}
523438ef2d0SNikolay Aleksandrov 		if (WARN_ON(llqt_srcs != ntohs(mld2q->mld2q_nsrcs))) {
524438ef2d0SNikolay Aleksandrov 			kfree_skb(skb);
525438ef2d0SNikolay Aleksandrov 			return NULL;
526438ef2d0SNikolay Aleksandrov 		}
527aa2ae3e7SNikolay Aleksandrov 		break;
528aa2ae3e7SNikolay Aleksandrov 	}
52908b202b6SYOSHIFUJI Hideaki 
530438ef2d0SNikolay Aleksandrov 	if (WARN_ON(!csum || !csum_start)) {
531438ef2d0SNikolay Aleksandrov 		kfree_skb(skb);
532438ef2d0SNikolay Aleksandrov 		return NULL;
533438ef2d0SNikolay Aleksandrov 	}
534438ef2d0SNikolay Aleksandrov 
535438ef2d0SNikolay Aleksandrov 	*csum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, mld_hdr_size,
536438ef2d0SNikolay Aleksandrov 				IPPROTO_ICMPV6,
537438ef2d0SNikolay Aleksandrov 				csum_partial(csum_start, mld_hdr_size, 0));
538438ef2d0SNikolay Aleksandrov 	skb_put(skb, mld_hdr_size);
53908b202b6SYOSHIFUJI Hideaki 	__skb_pull(skb, sizeof(*eth));
54008b202b6SYOSHIFUJI Hideaki 
54108b202b6SYOSHIFUJI Hideaki out:
54208b202b6SYOSHIFUJI Hideaki 	return skb;
54308b202b6SYOSHIFUJI Hideaki }
54408b202b6SYOSHIFUJI Hideaki #endif
54508b202b6SYOSHIFUJI Hideaki 
5468ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
547438ef2d0SNikolay Aleksandrov 						struct net_bridge_port_group *pg,
548438ef2d0SNikolay Aleksandrov 						struct br_ip *ip_dst,
549438ef2d0SNikolay Aleksandrov 						struct br_ip *group,
550438ef2d0SNikolay Aleksandrov 						bool with_srcs, bool over_lmqt,
551*42c11ccfSNikolay Aleksandrov 						u8 sflag, u8 *igmp_type,
552*42c11ccfSNikolay Aleksandrov 						bool *need_rexmit)
5538ef2a9a5SYOSHIFUJI Hideaki {
554438ef2d0SNikolay Aleksandrov 	__be32 ip4_dst;
555438ef2d0SNikolay Aleksandrov 
556438ef2d0SNikolay Aleksandrov 	switch (group->proto) {
5578ef2a9a5SYOSHIFUJI Hideaki 	case htons(ETH_P_IP):
558438ef2d0SNikolay Aleksandrov 		ip4_dst = ip_dst ? ip_dst->u.ip4 : htonl(INADDR_ALLHOSTS_GROUP);
559438ef2d0SNikolay Aleksandrov 		return br_ip4_multicast_alloc_query(br, pg,
560438ef2d0SNikolay Aleksandrov 						    ip4_dst, group->u.ip4,
561438ef2d0SNikolay Aleksandrov 						    with_srcs, over_lmqt,
562*42c11ccfSNikolay Aleksandrov 						    sflag, igmp_type,
563*42c11ccfSNikolay Aleksandrov 						    need_rexmit);
564dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
565438ef2d0SNikolay Aleksandrov 	case htons(ETH_P_IPV6): {
566438ef2d0SNikolay Aleksandrov 		struct in6_addr ip6_dst;
567438ef2d0SNikolay Aleksandrov 
568438ef2d0SNikolay Aleksandrov 		if (ip_dst)
569438ef2d0SNikolay Aleksandrov 			ip6_dst = ip_dst->u.ip6;
570438ef2d0SNikolay Aleksandrov 		else
571438ef2d0SNikolay Aleksandrov 			ipv6_addr_set(&ip6_dst, htonl(0xff020000), 0, 0,
572438ef2d0SNikolay Aleksandrov 				      htonl(1));
573438ef2d0SNikolay Aleksandrov 
574438ef2d0SNikolay Aleksandrov 		return br_ip6_multicast_alloc_query(br, pg,
575438ef2d0SNikolay Aleksandrov 						    &ip6_dst, &group->u.ip6,
576438ef2d0SNikolay Aleksandrov 						    with_srcs, over_lmqt,
577*42c11ccfSNikolay Aleksandrov 						    sflag, igmp_type,
578*42c11ccfSNikolay Aleksandrov 						    need_rexmit);
579438ef2d0SNikolay Aleksandrov 	}
58008b202b6SYOSHIFUJI Hideaki #endif
5818ef2a9a5SYOSHIFUJI Hideaki 	}
5828ef2a9a5SYOSHIFUJI Hideaki 	return NULL;
5838ef2a9a5SYOSHIFUJI Hideaki }
5848ef2a9a5SYOSHIFUJI Hideaki 
585cfd56754SCong Wang struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
5865e923585SNikolay Aleksandrov 						    struct br_ip *group)
587eb1d1641SHerbert Xu {
588eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
5894c0833bcSTobias Klauser 	int err;
590eb1d1641SHerbert Xu 
59119e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip_get(br, group);
59219e3a9c9SNikolay Aleksandrov 	if (mp)
59319e3a9c9SNikolay Aleksandrov 		return mp;
594eb1d1641SHerbert Xu 
59519e3a9c9SNikolay Aleksandrov 	if (atomic_read(&br->mdb_hash_tbl.nelems) >= br->hash_max) {
59619e3a9c9SNikolay Aleksandrov 		br_opt_toggle(br, BROPT_MULTICAST_ENABLED, false);
59719e3a9c9SNikolay Aleksandrov 		return ERR_PTR(-E2BIG);
598eb1d1641SHerbert Xu 	}
599eb1d1641SHerbert Xu 
600eb1d1641SHerbert Xu 	mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
601eb1d1641SHerbert Xu 	if (unlikely(!mp))
6024c0833bcSTobias Klauser 		return ERR_PTR(-ENOMEM);
603eb1d1641SHerbert Xu 
604eb1d1641SHerbert Xu 	mp->br = br;
6058ef2a9a5SYOSHIFUJI Hideaki 	mp->addr = *group;
60688c1f37fSAllen Pais 	timer_setup(&mp->timer, br_multicast_group_expired, 0);
60719e3a9c9SNikolay Aleksandrov 	err = rhashtable_lookup_insert_fast(&br->mdb_hash_tbl, &mp->rhnode,
60819e3a9c9SNikolay Aleksandrov 					    br_mdb_rht_params);
60919e3a9c9SNikolay Aleksandrov 	if (err) {
61019e3a9c9SNikolay Aleksandrov 		kfree(mp);
61119e3a9c9SNikolay Aleksandrov 		mp = ERR_PTR(err);
61219e3a9c9SNikolay Aleksandrov 	} else {
61319e3a9c9SNikolay Aleksandrov 		hlist_add_head_rcu(&mp->mdb_node, &br->mdb_list);
61419e3a9c9SNikolay Aleksandrov 	}
6151faabf2aSEric Dumazet 
616eb1d1641SHerbert Xu 	return mp;
617eb1d1641SHerbert Xu }
618eb1d1641SHerbert Xu 
6198b671779SNikolay Aleksandrov static void br_multicast_group_src_expired(struct timer_list *t)
6208b671779SNikolay Aleksandrov {
6218b671779SNikolay Aleksandrov 	struct net_bridge_group_src *src = from_timer(src, t, timer);
6228b671779SNikolay Aleksandrov 	struct net_bridge_port_group *pg;
6238b671779SNikolay Aleksandrov 	struct net_bridge *br = src->br;
6248b671779SNikolay Aleksandrov 
6258b671779SNikolay Aleksandrov 	spin_lock(&br->multicast_lock);
6268b671779SNikolay Aleksandrov 	if (hlist_unhashed(&src->node) || !netif_running(br->dev) ||
6278b671779SNikolay Aleksandrov 	    timer_pending(&src->timer))
6288b671779SNikolay Aleksandrov 		goto out;
6298b671779SNikolay Aleksandrov 
6308b671779SNikolay Aleksandrov 	pg = src->pg;
6318b671779SNikolay Aleksandrov 	if (pg->filter_mode == MCAST_INCLUDE) {
6328b671779SNikolay Aleksandrov 		br_multicast_del_group_src(src);
6338b671779SNikolay Aleksandrov 		if (!hlist_empty(&pg->src_list))
6348b671779SNikolay Aleksandrov 			goto out;
6358b671779SNikolay Aleksandrov 		br_multicast_find_del_pg(br, pg);
6368b671779SNikolay Aleksandrov 	}
6378b671779SNikolay Aleksandrov out:
6388b671779SNikolay Aleksandrov 	spin_unlock(&br->multicast_lock);
6398b671779SNikolay Aleksandrov }
6408b671779SNikolay Aleksandrov 
6418b671779SNikolay Aleksandrov static struct net_bridge_group_src *
6428b671779SNikolay Aleksandrov br_multicast_find_group_src(struct net_bridge_port_group *pg, struct br_ip *ip)
6438b671779SNikolay Aleksandrov {
6448b671779SNikolay Aleksandrov 	struct net_bridge_group_src *ent;
6458b671779SNikolay Aleksandrov 
6468b671779SNikolay Aleksandrov 	switch (ip->proto) {
6478b671779SNikolay Aleksandrov 	case htons(ETH_P_IP):
6488b671779SNikolay Aleksandrov 		hlist_for_each_entry(ent, &pg->src_list, node)
6498b671779SNikolay Aleksandrov 			if (ip->u.ip4 == ent->addr.u.ip4)
6508b671779SNikolay Aleksandrov 				return ent;
6518b671779SNikolay Aleksandrov 		break;
6528b671779SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
6538b671779SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
6548b671779SNikolay Aleksandrov 		hlist_for_each_entry(ent, &pg->src_list, node)
6558b671779SNikolay Aleksandrov 			if (!ipv6_addr_cmp(&ent->addr.u.ip6, &ip->u.ip6))
6568b671779SNikolay Aleksandrov 				return ent;
6578b671779SNikolay Aleksandrov 		break;
6588b671779SNikolay Aleksandrov #endif
6598b671779SNikolay Aleksandrov 	}
6608b671779SNikolay Aleksandrov 
6618b671779SNikolay Aleksandrov 	return NULL;
6628b671779SNikolay Aleksandrov }
6638b671779SNikolay Aleksandrov 
6648b671779SNikolay Aleksandrov static struct net_bridge_group_src *
6658b671779SNikolay Aleksandrov br_multicast_new_group_src(struct net_bridge_port_group *pg, struct br_ip *src_ip)
6668b671779SNikolay Aleksandrov {
6678b671779SNikolay Aleksandrov 	struct net_bridge_group_src *grp_src;
6688b671779SNikolay Aleksandrov 
6698b671779SNikolay Aleksandrov 	if (unlikely(pg->src_ents >= PG_SRC_ENT_LIMIT))
6708b671779SNikolay Aleksandrov 		return NULL;
6718b671779SNikolay Aleksandrov 
6728b671779SNikolay Aleksandrov 	switch (src_ip->proto) {
6738b671779SNikolay Aleksandrov 	case htons(ETH_P_IP):
6748b671779SNikolay Aleksandrov 		if (ipv4_is_zeronet(src_ip->u.ip4) ||
6758b671779SNikolay Aleksandrov 		    ipv4_is_multicast(src_ip->u.ip4))
6768b671779SNikolay Aleksandrov 			return NULL;
6778b671779SNikolay Aleksandrov 		break;
6788b671779SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
6798b671779SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
6808b671779SNikolay Aleksandrov 		if (ipv6_addr_any(&src_ip->u.ip6) ||
6818b671779SNikolay Aleksandrov 		    ipv6_addr_is_multicast(&src_ip->u.ip6))
6828b671779SNikolay Aleksandrov 			return NULL;
6838b671779SNikolay Aleksandrov 		break;
6848b671779SNikolay Aleksandrov #endif
6858b671779SNikolay Aleksandrov 	}
6868b671779SNikolay Aleksandrov 
6878b671779SNikolay Aleksandrov 	grp_src = kzalloc(sizeof(*grp_src), GFP_ATOMIC);
6888b671779SNikolay Aleksandrov 	if (unlikely(!grp_src))
6898b671779SNikolay Aleksandrov 		return NULL;
6908b671779SNikolay Aleksandrov 
6918b671779SNikolay Aleksandrov 	grp_src->pg = pg;
6928b671779SNikolay Aleksandrov 	grp_src->br = pg->port->br;
6938b671779SNikolay Aleksandrov 	grp_src->addr = *src_ip;
6948b671779SNikolay Aleksandrov 	timer_setup(&grp_src->timer, br_multicast_group_src_expired, 0);
6958b671779SNikolay Aleksandrov 
6968b671779SNikolay Aleksandrov 	hlist_add_head_rcu(&grp_src->node, &pg->src_list);
6978b671779SNikolay Aleksandrov 	pg->src_ents++;
6988b671779SNikolay Aleksandrov 
6998b671779SNikolay Aleksandrov 	return grp_src;
7008b671779SNikolay Aleksandrov }
7018b671779SNikolay Aleksandrov 
702cfd56754SCong Wang struct net_bridge_port_group *br_multicast_new_port_group(
703cfd56754SCong Wang 			struct net_bridge_port *port,
704cfd56754SCong Wang 			struct br_ip *group,
705ccb1c31aSAmerigo Wang 			struct net_bridge_port_group __rcu *next,
7066db6f0eaSFelix Fietkau 			unsigned char flags,
7078b671779SNikolay Aleksandrov 			const unsigned char *src,
7088b671779SNikolay Aleksandrov 			u8 filter_mode)
709cfd56754SCong Wang {
710cfd56754SCong Wang 	struct net_bridge_port_group *p;
711cfd56754SCong Wang 
712cfd56754SCong Wang 	p = kzalloc(sizeof(*p), GFP_ATOMIC);
713cfd56754SCong Wang 	if (unlikely(!p))
714cfd56754SCong Wang 		return NULL;
715cfd56754SCong Wang 
716cfd56754SCong Wang 	p->addr = *group;
717cfd56754SCong Wang 	p->port = port;
7189d06b6d8SElad Raz 	p->flags = flags;
7198b671779SNikolay Aleksandrov 	p->filter_mode = filter_mode;
7208b671779SNikolay Aleksandrov 	INIT_HLIST_HEAD(&p->src_list);
721eca2a43bSstephen hemminger 	rcu_assign_pointer(p->next, next);
72288c1f37fSAllen Pais 	timer_setup(&p->timer, br_multicast_port_group_expired, 0);
723*42c11ccfSNikolay Aleksandrov 	timer_setup(&p->rexmit_timer, br_multicast_port_group_rexmit, 0);
724*42c11ccfSNikolay Aleksandrov 	hlist_add_head(&p->mglist, &port->mglist);
7256db6f0eaSFelix Fietkau 
7266db6f0eaSFelix Fietkau 	if (src)
7276db6f0eaSFelix Fietkau 		memcpy(p->eth_addr, src, ETH_ALEN);
7286db6f0eaSFelix Fietkau 	else
7291bfe45f4SMao Wenan 		eth_broadcast_addr(p->eth_addr);
7306db6f0eaSFelix Fietkau 
731cfd56754SCong Wang 	return p;
732cfd56754SCong Wang }
733cfd56754SCong Wang 
7346db6f0eaSFelix Fietkau static bool br_port_group_equal(struct net_bridge_port_group *p,
7356db6f0eaSFelix Fietkau 				struct net_bridge_port *port,
7366db6f0eaSFelix Fietkau 				const unsigned char *src)
7376db6f0eaSFelix Fietkau {
7386db6f0eaSFelix Fietkau 	if (p->port != port)
7396db6f0eaSFelix Fietkau 		return false;
7406db6f0eaSFelix Fietkau 
7416db6f0eaSFelix Fietkau 	if (!(port->flags & BR_MULTICAST_TO_UNICAST))
7426db6f0eaSFelix Fietkau 		return true;
7436db6f0eaSFelix Fietkau 
7446db6f0eaSFelix Fietkau 	return ether_addr_equal(src, p->eth_addr);
7456db6f0eaSFelix Fietkau }
7466db6f0eaSFelix Fietkau 
7471bc844eeSNikolay Aleksandrov void br_multicast_host_join(struct net_bridge_mdb_entry *mp, bool notify)
7481bc844eeSNikolay Aleksandrov {
7491bc844eeSNikolay Aleksandrov 	if (!mp->host_joined) {
7501bc844eeSNikolay Aleksandrov 		mp->host_joined = true;
7511bc844eeSNikolay Aleksandrov 		if (notify)
7521bc844eeSNikolay Aleksandrov 			br_mdb_notify(mp->br->dev, NULL, &mp->addr,
7531bc844eeSNikolay Aleksandrov 				      RTM_NEWMDB, 0);
7541bc844eeSNikolay Aleksandrov 	}
7551bc844eeSNikolay Aleksandrov 	mod_timer(&mp->timer, jiffies + mp->br->multicast_membership_interval);
7561bc844eeSNikolay Aleksandrov }
7571bc844eeSNikolay Aleksandrov 
7581bc844eeSNikolay Aleksandrov void br_multicast_host_leave(struct net_bridge_mdb_entry *mp, bool notify)
7591bc844eeSNikolay Aleksandrov {
7601bc844eeSNikolay Aleksandrov 	if (!mp->host_joined)
7611bc844eeSNikolay Aleksandrov 		return;
7621bc844eeSNikolay Aleksandrov 
7631bc844eeSNikolay Aleksandrov 	mp->host_joined = false;
7641bc844eeSNikolay Aleksandrov 	if (notify)
7651bc844eeSNikolay Aleksandrov 		br_mdb_notify(mp->br->dev, NULL, &mp->addr, RTM_DELMDB, 0);
7661bc844eeSNikolay Aleksandrov }
7671bc844eeSNikolay Aleksandrov 
768eb1d1641SHerbert Xu static int br_multicast_add_group(struct net_bridge *br,
7698ef2a9a5SYOSHIFUJI Hideaki 				  struct net_bridge_port *port,
7706db6f0eaSFelix Fietkau 				  struct br_ip *group,
7718b671779SNikolay Aleksandrov 				  const unsigned char *src,
7728b671779SNikolay Aleksandrov 				  u8 filter_mode)
773eb1d1641SHerbert Xu {
774e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
7755e923585SNikolay Aleksandrov 	struct net_bridge_port_group *p;
7765e923585SNikolay Aleksandrov 	struct net_bridge_mdb_entry *mp;
777454594f3SLinus Lüssing 	unsigned long now = jiffies;
778eb1d1641SHerbert Xu 	int err;
779eb1d1641SHerbert Xu 
780eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
781eb1d1641SHerbert Xu 	if (!netif_running(br->dev) ||
782eb1d1641SHerbert Xu 	    (port && port->state == BR_STATE_DISABLED))
783eb1d1641SHerbert Xu 		goto out;
784eb1d1641SHerbert Xu 
78519e3a9c9SNikolay Aleksandrov 	mp = br_multicast_new_group(br, group);
786eb1d1641SHerbert Xu 	err = PTR_ERR(mp);
7874c0833bcSTobias Klauser 	if (IS_ERR(mp))
788eb1d1641SHerbert Xu 		goto err;
789eb1d1641SHerbert Xu 
790eb1d1641SHerbert Xu 	if (!port) {
7911bc844eeSNikolay Aleksandrov 		br_multicast_host_join(mp, true);
792eb1d1641SHerbert Xu 		goto out;
793eb1d1641SHerbert Xu 	}
794eb1d1641SHerbert Xu 
795e8051688SEric Dumazet 	for (pp = &mp->ports;
796e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
797e8051688SEric Dumazet 	     pp = &p->next) {
7986db6f0eaSFelix Fietkau 		if (br_port_group_equal(p, port, src))
799454594f3SLinus Lüssing 			goto found;
800eb1d1641SHerbert Xu 		if ((unsigned long)p->port < (unsigned long)port)
801eb1d1641SHerbert Xu 			break;
802eb1d1641SHerbert Xu 	}
803eb1d1641SHerbert Xu 
8048b671779SNikolay Aleksandrov 	p = br_multicast_new_port_group(port, group, *pp, 0, src, filter_mode);
805eb1d1641SHerbert Xu 	if (unlikely(!p))
806eb1d1641SHerbert Xu 		goto err;
807eb1d1641SHerbert Xu 	rcu_assign_pointer(*pp, p);
80845ebcce5SElad Raz 	br_mdb_notify(br->dev, port, group, RTM_NEWMDB, 0);
809eb1d1641SHerbert Xu 
810454594f3SLinus Lüssing found:
811454594f3SLinus Lüssing 	mod_timer(&p->timer, now + br->multicast_membership_interval);
812eb1d1641SHerbert Xu out:
813eb1d1641SHerbert Xu 	err = 0;
814eb1d1641SHerbert Xu 
815eb1d1641SHerbert Xu err:
816eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
817eb1d1641SHerbert Xu 	return err;
818eb1d1641SHerbert Xu }
819eb1d1641SHerbert Xu 
8208ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_add_group(struct net_bridge *br,
8218ef2a9a5SYOSHIFUJI Hideaki 				      struct net_bridge_port *port,
822b0e9a30dSVlad Yasevich 				      __be32 group,
8236db6f0eaSFelix Fietkau 				      __u16 vid,
8248b671779SNikolay Aleksandrov 				      const unsigned char *src,
8258b671779SNikolay Aleksandrov 				      bool igmpv2)
8268ef2a9a5SYOSHIFUJI Hideaki {
8278ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip br_group;
8288b671779SNikolay Aleksandrov 	u8 filter_mode;
8298ef2a9a5SYOSHIFUJI Hideaki 
8308ef2a9a5SYOSHIFUJI Hideaki 	if (ipv4_is_local_multicast(group))
8318ef2a9a5SYOSHIFUJI Hideaki 		return 0;
8328ef2a9a5SYOSHIFUJI Hideaki 
8331515a63fSNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
8348ef2a9a5SYOSHIFUJI Hideaki 	br_group.u.ip4 = group;
8358ef2a9a5SYOSHIFUJI Hideaki 	br_group.proto = htons(ETH_P_IP);
836b0e9a30dSVlad Yasevich 	br_group.vid = vid;
8378b671779SNikolay Aleksandrov 	filter_mode = igmpv2 ? MCAST_EXCLUDE : MCAST_INCLUDE;
8388ef2a9a5SYOSHIFUJI Hideaki 
8398b671779SNikolay Aleksandrov 	return br_multicast_add_group(br, port, &br_group, src, filter_mode);
8408ef2a9a5SYOSHIFUJI Hideaki }
8418ef2a9a5SYOSHIFUJI Hideaki 
842dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
84308b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_add_group(struct net_bridge *br,
84408b202b6SYOSHIFUJI Hideaki 				      struct net_bridge_port *port,
845b0e9a30dSVlad Yasevich 				      const struct in6_addr *group,
8466db6f0eaSFelix Fietkau 				      __u16 vid,
8478b671779SNikolay Aleksandrov 				      const unsigned char *src,
8488b671779SNikolay Aleksandrov 				      bool mldv1)
84908b202b6SYOSHIFUJI Hideaki {
85008b202b6SYOSHIFUJI Hideaki 	struct br_ip br_group;
8518b671779SNikolay Aleksandrov 	u8 filter_mode;
85208b202b6SYOSHIFUJI Hideaki 
8533c3769e6SLinus Lüssing 	if (ipv6_addr_is_ll_all_nodes(group))
85408b202b6SYOSHIFUJI Hideaki 		return 0;
85508b202b6SYOSHIFUJI Hideaki 
85619e3a9c9SNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
8574e3fd7a0SAlexey Dobriyan 	br_group.u.ip6 = *group;
8589cc6e0c4SLinus Lüssing 	br_group.proto = htons(ETH_P_IPV6);
859b0e9a30dSVlad Yasevich 	br_group.vid = vid;
8608b671779SNikolay Aleksandrov 	filter_mode = mldv1 ? MCAST_EXCLUDE : MCAST_INCLUDE;
86108b202b6SYOSHIFUJI Hideaki 
8628b671779SNikolay Aleksandrov 	return br_multicast_add_group(br, port, &br_group, src, filter_mode);
86308b202b6SYOSHIFUJI Hideaki }
86408b202b6SYOSHIFUJI Hideaki #endif
86508b202b6SYOSHIFUJI Hideaki 
86688c1f37fSAllen Pais static void br_multicast_router_expired(struct timer_list *t)
867eb1d1641SHerbert Xu {
86888c1f37fSAllen Pais 	struct net_bridge_port *port =
86988c1f37fSAllen Pais 			from_timer(port, t, multicast_router_timer);
870eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
871eb1d1641SHerbert Xu 
872eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
873a55d8246SNikolay Aleksandrov 	if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
874a55d8246SNikolay Aleksandrov 	    port->multicast_router == MDB_RTR_TYPE_PERM ||
875f12e7d95SNogah Frankel 	    timer_pending(&port->multicast_router_timer))
876eb1d1641SHerbert Xu 		goto out;
877eb1d1641SHerbert Xu 
878f12e7d95SNogah Frankel 	__del_port_router(port);
879eb1d1641SHerbert Xu out:
880eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
881eb1d1641SHerbert Xu }
882eb1d1641SHerbert Xu 
88377041420SYotam Gigi static void br_mc_router_state_change(struct net_bridge *p,
88477041420SYotam Gigi 				      bool is_mc_router)
88577041420SYotam Gigi {
88677041420SYotam Gigi 	struct switchdev_attr attr = {
88777041420SYotam Gigi 		.orig_dev = p->dev,
88877041420SYotam Gigi 		.id = SWITCHDEV_ATTR_ID_BRIDGE_MROUTER,
88977041420SYotam Gigi 		.flags = SWITCHDEV_F_DEFER,
89077041420SYotam Gigi 		.u.mrouter = is_mc_router,
89177041420SYotam Gigi 	};
89277041420SYotam Gigi 
89377041420SYotam Gigi 	switchdev_port_attr_set(p->dev, &attr);
89477041420SYotam Gigi }
89577041420SYotam Gigi 
89688c1f37fSAllen Pais static void br_multicast_local_router_expired(struct timer_list *t)
897eb1d1641SHerbert Xu {
89888c1f37fSAllen Pais 	struct net_bridge *br = from_timer(br, t, multicast_router_timer);
89977041420SYotam Gigi 
90077041420SYotam Gigi 	spin_lock(&br->multicast_lock);
90177041420SYotam Gigi 	if (br->multicast_router == MDB_RTR_TYPE_DISABLED ||
90277041420SYotam Gigi 	    br->multicast_router == MDB_RTR_TYPE_PERM ||
90377041420SYotam Gigi 	    timer_pending(&br->multicast_router_timer))
90477041420SYotam Gigi 		goto out;
90577041420SYotam Gigi 
90677041420SYotam Gigi 	br_mc_router_state_change(br, false);
90777041420SYotam Gigi out:
90877041420SYotam Gigi 	spin_unlock(&br->multicast_lock);
909eb1d1641SHerbert Xu }
910eb1d1641SHerbert Xu 
911cc0fdd80SLinus Lüssing static void br_multicast_querier_expired(struct net_bridge *br,
91290010b36SLinus Lüssing 					 struct bridge_mcast_own_query *query)
913c83b8fabSHerbert Xu {
914c83b8fabSHerbert Xu 	spin_lock(&br->multicast_lock);
91513cefad2SNikolay Aleksandrov 	if (!netif_running(br->dev) || !br_opt_get(br, BROPT_MULTICAST_ENABLED))
916c83b8fabSHerbert Xu 		goto out;
917c83b8fabSHerbert Xu 
918cc0fdd80SLinus Lüssing 	br_multicast_start_querier(br, query);
919c83b8fabSHerbert Xu 
920c83b8fabSHerbert Xu out:
921c83b8fabSHerbert Xu 	spin_unlock(&br->multicast_lock);
922c83b8fabSHerbert Xu }
923c83b8fabSHerbert Xu 
92488c1f37fSAllen Pais static void br_ip4_multicast_querier_expired(struct timer_list *t)
925cc0fdd80SLinus Lüssing {
92688c1f37fSAllen Pais 	struct net_bridge *br = from_timer(br, t, ip4_other_query.timer);
927cc0fdd80SLinus Lüssing 
92890010b36SLinus Lüssing 	br_multicast_querier_expired(br, &br->ip4_own_query);
929cc0fdd80SLinus Lüssing }
930cc0fdd80SLinus Lüssing 
931cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
93288c1f37fSAllen Pais static void br_ip6_multicast_querier_expired(struct timer_list *t)
933cc0fdd80SLinus Lüssing {
93488c1f37fSAllen Pais 	struct net_bridge *br = from_timer(br, t, ip6_other_query.timer);
935cc0fdd80SLinus Lüssing 
93690010b36SLinus Lüssing 	br_multicast_querier_expired(br, &br->ip6_own_query);
937cc0fdd80SLinus Lüssing }
938cc0fdd80SLinus Lüssing #endif
939cc0fdd80SLinus Lüssing 
940dc4eb53aSLinus Lüssing static void br_multicast_select_own_querier(struct net_bridge *br,
941dc4eb53aSLinus Lüssing 					    struct br_ip *ip,
942dc4eb53aSLinus Lüssing 					    struct sk_buff *skb)
943dc4eb53aSLinus Lüssing {
944dc4eb53aSLinus Lüssing 	if (ip->proto == htons(ETH_P_IP))
945dc4eb53aSLinus Lüssing 		br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr;
946dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
947dc4eb53aSLinus Lüssing 	else
948dc4eb53aSLinus Lüssing 		br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr;
949dc4eb53aSLinus Lüssing #endif
950dc4eb53aSLinus Lüssing }
951dc4eb53aSLinus Lüssing 
9528ef2a9a5SYOSHIFUJI Hideaki static void __br_multicast_send_query(struct net_bridge *br,
9538ef2a9a5SYOSHIFUJI Hideaki 				      struct net_bridge_port *port,
954438ef2d0SNikolay Aleksandrov 				      struct net_bridge_port_group *pg,
955438ef2d0SNikolay Aleksandrov 				      struct br_ip *ip_dst,
956438ef2d0SNikolay Aleksandrov 				      struct br_ip *group,
957438ef2d0SNikolay Aleksandrov 				      bool with_srcs,
958*42c11ccfSNikolay Aleksandrov 				      u8 sflag,
959*42c11ccfSNikolay Aleksandrov 				      bool *need_rexmit)
960eb1d1641SHerbert Xu {
961438ef2d0SNikolay Aleksandrov 	bool over_lmqt = !!sflag;
962eb1d1641SHerbert Xu 	struct sk_buff *skb;
9631080ab95SNikolay Aleksandrov 	u8 igmp_type;
964eb1d1641SHerbert Xu 
965438ef2d0SNikolay Aleksandrov again_under_lmqt:
966438ef2d0SNikolay Aleksandrov 	skb = br_multicast_alloc_query(br, pg, ip_dst, group, with_srcs,
967*42c11ccfSNikolay Aleksandrov 				       over_lmqt, sflag, &igmp_type,
968*42c11ccfSNikolay Aleksandrov 				       need_rexmit);
969eb1d1641SHerbert Xu 	if (!skb)
9708ef2a9a5SYOSHIFUJI Hideaki 		return;
971eb1d1641SHerbert Xu 
972eb1d1641SHerbert Xu 	if (port) {
973eb1d1641SHerbert Xu 		skb->dev = port->dev;
974a65056ecSNikolay Aleksandrov 		br_multicast_count(br, port, skb, igmp_type,
9751080ab95SNikolay Aleksandrov 				   BR_MCAST_DIR_TX);
97629a26a56SEric W. Biederman 		NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
97729a26a56SEric W. Biederman 			dev_net(port->dev), NULL, skb, NULL, skb->dev,
978f0b4eeceSLinus Lüssing 			br_dev_queue_push_xmit);
979438ef2d0SNikolay Aleksandrov 
980438ef2d0SNikolay Aleksandrov 		if (over_lmqt && with_srcs && sflag) {
981438ef2d0SNikolay Aleksandrov 			over_lmqt = false;
982438ef2d0SNikolay Aleksandrov 			goto again_under_lmqt;
983438ef2d0SNikolay Aleksandrov 		}
984dc4eb53aSLinus Lüssing 	} else {
985438ef2d0SNikolay Aleksandrov 		br_multicast_select_own_querier(br, group, skb);
986a65056ecSNikolay Aleksandrov 		br_multicast_count(br, port, skb, igmp_type,
9871080ab95SNikolay Aleksandrov 				   BR_MCAST_DIR_RX);
988eb1d1641SHerbert Xu 		netif_rx(skb);
9898ef2a9a5SYOSHIFUJI Hideaki 	}
990dc4eb53aSLinus Lüssing }
991eb1d1641SHerbert Xu 
9928ef2a9a5SYOSHIFUJI Hideaki static void br_multicast_send_query(struct net_bridge *br,
993cc0fdd80SLinus Lüssing 				    struct net_bridge_port *port,
99490010b36SLinus Lüssing 				    struct bridge_mcast_own_query *own_query)
9958ef2a9a5SYOSHIFUJI Hideaki {
99690010b36SLinus Lüssing 	struct bridge_mcast_other_query *other_query = NULL;
9975e923585SNikolay Aleksandrov 	struct br_ip br_group;
9985e923585SNikolay Aleksandrov 	unsigned long time;
9998ef2a9a5SYOSHIFUJI Hideaki 
100013cefad2SNikolay Aleksandrov 	if (!netif_running(br->dev) ||
100113cefad2SNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_ENABLED) ||
1002675779adSNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_QUERIER))
10038ef2a9a5SYOSHIFUJI Hideaki 		return;
10048ef2a9a5SYOSHIFUJI Hideaki 
100508b202b6SYOSHIFUJI Hideaki 	memset(&br_group.u, 0, sizeof(br_group.u));
10068ef2a9a5SYOSHIFUJI Hideaki 
100790010b36SLinus Lüssing 	if (port ? (own_query == &port->ip4_own_query) :
100890010b36SLinus Lüssing 		   (own_query == &br->ip4_own_query)) {
100990010b36SLinus Lüssing 		other_query = &br->ip4_other_query;
101008b202b6SYOSHIFUJI Hideaki 		br_group.proto = htons(ETH_P_IP);
1011dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1012cc0fdd80SLinus Lüssing 	} else {
101390010b36SLinus Lüssing 		other_query = &br->ip6_other_query;
101408b202b6SYOSHIFUJI Hideaki 		br_group.proto = htons(ETH_P_IPV6);
101508b202b6SYOSHIFUJI Hideaki #endif
1016eb1d1641SHerbert Xu 	}
1017eb1d1641SHerbert Xu 
101890010b36SLinus Lüssing 	if (!other_query || timer_pending(&other_query->timer))
1019cc0fdd80SLinus Lüssing 		return;
1020cc0fdd80SLinus Lüssing 
1021*42c11ccfSNikolay Aleksandrov 	__br_multicast_send_query(br, port, NULL, NULL, &br_group, false, 0,
1022*42c11ccfSNikolay Aleksandrov 				  NULL);
1023cc0fdd80SLinus Lüssing 
1024cc0fdd80SLinus Lüssing 	time = jiffies;
102590010b36SLinus Lüssing 	time += own_query->startup_sent < br->multicast_startup_query_count ?
1026cc0fdd80SLinus Lüssing 		br->multicast_startup_query_interval :
1027cc0fdd80SLinus Lüssing 		br->multicast_query_interval;
102890010b36SLinus Lüssing 	mod_timer(&own_query->timer, time);
1029cc0fdd80SLinus Lüssing }
1030cc0fdd80SLinus Lüssing 
103190010b36SLinus Lüssing static void
103290010b36SLinus Lüssing br_multicast_port_query_expired(struct net_bridge_port *port,
103390010b36SLinus Lüssing 				struct bridge_mcast_own_query *query)
1034eb1d1641SHerbert Xu {
1035eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
1036eb1d1641SHerbert Xu 
1037eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
103802a780c0SDan Carpenter 	if (port->state == BR_STATE_DISABLED ||
103902a780c0SDan Carpenter 	    port->state == BR_STATE_BLOCKING)
1040eb1d1641SHerbert Xu 		goto out;
1041eb1d1641SHerbert Xu 
1042cc0fdd80SLinus Lüssing 	if (query->startup_sent < br->multicast_startup_query_count)
1043cc0fdd80SLinus Lüssing 		query->startup_sent++;
1044eb1d1641SHerbert Xu 
1045cc0fdd80SLinus Lüssing 	br_multicast_send_query(port->br, port, query);
1046eb1d1641SHerbert Xu 
1047eb1d1641SHerbert Xu out:
1048eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1049eb1d1641SHerbert Xu }
1050eb1d1641SHerbert Xu 
105188c1f37fSAllen Pais static void br_ip4_multicast_port_query_expired(struct timer_list *t)
1052cc0fdd80SLinus Lüssing {
105388c1f37fSAllen Pais 	struct net_bridge_port *port = from_timer(port, t, ip4_own_query.timer);
1054cc0fdd80SLinus Lüssing 
105590010b36SLinus Lüssing 	br_multicast_port_query_expired(port, &port->ip4_own_query);
1056cc0fdd80SLinus Lüssing }
1057cc0fdd80SLinus Lüssing 
1058cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
105988c1f37fSAllen Pais static void br_ip6_multicast_port_query_expired(struct timer_list *t)
1060cc0fdd80SLinus Lüssing {
106188c1f37fSAllen Pais 	struct net_bridge_port *port = from_timer(port, t, ip6_own_query.timer);
1062cc0fdd80SLinus Lüssing 
106390010b36SLinus Lüssing 	br_multicast_port_query_expired(port, &port->ip6_own_query);
1064cc0fdd80SLinus Lüssing }
1065cc0fdd80SLinus Lüssing #endif
1066cc0fdd80SLinus Lüssing 
1067*42c11ccfSNikolay Aleksandrov static void br_multicast_port_group_rexmit(struct timer_list *t)
1068*42c11ccfSNikolay Aleksandrov {
1069*42c11ccfSNikolay Aleksandrov 	struct net_bridge_port_group *pg = from_timer(pg, t, rexmit_timer);
1070*42c11ccfSNikolay Aleksandrov 	struct bridge_mcast_other_query *other_query = NULL;
1071*42c11ccfSNikolay Aleksandrov 	struct net_bridge *br = pg->port->br;
1072*42c11ccfSNikolay Aleksandrov 	bool need_rexmit = false;
1073*42c11ccfSNikolay Aleksandrov 
1074*42c11ccfSNikolay Aleksandrov 	spin_lock(&br->multicast_lock);
1075*42c11ccfSNikolay Aleksandrov 	if (!netif_running(br->dev) || hlist_unhashed(&pg->mglist) ||
1076*42c11ccfSNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_ENABLED) ||
1077*42c11ccfSNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_QUERIER))
1078*42c11ccfSNikolay Aleksandrov 		goto out;
1079*42c11ccfSNikolay Aleksandrov 
1080*42c11ccfSNikolay Aleksandrov 	if (pg->addr.proto == htons(ETH_P_IP))
1081*42c11ccfSNikolay Aleksandrov 		other_query = &br->ip4_other_query;
1082*42c11ccfSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
1083*42c11ccfSNikolay Aleksandrov 	else
1084*42c11ccfSNikolay Aleksandrov 		other_query = &br->ip6_other_query;
1085*42c11ccfSNikolay Aleksandrov #endif
1086*42c11ccfSNikolay Aleksandrov 
1087*42c11ccfSNikolay Aleksandrov 	if (!other_query || timer_pending(&other_query->timer))
1088*42c11ccfSNikolay Aleksandrov 		goto out;
1089*42c11ccfSNikolay Aleksandrov 
1090*42c11ccfSNikolay Aleksandrov 	if (pg->grp_query_rexmit_cnt) {
1091*42c11ccfSNikolay Aleksandrov 		pg->grp_query_rexmit_cnt--;
1092*42c11ccfSNikolay Aleksandrov 		__br_multicast_send_query(br, pg->port, pg, &pg->addr,
1093*42c11ccfSNikolay Aleksandrov 					  &pg->addr, false, 1, NULL);
1094*42c11ccfSNikolay Aleksandrov 	}
1095*42c11ccfSNikolay Aleksandrov 	__br_multicast_send_query(br, pg->port, pg, &pg->addr,
1096*42c11ccfSNikolay Aleksandrov 				  &pg->addr, true, 0, &need_rexmit);
1097*42c11ccfSNikolay Aleksandrov 
1098*42c11ccfSNikolay Aleksandrov 	if (pg->grp_query_rexmit_cnt || need_rexmit)
1099*42c11ccfSNikolay Aleksandrov 		mod_timer(&pg->rexmit_timer, jiffies +
1100*42c11ccfSNikolay Aleksandrov 					     br->multicast_last_member_interval);
1101*42c11ccfSNikolay Aleksandrov out:
1102*42c11ccfSNikolay Aleksandrov 	spin_unlock(&br->multicast_lock);
1103*42c11ccfSNikolay Aleksandrov }
1104*42c11ccfSNikolay Aleksandrov 
1105147c1e9bSNogah Frankel static void br_mc_disabled_update(struct net_device *dev, bool value)
1106147c1e9bSNogah Frankel {
1107147c1e9bSNogah Frankel 	struct switchdev_attr attr = {
1108147c1e9bSNogah Frankel 		.orig_dev = dev,
1109147c1e9bSNogah Frankel 		.id = SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED,
1110147c1e9bSNogah Frankel 		.flags = SWITCHDEV_F_DEFER,
111113cefad2SNikolay Aleksandrov 		.u.mc_disabled = !value,
1112147c1e9bSNogah Frankel 	};
1113147c1e9bSNogah Frankel 
1114147c1e9bSNogah Frankel 	switchdev_port_attr_set(dev, &attr);
1115147c1e9bSNogah Frankel }
1116147c1e9bSNogah Frankel 
11171080ab95SNikolay Aleksandrov int br_multicast_add_port(struct net_bridge_port *port)
1118eb1d1641SHerbert Xu {
11197f0aec7aSNikolay Aleksandrov 	port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
1120eb1d1641SHerbert Xu 
112188c1f37fSAllen Pais 	timer_setup(&port->multicast_router_timer,
112288c1f37fSAllen Pais 		    br_multicast_router_expired, 0);
112388c1f37fSAllen Pais 	timer_setup(&port->ip4_own_query.timer,
112488c1f37fSAllen Pais 		    br_ip4_multicast_port_query_expired, 0);
1125cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
112688c1f37fSAllen Pais 	timer_setup(&port->ip6_own_query.timer,
112788c1f37fSAllen Pais 		    br_ip6_multicast_port_query_expired, 0);
1128cc0fdd80SLinus Lüssing #endif
112913cefad2SNikolay Aleksandrov 	br_mc_disabled_update(port->dev,
113013cefad2SNikolay Aleksandrov 			      br_opt_get(port->br, BROPT_MULTICAST_ENABLED));
1131147c1e9bSNogah Frankel 
11321080ab95SNikolay Aleksandrov 	port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
11331080ab95SNikolay Aleksandrov 	if (!port->mcast_stats)
11341080ab95SNikolay Aleksandrov 		return -ENOMEM;
11351080ab95SNikolay Aleksandrov 
11361080ab95SNikolay Aleksandrov 	return 0;
1137eb1d1641SHerbert Xu }
1138eb1d1641SHerbert Xu 
1139eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port)
1140eb1d1641SHerbert Xu {
1141e10177abSSatish Ashok 	struct net_bridge *br = port->br;
1142e10177abSSatish Ashok 	struct net_bridge_port_group *pg;
1143e10177abSSatish Ashok 	struct hlist_node *n;
1144e10177abSSatish Ashok 
1145e10177abSSatish Ashok 	/* Take care of the remaining groups, only perm ones should be left */
1146e10177abSSatish Ashok 	spin_lock_bh(&br->multicast_lock);
1147e10177abSSatish Ashok 	hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
1148681590bdSNikolay Aleksandrov 		br_multicast_find_del_pg(br, pg);
1149e10177abSSatish Ashok 	spin_unlock_bh(&br->multicast_lock);
1150eb1d1641SHerbert Xu 	del_timer_sync(&port->multicast_router_timer);
11511080ab95SNikolay Aleksandrov 	free_percpu(port->mcast_stats);
1152eb1d1641SHerbert Xu }
1153eb1d1641SHerbert Xu 
115490010b36SLinus Lüssing static void br_multicast_enable(struct bridge_mcast_own_query *query)
1155561f1103SHerbert Xu {
1156cc0fdd80SLinus Lüssing 	query->startup_sent = 0;
1157561f1103SHerbert Xu 
1158cc0fdd80SLinus Lüssing 	if (try_to_del_timer_sync(&query->timer) >= 0 ||
1159cc0fdd80SLinus Lüssing 	    del_timer(&query->timer))
1160cc0fdd80SLinus Lüssing 		mod_timer(&query->timer, jiffies);
1161561f1103SHerbert Xu }
1162561f1103SHerbert Xu 
11637cb3f921SNikolay Aleksandrov static void __br_multicast_enable_port(struct net_bridge_port *port)
1164eb1d1641SHerbert Xu {
1165eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
1166eb1d1641SHerbert Xu 
116713cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED) || !netif_running(br->dev))
11687cb3f921SNikolay Aleksandrov 		return;
1169eb1d1641SHerbert Xu 
117090010b36SLinus Lüssing 	br_multicast_enable(&port->ip4_own_query);
1171cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
117290010b36SLinus Lüssing 	br_multicast_enable(&port->ip6_own_query);
1173cc0fdd80SLinus Lüssing #endif
11747f0aec7aSNikolay Aleksandrov 	if (port->multicast_router == MDB_RTR_TYPE_PERM &&
11757f0aec7aSNikolay Aleksandrov 	    hlist_unhashed(&port->rlist))
1176754bc547SSatish Ashok 		br_multicast_add_router(br, port);
11777cb3f921SNikolay Aleksandrov }
1178eb1d1641SHerbert Xu 
11797cb3f921SNikolay Aleksandrov void br_multicast_enable_port(struct net_bridge_port *port)
11807cb3f921SNikolay Aleksandrov {
11817cb3f921SNikolay Aleksandrov 	struct net_bridge *br = port->br;
11827cb3f921SNikolay Aleksandrov 
11837cb3f921SNikolay Aleksandrov 	spin_lock(&br->multicast_lock);
11847cb3f921SNikolay Aleksandrov 	__br_multicast_enable_port(port);
1185eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1186eb1d1641SHerbert Xu }
1187eb1d1641SHerbert Xu 
1188eb1d1641SHerbert Xu void br_multicast_disable_port(struct net_bridge_port *port)
1189eb1d1641SHerbert Xu {
1190eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
1191eb1d1641SHerbert Xu 	struct net_bridge_port_group *pg;
1192b67bfe0dSSasha Levin 	struct hlist_node *n;
1193eb1d1641SHerbert Xu 
1194eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
1195b67bfe0dSSasha Levin 	hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
11969d06b6d8SElad Raz 		if (!(pg->flags & MDB_PG_FLAGS_PERMANENT))
1197681590bdSNikolay Aleksandrov 			br_multicast_find_del_pg(br, pg);
1198eb1d1641SHerbert Xu 
1199f12e7d95SNogah Frankel 	__del_port_router(port);
1200f12e7d95SNogah Frankel 
1201eb1d1641SHerbert Xu 	del_timer(&port->multicast_router_timer);
120290010b36SLinus Lüssing 	del_timer(&port->ip4_own_query.timer);
1203cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
120490010b36SLinus Lüssing 	del_timer(&port->ip6_own_query.timer);
1205cc0fdd80SLinus Lüssing #endif
1206eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1207eb1d1641SHerbert Xu }
1208eb1d1641SHerbert Xu 
12098ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
1210eb1d1641SHerbert Xu 					 struct net_bridge_port *port,
121106499098SVlad Yasevich 					 struct sk_buff *skb,
121206499098SVlad Yasevich 					 u16 vid)
1213eb1d1641SHerbert Xu {
12146db6f0eaSFelix Fietkau 	const unsigned char *src;
1215eb1d1641SHerbert Xu 	struct igmpv3_report *ih;
1216eb1d1641SHerbert Xu 	struct igmpv3_grec *grec;
1217eb1d1641SHerbert Xu 	int i;
1218eb1d1641SHerbert Xu 	int len;
1219eb1d1641SHerbert Xu 	int num;
1220eb1d1641SHerbert Xu 	int type;
1221eb1d1641SHerbert Xu 	int err = 0;
1222eb1d1641SHerbert Xu 	__be32 group;
1223e57f6185SNikolay Aleksandrov 	u16 nsrcs;
1224eb1d1641SHerbert Xu 
1225eb1d1641SHerbert Xu 	ih = igmpv3_report_hdr(skb);
1226eb1d1641SHerbert Xu 	num = ntohs(ih->ngrec);
1227c2d4fbd2SLinus Lüssing 	len = skb_transport_offset(skb) + sizeof(*ih);
1228eb1d1641SHerbert Xu 
1229eb1d1641SHerbert Xu 	for (i = 0; i < num; i++) {
1230eb1d1641SHerbert Xu 		len += sizeof(*grec);
1231ba5ea614SLinus Lüssing 		if (!ip_mc_may_pull(skb, len))
1232eb1d1641SHerbert Xu 			return -EINVAL;
1233eb1d1641SHerbert Xu 
1234fd218cf9SHerbert Xu 		grec = (void *)(skb->data + len - sizeof(*grec));
1235eb1d1641SHerbert Xu 		group = grec->grec_mca;
1236eb1d1641SHerbert Xu 		type = grec->grec_type;
1237e57f6185SNikolay Aleksandrov 		nsrcs = ntohs(grec->grec_nsrcs);
1238eb1d1641SHerbert Xu 
1239e57f6185SNikolay Aleksandrov 		len += nsrcs * 4;
1240ba5ea614SLinus Lüssing 		if (!ip_mc_may_pull(skb, len))
1241eb1d1641SHerbert Xu 			return -EINVAL;
1242eb1d1641SHerbert Xu 
1243eb1d1641SHerbert Xu 		/* We treat this as an IGMPv2 report for now. */
1244eb1d1641SHerbert Xu 		switch (type) {
1245eb1d1641SHerbert Xu 		case IGMPV3_MODE_IS_INCLUDE:
1246eb1d1641SHerbert Xu 		case IGMPV3_MODE_IS_EXCLUDE:
1247eb1d1641SHerbert Xu 		case IGMPV3_CHANGE_TO_INCLUDE:
1248eb1d1641SHerbert Xu 		case IGMPV3_CHANGE_TO_EXCLUDE:
1249eb1d1641SHerbert Xu 		case IGMPV3_ALLOW_NEW_SOURCES:
1250eb1d1641SHerbert Xu 		case IGMPV3_BLOCK_OLD_SOURCES:
1251eb1d1641SHerbert Xu 			break;
1252eb1d1641SHerbert Xu 
1253eb1d1641SHerbert Xu 		default:
1254eb1d1641SHerbert Xu 			continue;
1255eb1d1641SHerbert Xu 		}
1256eb1d1641SHerbert Xu 
12576db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
1258bc8c20acSSatish Ashok 		if ((type == IGMPV3_CHANGE_TO_INCLUDE ||
1259bc8c20acSSatish Ashok 		     type == IGMPV3_MODE_IS_INCLUDE) &&
1260e57f6185SNikolay Aleksandrov 		    nsrcs == 0) {
12616db6f0eaSFelix Fietkau 			br_ip4_multicast_leave_group(br, port, group, vid, src);
1262bc8c20acSSatish Ashok 		} else {
12636db6f0eaSFelix Fietkau 			err = br_ip4_multicast_add_group(br, port, group, vid,
12648b671779SNikolay Aleksandrov 							 src, true);
1265eb1d1641SHerbert Xu 			if (err)
1266eb1d1641SHerbert Xu 				break;
1267eb1d1641SHerbert Xu 		}
1268bc8c20acSSatish Ashok 	}
1269eb1d1641SHerbert Xu 
1270eb1d1641SHerbert Xu 	return err;
1271eb1d1641SHerbert Xu }
1272eb1d1641SHerbert Xu 
1273dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
127408b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_mld2_report(struct net_bridge *br,
127508b202b6SYOSHIFUJI Hideaki 					struct net_bridge_port *port,
127606499098SVlad Yasevich 					struct sk_buff *skb,
127706499098SVlad Yasevich 					u16 vid)
127808b202b6SYOSHIFUJI Hideaki {
1279ba5ea614SLinus Lüssing 	unsigned int nsrcs_offset;
12806db6f0eaSFelix Fietkau 	const unsigned char *src;
128108b202b6SYOSHIFUJI Hideaki 	struct icmp6hdr *icmp6h;
128208b202b6SYOSHIFUJI Hideaki 	struct mld2_grec *grec;
1283ba5ea614SLinus Lüssing 	unsigned int grec_len;
128408b202b6SYOSHIFUJI Hideaki 	int i;
128508b202b6SYOSHIFUJI Hideaki 	int len;
128608b202b6SYOSHIFUJI Hideaki 	int num;
128708b202b6SYOSHIFUJI Hideaki 	int err = 0;
128808b202b6SYOSHIFUJI Hideaki 
1289ba5ea614SLinus Lüssing 	if (!ipv6_mc_may_pull(skb, sizeof(*icmp6h)))
129008b202b6SYOSHIFUJI Hideaki 		return -EINVAL;
129108b202b6SYOSHIFUJI Hideaki 
129208b202b6SYOSHIFUJI Hideaki 	icmp6h = icmp6_hdr(skb);
129308b202b6SYOSHIFUJI Hideaki 	num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
1294c2d4fbd2SLinus Lüssing 	len = skb_transport_offset(skb) + sizeof(*icmp6h);
129508b202b6SYOSHIFUJI Hideaki 
129608b202b6SYOSHIFUJI Hideaki 	for (i = 0; i < num; i++) {
1297e57f6185SNikolay Aleksandrov 		__be16 *_nsrcs, __nsrcs;
1298e57f6185SNikolay Aleksandrov 		u16 nsrcs;
129908b202b6SYOSHIFUJI Hideaki 
1300ba5ea614SLinus Lüssing 		nsrcs_offset = len + offsetof(struct mld2_grec, grec_nsrcs);
1301ba5ea614SLinus Lüssing 
1302ba5ea614SLinus Lüssing 		if (skb_transport_offset(skb) + ipv6_transport_len(skb) <
13035fc6266aSLinus Lüssing 		    nsrcs_offset + sizeof(__nsrcs))
1304ba5ea614SLinus Lüssing 			return -EINVAL;
1305ba5ea614SLinus Lüssing 
1306e57f6185SNikolay Aleksandrov 		_nsrcs = skb_header_pointer(skb, nsrcs_offset,
1307e57f6185SNikolay Aleksandrov 					    sizeof(__nsrcs), &__nsrcs);
1308e57f6185SNikolay Aleksandrov 		if (!_nsrcs)
130908b202b6SYOSHIFUJI Hideaki 			return -EINVAL;
131008b202b6SYOSHIFUJI Hideaki 
1311e57f6185SNikolay Aleksandrov 		nsrcs = ntohs(*_nsrcs);
1312e57f6185SNikolay Aleksandrov 		grec_len = struct_size(grec, grec_src, nsrcs);
1313ba5ea614SLinus Lüssing 
1314ba5ea614SLinus Lüssing 		if (!ipv6_mc_may_pull(skb, len + grec_len))
131508b202b6SYOSHIFUJI Hideaki 			return -EINVAL;
131608b202b6SYOSHIFUJI Hideaki 
131708b202b6SYOSHIFUJI Hideaki 		grec = (struct mld2_grec *)(skb->data + len);
1318ba5ea614SLinus Lüssing 		len += grec_len;
131908b202b6SYOSHIFUJI Hideaki 
132008b202b6SYOSHIFUJI Hideaki 		/* We treat these as MLDv1 reports for now. */
132108b202b6SYOSHIFUJI Hideaki 		switch (grec->grec_type) {
132208b202b6SYOSHIFUJI Hideaki 		case MLD2_MODE_IS_INCLUDE:
132308b202b6SYOSHIFUJI Hideaki 		case MLD2_MODE_IS_EXCLUDE:
132408b202b6SYOSHIFUJI Hideaki 		case MLD2_CHANGE_TO_INCLUDE:
132508b202b6SYOSHIFUJI Hideaki 		case MLD2_CHANGE_TO_EXCLUDE:
132608b202b6SYOSHIFUJI Hideaki 		case MLD2_ALLOW_NEW_SOURCES:
132708b202b6SYOSHIFUJI Hideaki 		case MLD2_BLOCK_OLD_SOURCES:
132808b202b6SYOSHIFUJI Hideaki 			break;
132908b202b6SYOSHIFUJI Hideaki 
133008b202b6SYOSHIFUJI Hideaki 		default:
133108b202b6SYOSHIFUJI Hideaki 			continue;
133208b202b6SYOSHIFUJI Hideaki 		}
133308b202b6SYOSHIFUJI Hideaki 
13346db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
1335bc8c20acSSatish Ashok 		if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE ||
1336bc8c20acSSatish Ashok 		     grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
1337e57f6185SNikolay Aleksandrov 		    nsrcs == 0) {
1338bc8c20acSSatish Ashok 			br_ip6_multicast_leave_group(br, port, &grec->grec_mca,
13396db6f0eaSFelix Fietkau 						     vid, src);
1340bc8c20acSSatish Ashok 		} else {
1341bc8c20acSSatish Ashok 			err = br_ip6_multicast_add_group(br, port,
13426db6f0eaSFelix Fietkau 							 &grec->grec_mca, vid,
13438b671779SNikolay Aleksandrov 							 src, true);
13449264251eSDavide Caratti 			if (err)
134508b202b6SYOSHIFUJI Hideaki 				break;
134608b202b6SYOSHIFUJI Hideaki 		}
1347bc8c20acSSatish Ashok 	}
134808b202b6SYOSHIFUJI Hideaki 
134908b202b6SYOSHIFUJI Hideaki 	return err;
135008b202b6SYOSHIFUJI Hideaki }
135108b202b6SYOSHIFUJI Hideaki #endif
135208b202b6SYOSHIFUJI Hideaki 
1353dc4eb53aSLinus Lüssing static bool br_ip4_multicast_select_querier(struct net_bridge *br,
13542cd41431SLinus Lüssing 					    struct net_bridge_port *port,
1355dc4eb53aSLinus Lüssing 					    __be32 saddr)
1356dc4eb53aSLinus Lüssing {
1357dc4eb53aSLinus Lüssing 	if (!timer_pending(&br->ip4_own_query.timer) &&
1358dc4eb53aSLinus Lüssing 	    !timer_pending(&br->ip4_other_query.timer))
1359dc4eb53aSLinus Lüssing 		goto update;
1360dc4eb53aSLinus Lüssing 
1361dc4eb53aSLinus Lüssing 	if (!br->ip4_querier.addr.u.ip4)
1362dc4eb53aSLinus Lüssing 		goto update;
1363dc4eb53aSLinus Lüssing 
1364dc4eb53aSLinus Lüssing 	if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4))
1365dc4eb53aSLinus Lüssing 		goto update;
1366dc4eb53aSLinus Lüssing 
1367dc4eb53aSLinus Lüssing 	return false;
1368dc4eb53aSLinus Lüssing 
1369dc4eb53aSLinus Lüssing update:
1370dc4eb53aSLinus Lüssing 	br->ip4_querier.addr.u.ip4 = saddr;
1371dc4eb53aSLinus Lüssing 
13722cd41431SLinus Lüssing 	/* update protected by general multicast_lock by caller */
13732cd41431SLinus Lüssing 	rcu_assign_pointer(br->ip4_querier.port, port);
13742cd41431SLinus Lüssing 
1375dc4eb53aSLinus Lüssing 	return true;
1376dc4eb53aSLinus Lüssing }
1377dc4eb53aSLinus Lüssing 
1378dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
1379dc4eb53aSLinus Lüssing static bool br_ip6_multicast_select_querier(struct net_bridge *br,
13802cd41431SLinus Lüssing 					    struct net_bridge_port *port,
1381dc4eb53aSLinus Lüssing 					    struct in6_addr *saddr)
1382dc4eb53aSLinus Lüssing {
1383dc4eb53aSLinus Lüssing 	if (!timer_pending(&br->ip6_own_query.timer) &&
1384dc4eb53aSLinus Lüssing 	    !timer_pending(&br->ip6_other_query.timer))
1385dc4eb53aSLinus Lüssing 		goto update;
1386dc4eb53aSLinus Lüssing 
1387dc4eb53aSLinus Lüssing 	if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0)
1388dc4eb53aSLinus Lüssing 		goto update;
1389dc4eb53aSLinus Lüssing 
1390dc4eb53aSLinus Lüssing 	return false;
1391dc4eb53aSLinus Lüssing 
1392dc4eb53aSLinus Lüssing update:
1393dc4eb53aSLinus Lüssing 	br->ip6_querier.addr.u.ip6 = *saddr;
1394dc4eb53aSLinus Lüssing 
13952cd41431SLinus Lüssing 	/* update protected by general multicast_lock by caller */
13962cd41431SLinus Lüssing 	rcu_assign_pointer(br->ip6_querier.port, port);
13972cd41431SLinus Lüssing 
1398dc4eb53aSLinus Lüssing 	return true;
1399dc4eb53aSLinus Lüssing }
1400dc4eb53aSLinus Lüssing #endif
1401dc4eb53aSLinus Lüssing 
1402dc4eb53aSLinus Lüssing static bool br_multicast_select_querier(struct net_bridge *br,
14032cd41431SLinus Lüssing 					struct net_bridge_port *port,
1404dc4eb53aSLinus Lüssing 					struct br_ip *saddr)
1405dc4eb53aSLinus Lüssing {
1406dc4eb53aSLinus Lüssing 	switch (saddr->proto) {
1407dc4eb53aSLinus Lüssing 	case htons(ETH_P_IP):
14082cd41431SLinus Lüssing 		return br_ip4_multicast_select_querier(br, port, saddr->u.ip4);
1409dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
1410dc4eb53aSLinus Lüssing 	case htons(ETH_P_IPV6):
14112cd41431SLinus Lüssing 		return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6);
1412dc4eb53aSLinus Lüssing #endif
1413dc4eb53aSLinus Lüssing 	}
1414dc4eb53aSLinus Lüssing 
1415dc4eb53aSLinus Lüssing 	return false;
1416dc4eb53aSLinus Lüssing }
1417dc4eb53aSLinus Lüssing 
1418cc0fdd80SLinus Lüssing static void
141990010b36SLinus Lüssing br_multicast_update_query_timer(struct net_bridge *br,
142090010b36SLinus Lüssing 				struct bridge_mcast_other_query *query,
1421b00589afSLinus Lüssing 				unsigned long max_delay)
1422b00589afSLinus Lüssing {
142390010b36SLinus Lüssing 	if (!timer_pending(&query->timer))
142490010b36SLinus Lüssing 		query->delay_time = jiffies + max_delay;
1425b00589afSLinus Lüssing 
142690010b36SLinus Lüssing 	mod_timer(&query->timer, jiffies + br->multicast_querier_interval);
1427b00589afSLinus Lüssing }
1428b00589afSLinus Lüssing 
14296d549648SNogah Frankel static void br_port_mc_router_state_change(struct net_bridge_port *p,
14306d549648SNogah Frankel 					   bool is_mc_router)
14316d549648SNogah Frankel {
14326d549648SNogah Frankel 	struct switchdev_attr attr = {
14336d549648SNogah Frankel 		.orig_dev = p->dev,
14346d549648SNogah Frankel 		.id = SWITCHDEV_ATTR_ID_PORT_MROUTER,
14356d549648SNogah Frankel 		.flags = SWITCHDEV_F_DEFER,
14366d549648SNogah Frankel 		.u.mrouter = is_mc_router,
14376d549648SNogah Frankel 	};
14386d549648SNogah Frankel 
14396d549648SNogah Frankel 	switchdev_port_attr_set(p->dev, &attr);
14406d549648SNogah Frankel }
14416d549648SNogah Frankel 
14427e80c124Sstephen hemminger /*
14437c77602fSCong Wang  * Add port to router_list
14447e80c124Sstephen hemminger  *  list is maintained ordered by pointer value
14457e80c124Sstephen hemminger  *  and locked by br->multicast_lock and RCU
14467e80c124Sstephen hemminger  */
14470909e117SHerbert Xu static void br_multicast_add_router(struct net_bridge *br,
14480909e117SHerbert Xu 				    struct net_bridge_port *port)
14490909e117SHerbert Xu {
1450dcdca2c4Sstephen hemminger 	struct net_bridge_port *p;
1451b67bfe0dSSasha Levin 	struct hlist_node *slot = NULL;
14520909e117SHerbert Xu 
14531a040eacSNikolay Aleksandrov 	if (!hlist_unhashed(&port->rlist))
14541a040eacSNikolay Aleksandrov 		return;
14551a040eacSNikolay Aleksandrov 
1456b67bfe0dSSasha Levin 	hlist_for_each_entry(p, &br->router_list, rlist) {
14577e80c124Sstephen hemminger 		if ((unsigned long) port >= (unsigned long) p)
14587e80c124Sstephen hemminger 			break;
1459b67bfe0dSSasha Levin 		slot = &p->rlist;
1460dcdca2c4Sstephen hemminger 	}
14610909e117SHerbert Xu 
14627e80c124Sstephen hemminger 	if (slot)
14631d023284SKen Helias 		hlist_add_behind_rcu(&port->rlist, slot);
1464dcdca2c4Sstephen hemminger 	else
1465dcdca2c4Sstephen hemminger 		hlist_add_head_rcu(&port->rlist, &br->router_list);
1466949f1e39SSatish Ashok 	br_rtr_notify(br->dev, port, RTM_NEWMDB);
14676d549648SNogah Frankel 	br_port_mc_router_state_change(port, true);
14680909e117SHerbert Xu }
14690909e117SHerbert Xu 
1470eb1d1641SHerbert Xu static void br_multicast_mark_router(struct net_bridge *br,
1471eb1d1641SHerbert Xu 				     struct net_bridge_port *port)
1472eb1d1641SHerbert Xu {
1473eb1d1641SHerbert Xu 	unsigned long now = jiffies;
1474eb1d1641SHerbert Xu 
1475eb1d1641SHerbert Xu 	if (!port) {
147677041420SYotam Gigi 		if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) {
147777041420SYotam Gigi 			if (!timer_pending(&br->multicast_router_timer))
147877041420SYotam Gigi 				br_mc_router_state_change(br, true);
1479eb1d1641SHerbert Xu 			mod_timer(&br->multicast_router_timer,
1480eb1d1641SHerbert Xu 				  now + br->multicast_querier_interval);
148177041420SYotam Gigi 		}
1482eb1d1641SHerbert Xu 		return;
1483eb1d1641SHerbert Xu 	}
1484eb1d1641SHerbert Xu 
1485a55d8246SNikolay Aleksandrov 	if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
1486a55d8246SNikolay Aleksandrov 	    port->multicast_router == MDB_RTR_TYPE_PERM)
1487eb1d1641SHerbert Xu 		return;
1488eb1d1641SHerbert Xu 
14890909e117SHerbert Xu 	br_multicast_add_router(br, port);
1490eb1d1641SHerbert Xu 
1491eb1d1641SHerbert Xu 	mod_timer(&port->multicast_router_timer,
1492eb1d1641SHerbert Xu 		  now + br->multicast_querier_interval);
1493eb1d1641SHerbert Xu }
1494eb1d1641SHerbert Xu 
1495eb1d1641SHerbert Xu static void br_multicast_query_received(struct net_bridge *br,
1496eb1d1641SHerbert Xu 					struct net_bridge_port *port,
149790010b36SLinus Lüssing 					struct bridge_mcast_other_query *query,
1498dc4eb53aSLinus Lüssing 					struct br_ip *saddr,
1499b00589afSLinus Lüssing 					unsigned long max_delay)
1500eb1d1641SHerbert Xu {
15012cd41431SLinus Lüssing 	if (!br_multicast_select_querier(br, port, saddr))
1502eb1d1641SHerbert Xu 		return;
1503eb1d1641SHerbert Xu 
1504dc4eb53aSLinus Lüssing 	br_multicast_update_query_timer(br, query, max_delay);
1505eb1d1641SHerbert Xu 	br_multicast_mark_router(br, port);
1506eb1d1641SHerbert Xu }
1507eb1d1641SHerbert Xu 
15089c2e955cSzhong jiang static void br_ip4_multicast_query(struct net_bridge *br,
1509eb1d1641SHerbert Xu 				   struct net_bridge_port *port,
151006499098SVlad Yasevich 				   struct sk_buff *skb,
151106499098SVlad Yasevich 				   u16 vid)
1512eb1d1641SHerbert Xu {
1513ba5ea614SLinus Lüssing 	unsigned int transport_len = ip_transport_len(skb);
1514b71d1d42SEric Dumazet 	const struct iphdr *iph = ip_hdr(skb);
1515eb1d1641SHerbert Xu 	struct igmphdr *ih = igmp_hdr(skb);
1516eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
1517eb1d1641SHerbert Xu 	struct igmpv3_query *ih3;
1518eb1d1641SHerbert Xu 	struct net_bridge_port_group *p;
1519e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
1520dc4eb53aSLinus Lüssing 	struct br_ip saddr;
1521eb1d1641SHerbert Xu 	unsigned long max_delay;
1522eb1d1641SHerbert Xu 	unsigned long now = jiffies;
1523eb1d1641SHerbert Xu 	__be32 group;
1524eb1d1641SHerbert Xu 
1525eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
1526eb1d1641SHerbert Xu 	if (!netif_running(br->dev) ||
1527eb1d1641SHerbert Xu 	    (port && port->state == BR_STATE_DISABLED))
1528eb1d1641SHerbert Xu 		goto out;
1529eb1d1641SHerbert Xu 
1530eb1d1641SHerbert Xu 	group = ih->group;
1531eb1d1641SHerbert Xu 
1532ba5ea614SLinus Lüssing 	if (transport_len == sizeof(*ih)) {
1533eb1d1641SHerbert Xu 		max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1534eb1d1641SHerbert Xu 
1535eb1d1641SHerbert Xu 		if (!max_delay) {
1536eb1d1641SHerbert Xu 			max_delay = 10 * HZ;
1537eb1d1641SHerbert Xu 			group = 0;
1538eb1d1641SHerbert Xu 		}
1539ba5ea614SLinus Lüssing 	} else if (transport_len >= sizeof(*ih3)) {
1540eb1d1641SHerbert Xu 		ih3 = igmpv3_query_hdr(skb);
1541eb1d1641SHerbert Xu 		if (ih3->nsrcs)
1542bec68ff1SYOSHIFUJI Hideaki 			goto out;
1543eb1d1641SHerbert Xu 
15440ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 		max_delay = ih3->code ?
15450ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 			    IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
15469afd85c9SLinus Lüssing 	} else {
15479ed973ccSLinus Lüssing 		goto out;
15489ed973ccSLinus Lüssing 	}
15499ed973ccSLinus Lüssing 
1550dc4eb53aSLinus Lüssing 	if (!group) {
1551dc4eb53aSLinus Lüssing 		saddr.proto = htons(ETH_P_IP);
1552dc4eb53aSLinus Lüssing 		saddr.u.ip4 = iph->saddr;
1553b00589afSLinus Lüssing 
1554dc4eb53aSLinus Lüssing 		br_multicast_query_received(br, port, &br->ip4_other_query,
1555dc4eb53aSLinus Lüssing 					    &saddr, max_delay);
1556eb1d1641SHerbert Xu 		goto out;
1557dc4eb53aSLinus Lüssing 	}
1558eb1d1641SHerbert Xu 
155919e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip4_get(br, group, vid);
1560eb1d1641SHerbert Xu 	if (!mp)
1561eb1d1641SHerbert Xu 		goto out;
1562eb1d1641SHerbert Xu 
1563eb1d1641SHerbert Xu 	max_delay *= br->multicast_last_member_count;
1564eb1d1641SHerbert Xu 
1565ff0fd34eSAndrew Lunn 	if (mp->host_joined &&
1566eb1d1641SHerbert Xu 	    (timer_pending(&mp->timer) ?
1567eb1d1641SHerbert Xu 	     time_after(mp->timer.expires, now + max_delay) :
1568eb1d1641SHerbert Xu 	     try_to_del_timer_sync(&mp->timer) >= 0))
1569eb1d1641SHerbert Xu 		mod_timer(&mp->timer, now + max_delay);
1570eb1d1641SHerbert Xu 
1571e8051688SEric Dumazet 	for (pp = &mp->ports;
1572e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
1573e8051688SEric Dumazet 	     pp = &p->next) {
1574eb1d1641SHerbert Xu 		if (timer_pending(&p->timer) ?
1575eb1d1641SHerbert Xu 		    time_after(p->timer.expires, now + max_delay) :
1576eb1d1641SHerbert Xu 		    try_to_del_timer_sync(&p->timer) >= 0)
157724f9cdcbSHerbert Xu 			mod_timer(&p->timer, now + max_delay);
1578eb1d1641SHerbert Xu 	}
1579eb1d1641SHerbert Xu 
1580eb1d1641SHerbert Xu out:
1581eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1582eb1d1641SHerbert Xu }
1583eb1d1641SHerbert Xu 
1584dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
158508b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_query(struct net_bridge *br,
158608b202b6SYOSHIFUJI Hideaki 				  struct net_bridge_port *port,
158706499098SVlad Yasevich 				  struct sk_buff *skb,
158806499098SVlad Yasevich 				  u16 vid)
158908b202b6SYOSHIFUJI Hideaki {
1590ba5ea614SLinus Lüssing 	unsigned int transport_len = ipv6_transport_len(skb);
1591eca2a43bSstephen hemminger 	struct mld_msg *mld;
159208b202b6SYOSHIFUJI Hideaki 	struct net_bridge_mdb_entry *mp;
159308b202b6SYOSHIFUJI Hideaki 	struct mld2_query *mld2q;
1594e8051688SEric Dumazet 	struct net_bridge_port_group *p;
1595e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
1596dc4eb53aSLinus Lüssing 	struct br_ip saddr;
159708b202b6SYOSHIFUJI Hideaki 	unsigned long max_delay;
159808b202b6SYOSHIFUJI Hideaki 	unsigned long now = jiffies;
1599856ce5d0SLinus Lüssing 	unsigned int offset = skb_transport_offset(skb);
1600b71d1d42SEric Dumazet 	const struct in6_addr *group = NULL;
16019ed973ccSLinus Lüssing 	bool is_general_query;
160208b202b6SYOSHIFUJI Hideaki 	int err = 0;
160308b202b6SYOSHIFUJI Hideaki 
160408b202b6SYOSHIFUJI Hideaki 	spin_lock(&br->multicast_lock);
160508b202b6SYOSHIFUJI Hideaki 	if (!netif_running(br->dev) ||
160608b202b6SYOSHIFUJI Hideaki 	    (port && port->state == BR_STATE_DISABLED))
160708b202b6SYOSHIFUJI Hideaki 		goto out;
160808b202b6SYOSHIFUJI Hideaki 
1609ba5ea614SLinus Lüssing 	if (transport_len == sizeof(*mld)) {
1610856ce5d0SLinus Lüssing 		if (!pskb_may_pull(skb, offset + sizeof(*mld))) {
161108b202b6SYOSHIFUJI Hideaki 			err = -EINVAL;
161208b202b6SYOSHIFUJI Hideaki 			goto out;
161308b202b6SYOSHIFUJI Hideaki 		}
161408b202b6SYOSHIFUJI Hideaki 		mld = (struct mld_msg *) icmp6_hdr(skb);
16154715213dSLi RongQing 		max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
161608b202b6SYOSHIFUJI Hideaki 		if (max_delay)
161708b202b6SYOSHIFUJI Hideaki 			group = &mld->mld_mca;
1618248ba8ecSLinus Lüssing 	} else {
1619856ce5d0SLinus Lüssing 		if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) {
162008b202b6SYOSHIFUJI Hideaki 			err = -EINVAL;
162108b202b6SYOSHIFUJI Hideaki 			goto out;
162208b202b6SYOSHIFUJI Hideaki 		}
162308b202b6SYOSHIFUJI Hideaki 		mld2q = (struct mld2_query *)icmp6_hdr(skb);
162408b202b6SYOSHIFUJI Hideaki 		if (!mld2q->mld2q_nsrcs)
162508b202b6SYOSHIFUJI Hideaki 			group = &mld2q->mld2q_mca;
1626e3f5b170SDaniel Borkmann 
1627e3f5b170SDaniel Borkmann 		max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
162808b202b6SYOSHIFUJI Hideaki 	}
162908b202b6SYOSHIFUJI Hideaki 
16309ed973ccSLinus Lüssing 	is_general_query = group && ipv6_addr_any(group);
16319ed973ccSLinus Lüssing 
1632dc4eb53aSLinus Lüssing 	if (is_general_query) {
1633dc4eb53aSLinus Lüssing 		saddr.proto = htons(ETH_P_IPV6);
16343b26a5d0SNikolay Aleksandrov 		saddr.u.ip6 = ipv6_hdr(skb)->saddr;
1635b00589afSLinus Lüssing 
1636dc4eb53aSLinus Lüssing 		br_multicast_query_received(br, port, &br->ip6_other_query,
1637dc4eb53aSLinus Lüssing 					    &saddr, max_delay);
163808b202b6SYOSHIFUJI Hideaki 		goto out;
16396c03ee8bSLinus Lüssing 	} else if (!group) {
16406c03ee8bSLinus Lüssing 		goto out;
1641dc4eb53aSLinus Lüssing 	}
164208b202b6SYOSHIFUJI Hideaki 
164319e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip6_get(br, group, vid);
164408b202b6SYOSHIFUJI Hideaki 	if (!mp)
164508b202b6SYOSHIFUJI Hideaki 		goto out;
164608b202b6SYOSHIFUJI Hideaki 
164708b202b6SYOSHIFUJI Hideaki 	max_delay *= br->multicast_last_member_count;
1648ff0fd34eSAndrew Lunn 	if (mp->host_joined &&
164908b202b6SYOSHIFUJI Hideaki 	    (timer_pending(&mp->timer) ?
165008b202b6SYOSHIFUJI Hideaki 	     time_after(mp->timer.expires, now + max_delay) :
165108b202b6SYOSHIFUJI Hideaki 	     try_to_del_timer_sync(&mp->timer) >= 0))
165208b202b6SYOSHIFUJI Hideaki 		mod_timer(&mp->timer, now + max_delay);
165308b202b6SYOSHIFUJI Hideaki 
1654e8051688SEric Dumazet 	for (pp = &mp->ports;
1655e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
1656e8051688SEric Dumazet 	     pp = &p->next) {
165708b202b6SYOSHIFUJI Hideaki 		if (timer_pending(&p->timer) ?
165808b202b6SYOSHIFUJI Hideaki 		    time_after(p->timer.expires, now + max_delay) :
165908b202b6SYOSHIFUJI Hideaki 		    try_to_del_timer_sync(&p->timer) >= 0)
166024f9cdcbSHerbert Xu 			mod_timer(&p->timer, now + max_delay);
166108b202b6SYOSHIFUJI Hideaki 	}
166208b202b6SYOSHIFUJI Hideaki 
166308b202b6SYOSHIFUJI Hideaki out:
166408b202b6SYOSHIFUJI Hideaki 	spin_unlock(&br->multicast_lock);
166508b202b6SYOSHIFUJI Hideaki 	return err;
166608b202b6SYOSHIFUJI Hideaki }
166708b202b6SYOSHIFUJI Hideaki #endif
166808b202b6SYOSHIFUJI Hideaki 
166990010b36SLinus Lüssing static void
167090010b36SLinus Lüssing br_multicast_leave_group(struct net_bridge *br,
1671eb1d1641SHerbert Xu 			 struct net_bridge_port *port,
1672cc0fdd80SLinus Lüssing 			 struct br_ip *group,
167390010b36SLinus Lüssing 			 struct bridge_mcast_other_query *other_query,
16746db6f0eaSFelix Fietkau 			 struct bridge_mcast_own_query *own_query,
16756db6f0eaSFelix Fietkau 			 const unsigned char *src)
1676eb1d1641SHerbert Xu {
1677eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
1678eb1d1641SHerbert Xu 	struct net_bridge_port_group *p;
1679eb1d1641SHerbert Xu 	unsigned long now;
1680eb1d1641SHerbert Xu 	unsigned long time;
1681eb1d1641SHerbert Xu 
1682eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
1683eb1d1641SHerbert Xu 	if (!netif_running(br->dev) ||
1684544586f7SSatish Ashok 	    (port && port->state == BR_STATE_DISABLED))
1685eb1d1641SHerbert Xu 		goto out;
1686eb1d1641SHerbert Xu 
168719e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip_get(br, group);
1688eb1d1641SHerbert Xu 	if (!mp)
1689eb1d1641SHerbert Xu 		goto out;
1690eb1d1641SHerbert Xu 
1691544586f7SSatish Ashok 	if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
1692544586f7SSatish Ashok 		struct net_bridge_port_group __rcu **pp;
1693544586f7SSatish Ashok 
1694544586f7SSatish Ashok 		for (pp = &mp->ports;
1695544586f7SSatish Ashok 		     (p = mlock_dereference(*pp, br)) != NULL;
1696544586f7SSatish Ashok 		     pp = &p->next) {
16976db6f0eaSFelix Fietkau 			if (!br_port_group_equal(p, port, src))
1698544586f7SSatish Ashok 				continue;
1699544586f7SSatish Ashok 
17005c725b6bSNikolay Aleksandrov 			if (p->flags & MDB_PG_FLAGS_PERMANENT)
17015c725b6bSNikolay Aleksandrov 				break;
17025c725b6bSNikolay Aleksandrov 
1703681590bdSNikolay Aleksandrov 			p->flags |= MDB_PG_FLAGS_FAST_LEAVE;
1704681590bdSNikolay Aleksandrov 			br_multicast_del_pg(mp, p, pp);
1705544586f7SSatish Ashok 		}
1706544586f7SSatish Ashok 		goto out;
1707544586f7SSatish Ashok 	}
1708544586f7SSatish Ashok 
1709544586f7SSatish Ashok 	if (timer_pending(&other_query->timer))
1710544586f7SSatish Ashok 		goto out;
1711544586f7SSatish Ashok 
1712675779adSNikolay Aleksandrov 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER)) {
1713438ef2d0SNikolay Aleksandrov 		__br_multicast_send_query(br, port, NULL, NULL, &mp->addr,
1714*42c11ccfSNikolay Aleksandrov 					  false, 0, NULL);
17156b7df111SCong Wang 
17166b7df111SCong Wang 		time = jiffies + br->multicast_last_member_count *
17176b7df111SCong Wang 				 br->multicast_last_member_interval;
1718cc0fdd80SLinus Lüssing 
171990010b36SLinus Lüssing 		mod_timer(&own_query->timer, time);
17206b7df111SCong Wang 
17216b7df111SCong Wang 		for (p = mlock_dereference(mp->ports, br);
17226b7df111SCong Wang 		     p != NULL;
17236b7df111SCong Wang 		     p = mlock_dereference(p->next, br)) {
17246db6f0eaSFelix Fietkau 			if (!br_port_group_equal(p, port, src))
17256b7df111SCong Wang 				continue;
17266b7df111SCong Wang 
17276b7df111SCong Wang 			if (!hlist_unhashed(&p->mglist) &&
17286b7df111SCong Wang 			    (timer_pending(&p->timer) ?
17296b7df111SCong Wang 			     time_after(p->timer.expires, time) :
17306b7df111SCong Wang 			     try_to_del_timer_sync(&p->timer) >= 0)) {
17316b7df111SCong Wang 				mod_timer(&p->timer, time);
17326b7df111SCong Wang 			}
17336b7df111SCong Wang 
17346b7df111SCong Wang 			break;
17356b7df111SCong Wang 		}
17366b7df111SCong Wang 	}
17376b7df111SCong Wang 
1738eb1d1641SHerbert Xu 	now = jiffies;
1739eb1d1641SHerbert Xu 	time = now + br->multicast_last_member_count *
1740eb1d1641SHerbert Xu 		     br->multicast_last_member_interval;
1741eb1d1641SHerbert Xu 
1742eb1d1641SHerbert Xu 	if (!port) {
1743ff0fd34eSAndrew Lunn 		if (mp->host_joined &&
1744eb1d1641SHerbert Xu 		    (timer_pending(&mp->timer) ?
1745eb1d1641SHerbert Xu 		     time_after(mp->timer.expires, time) :
1746eb1d1641SHerbert Xu 		     try_to_del_timer_sync(&mp->timer) >= 0)) {
1747eb1d1641SHerbert Xu 			mod_timer(&mp->timer, time);
1748eb1d1641SHerbert Xu 		}
1749454594f3SLinus Lüssing 
1750454594f3SLinus Lüssing 		goto out;
1751454594f3SLinus Lüssing 	}
1752454594f3SLinus Lüssing 
1753454594f3SLinus Lüssing 	for (p = mlock_dereference(mp->ports, br);
1754454594f3SLinus Lüssing 	     p != NULL;
1755454594f3SLinus Lüssing 	     p = mlock_dereference(p->next, br)) {
1756454594f3SLinus Lüssing 		if (p->port != port)
1757454594f3SLinus Lüssing 			continue;
1758454594f3SLinus Lüssing 
1759454594f3SLinus Lüssing 		if (!hlist_unhashed(&p->mglist) &&
1760454594f3SLinus Lüssing 		    (timer_pending(&p->timer) ?
1761454594f3SLinus Lüssing 		     time_after(p->timer.expires, time) :
1762454594f3SLinus Lüssing 		     try_to_del_timer_sync(&p->timer) >= 0)) {
1763454594f3SLinus Lüssing 			mod_timer(&p->timer, time);
1764454594f3SLinus Lüssing 		}
1765454594f3SLinus Lüssing 
1766454594f3SLinus Lüssing 		break;
1767eb1d1641SHerbert Xu 	}
1768eb1d1641SHerbert Xu out:
1769eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1770eb1d1641SHerbert Xu }
1771eb1d1641SHerbert Xu 
17728ef2a9a5SYOSHIFUJI Hideaki static void br_ip4_multicast_leave_group(struct net_bridge *br,
17738ef2a9a5SYOSHIFUJI Hideaki 					 struct net_bridge_port *port,
1774b0e9a30dSVlad Yasevich 					 __be32 group,
17756db6f0eaSFelix Fietkau 					 __u16 vid,
17766db6f0eaSFelix Fietkau 					 const unsigned char *src)
17778ef2a9a5SYOSHIFUJI Hideaki {
17788ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip br_group;
177990010b36SLinus Lüssing 	struct bridge_mcast_own_query *own_query;
17808ef2a9a5SYOSHIFUJI Hideaki 
17818ef2a9a5SYOSHIFUJI Hideaki 	if (ipv4_is_local_multicast(group))
17828ef2a9a5SYOSHIFUJI Hideaki 		return;
17838ef2a9a5SYOSHIFUJI Hideaki 
178490010b36SLinus Lüssing 	own_query = port ? &port->ip4_own_query : &br->ip4_own_query;
178590010b36SLinus Lüssing 
17861515a63fSNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
17878ef2a9a5SYOSHIFUJI Hideaki 	br_group.u.ip4 = group;
17888ef2a9a5SYOSHIFUJI Hideaki 	br_group.proto = htons(ETH_P_IP);
1789b0e9a30dSVlad Yasevich 	br_group.vid = vid;
17908ef2a9a5SYOSHIFUJI Hideaki 
179190010b36SLinus Lüssing 	br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
17926db6f0eaSFelix Fietkau 				 own_query, src);
17938ef2a9a5SYOSHIFUJI Hideaki }
17948ef2a9a5SYOSHIFUJI Hideaki 
1795dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
179608b202b6SYOSHIFUJI Hideaki static void br_ip6_multicast_leave_group(struct net_bridge *br,
179708b202b6SYOSHIFUJI Hideaki 					 struct net_bridge_port *port,
1798b0e9a30dSVlad Yasevich 					 const struct in6_addr *group,
17996db6f0eaSFelix Fietkau 					 __u16 vid,
18006db6f0eaSFelix Fietkau 					 const unsigned char *src)
180108b202b6SYOSHIFUJI Hideaki {
180208b202b6SYOSHIFUJI Hideaki 	struct br_ip br_group;
180390010b36SLinus Lüssing 	struct bridge_mcast_own_query *own_query;
180408b202b6SYOSHIFUJI Hideaki 
18053c3769e6SLinus Lüssing 	if (ipv6_addr_is_ll_all_nodes(group))
180608b202b6SYOSHIFUJI Hideaki 		return;
180708b202b6SYOSHIFUJI Hideaki 
180890010b36SLinus Lüssing 	own_query = port ? &port->ip6_own_query : &br->ip6_own_query;
180990010b36SLinus Lüssing 
18101515a63fSNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
18114e3fd7a0SAlexey Dobriyan 	br_group.u.ip6 = *group;
181208b202b6SYOSHIFUJI Hideaki 	br_group.proto = htons(ETH_P_IPV6);
1813b0e9a30dSVlad Yasevich 	br_group.vid = vid;
181408b202b6SYOSHIFUJI Hideaki 
181590010b36SLinus Lüssing 	br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
18166db6f0eaSFelix Fietkau 				 own_query, src);
181708b202b6SYOSHIFUJI Hideaki }
181808b202b6SYOSHIFUJI Hideaki #endif
18198ef2a9a5SYOSHIFUJI Hideaki 
18201080ab95SNikolay Aleksandrov static void br_multicast_err_count(const struct net_bridge *br,
18211080ab95SNikolay Aleksandrov 				   const struct net_bridge_port *p,
18221080ab95SNikolay Aleksandrov 				   __be16 proto)
18231080ab95SNikolay Aleksandrov {
18241080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
18251080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats *pstats;
18261080ab95SNikolay Aleksandrov 
1827675779adSNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
18281080ab95SNikolay Aleksandrov 		return;
18291080ab95SNikolay Aleksandrov 
18301080ab95SNikolay Aleksandrov 	if (p)
18311080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
18321080ab95SNikolay Aleksandrov 	else
18331080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
18341080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
18351080ab95SNikolay Aleksandrov 		return;
18361080ab95SNikolay Aleksandrov 
18371080ab95SNikolay Aleksandrov 	pstats = this_cpu_ptr(stats);
18381080ab95SNikolay Aleksandrov 
18391080ab95SNikolay Aleksandrov 	u64_stats_update_begin(&pstats->syncp);
18401080ab95SNikolay Aleksandrov 	switch (proto) {
18411080ab95SNikolay Aleksandrov 	case htons(ETH_P_IP):
18421080ab95SNikolay Aleksandrov 		pstats->mstats.igmp_parse_errors++;
18431080ab95SNikolay Aleksandrov 		break;
18441080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
18451080ab95SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
18461080ab95SNikolay Aleksandrov 		pstats->mstats.mld_parse_errors++;
18471080ab95SNikolay Aleksandrov 		break;
18481080ab95SNikolay Aleksandrov #endif
18491080ab95SNikolay Aleksandrov 	}
18501080ab95SNikolay Aleksandrov 	u64_stats_update_end(&pstats->syncp);
18511080ab95SNikolay Aleksandrov }
18521080ab95SNikolay Aleksandrov 
185391b02d3dSNikolay Aleksandrov static void br_multicast_pim(struct net_bridge *br,
185491b02d3dSNikolay Aleksandrov 			     struct net_bridge_port *port,
185591b02d3dSNikolay Aleksandrov 			     const struct sk_buff *skb)
185691b02d3dSNikolay Aleksandrov {
185791b02d3dSNikolay Aleksandrov 	unsigned int offset = skb_transport_offset(skb);
185891b02d3dSNikolay Aleksandrov 	struct pimhdr *pimhdr, _pimhdr;
185991b02d3dSNikolay Aleksandrov 
186091b02d3dSNikolay Aleksandrov 	pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr);
186191b02d3dSNikolay Aleksandrov 	if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION ||
186291b02d3dSNikolay Aleksandrov 	    pim_hdr_type(pimhdr) != PIM_TYPE_HELLO)
186391b02d3dSNikolay Aleksandrov 		return;
186491b02d3dSNikolay Aleksandrov 
186591b02d3dSNikolay Aleksandrov 	br_multicast_mark_router(br, port);
186691b02d3dSNikolay Aleksandrov }
186791b02d3dSNikolay Aleksandrov 
18684b3087c7SLinus Lüssing static int br_ip4_multicast_mrd_rcv(struct net_bridge *br,
18694b3087c7SLinus Lüssing 				    struct net_bridge_port *port,
18704b3087c7SLinus Lüssing 				    struct sk_buff *skb)
18714b3087c7SLinus Lüssing {
18724b3087c7SLinus Lüssing 	if (ip_hdr(skb)->protocol != IPPROTO_IGMP ||
18734b3087c7SLinus Lüssing 	    igmp_hdr(skb)->type != IGMP_MRDISC_ADV)
18744b3087c7SLinus Lüssing 		return -ENOMSG;
18754b3087c7SLinus Lüssing 
18764b3087c7SLinus Lüssing 	br_multicast_mark_router(br, port);
18774b3087c7SLinus Lüssing 
18784b3087c7SLinus Lüssing 	return 0;
18794b3087c7SLinus Lüssing }
18804b3087c7SLinus Lüssing 
1881eb1d1641SHerbert Xu static int br_multicast_ipv4_rcv(struct net_bridge *br,
1882eb1d1641SHerbert Xu 				 struct net_bridge_port *port,
188306499098SVlad Yasevich 				 struct sk_buff *skb,
188406499098SVlad Yasevich 				 u16 vid)
1885eb1d1641SHerbert Xu {
18866db6f0eaSFelix Fietkau 	const unsigned char *src;
1887eb1d1641SHerbert Xu 	struct igmphdr *ih;
1888eb1d1641SHerbert Xu 	int err;
1889eb1d1641SHerbert Xu 
1890ba5ea614SLinus Lüssing 	err = ip_mc_check_igmp(skb);
1891eb1d1641SHerbert Xu 
18929afd85c9SLinus Lüssing 	if (err == -ENOMSG) {
189391b02d3dSNikolay Aleksandrov 		if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) {
1894bd4265feSHerbert Xu 			BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
189591b02d3dSNikolay Aleksandrov 		} else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) {
189691b02d3dSNikolay Aleksandrov 			if (ip_hdr(skb)->protocol == IPPROTO_PIM)
189791b02d3dSNikolay Aleksandrov 				br_multicast_pim(br, port, skb);
18984b3087c7SLinus Lüssing 		} else if (ipv4_is_all_snoopers(ip_hdr(skb)->daddr)) {
189908e71623SLi RongQing 			br_ip4_multicast_mrd_rcv(br, port, skb);
19004b3087c7SLinus Lüssing 		}
19014b3087c7SLinus Lüssing 
1902eb1d1641SHerbert Xu 		return 0;
19039afd85c9SLinus Lüssing 	} else if (err < 0) {
19041080ab95SNikolay Aleksandrov 		br_multicast_err_count(br, port, skb->protocol);
19059afd85c9SLinus Lüssing 		return err;
1906bd4265feSHerbert Xu 	}
1907eb1d1641SHerbert Xu 
19089afd85c9SLinus Lüssing 	ih = igmp_hdr(skb);
19096db6f0eaSFelix Fietkau 	src = eth_hdr(skb)->h_source;
19101080ab95SNikolay Aleksandrov 	BR_INPUT_SKB_CB(skb)->igmp = ih->type;
1911eb1d1641SHerbert Xu 
1912eb1d1641SHerbert Xu 	switch (ih->type) {
1913eb1d1641SHerbert Xu 	case IGMP_HOST_MEMBERSHIP_REPORT:
1914eb1d1641SHerbert Xu 	case IGMPV2_HOST_MEMBERSHIP_REPORT:
191562b2bcb4SFernando Luis Vázquez Cao 		BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
19168b671779SNikolay Aleksandrov 		err = br_ip4_multicast_add_group(br, port, ih->group, vid, src,
19178b671779SNikolay Aleksandrov 						 true);
1918eb1d1641SHerbert Xu 		break;
1919eb1d1641SHerbert Xu 	case IGMPV3_HOST_MEMBERSHIP_REPORT:
1920ba5ea614SLinus Lüssing 		err = br_ip4_multicast_igmp3_report(br, port, skb, vid);
1921eb1d1641SHerbert Xu 		break;
1922eb1d1641SHerbert Xu 	case IGMP_HOST_MEMBERSHIP_QUERY:
1923ba5ea614SLinus Lüssing 		br_ip4_multicast_query(br, port, skb, vid);
1924eb1d1641SHerbert Xu 		break;
1925eb1d1641SHerbert Xu 	case IGMP_HOST_LEAVE_MESSAGE:
19266db6f0eaSFelix Fietkau 		br_ip4_multicast_leave_group(br, port, ih->group, vid, src);
1927eb1d1641SHerbert Xu 		break;
1928eb1d1641SHerbert Xu 	}
1929eb1d1641SHerbert Xu 
1930a65056ecSNikolay Aleksandrov 	br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
19311080ab95SNikolay Aleksandrov 			   BR_MCAST_DIR_RX);
19321080ab95SNikolay Aleksandrov 
1933eb1d1641SHerbert Xu 	return err;
1934eb1d1641SHerbert Xu }
1935eb1d1641SHerbert Xu 
1936dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
19374b3087c7SLinus Lüssing static int br_ip6_multicast_mrd_rcv(struct net_bridge *br,
19384b3087c7SLinus Lüssing 				    struct net_bridge_port *port,
19394b3087c7SLinus Lüssing 				    struct sk_buff *skb)
19404b3087c7SLinus Lüssing {
19414b3087c7SLinus Lüssing 	int ret;
19424b3087c7SLinus Lüssing 
19434b3087c7SLinus Lüssing 	if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6)
19444b3087c7SLinus Lüssing 		return -ENOMSG;
19454b3087c7SLinus Lüssing 
19464b3087c7SLinus Lüssing 	ret = ipv6_mc_check_icmpv6(skb);
19474b3087c7SLinus Lüssing 	if (ret < 0)
19484b3087c7SLinus Lüssing 		return ret;
19494b3087c7SLinus Lüssing 
19504b3087c7SLinus Lüssing 	if (icmp6_hdr(skb)->icmp6_type != ICMPV6_MRDISC_ADV)
19514b3087c7SLinus Lüssing 		return -ENOMSG;
19524b3087c7SLinus Lüssing 
19534b3087c7SLinus Lüssing 	br_multicast_mark_router(br, port);
19544b3087c7SLinus Lüssing 
19554b3087c7SLinus Lüssing 	return 0;
19564b3087c7SLinus Lüssing }
19574b3087c7SLinus Lüssing 
195808b202b6SYOSHIFUJI Hideaki static int br_multicast_ipv6_rcv(struct net_bridge *br,
195908b202b6SYOSHIFUJI Hideaki 				 struct net_bridge_port *port,
196006499098SVlad Yasevich 				 struct sk_buff *skb,
196106499098SVlad Yasevich 				 u16 vid)
196208b202b6SYOSHIFUJI Hideaki {
19636db6f0eaSFelix Fietkau 	const unsigned char *src;
19649afd85c9SLinus Lüssing 	struct mld_msg *mld;
196508b202b6SYOSHIFUJI Hideaki 	int err;
196608b202b6SYOSHIFUJI Hideaki 
1967ba5ea614SLinus Lüssing 	err = ipv6_mc_check_mld(skb);
196808b202b6SYOSHIFUJI Hideaki 
19699afd85c9SLinus Lüssing 	if (err == -ENOMSG) {
19709afd85c9SLinus Lüssing 		if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
19718fad9c39SLinus Lüssing 			BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
19724b3087c7SLinus Lüssing 
19734b3087c7SLinus Lüssing 		if (ipv6_addr_is_all_snoopers(&ipv6_hdr(skb)->daddr)) {
19744b3087c7SLinus Lüssing 			err = br_ip6_multicast_mrd_rcv(br, port, skb);
19754b3087c7SLinus Lüssing 
19764b3087c7SLinus Lüssing 			if (err < 0 && err != -ENOMSG) {
19774b3087c7SLinus Lüssing 				br_multicast_err_count(br, port, skb->protocol);
19784b3087c7SLinus Lüssing 				return err;
19794b3087c7SLinus Lüssing 			}
19804b3087c7SLinus Lüssing 		}
19814b3087c7SLinus Lüssing 
198208b202b6SYOSHIFUJI Hideaki 		return 0;
19839afd85c9SLinus Lüssing 	} else if (err < 0) {
19841080ab95SNikolay Aleksandrov 		br_multicast_err_count(br, port, skb->protocol);
19859afd85c9SLinus Lüssing 		return err;
198608b202b6SYOSHIFUJI Hideaki 	}
198708b202b6SYOSHIFUJI Hideaki 
19889afd85c9SLinus Lüssing 	mld = (struct mld_msg *)skb_transport_header(skb);
19891080ab95SNikolay Aleksandrov 	BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type;
199008b202b6SYOSHIFUJI Hideaki 
19919afd85c9SLinus Lüssing 	switch (mld->mld_type) {
199208b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_REPORT:
19936db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
1994fc2af6c7SFernando Luis Vázquez Cao 		BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
19956db6f0eaSFelix Fietkau 		err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid,
19968b671779SNikolay Aleksandrov 						 src, true);
199708b202b6SYOSHIFUJI Hideaki 		break;
199808b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MLD2_REPORT:
1999ba5ea614SLinus Lüssing 		err = br_ip6_multicast_mld2_report(br, port, skb, vid);
200008b202b6SYOSHIFUJI Hideaki 		break;
200108b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_QUERY:
2002ba5ea614SLinus Lüssing 		err = br_ip6_multicast_query(br, port, skb, vid);
200308b202b6SYOSHIFUJI Hideaki 		break;
200408b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_REDUCTION:
20056db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
20066db6f0eaSFelix Fietkau 		br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src);
20079afd85c9SLinus Lüssing 		break;
200808b202b6SYOSHIFUJI Hideaki 	}
200908b202b6SYOSHIFUJI Hideaki 
2010a65056ecSNikolay Aleksandrov 	br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
20111080ab95SNikolay Aleksandrov 			   BR_MCAST_DIR_RX);
20121080ab95SNikolay Aleksandrov 
201308b202b6SYOSHIFUJI Hideaki 	return err;
201408b202b6SYOSHIFUJI Hideaki }
201508b202b6SYOSHIFUJI Hideaki #endif
201608b202b6SYOSHIFUJI Hideaki 
2017eb1d1641SHerbert Xu int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
201806499098SVlad Yasevich 		     struct sk_buff *skb, u16 vid)
2019eb1d1641SHerbert Xu {
20201080ab95SNikolay Aleksandrov 	int ret = 0;
20211080ab95SNikolay Aleksandrov 
20221fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 	BR_INPUT_SKB_CB(skb)->igmp = 0;
20231fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 	BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
20241fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 
202513cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
2026eb1d1641SHerbert Xu 		return 0;
2027eb1d1641SHerbert Xu 
2028eb1d1641SHerbert Xu 	switch (skb->protocol) {
2029eb1d1641SHerbert Xu 	case htons(ETH_P_IP):
20301080ab95SNikolay Aleksandrov 		ret = br_multicast_ipv4_rcv(br, port, skb, vid);
20311080ab95SNikolay Aleksandrov 		break;
2032dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
203308b202b6SYOSHIFUJI Hideaki 	case htons(ETH_P_IPV6):
20341080ab95SNikolay Aleksandrov 		ret = br_multicast_ipv6_rcv(br, port, skb, vid);
20351080ab95SNikolay Aleksandrov 		break;
203608b202b6SYOSHIFUJI Hideaki #endif
2037eb1d1641SHerbert Xu 	}
2038eb1d1641SHerbert Xu 
20391080ab95SNikolay Aleksandrov 	return ret;
2040eb1d1641SHerbert Xu }
2041eb1d1641SHerbert Xu 
2042cc0fdd80SLinus Lüssing static void br_multicast_query_expired(struct net_bridge *br,
20432cd41431SLinus Lüssing 				       struct bridge_mcast_own_query *query,
20442cd41431SLinus Lüssing 				       struct bridge_mcast_querier *querier)
2045cc0fdd80SLinus Lüssing {
2046cc0fdd80SLinus Lüssing 	spin_lock(&br->multicast_lock);
2047cc0fdd80SLinus Lüssing 	if (query->startup_sent < br->multicast_startup_query_count)
2048cc0fdd80SLinus Lüssing 		query->startup_sent++;
2049cc0fdd80SLinus Lüssing 
205071d9f614SEric Dumazet 	RCU_INIT_POINTER(querier->port, NULL);
2051cc0fdd80SLinus Lüssing 	br_multicast_send_query(br, NULL, query);
2052cc0fdd80SLinus Lüssing 	spin_unlock(&br->multicast_lock);
2053cc0fdd80SLinus Lüssing }
2054cc0fdd80SLinus Lüssing 
205588c1f37fSAllen Pais static void br_ip4_multicast_query_expired(struct timer_list *t)
2056eb1d1641SHerbert Xu {
205788c1f37fSAllen Pais 	struct net_bridge *br = from_timer(br, t, ip4_own_query.timer);
2058eb1d1641SHerbert Xu 
20592cd41431SLinus Lüssing 	br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier);
2060eb1d1641SHerbert Xu }
2061eb1d1641SHerbert Xu 
2062cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
206388c1f37fSAllen Pais static void br_ip6_multicast_query_expired(struct timer_list *t)
2064cc0fdd80SLinus Lüssing {
206588c1f37fSAllen Pais 	struct net_bridge *br = from_timer(br, t, ip6_own_query.timer);
2066cc0fdd80SLinus Lüssing 
20672cd41431SLinus Lüssing 	br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier);
2068cc0fdd80SLinus Lüssing }
2069cc0fdd80SLinus Lüssing #endif
2070cc0fdd80SLinus Lüssing 
20718b671779SNikolay Aleksandrov static void __grp_src_gc(struct hlist_head *head)
20728b671779SNikolay Aleksandrov {
20738b671779SNikolay Aleksandrov 	struct net_bridge_group_src *ent;
20748b671779SNikolay Aleksandrov 	struct hlist_node *tmp;
20758b671779SNikolay Aleksandrov 
20768b671779SNikolay Aleksandrov 	hlist_for_each_entry_safe(ent, tmp, head, del_node) {
20778b671779SNikolay Aleksandrov 		hlist_del_init(&ent->del_node);
20788b671779SNikolay Aleksandrov 		del_timer_sync(&ent->timer);
20798b671779SNikolay Aleksandrov 		kfree_rcu(ent, rcu);
20808b671779SNikolay Aleksandrov 	}
20818b671779SNikolay Aleksandrov }
20828b671779SNikolay Aleksandrov 
20838b671779SNikolay Aleksandrov static void br_multicast_src_gc(struct work_struct *work)
20848b671779SNikolay Aleksandrov {
20858b671779SNikolay Aleksandrov 	struct net_bridge *br = container_of(work, struct net_bridge,
20868b671779SNikolay Aleksandrov 					     src_gc_work);
20878b671779SNikolay Aleksandrov 	HLIST_HEAD(deleted_head);
20888b671779SNikolay Aleksandrov 
20898b671779SNikolay Aleksandrov 	spin_lock_bh(&br->multicast_lock);
20908b671779SNikolay Aleksandrov 	hlist_move_list(&br->src_gc_list, &deleted_head);
20918b671779SNikolay Aleksandrov 	spin_unlock_bh(&br->multicast_lock);
20928b671779SNikolay Aleksandrov 
20938b671779SNikolay Aleksandrov 	__grp_src_gc(&deleted_head);
20948b671779SNikolay Aleksandrov }
20958b671779SNikolay Aleksandrov 
2096eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br)
2097eb1d1641SHerbert Xu {
2098d08c6bc0SNikolay Aleksandrov 	br->hash_max = BR_MULTICAST_DEFAULT_HASH_MAX;
2099eb1d1641SHerbert Xu 
21007f0aec7aSNikolay Aleksandrov 	br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
2101eb1d1641SHerbert Xu 	br->multicast_last_member_count = 2;
2102eb1d1641SHerbert Xu 	br->multicast_startup_query_count = 2;
2103eb1d1641SHerbert Xu 
2104eb1d1641SHerbert Xu 	br->multicast_last_member_interval = HZ;
2105eb1d1641SHerbert Xu 	br->multicast_query_response_interval = 10 * HZ;
2106eb1d1641SHerbert Xu 	br->multicast_startup_query_interval = 125 * HZ / 4;
2107eb1d1641SHerbert Xu 	br->multicast_query_interval = 125 * HZ;
2108eb1d1641SHerbert Xu 	br->multicast_querier_interval = 255 * HZ;
2109eb1d1641SHerbert Xu 	br->multicast_membership_interval = 260 * HZ;
2110eb1d1641SHerbert Xu 
211190010b36SLinus Lüssing 	br->ip4_other_query.delay_time = 0;
21122cd41431SLinus Lüssing 	br->ip4_querier.port = NULL;
2113aa2ae3e7SNikolay Aleksandrov 	br->multicast_igmp_version = 2;
2114cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
2115aa2ae3e7SNikolay Aleksandrov 	br->multicast_mld_version = 1;
211690010b36SLinus Lüssing 	br->ip6_other_query.delay_time = 0;
21172cd41431SLinus Lüssing 	br->ip6_querier.port = NULL;
2118cc0fdd80SLinus Lüssing #endif
21196919622aSIdo Schimmel 	br_opt_toggle(br, BROPT_MULTICAST_ENABLED, true);
2120675779adSNikolay Aleksandrov 	br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true);
2121b00589afSLinus Lüssing 
2122eb1d1641SHerbert Xu 	spin_lock_init(&br->multicast_lock);
212388c1f37fSAllen Pais 	timer_setup(&br->multicast_router_timer,
212488c1f37fSAllen Pais 		    br_multicast_local_router_expired, 0);
212588c1f37fSAllen Pais 	timer_setup(&br->ip4_other_query.timer,
212688c1f37fSAllen Pais 		    br_ip4_multicast_querier_expired, 0);
212788c1f37fSAllen Pais 	timer_setup(&br->ip4_own_query.timer,
212888c1f37fSAllen Pais 		    br_ip4_multicast_query_expired, 0);
2129cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
213088c1f37fSAllen Pais 	timer_setup(&br->ip6_other_query.timer,
213188c1f37fSAllen Pais 		    br_ip6_multicast_querier_expired, 0);
213288c1f37fSAllen Pais 	timer_setup(&br->ip6_own_query.timer,
213388c1f37fSAllen Pais 		    br_ip6_multicast_query_expired, 0);
2134cc0fdd80SLinus Lüssing #endif
213519e3a9c9SNikolay Aleksandrov 	INIT_HLIST_HEAD(&br->mdb_list);
21368b671779SNikolay Aleksandrov 	INIT_HLIST_HEAD(&br->src_gc_list);
21378b671779SNikolay Aleksandrov 	INIT_WORK(&br->src_gc_work, br_multicast_src_gc);
2138eb1d1641SHerbert Xu }
2139eb1d1641SHerbert Xu 
21404effd28cSLinus Lüssing static void br_ip4_multicast_join_snoopers(struct net_bridge *br)
21414effd28cSLinus Lüssing {
21424effd28cSLinus Lüssing 	struct in_device *in_dev = in_dev_get(br->dev);
21434effd28cSLinus Lüssing 
21444effd28cSLinus Lüssing 	if (!in_dev)
21454effd28cSLinus Lüssing 		return;
21464effd28cSLinus Lüssing 
21479fb20801SFlorian Fainelli 	__ip_mc_inc_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
21484effd28cSLinus Lüssing 	in_dev_put(in_dev);
21494effd28cSLinus Lüssing }
21504effd28cSLinus Lüssing 
21514effd28cSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
21524effd28cSLinus Lüssing static void br_ip6_multicast_join_snoopers(struct net_bridge *br)
21534effd28cSLinus Lüssing {
21544effd28cSLinus Lüssing 	struct in6_addr addr;
21554effd28cSLinus Lüssing 
21564effd28cSLinus Lüssing 	ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
21574effd28cSLinus Lüssing 	ipv6_dev_mc_inc(br->dev, &addr);
21584effd28cSLinus Lüssing }
21594effd28cSLinus Lüssing #else
21604effd28cSLinus Lüssing static inline void br_ip6_multicast_join_snoopers(struct net_bridge *br)
21614effd28cSLinus Lüssing {
21624effd28cSLinus Lüssing }
21634effd28cSLinus Lüssing #endif
21644effd28cSLinus Lüssing 
21654effd28cSLinus Lüssing static void br_multicast_join_snoopers(struct net_bridge *br)
21664effd28cSLinus Lüssing {
21674effd28cSLinus Lüssing 	br_ip4_multicast_join_snoopers(br);
21684effd28cSLinus Lüssing 	br_ip6_multicast_join_snoopers(br);
21694effd28cSLinus Lüssing }
21704effd28cSLinus Lüssing 
21714effd28cSLinus Lüssing static void br_ip4_multicast_leave_snoopers(struct net_bridge *br)
21724effd28cSLinus Lüssing {
21734effd28cSLinus Lüssing 	struct in_device *in_dev = in_dev_get(br->dev);
21744effd28cSLinus Lüssing 
21754effd28cSLinus Lüssing 	if (WARN_ON(!in_dev))
21764effd28cSLinus Lüssing 		return;
21774effd28cSLinus Lüssing 
21789fb20801SFlorian Fainelli 	__ip_mc_dec_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
21794effd28cSLinus Lüssing 	in_dev_put(in_dev);
21804effd28cSLinus Lüssing }
21814effd28cSLinus Lüssing 
21824effd28cSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
21834effd28cSLinus Lüssing static void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
21844effd28cSLinus Lüssing {
21854effd28cSLinus Lüssing 	struct in6_addr addr;
21864effd28cSLinus Lüssing 
21874effd28cSLinus Lüssing 	ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
21884effd28cSLinus Lüssing 	ipv6_dev_mc_dec(br->dev, &addr);
21894effd28cSLinus Lüssing }
21904effd28cSLinus Lüssing #else
21914effd28cSLinus Lüssing static inline void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
21924effd28cSLinus Lüssing {
21934effd28cSLinus Lüssing }
21944effd28cSLinus Lüssing #endif
21954effd28cSLinus Lüssing 
21964effd28cSLinus Lüssing static void br_multicast_leave_snoopers(struct net_bridge *br)
21974effd28cSLinus Lüssing {
21984effd28cSLinus Lüssing 	br_ip4_multicast_leave_snoopers(br);
21994effd28cSLinus Lüssing 	br_ip6_multicast_leave_snoopers(br);
22004effd28cSLinus Lüssing }
22014effd28cSLinus Lüssing 
2202cc0fdd80SLinus Lüssing static void __br_multicast_open(struct net_bridge *br,
220390010b36SLinus Lüssing 				struct bridge_mcast_own_query *query)
2204eb1d1641SHerbert Xu {
2205cc0fdd80SLinus Lüssing 	query->startup_sent = 0;
2206eb1d1641SHerbert Xu 
220713cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
2208eb1d1641SHerbert Xu 		return;
2209eb1d1641SHerbert Xu 
2210cc0fdd80SLinus Lüssing 	mod_timer(&query->timer, jiffies);
2211cc0fdd80SLinus Lüssing }
2212cc0fdd80SLinus Lüssing 
2213cc0fdd80SLinus Lüssing void br_multicast_open(struct net_bridge *br)
2214cc0fdd80SLinus Lüssing {
22154effd28cSLinus Lüssing 	if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
22164effd28cSLinus Lüssing 		br_multicast_join_snoopers(br);
22174effd28cSLinus Lüssing 
221890010b36SLinus Lüssing 	__br_multicast_open(br, &br->ip4_own_query);
2219cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
222090010b36SLinus Lüssing 	__br_multicast_open(br, &br->ip6_own_query);
2221cc0fdd80SLinus Lüssing #endif
2222eb1d1641SHerbert Xu }
2223eb1d1641SHerbert Xu 
2224eb1d1641SHerbert Xu void br_multicast_stop(struct net_bridge *br)
2225eb1d1641SHerbert Xu {
2226eb1d1641SHerbert Xu 	del_timer_sync(&br->multicast_router_timer);
222790010b36SLinus Lüssing 	del_timer_sync(&br->ip4_other_query.timer);
222890010b36SLinus Lüssing 	del_timer_sync(&br->ip4_own_query.timer);
2229cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
223090010b36SLinus Lüssing 	del_timer_sync(&br->ip6_other_query.timer);
223190010b36SLinus Lüssing 	del_timer_sync(&br->ip6_own_query.timer);
2232cc0fdd80SLinus Lüssing #endif
22334effd28cSLinus Lüssing 
22344effd28cSLinus Lüssing 	if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
22354effd28cSLinus Lüssing 		br_multicast_leave_snoopers(br);
2236e10177abSSatish Ashok }
2237e10177abSSatish Ashok 
2238e10177abSSatish Ashok void br_multicast_dev_del(struct net_bridge *br)
2239e10177abSSatish Ashok {
2240e10177abSSatish Ashok 	struct net_bridge_mdb_entry *mp;
22418b671779SNikolay Aleksandrov 	HLIST_HEAD(deleted_head);
224219e3a9c9SNikolay Aleksandrov 	struct hlist_node *tmp;
2243eb1d1641SHerbert Xu 
2244eb1d1641SHerbert Xu 	spin_lock_bh(&br->multicast_lock);
224519e3a9c9SNikolay Aleksandrov 	hlist_for_each_entry_safe(mp, tmp, &br->mdb_list, mdb_node) {
2246eb1d1641SHerbert Xu 		del_timer(&mp->timer);
224719e3a9c9SNikolay Aleksandrov 		rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode,
224819e3a9c9SNikolay Aleksandrov 				       br_mdb_rht_params);
224919e3a9c9SNikolay Aleksandrov 		hlist_del_rcu(&mp->mdb_node);
22504329596cSNikolay Aleksandrov 		kfree_rcu(mp, rcu);
2251eb1d1641SHerbert Xu 	}
22528b671779SNikolay Aleksandrov 	hlist_move_list(&br->src_gc_list, &deleted_head);
2253eb1d1641SHerbert Xu 	spin_unlock_bh(&br->multicast_lock);
225419e3a9c9SNikolay Aleksandrov 
22558b671779SNikolay Aleksandrov 	__grp_src_gc(&deleted_head);
22568b671779SNikolay Aleksandrov 	cancel_work_sync(&br->src_gc_work);
22578b671779SNikolay Aleksandrov 
22584329596cSNikolay Aleksandrov 	rcu_barrier();
2259eb1d1641SHerbert Xu }
22600909e117SHerbert Xu 
22610909e117SHerbert Xu int br_multicast_set_router(struct net_bridge *br, unsigned long val)
22620909e117SHerbert Xu {
22636ae4ae8eSLinus Lüssing 	int err = -EINVAL;
22640909e117SHerbert Xu 
22650909e117SHerbert Xu 	spin_lock_bh(&br->multicast_lock);
22660909e117SHerbert Xu 
22670909e117SHerbert Xu 	switch (val) {
22687f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_DISABLED:
22697f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_PERM:
227077041420SYotam Gigi 		br_mc_router_state_change(br, val == MDB_RTR_TYPE_PERM);
22710909e117SHerbert Xu 		del_timer(&br->multicast_router_timer);
227277041420SYotam Gigi 		br->multicast_router = val;
227377041420SYotam Gigi 		err = 0;
227477041420SYotam Gigi 		break;
22757f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP_QUERY:
227677041420SYotam Gigi 		if (br->multicast_router != MDB_RTR_TYPE_TEMP_QUERY)
227777041420SYotam Gigi 			br_mc_router_state_change(br, false);
22780909e117SHerbert Xu 		br->multicast_router = val;
22790909e117SHerbert Xu 		err = 0;
22800909e117SHerbert Xu 		break;
22810909e117SHerbert Xu 	}
22820909e117SHerbert Xu 
22830909e117SHerbert Xu 	spin_unlock_bh(&br->multicast_lock);
22840909e117SHerbert Xu 
22850909e117SHerbert Xu 	return err;
22860909e117SHerbert Xu }
22870909e117SHerbert Xu 
22887f0aec7aSNikolay Aleksandrov static void __del_port_router(struct net_bridge_port *p)
22897f0aec7aSNikolay Aleksandrov {
22907f0aec7aSNikolay Aleksandrov 	if (hlist_unhashed(&p->rlist))
22917f0aec7aSNikolay Aleksandrov 		return;
22927f0aec7aSNikolay Aleksandrov 	hlist_del_init_rcu(&p->rlist);
22937f0aec7aSNikolay Aleksandrov 	br_rtr_notify(p->br->dev, p, RTM_DELMDB);
22946d549648SNogah Frankel 	br_port_mc_router_state_change(p, false);
2295f12e7d95SNogah Frankel 
2296f12e7d95SNogah Frankel 	/* don't allow timer refresh */
2297f12e7d95SNogah Frankel 	if (p->multicast_router == MDB_RTR_TYPE_TEMP)
2298f12e7d95SNogah Frankel 		p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
22997f0aec7aSNikolay Aleksandrov }
23007f0aec7aSNikolay Aleksandrov 
23010909e117SHerbert Xu int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
23020909e117SHerbert Xu {
23030909e117SHerbert Xu 	struct net_bridge *br = p->br;
2304a55d8246SNikolay Aleksandrov 	unsigned long now = jiffies;
23056ae4ae8eSLinus Lüssing 	int err = -EINVAL;
23060909e117SHerbert Xu 
23070909e117SHerbert Xu 	spin_lock(&br->multicast_lock);
23084950cfd1SNikolay Aleksandrov 	if (p->multicast_router == val) {
2309a55d8246SNikolay Aleksandrov 		/* Refresh the temp router port timer */
2310a55d8246SNikolay Aleksandrov 		if (p->multicast_router == MDB_RTR_TYPE_TEMP)
2311a55d8246SNikolay Aleksandrov 			mod_timer(&p->multicast_router_timer,
2312a55d8246SNikolay Aleksandrov 				  now + br->multicast_querier_interval);
23134950cfd1SNikolay Aleksandrov 		err = 0;
23144950cfd1SNikolay Aleksandrov 		goto unlock;
23154950cfd1SNikolay Aleksandrov 	}
23160909e117SHerbert Xu 	switch (val) {
23177f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_DISABLED:
23187f0aec7aSNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_DISABLED;
23197f0aec7aSNikolay Aleksandrov 		__del_port_router(p);
23200909e117SHerbert Xu 		del_timer(&p->multicast_router_timer);
23210909e117SHerbert Xu 		break;
23227f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP_QUERY:
23237f0aec7aSNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
23247f0aec7aSNikolay Aleksandrov 		__del_port_router(p);
23257f0aec7aSNikolay Aleksandrov 		break;
23267f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_PERM:
23277f0aec7aSNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_PERM;
23287f0aec7aSNikolay Aleksandrov 		del_timer(&p->multicast_router_timer);
23290909e117SHerbert Xu 		br_multicast_add_router(br, p);
23300909e117SHerbert Xu 		break;
2331a55d8246SNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP:
2332a55d8246SNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_TEMP;
2333a55d8246SNikolay Aleksandrov 		br_multicast_mark_router(br, p);
2334a55d8246SNikolay Aleksandrov 		break;
23357f0aec7aSNikolay Aleksandrov 	default:
23367f0aec7aSNikolay Aleksandrov 		goto unlock;
23370909e117SHerbert Xu 	}
23387f0aec7aSNikolay Aleksandrov 	err = 0;
23397f0aec7aSNikolay Aleksandrov unlock:
23400909e117SHerbert Xu 	spin_unlock(&br->multicast_lock);
23410909e117SHerbert Xu 
23420909e117SHerbert Xu 	return err;
23430909e117SHerbert Xu }
2344561f1103SHerbert Xu 
2345cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br,
234690010b36SLinus Lüssing 				       struct bridge_mcast_own_query *query)
2347561f1103SHerbert Xu {
2348561f1103SHerbert Xu 	struct net_bridge_port *port;
234974857216SHerbert Xu 
2350cc0fdd80SLinus Lüssing 	__br_multicast_open(br, query);
235174857216SHerbert Xu 
2352c5b493ceSNikolay Aleksandrov 	rcu_read_lock();
2353c5b493ceSNikolay Aleksandrov 	list_for_each_entry_rcu(port, &br->port_list, list) {
235474857216SHerbert Xu 		if (port->state == BR_STATE_DISABLED ||
235574857216SHerbert Xu 		    port->state == BR_STATE_BLOCKING)
235674857216SHerbert Xu 			continue;
235774857216SHerbert Xu 
235890010b36SLinus Lüssing 		if (query == &br->ip4_own_query)
235990010b36SLinus Lüssing 			br_multicast_enable(&port->ip4_own_query);
2360cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
2361cc0fdd80SLinus Lüssing 		else
236290010b36SLinus Lüssing 			br_multicast_enable(&port->ip6_own_query);
2363cc0fdd80SLinus Lüssing #endif
236474857216SHerbert Xu 	}
2365c5b493ceSNikolay Aleksandrov 	rcu_read_unlock();
236674857216SHerbert Xu }
236774857216SHerbert Xu 
236874857216SHerbert Xu int br_multicast_toggle(struct net_bridge *br, unsigned long val)
236974857216SHerbert Xu {
23707cb3f921SNikolay Aleksandrov 	struct net_bridge_port *port;
2371561f1103SHerbert Xu 
2372ef5e0d82SAndrey Vagin 	spin_lock_bh(&br->multicast_lock);
237313cefad2SNikolay Aleksandrov 	if (!!br_opt_get(br, BROPT_MULTICAST_ENABLED) == !!val)
2374561f1103SHerbert Xu 		goto unlock;
2375561f1103SHerbert Xu 
237613cefad2SNikolay Aleksandrov 	br_mc_disabled_update(br->dev, val);
237713cefad2SNikolay Aleksandrov 	br_opt_toggle(br, BROPT_MULTICAST_ENABLED, !!val);
23784effd28cSLinus Lüssing 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) {
23794effd28cSLinus Lüssing 		br_multicast_leave_snoopers(br);
2380561f1103SHerbert Xu 		goto unlock;
23814effd28cSLinus Lüssing 	}
2382561f1103SHerbert Xu 
23833a7fda06SHerbert Xu 	if (!netif_running(br->dev))
23843a7fda06SHerbert Xu 		goto unlock;
23853a7fda06SHerbert Xu 
23867cb3f921SNikolay Aleksandrov 	br_multicast_open(br);
23877cb3f921SNikolay Aleksandrov 	list_for_each_entry(port, &br->port_list, list)
23887cb3f921SNikolay Aleksandrov 		__br_multicast_enable_port(port);
2389561f1103SHerbert Xu 
2390561f1103SHerbert Xu unlock:
2391ef5e0d82SAndrey Vagin 	spin_unlock_bh(&br->multicast_lock);
2392561f1103SHerbert Xu 
2393a26d94bfSYueHaibing 	return 0;
2394561f1103SHerbert Xu }
2395b195167fSHerbert Xu 
23969341b988SIdo Schimmel bool br_multicast_enabled(const struct net_device *dev)
23979341b988SIdo Schimmel {
23989341b988SIdo Schimmel 	struct net_bridge *br = netdev_priv(dev);
23999341b988SIdo Schimmel 
240013cefad2SNikolay Aleksandrov 	return !!br_opt_get(br, BROPT_MULTICAST_ENABLED);
24019341b988SIdo Schimmel }
24029341b988SIdo Schimmel EXPORT_SYMBOL_GPL(br_multicast_enabled);
24039341b988SIdo Schimmel 
24040912bda4SYotam Gigi bool br_multicast_router(const struct net_device *dev)
24050912bda4SYotam Gigi {
24060912bda4SYotam Gigi 	struct net_bridge *br = netdev_priv(dev);
24070912bda4SYotam Gigi 	bool is_router;
24080912bda4SYotam Gigi 
24090912bda4SYotam Gigi 	spin_lock_bh(&br->multicast_lock);
24100912bda4SYotam Gigi 	is_router = br_multicast_is_router(br);
24110912bda4SYotam Gigi 	spin_unlock_bh(&br->multicast_lock);
24120912bda4SYotam Gigi 	return is_router;
24130912bda4SYotam Gigi }
24140912bda4SYotam Gigi EXPORT_SYMBOL_GPL(br_multicast_router);
24150912bda4SYotam Gigi 
2416c5c23260SHerbert Xu int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
2417c5c23260SHerbert Xu {
2418b00589afSLinus Lüssing 	unsigned long max_delay;
2419b00589afSLinus Lüssing 
2420c5c23260SHerbert Xu 	val = !!val;
2421c5c23260SHerbert Xu 
2422c5c23260SHerbert Xu 	spin_lock_bh(&br->multicast_lock);
2423675779adSNikolay Aleksandrov 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER) == val)
2424c5c23260SHerbert Xu 		goto unlock;
2425c5c23260SHerbert Xu 
2426675779adSNikolay Aleksandrov 	br_opt_toggle(br, BROPT_MULTICAST_QUERIER, !!val);
2427b00589afSLinus Lüssing 	if (!val)
2428b00589afSLinus Lüssing 		goto unlock;
2429b00589afSLinus Lüssing 
2430b00589afSLinus Lüssing 	max_delay = br->multicast_query_response_interval;
2431b00589afSLinus Lüssing 
243290010b36SLinus Lüssing 	if (!timer_pending(&br->ip4_other_query.timer))
243390010b36SLinus Lüssing 		br->ip4_other_query.delay_time = jiffies + max_delay;
2434cc0fdd80SLinus Lüssing 
243590010b36SLinus Lüssing 	br_multicast_start_querier(br, &br->ip4_own_query);
2436cc0fdd80SLinus Lüssing 
2437cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
243890010b36SLinus Lüssing 	if (!timer_pending(&br->ip6_other_query.timer))
243990010b36SLinus Lüssing 		br->ip6_other_query.delay_time = jiffies + max_delay;
2440cc0fdd80SLinus Lüssing 
244190010b36SLinus Lüssing 	br_multicast_start_querier(br, &br->ip6_own_query);
2442cc0fdd80SLinus Lüssing #endif
2443c5c23260SHerbert Xu 
2444c5c23260SHerbert Xu unlock:
2445c5c23260SHerbert Xu 	spin_unlock_bh(&br->multicast_lock);
2446c5c23260SHerbert Xu 
2447c5c23260SHerbert Xu 	return 0;
2448c5c23260SHerbert Xu }
2449c5c23260SHerbert Xu 
24505e923585SNikolay Aleksandrov int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val)
24515e923585SNikolay Aleksandrov {
24525e923585SNikolay Aleksandrov 	/* Currently we support only version 2 and 3 */
24535e923585SNikolay Aleksandrov 	switch (val) {
24545e923585SNikolay Aleksandrov 	case 2:
24555e923585SNikolay Aleksandrov 	case 3:
24565e923585SNikolay Aleksandrov 		break;
24575e923585SNikolay Aleksandrov 	default:
24585e923585SNikolay Aleksandrov 		return -EINVAL;
24595e923585SNikolay Aleksandrov 	}
24605e923585SNikolay Aleksandrov 
24615e923585SNikolay Aleksandrov 	spin_lock_bh(&br->multicast_lock);
24625e923585SNikolay Aleksandrov 	br->multicast_igmp_version = val;
24635e923585SNikolay Aleksandrov 	spin_unlock_bh(&br->multicast_lock);
24645e923585SNikolay Aleksandrov 
24655e923585SNikolay Aleksandrov 	return 0;
24665e923585SNikolay Aleksandrov }
24675e923585SNikolay Aleksandrov 
2468aa2ae3e7SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
2469aa2ae3e7SNikolay Aleksandrov int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val)
2470aa2ae3e7SNikolay Aleksandrov {
2471aa2ae3e7SNikolay Aleksandrov 	/* Currently we support version 1 and 2 */
2472aa2ae3e7SNikolay Aleksandrov 	switch (val) {
2473aa2ae3e7SNikolay Aleksandrov 	case 1:
2474aa2ae3e7SNikolay Aleksandrov 	case 2:
2475aa2ae3e7SNikolay Aleksandrov 		break;
2476aa2ae3e7SNikolay Aleksandrov 	default:
2477aa2ae3e7SNikolay Aleksandrov 		return -EINVAL;
2478aa2ae3e7SNikolay Aleksandrov 	}
2479aa2ae3e7SNikolay Aleksandrov 
2480aa2ae3e7SNikolay Aleksandrov 	spin_lock_bh(&br->multicast_lock);
2481aa2ae3e7SNikolay Aleksandrov 	br->multicast_mld_version = val;
2482aa2ae3e7SNikolay Aleksandrov 	spin_unlock_bh(&br->multicast_lock);
2483aa2ae3e7SNikolay Aleksandrov 
2484aa2ae3e7SNikolay Aleksandrov 	return 0;
2485aa2ae3e7SNikolay Aleksandrov }
2486aa2ae3e7SNikolay Aleksandrov #endif
2487aa2ae3e7SNikolay Aleksandrov 
248807f8ac4aSLinus Lüssing /**
248907f8ac4aSLinus Lüssing  * br_multicast_list_adjacent - Returns snooped multicast addresses
249007f8ac4aSLinus Lüssing  * @dev:	The bridge port adjacent to which to retrieve addresses
249107f8ac4aSLinus Lüssing  * @br_ip_list:	The list to store found, snooped multicast IP addresses in
249207f8ac4aSLinus Lüssing  *
249307f8ac4aSLinus Lüssing  * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
249407f8ac4aSLinus Lüssing  * snooping feature on all bridge ports of dev's bridge device, excluding
249507f8ac4aSLinus Lüssing  * the addresses from dev itself.
249607f8ac4aSLinus Lüssing  *
249707f8ac4aSLinus Lüssing  * Returns the number of items added to br_ip_list.
249807f8ac4aSLinus Lüssing  *
249907f8ac4aSLinus Lüssing  * Notes:
250007f8ac4aSLinus Lüssing  * - br_ip_list needs to be initialized by caller
250107f8ac4aSLinus Lüssing  * - br_ip_list might contain duplicates in the end
250207f8ac4aSLinus Lüssing  *   (needs to be taken care of by caller)
250307f8ac4aSLinus Lüssing  * - br_ip_list needs to be freed by caller
250407f8ac4aSLinus Lüssing  */
250507f8ac4aSLinus Lüssing int br_multicast_list_adjacent(struct net_device *dev,
250607f8ac4aSLinus Lüssing 			       struct list_head *br_ip_list)
250707f8ac4aSLinus Lüssing {
250807f8ac4aSLinus Lüssing 	struct net_bridge *br;
250907f8ac4aSLinus Lüssing 	struct net_bridge_port *port;
251007f8ac4aSLinus Lüssing 	struct net_bridge_port_group *group;
251107f8ac4aSLinus Lüssing 	struct br_ip_list *entry;
251207f8ac4aSLinus Lüssing 	int count = 0;
251307f8ac4aSLinus Lüssing 
251407f8ac4aSLinus Lüssing 	rcu_read_lock();
251535f861e3SJulian Wiedmann 	if (!br_ip_list || !netif_is_bridge_port(dev))
251607f8ac4aSLinus Lüssing 		goto unlock;
251707f8ac4aSLinus Lüssing 
251807f8ac4aSLinus Lüssing 	port = br_port_get_rcu(dev);
251907f8ac4aSLinus Lüssing 	if (!port || !port->br)
252007f8ac4aSLinus Lüssing 		goto unlock;
252107f8ac4aSLinus Lüssing 
252207f8ac4aSLinus Lüssing 	br = port->br;
252307f8ac4aSLinus Lüssing 
252407f8ac4aSLinus Lüssing 	list_for_each_entry_rcu(port, &br->port_list, list) {
252507f8ac4aSLinus Lüssing 		if (!port->dev || port->dev == dev)
252607f8ac4aSLinus Lüssing 			continue;
252707f8ac4aSLinus Lüssing 
252807f8ac4aSLinus Lüssing 		hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
252907f8ac4aSLinus Lüssing 			entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
253007f8ac4aSLinus Lüssing 			if (!entry)
253107f8ac4aSLinus Lüssing 				goto unlock;
253207f8ac4aSLinus Lüssing 
253307f8ac4aSLinus Lüssing 			entry->addr = group->addr;
253407f8ac4aSLinus Lüssing 			list_add(&entry->list, br_ip_list);
253507f8ac4aSLinus Lüssing 			count++;
253607f8ac4aSLinus Lüssing 		}
253707f8ac4aSLinus Lüssing 	}
253807f8ac4aSLinus Lüssing 
253907f8ac4aSLinus Lüssing unlock:
254007f8ac4aSLinus Lüssing 	rcu_read_unlock();
254107f8ac4aSLinus Lüssing 	return count;
254207f8ac4aSLinus Lüssing }
254307f8ac4aSLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
25442cd41431SLinus Lüssing 
25452cd41431SLinus Lüssing /**
2546c34963e2SLinus Lüssing  * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2547c34963e2SLinus Lüssing  * @dev: The bridge port providing the bridge on which to check for a querier
2548c34963e2SLinus Lüssing  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2549c34963e2SLinus Lüssing  *
2550c34963e2SLinus Lüssing  * Checks whether the given interface has a bridge on top and if so returns
2551c34963e2SLinus Lüssing  * true if a valid querier exists anywhere on the bridged link layer.
2552c34963e2SLinus Lüssing  * Otherwise returns false.
2553c34963e2SLinus Lüssing  */
2554c34963e2SLinus Lüssing bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
2555c34963e2SLinus Lüssing {
2556c34963e2SLinus Lüssing 	struct net_bridge *br;
2557c34963e2SLinus Lüssing 	struct net_bridge_port *port;
2558c34963e2SLinus Lüssing 	struct ethhdr eth;
2559c34963e2SLinus Lüssing 	bool ret = false;
2560c34963e2SLinus Lüssing 
2561c34963e2SLinus Lüssing 	rcu_read_lock();
256235f861e3SJulian Wiedmann 	if (!netif_is_bridge_port(dev))
2563c34963e2SLinus Lüssing 		goto unlock;
2564c34963e2SLinus Lüssing 
2565c34963e2SLinus Lüssing 	port = br_port_get_rcu(dev);
2566c34963e2SLinus Lüssing 	if (!port || !port->br)
2567c34963e2SLinus Lüssing 		goto unlock;
2568c34963e2SLinus Lüssing 
2569c34963e2SLinus Lüssing 	br = port->br;
2570c34963e2SLinus Lüssing 
2571c34963e2SLinus Lüssing 	memset(&eth, 0, sizeof(eth));
2572c34963e2SLinus Lüssing 	eth.h_proto = htons(proto);
2573c34963e2SLinus Lüssing 
2574c34963e2SLinus Lüssing 	ret = br_multicast_querier_exists(br, &eth);
2575c34963e2SLinus Lüssing 
2576c34963e2SLinus Lüssing unlock:
2577c34963e2SLinus Lüssing 	rcu_read_unlock();
2578c34963e2SLinus Lüssing 	return ret;
2579c34963e2SLinus Lüssing }
2580c34963e2SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
2581c34963e2SLinus Lüssing 
2582c34963e2SLinus Lüssing /**
25832cd41431SLinus Lüssing  * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
25842cd41431SLinus Lüssing  * @dev: The bridge port adjacent to which to check for a querier
25852cd41431SLinus Lüssing  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
25862cd41431SLinus Lüssing  *
25872cd41431SLinus Lüssing  * Checks whether the given interface has a bridge on top and if so returns
25882cd41431SLinus Lüssing  * true if a selected querier is behind one of the other ports of this
25892cd41431SLinus Lüssing  * bridge. Otherwise returns false.
25902cd41431SLinus Lüssing  */
25912cd41431SLinus Lüssing bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
25922cd41431SLinus Lüssing {
25932cd41431SLinus Lüssing 	struct net_bridge *br;
25942cd41431SLinus Lüssing 	struct net_bridge_port *port;
25952cd41431SLinus Lüssing 	bool ret = false;
25962cd41431SLinus Lüssing 
25972cd41431SLinus Lüssing 	rcu_read_lock();
259835f861e3SJulian Wiedmann 	if (!netif_is_bridge_port(dev))
25992cd41431SLinus Lüssing 		goto unlock;
26002cd41431SLinus Lüssing 
26012cd41431SLinus Lüssing 	port = br_port_get_rcu(dev);
26022cd41431SLinus Lüssing 	if (!port || !port->br)
26032cd41431SLinus Lüssing 		goto unlock;
26042cd41431SLinus Lüssing 
26052cd41431SLinus Lüssing 	br = port->br;
26062cd41431SLinus Lüssing 
26072cd41431SLinus Lüssing 	switch (proto) {
26082cd41431SLinus Lüssing 	case ETH_P_IP:
26092cd41431SLinus Lüssing 		if (!timer_pending(&br->ip4_other_query.timer) ||
26102cd41431SLinus Lüssing 		    rcu_dereference(br->ip4_querier.port) == port)
26112cd41431SLinus Lüssing 			goto unlock;
26122cd41431SLinus Lüssing 		break;
26133993c4e1SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
26142cd41431SLinus Lüssing 	case ETH_P_IPV6:
26152cd41431SLinus Lüssing 		if (!timer_pending(&br->ip6_other_query.timer) ||
26162cd41431SLinus Lüssing 		    rcu_dereference(br->ip6_querier.port) == port)
26172cd41431SLinus Lüssing 			goto unlock;
26182cd41431SLinus Lüssing 		break;
26193993c4e1SLinus Lüssing #endif
26202cd41431SLinus Lüssing 	default:
26212cd41431SLinus Lüssing 		goto unlock;
26222cd41431SLinus Lüssing 	}
26232cd41431SLinus Lüssing 
26242cd41431SLinus Lüssing 	ret = true;
26252cd41431SLinus Lüssing unlock:
26262cd41431SLinus Lüssing 	rcu_read_unlock();
26272cd41431SLinus Lüssing 	return ret;
26282cd41431SLinus Lüssing }
26292cd41431SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
26301080ab95SNikolay Aleksandrov 
26311080ab95SNikolay Aleksandrov static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
2632a65056ecSNikolay Aleksandrov 			       const struct sk_buff *skb, u8 type, u8 dir)
26331080ab95SNikolay Aleksandrov {
26341080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
2635a65056ecSNikolay Aleksandrov 	__be16 proto = skb->protocol;
2636a65056ecSNikolay Aleksandrov 	unsigned int t_len;
26371080ab95SNikolay Aleksandrov 
26381080ab95SNikolay Aleksandrov 	u64_stats_update_begin(&pstats->syncp);
26391080ab95SNikolay Aleksandrov 	switch (proto) {
26401080ab95SNikolay Aleksandrov 	case htons(ETH_P_IP):
2641a65056ecSNikolay Aleksandrov 		t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
26421080ab95SNikolay Aleksandrov 		switch (type) {
26431080ab95SNikolay Aleksandrov 		case IGMP_HOST_MEMBERSHIP_REPORT:
26441080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v1reports[dir]++;
26451080ab95SNikolay Aleksandrov 			break;
26461080ab95SNikolay Aleksandrov 		case IGMPV2_HOST_MEMBERSHIP_REPORT:
26471080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v2reports[dir]++;
26481080ab95SNikolay Aleksandrov 			break;
26491080ab95SNikolay Aleksandrov 		case IGMPV3_HOST_MEMBERSHIP_REPORT:
26501080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v3reports[dir]++;
26511080ab95SNikolay Aleksandrov 			break;
26521080ab95SNikolay Aleksandrov 		case IGMP_HOST_MEMBERSHIP_QUERY:
2653a65056ecSNikolay Aleksandrov 			if (t_len != sizeof(struct igmphdr)) {
2654a65056ecSNikolay Aleksandrov 				pstats->mstats.igmp_v3queries[dir]++;
2655a65056ecSNikolay Aleksandrov 			} else {
2656a65056ecSNikolay Aleksandrov 				unsigned int offset = skb_transport_offset(skb);
2657a65056ecSNikolay Aleksandrov 				struct igmphdr *ih, _ihdr;
2658a65056ecSNikolay Aleksandrov 
2659a65056ecSNikolay Aleksandrov 				ih = skb_header_pointer(skb, offset,
2660a65056ecSNikolay Aleksandrov 							sizeof(_ihdr), &_ihdr);
2661a65056ecSNikolay Aleksandrov 				if (!ih)
2662a65056ecSNikolay Aleksandrov 					break;
2663a65056ecSNikolay Aleksandrov 				if (!ih->code)
2664a65056ecSNikolay Aleksandrov 					pstats->mstats.igmp_v1queries[dir]++;
2665a65056ecSNikolay Aleksandrov 				else
2666a65056ecSNikolay Aleksandrov 					pstats->mstats.igmp_v2queries[dir]++;
2667a65056ecSNikolay Aleksandrov 			}
26681080ab95SNikolay Aleksandrov 			break;
26691080ab95SNikolay Aleksandrov 		case IGMP_HOST_LEAVE_MESSAGE:
26701080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_leaves[dir]++;
26711080ab95SNikolay Aleksandrov 			break;
26721080ab95SNikolay Aleksandrov 		}
26731080ab95SNikolay Aleksandrov 		break;
26741080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
26751080ab95SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
2676a65056ecSNikolay Aleksandrov 		t_len = ntohs(ipv6_hdr(skb)->payload_len) +
2677a65056ecSNikolay Aleksandrov 			sizeof(struct ipv6hdr);
2678a65056ecSNikolay Aleksandrov 		t_len -= skb_network_header_len(skb);
26791080ab95SNikolay Aleksandrov 		switch (type) {
26801080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_REPORT:
26811080ab95SNikolay Aleksandrov 			pstats->mstats.mld_v1reports[dir]++;
26821080ab95SNikolay Aleksandrov 			break;
26831080ab95SNikolay Aleksandrov 		case ICMPV6_MLD2_REPORT:
26841080ab95SNikolay Aleksandrov 			pstats->mstats.mld_v2reports[dir]++;
26851080ab95SNikolay Aleksandrov 			break;
26861080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_QUERY:
2687a65056ecSNikolay Aleksandrov 			if (t_len != sizeof(struct mld_msg))
2688a65056ecSNikolay Aleksandrov 				pstats->mstats.mld_v2queries[dir]++;
2689a65056ecSNikolay Aleksandrov 			else
2690a65056ecSNikolay Aleksandrov 				pstats->mstats.mld_v1queries[dir]++;
26911080ab95SNikolay Aleksandrov 			break;
26921080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_REDUCTION:
26931080ab95SNikolay Aleksandrov 			pstats->mstats.mld_leaves[dir]++;
26941080ab95SNikolay Aleksandrov 			break;
26951080ab95SNikolay Aleksandrov 		}
26961080ab95SNikolay Aleksandrov 		break;
26971080ab95SNikolay Aleksandrov #endif /* CONFIG_IPV6 */
26981080ab95SNikolay Aleksandrov 	}
26991080ab95SNikolay Aleksandrov 	u64_stats_update_end(&pstats->syncp);
27001080ab95SNikolay Aleksandrov }
27011080ab95SNikolay Aleksandrov 
27021080ab95SNikolay Aleksandrov void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
2703a65056ecSNikolay Aleksandrov 			const struct sk_buff *skb, u8 type, u8 dir)
27041080ab95SNikolay Aleksandrov {
27051080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
27061080ab95SNikolay Aleksandrov 
27071080ab95SNikolay Aleksandrov 	/* if multicast_disabled is true then igmp type can't be set */
2708675779adSNikolay Aleksandrov 	if (!type || !br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
27091080ab95SNikolay Aleksandrov 		return;
27101080ab95SNikolay Aleksandrov 
27111080ab95SNikolay Aleksandrov 	if (p)
27121080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
27131080ab95SNikolay Aleksandrov 	else
27141080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
27151080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
27161080ab95SNikolay Aleksandrov 		return;
27171080ab95SNikolay Aleksandrov 
2718a65056ecSNikolay Aleksandrov 	br_mcast_stats_add(stats, skb, type, dir);
27191080ab95SNikolay Aleksandrov }
27201080ab95SNikolay Aleksandrov 
27211080ab95SNikolay Aleksandrov int br_multicast_init_stats(struct net_bridge *br)
27221080ab95SNikolay Aleksandrov {
27231080ab95SNikolay Aleksandrov 	br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
27241080ab95SNikolay Aleksandrov 	if (!br->mcast_stats)
27251080ab95SNikolay Aleksandrov 		return -ENOMEM;
27261080ab95SNikolay Aleksandrov 
27271080ab95SNikolay Aleksandrov 	return 0;
27281080ab95SNikolay Aleksandrov }
27291080ab95SNikolay Aleksandrov 
2730b6fe0440SIdo Schimmel void br_multicast_uninit_stats(struct net_bridge *br)
2731b6fe0440SIdo Schimmel {
2732b6fe0440SIdo Schimmel 	free_percpu(br->mcast_stats);
2733b6fe0440SIdo Schimmel }
2734b6fe0440SIdo Schimmel 
2735b3b6a84cSArnd Bergmann /* noinline for https://bugs.llvm.org/show_bug.cgi?id=45802#c9 */
2736b3b6a84cSArnd Bergmann static noinline_for_stack void mcast_stats_add_dir(u64 *dst, u64 *src)
27371080ab95SNikolay Aleksandrov {
27381080ab95SNikolay Aleksandrov 	dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
27391080ab95SNikolay Aleksandrov 	dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
27401080ab95SNikolay Aleksandrov }
27411080ab95SNikolay Aleksandrov 
27421080ab95SNikolay Aleksandrov void br_multicast_get_stats(const struct net_bridge *br,
27431080ab95SNikolay Aleksandrov 			    const struct net_bridge_port *p,
27441080ab95SNikolay Aleksandrov 			    struct br_mcast_stats *dest)
27451080ab95SNikolay Aleksandrov {
27461080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
27471080ab95SNikolay Aleksandrov 	struct br_mcast_stats tdst;
27481080ab95SNikolay Aleksandrov 	int i;
27491080ab95SNikolay Aleksandrov 
27501080ab95SNikolay Aleksandrov 	memset(dest, 0, sizeof(*dest));
27511080ab95SNikolay Aleksandrov 	if (p)
27521080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
27531080ab95SNikolay Aleksandrov 	else
27541080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
27551080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
27561080ab95SNikolay Aleksandrov 		return;
27571080ab95SNikolay Aleksandrov 
27581080ab95SNikolay Aleksandrov 	memset(&tdst, 0, sizeof(tdst));
27591080ab95SNikolay Aleksandrov 	for_each_possible_cpu(i) {
27601080ab95SNikolay Aleksandrov 		struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
27611080ab95SNikolay Aleksandrov 		struct br_mcast_stats temp;
27621080ab95SNikolay Aleksandrov 		unsigned int start;
27631080ab95SNikolay Aleksandrov 
27641080ab95SNikolay Aleksandrov 		do {
27651080ab95SNikolay Aleksandrov 			start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
27661080ab95SNikolay Aleksandrov 			memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
27671080ab95SNikolay Aleksandrov 		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
27681080ab95SNikolay Aleksandrov 
2769a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
2770a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
2771a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
27721080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
27731080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
27741080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
27751080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
27761080ab95SNikolay Aleksandrov 		tdst.igmp_parse_errors += temp.igmp_parse_errors;
27771080ab95SNikolay Aleksandrov 
2778a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
2779a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
27801080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
27811080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
27821080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
27831080ab95SNikolay Aleksandrov 		tdst.mld_parse_errors += temp.mld_parse_errors;
27841080ab95SNikolay Aleksandrov 	}
27851080ab95SNikolay Aleksandrov 	memcpy(dest, &tdst, sizeof(*dest));
27861080ab95SNikolay Aleksandrov }
278719e3a9c9SNikolay Aleksandrov 
278819e3a9c9SNikolay Aleksandrov int br_mdb_hash_init(struct net_bridge *br)
278919e3a9c9SNikolay Aleksandrov {
279019e3a9c9SNikolay Aleksandrov 	return rhashtable_init(&br->mdb_hash_tbl, &br_mdb_rht_params);
279119e3a9c9SNikolay Aleksandrov }
279219e3a9c9SNikolay Aleksandrov 
279319e3a9c9SNikolay Aleksandrov void br_mdb_hash_fini(struct net_bridge *br)
279419e3a9c9SNikolay Aleksandrov {
279519e3a9c9SNikolay Aleksandrov 	rhashtable_destroy(&br->mdb_hash_tbl);
279619e3a9c9SNikolay Aleksandrov }
2797