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 { 747c83b8fabSHerbert Xu struct net_bridge_port *port = (void *)data; 748c83b8fabSHerbert Xu struct net_bridge *br = port->br; 749c83b8fabSHerbert Xu 750c83b8fabSHerbert Xu spin_lock(&br->multicast_lock); 751c83b8fabSHerbert Xu if (!netif_running(br->dev) || br->multicast_disabled) 752c83b8fabSHerbert Xu goto out; 753c83b8fabSHerbert Xu 754c83b8fabSHerbert Xu br_multicast_start_querier(br); 755c83b8fabSHerbert Xu 756c83b8fabSHerbert Xu out: 757c83b8fabSHerbert Xu spin_unlock(&br->multicast_lock); 758c83b8fabSHerbert Xu } 759c83b8fabSHerbert Xu 7608ef2a9a5SYOSHIFUJI Hideaki static void __br_multicast_send_query(struct net_bridge *br, 7618ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 7628ef2a9a5SYOSHIFUJI Hideaki struct br_ip *ip) 763eb1d1641SHerbert Xu { 764eb1d1641SHerbert Xu struct sk_buff *skb; 765eb1d1641SHerbert Xu 7668ef2a9a5SYOSHIFUJI Hideaki skb = br_multicast_alloc_query(br, ip); 767eb1d1641SHerbert Xu if (!skb) 7688ef2a9a5SYOSHIFUJI Hideaki return; 769eb1d1641SHerbert Xu 770eb1d1641SHerbert Xu if (port) { 771eb1d1641SHerbert Xu __skb_push(skb, sizeof(struct ethhdr)); 772eb1d1641SHerbert Xu skb->dev = port->dev; 773713aefa3SJan Engelhardt NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev, 774eb1d1641SHerbert Xu dev_queue_xmit); 775eb1d1641SHerbert Xu } else 776eb1d1641SHerbert Xu netif_rx(skb); 7778ef2a9a5SYOSHIFUJI Hideaki } 778eb1d1641SHerbert Xu 7798ef2a9a5SYOSHIFUJI Hideaki static void br_multicast_send_query(struct net_bridge *br, 7808ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, u32 sent) 7818ef2a9a5SYOSHIFUJI Hideaki { 7828ef2a9a5SYOSHIFUJI Hideaki unsigned long time; 7838ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 7848ef2a9a5SYOSHIFUJI Hideaki 7858ef2a9a5SYOSHIFUJI Hideaki if (!netif_running(br->dev) || br->multicast_disabled || 786*c5c23260SHerbert Xu !br->multicast_querier || 7878ef2a9a5SYOSHIFUJI Hideaki timer_pending(&br->multicast_querier_timer)) 7888ef2a9a5SYOSHIFUJI Hideaki return; 7898ef2a9a5SYOSHIFUJI Hideaki 79008b202b6SYOSHIFUJI Hideaki memset(&br_group.u, 0, sizeof(br_group.u)); 7918ef2a9a5SYOSHIFUJI Hideaki 79208b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 7938ef2a9a5SYOSHIFUJI Hideaki __br_multicast_send_query(br, port, &br_group); 7948ef2a9a5SYOSHIFUJI Hideaki 795dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 79608b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 79708b202b6SYOSHIFUJI Hideaki __br_multicast_send_query(br, port, &br_group); 79808b202b6SYOSHIFUJI Hideaki #endif 79908b202b6SYOSHIFUJI Hideaki 800eb1d1641SHerbert Xu time = jiffies; 801eb1d1641SHerbert Xu time += sent < br->multicast_startup_query_count ? 802eb1d1641SHerbert Xu br->multicast_startup_query_interval : 803eb1d1641SHerbert Xu br->multicast_query_interval; 804eb1d1641SHerbert Xu mod_timer(port ? &port->multicast_query_timer : 805eb1d1641SHerbert Xu &br->multicast_query_timer, time); 806eb1d1641SHerbert Xu } 807eb1d1641SHerbert Xu 808eb1d1641SHerbert Xu static void br_multicast_port_query_expired(unsigned long data) 809eb1d1641SHerbert Xu { 810eb1d1641SHerbert Xu struct net_bridge_port *port = (void *)data; 811eb1d1641SHerbert Xu struct net_bridge *br = port->br; 812eb1d1641SHerbert Xu 813eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 81402a780c0SDan Carpenter if (port->state == BR_STATE_DISABLED || 81502a780c0SDan Carpenter port->state == BR_STATE_BLOCKING) 816eb1d1641SHerbert Xu goto out; 817eb1d1641SHerbert Xu 818eb1d1641SHerbert Xu if (port->multicast_startup_queries_sent < 819eb1d1641SHerbert Xu br->multicast_startup_query_count) 820eb1d1641SHerbert Xu port->multicast_startup_queries_sent++; 821eb1d1641SHerbert Xu 822eb1d1641SHerbert Xu br_multicast_send_query(port->br, port, 823eb1d1641SHerbert Xu port->multicast_startup_queries_sent); 824eb1d1641SHerbert Xu 825eb1d1641SHerbert Xu out: 826eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 827eb1d1641SHerbert Xu } 828eb1d1641SHerbert Xu 829eb1d1641SHerbert Xu void br_multicast_add_port(struct net_bridge_port *port) 830eb1d1641SHerbert Xu { 831eb1d1641SHerbert Xu port->multicast_router = 1; 832eb1d1641SHerbert Xu 833eb1d1641SHerbert Xu setup_timer(&port->multicast_router_timer, br_multicast_router_expired, 834eb1d1641SHerbert Xu (unsigned long)port); 835eb1d1641SHerbert Xu setup_timer(&port->multicast_query_timer, 836eb1d1641SHerbert Xu br_multicast_port_query_expired, (unsigned long)port); 837eb1d1641SHerbert Xu } 838eb1d1641SHerbert Xu 839eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port) 840eb1d1641SHerbert Xu { 841eb1d1641SHerbert Xu del_timer_sync(&port->multicast_router_timer); 842eb1d1641SHerbert Xu } 843eb1d1641SHerbert Xu 844561f1103SHerbert Xu static void __br_multicast_enable_port(struct net_bridge_port *port) 845561f1103SHerbert Xu { 846561f1103SHerbert Xu port->multicast_startup_queries_sent = 0; 847561f1103SHerbert Xu 848561f1103SHerbert Xu if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 || 849561f1103SHerbert Xu del_timer(&port->multicast_query_timer)) 850561f1103SHerbert Xu mod_timer(&port->multicast_query_timer, jiffies); 851561f1103SHerbert Xu } 852561f1103SHerbert Xu 853eb1d1641SHerbert Xu void br_multicast_enable_port(struct net_bridge_port *port) 854eb1d1641SHerbert Xu { 855eb1d1641SHerbert Xu struct net_bridge *br = port->br; 856eb1d1641SHerbert Xu 857eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 858eb1d1641SHerbert Xu if (br->multicast_disabled || !netif_running(br->dev)) 859eb1d1641SHerbert Xu goto out; 860eb1d1641SHerbert Xu 861561f1103SHerbert Xu __br_multicast_enable_port(port); 862eb1d1641SHerbert Xu 863eb1d1641SHerbert Xu out: 864eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 865eb1d1641SHerbert Xu } 866eb1d1641SHerbert Xu 867eb1d1641SHerbert Xu void br_multicast_disable_port(struct net_bridge_port *port) 868eb1d1641SHerbert Xu { 869eb1d1641SHerbert Xu struct net_bridge *br = port->br; 870eb1d1641SHerbert Xu struct net_bridge_port_group *pg; 871eb1d1641SHerbert Xu struct hlist_node *p, *n; 872eb1d1641SHerbert Xu 873eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 874eb1d1641SHerbert Xu hlist_for_each_entry_safe(pg, p, n, &port->mglist, mglist) 875eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 876eb1d1641SHerbert Xu 877eb1d1641SHerbert Xu if (!hlist_unhashed(&port->rlist)) 878eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 879eb1d1641SHerbert Xu del_timer(&port->multicast_router_timer); 880eb1d1641SHerbert Xu del_timer(&port->multicast_query_timer); 881eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 882eb1d1641SHerbert Xu } 883eb1d1641SHerbert Xu 8848ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_igmp3_report(struct net_bridge *br, 885eb1d1641SHerbert Xu struct net_bridge_port *port, 886eb1d1641SHerbert Xu struct sk_buff *skb) 887eb1d1641SHerbert Xu { 888eb1d1641SHerbert Xu struct igmpv3_report *ih; 889eb1d1641SHerbert Xu struct igmpv3_grec *grec; 890eb1d1641SHerbert Xu int i; 891eb1d1641SHerbert Xu int len; 892eb1d1641SHerbert Xu int num; 893eb1d1641SHerbert Xu int type; 894eb1d1641SHerbert Xu int err = 0; 895eb1d1641SHerbert Xu __be32 group; 896eb1d1641SHerbert Xu 897eb1d1641SHerbert Xu if (!pskb_may_pull(skb, sizeof(*ih))) 898eb1d1641SHerbert Xu return -EINVAL; 899eb1d1641SHerbert Xu 900eb1d1641SHerbert Xu ih = igmpv3_report_hdr(skb); 901eb1d1641SHerbert Xu num = ntohs(ih->ngrec); 902eb1d1641SHerbert Xu len = sizeof(*ih); 903eb1d1641SHerbert Xu 904eb1d1641SHerbert Xu for (i = 0; i < num; i++) { 905eb1d1641SHerbert Xu len += sizeof(*grec); 906eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 907eb1d1641SHerbert Xu return -EINVAL; 908eb1d1641SHerbert Xu 909fd218cf9SHerbert Xu grec = (void *)(skb->data + len - sizeof(*grec)); 910eb1d1641SHerbert Xu group = grec->grec_mca; 911eb1d1641SHerbert Xu type = grec->grec_type; 912eb1d1641SHerbert Xu 9138eabf95cSEric Dumazet len += ntohs(grec->grec_nsrcs) * 4; 914eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 915eb1d1641SHerbert Xu return -EINVAL; 916eb1d1641SHerbert Xu 917eb1d1641SHerbert Xu /* We treat this as an IGMPv2 report for now. */ 918eb1d1641SHerbert Xu switch (type) { 919eb1d1641SHerbert Xu case IGMPV3_MODE_IS_INCLUDE: 920eb1d1641SHerbert Xu case IGMPV3_MODE_IS_EXCLUDE: 921eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_INCLUDE: 922eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_EXCLUDE: 923eb1d1641SHerbert Xu case IGMPV3_ALLOW_NEW_SOURCES: 924eb1d1641SHerbert Xu case IGMPV3_BLOCK_OLD_SOURCES: 925eb1d1641SHerbert Xu break; 926eb1d1641SHerbert Xu 927eb1d1641SHerbert Xu default: 928eb1d1641SHerbert Xu continue; 929eb1d1641SHerbert Xu } 930eb1d1641SHerbert Xu 9318ef2a9a5SYOSHIFUJI Hideaki err = br_ip4_multicast_add_group(br, port, group); 932eb1d1641SHerbert Xu if (err) 933eb1d1641SHerbert Xu break; 934eb1d1641SHerbert Xu } 935eb1d1641SHerbert Xu 936eb1d1641SHerbert Xu return err; 937eb1d1641SHerbert Xu } 938eb1d1641SHerbert Xu 939dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 94008b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_mld2_report(struct net_bridge *br, 94108b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 94208b202b6SYOSHIFUJI Hideaki struct sk_buff *skb) 94308b202b6SYOSHIFUJI Hideaki { 94408b202b6SYOSHIFUJI Hideaki struct icmp6hdr *icmp6h; 94508b202b6SYOSHIFUJI Hideaki struct mld2_grec *grec; 94608b202b6SYOSHIFUJI Hideaki int i; 94708b202b6SYOSHIFUJI Hideaki int len; 94808b202b6SYOSHIFUJI Hideaki int num; 94908b202b6SYOSHIFUJI Hideaki int err = 0; 95008b202b6SYOSHIFUJI Hideaki 95108b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*icmp6h))) 95208b202b6SYOSHIFUJI Hideaki return -EINVAL; 95308b202b6SYOSHIFUJI Hideaki 95408b202b6SYOSHIFUJI Hideaki icmp6h = icmp6_hdr(skb); 95508b202b6SYOSHIFUJI Hideaki num = ntohs(icmp6h->icmp6_dataun.un_data16[1]); 95608b202b6SYOSHIFUJI Hideaki len = sizeof(*icmp6h); 95708b202b6SYOSHIFUJI Hideaki 95808b202b6SYOSHIFUJI Hideaki for (i = 0; i < num; i++) { 95908b202b6SYOSHIFUJI Hideaki __be16 *nsrcs, _nsrcs; 96008b202b6SYOSHIFUJI Hideaki 96108b202b6SYOSHIFUJI Hideaki nsrcs = skb_header_pointer(skb, 96208b202b6SYOSHIFUJI Hideaki len + offsetof(struct mld2_grec, 963649e984dSLinus Lüssing grec_nsrcs), 96408b202b6SYOSHIFUJI Hideaki sizeof(_nsrcs), &_nsrcs); 96508b202b6SYOSHIFUJI Hideaki if (!nsrcs) 96608b202b6SYOSHIFUJI Hideaki return -EINVAL; 96708b202b6SYOSHIFUJI Hideaki 96808b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, 96908b202b6SYOSHIFUJI Hideaki len + sizeof(*grec) + 970d41db9f3SLinus Lüssing sizeof(struct in6_addr) * ntohs(*nsrcs))) 97108b202b6SYOSHIFUJI Hideaki return -EINVAL; 97208b202b6SYOSHIFUJI Hideaki 97308b202b6SYOSHIFUJI Hideaki grec = (struct mld2_grec *)(skb->data + len); 974d41db9f3SLinus Lüssing len += sizeof(*grec) + 975d41db9f3SLinus Lüssing sizeof(struct in6_addr) * ntohs(*nsrcs); 97608b202b6SYOSHIFUJI Hideaki 97708b202b6SYOSHIFUJI Hideaki /* We treat these as MLDv1 reports for now. */ 97808b202b6SYOSHIFUJI Hideaki switch (grec->grec_type) { 97908b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_INCLUDE: 98008b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_EXCLUDE: 98108b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_INCLUDE: 98208b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_EXCLUDE: 98308b202b6SYOSHIFUJI Hideaki case MLD2_ALLOW_NEW_SOURCES: 98408b202b6SYOSHIFUJI Hideaki case MLD2_BLOCK_OLD_SOURCES: 98508b202b6SYOSHIFUJI Hideaki break; 98608b202b6SYOSHIFUJI Hideaki 98708b202b6SYOSHIFUJI Hideaki default: 98808b202b6SYOSHIFUJI Hideaki continue; 98908b202b6SYOSHIFUJI Hideaki } 99008b202b6SYOSHIFUJI Hideaki 99108b202b6SYOSHIFUJI Hideaki err = br_ip6_multicast_add_group(br, port, &grec->grec_mca); 99208b202b6SYOSHIFUJI Hideaki if (!err) 99308b202b6SYOSHIFUJI Hideaki break; 99408b202b6SYOSHIFUJI Hideaki } 99508b202b6SYOSHIFUJI Hideaki 99608b202b6SYOSHIFUJI Hideaki return err; 99708b202b6SYOSHIFUJI Hideaki } 99808b202b6SYOSHIFUJI Hideaki #endif 99908b202b6SYOSHIFUJI Hideaki 10007e80c124Sstephen hemminger /* 10017e80c124Sstephen hemminger * Add port to rotuer_list 10027e80c124Sstephen hemminger * list is maintained ordered by pointer value 10037e80c124Sstephen hemminger * and locked by br->multicast_lock and RCU 10047e80c124Sstephen hemminger */ 10050909e117SHerbert Xu static void br_multicast_add_router(struct net_bridge *br, 10060909e117SHerbert Xu struct net_bridge_port *port) 10070909e117SHerbert Xu { 1008dcdca2c4Sstephen hemminger struct net_bridge_port *p; 10097e80c124Sstephen hemminger struct hlist_node *n, *slot = NULL; 10100909e117SHerbert Xu 1011709b9326SDavid S. Miller hlist_for_each_entry(p, n, &br->router_list, rlist) { 10127e80c124Sstephen hemminger if ((unsigned long) port >= (unsigned long) p) 10137e80c124Sstephen hemminger break; 10147e80c124Sstephen hemminger slot = n; 1015dcdca2c4Sstephen hemminger } 10160909e117SHerbert Xu 10177e80c124Sstephen hemminger if (slot) 10187e80c124Sstephen hemminger hlist_add_after_rcu(slot, &port->rlist); 1019dcdca2c4Sstephen hemminger else 1020dcdca2c4Sstephen hemminger hlist_add_head_rcu(&port->rlist, &br->router_list); 10210909e117SHerbert Xu } 10220909e117SHerbert Xu 1023eb1d1641SHerbert Xu static void br_multicast_mark_router(struct net_bridge *br, 1024eb1d1641SHerbert Xu struct net_bridge_port *port) 1025eb1d1641SHerbert Xu { 1026eb1d1641SHerbert Xu unsigned long now = jiffies; 1027eb1d1641SHerbert Xu 1028eb1d1641SHerbert Xu if (!port) { 1029eb1d1641SHerbert Xu if (br->multicast_router == 1) 1030eb1d1641SHerbert Xu mod_timer(&br->multicast_router_timer, 1031eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1032eb1d1641SHerbert Xu return; 1033eb1d1641SHerbert Xu } 1034eb1d1641SHerbert Xu 1035eb1d1641SHerbert Xu if (port->multicast_router != 1) 1036eb1d1641SHerbert Xu return; 1037eb1d1641SHerbert Xu 1038eb1d1641SHerbert Xu if (!hlist_unhashed(&port->rlist)) 1039eb1d1641SHerbert Xu goto timer; 1040eb1d1641SHerbert Xu 10410909e117SHerbert Xu br_multicast_add_router(br, port); 1042eb1d1641SHerbert Xu 1043eb1d1641SHerbert Xu timer: 1044eb1d1641SHerbert Xu mod_timer(&port->multicast_router_timer, 1045eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1046eb1d1641SHerbert Xu } 1047eb1d1641SHerbert Xu 1048eb1d1641SHerbert Xu static void br_multicast_query_received(struct net_bridge *br, 1049eb1d1641SHerbert Xu struct net_bridge_port *port, 10508ef2a9a5SYOSHIFUJI Hideaki int saddr) 1051eb1d1641SHerbert Xu { 1052eb1d1641SHerbert Xu if (saddr) 1053eb1d1641SHerbert Xu mod_timer(&br->multicast_querier_timer, 1054eb1d1641SHerbert Xu jiffies + br->multicast_querier_interval); 1055eb1d1641SHerbert Xu else if (timer_pending(&br->multicast_querier_timer)) 1056eb1d1641SHerbert Xu return; 1057eb1d1641SHerbert Xu 1058eb1d1641SHerbert Xu br_multicast_mark_router(br, port); 1059eb1d1641SHerbert Xu } 1060eb1d1641SHerbert Xu 10618ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_query(struct net_bridge *br, 1062eb1d1641SHerbert Xu struct net_bridge_port *port, 1063eb1d1641SHerbert Xu struct sk_buff *skb) 1064eb1d1641SHerbert Xu { 1065b71d1d42SEric Dumazet const struct iphdr *iph = ip_hdr(skb); 1066eb1d1641SHerbert Xu struct igmphdr *ih = igmp_hdr(skb); 1067eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1068eb1d1641SHerbert Xu struct igmpv3_query *ih3; 1069eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1070e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 1071eb1d1641SHerbert Xu unsigned long max_delay; 1072eb1d1641SHerbert Xu unsigned long now = jiffies; 1073eb1d1641SHerbert Xu __be32 group; 1074bec68ff1SYOSHIFUJI Hideaki int err = 0; 1075eb1d1641SHerbert Xu 1076eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1077eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1078eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 1079eb1d1641SHerbert Xu goto out; 1080eb1d1641SHerbert Xu 10818ef2a9a5SYOSHIFUJI Hideaki br_multicast_query_received(br, port, !!iph->saddr); 1082eb1d1641SHerbert Xu 1083eb1d1641SHerbert Xu group = ih->group; 1084eb1d1641SHerbert Xu 1085eb1d1641SHerbert Xu if (skb->len == sizeof(*ih)) { 1086eb1d1641SHerbert Xu max_delay = ih->code * (HZ / IGMP_TIMER_SCALE); 1087eb1d1641SHerbert Xu 1088eb1d1641SHerbert Xu if (!max_delay) { 1089eb1d1641SHerbert Xu max_delay = 10 * HZ; 1090eb1d1641SHerbert Xu group = 0; 1091eb1d1641SHerbert Xu } 1092eb1d1641SHerbert Xu } else { 1093bec68ff1SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) { 1094bec68ff1SYOSHIFUJI Hideaki err = -EINVAL; 1095bec68ff1SYOSHIFUJI Hideaki goto out; 1096bec68ff1SYOSHIFUJI Hideaki } 1097eb1d1641SHerbert Xu 1098eb1d1641SHerbert Xu ih3 = igmpv3_query_hdr(skb); 1099eb1d1641SHerbert Xu if (ih3->nsrcs) 1100bec68ff1SYOSHIFUJI Hideaki goto out; 1101eb1d1641SHerbert Xu 11020ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 max_delay = ih3->code ? 11030ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1; 1104eb1d1641SHerbert Xu } 1105eb1d1641SHerbert Xu 1106eb1d1641SHerbert Xu if (!group) 1107eb1d1641SHerbert Xu goto out; 1108eb1d1641SHerbert Xu 1109e8051688SEric Dumazet mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group); 1110eb1d1641SHerbert Xu if (!mp) 1111eb1d1641SHerbert Xu goto out; 1112eb1d1641SHerbert Xu 1113eb1d1641SHerbert Xu max_delay *= br->multicast_last_member_count; 1114eb1d1641SHerbert Xu 11158a870178SHerbert Xu if (mp->mglist && 1116eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1117eb1d1641SHerbert Xu time_after(mp->timer.expires, now + max_delay) : 1118eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) 1119eb1d1641SHerbert Xu mod_timer(&mp->timer, now + max_delay); 1120eb1d1641SHerbert Xu 1121e8051688SEric Dumazet for (pp = &mp->ports; 1122e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 1123e8051688SEric Dumazet pp = &p->next) { 1124eb1d1641SHerbert Xu if (timer_pending(&p->timer) ? 1125eb1d1641SHerbert Xu time_after(p->timer.expires, now + max_delay) : 1126eb1d1641SHerbert Xu try_to_del_timer_sync(&p->timer) >= 0) 112724f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 1128eb1d1641SHerbert Xu } 1129eb1d1641SHerbert Xu 1130eb1d1641SHerbert Xu out: 1131eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1132bec68ff1SYOSHIFUJI Hideaki return err; 1133eb1d1641SHerbert Xu } 1134eb1d1641SHerbert Xu 1135dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 113608b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_query(struct net_bridge *br, 113708b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 113808b202b6SYOSHIFUJI Hideaki struct sk_buff *skb) 113908b202b6SYOSHIFUJI Hideaki { 1140b71d1d42SEric Dumazet const struct ipv6hdr *ip6h = ipv6_hdr(skb); 114108b202b6SYOSHIFUJI Hideaki struct mld_msg *mld = (struct mld_msg *) icmp6_hdr(skb); 114208b202b6SYOSHIFUJI Hideaki struct net_bridge_mdb_entry *mp; 114308b202b6SYOSHIFUJI Hideaki struct mld2_query *mld2q; 1144e8051688SEric Dumazet struct net_bridge_port_group *p; 1145e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 114608b202b6SYOSHIFUJI Hideaki unsigned long max_delay; 114708b202b6SYOSHIFUJI Hideaki unsigned long now = jiffies; 1148b71d1d42SEric Dumazet const struct in6_addr *group = NULL; 114908b202b6SYOSHIFUJI Hideaki int err = 0; 115008b202b6SYOSHIFUJI Hideaki 115108b202b6SYOSHIFUJI Hideaki spin_lock(&br->multicast_lock); 115208b202b6SYOSHIFUJI Hideaki if (!netif_running(br->dev) || 115308b202b6SYOSHIFUJI Hideaki (port && port->state == BR_STATE_DISABLED)) 115408b202b6SYOSHIFUJI Hideaki goto out; 115508b202b6SYOSHIFUJI Hideaki 115608b202b6SYOSHIFUJI Hideaki br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->saddr)); 115708b202b6SYOSHIFUJI Hideaki 115808b202b6SYOSHIFUJI Hideaki if (skb->len == sizeof(*mld)) { 115908b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*mld))) { 116008b202b6SYOSHIFUJI Hideaki err = -EINVAL; 116108b202b6SYOSHIFUJI Hideaki goto out; 116208b202b6SYOSHIFUJI Hideaki } 116308b202b6SYOSHIFUJI Hideaki mld = (struct mld_msg *) icmp6_hdr(skb); 116408b202b6SYOSHIFUJI Hideaki max_delay = msecs_to_jiffies(htons(mld->mld_maxdelay)); 116508b202b6SYOSHIFUJI Hideaki if (max_delay) 116608b202b6SYOSHIFUJI Hideaki group = &mld->mld_mca; 116708b202b6SYOSHIFUJI Hideaki } else if (skb->len >= sizeof(*mld2q)) { 116808b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*mld2q))) { 116908b202b6SYOSHIFUJI Hideaki err = -EINVAL; 117008b202b6SYOSHIFUJI Hideaki goto out; 117108b202b6SYOSHIFUJI Hideaki } 117208b202b6SYOSHIFUJI Hideaki mld2q = (struct mld2_query *)icmp6_hdr(skb); 117308b202b6SYOSHIFUJI Hideaki if (!mld2q->mld2q_nsrcs) 117408b202b6SYOSHIFUJI Hideaki group = &mld2q->mld2q_mca; 117508b202b6SYOSHIFUJI Hideaki max_delay = mld2q->mld2q_mrc ? MLDV2_MRC(mld2q->mld2q_mrc) : 1; 117608b202b6SYOSHIFUJI Hideaki } 117708b202b6SYOSHIFUJI Hideaki 117808b202b6SYOSHIFUJI Hideaki if (!group) 117908b202b6SYOSHIFUJI Hideaki goto out; 118008b202b6SYOSHIFUJI Hideaki 1181e8051688SEric Dumazet mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group); 118208b202b6SYOSHIFUJI Hideaki if (!mp) 118308b202b6SYOSHIFUJI Hideaki goto out; 118408b202b6SYOSHIFUJI Hideaki 118508b202b6SYOSHIFUJI Hideaki max_delay *= br->multicast_last_member_count; 11868a870178SHerbert Xu if (mp->mglist && 118708b202b6SYOSHIFUJI Hideaki (timer_pending(&mp->timer) ? 118808b202b6SYOSHIFUJI Hideaki time_after(mp->timer.expires, now + max_delay) : 118908b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&mp->timer) >= 0)) 119008b202b6SYOSHIFUJI Hideaki mod_timer(&mp->timer, now + max_delay); 119108b202b6SYOSHIFUJI Hideaki 1192e8051688SEric Dumazet for (pp = &mp->ports; 1193e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 1194e8051688SEric Dumazet pp = &p->next) { 119508b202b6SYOSHIFUJI Hideaki if (timer_pending(&p->timer) ? 119608b202b6SYOSHIFUJI Hideaki time_after(p->timer.expires, now + max_delay) : 119708b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&p->timer) >= 0) 119824f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 119908b202b6SYOSHIFUJI Hideaki } 120008b202b6SYOSHIFUJI Hideaki 120108b202b6SYOSHIFUJI Hideaki out: 120208b202b6SYOSHIFUJI Hideaki spin_unlock(&br->multicast_lock); 120308b202b6SYOSHIFUJI Hideaki return err; 120408b202b6SYOSHIFUJI Hideaki } 120508b202b6SYOSHIFUJI Hideaki #endif 120608b202b6SYOSHIFUJI Hideaki 1207eb1d1641SHerbert Xu static void br_multicast_leave_group(struct net_bridge *br, 1208eb1d1641SHerbert Xu struct net_bridge_port *port, 12098ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group) 1210eb1d1641SHerbert Xu { 1211eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 1212eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1213eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1214eb1d1641SHerbert Xu unsigned long now; 1215eb1d1641SHerbert Xu unsigned long time; 1216eb1d1641SHerbert Xu 1217eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1218eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1219eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED) || 1220eb1d1641SHerbert Xu timer_pending(&br->multicast_querier_timer)) 1221eb1d1641SHerbert Xu goto out; 1222eb1d1641SHerbert Xu 1223e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1224eb1d1641SHerbert Xu mp = br_mdb_ip_get(mdb, group); 1225eb1d1641SHerbert Xu if (!mp) 1226eb1d1641SHerbert Xu goto out; 1227eb1d1641SHerbert Xu 1228eb1d1641SHerbert Xu now = jiffies; 1229eb1d1641SHerbert Xu time = now + br->multicast_last_member_count * 1230eb1d1641SHerbert Xu br->multicast_last_member_interval; 1231eb1d1641SHerbert Xu 1232eb1d1641SHerbert Xu if (!port) { 12338a870178SHerbert Xu if (mp->mglist && 1234eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1235eb1d1641SHerbert Xu time_after(mp->timer.expires, time) : 1236eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) { 1237eb1d1641SHerbert Xu mod_timer(&mp->timer, time); 1238eb1d1641SHerbert Xu } 1239eb1d1641SHerbert Xu 1240eb1d1641SHerbert Xu goto out; 1241eb1d1641SHerbert Xu } 1242eb1d1641SHerbert Xu 1243e8051688SEric Dumazet for (p = mlock_dereference(mp->ports, br); 1244e8051688SEric Dumazet p != NULL; 1245e8051688SEric Dumazet p = mlock_dereference(p->next, br)) { 1246eb1d1641SHerbert Xu if (p->port != port) 1247eb1d1641SHerbert Xu continue; 1248eb1d1641SHerbert Xu 1249eb1d1641SHerbert Xu if (!hlist_unhashed(&p->mglist) && 1250eb1d1641SHerbert Xu (timer_pending(&p->timer) ? 1251eb1d1641SHerbert Xu time_after(p->timer.expires, time) : 1252eb1d1641SHerbert Xu try_to_del_timer_sync(&p->timer) >= 0)) { 1253eb1d1641SHerbert Xu mod_timer(&p->timer, time); 1254eb1d1641SHerbert Xu } 1255eb1d1641SHerbert Xu 1256eb1d1641SHerbert Xu break; 1257eb1d1641SHerbert Xu } 1258eb1d1641SHerbert Xu 1259eb1d1641SHerbert Xu out: 1260eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1261eb1d1641SHerbert Xu } 1262eb1d1641SHerbert Xu 12638ef2a9a5SYOSHIFUJI Hideaki static void br_ip4_multicast_leave_group(struct net_bridge *br, 12648ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 12658ef2a9a5SYOSHIFUJI Hideaki __be32 group) 12668ef2a9a5SYOSHIFUJI Hideaki { 12678ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 12688ef2a9a5SYOSHIFUJI Hideaki 12698ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 12708ef2a9a5SYOSHIFUJI Hideaki return; 12718ef2a9a5SYOSHIFUJI Hideaki 12728ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 12738ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 12748ef2a9a5SYOSHIFUJI Hideaki 12758ef2a9a5SYOSHIFUJI Hideaki br_multicast_leave_group(br, port, &br_group); 12768ef2a9a5SYOSHIFUJI Hideaki } 12778ef2a9a5SYOSHIFUJI Hideaki 1278dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 127908b202b6SYOSHIFUJI Hideaki static void br_ip6_multicast_leave_group(struct net_bridge *br, 128008b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 128108b202b6SYOSHIFUJI Hideaki const struct in6_addr *group) 128208b202b6SYOSHIFUJI Hideaki { 128308b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 128408b202b6SYOSHIFUJI Hideaki 1285e4de9f9eSLinus Lüssing if (!ipv6_is_transient_multicast(group)) 128608b202b6SYOSHIFUJI Hideaki return; 128708b202b6SYOSHIFUJI Hideaki 12884e3fd7a0SAlexey Dobriyan br_group.u.ip6 = *group; 128908b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 129008b202b6SYOSHIFUJI Hideaki 129108b202b6SYOSHIFUJI Hideaki br_multicast_leave_group(br, port, &br_group); 129208b202b6SYOSHIFUJI Hideaki } 129308b202b6SYOSHIFUJI Hideaki #endif 12948ef2a9a5SYOSHIFUJI Hideaki 1295eb1d1641SHerbert Xu static int br_multicast_ipv4_rcv(struct net_bridge *br, 1296eb1d1641SHerbert Xu struct net_bridge_port *port, 1297eb1d1641SHerbert Xu struct sk_buff *skb) 1298eb1d1641SHerbert Xu { 1299eb1d1641SHerbert Xu struct sk_buff *skb2 = skb; 1300b71d1d42SEric Dumazet const struct iphdr *iph; 1301eb1d1641SHerbert Xu struct igmphdr *ih; 130295c96174SEric Dumazet unsigned int len; 130395c96174SEric Dumazet unsigned int offset; 1304eb1d1641SHerbert Xu int err; 1305eb1d1641SHerbert Xu 1306eb1d1641SHerbert Xu /* We treat OOM as packet loss for now. */ 1307eb1d1641SHerbert Xu if (!pskb_may_pull(skb, sizeof(*iph))) 1308eb1d1641SHerbert Xu return -EINVAL; 1309eb1d1641SHerbert Xu 1310eb1d1641SHerbert Xu iph = ip_hdr(skb); 1311eb1d1641SHerbert Xu 1312eb1d1641SHerbert Xu if (iph->ihl < 5 || iph->version != 4) 1313eb1d1641SHerbert Xu return -EINVAL; 1314eb1d1641SHerbert Xu 1315eb1d1641SHerbert Xu if (!pskb_may_pull(skb, ip_hdrlen(skb))) 1316eb1d1641SHerbert Xu return -EINVAL; 1317eb1d1641SHerbert Xu 1318eb1d1641SHerbert Xu iph = ip_hdr(skb); 1319eb1d1641SHerbert Xu 1320eb1d1641SHerbert Xu if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl))) 1321eb1d1641SHerbert Xu return -EINVAL; 1322eb1d1641SHerbert Xu 1323bd4265feSHerbert Xu if (iph->protocol != IPPROTO_IGMP) { 1324bd4265feSHerbert Xu if ((iph->daddr & IGMP_LOCAL_GROUP_MASK) != IGMP_LOCAL_GROUP) 1325bd4265feSHerbert Xu BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 1326eb1d1641SHerbert Xu return 0; 1327bd4265feSHerbert Xu } 1328eb1d1641SHerbert Xu 1329eb1d1641SHerbert Xu len = ntohs(iph->tot_len); 1330eb1d1641SHerbert Xu if (skb->len < len || len < ip_hdrlen(skb)) 1331eb1d1641SHerbert Xu return -EINVAL; 1332eb1d1641SHerbert Xu 1333eb1d1641SHerbert Xu if (skb->len > len) { 1334eb1d1641SHerbert Xu skb2 = skb_clone(skb, GFP_ATOMIC); 1335eb1d1641SHerbert Xu if (!skb2) 1336eb1d1641SHerbert Xu return -ENOMEM; 1337eb1d1641SHerbert Xu 1338eb1d1641SHerbert Xu err = pskb_trim_rcsum(skb2, len); 1339eb1d1641SHerbert Xu if (err) 13408440853bSYOSHIFUJI Hideaki / 吉藤英明 goto err_out; 1341eb1d1641SHerbert Xu } 1342eb1d1641SHerbert Xu 1343eb1d1641SHerbert Xu len -= ip_hdrlen(skb2); 1344eb1d1641SHerbert Xu offset = skb_network_offset(skb2) + ip_hdrlen(skb2); 1345eb1d1641SHerbert Xu __skb_pull(skb2, offset); 1346eb1d1641SHerbert Xu skb_reset_transport_header(skb2); 1347eb1d1641SHerbert Xu 1348eb1d1641SHerbert Xu err = -EINVAL; 1349eb1d1641SHerbert Xu if (!pskb_may_pull(skb2, sizeof(*ih))) 1350eb1d1641SHerbert Xu goto out; 1351eb1d1641SHerbert Xu 1352eb1d1641SHerbert Xu switch (skb2->ip_summed) { 1353eb1d1641SHerbert Xu case CHECKSUM_COMPLETE: 1354eb1d1641SHerbert Xu if (!csum_fold(skb2->csum)) 1355eb1d1641SHerbert Xu break; 1356eb1d1641SHerbert Xu /* fall through */ 1357eb1d1641SHerbert Xu case CHECKSUM_NONE: 1358eb1d1641SHerbert Xu skb2->csum = 0; 1359eb1d1641SHerbert Xu if (skb_checksum_complete(skb2)) 13608440853bSYOSHIFUJI Hideaki / 吉藤英明 goto out; 1361eb1d1641SHerbert Xu } 1362eb1d1641SHerbert Xu 1363eb1d1641SHerbert Xu err = 0; 1364eb1d1641SHerbert Xu 1365eb1d1641SHerbert Xu BR_INPUT_SKB_CB(skb)->igmp = 1; 1366eb1d1641SHerbert Xu ih = igmp_hdr(skb2); 1367eb1d1641SHerbert Xu 1368eb1d1641SHerbert Xu switch (ih->type) { 1369eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_REPORT: 1370eb1d1641SHerbert Xu case IGMPV2_HOST_MEMBERSHIP_REPORT: 137162b2bcb4SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 13728ef2a9a5SYOSHIFUJI Hideaki err = br_ip4_multicast_add_group(br, port, ih->group); 1373eb1d1641SHerbert Xu break; 1374eb1d1641SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 13758ef2a9a5SYOSHIFUJI Hideaki err = br_ip4_multicast_igmp3_report(br, port, skb2); 1376eb1d1641SHerbert Xu break; 1377eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_QUERY: 13788ef2a9a5SYOSHIFUJI Hideaki err = br_ip4_multicast_query(br, port, skb2); 1379eb1d1641SHerbert Xu break; 1380eb1d1641SHerbert Xu case IGMP_HOST_LEAVE_MESSAGE: 13818ef2a9a5SYOSHIFUJI Hideaki br_ip4_multicast_leave_group(br, port, ih->group); 1382eb1d1641SHerbert Xu break; 1383eb1d1641SHerbert Xu } 1384eb1d1641SHerbert Xu 1385eb1d1641SHerbert Xu out: 1386eb1d1641SHerbert Xu __skb_push(skb2, offset); 13878440853bSYOSHIFUJI Hideaki / 吉藤英明 err_out: 1388eb1d1641SHerbert Xu if (skb2 != skb) 1389eb1d1641SHerbert Xu kfree_skb(skb2); 1390eb1d1641SHerbert Xu return err; 1391eb1d1641SHerbert Xu } 1392eb1d1641SHerbert Xu 1393dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 139408b202b6SYOSHIFUJI Hideaki static int br_multicast_ipv6_rcv(struct net_bridge *br, 139508b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 139608b202b6SYOSHIFUJI Hideaki struct sk_buff *skb) 139708b202b6SYOSHIFUJI Hideaki { 13989d89081dSTomas Winkler struct sk_buff *skb2; 1399b71d1d42SEric Dumazet const struct ipv6hdr *ip6h; 140022df1331SEric Dumazet u8 icmp6_type; 140108b202b6SYOSHIFUJI Hideaki u8 nexthdr; 140275f2811cSJesse Gross __be16 frag_off; 140395c96174SEric Dumazet unsigned int len; 1404bb7a0bd6SKulikov Vasiliy int offset; 140508b202b6SYOSHIFUJI Hideaki int err; 140608b202b6SYOSHIFUJI Hideaki 140708b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*ip6h))) 140808b202b6SYOSHIFUJI Hideaki return -EINVAL; 140908b202b6SYOSHIFUJI Hideaki 141008b202b6SYOSHIFUJI Hideaki ip6h = ipv6_hdr(skb); 141108b202b6SYOSHIFUJI Hideaki 141208b202b6SYOSHIFUJI Hideaki /* 141308b202b6SYOSHIFUJI Hideaki * We're interested in MLD messages only. 141408b202b6SYOSHIFUJI Hideaki * - Version is 6 141508b202b6SYOSHIFUJI Hideaki * - MLD has always Router Alert hop-by-hop option 141608b202b6SYOSHIFUJI Hideaki * - But we do not support jumbrograms. 141708b202b6SYOSHIFUJI Hideaki */ 141808b202b6SYOSHIFUJI Hideaki if (ip6h->version != 6 || 141908b202b6SYOSHIFUJI Hideaki ip6h->nexthdr != IPPROTO_HOPOPTS || 142008b202b6SYOSHIFUJI Hideaki ip6h->payload_len == 0) 142108b202b6SYOSHIFUJI Hideaki return 0; 142208b202b6SYOSHIFUJI Hideaki 1423ff9a57a6SLinus Lüssing len = ntohs(ip6h->payload_len) + sizeof(*ip6h); 142408b202b6SYOSHIFUJI Hideaki if (skb->len < len) 142508b202b6SYOSHIFUJI Hideaki return -EINVAL; 142608b202b6SYOSHIFUJI Hideaki 142708b202b6SYOSHIFUJI Hideaki nexthdr = ip6h->nexthdr; 142875f2811cSJesse Gross offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr, &frag_off); 142908b202b6SYOSHIFUJI Hideaki 143008b202b6SYOSHIFUJI Hideaki if (offset < 0 || nexthdr != IPPROTO_ICMPV6) 143108b202b6SYOSHIFUJI Hideaki return 0; 143208b202b6SYOSHIFUJI Hideaki 143308b202b6SYOSHIFUJI Hideaki /* Okay, we found ICMPv6 header */ 143408b202b6SYOSHIFUJI Hideaki skb2 = skb_clone(skb, GFP_ATOMIC); 143508b202b6SYOSHIFUJI Hideaki if (!skb2) 143608b202b6SYOSHIFUJI Hideaki return -ENOMEM; 143708b202b6SYOSHIFUJI Hideaki 14389d89081dSTomas Winkler err = -EINVAL; 14399d89081dSTomas Winkler if (!pskb_may_pull(skb2, offset + sizeof(struct icmp6hdr))) 14409d89081dSTomas Winkler goto out; 14419d89081dSTomas Winkler 144208b202b6SYOSHIFUJI Hideaki len -= offset - skb_network_offset(skb2); 144308b202b6SYOSHIFUJI Hideaki 144408b202b6SYOSHIFUJI Hideaki __skb_pull(skb2, offset); 144508b202b6SYOSHIFUJI Hideaki skb_reset_transport_header(skb2); 1446fa2da8cdSstephen hemminger skb_postpull_rcsum(skb2, skb_network_header(skb2), 1447fa2da8cdSstephen hemminger skb_network_header_len(skb2)); 144808b202b6SYOSHIFUJI Hideaki 144922df1331SEric Dumazet icmp6_type = icmp6_hdr(skb2)->icmp6_type; 145008b202b6SYOSHIFUJI Hideaki 145122df1331SEric Dumazet switch (icmp6_type) { 145208b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 145308b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 145408b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 145508b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 145608b202b6SYOSHIFUJI Hideaki break; 145708b202b6SYOSHIFUJI Hideaki default: 145808b202b6SYOSHIFUJI Hideaki err = 0; 145908b202b6SYOSHIFUJI Hideaki goto out; 146008b202b6SYOSHIFUJI Hideaki } 146108b202b6SYOSHIFUJI Hideaki 146208b202b6SYOSHIFUJI Hideaki /* Okay, we found MLD message. Check further. */ 146308b202b6SYOSHIFUJI Hideaki if (skb2->len > len) { 146408b202b6SYOSHIFUJI Hideaki err = pskb_trim_rcsum(skb2, len); 146508b202b6SYOSHIFUJI Hideaki if (err) 146608b202b6SYOSHIFUJI Hideaki goto out; 14674b275d7eSYan, Zheng err = -EINVAL; 146808b202b6SYOSHIFUJI Hideaki } 146908b202b6SYOSHIFUJI Hideaki 14704b275d7eSYan, Zheng ip6h = ipv6_hdr(skb2); 14714b275d7eSYan, Zheng 147208b202b6SYOSHIFUJI Hideaki switch (skb2->ip_summed) { 147308b202b6SYOSHIFUJI Hideaki case CHECKSUM_COMPLETE: 14744b275d7eSYan, Zheng if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, skb2->len, 14754b275d7eSYan, Zheng IPPROTO_ICMPV6, skb2->csum)) 147608b202b6SYOSHIFUJI Hideaki break; 147708b202b6SYOSHIFUJI Hideaki /*FALLTHROUGH*/ 147808b202b6SYOSHIFUJI Hideaki case CHECKSUM_NONE: 14794b275d7eSYan, Zheng skb2->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr, 14804b275d7eSYan, Zheng &ip6h->daddr, 14814b275d7eSYan, Zheng skb2->len, 14824b275d7eSYan, Zheng IPPROTO_ICMPV6, 0)); 14834b275d7eSYan, Zheng if (__skb_checksum_complete(skb2)) 148408b202b6SYOSHIFUJI Hideaki goto out; 148508b202b6SYOSHIFUJI Hideaki } 148608b202b6SYOSHIFUJI Hideaki 148708b202b6SYOSHIFUJI Hideaki err = 0; 148808b202b6SYOSHIFUJI Hideaki 148908b202b6SYOSHIFUJI Hideaki BR_INPUT_SKB_CB(skb)->igmp = 1; 149008b202b6SYOSHIFUJI Hideaki 149122df1331SEric Dumazet switch (icmp6_type) { 149208b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 149308b202b6SYOSHIFUJI Hideaki { 14949d89081dSTomas Winkler struct mld_msg *mld; 14959d89081dSTomas Winkler if (!pskb_may_pull(skb2, sizeof(*mld))) { 14969d89081dSTomas Winkler err = -EINVAL; 14979d89081dSTomas Winkler goto out; 14989d89081dSTomas Winkler } 14999d89081dSTomas Winkler mld = (struct mld_msg *)skb_transport_header(skb2); 1500fc2af6c7SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 150108b202b6SYOSHIFUJI Hideaki err = br_ip6_multicast_add_group(br, port, &mld->mld_mca); 150208b202b6SYOSHIFUJI Hideaki break; 150308b202b6SYOSHIFUJI Hideaki } 150408b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 150508b202b6SYOSHIFUJI Hideaki err = br_ip6_multicast_mld2_report(br, port, skb2); 150608b202b6SYOSHIFUJI Hideaki break; 150708b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 150808b202b6SYOSHIFUJI Hideaki err = br_ip6_multicast_query(br, port, skb2); 150908b202b6SYOSHIFUJI Hideaki break; 151008b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 151108b202b6SYOSHIFUJI Hideaki { 15129d89081dSTomas Winkler struct mld_msg *mld; 15139d89081dSTomas Winkler if (!pskb_may_pull(skb2, sizeof(*mld))) { 15149d89081dSTomas Winkler err = -EINVAL; 15159d89081dSTomas Winkler goto out; 15169d89081dSTomas Winkler } 15179d89081dSTomas Winkler mld = (struct mld_msg *)skb_transport_header(skb2); 151808b202b6SYOSHIFUJI Hideaki br_ip6_multicast_leave_group(br, port, &mld->mld_mca); 151908b202b6SYOSHIFUJI Hideaki } 152008b202b6SYOSHIFUJI Hideaki } 152108b202b6SYOSHIFUJI Hideaki 152208b202b6SYOSHIFUJI Hideaki out: 152308b202b6SYOSHIFUJI Hideaki kfree_skb(skb2); 152408b202b6SYOSHIFUJI Hideaki return err; 152508b202b6SYOSHIFUJI Hideaki } 152608b202b6SYOSHIFUJI Hideaki #endif 152708b202b6SYOSHIFUJI Hideaki 1528eb1d1641SHerbert Xu int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port, 1529eb1d1641SHerbert Xu struct sk_buff *skb) 1530eb1d1641SHerbert Xu { 15311fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->igmp = 0; 15321fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->mrouters_only = 0; 15331fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 1534eb1d1641SHerbert Xu if (br->multicast_disabled) 1535eb1d1641SHerbert Xu return 0; 1536eb1d1641SHerbert Xu 1537eb1d1641SHerbert Xu switch (skb->protocol) { 1538eb1d1641SHerbert Xu case htons(ETH_P_IP): 1539eb1d1641SHerbert Xu return br_multicast_ipv4_rcv(br, port, skb); 1540dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 154108b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 154208b202b6SYOSHIFUJI Hideaki return br_multicast_ipv6_rcv(br, port, skb); 154308b202b6SYOSHIFUJI Hideaki #endif 1544eb1d1641SHerbert Xu } 1545eb1d1641SHerbert Xu 1546eb1d1641SHerbert Xu return 0; 1547eb1d1641SHerbert Xu } 1548eb1d1641SHerbert Xu 1549eb1d1641SHerbert Xu static void br_multicast_query_expired(unsigned long data) 1550eb1d1641SHerbert Xu { 1551eb1d1641SHerbert Xu struct net_bridge *br = (void *)data; 1552eb1d1641SHerbert Xu 1553eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1554eb1d1641SHerbert Xu if (br->multicast_startup_queries_sent < 1555eb1d1641SHerbert Xu br->multicast_startup_query_count) 1556eb1d1641SHerbert Xu br->multicast_startup_queries_sent++; 1557eb1d1641SHerbert Xu 1558eb1d1641SHerbert Xu br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent); 1559eb1d1641SHerbert Xu 1560eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1561eb1d1641SHerbert Xu } 1562eb1d1641SHerbert Xu 1563eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br) 1564eb1d1641SHerbert Xu { 1565eb1d1641SHerbert Xu br->hash_elasticity = 4; 1566eb1d1641SHerbert Xu br->hash_max = 512; 1567eb1d1641SHerbert Xu 1568eb1d1641SHerbert Xu br->multicast_router = 1; 1569*c5c23260SHerbert Xu br->multicast_querier = 0; 1570eb1d1641SHerbert Xu br->multicast_last_member_count = 2; 1571eb1d1641SHerbert Xu br->multicast_startup_query_count = 2; 1572eb1d1641SHerbert Xu 1573eb1d1641SHerbert Xu br->multicast_last_member_interval = HZ; 1574eb1d1641SHerbert Xu br->multicast_query_response_interval = 10 * HZ; 1575eb1d1641SHerbert Xu br->multicast_startup_query_interval = 125 * HZ / 4; 1576eb1d1641SHerbert Xu br->multicast_query_interval = 125 * HZ; 1577eb1d1641SHerbert Xu br->multicast_querier_interval = 255 * HZ; 1578eb1d1641SHerbert Xu br->multicast_membership_interval = 260 * HZ; 1579eb1d1641SHerbert Xu 1580eb1d1641SHerbert Xu spin_lock_init(&br->multicast_lock); 1581eb1d1641SHerbert Xu setup_timer(&br->multicast_router_timer, 1582eb1d1641SHerbert Xu br_multicast_local_router_expired, 0); 1583eb1d1641SHerbert Xu setup_timer(&br->multicast_querier_timer, 1584c83b8fabSHerbert Xu br_multicast_querier_expired, 0); 1585eb1d1641SHerbert Xu setup_timer(&br->multicast_query_timer, br_multicast_query_expired, 1586eb1d1641SHerbert Xu (unsigned long)br); 1587eb1d1641SHerbert Xu } 1588eb1d1641SHerbert Xu 1589eb1d1641SHerbert Xu void br_multicast_open(struct net_bridge *br) 1590eb1d1641SHerbert Xu { 1591eb1d1641SHerbert Xu br->multicast_startup_queries_sent = 0; 1592eb1d1641SHerbert Xu 1593eb1d1641SHerbert Xu if (br->multicast_disabled) 1594eb1d1641SHerbert Xu return; 1595eb1d1641SHerbert Xu 1596eb1d1641SHerbert Xu mod_timer(&br->multicast_query_timer, jiffies); 1597eb1d1641SHerbert Xu } 1598eb1d1641SHerbert Xu 1599eb1d1641SHerbert Xu void br_multicast_stop(struct net_bridge *br) 1600eb1d1641SHerbert Xu { 1601eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 1602eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1603eb1d1641SHerbert Xu struct hlist_node *p, *n; 1604eb1d1641SHerbert Xu u32 ver; 1605eb1d1641SHerbert Xu int i; 1606eb1d1641SHerbert Xu 1607eb1d1641SHerbert Xu del_timer_sync(&br->multicast_router_timer); 1608eb1d1641SHerbert Xu del_timer_sync(&br->multicast_querier_timer); 1609eb1d1641SHerbert Xu del_timer_sync(&br->multicast_query_timer); 1610eb1d1641SHerbert Xu 1611eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1612e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1613eb1d1641SHerbert Xu if (!mdb) 1614eb1d1641SHerbert Xu goto out; 1615eb1d1641SHerbert Xu 1616eb1d1641SHerbert Xu br->mdb = NULL; 1617eb1d1641SHerbert Xu 1618eb1d1641SHerbert Xu ver = mdb->ver; 1619eb1d1641SHerbert Xu for (i = 0; i < mdb->max; i++) { 1620eb1d1641SHerbert Xu hlist_for_each_entry_safe(mp, p, n, &mdb->mhash[i], 1621eb1d1641SHerbert Xu hlist[ver]) { 1622eb1d1641SHerbert Xu del_timer(&mp->timer); 1623eb1d1641SHerbert Xu call_rcu_bh(&mp->rcu, br_multicast_free_group); 1624eb1d1641SHerbert Xu } 1625eb1d1641SHerbert Xu } 1626eb1d1641SHerbert Xu 1627eb1d1641SHerbert Xu if (mdb->old) { 1628eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 162910cc2b50SHerbert Xu rcu_barrier_bh(); 1630eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1631eb1d1641SHerbert Xu WARN_ON(mdb->old); 1632eb1d1641SHerbert Xu } 1633eb1d1641SHerbert Xu 1634eb1d1641SHerbert Xu mdb->old = mdb; 1635eb1d1641SHerbert Xu call_rcu_bh(&mdb->rcu, br_mdb_free); 1636eb1d1641SHerbert Xu 1637eb1d1641SHerbert Xu out: 1638eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 1639eb1d1641SHerbert Xu } 16400909e117SHerbert Xu 16410909e117SHerbert Xu int br_multicast_set_router(struct net_bridge *br, unsigned long val) 16420909e117SHerbert Xu { 16430909e117SHerbert Xu int err = -ENOENT; 16440909e117SHerbert Xu 16450909e117SHerbert Xu spin_lock_bh(&br->multicast_lock); 16460909e117SHerbert Xu if (!netif_running(br->dev)) 16470909e117SHerbert Xu goto unlock; 16480909e117SHerbert Xu 16490909e117SHerbert Xu switch (val) { 16500909e117SHerbert Xu case 0: 16510909e117SHerbert Xu case 2: 16520909e117SHerbert Xu del_timer(&br->multicast_router_timer); 16530909e117SHerbert Xu /* fall through */ 16540909e117SHerbert Xu case 1: 16550909e117SHerbert Xu br->multicast_router = val; 16560909e117SHerbert Xu err = 0; 16570909e117SHerbert Xu break; 16580909e117SHerbert Xu 16590909e117SHerbert Xu default: 16600909e117SHerbert Xu err = -EINVAL; 16610909e117SHerbert Xu break; 16620909e117SHerbert Xu } 16630909e117SHerbert Xu 16640909e117SHerbert Xu unlock: 16650909e117SHerbert Xu spin_unlock_bh(&br->multicast_lock); 16660909e117SHerbert Xu 16670909e117SHerbert Xu return err; 16680909e117SHerbert Xu } 16690909e117SHerbert Xu 16700909e117SHerbert Xu int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val) 16710909e117SHerbert Xu { 16720909e117SHerbert Xu struct net_bridge *br = p->br; 16730909e117SHerbert Xu int err = -ENOENT; 16740909e117SHerbert Xu 16750909e117SHerbert Xu spin_lock(&br->multicast_lock); 16760909e117SHerbert Xu if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED) 16770909e117SHerbert Xu goto unlock; 16780909e117SHerbert Xu 16790909e117SHerbert Xu switch (val) { 16800909e117SHerbert Xu case 0: 16810909e117SHerbert Xu case 1: 16820909e117SHerbert Xu case 2: 16830909e117SHerbert Xu p->multicast_router = val; 16840909e117SHerbert Xu err = 0; 16850909e117SHerbert Xu 16860909e117SHerbert Xu if (val < 2 && !hlist_unhashed(&p->rlist)) 16870909e117SHerbert Xu hlist_del_init_rcu(&p->rlist); 16880909e117SHerbert Xu 16890909e117SHerbert Xu if (val == 1) 16900909e117SHerbert Xu break; 16910909e117SHerbert Xu 16920909e117SHerbert Xu del_timer(&p->multicast_router_timer); 16930909e117SHerbert Xu 16940909e117SHerbert Xu if (val == 0) 16950909e117SHerbert Xu break; 16960909e117SHerbert Xu 16970909e117SHerbert Xu br_multicast_add_router(br, p); 16980909e117SHerbert Xu break; 16990909e117SHerbert Xu 17000909e117SHerbert Xu default: 17010909e117SHerbert Xu err = -EINVAL; 17020909e117SHerbert Xu break; 17030909e117SHerbert Xu } 17040909e117SHerbert Xu 17050909e117SHerbert Xu unlock: 17060909e117SHerbert Xu spin_unlock(&br->multicast_lock); 17070909e117SHerbert Xu 17080909e117SHerbert Xu return err; 17090909e117SHerbert Xu } 1710561f1103SHerbert Xu 171174857216SHerbert Xu static void br_multicast_start_querier(struct net_bridge *br) 1712561f1103SHerbert Xu { 1713561f1103SHerbert Xu struct net_bridge_port *port; 171474857216SHerbert Xu 171574857216SHerbert Xu br_multicast_open(br); 171674857216SHerbert Xu 171774857216SHerbert Xu list_for_each_entry(port, &br->port_list, list) { 171874857216SHerbert Xu if (port->state == BR_STATE_DISABLED || 171974857216SHerbert Xu port->state == BR_STATE_BLOCKING) 172074857216SHerbert Xu continue; 172174857216SHerbert Xu 172274857216SHerbert Xu __br_multicast_enable_port(port); 172374857216SHerbert Xu } 172474857216SHerbert Xu } 172574857216SHerbert Xu 172674857216SHerbert Xu int br_multicast_toggle(struct net_bridge *br, unsigned long val) 172774857216SHerbert Xu { 17283a7fda06SHerbert Xu int err = 0; 1729e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 1730561f1103SHerbert Xu 1731ef5e0d82SAndrey Vagin spin_lock_bh(&br->multicast_lock); 1732561f1103SHerbert Xu if (br->multicast_disabled == !val) 1733561f1103SHerbert Xu goto unlock; 1734561f1103SHerbert Xu 1735561f1103SHerbert Xu br->multicast_disabled = !val; 1736561f1103SHerbert Xu if (br->multicast_disabled) 1737561f1103SHerbert Xu goto unlock; 1738561f1103SHerbert Xu 17393a7fda06SHerbert Xu if (!netif_running(br->dev)) 17403a7fda06SHerbert Xu goto unlock; 17413a7fda06SHerbert Xu 1742e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1743e8051688SEric Dumazet if (mdb) { 1744e8051688SEric Dumazet if (mdb->old) { 1745561f1103SHerbert Xu err = -EEXIST; 1746561f1103SHerbert Xu rollback: 1747561f1103SHerbert Xu br->multicast_disabled = !!val; 1748561f1103SHerbert Xu goto unlock; 1749561f1103SHerbert Xu } 1750561f1103SHerbert Xu 1751e8051688SEric Dumazet err = br_mdb_rehash(&br->mdb, mdb->max, 1752561f1103SHerbert Xu br->hash_elasticity); 1753561f1103SHerbert Xu if (err) 1754561f1103SHerbert Xu goto rollback; 1755561f1103SHerbert Xu } 1756561f1103SHerbert Xu 175774857216SHerbert Xu br_multicast_start_querier(br); 1758561f1103SHerbert Xu 1759561f1103SHerbert Xu unlock: 1760ef5e0d82SAndrey Vagin spin_unlock_bh(&br->multicast_lock); 1761561f1103SHerbert Xu 1762561f1103SHerbert Xu return err; 1763561f1103SHerbert Xu } 1764b195167fSHerbert Xu 1765*c5c23260SHerbert Xu int br_multicast_set_querier(struct net_bridge *br, unsigned long val) 1766*c5c23260SHerbert Xu { 1767*c5c23260SHerbert Xu val = !!val; 1768*c5c23260SHerbert Xu 1769*c5c23260SHerbert Xu spin_lock_bh(&br->multicast_lock); 1770*c5c23260SHerbert Xu if (br->multicast_querier == val) 1771*c5c23260SHerbert Xu goto unlock; 1772*c5c23260SHerbert Xu 1773*c5c23260SHerbert Xu br->multicast_querier = val; 1774*c5c23260SHerbert Xu if (val) 1775*c5c23260SHerbert Xu br_multicast_start_querier(br); 1776*c5c23260SHerbert Xu 1777*c5c23260SHerbert Xu unlock: 1778*c5c23260SHerbert Xu spin_unlock_bh(&br->multicast_lock); 1779*c5c23260SHerbert Xu 1780*c5c23260SHerbert Xu return 0; 1781*c5c23260SHerbert Xu } 1782*c5c23260SHerbert Xu 1783b195167fSHerbert Xu int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val) 1784b195167fSHerbert Xu { 1785b195167fSHerbert Xu int err = -ENOENT; 1786b195167fSHerbert Xu u32 old; 1787e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 1788b195167fSHerbert Xu 1789b195167fSHerbert Xu spin_lock(&br->multicast_lock); 1790b195167fSHerbert Xu if (!netif_running(br->dev)) 1791b195167fSHerbert Xu goto unlock; 1792b195167fSHerbert Xu 1793b195167fSHerbert Xu err = -EINVAL; 1794b195167fSHerbert Xu if (!is_power_of_2(val)) 1795b195167fSHerbert Xu goto unlock; 1796e8051688SEric Dumazet 1797e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1798e8051688SEric Dumazet if (mdb && val < mdb->size) 1799b195167fSHerbert Xu goto unlock; 1800b195167fSHerbert Xu 1801b195167fSHerbert Xu err = 0; 1802b195167fSHerbert Xu 1803b195167fSHerbert Xu old = br->hash_max; 1804b195167fSHerbert Xu br->hash_max = val; 1805b195167fSHerbert Xu 1806e8051688SEric Dumazet if (mdb) { 1807e8051688SEric Dumazet if (mdb->old) { 1808b195167fSHerbert Xu err = -EEXIST; 1809b195167fSHerbert Xu rollback: 1810b195167fSHerbert Xu br->hash_max = old; 1811b195167fSHerbert Xu goto unlock; 1812b195167fSHerbert Xu } 1813b195167fSHerbert Xu 1814b195167fSHerbert Xu err = br_mdb_rehash(&br->mdb, br->hash_max, 1815b195167fSHerbert Xu br->hash_elasticity); 1816b195167fSHerbert Xu if (err) 1817b195167fSHerbert Xu goto rollback; 1818b195167fSHerbert Xu } 1819b195167fSHerbert Xu 1820b195167fSHerbert Xu unlock: 1821b195167fSHerbert Xu spin_unlock(&br->multicast_lock); 1822b195167fSHerbert Xu 1823b195167fSHerbert Xu return err; 1824b195167fSHerbert Xu } 1825