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