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 defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
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 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
40 static inline int ipv6_is_transient_multicast(const struct in6_addr
*addr
)
42 if (ipv6_addr_is_multicast(addr
) && IPV6_ADDR_MC_FLAG_TRANSIENT(addr
))
48 static inline int br_ip_equal(const struct br_ip
*a
, const struct br_ip
*b
)
50 if (a
->proto
!= b
->proto
)
54 return a
->u
.ip4
== b
->u
.ip4
;
55 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
56 case htons(ETH_P_IPV6
):
57 return ipv6_addr_equal(&a
->u
.ip6
, &b
->u
.ip6
);
63 static inline int __br_ip4_hash(struct net_bridge_mdb_htable
*mdb
, __be32 ip
)
65 return jhash_1word(mdb
->secret
, (__force u32
)ip
) & (mdb
->max
- 1);
68 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
69 static inline int __br_ip6_hash(struct net_bridge_mdb_htable
*mdb
,
70 const struct in6_addr
*ip
)
72 return jhash2((__force u32
*)ip
->s6_addr32
, 4, mdb
->secret
) & (mdb
->max
- 1);
76 static inline int br_ip_hash(struct net_bridge_mdb_htable
*mdb
,
81 return __br_ip4_hash(mdb
, ip
->u
.ip4
);
82 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
83 case htons(ETH_P_IPV6
):
84 return __br_ip6_hash(mdb
, &ip
->u
.ip6
);
90 static struct net_bridge_mdb_entry
*__br_mdb_ip_get(
91 struct net_bridge_mdb_htable
*mdb
, struct br_ip
*dst
, int hash
)
93 struct net_bridge_mdb_entry
*mp
;
96 hlist_for_each_entry_rcu(mp
, p
, &mdb
->mhash
[hash
], hlist
[mdb
->ver
]) {
97 if (br_ip_equal(&mp
->addr
, dst
))
104 static struct net_bridge_mdb_entry
*br_mdb_ip_get(
105 struct net_bridge_mdb_htable
*mdb
, struct br_ip
*dst
)
110 return __br_mdb_ip_get(mdb
, dst
, br_ip_hash(mdb
, dst
));
113 static struct net_bridge_mdb_entry
*br_mdb_ip4_get(
114 struct net_bridge_mdb_htable
*mdb
, __be32 dst
)
119 br_dst
.proto
= htons(ETH_P_IP
);
121 return br_mdb_ip_get(mdb
, &br_dst
);
124 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
125 static struct net_bridge_mdb_entry
*br_mdb_ip6_get(
126 struct net_bridge_mdb_htable
*mdb
, const struct in6_addr
*dst
)
130 ipv6_addr_copy(&br_dst
.u
.ip6
, dst
);
131 br_dst
.proto
= htons(ETH_P_IPV6
);
133 return br_mdb_ip_get(mdb
, &br_dst
);
137 struct net_bridge_mdb_entry
*br_mdb_get(struct net_bridge
*br
,
140 struct net_bridge_mdb_htable
*mdb
= rcu_dereference(br
->mdb
);
143 if (br
->multicast_disabled
)
146 if (BR_INPUT_SKB_CB(skb
)->igmp
)
149 ip
.proto
= skb
->protocol
;
151 switch (skb
->protocol
) {
152 case htons(ETH_P_IP
):
153 ip
.u
.ip4
= ip_hdr(skb
)->daddr
;
155 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
156 case htons(ETH_P_IPV6
):
157 ipv6_addr_copy(&ip
.u
.ip6
, &ipv6_hdr(skb
)->daddr
);
164 return br_mdb_ip_get(mdb
, &ip
);
167 static void br_mdb_free(struct rcu_head
*head
)
169 struct net_bridge_mdb_htable
*mdb
=
170 container_of(head
, struct net_bridge_mdb_htable
, rcu
);
171 struct net_bridge_mdb_htable
*old
= mdb
->old
;
178 static int br_mdb_copy(struct net_bridge_mdb_htable
*new,
179 struct net_bridge_mdb_htable
*old
,
182 struct net_bridge_mdb_entry
*mp
;
183 struct hlist_node
*p
;
188 for (i
= 0; i
< old
->max
; i
++)
189 hlist_for_each_entry(mp
, p
, &old
->mhash
[i
], hlist
[old
->ver
])
190 hlist_add_head(&mp
->hlist
[new->ver
],
191 &new->mhash
[br_ip_hash(new, &mp
->addr
)]);
197 for (i
= 0; i
< new->max
; i
++) {
199 hlist_for_each_entry(mp
, p
, &new->mhash
[i
], hlist
[new->ver
])
205 return maxlen
> elasticity
? -EINVAL
: 0;
208 static void br_multicast_free_pg(struct rcu_head
*head
)
210 struct net_bridge_port_group
*p
=
211 container_of(head
, struct net_bridge_port_group
, rcu
);
216 static void br_multicast_free_group(struct rcu_head
*head
)
218 struct net_bridge_mdb_entry
*mp
=
219 container_of(head
, struct net_bridge_mdb_entry
, rcu
);
224 static void br_multicast_group_expired(unsigned long data
)
226 struct net_bridge_mdb_entry
*mp
= (void *)data
;
227 struct net_bridge
*br
= mp
->br
;
228 struct net_bridge_mdb_htable
*mdb
;
230 spin_lock(&br
->multicast_lock
);
231 if (!netif_running(br
->dev
) || timer_pending(&mp
->timer
))
239 mdb
= mlock_dereference(br
->mdb
, br
);
241 hlist_del_rcu(&mp
->hlist
[mdb
->ver
]);
244 call_rcu_bh(&mp
->rcu
, br_multicast_free_group
);
247 spin_unlock(&br
->multicast_lock
);
250 static void br_multicast_del_pg(struct net_bridge
*br
,
251 struct net_bridge_port_group
*pg
)
253 struct net_bridge_mdb_htable
*mdb
;
254 struct net_bridge_mdb_entry
*mp
;
255 struct net_bridge_port_group
*p
;
256 struct net_bridge_port_group __rcu
**pp
;
258 mdb
= mlock_dereference(br
->mdb
, br
);
260 mp
= br_mdb_ip_get(mdb
, &pg
->addr
);
264 for (pp
= &mp
->ports
;
265 (p
= mlock_dereference(*pp
, br
)) != NULL
;
270 rcu_assign_pointer(*pp
, p
->next
);
271 hlist_del_init(&p
->mglist
);
272 del_timer(&p
->timer
);
273 call_rcu_bh(&p
->rcu
, br_multicast_free_pg
);
275 if (!mp
->ports
&& !mp
->mglist
&&
276 netif_running(br
->dev
))
277 mod_timer(&mp
->timer
, jiffies
);
285 static void br_multicast_port_group_expired(unsigned long data
)
287 struct net_bridge_port_group
*pg
= (void *)data
;
288 struct net_bridge
*br
= pg
->port
->br
;
290 spin_lock(&br
->multicast_lock
);
291 if (!netif_running(br
->dev
) || timer_pending(&pg
->timer
) ||
292 hlist_unhashed(&pg
->mglist
))
295 br_multicast_del_pg(br
, pg
);
298 spin_unlock(&br
->multicast_lock
);
301 static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu
**mdbp
, int max
,
304 struct net_bridge_mdb_htable
*old
= rcu_dereference_protected(*mdbp
, 1);
305 struct net_bridge_mdb_htable
*mdb
;
308 mdb
= kmalloc(sizeof(*mdb
), GFP_ATOMIC
);
315 mdb
->mhash
= kzalloc(max
* sizeof(*mdb
->mhash
), GFP_ATOMIC
);
321 mdb
->size
= old
? old
->size
: 0;
322 mdb
->ver
= old
? old
->ver
^ 1 : 0;
324 if (!old
|| elasticity
)
325 get_random_bytes(&mdb
->secret
, sizeof(mdb
->secret
));
327 mdb
->secret
= old
->secret
;
332 err
= br_mdb_copy(mdb
, old
, elasticity
);
339 call_rcu_bh(&mdb
->rcu
, br_mdb_free
);
342 rcu_assign_pointer(*mdbp
, mdb
);
347 static struct sk_buff
*br_ip4_multicast_alloc_query(struct net_bridge
*br
,
355 skb
= netdev_alloc_skb_ip_align(br
->dev
, sizeof(*eth
) + sizeof(*iph
) +
360 skb
->protocol
= htons(ETH_P_IP
);
362 skb_reset_mac_header(skb
);
365 memcpy(eth
->h_source
, br
->dev
->dev_addr
, 6);
368 eth
->h_dest
[2] = 0x5e;
372 eth
->h_proto
= htons(ETH_P_IP
);
373 skb_put(skb
, sizeof(*eth
));
375 skb_set_network_header(skb
, skb
->len
);
381 iph
->tot_len
= htons(sizeof(*iph
) + sizeof(*ih
) + 4);
383 iph
->frag_off
= htons(IP_DF
);
385 iph
->protocol
= IPPROTO_IGMP
;
387 iph
->daddr
= htonl(INADDR_ALLHOSTS_GROUP
);
388 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
389 ((u8
*)&iph
[1])[1] = 4;
390 ((u8
*)&iph
[1])[2] = 0;
391 ((u8
*)&iph
[1])[3] = 0;
395 skb_set_transport_header(skb
, skb
->len
);
397 ih
->type
= IGMP_HOST_MEMBERSHIP_QUERY
;
398 ih
->code
= (group
? br
->multicast_last_member_interval
:
399 br
->multicast_query_response_interval
) /
400 (HZ
/ IGMP_TIMER_SCALE
);
403 ih
->csum
= ip_compute_csum((void *)ih
, sizeof(struct igmphdr
));
404 skb_put(skb
, sizeof(*ih
));
406 __skb_pull(skb
, sizeof(*eth
));
412 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
413 static struct sk_buff
*br_ip6_multicast_alloc_query(struct net_bridge
*br
,
414 const struct in6_addr
*group
)
417 struct ipv6hdr
*ip6h
;
418 struct mld_msg
*mldq
;
421 unsigned long interval
;
423 skb
= netdev_alloc_skb_ip_align(br
->dev
, sizeof(*eth
) + sizeof(*ip6h
) +
428 skb
->protocol
= htons(ETH_P_IPV6
);
430 /* Ethernet header */
431 skb_reset_mac_header(skb
);
434 memcpy(eth
->h_source
, br
->dev
->dev_addr
, 6);
435 eth
->h_proto
= htons(ETH_P_IPV6
);
436 skb_put(skb
, sizeof(*eth
));
438 /* IPv6 header + HbH option */
439 skb_set_network_header(skb
, skb
->len
);
440 ip6h
= ipv6_hdr(skb
);
442 *(__force __be32
*)ip6h
= htonl(0x60000000);
443 ip6h
->payload_len
= htons(8 + sizeof(*mldq
));
444 ip6h
->nexthdr
= IPPROTO_HOPOPTS
;
446 ipv6_addr_set(&ip6h
->daddr
, htonl(0xff020000), 0, 0, htonl(1));
447 if (ipv6_dev_get_saddr(dev_net(br
->dev
), br
->dev
, &ip6h
->daddr
, 0,
452 ipv6_eth_mc_map(&ip6h
->daddr
, eth
->h_dest
);
454 hopopt
= (u8
*)(ip6h
+ 1);
455 hopopt
[0] = IPPROTO_ICMPV6
; /* next hdr */
456 hopopt
[1] = 0; /* length of HbH */
457 hopopt
[2] = IPV6_TLV_ROUTERALERT
; /* Router Alert */
458 hopopt
[3] = 2; /* Length of RA Option */
459 hopopt
[4] = 0; /* Type = 0x0000 (MLD) */
461 hopopt
[6] = IPV6_TLV_PAD0
; /* Pad0 */
462 hopopt
[7] = IPV6_TLV_PAD0
; /* Pad0 */
464 skb_put(skb
, sizeof(*ip6h
) + 8);
467 skb_set_transport_header(skb
, skb
->len
);
468 mldq
= (struct mld_msg
*) icmp6_hdr(skb
);
470 interval
= ipv6_addr_any(group
) ? br
->multicast_last_member_interval
:
471 br
->multicast_query_response_interval
;
473 mldq
->mld_type
= ICMPV6_MGM_QUERY
;
476 mldq
->mld_maxdelay
= htons((u16
)jiffies_to_msecs(interval
));
477 mldq
->mld_reserved
= 0;
478 ipv6_addr_copy(&mldq
->mld_mca
, group
);
481 mldq
->mld_cksum
= csum_ipv6_magic(&ip6h
->saddr
, &ip6h
->daddr
,
482 sizeof(*mldq
), IPPROTO_ICMPV6
,
485 skb_put(skb
, sizeof(*mldq
));
487 __skb_pull(skb
, sizeof(*eth
));
494 static struct sk_buff
*br_multicast_alloc_query(struct net_bridge
*br
,
497 switch (addr
->proto
) {
498 case htons(ETH_P_IP
):
499 return br_ip4_multicast_alloc_query(br
, addr
->u
.ip4
);
500 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
501 case htons(ETH_P_IPV6
):
502 return br_ip6_multicast_alloc_query(br
, &addr
->u
.ip6
);
508 static struct net_bridge_mdb_entry
*br_multicast_get_group(
509 struct net_bridge
*br
, struct net_bridge_port
*port
,
510 struct br_ip
*group
, int hash
)
512 struct net_bridge_mdb_htable
*mdb
;
513 struct net_bridge_mdb_entry
*mp
;
514 struct hlist_node
*p
;
520 mdb
= rcu_dereference_protected(br
->mdb
, 1);
521 hlist_for_each_entry(mp
, p
, &mdb
->mhash
[hash
], hlist
[mdb
->ver
]) {
523 if (unlikely(br_ip_equal(group
, &mp
->addr
)))
530 if (unlikely(count
> br
->hash_elasticity
&& count
)) {
532 br_info(br
, "Multicast hash table "
533 "chain limit reached: %s\n",
534 port
? port
->dev
->name
: br
->dev
->name
);
536 elasticity
= br
->hash_elasticity
;
539 if (mdb
->size
>= max
) {
541 if (unlikely(max
>= br
->hash_max
)) {
542 br_warn(br
, "Multicast hash table maximum "
543 "reached, disabling snooping: %s, %d\n",
544 port
? port
->dev
->name
: br
->dev
->name
, max
);
547 br
->multicast_disabled
= 1;
552 if (max
> mdb
->max
|| elasticity
) {
555 br_info(br
, "Multicast hash table "
557 port
? port
->dev
->name
: br
->dev
->name
);
562 err
= br_mdb_rehash(&br
->mdb
, max
, elasticity
);
564 br_warn(br
, "Cannot rehash multicast "
565 "hash table, disabling snooping: %s, %d, %d\n",
566 port
? port
->dev
->name
: br
->dev
->name
,
582 static struct net_bridge_mdb_entry
*br_multicast_new_group(
583 struct net_bridge
*br
, struct net_bridge_port
*port
,
586 struct net_bridge_mdb_htable
*mdb
;
587 struct net_bridge_mdb_entry
*mp
;
591 mdb
= rcu_dereference_protected(br
->mdb
, 1);
593 err
= br_mdb_rehash(&br
->mdb
, BR_HASH_SIZE
, 0);
599 hash
= br_ip_hash(mdb
, group
);
600 mp
= br_multicast_get_group(br
, port
, group
, hash
);
601 switch (PTR_ERR(mp
)) {
607 mdb
= rcu_dereference_protected(br
->mdb
, 1);
608 hash
= br_ip_hash(mdb
, group
);
615 mp
= kzalloc(sizeof(*mp
), GFP_ATOMIC
);
617 return ERR_PTR(-ENOMEM
);
621 setup_timer(&mp
->timer
, br_multicast_group_expired
,
624 hlist_add_head_rcu(&mp
->hlist
[mdb
->ver
], &mdb
->mhash
[hash
]);
631 static int br_multicast_add_group(struct net_bridge
*br
,
632 struct net_bridge_port
*port
,
635 struct net_bridge_mdb_entry
*mp
;
636 struct net_bridge_port_group
*p
;
637 struct net_bridge_port_group __rcu
**pp
;
638 unsigned long now
= jiffies
;
641 spin_lock(&br
->multicast_lock
);
642 if (!netif_running(br
->dev
) ||
643 (port
&& port
->state
== BR_STATE_DISABLED
))
646 mp
= br_multicast_new_group(br
, port
, group
);
653 mod_timer(&mp
->timer
, now
+ br
->multicast_membership_interval
);
657 for (pp
= &mp
->ports
;
658 (p
= mlock_dereference(*pp
, br
)) != NULL
;
662 if ((unsigned long)p
->port
< (unsigned long)port
)
666 p
= kzalloc(sizeof(*p
), GFP_ATOMIC
);
674 hlist_add_head(&p
->mglist
, &port
->mglist
);
675 setup_timer(&p
->timer
, br_multicast_port_group_expired
,
678 rcu_assign_pointer(*pp
, p
);
681 mod_timer(&p
->timer
, now
+ br
->multicast_membership_interval
);
686 spin_unlock(&br
->multicast_lock
);
690 static int br_ip4_multicast_add_group(struct net_bridge
*br
,
691 struct net_bridge_port
*port
,
694 struct br_ip br_group
;
696 if (ipv4_is_local_multicast(group
))
699 br_group
.u
.ip4
= group
;
700 br_group
.proto
= htons(ETH_P_IP
);
702 return br_multicast_add_group(br
, port
, &br_group
);
705 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
706 static int br_ip6_multicast_add_group(struct net_bridge
*br
,
707 struct net_bridge_port
*port
,
708 const struct in6_addr
*group
)
710 struct br_ip br_group
;
712 if (!ipv6_is_transient_multicast(group
))
715 ipv6_addr_copy(&br_group
.u
.ip6
, group
);
716 br_group
.proto
= htons(ETH_P_IPV6
);
718 return br_multicast_add_group(br
, port
, &br_group
);
722 static void br_multicast_router_expired(unsigned long data
)
724 struct net_bridge_port
*port
= (void *)data
;
725 struct net_bridge
*br
= port
->br
;
727 spin_lock(&br
->multicast_lock
);
728 if (port
->multicast_router
!= 1 ||
729 timer_pending(&port
->multicast_router_timer
) ||
730 hlist_unhashed(&port
->rlist
))
733 hlist_del_init_rcu(&port
->rlist
);
736 spin_unlock(&br
->multicast_lock
);
739 static void br_multicast_local_router_expired(unsigned long data
)
743 static void __br_multicast_send_query(struct net_bridge
*br
,
744 struct net_bridge_port
*port
,
749 skb
= br_multicast_alloc_query(br
, ip
);
754 __skb_push(skb
, sizeof(struct ethhdr
));
755 skb
->dev
= port
->dev
;
756 NF_HOOK(NFPROTO_BRIDGE
, NF_BR_LOCAL_OUT
, skb
, NULL
, skb
->dev
,
762 static void br_multicast_send_query(struct net_bridge
*br
,
763 struct net_bridge_port
*port
, u32 sent
)
766 struct br_ip br_group
;
768 if (!netif_running(br
->dev
) || br
->multicast_disabled
||
769 timer_pending(&br
->multicast_querier_timer
))
772 memset(&br_group
.u
, 0, sizeof(br_group
.u
));
774 br_group
.proto
= htons(ETH_P_IP
);
775 __br_multicast_send_query(br
, port
, &br_group
);
777 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
778 br_group
.proto
= htons(ETH_P_IPV6
);
779 __br_multicast_send_query(br
, port
, &br_group
);
783 time
+= sent
< br
->multicast_startup_query_count
?
784 br
->multicast_startup_query_interval
:
785 br
->multicast_query_interval
;
786 mod_timer(port
? &port
->multicast_query_timer
:
787 &br
->multicast_query_timer
, time
);
790 static void br_multicast_port_query_expired(unsigned long data
)
792 struct net_bridge_port
*port
= (void *)data
;
793 struct net_bridge
*br
= port
->br
;
795 spin_lock(&br
->multicast_lock
);
796 if (port
->state
== BR_STATE_DISABLED
||
797 port
->state
== BR_STATE_BLOCKING
)
800 if (port
->multicast_startup_queries_sent
<
801 br
->multicast_startup_query_count
)
802 port
->multicast_startup_queries_sent
++;
804 br_multicast_send_query(port
->br
, port
,
805 port
->multicast_startup_queries_sent
);
808 spin_unlock(&br
->multicast_lock
);
811 void br_multicast_add_port(struct net_bridge_port
*port
)
813 port
->multicast_router
= 1;
815 setup_timer(&port
->multicast_router_timer
, br_multicast_router_expired
,
816 (unsigned long)port
);
817 setup_timer(&port
->multicast_query_timer
,
818 br_multicast_port_query_expired
, (unsigned long)port
);
821 void br_multicast_del_port(struct net_bridge_port
*port
)
823 del_timer_sync(&port
->multicast_router_timer
);
826 static void __br_multicast_enable_port(struct net_bridge_port
*port
)
828 port
->multicast_startup_queries_sent
= 0;
830 if (try_to_del_timer_sync(&port
->multicast_query_timer
) >= 0 ||
831 del_timer(&port
->multicast_query_timer
))
832 mod_timer(&port
->multicast_query_timer
, jiffies
);
835 void br_multicast_enable_port(struct net_bridge_port
*port
)
837 struct net_bridge
*br
= port
->br
;
839 spin_lock(&br
->multicast_lock
);
840 if (br
->multicast_disabled
|| !netif_running(br
->dev
))
843 __br_multicast_enable_port(port
);
846 spin_unlock(&br
->multicast_lock
);
849 void br_multicast_disable_port(struct net_bridge_port
*port
)
851 struct net_bridge
*br
= port
->br
;
852 struct net_bridge_port_group
*pg
;
853 struct hlist_node
*p
, *n
;
855 spin_lock(&br
->multicast_lock
);
856 hlist_for_each_entry_safe(pg
, p
, n
, &port
->mglist
, mglist
)
857 br_multicast_del_pg(br
, pg
);
859 if (!hlist_unhashed(&port
->rlist
))
860 hlist_del_init_rcu(&port
->rlist
);
861 del_timer(&port
->multicast_router_timer
);
862 del_timer(&port
->multicast_query_timer
);
863 spin_unlock(&br
->multicast_lock
);
866 static int br_ip4_multicast_igmp3_report(struct net_bridge
*br
,
867 struct net_bridge_port
*port
,
870 struct igmpv3_report
*ih
;
871 struct igmpv3_grec
*grec
;
879 if (!pskb_may_pull(skb
, sizeof(*ih
)))
882 ih
= igmpv3_report_hdr(skb
);
883 num
= ntohs(ih
->ngrec
);
886 for (i
= 0; i
< num
; i
++) {
887 len
+= sizeof(*grec
);
888 if (!pskb_may_pull(skb
, len
))
891 grec
= (void *)(skb
->data
+ len
- sizeof(*grec
));
892 group
= grec
->grec_mca
;
893 type
= grec
->grec_type
;
895 len
+= ntohs(grec
->grec_nsrcs
) * 4;
896 if (!pskb_may_pull(skb
, len
))
899 /* We treat this as an IGMPv2 report for now. */
901 case IGMPV3_MODE_IS_INCLUDE
:
902 case IGMPV3_MODE_IS_EXCLUDE
:
903 case IGMPV3_CHANGE_TO_INCLUDE
:
904 case IGMPV3_CHANGE_TO_EXCLUDE
:
905 case IGMPV3_ALLOW_NEW_SOURCES
:
906 case IGMPV3_BLOCK_OLD_SOURCES
:
913 err
= br_ip4_multicast_add_group(br
, port
, group
);
921 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
922 static int br_ip6_multicast_mld2_report(struct net_bridge
*br
,
923 struct net_bridge_port
*port
,
926 struct icmp6hdr
*icmp6h
;
927 struct mld2_grec
*grec
;
933 if (!pskb_may_pull(skb
, sizeof(*icmp6h
)))
936 icmp6h
= icmp6_hdr(skb
);
937 num
= ntohs(icmp6h
->icmp6_dataun
.un_data16
[1]);
938 len
= sizeof(*icmp6h
);
940 for (i
= 0; i
< num
; i
++) {
941 __be16
*nsrcs
, _nsrcs
;
943 nsrcs
= skb_header_pointer(skb
,
944 len
+ offsetof(struct mld2_grec
,
946 sizeof(_nsrcs
), &_nsrcs
);
950 if (!pskb_may_pull(skb
,
951 len
+ sizeof(*grec
) +
952 sizeof(struct in6_addr
) * ntohs(*nsrcs
)))
955 grec
= (struct mld2_grec
*)(skb
->data
+ len
);
956 len
+= sizeof(*grec
) +
957 sizeof(struct in6_addr
) * ntohs(*nsrcs
);
959 /* We treat these as MLDv1 reports for now. */
960 switch (grec
->grec_type
) {
961 case MLD2_MODE_IS_INCLUDE
:
962 case MLD2_MODE_IS_EXCLUDE
:
963 case MLD2_CHANGE_TO_INCLUDE
:
964 case MLD2_CHANGE_TO_EXCLUDE
:
965 case MLD2_ALLOW_NEW_SOURCES
:
966 case MLD2_BLOCK_OLD_SOURCES
:
973 err
= br_ip6_multicast_add_group(br
, port
, &grec
->grec_mca
);
983 * Add port to rotuer_list
984 * list is maintained ordered by pointer value
985 * and locked by br->multicast_lock and RCU
987 static void br_multicast_add_router(struct net_bridge
*br
,
988 struct net_bridge_port
*port
)
990 struct net_bridge_port
*p
;
991 struct hlist_node
*n
, *slot
= NULL
;
993 hlist_for_each_entry(p
, n
, &br
->router_list
, rlist
) {
994 if ((unsigned long) port
>= (unsigned long) p
)
1000 hlist_add_after_rcu(slot
, &port
->rlist
);
1002 hlist_add_head_rcu(&port
->rlist
, &br
->router_list
);
1005 static void br_multicast_mark_router(struct net_bridge
*br
,
1006 struct net_bridge_port
*port
)
1008 unsigned long now
= jiffies
;
1011 if (br
->multicast_router
== 1)
1012 mod_timer(&br
->multicast_router_timer
,
1013 now
+ br
->multicast_querier_interval
);
1017 if (port
->multicast_router
!= 1)
1020 if (!hlist_unhashed(&port
->rlist
))
1023 br_multicast_add_router(br
, port
);
1026 mod_timer(&port
->multicast_router_timer
,
1027 now
+ br
->multicast_querier_interval
);
1030 static void br_multicast_query_received(struct net_bridge
*br
,
1031 struct net_bridge_port
*port
,
1035 mod_timer(&br
->multicast_querier_timer
,
1036 jiffies
+ br
->multicast_querier_interval
);
1037 else if (timer_pending(&br
->multicast_querier_timer
))
1040 br_multicast_mark_router(br
, port
);
1043 static int br_ip4_multicast_query(struct net_bridge
*br
,
1044 struct net_bridge_port
*port
,
1045 struct sk_buff
*skb
)
1047 const struct iphdr
*iph
= ip_hdr(skb
);
1048 struct igmphdr
*ih
= igmp_hdr(skb
);
1049 struct net_bridge_mdb_entry
*mp
;
1050 struct igmpv3_query
*ih3
;
1051 struct net_bridge_port_group
*p
;
1052 struct net_bridge_port_group __rcu
**pp
;
1053 unsigned long max_delay
;
1054 unsigned long now
= jiffies
;
1058 spin_lock(&br
->multicast_lock
);
1059 if (!netif_running(br
->dev
) ||
1060 (port
&& port
->state
== BR_STATE_DISABLED
))
1063 br_multicast_query_received(br
, port
, !!iph
->saddr
);
1067 if (skb
->len
== sizeof(*ih
)) {
1068 max_delay
= ih
->code
* (HZ
/ IGMP_TIMER_SCALE
);
1071 max_delay
= 10 * HZ
;
1075 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
))) {
1080 ih3
= igmpv3_query_hdr(skb
);
1084 max_delay
= ih3
->code
?
1085 IGMPV3_MRC(ih3
->code
) * (HZ
/ IGMP_TIMER_SCALE
) : 1;
1091 mp
= br_mdb_ip4_get(mlock_dereference(br
->mdb
, br
), group
);
1095 max_delay
*= br
->multicast_last_member_count
;
1098 (timer_pending(&mp
->timer
) ?
1099 time_after(mp
->timer
.expires
, now
+ max_delay
) :
1100 try_to_del_timer_sync(&mp
->timer
) >= 0))
1101 mod_timer(&mp
->timer
, now
+ max_delay
);
1103 for (pp
= &mp
->ports
;
1104 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1106 if (timer_pending(&p
->timer
) ?
1107 time_after(p
->timer
.expires
, now
+ max_delay
) :
1108 try_to_del_timer_sync(&p
->timer
) >= 0)
1109 mod_timer(&p
->timer
, now
+ max_delay
);
1113 spin_unlock(&br
->multicast_lock
);
1117 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1118 static int br_ip6_multicast_query(struct net_bridge
*br
,
1119 struct net_bridge_port
*port
,
1120 struct sk_buff
*skb
)
1122 const struct ipv6hdr
*ip6h
= ipv6_hdr(skb
);
1123 struct mld_msg
*mld
= (struct mld_msg
*) icmp6_hdr(skb
);
1124 struct net_bridge_mdb_entry
*mp
;
1125 struct mld2_query
*mld2q
;
1126 struct net_bridge_port_group
*p
;
1127 struct net_bridge_port_group __rcu
**pp
;
1128 unsigned long max_delay
;
1129 unsigned long now
= jiffies
;
1130 const struct in6_addr
*group
= NULL
;
1133 spin_lock(&br
->multicast_lock
);
1134 if (!netif_running(br
->dev
) ||
1135 (port
&& port
->state
== BR_STATE_DISABLED
))
1138 br_multicast_query_received(br
, port
, !ipv6_addr_any(&ip6h
->saddr
));
1140 if (skb
->len
== sizeof(*mld
)) {
1141 if (!pskb_may_pull(skb
, sizeof(*mld
))) {
1145 mld
= (struct mld_msg
*) icmp6_hdr(skb
);
1146 max_delay
= msecs_to_jiffies(htons(mld
->mld_maxdelay
));
1148 group
= &mld
->mld_mca
;
1149 } else if (skb
->len
>= sizeof(*mld2q
)) {
1150 if (!pskb_may_pull(skb
, sizeof(*mld2q
))) {
1154 mld2q
= (struct mld2_query
*)icmp6_hdr(skb
);
1155 if (!mld2q
->mld2q_nsrcs
)
1156 group
= &mld2q
->mld2q_mca
;
1157 max_delay
= mld2q
->mld2q_mrc
? MLDV2_MRC(mld2q
->mld2q_mrc
) : 1;
1163 mp
= br_mdb_ip6_get(mlock_dereference(br
->mdb
, br
), group
);
1167 max_delay
*= br
->multicast_last_member_count
;
1169 (timer_pending(&mp
->timer
) ?
1170 time_after(mp
->timer
.expires
, now
+ max_delay
) :
1171 try_to_del_timer_sync(&mp
->timer
) >= 0))
1172 mod_timer(&mp
->timer
, now
+ max_delay
);
1174 for (pp
= &mp
->ports
;
1175 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1177 if (timer_pending(&p
->timer
) ?
1178 time_after(p
->timer
.expires
, now
+ max_delay
) :
1179 try_to_del_timer_sync(&p
->timer
) >= 0)
1180 mod_timer(&p
->timer
, now
+ max_delay
);
1184 spin_unlock(&br
->multicast_lock
);
1189 static void br_multicast_leave_group(struct net_bridge
*br
,
1190 struct net_bridge_port
*port
,
1191 struct br_ip
*group
)
1193 struct net_bridge_mdb_htable
*mdb
;
1194 struct net_bridge_mdb_entry
*mp
;
1195 struct net_bridge_port_group
*p
;
1199 spin_lock(&br
->multicast_lock
);
1200 if (!netif_running(br
->dev
) ||
1201 (port
&& port
->state
== BR_STATE_DISABLED
) ||
1202 timer_pending(&br
->multicast_querier_timer
))
1205 mdb
= mlock_dereference(br
->mdb
, br
);
1206 mp
= br_mdb_ip_get(mdb
, group
);
1211 time
= now
+ br
->multicast_last_member_count
*
1212 br
->multicast_last_member_interval
;
1216 (timer_pending(&mp
->timer
) ?
1217 time_after(mp
->timer
.expires
, time
) :
1218 try_to_del_timer_sync(&mp
->timer
) >= 0)) {
1219 mod_timer(&mp
->timer
, time
);
1225 for (p
= mlock_dereference(mp
->ports
, br
);
1227 p
= mlock_dereference(p
->next
, br
)) {
1228 if (p
->port
!= port
)
1231 if (!hlist_unhashed(&p
->mglist
) &&
1232 (timer_pending(&p
->timer
) ?
1233 time_after(p
->timer
.expires
, time
) :
1234 try_to_del_timer_sync(&p
->timer
) >= 0)) {
1235 mod_timer(&p
->timer
, time
);
1242 spin_unlock(&br
->multicast_lock
);
1245 static void br_ip4_multicast_leave_group(struct net_bridge
*br
,
1246 struct net_bridge_port
*port
,
1249 struct br_ip br_group
;
1251 if (ipv4_is_local_multicast(group
))
1254 br_group
.u
.ip4
= group
;
1255 br_group
.proto
= htons(ETH_P_IP
);
1257 br_multicast_leave_group(br
, port
, &br_group
);
1260 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1261 static void br_ip6_multicast_leave_group(struct net_bridge
*br
,
1262 struct net_bridge_port
*port
,
1263 const struct in6_addr
*group
)
1265 struct br_ip br_group
;
1267 if (!ipv6_is_transient_multicast(group
))
1270 ipv6_addr_copy(&br_group
.u
.ip6
, group
);
1271 br_group
.proto
= htons(ETH_P_IPV6
);
1273 br_multicast_leave_group(br
, port
, &br_group
);
1277 static int br_multicast_ipv4_rcv(struct net_bridge
*br
,
1278 struct net_bridge_port
*port
,
1279 struct sk_buff
*skb
)
1281 struct sk_buff
*skb2
= skb
;
1282 const struct iphdr
*iph
;
1288 /* We treat OOM as packet loss for now. */
1289 if (!pskb_may_pull(skb
, sizeof(*iph
)))
1294 if (iph
->ihl
< 5 || iph
->version
!= 4)
1297 if (!pskb_may_pull(skb
, ip_hdrlen(skb
)))
1302 if (unlikely(ip_fast_csum((u8
*)iph
, iph
->ihl
)))
1305 if (iph
->protocol
!= IPPROTO_IGMP
) {
1306 if ((iph
->daddr
& IGMP_LOCAL_GROUP_MASK
) != IGMP_LOCAL_GROUP
)
1307 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1311 len
= ntohs(iph
->tot_len
);
1312 if (skb
->len
< len
|| len
< ip_hdrlen(skb
))
1315 if (skb
->len
> len
) {
1316 skb2
= skb_clone(skb
, GFP_ATOMIC
);
1320 err
= pskb_trim_rcsum(skb2
, len
);
1325 len
-= ip_hdrlen(skb2
);
1326 offset
= skb_network_offset(skb2
) + ip_hdrlen(skb2
);
1327 __skb_pull(skb2
, offset
);
1328 skb_reset_transport_header(skb2
);
1331 if (!pskb_may_pull(skb2
, sizeof(*ih
)))
1334 switch (skb2
->ip_summed
) {
1335 case CHECKSUM_COMPLETE
:
1336 if (!csum_fold(skb2
->csum
))
1341 if (skb_checksum_complete(skb2
))
1347 BR_INPUT_SKB_CB(skb
)->igmp
= 1;
1348 ih
= igmp_hdr(skb2
);
1351 case IGMP_HOST_MEMBERSHIP_REPORT
:
1352 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1353 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1354 err
= br_ip4_multicast_add_group(br
, port
, ih
->group
);
1356 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1357 err
= br_ip4_multicast_igmp3_report(br
, port
, skb2
);
1359 case IGMP_HOST_MEMBERSHIP_QUERY
:
1360 err
= br_ip4_multicast_query(br
, port
, skb2
);
1362 case IGMP_HOST_LEAVE_MESSAGE
:
1363 br_ip4_multicast_leave_group(br
, port
, ih
->group
);
1368 __skb_push(skb2
, offset
);
1375 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1376 static int br_multicast_ipv6_rcv(struct net_bridge
*br
,
1377 struct net_bridge_port
*port
,
1378 struct sk_buff
*skb
)
1380 struct sk_buff
*skb2
;
1381 const struct ipv6hdr
*ip6h
;
1388 if (!pskb_may_pull(skb
, sizeof(*ip6h
)))
1391 ip6h
= ipv6_hdr(skb
);
1394 * We're interested in MLD messages only.
1396 * - MLD has always Router Alert hop-by-hop option
1397 * - But we do not support jumbrograms.
1399 if (ip6h
->version
!= 6 ||
1400 ip6h
->nexthdr
!= IPPROTO_HOPOPTS
||
1401 ip6h
->payload_len
== 0)
1404 len
= ntohs(ip6h
->payload_len
) + sizeof(*ip6h
);
1408 nexthdr
= ip6h
->nexthdr
;
1409 offset
= ipv6_skip_exthdr(skb
, sizeof(*ip6h
), &nexthdr
);
1411 if (offset
< 0 || nexthdr
!= IPPROTO_ICMPV6
)
1414 /* Okay, we found ICMPv6 header */
1415 skb2
= skb_clone(skb
, GFP_ATOMIC
);
1420 if (!pskb_may_pull(skb2
, offset
+ sizeof(struct icmp6hdr
)))
1423 len
-= offset
- skb_network_offset(skb2
);
1425 __skb_pull(skb2
, offset
);
1426 skb_reset_transport_header(skb2
);
1428 icmp6_type
= icmp6_hdr(skb2
)->icmp6_type
;
1430 switch (icmp6_type
) {
1431 case ICMPV6_MGM_QUERY
:
1432 case ICMPV6_MGM_REPORT
:
1433 case ICMPV6_MGM_REDUCTION
:
1434 case ICMPV6_MLD2_REPORT
:
1441 /* Okay, we found MLD message. Check further. */
1442 if (skb2
->len
> len
) {
1443 err
= pskb_trim_rcsum(skb2
, len
);
1449 ip6h
= ipv6_hdr(skb2
);
1451 switch (skb2
->ip_summed
) {
1452 case CHECKSUM_COMPLETE
:
1453 if (!csum_ipv6_magic(&ip6h
->saddr
, &ip6h
->daddr
, skb2
->len
,
1454 IPPROTO_ICMPV6
, skb2
->csum
))
1458 skb2
->csum
= ~csum_unfold(csum_ipv6_magic(&ip6h
->saddr
,
1461 IPPROTO_ICMPV6
, 0));
1462 if (__skb_checksum_complete(skb2
))
1468 BR_INPUT_SKB_CB(skb
)->igmp
= 1;
1470 switch (icmp6_type
) {
1471 case ICMPV6_MGM_REPORT
:
1473 struct mld_msg
*mld
;
1474 if (!pskb_may_pull(skb2
, sizeof(*mld
))) {
1478 mld
= (struct mld_msg
*)skb_transport_header(skb2
);
1479 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1480 err
= br_ip6_multicast_add_group(br
, port
, &mld
->mld_mca
);
1483 case ICMPV6_MLD2_REPORT
:
1484 err
= br_ip6_multicast_mld2_report(br
, port
, skb2
);
1486 case ICMPV6_MGM_QUERY
:
1487 err
= br_ip6_multicast_query(br
, port
, skb2
);
1489 case ICMPV6_MGM_REDUCTION
:
1491 struct mld_msg
*mld
;
1492 if (!pskb_may_pull(skb2
, sizeof(*mld
))) {
1496 mld
= (struct mld_msg
*)skb_transport_header(skb2
);
1497 br_ip6_multicast_leave_group(br
, port
, &mld
->mld_mca
);
1507 int br_multicast_rcv(struct net_bridge
*br
, struct net_bridge_port
*port
,
1508 struct sk_buff
*skb
)
1510 BR_INPUT_SKB_CB(skb
)->igmp
= 0;
1511 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 0;
1513 if (br
->multicast_disabled
)
1516 switch (skb
->protocol
) {
1517 case htons(ETH_P_IP
):
1518 return br_multicast_ipv4_rcv(br
, port
, skb
);
1519 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1520 case htons(ETH_P_IPV6
):
1521 return br_multicast_ipv6_rcv(br
, port
, skb
);
1528 static void br_multicast_query_expired(unsigned long data
)
1530 struct net_bridge
*br
= (void *)data
;
1532 spin_lock(&br
->multicast_lock
);
1533 if (br
->multicast_startup_queries_sent
<
1534 br
->multicast_startup_query_count
)
1535 br
->multicast_startup_queries_sent
++;
1537 br_multicast_send_query(br
, NULL
, br
->multicast_startup_queries_sent
);
1539 spin_unlock(&br
->multicast_lock
);
1542 void br_multicast_init(struct net_bridge
*br
)
1544 br
->hash_elasticity
= 4;
1547 br
->multicast_router
= 1;
1548 br
->multicast_last_member_count
= 2;
1549 br
->multicast_startup_query_count
= 2;
1551 br
->multicast_last_member_interval
= HZ
;
1552 br
->multicast_query_response_interval
= 10 * HZ
;
1553 br
->multicast_startup_query_interval
= 125 * HZ
/ 4;
1554 br
->multicast_query_interval
= 125 * HZ
;
1555 br
->multicast_querier_interval
= 255 * HZ
;
1556 br
->multicast_membership_interval
= 260 * HZ
;
1558 spin_lock_init(&br
->multicast_lock
);
1559 setup_timer(&br
->multicast_router_timer
,
1560 br_multicast_local_router_expired
, 0);
1561 setup_timer(&br
->multicast_querier_timer
,
1562 br_multicast_local_router_expired
, 0);
1563 setup_timer(&br
->multicast_query_timer
, br_multicast_query_expired
,
1567 void br_multicast_open(struct net_bridge
*br
)
1569 br
->multicast_startup_queries_sent
= 0;
1571 if (br
->multicast_disabled
)
1574 mod_timer(&br
->multicast_query_timer
, jiffies
);
1577 void br_multicast_stop(struct net_bridge
*br
)
1579 struct net_bridge_mdb_htable
*mdb
;
1580 struct net_bridge_mdb_entry
*mp
;
1581 struct hlist_node
*p
, *n
;
1585 del_timer_sync(&br
->multicast_router_timer
);
1586 del_timer_sync(&br
->multicast_querier_timer
);
1587 del_timer_sync(&br
->multicast_query_timer
);
1589 spin_lock_bh(&br
->multicast_lock
);
1590 mdb
= mlock_dereference(br
->mdb
, br
);
1597 for (i
= 0; i
< mdb
->max
; i
++) {
1598 hlist_for_each_entry_safe(mp
, p
, n
, &mdb
->mhash
[i
],
1600 del_timer(&mp
->timer
);
1601 call_rcu_bh(&mp
->rcu
, br_multicast_free_group
);
1606 spin_unlock_bh(&br
->multicast_lock
);
1608 spin_lock_bh(&br
->multicast_lock
);
1613 call_rcu_bh(&mdb
->rcu
, br_mdb_free
);
1616 spin_unlock_bh(&br
->multicast_lock
);
1619 int br_multicast_set_router(struct net_bridge
*br
, unsigned long val
)
1623 spin_lock_bh(&br
->multicast_lock
);
1624 if (!netif_running(br
->dev
))
1630 del_timer(&br
->multicast_router_timer
);
1633 br
->multicast_router
= val
;
1643 spin_unlock_bh(&br
->multicast_lock
);
1648 int br_multicast_set_port_router(struct net_bridge_port
*p
, unsigned long val
)
1650 struct net_bridge
*br
= p
->br
;
1653 spin_lock(&br
->multicast_lock
);
1654 if (!netif_running(br
->dev
) || p
->state
== BR_STATE_DISABLED
)
1661 p
->multicast_router
= val
;
1664 if (val
< 2 && !hlist_unhashed(&p
->rlist
))
1665 hlist_del_init_rcu(&p
->rlist
);
1670 del_timer(&p
->multicast_router_timer
);
1675 br_multicast_add_router(br
, p
);
1684 spin_unlock(&br
->multicast_lock
);
1689 int br_multicast_toggle(struct net_bridge
*br
, unsigned long val
)
1691 struct net_bridge_port
*port
;
1693 struct net_bridge_mdb_htable
*mdb
;
1695 spin_lock(&br
->multicast_lock
);
1696 if (br
->multicast_disabled
== !val
)
1699 br
->multicast_disabled
= !val
;
1700 if (br
->multicast_disabled
)
1703 if (!netif_running(br
->dev
))
1706 mdb
= mlock_dereference(br
->mdb
, br
);
1711 br
->multicast_disabled
= !!val
;
1715 err
= br_mdb_rehash(&br
->mdb
, mdb
->max
,
1716 br
->hash_elasticity
);
1721 br_multicast_open(br
);
1722 list_for_each_entry(port
, &br
->port_list
, list
) {
1723 if (port
->state
== BR_STATE_DISABLED
||
1724 port
->state
== BR_STATE_BLOCKING
)
1727 __br_multicast_enable_port(port
);
1731 spin_unlock(&br
->multicast_lock
);
1736 int br_multicast_set_hash_max(struct net_bridge
*br
, unsigned long val
)
1740 struct net_bridge_mdb_htable
*mdb
;
1742 spin_lock(&br
->multicast_lock
);
1743 if (!netif_running(br
->dev
))
1747 if (!is_power_of_2(val
))
1750 mdb
= mlock_dereference(br
->mdb
, br
);
1751 if (mdb
&& val
< mdb
->size
)
1767 err
= br_mdb_rehash(&br
->mdb
, br
->hash_max
,
1768 br
->hash_elasticity
);
1774 spin_unlock(&br
->multicast_lock
);