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
, (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(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
)
52 return __br_mdb_ip_get(mdb
, dst
, br_ip_hash(mdb
, dst
));
55 struct net_bridge_mdb_entry
*br_mdb_get(struct net_bridge
*br
,
58 struct net_bridge_mdb_htable
*mdb
= br
->mdb
;
60 if (!mdb
|| br
->multicast_disabled
)
63 switch (skb
->protocol
) {
65 if (BR_INPUT_SKB_CB(skb
)->igmp
)
67 return br_mdb_ip_get(mdb
, ip_hdr(skb
)->daddr
);
73 static void br_mdb_free(struct rcu_head
*head
)
75 struct net_bridge_mdb_htable
*mdb
=
76 container_of(head
, struct net_bridge_mdb_htable
, rcu
);
77 struct net_bridge_mdb_htable
*old
= mdb
->old
;
84 static int br_mdb_copy(struct net_bridge_mdb_htable
*new,
85 struct net_bridge_mdb_htable
*old
,
88 struct net_bridge_mdb_entry
*mp
;
94 for (i
= 0; i
< old
->max
; i
++)
95 hlist_for_each_entry(mp
, p
, &old
->mhash
[i
], hlist
[old
->ver
])
96 hlist_add_head(&mp
->hlist
[new->ver
],
97 &new->mhash
[br_ip_hash(new, mp
->addr
)]);
103 for (i
= 0; i
< new->max
; i
++) {
105 hlist_for_each_entry(mp
, p
, &new->mhash
[i
], hlist
[new->ver
])
111 return maxlen
> elasticity
? -EINVAL
: 0;
114 static void br_multicast_free_pg(struct rcu_head
*head
)
116 struct net_bridge_port_group
*p
=
117 container_of(head
, struct net_bridge_port_group
, rcu
);
122 static void br_multicast_free_group(struct rcu_head
*head
)
124 struct net_bridge_mdb_entry
*mp
=
125 container_of(head
, struct net_bridge_mdb_entry
, rcu
);
130 static void br_multicast_group_expired(unsigned long data
)
132 struct net_bridge_mdb_entry
*mp
= (void *)data
;
133 struct net_bridge
*br
= mp
->br
;
134 struct net_bridge_mdb_htable
*mdb
;
136 spin_lock(&br
->multicast_lock
);
137 if (!netif_running(br
->dev
) || timer_pending(&mp
->timer
))
140 if (!hlist_unhashed(&mp
->mglist
))
141 hlist_del_init(&mp
->mglist
);
147 hlist_del_rcu(&mp
->hlist
[mdb
->ver
]);
150 del_timer(&mp
->query_timer
);
151 call_rcu_bh(&mp
->rcu
, br_multicast_free_group
);
154 spin_unlock(&br
->multicast_lock
);
157 static void br_multicast_del_pg(struct net_bridge
*br
,
158 struct net_bridge_port_group
*pg
)
160 struct net_bridge_mdb_htable
*mdb
= br
->mdb
;
161 struct net_bridge_mdb_entry
*mp
;
162 struct net_bridge_port_group
*p
;
163 struct net_bridge_port_group
**pp
;
165 mp
= br_mdb_ip_get(mdb
, pg
->addr
);
169 for (pp
= &mp
->ports
; (p
= *pp
); pp
= &p
->next
) {
174 hlist_del_init(&p
->mglist
);
175 del_timer(&p
->timer
);
176 del_timer(&p
->query_timer
);
177 call_rcu_bh(&p
->rcu
, br_multicast_free_pg
);
179 if (!mp
->ports
&& hlist_unhashed(&mp
->mglist
) &&
180 netif_running(br
->dev
))
181 mod_timer(&mp
->timer
, jiffies
);
189 static void br_multicast_port_group_expired(unsigned long data
)
191 struct net_bridge_port_group
*pg
= (void *)data
;
192 struct net_bridge
*br
= pg
->port
->br
;
194 spin_lock(&br
->multicast_lock
);
195 if (!netif_running(br
->dev
) || timer_pending(&pg
->timer
) ||
196 hlist_unhashed(&pg
->mglist
))
199 br_multicast_del_pg(br
, pg
);
202 spin_unlock(&br
->multicast_lock
);
205 static int br_mdb_rehash(struct net_bridge_mdb_htable
**mdbp
, int max
,
208 struct net_bridge_mdb_htable
*old
= *mdbp
;
209 struct net_bridge_mdb_htable
*mdb
;
212 mdb
= kmalloc(sizeof(*mdb
), GFP_ATOMIC
);
219 mdb
->mhash
= kzalloc(max
* sizeof(*mdb
->mhash
), GFP_ATOMIC
);
225 mdb
->size
= old
? old
->size
: 0;
226 mdb
->ver
= old
? old
->ver
^ 1 : 0;
228 if (!old
|| elasticity
)
229 get_random_bytes(&mdb
->secret
, sizeof(mdb
->secret
));
231 mdb
->secret
= old
->secret
;
236 err
= br_mdb_copy(mdb
, old
, elasticity
);
243 call_rcu_bh(&mdb
->rcu
, br_mdb_free
);
246 rcu_assign_pointer(*mdbp
, mdb
);
251 static struct sk_buff
*br_multicast_alloc_query(struct net_bridge
*br
,
259 skb
= netdev_alloc_skb_ip_align(br
->dev
, sizeof(*eth
) + sizeof(*iph
) +
264 skb
->protocol
= htons(ETH_P_IP
);
266 skb_reset_mac_header(skb
);
269 memcpy(eth
->h_source
, br
->dev
->dev_addr
, 6);
272 eth
->h_dest
[2] = 0x5e;
276 eth
->h_proto
= htons(ETH_P_IP
);
277 skb_put(skb
, sizeof(*eth
));
279 skb_set_network_header(skb
, skb
->len
);
285 iph
->tot_len
= htons(sizeof(*iph
) + sizeof(*ih
) + 4);
287 iph
->frag_off
= htons(IP_DF
);
289 iph
->protocol
= IPPROTO_IGMP
;
291 iph
->daddr
= htonl(INADDR_ALLHOSTS_GROUP
);
292 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
293 ((u8
*)&iph
[1])[1] = 4;
294 ((u8
*)&iph
[1])[2] = 0;
295 ((u8
*)&iph
[1])[3] = 0;
299 skb_set_transport_header(skb
, skb
->len
);
301 ih
->type
= IGMP_HOST_MEMBERSHIP_QUERY
;
302 ih
->code
= (group
? br
->multicast_last_member_interval
:
303 br
->multicast_query_response_interval
) /
304 (HZ
/ IGMP_TIMER_SCALE
);
307 ih
->csum
= ip_compute_csum((void *)ih
, sizeof(struct igmphdr
));
308 skb_put(skb
, sizeof(*ih
));
310 __skb_pull(skb
, sizeof(*eth
));
316 static void br_multicast_send_group_query(struct net_bridge_mdb_entry
*mp
)
318 struct net_bridge
*br
= mp
->br
;
321 skb
= br_multicast_alloc_query(br
, mp
->addr
);
328 if (++mp
->queries_sent
< br
->multicast_last_member_count
)
329 mod_timer(&mp
->query_timer
,
330 jiffies
+ br
->multicast_last_member_interval
);
333 static void br_multicast_group_query_expired(unsigned long data
)
335 struct net_bridge_mdb_entry
*mp
= (void *)data
;
336 struct net_bridge
*br
= mp
->br
;
338 spin_lock(&br
->multicast_lock
);
339 if (!netif_running(br
->dev
) || hlist_unhashed(&mp
->mglist
) ||
340 mp
->queries_sent
>= br
->multicast_last_member_count
)
343 br_multicast_send_group_query(mp
);
346 spin_unlock(&br
->multicast_lock
);
349 static void br_multicast_send_port_group_query(struct net_bridge_port_group
*pg
)
351 struct net_bridge_port
*port
= pg
->port
;
352 struct net_bridge
*br
= port
->br
;
355 skb
= br_multicast_alloc_query(br
, pg
->addr
);
359 br_deliver(port
, skb
);
362 if (++pg
->queries_sent
< br
->multicast_last_member_count
)
363 mod_timer(&pg
->query_timer
,
364 jiffies
+ br
->multicast_last_member_interval
);
367 static void br_multicast_port_group_query_expired(unsigned long data
)
369 struct net_bridge_port_group
*pg
= (void *)data
;
370 struct net_bridge_port
*port
= pg
->port
;
371 struct net_bridge
*br
= port
->br
;
373 spin_lock(&br
->multicast_lock
);
374 if (!netif_running(br
->dev
) || hlist_unhashed(&pg
->mglist
) ||
375 pg
->queries_sent
>= br
->multicast_last_member_count
)
378 br_multicast_send_port_group_query(pg
);
381 spin_unlock(&br
->multicast_lock
);
384 static struct net_bridge_mdb_entry
*br_multicast_get_group(
385 struct net_bridge
*br
, struct net_bridge_port
*port
, __be32 group
,
388 struct net_bridge_mdb_htable
*mdb
= br
->mdb
;
389 struct net_bridge_mdb_entry
*mp
;
390 struct hlist_node
*p
;
396 hlist_for_each_entry(mp
, p
, &mdb
->mhash
[hash
], hlist
[mdb
->ver
]) {
398 if (unlikely(group
== mp
->addr
)) {
406 if (unlikely(count
> br
->hash_elasticity
&& count
)) {
408 printk(KERN_INFO
"%s: Multicast hash table "
409 "chain limit reached: %s\n",
410 br
->dev
->name
, port
? port
->dev
->name
:
413 elasticity
= br
->hash_elasticity
;
416 if (mdb
->size
>= max
) {
418 if (unlikely(max
>= br
->hash_max
)) {
419 printk(KERN_WARNING
"%s: Multicast hash table maximum "
420 "reached, disabling snooping: %s, %d\n",
421 br
->dev
->name
, port
? port
->dev
->name
:
426 br
->multicast_disabled
= 1;
431 if (max
> mdb
->max
|| elasticity
) {
434 printk(KERN_INFO
"%s: Multicast hash table "
436 br
->dev
->name
, port
? port
->dev
->name
:
442 err
= br_mdb_rehash(&br
->mdb
, max
, elasticity
);
444 printk(KERN_WARNING
"%s: Cannot rehash multicast "
445 "hash table, disabling snooping: "
447 br
->dev
->name
, port
? port
->dev
->name
:
464 static struct net_bridge_mdb_entry
*br_multicast_new_group(
465 struct net_bridge
*br
, struct net_bridge_port
*port
, __be32 group
)
467 struct net_bridge_mdb_htable
*mdb
= br
->mdb
;
468 struct net_bridge_mdb_entry
*mp
;
472 if (br_mdb_rehash(&br
->mdb
, BR_HASH_SIZE
, 0))
477 hash
= br_ip_hash(mdb
, group
);
478 mp
= br_multicast_get_group(br
, port
, group
, hash
);
479 switch (PTR_ERR(mp
)) {
486 hash
= br_ip_hash(mdb
, group
);
493 mp
= kzalloc(sizeof(*mp
), GFP_ATOMIC
);
499 setup_timer(&mp
->timer
, br_multicast_group_expired
,
501 setup_timer(&mp
->query_timer
, br_multicast_group_query_expired
,
504 hlist_add_head_rcu(&mp
->hlist
[mdb
->ver
], &mdb
->mhash
[hash
]);
511 static int br_multicast_add_group(struct net_bridge
*br
,
512 struct net_bridge_port
*port
, __be32 group
)
514 struct net_bridge_mdb_entry
*mp
;
515 struct net_bridge_port_group
*p
;
516 struct net_bridge_port_group
**pp
;
517 unsigned long now
= jiffies
;
520 if (ipv4_is_local_multicast(group
))
523 spin_lock(&br
->multicast_lock
);
524 if (!netif_running(br
->dev
) ||
525 (port
&& port
->state
== BR_STATE_DISABLED
))
528 mp
= br_multicast_new_group(br
, port
, group
);
530 if (unlikely(IS_ERR(mp
) || !mp
))
534 hlist_add_head(&mp
->mglist
, &br
->mglist
);
535 mod_timer(&mp
->timer
, now
+ br
->multicast_membership_interval
);
539 for (pp
= &mp
->ports
; (p
= *pp
); pp
= &p
->next
) {
542 if ((unsigned long)p
->port
< (unsigned long)port
)
546 p
= kzalloc(sizeof(*p
), GFP_ATOMIC
);
554 hlist_add_head(&p
->mglist
, &port
->mglist
);
555 setup_timer(&p
->timer
, br_multicast_port_group_expired
,
557 setup_timer(&p
->query_timer
, br_multicast_port_group_query_expired
,
560 rcu_assign_pointer(*pp
, p
);
563 mod_timer(&p
->timer
, now
+ br
->multicast_membership_interval
);
568 spin_unlock(&br
->multicast_lock
);
572 static void br_multicast_router_expired(unsigned long data
)
574 struct net_bridge_port
*port
= (void *)data
;
575 struct net_bridge
*br
= port
->br
;
577 spin_lock(&br
->multicast_lock
);
578 if (port
->multicast_router
!= 1 ||
579 timer_pending(&port
->multicast_router_timer
) ||
580 hlist_unhashed(&port
->rlist
))
583 hlist_del_init_rcu(&port
->rlist
);
586 spin_unlock(&br
->multicast_lock
);
589 static void br_multicast_local_router_expired(unsigned long data
)
593 static void br_multicast_send_query(struct net_bridge
*br
,
594 struct net_bridge_port
*port
, u32 sent
)
599 if (!netif_running(br
->dev
) || br
->multicast_disabled
||
600 timer_pending(&br
->multicast_querier_timer
))
603 skb
= br_multicast_alloc_query(br
, 0);
608 __skb_push(skb
, sizeof(struct ethhdr
));
609 skb
->dev
= port
->dev
;
610 NF_HOOK(PF_BRIDGE
, NF_BR_LOCAL_OUT
, skb
, NULL
, skb
->dev
,
617 time
+= sent
< br
->multicast_startup_query_count
?
618 br
->multicast_startup_query_interval
:
619 br
->multicast_query_interval
;
620 mod_timer(port
? &port
->multicast_query_timer
:
621 &br
->multicast_query_timer
, time
);
624 static void br_multicast_port_query_expired(unsigned long data
)
626 struct net_bridge_port
*port
= (void *)data
;
627 struct net_bridge
*br
= port
->br
;
629 spin_lock(&br
->multicast_lock
);
630 if (port
&& (port
->state
== BR_STATE_DISABLED
||
631 port
->state
== BR_STATE_BLOCKING
))
634 if (port
->multicast_startup_queries_sent
<
635 br
->multicast_startup_query_count
)
636 port
->multicast_startup_queries_sent
++;
638 br_multicast_send_query(port
->br
, port
,
639 port
->multicast_startup_queries_sent
);
642 spin_unlock(&br
->multicast_lock
);
645 void br_multicast_add_port(struct net_bridge_port
*port
)
647 port
->multicast_router
= 1;
649 setup_timer(&port
->multicast_router_timer
, br_multicast_router_expired
,
650 (unsigned long)port
);
651 setup_timer(&port
->multicast_query_timer
,
652 br_multicast_port_query_expired
, (unsigned long)port
);
655 void br_multicast_del_port(struct net_bridge_port
*port
)
657 del_timer_sync(&port
->multicast_router_timer
);
660 static void __br_multicast_enable_port(struct net_bridge_port
*port
)
662 port
->multicast_startup_queries_sent
= 0;
664 if (try_to_del_timer_sync(&port
->multicast_query_timer
) >= 0 ||
665 del_timer(&port
->multicast_query_timer
))
666 mod_timer(&port
->multicast_query_timer
, jiffies
);
669 void br_multicast_enable_port(struct net_bridge_port
*port
)
671 struct net_bridge
*br
= port
->br
;
673 spin_lock(&br
->multicast_lock
);
674 if (br
->multicast_disabled
|| !netif_running(br
->dev
))
677 __br_multicast_enable_port(port
);
680 spin_unlock(&br
->multicast_lock
);
683 void br_multicast_disable_port(struct net_bridge_port
*port
)
685 struct net_bridge
*br
= port
->br
;
686 struct net_bridge_port_group
*pg
;
687 struct hlist_node
*p
, *n
;
689 spin_lock(&br
->multicast_lock
);
690 hlist_for_each_entry_safe(pg
, p
, n
, &port
->mglist
, mglist
)
691 br_multicast_del_pg(br
, pg
);
693 if (!hlist_unhashed(&port
->rlist
))
694 hlist_del_init_rcu(&port
->rlist
);
695 del_timer(&port
->multicast_router_timer
);
696 del_timer(&port
->multicast_query_timer
);
697 spin_unlock(&br
->multicast_lock
);
700 static int br_multicast_igmp3_report(struct net_bridge
*br
,
701 struct net_bridge_port
*port
,
704 struct igmpv3_report
*ih
;
705 struct igmpv3_grec
*grec
;
713 if (!pskb_may_pull(skb
, sizeof(*ih
)))
716 ih
= igmpv3_report_hdr(skb
);
717 num
= ntohs(ih
->ngrec
);
720 for (i
= 0; i
< num
; i
++) {
721 len
+= sizeof(*grec
);
722 if (!pskb_may_pull(skb
, len
))
725 grec
= (void *)(skb
->data
+ len
);
726 group
= grec
->grec_mca
;
727 type
= grec
->grec_type
;
729 len
+= grec
->grec_nsrcs
* 4;
730 if (!pskb_may_pull(skb
, len
))
733 /* We treat this as an IGMPv2 report for now. */
735 case IGMPV3_MODE_IS_INCLUDE
:
736 case IGMPV3_MODE_IS_EXCLUDE
:
737 case IGMPV3_CHANGE_TO_INCLUDE
:
738 case IGMPV3_CHANGE_TO_EXCLUDE
:
739 case IGMPV3_ALLOW_NEW_SOURCES
:
740 case IGMPV3_BLOCK_OLD_SOURCES
:
747 err
= br_multicast_add_group(br
, port
, group
);
755 static void br_multicast_add_router(struct net_bridge
*br
,
756 struct net_bridge_port
*port
)
758 struct hlist_node
*p
;
759 struct hlist_node
**h
;
761 for (h
= &br
->router_list
.first
;
763 (unsigned long)container_of(p
, struct net_bridge_port
, rlist
) >
768 port
->rlist
.pprev
= h
;
769 port
->rlist
.next
= p
;
770 rcu_assign_pointer(*h
, &port
->rlist
);
772 p
->pprev
= &port
->rlist
.next
;
775 static void br_multicast_mark_router(struct net_bridge
*br
,
776 struct net_bridge_port
*port
)
778 unsigned long now
= jiffies
;
781 if (br
->multicast_router
== 1)
782 mod_timer(&br
->multicast_router_timer
,
783 now
+ br
->multicast_querier_interval
);
787 if (port
->multicast_router
!= 1)
790 if (!hlist_unhashed(&port
->rlist
))
793 br_multicast_add_router(br
, port
);
796 mod_timer(&port
->multicast_router_timer
,
797 now
+ br
->multicast_querier_interval
);
800 static void br_multicast_query_received(struct net_bridge
*br
,
801 struct net_bridge_port
*port
,
805 mod_timer(&br
->multicast_querier_timer
,
806 jiffies
+ br
->multicast_querier_interval
);
807 else if (timer_pending(&br
->multicast_querier_timer
))
810 br_multicast_mark_router(br
, port
);
813 static int br_multicast_query(struct net_bridge
*br
,
814 struct net_bridge_port
*port
,
817 struct iphdr
*iph
= ip_hdr(skb
);
818 struct igmphdr
*ih
= igmp_hdr(skb
);
819 struct net_bridge_mdb_entry
*mp
;
820 struct igmpv3_query
*ih3
;
821 struct net_bridge_port_group
*p
;
822 struct net_bridge_port_group
**pp
;
823 unsigned long max_delay
;
824 unsigned long now
= jiffies
;
827 spin_lock(&br
->multicast_lock
);
828 if (!netif_running(br
->dev
) ||
829 (port
&& port
->state
== BR_STATE_DISABLED
))
832 br_multicast_query_received(br
, port
, iph
->saddr
);
836 if (skb
->len
== sizeof(*ih
)) {
837 max_delay
= ih
->code
* (HZ
/ IGMP_TIMER_SCALE
);
844 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)))
847 ih3
= igmpv3_query_hdr(skb
);
851 max_delay
= ih3
->code
? 1 :
852 IGMPV3_MRC(ih3
->code
) * (HZ
/ IGMP_TIMER_SCALE
);
858 mp
= br_mdb_ip_get(br
->mdb
, group
);
862 max_delay
*= br
->multicast_last_member_count
;
864 if (!hlist_unhashed(&mp
->mglist
) &&
865 (timer_pending(&mp
->timer
) ?
866 time_after(mp
->timer
.expires
, now
+ max_delay
) :
867 try_to_del_timer_sync(&mp
->timer
) >= 0))
868 mod_timer(&mp
->timer
, now
+ max_delay
);
870 for (pp
= &mp
->ports
; (p
= *pp
); pp
= &p
->next
) {
871 if (timer_pending(&p
->timer
) ?
872 time_after(p
->timer
.expires
, now
+ max_delay
) :
873 try_to_del_timer_sync(&p
->timer
) >= 0)
874 mod_timer(&mp
->timer
, now
+ max_delay
);
878 spin_unlock(&br
->multicast_lock
);
882 static void br_multicast_leave_group(struct net_bridge
*br
,
883 struct net_bridge_port
*port
,
886 struct net_bridge_mdb_htable
*mdb
;
887 struct net_bridge_mdb_entry
*mp
;
888 struct net_bridge_port_group
*p
;
892 if (ipv4_is_local_multicast(group
))
895 spin_lock(&br
->multicast_lock
);
896 if (!netif_running(br
->dev
) ||
897 (port
&& port
->state
== BR_STATE_DISABLED
) ||
898 timer_pending(&br
->multicast_querier_timer
))
902 mp
= br_mdb_ip_get(mdb
, group
);
907 time
= now
+ br
->multicast_last_member_count
*
908 br
->multicast_last_member_interval
;
911 if (!hlist_unhashed(&mp
->mglist
) &&
912 (timer_pending(&mp
->timer
) ?
913 time_after(mp
->timer
.expires
, time
) :
914 try_to_del_timer_sync(&mp
->timer
) >= 0)) {
915 mod_timer(&mp
->timer
, time
);
917 mp
->queries_sent
= 0;
918 mod_timer(&mp
->query_timer
, now
);
924 for (p
= mp
->ports
; p
; p
= p
->next
) {
928 if (!hlist_unhashed(&p
->mglist
) &&
929 (timer_pending(&p
->timer
) ?
930 time_after(p
->timer
.expires
, time
) :
931 try_to_del_timer_sync(&p
->timer
) >= 0)) {
932 mod_timer(&p
->timer
, time
);
935 mod_timer(&p
->query_timer
, now
);
942 spin_unlock(&br
->multicast_lock
);
945 static int br_multicast_ipv4_rcv(struct net_bridge
*br
,
946 struct net_bridge_port
*port
,
949 struct sk_buff
*skb2
= skb
;
956 BR_INPUT_SKB_CB(skb
)->igmp
= 0;
957 BR_INPUT_SKB_CB(skb
)->mrouters_only
= 0;
959 /* We treat OOM as packet loss for now. */
960 if (!pskb_may_pull(skb
, sizeof(*iph
)))
965 if (iph
->ihl
< 5 || iph
->version
!= 4)
968 if (!pskb_may_pull(skb
, ip_hdrlen(skb
)))
973 if (unlikely(ip_fast_csum((u8
*)iph
, iph
->ihl
)))
976 if (iph
->protocol
!= IPPROTO_IGMP
)
979 len
= ntohs(iph
->tot_len
);
980 if (skb
->len
< len
|| len
< ip_hdrlen(skb
))
983 if (skb
->len
> len
) {
984 skb2
= skb_clone(skb
, GFP_ATOMIC
);
988 err
= pskb_trim_rcsum(skb2
, len
);
993 len
-= ip_hdrlen(skb2
);
994 offset
= skb_network_offset(skb2
) + ip_hdrlen(skb2
);
995 __skb_pull(skb2
, offset
);
996 skb_reset_transport_header(skb2
);
999 if (!pskb_may_pull(skb2
, sizeof(*ih
)))
1004 switch (skb2
->ip_summed
) {
1005 case CHECKSUM_COMPLETE
:
1006 if (!csum_fold(skb2
->csum
))
1011 if (skb_checksum_complete(skb2
))
1017 BR_INPUT_SKB_CB(skb
)->igmp
= 1;
1018 ih
= igmp_hdr(skb2
);
1021 case IGMP_HOST_MEMBERSHIP_REPORT
:
1022 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1023 BR_INPUT_SKB_CB(skb2
)->mrouters_only
= 1;
1024 err
= br_multicast_add_group(br
, port
, ih
->group
);
1026 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1027 err
= br_multicast_igmp3_report(br
, port
, skb2
);
1029 case IGMP_HOST_MEMBERSHIP_QUERY
:
1030 err
= br_multicast_query(br
, port
, skb2
);
1032 case IGMP_HOST_LEAVE_MESSAGE
:
1033 br_multicast_leave_group(br
, port
, ih
->group
);
1038 __skb_push(skb2
, offset
);
1044 int br_multicast_rcv(struct net_bridge
*br
, struct net_bridge_port
*port
,
1045 struct sk_buff
*skb
)
1047 if (br
->multicast_disabled
)
1050 switch (skb
->protocol
) {
1051 case htons(ETH_P_IP
):
1052 return br_multicast_ipv4_rcv(br
, port
, skb
);
1058 static void br_multicast_query_expired(unsigned long data
)
1060 struct net_bridge
*br
= (void *)data
;
1062 spin_lock(&br
->multicast_lock
);
1063 if (br
->multicast_startup_queries_sent
<
1064 br
->multicast_startup_query_count
)
1065 br
->multicast_startup_queries_sent
++;
1067 br_multicast_send_query(br
, NULL
, br
->multicast_startup_queries_sent
);
1069 spin_unlock(&br
->multicast_lock
);
1072 void br_multicast_init(struct net_bridge
*br
)
1074 br
->hash_elasticity
= 4;
1077 br
->multicast_router
= 1;
1078 br
->multicast_last_member_count
= 2;
1079 br
->multicast_startup_query_count
= 2;
1081 br
->multicast_last_member_interval
= HZ
;
1082 br
->multicast_query_response_interval
= 10 * HZ
;
1083 br
->multicast_startup_query_interval
= 125 * HZ
/ 4;
1084 br
->multicast_query_interval
= 125 * HZ
;
1085 br
->multicast_querier_interval
= 255 * HZ
;
1086 br
->multicast_membership_interval
= 260 * HZ
;
1088 spin_lock_init(&br
->multicast_lock
);
1089 setup_timer(&br
->multicast_router_timer
,
1090 br_multicast_local_router_expired
, 0);
1091 setup_timer(&br
->multicast_querier_timer
,
1092 br_multicast_local_router_expired
, 0);
1093 setup_timer(&br
->multicast_query_timer
, br_multicast_query_expired
,
1097 void br_multicast_open(struct net_bridge
*br
)
1099 br
->multicast_startup_queries_sent
= 0;
1101 if (br
->multicast_disabled
)
1104 mod_timer(&br
->multicast_query_timer
, jiffies
);
1107 void br_multicast_stop(struct net_bridge
*br
)
1109 struct net_bridge_mdb_htable
*mdb
;
1110 struct net_bridge_mdb_entry
*mp
;
1111 struct hlist_node
*p
, *n
;
1115 del_timer_sync(&br
->multicast_router_timer
);
1116 del_timer_sync(&br
->multicast_querier_timer
);
1117 del_timer_sync(&br
->multicast_query_timer
);
1119 spin_lock_bh(&br
->multicast_lock
);
1127 for (i
= 0; i
< mdb
->max
; i
++) {
1128 hlist_for_each_entry_safe(mp
, p
, n
, &mdb
->mhash
[i
],
1130 del_timer(&mp
->timer
);
1131 del_timer(&mp
->query_timer
);
1132 call_rcu_bh(&mp
->rcu
, br_multicast_free_group
);
1137 spin_unlock_bh(&br
->multicast_lock
);
1138 synchronize_rcu_bh();
1139 spin_lock_bh(&br
->multicast_lock
);
1144 call_rcu_bh(&mdb
->rcu
, br_mdb_free
);
1147 spin_unlock_bh(&br
->multicast_lock
);
1150 int br_multicast_set_router(struct net_bridge
*br
, unsigned long val
)
1154 spin_lock_bh(&br
->multicast_lock
);
1155 if (!netif_running(br
->dev
))
1161 del_timer(&br
->multicast_router_timer
);
1164 br
->multicast_router
= val
;
1174 spin_unlock_bh(&br
->multicast_lock
);
1179 int br_multicast_set_port_router(struct net_bridge_port
*p
, unsigned long val
)
1181 struct net_bridge
*br
= p
->br
;
1184 spin_lock(&br
->multicast_lock
);
1185 if (!netif_running(br
->dev
) || p
->state
== BR_STATE_DISABLED
)
1192 p
->multicast_router
= val
;
1195 if (val
< 2 && !hlist_unhashed(&p
->rlist
))
1196 hlist_del_init_rcu(&p
->rlist
);
1201 del_timer(&p
->multicast_router_timer
);
1206 br_multicast_add_router(br
, p
);
1215 spin_unlock(&br
->multicast_lock
);
1220 int br_multicast_toggle(struct net_bridge
*br
, unsigned long val
)
1222 struct net_bridge_port
*port
;
1225 spin_lock(&br
->multicast_lock
);
1226 if (!netif_running(br
->dev
))
1230 if (br
->multicast_disabled
== !val
)
1233 br
->multicast_disabled
= !val
;
1234 if (br
->multicast_disabled
)
1241 br
->multicast_disabled
= !!val
;
1245 err
= br_mdb_rehash(&br
->mdb
, br
->mdb
->max
,
1246 br
->hash_elasticity
);
1251 br_multicast_open(br
);
1252 list_for_each_entry(port
, &br
->port_list
, list
) {
1253 if (port
->state
== BR_STATE_DISABLED
||
1254 port
->state
== BR_STATE_BLOCKING
)
1257 __br_multicast_enable_port(port
);
1261 spin_unlock(&br
->multicast_lock
);
1266 int br_multicast_set_hash_max(struct net_bridge
*br
, unsigned long val
)
1271 spin_lock(&br
->multicast_lock
);
1272 if (!netif_running(br
->dev
))
1276 if (!is_power_of_2(val
))
1278 if (br
->mdb
&& val
< br
->mdb
->size
)
1294 err
= br_mdb_rehash(&br
->mdb
, br
->hash_max
,
1295 br
->hash_elasticity
);
1301 spin_unlock(&br
->multicast_lock
);