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 unsigned int br_mdb_rehash_seq
;
42 static inline int br_ip_equal(const struct br_ip
*a
, const struct br_ip
*b
)
44 if (a
->proto
!= b
->proto
)
50 return a
->u
.ip4
== b
->u
.ip4
;
51 #if IS_ENABLED(CONFIG_IPV6)
52 case htons(ETH_P_IPV6
):
53 return ipv6_addr_equal(&a
->u
.ip6
, &b
->u
.ip6
);
59 static inline int __br_ip4_hash(struct net_bridge_mdb_htable
*mdb
, __be32 ip
,
62 return jhash_2words((__force u32
)ip
, vid
, mdb
->secret
) & (mdb
->max
- 1);
65 #if IS_ENABLED(CONFIG_IPV6)
66 static inline int __br_ip6_hash(struct net_bridge_mdb_htable
*mdb
,
67 const struct in6_addr
*ip
,
70 return jhash_2words(ipv6_addr_hash(ip
), vid
,
71 mdb
->secret
) & (mdb
->max
- 1);
75 static inline int br_ip_hash(struct net_bridge_mdb_htable
*mdb
,
80 return __br_ip4_hash(mdb
, ip
->u
.ip4
, ip
->vid
);
81 #if IS_ENABLED(CONFIG_IPV6)
82 case htons(ETH_P_IPV6
):
83 return __br_ip6_hash(mdb
, &ip
->u
.ip6
, ip
->vid
);
89 static struct net_bridge_mdb_entry
*__br_mdb_ip_get(
90 struct net_bridge_mdb_htable
*mdb
, struct br_ip
*dst
, int hash
)
92 struct net_bridge_mdb_entry
*mp
;
94 hlist_for_each_entry_rcu(mp
, &mdb
->mhash
[hash
], hlist
[mdb
->ver
]) {
95 if (br_ip_equal(&mp
->addr
, dst
))
102 struct net_bridge_mdb_entry
*br_mdb_ip_get(struct net_bridge_mdb_htable
*mdb
,
108 return __br_mdb_ip_get(mdb
, dst
, br_ip_hash(mdb
, dst
));
111 static struct net_bridge_mdb_entry
*br_mdb_ip4_get(
112 struct net_bridge_mdb_htable
*mdb
, __be32 dst
, __u16 vid
)
117 br_dst
.proto
= htons(ETH_P_IP
);
120 return br_mdb_ip_get(mdb
, &br_dst
);
123 #if IS_ENABLED(CONFIG_IPV6)
124 static struct net_bridge_mdb_entry
*br_mdb_ip6_get(
125 struct net_bridge_mdb_htable
*mdb
, const struct in6_addr
*dst
,
131 br_dst
.proto
= htons(ETH_P_IPV6
);
134 return br_mdb_ip_get(mdb
, &br_dst
);
138 struct net_bridge_mdb_entry
*br_mdb_get(struct net_bridge
*br
,
139 struct sk_buff
*skb
, u16 vid
)
141 struct net_bridge_mdb_htable
*mdb
= rcu_dereference(br
->mdb
);
144 if (br
->multicast_disabled
)
147 if (BR_INPUT_SKB_CB(skb
)->igmp
)
150 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
;
189 for (i
= 0; i
< old
->max
; i
++)
190 hlist_for_each_entry(mp
, &old
->mhash
[i
], hlist
[old
->ver
])
191 hlist_add_head(&mp
->hlist
[new->ver
],
192 &new->mhash
[br_ip_hash(new, &mp
->addr
)]);
198 for (i
= 0; i
< new->max
; i
++) {
200 hlist_for_each_entry(mp
, &new->mhash
[i
], hlist
[new->ver
])
206 return maxlen
> elasticity
? -EINVAL
: 0;
209 void br_multicast_free_pg(struct rcu_head
*head
)
211 struct net_bridge_port_group
*p
=
212 container_of(head
, struct net_bridge_port_group
, rcu
);
217 static void br_multicast_free_group(struct rcu_head
*head
)
219 struct net_bridge_mdb_entry
*mp
=
220 container_of(head
, struct net_bridge_mdb_entry
, rcu
);
225 static void br_multicast_group_expired(unsigned long data
)
227 struct net_bridge_mdb_entry
*mp
= (void *)data
;
228 struct net_bridge
*br
= mp
->br
;
229 struct net_bridge_mdb_htable
*mdb
;
231 spin_lock(&br
->multicast_lock
);
232 if (!netif_running(br
->dev
) || timer_pending(&mp
->timer
))
240 mdb
= mlock_dereference(br
->mdb
, br
);
242 hlist_del_rcu(&mp
->hlist
[mdb
->ver
]);
245 call_rcu_bh(&mp
->rcu
, br_multicast_free_group
);
248 spin_unlock(&br
->multicast_lock
);
251 static void br_multicast_del_pg(struct net_bridge
*br
,
252 struct net_bridge_port_group
*pg
)
254 struct net_bridge_mdb_htable
*mdb
;
255 struct net_bridge_mdb_entry
*mp
;
256 struct net_bridge_port_group
*p
;
257 struct net_bridge_port_group __rcu
**pp
;
259 mdb
= mlock_dereference(br
->mdb
, br
);
261 mp
= br_mdb_ip_get(mdb
, &pg
->addr
);
265 for (pp
= &mp
->ports
;
266 (p
= mlock_dereference(*pp
, br
)) != NULL
;
271 rcu_assign_pointer(*pp
, p
->next
);
272 hlist_del_init(&p
->mglist
);
273 del_timer(&p
->timer
);
274 call_rcu_bh(&p
->rcu
, br_multicast_free_pg
);
276 if (!mp
->ports
&& !mp
->mglist
&&
277 netif_running(br
->dev
))
278 mod_timer(&mp
->timer
, jiffies
);
286 static void br_multicast_port_group_expired(unsigned long data
)
288 struct net_bridge_port_group
*pg
= (void *)data
;
289 struct net_bridge
*br
= pg
->port
->br
;
291 spin_lock(&br
->multicast_lock
);
292 if (!netif_running(br
->dev
) || timer_pending(&pg
->timer
) ||
293 hlist_unhashed(&pg
->mglist
) || pg
->state
& MDB_PERMANENT
)
296 br_multicast_del_pg(br
, pg
);
299 spin_unlock(&br
->multicast_lock
);
302 static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu
**mdbp
, int max
,
305 struct net_bridge_mdb_htable
*old
= rcu_dereference_protected(*mdbp
, 1);
306 struct net_bridge_mdb_htable
*mdb
;
309 mdb
= kmalloc(sizeof(*mdb
), GFP_ATOMIC
);
316 mdb
->mhash
= kzalloc(max
* sizeof(*mdb
->mhash
), GFP_ATOMIC
);
322 mdb
->size
= old
? old
->size
: 0;
323 mdb
->ver
= old
? old
->ver
^ 1 : 0;
325 if (!old
|| elasticity
)
326 get_random_bytes(&mdb
->secret
, sizeof(mdb
->secret
));
328 mdb
->secret
= old
->secret
;
333 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 ether_addr_copy(eth
->h_source
, br
->dev
->dev_addr
);
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
;
388 iph
->saddr
= br
->multicast_query_use_ifaddr
?
389 inet_select_addr(br
->dev
, 0, RT_SCOPE_LINK
) : 0;
390 iph
->daddr
= htonl(INADDR_ALLHOSTS_GROUP
);
391 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
392 ((u8
*)&iph
[1])[1] = 4;
393 ((u8
*)&iph
[1])[2] = 0;
394 ((u8
*)&iph
[1])[3] = 0;
398 skb_set_transport_header(skb
, skb
->len
);
400 ih
->type
= IGMP_HOST_MEMBERSHIP_QUERY
;
401 ih
->code
= (group
? br
->multicast_last_member_interval
:
402 br
->multicast_query_response_interval
) /
403 (HZ
/ IGMP_TIMER_SCALE
);
406 ih
->csum
= ip_compute_csum((void *)ih
, sizeof(struct igmphdr
));
407 skb_put(skb
, sizeof(*ih
));
409 __skb_pull(skb
, sizeof(*eth
));
415 #if IS_ENABLED(CONFIG_IPV6)
416 static struct sk_buff
*br_ip6_multicast_alloc_query(struct net_bridge
*br
,
417 const struct in6_addr
*group
)
420 struct ipv6hdr
*ip6h
;
421 struct mld_msg
*mldq
;
424 unsigned long interval
;
426 skb
= netdev_alloc_skb_ip_align(br
->dev
, sizeof(*eth
) + sizeof(*ip6h
) +
431 skb
->protocol
= htons(ETH_P_IPV6
);
433 /* Ethernet header */
434 skb_reset_mac_header(skb
);
437 ether_addr_copy(eth
->h_source
, br
->dev
->dev_addr
);
438 eth
->h_proto
= htons(ETH_P_IPV6
);
439 skb_put(skb
, sizeof(*eth
));
441 /* IPv6 header + HbH option */
442 skb_set_network_header(skb
, skb
->len
);
443 ip6h
= ipv6_hdr(skb
);
445 *(__force __be32
*)ip6h
= htonl(0x60000000);
446 ip6h
->payload_len
= htons(8 + sizeof(*mldq
));
447 ip6h
->nexthdr
= IPPROTO_HOPOPTS
;
449 ipv6_addr_set(&ip6h
->daddr
, htonl(0xff020000), 0, 0, htonl(1));
450 if (ipv6_dev_get_saddr(dev_net(br
->dev
), br
->dev
, &ip6h
->daddr
, 0,
455 ipv6_eth_mc_map(&ip6h
->daddr
, eth
->h_dest
);
457 hopopt
= (u8
*)(ip6h
+ 1);
458 hopopt
[0] = IPPROTO_ICMPV6
; /* next hdr */
459 hopopt
[1] = 0; /* length of HbH */
460 hopopt
[2] = IPV6_TLV_ROUTERALERT
; /* Router Alert */
461 hopopt
[3] = 2; /* Length of RA Option */
462 hopopt
[4] = 0; /* Type = 0x0000 (MLD) */
464 hopopt
[6] = IPV6_TLV_PAD1
; /* Pad1 */
465 hopopt
[7] = IPV6_TLV_PAD1
; /* Pad1 */
467 skb_put(skb
, sizeof(*ip6h
) + 8);
470 skb_set_transport_header(skb
, skb
->len
);
471 mldq
= (struct mld_msg
*) icmp6_hdr(skb
);
473 interval
= ipv6_addr_any(group
) ?
474 br
->multicast_query_response_interval
:
475 br
->multicast_last_member_interval
;
477 mldq
->mld_type
= ICMPV6_MGM_QUERY
;
480 mldq
->mld_maxdelay
= htons((u16
)jiffies_to_msecs(interval
));
481 mldq
->mld_reserved
= 0;
482 mldq
->mld_mca
= *group
;
485 mldq
->mld_cksum
= csum_ipv6_magic(&ip6h
->saddr
, &ip6h
->daddr
,
486 sizeof(*mldq
), IPPROTO_ICMPV6
,
489 skb_put(skb
, sizeof(*mldq
));
491 __skb_pull(skb
, sizeof(*eth
));
498 static struct sk_buff
*br_multicast_alloc_query(struct net_bridge
*br
,
501 switch (addr
->proto
) {
502 case htons(ETH_P_IP
):
503 return br_ip4_multicast_alloc_query(br
, addr
->u
.ip4
);
504 #if IS_ENABLED(CONFIG_IPV6)
505 case htons(ETH_P_IPV6
):
506 return br_ip6_multicast_alloc_query(br
, &addr
->u
.ip6
);
512 static struct net_bridge_mdb_entry
*br_multicast_get_group(
513 struct net_bridge
*br
, struct net_bridge_port
*port
,
514 struct br_ip
*group
, int hash
)
516 struct net_bridge_mdb_htable
*mdb
;
517 struct net_bridge_mdb_entry
*mp
;
518 unsigned int count
= 0;
523 mdb
= rcu_dereference_protected(br
->mdb
, 1);
524 hlist_for_each_entry(mp
, &mdb
->mhash
[hash
], hlist
[mdb
->ver
]) {
526 if (unlikely(br_ip_equal(group
, &mp
->addr
)))
533 if (unlikely(count
> br
->hash_elasticity
&& count
)) {
535 br_info(br
, "Multicast hash table "
536 "chain limit reached: %s\n",
537 port
? port
->dev
->name
: br
->dev
->name
);
539 elasticity
= br
->hash_elasticity
;
542 if (mdb
->size
>= max
) {
544 if (unlikely(max
> br
->hash_max
)) {
545 br_warn(br
, "Multicast hash table maximum of %d "
546 "reached, disabling snooping: %s\n",
548 port
? port
->dev
->name
: br
->dev
->name
);
551 br
->multicast_disabled
= 1;
556 if (max
> mdb
->max
|| elasticity
) {
559 br_info(br
, "Multicast hash table "
561 port
? port
->dev
->name
: br
->dev
->name
);
566 err
= br_mdb_rehash(&br
->mdb
, max
, elasticity
);
568 br_warn(br
, "Cannot rehash multicast "
569 "hash table, disabling snooping: %s, %d, %d\n",
570 port
? port
->dev
->name
: br
->dev
->name
,
586 struct net_bridge_mdb_entry
*br_multicast_new_group(struct net_bridge
*br
,
587 struct net_bridge_port
*port
, struct br_ip
*group
)
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 struct net_bridge_port_group
*br_multicast_new_port_group(
635 struct net_bridge_port
*port
,
637 struct net_bridge_port_group __rcu
*next
,
640 struct net_bridge_port_group
*p
;
642 p
= kzalloc(sizeof(*p
), GFP_ATOMIC
);
649 rcu_assign_pointer(p
->next
, next
);
650 hlist_add_head(&p
->mglist
, &port
->mglist
);
651 setup_timer(&p
->timer
, br_multicast_port_group_expired
,
656 static int br_multicast_add_group(struct net_bridge
*br
,
657 struct net_bridge_port
*port
,
660 struct net_bridge_mdb_entry
*mp
;
661 struct net_bridge_port_group
*p
;
662 struct net_bridge_port_group __rcu
**pp
;
663 unsigned long now
= jiffies
;
666 spin_lock(&br
->multicast_lock
);
667 if (!netif_running(br
->dev
) ||
668 (port
&& port
->state
== BR_STATE_DISABLED
))
671 mp
= br_multicast_new_group(br
, port
, group
);
678 mod_timer(&mp
->timer
, now
+ br
->multicast_membership_interval
);
682 for (pp
= &mp
->ports
;
683 (p
= mlock_dereference(*pp
, br
)) != NULL
;
687 if ((unsigned long)p
->port
< (unsigned long)port
)
691 p
= br_multicast_new_port_group(port
, group
, *pp
, MDB_TEMPORARY
);
694 rcu_assign_pointer(*pp
, p
);
695 br_mdb_notify(br
->dev
, port
, group
, RTM_NEWMDB
);
698 mod_timer(&p
->timer
, now
+ br
->multicast_membership_interval
);
703 spin_unlock(&br
->multicast_lock
);
707 static int br_ip4_multicast_add_group(struct net_bridge
*br
,
708 struct net_bridge_port
*port
,
712 struct br_ip br_group
;
714 if (ipv4_is_local_multicast(group
))
717 br_group
.u
.ip4
= group
;
718 br_group
.proto
= htons(ETH_P_IP
);
721 return br_multicast_add_group(br
, port
, &br_group
);
724 #if IS_ENABLED(CONFIG_IPV6)
725 static int br_ip6_multicast_add_group(struct net_bridge
*br
,
726 struct net_bridge_port
*port
,
727 const struct in6_addr
*group
,
730 struct br_ip br_group
;
732 if (ipv6_addr_is_ll_all_nodes(group
))
735 br_group
.u
.ip6
= *group
;
736 br_group
.proto
= htons(ETH_P_IPV6
);
739 return br_multicast_add_group(br
, port
, &br_group
);
743 static void br_multicast_router_expired(unsigned long data
)
745 struct net_bridge_port
*port
= (void *)data
;
746 struct net_bridge
*br
= port
->br
;
748 spin_lock(&br
->multicast_lock
);
749 if (port
->multicast_router
!= 1 ||
750 timer_pending(&port
->multicast_router_timer
) ||
751 hlist_unhashed(&port
->rlist
))
754 hlist_del_init_rcu(&port
->rlist
);
757 spin_unlock(&br
->multicast_lock
);
760 static void br_multicast_local_router_expired(unsigned long data
)
764 static void br_multicast_querier_expired(struct net_bridge
*br
,
765 struct bridge_mcast_own_query
*query
)
767 spin_lock(&br
->multicast_lock
);
768 if (!netif_running(br
->dev
) || br
->multicast_disabled
)
771 br_multicast_start_querier(br
, query
);
774 spin_unlock(&br
->multicast_lock
);
777 static void br_ip4_multicast_querier_expired(unsigned long data
)
779 struct net_bridge
*br
= (void *)data
;
781 br_multicast_querier_expired(br
, &br
->ip4_own_query
);
784 #if IS_ENABLED(CONFIG_IPV6)
785 static void br_ip6_multicast_querier_expired(unsigned long data
)
787 struct net_bridge
*br
= (void *)data
;
789 br_multicast_querier_expired(br
, &br
->ip6_own_query
);
793 static void br_multicast_select_own_querier(struct net_bridge
*br
,
797 if (ip
->proto
== htons(ETH_P_IP
))
798 br
->ip4_querier
.addr
.u
.ip4
= ip_hdr(skb
)->saddr
;
799 #if IS_ENABLED(CONFIG_IPV6)
801 br
->ip6_querier
.addr
.u
.ip6
= ipv6_hdr(skb
)->saddr
;
805 static void __br_multicast_send_query(struct net_bridge
*br
,
806 struct net_bridge_port
*port
,
811 skb
= br_multicast_alloc_query(br
, ip
);
816 skb
->dev
= port
->dev
;
817 NF_HOOK(NFPROTO_BRIDGE
, NF_BR_LOCAL_OUT
, skb
, NULL
, skb
->dev
,
818 br_dev_queue_push_xmit
);
820 br_multicast_select_own_querier(br
, ip
, skb
);
825 static void br_multicast_send_query(struct net_bridge
*br
,
826 struct net_bridge_port
*port
,
827 struct bridge_mcast_own_query
*own_query
)
830 struct br_ip br_group
;
831 struct bridge_mcast_other_query
*other_query
= NULL
;
833 if (!netif_running(br
->dev
) || br
->multicast_disabled
||
834 !br
->multicast_querier
)
837 memset(&br_group
.u
, 0, sizeof(br_group
.u
));
839 if (port
? (own_query
== &port
->ip4_own_query
) :
840 (own_query
== &br
->ip4_own_query
)) {
841 other_query
= &br
->ip4_other_query
;
842 br_group
.proto
= htons(ETH_P_IP
);
843 #if IS_ENABLED(CONFIG_IPV6)
845 other_query
= &br
->ip6_other_query
;
846 br_group
.proto
= htons(ETH_P_IPV6
);
850 if (!other_query
|| timer_pending(&other_query
->timer
))
853 __br_multicast_send_query(br
, port
, &br_group
);
856 time
+= own_query
->startup_sent
< br
->multicast_startup_query_count
?
857 br
->multicast_startup_query_interval
:
858 br
->multicast_query_interval
;
859 mod_timer(&own_query
->timer
, time
);
863 br_multicast_port_query_expired(struct net_bridge_port
*port
,
864 struct bridge_mcast_own_query
*query
)
866 struct net_bridge
*br
= port
->br
;
868 spin_lock(&br
->multicast_lock
);
869 if (port
->state
== BR_STATE_DISABLED
||
870 port
->state
== BR_STATE_BLOCKING
)
873 if (query
->startup_sent
< br
->multicast_startup_query_count
)
874 query
->startup_sent
++;
876 br_multicast_send_query(port
->br
, port
, query
);
879 spin_unlock(&br
->multicast_lock
);
882 static void br_ip4_multicast_port_query_expired(unsigned long data
)
884 struct net_bridge_port
*port
= (void *)data
;
886 br_multicast_port_query_expired(port
, &port
->ip4_own_query
);
889 #if IS_ENABLED(CONFIG_IPV6)
890 static void br_ip6_multicast_port_query_expired(unsigned long data
)
892 struct net_bridge_port
*port
= (void *)data
;
894 br_multicast_port_query_expired(port
, &port
->ip6_own_query
);
898 void br_multicast_add_port(struct net_bridge_port
*port
)
900 port
->multicast_router
= 1;
902 setup_timer(&port
->multicast_router_timer
, br_multicast_router_expired
,
903 (unsigned long)port
);
904 setup_timer(&port
->ip4_own_query
.timer
,
905 br_ip4_multicast_port_query_expired
, (unsigned long)port
);
906 #if IS_ENABLED(CONFIG_IPV6)
907 setup_timer(&port
->ip6_own_query
.timer
,
908 br_ip6_multicast_port_query_expired
, (unsigned long)port
);
912 void br_multicast_del_port(struct net_bridge_port
*port
)
914 del_timer_sync(&port
->multicast_router_timer
);
917 static void br_multicast_enable(struct bridge_mcast_own_query
*query
)
919 query
->startup_sent
= 0;
921 if (try_to_del_timer_sync(&query
->timer
) >= 0 ||
922 del_timer(&query
->timer
))
923 mod_timer(&query
->timer
, jiffies
);
926 void br_multicast_enable_port(struct net_bridge_port
*port
)
928 struct net_bridge
*br
= port
->br
;
930 spin_lock(&br
->multicast_lock
);
931 if (br
->multicast_disabled
|| !netif_running(br
->dev
))
934 br_multicast_enable(&port
->ip4_own_query
);
935 #if IS_ENABLED(CONFIG_IPV6)
936 br_multicast_enable(&port
->ip6_own_query
);
940 spin_unlock(&br
->multicast_lock
);
943 void br_multicast_disable_port(struct net_bridge_port
*port
)
945 struct net_bridge
*br
= port
->br
;
946 struct net_bridge_port_group
*pg
;
947 struct hlist_node
*n
;
949 spin_lock(&br
->multicast_lock
);
950 hlist_for_each_entry_safe(pg
, n
, &port
->mglist
, mglist
)
951 br_multicast_del_pg(br
, pg
);
953 if (!hlist_unhashed(&port
->rlist
))
954 hlist_del_init_rcu(&port
->rlist
);
955 del_timer(&port
->multicast_router_timer
);
956 del_timer(&port
->ip4_own_query
.timer
);
957 #if IS_ENABLED(CONFIG_IPV6)
958 del_timer(&port
->ip6_own_query
.timer
);
960 spin_unlock(&br
->multicast_lock
);
963 static int br_ip4_multicast_igmp3_report(struct net_bridge
*br
,
964 struct net_bridge_port
*port
,
968 struct igmpv3_report
*ih
;
969 struct igmpv3_grec
*grec
;
977 if (!pskb_may_pull(skb
, sizeof(*ih
)))
980 ih
= igmpv3_report_hdr(skb
);
981 num
= ntohs(ih
->ngrec
);
984 for (i
= 0; i
< num
; i
++) {
985 len
+= sizeof(*grec
);
986 if (!pskb_may_pull(skb
, len
))
989 grec
= (void *)(skb
->data
+ len
- sizeof(*grec
));
990 group
= grec
->grec_mca
;
991 type
= grec
->grec_type
;
993 len
+= ntohs(grec
->grec_nsrcs
) * 4;
994 if (!pskb_may_pull(skb
, len
))
997 /* We treat this as an IGMPv2 report for now. */
999 case IGMPV3_MODE_IS_INCLUDE
:
1000 case IGMPV3_MODE_IS_EXCLUDE
:
1001 case IGMPV3_CHANGE_TO_INCLUDE
:
1002 case IGMPV3_CHANGE_TO_EXCLUDE
:
1003 case IGMPV3_ALLOW_NEW_SOURCES
:
1004 case IGMPV3_BLOCK_OLD_SOURCES
:
1011 err
= br_ip4_multicast_add_group(br
, port
, group
, vid
);
1019 #if IS_ENABLED(CONFIG_IPV6)
1020 static int br_ip6_multicast_mld2_report(struct net_bridge
*br
,
1021 struct net_bridge_port
*port
,
1022 struct sk_buff
*skb
,
1025 struct icmp6hdr
*icmp6h
;
1026 struct mld2_grec
*grec
;
1032 if (!pskb_may_pull(skb
, sizeof(*icmp6h
)))
1035 icmp6h
= icmp6_hdr(skb
);
1036 num
= ntohs(icmp6h
->icmp6_dataun
.un_data16
[1]);
1037 len
= sizeof(*icmp6h
);
1039 for (i
= 0; i
< num
; i
++) {
1040 __be16
*nsrcs
, _nsrcs
;
1042 nsrcs
= skb_header_pointer(skb
,
1043 len
+ offsetof(struct mld2_grec
,
1045 sizeof(_nsrcs
), &_nsrcs
);
1049 if (!pskb_may_pull(skb
,
1050 len
+ sizeof(*grec
) +
1051 sizeof(struct in6_addr
) * ntohs(*nsrcs
)))
1054 grec
= (struct mld2_grec
*)(skb
->data
+ len
);
1055 len
+= sizeof(*grec
) +
1056 sizeof(struct in6_addr
) * ntohs(*nsrcs
);
1058 /* We treat these as MLDv1 reports for now. */
1059 switch (grec
->grec_type
) {
1060 case MLD2_MODE_IS_INCLUDE
:
1061 case MLD2_MODE_IS_EXCLUDE
:
1062 case MLD2_CHANGE_TO_INCLUDE
:
1063 case MLD2_CHANGE_TO_EXCLUDE
:
1064 case MLD2_ALLOW_NEW_SOURCES
:
1065 case MLD2_BLOCK_OLD_SOURCES
:
1072 err
= br_ip6_multicast_add_group(br
, port
, &grec
->grec_mca
,
1082 static bool br_ip4_multicast_select_querier(struct net_bridge
*br
,
1083 struct net_bridge_port
*port
,
1086 if (!timer_pending(&br
->ip4_own_query
.timer
) &&
1087 !timer_pending(&br
->ip4_other_query
.timer
))
1090 if (!br
->ip4_querier
.addr
.u
.ip4
)
1093 if (ntohl(saddr
) <= ntohl(br
->ip4_querier
.addr
.u
.ip4
))
1099 br
->ip4_querier
.addr
.u
.ip4
= saddr
;
1101 /* update protected by general multicast_lock by caller */
1102 rcu_assign_pointer(br
->ip4_querier
.port
, port
);
1107 #if IS_ENABLED(CONFIG_IPV6)
1108 static bool br_ip6_multicast_select_querier(struct net_bridge
*br
,
1109 struct net_bridge_port
*port
,
1110 struct in6_addr
*saddr
)
1112 if (!timer_pending(&br
->ip6_own_query
.timer
) &&
1113 !timer_pending(&br
->ip6_other_query
.timer
))
1116 if (ipv6_addr_cmp(saddr
, &br
->ip6_querier
.addr
.u
.ip6
) <= 0)
1122 br
->ip6_querier
.addr
.u
.ip6
= *saddr
;
1124 /* update protected by general multicast_lock by caller */
1125 rcu_assign_pointer(br
->ip6_querier
.port
, port
);
1131 static bool br_multicast_select_querier(struct net_bridge
*br
,
1132 struct net_bridge_port
*port
,
1133 struct br_ip
*saddr
)
1135 switch (saddr
->proto
) {
1136 case htons(ETH_P_IP
):
1137 return br_ip4_multicast_select_querier(br
, port
, saddr
->u
.ip4
);
1138 #if IS_ENABLED(CONFIG_IPV6)
1139 case htons(ETH_P_IPV6
):
1140 return br_ip6_multicast_select_querier(br
, port
, &saddr
->u
.ip6
);
1148 br_multicast_update_query_timer(struct net_bridge
*br
,
1149 struct bridge_mcast_other_query
*query
,
1150 unsigned long max_delay
)
1152 if (!timer_pending(&query
->timer
))
1153 query
->delay_time
= jiffies
+ max_delay
;
1155 mod_timer(&query
->timer
, jiffies
+ br
->multicast_querier_interval
);
1159 * Add port to router_list
1160 * list is maintained ordered by pointer value
1161 * and locked by br->multicast_lock and RCU
1163 static void br_multicast_add_router(struct net_bridge
*br
,
1164 struct net_bridge_port
*port
)
1166 struct net_bridge_port
*p
;
1167 struct hlist_node
*slot
= NULL
;
1169 hlist_for_each_entry(p
, &br
->router_list
, rlist
) {
1170 if ((unsigned long) port
>= (unsigned long) p
)
1176 hlist_add_behind_rcu(&port
->rlist
, slot
);
1178 hlist_add_head_rcu(&port
->rlist
, &br
->router_list
);
1181 static void br_multicast_mark_router(struct net_bridge
*br
,
1182 struct net_bridge_port
*port
)
1184 unsigned long now
= jiffies
;
1187 if (br
->multicast_router
== 1)
1188 mod_timer(&br
->multicast_router_timer
,
1189 now
+ br
->multicast_querier_interval
);
1193 if (port
->multicast_router
!= 1)
1196 if (!hlist_unhashed(&port
->rlist
))
1199 br_multicast_add_router(br
, port
);
1202 mod_timer(&port
->multicast_router_timer
,
1203 now
+ br
->multicast_querier_interval
);
1206 static void br_multicast_query_received(struct net_bridge
*br
,
1207 struct net_bridge_port
*port
,
1208 struct bridge_mcast_other_query
*query
,
1209 struct br_ip
*saddr
,
1210 unsigned long max_delay
)
1212 if (!br_multicast_select_querier(br
, port
, saddr
))
1215 br_multicast_update_query_timer(br
, query
, max_delay
);
1216 br_multicast_mark_router(br
, port
);
1219 static int br_ip4_multicast_query(struct net_bridge
*br
,
1220 struct net_bridge_port
*port
,
1221 struct sk_buff
*skb
,
1224 const struct iphdr
*iph
= ip_hdr(skb
);
1225 struct igmphdr
*ih
= igmp_hdr(skb
);
1226 struct net_bridge_mdb_entry
*mp
;
1227 struct igmpv3_query
*ih3
;
1228 struct net_bridge_port_group
*p
;
1229 struct net_bridge_port_group __rcu
**pp
;
1231 unsigned long max_delay
;
1232 unsigned long now
= jiffies
;
1236 spin_lock(&br
->multicast_lock
);
1237 if (!netif_running(br
->dev
) ||
1238 (port
&& port
->state
== BR_STATE_DISABLED
))
1243 if (skb
->len
== sizeof(*ih
)) {
1244 max_delay
= ih
->code
* (HZ
/ IGMP_TIMER_SCALE
);
1247 max_delay
= 10 * HZ
;
1251 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
))) {
1256 ih3
= igmpv3_query_hdr(skb
);
1260 max_delay
= ih3
->code
?
1261 IGMPV3_MRC(ih3
->code
) * (HZ
/ IGMP_TIMER_SCALE
) : 1;
1264 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1265 * all-systems destination addresses (224.0.0.1) for general queries
1267 if (!group
&& iph
->daddr
!= htonl(INADDR_ALLHOSTS_GROUP
)) {
1273 saddr
.proto
= htons(ETH_P_IP
);
1274 saddr
.u
.ip4
= iph
->saddr
;
1276 br_multicast_query_received(br
, port
, &br
->ip4_other_query
,
1281 mp
= br_mdb_ip4_get(mlock_dereference(br
->mdb
, br
), group
, vid
);
1285 max_delay
*= br
->multicast_last_member_count
;
1288 (timer_pending(&mp
->timer
) ?
1289 time_after(mp
->timer
.expires
, now
+ max_delay
) :
1290 try_to_del_timer_sync(&mp
->timer
) >= 0))
1291 mod_timer(&mp
->timer
, now
+ max_delay
);
1293 for (pp
= &mp
->ports
;
1294 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1296 if (timer_pending(&p
->timer
) ?
1297 time_after(p
->timer
.expires
, now
+ max_delay
) :
1298 try_to_del_timer_sync(&p
->timer
) >= 0)
1299 mod_timer(&p
->timer
, now
+ max_delay
);
1303 spin_unlock(&br
->multicast_lock
);
1307 #if IS_ENABLED(CONFIG_IPV6)
1308 static int br_ip6_multicast_query(struct net_bridge
*br
,
1309 struct net_bridge_port
*port
,
1310 struct sk_buff
*skb
,
1313 const struct ipv6hdr
*ip6h
= ipv6_hdr(skb
);
1314 struct mld_msg
*mld
;
1315 struct net_bridge_mdb_entry
*mp
;
1316 struct mld2_query
*mld2q
;
1317 struct net_bridge_port_group
*p
;
1318 struct net_bridge_port_group __rcu
**pp
;
1320 unsigned long max_delay
;
1321 unsigned long now
= jiffies
;
1322 const struct in6_addr
*group
= NULL
;
1323 bool is_general_query
;
1326 spin_lock(&br
->multicast_lock
);
1327 if (!netif_running(br
->dev
) ||
1328 (port
&& port
->state
== BR_STATE_DISABLED
))
1331 /* RFC2710+RFC3810 (MLDv1+MLDv2) require link-local source addresses */
1332 if (!(ipv6_addr_type(&ip6h
->saddr
) & IPV6_ADDR_LINKLOCAL
)) {
1337 if (skb
->len
== sizeof(*mld
)) {
1338 if (!pskb_may_pull(skb
, sizeof(*mld
))) {
1342 mld
= (struct mld_msg
*) icmp6_hdr(skb
);
1343 max_delay
= msecs_to_jiffies(ntohs(mld
->mld_maxdelay
));
1345 group
= &mld
->mld_mca
;
1347 if (!pskb_may_pull(skb
, sizeof(*mld2q
))) {
1351 mld2q
= (struct mld2_query
*)icmp6_hdr(skb
);
1352 if (!mld2q
->mld2q_nsrcs
)
1353 group
= &mld2q
->mld2q_mca
;
1355 max_delay
= max(msecs_to_jiffies(mldv2_mrc(mld2q
)), 1UL);
1358 is_general_query
= group
&& ipv6_addr_any(group
);
1360 /* RFC2710+RFC3810 (MLDv1+MLDv2) require the multicast link layer
1361 * all-nodes destination address (ff02::1) for general queries
1363 if (is_general_query
&& !ipv6_addr_is_ll_all_nodes(&ip6h
->daddr
)) {
1368 if (is_general_query
) {
1369 saddr
.proto
= htons(ETH_P_IPV6
);
1370 saddr
.u
.ip6
= ip6h
->saddr
;
1372 br_multicast_query_received(br
, port
, &br
->ip6_other_query
,
1375 } else if (!group
) {
1379 mp
= br_mdb_ip6_get(mlock_dereference(br
->mdb
, br
), group
, vid
);
1383 max_delay
*= br
->multicast_last_member_count
;
1385 (timer_pending(&mp
->timer
) ?
1386 time_after(mp
->timer
.expires
, now
+ max_delay
) :
1387 try_to_del_timer_sync(&mp
->timer
) >= 0))
1388 mod_timer(&mp
->timer
, now
+ max_delay
);
1390 for (pp
= &mp
->ports
;
1391 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1393 if (timer_pending(&p
->timer
) ?
1394 time_after(p
->timer
.expires
, now
+ max_delay
) :
1395 try_to_del_timer_sync(&p
->timer
) >= 0)
1396 mod_timer(&p
->timer
, now
+ max_delay
);
1400 spin_unlock(&br
->multicast_lock
);
1406 br_multicast_leave_group(struct net_bridge
*br
,
1407 struct net_bridge_port
*port
,
1408 struct br_ip
*group
,
1409 struct bridge_mcast_other_query
*other_query
,
1410 struct bridge_mcast_own_query
*own_query
)
1412 struct net_bridge_mdb_htable
*mdb
;
1413 struct net_bridge_mdb_entry
*mp
;
1414 struct net_bridge_port_group
*p
;
1418 spin_lock(&br
->multicast_lock
);
1419 if (!netif_running(br
->dev
) ||
1420 (port
&& port
->state
== BR_STATE_DISABLED
) ||
1421 timer_pending(&other_query
->timer
))
1424 mdb
= mlock_dereference(br
->mdb
, br
);
1425 mp
= br_mdb_ip_get(mdb
, group
);
1429 if (br
->multicast_querier
) {
1430 __br_multicast_send_query(br
, port
, &mp
->addr
);
1432 time
= jiffies
+ br
->multicast_last_member_count
*
1433 br
->multicast_last_member_interval
;
1435 mod_timer(&own_query
->timer
, time
);
1437 for (p
= mlock_dereference(mp
->ports
, br
);
1439 p
= mlock_dereference(p
->next
, br
)) {
1440 if (p
->port
!= port
)
1443 if (!hlist_unhashed(&p
->mglist
) &&
1444 (timer_pending(&p
->timer
) ?
1445 time_after(p
->timer
.expires
, time
) :
1446 try_to_del_timer_sync(&p
->timer
) >= 0)) {
1447 mod_timer(&p
->timer
, time
);
1454 if (port
&& (port
->flags
& BR_MULTICAST_FAST_LEAVE
)) {
1455 struct net_bridge_port_group __rcu
**pp
;
1457 for (pp
= &mp
->ports
;
1458 (p
= mlock_dereference(*pp
, br
)) != NULL
;
1460 if (p
->port
!= port
)
1463 rcu_assign_pointer(*pp
, p
->next
);
1464 hlist_del_init(&p
->mglist
);
1465 del_timer(&p
->timer
);
1466 call_rcu_bh(&p
->rcu
, br_multicast_free_pg
);
1467 br_mdb_notify(br
->dev
, port
, group
, RTM_DELMDB
);
1469 if (!mp
->ports
&& !mp
->mglist
&&
1470 netif_running(br
->dev
))
1471 mod_timer(&mp
->timer
, jiffies
);
1477 time
= now
+ br
->multicast_last_member_count
*
1478 br
->multicast_last_member_interval
;
1482 (timer_pending(&mp
->timer
) ?
1483 time_after(mp
->timer
.expires
, time
) :
1484 try_to_del_timer_sync(&mp
->timer
) >= 0)) {
1485 mod_timer(&mp
->timer
, time
);
1491 for (p
= mlock_dereference(mp
->ports
, br
);
1493 p
= mlock_dereference(p
->next
, br
)) {
1494 if (p
->port
!= port
)
1497 if (!hlist_unhashed(&p
->mglist
) &&
1498 (timer_pending(&p
->timer
) ?
1499 time_after(p
->timer
.expires
, time
) :
1500 try_to_del_timer_sync(&p
->timer
) >= 0)) {
1501 mod_timer(&p
->timer
, time
);
1507 spin_unlock(&br
->multicast_lock
);
1510 static void br_ip4_multicast_leave_group(struct net_bridge
*br
,
1511 struct net_bridge_port
*port
,
1515 struct br_ip br_group
;
1516 struct bridge_mcast_own_query
*own_query
;
1518 if (ipv4_is_local_multicast(group
))
1521 own_query
= port
? &port
->ip4_own_query
: &br
->ip4_own_query
;
1523 br_group
.u
.ip4
= group
;
1524 br_group
.proto
= htons(ETH_P_IP
);
1527 br_multicast_leave_group(br
, port
, &br_group
, &br
->ip4_other_query
,
1531 #if IS_ENABLED(CONFIG_IPV6)
1532 static void br_ip6_multicast_leave_group(struct net_bridge
*br
,
1533 struct net_bridge_port
*port
,
1534 const struct in6_addr
*group
,
1537 struct br_ip br_group
;
1538 struct bridge_mcast_own_query
*own_query
;
1540 if (ipv6_addr_is_ll_all_nodes(group
))
1543 own_query
= port
? &port
->ip6_own_query
: &br
->ip6_own_query
;
1545 br_group
.u
.ip6
= *group
;
1546 br_group
.proto
= htons(ETH_P_IPV6
);
1549 br_multicast_leave_group(br
, port
, &br_group
, &br
->ip6_other_query
,
1554 static int br_multicast_ipv4_rcv(struct net_bridge
*br
,
1555 struct net_bridge_port
*port
,
1556 struct sk_buff
*skb
,
1559 struct sk_buff
*skb2
= skb
;
1560 const struct iphdr
*iph
;
1563 unsigned int offset
;
1566 /* We treat OOM as packet loss for now. */
1567 if (!pskb_may_pull(skb
, sizeof(*iph
)))
1572 if (iph
->ihl
< 5 || iph
->version
!= 4)
1575 if (!pskb_may_pull(skb
, ip_hdrlen(skb
)))
1580 if (unlikely(ip_fast_csum((u8
*)iph
, iph
->ihl
)))
1583 if (iph
->protocol
!= IPPROTO_IGMP
) {
1584 if (!ipv4_is_local_multicast(iph
->daddr
))
1585 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1589 len
= ntohs(iph
->tot_len
);
1590 if (skb
->len
< len
|| len
< ip_hdrlen(skb
))
1593 if (skb
->len
> len
) {
1594 skb2
= skb_clone(skb
, GFP_ATOMIC
);
1598 err
= pskb_trim_rcsum(skb2
, len
);
1603 len
-= ip_hdrlen(skb2
);
1604 offset
= skb_network_offset(skb2
) + ip_hdrlen(skb2
);
1605 __skb_pull(skb2
, offset
);
1606 skb_reset_transport_header(skb2
);
1609 if (!pskb_may_pull(skb2
, sizeof(*ih
)))
1612 switch (skb2
->ip_summed
) {
1613 case CHECKSUM_COMPLETE
:
1614 if (!csum_fold(skb2
->csum
))
1619 if (skb_checksum_complete(skb2
))
1625 BR_INPUT_SKB_CB(skb
)->igmp
= 1;
1626 ih
= igmp_hdr(skb2
);
1629 case IGMP_HOST_MEMBERSHIP_REPORT
:
1630 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1631 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1632 err
= br_ip4_multicast_add_group(br
, port
, ih
->group
, vid
);
1634 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1635 err
= br_ip4_multicast_igmp3_report(br
, port
, skb2
, vid
);
1637 case IGMP_HOST_MEMBERSHIP_QUERY
:
1638 err
= br_ip4_multicast_query(br
, port
, skb2
, vid
);
1640 case IGMP_HOST_LEAVE_MESSAGE
:
1641 br_ip4_multicast_leave_group(br
, port
, ih
->group
, vid
);
1646 __skb_push(skb2
, offset
);
1653 #if IS_ENABLED(CONFIG_IPV6)
1654 static int br_multicast_ipv6_rcv(struct net_bridge
*br
,
1655 struct net_bridge_port
*port
,
1656 struct sk_buff
*skb
,
1659 struct sk_buff
*skb2
;
1660 const struct ipv6hdr
*ip6h
;
1668 if (!pskb_may_pull(skb
, sizeof(*ip6h
)))
1671 ip6h
= ipv6_hdr(skb
);
1674 * We're interested in MLD messages only.
1676 * - MLD has always Router Alert hop-by-hop option
1677 * - But we do not support jumbrograms.
1679 if (ip6h
->version
!= 6)
1682 /* Prevent flooding this packet if there is no listener present */
1683 if (!ipv6_addr_is_ll_all_nodes(&ip6h
->daddr
))
1684 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1686 if (ip6h
->nexthdr
!= IPPROTO_HOPOPTS
||
1687 ip6h
->payload_len
== 0)
1690 len
= ntohs(ip6h
->payload_len
) + sizeof(*ip6h
);
1694 nexthdr
= ip6h
->nexthdr
;
1695 offset
= ipv6_skip_exthdr(skb
, sizeof(*ip6h
), &nexthdr
, &frag_off
);
1697 if (offset
< 0 || nexthdr
!= IPPROTO_ICMPV6
)
1700 /* Okay, we found ICMPv6 header */
1701 skb2
= skb_clone(skb
, GFP_ATOMIC
);
1706 if (!pskb_may_pull(skb2
, offset
+ sizeof(struct icmp6hdr
)))
1709 len
-= offset
- skb_network_offset(skb2
);
1711 __skb_pull(skb2
, offset
);
1712 skb_reset_transport_header(skb2
);
1713 skb_postpull_rcsum(skb2
, skb_network_header(skb2
),
1714 skb_network_header_len(skb2
));
1716 icmp6_type
= icmp6_hdr(skb2
)->icmp6_type
;
1718 switch (icmp6_type
) {
1719 case ICMPV6_MGM_QUERY
:
1720 case ICMPV6_MGM_REPORT
:
1721 case ICMPV6_MGM_REDUCTION
:
1722 case ICMPV6_MLD2_REPORT
:
1729 /* Okay, we found MLD message. Check further. */
1730 if (skb2
->len
> len
) {
1731 err
= pskb_trim_rcsum(skb2
, len
);
1737 ip6h
= ipv6_hdr(skb2
);
1739 switch (skb2
->ip_summed
) {
1740 case CHECKSUM_COMPLETE
:
1741 if (!csum_ipv6_magic(&ip6h
->saddr
, &ip6h
->daddr
, skb2
->len
,
1742 IPPROTO_ICMPV6
, skb2
->csum
))
1746 skb2
->csum
= ~csum_unfold(csum_ipv6_magic(&ip6h
->saddr
,
1749 IPPROTO_ICMPV6
, 0));
1750 if (__skb_checksum_complete(skb2
))
1756 BR_INPUT_SKB_CB(skb
)->igmp
= 1;
1758 switch (icmp6_type
) {
1759 case ICMPV6_MGM_REPORT
:
1761 struct mld_msg
*mld
;
1762 if (!pskb_may_pull(skb2
, sizeof(*mld
))) {
1766 mld
= (struct mld_msg
*)skb_transport_header(skb2
);
1767 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 1;
1768 err
= br_ip6_multicast_add_group(br
, port
, &mld
->mld_mca
, vid
);
1771 case ICMPV6_MLD2_REPORT
:
1772 err
= br_ip6_multicast_mld2_report(br
, port
, skb2
, vid
);
1774 case ICMPV6_MGM_QUERY
:
1775 err
= br_ip6_multicast_query(br
, port
, skb2
, vid
);
1777 case ICMPV6_MGM_REDUCTION
:
1779 struct mld_msg
*mld
;
1780 if (!pskb_may_pull(skb2
, sizeof(*mld
))) {
1784 mld
= (struct mld_msg
*)skb_transport_header(skb2
);
1785 br_ip6_multicast_leave_group(br
, port
, &mld
->mld_mca
, vid
);
1795 int br_multicast_rcv(struct net_bridge
*br
, struct net_bridge_port
*port
,
1796 struct sk_buff
*skb
, u16 vid
)
1798 BR_INPUT_SKB_CB(skb
)->igmp
= 0;
1799 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 0;
1801 if (br
->multicast_disabled
)
1804 switch (skb
->protocol
) {
1805 case htons(ETH_P_IP
):
1806 return br_multicast_ipv4_rcv(br
, port
, skb
, vid
);
1807 #if IS_ENABLED(CONFIG_IPV6)
1808 case htons(ETH_P_IPV6
):
1809 return br_multicast_ipv6_rcv(br
, port
, skb
, vid
);
1816 static void br_multicast_query_expired(struct net_bridge
*br
,
1817 struct bridge_mcast_own_query
*query
,
1818 struct bridge_mcast_querier
*querier
)
1820 spin_lock(&br
->multicast_lock
);
1821 if (query
->startup_sent
< br
->multicast_startup_query_count
)
1822 query
->startup_sent
++;
1824 RCU_INIT_POINTER(querier
, NULL
);
1825 br_multicast_send_query(br
, NULL
, query
);
1826 spin_unlock(&br
->multicast_lock
);
1829 static void br_ip4_multicast_query_expired(unsigned long data
)
1831 struct net_bridge
*br
= (void *)data
;
1833 br_multicast_query_expired(br
, &br
->ip4_own_query
, &br
->ip4_querier
);
1836 #if IS_ENABLED(CONFIG_IPV6)
1837 static void br_ip6_multicast_query_expired(unsigned long data
)
1839 struct net_bridge
*br
= (void *)data
;
1841 br_multicast_query_expired(br
, &br
->ip6_own_query
, &br
->ip6_querier
);
1845 void br_multicast_init(struct net_bridge
*br
)
1847 br
->hash_elasticity
= 4;
1850 br
->multicast_router
= 1;
1851 br
->multicast_querier
= 0;
1852 br
->multicast_query_use_ifaddr
= 0;
1853 br
->multicast_last_member_count
= 2;
1854 br
->multicast_startup_query_count
= 2;
1856 br
->multicast_last_member_interval
= HZ
;
1857 br
->multicast_query_response_interval
= 10 * HZ
;
1858 br
->multicast_startup_query_interval
= 125 * HZ
/ 4;
1859 br
->multicast_query_interval
= 125 * HZ
;
1860 br
->multicast_querier_interval
= 255 * HZ
;
1861 br
->multicast_membership_interval
= 260 * HZ
;
1863 br
->ip4_other_query
.delay_time
= 0;
1864 br
->ip4_querier
.port
= NULL
;
1865 #if IS_ENABLED(CONFIG_IPV6)
1866 br
->ip6_other_query
.delay_time
= 0;
1867 br
->ip6_querier
.port
= NULL
;
1870 spin_lock_init(&br
->multicast_lock
);
1871 setup_timer(&br
->multicast_router_timer
,
1872 br_multicast_local_router_expired
, 0);
1873 setup_timer(&br
->ip4_other_query
.timer
,
1874 br_ip4_multicast_querier_expired
, (unsigned long)br
);
1875 setup_timer(&br
->ip4_own_query
.timer
, br_ip4_multicast_query_expired
,
1877 #if IS_ENABLED(CONFIG_IPV6)
1878 setup_timer(&br
->ip6_other_query
.timer
,
1879 br_ip6_multicast_querier_expired
, (unsigned long)br
);
1880 setup_timer(&br
->ip6_own_query
.timer
, br_ip6_multicast_query_expired
,
1885 static void __br_multicast_open(struct net_bridge
*br
,
1886 struct bridge_mcast_own_query
*query
)
1888 query
->startup_sent
= 0;
1890 if (br
->multicast_disabled
)
1893 mod_timer(&query
->timer
, jiffies
);
1896 void br_multicast_open(struct net_bridge
*br
)
1898 __br_multicast_open(br
, &br
->ip4_own_query
);
1899 #if IS_ENABLED(CONFIG_IPV6)
1900 __br_multicast_open(br
, &br
->ip6_own_query
);
1904 void br_multicast_stop(struct net_bridge
*br
)
1906 struct net_bridge_mdb_htable
*mdb
;
1907 struct net_bridge_mdb_entry
*mp
;
1908 struct hlist_node
*n
;
1912 del_timer_sync(&br
->multicast_router_timer
);
1913 del_timer_sync(&br
->ip4_other_query
.timer
);
1914 del_timer_sync(&br
->ip4_own_query
.timer
);
1915 #if IS_ENABLED(CONFIG_IPV6)
1916 del_timer_sync(&br
->ip6_other_query
.timer
);
1917 del_timer_sync(&br
->ip6_own_query
.timer
);
1920 spin_lock_bh(&br
->multicast_lock
);
1921 mdb
= mlock_dereference(br
->mdb
, br
);
1928 for (i
= 0; i
< mdb
->max
; i
++) {
1929 hlist_for_each_entry_safe(mp
, n
, &mdb
->mhash
[i
],
1931 del_timer(&mp
->timer
);
1932 call_rcu_bh(&mp
->rcu
, br_multicast_free_group
);
1937 spin_unlock_bh(&br
->multicast_lock
);
1939 spin_lock_bh(&br
->multicast_lock
);
1944 call_rcu_bh(&mdb
->rcu
, br_mdb_free
);
1947 spin_unlock_bh(&br
->multicast_lock
);
1950 int br_multicast_set_router(struct net_bridge
*br
, unsigned long val
)
1954 spin_lock_bh(&br
->multicast_lock
);
1955 if (!netif_running(br
->dev
))
1961 del_timer(&br
->multicast_router_timer
);
1964 br
->multicast_router
= val
;
1974 spin_unlock_bh(&br
->multicast_lock
);
1979 int br_multicast_set_port_router(struct net_bridge_port
*p
, unsigned long val
)
1981 struct net_bridge
*br
= p
->br
;
1984 spin_lock(&br
->multicast_lock
);
1985 if (!netif_running(br
->dev
) || p
->state
== BR_STATE_DISABLED
)
1992 p
->multicast_router
= val
;
1995 if (val
< 2 && !hlist_unhashed(&p
->rlist
))
1996 hlist_del_init_rcu(&p
->rlist
);
2001 del_timer(&p
->multicast_router_timer
);
2006 br_multicast_add_router(br
, p
);
2015 spin_unlock(&br
->multicast_lock
);
2020 static void br_multicast_start_querier(struct net_bridge
*br
,
2021 struct bridge_mcast_own_query
*query
)
2023 struct net_bridge_port
*port
;
2025 __br_multicast_open(br
, query
);
2027 list_for_each_entry(port
, &br
->port_list
, list
) {
2028 if (port
->state
== BR_STATE_DISABLED
||
2029 port
->state
== BR_STATE_BLOCKING
)
2032 if (query
== &br
->ip4_own_query
)
2033 br_multicast_enable(&port
->ip4_own_query
);
2034 #if IS_ENABLED(CONFIG_IPV6)
2036 br_multicast_enable(&port
->ip6_own_query
);
2041 int br_multicast_toggle(struct net_bridge
*br
, unsigned long val
)
2044 struct net_bridge_mdb_htable
*mdb
;
2046 spin_lock_bh(&br
->multicast_lock
);
2047 if (br
->multicast_disabled
== !val
)
2050 br
->multicast_disabled
= !val
;
2051 if (br
->multicast_disabled
)
2054 if (!netif_running(br
->dev
))
2057 mdb
= mlock_dereference(br
->mdb
, br
);
2062 br
->multicast_disabled
= !!val
;
2066 err
= br_mdb_rehash(&br
->mdb
, mdb
->max
,
2067 br
->hash_elasticity
);
2072 br_multicast_start_querier(br
, &br
->ip4_own_query
);
2073 #if IS_ENABLED(CONFIG_IPV6)
2074 br_multicast_start_querier(br
, &br
->ip6_own_query
);
2078 spin_unlock_bh(&br
->multicast_lock
);
2083 int br_multicast_set_querier(struct net_bridge
*br
, unsigned long val
)
2085 unsigned long max_delay
;
2089 spin_lock_bh(&br
->multicast_lock
);
2090 if (br
->multicast_querier
== val
)
2093 br
->multicast_querier
= val
;
2097 max_delay
= br
->multicast_query_response_interval
;
2099 if (!timer_pending(&br
->ip4_other_query
.timer
))
2100 br
->ip4_other_query
.delay_time
= jiffies
+ max_delay
;
2102 br_multicast_start_querier(br
, &br
->ip4_own_query
);
2104 #if IS_ENABLED(CONFIG_IPV6)
2105 if (!timer_pending(&br
->ip6_other_query
.timer
))
2106 br
->ip6_other_query
.delay_time
= jiffies
+ max_delay
;
2108 br_multicast_start_querier(br
, &br
->ip6_own_query
);
2112 spin_unlock_bh(&br
->multicast_lock
);
2117 int br_multicast_set_hash_max(struct net_bridge
*br
, unsigned long val
)
2121 struct net_bridge_mdb_htable
*mdb
;
2123 spin_lock_bh(&br
->multicast_lock
);
2124 if (!netif_running(br
->dev
))
2128 if (!is_power_of_2(val
))
2131 mdb
= mlock_dereference(br
->mdb
, br
);
2132 if (mdb
&& val
< mdb
->size
)
2148 err
= br_mdb_rehash(&br
->mdb
, br
->hash_max
,
2149 br
->hash_elasticity
);
2155 spin_unlock_bh(&br
->multicast_lock
);
2161 * br_multicast_list_adjacent - Returns snooped multicast addresses
2162 * @dev: The bridge port adjacent to which to retrieve addresses
2163 * @br_ip_list: The list to store found, snooped multicast IP addresses in
2165 * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
2166 * snooping feature on all bridge ports of dev's bridge device, excluding
2167 * the addresses from dev itself.
2169 * Returns the number of items added to br_ip_list.
2172 * - br_ip_list needs to be initialized by caller
2173 * - br_ip_list might contain duplicates in the end
2174 * (needs to be taken care of by caller)
2175 * - br_ip_list needs to be freed by caller
2177 int br_multicast_list_adjacent(struct net_device
*dev
,
2178 struct list_head
*br_ip_list
)
2180 struct net_bridge
*br
;
2181 struct net_bridge_port
*port
;
2182 struct net_bridge_port_group
*group
;
2183 struct br_ip_list
*entry
;
2187 if (!br_ip_list
|| !br_port_exists(dev
))
2190 port
= br_port_get_rcu(dev
);
2191 if (!port
|| !port
->br
)
2196 list_for_each_entry_rcu(port
, &br
->port_list
, list
) {
2197 if (!port
->dev
|| port
->dev
== dev
)
2200 hlist_for_each_entry_rcu(group
, &port
->mglist
, mglist
) {
2201 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
2205 entry
->addr
= group
->addr
;
2206 list_add(&entry
->list
, br_ip_list
);
2215 EXPORT_SYMBOL_GPL(br_multicast_list_adjacent
);
2218 * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2219 * @dev: The bridge port providing the bridge on which to check for a querier
2220 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2222 * Checks whether the given interface has a bridge on top and if so returns
2223 * true if a valid querier exists anywhere on the bridged link layer.
2224 * Otherwise returns false.
2226 bool br_multicast_has_querier_anywhere(struct net_device
*dev
, int proto
)
2228 struct net_bridge
*br
;
2229 struct net_bridge_port
*port
;
2234 if (!br_port_exists(dev
))
2237 port
= br_port_get_rcu(dev
);
2238 if (!port
|| !port
->br
)
2243 memset(ð
, 0, sizeof(eth
));
2244 eth
.h_proto
= htons(proto
);
2246 ret
= br_multicast_querier_exists(br
, ð
);
2252 EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere
);
2255 * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
2256 * @dev: The bridge port adjacent to which to check for a querier
2257 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2259 * Checks whether the given interface has a bridge on top and if so returns
2260 * true if a selected querier is behind one of the other ports of this
2261 * bridge. Otherwise returns false.
2263 bool br_multicast_has_querier_adjacent(struct net_device
*dev
, int proto
)
2265 struct net_bridge
*br
;
2266 struct net_bridge_port
*port
;
2270 if (!br_port_exists(dev
))
2273 port
= br_port_get_rcu(dev
);
2274 if (!port
|| !port
->br
)
2281 if (!timer_pending(&br
->ip4_other_query
.timer
) ||
2282 rcu_dereference(br
->ip4_querier
.port
) == port
)
2285 #if IS_ENABLED(CONFIG_IPV6)
2287 if (!timer_pending(&br
->ip6_other_query
.timer
) ||
2288 rcu_dereference(br
->ip6_querier
.port
) == port
)
2301 EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent
);