dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / net / bridge / br_multicast.c
blobd80c15d028fe2a65e0a61d0d2fad447ecad13102
1 /*
2 * Bridge multicast support.
4 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
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.
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
36 #include "br_private.h"
38 static void br_multicast_start_querier(struct net_bridge *br,
39 struct bridge_mcast_own_query *query);
40 static void br_multicast_add_router(struct net_bridge *br,
41 struct net_bridge_port *port);
42 static void br_ip4_multicast_leave_group(struct net_bridge *br,
43 struct net_bridge_port *port,
44 __be32 group,
45 __u16 vid);
46 #if IS_ENABLED(CONFIG_IPV6)
47 static void br_ip6_multicast_leave_group(struct net_bridge *br,
48 struct net_bridge_port *port,
49 const struct in6_addr *group,
50 __u16 vid);
51 #endif
52 unsigned int br_mdb_rehash_seq;
54 static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
56 if (a->proto != b->proto)
57 return 0;
58 if (a->vid != b->vid)
59 return 0;
60 switch (a->proto) {
61 case htons(ETH_P_IP):
62 return a->u.ip4 == b->u.ip4;
63 #if IS_ENABLED(CONFIG_IPV6)
64 case htons(ETH_P_IPV6):
65 return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
66 #endif
68 return 0;
71 static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip,
72 __u16 vid)
74 return jhash_2words((__force u32)ip, vid, mdb->secret) & (mdb->max - 1);
77 #if IS_ENABLED(CONFIG_IPV6)
78 static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb,
79 const struct in6_addr *ip,
80 __u16 vid)
82 return jhash_2words(ipv6_addr_hash(ip), vid,
83 mdb->secret) & (mdb->max - 1);
85 #endif
87 static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb,
88 struct br_ip *ip)
90 switch (ip->proto) {
91 case htons(ETH_P_IP):
92 return __br_ip4_hash(mdb, ip->u.ip4, ip->vid);
93 #if IS_ENABLED(CONFIG_IPV6)
94 case htons(ETH_P_IPV6):
95 return __br_ip6_hash(mdb, &ip->u.ip6, ip->vid);
96 #endif
98 return 0;
101 static struct net_bridge_mdb_entry *__br_mdb_ip_get(
102 struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash)
104 struct net_bridge_mdb_entry *mp;
106 hlist_for_each_entry_rcu(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
107 if (br_ip_equal(&mp->addr, dst))
108 return mp;
111 return NULL;
114 struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge_mdb_htable *mdb,
115 struct br_ip *dst)
117 if (!mdb)
118 return NULL;
120 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
123 static struct net_bridge_mdb_entry *br_mdb_ip4_get(
124 struct net_bridge_mdb_htable *mdb, __be32 dst, __u16 vid)
126 struct br_ip br_dst;
128 br_dst.u.ip4 = dst;
129 br_dst.proto = htons(ETH_P_IP);
130 br_dst.vid = vid;
132 return br_mdb_ip_get(mdb, &br_dst);
135 #if IS_ENABLED(CONFIG_IPV6)
136 static struct net_bridge_mdb_entry *br_mdb_ip6_get(
137 struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst,
138 __u16 vid)
140 struct br_ip br_dst;
142 br_dst.u.ip6 = *dst;
143 br_dst.proto = htons(ETH_P_IPV6);
144 br_dst.vid = vid;
146 return br_mdb_ip_get(mdb, &br_dst);
148 #endif
150 struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
151 struct sk_buff *skb, u16 vid)
153 struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb);
154 struct br_ip ip;
156 if (br->multicast_disabled)
157 return NULL;
159 if (BR_INPUT_SKB_CB(skb)->igmp)
160 return NULL;
162 ip.proto = skb->protocol;
163 ip.vid = vid;
165 switch (skb->protocol) {
166 case htons(ETH_P_IP):
167 ip.u.ip4 = ip_hdr(skb)->daddr;
168 break;
169 #if IS_ENABLED(CONFIG_IPV6)
170 case htons(ETH_P_IPV6):
171 ip.u.ip6 = ipv6_hdr(skb)->daddr;
172 break;
173 #endif
174 default:
175 return NULL;
178 return br_mdb_ip_get(mdb, &ip);
181 static void br_mdb_free(struct rcu_head *head)
183 struct net_bridge_mdb_htable *mdb =
184 container_of(head, struct net_bridge_mdb_htable, rcu);
185 struct net_bridge_mdb_htable *old = mdb->old;
187 mdb->old = NULL;
188 kfree(old->mhash);
189 kfree(old);
192 static int br_mdb_copy(struct net_bridge_mdb_htable *new,
193 struct net_bridge_mdb_htable *old,
194 int elasticity)
196 struct net_bridge_mdb_entry *mp;
197 int maxlen;
198 int len;
199 int i;
201 for (i = 0; i < old->max; i++)
202 hlist_for_each_entry(mp, &old->mhash[i], hlist[old->ver])
203 hlist_add_head(&mp->hlist[new->ver],
204 &new->mhash[br_ip_hash(new, &mp->addr)]);
206 if (!elasticity)
207 return 0;
209 maxlen = 0;
210 for (i = 0; i < new->max; i++) {
211 len = 0;
212 hlist_for_each_entry(mp, &new->mhash[i], hlist[new->ver])
213 len++;
214 if (len > maxlen)
215 maxlen = len;
218 return maxlen > elasticity ? -EINVAL : 0;
221 void br_multicast_free_pg(struct rcu_head *head)
223 struct net_bridge_port_group *p =
224 container_of(head, struct net_bridge_port_group, rcu);
226 kfree(p);
229 static void br_multicast_free_group(struct rcu_head *head)
231 struct net_bridge_mdb_entry *mp =
232 container_of(head, struct net_bridge_mdb_entry, rcu);
234 kfree(mp);
237 static void br_multicast_group_expired(unsigned long data)
239 struct net_bridge_mdb_entry *mp = (void *)data;
240 struct net_bridge *br = mp->br;
241 struct net_bridge_mdb_htable *mdb;
243 spin_lock(&br->multicast_lock);
244 if (!netif_running(br->dev) || timer_pending(&mp->timer))
245 goto out;
247 mp->mglist = false;
249 if (mp->ports)
250 goto out;
252 mdb = mlock_dereference(br->mdb, br);
254 hlist_del_rcu(&mp->hlist[mdb->ver]);
255 mdb->size--;
257 call_rcu_bh(&mp->rcu, br_multicast_free_group);
259 out:
260 spin_unlock(&br->multicast_lock);
263 static void br_multicast_del_pg(struct net_bridge *br,
264 struct net_bridge_port_group *pg)
266 struct net_bridge_mdb_htable *mdb;
267 struct net_bridge_mdb_entry *mp;
268 struct net_bridge_port_group *p;
269 struct net_bridge_port_group __rcu **pp;
271 mdb = mlock_dereference(br->mdb, br);
273 mp = br_mdb_ip_get(mdb, &pg->addr);
274 if (WARN_ON(!mp))
275 return;
277 for (pp = &mp->ports;
278 (p = mlock_dereference(*pp, br)) != NULL;
279 pp = &p->next) {
280 if (p != pg)
281 continue;
283 rcu_assign_pointer(*pp, p->next);
284 hlist_del_init(&p->mglist);
285 del_timer(&p->timer);
286 br_mdb_notify(br->dev, p->port, &pg->addr, RTM_DELMDB,
287 p->state);
288 call_rcu_bh(&p->rcu, br_multicast_free_pg);
290 if (!mp->ports && !mp->mglist &&
291 netif_running(br->dev))
292 mod_timer(&mp->timer, jiffies);
294 return;
297 WARN_ON(1);
300 static void br_multicast_port_group_expired(unsigned long data)
302 struct net_bridge_port_group *pg = (void *)data;
303 struct net_bridge *br = pg->port->br;
305 spin_lock(&br->multicast_lock);
306 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
307 hlist_unhashed(&pg->mglist) || pg->state & MDB_PERMANENT)
308 goto out;
310 br_multicast_del_pg(br, pg);
312 out:
313 spin_unlock(&br->multicast_lock);
316 static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max,
317 int elasticity)
319 struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1);
320 struct net_bridge_mdb_htable *mdb;
321 int err;
323 mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
324 if (!mdb)
325 return -ENOMEM;
327 mdb->max = max;
328 mdb->old = old;
330 mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
331 if (!mdb->mhash) {
332 kfree(mdb);
333 return -ENOMEM;
336 mdb->size = old ? old->size : 0;
337 mdb->ver = old ? old->ver ^ 1 : 0;
339 if (!old || elasticity)
340 get_random_bytes(&mdb->secret, sizeof(mdb->secret));
341 else
342 mdb->secret = old->secret;
344 if (!old)
345 goto out;
347 err = br_mdb_copy(mdb, old, elasticity);
348 if (err) {
349 kfree(mdb->mhash);
350 kfree(mdb);
351 return err;
354 br_mdb_rehash_seq++;
355 call_rcu_bh(&mdb->rcu, br_mdb_free);
357 out:
358 rcu_assign_pointer(*mdbp, mdb);
360 return 0;
363 static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
364 __be32 group)
366 struct sk_buff *skb;
367 struct igmphdr *ih;
368 struct ethhdr *eth;
369 struct iphdr *iph;
371 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
372 sizeof(*ih) + 4);
373 if (!skb)
374 goto out;
376 skb->protocol = htons(ETH_P_IP);
378 skb_reset_mac_header(skb);
379 eth = eth_hdr(skb);
381 ether_addr_copy(eth->h_source, br->dev->dev_addr);
382 eth->h_dest[0] = 1;
383 eth->h_dest[1] = 0;
384 eth->h_dest[2] = 0x5e;
385 eth->h_dest[3] = 0;
386 eth->h_dest[4] = 0;
387 eth->h_dest[5] = 1;
388 eth->h_proto = htons(ETH_P_IP);
389 skb_put(skb, sizeof(*eth));
391 skb_set_network_header(skb, skb->len);
392 iph = ip_hdr(skb);
394 iph->version = 4;
395 iph->ihl = 6;
396 iph->tos = 0xc0;
397 iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4);
398 iph->id = 0;
399 iph->frag_off = htons(IP_DF);
400 iph->ttl = 1;
401 iph->protocol = IPPROTO_IGMP;
402 iph->saddr = br->multicast_query_use_ifaddr ?
403 inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0;
404 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
405 ((u8 *)&iph[1])[0] = IPOPT_RA;
406 ((u8 *)&iph[1])[1] = 4;
407 ((u8 *)&iph[1])[2] = 0;
408 ((u8 *)&iph[1])[3] = 0;
409 ip_send_check(iph);
410 skb_put(skb, 24);
412 skb_set_transport_header(skb, skb->len);
413 ih = igmp_hdr(skb);
414 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
415 ih->code = (group ? br->multicast_last_member_interval :
416 br->multicast_query_response_interval) /
417 (HZ / IGMP_TIMER_SCALE);
418 ih->group = group;
419 ih->csum = 0;
420 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
421 skb_put(skb, sizeof(*ih));
423 __skb_pull(skb, sizeof(*eth));
425 out:
426 return skb;
429 #if IS_ENABLED(CONFIG_IPV6)
430 static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
431 const struct in6_addr *group)
433 struct sk_buff *skb;
434 struct ipv6hdr *ip6h;
435 struct mld_msg *mldq;
436 struct ethhdr *eth;
437 u8 *hopopt;
438 unsigned long interval;
440 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
441 8 + sizeof(*mldq));
442 if (!skb)
443 goto out;
445 skb->protocol = htons(ETH_P_IPV6);
447 /* Ethernet header */
448 skb_reset_mac_header(skb);
449 eth = eth_hdr(skb);
451 ether_addr_copy(eth->h_source, br->dev->dev_addr);
452 eth->h_proto = htons(ETH_P_IPV6);
453 skb_put(skb, sizeof(*eth));
455 /* IPv6 header + HbH option */
456 skb_set_network_header(skb, skb->len);
457 ip6h = ipv6_hdr(skb);
459 *(__force __be32 *)ip6h = htonl(0x60000000);
460 ip6h->payload_len = htons(8 + sizeof(*mldq));
461 ip6h->nexthdr = IPPROTO_HOPOPTS;
462 ip6h->hop_limit = 1;
463 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
464 if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
465 &ip6h->saddr)) {
466 kfree_skb(skb);
467 br->has_ipv6_addr = 0;
468 return NULL;
471 br->has_ipv6_addr = 1;
472 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
474 hopopt = (u8 *)(ip6h + 1);
475 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
476 hopopt[1] = 0; /* length of HbH */
477 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
478 hopopt[3] = 2; /* Length of RA Option */
479 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
480 hopopt[5] = 0;
481 hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */
482 hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */
484 skb_put(skb, sizeof(*ip6h) + 8);
486 /* ICMPv6 */
487 skb_set_transport_header(skb, skb->len);
488 mldq = (struct mld_msg *) icmp6_hdr(skb);
490 interval = ipv6_addr_any(group) ?
491 br->multicast_query_response_interval :
492 br->multicast_last_member_interval;
494 mldq->mld_type = ICMPV6_MGM_QUERY;
495 mldq->mld_code = 0;
496 mldq->mld_cksum = 0;
497 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
498 mldq->mld_reserved = 0;
499 mldq->mld_mca = *group;
501 /* checksum */
502 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
503 sizeof(*mldq), IPPROTO_ICMPV6,
504 csum_partial(mldq,
505 sizeof(*mldq), 0));
506 skb_put(skb, sizeof(*mldq));
508 __skb_pull(skb, sizeof(*eth));
510 out:
511 return skb;
513 #endif
515 static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
516 struct br_ip *addr)
518 switch (addr->proto) {
519 case htons(ETH_P_IP):
520 return br_ip4_multicast_alloc_query(br, addr->u.ip4);
521 #if IS_ENABLED(CONFIG_IPV6)
522 case htons(ETH_P_IPV6):
523 return br_ip6_multicast_alloc_query(br, &addr->u.ip6);
524 #endif
526 return NULL;
529 static struct net_bridge_mdb_entry *br_multicast_get_group(
530 struct net_bridge *br, struct net_bridge_port *port,
531 struct br_ip *group, int hash)
533 struct net_bridge_mdb_htable *mdb;
534 struct net_bridge_mdb_entry *mp;
535 unsigned int count = 0;
536 unsigned int max;
537 int elasticity;
538 int err;
540 mdb = rcu_dereference_protected(br->mdb, 1);
541 hlist_for_each_entry(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
542 count++;
543 if (unlikely(br_ip_equal(group, &mp->addr)))
544 return mp;
547 elasticity = 0;
548 max = mdb->max;
550 if (unlikely(count > br->hash_elasticity && count)) {
551 if (net_ratelimit())
552 br_info(br, "Multicast hash table "
553 "chain limit reached: %s\n",
554 port ? port->dev->name : br->dev->name);
556 elasticity = br->hash_elasticity;
559 if (mdb->size >= max) {
560 max *= 2;
561 if (unlikely(max > br->hash_max)) {
562 br_warn(br, "Multicast hash table maximum of %d "
563 "reached, disabling snooping: %s\n",
564 br->hash_max,
565 port ? port->dev->name : br->dev->name);
566 err = -E2BIG;
567 disable:
568 br->multicast_disabled = 1;
569 goto err;
573 if (max > mdb->max || elasticity) {
574 if (mdb->old) {
575 if (net_ratelimit())
576 br_info(br, "Multicast hash table "
577 "on fire: %s\n",
578 port ? port->dev->name : br->dev->name);
579 err = -EEXIST;
580 goto err;
583 err = br_mdb_rehash(&br->mdb, max, elasticity);
584 if (err) {
585 br_warn(br, "Cannot rehash multicast "
586 "hash table, disabling snooping: %s, %d, %d\n",
587 port ? port->dev->name : br->dev->name,
588 mdb->size, err);
589 goto disable;
592 err = -EAGAIN;
593 goto err;
596 return NULL;
598 err:
599 mp = ERR_PTR(err);
600 return mp;
603 struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
604 struct net_bridge_port *port, struct br_ip *group)
606 struct net_bridge_mdb_htable *mdb;
607 struct net_bridge_mdb_entry *mp;
608 int hash;
609 int err;
611 mdb = rcu_dereference_protected(br->mdb, 1);
612 if (!mdb) {
613 err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0);
614 if (err)
615 return ERR_PTR(err);
616 goto rehash;
619 hash = br_ip_hash(mdb, group);
620 mp = br_multicast_get_group(br, port, group, hash);
621 switch (PTR_ERR(mp)) {
622 case 0:
623 break;
625 case -EAGAIN:
626 rehash:
627 mdb = rcu_dereference_protected(br->mdb, 1);
628 hash = br_ip_hash(mdb, group);
629 break;
631 default:
632 goto out;
635 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
636 if (unlikely(!mp))
637 return ERR_PTR(-ENOMEM);
639 mp->br = br;
640 mp->addr = *group;
641 setup_timer(&mp->timer, br_multicast_group_expired,
642 (unsigned long)mp);
644 hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
645 mdb->size++;
647 out:
648 return mp;
651 struct net_bridge_port_group *br_multicast_new_port_group(
652 struct net_bridge_port *port,
653 struct br_ip *group,
654 struct net_bridge_port_group __rcu *next,
655 unsigned char state)
657 struct net_bridge_port_group *p;
659 p = kzalloc(sizeof(*p), GFP_ATOMIC);
660 if (unlikely(!p))
661 return NULL;
663 p->addr = *group;
664 p->port = port;
665 p->state = state;
666 rcu_assign_pointer(p->next, next);
667 hlist_add_head(&p->mglist, &port->mglist);
668 setup_timer(&p->timer, br_multicast_port_group_expired,
669 (unsigned long)p);
670 return p;
673 static int br_multicast_add_group(struct net_bridge *br,
674 struct net_bridge_port *port,
675 struct br_ip *group)
677 struct net_bridge_mdb_entry *mp;
678 struct net_bridge_port_group *p;
679 struct net_bridge_port_group __rcu **pp;
680 unsigned long now = jiffies;
681 int err;
683 spin_lock(&br->multicast_lock);
684 if (!netif_running(br->dev) ||
685 (port && port->state == BR_STATE_DISABLED))
686 goto out;
688 mp = br_multicast_new_group(br, port, group);
689 err = PTR_ERR(mp);
690 if (IS_ERR(mp))
691 goto err;
693 if (!port) {
694 mp->mglist = true;
695 mod_timer(&mp->timer, now + br->multicast_membership_interval);
696 goto out;
699 for (pp = &mp->ports;
700 (p = mlock_dereference(*pp, br)) != NULL;
701 pp = &p->next) {
702 if (p->port == port)
703 goto found;
704 if ((unsigned long)p->port < (unsigned long)port)
705 break;
708 p = br_multicast_new_port_group(port, group, *pp, MDB_TEMPORARY);
709 if (unlikely(!p))
710 goto err;
711 rcu_assign_pointer(*pp, p);
712 br_mdb_notify(br->dev, port, group, RTM_NEWMDB, MDB_TEMPORARY);
714 found:
715 mod_timer(&p->timer, now + br->multicast_membership_interval);
716 out:
717 err = 0;
719 err:
720 spin_unlock(&br->multicast_lock);
721 return err;
724 static int br_ip4_multicast_add_group(struct net_bridge *br,
725 struct net_bridge_port *port,
726 __be32 group,
727 __u16 vid)
729 struct br_ip br_group;
731 if (ipv4_is_local_multicast(group))
732 return 0;
734 br_group.u.ip4 = group;
735 br_group.proto = htons(ETH_P_IP);
736 br_group.vid = vid;
738 return br_multicast_add_group(br, port, &br_group);
741 #if IS_ENABLED(CONFIG_IPV6)
742 static int br_ip6_multicast_add_group(struct net_bridge *br,
743 struct net_bridge_port *port,
744 const struct in6_addr *group,
745 __u16 vid)
747 struct br_ip br_group;
749 if (ipv6_addr_is_ll_all_nodes(group))
750 return 0;
752 br_group.u.ip6 = *group;
753 br_group.proto = htons(ETH_P_IPV6);
754 br_group.vid = vid;
756 return br_multicast_add_group(br, port, &br_group);
758 #endif
760 static void br_multicast_router_expired(unsigned long data)
762 struct net_bridge_port *port = (void *)data;
763 struct net_bridge *br = port->br;
765 spin_lock(&br->multicast_lock);
766 if (port->multicast_router != 1 ||
767 timer_pending(&port->multicast_router_timer) ||
768 hlist_unhashed(&port->rlist))
769 goto out;
771 hlist_del_init_rcu(&port->rlist);
772 br_rtr_notify(br->dev, port, RTM_DELMDB);
774 out:
775 spin_unlock(&br->multicast_lock);
778 static void br_multicast_local_router_expired(unsigned long data)
782 static void br_multicast_querier_expired(struct net_bridge *br,
783 struct bridge_mcast_own_query *query)
785 spin_lock(&br->multicast_lock);
786 if (!netif_running(br->dev) || br->multicast_disabled)
787 goto out;
789 br_multicast_start_querier(br, query);
791 out:
792 spin_unlock(&br->multicast_lock);
795 static void br_ip4_multicast_querier_expired(unsigned long data)
797 struct net_bridge *br = (void *)data;
799 br_multicast_querier_expired(br, &br->ip4_own_query);
802 #if IS_ENABLED(CONFIG_IPV6)
803 static void br_ip6_multicast_querier_expired(unsigned long data)
805 struct net_bridge *br = (void *)data;
807 br_multicast_querier_expired(br, &br->ip6_own_query);
809 #endif
811 static void br_multicast_select_own_querier(struct net_bridge *br,
812 struct br_ip *ip,
813 struct sk_buff *skb)
815 if (ip->proto == htons(ETH_P_IP))
816 br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr;
817 #if IS_ENABLED(CONFIG_IPV6)
818 else
819 br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr;
820 #endif
823 static void __br_multicast_send_query(struct net_bridge *br,
824 struct net_bridge_port *port,
825 struct br_ip *ip)
827 struct sk_buff *skb;
829 skb = br_multicast_alloc_query(br, ip);
830 if (!skb)
831 return;
833 if (port) {
834 skb->dev = port->dev;
835 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
836 dev_net(port->dev), NULL, skb, NULL, skb->dev,
837 br_dev_queue_push_xmit);
838 } else {
839 br_multicast_select_own_querier(br, ip, skb);
840 netif_rx(skb);
844 static void br_multicast_send_query(struct net_bridge *br,
845 struct net_bridge_port *port,
846 struct bridge_mcast_own_query *own_query)
848 unsigned long time;
849 struct br_ip br_group;
850 struct bridge_mcast_other_query *other_query = NULL;
852 if (!netif_running(br->dev) || br->multicast_disabled ||
853 !br->multicast_querier)
854 return;
856 memset(&br_group.u, 0, sizeof(br_group.u));
858 if (port ? (own_query == &port->ip4_own_query) :
859 (own_query == &br->ip4_own_query)) {
860 other_query = &br->ip4_other_query;
861 br_group.proto = htons(ETH_P_IP);
862 #if IS_ENABLED(CONFIG_IPV6)
863 } else {
864 other_query = &br->ip6_other_query;
865 br_group.proto = htons(ETH_P_IPV6);
866 #endif
869 if (!other_query || timer_pending(&other_query->timer))
870 return;
872 __br_multicast_send_query(br, port, &br_group);
874 time = jiffies;
875 time += own_query->startup_sent < br->multicast_startup_query_count ?
876 br->multicast_startup_query_interval :
877 br->multicast_query_interval;
878 mod_timer(&own_query->timer, time);
881 static void
882 br_multicast_port_query_expired(struct net_bridge_port *port,
883 struct bridge_mcast_own_query *query)
885 struct net_bridge *br = port->br;
887 spin_lock(&br->multicast_lock);
888 if (port->state == BR_STATE_DISABLED ||
889 port->state == BR_STATE_BLOCKING)
890 goto out;
892 if (query->startup_sent < br->multicast_startup_query_count)
893 query->startup_sent++;
895 br_multicast_send_query(port->br, port, query);
897 out:
898 spin_unlock(&br->multicast_lock);
901 static void br_ip4_multicast_port_query_expired(unsigned long data)
903 struct net_bridge_port *port = (void *)data;
905 br_multicast_port_query_expired(port, &port->ip4_own_query);
908 #if IS_ENABLED(CONFIG_IPV6)
909 static void br_ip6_multicast_port_query_expired(unsigned long data)
911 struct net_bridge_port *port = (void *)data;
913 br_multicast_port_query_expired(port, &port->ip6_own_query);
915 #endif
917 void br_multicast_add_port(struct net_bridge_port *port)
919 port->multicast_router = 1;
921 setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
922 (unsigned long)port);
923 setup_timer(&port->ip4_own_query.timer,
924 br_ip4_multicast_port_query_expired, (unsigned long)port);
925 #if IS_ENABLED(CONFIG_IPV6)
926 setup_timer(&port->ip6_own_query.timer,
927 br_ip6_multicast_port_query_expired, (unsigned long)port);
928 #endif
931 void br_multicast_del_port(struct net_bridge_port *port)
933 struct net_bridge *br = port->br;
934 struct net_bridge_port_group *pg;
935 struct hlist_node *n;
937 /* Take care of the remaining groups, only perm ones should be left */
938 spin_lock_bh(&br->multicast_lock);
939 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
940 br_multicast_del_pg(br, pg);
941 spin_unlock_bh(&br->multicast_lock);
942 del_timer_sync(&port->multicast_router_timer);
945 static void br_multicast_enable(struct bridge_mcast_own_query *query)
947 query->startup_sent = 0;
949 if (try_to_del_timer_sync(&query->timer) >= 0 ||
950 del_timer(&query->timer))
951 mod_timer(&query->timer, jiffies);
954 static void __br_multicast_enable_port(struct net_bridge_port *port)
956 struct net_bridge *br = port->br;
958 if (br->multicast_disabled || !netif_running(br->dev))
959 return;
961 br_multicast_enable(&port->ip4_own_query);
962 #if IS_ENABLED(CONFIG_IPV6)
963 br_multicast_enable(&port->ip6_own_query);
964 #endif
965 if (port->multicast_router == 2 && hlist_unhashed(&port->rlist))
966 br_multicast_add_router(br, port);
969 void br_multicast_enable_port(struct net_bridge_port *port)
971 struct net_bridge *br = port->br;
973 spin_lock(&br->multicast_lock);
974 __br_multicast_enable_port(port);
975 spin_unlock(&br->multicast_lock);
978 void br_multicast_disable_port(struct net_bridge_port *port)
980 struct net_bridge *br = port->br;
981 struct net_bridge_port_group *pg;
982 struct hlist_node *n;
984 spin_lock(&br->multicast_lock);
985 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
986 if (pg->state == MDB_TEMPORARY)
987 br_multicast_del_pg(br, pg);
989 if (!hlist_unhashed(&port->rlist)) {
990 hlist_del_init_rcu(&port->rlist);
991 br_rtr_notify(br->dev, port, RTM_DELMDB);
993 del_timer(&port->multicast_router_timer);
994 del_timer(&port->ip4_own_query.timer);
995 #if IS_ENABLED(CONFIG_IPV6)
996 del_timer(&port->ip6_own_query.timer);
997 #endif
998 spin_unlock(&br->multicast_lock);
1001 static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
1002 struct net_bridge_port *port,
1003 struct sk_buff *skb,
1004 u16 vid)
1006 struct igmpv3_report *ih;
1007 struct igmpv3_grec *grec;
1008 int i;
1009 int len;
1010 int num;
1011 int type;
1012 int err = 0;
1013 __be32 group;
1015 ih = igmpv3_report_hdr(skb);
1016 num = ntohs(ih->ngrec);
1017 len = skb_transport_offset(skb) + sizeof(*ih);
1019 for (i = 0; i < num; i++) {
1020 len += sizeof(*grec);
1021 if (!pskb_may_pull(skb, len))
1022 return -EINVAL;
1024 grec = (void *)(skb->data + len - sizeof(*grec));
1025 group = grec->grec_mca;
1026 type = grec->grec_type;
1028 len += ntohs(grec->grec_nsrcs) * 4;
1029 if (!pskb_may_pull(skb, len))
1030 return -EINVAL;
1032 /* We treat this as an IGMPv2 report for now. */
1033 switch (type) {
1034 case IGMPV3_MODE_IS_INCLUDE:
1035 case IGMPV3_MODE_IS_EXCLUDE:
1036 case IGMPV3_CHANGE_TO_INCLUDE:
1037 case IGMPV3_CHANGE_TO_EXCLUDE:
1038 case IGMPV3_ALLOW_NEW_SOURCES:
1039 case IGMPV3_BLOCK_OLD_SOURCES:
1040 break;
1042 default:
1043 continue;
1046 if ((type == IGMPV3_CHANGE_TO_INCLUDE ||
1047 type == IGMPV3_MODE_IS_INCLUDE) &&
1048 ntohs(grec->grec_nsrcs) == 0) {
1049 br_ip4_multicast_leave_group(br, port, group, vid);
1050 } else {
1051 err = br_ip4_multicast_add_group(br, port, group, vid);
1052 if (err)
1053 break;
1057 return err;
1060 #if IS_ENABLED(CONFIG_IPV6)
1061 static int br_ip6_multicast_mld2_report(struct net_bridge *br,
1062 struct net_bridge_port *port,
1063 struct sk_buff *skb,
1064 u16 vid)
1066 struct icmp6hdr *icmp6h;
1067 struct mld2_grec *grec;
1068 int i;
1069 int len;
1070 int num;
1071 int err = 0;
1073 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
1074 return -EINVAL;
1076 icmp6h = icmp6_hdr(skb);
1077 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
1078 len = skb_transport_offset(skb) + sizeof(*icmp6h);
1080 for (i = 0; i < num; i++) {
1081 __be16 *nsrcs, _nsrcs;
1083 nsrcs = skb_header_pointer(skb,
1084 len + offsetof(struct mld2_grec,
1085 grec_nsrcs),
1086 sizeof(_nsrcs), &_nsrcs);
1087 if (!nsrcs)
1088 return -EINVAL;
1090 if (!pskb_may_pull(skb,
1091 len + sizeof(*grec) +
1092 sizeof(struct in6_addr) * ntohs(*nsrcs)))
1093 return -EINVAL;
1095 grec = (struct mld2_grec *)(skb->data + len);
1096 len += sizeof(*grec) +
1097 sizeof(struct in6_addr) * ntohs(*nsrcs);
1099 /* We treat these as MLDv1 reports for now. */
1100 switch (grec->grec_type) {
1101 case MLD2_MODE_IS_INCLUDE:
1102 case MLD2_MODE_IS_EXCLUDE:
1103 case MLD2_CHANGE_TO_INCLUDE:
1104 case MLD2_CHANGE_TO_EXCLUDE:
1105 case MLD2_ALLOW_NEW_SOURCES:
1106 case MLD2_BLOCK_OLD_SOURCES:
1107 break;
1109 default:
1110 continue;
1113 if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE ||
1114 grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
1115 ntohs(*nsrcs) == 0) {
1116 br_ip6_multicast_leave_group(br, port, &grec->grec_mca,
1117 vid);
1118 } else {
1119 err = br_ip6_multicast_add_group(br, port,
1120 &grec->grec_mca, vid);
1121 if (err)
1122 break;
1126 return err;
1128 #endif
1130 static bool br_ip4_multicast_select_querier(struct net_bridge *br,
1131 struct net_bridge_port *port,
1132 __be32 saddr)
1134 if (!timer_pending(&br->ip4_own_query.timer) &&
1135 !timer_pending(&br->ip4_other_query.timer))
1136 goto update;
1138 if (!br->ip4_querier.addr.u.ip4)
1139 goto update;
1141 if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4))
1142 goto update;
1144 return false;
1146 update:
1147 br->ip4_querier.addr.u.ip4 = saddr;
1149 /* update protected by general multicast_lock by caller */
1150 rcu_assign_pointer(br->ip4_querier.port, port);
1152 return true;
1155 #if IS_ENABLED(CONFIG_IPV6)
1156 static bool br_ip6_multicast_select_querier(struct net_bridge *br,
1157 struct net_bridge_port *port,
1158 struct in6_addr *saddr)
1160 if (!timer_pending(&br->ip6_own_query.timer) &&
1161 !timer_pending(&br->ip6_other_query.timer))
1162 goto update;
1164 if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0)
1165 goto update;
1167 return false;
1169 update:
1170 br->ip6_querier.addr.u.ip6 = *saddr;
1172 /* update protected by general multicast_lock by caller */
1173 rcu_assign_pointer(br->ip6_querier.port, port);
1175 return true;
1177 #endif
1179 static bool br_multicast_select_querier(struct net_bridge *br,
1180 struct net_bridge_port *port,
1181 struct br_ip *saddr)
1183 switch (saddr->proto) {
1184 case htons(ETH_P_IP):
1185 return br_ip4_multicast_select_querier(br, port, saddr->u.ip4);
1186 #if IS_ENABLED(CONFIG_IPV6)
1187 case htons(ETH_P_IPV6):
1188 return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6);
1189 #endif
1192 return false;
1195 static void
1196 br_multicast_update_query_timer(struct net_bridge *br,
1197 struct bridge_mcast_other_query *query,
1198 unsigned long max_delay)
1200 if (!timer_pending(&query->timer))
1201 query->delay_time = jiffies + max_delay;
1203 mod_timer(&query->timer, jiffies + br->multicast_querier_interval);
1207 * Add port to router_list
1208 * list is maintained ordered by pointer value
1209 * and locked by br->multicast_lock and RCU
1211 static void br_multicast_add_router(struct net_bridge *br,
1212 struct net_bridge_port *port)
1214 struct net_bridge_port *p;
1215 struct hlist_node *slot = NULL;
1217 if (!hlist_unhashed(&port->rlist))
1218 return;
1220 hlist_for_each_entry(p, &br->router_list, rlist) {
1221 if ((unsigned long) port >= (unsigned long) p)
1222 break;
1223 slot = &p->rlist;
1226 if (slot)
1227 hlist_add_behind_rcu(&port->rlist, slot);
1228 else
1229 hlist_add_head_rcu(&port->rlist, &br->router_list);
1230 br_rtr_notify(br->dev, port, RTM_NEWMDB);
1233 static void br_multicast_mark_router(struct net_bridge *br,
1234 struct net_bridge_port *port)
1236 unsigned long now = jiffies;
1238 if (!port) {
1239 if (br->multicast_router == 1)
1240 mod_timer(&br->multicast_router_timer,
1241 now + br->multicast_querier_interval);
1242 return;
1245 if (port->multicast_router != 1)
1246 return;
1248 br_multicast_add_router(br, port);
1250 mod_timer(&port->multicast_router_timer,
1251 now + br->multicast_querier_interval);
1254 static void br_multicast_query_received(struct net_bridge *br,
1255 struct net_bridge_port *port,
1256 struct bridge_mcast_other_query *query,
1257 struct br_ip *saddr,
1258 unsigned long max_delay)
1260 if (!br_multicast_select_querier(br, port, saddr))
1261 return;
1263 br_multicast_update_query_timer(br, query, max_delay);
1264 br_multicast_mark_router(br, port);
1267 static int br_ip4_multicast_query(struct net_bridge *br,
1268 struct net_bridge_port *port,
1269 struct sk_buff *skb,
1270 u16 vid)
1272 const struct iphdr *iph = ip_hdr(skb);
1273 struct igmphdr *ih = igmp_hdr(skb);
1274 struct net_bridge_mdb_entry *mp;
1275 struct igmpv3_query *ih3;
1276 struct net_bridge_port_group *p;
1277 struct net_bridge_port_group __rcu **pp;
1278 struct br_ip saddr;
1279 unsigned long max_delay;
1280 unsigned long now = jiffies;
1281 unsigned int offset = skb_transport_offset(skb);
1282 __be32 group;
1283 int err = 0;
1285 spin_lock(&br->multicast_lock);
1286 if (!netif_running(br->dev) ||
1287 (port && port->state == BR_STATE_DISABLED))
1288 goto out;
1290 group = ih->group;
1292 if (skb->len == offset + sizeof(*ih)) {
1293 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1295 if (!max_delay) {
1296 max_delay = 10 * HZ;
1297 group = 0;
1299 } else if (skb->len >= offset + sizeof(*ih3)) {
1300 ih3 = igmpv3_query_hdr(skb);
1301 if (ih3->nsrcs)
1302 goto out;
1304 max_delay = ih3->code ?
1305 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
1306 } else {
1307 goto out;
1310 if (!group) {
1311 saddr.proto = htons(ETH_P_IP);
1312 saddr.u.ip4 = iph->saddr;
1314 br_multicast_query_received(br, port, &br->ip4_other_query,
1315 &saddr, max_delay);
1316 goto out;
1319 mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid);
1320 if (!mp)
1321 goto out;
1323 max_delay *= br->multicast_last_member_count;
1325 if (mp->mglist &&
1326 (timer_pending(&mp->timer) ?
1327 time_after(mp->timer.expires, now + max_delay) :
1328 try_to_del_timer_sync(&mp->timer) >= 0))
1329 mod_timer(&mp->timer, now + max_delay);
1331 for (pp = &mp->ports;
1332 (p = mlock_dereference(*pp, br)) != NULL;
1333 pp = &p->next) {
1334 if (timer_pending(&p->timer) ?
1335 time_after(p->timer.expires, now + max_delay) :
1336 try_to_del_timer_sync(&p->timer) >= 0)
1337 mod_timer(&p->timer, now + max_delay);
1340 out:
1341 spin_unlock(&br->multicast_lock);
1342 return err;
1345 #if IS_ENABLED(CONFIG_IPV6)
1346 static int br_ip6_multicast_query(struct net_bridge *br,
1347 struct net_bridge_port *port,
1348 struct sk_buff *skb,
1349 u16 vid)
1351 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
1352 struct mld_msg *mld;
1353 struct net_bridge_mdb_entry *mp;
1354 struct mld2_query *mld2q;
1355 struct net_bridge_port_group *p;
1356 struct net_bridge_port_group __rcu **pp;
1357 struct br_ip saddr;
1358 unsigned long max_delay;
1359 unsigned long now = jiffies;
1360 unsigned int offset = skb_transport_offset(skb);
1361 const struct in6_addr *group = NULL;
1362 bool is_general_query;
1363 int err = 0;
1365 spin_lock(&br->multicast_lock);
1366 if (!netif_running(br->dev) ||
1367 (port && port->state == BR_STATE_DISABLED))
1368 goto out;
1370 if (skb->len == offset + sizeof(*mld)) {
1371 if (!pskb_may_pull(skb, offset + sizeof(*mld))) {
1372 err = -EINVAL;
1373 goto out;
1375 mld = (struct mld_msg *) icmp6_hdr(skb);
1376 max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
1377 if (max_delay)
1378 group = &mld->mld_mca;
1379 } else {
1380 if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) {
1381 err = -EINVAL;
1382 goto out;
1384 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1385 if (!mld2q->mld2q_nsrcs)
1386 group = &mld2q->mld2q_mca;
1388 max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
1391 is_general_query = group && ipv6_addr_any(group);
1393 if (is_general_query) {
1394 saddr.proto = htons(ETH_P_IPV6);
1395 saddr.u.ip6 = ip6h->saddr;
1397 br_multicast_query_received(br, port, &br->ip6_other_query,
1398 &saddr, max_delay);
1399 goto out;
1400 } else if (!group) {
1401 goto out;
1404 mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid);
1405 if (!mp)
1406 goto out;
1408 max_delay *= br->multicast_last_member_count;
1409 if (mp->mglist &&
1410 (timer_pending(&mp->timer) ?
1411 time_after(mp->timer.expires, now + max_delay) :
1412 try_to_del_timer_sync(&mp->timer) >= 0))
1413 mod_timer(&mp->timer, now + max_delay);
1415 for (pp = &mp->ports;
1416 (p = mlock_dereference(*pp, br)) != NULL;
1417 pp = &p->next) {
1418 if (timer_pending(&p->timer) ?
1419 time_after(p->timer.expires, now + max_delay) :
1420 try_to_del_timer_sync(&p->timer) >= 0)
1421 mod_timer(&p->timer, now + max_delay);
1424 out:
1425 spin_unlock(&br->multicast_lock);
1426 return err;
1428 #endif
1430 static void
1431 br_multicast_leave_group(struct net_bridge *br,
1432 struct net_bridge_port *port,
1433 struct br_ip *group,
1434 struct bridge_mcast_other_query *other_query,
1435 struct bridge_mcast_own_query *own_query)
1437 struct net_bridge_mdb_htable *mdb;
1438 struct net_bridge_mdb_entry *mp;
1439 struct net_bridge_port_group *p;
1440 unsigned long now;
1441 unsigned long time;
1443 spin_lock(&br->multicast_lock);
1444 if (!netif_running(br->dev) ||
1445 (port && port->state == BR_STATE_DISABLED))
1446 goto out;
1448 mdb = mlock_dereference(br->mdb, br);
1449 mp = br_mdb_ip_get(mdb, group);
1450 if (!mp)
1451 goto out;
1453 if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
1454 struct net_bridge_port_group __rcu **pp;
1456 for (pp = &mp->ports;
1457 (p = mlock_dereference(*pp, br)) != NULL;
1458 pp = &p->next) {
1459 if (p->port != port)
1460 continue;
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 p->state);
1469 if (!mp->ports && !mp->mglist &&
1470 netif_running(br->dev))
1471 mod_timer(&mp->timer, jiffies);
1473 goto out;
1476 if (timer_pending(&other_query->timer))
1477 goto out;
1479 if (br->multicast_querier) {
1480 __br_multicast_send_query(br, port, &mp->addr);
1482 time = jiffies + br->multicast_last_member_count *
1483 br->multicast_last_member_interval;
1485 mod_timer(&own_query->timer, time);
1487 for (p = mlock_dereference(mp->ports, br);
1488 p != NULL;
1489 p = mlock_dereference(p->next, br)) {
1490 if (p->port != port)
1491 continue;
1493 if (!hlist_unhashed(&p->mglist) &&
1494 (timer_pending(&p->timer) ?
1495 time_after(p->timer.expires, time) :
1496 try_to_del_timer_sync(&p->timer) >= 0)) {
1497 mod_timer(&p->timer, time);
1500 break;
1504 now = jiffies;
1505 time = now + br->multicast_last_member_count *
1506 br->multicast_last_member_interval;
1508 if (!port) {
1509 if (mp->mglist &&
1510 (timer_pending(&mp->timer) ?
1511 time_after(mp->timer.expires, time) :
1512 try_to_del_timer_sync(&mp->timer) >= 0)) {
1513 mod_timer(&mp->timer, time);
1516 goto out;
1519 for (p = mlock_dereference(mp->ports, br);
1520 p != NULL;
1521 p = mlock_dereference(p->next, br)) {
1522 if (p->port != port)
1523 continue;
1525 if (!hlist_unhashed(&p->mglist) &&
1526 (timer_pending(&p->timer) ?
1527 time_after(p->timer.expires, time) :
1528 try_to_del_timer_sync(&p->timer) >= 0)) {
1529 mod_timer(&p->timer, time);
1532 break;
1534 out:
1535 spin_unlock(&br->multicast_lock);
1538 static void br_ip4_multicast_leave_group(struct net_bridge *br,
1539 struct net_bridge_port *port,
1540 __be32 group,
1541 __u16 vid)
1543 struct br_ip br_group;
1544 struct bridge_mcast_own_query *own_query;
1546 if (ipv4_is_local_multicast(group))
1547 return;
1549 own_query = port ? &port->ip4_own_query : &br->ip4_own_query;
1551 br_group.u.ip4 = group;
1552 br_group.proto = htons(ETH_P_IP);
1553 br_group.vid = vid;
1555 br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
1556 own_query);
1559 #if IS_ENABLED(CONFIG_IPV6)
1560 static void br_ip6_multicast_leave_group(struct net_bridge *br,
1561 struct net_bridge_port *port,
1562 const struct in6_addr *group,
1563 __u16 vid)
1565 struct br_ip br_group;
1566 struct bridge_mcast_own_query *own_query;
1568 if (ipv6_addr_is_ll_all_nodes(group))
1569 return;
1571 own_query = port ? &port->ip6_own_query : &br->ip6_own_query;
1573 br_group.u.ip6 = *group;
1574 br_group.proto = htons(ETH_P_IPV6);
1575 br_group.vid = vid;
1577 br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
1578 own_query);
1580 #endif
1582 static int br_multicast_ipv4_rcv(struct net_bridge *br,
1583 struct net_bridge_port *port,
1584 struct sk_buff *skb,
1585 u16 vid)
1587 struct sk_buff *skb_trimmed = NULL;
1588 struct igmphdr *ih;
1589 int err;
1591 err = ip_mc_check_igmp(skb, &skb_trimmed);
1593 if (err == -ENOMSG) {
1594 if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr))
1595 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1596 return 0;
1597 } else if (err < 0) {
1598 return err;
1601 BR_INPUT_SKB_CB(skb)->igmp = 1;
1602 ih = igmp_hdr(skb);
1604 switch (ih->type) {
1605 case IGMP_HOST_MEMBERSHIP_REPORT:
1606 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1607 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1608 err = br_ip4_multicast_add_group(br, port, ih->group, vid);
1609 break;
1610 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1611 err = br_ip4_multicast_igmp3_report(br, port, skb_trimmed, vid);
1612 break;
1613 case IGMP_HOST_MEMBERSHIP_QUERY:
1614 err = br_ip4_multicast_query(br, port, skb_trimmed, vid);
1615 break;
1616 case IGMP_HOST_LEAVE_MESSAGE:
1617 br_ip4_multicast_leave_group(br, port, ih->group, vid);
1618 break;
1621 if (skb_trimmed && skb_trimmed != skb)
1622 kfree_skb(skb_trimmed);
1624 return err;
1627 #if IS_ENABLED(CONFIG_IPV6)
1628 static int br_multicast_ipv6_rcv(struct net_bridge *br,
1629 struct net_bridge_port *port,
1630 struct sk_buff *skb,
1631 u16 vid)
1633 struct sk_buff *skb_trimmed = NULL;
1634 struct mld_msg *mld;
1635 int err;
1637 err = ipv6_mc_check_mld(skb, &skb_trimmed);
1639 if (err == -ENOMSG) {
1640 if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
1641 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1642 return 0;
1643 } else if (err < 0) {
1644 return err;
1647 BR_INPUT_SKB_CB(skb)->igmp = 1;
1648 mld = (struct mld_msg *)skb_transport_header(skb);
1650 switch (mld->mld_type) {
1651 case ICMPV6_MGM_REPORT:
1652 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1653 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid);
1654 break;
1655 case ICMPV6_MLD2_REPORT:
1656 err = br_ip6_multicast_mld2_report(br, port, skb_trimmed, vid);
1657 break;
1658 case ICMPV6_MGM_QUERY:
1659 err = br_ip6_multicast_query(br, port, skb_trimmed, vid);
1660 break;
1661 case ICMPV6_MGM_REDUCTION:
1662 br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid);
1663 break;
1666 if (skb_trimmed && skb_trimmed != skb)
1667 kfree_skb(skb_trimmed);
1669 return err;
1671 #endif
1673 int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1674 struct sk_buff *skb, u16 vid)
1676 BR_INPUT_SKB_CB(skb)->igmp = 0;
1677 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1679 if (br->multicast_disabled)
1680 return 0;
1682 switch (skb->protocol) {
1683 case htons(ETH_P_IP):
1684 return br_multicast_ipv4_rcv(br, port, skb, vid);
1685 #if IS_ENABLED(CONFIG_IPV6)
1686 case htons(ETH_P_IPV6):
1687 return br_multicast_ipv6_rcv(br, port, skb, vid);
1688 #endif
1691 return 0;
1694 static void br_multicast_query_expired(struct net_bridge *br,
1695 struct bridge_mcast_own_query *query,
1696 struct bridge_mcast_querier *querier)
1698 spin_lock(&br->multicast_lock);
1699 if (query->startup_sent < br->multicast_startup_query_count)
1700 query->startup_sent++;
1702 RCU_INIT_POINTER(querier->port, NULL);
1703 br_multicast_send_query(br, NULL, query);
1704 spin_unlock(&br->multicast_lock);
1707 static void br_ip4_multicast_query_expired(unsigned long data)
1709 struct net_bridge *br = (void *)data;
1711 br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier);
1714 #if IS_ENABLED(CONFIG_IPV6)
1715 static void br_ip6_multicast_query_expired(unsigned long data)
1717 struct net_bridge *br = (void *)data;
1719 br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier);
1721 #endif
1723 void br_multicast_init(struct net_bridge *br)
1725 br->hash_elasticity = 4;
1726 br->hash_max = 512;
1728 br->multicast_router = 1;
1729 br->multicast_querier = 0;
1730 br->multicast_query_use_ifaddr = 0;
1731 br->multicast_last_member_count = 2;
1732 br->multicast_startup_query_count = 2;
1734 br->multicast_last_member_interval = HZ;
1735 br->multicast_query_response_interval = 10 * HZ;
1736 br->multicast_startup_query_interval = 125 * HZ / 4;
1737 br->multicast_query_interval = 125 * HZ;
1738 br->multicast_querier_interval = 255 * HZ;
1739 br->multicast_membership_interval = 260 * HZ;
1741 br->ip4_other_query.delay_time = 0;
1742 br->ip4_querier.port = NULL;
1743 #if IS_ENABLED(CONFIG_IPV6)
1744 br->ip6_other_query.delay_time = 0;
1745 br->ip6_querier.port = NULL;
1746 #endif
1747 br->has_ipv6_addr = 1;
1749 spin_lock_init(&br->multicast_lock);
1750 setup_timer(&br->multicast_router_timer,
1751 br_multicast_local_router_expired, 0);
1752 setup_timer(&br->ip4_other_query.timer,
1753 br_ip4_multicast_querier_expired, (unsigned long)br);
1754 setup_timer(&br->ip4_own_query.timer, br_ip4_multicast_query_expired,
1755 (unsigned long)br);
1756 #if IS_ENABLED(CONFIG_IPV6)
1757 setup_timer(&br->ip6_other_query.timer,
1758 br_ip6_multicast_querier_expired, (unsigned long)br);
1759 setup_timer(&br->ip6_own_query.timer, br_ip6_multicast_query_expired,
1760 (unsigned long)br);
1761 #endif
1764 static void __br_multicast_open(struct net_bridge *br,
1765 struct bridge_mcast_own_query *query)
1767 query->startup_sent = 0;
1769 if (br->multicast_disabled)
1770 return;
1772 mod_timer(&query->timer, jiffies);
1775 void br_multicast_open(struct net_bridge *br)
1777 __br_multicast_open(br, &br->ip4_own_query);
1778 #if IS_ENABLED(CONFIG_IPV6)
1779 __br_multicast_open(br, &br->ip6_own_query);
1780 #endif
1783 void br_multicast_stop(struct net_bridge *br)
1785 del_timer_sync(&br->multicast_router_timer);
1786 del_timer_sync(&br->ip4_other_query.timer);
1787 del_timer_sync(&br->ip4_own_query.timer);
1788 #if IS_ENABLED(CONFIG_IPV6)
1789 del_timer_sync(&br->ip6_other_query.timer);
1790 del_timer_sync(&br->ip6_own_query.timer);
1791 #endif
1794 void br_multicast_dev_del(struct net_bridge *br)
1796 struct net_bridge_mdb_htable *mdb;
1797 struct net_bridge_mdb_entry *mp;
1798 struct hlist_node *n;
1799 u32 ver;
1800 int i;
1802 spin_lock_bh(&br->multicast_lock);
1803 mdb = mlock_dereference(br->mdb, br);
1804 if (!mdb)
1805 goto out;
1807 br->mdb = NULL;
1809 ver = mdb->ver;
1810 for (i = 0; i < mdb->max; i++) {
1811 hlist_for_each_entry_safe(mp, n, &mdb->mhash[i],
1812 hlist[ver]) {
1813 del_timer(&mp->timer);
1814 call_rcu_bh(&mp->rcu, br_multicast_free_group);
1818 if (mdb->old) {
1819 spin_unlock_bh(&br->multicast_lock);
1820 rcu_barrier_bh();
1821 spin_lock_bh(&br->multicast_lock);
1822 WARN_ON(mdb->old);
1825 mdb->old = mdb;
1826 call_rcu_bh(&mdb->rcu, br_mdb_free);
1828 out:
1829 spin_unlock_bh(&br->multicast_lock);
1832 int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1834 int err = -EINVAL;
1836 spin_lock_bh(&br->multicast_lock);
1838 switch (val) {
1839 case 0:
1840 case 2:
1841 del_timer(&br->multicast_router_timer);
1842 /* fall through */
1843 case 1:
1844 br->multicast_router = val;
1845 err = 0;
1846 break;
1849 spin_unlock_bh(&br->multicast_lock);
1851 return err;
1854 int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1856 struct net_bridge *br = p->br;
1857 int err = -EINVAL;
1859 spin_lock(&br->multicast_lock);
1861 switch (val) {
1862 case 0:
1863 case 1:
1864 case 2:
1865 p->multicast_router = val;
1866 err = 0;
1868 if (val < 2 && !hlist_unhashed(&p->rlist)) {
1869 hlist_del_init_rcu(&p->rlist);
1870 br_rtr_notify(br->dev, p, RTM_DELMDB);
1873 if (val == 1)
1874 break;
1876 del_timer(&p->multicast_router_timer);
1878 if (val == 0)
1879 break;
1881 br_multicast_add_router(br, p);
1882 break;
1885 spin_unlock(&br->multicast_lock);
1887 return err;
1890 static void br_multicast_start_querier(struct net_bridge *br,
1891 struct bridge_mcast_own_query *query)
1893 struct net_bridge_port *port;
1895 __br_multicast_open(br, query);
1897 list_for_each_entry(port, &br->port_list, list) {
1898 if (port->state == BR_STATE_DISABLED ||
1899 port->state == BR_STATE_BLOCKING)
1900 continue;
1902 if (query == &br->ip4_own_query)
1903 br_multicast_enable(&port->ip4_own_query);
1904 #if IS_ENABLED(CONFIG_IPV6)
1905 else
1906 br_multicast_enable(&port->ip6_own_query);
1907 #endif
1911 int br_multicast_toggle(struct net_bridge *br, unsigned long val)
1913 struct net_bridge_mdb_htable *mdb;
1914 struct net_bridge_port *port;
1915 int err = 0;
1917 spin_lock_bh(&br->multicast_lock);
1918 if (br->multicast_disabled == !val)
1919 goto unlock;
1921 br->multicast_disabled = !val;
1922 if (br->multicast_disabled)
1923 goto unlock;
1925 if (!netif_running(br->dev))
1926 goto unlock;
1928 mdb = mlock_dereference(br->mdb, br);
1929 if (mdb) {
1930 if (mdb->old) {
1931 err = -EEXIST;
1932 rollback:
1933 br->multicast_disabled = !!val;
1934 goto unlock;
1937 err = br_mdb_rehash(&br->mdb, mdb->max,
1938 br->hash_elasticity);
1939 if (err)
1940 goto rollback;
1943 br_multicast_open(br);
1944 list_for_each_entry(port, &br->port_list, list)
1945 __br_multicast_enable_port(port);
1947 unlock:
1948 spin_unlock_bh(&br->multicast_lock);
1950 return err;
1953 int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
1955 unsigned long max_delay;
1957 val = !!val;
1959 spin_lock_bh(&br->multicast_lock);
1960 if (br->multicast_querier == val)
1961 goto unlock;
1963 br->multicast_querier = val;
1964 if (!val)
1965 goto unlock;
1967 max_delay = br->multicast_query_response_interval;
1969 if (!timer_pending(&br->ip4_other_query.timer))
1970 br->ip4_other_query.delay_time = jiffies + max_delay;
1972 br_multicast_start_querier(br, &br->ip4_own_query);
1974 #if IS_ENABLED(CONFIG_IPV6)
1975 if (!timer_pending(&br->ip6_other_query.timer))
1976 br->ip6_other_query.delay_time = jiffies + max_delay;
1978 br_multicast_start_querier(br, &br->ip6_own_query);
1979 #endif
1981 unlock:
1982 spin_unlock_bh(&br->multicast_lock);
1984 return 0;
1987 int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
1989 int err = -EINVAL;
1990 u32 old;
1991 struct net_bridge_mdb_htable *mdb;
1993 spin_lock_bh(&br->multicast_lock);
1994 if (!is_power_of_2(val))
1995 goto unlock;
1997 mdb = mlock_dereference(br->mdb, br);
1998 if (mdb && val < mdb->size)
1999 goto unlock;
2001 err = 0;
2003 old = br->hash_max;
2004 br->hash_max = val;
2006 if (mdb) {
2007 if (mdb->old) {
2008 err = -EEXIST;
2009 rollback:
2010 br->hash_max = old;
2011 goto unlock;
2014 err = br_mdb_rehash(&br->mdb, br->hash_max,
2015 br->hash_elasticity);
2016 if (err)
2017 goto rollback;
2020 unlock:
2021 spin_unlock_bh(&br->multicast_lock);
2023 return err;
2027 * br_multicast_list_adjacent - Returns snooped multicast addresses
2028 * @dev: The bridge port adjacent to which to retrieve addresses
2029 * @br_ip_list: The list to store found, snooped multicast IP addresses in
2031 * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
2032 * snooping feature on all bridge ports of dev's bridge device, excluding
2033 * the addresses from dev itself.
2035 * Returns the number of items added to br_ip_list.
2037 * Notes:
2038 * - br_ip_list needs to be initialized by caller
2039 * - br_ip_list might contain duplicates in the end
2040 * (needs to be taken care of by caller)
2041 * - br_ip_list needs to be freed by caller
2043 int br_multicast_list_adjacent(struct net_device *dev,
2044 struct list_head *br_ip_list)
2046 struct net_bridge *br;
2047 struct net_bridge_port *port;
2048 struct net_bridge_port_group *group;
2049 struct br_ip_list *entry;
2050 int count = 0;
2052 rcu_read_lock();
2053 if (!br_ip_list || !br_port_exists(dev))
2054 goto unlock;
2056 port = br_port_get_rcu(dev);
2057 if (!port || !port->br)
2058 goto unlock;
2060 br = port->br;
2062 list_for_each_entry_rcu(port, &br->port_list, list) {
2063 if (!port->dev || port->dev == dev)
2064 continue;
2066 hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
2067 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
2068 if (!entry)
2069 goto unlock;
2071 entry->addr = group->addr;
2072 list_add(&entry->list, br_ip_list);
2073 count++;
2077 unlock:
2078 rcu_read_unlock();
2079 return count;
2081 EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
2084 * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2085 * @dev: The bridge port providing the bridge on which to check for a querier
2086 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2088 * Checks whether the given interface has a bridge on top and if so returns
2089 * true if a valid querier exists anywhere on the bridged link layer.
2090 * Otherwise returns false.
2092 bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
2094 struct net_bridge *br;
2095 struct net_bridge_port *port;
2096 struct ethhdr eth;
2097 bool ret = false;
2099 rcu_read_lock();
2100 if (!br_port_exists(dev))
2101 goto unlock;
2103 port = br_port_get_rcu(dev);
2104 if (!port || !port->br)
2105 goto unlock;
2107 br = port->br;
2109 memset(&eth, 0, sizeof(eth));
2110 eth.h_proto = htons(proto);
2112 ret = br_multicast_querier_exists(br, &eth);
2114 unlock:
2115 rcu_read_unlock();
2116 return ret;
2118 EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
2121 * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
2122 * @dev: The bridge port adjacent to which to check for a querier
2123 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2125 * Checks whether the given interface has a bridge on top and if so returns
2126 * true if a selected querier is behind one of the other ports of this
2127 * bridge. Otherwise returns false.
2129 bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
2131 struct net_bridge *br;
2132 struct net_bridge_port *port;
2133 bool ret = false;
2135 rcu_read_lock();
2136 if (!br_port_exists(dev))
2137 goto unlock;
2139 port = br_port_get_rcu(dev);
2140 if (!port || !port->br)
2141 goto unlock;
2143 br = port->br;
2145 switch (proto) {
2146 case ETH_P_IP:
2147 if (!timer_pending(&br->ip4_other_query.timer) ||
2148 rcu_dereference(br->ip4_querier.port) == port)
2149 goto unlock;
2150 break;
2151 #if IS_ENABLED(CONFIG_IPV6)
2152 case ETH_P_IPV6:
2153 if (!timer_pending(&br->ip6_other_query.timer) ||
2154 rcu_dereference(br->ip6_querier.port) == port)
2155 goto unlock;
2156 break;
2157 #endif
2158 default:
2159 goto unlock;
2162 ret = true;
2163 unlock:
2164 rcu_read_unlock();
2165 return ret;
2167 EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);