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>
28 #include "br_private.h"
30 static inline int br_ip_hash(struct net_bridge_mdb_htable
*mdb
, __be32 ip
)
32 return jhash_1word(mdb
->secret
, (__force u32
)ip
) & (mdb
->max
- 1);
35 static struct net_bridge_mdb_entry
*__br_mdb_ip_get(
36 struct net_bridge_mdb_htable
*mdb
, __be32 dst
, int hash
)
38 struct net_bridge_mdb_entry
*mp
;
41 hlist_for_each_entry_rcu(mp
, p
, &mdb
->mhash
[hash
], hlist
[mdb
->ver
]) {
49 static struct net_bridge_mdb_entry
*br_mdb_ip_get(
50 struct net_bridge_mdb_htable
*mdb
, __be32 dst
)
55 return __br_mdb_ip_get(mdb
, dst
, br_ip_hash(mdb
, dst
));
58 struct net_bridge_mdb_entry
*br_mdb_get(struct net_bridge
*br
,
61 if (br
->multicast_disabled
)
64 switch (skb
->protocol
) {
66 if (BR_INPUT_SKB_CB(skb
)->igmp
)
68 return br_mdb_ip_get(br
->mdb
, ip_hdr(skb
)->daddr
);
74 static void br_mdb_free(struct rcu_head
*head
)
76 struct net_bridge_mdb_htable
*mdb
=
77 container_of(head
, struct net_bridge_mdb_htable
, rcu
);
78 struct net_bridge_mdb_htable
*old
= mdb
->old
;
85 static int br_mdb_copy(struct net_bridge_mdb_htable
*new,
86 struct net_bridge_mdb_htable
*old
,
89 struct net_bridge_mdb_entry
*mp
;
95 for (i
= 0; i
< old
->max
; i
++)
96 hlist_for_each_entry(mp
, p
, &old
->mhash
[i
], hlist
[old
->ver
])
97 hlist_add_head(&mp
->hlist
[new->ver
],
98 &new->mhash
[br_ip_hash(new, mp
->addr
)]);
104 for (i
= 0; i
< new->max
; i
++) {
106 hlist_for_each_entry(mp
, p
, &new->mhash
[i
], hlist
[new->ver
])
112 return maxlen
> elasticity
? -EINVAL
: 0;
115 static void br_multicast_free_pg(struct rcu_head
*head
)
117 struct net_bridge_port_group
*p
=
118 container_of(head
, struct net_bridge_port_group
, rcu
);
123 static void br_multicast_free_group(struct rcu_head
*head
)
125 struct net_bridge_mdb_entry
*mp
=
126 container_of(head
, struct net_bridge_mdb_entry
, rcu
);
131 static void br_multicast_group_expired(unsigned long data
)
133 struct net_bridge_mdb_entry
*mp
= (void *)data
;
134 struct net_bridge
*br
= mp
->br
;
135 struct net_bridge_mdb_htable
*mdb
;
137 spin_lock(&br
->multicast_lock
);
138 if (!netif_running(br
->dev
) || timer_pending(&mp
->timer
))
141 if (!hlist_unhashed(&mp
->mglist
))
142 hlist_del_init(&mp
->mglist
);
148 hlist_del_rcu(&mp
->hlist
[mdb
->ver
]);
151 del_timer(&mp
->query_timer
);
152 call_rcu_bh(&mp
->rcu
, br_multicast_free_group
);
155 spin_unlock(&br
->multicast_lock
);
158 static void br_multicast_del_pg(struct net_bridge
*br
,
159 struct net_bridge_port_group
*pg
)
161 struct net_bridge_mdb_htable
*mdb
= br
->mdb
;
162 struct net_bridge_mdb_entry
*mp
;
163 struct net_bridge_port_group
*p
;
164 struct net_bridge_port_group
**pp
;
166 mp
= br_mdb_ip_get(mdb
, pg
->addr
);
170 for (pp
= &mp
->ports
; (p
= *pp
); pp
= &p
->next
) {
175 hlist_del_init(&p
->mglist
);
176 del_timer(&p
->timer
);
177 del_timer(&p
->query_timer
);
178 call_rcu_bh(&p
->rcu
, br_multicast_free_pg
);
180 if (!mp
->ports
&& hlist_unhashed(&mp
->mglist
) &&
181 netif_running(br
->dev
))
182 mod_timer(&mp
->timer
, jiffies
);
190 static void br_multicast_port_group_expired(unsigned long data
)
192 struct net_bridge_port_group
*pg
= (void *)data
;
193 struct net_bridge
*br
= pg
->port
->br
;
195 spin_lock(&br
->multicast_lock
);
196 if (!netif_running(br
->dev
) || timer_pending(&pg
->timer
) ||
197 hlist_unhashed(&pg
->mglist
))
200 br_multicast_del_pg(br
, pg
);
203 spin_unlock(&br
->multicast_lock
);
206 static int br_mdb_rehash(struct net_bridge_mdb_htable
**mdbp
, int max
,
209 struct net_bridge_mdb_htable
*old
= *mdbp
;
210 struct net_bridge_mdb_htable
*mdb
;
213 mdb
= kmalloc(sizeof(*mdb
), GFP_ATOMIC
);
220 mdb
->mhash
= kzalloc(max
* sizeof(*mdb
->mhash
), GFP_ATOMIC
);
226 mdb
->size
= old
? old
->size
: 0;
227 mdb
->ver
= old
? old
->ver
^ 1 : 0;
229 if (!old
|| elasticity
)
230 get_random_bytes(&mdb
->secret
, sizeof(mdb
->secret
));
232 mdb
->secret
= old
->secret
;
237 err
= br_mdb_copy(mdb
, old
, elasticity
);
244 call_rcu_bh(&mdb
->rcu
, br_mdb_free
);
247 rcu_assign_pointer(*mdbp
, mdb
);
252 static struct sk_buff
*br_multicast_alloc_query(struct net_bridge
*br
,
260 skb
= netdev_alloc_skb_ip_align(br
->dev
, sizeof(*eth
) + sizeof(*iph
) +
265 skb
->protocol
= htons(ETH_P_IP
);
267 skb_reset_mac_header(skb
);
270 memcpy(eth
->h_source
, br
->dev
->dev_addr
, 6);
273 eth
->h_dest
[2] = 0x5e;
277 eth
->h_proto
= htons(ETH_P_IP
);
278 skb_put(skb
, sizeof(*eth
));
280 skb_set_network_header(skb
, skb
->len
);
286 iph
->tot_len
= htons(sizeof(*iph
) + sizeof(*ih
) + 4);
288 iph
->frag_off
= htons(IP_DF
);
290 iph
->protocol
= IPPROTO_IGMP
;
292 iph
->daddr
= htonl(INADDR_ALLHOSTS_GROUP
);
293 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
294 ((u8
*)&iph
[1])[1] = 4;
295 ((u8
*)&iph
[1])[2] = 0;
296 ((u8
*)&iph
[1])[3] = 0;
300 skb_set_transport_header(skb
, skb
->len
);
302 ih
->type
= IGMP_HOST_MEMBERSHIP_QUERY
;
303 ih
->code
= (group
? br
->multicast_last_member_interval
:
304 br
->multicast_query_response_interval
) /
305 (HZ
/ IGMP_TIMER_SCALE
);
308 ih
->csum
= ip_compute_csum((void *)ih
, sizeof(struct igmphdr
));
309 skb_put(skb
, sizeof(*ih
));
311 __skb_pull(skb
, sizeof(*eth
));
317 static void br_multicast_send_group_query(struct net_bridge_mdb_entry
*mp
)
319 struct net_bridge
*br
= mp
->br
;
322 skb
= br_multicast_alloc_query(br
, mp
->addr
);
329 if (++mp
->queries_sent
< br
->multicast_last_member_count
)
330 mod_timer(&mp
->query_timer
,
331 jiffies
+ br
->multicast_last_member_interval
);
334 static void br_multicast_group_query_expired(unsigned long data
)
336 struct net_bridge_mdb_entry
*mp
= (void *)data
;
337 struct net_bridge
*br
= mp
->br
;
339 spin_lock(&br
->multicast_lock
);
340 if (!netif_running(br
->dev
) || hlist_unhashed(&mp
->mglist
) ||
341 mp
->queries_sent
>= br
->multicast_last_member_count
)
344 br_multicast_send_group_query(mp
);
347 spin_unlock(&br
->multicast_lock
);
350 static void br_multicast_send_port_group_query(struct net_bridge_port_group
*pg
)
352 struct net_bridge_port
*port
= pg
->port
;
353 struct net_bridge
*br
= port
->br
;
356 skb
= br_multicast_alloc_query(br
, pg
->addr
);
360 br_deliver(port
, skb
);
363 if (++pg
->queries_sent
< br
->multicast_last_member_count
)
364 mod_timer(&pg
->query_timer
,
365 jiffies
+ br
->multicast_last_member_interval
);
368 static void br_multicast_port_group_query_expired(unsigned long data
)
370 struct net_bridge_port_group
*pg
= (void *)data
;
371 struct net_bridge_port
*port
= pg
->port
;
372 struct net_bridge
*br
= port
->br
;
374 spin_lock(&br
->multicast_lock
);
375 if (!netif_running(br
->dev
) || hlist_unhashed(&pg
->mglist
) ||
376 pg
->queries_sent
>= br
->multicast_last_member_count
)
379 br_multicast_send_port_group_query(pg
);
382 spin_unlock(&br
->multicast_lock
);
385 static struct net_bridge_mdb_entry
*br_multicast_get_group(
386 struct net_bridge
*br
, struct net_bridge_port
*port
, __be32 group
,
389 struct net_bridge_mdb_htable
*mdb
= br
->mdb
;
390 struct net_bridge_mdb_entry
*mp
;
391 struct hlist_node
*p
;
397 hlist_for_each_entry(mp
, p
, &mdb
->mhash
[hash
], hlist
[mdb
->ver
]) {
399 if (unlikely(group
== mp
->addr
)) {
407 if (unlikely(count
> br
->hash_elasticity
&& count
)) {
409 printk(KERN_INFO
"%s: Multicast hash table "
410 "chain limit reached: %s\n",
411 br
->dev
->name
, port
? port
->dev
->name
:
414 elasticity
= br
->hash_elasticity
;
417 if (mdb
->size
>= max
) {
419 if (unlikely(max
>= br
->hash_max
)) {
420 printk(KERN_WARNING
"%s: Multicast hash table maximum "
421 "reached, disabling snooping: %s, %d\n",
422 br
->dev
->name
, port
? port
->dev
->name
:
427 br
->multicast_disabled
= 1;
432 if (max
> mdb
->max
|| elasticity
) {
435 printk(KERN_INFO
"%s: Multicast hash table "
437 br
->dev
->name
, port
? port
->dev
->name
:
443 err
= br_mdb_rehash(&br
->mdb
, max
, elasticity
);
445 printk(KERN_WARNING
"%s: Cannot rehash multicast "
446 "hash table, disabling snooping: "
448 br
->dev
->name
, port
? port
->dev
->name
:
465 static struct net_bridge_mdb_entry
*br_multicast_new_group(
466 struct net_bridge
*br
, struct net_bridge_port
*port
, __be32 group
)
468 struct net_bridge_mdb_htable
*mdb
= br
->mdb
;
469 struct net_bridge_mdb_entry
*mp
;
473 if (br_mdb_rehash(&br
->mdb
, BR_HASH_SIZE
, 0))
478 hash
= br_ip_hash(mdb
, group
);
479 mp
= br_multicast_get_group(br
, port
, group
, hash
);
480 switch (PTR_ERR(mp
)) {
487 hash
= br_ip_hash(mdb
, group
);
494 mp
= kzalloc(sizeof(*mp
), GFP_ATOMIC
);
500 setup_timer(&mp
->timer
, br_multicast_group_expired
,
502 setup_timer(&mp
->query_timer
, br_multicast_group_query_expired
,
505 hlist_add_head_rcu(&mp
->hlist
[mdb
->ver
], &mdb
->mhash
[hash
]);
512 static int br_multicast_add_group(struct net_bridge
*br
,
513 struct net_bridge_port
*port
, __be32 group
)
515 struct net_bridge_mdb_entry
*mp
;
516 struct net_bridge_port_group
*p
;
517 struct net_bridge_port_group
**pp
;
518 unsigned long now
= jiffies
;
521 if (ipv4_is_local_multicast(group
))
524 spin_lock(&br
->multicast_lock
);
525 if (!netif_running(br
->dev
) ||
526 (port
&& port
->state
== BR_STATE_DISABLED
))
529 mp
= br_multicast_new_group(br
, port
, group
);
531 if (unlikely(IS_ERR(mp
) || !mp
))
535 hlist_add_head(&mp
->mglist
, &br
->mglist
);
536 mod_timer(&mp
->timer
, now
+ br
->multicast_membership_interval
);
540 for (pp
= &mp
->ports
; (p
= *pp
); pp
= &p
->next
) {
543 if ((unsigned long)p
->port
< (unsigned long)port
)
547 p
= kzalloc(sizeof(*p
), GFP_ATOMIC
);
555 hlist_add_head(&p
->mglist
, &port
->mglist
);
556 setup_timer(&p
->timer
, br_multicast_port_group_expired
,
558 setup_timer(&p
->query_timer
, br_multicast_port_group_query_expired
,
561 rcu_assign_pointer(*pp
, p
);
564 mod_timer(&p
->timer
, now
+ br
->multicast_membership_interval
);
569 spin_unlock(&br
->multicast_lock
);
573 static void br_multicast_router_expired(unsigned long data
)
575 struct net_bridge_port
*port
= (void *)data
;
576 struct net_bridge
*br
= port
->br
;
578 spin_lock(&br
->multicast_lock
);
579 if (port
->multicast_router
!= 1 ||
580 timer_pending(&port
->multicast_router_timer
) ||
581 hlist_unhashed(&port
->rlist
))
584 hlist_del_init_rcu(&port
->rlist
);
587 spin_unlock(&br
->multicast_lock
);
590 static void br_multicast_local_router_expired(unsigned long data
)
594 static void br_multicast_send_query(struct net_bridge
*br
,
595 struct net_bridge_port
*port
, u32 sent
)
600 if (!netif_running(br
->dev
) || br
->multicast_disabled
||
601 timer_pending(&br
->multicast_querier_timer
))
604 skb
= br_multicast_alloc_query(br
, 0);
609 __skb_push(skb
, sizeof(struct ethhdr
));
610 skb
->dev
= port
->dev
;
611 NF_HOOK(PF_BRIDGE
, NF_BR_LOCAL_OUT
, skb
, NULL
, skb
->dev
,
618 time
+= sent
< br
->multicast_startup_query_count
?
619 br
->multicast_startup_query_interval
:
620 br
->multicast_query_interval
;
621 mod_timer(port
? &port
->multicast_query_timer
:
622 &br
->multicast_query_timer
, time
);
625 static void br_multicast_port_query_expired(unsigned long data
)
627 struct net_bridge_port
*port
= (void *)data
;
628 struct net_bridge
*br
= port
->br
;
630 spin_lock(&br
->multicast_lock
);
631 if (port
->state
== BR_STATE_DISABLED
||
632 port
->state
== BR_STATE_BLOCKING
)
635 if (port
->multicast_startup_queries_sent
<
636 br
->multicast_startup_query_count
)
637 port
->multicast_startup_queries_sent
++;
639 br_multicast_send_query(port
->br
, port
,
640 port
->multicast_startup_queries_sent
);
643 spin_unlock(&br
->multicast_lock
);
646 void br_multicast_add_port(struct net_bridge_port
*port
)
648 port
->multicast_router
= 1;
650 setup_timer(&port
->multicast_router_timer
, br_multicast_router_expired
,
651 (unsigned long)port
);
652 setup_timer(&port
->multicast_query_timer
,
653 br_multicast_port_query_expired
, (unsigned long)port
);
656 void br_multicast_del_port(struct net_bridge_port
*port
)
658 del_timer_sync(&port
->multicast_router_timer
);
661 static void __br_multicast_enable_port(struct net_bridge_port
*port
)
663 port
->multicast_startup_queries_sent
= 0;
665 if (try_to_del_timer_sync(&port
->multicast_query_timer
) >= 0 ||
666 del_timer(&port
->multicast_query_timer
))
667 mod_timer(&port
->multicast_query_timer
, jiffies
);
670 void br_multicast_enable_port(struct net_bridge_port
*port
)
672 struct net_bridge
*br
= port
->br
;
674 spin_lock(&br
->multicast_lock
);
675 if (br
->multicast_disabled
|| !netif_running(br
->dev
))
678 __br_multicast_enable_port(port
);
681 spin_unlock(&br
->multicast_lock
);
684 void br_multicast_disable_port(struct net_bridge_port
*port
)
686 struct net_bridge
*br
= port
->br
;
687 struct net_bridge_port_group
*pg
;
688 struct hlist_node
*p
, *n
;
690 spin_lock(&br
->multicast_lock
);
691 hlist_for_each_entry_safe(pg
, p
, n
, &port
->mglist
, mglist
)
692 br_multicast_del_pg(br
, pg
);
694 if (!hlist_unhashed(&port
->rlist
))
695 hlist_del_init_rcu(&port
->rlist
);
696 del_timer(&port
->multicast_router_timer
);
697 del_timer(&port
->multicast_query_timer
);
698 spin_unlock(&br
->multicast_lock
);
701 static int br_multicast_igmp3_report(struct net_bridge
*br
,
702 struct net_bridge_port
*port
,
705 struct igmpv3_report
*ih
;
706 struct igmpv3_grec
*grec
;
714 if (!pskb_may_pull(skb
, sizeof(*ih
)))
717 ih
= igmpv3_report_hdr(skb
);
718 num
= ntohs(ih
->ngrec
);
721 for (i
= 0; i
< num
; i
++) {
722 len
+= sizeof(*grec
);
723 if (!pskb_may_pull(skb
, len
))
726 grec
= (void *)(skb
->data
+ len
- sizeof(*grec
));
727 group
= grec
->grec_mca
;
728 type
= grec
->grec_type
;
730 len
+= grec
->grec_nsrcs
* 4;
731 if (!pskb_may_pull(skb
, len
))
734 /* We treat this as an IGMPv2 report for now. */
736 case IGMPV3_MODE_IS_INCLUDE
:
737 case IGMPV3_MODE_IS_EXCLUDE
:
738 case IGMPV3_CHANGE_TO_INCLUDE
:
739 case IGMPV3_CHANGE_TO_EXCLUDE
:
740 case IGMPV3_ALLOW_NEW_SOURCES
:
741 case IGMPV3_BLOCK_OLD_SOURCES
:
748 err
= br_multicast_add_group(br
, port
, group
);
756 static void br_multicast_add_router(struct net_bridge
*br
,
757 struct net_bridge_port
*port
)
759 struct hlist_node
*p
;
760 struct hlist_node
**h
;
762 for (h
= &br
->router_list
.first
;
764 (unsigned long)container_of(p
, struct net_bridge_port
, rlist
) >
769 port
->rlist
.pprev
= h
;
770 port
->rlist
.next
= p
;
771 rcu_assign_pointer(*h
, &port
->rlist
);
773 p
->pprev
= &port
->rlist
.next
;
776 static void br_multicast_mark_router(struct net_bridge
*br
,
777 struct net_bridge_port
*port
)
779 unsigned long now
= jiffies
;
782 if (br
->multicast_router
== 1)
783 mod_timer(&br
->multicast_router_timer
,
784 now
+ br
->multicast_querier_interval
);
788 if (port
->multicast_router
!= 1)
791 if (!hlist_unhashed(&port
->rlist
))
794 br_multicast_add_router(br
, port
);
797 mod_timer(&port
->multicast_router_timer
,
798 now
+ br
->multicast_querier_interval
);
801 static void br_multicast_query_received(struct net_bridge
*br
,
802 struct net_bridge_port
*port
,
806 mod_timer(&br
->multicast_querier_timer
,
807 jiffies
+ br
->multicast_querier_interval
);
808 else if (timer_pending(&br
->multicast_querier_timer
))
811 br_multicast_mark_router(br
, port
);
814 static int br_multicast_query(struct net_bridge
*br
,
815 struct net_bridge_port
*port
,
818 struct iphdr
*iph
= ip_hdr(skb
);
819 struct igmphdr
*ih
= igmp_hdr(skb
);
820 struct net_bridge_mdb_entry
*mp
;
821 struct igmpv3_query
*ih3
;
822 struct net_bridge_port_group
*p
;
823 struct net_bridge_port_group
**pp
;
824 unsigned long max_delay
;
825 unsigned long now
= jiffies
;
829 spin_lock(&br
->multicast_lock
);
830 if (!netif_running(br
->dev
) ||
831 (port
&& port
->state
== BR_STATE_DISABLED
))
834 br_multicast_query_received(br
, port
, iph
->saddr
);
838 if (skb
->len
== sizeof(*ih
)) {
839 max_delay
= ih
->code
* (HZ
/ IGMP_TIMER_SCALE
);
846 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
))) {
851 ih3
= igmpv3_query_hdr(skb
);
855 max_delay
= ih3
->code
?
856 IGMPV3_MRC(ih3
->code
) * (HZ
/ IGMP_TIMER_SCALE
) : 1;
862 mp
= br_mdb_ip_get(br
->mdb
, group
);
866 max_delay
*= br
->multicast_last_member_count
;
868 if (!hlist_unhashed(&mp
->mglist
) &&
869 (timer_pending(&mp
->timer
) ?
870 time_after(mp
->timer
.expires
, now
+ max_delay
) :
871 try_to_del_timer_sync(&mp
->timer
) >= 0))
872 mod_timer(&mp
->timer
, now
+ max_delay
);
874 for (pp
= &mp
->ports
; (p
= *pp
); pp
= &p
->next
) {
875 if (timer_pending(&p
->timer
) ?
876 time_after(p
->timer
.expires
, now
+ max_delay
) :
877 try_to_del_timer_sync(&p
->timer
) >= 0)
878 mod_timer(&mp
->timer
, now
+ max_delay
);
882 spin_unlock(&br
->multicast_lock
);
886 static void br_multicast_leave_group(struct net_bridge
*br
,
887 struct net_bridge_port
*port
,
890 struct net_bridge_mdb_htable
*mdb
;
891 struct net_bridge_mdb_entry
*mp
;
892 struct net_bridge_port_group
*p
;
896 if (ipv4_is_local_multicast(group
))
899 spin_lock(&br
->multicast_lock
);
900 if (!netif_running(br
->dev
) ||
901 (port
&& port
->state
== BR_STATE_DISABLED
) ||
902 timer_pending(&br
->multicast_querier_timer
))
906 mp
= br_mdb_ip_get(mdb
, group
);
911 time
= now
+ br
->multicast_last_member_count
*
912 br
->multicast_last_member_interval
;
915 if (!hlist_unhashed(&mp
->mglist
) &&
916 (timer_pending(&mp
->timer
) ?
917 time_after(mp
->timer
.expires
, time
) :
918 try_to_del_timer_sync(&mp
->timer
) >= 0)) {
919 mod_timer(&mp
->timer
, time
);
921 mp
->queries_sent
= 0;
922 mod_timer(&mp
->query_timer
, now
);
928 for (p
= mp
->ports
; p
; p
= p
->next
) {
932 if (!hlist_unhashed(&p
->mglist
) &&
933 (timer_pending(&p
->timer
) ?
934 time_after(p
->timer
.expires
, time
) :
935 try_to_del_timer_sync(&p
->timer
) >= 0)) {
936 mod_timer(&p
->timer
, time
);
939 mod_timer(&p
->query_timer
, now
);
946 spin_unlock(&br
->multicast_lock
);
949 static int br_multicast_ipv4_rcv(struct net_bridge
*br
,
950 struct net_bridge_port
*port
,
953 struct sk_buff
*skb2
= skb
;
960 BR_INPUT_SKB_CB(skb
)->igmp
= 0;
961 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 0;
963 /* We treat OOM as packet loss for now. */
964 if (!pskb_may_pull(skb
, sizeof(*iph
)))
969 if (iph
->ihl
< 5 || iph
->version
!= 4)
972 if (!pskb_may_pull(skb
, ip_hdrlen(skb
)))
977 if (unlikely(ip_fast_csum((u8
*)iph
, iph
->ihl
)))
980 if (iph
->protocol
!= IPPROTO_IGMP
)
983 len
= ntohs(iph
->tot_len
);
984 if (skb
->len
< len
|| len
< ip_hdrlen(skb
))
987 if (skb
->len
> len
) {
988 skb2
= skb_clone(skb
, GFP_ATOMIC
);
992 err
= pskb_trim_rcsum(skb2
, len
);
997 len
-= ip_hdrlen(skb2
);
998 offset
= skb_network_offset(skb2
) + ip_hdrlen(skb2
);
999 __skb_pull(skb2
, offset
);
1000 skb_reset_transport_header(skb2
);
1003 if (!pskb_may_pull(skb2
, sizeof(*ih
)))
1006 switch (skb2
->ip_summed
) {
1007 case CHECKSUM_COMPLETE
:
1008 if (!csum_fold(skb2
->csum
))
1013 if (skb_checksum_complete(skb2
))
1019 BR_INPUT_SKB_CB(skb
)->igmp
= 1;
1020 ih
= igmp_hdr(skb2
);
1023 case IGMP_HOST_MEMBERSHIP_REPORT
:
1024 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1025 BR_INPUT_SKB_CB(skb2
)->mrouters_only
= 1;
1026 err
= br_multicast_add_group(br
, port
, ih
->group
);
1028 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1029 err
= br_multicast_igmp3_report(br
, port
, skb2
);
1031 case IGMP_HOST_MEMBERSHIP_QUERY
:
1032 err
= br_multicast_query(br
, port
, skb2
);
1034 case IGMP_HOST_LEAVE_MESSAGE
:
1035 br_multicast_leave_group(br
, port
, ih
->group
);
1040 __skb_push(skb2
, offset
);
1047 int br_multicast_rcv(struct net_bridge
*br
, struct net_bridge_port
*port
,
1048 struct sk_buff
*skb
)
1050 if (br
->multicast_disabled
)
1053 switch (skb
->protocol
) {
1054 case htons(ETH_P_IP
):
1055 return br_multicast_ipv4_rcv(br
, port
, skb
);
1061 static void br_multicast_query_expired(unsigned long data
)
1063 struct net_bridge
*br
= (void *)data
;
1065 spin_lock(&br
->multicast_lock
);
1066 if (br
->multicast_startup_queries_sent
<
1067 br
->multicast_startup_query_count
)
1068 br
->multicast_startup_queries_sent
++;
1070 br_multicast_send_query(br
, NULL
, br
->multicast_startup_queries_sent
);
1072 spin_unlock(&br
->multicast_lock
);
1075 void br_multicast_init(struct net_bridge
*br
)
1077 br
->hash_elasticity
= 4;
1080 br
->multicast_router
= 1;
1081 br
->multicast_last_member_count
= 2;
1082 br
->multicast_startup_query_count
= 2;
1084 br
->multicast_last_member_interval
= HZ
;
1085 br
->multicast_query_response_interval
= 10 * HZ
;
1086 br
->multicast_startup_query_interval
= 125 * HZ
/ 4;
1087 br
->multicast_query_interval
= 125 * HZ
;
1088 br
->multicast_querier_interval
= 255 * HZ
;
1089 br
->multicast_membership_interval
= 260 * HZ
;
1091 spin_lock_init(&br
->multicast_lock
);
1092 setup_timer(&br
->multicast_router_timer
,
1093 br_multicast_local_router_expired
, 0);
1094 setup_timer(&br
->multicast_querier_timer
,
1095 br_multicast_local_router_expired
, 0);
1096 setup_timer(&br
->multicast_query_timer
, br_multicast_query_expired
,
1100 void br_multicast_open(struct net_bridge
*br
)
1102 br
->multicast_startup_queries_sent
= 0;
1104 if (br
->multicast_disabled
)
1107 mod_timer(&br
->multicast_query_timer
, jiffies
);
1110 void br_multicast_stop(struct net_bridge
*br
)
1112 struct net_bridge_mdb_htable
*mdb
;
1113 struct net_bridge_mdb_entry
*mp
;
1114 struct hlist_node
*p
, *n
;
1118 del_timer_sync(&br
->multicast_router_timer
);
1119 del_timer_sync(&br
->multicast_querier_timer
);
1120 del_timer_sync(&br
->multicast_query_timer
);
1122 spin_lock_bh(&br
->multicast_lock
);
1130 for (i
= 0; i
< mdb
->max
; i
++) {
1131 hlist_for_each_entry_safe(mp
, p
, n
, &mdb
->mhash
[i
],
1133 del_timer(&mp
->timer
);
1134 del_timer(&mp
->query_timer
);
1135 call_rcu_bh(&mp
->rcu
, br_multicast_free_group
);
1140 spin_unlock_bh(&br
->multicast_lock
);
1142 spin_lock_bh(&br
->multicast_lock
);
1147 call_rcu_bh(&mdb
->rcu
, br_mdb_free
);
1150 spin_unlock_bh(&br
->multicast_lock
);
1153 int br_multicast_set_router(struct net_bridge
*br
, unsigned long val
)
1157 spin_lock_bh(&br
->multicast_lock
);
1158 if (!netif_running(br
->dev
))
1164 del_timer(&br
->multicast_router_timer
);
1167 br
->multicast_router
= val
;
1177 spin_unlock_bh(&br
->multicast_lock
);
1182 int br_multicast_set_port_router(struct net_bridge_port
*p
, unsigned long val
)
1184 struct net_bridge
*br
= p
->br
;
1187 spin_lock(&br
->multicast_lock
);
1188 if (!netif_running(br
->dev
) || p
->state
== BR_STATE_DISABLED
)
1195 p
->multicast_router
= val
;
1198 if (val
< 2 && !hlist_unhashed(&p
->rlist
))
1199 hlist_del_init_rcu(&p
->rlist
);
1204 del_timer(&p
->multicast_router_timer
);
1209 br_multicast_add_router(br
, p
);
1218 spin_unlock(&br
->multicast_lock
);
1223 int br_multicast_toggle(struct net_bridge
*br
, unsigned long val
)
1225 struct net_bridge_port
*port
;
1228 spin_lock(&br
->multicast_lock
);
1229 if (!netif_running(br
->dev
))
1233 if (br
->multicast_disabled
== !val
)
1236 br
->multicast_disabled
= !val
;
1237 if (br
->multicast_disabled
)
1244 br
->multicast_disabled
= !!val
;
1248 err
= br_mdb_rehash(&br
->mdb
, br
->mdb
->max
,
1249 br
->hash_elasticity
);
1254 br_multicast_open(br
);
1255 list_for_each_entry(port
, &br
->port_list
, list
) {
1256 if (port
->state
== BR_STATE_DISABLED
||
1257 port
->state
== BR_STATE_BLOCKING
)
1260 __br_multicast_enable_port(port
);
1264 spin_unlock(&br
->multicast_lock
);
1269 int br_multicast_set_hash_max(struct net_bridge
*br
, unsigned long val
)
1274 spin_lock(&br
->multicast_lock
);
1275 if (!netif_running(br
->dev
))
1279 if (!is_power_of_2(val
))
1281 if (br
->mdb
&& val
< br
->mdb
->size
)
1297 err
= br_mdb_rehash(&br
->mdb
, br
->hash_max
,
1298 br
->hash_elasticity
);
1304 spin_unlock(&br
->multicast_lock
);