aboutsummaryrefslogtreecommitdiffstats
path: root/net/bridge/br_multicast.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/bridge/br_multicast.c')
-rw-r--r--net/bridge/br_multicast.c690
1 files changed, 595 insertions, 95 deletions
diff --git a/net/bridge/br_multicast.c b/net/bridge/br_multicast.c
index eaa0e1bae49..9d21d98ae5f 100644
--- a/net/bridge/br_multicast.c
+++ b/net/bridge/br_multicast.c
@@ -24,51 +24,139 @@
24#include <linux/slab.h> 24#include <linux/slab.h>
25#include <linux/timer.h> 25#include <linux/timer.h>
26#include <net/ip.h> 26#include <net/ip.h>
27#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
28#include <net/ipv6.h>
29#include <net/mld.h>
30#include <net/addrconf.h>
31#include <net/ip6_checksum.h>
32#endif
27 33
28#include "br_private.h" 34#include "br_private.h"
29 35
30static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb, __be32 ip) 36#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
37static inline int ipv6_is_local_multicast(const struct in6_addr *addr)
31{ 38{
32 return jhash_1word(mdb->secret, (u32)ip) & (mdb->max - 1); 39 if (ipv6_addr_is_multicast(addr) &&
40 IPV6_ADDR_MC_SCOPE(addr) <= IPV6_ADDR_SCOPE_LINKLOCAL)
41 return 1;
42 return 0;
43}
44#endif
45
46static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
47{
48 if (a->proto != b->proto)
49 return 0;
50 switch (a->proto) {
51 case htons(ETH_P_IP):
52 return a->u.ip4 == b->u.ip4;
53#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
54 case htons(ETH_P_IPV6):
55 return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
56#endif
57 }
58 return 0;
59}
60
61static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip)
62{
63 return jhash_1word(mdb->secret, (__force u32)ip) & (mdb->max - 1);
64}
65
66#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
67static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb,
68 const struct in6_addr *ip)
69{
70 return jhash2((__force u32 *)ip->s6_addr32, 4, mdb->secret) & (mdb->max - 1);
71}
72#endif
73
74static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb,
75 struct br_ip *ip)
76{
77 switch (ip->proto) {
78 case htons(ETH_P_IP):
79 return __br_ip4_hash(mdb, ip->u.ip4);
80#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
81 case htons(ETH_P_IPV6):
82 return __br_ip6_hash(mdb, &ip->u.ip6);
83#endif
84 }
85 return 0;
33} 86}
34 87
35static struct net_bridge_mdb_entry *__br_mdb_ip_get( 88static struct net_bridge_mdb_entry *__br_mdb_ip_get(
36 struct net_bridge_mdb_htable *mdb, __be32 dst, int hash) 89 struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash)
37{ 90{
38 struct net_bridge_mdb_entry *mp; 91 struct net_bridge_mdb_entry *mp;
39 struct hlist_node *p; 92 struct hlist_node *p;
40 93
41 hlist_for_each_entry_rcu(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) { 94 hlist_for_each_entry_rcu(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) {
42 if (dst == mp->addr) 95 if (br_ip_equal(&mp->addr, dst))
43 return mp; 96 return mp;
44 } 97 }
45 98
46 return NULL; 99 return NULL;
47} 100}
48 101
49static struct net_bridge_mdb_entry *br_mdb_ip_get( 102static struct net_bridge_mdb_entry *br_mdb_ip4_get(
50 struct net_bridge_mdb_htable *mdb, __be32 dst) 103 struct net_bridge_mdb_htable *mdb, __be32 dst)
51{ 104{
52 if (!mdb) 105 struct br_ip br_dst;
53 return NULL; 106
107 br_dst.u.ip4 = dst;
108 br_dst.proto = htons(ETH_P_IP);
109
110 return __br_mdb_ip_get(mdb, &br_dst, __br_ip4_hash(mdb, dst));
111}
112
113#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
114static struct net_bridge_mdb_entry *br_mdb_ip6_get(
115 struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst)
116{
117 struct br_ip br_dst;
118
119 ipv6_addr_copy(&br_dst.u.ip6, dst);
120 br_dst.proto = htons(ETH_P_IPV6);
54 121
122 return __br_mdb_ip_get(mdb, &br_dst, __br_ip6_hash(mdb, dst));
123}
124#endif
125
126static struct net_bridge_mdb_entry *br_mdb_ip_get(
127 struct net_bridge_mdb_htable *mdb, struct br_ip *dst)
128{
55 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst)); 129 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
56} 130}
57 131
58struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br, 132struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
59 struct sk_buff *skb) 133 struct sk_buff *skb)
60{ 134{
61 if (br->multicast_disabled) 135 struct net_bridge_mdb_htable *mdb = br->mdb;
136 struct br_ip ip;
137
138 if (!mdb || br->multicast_disabled)
62 return NULL; 139 return NULL;
63 140
141 if (BR_INPUT_SKB_CB(skb)->igmp)
142 return NULL;
143
144 ip.proto = skb->protocol;
145
64 switch (skb->protocol) { 146 switch (skb->protocol) {
65 case htons(ETH_P_IP): 147 case htons(ETH_P_IP):
66 if (BR_INPUT_SKB_CB(skb)->igmp) 148 ip.u.ip4 = ip_hdr(skb)->daddr;
67 break; 149 break;
68 return br_mdb_ip_get(br->mdb, ip_hdr(skb)->daddr); 150#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
151 case htons(ETH_P_IPV6):
152 ipv6_addr_copy(&ip.u.ip6, &ipv6_hdr(skb)->daddr);
153 break;
154#endif
155 default:
156 return NULL;
69 } 157 }
70 158
71 return NULL; 159 return br_mdb_ip_get(mdb, &ip);
72} 160}
73 161
74static void br_mdb_free(struct rcu_head *head) 162static void br_mdb_free(struct rcu_head *head)
@@ -95,7 +183,7 @@ static int br_mdb_copy(struct net_bridge_mdb_htable *new,
95 for (i = 0; i < old->max; i++) 183 for (i = 0; i < old->max; i++)
96 hlist_for_each_entry(mp, p, &old->mhash[i], hlist[old->ver]) 184 hlist_for_each_entry(mp, p, &old->mhash[i], hlist[old->ver])
97 hlist_add_head(&mp->hlist[new->ver], 185 hlist_add_head(&mp->hlist[new->ver],
98 &new->mhash[br_ip_hash(new, mp->addr)]); 186 &new->mhash[br_ip_hash(new, &mp->addr)]);
99 187
100 if (!elasticity) 188 if (!elasticity)
101 return 0; 189 return 0;
@@ -163,7 +251,7 @@ static void br_multicast_del_pg(struct net_bridge *br,
163 struct net_bridge_port_group *p; 251 struct net_bridge_port_group *p;
164 struct net_bridge_port_group **pp; 252 struct net_bridge_port_group **pp;
165 253
166 mp = br_mdb_ip_get(mdb, pg->addr); 254 mp = br_mdb_ip_get(mdb, &pg->addr);
167 if (WARN_ON(!mp)) 255 if (WARN_ON(!mp))
168 return; 256 return;
169 257
@@ -171,7 +259,7 @@ static void br_multicast_del_pg(struct net_bridge *br,
171 if (p != pg) 259 if (p != pg)
172 continue; 260 continue;
173 261
174 *pp = p->next; 262 rcu_assign_pointer(*pp, p->next);
175 hlist_del_init(&p->mglist); 263 hlist_del_init(&p->mglist);
176 del_timer(&p->timer); 264 del_timer(&p->timer);
177 del_timer(&p->query_timer); 265 del_timer(&p->query_timer);
@@ -249,8 +337,8 @@ out:
249 return 0; 337 return 0;
250} 338}
251 339
252static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br, 340static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
253 __be32 group) 341 __be32 group)
254{ 342{
255 struct sk_buff *skb; 343 struct sk_buff *skb;
256 struct igmphdr *ih; 344 struct igmphdr *ih;
@@ -314,12 +402,104 @@ out:
314 return skb; 402 return skb;
315} 403}
316 404
405#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
406static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
407 struct in6_addr *group)
408{
409 struct sk_buff *skb;
410 struct ipv6hdr *ip6h;
411 struct mld_msg *mldq;
412 struct ethhdr *eth;
413 u8 *hopopt;
414 unsigned long interval;
415
416 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
417 8 + sizeof(*mldq));
418 if (!skb)
419 goto out;
420
421 skb->protocol = htons(ETH_P_IPV6);
422
423 /* Ethernet header */
424 skb_reset_mac_header(skb);
425 eth = eth_hdr(skb);
426
427 memcpy(eth->h_source, br->dev->dev_addr, 6);
428 ipv6_eth_mc_map(group, eth->h_dest);
429 eth->h_proto = htons(ETH_P_IPV6);
430 skb_put(skb, sizeof(*eth));
431
432 /* IPv6 header + HbH option */
433 skb_set_network_header(skb, skb->len);
434 ip6h = ipv6_hdr(skb);
435
436 *(__force __be32 *)ip6h = htonl(0x60000000);
437 ip6h->payload_len = 8 + sizeof(*mldq);
438 ip6h->nexthdr = IPPROTO_HOPOPTS;
439 ip6h->hop_limit = 1;
440 ipv6_addr_set(&ip6h->saddr, 0, 0, 0, 0);
441 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
442
443 hopopt = (u8 *)(ip6h + 1);
444 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
445 hopopt[1] = 0; /* length of HbH */
446 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
447 hopopt[3] = 2; /* Length of RA Option */
448 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
449 hopopt[5] = 0;
450 hopopt[6] = IPV6_TLV_PAD0; /* Pad0 */
451 hopopt[7] = IPV6_TLV_PAD0; /* Pad0 */
452
453 skb_put(skb, sizeof(*ip6h) + 8);
454
455 /* ICMPv6 */
456 skb_set_transport_header(skb, skb->len);
457 mldq = (struct mld_msg *) icmp6_hdr(skb);
458
459 interval = ipv6_addr_any(group) ? br->multicast_last_member_interval :
460 br->multicast_query_response_interval;
461
462 mldq->mld_type = ICMPV6_MGM_QUERY;
463 mldq->mld_code = 0;
464 mldq->mld_cksum = 0;
465 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
466 mldq->mld_reserved = 0;
467 ipv6_addr_copy(&mldq->mld_mca, group);
468
469 /* checksum */
470 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
471 sizeof(*mldq), IPPROTO_ICMPV6,
472 csum_partial(mldq,
473 sizeof(*mldq), 0));
474 skb_put(skb, sizeof(*mldq));
475
476 __skb_pull(skb, sizeof(*eth));
477
478out:
479 return skb;
480}
481#endif
482
483static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
484 struct br_ip *addr)
485{
486 switch (addr->proto) {
487 case htons(ETH_P_IP):
488 return br_ip4_multicast_alloc_query(br, addr->u.ip4);
489#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
490 case htons(ETH_P_IPV6):
491 return br_ip6_multicast_alloc_query(br, &addr->u.ip6);
492#endif
493 }
494 return NULL;
495}
496
317static void br_multicast_send_group_query(struct net_bridge_mdb_entry *mp) 497static void br_multicast_send_group_query(struct net_bridge_mdb_entry *mp)
318{ 498{
319 struct net_bridge *br = mp->br; 499 struct net_bridge *br = mp->br;
320 struct sk_buff *skb; 500 struct sk_buff *skb;
321 501
322 skb = br_multicast_alloc_query(br, mp->addr); 502 skb = br_multicast_alloc_query(br, &mp->addr);
323 if (!skb) 503 if (!skb)
324 goto timer; 504 goto timer;
325 505
@@ -353,7 +533,7 @@ static void br_multicast_send_port_group_query(struct net_bridge_port_group *pg)
353 struct net_bridge *br = port->br; 533 struct net_bridge *br = port->br;
354 struct sk_buff *skb; 534 struct sk_buff *skb;
355 535
356 skb = br_multicast_alloc_query(br, pg->addr); 536 skb = br_multicast_alloc_query(br, &pg->addr);
357 if (!skb) 537 if (!skb)
358 goto timer; 538 goto timer;
359 539
@@ -383,8 +563,8 @@ out:
383} 563}
384 564
385static struct net_bridge_mdb_entry *br_multicast_get_group( 565static struct net_bridge_mdb_entry *br_multicast_get_group(
386 struct net_bridge *br, struct net_bridge_port *port, __be32 group, 566 struct net_bridge *br, struct net_bridge_port *port,
387 int hash) 567 struct br_ip *group, int hash)
388{ 568{
389 struct net_bridge_mdb_htable *mdb = br->mdb; 569 struct net_bridge_mdb_htable *mdb = br->mdb;
390 struct net_bridge_mdb_entry *mp; 570 struct net_bridge_mdb_entry *mp;
@@ -396,9 +576,8 @@ static struct net_bridge_mdb_entry *br_multicast_get_group(
396 576
397 hlist_for_each_entry(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) { 577 hlist_for_each_entry(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) {
398 count++; 578 count++;
399 if (unlikely(group == mp->addr)) { 579 if (unlikely(br_ip_equal(group, &mp->addr)))
400 return mp; 580 return mp;
401 }
402 } 581 }
403 582
404 elasticity = 0; 583 elasticity = 0;
@@ -406,10 +585,9 @@ static struct net_bridge_mdb_entry *br_multicast_get_group(
406 585
407 if (unlikely(count > br->hash_elasticity && count)) { 586 if (unlikely(count > br->hash_elasticity && count)) {
408 if (net_ratelimit()) 587 if (net_ratelimit())
409 printk(KERN_INFO "%s: Multicast hash table " 588 br_info(br, "Multicast hash table "
410 "chain limit reached: %s\n", 589 "chain limit reached: %s\n",
411 br->dev->name, port ? port->dev->name : 590 port ? port->dev->name : br->dev->name);
412 br->dev->name);
413 591
414 elasticity = br->hash_elasticity; 592 elasticity = br->hash_elasticity;
415 } 593 }
@@ -417,11 +595,9 @@ static struct net_bridge_mdb_entry *br_multicast_get_group(
417 if (mdb->size >= max) { 595 if (mdb->size >= max) {
418 max *= 2; 596 max *= 2;
419 if (unlikely(max >= br->hash_max)) { 597 if (unlikely(max >= br->hash_max)) {
420 printk(KERN_WARNING "%s: Multicast hash table maximum " 598 br_warn(br, "Multicast hash table maximum "
421 "reached, disabling snooping: %s, %d\n", 599 "reached, disabling snooping: %s, %d\n",
422 br->dev->name, port ? port->dev->name : 600 port ? port->dev->name : br->dev->name, max);
423 br->dev->name,
424 max);
425 err = -E2BIG; 601 err = -E2BIG;
426disable: 602disable:
427 br->multicast_disabled = 1; 603 br->multicast_disabled = 1;
@@ -432,22 +608,19 @@ disable:
432 if (max > mdb->max || elasticity) { 608 if (max > mdb->max || elasticity) {
433 if (mdb->old) { 609 if (mdb->old) {
434 if (net_ratelimit()) 610 if (net_ratelimit())
435 printk(KERN_INFO "%s: Multicast hash table " 611 br_info(br, "Multicast hash table "
436 "on fire: %s\n", 612 "on fire: %s\n",
437 br->dev->name, port ? port->dev->name : 613 port ? port->dev->name : br->dev->name);
438 br->dev->name);
439 err = -EEXIST; 614 err = -EEXIST;
440 goto err; 615 goto err;
441 } 616 }
442 617
443 err = br_mdb_rehash(&br->mdb, max, elasticity); 618 err = br_mdb_rehash(&br->mdb, max, elasticity);
444 if (err) { 619 if (err) {
445 printk(KERN_WARNING "%s: Cannot rehash multicast " 620 br_warn(br, "Cannot rehash multicast "
446 "hash table, disabling snooping: " 621 "hash table, disabling snooping: %s, %d, %d\n",
447 "%s, %d, %d\n", 622 port ? port->dev->name : br->dev->name,
448 br->dev->name, port ? port->dev->name : 623 mdb->size, err);
449 br->dev->name,
450 mdb->size, err);
451 goto disable; 624 goto disable;
452 } 625 }
453 626
@@ -463,7 +636,8 @@ err:
463} 636}
464 637
465static struct net_bridge_mdb_entry *br_multicast_new_group( 638static struct net_bridge_mdb_entry *br_multicast_new_group(
466 struct net_bridge *br, struct net_bridge_port *port, __be32 group) 639 struct net_bridge *br, struct net_bridge_port *port,
640 struct br_ip *group)
467{ 641{
468 struct net_bridge_mdb_htable *mdb = br->mdb; 642 struct net_bridge_mdb_htable *mdb = br->mdb;
469 struct net_bridge_mdb_entry *mp; 643 struct net_bridge_mdb_entry *mp;
@@ -496,7 +670,7 @@ rehash:
496 goto out; 670 goto out;
497 671
498 mp->br = br; 672 mp->br = br;
499 mp->addr = group; 673 mp->addr = *group;
500 setup_timer(&mp->timer, br_multicast_group_expired, 674 setup_timer(&mp->timer, br_multicast_group_expired,
501 (unsigned long)mp); 675 (unsigned long)mp);
502 setup_timer(&mp->query_timer, br_multicast_group_query_expired, 676 setup_timer(&mp->query_timer, br_multicast_group_query_expired,
@@ -510,7 +684,8 @@ out:
510} 684}
511 685
512static int br_multicast_add_group(struct net_bridge *br, 686static int br_multicast_add_group(struct net_bridge *br,
513 struct net_bridge_port *port, __be32 group) 687 struct net_bridge_port *port,
688 struct br_ip *group)
514{ 689{
515 struct net_bridge_mdb_entry *mp; 690 struct net_bridge_mdb_entry *mp;
516 struct net_bridge_port_group *p; 691 struct net_bridge_port_group *p;
@@ -518,9 +693,6 @@ static int br_multicast_add_group(struct net_bridge *br,
518 unsigned long now = jiffies; 693 unsigned long now = jiffies;
519 int err; 694 int err;
520 695
521 if (ipv4_is_local_multicast(group))
522 return 0;
523
524 spin_lock(&br->multicast_lock); 696 spin_lock(&br->multicast_lock);
525 if (!netif_running(br->dev) || 697 if (!netif_running(br->dev) ||
526 (port && port->state == BR_STATE_DISABLED)) 698 (port && port->state == BR_STATE_DISABLED))
@@ -549,7 +721,7 @@ static int br_multicast_add_group(struct net_bridge *br,
549 if (unlikely(!p)) 721 if (unlikely(!p))
550 goto err; 722 goto err;
551 723
552 p->addr = group; 724 p->addr = *group;
553 p->port = port; 725 p->port = port;
554 p->next = *pp; 726 p->next = *pp;
555 hlist_add_head(&p->mglist, &port->mglist); 727 hlist_add_head(&p->mglist, &port->mglist);
@@ -570,6 +742,38 @@ err:
570 return err; 742 return err;
571} 743}
572 744
745static int br_ip4_multicast_add_group(struct net_bridge *br,
746 struct net_bridge_port *port,
747 __be32 group)
748{
749 struct br_ip br_group;
750
751 if (ipv4_is_local_multicast(group))
752 return 0;
753
754 br_group.u.ip4 = group;
755 br_group.proto = htons(ETH_P_IP);
756
757 return br_multicast_add_group(br, port, &br_group);
758}
759
760#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
761static int br_ip6_multicast_add_group(struct net_bridge *br,
762 struct net_bridge_port *port,
763 const struct in6_addr *group)
764{
765 struct br_ip br_group;
766
767 if (ipv6_is_local_multicast(group))
768 return 0;
769
770 ipv6_addr_copy(&br_group.u.ip6, group);
771 br_group.proto = htons(ETH_P_IP);
772
773 return br_multicast_add_group(br, port, &br_group);
774}
775#endif
776
573static void br_multicast_router_expired(unsigned long data) 777static void br_multicast_router_expired(unsigned long data)
574{ 778{
575 struct net_bridge_port *port = (void *)data; 779 struct net_bridge_port *port = (void *)data;
@@ -591,29 +795,45 @@ static void br_multicast_local_router_expired(unsigned long data)
591{ 795{
592} 796}
593 797
594static void br_multicast_send_query(struct net_bridge *br, 798static void __br_multicast_send_query(struct net_bridge *br,
595 struct net_bridge_port *port, u32 sent) 799 struct net_bridge_port *port,
800 struct br_ip *ip)
596{ 801{
597 unsigned long time;
598 struct sk_buff *skb; 802 struct sk_buff *skb;
599 803
600 if (!netif_running(br->dev) || br->multicast_disabled || 804 skb = br_multicast_alloc_query(br, ip);
601 timer_pending(&br->multicast_querier_timer))
602 return;
603
604 skb = br_multicast_alloc_query(br, 0);
605 if (!skb) 805 if (!skb)
606 goto timer; 806 return;
607 807
608 if (port) { 808 if (port) {
609 __skb_push(skb, sizeof(struct ethhdr)); 809 __skb_push(skb, sizeof(struct ethhdr));
610 skb->dev = port->dev; 810 skb->dev = port->dev;
611 NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev, 811 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
612 dev_queue_xmit); 812 dev_queue_xmit);
613 } else 813 } else
614 netif_rx(skb); 814 netif_rx(skb);
815}
816
817static void br_multicast_send_query(struct net_bridge *br,
818 struct net_bridge_port *port, u32 sent)
819{
820 unsigned long time;
821 struct br_ip br_group;
822
823 if (!netif_running(br->dev) || br->multicast_disabled ||
824 timer_pending(&br->multicast_querier_timer))
825 return;
826
827 memset(&br_group.u, 0, sizeof(br_group.u));
828
829 br_group.proto = htons(ETH_P_IP);
830 __br_multicast_send_query(br, port, &br_group);
831
832#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
833 br_group.proto = htons(ETH_P_IPV6);
834 __br_multicast_send_query(br, port, &br_group);
835#endif
615 836
616timer:
617 time = jiffies; 837 time = jiffies;
618 time += sent < br->multicast_startup_query_count ? 838 time += sent < br->multicast_startup_query_count ?
619 br->multicast_startup_query_interval : 839 br->multicast_startup_query_interval :
@@ -698,9 +918,9 @@ void br_multicast_disable_port(struct net_bridge_port *port)
698 spin_unlock(&br->multicast_lock); 918 spin_unlock(&br->multicast_lock);
699} 919}
700 920
701static int br_multicast_igmp3_report(struct net_bridge *br, 921static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
702 struct net_bridge_port *port, 922 struct net_bridge_port *port,
703 struct sk_buff *skb) 923 struct sk_buff *skb)
704{ 924{
705 struct igmpv3_report *ih; 925 struct igmpv3_report *ih;
706 struct igmpv3_grec *grec; 926 struct igmpv3_grec *grec;
@@ -745,7 +965,7 @@ static int br_multicast_igmp3_report(struct net_bridge *br,
745 continue; 965 continue;
746 } 966 }
747 967
748 err = br_multicast_add_group(br, port, group); 968 err = br_ip4_multicast_add_group(br, port, group);
749 if (err) 969 if (err)
750 break; 970 break;
751 } 971 }
@@ -753,24 +973,87 @@ static int br_multicast_igmp3_report(struct net_bridge *br,
753 return err; 973 return err;
754} 974}
755 975
976#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
977static int br_ip6_multicast_mld2_report(struct net_bridge *br,
978 struct net_bridge_port *port,
979 struct sk_buff *skb)
980{
981 struct icmp6hdr *icmp6h;
982 struct mld2_grec *grec;
983 int i;
984 int len;
985 int num;
986 int err = 0;
987
988 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
989 return -EINVAL;
990
991 icmp6h = icmp6_hdr(skb);
992 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
993 len = sizeof(*icmp6h);
994
995 for (i = 0; i < num; i++) {
996 __be16 *nsrcs, _nsrcs;
997
998 nsrcs = skb_header_pointer(skb,
999 len + offsetof(struct mld2_grec,
1000 grec_mca),
1001 sizeof(_nsrcs), &_nsrcs);
1002 if (!nsrcs)
1003 return -EINVAL;
1004
1005 if (!pskb_may_pull(skb,
1006 len + sizeof(*grec) +
1007 sizeof(struct in6_addr) * (*nsrcs)))
1008 return -EINVAL;
1009
1010 grec = (struct mld2_grec *)(skb->data + len);
1011 len += sizeof(*grec) + sizeof(struct in6_addr) * (*nsrcs);
1012
1013 /* We treat these as MLDv1 reports for now. */
1014 switch (grec->grec_type) {
1015 case MLD2_MODE_IS_INCLUDE:
1016 case MLD2_MODE_IS_EXCLUDE:
1017 case MLD2_CHANGE_TO_INCLUDE:
1018 case MLD2_CHANGE_TO_EXCLUDE:
1019 case MLD2_ALLOW_NEW_SOURCES:
1020 case MLD2_BLOCK_OLD_SOURCES:
1021 break;
1022
1023 default:
1024 continue;
1025 }
1026
1027 err = br_ip6_multicast_add_group(br, port, &grec->grec_mca);
1028 if (!err)
1029 break;
1030 }
1031
1032 return err;
1033}
1034#endif
1035
1036/*
1037 * Add port to rotuer_list
1038 * list is maintained ordered by pointer value
1039 * and locked by br->multicast_lock and RCU
1040 */
756static void br_multicast_add_router(struct net_bridge *br, 1041static void br_multicast_add_router(struct net_bridge *br,
757 struct net_bridge_port *port) 1042 struct net_bridge_port *port)
758{ 1043{
759 struct hlist_node *p; 1044 struct net_bridge_port *p;
760 struct hlist_node **h; 1045 struct hlist_node *n, *slot = NULL;
761 1046
762 for (h = &br->router_list.first; 1047 hlist_for_each_entry(p, n, &br->router_list, rlist) {
763 (p = *h) && 1048 if ((unsigned long) port >= (unsigned long) p)
764 (unsigned long)container_of(p, struct net_bridge_port, rlist) > 1049 break;
765 (unsigned long)port; 1050 slot = n;
766 h = &p->next) 1051 }
767 ; 1052
768 1053 if (slot)
769 port->rlist.pprev = h; 1054 hlist_add_after_rcu(slot, &port->rlist);
770 port->rlist.next = p; 1055 else
771 rcu_assign_pointer(*h, &port->rlist); 1056 hlist_add_head_rcu(&port->rlist, &br->router_list);
772 if (p)
773 p->pprev = &port->rlist.next;
774} 1057}
775 1058
776static void br_multicast_mark_router(struct net_bridge *br, 1059static void br_multicast_mark_router(struct net_bridge *br,
@@ -800,7 +1083,7 @@ timer:
800 1083
801static void br_multicast_query_received(struct net_bridge *br, 1084static void br_multicast_query_received(struct net_bridge *br,
802 struct net_bridge_port *port, 1085 struct net_bridge_port *port,
803 __be32 saddr) 1086 int saddr)
804{ 1087{
805 if (saddr) 1088 if (saddr)
806 mod_timer(&br->multicast_querier_timer, 1089 mod_timer(&br->multicast_querier_timer,
@@ -811,9 +1094,9 @@ static void br_multicast_query_received(struct net_bridge *br,
811 br_multicast_mark_router(br, port); 1094 br_multicast_mark_router(br, port);
812} 1095}
813 1096
814static int br_multicast_query(struct net_bridge *br, 1097static int br_ip4_multicast_query(struct net_bridge *br,
815 struct net_bridge_port *port, 1098 struct net_bridge_port *port,
816 struct sk_buff *skb) 1099 struct sk_buff *skb)
817{ 1100{
818 struct iphdr *iph = ip_hdr(skb); 1101 struct iphdr *iph = ip_hdr(skb);
819 struct igmphdr *ih = igmp_hdr(skb); 1102 struct igmphdr *ih = igmp_hdr(skb);
@@ -831,7 +1114,7 @@ static int br_multicast_query(struct net_bridge *br,
831 (port && port->state == BR_STATE_DISABLED)) 1114 (port && port->state == BR_STATE_DISABLED))
832 goto out; 1115 goto out;
833 1116
834 br_multicast_query_received(br, port, iph->saddr); 1117 br_multicast_query_received(br, port, !!iph->saddr);
835 1118
836 group = ih->group; 1119 group = ih->group;
837 1120
@@ -859,7 +1142,7 @@ static int br_multicast_query(struct net_bridge *br,
859 if (!group) 1142 if (!group)
860 goto out; 1143 goto out;
861 1144
862 mp = br_mdb_ip_get(br->mdb, group); 1145 mp = br_mdb_ip4_get(br->mdb, group);
863 if (!mp) 1146 if (!mp)
864 goto out; 1147 goto out;
865 1148
@@ -883,9 +1166,78 @@ out:
883 return err; 1166 return err;
884} 1167}
885 1168
1169#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1170static int br_ip6_multicast_query(struct net_bridge *br,
1171 struct net_bridge_port *port,
1172 struct sk_buff *skb)
1173{
1174 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1175 struct mld_msg *mld = (struct mld_msg *) icmp6_hdr(skb);
1176 struct net_bridge_mdb_entry *mp;
1177 struct mld2_query *mld2q;
1178 struct net_bridge_port_group *p, **pp;
1179 unsigned long max_delay;
1180 unsigned long now = jiffies;
1181 struct in6_addr *group = NULL;
1182 int err = 0;
1183
1184 spin_lock(&br->multicast_lock);
1185 if (!netif_running(br->dev) ||
1186 (port && port->state == BR_STATE_DISABLED))
1187 goto out;
1188
1189 br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->saddr));
1190
1191 if (skb->len == sizeof(*mld)) {
1192 if (!pskb_may_pull(skb, sizeof(*mld))) {
1193 err = -EINVAL;
1194 goto out;
1195 }
1196 mld = (struct mld_msg *) icmp6_hdr(skb);
1197 max_delay = msecs_to_jiffies(htons(mld->mld_maxdelay));
1198 if (max_delay)
1199 group = &mld->mld_mca;
1200 } else if (skb->len >= sizeof(*mld2q)) {
1201 if (!pskb_may_pull(skb, sizeof(*mld2q))) {
1202 err = -EINVAL;
1203 goto out;
1204 }
1205 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1206 if (!mld2q->mld2q_nsrcs)
1207 group = &mld2q->mld2q_mca;
1208 max_delay = mld2q->mld2q_mrc ? MLDV2_MRC(mld2q->mld2q_mrc) : 1;
1209 }
1210
1211 if (!group)
1212 goto out;
1213
1214 mp = br_mdb_ip6_get(br->mdb, group);
1215 if (!mp)
1216 goto out;
1217
1218 max_delay *= br->multicast_last_member_count;
1219 if (!hlist_unhashed(&mp->mglist) &&
1220 (timer_pending(&mp->timer) ?
1221 time_after(mp->timer.expires, now + max_delay) :
1222 try_to_del_timer_sync(&mp->timer) >= 0))
1223 mod_timer(&mp->timer, now + max_delay);
1224
1225 for (pp = &mp->ports; (p = *pp); pp = &p->next) {
1226 if (timer_pending(&p->timer) ?
1227 time_after(p->timer.expires, now + max_delay) :
1228 try_to_del_timer_sync(&p->timer) >= 0)
1229 mod_timer(&mp->timer, now + max_delay);
1230 }
1231
1232out:
1233 spin_unlock(&br->multicast_lock);
1234 return err;
1235}
1236#endif
1237
886static void br_multicast_leave_group(struct net_bridge *br, 1238static void br_multicast_leave_group(struct net_bridge *br,
887 struct net_bridge_port *port, 1239 struct net_bridge_port *port,
888 __be32 group) 1240 struct br_ip *group)
889{ 1241{
890 struct net_bridge_mdb_htable *mdb; 1242 struct net_bridge_mdb_htable *mdb;
891 struct net_bridge_mdb_entry *mp; 1243 struct net_bridge_mdb_entry *mp;
@@ -893,9 +1245,6 @@ static void br_multicast_leave_group(struct net_bridge *br,
893 unsigned long now; 1245 unsigned long now;
894 unsigned long time; 1246 unsigned long time;
895 1247
896 if (ipv4_is_local_multicast(group))
897 return;
898
899 spin_lock(&br->multicast_lock); 1248 spin_lock(&br->multicast_lock);
900 if (!netif_running(br->dev) || 1249 if (!netif_running(br->dev) ||
901 (port && port->state == BR_STATE_DISABLED) || 1250 (port && port->state == BR_STATE_DISABLED) ||
@@ -946,6 +1295,38 @@ out:
946 spin_unlock(&br->multicast_lock); 1295 spin_unlock(&br->multicast_lock);
947} 1296}
948 1297
1298static void br_ip4_multicast_leave_group(struct net_bridge *br,
1299 struct net_bridge_port *port,
1300 __be32 group)
1301{
1302 struct br_ip br_group;
1303
1304 if (ipv4_is_local_multicast(group))
1305 return;
1306
1307 br_group.u.ip4 = group;
1308 br_group.proto = htons(ETH_P_IP);
1309
1310 br_multicast_leave_group(br, port, &br_group);
1311}
1312
1313#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1314static void br_ip6_multicast_leave_group(struct net_bridge *br,
1315 struct net_bridge_port *port,
1316 const struct in6_addr *group)
1317{
1318 struct br_ip br_group;
1319
1320 if (ipv6_is_local_multicast(group))
1321 return;
1322
1323 ipv6_addr_copy(&br_group.u.ip6, group);
1324 br_group.proto = htons(ETH_P_IPV6);
1325
1326 br_multicast_leave_group(br, port, &br_group);
1327}
1328#endif
1329
949static int br_multicast_ipv4_rcv(struct net_bridge *br, 1330static int br_multicast_ipv4_rcv(struct net_bridge *br,
950 struct net_bridge_port *port, 1331 struct net_bridge_port *port,
951 struct sk_buff *skb) 1332 struct sk_buff *skb)
@@ -1000,8 +1381,6 @@ static int br_multicast_ipv4_rcv(struct net_bridge *br,
1000 if (!pskb_may_pull(skb2, sizeof(*ih))) 1381 if (!pskb_may_pull(skb2, sizeof(*ih)))
1001 goto out; 1382 goto out;
1002 1383
1003 iph = ip_hdr(skb2);
1004
1005 switch (skb2->ip_summed) { 1384 switch (skb2->ip_summed) {
1006 case CHECKSUM_COMPLETE: 1385 case CHECKSUM_COMPLETE:
1007 if (!csum_fold(skb2->csum)) 1386 if (!csum_fold(skb2->csum))
@@ -1022,16 +1401,16 @@ static int br_multicast_ipv4_rcv(struct net_bridge *br,
1022 case IGMP_HOST_MEMBERSHIP_REPORT: 1401 case IGMP_HOST_MEMBERSHIP_REPORT:
1023 case IGMPV2_HOST_MEMBERSHIP_REPORT: 1402 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1024 BR_INPUT_SKB_CB(skb2)->mrouters_only = 1; 1403 BR_INPUT_SKB_CB(skb2)->mrouters_only = 1;
1025 err = br_multicast_add_group(br, port, ih->group); 1404 err = br_ip4_multicast_add_group(br, port, ih->group);
1026 break; 1405 break;
1027 case IGMPV3_HOST_MEMBERSHIP_REPORT: 1406 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1028 err = br_multicast_igmp3_report(br, port, skb2); 1407 err = br_ip4_multicast_igmp3_report(br, port, skb2);
1029 break; 1408 break;
1030 case IGMP_HOST_MEMBERSHIP_QUERY: 1409 case IGMP_HOST_MEMBERSHIP_QUERY:
1031 err = br_multicast_query(br, port, skb2); 1410 err = br_ip4_multicast_query(br, port, skb2);
1032 break; 1411 break;
1033 case IGMP_HOST_LEAVE_MESSAGE: 1412 case IGMP_HOST_LEAVE_MESSAGE:
1034 br_multicast_leave_group(br, port, ih->group); 1413 br_ip4_multicast_leave_group(br, port, ih->group);
1035 break; 1414 break;
1036 } 1415 }
1037 1416
@@ -1043,6 +1422,123 @@ err_out:
1043 return err; 1422 return err;
1044} 1423}
1045 1424
1425#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1426static int br_multicast_ipv6_rcv(struct net_bridge *br,
1427 struct net_bridge_port *port,
1428 struct sk_buff *skb)
1429{
1430 struct sk_buff *skb2 = skb;
1431 struct ipv6hdr *ip6h;
1432 struct icmp6hdr *icmp6h;
1433 u8 nexthdr;
1434 unsigned len;
1435 unsigned offset;
1436 int err;
1437
1438 if (!pskb_may_pull(skb, sizeof(*ip6h)))
1439 return -EINVAL;
1440
1441 ip6h = ipv6_hdr(skb);
1442
1443 /*
1444 * We're interested in MLD messages only.
1445 * - Version is 6
1446 * - MLD has always Router Alert hop-by-hop option
1447 * - But we do not support jumbrograms.
1448 */
1449 if (ip6h->version != 6 ||
1450 ip6h->nexthdr != IPPROTO_HOPOPTS ||
1451 ip6h->payload_len == 0)
1452 return 0;
1453
1454 len = ntohs(ip6h->payload_len);
1455 if (skb->len < len)
1456 return -EINVAL;
1457
1458 nexthdr = ip6h->nexthdr;
1459 offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr);
1460
1461 if (offset < 0 || nexthdr != IPPROTO_ICMPV6)
1462 return 0;
1463
1464 /* Okay, we found ICMPv6 header */
1465 skb2 = skb_clone(skb, GFP_ATOMIC);
1466 if (!skb2)
1467 return -ENOMEM;
1468
1469 len -= offset - skb_network_offset(skb2);
1470
1471 __skb_pull(skb2, offset);
1472 skb_reset_transport_header(skb2);
1473
1474 err = -EINVAL;
1475 if (!pskb_may_pull(skb2, sizeof(*icmp6h)))
1476 goto out;
1477
1478 icmp6h = icmp6_hdr(skb2);
1479
1480 switch (icmp6h->icmp6_type) {
1481 case ICMPV6_MGM_QUERY:
1482 case ICMPV6_MGM_REPORT:
1483 case ICMPV6_MGM_REDUCTION:
1484 case ICMPV6_MLD2_REPORT:
1485 break;
1486 default:
1487 err = 0;
1488 goto out;
1489 }
1490
1491 /* Okay, we found MLD message. Check further. */
1492 if (skb2->len > len) {
1493 err = pskb_trim_rcsum(skb2, len);
1494 if (err)
1495 goto out;
1496 }
1497
1498 switch (skb2->ip_summed) {
1499 case CHECKSUM_COMPLETE:
1500 if (!csum_fold(skb2->csum))
1501 break;
1502 /*FALLTHROUGH*/
1503 case CHECKSUM_NONE:
1504 skb2->csum = 0;
1505 if (skb_checksum_complete(skb2))
1506 goto out;
1507 }
1508
1509 err = 0;
1510
1511 BR_INPUT_SKB_CB(skb)->igmp = 1;
1512
1513 switch (icmp6h->icmp6_type) {
1514 case ICMPV6_MGM_REPORT:
1515 {
1516 struct mld_msg *mld = (struct mld_msg *)icmp6h;
1517 BR_INPUT_SKB_CB(skb2)->mrouters_only = 1;
1518 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca);
1519 break;
1520 }
1521 case ICMPV6_MLD2_REPORT:
1522 err = br_ip6_multicast_mld2_report(br, port, skb2);
1523 break;
1524 case ICMPV6_MGM_QUERY:
1525 err = br_ip6_multicast_query(br, port, skb2);
1526 break;
1527 case ICMPV6_MGM_REDUCTION:
1528 {
1529 struct mld_msg *mld = (struct mld_msg *)icmp6h;
1530 br_ip6_multicast_leave_group(br, port, &mld->mld_mca);
1531 }
1532 }
1533
1534out:
1535 __skb_push(skb2, offset);
1536 if (skb2 != skb)
1537 kfree_skb(skb2);
1538 return err;
1539}
1540#endif
1541
1046int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port, 1542int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1047 struct sk_buff *skb) 1543 struct sk_buff *skb)
1048{ 1544{
@@ -1055,6 +1551,10 @@ int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1055 switch (skb->protocol) { 1551 switch (skb->protocol) {
1056 case htons(ETH_P_IP): 1552 case htons(ETH_P_IP):
1057 return br_multicast_ipv4_rcv(br, port, skb); 1553 return br_multicast_ipv4_rcv(br, port, skb);
1554#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1555 case htons(ETH_P_IPV6):
1556 return br_multicast_ipv6_rcv(br, port, skb);
1557#endif
1058 } 1558 }
1059 1559
1060 return 0; 1560 return 0;