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)
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>
29 #if IS_ENABLED(CONFIG_IPV6)
32 #include <net/ip6_checksum.h>
33 #include <net/addrconf.h>
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 unsigned int br_mdb_rehash_seq
;
44 static inline int br_ip_equal(const struct br_ip
*a
, const struct br_ip
*b
)
46 if (a
->proto
!= b
->proto
)
52 return a
->u
.ip4
== b
->u
.ip4
;
53 #if IS_ENABLED(CONFIG_IPV6)
54 case htons(ETH_P_IPV6
):
55 return ipv6_addr_equal(&a
->u
.ip6
, &b
->u
.ip6
);
61 static inline int __br_ip4_hash(struct net_bridge_mdb_htable
*mdb
, __be32 ip
,
64 return jhash_2words((__force u32
)ip
, vid
, mdb
->secret
) & (mdb
->max
- 1);
67 #if IS_ENABLED(CONFIG_IPV6)
68 static inline int __br_ip6_hash(struct net_bridge_mdb_htable
*mdb
,
69 const struct in6_addr
*ip
,
72 return jhash_2words(ipv6_addr_hash(ip
), vid
,
73 mdb
->secret
) & (mdb
->max
- 1);
77 static inline int br_ip_hash(struct net_bridge_mdb_htable
*mdb
,
82 return __br_ip4_hash(mdb
, ip
->u
.ip4
, ip
->vid
);
83 #if IS_ENABLED(CONFIG_IPV6)
84 case htons(ETH_P_IPV6
):
85 return __br_ip6_hash(mdb
, &ip
->u
.ip6
, ip
->vid
);
91 static struct net_bridge_mdb_entry
*__br_mdb_ip_get(
92 struct net_bridge_mdb_htable
*mdb
, struct br_ip
*dst
, int hash
)
94 struct net_bridge_mdb_entry
*mp
;
96 hlist_for_each_entry_rcu(mp
, &mdb
->mhash
[hash
], hlist
[mdb
->ver
]) {
97 if (br_ip_equal(&mp
->addr
, dst
))
104 struct net_bridge_mdb_entry
*br_mdb_ip_get(struct net_bridge_mdb_htable
*mdb
,
110 return __br_mdb_ip_get(mdb
, dst
, br_ip_hash(mdb
, dst
));
113 static struct net_bridge_mdb_entry
*br_mdb_ip4_get(
114 struct net_bridge_mdb_htable
*mdb
, __be32 dst
, __u16 vid
)
119 br_dst
.proto
= htons(ETH_P_IP
);
122 return br_mdb_ip_get(mdb
, &br_dst
);
125 #if IS_ENABLED(CONFIG_IPV6)
126 static struct net_bridge_mdb_entry
*br_mdb_ip6_get(
127 struct net_bridge_mdb_htable
*mdb
, const struct in6_addr
*dst
,
133 br_dst
.proto
= htons(ETH_P_IPV6
);
136 return br_mdb_ip_get(mdb
, &br_dst
);
140 struct net_bridge_mdb_entry
*br_mdb_get(struct net_bridge
*br
,
141 struct sk_buff
*skb
, u16 vid
)
143 struct net_bridge_mdb_htable
*mdb
= rcu_dereference(br
->mdb
);
146 if (br
->multicast_disabled
)
149 if (BR_INPUT_SKB_CB(skb
)->igmp
)
152 ip
.proto
= skb
->protocol
;
155 switch (skb
->protocol
) {
156 case htons(ETH_P_IP
):
157 ip
.u
.ip4
= ip_hdr(skb
)->daddr
;
159 #if IS_ENABLED(CONFIG_IPV6)
160 case htons(ETH_P_IPV6
):
161 ip
.u
.ip6
= ipv6_hdr(skb
)->daddr
;
168 return br_mdb_ip_get(mdb
, &ip
);
171 static void br_mdb_free(struct rcu_head
*head
)
173 struct net_bridge_mdb_htable
*mdb
=
174 container_of(head
, struct net_bridge_mdb_htable
, rcu
);
175 struct net_bridge_mdb_htable
*old
= mdb
->old
;
182 static int br_mdb_copy(struct net_bridge_mdb_htable
*new,
183 struct net_bridge_mdb_htable
*old
,
186 struct net_bridge_mdb_entry
*mp
;
191 for (i
= 0; i
< old
->max
; i
++)
192 hlist_for_each_entry(mp
, &old
->mhash
[i
], hlist
[old
->ver
])
193 hlist_add_head(&mp
->hlist
[new->ver
],
194 &new->mhash
[br_ip_hash(new, &mp
->addr
)]);
200 for (i
= 0; i
< new->max
; i
++) {
202 hlist_for_each_entry(mp
, &new->mhash
[i
], hlist
[new->ver
])
208 return maxlen
> elasticity
? -EINVAL
: 0;
211 void br_multicast_free_pg(struct rcu_head
*head
)
213 struct net_bridge_port_group
*p
=
214 container_of(head
, struct net_bridge_port_group
, rcu
);
219 static void br_multicast_free_group(struct rcu_head
*head
)
221 struct net_bridge_mdb_entry
*mp
=
222 container_of(head
, struct net_bridge_mdb_entry
, rcu
);
227 static void br_multicast_group_expired(unsigned long data
)
229 struct net_bridge_mdb_entry
*mp
= (void *)data
;
230 struct net_bridge
*br
= mp
->br
;
231 struct net_bridge_mdb_htable
*mdb
;
233 spin_lock(&br
->multicast_lock
);
234 if (!netif_running(br
->dev
) || timer_pending(&mp
->timer
))
242 mdb
= mlock_dereference(br
->mdb
, br
);
244 hlist_del_rcu(&mp
->hlist
[mdb
->ver
]);
247 call_rcu_bh(&mp
->rcu
, br_multicast_free_group
);
250 spin_unlock(&br
->multicast_lock
);
253 static void br_multicast_del_pg(struct net_bridge
*br
,
254 struct net_bridge_port_group
*pg
)
256 struct net_bridge_mdb_htable
*mdb
;
257 struct net_bridge_mdb_entry
*mp
;
258 struct net_bridge_port_group
*p
;
259 struct net_bridge_port_group __rcu
**pp
;
261 mdb
= mlock_dereference(br
->mdb
, br
);
263 mp
= br_mdb_ip_get(mdb
, &pg
->addr
);
267 for (pp
= &mp
->ports
;
268 (p
= mlock_dereference(*pp
, br
)) != NULL
;
273 rcu_assign_pointer(*pp
, p
->next
);
274 hlist_del_init(&p
->mglist
);
275 del_timer(&p
->timer
);
276 br_mdb_notify(br
->dev
, p
->port
, &pg
->addr
, RTM_DELMDB
,
278 call_rcu_bh(&p
->rcu
, br_multicast_free_pg
);
280 if (!mp
->ports
&& !mp
->mglist
&&
281 netif_running(br
->dev
))
282 mod_timer(&mp
->timer
, jiffies
);
290 static void br_multicast_port_group_expired(unsigned long data
)
292 struct net_bridge_port_group
*pg
= (void *)data
;
293 struct net_bridge
*br
= pg
->port
->br
;
295 spin_lock(&br
->multicast_lock
);
296 if (!netif_running(br
->dev
) || timer_pending(&pg
->timer
) ||
297 hlist_unhashed(&pg
->mglist
) || pg
->state
& MDB_PERMANENT
)
300 br_multicast_del_pg(br
, pg
);
303 spin_unlock(&br
->multicast_lock
);
306 static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu
**mdbp
, int max
,
309 struct net_bridge_mdb_htable
*old
= rcu_dereference_protected(*mdbp
, 1);
310 struct net_bridge_mdb_htable
*mdb
;
313 mdb
= kmalloc(sizeof(*mdb
), GFP_ATOMIC
);
320 mdb
->mhash
= kzalloc(max
* sizeof(*mdb
->mhash
), GFP_ATOMIC
);
326 mdb
->size
= old
? old
->size
: 0;
327 mdb
->ver
= old
? old
->ver
^ 1 : 0;
329 if (!old
|| elasticity
)
330 get_random_bytes(&mdb
->secret
, sizeof(mdb
->secret
));
332 mdb
->secret
= old
->secret
;
337 err
= br_mdb_copy(mdb
, old
, elasticity
);
345 call_rcu_bh(&mdb
->rcu
, br_mdb_free
);
348 rcu_assign_pointer(*mdbp
, mdb
);
353 static struct sk_buff
*br_ip4_multicast_alloc_query(struct net_bridge
*br
,
361 skb
= netdev_alloc_skb_ip_align(br
->dev
, sizeof(*eth
) + sizeof(*iph
) +
366 skb
->protocol
= htons(ETH_P_IP
);
368 skb_reset_mac_header(skb
);
371 ether_addr_copy(eth
->h_source
, br
->dev
->dev_addr
);
374 eth
->h_dest
[2] = 0x5e;
378 eth
->h_proto
= htons(ETH_P_IP
);
379 skb_put(skb
, sizeof(*eth
));
381 skb_set_network_header(skb
, skb
->len
);
387 iph
->tot_len
= htons(sizeof(*iph
) + sizeof(*ih
) + 4);
389 iph
->frag_off
= htons(IP_DF
);
391 iph
->protocol
= IPPROTO_IGMP
;
392 iph
->saddr
= br
->multicast_query_use_ifaddr
?
393 inet_select_addr(br
->dev
, 0, RT_SCOPE_LINK
) : 0;
394 iph
->daddr
= htonl(INADDR_ALLHOSTS_GROUP
);
395 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
396 ((u8
*)&iph
[1])[1] = 4;
397 ((u8
*)&iph
[1])[2] = 0;
398 ((u8
*)&iph
[1])[3] = 0;
402 skb_set_transport_header(skb
, skb
->len
);
404 ih
->type
= IGMP_HOST_MEMBERSHIP_QUERY
;
405 ih
->code
= (group
? br
->multicast_last_member_interval
:
406 br
->multicast_query_response_interval
) /
407 (HZ
/ IGMP_TIMER_SCALE
);
410 ih
->csum
= ip_compute_csum((void *)ih
, sizeof(struct igmphdr
));
411 skb_put(skb
, sizeof(*ih
));
413 __skb_pull(skb
, sizeof(*eth
));
419 #if IS_ENABLED(CONFIG_IPV6)
420 static struct sk_buff
*br_ip6_multicast_alloc_query(struct net_bridge
*br
,
421 const struct in6_addr
*group
)
424 struct ipv6hdr
*ip6h
;
425 struct mld_msg
*mldq
;
428 unsigned long interval
;
430 skb
= netdev_alloc_skb_ip_align(br
->dev
, sizeof(*eth
) + sizeof(*ip6h
) +
435 skb
->protocol
= htons(ETH_P_IPV6
);
437 /* Ethernet header */
438 skb_reset_mac_header(skb
);
441 ether_addr_copy(eth
->h_source
, br
->dev
->dev_addr
);
442 eth
->h_proto
= htons(ETH_P_IPV6
);
443 skb_put(skb
, sizeof(*eth
));
445 /* IPv6 header + HbH option */
446 skb_set_network_header(skb
, skb
->len
);
447 ip6h
= ipv6_hdr(skb
);
449 *(__force __be32
*)ip6h
= htonl(0x60000000);
450 ip6h
->payload_len
= htons(8 + sizeof(*mldq
));
451 ip6h
->nexthdr
= IPPROTO_HOPOPTS
;
453 ipv6_addr_set(&ip6h
->daddr
, htonl(0xff020000), 0, 0, htonl(1));
454 if (ipv6_dev_get_saddr(dev_net(br
->dev
), br
->dev
, &ip6h
->daddr
, 0,
459 ipv6_eth_mc_map(&ip6h
->daddr
, eth
->h_dest
);
461 hopopt
= (u8
*)(ip6h
+ 1);
462 hopopt
[0] = IPPROTO_ICMPV6
; /* next hdr */
463 hopopt
[1] = 0; /* length of HbH */
464 hopopt
[2] = IPV6_TLV_ROUTERALERT
; /* Router Alert */
465 hopopt
[3] = 2; /* Length of RA Option */
466 hopopt
[4] = 0; /* Type = 0x0000 (MLD) */
468 hopopt
[6] = IPV6_TLV_PAD1
; /* Pad1 */
469 hopopt
[7] = IPV6_TLV_PAD1
; /* Pad1 */
471 skb_put(skb
, sizeof(*ip6h
) + 8);
474 skb_set_transport_header(skb
, skb
->len
);
475 mldq
= (struct mld_msg
*) icmp6_hdr(skb
);
477 interval
= ipv6_addr_any(group
) ?
478 br
->multicast_query_response_interval
:
479 br
->multicast_last_member_interval
;
481 mldq
->mld_type
= ICMPV6_MGM_QUERY
;
484 mldq
->mld_maxdelay
= htons((u16
)jiffies_to_msecs(interval
));
485 mldq
->mld_reserved
= 0;
486 mldq
->mld_mca
= *group
;
489 mldq
->mld_cksum
= csum_ipv6_magic(&ip6h
->saddr
, &ip6h
->daddr
,
490 sizeof(*mldq
), IPPROTO_ICMPV6
,
493 skb_put(skb
, sizeof(*mldq
));
495 __skb_pull(skb
, sizeof(*eth
));
502 static struct sk_buff
*br_multicast_alloc_query(struct net_bridge
*br
,
505 switch (addr
->proto
) {
506 case htons(ETH_P_IP
):
507 return br_ip4_multicast_alloc_query(br
, addr
->u
.ip4
);
508 #if IS_ENABLED(CONFIG_IPV6)
509 case htons(ETH_P_IPV6
):
510 return br_ip6_multicast_alloc_query(br
, &addr
->u
.ip6
);
516 static struct net_bridge_mdb_entry
*br_multicast_get_group(
517 struct net_bridge
*br
, struct net_bridge_port
*port
,
518 struct br_ip
*group
, int hash
)
520 struct net_bridge_mdb_htable
*mdb
;
521 struct net_bridge_mdb_entry
*mp
;
522 unsigned int count
= 0;
527 mdb
= rcu_dereference_protected(br
->mdb
, 1);
528 hlist_for_each_entry(mp
, &mdb
->mhash
[hash
], hlist
[mdb
->ver
]) {
530 if (unlikely(br_ip_equal(group
, &mp
->addr
)))
537 if (unlikely(count
> br
->hash_elasticity
&& count
)) {
539 br_info(br
, "Multicast hash table "
540 "chain limit reached: %s\n",
541 port
? port
->dev
->name
: br
->dev
->name
);
543 elasticity
= br
->hash_elasticity
;
546 if (mdb
->size
>= max
) {
548 if (unlikely(max
> br
->hash_max
)) {
549 br_warn(br
, "Multicast hash table maximum of %d "
550 "reached, disabling snooping: %s\n",
552 port
? port
->dev
->name
: br
->dev
->name
);
555 br
->multicast_disabled
= 1;
560 if (max
> mdb
->max
|| elasticity
) {
563 br_info(br
, "Multicast hash table "
565 port
? port
->dev
->name
: br
->dev
->name
);
570 err
= br_mdb_rehash(&br
->mdb
, max
, elasticity
);
572 br_warn(br
, "Cannot rehash multicast "
573 "hash table, disabling snooping: %s, %d, %d\n",
574 port
? port
->dev
->name
: br
->dev
->name
,
590 struct net_bridge_mdb_entry
*br_multicast_new_group(struct net_bridge
*br
,
591 struct net_bridge_port
*port
, struct br_ip
*group
)
593 struct net_bridge_mdb_htable
*mdb
;
594 struct net_bridge_mdb_entry
*mp
;
598 mdb
= rcu_dereference_protected(br
->mdb
, 1);
600 err
= br_mdb_rehash(&br
->mdb
, BR_HASH_SIZE
, 0);
606 hash
= br_ip_hash(mdb
, group
);
607 mp
= br_multicast_get_group(br
, port
, group
, hash
);
608 switch (PTR_ERR(mp
)) {
614 mdb
= rcu_dereference_protected(br
->mdb
, 1);
615 hash
= br_ip_hash(mdb
, group
);
622 mp
= kzalloc(sizeof(*mp
), GFP_ATOMIC
);
624 return ERR_PTR(-ENOMEM
);
628 setup_timer(&mp
->timer
, br_multicast_group_expired
,
631 hlist_add_head_rcu(&mp
->hlist
[mdb
->ver
], &mdb
->mhash
[hash
]);
638 struct net_bridge_port_group
*br_multicast_new_port_group(
639 struct net_bridge_port
*port
,
641 struct net_bridge_port_group __rcu
*next
,
644 struct net_bridge_port_group
*p
;
646 p
= kzalloc(sizeof(*p
), GFP_ATOMIC
);
653 rcu_assign_pointer(p
->next
, next
);
654 hlist_add_head(&p
->mglist
, &port
->mglist
);
655 setup_timer(&p
->timer
, br_multicast_port_group_expired
,
660 static int br_multicast_add_group(struct net_bridge
*br
,
661 struct net_bridge_port
*port
,
664 struct net_bridge_mdb_entry
*mp
;
665 struct net_bridge_port_group
*p
;
666 struct net_bridge_port_group __rcu
**pp
;
667 unsigned long now
= jiffies
;
670 spin_lock(&br
->multicast_lock
);
671 if (!netif_running(br
->dev
) ||
672 (port
&& port
->state
== BR_STATE_DISABLED
))
675 mp
= br_multicast_new_group(br
, port
, group
);
682 mod_timer(&mp
->timer
, now
+ br
->multicast_membership_interval
);
686 for (pp
= &mp
->ports
;
687 (p
= mlock_dereference(*pp
, br
)) != NULL
;
691 if ((unsigned long)p
->port
< (unsigned long)port
)
695 p
= br_multicast_new_port_group(port
, group
, *pp
, MDB_TEMPORARY
);
698 rcu_assign_pointer(*pp
, p
);
699 br_mdb_notify(br
->dev
, port
, group
, RTM_NEWMDB
, MDB_TEMPORARY
);
702 mod_timer(&p
->timer
, now
+ br
->multicast_membership_interval
);
707 spin_unlock(&br
->multicast_lock
);
711 static int br_ip4_multicast_add_group(struct net_bridge
*br
,
712 struct net_bridge_port
*port
,
716 struct br_ip br_group
;
718 if (ipv4_is_local_multicast(group
))
721 br_group
.u
.ip4
= group
;
722 br_group
.proto
= htons(ETH_P_IP
);
725 return br_multicast_add_group(br
, port
, &br_group
);
728 #if IS_ENABLED(CONFIG_IPV6)
729 static int br_ip6_multicast_add_group(struct net_bridge
*br
,
730 struct net_bridge_port
*port
,
731 const struct in6_addr
*group
,
734 struct br_ip br_group
;
736 if (ipv6_addr_is_ll_all_nodes(group
))
739 br_group
.u
.ip6
= *group
;
740 br_group
.proto
= htons(ETH_P_IPV6
);
743 return br_multicast_add_group(br
, port
, &br_group
);
747 static void br_multicast_router_expired(unsigned long data
)
749 struct net_bridge_port
*port
= (void *)data
;
750 struct net_bridge
*br
= port
->br
;
752 spin_lock(&br
->multicast_lock
);
753 if (port
->multicast_router
!= 1 ||
754 timer_pending(&port
->multicast_router_timer
) ||
755 hlist_unhashed(&port
->rlist
))
758 hlist_del_init_rcu(&port
->rlist
);
761 spin_unlock(&br
->multicast_lock
);
764 static void br_multicast_local_router_expired(unsigned long data
)
768 static void br_multicast_querier_expired(struct net_bridge
*br
,
769 struct bridge_mcast_own_query
*query
)
771 spin_lock(&br
->multicast_lock
);
772 if (!netif_running(br
->dev
) || br
->multicast_disabled
)
775 br_multicast_start_querier(br
, query
);
778 spin_unlock(&br
->multicast_lock
);
781 static void br_ip4_multicast_querier_expired(unsigned long data
)
783 struct net_bridge
*br
= (void *)data
;
785 br_multicast_querier_expired(br
, &br
->ip4_own_query
);
788 #if IS_ENABLED(CONFIG_IPV6)
789 static void br_ip6_multicast_querier_expired(unsigned long data
)
791 struct net_bridge
*br
= (void *)data
;
793 br_multicast_querier_expired(br
, &br
->ip6_own_query
);
797 static void br_multicast_select_own_querier(struct net_bridge
*br
,
801 if (ip
->proto
== htons(ETH_P_IP
))
802 br
->ip4_querier
.addr
.u
.ip4
= ip_hdr(skb
)->saddr
;
803 #if IS_ENABLED(CONFIG_IPV6)
805 br
->ip6_querier
.addr
.u
.ip6
= ipv6_hdr(skb
)->saddr
;
809 static void __br_multicast_send_query(struct net_bridge
*br
,
810 struct net_bridge_port
*port
,
815 skb
= br_multicast_alloc_query(br
, ip
);
820 skb
->dev
= port
->dev
;
821 NF_HOOK(NFPROTO_BRIDGE
, NF_BR_LOCAL_OUT
, NULL
, skb
,
823 br_dev_queue_push_xmit
);
825 br_multicast_select_own_querier(br
, ip
, skb
);
830 static void br_multicast_send_query(struct net_bridge
*br
,
831 struct net_bridge_port
*port
,
832 struct bridge_mcast_own_query
*own_query
)
835 struct br_ip br_group
;
836 struct bridge_mcast_other_query
*other_query
= NULL
;
838 if (!netif_running(br
->dev
) || br
->multicast_disabled
||
839 !br
->multicast_querier
)
842 memset(&br_group
.u
, 0, sizeof(br_group
.u
));
844 if (port
? (own_query
== &port
->ip4_own_query
) :
845 (own_query
== &br
->ip4_own_query
)) {
846 other_query
= &br
->ip4_other_query
;
847 br_group
.proto
= htons(ETH_P_IP
);
848 #if IS_ENABLED(CONFIG_IPV6)
850 other_query
= &br
->ip6_other_query
;
851 br_group
.proto
= htons(ETH_P_IPV6
);
855 if (!other_query
|| timer_pending(&other_query
->timer
))
858 __br_multicast_send_query(br
, port
, &br_group
);
861 time
+= own_query
->startup_sent
< br
->multicast_startup_query_count
?
862 br
->multicast_startup_query_interval
:
863 br
->multicast_query_interval
;
864 mod_timer(&own_query
->timer
, time
);
868 br_multicast_port_query_expired(struct net_bridge_port
*port
,
869 struct bridge_mcast_own_query
*query
)
871 struct net_bridge
*br
= port
->br
;
873 spin_lock(&br
->multicast_lock
);
874 if (port
->state
== BR_STATE_DISABLED
||
875 port
->state
== BR_STATE_BLOCKING
)
878 if (query
->startup_sent
< br
->multicast_startup_query_count
)
879 query
->startup_sent
++;
881 br_multicast_send_query(port
->br
, port
, query
);
884 spin_unlock(&br
->multicast_lock
);
887 static void br_ip4_multicast_port_query_expired(unsigned long data
)
889 struct net_bridge_port
*port
= (void *)data
;
891 br_multicast_port_query_expired(port
, &port
->ip4_own_query
);
894 #if IS_ENABLED(CONFIG_IPV6)
895 static void br_ip6_multicast_port_query_expired(unsigned long data
)
897 struct net_bridge_port
*port
= (void *)data
;
899 br_multicast_port_query_expired(port
, &port
->ip6_own_query
);
903 void br_multicast_add_port(struct net_bridge_port
*port
)
905 port
->multicast_router
= 1;
907 setup_timer(&port
->multicast_router_timer
, br_multicast_router_expired
,
908 (unsigned long)port
);
909 setup_timer(&port
->ip4_own_query
.timer
,
910 br_ip4_multicast_port_query_expired
, (unsigned long)port
);
911 #if IS_ENABLED(CONFIG_IPV6)
912 setup_timer(&port
->ip6_own_query
.timer
,
913 br_ip6_multicast_port_query_expired
, (unsigned long)port
);
917 void br_multicast_del_port(struct net_bridge_port
*port
)
919 struct net_bridge
*br
= port
->br
;
920 struct net_bridge_port_group
*pg
;
921 struct hlist_node
*n
;
923 /* Take care of the remaining groups, only perm ones should be left */
924 spin_lock_bh(&br
->multicast_lock
);
925 hlist_for_each_entry_safe(pg
, n
, &port
->mglist
, mglist
)
926 br_multicast_del_pg(br
, pg
);
927 spin_unlock_bh(&br
->multicast_lock
);
928 del_timer_sync(&port
->multicast_router_timer
);
931 static void br_multicast_enable(struct bridge_mcast_own_query
*query
)
933 query
->startup_sent
= 0;
935 if (try_to_del_timer_sync(&query
->timer
) >= 0 ||
936 del_timer(&query
->timer
))
937 mod_timer(&query
->timer
, jiffies
);
940 void br_multicast_enable_port(struct net_bridge_port
*port
)
942 struct net_bridge
*br
= port
->br
;
944 spin_lock(&br
->multicast_lock
);
945 if (br
->multicast_disabled
|| !netif_running(br
->dev
))
948 br_multicast_enable(&port
->ip4_own_query
);
949 #if IS_ENABLED(CONFIG_IPV6)
950 br_multicast_enable(&port
->ip6_own_query
);
952 if (port
->multicast_router
== 2 && hlist_unhashed(&port
->rlist
))
953 br_multicast_add_router(br
, port
);
956 spin_unlock(&br
->multicast_lock
);
959 void br_multicast_disable_port(struct net_bridge_port
*port
)
961 struct net_bridge
*br
= port
->br
;
962 struct net_bridge_port_group
*pg
;
963 struct hlist_node
*n
;
965 spin_lock(&br
->multicast_lock
);
966 hlist_for_each_entry_safe(pg
, n
, &port
->mglist
, mglist
)
967 if (pg
->state
== MDB_TEMPORARY
)
968 br_multicast_del_pg(br
, pg
);
970 if (!hlist_unhashed(&port
->rlist
))
971 hlist_del_init_rcu(&port
->rlist
);
972 del_timer(&port
->multicast_router_timer
);
973 del_timer(&port
->ip4_own_query
.timer
);
974 #if IS_ENABLED(CONFIG_IPV6)
975 del_timer(&port
->ip6_own_query
.timer
);
977 spin_unlock(&br
->multicast_lock
);
980 static int br_ip4_multicast_igmp3_report(struct net_bridge
*br
,
981 struct net_bridge_port
*port
,
985 struct igmpv3_report
*ih
;
986 struct igmpv3_grec
*grec
;
994 ih
= igmpv3_report_hdr(skb
);
995 num
= ntohs(ih
->ngrec
);
998 for (i
= 0; i
< num
; i
++) {
999 len
+= sizeof(*grec
);
1000 if (!pskb_may_pull(skb
, len
))
1003 grec
= (void *)(skb
->data
+ len
- sizeof(*grec
));
1004 group
= grec
->grec_mca
;
1005 type
= grec
->grec_type
;
1007 len
+= ntohs(grec
->grec_nsrcs
) * 4;
1008 if (!pskb_may_pull(skb
, len
))
1011 /* We treat this as an IGMPv2 report for now. */
1013 case IGMPV3_MODE_IS_INCLUDE
:
1014 case IGMPV3_MODE_IS_EXCLUDE
:
1015 case IGMPV3_CHANGE_TO_INCLUDE
:
1016 case IGMPV3_CHANGE_TO_EXCLUDE
:
1017 case IGMPV3_ALLOW_NEW_SOURCES
:
1018 case IGMPV3_BLOCK_OLD_SOURCES
:
1025 err
= br_ip4_multicast_add_group(br
, port
, group
, vid
);
1033 #if IS_ENABLED(CONFIG_IPV6)
1034 static int br_ip6_multicast_mld2_report(struct net_bridge
*br
,
1035 struct net_bridge_port
*port
,
1036 struct sk_buff
*skb
,
1039 struct icmp6hdr
*icmp6h
;
1040 struct mld2_grec
*grec
;
1046 if (!pskb_may_pull(skb
, sizeof(*icmp6h
)))
1049 icmp6h
= icmp6_hdr(skb
);
1050 num
= ntohs(icmp6h
->icmp6_dataun
.un_data16
[1]);
1051 len
= sizeof(*icmp6h
);
1053 for (i
= 0; i
< num
; i
++) {
1054 __be16
*nsrcs
, _nsrcs
;
1056 nsrcs
= skb_header_pointer(skb
,
1057 len
+ offsetof(struct mld2_grec
,
1059 sizeof(_nsrcs
), &_nsrcs
);
1063 if (!pskb_may_pull(skb
,
1064 len
+ sizeof(*grec
) +
1065 sizeof(struct in6_addr
) * ntohs(*nsrcs
)))
1068 grec
= (struct mld2_grec
*)(skb
->data
+ len
);
1069 len
+= sizeof(*grec
) +
1070 sizeof(struct in6_addr
) * ntohs(*nsrcs
);
1072 /* We treat these as MLDv1 reports for now. */
1073 switch (grec
->grec_type
) {
1074 case MLD2_MODE_IS_INCLUDE
:
1075 case MLD2_MODE_IS_EXCLUDE
:
1076 case MLD2_CHANGE_TO_INCLUDE
:
1077 case MLD2_CHANGE_TO_EXCLUDE
:
1078 case MLD2_ALLOW_NEW_SOURCES
:
1079 case MLD2_BLOCK_OLD_SOURCES
:
1086 err
= br_ip6_multicast_add_group(br
, port
, &grec
->grec_mca
,
1096 static bool br_ip4_multicast_select_querier(struct net_bridge
*br
,
1097 struct net_bridge_port
*port
,
1100 if (!timer_pending(&br
->ip4_own_query
.timer
) &&
1101 !timer_pending(&br
->ip4_other_query
.timer
))
1104 if (!br
->ip4_querier
.addr
.u
.ip4
)
1107 if (ntohl(saddr
) <= ntohl(br
->ip4_querier
.addr
.u
.ip4
))
1113 br
->ip4_querier
.addr
.u
.ip4
= saddr
;
1115 /* update protected by general multicast_lock by caller */
1116 rcu_assign_pointer(br
->ip4_querier
.port
, port
);
1121 #if IS_ENABLED(CONFIG_IPV6)
1122 static bool br_ip6_multicast_select_querier(struct net_bridge
*br
,
1123 struct net_bridge_port
*port
,
1124 struct in6_addr
*saddr
)
1126 if (!timer_pending(&br
->ip6_own_query
.timer
) &&
1127 !timer_pending(&br
->ip6_other_query
.timer
))
1130 if (ipv6_addr_cmp(saddr
, &br
->ip6_querier
.addr
.u
.ip6
) <= 0)
1136 br
->ip6_querier
.addr
.u
.ip6
= *saddr
;
1138 /* update protected by general multicast_lock by caller */
1139 rcu_assign_pointer(br
->ip6_querier
.port
, port
);
1145 static bool br_multicast_select_querier(struct net_bridge
*br
,
1146 struct net_bridge_port
*port
,
1147 struct br_ip
*saddr
)
1149 switch (saddr
->proto
) {
1150 case htons(ETH_P_IP
):
1151 return br_ip4_multicast_select_querier(br
, port
, saddr
->u
.ip4
);
1152 #if IS_ENABLED(CONFIG_IPV6)
1153 case htons(ETH_P_IPV6
):
1154 return br_ip6_multicast_select_querier(br
, port
, &saddr
->u
.ip6
);
1162 br_multicast_update_query_timer(struct net_bridge
*br
,
1163 struct bridge_mcast_other_query
*query
,
1164 unsigned long max_delay
)
1166 if (!timer_pending(&query
->timer
))
1167 query
->delay_time
= jiffies
+ max_delay
;
1169 mod_timer(&query
->timer
, jiffies
+ br
->multicast_querier_interval
);
1173 * Add port to router_list
1174 * list is maintained ordered by pointer value
1175 * and locked by br->multicast_lock and RCU
1177 static void br_multicast_add_router(struct net_bridge
*br
,
1178 struct net_bridge_port
*port
)
1180 struct net_bridge_port
*p
;
1181 struct hlist_node
*slot
= NULL
;
1183 if (!hlist_unhashed(&port
->rlist
))
1186 hlist_for_each_entry(p
, &br
->router_list
, rlist
) {
1187 if ((unsigned long) port
>= (unsigned long) p
)
1193 hlist_add_behind_rcu(&port
->rlist
, slot
);
1195 hlist_add_head_rcu(&port
->rlist
, &br
->router_list
);
1198 static void br_multicast_mark_router(struct net_bridge
*br
,
1199 struct net_bridge_port
*port
)
1201 unsigned long now
= jiffies
;
1204 if (br
->multicast_router
== 1)
1205 mod_timer(&br
->multicast_router_timer
,
1206 now
+ br
->multicast_querier_interval
);
1210 if (port
->multicast_router
!= 1)
1213 br_multicast_add_router(br
, port
);
1215 mod_timer(&port
->multicast_router_timer
,
1216 now
+ br
->multicast_querier_interval
);
1219 static void br_multicast_query_received(struct net_bridge
*br
,
1220 struct net_bridge_port
*port
,
1221 struct bridge_mcast_other_query
*query
,
1222 struct br_ip
*saddr
,
1223 unsigned long max_delay
)
1225 if (!br_multicast_select_querier(br
, port
, saddr
))
1228 br_multicast_update_query_timer(br
, query
, max_delay
);
1229 br_multicast_mark_router(br
, port
);
1232 static int br_ip4_multicast_query(struct net_bridge
*br
,
1233 struct net_bridge_port
*port
,
1234 struct sk_buff
*skb
,
1237 const struct iphdr
*iph
= ip_hdr(skb
);
1238 struct igmphdr
*ih
= igmp_hdr(skb
);
1239 struct net_bridge_mdb_entry
*mp
;
1240 struct igmpv3_query
*ih3
;
1241 struct net_bridge_port_group
*p
;
1242 struct net_bridge_port_group __rcu
**pp
;
1244 unsigned long max_delay
;
1245 unsigned long now
= jiffies
;
1249 spin_lock(&br
->multicast_lock
);
1250 if (!netif_running(br
->dev
) ||
1251 (port
&& port
->state
== BR_STATE_DISABLED
))
1256 if (skb
->len
== sizeof(*ih
)) {
1257 max_delay
= ih
->code
* (HZ
/ IGMP_TIMER_SCALE
);
1260 max_delay
= 10 * HZ
;
1263 } else if (skb
->len
>= sizeof(*ih3
)) {
1264 ih3
= igmpv3_query_hdr(skb
);
1268 max_delay
= ih3
->code
?
1269 IGMPV3_MRC(ih3
->code
) * (HZ
/ IGMP_TIMER_SCALE
) : 1;
1275 saddr
.proto
= htons(ETH_P_IP
);
1276 saddr
.u
.ip4
= iph
->saddr
;
1278 br_multicast_query_received(br
, port
, &br
->ip4_other_query
,
1283 mp
= br_mdb_ip4_get(mlock_dereference(br
->mdb
, br
), group
, vid
);
1287 max_delay
*= br
->multicast_last_member_count
;
1290 (timer_pending(&mp
->timer
) ?
1291 time_after(mp
->timer
.expires
, now
+ max_delay
) :
1292 try_to_del_timer_sync(&mp
->timer
) >= 0))
1293 mod_timer(&mp
->timer
, now
+ max_delay
);
1295 for (pp
= &mp
->ports
;
1296 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1298 if (timer_pending(&p
->timer
) ?
1299 time_after(p
->timer
.expires
, now
+ max_delay
) :
1300 try_to_del_timer_sync(&p
->timer
) >= 0)
1301 mod_timer(&p
->timer
, now
+ max_delay
);
1305 spin_unlock(&br
->multicast_lock
);
1309 #if IS_ENABLED(CONFIG_IPV6)
1310 static int br_ip6_multicast_query(struct net_bridge
*br
,
1311 struct net_bridge_port
*port
,
1312 struct sk_buff
*skb
,
1315 const struct ipv6hdr
*ip6h
= ipv6_hdr(skb
);
1316 struct mld_msg
*mld
;
1317 struct net_bridge_mdb_entry
*mp
;
1318 struct mld2_query
*mld2q
;
1319 struct net_bridge_port_group
*p
;
1320 struct net_bridge_port_group __rcu
**pp
;
1322 unsigned long max_delay
;
1323 unsigned long now
= jiffies
;
1324 const struct in6_addr
*group
= NULL
;
1325 bool is_general_query
;
1328 spin_lock(&br
->multicast_lock
);
1329 if (!netif_running(br
->dev
) ||
1330 (port
&& port
->state
== BR_STATE_DISABLED
))
1333 if (skb
->len
== sizeof(*mld
)) {
1334 if (!pskb_may_pull(skb
, sizeof(*mld
))) {
1338 mld
= (struct mld_msg
*) icmp6_hdr(skb
);
1339 max_delay
= msecs_to_jiffies(ntohs(mld
->mld_maxdelay
));
1341 group
= &mld
->mld_mca
;
1343 if (!pskb_may_pull(skb
, sizeof(*mld2q
))) {
1347 mld2q
= (struct mld2_query
*)icmp6_hdr(skb
);
1348 if (!mld2q
->mld2q_nsrcs
)
1349 group
= &mld2q
->mld2q_mca
;
1351 max_delay
= max(msecs_to_jiffies(mldv2_mrc(mld2q
)), 1UL);
1354 is_general_query
= group
&& ipv6_addr_any(group
);
1356 if (is_general_query
) {
1357 saddr
.proto
= htons(ETH_P_IPV6
);
1358 saddr
.u
.ip6
= ip6h
->saddr
;
1360 br_multicast_query_received(br
, port
, &br
->ip6_other_query
,
1363 } else if (!group
) {
1367 mp
= br_mdb_ip6_get(mlock_dereference(br
->mdb
, br
), group
, vid
);
1371 max_delay
*= br
->multicast_last_member_count
;
1373 (timer_pending(&mp
->timer
) ?
1374 time_after(mp
->timer
.expires
, now
+ max_delay
) :
1375 try_to_del_timer_sync(&mp
->timer
) >= 0))
1376 mod_timer(&mp
->timer
, now
+ max_delay
);
1378 for (pp
= &mp
->ports
;
1379 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1381 if (timer_pending(&p
->timer
) ?
1382 time_after(p
->timer
.expires
, now
+ max_delay
) :
1383 try_to_del_timer_sync(&p
->timer
) >= 0)
1384 mod_timer(&p
->timer
, now
+ max_delay
);
1388 spin_unlock(&br
->multicast_lock
);
1394 br_multicast_leave_group(struct net_bridge
*br
,
1395 struct net_bridge_port
*port
,
1396 struct br_ip
*group
,
1397 struct bridge_mcast_other_query
*other_query
,
1398 struct bridge_mcast_own_query
*own_query
)
1400 struct net_bridge_mdb_htable
*mdb
;
1401 struct net_bridge_mdb_entry
*mp
;
1402 struct net_bridge_port_group
*p
;
1406 spin_lock(&br
->multicast_lock
);
1407 if (!netif_running(br
->dev
) ||
1408 (port
&& port
->state
== BR_STATE_DISABLED
) ||
1409 timer_pending(&other_query
->timer
))
1412 mdb
= mlock_dereference(br
->mdb
, br
);
1413 mp
= br_mdb_ip_get(mdb
, group
);
1417 if (br
->multicast_querier
) {
1418 __br_multicast_send_query(br
, port
, &mp
->addr
);
1420 time
= jiffies
+ br
->multicast_last_member_count
*
1421 br
->multicast_last_member_interval
;
1423 mod_timer(&own_query
->timer
, time
);
1425 for (p
= mlock_dereference(mp
->ports
, br
);
1427 p
= mlock_dereference(p
->next
, br
)) {
1428 if (p
->port
!= port
)
1431 if (!hlist_unhashed(&p
->mglist
) &&
1432 (timer_pending(&p
->timer
) ?
1433 time_after(p
->timer
.expires
, time
) :
1434 try_to_del_timer_sync(&p
->timer
) >= 0)) {
1435 mod_timer(&p
->timer
, time
);
1442 if (port
&& (port
->flags
& BR_MULTICAST_FAST_LEAVE
)) {
1443 struct net_bridge_port_group __rcu
**pp
;
1445 for (pp
= &mp
->ports
;
1446 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1448 if (p
->port
!= port
)
1451 rcu_assign_pointer(*pp
, p
->next
);
1452 hlist_del_init(&p
->mglist
);
1453 del_timer(&p
->timer
);
1454 br_mdb_notify(br
->dev
, port
, group
, RTM_DELMDB
,
1456 call_rcu_bh(&p
->rcu
, br_multicast_free_pg
);
1458 if (!mp
->ports
&& !mp
->mglist
&&
1459 netif_running(br
->dev
))
1460 mod_timer(&mp
->timer
, jiffies
);
1466 time
= now
+ br
->multicast_last_member_count
*
1467 br
->multicast_last_member_interval
;
1471 (timer_pending(&mp
->timer
) ?
1472 time_after(mp
->timer
.expires
, time
) :
1473 try_to_del_timer_sync(&mp
->timer
) >= 0)) {
1474 mod_timer(&mp
->timer
, time
);
1480 for (p
= mlock_dereference(mp
->ports
, br
);
1482 p
= mlock_dereference(p
->next
, br
)) {
1483 if (p
->port
!= port
)
1486 if (!hlist_unhashed(&p
->mglist
) &&
1487 (timer_pending(&p
->timer
) ?
1488 time_after(p
->timer
.expires
, time
) :
1489 try_to_del_timer_sync(&p
->timer
) >= 0)) {
1490 mod_timer(&p
->timer
, time
);
1496 spin_unlock(&br
->multicast_lock
);
1499 static void br_ip4_multicast_leave_group(struct net_bridge
*br
,
1500 struct net_bridge_port
*port
,
1504 struct br_ip br_group
;
1505 struct bridge_mcast_own_query
*own_query
;
1507 if (ipv4_is_local_multicast(group
))
1510 own_query
= port
? &port
->ip4_own_query
: &br
->ip4_own_query
;
1512 br_group
.u
.ip4
= group
;
1513 br_group
.proto
= htons(ETH_P_IP
);
1516 br_multicast_leave_group(br
, port
, &br_group
, &br
->ip4_other_query
,
1520 #if IS_ENABLED(CONFIG_IPV6)
1521 static void br_ip6_multicast_leave_group(struct net_bridge
*br
,
1522 struct net_bridge_port
*port
,
1523 const struct in6_addr
*group
,
1526 struct br_ip br_group
;
1527 struct bridge_mcast_own_query
*own_query
;
1529 if (ipv6_addr_is_ll_all_nodes(group
))
1532 own_query
= port
? &port
->ip6_own_query
: &br
->ip6_own_query
;
1534 br_group
.u
.ip6
= *group
;
1535 br_group
.proto
= htons(ETH_P_IPV6
);
1538 br_multicast_leave_group(br
, port
, &br_group
, &br
->ip6_other_query
,
1543 static int br_multicast_ipv4_rcv(struct net_bridge
*br
,
1544 struct net_bridge_port
*port
,
1545 struct sk_buff
*skb
,
1548 struct sk_buff
*skb_trimmed
= NULL
;
1552 err
= ip_mc_check_igmp(skb
, &skb_trimmed
);
1554 if (err
== -ENOMSG
) {
1555 if (!ipv4_is_local_multicast(ip_hdr(skb
)->daddr
))
1556 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1558 } else if (err
< 0) {
1562 BR_INPUT_SKB_CB(skb
)->igmp
= 1;
1566 case IGMP_HOST_MEMBERSHIP_REPORT
:
1567 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1568 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1569 err
= br_ip4_multicast_add_group(br
, port
, ih
->group
, vid
);
1571 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1572 err
= br_ip4_multicast_igmp3_report(br
, port
, skb_trimmed
, vid
);
1574 case IGMP_HOST_MEMBERSHIP_QUERY
:
1575 err
= br_ip4_multicast_query(br
, port
, skb_trimmed
, vid
);
1577 case IGMP_HOST_LEAVE_MESSAGE
:
1578 br_ip4_multicast_leave_group(br
, port
, ih
->group
, vid
);
1583 kfree_skb(skb_trimmed
);
1588 #if IS_ENABLED(CONFIG_IPV6)
1589 static int br_multicast_ipv6_rcv(struct net_bridge
*br
,
1590 struct net_bridge_port
*port
,
1591 struct sk_buff
*skb
,
1594 struct sk_buff
*skb_trimmed
= NULL
;
1595 struct mld_msg
*mld
;
1598 err
= ipv6_mc_check_mld(skb
, &skb_trimmed
);
1600 if (err
== -ENOMSG
) {
1601 if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb
)->daddr
))
1602 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1604 } else if (err
< 0) {
1608 BR_INPUT_SKB_CB(skb
)->igmp
= 1;
1609 mld
= (struct mld_msg
*)skb_transport_header(skb
);
1611 switch (mld
->mld_type
) {
1612 case ICMPV6_MGM_REPORT
:
1613 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1614 err
= br_ip6_multicast_add_group(br
, port
, &mld
->mld_mca
, vid
);
1616 case ICMPV6_MLD2_REPORT
:
1617 err
= br_ip6_multicast_mld2_report(br
, port
, skb_trimmed
, vid
);
1619 case ICMPV6_MGM_QUERY
:
1620 err
= br_ip6_multicast_query(br
, port
, skb_trimmed
, vid
);
1622 case ICMPV6_MGM_REDUCTION
:
1623 br_ip6_multicast_leave_group(br
, port
, &mld
->mld_mca
, vid
);
1628 kfree_skb(skb_trimmed
);
1634 int br_multicast_rcv(struct net_bridge
*br
, struct net_bridge_port
*port
,
1635 struct sk_buff
*skb
, u16 vid
)
1637 BR_INPUT_SKB_CB(skb
)->igmp
= 0;
1638 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 0;
1640 if (br
->multicast_disabled
)
1643 switch (skb
->protocol
) {
1644 case htons(ETH_P_IP
):
1645 return br_multicast_ipv4_rcv(br
, port
, skb
, vid
);
1646 #if IS_ENABLED(CONFIG_IPV6)
1647 case htons(ETH_P_IPV6
):
1648 return br_multicast_ipv6_rcv(br
, port
, skb
, vid
);
1655 static void br_multicast_query_expired(struct net_bridge
*br
,
1656 struct bridge_mcast_own_query
*query
,
1657 struct bridge_mcast_querier
*querier
)
1659 spin_lock(&br
->multicast_lock
);
1660 if (query
->startup_sent
< br
->multicast_startup_query_count
)
1661 query
->startup_sent
++;
1663 RCU_INIT_POINTER(querier
->port
, NULL
);
1664 br_multicast_send_query(br
, NULL
, query
);
1665 spin_unlock(&br
->multicast_lock
);
1668 static void br_ip4_multicast_query_expired(unsigned long data
)
1670 struct net_bridge
*br
= (void *)data
;
1672 br_multicast_query_expired(br
, &br
->ip4_own_query
, &br
->ip4_querier
);
1675 #if IS_ENABLED(CONFIG_IPV6)
1676 static void br_ip6_multicast_query_expired(unsigned long data
)
1678 struct net_bridge
*br
= (void *)data
;
1680 br_multicast_query_expired(br
, &br
->ip6_own_query
, &br
->ip6_querier
);
1684 void br_multicast_init(struct net_bridge
*br
)
1686 br
->hash_elasticity
= 4;
1689 br
->multicast_router
= 1;
1690 br
->multicast_querier
= 0;
1691 br
->multicast_query_use_ifaddr
= 0;
1692 br
->multicast_last_member_count
= 2;
1693 br
->multicast_startup_query_count
= 2;
1695 br
->multicast_last_member_interval
= HZ
;
1696 br
->multicast_query_response_interval
= 10 * HZ
;
1697 br
->multicast_startup_query_interval
= 125 * HZ
/ 4;
1698 br
->multicast_query_interval
= 125 * HZ
;
1699 br
->multicast_querier_interval
= 255 * HZ
;
1700 br
->multicast_membership_interval
= 260 * HZ
;
1702 br
->ip4_other_query
.delay_time
= 0;
1703 br
->ip4_querier
.port
= NULL
;
1704 #if IS_ENABLED(CONFIG_IPV6)
1705 br
->ip6_other_query
.delay_time
= 0;
1706 br
->ip6_querier
.port
= NULL
;
1709 spin_lock_init(&br
->multicast_lock
);
1710 setup_timer(&br
->multicast_router_timer
,
1711 br_multicast_local_router_expired
, 0);
1712 setup_timer(&br
->ip4_other_query
.timer
,
1713 br_ip4_multicast_querier_expired
, (unsigned long)br
);
1714 setup_timer(&br
->ip4_own_query
.timer
, br_ip4_multicast_query_expired
,
1716 #if IS_ENABLED(CONFIG_IPV6)
1717 setup_timer(&br
->ip6_other_query
.timer
,
1718 br_ip6_multicast_querier_expired
, (unsigned long)br
);
1719 setup_timer(&br
->ip6_own_query
.timer
, br_ip6_multicast_query_expired
,
1724 static void __br_multicast_open(struct net_bridge
*br
,
1725 struct bridge_mcast_own_query
*query
)
1727 query
->startup_sent
= 0;
1729 if (br
->multicast_disabled
)
1732 mod_timer(&query
->timer
, jiffies
);
1735 void br_multicast_open(struct net_bridge
*br
)
1737 __br_multicast_open(br
, &br
->ip4_own_query
);
1738 #if IS_ENABLED(CONFIG_IPV6)
1739 __br_multicast_open(br
, &br
->ip6_own_query
);
1743 void br_multicast_stop(struct net_bridge
*br
)
1745 del_timer_sync(&br
->multicast_router_timer
);
1746 del_timer_sync(&br
->ip4_other_query
.timer
);
1747 del_timer_sync(&br
->ip4_own_query
.timer
);
1748 #if IS_ENABLED(CONFIG_IPV6)
1749 del_timer_sync(&br
->ip6_other_query
.timer
);
1750 del_timer_sync(&br
->ip6_own_query
.timer
);
1754 void br_multicast_dev_del(struct net_bridge
*br
)
1756 struct net_bridge_mdb_htable
*mdb
;
1757 struct net_bridge_mdb_entry
*mp
;
1758 struct hlist_node
*n
;
1762 spin_lock_bh(&br
->multicast_lock
);
1763 mdb
= mlock_dereference(br
->mdb
, br
);
1770 for (i
= 0; i
< mdb
->max
; i
++) {
1771 hlist_for_each_entry_safe(mp
, n
, &mdb
->mhash
[i
],
1773 del_timer(&mp
->timer
);
1774 call_rcu_bh(&mp
->rcu
, br_multicast_free_group
);
1779 spin_unlock_bh(&br
->multicast_lock
);
1781 spin_lock_bh(&br
->multicast_lock
);
1786 call_rcu_bh(&mdb
->rcu
, br_mdb_free
);
1789 spin_unlock_bh(&br
->multicast_lock
);
1792 int br_multicast_set_router(struct net_bridge
*br
, unsigned long val
)
1796 spin_lock_bh(&br
->multicast_lock
);
1801 del_timer(&br
->multicast_router_timer
);
1804 br
->multicast_router
= val
;
1809 spin_unlock_bh(&br
->multicast_lock
);
1814 int br_multicast_set_port_router(struct net_bridge_port
*p
, unsigned long val
)
1816 struct net_bridge
*br
= p
->br
;
1819 spin_lock(&br
->multicast_lock
);
1825 p
->multicast_router
= val
;
1828 if (val
< 2 && !hlist_unhashed(&p
->rlist
))
1829 hlist_del_init_rcu(&p
->rlist
);
1834 del_timer(&p
->multicast_router_timer
);
1839 br_multicast_add_router(br
, p
);
1843 spin_unlock(&br
->multicast_lock
);
1848 static void br_multicast_start_querier(struct net_bridge
*br
,
1849 struct bridge_mcast_own_query
*query
)
1851 struct net_bridge_port
*port
;
1853 __br_multicast_open(br
, query
);
1855 list_for_each_entry(port
, &br
->port_list
, list
) {
1856 if (port
->state
== BR_STATE_DISABLED
||
1857 port
->state
== BR_STATE_BLOCKING
)
1860 if (query
== &br
->ip4_own_query
)
1861 br_multicast_enable(&port
->ip4_own_query
);
1862 #if IS_ENABLED(CONFIG_IPV6)
1864 br_multicast_enable(&port
->ip6_own_query
);
1869 int br_multicast_toggle(struct net_bridge
*br
, unsigned long val
)
1872 struct net_bridge_mdb_htable
*mdb
;
1874 spin_lock_bh(&br
->multicast_lock
);
1875 if (br
->multicast_disabled
== !val
)
1878 br
->multicast_disabled
= !val
;
1879 if (br
->multicast_disabled
)
1882 if (!netif_running(br
->dev
))
1885 mdb
= mlock_dereference(br
->mdb
, br
);
1890 br
->multicast_disabled
= !!val
;
1894 err
= br_mdb_rehash(&br
->mdb
, mdb
->max
,
1895 br
->hash_elasticity
);
1900 br_multicast_start_querier(br
, &br
->ip4_own_query
);
1901 #if IS_ENABLED(CONFIG_IPV6)
1902 br_multicast_start_querier(br
, &br
->ip6_own_query
);
1906 spin_unlock_bh(&br
->multicast_lock
);
1911 int br_multicast_set_querier(struct net_bridge
*br
, unsigned long val
)
1913 unsigned long max_delay
;
1917 spin_lock_bh(&br
->multicast_lock
);
1918 if (br
->multicast_querier
== val
)
1921 br
->multicast_querier
= val
;
1925 max_delay
= br
->multicast_query_response_interval
;
1927 if (!timer_pending(&br
->ip4_other_query
.timer
))
1928 br
->ip4_other_query
.delay_time
= jiffies
+ max_delay
;
1930 br_multicast_start_querier(br
, &br
->ip4_own_query
);
1932 #if IS_ENABLED(CONFIG_IPV6)
1933 if (!timer_pending(&br
->ip6_other_query
.timer
))
1934 br
->ip6_other_query
.delay_time
= jiffies
+ max_delay
;
1936 br_multicast_start_querier(br
, &br
->ip6_own_query
);
1940 spin_unlock_bh(&br
->multicast_lock
);
1945 int br_multicast_set_hash_max(struct net_bridge
*br
, unsigned long val
)
1949 struct net_bridge_mdb_htable
*mdb
;
1951 spin_lock_bh(&br
->multicast_lock
);
1952 if (!is_power_of_2(val
))
1955 mdb
= mlock_dereference(br
->mdb
, br
);
1956 if (mdb
&& val
< mdb
->size
)
1972 err
= br_mdb_rehash(&br
->mdb
, br
->hash_max
,
1973 br
->hash_elasticity
);
1979 spin_unlock_bh(&br
->multicast_lock
);
1985 * br_multicast_list_adjacent - Returns snooped multicast addresses
1986 * @dev: The bridge port adjacent to which to retrieve addresses
1987 * @br_ip_list: The list to store found, snooped multicast IP addresses in
1989 * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
1990 * snooping feature on all bridge ports of dev's bridge device, excluding
1991 * the addresses from dev itself.
1993 * Returns the number of items added to br_ip_list.
1996 * - br_ip_list needs to be initialized by caller
1997 * - br_ip_list might contain duplicates in the end
1998 * (needs to be taken care of by caller)
1999 * - br_ip_list needs to be freed by caller
2001 int br_multicast_list_adjacent(struct net_device
*dev
,
2002 struct list_head
*br_ip_list
)
2004 struct net_bridge
*br
;
2005 struct net_bridge_port
*port
;
2006 struct net_bridge_port_group
*group
;
2007 struct br_ip_list
*entry
;
2011 if (!br_ip_list
|| !br_port_exists(dev
))
2014 port
= br_port_get_rcu(dev
);
2015 if (!port
|| !port
->br
)
2020 list_for_each_entry_rcu(port
, &br
->port_list
, list
) {
2021 if (!port
->dev
|| port
->dev
== dev
)
2024 hlist_for_each_entry_rcu(group
, &port
->mglist
, mglist
) {
2025 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
2029 entry
->addr
= group
->addr
;
2030 list_add(&entry
->list
, br_ip_list
);
2039 EXPORT_SYMBOL_GPL(br_multicast_list_adjacent
);
2042 * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2043 * @dev: The bridge port providing the bridge on which to check for a querier
2044 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2046 * Checks whether the given interface has a bridge on top and if so returns
2047 * true if a valid querier exists anywhere on the bridged link layer.
2048 * Otherwise returns false.
2050 bool br_multicast_has_querier_anywhere(struct net_device
*dev
, int proto
)
2052 struct net_bridge
*br
;
2053 struct net_bridge_port
*port
;
2058 if (!br_port_exists(dev
))
2061 port
= br_port_get_rcu(dev
);
2062 if (!port
|| !port
->br
)
2067 memset(ð
, 0, sizeof(eth
));
2068 eth
.h_proto
= htons(proto
);
2070 ret
= br_multicast_querier_exists(br
, ð
);
2076 EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere
);
2079 * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
2080 * @dev: The bridge port adjacent to which to check for a querier
2081 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2083 * Checks whether the given interface has a bridge on top and if so returns
2084 * true if a selected querier is behind one of the other ports of this
2085 * bridge. Otherwise returns false.
2087 bool br_multicast_has_querier_adjacent(struct net_device
*dev
, int proto
)
2089 struct net_bridge
*br
;
2090 struct net_bridge_port
*port
;
2094 if (!br_port_exists(dev
))
2097 port
= br_port_get_rcu(dev
);
2098 if (!port
|| !port
->br
)
2105 if (!timer_pending(&br
->ip4_other_query
.timer
) ||
2106 rcu_dereference(br
->ip4_querier
.port
) == port
)
2109 #if IS_ENABLED(CONFIG_IPV6)
2111 if (!timer_pending(&br
->ip6_other_query
.timer
) ||
2112 rcu_dereference(br
->ip6_querier
.port
) == port
)
2125 EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent
);