linux/net/bridge/br_multicast.c
<<
>>
Prefs
   1/*
   2 * Bridge multicast support.
   3 *
   4 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
   5 *
   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)
   9 * any later version.
  10 *
  11 */
  12
  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>
  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
  33
  34#include "br_private.h"
  35
  36#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  37static inline int ipv6_is_local_multicast(const struct in6_addr *addr)
  38{
  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;
  86}
  87
  88static struct net_bridge_mdb_entry *__br_mdb_ip_get(
  89        struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash)
  90{
  91        struct net_bridge_mdb_entry *mp;
  92        struct hlist_node *p;
  93
  94        hlist_for_each_entry_rcu(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) {
  95                if (br_ip_equal(&mp->addr, dst))
  96                        return mp;
  97        }
  98
  99        return NULL;
 100}
 101
 102static struct net_bridge_mdb_entry *br_mdb_ip_get(
 103        struct net_bridge_mdb_htable *mdb, struct br_ip *dst)
 104{
 105        if (!mdb)
 106                return NULL;
 107
 108        return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
 109}
 110
 111static struct net_bridge_mdb_entry *br_mdb_ip4_get(
 112        struct net_bridge_mdb_htable *mdb, __be32 dst)
 113{
 114        struct br_ip br_dst;
 115
 116        br_dst.u.ip4 = dst;
 117        br_dst.proto = htons(ETH_P_IP);
 118
 119        return br_mdb_ip_get(mdb, &br_dst);
 120}
 121
 122#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
 123static struct net_bridge_mdb_entry *br_mdb_ip6_get(
 124        struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst)
 125{
 126        struct br_ip br_dst;
 127
 128        ipv6_addr_copy(&br_dst.u.ip6, dst);
 129        br_dst.proto = htons(ETH_P_IPV6);
 130
 131        return br_mdb_ip_get(mdb, &br_dst);
 132}
 133#endif
 134
 135struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
 136                                        struct sk_buff *skb)
 137{
 138        struct net_bridge_mdb_htable *mdb = br->mdb;
 139        struct br_ip ip;
 140
 141        if (br->multicast_disabled)
 142                return NULL;
 143
 144        if (BR_INPUT_SKB_CB(skb)->igmp)
 145                return NULL;
 146
 147        ip.proto = skb->protocol;
 148
 149        switch (skb->protocol) {
 150        case htons(ETH_P_IP):
 151                ip.u.ip4 = ip_hdr(skb)->daddr;
 152                break;
 153#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
 154        case htons(ETH_P_IPV6):
 155                ipv6_addr_copy(&ip.u.ip6, &ipv6_hdr(skb)->daddr);
 156                break;
 157#endif
 158        default:
 159                return NULL;
 160        }
 161
 162        return br_mdb_ip_get(mdb, &ip);
 163}
 164
 165static void br_mdb_free(struct rcu_head *head)
 166{
 167        struct net_bridge_mdb_htable *mdb =
 168                container_of(head, struct net_bridge_mdb_htable, rcu);
 169        struct net_bridge_mdb_htable *old = mdb->old;
 170
 171        mdb->old = NULL;
 172        kfree(old->mhash);
 173        kfree(old);
 174}
 175
 176static int br_mdb_copy(struct net_bridge_mdb_htable *new,
 177                       struct net_bridge_mdb_htable *old,
 178                       int elasticity)
 179{
 180        struct net_bridge_mdb_entry *mp;
 181        struct hlist_node *p;
 182        int maxlen;
 183        int len;
 184        int i;
 185
 186        for (i = 0; i < old->max; i++)
 187                hlist_for_each_entry(mp, p, &old->mhash[i], hlist[old->ver])
 188                        hlist_add_head(&mp->hlist[new->ver],
 189                                       &new->mhash[br_ip_hash(new, &mp->addr)]);
 190
 191        if (!elasticity)
 192                return 0;
 193
 194        maxlen = 0;
 195        for (i = 0; i < new->max; i++) {
 196                len = 0;
 197                hlist_for_each_entry(mp, p, &new->mhash[i], hlist[new->ver])
 198                        len++;
 199                if (len > maxlen)
 200                        maxlen = len;
 201        }
 202
 203        return maxlen > elasticity ? -EINVAL : 0;
 204}
 205
 206static void br_multicast_free_pg(struct rcu_head *head)
 207{
 208        struct net_bridge_port_group *p =
 209                container_of(head, struct net_bridge_port_group, rcu);
 210
 211        kfree(p);
 212}
 213
 214static void br_multicast_free_group(struct rcu_head *head)
 215{
 216        struct net_bridge_mdb_entry *mp =
 217                container_of(head, struct net_bridge_mdb_entry, rcu);
 218
 219        kfree(mp);
 220}
 221
 222static void br_multicast_group_expired(unsigned long data)
 223{
 224        struct net_bridge_mdb_entry *mp = (void *)data;
 225        struct net_bridge *br = mp->br;
 226        struct net_bridge_mdb_htable *mdb;
 227
 228        spin_lock(&br->multicast_lock);
 229        if (!netif_running(br->dev) || timer_pending(&mp->timer))
 230                goto out;
 231
 232        if (!hlist_unhashed(&mp->mglist))
 233                hlist_del_init(&mp->mglist);
 234
 235        if (mp->ports)
 236                goto out;
 237
 238        mdb = br->mdb;
 239        hlist_del_rcu(&mp->hlist[mdb->ver]);
 240        mdb->size--;
 241
 242        del_timer(&mp->query_timer);
 243        call_rcu_bh(&mp->rcu, br_multicast_free_group);
 244
 245out:
 246        spin_unlock(&br->multicast_lock);
 247}
 248
 249static void br_multicast_del_pg(struct net_bridge *br,
 250                                struct net_bridge_port_group *pg)
 251{
 252        struct net_bridge_mdb_htable *mdb = br->mdb;
 253        struct net_bridge_mdb_entry *mp;
 254        struct net_bridge_port_group *p;
 255        struct net_bridge_port_group **pp;
 256
 257        mp = br_mdb_ip_get(mdb, &pg->addr);
 258        if (WARN_ON(!mp))
 259                return;
 260
 261        for (pp = &mp->ports; (p = *pp); pp = &p->next) {
 262                if (p != pg)
 263                        continue;
 264
 265                rcu_assign_pointer(*pp, p->next);
 266                hlist_del_init(&p->mglist);
 267                del_timer(&p->timer);
 268                del_timer(&p->query_timer);
 269                call_rcu_bh(&p->rcu, br_multicast_free_pg);
 270
 271                if (!mp->ports && hlist_unhashed(&mp->mglist) &&
 272                    netif_running(br->dev))
 273                        mod_timer(&mp->timer, jiffies);
 274
 275                return;
 276        }
 277
 278        WARN_ON(1);
 279}
 280
 281static void br_multicast_port_group_expired(unsigned long data)
 282{
 283        struct net_bridge_port_group *pg = (void *)data;
 284        struct net_bridge *br = pg->port->br;
 285
 286        spin_lock(&br->multicast_lock);
 287        if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
 288            hlist_unhashed(&pg->mglist))
 289                goto out;
 290
 291        br_multicast_del_pg(br, pg);
 292
 293out:
 294        spin_unlock(&br->multicast_lock);
 295}
 296
 297static int br_mdb_rehash(struct net_bridge_mdb_htable **mdbp, int max,
 298                         int elasticity)
 299{
 300        struct net_bridge_mdb_htable *old = *mdbp;
 301        struct net_bridge_mdb_htable *mdb;
 302        int err;
 303
 304        mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
 305        if (!mdb)
 306                return -ENOMEM;
 307
 308        mdb->max = max;
 309        mdb->old = old;
 310
 311        mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
 312        if (!mdb->mhash) {
 313                kfree(mdb);
 314                return -ENOMEM;
 315        }
 316
 317        mdb->size = old ? old->size : 0;
 318        mdb->ver = old ? old->ver ^ 1 : 0;
 319
 320        if (!old || elasticity)
 321                get_random_bytes(&mdb->secret, sizeof(mdb->secret));
 322        else
 323                mdb->secret = old->secret;
 324
 325        if (!old)
 326                goto out;
 327
 328        err = br_mdb_copy(mdb, old, elasticity);
 329        if (err) {
 330                kfree(mdb->mhash);
 331                kfree(mdb);
 332                return err;
 333        }
 334
 335        call_rcu_bh(&mdb->rcu, br_mdb_free);
 336
 337out:
 338        rcu_assign_pointer(*mdbp, mdb);
 339
 340        return 0;
 341}
 342
 343static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
 344                                                    __be32 group)
 345{
 346        struct sk_buff *skb;
 347        struct igmphdr *ih;
 348        struct ethhdr *eth;
 349        struct iphdr *iph;
 350
 351        skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
 352                                                 sizeof(*ih) + 4);
 353        if (!skb)
 354                goto out;
 355
 356        skb->protocol = htons(ETH_P_IP);
 357
 358        skb_reset_mac_header(skb);
 359        eth = eth_hdr(skb);
 360
 361        memcpy(eth->h_source, br->dev->dev_addr, 6);
 362        eth->h_dest[0] = 1;
 363        eth->h_dest[1] = 0;
 364        eth->h_dest[2] = 0x5e;
 365        eth->h_dest[3] = 0;
 366        eth->h_dest[4] = 0;
 367        eth->h_dest[5] = 1;
 368        eth->h_proto = htons(ETH_P_IP);
 369        skb_put(skb, sizeof(*eth));
 370
 371        skb_set_network_header(skb, skb->len);
 372        iph = ip_hdr(skb);
 373
 374        iph->version = 4;
 375        iph->ihl = 6;
 376        iph->tos = 0xc0;
 377        iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4);
 378        iph->id = 0;
 379        iph->frag_off = htons(IP_DF);
 380        iph->ttl = 1;
 381        iph->protocol = IPPROTO_IGMP;
 382        iph->saddr = 0;
 383        iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
 384        ((u8 *)&iph[1])[0] = IPOPT_RA;
 385        ((u8 *)&iph[1])[1] = 4;
 386        ((u8 *)&iph[1])[2] = 0;
 387        ((u8 *)&iph[1])[3] = 0;
 388        ip_send_check(iph);
 389        skb_put(skb, 24);
 390
 391        skb_set_transport_header(skb, skb->len);
 392        ih = igmp_hdr(skb);
 393        ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
 394        ih->code = (group ? br->multicast_last_member_interval :
 395                            br->multicast_query_response_interval) /
 396                   (HZ / IGMP_TIMER_SCALE);
 397        ih->group = group;
 398        ih->csum = 0;
 399        ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
 400        skb_put(skb, sizeof(*ih));
 401
 402        __skb_pull(skb, sizeof(*eth));
 403
 404out:
 405        return skb;
 406}
 407
 408#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
 409static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
 410                                                    struct in6_addr *group)
 411{
 412        struct sk_buff *skb;
 413        struct ipv6hdr *ip6h;
 414        struct mld_msg *mldq;
 415        struct ethhdr *eth;
 416        u8 *hopopt;
 417        unsigned long interval;
 418
 419        skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
 420                                                 8 + sizeof(*mldq));
 421        if (!skb)
 422                goto out;
 423
 424        skb->protocol = htons(ETH_P_IPV6);
 425
 426        /* Ethernet header */
 427        skb_reset_mac_header(skb);
 428        eth = eth_hdr(skb);
 429
 430        memcpy(eth->h_source, br->dev->dev_addr, 6);
 431        ipv6_eth_mc_map(group, eth->h_dest);
 432        eth->h_proto = htons(ETH_P_IPV6);
 433        skb_put(skb, sizeof(*eth));
 434
 435        /* IPv6 header + HbH option */
 436        skb_set_network_header(skb, skb->len);
 437        ip6h = ipv6_hdr(skb);
 438
 439        *(__force __be32 *)ip6h = htonl(0x60000000);
 440        ip6h->payload_len = htons(8 + sizeof(*mldq));
 441        ip6h->nexthdr = IPPROTO_HOPOPTS;
 442        ip6h->hop_limit = 1;
 443        ipv6_addr_set(&ip6h->saddr, 0, 0, 0, 0);
 444        ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
 445
 446        hopopt = (u8 *)(ip6h + 1);
 447        hopopt[0] = IPPROTO_ICMPV6;             /* next hdr */
 448        hopopt[1] = 0;                          /* length of HbH */
 449        hopopt[2] = IPV6_TLV_ROUTERALERT;       /* Router Alert */
 450        hopopt[3] = 2;                          /* Length of RA Option */
 451        hopopt[4] = 0;                          /* Type = 0x0000 (MLD) */
 452        hopopt[5] = 0;
 453        hopopt[6] = IPV6_TLV_PAD0;              /* Pad0 */
 454        hopopt[7] = IPV6_TLV_PAD0;              /* Pad0 */
 455
 456        skb_put(skb, sizeof(*ip6h) + 8);
 457
 458        /* ICMPv6 */
 459        skb_set_transport_header(skb, skb->len);
 460        mldq = (struct mld_msg *) icmp6_hdr(skb);
 461
 462        interval = ipv6_addr_any(group) ? br->multicast_last_member_interval :
 463                                          br->multicast_query_response_interval;
 464
 465        mldq->mld_type = ICMPV6_MGM_QUERY;
 466        mldq->mld_code = 0;
 467        mldq->mld_cksum = 0;
 468        mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
 469        mldq->mld_reserved = 0;
 470        ipv6_addr_copy(&mldq->mld_mca, group);
 471
 472        /* checksum */
 473        mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
 474                                          sizeof(*mldq), IPPROTO_ICMPV6,
 475                                          csum_partial(mldq,
 476                                                       sizeof(*mldq), 0));
 477        skb_put(skb, sizeof(*mldq));
 478
 479        __skb_pull(skb, sizeof(*eth));
 480
 481out:
 482        return skb;
 483}
 484#endif
 485
 486static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
 487                                                struct br_ip *addr)
 488{
 489        switch (addr->proto) {
 490        case htons(ETH_P_IP):
 491                return br_ip4_multicast_alloc_query(br, addr->u.ip4);
 492#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
 493        case htons(ETH_P_IPV6):
 494                return br_ip6_multicast_alloc_query(br, &addr->u.ip6);
 495#endif
 496        }
 497        return NULL;
 498}
 499
 500static void br_multicast_send_group_query(struct net_bridge_mdb_entry *mp)
 501{
 502        struct net_bridge *br = mp->br;
 503        struct sk_buff *skb;
 504
 505        skb = br_multicast_alloc_query(br, &mp->addr);
 506        if (!skb)
 507                goto timer;
 508
 509        netif_rx(skb);
 510
 511timer:
 512        if (++mp->queries_sent < br->multicast_last_member_count)
 513                mod_timer(&mp->query_timer,
 514                          jiffies + br->multicast_last_member_interval);
 515}
 516
 517static void br_multicast_group_query_expired(unsigned long data)
 518{
 519        struct net_bridge_mdb_entry *mp = (void *)data;
 520        struct net_bridge *br = mp->br;
 521
 522        spin_lock(&br->multicast_lock);
 523        if (!netif_running(br->dev) || hlist_unhashed(&mp->mglist) ||
 524            mp->queries_sent >= br->multicast_last_member_count)
 525                goto out;
 526
 527        br_multicast_send_group_query(mp);
 528
 529out:
 530        spin_unlock(&br->multicast_lock);
 531}
 532
 533static void br_multicast_send_port_group_query(struct net_bridge_port_group *pg)
 534{
 535        struct net_bridge_port *port = pg->port;
 536        struct net_bridge *br = port->br;
 537        struct sk_buff *skb;
 538
 539        skb = br_multicast_alloc_query(br, &pg->addr);
 540        if (!skb)
 541                goto timer;
 542
 543        br_deliver(port, skb);
 544
 545timer:
 546        if (++pg->queries_sent < br->multicast_last_member_count)
 547                mod_timer(&pg->query_timer,
 548                          jiffies + br->multicast_last_member_interval);
 549}
 550
 551static void br_multicast_port_group_query_expired(unsigned long data)
 552{
 553        struct net_bridge_port_group *pg = (void *)data;
 554        struct net_bridge_port *port = pg->port;
 555        struct net_bridge *br = port->br;
 556
 557        spin_lock(&br->multicast_lock);
 558        if (!netif_running(br->dev) || hlist_unhashed(&pg->mglist) ||
 559            pg->queries_sent >= br->multicast_last_member_count)
 560                goto out;
 561
 562        br_multicast_send_port_group_query(pg);
 563
 564out:
 565        spin_unlock(&br->multicast_lock);
 566}
 567
 568static struct net_bridge_mdb_entry *br_multicast_get_group(
 569        struct net_bridge *br, struct net_bridge_port *port,
 570        struct br_ip *group, int hash)
 571{
 572        struct net_bridge_mdb_htable *mdb = br->mdb;
 573        struct net_bridge_mdb_entry *mp;
 574        struct hlist_node *p;
 575        unsigned count = 0;
 576        unsigned max;
 577        int elasticity;
 578        int err;
 579
 580        hlist_for_each_entry(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) {
 581                count++;
 582                if (unlikely(br_ip_equal(group, &mp->addr)))
 583                        return mp;
 584        }
 585
 586        elasticity = 0;
 587        max = mdb->max;
 588
 589        if (unlikely(count > br->hash_elasticity && count)) {
 590                if (net_ratelimit())
 591                        br_info(br, "Multicast hash table "
 592                                "chain limit reached: %s\n",
 593                                port ? port->dev->name : br->dev->name);
 594
 595                elasticity = br->hash_elasticity;
 596        }
 597
 598        if (mdb->size >= max) {
 599                max *= 2;
 600                if (unlikely(max >= br->hash_max)) {
 601                        br_warn(br, "Multicast hash table maximum "
 602                                "reached, disabling snooping: %s, %d\n",
 603                                port ? port->dev->name : br->dev->name, max);
 604                        err = -E2BIG;
 605disable:
 606                        br->multicast_disabled = 1;
 607                        goto err;
 608                }
 609        }
 610
 611        if (max > mdb->max || elasticity) {
 612                if (mdb->old) {
 613                        if (net_ratelimit())
 614                                br_info(br, "Multicast hash table "
 615                                        "on fire: %s\n",
 616                                        port ? port->dev->name : br->dev->name);
 617                        err = -EEXIST;
 618                        goto err;
 619                }
 620
 621                err = br_mdb_rehash(&br->mdb, max, elasticity);
 622                if (err) {
 623                        br_warn(br, "Cannot rehash multicast "
 624                                "hash table, disabling snooping: %s, %d, %d\n",
 625                                port ? port->dev->name : br->dev->name,
 626                                mdb->size, err);
 627                        goto disable;
 628                }
 629
 630                err = -EAGAIN;
 631                goto err;
 632        }
 633
 634        return NULL;
 635
 636err:
 637        mp = ERR_PTR(err);
 638        return mp;
 639}
 640
 641static struct net_bridge_mdb_entry *br_multicast_new_group(
 642        struct net_bridge *br, struct net_bridge_port *port,
 643        struct br_ip *group)
 644{
 645        struct net_bridge_mdb_htable *mdb = br->mdb;
 646        struct net_bridge_mdb_entry *mp;
 647        int hash;
 648
 649        if (!mdb) {
 650                if (br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0))
 651                        return NULL;
 652                goto rehash;
 653        }
 654
 655        hash = br_ip_hash(mdb, group);
 656        mp = br_multicast_get_group(br, port, group, hash);
 657        switch (PTR_ERR(mp)) {
 658        case 0:
 659                break;
 660
 661        case -EAGAIN:
 662rehash:
 663                mdb = br->mdb;
 664                hash = br_ip_hash(mdb, group);
 665                break;
 666
 667        default:
 668                goto out;
 669        }
 670
 671        mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
 672        if (unlikely(!mp))
 673                goto out;
 674
 675        mp->br = br;
 676        mp->addr = *group;
 677        setup_timer(&mp->timer, br_multicast_group_expired,
 678                    (unsigned long)mp);
 679        setup_timer(&mp->query_timer, br_multicast_group_query_expired,
 680                    (unsigned long)mp);
 681
 682        hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
 683        mdb->size++;
 684
 685out:
 686        return mp;
 687}
 688
 689static int br_multicast_add_group(struct net_bridge *br,
 690                                  struct net_bridge_port *port,
 691                                  struct br_ip *group)
 692{
 693        struct net_bridge_mdb_entry *mp;
 694        struct net_bridge_port_group *p;
 695        struct net_bridge_port_group **pp;
 696        unsigned long now = jiffies;
 697        int err;
 698
 699        spin_lock(&br->multicast_lock);
 700        if (!netif_running(br->dev) ||
 701            (port && port->state == BR_STATE_DISABLED))
 702                goto out;
 703
 704        mp = br_multicast_new_group(br, port, group);
 705        err = PTR_ERR(mp);
 706        if (unlikely(IS_ERR(mp) || !mp))
 707                goto err;
 708
 709        if (!port) {
 710                hlist_add_head(&mp->mglist, &br->mglist);
 711                mod_timer(&mp->timer, now + br->multicast_membership_interval);
 712                goto out;
 713        }
 714
 715        for (pp = &mp->ports; (p = *pp); pp = &p->next) {
 716                if (p->port == port)
 717                        goto found;
 718                if ((unsigned long)p->port < (unsigned long)port)
 719                        break;
 720        }
 721
 722        p = kzalloc(sizeof(*p), GFP_ATOMIC);
 723        err = -ENOMEM;
 724        if (unlikely(!p))
 725                goto err;
 726
 727        p->addr = *group;
 728        p->port = port;
 729        p->next = *pp;
 730        hlist_add_head(&p->mglist, &port->mglist);
 731        setup_timer(&p->timer, br_multicast_port_group_expired,
 732                    (unsigned long)p);
 733        setup_timer(&p->query_timer, br_multicast_port_group_query_expired,
 734                    (unsigned long)p);
 735
 736        rcu_assign_pointer(*pp, p);
 737
 738found:
 739        mod_timer(&p->timer, now + br->multicast_membership_interval);
 740out:
 741        err = 0;
 742
 743err:
 744        spin_unlock(&br->multicast_lock);
 745        return err;
 746}
 747
 748static int br_ip4_multicast_add_group(struct net_bridge *br,
 749                                      struct net_bridge_port *port,
 750                                      __be32 group)
 751{
 752        struct br_ip br_group;
 753
 754        if (ipv4_is_local_multicast(group))
 755                return 0;
 756
 757        br_group.u.ip4 = group;
 758        br_group.proto = htons(ETH_P_IP);
 759
 760        return br_multicast_add_group(br, port, &br_group);
 761}
 762
 763#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
 764static int br_ip6_multicast_add_group(struct net_bridge *br,
 765                                      struct net_bridge_port *port,
 766                                      const struct in6_addr *group)
 767{
 768        struct br_ip br_group;
 769
 770        if (ipv6_is_local_multicast(group))
 771                return 0;
 772
 773        ipv6_addr_copy(&br_group.u.ip6, group);
 774        br_group.proto = htons(ETH_P_IP);
 775
 776        return br_multicast_add_group(br, port, &br_group);
 777}
 778#endif
 779
 780static void br_multicast_router_expired(unsigned long data)
 781{
 782        struct net_bridge_port *port = (void *)data;
 783        struct net_bridge *br = port->br;
 784
 785        spin_lock(&br->multicast_lock);
 786        if (port->multicast_router != 1 ||
 787            timer_pending(&port->multicast_router_timer) ||
 788            hlist_unhashed(&port->rlist))
 789                goto out;
 790
 791        hlist_del_init_rcu(&port->rlist);
 792
 793out:
 794        spin_unlock(&br->multicast_lock);
 795}
 796
 797static void br_multicast_local_router_expired(unsigned long data)
 798{
 799}
 800
 801static void __br_multicast_send_query(struct net_bridge *br,
 802                                      struct net_bridge_port *port,
 803                                      struct br_ip *ip)
 804{
 805        struct sk_buff *skb;
 806
 807        skb = br_multicast_alloc_query(br, ip);
 808        if (!skb)
 809                return;
 810
 811        if (port) {
 812                __skb_push(skb, sizeof(struct ethhdr));
 813                skb->dev = port->dev;
 814                NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
 815                        dev_queue_xmit);
 816        } else
 817                netif_rx(skb);
 818}
 819
 820static void br_multicast_send_query(struct net_bridge *br,
 821                                    struct net_bridge_port *port, u32 sent)
 822{
 823        unsigned long time;
 824        struct br_ip br_group;
 825
 826        if (!netif_running(br->dev) || br->multicast_disabled ||
 827            timer_pending(&br->multicast_querier_timer))
 828                return;
 829
 830        memset(&br_group.u, 0, sizeof(br_group.u));
 831
 832        br_group.proto = htons(ETH_P_IP);
 833        __br_multicast_send_query(br, port, &br_group);
 834
 835#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
 836        br_group.proto = htons(ETH_P_IPV6);
 837        __br_multicast_send_query(br, port, &br_group);
 838#endif
 839
 840        time = jiffies;
 841        time += sent < br->multicast_startup_query_count ?
 842                br->multicast_startup_query_interval :
 843                br->multicast_query_interval;
 844        mod_timer(port ? &port->multicast_query_timer :
 845                         &br->multicast_query_timer, time);
 846}
 847
 848static void br_multicast_port_query_expired(unsigned long data)
 849{
 850        struct net_bridge_port *port = (void *)data;
 851        struct net_bridge *br = port->br;
 852
 853        spin_lock(&br->multicast_lock);
 854        if (port->state == BR_STATE_DISABLED ||
 855            port->state == BR_STATE_BLOCKING)
 856                goto out;
 857
 858        if (port->multicast_startup_queries_sent <
 859            br->multicast_startup_query_count)
 860                port->multicast_startup_queries_sent++;
 861
 862        br_multicast_send_query(port->br, port,
 863                                port->multicast_startup_queries_sent);
 864
 865out:
 866        spin_unlock(&br->multicast_lock);
 867}
 868
 869void br_multicast_add_port(struct net_bridge_port *port)
 870{
 871        port->multicast_router = 1;
 872
 873        setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
 874                    (unsigned long)port);
 875        setup_timer(&port->multicast_query_timer,
 876                    br_multicast_port_query_expired, (unsigned long)port);
 877}
 878
 879void br_multicast_del_port(struct net_bridge_port *port)
 880{
 881        del_timer_sync(&port->multicast_router_timer);
 882}
 883
 884static void __br_multicast_enable_port(struct net_bridge_port *port)
 885{
 886        port->multicast_startup_queries_sent = 0;
 887
 888        if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 ||
 889            del_timer(&port->multicast_query_timer))
 890                mod_timer(&port->multicast_query_timer, jiffies);
 891}
 892
 893void br_multicast_enable_port(struct net_bridge_port *port)
 894{
 895        struct net_bridge *br = port->br;
 896
 897        spin_lock(&br->multicast_lock);
 898        if (br->multicast_disabled || !netif_running(br->dev))
 899                goto out;
 900
 901        __br_multicast_enable_port(port);
 902
 903out:
 904        spin_unlock(&br->multicast_lock);
 905}
 906
 907void br_multicast_disable_port(struct net_bridge_port *port)
 908{
 909        struct net_bridge *br = port->br;
 910        struct net_bridge_port_group *pg;
 911        struct hlist_node *p, *n;
 912
 913        spin_lock(&br->multicast_lock);
 914        hlist_for_each_entry_safe(pg, p, n, &port->mglist, mglist)
 915                br_multicast_del_pg(br, pg);
 916
 917        if (!hlist_unhashed(&port->rlist))
 918                hlist_del_init_rcu(&port->rlist);
 919        del_timer(&port->multicast_router_timer);
 920        del_timer(&port->multicast_query_timer);
 921        spin_unlock(&br->multicast_lock);
 922}
 923
 924static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
 925                                         struct net_bridge_port *port,
 926                                         struct sk_buff *skb)
 927{
 928        struct igmpv3_report *ih;
 929        struct igmpv3_grec *grec;
 930        int i;
 931        int len;
 932        int num;
 933        int type;
 934        int err = 0;
 935        __be32 group;
 936
 937        if (!pskb_may_pull(skb, sizeof(*ih)))
 938                return -EINVAL;
 939
 940        ih = igmpv3_report_hdr(skb);
 941        num = ntohs(ih->ngrec);
 942        len = sizeof(*ih);
 943
 944        for (i = 0; i < num; i++) {
 945                len += sizeof(*grec);
 946                if (!pskb_may_pull(skb, len))
 947                        return -EINVAL;
 948
 949                grec = (void *)(skb->data + len - sizeof(*grec));
 950                group = grec->grec_mca;
 951                type = grec->grec_type;
 952
 953                len += ntohs(grec->grec_nsrcs) * 4;
 954                if (!pskb_may_pull(skb, len))
 955                        return -EINVAL;
 956
 957                /* We treat this as an IGMPv2 report for now. */
 958                switch (type) {
 959                case IGMPV3_MODE_IS_INCLUDE:
 960                case IGMPV3_MODE_IS_EXCLUDE:
 961                case IGMPV3_CHANGE_TO_INCLUDE:
 962                case IGMPV3_CHANGE_TO_EXCLUDE:
 963                case IGMPV3_ALLOW_NEW_SOURCES:
 964                case IGMPV3_BLOCK_OLD_SOURCES:
 965                        break;
 966
 967                default:
 968                        continue;
 969                }
 970
 971                err = br_ip4_multicast_add_group(br, port, group);
 972                if (err)
 973                        break;
 974        }
 975
 976        return err;
 977}
 978
 979#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
 980static int br_ip6_multicast_mld2_report(struct net_bridge *br,
 981                                        struct net_bridge_port *port,
 982                                        struct sk_buff *skb)
 983{
 984        struct icmp6hdr *icmp6h;
 985        struct mld2_grec *grec;
 986        int i;
 987        int len;
 988        int num;
 989        int err = 0;
 990
 991        if (!pskb_may_pull(skb, sizeof(*icmp6h)))
 992                return -EINVAL;
 993
 994        icmp6h = icmp6_hdr(skb);
 995        num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
 996        len = sizeof(*icmp6h);
 997
 998        for (i = 0; i < num; i++) {
 999                __be16 *nsrcs, _nsrcs;
1000
1001                nsrcs = skb_header_pointer(skb,
1002                                           len + offsetof(struct mld2_grec,
1003                                                          grec_mca),
1004                                           sizeof(_nsrcs), &_nsrcs);
1005                if (!nsrcs)
1006                        return -EINVAL;
1007
1008                if (!pskb_may_pull(skb,
1009                                   len + sizeof(*grec) +
1010                                   sizeof(struct in6_addr) * (*nsrcs)))
1011                        return -EINVAL;
1012
1013                grec = (struct mld2_grec *)(skb->data + len);
1014                len += sizeof(*grec) + sizeof(struct in6_addr) * (*nsrcs);
1015
1016                /* We treat these as MLDv1 reports for now. */
1017                switch (grec->grec_type) {
1018                case MLD2_MODE_IS_INCLUDE:
1019                case MLD2_MODE_IS_EXCLUDE:
1020                case MLD2_CHANGE_TO_INCLUDE:
1021                case MLD2_CHANGE_TO_EXCLUDE:
1022                case MLD2_ALLOW_NEW_SOURCES:
1023                case MLD2_BLOCK_OLD_SOURCES:
1024                        break;
1025
1026                default:
1027                        continue;
1028                }
1029
1030                err = br_ip6_multicast_add_group(br, port, &grec->grec_mca);
1031                if (!err)
1032                        break;
1033        }
1034
1035        return err;
1036}
1037#endif
1038
1039/*
1040 * Add port to rotuer_list
1041 *  list is maintained ordered by pointer value
1042 *  and locked by br->multicast_lock and RCU
1043 */
1044static void br_multicast_add_router(struct net_bridge *br,
1045                                    struct net_bridge_port *port)
1046{
1047        struct net_bridge_port *p;
1048        struct hlist_node *n, *slot = NULL;
1049
1050        hlist_for_each_entry(p, n, &br->router_list, rlist) {
1051                if ((unsigned long) port >= (unsigned long) p)
1052                        break;
1053                slot = n;
1054        }
1055
1056        if (slot)
1057                hlist_add_after_rcu(slot, &port->rlist);
1058        else
1059                hlist_add_head_rcu(&port->rlist, &br->router_list);
1060}
1061
1062static void br_multicast_mark_router(struct net_bridge *br,
1063                                     struct net_bridge_port *port)
1064{
1065        unsigned long now = jiffies;
1066
1067        if (!port) {
1068                if (br->multicast_router == 1)
1069                        mod_timer(&br->multicast_router_timer,
1070                                  now + br->multicast_querier_interval);
1071                return;
1072        }
1073
1074        if (port->multicast_router != 1)
1075                return;
1076
1077        if (!hlist_unhashed(&port->rlist))
1078                goto timer;
1079
1080        br_multicast_add_router(br, port);
1081
1082timer:
1083        mod_timer(&port->multicast_router_timer,
1084                  now + br->multicast_querier_interval);
1085}
1086
1087static void br_multicast_query_received(struct net_bridge *br,
1088                                        struct net_bridge_port *port,
1089                                        int saddr)
1090{
1091        if (saddr)
1092                mod_timer(&br->multicast_querier_timer,
1093                          jiffies + br->multicast_querier_interval);
1094        else if (timer_pending(&br->multicast_querier_timer))
1095                return;
1096
1097        br_multicast_mark_router(br, port);
1098}
1099
1100static int br_ip4_multicast_query(struct net_bridge *br,
1101                                  struct net_bridge_port *port,
1102                                  struct sk_buff *skb)
1103{
1104        struct iphdr *iph = ip_hdr(skb);
1105        struct igmphdr *ih = igmp_hdr(skb);
1106        struct net_bridge_mdb_entry *mp;
1107        struct igmpv3_query *ih3;
1108        struct net_bridge_port_group *p;
1109        struct net_bridge_port_group **pp;
1110        unsigned long max_delay;
1111        unsigned long now = jiffies;
1112        __be32 group;
1113        int err = 0;
1114
1115        spin_lock(&br->multicast_lock);
1116        if (!netif_running(br->dev) ||
1117            (port && port->state == BR_STATE_DISABLED))
1118                goto out;
1119
1120        br_multicast_query_received(br, port, !!iph->saddr);
1121
1122        group = ih->group;
1123
1124        if (skb->len == sizeof(*ih)) {
1125                max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1126
1127                if (!max_delay) {
1128                        max_delay = 10 * HZ;
1129                        group = 0;
1130                }
1131        } else {
1132                if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) {
1133                        err = -EINVAL;
1134                        goto out;
1135                }
1136
1137                ih3 = igmpv3_query_hdr(skb);
1138                if (ih3->nsrcs)
1139                        goto out;
1140
1141                max_delay = ih3->code ?
1142                            IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
1143        }
1144
1145        if (!group)
1146                goto out;
1147
1148        mp = br_mdb_ip4_get(br->mdb, group);
1149        if (!mp)
1150                goto out;
1151
1152        max_delay *= br->multicast_last_member_count;
1153
1154        if (!hlist_unhashed(&mp->mglist) &&
1155            (timer_pending(&mp->timer) ?
1156             time_after(mp->timer.expires, now + max_delay) :
1157             try_to_del_timer_sync(&mp->timer) >= 0))
1158                mod_timer(&mp->timer, now + max_delay);
1159
1160        for (pp = &mp->ports; (p = *pp); pp = &p->next) {
1161                if (timer_pending(&p->timer) ?
1162                    time_after(p->timer.expires, now + max_delay) :
1163                    try_to_del_timer_sync(&p->timer) >= 0)
1164                        mod_timer(&mp->timer, now + max_delay);
1165        }
1166
1167out:
1168        spin_unlock(&br->multicast_lock);
1169        return err;
1170}
1171
1172#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1173static int br_ip6_multicast_query(struct net_bridge *br,
1174                                  struct net_bridge_port *port,
1175                                  struct sk_buff *skb)
1176{
1177        struct ipv6hdr *ip6h = ipv6_hdr(skb);
1178        struct mld_msg *mld = (struct mld_msg *) icmp6_hdr(skb);
1179        struct net_bridge_mdb_entry *mp;
1180        struct mld2_query *mld2q;
1181        struct net_bridge_port_group *p, **pp;
1182        unsigned long max_delay;
1183        unsigned long now = jiffies;
1184        struct in6_addr *group = NULL;
1185        int err = 0;
1186
1187        spin_lock(&br->multicast_lock);
1188        if (!netif_running(br->dev) ||
1189            (port && port->state == BR_STATE_DISABLED))
1190                goto out;
1191
1192        br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->saddr));
1193
1194        if (skb->len == sizeof(*mld)) {
1195                if (!pskb_may_pull(skb, sizeof(*mld))) {
1196                        err = -EINVAL;
1197                        goto out;
1198                }
1199                mld = (struct mld_msg *) icmp6_hdr(skb);
1200                max_delay = msecs_to_jiffies(htons(mld->mld_maxdelay));
1201                if (max_delay)
1202                        group = &mld->mld_mca;
1203        } else if (skb->len >= sizeof(*mld2q)) {
1204                if (!pskb_may_pull(skb, sizeof(*mld2q))) {
1205                        err = -EINVAL;
1206                        goto out;
1207                }
1208                mld2q = (struct mld2_query *)icmp6_hdr(skb);
1209                if (!mld2q->mld2q_nsrcs)
1210                        group = &mld2q->mld2q_mca;
1211                max_delay = mld2q->mld2q_mrc ? MLDV2_MRC(mld2q->mld2q_mrc) : 1;
1212        }
1213
1214        if (!group)
1215                goto out;
1216
1217        mp = br_mdb_ip6_get(br->mdb, group);
1218        if (!mp)
1219                goto out;
1220
1221        max_delay *= br->multicast_last_member_count;
1222        if (!hlist_unhashed(&mp->mglist) &&
1223            (timer_pending(&mp->timer) ?
1224             time_after(mp->timer.expires, now + max_delay) :
1225             try_to_del_timer_sync(&mp->timer) >= 0))
1226                mod_timer(&mp->timer, now + max_delay);
1227
1228        for (pp = &mp->ports; (p = *pp); pp = &p->next) {
1229                if (timer_pending(&p->timer) ?
1230                    time_after(p->timer.expires, now + max_delay) :
1231                    try_to_del_timer_sync(&p->timer) >= 0)
1232                        mod_timer(&mp->timer, now + max_delay);
1233        }
1234
1235out:
1236        spin_unlock(&br->multicast_lock);
1237        return err;
1238}
1239#endif
1240
1241static void br_multicast_leave_group(struct net_bridge *br,
1242                                     struct net_bridge_port *port,
1243                                     struct br_ip *group)
1244{
1245        struct net_bridge_mdb_htable *mdb;
1246        struct net_bridge_mdb_entry *mp;
1247        struct net_bridge_port_group *p;
1248        unsigned long now;
1249        unsigned long time;
1250
1251        spin_lock(&br->multicast_lock);
1252        if (!netif_running(br->dev) ||
1253            (port && port->state == BR_STATE_DISABLED) ||
1254            timer_pending(&br->multicast_querier_timer))
1255                goto out;
1256
1257        mdb = br->mdb;
1258        mp = br_mdb_ip_get(mdb, group);
1259        if (!mp)
1260                goto out;
1261
1262        now = jiffies;
1263        time = now + br->multicast_last_member_count *
1264                     br->multicast_last_member_interval;
1265
1266        if (!port) {
1267                if (!hlist_unhashed(&mp->mglist) &&
1268                    (timer_pending(&mp->timer) ?
1269                     time_after(mp->timer.expires, time) :
1270                     try_to_del_timer_sync(&mp->timer) >= 0)) {
1271                        mod_timer(&mp->timer, time);
1272
1273                        mp->queries_sent = 0;
1274                        mod_timer(&mp->query_timer, now);
1275                }
1276
1277                goto out;
1278        }
1279
1280        for (p = mp->ports; p; p = p->next) {
1281                if (p->port != port)
1282                        continue;
1283
1284                if (!hlist_unhashed(&p->mglist) &&
1285                    (timer_pending(&p->timer) ?
1286                     time_after(p->timer.expires, time) :
1287                     try_to_del_timer_sync(&p->timer) >= 0)) {
1288                        mod_timer(&p->timer, time);
1289
1290                        p->queries_sent = 0;
1291                        mod_timer(&p->query_timer, now);
1292                }
1293
1294                break;
1295        }
1296
1297out:
1298        spin_unlock(&br->multicast_lock);
1299}
1300
1301static void br_ip4_multicast_leave_group(struct net_bridge *br,
1302                                         struct net_bridge_port *port,
1303                                         __be32 group)
1304{
1305        struct br_ip br_group;
1306
1307        if (ipv4_is_local_multicast(group))
1308                return;
1309
1310        br_group.u.ip4 = group;
1311        br_group.proto = htons(ETH_P_IP);
1312
1313        br_multicast_leave_group(br, port, &br_group);
1314}
1315
1316#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1317static void br_ip6_multicast_leave_group(struct net_bridge *br,
1318                                         struct net_bridge_port *port,
1319                                         const struct in6_addr *group)
1320{
1321        struct br_ip br_group;
1322
1323        if (ipv6_is_local_multicast(group))
1324                return;
1325
1326        ipv6_addr_copy(&br_group.u.ip6, group);
1327        br_group.proto = htons(ETH_P_IPV6);
1328
1329        br_multicast_leave_group(br, port, &br_group);
1330}
1331#endif
1332
1333static int br_multicast_ipv4_rcv(struct net_bridge *br,
1334                                 struct net_bridge_port *port,
1335                                 struct sk_buff *skb)
1336{
1337        struct sk_buff *skb2 = skb;
1338        struct iphdr *iph;
1339        struct igmphdr *ih;
1340        unsigned len;
1341        unsigned offset;
1342        int err;
1343
1344        /* We treat OOM as packet loss for now. */
1345        if (!pskb_may_pull(skb, sizeof(*iph)))
1346                return -EINVAL;
1347
1348        iph = ip_hdr(skb);
1349
1350        if (iph->ihl < 5 || iph->version != 4)
1351                return -EINVAL;
1352
1353        if (!pskb_may_pull(skb, ip_hdrlen(skb)))
1354                return -EINVAL;
1355
1356        iph = ip_hdr(skb);
1357
1358        if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1359                return -EINVAL;
1360
1361        if (iph->protocol != IPPROTO_IGMP)
1362                return 0;
1363
1364        len = ntohs(iph->tot_len);
1365        if (skb->len < len || len < ip_hdrlen(skb))
1366                return -EINVAL;
1367
1368        if (skb->len > len) {
1369                skb2 = skb_clone(skb, GFP_ATOMIC);
1370                if (!skb2)
1371                        return -ENOMEM;
1372
1373                err = pskb_trim_rcsum(skb2, len);
1374                if (err)
1375                        goto err_out;
1376        }
1377
1378        len -= ip_hdrlen(skb2);
1379        offset = skb_network_offset(skb2) + ip_hdrlen(skb2);
1380        __skb_pull(skb2, offset);
1381        skb_reset_transport_header(skb2);
1382
1383        err = -EINVAL;
1384        if (!pskb_may_pull(skb2, sizeof(*ih)))
1385                goto out;
1386
1387        switch (skb2->ip_summed) {
1388        case CHECKSUM_COMPLETE:
1389                if (!csum_fold(skb2->csum))
1390                        break;
1391                /* fall through */
1392        case CHECKSUM_NONE:
1393                skb2->csum = 0;
1394                if (skb_checksum_complete(skb2))
1395                        goto out;
1396        }
1397
1398        err = 0;
1399
1400        BR_INPUT_SKB_CB(skb)->igmp = 1;
1401        ih = igmp_hdr(skb2);
1402
1403        switch (ih->type) {
1404        case IGMP_HOST_MEMBERSHIP_REPORT:
1405        case IGMPV2_HOST_MEMBERSHIP_REPORT:
1406                BR_INPUT_SKB_CB(skb2)->mrouters_only = 1;
1407                err = br_ip4_multicast_add_group(br, port, ih->group);
1408                break;
1409        case IGMPV3_HOST_MEMBERSHIP_REPORT:
1410                err = br_ip4_multicast_igmp3_report(br, port, skb2);
1411                break;
1412        case IGMP_HOST_MEMBERSHIP_QUERY:
1413                err = br_ip4_multicast_query(br, port, skb2);
1414                break;
1415        case IGMP_HOST_LEAVE_MESSAGE:
1416                br_ip4_multicast_leave_group(br, port, ih->group);
1417                break;
1418        }
1419
1420out:
1421        __skb_push(skb2, offset);
1422err_out:
1423        if (skb2 != skb)
1424                kfree_skb(skb2);
1425        return err;
1426}
1427
1428#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1429static int br_multicast_ipv6_rcv(struct net_bridge *br,
1430                                 struct net_bridge_port *port,
1431                                 struct sk_buff *skb)
1432{
1433        struct sk_buff *skb2;
1434        struct ipv6hdr *ip6h;
1435        struct icmp6hdr *icmp6h;
1436        u8 nexthdr;
1437        unsigned len;
1438        int offset;
1439        int err;
1440
1441        if (!pskb_may_pull(skb, sizeof(*ip6h)))
1442                return -EINVAL;
1443
1444        ip6h = ipv6_hdr(skb);
1445
1446        /*
1447         * We're interested in MLD messages only.
1448         *  - Version is 6
1449         *  - MLD has always Router Alert hop-by-hop option
1450         *  - But we do not support jumbrograms.
1451         */
1452        if (ip6h->version != 6 ||
1453            ip6h->nexthdr != IPPROTO_HOPOPTS ||
1454            ip6h->payload_len == 0)
1455                return 0;
1456
1457        len = ntohs(ip6h->payload_len);
1458        if (skb->len < len)
1459                return -EINVAL;
1460
1461        nexthdr = ip6h->nexthdr;
1462        offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr);
1463
1464        if (offset < 0 || nexthdr != IPPROTO_ICMPV6)
1465                return 0;
1466
1467        /* Okay, we found ICMPv6 header */
1468        skb2 = skb_clone(skb, GFP_ATOMIC);
1469        if (!skb2)
1470                return -ENOMEM;
1471
1472        err = -EINVAL;
1473        if (!pskb_may_pull(skb2, offset + sizeof(struct icmp6hdr)))
1474                goto out;
1475
1476        len -= offset - skb_network_offset(skb2);
1477
1478        __skb_pull(skb2, offset);
1479        skb_reset_transport_header(skb2);
1480
1481        icmp6h = icmp6_hdr(skb2);
1482
1483        switch (icmp6h->icmp6_type) {
1484        case ICMPV6_MGM_QUERY:
1485        case ICMPV6_MGM_REPORT:
1486        case ICMPV6_MGM_REDUCTION:
1487        case ICMPV6_MLD2_REPORT:
1488                break;
1489        default:
1490                err = 0;
1491                goto out;
1492        }
1493
1494        /* Okay, we found MLD message. Check further. */
1495        if (skb2->len > len) {
1496                err = pskb_trim_rcsum(skb2, len);
1497                if (err)
1498                        goto out;
1499        }
1500
1501        switch (skb2->ip_summed) {
1502        case CHECKSUM_COMPLETE:
1503                if (!csum_fold(skb2->csum))
1504                        break;
1505                /*FALLTHROUGH*/
1506        case CHECKSUM_NONE:
1507                skb2->csum = 0;
1508                if (skb_checksum_complete(skb2))
1509                        goto out;
1510        }
1511
1512        err = 0;
1513
1514        BR_INPUT_SKB_CB(skb)->igmp = 1;
1515
1516        switch (icmp6h->icmp6_type) {
1517        case ICMPV6_MGM_REPORT:
1518            {
1519                struct mld_msg *mld;
1520                if (!pskb_may_pull(skb2, sizeof(*mld))) {
1521                        err = -EINVAL;
1522                        goto out;
1523                }
1524                mld = (struct mld_msg *)skb_transport_header(skb2);
1525                BR_INPUT_SKB_CB(skb2)->mrouters_only = 1;
1526                err = br_ip6_multicast_add_group(br, port, &mld->mld_mca);
1527                break;
1528            }
1529        case ICMPV6_MLD2_REPORT:
1530                err = br_ip6_multicast_mld2_report(br, port, skb2);
1531                break;
1532        case ICMPV6_MGM_QUERY:
1533                err = br_ip6_multicast_query(br, port, skb2);
1534                break;
1535        case ICMPV6_MGM_REDUCTION:
1536            {
1537                struct mld_msg *mld;
1538                if (!pskb_may_pull(skb2, sizeof(*mld))) {
1539                        err = -EINVAL;
1540                        goto out;
1541                }
1542                mld = (struct mld_msg *)skb_transport_header(skb2);
1543                br_ip6_multicast_leave_group(br, port, &mld->mld_mca);
1544            }
1545        }
1546
1547out:
1548        kfree_skb(skb2);
1549        return err;
1550}
1551#endif
1552
1553int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1554                     struct sk_buff *skb)
1555{
1556        BR_INPUT_SKB_CB(skb)->igmp = 0;
1557        BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1558
1559        if (br->multicast_disabled)
1560                return 0;
1561
1562        switch (skb->protocol) {
1563        case htons(ETH_P_IP):
1564                return br_multicast_ipv4_rcv(br, port, skb);
1565#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1566        case htons(ETH_P_IPV6):
1567                return br_multicast_ipv6_rcv(br, port, skb);
1568#endif
1569        }
1570
1571        return 0;
1572}
1573
1574static void br_multicast_query_expired(unsigned long data)
1575{
1576        struct net_bridge *br = (void *)data;
1577
1578        spin_lock(&br->multicast_lock);
1579        if (br->multicast_startup_queries_sent <
1580            br->multicast_startup_query_count)
1581                br->multicast_startup_queries_sent++;
1582
1583        br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent);
1584
1585        spin_unlock(&br->multicast_lock);
1586}
1587
1588void br_multicast_init(struct net_bridge *br)
1589{
1590        br->hash_elasticity = 4;
1591        br->hash_max = 512;
1592
1593        br->multicast_router = 1;
1594        br->multicast_last_member_count = 2;
1595        br->multicast_startup_query_count = 2;
1596
1597        br->multicast_last_member_interval = HZ;
1598        br->multicast_query_response_interval = 10 * HZ;
1599        br->multicast_startup_query_interval = 125 * HZ / 4;
1600        br->multicast_query_interval = 125 * HZ;
1601        br->multicast_querier_interval = 255 * HZ;
1602        br->multicast_membership_interval = 260 * HZ;
1603
1604        spin_lock_init(&br->multicast_lock);
1605        setup_timer(&br->multicast_router_timer,
1606                    br_multicast_local_router_expired, 0);
1607        setup_timer(&br->multicast_querier_timer,
1608                    br_multicast_local_router_expired, 0);
1609        setup_timer(&br->multicast_query_timer, br_multicast_query_expired,
1610                    (unsigned long)br);
1611}
1612
1613void br_multicast_open(struct net_bridge *br)
1614{
1615        br->multicast_startup_queries_sent = 0;
1616
1617        if (br->multicast_disabled)
1618                return;
1619
1620        mod_timer(&br->multicast_query_timer, jiffies);
1621}
1622
1623void br_multicast_stop(struct net_bridge *br)
1624{
1625        struct net_bridge_mdb_htable *mdb;
1626        struct net_bridge_mdb_entry *mp;
1627        struct hlist_node *p, *n;
1628        u32 ver;
1629        int i;
1630
1631        del_timer_sync(&br->multicast_router_timer);
1632        del_timer_sync(&br->multicast_querier_timer);
1633        del_timer_sync(&br->multicast_query_timer);
1634
1635        spin_lock_bh(&br->multicast_lock);
1636        mdb = br->mdb;
1637        if (!mdb)
1638                goto out;
1639
1640        br->mdb = NULL;
1641
1642        ver = mdb->ver;
1643        for (i = 0; i < mdb->max; i++) {
1644                hlist_for_each_entry_safe(mp, p, n, &mdb->mhash[i],
1645                                          hlist[ver]) {
1646                        del_timer(&mp->timer);
1647                        del_timer(&mp->query_timer);
1648                        call_rcu_bh(&mp->rcu, br_multicast_free_group);
1649                }
1650        }
1651
1652        if (mdb->old) {
1653                spin_unlock_bh(&br->multicast_lock);
1654                rcu_barrier_bh();
1655                spin_lock_bh(&br->multicast_lock);
1656                WARN_ON(mdb->old);
1657        }
1658
1659        mdb->old = mdb;
1660        call_rcu_bh(&mdb->rcu, br_mdb_free);
1661
1662out:
1663        spin_unlock_bh(&br->multicast_lock);
1664}
1665
1666int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1667{
1668        int err = -ENOENT;
1669
1670        spin_lock_bh(&br->multicast_lock);
1671        if (!netif_running(br->dev))
1672                goto unlock;
1673
1674        switch (val) {
1675        case 0:
1676        case 2:
1677                del_timer(&br->multicast_router_timer);
1678                /* fall through */
1679        case 1:
1680                br->multicast_router = val;
1681                err = 0;
1682                break;
1683
1684        default:
1685                err = -EINVAL;
1686                break;
1687        }
1688
1689unlock:
1690        spin_unlock_bh(&br->multicast_lock);
1691
1692        return err;
1693}
1694
1695int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1696{
1697        struct net_bridge *br = p->br;
1698        int err = -ENOENT;
1699
1700        spin_lock(&br->multicast_lock);
1701        if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED)
1702                goto unlock;
1703
1704        switch (val) {
1705        case 0:
1706        case 1:
1707        case 2:
1708                p->multicast_router = val;
1709                err = 0;
1710
1711                if (val < 2 && !hlist_unhashed(&p->rlist))
1712                        hlist_del_init_rcu(&p->rlist);
1713
1714                if (val == 1)
1715                        break;
1716
1717                del_timer(&p->multicast_router_timer);
1718
1719                if (val == 0)
1720                        break;
1721
1722                br_multicast_add_router(br, p);
1723                break;
1724
1725        default:
1726                err = -EINVAL;
1727                break;
1728        }
1729
1730unlock:
1731        spin_unlock(&br->multicast_lock);
1732
1733        return err;
1734}
1735
1736int br_multicast_toggle(struct net_bridge *br, unsigned long val)
1737{
1738        struct net_bridge_port *port;
1739        int err = 0;
1740
1741        spin_lock(&br->multicast_lock);
1742        if (br->multicast_disabled == !val)
1743                goto unlock;
1744
1745        br->multicast_disabled = !val;
1746        if (br->multicast_disabled)
1747                goto unlock;
1748
1749        if (!netif_running(br->dev))
1750                goto unlock;
1751
1752        if (br->mdb) {
1753                if (br->mdb->old) {
1754                        err = -EEXIST;
1755rollback:
1756                        br->multicast_disabled = !!val;
1757                        goto unlock;
1758                }
1759
1760                err = br_mdb_rehash(&br->mdb, br->mdb->max,
1761                                    br->hash_elasticity);
1762                if (err)
1763                        goto rollback;
1764        }
1765
1766        br_multicast_open(br);
1767        list_for_each_entry(port, &br->port_list, list) {
1768                if (port->state == BR_STATE_DISABLED ||
1769                    port->state == BR_STATE_BLOCKING)
1770                        continue;
1771
1772                __br_multicast_enable_port(port);
1773        }
1774
1775unlock:
1776        spin_unlock(&br->multicast_lock);
1777
1778        return err;
1779}
1780
1781int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
1782{
1783        int err = -ENOENT;
1784        u32 old;
1785
1786        spin_lock(&br->multicast_lock);
1787        if (!netif_running(br->dev))
1788                goto unlock;
1789
1790        err = -EINVAL;
1791        if (!is_power_of_2(val))
1792                goto unlock;
1793        if (br->mdb && val < br->mdb->size)
1794                goto unlock;
1795
1796        err = 0;
1797
1798        old = br->hash_max;
1799        br->hash_max = val;
1800
1801        if (br->mdb) {
1802                if (br->mdb->old) {
1803                        err = -EEXIST;
1804rollback:
1805                        br->hash_max = old;
1806                        goto unlock;
1807                }
1808
1809                err = br_mdb_rehash(&br->mdb, br->hash_max,
1810                                    br->hash_elasticity);
1811                if (err)
1812                        goto rollback;
1813        }
1814
1815unlock:
1816        spin_unlock(&br->multicast_lock);
1817
1818        return err;
1819}
1820