xref: /openbmc/linux/net/bridge/br_multicast.c (revision ba5ea614622dca6d675b4cc8a97270569ae13a23)
1eb1d1641SHerbert Xu /*
2eb1d1641SHerbert Xu  * Bridge multicast support.
3eb1d1641SHerbert Xu  *
4eb1d1641SHerbert Xu  * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
5eb1d1641SHerbert Xu  *
6eb1d1641SHerbert Xu  * This program is free software; you can redistribute it and/or modify it
7eb1d1641SHerbert Xu  * under the terms of the GNU General Public License as published by the Free
8eb1d1641SHerbert Xu  * Software Foundation; either version 2 of the License, or (at your option)
9eb1d1641SHerbert Xu  * any later version.
10eb1d1641SHerbert Xu  *
11eb1d1641SHerbert Xu  */
12eb1d1641SHerbert Xu 
13eb1d1641SHerbert Xu #include <linux/err.h>
1407f8ac4aSLinus Lüssing #include <linux/export.h>
15eb1d1641SHerbert Xu #include <linux/if_ether.h>
16eb1d1641SHerbert Xu #include <linux/igmp.h>
17eb1d1641SHerbert Xu #include <linux/jhash.h>
18eb1d1641SHerbert Xu #include <linux/kernel.h>
19b195167fSHerbert Xu #include <linux/log2.h>
20eb1d1641SHerbert Xu #include <linux/netdevice.h>
21eb1d1641SHerbert Xu #include <linux/netfilter_bridge.h>
22eb1d1641SHerbert Xu #include <linux/random.h>
23eb1d1641SHerbert Xu #include <linux/rculist.h>
24eb1d1641SHerbert Xu #include <linux/skbuff.h>
25eb1d1641SHerbert Xu #include <linux/slab.h>
26eb1d1641SHerbert Xu #include <linux/timer.h>
271c8ad5bfSCong Wang #include <linux/inetdevice.h>
2891b02d3dSNikolay Aleksandrov #include <linux/mroute.h>
29eb1d1641SHerbert Xu #include <net/ip.h>
30147c1e9bSNogah Frankel #include <net/switchdev.h>
31dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
3208b202b6SYOSHIFUJI Hideaki #include <net/ipv6.h>
3308b202b6SYOSHIFUJI Hideaki #include <net/mld.h>
34d4c4f07dSDavid S. Miller #include <net/ip6_checksum.h>
353c3769e6SLinus Lüssing #include <net/addrconf.h>
3608b202b6SYOSHIFUJI Hideaki #endif
37eb1d1641SHerbert Xu 
38eb1d1641SHerbert Xu #include "br_private.h"
39eb1d1641SHerbert Xu 
4019e3a9c9SNikolay Aleksandrov static const struct rhashtable_params br_mdb_rht_params = {
4119e3a9c9SNikolay Aleksandrov 	.head_offset = offsetof(struct net_bridge_mdb_entry, rhnode),
4219e3a9c9SNikolay Aleksandrov 	.key_offset = offsetof(struct net_bridge_mdb_entry, addr),
4319e3a9c9SNikolay Aleksandrov 	.key_len = sizeof(struct br_ip),
4419e3a9c9SNikolay Aleksandrov 	.automatic_shrinking = true,
4519e3a9c9SNikolay Aleksandrov 	.locks_mul = 1,
4619e3a9c9SNikolay Aleksandrov };
4719e3a9c9SNikolay Aleksandrov 
48cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br,
4990010b36SLinus Lüssing 				       struct bridge_mcast_own_query *query);
50754bc547SSatish Ashok static void br_multicast_add_router(struct net_bridge *br,
51754bc547SSatish Ashok 				    struct net_bridge_port *port);
52bc8c20acSSatish Ashok static void br_ip4_multicast_leave_group(struct net_bridge *br,
53bc8c20acSSatish Ashok 					 struct net_bridge_port *port,
54bc8c20acSSatish Ashok 					 __be32 group,
556db6f0eaSFelix Fietkau 					 __u16 vid,
566db6f0eaSFelix Fietkau 					 const unsigned char *src);
576db6f0eaSFelix Fietkau 
58f12e7d95SNogah Frankel static void __del_port_router(struct net_bridge_port *p);
59bc8c20acSSatish Ashok #if IS_ENABLED(CONFIG_IPV6)
60bc8c20acSSatish Ashok static void br_ip6_multicast_leave_group(struct net_bridge *br,
61bc8c20acSSatish Ashok 					 struct net_bridge_port *port,
62bc8c20acSSatish Ashok 					 const struct in6_addr *group,
636db6f0eaSFelix Fietkau 					 __u16 vid, const unsigned char *src);
64bc8c20acSSatish Ashok #endif
65c83b8fabSHerbert Xu 
668ef2a9a5SYOSHIFUJI Hideaki static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
678ef2a9a5SYOSHIFUJI Hideaki {
688ef2a9a5SYOSHIFUJI Hideaki 	if (a->proto != b->proto)
698ef2a9a5SYOSHIFUJI Hideaki 		return 0;
70b0e9a30dSVlad Yasevich 	if (a->vid != b->vid)
71b0e9a30dSVlad Yasevich 		return 0;
728ef2a9a5SYOSHIFUJI Hideaki 	switch (a->proto) {
738ef2a9a5SYOSHIFUJI Hideaki 	case htons(ETH_P_IP):
748ef2a9a5SYOSHIFUJI Hideaki 		return a->u.ip4 == b->u.ip4;
75dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
7608b202b6SYOSHIFUJI Hideaki 	case htons(ETH_P_IPV6):
7708b202b6SYOSHIFUJI Hideaki 		return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
7808b202b6SYOSHIFUJI Hideaki #endif
798ef2a9a5SYOSHIFUJI Hideaki 	}
808ef2a9a5SYOSHIFUJI Hideaki 	return 0;
818ef2a9a5SYOSHIFUJI Hideaki }
828ef2a9a5SYOSHIFUJI Hideaki 
8319e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip_get_rcu(struct net_bridge *br,
84cfd56754SCong Wang 						      struct br_ip *dst)
857f285fa7SHerbert Xu {
8619e3a9c9SNikolay Aleksandrov 	return rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params);
877f285fa7SHerbert Xu }
887f285fa7SHerbert Xu 
8919e3a9c9SNikolay Aleksandrov struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge *br,
9019e3a9c9SNikolay Aleksandrov 					   struct br_ip *dst)
9119e3a9c9SNikolay Aleksandrov {
9219e3a9c9SNikolay Aleksandrov 	struct net_bridge_mdb_entry *ent;
9319e3a9c9SNikolay Aleksandrov 
9419e3a9c9SNikolay Aleksandrov 	lockdep_assert_held_once(&br->multicast_lock);
9519e3a9c9SNikolay Aleksandrov 
9619e3a9c9SNikolay Aleksandrov 	rcu_read_lock();
9719e3a9c9SNikolay Aleksandrov 	ent = rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params);
9819e3a9c9SNikolay Aleksandrov 	rcu_read_unlock();
9919e3a9c9SNikolay Aleksandrov 
10019e3a9c9SNikolay Aleksandrov 	return ent;
10119e3a9c9SNikolay Aleksandrov }
10219e3a9c9SNikolay Aleksandrov 
10319e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip4_get(struct net_bridge *br,
10419e3a9c9SNikolay Aleksandrov 						   __be32 dst, __u16 vid)
105eb1d1641SHerbert Xu {
1068ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip br_dst;
1070821ec55SHerbert Xu 
10819e3a9c9SNikolay Aleksandrov 	memset(&br_dst, 0, sizeof(br_dst));
1098ef2a9a5SYOSHIFUJI Hideaki 	br_dst.u.ip4 = dst;
1108ef2a9a5SYOSHIFUJI Hideaki 	br_dst.proto = htons(ETH_P_IP);
111b0e9a30dSVlad Yasevich 	br_dst.vid = vid;
1128ef2a9a5SYOSHIFUJI Hideaki 
11319e3a9c9SNikolay Aleksandrov 	return br_mdb_ip_get(br, &br_dst);
1148ef2a9a5SYOSHIFUJI Hideaki }
1158ef2a9a5SYOSHIFUJI Hideaki 
116dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
11719e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip6_get(struct net_bridge *br,
11819e3a9c9SNikolay Aleksandrov 						   const struct in6_addr *dst,
119b0e9a30dSVlad Yasevich 						   __u16 vid)
12008b202b6SYOSHIFUJI Hideaki {
12108b202b6SYOSHIFUJI Hideaki 	struct br_ip br_dst;
12208b202b6SYOSHIFUJI Hideaki 
12319e3a9c9SNikolay Aleksandrov 	memset(&br_dst, 0, sizeof(br_dst));
1244e3fd7a0SAlexey Dobriyan 	br_dst.u.ip6 = *dst;
12508b202b6SYOSHIFUJI Hideaki 	br_dst.proto = htons(ETH_P_IPV6);
126b0e9a30dSVlad Yasevich 	br_dst.vid = vid;
12708b202b6SYOSHIFUJI Hideaki 
12819e3a9c9SNikolay Aleksandrov 	return br_mdb_ip_get(br, &br_dst);
12908b202b6SYOSHIFUJI Hideaki }
13008b202b6SYOSHIFUJI Hideaki #endif
13108b202b6SYOSHIFUJI Hideaki 
132eb1d1641SHerbert Xu struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
133fbca58a2SCong Wang 					struct sk_buff *skb, u16 vid)
134eb1d1641SHerbert Xu {
1358ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip ip;
1368ef2a9a5SYOSHIFUJI Hideaki 
13713cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
138eb1d1641SHerbert Xu 		return NULL;
139eb1d1641SHerbert Xu 
1408ef2a9a5SYOSHIFUJI Hideaki 	if (BR_INPUT_SKB_CB(skb)->igmp)
1418ef2a9a5SYOSHIFUJI Hideaki 		return NULL;
1428ef2a9a5SYOSHIFUJI Hideaki 
14319e3a9c9SNikolay Aleksandrov 	memset(&ip, 0, sizeof(ip));
1448ef2a9a5SYOSHIFUJI Hideaki 	ip.proto = skb->protocol;
145fbca58a2SCong Wang 	ip.vid = vid;
1468ef2a9a5SYOSHIFUJI Hideaki 
147eb1d1641SHerbert Xu 	switch (skb->protocol) {
148eb1d1641SHerbert Xu 	case htons(ETH_P_IP):
1498ef2a9a5SYOSHIFUJI Hideaki 		ip.u.ip4 = ip_hdr(skb)->daddr;
150eb1d1641SHerbert Xu 		break;
151dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
15208b202b6SYOSHIFUJI Hideaki 	case htons(ETH_P_IPV6):
1534e3fd7a0SAlexey Dobriyan 		ip.u.ip6 = ipv6_hdr(skb)->daddr;
15408b202b6SYOSHIFUJI Hideaki 		break;
15508b202b6SYOSHIFUJI Hideaki #endif
1568ef2a9a5SYOSHIFUJI Hideaki 	default:
1578ef2a9a5SYOSHIFUJI Hideaki 		return NULL;
158eb1d1641SHerbert Xu 	}
159eb1d1641SHerbert Xu 
16019e3a9c9SNikolay Aleksandrov 	return br_mdb_ip_get_rcu(br, &ip);
161eb1d1641SHerbert Xu }
162eb1d1641SHerbert Xu 
16388c1f37fSAllen Pais static void br_multicast_group_expired(struct timer_list *t)
164eb1d1641SHerbert Xu {
16588c1f37fSAllen Pais 	struct net_bridge_mdb_entry *mp = from_timer(mp, t, timer);
166eb1d1641SHerbert Xu 	struct net_bridge *br = mp->br;
167eb1d1641SHerbert Xu 
168eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
169eb1d1641SHerbert Xu 	if (!netif_running(br->dev) || timer_pending(&mp->timer))
170eb1d1641SHerbert Xu 		goto out;
171eb1d1641SHerbert Xu 
172ff0fd34eSAndrew Lunn 	mp->host_joined = false;
1732a26028dSAndrew Lunn 	br_mdb_notify(br->dev, NULL, &mp->addr, RTM_DELMDB, 0);
174eb1d1641SHerbert Xu 
175eb1d1641SHerbert Xu 	if (mp->ports)
176eb1d1641SHerbert Xu 		goto out;
177eb1d1641SHerbert Xu 
17819e3a9c9SNikolay Aleksandrov 	rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode,
17919e3a9c9SNikolay Aleksandrov 			       br_mdb_rht_params);
18019e3a9c9SNikolay Aleksandrov 	hlist_del_rcu(&mp->mdb_node);
181eb1d1641SHerbert Xu 
1824329596cSNikolay Aleksandrov 	kfree_rcu(mp, rcu);
183eb1d1641SHerbert Xu 
184eb1d1641SHerbert Xu out:
185eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
186eb1d1641SHerbert Xu }
187eb1d1641SHerbert Xu 
188eb1d1641SHerbert Xu static void br_multicast_del_pg(struct net_bridge *br,
189eb1d1641SHerbert Xu 				struct net_bridge_port_group *pg)
190eb1d1641SHerbert Xu {
191eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
192eb1d1641SHerbert Xu 	struct net_bridge_port_group *p;
193e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
194e8051688SEric Dumazet 
19519e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip_get(br, &pg->addr);
196eb1d1641SHerbert Xu 	if (WARN_ON(!mp))
197eb1d1641SHerbert Xu 		return;
198eb1d1641SHerbert Xu 
199e8051688SEric Dumazet 	for (pp = &mp->ports;
200e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
201e8051688SEric Dumazet 	     pp = &p->next) {
202eb1d1641SHerbert Xu 		if (p != pg)
203eb1d1641SHerbert Xu 			continue;
204eb1d1641SHerbert Xu 
20583f6a740Sstephen hemminger 		rcu_assign_pointer(*pp, p->next);
206eb1d1641SHerbert Xu 		hlist_del_init(&p->mglist);
207eb1d1641SHerbert Xu 		del_timer(&p->timer);
20845ebcce5SElad Raz 		br_mdb_notify(br->dev, p->port, &pg->addr, RTM_DELMDB,
20945ebcce5SElad Raz 			      p->flags);
2104329596cSNikolay Aleksandrov 		kfree_rcu(p, rcu);
211eb1d1641SHerbert Xu 
212ff0fd34eSAndrew Lunn 		if (!mp->ports && !mp->host_joined &&
213eb1d1641SHerbert Xu 		    netif_running(br->dev))
214eb1d1641SHerbert Xu 			mod_timer(&mp->timer, jiffies);
215eb1d1641SHerbert Xu 
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 
232eb1d1641SHerbert Xu 	br_multicast_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,
2391080ab95SNikolay Aleksandrov 						    __be32 group,
2401080ab95SNikolay Aleksandrov 						    u8 *igmp_type)
241eb1d1641SHerbert Xu {
2425e923585SNikolay Aleksandrov 	struct igmpv3_query *ihv3;
2435e923585SNikolay Aleksandrov 	size_t igmp_hdr_size;
244eb1d1641SHerbert Xu 	struct sk_buff *skb;
245eb1d1641SHerbert Xu 	struct igmphdr *ih;
246eb1d1641SHerbert Xu 	struct ethhdr *eth;
247eb1d1641SHerbert Xu 	struct iphdr *iph;
248eb1d1641SHerbert Xu 
2495e923585SNikolay Aleksandrov 	igmp_hdr_size = sizeof(*ih);
2505e923585SNikolay Aleksandrov 	if (br->multicast_igmp_version == 3)
2515e923585SNikolay Aleksandrov 		igmp_hdr_size = sizeof(*ihv3);
252eb1d1641SHerbert Xu 	skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
2535e923585SNikolay Aleksandrov 						 igmp_hdr_size + 4);
254eb1d1641SHerbert Xu 	if (!skb)
255eb1d1641SHerbert Xu 		goto out;
256eb1d1641SHerbert Xu 
257eb1d1641SHerbert Xu 	skb->protocol = htons(ETH_P_IP);
258eb1d1641SHerbert Xu 
259eb1d1641SHerbert Xu 	skb_reset_mac_header(skb);
260eb1d1641SHerbert Xu 	eth = eth_hdr(skb);
261eb1d1641SHerbert Xu 
262e5a727f6SJoe Perches 	ether_addr_copy(eth->h_source, br->dev->dev_addr);
263eb1d1641SHerbert Xu 	eth->h_dest[0] = 1;
264eb1d1641SHerbert Xu 	eth->h_dest[1] = 0;
265eb1d1641SHerbert Xu 	eth->h_dest[2] = 0x5e;
266eb1d1641SHerbert Xu 	eth->h_dest[3] = 0;
267eb1d1641SHerbert Xu 	eth->h_dest[4] = 0;
268eb1d1641SHerbert Xu 	eth->h_dest[5] = 1;
269eb1d1641SHerbert Xu 	eth->h_proto = htons(ETH_P_IP);
270eb1d1641SHerbert Xu 	skb_put(skb, sizeof(*eth));
271eb1d1641SHerbert Xu 
272eb1d1641SHerbert Xu 	skb_set_network_header(skb, skb->len);
273eb1d1641SHerbert Xu 	iph = ip_hdr(skb);
274eb1d1641SHerbert Xu 
275eb1d1641SHerbert Xu 	iph->version = 4;
276eb1d1641SHerbert Xu 	iph->ihl = 6;
277eb1d1641SHerbert Xu 	iph->tos = 0xc0;
2785e923585SNikolay Aleksandrov 	iph->tot_len = htons(sizeof(*iph) + igmp_hdr_size + 4);
279eb1d1641SHerbert Xu 	iph->id = 0;
280eb1d1641SHerbert Xu 	iph->frag_off = htons(IP_DF);
281eb1d1641SHerbert Xu 	iph->ttl = 1;
282eb1d1641SHerbert Xu 	iph->protocol = IPPROTO_IGMP;
283675779adSNikolay Aleksandrov 	iph->saddr = br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR) ?
2841c8ad5bfSCong Wang 		     inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0;
285eb1d1641SHerbert Xu 	iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
286eb1d1641SHerbert Xu 	((u8 *)&iph[1])[0] = IPOPT_RA;
287eb1d1641SHerbert Xu 	((u8 *)&iph[1])[1] = 4;
288eb1d1641SHerbert Xu 	((u8 *)&iph[1])[2] = 0;
289eb1d1641SHerbert Xu 	((u8 *)&iph[1])[3] = 0;
290eb1d1641SHerbert Xu 	ip_send_check(iph);
291eb1d1641SHerbert Xu 	skb_put(skb, 24);
292eb1d1641SHerbert Xu 
293eb1d1641SHerbert Xu 	skb_set_transport_header(skb, skb->len);
2941080ab95SNikolay Aleksandrov 	*igmp_type = IGMP_HOST_MEMBERSHIP_QUERY;
2955e923585SNikolay Aleksandrov 
2965e923585SNikolay Aleksandrov 	switch (br->multicast_igmp_version) {
2975e923585SNikolay Aleksandrov 	case 2:
2985e923585SNikolay Aleksandrov 		ih = igmp_hdr(skb);
299eb1d1641SHerbert Xu 		ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
300eb1d1641SHerbert Xu 		ih->code = (group ? br->multicast_last_member_interval :
301eb1d1641SHerbert Xu 				    br->multicast_query_response_interval) /
302eb1d1641SHerbert Xu 			   (HZ / IGMP_TIMER_SCALE);
303eb1d1641SHerbert Xu 		ih->group = group;
304eb1d1641SHerbert Xu 		ih->csum = 0;
3055e923585SNikolay Aleksandrov 		ih->csum = ip_compute_csum((void *)ih, sizeof(*ih));
3065e923585SNikolay Aleksandrov 		break;
3075e923585SNikolay Aleksandrov 	case 3:
3085e923585SNikolay Aleksandrov 		ihv3 = igmpv3_query_hdr(skb);
3095e923585SNikolay Aleksandrov 		ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY;
3105e923585SNikolay Aleksandrov 		ihv3->code = (group ? br->multicast_last_member_interval :
3115e923585SNikolay Aleksandrov 				      br->multicast_query_response_interval) /
3125e923585SNikolay Aleksandrov 			     (HZ / IGMP_TIMER_SCALE);
3135e923585SNikolay Aleksandrov 		ihv3->group = group;
3145e923585SNikolay Aleksandrov 		ihv3->qqic = br->multicast_query_interval / HZ;
3155e923585SNikolay Aleksandrov 		ihv3->nsrcs = 0;
3165e923585SNikolay Aleksandrov 		ihv3->resv = 0;
3175e923585SNikolay Aleksandrov 		ihv3->suppress = 0;
3185e923585SNikolay Aleksandrov 		ihv3->qrv = 2;
3195e923585SNikolay Aleksandrov 		ihv3->csum = 0;
3205e923585SNikolay Aleksandrov 		ihv3->csum = ip_compute_csum((void *)ihv3, sizeof(*ihv3));
3215e923585SNikolay Aleksandrov 		break;
3225e923585SNikolay Aleksandrov 	}
323eb1d1641SHerbert Xu 
3245e923585SNikolay Aleksandrov 	skb_put(skb, igmp_hdr_size);
325eb1d1641SHerbert Xu 	__skb_pull(skb, sizeof(*eth));
326eb1d1641SHerbert Xu 
327eb1d1641SHerbert Xu out:
328eb1d1641SHerbert Xu 	return skb;
329eb1d1641SHerbert Xu }
330eb1d1641SHerbert Xu 
331dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
33208b202b6SYOSHIFUJI Hideaki static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
3331080ab95SNikolay Aleksandrov 						    const struct in6_addr *grp,
3341080ab95SNikolay Aleksandrov 						    u8 *igmp_type)
33508b202b6SYOSHIFUJI Hideaki {
336aa2ae3e7SNikolay Aleksandrov 	struct mld2_query *mld2q;
337aa2ae3e7SNikolay Aleksandrov 	unsigned long interval;
33808b202b6SYOSHIFUJI Hideaki 	struct ipv6hdr *ip6h;
33908b202b6SYOSHIFUJI Hideaki 	struct mld_msg *mldq;
340aa2ae3e7SNikolay Aleksandrov 	size_t mld_hdr_size;
341aa2ae3e7SNikolay Aleksandrov 	struct sk_buff *skb;
34208b202b6SYOSHIFUJI Hideaki 	struct ethhdr *eth;
34308b202b6SYOSHIFUJI Hideaki 	u8 *hopopt;
34408b202b6SYOSHIFUJI Hideaki 
345aa2ae3e7SNikolay Aleksandrov 	mld_hdr_size = sizeof(*mldq);
346aa2ae3e7SNikolay Aleksandrov 	if (br->multicast_mld_version == 2)
347aa2ae3e7SNikolay Aleksandrov 		mld_hdr_size = sizeof(*mld2q);
34808b202b6SYOSHIFUJI Hideaki 	skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
349aa2ae3e7SNikolay Aleksandrov 						 8 + mld_hdr_size);
35008b202b6SYOSHIFUJI Hideaki 	if (!skb)
35108b202b6SYOSHIFUJI Hideaki 		goto out;
35208b202b6SYOSHIFUJI Hideaki 
35308b202b6SYOSHIFUJI Hideaki 	skb->protocol = htons(ETH_P_IPV6);
35408b202b6SYOSHIFUJI Hideaki 
35508b202b6SYOSHIFUJI Hideaki 	/* Ethernet header */
35608b202b6SYOSHIFUJI Hideaki 	skb_reset_mac_header(skb);
35708b202b6SYOSHIFUJI Hideaki 	eth = eth_hdr(skb);
35808b202b6SYOSHIFUJI Hideaki 
359e5a727f6SJoe Perches 	ether_addr_copy(eth->h_source, br->dev->dev_addr);
36008b202b6SYOSHIFUJI Hideaki 	eth->h_proto = htons(ETH_P_IPV6);
36108b202b6SYOSHIFUJI Hideaki 	skb_put(skb, sizeof(*eth));
36208b202b6SYOSHIFUJI Hideaki 
36308b202b6SYOSHIFUJI Hideaki 	/* IPv6 header + HbH option */
36408b202b6SYOSHIFUJI Hideaki 	skb_set_network_header(skb, skb->len);
36508b202b6SYOSHIFUJI Hideaki 	ip6h = ipv6_hdr(skb);
36608b202b6SYOSHIFUJI Hideaki 
36708b202b6SYOSHIFUJI Hideaki 	*(__force __be32 *)ip6h = htonl(0x60000000);
368aa2ae3e7SNikolay Aleksandrov 	ip6h->payload_len = htons(8 + mld_hdr_size);
36908b202b6SYOSHIFUJI Hideaki 	ip6h->nexthdr = IPPROTO_HOPOPTS;
37008b202b6SYOSHIFUJI Hideaki 	ip6h->hop_limit = 1;
371a7bff75bSLinus Lüssing 	ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
372d1d81d4cSUlrich Weber 	if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
373d1d81d4cSUlrich Weber 			       &ip6h->saddr)) {
374d1d81d4cSUlrich Weber 		kfree_skb(skb);
375675779adSNikolay Aleksandrov 		br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, false);
376d1d81d4cSUlrich Weber 		return NULL;
377d1d81d4cSUlrich Weber 	}
3780888d5f3Sdaniel 
379675779adSNikolay Aleksandrov 	br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true);
38036cff5a1SLinus Lüssing 	ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
38108b202b6SYOSHIFUJI Hideaki 
38208b202b6SYOSHIFUJI Hideaki 	hopopt = (u8 *)(ip6h + 1);
38308b202b6SYOSHIFUJI Hideaki 	hopopt[0] = IPPROTO_ICMPV6;		/* next hdr */
38408b202b6SYOSHIFUJI Hideaki 	hopopt[1] = 0;				/* length of HbH */
38508b202b6SYOSHIFUJI Hideaki 	hopopt[2] = IPV6_TLV_ROUTERALERT;	/* Router Alert */
38608b202b6SYOSHIFUJI Hideaki 	hopopt[3] = 2;				/* Length of RA Option */
38708b202b6SYOSHIFUJI Hideaki 	hopopt[4] = 0;				/* Type = 0x0000 (MLD) */
38808b202b6SYOSHIFUJI Hideaki 	hopopt[5] = 0;
3891de5a71cSEldad Zack 	hopopt[6] = IPV6_TLV_PAD1;		/* Pad1 */
3901de5a71cSEldad Zack 	hopopt[7] = IPV6_TLV_PAD1;		/* Pad1 */
39108b202b6SYOSHIFUJI Hideaki 
39208b202b6SYOSHIFUJI Hideaki 	skb_put(skb, sizeof(*ip6h) + 8);
39308b202b6SYOSHIFUJI Hideaki 
39408b202b6SYOSHIFUJI Hideaki 	/* ICMPv6 */
39508b202b6SYOSHIFUJI Hideaki 	skb_set_transport_header(skb, skb->len);
3961080ab95SNikolay Aleksandrov 	interval = ipv6_addr_any(grp) ?
39732de868cSLinus Lüssing 			br->multicast_query_response_interval :
39832de868cSLinus Lüssing 			br->multicast_last_member_interval;
3991080ab95SNikolay Aleksandrov 	*igmp_type = ICMPV6_MGM_QUERY;
400aa2ae3e7SNikolay Aleksandrov 	switch (br->multicast_mld_version) {
401aa2ae3e7SNikolay Aleksandrov 	case 1:
402aa2ae3e7SNikolay Aleksandrov 		mldq = (struct mld_msg *)icmp6_hdr(skb);
40308b202b6SYOSHIFUJI Hideaki 		mldq->mld_type = ICMPV6_MGM_QUERY;
40408b202b6SYOSHIFUJI Hideaki 		mldq->mld_code = 0;
40508b202b6SYOSHIFUJI Hideaki 		mldq->mld_cksum = 0;
40608b202b6SYOSHIFUJI Hideaki 		mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
40708b202b6SYOSHIFUJI Hideaki 		mldq->mld_reserved = 0;
4081080ab95SNikolay Aleksandrov 		mldq->mld_mca = *grp;
40908b202b6SYOSHIFUJI Hideaki 		mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
41008b202b6SYOSHIFUJI Hideaki 						  sizeof(*mldq), IPPROTO_ICMPV6,
41108b202b6SYOSHIFUJI Hideaki 						  csum_partial(mldq,
412aa2ae3e7SNikolay Aleksandrov 							       sizeof(*mldq),
413aa2ae3e7SNikolay Aleksandrov 							       0));
414aa2ae3e7SNikolay Aleksandrov 		break;
415aa2ae3e7SNikolay Aleksandrov 	case 2:
416aa2ae3e7SNikolay Aleksandrov 		mld2q = (struct mld2_query *)icmp6_hdr(skb);
41753631a5fSLance Richardson 		mld2q->mld2q_mrc = htons((u16)jiffies_to_msecs(interval));
418aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_type = ICMPV6_MGM_QUERY;
419aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_code = 0;
420aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_cksum = 0;
421aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_resv1 = 0;
422aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_resv2 = 0;
423aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_suppress = 0;
424aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_qrv = 2;
425aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_nsrcs = 0;
426aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_qqic = br->multicast_query_interval / HZ;
427aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_mca = *grp;
428aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
429aa2ae3e7SNikolay Aleksandrov 						     sizeof(*mld2q),
430aa2ae3e7SNikolay Aleksandrov 						     IPPROTO_ICMPV6,
431aa2ae3e7SNikolay Aleksandrov 						     csum_partial(mld2q,
432aa2ae3e7SNikolay Aleksandrov 								  sizeof(*mld2q),
433aa2ae3e7SNikolay Aleksandrov 								  0));
434aa2ae3e7SNikolay Aleksandrov 		break;
435aa2ae3e7SNikolay Aleksandrov 	}
436aa2ae3e7SNikolay Aleksandrov 	skb_put(skb, mld_hdr_size);
43708b202b6SYOSHIFUJI Hideaki 
43808b202b6SYOSHIFUJI Hideaki 	__skb_pull(skb, sizeof(*eth));
43908b202b6SYOSHIFUJI Hideaki 
44008b202b6SYOSHIFUJI Hideaki out:
44108b202b6SYOSHIFUJI Hideaki 	return skb;
44208b202b6SYOSHIFUJI Hideaki }
44308b202b6SYOSHIFUJI Hideaki #endif
44408b202b6SYOSHIFUJI Hideaki 
4458ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
4461080ab95SNikolay Aleksandrov 						struct br_ip *addr,
4471080ab95SNikolay Aleksandrov 						u8 *igmp_type)
4488ef2a9a5SYOSHIFUJI Hideaki {
4498ef2a9a5SYOSHIFUJI Hideaki 	switch (addr->proto) {
4508ef2a9a5SYOSHIFUJI Hideaki 	case htons(ETH_P_IP):
4511080ab95SNikolay Aleksandrov 		return br_ip4_multicast_alloc_query(br, addr->u.ip4, igmp_type);
452dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
45308b202b6SYOSHIFUJI Hideaki 	case htons(ETH_P_IPV6):
4541080ab95SNikolay Aleksandrov 		return br_ip6_multicast_alloc_query(br, &addr->u.ip6,
4551080ab95SNikolay Aleksandrov 						    igmp_type);
45608b202b6SYOSHIFUJI Hideaki #endif
4578ef2a9a5SYOSHIFUJI Hideaki 	}
4588ef2a9a5SYOSHIFUJI Hideaki 	return NULL;
4598ef2a9a5SYOSHIFUJI Hideaki }
4608ef2a9a5SYOSHIFUJI Hideaki 
461cfd56754SCong Wang struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
4625e923585SNikolay Aleksandrov 						    struct br_ip *group)
463eb1d1641SHerbert Xu {
464eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
4654c0833bcSTobias Klauser 	int err;
466eb1d1641SHerbert Xu 
46719e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip_get(br, group);
46819e3a9c9SNikolay Aleksandrov 	if (mp)
46919e3a9c9SNikolay Aleksandrov 		return mp;
470eb1d1641SHerbert Xu 
47119e3a9c9SNikolay Aleksandrov 	if (atomic_read(&br->mdb_hash_tbl.nelems) >= br->hash_max) {
47219e3a9c9SNikolay Aleksandrov 		br_opt_toggle(br, BROPT_MULTICAST_ENABLED, false);
47319e3a9c9SNikolay Aleksandrov 		return ERR_PTR(-E2BIG);
474eb1d1641SHerbert Xu 	}
475eb1d1641SHerbert Xu 
476eb1d1641SHerbert Xu 	mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
477eb1d1641SHerbert Xu 	if (unlikely(!mp))
4784c0833bcSTobias Klauser 		return ERR_PTR(-ENOMEM);
479eb1d1641SHerbert Xu 
480eb1d1641SHerbert Xu 	mp->br = br;
4818ef2a9a5SYOSHIFUJI Hideaki 	mp->addr = *group;
48288c1f37fSAllen Pais 	timer_setup(&mp->timer, br_multicast_group_expired, 0);
48319e3a9c9SNikolay Aleksandrov 	err = rhashtable_lookup_insert_fast(&br->mdb_hash_tbl, &mp->rhnode,
48419e3a9c9SNikolay Aleksandrov 					    br_mdb_rht_params);
48519e3a9c9SNikolay Aleksandrov 	if (err) {
48619e3a9c9SNikolay Aleksandrov 		kfree(mp);
48719e3a9c9SNikolay Aleksandrov 		mp = ERR_PTR(err);
48819e3a9c9SNikolay Aleksandrov 	} else {
48919e3a9c9SNikolay Aleksandrov 		hlist_add_head_rcu(&mp->mdb_node, &br->mdb_list);
49019e3a9c9SNikolay Aleksandrov 	}
4911faabf2aSEric Dumazet 
492eb1d1641SHerbert Xu 	return mp;
493eb1d1641SHerbert Xu }
494eb1d1641SHerbert Xu 
495cfd56754SCong Wang struct net_bridge_port_group *br_multicast_new_port_group(
496cfd56754SCong Wang 			struct net_bridge_port *port,
497cfd56754SCong Wang 			struct br_ip *group,
498ccb1c31aSAmerigo Wang 			struct net_bridge_port_group __rcu *next,
4996db6f0eaSFelix Fietkau 			unsigned char flags,
5006db6f0eaSFelix Fietkau 			const unsigned char *src)
501cfd56754SCong Wang {
502cfd56754SCong Wang 	struct net_bridge_port_group *p;
503cfd56754SCong Wang 
504cfd56754SCong Wang 	p = kzalloc(sizeof(*p), GFP_ATOMIC);
505cfd56754SCong Wang 	if (unlikely(!p))
506cfd56754SCong Wang 		return NULL;
507cfd56754SCong Wang 
508cfd56754SCong Wang 	p->addr = *group;
509cfd56754SCong Wang 	p->port = port;
5109d06b6d8SElad Raz 	p->flags = flags;
511eca2a43bSstephen hemminger 	rcu_assign_pointer(p->next, next);
512cfd56754SCong Wang 	hlist_add_head(&p->mglist, &port->mglist);
51388c1f37fSAllen Pais 	timer_setup(&p->timer, br_multicast_port_group_expired, 0);
5146db6f0eaSFelix Fietkau 
5156db6f0eaSFelix Fietkau 	if (src)
5166db6f0eaSFelix Fietkau 		memcpy(p->eth_addr, src, ETH_ALEN);
5176db6f0eaSFelix Fietkau 	else
5186db6f0eaSFelix Fietkau 		memset(p->eth_addr, 0xff, ETH_ALEN);
5196db6f0eaSFelix Fietkau 
520cfd56754SCong Wang 	return p;
521cfd56754SCong Wang }
522cfd56754SCong Wang 
5236db6f0eaSFelix Fietkau static bool br_port_group_equal(struct net_bridge_port_group *p,
5246db6f0eaSFelix Fietkau 				struct net_bridge_port *port,
5256db6f0eaSFelix Fietkau 				const unsigned char *src)
5266db6f0eaSFelix Fietkau {
5276db6f0eaSFelix Fietkau 	if (p->port != port)
5286db6f0eaSFelix Fietkau 		return false;
5296db6f0eaSFelix Fietkau 
5306db6f0eaSFelix Fietkau 	if (!(port->flags & BR_MULTICAST_TO_UNICAST))
5316db6f0eaSFelix Fietkau 		return true;
5326db6f0eaSFelix Fietkau 
5336db6f0eaSFelix Fietkau 	return ether_addr_equal(src, p->eth_addr);
5346db6f0eaSFelix Fietkau }
5356db6f0eaSFelix Fietkau 
536eb1d1641SHerbert Xu static int br_multicast_add_group(struct net_bridge *br,
5378ef2a9a5SYOSHIFUJI Hideaki 				  struct net_bridge_port *port,
5386db6f0eaSFelix Fietkau 				  struct br_ip *group,
5396db6f0eaSFelix Fietkau 				  const unsigned char *src)
540eb1d1641SHerbert Xu {
541e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
5425e923585SNikolay Aleksandrov 	struct net_bridge_port_group *p;
5435e923585SNikolay Aleksandrov 	struct net_bridge_mdb_entry *mp;
544454594f3SLinus Lüssing 	unsigned long now = jiffies;
545eb1d1641SHerbert Xu 	int err;
546eb1d1641SHerbert Xu 
547eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
548eb1d1641SHerbert Xu 	if (!netif_running(br->dev) ||
549eb1d1641SHerbert Xu 	    (port && port->state == BR_STATE_DISABLED))
550eb1d1641SHerbert Xu 		goto out;
551eb1d1641SHerbert Xu 
55219e3a9c9SNikolay Aleksandrov 	mp = br_multicast_new_group(br, group);
553eb1d1641SHerbert Xu 	err = PTR_ERR(mp);
5544c0833bcSTobias Klauser 	if (IS_ERR(mp))
555eb1d1641SHerbert Xu 		goto err;
556eb1d1641SHerbert Xu 
557eb1d1641SHerbert Xu 	if (!port) {
5582a26028dSAndrew Lunn 		if (!mp->host_joined) {
559ff0fd34eSAndrew Lunn 			mp->host_joined = true;
5602a26028dSAndrew Lunn 			br_mdb_notify(br->dev, NULL, &mp->addr, RTM_NEWMDB, 0);
5612a26028dSAndrew Lunn 		}
562454594f3SLinus Lüssing 		mod_timer(&mp->timer, now + br->multicast_membership_interval);
563eb1d1641SHerbert Xu 		goto out;
564eb1d1641SHerbert Xu 	}
565eb1d1641SHerbert Xu 
566e8051688SEric Dumazet 	for (pp = &mp->ports;
567e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
568e8051688SEric Dumazet 	     pp = &p->next) {
5696db6f0eaSFelix Fietkau 		if (br_port_group_equal(p, port, src))
570454594f3SLinus Lüssing 			goto found;
571eb1d1641SHerbert Xu 		if ((unsigned long)p->port < (unsigned long)port)
572eb1d1641SHerbert Xu 			break;
573eb1d1641SHerbert Xu 	}
574eb1d1641SHerbert Xu 
5756db6f0eaSFelix Fietkau 	p = br_multicast_new_port_group(port, group, *pp, 0, src);
576eb1d1641SHerbert Xu 	if (unlikely(!p))
577eb1d1641SHerbert Xu 		goto err;
578eb1d1641SHerbert Xu 	rcu_assign_pointer(*pp, p);
57945ebcce5SElad Raz 	br_mdb_notify(br->dev, port, group, RTM_NEWMDB, 0);
580eb1d1641SHerbert Xu 
581454594f3SLinus Lüssing found:
582454594f3SLinus Lüssing 	mod_timer(&p->timer, now + br->multicast_membership_interval);
583eb1d1641SHerbert Xu out:
584eb1d1641SHerbert Xu 	err = 0;
585eb1d1641SHerbert Xu 
586eb1d1641SHerbert Xu err:
587eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
588eb1d1641SHerbert Xu 	return err;
589eb1d1641SHerbert Xu }
590eb1d1641SHerbert Xu 
5918ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_add_group(struct net_bridge *br,
5928ef2a9a5SYOSHIFUJI Hideaki 				      struct net_bridge_port *port,
593b0e9a30dSVlad Yasevich 				      __be32 group,
5946db6f0eaSFelix Fietkau 				      __u16 vid,
5956db6f0eaSFelix Fietkau 				      const unsigned char *src)
5968ef2a9a5SYOSHIFUJI Hideaki {
5978ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip br_group;
5988ef2a9a5SYOSHIFUJI Hideaki 
5998ef2a9a5SYOSHIFUJI Hideaki 	if (ipv4_is_local_multicast(group))
6008ef2a9a5SYOSHIFUJI Hideaki 		return 0;
6018ef2a9a5SYOSHIFUJI Hideaki 
6028ef2a9a5SYOSHIFUJI Hideaki 	br_group.u.ip4 = group;
6038ef2a9a5SYOSHIFUJI Hideaki 	br_group.proto = htons(ETH_P_IP);
604b0e9a30dSVlad Yasevich 	br_group.vid = vid;
6058ef2a9a5SYOSHIFUJI Hideaki 
6066db6f0eaSFelix Fietkau 	return br_multicast_add_group(br, port, &br_group, src);
6078ef2a9a5SYOSHIFUJI Hideaki }
6088ef2a9a5SYOSHIFUJI Hideaki 
609dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
61008b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_add_group(struct net_bridge *br,
61108b202b6SYOSHIFUJI Hideaki 				      struct net_bridge_port *port,
612b0e9a30dSVlad Yasevich 				      const struct in6_addr *group,
6136db6f0eaSFelix Fietkau 				      __u16 vid,
6146db6f0eaSFelix Fietkau 				      const unsigned char *src)
61508b202b6SYOSHIFUJI Hideaki {
61608b202b6SYOSHIFUJI Hideaki 	struct br_ip br_group;
61708b202b6SYOSHIFUJI Hideaki 
6183c3769e6SLinus Lüssing 	if (ipv6_addr_is_ll_all_nodes(group))
61908b202b6SYOSHIFUJI Hideaki 		return 0;
62008b202b6SYOSHIFUJI Hideaki 
62119e3a9c9SNikolay Aleksandrov 	memset(&br_group, 0, sizeof(br_group));
6224e3fd7a0SAlexey Dobriyan 	br_group.u.ip6 = *group;
6239cc6e0c4SLinus Lüssing 	br_group.proto = htons(ETH_P_IPV6);
624b0e9a30dSVlad Yasevich 	br_group.vid = vid;
62508b202b6SYOSHIFUJI Hideaki 
6266db6f0eaSFelix Fietkau 	return br_multicast_add_group(br, port, &br_group, src);
62708b202b6SYOSHIFUJI Hideaki }
62808b202b6SYOSHIFUJI Hideaki #endif
62908b202b6SYOSHIFUJI Hideaki 
63088c1f37fSAllen Pais static void br_multicast_router_expired(struct timer_list *t)
631eb1d1641SHerbert Xu {
63288c1f37fSAllen Pais 	struct net_bridge_port *port =
63388c1f37fSAllen Pais 			from_timer(port, t, multicast_router_timer);
634eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
635eb1d1641SHerbert Xu 
636eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
637a55d8246SNikolay Aleksandrov 	if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
638a55d8246SNikolay Aleksandrov 	    port->multicast_router == MDB_RTR_TYPE_PERM ||
639f12e7d95SNogah Frankel 	    timer_pending(&port->multicast_router_timer))
640eb1d1641SHerbert Xu 		goto out;
641eb1d1641SHerbert Xu 
642f12e7d95SNogah Frankel 	__del_port_router(port);
643eb1d1641SHerbert Xu out:
644eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
645eb1d1641SHerbert Xu }
646eb1d1641SHerbert Xu 
64777041420SYotam Gigi static void br_mc_router_state_change(struct net_bridge *p,
64877041420SYotam Gigi 				      bool is_mc_router)
64977041420SYotam Gigi {
65077041420SYotam Gigi 	struct switchdev_attr attr = {
65177041420SYotam Gigi 		.orig_dev = p->dev,
65277041420SYotam Gigi 		.id = SWITCHDEV_ATTR_ID_BRIDGE_MROUTER,
65377041420SYotam Gigi 		.flags = SWITCHDEV_F_DEFER,
65477041420SYotam Gigi 		.u.mrouter = is_mc_router,
65577041420SYotam Gigi 	};
65677041420SYotam Gigi 
65777041420SYotam Gigi 	switchdev_port_attr_set(p->dev, &attr);
65877041420SYotam Gigi }
65977041420SYotam Gigi 
66088c1f37fSAllen Pais static void br_multicast_local_router_expired(struct timer_list *t)
661eb1d1641SHerbert Xu {
66288c1f37fSAllen Pais 	struct net_bridge *br = from_timer(br, t, multicast_router_timer);
66377041420SYotam Gigi 
66477041420SYotam Gigi 	spin_lock(&br->multicast_lock);
66577041420SYotam Gigi 	if (br->multicast_router == MDB_RTR_TYPE_DISABLED ||
66677041420SYotam Gigi 	    br->multicast_router == MDB_RTR_TYPE_PERM ||
66777041420SYotam Gigi 	    timer_pending(&br->multicast_router_timer))
66877041420SYotam Gigi 		goto out;
66977041420SYotam Gigi 
67077041420SYotam Gigi 	br_mc_router_state_change(br, false);
67177041420SYotam Gigi out:
67277041420SYotam Gigi 	spin_unlock(&br->multicast_lock);
673eb1d1641SHerbert Xu }
674eb1d1641SHerbert Xu 
675cc0fdd80SLinus Lüssing static void br_multicast_querier_expired(struct net_bridge *br,
67690010b36SLinus Lüssing 					 struct bridge_mcast_own_query *query)
677c83b8fabSHerbert Xu {
678c83b8fabSHerbert Xu 	spin_lock(&br->multicast_lock);
67913cefad2SNikolay Aleksandrov 	if (!netif_running(br->dev) || !br_opt_get(br, BROPT_MULTICAST_ENABLED))
680c83b8fabSHerbert Xu 		goto out;
681c83b8fabSHerbert Xu 
682cc0fdd80SLinus Lüssing 	br_multicast_start_querier(br, query);
683c83b8fabSHerbert Xu 
684c83b8fabSHerbert Xu out:
685c83b8fabSHerbert Xu 	spin_unlock(&br->multicast_lock);
686c83b8fabSHerbert Xu }
687c83b8fabSHerbert Xu 
68888c1f37fSAllen Pais static void br_ip4_multicast_querier_expired(struct timer_list *t)
689cc0fdd80SLinus Lüssing {
69088c1f37fSAllen Pais 	struct net_bridge *br = from_timer(br, t, ip4_other_query.timer);
691cc0fdd80SLinus Lüssing 
69290010b36SLinus Lüssing 	br_multicast_querier_expired(br, &br->ip4_own_query);
693cc0fdd80SLinus Lüssing }
694cc0fdd80SLinus Lüssing 
695cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
69688c1f37fSAllen Pais static void br_ip6_multicast_querier_expired(struct timer_list *t)
697cc0fdd80SLinus Lüssing {
69888c1f37fSAllen Pais 	struct net_bridge *br = from_timer(br, t, ip6_other_query.timer);
699cc0fdd80SLinus Lüssing 
70090010b36SLinus Lüssing 	br_multicast_querier_expired(br, &br->ip6_own_query);
701cc0fdd80SLinus Lüssing }
702cc0fdd80SLinus Lüssing #endif
703cc0fdd80SLinus Lüssing 
704dc4eb53aSLinus Lüssing static void br_multicast_select_own_querier(struct net_bridge *br,
705dc4eb53aSLinus Lüssing 					    struct br_ip *ip,
706dc4eb53aSLinus Lüssing 					    struct sk_buff *skb)
707dc4eb53aSLinus Lüssing {
708dc4eb53aSLinus Lüssing 	if (ip->proto == htons(ETH_P_IP))
709dc4eb53aSLinus Lüssing 		br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr;
710dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
711dc4eb53aSLinus Lüssing 	else
712dc4eb53aSLinus Lüssing 		br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr;
713dc4eb53aSLinus Lüssing #endif
714dc4eb53aSLinus Lüssing }
715dc4eb53aSLinus Lüssing 
7168ef2a9a5SYOSHIFUJI Hideaki static void __br_multicast_send_query(struct net_bridge *br,
7178ef2a9a5SYOSHIFUJI Hideaki 				      struct net_bridge_port *port,
7188ef2a9a5SYOSHIFUJI Hideaki 				      struct br_ip *ip)
719eb1d1641SHerbert Xu {
720eb1d1641SHerbert Xu 	struct sk_buff *skb;
7211080ab95SNikolay Aleksandrov 	u8 igmp_type;
722eb1d1641SHerbert Xu 
7231080ab95SNikolay Aleksandrov 	skb = br_multicast_alloc_query(br, ip, &igmp_type);
724eb1d1641SHerbert Xu 	if (!skb)
7258ef2a9a5SYOSHIFUJI Hideaki 		return;
726eb1d1641SHerbert Xu 
727eb1d1641SHerbert Xu 	if (port) {
728eb1d1641SHerbert Xu 		skb->dev = port->dev;
729a65056ecSNikolay Aleksandrov 		br_multicast_count(br, port, skb, igmp_type,
7301080ab95SNikolay Aleksandrov 				   BR_MCAST_DIR_TX);
73129a26a56SEric W. Biederman 		NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
73229a26a56SEric W. Biederman 			dev_net(port->dev), NULL, skb, NULL, skb->dev,
733f0b4eeceSLinus Lüssing 			br_dev_queue_push_xmit);
734dc4eb53aSLinus Lüssing 	} else {
735dc4eb53aSLinus Lüssing 		br_multicast_select_own_querier(br, ip, skb);
736a65056ecSNikolay Aleksandrov 		br_multicast_count(br, port, skb, igmp_type,
7371080ab95SNikolay Aleksandrov 				   BR_MCAST_DIR_RX);
738eb1d1641SHerbert Xu 		netif_rx(skb);
7398ef2a9a5SYOSHIFUJI Hideaki 	}
740dc4eb53aSLinus Lüssing }
741eb1d1641SHerbert Xu 
7428ef2a9a5SYOSHIFUJI Hideaki static void br_multicast_send_query(struct net_bridge *br,
743cc0fdd80SLinus Lüssing 				    struct net_bridge_port *port,
74490010b36SLinus Lüssing 				    struct bridge_mcast_own_query *own_query)
7458ef2a9a5SYOSHIFUJI Hideaki {
74690010b36SLinus Lüssing 	struct bridge_mcast_other_query *other_query = NULL;
7475e923585SNikolay Aleksandrov 	struct br_ip br_group;
7485e923585SNikolay Aleksandrov 	unsigned long time;
7498ef2a9a5SYOSHIFUJI Hideaki 
75013cefad2SNikolay Aleksandrov 	if (!netif_running(br->dev) ||
75113cefad2SNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_ENABLED) ||
752675779adSNikolay Aleksandrov 	    !br_opt_get(br, BROPT_MULTICAST_QUERIER))
7538ef2a9a5SYOSHIFUJI Hideaki 		return;
7548ef2a9a5SYOSHIFUJI Hideaki 
75508b202b6SYOSHIFUJI Hideaki 	memset(&br_group.u, 0, sizeof(br_group.u));
7568ef2a9a5SYOSHIFUJI Hideaki 
75790010b36SLinus Lüssing 	if (port ? (own_query == &port->ip4_own_query) :
75890010b36SLinus Lüssing 		   (own_query == &br->ip4_own_query)) {
75990010b36SLinus Lüssing 		other_query = &br->ip4_other_query;
76008b202b6SYOSHIFUJI Hideaki 		br_group.proto = htons(ETH_P_IP);
761dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
762cc0fdd80SLinus Lüssing 	} else {
76390010b36SLinus Lüssing 		other_query = &br->ip6_other_query;
76408b202b6SYOSHIFUJI Hideaki 		br_group.proto = htons(ETH_P_IPV6);
76508b202b6SYOSHIFUJI Hideaki #endif
766eb1d1641SHerbert Xu 	}
767eb1d1641SHerbert Xu 
76890010b36SLinus Lüssing 	if (!other_query || timer_pending(&other_query->timer))
769cc0fdd80SLinus Lüssing 		return;
770cc0fdd80SLinus Lüssing 
771cc0fdd80SLinus Lüssing 	__br_multicast_send_query(br, port, &br_group);
772cc0fdd80SLinus Lüssing 
773cc0fdd80SLinus Lüssing 	time = jiffies;
77490010b36SLinus Lüssing 	time += own_query->startup_sent < br->multicast_startup_query_count ?
775cc0fdd80SLinus Lüssing 		br->multicast_startup_query_interval :
776cc0fdd80SLinus Lüssing 		br->multicast_query_interval;
77790010b36SLinus Lüssing 	mod_timer(&own_query->timer, time);
778cc0fdd80SLinus Lüssing }
779cc0fdd80SLinus Lüssing 
78090010b36SLinus Lüssing static void
78190010b36SLinus Lüssing br_multicast_port_query_expired(struct net_bridge_port *port,
78290010b36SLinus Lüssing 				struct bridge_mcast_own_query *query)
783eb1d1641SHerbert Xu {
784eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
785eb1d1641SHerbert Xu 
786eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
78702a780c0SDan Carpenter 	if (port->state == BR_STATE_DISABLED ||
78802a780c0SDan Carpenter 	    port->state == BR_STATE_BLOCKING)
789eb1d1641SHerbert Xu 		goto out;
790eb1d1641SHerbert Xu 
791cc0fdd80SLinus Lüssing 	if (query->startup_sent < br->multicast_startup_query_count)
792cc0fdd80SLinus Lüssing 		query->startup_sent++;
793eb1d1641SHerbert Xu 
794cc0fdd80SLinus Lüssing 	br_multicast_send_query(port->br, port, query);
795eb1d1641SHerbert Xu 
796eb1d1641SHerbert Xu out:
797eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
798eb1d1641SHerbert Xu }
799eb1d1641SHerbert Xu 
80088c1f37fSAllen Pais static void br_ip4_multicast_port_query_expired(struct timer_list *t)
801cc0fdd80SLinus Lüssing {
80288c1f37fSAllen Pais 	struct net_bridge_port *port = from_timer(port, t, ip4_own_query.timer);
803cc0fdd80SLinus Lüssing 
80490010b36SLinus Lüssing 	br_multicast_port_query_expired(port, &port->ip4_own_query);
805cc0fdd80SLinus Lüssing }
806cc0fdd80SLinus Lüssing 
807cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
80888c1f37fSAllen Pais static void br_ip6_multicast_port_query_expired(struct timer_list *t)
809cc0fdd80SLinus Lüssing {
81088c1f37fSAllen Pais 	struct net_bridge_port *port = from_timer(port, t, ip6_own_query.timer);
811cc0fdd80SLinus Lüssing 
81290010b36SLinus Lüssing 	br_multicast_port_query_expired(port, &port->ip6_own_query);
813cc0fdd80SLinus Lüssing }
814cc0fdd80SLinus Lüssing #endif
815cc0fdd80SLinus Lüssing 
816147c1e9bSNogah Frankel static void br_mc_disabled_update(struct net_device *dev, bool value)
817147c1e9bSNogah Frankel {
818147c1e9bSNogah Frankel 	struct switchdev_attr attr = {
819147c1e9bSNogah Frankel 		.orig_dev = dev,
820147c1e9bSNogah Frankel 		.id = SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED,
821147c1e9bSNogah Frankel 		.flags = SWITCHDEV_F_DEFER,
82213cefad2SNikolay Aleksandrov 		.u.mc_disabled = !value,
823147c1e9bSNogah Frankel 	};
824147c1e9bSNogah Frankel 
825147c1e9bSNogah Frankel 	switchdev_port_attr_set(dev, &attr);
826147c1e9bSNogah Frankel }
827147c1e9bSNogah Frankel 
8281080ab95SNikolay Aleksandrov int br_multicast_add_port(struct net_bridge_port *port)
829eb1d1641SHerbert Xu {
8307f0aec7aSNikolay Aleksandrov 	port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
831eb1d1641SHerbert Xu 
83288c1f37fSAllen Pais 	timer_setup(&port->multicast_router_timer,
83388c1f37fSAllen Pais 		    br_multicast_router_expired, 0);
83488c1f37fSAllen Pais 	timer_setup(&port->ip4_own_query.timer,
83588c1f37fSAllen Pais 		    br_ip4_multicast_port_query_expired, 0);
836cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
83788c1f37fSAllen Pais 	timer_setup(&port->ip6_own_query.timer,
83888c1f37fSAllen Pais 		    br_ip6_multicast_port_query_expired, 0);
839cc0fdd80SLinus Lüssing #endif
84013cefad2SNikolay Aleksandrov 	br_mc_disabled_update(port->dev,
84113cefad2SNikolay Aleksandrov 			      br_opt_get(port->br, BROPT_MULTICAST_ENABLED));
842147c1e9bSNogah Frankel 
8431080ab95SNikolay Aleksandrov 	port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
8441080ab95SNikolay Aleksandrov 	if (!port->mcast_stats)
8451080ab95SNikolay Aleksandrov 		return -ENOMEM;
8461080ab95SNikolay Aleksandrov 
8471080ab95SNikolay Aleksandrov 	return 0;
848eb1d1641SHerbert Xu }
849eb1d1641SHerbert Xu 
850eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port)
851eb1d1641SHerbert Xu {
852e10177abSSatish Ashok 	struct net_bridge *br = port->br;
853e10177abSSatish Ashok 	struct net_bridge_port_group *pg;
854e10177abSSatish Ashok 	struct hlist_node *n;
855e10177abSSatish Ashok 
856e10177abSSatish Ashok 	/* Take care of the remaining groups, only perm ones should be left */
857e10177abSSatish Ashok 	spin_lock_bh(&br->multicast_lock);
858e10177abSSatish Ashok 	hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
859e10177abSSatish Ashok 		br_multicast_del_pg(br, pg);
860e10177abSSatish Ashok 	spin_unlock_bh(&br->multicast_lock);
861eb1d1641SHerbert Xu 	del_timer_sync(&port->multicast_router_timer);
8621080ab95SNikolay Aleksandrov 	free_percpu(port->mcast_stats);
863eb1d1641SHerbert Xu }
864eb1d1641SHerbert Xu 
86590010b36SLinus Lüssing static void br_multicast_enable(struct bridge_mcast_own_query *query)
866561f1103SHerbert Xu {
867cc0fdd80SLinus Lüssing 	query->startup_sent = 0;
868561f1103SHerbert Xu 
869cc0fdd80SLinus Lüssing 	if (try_to_del_timer_sync(&query->timer) >= 0 ||
870cc0fdd80SLinus Lüssing 	    del_timer(&query->timer))
871cc0fdd80SLinus Lüssing 		mod_timer(&query->timer, jiffies);
872561f1103SHerbert Xu }
873561f1103SHerbert Xu 
8747cb3f921SNikolay Aleksandrov static void __br_multicast_enable_port(struct net_bridge_port *port)
875eb1d1641SHerbert Xu {
876eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
877eb1d1641SHerbert Xu 
87813cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED) || !netif_running(br->dev))
8797cb3f921SNikolay Aleksandrov 		return;
880eb1d1641SHerbert Xu 
88190010b36SLinus Lüssing 	br_multicast_enable(&port->ip4_own_query);
882cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
88390010b36SLinus Lüssing 	br_multicast_enable(&port->ip6_own_query);
884cc0fdd80SLinus Lüssing #endif
8857f0aec7aSNikolay Aleksandrov 	if (port->multicast_router == MDB_RTR_TYPE_PERM &&
8867f0aec7aSNikolay Aleksandrov 	    hlist_unhashed(&port->rlist))
887754bc547SSatish Ashok 		br_multicast_add_router(br, port);
8887cb3f921SNikolay Aleksandrov }
889eb1d1641SHerbert Xu 
8907cb3f921SNikolay Aleksandrov void br_multicast_enable_port(struct net_bridge_port *port)
8917cb3f921SNikolay Aleksandrov {
8927cb3f921SNikolay Aleksandrov 	struct net_bridge *br = port->br;
8937cb3f921SNikolay Aleksandrov 
8947cb3f921SNikolay Aleksandrov 	spin_lock(&br->multicast_lock);
8957cb3f921SNikolay Aleksandrov 	__br_multicast_enable_port(port);
896eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
897eb1d1641SHerbert Xu }
898eb1d1641SHerbert Xu 
899eb1d1641SHerbert Xu void br_multicast_disable_port(struct net_bridge_port *port)
900eb1d1641SHerbert Xu {
901eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
902eb1d1641SHerbert Xu 	struct net_bridge_port_group *pg;
903b67bfe0dSSasha Levin 	struct hlist_node *n;
904eb1d1641SHerbert Xu 
905eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
906b67bfe0dSSasha Levin 	hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
9079d06b6d8SElad Raz 		if (!(pg->flags & MDB_PG_FLAGS_PERMANENT))
908eb1d1641SHerbert Xu 			br_multicast_del_pg(br, pg);
909eb1d1641SHerbert Xu 
910f12e7d95SNogah Frankel 	__del_port_router(port);
911f12e7d95SNogah Frankel 
912eb1d1641SHerbert Xu 	del_timer(&port->multicast_router_timer);
91390010b36SLinus Lüssing 	del_timer(&port->ip4_own_query.timer);
914cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
91590010b36SLinus Lüssing 	del_timer(&port->ip6_own_query.timer);
916cc0fdd80SLinus Lüssing #endif
917eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
918eb1d1641SHerbert Xu }
919eb1d1641SHerbert Xu 
9208ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
921eb1d1641SHerbert Xu 					 struct net_bridge_port *port,
92206499098SVlad Yasevich 					 struct sk_buff *skb,
92306499098SVlad Yasevich 					 u16 vid)
924eb1d1641SHerbert Xu {
9256db6f0eaSFelix Fietkau 	const unsigned char *src;
926eb1d1641SHerbert Xu 	struct igmpv3_report *ih;
927eb1d1641SHerbert Xu 	struct igmpv3_grec *grec;
928eb1d1641SHerbert Xu 	int i;
929eb1d1641SHerbert Xu 	int len;
930eb1d1641SHerbert Xu 	int num;
931eb1d1641SHerbert Xu 	int type;
932eb1d1641SHerbert Xu 	int err = 0;
933eb1d1641SHerbert Xu 	__be32 group;
934eb1d1641SHerbert Xu 
935eb1d1641SHerbert Xu 	ih = igmpv3_report_hdr(skb);
936eb1d1641SHerbert Xu 	num = ntohs(ih->ngrec);
937c2d4fbd2SLinus Lüssing 	len = skb_transport_offset(skb) + sizeof(*ih);
938eb1d1641SHerbert Xu 
939eb1d1641SHerbert Xu 	for (i = 0; i < num; i++) {
940eb1d1641SHerbert Xu 		len += sizeof(*grec);
941*ba5ea614SLinus Lüssing 		if (!ip_mc_may_pull(skb, len))
942eb1d1641SHerbert Xu 			return -EINVAL;
943eb1d1641SHerbert Xu 
944fd218cf9SHerbert Xu 		grec = (void *)(skb->data + len - sizeof(*grec));
945eb1d1641SHerbert Xu 		group = grec->grec_mca;
946eb1d1641SHerbert Xu 		type = grec->grec_type;
947eb1d1641SHerbert Xu 
9488eabf95cSEric Dumazet 		len += ntohs(grec->grec_nsrcs) * 4;
949*ba5ea614SLinus Lüssing 		if (!ip_mc_may_pull(skb, len))
950eb1d1641SHerbert Xu 			return -EINVAL;
951eb1d1641SHerbert Xu 
952eb1d1641SHerbert Xu 		/* We treat this as an IGMPv2 report for now. */
953eb1d1641SHerbert Xu 		switch (type) {
954eb1d1641SHerbert Xu 		case IGMPV3_MODE_IS_INCLUDE:
955eb1d1641SHerbert Xu 		case IGMPV3_MODE_IS_EXCLUDE:
956eb1d1641SHerbert Xu 		case IGMPV3_CHANGE_TO_INCLUDE:
957eb1d1641SHerbert Xu 		case IGMPV3_CHANGE_TO_EXCLUDE:
958eb1d1641SHerbert Xu 		case IGMPV3_ALLOW_NEW_SOURCES:
959eb1d1641SHerbert Xu 		case IGMPV3_BLOCK_OLD_SOURCES:
960eb1d1641SHerbert Xu 			break;
961eb1d1641SHerbert Xu 
962eb1d1641SHerbert Xu 		default:
963eb1d1641SHerbert Xu 			continue;
964eb1d1641SHerbert Xu 		}
965eb1d1641SHerbert Xu 
9666db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
967bc8c20acSSatish Ashok 		if ((type == IGMPV3_CHANGE_TO_INCLUDE ||
968bc8c20acSSatish Ashok 		     type == IGMPV3_MODE_IS_INCLUDE) &&
969bc8c20acSSatish Ashok 		    ntohs(grec->grec_nsrcs) == 0) {
9706db6f0eaSFelix Fietkau 			br_ip4_multicast_leave_group(br, port, group, vid, src);
971bc8c20acSSatish Ashok 		} else {
9726db6f0eaSFelix Fietkau 			err = br_ip4_multicast_add_group(br, port, group, vid,
9736db6f0eaSFelix Fietkau 							 src);
974eb1d1641SHerbert Xu 			if (err)
975eb1d1641SHerbert Xu 				break;
976eb1d1641SHerbert Xu 		}
977bc8c20acSSatish Ashok 	}
978eb1d1641SHerbert Xu 
979eb1d1641SHerbert Xu 	return err;
980eb1d1641SHerbert Xu }
981eb1d1641SHerbert Xu 
982dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
98308b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_mld2_report(struct net_bridge *br,
98408b202b6SYOSHIFUJI Hideaki 					struct net_bridge_port *port,
98506499098SVlad Yasevich 					struct sk_buff *skb,
98606499098SVlad Yasevich 					u16 vid)
98708b202b6SYOSHIFUJI Hideaki {
988*ba5ea614SLinus Lüssing 	unsigned int nsrcs_offset;
9896db6f0eaSFelix Fietkau 	const unsigned char *src;
99008b202b6SYOSHIFUJI Hideaki 	struct icmp6hdr *icmp6h;
99108b202b6SYOSHIFUJI Hideaki 	struct mld2_grec *grec;
992*ba5ea614SLinus Lüssing 	unsigned int grec_len;
99308b202b6SYOSHIFUJI Hideaki 	int i;
99408b202b6SYOSHIFUJI Hideaki 	int len;
99508b202b6SYOSHIFUJI Hideaki 	int num;
99608b202b6SYOSHIFUJI Hideaki 	int err = 0;
99708b202b6SYOSHIFUJI Hideaki 
998*ba5ea614SLinus Lüssing 	if (!ipv6_mc_may_pull(skb, sizeof(*icmp6h)))
99908b202b6SYOSHIFUJI Hideaki 		return -EINVAL;
100008b202b6SYOSHIFUJI Hideaki 
100108b202b6SYOSHIFUJI Hideaki 	icmp6h = icmp6_hdr(skb);
100208b202b6SYOSHIFUJI Hideaki 	num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
1003c2d4fbd2SLinus Lüssing 	len = skb_transport_offset(skb) + sizeof(*icmp6h);
100408b202b6SYOSHIFUJI Hideaki 
100508b202b6SYOSHIFUJI Hideaki 	for (i = 0; i < num; i++) {
100608b202b6SYOSHIFUJI Hideaki 		__be16 *nsrcs, _nsrcs;
100708b202b6SYOSHIFUJI Hideaki 
1008*ba5ea614SLinus Lüssing 		nsrcs_offset = len + offsetof(struct mld2_grec, grec_nsrcs);
1009*ba5ea614SLinus Lüssing 
1010*ba5ea614SLinus Lüssing 		if (skb_transport_offset(skb) + ipv6_transport_len(skb) <
1011*ba5ea614SLinus Lüssing 		    nsrcs_offset + sizeof(_nsrcs))
1012*ba5ea614SLinus Lüssing 			return -EINVAL;
1013*ba5ea614SLinus Lüssing 
1014*ba5ea614SLinus Lüssing 		nsrcs = skb_header_pointer(skb, nsrcs_offset,
101508b202b6SYOSHIFUJI Hideaki 					   sizeof(_nsrcs), &_nsrcs);
101608b202b6SYOSHIFUJI Hideaki 		if (!nsrcs)
101708b202b6SYOSHIFUJI Hideaki 			return -EINVAL;
101808b202b6SYOSHIFUJI Hideaki 
1019*ba5ea614SLinus Lüssing 		grec_len = sizeof(*grec) +
1020*ba5ea614SLinus Lüssing 			   sizeof(struct in6_addr) * ntohs(*nsrcs);
1021*ba5ea614SLinus Lüssing 
1022*ba5ea614SLinus Lüssing 		if (!ipv6_mc_may_pull(skb, len + grec_len))
102308b202b6SYOSHIFUJI Hideaki 			return -EINVAL;
102408b202b6SYOSHIFUJI Hideaki 
102508b202b6SYOSHIFUJI Hideaki 		grec = (struct mld2_grec *)(skb->data + len);
1026*ba5ea614SLinus Lüssing 		len += grec_len;
102708b202b6SYOSHIFUJI Hideaki 
102808b202b6SYOSHIFUJI Hideaki 		/* We treat these as MLDv1 reports for now. */
102908b202b6SYOSHIFUJI Hideaki 		switch (grec->grec_type) {
103008b202b6SYOSHIFUJI Hideaki 		case MLD2_MODE_IS_INCLUDE:
103108b202b6SYOSHIFUJI Hideaki 		case MLD2_MODE_IS_EXCLUDE:
103208b202b6SYOSHIFUJI Hideaki 		case MLD2_CHANGE_TO_INCLUDE:
103308b202b6SYOSHIFUJI Hideaki 		case MLD2_CHANGE_TO_EXCLUDE:
103408b202b6SYOSHIFUJI Hideaki 		case MLD2_ALLOW_NEW_SOURCES:
103508b202b6SYOSHIFUJI Hideaki 		case MLD2_BLOCK_OLD_SOURCES:
103608b202b6SYOSHIFUJI Hideaki 			break;
103708b202b6SYOSHIFUJI Hideaki 
103808b202b6SYOSHIFUJI Hideaki 		default:
103908b202b6SYOSHIFUJI Hideaki 			continue;
104008b202b6SYOSHIFUJI Hideaki 		}
104108b202b6SYOSHIFUJI Hideaki 
10426db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
1043bc8c20acSSatish Ashok 		if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE ||
1044bc8c20acSSatish Ashok 		     grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
1045bc8c20acSSatish Ashok 		    ntohs(*nsrcs) == 0) {
1046bc8c20acSSatish Ashok 			br_ip6_multicast_leave_group(br, port, &grec->grec_mca,
10476db6f0eaSFelix Fietkau 						     vid, src);
1048bc8c20acSSatish Ashok 		} else {
1049bc8c20acSSatish Ashok 			err = br_ip6_multicast_add_group(br, port,
10506db6f0eaSFelix Fietkau 							 &grec->grec_mca, vid,
10516db6f0eaSFelix Fietkau 							 src);
10529264251eSDavide Caratti 			if (err)
105308b202b6SYOSHIFUJI Hideaki 				break;
105408b202b6SYOSHIFUJI Hideaki 		}
1055bc8c20acSSatish Ashok 	}
105608b202b6SYOSHIFUJI Hideaki 
105708b202b6SYOSHIFUJI Hideaki 	return err;
105808b202b6SYOSHIFUJI Hideaki }
105908b202b6SYOSHIFUJI Hideaki #endif
106008b202b6SYOSHIFUJI Hideaki 
1061dc4eb53aSLinus Lüssing static bool br_ip4_multicast_select_querier(struct net_bridge *br,
10622cd41431SLinus Lüssing 					    struct net_bridge_port *port,
1063dc4eb53aSLinus Lüssing 					    __be32 saddr)
1064dc4eb53aSLinus Lüssing {
1065dc4eb53aSLinus Lüssing 	if (!timer_pending(&br->ip4_own_query.timer) &&
1066dc4eb53aSLinus Lüssing 	    !timer_pending(&br->ip4_other_query.timer))
1067dc4eb53aSLinus Lüssing 		goto update;
1068dc4eb53aSLinus Lüssing 
1069dc4eb53aSLinus Lüssing 	if (!br->ip4_querier.addr.u.ip4)
1070dc4eb53aSLinus Lüssing 		goto update;
1071dc4eb53aSLinus Lüssing 
1072dc4eb53aSLinus Lüssing 	if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4))
1073dc4eb53aSLinus Lüssing 		goto update;
1074dc4eb53aSLinus Lüssing 
1075dc4eb53aSLinus Lüssing 	return false;
1076dc4eb53aSLinus Lüssing 
1077dc4eb53aSLinus Lüssing update:
1078dc4eb53aSLinus Lüssing 	br->ip4_querier.addr.u.ip4 = saddr;
1079dc4eb53aSLinus Lüssing 
10802cd41431SLinus Lüssing 	/* update protected by general multicast_lock by caller */
10812cd41431SLinus Lüssing 	rcu_assign_pointer(br->ip4_querier.port, port);
10822cd41431SLinus Lüssing 
1083dc4eb53aSLinus Lüssing 	return true;
1084dc4eb53aSLinus Lüssing }
1085dc4eb53aSLinus Lüssing 
1086dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
1087dc4eb53aSLinus Lüssing static bool br_ip6_multicast_select_querier(struct net_bridge *br,
10882cd41431SLinus Lüssing 					    struct net_bridge_port *port,
1089dc4eb53aSLinus Lüssing 					    struct in6_addr *saddr)
1090dc4eb53aSLinus Lüssing {
1091dc4eb53aSLinus Lüssing 	if (!timer_pending(&br->ip6_own_query.timer) &&
1092dc4eb53aSLinus Lüssing 	    !timer_pending(&br->ip6_other_query.timer))
1093dc4eb53aSLinus Lüssing 		goto update;
1094dc4eb53aSLinus Lüssing 
1095dc4eb53aSLinus Lüssing 	if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0)
1096dc4eb53aSLinus Lüssing 		goto update;
1097dc4eb53aSLinus Lüssing 
1098dc4eb53aSLinus Lüssing 	return false;
1099dc4eb53aSLinus Lüssing 
1100dc4eb53aSLinus Lüssing update:
1101dc4eb53aSLinus Lüssing 	br->ip6_querier.addr.u.ip6 = *saddr;
1102dc4eb53aSLinus Lüssing 
11032cd41431SLinus Lüssing 	/* update protected by general multicast_lock by caller */
11042cd41431SLinus Lüssing 	rcu_assign_pointer(br->ip6_querier.port, port);
11052cd41431SLinus Lüssing 
1106dc4eb53aSLinus Lüssing 	return true;
1107dc4eb53aSLinus Lüssing }
1108dc4eb53aSLinus Lüssing #endif
1109dc4eb53aSLinus Lüssing 
1110dc4eb53aSLinus Lüssing static bool br_multicast_select_querier(struct net_bridge *br,
11112cd41431SLinus Lüssing 					struct net_bridge_port *port,
1112dc4eb53aSLinus Lüssing 					struct br_ip *saddr)
1113dc4eb53aSLinus Lüssing {
1114dc4eb53aSLinus Lüssing 	switch (saddr->proto) {
1115dc4eb53aSLinus Lüssing 	case htons(ETH_P_IP):
11162cd41431SLinus Lüssing 		return br_ip4_multicast_select_querier(br, port, saddr->u.ip4);
1117dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
1118dc4eb53aSLinus Lüssing 	case htons(ETH_P_IPV6):
11192cd41431SLinus Lüssing 		return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6);
1120dc4eb53aSLinus Lüssing #endif
1121dc4eb53aSLinus Lüssing 	}
1122dc4eb53aSLinus Lüssing 
1123dc4eb53aSLinus Lüssing 	return false;
1124dc4eb53aSLinus Lüssing }
1125dc4eb53aSLinus Lüssing 
1126cc0fdd80SLinus Lüssing static void
112790010b36SLinus Lüssing br_multicast_update_query_timer(struct net_bridge *br,
112890010b36SLinus Lüssing 				struct bridge_mcast_other_query *query,
1129b00589afSLinus Lüssing 				unsigned long max_delay)
1130b00589afSLinus Lüssing {
113190010b36SLinus Lüssing 	if (!timer_pending(&query->timer))
113290010b36SLinus Lüssing 		query->delay_time = jiffies + max_delay;
1133b00589afSLinus Lüssing 
113490010b36SLinus Lüssing 	mod_timer(&query->timer, jiffies + br->multicast_querier_interval);
1135b00589afSLinus Lüssing }
1136b00589afSLinus Lüssing 
11376d549648SNogah Frankel static void br_port_mc_router_state_change(struct net_bridge_port *p,
11386d549648SNogah Frankel 					   bool is_mc_router)
11396d549648SNogah Frankel {
11406d549648SNogah Frankel 	struct switchdev_attr attr = {
11416d549648SNogah Frankel 		.orig_dev = p->dev,
11426d549648SNogah Frankel 		.id = SWITCHDEV_ATTR_ID_PORT_MROUTER,
11436d549648SNogah Frankel 		.flags = SWITCHDEV_F_DEFER,
11446d549648SNogah Frankel 		.u.mrouter = is_mc_router,
11456d549648SNogah Frankel 	};
11466d549648SNogah Frankel 
11476d549648SNogah Frankel 	switchdev_port_attr_set(p->dev, &attr);
11486d549648SNogah Frankel }
11496d549648SNogah Frankel 
11507e80c124Sstephen hemminger /*
11517c77602fSCong Wang  * Add port to router_list
11527e80c124Sstephen hemminger  *  list is maintained ordered by pointer value
11537e80c124Sstephen hemminger  *  and locked by br->multicast_lock and RCU
11547e80c124Sstephen hemminger  */
11550909e117SHerbert Xu static void br_multicast_add_router(struct net_bridge *br,
11560909e117SHerbert Xu 				    struct net_bridge_port *port)
11570909e117SHerbert Xu {
1158dcdca2c4Sstephen hemminger 	struct net_bridge_port *p;
1159b67bfe0dSSasha Levin 	struct hlist_node *slot = NULL;
11600909e117SHerbert Xu 
11611a040eacSNikolay Aleksandrov 	if (!hlist_unhashed(&port->rlist))
11621a040eacSNikolay Aleksandrov 		return;
11631a040eacSNikolay Aleksandrov 
1164b67bfe0dSSasha Levin 	hlist_for_each_entry(p, &br->router_list, rlist) {
11657e80c124Sstephen hemminger 		if ((unsigned long) port >= (unsigned long) p)
11667e80c124Sstephen hemminger 			break;
1167b67bfe0dSSasha Levin 		slot = &p->rlist;
1168dcdca2c4Sstephen hemminger 	}
11690909e117SHerbert Xu 
11707e80c124Sstephen hemminger 	if (slot)
11711d023284SKen Helias 		hlist_add_behind_rcu(&port->rlist, slot);
1172dcdca2c4Sstephen hemminger 	else
1173dcdca2c4Sstephen hemminger 		hlist_add_head_rcu(&port->rlist, &br->router_list);
1174949f1e39SSatish Ashok 	br_rtr_notify(br->dev, port, RTM_NEWMDB);
11756d549648SNogah Frankel 	br_port_mc_router_state_change(port, true);
11760909e117SHerbert Xu }
11770909e117SHerbert Xu 
1178eb1d1641SHerbert Xu static void br_multicast_mark_router(struct net_bridge *br,
1179eb1d1641SHerbert Xu 				     struct net_bridge_port *port)
1180eb1d1641SHerbert Xu {
1181eb1d1641SHerbert Xu 	unsigned long now = jiffies;
1182eb1d1641SHerbert Xu 
1183eb1d1641SHerbert Xu 	if (!port) {
118477041420SYotam Gigi 		if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) {
118577041420SYotam Gigi 			if (!timer_pending(&br->multicast_router_timer))
118677041420SYotam Gigi 				br_mc_router_state_change(br, true);
1187eb1d1641SHerbert Xu 			mod_timer(&br->multicast_router_timer,
1188eb1d1641SHerbert Xu 				  now + br->multicast_querier_interval);
118977041420SYotam Gigi 		}
1190eb1d1641SHerbert Xu 		return;
1191eb1d1641SHerbert Xu 	}
1192eb1d1641SHerbert Xu 
1193a55d8246SNikolay Aleksandrov 	if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
1194a55d8246SNikolay Aleksandrov 	    port->multicast_router == MDB_RTR_TYPE_PERM)
1195eb1d1641SHerbert Xu 		return;
1196eb1d1641SHerbert Xu 
11970909e117SHerbert Xu 	br_multicast_add_router(br, port);
1198eb1d1641SHerbert Xu 
1199eb1d1641SHerbert Xu 	mod_timer(&port->multicast_router_timer,
1200eb1d1641SHerbert Xu 		  now + br->multicast_querier_interval);
1201eb1d1641SHerbert Xu }
1202eb1d1641SHerbert Xu 
1203eb1d1641SHerbert Xu static void br_multicast_query_received(struct net_bridge *br,
1204eb1d1641SHerbert Xu 					struct net_bridge_port *port,
120590010b36SLinus Lüssing 					struct bridge_mcast_other_query *query,
1206dc4eb53aSLinus Lüssing 					struct br_ip *saddr,
1207b00589afSLinus Lüssing 					unsigned long max_delay)
1208eb1d1641SHerbert Xu {
12092cd41431SLinus Lüssing 	if (!br_multicast_select_querier(br, port, saddr))
1210eb1d1641SHerbert Xu 		return;
1211eb1d1641SHerbert Xu 
1212dc4eb53aSLinus Lüssing 	br_multicast_update_query_timer(br, query, max_delay);
12135a2de63fSHangbin Liu 
12145a2de63fSHangbin Liu 	/* Based on RFC4541, section 2.1.1 IGMP Forwarding Rules,
12155a2de63fSHangbin Liu 	 * the arrival port for IGMP Queries where the source address
12165a2de63fSHangbin Liu 	 * is 0.0.0.0 should not be added to router port list.
12175a2de63fSHangbin Liu 	 */
12185a2de63fSHangbin Liu 	if ((saddr->proto == htons(ETH_P_IP) && saddr->u.ip4) ||
12190fe5119eSNikolay Aleksandrov 	    saddr->proto == htons(ETH_P_IPV6))
1220eb1d1641SHerbert Xu 		br_multicast_mark_router(br, port);
1221eb1d1641SHerbert Xu }
1222eb1d1641SHerbert Xu 
12239c2e955cSzhong jiang static void br_ip4_multicast_query(struct net_bridge *br,
1224eb1d1641SHerbert Xu 				   struct net_bridge_port *port,
122506499098SVlad Yasevich 				   struct sk_buff *skb,
122606499098SVlad Yasevich 				   u16 vid)
1227eb1d1641SHerbert Xu {
1228*ba5ea614SLinus Lüssing 	unsigned int transport_len = ip_transport_len(skb);
1229b71d1d42SEric Dumazet 	const struct iphdr *iph = ip_hdr(skb);
1230eb1d1641SHerbert Xu 	struct igmphdr *ih = igmp_hdr(skb);
1231eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
1232eb1d1641SHerbert Xu 	struct igmpv3_query *ih3;
1233eb1d1641SHerbert Xu 	struct net_bridge_port_group *p;
1234e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
1235dc4eb53aSLinus Lüssing 	struct br_ip saddr;
1236eb1d1641SHerbert Xu 	unsigned long max_delay;
1237eb1d1641SHerbert Xu 	unsigned long now = jiffies;
1238eb1d1641SHerbert Xu 	__be32 group;
1239eb1d1641SHerbert Xu 
1240eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
1241eb1d1641SHerbert Xu 	if (!netif_running(br->dev) ||
1242eb1d1641SHerbert Xu 	    (port && port->state == BR_STATE_DISABLED))
1243eb1d1641SHerbert Xu 		goto out;
1244eb1d1641SHerbert Xu 
1245eb1d1641SHerbert Xu 	group = ih->group;
1246eb1d1641SHerbert Xu 
1247*ba5ea614SLinus Lüssing 	if (transport_len == sizeof(*ih)) {
1248eb1d1641SHerbert Xu 		max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1249eb1d1641SHerbert Xu 
1250eb1d1641SHerbert Xu 		if (!max_delay) {
1251eb1d1641SHerbert Xu 			max_delay = 10 * HZ;
1252eb1d1641SHerbert Xu 			group = 0;
1253eb1d1641SHerbert Xu 		}
1254*ba5ea614SLinus Lüssing 	} else if (transport_len >= sizeof(*ih3)) {
1255eb1d1641SHerbert Xu 		ih3 = igmpv3_query_hdr(skb);
1256eb1d1641SHerbert Xu 		if (ih3->nsrcs)
1257bec68ff1SYOSHIFUJI Hideaki 			goto out;
1258eb1d1641SHerbert Xu 
12590ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 		max_delay = ih3->code ?
12600ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 			    IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
12619afd85c9SLinus Lüssing 	} else {
12629ed973ccSLinus Lüssing 		goto out;
12639ed973ccSLinus Lüssing 	}
12649ed973ccSLinus Lüssing 
1265dc4eb53aSLinus Lüssing 	if (!group) {
1266dc4eb53aSLinus Lüssing 		saddr.proto = htons(ETH_P_IP);
1267dc4eb53aSLinus Lüssing 		saddr.u.ip4 = iph->saddr;
1268b00589afSLinus Lüssing 
1269dc4eb53aSLinus Lüssing 		br_multicast_query_received(br, port, &br->ip4_other_query,
1270dc4eb53aSLinus Lüssing 					    &saddr, max_delay);
1271eb1d1641SHerbert Xu 		goto out;
1272dc4eb53aSLinus Lüssing 	}
1273eb1d1641SHerbert Xu 
127419e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip4_get(br, group, vid);
1275eb1d1641SHerbert Xu 	if (!mp)
1276eb1d1641SHerbert Xu 		goto out;
1277eb1d1641SHerbert Xu 
1278eb1d1641SHerbert Xu 	max_delay *= br->multicast_last_member_count;
1279eb1d1641SHerbert Xu 
1280ff0fd34eSAndrew Lunn 	if (mp->host_joined &&
1281eb1d1641SHerbert Xu 	    (timer_pending(&mp->timer) ?
1282eb1d1641SHerbert Xu 	     time_after(mp->timer.expires, now + max_delay) :
1283eb1d1641SHerbert Xu 	     try_to_del_timer_sync(&mp->timer) >= 0))
1284eb1d1641SHerbert Xu 		mod_timer(&mp->timer, now + max_delay);
1285eb1d1641SHerbert Xu 
1286e8051688SEric Dumazet 	for (pp = &mp->ports;
1287e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
1288e8051688SEric Dumazet 	     pp = &p->next) {
1289eb1d1641SHerbert Xu 		if (timer_pending(&p->timer) ?
1290eb1d1641SHerbert Xu 		    time_after(p->timer.expires, now + max_delay) :
1291eb1d1641SHerbert Xu 		    try_to_del_timer_sync(&p->timer) >= 0)
129224f9cdcbSHerbert Xu 			mod_timer(&p->timer, now + max_delay);
1293eb1d1641SHerbert Xu 	}
1294eb1d1641SHerbert Xu 
1295eb1d1641SHerbert Xu out:
1296eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1297eb1d1641SHerbert Xu }
1298eb1d1641SHerbert Xu 
1299dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
130008b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_query(struct net_bridge *br,
130108b202b6SYOSHIFUJI Hideaki 				  struct net_bridge_port *port,
130206499098SVlad Yasevich 				  struct sk_buff *skb,
130306499098SVlad Yasevich 				  u16 vid)
130408b202b6SYOSHIFUJI Hideaki {
1305*ba5ea614SLinus Lüssing 	unsigned int transport_len = ipv6_transport_len(skb);
1306b71d1d42SEric Dumazet 	const struct ipv6hdr *ip6h = ipv6_hdr(skb);
1307eca2a43bSstephen hemminger 	struct mld_msg *mld;
130808b202b6SYOSHIFUJI Hideaki 	struct net_bridge_mdb_entry *mp;
130908b202b6SYOSHIFUJI Hideaki 	struct mld2_query *mld2q;
1310e8051688SEric Dumazet 	struct net_bridge_port_group *p;
1311e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
1312dc4eb53aSLinus Lüssing 	struct br_ip saddr;
131308b202b6SYOSHIFUJI Hideaki 	unsigned long max_delay;
131408b202b6SYOSHIFUJI Hideaki 	unsigned long now = jiffies;
1315856ce5d0SLinus Lüssing 	unsigned int offset = skb_transport_offset(skb);
1316b71d1d42SEric Dumazet 	const struct in6_addr *group = NULL;
13179ed973ccSLinus Lüssing 	bool is_general_query;
131808b202b6SYOSHIFUJI Hideaki 	int err = 0;
131908b202b6SYOSHIFUJI Hideaki 
132008b202b6SYOSHIFUJI Hideaki 	spin_lock(&br->multicast_lock);
132108b202b6SYOSHIFUJI Hideaki 	if (!netif_running(br->dev) ||
132208b202b6SYOSHIFUJI Hideaki 	    (port && port->state == BR_STATE_DISABLED))
132308b202b6SYOSHIFUJI Hideaki 		goto out;
132408b202b6SYOSHIFUJI Hideaki 
1325*ba5ea614SLinus Lüssing 	if (transport_len == sizeof(*mld)) {
1326856ce5d0SLinus Lüssing 		if (!pskb_may_pull(skb, offset + sizeof(*mld))) {
132708b202b6SYOSHIFUJI Hideaki 			err = -EINVAL;
132808b202b6SYOSHIFUJI Hideaki 			goto out;
132908b202b6SYOSHIFUJI Hideaki 		}
133008b202b6SYOSHIFUJI Hideaki 		mld = (struct mld_msg *) icmp6_hdr(skb);
13314715213dSLi RongQing 		max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
133208b202b6SYOSHIFUJI Hideaki 		if (max_delay)
133308b202b6SYOSHIFUJI Hideaki 			group = &mld->mld_mca;
1334248ba8ecSLinus Lüssing 	} else {
1335856ce5d0SLinus Lüssing 		if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) {
133608b202b6SYOSHIFUJI Hideaki 			err = -EINVAL;
133708b202b6SYOSHIFUJI Hideaki 			goto out;
133808b202b6SYOSHIFUJI Hideaki 		}
133908b202b6SYOSHIFUJI Hideaki 		mld2q = (struct mld2_query *)icmp6_hdr(skb);
134008b202b6SYOSHIFUJI Hideaki 		if (!mld2q->mld2q_nsrcs)
134108b202b6SYOSHIFUJI Hideaki 			group = &mld2q->mld2q_mca;
1342e3f5b170SDaniel Borkmann 
1343e3f5b170SDaniel Borkmann 		max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
134408b202b6SYOSHIFUJI Hideaki 	}
134508b202b6SYOSHIFUJI Hideaki 
13469ed973ccSLinus Lüssing 	is_general_query = group && ipv6_addr_any(group);
13479ed973ccSLinus Lüssing 
1348dc4eb53aSLinus Lüssing 	if (is_general_query) {
1349dc4eb53aSLinus Lüssing 		saddr.proto = htons(ETH_P_IPV6);
1350dc4eb53aSLinus Lüssing 		saddr.u.ip6 = ip6h->saddr;
1351b00589afSLinus Lüssing 
1352dc4eb53aSLinus Lüssing 		br_multicast_query_received(br, port, &br->ip6_other_query,
1353dc4eb53aSLinus Lüssing 					    &saddr, max_delay);
135408b202b6SYOSHIFUJI Hideaki 		goto out;
13556c03ee8bSLinus Lüssing 	} else if (!group) {
13566c03ee8bSLinus Lüssing 		goto out;
1357dc4eb53aSLinus Lüssing 	}
135808b202b6SYOSHIFUJI Hideaki 
135919e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip6_get(br, group, vid);
136008b202b6SYOSHIFUJI Hideaki 	if (!mp)
136108b202b6SYOSHIFUJI Hideaki 		goto out;
136208b202b6SYOSHIFUJI Hideaki 
136308b202b6SYOSHIFUJI Hideaki 	max_delay *= br->multicast_last_member_count;
1364ff0fd34eSAndrew Lunn 	if (mp->host_joined &&
136508b202b6SYOSHIFUJI Hideaki 	    (timer_pending(&mp->timer) ?
136608b202b6SYOSHIFUJI Hideaki 	     time_after(mp->timer.expires, now + max_delay) :
136708b202b6SYOSHIFUJI Hideaki 	     try_to_del_timer_sync(&mp->timer) >= 0))
136808b202b6SYOSHIFUJI Hideaki 		mod_timer(&mp->timer, now + max_delay);
136908b202b6SYOSHIFUJI Hideaki 
1370e8051688SEric Dumazet 	for (pp = &mp->ports;
1371e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
1372e8051688SEric Dumazet 	     pp = &p->next) {
137308b202b6SYOSHIFUJI Hideaki 		if (timer_pending(&p->timer) ?
137408b202b6SYOSHIFUJI Hideaki 		    time_after(p->timer.expires, now + max_delay) :
137508b202b6SYOSHIFUJI Hideaki 		    try_to_del_timer_sync(&p->timer) >= 0)
137624f9cdcbSHerbert Xu 			mod_timer(&p->timer, now + max_delay);
137708b202b6SYOSHIFUJI Hideaki 	}
137808b202b6SYOSHIFUJI Hideaki 
137908b202b6SYOSHIFUJI Hideaki out:
138008b202b6SYOSHIFUJI Hideaki 	spin_unlock(&br->multicast_lock);
138108b202b6SYOSHIFUJI Hideaki 	return err;
138208b202b6SYOSHIFUJI Hideaki }
138308b202b6SYOSHIFUJI Hideaki #endif
138408b202b6SYOSHIFUJI Hideaki 
138590010b36SLinus Lüssing static void
138690010b36SLinus Lüssing br_multicast_leave_group(struct net_bridge *br,
1387eb1d1641SHerbert Xu 			 struct net_bridge_port *port,
1388cc0fdd80SLinus Lüssing 			 struct br_ip *group,
138990010b36SLinus Lüssing 			 struct bridge_mcast_other_query *other_query,
13906db6f0eaSFelix Fietkau 			 struct bridge_mcast_own_query *own_query,
13916db6f0eaSFelix Fietkau 			 const unsigned char *src)
1392eb1d1641SHerbert Xu {
1393eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
1394eb1d1641SHerbert Xu 	struct net_bridge_port_group *p;
1395eb1d1641SHerbert Xu 	unsigned long now;
1396eb1d1641SHerbert Xu 	unsigned long time;
1397eb1d1641SHerbert Xu 
1398eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
1399eb1d1641SHerbert Xu 	if (!netif_running(br->dev) ||
1400544586f7SSatish Ashok 	    (port && port->state == BR_STATE_DISABLED))
1401eb1d1641SHerbert Xu 		goto out;
1402eb1d1641SHerbert Xu 
140319e3a9c9SNikolay Aleksandrov 	mp = br_mdb_ip_get(br, group);
1404eb1d1641SHerbert Xu 	if (!mp)
1405eb1d1641SHerbert Xu 		goto out;
1406eb1d1641SHerbert Xu 
1407544586f7SSatish Ashok 	if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
1408544586f7SSatish Ashok 		struct net_bridge_port_group __rcu **pp;
1409544586f7SSatish Ashok 
1410544586f7SSatish Ashok 		for (pp = &mp->ports;
1411544586f7SSatish Ashok 		     (p = mlock_dereference(*pp, br)) != NULL;
1412544586f7SSatish Ashok 		     pp = &p->next) {
14136db6f0eaSFelix Fietkau 			if (!br_port_group_equal(p, port, src))
1414544586f7SSatish Ashok 				continue;
1415544586f7SSatish Ashok 
1416544586f7SSatish Ashok 			rcu_assign_pointer(*pp, p->next);
1417544586f7SSatish Ashok 			hlist_del_init(&p->mglist);
1418544586f7SSatish Ashok 			del_timer(&p->timer);
14194329596cSNikolay Aleksandrov 			kfree_rcu(p, rcu);
142045ebcce5SElad Raz 			br_mdb_notify(br->dev, port, group, RTM_DELMDB,
142145ebcce5SElad Raz 				      p->flags);
1422544586f7SSatish Ashok 
1423ff0fd34eSAndrew Lunn 			if (!mp->ports && !mp->host_joined &&
1424544586f7SSatish Ashok 			    netif_running(br->dev))
1425544586f7SSatish Ashok 				mod_timer(&mp->timer, jiffies);
1426544586f7SSatish Ashok 		}
1427544586f7SSatish Ashok 		goto out;
1428544586f7SSatish Ashok 	}
1429544586f7SSatish Ashok 
1430544586f7SSatish Ashok 	if (timer_pending(&other_query->timer))
1431544586f7SSatish Ashok 		goto out;
1432544586f7SSatish Ashok 
1433675779adSNikolay Aleksandrov 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER)) {
14346b7df111SCong Wang 		__br_multicast_send_query(br, port, &mp->addr);
14356b7df111SCong Wang 
14366b7df111SCong Wang 		time = jiffies + br->multicast_last_member_count *
14376b7df111SCong Wang 				 br->multicast_last_member_interval;
1438cc0fdd80SLinus Lüssing 
143990010b36SLinus Lüssing 		mod_timer(&own_query->timer, time);
14406b7df111SCong Wang 
14416b7df111SCong Wang 		for (p = mlock_dereference(mp->ports, br);
14426b7df111SCong Wang 		     p != NULL;
14436b7df111SCong Wang 		     p = mlock_dereference(p->next, br)) {
14446db6f0eaSFelix Fietkau 			if (!br_port_group_equal(p, port, src))
14456b7df111SCong Wang 				continue;
14466b7df111SCong Wang 
14476b7df111SCong Wang 			if (!hlist_unhashed(&p->mglist) &&
14486b7df111SCong Wang 			    (timer_pending(&p->timer) ?
14496b7df111SCong Wang 			     time_after(p->timer.expires, time) :
14506b7df111SCong Wang 			     try_to_del_timer_sync(&p->timer) >= 0)) {
14516b7df111SCong Wang 				mod_timer(&p->timer, time);
14526b7df111SCong Wang 			}
14536b7df111SCong Wang 
14546b7df111SCong Wang 			break;
14556b7df111SCong Wang 		}
14566b7df111SCong Wang 	}
14576b7df111SCong Wang 
1458eb1d1641SHerbert Xu 	now = jiffies;
1459eb1d1641SHerbert Xu 	time = now + br->multicast_last_member_count *
1460eb1d1641SHerbert Xu 		     br->multicast_last_member_interval;
1461eb1d1641SHerbert Xu 
1462eb1d1641SHerbert Xu 	if (!port) {
1463ff0fd34eSAndrew Lunn 		if (mp->host_joined &&
1464eb1d1641SHerbert Xu 		    (timer_pending(&mp->timer) ?
1465eb1d1641SHerbert Xu 		     time_after(mp->timer.expires, time) :
1466eb1d1641SHerbert Xu 		     try_to_del_timer_sync(&mp->timer) >= 0)) {
1467eb1d1641SHerbert Xu 			mod_timer(&mp->timer, time);
1468eb1d1641SHerbert Xu 		}
1469454594f3SLinus Lüssing 
1470454594f3SLinus Lüssing 		goto out;
1471454594f3SLinus Lüssing 	}
1472454594f3SLinus Lüssing 
1473454594f3SLinus Lüssing 	for (p = mlock_dereference(mp->ports, br);
1474454594f3SLinus Lüssing 	     p != NULL;
1475454594f3SLinus Lüssing 	     p = mlock_dereference(p->next, br)) {
1476454594f3SLinus Lüssing 		if (p->port != port)
1477454594f3SLinus Lüssing 			continue;
1478454594f3SLinus Lüssing 
1479454594f3SLinus Lüssing 		if (!hlist_unhashed(&p->mglist) &&
1480454594f3SLinus Lüssing 		    (timer_pending(&p->timer) ?
1481454594f3SLinus Lüssing 		     time_after(p->timer.expires, time) :
1482454594f3SLinus Lüssing 		     try_to_del_timer_sync(&p->timer) >= 0)) {
1483454594f3SLinus Lüssing 			mod_timer(&p->timer, time);
1484454594f3SLinus Lüssing 		}
1485454594f3SLinus Lüssing 
1486454594f3SLinus Lüssing 		break;
1487eb1d1641SHerbert Xu 	}
1488eb1d1641SHerbert Xu out:
1489eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1490eb1d1641SHerbert Xu }
1491eb1d1641SHerbert Xu 
14928ef2a9a5SYOSHIFUJI Hideaki static void br_ip4_multicast_leave_group(struct net_bridge *br,
14938ef2a9a5SYOSHIFUJI Hideaki 					 struct net_bridge_port *port,
1494b0e9a30dSVlad Yasevich 					 __be32 group,
14956db6f0eaSFelix Fietkau 					 __u16 vid,
14966db6f0eaSFelix Fietkau 					 const unsigned char *src)
14978ef2a9a5SYOSHIFUJI Hideaki {
14988ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip br_group;
149990010b36SLinus Lüssing 	struct bridge_mcast_own_query *own_query;
15008ef2a9a5SYOSHIFUJI Hideaki 
15018ef2a9a5SYOSHIFUJI Hideaki 	if (ipv4_is_local_multicast(group))
15028ef2a9a5SYOSHIFUJI Hideaki 		return;
15038ef2a9a5SYOSHIFUJI Hideaki 
150490010b36SLinus Lüssing 	own_query = port ? &port->ip4_own_query : &br->ip4_own_query;
150590010b36SLinus Lüssing 
15068ef2a9a5SYOSHIFUJI Hideaki 	br_group.u.ip4 = group;
15078ef2a9a5SYOSHIFUJI Hideaki 	br_group.proto = htons(ETH_P_IP);
1508b0e9a30dSVlad Yasevich 	br_group.vid = vid;
15098ef2a9a5SYOSHIFUJI Hideaki 
151090010b36SLinus Lüssing 	br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
15116db6f0eaSFelix Fietkau 				 own_query, src);
15128ef2a9a5SYOSHIFUJI Hideaki }
15138ef2a9a5SYOSHIFUJI Hideaki 
1514dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
151508b202b6SYOSHIFUJI Hideaki static void br_ip6_multicast_leave_group(struct net_bridge *br,
151608b202b6SYOSHIFUJI Hideaki 					 struct net_bridge_port *port,
1517b0e9a30dSVlad Yasevich 					 const struct in6_addr *group,
15186db6f0eaSFelix Fietkau 					 __u16 vid,
15196db6f0eaSFelix Fietkau 					 const unsigned char *src)
152008b202b6SYOSHIFUJI Hideaki {
152108b202b6SYOSHIFUJI Hideaki 	struct br_ip br_group;
152290010b36SLinus Lüssing 	struct bridge_mcast_own_query *own_query;
152308b202b6SYOSHIFUJI Hideaki 
15243c3769e6SLinus Lüssing 	if (ipv6_addr_is_ll_all_nodes(group))
152508b202b6SYOSHIFUJI Hideaki 		return;
152608b202b6SYOSHIFUJI Hideaki 
152790010b36SLinus Lüssing 	own_query = port ? &port->ip6_own_query : &br->ip6_own_query;
152890010b36SLinus Lüssing 
15294e3fd7a0SAlexey Dobriyan 	br_group.u.ip6 = *group;
153008b202b6SYOSHIFUJI Hideaki 	br_group.proto = htons(ETH_P_IPV6);
1531b0e9a30dSVlad Yasevich 	br_group.vid = vid;
153208b202b6SYOSHIFUJI Hideaki 
153390010b36SLinus Lüssing 	br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
15346db6f0eaSFelix Fietkau 				 own_query, src);
153508b202b6SYOSHIFUJI Hideaki }
153608b202b6SYOSHIFUJI Hideaki #endif
15378ef2a9a5SYOSHIFUJI Hideaki 
15381080ab95SNikolay Aleksandrov static void br_multicast_err_count(const struct net_bridge *br,
15391080ab95SNikolay Aleksandrov 				   const struct net_bridge_port *p,
15401080ab95SNikolay Aleksandrov 				   __be16 proto)
15411080ab95SNikolay Aleksandrov {
15421080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
15431080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats *pstats;
15441080ab95SNikolay Aleksandrov 
1545675779adSNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
15461080ab95SNikolay Aleksandrov 		return;
15471080ab95SNikolay Aleksandrov 
15481080ab95SNikolay Aleksandrov 	if (p)
15491080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
15501080ab95SNikolay Aleksandrov 	else
15511080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
15521080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
15531080ab95SNikolay Aleksandrov 		return;
15541080ab95SNikolay Aleksandrov 
15551080ab95SNikolay Aleksandrov 	pstats = this_cpu_ptr(stats);
15561080ab95SNikolay Aleksandrov 
15571080ab95SNikolay Aleksandrov 	u64_stats_update_begin(&pstats->syncp);
15581080ab95SNikolay Aleksandrov 	switch (proto) {
15591080ab95SNikolay Aleksandrov 	case htons(ETH_P_IP):
15601080ab95SNikolay Aleksandrov 		pstats->mstats.igmp_parse_errors++;
15611080ab95SNikolay Aleksandrov 		break;
15621080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
15631080ab95SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
15641080ab95SNikolay Aleksandrov 		pstats->mstats.mld_parse_errors++;
15651080ab95SNikolay Aleksandrov 		break;
15661080ab95SNikolay Aleksandrov #endif
15671080ab95SNikolay Aleksandrov 	}
15681080ab95SNikolay Aleksandrov 	u64_stats_update_end(&pstats->syncp);
15691080ab95SNikolay Aleksandrov }
15701080ab95SNikolay Aleksandrov 
157191b02d3dSNikolay Aleksandrov static void br_multicast_pim(struct net_bridge *br,
157291b02d3dSNikolay Aleksandrov 			     struct net_bridge_port *port,
157391b02d3dSNikolay Aleksandrov 			     const struct sk_buff *skb)
157491b02d3dSNikolay Aleksandrov {
157591b02d3dSNikolay Aleksandrov 	unsigned int offset = skb_transport_offset(skb);
157691b02d3dSNikolay Aleksandrov 	struct pimhdr *pimhdr, _pimhdr;
157791b02d3dSNikolay Aleksandrov 
157891b02d3dSNikolay Aleksandrov 	pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr);
157991b02d3dSNikolay Aleksandrov 	if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION ||
158091b02d3dSNikolay Aleksandrov 	    pim_hdr_type(pimhdr) != PIM_TYPE_HELLO)
158191b02d3dSNikolay Aleksandrov 		return;
158291b02d3dSNikolay Aleksandrov 
158391b02d3dSNikolay Aleksandrov 	br_multicast_mark_router(br, port);
158491b02d3dSNikolay Aleksandrov }
158591b02d3dSNikolay Aleksandrov 
1586eb1d1641SHerbert Xu static int br_multicast_ipv4_rcv(struct net_bridge *br,
1587eb1d1641SHerbert Xu 				 struct net_bridge_port *port,
158806499098SVlad Yasevich 				 struct sk_buff *skb,
158906499098SVlad Yasevich 				 u16 vid)
1590eb1d1641SHerbert Xu {
15916db6f0eaSFelix Fietkau 	const unsigned char *src;
1592eb1d1641SHerbert Xu 	struct igmphdr *ih;
1593eb1d1641SHerbert Xu 	int err;
1594eb1d1641SHerbert Xu 
1595*ba5ea614SLinus Lüssing 	err = ip_mc_check_igmp(skb);
1596eb1d1641SHerbert Xu 
15979afd85c9SLinus Lüssing 	if (err == -ENOMSG) {
159891b02d3dSNikolay Aleksandrov 		if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) {
1599bd4265feSHerbert Xu 			BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
160091b02d3dSNikolay Aleksandrov 		} else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) {
160191b02d3dSNikolay Aleksandrov 			if (ip_hdr(skb)->protocol == IPPROTO_PIM)
160291b02d3dSNikolay Aleksandrov 				br_multicast_pim(br, port, skb);
160391b02d3dSNikolay Aleksandrov 		}
1604eb1d1641SHerbert Xu 		return 0;
16059afd85c9SLinus Lüssing 	} else if (err < 0) {
16061080ab95SNikolay Aleksandrov 		br_multicast_err_count(br, port, skb->protocol);
16079afd85c9SLinus Lüssing 		return err;
1608bd4265feSHerbert Xu 	}
1609eb1d1641SHerbert Xu 
16109afd85c9SLinus Lüssing 	ih = igmp_hdr(skb);
16116db6f0eaSFelix Fietkau 	src = eth_hdr(skb)->h_source;
16121080ab95SNikolay Aleksandrov 	BR_INPUT_SKB_CB(skb)->igmp = ih->type;
1613eb1d1641SHerbert Xu 
1614eb1d1641SHerbert Xu 	switch (ih->type) {
1615eb1d1641SHerbert Xu 	case IGMP_HOST_MEMBERSHIP_REPORT:
1616eb1d1641SHerbert Xu 	case IGMPV2_HOST_MEMBERSHIP_REPORT:
161762b2bcb4SFernando Luis Vázquez Cao 		BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
16186db6f0eaSFelix Fietkau 		err = br_ip4_multicast_add_group(br, port, ih->group, vid, src);
1619eb1d1641SHerbert Xu 		break;
1620eb1d1641SHerbert Xu 	case IGMPV3_HOST_MEMBERSHIP_REPORT:
1621*ba5ea614SLinus Lüssing 		err = br_ip4_multicast_igmp3_report(br, port, skb, vid);
1622eb1d1641SHerbert Xu 		break;
1623eb1d1641SHerbert Xu 	case IGMP_HOST_MEMBERSHIP_QUERY:
1624*ba5ea614SLinus Lüssing 		br_ip4_multicast_query(br, port, skb, vid);
1625eb1d1641SHerbert Xu 		break;
1626eb1d1641SHerbert Xu 	case IGMP_HOST_LEAVE_MESSAGE:
16276db6f0eaSFelix Fietkau 		br_ip4_multicast_leave_group(br, port, ih->group, vid, src);
1628eb1d1641SHerbert Xu 		break;
1629eb1d1641SHerbert Xu 	}
1630eb1d1641SHerbert Xu 
1631a65056ecSNikolay Aleksandrov 	br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
16321080ab95SNikolay Aleksandrov 			   BR_MCAST_DIR_RX);
16331080ab95SNikolay Aleksandrov 
1634eb1d1641SHerbert Xu 	return err;
1635eb1d1641SHerbert Xu }
1636eb1d1641SHerbert Xu 
1637dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
163808b202b6SYOSHIFUJI Hideaki static int br_multicast_ipv6_rcv(struct net_bridge *br,
163908b202b6SYOSHIFUJI Hideaki 				 struct net_bridge_port *port,
164006499098SVlad Yasevich 				 struct sk_buff *skb,
164106499098SVlad Yasevich 				 u16 vid)
164208b202b6SYOSHIFUJI Hideaki {
16436db6f0eaSFelix Fietkau 	const unsigned char *src;
16449afd85c9SLinus Lüssing 	struct mld_msg *mld;
164508b202b6SYOSHIFUJI Hideaki 	int err;
164608b202b6SYOSHIFUJI Hideaki 
1647*ba5ea614SLinus Lüssing 	err = ipv6_mc_check_mld(skb);
164808b202b6SYOSHIFUJI Hideaki 
16499afd85c9SLinus Lüssing 	if (err == -ENOMSG) {
16509afd85c9SLinus Lüssing 		if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
16518fad9c39SLinus Lüssing 			BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
165208b202b6SYOSHIFUJI Hideaki 		return 0;
16539afd85c9SLinus Lüssing 	} else if (err < 0) {
16541080ab95SNikolay Aleksandrov 		br_multicast_err_count(br, port, skb->protocol);
16559afd85c9SLinus Lüssing 		return err;
165608b202b6SYOSHIFUJI Hideaki 	}
165708b202b6SYOSHIFUJI Hideaki 
16589afd85c9SLinus Lüssing 	mld = (struct mld_msg *)skb_transport_header(skb);
16591080ab95SNikolay Aleksandrov 	BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type;
166008b202b6SYOSHIFUJI Hideaki 
16619afd85c9SLinus Lüssing 	switch (mld->mld_type) {
166208b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_REPORT:
16636db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
1664fc2af6c7SFernando Luis Vázquez Cao 		BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
16656db6f0eaSFelix Fietkau 		err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid,
16666db6f0eaSFelix Fietkau 						 src);
166708b202b6SYOSHIFUJI Hideaki 		break;
166808b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MLD2_REPORT:
1669*ba5ea614SLinus Lüssing 		err = br_ip6_multicast_mld2_report(br, port, skb, vid);
167008b202b6SYOSHIFUJI Hideaki 		break;
167108b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_QUERY:
1672*ba5ea614SLinus Lüssing 		err = br_ip6_multicast_query(br, port, skb, vid);
167308b202b6SYOSHIFUJI Hideaki 		break;
167408b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_REDUCTION:
16756db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
16766db6f0eaSFelix Fietkau 		br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src);
16779afd85c9SLinus Lüssing 		break;
167808b202b6SYOSHIFUJI Hideaki 	}
167908b202b6SYOSHIFUJI Hideaki 
1680a65056ecSNikolay Aleksandrov 	br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
16811080ab95SNikolay Aleksandrov 			   BR_MCAST_DIR_RX);
16821080ab95SNikolay Aleksandrov 
168308b202b6SYOSHIFUJI Hideaki 	return err;
168408b202b6SYOSHIFUJI Hideaki }
168508b202b6SYOSHIFUJI Hideaki #endif
168608b202b6SYOSHIFUJI Hideaki 
1687eb1d1641SHerbert Xu int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
168806499098SVlad Yasevich 		     struct sk_buff *skb, u16 vid)
1689eb1d1641SHerbert Xu {
16901080ab95SNikolay Aleksandrov 	int ret = 0;
16911080ab95SNikolay Aleksandrov 
16921fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 	BR_INPUT_SKB_CB(skb)->igmp = 0;
16931fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 	BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
16941fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 
169513cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
1696eb1d1641SHerbert Xu 		return 0;
1697eb1d1641SHerbert Xu 
1698eb1d1641SHerbert Xu 	switch (skb->protocol) {
1699eb1d1641SHerbert Xu 	case htons(ETH_P_IP):
17001080ab95SNikolay Aleksandrov 		ret = br_multicast_ipv4_rcv(br, port, skb, vid);
17011080ab95SNikolay Aleksandrov 		break;
1702dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
170308b202b6SYOSHIFUJI Hideaki 	case htons(ETH_P_IPV6):
17041080ab95SNikolay Aleksandrov 		ret = br_multicast_ipv6_rcv(br, port, skb, vid);
17051080ab95SNikolay Aleksandrov 		break;
170608b202b6SYOSHIFUJI Hideaki #endif
1707eb1d1641SHerbert Xu 	}
1708eb1d1641SHerbert Xu 
17091080ab95SNikolay Aleksandrov 	return ret;
1710eb1d1641SHerbert Xu }
1711eb1d1641SHerbert Xu 
1712cc0fdd80SLinus Lüssing static void br_multicast_query_expired(struct net_bridge *br,
17132cd41431SLinus Lüssing 				       struct bridge_mcast_own_query *query,
17142cd41431SLinus Lüssing 				       struct bridge_mcast_querier *querier)
1715cc0fdd80SLinus Lüssing {
1716cc0fdd80SLinus Lüssing 	spin_lock(&br->multicast_lock);
1717cc0fdd80SLinus Lüssing 	if (query->startup_sent < br->multicast_startup_query_count)
1718cc0fdd80SLinus Lüssing 		query->startup_sent++;
1719cc0fdd80SLinus Lüssing 
172071d9f614SEric Dumazet 	RCU_INIT_POINTER(querier->port, NULL);
1721cc0fdd80SLinus Lüssing 	br_multicast_send_query(br, NULL, query);
1722cc0fdd80SLinus Lüssing 	spin_unlock(&br->multicast_lock);
1723cc0fdd80SLinus Lüssing }
1724cc0fdd80SLinus Lüssing 
172588c1f37fSAllen Pais static void br_ip4_multicast_query_expired(struct timer_list *t)
1726eb1d1641SHerbert Xu {
172788c1f37fSAllen Pais 	struct net_bridge *br = from_timer(br, t, ip4_own_query.timer);
1728eb1d1641SHerbert Xu 
17292cd41431SLinus Lüssing 	br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier);
1730eb1d1641SHerbert Xu }
1731eb1d1641SHerbert Xu 
1732cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
173388c1f37fSAllen Pais static void br_ip6_multicast_query_expired(struct timer_list *t)
1734cc0fdd80SLinus Lüssing {
173588c1f37fSAllen Pais 	struct net_bridge *br = from_timer(br, t, ip6_own_query.timer);
1736cc0fdd80SLinus Lüssing 
17372cd41431SLinus Lüssing 	br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier);
1738cc0fdd80SLinus Lüssing }
1739cc0fdd80SLinus Lüssing #endif
1740cc0fdd80SLinus Lüssing 
1741eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br)
1742eb1d1641SHerbert Xu {
1743d08c6bc0SNikolay Aleksandrov 	br->hash_max = BR_MULTICAST_DEFAULT_HASH_MAX;
1744eb1d1641SHerbert Xu 
17457f0aec7aSNikolay Aleksandrov 	br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
1746eb1d1641SHerbert Xu 	br->multicast_last_member_count = 2;
1747eb1d1641SHerbert Xu 	br->multicast_startup_query_count = 2;
1748eb1d1641SHerbert Xu 
1749eb1d1641SHerbert Xu 	br->multicast_last_member_interval = HZ;
1750eb1d1641SHerbert Xu 	br->multicast_query_response_interval = 10 * HZ;
1751eb1d1641SHerbert Xu 	br->multicast_startup_query_interval = 125 * HZ / 4;
1752eb1d1641SHerbert Xu 	br->multicast_query_interval = 125 * HZ;
1753eb1d1641SHerbert Xu 	br->multicast_querier_interval = 255 * HZ;
1754eb1d1641SHerbert Xu 	br->multicast_membership_interval = 260 * HZ;
1755eb1d1641SHerbert Xu 
175690010b36SLinus Lüssing 	br->ip4_other_query.delay_time = 0;
17572cd41431SLinus Lüssing 	br->ip4_querier.port = NULL;
1758aa2ae3e7SNikolay Aleksandrov 	br->multicast_igmp_version = 2;
1759cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
1760aa2ae3e7SNikolay Aleksandrov 	br->multicast_mld_version = 1;
176190010b36SLinus Lüssing 	br->ip6_other_query.delay_time = 0;
17622cd41431SLinus Lüssing 	br->ip6_querier.port = NULL;
1763cc0fdd80SLinus Lüssing #endif
17646919622aSIdo Schimmel 	br_opt_toggle(br, BROPT_MULTICAST_ENABLED, true);
1765675779adSNikolay Aleksandrov 	br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true);
1766b00589afSLinus Lüssing 
1767eb1d1641SHerbert Xu 	spin_lock_init(&br->multicast_lock);
176888c1f37fSAllen Pais 	timer_setup(&br->multicast_router_timer,
176988c1f37fSAllen Pais 		    br_multicast_local_router_expired, 0);
177088c1f37fSAllen Pais 	timer_setup(&br->ip4_other_query.timer,
177188c1f37fSAllen Pais 		    br_ip4_multicast_querier_expired, 0);
177288c1f37fSAllen Pais 	timer_setup(&br->ip4_own_query.timer,
177388c1f37fSAllen Pais 		    br_ip4_multicast_query_expired, 0);
1774cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
177588c1f37fSAllen Pais 	timer_setup(&br->ip6_other_query.timer,
177688c1f37fSAllen Pais 		    br_ip6_multicast_querier_expired, 0);
177788c1f37fSAllen Pais 	timer_setup(&br->ip6_own_query.timer,
177888c1f37fSAllen Pais 		    br_ip6_multicast_query_expired, 0);
1779cc0fdd80SLinus Lüssing #endif
178019e3a9c9SNikolay Aleksandrov 	INIT_HLIST_HEAD(&br->mdb_list);
1781eb1d1641SHerbert Xu }
1782eb1d1641SHerbert Xu 
1783cc0fdd80SLinus Lüssing static void __br_multicast_open(struct net_bridge *br,
178490010b36SLinus Lüssing 				struct bridge_mcast_own_query *query)
1785eb1d1641SHerbert Xu {
1786cc0fdd80SLinus Lüssing 	query->startup_sent = 0;
1787eb1d1641SHerbert Xu 
178813cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
1789eb1d1641SHerbert Xu 		return;
1790eb1d1641SHerbert Xu 
1791cc0fdd80SLinus Lüssing 	mod_timer(&query->timer, jiffies);
1792cc0fdd80SLinus Lüssing }
1793cc0fdd80SLinus Lüssing 
1794cc0fdd80SLinus Lüssing void br_multicast_open(struct net_bridge *br)
1795cc0fdd80SLinus Lüssing {
179690010b36SLinus Lüssing 	__br_multicast_open(br, &br->ip4_own_query);
1797cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
179890010b36SLinus Lüssing 	__br_multicast_open(br, &br->ip6_own_query);
1799cc0fdd80SLinus Lüssing #endif
1800eb1d1641SHerbert Xu }
1801eb1d1641SHerbert Xu 
1802eb1d1641SHerbert Xu void br_multicast_stop(struct net_bridge *br)
1803eb1d1641SHerbert Xu {
1804eb1d1641SHerbert Xu 	del_timer_sync(&br->multicast_router_timer);
180590010b36SLinus Lüssing 	del_timer_sync(&br->ip4_other_query.timer);
180690010b36SLinus Lüssing 	del_timer_sync(&br->ip4_own_query.timer);
1807cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
180890010b36SLinus Lüssing 	del_timer_sync(&br->ip6_other_query.timer);
180990010b36SLinus Lüssing 	del_timer_sync(&br->ip6_own_query.timer);
1810cc0fdd80SLinus Lüssing #endif
1811e10177abSSatish Ashok }
1812e10177abSSatish Ashok 
1813e10177abSSatish Ashok void br_multicast_dev_del(struct net_bridge *br)
1814e10177abSSatish Ashok {
1815e10177abSSatish Ashok 	struct net_bridge_mdb_entry *mp;
181619e3a9c9SNikolay Aleksandrov 	struct hlist_node *tmp;
1817eb1d1641SHerbert Xu 
1818eb1d1641SHerbert Xu 	spin_lock_bh(&br->multicast_lock);
181919e3a9c9SNikolay Aleksandrov 	hlist_for_each_entry_safe(mp, tmp, &br->mdb_list, mdb_node) {
1820eb1d1641SHerbert Xu 		del_timer(&mp->timer);
182119e3a9c9SNikolay Aleksandrov 		rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode,
182219e3a9c9SNikolay Aleksandrov 				       br_mdb_rht_params);
182319e3a9c9SNikolay Aleksandrov 		hlist_del_rcu(&mp->mdb_node);
18244329596cSNikolay Aleksandrov 		kfree_rcu(mp, rcu);
1825eb1d1641SHerbert Xu 	}
1826eb1d1641SHerbert Xu 	spin_unlock_bh(&br->multicast_lock);
182719e3a9c9SNikolay Aleksandrov 
18284329596cSNikolay Aleksandrov 	rcu_barrier();
1829eb1d1641SHerbert Xu }
18300909e117SHerbert Xu 
18310909e117SHerbert Xu int br_multicast_set_router(struct net_bridge *br, unsigned long val)
18320909e117SHerbert Xu {
18336ae4ae8eSLinus Lüssing 	int err = -EINVAL;
18340909e117SHerbert Xu 
18350909e117SHerbert Xu 	spin_lock_bh(&br->multicast_lock);
18360909e117SHerbert Xu 
18370909e117SHerbert Xu 	switch (val) {
18387f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_DISABLED:
18397f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_PERM:
184077041420SYotam Gigi 		br_mc_router_state_change(br, val == MDB_RTR_TYPE_PERM);
18410909e117SHerbert Xu 		del_timer(&br->multicast_router_timer);
184277041420SYotam Gigi 		br->multicast_router = val;
184377041420SYotam Gigi 		err = 0;
184477041420SYotam Gigi 		break;
18457f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP_QUERY:
184677041420SYotam Gigi 		if (br->multicast_router != MDB_RTR_TYPE_TEMP_QUERY)
184777041420SYotam Gigi 			br_mc_router_state_change(br, false);
18480909e117SHerbert Xu 		br->multicast_router = val;
18490909e117SHerbert Xu 		err = 0;
18500909e117SHerbert Xu 		break;
18510909e117SHerbert Xu 	}
18520909e117SHerbert Xu 
18530909e117SHerbert Xu 	spin_unlock_bh(&br->multicast_lock);
18540909e117SHerbert Xu 
18550909e117SHerbert Xu 	return err;
18560909e117SHerbert Xu }
18570909e117SHerbert Xu 
18587f0aec7aSNikolay Aleksandrov static void __del_port_router(struct net_bridge_port *p)
18597f0aec7aSNikolay Aleksandrov {
18607f0aec7aSNikolay Aleksandrov 	if (hlist_unhashed(&p->rlist))
18617f0aec7aSNikolay Aleksandrov 		return;
18627f0aec7aSNikolay Aleksandrov 	hlist_del_init_rcu(&p->rlist);
18637f0aec7aSNikolay Aleksandrov 	br_rtr_notify(p->br->dev, p, RTM_DELMDB);
18646d549648SNogah Frankel 	br_port_mc_router_state_change(p, false);
1865f12e7d95SNogah Frankel 
1866f12e7d95SNogah Frankel 	/* don't allow timer refresh */
1867f12e7d95SNogah Frankel 	if (p->multicast_router == MDB_RTR_TYPE_TEMP)
1868f12e7d95SNogah Frankel 		p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
18697f0aec7aSNikolay Aleksandrov }
18707f0aec7aSNikolay Aleksandrov 
18710909e117SHerbert Xu int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
18720909e117SHerbert Xu {
18730909e117SHerbert Xu 	struct net_bridge *br = p->br;
1874a55d8246SNikolay Aleksandrov 	unsigned long now = jiffies;
18756ae4ae8eSLinus Lüssing 	int err = -EINVAL;
18760909e117SHerbert Xu 
18770909e117SHerbert Xu 	spin_lock(&br->multicast_lock);
18784950cfd1SNikolay Aleksandrov 	if (p->multicast_router == val) {
1879a55d8246SNikolay Aleksandrov 		/* Refresh the temp router port timer */
1880a55d8246SNikolay Aleksandrov 		if (p->multicast_router == MDB_RTR_TYPE_TEMP)
1881a55d8246SNikolay Aleksandrov 			mod_timer(&p->multicast_router_timer,
1882a55d8246SNikolay Aleksandrov 				  now + br->multicast_querier_interval);
18834950cfd1SNikolay Aleksandrov 		err = 0;
18844950cfd1SNikolay Aleksandrov 		goto unlock;
18854950cfd1SNikolay Aleksandrov 	}
18860909e117SHerbert Xu 	switch (val) {
18877f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_DISABLED:
18887f0aec7aSNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_DISABLED;
18897f0aec7aSNikolay Aleksandrov 		__del_port_router(p);
18900909e117SHerbert Xu 		del_timer(&p->multicast_router_timer);
18910909e117SHerbert Xu 		break;
18927f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP_QUERY:
18937f0aec7aSNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
18947f0aec7aSNikolay Aleksandrov 		__del_port_router(p);
18957f0aec7aSNikolay Aleksandrov 		break;
18967f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_PERM:
18977f0aec7aSNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_PERM;
18987f0aec7aSNikolay Aleksandrov 		del_timer(&p->multicast_router_timer);
18990909e117SHerbert Xu 		br_multicast_add_router(br, p);
19000909e117SHerbert Xu 		break;
1901a55d8246SNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP:
1902a55d8246SNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_TEMP;
1903a55d8246SNikolay Aleksandrov 		br_multicast_mark_router(br, p);
1904a55d8246SNikolay Aleksandrov 		break;
19057f0aec7aSNikolay Aleksandrov 	default:
19067f0aec7aSNikolay Aleksandrov 		goto unlock;
19070909e117SHerbert Xu 	}
19087f0aec7aSNikolay Aleksandrov 	err = 0;
19097f0aec7aSNikolay Aleksandrov unlock:
19100909e117SHerbert Xu 	spin_unlock(&br->multicast_lock);
19110909e117SHerbert Xu 
19120909e117SHerbert Xu 	return err;
19130909e117SHerbert Xu }
1914561f1103SHerbert Xu 
1915cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br,
191690010b36SLinus Lüssing 				       struct bridge_mcast_own_query *query)
1917561f1103SHerbert Xu {
1918561f1103SHerbert Xu 	struct net_bridge_port *port;
191974857216SHerbert Xu 
1920cc0fdd80SLinus Lüssing 	__br_multicast_open(br, query);
192174857216SHerbert Xu 
192274857216SHerbert Xu 	list_for_each_entry(port, &br->port_list, list) {
192374857216SHerbert Xu 		if (port->state == BR_STATE_DISABLED ||
192474857216SHerbert Xu 		    port->state == BR_STATE_BLOCKING)
192574857216SHerbert Xu 			continue;
192674857216SHerbert Xu 
192790010b36SLinus Lüssing 		if (query == &br->ip4_own_query)
192890010b36SLinus Lüssing 			br_multicast_enable(&port->ip4_own_query);
1929cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
1930cc0fdd80SLinus Lüssing 		else
193190010b36SLinus Lüssing 			br_multicast_enable(&port->ip6_own_query);
1932cc0fdd80SLinus Lüssing #endif
193374857216SHerbert Xu 	}
193474857216SHerbert Xu }
193574857216SHerbert Xu 
193674857216SHerbert Xu int br_multicast_toggle(struct net_bridge *br, unsigned long val)
193774857216SHerbert Xu {
19387cb3f921SNikolay Aleksandrov 	struct net_bridge_port *port;
1939561f1103SHerbert Xu 
1940ef5e0d82SAndrey Vagin 	spin_lock_bh(&br->multicast_lock);
194113cefad2SNikolay Aleksandrov 	if (!!br_opt_get(br, BROPT_MULTICAST_ENABLED) == !!val)
1942561f1103SHerbert Xu 		goto unlock;
1943561f1103SHerbert Xu 
194413cefad2SNikolay Aleksandrov 	br_mc_disabled_update(br->dev, val);
194513cefad2SNikolay Aleksandrov 	br_opt_toggle(br, BROPT_MULTICAST_ENABLED, !!val);
194613cefad2SNikolay Aleksandrov 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
1947561f1103SHerbert Xu 		goto unlock;
1948561f1103SHerbert Xu 
19493a7fda06SHerbert Xu 	if (!netif_running(br->dev))
19503a7fda06SHerbert Xu 		goto unlock;
19513a7fda06SHerbert Xu 
19527cb3f921SNikolay Aleksandrov 	br_multicast_open(br);
19537cb3f921SNikolay Aleksandrov 	list_for_each_entry(port, &br->port_list, list)
19547cb3f921SNikolay Aleksandrov 		__br_multicast_enable_port(port);
1955561f1103SHerbert Xu 
1956561f1103SHerbert Xu unlock:
1957ef5e0d82SAndrey Vagin 	spin_unlock_bh(&br->multicast_lock);
1958561f1103SHerbert Xu 
1959a26d94bfSYueHaibing 	return 0;
1960561f1103SHerbert Xu }
1961b195167fSHerbert Xu 
19629341b988SIdo Schimmel bool br_multicast_enabled(const struct net_device *dev)
19639341b988SIdo Schimmel {
19649341b988SIdo Schimmel 	struct net_bridge *br = netdev_priv(dev);
19659341b988SIdo Schimmel 
196613cefad2SNikolay Aleksandrov 	return !!br_opt_get(br, BROPT_MULTICAST_ENABLED);
19679341b988SIdo Schimmel }
19689341b988SIdo Schimmel EXPORT_SYMBOL_GPL(br_multicast_enabled);
19699341b988SIdo Schimmel 
19700912bda4SYotam Gigi bool br_multicast_router(const struct net_device *dev)
19710912bda4SYotam Gigi {
19720912bda4SYotam Gigi 	struct net_bridge *br = netdev_priv(dev);
19730912bda4SYotam Gigi 	bool is_router;
19740912bda4SYotam Gigi 
19750912bda4SYotam Gigi 	spin_lock_bh(&br->multicast_lock);
19760912bda4SYotam Gigi 	is_router = br_multicast_is_router(br);
19770912bda4SYotam Gigi 	spin_unlock_bh(&br->multicast_lock);
19780912bda4SYotam Gigi 	return is_router;
19790912bda4SYotam Gigi }
19800912bda4SYotam Gigi EXPORT_SYMBOL_GPL(br_multicast_router);
19810912bda4SYotam Gigi 
1982c5c23260SHerbert Xu int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
1983c5c23260SHerbert Xu {
1984b00589afSLinus Lüssing 	unsigned long max_delay;
1985b00589afSLinus Lüssing 
1986c5c23260SHerbert Xu 	val = !!val;
1987c5c23260SHerbert Xu 
1988c5c23260SHerbert Xu 	spin_lock_bh(&br->multicast_lock);
1989675779adSNikolay Aleksandrov 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER) == val)
1990c5c23260SHerbert Xu 		goto unlock;
1991c5c23260SHerbert Xu 
1992675779adSNikolay Aleksandrov 	br_opt_toggle(br, BROPT_MULTICAST_QUERIER, !!val);
1993b00589afSLinus Lüssing 	if (!val)
1994b00589afSLinus Lüssing 		goto unlock;
1995b00589afSLinus Lüssing 
1996b00589afSLinus Lüssing 	max_delay = br->multicast_query_response_interval;
1997b00589afSLinus Lüssing 
199890010b36SLinus Lüssing 	if (!timer_pending(&br->ip4_other_query.timer))
199990010b36SLinus Lüssing 		br->ip4_other_query.delay_time = jiffies + max_delay;
2000cc0fdd80SLinus Lüssing 
200190010b36SLinus Lüssing 	br_multicast_start_querier(br, &br->ip4_own_query);
2002cc0fdd80SLinus Lüssing 
2003cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
200490010b36SLinus Lüssing 	if (!timer_pending(&br->ip6_other_query.timer))
200590010b36SLinus Lüssing 		br->ip6_other_query.delay_time = jiffies + max_delay;
2006cc0fdd80SLinus Lüssing 
200790010b36SLinus Lüssing 	br_multicast_start_querier(br, &br->ip6_own_query);
2008cc0fdd80SLinus Lüssing #endif
2009c5c23260SHerbert Xu 
2010c5c23260SHerbert Xu unlock:
2011c5c23260SHerbert Xu 	spin_unlock_bh(&br->multicast_lock);
2012c5c23260SHerbert Xu 
2013c5c23260SHerbert Xu 	return 0;
2014c5c23260SHerbert Xu }
2015c5c23260SHerbert Xu 
20165e923585SNikolay Aleksandrov int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val)
20175e923585SNikolay Aleksandrov {
20185e923585SNikolay Aleksandrov 	/* Currently we support only version 2 and 3 */
20195e923585SNikolay Aleksandrov 	switch (val) {
20205e923585SNikolay Aleksandrov 	case 2:
20215e923585SNikolay Aleksandrov 	case 3:
20225e923585SNikolay Aleksandrov 		break;
20235e923585SNikolay Aleksandrov 	default:
20245e923585SNikolay Aleksandrov 		return -EINVAL;
20255e923585SNikolay Aleksandrov 	}
20265e923585SNikolay Aleksandrov 
20275e923585SNikolay Aleksandrov 	spin_lock_bh(&br->multicast_lock);
20285e923585SNikolay Aleksandrov 	br->multicast_igmp_version = val;
20295e923585SNikolay Aleksandrov 	spin_unlock_bh(&br->multicast_lock);
20305e923585SNikolay Aleksandrov 
20315e923585SNikolay Aleksandrov 	return 0;
20325e923585SNikolay Aleksandrov }
20335e923585SNikolay Aleksandrov 
2034aa2ae3e7SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
2035aa2ae3e7SNikolay Aleksandrov int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val)
2036aa2ae3e7SNikolay Aleksandrov {
2037aa2ae3e7SNikolay Aleksandrov 	/* Currently we support version 1 and 2 */
2038aa2ae3e7SNikolay Aleksandrov 	switch (val) {
2039aa2ae3e7SNikolay Aleksandrov 	case 1:
2040aa2ae3e7SNikolay Aleksandrov 	case 2:
2041aa2ae3e7SNikolay Aleksandrov 		break;
2042aa2ae3e7SNikolay Aleksandrov 	default:
2043aa2ae3e7SNikolay Aleksandrov 		return -EINVAL;
2044aa2ae3e7SNikolay Aleksandrov 	}
2045aa2ae3e7SNikolay Aleksandrov 
2046aa2ae3e7SNikolay Aleksandrov 	spin_lock_bh(&br->multicast_lock);
2047aa2ae3e7SNikolay Aleksandrov 	br->multicast_mld_version = val;
2048aa2ae3e7SNikolay Aleksandrov 	spin_unlock_bh(&br->multicast_lock);
2049aa2ae3e7SNikolay Aleksandrov 
2050aa2ae3e7SNikolay Aleksandrov 	return 0;
2051aa2ae3e7SNikolay Aleksandrov }
2052aa2ae3e7SNikolay Aleksandrov #endif
2053aa2ae3e7SNikolay Aleksandrov 
205407f8ac4aSLinus Lüssing /**
205507f8ac4aSLinus Lüssing  * br_multicast_list_adjacent - Returns snooped multicast addresses
205607f8ac4aSLinus Lüssing  * @dev:	The bridge port adjacent to which to retrieve addresses
205707f8ac4aSLinus Lüssing  * @br_ip_list:	The list to store found, snooped multicast IP addresses in
205807f8ac4aSLinus Lüssing  *
205907f8ac4aSLinus Lüssing  * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
206007f8ac4aSLinus Lüssing  * snooping feature on all bridge ports of dev's bridge device, excluding
206107f8ac4aSLinus Lüssing  * the addresses from dev itself.
206207f8ac4aSLinus Lüssing  *
206307f8ac4aSLinus Lüssing  * Returns the number of items added to br_ip_list.
206407f8ac4aSLinus Lüssing  *
206507f8ac4aSLinus Lüssing  * Notes:
206607f8ac4aSLinus Lüssing  * - br_ip_list needs to be initialized by caller
206707f8ac4aSLinus Lüssing  * - br_ip_list might contain duplicates in the end
206807f8ac4aSLinus Lüssing  *   (needs to be taken care of by caller)
206907f8ac4aSLinus Lüssing  * - br_ip_list needs to be freed by caller
207007f8ac4aSLinus Lüssing  */
207107f8ac4aSLinus Lüssing int br_multicast_list_adjacent(struct net_device *dev,
207207f8ac4aSLinus Lüssing 			       struct list_head *br_ip_list)
207307f8ac4aSLinus Lüssing {
207407f8ac4aSLinus Lüssing 	struct net_bridge *br;
207507f8ac4aSLinus Lüssing 	struct net_bridge_port *port;
207607f8ac4aSLinus Lüssing 	struct net_bridge_port_group *group;
207707f8ac4aSLinus Lüssing 	struct br_ip_list *entry;
207807f8ac4aSLinus Lüssing 	int count = 0;
207907f8ac4aSLinus Lüssing 
208007f8ac4aSLinus Lüssing 	rcu_read_lock();
208107f8ac4aSLinus Lüssing 	if (!br_ip_list || !br_port_exists(dev))
208207f8ac4aSLinus Lüssing 		goto unlock;
208307f8ac4aSLinus Lüssing 
208407f8ac4aSLinus Lüssing 	port = br_port_get_rcu(dev);
208507f8ac4aSLinus Lüssing 	if (!port || !port->br)
208607f8ac4aSLinus Lüssing 		goto unlock;
208707f8ac4aSLinus Lüssing 
208807f8ac4aSLinus Lüssing 	br = port->br;
208907f8ac4aSLinus Lüssing 
209007f8ac4aSLinus Lüssing 	list_for_each_entry_rcu(port, &br->port_list, list) {
209107f8ac4aSLinus Lüssing 		if (!port->dev || port->dev == dev)
209207f8ac4aSLinus Lüssing 			continue;
209307f8ac4aSLinus Lüssing 
209407f8ac4aSLinus Lüssing 		hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
209507f8ac4aSLinus Lüssing 			entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
209607f8ac4aSLinus Lüssing 			if (!entry)
209707f8ac4aSLinus Lüssing 				goto unlock;
209807f8ac4aSLinus Lüssing 
209907f8ac4aSLinus Lüssing 			entry->addr = group->addr;
210007f8ac4aSLinus Lüssing 			list_add(&entry->list, br_ip_list);
210107f8ac4aSLinus Lüssing 			count++;
210207f8ac4aSLinus Lüssing 		}
210307f8ac4aSLinus Lüssing 	}
210407f8ac4aSLinus Lüssing 
210507f8ac4aSLinus Lüssing unlock:
210607f8ac4aSLinus Lüssing 	rcu_read_unlock();
210707f8ac4aSLinus Lüssing 	return count;
210807f8ac4aSLinus Lüssing }
210907f8ac4aSLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
21102cd41431SLinus Lüssing 
21112cd41431SLinus Lüssing /**
2112c34963e2SLinus Lüssing  * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2113c34963e2SLinus Lüssing  * @dev: The bridge port providing the bridge on which to check for a querier
2114c34963e2SLinus Lüssing  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2115c34963e2SLinus Lüssing  *
2116c34963e2SLinus Lüssing  * Checks whether the given interface has a bridge on top and if so returns
2117c34963e2SLinus Lüssing  * true if a valid querier exists anywhere on the bridged link layer.
2118c34963e2SLinus Lüssing  * Otherwise returns false.
2119c34963e2SLinus Lüssing  */
2120c34963e2SLinus Lüssing bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
2121c34963e2SLinus Lüssing {
2122c34963e2SLinus Lüssing 	struct net_bridge *br;
2123c34963e2SLinus Lüssing 	struct net_bridge_port *port;
2124c34963e2SLinus Lüssing 	struct ethhdr eth;
2125c34963e2SLinus Lüssing 	bool ret = false;
2126c34963e2SLinus Lüssing 
2127c34963e2SLinus Lüssing 	rcu_read_lock();
2128c34963e2SLinus Lüssing 	if (!br_port_exists(dev))
2129c34963e2SLinus Lüssing 		goto unlock;
2130c34963e2SLinus Lüssing 
2131c34963e2SLinus Lüssing 	port = br_port_get_rcu(dev);
2132c34963e2SLinus Lüssing 	if (!port || !port->br)
2133c34963e2SLinus Lüssing 		goto unlock;
2134c34963e2SLinus Lüssing 
2135c34963e2SLinus Lüssing 	br = port->br;
2136c34963e2SLinus Lüssing 
2137c34963e2SLinus Lüssing 	memset(&eth, 0, sizeof(eth));
2138c34963e2SLinus Lüssing 	eth.h_proto = htons(proto);
2139c34963e2SLinus Lüssing 
2140c34963e2SLinus Lüssing 	ret = br_multicast_querier_exists(br, &eth);
2141c34963e2SLinus Lüssing 
2142c34963e2SLinus Lüssing unlock:
2143c34963e2SLinus Lüssing 	rcu_read_unlock();
2144c34963e2SLinus Lüssing 	return ret;
2145c34963e2SLinus Lüssing }
2146c34963e2SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
2147c34963e2SLinus Lüssing 
2148c34963e2SLinus Lüssing /**
21492cd41431SLinus Lüssing  * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
21502cd41431SLinus Lüssing  * @dev: The bridge port adjacent to which to check for a querier
21512cd41431SLinus Lüssing  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
21522cd41431SLinus Lüssing  *
21532cd41431SLinus Lüssing  * Checks whether the given interface has a bridge on top and if so returns
21542cd41431SLinus Lüssing  * true if a selected querier is behind one of the other ports of this
21552cd41431SLinus Lüssing  * bridge. Otherwise returns false.
21562cd41431SLinus Lüssing  */
21572cd41431SLinus Lüssing bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
21582cd41431SLinus Lüssing {
21592cd41431SLinus Lüssing 	struct net_bridge *br;
21602cd41431SLinus Lüssing 	struct net_bridge_port *port;
21612cd41431SLinus Lüssing 	bool ret = false;
21622cd41431SLinus Lüssing 
21632cd41431SLinus Lüssing 	rcu_read_lock();
21642cd41431SLinus Lüssing 	if (!br_port_exists(dev))
21652cd41431SLinus Lüssing 		goto unlock;
21662cd41431SLinus Lüssing 
21672cd41431SLinus Lüssing 	port = br_port_get_rcu(dev);
21682cd41431SLinus Lüssing 	if (!port || !port->br)
21692cd41431SLinus Lüssing 		goto unlock;
21702cd41431SLinus Lüssing 
21712cd41431SLinus Lüssing 	br = port->br;
21722cd41431SLinus Lüssing 
21732cd41431SLinus Lüssing 	switch (proto) {
21742cd41431SLinus Lüssing 	case ETH_P_IP:
21752cd41431SLinus Lüssing 		if (!timer_pending(&br->ip4_other_query.timer) ||
21762cd41431SLinus Lüssing 		    rcu_dereference(br->ip4_querier.port) == port)
21772cd41431SLinus Lüssing 			goto unlock;
21782cd41431SLinus Lüssing 		break;
21793993c4e1SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
21802cd41431SLinus Lüssing 	case ETH_P_IPV6:
21812cd41431SLinus Lüssing 		if (!timer_pending(&br->ip6_other_query.timer) ||
21822cd41431SLinus Lüssing 		    rcu_dereference(br->ip6_querier.port) == port)
21832cd41431SLinus Lüssing 			goto unlock;
21842cd41431SLinus Lüssing 		break;
21853993c4e1SLinus Lüssing #endif
21862cd41431SLinus Lüssing 	default:
21872cd41431SLinus Lüssing 		goto unlock;
21882cd41431SLinus Lüssing 	}
21892cd41431SLinus Lüssing 
21902cd41431SLinus Lüssing 	ret = true;
21912cd41431SLinus Lüssing unlock:
21922cd41431SLinus Lüssing 	rcu_read_unlock();
21932cd41431SLinus Lüssing 	return ret;
21942cd41431SLinus Lüssing }
21952cd41431SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
21961080ab95SNikolay Aleksandrov 
21971080ab95SNikolay Aleksandrov static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
2198a65056ecSNikolay Aleksandrov 			       const struct sk_buff *skb, u8 type, u8 dir)
21991080ab95SNikolay Aleksandrov {
22001080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
2201a65056ecSNikolay Aleksandrov 	__be16 proto = skb->protocol;
2202a65056ecSNikolay Aleksandrov 	unsigned int t_len;
22031080ab95SNikolay Aleksandrov 
22041080ab95SNikolay Aleksandrov 	u64_stats_update_begin(&pstats->syncp);
22051080ab95SNikolay Aleksandrov 	switch (proto) {
22061080ab95SNikolay Aleksandrov 	case htons(ETH_P_IP):
2207a65056ecSNikolay Aleksandrov 		t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
22081080ab95SNikolay Aleksandrov 		switch (type) {
22091080ab95SNikolay Aleksandrov 		case IGMP_HOST_MEMBERSHIP_REPORT:
22101080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v1reports[dir]++;
22111080ab95SNikolay Aleksandrov 			break;
22121080ab95SNikolay Aleksandrov 		case IGMPV2_HOST_MEMBERSHIP_REPORT:
22131080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v2reports[dir]++;
22141080ab95SNikolay Aleksandrov 			break;
22151080ab95SNikolay Aleksandrov 		case IGMPV3_HOST_MEMBERSHIP_REPORT:
22161080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v3reports[dir]++;
22171080ab95SNikolay Aleksandrov 			break;
22181080ab95SNikolay Aleksandrov 		case IGMP_HOST_MEMBERSHIP_QUERY:
2219a65056ecSNikolay Aleksandrov 			if (t_len != sizeof(struct igmphdr)) {
2220a65056ecSNikolay Aleksandrov 				pstats->mstats.igmp_v3queries[dir]++;
2221a65056ecSNikolay Aleksandrov 			} else {
2222a65056ecSNikolay Aleksandrov 				unsigned int offset = skb_transport_offset(skb);
2223a65056ecSNikolay Aleksandrov 				struct igmphdr *ih, _ihdr;
2224a65056ecSNikolay Aleksandrov 
2225a65056ecSNikolay Aleksandrov 				ih = skb_header_pointer(skb, offset,
2226a65056ecSNikolay Aleksandrov 							sizeof(_ihdr), &_ihdr);
2227a65056ecSNikolay Aleksandrov 				if (!ih)
2228a65056ecSNikolay Aleksandrov 					break;
2229a65056ecSNikolay Aleksandrov 				if (!ih->code)
2230a65056ecSNikolay Aleksandrov 					pstats->mstats.igmp_v1queries[dir]++;
2231a65056ecSNikolay Aleksandrov 				else
2232a65056ecSNikolay Aleksandrov 					pstats->mstats.igmp_v2queries[dir]++;
2233a65056ecSNikolay Aleksandrov 			}
22341080ab95SNikolay Aleksandrov 			break;
22351080ab95SNikolay Aleksandrov 		case IGMP_HOST_LEAVE_MESSAGE:
22361080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_leaves[dir]++;
22371080ab95SNikolay Aleksandrov 			break;
22381080ab95SNikolay Aleksandrov 		}
22391080ab95SNikolay Aleksandrov 		break;
22401080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
22411080ab95SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
2242a65056ecSNikolay Aleksandrov 		t_len = ntohs(ipv6_hdr(skb)->payload_len) +
2243a65056ecSNikolay Aleksandrov 			sizeof(struct ipv6hdr);
2244a65056ecSNikolay Aleksandrov 		t_len -= skb_network_header_len(skb);
22451080ab95SNikolay Aleksandrov 		switch (type) {
22461080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_REPORT:
22471080ab95SNikolay Aleksandrov 			pstats->mstats.mld_v1reports[dir]++;
22481080ab95SNikolay Aleksandrov 			break;
22491080ab95SNikolay Aleksandrov 		case ICMPV6_MLD2_REPORT:
22501080ab95SNikolay Aleksandrov 			pstats->mstats.mld_v2reports[dir]++;
22511080ab95SNikolay Aleksandrov 			break;
22521080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_QUERY:
2253a65056ecSNikolay Aleksandrov 			if (t_len != sizeof(struct mld_msg))
2254a65056ecSNikolay Aleksandrov 				pstats->mstats.mld_v2queries[dir]++;
2255a65056ecSNikolay Aleksandrov 			else
2256a65056ecSNikolay Aleksandrov 				pstats->mstats.mld_v1queries[dir]++;
22571080ab95SNikolay Aleksandrov 			break;
22581080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_REDUCTION:
22591080ab95SNikolay Aleksandrov 			pstats->mstats.mld_leaves[dir]++;
22601080ab95SNikolay Aleksandrov 			break;
22611080ab95SNikolay Aleksandrov 		}
22621080ab95SNikolay Aleksandrov 		break;
22631080ab95SNikolay Aleksandrov #endif /* CONFIG_IPV6 */
22641080ab95SNikolay Aleksandrov 	}
22651080ab95SNikolay Aleksandrov 	u64_stats_update_end(&pstats->syncp);
22661080ab95SNikolay Aleksandrov }
22671080ab95SNikolay Aleksandrov 
22681080ab95SNikolay Aleksandrov void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
2269a65056ecSNikolay Aleksandrov 			const struct sk_buff *skb, u8 type, u8 dir)
22701080ab95SNikolay Aleksandrov {
22711080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
22721080ab95SNikolay Aleksandrov 
22731080ab95SNikolay Aleksandrov 	/* if multicast_disabled is true then igmp type can't be set */
2274675779adSNikolay Aleksandrov 	if (!type || !br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
22751080ab95SNikolay Aleksandrov 		return;
22761080ab95SNikolay Aleksandrov 
22771080ab95SNikolay Aleksandrov 	if (p)
22781080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
22791080ab95SNikolay Aleksandrov 	else
22801080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
22811080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
22821080ab95SNikolay Aleksandrov 		return;
22831080ab95SNikolay Aleksandrov 
2284a65056ecSNikolay Aleksandrov 	br_mcast_stats_add(stats, skb, type, dir);
22851080ab95SNikolay Aleksandrov }
22861080ab95SNikolay Aleksandrov 
22871080ab95SNikolay Aleksandrov int br_multicast_init_stats(struct net_bridge *br)
22881080ab95SNikolay Aleksandrov {
22891080ab95SNikolay Aleksandrov 	br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
22901080ab95SNikolay Aleksandrov 	if (!br->mcast_stats)
22911080ab95SNikolay Aleksandrov 		return -ENOMEM;
22921080ab95SNikolay Aleksandrov 
22931080ab95SNikolay Aleksandrov 	return 0;
22941080ab95SNikolay Aleksandrov }
22951080ab95SNikolay Aleksandrov 
2296b6fe0440SIdo Schimmel void br_multicast_uninit_stats(struct net_bridge *br)
2297b6fe0440SIdo Schimmel {
2298b6fe0440SIdo Schimmel 	free_percpu(br->mcast_stats);
2299b6fe0440SIdo Schimmel }
2300b6fe0440SIdo Schimmel 
23011080ab95SNikolay Aleksandrov static void mcast_stats_add_dir(u64 *dst, u64 *src)
23021080ab95SNikolay Aleksandrov {
23031080ab95SNikolay Aleksandrov 	dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
23041080ab95SNikolay Aleksandrov 	dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
23051080ab95SNikolay Aleksandrov }
23061080ab95SNikolay Aleksandrov 
23071080ab95SNikolay Aleksandrov void br_multicast_get_stats(const struct net_bridge *br,
23081080ab95SNikolay Aleksandrov 			    const struct net_bridge_port *p,
23091080ab95SNikolay Aleksandrov 			    struct br_mcast_stats *dest)
23101080ab95SNikolay Aleksandrov {
23111080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
23121080ab95SNikolay Aleksandrov 	struct br_mcast_stats tdst;
23131080ab95SNikolay Aleksandrov 	int i;
23141080ab95SNikolay Aleksandrov 
23151080ab95SNikolay Aleksandrov 	memset(dest, 0, sizeof(*dest));
23161080ab95SNikolay Aleksandrov 	if (p)
23171080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
23181080ab95SNikolay Aleksandrov 	else
23191080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
23201080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
23211080ab95SNikolay Aleksandrov 		return;
23221080ab95SNikolay Aleksandrov 
23231080ab95SNikolay Aleksandrov 	memset(&tdst, 0, sizeof(tdst));
23241080ab95SNikolay Aleksandrov 	for_each_possible_cpu(i) {
23251080ab95SNikolay Aleksandrov 		struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
23261080ab95SNikolay Aleksandrov 		struct br_mcast_stats temp;
23271080ab95SNikolay Aleksandrov 		unsigned int start;
23281080ab95SNikolay Aleksandrov 
23291080ab95SNikolay Aleksandrov 		do {
23301080ab95SNikolay Aleksandrov 			start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
23311080ab95SNikolay Aleksandrov 			memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
23321080ab95SNikolay Aleksandrov 		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
23331080ab95SNikolay Aleksandrov 
2334a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
2335a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
2336a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
23371080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
23381080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
23391080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
23401080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
23411080ab95SNikolay Aleksandrov 		tdst.igmp_parse_errors += temp.igmp_parse_errors;
23421080ab95SNikolay Aleksandrov 
2343a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
2344a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
23451080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
23461080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
23471080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
23481080ab95SNikolay Aleksandrov 		tdst.mld_parse_errors += temp.mld_parse_errors;
23491080ab95SNikolay Aleksandrov 	}
23501080ab95SNikolay Aleksandrov 	memcpy(dest, &tdst, sizeof(*dest));
23511080ab95SNikolay Aleksandrov }
235219e3a9c9SNikolay Aleksandrov 
235319e3a9c9SNikolay Aleksandrov int br_mdb_hash_init(struct net_bridge *br)
235419e3a9c9SNikolay Aleksandrov {
235519e3a9c9SNikolay Aleksandrov 	return rhashtable_init(&br->mdb_hash_tbl, &br_mdb_rht_params);
235619e3a9c9SNikolay Aleksandrov }
235719e3a9c9SNikolay Aleksandrov 
235819e3a9c9SNikolay Aleksandrov void br_mdb_hash_fini(struct net_bridge *br)
235919e3a9c9SNikolay Aleksandrov {
236019e3a9c9SNikolay Aleksandrov 	rhashtable_destroy(&br->mdb_hash_tbl);
236119e3a9c9SNikolay Aleksandrov }
2362