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> 3108b202b6SYOSHIFUJI Hideaki #endif 32eb1d1641SHerbert Xu 33eb1d1641SHerbert Xu #include "br_private.h" 34eb1d1641SHerbert Xu 3508b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 3608b202b6SYOSHIFUJI Hideaki static inline int ipv6_is_local_multicast(const struct in6_addr *addr) 3708b202b6SYOSHIFUJI Hideaki { 3808b202b6SYOSHIFUJI Hideaki if (ipv6_addr_is_multicast(addr) && 3908b202b6SYOSHIFUJI Hideaki IPV6_ADDR_MC_SCOPE(addr) <= IPV6_ADDR_SCOPE_LINKLOCAL) 4008b202b6SYOSHIFUJI Hideaki return 1; 4108b202b6SYOSHIFUJI Hideaki return 0; 4208b202b6SYOSHIFUJI Hideaki } 4308b202b6SYOSHIFUJI Hideaki #endif 4408b202b6SYOSHIFUJI Hideaki 458ef2a9a5SYOSHIFUJI Hideaki static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b) 468ef2a9a5SYOSHIFUJI Hideaki { 478ef2a9a5SYOSHIFUJI Hideaki if (a->proto != b->proto) 488ef2a9a5SYOSHIFUJI Hideaki return 0; 498ef2a9a5SYOSHIFUJI Hideaki switch (a->proto) { 508ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 518ef2a9a5SYOSHIFUJI Hideaki return a->u.ip4 == b->u.ip4; 5208b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 5308b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 5408b202b6SYOSHIFUJI Hideaki return ipv6_addr_equal(&a->u.ip6, &b->u.ip6); 5508b202b6SYOSHIFUJI Hideaki #endif 568ef2a9a5SYOSHIFUJI Hideaki } 578ef2a9a5SYOSHIFUJI Hideaki return 0; 588ef2a9a5SYOSHIFUJI Hideaki } 598ef2a9a5SYOSHIFUJI Hideaki 608ef2a9a5SYOSHIFUJI Hideaki static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip) 61eb1d1641SHerbert Xu { 620eae88f3SEric Dumazet return jhash_1word(mdb->secret, (__force u32)ip) & (mdb->max - 1); 63eb1d1641SHerbert Xu } 64eb1d1641SHerbert Xu 6508b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 6608b202b6SYOSHIFUJI Hideaki static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb, 6708b202b6SYOSHIFUJI Hideaki const struct in6_addr *ip) 6808b202b6SYOSHIFUJI Hideaki { 6908b202b6SYOSHIFUJI Hideaki return jhash2((__force u32 *)ip->s6_addr32, 4, mdb->secret) & (mdb->max - 1); 7008b202b6SYOSHIFUJI Hideaki } 7108b202b6SYOSHIFUJI Hideaki #endif 7208b202b6SYOSHIFUJI Hideaki 738ef2a9a5SYOSHIFUJI Hideaki static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb, 748ef2a9a5SYOSHIFUJI Hideaki struct br_ip *ip) 758ef2a9a5SYOSHIFUJI Hideaki { 768ef2a9a5SYOSHIFUJI Hideaki switch (ip->proto) { 778ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 788ef2a9a5SYOSHIFUJI Hideaki return __br_ip4_hash(mdb, ip->u.ip4); 7908b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 8008b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 8108b202b6SYOSHIFUJI Hideaki return __br_ip6_hash(mdb, &ip->u.ip6); 8208b202b6SYOSHIFUJI Hideaki #endif 838ef2a9a5SYOSHIFUJI Hideaki } 848ef2a9a5SYOSHIFUJI Hideaki return 0; 858ef2a9a5SYOSHIFUJI Hideaki } 868ef2a9a5SYOSHIFUJI Hideaki 87eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *__br_mdb_ip_get( 888ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash) 89eb1d1641SHerbert Xu { 90eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 91eb1d1641SHerbert Xu struct hlist_node *p; 92eb1d1641SHerbert Xu 9349f5fcfdSHerbert Xu hlist_for_each_entry_rcu(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) { 948ef2a9a5SYOSHIFUJI Hideaki if (br_ip_equal(&mp->addr, dst)) 95eb1d1641SHerbert Xu return mp; 96eb1d1641SHerbert Xu } 97eb1d1641SHerbert Xu 98eb1d1641SHerbert Xu return NULL; 99eb1d1641SHerbert Xu } 100eb1d1641SHerbert Xu 1018ef2a9a5SYOSHIFUJI Hideaki static struct net_bridge_mdb_entry *br_mdb_ip4_get( 102eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb, __be32 dst) 103eb1d1641SHerbert Xu { 1048ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_dst; 1050821ec55SHerbert Xu 1068ef2a9a5SYOSHIFUJI Hideaki br_dst.u.ip4 = dst; 1078ef2a9a5SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IP); 1088ef2a9a5SYOSHIFUJI Hideaki 1098ef2a9a5SYOSHIFUJI Hideaki return __br_mdb_ip_get(mdb, &br_dst, __br_ip4_hash(mdb, dst)); 1108ef2a9a5SYOSHIFUJI Hideaki } 1118ef2a9a5SYOSHIFUJI Hideaki 11208b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 11308b202b6SYOSHIFUJI Hideaki static struct net_bridge_mdb_entry *br_mdb_ip6_get( 11408b202b6SYOSHIFUJI Hideaki struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst) 11508b202b6SYOSHIFUJI Hideaki { 11608b202b6SYOSHIFUJI Hideaki struct br_ip br_dst; 11708b202b6SYOSHIFUJI Hideaki 11808b202b6SYOSHIFUJI Hideaki ipv6_addr_copy(&br_dst.u.ip6, dst); 11908b202b6SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IPV6); 12008b202b6SYOSHIFUJI Hideaki 12108b202b6SYOSHIFUJI Hideaki return __br_mdb_ip_get(mdb, &br_dst, __br_ip6_hash(mdb, dst)); 12208b202b6SYOSHIFUJI Hideaki } 12308b202b6SYOSHIFUJI Hideaki #endif 12408b202b6SYOSHIFUJI Hideaki 1258ef2a9a5SYOSHIFUJI Hideaki static struct net_bridge_mdb_entry *br_mdb_ip_get( 1268ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_mdb_htable *mdb, struct br_ip *dst) 1278ef2a9a5SYOSHIFUJI Hideaki { 128eb1d1641SHerbert Xu return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst)); 129eb1d1641SHerbert Xu } 130eb1d1641SHerbert Xu 131eb1d1641SHerbert Xu struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br, 132eb1d1641SHerbert Xu struct sk_buff *skb) 133eb1d1641SHerbert Xu { 1348ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_mdb_htable *mdb = br->mdb; 1358ef2a9a5SYOSHIFUJI Hideaki struct br_ip ip; 1368ef2a9a5SYOSHIFUJI Hideaki 1378ef2a9a5SYOSHIFUJI Hideaki if (!mdb || br->multicast_disabled) 138eb1d1641SHerbert Xu return NULL; 139eb1d1641SHerbert Xu 1408ef2a9a5SYOSHIFUJI Hideaki if (BR_INPUT_SKB_CB(skb)->igmp) 1418ef2a9a5SYOSHIFUJI Hideaki return NULL; 1428ef2a9a5SYOSHIFUJI Hideaki 1438ef2a9a5SYOSHIFUJI Hideaki ip.proto = skb->protocol; 1448ef2a9a5SYOSHIFUJI Hideaki 145eb1d1641SHerbert Xu switch (skb->protocol) { 146eb1d1641SHerbert Xu case htons(ETH_P_IP): 1478ef2a9a5SYOSHIFUJI Hideaki ip.u.ip4 = ip_hdr(skb)->daddr; 148eb1d1641SHerbert Xu break; 14908b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 15008b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 15108b202b6SYOSHIFUJI Hideaki ipv6_addr_copy(&ip.u.ip6, &ipv6_hdr(skb)->daddr); 15208b202b6SYOSHIFUJI Hideaki break; 15308b202b6SYOSHIFUJI Hideaki #endif 1548ef2a9a5SYOSHIFUJI Hideaki default: 1558ef2a9a5SYOSHIFUJI Hideaki return NULL; 156eb1d1641SHerbert Xu } 157eb1d1641SHerbert Xu 1588ef2a9a5SYOSHIFUJI Hideaki return br_mdb_ip_get(mdb, &ip); 159eb1d1641SHerbert Xu } 160eb1d1641SHerbert Xu 161eb1d1641SHerbert Xu static void br_mdb_free(struct rcu_head *head) 162eb1d1641SHerbert Xu { 163eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb = 164eb1d1641SHerbert Xu container_of(head, struct net_bridge_mdb_htable, rcu); 165eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old = mdb->old; 166eb1d1641SHerbert Xu 167eb1d1641SHerbert Xu mdb->old = NULL; 168eb1d1641SHerbert Xu kfree(old->mhash); 169eb1d1641SHerbert Xu kfree(old); 170eb1d1641SHerbert Xu } 171eb1d1641SHerbert Xu 172eb1d1641SHerbert Xu static int br_mdb_copy(struct net_bridge_mdb_htable *new, 173eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old, 174eb1d1641SHerbert Xu int elasticity) 175eb1d1641SHerbert Xu { 176eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 177eb1d1641SHerbert Xu struct hlist_node *p; 178eb1d1641SHerbert Xu int maxlen; 179eb1d1641SHerbert Xu int len; 180eb1d1641SHerbert Xu int i; 181eb1d1641SHerbert Xu 182eb1d1641SHerbert Xu for (i = 0; i < old->max; i++) 183eb1d1641SHerbert Xu hlist_for_each_entry(mp, p, &old->mhash[i], hlist[old->ver]) 184eb1d1641SHerbert Xu hlist_add_head(&mp->hlist[new->ver], 1858ef2a9a5SYOSHIFUJI Hideaki &new->mhash[br_ip_hash(new, &mp->addr)]); 186eb1d1641SHerbert Xu 187eb1d1641SHerbert Xu if (!elasticity) 188eb1d1641SHerbert Xu return 0; 189eb1d1641SHerbert Xu 190eb1d1641SHerbert Xu maxlen = 0; 191eb1d1641SHerbert Xu for (i = 0; i < new->max; i++) { 192eb1d1641SHerbert Xu len = 0; 193eb1d1641SHerbert Xu hlist_for_each_entry(mp, p, &new->mhash[i], hlist[new->ver]) 194eb1d1641SHerbert Xu len++; 195eb1d1641SHerbert Xu if (len > maxlen) 196eb1d1641SHerbert Xu maxlen = len; 197eb1d1641SHerbert Xu } 198eb1d1641SHerbert Xu 199eb1d1641SHerbert Xu return maxlen > elasticity ? -EINVAL : 0; 200eb1d1641SHerbert Xu } 201eb1d1641SHerbert Xu 202eb1d1641SHerbert Xu static void br_multicast_free_pg(struct rcu_head *head) 203eb1d1641SHerbert Xu { 204eb1d1641SHerbert Xu struct net_bridge_port_group *p = 205eb1d1641SHerbert Xu container_of(head, struct net_bridge_port_group, rcu); 206eb1d1641SHerbert Xu 207eb1d1641SHerbert Xu kfree(p); 208eb1d1641SHerbert Xu } 209eb1d1641SHerbert Xu 210eb1d1641SHerbert Xu static void br_multicast_free_group(struct rcu_head *head) 211eb1d1641SHerbert Xu { 212eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = 213eb1d1641SHerbert Xu container_of(head, struct net_bridge_mdb_entry, rcu); 214eb1d1641SHerbert Xu 215eb1d1641SHerbert Xu kfree(mp); 216eb1d1641SHerbert Xu } 217eb1d1641SHerbert Xu 218eb1d1641SHerbert Xu static void br_multicast_group_expired(unsigned long data) 219eb1d1641SHerbert Xu { 220eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = (void *)data; 221eb1d1641SHerbert Xu struct net_bridge *br = mp->br; 222eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 223eb1d1641SHerbert Xu 224eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 225eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&mp->timer)) 226eb1d1641SHerbert Xu goto out; 227eb1d1641SHerbert Xu 228eb1d1641SHerbert Xu if (!hlist_unhashed(&mp->mglist)) 229eb1d1641SHerbert Xu hlist_del_init(&mp->mglist); 230eb1d1641SHerbert Xu 231eb1d1641SHerbert Xu if (mp->ports) 232eb1d1641SHerbert Xu goto out; 233eb1d1641SHerbert Xu 234eb1d1641SHerbert Xu mdb = br->mdb; 235eb1d1641SHerbert Xu hlist_del_rcu(&mp->hlist[mdb->ver]); 236eb1d1641SHerbert Xu mdb->size--; 237eb1d1641SHerbert Xu 238eb1d1641SHerbert Xu del_timer(&mp->query_timer); 239eb1d1641SHerbert Xu call_rcu_bh(&mp->rcu, br_multicast_free_group); 240eb1d1641SHerbert Xu 241eb1d1641SHerbert Xu out: 242eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 243eb1d1641SHerbert Xu } 244eb1d1641SHerbert Xu 245eb1d1641SHerbert Xu static void br_multicast_del_pg(struct net_bridge *br, 246eb1d1641SHerbert Xu struct net_bridge_port_group *pg) 247eb1d1641SHerbert Xu { 248eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb = br->mdb; 249eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 250eb1d1641SHerbert Xu struct net_bridge_port_group *p; 251eb1d1641SHerbert Xu struct net_bridge_port_group **pp; 252eb1d1641SHerbert Xu 2538ef2a9a5SYOSHIFUJI Hideaki mp = br_mdb_ip_get(mdb, &pg->addr); 254eb1d1641SHerbert Xu if (WARN_ON(!mp)) 255eb1d1641SHerbert Xu return; 256eb1d1641SHerbert Xu 257eb1d1641SHerbert Xu for (pp = &mp->ports; (p = *pp); pp = &p->next) { 258eb1d1641SHerbert Xu if (p != pg) 259eb1d1641SHerbert Xu continue; 260eb1d1641SHerbert Xu 261eb1d1641SHerbert Xu *pp = p->next; 262eb1d1641SHerbert Xu hlist_del_init(&p->mglist); 263eb1d1641SHerbert Xu del_timer(&p->timer); 264eb1d1641SHerbert Xu del_timer(&p->query_timer); 265eb1d1641SHerbert Xu call_rcu_bh(&p->rcu, br_multicast_free_pg); 266eb1d1641SHerbert Xu 267eb1d1641SHerbert Xu if (!mp->ports && hlist_unhashed(&mp->mglist) && 268eb1d1641SHerbert Xu netif_running(br->dev)) 269eb1d1641SHerbert Xu mod_timer(&mp->timer, jiffies); 270eb1d1641SHerbert Xu 271eb1d1641SHerbert Xu return; 272eb1d1641SHerbert Xu } 273eb1d1641SHerbert Xu 274eb1d1641SHerbert Xu WARN_ON(1); 275eb1d1641SHerbert Xu } 276eb1d1641SHerbert Xu 277eb1d1641SHerbert Xu static void br_multicast_port_group_expired(unsigned long data) 278eb1d1641SHerbert Xu { 279eb1d1641SHerbert Xu struct net_bridge_port_group *pg = (void *)data; 280eb1d1641SHerbert Xu struct net_bridge *br = pg->port->br; 281eb1d1641SHerbert Xu 282eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 283eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&pg->timer) || 284eb1d1641SHerbert Xu hlist_unhashed(&pg->mglist)) 285eb1d1641SHerbert Xu goto out; 286eb1d1641SHerbert Xu 287eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 288eb1d1641SHerbert Xu 289eb1d1641SHerbert Xu out: 290eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 291eb1d1641SHerbert Xu } 292eb1d1641SHerbert Xu 293eb1d1641SHerbert Xu static int br_mdb_rehash(struct net_bridge_mdb_htable **mdbp, int max, 294eb1d1641SHerbert Xu int elasticity) 295eb1d1641SHerbert Xu { 296eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old = *mdbp; 297eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 298eb1d1641SHerbert Xu int err; 299eb1d1641SHerbert Xu 300eb1d1641SHerbert Xu mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC); 301eb1d1641SHerbert Xu if (!mdb) 302eb1d1641SHerbert Xu return -ENOMEM; 303eb1d1641SHerbert Xu 304eb1d1641SHerbert Xu mdb->max = max; 305eb1d1641SHerbert Xu mdb->old = old; 306eb1d1641SHerbert Xu 307eb1d1641SHerbert Xu mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC); 308eb1d1641SHerbert Xu if (!mdb->mhash) { 309eb1d1641SHerbert Xu kfree(mdb); 310eb1d1641SHerbert Xu return -ENOMEM; 311eb1d1641SHerbert Xu } 312eb1d1641SHerbert Xu 313eb1d1641SHerbert Xu mdb->size = old ? old->size : 0; 314eb1d1641SHerbert Xu mdb->ver = old ? old->ver ^ 1 : 0; 315eb1d1641SHerbert Xu 316eb1d1641SHerbert Xu if (!old || elasticity) 317eb1d1641SHerbert Xu get_random_bytes(&mdb->secret, sizeof(mdb->secret)); 318eb1d1641SHerbert Xu else 319eb1d1641SHerbert Xu mdb->secret = old->secret; 320eb1d1641SHerbert Xu 321eb1d1641SHerbert Xu if (!old) 322eb1d1641SHerbert Xu goto out; 323eb1d1641SHerbert Xu 324eb1d1641SHerbert Xu err = br_mdb_copy(mdb, old, elasticity); 325eb1d1641SHerbert Xu if (err) { 326eb1d1641SHerbert Xu kfree(mdb->mhash); 327eb1d1641SHerbert Xu kfree(mdb); 328eb1d1641SHerbert Xu return err; 329eb1d1641SHerbert Xu } 330eb1d1641SHerbert Xu 331eb1d1641SHerbert Xu call_rcu_bh(&mdb->rcu, br_mdb_free); 332eb1d1641SHerbert Xu 333eb1d1641SHerbert Xu out: 334eb1d1641SHerbert Xu rcu_assign_pointer(*mdbp, mdb); 335eb1d1641SHerbert Xu 336eb1d1641SHerbert Xu return 0; 337eb1d1641SHerbert Xu } 338eb1d1641SHerbert Xu 3398ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br, 340eb1d1641SHerbert Xu __be32 group) 341eb1d1641SHerbert Xu { 342eb1d1641SHerbert Xu struct sk_buff *skb; 343eb1d1641SHerbert Xu struct igmphdr *ih; 344eb1d1641SHerbert Xu struct ethhdr *eth; 345eb1d1641SHerbert Xu struct iphdr *iph; 346eb1d1641SHerbert Xu 347eb1d1641SHerbert Xu skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) + 348eb1d1641SHerbert Xu sizeof(*ih) + 4); 349eb1d1641SHerbert Xu if (!skb) 350eb1d1641SHerbert Xu goto out; 351eb1d1641SHerbert Xu 352eb1d1641SHerbert Xu skb->protocol = htons(ETH_P_IP); 353eb1d1641SHerbert Xu 354eb1d1641SHerbert Xu skb_reset_mac_header(skb); 355eb1d1641SHerbert Xu eth = eth_hdr(skb); 356eb1d1641SHerbert Xu 357eb1d1641SHerbert Xu memcpy(eth->h_source, br->dev->dev_addr, 6); 358eb1d1641SHerbert Xu eth->h_dest[0] = 1; 359eb1d1641SHerbert Xu eth->h_dest[1] = 0; 360eb1d1641SHerbert Xu eth->h_dest[2] = 0x5e; 361eb1d1641SHerbert Xu eth->h_dest[3] = 0; 362eb1d1641SHerbert Xu eth->h_dest[4] = 0; 363eb1d1641SHerbert Xu eth->h_dest[5] = 1; 364eb1d1641SHerbert Xu eth->h_proto = htons(ETH_P_IP); 365eb1d1641SHerbert Xu skb_put(skb, sizeof(*eth)); 366eb1d1641SHerbert Xu 367eb1d1641SHerbert Xu skb_set_network_header(skb, skb->len); 368eb1d1641SHerbert Xu iph = ip_hdr(skb); 369eb1d1641SHerbert Xu 370eb1d1641SHerbert Xu iph->version = 4; 371eb1d1641SHerbert Xu iph->ihl = 6; 372eb1d1641SHerbert Xu iph->tos = 0xc0; 373eb1d1641SHerbert Xu iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4); 374eb1d1641SHerbert Xu iph->id = 0; 375eb1d1641SHerbert Xu iph->frag_off = htons(IP_DF); 376eb1d1641SHerbert Xu iph->ttl = 1; 377eb1d1641SHerbert Xu iph->protocol = IPPROTO_IGMP; 378eb1d1641SHerbert Xu iph->saddr = 0; 379eb1d1641SHerbert Xu iph->daddr = htonl(INADDR_ALLHOSTS_GROUP); 380eb1d1641SHerbert Xu ((u8 *)&iph[1])[0] = IPOPT_RA; 381eb1d1641SHerbert Xu ((u8 *)&iph[1])[1] = 4; 382eb1d1641SHerbert Xu ((u8 *)&iph[1])[2] = 0; 383eb1d1641SHerbert Xu ((u8 *)&iph[1])[3] = 0; 384eb1d1641SHerbert Xu ip_send_check(iph); 385eb1d1641SHerbert Xu skb_put(skb, 24); 386eb1d1641SHerbert Xu 387eb1d1641SHerbert Xu skb_set_transport_header(skb, skb->len); 388eb1d1641SHerbert Xu ih = igmp_hdr(skb); 389eb1d1641SHerbert Xu ih->type = IGMP_HOST_MEMBERSHIP_QUERY; 390eb1d1641SHerbert Xu ih->code = (group ? br->multicast_last_member_interval : 391eb1d1641SHerbert Xu br->multicast_query_response_interval) / 392eb1d1641SHerbert Xu (HZ / IGMP_TIMER_SCALE); 393eb1d1641SHerbert Xu ih->group = group; 394eb1d1641SHerbert Xu ih->csum = 0; 395eb1d1641SHerbert Xu ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr)); 396eb1d1641SHerbert Xu skb_put(skb, sizeof(*ih)); 397eb1d1641SHerbert Xu 398eb1d1641SHerbert Xu __skb_pull(skb, sizeof(*eth)); 399eb1d1641SHerbert Xu 400eb1d1641SHerbert Xu out: 401eb1d1641SHerbert Xu return skb; 402eb1d1641SHerbert Xu } 403eb1d1641SHerbert Xu 40408b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 40508b202b6SYOSHIFUJI Hideaki static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br, 40608b202b6SYOSHIFUJI Hideaki struct in6_addr *group) 40708b202b6SYOSHIFUJI Hideaki { 40808b202b6SYOSHIFUJI Hideaki struct sk_buff *skb; 40908b202b6SYOSHIFUJI Hideaki struct ipv6hdr *ip6h; 41008b202b6SYOSHIFUJI Hideaki struct mld_msg *mldq; 41108b202b6SYOSHIFUJI Hideaki struct ethhdr *eth; 41208b202b6SYOSHIFUJI Hideaki u8 *hopopt; 41308b202b6SYOSHIFUJI Hideaki unsigned long interval; 41408b202b6SYOSHIFUJI Hideaki 41508b202b6SYOSHIFUJI Hideaki skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) + 41608b202b6SYOSHIFUJI Hideaki 8 + sizeof(*mldq)); 41708b202b6SYOSHIFUJI Hideaki if (!skb) 41808b202b6SYOSHIFUJI Hideaki goto out; 41908b202b6SYOSHIFUJI Hideaki 42008b202b6SYOSHIFUJI Hideaki skb->protocol = htons(ETH_P_IPV6); 42108b202b6SYOSHIFUJI Hideaki 42208b202b6SYOSHIFUJI Hideaki /* Ethernet header */ 42308b202b6SYOSHIFUJI Hideaki skb_reset_mac_header(skb); 42408b202b6SYOSHIFUJI Hideaki eth = eth_hdr(skb); 42508b202b6SYOSHIFUJI Hideaki 42608b202b6SYOSHIFUJI Hideaki memcpy(eth->h_source, br->dev->dev_addr, 6); 42708b202b6SYOSHIFUJI Hideaki ipv6_eth_mc_map(group, eth->h_dest); 42808b202b6SYOSHIFUJI Hideaki eth->h_proto = htons(ETH_P_IPV6); 42908b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*eth)); 43008b202b6SYOSHIFUJI Hideaki 43108b202b6SYOSHIFUJI Hideaki /* IPv6 header + HbH option */ 43208b202b6SYOSHIFUJI Hideaki skb_set_network_header(skb, skb->len); 43308b202b6SYOSHIFUJI Hideaki ip6h = ipv6_hdr(skb); 43408b202b6SYOSHIFUJI Hideaki 43508b202b6SYOSHIFUJI Hideaki *(__force __be32 *)ip6h = htonl(0x60000000); 43608b202b6SYOSHIFUJI Hideaki ip6h->payload_len = 8 + sizeof(*mldq); 43708b202b6SYOSHIFUJI Hideaki ip6h->nexthdr = IPPROTO_HOPOPTS; 43808b202b6SYOSHIFUJI Hideaki ip6h->hop_limit = 1; 43908b202b6SYOSHIFUJI Hideaki ipv6_addr_set(&ip6h->saddr, 0, 0, 0, 0); 44008b202b6SYOSHIFUJI Hideaki ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1)); 44108b202b6SYOSHIFUJI Hideaki 44208b202b6SYOSHIFUJI Hideaki hopopt = (u8 *)(ip6h + 1); 44308b202b6SYOSHIFUJI Hideaki hopopt[0] = IPPROTO_ICMPV6; /* next hdr */ 44408b202b6SYOSHIFUJI Hideaki hopopt[1] = 0; /* length of HbH */ 44508b202b6SYOSHIFUJI Hideaki hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */ 44608b202b6SYOSHIFUJI Hideaki hopopt[3] = 2; /* Length of RA Option */ 44708b202b6SYOSHIFUJI Hideaki hopopt[4] = 0; /* Type = 0x0000 (MLD) */ 44808b202b6SYOSHIFUJI Hideaki hopopt[5] = 0; 44908b202b6SYOSHIFUJI Hideaki hopopt[6] = IPV6_TLV_PAD0; /* Pad0 */ 45008b202b6SYOSHIFUJI Hideaki hopopt[7] = IPV6_TLV_PAD0; /* Pad0 */ 45108b202b6SYOSHIFUJI Hideaki 45208b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*ip6h) + 8); 45308b202b6SYOSHIFUJI Hideaki 45408b202b6SYOSHIFUJI Hideaki /* ICMPv6 */ 45508b202b6SYOSHIFUJI Hideaki skb_set_transport_header(skb, skb->len); 45608b202b6SYOSHIFUJI Hideaki mldq = (struct mld_msg *) icmp6_hdr(skb); 45708b202b6SYOSHIFUJI Hideaki 45808b202b6SYOSHIFUJI Hideaki interval = ipv6_addr_any(group) ? br->multicast_last_member_interval : 45908b202b6SYOSHIFUJI Hideaki br->multicast_query_response_interval; 46008b202b6SYOSHIFUJI Hideaki 46108b202b6SYOSHIFUJI Hideaki mldq->mld_type = ICMPV6_MGM_QUERY; 46208b202b6SYOSHIFUJI Hideaki mldq->mld_code = 0; 46308b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = 0; 46408b202b6SYOSHIFUJI Hideaki mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval)); 46508b202b6SYOSHIFUJI Hideaki mldq->mld_reserved = 0; 46608b202b6SYOSHIFUJI Hideaki ipv6_addr_copy(&mldq->mld_mca, group); 46708b202b6SYOSHIFUJI Hideaki 46808b202b6SYOSHIFUJI Hideaki /* checksum */ 46908b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, 47008b202b6SYOSHIFUJI Hideaki sizeof(*mldq), IPPROTO_ICMPV6, 47108b202b6SYOSHIFUJI Hideaki csum_partial(mldq, 47208b202b6SYOSHIFUJI Hideaki sizeof(*mldq), 0)); 47308b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*mldq)); 47408b202b6SYOSHIFUJI Hideaki 47508b202b6SYOSHIFUJI Hideaki __skb_pull(skb, sizeof(*eth)); 47608b202b6SYOSHIFUJI Hideaki 47708b202b6SYOSHIFUJI Hideaki out: 47808b202b6SYOSHIFUJI Hideaki return skb; 47908b202b6SYOSHIFUJI Hideaki } 48008b202b6SYOSHIFUJI Hideaki #endif 48108b202b6SYOSHIFUJI Hideaki 4828ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br, 4838ef2a9a5SYOSHIFUJI Hideaki struct br_ip *addr) 4848ef2a9a5SYOSHIFUJI Hideaki { 4858ef2a9a5SYOSHIFUJI Hideaki switch (addr->proto) { 4868ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 4878ef2a9a5SYOSHIFUJI Hideaki return br_ip4_multicast_alloc_query(br, addr->u.ip4); 48808b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 48908b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 49008b202b6SYOSHIFUJI Hideaki return br_ip6_multicast_alloc_query(br, &addr->u.ip6); 49108b202b6SYOSHIFUJI Hideaki #endif 4928ef2a9a5SYOSHIFUJI Hideaki } 4938ef2a9a5SYOSHIFUJI Hideaki return NULL; 4948ef2a9a5SYOSHIFUJI Hideaki } 4958ef2a9a5SYOSHIFUJI Hideaki 496eb1d1641SHerbert Xu static void br_multicast_send_group_query(struct net_bridge_mdb_entry *mp) 497eb1d1641SHerbert Xu { 498eb1d1641SHerbert Xu struct net_bridge *br = mp->br; 499eb1d1641SHerbert Xu struct sk_buff *skb; 500eb1d1641SHerbert Xu 5018ef2a9a5SYOSHIFUJI Hideaki skb = br_multicast_alloc_query(br, &mp->addr); 502eb1d1641SHerbert Xu if (!skb) 503eb1d1641SHerbert Xu goto timer; 504eb1d1641SHerbert Xu 505eb1d1641SHerbert Xu netif_rx(skb); 506eb1d1641SHerbert Xu 507eb1d1641SHerbert Xu timer: 508eb1d1641SHerbert Xu if (++mp->queries_sent < br->multicast_last_member_count) 509eb1d1641SHerbert Xu mod_timer(&mp->query_timer, 510eb1d1641SHerbert Xu jiffies + br->multicast_last_member_interval); 511eb1d1641SHerbert Xu } 512eb1d1641SHerbert Xu 513eb1d1641SHerbert Xu static void br_multicast_group_query_expired(unsigned long data) 514eb1d1641SHerbert Xu { 515eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = (void *)data; 516eb1d1641SHerbert Xu struct net_bridge *br = mp->br; 517eb1d1641SHerbert Xu 518eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 519eb1d1641SHerbert Xu if (!netif_running(br->dev) || hlist_unhashed(&mp->mglist) || 520eb1d1641SHerbert Xu mp->queries_sent >= br->multicast_last_member_count) 521eb1d1641SHerbert Xu goto out; 522eb1d1641SHerbert Xu 523eb1d1641SHerbert Xu br_multicast_send_group_query(mp); 524eb1d1641SHerbert Xu 525eb1d1641SHerbert Xu out: 526eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 527eb1d1641SHerbert Xu } 528eb1d1641SHerbert Xu 529eb1d1641SHerbert Xu static void br_multicast_send_port_group_query(struct net_bridge_port_group *pg) 530eb1d1641SHerbert Xu { 531eb1d1641SHerbert Xu struct net_bridge_port *port = pg->port; 532eb1d1641SHerbert Xu struct net_bridge *br = port->br; 533eb1d1641SHerbert Xu struct sk_buff *skb; 534eb1d1641SHerbert Xu 5358ef2a9a5SYOSHIFUJI Hideaki skb = br_multicast_alloc_query(br, &pg->addr); 536eb1d1641SHerbert Xu if (!skb) 537eb1d1641SHerbert Xu goto timer; 538eb1d1641SHerbert Xu 539eb1d1641SHerbert Xu br_deliver(port, skb); 540eb1d1641SHerbert Xu 541eb1d1641SHerbert Xu timer: 542eb1d1641SHerbert Xu if (++pg->queries_sent < br->multicast_last_member_count) 543eb1d1641SHerbert Xu mod_timer(&pg->query_timer, 544eb1d1641SHerbert Xu jiffies + br->multicast_last_member_interval); 545eb1d1641SHerbert Xu } 546eb1d1641SHerbert Xu 547eb1d1641SHerbert Xu static void br_multicast_port_group_query_expired(unsigned long data) 548eb1d1641SHerbert Xu { 549eb1d1641SHerbert Xu struct net_bridge_port_group *pg = (void *)data; 550eb1d1641SHerbert Xu struct net_bridge_port *port = pg->port; 551eb1d1641SHerbert Xu struct net_bridge *br = port->br; 552eb1d1641SHerbert Xu 553eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 554eb1d1641SHerbert Xu if (!netif_running(br->dev) || hlist_unhashed(&pg->mglist) || 555eb1d1641SHerbert Xu pg->queries_sent >= br->multicast_last_member_count) 556eb1d1641SHerbert Xu goto out; 557eb1d1641SHerbert Xu 558eb1d1641SHerbert Xu br_multicast_send_port_group_query(pg); 559eb1d1641SHerbert Xu 560eb1d1641SHerbert Xu out: 561eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 562eb1d1641SHerbert Xu } 563eb1d1641SHerbert Xu 564eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *br_multicast_get_group( 5658ef2a9a5SYOSHIFUJI Hideaki struct net_bridge *br, struct net_bridge_port *port, 5668ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group, int hash) 567eb1d1641SHerbert Xu { 568eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb = br->mdb; 569eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 570eb1d1641SHerbert Xu struct hlist_node *p; 571eb1d1641SHerbert Xu unsigned count = 0; 572eb1d1641SHerbert Xu unsigned max; 573eb1d1641SHerbert Xu int elasticity; 574eb1d1641SHerbert Xu int err; 575eb1d1641SHerbert Xu 576eb1d1641SHerbert Xu hlist_for_each_entry(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) { 577eb1d1641SHerbert Xu count++; 5788ef2a9a5SYOSHIFUJI Hideaki if (unlikely(br_ip_equal(group, &mp->addr))) 579eb1d1641SHerbert Xu return mp; 580eb1d1641SHerbert Xu } 581eb1d1641SHerbert Xu 582eb1d1641SHerbert Xu elasticity = 0; 583eb1d1641SHerbert Xu max = mdb->max; 584eb1d1641SHerbert Xu 585eb1d1641SHerbert Xu if (unlikely(count > br->hash_elasticity && count)) { 586eb1d1641SHerbert Xu if (net_ratelimit()) 587eb1d1641SHerbert Xu printk(KERN_INFO "%s: Multicast hash table " 588eb1d1641SHerbert Xu "chain limit reached: %s\n", 589eb1d1641SHerbert Xu br->dev->name, port ? port->dev->name : 590eb1d1641SHerbert Xu br->dev->name); 591eb1d1641SHerbert Xu 592eb1d1641SHerbert Xu elasticity = br->hash_elasticity; 593eb1d1641SHerbert Xu } 594eb1d1641SHerbert Xu 595eb1d1641SHerbert Xu if (mdb->size >= max) { 596eb1d1641SHerbert Xu max *= 2; 597eb1d1641SHerbert Xu if (unlikely(max >= br->hash_max)) { 598eb1d1641SHerbert Xu printk(KERN_WARNING "%s: Multicast hash table maximum " 599eb1d1641SHerbert Xu "reached, disabling snooping: %s, %d\n", 600eb1d1641SHerbert Xu br->dev->name, port ? port->dev->name : 601eb1d1641SHerbert Xu br->dev->name, 602eb1d1641SHerbert Xu max); 603eb1d1641SHerbert Xu err = -E2BIG; 604eb1d1641SHerbert Xu disable: 605eb1d1641SHerbert Xu br->multicast_disabled = 1; 606eb1d1641SHerbert Xu goto err; 607eb1d1641SHerbert Xu } 608eb1d1641SHerbert Xu } 609eb1d1641SHerbert Xu 610eb1d1641SHerbert Xu if (max > mdb->max || elasticity) { 611eb1d1641SHerbert Xu if (mdb->old) { 612eb1d1641SHerbert Xu if (net_ratelimit()) 613eb1d1641SHerbert Xu printk(KERN_INFO "%s: Multicast hash table " 614eb1d1641SHerbert Xu "on fire: %s\n", 615eb1d1641SHerbert Xu br->dev->name, port ? port->dev->name : 616eb1d1641SHerbert Xu 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) { 623eb1d1641SHerbert Xu printk(KERN_WARNING "%s: Cannot rehash multicast " 624eb1d1641SHerbert Xu "hash table, disabling snooping: " 625eb1d1641SHerbert Xu "%s, %d, %d\n", 626eb1d1641SHerbert Xu br->dev->name, port ? port->dev->name : 627eb1d1641SHerbert Xu br->dev->name, 628eb1d1641SHerbert Xu mdb->size, err); 629eb1d1641SHerbert Xu goto disable; 630eb1d1641SHerbert Xu } 631eb1d1641SHerbert Xu 632eb1d1641SHerbert Xu err = -EAGAIN; 633eb1d1641SHerbert Xu goto err; 634eb1d1641SHerbert Xu } 635eb1d1641SHerbert Xu 636eb1d1641SHerbert Xu return NULL; 637eb1d1641SHerbert Xu 638eb1d1641SHerbert Xu err: 639eb1d1641SHerbert Xu mp = ERR_PTR(err); 640eb1d1641SHerbert Xu return mp; 641eb1d1641SHerbert Xu } 642eb1d1641SHerbert Xu 643eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *br_multicast_new_group( 6448ef2a9a5SYOSHIFUJI Hideaki struct net_bridge *br, struct net_bridge_port *port, 6458ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group) 646eb1d1641SHerbert Xu { 647eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb = br->mdb; 648eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 649eb1d1641SHerbert Xu int hash; 650eb1d1641SHerbert Xu 651eb1d1641SHerbert Xu if (!mdb) { 652eb1d1641SHerbert Xu if (br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0)) 653eb1d1641SHerbert Xu return NULL; 654eb1d1641SHerbert Xu goto rehash; 655eb1d1641SHerbert Xu } 656eb1d1641SHerbert Xu 657eb1d1641SHerbert Xu hash = br_ip_hash(mdb, group); 658eb1d1641SHerbert Xu mp = br_multicast_get_group(br, port, group, hash); 659eb1d1641SHerbert Xu switch (PTR_ERR(mp)) { 660eb1d1641SHerbert Xu case 0: 661eb1d1641SHerbert Xu break; 662eb1d1641SHerbert Xu 663eb1d1641SHerbert Xu case -EAGAIN: 664eb1d1641SHerbert Xu rehash: 665eb1d1641SHerbert Xu mdb = br->mdb; 666eb1d1641SHerbert Xu hash = br_ip_hash(mdb, group); 667eb1d1641SHerbert Xu break; 668eb1d1641SHerbert Xu 669eb1d1641SHerbert Xu default: 670eb1d1641SHerbert Xu goto out; 671eb1d1641SHerbert Xu } 672eb1d1641SHerbert Xu 673eb1d1641SHerbert Xu mp = kzalloc(sizeof(*mp), GFP_ATOMIC); 674eb1d1641SHerbert Xu if (unlikely(!mp)) 675eb1d1641SHerbert Xu goto out; 676eb1d1641SHerbert Xu 677eb1d1641SHerbert Xu mp->br = br; 6788ef2a9a5SYOSHIFUJI Hideaki mp->addr = *group; 679eb1d1641SHerbert Xu setup_timer(&mp->timer, br_multicast_group_expired, 680eb1d1641SHerbert Xu (unsigned long)mp); 681eb1d1641SHerbert Xu setup_timer(&mp->query_timer, br_multicast_group_query_expired, 682eb1d1641SHerbert Xu (unsigned long)mp); 683eb1d1641SHerbert Xu 684eb1d1641SHerbert Xu hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]); 685eb1d1641SHerbert Xu mdb->size++; 686eb1d1641SHerbert Xu 687eb1d1641SHerbert Xu out: 688eb1d1641SHerbert Xu return mp; 689eb1d1641SHerbert Xu } 690eb1d1641SHerbert Xu 691eb1d1641SHerbert Xu static int br_multicast_add_group(struct net_bridge *br, 6928ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 6938ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group) 694eb1d1641SHerbert Xu { 695eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 696eb1d1641SHerbert Xu struct net_bridge_port_group *p; 697eb1d1641SHerbert Xu struct net_bridge_port_group **pp; 698eb1d1641SHerbert Xu unsigned long now = jiffies; 699eb1d1641SHerbert Xu int err; 700eb1d1641SHerbert Xu 701eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 702eb1d1641SHerbert Xu if (!netif_running(br->dev) || 703eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 704eb1d1641SHerbert Xu goto out; 705eb1d1641SHerbert Xu 706eb1d1641SHerbert Xu mp = br_multicast_new_group(br, port, group); 707eb1d1641SHerbert Xu err = PTR_ERR(mp); 708eb1d1641SHerbert Xu if (unlikely(IS_ERR(mp) || !mp)) 709eb1d1641SHerbert Xu goto err; 710eb1d1641SHerbert Xu 711eb1d1641SHerbert Xu if (!port) { 712eb1d1641SHerbert Xu hlist_add_head(&mp->mglist, &br->mglist); 713eb1d1641SHerbert Xu mod_timer(&mp->timer, now + br->multicast_membership_interval); 714eb1d1641SHerbert Xu goto out; 715eb1d1641SHerbert Xu } 716eb1d1641SHerbert Xu 717eb1d1641SHerbert Xu for (pp = &mp->ports; (p = *pp); pp = &p->next) { 718eb1d1641SHerbert Xu if (p->port == port) 719eb1d1641SHerbert Xu goto found; 720eb1d1641SHerbert Xu if ((unsigned long)p->port < (unsigned long)port) 721eb1d1641SHerbert Xu break; 722eb1d1641SHerbert Xu } 723eb1d1641SHerbert Xu 724eb1d1641SHerbert Xu p = kzalloc(sizeof(*p), GFP_ATOMIC); 725eb1d1641SHerbert Xu err = -ENOMEM; 726eb1d1641SHerbert Xu if (unlikely(!p)) 727eb1d1641SHerbert Xu goto err; 728eb1d1641SHerbert Xu 7298ef2a9a5SYOSHIFUJI Hideaki p->addr = *group; 730eb1d1641SHerbert Xu p->port = port; 731eb1d1641SHerbert Xu p->next = *pp; 732eb1d1641SHerbert Xu hlist_add_head(&p->mglist, &port->mglist); 733eb1d1641SHerbert Xu setup_timer(&p->timer, br_multicast_port_group_expired, 734eb1d1641SHerbert Xu (unsigned long)p); 735eb1d1641SHerbert Xu setup_timer(&p->query_timer, br_multicast_port_group_query_expired, 736eb1d1641SHerbert Xu (unsigned long)p); 737eb1d1641SHerbert Xu 738eb1d1641SHerbert Xu rcu_assign_pointer(*pp, p); 739eb1d1641SHerbert Xu 740eb1d1641SHerbert Xu found: 741eb1d1641SHerbert Xu mod_timer(&p->timer, now + br->multicast_membership_interval); 742eb1d1641SHerbert Xu out: 743eb1d1641SHerbert Xu err = 0; 744eb1d1641SHerbert Xu 745eb1d1641SHerbert Xu err: 746eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 747eb1d1641SHerbert Xu return err; 748eb1d1641SHerbert Xu } 749eb1d1641SHerbert Xu 7508ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_add_group(struct net_bridge *br, 7518ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 7528ef2a9a5SYOSHIFUJI Hideaki __be32 group) 7538ef2a9a5SYOSHIFUJI Hideaki { 7548ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 7558ef2a9a5SYOSHIFUJI Hideaki 7568ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 7578ef2a9a5SYOSHIFUJI Hideaki return 0; 7588ef2a9a5SYOSHIFUJI Hideaki 7598ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 7608ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 7618ef2a9a5SYOSHIFUJI Hideaki 7628ef2a9a5SYOSHIFUJI Hideaki return br_multicast_add_group(br, port, &br_group); 7638ef2a9a5SYOSHIFUJI Hideaki } 7648ef2a9a5SYOSHIFUJI Hideaki 76508b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 76608b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_add_group(struct net_bridge *br, 76708b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 76808b202b6SYOSHIFUJI Hideaki const struct in6_addr *group) 76908b202b6SYOSHIFUJI Hideaki { 77008b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 77108b202b6SYOSHIFUJI Hideaki 77208b202b6SYOSHIFUJI Hideaki if (ipv6_is_local_multicast(group)) 77308b202b6SYOSHIFUJI Hideaki return 0; 77408b202b6SYOSHIFUJI Hideaki 77508b202b6SYOSHIFUJI Hideaki ipv6_addr_copy(&br_group.u.ip6, group); 77608b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 77708b202b6SYOSHIFUJI Hideaki 77808b202b6SYOSHIFUJI Hideaki return br_multicast_add_group(br, port, &br_group); 77908b202b6SYOSHIFUJI Hideaki } 78008b202b6SYOSHIFUJI Hideaki #endif 78108b202b6SYOSHIFUJI Hideaki 782eb1d1641SHerbert Xu static void br_multicast_router_expired(unsigned long data) 783eb1d1641SHerbert Xu { 784eb1d1641SHerbert Xu struct net_bridge_port *port = (void *)data; 785eb1d1641SHerbert Xu struct net_bridge *br = port->br; 786eb1d1641SHerbert Xu 787eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 788eb1d1641SHerbert Xu if (port->multicast_router != 1 || 789eb1d1641SHerbert Xu timer_pending(&port->multicast_router_timer) || 790eb1d1641SHerbert Xu hlist_unhashed(&port->rlist)) 791eb1d1641SHerbert Xu goto out; 792eb1d1641SHerbert Xu 793eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 794eb1d1641SHerbert Xu 795eb1d1641SHerbert Xu out: 796eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 797eb1d1641SHerbert Xu } 798eb1d1641SHerbert Xu 799eb1d1641SHerbert Xu static void br_multicast_local_router_expired(unsigned long data) 800eb1d1641SHerbert Xu { 801eb1d1641SHerbert Xu } 802eb1d1641SHerbert Xu 8038ef2a9a5SYOSHIFUJI Hideaki static void __br_multicast_send_query(struct net_bridge *br, 8048ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 8058ef2a9a5SYOSHIFUJI Hideaki struct br_ip *ip) 806eb1d1641SHerbert Xu { 807eb1d1641SHerbert Xu struct sk_buff *skb; 808eb1d1641SHerbert Xu 8098ef2a9a5SYOSHIFUJI Hideaki skb = br_multicast_alloc_query(br, ip); 810eb1d1641SHerbert Xu if (!skb) 8118ef2a9a5SYOSHIFUJI Hideaki return; 812eb1d1641SHerbert Xu 813eb1d1641SHerbert Xu if (port) { 814eb1d1641SHerbert Xu __skb_push(skb, sizeof(struct ethhdr)); 815eb1d1641SHerbert Xu skb->dev = port->dev; 816eb1d1641SHerbert Xu NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev, 817eb1d1641SHerbert Xu dev_queue_xmit); 818eb1d1641SHerbert Xu } else 819eb1d1641SHerbert Xu netif_rx(skb); 8208ef2a9a5SYOSHIFUJI Hideaki } 821eb1d1641SHerbert Xu 8228ef2a9a5SYOSHIFUJI Hideaki static void br_multicast_send_query(struct net_bridge *br, 8238ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, u32 sent) 8248ef2a9a5SYOSHIFUJI Hideaki { 8258ef2a9a5SYOSHIFUJI Hideaki unsigned long time; 8268ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 8278ef2a9a5SYOSHIFUJI Hideaki 8288ef2a9a5SYOSHIFUJI Hideaki if (!netif_running(br->dev) || br->multicast_disabled || 8298ef2a9a5SYOSHIFUJI Hideaki timer_pending(&br->multicast_querier_timer)) 8308ef2a9a5SYOSHIFUJI Hideaki return; 8318ef2a9a5SYOSHIFUJI Hideaki 83208b202b6SYOSHIFUJI Hideaki memset(&br_group.u, 0, sizeof(br_group.u)); 8338ef2a9a5SYOSHIFUJI Hideaki 83408b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 8358ef2a9a5SYOSHIFUJI Hideaki __br_multicast_send_query(br, port, &br_group); 8368ef2a9a5SYOSHIFUJI Hideaki 83708b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 83808b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 83908b202b6SYOSHIFUJI Hideaki __br_multicast_send_query(br, port, &br_group); 84008b202b6SYOSHIFUJI Hideaki #endif 84108b202b6SYOSHIFUJI Hideaki 842eb1d1641SHerbert Xu time = jiffies; 843eb1d1641SHerbert Xu time += sent < br->multicast_startup_query_count ? 844eb1d1641SHerbert Xu br->multicast_startup_query_interval : 845eb1d1641SHerbert Xu br->multicast_query_interval; 846eb1d1641SHerbert Xu mod_timer(port ? &port->multicast_query_timer : 847eb1d1641SHerbert Xu &br->multicast_query_timer, time); 848eb1d1641SHerbert Xu } 849eb1d1641SHerbert Xu 850eb1d1641SHerbert Xu static void br_multicast_port_query_expired(unsigned long data) 851eb1d1641SHerbert Xu { 852eb1d1641SHerbert Xu struct net_bridge_port *port = (void *)data; 853eb1d1641SHerbert Xu struct net_bridge *br = port->br; 854eb1d1641SHerbert Xu 855eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 85602a780c0SDan Carpenter if (port->state == BR_STATE_DISABLED || 85702a780c0SDan Carpenter port->state == BR_STATE_BLOCKING) 858eb1d1641SHerbert Xu goto out; 859eb1d1641SHerbert Xu 860eb1d1641SHerbert Xu if (port->multicast_startup_queries_sent < 861eb1d1641SHerbert Xu br->multicast_startup_query_count) 862eb1d1641SHerbert Xu port->multicast_startup_queries_sent++; 863eb1d1641SHerbert Xu 864eb1d1641SHerbert Xu br_multicast_send_query(port->br, port, 865eb1d1641SHerbert Xu port->multicast_startup_queries_sent); 866eb1d1641SHerbert Xu 867eb1d1641SHerbert Xu out: 868eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 869eb1d1641SHerbert Xu } 870eb1d1641SHerbert Xu 871eb1d1641SHerbert Xu void br_multicast_add_port(struct net_bridge_port *port) 872eb1d1641SHerbert Xu { 873eb1d1641SHerbert Xu port->multicast_router = 1; 874eb1d1641SHerbert Xu 875eb1d1641SHerbert Xu setup_timer(&port->multicast_router_timer, br_multicast_router_expired, 876eb1d1641SHerbert Xu (unsigned long)port); 877eb1d1641SHerbert Xu setup_timer(&port->multicast_query_timer, 878eb1d1641SHerbert Xu br_multicast_port_query_expired, (unsigned long)port); 879eb1d1641SHerbert Xu } 880eb1d1641SHerbert Xu 881eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port) 882eb1d1641SHerbert Xu { 883eb1d1641SHerbert Xu del_timer_sync(&port->multicast_router_timer); 884eb1d1641SHerbert Xu } 885eb1d1641SHerbert Xu 886561f1103SHerbert Xu static void __br_multicast_enable_port(struct net_bridge_port *port) 887561f1103SHerbert Xu { 888561f1103SHerbert Xu port->multicast_startup_queries_sent = 0; 889561f1103SHerbert Xu 890561f1103SHerbert Xu if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 || 891561f1103SHerbert Xu del_timer(&port->multicast_query_timer)) 892561f1103SHerbert Xu mod_timer(&port->multicast_query_timer, jiffies); 893561f1103SHerbert Xu } 894561f1103SHerbert Xu 895eb1d1641SHerbert Xu void br_multicast_enable_port(struct net_bridge_port *port) 896eb1d1641SHerbert Xu { 897eb1d1641SHerbert Xu struct net_bridge *br = port->br; 898eb1d1641SHerbert Xu 899eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 900eb1d1641SHerbert Xu if (br->multicast_disabled || !netif_running(br->dev)) 901eb1d1641SHerbert Xu goto out; 902eb1d1641SHerbert Xu 903561f1103SHerbert Xu __br_multicast_enable_port(port); 904eb1d1641SHerbert Xu 905eb1d1641SHerbert Xu out: 906eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 907eb1d1641SHerbert Xu } 908eb1d1641SHerbert Xu 909eb1d1641SHerbert Xu void br_multicast_disable_port(struct net_bridge_port *port) 910eb1d1641SHerbert Xu { 911eb1d1641SHerbert Xu struct net_bridge *br = port->br; 912eb1d1641SHerbert Xu struct net_bridge_port_group *pg; 913eb1d1641SHerbert Xu struct hlist_node *p, *n; 914eb1d1641SHerbert Xu 915eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 916eb1d1641SHerbert Xu hlist_for_each_entry_safe(pg, p, n, &port->mglist, mglist) 917eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 918eb1d1641SHerbert Xu 919eb1d1641SHerbert Xu if (!hlist_unhashed(&port->rlist)) 920eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 921eb1d1641SHerbert Xu del_timer(&port->multicast_router_timer); 922eb1d1641SHerbert Xu del_timer(&port->multicast_query_timer); 923eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 924eb1d1641SHerbert Xu } 925eb1d1641SHerbert Xu 9268ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_igmp3_report(struct net_bridge *br, 927eb1d1641SHerbert Xu struct net_bridge_port *port, 928eb1d1641SHerbert Xu struct sk_buff *skb) 929eb1d1641SHerbert Xu { 930eb1d1641SHerbert Xu struct igmpv3_report *ih; 931eb1d1641SHerbert Xu struct igmpv3_grec *grec; 932eb1d1641SHerbert Xu int i; 933eb1d1641SHerbert Xu int len; 934eb1d1641SHerbert Xu int num; 935eb1d1641SHerbert Xu int type; 936eb1d1641SHerbert Xu int err = 0; 937eb1d1641SHerbert Xu __be32 group; 938eb1d1641SHerbert Xu 939eb1d1641SHerbert Xu if (!pskb_may_pull(skb, sizeof(*ih))) 940eb1d1641SHerbert Xu return -EINVAL; 941eb1d1641SHerbert Xu 942eb1d1641SHerbert Xu ih = igmpv3_report_hdr(skb); 943eb1d1641SHerbert Xu num = ntohs(ih->ngrec); 944eb1d1641SHerbert Xu len = sizeof(*ih); 945eb1d1641SHerbert Xu 946eb1d1641SHerbert Xu for (i = 0; i < num; i++) { 947eb1d1641SHerbert Xu len += sizeof(*grec); 948eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 949eb1d1641SHerbert Xu return -EINVAL; 950eb1d1641SHerbert Xu 951fd218cf9SHerbert Xu grec = (void *)(skb->data + len - sizeof(*grec)); 952eb1d1641SHerbert Xu group = grec->grec_mca; 953eb1d1641SHerbert Xu type = grec->grec_type; 954eb1d1641SHerbert Xu 9558eabf95cSEric Dumazet len += ntohs(grec->grec_nsrcs) * 4; 956eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 957eb1d1641SHerbert Xu return -EINVAL; 958eb1d1641SHerbert Xu 959eb1d1641SHerbert Xu /* We treat this as an IGMPv2 report for now. */ 960eb1d1641SHerbert Xu switch (type) { 961eb1d1641SHerbert Xu case IGMPV3_MODE_IS_INCLUDE: 962eb1d1641SHerbert Xu case IGMPV3_MODE_IS_EXCLUDE: 963eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_INCLUDE: 964eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_EXCLUDE: 965eb1d1641SHerbert Xu case IGMPV3_ALLOW_NEW_SOURCES: 966eb1d1641SHerbert Xu case IGMPV3_BLOCK_OLD_SOURCES: 967eb1d1641SHerbert Xu break; 968eb1d1641SHerbert Xu 969eb1d1641SHerbert Xu default: 970eb1d1641SHerbert Xu continue; 971eb1d1641SHerbert Xu } 972eb1d1641SHerbert Xu 9738ef2a9a5SYOSHIFUJI Hideaki err = br_ip4_multicast_add_group(br, port, group); 974eb1d1641SHerbert Xu if (err) 975eb1d1641SHerbert Xu break; 976eb1d1641SHerbert Xu } 977eb1d1641SHerbert Xu 978eb1d1641SHerbert Xu return err; 979eb1d1641SHerbert Xu } 980eb1d1641SHerbert Xu 98108b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 98208b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_mld2_report(struct net_bridge *br, 98308b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 98408b202b6SYOSHIFUJI Hideaki struct sk_buff *skb) 98508b202b6SYOSHIFUJI Hideaki { 98608b202b6SYOSHIFUJI Hideaki struct icmp6hdr *icmp6h; 98708b202b6SYOSHIFUJI Hideaki struct mld2_grec *grec; 98808b202b6SYOSHIFUJI Hideaki int i; 98908b202b6SYOSHIFUJI Hideaki int len; 99008b202b6SYOSHIFUJI Hideaki int num; 99108b202b6SYOSHIFUJI Hideaki int err = 0; 99208b202b6SYOSHIFUJI Hideaki 99308b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*icmp6h))) 99408b202b6SYOSHIFUJI Hideaki return -EINVAL; 99508b202b6SYOSHIFUJI Hideaki 99608b202b6SYOSHIFUJI Hideaki icmp6h = icmp6_hdr(skb); 99708b202b6SYOSHIFUJI Hideaki num = ntohs(icmp6h->icmp6_dataun.un_data16[1]); 99808b202b6SYOSHIFUJI Hideaki len = sizeof(*icmp6h); 99908b202b6SYOSHIFUJI Hideaki 100008b202b6SYOSHIFUJI Hideaki for (i = 0; i < num; i++) { 100108b202b6SYOSHIFUJI Hideaki __be16 *nsrcs, _nsrcs; 100208b202b6SYOSHIFUJI Hideaki 100308b202b6SYOSHIFUJI Hideaki nsrcs = skb_header_pointer(skb, 100408b202b6SYOSHIFUJI Hideaki len + offsetof(struct mld2_grec, 100508b202b6SYOSHIFUJI Hideaki grec_mca), 100608b202b6SYOSHIFUJI Hideaki sizeof(_nsrcs), &_nsrcs); 100708b202b6SYOSHIFUJI Hideaki if (!nsrcs) 100808b202b6SYOSHIFUJI Hideaki return -EINVAL; 100908b202b6SYOSHIFUJI Hideaki 101008b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, 101108b202b6SYOSHIFUJI Hideaki len + sizeof(*grec) + 101208b202b6SYOSHIFUJI Hideaki sizeof(struct in6_addr) * (*nsrcs))) 101308b202b6SYOSHIFUJI Hideaki return -EINVAL; 101408b202b6SYOSHIFUJI Hideaki 101508b202b6SYOSHIFUJI Hideaki grec = (struct mld2_grec *)(skb->data + len); 101608b202b6SYOSHIFUJI Hideaki len += sizeof(*grec) + sizeof(struct in6_addr) * (*nsrcs); 101708b202b6SYOSHIFUJI Hideaki 101808b202b6SYOSHIFUJI Hideaki /* We treat these as MLDv1 reports for now. */ 101908b202b6SYOSHIFUJI Hideaki switch (grec->grec_type) { 102008b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_INCLUDE: 102108b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_EXCLUDE: 102208b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_INCLUDE: 102308b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_EXCLUDE: 102408b202b6SYOSHIFUJI Hideaki case MLD2_ALLOW_NEW_SOURCES: 102508b202b6SYOSHIFUJI Hideaki case MLD2_BLOCK_OLD_SOURCES: 102608b202b6SYOSHIFUJI Hideaki break; 102708b202b6SYOSHIFUJI Hideaki 102808b202b6SYOSHIFUJI Hideaki default: 102908b202b6SYOSHIFUJI Hideaki continue; 103008b202b6SYOSHIFUJI Hideaki } 103108b202b6SYOSHIFUJI Hideaki 103208b202b6SYOSHIFUJI Hideaki err = br_ip6_multicast_add_group(br, port, &grec->grec_mca); 103308b202b6SYOSHIFUJI Hideaki if (!err) 103408b202b6SYOSHIFUJI Hideaki break; 103508b202b6SYOSHIFUJI Hideaki } 103608b202b6SYOSHIFUJI Hideaki 103708b202b6SYOSHIFUJI Hideaki return err; 103808b202b6SYOSHIFUJI Hideaki } 103908b202b6SYOSHIFUJI Hideaki #endif 104008b202b6SYOSHIFUJI Hideaki 10410909e117SHerbert Xu static void br_multicast_add_router(struct net_bridge *br, 10420909e117SHerbert Xu struct net_bridge_port *port) 10430909e117SHerbert Xu { 10440909e117SHerbert Xu struct hlist_node *p; 10450909e117SHerbert Xu struct hlist_node **h; 10460909e117SHerbert Xu 10470909e117SHerbert Xu for (h = &br->router_list.first; 10480909e117SHerbert Xu (p = *h) && 10490909e117SHerbert Xu (unsigned long)container_of(p, struct net_bridge_port, rlist) > 10500909e117SHerbert Xu (unsigned long)port; 10510909e117SHerbert Xu h = &p->next) 10520909e117SHerbert Xu ; 10530909e117SHerbert Xu 10540909e117SHerbert Xu port->rlist.pprev = h; 10550909e117SHerbert Xu port->rlist.next = p; 10560909e117SHerbert Xu rcu_assign_pointer(*h, &port->rlist); 10570909e117SHerbert Xu if (p) 10580909e117SHerbert Xu p->pprev = &port->rlist.next; 10590909e117SHerbert Xu } 10600909e117SHerbert Xu 1061eb1d1641SHerbert Xu static void br_multicast_mark_router(struct net_bridge *br, 1062eb1d1641SHerbert Xu struct net_bridge_port *port) 1063eb1d1641SHerbert Xu { 1064eb1d1641SHerbert Xu unsigned long now = jiffies; 1065eb1d1641SHerbert Xu 1066eb1d1641SHerbert Xu if (!port) { 1067eb1d1641SHerbert Xu if (br->multicast_router == 1) 1068eb1d1641SHerbert Xu mod_timer(&br->multicast_router_timer, 1069eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1070eb1d1641SHerbert Xu return; 1071eb1d1641SHerbert Xu } 1072eb1d1641SHerbert Xu 1073eb1d1641SHerbert Xu if (port->multicast_router != 1) 1074eb1d1641SHerbert Xu return; 1075eb1d1641SHerbert Xu 1076eb1d1641SHerbert Xu if (!hlist_unhashed(&port->rlist)) 1077eb1d1641SHerbert Xu goto timer; 1078eb1d1641SHerbert Xu 10790909e117SHerbert Xu br_multicast_add_router(br, port); 1080eb1d1641SHerbert Xu 1081eb1d1641SHerbert Xu timer: 1082eb1d1641SHerbert Xu mod_timer(&port->multicast_router_timer, 1083eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1084eb1d1641SHerbert Xu } 1085eb1d1641SHerbert Xu 1086eb1d1641SHerbert Xu static void br_multicast_query_received(struct net_bridge *br, 1087eb1d1641SHerbert Xu struct net_bridge_port *port, 10888ef2a9a5SYOSHIFUJI Hideaki int saddr) 1089eb1d1641SHerbert Xu { 1090eb1d1641SHerbert Xu if (saddr) 1091eb1d1641SHerbert Xu mod_timer(&br->multicast_querier_timer, 1092eb1d1641SHerbert Xu jiffies + br->multicast_querier_interval); 1093eb1d1641SHerbert Xu else if (timer_pending(&br->multicast_querier_timer)) 1094eb1d1641SHerbert Xu return; 1095eb1d1641SHerbert Xu 1096eb1d1641SHerbert Xu br_multicast_mark_router(br, port); 1097eb1d1641SHerbert Xu } 1098eb1d1641SHerbert Xu 10998ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_query(struct net_bridge *br, 1100eb1d1641SHerbert Xu struct net_bridge_port *port, 1101eb1d1641SHerbert Xu struct sk_buff *skb) 1102eb1d1641SHerbert Xu { 1103eb1d1641SHerbert Xu struct iphdr *iph = ip_hdr(skb); 1104eb1d1641SHerbert Xu struct igmphdr *ih = igmp_hdr(skb); 1105eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1106eb1d1641SHerbert Xu struct igmpv3_query *ih3; 1107eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1108eb1d1641SHerbert Xu struct net_bridge_port_group **pp; 1109eb1d1641SHerbert Xu unsigned long max_delay; 1110eb1d1641SHerbert Xu unsigned long now = jiffies; 1111eb1d1641SHerbert Xu __be32 group; 1112bec68ff1SYOSHIFUJI Hideaki int err = 0; 1113eb1d1641SHerbert Xu 1114eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1115eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1116eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 1117eb1d1641SHerbert Xu goto out; 1118eb1d1641SHerbert Xu 11198ef2a9a5SYOSHIFUJI Hideaki br_multicast_query_received(br, port, !!iph->saddr); 1120eb1d1641SHerbert Xu 1121eb1d1641SHerbert Xu group = ih->group; 1122eb1d1641SHerbert Xu 1123eb1d1641SHerbert Xu if (skb->len == sizeof(*ih)) { 1124eb1d1641SHerbert Xu max_delay = ih->code * (HZ / IGMP_TIMER_SCALE); 1125eb1d1641SHerbert Xu 1126eb1d1641SHerbert Xu if (!max_delay) { 1127eb1d1641SHerbert Xu max_delay = 10 * HZ; 1128eb1d1641SHerbert Xu group = 0; 1129eb1d1641SHerbert Xu } 1130eb1d1641SHerbert Xu } else { 1131bec68ff1SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) { 1132bec68ff1SYOSHIFUJI Hideaki err = -EINVAL; 1133bec68ff1SYOSHIFUJI Hideaki goto out; 1134bec68ff1SYOSHIFUJI Hideaki } 1135eb1d1641SHerbert Xu 1136eb1d1641SHerbert Xu ih3 = igmpv3_query_hdr(skb); 1137eb1d1641SHerbert Xu if (ih3->nsrcs) 1138bec68ff1SYOSHIFUJI Hideaki goto out; 1139eb1d1641SHerbert Xu 11400ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 max_delay = ih3->code ? 11410ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1; 1142eb1d1641SHerbert Xu } 1143eb1d1641SHerbert Xu 1144eb1d1641SHerbert Xu if (!group) 1145eb1d1641SHerbert Xu goto out; 1146eb1d1641SHerbert Xu 11478ef2a9a5SYOSHIFUJI Hideaki mp = br_mdb_ip4_get(br->mdb, group); 1148eb1d1641SHerbert Xu if (!mp) 1149eb1d1641SHerbert Xu goto out; 1150eb1d1641SHerbert Xu 1151eb1d1641SHerbert Xu max_delay *= br->multicast_last_member_count; 1152eb1d1641SHerbert Xu 1153eb1d1641SHerbert Xu if (!hlist_unhashed(&mp->mglist) && 1154eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1155eb1d1641SHerbert Xu time_after(mp->timer.expires, now + max_delay) : 1156eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) 1157eb1d1641SHerbert Xu mod_timer(&mp->timer, now + max_delay); 1158eb1d1641SHerbert Xu 1159eb1d1641SHerbert Xu for (pp = &mp->ports; (p = *pp); pp = &p->next) { 1160eb1d1641SHerbert Xu if (timer_pending(&p->timer) ? 1161eb1d1641SHerbert Xu time_after(p->timer.expires, now + max_delay) : 1162eb1d1641SHerbert Xu try_to_del_timer_sync(&p->timer) >= 0) 1163eb1d1641SHerbert Xu mod_timer(&mp->timer, now + max_delay); 1164eb1d1641SHerbert Xu } 1165eb1d1641SHerbert Xu 1166eb1d1641SHerbert Xu out: 1167eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1168bec68ff1SYOSHIFUJI Hideaki return err; 1169eb1d1641SHerbert Xu } 1170eb1d1641SHerbert Xu 117108b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 117208b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_query(struct net_bridge *br, 117308b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 117408b202b6SYOSHIFUJI Hideaki struct sk_buff *skb) 117508b202b6SYOSHIFUJI Hideaki { 117608b202b6SYOSHIFUJI Hideaki struct ipv6hdr *ip6h = ipv6_hdr(skb); 117708b202b6SYOSHIFUJI Hideaki struct mld_msg *mld = (struct mld_msg *) icmp6_hdr(skb); 117808b202b6SYOSHIFUJI Hideaki struct net_bridge_mdb_entry *mp; 117908b202b6SYOSHIFUJI Hideaki struct mld2_query *mld2q; 118008b202b6SYOSHIFUJI Hideaki struct net_bridge_port_group *p, **pp; 118108b202b6SYOSHIFUJI Hideaki unsigned long max_delay; 118208b202b6SYOSHIFUJI Hideaki unsigned long now = jiffies; 118308b202b6SYOSHIFUJI Hideaki struct in6_addr *group = NULL; 118408b202b6SYOSHIFUJI Hideaki int err = 0; 118508b202b6SYOSHIFUJI Hideaki 118608b202b6SYOSHIFUJI Hideaki spin_lock(&br->multicast_lock); 118708b202b6SYOSHIFUJI Hideaki if (!netif_running(br->dev) || 118808b202b6SYOSHIFUJI Hideaki (port && port->state == BR_STATE_DISABLED)) 118908b202b6SYOSHIFUJI Hideaki goto out; 119008b202b6SYOSHIFUJI Hideaki 119108b202b6SYOSHIFUJI Hideaki br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->saddr)); 119208b202b6SYOSHIFUJI Hideaki 119308b202b6SYOSHIFUJI Hideaki if (skb->len == sizeof(*mld)) { 119408b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*mld))) { 119508b202b6SYOSHIFUJI Hideaki err = -EINVAL; 119608b202b6SYOSHIFUJI Hideaki goto out; 119708b202b6SYOSHIFUJI Hideaki } 119808b202b6SYOSHIFUJI Hideaki mld = (struct mld_msg *) icmp6_hdr(skb); 119908b202b6SYOSHIFUJI Hideaki max_delay = msecs_to_jiffies(htons(mld->mld_maxdelay)); 120008b202b6SYOSHIFUJI Hideaki if (max_delay) 120108b202b6SYOSHIFUJI Hideaki group = &mld->mld_mca; 120208b202b6SYOSHIFUJI Hideaki } else if (skb->len >= sizeof(*mld2q)) { 120308b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*mld2q))) { 120408b202b6SYOSHIFUJI Hideaki err = -EINVAL; 120508b202b6SYOSHIFUJI Hideaki goto out; 120608b202b6SYOSHIFUJI Hideaki } 120708b202b6SYOSHIFUJI Hideaki mld2q = (struct mld2_query *)icmp6_hdr(skb); 120808b202b6SYOSHIFUJI Hideaki if (!mld2q->mld2q_nsrcs) 120908b202b6SYOSHIFUJI Hideaki group = &mld2q->mld2q_mca; 121008b202b6SYOSHIFUJI Hideaki max_delay = mld2q->mld2q_mrc ? MLDV2_MRC(mld2q->mld2q_mrc) : 1; 121108b202b6SYOSHIFUJI Hideaki } 121208b202b6SYOSHIFUJI Hideaki 121308b202b6SYOSHIFUJI Hideaki if (!group) 121408b202b6SYOSHIFUJI Hideaki goto out; 121508b202b6SYOSHIFUJI Hideaki 121608b202b6SYOSHIFUJI Hideaki mp = br_mdb_ip6_get(br->mdb, group); 121708b202b6SYOSHIFUJI Hideaki if (!mp) 121808b202b6SYOSHIFUJI Hideaki goto out; 121908b202b6SYOSHIFUJI Hideaki 122008b202b6SYOSHIFUJI Hideaki max_delay *= br->multicast_last_member_count; 122108b202b6SYOSHIFUJI Hideaki if (!hlist_unhashed(&mp->mglist) && 122208b202b6SYOSHIFUJI Hideaki (timer_pending(&mp->timer) ? 122308b202b6SYOSHIFUJI Hideaki time_after(mp->timer.expires, now + max_delay) : 122408b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&mp->timer) >= 0)) 122508b202b6SYOSHIFUJI Hideaki mod_timer(&mp->timer, now + max_delay); 122608b202b6SYOSHIFUJI Hideaki 122708b202b6SYOSHIFUJI Hideaki for (pp = &mp->ports; (p = *pp); pp = &p->next) { 122808b202b6SYOSHIFUJI Hideaki if (timer_pending(&p->timer) ? 122908b202b6SYOSHIFUJI Hideaki time_after(p->timer.expires, now + max_delay) : 123008b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&p->timer) >= 0) 123108b202b6SYOSHIFUJI Hideaki mod_timer(&mp->timer, now + max_delay); 123208b202b6SYOSHIFUJI Hideaki } 123308b202b6SYOSHIFUJI Hideaki 123408b202b6SYOSHIFUJI Hideaki out: 123508b202b6SYOSHIFUJI Hideaki spin_unlock(&br->multicast_lock); 123608b202b6SYOSHIFUJI Hideaki return err; 123708b202b6SYOSHIFUJI Hideaki } 123808b202b6SYOSHIFUJI Hideaki #endif 123908b202b6SYOSHIFUJI Hideaki 1240eb1d1641SHerbert Xu static void br_multicast_leave_group(struct net_bridge *br, 1241eb1d1641SHerbert Xu struct net_bridge_port *port, 12428ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group) 1243eb1d1641SHerbert Xu { 1244eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 1245eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1246eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1247eb1d1641SHerbert Xu unsigned long now; 1248eb1d1641SHerbert Xu unsigned long time; 1249eb1d1641SHerbert Xu 1250eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1251eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1252eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED) || 1253eb1d1641SHerbert Xu timer_pending(&br->multicast_querier_timer)) 1254eb1d1641SHerbert Xu goto out; 1255eb1d1641SHerbert Xu 1256eb1d1641SHerbert Xu mdb = br->mdb; 1257eb1d1641SHerbert Xu mp = br_mdb_ip_get(mdb, group); 1258eb1d1641SHerbert Xu if (!mp) 1259eb1d1641SHerbert Xu goto out; 1260eb1d1641SHerbert Xu 1261eb1d1641SHerbert Xu now = jiffies; 1262eb1d1641SHerbert Xu time = now + br->multicast_last_member_count * 1263eb1d1641SHerbert Xu br->multicast_last_member_interval; 1264eb1d1641SHerbert Xu 1265eb1d1641SHerbert Xu if (!port) { 1266eb1d1641SHerbert Xu if (!hlist_unhashed(&mp->mglist) && 1267eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1268eb1d1641SHerbert Xu time_after(mp->timer.expires, time) : 1269eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) { 1270eb1d1641SHerbert Xu mod_timer(&mp->timer, time); 1271eb1d1641SHerbert Xu 1272eb1d1641SHerbert Xu mp->queries_sent = 0; 1273eb1d1641SHerbert Xu mod_timer(&mp->query_timer, now); 1274eb1d1641SHerbert Xu } 1275eb1d1641SHerbert Xu 1276eb1d1641SHerbert Xu goto out; 1277eb1d1641SHerbert Xu } 1278eb1d1641SHerbert Xu 1279eb1d1641SHerbert Xu for (p = mp->ports; p; p = p->next) { 1280eb1d1641SHerbert Xu if (p->port != port) 1281eb1d1641SHerbert Xu continue; 1282eb1d1641SHerbert Xu 1283eb1d1641SHerbert Xu if (!hlist_unhashed(&p->mglist) && 1284eb1d1641SHerbert Xu (timer_pending(&p->timer) ? 1285eb1d1641SHerbert Xu time_after(p->timer.expires, time) : 1286eb1d1641SHerbert Xu try_to_del_timer_sync(&p->timer) >= 0)) { 1287eb1d1641SHerbert Xu mod_timer(&p->timer, time); 1288eb1d1641SHerbert Xu 1289eb1d1641SHerbert Xu p->queries_sent = 0; 1290eb1d1641SHerbert Xu mod_timer(&p->query_timer, now); 1291eb1d1641SHerbert Xu } 1292eb1d1641SHerbert Xu 1293eb1d1641SHerbert Xu break; 1294eb1d1641SHerbert Xu } 1295eb1d1641SHerbert Xu 1296eb1d1641SHerbert Xu out: 1297eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1298eb1d1641SHerbert Xu } 1299eb1d1641SHerbert Xu 13008ef2a9a5SYOSHIFUJI Hideaki static void br_ip4_multicast_leave_group(struct net_bridge *br, 13018ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 13028ef2a9a5SYOSHIFUJI Hideaki __be32 group) 13038ef2a9a5SYOSHIFUJI Hideaki { 13048ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 13058ef2a9a5SYOSHIFUJI Hideaki 13068ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 13078ef2a9a5SYOSHIFUJI Hideaki return; 13088ef2a9a5SYOSHIFUJI Hideaki 13098ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 13108ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 13118ef2a9a5SYOSHIFUJI Hideaki 13128ef2a9a5SYOSHIFUJI Hideaki br_multicast_leave_group(br, port, &br_group); 13138ef2a9a5SYOSHIFUJI Hideaki } 13148ef2a9a5SYOSHIFUJI Hideaki 131508b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 131608b202b6SYOSHIFUJI Hideaki static void br_ip6_multicast_leave_group(struct net_bridge *br, 131708b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 131808b202b6SYOSHIFUJI Hideaki const struct in6_addr *group) 131908b202b6SYOSHIFUJI Hideaki { 132008b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 132108b202b6SYOSHIFUJI Hideaki 132208b202b6SYOSHIFUJI Hideaki if (ipv6_is_local_multicast(group)) 132308b202b6SYOSHIFUJI Hideaki return; 132408b202b6SYOSHIFUJI Hideaki 132508b202b6SYOSHIFUJI Hideaki ipv6_addr_copy(&br_group.u.ip6, group); 132608b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 132708b202b6SYOSHIFUJI Hideaki 132808b202b6SYOSHIFUJI Hideaki br_multicast_leave_group(br, port, &br_group); 132908b202b6SYOSHIFUJI Hideaki } 133008b202b6SYOSHIFUJI Hideaki #endif 13318ef2a9a5SYOSHIFUJI Hideaki 1332eb1d1641SHerbert Xu static int br_multicast_ipv4_rcv(struct net_bridge *br, 1333eb1d1641SHerbert Xu struct net_bridge_port *port, 1334eb1d1641SHerbert Xu struct sk_buff *skb) 1335eb1d1641SHerbert Xu { 1336eb1d1641SHerbert Xu struct sk_buff *skb2 = skb; 1337eb1d1641SHerbert Xu struct iphdr *iph; 1338eb1d1641SHerbert Xu struct igmphdr *ih; 1339eb1d1641SHerbert Xu unsigned len; 1340eb1d1641SHerbert Xu unsigned offset; 1341eb1d1641SHerbert Xu int err; 1342eb1d1641SHerbert Xu 1343eb1d1641SHerbert Xu /* We treat OOM as packet loss for now. */ 1344eb1d1641SHerbert Xu if (!pskb_may_pull(skb, sizeof(*iph))) 1345eb1d1641SHerbert Xu return -EINVAL; 1346eb1d1641SHerbert Xu 1347eb1d1641SHerbert Xu iph = ip_hdr(skb); 1348eb1d1641SHerbert Xu 1349eb1d1641SHerbert Xu if (iph->ihl < 5 || iph->version != 4) 1350eb1d1641SHerbert Xu return -EINVAL; 1351eb1d1641SHerbert Xu 1352eb1d1641SHerbert Xu if (!pskb_may_pull(skb, ip_hdrlen(skb))) 1353eb1d1641SHerbert Xu return -EINVAL; 1354eb1d1641SHerbert Xu 1355eb1d1641SHerbert Xu iph = ip_hdr(skb); 1356eb1d1641SHerbert Xu 1357eb1d1641SHerbert Xu if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl))) 1358eb1d1641SHerbert Xu return -EINVAL; 1359eb1d1641SHerbert Xu 1360eb1d1641SHerbert Xu if (iph->protocol != IPPROTO_IGMP) 1361eb1d1641SHerbert Xu return 0; 1362eb1d1641SHerbert Xu 1363eb1d1641SHerbert Xu len = ntohs(iph->tot_len); 1364eb1d1641SHerbert Xu if (skb->len < len || len < ip_hdrlen(skb)) 1365eb1d1641SHerbert Xu return -EINVAL; 1366eb1d1641SHerbert Xu 1367eb1d1641SHerbert Xu if (skb->len > len) { 1368eb1d1641SHerbert Xu skb2 = skb_clone(skb, GFP_ATOMIC); 1369eb1d1641SHerbert Xu if (!skb2) 1370eb1d1641SHerbert Xu return -ENOMEM; 1371eb1d1641SHerbert Xu 1372eb1d1641SHerbert Xu err = pskb_trim_rcsum(skb2, len); 1373eb1d1641SHerbert Xu if (err) 13748440853bSYOSHIFUJI Hideaki / 吉藤英明 goto err_out; 1375eb1d1641SHerbert Xu } 1376eb1d1641SHerbert Xu 1377eb1d1641SHerbert Xu len -= ip_hdrlen(skb2); 1378eb1d1641SHerbert Xu offset = skb_network_offset(skb2) + ip_hdrlen(skb2); 1379eb1d1641SHerbert Xu __skb_pull(skb2, offset); 1380eb1d1641SHerbert Xu skb_reset_transport_header(skb2); 1381eb1d1641SHerbert Xu 1382eb1d1641SHerbert Xu err = -EINVAL; 1383eb1d1641SHerbert Xu if (!pskb_may_pull(skb2, sizeof(*ih))) 1384eb1d1641SHerbert Xu goto out; 1385eb1d1641SHerbert Xu 1386eb1d1641SHerbert Xu switch (skb2->ip_summed) { 1387eb1d1641SHerbert Xu case CHECKSUM_COMPLETE: 1388eb1d1641SHerbert Xu if (!csum_fold(skb2->csum)) 1389eb1d1641SHerbert Xu break; 1390eb1d1641SHerbert Xu /* fall through */ 1391eb1d1641SHerbert Xu case CHECKSUM_NONE: 1392eb1d1641SHerbert Xu skb2->csum = 0; 1393eb1d1641SHerbert Xu if (skb_checksum_complete(skb2)) 13948440853bSYOSHIFUJI Hideaki / 吉藤英明 goto out; 1395eb1d1641SHerbert Xu } 1396eb1d1641SHerbert Xu 1397eb1d1641SHerbert Xu err = 0; 1398eb1d1641SHerbert Xu 1399eb1d1641SHerbert Xu BR_INPUT_SKB_CB(skb)->igmp = 1; 1400eb1d1641SHerbert Xu ih = igmp_hdr(skb2); 1401eb1d1641SHerbert Xu 1402eb1d1641SHerbert Xu switch (ih->type) { 1403eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_REPORT: 1404eb1d1641SHerbert Xu case IGMPV2_HOST_MEMBERSHIP_REPORT: 1405eb1d1641SHerbert Xu BR_INPUT_SKB_CB(skb2)->mrouters_only = 1; 14068ef2a9a5SYOSHIFUJI Hideaki err = br_ip4_multicast_add_group(br, port, ih->group); 1407eb1d1641SHerbert Xu break; 1408eb1d1641SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 14098ef2a9a5SYOSHIFUJI Hideaki err = br_ip4_multicast_igmp3_report(br, port, skb2); 1410eb1d1641SHerbert Xu break; 1411eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_QUERY: 14128ef2a9a5SYOSHIFUJI Hideaki err = br_ip4_multicast_query(br, port, skb2); 1413eb1d1641SHerbert Xu break; 1414eb1d1641SHerbert Xu case IGMP_HOST_LEAVE_MESSAGE: 14158ef2a9a5SYOSHIFUJI Hideaki br_ip4_multicast_leave_group(br, port, ih->group); 1416eb1d1641SHerbert Xu break; 1417eb1d1641SHerbert Xu } 1418eb1d1641SHerbert Xu 1419eb1d1641SHerbert Xu out: 1420eb1d1641SHerbert Xu __skb_push(skb2, offset); 14218440853bSYOSHIFUJI Hideaki / 吉藤英明 err_out: 1422eb1d1641SHerbert Xu if (skb2 != skb) 1423eb1d1641SHerbert Xu kfree_skb(skb2); 1424eb1d1641SHerbert Xu return err; 1425eb1d1641SHerbert Xu } 1426eb1d1641SHerbert Xu 142708b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 142808b202b6SYOSHIFUJI Hideaki static int br_multicast_ipv6_rcv(struct net_bridge *br, 142908b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 143008b202b6SYOSHIFUJI Hideaki struct sk_buff *skb) 143108b202b6SYOSHIFUJI Hideaki { 143208b202b6SYOSHIFUJI Hideaki struct sk_buff *skb2 = skb; 143308b202b6SYOSHIFUJI Hideaki struct ipv6hdr *ip6h; 143408b202b6SYOSHIFUJI Hideaki struct icmp6hdr *icmp6h; 143508b202b6SYOSHIFUJI Hideaki u8 nexthdr; 143608b202b6SYOSHIFUJI Hideaki unsigned len; 143708b202b6SYOSHIFUJI Hideaki unsigned offset; 143808b202b6SYOSHIFUJI Hideaki int err; 143908b202b6SYOSHIFUJI Hideaki 144008b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*ip6h))) 144108b202b6SYOSHIFUJI Hideaki return -EINVAL; 144208b202b6SYOSHIFUJI Hideaki 144308b202b6SYOSHIFUJI Hideaki ip6h = ipv6_hdr(skb); 144408b202b6SYOSHIFUJI Hideaki 144508b202b6SYOSHIFUJI Hideaki /* 144608b202b6SYOSHIFUJI Hideaki * We're interested in MLD messages only. 144708b202b6SYOSHIFUJI Hideaki * - Version is 6 144808b202b6SYOSHIFUJI Hideaki * - MLD has always Router Alert hop-by-hop option 144908b202b6SYOSHIFUJI Hideaki * - But we do not support jumbrograms. 145008b202b6SYOSHIFUJI Hideaki */ 145108b202b6SYOSHIFUJI Hideaki if (ip6h->version != 6 || 145208b202b6SYOSHIFUJI Hideaki ip6h->nexthdr != IPPROTO_HOPOPTS || 145308b202b6SYOSHIFUJI Hideaki ip6h->payload_len == 0) 145408b202b6SYOSHIFUJI Hideaki return 0; 145508b202b6SYOSHIFUJI Hideaki 145608b202b6SYOSHIFUJI Hideaki len = ntohs(ip6h->payload_len); 145708b202b6SYOSHIFUJI Hideaki if (skb->len < len) 145808b202b6SYOSHIFUJI Hideaki return -EINVAL; 145908b202b6SYOSHIFUJI Hideaki 146008b202b6SYOSHIFUJI Hideaki nexthdr = ip6h->nexthdr; 146108b202b6SYOSHIFUJI Hideaki offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr); 146208b202b6SYOSHIFUJI Hideaki 146308b202b6SYOSHIFUJI Hideaki if (offset < 0 || nexthdr != IPPROTO_ICMPV6) 146408b202b6SYOSHIFUJI Hideaki return 0; 146508b202b6SYOSHIFUJI Hideaki 146608b202b6SYOSHIFUJI Hideaki /* Okay, we found ICMPv6 header */ 146708b202b6SYOSHIFUJI Hideaki skb2 = skb_clone(skb, GFP_ATOMIC); 146808b202b6SYOSHIFUJI Hideaki if (!skb2) 146908b202b6SYOSHIFUJI Hideaki return -ENOMEM; 147008b202b6SYOSHIFUJI Hideaki 147108b202b6SYOSHIFUJI Hideaki len -= offset - skb_network_offset(skb2); 147208b202b6SYOSHIFUJI Hideaki 147308b202b6SYOSHIFUJI Hideaki __skb_pull(skb2, offset); 147408b202b6SYOSHIFUJI Hideaki skb_reset_transport_header(skb2); 147508b202b6SYOSHIFUJI Hideaki 147608b202b6SYOSHIFUJI Hideaki err = -EINVAL; 147708b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb2, sizeof(*icmp6h))) 147808b202b6SYOSHIFUJI Hideaki goto out; 147908b202b6SYOSHIFUJI Hideaki 148008b202b6SYOSHIFUJI Hideaki icmp6h = icmp6_hdr(skb2); 148108b202b6SYOSHIFUJI Hideaki 148208b202b6SYOSHIFUJI Hideaki switch (icmp6h->icmp6_type) { 148308b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 148408b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 148508b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 148608b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 148708b202b6SYOSHIFUJI Hideaki break; 148808b202b6SYOSHIFUJI Hideaki default: 148908b202b6SYOSHIFUJI Hideaki err = 0; 149008b202b6SYOSHIFUJI Hideaki goto out; 149108b202b6SYOSHIFUJI Hideaki } 149208b202b6SYOSHIFUJI Hideaki 149308b202b6SYOSHIFUJI Hideaki /* Okay, we found MLD message. Check further. */ 149408b202b6SYOSHIFUJI Hideaki if (skb2->len > len) { 149508b202b6SYOSHIFUJI Hideaki err = pskb_trim_rcsum(skb2, len); 149608b202b6SYOSHIFUJI Hideaki if (err) 149708b202b6SYOSHIFUJI Hideaki goto out; 149808b202b6SYOSHIFUJI Hideaki } 149908b202b6SYOSHIFUJI Hideaki 150008b202b6SYOSHIFUJI Hideaki switch (skb2->ip_summed) { 150108b202b6SYOSHIFUJI Hideaki case CHECKSUM_COMPLETE: 150208b202b6SYOSHIFUJI Hideaki if (!csum_fold(skb2->csum)) 150308b202b6SYOSHIFUJI Hideaki break; 150408b202b6SYOSHIFUJI Hideaki /*FALLTHROUGH*/ 150508b202b6SYOSHIFUJI Hideaki case CHECKSUM_NONE: 150608b202b6SYOSHIFUJI Hideaki skb2->csum = 0; 150708b202b6SYOSHIFUJI Hideaki if (skb_checksum_complete(skb2)) 150808b202b6SYOSHIFUJI Hideaki goto out; 150908b202b6SYOSHIFUJI Hideaki } 151008b202b6SYOSHIFUJI Hideaki 151108b202b6SYOSHIFUJI Hideaki err = 0; 151208b202b6SYOSHIFUJI Hideaki 151308b202b6SYOSHIFUJI Hideaki BR_INPUT_SKB_CB(skb)->igmp = 1; 151408b202b6SYOSHIFUJI Hideaki 151508b202b6SYOSHIFUJI Hideaki switch (icmp6h->icmp6_type) { 151608b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 151708b202b6SYOSHIFUJI Hideaki { 151808b202b6SYOSHIFUJI Hideaki struct mld_msg *mld = (struct mld_msg *)icmp6h; 151908b202b6SYOSHIFUJI Hideaki BR_INPUT_SKB_CB(skb2)->mrouters_only = 1; 152008b202b6SYOSHIFUJI Hideaki err = br_ip6_multicast_add_group(br, port, &mld->mld_mca); 152108b202b6SYOSHIFUJI Hideaki break; 152208b202b6SYOSHIFUJI Hideaki } 152308b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 152408b202b6SYOSHIFUJI Hideaki err = br_ip6_multicast_mld2_report(br, port, skb2); 152508b202b6SYOSHIFUJI Hideaki break; 152608b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 152708b202b6SYOSHIFUJI Hideaki err = br_ip6_multicast_query(br, port, skb2); 152808b202b6SYOSHIFUJI Hideaki break; 152908b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 153008b202b6SYOSHIFUJI Hideaki { 153108b202b6SYOSHIFUJI Hideaki struct mld_msg *mld = (struct mld_msg *)icmp6h; 153208b202b6SYOSHIFUJI Hideaki br_ip6_multicast_leave_group(br, port, &mld->mld_mca); 153308b202b6SYOSHIFUJI Hideaki } 153408b202b6SYOSHIFUJI Hideaki } 153508b202b6SYOSHIFUJI Hideaki 153608b202b6SYOSHIFUJI Hideaki out: 153708b202b6SYOSHIFUJI Hideaki __skb_push(skb2, offset); 153808b202b6SYOSHIFUJI Hideaki if (skb2 != skb) 153908b202b6SYOSHIFUJI Hideaki kfree_skb(skb2); 154008b202b6SYOSHIFUJI Hideaki return err; 154108b202b6SYOSHIFUJI Hideaki } 154208b202b6SYOSHIFUJI Hideaki #endif 154308b202b6SYOSHIFUJI Hideaki 1544eb1d1641SHerbert Xu int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port, 1545eb1d1641SHerbert Xu struct sk_buff *skb) 1546eb1d1641SHerbert Xu { 1547*1fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->igmp = 0; 1548*1fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->mrouters_only = 0; 1549*1fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 1550eb1d1641SHerbert Xu if (br->multicast_disabled) 1551eb1d1641SHerbert Xu return 0; 1552eb1d1641SHerbert Xu 1553eb1d1641SHerbert Xu switch (skb->protocol) { 1554eb1d1641SHerbert Xu case htons(ETH_P_IP): 1555eb1d1641SHerbert Xu return br_multicast_ipv4_rcv(br, port, skb); 155608b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 155708b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 155808b202b6SYOSHIFUJI Hideaki return br_multicast_ipv6_rcv(br, port, skb); 155908b202b6SYOSHIFUJI Hideaki #endif 1560eb1d1641SHerbert Xu } 1561eb1d1641SHerbert Xu 1562eb1d1641SHerbert Xu return 0; 1563eb1d1641SHerbert Xu } 1564eb1d1641SHerbert Xu 1565eb1d1641SHerbert Xu static void br_multicast_query_expired(unsigned long data) 1566eb1d1641SHerbert Xu { 1567eb1d1641SHerbert Xu struct net_bridge *br = (void *)data; 1568eb1d1641SHerbert Xu 1569eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1570eb1d1641SHerbert Xu if (br->multicast_startup_queries_sent < 1571eb1d1641SHerbert Xu br->multicast_startup_query_count) 1572eb1d1641SHerbert Xu br->multicast_startup_queries_sent++; 1573eb1d1641SHerbert Xu 1574eb1d1641SHerbert Xu br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent); 1575eb1d1641SHerbert Xu 1576eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1577eb1d1641SHerbert Xu } 1578eb1d1641SHerbert Xu 1579eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br) 1580eb1d1641SHerbert Xu { 1581eb1d1641SHerbert Xu br->hash_elasticity = 4; 1582eb1d1641SHerbert Xu br->hash_max = 512; 1583eb1d1641SHerbert Xu 1584eb1d1641SHerbert Xu br->multicast_router = 1; 1585eb1d1641SHerbert Xu br->multicast_last_member_count = 2; 1586eb1d1641SHerbert Xu br->multicast_startup_query_count = 2; 1587eb1d1641SHerbert Xu 1588eb1d1641SHerbert Xu br->multicast_last_member_interval = HZ; 1589eb1d1641SHerbert Xu br->multicast_query_response_interval = 10 * HZ; 1590eb1d1641SHerbert Xu br->multicast_startup_query_interval = 125 * HZ / 4; 1591eb1d1641SHerbert Xu br->multicast_query_interval = 125 * HZ; 1592eb1d1641SHerbert Xu br->multicast_querier_interval = 255 * HZ; 1593eb1d1641SHerbert Xu br->multicast_membership_interval = 260 * HZ; 1594eb1d1641SHerbert Xu 1595eb1d1641SHerbert Xu spin_lock_init(&br->multicast_lock); 1596eb1d1641SHerbert Xu setup_timer(&br->multicast_router_timer, 1597eb1d1641SHerbert Xu br_multicast_local_router_expired, 0); 1598eb1d1641SHerbert Xu setup_timer(&br->multicast_querier_timer, 1599eb1d1641SHerbert Xu br_multicast_local_router_expired, 0); 1600eb1d1641SHerbert Xu setup_timer(&br->multicast_query_timer, br_multicast_query_expired, 1601eb1d1641SHerbert Xu (unsigned long)br); 1602eb1d1641SHerbert Xu } 1603eb1d1641SHerbert Xu 1604eb1d1641SHerbert Xu void br_multicast_open(struct net_bridge *br) 1605eb1d1641SHerbert Xu { 1606eb1d1641SHerbert Xu br->multicast_startup_queries_sent = 0; 1607eb1d1641SHerbert Xu 1608eb1d1641SHerbert Xu if (br->multicast_disabled) 1609eb1d1641SHerbert Xu return; 1610eb1d1641SHerbert Xu 1611eb1d1641SHerbert Xu mod_timer(&br->multicast_query_timer, jiffies); 1612eb1d1641SHerbert Xu } 1613eb1d1641SHerbert Xu 1614eb1d1641SHerbert Xu void br_multicast_stop(struct net_bridge *br) 1615eb1d1641SHerbert Xu { 1616eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 1617eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1618eb1d1641SHerbert Xu struct hlist_node *p, *n; 1619eb1d1641SHerbert Xu u32 ver; 1620eb1d1641SHerbert Xu int i; 1621eb1d1641SHerbert Xu 1622eb1d1641SHerbert Xu del_timer_sync(&br->multicast_router_timer); 1623eb1d1641SHerbert Xu del_timer_sync(&br->multicast_querier_timer); 1624eb1d1641SHerbert Xu del_timer_sync(&br->multicast_query_timer); 1625eb1d1641SHerbert Xu 1626eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1627eb1d1641SHerbert Xu mdb = br->mdb; 1628eb1d1641SHerbert Xu if (!mdb) 1629eb1d1641SHerbert Xu goto out; 1630eb1d1641SHerbert Xu 1631eb1d1641SHerbert Xu br->mdb = NULL; 1632eb1d1641SHerbert Xu 1633eb1d1641SHerbert Xu ver = mdb->ver; 1634eb1d1641SHerbert Xu for (i = 0; i < mdb->max; i++) { 1635eb1d1641SHerbert Xu hlist_for_each_entry_safe(mp, p, n, &mdb->mhash[i], 1636eb1d1641SHerbert Xu hlist[ver]) { 1637eb1d1641SHerbert Xu del_timer(&mp->timer); 1638eb1d1641SHerbert Xu del_timer(&mp->query_timer); 1639eb1d1641SHerbert Xu call_rcu_bh(&mp->rcu, br_multicast_free_group); 1640eb1d1641SHerbert Xu } 1641eb1d1641SHerbert Xu } 1642eb1d1641SHerbert Xu 1643eb1d1641SHerbert Xu if (mdb->old) { 1644eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 164510cc2b50SHerbert Xu rcu_barrier_bh(); 1646eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1647eb1d1641SHerbert Xu WARN_ON(mdb->old); 1648eb1d1641SHerbert Xu } 1649eb1d1641SHerbert Xu 1650eb1d1641SHerbert Xu mdb->old = mdb; 1651eb1d1641SHerbert Xu call_rcu_bh(&mdb->rcu, br_mdb_free); 1652eb1d1641SHerbert Xu 1653eb1d1641SHerbert Xu out: 1654eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 1655eb1d1641SHerbert Xu } 16560909e117SHerbert Xu 16570909e117SHerbert Xu int br_multicast_set_router(struct net_bridge *br, unsigned long val) 16580909e117SHerbert Xu { 16590909e117SHerbert Xu int err = -ENOENT; 16600909e117SHerbert Xu 16610909e117SHerbert Xu spin_lock_bh(&br->multicast_lock); 16620909e117SHerbert Xu if (!netif_running(br->dev)) 16630909e117SHerbert Xu goto unlock; 16640909e117SHerbert Xu 16650909e117SHerbert Xu switch (val) { 16660909e117SHerbert Xu case 0: 16670909e117SHerbert Xu case 2: 16680909e117SHerbert Xu del_timer(&br->multicast_router_timer); 16690909e117SHerbert Xu /* fall through */ 16700909e117SHerbert Xu case 1: 16710909e117SHerbert Xu br->multicast_router = val; 16720909e117SHerbert Xu err = 0; 16730909e117SHerbert Xu break; 16740909e117SHerbert Xu 16750909e117SHerbert Xu default: 16760909e117SHerbert Xu err = -EINVAL; 16770909e117SHerbert Xu break; 16780909e117SHerbert Xu } 16790909e117SHerbert Xu 16800909e117SHerbert Xu unlock: 16810909e117SHerbert Xu spin_unlock_bh(&br->multicast_lock); 16820909e117SHerbert Xu 16830909e117SHerbert Xu return err; 16840909e117SHerbert Xu } 16850909e117SHerbert Xu 16860909e117SHerbert Xu int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val) 16870909e117SHerbert Xu { 16880909e117SHerbert Xu struct net_bridge *br = p->br; 16890909e117SHerbert Xu int err = -ENOENT; 16900909e117SHerbert Xu 16910909e117SHerbert Xu spin_lock(&br->multicast_lock); 16920909e117SHerbert Xu if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED) 16930909e117SHerbert Xu goto unlock; 16940909e117SHerbert Xu 16950909e117SHerbert Xu switch (val) { 16960909e117SHerbert Xu case 0: 16970909e117SHerbert Xu case 1: 16980909e117SHerbert Xu case 2: 16990909e117SHerbert Xu p->multicast_router = val; 17000909e117SHerbert Xu err = 0; 17010909e117SHerbert Xu 17020909e117SHerbert Xu if (val < 2 && !hlist_unhashed(&p->rlist)) 17030909e117SHerbert Xu hlist_del_init_rcu(&p->rlist); 17040909e117SHerbert Xu 17050909e117SHerbert Xu if (val == 1) 17060909e117SHerbert Xu break; 17070909e117SHerbert Xu 17080909e117SHerbert Xu del_timer(&p->multicast_router_timer); 17090909e117SHerbert Xu 17100909e117SHerbert Xu if (val == 0) 17110909e117SHerbert Xu break; 17120909e117SHerbert Xu 17130909e117SHerbert Xu br_multicast_add_router(br, p); 17140909e117SHerbert Xu break; 17150909e117SHerbert Xu 17160909e117SHerbert Xu default: 17170909e117SHerbert Xu err = -EINVAL; 17180909e117SHerbert Xu break; 17190909e117SHerbert Xu } 17200909e117SHerbert Xu 17210909e117SHerbert Xu unlock: 17220909e117SHerbert Xu spin_unlock(&br->multicast_lock); 17230909e117SHerbert Xu 17240909e117SHerbert Xu return err; 17250909e117SHerbert Xu } 1726561f1103SHerbert Xu 1727561f1103SHerbert Xu int br_multicast_toggle(struct net_bridge *br, unsigned long val) 1728561f1103SHerbert Xu { 1729561f1103SHerbert Xu struct net_bridge_port *port; 1730561f1103SHerbert Xu int err = -ENOENT; 1731561f1103SHerbert Xu 1732561f1103SHerbert Xu spin_lock(&br->multicast_lock); 1733561f1103SHerbert Xu if (!netif_running(br->dev)) 1734561f1103SHerbert Xu goto unlock; 1735561f1103SHerbert Xu 1736561f1103SHerbert Xu err = 0; 1737561f1103SHerbert Xu if (br->multicast_disabled == !val) 1738561f1103SHerbert Xu goto unlock; 1739561f1103SHerbert Xu 1740561f1103SHerbert Xu br->multicast_disabled = !val; 1741561f1103SHerbert Xu if (br->multicast_disabled) 1742561f1103SHerbert Xu goto unlock; 1743561f1103SHerbert Xu 1744561f1103SHerbert Xu if (br->mdb) { 1745561f1103SHerbert Xu if (br->mdb->old) { 1746561f1103SHerbert Xu err = -EEXIST; 1747561f1103SHerbert Xu rollback: 1748561f1103SHerbert Xu br->multicast_disabled = !!val; 1749561f1103SHerbert Xu goto unlock; 1750561f1103SHerbert Xu } 1751561f1103SHerbert Xu 1752561f1103SHerbert Xu err = br_mdb_rehash(&br->mdb, br->mdb->max, 1753561f1103SHerbert Xu br->hash_elasticity); 1754561f1103SHerbert Xu if (err) 1755561f1103SHerbert Xu goto rollback; 1756561f1103SHerbert Xu } 1757561f1103SHerbert Xu 1758561f1103SHerbert Xu br_multicast_open(br); 1759561f1103SHerbert Xu list_for_each_entry(port, &br->port_list, list) { 1760561f1103SHerbert Xu if (port->state == BR_STATE_DISABLED || 1761561f1103SHerbert Xu port->state == BR_STATE_BLOCKING) 1762561f1103SHerbert Xu continue; 1763561f1103SHerbert Xu 1764561f1103SHerbert Xu __br_multicast_enable_port(port); 1765561f1103SHerbert Xu } 1766561f1103SHerbert Xu 1767561f1103SHerbert Xu unlock: 1768561f1103SHerbert Xu spin_unlock(&br->multicast_lock); 1769561f1103SHerbert Xu 1770561f1103SHerbert Xu return err; 1771561f1103SHerbert Xu } 1772b195167fSHerbert Xu 1773b195167fSHerbert Xu int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val) 1774b195167fSHerbert Xu { 1775b195167fSHerbert Xu int err = -ENOENT; 1776b195167fSHerbert Xu u32 old; 1777b195167fSHerbert Xu 1778b195167fSHerbert Xu spin_lock(&br->multicast_lock); 1779b195167fSHerbert Xu if (!netif_running(br->dev)) 1780b195167fSHerbert Xu goto unlock; 1781b195167fSHerbert Xu 1782b195167fSHerbert Xu err = -EINVAL; 1783b195167fSHerbert Xu if (!is_power_of_2(val)) 1784b195167fSHerbert Xu goto unlock; 1785b195167fSHerbert Xu if (br->mdb && val < br->mdb->size) 1786b195167fSHerbert Xu goto unlock; 1787b195167fSHerbert Xu 1788b195167fSHerbert Xu err = 0; 1789b195167fSHerbert Xu 1790b195167fSHerbert Xu old = br->hash_max; 1791b195167fSHerbert Xu br->hash_max = val; 1792b195167fSHerbert Xu 1793b195167fSHerbert Xu if (br->mdb) { 1794b195167fSHerbert Xu if (br->mdb->old) { 1795b195167fSHerbert Xu err = -EEXIST; 1796b195167fSHerbert Xu rollback: 1797b195167fSHerbert Xu br->hash_max = old; 1798b195167fSHerbert Xu goto unlock; 1799b195167fSHerbert Xu } 1800b195167fSHerbert Xu 1801b195167fSHerbert Xu err = br_mdb_rehash(&br->mdb, br->hash_max, 1802b195167fSHerbert Xu br->hash_elasticity); 1803b195167fSHerbert Xu if (err) 1804b195167fSHerbert Xu goto rollback; 1805b195167fSHerbert Xu } 1806b195167fSHerbert Xu 1807b195167fSHerbert Xu unlock: 1808b195167fSHerbert Xu spin_unlock(&br->multicast_lock); 1809b195167fSHerbert Xu 1810b195167fSHerbert Xu return err; 1811b195167fSHerbert Xu } 1812