xref: /openbmc/linux/net/bridge/br_multicast.c (revision 4e95bc26)
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 			rcu_assign_pointer(*pp, p->next);
1392 			hlist_del_init(&p->mglist);
1393 			del_timer(&p->timer);
1394 			kfree_rcu(p, rcu);
1395 			br_mdb_notify(br->dev, port, group, RTM_DELMDB,
1396 				      p->flags);
1397 
1398 			if (!mp->ports && !mp->host_joined &&
1399 			    netif_running(br->dev))
1400 				mod_timer(&mp->timer, jiffies);
1401 		}
1402 		goto out;
1403 	}
1404 
1405 	if (timer_pending(&other_query->timer))
1406 		goto out;
1407 
1408 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER)) {
1409 		__br_multicast_send_query(br, port, &mp->addr);
1410 
1411 		time = jiffies + br->multicast_last_member_count *
1412 				 br->multicast_last_member_interval;
1413 
1414 		mod_timer(&own_query->timer, time);
1415 
1416 		for (p = mlock_dereference(mp->ports, br);
1417 		     p != NULL;
1418 		     p = mlock_dereference(p->next, br)) {
1419 			if (!br_port_group_equal(p, port, src))
1420 				continue;
1421 
1422 			if (!hlist_unhashed(&p->mglist) &&
1423 			    (timer_pending(&p->timer) ?
1424 			     time_after(p->timer.expires, time) :
1425 			     try_to_del_timer_sync(&p->timer) >= 0)) {
1426 				mod_timer(&p->timer, time);
1427 			}
1428 
1429 			break;
1430 		}
1431 	}
1432 
1433 	now = jiffies;
1434 	time = now + br->multicast_last_member_count *
1435 		     br->multicast_last_member_interval;
1436 
1437 	if (!port) {
1438 		if (mp->host_joined &&
1439 		    (timer_pending(&mp->timer) ?
1440 		     time_after(mp->timer.expires, time) :
1441 		     try_to_del_timer_sync(&mp->timer) >= 0)) {
1442 			mod_timer(&mp->timer, time);
1443 		}
1444 
1445 		goto out;
1446 	}
1447 
1448 	for (p = mlock_dereference(mp->ports, br);
1449 	     p != NULL;
1450 	     p = mlock_dereference(p->next, br)) {
1451 		if (p->port != port)
1452 			continue;
1453 
1454 		if (!hlist_unhashed(&p->mglist) &&
1455 		    (timer_pending(&p->timer) ?
1456 		     time_after(p->timer.expires, time) :
1457 		     try_to_del_timer_sync(&p->timer) >= 0)) {
1458 			mod_timer(&p->timer, time);
1459 		}
1460 
1461 		break;
1462 	}
1463 out:
1464 	spin_unlock(&br->multicast_lock);
1465 }
1466 
1467 static void br_ip4_multicast_leave_group(struct net_bridge *br,
1468 					 struct net_bridge_port *port,
1469 					 __be32 group,
1470 					 __u16 vid,
1471 					 const unsigned char *src)
1472 {
1473 	struct br_ip br_group;
1474 	struct bridge_mcast_own_query *own_query;
1475 
1476 	if (ipv4_is_local_multicast(group))
1477 		return;
1478 
1479 	own_query = port ? &port->ip4_own_query : &br->ip4_own_query;
1480 
1481 	memset(&br_group, 0, sizeof(br_group));
1482 	br_group.u.ip4 = group;
1483 	br_group.proto = htons(ETH_P_IP);
1484 	br_group.vid = vid;
1485 
1486 	br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
1487 				 own_query, src);
1488 }
1489 
1490 #if IS_ENABLED(CONFIG_IPV6)
1491 static void br_ip6_multicast_leave_group(struct net_bridge *br,
1492 					 struct net_bridge_port *port,
1493 					 const struct in6_addr *group,
1494 					 __u16 vid,
1495 					 const unsigned char *src)
1496 {
1497 	struct br_ip br_group;
1498 	struct bridge_mcast_own_query *own_query;
1499 
1500 	if (ipv6_addr_is_ll_all_nodes(group))
1501 		return;
1502 
1503 	own_query = port ? &port->ip6_own_query : &br->ip6_own_query;
1504 
1505 	memset(&br_group, 0, sizeof(br_group));
1506 	br_group.u.ip6 = *group;
1507 	br_group.proto = htons(ETH_P_IPV6);
1508 	br_group.vid = vid;
1509 
1510 	br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
1511 				 own_query, src);
1512 }
1513 #endif
1514 
1515 static void br_multicast_err_count(const struct net_bridge *br,
1516 				   const struct net_bridge_port *p,
1517 				   __be16 proto)
1518 {
1519 	struct bridge_mcast_stats __percpu *stats;
1520 	struct bridge_mcast_stats *pstats;
1521 
1522 	if (!br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
1523 		return;
1524 
1525 	if (p)
1526 		stats = p->mcast_stats;
1527 	else
1528 		stats = br->mcast_stats;
1529 	if (WARN_ON(!stats))
1530 		return;
1531 
1532 	pstats = this_cpu_ptr(stats);
1533 
1534 	u64_stats_update_begin(&pstats->syncp);
1535 	switch (proto) {
1536 	case htons(ETH_P_IP):
1537 		pstats->mstats.igmp_parse_errors++;
1538 		break;
1539 #if IS_ENABLED(CONFIG_IPV6)
1540 	case htons(ETH_P_IPV6):
1541 		pstats->mstats.mld_parse_errors++;
1542 		break;
1543 #endif
1544 	}
1545 	u64_stats_update_end(&pstats->syncp);
1546 }
1547 
1548 static void br_multicast_pim(struct net_bridge *br,
1549 			     struct net_bridge_port *port,
1550 			     const struct sk_buff *skb)
1551 {
1552 	unsigned int offset = skb_transport_offset(skb);
1553 	struct pimhdr *pimhdr, _pimhdr;
1554 
1555 	pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr);
1556 	if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION ||
1557 	    pim_hdr_type(pimhdr) != PIM_TYPE_HELLO)
1558 		return;
1559 
1560 	br_multicast_mark_router(br, port);
1561 }
1562 
1563 static int br_ip4_multicast_mrd_rcv(struct net_bridge *br,
1564 				    struct net_bridge_port *port,
1565 				    struct sk_buff *skb)
1566 {
1567 	if (ip_hdr(skb)->protocol != IPPROTO_IGMP ||
1568 	    igmp_hdr(skb)->type != IGMP_MRDISC_ADV)
1569 		return -ENOMSG;
1570 
1571 	br_multicast_mark_router(br, port);
1572 
1573 	return 0;
1574 }
1575 
1576 static int br_multicast_ipv4_rcv(struct net_bridge *br,
1577 				 struct net_bridge_port *port,
1578 				 struct sk_buff *skb,
1579 				 u16 vid)
1580 {
1581 	const unsigned char *src;
1582 	struct igmphdr *ih;
1583 	int err;
1584 
1585 	err = ip_mc_check_igmp(skb);
1586 
1587 	if (err == -ENOMSG) {
1588 		if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) {
1589 			BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1590 		} else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) {
1591 			if (ip_hdr(skb)->protocol == IPPROTO_PIM)
1592 				br_multicast_pim(br, port, skb);
1593 		} else if (ipv4_is_all_snoopers(ip_hdr(skb)->daddr)) {
1594 			br_ip4_multicast_mrd_rcv(br, port, skb);
1595 		}
1596 
1597 		return 0;
1598 	} else if (err < 0) {
1599 		br_multicast_err_count(br, port, skb->protocol);
1600 		return err;
1601 	}
1602 
1603 	ih = igmp_hdr(skb);
1604 	src = eth_hdr(skb)->h_source;
1605 	BR_INPUT_SKB_CB(skb)->igmp = ih->type;
1606 
1607 	switch (ih->type) {
1608 	case IGMP_HOST_MEMBERSHIP_REPORT:
1609 	case IGMPV2_HOST_MEMBERSHIP_REPORT:
1610 		BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1611 		err = br_ip4_multicast_add_group(br, port, ih->group, vid, src);
1612 		break;
1613 	case IGMPV3_HOST_MEMBERSHIP_REPORT:
1614 		err = br_ip4_multicast_igmp3_report(br, port, skb, vid);
1615 		break;
1616 	case IGMP_HOST_MEMBERSHIP_QUERY:
1617 		br_ip4_multicast_query(br, port, skb, vid);
1618 		break;
1619 	case IGMP_HOST_LEAVE_MESSAGE:
1620 		br_ip4_multicast_leave_group(br, port, ih->group, vid, src);
1621 		break;
1622 	}
1623 
1624 	br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
1625 			   BR_MCAST_DIR_RX);
1626 
1627 	return err;
1628 }
1629 
1630 #if IS_ENABLED(CONFIG_IPV6)
1631 static int br_ip6_multicast_mrd_rcv(struct net_bridge *br,
1632 				    struct net_bridge_port *port,
1633 				    struct sk_buff *skb)
1634 {
1635 	int ret;
1636 
1637 	if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6)
1638 		return -ENOMSG;
1639 
1640 	ret = ipv6_mc_check_icmpv6(skb);
1641 	if (ret < 0)
1642 		return ret;
1643 
1644 	if (icmp6_hdr(skb)->icmp6_type != ICMPV6_MRDISC_ADV)
1645 		return -ENOMSG;
1646 
1647 	br_multicast_mark_router(br, port);
1648 
1649 	return 0;
1650 }
1651 
1652 static int br_multicast_ipv6_rcv(struct net_bridge *br,
1653 				 struct net_bridge_port *port,
1654 				 struct sk_buff *skb,
1655 				 u16 vid)
1656 {
1657 	const unsigned char *src;
1658 	struct mld_msg *mld;
1659 	int err;
1660 
1661 	err = ipv6_mc_check_mld(skb);
1662 
1663 	if (err == -ENOMSG) {
1664 		if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
1665 			BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1666 
1667 		if (ipv6_addr_is_all_snoopers(&ipv6_hdr(skb)->daddr)) {
1668 			err = br_ip6_multicast_mrd_rcv(br, port, skb);
1669 
1670 			if (err < 0 && err != -ENOMSG) {
1671 				br_multicast_err_count(br, port, skb->protocol);
1672 				return err;
1673 			}
1674 		}
1675 
1676 		return 0;
1677 	} else if (err < 0) {
1678 		br_multicast_err_count(br, port, skb->protocol);
1679 		return err;
1680 	}
1681 
1682 	mld = (struct mld_msg *)skb_transport_header(skb);
1683 	BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type;
1684 
1685 	switch (mld->mld_type) {
1686 	case ICMPV6_MGM_REPORT:
1687 		src = eth_hdr(skb)->h_source;
1688 		BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1689 		err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid,
1690 						 src);
1691 		break;
1692 	case ICMPV6_MLD2_REPORT:
1693 		err = br_ip6_multicast_mld2_report(br, port, skb, vid);
1694 		break;
1695 	case ICMPV6_MGM_QUERY:
1696 		err = br_ip6_multicast_query(br, port, skb, vid);
1697 		break;
1698 	case ICMPV6_MGM_REDUCTION:
1699 		src = eth_hdr(skb)->h_source;
1700 		br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src);
1701 		break;
1702 	}
1703 
1704 	br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
1705 			   BR_MCAST_DIR_RX);
1706 
1707 	return err;
1708 }
1709 #endif
1710 
1711 int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1712 		     struct sk_buff *skb, u16 vid)
1713 {
1714 	int ret = 0;
1715 
1716 	BR_INPUT_SKB_CB(skb)->igmp = 0;
1717 	BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1718 
1719 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
1720 		return 0;
1721 
1722 	switch (skb->protocol) {
1723 	case htons(ETH_P_IP):
1724 		ret = br_multicast_ipv4_rcv(br, port, skb, vid);
1725 		break;
1726 #if IS_ENABLED(CONFIG_IPV6)
1727 	case htons(ETH_P_IPV6):
1728 		ret = br_multicast_ipv6_rcv(br, port, skb, vid);
1729 		break;
1730 #endif
1731 	}
1732 
1733 	return ret;
1734 }
1735 
1736 static void br_multicast_query_expired(struct net_bridge *br,
1737 				       struct bridge_mcast_own_query *query,
1738 				       struct bridge_mcast_querier *querier)
1739 {
1740 	spin_lock(&br->multicast_lock);
1741 	if (query->startup_sent < br->multicast_startup_query_count)
1742 		query->startup_sent++;
1743 
1744 	RCU_INIT_POINTER(querier->port, NULL);
1745 	br_multicast_send_query(br, NULL, query);
1746 	spin_unlock(&br->multicast_lock);
1747 }
1748 
1749 static void br_ip4_multicast_query_expired(struct timer_list *t)
1750 {
1751 	struct net_bridge *br = from_timer(br, t, ip4_own_query.timer);
1752 
1753 	br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier);
1754 }
1755 
1756 #if IS_ENABLED(CONFIG_IPV6)
1757 static void br_ip6_multicast_query_expired(struct timer_list *t)
1758 {
1759 	struct net_bridge *br = from_timer(br, t, ip6_own_query.timer);
1760 
1761 	br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier);
1762 }
1763 #endif
1764 
1765 void br_multicast_init(struct net_bridge *br)
1766 {
1767 	br->hash_max = BR_MULTICAST_DEFAULT_HASH_MAX;
1768 
1769 	br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
1770 	br->multicast_last_member_count = 2;
1771 	br->multicast_startup_query_count = 2;
1772 
1773 	br->multicast_last_member_interval = HZ;
1774 	br->multicast_query_response_interval = 10 * HZ;
1775 	br->multicast_startup_query_interval = 125 * HZ / 4;
1776 	br->multicast_query_interval = 125 * HZ;
1777 	br->multicast_querier_interval = 255 * HZ;
1778 	br->multicast_membership_interval = 260 * HZ;
1779 
1780 	br->ip4_other_query.delay_time = 0;
1781 	br->ip4_querier.port = NULL;
1782 	br->multicast_igmp_version = 2;
1783 #if IS_ENABLED(CONFIG_IPV6)
1784 	br->multicast_mld_version = 1;
1785 	br->ip6_other_query.delay_time = 0;
1786 	br->ip6_querier.port = NULL;
1787 #endif
1788 	br_opt_toggle(br, BROPT_MULTICAST_ENABLED, true);
1789 	br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true);
1790 
1791 	spin_lock_init(&br->multicast_lock);
1792 	timer_setup(&br->multicast_router_timer,
1793 		    br_multicast_local_router_expired, 0);
1794 	timer_setup(&br->ip4_other_query.timer,
1795 		    br_ip4_multicast_querier_expired, 0);
1796 	timer_setup(&br->ip4_own_query.timer,
1797 		    br_ip4_multicast_query_expired, 0);
1798 #if IS_ENABLED(CONFIG_IPV6)
1799 	timer_setup(&br->ip6_other_query.timer,
1800 		    br_ip6_multicast_querier_expired, 0);
1801 	timer_setup(&br->ip6_own_query.timer,
1802 		    br_ip6_multicast_query_expired, 0);
1803 #endif
1804 	INIT_HLIST_HEAD(&br->mdb_list);
1805 }
1806 
1807 static void br_ip4_multicast_join_snoopers(struct net_bridge *br)
1808 {
1809 	struct in_device *in_dev = in_dev_get(br->dev);
1810 
1811 	if (!in_dev)
1812 		return;
1813 
1814 	__ip_mc_inc_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
1815 	in_dev_put(in_dev);
1816 }
1817 
1818 #if IS_ENABLED(CONFIG_IPV6)
1819 static void br_ip6_multicast_join_snoopers(struct net_bridge *br)
1820 {
1821 	struct in6_addr addr;
1822 
1823 	ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
1824 	ipv6_dev_mc_inc(br->dev, &addr);
1825 }
1826 #else
1827 static inline void br_ip6_multicast_join_snoopers(struct net_bridge *br)
1828 {
1829 }
1830 #endif
1831 
1832 static void br_multicast_join_snoopers(struct net_bridge *br)
1833 {
1834 	br_ip4_multicast_join_snoopers(br);
1835 	br_ip6_multicast_join_snoopers(br);
1836 }
1837 
1838 static void br_ip4_multicast_leave_snoopers(struct net_bridge *br)
1839 {
1840 	struct in_device *in_dev = in_dev_get(br->dev);
1841 
1842 	if (WARN_ON(!in_dev))
1843 		return;
1844 
1845 	__ip_mc_dec_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
1846 	in_dev_put(in_dev);
1847 }
1848 
1849 #if IS_ENABLED(CONFIG_IPV6)
1850 static void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
1851 {
1852 	struct in6_addr addr;
1853 
1854 	ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
1855 	ipv6_dev_mc_dec(br->dev, &addr);
1856 }
1857 #else
1858 static inline void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
1859 {
1860 }
1861 #endif
1862 
1863 static void br_multicast_leave_snoopers(struct net_bridge *br)
1864 {
1865 	br_ip4_multicast_leave_snoopers(br);
1866 	br_ip6_multicast_leave_snoopers(br);
1867 }
1868 
1869 static void __br_multicast_open(struct net_bridge *br,
1870 				struct bridge_mcast_own_query *query)
1871 {
1872 	query->startup_sent = 0;
1873 
1874 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
1875 		return;
1876 
1877 	mod_timer(&query->timer, jiffies);
1878 }
1879 
1880 void br_multicast_open(struct net_bridge *br)
1881 {
1882 	if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
1883 		br_multicast_join_snoopers(br);
1884 
1885 	__br_multicast_open(br, &br->ip4_own_query);
1886 #if IS_ENABLED(CONFIG_IPV6)
1887 	__br_multicast_open(br, &br->ip6_own_query);
1888 #endif
1889 }
1890 
1891 void br_multicast_stop(struct net_bridge *br)
1892 {
1893 	del_timer_sync(&br->multicast_router_timer);
1894 	del_timer_sync(&br->ip4_other_query.timer);
1895 	del_timer_sync(&br->ip4_own_query.timer);
1896 #if IS_ENABLED(CONFIG_IPV6)
1897 	del_timer_sync(&br->ip6_other_query.timer);
1898 	del_timer_sync(&br->ip6_own_query.timer);
1899 #endif
1900 
1901 	if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
1902 		br_multicast_leave_snoopers(br);
1903 }
1904 
1905 void br_multicast_dev_del(struct net_bridge *br)
1906 {
1907 	struct net_bridge_mdb_entry *mp;
1908 	struct hlist_node *tmp;
1909 
1910 	spin_lock_bh(&br->multicast_lock);
1911 	hlist_for_each_entry_safe(mp, tmp, &br->mdb_list, mdb_node) {
1912 		del_timer(&mp->timer);
1913 		rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode,
1914 				       br_mdb_rht_params);
1915 		hlist_del_rcu(&mp->mdb_node);
1916 		kfree_rcu(mp, rcu);
1917 	}
1918 	spin_unlock_bh(&br->multicast_lock);
1919 
1920 	rcu_barrier();
1921 }
1922 
1923 int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1924 {
1925 	int err = -EINVAL;
1926 
1927 	spin_lock_bh(&br->multicast_lock);
1928 
1929 	switch (val) {
1930 	case MDB_RTR_TYPE_DISABLED:
1931 	case MDB_RTR_TYPE_PERM:
1932 		br_mc_router_state_change(br, val == MDB_RTR_TYPE_PERM);
1933 		del_timer(&br->multicast_router_timer);
1934 		br->multicast_router = val;
1935 		err = 0;
1936 		break;
1937 	case MDB_RTR_TYPE_TEMP_QUERY:
1938 		if (br->multicast_router != MDB_RTR_TYPE_TEMP_QUERY)
1939 			br_mc_router_state_change(br, false);
1940 		br->multicast_router = val;
1941 		err = 0;
1942 		break;
1943 	}
1944 
1945 	spin_unlock_bh(&br->multicast_lock);
1946 
1947 	return err;
1948 }
1949 
1950 static void __del_port_router(struct net_bridge_port *p)
1951 {
1952 	if (hlist_unhashed(&p->rlist))
1953 		return;
1954 	hlist_del_init_rcu(&p->rlist);
1955 	br_rtr_notify(p->br->dev, p, RTM_DELMDB);
1956 	br_port_mc_router_state_change(p, false);
1957 
1958 	/* don't allow timer refresh */
1959 	if (p->multicast_router == MDB_RTR_TYPE_TEMP)
1960 		p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
1961 }
1962 
1963 int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1964 {
1965 	struct net_bridge *br = p->br;
1966 	unsigned long now = jiffies;
1967 	int err = -EINVAL;
1968 
1969 	spin_lock(&br->multicast_lock);
1970 	if (p->multicast_router == val) {
1971 		/* Refresh the temp router port timer */
1972 		if (p->multicast_router == MDB_RTR_TYPE_TEMP)
1973 			mod_timer(&p->multicast_router_timer,
1974 				  now + br->multicast_querier_interval);
1975 		err = 0;
1976 		goto unlock;
1977 	}
1978 	switch (val) {
1979 	case MDB_RTR_TYPE_DISABLED:
1980 		p->multicast_router = MDB_RTR_TYPE_DISABLED;
1981 		__del_port_router(p);
1982 		del_timer(&p->multicast_router_timer);
1983 		break;
1984 	case MDB_RTR_TYPE_TEMP_QUERY:
1985 		p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
1986 		__del_port_router(p);
1987 		break;
1988 	case MDB_RTR_TYPE_PERM:
1989 		p->multicast_router = MDB_RTR_TYPE_PERM;
1990 		del_timer(&p->multicast_router_timer);
1991 		br_multicast_add_router(br, p);
1992 		break;
1993 	case MDB_RTR_TYPE_TEMP:
1994 		p->multicast_router = MDB_RTR_TYPE_TEMP;
1995 		br_multicast_mark_router(br, p);
1996 		break;
1997 	default:
1998 		goto unlock;
1999 	}
2000 	err = 0;
2001 unlock:
2002 	spin_unlock(&br->multicast_lock);
2003 
2004 	return err;
2005 }
2006 
2007 static void br_multicast_start_querier(struct net_bridge *br,
2008 				       struct bridge_mcast_own_query *query)
2009 {
2010 	struct net_bridge_port *port;
2011 
2012 	__br_multicast_open(br, query);
2013 
2014 	rcu_read_lock();
2015 	list_for_each_entry_rcu(port, &br->port_list, list) {
2016 		if (port->state == BR_STATE_DISABLED ||
2017 		    port->state == BR_STATE_BLOCKING)
2018 			continue;
2019 
2020 		if (query == &br->ip4_own_query)
2021 			br_multicast_enable(&port->ip4_own_query);
2022 #if IS_ENABLED(CONFIG_IPV6)
2023 		else
2024 			br_multicast_enable(&port->ip6_own_query);
2025 #endif
2026 	}
2027 	rcu_read_unlock();
2028 }
2029 
2030 int br_multicast_toggle(struct net_bridge *br, unsigned long val)
2031 {
2032 	struct net_bridge_port *port;
2033 
2034 	spin_lock_bh(&br->multicast_lock);
2035 	if (!!br_opt_get(br, BROPT_MULTICAST_ENABLED) == !!val)
2036 		goto unlock;
2037 
2038 	br_mc_disabled_update(br->dev, val);
2039 	br_opt_toggle(br, BROPT_MULTICAST_ENABLED, !!val);
2040 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) {
2041 		br_multicast_leave_snoopers(br);
2042 		goto unlock;
2043 	}
2044 
2045 	if (!netif_running(br->dev))
2046 		goto unlock;
2047 
2048 	br_multicast_open(br);
2049 	list_for_each_entry(port, &br->port_list, list)
2050 		__br_multicast_enable_port(port);
2051 
2052 unlock:
2053 	spin_unlock_bh(&br->multicast_lock);
2054 
2055 	return 0;
2056 }
2057 
2058 bool br_multicast_enabled(const struct net_device *dev)
2059 {
2060 	struct net_bridge *br = netdev_priv(dev);
2061 
2062 	return !!br_opt_get(br, BROPT_MULTICAST_ENABLED);
2063 }
2064 EXPORT_SYMBOL_GPL(br_multicast_enabled);
2065 
2066 bool br_multicast_router(const struct net_device *dev)
2067 {
2068 	struct net_bridge *br = netdev_priv(dev);
2069 	bool is_router;
2070 
2071 	spin_lock_bh(&br->multicast_lock);
2072 	is_router = br_multicast_is_router(br);
2073 	spin_unlock_bh(&br->multicast_lock);
2074 	return is_router;
2075 }
2076 EXPORT_SYMBOL_GPL(br_multicast_router);
2077 
2078 int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
2079 {
2080 	unsigned long max_delay;
2081 
2082 	val = !!val;
2083 
2084 	spin_lock_bh(&br->multicast_lock);
2085 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER) == val)
2086 		goto unlock;
2087 
2088 	br_opt_toggle(br, BROPT_MULTICAST_QUERIER, !!val);
2089 	if (!val)
2090 		goto unlock;
2091 
2092 	max_delay = br->multicast_query_response_interval;
2093 
2094 	if (!timer_pending(&br->ip4_other_query.timer))
2095 		br->ip4_other_query.delay_time = jiffies + max_delay;
2096 
2097 	br_multicast_start_querier(br, &br->ip4_own_query);
2098 
2099 #if IS_ENABLED(CONFIG_IPV6)
2100 	if (!timer_pending(&br->ip6_other_query.timer))
2101 		br->ip6_other_query.delay_time = jiffies + max_delay;
2102 
2103 	br_multicast_start_querier(br, &br->ip6_own_query);
2104 #endif
2105 
2106 unlock:
2107 	spin_unlock_bh(&br->multicast_lock);
2108 
2109 	return 0;
2110 }
2111 
2112 int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val)
2113 {
2114 	/* Currently we support only version 2 and 3 */
2115 	switch (val) {
2116 	case 2:
2117 	case 3:
2118 		break;
2119 	default:
2120 		return -EINVAL;
2121 	}
2122 
2123 	spin_lock_bh(&br->multicast_lock);
2124 	br->multicast_igmp_version = val;
2125 	spin_unlock_bh(&br->multicast_lock);
2126 
2127 	return 0;
2128 }
2129 
2130 #if IS_ENABLED(CONFIG_IPV6)
2131 int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val)
2132 {
2133 	/* Currently we support version 1 and 2 */
2134 	switch (val) {
2135 	case 1:
2136 	case 2:
2137 		break;
2138 	default:
2139 		return -EINVAL;
2140 	}
2141 
2142 	spin_lock_bh(&br->multicast_lock);
2143 	br->multicast_mld_version = val;
2144 	spin_unlock_bh(&br->multicast_lock);
2145 
2146 	return 0;
2147 }
2148 #endif
2149 
2150 /**
2151  * br_multicast_list_adjacent - Returns snooped multicast addresses
2152  * @dev:	The bridge port adjacent to which to retrieve addresses
2153  * @br_ip_list:	The list to store found, snooped multicast IP addresses in
2154  *
2155  * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
2156  * snooping feature on all bridge ports of dev's bridge device, excluding
2157  * the addresses from dev itself.
2158  *
2159  * Returns the number of items added to br_ip_list.
2160  *
2161  * Notes:
2162  * - br_ip_list needs to be initialized by caller
2163  * - br_ip_list might contain duplicates in the end
2164  *   (needs to be taken care of by caller)
2165  * - br_ip_list needs to be freed by caller
2166  */
2167 int br_multicast_list_adjacent(struct net_device *dev,
2168 			       struct list_head *br_ip_list)
2169 {
2170 	struct net_bridge *br;
2171 	struct net_bridge_port *port;
2172 	struct net_bridge_port_group *group;
2173 	struct br_ip_list *entry;
2174 	int count = 0;
2175 
2176 	rcu_read_lock();
2177 	if (!br_ip_list || !netif_is_bridge_port(dev))
2178 		goto unlock;
2179 
2180 	port = br_port_get_rcu(dev);
2181 	if (!port || !port->br)
2182 		goto unlock;
2183 
2184 	br = port->br;
2185 
2186 	list_for_each_entry_rcu(port, &br->port_list, list) {
2187 		if (!port->dev || port->dev == dev)
2188 			continue;
2189 
2190 		hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
2191 			entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
2192 			if (!entry)
2193 				goto unlock;
2194 
2195 			entry->addr = group->addr;
2196 			list_add(&entry->list, br_ip_list);
2197 			count++;
2198 		}
2199 	}
2200 
2201 unlock:
2202 	rcu_read_unlock();
2203 	return count;
2204 }
2205 EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
2206 
2207 /**
2208  * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2209  * @dev: The bridge port providing the bridge on which to check for a querier
2210  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2211  *
2212  * Checks whether the given interface has a bridge on top and if so returns
2213  * true if a valid querier exists anywhere on the bridged link layer.
2214  * Otherwise returns false.
2215  */
2216 bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
2217 {
2218 	struct net_bridge *br;
2219 	struct net_bridge_port *port;
2220 	struct ethhdr eth;
2221 	bool ret = false;
2222 
2223 	rcu_read_lock();
2224 	if (!netif_is_bridge_port(dev))
2225 		goto unlock;
2226 
2227 	port = br_port_get_rcu(dev);
2228 	if (!port || !port->br)
2229 		goto unlock;
2230 
2231 	br = port->br;
2232 
2233 	memset(&eth, 0, sizeof(eth));
2234 	eth.h_proto = htons(proto);
2235 
2236 	ret = br_multicast_querier_exists(br, &eth);
2237 
2238 unlock:
2239 	rcu_read_unlock();
2240 	return ret;
2241 }
2242 EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
2243 
2244 /**
2245  * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
2246  * @dev: The bridge port adjacent to which to check for a querier
2247  * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2248  *
2249  * Checks whether the given interface has a bridge on top and if so returns
2250  * true if a selected querier is behind one of the other ports of this
2251  * bridge. Otherwise returns false.
2252  */
2253 bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
2254 {
2255 	struct net_bridge *br;
2256 	struct net_bridge_port *port;
2257 	bool ret = false;
2258 
2259 	rcu_read_lock();
2260 	if (!netif_is_bridge_port(dev))
2261 		goto unlock;
2262 
2263 	port = br_port_get_rcu(dev);
2264 	if (!port || !port->br)
2265 		goto unlock;
2266 
2267 	br = port->br;
2268 
2269 	switch (proto) {
2270 	case ETH_P_IP:
2271 		if (!timer_pending(&br->ip4_other_query.timer) ||
2272 		    rcu_dereference(br->ip4_querier.port) == port)
2273 			goto unlock;
2274 		break;
2275 #if IS_ENABLED(CONFIG_IPV6)
2276 	case ETH_P_IPV6:
2277 		if (!timer_pending(&br->ip6_other_query.timer) ||
2278 		    rcu_dereference(br->ip6_querier.port) == port)
2279 			goto unlock;
2280 		break;
2281 #endif
2282 	default:
2283 		goto unlock;
2284 	}
2285 
2286 	ret = true;
2287 unlock:
2288 	rcu_read_unlock();
2289 	return ret;
2290 }
2291 EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
2292 
2293 static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
2294 			       const struct sk_buff *skb, u8 type, u8 dir)
2295 {
2296 	struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
2297 	__be16 proto = skb->protocol;
2298 	unsigned int t_len;
2299 
2300 	u64_stats_update_begin(&pstats->syncp);
2301 	switch (proto) {
2302 	case htons(ETH_P_IP):
2303 		t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
2304 		switch (type) {
2305 		case IGMP_HOST_MEMBERSHIP_REPORT:
2306 			pstats->mstats.igmp_v1reports[dir]++;
2307 			break;
2308 		case IGMPV2_HOST_MEMBERSHIP_REPORT:
2309 			pstats->mstats.igmp_v2reports[dir]++;
2310 			break;
2311 		case IGMPV3_HOST_MEMBERSHIP_REPORT:
2312 			pstats->mstats.igmp_v3reports[dir]++;
2313 			break;
2314 		case IGMP_HOST_MEMBERSHIP_QUERY:
2315 			if (t_len != sizeof(struct igmphdr)) {
2316 				pstats->mstats.igmp_v3queries[dir]++;
2317 			} else {
2318 				unsigned int offset = skb_transport_offset(skb);
2319 				struct igmphdr *ih, _ihdr;
2320 
2321 				ih = skb_header_pointer(skb, offset,
2322 							sizeof(_ihdr), &_ihdr);
2323 				if (!ih)
2324 					break;
2325 				if (!ih->code)
2326 					pstats->mstats.igmp_v1queries[dir]++;
2327 				else
2328 					pstats->mstats.igmp_v2queries[dir]++;
2329 			}
2330 			break;
2331 		case IGMP_HOST_LEAVE_MESSAGE:
2332 			pstats->mstats.igmp_leaves[dir]++;
2333 			break;
2334 		}
2335 		break;
2336 #if IS_ENABLED(CONFIG_IPV6)
2337 	case htons(ETH_P_IPV6):
2338 		t_len = ntohs(ipv6_hdr(skb)->payload_len) +
2339 			sizeof(struct ipv6hdr);
2340 		t_len -= skb_network_header_len(skb);
2341 		switch (type) {
2342 		case ICMPV6_MGM_REPORT:
2343 			pstats->mstats.mld_v1reports[dir]++;
2344 			break;
2345 		case ICMPV6_MLD2_REPORT:
2346 			pstats->mstats.mld_v2reports[dir]++;
2347 			break;
2348 		case ICMPV6_MGM_QUERY:
2349 			if (t_len != sizeof(struct mld_msg))
2350 				pstats->mstats.mld_v2queries[dir]++;
2351 			else
2352 				pstats->mstats.mld_v1queries[dir]++;
2353 			break;
2354 		case ICMPV6_MGM_REDUCTION:
2355 			pstats->mstats.mld_leaves[dir]++;
2356 			break;
2357 		}
2358 		break;
2359 #endif /* CONFIG_IPV6 */
2360 	}
2361 	u64_stats_update_end(&pstats->syncp);
2362 }
2363 
2364 void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
2365 			const struct sk_buff *skb, u8 type, u8 dir)
2366 {
2367 	struct bridge_mcast_stats __percpu *stats;
2368 
2369 	/* if multicast_disabled is true then igmp type can't be set */
2370 	if (!type || !br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
2371 		return;
2372 
2373 	if (p)
2374 		stats = p->mcast_stats;
2375 	else
2376 		stats = br->mcast_stats;
2377 	if (WARN_ON(!stats))
2378 		return;
2379 
2380 	br_mcast_stats_add(stats, skb, type, dir);
2381 }
2382 
2383 int br_multicast_init_stats(struct net_bridge *br)
2384 {
2385 	br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
2386 	if (!br->mcast_stats)
2387 		return -ENOMEM;
2388 
2389 	return 0;
2390 }
2391 
2392 void br_multicast_uninit_stats(struct net_bridge *br)
2393 {
2394 	free_percpu(br->mcast_stats);
2395 }
2396 
2397 static void mcast_stats_add_dir(u64 *dst, u64 *src)
2398 {
2399 	dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
2400 	dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
2401 }
2402 
2403 void br_multicast_get_stats(const struct net_bridge *br,
2404 			    const struct net_bridge_port *p,
2405 			    struct br_mcast_stats *dest)
2406 {
2407 	struct bridge_mcast_stats __percpu *stats;
2408 	struct br_mcast_stats tdst;
2409 	int i;
2410 
2411 	memset(dest, 0, sizeof(*dest));
2412 	if (p)
2413 		stats = p->mcast_stats;
2414 	else
2415 		stats = br->mcast_stats;
2416 	if (WARN_ON(!stats))
2417 		return;
2418 
2419 	memset(&tdst, 0, sizeof(tdst));
2420 	for_each_possible_cpu(i) {
2421 		struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
2422 		struct br_mcast_stats temp;
2423 		unsigned int start;
2424 
2425 		do {
2426 			start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
2427 			memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
2428 		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
2429 
2430 		mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
2431 		mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
2432 		mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
2433 		mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
2434 		mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
2435 		mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
2436 		mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
2437 		tdst.igmp_parse_errors += temp.igmp_parse_errors;
2438 
2439 		mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
2440 		mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
2441 		mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
2442 		mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
2443 		mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
2444 		tdst.mld_parse_errors += temp.mld_parse_errors;
2445 	}
2446 	memcpy(dest, &tdst, sizeof(*dest));
2447 }
2448 
2449 int br_mdb_hash_init(struct net_bridge *br)
2450 {
2451 	return rhashtable_init(&br->mdb_hash_tbl, &br_mdb_rht_params);
2452 }
2453 
2454 void br_mdb_hash_fini(struct net_bridge *br)
2455 {
2456 	rhashtable_destroy(&br->mdb_hash_tbl);
2457 }
2458