xref: /openbmc/linux/net/bridge/br_multicast.c (revision c16c6655)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Bridge multicast support.
4  *
5  * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
6  */
7 
8 #include <linux/err.h>
9 #include <linux/export.h>
10 #include <linux/if_ether.h>
11 #include <linux/igmp.h>
12 #include <linux/in.h>
13 #include <linux/jhash.h>
14 #include <linux/kernel.h>
15 #include <linux/log2.h>
16 #include <linux/netdevice.h>
17 #include <linux/netfilter_bridge.h>
18 #include <linux/random.h>
19 #include <linux/rculist.h>
20 #include <linux/skbuff.h>
21 #include <linux/slab.h>
22 #include <linux/timer.h>
23 #include <linux/inetdevice.h>
24 #include <linux/mroute.h>
25 #include <net/ip.h>
26 #include <net/switchdev.h>
27 #if IS_ENABLED(CONFIG_IPV6)
28 #include <linux/icmpv6.h>
29 #include <net/ipv6.h>
30 #include <net/mld.h>
31 #include <net/ip6_checksum.h>
32 #include <net/addrconf.h>
33 #endif
34 
35 #include "br_private.h"
36 
37 static const struct rhashtable_params br_mdb_rht_params = {
38 	.head_offset = offsetof(struct net_bridge_mdb_entry, rhnode),
39 	.key_offset = offsetof(struct net_bridge_mdb_entry, addr),
40 	.key_len = sizeof(struct br_ip),
41 	.automatic_shrinking = true,
42 };
43 
44 static void br_multicast_start_querier(struct net_bridge *br,
45 				       struct bridge_mcast_own_query *query);
46 static void br_multicast_add_router(struct net_bridge *br,
47 				    struct net_bridge_port *port);
48 static void br_ip4_multicast_leave_group(struct net_bridge *br,
49 					 struct net_bridge_port *port,
50 					 __be32 group,
51 					 __u16 vid,
52 					 const unsigned char *src);
53 
54 static void __del_port_router(struct net_bridge_port *p);
55 #if IS_ENABLED(CONFIG_IPV6)
56 static void br_ip6_multicast_leave_group(struct net_bridge *br,
57 					 struct net_bridge_port *port,
58 					 const struct in6_addr *group,
59 					 __u16 vid, const unsigned char *src);
60 #endif
61 
62 static struct net_bridge_mdb_entry *br_mdb_ip_get_rcu(struct net_bridge *br,
63 						      struct br_ip *dst)
64 {
65 	return rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params);
66 }
67 
68 struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge *br,
69 					   struct br_ip *dst)
70 {
71 	struct net_bridge_mdb_entry *ent;
72 
73 	lockdep_assert_held_once(&br->multicast_lock);
74 
75 	rcu_read_lock();
76 	ent = rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params);
77 	rcu_read_unlock();
78 
79 	return ent;
80 }
81 
82 static struct net_bridge_mdb_entry *br_mdb_ip4_get(struct net_bridge *br,
83 						   __be32 dst, __u16 vid)
84 {
85 	struct br_ip br_dst;
86 
87 	memset(&br_dst, 0, sizeof(br_dst));
88 	br_dst.u.ip4 = dst;
89 	br_dst.proto = htons(ETH_P_IP);
90 	br_dst.vid = vid;
91 
92 	return br_mdb_ip_get(br, &br_dst);
93 }
94 
95 #if IS_ENABLED(CONFIG_IPV6)
96 static struct net_bridge_mdb_entry *br_mdb_ip6_get(struct net_bridge *br,
97 						   const struct in6_addr *dst,
98 						   __u16 vid)
99 {
100 	struct br_ip br_dst;
101 
102 	memset(&br_dst, 0, sizeof(br_dst));
103 	br_dst.u.ip6 = *dst;
104 	br_dst.proto = htons(ETH_P_IPV6);
105 	br_dst.vid = vid;
106 
107 	return br_mdb_ip_get(br, &br_dst);
108 }
109 #endif
110 
111 struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
112 					struct sk_buff *skb, u16 vid)
113 {
114 	struct br_ip ip;
115 
116 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
117 		return NULL;
118 
119 	if (BR_INPUT_SKB_CB(skb)->igmp)
120 		return NULL;
121 
122 	memset(&ip, 0, sizeof(ip));
123 	ip.proto = skb->protocol;
124 	ip.vid = vid;
125 
126 	switch (skb->protocol) {
127 	case htons(ETH_P_IP):
128 		ip.u.ip4 = ip_hdr(skb)->daddr;
129 		break;
130 #if IS_ENABLED(CONFIG_IPV6)
131 	case htons(ETH_P_IPV6):
132 		ip.u.ip6 = ipv6_hdr(skb)->daddr;
133 		break;
134 #endif
135 	default:
136 		return NULL;
137 	}
138 
139 	return br_mdb_ip_get_rcu(br, &ip);
140 }
141 
142 static void br_multicast_group_expired(struct timer_list *t)
143 {
144 	struct net_bridge_mdb_entry *mp = from_timer(mp, t, timer);
145 	struct net_bridge *br = mp->br;
146 
147 	spin_lock(&br->multicast_lock);
148 	if (!netif_running(br->dev) || timer_pending(&mp->timer))
149 		goto out;
150 
151 	mp->host_joined = false;
152 	br_mdb_notify(br->dev, NULL, &mp->addr, RTM_DELMDB, 0);
153 
154 	if (mp->ports)
155 		goto out;
156 
157 	rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode,
158 			       br_mdb_rht_params);
159 	hlist_del_rcu(&mp->mdb_node);
160 
161 	kfree_rcu(mp, rcu);
162 
163 out:
164 	spin_unlock(&br->multicast_lock);
165 }
166 
167 static void br_multicast_del_pg(struct net_bridge *br,
168 				struct net_bridge_port_group *pg)
169 {
170 	struct net_bridge_mdb_entry *mp;
171 	struct net_bridge_port_group *p;
172 	struct net_bridge_port_group __rcu **pp;
173 
174 	mp = br_mdb_ip_get(br, &pg->addr);
175 	if (WARN_ON(!mp))
176 		return;
177 
178 	for (pp = &mp->ports;
179 	     (p = mlock_dereference(*pp, br)) != NULL;
180 	     pp = &p->next) {
181 		if (p != pg)
182 			continue;
183 
184 		rcu_assign_pointer(*pp, p->next);
185 		hlist_del_init(&p->mglist);
186 		del_timer(&p->timer);
187 		br_mdb_notify(br->dev, p->port, &pg->addr, RTM_DELMDB,
188 			      p->flags);
189 		kfree_rcu(p, rcu);
190 
191 		if (!mp->ports && !mp->host_joined &&
192 		    netif_running(br->dev))
193 			mod_timer(&mp->timer, jiffies);
194 
195 		return;
196 	}
197 
198 	WARN_ON(1);
199 }
200 
201 static void br_multicast_port_group_expired(struct timer_list *t)
202 {
203 	struct net_bridge_port_group *pg = from_timer(pg, t, timer);
204 	struct net_bridge *br = pg->port->br;
205 
206 	spin_lock(&br->multicast_lock);
207 	if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
208 	    hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT)
209 		goto out;
210 
211 	br_multicast_del_pg(br, pg);
212 
213 out:
214 	spin_unlock(&br->multicast_lock);
215 }
216 
217 static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
218 						    __be32 group,
219 						    u8 *igmp_type)
220 {
221 	struct igmpv3_query *ihv3;
222 	size_t igmp_hdr_size;
223 	struct sk_buff *skb;
224 	struct igmphdr *ih;
225 	struct ethhdr *eth;
226 	struct iphdr *iph;
227 
228 	igmp_hdr_size = sizeof(*ih);
229 	if (br->multicast_igmp_version == 3)
230 		igmp_hdr_size = sizeof(*ihv3);
231 	skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
232 						 igmp_hdr_size + 4);
233 	if (!skb)
234 		goto out;
235 
236 	skb->protocol = htons(ETH_P_IP);
237 
238 	skb_reset_mac_header(skb);
239 	eth = eth_hdr(skb);
240 
241 	ether_addr_copy(eth->h_source, br->dev->dev_addr);
242 	eth->h_dest[0] = 1;
243 	eth->h_dest[1] = 0;
244 	eth->h_dest[2] = 0x5e;
245 	eth->h_dest[3] = 0;
246 	eth->h_dest[4] = 0;
247 	eth->h_dest[5] = 1;
248 	eth->h_proto = htons(ETH_P_IP);
249 	skb_put(skb, sizeof(*eth));
250 
251 	skb_set_network_header(skb, skb->len);
252 	iph = ip_hdr(skb);
253 
254 	iph->version = 4;
255 	iph->ihl = 6;
256 	iph->tos = 0xc0;
257 	iph->tot_len = htons(sizeof(*iph) + igmp_hdr_size + 4);
258 	iph->id = 0;
259 	iph->frag_off = htons(IP_DF);
260 	iph->ttl = 1;
261 	iph->protocol = IPPROTO_IGMP;
262 	iph->saddr = br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR) ?
263 		     inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0;
264 	iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
265 	((u8 *)&iph[1])[0] = IPOPT_RA;
266 	((u8 *)&iph[1])[1] = 4;
267 	((u8 *)&iph[1])[2] = 0;
268 	((u8 *)&iph[1])[3] = 0;
269 	ip_send_check(iph);
270 	skb_put(skb, 24);
271 
272 	skb_set_transport_header(skb, skb->len);
273 	*igmp_type = IGMP_HOST_MEMBERSHIP_QUERY;
274 
275 	switch (br->multicast_igmp_version) {
276 	case 2:
277 		ih = igmp_hdr(skb);
278 		ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
279 		ih->code = (group ? br->multicast_last_member_interval :
280 				    br->multicast_query_response_interval) /
281 			   (HZ / IGMP_TIMER_SCALE);
282 		ih->group = group;
283 		ih->csum = 0;
284 		ih->csum = ip_compute_csum((void *)ih, sizeof(*ih));
285 		break;
286 	case 3:
287 		ihv3 = igmpv3_query_hdr(skb);
288 		ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY;
289 		ihv3->code = (group ? br->multicast_last_member_interval :
290 				      br->multicast_query_response_interval) /
291 			     (HZ / IGMP_TIMER_SCALE);
292 		ihv3->group = group;
293 		ihv3->qqic = br->multicast_query_interval / HZ;
294 		ihv3->nsrcs = 0;
295 		ihv3->resv = 0;
296 		ihv3->suppress = 0;
297 		ihv3->qrv = 2;
298 		ihv3->csum = 0;
299 		ihv3->csum = ip_compute_csum((void *)ihv3, sizeof(*ihv3));
300 		break;
301 	}
302 
303 	skb_put(skb, igmp_hdr_size);
304 	__skb_pull(skb, sizeof(*eth));
305 
306 out:
307 	return skb;
308 }
309 
310 #if IS_ENABLED(CONFIG_IPV6)
311 static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
312 						    const struct in6_addr *grp,
313 						    u8 *igmp_type)
314 {
315 	struct mld2_query *mld2q;
316 	unsigned long interval;
317 	struct ipv6hdr *ip6h;
318 	struct mld_msg *mldq;
319 	size_t mld_hdr_size;
320 	struct sk_buff *skb;
321 	struct ethhdr *eth;
322 	u8 *hopopt;
323 
324 	mld_hdr_size = sizeof(*mldq);
325 	if (br->multicast_mld_version == 2)
326 		mld_hdr_size = sizeof(*mld2q);
327 	skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
328 						 8 + mld_hdr_size);
329 	if (!skb)
330 		goto out;
331 
332 	skb->protocol = htons(ETH_P_IPV6);
333 
334 	/* Ethernet header */
335 	skb_reset_mac_header(skb);
336 	eth = eth_hdr(skb);
337 
338 	ether_addr_copy(eth->h_source, br->dev->dev_addr);
339 	eth->h_proto = htons(ETH_P_IPV6);
340 	skb_put(skb, sizeof(*eth));
341 
342 	/* IPv6 header + HbH option */
343 	skb_set_network_header(skb, skb->len);
344 	ip6h = ipv6_hdr(skb);
345 
346 	*(__force __be32 *)ip6h = htonl(0x60000000);
347 	ip6h->payload_len = htons(8 + mld_hdr_size);
348 	ip6h->nexthdr = IPPROTO_HOPOPTS;
349 	ip6h->hop_limit = 1;
350 	ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
351 	if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
352 			       &ip6h->saddr)) {
353 		kfree_skb(skb);
354 		br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, false);
355 		return NULL;
356 	}
357 
358 	br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true);
359 	ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
360 
361 	hopopt = (u8 *)(ip6h + 1);
362 	hopopt[0] = IPPROTO_ICMPV6;		/* next hdr */
363 	hopopt[1] = 0;				/* length of HbH */
364 	hopopt[2] = IPV6_TLV_ROUTERALERT;	/* Router Alert */
365 	hopopt[3] = 2;				/* Length of RA Option */
366 	hopopt[4] = 0;				/* Type = 0x0000 (MLD) */
367 	hopopt[5] = 0;
368 	hopopt[6] = IPV6_TLV_PAD1;		/* Pad1 */
369 	hopopt[7] = IPV6_TLV_PAD1;		/* Pad1 */
370 
371 	skb_put(skb, sizeof(*ip6h) + 8);
372 
373 	/* ICMPv6 */
374 	skb_set_transport_header(skb, skb->len);
375 	interval = ipv6_addr_any(grp) ?
376 			br->multicast_query_response_interval :
377 			br->multicast_last_member_interval;
378 	*igmp_type = ICMPV6_MGM_QUERY;
379 	switch (br->multicast_mld_version) {
380 	case 1:
381 		mldq = (struct mld_msg *)icmp6_hdr(skb);
382 		mldq->mld_type = ICMPV6_MGM_QUERY;
383 		mldq->mld_code = 0;
384 		mldq->mld_cksum = 0;
385 		mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
386 		mldq->mld_reserved = 0;
387 		mldq->mld_mca = *grp;
388 		mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
389 						  sizeof(*mldq), IPPROTO_ICMPV6,
390 						  csum_partial(mldq,
391 							       sizeof(*mldq),
392 							       0));
393 		break;
394 	case 2:
395 		mld2q = (struct mld2_query *)icmp6_hdr(skb);
396 		mld2q->mld2q_mrc = htons((u16)jiffies_to_msecs(interval));
397 		mld2q->mld2q_type = ICMPV6_MGM_QUERY;
398 		mld2q->mld2q_code = 0;
399 		mld2q->mld2q_cksum = 0;
400 		mld2q->mld2q_resv1 = 0;
401 		mld2q->mld2q_resv2 = 0;
402 		mld2q->mld2q_suppress = 0;
403 		mld2q->mld2q_qrv = 2;
404 		mld2q->mld2q_nsrcs = 0;
405 		mld2q->mld2q_qqic = br->multicast_query_interval / HZ;
406 		mld2q->mld2q_mca = *grp;
407 		mld2q->mld2q_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
408 						     sizeof(*mld2q),
409 						     IPPROTO_ICMPV6,
410 						     csum_partial(mld2q,
411 								  sizeof(*mld2q),
412 								  0));
413 		break;
414 	}
415 	skb_put(skb, mld_hdr_size);
416 
417 	__skb_pull(skb, sizeof(*eth));
418 
419 out:
420 	return skb;
421 }
422 #endif
423 
424 static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
425 						struct br_ip *addr,
426 						u8 *igmp_type)
427 {
428 	switch (addr->proto) {
429 	case htons(ETH_P_IP):
430 		return br_ip4_multicast_alloc_query(br, addr->u.ip4, igmp_type);
431 #if IS_ENABLED(CONFIG_IPV6)
432 	case htons(ETH_P_IPV6):
433 		return br_ip6_multicast_alloc_query(br, &addr->u.ip6,
434 						    igmp_type);
435 #endif
436 	}
437 	return NULL;
438 }
439 
440 struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
441 						    struct br_ip *group)
442 {
443 	struct net_bridge_mdb_entry *mp;
444 	int err;
445 
446 	mp = br_mdb_ip_get(br, group);
447 	if (mp)
448 		return mp;
449 
450 	if (atomic_read(&br->mdb_hash_tbl.nelems) >= br->hash_max) {
451 		br_opt_toggle(br, BROPT_MULTICAST_ENABLED, false);
452 		return ERR_PTR(-E2BIG);
453 	}
454 
455 	mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
456 	if (unlikely(!mp))
457 		return ERR_PTR(-ENOMEM);
458 
459 	mp->br = br;
460 	mp->addr = *group;
461 	timer_setup(&mp->timer, br_multicast_group_expired, 0);
462 	err = rhashtable_lookup_insert_fast(&br->mdb_hash_tbl, &mp->rhnode,
463 					    br_mdb_rht_params);
464 	if (err) {
465 		kfree(mp);
466 		mp = ERR_PTR(err);
467 	} else {
468 		hlist_add_head_rcu(&mp->mdb_node, &br->mdb_list);
469 	}
470 
471 	return mp;
472 }
473 
474 struct net_bridge_port_group *br_multicast_new_port_group(
475 			struct net_bridge_port *port,
476 			struct br_ip *group,
477 			struct net_bridge_port_group __rcu *next,
478 			unsigned char flags,
479 			const unsigned char *src)
480 {
481 	struct net_bridge_port_group *p;
482 
483 	p = kzalloc(sizeof(*p), GFP_ATOMIC);
484 	if (unlikely(!p))
485 		return NULL;
486 
487 	p->addr = *group;
488 	p->port = port;
489 	p->flags = flags;
490 	rcu_assign_pointer(p->next, next);
491 	hlist_add_head(&p->mglist, &port->mglist);
492 	timer_setup(&p->timer, br_multicast_port_group_expired, 0);
493 
494 	if (src)
495 		memcpy(p->eth_addr, src, ETH_ALEN);
496 	else
497 		eth_broadcast_addr(p->eth_addr);
498 
499 	return p;
500 }
501 
502 static bool br_port_group_equal(struct net_bridge_port_group *p,
503 				struct net_bridge_port *port,
504 				const unsigned char *src)
505 {
506 	if (p->port != port)
507 		return false;
508 
509 	if (!(port->flags & BR_MULTICAST_TO_UNICAST))
510 		return true;
511 
512 	return ether_addr_equal(src, p->eth_addr);
513 }
514 
515 static int br_multicast_add_group(struct net_bridge *br,
516 				  struct net_bridge_port *port,
517 				  struct br_ip *group,
518 				  const unsigned char *src)
519 {
520 	struct net_bridge_port_group __rcu **pp;
521 	struct net_bridge_port_group *p;
522 	struct net_bridge_mdb_entry *mp;
523 	unsigned long now = jiffies;
524 	int err;
525 
526 	spin_lock(&br->multicast_lock);
527 	if (!netif_running(br->dev) ||
528 	    (port && port->state == BR_STATE_DISABLED))
529 		goto out;
530 
531 	mp = br_multicast_new_group(br, group);
532 	err = PTR_ERR(mp);
533 	if (IS_ERR(mp))
534 		goto err;
535 
536 	if (!port) {
537 		if (!mp->host_joined) {
538 			mp->host_joined = true;
539 			br_mdb_notify(br->dev, NULL, &mp->addr, RTM_NEWMDB, 0);
540 		}
541 		mod_timer(&mp->timer, now + br->multicast_membership_interval);
542 		goto out;
543 	}
544 
545 	for (pp = &mp->ports;
546 	     (p = mlock_dereference(*pp, br)) != NULL;
547 	     pp = &p->next) {
548 		if (br_port_group_equal(p, port, src))
549 			goto found;
550 		if ((unsigned long)p->port < (unsigned long)port)
551 			break;
552 	}
553 
554 	p = br_multicast_new_port_group(port, group, *pp, 0, src);
555 	if (unlikely(!p))
556 		goto err;
557 	rcu_assign_pointer(*pp, p);
558 	br_mdb_notify(br->dev, port, group, RTM_NEWMDB, 0);
559 
560 found:
561 	mod_timer(&p->timer, now + br->multicast_membership_interval);
562 out:
563 	err = 0;
564 
565 err:
566 	spin_unlock(&br->multicast_lock);
567 	return err;
568 }
569 
570 static int br_ip4_multicast_add_group(struct net_bridge *br,
571 				      struct net_bridge_port *port,
572 				      __be32 group,
573 				      __u16 vid,
574 				      const unsigned char *src)
575 {
576 	struct br_ip br_group;
577 
578 	if (ipv4_is_local_multicast(group))
579 		return 0;
580 
581 	memset(&br_group, 0, sizeof(br_group));
582 	br_group.u.ip4 = group;
583 	br_group.proto = htons(ETH_P_IP);
584 	br_group.vid = vid;
585 
586 	return br_multicast_add_group(br, port, &br_group, src);
587 }
588 
589 #if IS_ENABLED(CONFIG_IPV6)
590 static int br_ip6_multicast_add_group(struct net_bridge *br,
591 				      struct net_bridge_port *port,
592 				      const struct in6_addr *group,
593 				      __u16 vid,
594 				      const unsigned char *src)
595 {
596 	struct br_ip br_group;
597 
598 	if (ipv6_addr_is_ll_all_nodes(group))
599 		return 0;
600 
601 	memset(&br_group, 0, sizeof(br_group));
602 	br_group.u.ip6 = *group;
603 	br_group.proto = htons(ETH_P_IPV6);
604 	br_group.vid = vid;
605 
606 	return br_multicast_add_group(br, port, &br_group, src);
607 }
608 #endif
609 
610 static void br_multicast_router_expired(struct timer_list *t)
611 {
612 	struct net_bridge_port *port =
613 			from_timer(port, t, multicast_router_timer);
614 	struct net_bridge *br = port->br;
615 
616 	spin_lock(&br->multicast_lock);
617 	if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
618 	    port->multicast_router == MDB_RTR_TYPE_PERM ||
619 	    timer_pending(&port->multicast_router_timer))
620 		goto out;
621 
622 	__del_port_router(port);
623 out:
624 	spin_unlock(&br->multicast_lock);
625 }
626 
627 static void br_mc_router_state_change(struct net_bridge *p,
628 				      bool is_mc_router)
629 {
630 	struct switchdev_attr attr = {
631 		.orig_dev = p->dev,
632 		.id = SWITCHDEV_ATTR_ID_BRIDGE_MROUTER,
633 		.flags = SWITCHDEV_F_DEFER,
634 		.u.mrouter = is_mc_router,
635 	};
636 
637 	switchdev_port_attr_set(p->dev, &attr);
638 }
639 
640 static void br_multicast_local_router_expired(struct timer_list *t)
641 {
642 	struct net_bridge *br = from_timer(br, t, multicast_router_timer);
643 
644 	spin_lock(&br->multicast_lock);
645 	if (br->multicast_router == MDB_RTR_TYPE_DISABLED ||
646 	    br->multicast_router == MDB_RTR_TYPE_PERM ||
647 	    timer_pending(&br->multicast_router_timer))
648 		goto out;
649 
650 	br_mc_router_state_change(br, false);
651 out:
652 	spin_unlock(&br->multicast_lock);
653 }
654 
655 static void br_multicast_querier_expired(struct net_bridge *br,
656 					 struct bridge_mcast_own_query *query)
657 {
658 	spin_lock(&br->multicast_lock);
659 	if (!netif_running(br->dev) || !br_opt_get(br, BROPT_MULTICAST_ENABLED))
660 		goto out;
661 
662 	br_multicast_start_querier(br, query);
663 
664 out:
665 	spin_unlock(&br->multicast_lock);
666 }
667 
668 static void br_ip4_multicast_querier_expired(struct timer_list *t)
669 {
670 	struct net_bridge *br = from_timer(br, t, ip4_other_query.timer);
671 
672 	br_multicast_querier_expired(br, &br->ip4_own_query);
673 }
674 
675 #if IS_ENABLED(CONFIG_IPV6)
676 static void br_ip6_multicast_querier_expired(struct timer_list *t)
677 {
678 	struct net_bridge *br = from_timer(br, t, ip6_other_query.timer);
679 
680 	br_multicast_querier_expired(br, &br->ip6_own_query);
681 }
682 #endif
683 
684 static void br_multicast_select_own_querier(struct net_bridge *br,
685 					    struct br_ip *ip,
686 					    struct sk_buff *skb)
687 {
688 	if (ip->proto == htons(ETH_P_IP))
689 		br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr;
690 #if IS_ENABLED(CONFIG_IPV6)
691 	else
692 		br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr;
693 #endif
694 }
695 
696 static void __br_multicast_send_query(struct net_bridge *br,
697 				      struct net_bridge_port *port,
698 				      struct br_ip *ip)
699 {
700 	struct sk_buff *skb;
701 	u8 igmp_type;
702 
703 	skb = br_multicast_alloc_query(br, ip, &igmp_type);
704 	if (!skb)
705 		return;
706 
707 	if (port) {
708 		skb->dev = port->dev;
709 		br_multicast_count(br, port, skb, igmp_type,
710 				   BR_MCAST_DIR_TX);
711 		NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
712 			dev_net(port->dev), NULL, skb, NULL, skb->dev,
713 			br_dev_queue_push_xmit);
714 	} else {
715 		br_multicast_select_own_querier(br, ip, skb);
716 		br_multicast_count(br, port, skb, igmp_type,
717 				   BR_MCAST_DIR_RX);
718 		netif_rx(skb);
719 	}
720 }
721 
722 static void br_multicast_send_query(struct net_bridge *br,
723 				    struct net_bridge_port *port,
724 				    struct bridge_mcast_own_query *own_query)
725 {
726 	struct bridge_mcast_other_query *other_query = NULL;
727 	struct br_ip br_group;
728 	unsigned long time;
729 
730 	if (!netif_running(br->dev) ||
731 	    !br_opt_get(br, BROPT_MULTICAST_ENABLED) ||
732 	    !br_opt_get(br, BROPT_MULTICAST_QUERIER))
733 		return;
734 
735 	memset(&br_group.u, 0, sizeof(br_group.u));
736 
737 	if (port ? (own_query == &port->ip4_own_query) :
738 		   (own_query == &br->ip4_own_query)) {
739 		other_query = &br->ip4_other_query;
740 		br_group.proto = htons(ETH_P_IP);
741 #if IS_ENABLED(CONFIG_IPV6)
742 	} else {
743 		other_query = &br->ip6_other_query;
744 		br_group.proto = htons(ETH_P_IPV6);
745 #endif
746 	}
747 
748 	if (!other_query || timer_pending(&other_query->timer))
749 		return;
750 
751 	__br_multicast_send_query(br, port, &br_group);
752 
753 	time = jiffies;
754 	time += own_query->startup_sent < br->multicast_startup_query_count ?
755 		br->multicast_startup_query_interval :
756 		br->multicast_query_interval;
757 	mod_timer(&own_query->timer, time);
758 }
759 
760 static void
761 br_multicast_port_query_expired(struct net_bridge_port *port,
762 				struct bridge_mcast_own_query *query)
763 {
764 	struct net_bridge *br = port->br;
765 
766 	spin_lock(&br->multicast_lock);
767 	if (port->state == BR_STATE_DISABLED ||
768 	    port->state == BR_STATE_BLOCKING)
769 		goto out;
770 
771 	if (query->startup_sent < br->multicast_startup_query_count)
772 		query->startup_sent++;
773 
774 	br_multicast_send_query(port->br, port, query);
775 
776 out:
777 	spin_unlock(&br->multicast_lock);
778 }
779 
780 static void br_ip4_multicast_port_query_expired(struct timer_list *t)
781 {
782 	struct net_bridge_port *port = from_timer(port, t, ip4_own_query.timer);
783 
784 	br_multicast_port_query_expired(port, &port->ip4_own_query);
785 }
786 
787 #if IS_ENABLED(CONFIG_IPV6)
788 static void br_ip6_multicast_port_query_expired(struct timer_list *t)
789 {
790 	struct net_bridge_port *port = from_timer(port, t, ip6_own_query.timer);
791 
792 	br_multicast_port_query_expired(port, &port->ip6_own_query);
793 }
794 #endif
795 
796 static void br_mc_disabled_update(struct net_device *dev, bool value)
797 {
798 	struct switchdev_attr attr = {
799 		.orig_dev = dev,
800 		.id = SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED,
801 		.flags = SWITCHDEV_F_DEFER,
802 		.u.mc_disabled = !value,
803 	};
804 
805 	switchdev_port_attr_set(dev, &attr);
806 }
807 
808 int br_multicast_add_port(struct net_bridge_port *port)
809 {
810 	port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
811 
812 	timer_setup(&port->multicast_router_timer,
813 		    br_multicast_router_expired, 0);
814 	timer_setup(&port->ip4_own_query.timer,
815 		    br_ip4_multicast_port_query_expired, 0);
816 #if IS_ENABLED(CONFIG_IPV6)
817 	timer_setup(&port->ip6_own_query.timer,
818 		    br_ip6_multicast_port_query_expired, 0);
819 #endif
820 	br_mc_disabled_update(port->dev,
821 			      br_opt_get(port->br, BROPT_MULTICAST_ENABLED));
822 
823 	port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
824 	if (!port->mcast_stats)
825 		return -ENOMEM;
826 
827 	return 0;
828 }
829 
830 void br_multicast_del_port(struct net_bridge_port *port)
831 {
832 	struct net_bridge *br = port->br;
833 	struct net_bridge_port_group *pg;
834 	struct hlist_node *n;
835 
836 	/* Take care of the remaining groups, only perm ones should be left */
837 	spin_lock_bh(&br->multicast_lock);
838 	hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
839 		br_multicast_del_pg(br, pg);
840 	spin_unlock_bh(&br->multicast_lock);
841 	del_timer_sync(&port->multicast_router_timer);
842 	free_percpu(port->mcast_stats);
843 }
844 
845 static void br_multicast_enable(struct bridge_mcast_own_query *query)
846 {
847 	query->startup_sent = 0;
848 
849 	if (try_to_del_timer_sync(&query->timer) >= 0 ||
850 	    del_timer(&query->timer))
851 		mod_timer(&query->timer, jiffies);
852 }
853 
854 static void __br_multicast_enable_port(struct net_bridge_port *port)
855 {
856 	struct net_bridge *br = port->br;
857 
858 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED) || !netif_running(br->dev))
859 		return;
860 
861 	br_multicast_enable(&port->ip4_own_query);
862 #if IS_ENABLED(CONFIG_IPV6)
863 	br_multicast_enable(&port->ip6_own_query);
864 #endif
865 	if (port->multicast_router == MDB_RTR_TYPE_PERM &&
866 	    hlist_unhashed(&port->rlist))
867 		br_multicast_add_router(br, port);
868 }
869 
870 void br_multicast_enable_port(struct net_bridge_port *port)
871 {
872 	struct net_bridge *br = port->br;
873 
874 	spin_lock(&br->multicast_lock);
875 	__br_multicast_enable_port(port);
876 	spin_unlock(&br->multicast_lock);
877 }
878 
879 void br_multicast_disable_port(struct net_bridge_port *port)
880 {
881 	struct net_bridge *br = port->br;
882 	struct net_bridge_port_group *pg;
883 	struct hlist_node *n;
884 
885 	spin_lock(&br->multicast_lock);
886 	hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
887 		if (!(pg->flags & MDB_PG_FLAGS_PERMANENT))
888 			br_multicast_del_pg(br, pg);
889 
890 	__del_port_router(port);
891 
892 	del_timer(&port->multicast_router_timer);
893 	del_timer(&port->ip4_own_query.timer);
894 #if IS_ENABLED(CONFIG_IPV6)
895 	del_timer(&port->ip6_own_query.timer);
896 #endif
897 	spin_unlock(&br->multicast_lock);
898 }
899 
900 static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
901 					 struct net_bridge_port *port,
902 					 struct sk_buff *skb,
903 					 u16 vid)
904 {
905 	const unsigned char *src;
906 	struct igmpv3_report *ih;
907 	struct igmpv3_grec *grec;
908 	int i;
909 	int len;
910 	int num;
911 	int type;
912 	int err = 0;
913 	__be32 group;
914 	u16 nsrcs;
915 
916 	ih = igmpv3_report_hdr(skb);
917 	num = ntohs(ih->ngrec);
918 	len = skb_transport_offset(skb) + sizeof(*ih);
919 
920 	for (i = 0; i < num; i++) {
921 		len += sizeof(*grec);
922 		if (!ip_mc_may_pull(skb, len))
923 			return -EINVAL;
924 
925 		grec = (void *)(skb->data + len - sizeof(*grec));
926 		group = grec->grec_mca;
927 		type = grec->grec_type;
928 		nsrcs = ntohs(grec->grec_nsrcs);
929 
930 		len += nsrcs * 4;
931 		if (!ip_mc_may_pull(skb, len))
932 			return -EINVAL;
933 
934 		/* We treat this as an IGMPv2 report for now. */
935 		switch (type) {
936 		case IGMPV3_MODE_IS_INCLUDE:
937 		case IGMPV3_MODE_IS_EXCLUDE:
938 		case IGMPV3_CHANGE_TO_INCLUDE:
939 		case IGMPV3_CHANGE_TO_EXCLUDE:
940 		case IGMPV3_ALLOW_NEW_SOURCES:
941 		case IGMPV3_BLOCK_OLD_SOURCES:
942 			break;
943 
944 		default:
945 			continue;
946 		}
947 
948 		src = eth_hdr(skb)->h_source;
949 		if ((type == IGMPV3_CHANGE_TO_INCLUDE ||
950 		     type == IGMPV3_MODE_IS_INCLUDE) &&
951 		    nsrcs == 0) {
952 			br_ip4_multicast_leave_group(br, port, group, vid, src);
953 		} else {
954 			err = br_ip4_multicast_add_group(br, port, group, vid,
955 							 src);
956 			if (err)
957 				break;
958 		}
959 	}
960 
961 	return err;
962 }
963 
964 #if IS_ENABLED(CONFIG_IPV6)
965 static int br_ip6_multicast_mld2_report(struct net_bridge *br,
966 					struct net_bridge_port *port,
967 					struct sk_buff *skb,
968 					u16 vid)
969 {
970 	unsigned int nsrcs_offset;
971 	const unsigned char *src;
972 	struct icmp6hdr *icmp6h;
973 	struct mld2_grec *grec;
974 	unsigned int grec_len;
975 	int i;
976 	int len;
977 	int num;
978 	int err = 0;
979 
980 	if (!ipv6_mc_may_pull(skb, sizeof(*icmp6h)))
981 		return -EINVAL;
982 
983 	icmp6h = icmp6_hdr(skb);
984 	num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
985 	len = skb_transport_offset(skb) + sizeof(*icmp6h);
986 
987 	for (i = 0; i < num; i++) {
988 		__be16 *_nsrcs, __nsrcs;
989 		u16 nsrcs;
990 
991 		nsrcs_offset = len + offsetof(struct mld2_grec, grec_nsrcs);
992 
993 		if (skb_transport_offset(skb) + ipv6_transport_len(skb) <
994 		    nsrcs_offset + sizeof(_nsrcs))
995 			return -EINVAL;
996 
997 		_nsrcs = skb_header_pointer(skb, nsrcs_offset,
998 					    sizeof(__nsrcs), &__nsrcs);
999 		if (!_nsrcs)
1000 			return -EINVAL;
1001 
1002 		nsrcs = ntohs(*_nsrcs);
1003 		grec_len = struct_size(grec, grec_src, nsrcs);
1004 
1005 		if (!ipv6_mc_may_pull(skb, len + grec_len))
1006 			return -EINVAL;
1007 
1008 		grec = (struct mld2_grec *)(skb->data + len);
1009 		len += grec_len;
1010 
1011 		/* We treat these as MLDv1 reports for now. */
1012 		switch (grec->grec_type) {
1013 		case MLD2_MODE_IS_INCLUDE:
1014 		case MLD2_MODE_IS_EXCLUDE:
1015 		case MLD2_CHANGE_TO_INCLUDE:
1016 		case MLD2_CHANGE_TO_EXCLUDE:
1017 		case MLD2_ALLOW_NEW_SOURCES:
1018 		case MLD2_BLOCK_OLD_SOURCES:
1019 			break;
1020 
1021 		default:
1022 			continue;
1023 		}
1024 
1025 		src = eth_hdr(skb)->h_source;
1026 		if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE ||
1027 		     grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
1028 		    nsrcs == 0) {
1029 			br_ip6_multicast_leave_group(br, port, &grec->grec_mca,
1030 						     vid, src);
1031 		} else {
1032 			err = br_ip6_multicast_add_group(br, port,
1033 							 &grec->grec_mca, vid,
1034 							 src);
1035 			if (err)
1036 				break;
1037 		}
1038 	}
1039 
1040 	return err;
1041 }
1042 #endif
1043 
1044 static bool br_ip4_multicast_select_querier(struct net_bridge *br,
1045 					    struct net_bridge_port *port,
1046 					    __be32 saddr)
1047 {
1048 	if (!timer_pending(&br->ip4_own_query.timer) &&
1049 	    !timer_pending(&br->ip4_other_query.timer))
1050 		goto update;
1051 
1052 	if (!br->ip4_querier.addr.u.ip4)
1053 		goto update;
1054 
1055 	if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4))
1056 		goto update;
1057 
1058 	return false;
1059 
1060 update:
1061 	br->ip4_querier.addr.u.ip4 = saddr;
1062 
1063 	/* update protected by general multicast_lock by caller */
1064 	rcu_assign_pointer(br->ip4_querier.port, port);
1065 
1066 	return true;
1067 }
1068 
1069 #if IS_ENABLED(CONFIG_IPV6)
1070 static bool br_ip6_multicast_select_querier(struct net_bridge *br,
1071 					    struct net_bridge_port *port,
1072 					    struct in6_addr *saddr)
1073 {
1074 	if (!timer_pending(&br->ip6_own_query.timer) &&
1075 	    !timer_pending(&br->ip6_other_query.timer))
1076 		goto update;
1077 
1078 	if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0)
1079 		goto update;
1080 
1081 	return false;
1082 
1083 update:
1084 	br->ip6_querier.addr.u.ip6 = *saddr;
1085 
1086 	/* update protected by general multicast_lock by caller */
1087 	rcu_assign_pointer(br->ip6_querier.port, port);
1088 
1089 	return true;
1090 }
1091 #endif
1092 
1093 static bool br_multicast_select_querier(struct net_bridge *br,
1094 					struct net_bridge_port *port,
1095 					struct br_ip *saddr)
1096 {
1097 	switch (saddr->proto) {
1098 	case htons(ETH_P_IP):
1099 		return br_ip4_multicast_select_querier(br, port, saddr->u.ip4);
1100 #if IS_ENABLED(CONFIG_IPV6)
1101 	case htons(ETH_P_IPV6):
1102 		return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6);
1103 #endif
1104 	}
1105 
1106 	return false;
1107 }
1108 
1109 static void
1110 br_multicast_update_query_timer(struct net_bridge *br,
1111 				struct bridge_mcast_other_query *query,
1112 				unsigned long max_delay)
1113 {
1114 	if (!timer_pending(&query->timer))
1115 		query->delay_time = jiffies + max_delay;
1116 
1117 	mod_timer(&query->timer, jiffies + br->multicast_querier_interval);
1118 }
1119 
1120 static void br_port_mc_router_state_change(struct net_bridge_port *p,
1121 					   bool is_mc_router)
1122 {
1123 	struct switchdev_attr attr = {
1124 		.orig_dev = p->dev,
1125 		.id = SWITCHDEV_ATTR_ID_PORT_MROUTER,
1126 		.flags = SWITCHDEV_F_DEFER,
1127 		.u.mrouter = is_mc_router,
1128 	};
1129 
1130 	switchdev_port_attr_set(p->dev, &attr);
1131 }
1132 
1133 /*
1134  * Add port to router_list
1135  *  list is maintained ordered by pointer value
1136  *  and locked by br->multicast_lock and RCU
1137  */
1138 static void br_multicast_add_router(struct net_bridge *br,
1139 				    struct net_bridge_port *port)
1140 {
1141 	struct net_bridge_port *p;
1142 	struct hlist_node *slot = NULL;
1143 
1144 	if (!hlist_unhashed(&port->rlist))
1145 		return;
1146 
1147 	hlist_for_each_entry(p, &br->router_list, rlist) {
1148 		if ((unsigned long) port >= (unsigned long) p)
1149 			break;
1150 		slot = &p->rlist;
1151 	}
1152 
1153 	if (slot)
1154 		hlist_add_behind_rcu(&port->rlist, slot);
1155 	else
1156 		hlist_add_head_rcu(&port->rlist, &br->router_list);
1157 	br_rtr_notify(br->dev, port, RTM_NEWMDB);
1158 	br_port_mc_router_state_change(port, true);
1159 }
1160 
1161 static void br_multicast_mark_router(struct net_bridge *br,
1162 				     struct net_bridge_port *port)
1163 {
1164 	unsigned long now = jiffies;
1165 
1166 	if (!port) {
1167 		if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) {
1168 			if (!timer_pending(&br->multicast_router_timer))
1169 				br_mc_router_state_change(br, true);
1170 			mod_timer(&br->multicast_router_timer,
1171 				  now + br->multicast_querier_interval);
1172 		}
1173 		return;
1174 	}
1175 
1176 	if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
1177 	    port->multicast_router == MDB_RTR_TYPE_PERM)
1178 		return;
1179 
1180 	br_multicast_add_router(br, port);
1181 
1182 	mod_timer(&port->multicast_router_timer,
1183 		  now + br->multicast_querier_interval);
1184 }
1185 
1186 static void br_multicast_query_received(struct net_bridge *br,
1187 					struct net_bridge_port *port,
1188 					struct bridge_mcast_other_query *query,
1189 					struct br_ip *saddr,
1190 					unsigned long max_delay)
1191 {
1192 	if (!br_multicast_select_querier(br, port, saddr))
1193 		return;
1194 
1195 	br_multicast_update_query_timer(br, query, max_delay);
1196 	br_multicast_mark_router(br, port);
1197 }
1198 
1199 static void br_ip4_multicast_query(struct net_bridge *br,
1200 				   struct net_bridge_port *port,
1201 				   struct sk_buff *skb,
1202 				   u16 vid)
1203 {
1204 	unsigned int transport_len = ip_transport_len(skb);
1205 	const struct iphdr *iph = ip_hdr(skb);
1206 	struct igmphdr *ih = igmp_hdr(skb);
1207 	struct net_bridge_mdb_entry *mp;
1208 	struct igmpv3_query *ih3;
1209 	struct net_bridge_port_group *p;
1210 	struct net_bridge_port_group __rcu **pp;
1211 	struct br_ip saddr;
1212 	unsigned long max_delay;
1213 	unsigned long now = jiffies;
1214 	__be32 group;
1215 
1216 	spin_lock(&br->multicast_lock);
1217 	if (!netif_running(br->dev) ||
1218 	    (port && port->state == BR_STATE_DISABLED))
1219 		goto out;
1220 
1221 	group = ih->group;
1222 
1223 	if (transport_len == sizeof(*ih)) {
1224 		max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1225 
1226 		if (!max_delay) {
1227 			max_delay = 10 * HZ;
1228 			group = 0;
1229 		}
1230 	} else if (transport_len >= sizeof(*ih3)) {
1231 		ih3 = igmpv3_query_hdr(skb);
1232 		if (ih3->nsrcs)
1233 			goto out;
1234 
1235 		max_delay = ih3->code ?
1236 			    IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
1237 	} else {
1238 		goto out;
1239 	}
1240 
1241 	if (!group) {
1242 		saddr.proto = htons(ETH_P_IP);
1243 		saddr.u.ip4 = iph->saddr;
1244 
1245 		br_multicast_query_received(br, port, &br->ip4_other_query,
1246 					    &saddr, max_delay);
1247 		goto out;
1248 	}
1249 
1250 	mp = br_mdb_ip4_get(br, group, vid);
1251 	if (!mp)
1252 		goto out;
1253 
1254 	max_delay *= br->multicast_last_member_count;
1255 
1256 	if (mp->host_joined &&
1257 	    (timer_pending(&mp->timer) ?
1258 	     time_after(mp->timer.expires, now + max_delay) :
1259 	     try_to_del_timer_sync(&mp->timer) >= 0))
1260 		mod_timer(&mp->timer, now + max_delay);
1261 
1262 	for (pp = &mp->ports;
1263 	     (p = mlock_dereference(*pp, br)) != NULL;
1264 	     pp = &p->next) {
1265 		if (timer_pending(&p->timer) ?
1266 		    time_after(p->timer.expires, now + max_delay) :
1267 		    try_to_del_timer_sync(&p->timer) >= 0)
1268 			mod_timer(&p->timer, now + max_delay);
1269 	}
1270 
1271 out:
1272 	spin_unlock(&br->multicast_lock);
1273 }
1274 
1275 #if IS_ENABLED(CONFIG_IPV6)
1276 static int br_ip6_multicast_query(struct net_bridge *br,
1277 				  struct net_bridge_port *port,
1278 				  struct sk_buff *skb,
1279 				  u16 vid)
1280 {
1281 	unsigned int transport_len = ipv6_transport_len(skb);
1282 	struct mld_msg *mld;
1283 	struct net_bridge_mdb_entry *mp;
1284 	struct mld2_query *mld2q;
1285 	struct net_bridge_port_group *p;
1286 	struct net_bridge_port_group __rcu **pp;
1287 	struct br_ip saddr;
1288 	unsigned long max_delay;
1289 	unsigned long now = jiffies;
1290 	unsigned int offset = skb_transport_offset(skb);
1291 	const struct in6_addr *group = NULL;
1292 	bool is_general_query;
1293 	int err = 0;
1294 
1295 	spin_lock(&br->multicast_lock);
1296 	if (!netif_running(br->dev) ||
1297 	    (port && port->state == BR_STATE_DISABLED))
1298 		goto out;
1299 
1300 	if (transport_len == sizeof(*mld)) {
1301 		if (!pskb_may_pull(skb, offset + sizeof(*mld))) {
1302 			err = -EINVAL;
1303 			goto out;
1304 		}
1305 		mld = (struct mld_msg *) icmp6_hdr(skb);
1306 		max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
1307 		if (max_delay)
1308 			group = &mld->mld_mca;
1309 	} else {
1310 		if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) {
1311 			err = -EINVAL;
1312 			goto out;
1313 		}
1314 		mld2q = (struct mld2_query *)icmp6_hdr(skb);
1315 		if (!mld2q->mld2q_nsrcs)
1316 			group = &mld2q->mld2q_mca;
1317 
1318 		max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
1319 	}
1320 
1321 	is_general_query = group && ipv6_addr_any(group);
1322 
1323 	if (is_general_query) {
1324 		saddr.proto = htons(ETH_P_IPV6);
1325 		saddr.u.ip6 = ipv6_hdr(skb)->saddr;
1326 
1327 		br_multicast_query_received(br, port, &br->ip6_other_query,
1328 					    &saddr, max_delay);
1329 		goto out;
1330 	} else if (!group) {
1331 		goto out;
1332 	}
1333 
1334 	mp = br_mdb_ip6_get(br, group, vid);
1335 	if (!mp)
1336 		goto out;
1337 
1338 	max_delay *= br->multicast_last_member_count;
1339 	if (mp->host_joined &&
1340 	    (timer_pending(&mp->timer) ?
1341 	     time_after(mp->timer.expires, now + max_delay) :
1342 	     try_to_del_timer_sync(&mp->timer) >= 0))
1343 		mod_timer(&mp->timer, now + max_delay);
1344 
1345 	for (pp = &mp->ports;
1346 	     (p = mlock_dereference(*pp, br)) != NULL;
1347 	     pp = &p->next) {
1348 		if (timer_pending(&p->timer) ?
1349 		    time_after(p->timer.expires, now + max_delay) :
1350 		    try_to_del_timer_sync(&p->timer) >= 0)
1351 			mod_timer(&p->timer, now + max_delay);
1352 	}
1353 
1354 out:
1355 	spin_unlock(&br->multicast_lock);
1356 	return err;
1357 }
1358 #endif
1359 
1360 static void
1361 br_multicast_leave_group(struct net_bridge *br,
1362 			 struct net_bridge_port *port,
1363 			 struct br_ip *group,
1364 			 struct bridge_mcast_other_query *other_query,
1365 			 struct bridge_mcast_own_query *own_query,
1366 			 const unsigned char *src)
1367 {
1368 	struct net_bridge_mdb_entry *mp;
1369 	struct net_bridge_port_group *p;
1370 	unsigned long now;
1371 	unsigned long time;
1372 
1373 	spin_lock(&br->multicast_lock);
1374 	if (!netif_running(br->dev) ||
1375 	    (port && port->state == BR_STATE_DISABLED))
1376 		goto out;
1377 
1378 	mp = br_mdb_ip_get(br, group);
1379 	if (!mp)
1380 		goto out;
1381 
1382 	if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
1383 		struct net_bridge_port_group __rcu **pp;
1384 
1385 		for (pp = &mp->ports;
1386 		     (p = mlock_dereference(*pp, br)) != NULL;
1387 		     pp = &p->next) {
1388 			if (!br_port_group_equal(p, port, src))
1389 				continue;
1390 
1391 			if (p->flags & MDB_PG_FLAGS_PERMANENT)
1392 				break;
1393 
1394 			rcu_assign_pointer(*pp, p->next);
1395 			hlist_del_init(&p->mglist);
1396 			del_timer(&p->timer);
1397 			kfree_rcu(p, rcu);
1398 			br_mdb_notify(br->dev, port, group, RTM_DELMDB,
1399 				      p->flags);
1400 
1401 			if (!mp->ports && !mp->host_joined &&
1402 			    netif_running(br->dev))
1403 				mod_timer(&mp->timer, jiffies);
1404 		}
1405 		goto out;
1406 	}
1407 
1408 	if (timer_pending(&other_query->timer))
1409 		goto out;
1410 
1411 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER)) {
1412 		__br_multicast_send_query(br, port, &mp->addr);
1413 
1414 		time = jiffies + br->multicast_last_member_count *
1415 				 br->multicast_last_member_interval;
1416 
1417 		mod_timer(&own_query->timer, time);
1418 
1419 		for (p = mlock_dereference(mp->ports, br);
1420 		     p != NULL;
1421 		     p = mlock_dereference(p->next, br)) {
1422 			if (!br_port_group_equal(p, port, src))
1423 				continue;
1424 
1425 			if (!hlist_unhashed(&p->mglist) &&
1426 			    (timer_pending(&p->timer) ?
1427 			     time_after(p->timer.expires, time) :
1428 			     try_to_del_timer_sync(&p->timer) >= 0)) {
1429 				mod_timer(&p->timer, time);
1430 			}
1431 
1432 			break;
1433 		}
1434 	}
1435 
1436 	now = jiffies;
1437 	time = now + br->multicast_last_member_count *
1438 		     br->multicast_last_member_interval;
1439 
1440 	if (!port) {
1441 		if (mp->host_joined &&
1442 		    (timer_pending(&mp->timer) ?
1443 		     time_after(mp->timer.expires, time) :
1444 		     try_to_del_timer_sync(&mp->timer) >= 0)) {
1445 			mod_timer(&mp->timer, time);
1446 		}
1447 
1448 		goto out;
1449 	}
1450 
1451 	for (p = mlock_dereference(mp->ports, br);
1452 	     p != NULL;
1453 	     p = mlock_dereference(p->next, br)) {
1454 		if (p->port != port)
1455 			continue;
1456 
1457 		if (!hlist_unhashed(&p->mglist) &&
1458 		    (timer_pending(&p->timer) ?
1459 		     time_after(p->timer.expires, time) :
1460 		     try_to_del_timer_sync(&p->timer) >= 0)) {
1461 			mod_timer(&p->timer, time);
1462 		}
1463 
1464 		break;
1465 	}
1466 out:
1467 	spin_unlock(&br->multicast_lock);
1468 }
1469 
1470 static void br_ip4_multicast_leave_group(struct net_bridge *br,
1471 					 struct net_bridge_port *port,
1472 					 __be32 group,
1473 					 __u16 vid,
1474 					 const unsigned char *src)
1475 {
1476 	struct br_ip br_group;
1477 	struct bridge_mcast_own_query *own_query;
1478 
1479 	if (ipv4_is_local_multicast(group))
1480 		return;
1481 
1482 	own_query = port ? &port->ip4_own_query : &br->ip4_own_query;
1483 
1484 	memset(&br_group, 0, sizeof(br_group));
1485 	br_group.u.ip4 = group;
1486 	br_group.proto = htons(ETH_P_IP);
1487 	br_group.vid = vid;
1488 
1489 	br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
1490 				 own_query, src);
1491 }
1492 
1493 #if IS_ENABLED(CONFIG_IPV6)
1494 static void br_ip6_multicast_leave_group(struct net_bridge *br,
1495 					 struct net_bridge_port *port,
1496 					 const struct in6_addr *group,
1497 					 __u16 vid,
1498 					 const unsigned char *src)
1499 {
1500 	struct br_ip br_group;
1501 	struct bridge_mcast_own_query *own_query;
1502 
1503 	if (ipv6_addr_is_ll_all_nodes(group))
1504 		return;
1505 
1506 	own_query = port ? &port->ip6_own_query : &br->ip6_own_query;
1507 
1508 	memset(&br_group, 0, sizeof(br_group));
1509 	br_group.u.ip6 = *group;
1510 	br_group.proto = htons(ETH_P_IPV6);
1511 	br_group.vid = vid;
1512 
1513 	br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
1514 				 own_query, src);
1515 }
1516 #endif
1517 
1518 static void br_multicast_err_count(const struct net_bridge *br,
1519 				   const struct net_bridge_port *p,
1520 				   __be16 proto)
1521 {
1522 	struct bridge_mcast_stats __percpu *stats;
1523 	struct bridge_mcast_stats *pstats;
1524 
1525 	if (!br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
1526 		return;
1527 
1528 	if (p)
1529 		stats = p->mcast_stats;
1530 	else
1531 		stats = br->mcast_stats;
1532 	if (WARN_ON(!stats))
1533 		return;
1534 
1535 	pstats = this_cpu_ptr(stats);
1536 
1537 	u64_stats_update_begin(&pstats->syncp);
1538 	switch (proto) {
1539 	case htons(ETH_P_IP):
1540 		pstats->mstats.igmp_parse_errors++;
1541 		break;
1542 #if IS_ENABLED(CONFIG_IPV6)
1543 	case htons(ETH_P_IPV6):
1544 		pstats->mstats.mld_parse_errors++;
1545 		break;
1546 #endif
1547 	}
1548 	u64_stats_update_end(&pstats->syncp);
1549 }
1550 
1551 static void br_multicast_pim(struct net_bridge *br,
1552 			     struct net_bridge_port *port,
1553 			     const struct sk_buff *skb)
1554 {
1555 	unsigned int offset = skb_transport_offset(skb);
1556 	struct pimhdr *pimhdr, _pimhdr;
1557 
1558 	pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr);
1559 	if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION ||
1560 	    pim_hdr_type(pimhdr) != PIM_TYPE_HELLO)
1561 		return;
1562 
1563 	br_multicast_mark_router(br, port);
1564 }
1565 
1566 static int br_ip4_multicast_mrd_rcv(struct net_bridge *br,
1567 				    struct net_bridge_port *port,
1568 				    struct sk_buff *skb)
1569 {
1570 	if (ip_hdr(skb)->protocol != IPPROTO_IGMP ||
1571 	    igmp_hdr(skb)->type != IGMP_MRDISC_ADV)
1572 		return -ENOMSG;
1573 
1574 	br_multicast_mark_router(br, port);
1575 
1576 	return 0;
1577 }
1578 
1579 static int br_multicast_ipv4_rcv(struct net_bridge *br,
1580 				 struct net_bridge_port *port,
1581 				 struct sk_buff *skb,
1582 				 u16 vid)
1583 {
1584 	const unsigned char *src;
1585 	struct igmphdr *ih;
1586 	int err;
1587 
1588 	err = ip_mc_check_igmp(skb);
1589 
1590 	if (err == -ENOMSG) {
1591 		if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) {
1592 			BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1593 		} else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) {
1594 			if (ip_hdr(skb)->protocol == IPPROTO_PIM)
1595 				br_multicast_pim(br, port, skb);
1596 		} else if (ipv4_is_all_snoopers(ip_hdr(skb)->daddr)) {
1597 			br_ip4_multicast_mrd_rcv(br, port, skb);
1598 		}
1599 
1600 		return 0;
1601 	} else if (err < 0) {
1602 		br_multicast_err_count(br, port, skb->protocol);
1603 		return err;
1604 	}
1605 
1606 	ih = igmp_hdr(skb);
1607 	src = eth_hdr(skb)->h_source;
1608 	BR_INPUT_SKB_CB(skb)->igmp = ih->type;
1609 
1610 	switch (ih->type) {
1611 	case IGMP_HOST_MEMBERSHIP_REPORT:
1612 	case IGMPV2_HOST_MEMBERSHIP_REPORT:
1613 		BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1614 		err = br_ip4_multicast_add_group(br, port, ih->group, vid, src);
1615 		break;
1616 	case IGMPV3_HOST_MEMBERSHIP_REPORT:
1617 		err = br_ip4_multicast_igmp3_report(br, port, skb, vid);
1618 		break;
1619 	case IGMP_HOST_MEMBERSHIP_QUERY:
1620 		br_ip4_multicast_query(br, port, skb, vid);
1621 		break;
1622 	case IGMP_HOST_LEAVE_MESSAGE:
1623 		br_ip4_multicast_leave_group(br, port, ih->group, vid, src);
1624 		break;
1625 	}
1626 
1627 	br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
1628 			   BR_MCAST_DIR_RX);
1629 
1630 	return err;
1631 }
1632 
1633 #if IS_ENABLED(CONFIG_IPV6)
1634 static int br_ip6_multicast_mrd_rcv(struct net_bridge *br,
1635 				    struct net_bridge_port *port,
1636 				    struct sk_buff *skb)
1637 {
1638 	int ret;
1639 
1640 	if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6)
1641 		return -ENOMSG;
1642 
1643 	ret = ipv6_mc_check_icmpv6(skb);
1644 	if (ret < 0)
1645 		return ret;
1646 
1647 	if (icmp6_hdr(skb)->icmp6_type != ICMPV6_MRDISC_ADV)
1648 		return -ENOMSG;
1649 
1650 	br_multicast_mark_router(br, port);
1651 
1652 	return 0;
1653 }
1654 
1655 static int br_multicast_ipv6_rcv(struct net_bridge *br,
1656 				 struct net_bridge_port *port,
1657 				 struct sk_buff *skb,
1658 				 u16 vid)
1659 {
1660 	const unsigned char *src;
1661 	struct mld_msg *mld;
1662 	int err;
1663 
1664 	err = ipv6_mc_check_mld(skb);
1665 
1666 	if (err == -ENOMSG) {
1667 		if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
1668 			BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1669 
1670 		if (ipv6_addr_is_all_snoopers(&ipv6_hdr(skb)->daddr)) {
1671 			err = br_ip6_multicast_mrd_rcv(br, port, skb);
1672 
1673 			if (err < 0 && err != -ENOMSG) {
1674 				br_multicast_err_count(br, port, skb->protocol);
1675 				return err;
1676 			}
1677 		}
1678 
1679 		return 0;
1680 	} else if (err < 0) {
1681 		br_multicast_err_count(br, port, skb->protocol);
1682 		return err;
1683 	}
1684 
1685 	mld = (struct mld_msg *)skb_transport_header(skb);
1686 	BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type;
1687 
1688 	switch (mld->mld_type) {
1689 	case ICMPV6_MGM_REPORT:
1690 		src = eth_hdr(skb)->h_source;
1691 		BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1692 		err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid,
1693 						 src);
1694 		break;
1695 	case ICMPV6_MLD2_REPORT:
1696 		err = br_ip6_multicast_mld2_report(br, port, skb, vid);
1697 		break;
1698 	case ICMPV6_MGM_QUERY:
1699 		err = br_ip6_multicast_query(br, port, skb, vid);
1700 		break;
1701 	case ICMPV6_MGM_REDUCTION:
1702 		src = eth_hdr(skb)->h_source;
1703 		br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src);
1704 		break;
1705 	}
1706 
1707 	br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
1708 			   BR_MCAST_DIR_RX);
1709 
1710 	return err;
1711 }
1712 #endif
1713 
1714 int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1715 		     struct sk_buff *skb, u16 vid)
1716 {
1717 	int ret = 0;
1718 
1719 	BR_INPUT_SKB_CB(skb)->igmp = 0;
1720 	BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1721 
1722 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
1723 		return 0;
1724 
1725 	switch (skb->protocol) {
1726 	case htons(ETH_P_IP):
1727 		ret = br_multicast_ipv4_rcv(br, port, skb, vid);
1728 		break;
1729 #if IS_ENABLED(CONFIG_IPV6)
1730 	case htons(ETH_P_IPV6):
1731 		ret = br_multicast_ipv6_rcv(br, port, skb, vid);
1732 		break;
1733 #endif
1734 	}
1735 
1736 	return ret;
1737 }
1738 
1739 static void br_multicast_query_expired(struct net_bridge *br,
1740 				       struct bridge_mcast_own_query *query,
1741 				       struct bridge_mcast_querier *querier)
1742 {
1743 	spin_lock(&br->multicast_lock);
1744 	if (query->startup_sent < br->multicast_startup_query_count)
1745 		query->startup_sent++;
1746 
1747 	RCU_INIT_POINTER(querier->port, NULL);
1748 	br_multicast_send_query(br, NULL, query);
1749 	spin_unlock(&br->multicast_lock);
1750 }
1751 
1752 static void br_ip4_multicast_query_expired(struct timer_list *t)
1753 {
1754 	struct net_bridge *br = from_timer(br, t, ip4_own_query.timer);
1755 
1756 	br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier);
1757 }
1758 
1759 #if IS_ENABLED(CONFIG_IPV6)
1760 static void br_ip6_multicast_query_expired(struct timer_list *t)
1761 {
1762 	struct net_bridge *br = from_timer(br, t, ip6_own_query.timer);
1763 
1764 	br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier);
1765 }
1766 #endif
1767 
1768 void br_multicast_init(struct net_bridge *br)
1769 {
1770 	br->hash_max = BR_MULTICAST_DEFAULT_HASH_MAX;
1771 
1772 	br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
1773 	br->multicast_last_member_count = 2;
1774 	br->multicast_startup_query_count = 2;
1775 
1776 	br->multicast_last_member_interval = HZ;
1777 	br->multicast_query_response_interval = 10 * HZ;
1778 	br->multicast_startup_query_interval = 125 * HZ / 4;
1779 	br->multicast_query_interval = 125 * HZ;
1780 	br->multicast_querier_interval = 255 * HZ;
1781 	br->multicast_membership_interval = 260 * HZ;
1782 
1783 	br->ip4_other_query.delay_time = 0;
1784 	br->ip4_querier.port = NULL;
1785 	br->multicast_igmp_version = 2;
1786 #if IS_ENABLED(CONFIG_IPV6)
1787 	br->multicast_mld_version = 1;
1788 	br->ip6_other_query.delay_time = 0;
1789 	br->ip6_querier.port = NULL;
1790 #endif
1791 	br_opt_toggle(br, BROPT_MULTICAST_ENABLED, true);
1792 	br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true);
1793 
1794 	spin_lock_init(&br->multicast_lock);
1795 	timer_setup(&br->multicast_router_timer,
1796 		    br_multicast_local_router_expired, 0);
1797 	timer_setup(&br->ip4_other_query.timer,
1798 		    br_ip4_multicast_querier_expired, 0);
1799 	timer_setup(&br->ip4_own_query.timer,
1800 		    br_ip4_multicast_query_expired, 0);
1801 #if IS_ENABLED(CONFIG_IPV6)
1802 	timer_setup(&br->ip6_other_query.timer,
1803 		    br_ip6_multicast_querier_expired, 0);
1804 	timer_setup(&br->ip6_own_query.timer,
1805 		    br_ip6_multicast_query_expired, 0);
1806 #endif
1807 	INIT_HLIST_HEAD(&br->mdb_list);
1808 }
1809 
1810 static void br_ip4_multicast_join_snoopers(struct net_bridge *br)
1811 {
1812 	struct in_device *in_dev = in_dev_get(br->dev);
1813 
1814 	if (!in_dev)
1815 		return;
1816 
1817 	__ip_mc_inc_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
1818 	in_dev_put(in_dev);
1819 }
1820 
1821 #if IS_ENABLED(CONFIG_IPV6)
1822 static void br_ip6_multicast_join_snoopers(struct net_bridge *br)
1823 {
1824 	struct in6_addr addr;
1825 
1826 	ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
1827 	ipv6_dev_mc_inc(br->dev, &addr);
1828 }
1829 #else
1830 static inline void br_ip6_multicast_join_snoopers(struct net_bridge *br)
1831 {
1832 }
1833 #endif
1834 
1835 static void br_multicast_join_snoopers(struct net_bridge *br)
1836 {
1837 	br_ip4_multicast_join_snoopers(br);
1838 	br_ip6_multicast_join_snoopers(br);
1839 }
1840 
1841 static void br_ip4_multicast_leave_snoopers(struct net_bridge *br)
1842 {
1843 	struct in_device *in_dev = in_dev_get(br->dev);
1844 
1845 	if (WARN_ON(!in_dev))
1846 		return;
1847 
1848 	__ip_mc_dec_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
1849 	in_dev_put(in_dev);
1850 }
1851 
1852 #if IS_ENABLED(CONFIG_IPV6)
1853 static void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
1854 {
1855 	struct in6_addr addr;
1856 
1857 	ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
1858 	ipv6_dev_mc_dec(br->dev, &addr);
1859 }
1860 #else
1861 static inline void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
1862 {
1863 }
1864 #endif
1865 
1866 static void br_multicast_leave_snoopers(struct net_bridge *br)
1867 {
1868 	br_ip4_multicast_leave_snoopers(br);
1869 	br_ip6_multicast_leave_snoopers(br);
1870 }
1871 
1872 static void __br_multicast_open(struct net_bridge *br,
1873 				struct bridge_mcast_own_query *query)
1874 {
1875 	query->startup_sent = 0;
1876 
1877 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
1878 		return;
1879 
1880 	mod_timer(&query->timer, jiffies);
1881 }
1882 
1883 void br_multicast_open(struct net_bridge *br)
1884 {
1885 	if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
1886 		br_multicast_join_snoopers(br);
1887 
1888 	__br_multicast_open(br, &br->ip4_own_query);
1889 #if IS_ENABLED(CONFIG_IPV6)
1890 	__br_multicast_open(br, &br->ip6_own_query);
1891 #endif
1892 }
1893 
1894 void br_multicast_stop(struct net_bridge *br)
1895 {
1896 	del_timer_sync(&br->multicast_router_timer);
1897 	del_timer_sync(&br->ip4_other_query.timer);
1898 	del_timer_sync(&br->ip4_own_query.timer);
1899 #if IS_ENABLED(CONFIG_IPV6)
1900 	del_timer_sync(&br->ip6_other_query.timer);
1901 	del_timer_sync(&br->ip6_own_query.timer);
1902 #endif
1903 
1904 	if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
1905 		br_multicast_leave_snoopers(br);
1906 }
1907 
1908 void br_multicast_dev_del(struct net_bridge *br)
1909 {
1910 	struct net_bridge_mdb_entry *mp;
1911 	struct hlist_node *tmp;
1912 
1913 	spin_lock_bh(&br->multicast_lock);
1914 	hlist_for_each_entry_safe(mp, tmp, &br->mdb_list, mdb_node) {
1915 		del_timer(&mp->timer);
1916 		rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode,
1917 				       br_mdb_rht_params);
1918 		hlist_del_rcu(&mp->mdb_node);
1919 		kfree_rcu(mp, rcu);
1920 	}
1921 	spin_unlock_bh(&br->multicast_lock);
1922 
1923 	rcu_barrier();
1924 }
1925 
1926 int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1927 {
1928 	int err = -EINVAL;
1929 
1930 	spin_lock_bh(&br->multicast_lock);
1931 
1932 	switch (val) {
1933 	case MDB_RTR_TYPE_DISABLED:
1934 	case MDB_RTR_TYPE_PERM:
1935 		br_mc_router_state_change(br, val == MDB_RTR_TYPE_PERM);
1936 		del_timer(&br->multicast_router_timer);
1937 		br->multicast_router = val;
1938 		err = 0;
1939 		break;
1940 	case MDB_RTR_TYPE_TEMP_QUERY:
1941 		if (br->multicast_router != MDB_RTR_TYPE_TEMP_QUERY)
1942 			br_mc_router_state_change(br, false);
1943 		br->multicast_router = val;
1944 		err = 0;
1945 		break;
1946 	}
1947 
1948 	spin_unlock_bh(&br->multicast_lock);
1949 
1950 	return err;
1951 }
1952 
1953 static void __del_port_router(struct net_bridge_port *p)
1954 {
1955 	if (hlist_unhashed(&p->rlist))
1956 		return;
1957 	hlist_del_init_rcu(&p->rlist);
1958 	br_rtr_notify(p->br->dev, p, RTM_DELMDB);
1959 	br_port_mc_router_state_change(p, false);
1960 
1961 	/* don't allow timer refresh */
1962 	if (p->multicast_router == MDB_RTR_TYPE_TEMP)
1963 		p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
1964 }
1965 
1966 int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1967 {
1968 	struct net_bridge *br = p->br;
1969 	unsigned long now = jiffies;
1970 	int err = -EINVAL;
1971 
1972 	spin_lock(&br->multicast_lock);
1973 	if (p->multicast_router == val) {
1974 		/* Refresh the temp router port timer */
1975 		if (p->multicast_router == MDB_RTR_TYPE_TEMP)
1976 			mod_timer(&p->multicast_router_timer,
1977 				  now + br->multicast_querier_interval);
1978 		err = 0;
1979 		goto unlock;
1980 	}
1981 	switch (val) {
1982 	case MDB_RTR_TYPE_DISABLED:
1983 		p->multicast_router = MDB_RTR_TYPE_DISABLED;
1984 		__del_port_router(p);
1985 		del_timer(&p->multicast_router_timer);
1986 		break;
1987 	case MDB_RTR_TYPE_TEMP_QUERY:
1988 		p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
1989 		__del_port_router(p);
1990 		break;
1991 	case MDB_RTR_TYPE_PERM:
1992 		p->multicast_router = MDB_RTR_TYPE_PERM;
1993 		del_timer(&p->multicast_router_timer);
1994 		br_multicast_add_router(br, p);
1995 		break;
1996 	case MDB_RTR_TYPE_TEMP:
1997 		p->multicast_router = MDB_RTR_TYPE_TEMP;
1998 		br_multicast_mark_router(br, p);
1999 		break;
2000 	default:
2001 		goto unlock;
2002 	}
2003 	err = 0;
2004 unlock:
2005 	spin_unlock(&br->multicast_lock);
2006 
2007 	return err;
2008 }
2009 
2010 static void br_multicast_start_querier(struct net_bridge *br,
2011 				       struct bridge_mcast_own_query *query)
2012 {
2013 	struct net_bridge_port *port;
2014 
2015 	__br_multicast_open(br, query);
2016 
2017 	rcu_read_lock();
2018 	list_for_each_entry_rcu(port, &br->port_list, list) {
2019 		if (port->state == BR_STATE_DISABLED ||
2020 		    port->state == BR_STATE_BLOCKING)
2021 			continue;
2022 
2023 		if (query == &br->ip4_own_query)
2024 			br_multicast_enable(&port->ip4_own_query);
2025 #if IS_ENABLED(CONFIG_IPV6)
2026 		else
2027 			br_multicast_enable(&port->ip6_own_query);
2028 #endif
2029 	}
2030 	rcu_read_unlock();
2031 }
2032 
2033 int br_multicast_toggle(struct net_bridge *br, unsigned long val)
2034 {
2035 	struct net_bridge_port *port;
2036 
2037 	spin_lock_bh(&br->multicast_lock);
2038 	if (!!br_opt_get(br, BROPT_MULTICAST_ENABLED) == !!val)
2039 		goto unlock;
2040 
2041 	br_mc_disabled_update(br->dev, val);
2042 	br_opt_toggle(br, BROPT_MULTICAST_ENABLED, !!val);
2043 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) {
2044 		br_multicast_leave_snoopers(br);
2045 		goto unlock;
2046 	}
2047 
2048 	if (!netif_running(br->dev))
2049 		goto unlock;
2050 
2051 	br_multicast_open(br);
2052 	list_for_each_entry(port, &br->port_list, list)
2053 		__br_multicast_enable_port(port);
2054 
2055 unlock:
2056 	spin_unlock_bh(&br->multicast_lock);
2057 
2058 	return 0;
2059 }
2060 
2061 bool br_multicast_enabled(const struct net_device *dev)
2062 {
2063 	struct net_bridge *br = netdev_priv(dev);
2064 
2065 	return !!br_opt_get(br, BROPT_MULTICAST_ENABLED);
2066 }
2067 EXPORT_SYMBOL_GPL(br_multicast_enabled);
2068 
2069 bool br_multicast_router(const struct net_device *dev)
2070 {
2071 	struct net_bridge *br = netdev_priv(dev);
2072 	bool is_router;
2073 
2074 	spin_lock_bh(&br->multicast_lock);
2075 	is_router = br_multicast_is_router(br);
2076 	spin_unlock_bh(&br->multicast_lock);
2077 	return is_router;
2078 }
2079 EXPORT_SYMBOL_GPL(br_multicast_router);
2080 
2081 int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
2082 {
2083 	unsigned long max_delay;
2084 
2085 	val = !!val;
2086 
2087 	spin_lock_bh(&br->multicast_lock);
2088 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER) == val)
2089 		goto unlock;
2090 
2091 	br_opt_toggle(br, BROPT_MULTICAST_QUERIER, !!val);
2092 	if (!val)
2093 		goto unlock;
2094 
2095 	max_delay = br->multicast_query_response_interval;
2096 
2097 	if (!timer_pending(&br->ip4_other_query.timer))
2098 		br->ip4_other_query.delay_time = jiffies + max_delay;
2099 
2100 	br_multicast_start_querier(br, &br->ip4_own_query);
2101 
2102 #if IS_ENABLED(CONFIG_IPV6)
2103 	if (!timer_pending(&br->ip6_other_query.timer))
2104 		br->ip6_other_query.delay_time = jiffies + max_delay;
2105 
2106 	br_multicast_start_querier(br, &br->ip6_own_query);
2107 #endif
2108 
2109 unlock:
2110 	spin_unlock_bh(&br->multicast_lock);
2111 
2112 	return 0;
2113 }
2114 
2115 int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val)
2116 {
2117 	/* Currently we support only version 2 and 3 */
2118 	switch (val) {
2119 	case 2:
2120 	case 3:
2121 		break;
2122 	default:
2123 		return -EINVAL;
2124 	}
2125 
2126 	spin_lock_bh(&br->multicast_lock);
2127 	br->multicast_igmp_version = val;
2128 	spin_unlock_bh(&br->multicast_lock);
2129 
2130 	return 0;
2131 }
2132 
2133 #if IS_ENABLED(CONFIG_IPV6)
2134 int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val)
2135 {
2136 	/* Currently we support version 1 and 2 */
2137 	switch (val) {
2138 	case 1:
2139 	case 2:
2140 		break;
2141 	default:
2142 		return -EINVAL;
2143 	}
2144 
2145 	spin_lock_bh(&br->multicast_lock);
2146 	br->multicast_mld_version = val;
2147 	spin_unlock_bh(&br->multicast_lock);
2148 
2149 	return 0;
2150 }
2151 #endif
2152 
2153 /**
2154  * br_multicast_list_adjacent - Returns snooped multicast addresses
2155  * @dev:	The bridge port adjacent to which to retrieve addresses
2156  * @br_ip_list:	The list to store found, snooped multicast IP addresses in
2157  *
2158  * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
2159  * snooping feature on all bridge ports of dev's bridge device, excluding
2160  * the addresses from dev itself.
2161  *
2162  * Returns the number of items added to br_ip_list.
2163  *
2164  * Notes:
2165  * - br_ip_list needs to be initialized by caller
2166  * - br_ip_list might contain duplicates in the end
2167  *   (needs to be taken care of by caller)
2168  * - br_ip_list needs to be freed by caller
2169  */
2170 int br_multicast_list_adjacent(struct net_device *dev,
2171 			       struct list_head *br_ip_list)
2172 {
2173 	struct net_bridge *br;
2174 	struct net_bridge_port *port;
2175 	struct net_bridge_port_group *group;
2176 	struct br_ip_list *entry;
2177 	int count = 0;
2178 
2179 	rcu_read_lock();
2180 	if (!br_ip_list || !netif_is_bridge_port(dev))
2181 		goto unlock;
2182 
2183 	port = br_port_get_rcu(dev);
2184 	if (!port || !port->br)
2185 		goto unlock;
2186 
2187 	br = port->br;
2188 
2189 	list_for_each_entry_rcu(port, &br->port_list, list) {
2190 		if (!port->dev || port->dev == dev)
2191 			continue;
2192 
2193 		hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
2194 			entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
2195 			if (!entry)
2196 				goto unlock;
2197 
2198 			entry->addr = group->addr;
2199 			list_add(&entry->list, br_ip_list);
2200 			count++;
2201 		}
2202 	}
2203 
2204 unlock:
2205 	rcu_read_unlock();
2206 	return count;
2207 }
2208 EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
2209 
2210 /**
2211  * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2212  * @dev: The bridge port providing the bridge on which to check for a querier
2213  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2214  *
2215  * Checks whether the given interface has a bridge on top and if so returns
2216  * true if a valid querier exists anywhere on the bridged link layer.
2217  * Otherwise returns false.
2218  */
2219 bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
2220 {
2221 	struct net_bridge *br;
2222 	struct net_bridge_port *port;
2223 	struct ethhdr eth;
2224 	bool ret = false;
2225 
2226 	rcu_read_lock();
2227 	if (!netif_is_bridge_port(dev))
2228 		goto unlock;
2229 
2230 	port = br_port_get_rcu(dev);
2231 	if (!port || !port->br)
2232 		goto unlock;
2233 
2234 	br = port->br;
2235 
2236 	memset(&eth, 0, sizeof(eth));
2237 	eth.h_proto = htons(proto);
2238 
2239 	ret = br_multicast_querier_exists(br, &eth);
2240 
2241 unlock:
2242 	rcu_read_unlock();
2243 	return ret;
2244 }
2245 EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
2246 
2247 /**
2248  * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
2249  * @dev: The bridge port adjacent to which to check for a querier
2250  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2251  *
2252  * Checks whether the given interface has a bridge on top and if so returns
2253  * true if a selected querier is behind one of the other ports of this
2254  * bridge. Otherwise returns false.
2255  */
2256 bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
2257 {
2258 	struct net_bridge *br;
2259 	struct net_bridge_port *port;
2260 	bool ret = false;
2261 
2262 	rcu_read_lock();
2263 	if (!netif_is_bridge_port(dev))
2264 		goto unlock;
2265 
2266 	port = br_port_get_rcu(dev);
2267 	if (!port || !port->br)
2268 		goto unlock;
2269 
2270 	br = port->br;
2271 
2272 	switch (proto) {
2273 	case ETH_P_IP:
2274 		if (!timer_pending(&br->ip4_other_query.timer) ||
2275 		    rcu_dereference(br->ip4_querier.port) == port)
2276 			goto unlock;
2277 		break;
2278 #if IS_ENABLED(CONFIG_IPV6)
2279 	case ETH_P_IPV6:
2280 		if (!timer_pending(&br->ip6_other_query.timer) ||
2281 		    rcu_dereference(br->ip6_querier.port) == port)
2282 			goto unlock;
2283 		break;
2284 #endif
2285 	default:
2286 		goto unlock;
2287 	}
2288 
2289 	ret = true;
2290 unlock:
2291 	rcu_read_unlock();
2292 	return ret;
2293 }
2294 EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
2295 
2296 static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
2297 			       const struct sk_buff *skb, u8 type, u8 dir)
2298 {
2299 	struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
2300 	__be16 proto = skb->protocol;
2301 	unsigned int t_len;
2302 
2303 	u64_stats_update_begin(&pstats->syncp);
2304 	switch (proto) {
2305 	case htons(ETH_P_IP):
2306 		t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
2307 		switch (type) {
2308 		case IGMP_HOST_MEMBERSHIP_REPORT:
2309 			pstats->mstats.igmp_v1reports[dir]++;
2310 			break;
2311 		case IGMPV2_HOST_MEMBERSHIP_REPORT:
2312 			pstats->mstats.igmp_v2reports[dir]++;
2313 			break;
2314 		case IGMPV3_HOST_MEMBERSHIP_REPORT:
2315 			pstats->mstats.igmp_v3reports[dir]++;
2316 			break;
2317 		case IGMP_HOST_MEMBERSHIP_QUERY:
2318 			if (t_len != sizeof(struct igmphdr)) {
2319 				pstats->mstats.igmp_v3queries[dir]++;
2320 			} else {
2321 				unsigned int offset = skb_transport_offset(skb);
2322 				struct igmphdr *ih, _ihdr;
2323 
2324 				ih = skb_header_pointer(skb, offset,
2325 							sizeof(_ihdr), &_ihdr);
2326 				if (!ih)
2327 					break;
2328 				if (!ih->code)
2329 					pstats->mstats.igmp_v1queries[dir]++;
2330 				else
2331 					pstats->mstats.igmp_v2queries[dir]++;
2332 			}
2333 			break;
2334 		case IGMP_HOST_LEAVE_MESSAGE:
2335 			pstats->mstats.igmp_leaves[dir]++;
2336 			break;
2337 		}
2338 		break;
2339 #if IS_ENABLED(CONFIG_IPV6)
2340 	case htons(ETH_P_IPV6):
2341 		t_len = ntohs(ipv6_hdr(skb)->payload_len) +
2342 			sizeof(struct ipv6hdr);
2343 		t_len -= skb_network_header_len(skb);
2344 		switch (type) {
2345 		case ICMPV6_MGM_REPORT:
2346 			pstats->mstats.mld_v1reports[dir]++;
2347 			break;
2348 		case ICMPV6_MLD2_REPORT:
2349 			pstats->mstats.mld_v2reports[dir]++;
2350 			break;
2351 		case ICMPV6_MGM_QUERY:
2352 			if (t_len != sizeof(struct mld_msg))
2353 				pstats->mstats.mld_v2queries[dir]++;
2354 			else
2355 				pstats->mstats.mld_v1queries[dir]++;
2356 			break;
2357 		case ICMPV6_MGM_REDUCTION:
2358 			pstats->mstats.mld_leaves[dir]++;
2359 			break;
2360 		}
2361 		break;
2362 #endif /* CONFIG_IPV6 */
2363 	}
2364 	u64_stats_update_end(&pstats->syncp);
2365 }
2366 
2367 void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
2368 			const struct sk_buff *skb, u8 type, u8 dir)
2369 {
2370 	struct bridge_mcast_stats __percpu *stats;
2371 
2372 	/* if multicast_disabled is true then igmp type can't be set */
2373 	if (!type || !br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
2374 		return;
2375 
2376 	if (p)
2377 		stats = p->mcast_stats;
2378 	else
2379 		stats = br->mcast_stats;
2380 	if (WARN_ON(!stats))
2381 		return;
2382 
2383 	br_mcast_stats_add(stats, skb, type, dir);
2384 }
2385 
2386 int br_multicast_init_stats(struct net_bridge *br)
2387 {
2388 	br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
2389 	if (!br->mcast_stats)
2390 		return -ENOMEM;
2391 
2392 	return 0;
2393 }
2394 
2395 void br_multicast_uninit_stats(struct net_bridge *br)
2396 {
2397 	free_percpu(br->mcast_stats);
2398 }
2399 
2400 static void mcast_stats_add_dir(u64 *dst, u64 *src)
2401 {
2402 	dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
2403 	dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
2404 }
2405 
2406 void br_multicast_get_stats(const struct net_bridge *br,
2407 			    const struct net_bridge_port *p,
2408 			    struct br_mcast_stats *dest)
2409 {
2410 	struct bridge_mcast_stats __percpu *stats;
2411 	struct br_mcast_stats tdst;
2412 	int i;
2413 
2414 	memset(dest, 0, sizeof(*dest));
2415 	if (p)
2416 		stats = p->mcast_stats;
2417 	else
2418 		stats = br->mcast_stats;
2419 	if (WARN_ON(!stats))
2420 		return;
2421 
2422 	memset(&tdst, 0, sizeof(tdst));
2423 	for_each_possible_cpu(i) {
2424 		struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
2425 		struct br_mcast_stats temp;
2426 		unsigned int start;
2427 
2428 		do {
2429 			start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
2430 			memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
2431 		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
2432 
2433 		mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
2434 		mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
2435 		mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
2436 		mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
2437 		mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
2438 		mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
2439 		mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
2440 		tdst.igmp_parse_errors += temp.igmp_parse_errors;
2441 
2442 		mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
2443 		mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
2444 		mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
2445 		mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
2446 		mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
2447 		tdst.mld_parse_errors += temp.mld_parse_errors;
2448 	}
2449 	memcpy(dest, &tdst, sizeof(*dest));
2450 }
2451 
2452 int br_mdb_hash_init(struct net_bridge *br)
2453 {
2454 	return rhashtable_init(&br->mdb_hash_tbl, &br_mdb_rht_params);
2455 }
2456 
2457 void br_mdb_hash_fini(struct net_bridge *br)
2458 {
2459 	rhashtable_destroy(&br->mdb_hash_tbl);
2460 }
2461