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 static void br_ip4_multicast_leave_group(struct net_bridge
*br
,
43 struct net_bridge_port
*port
,
46 #if IS_ENABLED(CONFIG_IPV6)
47 static void br_ip6_multicast_leave_group(struct net_bridge
*br
,
48 struct net_bridge_port
*port
,
49 const struct in6_addr
*group
,
52 unsigned int br_mdb_rehash_seq
;
54 static inline int br_ip_equal(const struct br_ip
*a
, const struct br_ip
*b
)
56 if (a
->proto
!= b
->proto
)
62 return a
->u
.ip4
== b
->u
.ip4
;
63 #if IS_ENABLED(CONFIG_IPV6)
64 case htons(ETH_P_IPV6
):
65 return ipv6_addr_equal(&a
->u
.ip6
, &b
->u
.ip6
);
71 static inline int __br_ip4_hash(struct net_bridge_mdb_htable
*mdb
, __be32 ip
,
74 return jhash_2words((__force u32
)ip
, vid
, mdb
->secret
) & (mdb
->max
- 1);
77 #if IS_ENABLED(CONFIG_IPV6)
78 static inline int __br_ip6_hash(struct net_bridge_mdb_htable
*mdb
,
79 const struct in6_addr
*ip
,
82 return jhash_2words(ipv6_addr_hash(ip
), vid
,
83 mdb
->secret
) & (mdb
->max
- 1);
87 static inline int br_ip_hash(struct net_bridge_mdb_htable
*mdb
,
92 return __br_ip4_hash(mdb
, ip
->u
.ip4
, ip
->vid
);
93 #if IS_ENABLED(CONFIG_IPV6)
94 case htons(ETH_P_IPV6
):
95 return __br_ip6_hash(mdb
, &ip
->u
.ip6
, ip
->vid
);
101 static struct net_bridge_mdb_entry
*__br_mdb_ip_get(
102 struct net_bridge_mdb_htable
*mdb
, struct br_ip
*dst
, int hash
)
104 struct net_bridge_mdb_entry
*mp
;
106 hlist_for_each_entry_rcu(mp
, &mdb
->mhash
[hash
], hlist
[mdb
->ver
]) {
107 if (br_ip_equal(&mp
->addr
, dst
))
114 struct net_bridge_mdb_entry
*br_mdb_ip_get(struct net_bridge_mdb_htable
*mdb
,
120 return __br_mdb_ip_get(mdb
, dst
, br_ip_hash(mdb
, dst
));
123 static struct net_bridge_mdb_entry
*br_mdb_ip4_get(
124 struct net_bridge_mdb_htable
*mdb
, __be32 dst
, __u16 vid
)
129 br_dst
.proto
= htons(ETH_P_IP
);
132 return br_mdb_ip_get(mdb
, &br_dst
);
135 #if IS_ENABLED(CONFIG_IPV6)
136 static struct net_bridge_mdb_entry
*br_mdb_ip6_get(
137 struct net_bridge_mdb_htable
*mdb
, const struct in6_addr
*dst
,
143 br_dst
.proto
= htons(ETH_P_IPV6
);
146 return br_mdb_ip_get(mdb
, &br_dst
);
150 struct net_bridge_mdb_entry
*br_mdb_get(struct net_bridge
*br
,
151 struct sk_buff
*skb
, u16 vid
)
153 struct net_bridge_mdb_htable
*mdb
= rcu_dereference(br
->mdb
);
156 if (br
->multicast_disabled
)
159 if (BR_INPUT_SKB_CB(skb
)->igmp
)
162 ip
.proto
= skb
->protocol
;
165 switch (skb
->protocol
) {
166 case htons(ETH_P_IP
):
167 ip
.u
.ip4
= ip_hdr(skb
)->daddr
;
169 #if IS_ENABLED(CONFIG_IPV6)
170 case htons(ETH_P_IPV6
):
171 ip
.u
.ip6
= ipv6_hdr(skb
)->daddr
;
178 return br_mdb_ip_get(mdb
, &ip
);
181 static void br_mdb_free(struct rcu_head
*head
)
183 struct net_bridge_mdb_htable
*mdb
=
184 container_of(head
, struct net_bridge_mdb_htable
, rcu
);
185 struct net_bridge_mdb_htable
*old
= mdb
->old
;
192 static int br_mdb_copy(struct net_bridge_mdb_htable
*new,
193 struct net_bridge_mdb_htable
*old
,
196 struct net_bridge_mdb_entry
*mp
;
201 for (i
= 0; i
< old
->max
; i
++)
202 hlist_for_each_entry(mp
, &old
->mhash
[i
], hlist
[old
->ver
])
203 hlist_add_head(&mp
->hlist
[new->ver
],
204 &new->mhash
[br_ip_hash(new, &mp
->addr
)]);
210 for (i
= 0; i
< new->max
; i
++) {
212 hlist_for_each_entry(mp
, &new->mhash
[i
], hlist
[new->ver
])
218 return maxlen
> elasticity
? -EINVAL
: 0;
221 void br_multicast_free_pg(struct rcu_head
*head
)
223 struct net_bridge_port_group
*p
=
224 container_of(head
, struct net_bridge_port_group
, rcu
);
229 static void br_multicast_free_group(struct rcu_head
*head
)
231 struct net_bridge_mdb_entry
*mp
=
232 container_of(head
, struct net_bridge_mdb_entry
, rcu
);
237 static void br_multicast_group_expired(unsigned long data
)
239 struct net_bridge_mdb_entry
*mp
= (void *)data
;
240 struct net_bridge
*br
= mp
->br
;
241 struct net_bridge_mdb_htable
*mdb
;
243 spin_lock(&br
->multicast_lock
);
244 if (!netif_running(br
->dev
) || timer_pending(&mp
->timer
))
252 mdb
= mlock_dereference(br
->mdb
, br
);
254 hlist_del_rcu(&mp
->hlist
[mdb
->ver
]);
257 call_rcu_bh(&mp
->rcu
, br_multicast_free_group
);
260 spin_unlock(&br
->multicast_lock
);
263 static void br_multicast_del_pg(struct net_bridge
*br
,
264 struct net_bridge_port_group
*pg
)
266 struct net_bridge_mdb_htable
*mdb
;
267 struct net_bridge_mdb_entry
*mp
;
268 struct net_bridge_port_group
*p
;
269 struct net_bridge_port_group __rcu
**pp
;
271 mdb
= mlock_dereference(br
->mdb
, br
);
273 mp
= br_mdb_ip_get(mdb
, &pg
->addr
);
277 for (pp
= &mp
->ports
;
278 (p
= mlock_dereference(*pp
, br
)) != NULL
;
283 rcu_assign_pointer(*pp
, p
->next
);
284 hlist_del_init(&p
->mglist
);
285 del_timer(&p
->timer
);
286 br_mdb_notify(br
->dev
, p
->port
, &pg
->addr
, RTM_DELMDB
,
288 call_rcu_bh(&p
->rcu
, br_multicast_free_pg
);
290 if (!mp
->ports
&& !mp
->mglist
&&
291 netif_running(br
->dev
))
292 mod_timer(&mp
->timer
, jiffies
);
300 static void br_multicast_port_group_expired(unsigned long data
)
302 struct net_bridge_port_group
*pg
= (void *)data
;
303 struct net_bridge
*br
= pg
->port
->br
;
305 spin_lock(&br
->multicast_lock
);
306 if (!netif_running(br
->dev
) || timer_pending(&pg
->timer
) ||
307 hlist_unhashed(&pg
->mglist
) || pg
->flags
& MDB_PG_FLAGS_PERMANENT
)
310 br_multicast_del_pg(br
, pg
);
313 spin_unlock(&br
->multicast_lock
);
316 static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu
**mdbp
, int max
,
319 struct net_bridge_mdb_htable
*old
= rcu_dereference_protected(*mdbp
, 1);
320 struct net_bridge_mdb_htable
*mdb
;
323 mdb
= kmalloc(sizeof(*mdb
), GFP_ATOMIC
);
330 mdb
->mhash
= kzalloc(max
* sizeof(*mdb
->mhash
), GFP_ATOMIC
);
336 mdb
->size
= old
? old
->size
: 0;
337 mdb
->ver
= old
? old
->ver
^ 1 : 0;
339 if (!old
|| elasticity
)
340 get_random_bytes(&mdb
->secret
, sizeof(mdb
->secret
));
342 mdb
->secret
= old
->secret
;
347 err
= br_mdb_copy(mdb
, old
, elasticity
);
355 call_rcu_bh(&mdb
->rcu
, br_mdb_free
);
358 rcu_assign_pointer(*mdbp
, mdb
);
363 static struct sk_buff
*br_ip4_multicast_alloc_query(struct net_bridge
*br
,
371 skb
= netdev_alloc_skb_ip_align(br
->dev
, sizeof(*eth
) + sizeof(*iph
) +
376 skb
->protocol
= htons(ETH_P_IP
);
378 skb_reset_mac_header(skb
);
381 ether_addr_copy(eth
->h_source
, br
->dev
->dev_addr
);
384 eth
->h_dest
[2] = 0x5e;
388 eth
->h_proto
= htons(ETH_P_IP
);
389 skb_put(skb
, sizeof(*eth
));
391 skb_set_network_header(skb
, skb
->len
);
397 iph
->tot_len
= htons(sizeof(*iph
) + sizeof(*ih
) + 4);
399 iph
->frag_off
= htons(IP_DF
);
401 iph
->protocol
= IPPROTO_IGMP
;
402 iph
->saddr
= br
->multicast_query_use_ifaddr
?
403 inet_select_addr(br
->dev
, 0, RT_SCOPE_LINK
) : 0;
404 iph
->daddr
= htonl(INADDR_ALLHOSTS_GROUP
);
405 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
406 ((u8
*)&iph
[1])[1] = 4;
407 ((u8
*)&iph
[1])[2] = 0;
408 ((u8
*)&iph
[1])[3] = 0;
412 skb_set_transport_header(skb
, skb
->len
);
414 ih
->type
= IGMP_HOST_MEMBERSHIP_QUERY
;
415 ih
->code
= (group
? br
->multicast_last_member_interval
:
416 br
->multicast_query_response_interval
) /
417 (HZ
/ IGMP_TIMER_SCALE
);
420 ih
->csum
= ip_compute_csum((void *)ih
, sizeof(struct igmphdr
));
421 skb_put(skb
, sizeof(*ih
));
423 __skb_pull(skb
, sizeof(*eth
));
429 #if IS_ENABLED(CONFIG_IPV6)
430 static struct sk_buff
*br_ip6_multicast_alloc_query(struct net_bridge
*br
,
431 const struct in6_addr
*group
)
434 struct ipv6hdr
*ip6h
;
435 struct mld_msg
*mldq
;
438 unsigned long interval
;
440 skb
= netdev_alloc_skb_ip_align(br
->dev
, sizeof(*eth
) + sizeof(*ip6h
) +
445 skb
->protocol
= htons(ETH_P_IPV6
);
447 /* Ethernet header */
448 skb_reset_mac_header(skb
);
451 ether_addr_copy(eth
->h_source
, br
->dev
->dev_addr
);
452 eth
->h_proto
= htons(ETH_P_IPV6
);
453 skb_put(skb
, sizeof(*eth
));
455 /* IPv6 header + HbH option */
456 skb_set_network_header(skb
, skb
->len
);
457 ip6h
= ipv6_hdr(skb
);
459 *(__force __be32
*)ip6h
= htonl(0x60000000);
460 ip6h
->payload_len
= htons(8 + sizeof(*mldq
));
461 ip6h
->nexthdr
= IPPROTO_HOPOPTS
;
463 ipv6_addr_set(&ip6h
->daddr
, htonl(0xff020000), 0, 0, htonl(1));
464 if (ipv6_dev_get_saddr(dev_net(br
->dev
), br
->dev
, &ip6h
->daddr
, 0,
469 ipv6_eth_mc_map(&ip6h
->daddr
, eth
->h_dest
);
471 hopopt
= (u8
*)(ip6h
+ 1);
472 hopopt
[0] = IPPROTO_ICMPV6
; /* next hdr */
473 hopopt
[1] = 0; /* length of HbH */
474 hopopt
[2] = IPV6_TLV_ROUTERALERT
; /* Router Alert */
475 hopopt
[3] = 2; /* Length of RA Option */
476 hopopt
[4] = 0; /* Type = 0x0000 (MLD) */
478 hopopt
[6] = IPV6_TLV_PAD1
; /* Pad1 */
479 hopopt
[7] = IPV6_TLV_PAD1
; /* Pad1 */
481 skb_put(skb
, sizeof(*ip6h
) + 8);
484 skb_set_transport_header(skb
, skb
->len
);
485 mldq
= (struct mld_msg
*) icmp6_hdr(skb
);
487 interval
= ipv6_addr_any(group
) ?
488 br
->multicast_query_response_interval
:
489 br
->multicast_last_member_interval
;
491 mldq
->mld_type
= ICMPV6_MGM_QUERY
;
494 mldq
->mld_maxdelay
= htons((u16
)jiffies_to_msecs(interval
));
495 mldq
->mld_reserved
= 0;
496 mldq
->mld_mca
= *group
;
499 mldq
->mld_cksum
= csum_ipv6_magic(&ip6h
->saddr
, &ip6h
->daddr
,
500 sizeof(*mldq
), IPPROTO_ICMPV6
,
503 skb_put(skb
, sizeof(*mldq
));
505 __skb_pull(skb
, sizeof(*eth
));
512 static struct sk_buff
*br_multicast_alloc_query(struct net_bridge
*br
,
515 switch (addr
->proto
) {
516 case htons(ETH_P_IP
):
517 return br_ip4_multicast_alloc_query(br
, addr
->u
.ip4
);
518 #if IS_ENABLED(CONFIG_IPV6)
519 case htons(ETH_P_IPV6
):
520 return br_ip6_multicast_alloc_query(br
, &addr
->u
.ip6
);
526 static struct net_bridge_mdb_entry
*br_multicast_get_group(
527 struct net_bridge
*br
, struct net_bridge_port
*port
,
528 struct br_ip
*group
, int hash
)
530 struct net_bridge_mdb_htable
*mdb
;
531 struct net_bridge_mdb_entry
*mp
;
532 unsigned int count
= 0;
537 mdb
= rcu_dereference_protected(br
->mdb
, 1);
538 hlist_for_each_entry(mp
, &mdb
->mhash
[hash
], hlist
[mdb
->ver
]) {
540 if (unlikely(br_ip_equal(group
, &mp
->addr
)))
547 if (unlikely(count
> br
->hash_elasticity
&& count
)) {
549 br_info(br
, "Multicast hash table "
550 "chain limit reached: %s\n",
551 port
? port
->dev
->name
: br
->dev
->name
);
553 elasticity
= br
->hash_elasticity
;
556 if (mdb
->size
>= max
) {
558 if (unlikely(max
> br
->hash_max
)) {
559 br_warn(br
, "Multicast hash table maximum of %d "
560 "reached, disabling snooping: %s\n",
562 port
? port
->dev
->name
: br
->dev
->name
);
565 br
->multicast_disabled
= 1;
570 if (max
> mdb
->max
|| elasticity
) {
573 br_info(br
, "Multicast hash table "
575 port
? port
->dev
->name
: br
->dev
->name
);
580 err
= br_mdb_rehash(&br
->mdb
, max
, elasticity
);
582 br_warn(br
, "Cannot rehash multicast "
583 "hash table, disabling snooping: %s, %d, %d\n",
584 port
? port
->dev
->name
: br
->dev
->name
,
600 struct net_bridge_mdb_entry
*br_multicast_new_group(struct net_bridge
*br
,
601 struct net_bridge_port
*port
, struct br_ip
*group
)
603 struct net_bridge_mdb_htable
*mdb
;
604 struct net_bridge_mdb_entry
*mp
;
608 mdb
= rcu_dereference_protected(br
->mdb
, 1);
610 err
= br_mdb_rehash(&br
->mdb
, BR_HASH_SIZE
, 0);
616 hash
= br_ip_hash(mdb
, group
);
617 mp
= br_multicast_get_group(br
, port
, group
, hash
);
618 switch (PTR_ERR(mp
)) {
624 mdb
= rcu_dereference_protected(br
->mdb
, 1);
625 hash
= br_ip_hash(mdb
, group
);
632 mp
= kzalloc(sizeof(*mp
), GFP_ATOMIC
);
634 return ERR_PTR(-ENOMEM
);
638 setup_timer(&mp
->timer
, br_multicast_group_expired
,
641 hlist_add_head_rcu(&mp
->hlist
[mdb
->ver
], &mdb
->mhash
[hash
]);
648 struct net_bridge_port_group
*br_multicast_new_port_group(
649 struct net_bridge_port
*port
,
651 struct net_bridge_port_group __rcu
*next
,
654 struct net_bridge_port_group
*p
;
656 p
= kzalloc(sizeof(*p
), GFP_ATOMIC
);
663 rcu_assign_pointer(p
->next
, next
);
664 hlist_add_head(&p
->mglist
, &port
->mglist
);
665 setup_timer(&p
->timer
, br_multicast_port_group_expired
,
670 static int br_multicast_add_group(struct net_bridge
*br
,
671 struct net_bridge_port
*port
,
674 struct net_bridge_mdb_entry
*mp
;
675 struct net_bridge_port_group
*p
;
676 struct net_bridge_port_group __rcu
**pp
;
677 unsigned long now
= jiffies
;
680 spin_lock(&br
->multicast_lock
);
681 if (!netif_running(br
->dev
) ||
682 (port
&& port
->state
== BR_STATE_DISABLED
))
685 mp
= br_multicast_new_group(br
, port
, group
);
692 mod_timer(&mp
->timer
, now
+ br
->multicast_membership_interval
);
696 for (pp
= &mp
->ports
;
697 (p
= mlock_dereference(*pp
, br
)) != NULL
;
701 if ((unsigned long)p
->port
< (unsigned long)port
)
705 p
= br_multicast_new_port_group(port
, group
, *pp
, 0);
708 rcu_assign_pointer(*pp
, p
);
709 br_mdb_notify(br
->dev
, port
, group
, RTM_NEWMDB
, 0);
712 mod_timer(&p
->timer
, now
+ br
->multicast_membership_interval
);
717 spin_unlock(&br
->multicast_lock
);
721 static int br_ip4_multicast_add_group(struct net_bridge
*br
,
722 struct net_bridge_port
*port
,
726 struct br_ip br_group
;
728 if (ipv4_is_local_multicast(group
))
731 br_group
.u
.ip4
= group
;
732 br_group
.proto
= htons(ETH_P_IP
);
735 return br_multicast_add_group(br
, port
, &br_group
);
738 #if IS_ENABLED(CONFIG_IPV6)
739 static int br_ip6_multicast_add_group(struct net_bridge
*br
,
740 struct net_bridge_port
*port
,
741 const struct in6_addr
*group
,
744 struct br_ip br_group
;
746 if (ipv6_addr_is_ll_all_nodes(group
))
749 br_group
.u
.ip6
= *group
;
750 br_group
.proto
= htons(ETH_P_IPV6
);
753 return br_multicast_add_group(br
, port
, &br_group
);
757 static void br_multicast_router_expired(unsigned long data
)
759 struct net_bridge_port
*port
= (void *)data
;
760 struct net_bridge
*br
= port
->br
;
762 spin_lock(&br
->multicast_lock
);
763 if (port
->multicast_router
== MDB_RTR_TYPE_DISABLED
||
764 port
->multicast_router
== MDB_RTR_TYPE_PERM
||
765 timer_pending(&port
->multicast_router_timer
) ||
766 hlist_unhashed(&port
->rlist
))
769 hlist_del_init_rcu(&port
->rlist
);
770 br_rtr_notify(br
->dev
, port
, RTM_DELMDB
);
771 /* Don't allow timer refresh if the router expired */
772 if (port
->multicast_router
== MDB_RTR_TYPE_TEMP
)
773 port
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
776 spin_unlock(&br
->multicast_lock
);
779 static void br_multicast_local_router_expired(unsigned long data
)
783 static void br_multicast_querier_expired(struct net_bridge
*br
,
784 struct bridge_mcast_own_query
*query
)
786 spin_lock(&br
->multicast_lock
);
787 if (!netif_running(br
->dev
) || br
->multicast_disabled
)
790 br_multicast_start_querier(br
, query
);
793 spin_unlock(&br
->multicast_lock
);
796 static void br_ip4_multicast_querier_expired(unsigned long data
)
798 struct net_bridge
*br
= (void *)data
;
800 br_multicast_querier_expired(br
, &br
->ip4_own_query
);
803 #if IS_ENABLED(CONFIG_IPV6)
804 static void br_ip6_multicast_querier_expired(unsigned long data
)
806 struct net_bridge
*br
= (void *)data
;
808 br_multicast_querier_expired(br
, &br
->ip6_own_query
);
812 static void br_multicast_select_own_querier(struct net_bridge
*br
,
816 if (ip
->proto
== htons(ETH_P_IP
))
817 br
->ip4_querier
.addr
.u
.ip4
= ip_hdr(skb
)->saddr
;
818 #if IS_ENABLED(CONFIG_IPV6)
820 br
->ip6_querier
.addr
.u
.ip6
= ipv6_hdr(skb
)->saddr
;
824 static void __br_multicast_send_query(struct net_bridge
*br
,
825 struct net_bridge_port
*port
,
830 skb
= br_multicast_alloc_query(br
, ip
);
835 skb
->dev
= port
->dev
;
836 NF_HOOK(NFPROTO_BRIDGE
, NF_BR_LOCAL_OUT
,
837 dev_net(port
->dev
), NULL
, skb
, NULL
, skb
->dev
,
838 br_dev_queue_push_xmit
);
840 br_multicast_select_own_querier(br
, ip
, skb
);
845 static void br_multicast_send_query(struct net_bridge
*br
,
846 struct net_bridge_port
*port
,
847 struct bridge_mcast_own_query
*own_query
)
850 struct br_ip br_group
;
851 struct bridge_mcast_other_query
*other_query
= NULL
;
853 if (!netif_running(br
->dev
) || br
->multicast_disabled
||
854 !br
->multicast_querier
)
857 memset(&br_group
.u
, 0, sizeof(br_group
.u
));
859 if (port
? (own_query
== &port
->ip4_own_query
) :
860 (own_query
== &br
->ip4_own_query
)) {
861 other_query
= &br
->ip4_other_query
;
862 br_group
.proto
= htons(ETH_P_IP
);
863 #if IS_ENABLED(CONFIG_IPV6)
865 other_query
= &br
->ip6_other_query
;
866 br_group
.proto
= htons(ETH_P_IPV6
);
870 if (!other_query
|| timer_pending(&other_query
->timer
))
873 __br_multicast_send_query(br
, port
, &br_group
);
876 time
+= own_query
->startup_sent
< br
->multicast_startup_query_count
?
877 br
->multicast_startup_query_interval
:
878 br
->multicast_query_interval
;
879 mod_timer(&own_query
->timer
, time
);
883 br_multicast_port_query_expired(struct net_bridge_port
*port
,
884 struct bridge_mcast_own_query
*query
)
886 struct net_bridge
*br
= port
->br
;
888 spin_lock(&br
->multicast_lock
);
889 if (port
->state
== BR_STATE_DISABLED
||
890 port
->state
== BR_STATE_BLOCKING
)
893 if (query
->startup_sent
< br
->multicast_startup_query_count
)
894 query
->startup_sent
++;
896 br_multicast_send_query(port
->br
, port
, query
);
899 spin_unlock(&br
->multicast_lock
);
902 static void br_ip4_multicast_port_query_expired(unsigned long data
)
904 struct net_bridge_port
*port
= (void *)data
;
906 br_multicast_port_query_expired(port
, &port
->ip4_own_query
);
909 #if IS_ENABLED(CONFIG_IPV6)
910 static void br_ip6_multicast_port_query_expired(unsigned long data
)
912 struct net_bridge_port
*port
= (void *)data
;
914 br_multicast_port_query_expired(port
, &port
->ip6_own_query
);
918 void br_multicast_add_port(struct net_bridge_port
*port
)
920 port
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
922 setup_timer(&port
->multicast_router_timer
, br_multicast_router_expired
,
923 (unsigned long)port
);
924 setup_timer(&port
->ip4_own_query
.timer
,
925 br_ip4_multicast_port_query_expired
, (unsigned long)port
);
926 #if IS_ENABLED(CONFIG_IPV6)
927 setup_timer(&port
->ip6_own_query
.timer
,
928 br_ip6_multicast_port_query_expired
, (unsigned long)port
);
932 void br_multicast_del_port(struct net_bridge_port
*port
)
934 struct net_bridge
*br
= port
->br
;
935 struct net_bridge_port_group
*pg
;
936 struct hlist_node
*n
;
938 /* Take care of the remaining groups, only perm ones should be left */
939 spin_lock_bh(&br
->multicast_lock
);
940 hlist_for_each_entry_safe(pg
, n
, &port
->mglist
, mglist
)
941 br_multicast_del_pg(br
, pg
);
942 spin_unlock_bh(&br
->multicast_lock
);
943 del_timer_sync(&port
->multicast_router_timer
);
946 static void br_multicast_enable(struct bridge_mcast_own_query
*query
)
948 query
->startup_sent
= 0;
950 if (try_to_del_timer_sync(&query
->timer
) >= 0 ||
951 del_timer(&query
->timer
))
952 mod_timer(&query
->timer
, jiffies
);
955 void br_multicast_enable_port(struct net_bridge_port
*port
)
957 struct net_bridge
*br
= port
->br
;
959 spin_lock(&br
->multicast_lock
);
960 if (br
->multicast_disabled
|| !netif_running(br
->dev
))
963 br_multicast_enable(&port
->ip4_own_query
);
964 #if IS_ENABLED(CONFIG_IPV6)
965 br_multicast_enable(&port
->ip6_own_query
);
967 if (port
->multicast_router
== MDB_RTR_TYPE_PERM
&&
968 hlist_unhashed(&port
->rlist
))
969 br_multicast_add_router(br
, port
);
972 spin_unlock(&br
->multicast_lock
);
975 void br_multicast_disable_port(struct net_bridge_port
*port
)
977 struct net_bridge
*br
= port
->br
;
978 struct net_bridge_port_group
*pg
;
979 struct hlist_node
*n
;
981 spin_lock(&br
->multicast_lock
);
982 hlist_for_each_entry_safe(pg
, n
, &port
->mglist
, mglist
)
983 if (!(pg
->flags
& MDB_PG_FLAGS_PERMANENT
))
984 br_multicast_del_pg(br
, pg
);
986 if (!hlist_unhashed(&port
->rlist
)) {
987 hlist_del_init_rcu(&port
->rlist
);
988 br_rtr_notify(br
->dev
, port
, RTM_DELMDB
);
989 /* Don't allow timer refresh if disabling */
990 if (port
->multicast_router
== MDB_RTR_TYPE_TEMP
)
991 port
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
993 del_timer(&port
->multicast_router_timer
);
994 del_timer(&port
->ip4_own_query
.timer
);
995 #if IS_ENABLED(CONFIG_IPV6)
996 del_timer(&port
->ip6_own_query
.timer
);
998 spin_unlock(&br
->multicast_lock
);
1001 static int br_ip4_multicast_igmp3_report(struct net_bridge
*br
,
1002 struct net_bridge_port
*port
,
1003 struct sk_buff
*skb
,
1006 struct igmpv3_report
*ih
;
1007 struct igmpv3_grec
*grec
;
1015 ih
= igmpv3_report_hdr(skb
);
1016 num
= ntohs(ih
->ngrec
);
1017 len
= skb_transport_offset(skb
) + sizeof(*ih
);
1019 for (i
= 0; i
< num
; i
++) {
1020 len
+= sizeof(*grec
);
1021 if (!pskb_may_pull(skb
, len
))
1024 grec
= (void *)(skb
->data
+ len
- sizeof(*grec
));
1025 group
= grec
->grec_mca
;
1026 type
= grec
->grec_type
;
1028 len
+= ntohs(grec
->grec_nsrcs
) * 4;
1029 if (!pskb_may_pull(skb
, len
))
1032 /* We treat this as an IGMPv2 report for now. */
1034 case IGMPV3_MODE_IS_INCLUDE
:
1035 case IGMPV3_MODE_IS_EXCLUDE
:
1036 case IGMPV3_CHANGE_TO_INCLUDE
:
1037 case IGMPV3_CHANGE_TO_EXCLUDE
:
1038 case IGMPV3_ALLOW_NEW_SOURCES
:
1039 case IGMPV3_BLOCK_OLD_SOURCES
:
1046 if ((type
== IGMPV3_CHANGE_TO_INCLUDE
||
1047 type
== IGMPV3_MODE_IS_INCLUDE
) &&
1048 ntohs(grec
->grec_nsrcs
) == 0) {
1049 br_ip4_multicast_leave_group(br
, port
, group
, vid
);
1051 err
= br_ip4_multicast_add_group(br
, port
, group
, vid
);
1060 #if IS_ENABLED(CONFIG_IPV6)
1061 static int br_ip6_multicast_mld2_report(struct net_bridge
*br
,
1062 struct net_bridge_port
*port
,
1063 struct sk_buff
*skb
,
1066 struct icmp6hdr
*icmp6h
;
1067 struct mld2_grec
*grec
;
1073 if (!pskb_may_pull(skb
, sizeof(*icmp6h
)))
1076 icmp6h
= icmp6_hdr(skb
);
1077 num
= ntohs(icmp6h
->icmp6_dataun
.un_data16
[1]);
1078 len
= skb_transport_offset(skb
) + sizeof(*icmp6h
);
1080 for (i
= 0; i
< num
; i
++) {
1081 __be16
*nsrcs
, _nsrcs
;
1083 nsrcs
= skb_header_pointer(skb
,
1084 len
+ offsetof(struct mld2_grec
,
1086 sizeof(_nsrcs
), &_nsrcs
);
1090 if (!pskb_may_pull(skb
,
1091 len
+ sizeof(*grec
) +
1092 sizeof(struct in6_addr
) * ntohs(*nsrcs
)))
1095 grec
= (struct mld2_grec
*)(skb
->data
+ len
);
1096 len
+= sizeof(*grec
) +
1097 sizeof(struct in6_addr
) * ntohs(*nsrcs
);
1099 /* We treat these as MLDv1 reports for now. */
1100 switch (grec
->grec_type
) {
1101 case MLD2_MODE_IS_INCLUDE
:
1102 case MLD2_MODE_IS_EXCLUDE
:
1103 case MLD2_CHANGE_TO_INCLUDE
:
1104 case MLD2_CHANGE_TO_EXCLUDE
:
1105 case MLD2_ALLOW_NEW_SOURCES
:
1106 case MLD2_BLOCK_OLD_SOURCES
:
1113 if ((grec
->grec_type
== MLD2_CHANGE_TO_INCLUDE
||
1114 grec
->grec_type
== MLD2_MODE_IS_INCLUDE
) &&
1115 ntohs(*nsrcs
) == 0) {
1116 br_ip6_multicast_leave_group(br
, port
, &grec
->grec_mca
,
1119 err
= br_ip6_multicast_add_group(br
, port
,
1120 &grec
->grec_mca
, vid
);
1130 static bool br_ip4_multicast_select_querier(struct net_bridge
*br
,
1131 struct net_bridge_port
*port
,
1134 if (!timer_pending(&br
->ip4_own_query
.timer
) &&
1135 !timer_pending(&br
->ip4_other_query
.timer
))
1138 if (!br
->ip4_querier
.addr
.u
.ip4
)
1141 if (ntohl(saddr
) <= ntohl(br
->ip4_querier
.addr
.u
.ip4
))
1147 br
->ip4_querier
.addr
.u
.ip4
= saddr
;
1149 /* update protected by general multicast_lock by caller */
1150 rcu_assign_pointer(br
->ip4_querier
.port
, port
);
1155 #if IS_ENABLED(CONFIG_IPV6)
1156 static bool br_ip6_multicast_select_querier(struct net_bridge
*br
,
1157 struct net_bridge_port
*port
,
1158 struct in6_addr
*saddr
)
1160 if (!timer_pending(&br
->ip6_own_query
.timer
) &&
1161 !timer_pending(&br
->ip6_other_query
.timer
))
1164 if (ipv6_addr_cmp(saddr
, &br
->ip6_querier
.addr
.u
.ip6
) <= 0)
1170 br
->ip6_querier
.addr
.u
.ip6
= *saddr
;
1172 /* update protected by general multicast_lock by caller */
1173 rcu_assign_pointer(br
->ip6_querier
.port
, port
);
1179 static bool br_multicast_select_querier(struct net_bridge
*br
,
1180 struct net_bridge_port
*port
,
1181 struct br_ip
*saddr
)
1183 switch (saddr
->proto
) {
1184 case htons(ETH_P_IP
):
1185 return br_ip4_multicast_select_querier(br
, port
, saddr
->u
.ip4
);
1186 #if IS_ENABLED(CONFIG_IPV6)
1187 case htons(ETH_P_IPV6
):
1188 return br_ip6_multicast_select_querier(br
, port
, &saddr
->u
.ip6
);
1196 br_multicast_update_query_timer(struct net_bridge
*br
,
1197 struct bridge_mcast_other_query
*query
,
1198 unsigned long max_delay
)
1200 if (!timer_pending(&query
->timer
))
1201 query
->delay_time
= jiffies
+ max_delay
;
1203 mod_timer(&query
->timer
, jiffies
+ br
->multicast_querier_interval
);
1207 * Add port to router_list
1208 * list is maintained ordered by pointer value
1209 * and locked by br->multicast_lock and RCU
1211 static void br_multicast_add_router(struct net_bridge
*br
,
1212 struct net_bridge_port
*port
)
1214 struct net_bridge_port
*p
;
1215 struct hlist_node
*slot
= NULL
;
1217 if (!hlist_unhashed(&port
->rlist
))
1220 hlist_for_each_entry(p
, &br
->router_list
, rlist
) {
1221 if ((unsigned long) port
>= (unsigned long) p
)
1227 hlist_add_behind_rcu(&port
->rlist
, slot
);
1229 hlist_add_head_rcu(&port
->rlist
, &br
->router_list
);
1230 br_rtr_notify(br
->dev
, port
, RTM_NEWMDB
);
1233 static void br_multicast_mark_router(struct net_bridge
*br
,
1234 struct net_bridge_port
*port
)
1236 unsigned long now
= jiffies
;
1239 if (br
->multicast_router
== MDB_RTR_TYPE_TEMP_QUERY
)
1240 mod_timer(&br
->multicast_router_timer
,
1241 now
+ br
->multicast_querier_interval
);
1245 if (port
->multicast_router
== MDB_RTR_TYPE_DISABLED
||
1246 port
->multicast_router
== MDB_RTR_TYPE_PERM
)
1249 br_multicast_add_router(br
, port
);
1251 mod_timer(&port
->multicast_router_timer
,
1252 now
+ br
->multicast_querier_interval
);
1255 static void br_multicast_query_received(struct net_bridge
*br
,
1256 struct net_bridge_port
*port
,
1257 struct bridge_mcast_other_query
*query
,
1258 struct br_ip
*saddr
,
1259 unsigned long max_delay
)
1261 if (!br_multicast_select_querier(br
, port
, saddr
))
1264 br_multicast_update_query_timer(br
, query
, max_delay
);
1265 br_multicast_mark_router(br
, port
);
1268 static int br_ip4_multicast_query(struct net_bridge
*br
,
1269 struct net_bridge_port
*port
,
1270 struct sk_buff
*skb
,
1273 const struct iphdr
*iph
= ip_hdr(skb
);
1274 struct igmphdr
*ih
= igmp_hdr(skb
);
1275 struct net_bridge_mdb_entry
*mp
;
1276 struct igmpv3_query
*ih3
;
1277 struct net_bridge_port_group
*p
;
1278 struct net_bridge_port_group __rcu
**pp
;
1280 unsigned long max_delay
;
1281 unsigned long now
= jiffies
;
1285 spin_lock(&br
->multicast_lock
);
1286 if (!netif_running(br
->dev
) ||
1287 (port
&& port
->state
== BR_STATE_DISABLED
))
1292 if (skb
->len
== sizeof(*ih
)) {
1293 max_delay
= ih
->code
* (HZ
/ IGMP_TIMER_SCALE
);
1296 max_delay
= 10 * HZ
;
1299 } else if (skb
->len
>= sizeof(*ih3
)) {
1300 ih3
= igmpv3_query_hdr(skb
);
1304 max_delay
= ih3
->code
?
1305 IGMPV3_MRC(ih3
->code
) * (HZ
/ IGMP_TIMER_SCALE
) : 1;
1311 saddr
.proto
= htons(ETH_P_IP
);
1312 saddr
.u
.ip4
= iph
->saddr
;
1314 br_multicast_query_received(br
, port
, &br
->ip4_other_query
,
1319 mp
= br_mdb_ip4_get(mlock_dereference(br
->mdb
, br
), group
, vid
);
1323 max_delay
*= br
->multicast_last_member_count
;
1326 (timer_pending(&mp
->timer
) ?
1327 time_after(mp
->timer
.expires
, now
+ max_delay
) :
1328 try_to_del_timer_sync(&mp
->timer
) >= 0))
1329 mod_timer(&mp
->timer
, now
+ max_delay
);
1331 for (pp
= &mp
->ports
;
1332 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1334 if (timer_pending(&p
->timer
) ?
1335 time_after(p
->timer
.expires
, now
+ max_delay
) :
1336 try_to_del_timer_sync(&p
->timer
) >= 0)
1337 mod_timer(&p
->timer
, now
+ max_delay
);
1341 spin_unlock(&br
->multicast_lock
);
1345 #if IS_ENABLED(CONFIG_IPV6)
1346 static int br_ip6_multicast_query(struct net_bridge
*br
,
1347 struct net_bridge_port
*port
,
1348 struct sk_buff
*skb
,
1351 const struct ipv6hdr
*ip6h
= ipv6_hdr(skb
);
1352 struct mld_msg
*mld
;
1353 struct net_bridge_mdb_entry
*mp
;
1354 struct mld2_query
*mld2q
;
1355 struct net_bridge_port_group
*p
;
1356 struct net_bridge_port_group __rcu
**pp
;
1358 unsigned long max_delay
;
1359 unsigned long now
= jiffies
;
1360 const struct in6_addr
*group
= NULL
;
1361 bool is_general_query
;
1364 spin_lock(&br
->multicast_lock
);
1365 if (!netif_running(br
->dev
) ||
1366 (port
&& port
->state
== BR_STATE_DISABLED
))
1369 if (skb
->len
== sizeof(*mld
)) {
1370 if (!pskb_may_pull(skb
, sizeof(*mld
))) {
1374 mld
= (struct mld_msg
*) icmp6_hdr(skb
);
1375 max_delay
= msecs_to_jiffies(ntohs(mld
->mld_maxdelay
));
1377 group
= &mld
->mld_mca
;
1379 if (!pskb_may_pull(skb
, sizeof(*mld2q
))) {
1383 mld2q
= (struct mld2_query
*)icmp6_hdr(skb
);
1384 if (!mld2q
->mld2q_nsrcs
)
1385 group
= &mld2q
->mld2q_mca
;
1387 max_delay
= max(msecs_to_jiffies(mldv2_mrc(mld2q
)), 1UL);
1390 is_general_query
= group
&& ipv6_addr_any(group
);
1392 if (is_general_query
) {
1393 saddr
.proto
= htons(ETH_P_IPV6
);
1394 saddr
.u
.ip6
= ip6h
->saddr
;
1396 br_multicast_query_received(br
, port
, &br
->ip6_other_query
,
1399 } else if (!group
) {
1403 mp
= br_mdb_ip6_get(mlock_dereference(br
->mdb
, br
), group
, vid
);
1407 max_delay
*= br
->multicast_last_member_count
;
1409 (timer_pending(&mp
->timer
) ?
1410 time_after(mp
->timer
.expires
, now
+ max_delay
) :
1411 try_to_del_timer_sync(&mp
->timer
) >= 0))
1412 mod_timer(&mp
->timer
, now
+ max_delay
);
1414 for (pp
= &mp
->ports
;
1415 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1417 if (timer_pending(&p
->timer
) ?
1418 time_after(p
->timer
.expires
, now
+ max_delay
) :
1419 try_to_del_timer_sync(&p
->timer
) >= 0)
1420 mod_timer(&p
->timer
, now
+ max_delay
);
1424 spin_unlock(&br
->multicast_lock
);
1430 br_multicast_leave_group(struct net_bridge
*br
,
1431 struct net_bridge_port
*port
,
1432 struct br_ip
*group
,
1433 struct bridge_mcast_other_query
*other_query
,
1434 struct bridge_mcast_own_query
*own_query
)
1436 struct net_bridge_mdb_htable
*mdb
;
1437 struct net_bridge_mdb_entry
*mp
;
1438 struct net_bridge_port_group
*p
;
1442 spin_lock(&br
->multicast_lock
);
1443 if (!netif_running(br
->dev
) ||
1444 (port
&& port
->state
== BR_STATE_DISABLED
))
1447 mdb
= mlock_dereference(br
->mdb
, br
);
1448 mp
= br_mdb_ip_get(mdb
, group
);
1452 if (port
&& (port
->flags
& BR_MULTICAST_FAST_LEAVE
)) {
1453 struct net_bridge_port_group __rcu
**pp
;
1455 for (pp
= &mp
->ports
;
1456 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1458 if (p
->port
!= port
)
1461 rcu_assign_pointer(*pp
, p
->next
);
1462 hlist_del_init(&p
->mglist
);
1463 del_timer(&p
->timer
);
1464 call_rcu_bh(&p
->rcu
, br_multicast_free_pg
);
1465 br_mdb_notify(br
->dev
, port
, group
, RTM_DELMDB
,
1468 if (!mp
->ports
&& !mp
->mglist
&&
1469 netif_running(br
->dev
))
1470 mod_timer(&mp
->timer
, jiffies
);
1475 if (timer_pending(&other_query
->timer
))
1478 if (br
->multicast_querier
) {
1479 __br_multicast_send_query(br
, port
, &mp
->addr
);
1481 time
= jiffies
+ br
->multicast_last_member_count
*
1482 br
->multicast_last_member_interval
;
1484 mod_timer(&own_query
->timer
, time
);
1486 for (p
= mlock_dereference(mp
->ports
, br
);
1488 p
= mlock_dereference(p
->next
, br
)) {
1489 if (p
->port
!= port
)
1492 if (!hlist_unhashed(&p
->mglist
) &&
1493 (timer_pending(&p
->timer
) ?
1494 time_after(p
->timer
.expires
, time
) :
1495 try_to_del_timer_sync(&p
->timer
) >= 0)) {
1496 mod_timer(&p
->timer
, time
);
1504 time
= now
+ br
->multicast_last_member_count
*
1505 br
->multicast_last_member_interval
;
1509 (timer_pending(&mp
->timer
) ?
1510 time_after(mp
->timer
.expires
, time
) :
1511 try_to_del_timer_sync(&mp
->timer
) >= 0)) {
1512 mod_timer(&mp
->timer
, time
);
1518 for (p
= mlock_dereference(mp
->ports
, br
);
1520 p
= mlock_dereference(p
->next
, br
)) {
1521 if (p
->port
!= port
)
1524 if (!hlist_unhashed(&p
->mglist
) &&
1525 (timer_pending(&p
->timer
) ?
1526 time_after(p
->timer
.expires
, time
) :
1527 try_to_del_timer_sync(&p
->timer
) >= 0)) {
1528 mod_timer(&p
->timer
, time
);
1534 spin_unlock(&br
->multicast_lock
);
1537 static void br_ip4_multicast_leave_group(struct net_bridge
*br
,
1538 struct net_bridge_port
*port
,
1542 struct br_ip br_group
;
1543 struct bridge_mcast_own_query
*own_query
;
1545 if (ipv4_is_local_multicast(group
))
1548 own_query
= port
? &port
->ip4_own_query
: &br
->ip4_own_query
;
1550 br_group
.u
.ip4
= group
;
1551 br_group
.proto
= htons(ETH_P_IP
);
1554 br_multicast_leave_group(br
, port
, &br_group
, &br
->ip4_other_query
,
1558 #if IS_ENABLED(CONFIG_IPV6)
1559 static void br_ip6_multicast_leave_group(struct net_bridge
*br
,
1560 struct net_bridge_port
*port
,
1561 const struct in6_addr
*group
,
1564 struct br_ip br_group
;
1565 struct bridge_mcast_own_query
*own_query
;
1567 if (ipv6_addr_is_ll_all_nodes(group
))
1570 own_query
= port
? &port
->ip6_own_query
: &br
->ip6_own_query
;
1572 br_group
.u
.ip6
= *group
;
1573 br_group
.proto
= htons(ETH_P_IPV6
);
1576 br_multicast_leave_group(br
, port
, &br_group
, &br
->ip6_other_query
,
1581 static int br_multicast_ipv4_rcv(struct net_bridge
*br
,
1582 struct net_bridge_port
*port
,
1583 struct sk_buff
*skb
,
1586 struct sk_buff
*skb_trimmed
= NULL
;
1590 err
= ip_mc_check_igmp(skb
, &skb_trimmed
);
1592 if (err
== -ENOMSG
) {
1593 if (!ipv4_is_local_multicast(ip_hdr(skb
)->daddr
))
1594 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1596 } else if (err
< 0) {
1600 BR_INPUT_SKB_CB(skb
)->igmp
= 1;
1604 case IGMP_HOST_MEMBERSHIP_REPORT
:
1605 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1606 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1607 err
= br_ip4_multicast_add_group(br
, port
, ih
->group
, vid
);
1609 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1610 err
= br_ip4_multicast_igmp3_report(br
, port
, skb_trimmed
, vid
);
1612 case IGMP_HOST_MEMBERSHIP_QUERY
:
1613 err
= br_ip4_multicast_query(br
, port
, skb_trimmed
, vid
);
1615 case IGMP_HOST_LEAVE_MESSAGE
:
1616 br_ip4_multicast_leave_group(br
, port
, ih
->group
, vid
);
1620 if (skb_trimmed
&& skb_trimmed
!= skb
)
1621 kfree_skb(skb_trimmed
);
1626 #if IS_ENABLED(CONFIG_IPV6)
1627 static int br_multicast_ipv6_rcv(struct net_bridge
*br
,
1628 struct net_bridge_port
*port
,
1629 struct sk_buff
*skb
,
1632 struct sk_buff
*skb_trimmed
= NULL
;
1633 struct mld_msg
*mld
;
1636 err
= ipv6_mc_check_mld(skb
, &skb_trimmed
);
1638 if (err
== -ENOMSG
) {
1639 if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb
)->daddr
))
1640 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1642 } else if (err
< 0) {
1646 BR_INPUT_SKB_CB(skb
)->igmp
= 1;
1647 mld
= (struct mld_msg
*)skb_transport_header(skb
);
1649 switch (mld
->mld_type
) {
1650 case ICMPV6_MGM_REPORT
:
1651 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1652 err
= br_ip6_multicast_add_group(br
, port
, &mld
->mld_mca
, vid
);
1654 case ICMPV6_MLD2_REPORT
:
1655 err
= br_ip6_multicast_mld2_report(br
, port
, skb_trimmed
, vid
);
1657 case ICMPV6_MGM_QUERY
:
1658 err
= br_ip6_multicast_query(br
, port
, skb_trimmed
, vid
);
1660 case ICMPV6_MGM_REDUCTION
:
1661 br_ip6_multicast_leave_group(br
, port
, &mld
->mld_mca
, vid
);
1665 if (skb_trimmed
&& skb_trimmed
!= skb
)
1666 kfree_skb(skb_trimmed
);
1672 int br_multicast_rcv(struct net_bridge
*br
, struct net_bridge_port
*port
,
1673 struct sk_buff
*skb
, u16 vid
)
1675 BR_INPUT_SKB_CB(skb
)->igmp
= 0;
1676 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 0;
1678 if (br
->multicast_disabled
)
1681 switch (skb
->protocol
) {
1682 case htons(ETH_P_IP
):
1683 return br_multicast_ipv4_rcv(br
, port
, skb
, vid
);
1684 #if IS_ENABLED(CONFIG_IPV6)
1685 case htons(ETH_P_IPV6
):
1686 return br_multicast_ipv6_rcv(br
, port
, skb
, vid
);
1693 static void br_multicast_query_expired(struct net_bridge
*br
,
1694 struct bridge_mcast_own_query
*query
,
1695 struct bridge_mcast_querier
*querier
)
1697 spin_lock(&br
->multicast_lock
);
1698 if (query
->startup_sent
< br
->multicast_startup_query_count
)
1699 query
->startup_sent
++;
1701 RCU_INIT_POINTER(querier
->port
, NULL
);
1702 br_multicast_send_query(br
, NULL
, query
);
1703 spin_unlock(&br
->multicast_lock
);
1706 static void br_ip4_multicast_query_expired(unsigned long data
)
1708 struct net_bridge
*br
= (void *)data
;
1710 br_multicast_query_expired(br
, &br
->ip4_own_query
, &br
->ip4_querier
);
1713 #if IS_ENABLED(CONFIG_IPV6)
1714 static void br_ip6_multicast_query_expired(unsigned long data
)
1716 struct net_bridge
*br
= (void *)data
;
1718 br_multicast_query_expired(br
, &br
->ip6_own_query
, &br
->ip6_querier
);
1722 void br_multicast_init(struct net_bridge
*br
)
1724 br
->hash_elasticity
= 4;
1727 br
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
1728 br
->multicast_querier
= 0;
1729 br
->multicast_query_use_ifaddr
= 0;
1730 br
->multicast_last_member_count
= 2;
1731 br
->multicast_startup_query_count
= 2;
1733 br
->multicast_last_member_interval
= HZ
;
1734 br
->multicast_query_response_interval
= 10 * HZ
;
1735 br
->multicast_startup_query_interval
= 125 * HZ
/ 4;
1736 br
->multicast_query_interval
= 125 * HZ
;
1737 br
->multicast_querier_interval
= 255 * HZ
;
1738 br
->multicast_membership_interval
= 260 * HZ
;
1740 br
->ip4_other_query
.delay_time
= 0;
1741 br
->ip4_querier
.port
= NULL
;
1742 #if IS_ENABLED(CONFIG_IPV6)
1743 br
->ip6_other_query
.delay_time
= 0;
1744 br
->ip6_querier
.port
= NULL
;
1747 spin_lock_init(&br
->multicast_lock
);
1748 setup_timer(&br
->multicast_router_timer
,
1749 br_multicast_local_router_expired
, 0);
1750 setup_timer(&br
->ip4_other_query
.timer
,
1751 br_ip4_multicast_querier_expired
, (unsigned long)br
);
1752 setup_timer(&br
->ip4_own_query
.timer
, br_ip4_multicast_query_expired
,
1754 #if IS_ENABLED(CONFIG_IPV6)
1755 setup_timer(&br
->ip6_other_query
.timer
,
1756 br_ip6_multicast_querier_expired
, (unsigned long)br
);
1757 setup_timer(&br
->ip6_own_query
.timer
, br_ip6_multicast_query_expired
,
1762 static void __br_multicast_open(struct net_bridge
*br
,
1763 struct bridge_mcast_own_query
*query
)
1765 query
->startup_sent
= 0;
1767 if (br
->multicast_disabled
)
1770 mod_timer(&query
->timer
, jiffies
);
1773 void br_multicast_open(struct net_bridge
*br
)
1775 __br_multicast_open(br
, &br
->ip4_own_query
);
1776 #if IS_ENABLED(CONFIG_IPV6)
1777 __br_multicast_open(br
, &br
->ip6_own_query
);
1781 void br_multicast_stop(struct net_bridge
*br
)
1783 del_timer_sync(&br
->multicast_router_timer
);
1784 del_timer_sync(&br
->ip4_other_query
.timer
);
1785 del_timer_sync(&br
->ip4_own_query
.timer
);
1786 #if IS_ENABLED(CONFIG_IPV6)
1787 del_timer_sync(&br
->ip6_other_query
.timer
);
1788 del_timer_sync(&br
->ip6_own_query
.timer
);
1792 void br_multicast_dev_del(struct net_bridge
*br
)
1794 struct net_bridge_mdb_htable
*mdb
;
1795 struct net_bridge_mdb_entry
*mp
;
1796 struct hlist_node
*n
;
1800 spin_lock_bh(&br
->multicast_lock
);
1801 mdb
= mlock_dereference(br
->mdb
, br
);
1808 for (i
= 0; i
< mdb
->max
; i
++) {
1809 hlist_for_each_entry_safe(mp
, n
, &mdb
->mhash
[i
],
1811 del_timer(&mp
->timer
);
1812 call_rcu_bh(&mp
->rcu
, br_multicast_free_group
);
1817 spin_unlock_bh(&br
->multicast_lock
);
1819 spin_lock_bh(&br
->multicast_lock
);
1824 call_rcu_bh(&mdb
->rcu
, br_mdb_free
);
1827 spin_unlock_bh(&br
->multicast_lock
);
1830 int br_multicast_set_router(struct net_bridge
*br
, unsigned long val
)
1834 spin_lock_bh(&br
->multicast_lock
);
1837 case MDB_RTR_TYPE_DISABLED
:
1838 case MDB_RTR_TYPE_PERM
:
1839 del_timer(&br
->multicast_router_timer
);
1841 case MDB_RTR_TYPE_TEMP_QUERY
:
1842 br
->multicast_router
= val
;
1847 spin_unlock_bh(&br
->multicast_lock
);
1852 static void __del_port_router(struct net_bridge_port
*p
)
1854 if (hlist_unhashed(&p
->rlist
))
1856 hlist_del_init_rcu(&p
->rlist
);
1857 br_rtr_notify(p
->br
->dev
, p
, RTM_DELMDB
);
1860 int br_multicast_set_port_router(struct net_bridge_port
*p
, unsigned long val
)
1862 struct net_bridge
*br
= p
->br
;
1863 unsigned long now
= jiffies
;
1866 spin_lock(&br
->multicast_lock
);
1867 if (p
->multicast_router
== val
) {
1868 /* Refresh the temp router port timer */
1869 if (p
->multicast_router
== MDB_RTR_TYPE_TEMP
)
1870 mod_timer(&p
->multicast_router_timer
,
1871 now
+ br
->multicast_querier_interval
);
1876 case MDB_RTR_TYPE_DISABLED
:
1877 p
->multicast_router
= MDB_RTR_TYPE_DISABLED
;
1878 __del_port_router(p
);
1879 del_timer(&p
->multicast_router_timer
);
1881 case MDB_RTR_TYPE_TEMP_QUERY
:
1882 p
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
1883 __del_port_router(p
);
1885 case MDB_RTR_TYPE_PERM
:
1886 p
->multicast_router
= MDB_RTR_TYPE_PERM
;
1887 del_timer(&p
->multicast_router_timer
);
1888 br_multicast_add_router(br
, p
);
1890 case MDB_RTR_TYPE_TEMP
:
1891 p
->multicast_router
= MDB_RTR_TYPE_TEMP
;
1892 br_multicast_mark_router(br
, p
);
1899 spin_unlock(&br
->multicast_lock
);
1904 static void br_multicast_start_querier(struct net_bridge
*br
,
1905 struct bridge_mcast_own_query
*query
)
1907 struct net_bridge_port
*port
;
1909 __br_multicast_open(br
, query
);
1911 list_for_each_entry(port
, &br
->port_list
, list
) {
1912 if (port
->state
== BR_STATE_DISABLED
||
1913 port
->state
== BR_STATE_BLOCKING
)
1916 if (query
== &br
->ip4_own_query
)
1917 br_multicast_enable(&port
->ip4_own_query
);
1918 #if IS_ENABLED(CONFIG_IPV6)
1920 br_multicast_enable(&port
->ip6_own_query
);
1925 int br_multicast_toggle(struct net_bridge
*br
, unsigned long val
)
1928 struct net_bridge_mdb_htable
*mdb
;
1930 spin_lock_bh(&br
->multicast_lock
);
1931 if (br
->multicast_disabled
== !val
)
1934 br
->multicast_disabled
= !val
;
1935 if (br
->multicast_disabled
)
1938 if (!netif_running(br
->dev
))
1941 mdb
= mlock_dereference(br
->mdb
, br
);
1946 br
->multicast_disabled
= !!val
;
1950 err
= br_mdb_rehash(&br
->mdb
, mdb
->max
,
1951 br
->hash_elasticity
);
1956 br_multicast_start_querier(br
, &br
->ip4_own_query
);
1957 #if IS_ENABLED(CONFIG_IPV6)
1958 br_multicast_start_querier(br
, &br
->ip6_own_query
);
1962 spin_unlock_bh(&br
->multicast_lock
);
1967 int br_multicast_set_querier(struct net_bridge
*br
, unsigned long val
)
1969 unsigned long max_delay
;
1973 spin_lock_bh(&br
->multicast_lock
);
1974 if (br
->multicast_querier
== val
)
1977 br
->multicast_querier
= val
;
1981 max_delay
= br
->multicast_query_response_interval
;
1983 if (!timer_pending(&br
->ip4_other_query
.timer
))
1984 br
->ip4_other_query
.delay_time
= jiffies
+ max_delay
;
1986 br_multicast_start_querier(br
, &br
->ip4_own_query
);
1988 #if IS_ENABLED(CONFIG_IPV6)
1989 if (!timer_pending(&br
->ip6_other_query
.timer
))
1990 br
->ip6_other_query
.delay_time
= jiffies
+ max_delay
;
1992 br_multicast_start_querier(br
, &br
->ip6_own_query
);
1996 spin_unlock_bh(&br
->multicast_lock
);
2001 int br_multicast_set_hash_max(struct net_bridge
*br
, unsigned long val
)
2005 struct net_bridge_mdb_htable
*mdb
;
2007 spin_lock_bh(&br
->multicast_lock
);
2008 if (!is_power_of_2(val
))
2011 mdb
= mlock_dereference(br
->mdb
, br
);
2012 if (mdb
&& val
< mdb
->size
)
2028 err
= br_mdb_rehash(&br
->mdb
, br
->hash_max
,
2029 br
->hash_elasticity
);
2035 spin_unlock_bh(&br
->multicast_lock
);
2041 * br_multicast_list_adjacent - Returns snooped multicast addresses
2042 * @dev: The bridge port adjacent to which to retrieve addresses
2043 * @br_ip_list: The list to store found, snooped multicast IP addresses in
2045 * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
2046 * snooping feature on all bridge ports of dev's bridge device, excluding
2047 * the addresses from dev itself.
2049 * Returns the number of items added to br_ip_list.
2052 * - br_ip_list needs to be initialized by caller
2053 * - br_ip_list might contain duplicates in the end
2054 * (needs to be taken care of by caller)
2055 * - br_ip_list needs to be freed by caller
2057 int br_multicast_list_adjacent(struct net_device
*dev
,
2058 struct list_head
*br_ip_list
)
2060 struct net_bridge
*br
;
2061 struct net_bridge_port
*port
;
2062 struct net_bridge_port_group
*group
;
2063 struct br_ip_list
*entry
;
2067 if (!br_ip_list
|| !br_port_exists(dev
))
2070 port
= br_port_get_rcu(dev
);
2071 if (!port
|| !port
->br
)
2076 list_for_each_entry_rcu(port
, &br
->port_list
, list
) {
2077 if (!port
->dev
|| port
->dev
== dev
)
2080 hlist_for_each_entry_rcu(group
, &port
->mglist
, mglist
) {
2081 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
2085 entry
->addr
= group
->addr
;
2086 list_add(&entry
->list
, br_ip_list
);
2095 EXPORT_SYMBOL_GPL(br_multicast_list_adjacent
);
2098 * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2099 * @dev: The bridge port providing the bridge on which to check for a querier
2100 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2102 * Checks whether the given interface has a bridge on top and if so returns
2103 * true if a valid querier exists anywhere on the bridged link layer.
2104 * Otherwise returns false.
2106 bool br_multicast_has_querier_anywhere(struct net_device
*dev
, int proto
)
2108 struct net_bridge
*br
;
2109 struct net_bridge_port
*port
;
2114 if (!br_port_exists(dev
))
2117 port
= br_port_get_rcu(dev
);
2118 if (!port
|| !port
->br
)
2123 memset(ð
, 0, sizeof(eth
));
2124 eth
.h_proto
= htons(proto
);
2126 ret
= br_multicast_querier_exists(br
, ð
);
2132 EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere
);
2135 * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
2136 * @dev: The bridge port adjacent to which to check for a querier
2137 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2139 * Checks whether the given interface has a bridge on top and if so returns
2140 * true if a selected querier is behind one of the other ports of this
2141 * bridge. Otherwise returns false.
2143 bool br_multicast_has_querier_adjacent(struct net_device
*dev
, int proto
)
2145 struct net_bridge
*br
;
2146 struct net_bridge_port
*port
;
2150 if (!br_port_exists(dev
))
2153 port
= br_port_get_rcu(dev
);
2154 if (!port
|| !port
->br
)
2161 if (!timer_pending(&br
->ip4_other_query
.timer
) ||
2162 rcu_dereference(br
->ip4_querier
.port
) == port
)
2165 #if IS_ENABLED(CONFIG_IPV6)
2167 if (!timer_pending(&br
->ip6_other_query
.timer
) ||
2168 rcu_dereference(br
->ip6_querier
.port
) == port
)
2181 EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent
);