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