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