xref: /openbmc/linux/net/bridge/br_multicast.c (revision d6fc9fcb)
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 
915 	ih = igmpv3_report_hdr(skb);
916 	num = ntohs(ih->ngrec);
917 	len = skb_transport_offset(skb) + sizeof(*ih);
918 
919 	for (i = 0; i < num; i++) {
920 		len += sizeof(*grec);
921 		if (!ip_mc_may_pull(skb, len))
922 			return -EINVAL;
923 
924 		grec = (void *)(skb->data + len - sizeof(*grec));
925 		group = grec->grec_mca;
926 		type = grec->grec_type;
927 
928 		len += ntohs(grec->grec_nsrcs) * 4;
929 		if (!ip_mc_may_pull(skb, len))
930 			return -EINVAL;
931 
932 		/* We treat this as an IGMPv2 report for now. */
933 		switch (type) {
934 		case IGMPV3_MODE_IS_INCLUDE:
935 		case IGMPV3_MODE_IS_EXCLUDE:
936 		case IGMPV3_CHANGE_TO_INCLUDE:
937 		case IGMPV3_CHANGE_TO_EXCLUDE:
938 		case IGMPV3_ALLOW_NEW_SOURCES:
939 		case IGMPV3_BLOCK_OLD_SOURCES:
940 			break;
941 
942 		default:
943 			continue;
944 		}
945 
946 		src = eth_hdr(skb)->h_source;
947 		if ((type == IGMPV3_CHANGE_TO_INCLUDE ||
948 		     type == IGMPV3_MODE_IS_INCLUDE) &&
949 		    ntohs(grec->grec_nsrcs) == 0) {
950 			br_ip4_multicast_leave_group(br, port, group, vid, src);
951 		} else {
952 			err = br_ip4_multicast_add_group(br, port, group, vid,
953 							 src);
954 			if (err)
955 				break;
956 		}
957 	}
958 
959 	return err;
960 }
961 
962 #if IS_ENABLED(CONFIG_IPV6)
963 static int br_ip6_multicast_mld2_report(struct net_bridge *br,
964 					struct net_bridge_port *port,
965 					struct sk_buff *skb,
966 					u16 vid)
967 {
968 	unsigned int nsrcs_offset;
969 	const unsigned char *src;
970 	struct icmp6hdr *icmp6h;
971 	struct mld2_grec *grec;
972 	unsigned int grec_len;
973 	int i;
974 	int len;
975 	int num;
976 	int err = 0;
977 
978 	if (!ipv6_mc_may_pull(skb, sizeof(*icmp6h)))
979 		return -EINVAL;
980 
981 	icmp6h = icmp6_hdr(skb);
982 	num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
983 	len = skb_transport_offset(skb) + sizeof(*icmp6h);
984 
985 	for (i = 0; i < num; i++) {
986 		__be16 *nsrcs, _nsrcs;
987 
988 		nsrcs_offset = len + offsetof(struct mld2_grec, grec_nsrcs);
989 
990 		if (skb_transport_offset(skb) + ipv6_transport_len(skb) <
991 		    nsrcs_offset + sizeof(_nsrcs))
992 			return -EINVAL;
993 
994 		nsrcs = skb_header_pointer(skb, nsrcs_offset,
995 					   sizeof(_nsrcs), &_nsrcs);
996 		if (!nsrcs)
997 			return -EINVAL;
998 
999 		grec_len = struct_size(grec, grec_src, ntohs(*nsrcs));
1000 
1001 		if (!ipv6_mc_may_pull(skb, len + grec_len))
1002 			return -EINVAL;
1003 
1004 		grec = (struct mld2_grec *)(skb->data + len);
1005 		len += grec_len;
1006 
1007 		/* We treat these as MLDv1 reports for now. */
1008 		switch (grec->grec_type) {
1009 		case MLD2_MODE_IS_INCLUDE:
1010 		case MLD2_MODE_IS_EXCLUDE:
1011 		case MLD2_CHANGE_TO_INCLUDE:
1012 		case MLD2_CHANGE_TO_EXCLUDE:
1013 		case MLD2_ALLOW_NEW_SOURCES:
1014 		case MLD2_BLOCK_OLD_SOURCES:
1015 			break;
1016 
1017 		default:
1018 			continue;
1019 		}
1020 
1021 		src = eth_hdr(skb)->h_source;
1022 		if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE ||
1023 		     grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
1024 		    ntohs(*nsrcs) == 0) {
1025 			br_ip6_multicast_leave_group(br, port, &grec->grec_mca,
1026 						     vid, src);
1027 		} else {
1028 			err = br_ip6_multicast_add_group(br, port,
1029 							 &grec->grec_mca, vid,
1030 							 src);
1031 			if (err)
1032 				break;
1033 		}
1034 	}
1035 
1036 	return err;
1037 }
1038 #endif
1039 
1040 static bool br_ip4_multicast_select_querier(struct net_bridge *br,
1041 					    struct net_bridge_port *port,
1042 					    __be32 saddr)
1043 {
1044 	if (!timer_pending(&br->ip4_own_query.timer) &&
1045 	    !timer_pending(&br->ip4_other_query.timer))
1046 		goto update;
1047 
1048 	if (!br->ip4_querier.addr.u.ip4)
1049 		goto update;
1050 
1051 	if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4))
1052 		goto update;
1053 
1054 	return false;
1055 
1056 update:
1057 	br->ip4_querier.addr.u.ip4 = saddr;
1058 
1059 	/* update protected by general multicast_lock by caller */
1060 	rcu_assign_pointer(br->ip4_querier.port, port);
1061 
1062 	return true;
1063 }
1064 
1065 #if IS_ENABLED(CONFIG_IPV6)
1066 static bool br_ip6_multicast_select_querier(struct net_bridge *br,
1067 					    struct net_bridge_port *port,
1068 					    struct in6_addr *saddr)
1069 {
1070 	if (!timer_pending(&br->ip6_own_query.timer) &&
1071 	    !timer_pending(&br->ip6_other_query.timer))
1072 		goto update;
1073 
1074 	if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0)
1075 		goto update;
1076 
1077 	return false;
1078 
1079 update:
1080 	br->ip6_querier.addr.u.ip6 = *saddr;
1081 
1082 	/* update protected by general multicast_lock by caller */
1083 	rcu_assign_pointer(br->ip6_querier.port, port);
1084 
1085 	return true;
1086 }
1087 #endif
1088 
1089 static bool br_multicast_select_querier(struct net_bridge *br,
1090 					struct net_bridge_port *port,
1091 					struct br_ip *saddr)
1092 {
1093 	switch (saddr->proto) {
1094 	case htons(ETH_P_IP):
1095 		return br_ip4_multicast_select_querier(br, port, saddr->u.ip4);
1096 #if IS_ENABLED(CONFIG_IPV6)
1097 	case htons(ETH_P_IPV6):
1098 		return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6);
1099 #endif
1100 	}
1101 
1102 	return false;
1103 }
1104 
1105 static void
1106 br_multicast_update_query_timer(struct net_bridge *br,
1107 				struct bridge_mcast_other_query *query,
1108 				unsigned long max_delay)
1109 {
1110 	if (!timer_pending(&query->timer))
1111 		query->delay_time = jiffies + max_delay;
1112 
1113 	mod_timer(&query->timer, jiffies + br->multicast_querier_interval);
1114 }
1115 
1116 static void br_port_mc_router_state_change(struct net_bridge_port *p,
1117 					   bool is_mc_router)
1118 {
1119 	struct switchdev_attr attr = {
1120 		.orig_dev = p->dev,
1121 		.id = SWITCHDEV_ATTR_ID_PORT_MROUTER,
1122 		.flags = SWITCHDEV_F_DEFER,
1123 		.u.mrouter = is_mc_router,
1124 	};
1125 
1126 	switchdev_port_attr_set(p->dev, &attr);
1127 }
1128 
1129 /*
1130  * Add port to router_list
1131  *  list is maintained ordered by pointer value
1132  *  and locked by br->multicast_lock and RCU
1133  */
1134 static void br_multicast_add_router(struct net_bridge *br,
1135 				    struct net_bridge_port *port)
1136 {
1137 	struct net_bridge_port *p;
1138 	struct hlist_node *slot = NULL;
1139 
1140 	if (!hlist_unhashed(&port->rlist))
1141 		return;
1142 
1143 	hlist_for_each_entry(p, &br->router_list, rlist) {
1144 		if ((unsigned long) port >= (unsigned long) p)
1145 			break;
1146 		slot = &p->rlist;
1147 	}
1148 
1149 	if (slot)
1150 		hlist_add_behind_rcu(&port->rlist, slot);
1151 	else
1152 		hlist_add_head_rcu(&port->rlist, &br->router_list);
1153 	br_rtr_notify(br->dev, port, RTM_NEWMDB);
1154 	br_port_mc_router_state_change(port, true);
1155 }
1156 
1157 static void br_multicast_mark_router(struct net_bridge *br,
1158 				     struct net_bridge_port *port)
1159 {
1160 	unsigned long now = jiffies;
1161 
1162 	if (!port) {
1163 		if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) {
1164 			if (!timer_pending(&br->multicast_router_timer))
1165 				br_mc_router_state_change(br, true);
1166 			mod_timer(&br->multicast_router_timer,
1167 				  now + br->multicast_querier_interval);
1168 		}
1169 		return;
1170 	}
1171 
1172 	if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
1173 	    port->multicast_router == MDB_RTR_TYPE_PERM)
1174 		return;
1175 
1176 	br_multicast_add_router(br, port);
1177 
1178 	mod_timer(&port->multicast_router_timer,
1179 		  now + br->multicast_querier_interval);
1180 }
1181 
1182 static void br_multicast_query_received(struct net_bridge *br,
1183 					struct net_bridge_port *port,
1184 					struct bridge_mcast_other_query *query,
1185 					struct br_ip *saddr,
1186 					unsigned long max_delay)
1187 {
1188 	if (!br_multicast_select_querier(br, port, saddr))
1189 		return;
1190 
1191 	br_multicast_update_query_timer(br, query, max_delay);
1192 	br_multicast_mark_router(br, port);
1193 }
1194 
1195 static void br_ip4_multicast_query(struct net_bridge *br,
1196 				   struct net_bridge_port *port,
1197 				   struct sk_buff *skb,
1198 				   u16 vid)
1199 {
1200 	unsigned int transport_len = ip_transport_len(skb);
1201 	const struct iphdr *iph = ip_hdr(skb);
1202 	struct igmphdr *ih = igmp_hdr(skb);
1203 	struct net_bridge_mdb_entry *mp;
1204 	struct igmpv3_query *ih3;
1205 	struct net_bridge_port_group *p;
1206 	struct net_bridge_port_group __rcu **pp;
1207 	struct br_ip saddr;
1208 	unsigned long max_delay;
1209 	unsigned long now = jiffies;
1210 	__be32 group;
1211 
1212 	spin_lock(&br->multicast_lock);
1213 	if (!netif_running(br->dev) ||
1214 	    (port && port->state == BR_STATE_DISABLED))
1215 		goto out;
1216 
1217 	group = ih->group;
1218 
1219 	if (transport_len == sizeof(*ih)) {
1220 		max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1221 
1222 		if (!max_delay) {
1223 			max_delay = 10 * HZ;
1224 			group = 0;
1225 		}
1226 	} else if (transport_len >= sizeof(*ih3)) {
1227 		ih3 = igmpv3_query_hdr(skb);
1228 		if (ih3->nsrcs)
1229 			goto out;
1230 
1231 		max_delay = ih3->code ?
1232 			    IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
1233 	} else {
1234 		goto out;
1235 	}
1236 
1237 	if (!group) {
1238 		saddr.proto = htons(ETH_P_IP);
1239 		saddr.u.ip4 = iph->saddr;
1240 
1241 		br_multicast_query_received(br, port, &br->ip4_other_query,
1242 					    &saddr, max_delay);
1243 		goto out;
1244 	}
1245 
1246 	mp = br_mdb_ip4_get(br, group, vid);
1247 	if (!mp)
1248 		goto out;
1249 
1250 	max_delay *= br->multicast_last_member_count;
1251 
1252 	if (mp->host_joined &&
1253 	    (timer_pending(&mp->timer) ?
1254 	     time_after(mp->timer.expires, now + max_delay) :
1255 	     try_to_del_timer_sync(&mp->timer) >= 0))
1256 		mod_timer(&mp->timer, now + max_delay);
1257 
1258 	for (pp = &mp->ports;
1259 	     (p = mlock_dereference(*pp, br)) != NULL;
1260 	     pp = &p->next) {
1261 		if (timer_pending(&p->timer) ?
1262 		    time_after(p->timer.expires, now + max_delay) :
1263 		    try_to_del_timer_sync(&p->timer) >= 0)
1264 			mod_timer(&p->timer, now + max_delay);
1265 	}
1266 
1267 out:
1268 	spin_unlock(&br->multicast_lock);
1269 }
1270 
1271 #if IS_ENABLED(CONFIG_IPV6)
1272 static int br_ip6_multicast_query(struct net_bridge *br,
1273 				  struct net_bridge_port *port,
1274 				  struct sk_buff *skb,
1275 				  u16 vid)
1276 {
1277 	unsigned int transport_len = ipv6_transport_len(skb);
1278 	const struct ipv6hdr *ip6h = ipv6_hdr(skb);
1279 	struct mld_msg *mld;
1280 	struct net_bridge_mdb_entry *mp;
1281 	struct mld2_query *mld2q;
1282 	struct net_bridge_port_group *p;
1283 	struct net_bridge_port_group __rcu **pp;
1284 	struct br_ip saddr;
1285 	unsigned long max_delay;
1286 	unsigned long now = jiffies;
1287 	unsigned int offset = skb_transport_offset(skb);
1288 	const struct in6_addr *group = NULL;
1289 	bool is_general_query;
1290 	int err = 0;
1291 
1292 	spin_lock(&br->multicast_lock);
1293 	if (!netif_running(br->dev) ||
1294 	    (port && port->state == BR_STATE_DISABLED))
1295 		goto out;
1296 
1297 	if (transport_len == sizeof(*mld)) {
1298 		if (!pskb_may_pull(skb, offset + sizeof(*mld))) {
1299 			err = -EINVAL;
1300 			goto out;
1301 		}
1302 		mld = (struct mld_msg *) icmp6_hdr(skb);
1303 		max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
1304 		if (max_delay)
1305 			group = &mld->mld_mca;
1306 	} else {
1307 		if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) {
1308 			err = -EINVAL;
1309 			goto out;
1310 		}
1311 		mld2q = (struct mld2_query *)icmp6_hdr(skb);
1312 		if (!mld2q->mld2q_nsrcs)
1313 			group = &mld2q->mld2q_mca;
1314 
1315 		max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
1316 	}
1317 
1318 	is_general_query = group && ipv6_addr_any(group);
1319 
1320 	if (is_general_query) {
1321 		saddr.proto = htons(ETH_P_IPV6);
1322 		saddr.u.ip6 = ip6h->saddr;
1323 
1324 		br_multicast_query_received(br, port, &br->ip6_other_query,
1325 					    &saddr, max_delay);
1326 		goto out;
1327 	} else if (!group) {
1328 		goto out;
1329 	}
1330 
1331 	mp = br_mdb_ip6_get(br, group, vid);
1332 	if (!mp)
1333 		goto out;
1334 
1335 	max_delay *= br->multicast_last_member_count;
1336 	if (mp->host_joined &&
1337 	    (timer_pending(&mp->timer) ?
1338 	     time_after(mp->timer.expires, now + max_delay) :
1339 	     try_to_del_timer_sync(&mp->timer) >= 0))
1340 		mod_timer(&mp->timer, now + max_delay);
1341 
1342 	for (pp = &mp->ports;
1343 	     (p = mlock_dereference(*pp, br)) != NULL;
1344 	     pp = &p->next) {
1345 		if (timer_pending(&p->timer) ?
1346 		    time_after(p->timer.expires, now + max_delay) :
1347 		    try_to_del_timer_sync(&p->timer) >= 0)
1348 			mod_timer(&p->timer, now + max_delay);
1349 	}
1350 
1351 out:
1352 	spin_unlock(&br->multicast_lock);
1353 	return err;
1354 }
1355 #endif
1356 
1357 static void
1358 br_multicast_leave_group(struct net_bridge *br,
1359 			 struct net_bridge_port *port,
1360 			 struct br_ip *group,
1361 			 struct bridge_mcast_other_query *other_query,
1362 			 struct bridge_mcast_own_query *own_query,
1363 			 const unsigned char *src)
1364 {
1365 	struct net_bridge_mdb_entry *mp;
1366 	struct net_bridge_port_group *p;
1367 	unsigned long now;
1368 	unsigned long time;
1369 
1370 	spin_lock(&br->multicast_lock);
1371 	if (!netif_running(br->dev) ||
1372 	    (port && port->state == BR_STATE_DISABLED))
1373 		goto out;
1374 
1375 	mp = br_mdb_ip_get(br, group);
1376 	if (!mp)
1377 		goto out;
1378 
1379 	if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
1380 		struct net_bridge_port_group __rcu **pp;
1381 
1382 		for (pp = &mp->ports;
1383 		     (p = mlock_dereference(*pp, br)) != NULL;
1384 		     pp = &p->next) {
1385 			if (!br_port_group_equal(p, port, src))
1386 				continue;
1387 
1388 			rcu_assign_pointer(*pp, p->next);
1389 			hlist_del_init(&p->mglist);
1390 			del_timer(&p->timer);
1391 			kfree_rcu(p, rcu);
1392 			br_mdb_notify(br->dev, port, group, RTM_DELMDB,
1393 				      p->flags);
1394 
1395 			if (!mp->ports && !mp->host_joined &&
1396 			    netif_running(br->dev))
1397 				mod_timer(&mp->timer, jiffies);
1398 		}
1399 		goto out;
1400 	}
1401 
1402 	if (timer_pending(&other_query->timer))
1403 		goto out;
1404 
1405 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER)) {
1406 		__br_multicast_send_query(br, port, &mp->addr);
1407 
1408 		time = jiffies + br->multicast_last_member_count *
1409 				 br->multicast_last_member_interval;
1410 
1411 		mod_timer(&own_query->timer, time);
1412 
1413 		for (p = mlock_dereference(mp->ports, br);
1414 		     p != NULL;
1415 		     p = mlock_dereference(p->next, br)) {
1416 			if (!br_port_group_equal(p, port, src))
1417 				continue;
1418 
1419 			if (!hlist_unhashed(&p->mglist) &&
1420 			    (timer_pending(&p->timer) ?
1421 			     time_after(p->timer.expires, time) :
1422 			     try_to_del_timer_sync(&p->timer) >= 0)) {
1423 				mod_timer(&p->timer, time);
1424 			}
1425 
1426 			break;
1427 		}
1428 	}
1429 
1430 	now = jiffies;
1431 	time = now + br->multicast_last_member_count *
1432 		     br->multicast_last_member_interval;
1433 
1434 	if (!port) {
1435 		if (mp->host_joined &&
1436 		    (timer_pending(&mp->timer) ?
1437 		     time_after(mp->timer.expires, time) :
1438 		     try_to_del_timer_sync(&mp->timer) >= 0)) {
1439 			mod_timer(&mp->timer, time);
1440 		}
1441 
1442 		goto out;
1443 	}
1444 
1445 	for (p = mlock_dereference(mp->ports, br);
1446 	     p != NULL;
1447 	     p = mlock_dereference(p->next, br)) {
1448 		if (p->port != port)
1449 			continue;
1450 
1451 		if (!hlist_unhashed(&p->mglist) &&
1452 		    (timer_pending(&p->timer) ?
1453 		     time_after(p->timer.expires, time) :
1454 		     try_to_del_timer_sync(&p->timer) >= 0)) {
1455 			mod_timer(&p->timer, time);
1456 		}
1457 
1458 		break;
1459 	}
1460 out:
1461 	spin_unlock(&br->multicast_lock);
1462 }
1463 
1464 static void br_ip4_multicast_leave_group(struct net_bridge *br,
1465 					 struct net_bridge_port *port,
1466 					 __be32 group,
1467 					 __u16 vid,
1468 					 const unsigned char *src)
1469 {
1470 	struct br_ip br_group;
1471 	struct bridge_mcast_own_query *own_query;
1472 
1473 	if (ipv4_is_local_multicast(group))
1474 		return;
1475 
1476 	own_query = port ? &port->ip4_own_query : &br->ip4_own_query;
1477 
1478 	memset(&br_group, 0, sizeof(br_group));
1479 	br_group.u.ip4 = group;
1480 	br_group.proto = htons(ETH_P_IP);
1481 	br_group.vid = vid;
1482 
1483 	br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
1484 				 own_query, src);
1485 }
1486 
1487 #if IS_ENABLED(CONFIG_IPV6)
1488 static void br_ip6_multicast_leave_group(struct net_bridge *br,
1489 					 struct net_bridge_port *port,
1490 					 const struct in6_addr *group,
1491 					 __u16 vid,
1492 					 const unsigned char *src)
1493 {
1494 	struct br_ip br_group;
1495 	struct bridge_mcast_own_query *own_query;
1496 
1497 	if (ipv6_addr_is_ll_all_nodes(group))
1498 		return;
1499 
1500 	own_query = port ? &port->ip6_own_query : &br->ip6_own_query;
1501 
1502 	memset(&br_group, 0, sizeof(br_group));
1503 	br_group.u.ip6 = *group;
1504 	br_group.proto = htons(ETH_P_IPV6);
1505 	br_group.vid = vid;
1506 
1507 	br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
1508 				 own_query, src);
1509 }
1510 #endif
1511 
1512 static void br_multicast_err_count(const struct net_bridge *br,
1513 				   const struct net_bridge_port *p,
1514 				   __be16 proto)
1515 {
1516 	struct bridge_mcast_stats __percpu *stats;
1517 	struct bridge_mcast_stats *pstats;
1518 
1519 	if (!br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
1520 		return;
1521 
1522 	if (p)
1523 		stats = p->mcast_stats;
1524 	else
1525 		stats = br->mcast_stats;
1526 	if (WARN_ON(!stats))
1527 		return;
1528 
1529 	pstats = this_cpu_ptr(stats);
1530 
1531 	u64_stats_update_begin(&pstats->syncp);
1532 	switch (proto) {
1533 	case htons(ETH_P_IP):
1534 		pstats->mstats.igmp_parse_errors++;
1535 		break;
1536 #if IS_ENABLED(CONFIG_IPV6)
1537 	case htons(ETH_P_IPV6):
1538 		pstats->mstats.mld_parse_errors++;
1539 		break;
1540 #endif
1541 	}
1542 	u64_stats_update_end(&pstats->syncp);
1543 }
1544 
1545 static void br_multicast_pim(struct net_bridge *br,
1546 			     struct net_bridge_port *port,
1547 			     const struct sk_buff *skb)
1548 {
1549 	unsigned int offset = skb_transport_offset(skb);
1550 	struct pimhdr *pimhdr, _pimhdr;
1551 
1552 	pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr);
1553 	if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION ||
1554 	    pim_hdr_type(pimhdr) != PIM_TYPE_HELLO)
1555 		return;
1556 
1557 	br_multicast_mark_router(br, port);
1558 }
1559 
1560 static int br_ip4_multicast_mrd_rcv(struct net_bridge *br,
1561 				    struct net_bridge_port *port,
1562 				    struct sk_buff *skb)
1563 {
1564 	if (ip_hdr(skb)->protocol != IPPROTO_IGMP ||
1565 	    igmp_hdr(skb)->type != IGMP_MRDISC_ADV)
1566 		return -ENOMSG;
1567 
1568 	br_multicast_mark_router(br, port);
1569 
1570 	return 0;
1571 }
1572 
1573 static int br_multicast_ipv4_rcv(struct net_bridge *br,
1574 				 struct net_bridge_port *port,
1575 				 struct sk_buff *skb,
1576 				 u16 vid)
1577 {
1578 	const unsigned char *src;
1579 	struct igmphdr *ih;
1580 	int err;
1581 
1582 	err = ip_mc_check_igmp(skb);
1583 
1584 	if (err == -ENOMSG) {
1585 		if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) {
1586 			BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1587 		} else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) {
1588 			if (ip_hdr(skb)->protocol == IPPROTO_PIM)
1589 				br_multicast_pim(br, port, skb);
1590 		} else if (ipv4_is_all_snoopers(ip_hdr(skb)->daddr)) {
1591 			br_ip4_multicast_mrd_rcv(br, port, skb);
1592 		}
1593 
1594 		return 0;
1595 	} else if (err < 0) {
1596 		br_multicast_err_count(br, port, skb->protocol);
1597 		return err;
1598 	}
1599 
1600 	ih = igmp_hdr(skb);
1601 	src = eth_hdr(skb)->h_source;
1602 	BR_INPUT_SKB_CB(skb)->igmp = ih->type;
1603 
1604 	switch (ih->type) {
1605 	case IGMP_HOST_MEMBERSHIP_REPORT:
1606 	case IGMPV2_HOST_MEMBERSHIP_REPORT:
1607 		BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1608 		err = br_ip4_multicast_add_group(br, port, ih->group, vid, src);
1609 		break;
1610 	case IGMPV3_HOST_MEMBERSHIP_REPORT:
1611 		err = br_ip4_multicast_igmp3_report(br, port, skb, vid);
1612 		break;
1613 	case IGMP_HOST_MEMBERSHIP_QUERY:
1614 		br_ip4_multicast_query(br, port, skb, vid);
1615 		break;
1616 	case IGMP_HOST_LEAVE_MESSAGE:
1617 		br_ip4_multicast_leave_group(br, port, ih->group, vid, src);
1618 		break;
1619 	}
1620 
1621 	br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
1622 			   BR_MCAST_DIR_RX);
1623 
1624 	return err;
1625 }
1626 
1627 #if IS_ENABLED(CONFIG_IPV6)
1628 static int br_ip6_multicast_mrd_rcv(struct net_bridge *br,
1629 				    struct net_bridge_port *port,
1630 				    struct sk_buff *skb)
1631 {
1632 	int ret;
1633 
1634 	if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6)
1635 		return -ENOMSG;
1636 
1637 	ret = ipv6_mc_check_icmpv6(skb);
1638 	if (ret < 0)
1639 		return ret;
1640 
1641 	if (icmp6_hdr(skb)->icmp6_type != ICMPV6_MRDISC_ADV)
1642 		return -ENOMSG;
1643 
1644 	br_multicast_mark_router(br, port);
1645 
1646 	return 0;
1647 }
1648 
1649 static int br_multicast_ipv6_rcv(struct net_bridge *br,
1650 				 struct net_bridge_port *port,
1651 				 struct sk_buff *skb,
1652 				 u16 vid)
1653 {
1654 	const unsigned char *src;
1655 	struct mld_msg *mld;
1656 	int err;
1657 
1658 	err = ipv6_mc_check_mld(skb);
1659 
1660 	if (err == -ENOMSG) {
1661 		if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
1662 			BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1663 
1664 		if (ipv6_addr_is_all_snoopers(&ipv6_hdr(skb)->daddr)) {
1665 			err = br_ip6_multicast_mrd_rcv(br, port, skb);
1666 
1667 			if (err < 0 && err != -ENOMSG) {
1668 				br_multicast_err_count(br, port, skb->protocol);
1669 				return err;
1670 			}
1671 		}
1672 
1673 		return 0;
1674 	} else if (err < 0) {
1675 		br_multicast_err_count(br, port, skb->protocol);
1676 		return err;
1677 	}
1678 
1679 	mld = (struct mld_msg *)skb_transport_header(skb);
1680 	BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type;
1681 
1682 	switch (mld->mld_type) {
1683 	case ICMPV6_MGM_REPORT:
1684 		src = eth_hdr(skb)->h_source;
1685 		BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1686 		err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid,
1687 						 src);
1688 		break;
1689 	case ICMPV6_MLD2_REPORT:
1690 		err = br_ip6_multicast_mld2_report(br, port, skb, vid);
1691 		break;
1692 	case ICMPV6_MGM_QUERY:
1693 		err = br_ip6_multicast_query(br, port, skb, vid);
1694 		break;
1695 	case ICMPV6_MGM_REDUCTION:
1696 		src = eth_hdr(skb)->h_source;
1697 		br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src);
1698 		break;
1699 	}
1700 
1701 	br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
1702 			   BR_MCAST_DIR_RX);
1703 
1704 	return err;
1705 }
1706 #endif
1707 
1708 int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1709 		     struct sk_buff *skb, u16 vid)
1710 {
1711 	int ret = 0;
1712 
1713 	BR_INPUT_SKB_CB(skb)->igmp = 0;
1714 	BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1715 
1716 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
1717 		return 0;
1718 
1719 	switch (skb->protocol) {
1720 	case htons(ETH_P_IP):
1721 		ret = br_multicast_ipv4_rcv(br, port, skb, vid);
1722 		break;
1723 #if IS_ENABLED(CONFIG_IPV6)
1724 	case htons(ETH_P_IPV6):
1725 		ret = br_multicast_ipv6_rcv(br, port, skb, vid);
1726 		break;
1727 #endif
1728 	}
1729 
1730 	return ret;
1731 }
1732 
1733 static void br_multicast_query_expired(struct net_bridge *br,
1734 				       struct bridge_mcast_own_query *query,
1735 				       struct bridge_mcast_querier *querier)
1736 {
1737 	spin_lock(&br->multicast_lock);
1738 	if (query->startup_sent < br->multicast_startup_query_count)
1739 		query->startup_sent++;
1740 
1741 	RCU_INIT_POINTER(querier->port, NULL);
1742 	br_multicast_send_query(br, NULL, query);
1743 	spin_unlock(&br->multicast_lock);
1744 }
1745 
1746 static void br_ip4_multicast_query_expired(struct timer_list *t)
1747 {
1748 	struct net_bridge *br = from_timer(br, t, ip4_own_query.timer);
1749 
1750 	br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier);
1751 }
1752 
1753 #if IS_ENABLED(CONFIG_IPV6)
1754 static void br_ip6_multicast_query_expired(struct timer_list *t)
1755 {
1756 	struct net_bridge *br = from_timer(br, t, ip6_own_query.timer);
1757 
1758 	br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier);
1759 }
1760 #endif
1761 
1762 void br_multicast_init(struct net_bridge *br)
1763 {
1764 	br->hash_max = BR_MULTICAST_DEFAULT_HASH_MAX;
1765 
1766 	br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
1767 	br->multicast_last_member_count = 2;
1768 	br->multicast_startup_query_count = 2;
1769 
1770 	br->multicast_last_member_interval = HZ;
1771 	br->multicast_query_response_interval = 10 * HZ;
1772 	br->multicast_startup_query_interval = 125 * HZ / 4;
1773 	br->multicast_query_interval = 125 * HZ;
1774 	br->multicast_querier_interval = 255 * HZ;
1775 	br->multicast_membership_interval = 260 * HZ;
1776 
1777 	br->ip4_other_query.delay_time = 0;
1778 	br->ip4_querier.port = NULL;
1779 	br->multicast_igmp_version = 2;
1780 #if IS_ENABLED(CONFIG_IPV6)
1781 	br->multicast_mld_version = 1;
1782 	br->ip6_other_query.delay_time = 0;
1783 	br->ip6_querier.port = NULL;
1784 #endif
1785 	br_opt_toggle(br, BROPT_MULTICAST_ENABLED, true);
1786 	br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true);
1787 
1788 	spin_lock_init(&br->multicast_lock);
1789 	timer_setup(&br->multicast_router_timer,
1790 		    br_multicast_local_router_expired, 0);
1791 	timer_setup(&br->ip4_other_query.timer,
1792 		    br_ip4_multicast_querier_expired, 0);
1793 	timer_setup(&br->ip4_own_query.timer,
1794 		    br_ip4_multicast_query_expired, 0);
1795 #if IS_ENABLED(CONFIG_IPV6)
1796 	timer_setup(&br->ip6_other_query.timer,
1797 		    br_ip6_multicast_querier_expired, 0);
1798 	timer_setup(&br->ip6_own_query.timer,
1799 		    br_ip6_multicast_query_expired, 0);
1800 #endif
1801 	INIT_HLIST_HEAD(&br->mdb_list);
1802 }
1803 
1804 static void br_ip4_multicast_join_snoopers(struct net_bridge *br)
1805 {
1806 	struct in_device *in_dev = in_dev_get(br->dev);
1807 
1808 	if (!in_dev)
1809 		return;
1810 
1811 	__ip_mc_inc_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
1812 	in_dev_put(in_dev);
1813 }
1814 
1815 #if IS_ENABLED(CONFIG_IPV6)
1816 static void br_ip6_multicast_join_snoopers(struct net_bridge *br)
1817 {
1818 	struct in6_addr addr;
1819 
1820 	ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
1821 	ipv6_dev_mc_inc(br->dev, &addr);
1822 }
1823 #else
1824 static inline void br_ip6_multicast_join_snoopers(struct net_bridge *br)
1825 {
1826 }
1827 #endif
1828 
1829 static void br_multicast_join_snoopers(struct net_bridge *br)
1830 {
1831 	br_ip4_multicast_join_snoopers(br);
1832 	br_ip6_multicast_join_snoopers(br);
1833 }
1834 
1835 static void br_ip4_multicast_leave_snoopers(struct net_bridge *br)
1836 {
1837 	struct in_device *in_dev = in_dev_get(br->dev);
1838 
1839 	if (WARN_ON(!in_dev))
1840 		return;
1841 
1842 	__ip_mc_dec_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
1843 	in_dev_put(in_dev);
1844 }
1845 
1846 #if IS_ENABLED(CONFIG_IPV6)
1847 static void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
1848 {
1849 	struct in6_addr addr;
1850 
1851 	ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
1852 	ipv6_dev_mc_dec(br->dev, &addr);
1853 }
1854 #else
1855 static inline void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
1856 {
1857 }
1858 #endif
1859 
1860 static void br_multicast_leave_snoopers(struct net_bridge *br)
1861 {
1862 	br_ip4_multicast_leave_snoopers(br);
1863 	br_ip6_multicast_leave_snoopers(br);
1864 }
1865 
1866 static void __br_multicast_open(struct net_bridge *br,
1867 				struct bridge_mcast_own_query *query)
1868 {
1869 	query->startup_sent = 0;
1870 
1871 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
1872 		return;
1873 
1874 	mod_timer(&query->timer, jiffies);
1875 }
1876 
1877 void br_multicast_open(struct net_bridge *br)
1878 {
1879 	if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
1880 		br_multicast_join_snoopers(br);
1881 
1882 	__br_multicast_open(br, &br->ip4_own_query);
1883 #if IS_ENABLED(CONFIG_IPV6)
1884 	__br_multicast_open(br, &br->ip6_own_query);
1885 #endif
1886 }
1887 
1888 void br_multicast_stop(struct net_bridge *br)
1889 {
1890 	del_timer_sync(&br->multicast_router_timer);
1891 	del_timer_sync(&br->ip4_other_query.timer);
1892 	del_timer_sync(&br->ip4_own_query.timer);
1893 #if IS_ENABLED(CONFIG_IPV6)
1894 	del_timer_sync(&br->ip6_other_query.timer);
1895 	del_timer_sync(&br->ip6_own_query.timer);
1896 #endif
1897 
1898 	if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
1899 		br_multicast_leave_snoopers(br);
1900 }
1901 
1902 void br_multicast_dev_del(struct net_bridge *br)
1903 {
1904 	struct net_bridge_mdb_entry *mp;
1905 	struct hlist_node *tmp;
1906 
1907 	spin_lock_bh(&br->multicast_lock);
1908 	hlist_for_each_entry_safe(mp, tmp, &br->mdb_list, mdb_node) {
1909 		del_timer(&mp->timer);
1910 		rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode,
1911 				       br_mdb_rht_params);
1912 		hlist_del_rcu(&mp->mdb_node);
1913 		kfree_rcu(mp, rcu);
1914 	}
1915 	spin_unlock_bh(&br->multicast_lock);
1916 
1917 	rcu_barrier();
1918 }
1919 
1920 int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1921 {
1922 	int err = -EINVAL;
1923 
1924 	spin_lock_bh(&br->multicast_lock);
1925 
1926 	switch (val) {
1927 	case MDB_RTR_TYPE_DISABLED:
1928 	case MDB_RTR_TYPE_PERM:
1929 		br_mc_router_state_change(br, val == MDB_RTR_TYPE_PERM);
1930 		del_timer(&br->multicast_router_timer);
1931 		br->multicast_router = val;
1932 		err = 0;
1933 		break;
1934 	case MDB_RTR_TYPE_TEMP_QUERY:
1935 		if (br->multicast_router != MDB_RTR_TYPE_TEMP_QUERY)
1936 			br_mc_router_state_change(br, false);
1937 		br->multicast_router = val;
1938 		err = 0;
1939 		break;
1940 	}
1941 
1942 	spin_unlock_bh(&br->multicast_lock);
1943 
1944 	return err;
1945 }
1946 
1947 static void __del_port_router(struct net_bridge_port *p)
1948 {
1949 	if (hlist_unhashed(&p->rlist))
1950 		return;
1951 	hlist_del_init_rcu(&p->rlist);
1952 	br_rtr_notify(p->br->dev, p, RTM_DELMDB);
1953 	br_port_mc_router_state_change(p, false);
1954 
1955 	/* don't allow timer refresh */
1956 	if (p->multicast_router == MDB_RTR_TYPE_TEMP)
1957 		p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
1958 }
1959 
1960 int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1961 {
1962 	struct net_bridge *br = p->br;
1963 	unsigned long now = jiffies;
1964 	int err = -EINVAL;
1965 
1966 	spin_lock(&br->multicast_lock);
1967 	if (p->multicast_router == val) {
1968 		/* Refresh the temp router port timer */
1969 		if (p->multicast_router == MDB_RTR_TYPE_TEMP)
1970 			mod_timer(&p->multicast_router_timer,
1971 				  now + br->multicast_querier_interval);
1972 		err = 0;
1973 		goto unlock;
1974 	}
1975 	switch (val) {
1976 	case MDB_RTR_TYPE_DISABLED:
1977 		p->multicast_router = MDB_RTR_TYPE_DISABLED;
1978 		__del_port_router(p);
1979 		del_timer(&p->multicast_router_timer);
1980 		break;
1981 	case MDB_RTR_TYPE_TEMP_QUERY:
1982 		p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
1983 		__del_port_router(p);
1984 		break;
1985 	case MDB_RTR_TYPE_PERM:
1986 		p->multicast_router = MDB_RTR_TYPE_PERM;
1987 		del_timer(&p->multicast_router_timer);
1988 		br_multicast_add_router(br, p);
1989 		break;
1990 	case MDB_RTR_TYPE_TEMP:
1991 		p->multicast_router = MDB_RTR_TYPE_TEMP;
1992 		br_multicast_mark_router(br, p);
1993 		break;
1994 	default:
1995 		goto unlock;
1996 	}
1997 	err = 0;
1998 unlock:
1999 	spin_unlock(&br->multicast_lock);
2000 
2001 	return err;
2002 }
2003 
2004 static void br_multicast_start_querier(struct net_bridge *br,
2005 				       struct bridge_mcast_own_query *query)
2006 {
2007 	struct net_bridge_port *port;
2008 
2009 	__br_multicast_open(br, query);
2010 
2011 	rcu_read_lock();
2012 	list_for_each_entry_rcu(port, &br->port_list, list) {
2013 		if (port->state == BR_STATE_DISABLED ||
2014 		    port->state == BR_STATE_BLOCKING)
2015 			continue;
2016 
2017 		if (query == &br->ip4_own_query)
2018 			br_multicast_enable(&port->ip4_own_query);
2019 #if IS_ENABLED(CONFIG_IPV6)
2020 		else
2021 			br_multicast_enable(&port->ip6_own_query);
2022 #endif
2023 	}
2024 	rcu_read_unlock();
2025 }
2026 
2027 int br_multicast_toggle(struct net_bridge *br, unsigned long val)
2028 {
2029 	struct net_bridge_port *port;
2030 
2031 	spin_lock_bh(&br->multicast_lock);
2032 	if (!!br_opt_get(br, BROPT_MULTICAST_ENABLED) == !!val)
2033 		goto unlock;
2034 
2035 	br_mc_disabled_update(br->dev, val);
2036 	br_opt_toggle(br, BROPT_MULTICAST_ENABLED, !!val);
2037 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) {
2038 		br_multicast_leave_snoopers(br);
2039 		goto unlock;
2040 	}
2041 
2042 	if (!netif_running(br->dev))
2043 		goto unlock;
2044 
2045 	br_multicast_open(br);
2046 	list_for_each_entry(port, &br->port_list, list)
2047 		__br_multicast_enable_port(port);
2048 
2049 unlock:
2050 	spin_unlock_bh(&br->multicast_lock);
2051 
2052 	return 0;
2053 }
2054 
2055 bool br_multicast_enabled(const struct net_device *dev)
2056 {
2057 	struct net_bridge *br = netdev_priv(dev);
2058 
2059 	return !!br_opt_get(br, BROPT_MULTICAST_ENABLED);
2060 }
2061 EXPORT_SYMBOL_GPL(br_multicast_enabled);
2062 
2063 bool br_multicast_router(const struct net_device *dev)
2064 {
2065 	struct net_bridge *br = netdev_priv(dev);
2066 	bool is_router;
2067 
2068 	spin_lock_bh(&br->multicast_lock);
2069 	is_router = br_multicast_is_router(br);
2070 	spin_unlock_bh(&br->multicast_lock);
2071 	return is_router;
2072 }
2073 EXPORT_SYMBOL_GPL(br_multicast_router);
2074 
2075 int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
2076 {
2077 	unsigned long max_delay;
2078 
2079 	val = !!val;
2080 
2081 	spin_lock_bh(&br->multicast_lock);
2082 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER) == val)
2083 		goto unlock;
2084 
2085 	br_opt_toggle(br, BROPT_MULTICAST_QUERIER, !!val);
2086 	if (!val)
2087 		goto unlock;
2088 
2089 	max_delay = br->multicast_query_response_interval;
2090 
2091 	if (!timer_pending(&br->ip4_other_query.timer))
2092 		br->ip4_other_query.delay_time = jiffies + max_delay;
2093 
2094 	br_multicast_start_querier(br, &br->ip4_own_query);
2095 
2096 #if IS_ENABLED(CONFIG_IPV6)
2097 	if (!timer_pending(&br->ip6_other_query.timer))
2098 		br->ip6_other_query.delay_time = jiffies + max_delay;
2099 
2100 	br_multicast_start_querier(br, &br->ip6_own_query);
2101 #endif
2102 
2103 unlock:
2104 	spin_unlock_bh(&br->multicast_lock);
2105 
2106 	return 0;
2107 }
2108 
2109 int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val)
2110 {
2111 	/* Currently we support only version 2 and 3 */
2112 	switch (val) {
2113 	case 2:
2114 	case 3:
2115 		break;
2116 	default:
2117 		return -EINVAL;
2118 	}
2119 
2120 	spin_lock_bh(&br->multicast_lock);
2121 	br->multicast_igmp_version = val;
2122 	spin_unlock_bh(&br->multicast_lock);
2123 
2124 	return 0;
2125 }
2126 
2127 #if IS_ENABLED(CONFIG_IPV6)
2128 int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val)
2129 {
2130 	/* Currently we support version 1 and 2 */
2131 	switch (val) {
2132 	case 1:
2133 	case 2:
2134 		break;
2135 	default:
2136 		return -EINVAL;
2137 	}
2138 
2139 	spin_lock_bh(&br->multicast_lock);
2140 	br->multicast_mld_version = val;
2141 	spin_unlock_bh(&br->multicast_lock);
2142 
2143 	return 0;
2144 }
2145 #endif
2146 
2147 /**
2148  * br_multicast_list_adjacent - Returns snooped multicast addresses
2149  * @dev:	The bridge port adjacent to which to retrieve addresses
2150  * @br_ip_list:	The list to store found, snooped multicast IP addresses in
2151  *
2152  * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
2153  * snooping feature on all bridge ports of dev's bridge device, excluding
2154  * the addresses from dev itself.
2155  *
2156  * Returns the number of items added to br_ip_list.
2157  *
2158  * Notes:
2159  * - br_ip_list needs to be initialized by caller
2160  * - br_ip_list might contain duplicates in the end
2161  *   (needs to be taken care of by caller)
2162  * - br_ip_list needs to be freed by caller
2163  */
2164 int br_multicast_list_adjacent(struct net_device *dev,
2165 			       struct list_head *br_ip_list)
2166 {
2167 	struct net_bridge *br;
2168 	struct net_bridge_port *port;
2169 	struct net_bridge_port_group *group;
2170 	struct br_ip_list *entry;
2171 	int count = 0;
2172 
2173 	rcu_read_lock();
2174 	if (!br_ip_list || !netif_is_bridge_port(dev))
2175 		goto unlock;
2176 
2177 	port = br_port_get_rcu(dev);
2178 	if (!port || !port->br)
2179 		goto unlock;
2180 
2181 	br = port->br;
2182 
2183 	list_for_each_entry_rcu(port, &br->port_list, list) {
2184 		if (!port->dev || port->dev == dev)
2185 			continue;
2186 
2187 		hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
2188 			entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
2189 			if (!entry)
2190 				goto unlock;
2191 
2192 			entry->addr = group->addr;
2193 			list_add(&entry->list, br_ip_list);
2194 			count++;
2195 		}
2196 	}
2197 
2198 unlock:
2199 	rcu_read_unlock();
2200 	return count;
2201 }
2202 EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
2203 
2204 /**
2205  * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2206  * @dev: The bridge port providing the bridge on which to check for a querier
2207  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2208  *
2209  * Checks whether the given interface has a bridge on top and if so returns
2210  * true if a valid querier exists anywhere on the bridged link layer.
2211  * Otherwise returns false.
2212  */
2213 bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
2214 {
2215 	struct net_bridge *br;
2216 	struct net_bridge_port *port;
2217 	struct ethhdr eth;
2218 	bool ret = false;
2219 
2220 	rcu_read_lock();
2221 	if (!netif_is_bridge_port(dev))
2222 		goto unlock;
2223 
2224 	port = br_port_get_rcu(dev);
2225 	if (!port || !port->br)
2226 		goto unlock;
2227 
2228 	br = port->br;
2229 
2230 	memset(&eth, 0, sizeof(eth));
2231 	eth.h_proto = htons(proto);
2232 
2233 	ret = br_multicast_querier_exists(br, &eth);
2234 
2235 unlock:
2236 	rcu_read_unlock();
2237 	return ret;
2238 }
2239 EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
2240 
2241 /**
2242  * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
2243  * @dev: The bridge port adjacent to which to check for a querier
2244  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2245  *
2246  * Checks whether the given interface has a bridge on top and if so returns
2247  * true if a selected querier is behind one of the other ports of this
2248  * bridge. Otherwise returns false.
2249  */
2250 bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
2251 {
2252 	struct net_bridge *br;
2253 	struct net_bridge_port *port;
2254 	bool ret = false;
2255 
2256 	rcu_read_lock();
2257 	if (!netif_is_bridge_port(dev))
2258 		goto unlock;
2259 
2260 	port = br_port_get_rcu(dev);
2261 	if (!port || !port->br)
2262 		goto unlock;
2263 
2264 	br = port->br;
2265 
2266 	switch (proto) {
2267 	case ETH_P_IP:
2268 		if (!timer_pending(&br->ip4_other_query.timer) ||
2269 		    rcu_dereference(br->ip4_querier.port) == port)
2270 			goto unlock;
2271 		break;
2272 #if IS_ENABLED(CONFIG_IPV6)
2273 	case ETH_P_IPV6:
2274 		if (!timer_pending(&br->ip6_other_query.timer) ||
2275 		    rcu_dereference(br->ip6_querier.port) == port)
2276 			goto unlock;
2277 		break;
2278 #endif
2279 	default:
2280 		goto unlock;
2281 	}
2282 
2283 	ret = true;
2284 unlock:
2285 	rcu_read_unlock();
2286 	return ret;
2287 }
2288 EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
2289 
2290 static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
2291 			       const struct sk_buff *skb, u8 type, u8 dir)
2292 {
2293 	struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
2294 	__be16 proto = skb->protocol;
2295 	unsigned int t_len;
2296 
2297 	u64_stats_update_begin(&pstats->syncp);
2298 	switch (proto) {
2299 	case htons(ETH_P_IP):
2300 		t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
2301 		switch (type) {
2302 		case IGMP_HOST_MEMBERSHIP_REPORT:
2303 			pstats->mstats.igmp_v1reports[dir]++;
2304 			break;
2305 		case IGMPV2_HOST_MEMBERSHIP_REPORT:
2306 			pstats->mstats.igmp_v2reports[dir]++;
2307 			break;
2308 		case IGMPV3_HOST_MEMBERSHIP_REPORT:
2309 			pstats->mstats.igmp_v3reports[dir]++;
2310 			break;
2311 		case IGMP_HOST_MEMBERSHIP_QUERY:
2312 			if (t_len != sizeof(struct igmphdr)) {
2313 				pstats->mstats.igmp_v3queries[dir]++;
2314 			} else {
2315 				unsigned int offset = skb_transport_offset(skb);
2316 				struct igmphdr *ih, _ihdr;
2317 
2318 				ih = skb_header_pointer(skb, offset,
2319 							sizeof(_ihdr), &_ihdr);
2320 				if (!ih)
2321 					break;
2322 				if (!ih->code)
2323 					pstats->mstats.igmp_v1queries[dir]++;
2324 				else
2325 					pstats->mstats.igmp_v2queries[dir]++;
2326 			}
2327 			break;
2328 		case IGMP_HOST_LEAVE_MESSAGE:
2329 			pstats->mstats.igmp_leaves[dir]++;
2330 			break;
2331 		}
2332 		break;
2333 #if IS_ENABLED(CONFIG_IPV6)
2334 	case htons(ETH_P_IPV6):
2335 		t_len = ntohs(ipv6_hdr(skb)->payload_len) +
2336 			sizeof(struct ipv6hdr);
2337 		t_len -= skb_network_header_len(skb);
2338 		switch (type) {
2339 		case ICMPV6_MGM_REPORT:
2340 			pstats->mstats.mld_v1reports[dir]++;
2341 			break;
2342 		case ICMPV6_MLD2_REPORT:
2343 			pstats->mstats.mld_v2reports[dir]++;
2344 			break;
2345 		case ICMPV6_MGM_QUERY:
2346 			if (t_len != sizeof(struct mld_msg))
2347 				pstats->mstats.mld_v2queries[dir]++;
2348 			else
2349 				pstats->mstats.mld_v1queries[dir]++;
2350 			break;
2351 		case ICMPV6_MGM_REDUCTION:
2352 			pstats->mstats.mld_leaves[dir]++;
2353 			break;
2354 		}
2355 		break;
2356 #endif /* CONFIG_IPV6 */
2357 	}
2358 	u64_stats_update_end(&pstats->syncp);
2359 }
2360 
2361 void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
2362 			const struct sk_buff *skb, u8 type, u8 dir)
2363 {
2364 	struct bridge_mcast_stats __percpu *stats;
2365 
2366 	/* if multicast_disabled is true then igmp type can't be set */
2367 	if (!type || !br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
2368 		return;
2369 
2370 	if (p)
2371 		stats = p->mcast_stats;
2372 	else
2373 		stats = br->mcast_stats;
2374 	if (WARN_ON(!stats))
2375 		return;
2376 
2377 	br_mcast_stats_add(stats, skb, type, dir);
2378 }
2379 
2380 int br_multicast_init_stats(struct net_bridge *br)
2381 {
2382 	br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
2383 	if (!br->mcast_stats)
2384 		return -ENOMEM;
2385 
2386 	return 0;
2387 }
2388 
2389 void br_multicast_uninit_stats(struct net_bridge *br)
2390 {
2391 	free_percpu(br->mcast_stats);
2392 }
2393 
2394 static void mcast_stats_add_dir(u64 *dst, u64 *src)
2395 {
2396 	dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
2397 	dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
2398 }
2399 
2400 void br_multicast_get_stats(const struct net_bridge *br,
2401 			    const struct net_bridge_port *p,
2402 			    struct br_mcast_stats *dest)
2403 {
2404 	struct bridge_mcast_stats __percpu *stats;
2405 	struct br_mcast_stats tdst;
2406 	int i;
2407 
2408 	memset(dest, 0, sizeof(*dest));
2409 	if (p)
2410 		stats = p->mcast_stats;
2411 	else
2412 		stats = br->mcast_stats;
2413 	if (WARN_ON(!stats))
2414 		return;
2415 
2416 	memset(&tdst, 0, sizeof(tdst));
2417 	for_each_possible_cpu(i) {
2418 		struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
2419 		struct br_mcast_stats temp;
2420 		unsigned int start;
2421 
2422 		do {
2423 			start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
2424 			memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
2425 		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
2426 
2427 		mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
2428 		mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
2429 		mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
2430 		mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
2431 		mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
2432 		mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
2433 		mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
2434 		tdst.igmp_parse_errors += temp.igmp_parse_errors;
2435 
2436 		mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
2437 		mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
2438 		mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
2439 		mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
2440 		mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
2441 		tdst.mld_parse_errors += temp.mld_parse_errors;
2442 	}
2443 	memcpy(dest, &tdst, sizeof(*dest));
2444 }
2445 
2446 int br_mdb_hash_init(struct net_bridge *br)
2447 {
2448 	return rhashtable_init(&br->mdb_hash_tbl, &br_mdb_rht_params);
2449 }
2450 
2451 void br_mdb_hash_fini(struct net_bridge *br)
2452 {
2453 	rhashtable_destroy(&br->mdb_hash_tbl);
2454 }
2455