xref: /openbmc/linux/net/bridge/br_multicast.c (revision 0912bda436388a02c72164b4b490b578e64c012e)
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 
40cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br,
4190010b36SLinus Lüssing 				       struct bridge_mcast_own_query *query);
42754bc547SSatish Ashok static void br_multicast_add_router(struct net_bridge *br,
43754bc547SSatish Ashok 				    struct net_bridge_port *port);
44bc8c20acSSatish Ashok static void br_ip4_multicast_leave_group(struct net_bridge *br,
45bc8c20acSSatish Ashok 					 struct net_bridge_port *port,
46bc8c20acSSatish Ashok 					 __be32 group,
476db6f0eaSFelix Fietkau 					 __u16 vid,
486db6f0eaSFelix Fietkau 					 const unsigned char *src);
496db6f0eaSFelix Fietkau 
50f12e7d95SNogah Frankel static void __del_port_router(struct net_bridge_port *p);
51bc8c20acSSatish Ashok #if IS_ENABLED(CONFIG_IPV6)
52bc8c20acSSatish Ashok static void br_ip6_multicast_leave_group(struct net_bridge *br,
53bc8c20acSSatish Ashok 					 struct net_bridge_port *port,
54bc8c20acSSatish Ashok 					 const struct in6_addr *group,
556db6f0eaSFelix Fietkau 					 __u16 vid, const unsigned char *src);
56bc8c20acSSatish Ashok #endif
572ce297fcSCong Wang unsigned int br_mdb_rehash_seq;
58c83b8fabSHerbert Xu 
598ef2a9a5SYOSHIFUJI Hideaki static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
608ef2a9a5SYOSHIFUJI Hideaki {
618ef2a9a5SYOSHIFUJI Hideaki 	if (a->proto != b->proto)
628ef2a9a5SYOSHIFUJI Hideaki 		return 0;
63b0e9a30dSVlad Yasevich 	if (a->vid != b->vid)
64b0e9a30dSVlad Yasevich 		return 0;
658ef2a9a5SYOSHIFUJI Hideaki 	switch (a->proto) {
668ef2a9a5SYOSHIFUJI Hideaki 	case htons(ETH_P_IP):
678ef2a9a5SYOSHIFUJI Hideaki 		return a->u.ip4 == b->u.ip4;
68dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
6908b202b6SYOSHIFUJI Hideaki 	case htons(ETH_P_IPV6):
7008b202b6SYOSHIFUJI Hideaki 		return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
7108b202b6SYOSHIFUJI Hideaki #endif
728ef2a9a5SYOSHIFUJI Hideaki 	}
738ef2a9a5SYOSHIFUJI Hideaki 	return 0;
748ef2a9a5SYOSHIFUJI Hideaki }
758ef2a9a5SYOSHIFUJI Hideaki 
76b0e9a30dSVlad Yasevich static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip,
77b0e9a30dSVlad Yasevich 				__u16 vid)
78eb1d1641SHerbert Xu {
79b0e9a30dSVlad Yasevich 	return jhash_2words((__force u32)ip, vid, mdb->secret) & (mdb->max - 1);
80eb1d1641SHerbert Xu }
81eb1d1641SHerbert Xu 
82dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
8308b202b6SYOSHIFUJI Hideaki static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb,
84b0e9a30dSVlad Yasevich 				const struct in6_addr *ip,
85b0e9a30dSVlad Yasevich 				__u16 vid)
8608b202b6SYOSHIFUJI Hideaki {
87b0e9a30dSVlad Yasevich 	return jhash_2words(ipv6_addr_hash(ip), vid,
88b0e9a30dSVlad Yasevich 			    mdb->secret) & (mdb->max - 1);
8908b202b6SYOSHIFUJI Hideaki }
9008b202b6SYOSHIFUJI Hideaki #endif
9108b202b6SYOSHIFUJI Hideaki 
928ef2a9a5SYOSHIFUJI Hideaki static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb,
938ef2a9a5SYOSHIFUJI Hideaki 			     struct br_ip *ip)
948ef2a9a5SYOSHIFUJI Hideaki {
958ef2a9a5SYOSHIFUJI Hideaki 	switch (ip->proto) {
968ef2a9a5SYOSHIFUJI Hideaki 	case htons(ETH_P_IP):
97b0e9a30dSVlad Yasevich 		return __br_ip4_hash(mdb, ip->u.ip4, ip->vid);
98dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
9908b202b6SYOSHIFUJI Hideaki 	case htons(ETH_P_IPV6):
100b0e9a30dSVlad Yasevich 		return __br_ip6_hash(mdb, &ip->u.ip6, ip->vid);
10108b202b6SYOSHIFUJI Hideaki #endif
1028ef2a9a5SYOSHIFUJI Hideaki 	}
1038ef2a9a5SYOSHIFUJI Hideaki 	return 0;
104eb1d1641SHerbert Xu }
105eb1d1641SHerbert Xu 
106eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *__br_mdb_ip_get(
1078ef2a9a5SYOSHIFUJI Hideaki 	struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash)
108eb1d1641SHerbert Xu {
109eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
110eb1d1641SHerbert Xu 
111b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
1128ef2a9a5SYOSHIFUJI Hideaki 		if (br_ip_equal(&mp->addr, dst))
113eb1d1641SHerbert Xu 			return mp;
114eb1d1641SHerbert Xu 	}
115eb1d1641SHerbert Xu 
116eb1d1641SHerbert Xu 	return NULL;
117eb1d1641SHerbert Xu }
118eb1d1641SHerbert Xu 
119cfd56754SCong Wang struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge_mdb_htable *mdb,
120cfd56754SCong Wang 					   struct br_ip *dst)
1217f285fa7SHerbert Xu {
1227f285fa7SHerbert Xu 	if (!mdb)
1237f285fa7SHerbert Xu 		return NULL;
1247f285fa7SHerbert Xu 
1257f285fa7SHerbert Xu 	return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
1267f285fa7SHerbert Xu }
1277f285fa7SHerbert Xu 
1288ef2a9a5SYOSHIFUJI Hideaki static struct net_bridge_mdb_entry *br_mdb_ip4_get(
129b0e9a30dSVlad Yasevich 	struct net_bridge_mdb_htable *mdb, __be32 dst, __u16 vid)
130eb1d1641SHerbert Xu {
1318ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip br_dst;
1320821ec55SHerbert Xu 
1338ef2a9a5SYOSHIFUJI Hideaki 	br_dst.u.ip4 = dst;
1348ef2a9a5SYOSHIFUJI Hideaki 	br_dst.proto = htons(ETH_P_IP);
135b0e9a30dSVlad Yasevich 	br_dst.vid = vid;
1368ef2a9a5SYOSHIFUJI Hideaki 
1377f285fa7SHerbert Xu 	return br_mdb_ip_get(mdb, &br_dst);
1388ef2a9a5SYOSHIFUJI Hideaki }
1398ef2a9a5SYOSHIFUJI Hideaki 
140dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
14108b202b6SYOSHIFUJI Hideaki static struct net_bridge_mdb_entry *br_mdb_ip6_get(
142b0e9a30dSVlad Yasevich 	struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst,
143b0e9a30dSVlad Yasevich 	__u16 vid)
14408b202b6SYOSHIFUJI Hideaki {
14508b202b6SYOSHIFUJI Hideaki 	struct br_ip br_dst;
14608b202b6SYOSHIFUJI Hideaki 
1474e3fd7a0SAlexey Dobriyan 	br_dst.u.ip6 = *dst;
14808b202b6SYOSHIFUJI Hideaki 	br_dst.proto = htons(ETH_P_IPV6);
149b0e9a30dSVlad Yasevich 	br_dst.vid = vid;
15008b202b6SYOSHIFUJI Hideaki 
1517f285fa7SHerbert Xu 	return br_mdb_ip_get(mdb, &br_dst);
15208b202b6SYOSHIFUJI Hideaki }
15308b202b6SYOSHIFUJI Hideaki #endif
15408b202b6SYOSHIFUJI Hideaki 
155eb1d1641SHerbert Xu struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
156fbca58a2SCong Wang 					struct sk_buff *skb, u16 vid)
157eb1d1641SHerbert Xu {
158e8051688SEric Dumazet 	struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb);
1598ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip ip;
1608ef2a9a5SYOSHIFUJI Hideaki 
1617f285fa7SHerbert Xu 	if (br->multicast_disabled)
162eb1d1641SHerbert Xu 		return NULL;
163eb1d1641SHerbert Xu 
1648ef2a9a5SYOSHIFUJI Hideaki 	if (BR_INPUT_SKB_CB(skb)->igmp)
1658ef2a9a5SYOSHIFUJI Hideaki 		return NULL;
1668ef2a9a5SYOSHIFUJI Hideaki 
1678ef2a9a5SYOSHIFUJI Hideaki 	ip.proto = skb->protocol;
168fbca58a2SCong Wang 	ip.vid = vid;
1698ef2a9a5SYOSHIFUJI Hideaki 
170eb1d1641SHerbert Xu 	switch (skb->protocol) {
171eb1d1641SHerbert Xu 	case htons(ETH_P_IP):
1728ef2a9a5SYOSHIFUJI Hideaki 		ip.u.ip4 = ip_hdr(skb)->daddr;
173eb1d1641SHerbert Xu 		break;
174dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
17508b202b6SYOSHIFUJI Hideaki 	case htons(ETH_P_IPV6):
1764e3fd7a0SAlexey Dobriyan 		ip.u.ip6 = ipv6_hdr(skb)->daddr;
17708b202b6SYOSHIFUJI Hideaki 		break;
17808b202b6SYOSHIFUJI Hideaki #endif
1798ef2a9a5SYOSHIFUJI Hideaki 	default:
1808ef2a9a5SYOSHIFUJI Hideaki 		return NULL;
181eb1d1641SHerbert Xu 	}
182eb1d1641SHerbert Xu 
1838ef2a9a5SYOSHIFUJI Hideaki 	return br_mdb_ip_get(mdb, &ip);
184eb1d1641SHerbert Xu }
185eb1d1641SHerbert Xu 
186eb1d1641SHerbert Xu static void br_mdb_free(struct rcu_head *head)
187eb1d1641SHerbert Xu {
188eb1d1641SHerbert Xu 	struct net_bridge_mdb_htable *mdb =
189eb1d1641SHerbert Xu 		container_of(head, struct net_bridge_mdb_htable, rcu);
190eb1d1641SHerbert Xu 	struct net_bridge_mdb_htable *old = mdb->old;
191eb1d1641SHerbert Xu 
192eb1d1641SHerbert Xu 	mdb->old = NULL;
193eb1d1641SHerbert Xu 	kfree(old->mhash);
194eb1d1641SHerbert Xu 	kfree(old);
195eb1d1641SHerbert Xu }
196eb1d1641SHerbert Xu 
197eb1d1641SHerbert Xu static int br_mdb_copy(struct net_bridge_mdb_htable *new,
198eb1d1641SHerbert Xu 		       struct net_bridge_mdb_htable *old,
199eb1d1641SHerbert Xu 		       int elasticity)
200eb1d1641SHerbert Xu {
201eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
202eb1d1641SHerbert Xu 	int maxlen;
203eb1d1641SHerbert Xu 	int len;
204eb1d1641SHerbert Xu 	int i;
205eb1d1641SHerbert Xu 
206eb1d1641SHerbert Xu 	for (i = 0; i < old->max; i++)
207b67bfe0dSSasha Levin 		hlist_for_each_entry(mp, &old->mhash[i], hlist[old->ver])
208eb1d1641SHerbert Xu 			hlist_add_head(&mp->hlist[new->ver],
2098ef2a9a5SYOSHIFUJI Hideaki 				       &new->mhash[br_ip_hash(new, &mp->addr)]);
210eb1d1641SHerbert Xu 
211eb1d1641SHerbert Xu 	if (!elasticity)
212eb1d1641SHerbert Xu 		return 0;
213eb1d1641SHerbert Xu 
214eb1d1641SHerbert Xu 	maxlen = 0;
215eb1d1641SHerbert Xu 	for (i = 0; i < new->max; i++) {
216eb1d1641SHerbert Xu 		len = 0;
217b67bfe0dSSasha Levin 		hlist_for_each_entry(mp, &new->mhash[i], hlist[new->ver])
218eb1d1641SHerbert Xu 			len++;
219eb1d1641SHerbert Xu 		if (len > maxlen)
220eb1d1641SHerbert Xu 			maxlen = len;
221eb1d1641SHerbert Xu 	}
222eb1d1641SHerbert Xu 
223eb1d1641SHerbert Xu 	return maxlen > elasticity ? -EINVAL : 0;
224eb1d1641SHerbert Xu }
225eb1d1641SHerbert Xu 
226cfd56754SCong Wang void br_multicast_free_pg(struct rcu_head *head)
227eb1d1641SHerbert Xu {
228eb1d1641SHerbert Xu 	struct net_bridge_port_group *p =
229eb1d1641SHerbert Xu 		container_of(head, struct net_bridge_port_group, rcu);
230eb1d1641SHerbert Xu 
231eb1d1641SHerbert Xu 	kfree(p);
232eb1d1641SHerbert Xu }
233eb1d1641SHerbert Xu 
234eb1d1641SHerbert Xu static void br_multicast_free_group(struct rcu_head *head)
235eb1d1641SHerbert Xu {
236eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp =
237eb1d1641SHerbert Xu 		container_of(head, struct net_bridge_mdb_entry, rcu);
238eb1d1641SHerbert Xu 
239eb1d1641SHerbert Xu 	kfree(mp);
240eb1d1641SHerbert Xu }
241eb1d1641SHerbert Xu 
242eb1d1641SHerbert Xu static void br_multicast_group_expired(unsigned long data)
243eb1d1641SHerbert Xu {
244eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp = (void *)data;
245eb1d1641SHerbert Xu 	struct net_bridge *br = mp->br;
246eb1d1641SHerbert Xu 	struct net_bridge_mdb_htable *mdb;
247eb1d1641SHerbert Xu 
248eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
249eb1d1641SHerbert Xu 	if (!netif_running(br->dev) || timer_pending(&mp->timer))
250eb1d1641SHerbert Xu 		goto out;
251eb1d1641SHerbert Xu 
2528a870178SHerbert Xu 	mp->mglist = false;
253eb1d1641SHerbert Xu 
254eb1d1641SHerbert Xu 	if (mp->ports)
255eb1d1641SHerbert Xu 		goto out;
256eb1d1641SHerbert Xu 
257e8051688SEric Dumazet 	mdb = mlock_dereference(br->mdb, br);
258e8051688SEric Dumazet 
259eb1d1641SHerbert Xu 	hlist_del_rcu(&mp->hlist[mdb->ver]);
260eb1d1641SHerbert Xu 	mdb->size--;
261eb1d1641SHerbert Xu 
262eb1d1641SHerbert Xu 	call_rcu_bh(&mp->rcu, br_multicast_free_group);
263eb1d1641SHerbert Xu 
264eb1d1641SHerbert Xu out:
265eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
266eb1d1641SHerbert Xu }
267eb1d1641SHerbert Xu 
268eb1d1641SHerbert Xu static void br_multicast_del_pg(struct net_bridge *br,
269eb1d1641SHerbert Xu 				struct net_bridge_port_group *pg)
270eb1d1641SHerbert Xu {
271e8051688SEric Dumazet 	struct net_bridge_mdb_htable *mdb;
272eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
273eb1d1641SHerbert Xu 	struct net_bridge_port_group *p;
274e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
275e8051688SEric Dumazet 
276e8051688SEric Dumazet 	mdb = mlock_dereference(br->mdb, br);
277eb1d1641SHerbert Xu 
2788ef2a9a5SYOSHIFUJI Hideaki 	mp = br_mdb_ip_get(mdb, &pg->addr);
279eb1d1641SHerbert Xu 	if (WARN_ON(!mp))
280eb1d1641SHerbert Xu 		return;
281eb1d1641SHerbert Xu 
282e8051688SEric Dumazet 	for (pp = &mp->ports;
283e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
284e8051688SEric Dumazet 	     pp = &p->next) {
285eb1d1641SHerbert Xu 		if (p != pg)
286eb1d1641SHerbert Xu 			continue;
287eb1d1641SHerbert Xu 
28883f6a740Sstephen hemminger 		rcu_assign_pointer(*pp, p->next);
289eb1d1641SHerbert Xu 		hlist_del_init(&p->mglist);
290eb1d1641SHerbert Xu 		del_timer(&p->timer);
29145ebcce5SElad Raz 		br_mdb_notify(br->dev, p->port, &pg->addr, RTM_DELMDB,
29245ebcce5SElad Raz 			      p->flags);
293eb1d1641SHerbert Xu 		call_rcu_bh(&p->rcu, br_multicast_free_pg);
294eb1d1641SHerbert Xu 
295454594f3SLinus Lüssing 		if (!mp->ports && !mp->mglist &&
296eb1d1641SHerbert Xu 		    netif_running(br->dev))
297eb1d1641SHerbert Xu 			mod_timer(&mp->timer, jiffies);
298eb1d1641SHerbert Xu 
299eb1d1641SHerbert Xu 		return;
300eb1d1641SHerbert Xu 	}
301eb1d1641SHerbert Xu 
302eb1d1641SHerbert Xu 	WARN_ON(1);
303eb1d1641SHerbert Xu }
304eb1d1641SHerbert Xu 
305eb1d1641SHerbert Xu static void br_multicast_port_group_expired(unsigned long data)
306eb1d1641SHerbert Xu {
307eb1d1641SHerbert Xu 	struct net_bridge_port_group *pg = (void *)data;
308eb1d1641SHerbert Xu 	struct net_bridge *br = pg->port->br;
309eb1d1641SHerbert Xu 
310eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
311eb1d1641SHerbert Xu 	if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
3129d06b6d8SElad Raz 	    hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT)
313eb1d1641SHerbert Xu 		goto out;
314eb1d1641SHerbert Xu 
315eb1d1641SHerbert Xu 	br_multicast_del_pg(br, pg);
316eb1d1641SHerbert Xu 
317eb1d1641SHerbert Xu out:
318eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
319eb1d1641SHerbert Xu }
320eb1d1641SHerbert Xu 
321e8051688SEric Dumazet static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max,
322eb1d1641SHerbert Xu 			 int elasticity)
323eb1d1641SHerbert Xu {
324e8051688SEric Dumazet 	struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1);
325eb1d1641SHerbert Xu 	struct net_bridge_mdb_htable *mdb;
326eb1d1641SHerbert Xu 	int err;
327eb1d1641SHerbert Xu 
328eb1d1641SHerbert Xu 	mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
329eb1d1641SHerbert Xu 	if (!mdb)
330eb1d1641SHerbert Xu 		return -ENOMEM;
331eb1d1641SHerbert Xu 
332eb1d1641SHerbert Xu 	mdb->max = max;
333eb1d1641SHerbert Xu 	mdb->old = old;
334eb1d1641SHerbert Xu 
335eb1d1641SHerbert Xu 	mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
336eb1d1641SHerbert Xu 	if (!mdb->mhash) {
337eb1d1641SHerbert Xu 		kfree(mdb);
338eb1d1641SHerbert Xu 		return -ENOMEM;
339eb1d1641SHerbert Xu 	}
340eb1d1641SHerbert Xu 
341eb1d1641SHerbert Xu 	mdb->size = old ? old->size : 0;
342eb1d1641SHerbert Xu 	mdb->ver = old ? old->ver ^ 1 : 0;
343eb1d1641SHerbert Xu 
344eb1d1641SHerbert Xu 	if (!old || elasticity)
345eb1d1641SHerbert Xu 		get_random_bytes(&mdb->secret, sizeof(mdb->secret));
346eb1d1641SHerbert Xu 	else
347eb1d1641SHerbert Xu 		mdb->secret = old->secret;
348eb1d1641SHerbert Xu 
349eb1d1641SHerbert Xu 	if (!old)
350eb1d1641SHerbert Xu 		goto out;
351eb1d1641SHerbert Xu 
352eb1d1641SHerbert Xu 	err = br_mdb_copy(mdb, old, elasticity);
353eb1d1641SHerbert Xu 	if (err) {
354eb1d1641SHerbert Xu 		kfree(mdb->mhash);
355eb1d1641SHerbert Xu 		kfree(mdb);
356eb1d1641SHerbert Xu 		return err;
357eb1d1641SHerbert Xu 	}
358eb1d1641SHerbert Xu 
3592ce297fcSCong Wang 	br_mdb_rehash_seq++;
360eb1d1641SHerbert Xu 	call_rcu_bh(&mdb->rcu, br_mdb_free);
361eb1d1641SHerbert Xu 
362eb1d1641SHerbert Xu out:
363eb1d1641SHerbert Xu 	rcu_assign_pointer(*mdbp, mdb);
364eb1d1641SHerbert Xu 
365eb1d1641SHerbert Xu 	return 0;
366eb1d1641SHerbert Xu }
367eb1d1641SHerbert Xu 
3688ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
3691080ab95SNikolay Aleksandrov 						    __be32 group,
3701080ab95SNikolay Aleksandrov 						    u8 *igmp_type)
371eb1d1641SHerbert Xu {
3725e923585SNikolay Aleksandrov 	struct igmpv3_query *ihv3;
3735e923585SNikolay Aleksandrov 	size_t igmp_hdr_size;
374eb1d1641SHerbert Xu 	struct sk_buff *skb;
375eb1d1641SHerbert Xu 	struct igmphdr *ih;
376eb1d1641SHerbert Xu 	struct ethhdr *eth;
377eb1d1641SHerbert Xu 	struct iphdr *iph;
378eb1d1641SHerbert Xu 
3795e923585SNikolay Aleksandrov 	igmp_hdr_size = sizeof(*ih);
3805e923585SNikolay Aleksandrov 	if (br->multicast_igmp_version == 3)
3815e923585SNikolay Aleksandrov 		igmp_hdr_size = sizeof(*ihv3);
382eb1d1641SHerbert Xu 	skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
3835e923585SNikolay Aleksandrov 						 igmp_hdr_size + 4);
384eb1d1641SHerbert Xu 	if (!skb)
385eb1d1641SHerbert Xu 		goto out;
386eb1d1641SHerbert Xu 
387eb1d1641SHerbert Xu 	skb->protocol = htons(ETH_P_IP);
388eb1d1641SHerbert Xu 
389eb1d1641SHerbert Xu 	skb_reset_mac_header(skb);
390eb1d1641SHerbert Xu 	eth = eth_hdr(skb);
391eb1d1641SHerbert Xu 
392e5a727f6SJoe Perches 	ether_addr_copy(eth->h_source, br->dev->dev_addr);
393eb1d1641SHerbert Xu 	eth->h_dest[0] = 1;
394eb1d1641SHerbert Xu 	eth->h_dest[1] = 0;
395eb1d1641SHerbert Xu 	eth->h_dest[2] = 0x5e;
396eb1d1641SHerbert Xu 	eth->h_dest[3] = 0;
397eb1d1641SHerbert Xu 	eth->h_dest[4] = 0;
398eb1d1641SHerbert Xu 	eth->h_dest[5] = 1;
399eb1d1641SHerbert Xu 	eth->h_proto = htons(ETH_P_IP);
400eb1d1641SHerbert Xu 	skb_put(skb, sizeof(*eth));
401eb1d1641SHerbert Xu 
402eb1d1641SHerbert Xu 	skb_set_network_header(skb, skb->len);
403eb1d1641SHerbert Xu 	iph = ip_hdr(skb);
404eb1d1641SHerbert Xu 
405eb1d1641SHerbert Xu 	iph->version = 4;
406eb1d1641SHerbert Xu 	iph->ihl = 6;
407eb1d1641SHerbert Xu 	iph->tos = 0xc0;
4085e923585SNikolay Aleksandrov 	iph->tot_len = htons(sizeof(*iph) + igmp_hdr_size + 4);
409eb1d1641SHerbert Xu 	iph->id = 0;
410eb1d1641SHerbert Xu 	iph->frag_off = htons(IP_DF);
411eb1d1641SHerbert Xu 	iph->ttl = 1;
412eb1d1641SHerbert Xu 	iph->protocol = IPPROTO_IGMP;
4131c8ad5bfSCong Wang 	iph->saddr = br->multicast_query_use_ifaddr ?
4141c8ad5bfSCong Wang 		     inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0;
415eb1d1641SHerbert Xu 	iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
416eb1d1641SHerbert Xu 	((u8 *)&iph[1])[0] = IPOPT_RA;
417eb1d1641SHerbert Xu 	((u8 *)&iph[1])[1] = 4;
418eb1d1641SHerbert Xu 	((u8 *)&iph[1])[2] = 0;
419eb1d1641SHerbert Xu 	((u8 *)&iph[1])[3] = 0;
420eb1d1641SHerbert Xu 	ip_send_check(iph);
421eb1d1641SHerbert Xu 	skb_put(skb, 24);
422eb1d1641SHerbert Xu 
423eb1d1641SHerbert Xu 	skb_set_transport_header(skb, skb->len);
4241080ab95SNikolay Aleksandrov 	*igmp_type = IGMP_HOST_MEMBERSHIP_QUERY;
4255e923585SNikolay Aleksandrov 
4265e923585SNikolay Aleksandrov 	switch (br->multicast_igmp_version) {
4275e923585SNikolay Aleksandrov 	case 2:
4285e923585SNikolay Aleksandrov 		ih = igmp_hdr(skb);
429eb1d1641SHerbert Xu 		ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
430eb1d1641SHerbert Xu 		ih->code = (group ? br->multicast_last_member_interval :
431eb1d1641SHerbert Xu 				    br->multicast_query_response_interval) /
432eb1d1641SHerbert Xu 			   (HZ / IGMP_TIMER_SCALE);
433eb1d1641SHerbert Xu 		ih->group = group;
434eb1d1641SHerbert Xu 		ih->csum = 0;
4355e923585SNikolay Aleksandrov 		ih->csum = ip_compute_csum((void *)ih, sizeof(*ih));
4365e923585SNikolay Aleksandrov 		break;
4375e923585SNikolay Aleksandrov 	case 3:
4385e923585SNikolay Aleksandrov 		ihv3 = igmpv3_query_hdr(skb);
4395e923585SNikolay Aleksandrov 		ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY;
4405e923585SNikolay Aleksandrov 		ihv3->code = (group ? br->multicast_last_member_interval :
4415e923585SNikolay Aleksandrov 				      br->multicast_query_response_interval) /
4425e923585SNikolay Aleksandrov 			     (HZ / IGMP_TIMER_SCALE);
4435e923585SNikolay Aleksandrov 		ihv3->group = group;
4445e923585SNikolay Aleksandrov 		ihv3->qqic = br->multicast_query_interval / HZ;
4455e923585SNikolay Aleksandrov 		ihv3->nsrcs = 0;
4465e923585SNikolay Aleksandrov 		ihv3->resv = 0;
4475e923585SNikolay Aleksandrov 		ihv3->suppress = 0;
4485e923585SNikolay Aleksandrov 		ihv3->qrv = 2;
4495e923585SNikolay Aleksandrov 		ihv3->csum = 0;
4505e923585SNikolay Aleksandrov 		ihv3->csum = ip_compute_csum((void *)ihv3, sizeof(*ihv3));
4515e923585SNikolay Aleksandrov 		break;
4525e923585SNikolay Aleksandrov 	}
453eb1d1641SHerbert Xu 
4545e923585SNikolay Aleksandrov 	skb_put(skb, igmp_hdr_size);
455eb1d1641SHerbert Xu 	__skb_pull(skb, sizeof(*eth));
456eb1d1641SHerbert Xu 
457eb1d1641SHerbert Xu out:
458eb1d1641SHerbert Xu 	return skb;
459eb1d1641SHerbert Xu }
460eb1d1641SHerbert Xu 
461dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
46208b202b6SYOSHIFUJI Hideaki static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
4631080ab95SNikolay Aleksandrov 						    const struct in6_addr *grp,
4641080ab95SNikolay Aleksandrov 						    u8 *igmp_type)
46508b202b6SYOSHIFUJI Hideaki {
466aa2ae3e7SNikolay Aleksandrov 	struct mld2_query *mld2q;
467aa2ae3e7SNikolay Aleksandrov 	unsigned long interval;
46808b202b6SYOSHIFUJI Hideaki 	struct ipv6hdr *ip6h;
46908b202b6SYOSHIFUJI Hideaki 	struct mld_msg *mldq;
470aa2ae3e7SNikolay Aleksandrov 	size_t mld_hdr_size;
471aa2ae3e7SNikolay Aleksandrov 	struct sk_buff *skb;
47208b202b6SYOSHIFUJI Hideaki 	struct ethhdr *eth;
47308b202b6SYOSHIFUJI Hideaki 	u8 *hopopt;
47408b202b6SYOSHIFUJI Hideaki 
475aa2ae3e7SNikolay Aleksandrov 	mld_hdr_size = sizeof(*mldq);
476aa2ae3e7SNikolay Aleksandrov 	if (br->multicast_mld_version == 2)
477aa2ae3e7SNikolay Aleksandrov 		mld_hdr_size = sizeof(*mld2q);
47808b202b6SYOSHIFUJI Hideaki 	skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
479aa2ae3e7SNikolay Aleksandrov 						 8 + mld_hdr_size);
48008b202b6SYOSHIFUJI Hideaki 	if (!skb)
48108b202b6SYOSHIFUJI Hideaki 		goto out;
48208b202b6SYOSHIFUJI Hideaki 
48308b202b6SYOSHIFUJI Hideaki 	skb->protocol = htons(ETH_P_IPV6);
48408b202b6SYOSHIFUJI Hideaki 
48508b202b6SYOSHIFUJI Hideaki 	/* Ethernet header */
48608b202b6SYOSHIFUJI Hideaki 	skb_reset_mac_header(skb);
48708b202b6SYOSHIFUJI Hideaki 	eth = eth_hdr(skb);
48808b202b6SYOSHIFUJI Hideaki 
489e5a727f6SJoe Perches 	ether_addr_copy(eth->h_source, br->dev->dev_addr);
49008b202b6SYOSHIFUJI Hideaki 	eth->h_proto = htons(ETH_P_IPV6);
49108b202b6SYOSHIFUJI Hideaki 	skb_put(skb, sizeof(*eth));
49208b202b6SYOSHIFUJI Hideaki 
49308b202b6SYOSHIFUJI Hideaki 	/* IPv6 header + HbH option */
49408b202b6SYOSHIFUJI Hideaki 	skb_set_network_header(skb, skb->len);
49508b202b6SYOSHIFUJI Hideaki 	ip6h = ipv6_hdr(skb);
49608b202b6SYOSHIFUJI Hideaki 
49708b202b6SYOSHIFUJI Hideaki 	*(__force __be32 *)ip6h = htonl(0x60000000);
498aa2ae3e7SNikolay Aleksandrov 	ip6h->payload_len = htons(8 + mld_hdr_size);
49908b202b6SYOSHIFUJI Hideaki 	ip6h->nexthdr = IPPROTO_HOPOPTS;
50008b202b6SYOSHIFUJI Hideaki 	ip6h->hop_limit = 1;
501a7bff75bSLinus Lüssing 	ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
502d1d81d4cSUlrich Weber 	if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
503d1d81d4cSUlrich Weber 			       &ip6h->saddr)) {
504d1d81d4cSUlrich Weber 		kfree_skb(skb);
5050888d5f3Sdaniel 		br->has_ipv6_addr = 0;
506d1d81d4cSUlrich Weber 		return NULL;
507d1d81d4cSUlrich Weber 	}
5080888d5f3Sdaniel 
5090888d5f3Sdaniel 	br->has_ipv6_addr = 1;
51036cff5a1SLinus Lüssing 	ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
51108b202b6SYOSHIFUJI Hideaki 
51208b202b6SYOSHIFUJI Hideaki 	hopopt = (u8 *)(ip6h + 1);
51308b202b6SYOSHIFUJI Hideaki 	hopopt[0] = IPPROTO_ICMPV6;		/* next hdr */
51408b202b6SYOSHIFUJI Hideaki 	hopopt[1] = 0;				/* length of HbH */
51508b202b6SYOSHIFUJI Hideaki 	hopopt[2] = IPV6_TLV_ROUTERALERT;	/* Router Alert */
51608b202b6SYOSHIFUJI Hideaki 	hopopt[3] = 2;				/* Length of RA Option */
51708b202b6SYOSHIFUJI Hideaki 	hopopt[4] = 0;				/* Type = 0x0000 (MLD) */
51808b202b6SYOSHIFUJI Hideaki 	hopopt[5] = 0;
5191de5a71cSEldad Zack 	hopopt[6] = IPV6_TLV_PAD1;		/* Pad1 */
5201de5a71cSEldad Zack 	hopopt[7] = IPV6_TLV_PAD1;		/* Pad1 */
52108b202b6SYOSHIFUJI Hideaki 
52208b202b6SYOSHIFUJI Hideaki 	skb_put(skb, sizeof(*ip6h) + 8);
52308b202b6SYOSHIFUJI Hideaki 
52408b202b6SYOSHIFUJI Hideaki 	/* ICMPv6 */
52508b202b6SYOSHIFUJI Hideaki 	skb_set_transport_header(skb, skb->len);
5261080ab95SNikolay Aleksandrov 	interval = ipv6_addr_any(grp) ?
52732de868cSLinus Lüssing 			br->multicast_query_response_interval :
52832de868cSLinus Lüssing 			br->multicast_last_member_interval;
5291080ab95SNikolay Aleksandrov 	*igmp_type = ICMPV6_MGM_QUERY;
530aa2ae3e7SNikolay Aleksandrov 	switch (br->multicast_mld_version) {
531aa2ae3e7SNikolay Aleksandrov 	case 1:
532aa2ae3e7SNikolay Aleksandrov 		mldq = (struct mld_msg *)icmp6_hdr(skb);
53308b202b6SYOSHIFUJI Hideaki 		mldq->mld_type = ICMPV6_MGM_QUERY;
53408b202b6SYOSHIFUJI Hideaki 		mldq->mld_code = 0;
53508b202b6SYOSHIFUJI Hideaki 		mldq->mld_cksum = 0;
53608b202b6SYOSHIFUJI Hideaki 		mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
53708b202b6SYOSHIFUJI Hideaki 		mldq->mld_reserved = 0;
5381080ab95SNikolay Aleksandrov 		mldq->mld_mca = *grp;
53908b202b6SYOSHIFUJI Hideaki 		mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
54008b202b6SYOSHIFUJI Hideaki 						  sizeof(*mldq), IPPROTO_ICMPV6,
54108b202b6SYOSHIFUJI Hideaki 						  csum_partial(mldq,
542aa2ae3e7SNikolay Aleksandrov 							       sizeof(*mldq),
543aa2ae3e7SNikolay Aleksandrov 							       0));
544aa2ae3e7SNikolay Aleksandrov 		break;
545aa2ae3e7SNikolay Aleksandrov 	case 2:
546aa2ae3e7SNikolay Aleksandrov 		mld2q = (struct mld2_query *)icmp6_hdr(skb);
54753631a5fSLance Richardson 		mld2q->mld2q_mrc = htons((u16)jiffies_to_msecs(interval));
548aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_type = ICMPV6_MGM_QUERY;
549aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_code = 0;
550aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_cksum = 0;
551aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_resv1 = 0;
552aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_resv2 = 0;
553aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_suppress = 0;
554aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_qrv = 2;
555aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_nsrcs = 0;
556aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_qqic = br->multicast_query_interval / HZ;
557aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_mca = *grp;
558aa2ae3e7SNikolay Aleksandrov 		mld2q->mld2q_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
559aa2ae3e7SNikolay Aleksandrov 						     sizeof(*mld2q),
560aa2ae3e7SNikolay Aleksandrov 						     IPPROTO_ICMPV6,
561aa2ae3e7SNikolay Aleksandrov 						     csum_partial(mld2q,
562aa2ae3e7SNikolay Aleksandrov 								  sizeof(*mld2q),
563aa2ae3e7SNikolay Aleksandrov 								  0));
564aa2ae3e7SNikolay Aleksandrov 		break;
565aa2ae3e7SNikolay Aleksandrov 	}
566aa2ae3e7SNikolay Aleksandrov 	skb_put(skb, mld_hdr_size);
56708b202b6SYOSHIFUJI Hideaki 
56808b202b6SYOSHIFUJI Hideaki 	__skb_pull(skb, sizeof(*eth));
56908b202b6SYOSHIFUJI Hideaki 
57008b202b6SYOSHIFUJI Hideaki out:
57108b202b6SYOSHIFUJI Hideaki 	return skb;
57208b202b6SYOSHIFUJI Hideaki }
57308b202b6SYOSHIFUJI Hideaki #endif
57408b202b6SYOSHIFUJI Hideaki 
5758ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
5761080ab95SNikolay Aleksandrov 						struct br_ip *addr,
5771080ab95SNikolay Aleksandrov 						u8 *igmp_type)
5788ef2a9a5SYOSHIFUJI Hideaki {
5798ef2a9a5SYOSHIFUJI Hideaki 	switch (addr->proto) {
5808ef2a9a5SYOSHIFUJI Hideaki 	case htons(ETH_P_IP):
5811080ab95SNikolay Aleksandrov 		return br_ip4_multicast_alloc_query(br, addr->u.ip4, igmp_type);
582dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
58308b202b6SYOSHIFUJI Hideaki 	case htons(ETH_P_IPV6):
5841080ab95SNikolay Aleksandrov 		return br_ip6_multicast_alloc_query(br, &addr->u.ip6,
5851080ab95SNikolay Aleksandrov 						    igmp_type);
58608b202b6SYOSHIFUJI Hideaki #endif
5878ef2a9a5SYOSHIFUJI Hideaki 	}
5888ef2a9a5SYOSHIFUJI Hideaki 	return NULL;
5898ef2a9a5SYOSHIFUJI Hideaki }
5908ef2a9a5SYOSHIFUJI Hideaki 
591eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *br_multicast_get_group(
5928ef2a9a5SYOSHIFUJI Hideaki 	struct net_bridge *br, struct net_bridge_port *port,
5938ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip *group, int hash)
594eb1d1641SHerbert Xu {
595e8051688SEric Dumazet 	struct net_bridge_mdb_htable *mdb;
596eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
59795c96174SEric Dumazet 	unsigned int count = 0;
59895c96174SEric Dumazet 	unsigned int max;
599eb1d1641SHerbert Xu 	int elasticity;
600eb1d1641SHerbert Xu 	int err;
601eb1d1641SHerbert Xu 
602e8051688SEric Dumazet 	mdb = rcu_dereference_protected(br->mdb, 1);
603b67bfe0dSSasha Levin 	hlist_for_each_entry(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
604eb1d1641SHerbert Xu 		count++;
6058ef2a9a5SYOSHIFUJI Hideaki 		if (unlikely(br_ip_equal(group, &mp->addr)))
606eb1d1641SHerbert Xu 			return mp;
607eb1d1641SHerbert Xu 	}
608eb1d1641SHerbert Xu 
609eb1d1641SHerbert Xu 	elasticity = 0;
610eb1d1641SHerbert Xu 	max = mdb->max;
611eb1d1641SHerbert Xu 
612eb1d1641SHerbert Xu 	if (unlikely(count > br->hash_elasticity && count)) {
613eb1d1641SHerbert Xu 		if (net_ratelimit())
61428a16c97Sstephen hemminger 			br_info(br, "Multicast hash table "
615eb1d1641SHerbert Xu 				"chain limit reached: %s\n",
61628a16c97Sstephen hemminger 				port ? port->dev->name : br->dev->name);
617eb1d1641SHerbert Xu 
618eb1d1641SHerbert Xu 		elasticity = br->hash_elasticity;
619eb1d1641SHerbert Xu 	}
620eb1d1641SHerbert Xu 
621eb1d1641SHerbert Xu 	if (mdb->size >= max) {
622eb1d1641SHerbert Xu 		max *= 2;
623036be6dbSThomas Graf 		if (unlikely(max > br->hash_max)) {
624036be6dbSThomas Graf 			br_warn(br, "Multicast hash table maximum of %d "
625036be6dbSThomas Graf 				"reached, disabling snooping: %s\n",
626036be6dbSThomas Graf 				br->hash_max,
627036be6dbSThomas Graf 				port ? port->dev->name : br->dev->name);
628eb1d1641SHerbert Xu 			err = -E2BIG;
629eb1d1641SHerbert Xu disable:
630eb1d1641SHerbert Xu 			br->multicast_disabled = 1;
631eb1d1641SHerbert Xu 			goto err;
632eb1d1641SHerbert Xu 		}
633eb1d1641SHerbert Xu 	}
634eb1d1641SHerbert Xu 
635eb1d1641SHerbert Xu 	if (max > mdb->max || elasticity) {
636eb1d1641SHerbert Xu 		if (mdb->old) {
637eb1d1641SHerbert Xu 			if (net_ratelimit())
63828a16c97Sstephen hemminger 				br_info(br, "Multicast hash table "
639eb1d1641SHerbert Xu 					"on fire: %s\n",
64028a16c97Sstephen hemminger 					port ? port->dev->name : br->dev->name);
641eb1d1641SHerbert Xu 			err = -EEXIST;
642eb1d1641SHerbert Xu 			goto err;
643eb1d1641SHerbert Xu 		}
644eb1d1641SHerbert Xu 
645eb1d1641SHerbert Xu 		err = br_mdb_rehash(&br->mdb, max, elasticity);
646eb1d1641SHerbert Xu 		if (err) {
64728a16c97Sstephen hemminger 			br_warn(br, "Cannot rehash multicast "
64828a16c97Sstephen hemminger 				"hash table, disabling snooping: %s, %d, %d\n",
64928a16c97Sstephen hemminger 				port ? port->dev->name : br->dev->name,
650eb1d1641SHerbert Xu 				mdb->size, err);
651eb1d1641SHerbert Xu 			goto disable;
652eb1d1641SHerbert Xu 		}
653eb1d1641SHerbert Xu 
654eb1d1641SHerbert Xu 		err = -EAGAIN;
655eb1d1641SHerbert Xu 		goto err;
656eb1d1641SHerbert Xu 	}
657eb1d1641SHerbert Xu 
658eb1d1641SHerbert Xu 	return NULL;
659eb1d1641SHerbert Xu 
660eb1d1641SHerbert Xu err:
661eb1d1641SHerbert Xu 	mp = ERR_PTR(err);
662eb1d1641SHerbert Xu 	return mp;
663eb1d1641SHerbert Xu }
664eb1d1641SHerbert Xu 
665cfd56754SCong Wang struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
6665e923585SNikolay Aleksandrov 						    struct net_bridge_port *p,
6675e923585SNikolay Aleksandrov 						    struct br_ip *group)
668eb1d1641SHerbert Xu {
669e8051688SEric Dumazet 	struct net_bridge_mdb_htable *mdb;
670eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
671eb1d1641SHerbert Xu 	int hash;
6724c0833bcSTobias Klauser 	int err;
673eb1d1641SHerbert Xu 
674e8051688SEric Dumazet 	mdb = rcu_dereference_protected(br->mdb, 1);
675eb1d1641SHerbert Xu 	if (!mdb) {
6764c0833bcSTobias Klauser 		err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0);
6774c0833bcSTobias Klauser 		if (err)
6784c0833bcSTobias Klauser 			return ERR_PTR(err);
679eb1d1641SHerbert Xu 		goto rehash;
680eb1d1641SHerbert Xu 	}
681eb1d1641SHerbert Xu 
682eb1d1641SHerbert Xu 	hash = br_ip_hash(mdb, group);
6835e923585SNikolay Aleksandrov 	mp = br_multicast_get_group(br, p, group, hash);
684eb1d1641SHerbert Xu 	switch (PTR_ERR(mp)) {
685eb1d1641SHerbert Xu 	case 0:
686eb1d1641SHerbert Xu 		break;
687eb1d1641SHerbert Xu 
688eb1d1641SHerbert Xu 	case -EAGAIN:
689eb1d1641SHerbert Xu rehash:
690e8051688SEric Dumazet 		mdb = rcu_dereference_protected(br->mdb, 1);
691eb1d1641SHerbert Xu 		hash = br_ip_hash(mdb, group);
692eb1d1641SHerbert Xu 		break;
693eb1d1641SHerbert Xu 
694eb1d1641SHerbert Xu 	default:
695eb1d1641SHerbert Xu 		goto out;
696eb1d1641SHerbert Xu 	}
697eb1d1641SHerbert Xu 
698eb1d1641SHerbert Xu 	mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
699eb1d1641SHerbert Xu 	if (unlikely(!mp))
7004c0833bcSTobias Klauser 		return ERR_PTR(-ENOMEM);
701eb1d1641SHerbert Xu 
702eb1d1641SHerbert Xu 	mp->br = br;
7038ef2a9a5SYOSHIFUJI Hideaki 	mp->addr = *group;
7041faabf2aSEric Dumazet 	setup_timer(&mp->timer, br_multicast_group_expired,
7051faabf2aSEric Dumazet 		    (unsigned long)mp);
7061faabf2aSEric Dumazet 
707eb1d1641SHerbert Xu 	hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
708eb1d1641SHerbert Xu 	mdb->size++;
709eb1d1641SHerbert Xu 
710eb1d1641SHerbert Xu out:
711eb1d1641SHerbert Xu 	return mp;
712eb1d1641SHerbert Xu }
713eb1d1641SHerbert Xu 
714cfd56754SCong Wang struct net_bridge_port_group *br_multicast_new_port_group(
715cfd56754SCong Wang 			struct net_bridge_port *port,
716cfd56754SCong Wang 			struct br_ip *group,
717ccb1c31aSAmerigo Wang 			struct net_bridge_port_group __rcu *next,
7186db6f0eaSFelix Fietkau 			unsigned char flags,
7196db6f0eaSFelix Fietkau 			const unsigned char *src)
720cfd56754SCong Wang {
721cfd56754SCong Wang 	struct net_bridge_port_group *p;
722cfd56754SCong Wang 
723cfd56754SCong Wang 	p = kzalloc(sizeof(*p), GFP_ATOMIC);
724cfd56754SCong Wang 	if (unlikely(!p))
725cfd56754SCong Wang 		return NULL;
726cfd56754SCong Wang 
727cfd56754SCong Wang 	p->addr = *group;
728cfd56754SCong Wang 	p->port = port;
7299d06b6d8SElad Raz 	p->flags = flags;
730eca2a43bSstephen hemminger 	rcu_assign_pointer(p->next, next);
731cfd56754SCong Wang 	hlist_add_head(&p->mglist, &port->mglist);
732cfd56754SCong Wang 	setup_timer(&p->timer, br_multicast_port_group_expired,
733cfd56754SCong Wang 		    (unsigned long)p);
7346db6f0eaSFelix Fietkau 
7356db6f0eaSFelix Fietkau 	if (src)
7366db6f0eaSFelix Fietkau 		memcpy(p->eth_addr, src, ETH_ALEN);
7376db6f0eaSFelix Fietkau 	else
7386db6f0eaSFelix Fietkau 		memset(p->eth_addr, 0xff, ETH_ALEN);
7396db6f0eaSFelix Fietkau 
740cfd56754SCong Wang 	return p;
741cfd56754SCong Wang }
742cfd56754SCong Wang 
7436db6f0eaSFelix Fietkau static bool br_port_group_equal(struct net_bridge_port_group *p,
7446db6f0eaSFelix Fietkau 				struct net_bridge_port *port,
7456db6f0eaSFelix Fietkau 				const unsigned char *src)
7466db6f0eaSFelix Fietkau {
7476db6f0eaSFelix Fietkau 	if (p->port != port)
7486db6f0eaSFelix Fietkau 		return false;
7496db6f0eaSFelix Fietkau 
7506db6f0eaSFelix Fietkau 	if (!(port->flags & BR_MULTICAST_TO_UNICAST))
7516db6f0eaSFelix Fietkau 		return true;
7526db6f0eaSFelix Fietkau 
7536db6f0eaSFelix Fietkau 	return ether_addr_equal(src, p->eth_addr);
7546db6f0eaSFelix Fietkau }
7556db6f0eaSFelix Fietkau 
756eb1d1641SHerbert Xu static int br_multicast_add_group(struct net_bridge *br,
7578ef2a9a5SYOSHIFUJI Hideaki 				  struct net_bridge_port *port,
7586db6f0eaSFelix Fietkau 				  struct br_ip *group,
7596db6f0eaSFelix Fietkau 				  const unsigned char *src)
760eb1d1641SHerbert Xu {
761e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
7625e923585SNikolay Aleksandrov 	struct net_bridge_port_group *p;
7635e923585SNikolay Aleksandrov 	struct net_bridge_mdb_entry *mp;
764454594f3SLinus Lüssing 	unsigned long now = jiffies;
765eb1d1641SHerbert Xu 	int err;
766eb1d1641SHerbert Xu 
767eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
768eb1d1641SHerbert Xu 	if (!netif_running(br->dev) ||
769eb1d1641SHerbert Xu 	    (port && port->state == BR_STATE_DISABLED))
770eb1d1641SHerbert Xu 		goto out;
771eb1d1641SHerbert Xu 
772eb1d1641SHerbert Xu 	mp = br_multicast_new_group(br, port, group);
773eb1d1641SHerbert Xu 	err = PTR_ERR(mp);
7744c0833bcSTobias Klauser 	if (IS_ERR(mp))
775eb1d1641SHerbert Xu 		goto err;
776eb1d1641SHerbert Xu 
777eb1d1641SHerbert Xu 	if (!port) {
7788a870178SHerbert Xu 		mp->mglist = true;
779454594f3SLinus Lüssing 		mod_timer(&mp->timer, now + br->multicast_membership_interval);
780eb1d1641SHerbert Xu 		goto out;
781eb1d1641SHerbert Xu 	}
782eb1d1641SHerbert Xu 
783e8051688SEric Dumazet 	for (pp = &mp->ports;
784e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
785e8051688SEric Dumazet 	     pp = &p->next) {
7866db6f0eaSFelix Fietkau 		if (br_port_group_equal(p, port, src))
787454594f3SLinus Lüssing 			goto found;
788eb1d1641SHerbert Xu 		if ((unsigned long)p->port < (unsigned long)port)
789eb1d1641SHerbert Xu 			break;
790eb1d1641SHerbert Xu 	}
791eb1d1641SHerbert Xu 
7926db6f0eaSFelix Fietkau 	p = br_multicast_new_port_group(port, group, *pp, 0, src);
793eb1d1641SHerbert Xu 	if (unlikely(!p))
794eb1d1641SHerbert Xu 		goto err;
795eb1d1641SHerbert Xu 	rcu_assign_pointer(*pp, p);
79645ebcce5SElad Raz 	br_mdb_notify(br->dev, port, group, RTM_NEWMDB, 0);
797eb1d1641SHerbert Xu 
798454594f3SLinus Lüssing found:
799454594f3SLinus Lüssing 	mod_timer(&p->timer, now + br->multicast_membership_interval);
800eb1d1641SHerbert Xu out:
801eb1d1641SHerbert Xu 	err = 0;
802eb1d1641SHerbert Xu 
803eb1d1641SHerbert Xu err:
804eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
805eb1d1641SHerbert Xu 	return err;
806eb1d1641SHerbert Xu }
807eb1d1641SHerbert Xu 
8088ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_add_group(struct net_bridge *br,
8098ef2a9a5SYOSHIFUJI Hideaki 				      struct net_bridge_port *port,
810b0e9a30dSVlad Yasevich 				      __be32 group,
8116db6f0eaSFelix Fietkau 				      __u16 vid,
8126db6f0eaSFelix Fietkau 				      const unsigned char *src)
8138ef2a9a5SYOSHIFUJI Hideaki {
8148ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip br_group;
8158ef2a9a5SYOSHIFUJI Hideaki 
8168ef2a9a5SYOSHIFUJI Hideaki 	if (ipv4_is_local_multicast(group))
8178ef2a9a5SYOSHIFUJI Hideaki 		return 0;
8188ef2a9a5SYOSHIFUJI Hideaki 
8198ef2a9a5SYOSHIFUJI Hideaki 	br_group.u.ip4 = group;
8208ef2a9a5SYOSHIFUJI Hideaki 	br_group.proto = htons(ETH_P_IP);
821b0e9a30dSVlad Yasevich 	br_group.vid = vid;
8228ef2a9a5SYOSHIFUJI Hideaki 
8236db6f0eaSFelix Fietkau 	return br_multicast_add_group(br, port, &br_group, src);
8248ef2a9a5SYOSHIFUJI Hideaki }
8258ef2a9a5SYOSHIFUJI Hideaki 
826dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
82708b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_add_group(struct net_bridge *br,
82808b202b6SYOSHIFUJI Hideaki 				      struct net_bridge_port *port,
829b0e9a30dSVlad Yasevich 				      const struct in6_addr *group,
8306db6f0eaSFelix Fietkau 				      __u16 vid,
8316db6f0eaSFelix Fietkau 				      const unsigned char *src)
83208b202b6SYOSHIFUJI Hideaki {
83308b202b6SYOSHIFUJI Hideaki 	struct br_ip br_group;
83408b202b6SYOSHIFUJI Hideaki 
8353c3769e6SLinus Lüssing 	if (ipv6_addr_is_ll_all_nodes(group))
83608b202b6SYOSHIFUJI Hideaki 		return 0;
83708b202b6SYOSHIFUJI Hideaki 
8384e3fd7a0SAlexey Dobriyan 	br_group.u.ip6 = *group;
8399cc6e0c4SLinus Lüssing 	br_group.proto = htons(ETH_P_IPV6);
840b0e9a30dSVlad Yasevich 	br_group.vid = vid;
84108b202b6SYOSHIFUJI Hideaki 
8426db6f0eaSFelix Fietkau 	return br_multicast_add_group(br, port, &br_group, src);
84308b202b6SYOSHIFUJI Hideaki }
84408b202b6SYOSHIFUJI Hideaki #endif
84508b202b6SYOSHIFUJI Hideaki 
846eb1d1641SHerbert Xu static void br_multicast_router_expired(unsigned long data)
847eb1d1641SHerbert Xu {
848eb1d1641SHerbert Xu 	struct net_bridge_port *port = (void *)data;
849eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
850eb1d1641SHerbert Xu 
851eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
852a55d8246SNikolay Aleksandrov 	if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
853a55d8246SNikolay Aleksandrov 	    port->multicast_router == MDB_RTR_TYPE_PERM ||
854f12e7d95SNogah Frankel 	    timer_pending(&port->multicast_router_timer))
855eb1d1641SHerbert Xu 		goto out;
856eb1d1641SHerbert Xu 
857f12e7d95SNogah Frankel 	__del_port_router(port);
858eb1d1641SHerbert Xu out:
859eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
860eb1d1641SHerbert Xu }
861eb1d1641SHerbert Xu 
86277041420SYotam Gigi static void br_mc_router_state_change(struct net_bridge *p,
86377041420SYotam Gigi 				      bool is_mc_router)
86477041420SYotam Gigi {
86577041420SYotam Gigi 	struct switchdev_attr attr = {
86677041420SYotam Gigi 		.orig_dev = p->dev,
86777041420SYotam Gigi 		.id = SWITCHDEV_ATTR_ID_BRIDGE_MROUTER,
86877041420SYotam Gigi 		.flags = SWITCHDEV_F_DEFER,
86977041420SYotam Gigi 		.u.mrouter = is_mc_router,
87077041420SYotam Gigi 	};
87177041420SYotam Gigi 
87277041420SYotam Gigi 	switchdev_port_attr_set(p->dev, &attr);
87377041420SYotam Gigi }
87477041420SYotam Gigi 
875eb1d1641SHerbert Xu static void br_multicast_local_router_expired(unsigned long data)
876eb1d1641SHerbert Xu {
87777041420SYotam Gigi 	struct net_bridge *br = (struct net_bridge *)data;
87877041420SYotam Gigi 
87977041420SYotam Gigi 	spin_lock(&br->multicast_lock);
88077041420SYotam Gigi 	if (br->multicast_router == MDB_RTR_TYPE_DISABLED ||
88177041420SYotam Gigi 	    br->multicast_router == MDB_RTR_TYPE_PERM ||
88277041420SYotam Gigi 	    timer_pending(&br->multicast_router_timer))
88377041420SYotam Gigi 		goto out;
88477041420SYotam Gigi 
88577041420SYotam Gigi 	br_mc_router_state_change(br, false);
88677041420SYotam Gigi out:
88777041420SYotam Gigi 	spin_unlock(&br->multicast_lock);
888eb1d1641SHerbert Xu }
889eb1d1641SHerbert Xu 
890cc0fdd80SLinus Lüssing static void br_multicast_querier_expired(struct net_bridge *br,
89190010b36SLinus Lüssing 					 struct bridge_mcast_own_query *query)
892c83b8fabSHerbert Xu {
893c83b8fabSHerbert Xu 	spin_lock(&br->multicast_lock);
894c83b8fabSHerbert Xu 	if (!netif_running(br->dev) || br->multicast_disabled)
895c83b8fabSHerbert Xu 		goto out;
896c83b8fabSHerbert Xu 
897cc0fdd80SLinus Lüssing 	br_multicast_start_querier(br, query);
898c83b8fabSHerbert Xu 
899c83b8fabSHerbert Xu out:
900c83b8fabSHerbert Xu 	spin_unlock(&br->multicast_lock);
901c83b8fabSHerbert Xu }
902c83b8fabSHerbert Xu 
903cc0fdd80SLinus Lüssing static void br_ip4_multicast_querier_expired(unsigned long data)
904cc0fdd80SLinus Lüssing {
905cc0fdd80SLinus Lüssing 	struct net_bridge *br = (void *)data;
906cc0fdd80SLinus Lüssing 
90790010b36SLinus Lüssing 	br_multicast_querier_expired(br, &br->ip4_own_query);
908cc0fdd80SLinus Lüssing }
909cc0fdd80SLinus Lüssing 
910cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
911cc0fdd80SLinus Lüssing static void br_ip6_multicast_querier_expired(unsigned long data)
912cc0fdd80SLinus Lüssing {
913cc0fdd80SLinus Lüssing 	struct net_bridge *br = (void *)data;
914cc0fdd80SLinus Lüssing 
91590010b36SLinus Lüssing 	br_multicast_querier_expired(br, &br->ip6_own_query);
916cc0fdd80SLinus Lüssing }
917cc0fdd80SLinus Lüssing #endif
918cc0fdd80SLinus Lüssing 
919dc4eb53aSLinus Lüssing static void br_multicast_select_own_querier(struct net_bridge *br,
920dc4eb53aSLinus Lüssing 					    struct br_ip *ip,
921dc4eb53aSLinus Lüssing 					    struct sk_buff *skb)
922dc4eb53aSLinus Lüssing {
923dc4eb53aSLinus Lüssing 	if (ip->proto == htons(ETH_P_IP))
924dc4eb53aSLinus Lüssing 		br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr;
925dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
926dc4eb53aSLinus Lüssing 	else
927dc4eb53aSLinus Lüssing 		br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr;
928dc4eb53aSLinus Lüssing #endif
929dc4eb53aSLinus Lüssing }
930dc4eb53aSLinus Lüssing 
9318ef2a9a5SYOSHIFUJI Hideaki static void __br_multicast_send_query(struct net_bridge *br,
9328ef2a9a5SYOSHIFUJI Hideaki 				      struct net_bridge_port *port,
9338ef2a9a5SYOSHIFUJI Hideaki 				      struct br_ip *ip)
934eb1d1641SHerbert Xu {
935eb1d1641SHerbert Xu 	struct sk_buff *skb;
9361080ab95SNikolay Aleksandrov 	u8 igmp_type;
937eb1d1641SHerbert Xu 
9381080ab95SNikolay Aleksandrov 	skb = br_multicast_alloc_query(br, ip, &igmp_type);
939eb1d1641SHerbert Xu 	if (!skb)
9408ef2a9a5SYOSHIFUJI Hideaki 		return;
941eb1d1641SHerbert Xu 
942eb1d1641SHerbert Xu 	if (port) {
943eb1d1641SHerbert Xu 		skb->dev = port->dev;
944a65056ecSNikolay Aleksandrov 		br_multicast_count(br, port, skb, igmp_type,
9451080ab95SNikolay Aleksandrov 				   BR_MCAST_DIR_TX);
94629a26a56SEric W. Biederman 		NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
94729a26a56SEric W. Biederman 			dev_net(port->dev), NULL, skb, NULL, skb->dev,
948f0b4eeceSLinus Lüssing 			br_dev_queue_push_xmit);
949dc4eb53aSLinus Lüssing 	} else {
950dc4eb53aSLinus Lüssing 		br_multicast_select_own_querier(br, ip, skb);
951a65056ecSNikolay Aleksandrov 		br_multicast_count(br, port, skb, igmp_type,
9521080ab95SNikolay Aleksandrov 				   BR_MCAST_DIR_RX);
953eb1d1641SHerbert Xu 		netif_rx(skb);
9548ef2a9a5SYOSHIFUJI Hideaki 	}
955dc4eb53aSLinus Lüssing }
956eb1d1641SHerbert Xu 
9578ef2a9a5SYOSHIFUJI Hideaki static void br_multicast_send_query(struct net_bridge *br,
958cc0fdd80SLinus Lüssing 				    struct net_bridge_port *port,
95990010b36SLinus Lüssing 				    struct bridge_mcast_own_query *own_query)
9608ef2a9a5SYOSHIFUJI Hideaki {
96190010b36SLinus Lüssing 	struct bridge_mcast_other_query *other_query = NULL;
9625e923585SNikolay Aleksandrov 	struct br_ip br_group;
9635e923585SNikolay Aleksandrov 	unsigned long time;
9648ef2a9a5SYOSHIFUJI Hideaki 
9658ef2a9a5SYOSHIFUJI Hideaki 	if (!netif_running(br->dev) || br->multicast_disabled ||
966cc0fdd80SLinus Lüssing 	    !br->multicast_querier)
9678ef2a9a5SYOSHIFUJI Hideaki 		return;
9688ef2a9a5SYOSHIFUJI Hideaki 
96908b202b6SYOSHIFUJI Hideaki 	memset(&br_group.u, 0, sizeof(br_group.u));
9708ef2a9a5SYOSHIFUJI Hideaki 
97190010b36SLinus Lüssing 	if (port ? (own_query == &port->ip4_own_query) :
97290010b36SLinus Lüssing 		   (own_query == &br->ip4_own_query)) {
97390010b36SLinus Lüssing 		other_query = &br->ip4_other_query;
97408b202b6SYOSHIFUJI Hideaki 		br_group.proto = htons(ETH_P_IP);
975dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
976cc0fdd80SLinus Lüssing 	} else {
97790010b36SLinus Lüssing 		other_query = &br->ip6_other_query;
97808b202b6SYOSHIFUJI Hideaki 		br_group.proto = htons(ETH_P_IPV6);
97908b202b6SYOSHIFUJI Hideaki #endif
980eb1d1641SHerbert Xu 	}
981eb1d1641SHerbert Xu 
98290010b36SLinus Lüssing 	if (!other_query || timer_pending(&other_query->timer))
983cc0fdd80SLinus Lüssing 		return;
984cc0fdd80SLinus Lüssing 
985cc0fdd80SLinus Lüssing 	__br_multicast_send_query(br, port, &br_group);
986cc0fdd80SLinus Lüssing 
987cc0fdd80SLinus Lüssing 	time = jiffies;
98890010b36SLinus Lüssing 	time += own_query->startup_sent < br->multicast_startup_query_count ?
989cc0fdd80SLinus Lüssing 		br->multicast_startup_query_interval :
990cc0fdd80SLinus Lüssing 		br->multicast_query_interval;
99190010b36SLinus Lüssing 	mod_timer(&own_query->timer, time);
992cc0fdd80SLinus Lüssing }
993cc0fdd80SLinus Lüssing 
99490010b36SLinus Lüssing static void
99590010b36SLinus Lüssing br_multicast_port_query_expired(struct net_bridge_port *port,
99690010b36SLinus Lüssing 				struct bridge_mcast_own_query *query)
997eb1d1641SHerbert Xu {
998eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
999eb1d1641SHerbert Xu 
1000eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
100102a780c0SDan Carpenter 	if (port->state == BR_STATE_DISABLED ||
100202a780c0SDan Carpenter 	    port->state == BR_STATE_BLOCKING)
1003eb1d1641SHerbert Xu 		goto out;
1004eb1d1641SHerbert Xu 
1005cc0fdd80SLinus Lüssing 	if (query->startup_sent < br->multicast_startup_query_count)
1006cc0fdd80SLinus Lüssing 		query->startup_sent++;
1007eb1d1641SHerbert Xu 
1008cc0fdd80SLinus Lüssing 	br_multicast_send_query(port->br, port, query);
1009eb1d1641SHerbert Xu 
1010eb1d1641SHerbert Xu out:
1011eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1012eb1d1641SHerbert Xu }
1013eb1d1641SHerbert Xu 
1014cc0fdd80SLinus Lüssing static void br_ip4_multicast_port_query_expired(unsigned long data)
1015cc0fdd80SLinus Lüssing {
1016cc0fdd80SLinus Lüssing 	struct net_bridge_port *port = (void *)data;
1017cc0fdd80SLinus Lüssing 
101890010b36SLinus Lüssing 	br_multicast_port_query_expired(port, &port->ip4_own_query);
1019cc0fdd80SLinus Lüssing }
1020cc0fdd80SLinus Lüssing 
1021cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
1022cc0fdd80SLinus Lüssing static void br_ip6_multicast_port_query_expired(unsigned long data)
1023cc0fdd80SLinus Lüssing {
1024cc0fdd80SLinus Lüssing 	struct net_bridge_port *port = (void *)data;
1025cc0fdd80SLinus Lüssing 
102690010b36SLinus Lüssing 	br_multicast_port_query_expired(port, &port->ip6_own_query);
1027cc0fdd80SLinus Lüssing }
1028cc0fdd80SLinus Lüssing #endif
1029cc0fdd80SLinus Lüssing 
1030147c1e9bSNogah Frankel static void br_mc_disabled_update(struct net_device *dev, bool value)
1031147c1e9bSNogah Frankel {
1032147c1e9bSNogah Frankel 	struct switchdev_attr attr = {
1033147c1e9bSNogah Frankel 		.orig_dev = dev,
1034147c1e9bSNogah Frankel 		.id = SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED,
1035147c1e9bSNogah Frankel 		.flags = SWITCHDEV_F_DEFER,
1036147c1e9bSNogah Frankel 		.u.mc_disabled = value,
1037147c1e9bSNogah Frankel 	};
1038147c1e9bSNogah Frankel 
1039147c1e9bSNogah Frankel 	switchdev_port_attr_set(dev, &attr);
1040147c1e9bSNogah Frankel }
1041147c1e9bSNogah Frankel 
10421080ab95SNikolay Aleksandrov int br_multicast_add_port(struct net_bridge_port *port)
1043eb1d1641SHerbert Xu {
10447f0aec7aSNikolay Aleksandrov 	port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
1045eb1d1641SHerbert Xu 
1046eb1d1641SHerbert Xu 	setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
1047eb1d1641SHerbert Xu 		    (unsigned long)port);
104890010b36SLinus Lüssing 	setup_timer(&port->ip4_own_query.timer,
104990010b36SLinus Lüssing 		    br_ip4_multicast_port_query_expired, (unsigned long)port);
1050cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
105190010b36SLinus Lüssing 	setup_timer(&port->ip6_own_query.timer,
105290010b36SLinus Lüssing 		    br_ip6_multicast_port_query_expired, (unsigned long)port);
1053cc0fdd80SLinus Lüssing #endif
1054147c1e9bSNogah Frankel 	br_mc_disabled_update(port->dev, port->br->multicast_disabled);
1055147c1e9bSNogah Frankel 
10561080ab95SNikolay Aleksandrov 	port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
10571080ab95SNikolay Aleksandrov 	if (!port->mcast_stats)
10581080ab95SNikolay Aleksandrov 		return -ENOMEM;
10591080ab95SNikolay Aleksandrov 
10601080ab95SNikolay Aleksandrov 	return 0;
1061eb1d1641SHerbert Xu }
1062eb1d1641SHerbert Xu 
1063eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port)
1064eb1d1641SHerbert Xu {
1065e10177abSSatish Ashok 	struct net_bridge *br = port->br;
1066e10177abSSatish Ashok 	struct net_bridge_port_group *pg;
1067e10177abSSatish Ashok 	struct hlist_node *n;
1068e10177abSSatish Ashok 
1069e10177abSSatish Ashok 	/* Take care of the remaining groups, only perm ones should be left */
1070e10177abSSatish Ashok 	spin_lock_bh(&br->multicast_lock);
1071e10177abSSatish Ashok 	hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
1072e10177abSSatish Ashok 		br_multicast_del_pg(br, pg);
1073e10177abSSatish Ashok 	spin_unlock_bh(&br->multicast_lock);
1074eb1d1641SHerbert Xu 	del_timer_sync(&port->multicast_router_timer);
10751080ab95SNikolay Aleksandrov 	free_percpu(port->mcast_stats);
1076eb1d1641SHerbert Xu }
1077eb1d1641SHerbert Xu 
107890010b36SLinus Lüssing static void br_multicast_enable(struct bridge_mcast_own_query *query)
1079561f1103SHerbert Xu {
1080cc0fdd80SLinus Lüssing 	query->startup_sent = 0;
1081561f1103SHerbert Xu 
1082cc0fdd80SLinus Lüssing 	if (try_to_del_timer_sync(&query->timer) >= 0 ||
1083cc0fdd80SLinus Lüssing 	    del_timer(&query->timer))
1084cc0fdd80SLinus Lüssing 		mod_timer(&query->timer, jiffies);
1085561f1103SHerbert Xu }
1086561f1103SHerbert Xu 
10877cb3f921SNikolay Aleksandrov static void __br_multicast_enable_port(struct net_bridge_port *port)
1088eb1d1641SHerbert Xu {
1089eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
1090eb1d1641SHerbert Xu 
1091eb1d1641SHerbert Xu 	if (br->multicast_disabled || !netif_running(br->dev))
10927cb3f921SNikolay Aleksandrov 		return;
1093eb1d1641SHerbert Xu 
109490010b36SLinus Lüssing 	br_multicast_enable(&port->ip4_own_query);
1095cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
109690010b36SLinus Lüssing 	br_multicast_enable(&port->ip6_own_query);
1097cc0fdd80SLinus Lüssing #endif
10987f0aec7aSNikolay Aleksandrov 	if (port->multicast_router == MDB_RTR_TYPE_PERM &&
10997f0aec7aSNikolay Aleksandrov 	    hlist_unhashed(&port->rlist))
1100754bc547SSatish Ashok 		br_multicast_add_router(br, port);
11017cb3f921SNikolay Aleksandrov }
1102eb1d1641SHerbert Xu 
11037cb3f921SNikolay Aleksandrov void br_multicast_enable_port(struct net_bridge_port *port)
11047cb3f921SNikolay Aleksandrov {
11057cb3f921SNikolay Aleksandrov 	struct net_bridge *br = port->br;
11067cb3f921SNikolay Aleksandrov 
11077cb3f921SNikolay Aleksandrov 	spin_lock(&br->multicast_lock);
11087cb3f921SNikolay Aleksandrov 	__br_multicast_enable_port(port);
1109eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1110eb1d1641SHerbert Xu }
1111eb1d1641SHerbert Xu 
1112eb1d1641SHerbert Xu void br_multicast_disable_port(struct net_bridge_port *port)
1113eb1d1641SHerbert Xu {
1114eb1d1641SHerbert Xu 	struct net_bridge *br = port->br;
1115eb1d1641SHerbert Xu 	struct net_bridge_port_group *pg;
1116b67bfe0dSSasha Levin 	struct hlist_node *n;
1117eb1d1641SHerbert Xu 
1118eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
1119b67bfe0dSSasha Levin 	hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
11209d06b6d8SElad Raz 		if (!(pg->flags & MDB_PG_FLAGS_PERMANENT))
1121eb1d1641SHerbert Xu 			br_multicast_del_pg(br, pg);
1122eb1d1641SHerbert Xu 
1123f12e7d95SNogah Frankel 	__del_port_router(port);
1124f12e7d95SNogah Frankel 
1125eb1d1641SHerbert Xu 	del_timer(&port->multicast_router_timer);
112690010b36SLinus Lüssing 	del_timer(&port->ip4_own_query.timer);
1127cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
112890010b36SLinus Lüssing 	del_timer(&port->ip6_own_query.timer);
1129cc0fdd80SLinus Lüssing #endif
1130eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1131eb1d1641SHerbert Xu }
1132eb1d1641SHerbert Xu 
11338ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
1134eb1d1641SHerbert Xu 					 struct net_bridge_port *port,
113506499098SVlad Yasevich 					 struct sk_buff *skb,
113606499098SVlad Yasevich 					 u16 vid)
1137eb1d1641SHerbert Xu {
11386db6f0eaSFelix Fietkau 	const unsigned char *src;
1139eb1d1641SHerbert Xu 	struct igmpv3_report *ih;
1140eb1d1641SHerbert Xu 	struct igmpv3_grec *grec;
1141eb1d1641SHerbert Xu 	int i;
1142eb1d1641SHerbert Xu 	int len;
1143eb1d1641SHerbert Xu 	int num;
1144eb1d1641SHerbert Xu 	int type;
1145eb1d1641SHerbert Xu 	int err = 0;
1146eb1d1641SHerbert Xu 	__be32 group;
1147eb1d1641SHerbert Xu 
1148eb1d1641SHerbert Xu 	ih = igmpv3_report_hdr(skb);
1149eb1d1641SHerbert Xu 	num = ntohs(ih->ngrec);
1150c2d4fbd2SLinus Lüssing 	len = skb_transport_offset(skb) + sizeof(*ih);
1151eb1d1641SHerbert Xu 
1152eb1d1641SHerbert Xu 	for (i = 0; i < num; i++) {
1153eb1d1641SHerbert Xu 		len += sizeof(*grec);
1154eb1d1641SHerbert Xu 		if (!pskb_may_pull(skb, len))
1155eb1d1641SHerbert Xu 			return -EINVAL;
1156eb1d1641SHerbert Xu 
1157fd218cf9SHerbert Xu 		grec = (void *)(skb->data + len - sizeof(*grec));
1158eb1d1641SHerbert Xu 		group = grec->grec_mca;
1159eb1d1641SHerbert Xu 		type = grec->grec_type;
1160eb1d1641SHerbert Xu 
11618eabf95cSEric Dumazet 		len += ntohs(grec->grec_nsrcs) * 4;
1162eb1d1641SHerbert Xu 		if (!pskb_may_pull(skb, len))
1163eb1d1641SHerbert Xu 			return -EINVAL;
1164eb1d1641SHerbert Xu 
1165eb1d1641SHerbert Xu 		/* We treat this as an IGMPv2 report for now. */
1166eb1d1641SHerbert Xu 		switch (type) {
1167eb1d1641SHerbert Xu 		case IGMPV3_MODE_IS_INCLUDE:
1168eb1d1641SHerbert Xu 		case IGMPV3_MODE_IS_EXCLUDE:
1169eb1d1641SHerbert Xu 		case IGMPV3_CHANGE_TO_INCLUDE:
1170eb1d1641SHerbert Xu 		case IGMPV3_CHANGE_TO_EXCLUDE:
1171eb1d1641SHerbert Xu 		case IGMPV3_ALLOW_NEW_SOURCES:
1172eb1d1641SHerbert Xu 		case IGMPV3_BLOCK_OLD_SOURCES:
1173eb1d1641SHerbert Xu 			break;
1174eb1d1641SHerbert Xu 
1175eb1d1641SHerbert Xu 		default:
1176eb1d1641SHerbert Xu 			continue;
1177eb1d1641SHerbert Xu 		}
1178eb1d1641SHerbert Xu 
11796db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
1180bc8c20acSSatish Ashok 		if ((type == IGMPV3_CHANGE_TO_INCLUDE ||
1181bc8c20acSSatish Ashok 		     type == IGMPV3_MODE_IS_INCLUDE) &&
1182bc8c20acSSatish Ashok 		    ntohs(grec->grec_nsrcs) == 0) {
11836db6f0eaSFelix Fietkau 			br_ip4_multicast_leave_group(br, port, group, vid, src);
1184bc8c20acSSatish Ashok 		} else {
11856db6f0eaSFelix Fietkau 			err = br_ip4_multicast_add_group(br, port, group, vid,
11866db6f0eaSFelix Fietkau 							 src);
1187eb1d1641SHerbert Xu 			if (err)
1188eb1d1641SHerbert Xu 				break;
1189eb1d1641SHerbert Xu 		}
1190bc8c20acSSatish Ashok 	}
1191eb1d1641SHerbert Xu 
1192eb1d1641SHerbert Xu 	return err;
1193eb1d1641SHerbert Xu }
1194eb1d1641SHerbert Xu 
1195dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
119608b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_mld2_report(struct net_bridge *br,
119708b202b6SYOSHIFUJI Hideaki 					struct net_bridge_port *port,
119806499098SVlad Yasevich 					struct sk_buff *skb,
119906499098SVlad Yasevich 					u16 vid)
120008b202b6SYOSHIFUJI Hideaki {
12016db6f0eaSFelix Fietkau 	const unsigned char *src;
120208b202b6SYOSHIFUJI Hideaki 	struct icmp6hdr *icmp6h;
120308b202b6SYOSHIFUJI Hideaki 	struct mld2_grec *grec;
120408b202b6SYOSHIFUJI Hideaki 	int i;
120508b202b6SYOSHIFUJI Hideaki 	int len;
120608b202b6SYOSHIFUJI Hideaki 	int num;
120708b202b6SYOSHIFUJI Hideaki 	int err = 0;
120808b202b6SYOSHIFUJI Hideaki 
120908b202b6SYOSHIFUJI Hideaki 	if (!pskb_may_pull(skb, sizeof(*icmp6h)))
121008b202b6SYOSHIFUJI Hideaki 		return -EINVAL;
121108b202b6SYOSHIFUJI Hideaki 
121208b202b6SYOSHIFUJI Hideaki 	icmp6h = icmp6_hdr(skb);
121308b202b6SYOSHIFUJI Hideaki 	num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
1214c2d4fbd2SLinus Lüssing 	len = skb_transport_offset(skb) + sizeof(*icmp6h);
121508b202b6SYOSHIFUJI Hideaki 
121608b202b6SYOSHIFUJI Hideaki 	for (i = 0; i < num; i++) {
121708b202b6SYOSHIFUJI Hideaki 		__be16 *nsrcs, _nsrcs;
121808b202b6SYOSHIFUJI Hideaki 
121908b202b6SYOSHIFUJI Hideaki 		nsrcs = skb_header_pointer(skb,
122008b202b6SYOSHIFUJI Hideaki 					   len + offsetof(struct mld2_grec,
1221649e984dSLinus Lüssing 							  grec_nsrcs),
122208b202b6SYOSHIFUJI Hideaki 					   sizeof(_nsrcs), &_nsrcs);
122308b202b6SYOSHIFUJI Hideaki 		if (!nsrcs)
122408b202b6SYOSHIFUJI Hideaki 			return -EINVAL;
122508b202b6SYOSHIFUJI Hideaki 
122608b202b6SYOSHIFUJI Hideaki 		if (!pskb_may_pull(skb,
122708b202b6SYOSHIFUJI Hideaki 				   len + sizeof(*grec) +
1228d41db9f3SLinus Lüssing 				   sizeof(struct in6_addr) * ntohs(*nsrcs)))
122908b202b6SYOSHIFUJI Hideaki 			return -EINVAL;
123008b202b6SYOSHIFUJI Hideaki 
123108b202b6SYOSHIFUJI Hideaki 		grec = (struct mld2_grec *)(skb->data + len);
1232d41db9f3SLinus Lüssing 		len += sizeof(*grec) +
1233d41db9f3SLinus Lüssing 		       sizeof(struct in6_addr) * ntohs(*nsrcs);
123408b202b6SYOSHIFUJI Hideaki 
123508b202b6SYOSHIFUJI Hideaki 		/* We treat these as MLDv1 reports for now. */
123608b202b6SYOSHIFUJI Hideaki 		switch (grec->grec_type) {
123708b202b6SYOSHIFUJI Hideaki 		case MLD2_MODE_IS_INCLUDE:
123808b202b6SYOSHIFUJI Hideaki 		case MLD2_MODE_IS_EXCLUDE:
123908b202b6SYOSHIFUJI Hideaki 		case MLD2_CHANGE_TO_INCLUDE:
124008b202b6SYOSHIFUJI Hideaki 		case MLD2_CHANGE_TO_EXCLUDE:
124108b202b6SYOSHIFUJI Hideaki 		case MLD2_ALLOW_NEW_SOURCES:
124208b202b6SYOSHIFUJI Hideaki 		case MLD2_BLOCK_OLD_SOURCES:
124308b202b6SYOSHIFUJI Hideaki 			break;
124408b202b6SYOSHIFUJI Hideaki 
124508b202b6SYOSHIFUJI Hideaki 		default:
124608b202b6SYOSHIFUJI Hideaki 			continue;
124708b202b6SYOSHIFUJI Hideaki 		}
124808b202b6SYOSHIFUJI Hideaki 
12496db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
1250bc8c20acSSatish Ashok 		if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE ||
1251bc8c20acSSatish Ashok 		     grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
1252bc8c20acSSatish Ashok 		    ntohs(*nsrcs) == 0) {
1253bc8c20acSSatish Ashok 			br_ip6_multicast_leave_group(br, port, &grec->grec_mca,
12546db6f0eaSFelix Fietkau 						     vid, src);
1255bc8c20acSSatish Ashok 		} else {
1256bc8c20acSSatish Ashok 			err = br_ip6_multicast_add_group(br, port,
12576db6f0eaSFelix Fietkau 							 &grec->grec_mca, vid,
12586db6f0eaSFelix Fietkau 							 src);
12599264251eSDavide Caratti 			if (err)
126008b202b6SYOSHIFUJI Hideaki 				break;
126108b202b6SYOSHIFUJI Hideaki 		}
1262bc8c20acSSatish Ashok 	}
126308b202b6SYOSHIFUJI Hideaki 
126408b202b6SYOSHIFUJI Hideaki 	return err;
126508b202b6SYOSHIFUJI Hideaki }
126608b202b6SYOSHIFUJI Hideaki #endif
126708b202b6SYOSHIFUJI Hideaki 
1268dc4eb53aSLinus Lüssing static bool br_ip4_multicast_select_querier(struct net_bridge *br,
12692cd41431SLinus Lüssing 					    struct net_bridge_port *port,
1270dc4eb53aSLinus Lüssing 					    __be32 saddr)
1271dc4eb53aSLinus Lüssing {
1272dc4eb53aSLinus Lüssing 	if (!timer_pending(&br->ip4_own_query.timer) &&
1273dc4eb53aSLinus Lüssing 	    !timer_pending(&br->ip4_other_query.timer))
1274dc4eb53aSLinus Lüssing 		goto update;
1275dc4eb53aSLinus Lüssing 
1276dc4eb53aSLinus Lüssing 	if (!br->ip4_querier.addr.u.ip4)
1277dc4eb53aSLinus Lüssing 		goto update;
1278dc4eb53aSLinus Lüssing 
1279dc4eb53aSLinus Lüssing 	if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4))
1280dc4eb53aSLinus Lüssing 		goto update;
1281dc4eb53aSLinus Lüssing 
1282dc4eb53aSLinus Lüssing 	return false;
1283dc4eb53aSLinus Lüssing 
1284dc4eb53aSLinus Lüssing update:
1285dc4eb53aSLinus Lüssing 	br->ip4_querier.addr.u.ip4 = saddr;
1286dc4eb53aSLinus Lüssing 
12872cd41431SLinus Lüssing 	/* update protected by general multicast_lock by caller */
12882cd41431SLinus Lüssing 	rcu_assign_pointer(br->ip4_querier.port, port);
12892cd41431SLinus Lüssing 
1290dc4eb53aSLinus Lüssing 	return true;
1291dc4eb53aSLinus Lüssing }
1292dc4eb53aSLinus Lüssing 
1293dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
1294dc4eb53aSLinus Lüssing static bool br_ip6_multicast_select_querier(struct net_bridge *br,
12952cd41431SLinus Lüssing 					    struct net_bridge_port *port,
1296dc4eb53aSLinus Lüssing 					    struct in6_addr *saddr)
1297dc4eb53aSLinus Lüssing {
1298dc4eb53aSLinus Lüssing 	if (!timer_pending(&br->ip6_own_query.timer) &&
1299dc4eb53aSLinus Lüssing 	    !timer_pending(&br->ip6_other_query.timer))
1300dc4eb53aSLinus Lüssing 		goto update;
1301dc4eb53aSLinus Lüssing 
1302dc4eb53aSLinus Lüssing 	if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0)
1303dc4eb53aSLinus Lüssing 		goto update;
1304dc4eb53aSLinus Lüssing 
1305dc4eb53aSLinus Lüssing 	return false;
1306dc4eb53aSLinus Lüssing 
1307dc4eb53aSLinus Lüssing update:
1308dc4eb53aSLinus Lüssing 	br->ip6_querier.addr.u.ip6 = *saddr;
1309dc4eb53aSLinus Lüssing 
13102cd41431SLinus Lüssing 	/* update protected by general multicast_lock by caller */
13112cd41431SLinus Lüssing 	rcu_assign_pointer(br->ip6_querier.port, port);
13122cd41431SLinus Lüssing 
1313dc4eb53aSLinus Lüssing 	return true;
1314dc4eb53aSLinus Lüssing }
1315dc4eb53aSLinus Lüssing #endif
1316dc4eb53aSLinus Lüssing 
1317dc4eb53aSLinus Lüssing static bool br_multicast_select_querier(struct net_bridge *br,
13182cd41431SLinus Lüssing 					struct net_bridge_port *port,
1319dc4eb53aSLinus Lüssing 					struct br_ip *saddr)
1320dc4eb53aSLinus Lüssing {
1321dc4eb53aSLinus Lüssing 	switch (saddr->proto) {
1322dc4eb53aSLinus Lüssing 	case htons(ETH_P_IP):
13232cd41431SLinus Lüssing 		return br_ip4_multicast_select_querier(br, port, saddr->u.ip4);
1324dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
1325dc4eb53aSLinus Lüssing 	case htons(ETH_P_IPV6):
13262cd41431SLinus Lüssing 		return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6);
1327dc4eb53aSLinus Lüssing #endif
1328dc4eb53aSLinus Lüssing 	}
1329dc4eb53aSLinus Lüssing 
1330dc4eb53aSLinus Lüssing 	return false;
1331dc4eb53aSLinus Lüssing }
1332dc4eb53aSLinus Lüssing 
1333cc0fdd80SLinus Lüssing static void
133490010b36SLinus Lüssing br_multicast_update_query_timer(struct net_bridge *br,
133590010b36SLinus Lüssing 				struct bridge_mcast_other_query *query,
1336b00589afSLinus Lüssing 				unsigned long max_delay)
1337b00589afSLinus Lüssing {
133890010b36SLinus Lüssing 	if (!timer_pending(&query->timer))
133990010b36SLinus Lüssing 		query->delay_time = jiffies + max_delay;
1340b00589afSLinus Lüssing 
134190010b36SLinus Lüssing 	mod_timer(&query->timer, jiffies + br->multicast_querier_interval);
1342b00589afSLinus Lüssing }
1343b00589afSLinus Lüssing 
13446d549648SNogah Frankel static void br_port_mc_router_state_change(struct net_bridge_port *p,
13456d549648SNogah Frankel 					   bool is_mc_router)
13466d549648SNogah Frankel {
13476d549648SNogah Frankel 	struct switchdev_attr attr = {
13486d549648SNogah Frankel 		.orig_dev = p->dev,
13496d549648SNogah Frankel 		.id = SWITCHDEV_ATTR_ID_PORT_MROUTER,
13506d549648SNogah Frankel 		.flags = SWITCHDEV_F_DEFER,
13516d549648SNogah Frankel 		.u.mrouter = is_mc_router,
13526d549648SNogah Frankel 	};
13536d549648SNogah Frankel 
13546d549648SNogah Frankel 	switchdev_port_attr_set(p->dev, &attr);
13556d549648SNogah Frankel }
13566d549648SNogah Frankel 
13577e80c124Sstephen hemminger /*
13587c77602fSCong Wang  * Add port to router_list
13597e80c124Sstephen hemminger  *  list is maintained ordered by pointer value
13607e80c124Sstephen hemminger  *  and locked by br->multicast_lock and RCU
13617e80c124Sstephen hemminger  */
13620909e117SHerbert Xu static void br_multicast_add_router(struct net_bridge *br,
13630909e117SHerbert Xu 				    struct net_bridge_port *port)
13640909e117SHerbert Xu {
1365dcdca2c4Sstephen hemminger 	struct net_bridge_port *p;
1366b67bfe0dSSasha Levin 	struct hlist_node *slot = NULL;
13670909e117SHerbert Xu 
13681a040eacSNikolay Aleksandrov 	if (!hlist_unhashed(&port->rlist))
13691a040eacSNikolay Aleksandrov 		return;
13701a040eacSNikolay Aleksandrov 
1371b67bfe0dSSasha Levin 	hlist_for_each_entry(p, &br->router_list, rlist) {
13727e80c124Sstephen hemminger 		if ((unsigned long) port >= (unsigned long) p)
13737e80c124Sstephen hemminger 			break;
1374b67bfe0dSSasha Levin 		slot = &p->rlist;
1375dcdca2c4Sstephen hemminger 	}
13760909e117SHerbert Xu 
13777e80c124Sstephen hemminger 	if (slot)
13781d023284SKen Helias 		hlist_add_behind_rcu(&port->rlist, slot);
1379dcdca2c4Sstephen hemminger 	else
1380dcdca2c4Sstephen hemminger 		hlist_add_head_rcu(&port->rlist, &br->router_list);
1381949f1e39SSatish Ashok 	br_rtr_notify(br->dev, port, RTM_NEWMDB);
13826d549648SNogah Frankel 	br_port_mc_router_state_change(port, true);
13830909e117SHerbert Xu }
13840909e117SHerbert Xu 
1385eb1d1641SHerbert Xu static void br_multicast_mark_router(struct net_bridge *br,
1386eb1d1641SHerbert Xu 				     struct net_bridge_port *port)
1387eb1d1641SHerbert Xu {
1388eb1d1641SHerbert Xu 	unsigned long now = jiffies;
1389eb1d1641SHerbert Xu 
1390eb1d1641SHerbert Xu 	if (!port) {
139177041420SYotam Gigi 		if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) {
139277041420SYotam Gigi 			if (!timer_pending(&br->multicast_router_timer))
139377041420SYotam Gigi 				br_mc_router_state_change(br, true);
1394eb1d1641SHerbert Xu 			mod_timer(&br->multicast_router_timer,
1395eb1d1641SHerbert Xu 				  now + br->multicast_querier_interval);
139677041420SYotam Gigi 		}
1397eb1d1641SHerbert Xu 		return;
1398eb1d1641SHerbert Xu 	}
1399eb1d1641SHerbert Xu 
1400a55d8246SNikolay Aleksandrov 	if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
1401a55d8246SNikolay Aleksandrov 	    port->multicast_router == MDB_RTR_TYPE_PERM)
1402eb1d1641SHerbert Xu 		return;
1403eb1d1641SHerbert Xu 
14040909e117SHerbert Xu 	br_multicast_add_router(br, port);
1405eb1d1641SHerbert Xu 
1406eb1d1641SHerbert Xu 	mod_timer(&port->multicast_router_timer,
1407eb1d1641SHerbert Xu 		  now + br->multicast_querier_interval);
1408eb1d1641SHerbert Xu }
1409eb1d1641SHerbert Xu 
1410eb1d1641SHerbert Xu static void br_multicast_query_received(struct net_bridge *br,
1411eb1d1641SHerbert Xu 					struct net_bridge_port *port,
141290010b36SLinus Lüssing 					struct bridge_mcast_other_query *query,
1413dc4eb53aSLinus Lüssing 					struct br_ip *saddr,
1414b00589afSLinus Lüssing 					unsigned long max_delay)
1415eb1d1641SHerbert Xu {
14162cd41431SLinus Lüssing 	if (!br_multicast_select_querier(br, port, saddr))
1417eb1d1641SHerbert Xu 		return;
1418eb1d1641SHerbert Xu 
1419dc4eb53aSLinus Lüssing 	br_multicast_update_query_timer(br, query, max_delay);
1420eb1d1641SHerbert Xu 	br_multicast_mark_router(br, port);
1421eb1d1641SHerbert Xu }
1422eb1d1641SHerbert Xu 
14238ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_query(struct net_bridge *br,
1424eb1d1641SHerbert Xu 				  struct net_bridge_port *port,
142506499098SVlad Yasevich 				  struct sk_buff *skb,
142606499098SVlad Yasevich 				  u16 vid)
1427eb1d1641SHerbert Xu {
1428b71d1d42SEric Dumazet 	const struct iphdr *iph = ip_hdr(skb);
1429eb1d1641SHerbert Xu 	struct igmphdr *ih = igmp_hdr(skb);
1430eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
1431eb1d1641SHerbert Xu 	struct igmpv3_query *ih3;
1432eb1d1641SHerbert Xu 	struct net_bridge_port_group *p;
1433e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
1434dc4eb53aSLinus Lüssing 	struct br_ip saddr;
1435eb1d1641SHerbert Xu 	unsigned long max_delay;
1436eb1d1641SHerbert Xu 	unsigned long now = jiffies;
1437856ce5d0SLinus Lüssing 	unsigned int offset = skb_transport_offset(skb);
1438eb1d1641SHerbert Xu 	__be32 group;
1439bec68ff1SYOSHIFUJI Hideaki 	int err = 0;
1440eb1d1641SHerbert Xu 
1441eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
1442eb1d1641SHerbert Xu 	if (!netif_running(br->dev) ||
1443eb1d1641SHerbert Xu 	    (port && port->state == BR_STATE_DISABLED))
1444eb1d1641SHerbert Xu 		goto out;
1445eb1d1641SHerbert Xu 
1446eb1d1641SHerbert Xu 	group = ih->group;
1447eb1d1641SHerbert Xu 
1448856ce5d0SLinus Lüssing 	if (skb->len == offset + sizeof(*ih)) {
1449eb1d1641SHerbert Xu 		max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1450eb1d1641SHerbert Xu 
1451eb1d1641SHerbert Xu 		if (!max_delay) {
1452eb1d1641SHerbert Xu 			max_delay = 10 * HZ;
1453eb1d1641SHerbert Xu 			group = 0;
1454eb1d1641SHerbert Xu 		}
1455856ce5d0SLinus Lüssing 	} else if (skb->len >= offset + sizeof(*ih3)) {
1456eb1d1641SHerbert Xu 		ih3 = igmpv3_query_hdr(skb);
1457eb1d1641SHerbert Xu 		if (ih3->nsrcs)
1458bec68ff1SYOSHIFUJI Hideaki 			goto out;
1459eb1d1641SHerbert Xu 
14600ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 		max_delay = ih3->code ?
14610ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 			    IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
14629afd85c9SLinus Lüssing 	} else {
14639ed973ccSLinus Lüssing 		goto out;
14649ed973ccSLinus Lüssing 	}
14659ed973ccSLinus Lüssing 
1466dc4eb53aSLinus Lüssing 	if (!group) {
1467dc4eb53aSLinus Lüssing 		saddr.proto = htons(ETH_P_IP);
1468dc4eb53aSLinus Lüssing 		saddr.u.ip4 = iph->saddr;
1469b00589afSLinus Lüssing 
1470dc4eb53aSLinus Lüssing 		br_multicast_query_received(br, port, &br->ip4_other_query,
1471dc4eb53aSLinus Lüssing 					    &saddr, max_delay);
1472eb1d1641SHerbert Xu 		goto out;
1473dc4eb53aSLinus Lüssing 	}
1474eb1d1641SHerbert Xu 
1475b0e9a30dSVlad Yasevich 	mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid);
1476eb1d1641SHerbert Xu 	if (!mp)
1477eb1d1641SHerbert Xu 		goto out;
1478eb1d1641SHerbert Xu 
1479eb1d1641SHerbert Xu 	max_delay *= br->multicast_last_member_count;
1480eb1d1641SHerbert Xu 
14818a870178SHerbert Xu 	if (mp->mglist &&
1482eb1d1641SHerbert Xu 	    (timer_pending(&mp->timer) ?
1483eb1d1641SHerbert Xu 	     time_after(mp->timer.expires, now + max_delay) :
1484eb1d1641SHerbert Xu 	     try_to_del_timer_sync(&mp->timer) >= 0))
1485eb1d1641SHerbert Xu 		mod_timer(&mp->timer, now + max_delay);
1486eb1d1641SHerbert Xu 
1487e8051688SEric Dumazet 	for (pp = &mp->ports;
1488e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
1489e8051688SEric Dumazet 	     pp = &p->next) {
1490eb1d1641SHerbert Xu 		if (timer_pending(&p->timer) ?
1491eb1d1641SHerbert Xu 		    time_after(p->timer.expires, now + max_delay) :
1492eb1d1641SHerbert Xu 		    try_to_del_timer_sync(&p->timer) >= 0)
149324f9cdcbSHerbert Xu 			mod_timer(&p->timer, now + max_delay);
1494eb1d1641SHerbert Xu 	}
1495eb1d1641SHerbert Xu 
1496eb1d1641SHerbert Xu out:
1497eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1498bec68ff1SYOSHIFUJI Hideaki 	return err;
1499eb1d1641SHerbert Xu }
1500eb1d1641SHerbert Xu 
1501dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
150208b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_query(struct net_bridge *br,
150308b202b6SYOSHIFUJI Hideaki 				  struct net_bridge_port *port,
150406499098SVlad Yasevich 				  struct sk_buff *skb,
150506499098SVlad Yasevich 				  u16 vid)
150608b202b6SYOSHIFUJI Hideaki {
1507b71d1d42SEric Dumazet 	const struct ipv6hdr *ip6h = ipv6_hdr(skb);
1508eca2a43bSstephen hemminger 	struct mld_msg *mld;
150908b202b6SYOSHIFUJI Hideaki 	struct net_bridge_mdb_entry *mp;
151008b202b6SYOSHIFUJI Hideaki 	struct mld2_query *mld2q;
1511e8051688SEric Dumazet 	struct net_bridge_port_group *p;
1512e8051688SEric Dumazet 	struct net_bridge_port_group __rcu **pp;
1513dc4eb53aSLinus Lüssing 	struct br_ip saddr;
151408b202b6SYOSHIFUJI Hideaki 	unsigned long max_delay;
151508b202b6SYOSHIFUJI Hideaki 	unsigned long now = jiffies;
1516856ce5d0SLinus Lüssing 	unsigned int offset = skb_transport_offset(skb);
1517b71d1d42SEric Dumazet 	const struct in6_addr *group = NULL;
15189ed973ccSLinus Lüssing 	bool is_general_query;
151908b202b6SYOSHIFUJI Hideaki 	int err = 0;
152008b202b6SYOSHIFUJI Hideaki 
152108b202b6SYOSHIFUJI Hideaki 	spin_lock(&br->multicast_lock);
152208b202b6SYOSHIFUJI Hideaki 	if (!netif_running(br->dev) ||
152308b202b6SYOSHIFUJI Hideaki 	    (port && port->state == BR_STATE_DISABLED))
152408b202b6SYOSHIFUJI Hideaki 		goto out;
152508b202b6SYOSHIFUJI Hideaki 
1526856ce5d0SLinus Lüssing 	if (skb->len == offset + sizeof(*mld)) {
1527856ce5d0SLinus Lüssing 		if (!pskb_may_pull(skb, offset + sizeof(*mld))) {
152808b202b6SYOSHIFUJI Hideaki 			err = -EINVAL;
152908b202b6SYOSHIFUJI Hideaki 			goto out;
153008b202b6SYOSHIFUJI Hideaki 		}
153108b202b6SYOSHIFUJI Hideaki 		mld = (struct mld_msg *) icmp6_hdr(skb);
15324715213dSLi RongQing 		max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
153308b202b6SYOSHIFUJI Hideaki 		if (max_delay)
153408b202b6SYOSHIFUJI Hideaki 			group = &mld->mld_mca;
1535248ba8ecSLinus Lüssing 	} else {
1536856ce5d0SLinus Lüssing 		if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) {
153708b202b6SYOSHIFUJI Hideaki 			err = -EINVAL;
153808b202b6SYOSHIFUJI Hideaki 			goto out;
153908b202b6SYOSHIFUJI Hideaki 		}
154008b202b6SYOSHIFUJI Hideaki 		mld2q = (struct mld2_query *)icmp6_hdr(skb);
154108b202b6SYOSHIFUJI Hideaki 		if (!mld2q->mld2q_nsrcs)
154208b202b6SYOSHIFUJI Hideaki 			group = &mld2q->mld2q_mca;
1543e3f5b170SDaniel Borkmann 
1544e3f5b170SDaniel Borkmann 		max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
154508b202b6SYOSHIFUJI Hideaki 	}
154608b202b6SYOSHIFUJI Hideaki 
15479ed973ccSLinus Lüssing 	is_general_query = group && ipv6_addr_any(group);
15489ed973ccSLinus Lüssing 
1549dc4eb53aSLinus Lüssing 	if (is_general_query) {
1550dc4eb53aSLinus Lüssing 		saddr.proto = htons(ETH_P_IPV6);
1551dc4eb53aSLinus Lüssing 		saddr.u.ip6 = ip6h->saddr;
1552b00589afSLinus Lüssing 
1553dc4eb53aSLinus Lüssing 		br_multicast_query_received(br, port, &br->ip6_other_query,
1554dc4eb53aSLinus Lüssing 					    &saddr, max_delay);
155508b202b6SYOSHIFUJI Hideaki 		goto out;
15566c03ee8bSLinus Lüssing 	} else if (!group) {
15576c03ee8bSLinus Lüssing 		goto out;
1558dc4eb53aSLinus Lüssing 	}
155908b202b6SYOSHIFUJI Hideaki 
1560b0e9a30dSVlad Yasevich 	mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid);
156108b202b6SYOSHIFUJI Hideaki 	if (!mp)
156208b202b6SYOSHIFUJI Hideaki 		goto out;
156308b202b6SYOSHIFUJI Hideaki 
156408b202b6SYOSHIFUJI Hideaki 	max_delay *= br->multicast_last_member_count;
15658a870178SHerbert Xu 	if (mp->mglist &&
156608b202b6SYOSHIFUJI Hideaki 	    (timer_pending(&mp->timer) ?
156708b202b6SYOSHIFUJI Hideaki 	     time_after(mp->timer.expires, now + max_delay) :
156808b202b6SYOSHIFUJI Hideaki 	     try_to_del_timer_sync(&mp->timer) >= 0))
156908b202b6SYOSHIFUJI Hideaki 		mod_timer(&mp->timer, now + max_delay);
157008b202b6SYOSHIFUJI Hideaki 
1571e8051688SEric Dumazet 	for (pp = &mp->ports;
1572e8051688SEric Dumazet 	     (p = mlock_dereference(*pp, br)) != NULL;
1573e8051688SEric Dumazet 	     pp = &p->next) {
157408b202b6SYOSHIFUJI Hideaki 		if (timer_pending(&p->timer) ?
157508b202b6SYOSHIFUJI Hideaki 		    time_after(p->timer.expires, now + max_delay) :
157608b202b6SYOSHIFUJI Hideaki 		    try_to_del_timer_sync(&p->timer) >= 0)
157724f9cdcbSHerbert Xu 			mod_timer(&p->timer, now + max_delay);
157808b202b6SYOSHIFUJI Hideaki 	}
157908b202b6SYOSHIFUJI Hideaki 
158008b202b6SYOSHIFUJI Hideaki out:
158108b202b6SYOSHIFUJI Hideaki 	spin_unlock(&br->multicast_lock);
158208b202b6SYOSHIFUJI Hideaki 	return err;
158308b202b6SYOSHIFUJI Hideaki }
158408b202b6SYOSHIFUJI Hideaki #endif
158508b202b6SYOSHIFUJI Hideaki 
158690010b36SLinus Lüssing static void
158790010b36SLinus Lüssing br_multicast_leave_group(struct net_bridge *br,
1588eb1d1641SHerbert Xu 			 struct net_bridge_port *port,
1589cc0fdd80SLinus Lüssing 			 struct br_ip *group,
159090010b36SLinus Lüssing 			 struct bridge_mcast_other_query *other_query,
15916db6f0eaSFelix Fietkau 			 struct bridge_mcast_own_query *own_query,
15926db6f0eaSFelix Fietkau 			 const unsigned char *src)
1593eb1d1641SHerbert Xu {
1594eb1d1641SHerbert Xu 	struct net_bridge_mdb_htable *mdb;
1595eb1d1641SHerbert Xu 	struct net_bridge_mdb_entry *mp;
1596eb1d1641SHerbert Xu 	struct net_bridge_port_group *p;
1597eb1d1641SHerbert Xu 	unsigned long now;
1598eb1d1641SHerbert Xu 	unsigned long time;
1599eb1d1641SHerbert Xu 
1600eb1d1641SHerbert Xu 	spin_lock(&br->multicast_lock);
1601eb1d1641SHerbert Xu 	if (!netif_running(br->dev) ||
1602544586f7SSatish Ashok 	    (port && port->state == BR_STATE_DISABLED))
1603eb1d1641SHerbert Xu 		goto out;
1604eb1d1641SHerbert Xu 
1605e8051688SEric Dumazet 	mdb = mlock_dereference(br->mdb, br);
1606eb1d1641SHerbert Xu 	mp = br_mdb_ip_get(mdb, group);
1607eb1d1641SHerbert Xu 	if (!mp)
1608eb1d1641SHerbert Xu 		goto out;
1609eb1d1641SHerbert Xu 
1610544586f7SSatish Ashok 	if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
1611544586f7SSatish Ashok 		struct net_bridge_port_group __rcu **pp;
1612544586f7SSatish Ashok 
1613544586f7SSatish Ashok 		for (pp = &mp->ports;
1614544586f7SSatish Ashok 		     (p = mlock_dereference(*pp, br)) != NULL;
1615544586f7SSatish Ashok 		     pp = &p->next) {
16166db6f0eaSFelix Fietkau 			if (!br_port_group_equal(p, port, src))
1617544586f7SSatish Ashok 				continue;
1618544586f7SSatish Ashok 
1619544586f7SSatish Ashok 			rcu_assign_pointer(*pp, p->next);
1620544586f7SSatish Ashok 			hlist_del_init(&p->mglist);
1621544586f7SSatish Ashok 			del_timer(&p->timer);
1622544586f7SSatish Ashok 			call_rcu_bh(&p->rcu, br_multicast_free_pg);
162345ebcce5SElad Raz 			br_mdb_notify(br->dev, port, group, RTM_DELMDB,
162445ebcce5SElad Raz 				      p->flags);
1625544586f7SSatish Ashok 
1626544586f7SSatish Ashok 			if (!mp->ports && !mp->mglist &&
1627544586f7SSatish Ashok 			    netif_running(br->dev))
1628544586f7SSatish Ashok 				mod_timer(&mp->timer, jiffies);
1629544586f7SSatish Ashok 		}
1630544586f7SSatish Ashok 		goto out;
1631544586f7SSatish Ashok 	}
1632544586f7SSatish Ashok 
1633544586f7SSatish Ashok 	if (timer_pending(&other_query->timer))
1634544586f7SSatish Ashok 		goto out;
1635544586f7SSatish Ashok 
1636cc0fdd80SLinus Lüssing 	if (br->multicast_querier) {
16376b7df111SCong Wang 		__br_multicast_send_query(br, port, &mp->addr);
16386b7df111SCong Wang 
16396b7df111SCong Wang 		time = jiffies + br->multicast_last_member_count *
16406b7df111SCong Wang 				 br->multicast_last_member_interval;
1641cc0fdd80SLinus Lüssing 
164290010b36SLinus Lüssing 		mod_timer(&own_query->timer, time);
16436b7df111SCong Wang 
16446b7df111SCong Wang 		for (p = mlock_dereference(mp->ports, br);
16456b7df111SCong Wang 		     p != NULL;
16466b7df111SCong Wang 		     p = mlock_dereference(p->next, br)) {
16476db6f0eaSFelix Fietkau 			if (!br_port_group_equal(p, port, src))
16486b7df111SCong Wang 				continue;
16496b7df111SCong Wang 
16506b7df111SCong Wang 			if (!hlist_unhashed(&p->mglist) &&
16516b7df111SCong Wang 			    (timer_pending(&p->timer) ?
16526b7df111SCong Wang 			     time_after(p->timer.expires, time) :
16536b7df111SCong Wang 			     try_to_del_timer_sync(&p->timer) >= 0)) {
16546b7df111SCong Wang 				mod_timer(&p->timer, time);
16556b7df111SCong Wang 			}
16566b7df111SCong Wang 
16576b7df111SCong Wang 			break;
16586b7df111SCong Wang 		}
16596b7df111SCong Wang 	}
16606b7df111SCong Wang 
1661eb1d1641SHerbert Xu 	now = jiffies;
1662eb1d1641SHerbert Xu 	time = now + br->multicast_last_member_count *
1663eb1d1641SHerbert Xu 		     br->multicast_last_member_interval;
1664eb1d1641SHerbert Xu 
1665eb1d1641SHerbert Xu 	if (!port) {
1666454594f3SLinus Lüssing 		if (mp->mglist &&
1667eb1d1641SHerbert Xu 		    (timer_pending(&mp->timer) ?
1668eb1d1641SHerbert Xu 		     time_after(mp->timer.expires, time) :
1669eb1d1641SHerbert Xu 		     try_to_del_timer_sync(&mp->timer) >= 0)) {
1670eb1d1641SHerbert Xu 			mod_timer(&mp->timer, time);
1671eb1d1641SHerbert Xu 		}
1672454594f3SLinus Lüssing 
1673454594f3SLinus Lüssing 		goto out;
1674454594f3SLinus Lüssing 	}
1675454594f3SLinus Lüssing 
1676454594f3SLinus Lüssing 	for (p = mlock_dereference(mp->ports, br);
1677454594f3SLinus Lüssing 	     p != NULL;
1678454594f3SLinus Lüssing 	     p = mlock_dereference(p->next, br)) {
1679454594f3SLinus Lüssing 		if (p->port != port)
1680454594f3SLinus Lüssing 			continue;
1681454594f3SLinus Lüssing 
1682454594f3SLinus Lüssing 		if (!hlist_unhashed(&p->mglist) &&
1683454594f3SLinus Lüssing 		    (timer_pending(&p->timer) ?
1684454594f3SLinus Lüssing 		     time_after(p->timer.expires, time) :
1685454594f3SLinus Lüssing 		     try_to_del_timer_sync(&p->timer) >= 0)) {
1686454594f3SLinus Lüssing 			mod_timer(&p->timer, time);
1687454594f3SLinus Lüssing 		}
1688454594f3SLinus Lüssing 
1689454594f3SLinus Lüssing 		break;
1690eb1d1641SHerbert Xu 	}
1691eb1d1641SHerbert Xu out:
1692eb1d1641SHerbert Xu 	spin_unlock(&br->multicast_lock);
1693eb1d1641SHerbert Xu }
1694eb1d1641SHerbert Xu 
16958ef2a9a5SYOSHIFUJI Hideaki static void br_ip4_multicast_leave_group(struct net_bridge *br,
16968ef2a9a5SYOSHIFUJI Hideaki 					 struct net_bridge_port *port,
1697b0e9a30dSVlad Yasevich 					 __be32 group,
16986db6f0eaSFelix Fietkau 					 __u16 vid,
16996db6f0eaSFelix Fietkau 					 const unsigned char *src)
17008ef2a9a5SYOSHIFUJI Hideaki {
17018ef2a9a5SYOSHIFUJI Hideaki 	struct br_ip br_group;
170290010b36SLinus Lüssing 	struct bridge_mcast_own_query *own_query;
17038ef2a9a5SYOSHIFUJI Hideaki 
17048ef2a9a5SYOSHIFUJI Hideaki 	if (ipv4_is_local_multicast(group))
17058ef2a9a5SYOSHIFUJI Hideaki 		return;
17068ef2a9a5SYOSHIFUJI Hideaki 
170790010b36SLinus Lüssing 	own_query = port ? &port->ip4_own_query : &br->ip4_own_query;
170890010b36SLinus Lüssing 
17098ef2a9a5SYOSHIFUJI Hideaki 	br_group.u.ip4 = group;
17108ef2a9a5SYOSHIFUJI Hideaki 	br_group.proto = htons(ETH_P_IP);
1711b0e9a30dSVlad Yasevich 	br_group.vid = vid;
17128ef2a9a5SYOSHIFUJI Hideaki 
171390010b36SLinus Lüssing 	br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
17146db6f0eaSFelix Fietkau 				 own_query, src);
17158ef2a9a5SYOSHIFUJI Hideaki }
17168ef2a9a5SYOSHIFUJI Hideaki 
1717dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
171808b202b6SYOSHIFUJI Hideaki static void br_ip6_multicast_leave_group(struct net_bridge *br,
171908b202b6SYOSHIFUJI Hideaki 					 struct net_bridge_port *port,
1720b0e9a30dSVlad Yasevich 					 const struct in6_addr *group,
17216db6f0eaSFelix Fietkau 					 __u16 vid,
17226db6f0eaSFelix Fietkau 					 const unsigned char *src)
172308b202b6SYOSHIFUJI Hideaki {
172408b202b6SYOSHIFUJI Hideaki 	struct br_ip br_group;
172590010b36SLinus Lüssing 	struct bridge_mcast_own_query *own_query;
172608b202b6SYOSHIFUJI Hideaki 
17273c3769e6SLinus Lüssing 	if (ipv6_addr_is_ll_all_nodes(group))
172808b202b6SYOSHIFUJI Hideaki 		return;
172908b202b6SYOSHIFUJI Hideaki 
173090010b36SLinus Lüssing 	own_query = port ? &port->ip6_own_query : &br->ip6_own_query;
173190010b36SLinus Lüssing 
17324e3fd7a0SAlexey Dobriyan 	br_group.u.ip6 = *group;
173308b202b6SYOSHIFUJI Hideaki 	br_group.proto = htons(ETH_P_IPV6);
1734b0e9a30dSVlad Yasevich 	br_group.vid = vid;
173508b202b6SYOSHIFUJI Hideaki 
173690010b36SLinus Lüssing 	br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
17376db6f0eaSFelix Fietkau 				 own_query, src);
173808b202b6SYOSHIFUJI Hideaki }
173908b202b6SYOSHIFUJI Hideaki #endif
17408ef2a9a5SYOSHIFUJI Hideaki 
17411080ab95SNikolay Aleksandrov static void br_multicast_err_count(const struct net_bridge *br,
17421080ab95SNikolay Aleksandrov 				   const struct net_bridge_port *p,
17431080ab95SNikolay Aleksandrov 				   __be16 proto)
17441080ab95SNikolay Aleksandrov {
17451080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
17461080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats *pstats;
17471080ab95SNikolay Aleksandrov 
17481080ab95SNikolay Aleksandrov 	if (!br->multicast_stats_enabled)
17491080ab95SNikolay Aleksandrov 		return;
17501080ab95SNikolay Aleksandrov 
17511080ab95SNikolay Aleksandrov 	if (p)
17521080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
17531080ab95SNikolay Aleksandrov 	else
17541080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
17551080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
17561080ab95SNikolay Aleksandrov 		return;
17571080ab95SNikolay Aleksandrov 
17581080ab95SNikolay Aleksandrov 	pstats = this_cpu_ptr(stats);
17591080ab95SNikolay Aleksandrov 
17601080ab95SNikolay Aleksandrov 	u64_stats_update_begin(&pstats->syncp);
17611080ab95SNikolay Aleksandrov 	switch (proto) {
17621080ab95SNikolay Aleksandrov 	case htons(ETH_P_IP):
17631080ab95SNikolay Aleksandrov 		pstats->mstats.igmp_parse_errors++;
17641080ab95SNikolay Aleksandrov 		break;
17651080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
17661080ab95SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
17671080ab95SNikolay Aleksandrov 		pstats->mstats.mld_parse_errors++;
17681080ab95SNikolay Aleksandrov 		break;
17691080ab95SNikolay Aleksandrov #endif
17701080ab95SNikolay Aleksandrov 	}
17711080ab95SNikolay Aleksandrov 	u64_stats_update_end(&pstats->syncp);
17721080ab95SNikolay Aleksandrov }
17731080ab95SNikolay Aleksandrov 
177491b02d3dSNikolay Aleksandrov static void br_multicast_pim(struct net_bridge *br,
177591b02d3dSNikolay Aleksandrov 			     struct net_bridge_port *port,
177691b02d3dSNikolay Aleksandrov 			     const struct sk_buff *skb)
177791b02d3dSNikolay Aleksandrov {
177891b02d3dSNikolay Aleksandrov 	unsigned int offset = skb_transport_offset(skb);
177991b02d3dSNikolay Aleksandrov 	struct pimhdr *pimhdr, _pimhdr;
178091b02d3dSNikolay Aleksandrov 
178191b02d3dSNikolay Aleksandrov 	pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr);
178291b02d3dSNikolay Aleksandrov 	if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION ||
178391b02d3dSNikolay Aleksandrov 	    pim_hdr_type(pimhdr) != PIM_TYPE_HELLO)
178491b02d3dSNikolay Aleksandrov 		return;
178591b02d3dSNikolay Aleksandrov 
178691b02d3dSNikolay Aleksandrov 	br_multicast_mark_router(br, port);
178791b02d3dSNikolay Aleksandrov }
178891b02d3dSNikolay Aleksandrov 
1789eb1d1641SHerbert Xu static int br_multicast_ipv4_rcv(struct net_bridge *br,
1790eb1d1641SHerbert Xu 				 struct net_bridge_port *port,
179106499098SVlad Yasevich 				 struct sk_buff *skb,
179206499098SVlad Yasevich 				 u16 vid)
1793eb1d1641SHerbert Xu {
17949afd85c9SLinus Lüssing 	struct sk_buff *skb_trimmed = NULL;
17956db6f0eaSFelix Fietkau 	const unsigned char *src;
1796eb1d1641SHerbert Xu 	struct igmphdr *ih;
1797eb1d1641SHerbert Xu 	int err;
1798eb1d1641SHerbert Xu 
17999afd85c9SLinus Lüssing 	err = ip_mc_check_igmp(skb, &skb_trimmed);
1800eb1d1641SHerbert Xu 
18019afd85c9SLinus Lüssing 	if (err == -ENOMSG) {
180291b02d3dSNikolay Aleksandrov 		if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) {
1803bd4265feSHerbert Xu 			BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
180491b02d3dSNikolay Aleksandrov 		} else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) {
180591b02d3dSNikolay Aleksandrov 			if (ip_hdr(skb)->protocol == IPPROTO_PIM)
180691b02d3dSNikolay Aleksandrov 				br_multicast_pim(br, port, skb);
180791b02d3dSNikolay Aleksandrov 		}
1808eb1d1641SHerbert Xu 		return 0;
18099afd85c9SLinus Lüssing 	} else if (err < 0) {
18101080ab95SNikolay Aleksandrov 		br_multicast_err_count(br, port, skb->protocol);
18119afd85c9SLinus Lüssing 		return err;
1812bd4265feSHerbert Xu 	}
1813eb1d1641SHerbert Xu 
18149afd85c9SLinus Lüssing 	ih = igmp_hdr(skb);
18156db6f0eaSFelix Fietkau 	src = eth_hdr(skb)->h_source;
18161080ab95SNikolay Aleksandrov 	BR_INPUT_SKB_CB(skb)->igmp = ih->type;
1817eb1d1641SHerbert Xu 
1818eb1d1641SHerbert Xu 	switch (ih->type) {
1819eb1d1641SHerbert Xu 	case IGMP_HOST_MEMBERSHIP_REPORT:
1820eb1d1641SHerbert Xu 	case IGMPV2_HOST_MEMBERSHIP_REPORT:
182162b2bcb4SFernando Luis Vázquez Cao 		BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
18226db6f0eaSFelix Fietkau 		err = br_ip4_multicast_add_group(br, port, ih->group, vid, src);
1823eb1d1641SHerbert Xu 		break;
1824eb1d1641SHerbert Xu 	case IGMPV3_HOST_MEMBERSHIP_REPORT:
18259afd85c9SLinus Lüssing 		err = br_ip4_multicast_igmp3_report(br, port, skb_trimmed, vid);
1826eb1d1641SHerbert Xu 		break;
1827eb1d1641SHerbert Xu 	case IGMP_HOST_MEMBERSHIP_QUERY:
18289afd85c9SLinus Lüssing 		err = br_ip4_multicast_query(br, port, skb_trimmed, vid);
1829eb1d1641SHerbert Xu 		break;
1830eb1d1641SHerbert Xu 	case IGMP_HOST_LEAVE_MESSAGE:
18316db6f0eaSFelix Fietkau 		br_ip4_multicast_leave_group(br, port, ih->group, vid, src);
1832eb1d1641SHerbert Xu 		break;
1833eb1d1641SHerbert Xu 	}
1834eb1d1641SHerbert Xu 
1835a516993fSLinus Lüssing 	if (skb_trimmed && skb_trimmed != skb)
18369afd85c9SLinus Lüssing 		kfree_skb(skb_trimmed);
18379afd85c9SLinus Lüssing 
1838a65056ecSNikolay Aleksandrov 	br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
18391080ab95SNikolay Aleksandrov 			   BR_MCAST_DIR_RX);
18401080ab95SNikolay Aleksandrov 
1841eb1d1641SHerbert Xu 	return err;
1842eb1d1641SHerbert Xu }
1843eb1d1641SHerbert Xu 
1844dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
184508b202b6SYOSHIFUJI Hideaki static int br_multicast_ipv6_rcv(struct net_bridge *br,
184608b202b6SYOSHIFUJI Hideaki 				 struct net_bridge_port *port,
184706499098SVlad Yasevich 				 struct sk_buff *skb,
184806499098SVlad Yasevich 				 u16 vid)
184908b202b6SYOSHIFUJI Hideaki {
18509afd85c9SLinus Lüssing 	struct sk_buff *skb_trimmed = NULL;
18516db6f0eaSFelix Fietkau 	const unsigned char *src;
18529afd85c9SLinus Lüssing 	struct mld_msg *mld;
185308b202b6SYOSHIFUJI Hideaki 	int err;
185408b202b6SYOSHIFUJI Hideaki 
18559afd85c9SLinus Lüssing 	err = ipv6_mc_check_mld(skb, &skb_trimmed);
185608b202b6SYOSHIFUJI Hideaki 
18579afd85c9SLinus Lüssing 	if (err == -ENOMSG) {
18589afd85c9SLinus Lüssing 		if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
18598fad9c39SLinus Lüssing 			BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
186008b202b6SYOSHIFUJI Hideaki 		return 0;
18619afd85c9SLinus Lüssing 	} else if (err < 0) {
18621080ab95SNikolay Aleksandrov 		br_multicast_err_count(br, port, skb->protocol);
18639afd85c9SLinus Lüssing 		return err;
186408b202b6SYOSHIFUJI Hideaki 	}
186508b202b6SYOSHIFUJI Hideaki 
18669afd85c9SLinus Lüssing 	mld = (struct mld_msg *)skb_transport_header(skb);
18671080ab95SNikolay Aleksandrov 	BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type;
186808b202b6SYOSHIFUJI Hideaki 
18699afd85c9SLinus Lüssing 	switch (mld->mld_type) {
187008b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_REPORT:
18716db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
1872fc2af6c7SFernando Luis Vázquez Cao 		BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
18736db6f0eaSFelix Fietkau 		err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid,
18746db6f0eaSFelix Fietkau 						 src);
187508b202b6SYOSHIFUJI Hideaki 		break;
187608b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MLD2_REPORT:
18779afd85c9SLinus Lüssing 		err = br_ip6_multicast_mld2_report(br, port, skb_trimmed, vid);
187808b202b6SYOSHIFUJI Hideaki 		break;
187908b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_QUERY:
18809afd85c9SLinus Lüssing 		err = br_ip6_multicast_query(br, port, skb_trimmed, vid);
188108b202b6SYOSHIFUJI Hideaki 		break;
188208b202b6SYOSHIFUJI Hideaki 	case ICMPV6_MGM_REDUCTION:
18836db6f0eaSFelix Fietkau 		src = eth_hdr(skb)->h_source;
18846db6f0eaSFelix Fietkau 		br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src);
18859afd85c9SLinus Lüssing 		break;
188608b202b6SYOSHIFUJI Hideaki 	}
188708b202b6SYOSHIFUJI Hideaki 
1888a516993fSLinus Lüssing 	if (skb_trimmed && skb_trimmed != skb)
18899afd85c9SLinus Lüssing 		kfree_skb(skb_trimmed);
18909afd85c9SLinus Lüssing 
1891a65056ecSNikolay Aleksandrov 	br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
18921080ab95SNikolay Aleksandrov 			   BR_MCAST_DIR_RX);
18931080ab95SNikolay Aleksandrov 
189408b202b6SYOSHIFUJI Hideaki 	return err;
189508b202b6SYOSHIFUJI Hideaki }
189608b202b6SYOSHIFUJI Hideaki #endif
189708b202b6SYOSHIFUJI Hideaki 
1898eb1d1641SHerbert Xu int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
189906499098SVlad Yasevich 		     struct sk_buff *skb, u16 vid)
1900eb1d1641SHerbert Xu {
19011080ab95SNikolay Aleksandrov 	int ret = 0;
19021080ab95SNikolay Aleksandrov 
19031fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 	BR_INPUT_SKB_CB(skb)->igmp = 0;
19041fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 	BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
19051fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 
1906eb1d1641SHerbert Xu 	if (br->multicast_disabled)
1907eb1d1641SHerbert Xu 		return 0;
1908eb1d1641SHerbert Xu 
1909eb1d1641SHerbert Xu 	switch (skb->protocol) {
1910eb1d1641SHerbert Xu 	case htons(ETH_P_IP):
19111080ab95SNikolay Aleksandrov 		ret = br_multicast_ipv4_rcv(br, port, skb, vid);
19121080ab95SNikolay Aleksandrov 		break;
1913dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
191408b202b6SYOSHIFUJI Hideaki 	case htons(ETH_P_IPV6):
19151080ab95SNikolay Aleksandrov 		ret = br_multicast_ipv6_rcv(br, port, skb, vid);
19161080ab95SNikolay Aleksandrov 		break;
191708b202b6SYOSHIFUJI Hideaki #endif
1918eb1d1641SHerbert Xu 	}
1919eb1d1641SHerbert Xu 
19201080ab95SNikolay Aleksandrov 	return ret;
1921eb1d1641SHerbert Xu }
1922eb1d1641SHerbert Xu 
1923cc0fdd80SLinus Lüssing static void br_multicast_query_expired(struct net_bridge *br,
19242cd41431SLinus Lüssing 				       struct bridge_mcast_own_query *query,
19252cd41431SLinus Lüssing 				       struct bridge_mcast_querier *querier)
1926cc0fdd80SLinus Lüssing {
1927cc0fdd80SLinus Lüssing 	spin_lock(&br->multicast_lock);
1928cc0fdd80SLinus Lüssing 	if (query->startup_sent < br->multicast_startup_query_count)
1929cc0fdd80SLinus Lüssing 		query->startup_sent++;
1930cc0fdd80SLinus Lüssing 
193171d9f614SEric Dumazet 	RCU_INIT_POINTER(querier->port, NULL);
1932cc0fdd80SLinus Lüssing 	br_multicast_send_query(br, NULL, query);
1933cc0fdd80SLinus Lüssing 	spin_unlock(&br->multicast_lock);
1934cc0fdd80SLinus Lüssing }
1935cc0fdd80SLinus Lüssing 
1936cc0fdd80SLinus Lüssing static void br_ip4_multicast_query_expired(unsigned long data)
1937eb1d1641SHerbert Xu {
1938eb1d1641SHerbert Xu 	struct net_bridge *br = (void *)data;
1939eb1d1641SHerbert Xu 
19402cd41431SLinus Lüssing 	br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier);
1941eb1d1641SHerbert Xu }
1942eb1d1641SHerbert Xu 
1943cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
1944cc0fdd80SLinus Lüssing static void br_ip6_multicast_query_expired(unsigned long data)
1945cc0fdd80SLinus Lüssing {
1946cc0fdd80SLinus Lüssing 	struct net_bridge *br = (void *)data;
1947cc0fdd80SLinus Lüssing 
19482cd41431SLinus Lüssing 	br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier);
1949cc0fdd80SLinus Lüssing }
1950cc0fdd80SLinus Lüssing #endif
1951cc0fdd80SLinus Lüssing 
1952eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br)
1953eb1d1641SHerbert Xu {
1954eb1d1641SHerbert Xu 	br->hash_elasticity = 4;
1955eb1d1641SHerbert Xu 	br->hash_max = 512;
1956eb1d1641SHerbert Xu 
19577f0aec7aSNikolay Aleksandrov 	br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
1958c5c23260SHerbert Xu 	br->multicast_querier = 0;
19591c8ad5bfSCong Wang 	br->multicast_query_use_ifaddr = 0;
1960eb1d1641SHerbert Xu 	br->multicast_last_member_count = 2;
1961eb1d1641SHerbert Xu 	br->multicast_startup_query_count = 2;
1962eb1d1641SHerbert Xu 
1963eb1d1641SHerbert Xu 	br->multicast_last_member_interval = HZ;
1964eb1d1641SHerbert Xu 	br->multicast_query_response_interval = 10 * HZ;
1965eb1d1641SHerbert Xu 	br->multicast_startup_query_interval = 125 * HZ / 4;
1966eb1d1641SHerbert Xu 	br->multicast_query_interval = 125 * HZ;
1967eb1d1641SHerbert Xu 	br->multicast_querier_interval = 255 * HZ;
1968eb1d1641SHerbert Xu 	br->multicast_membership_interval = 260 * HZ;
1969eb1d1641SHerbert Xu 
197090010b36SLinus Lüssing 	br->ip4_other_query.delay_time = 0;
19712cd41431SLinus Lüssing 	br->ip4_querier.port = NULL;
1972aa2ae3e7SNikolay Aleksandrov 	br->multicast_igmp_version = 2;
1973cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
1974aa2ae3e7SNikolay Aleksandrov 	br->multicast_mld_version = 1;
197590010b36SLinus Lüssing 	br->ip6_other_query.delay_time = 0;
19762cd41431SLinus Lüssing 	br->ip6_querier.port = NULL;
1977cc0fdd80SLinus Lüssing #endif
19780888d5f3Sdaniel 	br->has_ipv6_addr = 1;
1979b00589afSLinus Lüssing 
1980eb1d1641SHerbert Xu 	spin_lock_init(&br->multicast_lock);
1981eb1d1641SHerbert Xu 	setup_timer(&br->multicast_router_timer,
198277041420SYotam Gigi 		    br_multicast_local_router_expired, (unsigned long)br);
198390010b36SLinus Lüssing 	setup_timer(&br->ip4_other_query.timer,
198490010b36SLinus Lüssing 		    br_ip4_multicast_querier_expired, (unsigned long)br);
198590010b36SLinus Lüssing 	setup_timer(&br->ip4_own_query.timer, br_ip4_multicast_query_expired,
1986cc0fdd80SLinus Lüssing 		    (unsigned long)br);
1987cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
198890010b36SLinus Lüssing 	setup_timer(&br->ip6_other_query.timer,
198990010b36SLinus Lüssing 		    br_ip6_multicast_querier_expired, (unsigned long)br);
199090010b36SLinus Lüssing 	setup_timer(&br->ip6_own_query.timer, br_ip6_multicast_query_expired,
1991cc0fdd80SLinus Lüssing 		    (unsigned long)br);
1992cc0fdd80SLinus Lüssing #endif
1993eb1d1641SHerbert Xu }
1994eb1d1641SHerbert Xu 
1995cc0fdd80SLinus Lüssing static void __br_multicast_open(struct net_bridge *br,
199690010b36SLinus Lüssing 				struct bridge_mcast_own_query *query)
1997eb1d1641SHerbert Xu {
1998cc0fdd80SLinus Lüssing 	query->startup_sent = 0;
1999eb1d1641SHerbert Xu 
2000eb1d1641SHerbert Xu 	if (br->multicast_disabled)
2001eb1d1641SHerbert Xu 		return;
2002eb1d1641SHerbert Xu 
2003cc0fdd80SLinus Lüssing 	mod_timer(&query->timer, jiffies);
2004cc0fdd80SLinus Lüssing }
2005cc0fdd80SLinus Lüssing 
2006cc0fdd80SLinus Lüssing void br_multicast_open(struct net_bridge *br)
2007cc0fdd80SLinus Lüssing {
200890010b36SLinus Lüssing 	__br_multicast_open(br, &br->ip4_own_query);
2009cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
201090010b36SLinus Lüssing 	__br_multicast_open(br, &br->ip6_own_query);
2011cc0fdd80SLinus Lüssing #endif
2012eb1d1641SHerbert Xu }
2013eb1d1641SHerbert Xu 
2014eb1d1641SHerbert Xu void br_multicast_stop(struct net_bridge *br)
2015eb1d1641SHerbert Xu {
2016eb1d1641SHerbert Xu 	del_timer_sync(&br->multicast_router_timer);
201790010b36SLinus Lüssing 	del_timer_sync(&br->ip4_other_query.timer);
201890010b36SLinus Lüssing 	del_timer_sync(&br->ip4_own_query.timer);
2019cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
202090010b36SLinus Lüssing 	del_timer_sync(&br->ip6_other_query.timer);
202190010b36SLinus Lüssing 	del_timer_sync(&br->ip6_own_query.timer);
2022cc0fdd80SLinus Lüssing #endif
2023e10177abSSatish Ashok }
2024e10177abSSatish Ashok 
2025e10177abSSatish Ashok void br_multicast_dev_del(struct net_bridge *br)
2026e10177abSSatish Ashok {
2027e10177abSSatish Ashok 	struct net_bridge_mdb_htable *mdb;
2028e10177abSSatish Ashok 	struct net_bridge_mdb_entry *mp;
2029e10177abSSatish Ashok 	struct hlist_node *n;
2030e10177abSSatish Ashok 	u32 ver;
2031e10177abSSatish Ashok 	int i;
2032eb1d1641SHerbert Xu 
2033eb1d1641SHerbert Xu 	spin_lock_bh(&br->multicast_lock);
2034e8051688SEric Dumazet 	mdb = mlock_dereference(br->mdb, br);
2035eb1d1641SHerbert Xu 	if (!mdb)
2036eb1d1641SHerbert Xu 		goto out;
2037eb1d1641SHerbert Xu 
2038eb1d1641SHerbert Xu 	br->mdb = NULL;
2039eb1d1641SHerbert Xu 
2040eb1d1641SHerbert Xu 	ver = mdb->ver;
2041eb1d1641SHerbert Xu 	for (i = 0; i < mdb->max; i++) {
2042b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(mp, n, &mdb->mhash[i],
2043eb1d1641SHerbert Xu 					  hlist[ver]) {
2044eb1d1641SHerbert Xu 			del_timer(&mp->timer);
2045eb1d1641SHerbert Xu 			call_rcu_bh(&mp->rcu, br_multicast_free_group);
2046eb1d1641SHerbert Xu 		}
2047eb1d1641SHerbert Xu 	}
2048eb1d1641SHerbert Xu 
2049eb1d1641SHerbert Xu 	if (mdb->old) {
2050eb1d1641SHerbert Xu 		spin_unlock_bh(&br->multicast_lock);
205110cc2b50SHerbert Xu 		rcu_barrier_bh();
2052eb1d1641SHerbert Xu 		spin_lock_bh(&br->multicast_lock);
2053eb1d1641SHerbert Xu 		WARN_ON(mdb->old);
2054eb1d1641SHerbert Xu 	}
2055eb1d1641SHerbert Xu 
2056eb1d1641SHerbert Xu 	mdb->old = mdb;
2057eb1d1641SHerbert Xu 	call_rcu_bh(&mdb->rcu, br_mdb_free);
2058eb1d1641SHerbert Xu 
2059eb1d1641SHerbert Xu out:
2060eb1d1641SHerbert Xu 	spin_unlock_bh(&br->multicast_lock);
2061eb1d1641SHerbert Xu }
20620909e117SHerbert Xu 
20630909e117SHerbert Xu int br_multicast_set_router(struct net_bridge *br, unsigned long val)
20640909e117SHerbert Xu {
20656ae4ae8eSLinus Lüssing 	int err = -EINVAL;
20660909e117SHerbert Xu 
20670909e117SHerbert Xu 	spin_lock_bh(&br->multicast_lock);
20680909e117SHerbert Xu 
20690909e117SHerbert Xu 	switch (val) {
20707f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_DISABLED:
20717f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_PERM:
207277041420SYotam Gigi 		br_mc_router_state_change(br, val == MDB_RTR_TYPE_PERM);
20730909e117SHerbert Xu 		del_timer(&br->multicast_router_timer);
207477041420SYotam Gigi 		br->multicast_router = val;
207577041420SYotam Gigi 		err = 0;
207677041420SYotam Gigi 		break;
20777f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP_QUERY:
207877041420SYotam Gigi 		if (br->multicast_router != MDB_RTR_TYPE_TEMP_QUERY)
207977041420SYotam Gigi 			br_mc_router_state_change(br, false);
20800909e117SHerbert Xu 		br->multicast_router = val;
20810909e117SHerbert Xu 		err = 0;
20820909e117SHerbert Xu 		break;
20830909e117SHerbert Xu 	}
20840909e117SHerbert Xu 
20850909e117SHerbert Xu 	spin_unlock_bh(&br->multicast_lock);
20860909e117SHerbert Xu 
20870909e117SHerbert Xu 	return err;
20880909e117SHerbert Xu }
20890909e117SHerbert Xu 
20907f0aec7aSNikolay Aleksandrov static void __del_port_router(struct net_bridge_port *p)
20917f0aec7aSNikolay Aleksandrov {
20927f0aec7aSNikolay Aleksandrov 	if (hlist_unhashed(&p->rlist))
20937f0aec7aSNikolay Aleksandrov 		return;
20947f0aec7aSNikolay Aleksandrov 	hlist_del_init_rcu(&p->rlist);
20957f0aec7aSNikolay Aleksandrov 	br_rtr_notify(p->br->dev, p, RTM_DELMDB);
20966d549648SNogah Frankel 	br_port_mc_router_state_change(p, false);
2097f12e7d95SNogah Frankel 
2098f12e7d95SNogah Frankel 	/* don't allow timer refresh */
2099f12e7d95SNogah Frankel 	if (p->multicast_router == MDB_RTR_TYPE_TEMP)
2100f12e7d95SNogah Frankel 		p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
21017f0aec7aSNikolay Aleksandrov }
21027f0aec7aSNikolay Aleksandrov 
21030909e117SHerbert Xu int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
21040909e117SHerbert Xu {
21050909e117SHerbert Xu 	struct net_bridge *br = p->br;
2106a55d8246SNikolay Aleksandrov 	unsigned long now = jiffies;
21076ae4ae8eSLinus Lüssing 	int err = -EINVAL;
21080909e117SHerbert Xu 
21090909e117SHerbert Xu 	spin_lock(&br->multicast_lock);
21104950cfd1SNikolay Aleksandrov 	if (p->multicast_router == val) {
2111a55d8246SNikolay Aleksandrov 		/* Refresh the temp router port timer */
2112a55d8246SNikolay Aleksandrov 		if (p->multicast_router == MDB_RTR_TYPE_TEMP)
2113a55d8246SNikolay Aleksandrov 			mod_timer(&p->multicast_router_timer,
2114a55d8246SNikolay Aleksandrov 				  now + br->multicast_querier_interval);
21154950cfd1SNikolay Aleksandrov 		err = 0;
21164950cfd1SNikolay Aleksandrov 		goto unlock;
21174950cfd1SNikolay Aleksandrov 	}
21180909e117SHerbert Xu 	switch (val) {
21197f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_DISABLED:
21207f0aec7aSNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_DISABLED;
21217f0aec7aSNikolay Aleksandrov 		__del_port_router(p);
21220909e117SHerbert Xu 		del_timer(&p->multicast_router_timer);
21230909e117SHerbert Xu 		break;
21247f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP_QUERY:
21257f0aec7aSNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
21267f0aec7aSNikolay Aleksandrov 		__del_port_router(p);
21277f0aec7aSNikolay Aleksandrov 		break;
21287f0aec7aSNikolay Aleksandrov 	case MDB_RTR_TYPE_PERM:
21297f0aec7aSNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_PERM;
21307f0aec7aSNikolay Aleksandrov 		del_timer(&p->multicast_router_timer);
21310909e117SHerbert Xu 		br_multicast_add_router(br, p);
21320909e117SHerbert Xu 		break;
2133a55d8246SNikolay Aleksandrov 	case MDB_RTR_TYPE_TEMP:
2134a55d8246SNikolay Aleksandrov 		p->multicast_router = MDB_RTR_TYPE_TEMP;
2135a55d8246SNikolay Aleksandrov 		br_multicast_mark_router(br, p);
2136a55d8246SNikolay Aleksandrov 		break;
21377f0aec7aSNikolay Aleksandrov 	default:
21387f0aec7aSNikolay Aleksandrov 		goto unlock;
21390909e117SHerbert Xu 	}
21407f0aec7aSNikolay Aleksandrov 	err = 0;
21417f0aec7aSNikolay Aleksandrov unlock:
21420909e117SHerbert Xu 	spin_unlock(&br->multicast_lock);
21430909e117SHerbert Xu 
21440909e117SHerbert Xu 	return err;
21450909e117SHerbert Xu }
2146561f1103SHerbert Xu 
2147cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br,
214890010b36SLinus Lüssing 				       struct bridge_mcast_own_query *query)
2149561f1103SHerbert Xu {
2150561f1103SHerbert Xu 	struct net_bridge_port *port;
215174857216SHerbert Xu 
2152cc0fdd80SLinus Lüssing 	__br_multicast_open(br, query);
215374857216SHerbert Xu 
215474857216SHerbert Xu 	list_for_each_entry(port, &br->port_list, list) {
215574857216SHerbert Xu 		if (port->state == BR_STATE_DISABLED ||
215674857216SHerbert Xu 		    port->state == BR_STATE_BLOCKING)
215774857216SHerbert Xu 			continue;
215874857216SHerbert Xu 
215990010b36SLinus Lüssing 		if (query == &br->ip4_own_query)
216090010b36SLinus Lüssing 			br_multicast_enable(&port->ip4_own_query);
2161cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
2162cc0fdd80SLinus Lüssing 		else
216390010b36SLinus Lüssing 			br_multicast_enable(&port->ip6_own_query);
2164cc0fdd80SLinus Lüssing #endif
216574857216SHerbert Xu 	}
216674857216SHerbert Xu }
216774857216SHerbert Xu 
216874857216SHerbert Xu int br_multicast_toggle(struct net_bridge *br, unsigned long val)
216974857216SHerbert Xu {
2170e8051688SEric Dumazet 	struct net_bridge_mdb_htable *mdb;
21717cb3f921SNikolay Aleksandrov 	struct net_bridge_port *port;
21727cb3f921SNikolay Aleksandrov 	int err = 0;
2173561f1103SHerbert Xu 
2174ef5e0d82SAndrey Vagin 	spin_lock_bh(&br->multicast_lock);
2175561f1103SHerbert Xu 	if (br->multicast_disabled == !val)
2176561f1103SHerbert Xu 		goto unlock;
2177561f1103SHerbert Xu 
2178147c1e9bSNogah Frankel 	br_mc_disabled_update(br->dev, !val);
2179561f1103SHerbert Xu 	br->multicast_disabled = !val;
2180561f1103SHerbert Xu 	if (br->multicast_disabled)
2181561f1103SHerbert Xu 		goto unlock;
2182561f1103SHerbert Xu 
21833a7fda06SHerbert Xu 	if (!netif_running(br->dev))
21843a7fda06SHerbert Xu 		goto unlock;
21853a7fda06SHerbert Xu 
2186e8051688SEric Dumazet 	mdb = mlock_dereference(br->mdb, br);
2187e8051688SEric Dumazet 	if (mdb) {
2188e8051688SEric Dumazet 		if (mdb->old) {
2189561f1103SHerbert Xu 			err = -EEXIST;
2190561f1103SHerbert Xu rollback:
2191561f1103SHerbert Xu 			br->multicast_disabled = !!val;
2192561f1103SHerbert Xu 			goto unlock;
2193561f1103SHerbert Xu 		}
2194561f1103SHerbert Xu 
2195e8051688SEric Dumazet 		err = br_mdb_rehash(&br->mdb, mdb->max,
2196561f1103SHerbert Xu 				    br->hash_elasticity);
2197561f1103SHerbert Xu 		if (err)
2198561f1103SHerbert Xu 			goto rollback;
2199561f1103SHerbert Xu 	}
2200561f1103SHerbert Xu 
22017cb3f921SNikolay Aleksandrov 	br_multicast_open(br);
22027cb3f921SNikolay Aleksandrov 	list_for_each_entry(port, &br->port_list, list)
22037cb3f921SNikolay Aleksandrov 		__br_multicast_enable_port(port);
2204561f1103SHerbert Xu 
2205561f1103SHerbert Xu unlock:
2206ef5e0d82SAndrey Vagin 	spin_unlock_bh(&br->multicast_lock);
2207561f1103SHerbert Xu 
2208561f1103SHerbert Xu 	return err;
2209561f1103SHerbert Xu }
2210b195167fSHerbert Xu 
22119341b988SIdo Schimmel bool br_multicast_enabled(const struct net_device *dev)
22129341b988SIdo Schimmel {
22139341b988SIdo Schimmel 	struct net_bridge *br = netdev_priv(dev);
22149341b988SIdo Schimmel 
22159341b988SIdo Schimmel 	return !br->multicast_disabled;
22169341b988SIdo Schimmel }
22179341b988SIdo Schimmel EXPORT_SYMBOL_GPL(br_multicast_enabled);
22189341b988SIdo Schimmel 
2219*0912bda4SYotam Gigi bool br_multicast_router(const struct net_device *dev)
2220*0912bda4SYotam Gigi {
2221*0912bda4SYotam Gigi 	struct net_bridge *br = netdev_priv(dev);
2222*0912bda4SYotam Gigi 	bool is_router;
2223*0912bda4SYotam Gigi 
2224*0912bda4SYotam Gigi 	spin_lock_bh(&br->multicast_lock);
2225*0912bda4SYotam Gigi 	is_router = br_multicast_is_router(br);
2226*0912bda4SYotam Gigi 	spin_unlock_bh(&br->multicast_lock);
2227*0912bda4SYotam Gigi 	return is_router;
2228*0912bda4SYotam Gigi }
2229*0912bda4SYotam Gigi EXPORT_SYMBOL_GPL(br_multicast_router);
2230*0912bda4SYotam Gigi 
2231c5c23260SHerbert Xu int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
2232c5c23260SHerbert Xu {
2233b00589afSLinus Lüssing 	unsigned long max_delay;
2234b00589afSLinus Lüssing 
2235c5c23260SHerbert Xu 	val = !!val;
2236c5c23260SHerbert Xu 
2237c5c23260SHerbert Xu 	spin_lock_bh(&br->multicast_lock);
2238c5c23260SHerbert Xu 	if (br->multicast_querier == val)
2239c5c23260SHerbert Xu 		goto unlock;
2240c5c23260SHerbert Xu 
2241c5c23260SHerbert Xu 	br->multicast_querier = val;
2242b00589afSLinus Lüssing 	if (!val)
2243b00589afSLinus Lüssing 		goto unlock;
2244b00589afSLinus Lüssing 
2245b00589afSLinus Lüssing 	max_delay = br->multicast_query_response_interval;
2246b00589afSLinus Lüssing 
224790010b36SLinus Lüssing 	if (!timer_pending(&br->ip4_other_query.timer))
224890010b36SLinus Lüssing 		br->ip4_other_query.delay_time = jiffies + max_delay;
2249cc0fdd80SLinus Lüssing 
225090010b36SLinus Lüssing 	br_multicast_start_querier(br, &br->ip4_own_query);
2251cc0fdd80SLinus Lüssing 
2252cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
225390010b36SLinus Lüssing 	if (!timer_pending(&br->ip6_other_query.timer))
225490010b36SLinus Lüssing 		br->ip6_other_query.delay_time = jiffies + max_delay;
2255cc0fdd80SLinus Lüssing 
225690010b36SLinus Lüssing 	br_multicast_start_querier(br, &br->ip6_own_query);
2257cc0fdd80SLinus Lüssing #endif
2258c5c23260SHerbert Xu 
2259c5c23260SHerbert Xu unlock:
2260c5c23260SHerbert Xu 	spin_unlock_bh(&br->multicast_lock);
2261c5c23260SHerbert Xu 
2262c5c23260SHerbert Xu 	return 0;
2263c5c23260SHerbert Xu }
2264c5c23260SHerbert Xu 
2265b195167fSHerbert Xu int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
2266b195167fSHerbert Xu {
22676ae4ae8eSLinus Lüssing 	int err = -EINVAL;
2268b195167fSHerbert Xu 	u32 old;
2269e8051688SEric Dumazet 	struct net_bridge_mdb_htable *mdb;
2270b195167fSHerbert Xu 
2271fe0d692bSCurt Brune 	spin_lock_bh(&br->multicast_lock);
2272b195167fSHerbert Xu 	if (!is_power_of_2(val))
2273b195167fSHerbert Xu 		goto unlock;
2274e8051688SEric Dumazet 
2275e8051688SEric Dumazet 	mdb = mlock_dereference(br->mdb, br);
2276e8051688SEric Dumazet 	if (mdb && val < mdb->size)
2277b195167fSHerbert Xu 		goto unlock;
2278b195167fSHerbert Xu 
2279b195167fSHerbert Xu 	err = 0;
2280b195167fSHerbert Xu 
2281b195167fSHerbert Xu 	old = br->hash_max;
2282b195167fSHerbert Xu 	br->hash_max = val;
2283b195167fSHerbert Xu 
2284e8051688SEric Dumazet 	if (mdb) {
2285e8051688SEric Dumazet 		if (mdb->old) {
2286b195167fSHerbert Xu 			err = -EEXIST;
2287b195167fSHerbert Xu rollback:
2288b195167fSHerbert Xu 			br->hash_max = old;
2289b195167fSHerbert Xu 			goto unlock;
2290b195167fSHerbert Xu 		}
2291b195167fSHerbert Xu 
2292b195167fSHerbert Xu 		err = br_mdb_rehash(&br->mdb, br->hash_max,
2293b195167fSHerbert Xu 				    br->hash_elasticity);
2294b195167fSHerbert Xu 		if (err)
2295b195167fSHerbert Xu 			goto rollback;
2296b195167fSHerbert Xu 	}
2297b195167fSHerbert Xu 
2298b195167fSHerbert Xu unlock:
2299fe0d692bSCurt Brune 	spin_unlock_bh(&br->multicast_lock);
2300b195167fSHerbert Xu 
2301b195167fSHerbert Xu 	return err;
2302b195167fSHerbert Xu }
230307f8ac4aSLinus Lüssing 
23045e923585SNikolay Aleksandrov int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val)
23055e923585SNikolay Aleksandrov {
23065e923585SNikolay Aleksandrov 	/* Currently we support only version 2 and 3 */
23075e923585SNikolay Aleksandrov 	switch (val) {
23085e923585SNikolay Aleksandrov 	case 2:
23095e923585SNikolay Aleksandrov 	case 3:
23105e923585SNikolay Aleksandrov 		break;
23115e923585SNikolay Aleksandrov 	default:
23125e923585SNikolay Aleksandrov 		return -EINVAL;
23135e923585SNikolay Aleksandrov 	}
23145e923585SNikolay Aleksandrov 
23155e923585SNikolay Aleksandrov 	spin_lock_bh(&br->multicast_lock);
23165e923585SNikolay Aleksandrov 	br->multicast_igmp_version = val;
23175e923585SNikolay Aleksandrov 	spin_unlock_bh(&br->multicast_lock);
23185e923585SNikolay Aleksandrov 
23195e923585SNikolay Aleksandrov 	return 0;
23205e923585SNikolay Aleksandrov }
23215e923585SNikolay Aleksandrov 
2322aa2ae3e7SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
2323aa2ae3e7SNikolay Aleksandrov int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val)
2324aa2ae3e7SNikolay Aleksandrov {
2325aa2ae3e7SNikolay Aleksandrov 	/* Currently we support version 1 and 2 */
2326aa2ae3e7SNikolay Aleksandrov 	switch (val) {
2327aa2ae3e7SNikolay Aleksandrov 	case 1:
2328aa2ae3e7SNikolay Aleksandrov 	case 2:
2329aa2ae3e7SNikolay Aleksandrov 		break;
2330aa2ae3e7SNikolay Aleksandrov 	default:
2331aa2ae3e7SNikolay Aleksandrov 		return -EINVAL;
2332aa2ae3e7SNikolay Aleksandrov 	}
2333aa2ae3e7SNikolay Aleksandrov 
2334aa2ae3e7SNikolay Aleksandrov 	spin_lock_bh(&br->multicast_lock);
2335aa2ae3e7SNikolay Aleksandrov 	br->multicast_mld_version = val;
2336aa2ae3e7SNikolay Aleksandrov 	spin_unlock_bh(&br->multicast_lock);
2337aa2ae3e7SNikolay Aleksandrov 
2338aa2ae3e7SNikolay Aleksandrov 	return 0;
2339aa2ae3e7SNikolay Aleksandrov }
2340aa2ae3e7SNikolay Aleksandrov #endif
2341aa2ae3e7SNikolay Aleksandrov 
234207f8ac4aSLinus Lüssing /**
234307f8ac4aSLinus Lüssing  * br_multicast_list_adjacent - Returns snooped multicast addresses
234407f8ac4aSLinus Lüssing  * @dev:	The bridge port adjacent to which to retrieve addresses
234507f8ac4aSLinus Lüssing  * @br_ip_list:	The list to store found, snooped multicast IP addresses in
234607f8ac4aSLinus Lüssing  *
234707f8ac4aSLinus Lüssing  * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
234807f8ac4aSLinus Lüssing  * snooping feature on all bridge ports of dev's bridge device, excluding
234907f8ac4aSLinus Lüssing  * the addresses from dev itself.
235007f8ac4aSLinus Lüssing  *
235107f8ac4aSLinus Lüssing  * Returns the number of items added to br_ip_list.
235207f8ac4aSLinus Lüssing  *
235307f8ac4aSLinus Lüssing  * Notes:
235407f8ac4aSLinus Lüssing  * - br_ip_list needs to be initialized by caller
235507f8ac4aSLinus Lüssing  * - br_ip_list might contain duplicates in the end
235607f8ac4aSLinus Lüssing  *   (needs to be taken care of by caller)
235707f8ac4aSLinus Lüssing  * - br_ip_list needs to be freed by caller
235807f8ac4aSLinus Lüssing  */
235907f8ac4aSLinus Lüssing int br_multicast_list_adjacent(struct net_device *dev,
236007f8ac4aSLinus Lüssing 			       struct list_head *br_ip_list)
236107f8ac4aSLinus Lüssing {
236207f8ac4aSLinus Lüssing 	struct net_bridge *br;
236307f8ac4aSLinus Lüssing 	struct net_bridge_port *port;
236407f8ac4aSLinus Lüssing 	struct net_bridge_port_group *group;
236507f8ac4aSLinus Lüssing 	struct br_ip_list *entry;
236607f8ac4aSLinus Lüssing 	int count = 0;
236707f8ac4aSLinus Lüssing 
236807f8ac4aSLinus Lüssing 	rcu_read_lock();
236907f8ac4aSLinus Lüssing 	if (!br_ip_list || !br_port_exists(dev))
237007f8ac4aSLinus Lüssing 		goto unlock;
237107f8ac4aSLinus Lüssing 
237207f8ac4aSLinus Lüssing 	port = br_port_get_rcu(dev);
237307f8ac4aSLinus Lüssing 	if (!port || !port->br)
237407f8ac4aSLinus Lüssing 		goto unlock;
237507f8ac4aSLinus Lüssing 
237607f8ac4aSLinus Lüssing 	br = port->br;
237707f8ac4aSLinus Lüssing 
237807f8ac4aSLinus Lüssing 	list_for_each_entry_rcu(port, &br->port_list, list) {
237907f8ac4aSLinus Lüssing 		if (!port->dev || port->dev == dev)
238007f8ac4aSLinus Lüssing 			continue;
238107f8ac4aSLinus Lüssing 
238207f8ac4aSLinus Lüssing 		hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
238307f8ac4aSLinus Lüssing 			entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
238407f8ac4aSLinus Lüssing 			if (!entry)
238507f8ac4aSLinus Lüssing 				goto unlock;
238607f8ac4aSLinus Lüssing 
238707f8ac4aSLinus Lüssing 			entry->addr = group->addr;
238807f8ac4aSLinus Lüssing 			list_add(&entry->list, br_ip_list);
238907f8ac4aSLinus Lüssing 			count++;
239007f8ac4aSLinus Lüssing 		}
239107f8ac4aSLinus Lüssing 	}
239207f8ac4aSLinus Lüssing 
239307f8ac4aSLinus Lüssing unlock:
239407f8ac4aSLinus Lüssing 	rcu_read_unlock();
239507f8ac4aSLinus Lüssing 	return count;
239607f8ac4aSLinus Lüssing }
239707f8ac4aSLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
23982cd41431SLinus Lüssing 
23992cd41431SLinus Lüssing /**
2400c34963e2SLinus Lüssing  * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2401c34963e2SLinus Lüssing  * @dev: The bridge port providing the bridge on which to check for a querier
2402c34963e2SLinus Lüssing  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2403c34963e2SLinus Lüssing  *
2404c34963e2SLinus Lüssing  * Checks whether the given interface has a bridge on top and if so returns
2405c34963e2SLinus Lüssing  * true if a valid querier exists anywhere on the bridged link layer.
2406c34963e2SLinus Lüssing  * Otherwise returns false.
2407c34963e2SLinus Lüssing  */
2408c34963e2SLinus Lüssing bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
2409c34963e2SLinus Lüssing {
2410c34963e2SLinus Lüssing 	struct net_bridge *br;
2411c34963e2SLinus Lüssing 	struct net_bridge_port *port;
2412c34963e2SLinus Lüssing 	struct ethhdr eth;
2413c34963e2SLinus Lüssing 	bool ret = false;
2414c34963e2SLinus Lüssing 
2415c34963e2SLinus Lüssing 	rcu_read_lock();
2416c34963e2SLinus Lüssing 	if (!br_port_exists(dev))
2417c34963e2SLinus Lüssing 		goto unlock;
2418c34963e2SLinus Lüssing 
2419c34963e2SLinus Lüssing 	port = br_port_get_rcu(dev);
2420c34963e2SLinus Lüssing 	if (!port || !port->br)
2421c34963e2SLinus Lüssing 		goto unlock;
2422c34963e2SLinus Lüssing 
2423c34963e2SLinus Lüssing 	br = port->br;
2424c34963e2SLinus Lüssing 
2425c34963e2SLinus Lüssing 	memset(&eth, 0, sizeof(eth));
2426c34963e2SLinus Lüssing 	eth.h_proto = htons(proto);
2427c34963e2SLinus Lüssing 
2428c34963e2SLinus Lüssing 	ret = br_multicast_querier_exists(br, &eth);
2429c34963e2SLinus Lüssing 
2430c34963e2SLinus Lüssing unlock:
2431c34963e2SLinus Lüssing 	rcu_read_unlock();
2432c34963e2SLinus Lüssing 	return ret;
2433c34963e2SLinus Lüssing }
2434c34963e2SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
2435c34963e2SLinus Lüssing 
2436c34963e2SLinus Lüssing /**
24372cd41431SLinus Lüssing  * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
24382cd41431SLinus Lüssing  * @dev: The bridge port adjacent to which to check for a querier
24392cd41431SLinus Lüssing  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
24402cd41431SLinus Lüssing  *
24412cd41431SLinus Lüssing  * Checks whether the given interface has a bridge on top and if so returns
24422cd41431SLinus Lüssing  * true if a selected querier is behind one of the other ports of this
24432cd41431SLinus Lüssing  * bridge. Otherwise returns false.
24442cd41431SLinus Lüssing  */
24452cd41431SLinus Lüssing bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
24462cd41431SLinus Lüssing {
24472cd41431SLinus Lüssing 	struct net_bridge *br;
24482cd41431SLinus Lüssing 	struct net_bridge_port *port;
24492cd41431SLinus Lüssing 	bool ret = false;
24502cd41431SLinus Lüssing 
24512cd41431SLinus Lüssing 	rcu_read_lock();
24522cd41431SLinus Lüssing 	if (!br_port_exists(dev))
24532cd41431SLinus Lüssing 		goto unlock;
24542cd41431SLinus Lüssing 
24552cd41431SLinus Lüssing 	port = br_port_get_rcu(dev);
24562cd41431SLinus Lüssing 	if (!port || !port->br)
24572cd41431SLinus Lüssing 		goto unlock;
24582cd41431SLinus Lüssing 
24592cd41431SLinus Lüssing 	br = port->br;
24602cd41431SLinus Lüssing 
24612cd41431SLinus Lüssing 	switch (proto) {
24622cd41431SLinus Lüssing 	case ETH_P_IP:
24632cd41431SLinus Lüssing 		if (!timer_pending(&br->ip4_other_query.timer) ||
24642cd41431SLinus Lüssing 		    rcu_dereference(br->ip4_querier.port) == port)
24652cd41431SLinus Lüssing 			goto unlock;
24662cd41431SLinus Lüssing 		break;
24673993c4e1SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6)
24682cd41431SLinus Lüssing 	case ETH_P_IPV6:
24692cd41431SLinus Lüssing 		if (!timer_pending(&br->ip6_other_query.timer) ||
24702cd41431SLinus Lüssing 		    rcu_dereference(br->ip6_querier.port) == port)
24712cd41431SLinus Lüssing 			goto unlock;
24722cd41431SLinus Lüssing 		break;
24733993c4e1SLinus Lüssing #endif
24742cd41431SLinus Lüssing 	default:
24752cd41431SLinus Lüssing 		goto unlock;
24762cd41431SLinus Lüssing 	}
24772cd41431SLinus Lüssing 
24782cd41431SLinus Lüssing 	ret = true;
24792cd41431SLinus Lüssing unlock:
24802cd41431SLinus Lüssing 	rcu_read_unlock();
24812cd41431SLinus Lüssing 	return ret;
24822cd41431SLinus Lüssing }
24832cd41431SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
24841080ab95SNikolay Aleksandrov 
24851080ab95SNikolay Aleksandrov static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
2486a65056ecSNikolay Aleksandrov 			       const struct sk_buff *skb, u8 type, u8 dir)
24871080ab95SNikolay Aleksandrov {
24881080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
2489a65056ecSNikolay Aleksandrov 	__be16 proto = skb->protocol;
2490a65056ecSNikolay Aleksandrov 	unsigned int t_len;
24911080ab95SNikolay Aleksandrov 
24921080ab95SNikolay Aleksandrov 	u64_stats_update_begin(&pstats->syncp);
24931080ab95SNikolay Aleksandrov 	switch (proto) {
24941080ab95SNikolay Aleksandrov 	case htons(ETH_P_IP):
2495a65056ecSNikolay Aleksandrov 		t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
24961080ab95SNikolay Aleksandrov 		switch (type) {
24971080ab95SNikolay Aleksandrov 		case IGMP_HOST_MEMBERSHIP_REPORT:
24981080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v1reports[dir]++;
24991080ab95SNikolay Aleksandrov 			break;
25001080ab95SNikolay Aleksandrov 		case IGMPV2_HOST_MEMBERSHIP_REPORT:
25011080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v2reports[dir]++;
25021080ab95SNikolay Aleksandrov 			break;
25031080ab95SNikolay Aleksandrov 		case IGMPV3_HOST_MEMBERSHIP_REPORT:
25041080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_v3reports[dir]++;
25051080ab95SNikolay Aleksandrov 			break;
25061080ab95SNikolay Aleksandrov 		case IGMP_HOST_MEMBERSHIP_QUERY:
2507a65056ecSNikolay Aleksandrov 			if (t_len != sizeof(struct igmphdr)) {
2508a65056ecSNikolay Aleksandrov 				pstats->mstats.igmp_v3queries[dir]++;
2509a65056ecSNikolay Aleksandrov 			} else {
2510a65056ecSNikolay Aleksandrov 				unsigned int offset = skb_transport_offset(skb);
2511a65056ecSNikolay Aleksandrov 				struct igmphdr *ih, _ihdr;
2512a65056ecSNikolay Aleksandrov 
2513a65056ecSNikolay Aleksandrov 				ih = skb_header_pointer(skb, offset,
2514a65056ecSNikolay Aleksandrov 							sizeof(_ihdr), &_ihdr);
2515a65056ecSNikolay Aleksandrov 				if (!ih)
2516a65056ecSNikolay Aleksandrov 					break;
2517a65056ecSNikolay Aleksandrov 				if (!ih->code)
2518a65056ecSNikolay Aleksandrov 					pstats->mstats.igmp_v1queries[dir]++;
2519a65056ecSNikolay Aleksandrov 				else
2520a65056ecSNikolay Aleksandrov 					pstats->mstats.igmp_v2queries[dir]++;
2521a65056ecSNikolay Aleksandrov 			}
25221080ab95SNikolay Aleksandrov 			break;
25231080ab95SNikolay Aleksandrov 		case IGMP_HOST_LEAVE_MESSAGE:
25241080ab95SNikolay Aleksandrov 			pstats->mstats.igmp_leaves[dir]++;
25251080ab95SNikolay Aleksandrov 			break;
25261080ab95SNikolay Aleksandrov 		}
25271080ab95SNikolay Aleksandrov 		break;
25281080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6)
25291080ab95SNikolay Aleksandrov 	case htons(ETH_P_IPV6):
2530a65056ecSNikolay Aleksandrov 		t_len = ntohs(ipv6_hdr(skb)->payload_len) +
2531a65056ecSNikolay Aleksandrov 			sizeof(struct ipv6hdr);
2532a65056ecSNikolay Aleksandrov 		t_len -= skb_network_header_len(skb);
25331080ab95SNikolay Aleksandrov 		switch (type) {
25341080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_REPORT:
25351080ab95SNikolay Aleksandrov 			pstats->mstats.mld_v1reports[dir]++;
25361080ab95SNikolay Aleksandrov 			break;
25371080ab95SNikolay Aleksandrov 		case ICMPV6_MLD2_REPORT:
25381080ab95SNikolay Aleksandrov 			pstats->mstats.mld_v2reports[dir]++;
25391080ab95SNikolay Aleksandrov 			break;
25401080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_QUERY:
2541a65056ecSNikolay Aleksandrov 			if (t_len != sizeof(struct mld_msg))
2542a65056ecSNikolay Aleksandrov 				pstats->mstats.mld_v2queries[dir]++;
2543a65056ecSNikolay Aleksandrov 			else
2544a65056ecSNikolay Aleksandrov 				pstats->mstats.mld_v1queries[dir]++;
25451080ab95SNikolay Aleksandrov 			break;
25461080ab95SNikolay Aleksandrov 		case ICMPV6_MGM_REDUCTION:
25471080ab95SNikolay Aleksandrov 			pstats->mstats.mld_leaves[dir]++;
25481080ab95SNikolay Aleksandrov 			break;
25491080ab95SNikolay Aleksandrov 		}
25501080ab95SNikolay Aleksandrov 		break;
25511080ab95SNikolay Aleksandrov #endif /* CONFIG_IPV6 */
25521080ab95SNikolay Aleksandrov 	}
25531080ab95SNikolay Aleksandrov 	u64_stats_update_end(&pstats->syncp);
25541080ab95SNikolay Aleksandrov }
25551080ab95SNikolay Aleksandrov 
25561080ab95SNikolay Aleksandrov void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
2557a65056ecSNikolay Aleksandrov 			const struct sk_buff *skb, u8 type, u8 dir)
25581080ab95SNikolay Aleksandrov {
25591080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
25601080ab95SNikolay Aleksandrov 
25611080ab95SNikolay Aleksandrov 	/* if multicast_disabled is true then igmp type can't be set */
25621080ab95SNikolay Aleksandrov 	if (!type || !br->multicast_stats_enabled)
25631080ab95SNikolay Aleksandrov 		return;
25641080ab95SNikolay Aleksandrov 
25651080ab95SNikolay Aleksandrov 	if (p)
25661080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
25671080ab95SNikolay Aleksandrov 	else
25681080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
25691080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
25701080ab95SNikolay Aleksandrov 		return;
25711080ab95SNikolay Aleksandrov 
2572a65056ecSNikolay Aleksandrov 	br_mcast_stats_add(stats, skb, type, dir);
25731080ab95SNikolay Aleksandrov }
25741080ab95SNikolay Aleksandrov 
25751080ab95SNikolay Aleksandrov int br_multicast_init_stats(struct net_bridge *br)
25761080ab95SNikolay Aleksandrov {
25771080ab95SNikolay Aleksandrov 	br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
25781080ab95SNikolay Aleksandrov 	if (!br->mcast_stats)
25791080ab95SNikolay Aleksandrov 		return -ENOMEM;
25801080ab95SNikolay Aleksandrov 
25811080ab95SNikolay Aleksandrov 	return 0;
25821080ab95SNikolay Aleksandrov }
25831080ab95SNikolay Aleksandrov 
2584b6fe0440SIdo Schimmel void br_multicast_uninit_stats(struct net_bridge *br)
2585b6fe0440SIdo Schimmel {
2586b6fe0440SIdo Schimmel 	free_percpu(br->mcast_stats);
2587b6fe0440SIdo Schimmel }
2588b6fe0440SIdo Schimmel 
25891080ab95SNikolay Aleksandrov static void mcast_stats_add_dir(u64 *dst, u64 *src)
25901080ab95SNikolay Aleksandrov {
25911080ab95SNikolay Aleksandrov 	dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
25921080ab95SNikolay Aleksandrov 	dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
25931080ab95SNikolay Aleksandrov }
25941080ab95SNikolay Aleksandrov 
25951080ab95SNikolay Aleksandrov void br_multicast_get_stats(const struct net_bridge *br,
25961080ab95SNikolay Aleksandrov 			    const struct net_bridge_port *p,
25971080ab95SNikolay Aleksandrov 			    struct br_mcast_stats *dest)
25981080ab95SNikolay Aleksandrov {
25991080ab95SNikolay Aleksandrov 	struct bridge_mcast_stats __percpu *stats;
26001080ab95SNikolay Aleksandrov 	struct br_mcast_stats tdst;
26011080ab95SNikolay Aleksandrov 	int i;
26021080ab95SNikolay Aleksandrov 
26031080ab95SNikolay Aleksandrov 	memset(dest, 0, sizeof(*dest));
26041080ab95SNikolay Aleksandrov 	if (p)
26051080ab95SNikolay Aleksandrov 		stats = p->mcast_stats;
26061080ab95SNikolay Aleksandrov 	else
26071080ab95SNikolay Aleksandrov 		stats = br->mcast_stats;
26081080ab95SNikolay Aleksandrov 	if (WARN_ON(!stats))
26091080ab95SNikolay Aleksandrov 		return;
26101080ab95SNikolay Aleksandrov 
26111080ab95SNikolay Aleksandrov 	memset(&tdst, 0, sizeof(tdst));
26121080ab95SNikolay Aleksandrov 	for_each_possible_cpu(i) {
26131080ab95SNikolay Aleksandrov 		struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
26141080ab95SNikolay Aleksandrov 		struct br_mcast_stats temp;
26151080ab95SNikolay Aleksandrov 		unsigned int start;
26161080ab95SNikolay Aleksandrov 
26171080ab95SNikolay Aleksandrov 		do {
26181080ab95SNikolay Aleksandrov 			start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
26191080ab95SNikolay Aleksandrov 			memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
26201080ab95SNikolay Aleksandrov 		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
26211080ab95SNikolay Aleksandrov 
2622a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
2623a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
2624a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
26251080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
26261080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
26271080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
26281080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
26291080ab95SNikolay Aleksandrov 		tdst.igmp_parse_errors += temp.igmp_parse_errors;
26301080ab95SNikolay Aleksandrov 
2631a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
2632a65056ecSNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
26331080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
26341080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
26351080ab95SNikolay Aleksandrov 		mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
26361080ab95SNikolay Aleksandrov 		tdst.mld_parse_errors += temp.mld_parse_errors;
26371080ab95SNikolay Aleksandrov 	}
26381080ab95SNikolay Aleksandrov 	memcpy(dest, &tdst, sizeof(*dest));
26391080ab95SNikolay Aleksandrov }
2640