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