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
, RTM_DELMDB
);
287 call_rcu_bh(&p
->rcu
, br_multicast_free_pg
);
289 if (!mp
->ports
&& !mp
->mglist
&&
290 netif_running(br
->dev
))
291 mod_timer(&mp
->timer
, jiffies
);
299 static void br_multicast_port_group_expired(unsigned long data
)
301 struct net_bridge_port_group
*pg
= (void *)data
;
302 struct net_bridge
*br
= pg
->port
->br
;
304 spin_lock(&br
->multicast_lock
);
305 if (!netif_running(br
->dev
) || timer_pending(&pg
->timer
) ||
306 hlist_unhashed(&pg
->mglist
) || pg
->flags
& MDB_PG_FLAGS_PERMANENT
)
309 br_multicast_del_pg(br
, pg
);
312 spin_unlock(&br
->multicast_lock
);
315 static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu
**mdbp
, int max
,
318 struct net_bridge_mdb_htable
*old
= rcu_dereference_protected(*mdbp
, 1);
319 struct net_bridge_mdb_htable
*mdb
;
322 mdb
= kmalloc(sizeof(*mdb
), GFP_ATOMIC
);
329 mdb
->mhash
= kzalloc(max
* sizeof(*mdb
->mhash
), GFP_ATOMIC
);
335 mdb
->size
= old
? old
->size
: 0;
336 mdb
->ver
= old
? old
->ver
^ 1 : 0;
338 if (!old
|| elasticity
)
339 get_random_bytes(&mdb
->secret
, sizeof(mdb
->secret
));
341 mdb
->secret
= old
->secret
;
346 err
= br_mdb_copy(mdb
, old
, elasticity
);
354 call_rcu_bh(&mdb
->rcu
, br_mdb_free
);
357 rcu_assign_pointer(*mdbp
, mdb
);
362 static struct sk_buff
*br_ip4_multicast_alloc_query(struct net_bridge
*br
,
370 skb
= netdev_alloc_skb_ip_align(br
->dev
, sizeof(*eth
) + sizeof(*iph
) +
375 skb
->protocol
= htons(ETH_P_IP
);
377 skb_reset_mac_header(skb
);
380 ether_addr_copy(eth
->h_source
, br
->dev
->dev_addr
);
383 eth
->h_dest
[2] = 0x5e;
387 eth
->h_proto
= htons(ETH_P_IP
);
388 skb_put(skb
, sizeof(*eth
));
390 skb_set_network_header(skb
, skb
->len
);
396 iph
->tot_len
= htons(sizeof(*iph
) + sizeof(*ih
) + 4);
398 iph
->frag_off
= htons(IP_DF
);
400 iph
->protocol
= IPPROTO_IGMP
;
401 iph
->saddr
= br
->multicast_query_use_ifaddr
?
402 inet_select_addr(br
->dev
, 0, RT_SCOPE_LINK
) : 0;
403 iph
->daddr
= htonl(INADDR_ALLHOSTS_GROUP
);
404 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
405 ((u8
*)&iph
[1])[1] = 4;
406 ((u8
*)&iph
[1])[2] = 0;
407 ((u8
*)&iph
[1])[3] = 0;
411 skb_set_transport_header(skb
, skb
->len
);
413 ih
->type
= IGMP_HOST_MEMBERSHIP_QUERY
;
414 ih
->code
= (group
? br
->multicast_last_member_interval
:
415 br
->multicast_query_response_interval
) /
416 (HZ
/ IGMP_TIMER_SCALE
);
419 ih
->csum
= ip_compute_csum((void *)ih
, sizeof(struct igmphdr
));
420 skb_put(skb
, sizeof(*ih
));
422 __skb_pull(skb
, sizeof(*eth
));
428 #if IS_ENABLED(CONFIG_IPV6)
429 static struct sk_buff
*br_ip6_multicast_alloc_query(struct net_bridge
*br
,
430 const struct in6_addr
*group
)
433 struct ipv6hdr
*ip6h
;
434 struct mld_msg
*mldq
;
437 unsigned long interval
;
439 skb
= netdev_alloc_skb_ip_align(br
->dev
, sizeof(*eth
) + sizeof(*ip6h
) +
444 skb
->protocol
= htons(ETH_P_IPV6
);
446 /* Ethernet header */
447 skb_reset_mac_header(skb
);
450 ether_addr_copy(eth
->h_source
, br
->dev
->dev_addr
);
451 eth
->h_proto
= htons(ETH_P_IPV6
);
452 skb_put(skb
, sizeof(*eth
));
454 /* IPv6 header + HbH option */
455 skb_set_network_header(skb
, skb
->len
);
456 ip6h
= ipv6_hdr(skb
);
458 *(__force __be32
*)ip6h
= htonl(0x60000000);
459 ip6h
->payload_len
= htons(8 + sizeof(*mldq
));
460 ip6h
->nexthdr
= IPPROTO_HOPOPTS
;
462 ipv6_addr_set(&ip6h
->daddr
, htonl(0xff020000), 0, 0, htonl(1));
463 if (ipv6_dev_get_saddr(dev_net(br
->dev
), br
->dev
, &ip6h
->daddr
, 0,
468 ipv6_eth_mc_map(&ip6h
->daddr
, eth
->h_dest
);
470 hopopt
= (u8
*)(ip6h
+ 1);
471 hopopt
[0] = IPPROTO_ICMPV6
; /* next hdr */
472 hopopt
[1] = 0; /* length of HbH */
473 hopopt
[2] = IPV6_TLV_ROUTERALERT
; /* Router Alert */
474 hopopt
[3] = 2; /* Length of RA Option */
475 hopopt
[4] = 0; /* Type = 0x0000 (MLD) */
477 hopopt
[6] = IPV6_TLV_PAD1
; /* Pad1 */
478 hopopt
[7] = IPV6_TLV_PAD1
; /* Pad1 */
480 skb_put(skb
, sizeof(*ip6h
) + 8);
483 skb_set_transport_header(skb
, skb
->len
);
484 mldq
= (struct mld_msg
*) icmp6_hdr(skb
);
486 interval
= ipv6_addr_any(group
) ?
487 br
->multicast_query_response_interval
:
488 br
->multicast_last_member_interval
;
490 mldq
->mld_type
= ICMPV6_MGM_QUERY
;
493 mldq
->mld_maxdelay
= htons((u16
)jiffies_to_msecs(interval
));
494 mldq
->mld_reserved
= 0;
495 mldq
->mld_mca
= *group
;
498 mldq
->mld_cksum
= csum_ipv6_magic(&ip6h
->saddr
, &ip6h
->daddr
,
499 sizeof(*mldq
), IPPROTO_ICMPV6
,
502 skb_put(skb
, sizeof(*mldq
));
504 __skb_pull(skb
, sizeof(*eth
));
511 static struct sk_buff
*br_multicast_alloc_query(struct net_bridge
*br
,
514 switch (addr
->proto
) {
515 case htons(ETH_P_IP
):
516 return br_ip4_multicast_alloc_query(br
, addr
->u
.ip4
);
517 #if IS_ENABLED(CONFIG_IPV6)
518 case htons(ETH_P_IPV6
):
519 return br_ip6_multicast_alloc_query(br
, &addr
->u
.ip6
);
525 static struct net_bridge_mdb_entry
*br_multicast_get_group(
526 struct net_bridge
*br
, struct net_bridge_port
*port
,
527 struct br_ip
*group
, int hash
)
529 struct net_bridge_mdb_htable
*mdb
;
530 struct net_bridge_mdb_entry
*mp
;
531 unsigned int count
= 0;
536 mdb
= rcu_dereference_protected(br
->mdb
, 1);
537 hlist_for_each_entry(mp
, &mdb
->mhash
[hash
], hlist
[mdb
->ver
]) {
539 if (unlikely(br_ip_equal(group
, &mp
->addr
)))
546 if (unlikely(count
> br
->hash_elasticity
&& count
)) {
548 br_info(br
, "Multicast hash table "
549 "chain limit reached: %s\n",
550 port
? port
->dev
->name
: br
->dev
->name
);
552 elasticity
= br
->hash_elasticity
;
555 if (mdb
->size
>= max
) {
557 if (unlikely(max
> br
->hash_max
)) {
558 br_warn(br
, "Multicast hash table maximum of %d "
559 "reached, disabling snooping: %s\n",
561 port
? port
->dev
->name
: br
->dev
->name
);
564 br
->multicast_disabled
= 1;
569 if (max
> mdb
->max
|| elasticity
) {
572 br_info(br
, "Multicast hash table "
574 port
? port
->dev
->name
: br
->dev
->name
);
579 err
= br_mdb_rehash(&br
->mdb
, max
, elasticity
);
581 br_warn(br
, "Cannot rehash multicast "
582 "hash table, disabling snooping: %s, %d, %d\n",
583 port
? port
->dev
->name
: br
->dev
->name
,
599 struct net_bridge_mdb_entry
*br_multicast_new_group(struct net_bridge
*br
,
600 struct net_bridge_port
*port
, struct br_ip
*group
)
602 struct net_bridge_mdb_htable
*mdb
;
603 struct net_bridge_mdb_entry
*mp
;
607 mdb
= rcu_dereference_protected(br
->mdb
, 1);
609 err
= br_mdb_rehash(&br
->mdb
, BR_HASH_SIZE
, 0);
615 hash
= br_ip_hash(mdb
, group
);
616 mp
= br_multicast_get_group(br
, port
, group
, hash
);
617 switch (PTR_ERR(mp
)) {
623 mdb
= rcu_dereference_protected(br
->mdb
, 1);
624 hash
= br_ip_hash(mdb
, group
);
631 mp
= kzalloc(sizeof(*mp
), GFP_ATOMIC
);
633 return ERR_PTR(-ENOMEM
);
637 setup_timer(&mp
->timer
, br_multicast_group_expired
,
640 hlist_add_head_rcu(&mp
->hlist
[mdb
->ver
], &mdb
->mhash
[hash
]);
647 struct net_bridge_port_group
*br_multicast_new_port_group(
648 struct net_bridge_port
*port
,
650 struct net_bridge_port_group __rcu
*next
,
653 struct net_bridge_port_group
*p
;
655 p
= kzalloc(sizeof(*p
), GFP_ATOMIC
);
662 rcu_assign_pointer(p
->next
, next
);
663 hlist_add_head(&p
->mglist
, &port
->mglist
);
664 setup_timer(&p
->timer
, br_multicast_port_group_expired
,
669 static int br_multicast_add_group(struct net_bridge
*br
,
670 struct net_bridge_port
*port
,
673 struct net_bridge_mdb_entry
*mp
;
674 struct net_bridge_port_group
*p
;
675 struct net_bridge_port_group __rcu
**pp
;
676 unsigned long now
= jiffies
;
679 spin_lock(&br
->multicast_lock
);
680 if (!netif_running(br
->dev
) ||
681 (port
&& port
->state
== BR_STATE_DISABLED
))
684 mp
= br_multicast_new_group(br
, port
, group
);
691 mod_timer(&mp
->timer
, now
+ br
->multicast_membership_interval
);
695 for (pp
= &mp
->ports
;
696 (p
= mlock_dereference(*pp
, br
)) != NULL
;
700 if ((unsigned long)p
->port
< (unsigned long)port
)
704 p
= br_multicast_new_port_group(port
, group
, *pp
, 0);
707 rcu_assign_pointer(*pp
, p
);
708 br_mdb_notify(br
->dev
, p
, RTM_NEWMDB
);
711 mod_timer(&p
->timer
, now
+ br
->multicast_membership_interval
);
716 spin_unlock(&br
->multicast_lock
);
720 static int br_ip4_multicast_add_group(struct net_bridge
*br
,
721 struct net_bridge_port
*port
,
725 struct br_ip br_group
;
727 if (ipv4_is_local_multicast(group
))
730 br_group
.u
.ip4
= group
;
731 br_group
.proto
= htons(ETH_P_IP
);
734 return br_multicast_add_group(br
, port
, &br_group
);
737 #if IS_ENABLED(CONFIG_IPV6)
738 static int br_ip6_multicast_add_group(struct net_bridge
*br
,
739 struct net_bridge_port
*port
,
740 const struct in6_addr
*group
,
743 struct br_ip br_group
;
745 if (ipv6_addr_is_ll_all_nodes(group
))
748 br_group
.u
.ip6
= *group
;
749 br_group
.proto
= htons(ETH_P_IPV6
);
752 return br_multicast_add_group(br
, port
, &br_group
);
756 static void br_multicast_router_expired(unsigned long data
)
758 struct net_bridge_port
*port
= (void *)data
;
759 struct net_bridge
*br
= port
->br
;
761 spin_lock(&br
->multicast_lock
);
762 if (port
->multicast_router
== MDB_RTR_TYPE_DISABLED
||
763 port
->multicast_router
== MDB_RTR_TYPE_PERM
||
764 timer_pending(&port
->multicast_router_timer
) ||
765 hlist_unhashed(&port
->rlist
))
768 hlist_del_init_rcu(&port
->rlist
);
769 br_rtr_notify(br
->dev
, port
, RTM_DELMDB
);
770 /* Don't allow timer refresh if the router expired */
771 if (port
->multicast_router
== MDB_RTR_TYPE_TEMP
)
772 port
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
775 spin_unlock(&br
->multicast_lock
);
778 static void br_multicast_local_router_expired(unsigned long data
)
782 static void br_multicast_querier_expired(struct net_bridge
*br
,
783 struct bridge_mcast_own_query
*query
)
785 spin_lock(&br
->multicast_lock
);
786 if (!netif_running(br
->dev
) || br
->multicast_disabled
)
789 br_multicast_start_querier(br
, query
);
792 spin_unlock(&br
->multicast_lock
);
795 static void br_ip4_multicast_querier_expired(unsigned long data
)
797 struct net_bridge
*br
= (void *)data
;
799 br_multicast_querier_expired(br
, &br
->ip4_own_query
);
802 #if IS_ENABLED(CONFIG_IPV6)
803 static void br_ip6_multicast_querier_expired(unsigned long data
)
805 struct net_bridge
*br
= (void *)data
;
807 br_multicast_querier_expired(br
, &br
->ip6_own_query
);
811 static void br_multicast_select_own_querier(struct net_bridge
*br
,
815 if (ip
->proto
== htons(ETH_P_IP
))
816 br
->ip4_querier
.addr
.u
.ip4
= ip_hdr(skb
)->saddr
;
817 #if IS_ENABLED(CONFIG_IPV6)
819 br
->ip6_querier
.addr
.u
.ip6
= ipv6_hdr(skb
)->saddr
;
823 static void __br_multicast_send_query(struct net_bridge
*br
,
824 struct net_bridge_port
*port
,
829 skb
= br_multicast_alloc_query(br
, ip
);
834 skb
->dev
= port
->dev
;
835 NF_HOOK(NFPROTO_BRIDGE
, NF_BR_LOCAL_OUT
,
836 dev_net(port
->dev
), NULL
, skb
, NULL
, skb
->dev
,
837 br_dev_queue_push_xmit
);
839 br_multicast_select_own_querier(br
, ip
, skb
);
844 static void br_multicast_send_query(struct net_bridge
*br
,
845 struct net_bridge_port
*port
,
846 struct bridge_mcast_own_query
*own_query
)
849 struct br_ip br_group
;
850 struct bridge_mcast_other_query
*other_query
= NULL
;
852 if (!netif_running(br
->dev
) || br
->multicast_disabled
||
853 !br
->multicast_querier
)
856 memset(&br_group
.u
, 0, sizeof(br_group
.u
));
858 if (port
? (own_query
== &port
->ip4_own_query
) :
859 (own_query
== &br
->ip4_own_query
)) {
860 other_query
= &br
->ip4_other_query
;
861 br_group
.proto
= htons(ETH_P_IP
);
862 #if IS_ENABLED(CONFIG_IPV6)
864 other_query
= &br
->ip6_other_query
;
865 br_group
.proto
= htons(ETH_P_IPV6
);
869 if (!other_query
|| timer_pending(&other_query
->timer
))
872 __br_multicast_send_query(br
, port
, &br_group
);
875 time
+= own_query
->startup_sent
< br
->multicast_startup_query_count
?
876 br
->multicast_startup_query_interval
:
877 br
->multicast_query_interval
;
878 mod_timer(&own_query
->timer
, time
);
882 br_multicast_port_query_expired(struct net_bridge_port
*port
,
883 struct bridge_mcast_own_query
*query
)
885 struct net_bridge
*br
= port
->br
;
887 spin_lock(&br
->multicast_lock
);
888 if (port
->state
== BR_STATE_DISABLED
||
889 port
->state
== BR_STATE_BLOCKING
)
892 if (query
->startup_sent
< br
->multicast_startup_query_count
)
893 query
->startup_sent
++;
895 br_multicast_send_query(port
->br
, port
, query
);
898 spin_unlock(&br
->multicast_lock
);
901 static void br_ip4_multicast_port_query_expired(unsigned long data
)
903 struct net_bridge_port
*port
= (void *)data
;
905 br_multicast_port_query_expired(port
, &port
->ip4_own_query
);
908 #if IS_ENABLED(CONFIG_IPV6)
909 static void br_ip6_multicast_port_query_expired(unsigned long data
)
911 struct net_bridge_port
*port
= (void *)data
;
913 br_multicast_port_query_expired(port
, &port
->ip6_own_query
);
917 void br_multicast_add_port(struct net_bridge_port
*port
)
919 port
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
921 setup_timer(&port
->multicast_router_timer
, br_multicast_router_expired
,
922 (unsigned long)port
);
923 setup_timer(&port
->ip4_own_query
.timer
,
924 br_ip4_multicast_port_query_expired
, (unsigned long)port
);
925 #if IS_ENABLED(CONFIG_IPV6)
926 setup_timer(&port
->ip6_own_query
.timer
,
927 br_ip6_multicast_port_query_expired
, (unsigned long)port
);
931 void br_multicast_del_port(struct net_bridge_port
*port
)
933 struct net_bridge
*br
= port
->br
;
934 struct net_bridge_port_group
*pg
;
935 struct hlist_node
*n
;
937 /* Take care of the remaining groups, only perm ones should be left */
938 spin_lock_bh(&br
->multicast_lock
);
939 hlist_for_each_entry_safe(pg
, n
, &port
->mglist
, mglist
)
940 br_multicast_del_pg(br
, pg
);
941 spin_unlock_bh(&br
->multicast_lock
);
942 del_timer_sync(&port
->multicast_router_timer
);
945 static void br_multicast_enable(struct bridge_mcast_own_query
*query
)
947 query
->startup_sent
= 0;
949 if (try_to_del_timer_sync(&query
->timer
) >= 0 ||
950 del_timer(&query
->timer
))
951 mod_timer(&query
->timer
, jiffies
);
954 void br_multicast_enable_port(struct net_bridge_port
*port
)
956 struct net_bridge
*br
= port
->br
;
958 spin_lock(&br
->multicast_lock
);
959 if (br
->multicast_disabled
|| !netif_running(br
->dev
))
962 br_multicast_enable(&port
->ip4_own_query
);
963 #if IS_ENABLED(CONFIG_IPV6)
964 br_multicast_enable(&port
->ip6_own_query
);
966 if (port
->multicast_router
== MDB_RTR_TYPE_PERM
&&
967 hlist_unhashed(&port
->rlist
))
968 br_multicast_add_router(br
, port
);
971 spin_unlock(&br
->multicast_lock
);
974 void br_multicast_disable_port(struct net_bridge_port
*port
)
976 struct net_bridge
*br
= port
->br
;
977 struct net_bridge_port_group
*pg
;
978 struct hlist_node
*n
;
980 spin_lock(&br
->multicast_lock
);
981 hlist_for_each_entry_safe(pg
, n
, &port
->mglist
, mglist
)
982 if (!(pg
->flags
& MDB_PG_FLAGS_PERMANENT
))
983 br_multicast_del_pg(br
, pg
);
985 if (!hlist_unhashed(&port
->rlist
)) {
986 hlist_del_init_rcu(&port
->rlist
);
987 br_rtr_notify(br
->dev
, port
, RTM_DELMDB
);
988 /* Don't allow timer refresh if disabling */
989 if (port
->multicast_router
== MDB_RTR_TYPE_TEMP
)
990 port
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
992 del_timer(&port
->multicast_router_timer
);
993 del_timer(&port
->ip4_own_query
.timer
);
994 #if IS_ENABLED(CONFIG_IPV6)
995 del_timer(&port
->ip6_own_query
.timer
);
997 spin_unlock(&br
->multicast_lock
);
1000 static int br_ip4_multicast_igmp3_report(struct net_bridge
*br
,
1001 struct net_bridge_port
*port
,
1002 struct sk_buff
*skb
,
1005 struct igmpv3_report
*ih
;
1006 struct igmpv3_grec
*grec
;
1014 ih
= igmpv3_report_hdr(skb
);
1015 num
= ntohs(ih
->ngrec
);
1016 len
= skb_transport_offset(skb
) + sizeof(*ih
);
1018 for (i
= 0; i
< num
; i
++) {
1019 len
+= sizeof(*grec
);
1020 if (!pskb_may_pull(skb
, len
))
1023 grec
= (void *)(skb
->data
+ len
- sizeof(*grec
));
1024 group
= grec
->grec_mca
;
1025 type
= grec
->grec_type
;
1027 len
+= ntohs(grec
->grec_nsrcs
) * 4;
1028 if (!pskb_may_pull(skb
, len
))
1031 /* We treat this as an IGMPv2 report for now. */
1033 case IGMPV3_MODE_IS_INCLUDE
:
1034 case IGMPV3_MODE_IS_EXCLUDE
:
1035 case IGMPV3_CHANGE_TO_INCLUDE
:
1036 case IGMPV3_CHANGE_TO_EXCLUDE
:
1037 case IGMPV3_ALLOW_NEW_SOURCES
:
1038 case IGMPV3_BLOCK_OLD_SOURCES
:
1045 if ((type
== IGMPV3_CHANGE_TO_INCLUDE
||
1046 type
== IGMPV3_MODE_IS_INCLUDE
) &&
1047 ntohs(grec
->grec_nsrcs
) == 0) {
1048 br_ip4_multicast_leave_group(br
, port
, group
, vid
);
1050 err
= br_ip4_multicast_add_group(br
, port
, group
, vid
);
1059 #if IS_ENABLED(CONFIG_IPV6)
1060 static int br_ip6_multicast_mld2_report(struct net_bridge
*br
,
1061 struct net_bridge_port
*port
,
1062 struct sk_buff
*skb
,
1065 struct icmp6hdr
*icmp6h
;
1066 struct mld2_grec
*grec
;
1072 if (!pskb_may_pull(skb
, sizeof(*icmp6h
)))
1075 icmp6h
= icmp6_hdr(skb
);
1076 num
= ntohs(icmp6h
->icmp6_dataun
.un_data16
[1]);
1077 len
= skb_transport_offset(skb
) + sizeof(*icmp6h
);
1079 for (i
= 0; i
< num
; i
++) {
1080 __be16
*nsrcs
, _nsrcs
;
1082 nsrcs
= skb_header_pointer(skb
,
1083 len
+ offsetof(struct mld2_grec
,
1085 sizeof(_nsrcs
), &_nsrcs
);
1089 if (!pskb_may_pull(skb
,
1090 len
+ sizeof(*grec
) +
1091 sizeof(struct in6_addr
) * ntohs(*nsrcs
)))
1094 grec
= (struct mld2_grec
*)(skb
->data
+ len
);
1095 len
+= sizeof(*grec
) +
1096 sizeof(struct in6_addr
) * ntohs(*nsrcs
);
1098 /* We treat these as MLDv1 reports for now. */
1099 switch (grec
->grec_type
) {
1100 case MLD2_MODE_IS_INCLUDE
:
1101 case MLD2_MODE_IS_EXCLUDE
:
1102 case MLD2_CHANGE_TO_INCLUDE
:
1103 case MLD2_CHANGE_TO_EXCLUDE
:
1104 case MLD2_ALLOW_NEW_SOURCES
:
1105 case MLD2_BLOCK_OLD_SOURCES
:
1112 if ((grec
->grec_type
== MLD2_CHANGE_TO_INCLUDE
||
1113 grec
->grec_type
== MLD2_MODE_IS_INCLUDE
) &&
1114 ntohs(*nsrcs
) == 0) {
1115 br_ip6_multicast_leave_group(br
, port
, &grec
->grec_mca
,
1118 err
= br_ip6_multicast_add_group(br
, port
,
1119 &grec
->grec_mca
, vid
);
1129 static bool br_ip4_multicast_select_querier(struct net_bridge
*br
,
1130 struct net_bridge_port
*port
,
1133 if (!timer_pending(&br
->ip4_own_query
.timer
) &&
1134 !timer_pending(&br
->ip4_other_query
.timer
))
1137 if (!br
->ip4_querier
.addr
.u
.ip4
)
1140 if (ntohl(saddr
) <= ntohl(br
->ip4_querier
.addr
.u
.ip4
))
1146 br
->ip4_querier
.addr
.u
.ip4
= saddr
;
1148 /* update protected by general multicast_lock by caller */
1149 rcu_assign_pointer(br
->ip4_querier
.port
, port
);
1154 #if IS_ENABLED(CONFIG_IPV6)
1155 static bool br_ip6_multicast_select_querier(struct net_bridge
*br
,
1156 struct net_bridge_port
*port
,
1157 struct in6_addr
*saddr
)
1159 if (!timer_pending(&br
->ip6_own_query
.timer
) &&
1160 !timer_pending(&br
->ip6_other_query
.timer
))
1163 if (ipv6_addr_cmp(saddr
, &br
->ip6_querier
.addr
.u
.ip6
) <= 0)
1169 br
->ip6_querier
.addr
.u
.ip6
= *saddr
;
1171 /* update protected by general multicast_lock by caller */
1172 rcu_assign_pointer(br
->ip6_querier
.port
, port
);
1178 static bool br_multicast_select_querier(struct net_bridge
*br
,
1179 struct net_bridge_port
*port
,
1180 struct br_ip
*saddr
)
1182 switch (saddr
->proto
) {
1183 case htons(ETH_P_IP
):
1184 return br_ip4_multicast_select_querier(br
, port
, saddr
->u
.ip4
);
1185 #if IS_ENABLED(CONFIG_IPV6)
1186 case htons(ETH_P_IPV6
):
1187 return br_ip6_multicast_select_querier(br
, port
, &saddr
->u
.ip6
);
1195 br_multicast_update_query_timer(struct net_bridge
*br
,
1196 struct bridge_mcast_other_query
*query
,
1197 unsigned long max_delay
)
1199 if (!timer_pending(&query
->timer
))
1200 query
->delay_time
= jiffies
+ max_delay
;
1202 mod_timer(&query
->timer
, jiffies
+ br
->multicast_querier_interval
);
1206 * Add port to router_list
1207 * list is maintained ordered by pointer value
1208 * and locked by br->multicast_lock and RCU
1210 static void br_multicast_add_router(struct net_bridge
*br
,
1211 struct net_bridge_port
*port
)
1213 struct net_bridge_port
*p
;
1214 struct hlist_node
*slot
= NULL
;
1216 if (!hlist_unhashed(&port
->rlist
))
1219 hlist_for_each_entry(p
, &br
->router_list
, rlist
) {
1220 if ((unsigned long) port
>= (unsigned long) p
)
1226 hlist_add_behind_rcu(&port
->rlist
, slot
);
1228 hlist_add_head_rcu(&port
->rlist
, &br
->router_list
);
1229 br_rtr_notify(br
->dev
, port
, RTM_NEWMDB
);
1232 static void br_multicast_mark_router(struct net_bridge
*br
,
1233 struct net_bridge_port
*port
)
1235 unsigned long now
= jiffies
;
1238 if (br
->multicast_router
== MDB_RTR_TYPE_TEMP_QUERY
)
1239 mod_timer(&br
->multicast_router_timer
,
1240 now
+ br
->multicast_querier_interval
);
1244 if (port
->multicast_router
== MDB_RTR_TYPE_DISABLED
||
1245 port
->multicast_router
== MDB_RTR_TYPE_PERM
)
1248 br_multicast_add_router(br
, port
);
1250 mod_timer(&port
->multicast_router_timer
,
1251 now
+ br
->multicast_querier_interval
);
1254 static void br_multicast_query_received(struct net_bridge
*br
,
1255 struct net_bridge_port
*port
,
1256 struct bridge_mcast_other_query
*query
,
1257 struct br_ip
*saddr
,
1258 unsigned long max_delay
)
1260 if (!br_multicast_select_querier(br
, port
, saddr
))
1263 br_multicast_update_query_timer(br
, query
, max_delay
);
1264 br_multicast_mark_router(br
, port
);
1267 static int br_ip4_multicast_query(struct net_bridge
*br
,
1268 struct net_bridge_port
*port
,
1269 struct sk_buff
*skb
,
1272 const struct iphdr
*iph
= ip_hdr(skb
);
1273 struct igmphdr
*ih
= igmp_hdr(skb
);
1274 struct net_bridge_mdb_entry
*mp
;
1275 struct igmpv3_query
*ih3
;
1276 struct net_bridge_port_group
*p
;
1277 struct net_bridge_port_group __rcu
**pp
;
1279 unsigned long max_delay
;
1280 unsigned long now
= jiffies
;
1284 spin_lock(&br
->multicast_lock
);
1285 if (!netif_running(br
->dev
) ||
1286 (port
&& port
->state
== BR_STATE_DISABLED
))
1291 if (skb
->len
== sizeof(*ih
)) {
1292 max_delay
= ih
->code
* (HZ
/ IGMP_TIMER_SCALE
);
1295 max_delay
= 10 * HZ
;
1298 } else if (skb
->len
>= sizeof(*ih3
)) {
1299 ih3
= igmpv3_query_hdr(skb
);
1303 max_delay
= ih3
->code
?
1304 IGMPV3_MRC(ih3
->code
) * (HZ
/ IGMP_TIMER_SCALE
) : 1;
1310 saddr
.proto
= htons(ETH_P_IP
);
1311 saddr
.u
.ip4
= iph
->saddr
;
1313 br_multicast_query_received(br
, port
, &br
->ip4_other_query
,
1318 mp
= br_mdb_ip4_get(mlock_dereference(br
->mdb
, br
), group
, vid
);
1322 max_delay
*= br
->multicast_last_member_count
;
1325 (timer_pending(&mp
->timer
) ?
1326 time_after(mp
->timer
.expires
, now
+ max_delay
) :
1327 try_to_del_timer_sync(&mp
->timer
) >= 0))
1328 mod_timer(&mp
->timer
, now
+ max_delay
);
1330 for (pp
= &mp
->ports
;
1331 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1333 if (timer_pending(&p
->timer
) ?
1334 time_after(p
->timer
.expires
, now
+ max_delay
) :
1335 try_to_del_timer_sync(&p
->timer
) >= 0)
1336 mod_timer(&p
->timer
, now
+ max_delay
);
1340 spin_unlock(&br
->multicast_lock
);
1344 #if IS_ENABLED(CONFIG_IPV6)
1345 static int br_ip6_multicast_query(struct net_bridge
*br
,
1346 struct net_bridge_port
*port
,
1347 struct sk_buff
*skb
,
1350 const struct ipv6hdr
*ip6h
= ipv6_hdr(skb
);
1351 struct mld_msg
*mld
;
1352 struct net_bridge_mdb_entry
*mp
;
1353 struct mld2_query
*mld2q
;
1354 struct net_bridge_port_group
*p
;
1355 struct net_bridge_port_group __rcu
**pp
;
1357 unsigned long max_delay
;
1358 unsigned long now
= jiffies
;
1359 const struct in6_addr
*group
= NULL
;
1360 bool is_general_query
;
1363 spin_lock(&br
->multicast_lock
);
1364 if (!netif_running(br
->dev
) ||
1365 (port
&& port
->state
== BR_STATE_DISABLED
))
1368 if (skb
->len
== sizeof(*mld
)) {
1369 if (!pskb_may_pull(skb
, sizeof(*mld
))) {
1373 mld
= (struct mld_msg
*) icmp6_hdr(skb
);
1374 max_delay
= msecs_to_jiffies(ntohs(mld
->mld_maxdelay
));
1376 group
= &mld
->mld_mca
;
1378 if (!pskb_may_pull(skb
, sizeof(*mld2q
))) {
1382 mld2q
= (struct mld2_query
*)icmp6_hdr(skb
);
1383 if (!mld2q
->mld2q_nsrcs
)
1384 group
= &mld2q
->mld2q_mca
;
1386 max_delay
= max(msecs_to_jiffies(mldv2_mrc(mld2q
)), 1UL);
1389 is_general_query
= group
&& ipv6_addr_any(group
);
1391 if (is_general_query
) {
1392 saddr
.proto
= htons(ETH_P_IPV6
);
1393 saddr
.u
.ip6
= ip6h
->saddr
;
1395 br_multicast_query_received(br
, port
, &br
->ip6_other_query
,
1398 } else if (!group
) {
1402 mp
= br_mdb_ip6_get(mlock_dereference(br
->mdb
, br
), group
, vid
);
1406 max_delay
*= br
->multicast_last_member_count
;
1408 (timer_pending(&mp
->timer
) ?
1409 time_after(mp
->timer
.expires
, now
+ max_delay
) :
1410 try_to_del_timer_sync(&mp
->timer
) >= 0))
1411 mod_timer(&mp
->timer
, now
+ max_delay
);
1413 for (pp
= &mp
->ports
;
1414 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1416 if (timer_pending(&p
->timer
) ?
1417 time_after(p
->timer
.expires
, now
+ max_delay
) :
1418 try_to_del_timer_sync(&p
->timer
) >= 0)
1419 mod_timer(&p
->timer
, now
+ max_delay
);
1423 spin_unlock(&br
->multicast_lock
);
1429 br_multicast_leave_group(struct net_bridge
*br
,
1430 struct net_bridge_port
*port
,
1431 struct br_ip
*group
,
1432 struct bridge_mcast_other_query
*other_query
,
1433 struct bridge_mcast_own_query
*own_query
)
1435 struct net_bridge_mdb_htable
*mdb
;
1436 struct net_bridge_mdb_entry
*mp
;
1437 struct net_bridge_port_group
*p
;
1441 spin_lock(&br
->multicast_lock
);
1442 if (!netif_running(br
->dev
) ||
1443 (port
&& port
->state
== BR_STATE_DISABLED
))
1446 mdb
= mlock_dereference(br
->mdb
, br
);
1447 mp
= br_mdb_ip_get(mdb
, group
);
1451 if (port
&& (port
->flags
& BR_MULTICAST_FAST_LEAVE
)) {
1452 struct net_bridge_port_group __rcu
**pp
;
1454 for (pp
= &mp
->ports
;
1455 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1457 if (p
->port
!= port
)
1460 rcu_assign_pointer(*pp
, p
->next
);
1461 hlist_del_init(&p
->mglist
);
1462 del_timer(&p
->timer
);
1463 call_rcu_bh(&p
->rcu
, br_multicast_free_pg
);
1464 br_mdb_notify(br
->dev
, p
, RTM_DELMDB
);
1466 if (!mp
->ports
&& !mp
->mglist
&&
1467 netif_running(br
->dev
))
1468 mod_timer(&mp
->timer
, jiffies
);
1473 if (timer_pending(&other_query
->timer
))
1476 if (br
->multicast_querier
) {
1477 __br_multicast_send_query(br
, port
, &mp
->addr
);
1479 time
= jiffies
+ br
->multicast_last_member_count
*
1480 br
->multicast_last_member_interval
;
1482 mod_timer(&own_query
->timer
, time
);
1484 for (p
= mlock_dereference(mp
->ports
, br
);
1486 p
= mlock_dereference(p
->next
, br
)) {
1487 if (p
->port
!= port
)
1490 if (!hlist_unhashed(&p
->mglist
) &&
1491 (timer_pending(&p
->timer
) ?
1492 time_after(p
->timer
.expires
, time
) :
1493 try_to_del_timer_sync(&p
->timer
) >= 0)) {
1494 mod_timer(&p
->timer
, time
);
1502 time
= now
+ br
->multicast_last_member_count
*
1503 br
->multicast_last_member_interval
;
1507 (timer_pending(&mp
->timer
) ?
1508 time_after(mp
->timer
.expires
, time
) :
1509 try_to_del_timer_sync(&mp
->timer
) >= 0)) {
1510 mod_timer(&mp
->timer
, time
);
1516 for (p
= mlock_dereference(mp
->ports
, br
);
1518 p
= mlock_dereference(p
->next
, br
)) {
1519 if (p
->port
!= port
)
1522 if (!hlist_unhashed(&p
->mglist
) &&
1523 (timer_pending(&p
->timer
) ?
1524 time_after(p
->timer
.expires
, time
) :
1525 try_to_del_timer_sync(&p
->timer
) >= 0)) {
1526 mod_timer(&p
->timer
, time
);
1532 spin_unlock(&br
->multicast_lock
);
1535 static void br_ip4_multicast_leave_group(struct net_bridge
*br
,
1536 struct net_bridge_port
*port
,
1540 struct br_ip br_group
;
1541 struct bridge_mcast_own_query
*own_query
;
1543 if (ipv4_is_local_multicast(group
))
1546 own_query
= port
? &port
->ip4_own_query
: &br
->ip4_own_query
;
1548 br_group
.u
.ip4
= group
;
1549 br_group
.proto
= htons(ETH_P_IP
);
1552 br_multicast_leave_group(br
, port
, &br_group
, &br
->ip4_other_query
,
1556 #if IS_ENABLED(CONFIG_IPV6)
1557 static void br_ip6_multicast_leave_group(struct net_bridge
*br
,
1558 struct net_bridge_port
*port
,
1559 const struct in6_addr
*group
,
1562 struct br_ip br_group
;
1563 struct bridge_mcast_own_query
*own_query
;
1565 if (ipv6_addr_is_ll_all_nodes(group
))
1568 own_query
= port
? &port
->ip6_own_query
: &br
->ip6_own_query
;
1570 br_group
.u
.ip6
= *group
;
1571 br_group
.proto
= htons(ETH_P_IPV6
);
1574 br_multicast_leave_group(br
, port
, &br_group
, &br
->ip6_other_query
,
1579 static int br_multicast_ipv4_rcv(struct net_bridge
*br
,
1580 struct net_bridge_port
*port
,
1581 struct sk_buff
*skb
,
1584 struct sk_buff
*skb_trimmed
= NULL
;
1588 err
= ip_mc_check_igmp(skb
, &skb_trimmed
);
1590 if (err
== -ENOMSG
) {
1591 if (!ipv4_is_local_multicast(ip_hdr(skb
)->daddr
))
1592 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1594 } else if (err
< 0) {
1598 BR_INPUT_SKB_CB(skb
)->igmp
= 1;
1602 case IGMP_HOST_MEMBERSHIP_REPORT
:
1603 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1604 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1605 err
= br_ip4_multicast_add_group(br
, port
, ih
->group
, vid
);
1607 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1608 err
= br_ip4_multicast_igmp3_report(br
, port
, skb_trimmed
, vid
);
1610 case IGMP_HOST_MEMBERSHIP_QUERY
:
1611 err
= br_ip4_multicast_query(br
, port
, skb_trimmed
, vid
);
1613 case IGMP_HOST_LEAVE_MESSAGE
:
1614 br_ip4_multicast_leave_group(br
, port
, ih
->group
, vid
);
1618 if (skb_trimmed
&& skb_trimmed
!= skb
)
1619 kfree_skb(skb_trimmed
);
1624 #if IS_ENABLED(CONFIG_IPV6)
1625 static int br_multicast_ipv6_rcv(struct net_bridge
*br
,
1626 struct net_bridge_port
*port
,
1627 struct sk_buff
*skb
,
1630 struct sk_buff
*skb_trimmed
= NULL
;
1631 struct mld_msg
*mld
;
1634 err
= ipv6_mc_check_mld(skb
, &skb_trimmed
);
1636 if (err
== -ENOMSG
) {
1637 if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb
)->daddr
))
1638 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1640 } else if (err
< 0) {
1644 BR_INPUT_SKB_CB(skb
)->igmp
= 1;
1645 mld
= (struct mld_msg
*)skb_transport_header(skb
);
1647 switch (mld
->mld_type
) {
1648 case ICMPV6_MGM_REPORT
:
1649 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1650 err
= br_ip6_multicast_add_group(br
, port
, &mld
->mld_mca
, vid
);
1652 case ICMPV6_MLD2_REPORT
:
1653 err
= br_ip6_multicast_mld2_report(br
, port
, skb_trimmed
, vid
);
1655 case ICMPV6_MGM_QUERY
:
1656 err
= br_ip6_multicast_query(br
, port
, skb_trimmed
, vid
);
1658 case ICMPV6_MGM_REDUCTION
:
1659 br_ip6_multicast_leave_group(br
, port
, &mld
->mld_mca
, vid
);
1663 if (skb_trimmed
&& skb_trimmed
!= skb
)
1664 kfree_skb(skb_trimmed
);
1670 int br_multicast_rcv(struct net_bridge
*br
, struct net_bridge_port
*port
,
1671 struct sk_buff
*skb
, u16 vid
)
1673 BR_INPUT_SKB_CB(skb
)->igmp
= 0;
1674 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 0;
1676 if (br
->multicast_disabled
)
1679 switch (skb
->protocol
) {
1680 case htons(ETH_P_IP
):
1681 return br_multicast_ipv4_rcv(br
, port
, skb
, vid
);
1682 #if IS_ENABLED(CONFIG_IPV6)
1683 case htons(ETH_P_IPV6
):
1684 return br_multicast_ipv6_rcv(br
, port
, skb
, vid
);
1691 static void br_multicast_query_expired(struct net_bridge
*br
,
1692 struct bridge_mcast_own_query
*query
,
1693 struct bridge_mcast_querier
*querier
)
1695 spin_lock(&br
->multicast_lock
);
1696 if (query
->startup_sent
< br
->multicast_startup_query_count
)
1697 query
->startup_sent
++;
1699 RCU_INIT_POINTER(querier
->port
, NULL
);
1700 br_multicast_send_query(br
, NULL
, query
);
1701 spin_unlock(&br
->multicast_lock
);
1704 static void br_ip4_multicast_query_expired(unsigned long data
)
1706 struct net_bridge
*br
= (void *)data
;
1708 br_multicast_query_expired(br
, &br
->ip4_own_query
, &br
->ip4_querier
);
1711 #if IS_ENABLED(CONFIG_IPV6)
1712 static void br_ip6_multicast_query_expired(unsigned long data
)
1714 struct net_bridge
*br
= (void *)data
;
1716 br_multicast_query_expired(br
, &br
->ip6_own_query
, &br
->ip6_querier
);
1720 void br_multicast_init(struct net_bridge
*br
)
1722 br
->hash_elasticity
= 4;
1725 br
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
1726 br
->multicast_querier
= 0;
1727 br
->multicast_query_use_ifaddr
= 0;
1728 br
->multicast_last_member_count
= 2;
1729 br
->multicast_startup_query_count
= 2;
1731 br
->multicast_last_member_interval
= HZ
;
1732 br
->multicast_query_response_interval
= 10 * HZ
;
1733 br
->multicast_startup_query_interval
= 125 * HZ
/ 4;
1734 br
->multicast_query_interval
= 125 * HZ
;
1735 br
->multicast_querier_interval
= 255 * HZ
;
1736 br
->multicast_membership_interval
= 260 * HZ
;
1738 br
->ip4_other_query
.delay_time
= 0;
1739 br
->ip4_querier
.port
= NULL
;
1740 #if IS_ENABLED(CONFIG_IPV6)
1741 br
->ip6_other_query
.delay_time
= 0;
1742 br
->ip6_querier
.port
= NULL
;
1745 spin_lock_init(&br
->multicast_lock
);
1746 setup_timer(&br
->multicast_router_timer
,
1747 br_multicast_local_router_expired
, 0);
1748 setup_timer(&br
->ip4_other_query
.timer
,
1749 br_ip4_multicast_querier_expired
, (unsigned long)br
);
1750 setup_timer(&br
->ip4_own_query
.timer
, br_ip4_multicast_query_expired
,
1752 #if IS_ENABLED(CONFIG_IPV6)
1753 setup_timer(&br
->ip6_other_query
.timer
,
1754 br_ip6_multicast_querier_expired
, (unsigned long)br
);
1755 setup_timer(&br
->ip6_own_query
.timer
, br_ip6_multicast_query_expired
,
1760 static void __br_multicast_open(struct net_bridge
*br
,
1761 struct bridge_mcast_own_query
*query
)
1763 query
->startup_sent
= 0;
1765 if (br
->multicast_disabled
)
1768 mod_timer(&query
->timer
, jiffies
);
1771 void br_multicast_open(struct net_bridge
*br
)
1773 __br_multicast_open(br
, &br
->ip4_own_query
);
1774 #if IS_ENABLED(CONFIG_IPV6)
1775 __br_multicast_open(br
, &br
->ip6_own_query
);
1779 void br_multicast_stop(struct net_bridge
*br
)
1781 del_timer_sync(&br
->multicast_router_timer
);
1782 del_timer_sync(&br
->ip4_other_query
.timer
);
1783 del_timer_sync(&br
->ip4_own_query
.timer
);
1784 #if IS_ENABLED(CONFIG_IPV6)
1785 del_timer_sync(&br
->ip6_other_query
.timer
);
1786 del_timer_sync(&br
->ip6_own_query
.timer
);
1790 void br_multicast_dev_del(struct net_bridge
*br
)
1792 struct net_bridge_mdb_htable
*mdb
;
1793 struct net_bridge_mdb_entry
*mp
;
1794 struct hlist_node
*n
;
1798 spin_lock_bh(&br
->multicast_lock
);
1799 mdb
= mlock_dereference(br
->mdb
, br
);
1806 for (i
= 0; i
< mdb
->max
; i
++) {
1807 hlist_for_each_entry_safe(mp
, n
, &mdb
->mhash
[i
],
1809 del_timer(&mp
->timer
);
1810 call_rcu_bh(&mp
->rcu
, br_multicast_free_group
);
1815 spin_unlock_bh(&br
->multicast_lock
);
1817 spin_lock_bh(&br
->multicast_lock
);
1822 call_rcu_bh(&mdb
->rcu
, br_mdb_free
);
1825 spin_unlock_bh(&br
->multicast_lock
);
1828 int br_multicast_set_router(struct net_bridge
*br
, unsigned long val
)
1832 spin_lock_bh(&br
->multicast_lock
);
1835 case MDB_RTR_TYPE_DISABLED
:
1836 case MDB_RTR_TYPE_PERM
:
1837 del_timer(&br
->multicast_router_timer
);
1839 case MDB_RTR_TYPE_TEMP_QUERY
:
1840 br
->multicast_router
= val
;
1845 spin_unlock_bh(&br
->multicast_lock
);
1850 static void __del_port_router(struct net_bridge_port
*p
)
1852 if (hlist_unhashed(&p
->rlist
))
1854 hlist_del_init_rcu(&p
->rlist
);
1855 br_rtr_notify(p
->br
->dev
, p
, RTM_DELMDB
);
1858 int br_multicast_set_port_router(struct net_bridge_port
*p
, unsigned long val
)
1860 struct net_bridge
*br
= p
->br
;
1861 unsigned long now
= jiffies
;
1864 spin_lock(&br
->multicast_lock
);
1865 if (p
->multicast_router
== val
) {
1866 /* Refresh the temp router port timer */
1867 if (p
->multicast_router
== MDB_RTR_TYPE_TEMP
)
1868 mod_timer(&p
->multicast_router_timer
,
1869 now
+ br
->multicast_querier_interval
);
1874 case MDB_RTR_TYPE_DISABLED
:
1875 p
->multicast_router
= MDB_RTR_TYPE_DISABLED
;
1876 __del_port_router(p
);
1877 del_timer(&p
->multicast_router_timer
);
1879 case MDB_RTR_TYPE_TEMP_QUERY
:
1880 p
->multicast_router
= MDB_RTR_TYPE_TEMP_QUERY
;
1881 __del_port_router(p
);
1883 case MDB_RTR_TYPE_PERM
:
1884 p
->multicast_router
= MDB_RTR_TYPE_PERM
;
1885 del_timer(&p
->multicast_router_timer
);
1886 br_multicast_add_router(br
, p
);
1888 case MDB_RTR_TYPE_TEMP
:
1889 p
->multicast_router
= MDB_RTR_TYPE_TEMP
;
1890 br_multicast_mark_router(br
, p
);
1897 spin_unlock(&br
->multicast_lock
);
1902 static void br_multicast_start_querier(struct net_bridge
*br
,
1903 struct bridge_mcast_own_query
*query
)
1905 struct net_bridge_port
*port
;
1907 __br_multicast_open(br
, query
);
1909 list_for_each_entry(port
, &br
->port_list
, list
) {
1910 if (port
->state
== BR_STATE_DISABLED
||
1911 port
->state
== BR_STATE_BLOCKING
)
1914 if (query
== &br
->ip4_own_query
)
1915 br_multicast_enable(&port
->ip4_own_query
);
1916 #if IS_ENABLED(CONFIG_IPV6)
1918 br_multicast_enable(&port
->ip6_own_query
);
1923 int br_multicast_toggle(struct net_bridge
*br
, unsigned long val
)
1926 struct net_bridge_mdb_htable
*mdb
;
1928 spin_lock_bh(&br
->multicast_lock
);
1929 if (br
->multicast_disabled
== !val
)
1932 br
->multicast_disabled
= !val
;
1933 if (br
->multicast_disabled
)
1936 if (!netif_running(br
->dev
))
1939 mdb
= mlock_dereference(br
->mdb
, br
);
1944 br
->multicast_disabled
= !!val
;
1948 err
= br_mdb_rehash(&br
->mdb
, mdb
->max
,
1949 br
->hash_elasticity
);
1954 br_multicast_start_querier(br
, &br
->ip4_own_query
);
1955 #if IS_ENABLED(CONFIG_IPV6)
1956 br_multicast_start_querier(br
, &br
->ip6_own_query
);
1960 spin_unlock_bh(&br
->multicast_lock
);
1965 int br_multicast_set_querier(struct net_bridge
*br
, unsigned long val
)
1967 unsigned long max_delay
;
1971 spin_lock_bh(&br
->multicast_lock
);
1972 if (br
->multicast_querier
== val
)
1975 br
->multicast_querier
= val
;
1979 max_delay
= br
->multicast_query_response_interval
;
1981 if (!timer_pending(&br
->ip4_other_query
.timer
))
1982 br
->ip4_other_query
.delay_time
= jiffies
+ max_delay
;
1984 br_multicast_start_querier(br
, &br
->ip4_own_query
);
1986 #if IS_ENABLED(CONFIG_IPV6)
1987 if (!timer_pending(&br
->ip6_other_query
.timer
))
1988 br
->ip6_other_query
.delay_time
= jiffies
+ max_delay
;
1990 br_multicast_start_querier(br
, &br
->ip6_own_query
);
1994 spin_unlock_bh(&br
->multicast_lock
);
1999 int br_multicast_set_hash_max(struct net_bridge
*br
, unsigned long val
)
2003 struct net_bridge_mdb_htable
*mdb
;
2005 spin_lock_bh(&br
->multicast_lock
);
2006 if (!is_power_of_2(val
))
2009 mdb
= mlock_dereference(br
->mdb
, br
);
2010 if (mdb
&& val
< mdb
->size
)
2026 err
= br_mdb_rehash(&br
->mdb
, br
->hash_max
,
2027 br
->hash_elasticity
);
2033 spin_unlock_bh(&br
->multicast_lock
);
2039 * br_multicast_list_adjacent - Returns snooped multicast addresses
2040 * @dev: The bridge port adjacent to which to retrieve addresses
2041 * @br_ip_list: The list to store found, snooped multicast IP addresses in
2043 * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
2044 * snooping feature on all bridge ports of dev's bridge device, excluding
2045 * the addresses from dev itself.
2047 * Returns the number of items added to br_ip_list.
2050 * - br_ip_list needs to be initialized by caller
2051 * - br_ip_list might contain duplicates in the end
2052 * (needs to be taken care of by caller)
2053 * - br_ip_list needs to be freed by caller
2055 int br_multicast_list_adjacent(struct net_device
*dev
,
2056 struct list_head
*br_ip_list
)
2058 struct net_bridge
*br
;
2059 struct net_bridge_port
*port
;
2060 struct net_bridge_port_group
*group
;
2061 struct br_ip_list
*entry
;
2065 if (!br_ip_list
|| !br_port_exists(dev
))
2068 port
= br_port_get_rcu(dev
);
2069 if (!port
|| !port
->br
)
2074 list_for_each_entry_rcu(port
, &br
->port_list
, list
) {
2075 if (!port
->dev
|| port
->dev
== dev
)
2078 hlist_for_each_entry_rcu(group
, &port
->mglist
, mglist
) {
2079 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
2083 entry
->addr
= group
->addr
;
2084 list_add(&entry
->list
, br_ip_list
);
2093 EXPORT_SYMBOL_GPL(br_multicast_list_adjacent
);
2096 * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2097 * @dev: The bridge port providing the bridge on which to check for a querier
2098 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2100 * Checks whether the given interface has a bridge on top and if so returns
2101 * true if a valid querier exists anywhere on the bridged link layer.
2102 * Otherwise returns false.
2104 bool br_multicast_has_querier_anywhere(struct net_device
*dev
, int proto
)
2106 struct net_bridge
*br
;
2107 struct net_bridge_port
*port
;
2112 if (!br_port_exists(dev
))
2115 port
= br_port_get_rcu(dev
);
2116 if (!port
|| !port
->br
)
2121 memset(ð
, 0, sizeof(eth
));
2122 eth
.h_proto
= htons(proto
);
2124 ret
= br_multicast_querier_exists(br
, ð
);
2130 EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere
);
2133 * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
2134 * @dev: The bridge port adjacent to which to check for a querier
2135 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2137 * Checks whether the given interface has a bridge on top and if so returns
2138 * true if a selected querier is behind one of the other ports of this
2139 * bridge. Otherwise returns false.
2141 bool br_multicast_has_querier_adjacent(struct net_device
*dev
, int proto
)
2143 struct net_bridge
*br
;
2144 struct net_bridge_port
*port
;
2148 if (!br_port_exists(dev
))
2151 port
= br_port_get_rcu(dev
);
2152 if (!port
|| !port
->br
)
2159 if (!timer_pending(&br
->ip4_other_query
.timer
) ||
2160 rcu_dereference(br
->ip4_querier
.port
) == port
)
2163 #if IS_ENABLED(CONFIG_IPV6)
2165 if (!timer_pending(&br
->ip6_other_query
.timer
) ||
2166 rcu_dereference(br
->ip6_querier
.port
) == port
)
2179 EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent
);