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> 27dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 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 39dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 40e4de9f9eSLinus Lüssing static inline int ipv6_is_transient_multicast(const struct in6_addr *addr) 41eb1d1641SHerbert Xu { 42e4de9f9eSLinus Lüssing if (ipv6_addr_is_multicast(addr) && IPV6_ADDR_MC_FLAG_TRANSIENT(addr)) 4308b202b6SYOSHIFUJI Hideaki return 1; 4408b202b6SYOSHIFUJI Hideaki return 0; 4508b202b6SYOSHIFUJI Hideaki } 4608b202b6SYOSHIFUJI Hideaki #endif 4708b202b6SYOSHIFUJI Hideaki 488ef2a9a5SYOSHIFUJI Hideaki static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b) 498ef2a9a5SYOSHIFUJI Hideaki { 508ef2a9a5SYOSHIFUJI Hideaki if (a->proto != b->proto) 518ef2a9a5SYOSHIFUJI Hideaki return 0; 528ef2a9a5SYOSHIFUJI Hideaki switch (a->proto) { 538ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 548ef2a9a5SYOSHIFUJI Hideaki return a->u.ip4 == b->u.ip4; 55dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 5608b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 5708b202b6SYOSHIFUJI Hideaki return ipv6_addr_equal(&a->u.ip6, &b->u.ip6); 5808b202b6SYOSHIFUJI Hideaki #endif 598ef2a9a5SYOSHIFUJI Hideaki } 608ef2a9a5SYOSHIFUJI Hideaki return 0; 618ef2a9a5SYOSHIFUJI Hideaki } 628ef2a9a5SYOSHIFUJI Hideaki 638ef2a9a5SYOSHIFUJI Hideaki static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip) 64eb1d1641SHerbert Xu { 650eae88f3SEric Dumazet return jhash_1word(mdb->secret, (__force u32)ip) & (mdb->max - 1); 66eb1d1641SHerbert Xu } 67eb1d1641SHerbert Xu 68dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 6908b202b6SYOSHIFUJI Hideaki static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb, 7008b202b6SYOSHIFUJI Hideaki const struct in6_addr *ip) 7108b202b6SYOSHIFUJI Hideaki { 7208b202b6SYOSHIFUJI Hideaki return jhash2((__force u32 *)ip->s6_addr32, 4, mdb->secret) & (mdb->max - 1); 7308b202b6SYOSHIFUJI Hideaki } 7408b202b6SYOSHIFUJI Hideaki #endif 7508b202b6SYOSHIFUJI Hideaki 768ef2a9a5SYOSHIFUJI Hideaki static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb, 778ef2a9a5SYOSHIFUJI Hideaki struct br_ip *ip) 788ef2a9a5SYOSHIFUJI Hideaki { 798ef2a9a5SYOSHIFUJI Hideaki switch (ip->proto) { 808ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 818ef2a9a5SYOSHIFUJI Hideaki return __br_ip4_hash(mdb, ip->u.ip4); 82dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 8308b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 8408b202b6SYOSHIFUJI Hideaki return __br_ip6_hash(mdb, &ip->u.ip6); 8508b202b6SYOSHIFUJI Hideaki #endif 868ef2a9a5SYOSHIFUJI Hideaki } 878ef2a9a5SYOSHIFUJI Hideaki return 0; 88eb1d1641SHerbert Xu } 89eb1d1641SHerbert Xu 90eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *__br_mdb_ip_get( 918ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash) 92eb1d1641SHerbert Xu { 93eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 94eb1d1641SHerbert Xu struct hlist_node *p; 95eb1d1641SHerbert Xu 9649f5fcfdSHerbert Xu hlist_for_each_entry_rcu(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) { 978ef2a9a5SYOSHIFUJI Hideaki if (br_ip_equal(&mp->addr, dst)) 98eb1d1641SHerbert Xu return mp; 99eb1d1641SHerbert Xu } 100eb1d1641SHerbert Xu 101eb1d1641SHerbert Xu return NULL; 102eb1d1641SHerbert Xu } 103eb1d1641SHerbert Xu 1047f285fa7SHerbert Xu static struct net_bridge_mdb_entry *br_mdb_ip_get( 1057f285fa7SHerbert Xu struct net_bridge_mdb_htable *mdb, struct br_ip *dst) 1067f285fa7SHerbert Xu { 1077f285fa7SHerbert Xu if (!mdb) 1087f285fa7SHerbert Xu return NULL; 1097f285fa7SHerbert Xu 1107f285fa7SHerbert Xu return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst)); 1117f285fa7SHerbert Xu } 1127f285fa7SHerbert Xu 1138ef2a9a5SYOSHIFUJI Hideaki static struct net_bridge_mdb_entry *br_mdb_ip4_get( 114eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb, __be32 dst) 115eb1d1641SHerbert Xu { 1168ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_dst; 1170821ec55SHerbert Xu 1188ef2a9a5SYOSHIFUJI Hideaki br_dst.u.ip4 = dst; 1198ef2a9a5SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IP); 1208ef2a9a5SYOSHIFUJI Hideaki 1217f285fa7SHerbert Xu return br_mdb_ip_get(mdb, &br_dst); 1228ef2a9a5SYOSHIFUJI Hideaki } 1238ef2a9a5SYOSHIFUJI Hideaki 124dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 12508b202b6SYOSHIFUJI Hideaki static struct net_bridge_mdb_entry *br_mdb_ip6_get( 12608b202b6SYOSHIFUJI Hideaki struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst) 12708b202b6SYOSHIFUJI Hideaki { 12808b202b6SYOSHIFUJI Hideaki struct br_ip br_dst; 12908b202b6SYOSHIFUJI Hideaki 1304e3fd7a0SAlexey Dobriyan br_dst.u.ip6 = *dst; 13108b202b6SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IPV6); 13208b202b6SYOSHIFUJI Hideaki 1337f285fa7SHerbert Xu return br_mdb_ip_get(mdb, &br_dst); 13408b202b6SYOSHIFUJI Hideaki } 13508b202b6SYOSHIFUJI Hideaki #endif 13608b202b6SYOSHIFUJI Hideaki 137eb1d1641SHerbert Xu struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br, 138eb1d1641SHerbert Xu struct sk_buff *skb) 139eb1d1641SHerbert Xu { 140e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb); 1418ef2a9a5SYOSHIFUJI Hideaki struct br_ip ip; 1428ef2a9a5SYOSHIFUJI Hideaki 1437f285fa7SHerbert Xu if (br->multicast_disabled) 144eb1d1641SHerbert Xu return NULL; 145eb1d1641SHerbert Xu 1468ef2a9a5SYOSHIFUJI Hideaki if (BR_INPUT_SKB_CB(skb)->igmp) 1478ef2a9a5SYOSHIFUJI Hideaki return NULL; 1488ef2a9a5SYOSHIFUJI Hideaki 1498ef2a9a5SYOSHIFUJI Hideaki ip.proto = skb->protocol; 1508ef2a9a5SYOSHIFUJI Hideaki 151eb1d1641SHerbert Xu switch (skb->protocol) { 152eb1d1641SHerbert Xu case htons(ETH_P_IP): 1538ef2a9a5SYOSHIFUJI Hideaki ip.u.ip4 = ip_hdr(skb)->daddr; 154eb1d1641SHerbert Xu break; 155dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 15608b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 1574e3fd7a0SAlexey Dobriyan ip.u.ip6 = ipv6_hdr(skb)->daddr; 15808b202b6SYOSHIFUJI Hideaki break; 15908b202b6SYOSHIFUJI Hideaki #endif 1608ef2a9a5SYOSHIFUJI Hideaki default: 1618ef2a9a5SYOSHIFUJI Hideaki return NULL; 162eb1d1641SHerbert Xu } 163eb1d1641SHerbert Xu 1648ef2a9a5SYOSHIFUJI Hideaki return br_mdb_ip_get(mdb, &ip); 165eb1d1641SHerbert Xu } 166eb1d1641SHerbert Xu 167eb1d1641SHerbert Xu static void br_mdb_free(struct rcu_head *head) 168eb1d1641SHerbert Xu { 169eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb = 170eb1d1641SHerbert Xu container_of(head, struct net_bridge_mdb_htable, rcu); 171eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old = mdb->old; 172eb1d1641SHerbert Xu 173eb1d1641SHerbert Xu mdb->old = NULL; 174eb1d1641SHerbert Xu kfree(old->mhash); 175eb1d1641SHerbert Xu kfree(old); 176eb1d1641SHerbert Xu } 177eb1d1641SHerbert Xu 178eb1d1641SHerbert Xu static int br_mdb_copy(struct net_bridge_mdb_htable *new, 179eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old, 180eb1d1641SHerbert Xu int elasticity) 181eb1d1641SHerbert Xu { 182eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 183eb1d1641SHerbert Xu struct hlist_node *p; 184eb1d1641SHerbert Xu int maxlen; 185eb1d1641SHerbert Xu int len; 186eb1d1641SHerbert Xu int i; 187eb1d1641SHerbert Xu 188eb1d1641SHerbert Xu for (i = 0; i < old->max; i++) 189eb1d1641SHerbert Xu hlist_for_each_entry(mp, p, &old->mhash[i], hlist[old->ver]) 190eb1d1641SHerbert Xu hlist_add_head(&mp->hlist[new->ver], 1918ef2a9a5SYOSHIFUJI Hideaki &new->mhash[br_ip_hash(new, &mp->addr)]); 192eb1d1641SHerbert Xu 193eb1d1641SHerbert Xu if (!elasticity) 194eb1d1641SHerbert Xu return 0; 195eb1d1641SHerbert Xu 196eb1d1641SHerbert Xu maxlen = 0; 197eb1d1641SHerbert Xu for (i = 0; i < new->max; i++) { 198eb1d1641SHerbert Xu len = 0; 199eb1d1641SHerbert Xu hlist_for_each_entry(mp, p, &new->mhash[i], hlist[new->ver]) 200eb1d1641SHerbert Xu len++; 201eb1d1641SHerbert Xu if (len > maxlen) 202eb1d1641SHerbert Xu maxlen = len; 203eb1d1641SHerbert Xu } 204eb1d1641SHerbert Xu 205eb1d1641SHerbert Xu return maxlen > elasticity ? -EINVAL : 0; 206eb1d1641SHerbert Xu } 207eb1d1641SHerbert Xu 208eb1d1641SHerbert Xu static void br_multicast_free_pg(struct rcu_head *head) 209eb1d1641SHerbert Xu { 210eb1d1641SHerbert Xu struct net_bridge_port_group *p = 211eb1d1641SHerbert Xu container_of(head, struct net_bridge_port_group, rcu); 212eb1d1641SHerbert Xu 213eb1d1641SHerbert Xu kfree(p); 214eb1d1641SHerbert Xu } 215eb1d1641SHerbert Xu 216eb1d1641SHerbert Xu static void br_multicast_free_group(struct rcu_head *head) 217eb1d1641SHerbert Xu { 218eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = 219eb1d1641SHerbert Xu container_of(head, struct net_bridge_mdb_entry, rcu); 220eb1d1641SHerbert Xu 221eb1d1641SHerbert Xu kfree(mp); 222eb1d1641SHerbert Xu } 223eb1d1641SHerbert Xu 224eb1d1641SHerbert Xu static void br_multicast_group_expired(unsigned long data) 225eb1d1641SHerbert Xu { 226eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = (void *)data; 227eb1d1641SHerbert Xu struct net_bridge *br = mp->br; 228eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 229eb1d1641SHerbert Xu 230eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 231eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&mp->timer)) 232eb1d1641SHerbert Xu goto out; 233eb1d1641SHerbert Xu 2348a870178SHerbert Xu mp->mglist = false; 235eb1d1641SHerbert Xu 236eb1d1641SHerbert Xu if (mp->ports) 237eb1d1641SHerbert Xu goto out; 238eb1d1641SHerbert Xu 239e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 240e8051688SEric Dumazet 241eb1d1641SHerbert Xu hlist_del_rcu(&mp->hlist[mdb->ver]); 242eb1d1641SHerbert Xu mdb->size--; 243eb1d1641SHerbert Xu 244eb1d1641SHerbert Xu del_timer(&mp->query_timer); 245eb1d1641SHerbert Xu call_rcu_bh(&mp->rcu, br_multicast_free_group); 246eb1d1641SHerbert Xu 247eb1d1641SHerbert Xu out: 248eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 249eb1d1641SHerbert Xu } 250eb1d1641SHerbert Xu 251eb1d1641SHerbert Xu static void br_multicast_del_pg(struct net_bridge *br, 252eb1d1641SHerbert Xu struct net_bridge_port_group *pg) 253eb1d1641SHerbert Xu { 254e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 255eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 256eb1d1641SHerbert Xu struct net_bridge_port_group *p; 257e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 258e8051688SEric Dumazet 259e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 260eb1d1641SHerbert Xu 2618ef2a9a5SYOSHIFUJI Hideaki mp = br_mdb_ip_get(mdb, &pg->addr); 262eb1d1641SHerbert Xu if (WARN_ON(!mp)) 263eb1d1641SHerbert Xu return; 264eb1d1641SHerbert Xu 265e8051688SEric Dumazet for (pp = &mp->ports; 266e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 267e8051688SEric Dumazet pp = &p->next) { 268eb1d1641SHerbert Xu if (p != pg) 269eb1d1641SHerbert Xu continue; 270eb1d1641SHerbert Xu 27183f6a740Sstephen hemminger rcu_assign_pointer(*pp, p->next); 272eb1d1641SHerbert Xu hlist_del_init(&p->mglist); 273eb1d1641SHerbert Xu del_timer(&p->timer); 274eb1d1641SHerbert Xu del_timer(&p->query_timer); 275eb1d1641SHerbert Xu call_rcu_bh(&p->rcu, br_multicast_free_pg); 276eb1d1641SHerbert Xu 2778a870178SHerbert Xu if (!mp->ports && !mp->mglist && 278eb1d1641SHerbert Xu netif_running(br->dev)) 279eb1d1641SHerbert Xu mod_timer(&mp->timer, jiffies); 280eb1d1641SHerbert Xu 281eb1d1641SHerbert Xu return; 282eb1d1641SHerbert Xu } 283eb1d1641SHerbert Xu 284eb1d1641SHerbert Xu WARN_ON(1); 285eb1d1641SHerbert Xu } 286eb1d1641SHerbert Xu 287eb1d1641SHerbert Xu static void br_multicast_port_group_expired(unsigned long data) 288eb1d1641SHerbert Xu { 289eb1d1641SHerbert Xu struct net_bridge_port_group *pg = (void *)data; 290eb1d1641SHerbert Xu struct net_bridge *br = pg->port->br; 291eb1d1641SHerbert Xu 292eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 293eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&pg->timer) || 294eb1d1641SHerbert Xu hlist_unhashed(&pg->mglist)) 295eb1d1641SHerbert Xu goto out; 296eb1d1641SHerbert Xu 297eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 298eb1d1641SHerbert Xu 299eb1d1641SHerbert Xu out: 300eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 301eb1d1641SHerbert Xu } 302eb1d1641SHerbert Xu 303e8051688SEric Dumazet static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max, 304eb1d1641SHerbert Xu int elasticity) 305eb1d1641SHerbert Xu { 306e8051688SEric Dumazet struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1); 307eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 308eb1d1641SHerbert Xu int err; 309eb1d1641SHerbert Xu 310eb1d1641SHerbert Xu mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC); 311eb1d1641SHerbert Xu if (!mdb) 312eb1d1641SHerbert Xu return -ENOMEM; 313eb1d1641SHerbert Xu 314eb1d1641SHerbert Xu mdb->max = max; 315eb1d1641SHerbert Xu mdb->old = old; 316eb1d1641SHerbert Xu 317eb1d1641SHerbert Xu mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC); 318eb1d1641SHerbert Xu if (!mdb->mhash) { 319eb1d1641SHerbert Xu kfree(mdb); 320eb1d1641SHerbert Xu return -ENOMEM; 321eb1d1641SHerbert Xu } 322eb1d1641SHerbert Xu 323eb1d1641SHerbert Xu mdb->size = old ? old->size : 0; 324eb1d1641SHerbert Xu mdb->ver = old ? old->ver ^ 1 : 0; 325eb1d1641SHerbert Xu 326eb1d1641SHerbert Xu if (!old || elasticity) 327eb1d1641SHerbert Xu get_random_bytes(&mdb->secret, sizeof(mdb->secret)); 328eb1d1641SHerbert Xu else 329eb1d1641SHerbert Xu mdb->secret = old->secret; 330eb1d1641SHerbert Xu 331eb1d1641SHerbert Xu if (!old) 332eb1d1641SHerbert Xu goto out; 333eb1d1641SHerbert Xu 334eb1d1641SHerbert Xu err = br_mdb_copy(mdb, old, elasticity); 335eb1d1641SHerbert Xu if (err) { 336eb1d1641SHerbert Xu kfree(mdb->mhash); 337eb1d1641SHerbert Xu kfree(mdb); 338eb1d1641SHerbert Xu return err; 339eb1d1641SHerbert Xu } 340eb1d1641SHerbert Xu 341eb1d1641SHerbert Xu call_rcu_bh(&mdb->rcu, br_mdb_free); 342eb1d1641SHerbert Xu 343eb1d1641SHerbert Xu out: 344eb1d1641SHerbert Xu rcu_assign_pointer(*mdbp, mdb); 345eb1d1641SHerbert Xu 346eb1d1641SHerbert Xu return 0; 347eb1d1641SHerbert Xu } 348eb1d1641SHerbert Xu 3498ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br, 350eb1d1641SHerbert Xu __be32 group) 351eb1d1641SHerbert Xu { 352eb1d1641SHerbert Xu struct sk_buff *skb; 353eb1d1641SHerbert Xu struct igmphdr *ih; 354eb1d1641SHerbert Xu struct ethhdr *eth; 355eb1d1641SHerbert Xu struct iphdr *iph; 356eb1d1641SHerbert Xu 357eb1d1641SHerbert Xu skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) + 358eb1d1641SHerbert Xu sizeof(*ih) + 4); 359eb1d1641SHerbert Xu if (!skb) 360eb1d1641SHerbert Xu goto out; 361eb1d1641SHerbert Xu 362eb1d1641SHerbert Xu skb->protocol = htons(ETH_P_IP); 363eb1d1641SHerbert Xu 364eb1d1641SHerbert Xu skb_reset_mac_header(skb); 365eb1d1641SHerbert Xu eth = eth_hdr(skb); 366eb1d1641SHerbert Xu 367eb1d1641SHerbert Xu memcpy(eth->h_source, br->dev->dev_addr, 6); 368eb1d1641SHerbert Xu eth->h_dest[0] = 1; 369eb1d1641SHerbert Xu eth->h_dest[1] = 0; 370eb1d1641SHerbert Xu eth->h_dest[2] = 0x5e; 371eb1d1641SHerbert Xu eth->h_dest[3] = 0; 372eb1d1641SHerbert Xu eth->h_dest[4] = 0; 373eb1d1641SHerbert Xu eth->h_dest[5] = 1; 374eb1d1641SHerbert Xu eth->h_proto = htons(ETH_P_IP); 375eb1d1641SHerbert Xu skb_put(skb, sizeof(*eth)); 376eb1d1641SHerbert Xu 377eb1d1641SHerbert Xu skb_set_network_header(skb, skb->len); 378eb1d1641SHerbert Xu iph = ip_hdr(skb); 379eb1d1641SHerbert Xu 380eb1d1641SHerbert Xu iph->version = 4; 381eb1d1641SHerbert Xu iph->ihl = 6; 382eb1d1641SHerbert Xu iph->tos = 0xc0; 383eb1d1641SHerbert Xu iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4); 384eb1d1641SHerbert Xu iph->id = 0; 385eb1d1641SHerbert Xu iph->frag_off = htons(IP_DF); 386eb1d1641SHerbert Xu iph->ttl = 1; 387eb1d1641SHerbert Xu iph->protocol = IPPROTO_IGMP; 388eb1d1641SHerbert Xu iph->saddr = 0; 389eb1d1641SHerbert Xu iph->daddr = htonl(INADDR_ALLHOSTS_GROUP); 390eb1d1641SHerbert Xu ((u8 *)&iph[1])[0] = IPOPT_RA; 391eb1d1641SHerbert Xu ((u8 *)&iph[1])[1] = 4; 392eb1d1641SHerbert Xu ((u8 *)&iph[1])[2] = 0; 393eb1d1641SHerbert Xu ((u8 *)&iph[1])[3] = 0; 394eb1d1641SHerbert Xu ip_send_check(iph); 395eb1d1641SHerbert Xu skb_put(skb, 24); 396eb1d1641SHerbert Xu 397eb1d1641SHerbert Xu skb_set_transport_header(skb, skb->len); 398eb1d1641SHerbert Xu ih = igmp_hdr(skb); 399eb1d1641SHerbert Xu ih->type = IGMP_HOST_MEMBERSHIP_QUERY; 400eb1d1641SHerbert Xu ih->code = (group ? br->multicast_last_member_interval : 401eb1d1641SHerbert Xu br->multicast_query_response_interval) / 402eb1d1641SHerbert Xu (HZ / IGMP_TIMER_SCALE); 403eb1d1641SHerbert Xu ih->group = group; 404eb1d1641SHerbert Xu ih->csum = 0; 405eb1d1641SHerbert Xu ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr)); 406eb1d1641SHerbert Xu skb_put(skb, sizeof(*ih)); 407eb1d1641SHerbert Xu 408eb1d1641SHerbert Xu __skb_pull(skb, sizeof(*eth)); 409eb1d1641SHerbert Xu 410eb1d1641SHerbert Xu out: 411eb1d1641SHerbert Xu return skb; 412eb1d1641SHerbert Xu } 413eb1d1641SHerbert Xu 414dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 41508b202b6SYOSHIFUJI Hideaki static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br, 416b71d1d42SEric Dumazet const struct in6_addr *group) 41708b202b6SYOSHIFUJI Hideaki { 41808b202b6SYOSHIFUJI Hideaki struct sk_buff *skb; 41908b202b6SYOSHIFUJI Hideaki struct ipv6hdr *ip6h; 42008b202b6SYOSHIFUJI Hideaki struct mld_msg *mldq; 42108b202b6SYOSHIFUJI Hideaki struct ethhdr *eth; 42208b202b6SYOSHIFUJI Hideaki u8 *hopopt; 42308b202b6SYOSHIFUJI Hideaki unsigned long interval; 42408b202b6SYOSHIFUJI Hideaki 42508b202b6SYOSHIFUJI Hideaki skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) + 42608b202b6SYOSHIFUJI Hideaki 8 + sizeof(*mldq)); 42708b202b6SYOSHIFUJI Hideaki if (!skb) 42808b202b6SYOSHIFUJI Hideaki goto out; 42908b202b6SYOSHIFUJI Hideaki 43008b202b6SYOSHIFUJI Hideaki skb->protocol = htons(ETH_P_IPV6); 43108b202b6SYOSHIFUJI Hideaki 43208b202b6SYOSHIFUJI Hideaki /* Ethernet header */ 43308b202b6SYOSHIFUJI Hideaki skb_reset_mac_header(skb); 43408b202b6SYOSHIFUJI Hideaki eth = eth_hdr(skb); 43508b202b6SYOSHIFUJI Hideaki 43608b202b6SYOSHIFUJI Hideaki memcpy(eth->h_source, br->dev->dev_addr, 6); 43708b202b6SYOSHIFUJI Hideaki eth->h_proto = htons(ETH_P_IPV6); 43808b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*eth)); 43908b202b6SYOSHIFUJI Hideaki 44008b202b6SYOSHIFUJI Hideaki /* IPv6 header + HbH option */ 44108b202b6SYOSHIFUJI Hideaki skb_set_network_header(skb, skb->len); 44208b202b6SYOSHIFUJI Hideaki ip6h = ipv6_hdr(skb); 44308b202b6SYOSHIFUJI Hideaki 44408b202b6SYOSHIFUJI Hideaki *(__force __be32 *)ip6h = htonl(0x60000000); 44576d66158SDavid Stevens ip6h->payload_len = htons(8 + sizeof(*mldq)); 44608b202b6SYOSHIFUJI Hideaki ip6h->nexthdr = IPPROTO_HOPOPTS; 44708b202b6SYOSHIFUJI Hideaki ip6h->hop_limit = 1; 448a7bff75bSLinus Lüssing ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1)); 449*d1d81d4cSUlrich Weber if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0, 450*d1d81d4cSUlrich Weber &ip6h->saddr)) { 451*d1d81d4cSUlrich Weber kfree_skb(skb); 452*d1d81d4cSUlrich Weber return NULL; 453*d1d81d4cSUlrich Weber } 45436cff5a1SLinus Lüssing ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest); 45508b202b6SYOSHIFUJI Hideaki 45608b202b6SYOSHIFUJI Hideaki hopopt = (u8 *)(ip6h + 1); 45708b202b6SYOSHIFUJI Hideaki hopopt[0] = IPPROTO_ICMPV6; /* next hdr */ 45808b202b6SYOSHIFUJI Hideaki hopopt[1] = 0; /* length of HbH */ 45908b202b6SYOSHIFUJI Hideaki hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */ 46008b202b6SYOSHIFUJI Hideaki hopopt[3] = 2; /* Length of RA Option */ 46108b202b6SYOSHIFUJI Hideaki hopopt[4] = 0; /* Type = 0x0000 (MLD) */ 46208b202b6SYOSHIFUJI Hideaki hopopt[5] = 0; 46308b202b6SYOSHIFUJI Hideaki hopopt[6] = IPV6_TLV_PAD0; /* Pad0 */ 46408b202b6SYOSHIFUJI Hideaki hopopt[7] = IPV6_TLV_PAD0; /* Pad0 */ 46508b202b6SYOSHIFUJI Hideaki 46608b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*ip6h) + 8); 46708b202b6SYOSHIFUJI Hideaki 46808b202b6SYOSHIFUJI Hideaki /* ICMPv6 */ 46908b202b6SYOSHIFUJI Hideaki skb_set_transport_header(skb, skb->len); 47008b202b6SYOSHIFUJI Hideaki mldq = (struct mld_msg *) icmp6_hdr(skb); 47108b202b6SYOSHIFUJI Hideaki 47208b202b6SYOSHIFUJI Hideaki interval = ipv6_addr_any(group) ? br->multicast_last_member_interval : 47308b202b6SYOSHIFUJI Hideaki br->multicast_query_response_interval; 47408b202b6SYOSHIFUJI Hideaki 47508b202b6SYOSHIFUJI Hideaki mldq->mld_type = ICMPV6_MGM_QUERY; 47608b202b6SYOSHIFUJI Hideaki mldq->mld_code = 0; 47708b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = 0; 47808b202b6SYOSHIFUJI Hideaki mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval)); 47908b202b6SYOSHIFUJI Hideaki mldq->mld_reserved = 0; 4804e3fd7a0SAlexey Dobriyan mldq->mld_mca = *group; 48108b202b6SYOSHIFUJI Hideaki 48208b202b6SYOSHIFUJI Hideaki /* checksum */ 48308b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, 48408b202b6SYOSHIFUJI Hideaki sizeof(*mldq), IPPROTO_ICMPV6, 48508b202b6SYOSHIFUJI Hideaki csum_partial(mldq, 48608b202b6SYOSHIFUJI Hideaki sizeof(*mldq), 0)); 48708b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*mldq)); 48808b202b6SYOSHIFUJI Hideaki 48908b202b6SYOSHIFUJI Hideaki __skb_pull(skb, sizeof(*eth)); 49008b202b6SYOSHIFUJI Hideaki 49108b202b6SYOSHIFUJI Hideaki out: 49208b202b6SYOSHIFUJI Hideaki return skb; 49308b202b6SYOSHIFUJI Hideaki } 49408b202b6SYOSHIFUJI Hideaki #endif 49508b202b6SYOSHIFUJI Hideaki 4968ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br, 4978ef2a9a5SYOSHIFUJI Hideaki struct br_ip *addr) 4988ef2a9a5SYOSHIFUJI Hideaki { 4998ef2a9a5SYOSHIFUJI Hideaki switch (addr->proto) { 5008ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 5018ef2a9a5SYOSHIFUJI Hideaki return br_ip4_multicast_alloc_query(br, addr->u.ip4); 502dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 50308b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 50408b202b6SYOSHIFUJI Hideaki return br_ip6_multicast_alloc_query(br, &addr->u.ip6); 50508b202b6SYOSHIFUJI Hideaki #endif 5068ef2a9a5SYOSHIFUJI Hideaki } 5078ef2a9a5SYOSHIFUJI Hideaki return NULL; 5088ef2a9a5SYOSHIFUJI Hideaki } 5098ef2a9a5SYOSHIFUJI Hideaki 510eb1d1641SHerbert Xu static void br_multicast_send_group_query(struct net_bridge_mdb_entry *mp) 511eb1d1641SHerbert Xu { 512eb1d1641SHerbert Xu struct net_bridge *br = mp->br; 513eb1d1641SHerbert Xu struct sk_buff *skb; 514eb1d1641SHerbert Xu 5158ef2a9a5SYOSHIFUJI Hideaki skb = br_multicast_alloc_query(br, &mp->addr); 516eb1d1641SHerbert Xu if (!skb) 517eb1d1641SHerbert Xu goto timer; 518eb1d1641SHerbert Xu 519eb1d1641SHerbert Xu netif_rx(skb); 520eb1d1641SHerbert Xu 521eb1d1641SHerbert Xu timer: 522eb1d1641SHerbert Xu if (++mp->queries_sent < br->multicast_last_member_count) 523eb1d1641SHerbert Xu mod_timer(&mp->query_timer, 524eb1d1641SHerbert Xu jiffies + br->multicast_last_member_interval); 525eb1d1641SHerbert Xu } 526eb1d1641SHerbert Xu 527eb1d1641SHerbert Xu static void br_multicast_group_query_expired(unsigned long data) 528eb1d1641SHerbert Xu { 529eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = (void *)data; 530eb1d1641SHerbert Xu struct net_bridge *br = mp->br; 531eb1d1641SHerbert Xu 532eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 5338a870178SHerbert Xu if (!netif_running(br->dev) || !mp->mglist || 534eb1d1641SHerbert Xu mp->queries_sent >= br->multicast_last_member_count) 535eb1d1641SHerbert Xu goto out; 536eb1d1641SHerbert Xu 537eb1d1641SHerbert Xu br_multicast_send_group_query(mp); 538eb1d1641SHerbert Xu 539eb1d1641SHerbert Xu out: 540eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 541eb1d1641SHerbert Xu } 542eb1d1641SHerbert Xu 543eb1d1641SHerbert Xu static void br_multicast_send_port_group_query(struct net_bridge_port_group *pg) 544eb1d1641SHerbert Xu { 545eb1d1641SHerbert Xu struct net_bridge_port *port = pg->port; 546eb1d1641SHerbert Xu struct net_bridge *br = port->br; 547eb1d1641SHerbert Xu struct sk_buff *skb; 548eb1d1641SHerbert Xu 5498ef2a9a5SYOSHIFUJI Hideaki skb = br_multicast_alloc_query(br, &pg->addr); 550eb1d1641SHerbert Xu if (!skb) 551eb1d1641SHerbert Xu goto timer; 552eb1d1641SHerbert Xu 553eb1d1641SHerbert Xu br_deliver(port, skb); 554eb1d1641SHerbert Xu 555eb1d1641SHerbert Xu timer: 556eb1d1641SHerbert Xu if (++pg->queries_sent < br->multicast_last_member_count) 557eb1d1641SHerbert Xu mod_timer(&pg->query_timer, 558eb1d1641SHerbert Xu jiffies + br->multicast_last_member_interval); 559eb1d1641SHerbert Xu } 560eb1d1641SHerbert Xu 561eb1d1641SHerbert Xu static void br_multicast_port_group_query_expired(unsigned long data) 562eb1d1641SHerbert Xu { 563eb1d1641SHerbert Xu struct net_bridge_port_group *pg = (void *)data; 564eb1d1641SHerbert Xu struct net_bridge_port *port = pg->port; 565eb1d1641SHerbert Xu struct net_bridge *br = port->br; 566eb1d1641SHerbert Xu 567eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 568eb1d1641SHerbert Xu if (!netif_running(br->dev) || hlist_unhashed(&pg->mglist) || 569eb1d1641SHerbert Xu pg->queries_sent >= br->multicast_last_member_count) 570eb1d1641SHerbert Xu goto out; 571eb1d1641SHerbert Xu 572eb1d1641SHerbert Xu br_multicast_send_port_group_query(pg); 573eb1d1641SHerbert Xu 574eb1d1641SHerbert Xu out: 575eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 576eb1d1641SHerbert Xu } 577eb1d1641SHerbert Xu 578eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *br_multicast_get_group( 5798ef2a9a5SYOSHIFUJI Hideaki struct net_bridge *br, struct net_bridge_port *port, 5808ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group, int hash) 581eb1d1641SHerbert Xu { 582e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 583eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 584eb1d1641SHerbert Xu struct hlist_node *p; 585eb1d1641SHerbert Xu unsigned count = 0; 586eb1d1641SHerbert Xu unsigned max; 587eb1d1641SHerbert Xu int elasticity; 588eb1d1641SHerbert Xu int err; 589eb1d1641SHerbert Xu 590e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 591eb1d1641SHerbert Xu hlist_for_each_entry(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) { 592eb1d1641SHerbert Xu count++; 5938ef2a9a5SYOSHIFUJI Hideaki if (unlikely(br_ip_equal(group, &mp->addr))) 594eb1d1641SHerbert Xu return mp; 595eb1d1641SHerbert Xu } 596eb1d1641SHerbert Xu 597eb1d1641SHerbert Xu elasticity = 0; 598eb1d1641SHerbert Xu max = mdb->max; 599eb1d1641SHerbert Xu 600eb1d1641SHerbert Xu if (unlikely(count > br->hash_elasticity && count)) { 601eb1d1641SHerbert Xu if (net_ratelimit()) 60228a16c97Sstephen hemminger br_info(br, "Multicast hash table " 603eb1d1641SHerbert Xu "chain limit reached: %s\n", 60428a16c97Sstephen hemminger port ? port->dev->name : br->dev->name); 605eb1d1641SHerbert Xu 606eb1d1641SHerbert Xu elasticity = br->hash_elasticity; 607eb1d1641SHerbert Xu } 608eb1d1641SHerbert Xu 609eb1d1641SHerbert Xu if (mdb->size >= max) { 610eb1d1641SHerbert Xu max *= 2; 611eb1d1641SHerbert Xu if (unlikely(max >= br->hash_max)) { 61228a16c97Sstephen hemminger br_warn(br, "Multicast hash table maximum " 613eb1d1641SHerbert Xu "reached, disabling snooping: %s, %d\n", 61428a16c97Sstephen hemminger port ? port->dev->name : br->dev->name, max); 615eb1d1641SHerbert Xu err = -E2BIG; 616eb1d1641SHerbert Xu disable: 617eb1d1641SHerbert Xu br->multicast_disabled = 1; 618eb1d1641SHerbert Xu goto err; 619eb1d1641SHerbert Xu } 620eb1d1641SHerbert Xu } 621eb1d1641SHerbert Xu 622eb1d1641SHerbert Xu if (max > mdb->max || elasticity) { 623eb1d1641SHerbert Xu if (mdb->old) { 624eb1d1641SHerbert Xu if (net_ratelimit()) 62528a16c97Sstephen hemminger br_info(br, "Multicast hash table " 626eb1d1641SHerbert Xu "on fire: %s\n", 62728a16c97Sstephen hemminger port ? port->dev->name : br->dev->name); 628eb1d1641SHerbert Xu err = -EEXIST; 629eb1d1641SHerbert Xu goto err; 630eb1d1641SHerbert Xu } 631eb1d1641SHerbert Xu 632eb1d1641SHerbert Xu err = br_mdb_rehash(&br->mdb, max, elasticity); 633eb1d1641SHerbert Xu if (err) { 63428a16c97Sstephen hemminger br_warn(br, "Cannot rehash multicast " 63528a16c97Sstephen hemminger "hash table, disabling snooping: %s, %d, %d\n", 63628a16c97Sstephen hemminger port ? port->dev->name : br->dev->name, 637eb1d1641SHerbert Xu mdb->size, err); 638eb1d1641SHerbert Xu goto disable; 639eb1d1641SHerbert Xu } 640eb1d1641SHerbert Xu 641eb1d1641SHerbert Xu err = -EAGAIN; 642eb1d1641SHerbert Xu goto err; 643eb1d1641SHerbert Xu } 644eb1d1641SHerbert Xu 645eb1d1641SHerbert Xu return NULL; 646eb1d1641SHerbert Xu 647eb1d1641SHerbert Xu err: 648eb1d1641SHerbert Xu mp = ERR_PTR(err); 649eb1d1641SHerbert Xu return mp; 650eb1d1641SHerbert Xu } 651eb1d1641SHerbert Xu 652eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *br_multicast_new_group( 6538ef2a9a5SYOSHIFUJI Hideaki struct net_bridge *br, struct net_bridge_port *port, 6548ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group) 655eb1d1641SHerbert Xu { 656e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 657eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 658eb1d1641SHerbert Xu int hash; 6594c0833bcSTobias Klauser int err; 660eb1d1641SHerbert Xu 661e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 662eb1d1641SHerbert Xu if (!mdb) { 6634c0833bcSTobias Klauser err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0); 6644c0833bcSTobias Klauser if (err) 6654c0833bcSTobias Klauser return ERR_PTR(err); 666eb1d1641SHerbert Xu goto rehash; 667eb1d1641SHerbert Xu } 668eb1d1641SHerbert Xu 669eb1d1641SHerbert Xu hash = br_ip_hash(mdb, group); 670eb1d1641SHerbert Xu mp = br_multicast_get_group(br, port, group, hash); 671eb1d1641SHerbert Xu switch (PTR_ERR(mp)) { 672eb1d1641SHerbert Xu case 0: 673eb1d1641SHerbert Xu break; 674eb1d1641SHerbert Xu 675eb1d1641SHerbert Xu case -EAGAIN: 676eb1d1641SHerbert Xu rehash: 677e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 678eb1d1641SHerbert Xu hash = br_ip_hash(mdb, group); 679eb1d1641SHerbert Xu break; 680eb1d1641SHerbert Xu 681eb1d1641SHerbert Xu default: 682eb1d1641SHerbert Xu goto out; 683eb1d1641SHerbert Xu } 684eb1d1641SHerbert Xu 685eb1d1641SHerbert Xu mp = kzalloc(sizeof(*mp), GFP_ATOMIC); 686eb1d1641SHerbert Xu if (unlikely(!mp)) 6874c0833bcSTobias Klauser return ERR_PTR(-ENOMEM); 688eb1d1641SHerbert Xu 689eb1d1641SHerbert Xu mp->br = br; 6908ef2a9a5SYOSHIFUJI Hideaki mp->addr = *group; 691eb1d1641SHerbert Xu setup_timer(&mp->timer, br_multicast_group_expired, 692eb1d1641SHerbert Xu (unsigned long)mp); 693eb1d1641SHerbert Xu setup_timer(&mp->query_timer, br_multicast_group_query_expired, 694eb1d1641SHerbert Xu (unsigned long)mp); 695eb1d1641SHerbert Xu 696eb1d1641SHerbert Xu hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]); 697eb1d1641SHerbert Xu mdb->size++; 698eb1d1641SHerbert Xu 699eb1d1641SHerbert Xu out: 700eb1d1641SHerbert Xu return mp; 701eb1d1641SHerbert Xu } 702eb1d1641SHerbert Xu 703eb1d1641SHerbert Xu static int br_multicast_add_group(struct net_bridge *br, 7048ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 7058ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group) 706eb1d1641SHerbert Xu { 707eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 708eb1d1641SHerbert Xu struct net_bridge_port_group *p; 709e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 710eb1d1641SHerbert Xu unsigned long now = jiffies; 711eb1d1641SHerbert Xu int err; 712eb1d1641SHerbert Xu 713eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 714eb1d1641SHerbert Xu if (!netif_running(br->dev) || 715eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 716eb1d1641SHerbert Xu goto out; 717eb1d1641SHerbert Xu 718eb1d1641SHerbert Xu mp = br_multicast_new_group(br, port, group); 719eb1d1641SHerbert Xu err = PTR_ERR(mp); 7204c0833bcSTobias Klauser if (IS_ERR(mp)) 721eb1d1641SHerbert Xu goto err; 722eb1d1641SHerbert Xu 723eb1d1641SHerbert Xu if (!port) { 7248a870178SHerbert Xu mp->mglist = true; 725eb1d1641SHerbert Xu mod_timer(&mp->timer, now + br->multicast_membership_interval); 726eb1d1641SHerbert Xu goto out; 727eb1d1641SHerbert Xu } 728eb1d1641SHerbert Xu 729e8051688SEric Dumazet for (pp = &mp->ports; 730e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 731e8051688SEric Dumazet pp = &p->next) { 732eb1d1641SHerbert Xu if (p->port == port) 733eb1d1641SHerbert Xu goto found; 734eb1d1641SHerbert Xu if ((unsigned long)p->port < (unsigned long)port) 735eb1d1641SHerbert Xu break; 736eb1d1641SHerbert Xu } 737eb1d1641SHerbert Xu 738eb1d1641SHerbert Xu p = kzalloc(sizeof(*p), GFP_ATOMIC); 739eb1d1641SHerbert Xu err = -ENOMEM; 740eb1d1641SHerbert Xu if (unlikely(!p)) 741eb1d1641SHerbert Xu goto err; 742eb1d1641SHerbert Xu 7438ef2a9a5SYOSHIFUJI Hideaki p->addr = *group; 744eb1d1641SHerbert Xu p->port = port; 745eb1d1641SHerbert Xu p->next = *pp; 746eb1d1641SHerbert Xu hlist_add_head(&p->mglist, &port->mglist); 747eb1d1641SHerbert Xu setup_timer(&p->timer, br_multicast_port_group_expired, 748eb1d1641SHerbert Xu (unsigned long)p); 749eb1d1641SHerbert Xu setup_timer(&p->query_timer, br_multicast_port_group_query_expired, 750eb1d1641SHerbert Xu (unsigned long)p); 751eb1d1641SHerbert Xu 752eb1d1641SHerbert Xu rcu_assign_pointer(*pp, p); 753eb1d1641SHerbert Xu 754eb1d1641SHerbert Xu found: 755eb1d1641SHerbert Xu mod_timer(&p->timer, now + br->multicast_membership_interval); 756eb1d1641SHerbert Xu out: 757eb1d1641SHerbert Xu err = 0; 758eb1d1641SHerbert Xu 759eb1d1641SHerbert Xu err: 760eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 761eb1d1641SHerbert Xu return err; 762eb1d1641SHerbert Xu } 763eb1d1641SHerbert Xu 7648ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_add_group(struct net_bridge *br, 7658ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 7668ef2a9a5SYOSHIFUJI Hideaki __be32 group) 7678ef2a9a5SYOSHIFUJI Hideaki { 7688ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 7698ef2a9a5SYOSHIFUJI Hideaki 7708ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 7718ef2a9a5SYOSHIFUJI Hideaki return 0; 7728ef2a9a5SYOSHIFUJI Hideaki 7738ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 7748ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 7758ef2a9a5SYOSHIFUJI Hideaki 7768ef2a9a5SYOSHIFUJI Hideaki return br_multicast_add_group(br, port, &br_group); 7778ef2a9a5SYOSHIFUJI Hideaki } 7788ef2a9a5SYOSHIFUJI Hideaki 779dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 78008b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_add_group(struct net_bridge *br, 78108b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 78208b202b6SYOSHIFUJI Hideaki const struct in6_addr *group) 78308b202b6SYOSHIFUJI Hideaki { 78408b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 78508b202b6SYOSHIFUJI Hideaki 786e4de9f9eSLinus Lüssing if (!ipv6_is_transient_multicast(group)) 78708b202b6SYOSHIFUJI Hideaki return 0; 78808b202b6SYOSHIFUJI Hideaki 7894e3fd7a0SAlexey Dobriyan br_group.u.ip6 = *group; 7909cc6e0c4SLinus Lüssing br_group.proto = htons(ETH_P_IPV6); 79108b202b6SYOSHIFUJI Hideaki 79208b202b6SYOSHIFUJI Hideaki return br_multicast_add_group(br, port, &br_group); 79308b202b6SYOSHIFUJI Hideaki } 79408b202b6SYOSHIFUJI Hideaki #endif 79508b202b6SYOSHIFUJI Hideaki 796eb1d1641SHerbert Xu static void br_multicast_router_expired(unsigned long data) 797eb1d1641SHerbert Xu { 798eb1d1641SHerbert Xu struct net_bridge_port *port = (void *)data; 799eb1d1641SHerbert Xu struct net_bridge *br = port->br; 800eb1d1641SHerbert Xu 801eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 802eb1d1641SHerbert Xu if (port->multicast_router != 1 || 803eb1d1641SHerbert Xu timer_pending(&port->multicast_router_timer) || 804eb1d1641SHerbert Xu hlist_unhashed(&port->rlist)) 805eb1d1641SHerbert Xu goto out; 806eb1d1641SHerbert Xu 807eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 808eb1d1641SHerbert Xu 809eb1d1641SHerbert Xu out: 810eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 811eb1d1641SHerbert Xu } 812eb1d1641SHerbert Xu 813eb1d1641SHerbert Xu static void br_multicast_local_router_expired(unsigned long data) 814eb1d1641SHerbert Xu { 815eb1d1641SHerbert Xu } 816eb1d1641SHerbert Xu 8178ef2a9a5SYOSHIFUJI Hideaki static void __br_multicast_send_query(struct net_bridge *br, 8188ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 8198ef2a9a5SYOSHIFUJI Hideaki struct br_ip *ip) 820eb1d1641SHerbert Xu { 821eb1d1641SHerbert Xu struct sk_buff *skb; 822eb1d1641SHerbert Xu 8238ef2a9a5SYOSHIFUJI Hideaki skb = br_multicast_alloc_query(br, ip); 824eb1d1641SHerbert Xu if (!skb) 8258ef2a9a5SYOSHIFUJI Hideaki return; 826eb1d1641SHerbert Xu 827eb1d1641SHerbert Xu if (port) { 828eb1d1641SHerbert Xu __skb_push(skb, sizeof(struct ethhdr)); 829eb1d1641SHerbert Xu skb->dev = port->dev; 830713aefa3SJan Engelhardt NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev, 831eb1d1641SHerbert Xu dev_queue_xmit); 832eb1d1641SHerbert Xu } else 833eb1d1641SHerbert Xu netif_rx(skb); 8348ef2a9a5SYOSHIFUJI Hideaki } 835eb1d1641SHerbert Xu 8368ef2a9a5SYOSHIFUJI Hideaki static void br_multicast_send_query(struct net_bridge *br, 8378ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, u32 sent) 8388ef2a9a5SYOSHIFUJI Hideaki { 8398ef2a9a5SYOSHIFUJI Hideaki unsigned long time; 8408ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 8418ef2a9a5SYOSHIFUJI Hideaki 8428ef2a9a5SYOSHIFUJI Hideaki if (!netif_running(br->dev) || br->multicast_disabled || 8438ef2a9a5SYOSHIFUJI Hideaki timer_pending(&br->multicast_querier_timer)) 8448ef2a9a5SYOSHIFUJI Hideaki return; 8458ef2a9a5SYOSHIFUJI Hideaki 84608b202b6SYOSHIFUJI Hideaki memset(&br_group.u, 0, sizeof(br_group.u)); 8478ef2a9a5SYOSHIFUJI Hideaki 84808b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 8498ef2a9a5SYOSHIFUJI Hideaki __br_multicast_send_query(br, port, &br_group); 8508ef2a9a5SYOSHIFUJI Hideaki 851dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 85208b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 85308b202b6SYOSHIFUJI Hideaki __br_multicast_send_query(br, port, &br_group); 85408b202b6SYOSHIFUJI Hideaki #endif 85508b202b6SYOSHIFUJI Hideaki 856eb1d1641SHerbert Xu time = jiffies; 857eb1d1641SHerbert Xu time += sent < br->multicast_startup_query_count ? 858eb1d1641SHerbert Xu br->multicast_startup_query_interval : 859eb1d1641SHerbert Xu br->multicast_query_interval; 860eb1d1641SHerbert Xu mod_timer(port ? &port->multicast_query_timer : 861eb1d1641SHerbert Xu &br->multicast_query_timer, time); 862eb1d1641SHerbert Xu } 863eb1d1641SHerbert Xu 864eb1d1641SHerbert Xu static void br_multicast_port_query_expired(unsigned long data) 865eb1d1641SHerbert Xu { 866eb1d1641SHerbert Xu struct net_bridge_port *port = (void *)data; 867eb1d1641SHerbert Xu struct net_bridge *br = port->br; 868eb1d1641SHerbert Xu 869eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 87002a780c0SDan Carpenter if (port->state == BR_STATE_DISABLED || 87102a780c0SDan Carpenter port->state == BR_STATE_BLOCKING) 872eb1d1641SHerbert Xu goto out; 873eb1d1641SHerbert Xu 874eb1d1641SHerbert Xu if (port->multicast_startup_queries_sent < 875eb1d1641SHerbert Xu br->multicast_startup_query_count) 876eb1d1641SHerbert Xu port->multicast_startup_queries_sent++; 877eb1d1641SHerbert Xu 878eb1d1641SHerbert Xu br_multicast_send_query(port->br, port, 879eb1d1641SHerbert Xu port->multicast_startup_queries_sent); 880eb1d1641SHerbert Xu 881eb1d1641SHerbert Xu out: 882eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 883eb1d1641SHerbert Xu } 884eb1d1641SHerbert Xu 885eb1d1641SHerbert Xu void br_multicast_add_port(struct net_bridge_port *port) 886eb1d1641SHerbert Xu { 887eb1d1641SHerbert Xu port->multicast_router = 1; 888eb1d1641SHerbert Xu 889eb1d1641SHerbert Xu setup_timer(&port->multicast_router_timer, br_multicast_router_expired, 890eb1d1641SHerbert Xu (unsigned long)port); 891eb1d1641SHerbert Xu setup_timer(&port->multicast_query_timer, 892eb1d1641SHerbert Xu br_multicast_port_query_expired, (unsigned long)port); 893eb1d1641SHerbert Xu } 894eb1d1641SHerbert Xu 895eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port) 896eb1d1641SHerbert Xu { 897eb1d1641SHerbert Xu del_timer_sync(&port->multicast_router_timer); 898eb1d1641SHerbert Xu } 899eb1d1641SHerbert Xu 900561f1103SHerbert Xu static void __br_multicast_enable_port(struct net_bridge_port *port) 901561f1103SHerbert Xu { 902561f1103SHerbert Xu port->multicast_startup_queries_sent = 0; 903561f1103SHerbert Xu 904561f1103SHerbert Xu if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 || 905561f1103SHerbert Xu del_timer(&port->multicast_query_timer)) 906561f1103SHerbert Xu mod_timer(&port->multicast_query_timer, jiffies); 907561f1103SHerbert Xu } 908561f1103SHerbert Xu 909eb1d1641SHerbert Xu void br_multicast_enable_port(struct net_bridge_port *port) 910eb1d1641SHerbert Xu { 911eb1d1641SHerbert Xu struct net_bridge *br = port->br; 912eb1d1641SHerbert Xu 913eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 914eb1d1641SHerbert Xu if (br->multicast_disabled || !netif_running(br->dev)) 915eb1d1641SHerbert Xu goto out; 916eb1d1641SHerbert Xu 917561f1103SHerbert Xu __br_multicast_enable_port(port); 918eb1d1641SHerbert Xu 919eb1d1641SHerbert Xu out: 920eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 921eb1d1641SHerbert Xu } 922eb1d1641SHerbert Xu 923eb1d1641SHerbert Xu void br_multicast_disable_port(struct net_bridge_port *port) 924eb1d1641SHerbert Xu { 925eb1d1641SHerbert Xu struct net_bridge *br = port->br; 926eb1d1641SHerbert Xu struct net_bridge_port_group *pg; 927eb1d1641SHerbert Xu struct hlist_node *p, *n; 928eb1d1641SHerbert Xu 929eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 930eb1d1641SHerbert Xu hlist_for_each_entry_safe(pg, p, n, &port->mglist, mglist) 931eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 932eb1d1641SHerbert Xu 933eb1d1641SHerbert Xu if (!hlist_unhashed(&port->rlist)) 934eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 935eb1d1641SHerbert Xu del_timer(&port->multicast_router_timer); 936eb1d1641SHerbert Xu del_timer(&port->multicast_query_timer); 937eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 938eb1d1641SHerbert Xu } 939eb1d1641SHerbert Xu 9408ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_igmp3_report(struct net_bridge *br, 941eb1d1641SHerbert Xu struct net_bridge_port *port, 942eb1d1641SHerbert Xu struct sk_buff *skb) 943eb1d1641SHerbert Xu { 944eb1d1641SHerbert Xu struct igmpv3_report *ih; 945eb1d1641SHerbert Xu struct igmpv3_grec *grec; 946eb1d1641SHerbert Xu int i; 947eb1d1641SHerbert Xu int len; 948eb1d1641SHerbert Xu int num; 949eb1d1641SHerbert Xu int type; 950eb1d1641SHerbert Xu int err = 0; 951eb1d1641SHerbert Xu __be32 group; 952eb1d1641SHerbert Xu 953eb1d1641SHerbert Xu if (!pskb_may_pull(skb, sizeof(*ih))) 954eb1d1641SHerbert Xu return -EINVAL; 955eb1d1641SHerbert Xu 956eb1d1641SHerbert Xu ih = igmpv3_report_hdr(skb); 957eb1d1641SHerbert Xu num = ntohs(ih->ngrec); 958eb1d1641SHerbert Xu len = sizeof(*ih); 959eb1d1641SHerbert Xu 960eb1d1641SHerbert Xu for (i = 0; i < num; i++) { 961eb1d1641SHerbert Xu len += sizeof(*grec); 962eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 963eb1d1641SHerbert Xu return -EINVAL; 964eb1d1641SHerbert Xu 965fd218cf9SHerbert Xu grec = (void *)(skb->data + len - sizeof(*grec)); 966eb1d1641SHerbert Xu group = grec->grec_mca; 967eb1d1641SHerbert Xu type = grec->grec_type; 968eb1d1641SHerbert Xu 9698eabf95cSEric Dumazet len += ntohs(grec->grec_nsrcs) * 4; 970eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 971eb1d1641SHerbert Xu return -EINVAL; 972eb1d1641SHerbert Xu 973eb1d1641SHerbert Xu /* We treat this as an IGMPv2 report for now. */ 974eb1d1641SHerbert Xu switch (type) { 975eb1d1641SHerbert Xu case IGMPV3_MODE_IS_INCLUDE: 976eb1d1641SHerbert Xu case IGMPV3_MODE_IS_EXCLUDE: 977eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_INCLUDE: 978eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_EXCLUDE: 979eb1d1641SHerbert Xu case IGMPV3_ALLOW_NEW_SOURCES: 980eb1d1641SHerbert Xu case IGMPV3_BLOCK_OLD_SOURCES: 981eb1d1641SHerbert Xu break; 982eb1d1641SHerbert Xu 983eb1d1641SHerbert Xu default: 984eb1d1641SHerbert Xu continue; 985eb1d1641SHerbert Xu } 986eb1d1641SHerbert Xu 9878ef2a9a5SYOSHIFUJI Hideaki err = br_ip4_multicast_add_group(br, port, group); 988eb1d1641SHerbert Xu if (err) 989eb1d1641SHerbert Xu break; 990eb1d1641SHerbert Xu } 991eb1d1641SHerbert Xu 992eb1d1641SHerbert Xu return err; 993eb1d1641SHerbert Xu } 994eb1d1641SHerbert Xu 995dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 99608b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_mld2_report(struct net_bridge *br, 99708b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 99808b202b6SYOSHIFUJI Hideaki struct sk_buff *skb) 99908b202b6SYOSHIFUJI Hideaki { 100008b202b6SYOSHIFUJI Hideaki struct icmp6hdr *icmp6h; 100108b202b6SYOSHIFUJI Hideaki struct mld2_grec *grec; 100208b202b6SYOSHIFUJI Hideaki int i; 100308b202b6SYOSHIFUJI Hideaki int len; 100408b202b6SYOSHIFUJI Hideaki int num; 100508b202b6SYOSHIFUJI Hideaki int err = 0; 100608b202b6SYOSHIFUJI Hideaki 100708b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*icmp6h))) 100808b202b6SYOSHIFUJI Hideaki return -EINVAL; 100908b202b6SYOSHIFUJI Hideaki 101008b202b6SYOSHIFUJI Hideaki icmp6h = icmp6_hdr(skb); 101108b202b6SYOSHIFUJI Hideaki num = ntohs(icmp6h->icmp6_dataun.un_data16[1]); 101208b202b6SYOSHIFUJI Hideaki len = sizeof(*icmp6h); 101308b202b6SYOSHIFUJI Hideaki 101408b202b6SYOSHIFUJI Hideaki for (i = 0; i < num; i++) { 101508b202b6SYOSHIFUJI Hideaki __be16 *nsrcs, _nsrcs; 101608b202b6SYOSHIFUJI Hideaki 101708b202b6SYOSHIFUJI Hideaki nsrcs = skb_header_pointer(skb, 101808b202b6SYOSHIFUJI Hideaki len + offsetof(struct mld2_grec, 1019649e984dSLinus Lüssing grec_nsrcs), 102008b202b6SYOSHIFUJI Hideaki sizeof(_nsrcs), &_nsrcs); 102108b202b6SYOSHIFUJI Hideaki if (!nsrcs) 102208b202b6SYOSHIFUJI Hideaki return -EINVAL; 102308b202b6SYOSHIFUJI Hideaki 102408b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, 102508b202b6SYOSHIFUJI Hideaki len + sizeof(*grec) + 1026d41db9f3SLinus Lüssing sizeof(struct in6_addr) * ntohs(*nsrcs))) 102708b202b6SYOSHIFUJI Hideaki return -EINVAL; 102808b202b6SYOSHIFUJI Hideaki 102908b202b6SYOSHIFUJI Hideaki grec = (struct mld2_grec *)(skb->data + len); 1030d41db9f3SLinus Lüssing len += sizeof(*grec) + 1031d41db9f3SLinus Lüssing sizeof(struct in6_addr) * ntohs(*nsrcs); 103208b202b6SYOSHIFUJI Hideaki 103308b202b6SYOSHIFUJI Hideaki /* We treat these as MLDv1 reports for now. */ 103408b202b6SYOSHIFUJI Hideaki switch (grec->grec_type) { 103508b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_INCLUDE: 103608b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_EXCLUDE: 103708b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_INCLUDE: 103808b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_EXCLUDE: 103908b202b6SYOSHIFUJI Hideaki case MLD2_ALLOW_NEW_SOURCES: 104008b202b6SYOSHIFUJI Hideaki case MLD2_BLOCK_OLD_SOURCES: 104108b202b6SYOSHIFUJI Hideaki break; 104208b202b6SYOSHIFUJI Hideaki 104308b202b6SYOSHIFUJI Hideaki default: 104408b202b6SYOSHIFUJI Hideaki continue; 104508b202b6SYOSHIFUJI Hideaki } 104608b202b6SYOSHIFUJI Hideaki 104708b202b6SYOSHIFUJI Hideaki err = br_ip6_multicast_add_group(br, port, &grec->grec_mca); 104808b202b6SYOSHIFUJI Hideaki if (!err) 104908b202b6SYOSHIFUJI Hideaki break; 105008b202b6SYOSHIFUJI Hideaki } 105108b202b6SYOSHIFUJI Hideaki 105208b202b6SYOSHIFUJI Hideaki return err; 105308b202b6SYOSHIFUJI Hideaki } 105408b202b6SYOSHIFUJI Hideaki #endif 105508b202b6SYOSHIFUJI Hideaki 10567e80c124Sstephen hemminger /* 10577e80c124Sstephen hemminger * Add port to rotuer_list 10587e80c124Sstephen hemminger * list is maintained ordered by pointer value 10597e80c124Sstephen hemminger * and locked by br->multicast_lock and RCU 10607e80c124Sstephen hemminger */ 10610909e117SHerbert Xu static void br_multicast_add_router(struct net_bridge *br, 10620909e117SHerbert Xu struct net_bridge_port *port) 10630909e117SHerbert Xu { 1064dcdca2c4Sstephen hemminger struct net_bridge_port *p; 10657e80c124Sstephen hemminger struct hlist_node *n, *slot = NULL; 10660909e117SHerbert Xu 1067709b9326SDavid S. Miller hlist_for_each_entry(p, n, &br->router_list, rlist) { 10687e80c124Sstephen hemminger if ((unsigned long) port >= (unsigned long) p) 10697e80c124Sstephen hemminger break; 10707e80c124Sstephen hemminger slot = n; 1071dcdca2c4Sstephen hemminger } 10720909e117SHerbert Xu 10737e80c124Sstephen hemminger if (slot) 10747e80c124Sstephen hemminger hlist_add_after_rcu(slot, &port->rlist); 1075dcdca2c4Sstephen hemminger else 1076dcdca2c4Sstephen hemminger hlist_add_head_rcu(&port->rlist, &br->router_list); 10770909e117SHerbert Xu } 10780909e117SHerbert Xu 1079eb1d1641SHerbert Xu static void br_multicast_mark_router(struct net_bridge *br, 1080eb1d1641SHerbert Xu struct net_bridge_port *port) 1081eb1d1641SHerbert Xu { 1082eb1d1641SHerbert Xu unsigned long now = jiffies; 1083eb1d1641SHerbert Xu 1084eb1d1641SHerbert Xu if (!port) { 1085eb1d1641SHerbert Xu if (br->multicast_router == 1) 1086eb1d1641SHerbert Xu mod_timer(&br->multicast_router_timer, 1087eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1088eb1d1641SHerbert Xu return; 1089eb1d1641SHerbert Xu } 1090eb1d1641SHerbert Xu 1091eb1d1641SHerbert Xu if (port->multicast_router != 1) 1092eb1d1641SHerbert Xu return; 1093eb1d1641SHerbert Xu 1094eb1d1641SHerbert Xu if (!hlist_unhashed(&port->rlist)) 1095eb1d1641SHerbert Xu goto timer; 1096eb1d1641SHerbert Xu 10970909e117SHerbert Xu br_multicast_add_router(br, port); 1098eb1d1641SHerbert Xu 1099eb1d1641SHerbert Xu timer: 1100eb1d1641SHerbert Xu mod_timer(&port->multicast_router_timer, 1101eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1102eb1d1641SHerbert Xu } 1103eb1d1641SHerbert Xu 1104eb1d1641SHerbert Xu static void br_multicast_query_received(struct net_bridge *br, 1105eb1d1641SHerbert Xu struct net_bridge_port *port, 11068ef2a9a5SYOSHIFUJI Hideaki int saddr) 1107eb1d1641SHerbert Xu { 1108eb1d1641SHerbert Xu if (saddr) 1109eb1d1641SHerbert Xu mod_timer(&br->multicast_querier_timer, 1110eb1d1641SHerbert Xu jiffies + br->multicast_querier_interval); 1111eb1d1641SHerbert Xu else if (timer_pending(&br->multicast_querier_timer)) 1112eb1d1641SHerbert Xu return; 1113eb1d1641SHerbert Xu 1114eb1d1641SHerbert Xu br_multicast_mark_router(br, port); 1115eb1d1641SHerbert Xu } 1116eb1d1641SHerbert Xu 11178ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_query(struct net_bridge *br, 1118eb1d1641SHerbert Xu struct net_bridge_port *port, 1119eb1d1641SHerbert Xu struct sk_buff *skb) 1120eb1d1641SHerbert Xu { 1121b71d1d42SEric Dumazet const struct iphdr *iph = ip_hdr(skb); 1122eb1d1641SHerbert Xu struct igmphdr *ih = igmp_hdr(skb); 1123eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1124eb1d1641SHerbert Xu struct igmpv3_query *ih3; 1125eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1126e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 1127eb1d1641SHerbert Xu unsigned long max_delay; 1128eb1d1641SHerbert Xu unsigned long now = jiffies; 1129eb1d1641SHerbert Xu __be32 group; 1130bec68ff1SYOSHIFUJI Hideaki int err = 0; 1131eb1d1641SHerbert Xu 1132eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1133eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1134eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 1135eb1d1641SHerbert Xu goto out; 1136eb1d1641SHerbert Xu 11378ef2a9a5SYOSHIFUJI Hideaki br_multicast_query_received(br, port, !!iph->saddr); 1138eb1d1641SHerbert Xu 1139eb1d1641SHerbert Xu group = ih->group; 1140eb1d1641SHerbert Xu 1141eb1d1641SHerbert Xu if (skb->len == sizeof(*ih)) { 1142eb1d1641SHerbert Xu max_delay = ih->code * (HZ / IGMP_TIMER_SCALE); 1143eb1d1641SHerbert Xu 1144eb1d1641SHerbert Xu if (!max_delay) { 1145eb1d1641SHerbert Xu max_delay = 10 * HZ; 1146eb1d1641SHerbert Xu group = 0; 1147eb1d1641SHerbert Xu } 1148eb1d1641SHerbert Xu } else { 1149bec68ff1SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) { 1150bec68ff1SYOSHIFUJI Hideaki err = -EINVAL; 1151bec68ff1SYOSHIFUJI Hideaki goto out; 1152bec68ff1SYOSHIFUJI Hideaki } 1153eb1d1641SHerbert Xu 1154eb1d1641SHerbert Xu ih3 = igmpv3_query_hdr(skb); 1155eb1d1641SHerbert Xu if (ih3->nsrcs) 1156bec68ff1SYOSHIFUJI Hideaki goto out; 1157eb1d1641SHerbert Xu 11580ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 max_delay = ih3->code ? 11590ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1; 1160eb1d1641SHerbert Xu } 1161eb1d1641SHerbert Xu 1162eb1d1641SHerbert Xu if (!group) 1163eb1d1641SHerbert Xu goto out; 1164eb1d1641SHerbert Xu 1165e8051688SEric Dumazet mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group); 1166eb1d1641SHerbert Xu if (!mp) 1167eb1d1641SHerbert Xu goto out; 1168eb1d1641SHerbert Xu 1169eb1d1641SHerbert Xu max_delay *= br->multicast_last_member_count; 1170eb1d1641SHerbert Xu 11718a870178SHerbert Xu if (mp->mglist && 1172eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1173eb1d1641SHerbert Xu time_after(mp->timer.expires, now + max_delay) : 1174eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) 1175eb1d1641SHerbert Xu mod_timer(&mp->timer, now + max_delay); 1176eb1d1641SHerbert Xu 1177e8051688SEric Dumazet for (pp = &mp->ports; 1178e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 1179e8051688SEric Dumazet pp = &p->next) { 1180eb1d1641SHerbert Xu if (timer_pending(&p->timer) ? 1181eb1d1641SHerbert Xu time_after(p->timer.expires, now + max_delay) : 1182eb1d1641SHerbert Xu try_to_del_timer_sync(&p->timer) >= 0) 118324f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 1184eb1d1641SHerbert Xu } 1185eb1d1641SHerbert Xu 1186eb1d1641SHerbert Xu out: 1187eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1188bec68ff1SYOSHIFUJI Hideaki return err; 1189eb1d1641SHerbert Xu } 1190eb1d1641SHerbert Xu 1191dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 119208b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_query(struct net_bridge *br, 119308b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 119408b202b6SYOSHIFUJI Hideaki struct sk_buff *skb) 119508b202b6SYOSHIFUJI Hideaki { 1196b71d1d42SEric Dumazet const struct ipv6hdr *ip6h = ipv6_hdr(skb); 119708b202b6SYOSHIFUJI Hideaki struct mld_msg *mld = (struct mld_msg *) icmp6_hdr(skb); 119808b202b6SYOSHIFUJI Hideaki struct net_bridge_mdb_entry *mp; 119908b202b6SYOSHIFUJI Hideaki struct mld2_query *mld2q; 1200e8051688SEric Dumazet struct net_bridge_port_group *p; 1201e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 120208b202b6SYOSHIFUJI Hideaki unsigned long max_delay; 120308b202b6SYOSHIFUJI Hideaki unsigned long now = jiffies; 1204b71d1d42SEric Dumazet const struct in6_addr *group = NULL; 120508b202b6SYOSHIFUJI Hideaki int err = 0; 120608b202b6SYOSHIFUJI Hideaki 120708b202b6SYOSHIFUJI Hideaki spin_lock(&br->multicast_lock); 120808b202b6SYOSHIFUJI Hideaki if (!netif_running(br->dev) || 120908b202b6SYOSHIFUJI Hideaki (port && port->state == BR_STATE_DISABLED)) 121008b202b6SYOSHIFUJI Hideaki goto out; 121108b202b6SYOSHIFUJI Hideaki 121208b202b6SYOSHIFUJI Hideaki br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->saddr)); 121308b202b6SYOSHIFUJI Hideaki 121408b202b6SYOSHIFUJI Hideaki if (skb->len == sizeof(*mld)) { 121508b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*mld))) { 121608b202b6SYOSHIFUJI Hideaki err = -EINVAL; 121708b202b6SYOSHIFUJI Hideaki goto out; 121808b202b6SYOSHIFUJI Hideaki } 121908b202b6SYOSHIFUJI Hideaki mld = (struct mld_msg *) icmp6_hdr(skb); 122008b202b6SYOSHIFUJI Hideaki max_delay = msecs_to_jiffies(htons(mld->mld_maxdelay)); 122108b202b6SYOSHIFUJI Hideaki if (max_delay) 122208b202b6SYOSHIFUJI Hideaki group = &mld->mld_mca; 122308b202b6SYOSHIFUJI Hideaki } else if (skb->len >= sizeof(*mld2q)) { 122408b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*mld2q))) { 122508b202b6SYOSHIFUJI Hideaki err = -EINVAL; 122608b202b6SYOSHIFUJI Hideaki goto out; 122708b202b6SYOSHIFUJI Hideaki } 122808b202b6SYOSHIFUJI Hideaki mld2q = (struct mld2_query *)icmp6_hdr(skb); 122908b202b6SYOSHIFUJI Hideaki if (!mld2q->mld2q_nsrcs) 123008b202b6SYOSHIFUJI Hideaki group = &mld2q->mld2q_mca; 123108b202b6SYOSHIFUJI Hideaki max_delay = mld2q->mld2q_mrc ? MLDV2_MRC(mld2q->mld2q_mrc) : 1; 123208b202b6SYOSHIFUJI Hideaki } 123308b202b6SYOSHIFUJI Hideaki 123408b202b6SYOSHIFUJI Hideaki if (!group) 123508b202b6SYOSHIFUJI Hideaki goto out; 123608b202b6SYOSHIFUJI Hideaki 1237e8051688SEric Dumazet mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group); 123808b202b6SYOSHIFUJI Hideaki if (!mp) 123908b202b6SYOSHIFUJI Hideaki goto out; 124008b202b6SYOSHIFUJI Hideaki 124108b202b6SYOSHIFUJI Hideaki max_delay *= br->multicast_last_member_count; 12428a870178SHerbert Xu if (mp->mglist && 124308b202b6SYOSHIFUJI Hideaki (timer_pending(&mp->timer) ? 124408b202b6SYOSHIFUJI Hideaki time_after(mp->timer.expires, now + max_delay) : 124508b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&mp->timer) >= 0)) 124608b202b6SYOSHIFUJI Hideaki mod_timer(&mp->timer, now + max_delay); 124708b202b6SYOSHIFUJI Hideaki 1248e8051688SEric Dumazet for (pp = &mp->ports; 1249e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 1250e8051688SEric Dumazet pp = &p->next) { 125108b202b6SYOSHIFUJI Hideaki if (timer_pending(&p->timer) ? 125208b202b6SYOSHIFUJI Hideaki time_after(p->timer.expires, now + max_delay) : 125308b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&p->timer) >= 0) 125424f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 125508b202b6SYOSHIFUJI Hideaki } 125608b202b6SYOSHIFUJI Hideaki 125708b202b6SYOSHIFUJI Hideaki out: 125808b202b6SYOSHIFUJI Hideaki spin_unlock(&br->multicast_lock); 125908b202b6SYOSHIFUJI Hideaki return err; 126008b202b6SYOSHIFUJI Hideaki } 126108b202b6SYOSHIFUJI Hideaki #endif 126208b202b6SYOSHIFUJI Hideaki 1263eb1d1641SHerbert Xu static void br_multicast_leave_group(struct net_bridge *br, 1264eb1d1641SHerbert Xu struct net_bridge_port *port, 12658ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group) 1266eb1d1641SHerbert Xu { 1267eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 1268eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1269eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1270eb1d1641SHerbert Xu unsigned long now; 1271eb1d1641SHerbert Xu unsigned long time; 1272eb1d1641SHerbert Xu 1273eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1274eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1275eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED) || 1276eb1d1641SHerbert Xu timer_pending(&br->multicast_querier_timer)) 1277eb1d1641SHerbert Xu goto out; 1278eb1d1641SHerbert Xu 1279e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1280eb1d1641SHerbert Xu mp = br_mdb_ip_get(mdb, group); 1281eb1d1641SHerbert Xu if (!mp) 1282eb1d1641SHerbert Xu goto out; 1283eb1d1641SHerbert Xu 1284eb1d1641SHerbert Xu now = jiffies; 1285eb1d1641SHerbert Xu time = now + br->multicast_last_member_count * 1286eb1d1641SHerbert Xu br->multicast_last_member_interval; 1287eb1d1641SHerbert Xu 1288eb1d1641SHerbert Xu if (!port) { 12898a870178SHerbert Xu if (mp->mglist && 1290eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1291eb1d1641SHerbert Xu time_after(mp->timer.expires, time) : 1292eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) { 1293eb1d1641SHerbert Xu mod_timer(&mp->timer, time); 1294eb1d1641SHerbert Xu 1295eb1d1641SHerbert Xu mp->queries_sent = 0; 1296eb1d1641SHerbert Xu mod_timer(&mp->query_timer, now); 1297eb1d1641SHerbert Xu } 1298eb1d1641SHerbert Xu 1299eb1d1641SHerbert Xu goto out; 1300eb1d1641SHerbert Xu } 1301eb1d1641SHerbert Xu 1302e8051688SEric Dumazet for (p = mlock_dereference(mp->ports, br); 1303e8051688SEric Dumazet p != NULL; 1304e8051688SEric Dumazet p = mlock_dereference(p->next, br)) { 1305eb1d1641SHerbert Xu if (p->port != port) 1306eb1d1641SHerbert Xu continue; 1307eb1d1641SHerbert Xu 1308eb1d1641SHerbert Xu if (!hlist_unhashed(&p->mglist) && 1309eb1d1641SHerbert Xu (timer_pending(&p->timer) ? 1310eb1d1641SHerbert Xu time_after(p->timer.expires, time) : 1311eb1d1641SHerbert Xu try_to_del_timer_sync(&p->timer) >= 0)) { 1312eb1d1641SHerbert Xu mod_timer(&p->timer, time); 1313eb1d1641SHerbert Xu 1314eb1d1641SHerbert Xu p->queries_sent = 0; 1315eb1d1641SHerbert Xu mod_timer(&p->query_timer, now); 1316eb1d1641SHerbert Xu } 1317eb1d1641SHerbert Xu 1318eb1d1641SHerbert Xu break; 1319eb1d1641SHerbert Xu } 1320eb1d1641SHerbert Xu 1321eb1d1641SHerbert Xu out: 1322eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1323eb1d1641SHerbert Xu } 1324eb1d1641SHerbert Xu 13258ef2a9a5SYOSHIFUJI Hideaki static void br_ip4_multicast_leave_group(struct net_bridge *br, 13268ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 13278ef2a9a5SYOSHIFUJI Hideaki __be32 group) 13288ef2a9a5SYOSHIFUJI Hideaki { 13298ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 13308ef2a9a5SYOSHIFUJI Hideaki 13318ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 13328ef2a9a5SYOSHIFUJI Hideaki return; 13338ef2a9a5SYOSHIFUJI Hideaki 13348ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 13358ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 13368ef2a9a5SYOSHIFUJI Hideaki 13378ef2a9a5SYOSHIFUJI Hideaki br_multicast_leave_group(br, port, &br_group); 13388ef2a9a5SYOSHIFUJI Hideaki } 13398ef2a9a5SYOSHIFUJI Hideaki 1340dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 134108b202b6SYOSHIFUJI Hideaki static void br_ip6_multicast_leave_group(struct net_bridge *br, 134208b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 134308b202b6SYOSHIFUJI Hideaki const struct in6_addr *group) 134408b202b6SYOSHIFUJI Hideaki { 134508b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 134608b202b6SYOSHIFUJI Hideaki 1347e4de9f9eSLinus Lüssing if (!ipv6_is_transient_multicast(group)) 134808b202b6SYOSHIFUJI Hideaki return; 134908b202b6SYOSHIFUJI Hideaki 13504e3fd7a0SAlexey Dobriyan br_group.u.ip6 = *group; 135108b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 135208b202b6SYOSHIFUJI Hideaki 135308b202b6SYOSHIFUJI Hideaki br_multicast_leave_group(br, port, &br_group); 135408b202b6SYOSHIFUJI Hideaki } 135508b202b6SYOSHIFUJI Hideaki #endif 13568ef2a9a5SYOSHIFUJI Hideaki 1357eb1d1641SHerbert Xu static int br_multicast_ipv4_rcv(struct net_bridge *br, 1358eb1d1641SHerbert Xu struct net_bridge_port *port, 1359eb1d1641SHerbert Xu struct sk_buff *skb) 1360eb1d1641SHerbert Xu { 1361eb1d1641SHerbert Xu struct sk_buff *skb2 = skb; 1362b71d1d42SEric Dumazet const struct iphdr *iph; 1363eb1d1641SHerbert Xu struct igmphdr *ih; 1364eb1d1641SHerbert Xu unsigned len; 1365eb1d1641SHerbert Xu unsigned offset; 1366eb1d1641SHerbert Xu int err; 1367eb1d1641SHerbert Xu 1368eb1d1641SHerbert Xu /* We treat OOM as packet loss for now. */ 1369eb1d1641SHerbert Xu if (!pskb_may_pull(skb, sizeof(*iph))) 1370eb1d1641SHerbert Xu return -EINVAL; 1371eb1d1641SHerbert Xu 1372eb1d1641SHerbert Xu iph = ip_hdr(skb); 1373eb1d1641SHerbert Xu 1374eb1d1641SHerbert Xu if (iph->ihl < 5 || iph->version != 4) 1375eb1d1641SHerbert Xu return -EINVAL; 1376eb1d1641SHerbert Xu 1377eb1d1641SHerbert Xu if (!pskb_may_pull(skb, ip_hdrlen(skb))) 1378eb1d1641SHerbert Xu return -EINVAL; 1379eb1d1641SHerbert Xu 1380eb1d1641SHerbert Xu iph = ip_hdr(skb); 1381eb1d1641SHerbert Xu 1382eb1d1641SHerbert Xu if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl))) 1383eb1d1641SHerbert Xu return -EINVAL; 1384eb1d1641SHerbert Xu 1385bd4265feSHerbert Xu if (iph->protocol != IPPROTO_IGMP) { 1386bd4265feSHerbert Xu if ((iph->daddr & IGMP_LOCAL_GROUP_MASK) != IGMP_LOCAL_GROUP) 1387bd4265feSHerbert Xu BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 1388eb1d1641SHerbert Xu return 0; 1389bd4265feSHerbert Xu } 1390eb1d1641SHerbert Xu 1391eb1d1641SHerbert Xu len = ntohs(iph->tot_len); 1392eb1d1641SHerbert Xu if (skb->len < len || len < ip_hdrlen(skb)) 1393eb1d1641SHerbert Xu return -EINVAL; 1394eb1d1641SHerbert Xu 1395eb1d1641SHerbert Xu if (skb->len > len) { 1396eb1d1641SHerbert Xu skb2 = skb_clone(skb, GFP_ATOMIC); 1397eb1d1641SHerbert Xu if (!skb2) 1398eb1d1641SHerbert Xu return -ENOMEM; 1399eb1d1641SHerbert Xu 1400eb1d1641SHerbert Xu err = pskb_trim_rcsum(skb2, len); 1401eb1d1641SHerbert Xu if (err) 14028440853bSYOSHIFUJI Hideaki / 吉藤英明 goto err_out; 1403eb1d1641SHerbert Xu } 1404eb1d1641SHerbert Xu 1405eb1d1641SHerbert Xu len -= ip_hdrlen(skb2); 1406eb1d1641SHerbert Xu offset = skb_network_offset(skb2) + ip_hdrlen(skb2); 1407eb1d1641SHerbert Xu __skb_pull(skb2, offset); 1408eb1d1641SHerbert Xu skb_reset_transport_header(skb2); 1409eb1d1641SHerbert Xu 1410eb1d1641SHerbert Xu err = -EINVAL; 1411eb1d1641SHerbert Xu if (!pskb_may_pull(skb2, sizeof(*ih))) 1412eb1d1641SHerbert Xu goto out; 1413eb1d1641SHerbert Xu 1414eb1d1641SHerbert Xu switch (skb2->ip_summed) { 1415eb1d1641SHerbert Xu case CHECKSUM_COMPLETE: 1416eb1d1641SHerbert Xu if (!csum_fold(skb2->csum)) 1417eb1d1641SHerbert Xu break; 1418eb1d1641SHerbert Xu /* fall through */ 1419eb1d1641SHerbert Xu case CHECKSUM_NONE: 1420eb1d1641SHerbert Xu skb2->csum = 0; 1421eb1d1641SHerbert Xu if (skb_checksum_complete(skb2)) 14228440853bSYOSHIFUJI Hideaki / 吉藤英明 goto out; 1423eb1d1641SHerbert Xu } 1424eb1d1641SHerbert Xu 1425eb1d1641SHerbert Xu err = 0; 1426eb1d1641SHerbert Xu 1427eb1d1641SHerbert Xu BR_INPUT_SKB_CB(skb)->igmp = 1; 1428eb1d1641SHerbert Xu ih = igmp_hdr(skb2); 1429eb1d1641SHerbert Xu 1430eb1d1641SHerbert Xu switch (ih->type) { 1431eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_REPORT: 1432eb1d1641SHerbert Xu case IGMPV2_HOST_MEMBERSHIP_REPORT: 143362b2bcb4SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 14348ef2a9a5SYOSHIFUJI Hideaki err = br_ip4_multicast_add_group(br, port, ih->group); 1435eb1d1641SHerbert Xu break; 1436eb1d1641SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 14378ef2a9a5SYOSHIFUJI Hideaki err = br_ip4_multicast_igmp3_report(br, port, skb2); 1438eb1d1641SHerbert Xu break; 1439eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_QUERY: 14408ef2a9a5SYOSHIFUJI Hideaki err = br_ip4_multicast_query(br, port, skb2); 1441eb1d1641SHerbert Xu break; 1442eb1d1641SHerbert Xu case IGMP_HOST_LEAVE_MESSAGE: 14438ef2a9a5SYOSHIFUJI Hideaki br_ip4_multicast_leave_group(br, port, ih->group); 1444eb1d1641SHerbert Xu break; 1445eb1d1641SHerbert Xu } 1446eb1d1641SHerbert Xu 1447eb1d1641SHerbert Xu out: 1448eb1d1641SHerbert Xu __skb_push(skb2, offset); 14498440853bSYOSHIFUJI Hideaki / 吉藤英明 err_out: 1450eb1d1641SHerbert Xu if (skb2 != skb) 1451eb1d1641SHerbert Xu kfree_skb(skb2); 1452eb1d1641SHerbert Xu return err; 1453eb1d1641SHerbert Xu } 1454eb1d1641SHerbert Xu 1455dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 145608b202b6SYOSHIFUJI Hideaki static int br_multicast_ipv6_rcv(struct net_bridge *br, 145708b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 145808b202b6SYOSHIFUJI Hideaki struct sk_buff *skb) 145908b202b6SYOSHIFUJI Hideaki { 14609d89081dSTomas Winkler struct sk_buff *skb2; 1461b71d1d42SEric Dumazet const struct ipv6hdr *ip6h; 146222df1331SEric Dumazet u8 icmp6_type; 146308b202b6SYOSHIFUJI Hideaki u8 nexthdr; 146475f2811cSJesse Gross __be16 frag_off; 146508b202b6SYOSHIFUJI Hideaki unsigned len; 1466bb7a0bd6SKulikov Vasiliy int offset; 146708b202b6SYOSHIFUJI Hideaki int err; 146808b202b6SYOSHIFUJI Hideaki 146908b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*ip6h))) 147008b202b6SYOSHIFUJI Hideaki return -EINVAL; 147108b202b6SYOSHIFUJI Hideaki 147208b202b6SYOSHIFUJI Hideaki ip6h = ipv6_hdr(skb); 147308b202b6SYOSHIFUJI Hideaki 147408b202b6SYOSHIFUJI Hideaki /* 147508b202b6SYOSHIFUJI Hideaki * We're interested in MLD messages only. 147608b202b6SYOSHIFUJI Hideaki * - Version is 6 147708b202b6SYOSHIFUJI Hideaki * - MLD has always Router Alert hop-by-hop option 147808b202b6SYOSHIFUJI Hideaki * - But we do not support jumbrograms. 147908b202b6SYOSHIFUJI Hideaki */ 148008b202b6SYOSHIFUJI Hideaki if (ip6h->version != 6 || 148108b202b6SYOSHIFUJI Hideaki ip6h->nexthdr != IPPROTO_HOPOPTS || 148208b202b6SYOSHIFUJI Hideaki ip6h->payload_len == 0) 148308b202b6SYOSHIFUJI Hideaki return 0; 148408b202b6SYOSHIFUJI Hideaki 1485ff9a57a6SLinus Lüssing len = ntohs(ip6h->payload_len) + sizeof(*ip6h); 148608b202b6SYOSHIFUJI Hideaki if (skb->len < len) 148708b202b6SYOSHIFUJI Hideaki return -EINVAL; 148808b202b6SYOSHIFUJI Hideaki 148908b202b6SYOSHIFUJI Hideaki nexthdr = ip6h->nexthdr; 149075f2811cSJesse Gross offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr, &frag_off); 149108b202b6SYOSHIFUJI Hideaki 149208b202b6SYOSHIFUJI Hideaki if (offset < 0 || nexthdr != IPPROTO_ICMPV6) 149308b202b6SYOSHIFUJI Hideaki return 0; 149408b202b6SYOSHIFUJI Hideaki 149508b202b6SYOSHIFUJI Hideaki /* Okay, we found ICMPv6 header */ 149608b202b6SYOSHIFUJI Hideaki skb2 = skb_clone(skb, GFP_ATOMIC); 149708b202b6SYOSHIFUJI Hideaki if (!skb2) 149808b202b6SYOSHIFUJI Hideaki return -ENOMEM; 149908b202b6SYOSHIFUJI Hideaki 15009d89081dSTomas Winkler err = -EINVAL; 15019d89081dSTomas Winkler if (!pskb_may_pull(skb2, offset + sizeof(struct icmp6hdr))) 15029d89081dSTomas Winkler goto out; 15039d89081dSTomas Winkler 150408b202b6SYOSHIFUJI Hideaki len -= offset - skb_network_offset(skb2); 150508b202b6SYOSHIFUJI Hideaki 150608b202b6SYOSHIFUJI Hideaki __skb_pull(skb2, offset); 150708b202b6SYOSHIFUJI Hideaki skb_reset_transport_header(skb2); 1508fa2da8cdSstephen hemminger skb_postpull_rcsum(skb2, skb_network_header(skb2), 1509fa2da8cdSstephen hemminger skb_network_header_len(skb2)); 151008b202b6SYOSHIFUJI Hideaki 151122df1331SEric Dumazet icmp6_type = icmp6_hdr(skb2)->icmp6_type; 151208b202b6SYOSHIFUJI Hideaki 151322df1331SEric Dumazet switch (icmp6_type) { 151408b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 151508b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 151608b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 151708b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 151808b202b6SYOSHIFUJI Hideaki break; 151908b202b6SYOSHIFUJI Hideaki default: 152008b202b6SYOSHIFUJI Hideaki err = 0; 152108b202b6SYOSHIFUJI Hideaki goto out; 152208b202b6SYOSHIFUJI Hideaki } 152308b202b6SYOSHIFUJI Hideaki 152408b202b6SYOSHIFUJI Hideaki /* Okay, we found MLD message. Check further. */ 152508b202b6SYOSHIFUJI Hideaki if (skb2->len > len) { 152608b202b6SYOSHIFUJI Hideaki err = pskb_trim_rcsum(skb2, len); 152708b202b6SYOSHIFUJI Hideaki if (err) 152808b202b6SYOSHIFUJI Hideaki goto out; 15294b275d7eSYan, Zheng err = -EINVAL; 153008b202b6SYOSHIFUJI Hideaki } 153108b202b6SYOSHIFUJI Hideaki 15324b275d7eSYan, Zheng ip6h = ipv6_hdr(skb2); 15334b275d7eSYan, Zheng 153408b202b6SYOSHIFUJI Hideaki switch (skb2->ip_summed) { 153508b202b6SYOSHIFUJI Hideaki case CHECKSUM_COMPLETE: 15364b275d7eSYan, Zheng if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, skb2->len, 15374b275d7eSYan, Zheng IPPROTO_ICMPV6, skb2->csum)) 153808b202b6SYOSHIFUJI Hideaki break; 153908b202b6SYOSHIFUJI Hideaki /*FALLTHROUGH*/ 154008b202b6SYOSHIFUJI Hideaki case CHECKSUM_NONE: 15414b275d7eSYan, Zheng skb2->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr, 15424b275d7eSYan, Zheng &ip6h->daddr, 15434b275d7eSYan, Zheng skb2->len, 15444b275d7eSYan, Zheng IPPROTO_ICMPV6, 0)); 15454b275d7eSYan, Zheng if (__skb_checksum_complete(skb2)) 154608b202b6SYOSHIFUJI Hideaki goto out; 154708b202b6SYOSHIFUJI Hideaki } 154808b202b6SYOSHIFUJI Hideaki 154908b202b6SYOSHIFUJI Hideaki err = 0; 155008b202b6SYOSHIFUJI Hideaki 155108b202b6SYOSHIFUJI Hideaki BR_INPUT_SKB_CB(skb)->igmp = 1; 155208b202b6SYOSHIFUJI Hideaki 155322df1331SEric Dumazet switch (icmp6_type) { 155408b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 155508b202b6SYOSHIFUJI Hideaki { 15569d89081dSTomas Winkler struct mld_msg *mld; 15579d89081dSTomas Winkler if (!pskb_may_pull(skb2, sizeof(*mld))) { 15589d89081dSTomas Winkler err = -EINVAL; 15599d89081dSTomas Winkler goto out; 15609d89081dSTomas Winkler } 15619d89081dSTomas Winkler mld = (struct mld_msg *)skb_transport_header(skb2); 1562fc2af6c7SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 156308b202b6SYOSHIFUJI Hideaki err = br_ip6_multicast_add_group(br, port, &mld->mld_mca); 156408b202b6SYOSHIFUJI Hideaki break; 156508b202b6SYOSHIFUJI Hideaki } 156608b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 156708b202b6SYOSHIFUJI Hideaki err = br_ip6_multicast_mld2_report(br, port, skb2); 156808b202b6SYOSHIFUJI Hideaki break; 156908b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 157008b202b6SYOSHIFUJI Hideaki err = br_ip6_multicast_query(br, port, skb2); 157108b202b6SYOSHIFUJI Hideaki break; 157208b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 157308b202b6SYOSHIFUJI Hideaki { 15749d89081dSTomas Winkler struct mld_msg *mld; 15759d89081dSTomas Winkler if (!pskb_may_pull(skb2, sizeof(*mld))) { 15769d89081dSTomas Winkler err = -EINVAL; 15779d89081dSTomas Winkler goto out; 15789d89081dSTomas Winkler } 15799d89081dSTomas Winkler mld = (struct mld_msg *)skb_transport_header(skb2); 158008b202b6SYOSHIFUJI Hideaki br_ip6_multicast_leave_group(br, port, &mld->mld_mca); 158108b202b6SYOSHIFUJI Hideaki } 158208b202b6SYOSHIFUJI Hideaki } 158308b202b6SYOSHIFUJI Hideaki 158408b202b6SYOSHIFUJI Hideaki out: 158508b202b6SYOSHIFUJI Hideaki kfree_skb(skb2); 158608b202b6SYOSHIFUJI Hideaki return err; 158708b202b6SYOSHIFUJI Hideaki } 158808b202b6SYOSHIFUJI Hideaki #endif 158908b202b6SYOSHIFUJI Hideaki 1590eb1d1641SHerbert Xu int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port, 1591eb1d1641SHerbert Xu struct sk_buff *skb) 1592eb1d1641SHerbert Xu { 15931fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->igmp = 0; 15941fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->mrouters_only = 0; 15951fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 1596eb1d1641SHerbert Xu if (br->multicast_disabled) 1597eb1d1641SHerbert Xu return 0; 1598eb1d1641SHerbert Xu 1599eb1d1641SHerbert Xu switch (skb->protocol) { 1600eb1d1641SHerbert Xu case htons(ETH_P_IP): 1601eb1d1641SHerbert Xu return br_multicast_ipv4_rcv(br, port, skb); 1602dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 160308b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 160408b202b6SYOSHIFUJI Hideaki return br_multicast_ipv6_rcv(br, port, skb); 160508b202b6SYOSHIFUJI Hideaki #endif 1606eb1d1641SHerbert Xu } 1607eb1d1641SHerbert Xu 1608eb1d1641SHerbert Xu return 0; 1609eb1d1641SHerbert Xu } 1610eb1d1641SHerbert Xu 1611eb1d1641SHerbert Xu static void br_multicast_query_expired(unsigned long data) 1612eb1d1641SHerbert Xu { 1613eb1d1641SHerbert Xu struct net_bridge *br = (void *)data; 1614eb1d1641SHerbert Xu 1615eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1616eb1d1641SHerbert Xu if (br->multicast_startup_queries_sent < 1617eb1d1641SHerbert Xu br->multicast_startup_query_count) 1618eb1d1641SHerbert Xu br->multicast_startup_queries_sent++; 1619eb1d1641SHerbert Xu 1620eb1d1641SHerbert Xu br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent); 1621eb1d1641SHerbert Xu 1622eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1623eb1d1641SHerbert Xu } 1624eb1d1641SHerbert Xu 1625eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br) 1626eb1d1641SHerbert Xu { 1627eb1d1641SHerbert Xu br->hash_elasticity = 4; 1628eb1d1641SHerbert Xu br->hash_max = 512; 1629eb1d1641SHerbert Xu 1630eb1d1641SHerbert Xu br->multicast_router = 1; 1631eb1d1641SHerbert Xu br->multicast_last_member_count = 2; 1632eb1d1641SHerbert Xu br->multicast_startup_query_count = 2; 1633eb1d1641SHerbert Xu 1634eb1d1641SHerbert Xu br->multicast_last_member_interval = HZ; 1635eb1d1641SHerbert Xu br->multicast_query_response_interval = 10 * HZ; 1636eb1d1641SHerbert Xu br->multicast_startup_query_interval = 125 * HZ / 4; 1637eb1d1641SHerbert Xu br->multicast_query_interval = 125 * HZ; 1638eb1d1641SHerbert Xu br->multicast_querier_interval = 255 * HZ; 1639eb1d1641SHerbert Xu br->multicast_membership_interval = 260 * HZ; 1640eb1d1641SHerbert Xu 1641eb1d1641SHerbert Xu spin_lock_init(&br->multicast_lock); 1642eb1d1641SHerbert Xu setup_timer(&br->multicast_router_timer, 1643eb1d1641SHerbert Xu br_multicast_local_router_expired, 0); 1644eb1d1641SHerbert Xu setup_timer(&br->multicast_querier_timer, 1645eb1d1641SHerbert Xu br_multicast_local_router_expired, 0); 1646eb1d1641SHerbert Xu setup_timer(&br->multicast_query_timer, br_multicast_query_expired, 1647eb1d1641SHerbert Xu (unsigned long)br); 1648eb1d1641SHerbert Xu } 1649eb1d1641SHerbert Xu 1650eb1d1641SHerbert Xu void br_multicast_open(struct net_bridge *br) 1651eb1d1641SHerbert Xu { 1652eb1d1641SHerbert Xu br->multicast_startup_queries_sent = 0; 1653eb1d1641SHerbert Xu 1654eb1d1641SHerbert Xu if (br->multicast_disabled) 1655eb1d1641SHerbert Xu return; 1656eb1d1641SHerbert Xu 1657eb1d1641SHerbert Xu mod_timer(&br->multicast_query_timer, jiffies); 1658eb1d1641SHerbert Xu } 1659eb1d1641SHerbert Xu 1660eb1d1641SHerbert Xu void br_multicast_stop(struct net_bridge *br) 1661eb1d1641SHerbert Xu { 1662eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 1663eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1664eb1d1641SHerbert Xu struct hlist_node *p, *n; 1665eb1d1641SHerbert Xu u32 ver; 1666eb1d1641SHerbert Xu int i; 1667eb1d1641SHerbert Xu 1668eb1d1641SHerbert Xu del_timer_sync(&br->multicast_router_timer); 1669eb1d1641SHerbert Xu del_timer_sync(&br->multicast_querier_timer); 1670eb1d1641SHerbert Xu del_timer_sync(&br->multicast_query_timer); 1671eb1d1641SHerbert Xu 1672eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1673e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1674eb1d1641SHerbert Xu if (!mdb) 1675eb1d1641SHerbert Xu goto out; 1676eb1d1641SHerbert Xu 1677eb1d1641SHerbert Xu br->mdb = NULL; 1678eb1d1641SHerbert Xu 1679eb1d1641SHerbert Xu ver = mdb->ver; 1680eb1d1641SHerbert Xu for (i = 0; i < mdb->max; i++) { 1681eb1d1641SHerbert Xu hlist_for_each_entry_safe(mp, p, n, &mdb->mhash[i], 1682eb1d1641SHerbert Xu hlist[ver]) { 1683eb1d1641SHerbert Xu del_timer(&mp->timer); 1684eb1d1641SHerbert Xu del_timer(&mp->query_timer); 1685eb1d1641SHerbert Xu call_rcu_bh(&mp->rcu, br_multicast_free_group); 1686eb1d1641SHerbert Xu } 1687eb1d1641SHerbert Xu } 1688eb1d1641SHerbert Xu 1689eb1d1641SHerbert Xu if (mdb->old) { 1690eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 169110cc2b50SHerbert Xu rcu_barrier_bh(); 1692eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1693eb1d1641SHerbert Xu WARN_ON(mdb->old); 1694eb1d1641SHerbert Xu } 1695eb1d1641SHerbert Xu 1696eb1d1641SHerbert Xu mdb->old = mdb; 1697eb1d1641SHerbert Xu call_rcu_bh(&mdb->rcu, br_mdb_free); 1698eb1d1641SHerbert Xu 1699eb1d1641SHerbert Xu out: 1700eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 1701eb1d1641SHerbert Xu } 17020909e117SHerbert Xu 17030909e117SHerbert Xu int br_multicast_set_router(struct net_bridge *br, unsigned long val) 17040909e117SHerbert Xu { 17050909e117SHerbert Xu int err = -ENOENT; 17060909e117SHerbert Xu 17070909e117SHerbert Xu spin_lock_bh(&br->multicast_lock); 17080909e117SHerbert Xu if (!netif_running(br->dev)) 17090909e117SHerbert Xu goto unlock; 17100909e117SHerbert Xu 17110909e117SHerbert Xu switch (val) { 17120909e117SHerbert Xu case 0: 17130909e117SHerbert Xu case 2: 17140909e117SHerbert Xu del_timer(&br->multicast_router_timer); 17150909e117SHerbert Xu /* fall through */ 17160909e117SHerbert Xu case 1: 17170909e117SHerbert Xu br->multicast_router = val; 17180909e117SHerbert Xu err = 0; 17190909e117SHerbert Xu break; 17200909e117SHerbert Xu 17210909e117SHerbert Xu default: 17220909e117SHerbert Xu err = -EINVAL; 17230909e117SHerbert Xu break; 17240909e117SHerbert Xu } 17250909e117SHerbert Xu 17260909e117SHerbert Xu unlock: 17270909e117SHerbert Xu spin_unlock_bh(&br->multicast_lock); 17280909e117SHerbert Xu 17290909e117SHerbert Xu return err; 17300909e117SHerbert Xu } 17310909e117SHerbert Xu 17320909e117SHerbert Xu int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val) 17330909e117SHerbert Xu { 17340909e117SHerbert Xu struct net_bridge *br = p->br; 17350909e117SHerbert Xu int err = -ENOENT; 17360909e117SHerbert Xu 17370909e117SHerbert Xu spin_lock(&br->multicast_lock); 17380909e117SHerbert Xu if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED) 17390909e117SHerbert Xu goto unlock; 17400909e117SHerbert Xu 17410909e117SHerbert Xu switch (val) { 17420909e117SHerbert Xu case 0: 17430909e117SHerbert Xu case 1: 17440909e117SHerbert Xu case 2: 17450909e117SHerbert Xu p->multicast_router = val; 17460909e117SHerbert Xu err = 0; 17470909e117SHerbert Xu 17480909e117SHerbert Xu if (val < 2 && !hlist_unhashed(&p->rlist)) 17490909e117SHerbert Xu hlist_del_init_rcu(&p->rlist); 17500909e117SHerbert Xu 17510909e117SHerbert Xu if (val == 1) 17520909e117SHerbert Xu break; 17530909e117SHerbert Xu 17540909e117SHerbert Xu del_timer(&p->multicast_router_timer); 17550909e117SHerbert Xu 17560909e117SHerbert Xu if (val == 0) 17570909e117SHerbert Xu break; 17580909e117SHerbert Xu 17590909e117SHerbert Xu br_multicast_add_router(br, p); 17600909e117SHerbert Xu break; 17610909e117SHerbert Xu 17620909e117SHerbert Xu default: 17630909e117SHerbert Xu err = -EINVAL; 17640909e117SHerbert Xu break; 17650909e117SHerbert Xu } 17660909e117SHerbert Xu 17670909e117SHerbert Xu unlock: 17680909e117SHerbert Xu spin_unlock(&br->multicast_lock); 17690909e117SHerbert Xu 17700909e117SHerbert Xu return err; 17710909e117SHerbert Xu } 1772561f1103SHerbert Xu 1773561f1103SHerbert Xu int br_multicast_toggle(struct net_bridge *br, unsigned long val) 1774561f1103SHerbert Xu { 1775561f1103SHerbert Xu struct net_bridge_port *port; 17763a7fda06SHerbert Xu int err = 0; 1777e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 1778561f1103SHerbert Xu 1779ef5e0d82SAndrey Vagin spin_lock_bh(&br->multicast_lock); 1780561f1103SHerbert Xu if (br->multicast_disabled == !val) 1781561f1103SHerbert Xu goto unlock; 1782561f1103SHerbert Xu 1783561f1103SHerbert Xu br->multicast_disabled = !val; 1784561f1103SHerbert Xu if (br->multicast_disabled) 1785561f1103SHerbert Xu goto unlock; 1786561f1103SHerbert Xu 17873a7fda06SHerbert Xu if (!netif_running(br->dev)) 17883a7fda06SHerbert Xu goto unlock; 17893a7fda06SHerbert Xu 1790e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1791e8051688SEric Dumazet if (mdb) { 1792e8051688SEric Dumazet if (mdb->old) { 1793561f1103SHerbert Xu err = -EEXIST; 1794561f1103SHerbert Xu rollback: 1795561f1103SHerbert Xu br->multicast_disabled = !!val; 1796561f1103SHerbert Xu goto unlock; 1797561f1103SHerbert Xu } 1798561f1103SHerbert Xu 1799e8051688SEric Dumazet err = br_mdb_rehash(&br->mdb, mdb->max, 1800561f1103SHerbert Xu br->hash_elasticity); 1801561f1103SHerbert Xu if (err) 1802561f1103SHerbert Xu goto rollback; 1803561f1103SHerbert Xu } 1804561f1103SHerbert Xu 1805561f1103SHerbert Xu br_multicast_open(br); 1806561f1103SHerbert Xu list_for_each_entry(port, &br->port_list, list) { 1807561f1103SHerbert Xu if (port->state == BR_STATE_DISABLED || 1808561f1103SHerbert Xu port->state == BR_STATE_BLOCKING) 1809561f1103SHerbert Xu continue; 1810561f1103SHerbert Xu 1811561f1103SHerbert Xu __br_multicast_enable_port(port); 1812561f1103SHerbert Xu } 1813561f1103SHerbert Xu 1814561f1103SHerbert Xu unlock: 1815ef5e0d82SAndrey Vagin spin_unlock_bh(&br->multicast_lock); 1816561f1103SHerbert Xu 1817561f1103SHerbert Xu return err; 1818561f1103SHerbert Xu } 1819b195167fSHerbert Xu 1820b195167fSHerbert Xu int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val) 1821b195167fSHerbert Xu { 1822b195167fSHerbert Xu int err = -ENOENT; 1823b195167fSHerbert Xu u32 old; 1824e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 1825b195167fSHerbert Xu 1826b195167fSHerbert Xu spin_lock(&br->multicast_lock); 1827b195167fSHerbert Xu if (!netif_running(br->dev)) 1828b195167fSHerbert Xu goto unlock; 1829b195167fSHerbert Xu 1830b195167fSHerbert Xu err = -EINVAL; 1831b195167fSHerbert Xu if (!is_power_of_2(val)) 1832b195167fSHerbert Xu goto unlock; 1833e8051688SEric Dumazet 1834e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1835e8051688SEric Dumazet if (mdb && val < mdb->size) 1836b195167fSHerbert Xu goto unlock; 1837b195167fSHerbert Xu 1838b195167fSHerbert Xu err = 0; 1839b195167fSHerbert Xu 1840b195167fSHerbert Xu old = br->hash_max; 1841b195167fSHerbert Xu br->hash_max = val; 1842b195167fSHerbert Xu 1843e8051688SEric Dumazet if (mdb) { 1844e8051688SEric Dumazet if (mdb->old) { 1845b195167fSHerbert Xu err = -EEXIST; 1846b195167fSHerbert Xu rollback: 1847b195167fSHerbert Xu br->hash_max = old; 1848b195167fSHerbert Xu goto unlock; 1849b195167fSHerbert Xu } 1850b195167fSHerbert Xu 1851b195167fSHerbert Xu err = br_mdb_rehash(&br->mdb, br->hash_max, 1852b195167fSHerbert Xu br->hash_elasticity); 1853b195167fSHerbert Xu if (err) 1854b195167fSHerbert Xu goto rollback; 1855b195167fSHerbert Xu } 1856b195167fSHerbert Xu 1857b195167fSHerbert Xu unlock: 1858b195167fSHerbert Xu spin_unlock(&br->multicast_lock); 1859b195167fSHerbert Xu 1860b195167fSHerbert Xu return err; 1861b195167fSHerbert Xu } 1862