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