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
, ETH_ALEN
);
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
, ETH_ALEN
);
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,
454 ipv6_eth_mc_map(&ip6h
->daddr
, eth
->h_dest
);
456 hopopt
= (u8
*)(ip6h
+ 1);
457 hopopt
[0] = IPPROTO_ICMPV6
; /* next hdr */
458 hopopt
[1] = 0; /* length of HbH */
459 hopopt
[2] = IPV6_TLV_ROUTERALERT
; /* Router Alert */
460 hopopt
[3] = 2; /* Length of RA Option */
461 hopopt
[4] = 0; /* Type = 0x0000 (MLD) */
463 hopopt
[6] = IPV6_TLV_PAD1
; /* Pad1 */
464 hopopt
[7] = IPV6_TLV_PAD1
; /* Pad1 */
466 skb_put(skb
, sizeof(*ip6h
) + 8);
469 skb_set_transport_header(skb
, skb
->len
);
470 mldq
= (struct mld_msg
*) icmp6_hdr(skb
);
472 interval
= ipv6_addr_any(group
) ?
473 br
->multicast_query_response_interval
:
474 br
->multicast_last_member_interval
;
476 mldq
->mld_type
= ICMPV6_MGM_QUERY
;
479 mldq
->mld_maxdelay
= htons((u16
)jiffies_to_msecs(interval
));
480 mldq
->mld_reserved
= 0;
481 mldq
->mld_mca
= *group
;
484 mldq
->mld_cksum
= csum_ipv6_magic(&ip6h
->saddr
, &ip6h
->daddr
,
485 sizeof(*mldq
), IPPROTO_ICMPV6
,
488 skb_put(skb
, sizeof(*mldq
));
490 __skb_pull(skb
, sizeof(*eth
));
497 static struct sk_buff
*br_multicast_alloc_query(struct net_bridge
*br
,
500 switch (addr
->proto
) {
501 case htons(ETH_P_IP
):
502 return br_ip4_multicast_alloc_query(br
, addr
->u
.ip4
);
503 #if IS_ENABLED(CONFIG_IPV6)
504 case htons(ETH_P_IPV6
):
505 return br_ip6_multicast_alloc_query(br
, &addr
->u
.ip6
);
511 static struct net_bridge_mdb_entry
*br_multicast_get_group(
512 struct net_bridge
*br
, struct net_bridge_port
*port
,
513 struct br_ip
*group
, int hash
)
515 struct net_bridge_mdb_htable
*mdb
;
516 struct net_bridge_mdb_entry
*mp
;
517 unsigned int count
= 0;
522 mdb
= rcu_dereference_protected(br
->mdb
, 1);
523 hlist_for_each_entry(mp
, &mdb
->mhash
[hash
], hlist
[mdb
->ver
]) {
525 if (unlikely(br_ip_equal(group
, &mp
->addr
)))
532 if (unlikely(count
> br
->hash_elasticity
&& count
)) {
534 br_info(br
, "Multicast hash table "
535 "chain limit reached: %s\n",
536 port
? port
->dev
->name
: br
->dev
->name
);
538 elasticity
= br
->hash_elasticity
;
541 if (mdb
->size
>= max
) {
543 if (unlikely(max
> br
->hash_max
)) {
544 br_warn(br
, "Multicast hash table maximum of %d "
545 "reached, disabling snooping: %s\n",
547 port
? port
->dev
->name
: br
->dev
->name
);
550 br
->multicast_disabled
= 1;
555 if (max
> mdb
->max
|| elasticity
) {
558 br_info(br
, "Multicast hash table "
560 port
? port
->dev
->name
: br
->dev
->name
);
565 err
= br_mdb_rehash(&br
->mdb
, max
, elasticity
);
567 br_warn(br
, "Cannot rehash multicast "
568 "hash table, disabling snooping: %s, %d, %d\n",
569 port
? port
->dev
->name
: br
->dev
->name
,
585 struct net_bridge_mdb_entry
*br_multicast_new_group(struct net_bridge
*br
,
586 struct net_bridge_port
*port
, struct br_ip
*group
)
588 struct net_bridge_mdb_htable
*mdb
;
589 struct net_bridge_mdb_entry
*mp
;
593 mdb
= rcu_dereference_protected(br
->mdb
, 1);
595 err
= br_mdb_rehash(&br
->mdb
, BR_HASH_SIZE
, 0);
601 hash
= br_ip_hash(mdb
, group
);
602 mp
= br_multicast_get_group(br
, port
, group
, hash
);
603 switch (PTR_ERR(mp
)) {
609 mdb
= rcu_dereference_protected(br
->mdb
, 1);
610 hash
= br_ip_hash(mdb
, group
);
617 mp
= kzalloc(sizeof(*mp
), GFP_ATOMIC
);
619 return ERR_PTR(-ENOMEM
);
623 setup_timer(&mp
->timer
, br_multicast_group_expired
,
626 hlist_add_head_rcu(&mp
->hlist
[mdb
->ver
], &mdb
->mhash
[hash
]);
633 struct net_bridge_port_group
*br_multicast_new_port_group(
634 struct net_bridge_port
*port
,
636 struct net_bridge_port_group __rcu
*next
,
639 struct net_bridge_port_group
*p
;
641 p
= kzalloc(sizeof(*p
), GFP_ATOMIC
);
648 rcu_assign_pointer(p
->next
, next
);
649 hlist_add_head(&p
->mglist
, &port
->mglist
);
650 setup_timer(&p
->timer
, br_multicast_port_group_expired
,
655 static int br_multicast_add_group(struct net_bridge
*br
,
656 struct net_bridge_port
*port
,
659 struct net_bridge_mdb_entry
*mp
;
660 struct net_bridge_port_group
*p
;
661 struct net_bridge_port_group __rcu
**pp
;
662 unsigned long now
= jiffies
;
665 spin_lock(&br
->multicast_lock
);
666 if (!netif_running(br
->dev
) ||
667 (port
&& port
->state
== BR_STATE_DISABLED
))
670 mp
= br_multicast_new_group(br
, port
, group
);
677 mod_timer(&mp
->timer
, now
+ br
->multicast_membership_interval
);
681 for (pp
= &mp
->ports
;
682 (p
= mlock_dereference(*pp
, br
)) != NULL
;
686 if ((unsigned long)p
->port
< (unsigned long)port
)
690 p
= br_multicast_new_port_group(port
, group
, *pp
, MDB_TEMPORARY
);
693 rcu_assign_pointer(*pp
, p
);
694 br_mdb_notify(br
->dev
, port
, group
, RTM_NEWMDB
);
697 mod_timer(&p
->timer
, now
+ br
->multicast_membership_interval
);
702 spin_unlock(&br
->multicast_lock
);
706 static int br_ip4_multicast_add_group(struct net_bridge
*br
,
707 struct net_bridge_port
*port
,
711 struct br_ip br_group
;
713 if (ipv4_is_local_multicast(group
))
716 br_group
.u
.ip4
= group
;
717 br_group
.proto
= htons(ETH_P_IP
);
720 return br_multicast_add_group(br
, port
, &br_group
);
723 #if IS_ENABLED(CONFIG_IPV6)
724 static int br_ip6_multicast_add_group(struct net_bridge
*br
,
725 struct net_bridge_port
*port
,
726 const struct in6_addr
*group
,
729 struct br_ip br_group
;
731 if (ipv6_addr_is_ll_all_nodes(group
))
734 br_group
.u
.ip6
= *group
;
735 br_group
.proto
= htons(ETH_P_IPV6
);
738 return br_multicast_add_group(br
, port
, &br_group
);
742 static void br_multicast_router_expired(unsigned long data
)
744 struct net_bridge_port
*port
= (void *)data
;
745 struct net_bridge
*br
= port
->br
;
747 spin_lock(&br
->multicast_lock
);
748 if (port
->multicast_router
!= 1 ||
749 timer_pending(&port
->multicast_router_timer
) ||
750 hlist_unhashed(&port
->rlist
))
753 hlist_del_init_rcu(&port
->rlist
);
756 spin_unlock(&br
->multicast_lock
);
759 static void br_multicast_local_router_expired(unsigned long data
)
763 static void br_multicast_querier_expired(struct net_bridge
*br
,
764 struct bridge_mcast_query
*query
)
766 spin_lock(&br
->multicast_lock
);
767 if (!netif_running(br
->dev
) || br
->multicast_disabled
)
770 br_multicast_start_querier(br
, query
);
773 spin_unlock(&br
->multicast_lock
);
776 static void br_ip4_multicast_querier_expired(unsigned long data
)
778 struct net_bridge
*br
= (void *)data
;
780 br_multicast_querier_expired(br
, &br
->ip4_query
);
783 #if IS_ENABLED(CONFIG_IPV6)
784 static void br_ip6_multicast_querier_expired(unsigned long data
)
786 struct net_bridge
*br
= (void *)data
;
788 br_multicast_querier_expired(br
, &br
->ip6_query
);
792 static void __br_multicast_send_query(struct net_bridge
*br
,
793 struct net_bridge_port
*port
,
798 skb
= br_multicast_alloc_query(br
, ip
);
803 __skb_push(skb
, sizeof(struct ethhdr
));
804 skb
->dev
= port
->dev
;
805 NF_HOOK(NFPROTO_BRIDGE
, NF_BR_LOCAL_OUT
, skb
, NULL
, skb
->dev
,
811 static void br_multicast_send_query(struct net_bridge
*br
,
812 struct net_bridge_port
*port
,
813 struct bridge_mcast_query
*query
)
816 struct br_ip br_group
;
817 struct bridge_mcast_querier
*querier
= NULL
;
819 if (!netif_running(br
->dev
) || br
->multicast_disabled
||
820 !br
->multicast_querier
)
823 memset(&br_group
.u
, 0, sizeof(br_group
.u
));
825 if (port
? (query
== &port
->ip4_query
) :
826 (query
== &br
->ip4_query
)) {
827 querier
= &br
->ip4_querier
;
828 br_group
.proto
= htons(ETH_P_IP
);
829 #if IS_ENABLED(CONFIG_IPV6)
831 querier
= &br
->ip6_querier
;
832 br_group
.proto
= htons(ETH_P_IPV6
);
836 if (!querier
|| timer_pending(&querier
->timer
))
839 __br_multicast_send_query(br
, port
, &br_group
);
842 time
+= query
->startup_sent
< br
->multicast_startup_query_count
?
843 br
->multicast_startup_query_interval
:
844 br
->multicast_query_interval
;
845 mod_timer(&query
->timer
, time
);
848 static void br_multicast_port_query_expired(struct net_bridge_port
*port
,
849 struct bridge_mcast_query
*query
)
851 struct net_bridge
*br
= port
->br
;
853 spin_lock(&br
->multicast_lock
);
854 if (port
->state
== BR_STATE_DISABLED
||
855 port
->state
== BR_STATE_BLOCKING
)
858 if (query
->startup_sent
< br
->multicast_startup_query_count
)
859 query
->startup_sent
++;
861 br_multicast_send_query(port
->br
, port
, query
);
864 spin_unlock(&br
->multicast_lock
);
867 static void br_ip4_multicast_port_query_expired(unsigned long data
)
869 struct net_bridge_port
*port
= (void *)data
;
871 br_multicast_port_query_expired(port
, &port
->ip4_query
);
874 #if IS_ENABLED(CONFIG_IPV6)
875 static void br_ip6_multicast_port_query_expired(unsigned long data
)
877 struct net_bridge_port
*port
= (void *)data
;
879 br_multicast_port_query_expired(port
, &port
->ip6_query
);
883 void br_multicast_add_port(struct net_bridge_port
*port
)
885 port
->multicast_router
= 1;
887 setup_timer(&port
->multicast_router_timer
, br_multicast_router_expired
,
888 (unsigned long)port
);
889 setup_timer(&port
->ip4_query
.timer
, br_ip4_multicast_port_query_expired
,
890 (unsigned long)port
);
891 #if IS_ENABLED(CONFIG_IPV6)
892 setup_timer(&port
->ip6_query
.timer
, br_ip6_multicast_port_query_expired
,
893 (unsigned long)port
);
897 void br_multicast_del_port(struct net_bridge_port
*port
)
899 del_timer_sync(&port
->multicast_router_timer
);
902 static void br_multicast_enable(struct bridge_mcast_query
*query
)
904 query
->startup_sent
= 0;
906 if (try_to_del_timer_sync(&query
->timer
) >= 0 ||
907 del_timer(&query
->timer
))
908 mod_timer(&query
->timer
, jiffies
);
911 void br_multicast_enable_port(struct net_bridge_port
*port
)
913 struct net_bridge
*br
= port
->br
;
915 spin_lock(&br
->multicast_lock
);
916 if (br
->multicast_disabled
|| !netif_running(br
->dev
))
919 br_multicast_enable(&port
->ip4_query
);
920 #if IS_ENABLED(CONFIG_IPV6)
921 br_multicast_enable(&port
->ip6_query
);
925 spin_unlock(&br
->multicast_lock
);
928 void br_multicast_disable_port(struct net_bridge_port
*port
)
930 struct net_bridge
*br
= port
->br
;
931 struct net_bridge_port_group
*pg
;
932 struct hlist_node
*n
;
934 spin_lock(&br
->multicast_lock
);
935 hlist_for_each_entry_safe(pg
, n
, &port
->mglist
, mglist
)
936 br_multicast_del_pg(br
, pg
);
938 if (!hlist_unhashed(&port
->rlist
))
939 hlist_del_init_rcu(&port
->rlist
);
940 del_timer(&port
->multicast_router_timer
);
941 del_timer(&port
->ip4_query
.timer
);
942 #if IS_ENABLED(CONFIG_IPV6)
943 del_timer(&port
->ip6_query
.timer
);
945 spin_unlock(&br
->multicast_lock
);
948 static int br_ip4_multicast_igmp3_report(struct net_bridge
*br
,
949 struct net_bridge_port
*port
,
953 struct igmpv3_report
*ih
;
954 struct igmpv3_grec
*grec
;
962 if (!pskb_may_pull(skb
, sizeof(*ih
)))
965 ih
= igmpv3_report_hdr(skb
);
966 num
= ntohs(ih
->ngrec
);
969 for (i
= 0; i
< num
; i
++) {
970 len
+= sizeof(*grec
);
971 if (!pskb_may_pull(skb
, len
))
974 grec
= (void *)(skb
->data
+ len
- sizeof(*grec
));
975 group
= grec
->grec_mca
;
976 type
= grec
->grec_type
;
978 len
+= ntohs(grec
->grec_nsrcs
) * 4;
979 if (!pskb_may_pull(skb
, len
))
982 /* We treat this as an IGMPv2 report for now. */
984 case IGMPV3_MODE_IS_INCLUDE
:
985 case IGMPV3_MODE_IS_EXCLUDE
:
986 case IGMPV3_CHANGE_TO_INCLUDE
:
987 case IGMPV3_CHANGE_TO_EXCLUDE
:
988 case IGMPV3_ALLOW_NEW_SOURCES
:
989 case IGMPV3_BLOCK_OLD_SOURCES
:
996 err
= br_ip4_multicast_add_group(br
, port
, group
, vid
);
1004 #if IS_ENABLED(CONFIG_IPV6)
1005 static int br_ip6_multicast_mld2_report(struct net_bridge
*br
,
1006 struct net_bridge_port
*port
,
1007 struct sk_buff
*skb
,
1010 struct icmp6hdr
*icmp6h
;
1011 struct mld2_grec
*grec
;
1017 if (!pskb_may_pull(skb
, sizeof(*icmp6h
)))
1020 icmp6h
= icmp6_hdr(skb
);
1021 num
= ntohs(icmp6h
->icmp6_dataun
.un_data16
[1]);
1022 len
= sizeof(*icmp6h
);
1024 for (i
= 0; i
< num
; i
++) {
1025 __be16
*nsrcs
, _nsrcs
;
1027 nsrcs
= skb_header_pointer(skb
,
1028 len
+ offsetof(struct mld2_grec
,
1030 sizeof(_nsrcs
), &_nsrcs
);
1034 if (!pskb_may_pull(skb
,
1035 len
+ sizeof(*grec
) +
1036 sizeof(struct in6_addr
) * ntohs(*nsrcs
)))
1039 grec
= (struct mld2_grec
*)(skb
->data
+ len
);
1040 len
+= sizeof(*grec
) +
1041 sizeof(struct in6_addr
) * ntohs(*nsrcs
);
1043 /* We treat these as MLDv1 reports for now. */
1044 switch (grec
->grec_type
) {
1045 case MLD2_MODE_IS_INCLUDE
:
1046 case MLD2_MODE_IS_EXCLUDE
:
1047 case MLD2_CHANGE_TO_INCLUDE
:
1048 case MLD2_CHANGE_TO_EXCLUDE
:
1049 case MLD2_ALLOW_NEW_SOURCES
:
1050 case MLD2_BLOCK_OLD_SOURCES
:
1057 err
= br_ip6_multicast_add_group(br
, port
, &grec
->grec_mca
,
1068 br_multicast_update_querier_timer(struct net_bridge
*br
,
1069 struct bridge_mcast_querier
*querier
,
1070 unsigned long max_delay
)
1072 if (!timer_pending(&querier
->timer
))
1073 querier
->delay_time
= jiffies
+ max_delay
;
1075 mod_timer(&querier
->timer
, jiffies
+ br
->multicast_querier_interval
);
1079 * Add port to router_list
1080 * list is maintained ordered by pointer value
1081 * and locked by br->multicast_lock and RCU
1083 static void br_multicast_add_router(struct net_bridge
*br
,
1084 struct net_bridge_port
*port
)
1086 struct net_bridge_port
*p
;
1087 struct hlist_node
*slot
= NULL
;
1089 hlist_for_each_entry(p
, &br
->router_list
, rlist
) {
1090 if ((unsigned long) port
>= (unsigned long) p
)
1096 hlist_add_after_rcu(slot
, &port
->rlist
);
1098 hlist_add_head_rcu(&port
->rlist
, &br
->router_list
);
1101 static void br_multicast_mark_router(struct net_bridge
*br
,
1102 struct net_bridge_port
*port
)
1104 unsigned long now
= jiffies
;
1107 if (br
->multicast_router
== 1)
1108 mod_timer(&br
->multicast_router_timer
,
1109 now
+ br
->multicast_querier_interval
);
1113 if (port
->multicast_router
!= 1)
1116 if (!hlist_unhashed(&port
->rlist
))
1119 br_multicast_add_router(br
, port
);
1122 mod_timer(&port
->multicast_router_timer
,
1123 now
+ br
->multicast_querier_interval
);
1126 static void br_multicast_query_received(struct net_bridge
*br
,
1127 struct net_bridge_port
*port
,
1128 struct bridge_mcast_querier
*querier
,
1130 unsigned long max_delay
)
1133 br_multicast_update_querier_timer(br
, querier
, max_delay
);
1134 else if (timer_pending(&querier
->timer
))
1137 br_multicast_mark_router(br
, port
);
1140 static int br_ip4_multicast_query(struct net_bridge
*br
,
1141 struct net_bridge_port
*port
,
1142 struct sk_buff
*skb
,
1145 const struct iphdr
*iph
= ip_hdr(skb
);
1146 struct igmphdr
*ih
= igmp_hdr(skb
);
1147 struct net_bridge_mdb_entry
*mp
;
1148 struct igmpv3_query
*ih3
;
1149 struct net_bridge_port_group
*p
;
1150 struct net_bridge_port_group __rcu
**pp
;
1151 unsigned long max_delay
;
1152 unsigned long now
= jiffies
;
1156 spin_lock(&br
->multicast_lock
);
1157 if (!netif_running(br
->dev
) ||
1158 (port
&& port
->state
== BR_STATE_DISABLED
))
1163 if (skb
->len
== sizeof(*ih
)) {
1164 max_delay
= ih
->code
* (HZ
/ IGMP_TIMER_SCALE
);
1167 max_delay
= 10 * HZ
;
1171 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
))) {
1176 ih3
= igmpv3_query_hdr(skb
);
1180 max_delay
= ih3
->code
?
1181 IGMPV3_MRC(ih3
->code
) * (HZ
/ IGMP_TIMER_SCALE
) : 1;
1184 br_multicast_query_received(br
, port
, &br
->ip4_querier
, !!iph
->saddr
,
1190 mp
= br_mdb_ip4_get(mlock_dereference(br
->mdb
, br
), group
, vid
);
1194 max_delay
*= br
->multicast_last_member_count
;
1197 (timer_pending(&mp
->timer
) ?
1198 time_after(mp
->timer
.expires
, now
+ max_delay
) :
1199 try_to_del_timer_sync(&mp
->timer
) >= 0))
1200 mod_timer(&mp
->timer
, now
+ max_delay
);
1202 for (pp
= &mp
->ports
;
1203 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1205 if (timer_pending(&p
->timer
) ?
1206 time_after(p
->timer
.expires
, now
+ max_delay
) :
1207 try_to_del_timer_sync(&p
->timer
) >= 0)
1208 mod_timer(&p
->timer
, now
+ max_delay
);
1212 spin_unlock(&br
->multicast_lock
);
1216 #if IS_ENABLED(CONFIG_IPV6)
1217 static int br_ip6_multicast_query(struct net_bridge
*br
,
1218 struct net_bridge_port
*port
,
1219 struct sk_buff
*skb
,
1222 const struct ipv6hdr
*ip6h
= ipv6_hdr(skb
);
1223 struct mld_msg
*mld
;
1224 struct net_bridge_mdb_entry
*mp
;
1225 struct mld2_query
*mld2q
;
1226 struct net_bridge_port_group
*p
;
1227 struct net_bridge_port_group __rcu
**pp
;
1228 unsigned long max_delay
;
1229 unsigned long now
= jiffies
;
1230 const struct in6_addr
*group
= NULL
;
1233 spin_lock(&br
->multicast_lock
);
1234 if (!netif_running(br
->dev
) ||
1235 (port
&& port
->state
== BR_STATE_DISABLED
))
1238 if (skb
->len
== sizeof(*mld
)) {
1239 if (!pskb_may_pull(skb
, sizeof(*mld
))) {
1243 mld
= (struct mld_msg
*) icmp6_hdr(skb
);
1244 max_delay
= msecs_to_jiffies(ntohs(mld
->mld_maxdelay
));
1246 group
= &mld
->mld_mca
;
1248 if (!pskb_may_pull(skb
, sizeof(*mld2q
))) {
1252 mld2q
= (struct mld2_query
*)icmp6_hdr(skb
);
1253 if (!mld2q
->mld2q_nsrcs
)
1254 group
= &mld2q
->mld2q_mca
;
1256 max_delay
= max(msecs_to_jiffies(mldv2_mrc(mld2q
)), 1UL);
1259 br_multicast_query_received(br
, port
, &br
->ip6_querier
,
1260 !ipv6_addr_any(&ip6h
->saddr
), max_delay
);
1265 mp
= br_mdb_ip6_get(mlock_dereference(br
->mdb
, br
), group
, vid
);
1269 max_delay
*= br
->multicast_last_member_count
;
1271 (timer_pending(&mp
->timer
) ?
1272 time_after(mp
->timer
.expires
, now
+ max_delay
) :
1273 try_to_del_timer_sync(&mp
->timer
) >= 0))
1274 mod_timer(&mp
->timer
, now
+ max_delay
);
1276 for (pp
= &mp
->ports
;
1277 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1279 if (timer_pending(&p
->timer
) ?
1280 time_after(p
->timer
.expires
, now
+ max_delay
) :
1281 try_to_del_timer_sync(&p
->timer
) >= 0)
1282 mod_timer(&p
->timer
, now
+ max_delay
);
1286 spin_unlock(&br
->multicast_lock
);
1291 static void br_multicast_leave_group(struct net_bridge
*br
,
1292 struct net_bridge_port
*port
,
1293 struct br_ip
*group
,
1294 struct bridge_mcast_querier
*querier
,
1295 struct bridge_mcast_query
*query
)
1297 struct net_bridge_mdb_htable
*mdb
;
1298 struct net_bridge_mdb_entry
*mp
;
1299 struct net_bridge_port_group
*p
;
1303 spin_lock(&br
->multicast_lock
);
1304 if (!netif_running(br
->dev
) ||
1305 (port
&& port
->state
== BR_STATE_DISABLED
) ||
1306 timer_pending(&querier
->timer
))
1309 mdb
= mlock_dereference(br
->mdb
, br
);
1310 mp
= br_mdb_ip_get(mdb
, group
);
1314 if (br
->multicast_querier
) {
1315 __br_multicast_send_query(br
, port
, &mp
->addr
);
1317 time
= jiffies
+ br
->multicast_last_member_count
*
1318 br
->multicast_last_member_interval
;
1320 mod_timer(&query
->timer
, time
);
1322 for (p
= mlock_dereference(mp
->ports
, br
);
1324 p
= mlock_dereference(p
->next
, br
)) {
1325 if (p
->port
!= port
)
1328 if (!hlist_unhashed(&p
->mglist
) &&
1329 (timer_pending(&p
->timer
) ?
1330 time_after(p
->timer
.expires
, time
) :
1331 try_to_del_timer_sync(&p
->timer
) >= 0)) {
1332 mod_timer(&p
->timer
, time
);
1339 if (port
&& (port
->flags
& BR_MULTICAST_FAST_LEAVE
)) {
1340 struct net_bridge_port_group __rcu
**pp
;
1342 for (pp
= &mp
->ports
;
1343 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1345 if (p
->port
!= port
)
1348 rcu_assign_pointer(*pp
, p
->next
);
1349 hlist_del_init(&p
->mglist
);
1350 del_timer(&p
->timer
);
1351 call_rcu_bh(&p
->rcu
, br_multicast_free_pg
);
1352 br_mdb_notify(br
->dev
, port
, group
, RTM_DELMDB
);
1354 if (!mp
->ports
&& !mp
->mglist
&&
1355 netif_running(br
->dev
))
1356 mod_timer(&mp
->timer
, jiffies
);
1362 time
= now
+ br
->multicast_last_member_count
*
1363 br
->multicast_last_member_interval
;
1367 (timer_pending(&mp
->timer
) ?
1368 time_after(mp
->timer
.expires
, time
) :
1369 try_to_del_timer_sync(&mp
->timer
) >= 0)) {
1370 mod_timer(&mp
->timer
, time
);
1376 for (p
= mlock_dereference(mp
->ports
, br
);
1378 p
= mlock_dereference(p
->next
, br
)) {
1379 if (p
->port
!= port
)
1382 if (!hlist_unhashed(&p
->mglist
) &&
1383 (timer_pending(&p
->timer
) ?
1384 time_after(p
->timer
.expires
, time
) :
1385 try_to_del_timer_sync(&p
->timer
) >= 0)) {
1386 mod_timer(&p
->timer
, time
);
1392 spin_unlock(&br
->multicast_lock
);
1395 static void br_ip4_multicast_leave_group(struct net_bridge
*br
,
1396 struct net_bridge_port
*port
,
1400 struct br_ip br_group
;
1401 struct bridge_mcast_query
*query
= port
? &port
->ip4_query
:
1404 if (ipv4_is_local_multicast(group
))
1407 br_group
.u
.ip4
= group
;
1408 br_group
.proto
= htons(ETH_P_IP
);
1411 br_multicast_leave_group(br
, port
, &br_group
, &br
->ip4_querier
, query
);
1414 #if IS_ENABLED(CONFIG_IPV6)
1415 static void br_ip6_multicast_leave_group(struct net_bridge
*br
,
1416 struct net_bridge_port
*port
,
1417 const struct in6_addr
*group
,
1420 struct br_ip br_group
;
1421 struct bridge_mcast_query
*query
= port
? &port
->ip6_query
:
1425 if (ipv6_addr_is_ll_all_nodes(group
))
1428 br_group
.u
.ip6
= *group
;
1429 br_group
.proto
= htons(ETH_P_IPV6
);
1432 br_multicast_leave_group(br
, port
, &br_group
, &br
->ip6_querier
, query
);
1436 static int br_multicast_ipv4_rcv(struct net_bridge
*br
,
1437 struct net_bridge_port
*port
,
1438 struct sk_buff
*skb
,
1441 struct sk_buff
*skb2
= skb
;
1442 const struct iphdr
*iph
;
1445 unsigned int offset
;
1448 /* We treat OOM as packet loss for now. */
1449 if (!pskb_may_pull(skb
, sizeof(*iph
)))
1454 if (iph
->ihl
< 5 || iph
->version
!= 4)
1457 if (!pskb_may_pull(skb
, ip_hdrlen(skb
)))
1462 if (unlikely(ip_fast_csum((u8
*)iph
, iph
->ihl
)))
1465 if (iph
->protocol
!= IPPROTO_IGMP
) {
1466 if (!ipv4_is_local_multicast(iph
->daddr
))
1467 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1471 len
= ntohs(iph
->tot_len
);
1472 if (skb
->len
< len
|| len
< ip_hdrlen(skb
))
1475 if (skb
->len
> len
) {
1476 skb2
= skb_clone(skb
, GFP_ATOMIC
);
1480 err
= pskb_trim_rcsum(skb2
, len
);
1485 len
-= ip_hdrlen(skb2
);
1486 offset
= skb_network_offset(skb2
) + ip_hdrlen(skb2
);
1487 __skb_pull(skb2
, offset
);
1488 skb_reset_transport_header(skb2
);
1491 if (!pskb_may_pull(skb2
, sizeof(*ih
)))
1494 switch (skb2
->ip_summed
) {
1495 case CHECKSUM_COMPLETE
:
1496 if (!csum_fold(skb2
->csum
))
1501 if (skb_checksum_complete(skb2
))
1507 BR_INPUT_SKB_CB(skb
)->igmp
= 1;
1508 ih
= igmp_hdr(skb2
);
1511 case IGMP_HOST_MEMBERSHIP_REPORT
:
1512 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1513 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1514 err
= br_ip4_multicast_add_group(br
, port
, ih
->group
, vid
);
1516 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1517 err
= br_ip4_multicast_igmp3_report(br
, port
, skb2
, vid
);
1519 case IGMP_HOST_MEMBERSHIP_QUERY
:
1520 err
= br_ip4_multicast_query(br
, port
, skb2
, vid
);
1522 case IGMP_HOST_LEAVE_MESSAGE
:
1523 br_ip4_multicast_leave_group(br
, port
, ih
->group
, vid
);
1528 __skb_push(skb2
, offset
);
1535 #if IS_ENABLED(CONFIG_IPV6)
1536 static int br_multicast_ipv6_rcv(struct net_bridge
*br
,
1537 struct net_bridge_port
*port
,
1538 struct sk_buff
*skb
,
1541 struct sk_buff
*skb2
;
1542 const struct ipv6hdr
*ip6h
;
1550 if (!pskb_may_pull(skb
, sizeof(*ip6h
)))
1553 ip6h
= ipv6_hdr(skb
);
1556 * We're interested in MLD messages only.
1558 * - MLD has always Router Alert hop-by-hop option
1559 * - But we do not support jumbrograms.
1561 if (ip6h
->version
!= 6)
1564 /* Prevent flooding this packet if there is no listener present */
1565 if (!ipv6_addr_is_ll_all_nodes(&ip6h
->daddr
))
1566 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1568 if (ip6h
->nexthdr
!= IPPROTO_HOPOPTS
||
1569 ip6h
->payload_len
== 0)
1572 len
= ntohs(ip6h
->payload_len
) + sizeof(*ip6h
);
1576 nexthdr
= ip6h
->nexthdr
;
1577 offset
= ipv6_skip_exthdr(skb
, sizeof(*ip6h
), &nexthdr
, &frag_off
);
1579 if (offset
< 0 || nexthdr
!= IPPROTO_ICMPV6
)
1582 /* Okay, we found ICMPv6 header */
1583 skb2
= skb_clone(skb
, GFP_ATOMIC
);
1588 if (!pskb_may_pull(skb2
, offset
+ sizeof(struct icmp6hdr
)))
1591 len
-= offset
- skb_network_offset(skb2
);
1593 __skb_pull(skb2
, offset
);
1594 skb_reset_transport_header(skb2
);
1595 skb_postpull_rcsum(skb2
, skb_network_header(skb2
),
1596 skb_network_header_len(skb2
));
1598 icmp6_type
= icmp6_hdr(skb2
)->icmp6_type
;
1600 switch (icmp6_type
) {
1601 case ICMPV6_MGM_QUERY
:
1602 case ICMPV6_MGM_REPORT
:
1603 case ICMPV6_MGM_REDUCTION
:
1604 case ICMPV6_MLD2_REPORT
:
1611 /* Okay, we found MLD message. Check further. */
1612 if (skb2
->len
> len
) {
1613 err
= pskb_trim_rcsum(skb2
, len
);
1619 ip6h
= ipv6_hdr(skb2
);
1621 switch (skb2
->ip_summed
) {
1622 case CHECKSUM_COMPLETE
:
1623 if (!csum_ipv6_magic(&ip6h
->saddr
, &ip6h
->daddr
, skb2
->len
,
1624 IPPROTO_ICMPV6
, skb2
->csum
))
1628 skb2
->csum
= ~csum_unfold(csum_ipv6_magic(&ip6h
->saddr
,
1631 IPPROTO_ICMPV6
, 0));
1632 if (__skb_checksum_complete(skb2
))
1638 BR_INPUT_SKB_CB(skb
)->igmp
= 1;
1640 switch (icmp6_type
) {
1641 case ICMPV6_MGM_REPORT
:
1643 struct mld_msg
*mld
;
1644 if (!pskb_may_pull(skb2
, sizeof(*mld
))) {
1648 mld
= (struct mld_msg
*)skb_transport_header(skb2
);
1649 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1650 err
= br_ip6_multicast_add_group(br
, port
, &mld
->mld_mca
, vid
);
1653 case ICMPV6_MLD2_REPORT
:
1654 err
= br_ip6_multicast_mld2_report(br
, port
, skb2
, vid
);
1656 case ICMPV6_MGM_QUERY
:
1657 err
= br_ip6_multicast_query(br
, port
, skb2
, vid
);
1659 case ICMPV6_MGM_REDUCTION
:
1661 struct mld_msg
*mld
;
1662 if (!pskb_may_pull(skb2
, sizeof(*mld
))) {
1666 mld
= (struct mld_msg
*)skb_transport_header(skb2
);
1667 br_ip6_multicast_leave_group(br
, port
, &mld
->mld_mca
, vid
);
1677 int br_multicast_rcv(struct net_bridge
*br
, struct net_bridge_port
*port
,
1678 struct sk_buff
*skb
, u16 vid
)
1680 BR_INPUT_SKB_CB(skb
)->igmp
= 0;
1681 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 0;
1683 if (br
->multicast_disabled
)
1686 switch (skb
->protocol
) {
1687 case htons(ETH_P_IP
):
1688 return br_multicast_ipv4_rcv(br
, port
, skb
, vid
);
1689 #if IS_ENABLED(CONFIG_IPV6)
1690 case htons(ETH_P_IPV6
):
1691 return br_multicast_ipv6_rcv(br
, port
, skb
, vid
);
1698 static void br_multicast_query_expired(struct net_bridge
*br
,
1699 struct bridge_mcast_query
*query
)
1701 spin_lock(&br
->multicast_lock
);
1702 if (query
->startup_sent
< br
->multicast_startup_query_count
)
1703 query
->startup_sent
++;
1705 br_multicast_send_query(br
, NULL
, query
);
1706 spin_unlock(&br
->multicast_lock
);
1709 static void br_ip4_multicast_query_expired(unsigned long data
)
1711 struct net_bridge
*br
= (void *)data
;
1713 br_multicast_query_expired(br
, &br
->ip4_query
);
1716 #if IS_ENABLED(CONFIG_IPV6)
1717 static void br_ip6_multicast_query_expired(unsigned long data
)
1719 struct net_bridge
*br
= (void *)data
;
1721 br_multicast_query_expired(br
, &br
->ip6_query
);
1725 void br_multicast_init(struct net_bridge
*br
)
1727 br
->hash_elasticity
= 4;
1730 br
->multicast_router
= 1;
1731 br
->multicast_querier
= 0;
1732 br
->multicast_query_use_ifaddr
= 0;
1733 br
->multicast_last_member_count
= 2;
1734 br
->multicast_startup_query_count
= 2;
1736 br
->multicast_last_member_interval
= HZ
;
1737 br
->multicast_query_response_interval
= 10 * HZ
;
1738 br
->multicast_startup_query_interval
= 125 * HZ
/ 4;
1739 br
->multicast_query_interval
= 125 * HZ
;
1740 br
->multicast_querier_interval
= 255 * HZ
;
1741 br
->multicast_membership_interval
= 260 * HZ
;
1743 br
->ip4_querier
.delay_time
= 0;
1744 #if IS_ENABLED(CONFIG_IPV6)
1745 br
->ip6_querier
.delay_time
= 0;
1748 spin_lock_init(&br
->multicast_lock
);
1749 setup_timer(&br
->multicast_router_timer
,
1750 br_multicast_local_router_expired
, 0);
1751 setup_timer(&br
->ip4_querier
.timer
, br_ip4_multicast_querier_expired
,
1753 setup_timer(&br
->ip4_query
.timer
, br_ip4_multicast_query_expired
,
1755 #if IS_ENABLED(CONFIG_IPV6)
1756 setup_timer(&br
->ip6_querier
.timer
, br_ip6_multicast_querier_expired
,
1758 setup_timer(&br
->ip6_query
.timer
, br_ip6_multicast_query_expired
,
1763 static void __br_multicast_open(struct net_bridge
*br
,
1764 struct bridge_mcast_query
*query
)
1766 query
->startup_sent
= 0;
1768 if (br
->multicast_disabled
)
1771 mod_timer(&query
->timer
, jiffies
);
1774 void br_multicast_open(struct net_bridge
*br
)
1776 __br_multicast_open(br
, &br
->ip4_query
);
1777 #if IS_ENABLED(CONFIG_IPV6)
1778 __br_multicast_open(br
, &br
->ip6_query
);
1782 void br_multicast_stop(struct net_bridge
*br
)
1784 struct net_bridge_mdb_htable
*mdb
;
1785 struct net_bridge_mdb_entry
*mp
;
1786 struct hlist_node
*n
;
1790 del_timer_sync(&br
->multicast_router_timer
);
1791 del_timer_sync(&br
->ip4_querier
.timer
);
1792 del_timer_sync(&br
->ip4_query
.timer
);
1793 #if IS_ENABLED(CONFIG_IPV6)
1794 del_timer_sync(&br
->ip6_querier
.timer
);
1795 del_timer_sync(&br
->ip6_query
.timer
);
1798 spin_lock_bh(&br
->multicast_lock
);
1799 mdb
= mlock_dereference(br
->mdb
, br
);
1806 for (i
= 0; i
< mdb
->max
; i
++) {
1807 hlist_for_each_entry_safe(mp
, n
, &mdb
->mhash
[i
],
1809 del_timer(&mp
->timer
);
1810 call_rcu_bh(&mp
->rcu
, br_multicast_free_group
);
1815 spin_unlock_bh(&br
->multicast_lock
);
1817 spin_lock_bh(&br
->multicast_lock
);
1822 call_rcu_bh(&mdb
->rcu
, br_mdb_free
);
1825 spin_unlock_bh(&br
->multicast_lock
);
1828 int br_multicast_set_router(struct net_bridge
*br
, unsigned long val
)
1832 spin_lock_bh(&br
->multicast_lock
);
1833 if (!netif_running(br
->dev
))
1839 del_timer(&br
->multicast_router_timer
);
1842 br
->multicast_router
= val
;
1852 spin_unlock_bh(&br
->multicast_lock
);
1857 int br_multicast_set_port_router(struct net_bridge_port
*p
, unsigned long val
)
1859 struct net_bridge
*br
= p
->br
;
1862 spin_lock(&br
->multicast_lock
);
1863 if (!netif_running(br
->dev
) || p
->state
== BR_STATE_DISABLED
)
1870 p
->multicast_router
= val
;
1873 if (val
< 2 && !hlist_unhashed(&p
->rlist
))
1874 hlist_del_init_rcu(&p
->rlist
);
1879 del_timer(&p
->multicast_router_timer
);
1884 br_multicast_add_router(br
, p
);
1893 spin_unlock(&br
->multicast_lock
);
1898 static void br_multicast_start_querier(struct net_bridge
*br
,
1899 struct bridge_mcast_query
*query
)
1901 struct net_bridge_port
*port
;
1903 __br_multicast_open(br
, query
);
1905 list_for_each_entry(port
, &br
->port_list
, list
) {
1906 if (port
->state
== BR_STATE_DISABLED
||
1907 port
->state
== BR_STATE_BLOCKING
)
1910 if (query
== &br
->ip4_query
)
1911 br_multicast_enable(&port
->ip4_query
);
1912 #if IS_ENABLED(CONFIG_IPV6)
1914 br_multicast_enable(&port
->ip6_query
);
1919 int br_multicast_toggle(struct net_bridge
*br
, unsigned long val
)
1922 struct net_bridge_mdb_htable
*mdb
;
1924 spin_lock_bh(&br
->multicast_lock
);
1925 if (br
->multicast_disabled
== !val
)
1928 br
->multicast_disabled
= !val
;
1929 if (br
->multicast_disabled
)
1932 if (!netif_running(br
->dev
))
1935 mdb
= mlock_dereference(br
->mdb
, br
);
1940 br
->multicast_disabled
= !!val
;
1944 err
= br_mdb_rehash(&br
->mdb
, mdb
->max
,
1945 br
->hash_elasticity
);
1950 br_multicast_start_querier(br
, &br
->ip4_query
);
1951 #if IS_ENABLED(CONFIG_IPV6)
1952 br_multicast_start_querier(br
, &br
->ip6_query
);
1956 spin_unlock_bh(&br
->multicast_lock
);
1961 int br_multicast_set_querier(struct net_bridge
*br
, unsigned long val
)
1963 unsigned long max_delay
;
1967 spin_lock_bh(&br
->multicast_lock
);
1968 if (br
->multicast_querier
== val
)
1971 br
->multicast_querier
= val
;
1975 max_delay
= br
->multicast_query_response_interval
;
1977 if (!timer_pending(&br
->ip4_querier
.timer
))
1978 br
->ip4_querier
.delay_time
= jiffies
+ max_delay
;
1980 br_multicast_start_querier(br
, &br
->ip4_query
);
1982 #if IS_ENABLED(CONFIG_IPV6)
1983 if (!timer_pending(&br
->ip6_querier
.timer
))
1984 br
->ip6_querier
.delay_time
= jiffies
+ max_delay
;
1986 br_multicast_start_querier(br
, &br
->ip6_query
);
1990 spin_unlock_bh(&br
->multicast_lock
);
1995 int br_multicast_set_hash_max(struct net_bridge
*br
, unsigned long val
)
1999 struct net_bridge_mdb_htable
*mdb
;
2001 spin_lock_bh(&br
->multicast_lock
);
2002 if (!netif_running(br
->dev
))
2006 if (!is_power_of_2(val
))
2009 mdb
= mlock_dereference(br
->mdb
, br
);
2010 if (mdb
&& val
< mdb
->size
)
2026 err
= br_mdb_rehash(&br
->mdb
, br
->hash_max
,
2027 br
->hash_elasticity
);
2033 spin_unlock_bh(&br
->multicast_lock
);