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
,
372 skb
= netdev_alloc_skb_ip_align(br
->dev
, sizeof(*eth
) + sizeof(*iph
) +
377 skb
->protocol
= htons(ETH_P_IP
);
379 skb_reset_mac_header(skb
);
382 ether_addr_copy(eth
->h_source
, br
->dev
->dev_addr
);
385 eth
->h_dest
[2] = 0x5e;
389 eth
->h_proto
= htons(ETH_P_IP
);
390 skb_put(skb
, sizeof(*eth
));
392 skb_set_network_header(skb
, skb
->len
);
398 iph
->tot_len
= htons(sizeof(*iph
) + sizeof(*ih
) + 4);
400 iph
->frag_off
= htons(IP_DF
);
402 iph
->protocol
= IPPROTO_IGMP
;
403 iph
->saddr
= br
->multicast_query_use_ifaddr
?
404 inet_select_addr(br
->dev
, 0, RT_SCOPE_LINK
) : 0;
405 iph
->daddr
= htonl(INADDR_ALLHOSTS_GROUP
);
406 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
407 ((u8
*)&iph
[1])[1] = 4;
408 ((u8
*)&iph
[1])[2] = 0;
409 ((u8
*)&iph
[1])[3] = 0;
413 skb_set_transport_header(skb
, skb
->len
);
415 *igmp_type
= IGMP_HOST_MEMBERSHIP_QUERY
;
416 ih
->type
= IGMP_HOST_MEMBERSHIP_QUERY
;
417 ih
->code
= (group
? br
->multicast_last_member_interval
:
418 br
->multicast_query_response_interval
) /
419 (HZ
/ IGMP_TIMER_SCALE
);
422 ih
->csum
= ip_compute_csum((void *)ih
, sizeof(struct igmphdr
));
423 skb_put(skb
, sizeof(*ih
));
425 __skb_pull(skb
, sizeof(*eth
));
431 #if IS_ENABLED(CONFIG_IPV6)
432 static struct sk_buff
*br_ip6_multicast_alloc_query(struct net_bridge
*br
,
433 const struct in6_addr
*grp
,
437 struct ipv6hdr
*ip6h
;
438 struct mld_msg
*mldq
;
441 unsigned long interval
;
443 skb
= netdev_alloc_skb_ip_align(br
->dev
, sizeof(*eth
) + sizeof(*ip6h
) +
448 skb
->protocol
= htons(ETH_P_IPV6
);
450 /* Ethernet header */
451 skb_reset_mac_header(skb
);
454 ether_addr_copy(eth
->h_source
, br
->dev
->dev_addr
);
455 eth
->h_proto
= htons(ETH_P_IPV6
);
456 skb_put(skb
, sizeof(*eth
));
458 /* IPv6 header + HbH option */
459 skb_set_network_header(skb
, skb
->len
);
460 ip6h
= ipv6_hdr(skb
);
462 *(__force __be32
*)ip6h
= htonl(0x60000000);
463 ip6h
->payload_len
= htons(8 + sizeof(*mldq
));
464 ip6h
->nexthdr
= IPPROTO_HOPOPTS
;
466 ipv6_addr_set(&ip6h
->daddr
, htonl(0xff020000), 0, 0, htonl(1));
467 if (ipv6_dev_get_saddr(dev_net(br
->dev
), br
->dev
, &ip6h
->daddr
, 0,
470 br
->has_ipv6_addr
= 0;
474 br
->has_ipv6_addr
= 1;
475 ipv6_eth_mc_map(&ip6h
->daddr
, eth
->h_dest
);
477 hopopt
= (u8
*)(ip6h
+ 1);
478 hopopt
[0] = IPPROTO_ICMPV6
; /* next hdr */
479 hopopt
[1] = 0; /* length of HbH */
480 hopopt
[2] = IPV6_TLV_ROUTERALERT
; /* Router Alert */
481 hopopt
[3] = 2; /* Length of RA Option */
482 hopopt
[4] = 0; /* Type = 0x0000 (MLD) */
484 hopopt
[6] = IPV6_TLV_PAD1
; /* Pad1 */
485 hopopt
[7] = IPV6_TLV_PAD1
; /* Pad1 */
487 skb_put(skb
, sizeof(*ip6h
) + 8);
490 skb_set_transport_header(skb
, skb
->len
);
491 mldq
= (struct mld_msg
*) icmp6_hdr(skb
);
493 interval
= ipv6_addr_any(grp
) ?
494 br
->multicast_query_response_interval
:
495 br
->multicast_last_member_interval
;
497 *igmp_type
= ICMPV6_MGM_QUERY
;
498 mldq
->mld_type
= ICMPV6_MGM_QUERY
;
501 mldq
->mld_maxdelay
= htons((u16
)jiffies_to_msecs(interval
));
502 mldq
->mld_reserved
= 0;
503 mldq
->mld_mca
= *grp
;
506 mldq
->mld_cksum
= csum_ipv6_magic(&ip6h
->saddr
, &ip6h
->daddr
,
507 sizeof(*mldq
), IPPROTO_ICMPV6
,
510 skb_put(skb
, sizeof(*mldq
));
512 __skb_pull(skb
, sizeof(*eth
));
519 static struct sk_buff
*br_multicast_alloc_query(struct net_bridge
*br
,
523 switch (addr
->proto
) {
524 case htons(ETH_P_IP
):
525 return br_ip4_multicast_alloc_query(br
, addr
->u
.ip4
, igmp_type
);
526 #if IS_ENABLED(CONFIG_IPV6)
527 case htons(ETH_P_IPV6
):
528 return br_ip6_multicast_alloc_query(br
, &addr
->u
.ip6
,
535 static struct net_bridge_mdb_entry
*br_multicast_get_group(
536 struct net_bridge
*br
, struct net_bridge_port
*port
,
537 struct br_ip
*group
, int hash
)
539 struct net_bridge_mdb_htable
*mdb
;
540 struct net_bridge_mdb_entry
*mp
;
541 unsigned int count
= 0;
546 mdb
= rcu_dereference_protected(br
->mdb
, 1);
547 hlist_for_each_entry(mp
, &mdb
->mhash
[hash
], hlist
[mdb
->ver
]) {
549 if (unlikely(br_ip_equal(group
, &mp
->addr
)))
556 if (unlikely(count
> br
->hash_elasticity
&& count
)) {
558 br_info(br
, "Multicast hash table "
559 "chain limit reached: %s\n",
560 port
? port
->dev
->name
: br
->dev
->name
);
562 elasticity
= br
->hash_elasticity
;
565 if (mdb
->size
>= max
) {
567 if (unlikely(max
> br
->hash_max
)) {
568 br_warn(br
, "Multicast hash table maximum of %d "
569 "reached, disabling snooping: %s\n",
571 port
? port
->dev
->name
: br
->dev
->name
);
574 br
->multicast_disabled
= 1;
579 if (max
> mdb
->max
|| elasticity
) {
582 br_info(br
, "Multicast hash table "
584 port
? port
->dev
->name
: br
->dev
->name
);
589 err
= br_mdb_rehash(&br
->mdb
, max
, elasticity
);
591 br_warn(br
, "Cannot rehash multicast "
592 "hash table, disabling snooping: %s, %d, %d\n",
593 port
? port
->dev
->name
: br
->dev
->name
,
609 struct net_bridge_mdb_entry
*br_multicast_new_group(struct net_bridge
*br
,
610 struct net_bridge_port
*port
, struct br_ip
*group
)
612 struct net_bridge_mdb_htable
*mdb
;
613 struct net_bridge_mdb_entry
*mp
;
617 mdb
= rcu_dereference_protected(br
->mdb
, 1);
619 err
= br_mdb_rehash(&br
->mdb
, BR_HASH_SIZE
, 0);
625 hash
= br_ip_hash(mdb
, group
);
626 mp
= br_multicast_get_group(br
, port
, group
, hash
);
627 switch (PTR_ERR(mp
)) {
633 mdb
= rcu_dereference_protected(br
->mdb
, 1);
634 hash
= br_ip_hash(mdb
, group
);
641 mp
= kzalloc(sizeof(*mp
), GFP_ATOMIC
);
643 return ERR_PTR(-ENOMEM
);
647 setup_timer(&mp
->timer
, br_multicast_group_expired
,
650 hlist_add_head_rcu(&mp
->hlist
[mdb
->ver
], &mdb
->mhash
[hash
]);
657 struct net_bridge_port_group
*br_multicast_new_port_group(
658 struct net_bridge_port
*port
,
660 struct net_bridge_port_group __rcu
*next
,
663 struct net_bridge_port_group
*p
;
665 p
= kzalloc(sizeof(*p
), GFP_ATOMIC
);
672 rcu_assign_pointer(p
->next
, next
);
673 hlist_add_head(&p
->mglist
, &port
->mglist
);
674 setup_timer(&p
->timer
, br_multicast_port_group_expired
,
679 static int br_multicast_add_group(struct net_bridge
*br
,
680 struct net_bridge_port
*port
,
683 struct net_bridge_mdb_entry
*mp
;
684 struct net_bridge_port_group
*p
;
685 struct net_bridge_port_group __rcu
**pp
;
686 unsigned long now
= jiffies
;
689 spin_lock(&br
->multicast_lock
);
690 if (!netif_running(br
->dev
) ||
691 (port
&& port
->state
== BR_STATE_DISABLED
))
694 mp
= br_multicast_new_group(br
, port
, group
);
701 mod_timer(&mp
->timer
, now
+ br
->multicast_membership_interval
);
705 for (pp
= &mp
->ports
;
706 (p
= mlock_dereference(*pp
, br
)) != NULL
;
710 if ((unsigned long)p
->port
< (unsigned long)port
)
714 p
= br_multicast_new_port_group(port
, group
, *pp
, 0);
717 rcu_assign_pointer(*pp
, p
);
718 br_mdb_notify(br
->dev
, port
, group
, RTM_NEWMDB
, 0);
721 mod_timer(&p
->timer
, now
+ br
->multicast_membership_interval
);
726 spin_unlock(&br
->multicast_lock
);
730 static int br_ip4_multicast_add_group(struct net_bridge
*br
,
731 struct net_bridge_port
*port
,
735 struct br_ip br_group
;
737 if (ipv4_is_local_multicast(group
))
740 br_group
.u
.ip4
= group
;
741 br_group
.proto
= htons(ETH_P_IP
);
744 return br_multicast_add_group(br
, port
, &br_group
);
747 #if IS_ENABLED(CONFIG_IPV6)
748 static int br_ip6_multicast_add_group(struct net_bridge
*br
,
749 struct net_bridge_port
*port
,
750 const struct in6_addr
*group
,
753 struct br_ip br_group
;
755 if (ipv6_addr_is_ll_all_nodes(group
))
758 br_group
.u
.ip6
= *group
;
759 br_group
.proto
= htons(ETH_P_IPV6
);
762 return br_multicast_add_group(br
, port
, &br_group
);
766 static void br_multicast_router_expired(unsigned long data
)
768 struct net_bridge_port
*port
= (void *)data
;
769 struct net_bridge
*br
= port
->br
;
771 spin_lock(&br
->multicast_lock
);
772 if (port
->multicast_router
== MDB_RTR_TYPE_DISABLED
||
773 port
->multicast_router
== MDB_RTR_TYPE_PERM
||
774 timer_pending(&port
->multicast_router_timer
) ||
775 hlist_unhashed(&port
->rlist
))
778 hlist_del_init_rcu(&port
->rlist
);
779 br_rtr_notify(br
->dev
, port
, RTM_DELMDB
);
780 /* Don't allow timer refresh if the router expired */
781 if (port
->multicast_router
== MDB_RTR_TYPE_TEMP
)
782 port
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
785 spin_unlock(&br
->multicast_lock
);
788 static void br_multicast_local_router_expired(unsigned long data
)
792 static void br_multicast_querier_expired(struct net_bridge
*br
,
793 struct bridge_mcast_own_query
*query
)
795 spin_lock(&br
->multicast_lock
);
796 if (!netif_running(br
->dev
) || br
->multicast_disabled
)
799 br_multicast_start_querier(br
, query
);
802 spin_unlock(&br
->multicast_lock
);
805 static void br_ip4_multicast_querier_expired(unsigned long data
)
807 struct net_bridge
*br
= (void *)data
;
809 br_multicast_querier_expired(br
, &br
->ip4_own_query
);
812 #if IS_ENABLED(CONFIG_IPV6)
813 static void br_ip6_multicast_querier_expired(unsigned long data
)
815 struct net_bridge
*br
= (void *)data
;
817 br_multicast_querier_expired(br
, &br
->ip6_own_query
);
821 static void br_multicast_select_own_querier(struct net_bridge
*br
,
825 if (ip
->proto
== htons(ETH_P_IP
))
826 br
->ip4_querier
.addr
.u
.ip4
= ip_hdr(skb
)->saddr
;
827 #if IS_ENABLED(CONFIG_IPV6)
829 br
->ip6_querier
.addr
.u
.ip6
= ipv6_hdr(skb
)->saddr
;
833 static void __br_multicast_send_query(struct net_bridge
*br
,
834 struct net_bridge_port
*port
,
840 skb
= br_multicast_alloc_query(br
, ip
, &igmp_type
);
845 skb
->dev
= port
->dev
;
846 br_multicast_count(br
, port
, skb
, igmp_type
,
848 NF_HOOK(NFPROTO_BRIDGE
, NF_BR_LOCAL_OUT
,
849 dev_net(port
->dev
), NULL
, skb
, NULL
, skb
->dev
,
850 br_dev_queue_push_xmit
);
852 br_multicast_select_own_querier(br
, ip
, skb
);
853 br_multicast_count(br
, port
, skb
, igmp_type
,
859 static void br_multicast_send_query(struct net_bridge
*br
,
860 struct net_bridge_port
*port
,
861 struct bridge_mcast_own_query
*own_query
)
864 struct br_ip br_group
;
865 struct bridge_mcast_other_query
*other_query
= NULL
;
867 if (!netif_running(br
->dev
) || br
->multicast_disabled
||
868 !br
->multicast_querier
)
871 memset(&br_group
.u
, 0, sizeof(br_group
.u
));
873 if (port
? (own_query
== &port
->ip4_own_query
) :
874 (own_query
== &br
->ip4_own_query
)) {
875 other_query
= &br
->ip4_other_query
;
876 br_group
.proto
= htons(ETH_P_IP
);
877 #if IS_ENABLED(CONFIG_IPV6)
879 other_query
= &br
->ip6_other_query
;
880 br_group
.proto
= htons(ETH_P_IPV6
);
884 if (!other_query
|| timer_pending(&other_query
->timer
))
887 __br_multicast_send_query(br
, port
, &br_group
);
890 time
+= own_query
->startup_sent
< br
->multicast_startup_query_count
?
891 br
->multicast_startup_query_interval
:
892 br
->multicast_query_interval
;
893 mod_timer(&own_query
->timer
, time
);
897 br_multicast_port_query_expired(struct net_bridge_port
*port
,
898 struct bridge_mcast_own_query
*query
)
900 struct net_bridge
*br
= port
->br
;
902 spin_lock(&br
->multicast_lock
);
903 if (port
->state
== BR_STATE_DISABLED
||
904 port
->state
== BR_STATE_BLOCKING
)
907 if (query
->startup_sent
< br
->multicast_startup_query_count
)
908 query
->startup_sent
++;
910 br_multicast_send_query(port
->br
, port
, query
);
913 spin_unlock(&br
->multicast_lock
);
916 static void br_ip4_multicast_port_query_expired(unsigned long data
)
918 struct net_bridge_port
*port
= (void *)data
;
920 br_multicast_port_query_expired(port
, &port
->ip4_own_query
);
923 #if IS_ENABLED(CONFIG_IPV6)
924 static void br_ip6_multicast_port_query_expired(unsigned long data
)
926 struct net_bridge_port
*port
= (void *)data
;
928 br_multicast_port_query_expired(port
, &port
->ip6_own_query
);
932 int br_multicast_add_port(struct net_bridge_port
*port
)
934 port
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
936 setup_timer(&port
->multicast_router_timer
, br_multicast_router_expired
,
937 (unsigned long)port
);
938 setup_timer(&port
->ip4_own_query
.timer
,
939 br_ip4_multicast_port_query_expired
, (unsigned long)port
);
940 #if IS_ENABLED(CONFIG_IPV6)
941 setup_timer(&port
->ip6_own_query
.timer
,
942 br_ip6_multicast_port_query_expired
, (unsigned long)port
);
944 port
->mcast_stats
= netdev_alloc_pcpu_stats(struct bridge_mcast_stats
);
945 if (!port
->mcast_stats
)
951 void br_multicast_del_port(struct net_bridge_port
*port
)
953 struct net_bridge
*br
= port
->br
;
954 struct net_bridge_port_group
*pg
;
955 struct hlist_node
*n
;
957 /* Take care of the remaining groups, only perm ones should be left */
958 spin_lock_bh(&br
->multicast_lock
);
959 hlist_for_each_entry_safe(pg
, n
, &port
->mglist
, mglist
)
960 br_multicast_del_pg(br
, pg
);
961 spin_unlock_bh(&br
->multicast_lock
);
962 del_timer_sync(&port
->multicast_router_timer
);
963 free_percpu(port
->mcast_stats
);
966 static void br_multicast_enable(struct bridge_mcast_own_query
*query
)
968 query
->startup_sent
= 0;
970 if (try_to_del_timer_sync(&query
->timer
) >= 0 ||
971 del_timer(&query
->timer
))
972 mod_timer(&query
->timer
, jiffies
);
975 void br_multicast_enable_port(struct net_bridge_port
*port
)
977 struct net_bridge
*br
= port
->br
;
979 spin_lock(&br
->multicast_lock
);
980 if (br
->multicast_disabled
|| !netif_running(br
->dev
))
983 br_multicast_enable(&port
->ip4_own_query
);
984 #if IS_ENABLED(CONFIG_IPV6)
985 br_multicast_enable(&port
->ip6_own_query
);
987 if (port
->multicast_router
== MDB_RTR_TYPE_PERM
&&
988 hlist_unhashed(&port
->rlist
))
989 br_multicast_add_router(br
, port
);
992 spin_unlock(&br
->multicast_lock
);
995 void br_multicast_disable_port(struct net_bridge_port
*port
)
997 struct net_bridge
*br
= port
->br
;
998 struct net_bridge_port_group
*pg
;
999 struct hlist_node
*n
;
1001 spin_lock(&br
->multicast_lock
);
1002 hlist_for_each_entry_safe(pg
, n
, &port
->mglist
, mglist
)
1003 if (!(pg
->flags
& MDB_PG_FLAGS_PERMANENT
))
1004 br_multicast_del_pg(br
, pg
);
1006 if (!hlist_unhashed(&port
->rlist
)) {
1007 hlist_del_init_rcu(&port
->rlist
);
1008 br_rtr_notify(br
->dev
, port
, RTM_DELMDB
);
1009 /* Don't allow timer refresh if disabling */
1010 if (port
->multicast_router
== MDB_RTR_TYPE_TEMP
)
1011 port
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
1013 del_timer(&port
->multicast_router_timer
);
1014 del_timer(&port
->ip4_own_query
.timer
);
1015 #if IS_ENABLED(CONFIG_IPV6)
1016 del_timer(&port
->ip6_own_query
.timer
);
1018 spin_unlock(&br
->multicast_lock
);
1021 static int br_ip4_multicast_igmp3_report(struct net_bridge
*br
,
1022 struct net_bridge_port
*port
,
1023 struct sk_buff
*skb
,
1026 struct igmpv3_report
*ih
;
1027 struct igmpv3_grec
*grec
;
1035 ih
= igmpv3_report_hdr(skb
);
1036 num
= ntohs(ih
->ngrec
);
1037 len
= skb_transport_offset(skb
) + sizeof(*ih
);
1039 for (i
= 0; i
< num
; i
++) {
1040 len
+= sizeof(*grec
);
1041 if (!pskb_may_pull(skb
, len
))
1044 grec
= (void *)(skb
->data
+ len
- sizeof(*grec
));
1045 group
= grec
->grec_mca
;
1046 type
= grec
->grec_type
;
1048 len
+= ntohs(grec
->grec_nsrcs
) * 4;
1049 if (!pskb_may_pull(skb
, len
))
1052 /* We treat this as an IGMPv2 report for now. */
1054 case IGMPV3_MODE_IS_INCLUDE
:
1055 case IGMPV3_MODE_IS_EXCLUDE
:
1056 case IGMPV3_CHANGE_TO_INCLUDE
:
1057 case IGMPV3_CHANGE_TO_EXCLUDE
:
1058 case IGMPV3_ALLOW_NEW_SOURCES
:
1059 case IGMPV3_BLOCK_OLD_SOURCES
:
1066 if ((type
== IGMPV3_CHANGE_TO_INCLUDE
||
1067 type
== IGMPV3_MODE_IS_INCLUDE
) &&
1068 ntohs(grec
->grec_nsrcs
) == 0) {
1069 br_ip4_multicast_leave_group(br
, port
, group
, vid
);
1071 err
= br_ip4_multicast_add_group(br
, port
, group
, vid
);
1080 #if IS_ENABLED(CONFIG_IPV6)
1081 static int br_ip6_multicast_mld2_report(struct net_bridge
*br
,
1082 struct net_bridge_port
*port
,
1083 struct sk_buff
*skb
,
1086 struct icmp6hdr
*icmp6h
;
1087 struct mld2_grec
*grec
;
1093 if (!pskb_may_pull(skb
, sizeof(*icmp6h
)))
1096 icmp6h
= icmp6_hdr(skb
);
1097 num
= ntohs(icmp6h
->icmp6_dataun
.un_data16
[1]);
1098 len
= skb_transport_offset(skb
) + sizeof(*icmp6h
);
1100 for (i
= 0; i
< num
; i
++) {
1101 __be16
*nsrcs
, _nsrcs
;
1103 nsrcs
= skb_header_pointer(skb
,
1104 len
+ offsetof(struct mld2_grec
,
1106 sizeof(_nsrcs
), &_nsrcs
);
1110 if (!pskb_may_pull(skb
,
1111 len
+ sizeof(*grec
) +
1112 sizeof(struct in6_addr
) * ntohs(*nsrcs
)))
1115 grec
= (struct mld2_grec
*)(skb
->data
+ len
);
1116 len
+= sizeof(*grec
) +
1117 sizeof(struct in6_addr
) * ntohs(*nsrcs
);
1119 /* We treat these as MLDv1 reports for now. */
1120 switch (grec
->grec_type
) {
1121 case MLD2_MODE_IS_INCLUDE
:
1122 case MLD2_MODE_IS_EXCLUDE
:
1123 case MLD2_CHANGE_TO_INCLUDE
:
1124 case MLD2_CHANGE_TO_EXCLUDE
:
1125 case MLD2_ALLOW_NEW_SOURCES
:
1126 case MLD2_BLOCK_OLD_SOURCES
:
1133 if ((grec
->grec_type
== MLD2_CHANGE_TO_INCLUDE
||
1134 grec
->grec_type
== MLD2_MODE_IS_INCLUDE
) &&
1135 ntohs(*nsrcs
) == 0) {
1136 br_ip6_multicast_leave_group(br
, port
, &grec
->grec_mca
,
1139 err
= br_ip6_multicast_add_group(br
, port
,
1140 &grec
->grec_mca
, vid
);
1150 static bool br_ip4_multicast_select_querier(struct net_bridge
*br
,
1151 struct net_bridge_port
*port
,
1154 if (!timer_pending(&br
->ip4_own_query
.timer
) &&
1155 !timer_pending(&br
->ip4_other_query
.timer
))
1158 if (!br
->ip4_querier
.addr
.u
.ip4
)
1161 if (ntohl(saddr
) <= ntohl(br
->ip4_querier
.addr
.u
.ip4
))
1167 br
->ip4_querier
.addr
.u
.ip4
= saddr
;
1169 /* update protected by general multicast_lock by caller */
1170 rcu_assign_pointer(br
->ip4_querier
.port
, port
);
1175 #if IS_ENABLED(CONFIG_IPV6)
1176 static bool br_ip6_multicast_select_querier(struct net_bridge
*br
,
1177 struct net_bridge_port
*port
,
1178 struct in6_addr
*saddr
)
1180 if (!timer_pending(&br
->ip6_own_query
.timer
) &&
1181 !timer_pending(&br
->ip6_other_query
.timer
))
1184 if (ipv6_addr_cmp(saddr
, &br
->ip6_querier
.addr
.u
.ip6
) <= 0)
1190 br
->ip6_querier
.addr
.u
.ip6
= *saddr
;
1192 /* update protected by general multicast_lock by caller */
1193 rcu_assign_pointer(br
->ip6_querier
.port
, port
);
1199 static bool br_multicast_select_querier(struct net_bridge
*br
,
1200 struct net_bridge_port
*port
,
1201 struct br_ip
*saddr
)
1203 switch (saddr
->proto
) {
1204 case htons(ETH_P_IP
):
1205 return br_ip4_multicast_select_querier(br
, port
, saddr
->u
.ip4
);
1206 #if IS_ENABLED(CONFIG_IPV6)
1207 case htons(ETH_P_IPV6
):
1208 return br_ip6_multicast_select_querier(br
, port
, &saddr
->u
.ip6
);
1216 br_multicast_update_query_timer(struct net_bridge
*br
,
1217 struct bridge_mcast_other_query
*query
,
1218 unsigned long max_delay
)
1220 if (!timer_pending(&query
->timer
))
1221 query
->delay_time
= jiffies
+ max_delay
;
1223 mod_timer(&query
->timer
, jiffies
+ br
->multicast_querier_interval
);
1227 * Add port to router_list
1228 * list is maintained ordered by pointer value
1229 * and locked by br->multicast_lock and RCU
1231 static void br_multicast_add_router(struct net_bridge
*br
,
1232 struct net_bridge_port
*port
)
1234 struct net_bridge_port
*p
;
1235 struct hlist_node
*slot
= NULL
;
1237 if (!hlist_unhashed(&port
->rlist
))
1240 hlist_for_each_entry(p
, &br
->router_list
, rlist
) {
1241 if ((unsigned long) port
>= (unsigned long) p
)
1247 hlist_add_behind_rcu(&port
->rlist
, slot
);
1249 hlist_add_head_rcu(&port
->rlist
, &br
->router_list
);
1250 br_rtr_notify(br
->dev
, port
, RTM_NEWMDB
);
1253 static void br_multicast_mark_router(struct net_bridge
*br
,
1254 struct net_bridge_port
*port
)
1256 unsigned long now
= jiffies
;
1259 if (br
->multicast_router
== MDB_RTR_TYPE_TEMP_QUERY
)
1260 mod_timer(&br
->multicast_router_timer
,
1261 now
+ br
->multicast_querier_interval
);
1265 if (port
->multicast_router
== MDB_RTR_TYPE_DISABLED
||
1266 port
->multicast_router
== MDB_RTR_TYPE_PERM
)
1269 br_multicast_add_router(br
, port
);
1271 mod_timer(&port
->multicast_router_timer
,
1272 now
+ br
->multicast_querier_interval
);
1275 static void br_multicast_query_received(struct net_bridge
*br
,
1276 struct net_bridge_port
*port
,
1277 struct bridge_mcast_other_query
*query
,
1278 struct br_ip
*saddr
,
1279 unsigned long max_delay
)
1281 if (!br_multicast_select_querier(br
, port
, saddr
))
1284 br_multicast_update_query_timer(br
, query
, max_delay
);
1285 br_multicast_mark_router(br
, port
);
1288 static int br_ip4_multicast_query(struct net_bridge
*br
,
1289 struct net_bridge_port
*port
,
1290 struct sk_buff
*skb
,
1293 const struct iphdr
*iph
= ip_hdr(skb
);
1294 struct igmphdr
*ih
= igmp_hdr(skb
);
1295 struct net_bridge_mdb_entry
*mp
;
1296 struct igmpv3_query
*ih3
;
1297 struct net_bridge_port_group
*p
;
1298 struct net_bridge_port_group __rcu
**pp
;
1300 unsigned long max_delay
;
1301 unsigned long now
= jiffies
;
1302 unsigned int offset
= skb_transport_offset(skb
);
1306 spin_lock(&br
->multicast_lock
);
1307 if (!netif_running(br
->dev
) ||
1308 (port
&& port
->state
== BR_STATE_DISABLED
))
1313 if (skb
->len
== offset
+ sizeof(*ih
)) {
1314 max_delay
= ih
->code
* (HZ
/ IGMP_TIMER_SCALE
);
1317 max_delay
= 10 * HZ
;
1320 } else if (skb
->len
>= offset
+ sizeof(*ih3
)) {
1321 ih3
= igmpv3_query_hdr(skb
);
1325 max_delay
= ih3
->code
?
1326 IGMPV3_MRC(ih3
->code
) * (HZ
/ IGMP_TIMER_SCALE
) : 1;
1332 saddr
.proto
= htons(ETH_P_IP
);
1333 saddr
.u
.ip4
= iph
->saddr
;
1335 br_multicast_query_received(br
, port
, &br
->ip4_other_query
,
1340 mp
= br_mdb_ip4_get(mlock_dereference(br
->mdb
, br
), group
, vid
);
1344 max_delay
*= br
->multicast_last_member_count
;
1347 (timer_pending(&mp
->timer
) ?
1348 time_after(mp
->timer
.expires
, now
+ max_delay
) :
1349 try_to_del_timer_sync(&mp
->timer
) >= 0))
1350 mod_timer(&mp
->timer
, now
+ max_delay
);
1352 for (pp
= &mp
->ports
;
1353 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1355 if (timer_pending(&p
->timer
) ?
1356 time_after(p
->timer
.expires
, now
+ max_delay
) :
1357 try_to_del_timer_sync(&p
->timer
) >= 0)
1358 mod_timer(&p
->timer
, now
+ max_delay
);
1362 spin_unlock(&br
->multicast_lock
);
1366 #if IS_ENABLED(CONFIG_IPV6)
1367 static int br_ip6_multicast_query(struct net_bridge
*br
,
1368 struct net_bridge_port
*port
,
1369 struct sk_buff
*skb
,
1372 const struct ipv6hdr
*ip6h
= ipv6_hdr(skb
);
1373 struct mld_msg
*mld
;
1374 struct net_bridge_mdb_entry
*mp
;
1375 struct mld2_query
*mld2q
;
1376 struct net_bridge_port_group
*p
;
1377 struct net_bridge_port_group __rcu
**pp
;
1379 unsigned long max_delay
;
1380 unsigned long now
= jiffies
;
1381 unsigned int offset
= skb_transport_offset(skb
);
1382 const struct in6_addr
*group
= NULL
;
1383 bool is_general_query
;
1386 spin_lock(&br
->multicast_lock
);
1387 if (!netif_running(br
->dev
) ||
1388 (port
&& port
->state
== BR_STATE_DISABLED
))
1391 if (skb
->len
== offset
+ sizeof(*mld
)) {
1392 if (!pskb_may_pull(skb
, offset
+ sizeof(*mld
))) {
1396 mld
= (struct mld_msg
*) icmp6_hdr(skb
);
1397 max_delay
= msecs_to_jiffies(ntohs(mld
->mld_maxdelay
));
1399 group
= &mld
->mld_mca
;
1401 if (!pskb_may_pull(skb
, offset
+ sizeof(*mld2q
))) {
1405 mld2q
= (struct mld2_query
*)icmp6_hdr(skb
);
1406 if (!mld2q
->mld2q_nsrcs
)
1407 group
= &mld2q
->mld2q_mca
;
1409 max_delay
= max(msecs_to_jiffies(mldv2_mrc(mld2q
)), 1UL);
1412 is_general_query
= group
&& ipv6_addr_any(group
);
1414 if (is_general_query
) {
1415 saddr
.proto
= htons(ETH_P_IPV6
);
1416 saddr
.u
.ip6
= ip6h
->saddr
;
1418 br_multicast_query_received(br
, port
, &br
->ip6_other_query
,
1421 } else if (!group
) {
1425 mp
= br_mdb_ip6_get(mlock_dereference(br
->mdb
, br
), group
, vid
);
1429 max_delay
*= br
->multicast_last_member_count
;
1431 (timer_pending(&mp
->timer
) ?
1432 time_after(mp
->timer
.expires
, now
+ max_delay
) :
1433 try_to_del_timer_sync(&mp
->timer
) >= 0))
1434 mod_timer(&mp
->timer
, now
+ max_delay
);
1436 for (pp
= &mp
->ports
;
1437 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1439 if (timer_pending(&p
->timer
) ?
1440 time_after(p
->timer
.expires
, now
+ max_delay
) :
1441 try_to_del_timer_sync(&p
->timer
) >= 0)
1442 mod_timer(&p
->timer
, now
+ max_delay
);
1446 spin_unlock(&br
->multicast_lock
);
1452 br_multicast_leave_group(struct net_bridge
*br
,
1453 struct net_bridge_port
*port
,
1454 struct br_ip
*group
,
1455 struct bridge_mcast_other_query
*other_query
,
1456 struct bridge_mcast_own_query
*own_query
)
1458 struct net_bridge_mdb_htable
*mdb
;
1459 struct net_bridge_mdb_entry
*mp
;
1460 struct net_bridge_port_group
*p
;
1464 spin_lock(&br
->multicast_lock
);
1465 if (!netif_running(br
->dev
) ||
1466 (port
&& port
->state
== BR_STATE_DISABLED
))
1469 mdb
= mlock_dereference(br
->mdb
, br
);
1470 mp
= br_mdb_ip_get(mdb
, group
);
1474 if (port
&& (port
->flags
& BR_MULTICAST_FAST_LEAVE
)) {
1475 struct net_bridge_port_group __rcu
**pp
;
1477 for (pp
= &mp
->ports
;
1478 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1480 if (p
->port
!= port
)
1483 rcu_assign_pointer(*pp
, p
->next
);
1484 hlist_del_init(&p
->mglist
);
1485 del_timer(&p
->timer
);
1486 call_rcu_bh(&p
->rcu
, br_multicast_free_pg
);
1487 br_mdb_notify(br
->dev
, port
, group
, RTM_DELMDB
,
1490 if (!mp
->ports
&& !mp
->mglist
&&
1491 netif_running(br
->dev
))
1492 mod_timer(&mp
->timer
, jiffies
);
1497 if (timer_pending(&other_query
->timer
))
1500 if (br
->multicast_querier
) {
1501 __br_multicast_send_query(br
, port
, &mp
->addr
);
1503 time
= jiffies
+ br
->multicast_last_member_count
*
1504 br
->multicast_last_member_interval
;
1506 mod_timer(&own_query
->timer
, time
);
1508 for (p
= mlock_dereference(mp
->ports
, br
);
1510 p
= mlock_dereference(p
->next
, br
)) {
1511 if (p
->port
!= port
)
1514 if (!hlist_unhashed(&p
->mglist
) &&
1515 (timer_pending(&p
->timer
) ?
1516 time_after(p
->timer
.expires
, time
) :
1517 try_to_del_timer_sync(&p
->timer
) >= 0)) {
1518 mod_timer(&p
->timer
, time
);
1526 time
= now
+ br
->multicast_last_member_count
*
1527 br
->multicast_last_member_interval
;
1531 (timer_pending(&mp
->timer
) ?
1532 time_after(mp
->timer
.expires
, time
) :
1533 try_to_del_timer_sync(&mp
->timer
) >= 0)) {
1534 mod_timer(&mp
->timer
, time
);
1540 for (p
= mlock_dereference(mp
->ports
, br
);
1542 p
= mlock_dereference(p
->next
, br
)) {
1543 if (p
->port
!= port
)
1546 if (!hlist_unhashed(&p
->mglist
) &&
1547 (timer_pending(&p
->timer
) ?
1548 time_after(p
->timer
.expires
, time
) :
1549 try_to_del_timer_sync(&p
->timer
) >= 0)) {
1550 mod_timer(&p
->timer
, time
);
1556 spin_unlock(&br
->multicast_lock
);
1559 static void br_ip4_multicast_leave_group(struct net_bridge
*br
,
1560 struct net_bridge_port
*port
,
1564 struct br_ip br_group
;
1565 struct bridge_mcast_own_query
*own_query
;
1567 if (ipv4_is_local_multicast(group
))
1570 own_query
= port
? &port
->ip4_own_query
: &br
->ip4_own_query
;
1572 br_group
.u
.ip4
= group
;
1573 br_group
.proto
= htons(ETH_P_IP
);
1576 br_multicast_leave_group(br
, port
, &br_group
, &br
->ip4_other_query
,
1580 #if IS_ENABLED(CONFIG_IPV6)
1581 static void br_ip6_multicast_leave_group(struct net_bridge
*br
,
1582 struct net_bridge_port
*port
,
1583 const struct in6_addr
*group
,
1586 struct br_ip br_group
;
1587 struct bridge_mcast_own_query
*own_query
;
1589 if (ipv6_addr_is_ll_all_nodes(group
))
1592 own_query
= port
? &port
->ip6_own_query
: &br
->ip6_own_query
;
1594 br_group
.u
.ip6
= *group
;
1595 br_group
.proto
= htons(ETH_P_IPV6
);
1598 br_multicast_leave_group(br
, port
, &br_group
, &br
->ip6_other_query
,
1603 static void br_multicast_err_count(const struct net_bridge
*br
,
1604 const struct net_bridge_port
*p
,
1607 struct bridge_mcast_stats __percpu
*stats
;
1608 struct bridge_mcast_stats
*pstats
;
1610 if (!br
->multicast_stats_enabled
)
1614 stats
= p
->mcast_stats
;
1616 stats
= br
->mcast_stats
;
1617 if (WARN_ON(!stats
))
1620 pstats
= this_cpu_ptr(stats
);
1622 u64_stats_update_begin(&pstats
->syncp
);
1624 case htons(ETH_P_IP
):
1625 pstats
->mstats
.igmp_parse_errors
++;
1627 #if IS_ENABLED(CONFIG_IPV6)
1628 case htons(ETH_P_IPV6
):
1629 pstats
->mstats
.mld_parse_errors
++;
1633 u64_stats_update_end(&pstats
->syncp
);
1636 static int br_multicast_ipv4_rcv(struct net_bridge
*br
,
1637 struct net_bridge_port
*port
,
1638 struct sk_buff
*skb
,
1641 struct sk_buff
*skb_trimmed
= NULL
;
1645 err
= ip_mc_check_igmp(skb
, &skb_trimmed
);
1647 if (err
== -ENOMSG
) {
1648 if (!ipv4_is_local_multicast(ip_hdr(skb
)->daddr
))
1649 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1651 } else if (err
< 0) {
1652 br_multicast_err_count(br
, port
, skb
->protocol
);
1657 BR_INPUT_SKB_CB(skb
)->igmp
= ih
->type
;
1660 case IGMP_HOST_MEMBERSHIP_REPORT
:
1661 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1662 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1663 err
= br_ip4_multicast_add_group(br
, port
, ih
->group
, vid
);
1665 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1666 err
= br_ip4_multicast_igmp3_report(br
, port
, skb_trimmed
, vid
);
1668 case IGMP_HOST_MEMBERSHIP_QUERY
:
1669 err
= br_ip4_multicast_query(br
, port
, skb_trimmed
, vid
);
1671 case IGMP_HOST_LEAVE_MESSAGE
:
1672 br_ip4_multicast_leave_group(br
, port
, ih
->group
, vid
);
1676 if (skb_trimmed
&& skb_trimmed
!= skb
)
1677 kfree_skb(skb_trimmed
);
1679 br_multicast_count(br
, port
, skb
, BR_INPUT_SKB_CB(skb
)->igmp
,
1685 #if IS_ENABLED(CONFIG_IPV6)
1686 static int br_multicast_ipv6_rcv(struct net_bridge
*br
,
1687 struct net_bridge_port
*port
,
1688 struct sk_buff
*skb
,
1691 struct sk_buff
*skb_trimmed
= NULL
;
1692 struct mld_msg
*mld
;
1695 err
= ipv6_mc_check_mld(skb
, &skb_trimmed
);
1697 if (err
== -ENOMSG
) {
1698 if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb
)->daddr
))
1699 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1701 } else if (err
< 0) {
1702 br_multicast_err_count(br
, port
, skb
->protocol
);
1706 mld
= (struct mld_msg
*)skb_transport_header(skb
);
1707 BR_INPUT_SKB_CB(skb
)->igmp
= mld
->mld_type
;
1709 switch (mld
->mld_type
) {
1710 case ICMPV6_MGM_REPORT
:
1711 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1712 err
= br_ip6_multicast_add_group(br
, port
, &mld
->mld_mca
, vid
);
1714 case ICMPV6_MLD2_REPORT
:
1715 err
= br_ip6_multicast_mld2_report(br
, port
, skb_trimmed
, vid
);
1717 case ICMPV6_MGM_QUERY
:
1718 err
= br_ip6_multicast_query(br
, port
, skb_trimmed
, vid
);
1720 case ICMPV6_MGM_REDUCTION
:
1721 br_ip6_multicast_leave_group(br
, port
, &mld
->mld_mca
, vid
);
1725 if (skb_trimmed
&& skb_trimmed
!= skb
)
1726 kfree_skb(skb_trimmed
);
1728 br_multicast_count(br
, port
, skb
, BR_INPUT_SKB_CB(skb
)->igmp
,
1735 int br_multicast_rcv(struct net_bridge
*br
, struct net_bridge_port
*port
,
1736 struct sk_buff
*skb
, u16 vid
)
1740 BR_INPUT_SKB_CB(skb
)->igmp
= 0;
1741 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 0;
1743 if (br
->multicast_disabled
)
1746 switch (skb
->protocol
) {
1747 case htons(ETH_P_IP
):
1748 ret
= br_multicast_ipv4_rcv(br
, port
, skb
, vid
);
1750 #if IS_ENABLED(CONFIG_IPV6)
1751 case htons(ETH_P_IPV6
):
1752 ret
= br_multicast_ipv6_rcv(br
, port
, skb
, vid
);
1760 static void br_multicast_query_expired(struct net_bridge
*br
,
1761 struct bridge_mcast_own_query
*query
,
1762 struct bridge_mcast_querier
*querier
)
1764 spin_lock(&br
->multicast_lock
);
1765 if (query
->startup_sent
< br
->multicast_startup_query_count
)
1766 query
->startup_sent
++;
1768 RCU_INIT_POINTER(querier
->port
, NULL
);
1769 br_multicast_send_query(br
, NULL
, query
);
1770 spin_unlock(&br
->multicast_lock
);
1773 static void br_ip4_multicast_query_expired(unsigned long data
)
1775 struct net_bridge
*br
= (void *)data
;
1777 br_multicast_query_expired(br
, &br
->ip4_own_query
, &br
->ip4_querier
);
1780 #if IS_ENABLED(CONFIG_IPV6)
1781 static void br_ip6_multicast_query_expired(unsigned long data
)
1783 struct net_bridge
*br
= (void *)data
;
1785 br_multicast_query_expired(br
, &br
->ip6_own_query
, &br
->ip6_querier
);
1789 void br_multicast_init(struct net_bridge
*br
)
1791 br
->hash_elasticity
= 4;
1794 br
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
1795 br
->multicast_querier
= 0;
1796 br
->multicast_query_use_ifaddr
= 0;
1797 br
->multicast_last_member_count
= 2;
1798 br
->multicast_startup_query_count
= 2;
1800 br
->multicast_last_member_interval
= HZ
;
1801 br
->multicast_query_response_interval
= 10 * HZ
;
1802 br
->multicast_startup_query_interval
= 125 * HZ
/ 4;
1803 br
->multicast_query_interval
= 125 * HZ
;
1804 br
->multicast_querier_interval
= 255 * HZ
;
1805 br
->multicast_membership_interval
= 260 * HZ
;
1807 br
->ip4_other_query
.delay_time
= 0;
1808 br
->ip4_querier
.port
= NULL
;
1809 #if IS_ENABLED(CONFIG_IPV6)
1810 br
->ip6_other_query
.delay_time
= 0;
1811 br
->ip6_querier
.port
= NULL
;
1813 br
->has_ipv6_addr
= 1;
1815 spin_lock_init(&br
->multicast_lock
);
1816 setup_timer(&br
->multicast_router_timer
,
1817 br_multicast_local_router_expired
, 0);
1818 setup_timer(&br
->ip4_other_query
.timer
,
1819 br_ip4_multicast_querier_expired
, (unsigned long)br
);
1820 setup_timer(&br
->ip4_own_query
.timer
, br_ip4_multicast_query_expired
,
1822 #if IS_ENABLED(CONFIG_IPV6)
1823 setup_timer(&br
->ip6_other_query
.timer
,
1824 br_ip6_multicast_querier_expired
, (unsigned long)br
);
1825 setup_timer(&br
->ip6_own_query
.timer
, br_ip6_multicast_query_expired
,
1830 static void __br_multicast_open(struct net_bridge
*br
,
1831 struct bridge_mcast_own_query
*query
)
1833 query
->startup_sent
= 0;
1835 if (br
->multicast_disabled
)
1838 mod_timer(&query
->timer
, jiffies
);
1841 void br_multicast_open(struct net_bridge
*br
)
1843 __br_multicast_open(br
, &br
->ip4_own_query
);
1844 #if IS_ENABLED(CONFIG_IPV6)
1845 __br_multicast_open(br
, &br
->ip6_own_query
);
1849 void br_multicast_stop(struct net_bridge
*br
)
1851 del_timer_sync(&br
->multicast_router_timer
);
1852 del_timer_sync(&br
->ip4_other_query
.timer
);
1853 del_timer_sync(&br
->ip4_own_query
.timer
);
1854 #if IS_ENABLED(CONFIG_IPV6)
1855 del_timer_sync(&br
->ip6_other_query
.timer
);
1856 del_timer_sync(&br
->ip6_own_query
.timer
);
1860 void br_multicast_dev_del(struct net_bridge
*br
)
1862 struct net_bridge_mdb_htable
*mdb
;
1863 struct net_bridge_mdb_entry
*mp
;
1864 struct hlist_node
*n
;
1868 spin_lock_bh(&br
->multicast_lock
);
1869 mdb
= mlock_dereference(br
->mdb
, br
);
1876 for (i
= 0; i
< mdb
->max
; i
++) {
1877 hlist_for_each_entry_safe(mp
, n
, &mdb
->mhash
[i
],
1879 del_timer(&mp
->timer
);
1880 call_rcu_bh(&mp
->rcu
, br_multicast_free_group
);
1885 spin_unlock_bh(&br
->multicast_lock
);
1887 spin_lock_bh(&br
->multicast_lock
);
1892 call_rcu_bh(&mdb
->rcu
, br_mdb_free
);
1895 spin_unlock_bh(&br
->multicast_lock
);
1897 free_percpu(br
->mcast_stats
);
1900 int br_multicast_set_router(struct net_bridge
*br
, unsigned long val
)
1904 spin_lock_bh(&br
->multicast_lock
);
1907 case MDB_RTR_TYPE_DISABLED
:
1908 case MDB_RTR_TYPE_PERM
:
1909 del_timer(&br
->multicast_router_timer
);
1911 case MDB_RTR_TYPE_TEMP_QUERY
:
1912 br
->multicast_router
= val
;
1917 spin_unlock_bh(&br
->multicast_lock
);
1922 static void __del_port_router(struct net_bridge_port
*p
)
1924 if (hlist_unhashed(&p
->rlist
))
1926 hlist_del_init_rcu(&p
->rlist
);
1927 br_rtr_notify(p
->br
->dev
, p
, RTM_DELMDB
);
1930 int br_multicast_set_port_router(struct net_bridge_port
*p
, unsigned long val
)
1932 struct net_bridge
*br
= p
->br
;
1933 unsigned long now
= jiffies
;
1936 spin_lock(&br
->multicast_lock
);
1937 if (p
->multicast_router
== val
) {
1938 /* Refresh the temp router port timer */
1939 if (p
->multicast_router
== MDB_RTR_TYPE_TEMP
)
1940 mod_timer(&p
->multicast_router_timer
,
1941 now
+ br
->multicast_querier_interval
);
1946 case MDB_RTR_TYPE_DISABLED
:
1947 p
->multicast_router
= MDB_RTR_TYPE_DISABLED
;
1948 __del_port_router(p
);
1949 del_timer(&p
->multicast_router_timer
);
1951 case MDB_RTR_TYPE_TEMP_QUERY
:
1952 p
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
1953 __del_port_router(p
);
1955 case MDB_RTR_TYPE_PERM
:
1956 p
->multicast_router
= MDB_RTR_TYPE_PERM
;
1957 del_timer(&p
->multicast_router_timer
);
1958 br_multicast_add_router(br
, p
);
1960 case MDB_RTR_TYPE_TEMP
:
1961 p
->multicast_router
= MDB_RTR_TYPE_TEMP
;
1962 br_multicast_mark_router(br
, p
);
1969 spin_unlock(&br
->multicast_lock
);
1974 static void br_multicast_start_querier(struct net_bridge
*br
,
1975 struct bridge_mcast_own_query
*query
)
1977 struct net_bridge_port
*port
;
1979 __br_multicast_open(br
, query
);
1981 list_for_each_entry(port
, &br
->port_list
, list
) {
1982 if (port
->state
== BR_STATE_DISABLED
||
1983 port
->state
== BR_STATE_BLOCKING
)
1986 if (query
== &br
->ip4_own_query
)
1987 br_multicast_enable(&port
->ip4_own_query
);
1988 #if IS_ENABLED(CONFIG_IPV6)
1990 br_multicast_enable(&port
->ip6_own_query
);
1995 int br_multicast_toggle(struct net_bridge
*br
, unsigned long val
)
1998 struct net_bridge_mdb_htable
*mdb
;
2000 spin_lock_bh(&br
->multicast_lock
);
2001 if (br
->multicast_disabled
== !val
)
2004 br
->multicast_disabled
= !val
;
2005 if (br
->multicast_disabled
)
2008 if (!netif_running(br
->dev
))
2011 mdb
= mlock_dereference(br
->mdb
, br
);
2016 br
->multicast_disabled
= !!val
;
2020 err
= br_mdb_rehash(&br
->mdb
, mdb
->max
,
2021 br
->hash_elasticity
);
2026 br_multicast_start_querier(br
, &br
->ip4_own_query
);
2027 #if IS_ENABLED(CONFIG_IPV6)
2028 br_multicast_start_querier(br
, &br
->ip6_own_query
);
2032 spin_unlock_bh(&br
->multicast_lock
);
2037 int br_multicast_set_querier(struct net_bridge
*br
, unsigned long val
)
2039 unsigned long max_delay
;
2043 spin_lock_bh(&br
->multicast_lock
);
2044 if (br
->multicast_querier
== val
)
2047 br
->multicast_querier
= val
;
2051 max_delay
= br
->multicast_query_response_interval
;
2053 if (!timer_pending(&br
->ip4_other_query
.timer
))
2054 br
->ip4_other_query
.delay_time
= jiffies
+ max_delay
;
2056 br_multicast_start_querier(br
, &br
->ip4_own_query
);
2058 #if IS_ENABLED(CONFIG_IPV6)
2059 if (!timer_pending(&br
->ip6_other_query
.timer
))
2060 br
->ip6_other_query
.delay_time
= jiffies
+ max_delay
;
2062 br_multicast_start_querier(br
, &br
->ip6_own_query
);
2066 spin_unlock_bh(&br
->multicast_lock
);
2071 int br_multicast_set_hash_max(struct net_bridge
*br
, unsigned long val
)
2075 struct net_bridge_mdb_htable
*mdb
;
2077 spin_lock_bh(&br
->multicast_lock
);
2078 if (!is_power_of_2(val
))
2081 mdb
= mlock_dereference(br
->mdb
, br
);
2082 if (mdb
&& val
< mdb
->size
)
2098 err
= br_mdb_rehash(&br
->mdb
, br
->hash_max
,
2099 br
->hash_elasticity
);
2105 spin_unlock_bh(&br
->multicast_lock
);
2111 * br_multicast_list_adjacent - Returns snooped multicast addresses
2112 * @dev: The bridge port adjacent to which to retrieve addresses
2113 * @br_ip_list: The list to store found, snooped multicast IP addresses in
2115 * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
2116 * snooping feature on all bridge ports of dev's bridge device, excluding
2117 * the addresses from dev itself.
2119 * Returns the number of items added to br_ip_list.
2122 * - br_ip_list needs to be initialized by caller
2123 * - br_ip_list might contain duplicates in the end
2124 * (needs to be taken care of by caller)
2125 * - br_ip_list needs to be freed by caller
2127 int br_multicast_list_adjacent(struct net_device
*dev
,
2128 struct list_head
*br_ip_list
)
2130 struct net_bridge
*br
;
2131 struct net_bridge_port
*port
;
2132 struct net_bridge_port_group
*group
;
2133 struct br_ip_list
*entry
;
2137 if (!br_ip_list
|| !br_port_exists(dev
))
2140 port
= br_port_get_rcu(dev
);
2141 if (!port
|| !port
->br
)
2146 list_for_each_entry_rcu(port
, &br
->port_list
, list
) {
2147 if (!port
->dev
|| port
->dev
== dev
)
2150 hlist_for_each_entry_rcu(group
, &port
->mglist
, mglist
) {
2151 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
2155 entry
->addr
= group
->addr
;
2156 list_add(&entry
->list
, br_ip_list
);
2165 EXPORT_SYMBOL_GPL(br_multicast_list_adjacent
);
2168 * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2169 * @dev: The bridge port providing the bridge on which to check for a querier
2170 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2172 * Checks whether the given interface has a bridge on top and if so returns
2173 * true if a valid querier exists anywhere on the bridged link layer.
2174 * Otherwise returns false.
2176 bool br_multicast_has_querier_anywhere(struct net_device
*dev
, int proto
)
2178 struct net_bridge
*br
;
2179 struct net_bridge_port
*port
;
2184 if (!br_port_exists(dev
))
2187 port
= br_port_get_rcu(dev
);
2188 if (!port
|| !port
->br
)
2193 memset(ð
, 0, sizeof(eth
));
2194 eth
.h_proto
= htons(proto
);
2196 ret
= br_multicast_querier_exists(br
, ð
);
2202 EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere
);
2205 * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
2206 * @dev: The bridge port adjacent to which to check for a querier
2207 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2209 * Checks whether the given interface has a bridge on top and if so returns
2210 * true if a selected querier is behind one of the other ports of this
2211 * bridge. Otherwise returns false.
2213 bool br_multicast_has_querier_adjacent(struct net_device
*dev
, int proto
)
2215 struct net_bridge
*br
;
2216 struct net_bridge_port
*port
;
2220 if (!br_port_exists(dev
))
2223 port
= br_port_get_rcu(dev
);
2224 if (!port
|| !port
->br
)
2231 if (!timer_pending(&br
->ip4_other_query
.timer
) ||
2232 rcu_dereference(br
->ip4_querier
.port
) == port
)
2235 #if IS_ENABLED(CONFIG_IPV6)
2237 if (!timer_pending(&br
->ip6_other_query
.timer
) ||
2238 rcu_dereference(br
->ip6_querier
.port
) == port
)
2251 EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent
);
2253 static void br_mcast_stats_add(struct bridge_mcast_stats __percpu
*stats
,
2254 const struct sk_buff
*skb
, u8 type
, u8 dir
)
2256 struct bridge_mcast_stats
*pstats
= this_cpu_ptr(stats
);
2257 __be16 proto
= skb
->protocol
;
2260 u64_stats_update_begin(&pstats
->syncp
);
2262 case htons(ETH_P_IP
):
2263 t_len
= ntohs(ip_hdr(skb
)->tot_len
) - ip_hdrlen(skb
);
2265 case IGMP_HOST_MEMBERSHIP_REPORT
:
2266 pstats
->mstats
.igmp_v1reports
[dir
]++;
2268 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
2269 pstats
->mstats
.igmp_v2reports
[dir
]++;
2271 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
2272 pstats
->mstats
.igmp_v3reports
[dir
]++;
2274 case IGMP_HOST_MEMBERSHIP_QUERY
:
2275 if (t_len
!= sizeof(struct igmphdr
)) {
2276 pstats
->mstats
.igmp_v3queries
[dir
]++;
2278 unsigned int offset
= skb_transport_offset(skb
);
2279 struct igmphdr
*ih
, _ihdr
;
2281 ih
= skb_header_pointer(skb
, offset
,
2282 sizeof(_ihdr
), &_ihdr
);
2286 pstats
->mstats
.igmp_v1queries
[dir
]++;
2288 pstats
->mstats
.igmp_v2queries
[dir
]++;
2291 case IGMP_HOST_LEAVE_MESSAGE
:
2292 pstats
->mstats
.igmp_leaves
[dir
]++;
2296 #if IS_ENABLED(CONFIG_IPV6)
2297 case htons(ETH_P_IPV6
):
2298 t_len
= ntohs(ipv6_hdr(skb
)->payload_len
) +
2299 sizeof(struct ipv6hdr
);
2300 t_len
-= skb_network_header_len(skb
);
2302 case ICMPV6_MGM_REPORT
:
2303 pstats
->mstats
.mld_v1reports
[dir
]++;
2305 case ICMPV6_MLD2_REPORT
:
2306 pstats
->mstats
.mld_v2reports
[dir
]++;
2308 case ICMPV6_MGM_QUERY
:
2309 if (t_len
!= sizeof(struct mld_msg
))
2310 pstats
->mstats
.mld_v2queries
[dir
]++;
2312 pstats
->mstats
.mld_v1queries
[dir
]++;
2314 case ICMPV6_MGM_REDUCTION
:
2315 pstats
->mstats
.mld_leaves
[dir
]++;
2319 #endif /* CONFIG_IPV6 */
2321 u64_stats_update_end(&pstats
->syncp
);
2324 void br_multicast_count(struct net_bridge
*br
, const struct net_bridge_port
*p
,
2325 const struct sk_buff
*skb
, u8 type
, u8 dir
)
2327 struct bridge_mcast_stats __percpu
*stats
;
2329 /* if multicast_disabled is true then igmp type can't be set */
2330 if (!type
|| !br
->multicast_stats_enabled
)
2334 stats
= p
->mcast_stats
;
2336 stats
= br
->mcast_stats
;
2337 if (WARN_ON(!stats
))
2340 br_mcast_stats_add(stats
, skb
, type
, dir
);
2343 int br_multicast_init_stats(struct net_bridge
*br
)
2345 br
->mcast_stats
= netdev_alloc_pcpu_stats(struct bridge_mcast_stats
);
2346 if (!br
->mcast_stats
)
2352 static void mcast_stats_add_dir(u64
*dst
, u64
*src
)
2354 dst
[BR_MCAST_DIR_RX
] += src
[BR_MCAST_DIR_RX
];
2355 dst
[BR_MCAST_DIR_TX
] += src
[BR_MCAST_DIR_TX
];
2358 void br_multicast_get_stats(const struct net_bridge
*br
,
2359 const struct net_bridge_port
*p
,
2360 struct br_mcast_stats
*dest
)
2362 struct bridge_mcast_stats __percpu
*stats
;
2363 struct br_mcast_stats tdst
;
2366 memset(dest
, 0, sizeof(*dest
));
2368 stats
= p
->mcast_stats
;
2370 stats
= br
->mcast_stats
;
2371 if (WARN_ON(!stats
))
2374 memset(&tdst
, 0, sizeof(tdst
));
2375 for_each_possible_cpu(i
) {
2376 struct bridge_mcast_stats
*cpu_stats
= per_cpu_ptr(stats
, i
);
2377 struct br_mcast_stats temp
;
2381 start
= u64_stats_fetch_begin_irq(&cpu_stats
->syncp
);
2382 memcpy(&temp
, &cpu_stats
->mstats
, sizeof(temp
));
2383 } while (u64_stats_fetch_retry_irq(&cpu_stats
->syncp
, start
));
2385 mcast_stats_add_dir(tdst
.igmp_v1queries
, temp
.igmp_v1queries
);
2386 mcast_stats_add_dir(tdst
.igmp_v2queries
, temp
.igmp_v2queries
);
2387 mcast_stats_add_dir(tdst
.igmp_v3queries
, temp
.igmp_v3queries
);
2388 mcast_stats_add_dir(tdst
.igmp_leaves
, temp
.igmp_leaves
);
2389 mcast_stats_add_dir(tdst
.igmp_v1reports
, temp
.igmp_v1reports
);
2390 mcast_stats_add_dir(tdst
.igmp_v2reports
, temp
.igmp_v2reports
);
2391 mcast_stats_add_dir(tdst
.igmp_v3reports
, temp
.igmp_v3reports
);
2392 tdst
.igmp_parse_errors
+= temp
.igmp_parse_errors
;
2394 mcast_stats_add_dir(tdst
.mld_v1queries
, temp
.mld_v1queries
);
2395 mcast_stats_add_dir(tdst
.mld_v2queries
, temp
.mld_v2queries
);
2396 mcast_stats_add_dir(tdst
.mld_leaves
, temp
.mld_leaves
);
2397 mcast_stats_add_dir(tdst
.mld_v1reports
, temp
.mld_v1reports
);
2398 mcast_stats_add_dir(tdst
.mld_v2reports
, temp
.mld_v2reports
);
2399 tdst
.mld_parse_errors
+= temp
.mld_parse_errors
;
2401 memcpy(dest
, &tdst
, sizeof(*dest
));