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 39c83b8fabSHerbert Xu static void br_multicast_start_querier(struct net_bridge *br); 40c83b8fabSHerbert Xu 41dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 42e4de9f9eSLinus Lüssing static inline int ipv6_is_transient_multicast(const struct in6_addr *addr) 43eb1d1641SHerbert Xu { 44e4de9f9eSLinus Lüssing if (ipv6_addr_is_multicast(addr) && IPV6_ADDR_MC_FLAG_TRANSIENT(addr)) 4508b202b6SYOSHIFUJI Hideaki return 1; 4608b202b6SYOSHIFUJI Hideaki return 0; 4708b202b6SYOSHIFUJI Hideaki } 4808b202b6SYOSHIFUJI Hideaki #endif 4908b202b6SYOSHIFUJI Hideaki 508ef2a9a5SYOSHIFUJI Hideaki static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b) 518ef2a9a5SYOSHIFUJI Hideaki { 528ef2a9a5SYOSHIFUJI Hideaki if (a->proto != b->proto) 538ef2a9a5SYOSHIFUJI Hideaki return 0; 548ef2a9a5SYOSHIFUJI Hideaki switch (a->proto) { 558ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 568ef2a9a5SYOSHIFUJI Hideaki return a->u.ip4 == b->u.ip4; 57dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 5808b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 5908b202b6SYOSHIFUJI Hideaki return ipv6_addr_equal(&a->u.ip6, &b->u.ip6); 6008b202b6SYOSHIFUJI Hideaki #endif 618ef2a9a5SYOSHIFUJI Hideaki } 628ef2a9a5SYOSHIFUJI Hideaki return 0; 638ef2a9a5SYOSHIFUJI Hideaki } 648ef2a9a5SYOSHIFUJI Hideaki 658ef2a9a5SYOSHIFUJI Hideaki static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip) 66eb1d1641SHerbert Xu { 670eae88f3SEric Dumazet return jhash_1word(mdb->secret, (__force u32)ip) & (mdb->max - 1); 68eb1d1641SHerbert Xu } 69eb1d1641SHerbert Xu 70dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 7108b202b6SYOSHIFUJI Hideaki static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb, 7208b202b6SYOSHIFUJI Hideaki const struct in6_addr *ip) 7308b202b6SYOSHIFUJI Hideaki { 7408b202b6SYOSHIFUJI Hideaki return jhash2((__force u32 *)ip->s6_addr32, 4, mdb->secret) & (mdb->max - 1); 7508b202b6SYOSHIFUJI Hideaki } 7608b202b6SYOSHIFUJI Hideaki #endif 7708b202b6SYOSHIFUJI Hideaki 788ef2a9a5SYOSHIFUJI Hideaki static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb, 798ef2a9a5SYOSHIFUJI Hideaki struct br_ip *ip) 808ef2a9a5SYOSHIFUJI Hideaki { 818ef2a9a5SYOSHIFUJI Hideaki switch (ip->proto) { 828ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 838ef2a9a5SYOSHIFUJI Hideaki return __br_ip4_hash(mdb, ip->u.ip4); 84dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 8508b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 8608b202b6SYOSHIFUJI Hideaki return __br_ip6_hash(mdb, &ip->u.ip6); 8708b202b6SYOSHIFUJI Hideaki #endif 888ef2a9a5SYOSHIFUJI Hideaki } 898ef2a9a5SYOSHIFUJI Hideaki return 0; 90eb1d1641SHerbert Xu } 91eb1d1641SHerbert Xu 92eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *__br_mdb_ip_get( 938ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash) 94eb1d1641SHerbert Xu { 95eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 96eb1d1641SHerbert Xu struct hlist_node *p; 97eb1d1641SHerbert Xu 9849f5fcfdSHerbert Xu hlist_for_each_entry_rcu(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) { 998ef2a9a5SYOSHIFUJI Hideaki if (br_ip_equal(&mp->addr, dst)) 100eb1d1641SHerbert Xu return mp; 101eb1d1641SHerbert Xu } 102eb1d1641SHerbert Xu 103eb1d1641SHerbert Xu return NULL; 104eb1d1641SHerbert Xu } 105eb1d1641SHerbert Xu 1067f285fa7SHerbert Xu static struct net_bridge_mdb_entry *br_mdb_ip_get( 1077f285fa7SHerbert Xu struct net_bridge_mdb_htable *mdb, struct br_ip *dst) 1087f285fa7SHerbert Xu { 1097f285fa7SHerbert Xu if (!mdb) 1107f285fa7SHerbert Xu return NULL; 1117f285fa7SHerbert Xu 1127f285fa7SHerbert Xu return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst)); 1137f285fa7SHerbert Xu } 1147f285fa7SHerbert Xu 1158ef2a9a5SYOSHIFUJI Hideaki static struct net_bridge_mdb_entry *br_mdb_ip4_get( 116eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb, __be32 dst) 117eb1d1641SHerbert Xu { 1188ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_dst; 1190821ec55SHerbert Xu 1208ef2a9a5SYOSHIFUJI Hideaki br_dst.u.ip4 = dst; 1218ef2a9a5SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IP); 1228ef2a9a5SYOSHIFUJI Hideaki 1237f285fa7SHerbert Xu return br_mdb_ip_get(mdb, &br_dst); 1248ef2a9a5SYOSHIFUJI Hideaki } 1258ef2a9a5SYOSHIFUJI Hideaki 126dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 12708b202b6SYOSHIFUJI Hideaki static struct net_bridge_mdb_entry *br_mdb_ip6_get( 12808b202b6SYOSHIFUJI Hideaki struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst) 12908b202b6SYOSHIFUJI Hideaki { 13008b202b6SYOSHIFUJI Hideaki struct br_ip br_dst; 13108b202b6SYOSHIFUJI Hideaki 1324e3fd7a0SAlexey Dobriyan br_dst.u.ip6 = *dst; 13308b202b6SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IPV6); 13408b202b6SYOSHIFUJI Hideaki 1357f285fa7SHerbert Xu return br_mdb_ip_get(mdb, &br_dst); 13608b202b6SYOSHIFUJI Hideaki } 13708b202b6SYOSHIFUJI Hideaki #endif 13808b202b6SYOSHIFUJI Hideaki 139eb1d1641SHerbert Xu struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br, 140eb1d1641SHerbert Xu struct sk_buff *skb) 141eb1d1641SHerbert Xu { 142e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb); 1438ef2a9a5SYOSHIFUJI Hideaki struct br_ip ip; 1448ef2a9a5SYOSHIFUJI Hideaki 1457f285fa7SHerbert Xu if (br->multicast_disabled) 146eb1d1641SHerbert Xu return NULL; 147eb1d1641SHerbert Xu 1488ef2a9a5SYOSHIFUJI Hideaki if (BR_INPUT_SKB_CB(skb)->igmp) 1498ef2a9a5SYOSHIFUJI Hideaki return NULL; 1508ef2a9a5SYOSHIFUJI Hideaki 1518ef2a9a5SYOSHIFUJI Hideaki ip.proto = skb->protocol; 1528ef2a9a5SYOSHIFUJI Hideaki 153eb1d1641SHerbert Xu switch (skb->protocol) { 154eb1d1641SHerbert Xu case htons(ETH_P_IP): 1558ef2a9a5SYOSHIFUJI Hideaki ip.u.ip4 = ip_hdr(skb)->daddr; 156eb1d1641SHerbert Xu break; 157dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 15808b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 1594e3fd7a0SAlexey Dobriyan ip.u.ip6 = ipv6_hdr(skb)->daddr; 16008b202b6SYOSHIFUJI Hideaki break; 16108b202b6SYOSHIFUJI Hideaki #endif 1628ef2a9a5SYOSHIFUJI Hideaki default: 1638ef2a9a5SYOSHIFUJI Hideaki return NULL; 164eb1d1641SHerbert Xu } 165eb1d1641SHerbert Xu 1668ef2a9a5SYOSHIFUJI Hideaki return br_mdb_ip_get(mdb, &ip); 167eb1d1641SHerbert Xu } 168eb1d1641SHerbert Xu 169eb1d1641SHerbert Xu static void br_mdb_free(struct rcu_head *head) 170eb1d1641SHerbert Xu { 171eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb = 172eb1d1641SHerbert Xu container_of(head, struct net_bridge_mdb_htable, rcu); 173eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old = mdb->old; 174eb1d1641SHerbert Xu 175eb1d1641SHerbert Xu mdb->old = NULL; 176eb1d1641SHerbert Xu kfree(old->mhash); 177eb1d1641SHerbert Xu kfree(old); 178eb1d1641SHerbert Xu } 179eb1d1641SHerbert Xu 180eb1d1641SHerbert Xu static int br_mdb_copy(struct net_bridge_mdb_htable *new, 181eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old, 182eb1d1641SHerbert Xu int elasticity) 183eb1d1641SHerbert Xu { 184eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 185eb1d1641SHerbert Xu struct hlist_node *p; 186eb1d1641SHerbert Xu int maxlen; 187eb1d1641SHerbert Xu int len; 188eb1d1641SHerbert Xu int i; 189eb1d1641SHerbert Xu 190eb1d1641SHerbert Xu for (i = 0; i < old->max; i++) 191eb1d1641SHerbert Xu hlist_for_each_entry(mp, p, &old->mhash[i], hlist[old->ver]) 192eb1d1641SHerbert Xu hlist_add_head(&mp->hlist[new->ver], 1938ef2a9a5SYOSHIFUJI Hideaki &new->mhash[br_ip_hash(new, &mp->addr)]); 194eb1d1641SHerbert Xu 195eb1d1641SHerbert Xu if (!elasticity) 196eb1d1641SHerbert Xu return 0; 197eb1d1641SHerbert Xu 198eb1d1641SHerbert Xu maxlen = 0; 199eb1d1641SHerbert Xu for (i = 0; i < new->max; i++) { 200eb1d1641SHerbert Xu len = 0; 201eb1d1641SHerbert Xu hlist_for_each_entry(mp, p, &new->mhash[i], hlist[new->ver]) 202eb1d1641SHerbert Xu len++; 203eb1d1641SHerbert Xu if (len > maxlen) 204eb1d1641SHerbert Xu maxlen = len; 205eb1d1641SHerbert Xu } 206eb1d1641SHerbert Xu 207eb1d1641SHerbert Xu return maxlen > elasticity ? -EINVAL : 0; 208eb1d1641SHerbert Xu } 209eb1d1641SHerbert Xu 210eb1d1641SHerbert Xu static void br_multicast_free_pg(struct rcu_head *head) 211eb1d1641SHerbert Xu { 212eb1d1641SHerbert Xu struct net_bridge_port_group *p = 213eb1d1641SHerbert Xu container_of(head, struct net_bridge_port_group, rcu); 214eb1d1641SHerbert Xu 215eb1d1641SHerbert Xu kfree(p); 216eb1d1641SHerbert Xu } 217eb1d1641SHerbert Xu 218eb1d1641SHerbert Xu static void br_multicast_free_group(struct rcu_head *head) 219eb1d1641SHerbert Xu { 220eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = 221eb1d1641SHerbert Xu container_of(head, struct net_bridge_mdb_entry, rcu); 222eb1d1641SHerbert Xu 223eb1d1641SHerbert Xu kfree(mp); 224eb1d1641SHerbert Xu } 225eb1d1641SHerbert Xu 226eb1d1641SHerbert Xu static void br_multicast_group_expired(unsigned long data) 227eb1d1641SHerbert Xu { 228eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = (void *)data; 229eb1d1641SHerbert Xu struct net_bridge *br = mp->br; 230eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 231eb1d1641SHerbert Xu 232eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 233eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&mp->timer)) 234eb1d1641SHerbert Xu goto out; 235eb1d1641SHerbert Xu 2368a870178SHerbert Xu mp->mglist = false; 237eb1d1641SHerbert Xu 238eb1d1641SHerbert Xu if (mp->ports) 239eb1d1641SHerbert Xu goto out; 240eb1d1641SHerbert Xu 241e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 242e8051688SEric Dumazet 243eb1d1641SHerbert Xu hlist_del_rcu(&mp->hlist[mdb->ver]); 244eb1d1641SHerbert Xu mdb->size--; 245eb1d1641SHerbert Xu 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 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)); 449d1d81d4cSUlrich Weber if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0, 450d1d81d4cSUlrich Weber &ip6h->saddr)) { 451d1d81d4cSUlrich Weber kfree_skb(skb); 452d1d81d4cSUlrich Weber return NULL; 453d1d81d4cSUlrich 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 struct net_bridge_mdb_entry *br_multicast_get_group( 5118ef2a9a5SYOSHIFUJI Hideaki struct net_bridge *br, struct net_bridge_port *port, 5128ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group, int hash) 513eb1d1641SHerbert Xu { 514e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 515eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 516eb1d1641SHerbert Xu struct hlist_node *p; 51795c96174SEric Dumazet unsigned int count = 0; 51895c96174SEric Dumazet unsigned int max; 519eb1d1641SHerbert Xu int elasticity; 520eb1d1641SHerbert Xu int err; 521eb1d1641SHerbert Xu 522e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 523eb1d1641SHerbert Xu hlist_for_each_entry(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) { 524eb1d1641SHerbert Xu count++; 5258ef2a9a5SYOSHIFUJI Hideaki if (unlikely(br_ip_equal(group, &mp->addr))) 526eb1d1641SHerbert Xu return mp; 527eb1d1641SHerbert Xu } 528eb1d1641SHerbert Xu 529eb1d1641SHerbert Xu elasticity = 0; 530eb1d1641SHerbert Xu max = mdb->max; 531eb1d1641SHerbert Xu 532eb1d1641SHerbert Xu if (unlikely(count > br->hash_elasticity && count)) { 533eb1d1641SHerbert Xu if (net_ratelimit()) 53428a16c97Sstephen hemminger br_info(br, "Multicast hash table " 535eb1d1641SHerbert Xu "chain limit reached: %s\n", 53628a16c97Sstephen hemminger port ? port->dev->name : br->dev->name); 537eb1d1641SHerbert Xu 538eb1d1641SHerbert Xu elasticity = br->hash_elasticity; 539eb1d1641SHerbert Xu } 540eb1d1641SHerbert Xu 541eb1d1641SHerbert Xu if (mdb->size >= max) { 542eb1d1641SHerbert Xu max *= 2; 543eb1d1641SHerbert Xu if (unlikely(max >= br->hash_max)) { 54428a16c97Sstephen hemminger br_warn(br, "Multicast hash table maximum " 545eb1d1641SHerbert Xu "reached, disabling snooping: %s, %d\n", 54628a16c97Sstephen hemminger port ? port->dev->name : br->dev->name, max); 547eb1d1641SHerbert Xu err = -E2BIG; 548eb1d1641SHerbert Xu disable: 549eb1d1641SHerbert Xu br->multicast_disabled = 1; 550eb1d1641SHerbert Xu goto err; 551eb1d1641SHerbert Xu } 552eb1d1641SHerbert Xu } 553eb1d1641SHerbert Xu 554eb1d1641SHerbert Xu if (max > mdb->max || elasticity) { 555eb1d1641SHerbert Xu if (mdb->old) { 556eb1d1641SHerbert Xu if (net_ratelimit()) 55728a16c97Sstephen hemminger br_info(br, "Multicast hash table " 558eb1d1641SHerbert Xu "on fire: %s\n", 55928a16c97Sstephen hemminger port ? port->dev->name : br->dev->name); 560eb1d1641SHerbert Xu err = -EEXIST; 561eb1d1641SHerbert Xu goto err; 562eb1d1641SHerbert Xu } 563eb1d1641SHerbert Xu 564eb1d1641SHerbert Xu err = br_mdb_rehash(&br->mdb, max, elasticity); 565eb1d1641SHerbert Xu if (err) { 56628a16c97Sstephen hemminger br_warn(br, "Cannot rehash multicast " 56728a16c97Sstephen hemminger "hash table, disabling snooping: %s, %d, %d\n", 56828a16c97Sstephen hemminger port ? port->dev->name : br->dev->name, 569eb1d1641SHerbert Xu mdb->size, err); 570eb1d1641SHerbert Xu goto disable; 571eb1d1641SHerbert Xu } 572eb1d1641SHerbert Xu 573eb1d1641SHerbert Xu err = -EAGAIN; 574eb1d1641SHerbert Xu goto err; 575eb1d1641SHerbert Xu } 576eb1d1641SHerbert Xu 577eb1d1641SHerbert Xu return NULL; 578eb1d1641SHerbert Xu 579eb1d1641SHerbert Xu err: 580eb1d1641SHerbert Xu mp = ERR_PTR(err); 581eb1d1641SHerbert Xu return mp; 582eb1d1641SHerbert Xu } 583eb1d1641SHerbert Xu 584eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *br_multicast_new_group( 5858ef2a9a5SYOSHIFUJI Hideaki struct net_bridge *br, struct net_bridge_port *port, 5868ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group) 587eb1d1641SHerbert Xu { 588e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 589eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 590eb1d1641SHerbert Xu int hash; 5914c0833bcSTobias Klauser int err; 592eb1d1641SHerbert Xu 593e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 594eb1d1641SHerbert Xu if (!mdb) { 5954c0833bcSTobias Klauser err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0); 5964c0833bcSTobias Klauser if (err) 5974c0833bcSTobias Klauser return ERR_PTR(err); 598eb1d1641SHerbert Xu goto rehash; 599eb1d1641SHerbert Xu } 600eb1d1641SHerbert Xu 601eb1d1641SHerbert Xu hash = br_ip_hash(mdb, group); 602eb1d1641SHerbert Xu mp = br_multicast_get_group(br, port, group, hash); 603eb1d1641SHerbert Xu switch (PTR_ERR(mp)) { 604eb1d1641SHerbert Xu case 0: 605eb1d1641SHerbert Xu break; 606eb1d1641SHerbert Xu 607eb1d1641SHerbert Xu case -EAGAIN: 608eb1d1641SHerbert Xu rehash: 609e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 610eb1d1641SHerbert Xu hash = br_ip_hash(mdb, group); 611eb1d1641SHerbert Xu break; 612eb1d1641SHerbert Xu 613eb1d1641SHerbert Xu default: 614eb1d1641SHerbert Xu goto out; 615eb1d1641SHerbert Xu } 616eb1d1641SHerbert Xu 617eb1d1641SHerbert Xu mp = kzalloc(sizeof(*mp), GFP_ATOMIC); 618eb1d1641SHerbert Xu if (unlikely(!mp)) 6194c0833bcSTobias Klauser return ERR_PTR(-ENOMEM); 620eb1d1641SHerbert Xu 621eb1d1641SHerbert Xu mp->br = br; 6228ef2a9a5SYOSHIFUJI Hideaki mp->addr = *group; 623eb1d1641SHerbert Xu setup_timer(&mp->timer, br_multicast_group_expired, 624eb1d1641SHerbert Xu (unsigned long)mp); 625eb1d1641SHerbert Xu 626eb1d1641SHerbert Xu hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]); 627eb1d1641SHerbert Xu mdb->size++; 628eb1d1641SHerbert Xu 629eb1d1641SHerbert Xu out: 630eb1d1641SHerbert Xu return mp; 631eb1d1641SHerbert Xu } 632eb1d1641SHerbert Xu 633eb1d1641SHerbert Xu static int br_multicast_add_group(struct net_bridge *br, 6348ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 6358ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group) 636eb1d1641SHerbert Xu { 637eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 638eb1d1641SHerbert Xu struct net_bridge_port_group *p; 639e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 640eb1d1641SHerbert Xu unsigned long now = jiffies; 641eb1d1641SHerbert Xu int err; 642eb1d1641SHerbert Xu 643eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 644eb1d1641SHerbert Xu if (!netif_running(br->dev) || 645eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 646eb1d1641SHerbert Xu goto out; 647eb1d1641SHerbert Xu 648eb1d1641SHerbert Xu mp = br_multicast_new_group(br, port, group); 649eb1d1641SHerbert Xu err = PTR_ERR(mp); 6504c0833bcSTobias Klauser if (IS_ERR(mp)) 651eb1d1641SHerbert Xu goto err; 652eb1d1641SHerbert Xu 653eb1d1641SHerbert Xu if (!port) { 6548a870178SHerbert Xu mp->mglist = true; 655eb1d1641SHerbert Xu mod_timer(&mp->timer, now + br->multicast_membership_interval); 656eb1d1641SHerbert Xu goto out; 657eb1d1641SHerbert Xu } 658eb1d1641SHerbert Xu 659e8051688SEric Dumazet for (pp = &mp->ports; 660e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 661e8051688SEric Dumazet pp = &p->next) { 662eb1d1641SHerbert Xu if (p->port == port) 663eb1d1641SHerbert Xu goto found; 664eb1d1641SHerbert Xu if ((unsigned long)p->port < (unsigned long)port) 665eb1d1641SHerbert Xu break; 666eb1d1641SHerbert Xu } 667eb1d1641SHerbert Xu 668eb1d1641SHerbert Xu p = kzalloc(sizeof(*p), GFP_ATOMIC); 669eb1d1641SHerbert Xu err = -ENOMEM; 670eb1d1641SHerbert Xu if (unlikely(!p)) 671eb1d1641SHerbert Xu goto err; 672eb1d1641SHerbert Xu 6738ef2a9a5SYOSHIFUJI Hideaki p->addr = *group; 674eb1d1641SHerbert Xu p->port = port; 675eb1d1641SHerbert Xu p->next = *pp; 676eb1d1641SHerbert Xu hlist_add_head(&p->mglist, &port->mglist); 677eb1d1641SHerbert Xu setup_timer(&p->timer, br_multicast_port_group_expired, 678eb1d1641SHerbert Xu (unsigned long)p); 679eb1d1641SHerbert Xu 680eb1d1641SHerbert Xu rcu_assign_pointer(*pp, p); 681eb1d1641SHerbert Xu 682eb1d1641SHerbert Xu found: 683eb1d1641SHerbert Xu mod_timer(&p->timer, now + br->multicast_membership_interval); 684eb1d1641SHerbert Xu out: 685eb1d1641SHerbert Xu err = 0; 686eb1d1641SHerbert Xu 687eb1d1641SHerbert Xu err: 688eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 689eb1d1641SHerbert Xu return err; 690eb1d1641SHerbert Xu } 691eb1d1641SHerbert Xu 6928ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_add_group(struct net_bridge *br, 6938ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 6948ef2a9a5SYOSHIFUJI Hideaki __be32 group) 6958ef2a9a5SYOSHIFUJI Hideaki { 6968ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 6978ef2a9a5SYOSHIFUJI Hideaki 6988ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 6998ef2a9a5SYOSHIFUJI Hideaki return 0; 7008ef2a9a5SYOSHIFUJI Hideaki 7018ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 7028ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 7038ef2a9a5SYOSHIFUJI Hideaki 7048ef2a9a5SYOSHIFUJI Hideaki return br_multicast_add_group(br, port, &br_group); 7058ef2a9a5SYOSHIFUJI Hideaki } 7068ef2a9a5SYOSHIFUJI Hideaki 707dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 70808b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_add_group(struct net_bridge *br, 70908b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 71008b202b6SYOSHIFUJI Hideaki const struct in6_addr *group) 71108b202b6SYOSHIFUJI Hideaki { 71208b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 71308b202b6SYOSHIFUJI Hideaki 714e4de9f9eSLinus Lüssing if (!ipv6_is_transient_multicast(group)) 71508b202b6SYOSHIFUJI Hideaki return 0; 71608b202b6SYOSHIFUJI Hideaki 7174e3fd7a0SAlexey Dobriyan br_group.u.ip6 = *group; 7189cc6e0c4SLinus Lüssing br_group.proto = htons(ETH_P_IPV6); 71908b202b6SYOSHIFUJI Hideaki 72008b202b6SYOSHIFUJI Hideaki return br_multicast_add_group(br, port, &br_group); 72108b202b6SYOSHIFUJI Hideaki } 72208b202b6SYOSHIFUJI Hideaki #endif 72308b202b6SYOSHIFUJI Hideaki 724eb1d1641SHerbert Xu static void br_multicast_router_expired(unsigned long data) 725eb1d1641SHerbert Xu { 726eb1d1641SHerbert Xu struct net_bridge_port *port = (void *)data; 727eb1d1641SHerbert Xu struct net_bridge *br = port->br; 728eb1d1641SHerbert Xu 729eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 730eb1d1641SHerbert Xu if (port->multicast_router != 1 || 731eb1d1641SHerbert Xu timer_pending(&port->multicast_router_timer) || 732eb1d1641SHerbert Xu hlist_unhashed(&port->rlist)) 733eb1d1641SHerbert Xu goto out; 734eb1d1641SHerbert Xu 735eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 736eb1d1641SHerbert Xu 737eb1d1641SHerbert Xu out: 738eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 739eb1d1641SHerbert Xu } 740eb1d1641SHerbert Xu 741eb1d1641SHerbert Xu static void br_multicast_local_router_expired(unsigned long data) 742eb1d1641SHerbert Xu { 743eb1d1641SHerbert Xu } 744eb1d1641SHerbert Xu 745c83b8fabSHerbert Xu static void br_multicast_querier_expired(unsigned long data) 746c83b8fabSHerbert Xu { 747*bb63f1f8SHerbert Xu struct net_bridge *br = (void *)data; 748c83b8fabSHerbert Xu 749c83b8fabSHerbert Xu spin_lock(&br->multicast_lock); 750c83b8fabSHerbert Xu if (!netif_running(br->dev) || br->multicast_disabled) 751c83b8fabSHerbert Xu goto out; 752c83b8fabSHerbert Xu 753c83b8fabSHerbert Xu br_multicast_start_querier(br); 754c83b8fabSHerbert Xu 755c83b8fabSHerbert Xu out: 756c83b8fabSHerbert Xu spin_unlock(&br->multicast_lock); 757c83b8fabSHerbert Xu } 758c83b8fabSHerbert Xu 7598ef2a9a5SYOSHIFUJI Hideaki static void __br_multicast_send_query(struct net_bridge *br, 7608ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 7618ef2a9a5SYOSHIFUJI Hideaki struct br_ip *ip) 762eb1d1641SHerbert Xu { 763eb1d1641SHerbert Xu struct sk_buff *skb; 764eb1d1641SHerbert Xu 7658ef2a9a5SYOSHIFUJI Hideaki skb = br_multicast_alloc_query(br, ip); 766eb1d1641SHerbert Xu if (!skb) 7678ef2a9a5SYOSHIFUJI Hideaki return; 768eb1d1641SHerbert Xu 769eb1d1641SHerbert Xu if (port) { 770eb1d1641SHerbert Xu __skb_push(skb, sizeof(struct ethhdr)); 771eb1d1641SHerbert Xu skb->dev = port->dev; 772713aefa3SJan Engelhardt NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev, 773eb1d1641SHerbert Xu dev_queue_xmit); 774eb1d1641SHerbert Xu } else 775eb1d1641SHerbert Xu netif_rx(skb); 7768ef2a9a5SYOSHIFUJI Hideaki } 777eb1d1641SHerbert Xu 7788ef2a9a5SYOSHIFUJI Hideaki static void br_multicast_send_query(struct net_bridge *br, 7798ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, u32 sent) 7808ef2a9a5SYOSHIFUJI Hideaki { 7818ef2a9a5SYOSHIFUJI Hideaki unsigned long time; 7828ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 7838ef2a9a5SYOSHIFUJI Hideaki 7848ef2a9a5SYOSHIFUJI Hideaki if (!netif_running(br->dev) || br->multicast_disabled || 785c5c23260SHerbert Xu !br->multicast_querier || 7868ef2a9a5SYOSHIFUJI Hideaki timer_pending(&br->multicast_querier_timer)) 7878ef2a9a5SYOSHIFUJI Hideaki return; 7888ef2a9a5SYOSHIFUJI Hideaki 78908b202b6SYOSHIFUJI Hideaki memset(&br_group.u, 0, sizeof(br_group.u)); 7908ef2a9a5SYOSHIFUJI Hideaki 79108b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 7928ef2a9a5SYOSHIFUJI Hideaki __br_multicast_send_query(br, port, &br_group); 7938ef2a9a5SYOSHIFUJI Hideaki 794dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 79508b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 79608b202b6SYOSHIFUJI Hideaki __br_multicast_send_query(br, port, &br_group); 79708b202b6SYOSHIFUJI Hideaki #endif 79808b202b6SYOSHIFUJI Hideaki 799eb1d1641SHerbert Xu time = jiffies; 800eb1d1641SHerbert Xu time += sent < br->multicast_startup_query_count ? 801eb1d1641SHerbert Xu br->multicast_startup_query_interval : 802eb1d1641SHerbert Xu br->multicast_query_interval; 803eb1d1641SHerbert Xu mod_timer(port ? &port->multicast_query_timer : 804eb1d1641SHerbert Xu &br->multicast_query_timer, time); 805eb1d1641SHerbert Xu } 806eb1d1641SHerbert Xu 807eb1d1641SHerbert Xu static void br_multicast_port_query_expired(unsigned long data) 808eb1d1641SHerbert Xu { 809eb1d1641SHerbert Xu struct net_bridge_port *port = (void *)data; 810eb1d1641SHerbert Xu struct net_bridge *br = port->br; 811eb1d1641SHerbert Xu 812eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 81302a780c0SDan Carpenter if (port->state == BR_STATE_DISABLED || 81402a780c0SDan Carpenter port->state == BR_STATE_BLOCKING) 815eb1d1641SHerbert Xu goto out; 816eb1d1641SHerbert Xu 817eb1d1641SHerbert Xu if (port->multicast_startup_queries_sent < 818eb1d1641SHerbert Xu br->multicast_startup_query_count) 819eb1d1641SHerbert Xu port->multicast_startup_queries_sent++; 820eb1d1641SHerbert Xu 821eb1d1641SHerbert Xu br_multicast_send_query(port->br, port, 822eb1d1641SHerbert Xu port->multicast_startup_queries_sent); 823eb1d1641SHerbert Xu 824eb1d1641SHerbert Xu out: 825eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 826eb1d1641SHerbert Xu } 827eb1d1641SHerbert Xu 828eb1d1641SHerbert Xu void br_multicast_add_port(struct net_bridge_port *port) 829eb1d1641SHerbert Xu { 830eb1d1641SHerbert Xu port->multicast_router = 1; 831eb1d1641SHerbert Xu 832eb1d1641SHerbert Xu setup_timer(&port->multicast_router_timer, br_multicast_router_expired, 833eb1d1641SHerbert Xu (unsigned long)port); 834eb1d1641SHerbert Xu setup_timer(&port->multicast_query_timer, 835eb1d1641SHerbert Xu br_multicast_port_query_expired, (unsigned long)port); 836eb1d1641SHerbert Xu } 837eb1d1641SHerbert Xu 838eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port) 839eb1d1641SHerbert Xu { 840eb1d1641SHerbert Xu del_timer_sync(&port->multicast_router_timer); 841eb1d1641SHerbert Xu } 842eb1d1641SHerbert Xu 843561f1103SHerbert Xu static void __br_multicast_enable_port(struct net_bridge_port *port) 844561f1103SHerbert Xu { 845561f1103SHerbert Xu port->multicast_startup_queries_sent = 0; 846561f1103SHerbert Xu 847561f1103SHerbert Xu if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 || 848561f1103SHerbert Xu del_timer(&port->multicast_query_timer)) 849561f1103SHerbert Xu mod_timer(&port->multicast_query_timer, jiffies); 850561f1103SHerbert Xu } 851561f1103SHerbert Xu 852eb1d1641SHerbert Xu void br_multicast_enable_port(struct net_bridge_port *port) 853eb1d1641SHerbert Xu { 854eb1d1641SHerbert Xu struct net_bridge *br = port->br; 855eb1d1641SHerbert Xu 856eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 857eb1d1641SHerbert Xu if (br->multicast_disabled || !netif_running(br->dev)) 858eb1d1641SHerbert Xu goto out; 859eb1d1641SHerbert Xu 860561f1103SHerbert Xu __br_multicast_enable_port(port); 861eb1d1641SHerbert Xu 862eb1d1641SHerbert Xu out: 863eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 864eb1d1641SHerbert Xu } 865eb1d1641SHerbert Xu 866eb1d1641SHerbert Xu void br_multicast_disable_port(struct net_bridge_port *port) 867eb1d1641SHerbert Xu { 868eb1d1641SHerbert Xu struct net_bridge *br = port->br; 869eb1d1641SHerbert Xu struct net_bridge_port_group *pg; 870eb1d1641SHerbert Xu struct hlist_node *p, *n; 871eb1d1641SHerbert Xu 872eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 873eb1d1641SHerbert Xu hlist_for_each_entry_safe(pg, p, n, &port->mglist, mglist) 874eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 875eb1d1641SHerbert Xu 876eb1d1641SHerbert Xu if (!hlist_unhashed(&port->rlist)) 877eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 878eb1d1641SHerbert Xu del_timer(&port->multicast_router_timer); 879eb1d1641SHerbert Xu del_timer(&port->multicast_query_timer); 880eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 881eb1d1641SHerbert Xu } 882eb1d1641SHerbert Xu 8838ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_igmp3_report(struct net_bridge *br, 884eb1d1641SHerbert Xu struct net_bridge_port *port, 885eb1d1641SHerbert Xu struct sk_buff *skb) 886eb1d1641SHerbert Xu { 887eb1d1641SHerbert Xu struct igmpv3_report *ih; 888eb1d1641SHerbert Xu struct igmpv3_grec *grec; 889eb1d1641SHerbert Xu int i; 890eb1d1641SHerbert Xu int len; 891eb1d1641SHerbert Xu int num; 892eb1d1641SHerbert Xu int type; 893eb1d1641SHerbert Xu int err = 0; 894eb1d1641SHerbert Xu __be32 group; 895eb1d1641SHerbert Xu 896eb1d1641SHerbert Xu if (!pskb_may_pull(skb, sizeof(*ih))) 897eb1d1641SHerbert Xu return -EINVAL; 898eb1d1641SHerbert Xu 899eb1d1641SHerbert Xu ih = igmpv3_report_hdr(skb); 900eb1d1641SHerbert Xu num = ntohs(ih->ngrec); 901eb1d1641SHerbert Xu len = sizeof(*ih); 902eb1d1641SHerbert Xu 903eb1d1641SHerbert Xu for (i = 0; i < num; i++) { 904eb1d1641SHerbert Xu len += sizeof(*grec); 905eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 906eb1d1641SHerbert Xu return -EINVAL; 907eb1d1641SHerbert Xu 908fd218cf9SHerbert Xu grec = (void *)(skb->data + len - sizeof(*grec)); 909eb1d1641SHerbert Xu group = grec->grec_mca; 910eb1d1641SHerbert Xu type = grec->grec_type; 911eb1d1641SHerbert Xu 9128eabf95cSEric Dumazet len += ntohs(grec->grec_nsrcs) * 4; 913eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 914eb1d1641SHerbert Xu return -EINVAL; 915eb1d1641SHerbert Xu 916eb1d1641SHerbert Xu /* We treat this as an IGMPv2 report for now. */ 917eb1d1641SHerbert Xu switch (type) { 918eb1d1641SHerbert Xu case IGMPV3_MODE_IS_INCLUDE: 919eb1d1641SHerbert Xu case IGMPV3_MODE_IS_EXCLUDE: 920eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_INCLUDE: 921eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_EXCLUDE: 922eb1d1641SHerbert Xu case IGMPV3_ALLOW_NEW_SOURCES: 923eb1d1641SHerbert Xu case IGMPV3_BLOCK_OLD_SOURCES: 924eb1d1641SHerbert Xu break; 925eb1d1641SHerbert Xu 926eb1d1641SHerbert Xu default: 927eb1d1641SHerbert Xu continue; 928eb1d1641SHerbert Xu } 929eb1d1641SHerbert Xu 9308ef2a9a5SYOSHIFUJI Hideaki err = br_ip4_multicast_add_group(br, port, group); 931eb1d1641SHerbert Xu if (err) 932eb1d1641SHerbert Xu break; 933eb1d1641SHerbert Xu } 934eb1d1641SHerbert Xu 935eb1d1641SHerbert Xu return err; 936eb1d1641SHerbert Xu } 937eb1d1641SHerbert Xu 938dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 93908b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_mld2_report(struct net_bridge *br, 94008b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 94108b202b6SYOSHIFUJI Hideaki struct sk_buff *skb) 94208b202b6SYOSHIFUJI Hideaki { 94308b202b6SYOSHIFUJI Hideaki struct icmp6hdr *icmp6h; 94408b202b6SYOSHIFUJI Hideaki struct mld2_grec *grec; 94508b202b6SYOSHIFUJI Hideaki int i; 94608b202b6SYOSHIFUJI Hideaki int len; 94708b202b6SYOSHIFUJI Hideaki int num; 94808b202b6SYOSHIFUJI Hideaki int err = 0; 94908b202b6SYOSHIFUJI Hideaki 95008b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*icmp6h))) 95108b202b6SYOSHIFUJI Hideaki return -EINVAL; 95208b202b6SYOSHIFUJI Hideaki 95308b202b6SYOSHIFUJI Hideaki icmp6h = icmp6_hdr(skb); 95408b202b6SYOSHIFUJI Hideaki num = ntohs(icmp6h->icmp6_dataun.un_data16[1]); 95508b202b6SYOSHIFUJI Hideaki len = sizeof(*icmp6h); 95608b202b6SYOSHIFUJI Hideaki 95708b202b6SYOSHIFUJI Hideaki for (i = 0; i < num; i++) { 95808b202b6SYOSHIFUJI Hideaki __be16 *nsrcs, _nsrcs; 95908b202b6SYOSHIFUJI Hideaki 96008b202b6SYOSHIFUJI Hideaki nsrcs = skb_header_pointer(skb, 96108b202b6SYOSHIFUJI Hideaki len + offsetof(struct mld2_grec, 962649e984dSLinus Lüssing grec_nsrcs), 96308b202b6SYOSHIFUJI Hideaki sizeof(_nsrcs), &_nsrcs); 96408b202b6SYOSHIFUJI Hideaki if (!nsrcs) 96508b202b6SYOSHIFUJI Hideaki return -EINVAL; 96608b202b6SYOSHIFUJI Hideaki 96708b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, 96808b202b6SYOSHIFUJI Hideaki len + sizeof(*grec) + 969d41db9f3SLinus Lüssing sizeof(struct in6_addr) * ntohs(*nsrcs))) 97008b202b6SYOSHIFUJI Hideaki return -EINVAL; 97108b202b6SYOSHIFUJI Hideaki 97208b202b6SYOSHIFUJI Hideaki grec = (struct mld2_grec *)(skb->data + len); 973d41db9f3SLinus Lüssing len += sizeof(*grec) + 974d41db9f3SLinus Lüssing sizeof(struct in6_addr) * ntohs(*nsrcs); 97508b202b6SYOSHIFUJI Hideaki 97608b202b6SYOSHIFUJI Hideaki /* We treat these as MLDv1 reports for now. */ 97708b202b6SYOSHIFUJI Hideaki switch (grec->grec_type) { 97808b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_INCLUDE: 97908b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_EXCLUDE: 98008b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_INCLUDE: 98108b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_EXCLUDE: 98208b202b6SYOSHIFUJI Hideaki case MLD2_ALLOW_NEW_SOURCES: 98308b202b6SYOSHIFUJI Hideaki case MLD2_BLOCK_OLD_SOURCES: 98408b202b6SYOSHIFUJI Hideaki break; 98508b202b6SYOSHIFUJI Hideaki 98608b202b6SYOSHIFUJI Hideaki default: 98708b202b6SYOSHIFUJI Hideaki continue; 98808b202b6SYOSHIFUJI Hideaki } 98908b202b6SYOSHIFUJI Hideaki 99008b202b6SYOSHIFUJI Hideaki err = br_ip6_multicast_add_group(br, port, &grec->grec_mca); 99108b202b6SYOSHIFUJI Hideaki if (!err) 99208b202b6SYOSHIFUJI Hideaki break; 99308b202b6SYOSHIFUJI Hideaki } 99408b202b6SYOSHIFUJI Hideaki 99508b202b6SYOSHIFUJI Hideaki return err; 99608b202b6SYOSHIFUJI Hideaki } 99708b202b6SYOSHIFUJI Hideaki #endif 99808b202b6SYOSHIFUJI Hideaki 9997e80c124Sstephen hemminger /* 10007e80c124Sstephen hemminger * Add port to rotuer_list 10017e80c124Sstephen hemminger * list is maintained ordered by pointer value 10027e80c124Sstephen hemminger * and locked by br->multicast_lock and RCU 10037e80c124Sstephen hemminger */ 10040909e117SHerbert Xu static void br_multicast_add_router(struct net_bridge *br, 10050909e117SHerbert Xu struct net_bridge_port *port) 10060909e117SHerbert Xu { 1007dcdca2c4Sstephen hemminger struct net_bridge_port *p; 10087e80c124Sstephen hemminger struct hlist_node *n, *slot = NULL; 10090909e117SHerbert Xu 1010709b9326SDavid S. Miller hlist_for_each_entry(p, n, &br->router_list, rlist) { 10117e80c124Sstephen hemminger if ((unsigned long) port >= (unsigned long) p) 10127e80c124Sstephen hemminger break; 10137e80c124Sstephen hemminger slot = n; 1014dcdca2c4Sstephen hemminger } 10150909e117SHerbert Xu 10167e80c124Sstephen hemminger if (slot) 10177e80c124Sstephen hemminger hlist_add_after_rcu(slot, &port->rlist); 1018dcdca2c4Sstephen hemminger else 1019dcdca2c4Sstephen hemminger hlist_add_head_rcu(&port->rlist, &br->router_list); 10200909e117SHerbert Xu } 10210909e117SHerbert Xu 1022eb1d1641SHerbert Xu static void br_multicast_mark_router(struct net_bridge *br, 1023eb1d1641SHerbert Xu struct net_bridge_port *port) 1024eb1d1641SHerbert Xu { 1025eb1d1641SHerbert Xu unsigned long now = jiffies; 1026eb1d1641SHerbert Xu 1027eb1d1641SHerbert Xu if (!port) { 1028eb1d1641SHerbert Xu if (br->multicast_router == 1) 1029eb1d1641SHerbert Xu mod_timer(&br->multicast_router_timer, 1030eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1031eb1d1641SHerbert Xu return; 1032eb1d1641SHerbert Xu } 1033eb1d1641SHerbert Xu 1034eb1d1641SHerbert Xu if (port->multicast_router != 1) 1035eb1d1641SHerbert Xu return; 1036eb1d1641SHerbert Xu 1037eb1d1641SHerbert Xu if (!hlist_unhashed(&port->rlist)) 1038eb1d1641SHerbert Xu goto timer; 1039eb1d1641SHerbert Xu 10400909e117SHerbert Xu br_multicast_add_router(br, port); 1041eb1d1641SHerbert Xu 1042eb1d1641SHerbert Xu timer: 1043eb1d1641SHerbert Xu mod_timer(&port->multicast_router_timer, 1044eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1045eb1d1641SHerbert Xu } 1046eb1d1641SHerbert Xu 1047eb1d1641SHerbert Xu static void br_multicast_query_received(struct net_bridge *br, 1048eb1d1641SHerbert Xu struct net_bridge_port *port, 10498ef2a9a5SYOSHIFUJI Hideaki int saddr) 1050eb1d1641SHerbert Xu { 1051eb1d1641SHerbert Xu if (saddr) 1052eb1d1641SHerbert Xu mod_timer(&br->multicast_querier_timer, 1053eb1d1641SHerbert Xu jiffies + br->multicast_querier_interval); 1054eb1d1641SHerbert Xu else if (timer_pending(&br->multicast_querier_timer)) 1055eb1d1641SHerbert Xu return; 1056eb1d1641SHerbert Xu 1057eb1d1641SHerbert Xu br_multicast_mark_router(br, port); 1058eb1d1641SHerbert Xu } 1059eb1d1641SHerbert Xu 10608ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_query(struct net_bridge *br, 1061eb1d1641SHerbert Xu struct net_bridge_port *port, 1062eb1d1641SHerbert Xu struct sk_buff *skb) 1063eb1d1641SHerbert Xu { 1064b71d1d42SEric Dumazet const struct iphdr *iph = ip_hdr(skb); 1065eb1d1641SHerbert Xu struct igmphdr *ih = igmp_hdr(skb); 1066eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1067eb1d1641SHerbert Xu struct igmpv3_query *ih3; 1068eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1069e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 1070eb1d1641SHerbert Xu unsigned long max_delay; 1071eb1d1641SHerbert Xu unsigned long now = jiffies; 1072eb1d1641SHerbert Xu __be32 group; 1073bec68ff1SYOSHIFUJI Hideaki int err = 0; 1074eb1d1641SHerbert Xu 1075eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1076eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1077eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 1078eb1d1641SHerbert Xu goto out; 1079eb1d1641SHerbert Xu 10808ef2a9a5SYOSHIFUJI Hideaki br_multicast_query_received(br, port, !!iph->saddr); 1081eb1d1641SHerbert Xu 1082eb1d1641SHerbert Xu group = ih->group; 1083eb1d1641SHerbert Xu 1084eb1d1641SHerbert Xu if (skb->len == sizeof(*ih)) { 1085eb1d1641SHerbert Xu max_delay = ih->code * (HZ / IGMP_TIMER_SCALE); 1086eb1d1641SHerbert Xu 1087eb1d1641SHerbert Xu if (!max_delay) { 1088eb1d1641SHerbert Xu max_delay = 10 * HZ; 1089eb1d1641SHerbert Xu group = 0; 1090eb1d1641SHerbert Xu } 1091eb1d1641SHerbert Xu } else { 1092bec68ff1SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) { 1093bec68ff1SYOSHIFUJI Hideaki err = -EINVAL; 1094bec68ff1SYOSHIFUJI Hideaki goto out; 1095bec68ff1SYOSHIFUJI Hideaki } 1096eb1d1641SHerbert Xu 1097eb1d1641SHerbert Xu ih3 = igmpv3_query_hdr(skb); 1098eb1d1641SHerbert Xu if (ih3->nsrcs) 1099bec68ff1SYOSHIFUJI Hideaki goto out; 1100eb1d1641SHerbert Xu 11010ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 max_delay = ih3->code ? 11020ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1; 1103eb1d1641SHerbert Xu } 1104eb1d1641SHerbert Xu 1105eb1d1641SHerbert Xu if (!group) 1106eb1d1641SHerbert Xu goto out; 1107eb1d1641SHerbert Xu 1108e8051688SEric Dumazet mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group); 1109eb1d1641SHerbert Xu if (!mp) 1110eb1d1641SHerbert Xu goto out; 1111eb1d1641SHerbert Xu 1112eb1d1641SHerbert Xu max_delay *= br->multicast_last_member_count; 1113eb1d1641SHerbert Xu 11148a870178SHerbert Xu if (mp->mglist && 1115eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1116eb1d1641SHerbert Xu time_after(mp->timer.expires, now + max_delay) : 1117eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) 1118eb1d1641SHerbert Xu mod_timer(&mp->timer, now + max_delay); 1119eb1d1641SHerbert Xu 1120e8051688SEric Dumazet for (pp = &mp->ports; 1121e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 1122e8051688SEric Dumazet pp = &p->next) { 1123eb1d1641SHerbert Xu if (timer_pending(&p->timer) ? 1124eb1d1641SHerbert Xu time_after(p->timer.expires, now + max_delay) : 1125eb1d1641SHerbert Xu try_to_del_timer_sync(&p->timer) >= 0) 112624f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 1127eb1d1641SHerbert Xu } 1128eb1d1641SHerbert Xu 1129eb1d1641SHerbert Xu out: 1130eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1131bec68ff1SYOSHIFUJI Hideaki return err; 1132eb1d1641SHerbert Xu } 1133eb1d1641SHerbert Xu 1134dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 113508b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_query(struct net_bridge *br, 113608b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 113708b202b6SYOSHIFUJI Hideaki struct sk_buff *skb) 113808b202b6SYOSHIFUJI Hideaki { 1139b71d1d42SEric Dumazet const struct ipv6hdr *ip6h = ipv6_hdr(skb); 114008b202b6SYOSHIFUJI Hideaki struct mld_msg *mld = (struct mld_msg *) icmp6_hdr(skb); 114108b202b6SYOSHIFUJI Hideaki struct net_bridge_mdb_entry *mp; 114208b202b6SYOSHIFUJI Hideaki struct mld2_query *mld2q; 1143e8051688SEric Dumazet struct net_bridge_port_group *p; 1144e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 114508b202b6SYOSHIFUJI Hideaki unsigned long max_delay; 114608b202b6SYOSHIFUJI Hideaki unsigned long now = jiffies; 1147b71d1d42SEric Dumazet const struct in6_addr *group = NULL; 114808b202b6SYOSHIFUJI Hideaki int err = 0; 114908b202b6SYOSHIFUJI Hideaki 115008b202b6SYOSHIFUJI Hideaki spin_lock(&br->multicast_lock); 115108b202b6SYOSHIFUJI Hideaki if (!netif_running(br->dev) || 115208b202b6SYOSHIFUJI Hideaki (port && port->state == BR_STATE_DISABLED)) 115308b202b6SYOSHIFUJI Hideaki goto out; 115408b202b6SYOSHIFUJI Hideaki 115508b202b6SYOSHIFUJI Hideaki br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->saddr)); 115608b202b6SYOSHIFUJI Hideaki 115708b202b6SYOSHIFUJI Hideaki if (skb->len == sizeof(*mld)) { 115808b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*mld))) { 115908b202b6SYOSHIFUJI Hideaki err = -EINVAL; 116008b202b6SYOSHIFUJI Hideaki goto out; 116108b202b6SYOSHIFUJI Hideaki } 116208b202b6SYOSHIFUJI Hideaki mld = (struct mld_msg *) icmp6_hdr(skb); 116308b202b6SYOSHIFUJI Hideaki max_delay = msecs_to_jiffies(htons(mld->mld_maxdelay)); 116408b202b6SYOSHIFUJI Hideaki if (max_delay) 116508b202b6SYOSHIFUJI Hideaki group = &mld->mld_mca; 116608b202b6SYOSHIFUJI Hideaki } else if (skb->len >= sizeof(*mld2q)) { 116708b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*mld2q))) { 116808b202b6SYOSHIFUJI Hideaki err = -EINVAL; 116908b202b6SYOSHIFUJI Hideaki goto out; 117008b202b6SYOSHIFUJI Hideaki } 117108b202b6SYOSHIFUJI Hideaki mld2q = (struct mld2_query *)icmp6_hdr(skb); 117208b202b6SYOSHIFUJI Hideaki if (!mld2q->mld2q_nsrcs) 117308b202b6SYOSHIFUJI Hideaki group = &mld2q->mld2q_mca; 117408b202b6SYOSHIFUJI Hideaki max_delay = mld2q->mld2q_mrc ? MLDV2_MRC(mld2q->mld2q_mrc) : 1; 117508b202b6SYOSHIFUJI Hideaki } 117608b202b6SYOSHIFUJI Hideaki 117708b202b6SYOSHIFUJI Hideaki if (!group) 117808b202b6SYOSHIFUJI Hideaki goto out; 117908b202b6SYOSHIFUJI Hideaki 1180e8051688SEric Dumazet mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group); 118108b202b6SYOSHIFUJI Hideaki if (!mp) 118208b202b6SYOSHIFUJI Hideaki goto out; 118308b202b6SYOSHIFUJI Hideaki 118408b202b6SYOSHIFUJI Hideaki max_delay *= br->multicast_last_member_count; 11858a870178SHerbert Xu if (mp->mglist && 118608b202b6SYOSHIFUJI Hideaki (timer_pending(&mp->timer) ? 118708b202b6SYOSHIFUJI Hideaki time_after(mp->timer.expires, now + max_delay) : 118808b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&mp->timer) >= 0)) 118908b202b6SYOSHIFUJI Hideaki mod_timer(&mp->timer, now + max_delay); 119008b202b6SYOSHIFUJI Hideaki 1191e8051688SEric Dumazet for (pp = &mp->ports; 1192e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 1193e8051688SEric Dumazet pp = &p->next) { 119408b202b6SYOSHIFUJI Hideaki if (timer_pending(&p->timer) ? 119508b202b6SYOSHIFUJI Hideaki time_after(p->timer.expires, now + max_delay) : 119608b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&p->timer) >= 0) 119724f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 119808b202b6SYOSHIFUJI Hideaki } 119908b202b6SYOSHIFUJI Hideaki 120008b202b6SYOSHIFUJI Hideaki out: 120108b202b6SYOSHIFUJI Hideaki spin_unlock(&br->multicast_lock); 120208b202b6SYOSHIFUJI Hideaki return err; 120308b202b6SYOSHIFUJI Hideaki } 120408b202b6SYOSHIFUJI Hideaki #endif 120508b202b6SYOSHIFUJI Hideaki 1206eb1d1641SHerbert Xu static void br_multicast_leave_group(struct net_bridge *br, 1207eb1d1641SHerbert Xu struct net_bridge_port *port, 12088ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group) 1209eb1d1641SHerbert Xu { 1210eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 1211eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1212eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1213eb1d1641SHerbert Xu unsigned long now; 1214eb1d1641SHerbert Xu unsigned long time; 1215eb1d1641SHerbert Xu 1216eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1217eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1218eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED) || 1219eb1d1641SHerbert Xu timer_pending(&br->multicast_querier_timer)) 1220eb1d1641SHerbert Xu goto out; 1221eb1d1641SHerbert Xu 1222e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1223eb1d1641SHerbert Xu mp = br_mdb_ip_get(mdb, group); 1224eb1d1641SHerbert Xu if (!mp) 1225eb1d1641SHerbert Xu goto out; 1226eb1d1641SHerbert Xu 1227eb1d1641SHerbert Xu now = jiffies; 1228eb1d1641SHerbert Xu time = now + br->multicast_last_member_count * 1229eb1d1641SHerbert Xu br->multicast_last_member_interval; 1230eb1d1641SHerbert Xu 1231eb1d1641SHerbert Xu if (!port) { 12328a870178SHerbert Xu if (mp->mglist && 1233eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1234eb1d1641SHerbert Xu time_after(mp->timer.expires, time) : 1235eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) { 1236eb1d1641SHerbert Xu mod_timer(&mp->timer, time); 1237eb1d1641SHerbert Xu } 1238eb1d1641SHerbert Xu 1239eb1d1641SHerbert Xu goto out; 1240eb1d1641SHerbert Xu } 1241eb1d1641SHerbert Xu 1242e8051688SEric Dumazet for (p = mlock_dereference(mp->ports, br); 1243e8051688SEric Dumazet p != NULL; 1244e8051688SEric Dumazet p = mlock_dereference(p->next, br)) { 1245eb1d1641SHerbert Xu if (p->port != port) 1246eb1d1641SHerbert Xu continue; 1247eb1d1641SHerbert Xu 1248eb1d1641SHerbert Xu if (!hlist_unhashed(&p->mglist) && 1249eb1d1641SHerbert Xu (timer_pending(&p->timer) ? 1250eb1d1641SHerbert Xu time_after(p->timer.expires, time) : 1251eb1d1641SHerbert Xu try_to_del_timer_sync(&p->timer) >= 0)) { 1252eb1d1641SHerbert Xu mod_timer(&p->timer, time); 1253eb1d1641SHerbert Xu } 1254eb1d1641SHerbert Xu 1255eb1d1641SHerbert Xu break; 1256eb1d1641SHerbert Xu } 1257eb1d1641SHerbert Xu 1258eb1d1641SHerbert Xu out: 1259eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1260eb1d1641SHerbert Xu } 1261eb1d1641SHerbert Xu 12628ef2a9a5SYOSHIFUJI Hideaki static void br_ip4_multicast_leave_group(struct net_bridge *br, 12638ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 12648ef2a9a5SYOSHIFUJI Hideaki __be32 group) 12658ef2a9a5SYOSHIFUJI Hideaki { 12668ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 12678ef2a9a5SYOSHIFUJI Hideaki 12688ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 12698ef2a9a5SYOSHIFUJI Hideaki return; 12708ef2a9a5SYOSHIFUJI Hideaki 12718ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 12728ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 12738ef2a9a5SYOSHIFUJI Hideaki 12748ef2a9a5SYOSHIFUJI Hideaki br_multicast_leave_group(br, port, &br_group); 12758ef2a9a5SYOSHIFUJI Hideaki } 12768ef2a9a5SYOSHIFUJI Hideaki 1277dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 127808b202b6SYOSHIFUJI Hideaki static void br_ip6_multicast_leave_group(struct net_bridge *br, 127908b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 128008b202b6SYOSHIFUJI Hideaki const struct in6_addr *group) 128108b202b6SYOSHIFUJI Hideaki { 128208b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 128308b202b6SYOSHIFUJI Hideaki 1284e4de9f9eSLinus Lüssing if (!ipv6_is_transient_multicast(group)) 128508b202b6SYOSHIFUJI Hideaki return; 128608b202b6SYOSHIFUJI Hideaki 12874e3fd7a0SAlexey Dobriyan br_group.u.ip6 = *group; 128808b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 128908b202b6SYOSHIFUJI Hideaki 129008b202b6SYOSHIFUJI Hideaki br_multicast_leave_group(br, port, &br_group); 129108b202b6SYOSHIFUJI Hideaki } 129208b202b6SYOSHIFUJI Hideaki #endif 12938ef2a9a5SYOSHIFUJI Hideaki 1294eb1d1641SHerbert Xu static int br_multicast_ipv4_rcv(struct net_bridge *br, 1295eb1d1641SHerbert Xu struct net_bridge_port *port, 1296eb1d1641SHerbert Xu struct sk_buff *skb) 1297eb1d1641SHerbert Xu { 1298eb1d1641SHerbert Xu struct sk_buff *skb2 = skb; 1299b71d1d42SEric Dumazet const struct iphdr *iph; 1300eb1d1641SHerbert Xu struct igmphdr *ih; 130195c96174SEric Dumazet unsigned int len; 130295c96174SEric Dumazet unsigned int offset; 1303eb1d1641SHerbert Xu int err; 1304eb1d1641SHerbert Xu 1305eb1d1641SHerbert Xu /* We treat OOM as packet loss for now. */ 1306eb1d1641SHerbert Xu if (!pskb_may_pull(skb, sizeof(*iph))) 1307eb1d1641SHerbert Xu return -EINVAL; 1308eb1d1641SHerbert Xu 1309eb1d1641SHerbert Xu iph = ip_hdr(skb); 1310eb1d1641SHerbert Xu 1311eb1d1641SHerbert Xu if (iph->ihl < 5 || iph->version != 4) 1312eb1d1641SHerbert Xu return -EINVAL; 1313eb1d1641SHerbert Xu 1314eb1d1641SHerbert Xu if (!pskb_may_pull(skb, ip_hdrlen(skb))) 1315eb1d1641SHerbert Xu return -EINVAL; 1316eb1d1641SHerbert Xu 1317eb1d1641SHerbert Xu iph = ip_hdr(skb); 1318eb1d1641SHerbert Xu 1319eb1d1641SHerbert Xu if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl))) 1320eb1d1641SHerbert Xu return -EINVAL; 1321eb1d1641SHerbert Xu 1322bd4265feSHerbert Xu if (iph->protocol != IPPROTO_IGMP) { 1323bd4265feSHerbert Xu if ((iph->daddr & IGMP_LOCAL_GROUP_MASK) != IGMP_LOCAL_GROUP) 1324bd4265feSHerbert Xu BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 1325eb1d1641SHerbert Xu return 0; 1326bd4265feSHerbert Xu } 1327eb1d1641SHerbert Xu 1328eb1d1641SHerbert Xu len = ntohs(iph->tot_len); 1329eb1d1641SHerbert Xu if (skb->len < len || len < ip_hdrlen(skb)) 1330eb1d1641SHerbert Xu return -EINVAL; 1331eb1d1641SHerbert Xu 1332eb1d1641SHerbert Xu if (skb->len > len) { 1333eb1d1641SHerbert Xu skb2 = skb_clone(skb, GFP_ATOMIC); 1334eb1d1641SHerbert Xu if (!skb2) 1335eb1d1641SHerbert Xu return -ENOMEM; 1336eb1d1641SHerbert Xu 1337eb1d1641SHerbert Xu err = pskb_trim_rcsum(skb2, len); 1338eb1d1641SHerbert Xu if (err) 13398440853bSYOSHIFUJI Hideaki / 吉藤英明 goto err_out; 1340eb1d1641SHerbert Xu } 1341eb1d1641SHerbert Xu 1342eb1d1641SHerbert Xu len -= ip_hdrlen(skb2); 1343eb1d1641SHerbert Xu offset = skb_network_offset(skb2) + ip_hdrlen(skb2); 1344eb1d1641SHerbert Xu __skb_pull(skb2, offset); 1345eb1d1641SHerbert Xu skb_reset_transport_header(skb2); 1346eb1d1641SHerbert Xu 1347eb1d1641SHerbert Xu err = -EINVAL; 1348eb1d1641SHerbert Xu if (!pskb_may_pull(skb2, sizeof(*ih))) 1349eb1d1641SHerbert Xu goto out; 1350eb1d1641SHerbert Xu 1351eb1d1641SHerbert Xu switch (skb2->ip_summed) { 1352eb1d1641SHerbert Xu case CHECKSUM_COMPLETE: 1353eb1d1641SHerbert Xu if (!csum_fold(skb2->csum)) 1354eb1d1641SHerbert Xu break; 1355eb1d1641SHerbert Xu /* fall through */ 1356eb1d1641SHerbert Xu case CHECKSUM_NONE: 1357eb1d1641SHerbert Xu skb2->csum = 0; 1358eb1d1641SHerbert Xu if (skb_checksum_complete(skb2)) 13598440853bSYOSHIFUJI Hideaki / 吉藤英明 goto out; 1360eb1d1641SHerbert Xu } 1361eb1d1641SHerbert Xu 1362eb1d1641SHerbert Xu err = 0; 1363eb1d1641SHerbert Xu 1364eb1d1641SHerbert Xu BR_INPUT_SKB_CB(skb)->igmp = 1; 1365eb1d1641SHerbert Xu ih = igmp_hdr(skb2); 1366eb1d1641SHerbert Xu 1367eb1d1641SHerbert Xu switch (ih->type) { 1368eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_REPORT: 1369eb1d1641SHerbert Xu case IGMPV2_HOST_MEMBERSHIP_REPORT: 137062b2bcb4SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 13718ef2a9a5SYOSHIFUJI Hideaki err = br_ip4_multicast_add_group(br, port, ih->group); 1372eb1d1641SHerbert Xu break; 1373eb1d1641SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 13748ef2a9a5SYOSHIFUJI Hideaki err = br_ip4_multicast_igmp3_report(br, port, skb2); 1375eb1d1641SHerbert Xu break; 1376eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_QUERY: 13778ef2a9a5SYOSHIFUJI Hideaki err = br_ip4_multicast_query(br, port, skb2); 1378eb1d1641SHerbert Xu break; 1379eb1d1641SHerbert Xu case IGMP_HOST_LEAVE_MESSAGE: 13808ef2a9a5SYOSHIFUJI Hideaki br_ip4_multicast_leave_group(br, port, ih->group); 1381eb1d1641SHerbert Xu break; 1382eb1d1641SHerbert Xu } 1383eb1d1641SHerbert Xu 1384eb1d1641SHerbert Xu out: 1385eb1d1641SHerbert Xu __skb_push(skb2, offset); 13868440853bSYOSHIFUJI Hideaki / 吉藤英明 err_out: 1387eb1d1641SHerbert Xu if (skb2 != skb) 1388eb1d1641SHerbert Xu kfree_skb(skb2); 1389eb1d1641SHerbert Xu return err; 1390eb1d1641SHerbert Xu } 1391eb1d1641SHerbert Xu 1392dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 139308b202b6SYOSHIFUJI Hideaki static int br_multicast_ipv6_rcv(struct net_bridge *br, 139408b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 139508b202b6SYOSHIFUJI Hideaki struct sk_buff *skb) 139608b202b6SYOSHIFUJI Hideaki { 13979d89081dSTomas Winkler struct sk_buff *skb2; 1398b71d1d42SEric Dumazet const struct ipv6hdr *ip6h; 139922df1331SEric Dumazet u8 icmp6_type; 140008b202b6SYOSHIFUJI Hideaki u8 nexthdr; 140175f2811cSJesse Gross __be16 frag_off; 140295c96174SEric Dumazet unsigned int len; 1403bb7a0bd6SKulikov Vasiliy int offset; 140408b202b6SYOSHIFUJI Hideaki int err; 140508b202b6SYOSHIFUJI Hideaki 140608b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*ip6h))) 140708b202b6SYOSHIFUJI Hideaki return -EINVAL; 140808b202b6SYOSHIFUJI Hideaki 140908b202b6SYOSHIFUJI Hideaki ip6h = ipv6_hdr(skb); 141008b202b6SYOSHIFUJI Hideaki 141108b202b6SYOSHIFUJI Hideaki /* 141208b202b6SYOSHIFUJI Hideaki * We're interested in MLD messages only. 141308b202b6SYOSHIFUJI Hideaki * - Version is 6 141408b202b6SYOSHIFUJI Hideaki * - MLD has always Router Alert hop-by-hop option 141508b202b6SYOSHIFUJI Hideaki * - But we do not support jumbrograms. 141608b202b6SYOSHIFUJI Hideaki */ 141708b202b6SYOSHIFUJI Hideaki if (ip6h->version != 6 || 141808b202b6SYOSHIFUJI Hideaki ip6h->nexthdr != IPPROTO_HOPOPTS || 141908b202b6SYOSHIFUJI Hideaki ip6h->payload_len == 0) 142008b202b6SYOSHIFUJI Hideaki return 0; 142108b202b6SYOSHIFUJI Hideaki 1422ff9a57a6SLinus Lüssing len = ntohs(ip6h->payload_len) + sizeof(*ip6h); 142308b202b6SYOSHIFUJI Hideaki if (skb->len < len) 142408b202b6SYOSHIFUJI Hideaki return -EINVAL; 142508b202b6SYOSHIFUJI Hideaki 142608b202b6SYOSHIFUJI Hideaki nexthdr = ip6h->nexthdr; 142775f2811cSJesse Gross offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr, &frag_off); 142808b202b6SYOSHIFUJI Hideaki 142908b202b6SYOSHIFUJI Hideaki if (offset < 0 || nexthdr != IPPROTO_ICMPV6) 143008b202b6SYOSHIFUJI Hideaki return 0; 143108b202b6SYOSHIFUJI Hideaki 143208b202b6SYOSHIFUJI Hideaki /* Okay, we found ICMPv6 header */ 143308b202b6SYOSHIFUJI Hideaki skb2 = skb_clone(skb, GFP_ATOMIC); 143408b202b6SYOSHIFUJI Hideaki if (!skb2) 143508b202b6SYOSHIFUJI Hideaki return -ENOMEM; 143608b202b6SYOSHIFUJI Hideaki 14379d89081dSTomas Winkler err = -EINVAL; 14389d89081dSTomas Winkler if (!pskb_may_pull(skb2, offset + sizeof(struct icmp6hdr))) 14399d89081dSTomas Winkler goto out; 14409d89081dSTomas Winkler 144108b202b6SYOSHIFUJI Hideaki len -= offset - skb_network_offset(skb2); 144208b202b6SYOSHIFUJI Hideaki 144308b202b6SYOSHIFUJI Hideaki __skb_pull(skb2, offset); 144408b202b6SYOSHIFUJI Hideaki skb_reset_transport_header(skb2); 1445fa2da8cdSstephen hemminger skb_postpull_rcsum(skb2, skb_network_header(skb2), 1446fa2da8cdSstephen hemminger skb_network_header_len(skb2)); 144708b202b6SYOSHIFUJI Hideaki 144822df1331SEric Dumazet icmp6_type = icmp6_hdr(skb2)->icmp6_type; 144908b202b6SYOSHIFUJI Hideaki 145022df1331SEric Dumazet switch (icmp6_type) { 145108b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 145208b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 145308b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 145408b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 145508b202b6SYOSHIFUJI Hideaki break; 145608b202b6SYOSHIFUJI Hideaki default: 145708b202b6SYOSHIFUJI Hideaki err = 0; 145808b202b6SYOSHIFUJI Hideaki goto out; 145908b202b6SYOSHIFUJI Hideaki } 146008b202b6SYOSHIFUJI Hideaki 146108b202b6SYOSHIFUJI Hideaki /* Okay, we found MLD message. Check further. */ 146208b202b6SYOSHIFUJI Hideaki if (skb2->len > len) { 146308b202b6SYOSHIFUJI Hideaki err = pskb_trim_rcsum(skb2, len); 146408b202b6SYOSHIFUJI Hideaki if (err) 146508b202b6SYOSHIFUJI Hideaki goto out; 14664b275d7eSYan, Zheng err = -EINVAL; 146708b202b6SYOSHIFUJI Hideaki } 146808b202b6SYOSHIFUJI Hideaki 14694b275d7eSYan, Zheng ip6h = ipv6_hdr(skb2); 14704b275d7eSYan, Zheng 147108b202b6SYOSHIFUJI Hideaki switch (skb2->ip_summed) { 147208b202b6SYOSHIFUJI Hideaki case CHECKSUM_COMPLETE: 14734b275d7eSYan, Zheng if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, skb2->len, 14744b275d7eSYan, Zheng IPPROTO_ICMPV6, skb2->csum)) 147508b202b6SYOSHIFUJI Hideaki break; 147608b202b6SYOSHIFUJI Hideaki /*FALLTHROUGH*/ 147708b202b6SYOSHIFUJI Hideaki case CHECKSUM_NONE: 14784b275d7eSYan, Zheng skb2->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr, 14794b275d7eSYan, Zheng &ip6h->daddr, 14804b275d7eSYan, Zheng skb2->len, 14814b275d7eSYan, Zheng IPPROTO_ICMPV6, 0)); 14824b275d7eSYan, Zheng if (__skb_checksum_complete(skb2)) 148308b202b6SYOSHIFUJI Hideaki goto out; 148408b202b6SYOSHIFUJI Hideaki } 148508b202b6SYOSHIFUJI Hideaki 148608b202b6SYOSHIFUJI Hideaki err = 0; 148708b202b6SYOSHIFUJI Hideaki 148808b202b6SYOSHIFUJI Hideaki BR_INPUT_SKB_CB(skb)->igmp = 1; 148908b202b6SYOSHIFUJI Hideaki 149022df1331SEric Dumazet switch (icmp6_type) { 149108b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 149208b202b6SYOSHIFUJI Hideaki { 14939d89081dSTomas Winkler struct mld_msg *mld; 14949d89081dSTomas Winkler if (!pskb_may_pull(skb2, sizeof(*mld))) { 14959d89081dSTomas Winkler err = -EINVAL; 14969d89081dSTomas Winkler goto out; 14979d89081dSTomas Winkler } 14989d89081dSTomas Winkler mld = (struct mld_msg *)skb_transport_header(skb2); 1499fc2af6c7SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 150008b202b6SYOSHIFUJI Hideaki err = br_ip6_multicast_add_group(br, port, &mld->mld_mca); 150108b202b6SYOSHIFUJI Hideaki break; 150208b202b6SYOSHIFUJI Hideaki } 150308b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 150408b202b6SYOSHIFUJI Hideaki err = br_ip6_multicast_mld2_report(br, port, skb2); 150508b202b6SYOSHIFUJI Hideaki break; 150608b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 150708b202b6SYOSHIFUJI Hideaki err = br_ip6_multicast_query(br, port, skb2); 150808b202b6SYOSHIFUJI Hideaki break; 150908b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 151008b202b6SYOSHIFUJI Hideaki { 15119d89081dSTomas Winkler struct mld_msg *mld; 15129d89081dSTomas Winkler if (!pskb_may_pull(skb2, sizeof(*mld))) { 15139d89081dSTomas Winkler err = -EINVAL; 15149d89081dSTomas Winkler goto out; 15159d89081dSTomas Winkler } 15169d89081dSTomas Winkler mld = (struct mld_msg *)skb_transport_header(skb2); 151708b202b6SYOSHIFUJI Hideaki br_ip6_multicast_leave_group(br, port, &mld->mld_mca); 151808b202b6SYOSHIFUJI Hideaki } 151908b202b6SYOSHIFUJI Hideaki } 152008b202b6SYOSHIFUJI Hideaki 152108b202b6SYOSHIFUJI Hideaki out: 152208b202b6SYOSHIFUJI Hideaki kfree_skb(skb2); 152308b202b6SYOSHIFUJI Hideaki return err; 152408b202b6SYOSHIFUJI Hideaki } 152508b202b6SYOSHIFUJI Hideaki #endif 152608b202b6SYOSHIFUJI Hideaki 1527eb1d1641SHerbert Xu int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port, 1528eb1d1641SHerbert Xu struct sk_buff *skb) 1529eb1d1641SHerbert Xu { 15301fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->igmp = 0; 15311fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->mrouters_only = 0; 15321fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 1533eb1d1641SHerbert Xu if (br->multicast_disabled) 1534eb1d1641SHerbert Xu return 0; 1535eb1d1641SHerbert Xu 1536eb1d1641SHerbert Xu switch (skb->protocol) { 1537eb1d1641SHerbert Xu case htons(ETH_P_IP): 1538eb1d1641SHerbert Xu return br_multicast_ipv4_rcv(br, port, skb); 1539dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 154008b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 154108b202b6SYOSHIFUJI Hideaki return br_multicast_ipv6_rcv(br, port, skb); 154208b202b6SYOSHIFUJI Hideaki #endif 1543eb1d1641SHerbert Xu } 1544eb1d1641SHerbert Xu 1545eb1d1641SHerbert Xu return 0; 1546eb1d1641SHerbert Xu } 1547eb1d1641SHerbert Xu 1548eb1d1641SHerbert Xu static void br_multicast_query_expired(unsigned long data) 1549eb1d1641SHerbert Xu { 1550eb1d1641SHerbert Xu struct net_bridge *br = (void *)data; 1551eb1d1641SHerbert Xu 1552eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1553eb1d1641SHerbert Xu if (br->multicast_startup_queries_sent < 1554eb1d1641SHerbert Xu br->multicast_startup_query_count) 1555eb1d1641SHerbert Xu br->multicast_startup_queries_sent++; 1556eb1d1641SHerbert Xu 1557eb1d1641SHerbert Xu br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent); 1558eb1d1641SHerbert Xu 1559eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1560eb1d1641SHerbert Xu } 1561eb1d1641SHerbert Xu 1562eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br) 1563eb1d1641SHerbert Xu { 1564eb1d1641SHerbert Xu br->hash_elasticity = 4; 1565eb1d1641SHerbert Xu br->hash_max = 512; 1566eb1d1641SHerbert Xu 1567eb1d1641SHerbert Xu br->multicast_router = 1; 1568c5c23260SHerbert Xu br->multicast_querier = 0; 1569eb1d1641SHerbert Xu br->multicast_last_member_count = 2; 1570eb1d1641SHerbert Xu br->multicast_startup_query_count = 2; 1571eb1d1641SHerbert Xu 1572eb1d1641SHerbert Xu br->multicast_last_member_interval = HZ; 1573eb1d1641SHerbert Xu br->multicast_query_response_interval = 10 * HZ; 1574eb1d1641SHerbert Xu br->multicast_startup_query_interval = 125 * HZ / 4; 1575eb1d1641SHerbert Xu br->multicast_query_interval = 125 * HZ; 1576eb1d1641SHerbert Xu br->multicast_querier_interval = 255 * HZ; 1577eb1d1641SHerbert Xu br->multicast_membership_interval = 260 * HZ; 1578eb1d1641SHerbert Xu 1579eb1d1641SHerbert Xu spin_lock_init(&br->multicast_lock); 1580eb1d1641SHerbert Xu setup_timer(&br->multicast_router_timer, 1581eb1d1641SHerbert Xu br_multicast_local_router_expired, 0); 1582eb1d1641SHerbert Xu setup_timer(&br->multicast_querier_timer, 1583*bb63f1f8SHerbert Xu br_multicast_querier_expired, (unsigned long)br); 1584eb1d1641SHerbert Xu setup_timer(&br->multicast_query_timer, br_multicast_query_expired, 1585eb1d1641SHerbert Xu (unsigned long)br); 1586eb1d1641SHerbert Xu } 1587eb1d1641SHerbert Xu 1588eb1d1641SHerbert Xu void br_multicast_open(struct net_bridge *br) 1589eb1d1641SHerbert Xu { 1590eb1d1641SHerbert Xu br->multicast_startup_queries_sent = 0; 1591eb1d1641SHerbert Xu 1592eb1d1641SHerbert Xu if (br->multicast_disabled) 1593eb1d1641SHerbert Xu return; 1594eb1d1641SHerbert Xu 1595eb1d1641SHerbert Xu mod_timer(&br->multicast_query_timer, jiffies); 1596eb1d1641SHerbert Xu } 1597eb1d1641SHerbert Xu 1598eb1d1641SHerbert Xu void br_multicast_stop(struct net_bridge *br) 1599eb1d1641SHerbert Xu { 1600eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 1601eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1602eb1d1641SHerbert Xu struct hlist_node *p, *n; 1603eb1d1641SHerbert Xu u32 ver; 1604eb1d1641SHerbert Xu int i; 1605eb1d1641SHerbert Xu 1606eb1d1641SHerbert Xu del_timer_sync(&br->multicast_router_timer); 1607eb1d1641SHerbert Xu del_timer_sync(&br->multicast_querier_timer); 1608eb1d1641SHerbert Xu del_timer_sync(&br->multicast_query_timer); 1609eb1d1641SHerbert Xu 1610eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1611e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1612eb1d1641SHerbert Xu if (!mdb) 1613eb1d1641SHerbert Xu goto out; 1614eb1d1641SHerbert Xu 1615eb1d1641SHerbert Xu br->mdb = NULL; 1616eb1d1641SHerbert Xu 1617eb1d1641SHerbert Xu ver = mdb->ver; 1618eb1d1641SHerbert Xu for (i = 0; i < mdb->max; i++) { 1619eb1d1641SHerbert Xu hlist_for_each_entry_safe(mp, p, n, &mdb->mhash[i], 1620eb1d1641SHerbert Xu hlist[ver]) { 1621eb1d1641SHerbert Xu del_timer(&mp->timer); 1622eb1d1641SHerbert Xu call_rcu_bh(&mp->rcu, br_multicast_free_group); 1623eb1d1641SHerbert Xu } 1624eb1d1641SHerbert Xu } 1625eb1d1641SHerbert Xu 1626eb1d1641SHerbert Xu if (mdb->old) { 1627eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 162810cc2b50SHerbert Xu rcu_barrier_bh(); 1629eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1630eb1d1641SHerbert Xu WARN_ON(mdb->old); 1631eb1d1641SHerbert Xu } 1632eb1d1641SHerbert Xu 1633eb1d1641SHerbert Xu mdb->old = mdb; 1634eb1d1641SHerbert Xu call_rcu_bh(&mdb->rcu, br_mdb_free); 1635eb1d1641SHerbert Xu 1636eb1d1641SHerbert Xu out: 1637eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 1638eb1d1641SHerbert Xu } 16390909e117SHerbert Xu 16400909e117SHerbert Xu int br_multicast_set_router(struct net_bridge *br, unsigned long val) 16410909e117SHerbert Xu { 16420909e117SHerbert Xu int err = -ENOENT; 16430909e117SHerbert Xu 16440909e117SHerbert Xu spin_lock_bh(&br->multicast_lock); 16450909e117SHerbert Xu if (!netif_running(br->dev)) 16460909e117SHerbert Xu goto unlock; 16470909e117SHerbert Xu 16480909e117SHerbert Xu switch (val) { 16490909e117SHerbert Xu case 0: 16500909e117SHerbert Xu case 2: 16510909e117SHerbert Xu del_timer(&br->multicast_router_timer); 16520909e117SHerbert Xu /* fall through */ 16530909e117SHerbert Xu case 1: 16540909e117SHerbert Xu br->multicast_router = val; 16550909e117SHerbert Xu err = 0; 16560909e117SHerbert Xu break; 16570909e117SHerbert Xu 16580909e117SHerbert Xu default: 16590909e117SHerbert Xu err = -EINVAL; 16600909e117SHerbert Xu break; 16610909e117SHerbert Xu } 16620909e117SHerbert Xu 16630909e117SHerbert Xu unlock: 16640909e117SHerbert Xu spin_unlock_bh(&br->multicast_lock); 16650909e117SHerbert Xu 16660909e117SHerbert Xu return err; 16670909e117SHerbert Xu } 16680909e117SHerbert Xu 16690909e117SHerbert Xu int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val) 16700909e117SHerbert Xu { 16710909e117SHerbert Xu struct net_bridge *br = p->br; 16720909e117SHerbert Xu int err = -ENOENT; 16730909e117SHerbert Xu 16740909e117SHerbert Xu spin_lock(&br->multicast_lock); 16750909e117SHerbert Xu if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED) 16760909e117SHerbert Xu goto unlock; 16770909e117SHerbert Xu 16780909e117SHerbert Xu switch (val) { 16790909e117SHerbert Xu case 0: 16800909e117SHerbert Xu case 1: 16810909e117SHerbert Xu case 2: 16820909e117SHerbert Xu p->multicast_router = val; 16830909e117SHerbert Xu err = 0; 16840909e117SHerbert Xu 16850909e117SHerbert Xu if (val < 2 && !hlist_unhashed(&p->rlist)) 16860909e117SHerbert Xu hlist_del_init_rcu(&p->rlist); 16870909e117SHerbert Xu 16880909e117SHerbert Xu if (val == 1) 16890909e117SHerbert Xu break; 16900909e117SHerbert Xu 16910909e117SHerbert Xu del_timer(&p->multicast_router_timer); 16920909e117SHerbert Xu 16930909e117SHerbert Xu if (val == 0) 16940909e117SHerbert Xu break; 16950909e117SHerbert Xu 16960909e117SHerbert Xu br_multicast_add_router(br, p); 16970909e117SHerbert Xu break; 16980909e117SHerbert Xu 16990909e117SHerbert Xu default: 17000909e117SHerbert Xu err = -EINVAL; 17010909e117SHerbert Xu break; 17020909e117SHerbert Xu } 17030909e117SHerbert Xu 17040909e117SHerbert Xu unlock: 17050909e117SHerbert Xu spin_unlock(&br->multicast_lock); 17060909e117SHerbert Xu 17070909e117SHerbert Xu return err; 17080909e117SHerbert Xu } 1709561f1103SHerbert Xu 171074857216SHerbert Xu static void br_multicast_start_querier(struct net_bridge *br) 1711561f1103SHerbert Xu { 1712561f1103SHerbert Xu struct net_bridge_port *port; 171374857216SHerbert Xu 171474857216SHerbert Xu br_multicast_open(br); 171574857216SHerbert Xu 171674857216SHerbert Xu list_for_each_entry(port, &br->port_list, list) { 171774857216SHerbert Xu if (port->state == BR_STATE_DISABLED || 171874857216SHerbert Xu port->state == BR_STATE_BLOCKING) 171974857216SHerbert Xu continue; 172074857216SHerbert Xu 172174857216SHerbert Xu __br_multicast_enable_port(port); 172274857216SHerbert Xu } 172374857216SHerbert Xu } 172474857216SHerbert Xu 172574857216SHerbert Xu int br_multicast_toggle(struct net_bridge *br, unsigned long val) 172674857216SHerbert Xu { 17273a7fda06SHerbert Xu int err = 0; 1728e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 1729561f1103SHerbert Xu 1730ef5e0d82SAndrey Vagin spin_lock_bh(&br->multicast_lock); 1731561f1103SHerbert Xu if (br->multicast_disabled == !val) 1732561f1103SHerbert Xu goto unlock; 1733561f1103SHerbert Xu 1734561f1103SHerbert Xu br->multicast_disabled = !val; 1735561f1103SHerbert Xu if (br->multicast_disabled) 1736561f1103SHerbert Xu goto unlock; 1737561f1103SHerbert Xu 17383a7fda06SHerbert Xu if (!netif_running(br->dev)) 17393a7fda06SHerbert Xu goto unlock; 17403a7fda06SHerbert Xu 1741e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1742e8051688SEric Dumazet if (mdb) { 1743e8051688SEric Dumazet if (mdb->old) { 1744561f1103SHerbert Xu err = -EEXIST; 1745561f1103SHerbert Xu rollback: 1746561f1103SHerbert Xu br->multicast_disabled = !!val; 1747561f1103SHerbert Xu goto unlock; 1748561f1103SHerbert Xu } 1749561f1103SHerbert Xu 1750e8051688SEric Dumazet err = br_mdb_rehash(&br->mdb, mdb->max, 1751561f1103SHerbert Xu br->hash_elasticity); 1752561f1103SHerbert Xu if (err) 1753561f1103SHerbert Xu goto rollback; 1754561f1103SHerbert Xu } 1755561f1103SHerbert Xu 175674857216SHerbert Xu br_multicast_start_querier(br); 1757561f1103SHerbert Xu 1758561f1103SHerbert Xu unlock: 1759ef5e0d82SAndrey Vagin spin_unlock_bh(&br->multicast_lock); 1760561f1103SHerbert Xu 1761561f1103SHerbert Xu return err; 1762561f1103SHerbert Xu } 1763b195167fSHerbert Xu 1764c5c23260SHerbert Xu int br_multicast_set_querier(struct net_bridge *br, unsigned long val) 1765c5c23260SHerbert Xu { 1766c5c23260SHerbert Xu val = !!val; 1767c5c23260SHerbert Xu 1768c5c23260SHerbert Xu spin_lock_bh(&br->multicast_lock); 1769c5c23260SHerbert Xu if (br->multicast_querier == val) 1770c5c23260SHerbert Xu goto unlock; 1771c5c23260SHerbert Xu 1772c5c23260SHerbert Xu br->multicast_querier = val; 1773c5c23260SHerbert Xu if (val) 1774c5c23260SHerbert Xu br_multicast_start_querier(br); 1775c5c23260SHerbert Xu 1776c5c23260SHerbert Xu unlock: 1777c5c23260SHerbert Xu spin_unlock_bh(&br->multicast_lock); 1778c5c23260SHerbert Xu 1779c5c23260SHerbert Xu return 0; 1780c5c23260SHerbert Xu } 1781c5c23260SHerbert Xu 1782b195167fSHerbert Xu int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val) 1783b195167fSHerbert Xu { 1784b195167fSHerbert Xu int err = -ENOENT; 1785b195167fSHerbert Xu u32 old; 1786e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 1787b195167fSHerbert Xu 1788b195167fSHerbert Xu spin_lock(&br->multicast_lock); 1789b195167fSHerbert Xu if (!netif_running(br->dev)) 1790b195167fSHerbert Xu goto unlock; 1791b195167fSHerbert Xu 1792b195167fSHerbert Xu err = -EINVAL; 1793b195167fSHerbert Xu if (!is_power_of_2(val)) 1794b195167fSHerbert Xu goto unlock; 1795e8051688SEric Dumazet 1796e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1797e8051688SEric Dumazet if (mdb && val < mdb->size) 1798b195167fSHerbert Xu goto unlock; 1799b195167fSHerbert Xu 1800b195167fSHerbert Xu err = 0; 1801b195167fSHerbert Xu 1802b195167fSHerbert Xu old = br->hash_max; 1803b195167fSHerbert Xu br->hash_max = val; 1804b195167fSHerbert Xu 1805e8051688SEric Dumazet if (mdb) { 1806e8051688SEric Dumazet if (mdb->old) { 1807b195167fSHerbert Xu err = -EEXIST; 1808b195167fSHerbert Xu rollback: 1809b195167fSHerbert Xu br->hash_max = old; 1810b195167fSHerbert Xu goto unlock; 1811b195167fSHerbert Xu } 1812b195167fSHerbert Xu 1813b195167fSHerbert Xu err = br_mdb_rehash(&br->mdb, br->hash_max, 1814b195167fSHerbert Xu br->hash_elasticity); 1815b195167fSHerbert Xu if (err) 1816b195167fSHerbert Xu goto rollback; 1817b195167fSHerbert Xu } 1818b195167fSHerbert Xu 1819b195167fSHerbert Xu unlock: 1820b195167fSHerbert Xu spin_unlock(&br->multicast_lock); 1821b195167fSHerbert Xu 1822b195167fSHerbert Xu return err; 1823b195167fSHerbert Xu } 1824