2 * Bridge multicast support.
4 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
13 #include <linux/err.h>
14 #include <linux/if_ether.h>
15 #include <linux/igmp.h>
16 #include <linux/jhash.h>
17 #include <linux/kernel.h>
18 #include <linux/log2.h>
19 #include <linux/netdevice.h>
20 #include <linux/netfilter_bridge.h>
21 #include <linux/random.h>
22 #include <linux/rculist.h>
23 #include <linux/skbuff.h>
24 #include <linux/slab.h>
25 #include <linux/timer.h>
27 #if IS_ENABLED(CONFIG_IPV6)
30 #include <net/addrconf.h>
31 #include <net/ip6_checksum.h>
34 #include "br_private.h"
36 #define mlock_dereference(X, br) \
37 rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock))
39 static void br_multicast_start_querier(struct net_bridge
*br
);
41 #if IS_ENABLED(CONFIG_IPV6)
42 static inline int ipv6_is_transient_multicast(const struct in6_addr
*addr
)
44 if (ipv6_addr_is_multicast(addr
) && IPV6_ADDR_MC_FLAG_TRANSIENT(addr
))
50 static inline int br_ip_equal(const struct br_ip
*a
, const struct br_ip
*b
)
52 if (a
->proto
!= b
->proto
)
56 return a
->u
.ip4
== b
->u
.ip4
;
57 #if IS_ENABLED(CONFIG_IPV6)
58 case htons(ETH_P_IPV6
):
59 return ipv6_addr_equal(&a
->u
.ip6
, &b
->u
.ip6
);
65 static inline int __br_ip4_hash(struct net_bridge_mdb_htable
*mdb
, __be32 ip
)
67 return jhash_1word(mdb
->secret
, (__force u32
)ip
) & (mdb
->max
- 1);
70 #if IS_ENABLED(CONFIG_IPV6)
71 static inline int __br_ip6_hash(struct net_bridge_mdb_htable
*mdb
,
72 const struct in6_addr
*ip
)
74 return jhash2((__force u32
*)ip
->s6_addr32
, 4, mdb
->secret
) & (mdb
->max
- 1);
78 static inline int br_ip_hash(struct net_bridge_mdb_htable
*mdb
,
83 return __br_ip4_hash(mdb
, ip
->u
.ip4
);
84 #if IS_ENABLED(CONFIG_IPV6)
85 case htons(ETH_P_IPV6
):
86 return __br_ip6_hash(mdb
, &ip
->u
.ip6
);
92 static struct net_bridge_mdb_entry
*__br_mdb_ip_get(
93 struct net_bridge_mdb_htable
*mdb
, struct br_ip
*dst
, int hash
)
95 struct net_bridge_mdb_entry
*mp
;
98 hlist_for_each_entry_rcu(mp
, p
, &mdb
->mhash
[hash
], hlist
[mdb
->ver
]) {
99 if (br_ip_equal(&mp
->addr
, dst
))
106 static struct net_bridge_mdb_entry
*br_mdb_ip_get(
107 struct net_bridge_mdb_htable
*mdb
, struct br_ip
*dst
)
112 return __br_mdb_ip_get(mdb
, dst
, br_ip_hash(mdb
, dst
));
115 static struct net_bridge_mdb_entry
*br_mdb_ip4_get(
116 struct net_bridge_mdb_htable
*mdb
, __be32 dst
)
121 br_dst
.proto
= htons(ETH_P_IP
);
123 return br_mdb_ip_get(mdb
, &br_dst
);
126 #if IS_ENABLED(CONFIG_IPV6)
127 static struct net_bridge_mdb_entry
*br_mdb_ip6_get(
128 struct net_bridge_mdb_htable
*mdb
, const struct in6_addr
*dst
)
133 br_dst
.proto
= htons(ETH_P_IPV6
);
135 return br_mdb_ip_get(mdb
, &br_dst
);
139 struct net_bridge_mdb_entry
*br_mdb_get(struct net_bridge
*br
,
142 struct net_bridge_mdb_htable
*mdb
= rcu_dereference(br
->mdb
);
145 if (br
->multicast_disabled
)
148 if (BR_INPUT_SKB_CB(skb
)->igmp
)
151 ip
.proto
= skb
->protocol
;
153 switch (skb
->protocol
) {
154 case htons(ETH_P_IP
):
155 ip
.u
.ip4
= ip_hdr(skb
)->daddr
;
157 #if IS_ENABLED(CONFIG_IPV6)
158 case htons(ETH_P_IPV6
):
159 ip
.u
.ip6
= ipv6_hdr(skb
)->daddr
;
166 return br_mdb_ip_get(mdb
, &ip
);
169 static void br_mdb_free(struct rcu_head
*head
)
171 struct net_bridge_mdb_htable
*mdb
=
172 container_of(head
, struct net_bridge_mdb_htable
, rcu
);
173 struct net_bridge_mdb_htable
*old
= mdb
->old
;
180 static int br_mdb_copy(struct net_bridge_mdb_htable
*new,
181 struct net_bridge_mdb_htable
*old
,
184 struct net_bridge_mdb_entry
*mp
;
185 struct hlist_node
*p
;
190 for (i
= 0; i
< old
->max
; i
++)
191 hlist_for_each_entry(mp
, p
, &old
->mhash
[i
], hlist
[old
->ver
])
192 hlist_add_head(&mp
->hlist
[new->ver
],
193 &new->mhash
[br_ip_hash(new, &mp
->addr
)]);
199 for (i
= 0; i
< new->max
; i
++) {
201 hlist_for_each_entry(mp
, p
, &new->mhash
[i
], hlist
[new->ver
])
207 return maxlen
> elasticity
? -EINVAL
: 0;
210 static void br_multicast_free_pg(struct rcu_head
*head
)
212 struct net_bridge_port_group
*p
=
213 container_of(head
, struct net_bridge_port_group
, rcu
);
218 static void br_multicast_free_group(struct rcu_head
*head
)
220 struct net_bridge_mdb_entry
*mp
=
221 container_of(head
, struct net_bridge_mdb_entry
, rcu
);
226 static void br_multicast_group_expired(unsigned long data
)
228 struct net_bridge_mdb_entry
*mp
= (void *)data
;
229 struct net_bridge
*br
= mp
->br
;
230 struct net_bridge_mdb_htable
*mdb
;
232 spin_lock(&br
->multicast_lock
);
233 if (!netif_running(br
->dev
) || timer_pending(&mp
->timer
))
241 mdb
= mlock_dereference(br
->mdb
, br
);
243 hlist_del_rcu(&mp
->hlist
[mdb
->ver
]);
246 call_rcu_bh(&mp
->rcu
, br_multicast_free_group
);
249 spin_unlock(&br
->multicast_lock
);
252 static void br_multicast_del_pg(struct net_bridge
*br
,
253 struct net_bridge_port_group
*pg
)
255 struct net_bridge_mdb_htable
*mdb
;
256 struct net_bridge_mdb_entry
*mp
;
257 struct net_bridge_port_group
*p
;
258 struct net_bridge_port_group __rcu
**pp
;
260 mdb
= mlock_dereference(br
->mdb
, br
);
262 mp
= br_mdb_ip_get(mdb
, &pg
->addr
);
266 for (pp
= &mp
->ports
;
267 (p
= mlock_dereference(*pp
, br
)) != NULL
;
272 rcu_assign_pointer(*pp
, p
->next
);
273 hlist_del_init(&p
->mglist
);
274 del_timer(&p
->timer
);
275 call_rcu_bh(&p
->rcu
, br_multicast_free_pg
);
277 if (!mp
->ports
&& !mp
->mglist
&&
278 netif_running(br
->dev
))
279 mod_timer(&mp
->timer
, jiffies
);
287 static void br_multicast_port_group_expired(unsigned long data
)
289 struct net_bridge_port_group
*pg
= (void *)data
;
290 struct net_bridge
*br
= pg
->port
->br
;
292 spin_lock(&br
->multicast_lock
);
293 if (!netif_running(br
->dev
) || timer_pending(&pg
->timer
) ||
294 hlist_unhashed(&pg
->mglist
))
297 br_multicast_del_pg(br
, pg
);
300 spin_unlock(&br
->multicast_lock
);
303 static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu
**mdbp
, int max
,
306 struct net_bridge_mdb_htable
*old
= rcu_dereference_protected(*mdbp
, 1);
307 struct net_bridge_mdb_htable
*mdb
;
310 mdb
= kmalloc(sizeof(*mdb
), GFP_ATOMIC
);
317 mdb
->mhash
= kzalloc(max
* sizeof(*mdb
->mhash
), GFP_ATOMIC
);
323 mdb
->size
= old
? old
->size
: 0;
324 mdb
->ver
= old
? old
->ver
^ 1 : 0;
326 if (!old
|| elasticity
)
327 get_random_bytes(&mdb
->secret
, sizeof(mdb
->secret
));
329 mdb
->secret
= old
->secret
;
334 err
= br_mdb_copy(mdb
, old
, elasticity
);
341 call_rcu_bh(&mdb
->rcu
, br_mdb_free
);
344 rcu_assign_pointer(*mdbp
, mdb
);
349 static struct sk_buff
*br_ip4_multicast_alloc_query(struct net_bridge
*br
,
357 skb
= netdev_alloc_skb_ip_align(br
->dev
, sizeof(*eth
) + sizeof(*iph
) +
362 skb
->protocol
= htons(ETH_P_IP
);
364 skb_reset_mac_header(skb
);
367 memcpy(eth
->h_source
, br
->dev
->dev_addr
, 6);
370 eth
->h_dest
[2] = 0x5e;
374 eth
->h_proto
= htons(ETH_P_IP
);
375 skb_put(skb
, sizeof(*eth
));
377 skb_set_network_header(skb
, skb
->len
);
383 iph
->tot_len
= htons(sizeof(*iph
) + sizeof(*ih
) + 4);
385 iph
->frag_off
= htons(IP_DF
);
387 iph
->protocol
= IPPROTO_IGMP
;
389 iph
->daddr
= htonl(INADDR_ALLHOSTS_GROUP
);
390 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
391 ((u8
*)&iph
[1])[1] = 4;
392 ((u8
*)&iph
[1])[2] = 0;
393 ((u8
*)&iph
[1])[3] = 0;
397 skb_set_transport_header(skb
, skb
->len
);
399 ih
->type
= IGMP_HOST_MEMBERSHIP_QUERY
;
400 ih
->code
= (group
? br
->multicast_last_member_interval
:
401 br
->multicast_query_response_interval
) /
402 (HZ
/ IGMP_TIMER_SCALE
);
405 ih
->csum
= ip_compute_csum((void *)ih
, sizeof(struct igmphdr
));
406 skb_put(skb
, sizeof(*ih
));
408 __skb_pull(skb
, sizeof(*eth
));
414 #if IS_ENABLED(CONFIG_IPV6)
415 static struct sk_buff
*br_ip6_multicast_alloc_query(struct net_bridge
*br
,
416 const struct in6_addr
*group
)
419 struct ipv6hdr
*ip6h
;
420 struct mld_msg
*mldq
;
423 unsigned long interval
;
425 skb
= netdev_alloc_skb_ip_align(br
->dev
, sizeof(*eth
) + sizeof(*ip6h
) +
430 skb
->protocol
= htons(ETH_P_IPV6
);
432 /* Ethernet header */
433 skb_reset_mac_header(skb
);
436 memcpy(eth
->h_source
, br
->dev
->dev_addr
, 6);
437 eth
->h_proto
= htons(ETH_P_IPV6
);
438 skb_put(skb
, sizeof(*eth
));
440 /* IPv6 header + HbH option */
441 skb_set_network_header(skb
, skb
->len
);
442 ip6h
= ipv6_hdr(skb
);
444 *(__force __be32
*)ip6h
= htonl(0x60000000);
445 ip6h
->payload_len
= htons(8 + sizeof(*mldq
));
446 ip6h
->nexthdr
= IPPROTO_HOPOPTS
;
448 ipv6_addr_set(&ip6h
->daddr
, htonl(0xff020000), 0, 0, htonl(1));
449 if (ipv6_dev_get_saddr(dev_net(br
->dev
), br
->dev
, &ip6h
->daddr
, 0,
454 ipv6_eth_mc_map(&ip6h
->daddr
, eth
->h_dest
);
456 hopopt
= (u8
*)(ip6h
+ 1);
457 hopopt
[0] = IPPROTO_ICMPV6
; /* next hdr */
458 hopopt
[1] = 0; /* length of HbH */
459 hopopt
[2] = IPV6_TLV_ROUTERALERT
; /* Router Alert */
460 hopopt
[3] = 2; /* Length of RA Option */
461 hopopt
[4] = 0; /* Type = 0x0000 (MLD) */
463 hopopt
[6] = IPV6_TLV_PAD1
; /* Pad1 */
464 hopopt
[7] = IPV6_TLV_PAD1
; /* Pad1 */
466 skb_put(skb
, sizeof(*ip6h
) + 8);
469 skb_set_transport_header(skb
, skb
->len
);
470 mldq
= (struct mld_msg
*) icmp6_hdr(skb
);
472 interval
= ipv6_addr_any(group
) ? br
->multicast_last_member_interval
:
473 br
->multicast_query_response_interval
;
475 mldq
->mld_type
= ICMPV6_MGM_QUERY
;
478 mldq
->mld_maxdelay
= htons((u16
)jiffies_to_msecs(interval
));
479 mldq
->mld_reserved
= 0;
480 mldq
->mld_mca
= *group
;
483 mldq
->mld_cksum
= csum_ipv6_magic(&ip6h
->saddr
, &ip6h
->daddr
,
484 sizeof(*mldq
), IPPROTO_ICMPV6
,
487 skb_put(skb
, sizeof(*mldq
));
489 __skb_pull(skb
, sizeof(*eth
));
496 static struct sk_buff
*br_multicast_alloc_query(struct net_bridge
*br
,
499 switch (addr
->proto
) {
500 case htons(ETH_P_IP
):
501 return br_ip4_multicast_alloc_query(br
, addr
->u
.ip4
);
502 #if IS_ENABLED(CONFIG_IPV6)
503 case htons(ETH_P_IPV6
):
504 return br_ip6_multicast_alloc_query(br
, &addr
->u
.ip6
);
510 static struct net_bridge_mdb_entry
*br_multicast_get_group(
511 struct net_bridge
*br
, struct net_bridge_port
*port
,
512 struct br_ip
*group
, int hash
)
514 struct net_bridge_mdb_htable
*mdb
;
515 struct net_bridge_mdb_entry
*mp
;
516 struct hlist_node
*p
;
517 unsigned int count
= 0;
522 mdb
= rcu_dereference_protected(br
->mdb
, 1);
523 hlist_for_each_entry(mp
, p
, &mdb
->mhash
[hash
], hlist
[mdb
->ver
]) {
525 if (unlikely(br_ip_equal(group
, &mp
->addr
)))
532 if (unlikely(count
> br
->hash_elasticity
&& count
)) {
534 br_info(br
, "Multicast hash table "
535 "chain limit reached: %s\n",
536 port
? port
->dev
->name
: br
->dev
->name
);
538 elasticity
= br
->hash_elasticity
;
541 if (mdb
->size
>= max
) {
543 if (unlikely(max
> br
->hash_max
)) {
544 br_warn(br
, "Multicast hash table maximum of %d "
545 "reached, disabling snooping: %s\n",
547 port
? port
->dev
->name
: br
->dev
->name
);
550 br
->multicast_disabled
= 1;
555 if (max
> mdb
->max
|| elasticity
) {
558 br_info(br
, "Multicast hash table "
560 port
? port
->dev
->name
: br
->dev
->name
);
565 err
= br_mdb_rehash(&br
->mdb
, max
, elasticity
);
567 br_warn(br
, "Cannot rehash multicast "
568 "hash table, disabling snooping: %s, %d, %d\n",
569 port
? port
->dev
->name
: br
->dev
->name
,
585 static struct net_bridge_mdb_entry
*br_multicast_new_group(
586 struct net_bridge
*br
, struct net_bridge_port
*port
,
589 struct net_bridge_mdb_htable
*mdb
;
590 struct net_bridge_mdb_entry
*mp
;
594 mdb
= rcu_dereference_protected(br
->mdb
, 1);
596 err
= br_mdb_rehash(&br
->mdb
, BR_HASH_SIZE
, 0);
602 hash
= br_ip_hash(mdb
, group
);
603 mp
= br_multicast_get_group(br
, port
, group
, hash
);
604 switch (PTR_ERR(mp
)) {
610 mdb
= rcu_dereference_protected(br
->mdb
, 1);
611 hash
= br_ip_hash(mdb
, group
);
618 mp
= kzalloc(sizeof(*mp
), GFP_ATOMIC
);
620 return ERR_PTR(-ENOMEM
);
624 setup_timer(&mp
->timer
, br_multicast_group_expired
,
627 hlist_add_head_rcu(&mp
->hlist
[mdb
->ver
], &mdb
->mhash
[hash
]);
634 static int br_multicast_add_group(struct net_bridge
*br
,
635 struct net_bridge_port
*port
,
638 struct net_bridge_mdb_entry
*mp
;
639 struct net_bridge_port_group
*p
;
640 struct net_bridge_port_group __rcu
**pp
;
641 unsigned long now
= jiffies
;
644 spin_lock(&br
->multicast_lock
);
645 if (!netif_running(br
->dev
) ||
646 (port
&& port
->state
== BR_STATE_DISABLED
))
649 mp
= br_multicast_new_group(br
, port
, group
);
656 mod_timer(&mp
->timer
, now
+ br
->multicast_membership_interval
);
660 for (pp
= &mp
->ports
;
661 (p
= mlock_dereference(*pp
, br
)) != NULL
;
665 if ((unsigned long)p
->port
< (unsigned long)port
)
669 p
= kzalloc(sizeof(*p
), GFP_ATOMIC
);
677 hlist_add_head(&p
->mglist
, &port
->mglist
);
678 setup_timer(&p
->timer
, br_multicast_port_group_expired
,
681 rcu_assign_pointer(*pp
, p
);
684 mod_timer(&p
->timer
, now
+ br
->multicast_membership_interval
);
689 spin_unlock(&br
->multicast_lock
);
693 static int br_ip4_multicast_add_group(struct net_bridge
*br
,
694 struct net_bridge_port
*port
,
697 struct br_ip br_group
;
699 if (ipv4_is_local_multicast(group
))
702 br_group
.u
.ip4
= group
;
703 br_group
.proto
= htons(ETH_P_IP
);
705 return br_multicast_add_group(br
, port
, &br_group
);
708 #if IS_ENABLED(CONFIG_IPV6)
709 static int br_ip6_multicast_add_group(struct net_bridge
*br
,
710 struct net_bridge_port
*port
,
711 const struct in6_addr
*group
)
713 struct br_ip br_group
;
715 if (!ipv6_is_transient_multicast(group
))
718 br_group
.u
.ip6
= *group
;
719 br_group
.proto
= htons(ETH_P_IPV6
);
721 return br_multicast_add_group(br
, port
, &br_group
);
725 static void br_multicast_router_expired(unsigned long data
)
727 struct net_bridge_port
*port
= (void *)data
;
728 struct net_bridge
*br
= port
->br
;
730 spin_lock(&br
->multicast_lock
);
731 if (port
->multicast_router
!= 1 ||
732 timer_pending(&port
->multicast_router_timer
) ||
733 hlist_unhashed(&port
->rlist
))
736 hlist_del_init_rcu(&port
->rlist
);
739 spin_unlock(&br
->multicast_lock
);
742 static void br_multicast_local_router_expired(unsigned long data
)
746 static void br_multicast_querier_expired(unsigned long data
)
748 struct net_bridge
*br
= (void *)data
;
750 spin_lock(&br
->multicast_lock
);
751 if (!netif_running(br
->dev
) || br
->multicast_disabled
)
754 br_multicast_start_querier(br
);
757 spin_unlock(&br
->multicast_lock
);
760 static void __br_multicast_send_query(struct net_bridge
*br
,
761 struct net_bridge_port
*port
,
766 skb
= br_multicast_alloc_query(br
, ip
);
771 __skb_push(skb
, sizeof(struct ethhdr
));
772 skb
->dev
= port
->dev
;
773 NF_HOOK(NFPROTO_BRIDGE
, NF_BR_LOCAL_OUT
, skb
, NULL
, skb
->dev
,
779 static void br_multicast_send_query(struct net_bridge
*br
,
780 struct net_bridge_port
*port
, u32 sent
)
783 struct br_ip br_group
;
785 if (!netif_running(br
->dev
) || br
->multicast_disabled
||
786 !br
->multicast_querier
||
787 timer_pending(&br
->multicast_querier_timer
))
790 memset(&br_group
.u
, 0, sizeof(br_group
.u
));
792 br_group
.proto
= htons(ETH_P_IP
);
793 __br_multicast_send_query(br
, port
, &br_group
);
795 #if IS_ENABLED(CONFIG_IPV6)
796 br_group
.proto
= htons(ETH_P_IPV6
);
797 __br_multicast_send_query(br
, port
, &br_group
);
801 time
+= sent
< br
->multicast_startup_query_count
?
802 br
->multicast_startup_query_interval
:
803 br
->multicast_query_interval
;
804 mod_timer(port
? &port
->multicast_query_timer
:
805 &br
->multicast_query_timer
, time
);
808 static void br_multicast_port_query_expired(unsigned long data
)
810 struct net_bridge_port
*port
= (void *)data
;
811 struct net_bridge
*br
= port
->br
;
813 spin_lock(&br
->multicast_lock
);
814 if (port
->state
== BR_STATE_DISABLED
||
815 port
->state
== BR_STATE_BLOCKING
)
818 if (port
->multicast_startup_queries_sent
<
819 br
->multicast_startup_query_count
)
820 port
->multicast_startup_queries_sent
++;
822 br_multicast_send_query(port
->br
, port
,
823 port
->multicast_startup_queries_sent
);
826 spin_unlock(&br
->multicast_lock
);
829 void br_multicast_add_port(struct net_bridge_port
*port
)
831 port
->multicast_router
= 1;
833 setup_timer(&port
->multicast_router_timer
, br_multicast_router_expired
,
834 (unsigned long)port
);
835 setup_timer(&port
->multicast_query_timer
,
836 br_multicast_port_query_expired
, (unsigned long)port
);
839 void br_multicast_del_port(struct net_bridge_port
*port
)
841 del_timer_sync(&port
->multicast_router_timer
);
844 static void __br_multicast_enable_port(struct net_bridge_port
*port
)
846 port
->multicast_startup_queries_sent
= 0;
848 if (try_to_del_timer_sync(&port
->multicast_query_timer
) >= 0 ||
849 del_timer(&port
->multicast_query_timer
))
850 mod_timer(&port
->multicast_query_timer
, jiffies
);
853 void br_multicast_enable_port(struct net_bridge_port
*port
)
855 struct net_bridge
*br
= port
->br
;
857 spin_lock(&br
->multicast_lock
);
858 if (br
->multicast_disabled
|| !netif_running(br
->dev
))
861 __br_multicast_enable_port(port
);
864 spin_unlock(&br
->multicast_lock
);
867 void br_multicast_disable_port(struct net_bridge_port
*port
)
869 struct net_bridge
*br
= port
->br
;
870 struct net_bridge_port_group
*pg
;
871 struct hlist_node
*p
, *n
;
873 spin_lock(&br
->multicast_lock
);
874 hlist_for_each_entry_safe(pg
, p
, n
, &port
->mglist
, mglist
)
875 br_multicast_del_pg(br
, pg
);
877 if (!hlist_unhashed(&port
->rlist
))
878 hlist_del_init_rcu(&port
->rlist
);
879 del_timer(&port
->multicast_router_timer
);
880 del_timer(&port
->multicast_query_timer
);
881 spin_unlock(&br
->multicast_lock
);
884 static int br_ip4_multicast_igmp3_report(struct net_bridge
*br
,
885 struct net_bridge_port
*port
,
888 struct igmpv3_report
*ih
;
889 struct igmpv3_grec
*grec
;
897 if (!pskb_may_pull(skb
, sizeof(*ih
)))
900 ih
= igmpv3_report_hdr(skb
);
901 num
= ntohs(ih
->ngrec
);
904 for (i
= 0; i
< num
; i
++) {
905 len
+= sizeof(*grec
);
906 if (!pskb_may_pull(skb
, len
))
909 grec
= (void *)(skb
->data
+ len
- sizeof(*grec
));
910 group
= grec
->grec_mca
;
911 type
= grec
->grec_type
;
913 len
+= ntohs(grec
->grec_nsrcs
) * 4;
914 if (!pskb_may_pull(skb
, len
))
917 /* We treat this as an IGMPv2 report for now. */
919 case IGMPV3_MODE_IS_INCLUDE
:
920 case IGMPV3_MODE_IS_EXCLUDE
:
921 case IGMPV3_CHANGE_TO_INCLUDE
:
922 case IGMPV3_CHANGE_TO_EXCLUDE
:
923 case IGMPV3_ALLOW_NEW_SOURCES
:
924 case IGMPV3_BLOCK_OLD_SOURCES
:
931 err
= br_ip4_multicast_add_group(br
, port
, group
);
939 #if IS_ENABLED(CONFIG_IPV6)
940 static int br_ip6_multicast_mld2_report(struct net_bridge
*br
,
941 struct net_bridge_port
*port
,
944 struct icmp6hdr
*icmp6h
;
945 struct mld2_grec
*grec
;
951 if (!pskb_may_pull(skb
, sizeof(*icmp6h
)))
954 icmp6h
= icmp6_hdr(skb
);
955 num
= ntohs(icmp6h
->icmp6_dataun
.un_data16
[1]);
956 len
= sizeof(*icmp6h
);
958 for (i
= 0; i
< num
; i
++) {
959 __be16
*nsrcs
, _nsrcs
;
961 nsrcs
= skb_header_pointer(skb
,
962 len
+ offsetof(struct mld2_grec
,
964 sizeof(_nsrcs
), &_nsrcs
);
968 if (!pskb_may_pull(skb
,
969 len
+ sizeof(*grec
) +
970 sizeof(struct in6_addr
) * ntohs(*nsrcs
)))
973 grec
= (struct mld2_grec
*)(skb
->data
+ len
);
974 len
+= sizeof(*grec
) +
975 sizeof(struct in6_addr
) * ntohs(*nsrcs
);
977 /* We treat these as MLDv1 reports for now. */
978 switch (grec
->grec_type
) {
979 case MLD2_MODE_IS_INCLUDE
:
980 case MLD2_MODE_IS_EXCLUDE
:
981 case MLD2_CHANGE_TO_INCLUDE
:
982 case MLD2_CHANGE_TO_EXCLUDE
:
983 case MLD2_ALLOW_NEW_SOURCES
:
984 case MLD2_BLOCK_OLD_SOURCES
:
991 err
= br_ip6_multicast_add_group(br
, port
, &grec
->grec_mca
);
1001 * Add port to rotuer_list
1002 * list is maintained ordered by pointer value
1003 * and locked by br->multicast_lock and RCU
1005 static void br_multicast_add_router(struct net_bridge
*br
,
1006 struct net_bridge_port
*port
)
1008 struct net_bridge_port
*p
;
1009 struct hlist_node
*n
, *slot
= NULL
;
1011 hlist_for_each_entry(p
, n
, &br
->router_list
, rlist
) {
1012 if ((unsigned long) port
>= (unsigned long) p
)
1018 hlist_add_after_rcu(slot
, &port
->rlist
);
1020 hlist_add_head_rcu(&port
->rlist
, &br
->router_list
);
1023 static void br_multicast_mark_router(struct net_bridge
*br
,
1024 struct net_bridge_port
*port
)
1026 unsigned long now
= jiffies
;
1029 if (br
->multicast_router
== 1)
1030 mod_timer(&br
->multicast_router_timer
,
1031 now
+ br
->multicast_querier_interval
);
1035 if (port
->multicast_router
!= 1)
1038 if (!hlist_unhashed(&port
->rlist
))
1041 br_multicast_add_router(br
, port
);
1044 mod_timer(&port
->multicast_router_timer
,
1045 now
+ br
->multicast_querier_interval
);
1048 static void br_multicast_query_received(struct net_bridge
*br
,
1049 struct net_bridge_port
*port
,
1053 mod_timer(&br
->multicast_querier_timer
,
1054 jiffies
+ br
->multicast_querier_interval
);
1055 else if (timer_pending(&br
->multicast_querier_timer
))
1058 br_multicast_mark_router(br
, port
);
1061 static int br_ip4_multicast_query(struct net_bridge
*br
,
1062 struct net_bridge_port
*port
,
1063 struct sk_buff
*skb
)
1065 const struct iphdr
*iph
= ip_hdr(skb
);
1066 struct igmphdr
*ih
= igmp_hdr(skb
);
1067 struct net_bridge_mdb_entry
*mp
;
1068 struct igmpv3_query
*ih3
;
1069 struct net_bridge_port_group
*p
;
1070 struct net_bridge_port_group __rcu
**pp
;
1071 unsigned long max_delay
;
1072 unsigned long now
= jiffies
;
1076 spin_lock(&br
->multicast_lock
);
1077 if (!netif_running(br
->dev
) ||
1078 (port
&& port
->state
== BR_STATE_DISABLED
))
1081 br_multicast_query_received(br
, port
, !!iph
->saddr
);
1085 if (skb
->len
== sizeof(*ih
)) {
1086 max_delay
= ih
->code
* (HZ
/ IGMP_TIMER_SCALE
);
1089 max_delay
= 10 * HZ
;
1093 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
))) {
1098 ih3
= igmpv3_query_hdr(skb
);
1102 max_delay
= ih3
->code
?
1103 IGMPV3_MRC(ih3
->code
) * (HZ
/ IGMP_TIMER_SCALE
) : 1;
1109 mp
= br_mdb_ip4_get(mlock_dereference(br
->mdb
, br
), group
);
1113 max_delay
*= br
->multicast_last_member_count
;
1116 (timer_pending(&mp
->timer
) ?
1117 time_after(mp
->timer
.expires
, now
+ max_delay
) :
1118 try_to_del_timer_sync(&mp
->timer
) >= 0))
1119 mod_timer(&mp
->timer
, now
+ max_delay
);
1121 for (pp
= &mp
->ports
;
1122 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1124 if (timer_pending(&p
->timer
) ?
1125 time_after(p
->timer
.expires
, now
+ max_delay
) :
1126 try_to_del_timer_sync(&p
->timer
) >= 0)
1127 mod_timer(&p
->timer
, now
+ max_delay
);
1131 spin_unlock(&br
->multicast_lock
);
1135 #if IS_ENABLED(CONFIG_IPV6)
1136 static int br_ip6_multicast_query(struct net_bridge
*br
,
1137 struct net_bridge_port
*port
,
1138 struct sk_buff
*skb
)
1140 const struct ipv6hdr
*ip6h
= ipv6_hdr(skb
);
1141 struct mld_msg
*mld
= (struct mld_msg
*) icmp6_hdr(skb
);
1142 struct net_bridge_mdb_entry
*mp
;
1143 struct mld2_query
*mld2q
;
1144 struct net_bridge_port_group
*p
;
1145 struct net_bridge_port_group __rcu
**pp
;
1146 unsigned long max_delay
;
1147 unsigned long now
= jiffies
;
1148 const struct in6_addr
*group
= NULL
;
1151 spin_lock(&br
->multicast_lock
);
1152 if (!netif_running(br
->dev
) ||
1153 (port
&& port
->state
== BR_STATE_DISABLED
))
1156 br_multicast_query_received(br
, port
, !ipv6_addr_any(&ip6h
->saddr
));
1158 if (skb
->len
== sizeof(*mld
)) {
1159 if (!pskb_may_pull(skb
, sizeof(*mld
))) {
1163 mld
= (struct mld_msg
*) icmp6_hdr(skb
);
1164 max_delay
= msecs_to_jiffies(ntohs(mld
->mld_maxdelay
));
1166 group
= &mld
->mld_mca
;
1167 } else if (skb
->len
>= sizeof(*mld2q
)) {
1168 if (!pskb_may_pull(skb
, sizeof(*mld2q
))) {
1172 mld2q
= (struct mld2_query
*)icmp6_hdr(skb
);
1173 if (!mld2q
->mld2q_nsrcs
)
1174 group
= &mld2q
->mld2q_mca
;
1175 max_delay
= mld2q
->mld2q_mrc
? MLDV2_MRC(mld2q
->mld2q_mrc
) : 1;
1181 mp
= br_mdb_ip6_get(mlock_dereference(br
->mdb
, br
), group
);
1185 max_delay
*= br
->multicast_last_member_count
;
1187 (timer_pending(&mp
->timer
) ?
1188 time_after(mp
->timer
.expires
, now
+ max_delay
) :
1189 try_to_del_timer_sync(&mp
->timer
) >= 0))
1190 mod_timer(&mp
->timer
, now
+ max_delay
);
1192 for (pp
= &mp
->ports
;
1193 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1195 if (timer_pending(&p
->timer
) ?
1196 time_after(p
->timer
.expires
, now
+ max_delay
) :
1197 try_to_del_timer_sync(&p
->timer
) >= 0)
1198 mod_timer(&p
->timer
, now
+ max_delay
);
1202 spin_unlock(&br
->multicast_lock
);
1207 static void br_multicast_leave_group(struct net_bridge
*br
,
1208 struct net_bridge_port
*port
,
1209 struct br_ip
*group
)
1211 struct net_bridge_mdb_htable
*mdb
;
1212 struct net_bridge_mdb_entry
*mp
;
1213 struct net_bridge_port_group
*p
;
1217 spin_lock(&br
->multicast_lock
);
1218 if (!netif_running(br
->dev
) ||
1219 (port
&& port
->state
== BR_STATE_DISABLED
) ||
1220 timer_pending(&br
->multicast_querier_timer
))
1223 mdb
= mlock_dereference(br
->mdb
, br
);
1224 mp
= br_mdb_ip_get(mdb
, group
);
1229 time
= now
+ br
->multicast_last_member_count
*
1230 br
->multicast_last_member_interval
;
1234 (timer_pending(&mp
->timer
) ?
1235 time_after(mp
->timer
.expires
, time
) :
1236 try_to_del_timer_sync(&mp
->timer
) >= 0)) {
1237 mod_timer(&mp
->timer
, time
);
1243 for (p
= mlock_dereference(mp
->ports
, br
);
1245 p
= mlock_dereference(p
->next
, br
)) {
1246 if (p
->port
!= port
)
1249 if (!hlist_unhashed(&p
->mglist
) &&
1250 (timer_pending(&p
->timer
) ?
1251 time_after(p
->timer
.expires
, time
) :
1252 try_to_del_timer_sync(&p
->timer
) >= 0)) {
1253 mod_timer(&p
->timer
, time
);
1260 spin_unlock(&br
->multicast_lock
);
1263 static void br_ip4_multicast_leave_group(struct net_bridge
*br
,
1264 struct net_bridge_port
*port
,
1267 struct br_ip br_group
;
1269 if (ipv4_is_local_multicast(group
))
1272 br_group
.u
.ip4
= group
;
1273 br_group
.proto
= htons(ETH_P_IP
);
1275 br_multicast_leave_group(br
, port
, &br_group
);
1278 #if IS_ENABLED(CONFIG_IPV6)
1279 static void br_ip6_multicast_leave_group(struct net_bridge
*br
,
1280 struct net_bridge_port
*port
,
1281 const struct in6_addr
*group
)
1283 struct br_ip br_group
;
1285 if (!ipv6_is_transient_multicast(group
))
1288 br_group
.u
.ip6
= *group
;
1289 br_group
.proto
= htons(ETH_P_IPV6
);
1291 br_multicast_leave_group(br
, port
, &br_group
);
1295 static int br_multicast_ipv4_rcv(struct net_bridge
*br
,
1296 struct net_bridge_port
*port
,
1297 struct sk_buff
*skb
)
1299 struct sk_buff
*skb2
= skb
;
1300 const struct iphdr
*iph
;
1303 unsigned int offset
;
1306 /* We treat OOM as packet loss for now. */
1307 if (!pskb_may_pull(skb
, sizeof(*iph
)))
1312 if (iph
->ihl
< 5 || iph
->version
!= 4)
1315 if (!pskb_may_pull(skb
, ip_hdrlen(skb
)))
1320 if (unlikely(ip_fast_csum((u8
*)iph
, iph
->ihl
)))
1323 if (iph
->protocol
!= IPPROTO_IGMP
) {
1324 if ((iph
->daddr
& IGMP_LOCAL_GROUP_MASK
) != IGMP_LOCAL_GROUP
)
1325 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1329 len
= ntohs(iph
->tot_len
);
1330 if (skb
->len
< len
|| len
< ip_hdrlen(skb
))
1333 if (skb
->len
> len
) {
1334 skb2
= skb_clone(skb
, GFP_ATOMIC
);
1338 err
= pskb_trim_rcsum(skb2
, len
);
1343 len
-= ip_hdrlen(skb2
);
1344 offset
= skb_network_offset(skb2
) + ip_hdrlen(skb2
);
1345 __skb_pull(skb2
, offset
);
1346 skb_reset_transport_header(skb2
);
1349 if (!pskb_may_pull(skb2
, sizeof(*ih
)))
1352 switch (skb2
->ip_summed
) {
1353 case CHECKSUM_COMPLETE
:
1354 if (!csum_fold(skb2
->csum
))
1359 if (skb_checksum_complete(skb2
))
1365 BR_INPUT_SKB_CB(skb
)->igmp
= 1;
1366 ih
= igmp_hdr(skb2
);
1369 case IGMP_HOST_MEMBERSHIP_REPORT
:
1370 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1371 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1372 err
= br_ip4_multicast_add_group(br
, port
, ih
->group
);
1374 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1375 err
= br_ip4_multicast_igmp3_report(br
, port
, skb2
);
1377 case IGMP_HOST_MEMBERSHIP_QUERY
:
1378 err
= br_ip4_multicast_query(br
, port
, skb2
);
1380 case IGMP_HOST_LEAVE_MESSAGE
:
1381 br_ip4_multicast_leave_group(br
, port
, ih
->group
);
1386 __skb_push(skb2
, offset
);
1393 #if IS_ENABLED(CONFIG_IPV6)
1394 static int br_multicast_ipv6_rcv(struct net_bridge
*br
,
1395 struct net_bridge_port
*port
,
1396 struct sk_buff
*skb
)
1398 struct sk_buff
*skb2
;
1399 const struct ipv6hdr
*ip6h
;
1407 if (!pskb_may_pull(skb
, sizeof(*ip6h
)))
1410 ip6h
= ipv6_hdr(skb
);
1413 * We're interested in MLD messages only.
1415 * - MLD has always Router Alert hop-by-hop option
1416 * - But we do not support jumbrograms.
1418 if (ip6h
->version
!= 6 ||
1419 ip6h
->nexthdr
!= IPPROTO_HOPOPTS
||
1420 ip6h
->payload_len
== 0)
1423 len
= ntohs(ip6h
->payload_len
) + sizeof(*ip6h
);
1427 nexthdr
= ip6h
->nexthdr
;
1428 offset
= ipv6_skip_exthdr(skb
, sizeof(*ip6h
), &nexthdr
, &frag_off
);
1430 if (offset
< 0 || nexthdr
!= IPPROTO_ICMPV6
)
1433 /* Okay, we found ICMPv6 header */
1434 skb2
= skb_clone(skb
, GFP_ATOMIC
);
1439 if (!pskb_may_pull(skb2
, offset
+ sizeof(struct icmp6hdr
)))
1442 len
-= offset
- skb_network_offset(skb2
);
1444 __skb_pull(skb2
, offset
);
1445 skb_reset_transport_header(skb2
);
1446 skb_postpull_rcsum(skb2
, skb_network_header(skb2
),
1447 skb_network_header_len(skb2
));
1449 icmp6_type
= icmp6_hdr(skb2
)->icmp6_type
;
1451 switch (icmp6_type
) {
1452 case ICMPV6_MGM_QUERY
:
1453 case ICMPV6_MGM_REPORT
:
1454 case ICMPV6_MGM_REDUCTION
:
1455 case ICMPV6_MLD2_REPORT
:
1462 /* Okay, we found MLD message. Check further. */
1463 if (skb2
->len
> len
) {
1464 err
= pskb_trim_rcsum(skb2
, len
);
1470 ip6h
= ipv6_hdr(skb2
);
1472 switch (skb2
->ip_summed
) {
1473 case CHECKSUM_COMPLETE
:
1474 if (!csum_ipv6_magic(&ip6h
->saddr
, &ip6h
->daddr
, skb2
->len
,
1475 IPPROTO_ICMPV6
, skb2
->csum
))
1479 skb2
->csum
= ~csum_unfold(csum_ipv6_magic(&ip6h
->saddr
,
1482 IPPROTO_ICMPV6
, 0));
1483 if (__skb_checksum_complete(skb2
))
1489 BR_INPUT_SKB_CB(skb
)->igmp
= 1;
1491 switch (icmp6_type
) {
1492 case ICMPV6_MGM_REPORT
:
1494 struct mld_msg
*mld
;
1495 if (!pskb_may_pull(skb2
, sizeof(*mld
))) {
1499 mld
= (struct mld_msg
*)skb_transport_header(skb2
);
1500 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1501 err
= br_ip6_multicast_add_group(br
, port
, &mld
->mld_mca
);
1504 case ICMPV6_MLD2_REPORT
:
1505 err
= br_ip6_multicast_mld2_report(br
, port
, skb2
);
1507 case ICMPV6_MGM_QUERY
:
1508 err
= br_ip6_multicast_query(br
, port
, skb2
);
1510 case ICMPV6_MGM_REDUCTION
:
1512 struct mld_msg
*mld
;
1513 if (!pskb_may_pull(skb2
, sizeof(*mld
))) {
1517 mld
= (struct mld_msg
*)skb_transport_header(skb2
);
1518 br_ip6_multicast_leave_group(br
, port
, &mld
->mld_mca
);
1528 int br_multicast_rcv(struct net_bridge
*br
, struct net_bridge_port
*port
,
1529 struct sk_buff
*skb
)
1531 BR_INPUT_SKB_CB(skb
)->igmp
= 0;
1532 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 0;
1534 if (br
->multicast_disabled
)
1537 switch (skb
->protocol
) {
1538 case htons(ETH_P_IP
):
1539 return br_multicast_ipv4_rcv(br
, port
, skb
);
1540 #if IS_ENABLED(CONFIG_IPV6)
1541 case htons(ETH_P_IPV6
):
1542 return br_multicast_ipv6_rcv(br
, port
, skb
);
1549 static void br_multicast_query_expired(unsigned long data
)
1551 struct net_bridge
*br
= (void *)data
;
1553 spin_lock(&br
->multicast_lock
);
1554 if (br
->multicast_startup_queries_sent
<
1555 br
->multicast_startup_query_count
)
1556 br
->multicast_startup_queries_sent
++;
1558 br_multicast_send_query(br
, NULL
, br
->multicast_startup_queries_sent
);
1560 spin_unlock(&br
->multicast_lock
);
1563 void br_multicast_init(struct net_bridge
*br
)
1565 br
->hash_elasticity
= 4;
1568 br
->multicast_router
= 1;
1569 br
->multicast_querier
= 0;
1570 br
->multicast_last_member_count
= 2;
1571 br
->multicast_startup_query_count
= 2;
1573 br
->multicast_last_member_interval
= HZ
;
1574 br
->multicast_query_response_interval
= 10 * HZ
;
1575 br
->multicast_startup_query_interval
= 125 * HZ
/ 4;
1576 br
->multicast_query_interval
= 125 * HZ
;
1577 br
->multicast_querier_interval
= 255 * HZ
;
1578 br
->multicast_membership_interval
= 260 * HZ
;
1580 spin_lock_init(&br
->multicast_lock
);
1581 setup_timer(&br
->multicast_router_timer
,
1582 br_multicast_local_router_expired
, 0);
1583 setup_timer(&br
->multicast_querier_timer
,
1584 br_multicast_querier_expired
, (unsigned long)br
);
1585 setup_timer(&br
->multicast_query_timer
, br_multicast_query_expired
,
1589 void br_multicast_open(struct net_bridge
*br
)
1591 br
->multicast_startup_queries_sent
= 0;
1593 if (br
->multicast_disabled
)
1596 mod_timer(&br
->multicast_query_timer
, jiffies
);
1599 void br_multicast_stop(struct net_bridge
*br
)
1601 struct net_bridge_mdb_htable
*mdb
;
1602 struct net_bridge_mdb_entry
*mp
;
1603 struct hlist_node
*p
, *n
;
1607 del_timer_sync(&br
->multicast_router_timer
);
1608 del_timer_sync(&br
->multicast_querier_timer
);
1609 del_timer_sync(&br
->multicast_query_timer
);
1611 spin_lock_bh(&br
->multicast_lock
);
1612 mdb
= mlock_dereference(br
->mdb
, br
);
1619 for (i
= 0; i
< mdb
->max
; i
++) {
1620 hlist_for_each_entry_safe(mp
, p
, n
, &mdb
->mhash
[i
],
1622 del_timer(&mp
->timer
);
1623 call_rcu_bh(&mp
->rcu
, br_multicast_free_group
);
1628 spin_unlock_bh(&br
->multicast_lock
);
1630 spin_lock_bh(&br
->multicast_lock
);
1635 call_rcu_bh(&mdb
->rcu
, br_mdb_free
);
1638 spin_unlock_bh(&br
->multicast_lock
);
1641 int br_multicast_set_router(struct net_bridge
*br
, unsigned long val
)
1645 spin_lock_bh(&br
->multicast_lock
);
1646 if (!netif_running(br
->dev
))
1652 del_timer(&br
->multicast_router_timer
);
1655 br
->multicast_router
= val
;
1665 spin_unlock_bh(&br
->multicast_lock
);
1670 int br_multicast_set_port_router(struct net_bridge_port
*p
, unsigned long val
)
1672 struct net_bridge
*br
= p
->br
;
1675 spin_lock(&br
->multicast_lock
);
1676 if (!netif_running(br
->dev
) || p
->state
== BR_STATE_DISABLED
)
1683 p
->multicast_router
= val
;
1686 if (val
< 2 && !hlist_unhashed(&p
->rlist
))
1687 hlist_del_init_rcu(&p
->rlist
);
1692 del_timer(&p
->multicast_router_timer
);
1697 br_multicast_add_router(br
, p
);
1706 spin_unlock(&br
->multicast_lock
);
1711 static void br_multicast_start_querier(struct net_bridge
*br
)
1713 struct net_bridge_port
*port
;
1715 br_multicast_open(br
);
1717 list_for_each_entry(port
, &br
->port_list
, list
) {
1718 if (port
->state
== BR_STATE_DISABLED
||
1719 port
->state
== BR_STATE_BLOCKING
)
1722 __br_multicast_enable_port(port
);
1726 int br_multicast_toggle(struct net_bridge
*br
, unsigned long val
)
1729 struct net_bridge_mdb_htable
*mdb
;
1731 spin_lock_bh(&br
->multicast_lock
);
1732 if (br
->multicast_disabled
== !val
)
1735 br
->multicast_disabled
= !val
;
1736 if (br
->multicast_disabled
)
1739 if (!netif_running(br
->dev
))
1742 mdb
= mlock_dereference(br
->mdb
, br
);
1747 br
->multicast_disabled
= !!val
;
1751 err
= br_mdb_rehash(&br
->mdb
, mdb
->max
,
1752 br
->hash_elasticity
);
1757 br_multicast_start_querier(br
);
1760 spin_unlock_bh(&br
->multicast_lock
);
1765 int br_multicast_set_querier(struct net_bridge
*br
, unsigned long val
)
1769 spin_lock_bh(&br
->multicast_lock
);
1770 if (br
->multicast_querier
== val
)
1773 br
->multicast_querier
= val
;
1775 br_multicast_start_querier(br
);
1778 spin_unlock_bh(&br
->multicast_lock
);
1783 int br_multicast_set_hash_max(struct net_bridge
*br
, unsigned long val
)
1787 struct net_bridge_mdb_htable
*mdb
;
1789 spin_lock(&br
->multicast_lock
);
1790 if (!netif_running(br
->dev
))
1794 if (!is_power_of_2(val
))
1797 mdb
= mlock_dereference(br
->mdb
, br
);
1798 if (mdb
&& val
< mdb
->size
)
1814 err
= br_mdb_rehash(&br
->mdb
, br
->hash_max
,
1815 br
->hash_elasticity
);
1821 spin_unlock(&br
->multicast_lock
);