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/if_ether.h>
15 #include <linux/igmp.h>
16 #include <linux/jhash.h>
17 #include <linux/kernel.h>
18 #include <linux/log2.h>
19 #include <linux/netdevice.h>
20 #include <linux/netfilter_bridge.h>
21 #include <linux/random.h>
22 #include <linux/rculist.h>
23 #include <linux/skbuff.h>
24 #include <linux/slab.h>
25 #include <linux/timer.h>
26 #include <linux/inetdevice.h>
28 #if IS_ENABLED(CONFIG_IPV6)
31 #include <net/ip6_checksum.h>
32 #include <net/addrconf.h>
35 #include "br_private.h"
37 static void br_multicast_start_querier(struct net_bridge
*br
,
38 struct bridge_mcast_query
*query
);
39 unsigned int br_mdb_rehash_seq
;
41 static inline int br_ip_equal(const struct br_ip
*a
, const struct br_ip
*b
)
43 if (a
->proto
!= b
->proto
)
49 return a
->u
.ip4
== b
->u
.ip4
;
50 #if IS_ENABLED(CONFIG_IPV6)
51 case htons(ETH_P_IPV6
):
52 return ipv6_addr_equal(&a
->u
.ip6
, &b
->u
.ip6
);
58 static inline int __br_ip4_hash(struct net_bridge_mdb_htable
*mdb
, __be32 ip
,
61 return jhash_2words((__force u32
)ip
, vid
, mdb
->secret
) & (mdb
->max
- 1);
64 #if IS_ENABLED(CONFIG_IPV6)
65 static inline int __br_ip6_hash(struct net_bridge_mdb_htable
*mdb
,
66 const struct in6_addr
*ip
,
69 return jhash_2words(ipv6_addr_hash(ip
), vid
,
70 mdb
->secret
) & (mdb
->max
- 1);
74 static inline int br_ip_hash(struct net_bridge_mdb_htable
*mdb
,
79 return __br_ip4_hash(mdb
, ip
->u
.ip4
, ip
->vid
);
80 #if IS_ENABLED(CONFIG_IPV6)
81 case htons(ETH_P_IPV6
):
82 return __br_ip6_hash(mdb
, &ip
->u
.ip6
, ip
->vid
);
88 static struct net_bridge_mdb_entry
*__br_mdb_ip_get(
89 struct net_bridge_mdb_htable
*mdb
, struct br_ip
*dst
, int hash
)
91 struct net_bridge_mdb_entry
*mp
;
93 hlist_for_each_entry_rcu(mp
, &mdb
->mhash
[hash
], hlist
[mdb
->ver
]) {
94 if (br_ip_equal(&mp
->addr
, dst
))
101 struct net_bridge_mdb_entry
*br_mdb_ip_get(struct net_bridge_mdb_htable
*mdb
,
107 return __br_mdb_ip_get(mdb
, dst
, br_ip_hash(mdb
, dst
));
110 static struct net_bridge_mdb_entry
*br_mdb_ip4_get(
111 struct net_bridge_mdb_htable
*mdb
, __be32 dst
, __u16 vid
)
116 br_dst
.proto
= htons(ETH_P_IP
);
119 return br_mdb_ip_get(mdb
, &br_dst
);
122 #if IS_ENABLED(CONFIG_IPV6)
123 static struct net_bridge_mdb_entry
*br_mdb_ip6_get(
124 struct net_bridge_mdb_htable
*mdb
, const struct in6_addr
*dst
,
130 br_dst
.proto
= htons(ETH_P_IPV6
);
133 return br_mdb_ip_get(mdb
, &br_dst
);
137 struct net_bridge_mdb_entry
*br_mdb_get(struct net_bridge
*br
,
138 struct sk_buff
*skb
, u16 vid
)
140 struct net_bridge_mdb_htable
*mdb
= rcu_dereference(br
->mdb
);
143 if (br
->multicast_disabled
)
146 if (BR_INPUT_SKB_CB(skb
)->igmp
)
149 ip
.proto
= skb
->protocol
;
152 switch (skb
->protocol
) {
153 case htons(ETH_P_IP
):
154 ip
.u
.ip4
= ip_hdr(skb
)->daddr
;
156 #if IS_ENABLED(CONFIG_IPV6)
157 case htons(ETH_P_IPV6
):
158 ip
.u
.ip6
= ipv6_hdr(skb
)->daddr
;
165 return br_mdb_ip_get(mdb
, &ip
);
168 static void br_mdb_free(struct rcu_head
*head
)
170 struct net_bridge_mdb_htable
*mdb
=
171 container_of(head
, struct net_bridge_mdb_htable
, rcu
);
172 struct net_bridge_mdb_htable
*old
= mdb
->old
;
179 static int br_mdb_copy(struct net_bridge_mdb_htable
*new,
180 struct net_bridge_mdb_htable
*old
,
183 struct net_bridge_mdb_entry
*mp
;
188 for (i
= 0; i
< old
->max
; i
++)
189 hlist_for_each_entry(mp
, &old
->mhash
[i
], hlist
[old
->ver
])
190 hlist_add_head(&mp
->hlist
[new->ver
],
191 &new->mhash
[br_ip_hash(new, &mp
->addr
)]);
197 for (i
= 0; i
< new->max
; i
++) {
199 hlist_for_each_entry(mp
, &new->mhash
[i
], hlist
[new->ver
])
205 return maxlen
> elasticity
? -EINVAL
: 0;
208 void br_multicast_free_pg(struct rcu_head
*head
)
210 struct net_bridge_port_group
*p
=
211 container_of(head
, struct net_bridge_port_group
, rcu
);
216 static void br_multicast_free_group(struct rcu_head
*head
)
218 struct net_bridge_mdb_entry
*mp
=
219 container_of(head
, struct net_bridge_mdb_entry
, rcu
);
224 static void br_multicast_group_expired(unsigned long data
)
226 struct net_bridge_mdb_entry
*mp
= (void *)data
;
227 struct net_bridge
*br
= mp
->br
;
228 struct net_bridge_mdb_htable
*mdb
;
230 spin_lock(&br
->multicast_lock
);
231 if (!netif_running(br
->dev
) || timer_pending(&mp
->timer
))
239 mdb
= mlock_dereference(br
->mdb
, br
);
241 hlist_del_rcu(&mp
->hlist
[mdb
->ver
]);
244 call_rcu_bh(&mp
->rcu
, br_multicast_free_group
);
247 spin_unlock(&br
->multicast_lock
);
250 static void br_multicast_del_pg(struct net_bridge
*br
,
251 struct net_bridge_port_group
*pg
)
253 struct net_bridge_mdb_htable
*mdb
;
254 struct net_bridge_mdb_entry
*mp
;
255 struct net_bridge_port_group
*p
;
256 struct net_bridge_port_group __rcu
**pp
;
258 mdb
= mlock_dereference(br
->mdb
, br
);
260 mp
= br_mdb_ip_get(mdb
, &pg
->addr
);
264 for (pp
= &mp
->ports
;
265 (p
= mlock_dereference(*pp
, br
)) != NULL
;
270 rcu_assign_pointer(*pp
, p
->next
);
271 hlist_del_init(&p
->mglist
);
272 del_timer(&p
->timer
);
273 call_rcu_bh(&p
->rcu
, br_multicast_free_pg
);
275 if (!mp
->ports
&& !mp
->mglist
&&
276 netif_running(br
->dev
))
277 mod_timer(&mp
->timer
, jiffies
);
285 static void br_multicast_port_group_expired(unsigned long data
)
287 struct net_bridge_port_group
*pg
= (void *)data
;
288 struct net_bridge
*br
= pg
->port
->br
;
290 spin_lock(&br
->multicast_lock
);
291 if (!netif_running(br
->dev
) || timer_pending(&pg
->timer
) ||
292 hlist_unhashed(&pg
->mglist
) || pg
->state
& MDB_PERMANENT
)
295 br_multicast_del_pg(br
, pg
);
298 spin_unlock(&br
->multicast_lock
);
301 static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu
**mdbp
, int max
,
304 struct net_bridge_mdb_htable
*old
= rcu_dereference_protected(*mdbp
, 1);
305 struct net_bridge_mdb_htable
*mdb
;
308 mdb
= kmalloc(sizeof(*mdb
), GFP_ATOMIC
);
315 mdb
->mhash
= kzalloc(max
* sizeof(*mdb
->mhash
), GFP_ATOMIC
);
321 mdb
->size
= old
? old
->size
: 0;
322 mdb
->ver
= old
? old
->ver
^ 1 : 0;
324 if (!old
|| elasticity
)
325 get_random_bytes(&mdb
->secret
, sizeof(mdb
->secret
));
327 mdb
->secret
= old
->secret
;
332 err
= br_mdb_copy(mdb
, old
, elasticity
);
340 call_rcu_bh(&mdb
->rcu
, br_mdb_free
);
343 rcu_assign_pointer(*mdbp
, mdb
);
348 static struct sk_buff
*br_ip4_multicast_alloc_query(struct net_bridge
*br
,
356 skb
= netdev_alloc_skb_ip_align(br
->dev
, sizeof(*eth
) + sizeof(*iph
) +
361 skb
->protocol
= htons(ETH_P_IP
);
363 skb_reset_mac_header(skb
);
366 memcpy(eth
->h_source
, br
->dev
->dev_addr
, 6);
369 eth
->h_dest
[2] = 0x5e;
373 eth
->h_proto
= htons(ETH_P_IP
);
374 skb_put(skb
, sizeof(*eth
));
376 skb_set_network_header(skb
, skb
->len
);
382 iph
->tot_len
= htons(sizeof(*iph
) + sizeof(*ih
) + 4);
384 iph
->frag_off
= htons(IP_DF
);
386 iph
->protocol
= IPPROTO_IGMP
;
387 iph
->saddr
= br
->multicast_query_use_ifaddr
?
388 inet_select_addr(br
->dev
, 0, RT_SCOPE_LINK
) : 0;
389 iph
->daddr
= htonl(INADDR_ALLHOSTS_GROUP
);
390 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
391 ((u8
*)&iph
[1])[1] = 4;
392 ((u8
*)&iph
[1])[2] = 0;
393 ((u8
*)&iph
[1])[3] = 0;
397 skb_set_transport_header(skb
, skb
->len
);
399 ih
->type
= IGMP_HOST_MEMBERSHIP_QUERY
;
400 ih
->code
= (group
? br
->multicast_last_member_interval
:
401 br
->multicast_query_response_interval
) /
402 (HZ
/ IGMP_TIMER_SCALE
);
405 ih
->csum
= ip_compute_csum((void *)ih
, sizeof(struct igmphdr
));
406 skb_put(skb
, sizeof(*ih
));
408 __skb_pull(skb
, sizeof(*eth
));
414 #if IS_ENABLED(CONFIG_IPV6)
415 static struct sk_buff
*br_ip6_multicast_alloc_query(struct net_bridge
*br
,
416 const struct in6_addr
*group
)
419 struct ipv6hdr
*ip6h
;
420 struct mld_msg
*mldq
;
423 unsigned long interval
;
425 skb
= netdev_alloc_skb_ip_align(br
->dev
, sizeof(*eth
) + sizeof(*ip6h
) +
430 skb
->protocol
= htons(ETH_P_IPV6
);
432 /* Ethernet header */
433 skb_reset_mac_header(skb
);
436 memcpy(eth
->h_source
, br
->dev
->dev_addr
, 6);
437 eth
->h_proto
= htons(ETH_P_IPV6
);
438 skb_put(skb
, sizeof(*eth
));
440 /* IPv6 header + HbH option */
441 skb_set_network_header(skb
, skb
->len
);
442 ip6h
= ipv6_hdr(skb
);
444 *(__force __be32
*)ip6h
= htonl(0x60000000);
445 ip6h
->payload_len
= htons(8 + sizeof(*mldq
));
446 ip6h
->nexthdr
= IPPROTO_HOPOPTS
;
448 ipv6_addr_set(&ip6h
->daddr
, htonl(0xff020000), 0, 0, htonl(1));
449 if (ipv6_dev_get_saddr(dev_net(br
->dev
), br
->dev
, &ip6h
->daddr
, 0,
452 br
->has_ipv6_addr
= 0;
456 br
->has_ipv6_addr
= 1;
457 ipv6_eth_mc_map(&ip6h
->daddr
, eth
->h_dest
);
459 hopopt
= (u8
*)(ip6h
+ 1);
460 hopopt
[0] = IPPROTO_ICMPV6
; /* next hdr */
461 hopopt
[1] = 0; /* length of HbH */
462 hopopt
[2] = IPV6_TLV_ROUTERALERT
; /* Router Alert */
463 hopopt
[3] = 2; /* Length of RA Option */
464 hopopt
[4] = 0; /* Type = 0x0000 (MLD) */
466 hopopt
[6] = IPV6_TLV_PAD1
; /* Pad1 */
467 hopopt
[7] = IPV6_TLV_PAD1
; /* Pad1 */
469 skb_put(skb
, sizeof(*ip6h
) + 8);
472 skb_set_transport_header(skb
, skb
->len
);
473 mldq
= (struct mld_msg
*) icmp6_hdr(skb
);
475 interval
= ipv6_addr_any(group
) ?
476 br
->multicast_query_response_interval
:
477 br
->multicast_last_member_interval
;
479 mldq
->mld_type
= ICMPV6_MGM_QUERY
;
482 mldq
->mld_maxdelay
= htons((u16
)jiffies_to_msecs(interval
));
483 mldq
->mld_reserved
= 0;
484 mldq
->mld_mca
= *group
;
487 mldq
->mld_cksum
= csum_ipv6_magic(&ip6h
->saddr
, &ip6h
->daddr
,
488 sizeof(*mldq
), IPPROTO_ICMPV6
,
491 skb_put(skb
, sizeof(*mldq
));
493 __skb_pull(skb
, sizeof(*eth
));
500 static struct sk_buff
*br_multicast_alloc_query(struct net_bridge
*br
,
503 switch (addr
->proto
) {
504 case htons(ETH_P_IP
):
505 return br_ip4_multicast_alloc_query(br
, addr
->u
.ip4
);
506 #if IS_ENABLED(CONFIG_IPV6)
507 case htons(ETH_P_IPV6
):
508 return br_ip6_multicast_alloc_query(br
, &addr
->u
.ip6
);
514 static struct net_bridge_mdb_entry
*br_multicast_get_group(
515 struct net_bridge
*br
, struct net_bridge_port
*port
,
516 struct br_ip
*group
, int hash
)
518 struct net_bridge_mdb_htable
*mdb
;
519 struct net_bridge_mdb_entry
*mp
;
520 unsigned int count
= 0;
525 mdb
= rcu_dereference_protected(br
->mdb
, 1);
526 hlist_for_each_entry(mp
, &mdb
->mhash
[hash
], hlist
[mdb
->ver
]) {
528 if (unlikely(br_ip_equal(group
, &mp
->addr
)))
535 if (unlikely(count
> br
->hash_elasticity
&& count
)) {
537 br_info(br
, "Multicast hash table "
538 "chain limit reached: %s\n",
539 port
? port
->dev
->name
: br
->dev
->name
);
541 elasticity
= br
->hash_elasticity
;
544 if (mdb
->size
>= max
) {
546 if (unlikely(max
> br
->hash_max
)) {
547 br_warn(br
, "Multicast hash table maximum of %d "
548 "reached, disabling snooping: %s\n",
550 port
? port
->dev
->name
: br
->dev
->name
);
553 br
->multicast_disabled
= 1;
558 if (max
> mdb
->max
|| elasticity
) {
561 br_info(br
, "Multicast hash table "
563 port
? port
->dev
->name
: br
->dev
->name
);
568 err
= br_mdb_rehash(&br
->mdb
, max
, elasticity
);
570 br_warn(br
, "Cannot rehash multicast "
571 "hash table, disabling snooping: %s, %d, %d\n",
572 port
? port
->dev
->name
: br
->dev
->name
,
588 struct net_bridge_mdb_entry
*br_multicast_new_group(struct net_bridge
*br
,
589 struct net_bridge_port
*port
, struct br_ip
*group
)
591 struct net_bridge_mdb_htable
*mdb
;
592 struct net_bridge_mdb_entry
*mp
;
596 mdb
= rcu_dereference_protected(br
->mdb
, 1);
598 err
= br_mdb_rehash(&br
->mdb
, BR_HASH_SIZE
, 0);
604 hash
= br_ip_hash(mdb
, group
);
605 mp
= br_multicast_get_group(br
, port
, group
, hash
);
606 switch (PTR_ERR(mp
)) {
612 mdb
= rcu_dereference_protected(br
->mdb
, 1);
613 hash
= br_ip_hash(mdb
, group
);
620 mp
= kzalloc(sizeof(*mp
), GFP_ATOMIC
);
622 return ERR_PTR(-ENOMEM
);
626 setup_timer(&mp
->timer
, br_multicast_group_expired
,
629 hlist_add_head_rcu(&mp
->hlist
[mdb
->ver
], &mdb
->mhash
[hash
]);
636 struct net_bridge_port_group
*br_multicast_new_port_group(
637 struct net_bridge_port
*port
,
639 struct net_bridge_port_group __rcu
*next
,
642 struct net_bridge_port_group
*p
;
644 p
= kzalloc(sizeof(*p
), GFP_ATOMIC
);
651 rcu_assign_pointer(p
->next
, next
);
652 hlist_add_head(&p
->mglist
, &port
->mglist
);
653 setup_timer(&p
->timer
, br_multicast_port_group_expired
,
658 static int br_multicast_add_group(struct net_bridge
*br
,
659 struct net_bridge_port
*port
,
662 struct net_bridge_mdb_entry
*mp
;
663 struct net_bridge_port_group
*p
;
664 struct net_bridge_port_group __rcu
**pp
;
665 unsigned long now
= jiffies
;
668 spin_lock(&br
->multicast_lock
);
669 if (!netif_running(br
->dev
) ||
670 (port
&& port
->state
== BR_STATE_DISABLED
))
673 mp
= br_multicast_new_group(br
, port
, group
);
680 mod_timer(&mp
->timer
, now
+ br
->multicast_membership_interval
);
684 for (pp
= &mp
->ports
;
685 (p
= mlock_dereference(*pp
, br
)) != NULL
;
689 if ((unsigned long)p
->port
< (unsigned long)port
)
693 p
= br_multicast_new_port_group(port
, group
, *pp
, MDB_TEMPORARY
);
696 rcu_assign_pointer(*pp
, p
);
697 br_mdb_notify(br
->dev
, port
, group
, RTM_NEWMDB
);
700 mod_timer(&p
->timer
, now
+ br
->multicast_membership_interval
);
705 spin_unlock(&br
->multicast_lock
);
709 static int br_ip4_multicast_add_group(struct net_bridge
*br
,
710 struct net_bridge_port
*port
,
714 struct br_ip br_group
;
716 if (ipv4_is_local_multicast(group
))
719 br_group
.u
.ip4
= group
;
720 br_group
.proto
= htons(ETH_P_IP
);
723 return br_multicast_add_group(br
, port
, &br_group
);
726 #if IS_ENABLED(CONFIG_IPV6)
727 static int br_ip6_multicast_add_group(struct net_bridge
*br
,
728 struct net_bridge_port
*port
,
729 const struct in6_addr
*group
,
732 struct br_ip br_group
;
734 if (ipv6_addr_is_ll_all_nodes(group
))
737 br_group
.u
.ip6
= *group
;
738 br_group
.proto
= htons(ETH_P_IPV6
);
741 return br_multicast_add_group(br
, port
, &br_group
);
745 static void br_multicast_router_expired(unsigned long data
)
747 struct net_bridge_port
*port
= (void *)data
;
748 struct net_bridge
*br
= port
->br
;
750 spin_lock(&br
->multicast_lock
);
751 if (port
->multicast_router
!= 1 ||
752 timer_pending(&port
->multicast_router_timer
) ||
753 hlist_unhashed(&port
->rlist
))
756 hlist_del_init_rcu(&port
->rlist
);
759 spin_unlock(&br
->multicast_lock
);
762 static void br_multicast_local_router_expired(unsigned long data
)
766 static void br_multicast_querier_expired(struct net_bridge
*br
,
767 struct bridge_mcast_query
*query
)
769 spin_lock(&br
->multicast_lock
);
770 if (!netif_running(br
->dev
) || br
->multicast_disabled
)
773 br_multicast_start_querier(br
, query
);
776 spin_unlock(&br
->multicast_lock
);
779 static void br_ip4_multicast_querier_expired(unsigned long data
)
781 struct net_bridge
*br
= (void *)data
;
783 br_multicast_querier_expired(br
, &br
->ip4_query
);
786 #if IS_ENABLED(CONFIG_IPV6)
787 static void br_ip6_multicast_querier_expired(unsigned long data
)
789 struct net_bridge
*br
= (void *)data
;
791 br_multicast_querier_expired(br
, &br
->ip6_query
);
795 static void __br_multicast_send_query(struct net_bridge
*br
,
796 struct net_bridge_port
*port
,
801 skb
= br_multicast_alloc_query(br
, ip
);
806 __skb_push(skb
, sizeof(struct ethhdr
));
807 skb
->dev
= port
->dev
;
808 NF_HOOK(NFPROTO_BRIDGE
, NF_BR_LOCAL_OUT
, skb
, NULL
, skb
->dev
,
814 static void br_multicast_send_query(struct net_bridge
*br
,
815 struct net_bridge_port
*port
,
816 struct bridge_mcast_query
*query
)
819 struct br_ip br_group
;
820 struct bridge_mcast_querier
*querier
= NULL
;
822 if (!netif_running(br
->dev
) || br
->multicast_disabled
||
823 !br
->multicast_querier
)
826 memset(&br_group
.u
, 0, sizeof(br_group
.u
));
828 if (port
? (query
== &port
->ip4_query
) :
829 (query
== &br
->ip4_query
)) {
830 querier
= &br
->ip4_querier
;
831 br_group
.proto
= htons(ETH_P_IP
);
832 #if IS_ENABLED(CONFIG_IPV6)
834 querier
= &br
->ip6_querier
;
835 br_group
.proto
= htons(ETH_P_IPV6
);
839 if (!querier
|| timer_pending(&querier
->timer
))
842 __br_multicast_send_query(br
, port
, &br_group
);
845 time
+= query
->startup_sent
< br
->multicast_startup_query_count
?
846 br
->multicast_startup_query_interval
:
847 br
->multicast_query_interval
;
848 mod_timer(&query
->timer
, time
);
851 static void br_multicast_port_query_expired(struct net_bridge_port
*port
,
852 struct bridge_mcast_query
*query
)
854 struct net_bridge
*br
= port
->br
;
856 spin_lock(&br
->multicast_lock
);
857 if (port
->state
== BR_STATE_DISABLED
||
858 port
->state
== BR_STATE_BLOCKING
)
861 if (query
->startup_sent
< br
->multicast_startup_query_count
)
862 query
->startup_sent
++;
864 br_multicast_send_query(port
->br
, port
, query
);
867 spin_unlock(&br
->multicast_lock
);
870 static void br_ip4_multicast_port_query_expired(unsigned long data
)
872 struct net_bridge_port
*port
= (void *)data
;
874 br_multicast_port_query_expired(port
, &port
->ip4_query
);
877 #if IS_ENABLED(CONFIG_IPV6)
878 static void br_ip6_multicast_port_query_expired(unsigned long data
)
880 struct net_bridge_port
*port
= (void *)data
;
882 br_multicast_port_query_expired(port
, &port
->ip6_query
);
886 void br_multicast_add_port(struct net_bridge_port
*port
)
888 port
->multicast_router
= 1;
890 setup_timer(&port
->multicast_router_timer
, br_multicast_router_expired
,
891 (unsigned long)port
);
892 setup_timer(&port
->ip4_query
.timer
, br_ip4_multicast_port_query_expired
,
893 (unsigned long)port
);
894 #if IS_ENABLED(CONFIG_IPV6)
895 setup_timer(&port
->ip6_query
.timer
, br_ip6_multicast_port_query_expired
,
896 (unsigned long)port
);
900 void br_multicast_del_port(struct net_bridge_port
*port
)
902 del_timer_sync(&port
->multicast_router_timer
);
905 static void br_multicast_enable(struct bridge_mcast_query
*query
)
907 query
->startup_sent
= 0;
909 if (try_to_del_timer_sync(&query
->timer
) >= 0 ||
910 del_timer(&query
->timer
))
911 mod_timer(&query
->timer
, jiffies
);
914 void br_multicast_enable_port(struct net_bridge_port
*port
)
916 struct net_bridge
*br
= port
->br
;
918 spin_lock(&br
->multicast_lock
);
919 if (br
->multicast_disabled
|| !netif_running(br
->dev
))
922 br_multicast_enable(&port
->ip4_query
);
923 #if IS_ENABLED(CONFIG_IPV6)
924 br_multicast_enable(&port
->ip6_query
);
928 spin_unlock(&br
->multicast_lock
);
931 void br_multicast_disable_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 spin_lock(&br
->multicast_lock
);
938 hlist_for_each_entry_safe(pg
, n
, &port
->mglist
, mglist
)
939 br_multicast_del_pg(br
, pg
);
941 if (!hlist_unhashed(&port
->rlist
))
942 hlist_del_init_rcu(&port
->rlist
);
943 del_timer(&port
->multicast_router_timer
);
944 del_timer(&port
->ip4_query
.timer
);
945 #if IS_ENABLED(CONFIG_IPV6)
946 del_timer(&port
->ip6_query
.timer
);
948 spin_unlock(&br
->multicast_lock
);
951 static int br_ip4_multicast_igmp3_report(struct net_bridge
*br
,
952 struct net_bridge_port
*port
,
955 struct igmpv3_report
*ih
;
956 struct igmpv3_grec
*grec
;
965 if (!pskb_may_pull(skb
, sizeof(*ih
)))
968 br_vlan_get_tag(skb
, &vid
);
969 ih
= igmpv3_report_hdr(skb
);
970 num
= ntohs(ih
->ngrec
);
973 for (i
= 0; i
< num
; i
++) {
974 len
+= sizeof(*grec
);
975 if (!pskb_may_pull(skb
, len
))
978 grec
= (void *)(skb
->data
+ len
- sizeof(*grec
));
979 group
= grec
->grec_mca
;
980 type
= grec
->grec_type
;
982 len
+= ntohs(grec
->grec_nsrcs
) * 4;
983 if (!pskb_may_pull(skb
, len
))
986 /* We treat this as an IGMPv2 report for now. */
988 case IGMPV3_MODE_IS_INCLUDE
:
989 case IGMPV3_MODE_IS_EXCLUDE
:
990 case IGMPV3_CHANGE_TO_INCLUDE
:
991 case IGMPV3_CHANGE_TO_EXCLUDE
:
992 case IGMPV3_ALLOW_NEW_SOURCES
:
993 case IGMPV3_BLOCK_OLD_SOURCES
:
1000 err
= br_ip4_multicast_add_group(br
, port
, group
, vid
);
1008 #if IS_ENABLED(CONFIG_IPV6)
1009 static int br_ip6_multicast_mld2_report(struct net_bridge
*br
,
1010 struct net_bridge_port
*port
,
1011 struct sk_buff
*skb
)
1013 struct icmp6hdr
*icmp6h
;
1014 struct mld2_grec
*grec
;
1021 if (!pskb_may_pull(skb
, sizeof(*icmp6h
)))
1024 br_vlan_get_tag(skb
, &vid
);
1025 icmp6h
= icmp6_hdr(skb
);
1026 num
= ntohs(icmp6h
->icmp6_dataun
.un_data16
[1]);
1027 len
= sizeof(*icmp6h
);
1029 for (i
= 0; i
< num
; i
++) {
1030 __be16
*nsrcs
, _nsrcs
;
1032 nsrcs
= skb_header_pointer(skb
,
1033 len
+ offsetof(struct mld2_grec
,
1035 sizeof(_nsrcs
), &_nsrcs
);
1039 if (!pskb_may_pull(skb
,
1040 len
+ sizeof(*grec
) +
1041 sizeof(struct in6_addr
) * ntohs(*nsrcs
)))
1044 grec
= (struct mld2_grec
*)(skb
->data
+ len
);
1045 len
+= sizeof(*grec
) +
1046 sizeof(struct in6_addr
) * ntohs(*nsrcs
);
1048 /* We treat these as MLDv1 reports for now. */
1049 switch (grec
->grec_type
) {
1050 case MLD2_MODE_IS_INCLUDE
:
1051 case MLD2_MODE_IS_EXCLUDE
:
1052 case MLD2_CHANGE_TO_INCLUDE
:
1053 case MLD2_CHANGE_TO_EXCLUDE
:
1054 case MLD2_ALLOW_NEW_SOURCES
:
1055 case MLD2_BLOCK_OLD_SOURCES
:
1062 err
= br_ip6_multicast_add_group(br
, port
, &grec
->grec_mca
,
1073 br_multicast_update_querier_timer(struct net_bridge
*br
,
1074 struct bridge_mcast_querier
*querier
,
1075 unsigned long max_delay
)
1077 if (!timer_pending(&querier
->timer
))
1078 querier
->delay_time
= jiffies
+ max_delay
;
1080 mod_timer(&querier
->timer
, jiffies
+ br
->multicast_querier_interval
);
1084 * Add port to router_list
1085 * list is maintained ordered by pointer value
1086 * and locked by br->multicast_lock and RCU
1088 static void br_multicast_add_router(struct net_bridge
*br
,
1089 struct net_bridge_port
*port
)
1091 struct net_bridge_port
*p
;
1092 struct hlist_node
*slot
= NULL
;
1094 if (!hlist_unhashed(&port
->rlist
))
1097 hlist_for_each_entry(p
, &br
->router_list
, rlist
) {
1098 if ((unsigned long) port
>= (unsigned long) p
)
1104 hlist_add_after_rcu(slot
, &port
->rlist
);
1106 hlist_add_head_rcu(&port
->rlist
, &br
->router_list
);
1109 static void br_multicast_mark_router(struct net_bridge
*br
,
1110 struct net_bridge_port
*port
)
1112 unsigned long now
= jiffies
;
1115 if (br
->multicast_router
== 1)
1116 mod_timer(&br
->multicast_router_timer
,
1117 now
+ br
->multicast_querier_interval
);
1121 if (port
->multicast_router
!= 1)
1124 br_multicast_add_router(br
, port
);
1126 mod_timer(&port
->multicast_router_timer
,
1127 now
+ br
->multicast_querier_interval
);
1130 static void br_multicast_query_received(struct net_bridge
*br
,
1131 struct net_bridge_port
*port
,
1132 struct bridge_mcast_querier
*querier
,
1134 bool is_general_query
,
1135 unsigned long max_delay
)
1137 if (saddr
&& is_general_query
)
1138 br_multicast_update_querier_timer(br
, querier
, max_delay
);
1139 else if (timer_pending(&querier
->timer
))
1142 br_multicast_mark_router(br
, port
);
1145 static int br_ip4_multicast_query(struct net_bridge
*br
,
1146 struct net_bridge_port
*port
,
1147 struct sk_buff
*skb
)
1149 const struct iphdr
*iph
= ip_hdr(skb
);
1150 struct igmphdr
*ih
= igmp_hdr(skb
);
1151 struct net_bridge_mdb_entry
*mp
;
1152 struct igmpv3_query
*ih3
;
1153 struct net_bridge_port_group
*p
;
1154 struct net_bridge_port_group __rcu
**pp
;
1155 unsigned long max_delay
;
1156 unsigned long now
= jiffies
;
1161 spin_lock(&br
->multicast_lock
);
1162 if (!netif_running(br
->dev
) ||
1163 (port
&& port
->state
== BR_STATE_DISABLED
))
1168 if (skb
->len
== sizeof(*ih
)) {
1169 max_delay
= ih
->code
* (HZ
/ IGMP_TIMER_SCALE
);
1172 max_delay
= 10 * HZ
;
1176 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
))) {
1181 ih3
= igmpv3_query_hdr(skb
);
1185 max_delay
= ih3
->code
?
1186 IGMPV3_MRC(ih3
->code
) * (HZ
/ IGMP_TIMER_SCALE
) : 1;
1189 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1190 * all-systems destination addresses (224.0.0.1) for general queries
1192 if (!group
&& iph
->daddr
!= htonl(INADDR_ALLHOSTS_GROUP
)) {
1197 br_multicast_query_received(br
, port
, &br
->ip4_querier
, !!iph
->saddr
,
1203 br_vlan_get_tag(skb
, &vid
);
1204 mp
= br_mdb_ip4_get(mlock_dereference(br
->mdb
, br
), group
, vid
);
1208 max_delay
*= br
->multicast_last_member_count
;
1211 (timer_pending(&mp
->timer
) ?
1212 time_after(mp
->timer
.expires
, now
+ max_delay
) :
1213 try_to_del_timer_sync(&mp
->timer
) >= 0))
1214 mod_timer(&mp
->timer
, now
+ max_delay
);
1216 for (pp
= &mp
->ports
;
1217 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1219 if (timer_pending(&p
->timer
) ?
1220 time_after(p
->timer
.expires
, now
+ max_delay
) :
1221 try_to_del_timer_sync(&p
->timer
) >= 0)
1222 mod_timer(&p
->timer
, now
+ max_delay
);
1226 spin_unlock(&br
->multicast_lock
);
1230 #if IS_ENABLED(CONFIG_IPV6)
1231 static int br_ip6_multicast_query(struct net_bridge
*br
,
1232 struct net_bridge_port
*port
,
1233 struct sk_buff
*skb
)
1235 const struct ipv6hdr
*ip6h
= ipv6_hdr(skb
);
1236 struct mld_msg
*mld
;
1237 struct net_bridge_mdb_entry
*mp
;
1238 struct mld2_query
*mld2q
;
1239 struct net_bridge_port_group
*p
;
1240 struct net_bridge_port_group __rcu
**pp
;
1241 unsigned long max_delay
;
1242 unsigned long now
= jiffies
;
1243 const struct in6_addr
*group
= NULL
;
1244 bool is_general_query
;
1248 spin_lock(&br
->multicast_lock
);
1249 if (!netif_running(br
->dev
) ||
1250 (port
&& port
->state
== BR_STATE_DISABLED
))
1253 /* RFC2710+RFC3810 (MLDv1+MLDv2) require link-local source addresses */
1254 if (!(ipv6_addr_type(&ip6h
->saddr
) & IPV6_ADDR_LINKLOCAL
)) {
1259 if (skb
->len
== sizeof(*mld
)) {
1260 if (!pskb_may_pull(skb
, sizeof(*mld
))) {
1264 mld
= (struct mld_msg
*) icmp6_hdr(skb
);
1265 max_delay
= msecs_to_jiffies(ntohs(mld
->mld_maxdelay
));
1267 group
= &mld
->mld_mca
;
1269 if (!pskb_may_pull(skb
, sizeof(*mld2q
))) {
1273 mld2q
= (struct mld2_query
*)icmp6_hdr(skb
);
1274 if (!mld2q
->mld2q_nsrcs
)
1275 group
= &mld2q
->mld2q_mca
;
1277 max_delay
= max(msecs_to_jiffies(mldv2_mrc(mld2q
)), 1UL);
1280 is_general_query
= group
&& ipv6_addr_any(group
);
1282 /* RFC2710+RFC3810 (MLDv1+MLDv2) require the multicast link layer
1283 * all-nodes destination address (ff02::1) for general queries
1285 if (is_general_query
&& !ipv6_addr_is_ll_all_nodes(&ip6h
->daddr
)) {
1290 br_multicast_query_received(br
, port
, &br
->ip6_querier
,
1291 !ipv6_addr_any(&ip6h
->saddr
),
1292 is_general_query
, max_delay
);
1297 br_vlan_get_tag(skb
, &vid
);
1298 mp
= br_mdb_ip6_get(mlock_dereference(br
->mdb
, br
), group
, vid
);
1302 max_delay
*= br
->multicast_last_member_count
;
1304 (timer_pending(&mp
->timer
) ?
1305 time_after(mp
->timer
.expires
, now
+ max_delay
) :
1306 try_to_del_timer_sync(&mp
->timer
) >= 0))
1307 mod_timer(&mp
->timer
, now
+ max_delay
);
1309 for (pp
= &mp
->ports
;
1310 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1312 if (timer_pending(&p
->timer
) ?
1313 time_after(p
->timer
.expires
, now
+ max_delay
) :
1314 try_to_del_timer_sync(&p
->timer
) >= 0)
1315 mod_timer(&p
->timer
, now
+ max_delay
);
1319 spin_unlock(&br
->multicast_lock
);
1324 static void br_multicast_leave_group(struct net_bridge
*br
,
1325 struct net_bridge_port
*port
,
1326 struct br_ip
*group
,
1327 struct bridge_mcast_querier
*querier
,
1328 struct bridge_mcast_query
*query
)
1330 struct net_bridge_mdb_htable
*mdb
;
1331 struct net_bridge_mdb_entry
*mp
;
1332 struct net_bridge_port_group
*p
;
1336 spin_lock(&br
->multicast_lock
);
1337 if (!netif_running(br
->dev
) ||
1338 (port
&& port
->state
== BR_STATE_DISABLED
) ||
1339 timer_pending(&querier
->timer
))
1342 mdb
= mlock_dereference(br
->mdb
, br
);
1343 mp
= br_mdb_ip_get(mdb
, group
);
1347 if (br
->multicast_querier
) {
1348 __br_multicast_send_query(br
, port
, &mp
->addr
);
1350 time
= jiffies
+ br
->multicast_last_member_count
*
1351 br
->multicast_last_member_interval
;
1353 mod_timer(&query
->timer
, time
);
1355 for (p
= mlock_dereference(mp
->ports
, br
);
1357 p
= mlock_dereference(p
->next
, br
)) {
1358 if (p
->port
!= port
)
1361 if (!hlist_unhashed(&p
->mglist
) &&
1362 (timer_pending(&p
->timer
) ?
1363 time_after(p
->timer
.expires
, time
) :
1364 try_to_del_timer_sync(&p
->timer
) >= 0)) {
1365 mod_timer(&p
->timer
, time
);
1372 if (port
&& (port
->flags
& BR_MULTICAST_FAST_LEAVE
)) {
1373 struct net_bridge_port_group __rcu
**pp
;
1375 for (pp
= &mp
->ports
;
1376 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1378 if (p
->port
!= port
)
1381 rcu_assign_pointer(*pp
, p
->next
);
1382 hlist_del_init(&p
->mglist
);
1383 del_timer(&p
->timer
);
1384 call_rcu_bh(&p
->rcu
, br_multicast_free_pg
);
1385 br_mdb_notify(br
->dev
, port
, group
, RTM_DELMDB
);
1387 if (!mp
->ports
&& !mp
->mglist
&&
1388 netif_running(br
->dev
))
1389 mod_timer(&mp
->timer
, jiffies
);
1395 time
= now
+ br
->multicast_last_member_count
*
1396 br
->multicast_last_member_interval
;
1400 (timer_pending(&mp
->timer
) ?
1401 time_after(mp
->timer
.expires
, time
) :
1402 try_to_del_timer_sync(&mp
->timer
) >= 0)) {
1403 mod_timer(&mp
->timer
, time
);
1409 for (p
= mlock_dereference(mp
->ports
, br
);
1411 p
= mlock_dereference(p
->next
, br
)) {
1412 if (p
->port
!= port
)
1415 if (!hlist_unhashed(&p
->mglist
) &&
1416 (timer_pending(&p
->timer
) ?
1417 time_after(p
->timer
.expires
, time
) :
1418 try_to_del_timer_sync(&p
->timer
) >= 0)) {
1419 mod_timer(&p
->timer
, time
);
1425 spin_unlock(&br
->multicast_lock
);
1428 static void br_ip4_multicast_leave_group(struct net_bridge
*br
,
1429 struct net_bridge_port
*port
,
1433 struct br_ip br_group
;
1434 struct bridge_mcast_query
*query
= port
? &port
->ip4_query
:
1437 if (ipv4_is_local_multicast(group
))
1440 br_group
.u
.ip4
= group
;
1441 br_group
.proto
= htons(ETH_P_IP
);
1444 br_multicast_leave_group(br
, port
, &br_group
, &br
->ip4_querier
, query
);
1447 #if IS_ENABLED(CONFIG_IPV6)
1448 static void br_ip6_multicast_leave_group(struct net_bridge
*br
,
1449 struct net_bridge_port
*port
,
1450 const struct in6_addr
*group
,
1453 struct br_ip br_group
;
1454 struct bridge_mcast_query
*query
= port
? &port
->ip6_query
:
1458 if (ipv6_addr_is_ll_all_nodes(group
))
1461 br_group
.u
.ip6
= *group
;
1462 br_group
.proto
= htons(ETH_P_IPV6
);
1465 br_multicast_leave_group(br
, port
, &br_group
, &br
->ip6_querier
, query
);
1469 static int br_multicast_ipv4_rcv(struct net_bridge
*br
,
1470 struct net_bridge_port
*port
,
1471 struct sk_buff
*skb
)
1473 struct sk_buff
*skb2
= skb
;
1474 const struct iphdr
*iph
;
1477 unsigned int offset
;
1481 /* We treat OOM as packet loss for now. */
1482 if (!pskb_may_pull(skb
, sizeof(*iph
)))
1487 if (iph
->ihl
< 5 || iph
->version
!= 4)
1490 if (!pskb_may_pull(skb
, ip_hdrlen(skb
)))
1495 if (unlikely(ip_fast_csum((u8
*)iph
, iph
->ihl
)))
1498 if (iph
->protocol
!= IPPROTO_IGMP
) {
1499 if (!ipv4_is_local_multicast(iph
->daddr
))
1500 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1504 len
= ntohs(iph
->tot_len
);
1505 if (skb
->len
< len
|| len
< ip_hdrlen(skb
))
1508 if (skb
->len
> len
) {
1509 skb2
= skb_clone(skb
, GFP_ATOMIC
);
1513 err
= pskb_trim_rcsum(skb2
, len
);
1518 len
-= ip_hdrlen(skb2
);
1519 offset
= skb_network_offset(skb2
) + ip_hdrlen(skb2
);
1520 __skb_pull(skb2
, offset
);
1521 skb_reset_transport_header(skb2
);
1524 if (!pskb_may_pull(skb2
, sizeof(*ih
)))
1527 switch (skb2
->ip_summed
) {
1528 case CHECKSUM_COMPLETE
:
1529 if (!csum_fold(skb2
->csum
))
1534 if (skb_checksum_complete(skb2
))
1540 br_vlan_get_tag(skb2
, &vid
);
1541 BR_INPUT_SKB_CB(skb
)->igmp
= 1;
1542 ih
= igmp_hdr(skb2
);
1545 case IGMP_HOST_MEMBERSHIP_REPORT
:
1546 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1547 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1548 err
= br_ip4_multicast_add_group(br
, port
, ih
->group
, vid
);
1550 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1551 err
= br_ip4_multicast_igmp3_report(br
, port
, skb2
);
1553 case IGMP_HOST_MEMBERSHIP_QUERY
:
1554 err
= br_ip4_multicast_query(br
, port
, skb2
);
1556 case IGMP_HOST_LEAVE_MESSAGE
:
1557 br_ip4_multicast_leave_group(br
, port
, ih
->group
, vid
);
1562 __skb_push(skb2
, offset
);
1569 #if IS_ENABLED(CONFIG_IPV6)
1570 static int br_multicast_ipv6_rcv(struct net_bridge
*br
,
1571 struct net_bridge_port
*port
,
1572 struct sk_buff
*skb
)
1574 struct sk_buff
*skb2
;
1575 const struct ipv6hdr
*ip6h
;
1584 if (!pskb_may_pull(skb
, sizeof(*ip6h
)))
1587 ip6h
= ipv6_hdr(skb
);
1590 * We're interested in MLD messages only.
1592 * - MLD has always Router Alert hop-by-hop option
1593 * - But we do not support jumbrograms.
1595 if (ip6h
->version
!= 6)
1598 /* Prevent flooding this packet if there is no listener present */
1599 if (!ipv6_addr_is_ll_all_nodes(&ip6h
->daddr
))
1600 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1602 if (ip6h
->nexthdr
!= IPPROTO_HOPOPTS
||
1603 ip6h
->payload_len
== 0)
1606 len
= ntohs(ip6h
->payload_len
) + sizeof(*ip6h
);
1610 nexthdr
= ip6h
->nexthdr
;
1611 offset
= ipv6_skip_exthdr(skb
, sizeof(*ip6h
), &nexthdr
, &frag_off
);
1613 if (offset
< 0 || nexthdr
!= IPPROTO_ICMPV6
)
1616 /* Okay, we found ICMPv6 header */
1617 skb2
= skb_clone(skb
, GFP_ATOMIC
);
1622 if (!pskb_may_pull(skb2
, offset
+ sizeof(struct icmp6hdr
)))
1625 len
-= offset
- skb_network_offset(skb2
);
1627 __skb_pull(skb2
, offset
);
1628 skb_reset_transport_header(skb2
);
1629 skb_postpull_rcsum(skb2
, skb_network_header(skb2
),
1630 skb_network_header_len(skb2
));
1632 icmp6_type
= icmp6_hdr(skb2
)->icmp6_type
;
1634 switch (icmp6_type
) {
1635 case ICMPV6_MGM_QUERY
:
1636 case ICMPV6_MGM_REPORT
:
1637 case ICMPV6_MGM_REDUCTION
:
1638 case ICMPV6_MLD2_REPORT
:
1645 /* Okay, we found MLD message. Check further. */
1646 if (skb2
->len
> len
) {
1647 err
= pskb_trim_rcsum(skb2
, len
);
1653 ip6h
= ipv6_hdr(skb2
);
1655 switch (skb2
->ip_summed
) {
1656 case CHECKSUM_COMPLETE
:
1657 if (!csum_ipv6_magic(&ip6h
->saddr
, &ip6h
->daddr
, skb2
->len
,
1658 IPPROTO_ICMPV6
, skb2
->csum
))
1662 skb2
->csum
= ~csum_unfold(csum_ipv6_magic(&ip6h
->saddr
,
1665 IPPROTO_ICMPV6
, 0));
1666 if (__skb_checksum_complete(skb2
))
1672 br_vlan_get_tag(skb
, &vid
);
1673 BR_INPUT_SKB_CB(skb
)->igmp
= 1;
1675 switch (icmp6_type
) {
1676 case ICMPV6_MGM_REPORT
:
1678 struct mld_msg
*mld
;
1679 if (!pskb_may_pull(skb2
, sizeof(*mld
))) {
1683 mld
= (struct mld_msg
*)skb_transport_header(skb2
);
1684 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1685 err
= br_ip6_multicast_add_group(br
, port
, &mld
->mld_mca
, vid
);
1688 case ICMPV6_MLD2_REPORT
:
1689 err
= br_ip6_multicast_mld2_report(br
, port
, skb2
);
1691 case ICMPV6_MGM_QUERY
:
1692 err
= br_ip6_multicast_query(br
, port
, skb2
);
1694 case ICMPV6_MGM_REDUCTION
:
1696 struct mld_msg
*mld
;
1697 if (!pskb_may_pull(skb2
, sizeof(*mld
))) {
1701 mld
= (struct mld_msg
*)skb_transport_header(skb2
);
1702 br_ip6_multicast_leave_group(br
, port
, &mld
->mld_mca
, vid
);
1712 int br_multicast_rcv(struct net_bridge
*br
, struct net_bridge_port
*port
,
1713 struct sk_buff
*skb
)
1715 BR_INPUT_SKB_CB(skb
)->igmp
= 0;
1716 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 0;
1718 if (br
->multicast_disabled
)
1721 switch (skb
->protocol
) {
1722 case htons(ETH_P_IP
):
1723 return br_multicast_ipv4_rcv(br
, port
, skb
);
1724 #if IS_ENABLED(CONFIG_IPV6)
1725 case htons(ETH_P_IPV6
):
1726 return br_multicast_ipv6_rcv(br
, port
, skb
);
1733 static void br_multicast_query_expired(struct net_bridge
*br
,
1734 struct bridge_mcast_query
*query
)
1736 spin_lock(&br
->multicast_lock
);
1737 if (query
->startup_sent
< br
->multicast_startup_query_count
)
1738 query
->startup_sent
++;
1740 br_multicast_send_query(br
, NULL
, query
);
1741 spin_unlock(&br
->multicast_lock
);
1744 static void br_ip4_multicast_query_expired(unsigned long data
)
1746 struct net_bridge
*br
= (void *)data
;
1748 br_multicast_query_expired(br
, &br
->ip4_query
);
1751 #if IS_ENABLED(CONFIG_IPV6)
1752 static void br_ip6_multicast_query_expired(unsigned long data
)
1754 struct net_bridge
*br
= (void *)data
;
1756 br_multicast_query_expired(br
, &br
->ip6_query
);
1760 void br_multicast_init(struct net_bridge
*br
)
1762 br
->hash_elasticity
= 4;
1765 br
->multicast_router
= 1;
1766 br
->multicast_querier
= 0;
1767 br
->multicast_query_use_ifaddr
= 0;
1768 br
->multicast_last_member_count
= 2;
1769 br
->multicast_startup_query_count
= 2;
1771 br
->multicast_last_member_interval
= HZ
;
1772 br
->multicast_query_response_interval
= 10 * HZ
;
1773 br
->multicast_startup_query_interval
= 125 * HZ
/ 4;
1774 br
->multicast_query_interval
= 125 * HZ
;
1775 br
->multicast_querier_interval
= 255 * HZ
;
1776 br
->multicast_membership_interval
= 260 * HZ
;
1778 br
->ip4_querier
.delay_time
= 0;
1779 #if IS_ENABLED(CONFIG_IPV6)
1780 br
->ip6_querier
.delay_time
= 0;
1782 br
->has_ipv6_addr
= 1;
1784 spin_lock_init(&br
->multicast_lock
);
1785 setup_timer(&br
->multicast_router_timer
,
1786 br_multicast_local_router_expired
, 0);
1787 setup_timer(&br
->ip4_querier
.timer
, br_ip4_multicast_querier_expired
,
1789 setup_timer(&br
->ip4_query
.timer
, br_ip4_multicast_query_expired
,
1791 #if IS_ENABLED(CONFIG_IPV6)
1792 setup_timer(&br
->ip6_querier
.timer
, br_ip6_multicast_querier_expired
,
1794 setup_timer(&br
->ip6_query
.timer
, br_ip6_multicast_query_expired
,
1799 static void __br_multicast_open(struct net_bridge
*br
,
1800 struct bridge_mcast_query
*query
)
1802 query
->startup_sent
= 0;
1804 if (br
->multicast_disabled
)
1807 mod_timer(&query
->timer
, jiffies
);
1810 void br_multicast_open(struct net_bridge
*br
)
1812 __br_multicast_open(br
, &br
->ip4_query
);
1813 #if IS_ENABLED(CONFIG_IPV6)
1814 __br_multicast_open(br
, &br
->ip6_query
);
1818 void br_multicast_stop(struct net_bridge
*br
)
1820 struct net_bridge_mdb_htable
*mdb
;
1821 struct net_bridge_mdb_entry
*mp
;
1822 struct hlist_node
*n
;
1826 del_timer_sync(&br
->multicast_router_timer
);
1827 del_timer_sync(&br
->ip4_querier
.timer
);
1828 del_timer_sync(&br
->ip4_query
.timer
);
1829 #if IS_ENABLED(CONFIG_IPV6)
1830 del_timer_sync(&br
->ip6_querier
.timer
);
1831 del_timer_sync(&br
->ip6_query
.timer
);
1834 spin_lock_bh(&br
->multicast_lock
);
1835 mdb
= mlock_dereference(br
->mdb
, br
);
1842 for (i
= 0; i
< mdb
->max
; i
++) {
1843 hlist_for_each_entry_safe(mp
, n
, &mdb
->mhash
[i
],
1845 del_timer(&mp
->timer
);
1846 call_rcu_bh(&mp
->rcu
, br_multicast_free_group
);
1851 spin_unlock_bh(&br
->multicast_lock
);
1853 spin_lock_bh(&br
->multicast_lock
);
1858 call_rcu_bh(&mdb
->rcu
, br_mdb_free
);
1861 spin_unlock_bh(&br
->multicast_lock
);
1864 int br_multicast_set_router(struct net_bridge
*br
, unsigned long val
)
1868 spin_lock_bh(&br
->multicast_lock
);
1869 if (!netif_running(br
->dev
))
1875 del_timer(&br
->multicast_router_timer
);
1878 br
->multicast_router
= val
;
1888 spin_unlock_bh(&br
->multicast_lock
);
1893 int br_multicast_set_port_router(struct net_bridge_port
*p
, unsigned long val
)
1895 struct net_bridge
*br
= p
->br
;
1898 spin_lock(&br
->multicast_lock
);
1899 if (!netif_running(br
->dev
) || p
->state
== BR_STATE_DISABLED
)
1906 p
->multicast_router
= val
;
1909 if (val
< 2 && !hlist_unhashed(&p
->rlist
))
1910 hlist_del_init_rcu(&p
->rlist
);
1915 del_timer(&p
->multicast_router_timer
);
1920 br_multicast_add_router(br
, p
);
1929 spin_unlock(&br
->multicast_lock
);
1934 static void br_multicast_start_querier(struct net_bridge
*br
,
1935 struct bridge_mcast_query
*query
)
1937 struct net_bridge_port
*port
;
1939 __br_multicast_open(br
, query
);
1941 list_for_each_entry(port
, &br
->port_list
, list
) {
1942 if (port
->state
== BR_STATE_DISABLED
||
1943 port
->state
== BR_STATE_BLOCKING
)
1946 if (query
== &br
->ip4_query
)
1947 br_multicast_enable(&port
->ip4_query
);
1948 #if IS_ENABLED(CONFIG_IPV6)
1950 br_multicast_enable(&port
->ip6_query
);
1955 int br_multicast_toggle(struct net_bridge
*br
, unsigned long val
)
1958 struct net_bridge_mdb_htable
*mdb
;
1960 spin_lock_bh(&br
->multicast_lock
);
1961 if (br
->multicast_disabled
== !val
)
1964 br
->multicast_disabled
= !val
;
1965 if (br
->multicast_disabled
)
1968 if (!netif_running(br
->dev
))
1971 mdb
= mlock_dereference(br
->mdb
, br
);
1976 br
->multicast_disabled
= !!val
;
1980 err
= br_mdb_rehash(&br
->mdb
, mdb
->max
,
1981 br
->hash_elasticity
);
1986 br_multicast_start_querier(br
, &br
->ip4_query
);
1987 #if IS_ENABLED(CONFIG_IPV6)
1988 br_multicast_start_querier(br
, &br
->ip6_query
);
1992 spin_unlock_bh(&br
->multicast_lock
);
1997 int br_multicast_set_querier(struct net_bridge
*br
, unsigned long val
)
1999 unsigned long max_delay
;
2003 spin_lock_bh(&br
->multicast_lock
);
2004 if (br
->multicast_querier
== val
)
2007 br
->multicast_querier
= val
;
2011 max_delay
= br
->multicast_query_response_interval
;
2013 if (!timer_pending(&br
->ip4_querier
.timer
))
2014 br
->ip4_querier
.delay_time
= jiffies
+ max_delay
;
2016 br_multicast_start_querier(br
, &br
->ip4_query
);
2018 #if IS_ENABLED(CONFIG_IPV6)
2019 if (!timer_pending(&br
->ip6_querier
.timer
))
2020 br
->ip6_querier
.delay_time
= jiffies
+ max_delay
;
2022 br_multicast_start_querier(br
, &br
->ip6_query
);
2026 spin_unlock_bh(&br
->multicast_lock
);
2031 int br_multicast_set_hash_max(struct net_bridge
*br
, unsigned long val
)
2035 struct net_bridge_mdb_htable
*mdb
;
2037 spin_lock_bh(&br
->multicast_lock
);
2038 if (!netif_running(br
->dev
))
2042 if (!is_power_of_2(val
))
2045 mdb
= mlock_dereference(br
->mdb
, br
);
2046 if (mdb
&& val
< mdb
->size
)
2062 err
= br_mdb_rehash(&br
->mdb
, br
->hash_max
,
2063 br
->hash_elasticity
);
2069 spin_unlock_bh(&br
->multicast_lock
);