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