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 36e8051688SEric Dumazet #define mlock_dereference(X, br) \ 37e8051688SEric Dumazet rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock)) 38e8051688SEric Dumazet 3908b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 4008b202b6SYOSHIFUJI Hideaki static inline int ipv6_is_local_multicast(const struct in6_addr *addr) 41eb1d1641SHerbert Xu { 4208b202b6SYOSHIFUJI Hideaki if (ipv6_addr_is_multicast(addr) && 4308b202b6SYOSHIFUJI Hideaki IPV6_ADDR_MC_SCOPE(addr) <= IPV6_ADDR_SCOPE_LINKLOCAL) 4408b202b6SYOSHIFUJI Hideaki return 1; 4508b202b6SYOSHIFUJI Hideaki return 0; 4608b202b6SYOSHIFUJI Hideaki } 4708b202b6SYOSHIFUJI Hideaki #endif 4808b202b6SYOSHIFUJI Hideaki 498ef2a9a5SYOSHIFUJI Hideaki static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b) 508ef2a9a5SYOSHIFUJI Hideaki { 518ef2a9a5SYOSHIFUJI Hideaki if (a->proto != b->proto) 528ef2a9a5SYOSHIFUJI Hideaki return 0; 538ef2a9a5SYOSHIFUJI Hideaki switch (a->proto) { 548ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 558ef2a9a5SYOSHIFUJI Hideaki return a->u.ip4 == b->u.ip4; 5608b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 5708b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 5808b202b6SYOSHIFUJI Hideaki return ipv6_addr_equal(&a->u.ip6, &b->u.ip6); 5908b202b6SYOSHIFUJI Hideaki #endif 608ef2a9a5SYOSHIFUJI Hideaki } 618ef2a9a5SYOSHIFUJI Hideaki return 0; 628ef2a9a5SYOSHIFUJI Hideaki } 638ef2a9a5SYOSHIFUJI Hideaki 648ef2a9a5SYOSHIFUJI Hideaki static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip) 65eb1d1641SHerbert Xu { 660eae88f3SEric Dumazet return jhash_1word(mdb->secret, (__force u32)ip) & (mdb->max - 1); 67eb1d1641SHerbert Xu } 68eb1d1641SHerbert Xu 6908b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 7008b202b6SYOSHIFUJI Hideaki static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb, 7108b202b6SYOSHIFUJI Hideaki const struct in6_addr *ip) 7208b202b6SYOSHIFUJI Hideaki { 7308b202b6SYOSHIFUJI Hideaki return jhash2((__force u32 *)ip->s6_addr32, 4, mdb->secret) & (mdb->max - 1); 7408b202b6SYOSHIFUJI Hideaki } 7508b202b6SYOSHIFUJI Hideaki #endif 7608b202b6SYOSHIFUJI Hideaki 778ef2a9a5SYOSHIFUJI Hideaki static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb, 788ef2a9a5SYOSHIFUJI Hideaki struct br_ip *ip) 798ef2a9a5SYOSHIFUJI Hideaki { 808ef2a9a5SYOSHIFUJI Hideaki switch (ip->proto) { 818ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 828ef2a9a5SYOSHIFUJI Hideaki return __br_ip4_hash(mdb, ip->u.ip4); 8308b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 8408b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 8508b202b6SYOSHIFUJI Hideaki return __br_ip6_hash(mdb, &ip->u.ip6); 8608b202b6SYOSHIFUJI Hideaki #endif 878ef2a9a5SYOSHIFUJI Hideaki } 888ef2a9a5SYOSHIFUJI Hideaki return 0; 89eb1d1641SHerbert Xu } 90eb1d1641SHerbert Xu 91eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *__br_mdb_ip_get( 928ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash) 93eb1d1641SHerbert Xu { 94eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 95eb1d1641SHerbert Xu struct hlist_node *p; 96eb1d1641SHerbert Xu 9749f5fcfdSHerbert Xu hlist_for_each_entry_rcu(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) { 988ef2a9a5SYOSHIFUJI Hideaki if (br_ip_equal(&mp->addr, dst)) 99eb1d1641SHerbert Xu return mp; 100eb1d1641SHerbert Xu } 101eb1d1641SHerbert Xu 102eb1d1641SHerbert Xu return NULL; 103eb1d1641SHerbert Xu } 104eb1d1641SHerbert Xu 1057f285fa7SHerbert Xu static struct net_bridge_mdb_entry *br_mdb_ip_get( 1067f285fa7SHerbert Xu struct net_bridge_mdb_htable *mdb, struct br_ip *dst) 1077f285fa7SHerbert Xu { 1087f285fa7SHerbert Xu if (!mdb) 1097f285fa7SHerbert Xu return NULL; 1107f285fa7SHerbert Xu 1117f285fa7SHerbert Xu return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst)); 1127f285fa7SHerbert Xu } 1137f285fa7SHerbert Xu 1148ef2a9a5SYOSHIFUJI Hideaki static struct net_bridge_mdb_entry *br_mdb_ip4_get( 115eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb, __be32 dst) 116eb1d1641SHerbert Xu { 1178ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_dst; 1180821ec55SHerbert Xu 1198ef2a9a5SYOSHIFUJI Hideaki br_dst.u.ip4 = dst; 1208ef2a9a5SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IP); 1218ef2a9a5SYOSHIFUJI Hideaki 1227f285fa7SHerbert Xu return br_mdb_ip_get(mdb, &br_dst); 1238ef2a9a5SYOSHIFUJI Hideaki } 1248ef2a9a5SYOSHIFUJI Hideaki 12508b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 12608b202b6SYOSHIFUJI Hideaki static struct net_bridge_mdb_entry *br_mdb_ip6_get( 12708b202b6SYOSHIFUJI Hideaki struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst) 12808b202b6SYOSHIFUJI Hideaki { 12908b202b6SYOSHIFUJI Hideaki struct br_ip br_dst; 13008b202b6SYOSHIFUJI Hideaki 13108b202b6SYOSHIFUJI Hideaki ipv6_addr_copy(&br_dst.u.ip6, dst); 13208b202b6SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IPV6); 13308b202b6SYOSHIFUJI Hideaki 1347f285fa7SHerbert Xu return br_mdb_ip_get(mdb, &br_dst); 13508b202b6SYOSHIFUJI Hideaki } 13608b202b6SYOSHIFUJI Hideaki #endif 13708b202b6SYOSHIFUJI Hideaki 138eb1d1641SHerbert Xu struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br, 139eb1d1641SHerbert Xu struct sk_buff *skb) 140eb1d1641SHerbert Xu { 141e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb); 1428ef2a9a5SYOSHIFUJI Hideaki struct br_ip ip; 1438ef2a9a5SYOSHIFUJI Hideaki 1447f285fa7SHerbert Xu if (br->multicast_disabled) 145eb1d1641SHerbert Xu return NULL; 146eb1d1641SHerbert Xu 1478ef2a9a5SYOSHIFUJI Hideaki if (BR_INPUT_SKB_CB(skb)->igmp) 1488ef2a9a5SYOSHIFUJI Hideaki return NULL; 1498ef2a9a5SYOSHIFUJI Hideaki 1508ef2a9a5SYOSHIFUJI Hideaki ip.proto = skb->protocol; 1518ef2a9a5SYOSHIFUJI Hideaki 152eb1d1641SHerbert Xu switch (skb->protocol) { 153eb1d1641SHerbert Xu case htons(ETH_P_IP): 1548ef2a9a5SYOSHIFUJI Hideaki ip.u.ip4 = ip_hdr(skb)->daddr; 155eb1d1641SHerbert Xu break; 15608b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 15708b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 15808b202b6SYOSHIFUJI Hideaki ipv6_addr_copy(&ip.u.ip6, &ipv6_hdr(skb)->daddr); 15908b202b6SYOSHIFUJI Hideaki break; 16008b202b6SYOSHIFUJI Hideaki #endif 1618ef2a9a5SYOSHIFUJI Hideaki default: 1628ef2a9a5SYOSHIFUJI Hideaki return NULL; 163eb1d1641SHerbert Xu } 164eb1d1641SHerbert Xu 1658ef2a9a5SYOSHIFUJI Hideaki return br_mdb_ip_get(mdb, &ip); 166eb1d1641SHerbert Xu } 167eb1d1641SHerbert Xu 168eb1d1641SHerbert Xu static void br_mdb_free(struct rcu_head *head) 169eb1d1641SHerbert Xu { 170eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb = 171eb1d1641SHerbert Xu container_of(head, struct net_bridge_mdb_htable, rcu); 172eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old = mdb->old; 173eb1d1641SHerbert Xu 174eb1d1641SHerbert Xu mdb->old = NULL; 175eb1d1641SHerbert Xu kfree(old->mhash); 176eb1d1641SHerbert Xu kfree(old); 177eb1d1641SHerbert Xu } 178eb1d1641SHerbert Xu 179eb1d1641SHerbert Xu static int br_mdb_copy(struct net_bridge_mdb_htable *new, 180eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old, 181eb1d1641SHerbert Xu int elasticity) 182eb1d1641SHerbert Xu { 183eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 184eb1d1641SHerbert Xu struct hlist_node *p; 185eb1d1641SHerbert Xu int maxlen; 186eb1d1641SHerbert Xu int len; 187eb1d1641SHerbert Xu int i; 188eb1d1641SHerbert Xu 189eb1d1641SHerbert Xu for (i = 0; i < old->max; i++) 190eb1d1641SHerbert Xu hlist_for_each_entry(mp, p, &old->mhash[i], hlist[old->ver]) 191eb1d1641SHerbert Xu hlist_add_head(&mp->hlist[new->ver], 1928ef2a9a5SYOSHIFUJI Hideaki &new->mhash[br_ip_hash(new, &mp->addr)]); 193eb1d1641SHerbert Xu 194eb1d1641SHerbert Xu if (!elasticity) 195eb1d1641SHerbert Xu return 0; 196eb1d1641SHerbert Xu 197eb1d1641SHerbert Xu maxlen = 0; 198eb1d1641SHerbert Xu for (i = 0; i < new->max; i++) { 199eb1d1641SHerbert Xu len = 0; 200eb1d1641SHerbert Xu hlist_for_each_entry(mp, p, &new->mhash[i], hlist[new->ver]) 201eb1d1641SHerbert Xu len++; 202eb1d1641SHerbert Xu if (len > maxlen) 203eb1d1641SHerbert Xu maxlen = len; 204eb1d1641SHerbert Xu } 205eb1d1641SHerbert Xu 206eb1d1641SHerbert Xu return maxlen > elasticity ? -EINVAL : 0; 207eb1d1641SHerbert Xu } 208eb1d1641SHerbert Xu 209eb1d1641SHerbert Xu static void br_multicast_free_pg(struct rcu_head *head) 210eb1d1641SHerbert Xu { 211eb1d1641SHerbert Xu struct net_bridge_port_group *p = 212eb1d1641SHerbert Xu container_of(head, struct net_bridge_port_group, rcu); 213eb1d1641SHerbert Xu 214eb1d1641SHerbert Xu kfree(p); 215eb1d1641SHerbert Xu } 216eb1d1641SHerbert Xu 217eb1d1641SHerbert Xu static void br_multicast_free_group(struct rcu_head *head) 218eb1d1641SHerbert Xu { 219eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = 220eb1d1641SHerbert Xu container_of(head, struct net_bridge_mdb_entry, rcu); 221eb1d1641SHerbert Xu 222eb1d1641SHerbert Xu kfree(mp); 223eb1d1641SHerbert Xu } 224eb1d1641SHerbert Xu 225eb1d1641SHerbert Xu static void br_multicast_group_expired(unsigned long data) 226eb1d1641SHerbert Xu { 227eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = (void *)data; 228eb1d1641SHerbert Xu struct net_bridge *br = mp->br; 229eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 230eb1d1641SHerbert Xu 231eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 232eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&mp->timer)) 233eb1d1641SHerbert Xu goto out; 234eb1d1641SHerbert Xu 2358a870178SHerbert Xu mp->mglist = false; 236eb1d1641SHerbert Xu 237eb1d1641SHerbert Xu if (mp->ports) 238eb1d1641SHerbert Xu goto out; 239eb1d1641SHerbert Xu 240e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 241e8051688SEric Dumazet 242eb1d1641SHerbert Xu hlist_del_rcu(&mp->hlist[mdb->ver]); 243eb1d1641SHerbert Xu mdb->size--; 244eb1d1641SHerbert Xu 245eb1d1641SHerbert Xu del_timer(&mp->query_timer); 246eb1d1641SHerbert Xu call_rcu_bh(&mp->rcu, br_multicast_free_group); 247eb1d1641SHerbert Xu 248eb1d1641SHerbert Xu out: 249eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 250eb1d1641SHerbert Xu } 251eb1d1641SHerbert Xu 252eb1d1641SHerbert Xu static void br_multicast_del_pg(struct net_bridge *br, 253eb1d1641SHerbert Xu struct net_bridge_port_group *pg) 254eb1d1641SHerbert Xu { 255e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 256eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 257eb1d1641SHerbert Xu struct net_bridge_port_group *p; 258e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 259e8051688SEric Dumazet 260e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 261eb1d1641SHerbert Xu 2628ef2a9a5SYOSHIFUJI Hideaki mp = br_mdb_ip_get(mdb, &pg->addr); 263eb1d1641SHerbert Xu if (WARN_ON(!mp)) 264eb1d1641SHerbert Xu return; 265eb1d1641SHerbert Xu 266e8051688SEric Dumazet for (pp = &mp->ports; 267e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 268e8051688SEric Dumazet pp = &p->next) { 269eb1d1641SHerbert Xu if (p != pg) 270eb1d1641SHerbert Xu continue; 271eb1d1641SHerbert Xu 27283f6a740Sstephen hemminger rcu_assign_pointer(*pp, p->next); 273eb1d1641SHerbert Xu hlist_del_init(&p->mglist); 274eb1d1641SHerbert Xu del_timer(&p->timer); 275eb1d1641SHerbert Xu del_timer(&p->query_timer); 276eb1d1641SHerbert Xu call_rcu_bh(&p->rcu, br_multicast_free_pg); 277eb1d1641SHerbert Xu 2788a870178SHerbert Xu if (!mp->ports && !mp->mglist && 279eb1d1641SHerbert Xu netif_running(br->dev)) 280eb1d1641SHerbert Xu mod_timer(&mp->timer, jiffies); 281eb1d1641SHerbert Xu 282eb1d1641SHerbert Xu return; 283eb1d1641SHerbert Xu } 284eb1d1641SHerbert Xu 285eb1d1641SHerbert Xu WARN_ON(1); 286eb1d1641SHerbert Xu } 287eb1d1641SHerbert Xu 288eb1d1641SHerbert Xu static void br_multicast_port_group_expired(unsigned long data) 289eb1d1641SHerbert Xu { 290eb1d1641SHerbert Xu struct net_bridge_port_group *pg = (void *)data; 291eb1d1641SHerbert Xu struct net_bridge *br = pg->port->br; 292eb1d1641SHerbert Xu 293eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 294eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&pg->timer) || 295eb1d1641SHerbert Xu hlist_unhashed(&pg->mglist)) 296eb1d1641SHerbert Xu goto out; 297eb1d1641SHerbert Xu 298eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 299eb1d1641SHerbert Xu 300eb1d1641SHerbert Xu out: 301eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 302eb1d1641SHerbert Xu } 303eb1d1641SHerbert Xu 304e8051688SEric Dumazet static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max, 305eb1d1641SHerbert Xu int elasticity) 306eb1d1641SHerbert Xu { 307e8051688SEric Dumazet struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1); 308eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 309eb1d1641SHerbert Xu int err; 310eb1d1641SHerbert Xu 311eb1d1641SHerbert Xu mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC); 312eb1d1641SHerbert Xu if (!mdb) 313eb1d1641SHerbert Xu return -ENOMEM; 314eb1d1641SHerbert Xu 315eb1d1641SHerbert Xu mdb->max = max; 316eb1d1641SHerbert Xu mdb->old = old; 317eb1d1641SHerbert Xu 318eb1d1641SHerbert Xu mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC); 319eb1d1641SHerbert Xu if (!mdb->mhash) { 320eb1d1641SHerbert Xu kfree(mdb); 321eb1d1641SHerbert Xu return -ENOMEM; 322eb1d1641SHerbert Xu } 323eb1d1641SHerbert Xu 324eb1d1641SHerbert Xu mdb->size = old ? old->size : 0; 325eb1d1641SHerbert Xu mdb->ver = old ? old->ver ^ 1 : 0; 326eb1d1641SHerbert Xu 327eb1d1641SHerbert Xu if (!old || elasticity) 328eb1d1641SHerbert Xu get_random_bytes(&mdb->secret, sizeof(mdb->secret)); 329eb1d1641SHerbert Xu else 330eb1d1641SHerbert Xu mdb->secret = old->secret; 331eb1d1641SHerbert Xu 332eb1d1641SHerbert Xu if (!old) 333eb1d1641SHerbert Xu goto out; 334eb1d1641SHerbert Xu 335eb1d1641SHerbert Xu err = br_mdb_copy(mdb, old, elasticity); 336eb1d1641SHerbert Xu if (err) { 337eb1d1641SHerbert Xu kfree(mdb->mhash); 338eb1d1641SHerbert Xu kfree(mdb); 339eb1d1641SHerbert Xu return err; 340eb1d1641SHerbert Xu } 341eb1d1641SHerbert Xu 342eb1d1641SHerbert Xu call_rcu_bh(&mdb->rcu, br_mdb_free); 343eb1d1641SHerbert Xu 344eb1d1641SHerbert Xu out: 345eb1d1641SHerbert Xu rcu_assign_pointer(*mdbp, mdb); 346eb1d1641SHerbert Xu 347eb1d1641SHerbert Xu return 0; 348eb1d1641SHerbert Xu } 349eb1d1641SHerbert Xu 3508ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br, 351eb1d1641SHerbert Xu __be32 group) 352eb1d1641SHerbert Xu { 353eb1d1641SHerbert Xu struct sk_buff *skb; 354eb1d1641SHerbert Xu struct igmphdr *ih; 355eb1d1641SHerbert Xu struct ethhdr *eth; 356eb1d1641SHerbert Xu struct iphdr *iph; 357eb1d1641SHerbert Xu 358eb1d1641SHerbert Xu skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) + 359eb1d1641SHerbert Xu sizeof(*ih) + 4); 360eb1d1641SHerbert Xu if (!skb) 361eb1d1641SHerbert Xu goto out; 362eb1d1641SHerbert Xu 363eb1d1641SHerbert Xu skb->protocol = htons(ETH_P_IP); 364eb1d1641SHerbert Xu 365eb1d1641SHerbert Xu skb_reset_mac_header(skb); 366eb1d1641SHerbert Xu eth = eth_hdr(skb); 367eb1d1641SHerbert Xu 368eb1d1641SHerbert Xu memcpy(eth->h_source, br->dev->dev_addr, 6); 369eb1d1641SHerbert Xu eth->h_dest[0] = 1; 370eb1d1641SHerbert Xu eth->h_dest[1] = 0; 371eb1d1641SHerbert Xu eth->h_dest[2] = 0x5e; 372eb1d1641SHerbert Xu eth->h_dest[3] = 0; 373eb1d1641SHerbert Xu eth->h_dest[4] = 0; 374eb1d1641SHerbert Xu eth->h_dest[5] = 1; 375eb1d1641SHerbert Xu eth->h_proto = htons(ETH_P_IP); 376eb1d1641SHerbert Xu skb_put(skb, sizeof(*eth)); 377eb1d1641SHerbert Xu 378eb1d1641SHerbert Xu skb_set_network_header(skb, skb->len); 379eb1d1641SHerbert Xu iph = ip_hdr(skb); 380eb1d1641SHerbert Xu 381eb1d1641SHerbert Xu iph->version = 4; 382eb1d1641SHerbert Xu iph->ihl = 6; 383eb1d1641SHerbert Xu iph->tos = 0xc0; 384eb1d1641SHerbert Xu iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4); 385eb1d1641SHerbert Xu iph->id = 0; 386eb1d1641SHerbert Xu iph->frag_off = htons(IP_DF); 387eb1d1641SHerbert Xu iph->ttl = 1; 388eb1d1641SHerbert Xu iph->protocol = IPPROTO_IGMP; 389eb1d1641SHerbert Xu iph->saddr = 0; 390eb1d1641SHerbert Xu iph->daddr = htonl(INADDR_ALLHOSTS_GROUP); 391eb1d1641SHerbert Xu ((u8 *)&iph[1])[0] = IPOPT_RA; 392eb1d1641SHerbert Xu ((u8 *)&iph[1])[1] = 4; 393eb1d1641SHerbert Xu ((u8 *)&iph[1])[2] = 0; 394eb1d1641SHerbert Xu ((u8 *)&iph[1])[3] = 0; 395eb1d1641SHerbert Xu ip_send_check(iph); 396eb1d1641SHerbert Xu skb_put(skb, 24); 397eb1d1641SHerbert Xu 398eb1d1641SHerbert Xu skb_set_transport_header(skb, skb->len); 399eb1d1641SHerbert Xu ih = igmp_hdr(skb); 400eb1d1641SHerbert Xu ih->type = IGMP_HOST_MEMBERSHIP_QUERY; 401eb1d1641SHerbert Xu ih->code = (group ? br->multicast_last_member_interval : 402eb1d1641SHerbert Xu br->multicast_query_response_interval) / 403eb1d1641SHerbert Xu (HZ / IGMP_TIMER_SCALE); 404eb1d1641SHerbert Xu ih->group = group; 405eb1d1641SHerbert Xu ih->csum = 0; 406eb1d1641SHerbert Xu ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr)); 407eb1d1641SHerbert Xu skb_put(skb, sizeof(*ih)); 408eb1d1641SHerbert Xu 409eb1d1641SHerbert Xu __skb_pull(skb, sizeof(*eth)); 410eb1d1641SHerbert Xu 411eb1d1641SHerbert Xu out: 412eb1d1641SHerbert Xu return skb; 413eb1d1641SHerbert Xu } 414eb1d1641SHerbert Xu 41508b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 41608b202b6SYOSHIFUJI Hideaki static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br, 41708b202b6SYOSHIFUJI Hideaki struct in6_addr *group) 41808b202b6SYOSHIFUJI Hideaki { 41908b202b6SYOSHIFUJI Hideaki struct sk_buff *skb; 42008b202b6SYOSHIFUJI Hideaki struct ipv6hdr *ip6h; 42108b202b6SYOSHIFUJI Hideaki struct mld_msg *mldq; 42208b202b6SYOSHIFUJI Hideaki struct ethhdr *eth; 42308b202b6SYOSHIFUJI Hideaki u8 *hopopt; 42408b202b6SYOSHIFUJI Hideaki unsigned long interval; 42508b202b6SYOSHIFUJI Hideaki 42608b202b6SYOSHIFUJI Hideaki skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) + 42708b202b6SYOSHIFUJI Hideaki 8 + sizeof(*mldq)); 42808b202b6SYOSHIFUJI Hideaki if (!skb) 42908b202b6SYOSHIFUJI Hideaki goto out; 43008b202b6SYOSHIFUJI Hideaki 43108b202b6SYOSHIFUJI Hideaki skb->protocol = htons(ETH_P_IPV6); 43208b202b6SYOSHIFUJI Hideaki 43308b202b6SYOSHIFUJI Hideaki /* Ethernet header */ 43408b202b6SYOSHIFUJI Hideaki skb_reset_mac_header(skb); 43508b202b6SYOSHIFUJI Hideaki eth = eth_hdr(skb); 43608b202b6SYOSHIFUJI Hideaki 43708b202b6SYOSHIFUJI Hideaki memcpy(eth->h_source, br->dev->dev_addr, 6); 43808b202b6SYOSHIFUJI Hideaki ipv6_eth_mc_map(group, eth->h_dest); 43908b202b6SYOSHIFUJI Hideaki eth->h_proto = htons(ETH_P_IPV6); 44008b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*eth)); 44108b202b6SYOSHIFUJI Hideaki 44208b202b6SYOSHIFUJI Hideaki /* IPv6 header + HbH option */ 44308b202b6SYOSHIFUJI Hideaki skb_set_network_header(skb, skb->len); 44408b202b6SYOSHIFUJI Hideaki ip6h = ipv6_hdr(skb); 44508b202b6SYOSHIFUJI Hideaki 44608b202b6SYOSHIFUJI Hideaki *(__force __be32 *)ip6h = htonl(0x60000000); 44776d66158SDavid Stevens ip6h->payload_len = htons(8 + sizeof(*mldq)); 44808b202b6SYOSHIFUJI Hideaki ip6h->nexthdr = IPPROTO_HOPOPTS; 44908b202b6SYOSHIFUJI Hideaki ip6h->hop_limit = 1; 45008b202b6SYOSHIFUJI Hideaki ipv6_addr_set(&ip6h->saddr, 0, 0, 0, 0); 45108b202b6SYOSHIFUJI Hideaki ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1)); 45208b202b6SYOSHIFUJI Hideaki 45308b202b6SYOSHIFUJI Hideaki hopopt = (u8 *)(ip6h + 1); 45408b202b6SYOSHIFUJI Hideaki hopopt[0] = IPPROTO_ICMPV6; /* next hdr */ 45508b202b6SYOSHIFUJI Hideaki hopopt[1] = 0; /* length of HbH */ 45608b202b6SYOSHIFUJI Hideaki hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */ 45708b202b6SYOSHIFUJI Hideaki hopopt[3] = 2; /* Length of RA Option */ 45808b202b6SYOSHIFUJI Hideaki hopopt[4] = 0; /* Type = 0x0000 (MLD) */ 45908b202b6SYOSHIFUJI Hideaki hopopt[5] = 0; 46008b202b6SYOSHIFUJI Hideaki hopopt[6] = IPV6_TLV_PAD0; /* Pad0 */ 46108b202b6SYOSHIFUJI Hideaki hopopt[7] = IPV6_TLV_PAD0; /* Pad0 */ 46208b202b6SYOSHIFUJI Hideaki 46308b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*ip6h) + 8); 46408b202b6SYOSHIFUJI Hideaki 46508b202b6SYOSHIFUJI Hideaki /* ICMPv6 */ 46608b202b6SYOSHIFUJI Hideaki skb_set_transport_header(skb, skb->len); 46708b202b6SYOSHIFUJI Hideaki mldq = (struct mld_msg *) icmp6_hdr(skb); 46808b202b6SYOSHIFUJI Hideaki 46908b202b6SYOSHIFUJI Hideaki interval = ipv6_addr_any(group) ? br->multicast_last_member_interval : 47008b202b6SYOSHIFUJI Hideaki br->multicast_query_response_interval; 47108b202b6SYOSHIFUJI Hideaki 47208b202b6SYOSHIFUJI Hideaki mldq->mld_type = ICMPV6_MGM_QUERY; 47308b202b6SYOSHIFUJI Hideaki mldq->mld_code = 0; 47408b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = 0; 47508b202b6SYOSHIFUJI Hideaki mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval)); 47608b202b6SYOSHIFUJI Hideaki mldq->mld_reserved = 0; 47708b202b6SYOSHIFUJI Hideaki ipv6_addr_copy(&mldq->mld_mca, group); 47808b202b6SYOSHIFUJI Hideaki 47908b202b6SYOSHIFUJI Hideaki /* checksum */ 48008b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, 48108b202b6SYOSHIFUJI Hideaki sizeof(*mldq), IPPROTO_ICMPV6, 48208b202b6SYOSHIFUJI Hideaki csum_partial(mldq, 48308b202b6SYOSHIFUJI Hideaki sizeof(*mldq), 0)); 48408b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*mldq)); 48508b202b6SYOSHIFUJI Hideaki 48608b202b6SYOSHIFUJI Hideaki __skb_pull(skb, sizeof(*eth)); 48708b202b6SYOSHIFUJI Hideaki 48808b202b6SYOSHIFUJI Hideaki out: 48908b202b6SYOSHIFUJI Hideaki return skb; 49008b202b6SYOSHIFUJI Hideaki } 49108b202b6SYOSHIFUJI Hideaki #endif 49208b202b6SYOSHIFUJI Hideaki 4938ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br, 4948ef2a9a5SYOSHIFUJI Hideaki struct br_ip *addr) 4958ef2a9a5SYOSHIFUJI Hideaki { 4968ef2a9a5SYOSHIFUJI Hideaki switch (addr->proto) { 4978ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 4988ef2a9a5SYOSHIFUJI Hideaki return br_ip4_multicast_alloc_query(br, addr->u.ip4); 49908b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 50008b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 50108b202b6SYOSHIFUJI Hideaki return br_ip6_multicast_alloc_query(br, &addr->u.ip6); 50208b202b6SYOSHIFUJI Hideaki #endif 5038ef2a9a5SYOSHIFUJI Hideaki } 5048ef2a9a5SYOSHIFUJI Hideaki return NULL; 5058ef2a9a5SYOSHIFUJI Hideaki } 5068ef2a9a5SYOSHIFUJI Hideaki 507eb1d1641SHerbert Xu static void br_multicast_send_group_query(struct net_bridge_mdb_entry *mp) 508eb1d1641SHerbert Xu { 509eb1d1641SHerbert Xu struct net_bridge *br = mp->br; 510eb1d1641SHerbert Xu struct sk_buff *skb; 511eb1d1641SHerbert Xu 5128ef2a9a5SYOSHIFUJI Hideaki skb = br_multicast_alloc_query(br, &mp->addr); 513eb1d1641SHerbert Xu if (!skb) 514eb1d1641SHerbert Xu goto timer; 515eb1d1641SHerbert Xu 516eb1d1641SHerbert Xu netif_rx(skb); 517eb1d1641SHerbert Xu 518eb1d1641SHerbert Xu timer: 519eb1d1641SHerbert Xu if (++mp->queries_sent < br->multicast_last_member_count) 520eb1d1641SHerbert Xu mod_timer(&mp->query_timer, 521eb1d1641SHerbert Xu jiffies + br->multicast_last_member_interval); 522eb1d1641SHerbert Xu } 523eb1d1641SHerbert Xu 524eb1d1641SHerbert Xu static void br_multicast_group_query_expired(unsigned long data) 525eb1d1641SHerbert Xu { 526eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = (void *)data; 527eb1d1641SHerbert Xu struct net_bridge *br = mp->br; 528eb1d1641SHerbert Xu 529eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 5308a870178SHerbert Xu if (!netif_running(br->dev) || !mp->mglist || 531eb1d1641SHerbert Xu mp->queries_sent >= br->multicast_last_member_count) 532eb1d1641SHerbert Xu goto out; 533eb1d1641SHerbert Xu 534eb1d1641SHerbert Xu br_multicast_send_group_query(mp); 535eb1d1641SHerbert Xu 536eb1d1641SHerbert Xu out: 537eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 538eb1d1641SHerbert Xu } 539eb1d1641SHerbert Xu 540eb1d1641SHerbert Xu static void br_multicast_send_port_group_query(struct net_bridge_port_group *pg) 541eb1d1641SHerbert Xu { 542eb1d1641SHerbert Xu struct net_bridge_port *port = pg->port; 543eb1d1641SHerbert Xu struct net_bridge *br = port->br; 544eb1d1641SHerbert Xu struct sk_buff *skb; 545eb1d1641SHerbert Xu 5468ef2a9a5SYOSHIFUJI Hideaki skb = br_multicast_alloc_query(br, &pg->addr); 547eb1d1641SHerbert Xu if (!skb) 548eb1d1641SHerbert Xu goto timer; 549eb1d1641SHerbert Xu 550eb1d1641SHerbert Xu br_deliver(port, skb); 551eb1d1641SHerbert Xu 552eb1d1641SHerbert Xu timer: 553eb1d1641SHerbert Xu if (++pg->queries_sent < br->multicast_last_member_count) 554eb1d1641SHerbert Xu mod_timer(&pg->query_timer, 555eb1d1641SHerbert Xu jiffies + br->multicast_last_member_interval); 556eb1d1641SHerbert Xu } 557eb1d1641SHerbert Xu 558eb1d1641SHerbert Xu static void br_multicast_port_group_query_expired(unsigned long data) 559eb1d1641SHerbert Xu { 560eb1d1641SHerbert Xu struct net_bridge_port_group *pg = (void *)data; 561eb1d1641SHerbert Xu struct net_bridge_port *port = pg->port; 562eb1d1641SHerbert Xu struct net_bridge *br = port->br; 563eb1d1641SHerbert Xu 564eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 565eb1d1641SHerbert Xu if (!netif_running(br->dev) || hlist_unhashed(&pg->mglist) || 566eb1d1641SHerbert Xu pg->queries_sent >= br->multicast_last_member_count) 567eb1d1641SHerbert Xu goto out; 568eb1d1641SHerbert Xu 569eb1d1641SHerbert Xu br_multicast_send_port_group_query(pg); 570eb1d1641SHerbert Xu 571eb1d1641SHerbert Xu out: 572eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 573eb1d1641SHerbert Xu } 574eb1d1641SHerbert Xu 575eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *br_multicast_get_group( 5768ef2a9a5SYOSHIFUJI Hideaki struct net_bridge *br, struct net_bridge_port *port, 5778ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group, int hash) 578eb1d1641SHerbert Xu { 579e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 580eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 581eb1d1641SHerbert Xu struct hlist_node *p; 582eb1d1641SHerbert Xu unsigned count = 0; 583eb1d1641SHerbert Xu unsigned max; 584eb1d1641SHerbert Xu int elasticity; 585eb1d1641SHerbert Xu int err; 586eb1d1641SHerbert Xu 587e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 588eb1d1641SHerbert Xu hlist_for_each_entry(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) { 589eb1d1641SHerbert Xu count++; 5908ef2a9a5SYOSHIFUJI Hideaki if (unlikely(br_ip_equal(group, &mp->addr))) 591eb1d1641SHerbert Xu return mp; 592eb1d1641SHerbert Xu } 593eb1d1641SHerbert Xu 594eb1d1641SHerbert Xu elasticity = 0; 595eb1d1641SHerbert Xu max = mdb->max; 596eb1d1641SHerbert Xu 597eb1d1641SHerbert Xu if (unlikely(count > br->hash_elasticity && count)) { 598eb1d1641SHerbert Xu if (net_ratelimit()) 59928a16c97Sstephen hemminger br_info(br, "Multicast hash table " 600eb1d1641SHerbert Xu "chain limit reached: %s\n", 60128a16c97Sstephen hemminger port ? port->dev->name : br->dev->name); 602eb1d1641SHerbert Xu 603eb1d1641SHerbert Xu elasticity = br->hash_elasticity; 604eb1d1641SHerbert Xu } 605eb1d1641SHerbert Xu 606eb1d1641SHerbert Xu if (mdb->size >= max) { 607eb1d1641SHerbert Xu max *= 2; 608eb1d1641SHerbert Xu if (unlikely(max >= br->hash_max)) { 60928a16c97Sstephen hemminger br_warn(br, "Multicast hash table maximum " 610eb1d1641SHerbert Xu "reached, disabling snooping: %s, %d\n", 61128a16c97Sstephen hemminger port ? port->dev->name : br->dev->name, max); 612eb1d1641SHerbert Xu err = -E2BIG; 613eb1d1641SHerbert Xu disable: 614eb1d1641SHerbert Xu br->multicast_disabled = 1; 615eb1d1641SHerbert Xu goto err; 616eb1d1641SHerbert Xu } 617eb1d1641SHerbert Xu } 618eb1d1641SHerbert Xu 619eb1d1641SHerbert Xu if (max > mdb->max || elasticity) { 620eb1d1641SHerbert Xu if (mdb->old) { 621eb1d1641SHerbert Xu if (net_ratelimit()) 62228a16c97Sstephen hemminger br_info(br, "Multicast hash table " 623eb1d1641SHerbert Xu "on fire: %s\n", 62428a16c97Sstephen hemminger port ? port->dev->name : br->dev->name); 625eb1d1641SHerbert Xu err = -EEXIST; 626eb1d1641SHerbert Xu goto err; 627eb1d1641SHerbert Xu } 628eb1d1641SHerbert Xu 629eb1d1641SHerbert Xu err = br_mdb_rehash(&br->mdb, max, elasticity); 630eb1d1641SHerbert Xu if (err) { 63128a16c97Sstephen hemminger br_warn(br, "Cannot rehash multicast " 63228a16c97Sstephen hemminger "hash table, disabling snooping: %s, %d, %d\n", 63328a16c97Sstephen hemminger port ? port->dev->name : br->dev->name, 634eb1d1641SHerbert Xu mdb->size, err); 635eb1d1641SHerbert Xu goto disable; 636eb1d1641SHerbert Xu } 637eb1d1641SHerbert Xu 638eb1d1641SHerbert Xu err = -EAGAIN; 639eb1d1641SHerbert Xu goto err; 640eb1d1641SHerbert Xu } 641eb1d1641SHerbert Xu 642eb1d1641SHerbert Xu return NULL; 643eb1d1641SHerbert Xu 644eb1d1641SHerbert Xu err: 645eb1d1641SHerbert Xu mp = ERR_PTR(err); 646eb1d1641SHerbert Xu return mp; 647eb1d1641SHerbert Xu } 648eb1d1641SHerbert Xu 649eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *br_multicast_new_group( 6508ef2a9a5SYOSHIFUJI Hideaki struct net_bridge *br, struct net_bridge_port *port, 6518ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group) 652eb1d1641SHerbert Xu { 653e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 654eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 655eb1d1641SHerbert Xu int hash; 6564c0833bcSTobias Klauser int err; 657eb1d1641SHerbert Xu 658e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 659eb1d1641SHerbert Xu if (!mdb) { 6604c0833bcSTobias Klauser err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0); 6614c0833bcSTobias Klauser if (err) 6624c0833bcSTobias Klauser return ERR_PTR(err); 663eb1d1641SHerbert Xu goto rehash; 664eb1d1641SHerbert Xu } 665eb1d1641SHerbert Xu 666eb1d1641SHerbert Xu hash = br_ip_hash(mdb, group); 667eb1d1641SHerbert Xu mp = br_multicast_get_group(br, port, group, hash); 668eb1d1641SHerbert Xu switch (PTR_ERR(mp)) { 669eb1d1641SHerbert Xu case 0: 670eb1d1641SHerbert Xu break; 671eb1d1641SHerbert Xu 672eb1d1641SHerbert Xu case -EAGAIN: 673eb1d1641SHerbert Xu rehash: 674e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 675eb1d1641SHerbert Xu hash = br_ip_hash(mdb, group); 676eb1d1641SHerbert Xu break; 677eb1d1641SHerbert Xu 678eb1d1641SHerbert Xu default: 679eb1d1641SHerbert Xu goto out; 680eb1d1641SHerbert Xu } 681eb1d1641SHerbert Xu 682eb1d1641SHerbert Xu mp = kzalloc(sizeof(*mp), GFP_ATOMIC); 683eb1d1641SHerbert Xu if (unlikely(!mp)) 6844c0833bcSTobias Klauser return ERR_PTR(-ENOMEM); 685eb1d1641SHerbert Xu 686eb1d1641SHerbert Xu mp->br = br; 6878ef2a9a5SYOSHIFUJI Hideaki mp->addr = *group; 688eb1d1641SHerbert Xu setup_timer(&mp->timer, br_multicast_group_expired, 689eb1d1641SHerbert Xu (unsigned long)mp); 690eb1d1641SHerbert Xu setup_timer(&mp->query_timer, br_multicast_group_query_expired, 691eb1d1641SHerbert Xu (unsigned long)mp); 692eb1d1641SHerbert Xu 693eb1d1641SHerbert Xu hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]); 694eb1d1641SHerbert Xu mdb->size++; 695eb1d1641SHerbert Xu 696eb1d1641SHerbert Xu out: 697eb1d1641SHerbert Xu return mp; 698eb1d1641SHerbert Xu } 699eb1d1641SHerbert Xu 700eb1d1641SHerbert Xu static int br_multicast_add_group(struct net_bridge *br, 7018ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 7028ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group) 703eb1d1641SHerbert Xu { 704eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 705eb1d1641SHerbert Xu struct net_bridge_port_group *p; 706e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 707eb1d1641SHerbert Xu unsigned long now = jiffies; 708eb1d1641SHerbert Xu int err; 709eb1d1641SHerbert Xu 710eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 711eb1d1641SHerbert Xu if (!netif_running(br->dev) || 712eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 713eb1d1641SHerbert Xu goto out; 714eb1d1641SHerbert Xu 715eb1d1641SHerbert Xu mp = br_multicast_new_group(br, port, group); 716eb1d1641SHerbert Xu err = PTR_ERR(mp); 7174c0833bcSTobias Klauser if (IS_ERR(mp)) 718eb1d1641SHerbert Xu goto err; 719eb1d1641SHerbert Xu 720eb1d1641SHerbert Xu if (!port) { 7218a870178SHerbert Xu mp->mglist = true; 722eb1d1641SHerbert Xu mod_timer(&mp->timer, now + br->multicast_membership_interval); 723eb1d1641SHerbert Xu goto out; 724eb1d1641SHerbert Xu } 725eb1d1641SHerbert Xu 726e8051688SEric Dumazet for (pp = &mp->ports; 727e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 728e8051688SEric Dumazet pp = &p->next) { 729eb1d1641SHerbert Xu if (p->port == port) 730eb1d1641SHerbert Xu goto found; 731eb1d1641SHerbert Xu if ((unsigned long)p->port < (unsigned long)port) 732eb1d1641SHerbert Xu break; 733eb1d1641SHerbert Xu } 734eb1d1641SHerbert Xu 735eb1d1641SHerbert Xu p = kzalloc(sizeof(*p), GFP_ATOMIC); 736eb1d1641SHerbert Xu err = -ENOMEM; 737eb1d1641SHerbert Xu if (unlikely(!p)) 738eb1d1641SHerbert Xu goto err; 739eb1d1641SHerbert Xu 7408ef2a9a5SYOSHIFUJI Hideaki p->addr = *group; 741eb1d1641SHerbert Xu p->port = port; 742eb1d1641SHerbert Xu p->next = *pp; 743eb1d1641SHerbert Xu hlist_add_head(&p->mglist, &port->mglist); 744eb1d1641SHerbert Xu setup_timer(&p->timer, br_multicast_port_group_expired, 745eb1d1641SHerbert Xu (unsigned long)p); 746eb1d1641SHerbert Xu setup_timer(&p->query_timer, br_multicast_port_group_query_expired, 747eb1d1641SHerbert Xu (unsigned long)p); 748eb1d1641SHerbert Xu 749eb1d1641SHerbert Xu rcu_assign_pointer(*pp, p); 750eb1d1641SHerbert Xu 751eb1d1641SHerbert Xu found: 752eb1d1641SHerbert Xu mod_timer(&p->timer, now + br->multicast_membership_interval); 753eb1d1641SHerbert Xu out: 754eb1d1641SHerbert Xu err = 0; 755eb1d1641SHerbert Xu 756eb1d1641SHerbert Xu err: 757eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 758eb1d1641SHerbert Xu return err; 759eb1d1641SHerbert Xu } 760eb1d1641SHerbert Xu 7618ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_add_group(struct net_bridge *br, 7628ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 7638ef2a9a5SYOSHIFUJI Hideaki __be32 group) 7648ef2a9a5SYOSHIFUJI Hideaki { 7658ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 7668ef2a9a5SYOSHIFUJI Hideaki 7678ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 7688ef2a9a5SYOSHIFUJI Hideaki return 0; 7698ef2a9a5SYOSHIFUJI Hideaki 7708ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 7718ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 7728ef2a9a5SYOSHIFUJI Hideaki 7738ef2a9a5SYOSHIFUJI Hideaki return br_multicast_add_group(br, port, &br_group); 7748ef2a9a5SYOSHIFUJI Hideaki } 7758ef2a9a5SYOSHIFUJI Hideaki 77608b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 77708b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_add_group(struct net_bridge *br, 77808b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 77908b202b6SYOSHIFUJI Hideaki const struct in6_addr *group) 78008b202b6SYOSHIFUJI Hideaki { 78108b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 78208b202b6SYOSHIFUJI Hideaki 78308b202b6SYOSHIFUJI Hideaki if (ipv6_is_local_multicast(group)) 78408b202b6SYOSHIFUJI Hideaki return 0; 78508b202b6SYOSHIFUJI Hideaki 78608b202b6SYOSHIFUJI Hideaki ipv6_addr_copy(&br_group.u.ip6, group); 7879cc6e0c4SLinus Lüssing br_group.proto = htons(ETH_P_IPV6); 78808b202b6SYOSHIFUJI Hideaki 78908b202b6SYOSHIFUJI Hideaki return br_multicast_add_group(br, port, &br_group); 79008b202b6SYOSHIFUJI Hideaki } 79108b202b6SYOSHIFUJI Hideaki #endif 79208b202b6SYOSHIFUJI Hideaki 793eb1d1641SHerbert Xu static void br_multicast_router_expired(unsigned long data) 794eb1d1641SHerbert Xu { 795eb1d1641SHerbert Xu struct net_bridge_port *port = (void *)data; 796eb1d1641SHerbert Xu struct net_bridge *br = port->br; 797eb1d1641SHerbert Xu 798eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 799eb1d1641SHerbert Xu if (port->multicast_router != 1 || 800eb1d1641SHerbert Xu timer_pending(&port->multicast_router_timer) || 801eb1d1641SHerbert Xu hlist_unhashed(&port->rlist)) 802eb1d1641SHerbert Xu goto out; 803eb1d1641SHerbert Xu 804eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 805eb1d1641SHerbert Xu 806eb1d1641SHerbert Xu out: 807eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 808eb1d1641SHerbert Xu } 809eb1d1641SHerbert Xu 810eb1d1641SHerbert Xu static void br_multicast_local_router_expired(unsigned long data) 811eb1d1641SHerbert Xu { 812eb1d1641SHerbert Xu } 813eb1d1641SHerbert Xu 8148ef2a9a5SYOSHIFUJI Hideaki static void __br_multicast_send_query(struct net_bridge *br, 8158ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 8168ef2a9a5SYOSHIFUJI Hideaki struct br_ip *ip) 817eb1d1641SHerbert Xu { 818eb1d1641SHerbert Xu struct sk_buff *skb; 819eb1d1641SHerbert Xu 8208ef2a9a5SYOSHIFUJI Hideaki skb = br_multicast_alloc_query(br, ip); 821eb1d1641SHerbert Xu if (!skb) 8228ef2a9a5SYOSHIFUJI Hideaki return; 823eb1d1641SHerbert Xu 824eb1d1641SHerbert Xu if (port) { 825eb1d1641SHerbert Xu __skb_push(skb, sizeof(struct ethhdr)); 826eb1d1641SHerbert Xu skb->dev = port->dev; 827713aefa3SJan Engelhardt NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev, 828eb1d1641SHerbert Xu dev_queue_xmit); 829eb1d1641SHerbert Xu } else 830eb1d1641SHerbert Xu netif_rx(skb); 8318ef2a9a5SYOSHIFUJI Hideaki } 832eb1d1641SHerbert Xu 8338ef2a9a5SYOSHIFUJI Hideaki static void br_multicast_send_query(struct net_bridge *br, 8348ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, u32 sent) 8358ef2a9a5SYOSHIFUJI Hideaki { 8368ef2a9a5SYOSHIFUJI Hideaki unsigned long time; 8378ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 8388ef2a9a5SYOSHIFUJI Hideaki 8398ef2a9a5SYOSHIFUJI Hideaki if (!netif_running(br->dev) || br->multicast_disabled || 8408ef2a9a5SYOSHIFUJI Hideaki timer_pending(&br->multicast_querier_timer)) 8418ef2a9a5SYOSHIFUJI Hideaki return; 8428ef2a9a5SYOSHIFUJI Hideaki 84308b202b6SYOSHIFUJI Hideaki memset(&br_group.u, 0, sizeof(br_group.u)); 8448ef2a9a5SYOSHIFUJI Hideaki 84508b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 8468ef2a9a5SYOSHIFUJI Hideaki __br_multicast_send_query(br, port, &br_group); 8478ef2a9a5SYOSHIFUJI Hideaki 84808b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 84908b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 85008b202b6SYOSHIFUJI Hideaki __br_multicast_send_query(br, port, &br_group); 85108b202b6SYOSHIFUJI Hideaki #endif 85208b202b6SYOSHIFUJI Hideaki 853eb1d1641SHerbert Xu time = jiffies; 854eb1d1641SHerbert Xu time += sent < br->multicast_startup_query_count ? 855eb1d1641SHerbert Xu br->multicast_startup_query_interval : 856eb1d1641SHerbert Xu br->multicast_query_interval; 857eb1d1641SHerbert Xu mod_timer(port ? &port->multicast_query_timer : 858eb1d1641SHerbert Xu &br->multicast_query_timer, time); 859eb1d1641SHerbert Xu } 860eb1d1641SHerbert Xu 861eb1d1641SHerbert Xu static void br_multicast_port_query_expired(unsigned long data) 862eb1d1641SHerbert Xu { 863eb1d1641SHerbert Xu struct net_bridge_port *port = (void *)data; 864eb1d1641SHerbert Xu struct net_bridge *br = port->br; 865eb1d1641SHerbert Xu 866eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 86702a780c0SDan Carpenter if (port->state == BR_STATE_DISABLED || 86802a780c0SDan Carpenter port->state == BR_STATE_BLOCKING) 869eb1d1641SHerbert Xu goto out; 870eb1d1641SHerbert Xu 871eb1d1641SHerbert Xu if (port->multicast_startup_queries_sent < 872eb1d1641SHerbert Xu br->multicast_startup_query_count) 873eb1d1641SHerbert Xu port->multicast_startup_queries_sent++; 874eb1d1641SHerbert Xu 875eb1d1641SHerbert Xu br_multicast_send_query(port->br, port, 876eb1d1641SHerbert Xu port->multicast_startup_queries_sent); 877eb1d1641SHerbert Xu 878eb1d1641SHerbert Xu out: 879eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 880eb1d1641SHerbert Xu } 881eb1d1641SHerbert Xu 882eb1d1641SHerbert Xu void br_multicast_add_port(struct net_bridge_port *port) 883eb1d1641SHerbert Xu { 884eb1d1641SHerbert Xu port->multicast_router = 1; 885eb1d1641SHerbert Xu 886eb1d1641SHerbert Xu setup_timer(&port->multicast_router_timer, br_multicast_router_expired, 887eb1d1641SHerbert Xu (unsigned long)port); 888eb1d1641SHerbert Xu setup_timer(&port->multicast_query_timer, 889eb1d1641SHerbert Xu br_multicast_port_query_expired, (unsigned long)port); 890eb1d1641SHerbert Xu } 891eb1d1641SHerbert Xu 892eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port) 893eb1d1641SHerbert Xu { 894eb1d1641SHerbert Xu del_timer_sync(&port->multicast_router_timer); 895eb1d1641SHerbert Xu } 896eb1d1641SHerbert Xu 897561f1103SHerbert Xu static void __br_multicast_enable_port(struct net_bridge_port *port) 898561f1103SHerbert Xu { 899561f1103SHerbert Xu port->multicast_startup_queries_sent = 0; 900561f1103SHerbert Xu 901561f1103SHerbert Xu if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 || 902561f1103SHerbert Xu del_timer(&port->multicast_query_timer)) 903561f1103SHerbert Xu mod_timer(&port->multicast_query_timer, jiffies); 904561f1103SHerbert Xu } 905561f1103SHerbert Xu 906eb1d1641SHerbert Xu void br_multicast_enable_port(struct net_bridge_port *port) 907eb1d1641SHerbert Xu { 908eb1d1641SHerbert Xu struct net_bridge *br = port->br; 909eb1d1641SHerbert Xu 910eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 911eb1d1641SHerbert Xu if (br->multicast_disabled || !netif_running(br->dev)) 912eb1d1641SHerbert Xu goto out; 913eb1d1641SHerbert Xu 914561f1103SHerbert Xu __br_multicast_enable_port(port); 915eb1d1641SHerbert Xu 916eb1d1641SHerbert Xu out: 917eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 918eb1d1641SHerbert Xu } 919eb1d1641SHerbert Xu 920eb1d1641SHerbert Xu void br_multicast_disable_port(struct net_bridge_port *port) 921eb1d1641SHerbert Xu { 922eb1d1641SHerbert Xu struct net_bridge *br = port->br; 923eb1d1641SHerbert Xu struct net_bridge_port_group *pg; 924eb1d1641SHerbert Xu struct hlist_node *p, *n; 925eb1d1641SHerbert Xu 926eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 927eb1d1641SHerbert Xu hlist_for_each_entry_safe(pg, p, n, &port->mglist, mglist) 928eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 929eb1d1641SHerbert Xu 930eb1d1641SHerbert Xu if (!hlist_unhashed(&port->rlist)) 931eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 932eb1d1641SHerbert Xu del_timer(&port->multicast_router_timer); 933eb1d1641SHerbert Xu del_timer(&port->multicast_query_timer); 934eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 935eb1d1641SHerbert Xu } 936eb1d1641SHerbert Xu 9378ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_igmp3_report(struct net_bridge *br, 938eb1d1641SHerbert Xu struct net_bridge_port *port, 939eb1d1641SHerbert Xu struct sk_buff *skb) 940eb1d1641SHerbert Xu { 941eb1d1641SHerbert Xu struct igmpv3_report *ih; 942eb1d1641SHerbert Xu struct igmpv3_grec *grec; 943eb1d1641SHerbert Xu int i; 944eb1d1641SHerbert Xu int len; 945eb1d1641SHerbert Xu int num; 946eb1d1641SHerbert Xu int type; 947eb1d1641SHerbert Xu int err = 0; 948eb1d1641SHerbert Xu __be32 group; 949eb1d1641SHerbert Xu 950eb1d1641SHerbert Xu if (!pskb_may_pull(skb, sizeof(*ih))) 951eb1d1641SHerbert Xu return -EINVAL; 952eb1d1641SHerbert Xu 953eb1d1641SHerbert Xu ih = igmpv3_report_hdr(skb); 954eb1d1641SHerbert Xu num = ntohs(ih->ngrec); 955eb1d1641SHerbert Xu len = sizeof(*ih); 956eb1d1641SHerbert Xu 957eb1d1641SHerbert Xu for (i = 0; i < num; i++) { 958eb1d1641SHerbert Xu len += sizeof(*grec); 959eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 960eb1d1641SHerbert Xu return -EINVAL; 961eb1d1641SHerbert Xu 962fd218cf9SHerbert Xu grec = (void *)(skb->data + len - sizeof(*grec)); 963eb1d1641SHerbert Xu group = grec->grec_mca; 964eb1d1641SHerbert Xu type = grec->grec_type; 965eb1d1641SHerbert Xu 9668eabf95cSEric Dumazet len += ntohs(grec->grec_nsrcs) * 4; 967eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 968eb1d1641SHerbert Xu return -EINVAL; 969eb1d1641SHerbert Xu 970eb1d1641SHerbert Xu /* We treat this as an IGMPv2 report for now. */ 971eb1d1641SHerbert Xu switch (type) { 972eb1d1641SHerbert Xu case IGMPV3_MODE_IS_INCLUDE: 973eb1d1641SHerbert Xu case IGMPV3_MODE_IS_EXCLUDE: 974eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_INCLUDE: 975eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_EXCLUDE: 976eb1d1641SHerbert Xu case IGMPV3_ALLOW_NEW_SOURCES: 977eb1d1641SHerbert Xu case IGMPV3_BLOCK_OLD_SOURCES: 978eb1d1641SHerbert Xu break; 979eb1d1641SHerbert Xu 980eb1d1641SHerbert Xu default: 981eb1d1641SHerbert Xu continue; 982eb1d1641SHerbert Xu } 983eb1d1641SHerbert Xu 9848ef2a9a5SYOSHIFUJI Hideaki err = br_ip4_multicast_add_group(br, port, group); 985eb1d1641SHerbert Xu if (err) 986eb1d1641SHerbert Xu break; 987eb1d1641SHerbert Xu } 988eb1d1641SHerbert Xu 989eb1d1641SHerbert Xu return err; 990eb1d1641SHerbert Xu } 991eb1d1641SHerbert Xu 99208b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 99308b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_mld2_report(struct net_bridge *br, 99408b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 99508b202b6SYOSHIFUJI Hideaki struct sk_buff *skb) 99608b202b6SYOSHIFUJI Hideaki { 99708b202b6SYOSHIFUJI Hideaki struct icmp6hdr *icmp6h; 99808b202b6SYOSHIFUJI Hideaki struct mld2_grec *grec; 99908b202b6SYOSHIFUJI Hideaki int i; 100008b202b6SYOSHIFUJI Hideaki int len; 100108b202b6SYOSHIFUJI Hideaki int num; 100208b202b6SYOSHIFUJI Hideaki int err = 0; 100308b202b6SYOSHIFUJI Hideaki 100408b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*icmp6h))) 100508b202b6SYOSHIFUJI Hideaki return -EINVAL; 100608b202b6SYOSHIFUJI Hideaki 100708b202b6SYOSHIFUJI Hideaki icmp6h = icmp6_hdr(skb); 100808b202b6SYOSHIFUJI Hideaki num = ntohs(icmp6h->icmp6_dataun.un_data16[1]); 100908b202b6SYOSHIFUJI Hideaki len = sizeof(*icmp6h); 101008b202b6SYOSHIFUJI Hideaki 101108b202b6SYOSHIFUJI Hideaki for (i = 0; i < num; i++) { 101208b202b6SYOSHIFUJI Hideaki __be16 *nsrcs, _nsrcs; 101308b202b6SYOSHIFUJI Hideaki 101408b202b6SYOSHIFUJI Hideaki nsrcs = skb_header_pointer(skb, 101508b202b6SYOSHIFUJI Hideaki len + offsetof(struct mld2_grec, 1016*649e984dSLinus Lüssing grec_nsrcs), 101708b202b6SYOSHIFUJI Hideaki sizeof(_nsrcs), &_nsrcs); 101808b202b6SYOSHIFUJI Hideaki if (!nsrcs) 101908b202b6SYOSHIFUJI Hideaki return -EINVAL; 102008b202b6SYOSHIFUJI Hideaki 102108b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, 102208b202b6SYOSHIFUJI Hideaki len + sizeof(*grec) + 102308b202b6SYOSHIFUJI Hideaki sizeof(struct in6_addr) * (*nsrcs))) 102408b202b6SYOSHIFUJI Hideaki return -EINVAL; 102508b202b6SYOSHIFUJI Hideaki 102608b202b6SYOSHIFUJI Hideaki grec = (struct mld2_grec *)(skb->data + len); 102708b202b6SYOSHIFUJI Hideaki len += sizeof(*grec) + sizeof(struct in6_addr) * (*nsrcs); 102808b202b6SYOSHIFUJI Hideaki 102908b202b6SYOSHIFUJI Hideaki /* We treat these as MLDv1 reports for now. */ 103008b202b6SYOSHIFUJI Hideaki switch (grec->grec_type) { 103108b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_INCLUDE: 103208b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_EXCLUDE: 103308b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_INCLUDE: 103408b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_EXCLUDE: 103508b202b6SYOSHIFUJI Hideaki case MLD2_ALLOW_NEW_SOURCES: 103608b202b6SYOSHIFUJI Hideaki case MLD2_BLOCK_OLD_SOURCES: 103708b202b6SYOSHIFUJI Hideaki break; 103808b202b6SYOSHIFUJI Hideaki 103908b202b6SYOSHIFUJI Hideaki default: 104008b202b6SYOSHIFUJI Hideaki continue; 104108b202b6SYOSHIFUJI Hideaki } 104208b202b6SYOSHIFUJI Hideaki 104308b202b6SYOSHIFUJI Hideaki err = br_ip6_multicast_add_group(br, port, &grec->grec_mca); 104408b202b6SYOSHIFUJI Hideaki if (!err) 104508b202b6SYOSHIFUJI Hideaki break; 104608b202b6SYOSHIFUJI Hideaki } 104708b202b6SYOSHIFUJI Hideaki 104808b202b6SYOSHIFUJI Hideaki return err; 104908b202b6SYOSHIFUJI Hideaki } 105008b202b6SYOSHIFUJI Hideaki #endif 105108b202b6SYOSHIFUJI Hideaki 10527e80c124Sstephen hemminger /* 10537e80c124Sstephen hemminger * Add port to rotuer_list 10547e80c124Sstephen hemminger * list is maintained ordered by pointer value 10557e80c124Sstephen hemminger * and locked by br->multicast_lock and RCU 10567e80c124Sstephen hemminger */ 10570909e117SHerbert Xu static void br_multicast_add_router(struct net_bridge *br, 10580909e117SHerbert Xu struct net_bridge_port *port) 10590909e117SHerbert Xu { 1060dcdca2c4Sstephen hemminger struct net_bridge_port *p; 10617e80c124Sstephen hemminger struct hlist_node *n, *slot = NULL; 10620909e117SHerbert Xu 1063709b9326SDavid S. Miller hlist_for_each_entry(p, n, &br->router_list, rlist) { 10647e80c124Sstephen hemminger if ((unsigned long) port >= (unsigned long) p) 10657e80c124Sstephen hemminger break; 10667e80c124Sstephen hemminger slot = n; 1067dcdca2c4Sstephen hemminger } 10680909e117SHerbert Xu 10697e80c124Sstephen hemminger if (slot) 10707e80c124Sstephen hemminger hlist_add_after_rcu(slot, &port->rlist); 1071dcdca2c4Sstephen hemminger else 1072dcdca2c4Sstephen hemminger hlist_add_head_rcu(&port->rlist, &br->router_list); 10730909e117SHerbert Xu } 10740909e117SHerbert Xu 1075eb1d1641SHerbert Xu static void br_multicast_mark_router(struct net_bridge *br, 1076eb1d1641SHerbert Xu struct net_bridge_port *port) 1077eb1d1641SHerbert Xu { 1078eb1d1641SHerbert Xu unsigned long now = jiffies; 1079eb1d1641SHerbert Xu 1080eb1d1641SHerbert Xu if (!port) { 1081eb1d1641SHerbert Xu if (br->multicast_router == 1) 1082eb1d1641SHerbert Xu mod_timer(&br->multicast_router_timer, 1083eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1084eb1d1641SHerbert Xu return; 1085eb1d1641SHerbert Xu } 1086eb1d1641SHerbert Xu 1087eb1d1641SHerbert Xu if (port->multicast_router != 1) 1088eb1d1641SHerbert Xu return; 1089eb1d1641SHerbert Xu 1090eb1d1641SHerbert Xu if (!hlist_unhashed(&port->rlist)) 1091eb1d1641SHerbert Xu goto timer; 1092eb1d1641SHerbert Xu 10930909e117SHerbert Xu br_multicast_add_router(br, port); 1094eb1d1641SHerbert Xu 1095eb1d1641SHerbert Xu timer: 1096eb1d1641SHerbert Xu mod_timer(&port->multicast_router_timer, 1097eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1098eb1d1641SHerbert Xu } 1099eb1d1641SHerbert Xu 1100eb1d1641SHerbert Xu static void br_multicast_query_received(struct net_bridge *br, 1101eb1d1641SHerbert Xu struct net_bridge_port *port, 11028ef2a9a5SYOSHIFUJI Hideaki int saddr) 1103eb1d1641SHerbert Xu { 1104eb1d1641SHerbert Xu if (saddr) 1105eb1d1641SHerbert Xu mod_timer(&br->multicast_querier_timer, 1106eb1d1641SHerbert Xu jiffies + br->multicast_querier_interval); 1107eb1d1641SHerbert Xu else if (timer_pending(&br->multicast_querier_timer)) 1108eb1d1641SHerbert Xu return; 1109eb1d1641SHerbert Xu 1110eb1d1641SHerbert Xu br_multicast_mark_router(br, port); 1111eb1d1641SHerbert Xu } 1112eb1d1641SHerbert Xu 11138ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_query(struct net_bridge *br, 1114eb1d1641SHerbert Xu struct net_bridge_port *port, 1115eb1d1641SHerbert Xu struct sk_buff *skb) 1116eb1d1641SHerbert Xu { 1117eb1d1641SHerbert Xu struct iphdr *iph = ip_hdr(skb); 1118eb1d1641SHerbert Xu struct igmphdr *ih = igmp_hdr(skb); 1119eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1120eb1d1641SHerbert Xu struct igmpv3_query *ih3; 1121eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1122e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 1123eb1d1641SHerbert Xu unsigned long max_delay; 1124eb1d1641SHerbert Xu unsigned long now = jiffies; 1125eb1d1641SHerbert Xu __be32 group; 1126bec68ff1SYOSHIFUJI Hideaki int err = 0; 1127eb1d1641SHerbert Xu 1128eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1129eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1130eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 1131eb1d1641SHerbert Xu goto out; 1132eb1d1641SHerbert Xu 11338ef2a9a5SYOSHIFUJI Hideaki br_multicast_query_received(br, port, !!iph->saddr); 1134eb1d1641SHerbert Xu 1135eb1d1641SHerbert Xu group = ih->group; 1136eb1d1641SHerbert Xu 1137eb1d1641SHerbert Xu if (skb->len == sizeof(*ih)) { 1138eb1d1641SHerbert Xu max_delay = ih->code * (HZ / IGMP_TIMER_SCALE); 1139eb1d1641SHerbert Xu 1140eb1d1641SHerbert Xu if (!max_delay) { 1141eb1d1641SHerbert Xu max_delay = 10 * HZ; 1142eb1d1641SHerbert Xu group = 0; 1143eb1d1641SHerbert Xu } 1144eb1d1641SHerbert Xu } else { 1145bec68ff1SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) { 1146bec68ff1SYOSHIFUJI Hideaki err = -EINVAL; 1147bec68ff1SYOSHIFUJI Hideaki goto out; 1148bec68ff1SYOSHIFUJI Hideaki } 1149eb1d1641SHerbert Xu 1150eb1d1641SHerbert Xu ih3 = igmpv3_query_hdr(skb); 1151eb1d1641SHerbert Xu if (ih3->nsrcs) 1152bec68ff1SYOSHIFUJI Hideaki goto out; 1153eb1d1641SHerbert Xu 11540ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 max_delay = ih3->code ? 11550ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1; 1156eb1d1641SHerbert Xu } 1157eb1d1641SHerbert Xu 1158eb1d1641SHerbert Xu if (!group) 1159eb1d1641SHerbert Xu goto out; 1160eb1d1641SHerbert Xu 1161e8051688SEric Dumazet mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group); 1162eb1d1641SHerbert Xu if (!mp) 1163eb1d1641SHerbert Xu goto out; 1164eb1d1641SHerbert Xu 1165eb1d1641SHerbert Xu max_delay *= br->multicast_last_member_count; 1166eb1d1641SHerbert Xu 11678a870178SHerbert Xu if (mp->mglist && 1168eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1169eb1d1641SHerbert Xu time_after(mp->timer.expires, now + max_delay) : 1170eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) 1171eb1d1641SHerbert Xu mod_timer(&mp->timer, now + max_delay); 1172eb1d1641SHerbert Xu 1173e8051688SEric Dumazet for (pp = &mp->ports; 1174e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 1175e8051688SEric Dumazet pp = &p->next) { 1176eb1d1641SHerbert Xu if (timer_pending(&p->timer) ? 1177eb1d1641SHerbert Xu time_after(p->timer.expires, now + max_delay) : 1178eb1d1641SHerbert Xu try_to_del_timer_sync(&p->timer) >= 0) 117924f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 1180eb1d1641SHerbert Xu } 1181eb1d1641SHerbert Xu 1182eb1d1641SHerbert Xu out: 1183eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1184bec68ff1SYOSHIFUJI Hideaki return err; 1185eb1d1641SHerbert Xu } 1186eb1d1641SHerbert Xu 118708b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 118808b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_query(struct net_bridge *br, 118908b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 119008b202b6SYOSHIFUJI Hideaki struct sk_buff *skb) 119108b202b6SYOSHIFUJI Hideaki { 119208b202b6SYOSHIFUJI Hideaki struct ipv6hdr *ip6h = ipv6_hdr(skb); 119308b202b6SYOSHIFUJI Hideaki struct mld_msg *mld = (struct mld_msg *) icmp6_hdr(skb); 119408b202b6SYOSHIFUJI Hideaki struct net_bridge_mdb_entry *mp; 119508b202b6SYOSHIFUJI Hideaki struct mld2_query *mld2q; 1196e8051688SEric Dumazet struct net_bridge_port_group *p; 1197e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 119808b202b6SYOSHIFUJI Hideaki unsigned long max_delay; 119908b202b6SYOSHIFUJI Hideaki unsigned long now = jiffies; 120008b202b6SYOSHIFUJI Hideaki struct in6_addr *group = NULL; 120108b202b6SYOSHIFUJI Hideaki int err = 0; 120208b202b6SYOSHIFUJI Hideaki 120308b202b6SYOSHIFUJI Hideaki spin_lock(&br->multicast_lock); 120408b202b6SYOSHIFUJI Hideaki if (!netif_running(br->dev) || 120508b202b6SYOSHIFUJI Hideaki (port && port->state == BR_STATE_DISABLED)) 120608b202b6SYOSHIFUJI Hideaki goto out; 120708b202b6SYOSHIFUJI Hideaki 120808b202b6SYOSHIFUJI Hideaki br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->saddr)); 120908b202b6SYOSHIFUJI Hideaki 121008b202b6SYOSHIFUJI Hideaki if (skb->len == sizeof(*mld)) { 121108b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*mld))) { 121208b202b6SYOSHIFUJI Hideaki err = -EINVAL; 121308b202b6SYOSHIFUJI Hideaki goto out; 121408b202b6SYOSHIFUJI Hideaki } 121508b202b6SYOSHIFUJI Hideaki mld = (struct mld_msg *) icmp6_hdr(skb); 121608b202b6SYOSHIFUJI Hideaki max_delay = msecs_to_jiffies(htons(mld->mld_maxdelay)); 121708b202b6SYOSHIFUJI Hideaki if (max_delay) 121808b202b6SYOSHIFUJI Hideaki group = &mld->mld_mca; 121908b202b6SYOSHIFUJI Hideaki } else if (skb->len >= sizeof(*mld2q)) { 122008b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*mld2q))) { 122108b202b6SYOSHIFUJI Hideaki err = -EINVAL; 122208b202b6SYOSHIFUJI Hideaki goto out; 122308b202b6SYOSHIFUJI Hideaki } 122408b202b6SYOSHIFUJI Hideaki mld2q = (struct mld2_query *)icmp6_hdr(skb); 122508b202b6SYOSHIFUJI Hideaki if (!mld2q->mld2q_nsrcs) 122608b202b6SYOSHIFUJI Hideaki group = &mld2q->mld2q_mca; 122708b202b6SYOSHIFUJI Hideaki max_delay = mld2q->mld2q_mrc ? MLDV2_MRC(mld2q->mld2q_mrc) : 1; 122808b202b6SYOSHIFUJI Hideaki } 122908b202b6SYOSHIFUJI Hideaki 123008b202b6SYOSHIFUJI Hideaki if (!group) 123108b202b6SYOSHIFUJI Hideaki goto out; 123208b202b6SYOSHIFUJI Hideaki 1233e8051688SEric Dumazet mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group); 123408b202b6SYOSHIFUJI Hideaki if (!mp) 123508b202b6SYOSHIFUJI Hideaki goto out; 123608b202b6SYOSHIFUJI Hideaki 123708b202b6SYOSHIFUJI Hideaki max_delay *= br->multicast_last_member_count; 12388a870178SHerbert Xu if (mp->mglist && 123908b202b6SYOSHIFUJI Hideaki (timer_pending(&mp->timer) ? 124008b202b6SYOSHIFUJI Hideaki time_after(mp->timer.expires, now + max_delay) : 124108b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&mp->timer) >= 0)) 124208b202b6SYOSHIFUJI Hideaki mod_timer(&mp->timer, now + max_delay); 124308b202b6SYOSHIFUJI Hideaki 1244e8051688SEric Dumazet for (pp = &mp->ports; 1245e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 1246e8051688SEric Dumazet pp = &p->next) { 124708b202b6SYOSHIFUJI Hideaki if (timer_pending(&p->timer) ? 124808b202b6SYOSHIFUJI Hideaki time_after(p->timer.expires, now + max_delay) : 124908b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&p->timer) >= 0) 125024f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 125108b202b6SYOSHIFUJI Hideaki } 125208b202b6SYOSHIFUJI Hideaki 125308b202b6SYOSHIFUJI Hideaki out: 125408b202b6SYOSHIFUJI Hideaki spin_unlock(&br->multicast_lock); 125508b202b6SYOSHIFUJI Hideaki return err; 125608b202b6SYOSHIFUJI Hideaki } 125708b202b6SYOSHIFUJI Hideaki #endif 125808b202b6SYOSHIFUJI Hideaki 1259eb1d1641SHerbert Xu static void br_multicast_leave_group(struct net_bridge *br, 1260eb1d1641SHerbert Xu struct net_bridge_port *port, 12618ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group) 1262eb1d1641SHerbert Xu { 1263eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 1264eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1265eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1266eb1d1641SHerbert Xu unsigned long now; 1267eb1d1641SHerbert Xu unsigned long time; 1268eb1d1641SHerbert Xu 1269eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1270eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1271eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED) || 1272eb1d1641SHerbert Xu timer_pending(&br->multicast_querier_timer)) 1273eb1d1641SHerbert Xu goto out; 1274eb1d1641SHerbert Xu 1275e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1276eb1d1641SHerbert Xu mp = br_mdb_ip_get(mdb, group); 1277eb1d1641SHerbert Xu if (!mp) 1278eb1d1641SHerbert Xu goto out; 1279eb1d1641SHerbert Xu 1280eb1d1641SHerbert Xu now = jiffies; 1281eb1d1641SHerbert Xu time = now + br->multicast_last_member_count * 1282eb1d1641SHerbert Xu br->multicast_last_member_interval; 1283eb1d1641SHerbert Xu 1284eb1d1641SHerbert Xu if (!port) { 12858a870178SHerbert Xu if (mp->mglist && 1286eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1287eb1d1641SHerbert Xu time_after(mp->timer.expires, time) : 1288eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) { 1289eb1d1641SHerbert Xu mod_timer(&mp->timer, time); 1290eb1d1641SHerbert Xu 1291eb1d1641SHerbert Xu mp->queries_sent = 0; 1292eb1d1641SHerbert Xu mod_timer(&mp->query_timer, now); 1293eb1d1641SHerbert Xu } 1294eb1d1641SHerbert Xu 1295eb1d1641SHerbert Xu goto out; 1296eb1d1641SHerbert Xu } 1297eb1d1641SHerbert Xu 1298e8051688SEric Dumazet for (p = mlock_dereference(mp->ports, br); 1299e8051688SEric Dumazet p != NULL; 1300e8051688SEric Dumazet p = mlock_dereference(p->next, br)) { 1301eb1d1641SHerbert Xu if (p->port != port) 1302eb1d1641SHerbert Xu continue; 1303eb1d1641SHerbert Xu 1304eb1d1641SHerbert Xu if (!hlist_unhashed(&p->mglist) && 1305eb1d1641SHerbert Xu (timer_pending(&p->timer) ? 1306eb1d1641SHerbert Xu time_after(p->timer.expires, time) : 1307eb1d1641SHerbert Xu try_to_del_timer_sync(&p->timer) >= 0)) { 1308eb1d1641SHerbert Xu mod_timer(&p->timer, time); 1309eb1d1641SHerbert Xu 1310eb1d1641SHerbert Xu p->queries_sent = 0; 1311eb1d1641SHerbert Xu mod_timer(&p->query_timer, now); 1312eb1d1641SHerbert Xu } 1313eb1d1641SHerbert Xu 1314eb1d1641SHerbert Xu break; 1315eb1d1641SHerbert Xu } 1316eb1d1641SHerbert Xu 1317eb1d1641SHerbert Xu out: 1318eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1319eb1d1641SHerbert Xu } 1320eb1d1641SHerbert Xu 13218ef2a9a5SYOSHIFUJI Hideaki static void br_ip4_multicast_leave_group(struct net_bridge *br, 13228ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 13238ef2a9a5SYOSHIFUJI Hideaki __be32 group) 13248ef2a9a5SYOSHIFUJI Hideaki { 13258ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 13268ef2a9a5SYOSHIFUJI Hideaki 13278ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 13288ef2a9a5SYOSHIFUJI Hideaki return; 13298ef2a9a5SYOSHIFUJI Hideaki 13308ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 13318ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 13328ef2a9a5SYOSHIFUJI Hideaki 13338ef2a9a5SYOSHIFUJI Hideaki br_multicast_leave_group(br, port, &br_group); 13348ef2a9a5SYOSHIFUJI Hideaki } 13358ef2a9a5SYOSHIFUJI Hideaki 133608b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 133708b202b6SYOSHIFUJI Hideaki static void br_ip6_multicast_leave_group(struct net_bridge *br, 133808b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 133908b202b6SYOSHIFUJI Hideaki const struct in6_addr *group) 134008b202b6SYOSHIFUJI Hideaki { 134108b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 134208b202b6SYOSHIFUJI Hideaki 134308b202b6SYOSHIFUJI Hideaki if (ipv6_is_local_multicast(group)) 134408b202b6SYOSHIFUJI Hideaki return; 134508b202b6SYOSHIFUJI Hideaki 134608b202b6SYOSHIFUJI Hideaki ipv6_addr_copy(&br_group.u.ip6, group); 134708b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 134808b202b6SYOSHIFUJI Hideaki 134908b202b6SYOSHIFUJI Hideaki br_multicast_leave_group(br, port, &br_group); 135008b202b6SYOSHIFUJI Hideaki } 135108b202b6SYOSHIFUJI Hideaki #endif 13528ef2a9a5SYOSHIFUJI Hideaki 1353eb1d1641SHerbert Xu static int br_multicast_ipv4_rcv(struct net_bridge *br, 1354eb1d1641SHerbert Xu struct net_bridge_port *port, 1355eb1d1641SHerbert Xu struct sk_buff *skb) 1356eb1d1641SHerbert Xu { 1357eb1d1641SHerbert Xu struct sk_buff *skb2 = skb; 1358eb1d1641SHerbert Xu struct iphdr *iph; 1359eb1d1641SHerbert Xu struct igmphdr *ih; 1360eb1d1641SHerbert Xu unsigned len; 1361eb1d1641SHerbert Xu unsigned offset; 1362eb1d1641SHerbert Xu int err; 1363eb1d1641SHerbert Xu 1364eb1d1641SHerbert Xu /* We treat OOM as packet loss for now. */ 1365eb1d1641SHerbert Xu if (!pskb_may_pull(skb, sizeof(*iph))) 1366eb1d1641SHerbert Xu return -EINVAL; 1367eb1d1641SHerbert Xu 1368eb1d1641SHerbert Xu iph = ip_hdr(skb); 1369eb1d1641SHerbert Xu 1370eb1d1641SHerbert Xu if (iph->ihl < 5 || iph->version != 4) 1371eb1d1641SHerbert Xu return -EINVAL; 1372eb1d1641SHerbert Xu 1373eb1d1641SHerbert Xu if (!pskb_may_pull(skb, ip_hdrlen(skb))) 1374eb1d1641SHerbert Xu return -EINVAL; 1375eb1d1641SHerbert Xu 1376eb1d1641SHerbert Xu iph = ip_hdr(skb); 1377eb1d1641SHerbert Xu 1378eb1d1641SHerbert Xu if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl))) 1379eb1d1641SHerbert Xu return -EINVAL; 1380eb1d1641SHerbert Xu 1381eb1d1641SHerbert Xu if (iph->protocol != IPPROTO_IGMP) 1382eb1d1641SHerbert Xu return 0; 1383eb1d1641SHerbert Xu 1384eb1d1641SHerbert Xu len = ntohs(iph->tot_len); 1385eb1d1641SHerbert Xu if (skb->len < len || len < ip_hdrlen(skb)) 1386eb1d1641SHerbert Xu return -EINVAL; 1387eb1d1641SHerbert Xu 1388eb1d1641SHerbert Xu if (skb->len > len) { 1389eb1d1641SHerbert Xu skb2 = skb_clone(skb, GFP_ATOMIC); 1390eb1d1641SHerbert Xu if (!skb2) 1391eb1d1641SHerbert Xu return -ENOMEM; 1392eb1d1641SHerbert Xu 1393eb1d1641SHerbert Xu err = pskb_trim_rcsum(skb2, len); 1394eb1d1641SHerbert Xu if (err) 13958440853bSYOSHIFUJI Hideaki / 吉藤英明 goto err_out; 1396eb1d1641SHerbert Xu } 1397eb1d1641SHerbert Xu 1398eb1d1641SHerbert Xu len -= ip_hdrlen(skb2); 1399eb1d1641SHerbert Xu offset = skb_network_offset(skb2) + ip_hdrlen(skb2); 1400eb1d1641SHerbert Xu __skb_pull(skb2, offset); 1401eb1d1641SHerbert Xu skb_reset_transport_header(skb2); 1402eb1d1641SHerbert Xu 1403eb1d1641SHerbert Xu err = -EINVAL; 1404eb1d1641SHerbert Xu if (!pskb_may_pull(skb2, sizeof(*ih))) 1405eb1d1641SHerbert Xu goto out; 1406eb1d1641SHerbert Xu 1407eb1d1641SHerbert Xu switch (skb2->ip_summed) { 1408eb1d1641SHerbert Xu case CHECKSUM_COMPLETE: 1409eb1d1641SHerbert Xu if (!csum_fold(skb2->csum)) 1410eb1d1641SHerbert Xu break; 1411eb1d1641SHerbert Xu /* fall through */ 1412eb1d1641SHerbert Xu case CHECKSUM_NONE: 1413eb1d1641SHerbert Xu skb2->csum = 0; 1414eb1d1641SHerbert Xu if (skb_checksum_complete(skb2)) 14158440853bSYOSHIFUJI Hideaki / 吉藤英明 goto out; 1416eb1d1641SHerbert Xu } 1417eb1d1641SHerbert Xu 1418eb1d1641SHerbert Xu err = 0; 1419eb1d1641SHerbert Xu 1420eb1d1641SHerbert Xu BR_INPUT_SKB_CB(skb)->igmp = 1; 1421eb1d1641SHerbert Xu ih = igmp_hdr(skb2); 1422eb1d1641SHerbert Xu 1423eb1d1641SHerbert Xu switch (ih->type) { 1424eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_REPORT: 1425eb1d1641SHerbert Xu case IGMPV2_HOST_MEMBERSHIP_REPORT: 1426eb1d1641SHerbert Xu BR_INPUT_SKB_CB(skb2)->mrouters_only = 1; 14278ef2a9a5SYOSHIFUJI Hideaki err = br_ip4_multicast_add_group(br, port, ih->group); 1428eb1d1641SHerbert Xu break; 1429eb1d1641SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 14308ef2a9a5SYOSHIFUJI Hideaki err = br_ip4_multicast_igmp3_report(br, port, skb2); 1431eb1d1641SHerbert Xu break; 1432eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_QUERY: 14338ef2a9a5SYOSHIFUJI Hideaki err = br_ip4_multicast_query(br, port, skb2); 1434eb1d1641SHerbert Xu break; 1435eb1d1641SHerbert Xu case IGMP_HOST_LEAVE_MESSAGE: 14368ef2a9a5SYOSHIFUJI Hideaki br_ip4_multicast_leave_group(br, port, ih->group); 1437eb1d1641SHerbert Xu break; 1438eb1d1641SHerbert Xu } 1439eb1d1641SHerbert Xu 1440eb1d1641SHerbert Xu out: 1441eb1d1641SHerbert Xu __skb_push(skb2, offset); 14428440853bSYOSHIFUJI Hideaki / 吉藤英明 err_out: 1443eb1d1641SHerbert Xu if (skb2 != skb) 1444eb1d1641SHerbert Xu kfree_skb(skb2); 1445eb1d1641SHerbert Xu return err; 1446eb1d1641SHerbert Xu } 1447eb1d1641SHerbert Xu 144808b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 144908b202b6SYOSHIFUJI Hideaki static int br_multicast_ipv6_rcv(struct net_bridge *br, 145008b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 145108b202b6SYOSHIFUJI Hideaki struct sk_buff *skb) 145208b202b6SYOSHIFUJI Hideaki { 14539d89081dSTomas Winkler struct sk_buff *skb2; 145408b202b6SYOSHIFUJI Hideaki struct ipv6hdr *ip6h; 145508b202b6SYOSHIFUJI Hideaki struct icmp6hdr *icmp6h; 145608b202b6SYOSHIFUJI Hideaki u8 nexthdr; 145708b202b6SYOSHIFUJI Hideaki unsigned len; 1458bb7a0bd6SKulikov Vasiliy int offset; 145908b202b6SYOSHIFUJI Hideaki int err; 146008b202b6SYOSHIFUJI Hideaki 146108b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*ip6h))) 146208b202b6SYOSHIFUJI Hideaki return -EINVAL; 146308b202b6SYOSHIFUJI Hideaki 146408b202b6SYOSHIFUJI Hideaki ip6h = ipv6_hdr(skb); 146508b202b6SYOSHIFUJI Hideaki 146608b202b6SYOSHIFUJI Hideaki /* 146708b202b6SYOSHIFUJI Hideaki * We're interested in MLD messages only. 146808b202b6SYOSHIFUJI Hideaki * - Version is 6 146908b202b6SYOSHIFUJI Hideaki * - MLD has always Router Alert hop-by-hop option 147008b202b6SYOSHIFUJI Hideaki * - But we do not support jumbrograms. 147108b202b6SYOSHIFUJI Hideaki */ 147208b202b6SYOSHIFUJI Hideaki if (ip6h->version != 6 || 147308b202b6SYOSHIFUJI Hideaki ip6h->nexthdr != IPPROTO_HOPOPTS || 147408b202b6SYOSHIFUJI Hideaki ip6h->payload_len == 0) 147508b202b6SYOSHIFUJI Hideaki return 0; 147608b202b6SYOSHIFUJI Hideaki 147708b202b6SYOSHIFUJI Hideaki len = ntohs(ip6h->payload_len); 147808b202b6SYOSHIFUJI Hideaki if (skb->len < len) 147908b202b6SYOSHIFUJI Hideaki return -EINVAL; 148008b202b6SYOSHIFUJI Hideaki 148108b202b6SYOSHIFUJI Hideaki nexthdr = ip6h->nexthdr; 148208b202b6SYOSHIFUJI Hideaki offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr); 148308b202b6SYOSHIFUJI Hideaki 148408b202b6SYOSHIFUJI Hideaki if (offset < 0 || nexthdr != IPPROTO_ICMPV6) 148508b202b6SYOSHIFUJI Hideaki return 0; 148608b202b6SYOSHIFUJI Hideaki 148708b202b6SYOSHIFUJI Hideaki /* Okay, we found ICMPv6 header */ 148808b202b6SYOSHIFUJI Hideaki skb2 = skb_clone(skb, GFP_ATOMIC); 148908b202b6SYOSHIFUJI Hideaki if (!skb2) 149008b202b6SYOSHIFUJI Hideaki return -ENOMEM; 149108b202b6SYOSHIFUJI Hideaki 14929d89081dSTomas Winkler err = -EINVAL; 14939d89081dSTomas Winkler if (!pskb_may_pull(skb2, offset + sizeof(struct icmp6hdr))) 14949d89081dSTomas Winkler goto out; 14959d89081dSTomas Winkler 149608b202b6SYOSHIFUJI Hideaki len -= offset - skb_network_offset(skb2); 149708b202b6SYOSHIFUJI Hideaki 149808b202b6SYOSHIFUJI Hideaki __skb_pull(skb2, offset); 149908b202b6SYOSHIFUJI Hideaki skb_reset_transport_header(skb2); 150008b202b6SYOSHIFUJI Hideaki 150108b202b6SYOSHIFUJI Hideaki icmp6h = icmp6_hdr(skb2); 150208b202b6SYOSHIFUJI Hideaki 150308b202b6SYOSHIFUJI Hideaki switch (icmp6h->icmp6_type) { 150408b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 150508b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 150608b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 150708b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 150808b202b6SYOSHIFUJI Hideaki break; 150908b202b6SYOSHIFUJI Hideaki default: 151008b202b6SYOSHIFUJI Hideaki err = 0; 151108b202b6SYOSHIFUJI Hideaki goto out; 151208b202b6SYOSHIFUJI Hideaki } 151308b202b6SYOSHIFUJI Hideaki 151408b202b6SYOSHIFUJI Hideaki /* Okay, we found MLD message. Check further. */ 151508b202b6SYOSHIFUJI Hideaki if (skb2->len > len) { 151608b202b6SYOSHIFUJI Hideaki err = pskb_trim_rcsum(skb2, len); 151708b202b6SYOSHIFUJI Hideaki if (err) 151808b202b6SYOSHIFUJI Hideaki goto out; 151908b202b6SYOSHIFUJI Hideaki } 152008b202b6SYOSHIFUJI Hideaki 152108b202b6SYOSHIFUJI Hideaki switch (skb2->ip_summed) { 152208b202b6SYOSHIFUJI Hideaki case CHECKSUM_COMPLETE: 152308b202b6SYOSHIFUJI Hideaki if (!csum_fold(skb2->csum)) 152408b202b6SYOSHIFUJI Hideaki break; 152508b202b6SYOSHIFUJI Hideaki /*FALLTHROUGH*/ 152608b202b6SYOSHIFUJI Hideaki case CHECKSUM_NONE: 152708b202b6SYOSHIFUJI Hideaki skb2->csum = 0; 152808b202b6SYOSHIFUJI Hideaki if (skb_checksum_complete(skb2)) 152908b202b6SYOSHIFUJI Hideaki goto out; 153008b202b6SYOSHIFUJI Hideaki } 153108b202b6SYOSHIFUJI Hideaki 153208b202b6SYOSHIFUJI Hideaki err = 0; 153308b202b6SYOSHIFUJI Hideaki 153408b202b6SYOSHIFUJI Hideaki BR_INPUT_SKB_CB(skb)->igmp = 1; 153508b202b6SYOSHIFUJI Hideaki 153608b202b6SYOSHIFUJI Hideaki switch (icmp6h->icmp6_type) { 153708b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 153808b202b6SYOSHIFUJI Hideaki { 15399d89081dSTomas Winkler struct mld_msg *mld; 15409d89081dSTomas Winkler if (!pskb_may_pull(skb2, sizeof(*mld))) { 15419d89081dSTomas Winkler err = -EINVAL; 15429d89081dSTomas Winkler goto out; 15439d89081dSTomas Winkler } 15449d89081dSTomas Winkler mld = (struct mld_msg *)skb_transport_header(skb2); 154508b202b6SYOSHIFUJI Hideaki BR_INPUT_SKB_CB(skb2)->mrouters_only = 1; 154608b202b6SYOSHIFUJI Hideaki err = br_ip6_multicast_add_group(br, port, &mld->mld_mca); 154708b202b6SYOSHIFUJI Hideaki break; 154808b202b6SYOSHIFUJI Hideaki } 154908b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 155008b202b6SYOSHIFUJI Hideaki err = br_ip6_multicast_mld2_report(br, port, skb2); 155108b202b6SYOSHIFUJI Hideaki break; 155208b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 155308b202b6SYOSHIFUJI Hideaki err = br_ip6_multicast_query(br, port, skb2); 155408b202b6SYOSHIFUJI Hideaki break; 155508b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 155608b202b6SYOSHIFUJI Hideaki { 15579d89081dSTomas Winkler struct mld_msg *mld; 15589d89081dSTomas Winkler if (!pskb_may_pull(skb2, sizeof(*mld))) { 15599d89081dSTomas Winkler err = -EINVAL; 15609d89081dSTomas Winkler goto out; 15619d89081dSTomas Winkler } 15629d89081dSTomas Winkler mld = (struct mld_msg *)skb_transport_header(skb2); 156308b202b6SYOSHIFUJI Hideaki br_ip6_multicast_leave_group(br, port, &mld->mld_mca); 156408b202b6SYOSHIFUJI Hideaki } 156508b202b6SYOSHIFUJI Hideaki } 156608b202b6SYOSHIFUJI Hideaki 156708b202b6SYOSHIFUJI Hideaki out: 156808b202b6SYOSHIFUJI Hideaki kfree_skb(skb2); 156908b202b6SYOSHIFUJI Hideaki return err; 157008b202b6SYOSHIFUJI Hideaki } 157108b202b6SYOSHIFUJI Hideaki #endif 157208b202b6SYOSHIFUJI Hideaki 1573eb1d1641SHerbert Xu int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port, 1574eb1d1641SHerbert Xu struct sk_buff *skb) 1575eb1d1641SHerbert Xu { 15761fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->igmp = 0; 15771fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->mrouters_only = 0; 15781fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 1579eb1d1641SHerbert Xu if (br->multicast_disabled) 1580eb1d1641SHerbert Xu return 0; 1581eb1d1641SHerbert Xu 1582eb1d1641SHerbert Xu switch (skb->protocol) { 1583eb1d1641SHerbert Xu case htons(ETH_P_IP): 1584eb1d1641SHerbert Xu return br_multicast_ipv4_rcv(br, port, skb); 158508b202b6SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 158608b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 158708b202b6SYOSHIFUJI Hideaki return br_multicast_ipv6_rcv(br, port, skb); 158808b202b6SYOSHIFUJI Hideaki #endif 1589eb1d1641SHerbert Xu } 1590eb1d1641SHerbert Xu 1591eb1d1641SHerbert Xu return 0; 1592eb1d1641SHerbert Xu } 1593eb1d1641SHerbert Xu 1594eb1d1641SHerbert Xu static void br_multicast_query_expired(unsigned long data) 1595eb1d1641SHerbert Xu { 1596eb1d1641SHerbert Xu struct net_bridge *br = (void *)data; 1597eb1d1641SHerbert Xu 1598eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1599eb1d1641SHerbert Xu if (br->multicast_startup_queries_sent < 1600eb1d1641SHerbert Xu br->multicast_startup_query_count) 1601eb1d1641SHerbert Xu br->multicast_startup_queries_sent++; 1602eb1d1641SHerbert Xu 1603eb1d1641SHerbert Xu br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent); 1604eb1d1641SHerbert Xu 1605eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1606eb1d1641SHerbert Xu } 1607eb1d1641SHerbert Xu 1608eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br) 1609eb1d1641SHerbert Xu { 1610eb1d1641SHerbert Xu br->hash_elasticity = 4; 1611eb1d1641SHerbert Xu br->hash_max = 512; 1612eb1d1641SHerbert Xu 1613eb1d1641SHerbert Xu br->multicast_router = 1; 1614eb1d1641SHerbert Xu br->multicast_last_member_count = 2; 1615eb1d1641SHerbert Xu br->multicast_startup_query_count = 2; 1616eb1d1641SHerbert Xu 1617eb1d1641SHerbert Xu br->multicast_last_member_interval = HZ; 1618eb1d1641SHerbert Xu br->multicast_query_response_interval = 10 * HZ; 1619eb1d1641SHerbert Xu br->multicast_startup_query_interval = 125 * HZ / 4; 1620eb1d1641SHerbert Xu br->multicast_query_interval = 125 * HZ; 1621eb1d1641SHerbert Xu br->multicast_querier_interval = 255 * HZ; 1622eb1d1641SHerbert Xu br->multicast_membership_interval = 260 * HZ; 1623eb1d1641SHerbert Xu 1624eb1d1641SHerbert Xu spin_lock_init(&br->multicast_lock); 1625eb1d1641SHerbert Xu setup_timer(&br->multicast_router_timer, 1626eb1d1641SHerbert Xu br_multicast_local_router_expired, 0); 1627eb1d1641SHerbert Xu setup_timer(&br->multicast_querier_timer, 1628eb1d1641SHerbert Xu br_multicast_local_router_expired, 0); 1629eb1d1641SHerbert Xu setup_timer(&br->multicast_query_timer, br_multicast_query_expired, 1630eb1d1641SHerbert Xu (unsigned long)br); 1631eb1d1641SHerbert Xu } 1632eb1d1641SHerbert Xu 1633eb1d1641SHerbert Xu void br_multicast_open(struct net_bridge *br) 1634eb1d1641SHerbert Xu { 1635eb1d1641SHerbert Xu br->multicast_startup_queries_sent = 0; 1636eb1d1641SHerbert Xu 1637eb1d1641SHerbert Xu if (br->multicast_disabled) 1638eb1d1641SHerbert Xu return; 1639eb1d1641SHerbert Xu 1640eb1d1641SHerbert Xu mod_timer(&br->multicast_query_timer, jiffies); 1641eb1d1641SHerbert Xu } 1642eb1d1641SHerbert Xu 1643eb1d1641SHerbert Xu void br_multicast_stop(struct net_bridge *br) 1644eb1d1641SHerbert Xu { 1645eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 1646eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1647eb1d1641SHerbert Xu struct hlist_node *p, *n; 1648eb1d1641SHerbert Xu u32 ver; 1649eb1d1641SHerbert Xu int i; 1650eb1d1641SHerbert Xu 1651eb1d1641SHerbert Xu del_timer_sync(&br->multicast_router_timer); 1652eb1d1641SHerbert Xu del_timer_sync(&br->multicast_querier_timer); 1653eb1d1641SHerbert Xu del_timer_sync(&br->multicast_query_timer); 1654eb1d1641SHerbert Xu 1655eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1656e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1657eb1d1641SHerbert Xu if (!mdb) 1658eb1d1641SHerbert Xu goto out; 1659eb1d1641SHerbert Xu 1660eb1d1641SHerbert Xu br->mdb = NULL; 1661eb1d1641SHerbert Xu 1662eb1d1641SHerbert Xu ver = mdb->ver; 1663eb1d1641SHerbert Xu for (i = 0; i < mdb->max; i++) { 1664eb1d1641SHerbert Xu hlist_for_each_entry_safe(mp, p, n, &mdb->mhash[i], 1665eb1d1641SHerbert Xu hlist[ver]) { 1666eb1d1641SHerbert Xu del_timer(&mp->timer); 1667eb1d1641SHerbert Xu del_timer(&mp->query_timer); 1668eb1d1641SHerbert Xu call_rcu_bh(&mp->rcu, br_multicast_free_group); 1669eb1d1641SHerbert Xu } 1670eb1d1641SHerbert Xu } 1671eb1d1641SHerbert Xu 1672eb1d1641SHerbert Xu if (mdb->old) { 1673eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 167410cc2b50SHerbert Xu rcu_barrier_bh(); 1675eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1676eb1d1641SHerbert Xu WARN_ON(mdb->old); 1677eb1d1641SHerbert Xu } 1678eb1d1641SHerbert Xu 1679eb1d1641SHerbert Xu mdb->old = mdb; 1680eb1d1641SHerbert Xu call_rcu_bh(&mdb->rcu, br_mdb_free); 1681eb1d1641SHerbert Xu 1682eb1d1641SHerbert Xu out: 1683eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 1684eb1d1641SHerbert Xu } 16850909e117SHerbert Xu 16860909e117SHerbert Xu int br_multicast_set_router(struct net_bridge *br, unsigned long val) 16870909e117SHerbert Xu { 16880909e117SHerbert Xu int err = -ENOENT; 16890909e117SHerbert Xu 16900909e117SHerbert Xu spin_lock_bh(&br->multicast_lock); 16910909e117SHerbert Xu if (!netif_running(br->dev)) 16920909e117SHerbert Xu goto unlock; 16930909e117SHerbert Xu 16940909e117SHerbert Xu switch (val) { 16950909e117SHerbert Xu case 0: 16960909e117SHerbert Xu case 2: 16970909e117SHerbert Xu del_timer(&br->multicast_router_timer); 16980909e117SHerbert Xu /* fall through */ 16990909e117SHerbert Xu case 1: 17000909e117SHerbert Xu br->multicast_router = val; 17010909e117SHerbert Xu err = 0; 17020909e117SHerbert Xu break; 17030909e117SHerbert Xu 17040909e117SHerbert Xu default: 17050909e117SHerbert Xu err = -EINVAL; 17060909e117SHerbert Xu break; 17070909e117SHerbert Xu } 17080909e117SHerbert Xu 17090909e117SHerbert Xu unlock: 17100909e117SHerbert Xu spin_unlock_bh(&br->multicast_lock); 17110909e117SHerbert Xu 17120909e117SHerbert Xu return err; 17130909e117SHerbert Xu } 17140909e117SHerbert Xu 17150909e117SHerbert Xu int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val) 17160909e117SHerbert Xu { 17170909e117SHerbert Xu struct net_bridge *br = p->br; 17180909e117SHerbert Xu int err = -ENOENT; 17190909e117SHerbert Xu 17200909e117SHerbert Xu spin_lock(&br->multicast_lock); 17210909e117SHerbert Xu if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED) 17220909e117SHerbert Xu goto unlock; 17230909e117SHerbert Xu 17240909e117SHerbert Xu switch (val) { 17250909e117SHerbert Xu case 0: 17260909e117SHerbert Xu case 1: 17270909e117SHerbert Xu case 2: 17280909e117SHerbert Xu p->multicast_router = val; 17290909e117SHerbert Xu err = 0; 17300909e117SHerbert Xu 17310909e117SHerbert Xu if (val < 2 && !hlist_unhashed(&p->rlist)) 17320909e117SHerbert Xu hlist_del_init_rcu(&p->rlist); 17330909e117SHerbert Xu 17340909e117SHerbert Xu if (val == 1) 17350909e117SHerbert Xu break; 17360909e117SHerbert Xu 17370909e117SHerbert Xu del_timer(&p->multicast_router_timer); 17380909e117SHerbert Xu 17390909e117SHerbert Xu if (val == 0) 17400909e117SHerbert Xu break; 17410909e117SHerbert Xu 17420909e117SHerbert Xu br_multicast_add_router(br, p); 17430909e117SHerbert Xu break; 17440909e117SHerbert Xu 17450909e117SHerbert Xu default: 17460909e117SHerbert Xu err = -EINVAL; 17470909e117SHerbert Xu break; 17480909e117SHerbert Xu } 17490909e117SHerbert Xu 17500909e117SHerbert Xu unlock: 17510909e117SHerbert Xu spin_unlock(&br->multicast_lock); 17520909e117SHerbert Xu 17530909e117SHerbert Xu return err; 17540909e117SHerbert Xu } 1755561f1103SHerbert Xu 1756561f1103SHerbert Xu int br_multicast_toggle(struct net_bridge *br, unsigned long val) 1757561f1103SHerbert Xu { 1758561f1103SHerbert Xu struct net_bridge_port *port; 17593a7fda06SHerbert Xu int err = 0; 1760e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 1761561f1103SHerbert Xu 1762561f1103SHerbert Xu spin_lock(&br->multicast_lock); 1763561f1103SHerbert Xu if (br->multicast_disabled == !val) 1764561f1103SHerbert Xu goto unlock; 1765561f1103SHerbert Xu 1766561f1103SHerbert Xu br->multicast_disabled = !val; 1767561f1103SHerbert Xu if (br->multicast_disabled) 1768561f1103SHerbert Xu goto unlock; 1769561f1103SHerbert Xu 17703a7fda06SHerbert Xu if (!netif_running(br->dev)) 17713a7fda06SHerbert Xu goto unlock; 17723a7fda06SHerbert Xu 1773e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1774e8051688SEric Dumazet if (mdb) { 1775e8051688SEric Dumazet if (mdb->old) { 1776561f1103SHerbert Xu err = -EEXIST; 1777561f1103SHerbert Xu rollback: 1778561f1103SHerbert Xu br->multicast_disabled = !!val; 1779561f1103SHerbert Xu goto unlock; 1780561f1103SHerbert Xu } 1781561f1103SHerbert Xu 1782e8051688SEric Dumazet err = br_mdb_rehash(&br->mdb, mdb->max, 1783561f1103SHerbert Xu br->hash_elasticity); 1784561f1103SHerbert Xu if (err) 1785561f1103SHerbert Xu goto rollback; 1786561f1103SHerbert Xu } 1787561f1103SHerbert Xu 1788561f1103SHerbert Xu br_multicast_open(br); 1789561f1103SHerbert Xu list_for_each_entry(port, &br->port_list, list) { 1790561f1103SHerbert Xu if (port->state == BR_STATE_DISABLED || 1791561f1103SHerbert Xu port->state == BR_STATE_BLOCKING) 1792561f1103SHerbert Xu continue; 1793561f1103SHerbert Xu 1794561f1103SHerbert Xu __br_multicast_enable_port(port); 1795561f1103SHerbert Xu } 1796561f1103SHerbert Xu 1797561f1103SHerbert Xu unlock: 1798561f1103SHerbert Xu spin_unlock(&br->multicast_lock); 1799561f1103SHerbert Xu 1800561f1103SHerbert Xu return err; 1801561f1103SHerbert Xu } 1802b195167fSHerbert Xu 1803b195167fSHerbert Xu int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val) 1804b195167fSHerbert Xu { 1805b195167fSHerbert Xu int err = -ENOENT; 1806b195167fSHerbert Xu u32 old; 1807e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 1808b195167fSHerbert Xu 1809b195167fSHerbert Xu spin_lock(&br->multicast_lock); 1810b195167fSHerbert Xu if (!netif_running(br->dev)) 1811b195167fSHerbert Xu goto unlock; 1812b195167fSHerbert Xu 1813b195167fSHerbert Xu err = -EINVAL; 1814b195167fSHerbert Xu if (!is_power_of_2(val)) 1815b195167fSHerbert Xu goto unlock; 1816e8051688SEric Dumazet 1817e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1818e8051688SEric Dumazet if (mdb && val < mdb->size) 1819b195167fSHerbert Xu goto unlock; 1820b195167fSHerbert Xu 1821b195167fSHerbert Xu err = 0; 1822b195167fSHerbert Xu 1823b195167fSHerbert Xu old = br->hash_max; 1824b195167fSHerbert Xu br->hash_max = val; 1825b195167fSHerbert Xu 1826e8051688SEric Dumazet if (mdb) { 1827e8051688SEric Dumazet if (mdb->old) { 1828b195167fSHerbert Xu err = -EEXIST; 1829b195167fSHerbert Xu rollback: 1830b195167fSHerbert Xu br->hash_max = old; 1831b195167fSHerbert Xu goto unlock; 1832b195167fSHerbert Xu } 1833b195167fSHerbert Xu 1834b195167fSHerbert Xu err = br_mdb_rehash(&br->mdb, br->hash_max, 1835b195167fSHerbert Xu br->hash_elasticity); 1836b195167fSHerbert Xu if (err) 1837b195167fSHerbert Xu goto rollback; 1838b195167fSHerbert Xu } 1839b195167fSHerbert Xu 1840b195167fSHerbert Xu unlock: 1841b195167fSHerbert Xu spin_unlock(&br->multicast_lock); 1842b195167fSHerbert Xu 1843b195167fSHerbert Xu return err; 1844b195167fSHerbert Xu } 1845