1eb1d1641SHerbert Xu /* 2eb1d1641SHerbert Xu * Bridge multicast support. 3eb1d1641SHerbert Xu * 4eb1d1641SHerbert Xu * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au> 5eb1d1641SHerbert Xu * 6eb1d1641SHerbert Xu * This program is free software; you can redistribute it and/or modify it 7eb1d1641SHerbert Xu * under the terms of the GNU General Public License as published by the Free 8eb1d1641SHerbert Xu * Software Foundation; either version 2 of the License, or (at your option) 9eb1d1641SHerbert Xu * any later version. 10eb1d1641SHerbert Xu * 11eb1d1641SHerbert Xu */ 12eb1d1641SHerbert Xu 13eb1d1641SHerbert Xu #include <linux/err.h> 14eb1d1641SHerbert Xu #include <linux/if_ether.h> 15eb1d1641SHerbert Xu #include <linux/igmp.h> 16eb1d1641SHerbert Xu #include <linux/jhash.h> 17eb1d1641SHerbert Xu #include <linux/kernel.h> 18b195167fSHerbert Xu #include <linux/log2.h> 19eb1d1641SHerbert Xu #include <linux/netdevice.h> 20eb1d1641SHerbert Xu #include <linux/netfilter_bridge.h> 21eb1d1641SHerbert Xu #include <linux/random.h> 22eb1d1641SHerbert Xu #include <linux/rculist.h> 23eb1d1641SHerbert Xu #include <linux/skbuff.h> 24eb1d1641SHerbert Xu #include <linux/slab.h> 25eb1d1641SHerbert Xu #include <linux/timer.h> 26eb1d1641SHerbert Xu #include <net/ip.h> 2708b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 2808b202b6SYOSHIFUJI Hideaki #include <net/ipv6.h> 2908b202b6SYOSHIFUJI Hideaki #include <net/mld.h> 3008b202b6SYOSHIFUJI Hideaki #include <net/addrconf.h> 31d4c4f07dSDavid S. Miller #include <net/ip6_checksum.h> 3208b202b6SYOSHIFUJI Hideaki #endif 33eb1d1641SHerbert Xu 34eb1d1641SHerbert Xu #include "br_private.h" 35eb1d1641SHerbert Xu 3608b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 3708b202b6SYOSHIFUJI Hideaki static inline int ipv6_is_local_multicast(const struct in6_addr *addr) 38eb1d1641SHerbert Xu { 3908b202b6SYOSHIFUJI Hideaki if (ipv6_addr_is_multicast(addr) && 4008b202b6SYOSHIFUJI Hideaki IPV6_ADDR_MC_SCOPE(addr) <= IPV6_ADDR_SCOPE_LINKLOCAL) 4108b202b6SYOSHIFUJI Hideaki return 1; 4208b202b6SYOSHIFUJI Hideaki return 0; 4308b202b6SYOSHIFUJI Hideaki } 4408b202b6SYOSHIFUJI Hideaki #endif 4508b202b6SYOSHIFUJI Hideaki 468ef2a9a5SYOSHIFUJI Hideaki static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b) 478ef2a9a5SYOSHIFUJI Hideaki { 488ef2a9a5SYOSHIFUJI Hideaki if (a->proto != b->proto) 498ef2a9a5SYOSHIFUJI Hideaki return 0; 508ef2a9a5SYOSHIFUJI Hideaki switch (a->proto) { 518ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 528ef2a9a5SYOSHIFUJI Hideaki return a->u.ip4 == b->u.ip4; 5308b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 5408b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 5508b202b6SYOSHIFUJI Hideaki return ipv6_addr_equal(&a->u.ip6, &b->u.ip6); 5608b202b6SYOSHIFUJI Hideaki #endif 578ef2a9a5SYOSHIFUJI Hideaki } 588ef2a9a5SYOSHIFUJI Hideaki return 0; 598ef2a9a5SYOSHIFUJI Hideaki } 608ef2a9a5SYOSHIFUJI Hideaki 618ef2a9a5SYOSHIFUJI Hideaki static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip) 62eb1d1641SHerbert Xu { 630eae88f3SEric Dumazet return jhash_1word(mdb->secret, (__force u32)ip) & (mdb->max - 1); 64eb1d1641SHerbert Xu } 65eb1d1641SHerbert Xu 6608b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 6708b202b6SYOSHIFUJI Hideaki static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb, 6808b202b6SYOSHIFUJI Hideaki const struct in6_addr *ip) 6908b202b6SYOSHIFUJI Hideaki { 7008b202b6SYOSHIFUJI Hideaki return jhash2((__force u32 *)ip->s6_addr32, 4, mdb->secret) & (mdb->max - 1); 7108b202b6SYOSHIFUJI Hideaki } 7208b202b6SYOSHIFUJI Hideaki #endif 7308b202b6SYOSHIFUJI Hideaki 748ef2a9a5SYOSHIFUJI Hideaki static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb, 758ef2a9a5SYOSHIFUJI Hideaki struct br_ip *ip) 768ef2a9a5SYOSHIFUJI Hideaki { 778ef2a9a5SYOSHIFUJI Hideaki switch (ip->proto) { 788ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 798ef2a9a5SYOSHIFUJI Hideaki return __br_ip4_hash(mdb, ip->u.ip4); 8008b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 8108b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 8208b202b6SYOSHIFUJI Hideaki return __br_ip6_hash(mdb, &ip->u.ip6); 8308b202b6SYOSHIFUJI Hideaki #endif 848ef2a9a5SYOSHIFUJI Hideaki } 858ef2a9a5SYOSHIFUJI Hideaki return 0; 86eb1d1641SHerbert Xu } 87eb1d1641SHerbert Xu 88eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *__br_mdb_ip_get( 898ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash) 90eb1d1641SHerbert Xu { 91eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 92eb1d1641SHerbert Xu struct hlist_node *p; 93eb1d1641SHerbert Xu 9449f5fcfdSHerbert Xu hlist_for_each_entry_rcu(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) { 958ef2a9a5SYOSHIFUJI Hideaki if (br_ip_equal(&mp->addr, dst)) 96eb1d1641SHerbert Xu return mp; 97eb1d1641SHerbert Xu } 98eb1d1641SHerbert Xu 99eb1d1641SHerbert Xu return NULL; 100eb1d1641SHerbert Xu } 101eb1d1641SHerbert Xu 102*7f285fa7SHerbert Xu static struct net_bridge_mdb_entry *br_mdb_ip_get( 103*7f285fa7SHerbert Xu struct net_bridge_mdb_htable *mdb, struct br_ip *dst) 104*7f285fa7SHerbert Xu { 105*7f285fa7SHerbert Xu if (!mdb) 106*7f285fa7SHerbert Xu return NULL; 107*7f285fa7SHerbert Xu 108*7f285fa7SHerbert Xu return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst)); 109*7f285fa7SHerbert Xu } 110*7f285fa7SHerbert Xu 1118ef2a9a5SYOSHIFUJI Hideaki static struct net_bridge_mdb_entry *br_mdb_ip4_get( 112eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb, __be32 dst) 113eb1d1641SHerbert Xu { 1148ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_dst; 1150821ec55SHerbert Xu 1168ef2a9a5SYOSHIFUJI Hideaki br_dst.u.ip4 = dst; 1178ef2a9a5SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IP); 1188ef2a9a5SYOSHIFUJI Hideaki 119*7f285fa7SHerbert Xu return br_mdb_ip_get(mdb, &br_dst); 1208ef2a9a5SYOSHIFUJI Hideaki } 1218ef2a9a5SYOSHIFUJI Hideaki 12208b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 12308b202b6SYOSHIFUJI Hideaki static struct net_bridge_mdb_entry *br_mdb_ip6_get( 12408b202b6SYOSHIFUJI Hideaki struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst) 12508b202b6SYOSHIFUJI Hideaki { 12608b202b6SYOSHIFUJI Hideaki struct br_ip br_dst; 12708b202b6SYOSHIFUJI Hideaki 12808b202b6SYOSHIFUJI Hideaki ipv6_addr_copy(&br_dst.u.ip6, dst); 12908b202b6SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IPV6); 13008b202b6SYOSHIFUJI Hideaki 131*7f285fa7SHerbert Xu return br_mdb_ip_get(mdb, &br_dst); 13208b202b6SYOSHIFUJI Hideaki } 13308b202b6SYOSHIFUJI Hideaki #endif 13408b202b6SYOSHIFUJI Hideaki 135eb1d1641SHerbert Xu struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br, 136eb1d1641SHerbert Xu struct sk_buff *skb) 137eb1d1641SHerbert Xu { 1388ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_mdb_htable *mdb = br->mdb; 1398ef2a9a5SYOSHIFUJI Hideaki struct br_ip ip; 1408ef2a9a5SYOSHIFUJI Hideaki 141*7f285fa7SHerbert Xu if (br->multicast_disabled) 142eb1d1641SHerbert Xu return NULL; 143eb1d1641SHerbert Xu 1448ef2a9a5SYOSHIFUJI Hideaki if (BR_INPUT_SKB_CB(skb)->igmp) 1458ef2a9a5SYOSHIFUJI Hideaki return NULL; 1468ef2a9a5SYOSHIFUJI Hideaki 1478ef2a9a5SYOSHIFUJI Hideaki ip.proto = skb->protocol; 1488ef2a9a5SYOSHIFUJI Hideaki 149eb1d1641SHerbert Xu switch (skb->protocol) { 150eb1d1641SHerbert Xu case htons(ETH_P_IP): 1518ef2a9a5SYOSHIFUJI Hideaki ip.u.ip4 = ip_hdr(skb)->daddr; 152eb1d1641SHerbert Xu break; 15308b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 15408b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 15508b202b6SYOSHIFUJI Hideaki ipv6_addr_copy(&ip.u.ip6, &ipv6_hdr(skb)->daddr); 15608b202b6SYOSHIFUJI Hideaki break; 15708b202b6SYOSHIFUJI Hideaki #endif 1588ef2a9a5SYOSHIFUJI Hideaki default: 1598ef2a9a5SYOSHIFUJI Hideaki return NULL; 160eb1d1641SHerbert Xu } 161eb1d1641SHerbert Xu 1628ef2a9a5SYOSHIFUJI Hideaki return br_mdb_ip_get(mdb, &ip); 163eb1d1641SHerbert Xu } 164eb1d1641SHerbert Xu 165eb1d1641SHerbert Xu static void br_mdb_free(struct rcu_head *head) 166eb1d1641SHerbert Xu { 167eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb = 168eb1d1641SHerbert Xu container_of(head, struct net_bridge_mdb_htable, rcu); 169eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old = mdb->old; 170eb1d1641SHerbert Xu 171eb1d1641SHerbert Xu mdb->old = NULL; 172eb1d1641SHerbert Xu kfree(old->mhash); 173eb1d1641SHerbert Xu kfree(old); 174eb1d1641SHerbert Xu } 175eb1d1641SHerbert Xu 176eb1d1641SHerbert Xu static int br_mdb_copy(struct net_bridge_mdb_htable *new, 177eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old, 178eb1d1641SHerbert Xu int elasticity) 179eb1d1641SHerbert Xu { 180eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 181eb1d1641SHerbert Xu struct hlist_node *p; 182eb1d1641SHerbert Xu int maxlen; 183eb1d1641SHerbert Xu int len; 184eb1d1641SHerbert Xu int i; 185eb1d1641SHerbert Xu 186eb1d1641SHerbert Xu for (i = 0; i < old->max; i++) 187eb1d1641SHerbert Xu hlist_for_each_entry(mp, p, &old->mhash[i], hlist[old->ver]) 188eb1d1641SHerbert Xu hlist_add_head(&mp->hlist[new->ver], 1898ef2a9a5SYOSHIFUJI Hideaki &new->mhash[br_ip_hash(new, &mp->addr)]); 190eb1d1641SHerbert Xu 191eb1d1641SHerbert Xu if (!elasticity) 192eb1d1641SHerbert Xu return 0; 193eb1d1641SHerbert Xu 194eb1d1641SHerbert Xu maxlen = 0; 195eb1d1641SHerbert Xu for (i = 0; i < new->max; i++) { 196eb1d1641SHerbert Xu len = 0; 197eb1d1641SHerbert Xu hlist_for_each_entry(mp, p, &new->mhash[i], hlist[new->ver]) 198eb1d1641SHerbert Xu len++; 199eb1d1641SHerbert Xu if (len > maxlen) 200eb1d1641SHerbert Xu maxlen = len; 201eb1d1641SHerbert Xu } 202eb1d1641SHerbert Xu 203eb1d1641SHerbert Xu return maxlen > elasticity ? -EINVAL : 0; 204eb1d1641SHerbert Xu } 205eb1d1641SHerbert Xu 206eb1d1641SHerbert Xu static void br_multicast_free_pg(struct rcu_head *head) 207eb1d1641SHerbert Xu { 208eb1d1641SHerbert Xu struct net_bridge_port_group *p = 209eb1d1641SHerbert Xu container_of(head, struct net_bridge_port_group, rcu); 210eb1d1641SHerbert Xu 211eb1d1641SHerbert Xu kfree(p); 212eb1d1641SHerbert Xu } 213eb1d1641SHerbert Xu 214eb1d1641SHerbert Xu static void br_multicast_free_group(struct rcu_head *head) 215eb1d1641SHerbert Xu { 216eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = 217eb1d1641SHerbert Xu container_of(head, struct net_bridge_mdb_entry, rcu); 218eb1d1641SHerbert Xu 219eb1d1641SHerbert Xu kfree(mp); 220eb1d1641SHerbert Xu } 221eb1d1641SHerbert Xu 222eb1d1641SHerbert Xu static void br_multicast_group_expired(unsigned long data) 223eb1d1641SHerbert Xu { 224eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = (void *)data; 225eb1d1641SHerbert Xu struct net_bridge *br = mp->br; 226eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 227eb1d1641SHerbert Xu 228eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 229eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&mp->timer)) 230eb1d1641SHerbert Xu goto out; 231eb1d1641SHerbert Xu 232eb1d1641SHerbert Xu if (!hlist_unhashed(&mp->mglist)) 233eb1d1641SHerbert Xu hlist_del_init(&mp->mglist); 234eb1d1641SHerbert Xu 235eb1d1641SHerbert Xu if (mp->ports) 236eb1d1641SHerbert Xu goto out; 237eb1d1641SHerbert Xu 238eb1d1641SHerbert Xu mdb = br->mdb; 239eb1d1641SHerbert Xu hlist_del_rcu(&mp->hlist[mdb->ver]); 240eb1d1641SHerbert Xu mdb->size--; 241eb1d1641SHerbert Xu 242eb1d1641SHerbert Xu del_timer(&mp->query_timer); 243eb1d1641SHerbert Xu call_rcu_bh(&mp->rcu, br_multicast_free_group); 244eb1d1641SHerbert Xu 245eb1d1641SHerbert Xu out: 246eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 247eb1d1641SHerbert Xu } 248eb1d1641SHerbert Xu 249eb1d1641SHerbert Xu static void br_multicast_del_pg(struct net_bridge *br, 250eb1d1641SHerbert Xu struct net_bridge_port_group *pg) 251eb1d1641SHerbert Xu { 252eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb = br->mdb; 253eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 254eb1d1641SHerbert Xu struct net_bridge_port_group *p; 255eb1d1641SHerbert Xu struct net_bridge_port_group **pp; 256eb1d1641SHerbert Xu 2578ef2a9a5SYOSHIFUJI Hideaki mp = br_mdb_ip_get(mdb, &pg->addr); 258eb1d1641SHerbert Xu if (WARN_ON(!mp)) 259eb1d1641SHerbert Xu return; 260eb1d1641SHerbert Xu 261eb1d1641SHerbert Xu for (pp = &mp->ports; (p = *pp); pp = &p->next) { 262eb1d1641SHerbert Xu if (p != pg) 263eb1d1641SHerbert Xu continue; 264eb1d1641SHerbert Xu 26583f6a740Sstephen hemminger rcu_assign_pointer(*pp, p->next); 266eb1d1641SHerbert Xu hlist_del_init(&p->mglist); 267eb1d1641SHerbert Xu del_timer(&p->timer); 268eb1d1641SHerbert Xu del_timer(&p->query_timer); 269eb1d1641SHerbert Xu call_rcu_bh(&p->rcu, br_multicast_free_pg); 270eb1d1641SHerbert Xu 271eb1d1641SHerbert Xu if (!mp->ports && hlist_unhashed(&mp->mglist) && 272eb1d1641SHerbert Xu netif_running(br->dev)) 273eb1d1641SHerbert Xu mod_timer(&mp->timer, jiffies); 274eb1d1641SHerbert Xu 275eb1d1641SHerbert Xu return; 276eb1d1641SHerbert Xu } 277eb1d1641SHerbert Xu 278eb1d1641SHerbert Xu WARN_ON(1); 279eb1d1641SHerbert Xu } 280eb1d1641SHerbert Xu 281eb1d1641SHerbert Xu static void br_multicast_port_group_expired(unsigned long data) 282eb1d1641SHerbert Xu { 283eb1d1641SHerbert Xu struct net_bridge_port_group *pg = (void *)data; 284eb1d1641SHerbert Xu struct net_bridge *br = pg->port->br; 285eb1d1641SHerbert Xu 286eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 287eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&pg->timer) || 288eb1d1641SHerbert Xu hlist_unhashed(&pg->mglist)) 289eb1d1641SHerbert Xu goto out; 290eb1d1641SHerbert Xu 291eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 292eb1d1641SHerbert Xu 293eb1d1641SHerbert Xu out: 294eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 295eb1d1641SHerbert Xu } 296eb1d1641SHerbert Xu 297eb1d1641SHerbert Xu static int br_mdb_rehash(struct net_bridge_mdb_htable **mdbp, int max, 298eb1d1641SHerbert Xu int elasticity) 299eb1d1641SHerbert Xu { 300eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old = *mdbp; 301eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 302eb1d1641SHerbert Xu int err; 303eb1d1641SHerbert Xu 304eb1d1641SHerbert Xu mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC); 305eb1d1641SHerbert Xu if (!mdb) 306eb1d1641SHerbert Xu return -ENOMEM; 307eb1d1641SHerbert Xu 308eb1d1641SHerbert Xu mdb->max = max; 309eb1d1641SHerbert Xu mdb->old = old; 310eb1d1641SHerbert Xu 311eb1d1641SHerbert Xu mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC); 312eb1d1641SHerbert Xu if (!mdb->mhash) { 313eb1d1641SHerbert Xu kfree(mdb); 314eb1d1641SHerbert Xu return -ENOMEM; 315eb1d1641SHerbert Xu } 316eb1d1641SHerbert Xu 317eb1d1641SHerbert Xu mdb->size = old ? old->size : 0; 318eb1d1641SHerbert Xu mdb->ver = old ? old->ver ^ 1 : 0; 319eb1d1641SHerbert Xu 320eb1d1641SHerbert Xu if (!old || elasticity) 321eb1d1641SHerbert Xu get_random_bytes(&mdb->secret, sizeof(mdb->secret)); 322eb1d1641SHerbert Xu else 323eb1d1641SHerbert Xu mdb->secret = old->secret; 324eb1d1641SHerbert Xu 325eb1d1641SHerbert Xu if (!old) 326eb1d1641SHerbert Xu goto out; 327eb1d1641SHerbert Xu 328eb1d1641SHerbert Xu err = br_mdb_copy(mdb, old, elasticity); 329eb1d1641SHerbert Xu if (err) { 330eb1d1641SHerbert Xu kfree(mdb->mhash); 331eb1d1641SHerbert Xu kfree(mdb); 332eb1d1641SHerbert Xu return err; 333eb1d1641SHerbert Xu } 334eb1d1641SHerbert Xu 335eb1d1641SHerbert Xu call_rcu_bh(&mdb->rcu, br_mdb_free); 336eb1d1641SHerbert Xu 337eb1d1641SHerbert Xu out: 338eb1d1641SHerbert Xu rcu_assign_pointer(*mdbp, mdb); 339eb1d1641SHerbert Xu 340eb1d1641SHerbert Xu return 0; 341eb1d1641SHerbert Xu } 342eb1d1641SHerbert Xu 3438ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br, 344eb1d1641SHerbert Xu __be32 group) 345eb1d1641SHerbert Xu { 346eb1d1641SHerbert Xu struct sk_buff *skb; 347eb1d1641SHerbert Xu struct igmphdr *ih; 348eb1d1641SHerbert Xu struct ethhdr *eth; 349eb1d1641SHerbert Xu struct iphdr *iph; 350eb1d1641SHerbert Xu 351eb1d1641SHerbert Xu skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) + 352eb1d1641SHerbert Xu sizeof(*ih) + 4); 353eb1d1641SHerbert Xu if (!skb) 354eb1d1641SHerbert Xu goto out; 355eb1d1641SHerbert Xu 356eb1d1641SHerbert Xu skb->protocol = htons(ETH_P_IP); 357eb1d1641SHerbert Xu 358eb1d1641SHerbert Xu skb_reset_mac_header(skb); 359eb1d1641SHerbert Xu eth = eth_hdr(skb); 360eb1d1641SHerbert Xu 361eb1d1641SHerbert Xu memcpy(eth->h_source, br->dev->dev_addr, 6); 362eb1d1641SHerbert Xu eth->h_dest[0] = 1; 363eb1d1641SHerbert Xu eth->h_dest[1] = 0; 364eb1d1641SHerbert Xu eth->h_dest[2] = 0x5e; 365eb1d1641SHerbert Xu eth->h_dest[3] = 0; 366eb1d1641SHerbert Xu eth->h_dest[4] = 0; 367eb1d1641SHerbert Xu eth->h_dest[5] = 1; 368eb1d1641SHerbert Xu eth->h_proto = htons(ETH_P_IP); 369eb1d1641SHerbert Xu skb_put(skb, sizeof(*eth)); 370eb1d1641SHerbert Xu 371eb1d1641SHerbert Xu skb_set_network_header(skb, skb->len); 372eb1d1641SHerbert Xu iph = ip_hdr(skb); 373eb1d1641SHerbert Xu 374eb1d1641SHerbert Xu iph->version = 4; 375eb1d1641SHerbert Xu iph->ihl = 6; 376eb1d1641SHerbert Xu iph->tos = 0xc0; 377eb1d1641SHerbert Xu iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4); 378eb1d1641SHerbert Xu iph->id = 0; 379eb1d1641SHerbert Xu iph->frag_off = htons(IP_DF); 380eb1d1641SHerbert Xu iph->ttl = 1; 381eb1d1641SHerbert Xu iph->protocol = IPPROTO_IGMP; 382eb1d1641SHerbert Xu iph->saddr = 0; 383eb1d1641SHerbert Xu iph->daddr = htonl(INADDR_ALLHOSTS_GROUP); 384eb1d1641SHerbert Xu ((u8 *)&iph[1])[0] = IPOPT_RA; 385eb1d1641SHerbert Xu ((u8 *)&iph[1])[1] = 4; 386eb1d1641SHerbert Xu ((u8 *)&iph[1])[2] = 0; 387eb1d1641SHerbert Xu ((u8 *)&iph[1])[3] = 0; 388eb1d1641SHerbert Xu ip_send_check(iph); 389eb1d1641SHerbert Xu skb_put(skb, 24); 390eb1d1641SHerbert Xu 391eb1d1641SHerbert Xu skb_set_transport_header(skb, skb->len); 392eb1d1641SHerbert Xu ih = igmp_hdr(skb); 393eb1d1641SHerbert Xu ih->type = IGMP_HOST_MEMBERSHIP_QUERY; 394eb1d1641SHerbert Xu ih->code = (group ? br->multicast_last_member_interval : 395eb1d1641SHerbert Xu br->multicast_query_response_interval) / 396eb1d1641SHerbert Xu (HZ / IGMP_TIMER_SCALE); 397eb1d1641SHerbert Xu ih->group = group; 398eb1d1641SHerbert Xu ih->csum = 0; 399eb1d1641SHerbert Xu ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr)); 400eb1d1641SHerbert Xu skb_put(skb, sizeof(*ih)); 401eb1d1641SHerbert Xu 402eb1d1641SHerbert Xu __skb_pull(skb, sizeof(*eth)); 403eb1d1641SHerbert Xu 404eb1d1641SHerbert Xu out: 405eb1d1641SHerbert Xu return skb; 406eb1d1641SHerbert Xu } 407eb1d1641SHerbert Xu 40808b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 40908b202b6SYOSHIFUJI Hideaki static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br, 41008b202b6SYOSHIFUJI Hideaki struct in6_addr *group) 41108b202b6SYOSHIFUJI Hideaki { 41208b202b6SYOSHIFUJI Hideaki struct sk_buff *skb; 41308b202b6SYOSHIFUJI Hideaki struct ipv6hdr *ip6h; 41408b202b6SYOSHIFUJI Hideaki struct mld_msg *mldq; 41508b202b6SYOSHIFUJI Hideaki struct ethhdr *eth; 41608b202b6SYOSHIFUJI Hideaki u8 *hopopt; 41708b202b6SYOSHIFUJI Hideaki unsigned long interval; 41808b202b6SYOSHIFUJI Hideaki 41908b202b6SYOSHIFUJI Hideaki skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) + 42008b202b6SYOSHIFUJI Hideaki 8 + sizeof(*mldq)); 42108b202b6SYOSHIFUJI Hideaki if (!skb) 42208b202b6SYOSHIFUJI Hideaki goto out; 42308b202b6SYOSHIFUJI Hideaki 42408b202b6SYOSHIFUJI Hideaki skb->protocol = htons(ETH_P_IPV6); 42508b202b6SYOSHIFUJI Hideaki 42608b202b6SYOSHIFUJI Hideaki /* Ethernet header */ 42708b202b6SYOSHIFUJI Hideaki skb_reset_mac_header(skb); 42808b202b6SYOSHIFUJI Hideaki eth = eth_hdr(skb); 42908b202b6SYOSHIFUJI Hideaki 43008b202b6SYOSHIFUJI Hideaki memcpy(eth->h_source, br->dev->dev_addr, 6); 43108b202b6SYOSHIFUJI Hideaki ipv6_eth_mc_map(group, eth->h_dest); 43208b202b6SYOSHIFUJI Hideaki eth->h_proto = htons(ETH_P_IPV6); 43308b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*eth)); 43408b202b6SYOSHIFUJI Hideaki 43508b202b6SYOSHIFUJI Hideaki /* IPv6 header + HbH option */ 43608b202b6SYOSHIFUJI Hideaki skb_set_network_header(skb, skb->len); 43708b202b6SYOSHIFUJI Hideaki ip6h = ipv6_hdr(skb); 43808b202b6SYOSHIFUJI Hideaki 43908b202b6SYOSHIFUJI Hideaki *(__force __be32 *)ip6h = htonl(0x60000000); 44008b202b6SYOSHIFUJI Hideaki ip6h->payload_len = 8 + sizeof(*mldq); 44108b202b6SYOSHIFUJI Hideaki ip6h->nexthdr = IPPROTO_HOPOPTS; 44208b202b6SYOSHIFUJI Hideaki ip6h->hop_limit = 1; 44308b202b6SYOSHIFUJI Hideaki ipv6_addr_set(&ip6h->saddr, 0, 0, 0, 0); 44408b202b6SYOSHIFUJI Hideaki ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1)); 44508b202b6SYOSHIFUJI Hideaki 44608b202b6SYOSHIFUJI Hideaki hopopt = (u8 *)(ip6h + 1); 44708b202b6SYOSHIFUJI Hideaki hopopt[0] = IPPROTO_ICMPV6; /* next hdr */ 44808b202b6SYOSHIFUJI Hideaki hopopt[1] = 0; /* length of HbH */ 44908b202b6SYOSHIFUJI Hideaki hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */ 45008b202b6SYOSHIFUJI Hideaki hopopt[3] = 2; /* Length of RA Option */ 45108b202b6SYOSHIFUJI Hideaki hopopt[4] = 0; /* Type = 0x0000 (MLD) */ 45208b202b6SYOSHIFUJI Hideaki hopopt[5] = 0; 45308b202b6SYOSHIFUJI Hideaki hopopt[6] = IPV6_TLV_PAD0; /* Pad0 */ 45408b202b6SYOSHIFUJI Hideaki hopopt[7] = IPV6_TLV_PAD0; /* Pad0 */ 45508b202b6SYOSHIFUJI Hideaki 45608b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*ip6h) + 8); 45708b202b6SYOSHIFUJI Hideaki 45808b202b6SYOSHIFUJI Hideaki /* ICMPv6 */ 45908b202b6SYOSHIFUJI Hideaki skb_set_transport_header(skb, skb->len); 46008b202b6SYOSHIFUJI Hideaki mldq = (struct mld_msg *) icmp6_hdr(skb); 46108b202b6SYOSHIFUJI Hideaki 46208b202b6SYOSHIFUJI Hideaki interval = ipv6_addr_any(group) ? br->multicast_last_member_interval : 46308b202b6SYOSHIFUJI Hideaki br->multicast_query_response_interval; 46408b202b6SYOSHIFUJI Hideaki 46508b202b6SYOSHIFUJI Hideaki mldq->mld_type = ICMPV6_MGM_QUERY; 46608b202b6SYOSHIFUJI Hideaki mldq->mld_code = 0; 46708b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = 0; 46808b202b6SYOSHIFUJI Hideaki mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval)); 46908b202b6SYOSHIFUJI Hideaki mldq->mld_reserved = 0; 47008b202b6SYOSHIFUJI Hideaki ipv6_addr_copy(&mldq->mld_mca, group); 47108b202b6SYOSHIFUJI Hideaki 47208b202b6SYOSHIFUJI Hideaki /* checksum */ 47308b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, 47408b202b6SYOSHIFUJI Hideaki sizeof(*mldq), IPPROTO_ICMPV6, 47508b202b6SYOSHIFUJI Hideaki csum_partial(mldq, 47608b202b6SYOSHIFUJI Hideaki sizeof(*mldq), 0)); 47708b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*mldq)); 47808b202b6SYOSHIFUJI Hideaki 47908b202b6SYOSHIFUJI Hideaki __skb_pull(skb, sizeof(*eth)); 48008b202b6SYOSHIFUJI Hideaki 48108b202b6SYOSHIFUJI Hideaki out: 48208b202b6SYOSHIFUJI Hideaki return skb; 48308b202b6SYOSHIFUJI Hideaki } 48408b202b6SYOSHIFUJI Hideaki #endif 48508b202b6SYOSHIFUJI Hideaki 4868ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br, 4878ef2a9a5SYOSHIFUJI Hideaki struct br_ip *addr) 4888ef2a9a5SYOSHIFUJI Hideaki { 4898ef2a9a5SYOSHIFUJI Hideaki switch (addr->proto) { 4908ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 4918ef2a9a5SYOSHIFUJI Hideaki return br_ip4_multicast_alloc_query(br, addr->u.ip4); 49208b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 49308b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 49408b202b6SYOSHIFUJI Hideaki return br_ip6_multicast_alloc_query(br, &addr->u.ip6); 49508b202b6SYOSHIFUJI Hideaki #endif 4968ef2a9a5SYOSHIFUJI Hideaki } 4978ef2a9a5SYOSHIFUJI Hideaki return NULL; 4988ef2a9a5SYOSHIFUJI Hideaki } 4998ef2a9a5SYOSHIFUJI Hideaki 500eb1d1641SHerbert Xu static void br_multicast_send_group_query(struct net_bridge_mdb_entry *mp) 501eb1d1641SHerbert Xu { 502eb1d1641SHerbert Xu struct net_bridge *br = mp->br; 503eb1d1641SHerbert Xu struct sk_buff *skb; 504eb1d1641SHerbert Xu 5058ef2a9a5SYOSHIFUJI Hideaki skb = br_multicast_alloc_query(br, &mp->addr); 506eb1d1641SHerbert Xu if (!skb) 507eb1d1641SHerbert Xu goto timer; 508eb1d1641SHerbert Xu 509eb1d1641SHerbert Xu netif_rx(skb); 510eb1d1641SHerbert Xu 511eb1d1641SHerbert Xu timer: 512eb1d1641SHerbert Xu if (++mp->queries_sent < br->multicast_last_member_count) 513eb1d1641SHerbert Xu mod_timer(&mp->query_timer, 514eb1d1641SHerbert Xu jiffies + br->multicast_last_member_interval); 515eb1d1641SHerbert Xu } 516eb1d1641SHerbert Xu 517eb1d1641SHerbert Xu static void br_multicast_group_query_expired(unsigned long data) 518eb1d1641SHerbert Xu { 519eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = (void *)data; 520eb1d1641SHerbert Xu struct net_bridge *br = mp->br; 521eb1d1641SHerbert Xu 522eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 523eb1d1641SHerbert Xu if (!netif_running(br->dev) || hlist_unhashed(&mp->mglist) || 524eb1d1641SHerbert Xu mp->queries_sent >= br->multicast_last_member_count) 525eb1d1641SHerbert Xu goto out; 526eb1d1641SHerbert Xu 527eb1d1641SHerbert Xu br_multicast_send_group_query(mp); 528eb1d1641SHerbert Xu 529eb1d1641SHerbert Xu out: 530eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 531eb1d1641SHerbert Xu } 532eb1d1641SHerbert Xu 533eb1d1641SHerbert Xu static void br_multicast_send_port_group_query(struct net_bridge_port_group *pg) 534eb1d1641SHerbert Xu { 535eb1d1641SHerbert Xu struct net_bridge_port *port = pg->port; 536eb1d1641SHerbert Xu struct net_bridge *br = port->br; 537eb1d1641SHerbert Xu struct sk_buff *skb; 538eb1d1641SHerbert Xu 5398ef2a9a5SYOSHIFUJI Hideaki skb = br_multicast_alloc_query(br, &pg->addr); 540eb1d1641SHerbert Xu if (!skb) 541eb1d1641SHerbert Xu goto timer; 542eb1d1641SHerbert Xu 543eb1d1641SHerbert Xu br_deliver(port, skb); 544eb1d1641SHerbert Xu 545eb1d1641SHerbert Xu timer: 546eb1d1641SHerbert Xu if (++pg->queries_sent < br->multicast_last_member_count) 547eb1d1641SHerbert Xu mod_timer(&pg->query_timer, 548eb1d1641SHerbert Xu jiffies + br->multicast_last_member_interval); 549eb1d1641SHerbert Xu } 550eb1d1641SHerbert Xu 551eb1d1641SHerbert Xu static void br_multicast_port_group_query_expired(unsigned long data) 552eb1d1641SHerbert Xu { 553eb1d1641SHerbert Xu struct net_bridge_port_group *pg = (void *)data; 554eb1d1641SHerbert Xu struct net_bridge_port *port = pg->port; 555eb1d1641SHerbert Xu struct net_bridge *br = port->br; 556eb1d1641SHerbert Xu 557eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 558eb1d1641SHerbert Xu if (!netif_running(br->dev) || hlist_unhashed(&pg->mglist) || 559eb1d1641SHerbert Xu pg->queries_sent >= br->multicast_last_member_count) 560eb1d1641SHerbert Xu goto out; 561eb1d1641SHerbert Xu 562eb1d1641SHerbert Xu br_multicast_send_port_group_query(pg); 563eb1d1641SHerbert Xu 564eb1d1641SHerbert Xu out: 565eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 566eb1d1641SHerbert Xu } 567eb1d1641SHerbert Xu 568eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *br_multicast_get_group( 5698ef2a9a5SYOSHIFUJI Hideaki struct net_bridge *br, struct net_bridge_port *port, 5708ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group, int hash) 571eb1d1641SHerbert Xu { 572eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb = br->mdb; 573eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 574eb1d1641SHerbert Xu struct hlist_node *p; 575eb1d1641SHerbert Xu unsigned count = 0; 576eb1d1641SHerbert Xu unsigned max; 577eb1d1641SHerbert Xu int elasticity; 578eb1d1641SHerbert Xu int err; 579eb1d1641SHerbert Xu 580eb1d1641SHerbert Xu hlist_for_each_entry(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) { 581eb1d1641SHerbert Xu count++; 5828ef2a9a5SYOSHIFUJI Hideaki if (unlikely(br_ip_equal(group, &mp->addr))) 583eb1d1641SHerbert Xu return mp; 584eb1d1641SHerbert Xu } 585eb1d1641SHerbert Xu 586eb1d1641SHerbert Xu elasticity = 0; 587eb1d1641SHerbert Xu max = mdb->max; 588eb1d1641SHerbert Xu 589eb1d1641SHerbert Xu if (unlikely(count > br->hash_elasticity && count)) { 590eb1d1641SHerbert Xu if (net_ratelimit()) 59128a16c97Sstephen hemminger br_info(br, "Multicast hash table " 592eb1d1641SHerbert Xu "chain limit reached: %s\n", 59328a16c97Sstephen hemminger port ? port->dev->name : br->dev->name); 594eb1d1641SHerbert Xu 595eb1d1641SHerbert Xu elasticity = br->hash_elasticity; 596eb1d1641SHerbert Xu } 597eb1d1641SHerbert Xu 598eb1d1641SHerbert Xu if (mdb->size >= max) { 599eb1d1641SHerbert Xu max *= 2; 600eb1d1641SHerbert Xu if (unlikely(max >= br->hash_max)) { 60128a16c97Sstephen hemminger br_warn(br, "Multicast hash table maximum " 602eb1d1641SHerbert Xu "reached, disabling snooping: %s, %d\n", 60328a16c97Sstephen hemminger port ? port->dev->name : br->dev->name, max); 604eb1d1641SHerbert Xu err = -E2BIG; 605eb1d1641SHerbert Xu disable: 606eb1d1641SHerbert Xu br->multicast_disabled = 1; 607eb1d1641SHerbert Xu goto err; 608eb1d1641SHerbert Xu } 609eb1d1641SHerbert Xu } 610eb1d1641SHerbert Xu 611eb1d1641SHerbert Xu if (max > mdb->max || elasticity) { 612eb1d1641SHerbert Xu if (mdb->old) { 613eb1d1641SHerbert Xu if (net_ratelimit()) 61428a16c97Sstephen hemminger br_info(br, "Multicast hash table " 615eb1d1641SHerbert Xu "on fire: %s\n", 61628a16c97Sstephen hemminger port ? port->dev->name : br->dev->name); 617eb1d1641SHerbert Xu err = -EEXIST; 618eb1d1641SHerbert Xu goto err; 619eb1d1641SHerbert Xu } 620eb1d1641SHerbert Xu 621eb1d1641SHerbert Xu err = br_mdb_rehash(&br->mdb, max, elasticity); 622eb1d1641SHerbert Xu if (err) { 62328a16c97Sstephen hemminger br_warn(br, "Cannot rehash multicast " 62428a16c97Sstephen hemminger "hash table, disabling snooping: %s, %d, %d\n", 62528a16c97Sstephen hemminger port ? port->dev->name : br->dev->name, 626eb1d1641SHerbert Xu mdb->size, err); 627eb1d1641SHerbert Xu goto disable; 628eb1d1641SHerbert Xu } 629eb1d1641SHerbert Xu 630eb1d1641SHerbert Xu err = -EAGAIN; 631eb1d1641SHerbert Xu goto err; 632eb1d1641SHerbert Xu } 633eb1d1641SHerbert Xu 634eb1d1641SHerbert Xu return NULL; 635eb1d1641SHerbert Xu 636eb1d1641SHerbert Xu err: 637eb1d1641SHerbert Xu mp = ERR_PTR(err); 638eb1d1641SHerbert Xu return mp; 639eb1d1641SHerbert Xu } 640eb1d1641SHerbert Xu 641eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *br_multicast_new_group( 6428ef2a9a5SYOSHIFUJI Hideaki struct net_bridge *br, struct net_bridge_port *port, 6438ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group) 644eb1d1641SHerbert Xu { 645eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb = br->mdb; 646eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 647eb1d1641SHerbert Xu int hash; 648eb1d1641SHerbert Xu 649eb1d1641SHerbert Xu if (!mdb) { 650eb1d1641SHerbert Xu if (br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0)) 651eb1d1641SHerbert Xu return NULL; 652eb1d1641SHerbert Xu goto rehash; 653eb1d1641SHerbert Xu } 654eb1d1641SHerbert Xu 655eb1d1641SHerbert Xu hash = br_ip_hash(mdb, group); 656eb1d1641SHerbert Xu mp = br_multicast_get_group(br, port, group, hash); 657eb1d1641SHerbert Xu switch (PTR_ERR(mp)) { 658eb1d1641SHerbert Xu case 0: 659eb1d1641SHerbert Xu break; 660eb1d1641SHerbert Xu 661eb1d1641SHerbert Xu case -EAGAIN: 662eb1d1641SHerbert Xu rehash: 663eb1d1641SHerbert Xu mdb = br->mdb; 664eb1d1641SHerbert Xu hash = br_ip_hash(mdb, group); 665eb1d1641SHerbert Xu break; 666eb1d1641SHerbert Xu 667eb1d1641SHerbert Xu default: 668eb1d1641SHerbert Xu goto out; 669eb1d1641SHerbert Xu } 670eb1d1641SHerbert Xu 671eb1d1641SHerbert Xu mp = kzalloc(sizeof(*mp), GFP_ATOMIC); 672eb1d1641SHerbert Xu if (unlikely(!mp)) 673eb1d1641SHerbert Xu goto out; 674eb1d1641SHerbert Xu 675eb1d1641SHerbert Xu mp->br = br; 6768ef2a9a5SYOSHIFUJI Hideaki mp->addr = *group; 677eb1d1641SHerbert Xu setup_timer(&mp->timer, br_multicast_group_expired, 678eb1d1641SHerbert Xu (unsigned long)mp); 679eb1d1641SHerbert Xu setup_timer(&mp->query_timer, br_multicast_group_query_expired, 680eb1d1641SHerbert Xu (unsigned long)mp); 681eb1d1641SHerbert Xu 682eb1d1641SHerbert Xu hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]); 683eb1d1641SHerbert Xu mdb->size++; 684eb1d1641SHerbert Xu 685eb1d1641SHerbert Xu out: 686eb1d1641SHerbert Xu return mp; 687eb1d1641SHerbert Xu } 688eb1d1641SHerbert Xu 689eb1d1641SHerbert Xu static int br_multicast_add_group(struct net_bridge *br, 6908ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 6918ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group) 692eb1d1641SHerbert Xu { 693eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 694eb1d1641SHerbert Xu struct net_bridge_port_group *p; 695eb1d1641SHerbert Xu struct net_bridge_port_group **pp; 696eb1d1641SHerbert Xu unsigned long now = jiffies; 697eb1d1641SHerbert Xu int err; 698eb1d1641SHerbert Xu 699eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 700eb1d1641SHerbert Xu if (!netif_running(br->dev) || 701eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 702eb1d1641SHerbert Xu goto out; 703eb1d1641SHerbert Xu 704eb1d1641SHerbert Xu mp = br_multicast_new_group(br, port, group); 705eb1d1641SHerbert Xu err = PTR_ERR(mp); 706eb1d1641SHerbert Xu if (unlikely(IS_ERR(mp) || !mp)) 707eb1d1641SHerbert Xu goto err; 708eb1d1641SHerbert Xu 709eb1d1641SHerbert Xu if (!port) { 710eb1d1641SHerbert Xu hlist_add_head(&mp->mglist, &br->mglist); 711eb1d1641SHerbert Xu mod_timer(&mp->timer, now + br->multicast_membership_interval); 712eb1d1641SHerbert Xu goto out; 713eb1d1641SHerbert Xu } 714eb1d1641SHerbert Xu 715eb1d1641SHerbert Xu for (pp = &mp->ports; (p = *pp); pp = &p->next) { 716eb1d1641SHerbert Xu if (p->port == port) 717eb1d1641SHerbert Xu goto found; 718eb1d1641SHerbert Xu if ((unsigned long)p->port < (unsigned long)port) 719eb1d1641SHerbert Xu break; 720eb1d1641SHerbert Xu } 721eb1d1641SHerbert Xu 722eb1d1641SHerbert Xu p = kzalloc(sizeof(*p), GFP_ATOMIC); 723eb1d1641SHerbert Xu err = -ENOMEM; 724eb1d1641SHerbert Xu if (unlikely(!p)) 725eb1d1641SHerbert Xu goto err; 726eb1d1641SHerbert Xu 7278ef2a9a5SYOSHIFUJI Hideaki p->addr = *group; 728eb1d1641SHerbert Xu p->port = port; 729eb1d1641SHerbert Xu p->next = *pp; 730eb1d1641SHerbert Xu hlist_add_head(&p->mglist, &port->mglist); 731eb1d1641SHerbert Xu setup_timer(&p->timer, br_multicast_port_group_expired, 732eb1d1641SHerbert Xu (unsigned long)p); 733eb1d1641SHerbert Xu setup_timer(&p->query_timer, br_multicast_port_group_query_expired, 734eb1d1641SHerbert Xu (unsigned long)p); 735eb1d1641SHerbert Xu 736eb1d1641SHerbert Xu rcu_assign_pointer(*pp, p); 737eb1d1641SHerbert Xu 738eb1d1641SHerbert Xu found: 739eb1d1641SHerbert Xu mod_timer(&p->timer, now + br->multicast_membership_interval); 740eb1d1641SHerbert Xu out: 741eb1d1641SHerbert Xu err = 0; 742eb1d1641SHerbert Xu 743eb1d1641SHerbert Xu err: 744eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 745eb1d1641SHerbert Xu return err; 746eb1d1641SHerbert Xu } 747eb1d1641SHerbert Xu 7488ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_add_group(struct net_bridge *br, 7498ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 7508ef2a9a5SYOSHIFUJI Hideaki __be32 group) 7518ef2a9a5SYOSHIFUJI Hideaki { 7528ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 7538ef2a9a5SYOSHIFUJI Hideaki 7548ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 7558ef2a9a5SYOSHIFUJI Hideaki return 0; 7568ef2a9a5SYOSHIFUJI Hideaki 7578ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 7588ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 7598ef2a9a5SYOSHIFUJI Hideaki 7608ef2a9a5SYOSHIFUJI Hideaki return br_multicast_add_group(br, port, &br_group); 7618ef2a9a5SYOSHIFUJI Hideaki } 7628ef2a9a5SYOSHIFUJI Hideaki 76308b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 76408b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_add_group(struct net_bridge *br, 76508b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 76608b202b6SYOSHIFUJI Hideaki const struct in6_addr *group) 76708b202b6SYOSHIFUJI Hideaki { 76808b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 76908b202b6SYOSHIFUJI Hideaki 77008b202b6SYOSHIFUJI Hideaki if (ipv6_is_local_multicast(group)) 77108b202b6SYOSHIFUJI Hideaki return 0; 77208b202b6SYOSHIFUJI Hideaki 77308b202b6SYOSHIFUJI Hideaki ipv6_addr_copy(&br_group.u.ip6, group); 77408b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 77508b202b6SYOSHIFUJI Hideaki 77608b202b6SYOSHIFUJI Hideaki return br_multicast_add_group(br, port, &br_group); 77708b202b6SYOSHIFUJI Hideaki } 77808b202b6SYOSHIFUJI Hideaki #endif 77908b202b6SYOSHIFUJI Hideaki 780eb1d1641SHerbert Xu static void br_multicast_router_expired(unsigned long data) 781eb1d1641SHerbert Xu { 782eb1d1641SHerbert Xu struct net_bridge_port *port = (void *)data; 783eb1d1641SHerbert Xu struct net_bridge *br = port->br; 784eb1d1641SHerbert Xu 785eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 786eb1d1641SHerbert Xu if (port->multicast_router != 1 || 787eb1d1641SHerbert Xu timer_pending(&port->multicast_router_timer) || 788eb1d1641SHerbert Xu hlist_unhashed(&port->rlist)) 789eb1d1641SHerbert Xu goto out; 790eb1d1641SHerbert Xu 791eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 792eb1d1641SHerbert Xu 793eb1d1641SHerbert Xu out: 794eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 795eb1d1641SHerbert Xu } 796eb1d1641SHerbert Xu 797eb1d1641SHerbert Xu static void br_multicast_local_router_expired(unsigned long data) 798eb1d1641SHerbert Xu { 799eb1d1641SHerbert Xu } 800eb1d1641SHerbert Xu 8018ef2a9a5SYOSHIFUJI Hideaki static void __br_multicast_send_query(struct net_bridge *br, 8028ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 8038ef2a9a5SYOSHIFUJI Hideaki struct br_ip *ip) 804eb1d1641SHerbert Xu { 805eb1d1641SHerbert Xu struct sk_buff *skb; 806eb1d1641SHerbert Xu 8078ef2a9a5SYOSHIFUJI Hideaki skb = br_multicast_alloc_query(br, ip); 808eb1d1641SHerbert Xu if (!skb) 8098ef2a9a5SYOSHIFUJI Hideaki return; 810eb1d1641SHerbert Xu 811eb1d1641SHerbert Xu if (port) { 812eb1d1641SHerbert Xu __skb_push(skb, sizeof(struct ethhdr)); 813eb1d1641SHerbert Xu skb->dev = port->dev; 814713aefa3SJan Engelhardt NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev, 815eb1d1641SHerbert Xu dev_queue_xmit); 816eb1d1641SHerbert Xu } else 817eb1d1641SHerbert Xu netif_rx(skb); 8188ef2a9a5SYOSHIFUJI Hideaki } 819eb1d1641SHerbert Xu 8208ef2a9a5SYOSHIFUJI Hideaki static void br_multicast_send_query(struct net_bridge *br, 8218ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, u32 sent) 8228ef2a9a5SYOSHIFUJI Hideaki { 8238ef2a9a5SYOSHIFUJI Hideaki unsigned long time; 8248ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 8258ef2a9a5SYOSHIFUJI Hideaki 8268ef2a9a5SYOSHIFUJI Hideaki if (!netif_running(br->dev) || br->multicast_disabled || 8278ef2a9a5SYOSHIFUJI Hideaki timer_pending(&br->multicast_querier_timer)) 8288ef2a9a5SYOSHIFUJI Hideaki return; 8298ef2a9a5SYOSHIFUJI Hideaki 83008b202b6SYOSHIFUJI Hideaki memset(&br_group.u, 0, sizeof(br_group.u)); 8318ef2a9a5SYOSHIFUJI Hideaki 83208b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 8338ef2a9a5SYOSHIFUJI Hideaki __br_multicast_send_query(br, port, &br_group); 8348ef2a9a5SYOSHIFUJI Hideaki 83508b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 83608b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 83708b202b6SYOSHIFUJI Hideaki __br_multicast_send_query(br, port, &br_group); 83808b202b6SYOSHIFUJI Hideaki #endif 83908b202b6SYOSHIFUJI Hideaki 840eb1d1641SHerbert Xu time = jiffies; 841eb1d1641SHerbert Xu time += sent < br->multicast_startup_query_count ? 842eb1d1641SHerbert Xu br->multicast_startup_query_interval : 843eb1d1641SHerbert Xu br->multicast_query_interval; 844eb1d1641SHerbert Xu mod_timer(port ? &port->multicast_query_timer : 845eb1d1641SHerbert Xu &br->multicast_query_timer, time); 846eb1d1641SHerbert Xu } 847eb1d1641SHerbert Xu 848eb1d1641SHerbert Xu static void br_multicast_port_query_expired(unsigned long data) 849eb1d1641SHerbert Xu { 850eb1d1641SHerbert Xu struct net_bridge_port *port = (void *)data; 851eb1d1641SHerbert Xu struct net_bridge *br = port->br; 852eb1d1641SHerbert Xu 853eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 85402a780c0SDan Carpenter if (port->state == BR_STATE_DISABLED || 85502a780c0SDan Carpenter port->state == BR_STATE_BLOCKING) 856eb1d1641SHerbert Xu goto out; 857eb1d1641SHerbert Xu 858eb1d1641SHerbert Xu if (port->multicast_startup_queries_sent < 859eb1d1641SHerbert Xu br->multicast_startup_query_count) 860eb1d1641SHerbert Xu port->multicast_startup_queries_sent++; 861eb1d1641SHerbert Xu 862eb1d1641SHerbert Xu br_multicast_send_query(port->br, port, 863eb1d1641SHerbert Xu port->multicast_startup_queries_sent); 864eb1d1641SHerbert Xu 865eb1d1641SHerbert Xu out: 866eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 867eb1d1641SHerbert Xu } 868eb1d1641SHerbert Xu 869eb1d1641SHerbert Xu void br_multicast_add_port(struct net_bridge_port *port) 870eb1d1641SHerbert Xu { 871eb1d1641SHerbert Xu port->multicast_router = 1; 872eb1d1641SHerbert Xu 873eb1d1641SHerbert Xu setup_timer(&port->multicast_router_timer, br_multicast_router_expired, 874eb1d1641SHerbert Xu (unsigned long)port); 875eb1d1641SHerbert Xu setup_timer(&port->multicast_query_timer, 876eb1d1641SHerbert Xu br_multicast_port_query_expired, (unsigned long)port); 877eb1d1641SHerbert Xu } 878eb1d1641SHerbert Xu 879eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port) 880eb1d1641SHerbert Xu { 881eb1d1641SHerbert Xu del_timer_sync(&port->multicast_router_timer); 882eb1d1641SHerbert Xu } 883eb1d1641SHerbert Xu 884561f1103SHerbert Xu static void __br_multicast_enable_port(struct net_bridge_port *port) 885561f1103SHerbert Xu { 886561f1103SHerbert Xu port->multicast_startup_queries_sent = 0; 887561f1103SHerbert Xu 888561f1103SHerbert Xu if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 || 889561f1103SHerbert Xu del_timer(&port->multicast_query_timer)) 890561f1103SHerbert Xu mod_timer(&port->multicast_query_timer, jiffies); 891561f1103SHerbert Xu } 892561f1103SHerbert Xu 893eb1d1641SHerbert Xu void br_multicast_enable_port(struct net_bridge_port *port) 894eb1d1641SHerbert Xu { 895eb1d1641SHerbert Xu struct net_bridge *br = port->br; 896eb1d1641SHerbert Xu 897eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 898eb1d1641SHerbert Xu if (br->multicast_disabled || !netif_running(br->dev)) 899eb1d1641SHerbert Xu goto out; 900eb1d1641SHerbert Xu 901561f1103SHerbert Xu __br_multicast_enable_port(port); 902eb1d1641SHerbert Xu 903eb1d1641SHerbert Xu out: 904eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 905eb1d1641SHerbert Xu } 906eb1d1641SHerbert Xu 907eb1d1641SHerbert Xu void br_multicast_disable_port(struct net_bridge_port *port) 908eb1d1641SHerbert Xu { 909eb1d1641SHerbert Xu struct net_bridge *br = port->br; 910eb1d1641SHerbert Xu struct net_bridge_port_group *pg; 911eb1d1641SHerbert Xu struct hlist_node *p, *n; 912eb1d1641SHerbert Xu 913eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 914eb1d1641SHerbert Xu hlist_for_each_entry_safe(pg, p, n, &port->mglist, mglist) 915eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 916eb1d1641SHerbert Xu 917eb1d1641SHerbert Xu if (!hlist_unhashed(&port->rlist)) 918eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 919eb1d1641SHerbert Xu del_timer(&port->multicast_router_timer); 920eb1d1641SHerbert Xu del_timer(&port->multicast_query_timer); 921eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 922eb1d1641SHerbert Xu } 923eb1d1641SHerbert Xu 9248ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_igmp3_report(struct net_bridge *br, 925eb1d1641SHerbert Xu struct net_bridge_port *port, 926eb1d1641SHerbert Xu struct sk_buff *skb) 927eb1d1641SHerbert Xu { 928eb1d1641SHerbert Xu struct igmpv3_report *ih; 929eb1d1641SHerbert Xu struct igmpv3_grec *grec; 930eb1d1641SHerbert Xu int i; 931eb1d1641SHerbert Xu int len; 932eb1d1641SHerbert Xu int num; 933eb1d1641SHerbert Xu int type; 934eb1d1641SHerbert Xu int err = 0; 935eb1d1641SHerbert Xu __be32 group; 936eb1d1641SHerbert Xu 937eb1d1641SHerbert Xu if (!pskb_may_pull(skb, sizeof(*ih))) 938eb1d1641SHerbert Xu return -EINVAL; 939eb1d1641SHerbert Xu 940eb1d1641SHerbert Xu ih = igmpv3_report_hdr(skb); 941eb1d1641SHerbert Xu num = ntohs(ih->ngrec); 942eb1d1641SHerbert Xu len = sizeof(*ih); 943eb1d1641SHerbert Xu 944eb1d1641SHerbert Xu for (i = 0; i < num; i++) { 945eb1d1641SHerbert Xu len += sizeof(*grec); 946eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 947eb1d1641SHerbert Xu return -EINVAL; 948eb1d1641SHerbert Xu 949fd218cf9SHerbert Xu grec = (void *)(skb->data + len - sizeof(*grec)); 950eb1d1641SHerbert Xu group = grec->grec_mca; 951eb1d1641SHerbert Xu type = grec->grec_type; 952eb1d1641SHerbert Xu 9538eabf95cSEric Dumazet len += ntohs(grec->grec_nsrcs) * 4; 954eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 955eb1d1641SHerbert Xu return -EINVAL; 956eb1d1641SHerbert Xu 957eb1d1641SHerbert Xu /* We treat this as an IGMPv2 report for now. */ 958eb1d1641SHerbert Xu switch (type) { 959eb1d1641SHerbert Xu case IGMPV3_MODE_IS_INCLUDE: 960eb1d1641SHerbert Xu case IGMPV3_MODE_IS_EXCLUDE: 961eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_INCLUDE: 962eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_EXCLUDE: 963eb1d1641SHerbert Xu case IGMPV3_ALLOW_NEW_SOURCES: 964eb1d1641SHerbert Xu case IGMPV3_BLOCK_OLD_SOURCES: 965eb1d1641SHerbert Xu break; 966eb1d1641SHerbert Xu 967eb1d1641SHerbert Xu default: 968eb1d1641SHerbert Xu continue; 969eb1d1641SHerbert Xu } 970eb1d1641SHerbert Xu 9718ef2a9a5SYOSHIFUJI Hideaki err = br_ip4_multicast_add_group(br, port, group); 972eb1d1641SHerbert Xu if (err) 973eb1d1641SHerbert Xu break; 974eb1d1641SHerbert Xu } 975eb1d1641SHerbert Xu 976eb1d1641SHerbert Xu return err; 977eb1d1641SHerbert Xu } 978eb1d1641SHerbert Xu 97908b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 98008b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_mld2_report(struct net_bridge *br, 98108b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 98208b202b6SYOSHIFUJI Hideaki struct sk_buff *skb) 98308b202b6SYOSHIFUJI Hideaki { 98408b202b6SYOSHIFUJI Hideaki struct icmp6hdr *icmp6h; 98508b202b6SYOSHIFUJI Hideaki struct mld2_grec *grec; 98608b202b6SYOSHIFUJI Hideaki int i; 98708b202b6SYOSHIFUJI Hideaki int len; 98808b202b6SYOSHIFUJI Hideaki int num; 98908b202b6SYOSHIFUJI Hideaki int err = 0; 99008b202b6SYOSHIFUJI Hideaki 99108b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*icmp6h))) 99208b202b6SYOSHIFUJI Hideaki return -EINVAL; 99308b202b6SYOSHIFUJI Hideaki 99408b202b6SYOSHIFUJI Hideaki icmp6h = icmp6_hdr(skb); 99508b202b6SYOSHIFUJI Hideaki num = ntohs(icmp6h->icmp6_dataun.un_data16[1]); 99608b202b6SYOSHIFUJI Hideaki len = sizeof(*icmp6h); 99708b202b6SYOSHIFUJI Hideaki 99808b202b6SYOSHIFUJI Hideaki for (i = 0; i < num; i++) { 99908b202b6SYOSHIFUJI Hideaki __be16 *nsrcs, _nsrcs; 100008b202b6SYOSHIFUJI Hideaki 100108b202b6SYOSHIFUJI Hideaki nsrcs = skb_header_pointer(skb, 100208b202b6SYOSHIFUJI Hideaki len + offsetof(struct mld2_grec, 100308b202b6SYOSHIFUJI Hideaki grec_mca), 100408b202b6SYOSHIFUJI Hideaki sizeof(_nsrcs), &_nsrcs); 100508b202b6SYOSHIFUJI Hideaki if (!nsrcs) 100608b202b6SYOSHIFUJI Hideaki return -EINVAL; 100708b202b6SYOSHIFUJI Hideaki 100808b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, 100908b202b6SYOSHIFUJI Hideaki len + sizeof(*grec) + 101008b202b6SYOSHIFUJI Hideaki sizeof(struct in6_addr) * (*nsrcs))) 101108b202b6SYOSHIFUJI Hideaki return -EINVAL; 101208b202b6SYOSHIFUJI Hideaki 101308b202b6SYOSHIFUJI Hideaki grec = (struct mld2_grec *)(skb->data + len); 101408b202b6SYOSHIFUJI Hideaki len += sizeof(*grec) + sizeof(struct in6_addr) * (*nsrcs); 101508b202b6SYOSHIFUJI Hideaki 101608b202b6SYOSHIFUJI Hideaki /* We treat these as MLDv1 reports for now. */ 101708b202b6SYOSHIFUJI Hideaki switch (grec->grec_type) { 101808b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_INCLUDE: 101908b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_EXCLUDE: 102008b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_INCLUDE: 102108b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_EXCLUDE: 102208b202b6SYOSHIFUJI Hideaki case MLD2_ALLOW_NEW_SOURCES: 102308b202b6SYOSHIFUJI Hideaki case MLD2_BLOCK_OLD_SOURCES: 102408b202b6SYOSHIFUJI Hideaki break; 102508b202b6SYOSHIFUJI Hideaki 102608b202b6SYOSHIFUJI Hideaki default: 102708b202b6SYOSHIFUJI Hideaki continue; 102808b202b6SYOSHIFUJI Hideaki } 102908b202b6SYOSHIFUJI Hideaki 103008b202b6SYOSHIFUJI Hideaki err = br_ip6_multicast_add_group(br, port, &grec->grec_mca); 103108b202b6SYOSHIFUJI Hideaki if (!err) 103208b202b6SYOSHIFUJI Hideaki break; 103308b202b6SYOSHIFUJI Hideaki } 103408b202b6SYOSHIFUJI Hideaki 103508b202b6SYOSHIFUJI Hideaki return err; 103608b202b6SYOSHIFUJI Hideaki } 103708b202b6SYOSHIFUJI Hideaki #endif 103808b202b6SYOSHIFUJI Hideaki 10397e80c124Sstephen hemminger /* 10407e80c124Sstephen hemminger * Add port to rotuer_list 10417e80c124Sstephen hemminger * list is maintained ordered by pointer value 10427e80c124Sstephen hemminger * and locked by br->multicast_lock and RCU 10437e80c124Sstephen hemminger */ 10440909e117SHerbert Xu static void br_multicast_add_router(struct net_bridge *br, 10450909e117SHerbert Xu struct net_bridge_port *port) 10460909e117SHerbert Xu { 1047dcdca2c4Sstephen hemminger struct net_bridge_port *p; 10487e80c124Sstephen hemminger struct hlist_node *n, *slot = NULL; 10490909e117SHerbert Xu 1050709b9326SDavid S. Miller hlist_for_each_entry(p, n, &br->router_list, rlist) { 10517e80c124Sstephen hemminger if ((unsigned long) port >= (unsigned long) p) 10527e80c124Sstephen hemminger break; 10537e80c124Sstephen hemminger slot = n; 1054dcdca2c4Sstephen hemminger } 10550909e117SHerbert Xu 10567e80c124Sstephen hemminger if (slot) 10577e80c124Sstephen hemminger hlist_add_after_rcu(slot, &port->rlist); 1058dcdca2c4Sstephen hemminger else 1059dcdca2c4Sstephen hemminger hlist_add_head_rcu(&port->rlist, &br->router_list); 10600909e117SHerbert Xu } 10610909e117SHerbert Xu 1062eb1d1641SHerbert Xu static void br_multicast_mark_router(struct net_bridge *br, 1063eb1d1641SHerbert Xu struct net_bridge_port *port) 1064eb1d1641SHerbert Xu { 1065eb1d1641SHerbert Xu unsigned long now = jiffies; 1066eb1d1641SHerbert Xu 1067eb1d1641SHerbert Xu if (!port) { 1068eb1d1641SHerbert Xu if (br->multicast_router == 1) 1069eb1d1641SHerbert Xu mod_timer(&br->multicast_router_timer, 1070eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1071eb1d1641SHerbert Xu return; 1072eb1d1641SHerbert Xu } 1073eb1d1641SHerbert Xu 1074eb1d1641SHerbert Xu if (port->multicast_router != 1) 1075eb1d1641SHerbert Xu return; 1076eb1d1641SHerbert Xu 1077eb1d1641SHerbert Xu if (!hlist_unhashed(&port->rlist)) 1078eb1d1641SHerbert Xu goto timer; 1079eb1d1641SHerbert Xu 10800909e117SHerbert Xu br_multicast_add_router(br, port); 1081eb1d1641SHerbert Xu 1082eb1d1641SHerbert Xu timer: 1083eb1d1641SHerbert Xu mod_timer(&port->multicast_router_timer, 1084eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1085eb1d1641SHerbert Xu } 1086eb1d1641SHerbert Xu 1087eb1d1641SHerbert Xu static void br_multicast_query_received(struct net_bridge *br, 1088eb1d1641SHerbert Xu struct net_bridge_port *port, 10898ef2a9a5SYOSHIFUJI Hideaki int saddr) 1090eb1d1641SHerbert Xu { 1091eb1d1641SHerbert Xu if (saddr) 1092eb1d1641SHerbert Xu mod_timer(&br->multicast_querier_timer, 1093eb1d1641SHerbert Xu jiffies + br->multicast_querier_interval); 1094eb1d1641SHerbert Xu else if (timer_pending(&br->multicast_querier_timer)) 1095eb1d1641SHerbert Xu return; 1096eb1d1641SHerbert Xu 1097eb1d1641SHerbert Xu br_multicast_mark_router(br, port); 1098eb1d1641SHerbert Xu } 1099eb1d1641SHerbert Xu 11008ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_query(struct net_bridge *br, 1101eb1d1641SHerbert Xu struct net_bridge_port *port, 1102eb1d1641SHerbert Xu struct sk_buff *skb) 1103eb1d1641SHerbert Xu { 1104eb1d1641SHerbert Xu struct iphdr *iph = ip_hdr(skb); 1105eb1d1641SHerbert Xu struct igmphdr *ih = igmp_hdr(skb); 1106eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1107eb1d1641SHerbert Xu struct igmpv3_query *ih3; 1108eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1109eb1d1641SHerbert Xu struct net_bridge_port_group **pp; 1110eb1d1641SHerbert Xu unsigned long max_delay; 1111eb1d1641SHerbert Xu unsigned long now = jiffies; 1112eb1d1641SHerbert Xu __be32 group; 1113bec68ff1SYOSHIFUJI Hideaki int err = 0; 1114eb1d1641SHerbert Xu 1115eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1116eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1117eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 1118eb1d1641SHerbert Xu goto out; 1119eb1d1641SHerbert Xu 11208ef2a9a5SYOSHIFUJI Hideaki br_multicast_query_received(br, port, !!iph->saddr); 1121eb1d1641SHerbert Xu 1122eb1d1641SHerbert Xu group = ih->group; 1123eb1d1641SHerbert Xu 1124eb1d1641SHerbert Xu if (skb->len == sizeof(*ih)) { 1125eb1d1641SHerbert Xu max_delay = ih->code * (HZ / IGMP_TIMER_SCALE); 1126eb1d1641SHerbert Xu 1127eb1d1641SHerbert Xu if (!max_delay) { 1128eb1d1641SHerbert Xu max_delay = 10 * HZ; 1129eb1d1641SHerbert Xu group = 0; 1130eb1d1641SHerbert Xu } 1131eb1d1641SHerbert Xu } else { 1132bec68ff1SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) { 1133bec68ff1SYOSHIFUJI Hideaki err = -EINVAL; 1134bec68ff1SYOSHIFUJI Hideaki goto out; 1135bec68ff1SYOSHIFUJI Hideaki } 1136eb1d1641SHerbert Xu 1137eb1d1641SHerbert Xu ih3 = igmpv3_query_hdr(skb); 1138eb1d1641SHerbert Xu if (ih3->nsrcs) 1139bec68ff1SYOSHIFUJI Hideaki goto out; 1140eb1d1641SHerbert Xu 11410ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 max_delay = ih3->code ? 11420ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1; 1143eb1d1641SHerbert Xu } 1144eb1d1641SHerbert Xu 1145eb1d1641SHerbert Xu if (!group) 1146eb1d1641SHerbert Xu goto out; 1147eb1d1641SHerbert Xu 11488ef2a9a5SYOSHIFUJI Hideaki mp = br_mdb_ip4_get(br->mdb, group); 1149eb1d1641SHerbert Xu if (!mp) 1150eb1d1641SHerbert Xu goto out; 1151eb1d1641SHerbert Xu 1152eb1d1641SHerbert Xu max_delay *= br->multicast_last_member_count; 1153eb1d1641SHerbert Xu 1154eb1d1641SHerbert Xu if (!hlist_unhashed(&mp->mglist) && 1155eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1156eb1d1641SHerbert Xu time_after(mp->timer.expires, now + max_delay) : 1157eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) 1158eb1d1641SHerbert Xu mod_timer(&mp->timer, now + max_delay); 1159eb1d1641SHerbert Xu 1160eb1d1641SHerbert Xu for (pp = &mp->ports; (p = *pp); pp = &p->next) { 1161eb1d1641SHerbert Xu if (timer_pending(&p->timer) ? 1162eb1d1641SHerbert Xu time_after(p->timer.expires, now + max_delay) : 1163eb1d1641SHerbert Xu try_to_del_timer_sync(&p->timer) >= 0) 1164eb1d1641SHerbert Xu mod_timer(&mp->timer, now + max_delay); 1165eb1d1641SHerbert Xu } 1166eb1d1641SHerbert Xu 1167eb1d1641SHerbert Xu out: 1168eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1169bec68ff1SYOSHIFUJI Hideaki return err; 1170eb1d1641SHerbert Xu } 1171eb1d1641SHerbert Xu 117208b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 117308b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_query(struct net_bridge *br, 117408b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 117508b202b6SYOSHIFUJI Hideaki struct sk_buff *skb) 117608b202b6SYOSHIFUJI Hideaki { 117708b202b6SYOSHIFUJI Hideaki struct ipv6hdr *ip6h = ipv6_hdr(skb); 117808b202b6SYOSHIFUJI Hideaki struct mld_msg *mld = (struct mld_msg *) icmp6_hdr(skb); 117908b202b6SYOSHIFUJI Hideaki struct net_bridge_mdb_entry *mp; 118008b202b6SYOSHIFUJI Hideaki struct mld2_query *mld2q; 118108b202b6SYOSHIFUJI Hideaki struct net_bridge_port_group *p, **pp; 118208b202b6SYOSHIFUJI Hideaki unsigned long max_delay; 118308b202b6SYOSHIFUJI Hideaki unsigned long now = jiffies; 118408b202b6SYOSHIFUJI Hideaki struct in6_addr *group = NULL; 118508b202b6SYOSHIFUJI Hideaki int err = 0; 118608b202b6SYOSHIFUJI Hideaki 118708b202b6SYOSHIFUJI Hideaki spin_lock(&br->multicast_lock); 118808b202b6SYOSHIFUJI Hideaki if (!netif_running(br->dev) || 118908b202b6SYOSHIFUJI Hideaki (port && port->state == BR_STATE_DISABLED)) 119008b202b6SYOSHIFUJI Hideaki goto out; 119108b202b6SYOSHIFUJI Hideaki 119208b202b6SYOSHIFUJI Hideaki br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->saddr)); 119308b202b6SYOSHIFUJI Hideaki 119408b202b6SYOSHIFUJI Hideaki if (skb->len == sizeof(*mld)) { 119508b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*mld))) { 119608b202b6SYOSHIFUJI Hideaki err = -EINVAL; 119708b202b6SYOSHIFUJI Hideaki goto out; 119808b202b6SYOSHIFUJI Hideaki } 119908b202b6SYOSHIFUJI Hideaki mld = (struct mld_msg *) icmp6_hdr(skb); 120008b202b6SYOSHIFUJI Hideaki max_delay = msecs_to_jiffies(htons(mld->mld_maxdelay)); 120108b202b6SYOSHIFUJI Hideaki if (max_delay) 120208b202b6SYOSHIFUJI Hideaki group = &mld->mld_mca; 120308b202b6SYOSHIFUJI Hideaki } else if (skb->len >= sizeof(*mld2q)) { 120408b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*mld2q))) { 120508b202b6SYOSHIFUJI Hideaki err = -EINVAL; 120608b202b6SYOSHIFUJI Hideaki goto out; 120708b202b6SYOSHIFUJI Hideaki } 120808b202b6SYOSHIFUJI Hideaki mld2q = (struct mld2_query *)icmp6_hdr(skb); 120908b202b6SYOSHIFUJI Hideaki if (!mld2q->mld2q_nsrcs) 121008b202b6SYOSHIFUJI Hideaki group = &mld2q->mld2q_mca; 121108b202b6SYOSHIFUJI Hideaki max_delay = mld2q->mld2q_mrc ? MLDV2_MRC(mld2q->mld2q_mrc) : 1; 121208b202b6SYOSHIFUJI Hideaki } 121308b202b6SYOSHIFUJI Hideaki 121408b202b6SYOSHIFUJI Hideaki if (!group) 121508b202b6SYOSHIFUJI Hideaki goto out; 121608b202b6SYOSHIFUJI Hideaki 121708b202b6SYOSHIFUJI Hideaki mp = br_mdb_ip6_get(br->mdb, group); 121808b202b6SYOSHIFUJI Hideaki if (!mp) 121908b202b6SYOSHIFUJI Hideaki goto out; 122008b202b6SYOSHIFUJI Hideaki 122108b202b6SYOSHIFUJI Hideaki max_delay *= br->multicast_last_member_count; 122208b202b6SYOSHIFUJI Hideaki if (!hlist_unhashed(&mp->mglist) && 122308b202b6SYOSHIFUJI Hideaki (timer_pending(&mp->timer) ? 122408b202b6SYOSHIFUJI Hideaki time_after(mp->timer.expires, now + max_delay) : 122508b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&mp->timer) >= 0)) 122608b202b6SYOSHIFUJI Hideaki mod_timer(&mp->timer, now + max_delay); 122708b202b6SYOSHIFUJI Hideaki 122808b202b6SYOSHIFUJI Hideaki for (pp = &mp->ports; (p = *pp); pp = &p->next) { 122908b202b6SYOSHIFUJI Hideaki if (timer_pending(&p->timer) ? 123008b202b6SYOSHIFUJI Hideaki time_after(p->timer.expires, now + max_delay) : 123108b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&p->timer) >= 0) 123208b202b6SYOSHIFUJI Hideaki mod_timer(&mp->timer, now + max_delay); 123308b202b6SYOSHIFUJI Hideaki } 123408b202b6SYOSHIFUJI Hideaki 123508b202b6SYOSHIFUJI Hideaki out: 123608b202b6SYOSHIFUJI Hideaki spin_unlock(&br->multicast_lock); 123708b202b6SYOSHIFUJI Hideaki return err; 123808b202b6SYOSHIFUJI Hideaki } 123908b202b6SYOSHIFUJI Hideaki #endif 124008b202b6SYOSHIFUJI Hideaki 1241eb1d1641SHerbert Xu static void br_multicast_leave_group(struct net_bridge *br, 1242eb1d1641SHerbert Xu struct net_bridge_port *port, 12438ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group) 1244eb1d1641SHerbert Xu { 1245eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 1246eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1247eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1248eb1d1641SHerbert Xu unsigned long now; 1249eb1d1641SHerbert Xu unsigned long time; 1250eb1d1641SHerbert Xu 1251eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1252eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1253eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED) || 1254eb1d1641SHerbert Xu timer_pending(&br->multicast_querier_timer)) 1255eb1d1641SHerbert Xu goto out; 1256eb1d1641SHerbert Xu 1257eb1d1641SHerbert Xu mdb = br->mdb; 1258eb1d1641SHerbert Xu mp = br_mdb_ip_get(mdb, group); 1259eb1d1641SHerbert Xu if (!mp) 1260eb1d1641SHerbert Xu goto out; 1261eb1d1641SHerbert Xu 1262eb1d1641SHerbert Xu now = jiffies; 1263eb1d1641SHerbert Xu time = now + br->multicast_last_member_count * 1264eb1d1641SHerbert Xu br->multicast_last_member_interval; 1265eb1d1641SHerbert Xu 1266eb1d1641SHerbert Xu if (!port) { 1267eb1d1641SHerbert Xu if (!hlist_unhashed(&mp->mglist) && 1268eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1269eb1d1641SHerbert Xu time_after(mp->timer.expires, time) : 1270eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) { 1271eb1d1641SHerbert Xu mod_timer(&mp->timer, time); 1272eb1d1641SHerbert Xu 1273eb1d1641SHerbert Xu mp->queries_sent = 0; 1274eb1d1641SHerbert Xu mod_timer(&mp->query_timer, now); 1275eb1d1641SHerbert Xu } 1276eb1d1641SHerbert Xu 1277eb1d1641SHerbert Xu goto out; 1278eb1d1641SHerbert Xu } 1279eb1d1641SHerbert Xu 1280eb1d1641SHerbert Xu for (p = mp->ports; p; p = p->next) { 1281eb1d1641SHerbert Xu if (p->port != port) 1282eb1d1641SHerbert Xu continue; 1283eb1d1641SHerbert Xu 1284eb1d1641SHerbert Xu if (!hlist_unhashed(&p->mglist) && 1285eb1d1641SHerbert Xu (timer_pending(&p->timer) ? 1286eb1d1641SHerbert Xu time_after(p->timer.expires, time) : 1287eb1d1641SHerbert Xu try_to_del_timer_sync(&p->timer) >= 0)) { 1288eb1d1641SHerbert Xu mod_timer(&p->timer, time); 1289eb1d1641SHerbert Xu 1290eb1d1641SHerbert Xu p->queries_sent = 0; 1291eb1d1641SHerbert Xu mod_timer(&p->query_timer, now); 1292eb1d1641SHerbert Xu } 1293eb1d1641SHerbert Xu 1294eb1d1641SHerbert Xu break; 1295eb1d1641SHerbert Xu } 1296eb1d1641SHerbert Xu 1297eb1d1641SHerbert Xu out: 1298eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1299eb1d1641SHerbert Xu } 1300eb1d1641SHerbert Xu 13018ef2a9a5SYOSHIFUJI Hideaki static void br_ip4_multicast_leave_group(struct net_bridge *br, 13028ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 13038ef2a9a5SYOSHIFUJI Hideaki __be32 group) 13048ef2a9a5SYOSHIFUJI Hideaki { 13058ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 13068ef2a9a5SYOSHIFUJI Hideaki 13078ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 13088ef2a9a5SYOSHIFUJI Hideaki return; 13098ef2a9a5SYOSHIFUJI Hideaki 13108ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 13118ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 13128ef2a9a5SYOSHIFUJI Hideaki 13138ef2a9a5SYOSHIFUJI Hideaki br_multicast_leave_group(br, port, &br_group); 13148ef2a9a5SYOSHIFUJI Hideaki } 13158ef2a9a5SYOSHIFUJI Hideaki 131608b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 131708b202b6SYOSHIFUJI Hideaki static void br_ip6_multicast_leave_group(struct net_bridge *br, 131808b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 131908b202b6SYOSHIFUJI Hideaki const struct in6_addr *group) 132008b202b6SYOSHIFUJI Hideaki { 132108b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 132208b202b6SYOSHIFUJI Hideaki 132308b202b6SYOSHIFUJI Hideaki if (ipv6_is_local_multicast(group)) 132408b202b6SYOSHIFUJI Hideaki return; 132508b202b6SYOSHIFUJI Hideaki 132608b202b6SYOSHIFUJI Hideaki ipv6_addr_copy(&br_group.u.ip6, group); 132708b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 132808b202b6SYOSHIFUJI Hideaki 132908b202b6SYOSHIFUJI Hideaki br_multicast_leave_group(br, port, &br_group); 133008b202b6SYOSHIFUJI Hideaki } 133108b202b6SYOSHIFUJI Hideaki #endif 13328ef2a9a5SYOSHIFUJI Hideaki 1333eb1d1641SHerbert Xu static int br_multicast_ipv4_rcv(struct net_bridge *br, 1334eb1d1641SHerbert Xu struct net_bridge_port *port, 1335eb1d1641SHerbert Xu struct sk_buff *skb) 1336eb1d1641SHerbert Xu { 1337eb1d1641SHerbert Xu struct sk_buff *skb2 = skb; 1338eb1d1641SHerbert Xu struct iphdr *iph; 1339eb1d1641SHerbert Xu struct igmphdr *ih; 1340eb1d1641SHerbert Xu unsigned len; 1341eb1d1641SHerbert Xu unsigned offset; 1342eb1d1641SHerbert Xu int err; 1343eb1d1641SHerbert Xu 1344eb1d1641SHerbert Xu /* We treat OOM as packet loss for now. */ 1345eb1d1641SHerbert Xu if (!pskb_may_pull(skb, sizeof(*iph))) 1346eb1d1641SHerbert Xu return -EINVAL; 1347eb1d1641SHerbert Xu 1348eb1d1641SHerbert Xu iph = ip_hdr(skb); 1349eb1d1641SHerbert Xu 1350eb1d1641SHerbert Xu if (iph->ihl < 5 || iph->version != 4) 1351eb1d1641SHerbert Xu return -EINVAL; 1352eb1d1641SHerbert Xu 1353eb1d1641SHerbert Xu if (!pskb_may_pull(skb, ip_hdrlen(skb))) 1354eb1d1641SHerbert Xu return -EINVAL; 1355eb1d1641SHerbert Xu 1356eb1d1641SHerbert Xu iph = ip_hdr(skb); 1357eb1d1641SHerbert Xu 1358eb1d1641SHerbert Xu if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl))) 1359eb1d1641SHerbert Xu return -EINVAL; 1360eb1d1641SHerbert Xu 1361eb1d1641SHerbert Xu if (iph->protocol != IPPROTO_IGMP) 1362eb1d1641SHerbert Xu return 0; 1363eb1d1641SHerbert Xu 1364eb1d1641SHerbert Xu len = ntohs(iph->tot_len); 1365eb1d1641SHerbert Xu if (skb->len < len || len < ip_hdrlen(skb)) 1366eb1d1641SHerbert Xu return -EINVAL; 1367eb1d1641SHerbert Xu 1368eb1d1641SHerbert Xu if (skb->len > len) { 1369eb1d1641SHerbert Xu skb2 = skb_clone(skb, GFP_ATOMIC); 1370eb1d1641SHerbert Xu if (!skb2) 1371eb1d1641SHerbert Xu return -ENOMEM; 1372eb1d1641SHerbert Xu 1373eb1d1641SHerbert Xu err = pskb_trim_rcsum(skb2, len); 1374eb1d1641SHerbert Xu if (err) 13758440853bSYOSHIFUJI Hideaki / 吉藤英明 goto err_out; 1376eb1d1641SHerbert Xu } 1377eb1d1641SHerbert Xu 1378eb1d1641SHerbert Xu len -= ip_hdrlen(skb2); 1379eb1d1641SHerbert Xu offset = skb_network_offset(skb2) + ip_hdrlen(skb2); 1380eb1d1641SHerbert Xu __skb_pull(skb2, offset); 1381eb1d1641SHerbert Xu skb_reset_transport_header(skb2); 1382eb1d1641SHerbert Xu 1383eb1d1641SHerbert Xu err = -EINVAL; 1384eb1d1641SHerbert Xu if (!pskb_may_pull(skb2, sizeof(*ih))) 1385eb1d1641SHerbert Xu goto out; 1386eb1d1641SHerbert Xu 1387eb1d1641SHerbert Xu switch (skb2->ip_summed) { 1388eb1d1641SHerbert Xu case CHECKSUM_COMPLETE: 1389eb1d1641SHerbert Xu if (!csum_fold(skb2->csum)) 1390eb1d1641SHerbert Xu break; 1391eb1d1641SHerbert Xu /* fall through */ 1392eb1d1641SHerbert Xu case CHECKSUM_NONE: 1393eb1d1641SHerbert Xu skb2->csum = 0; 1394eb1d1641SHerbert Xu if (skb_checksum_complete(skb2)) 13958440853bSYOSHIFUJI Hideaki / 吉藤英明 goto out; 1396eb1d1641SHerbert Xu } 1397eb1d1641SHerbert Xu 1398eb1d1641SHerbert Xu err = 0; 1399eb1d1641SHerbert Xu 1400eb1d1641SHerbert Xu BR_INPUT_SKB_CB(skb)->igmp = 1; 1401eb1d1641SHerbert Xu ih = igmp_hdr(skb2); 1402eb1d1641SHerbert Xu 1403eb1d1641SHerbert Xu switch (ih->type) { 1404eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_REPORT: 1405eb1d1641SHerbert Xu case IGMPV2_HOST_MEMBERSHIP_REPORT: 1406eb1d1641SHerbert Xu BR_INPUT_SKB_CB(skb2)->mrouters_only = 1; 14078ef2a9a5SYOSHIFUJI Hideaki err = br_ip4_multicast_add_group(br, port, ih->group); 1408eb1d1641SHerbert Xu break; 1409eb1d1641SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 14108ef2a9a5SYOSHIFUJI Hideaki err = br_ip4_multicast_igmp3_report(br, port, skb2); 1411eb1d1641SHerbert Xu break; 1412eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_QUERY: 14138ef2a9a5SYOSHIFUJI Hideaki err = br_ip4_multicast_query(br, port, skb2); 1414eb1d1641SHerbert Xu break; 1415eb1d1641SHerbert Xu case IGMP_HOST_LEAVE_MESSAGE: 14168ef2a9a5SYOSHIFUJI Hideaki br_ip4_multicast_leave_group(br, port, ih->group); 1417eb1d1641SHerbert Xu break; 1418eb1d1641SHerbert Xu } 1419eb1d1641SHerbert Xu 1420eb1d1641SHerbert Xu out: 1421eb1d1641SHerbert Xu __skb_push(skb2, offset); 14228440853bSYOSHIFUJI Hideaki / 吉藤英明 err_out: 1423eb1d1641SHerbert Xu if (skb2 != skb) 1424eb1d1641SHerbert Xu kfree_skb(skb2); 1425eb1d1641SHerbert Xu return err; 1426eb1d1641SHerbert Xu } 1427eb1d1641SHerbert Xu 142808b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 142908b202b6SYOSHIFUJI Hideaki static int br_multicast_ipv6_rcv(struct net_bridge *br, 143008b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 143108b202b6SYOSHIFUJI Hideaki struct sk_buff *skb) 143208b202b6SYOSHIFUJI Hideaki { 143308b202b6SYOSHIFUJI Hideaki struct sk_buff *skb2 = skb; 143408b202b6SYOSHIFUJI Hideaki struct ipv6hdr *ip6h; 143508b202b6SYOSHIFUJI Hideaki struct icmp6hdr *icmp6h; 143608b202b6SYOSHIFUJI Hideaki u8 nexthdr; 143708b202b6SYOSHIFUJI Hideaki unsigned len; 143808b202b6SYOSHIFUJI Hideaki unsigned offset; 143908b202b6SYOSHIFUJI Hideaki int err; 144008b202b6SYOSHIFUJI Hideaki 144108b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*ip6h))) 144208b202b6SYOSHIFUJI Hideaki return -EINVAL; 144308b202b6SYOSHIFUJI Hideaki 144408b202b6SYOSHIFUJI Hideaki ip6h = ipv6_hdr(skb); 144508b202b6SYOSHIFUJI Hideaki 144608b202b6SYOSHIFUJI Hideaki /* 144708b202b6SYOSHIFUJI Hideaki * We're interested in MLD messages only. 144808b202b6SYOSHIFUJI Hideaki * - Version is 6 144908b202b6SYOSHIFUJI Hideaki * - MLD has always Router Alert hop-by-hop option 145008b202b6SYOSHIFUJI Hideaki * - But we do not support jumbrograms. 145108b202b6SYOSHIFUJI Hideaki */ 145208b202b6SYOSHIFUJI Hideaki if (ip6h->version != 6 || 145308b202b6SYOSHIFUJI Hideaki ip6h->nexthdr != IPPROTO_HOPOPTS || 145408b202b6SYOSHIFUJI Hideaki ip6h->payload_len == 0) 145508b202b6SYOSHIFUJI Hideaki return 0; 145608b202b6SYOSHIFUJI Hideaki 145708b202b6SYOSHIFUJI Hideaki len = ntohs(ip6h->payload_len); 145808b202b6SYOSHIFUJI Hideaki if (skb->len < len) 145908b202b6SYOSHIFUJI Hideaki return -EINVAL; 146008b202b6SYOSHIFUJI Hideaki 146108b202b6SYOSHIFUJI Hideaki nexthdr = ip6h->nexthdr; 146208b202b6SYOSHIFUJI Hideaki offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr); 146308b202b6SYOSHIFUJI Hideaki 146408b202b6SYOSHIFUJI Hideaki if (offset < 0 || nexthdr != IPPROTO_ICMPV6) 146508b202b6SYOSHIFUJI Hideaki return 0; 146608b202b6SYOSHIFUJI Hideaki 146708b202b6SYOSHIFUJI Hideaki /* Okay, we found ICMPv6 header */ 146808b202b6SYOSHIFUJI Hideaki skb2 = skb_clone(skb, GFP_ATOMIC); 146908b202b6SYOSHIFUJI Hideaki if (!skb2) 147008b202b6SYOSHIFUJI Hideaki return -ENOMEM; 147108b202b6SYOSHIFUJI Hideaki 147208b202b6SYOSHIFUJI Hideaki len -= offset - skb_network_offset(skb2); 147308b202b6SYOSHIFUJI Hideaki 147408b202b6SYOSHIFUJI Hideaki __skb_pull(skb2, offset); 147508b202b6SYOSHIFUJI Hideaki skb_reset_transport_header(skb2); 147608b202b6SYOSHIFUJI Hideaki 147708b202b6SYOSHIFUJI Hideaki err = -EINVAL; 147808b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb2, sizeof(*icmp6h))) 147908b202b6SYOSHIFUJI Hideaki goto out; 148008b202b6SYOSHIFUJI Hideaki 148108b202b6SYOSHIFUJI Hideaki icmp6h = icmp6_hdr(skb2); 148208b202b6SYOSHIFUJI Hideaki 148308b202b6SYOSHIFUJI Hideaki switch (icmp6h->icmp6_type) { 148408b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 148508b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 148608b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 148708b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 148808b202b6SYOSHIFUJI Hideaki break; 148908b202b6SYOSHIFUJI Hideaki default: 149008b202b6SYOSHIFUJI Hideaki err = 0; 149108b202b6SYOSHIFUJI Hideaki goto out; 149208b202b6SYOSHIFUJI Hideaki } 149308b202b6SYOSHIFUJI Hideaki 149408b202b6SYOSHIFUJI Hideaki /* Okay, we found MLD message. Check further. */ 149508b202b6SYOSHIFUJI Hideaki if (skb2->len > len) { 149608b202b6SYOSHIFUJI Hideaki err = pskb_trim_rcsum(skb2, len); 149708b202b6SYOSHIFUJI Hideaki if (err) 149808b202b6SYOSHIFUJI Hideaki goto out; 149908b202b6SYOSHIFUJI Hideaki } 150008b202b6SYOSHIFUJI Hideaki 150108b202b6SYOSHIFUJI Hideaki switch (skb2->ip_summed) { 150208b202b6SYOSHIFUJI Hideaki case CHECKSUM_COMPLETE: 150308b202b6SYOSHIFUJI Hideaki if (!csum_fold(skb2->csum)) 150408b202b6SYOSHIFUJI Hideaki break; 150508b202b6SYOSHIFUJI Hideaki /*FALLTHROUGH*/ 150608b202b6SYOSHIFUJI Hideaki case CHECKSUM_NONE: 150708b202b6SYOSHIFUJI Hideaki skb2->csum = 0; 150808b202b6SYOSHIFUJI Hideaki if (skb_checksum_complete(skb2)) 150908b202b6SYOSHIFUJI Hideaki goto out; 151008b202b6SYOSHIFUJI Hideaki } 151108b202b6SYOSHIFUJI Hideaki 151208b202b6SYOSHIFUJI Hideaki err = 0; 151308b202b6SYOSHIFUJI Hideaki 151408b202b6SYOSHIFUJI Hideaki BR_INPUT_SKB_CB(skb)->igmp = 1; 151508b202b6SYOSHIFUJI Hideaki 151608b202b6SYOSHIFUJI Hideaki switch (icmp6h->icmp6_type) { 151708b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 151808b202b6SYOSHIFUJI Hideaki { 151908b202b6SYOSHIFUJI Hideaki struct mld_msg *mld = (struct mld_msg *)icmp6h; 152008b202b6SYOSHIFUJI Hideaki BR_INPUT_SKB_CB(skb2)->mrouters_only = 1; 152108b202b6SYOSHIFUJI Hideaki err = br_ip6_multicast_add_group(br, port, &mld->mld_mca); 152208b202b6SYOSHIFUJI Hideaki break; 152308b202b6SYOSHIFUJI Hideaki } 152408b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 152508b202b6SYOSHIFUJI Hideaki err = br_ip6_multicast_mld2_report(br, port, skb2); 152608b202b6SYOSHIFUJI Hideaki break; 152708b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 152808b202b6SYOSHIFUJI Hideaki err = br_ip6_multicast_query(br, port, skb2); 152908b202b6SYOSHIFUJI Hideaki break; 153008b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 153108b202b6SYOSHIFUJI Hideaki { 153208b202b6SYOSHIFUJI Hideaki struct mld_msg *mld = (struct mld_msg *)icmp6h; 153308b202b6SYOSHIFUJI Hideaki br_ip6_multicast_leave_group(br, port, &mld->mld_mca); 153408b202b6SYOSHIFUJI Hideaki } 153508b202b6SYOSHIFUJI Hideaki } 153608b202b6SYOSHIFUJI Hideaki 153708b202b6SYOSHIFUJI Hideaki out: 153808b202b6SYOSHIFUJI Hideaki __skb_push(skb2, offset); 153908b202b6SYOSHIFUJI Hideaki if (skb2 != skb) 154008b202b6SYOSHIFUJI Hideaki kfree_skb(skb2); 154108b202b6SYOSHIFUJI Hideaki return err; 154208b202b6SYOSHIFUJI Hideaki } 154308b202b6SYOSHIFUJI Hideaki #endif 154408b202b6SYOSHIFUJI Hideaki 1545eb1d1641SHerbert Xu int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port, 1546eb1d1641SHerbert Xu struct sk_buff *skb) 1547eb1d1641SHerbert Xu { 15481fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->igmp = 0; 15491fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->mrouters_only = 0; 15501fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 1551eb1d1641SHerbert Xu if (br->multicast_disabled) 1552eb1d1641SHerbert Xu return 0; 1553eb1d1641SHerbert Xu 1554eb1d1641SHerbert Xu switch (skb->protocol) { 1555eb1d1641SHerbert Xu case htons(ETH_P_IP): 1556eb1d1641SHerbert Xu return br_multicast_ipv4_rcv(br, port, skb); 155708b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 155808b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 155908b202b6SYOSHIFUJI Hideaki return br_multicast_ipv6_rcv(br, port, skb); 156008b202b6SYOSHIFUJI Hideaki #endif 1561eb1d1641SHerbert Xu } 1562eb1d1641SHerbert Xu 1563eb1d1641SHerbert Xu return 0; 1564eb1d1641SHerbert Xu } 1565eb1d1641SHerbert Xu 1566eb1d1641SHerbert Xu static void br_multicast_query_expired(unsigned long data) 1567eb1d1641SHerbert Xu { 1568eb1d1641SHerbert Xu struct net_bridge *br = (void *)data; 1569eb1d1641SHerbert Xu 1570eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1571eb1d1641SHerbert Xu if (br->multicast_startup_queries_sent < 1572eb1d1641SHerbert Xu br->multicast_startup_query_count) 1573eb1d1641SHerbert Xu br->multicast_startup_queries_sent++; 1574eb1d1641SHerbert Xu 1575eb1d1641SHerbert Xu br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent); 1576eb1d1641SHerbert Xu 1577eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1578eb1d1641SHerbert Xu } 1579eb1d1641SHerbert Xu 1580eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br) 1581eb1d1641SHerbert Xu { 1582eb1d1641SHerbert Xu br->hash_elasticity = 4; 1583eb1d1641SHerbert Xu br->hash_max = 512; 1584eb1d1641SHerbert Xu 1585eb1d1641SHerbert Xu br->multicast_router = 1; 1586eb1d1641SHerbert Xu br->multicast_last_member_count = 2; 1587eb1d1641SHerbert Xu br->multicast_startup_query_count = 2; 1588eb1d1641SHerbert Xu 1589eb1d1641SHerbert Xu br->multicast_last_member_interval = HZ; 1590eb1d1641SHerbert Xu br->multicast_query_response_interval = 10 * HZ; 1591eb1d1641SHerbert Xu br->multicast_startup_query_interval = 125 * HZ / 4; 1592eb1d1641SHerbert Xu br->multicast_query_interval = 125 * HZ; 1593eb1d1641SHerbert Xu br->multicast_querier_interval = 255 * HZ; 1594eb1d1641SHerbert Xu br->multicast_membership_interval = 260 * HZ; 1595eb1d1641SHerbert Xu 1596eb1d1641SHerbert Xu spin_lock_init(&br->multicast_lock); 1597eb1d1641SHerbert Xu setup_timer(&br->multicast_router_timer, 1598eb1d1641SHerbert Xu br_multicast_local_router_expired, 0); 1599eb1d1641SHerbert Xu setup_timer(&br->multicast_querier_timer, 1600eb1d1641SHerbert Xu br_multicast_local_router_expired, 0); 1601eb1d1641SHerbert Xu setup_timer(&br->multicast_query_timer, br_multicast_query_expired, 1602eb1d1641SHerbert Xu (unsigned long)br); 1603eb1d1641SHerbert Xu } 1604eb1d1641SHerbert Xu 1605eb1d1641SHerbert Xu void br_multicast_open(struct net_bridge *br) 1606eb1d1641SHerbert Xu { 1607eb1d1641SHerbert Xu br->multicast_startup_queries_sent = 0; 1608eb1d1641SHerbert Xu 1609eb1d1641SHerbert Xu if (br->multicast_disabled) 1610eb1d1641SHerbert Xu return; 1611eb1d1641SHerbert Xu 1612eb1d1641SHerbert Xu mod_timer(&br->multicast_query_timer, jiffies); 1613eb1d1641SHerbert Xu } 1614eb1d1641SHerbert Xu 1615eb1d1641SHerbert Xu void br_multicast_stop(struct net_bridge *br) 1616eb1d1641SHerbert Xu { 1617eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 1618eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1619eb1d1641SHerbert Xu struct hlist_node *p, *n; 1620eb1d1641SHerbert Xu u32 ver; 1621eb1d1641SHerbert Xu int i; 1622eb1d1641SHerbert Xu 1623eb1d1641SHerbert Xu del_timer_sync(&br->multicast_router_timer); 1624eb1d1641SHerbert Xu del_timer_sync(&br->multicast_querier_timer); 1625eb1d1641SHerbert Xu del_timer_sync(&br->multicast_query_timer); 1626eb1d1641SHerbert Xu 1627eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1628eb1d1641SHerbert Xu mdb = br->mdb; 1629eb1d1641SHerbert Xu if (!mdb) 1630eb1d1641SHerbert Xu goto out; 1631eb1d1641SHerbert Xu 1632eb1d1641SHerbert Xu br->mdb = NULL; 1633eb1d1641SHerbert Xu 1634eb1d1641SHerbert Xu ver = mdb->ver; 1635eb1d1641SHerbert Xu for (i = 0; i < mdb->max; i++) { 1636eb1d1641SHerbert Xu hlist_for_each_entry_safe(mp, p, n, &mdb->mhash[i], 1637eb1d1641SHerbert Xu hlist[ver]) { 1638eb1d1641SHerbert Xu del_timer(&mp->timer); 1639eb1d1641SHerbert Xu del_timer(&mp->query_timer); 1640eb1d1641SHerbert Xu call_rcu_bh(&mp->rcu, br_multicast_free_group); 1641eb1d1641SHerbert Xu } 1642eb1d1641SHerbert Xu } 1643eb1d1641SHerbert Xu 1644eb1d1641SHerbert Xu if (mdb->old) { 1645eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 164610cc2b50SHerbert Xu rcu_barrier_bh(); 1647eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1648eb1d1641SHerbert Xu WARN_ON(mdb->old); 1649eb1d1641SHerbert Xu } 1650eb1d1641SHerbert Xu 1651eb1d1641SHerbert Xu mdb->old = mdb; 1652eb1d1641SHerbert Xu call_rcu_bh(&mdb->rcu, br_mdb_free); 1653eb1d1641SHerbert Xu 1654eb1d1641SHerbert Xu out: 1655eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 1656eb1d1641SHerbert Xu } 16570909e117SHerbert Xu 16580909e117SHerbert Xu int br_multicast_set_router(struct net_bridge *br, unsigned long val) 16590909e117SHerbert Xu { 16600909e117SHerbert Xu int err = -ENOENT; 16610909e117SHerbert Xu 16620909e117SHerbert Xu spin_lock_bh(&br->multicast_lock); 16630909e117SHerbert Xu if (!netif_running(br->dev)) 16640909e117SHerbert Xu goto unlock; 16650909e117SHerbert Xu 16660909e117SHerbert Xu switch (val) { 16670909e117SHerbert Xu case 0: 16680909e117SHerbert Xu case 2: 16690909e117SHerbert Xu del_timer(&br->multicast_router_timer); 16700909e117SHerbert Xu /* fall through */ 16710909e117SHerbert Xu case 1: 16720909e117SHerbert Xu br->multicast_router = val; 16730909e117SHerbert Xu err = 0; 16740909e117SHerbert Xu break; 16750909e117SHerbert Xu 16760909e117SHerbert Xu default: 16770909e117SHerbert Xu err = -EINVAL; 16780909e117SHerbert Xu break; 16790909e117SHerbert Xu } 16800909e117SHerbert Xu 16810909e117SHerbert Xu unlock: 16820909e117SHerbert Xu spin_unlock_bh(&br->multicast_lock); 16830909e117SHerbert Xu 16840909e117SHerbert Xu return err; 16850909e117SHerbert Xu } 16860909e117SHerbert Xu 16870909e117SHerbert Xu int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val) 16880909e117SHerbert Xu { 16890909e117SHerbert Xu struct net_bridge *br = p->br; 16900909e117SHerbert Xu int err = -ENOENT; 16910909e117SHerbert Xu 16920909e117SHerbert Xu spin_lock(&br->multicast_lock); 16930909e117SHerbert Xu if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED) 16940909e117SHerbert Xu goto unlock; 16950909e117SHerbert Xu 16960909e117SHerbert Xu switch (val) { 16970909e117SHerbert Xu case 0: 16980909e117SHerbert Xu case 1: 16990909e117SHerbert Xu case 2: 17000909e117SHerbert Xu p->multicast_router = val; 17010909e117SHerbert Xu err = 0; 17020909e117SHerbert Xu 17030909e117SHerbert Xu if (val < 2 && !hlist_unhashed(&p->rlist)) 17040909e117SHerbert Xu hlist_del_init_rcu(&p->rlist); 17050909e117SHerbert Xu 17060909e117SHerbert Xu if (val == 1) 17070909e117SHerbert Xu break; 17080909e117SHerbert Xu 17090909e117SHerbert Xu del_timer(&p->multicast_router_timer); 17100909e117SHerbert Xu 17110909e117SHerbert Xu if (val == 0) 17120909e117SHerbert Xu break; 17130909e117SHerbert Xu 17140909e117SHerbert Xu br_multicast_add_router(br, p); 17150909e117SHerbert Xu break; 17160909e117SHerbert Xu 17170909e117SHerbert Xu default: 17180909e117SHerbert Xu err = -EINVAL; 17190909e117SHerbert Xu break; 17200909e117SHerbert Xu } 17210909e117SHerbert Xu 17220909e117SHerbert Xu unlock: 17230909e117SHerbert Xu spin_unlock(&br->multicast_lock); 17240909e117SHerbert Xu 17250909e117SHerbert Xu return err; 17260909e117SHerbert Xu } 1727561f1103SHerbert Xu 1728561f1103SHerbert Xu int br_multicast_toggle(struct net_bridge *br, unsigned long val) 1729561f1103SHerbert Xu { 1730561f1103SHerbert Xu struct net_bridge_port *port; 1731561f1103SHerbert Xu int err = -ENOENT; 1732561f1103SHerbert Xu 1733561f1103SHerbert Xu spin_lock(&br->multicast_lock); 1734561f1103SHerbert Xu if (!netif_running(br->dev)) 1735561f1103SHerbert Xu goto unlock; 1736561f1103SHerbert Xu 1737561f1103SHerbert Xu err = 0; 1738561f1103SHerbert Xu if (br->multicast_disabled == !val) 1739561f1103SHerbert Xu goto unlock; 1740561f1103SHerbert Xu 1741561f1103SHerbert Xu br->multicast_disabled = !val; 1742561f1103SHerbert Xu if (br->multicast_disabled) 1743561f1103SHerbert Xu goto unlock; 1744561f1103SHerbert Xu 1745561f1103SHerbert Xu if (br->mdb) { 1746561f1103SHerbert Xu if (br->mdb->old) { 1747561f1103SHerbert Xu err = -EEXIST; 1748561f1103SHerbert Xu rollback: 1749561f1103SHerbert Xu br->multicast_disabled = !!val; 1750561f1103SHerbert Xu goto unlock; 1751561f1103SHerbert Xu } 1752561f1103SHerbert Xu 1753561f1103SHerbert Xu err = br_mdb_rehash(&br->mdb, br->mdb->max, 1754561f1103SHerbert Xu br->hash_elasticity); 1755561f1103SHerbert Xu if (err) 1756561f1103SHerbert Xu goto rollback; 1757561f1103SHerbert Xu } 1758561f1103SHerbert Xu 1759561f1103SHerbert Xu br_multicast_open(br); 1760561f1103SHerbert Xu list_for_each_entry(port, &br->port_list, list) { 1761561f1103SHerbert Xu if (port->state == BR_STATE_DISABLED || 1762561f1103SHerbert Xu port->state == BR_STATE_BLOCKING) 1763561f1103SHerbert Xu continue; 1764561f1103SHerbert Xu 1765561f1103SHerbert Xu __br_multicast_enable_port(port); 1766561f1103SHerbert Xu } 1767561f1103SHerbert Xu 1768561f1103SHerbert Xu unlock: 1769561f1103SHerbert Xu spin_unlock(&br->multicast_lock); 1770561f1103SHerbert Xu 1771561f1103SHerbert Xu return err; 1772561f1103SHerbert Xu } 1773b195167fSHerbert Xu 1774b195167fSHerbert Xu int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val) 1775b195167fSHerbert Xu { 1776b195167fSHerbert Xu int err = -ENOENT; 1777b195167fSHerbert Xu u32 old; 1778b195167fSHerbert Xu 1779b195167fSHerbert Xu spin_lock(&br->multicast_lock); 1780b195167fSHerbert Xu if (!netif_running(br->dev)) 1781b195167fSHerbert Xu goto unlock; 1782b195167fSHerbert Xu 1783b195167fSHerbert Xu err = -EINVAL; 1784b195167fSHerbert Xu if (!is_power_of_2(val)) 1785b195167fSHerbert Xu goto unlock; 1786b195167fSHerbert Xu if (br->mdb && val < br->mdb->size) 1787b195167fSHerbert Xu goto unlock; 1788b195167fSHerbert Xu 1789b195167fSHerbert Xu err = 0; 1790b195167fSHerbert Xu 1791b195167fSHerbert Xu old = br->hash_max; 1792b195167fSHerbert Xu br->hash_max = val; 1793b195167fSHerbert Xu 1794b195167fSHerbert Xu if (br->mdb) { 1795b195167fSHerbert Xu if (br->mdb->old) { 1796b195167fSHerbert Xu err = -EEXIST; 1797b195167fSHerbert Xu rollback: 1798b195167fSHerbert Xu br->hash_max = old; 1799b195167fSHerbert Xu goto unlock; 1800b195167fSHerbert Xu } 1801b195167fSHerbert Xu 1802b195167fSHerbert Xu err = br_mdb_rehash(&br->mdb, br->hash_max, 1803b195167fSHerbert Xu br->hash_elasticity); 1804b195167fSHerbert Xu if (err) 1805b195167fSHerbert Xu goto rollback; 1806b195167fSHerbert Xu } 1807b195167fSHerbert Xu 1808b195167fSHerbert Xu unlock: 1809b195167fSHerbert Xu spin_unlock(&br->multicast_lock); 1810b195167fSHerbert Xu 1811b195167fSHerbert Xu return err; 1812b195167fSHerbert Xu } 1813