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> 30d4c4f07dSDavid S. Miller #include <net/ip6_checksum.h> 3108b202b6SYOSHIFUJI Hideaki #endif 32eb1d1641SHerbert Xu 33eb1d1641SHerbert Xu #include "br_private.h" 34eb1d1641SHerbert Xu 35c83b8fabSHerbert Xu static void br_multicast_start_querier(struct net_bridge *br); 362ce297fcSCong Wang unsigned int br_mdb_rehash_seq; 37c83b8fabSHerbert Xu 388ef2a9a5SYOSHIFUJI Hideaki static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b) 398ef2a9a5SYOSHIFUJI Hideaki { 408ef2a9a5SYOSHIFUJI Hideaki if (a->proto != b->proto) 418ef2a9a5SYOSHIFUJI Hideaki return 0; 42b0e9a30dSVlad Yasevich if (a->vid != b->vid) 43b0e9a30dSVlad Yasevich return 0; 448ef2a9a5SYOSHIFUJI Hideaki switch (a->proto) { 458ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 468ef2a9a5SYOSHIFUJI Hideaki return a->u.ip4 == b->u.ip4; 47dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 4808b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 4908b202b6SYOSHIFUJI Hideaki return ipv6_addr_equal(&a->u.ip6, &b->u.ip6); 5008b202b6SYOSHIFUJI Hideaki #endif 518ef2a9a5SYOSHIFUJI Hideaki } 528ef2a9a5SYOSHIFUJI Hideaki return 0; 538ef2a9a5SYOSHIFUJI Hideaki } 548ef2a9a5SYOSHIFUJI Hideaki 55b0e9a30dSVlad Yasevich static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip, 56b0e9a30dSVlad Yasevich __u16 vid) 57eb1d1641SHerbert Xu { 58b0e9a30dSVlad Yasevich return jhash_2words((__force u32)ip, vid, mdb->secret) & (mdb->max - 1); 59eb1d1641SHerbert Xu } 60eb1d1641SHerbert Xu 61dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 6208b202b6SYOSHIFUJI Hideaki static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb, 63b0e9a30dSVlad Yasevich const struct in6_addr *ip, 64b0e9a30dSVlad Yasevich __u16 vid) 6508b202b6SYOSHIFUJI Hideaki { 66b0e9a30dSVlad Yasevich return jhash_2words(ipv6_addr_hash(ip), vid, 67b0e9a30dSVlad Yasevich mdb->secret) & (mdb->max - 1); 6808b202b6SYOSHIFUJI Hideaki } 6908b202b6SYOSHIFUJI Hideaki #endif 7008b202b6SYOSHIFUJI Hideaki 718ef2a9a5SYOSHIFUJI Hideaki static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb, 728ef2a9a5SYOSHIFUJI Hideaki struct br_ip *ip) 738ef2a9a5SYOSHIFUJI Hideaki { 748ef2a9a5SYOSHIFUJI Hideaki switch (ip->proto) { 758ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 76b0e9a30dSVlad Yasevich return __br_ip4_hash(mdb, ip->u.ip4, ip->vid); 77dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 7808b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 79b0e9a30dSVlad Yasevich return __br_ip6_hash(mdb, &ip->u.ip6, ip->vid); 8008b202b6SYOSHIFUJI Hideaki #endif 818ef2a9a5SYOSHIFUJI Hideaki } 828ef2a9a5SYOSHIFUJI Hideaki return 0; 83eb1d1641SHerbert Xu } 84eb1d1641SHerbert Xu 85eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *__br_mdb_ip_get( 868ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash) 87eb1d1641SHerbert Xu { 88eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 89eb1d1641SHerbert Xu 90b67bfe0dSSasha Levin hlist_for_each_entry_rcu(mp, &mdb->mhash[hash], hlist[mdb->ver]) { 918ef2a9a5SYOSHIFUJI Hideaki if (br_ip_equal(&mp->addr, dst)) 92eb1d1641SHerbert Xu return mp; 93eb1d1641SHerbert Xu } 94eb1d1641SHerbert Xu 95eb1d1641SHerbert Xu return NULL; 96eb1d1641SHerbert Xu } 97eb1d1641SHerbert Xu 98cfd56754SCong Wang struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge_mdb_htable *mdb, 99cfd56754SCong Wang struct br_ip *dst) 1007f285fa7SHerbert Xu { 1017f285fa7SHerbert Xu if (!mdb) 1027f285fa7SHerbert Xu return NULL; 1037f285fa7SHerbert Xu 1047f285fa7SHerbert Xu return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst)); 1057f285fa7SHerbert Xu } 1067f285fa7SHerbert Xu 1078ef2a9a5SYOSHIFUJI Hideaki static struct net_bridge_mdb_entry *br_mdb_ip4_get( 108b0e9a30dSVlad Yasevich struct net_bridge_mdb_htable *mdb, __be32 dst, __u16 vid) 109eb1d1641SHerbert Xu { 1108ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_dst; 1110821ec55SHerbert Xu 1128ef2a9a5SYOSHIFUJI Hideaki br_dst.u.ip4 = dst; 1138ef2a9a5SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IP); 114b0e9a30dSVlad Yasevich br_dst.vid = vid; 1158ef2a9a5SYOSHIFUJI Hideaki 1167f285fa7SHerbert Xu return br_mdb_ip_get(mdb, &br_dst); 1178ef2a9a5SYOSHIFUJI Hideaki } 1188ef2a9a5SYOSHIFUJI Hideaki 119dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 12008b202b6SYOSHIFUJI Hideaki static struct net_bridge_mdb_entry *br_mdb_ip6_get( 121b0e9a30dSVlad Yasevich struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst, 122b0e9a30dSVlad Yasevich __u16 vid) 12308b202b6SYOSHIFUJI Hideaki { 12408b202b6SYOSHIFUJI Hideaki struct br_ip br_dst; 12508b202b6SYOSHIFUJI Hideaki 1264e3fd7a0SAlexey Dobriyan br_dst.u.ip6 = *dst; 12708b202b6SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IPV6); 128b0e9a30dSVlad Yasevich br_dst.vid = vid; 12908b202b6SYOSHIFUJI Hideaki 1307f285fa7SHerbert Xu return br_mdb_ip_get(mdb, &br_dst); 13108b202b6SYOSHIFUJI Hideaki } 13208b202b6SYOSHIFUJI Hideaki #endif 13308b202b6SYOSHIFUJI Hideaki 134eb1d1641SHerbert Xu struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br, 135*fbca58a2SCong Wang struct sk_buff *skb, u16 vid) 136eb1d1641SHerbert Xu { 137e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb); 1388ef2a9a5SYOSHIFUJI Hideaki struct br_ip ip; 1398ef2a9a5SYOSHIFUJI Hideaki 1407f285fa7SHerbert Xu if (br->multicast_disabled) 141eb1d1641SHerbert Xu return NULL; 142eb1d1641SHerbert Xu 1438ef2a9a5SYOSHIFUJI Hideaki if (BR_INPUT_SKB_CB(skb)->igmp) 1448ef2a9a5SYOSHIFUJI Hideaki return NULL; 1458ef2a9a5SYOSHIFUJI Hideaki 1468ef2a9a5SYOSHIFUJI Hideaki ip.proto = skb->protocol; 147*fbca58a2SCong Wang ip.vid = vid; 1488ef2a9a5SYOSHIFUJI Hideaki 149eb1d1641SHerbert Xu switch (skb->protocol) { 150eb1d1641SHerbert Xu case htons(ETH_P_IP): 1518ef2a9a5SYOSHIFUJI Hideaki ip.u.ip4 = ip_hdr(skb)->daddr; 152eb1d1641SHerbert Xu break; 153dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 15408b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 1554e3fd7a0SAlexey Dobriyan ip.u.ip6 = ipv6_hdr(skb)->daddr; 15608b202b6SYOSHIFUJI Hideaki break; 15708b202b6SYOSHIFUJI Hideaki #endif 1588ef2a9a5SYOSHIFUJI Hideaki default: 1598ef2a9a5SYOSHIFUJI Hideaki return NULL; 160eb1d1641SHerbert Xu } 161eb1d1641SHerbert Xu 1628ef2a9a5SYOSHIFUJI Hideaki return br_mdb_ip_get(mdb, &ip); 163eb1d1641SHerbert Xu } 164eb1d1641SHerbert Xu 165eb1d1641SHerbert Xu static void br_mdb_free(struct rcu_head *head) 166eb1d1641SHerbert Xu { 167eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb = 168eb1d1641SHerbert Xu container_of(head, struct net_bridge_mdb_htable, rcu); 169eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old = mdb->old; 170eb1d1641SHerbert Xu 171eb1d1641SHerbert Xu mdb->old = NULL; 172eb1d1641SHerbert Xu kfree(old->mhash); 173eb1d1641SHerbert Xu kfree(old); 174eb1d1641SHerbert Xu } 175eb1d1641SHerbert Xu 176eb1d1641SHerbert Xu static int br_mdb_copy(struct net_bridge_mdb_htable *new, 177eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old, 178eb1d1641SHerbert Xu int elasticity) 179eb1d1641SHerbert Xu { 180eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 181eb1d1641SHerbert Xu int maxlen; 182eb1d1641SHerbert Xu int len; 183eb1d1641SHerbert Xu int i; 184eb1d1641SHerbert Xu 185eb1d1641SHerbert Xu for (i = 0; i < old->max; i++) 186b67bfe0dSSasha Levin hlist_for_each_entry(mp, &old->mhash[i], hlist[old->ver]) 187eb1d1641SHerbert Xu hlist_add_head(&mp->hlist[new->ver], 1888ef2a9a5SYOSHIFUJI Hideaki &new->mhash[br_ip_hash(new, &mp->addr)]); 189eb1d1641SHerbert Xu 190eb1d1641SHerbert Xu if (!elasticity) 191eb1d1641SHerbert Xu return 0; 192eb1d1641SHerbert Xu 193eb1d1641SHerbert Xu maxlen = 0; 194eb1d1641SHerbert Xu for (i = 0; i < new->max; i++) { 195eb1d1641SHerbert Xu len = 0; 196b67bfe0dSSasha Levin hlist_for_each_entry(mp, &new->mhash[i], hlist[new->ver]) 197eb1d1641SHerbert Xu len++; 198eb1d1641SHerbert Xu if (len > maxlen) 199eb1d1641SHerbert Xu maxlen = len; 200eb1d1641SHerbert Xu } 201eb1d1641SHerbert Xu 202eb1d1641SHerbert Xu return maxlen > elasticity ? -EINVAL : 0; 203eb1d1641SHerbert Xu } 204eb1d1641SHerbert Xu 205cfd56754SCong Wang void br_multicast_free_pg(struct rcu_head *head) 206eb1d1641SHerbert Xu { 207eb1d1641SHerbert Xu struct net_bridge_port_group *p = 208eb1d1641SHerbert Xu container_of(head, struct net_bridge_port_group, rcu); 209eb1d1641SHerbert Xu 210eb1d1641SHerbert Xu kfree(p); 211eb1d1641SHerbert Xu } 212eb1d1641SHerbert Xu 213eb1d1641SHerbert Xu static void br_multicast_free_group(struct rcu_head *head) 214eb1d1641SHerbert Xu { 215eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = 216eb1d1641SHerbert Xu container_of(head, struct net_bridge_mdb_entry, rcu); 217eb1d1641SHerbert Xu 218eb1d1641SHerbert Xu kfree(mp); 219eb1d1641SHerbert Xu } 220eb1d1641SHerbert Xu 221eb1d1641SHerbert Xu static void br_multicast_group_expired(unsigned long data) 222eb1d1641SHerbert Xu { 223eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = (void *)data; 224eb1d1641SHerbert Xu struct net_bridge *br = mp->br; 225eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 226eb1d1641SHerbert Xu 227eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 228eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&mp->timer)) 229eb1d1641SHerbert Xu goto out; 230eb1d1641SHerbert Xu 2318a870178SHerbert Xu mp->mglist = false; 232eb1d1641SHerbert Xu 233eb1d1641SHerbert Xu if (mp->ports) 234eb1d1641SHerbert Xu goto out; 235eb1d1641SHerbert Xu 236e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 237e8051688SEric Dumazet 238eb1d1641SHerbert Xu hlist_del_rcu(&mp->hlist[mdb->ver]); 239eb1d1641SHerbert Xu mdb->size--; 240eb1d1641SHerbert Xu 241eb1d1641SHerbert Xu call_rcu_bh(&mp->rcu, br_multicast_free_group); 242eb1d1641SHerbert Xu 243eb1d1641SHerbert Xu out: 244eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 245eb1d1641SHerbert Xu } 246eb1d1641SHerbert Xu 247eb1d1641SHerbert Xu static void br_multicast_del_pg(struct net_bridge *br, 248eb1d1641SHerbert Xu struct net_bridge_port_group *pg) 249eb1d1641SHerbert Xu { 250e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 251eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 252eb1d1641SHerbert Xu struct net_bridge_port_group *p; 253e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 254e8051688SEric Dumazet 255e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 256eb1d1641SHerbert Xu 2578ef2a9a5SYOSHIFUJI Hideaki mp = br_mdb_ip_get(mdb, &pg->addr); 258eb1d1641SHerbert Xu if (WARN_ON(!mp)) 259eb1d1641SHerbert Xu return; 260eb1d1641SHerbert Xu 261e8051688SEric Dumazet for (pp = &mp->ports; 262e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 263e8051688SEric Dumazet pp = &p->next) { 264eb1d1641SHerbert Xu if (p != pg) 265eb1d1641SHerbert Xu continue; 266eb1d1641SHerbert Xu 26783f6a740Sstephen hemminger rcu_assign_pointer(*pp, p->next); 268eb1d1641SHerbert Xu hlist_del_init(&p->mglist); 269eb1d1641SHerbert Xu del_timer(&p->timer); 270eb1d1641SHerbert Xu call_rcu_bh(&p->rcu, br_multicast_free_pg); 271eb1d1641SHerbert Xu 2728a870178SHerbert Xu if (!mp->ports && !mp->mglist && 273eb1d1641SHerbert Xu netif_running(br->dev)) 274eb1d1641SHerbert Xu mod_timer(&mp->timer, jiffies); 275eb1d1641SHerbert Xu 276eb1d1641SHerbert Xu return; 277eb1d1641SHerbert Xu } 278eb1d1641SHerbert Xu 279eb1d1641SHerbert Xu WARN_ON(1); 280eb1d1641SHerbert Xu } 281eb1d1641SHerbert Xu 282eb1d1641SHerbert Xu static void br_multicast_port_group_expired(unsigned long data) 283eb1d1641SHerbert Xu { 284eb1d1641SHerbert Xu struct net_bridge_port_group *pg = (void *)data; 285eb1d1641SHerbert Xu struct net_bridge *br = pg->port->br; 286eb1d1641SHerbert Xu 287eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 288eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&pg->timer) || 289ccb1c31aSAmerigo Wang hlist_unhashed(&pg->mglist) || pg->state & MDB_PERMANENT) 290eb1d1641SHerbert Xu goto out; 291eb1d1641SHerbert Xu 292eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 293eb1d1641SHerbert Xu 294eb1d1641SHerbert Xu out: 295eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 296eb1d1641SHerbert Xu } 297eb1d1641SHerbert Xu 298e8051688SEric Dumazet static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max, 299eb1d1641SHerbert Xu int elasticity) 300eb1d1641SHerbert Xu { 301e8051688SEric Dumazet struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1); 302eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 303eb1d1641SHerbert Xu int err; 304eb1d1641SHerbert Xu 305eb1d1641SHerbert Xu mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC); 306eb1d1641SHerbert Xu if (!mdb) 307eb1d1641SHerbert Xu return -ENOMEM; 308eb1d1641SHerbert Xu 309eb1d1641SHerbert Xu mdb->max = max; 310eb1d1641SHerbert Xu mdb->old = old; 311eb1d1641SHerbert Xu 312eb1d1641SHerbert Xu mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC); 313eb1d1641SHerbert Xu if (!mdb->mhash) { 314eb1d1641SHerbert Xu kfree(mdb); 315eb1d1641SHerbert Xu return -ENOMEM; 316eb1d1641SHerbert Xu } 317eb1d1641SHerbert Xu 318eb1d1641SHerbert Xu mdb->size = old ? old->size : 0; 319eb1d1641SHerbert Xu mdb->ver = old ? old->ver ^ 1 : 0; 320eb1d1641SHerbert Xu 321eb1d1641SHerbert Xu if (!old || elasticity) 322eb1d1641SHerbert Xu get_random_bytes(&mdb->secret, sizeof(mdb->secret)); 323eb1d1641SHerbert Xu else 324eb1d1641SHerbert Xu mdb->secret = old->secret; 325eb1d1641SHerbert Xu 326eb1d1641SHerbert Xu if (!old) 327eb1d1641SHerbert Xu goto out; 328eb1d1641SHerbert Xu 329eb1d1641SHerbert Xu err = br_mdb_copy(mdb, old, elasticity); 330eb1d1641SHerbert Xu if (err) { 331eb1d1641SHerbert Xu kfree(mdb->mhash); 332eb1d1641SHerbert Xu kfree(mdb); 333eb1d1641SHerbert Xu return err; 334eb1d1641SHerbert Xu } 335eb1d1641SHerbert Xu 3362ce297fcSCong Wang br_mdb_rehash_seq++; 337eb1d1641SHerbert Xu call_rcu_bh(&mdb->rcu, br_mdb_free); 338eb1d1641SHerbert Xu 339eb1d1641SHerbert Xu out: 340eb1d1641SHerbert Xu rcu_assign_pointer(*mdbp, mdb); 341eb1d1641SHerbert Xu 342eb1d1641SHerbert Xu return 0; 343eb1d1641SHerbert Xu } 344eb1d1641SHerbert Xu 3458ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br, 346eb1d1641SHerbert Xu __be32 group) 347eb1d1641SHerbert Xu { 348eb1d1641SHerbert Xu struct sk_buff *skb; 349eb1d1641SHerbert Xu struct igmphdr *ih; 350eb1d1641SHerbert Xu struct ethhdr *eth; 351eb1d1641SHerbert Xu struct iphdr *iph; 352eb1d1641SHerbert Xu 353eb1d1641SHerbert Xu skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) + 354eb1d1641SHerbert Xu sizeof(*ih) + 4); 355eb1d1641SHerbert Xu if (!skb) 356eb1d1641SHerbert Xu goto out; 357eb1d1641SHerbert Xu 358eb1d1641SHerbert Xu skb->protocol = htons(ETH_P_IP); 359eb1d1641SHerbert Xu 360eb1d1641SHerbert Xu skb_reset_mac_header(skb); 361eb1d1641SHerbert Xu eth = eth_hdr(skb); 362eb1d1641SHerbert Xu 363eb1d1641SHerbert Xu memcpy(eth->h_source, br->dev->dev_addr, 6); 364eb1d1641SHerbert Xu eth->h_dest[0] = 1; 365eb1d1641SHerbert Xu eth->h_dest[1] = 0; 366eb1d1641SHerbert Xu eth->h_dest[2] = 0x5e; 367eb1d1641SHerbert Xu eth->h_dest[3] = 0; 368eb1d1641SHerbert Xu eth->h_dest[4] = 0; 369eb1d1641SHerbert Xu eth->h_dest[5] = 1; 370eb1d1641SHerbert Xu eth->h_proto = htons(ETH_P_IP); 371eb1d1641SHerbert Xu skb_put(skb, sizeof(*eth)); 372eb1d1641SHerbert Xu 373eb1d1641SHerbert Xu skb_set_network_header(skb, skb->len); 374eb1d1641SHerbert Xu iph = ip_hdr(skb); 375eb1d1641SHerbert Xu 376eb1d1641SHerbert Xu iph->version = 4; 377eb1d1641SHerbert Xu iph->ihl = 6; 378eb1d1641SHerbert Xu iph->tos = 0xc0; 379eb1d1641SHerbert Xu iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4); 380eb1d1641SHerbert Xu iph->id = 0; 381eb1d1641SHerbert Xu iph->frag_off = htons(IP_DF); 382eb1d1641SHerbert Xu iph->ttl = 1; 383eb1d1641SHerbert Xu iph->protocol = IPPROTO_IGMP; 384eb1d1641SHerbert Xu iph->saddr = 0; 385eb1d1641SHerbert Xu iph->daddr = htonl(INADDR_ALLHOSTS_GROUP); 386eb1d1641SHerbert Xu ((u8 *)&iph[1])[0] = IPOPT_RA; 387eb1d1641SHerbert Xu ((u8 *)&iph[1])[1] = 4; 388eb1d1641SHerbert Xu ((u8 *)&iph[1])[2] = 0; 389eb1d1641SHerbert Xu ((u8 *)&iph[1])[3] = 0; 390eb1d1641SHerbert Xu ip_send_check(iph); 391eb1d1641SHerbert Xu skb_put(skb, 24); 392eb1d1641SHerbert Xu 393eb1d1641SHerbert Xu skb_set_transport_header(skb, skb->len); 394eb1d1641SHerbert Xu ih = igmp_hdr(skb); 395eb1d1641SHerbert Xu ih->type = IGMP_HOST_MEMBERSHIP_QUERY; 396eb1d1641SHerbert Xu ih->code = (group ? br->multicast_last_member_interval : 397eb1d1641SHerbert Xu br->multicast_query_response_interval) / 398eb1d1641SHerbert Xu (HZ / IGMP_TIMER_SCALE); 399eb1d1641SHerbert Xu ih->group = group; 400eb1d1641SHerbert Xu ih->csum = 0; 401eb1d1641SHerbert Xu ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr)); 402eb1d1641SHerbert Xu skb_put(skb, sizeof(*ih)); 403eb1d1641SHerbert Xu 404eb1d1641SHerbert Xu __skb_pull(skb, sizeof(*eth)); 405eb1d1641SHerbert Xu 406eb1d1641SHerbert Xu out: 407eb1d1641SHerbert Xu return skb; 408eb1d1641SHerbert Xu } 409eb1d1641SHerbert Xu 410dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 41108b202b6SYOSHIFUJI Hideaki static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br, 412b71d1d42SEric Dumazet const struct in6_addr *group) 41308b202b6SYOSHIFUJI Hideaki { 41408b202b6SYOSHIFUJI Hideaki struct sk_buff *skb; 41508b202b6SYOSHIFUJI Hideaki struct ipv6hdr *ip6h; 41608b202b6SYOSHIFUJI Hideaki struct mld_msg *mldq; 41708b202b6SYOSHIFUJI Hideaki struct ethhdr *eth; 41808b202b6SYOSHIFUJI Hideaki u8 *hopopt; 41908b202b6SYOSHIFUJI Hideaki unsigned long interval; 42008b202b6SYOSHIFUJI Hideaki 42108b202b6SYOSHIFUJI Hideaki skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) + 42208b202b6SYOSHIFUJI Hideaki 8 + sizeof(*mldq)); 42308b202b6SYOSHIFUJI Hideaki if (!skb) 42408b202b6SYOSHIFUJI Hideaki goto out; 42508b202b6SYOSHIFUJI Hideaki 42608b202b6SYOSHIFUJI Hideaki skb->protocol = htons(ETH_P_IPV6); 42708b202b6SYOSHIFUJI Hideaki 42808b202b6SYOSHIFUJI Hideaki /* Ethernet header */ 42908b202b6SYOSHIFUJI Hideaki skb_reset_mac_header(skb); 43008b202b6SYOSHIFUJI Hideaki eth = eth_hdr(skb); 43108b202b6SYOSHIFUJI Hideaki 43208b202b6SYOSHIFUJI Hideaki memcpy(eth->h_source, br->dev->dev_addr, 6); 43308b202b6SYOSHIFUJI Hideaki eth->h_proto = htons(ETH_P_IPV6); 43408b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*eth)); 43508b202b6SYOSHIFUJI Hideaki 43608b202b6SYOSHIFUJI Hideaki /* IPv6 header + HbH option */ 43708b202b6SYOSHIFUJI Hideaki skb_set_network_header(skb, skb->len); 43808b202b6SYOSHIFUJI Hideaki ip6h = ipv6_hdr(skb); 43908b202b6SYOSHIFUJI Hideaki 44008b202b6SYOSHIFUJI Hideaki *(__force __be32 *)ip6h = htonl(0x60000000); 44176d66158SDavid Stevens ip6h->payload_len = htons(8 + sizeof(*mldq)); 44208b202b6SYOSHIFUJI Hideaki ip6h->nexthdr = IPPROTO_HOPOPTS; 44308b202b6SYOSHIFUJI Hideaki ip6h->hop_limit = 1; 444a7bff75bSLinus Lüssing ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1)); 445d1d81d4cSUlrich Weber if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0, 446d1d81d4cSUlrich Weber &ip6h->saddr)) { 447d1d81d4cSUlrich Weber kfree_skb(skb); 448d1d81d4cSUlrich Weber return NULL; 449d1d81d4cSUlrich Weber } 45036cff5a1SLinus Lüssing ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest); 45108b202b6SYOSHIFUJI Hideaki 45208b202b6SYOSHIFUJI Hideaki hopopt = (u8 *)(ip6h + 1); 45308b202b6SYOSHIFUJI Hideaki hopopt[0] = IPPROTO_ICMPV6; /* next hdr */ 45408b202b6SYOSHIFUJI Hideaki hopopt[1] = 0; /* length of HbH */ 45508b202b6SYOSHIFUJI Hideaki hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */ 45608b202b6SYOSHIFUJI Hideaki hopopt[3] = 2; /* Length of RA Option */ 45708b202b6SYOSHIFUJI Hideaki hopopt[4] = 0; /* Type = 0x0000 (MLD) */ 45808b202b6SYOSHIFUJI Hideaki hopopt[5] = 0; 4591de5a71cSEldad Zack hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */ 4601de5a71cSEldad Zack hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */ 46108b202b6SYOSHIFUJI Hideaki 46208b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*ip6h) + 8); 46308b202b6SYOSHIFUJI Hideaki 46408b202b6SYOSHIFUJI Hideaki /* ICMPv6 */ 46508b202b6SYOSHIFUJI Hideaki skb_set_transport_header(skb, skb->len); 46608b202b6SYOSHIFUJI Hideaki mldq = (struct mld_msg *) icmp6_hdr(skb); 46708b202b6SYOSHIFUJI Hideaki 46808b202b6SYOSHIFUJI Hideaki interval = ipv6_addr_any(group) ? br->multicast_last_member_interval : 46908b202b6SYOSHIFUJI Hideaki br->multicast_query_response_interval; 47008b202b6SYOSHIFUJI Hideaki 47108b202b6SYOSHIFUJI Hideaki mldq->mld_type = ICMPV6_MGM_QUERY; 47208b202b6SYOSHIFUJI Hideaki mldq->mld_code = 0; 47308b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = 0; 47408b202b6SYOSHIFUJI Hideaki mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval)); 47508b202b6SYOSHIFUJI Hideaki mldq->mld_reserved = 0; 4764e3fd7a0SAlexey Dobriyan mldq->mld_mca = *group; 47708b202b6SYOSHIFUJI Hideaki 47808b202b6SYOSHIFUJI Hideaki /* checksum */ 47908b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, 48008b202b6SYOSHIFUJI Hideaki sizeof(*mldq), IPPROTO_ICMPV6, 48108b202b6SYOSHIFUJI Hideaki csum_partial(mldq, 48208b202b6SYOSHIFUJI Hideaki sizeof(*mldq), 0)); 48308b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*mldq)); 48408b202b6SYOSHIFUJI Hideaki 48508b202b6SYOSHIFUJI Hideaki __skb_pull(skb, sizeof(*eth)); 48608b202b6SYOSHIFUJI Hideaki 48708b202b6SYOSHIFUJI Hideaki out: 48808b202b6SYOSHIFUJI Hideaki return skb; 48908b202b6SYOSHIFUJI Hideaki } 49008b202b6SYOSHIFUJI Hideaki #endif 49108b202b6SYOSHIFUJI Hideaki 4928ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br, 4938ef2a9a5SYOSHIFUJI Hideaki struct br_ip *addr) 4948ef2a9a5SYOSHIFUJI Hideaki { 4958ef2a9a5SYOSHIFUJI Hideaki switch (addr->proto) { 4968ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 4978ef2a9a5SYOSHIFUJI Hideaki return br_ip4_multicast_alloc_query(br, addr->u.ip4); 498dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 49908b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 50008b202b6SYOSHIFUJI Hideaki return br_ip6_multicast_alloc_query(br, &addr->u.ip6); 50108b202b6SYOSHIFUJI Hideaki #endif 5028ef2a9a5SYOSHIFUJI Hideaki } 5038ef2a9a5SYOSHIFUJI Hideaki return NULL; 5048ef2a9a5SYOSHIFUJI Hideaki } 5058ef2a9a5SYOSHIFUJI Hideaki 506eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *br_multicast_get_group( 5078ef2a9a5SYOSHIFUJI Hideaki struct net_bridge *br, struct net_bridge_port *port, 5088ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group, int hash) 509eb1d1641SHerbert Xu { 510e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 511eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 51295c96174SEric Dumazet unsigned int count = 0; 51395c96174SEric Dumazet unsigned int max; 514eb1d1641SHerbert Xu int elasticity; 515eb1d1641SHerbert Xu int err; 516eb1d1641SHerbert Xu 517e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 518b67bfe0dSSasha Levin hlist_for_each_entry(mp, &mdb->mhash[hash], hlist[mdb->ver]) { 519eb1d1641SHerbert Xu count++; 5208ef2a9a5SYOSHIFUJI Hideaki if (unlikely(br_ip_equal(group, &mp->addr))) 521eb1d1641SHerbert Xu return mp; 522eb1d1641SHerbert Xu } 523eb1d1641SHerbert Xu 524eb1d1641SHerbert Xu elasticity = 0; 525eb1d1641SHerbert Xu max = mdb->max; 526eb1d1641SHerbert Xu 527eb1d1641SHerbert Xu if (unlikely(count > br->hash_elasticity && count)) { 528eb1d1641SHerbert Xu if (net_ratelimit()) 52928a16c97Sstephen hemminger br_info(br, "Multicast hash table " 530eb1d1641SHerbert Xu "chain limit reached: %s\n", 53128a16c97Sstephen hemminger port ? port->dev->name : br->dev->name); 532eb1d1641SHerbert Xu 533eb1d1641SHerbert Xu elasticity = br->hash_elasticity; 534eb1d1641SHerbert Xu } 535eb1d1641SHerbert Xu 536eb1d1641SHerbert Xu if (mdb->size >= max) { 537eb1d1641SHerbert Xu max *= 2; 538036be6dbSThomas Graf if (unlikely(max > br->hash_max)) { 539036be6dbSThomas Graf br_warn(br, "Multicast hash table maximum of %d " 540036be6dbSThomas Graf "reached, disabling snooping: %s\n", 541036be6dbSThomas Graf br->hash_max, 542036be6dbSThomas Graf port ? port->dev->name : br->dev->name); 543eb1d1641SHerbert Xu err = -E2BIG; 544eb1d1641SHerbert Xu disable: 545eb1d1641SHerbert Xu br->multicast_disabled = 1; 546eb1d1641SHerbert Xu goto err; 547eb1d1641SHerbert Xu } 548eb1d1641SHerbert Xu } 549eb1d1641SHerbert Xu 550eb1d1641SHerbert Xu if (max > mdb->max || elasticity) { 551eb1d1641SHerbert Xu if (mdb->old) { 552eb1d1641SHerbert Xu if (net_ratelimit()) 55328a16c97Sstephen hemminger br_info(br, "Multicast hash table " 554eb1d1641SHerbert Xu "on fire: %s\n", 55528a16c97Sstephen hemminger port ? port->dev->name : br->dev->name); 556eb1d1641SHerbert Xu err = -EEXIST; 557eb1d1641SHerbert Xu goto err; 558eb1d1641SHerbert Xu } 559eb1d1641SHerbert Xu 560eb1d1641SHerbert Xu err = br_mdb_rehash(&br->mdb, max, elasticity); 561eb1d1641SHerbert Xu if (err) { 56228a16c97Sstephen hemminger br_warn(br, "Cannot rehash multicast " 56328a16c97Sstephen hemminger "hash table, disabling snooping: %s, %d, %d\n", 56428a16c97Sstephen hemminger port ? port->dev->name : br->dev->name, 565eb1d1641SHerbert Xu mdb->size, err); 566eb1d1641SHerbert Xu goto disable; 567eb1d1641SHerbert Xu } 568eb1d1641SHerbert Xu 569eb1d1641SHerbert Xu err = -EAGAIN; 570eb1d1641SHerbert Xu goto err; 571eb1d1641SHerbert Xu } 572eb1d1641SHerbert Xu 573eb1d1641SHerbert Xu return NULL; 574eb1d1641SHerbert Xu 575eb1d1641SHerbert Xu err: 576eb1d1641SHerbert Xu mp = ERR_PTR(err); 577eb1d1641SHerbert Xu return mp; 578eb1d1641SHerbert Xu } 579eb1d1641SHerbert Xu 580cfd56754SCong Wang struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br, 581cfd56754SCong Wang struct net_bridge_port *port, struct br_ip *group) 582eb1d1641SHerbert Xu { 583e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 584eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 585eb1d1641SHerbert Xu int hash; 5864c0833bcSTobias Klauser int err; 587eb1d1641SHerbert Xu 588e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 589eb1d1641SHerbert Xu if (!mdb) { 5904c0833bcSTobias Klauser err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0); 5914c0833bcSTobias Klauser if (err) 5924c0833bcSTobias Klauser return ERR_PTR(err); 593eb1d1641SHerbert Xu goto rehash; 594eb1d1641SHerbert Xu } 595eb1d1641SHerbert Xu 596eb1d1641SHerbert Xu hash = br_ip_hash(mdb, group); 597eb1d1641SHerbert Xu mp = br_multicast_get_group(br, port, group, hash); 598eb1d1641SHerbert Xu switch (PTR_ERR(mp)) { 599eb1d1641SHerbert Xu case 0: 600eb1d1641SHerbert Xu break; 601eb1d1641SHerbert Xu 602eb1d1641SHerbert Xu case -EAGAIN: 603eb1d1641SHerbert Xu rehash: 604e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 605eb1d1641SHerbert Xu hash = br_ip_hash(mdb, group); 606eb1d1641SHerbert Xu break; 607eb1d1641SHerbert Xu 608eb1d1641SHerbert Xu default: 609eb1d1641SHerbert Xu goto out; 610eb1d1641SHerbert Xu } 611eb1d1641SHerbert Xu 612eb1d1641SHerbert Xu mp = kzalloc(sizeof(*mp), GFP_ATOMIC); 613eb1d1641SHerbert Xu if (unlikely(!mp)) 6144c0833bcSTobias Klauser return ERR_PTR(-ENOMEM); 615eb1d1641SHerbert Xu 616eb1d1641SHerbert Xu mp->br = br; 6178ef2a9a5SYOSHIFUJI Hideaki mp->addr = *group; 618eb1d1641SHerbert Xu setup_timer(&mp->timer, br_multicast_group_expired, 619eb1d1641SHerbert Xu (unsigned long)mp); 620eb1d1641SHerbert Xu 621eb1d1641SHerbert Xu hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]); 622eb1d1641SHerbert Xu mdb->size++; 623eb1d1641SHerbert Xu 624eb1d1641SHerbert Xu out: 625eb1d1641SHerbert Xu return mp; 626eb1d1641SHerbert Xu } 627eb1d1641SHerbert Xu 628cfd56754SCong Wang struct net_bridge_port_group *br_multicast_new_port_group( 629cfd56754SCong Wang struct net_bridge_port *port, 630cfd56754SCong Wang struct br_ip *group, 631ccb1c31aSAmerigo Wang struct net_bridge_port_group __rcu *next, 632ccb1c31aSAmerigo Wang unsigned char state) 633cfd56754SCong Wang { 634cfd56754SCong Wang struct net_bridge_port_group *p; 635cfd56754SCong Wang 636cfd56754SCong Wang p = kzalloc(sizeof(*p), GFP_ATOMIC); 637cfd56754SCong Wang if (unlikely(!p)) 638cfd56754SCong Wang return NULL; 639cfd56754SCong Wang 640cfd56754SCong Wang p->addr = *group; 641cfd56754SCong Wang p->port = port; 642ccb1c31aSAmerigo Wang p->state = state; 643eca2a43bSstephen hemminger rcu_assign_pointer(p->next, next); 644cfd56754SCong Wang hlist_add_head(&p->mglist, &port->mglist); 645cfd56754SCong Wang setup_timer(&p->timer, br_multicast_port_group_expired, 646cfd56754SCong Wang (unsigned long)p); 647cfd56754SCong Wang return p; 648cfd56754SCong Wang } 649cfd56754SCong Wang 650eb1d1641SHerbert Xu static int br_multicast_add_group(struct net_bridge *br, 6518ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 6528ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group) 653eb1d1641SHerbert Xu { 654eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 655eb1d1641SHerbert Xu struct net_bridge_port_group *p; 656e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 657eb1d1641SHerbert Xu unsigned long now = jiffies; 658eb1d1641SHerbert Xu int err; 659eb1d1641SHerbert Xu 660eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 661eb1d1641SHerbert Xu if (!netif_running(br->dev) || 662eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 663eb1d1641SHerbert Xu goto out; 664eb1d1641SHerbert Xu 665eb1d1641SHerbert Xu mp = br_multicast_new_group(br, port, group); 666eb1d1641SHerbert Xu err = PTR_ERR(mp); 6674c0833bcSTobias Klauser if (IS_ERR(mp)) 668eb1d1641SHerbert Xu goto err; 669eb1d1641SHerbert Xu 670eb1d1641SHerbert Xu if (!port) { 6718a870178SHerbert Xu mp->mglist = true; 672eb1d1641SHerbert Xu mod_timer(&mp->timer, now + br->multicast_membership_interval); 673eb1d1641SHerbert Xu goto out; 674eb1d1641SHerbert Xu } 675eb1d1641SHerbert Xu 676e8051688SEric Dumazet for (pp = &mp->ports; 677e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 678e8051688SEric Dumazet pp = &p->next) { 679eb1d1641SHerbert Xu if (p->port == port) 680eb1d1641SHerbert Xu goto found; 681eb1d1641SHerbert Xu if ((unsigned long)p->port < (unsigned long)port) 682eb1d1641SHerbert Xu break; 683eb1d1641SHerbert Xu } 684eb1d1641SHerbert Xu 685ccb1c31aSAmerigo Wang p = br_multicast_new_port_group(port, group, *pp, MDB_TEMPORARY); 686eb1d1641SHerbert Xu if (unlikely(!p)) 687eb1d1641SHerbert Xu goto err; 688eb1d1641SHerbert Xu rcu_assign_pointer(*pp, p); 68937a393bcSCong Wang br_mdb_notify(br->dev, port, group, RTM_NEWMDB); 690eb1d1641SHerbert Xu 691eb1d1641SHerbert Xu found: 692eb1d1641SHerbert Xu mod_timer(&p->timer, now + br->multicast_membership_interval); 693eb1d1641SHerbert Xu out: 694eb1d1641SHerbert Xu err = 0; 695eb1d1641SHerbert Xu 696eb1d1641SHerbert Xu err: 697eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 698eb1d1641SHerbert Xu return err; 699eb1d1641SHerbert Xu } 700eb1d1641SHerbert Xu 7018ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_add_group(struct net_bridge *br, 7028ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 703b0e9a30dSVlad Yasevich __be32 group, 704b0e9a30dSVlad Yasevich __u16 vid) 7058ef2a9a5SYOSHIFUJI Hideaki { 7068ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 7078ef2a9a5SYOSHIFUJI Hideaki 7088ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 7098ef2a9a5SYOSHIFUJI Hideaki return 0; 7108ef2a9a5SYOSHIFUJI Hideaki 7118ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 7128ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 713b0e9a30dSVlad Yasevich br_group.vid = vid; 7148ef2a9a5SYOSHIFUJI Hideaki 7158ef2a9a5SYOSHIFUJI Hideaki return br_multicast_add_group(br, port, &br_group); 7168ef2a9a5SYOSHIFUJI Hideaki } 7178ef2a9a5SYOSHIFUJI Hideaki 718dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 71908b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_add_group(struct net_bridge *br, 72008b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 721b0e9a30dSVlad Yasevich const struct in6_addr *group, 722b0e9a30dSVlad Yasevich __u16 vid) 72308b202b6SYOSHIFUJI Hideaki { 72408b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 72508b202b6SYOSHIFUJI Hideaki 726e4de9f9eSLinus Lüssing if (!ipv6_is_transient_multicast(group)) 72708b202b6SYOSHIFUJI Hideaki return 0; 72808b202b6SYOSHIFUJI Hideaki 7294e3fd7a0SAlexey Dobriyan br_group.u.ip6 = *group; 7309cc6e0c4SLinus Lüssing br_group.proto = htons(ETH_P_IPV6); 731b0e9a30dSVlad Yasevich br_group.vid = vid; 73208b202b6SYOSHIFUJI Hideaki 73308b202b6SYOSHIFUJI Hideaki return br_multicast_add_group(br, port, &br_group); 73408b202b6SYOSHIFUJI Hideaki } 73508b202b6SYOSHIFUJI Hideaki #endif 73608b202b6SYOSHIFUJI Hideaki 737eb1d1641SHerbert Xu static void br_multicast_router_expired(unsigned long data) 738eb1d1641SHerbert Xu { 739eb1d1641SHerbert Xu struct net_bridge_port *port = (void *)data; 740eb1d1641SHerbert Xu struct net_bridge *br = port->br; 741eb1d1641SHerbert Xu 742eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 743eb1d1641SHerbert Xu if (port->multicast_router != 1 || 744eb1d1641SHerbert Xu timer_pending(&port->multicast_router_timer) || 745eb1d1641SHerbert Xu hlist_unhashed(&port->rlist)) 746eb1d1641SHerbert Xu goto out; 747eb1d1641SHerbert Xu 748eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 749eb1d1641SHerbert Xu 750eb1d1641SHerbert Xu out: 751eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 752eb1d1641SHerbert Xu } 753eb1d1641SHerbert Xu 754eb1d1641SHerbert Xu static void br_multicast_local_router_expired(unsigned long data) 755eb1d1641SHerbert Xu { 756eb1d1641SHerbert Xu } 757eb1d1641SHerbert Xu 758c83b8fabSHerbert Xu static void br_multicast_querier_expired(unsigned long data) 759c83b8fabSHerbert Xu { 760bb63f1f8SHerbert Xu struct net_bridge *br = (void *)data; 761c83b8fabSHerbert Xu 762c83b8fabSHerbert Xu spin_lock(&br->multicast_lock); 763c83b8fabSHerbert Xu if (!netif_running(br->dev) || br->multicast_disabled) 764c83b8fabSHerbert Xu goto out; 765c83b8fabSHerbert Xu 766c83b8fabSHerbert Xu br_multicast_start_querier(br); 767c83b8fabSHerbert Xu 768c83b8fabSHerbert Xu out: 769c83b8fabSHerbert Xu spin_unlock(&br->multicast_lock); 770c83b8fabSHerbert Xu } 771c83b8fabSHerbert Xu 7728ef2a9a5SYOSHIFUJI Hideaki static void __br_multicast_send_query(struct net_bridge *br, 7738ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 7748ef2a9a5SYOSHIFUJI Hideaki struct br_ip *ip) 775eb1d1641SHerbert Xu { 776eb1d1641SHerbert Xu struct sk_buff *skb; 777eb1d1641SHerbert Xu 7788ef2a9a5SYOSHIFUJI Hideaki skb = br_multicast_alloc_query(br, ip); 779eb1d1641SHerbert Xu if (!skb) 7808ef2a9a5SYOSHIFUJI Hideaki return; 781eb1d1641SHerbert Xu 782eb1d1641SHerbert Xu if (port) { 783eb1d1641SHerbert Xu __skb_push(skb, sizeof(struct ethhdr)); 784eb1d1641SHerbert Xu skb->dev = port->dev; 785713aefa3SJan Engelhardt NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev, 786eb1d1641SHerbert Xu dev_queue_xmit); 787eb1d1641SHerbert Xu } else 788eb1d1641SHerbert Xu netif_rx(skb); 7898ef2a9a5SYOSHIFUJI Hideaki } 790eb1d1641SHerbert Xu 7918ef2a9a5SYOSHIFUJI Hideaki static void br_multicast_send_query(struct net_bridge *br, 7928ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, u32 sent) 7938ef2a9a5SYOSHIFUJI Hideaki { 7948ef2a9a5SYOSHIFUJI Hideaki unsigned long time; 7958ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 7968ef2a9a5SYOSHIFUJI Hideaki 7978ef2a9a5SYOSHIFUJI Hideaki if (!netif_running(br->dev) || br->multicast_disabled || 798c5c23260SHerbert Xu !br->multicast_querier || 7998ef2a9a5SYOSHIFUJI Hideaki timer_pending(&br->multicast_querier_timer)) 8008ef2a9a5SYOSHIFUJI Hideaki return; 8018ef2a9a5SYOSHIFUJI Hideaki 80208b202b6SYOSHIFUJI Hideaki memset(&br_group.u, 0, sizeof(br_group.u)); 8038ef2a9a5SYOSHIFUJI Hideaki 80408b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 8058ef2a9a5SYOSHIFUJI Hideaki __br_multicast_send_query(br, port, &br_group); 8068ef2a9a5SYOSHIFUJI Hideaki 807dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 80808b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 80908b202b6SYOSHIFUJI Hideaki __br_multicast_send_query(br, port, &br_group); 81008b202b6SYOSHIFUJI Hideaki #endif 81108b202b6SYOSHIFUJI Hideaki 812eb1d1641SHerbert Xu time = jiffies; 813eb1d1641SHerbert Xu time += sent < br->multicast_startup_query_count ? 814eb1d1641SHerbert Xu br->multicast_startup_query_interval : 815eb1d1641SHerbert Xu br->multicast_query_interval; 816eb1d1641SHerbert Xu mod_timer(port ? &port->multicast_query_timer : 817eb1d1641SHerbert Xu &br->multicast_query_timer, time); 818eb1d1641SHerbert Xu } 819eb1d1641SHerbert Xu 820eb1d1641SHerbert Xu static void br_multicast_port_query_expired(unsigned long data) 821eb1d1641SHerbert Xu { 822eb1d1641SHerbert Xu struct net_bridge_port *port = (void *)data; 823eb1d1641SHerbert Xu struct net_bridge *br = port->br; 824eb1d1641SHerbert Xu 825eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 82602a780c0SDan Carpenter if (port->state == BR_STATE_DISABLED || 82702a780c0SDan Carpenter port->state == BR_STATE_BLOCKING) 828eb1d1641SHerbert Xu goto out; 829eb1d1641SHerbert Xu 830eb1d1641SHerbert Xu if (port->multicast_startup_queries_sent < 831eb1d1641SHerbert Xu br->multicast_startup_query_count) 832eb1d1641SHerbert Xu port->multicast_startup_queries_sent++; 833eb1d1641SHerbert Xu 834eb1d1641SHerbert Xu br_multicast_send_query(port->br, port, 835eb1d1641SHerbert Xu port->multicast_startup_queries_sent); 836eb1d1641SHerbert Xu 837eb1d1641SHerbert Xu out: 838eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 839eb1d1641SHerbert Xu } 840eb1d1641SHerbert Xu 841eb1d1641SHerbert Xu void br_multicast_add_port(struct net_bridge_port *port) 842eb1d1641SHerbert Xu { 843eb1d1641SHerbert Xu port->multicast_router = 1; 844eb1d1641SHerbert Xu 845eb1d1641SHerbert Xu setup_timer(&port->multicast_router_timer, br_multicast_router_expired, 846eb1d1641SHerbert Xu (unsigned long)port); 847eb1d1641SHerbert Xu setup_timer(&port->multicast_query_timer, 848eb1d1641SHerbert Xu br_multicast_port_query_expired, (unsigned long)port); 849eb1d1641SHerbert Xu } 850eb1d1641SHerbert Xu 851eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port) 852eb1d1641SHerbert Xu { 853eb1d1641SHerbert Xu del_timer_sync(&port->multicast_router_timer); 854eb1d1641SHerbert Xu } 855eb1d1641SHerbert Xu 856561f1103SHerbert Xu static void __br_multicast_enable_port(struct net_bridge_port *port) 857561f1103SHerbert Xu { 858561f1103SHerbert Xu port->multicast_startup_queries_sent = 0; 859561f1103SHerbert Xu 860561f1103SHerbert Xu if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 || 861561f1103SHerbert Xu del_timer(&port->multicast_query_timer)) 862561f1103SHerbert Xu mod_timer(&port->multicast_query_timer, jiffies); 863561f1103SHerbert Xu } 864561f1103SHerbert Xu 865eb1d1641SHerbert Xu void br_multicast_enable_port(struct net_bridge_port *port) 866eb1d1641SHerbert Xu { 867eb1d1641SHerbert Xu struct net_bridge *br = port->br; 868eb1d1641SHerbert Xu 869eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 870eb1d1641SHerbert Xu if (br->multicast_disabled || !netif_running(br->dev)) 871eb1d1641SHerbert Xu goto out; 872eb1d1641SHerbert Xu 873561f1103SHerbert Xu __br_multicast_enable_port(port); 874eb1d1641SHerbert Xu 875eb1d1641SHerbert Xu out: 876eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 877eb1d1641SHerbert Xu } 878eb1d1641SHerbert Xu 879eb1d1641SHerbert Xu void br_multicast_disable_port(struct net_bridge_port *port) 880eb1d1641SHerbert Xu { 881eb1d1641SHerbert Xu struct net_bridge *br = port->br; 882eb1d1641SHerbert Xu struct net_bridge_port_group *pg; 883b67bfe0dSSasha Levin struct hlist_node *n; 884eb1d1641SHerbert Xu 885eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 886b67bfe0dSSasha Levin hlist_for_each_entry_safe(pg, n, &port->mglist, mglist) 887eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 888eb1d1641SHerbert Xu 889eb1d1641SHerbert Xu if (!hlist_unhashed(&port->rlist)) 890eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 891eb1d1641SHerbert Xu del_timer(&port->multicast_router_timer); 892eb1d1641SHerbert Xu del_timer(&port->multicast_query_timer); 893eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 894eb1d1641SHerbert Xu } 895eb1d1641SHerbert Xu 8968ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_igmp3_report(struct net_bridge *br, 897eb1d1641SHerbert Xu struct net_bridge_port *port, 898eb1d1641SHerbert Xu struct sk_buff *skb) 899eb1d1641SHerbert Xu { 900eb1d1641SHerbert Xu struct igmpv3_report *ih; 901eb1d1641SHerbert Xu struct igmpv3_grec *grec; 902eb1d1641SHerbert Xu int i; 903eb1d1641SHerbert Xu int len; 904eb1d1641SHerbert Xu int num; 905eb1d1641SHerbert Xu int type; 906eb1d1641SHerbert Xu int err = 0; 907eb1d1641SHerbert Xu __be32 group; 908b0e9a30dSVlad Yasevich u16 vid = 0; 909eb1d1641SHerbert Xu 910eb1d1641SHerbert Xu if (!pskb_may_pull(skb, sizeof(*ih))) 911eb1d1641SHerbert Xu return -EINVAL; 912eb1d1641SHerbert Xu 913b0e9a30dSVlad Yasevich br_vlan_get_tag(skb, &vid); 914eb1d1641SHerbert Xu ih = igmpv3_report_hdr(skb); 915eb1d1641SHerbert Xu num = ntohs(ih->ngrec); 916eb1d1641SHerbert Xu len = sizeof(*ih); 917eb1d1641SHerbert Xu 918eb1d1641SHerbert Xu for (i = 0; i < num; i++) { 919eb1d1641SHerbert Xu len += sizeof(*grec); 920eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 921eb1d1641SHerbert Xu return -EINVAL; 922eb1d1641SHerbert Xu 923fd218cf9SHerbert Xu grec = (void *)(skb->data + len - sizeof(*grec)); 924eb1d1641SHerbert Xu group = grec->grec_mca; 925eb1d1641SHerbert Xu type = grec->grec_type; 926eb1d1641SHerbert Xu 9278eabf95cSEric Dumazet len += ntohs(grec->grec_nsrcs) * 4; 928eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 929eb1d1641SHerbert Xu return -EINVAL; 930eb1d1641SHerbert Xu 931eb1d1641SHerbert Xu /* We treat this as an IGMPv2 report for now. */ 932eb1d1641SHerbert Xu switch (type) { 933eb1d1641SHerbert Xu case IGMPV3_MODE_IS_INCLUDE: 934eb1d1641SHerbert Xu case IGMPV3_MODE_IS_EXCLUDE: 935eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_INCLUDE: 936eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_EXCLUDE: 937eb1d1641SHerbert Xu case IGMPV3_ALLOW_NEW_SOURCES: 938eb1d1641SHerbert Xu case IGMPV3_BLOCK_OLD_SOURCES: 939eb1d1641SHerbert Xu break; 940eb1d1641SHerbert Xu 941eb1d1641SHerbert Xu default: 942eb1d1641SHerbert Xu continue; 943eb1d1641SHerbert Xu } 944eb1d1641SHerbert Xu 945b0e9a30dSVlad Yasevich err = br_ip4_multicast_add_group(br, port, group, vid); 946eb1d1641SHerbert Xu if (err) 947eb1d1641SHerbert Xu break; 948eb1d1641SHerbert Xu } 949eb1d1641SHerbert Xu 950eb1d1641SHerbert Xu return err; 951eb1d1641SHerbert Xu } 952eb1d1641SHerbert Xu 953dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 95408b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_mld2_report(struct net_bridge *br, 95508b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 95608b202b6SYOSHIFUJI Hideaki struct sk_buff *skb) 95708b202b6SYOSHIFUJI Hideaki { 95808b202b6SYOSHIFUJI Hideaki struct icmp6hdr *icmp6h; 95908b202b6SYOSHIFUJI Hideaki struct mld2_grec *grec; 96008b202b6SYOSHIFUJI Hideaki int i; 96108b202b6SYOSHIFUJI Hideaki int len; 96208b202b6SYOSHIFUJI Hideaki int num; 96308b202b6SYOSHIFUJI Hideaki int err = 0; 964b0e9a30dSVlad Yasevich u16 vid = 0; 96508b202b6SYOSHIFUJI Hideaki 96608b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*icmp6h))) 96708b202b6SYOSHIFUJI Hideaki return -EINVAL; 96808b202b6SYOSHIFUJI Hideaki 969b0e9a30dSVlad Yasevich br_vlan_get_tag(skb, &vid); 97008b202b6SYOSHIFUJI Hideaki icmp6h = icmp6_hdr(skb); 97108b202b6SYOSHIFUJI Hideaki num = ntohs(icmp6h->icmp6_dataun.un_data16[1]); 97208b202b6SYOSHIFUJI Hideaki len = sizeof(*icmp6h); 97308b202b6SYOSHIFUJI Hideaki 97408b202b6SYOSHIFUJI Hideaki for (i = 0; i < num; i++) { 97508b202b6SYOSHIFUJI Hideaki __be16 *nsrcs, _nsrcs; 97608b202b6SYOSHIFUJI Hideaki 97708b202b6SYOSHIFUJI Hideaki nsrcs = skb_header_pointer(skb, 97808b202b6SYOSHIFUJI Hideaki len + offsetof(struct mld2_grec, 979649e984dSLinus Lüssing grec_nsrcs), 98008b202b6SYOSHIFUJI Hideaki sizeof(_nsrcs), &_nsrcs); 98108b202b6SYOSHIFUJI Hideaki if (!nsrcs) 98208b202b6SYOSHIFUJI Hideaki return -EINVAL; 98308b202b6SYOSHIFUJI Hideaki 98408b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, 98508b202b6SYOSHIFUJI Hideaki len + sizeof(*grec) + 986d41db9f3SLinus Lüssing sizeof(struct in6_addr) * ntohs(*nsrcs))) 98708b202b6SYOSHIFUJI Hideaki return -EINVAL; 98808b202b6SYOSHIFUJI Hideaki 98908b202b6SYOSHIFUJI Hideaki grec = (struct mld2_grec *)(skb->data + len); 990d41db9f3SLinus Lüssing len += sizeof(*grec) + 991d41db9f3SLinus Lüssing sizeof(struct in6_addr) * ntohs(*nsrcs); 99208b202b6SYOSHIFUJI Hideaki 99308b202b6SYOSHIFUJI Hideaki /* We treat these as MLDv1 reports for now. */ 99408b202b6SYOSHIFUJI Hideaki switch (grec->grec_type) { 99508b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_INCLUDE: 99608b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_EXCLUDE: 99708b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_INCLUDE: 99808b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_EXCLUDE: 99908b202b6SYOSHIFUJI Hideaki case MLD2_ALLOW_NEW_SOURCES: 100008b202b6SYOSHIFUJI Hideaki case MLD2_BLOCK_OLD_SOURCES: 100108b202b6SYOSHIFUJI Hideaki break; 100208b202b6SYOSHIFUJI Hideaki 100308b202b6SYOSHIFUJI Hideaki default: 100408b202b6SYOSHIFUJI Hideaki continue; 100508b202b6SYOSHIFUJI Hideaki } 100608b202b6SYOSHIFUJI Hideaki 1007b0e9a30dSVlad Yasevich err = br_ip6_multicast_add_group(br, port, &grec->grec_mca, 1008b0e9a30dSVlad Yasevich vid); 100908b202b6SYOSHIFUJI Hideaki if (!err) 101008b202b6SYOSHIFUJI Hideaki break; 101108b202b6SYOSHIFUJI Hideaki } 101208b202b6SYOSHIFUJI Hideaki 101308b202b6SYOSHIFUJI Hideaki return err; 101408b202b6SYOSHIFUJI Hideaki } 101508b202b6SYOSHIFUJI Hideaki #endif 101608b202b6SYOSHIFUJI Hideaki 10177e80c124Sstephen hemminger /* 10187e80c124Sstephen hemminger * Add port to rotuer_list 10197e80c124Sstephen hemminger * list is maintained ordered by pointer value 10207e80c124Sstephen hemminger * and locked by br->multicast_lock and RCU 10217e80c124Sstephen hemminger */ 10220909e117SHerbert Xu static void br_multicast_add_router(struct net_bridge *br, 10230909e117SHerbert Xu struct net_bridge_port *port) 10240909e117SHerbert Xu { 1025dcdca2c4Sstephen hemminger struct net_bridge_port *p; 1026b67bfe0dSSasha Levin struct hlist_node *slot = NULL; 10270909e117SHerbert Xu 1028b67bfe0dSSasha Levin hlist_for_each_entry(p, &br->router_list, rlist) { 10297e80c124Sstephen hemminger if ((unsigned long) port >= (unsigned long) p) 10307e80c124Sstephen hemminger break; 1031b67bfe0dSSasha Levin slot = &p->rlist; 1032dcdca2c4Sstephen hemminger } 10330909e117SHerbert Xu 10347e80c124Sstephen hemminger if (slot) 10357e80c124Sstephen hemminger hlist_add_after_rcu(slot, &port->rlist); 1036dcdca2c4Sstephen hemminger else 1037dcdca2c4Sstephen hemminger hlist_add_head_rcu(&port->rlist, &br->router_list); 10380909e117SHerbert Xu } 10390909e117SHerbert Xu 1040eb1d1641SHerbert Xu static void br_multicast_mark_router(struct net_bridge *br, 1041eb1d1641SHerbert Xu struct net_bridge_port *port) 1042eb1d1641SHerbert Xu { 1043eb1d1641SHerbert Xu unsigned long now = jiffies; 1044eb1d1641SHerbert Xu 1045eb1d1641SHerbert Xu if (!port) { 1046eb1d1641SHerbert Xu if (br->multicast_router == 1) 1047eb1d1641SHerbert Xu mod_timer(&br->multicast_router_timer, 1048eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1049eb1d1641SHerbert Xu return; 1050eb1d1641SHerbert Xu } 1051eb1d1641SHerbert Xu 1052eb1d1641SHerbert Xu if (port->multicast_router != 1) 1053eb1d1641SHerbert Xu return; 1054eb1d1641SHerbert Xu 1055eb1d1641SHerbert Xu if (!hlist_unhashed(&port->rlist)) 1056eb1d1641SHerbert Xu goto timer; 1057eb1d1641SHerbert Xu 10580909e117SHerbert Xu br_multicast_add_router(br, port); 1059eb1d1641SHerbert Xu 1060eb1d1641SHerbert Xu timer: 1061eb1d1641SHerbert Xu mod_timer(&port->multicast_router_timer, 1062eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1063eb1d1641SHerbert Xu } 1064eb1d1641SHerbert Xu 1065eb1d1641SHerbert Xu static void br_multicast_query_received(struct net_bridge *br, 1066eb1d1641SHerbert Xu struct net_bridge_port *port, 10678ef2a9a5SYOSHIFUJI Hideaki int saddr) 1068eb1d1641SHerbert Xu { 1069eb1d1641SHerbert Xu if (saddr) 1070eb1d1641SHerbert Xu mod_timer(&br->multicast_querier_timer, 1071eb1d1641SHerbert Xu jiffies + br->multicast_querier_interval); 1072eb1d1641SHerbert Xu else if (timer_pending(&br->multicast_querier_timer)) 1073eb1d1641SHerbert Xu return; 1074eb1d1641SHerbert Xu 1075eb1d1641SHerbert Xu br_multicast_mark_router(br, port); 1076eb1d1641SHerbert Xu } 1077eb1d1641SHerbert Xu 10788ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_query(struct net_bridge *br, 1079eb1d1641SHerbert Xu struct net_bridge_port *port, 1080eb1d1641SHerbert Xu struct sk_buff *skb) 1081eb1d1641SHerbert Xu { 1082b71d1d42SEric Dumazet const struct iphdr *iph = ip_hdr(skb); 1083eb1d1641SHerbert Xu struct igmphdr *ih = igmp_hdr(skb); 1084eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1085eb1d1641SHerbert Xu struct igmpv3_query *ih3; 1086eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1087e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 1088eb1d1641SHerbert Xu unsigned long max_delay; 1089eb1d1641SHerbert Xu unsigned long now = jiffies; 1090eb1d1641SHerbert Xu __be32 group; 1091bec68ff1SYOSHIFUJI Hideaki int err = 0; 1092b0e9a30dSVlad Yasevich u16 vid = 0; 1093eb1d1641SHerbert Xu 1094eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1095eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1096eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 1097eb1d1641SHerbert Xu goto out; 1098eb1d1641SHerbert Xu 10998ef2a9a5SYOSHIFUJI Hideaki br_multicast_query_received(br, port, !!iph->saddr); 1100eb1d1641SHerbert Xu 1101eb1d1641SHerbert Xu group = ih->group; 1102eb1d1641SHerbert Xu 1103eb1d1641SHerbert Xu if (skb->len == sizeof(*ih)) { 1104eb1d1641SHerbert Xu max_delay = ih->code * (HZ / IGMP_TIMER_SCALE); 1105eb1d1641SHerbert Xu 1106eb1d1641SHerbert Xu if (!max_delay) { 1107eb1d1641SHerbert Xu max_delay = 10 * HZ; 1108eb1d1641SHerbert Xu group = 0; 1109eb1d1641SHerbert Xu } 1110eb1d1641SHerbert Xu } else { 1111bec68ff1SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) { 1112bec68ff1SYOSHIFUJI Hideaki err = -EINVAL; 1113bec68ff1SYOSHIFUJI Hideaki goto out; 1114bec68ff1SYOSHIFUJI Hideaki } 1115eb1d1641SHerbert Xu 1116eb1d1641SHerbert Xu ih3 = igmpv3_query_hdr(skb); 1117eb1d1641SHerbert Xu if (ih3->nsrcs) 1118bec68ff1SYOSHIFUJI Hideaki goto out; 1119eb1d1641SHerbert Xu 11200ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 max_delay = ih3->code ? 11210ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1; 1122eb1d1641SHerbert Xu } 1123eb1d1641SHerbert Xu 1124eb1d1641SHerbert Xu if (!group) 1125eb1d1641SHerbert Xu goto out; 1126eb1d1641SHerbert Xu 1127b0e9a30dSVlad Yasevich br_vlan_get_tag(skb, &vid); 1128b0e9a30dSVlad Yasevich mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid); 1129eb1d1641SHerbert Xu if (!mp) 1130eb1d1641SHerbert Xu goto out; 1131eb1d1641SHerbert Xu 1132eb1d1641SHerbert Xu max_delay *= br->multicast_last_member_count; 1133eb1d1641SHerbert Xu 11348a870178SHerbert Xu if (mp->mglist && 1135eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1136eb1d1641SHerbert Xu time_after(mp->timer.expires, now + max_delay) : 1137eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) 1138eb1d1641SHerbert Xu mod_timer(&mp->timer, now + max_delay); 1139eb1d1641SHerbert Xu 1140e8051688SEric Dumazet for (pp = &mp->ports; 1141e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 1142e8051688SEric Dumazet pp = &p->next) { 1143eb1d1641SHerbert Xu if (timer_pending(&p->timer) ? 1144eb1d1641SHerbert Xu time_after(p->timer.expires, now + max_delay) : 1145eb1d1641SHerbert Xu try_to_del_timer_sync(&p->timer) >= 0) 114624f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 1147eb1d1641SHerbert Xu } 1148eb1d1641SHerbert Xu 1149eb1d1641SHerbert Xu out: 1150eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1151bec68ff1SYOSHIFUJI Hideaki return err; 1152eb1d1641SHerbert Xu } 1153eb1d1641SHerbert Xu 1154dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 115508b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_query(struct net_bridge *br, 115608b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 115708b202b6SYOSHIFUJI Hideaki struct sk_buff *skb) 115808b202b6SYOSHIFUJI Hideaki { 1159b71d1d42SEric Dumazet const struct ipv6hdr *ip6h = ipv6_hdr(skb); 1160eca2a43bSstephen hemminger struct mld_msg *mld; 116108b202b6SYOSHIFUJI Hideaki struct net_bridge_mdb_entry *mp; 116208b202b6SYOSHIFUJI Hideaki struct mld2_query *mld2q; 1163e8051688SEric Dumazet struct net_bridge_port_group *p; 1164e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 116508b202b6SYOSHIFUJI Hideaki unsigned long max_delay; 116608b202b6SYOSHIFUJI Hideaki unsigned long now = jiffies; 1167b71d1d42SEric Dumazet const struct in6_addr *group = NULL; 116808b202b6SYOSHIFUJI Hideaki int err = 0; 1169b0e9a30dSVlad Yasevich u16 vid = 0; 117008b202b6SYOSHIFUJI Hideaki 117108b202b6SYOSHIFUJI Hideaki spin_lock(&br->multicast_lock); 117208b202b6SYOSHIFUJI Hideaki if (!netif_running(br->dev) || 117308b202b6SYOSHIFUJI Hideaki (port && port->state == BR_STATE_DISABLED)) 117408b202b6SYOSHIFUJI Hideaki goto out; 117508b202b6SYOSHIFUJI Hideaki 117608b202b6SYOSHIFUJI Hideaki br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->saddr)); 117708b202b6SYOSHIFUJI Hideaki 117808b202b6SYOSHIFUJI Hideaki if (skb->len == sizeof(*mld)) { 117908b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*mld))) { 118008b202b6SYOSHIFUJI Hideaki err = -EINVAL; 118108b202b6SYOSHIFUJI Hideaki goto out; 118208b202b6SYOSHIFUJI Hideaki } 118308b202b6SYOSHIFUJI Hideaki mld = (struct mld_msg *) icmp6_hdr(skb); 11844715213dSLi RongQing max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay)); 118508b202b6SYOSHIFUJI Hideaki if (max_delay) 118608b202b6SYOSHIFUJI Hideaki group = &mld->mld_mca; 118708b202b6SYOSHIFUJI Hideaki } else if (skb->len >= sizeof(*mld2q)) { 118808b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*mld2q))) { 118908b202b6SYOSHIFUJI Hideaki err = -EINVAL; 119008b202b6SYOSHIFUJI Hideaki goto out; 119108b202b6SYOSHIFUJI Hideaki } 119208b202b6SYOSHIFUJI Hideaki mld2q = (struct mld2_query *)icmp6_hdr(skb); 119308b202b6SYOSHIFUJI Hideaki if (!mld2q->mld2q_nsrcs) 119408b202b6SYOSHIFUJI Hideaki group = &mld2q->mld2q_mca; 11958fa45a70SAng Way Chuang max_delay = mld2q->mld2q_mrc ? MLDV2_MRC(ntohs(mld2q->mld2q_mrc)) : 1; 119608b202b6SYOSHIFUJI Hideaki } 119708b202b6SYOSHIFUJI Hideaki 119808b202b6SYOSHIFUJI Hideaki if (!group) 119908b202b6SYOSHIFUJI Hideaki goto out; 120008b202b6SYOSHIFUJI Hideaki 1201b0e9a30dSVlad Yasevich br_vlan_get_tag(skb, &vid); 1202b0e9a30dSVlad Yasevich mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid); 120308b202b6SYOSHIFUJI Hideaki if (!mp) 120408b202b6SYOSHIFUJI Hideaki goto out; 120508b202b6SYOSHIFUJI Hideaki 120608b202b6SYOSHIFUJI Hideaki max_delay *= br->multicast_last_member_count; 12078a870178SHerbert Xu if (mp->mglist && 120808b202b6SYOSHIFUJI Hideaki (timer_pending(&mp->timer) ? 120908b202b6SYOSHIFUJI Hideaki time_after(mp->timer.expires, now + max_delay) : 121008b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&mp->timer) >= 0)) 121108b202b6SYOSHIFUJI Hideaki mod_timer(&mp->timer, now + max_delay); 121208b202b6SYOSHIFUJI Hideaki 1213e8051688SEric Dumazet for (pp = &mp->ports; 1214e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 1215e8051688SEric Dumazet pp = &p->next) { 121608b202b6SYOSHIFUJI Hideaki if (timer_pending(&p->timer) ? 121708b202b6SYOSHIFUJI Hideaki time_after(p->timer.expires, now + max_delay) : 121808b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&p->timer) >= 0) 121924f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 122008b202b6SYOSHIFUJI Hideaki } 122108b202b6SYOSHIFUJI Hideaki 122208b202b6SYOSHIFUJI Hideaki out: 122308b202b6SYOSHIFUJI Hideaki spin_unlock(&br->multicast_lock); 122408b202b6SYOSHIFUJI Hideaki return err; 122508b202b6SYOSHIFUJI Hideaki } 122608b202b6SYOSHIFUJI Hideaki #endif 122708b202b6SYOSHIFUJI Hideaki 1228eb1d1641SHerbert Xu static void br_multicast_leave_group(struct net_bridge *br, 1229eb1d1641SHerbert Xu struct net_bridge_port *port, 12308ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group) 1231eb1d1641SHerbert Xu { 1232eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 1233eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1234eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1235eb1d1641SHerbert Xu unsigned long now; 1236eb1d1641SHerbert Xu unsigned long time; 1237eb1d1641SHerbert Xu 1238eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1239eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1240eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED) || 1241eb1d1641SHerbert Xu timer_pending(&br->multicast_querier_timer)) 1242eb1d1641SHerbert Xu goto out; 1243eb1d1641SHerbert Xu 1244e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1245eb1d1641SHerbert Xu mp = br_mdb_ip_get(mdb, group); 1246eb1d1641SHerbert Xu if (!mp) 1247eb1d1641SHerbert Xu goto out; 1248eb1d1641SHerbert Xu 1249c2d3babfSDavid S. Miller if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) { 125050426b59SAmerigo Wang struct net_bridge_port_group __rcu **pp; 125150426b59SAmerigo Wang 125250426b59SAmerigo Wang for (pp = &mp->ports; 125350426b59SAmerigo Wang (p = mlock_dereference(*pp, br)) != NULL; 125450426b59SAmerigo Wang pp = &p->next) { 125550426b59SAmerigo Wang if (p->port != port) 125650426b59SAmerigo Wang continue; 125750426b59SAmerigo Wang 125850426b59SAmerigo Wang rcu_assign_pointer(*pp, p->next); 125950426b59SAmerigo Wang hlist_del_init(&p->mglist); 126050426b59SAmerigo Wang del_timer(&p->timer); 126150426b59SAmerigo Wang call_rcu_bh(&p->rcu, br_multicast_free_pg); 126237a393bcSCong Wang br_mdb_notify(br->dev, port, group, RTM_DELMDB); 126350426b59SAmerigo Wang 126450426b59SAmerigo Wang if (!mp->ports && !mp->mglist && 126550426b59SAmerigo Wang netif_running(br->dev)) 126650426b59SAmerigo Wang mod_timer(&mp->timer, jiffies); 126750426b59SAmerigo Wang } 126850426b59SAmerigo Wang goto out; 126950426b59SAmerigo Wang } 127050426b59SAmerigo Wang 1271eb1d1641SHerbert Xu now = jiffies; 1272eb1d1641SHerbert Xu time = now + br->multicast_last_member_count * 1273eb1d1641SHerbert Xu br->multicast_last_member_interval; 1274eb1d1641SHerbert Xu 1275eb1d1641SHerbert Xu if (!port) { 12768a870178SHerbert Xu if (mp->mglist && 1277eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1278eb1d1641SHerbert Xu time_after(mp->timer.expires, time) : 1279eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) { 1280eb1d1641SHerbert Xu mod_timer(&mp->timer, time); 1281eb1d1641SHerbert Xu } 1282eb1d1641SHerbert Xu 1283eb1d1641SHerbert Xu goto out; 1284eb1d1641SHerbert Xu } 1285eb1d1641SHerbert Xu 1286e8051688SEric Dumazet for (p = mlock_dereference(mp->ports, br); 1287e8051688SEric Dumazet p != NULL; 1288e8051688SEric Dumazet p = mlock_dereference(p->next, br)) { 1289eb1d1641SHerbert Xu if (p->port != port) 1290eb1d1641SHerbert Xu continue; 1291eb1d1641SHerbert Xu 1292eb1d1641SHerbert Xu if (!hlist_unhashed(&p->mglist) && 1293eb1d1641SHerbert Xu (timer_pending(&p->timer) ? 1294eb1d1641SHerbert Xu time_after(p->timer.expires, time) : 1295eb1d1641SHerbert Xu try_to_del_timer_sync(&p->timer) >= 0)) { 1296eb1d1641SHerbert Xu mod_timer(&p->timer, time); 1297eb1d1641SHerbert Xu } 1298eb1d1641SHerbert Xu 1299eb1d1641SHerbert Xu break; 1300eb1d1641SHerbert Xu } 1301eb1d1641SHerbert Xu 1302eb1d1641SHerbert Xu out: 1303eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1304eb1d1641SHerbert Xu } 1305eb1d1641SHerbert Xu 13068ef2a9a5SYOSHIFUJI Hideaki static void br_ip4_multicast_leave_group(struct net_bridge *br, 13078ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 1308b0e9a30dSVlad Yasevich __be32 group, 1309b0e9a30dSVlad Yasevich __u16 vid) 13108ef2a9a5SYOSHIFUJI Hideaki { 13118ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 13128ef2a9a5SYOSHIFUJI Hideaki 13138ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 13148ef2a9a5SYOSHIFUJI Hideaki return; 13158ef2a9a5SYOSHIFUJI Hideaki 13168ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 13178ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 1318b0e9a30dSVlad Yasevich br_group.vid = vid; 13198ef2a9a5SYOSHIFUJI Hideaki 13208ef2a9a5SYOSHIFUJI Hideaki br_multicast_leave_group(br, port, &br_group); 13218ef2a9a5SYOSHIFUJI Hideaki } 13228ef2a9a5SYOSHIFUJI Hideaki 1323dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 132408b202b6SYOSHIFUJI Hideaki static void br_ip6_multicast_leave_group(struct net_bridge *br, 132508b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 1326b0e9a30dSVlad Yasevich const struct in6_addr *group, 1327b0e9a30dSVlad Yasevich __u16 vid) 132808b202b6SYOSHIFUJI Hideaki { 132908b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 133008b202b6SYOSHIFUJI Hideaki 1331e4de9f9eSLinus Lüssing if (!ipv6_is_transient_multicast(group)) 133208b202b6SYOSHIFUJI Hideaki return; 133308b202b6SYOSHIFUJI Hideaki 13344e3fd7a0SAlexey Dobriyan br_group.u.ip6 = *group; 133508b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 1336b0e9a30dSVlad Yasevich br_group.vid = vid; 133708b202b6SYOSHIFUJI Hideaki 133808b202b6SYOSHIFUJI Hideaki br_multicast_leave_group(br, port, &br_group); 133908b202b6SYOSHIFUJI Hideaki } 134008b202b6SYOSHIFUJI Hideaki #endif 13418ef2a9a5SYOSHIFUJI Hideaki 1342eb1d1641SHerbert Xu static int br_multicast_ipv4_rcv(struct net_bridge *br, 1343eb1d1641SHerbert Xu struct net_bridge_port *port, 1344eb1d1641SHerbert Xu struct sk_buff *skb) 1345eb1d1641SHerbert Xu { 1346eb1d1641SHerbert Xu struct sk_buff *skb2 = skb; 1347b71d1d42SEric Dumazet const struct iphdr *iph; 1348eb1d1641SHerbert Xu struct igmphdr *ih; 134995c96174SEric Dumazet unsigned int len; 135095c96174SEric Dumazet unsigned int offset; 1351eb1d1641SHerbert Xu int err; 1352b0e9a30dSVlad Yasevich u16 vid = 0; 1353eb1d1641SHerbert Xu 1354eb1d1641SHerbert Xu /* We treat OOM as packet loss for now. */ 1355eb1d1641SHerbert Xu if (!pskb_may_pull(skb, sizeof(*iph))) 1356eb1d1641SHerbert Xu return -EINVAL; 1357eb1d1641SHerbert Xu 1358eb1d1641SHerbert Xu iph = ip_hdr(skb); 1359eb1d1641SHerbert Xu 1360eb1d1641SHerbert Xu if (iph->ihl < 5 || iph->version != 4) 1361eb1d1641SHerbert Xu return -EINVAL; 1362eb1d1641SHerbert Xu 1363eb1d1641SHerbert Xu if (!pskb_may_pull(skb, ip_hdrlen(skb))) 1364eb1d1641SHerbert Xu return -EINVAL; 1365eb1d1641SHerbert Xu 1366eb1d1641SHerbert Xu iph = ip_hdr(skb); 1367eb1d1641SHerbert Xu 1368eb1d1641SHerbert Xu if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl))) 1369eb1d1641SHerbert Xu return -EINVAL; 1370eb1d1641SHerbert Xu 1371bd4265feSHerbert Xu if (iph->protocol != IPPROTO_IGMP) { 1372bd4265feSHerbert Xu if ((iph->daddr & IGMP_LOCAL_GROUP_MASK) != IGMP_LOCAL_GROUP) 1373bd4265feSHerbert Xu BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 1374eb1d1641SHerbert Xu return 0; 1375bd4265feSHerbert Xu } 1376eb1d1641SHerbert Xu 1377eb1d1641SHerbert Xu len = ntohs(iph->tot_len); 1378eb1d1641SHerbert Xu if (skb->len < len || len < ip_hdrlen(skb)) 1379eb1d1641SHerbert Xu return -EINVAL; 1380eb1d1641SHerbert Xu 1381eb1d1641SHerbert Xu if (skb->len > len) { 1382eb1d1641SHerbert Xu skb2 = skb_clone(skb, GFP_ATOMIC); 1383eb1d1641SHerbert Xu if (!skb2) 1384eb1d1641SHerbert Xu return -ENOMEM; 1385eb1d1641SHerbert Xu 1386eb1d1641SHerbert Xu err = pskb_trim_rcsum(skb2, len); 1387eb1d1641SHerbert Xu if (err) 13888440853bSYOSHIFUJI Hideaki / 吉藤英明 goto err_out; 1389eb1d1641SHerbert Xu } 1390eb1d1641SHerbert Xu 1391eb1d1641SHerbert Xu len -= ip_hdrlen(skb2); 1392eb1d1641SHerbert Xu offset = skb_network_offset(skb2) + ip_hdrlen(skb2); 1393eb1d1641SHerbert Xu __skb_pull(skb2, offset); 1394eb1d1641SHerbert Xu skb_reset_transport_header(skb2); 1395eb1d1641SHerbert Xu 1396eb1d1641SHerbert Xu err = -EINVAL; 1397eb1d1641SHerbert Xu if (!pskb_may_pull(skb2, sizeof(*ih))) 1398eb1d1641SHerbert Xu goto out; 1399eb1d1641SHerbert Xu 1400eb1d1641SHerbert Xu switch (skb2->ip_summed) { 1401eb1d1641SHerbert Xu case CHECKSUM_COMPLETE: 1402eb1d1641SHerbert Xu if (!csum_fold(skb2->csum)) 1403eb1d1641SHerbert Xu break; 1404eb1d1641SHerbert Xu /* fall through */ 1405eb1d1641SHerbert Xu case CHECKSUM_NONE: 1406eb1d1641SHerbert Xu skb2->csum = 0; 1407eb1d1641SHerbert Xu if (skb_checksum_complete(skb2)) 14088440853bSYOSHIFUJI Hideaki / 吉藤英明 goto out; 1409eb1d1641SHerbert Xu } 1410eb1d1641SHerbert Xu 1411eb1d1641SHerbert Xu err = 0; 1412eb1d1641SHerbert Xu 1413b0e9a30dSVlad Yasevich br_vlan_get_tag(skb2, &vid); 1414eb1d1641SHerbert Xu BR_INPUT_SKB_CB(skb)->igmp = 1; 1415eb1d1641SHerbert Xu ih = igmp_hdr(skb2); 1416eb1d1641SHerbert Xu 1417eb1d1641SHerbert Xu switch (ih->type) { 1418eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_REPORT: 1419eb1d1641SHerbert Xu case IGMPV2_HOST_MEMBERSHIP_REPORT: 142062b2bcb4SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 1421b0e9a30dSVlad Yasevich err = br_ip4_multicast_add_group(br, port, ih->group, vid); 1422eb1d1641SHerbert Xu break; 1423eb1d1641SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 14248ef2a9a5SYOSHIFUJI Hideaki err = br_ip4_multicast_igmp3_report(br, port, skb2); 1425eb1d1641SHerbert Xu break; 1426eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_QUERY: 14278ef2a9a5SYOSHIFUJI Hideaki err = br_ip4_multicast_query(br, port, skb2); 1428eb1d1641SHerbert Xu break; 1429eb1d1641SHerbert Xu case IGMP_HOST_LEAVE_MESSAGE: 1430b0e9a30dSVlad Yasevich br_ip4_multicast_leave_group(br, port, ih->group, vid); 1431eb1d1641SHerbert Xu break; 1432eb1d1641SHerbert Xu } 1433eb1d1641SHerbert Xu 1434eb1d1641SHerbert Xu out: 1435eb1d1641SHerbert Xu __skb_push(skb2, offset); 14368440853bSYOSHIFUJI Hideaki / 吉藤英明 err_out: 1437eb1d1641SHerbert Xu if (skb2 != skb) 1438eb1d1641SHerbert Xu kfree_skb(skb2); 1439eb1d1641SHerbert Xu return err; 1440eb1d1641SHerbert Xu } 1441eb1d1641SHerbert Xu 1442dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 144308b202b6SYOSHIFUJI Hideaki static int br_multicast_ipv6_rcv(struct net_bridge *br, 144408b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 144508b202b6SYOSHIFUJI Hideaki struct sk_buff *skb) 144608b202b6SYOSHIFUJI Hideaki { 14479d89081dSTomas Winkler struct sk_buff *skb2; 1448b71d1d42SEric Dumazet const struct ipv6hdr *ip6h; 144922df1331SEric Dumazet u8 icmp6_type; 145008b202b6SYOSHIFUJI Hideaki u8 nexthdr; 145175f2811cSJesse Gross __be16 frag_off; 145295c96174SEric Dumazet unsigned int len; 1453bb7a0bd6SKulikov Vasiliy int offset; 145408b202b6SYOSHIFUJI Hideaki int err; 1455b0e9a30dSVlad Yasevich u16 vid = 0; 145608b202b6SYOSHIFUJI Hideaki 145708b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*ip6h))) 145808b202b6SYOSHIFUJI Hideaki return -EINVAL; 145908b202b6SYOSHIFUJI Hideaki 146008b202b6SYOSHIFUJI Hideaki ip6h = ipv6_hdr(skb); 146108b202b6SYOSHIFUJI Hideaki 146208b202b6SYOSHIFUJI Hideaki /* 146308b202b6SYOSHIFUJI Hideaki * We're interested in MLD messages only. 146408b202b6SYOSHIFUJI Hideaki * - Version is 6 146508b202b6SYOSHIFUJI Hideaki * - MLD has always Router Alert hop-by-hop option 146608b202b6SYOSHIFUJI Hideaki * - But we do not support jumbrograms. 146708b202b6SYOSHIFUJI Hideaki */ 146808b202b6SYOSHIFUJI Hideaki if (ip6h->version != 6 || 146908b202b6SYOSHIFUJI Hideaki ip6h->nexthdr != IPPROTO_HOPOPTS || 147008b202b6SYOSHIFUJI Hideaki ip6h->payload_len == 0) 147108b202b6SYOSHIFUJI Hideaki return 0; 147208b202b6SYOSHIFUJI Hideaki 1473ff9a57a6SLinus Lüssing len = ntohs(ip6h->payload_len) + sizeof(*ip6h); 147408b202b6SYOSHIFUJI Hideaki if (skb->len < len) 147508b202b6SYOSHIFUJI Hideaki return -EINVAL; 147608b202b6SYOSHIFUJI Hideaki 147708b202b6SYOSHIFUJI Hideaki nexthdr = ip6h->nexthdr; 147875f2811cSJesse Gross offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr, &frag_off); 147908b202b6SYOSHIFUJI Hideaki 148008b202b6SYOSHIFUJI Hideaki if (offset < 0 || nexthdr != IPPROTO_ICMPV6) 148108b202b6SYOSHIFUJI Hideaki return 0; 148208b202b6SYOSHIFUJI Hideaki 148308b202b6SYOSHIFUJI Hideaki /* Okay, we found ICMPv6 header */ 148408b202b6SYOSHIFUJI Hideaki skb2 = skb_clone(skb, GFP_ATOMIC); 148508b202b6SYOSHIFUJI Hideaki if (!skb2) 148608b202b6SYOSHIFUJI Hideaki return -ENOMEM; 148708b202b6SYOSHIFUJI Hideaki 14889d89081dSTomas Winkler err = -EINVAL; 14899d89081dSTomas Winkler if (!pskb_may_pull(skb2, offset + sizeof(struct icmp6hdr))) 14909d89081dSTomas Winkler goto out; 14919d89081dSTomas Winkler 149208b202b6SYOSHIFUJI Hideaki len -= offset - skb_network_offset(skb2); 149308b202b6SYOSHIFUJI Hideaki 149408b202b6SYOSHIFUJI Hideaki __skb_pull(skb2, offset); 149508b202b6SYOSHIFUJI Hideaki skb_reset_transport_header(skb2); 1496fa2da8cdSstephen hemminger skb_postpull_rcsum(skb2, skb_network_header(skb2), 1497fa2da8cdSstephen hemminger skb_network_header_len(skb2)); 149808b202b6SYOSHIFUJI Hideaki 149922df1331SEric Dumazet icmp6_type = icmp6_hdr(skb2)->icmp6_type; 150008b202b6SYOSHIFUJI Hideaki 150122df1331SEric Dumazet switch (icmp6_type) { 150208b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 150308b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 150408b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 150508b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 150608b202b6SYOSHIFUJI Hideaki break; 150708b202b6SYOSHIFUJI Hideaki default: 150808b202b6SYOSHIFUJI Hideaki err = 0; 150908b202b6SYOSHIFUJI Hideaki goto out; 151008b202b6SYOSHIFUJI Hideaki } 151108b202b6SYOSHIFUJI Hideaki 151208b202b6SYOSHIFUJI Hideaki /* Okay, we found MLD message. Check further. */ 151308b202b6SYOSHIFUJI Hideaki if (skb2->len > len) { 151408b202b6SYOSHIFUJI Hideaki err = pskb_trim_rcsum(skb2, len); 151508b202b6SYOSHIFUJI Hideaki if (err) 151608b202b6SYOSHIFUJI Hideaki goto out; 15174b275d7eSYan, Zheng err = -EINVAL; 151808b202b6SYOSHIFUJI Hideaki } 151908b202b6SYOSHIFUJI Hideaki 15204b275d7eSYan, Zheng ip6h = ipv6_hdr(skb2); 15214b275d7eSYan, Zheng 152208b202b6SYOSHIFUJI Hideaki switch (skb2->ip_summed) { 152308b202b6SYOSHIFUJI Hideaki case CHECKSUM_COMPLETE: 15244b275d7eSYan, Zheng if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, skb2->len, 15254b275d7eSYan, Zheng IPPROTO_ICMPV6, skb2->csum)) 152608b202b6SYOSHIFUJI Hideaki break; 152708b202b6SYOSHIFUJI Hideaki /*FALLTHROUGH*/ 152808b202b6SYOSHIFUJI Hideaki case CHECKSUM_NONE: 15294b275d7eSYan, Zheng skb2->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr, 15304b275d7eSYan, Zheng &ip6h->daddr, 15314b275d7eSYan, Zheng skb2->len, 15324b275d7eSYan, Zheng IPPROTO_ICMPV6, 0)); 15334b275d7eSYan, Zheng if (__skb_checksum_complete(skb2)) 153408b202b6SYOSHIFUJI Hideaki goto out; 153508b202b6SYOSHIFUJI Hideaki } 153608b202b6SYOSHIFUJI Hideaki 153708b202b6SYOSHIFUJI Hideaki err = 0; 153808b202b6SYOSHIFUJI Hideaki 1539b0e9a30dSVlad Yasevich br_vlan_get_tag(skb, &vid); 154008b202b6SYOSHIFUJI Hideaki BR_INPUT_SKB_CB(skb)->igmp = 1; 154108b202b6SYOSHIFUJI Hideaki 154222df1331SEric Dumazet switch (icmp6_type) { 154308b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 154408b202b6SYOSHIFUJI Hideaki { 15459d89081dSTomas Winkler struct mld_msg *mld; 15469d89081dSTomas Winkler if (!pskb_may_pull(skb2, sizeof(*mld))) { 15479d89081dSTomas Winkler err = -EINVAL; 15489d89081dSTomas Winkler goto out; 15499d89081dSTomas Winkler } 15509d89081dSTomas Winkler mld = (struct mld_msg *)skb_transport_header(skb2); 1551fc2af6c7SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 1552b0e9a30dSVlad Yasevich err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid); 155308b202b6SYOSHIFUJI Hideaki break; 155408b202b6SYOSHIFUJI Hideaki } 155508b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 155608b202b6SYOSHIFUJI Hideaki err = br_ip6_multicast_mld2_report(br, port, skb2); 155708b202b6SYOSHIFUJI Hideaki break; 155808b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 155908b202b6SYOSHIFUJI Hideaki err = br_ip6_multicast_query(br, port, skb2); 156008b202b6SYOSHIFUJI Hideaki break; 156108b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 156208b202b6SYOSHIFUJI Hideaki { 15639d89081dSTomas Winkler struct mld_msg *mld; 15649d89081dSTomas Winkler if (!pskb_may_pull(skb2, sizeof(*mld))) { 15659d89081dSTomas Winkler err = -EINVAL; 15669d89081dSTomas Winkler goto out; 15679d89081dSTomas Winkler } 15689d89081dSTomas Winkler mld = (struct mld_msg *)skb_transport_header(skb2); 1569b0e9a30dSVlad Yasevich br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid); 157008b202b6SYOSHIFUJI Hideaki } 157108b202b6SYOSHIFUJI Hideaki } 157208b202b6SYOSHIFUJI Hideaki 157308b202b6SYOSHIFUJI Hideaki out: 157408b202b6SYOSHIFUJI Hideaki kfree_skb(skb2); 157508b202b6SYOSHIFUJI Hideaki return err; 157608b202b6SYOSHIFUJI Hideaki } 157708b202b6SYOSHIFUJI Hideaki #endif 157808b202b6SYOSHIFUJI Hideaki 1579eb1d1641SHerbert Xu int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port, 1580eb1d1641SHerbert Xu struct sk_buff *skb) 1581eb1d1641SHerbert Xu { 15821fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->igmp = 0; 15831fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->mrouters_only = 0; 15841fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 1585eb1d1641SHerbert Xu if (br->multicast_disabled) 1586eb1d1641SHerbert Xu return 0; 1587eb1d1641SHerbert Xu 1588eb1d1641SHerbert Xu switch (skb->protocol) { 1589eb1d1641SHerbert Xu case htons(ETH_P_IP): 1590eb1d1641SHerbert Xu return br_multicast_ipv4_rcv(br, port, skb); 1591dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 159208b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 159308b202b6SYOSHIFUJI Hideaki return br_multicast_ipv6_rcv(br, port, skb); 159408b202b6SYOSHIFUJI Hideaki #endif 1595eb1d1641SHerbert Xu } 1596eb1d1641SHerbert Xu 1597eb1d1641SHerbert Xu return 0; 1598eb1d1641SHerbert Xu } 1599eb1d1641SHerbert Xu 1600eb1d1641SHerbert Xu static void br_multicast_query_expired(unsigned long data) 1601eb1d1641SHerbert Xu { 1602eb1d1641SHerbert Xu struct net_bridge *br = (void *)data; 1603eb1d1641SHerbert Xu 1604eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1605eb1d1641SHerbert Xu if (br->multicast_startup_queries_sent < 1606eb1d1641SHerbert Xu br->multicast_startup_query_count) 1607eb1d1641SHerbert Xu br->multicast_startup_queries_sent++; 1608eb1d1641SHerbert Xu 1609eb1d1641SHerbert Xu br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent); 1610eb1d1641SHerbert Xu 1611eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1612eb1d1641SHerbert Xu } 1613eb1d1641SHerbert Xu 1614eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br) 1615eb1d1641SHerbert Xu { 1616eb1d1641SHerbert Xu br->hash_elasticity = 4; 1617eb1d1641SHerbert Xu br->hash_max = 512; 1618eb1d1641SHerbert Xu 1619eb1d1641SHerbert Xu br->multicast_router = 1; 1620c5c23260SHerbert Xu br->multicast_querier = 0; 1621eb1d1641SHerbert Xu br->multicast_last_member_count = 2; 1622eb1d1641SHerbert Xu br->multicast_startup_query_count = 2; 1623eb1d1641SHerbert Xu 1624eb1d1641SHerbert Xu br->multicast_last_member_interval = HZ; 1625eb1d1641SHerbert Xu br->multicast_query_response_interval = 10 * HZ; 1626eb1d1641SHerbert Xu br->multicast_startup_query_interval = 125 * HZ / 4; 1627eb1d1641SHerbert Xu br->multicast_query_interval = 125 * HZ; 1628eb1d1641SHerbert Xu br->multicast_querier_interval = 255 * HZ; 1629eb1d1641SHerbert Xu br->multicast_membership_interval = 260 * HZ; 1630eb1d1641SHerbert Xu 1631eb1d1641SHerbert Xu spin_lock_init(&br->multicast_lock); 1632eb1d1641SHerbert Xu setup_timer(&br->multicast_router_timer, 1633eb1d1641SHerbert Xu br_multicast_local_router_expired, 0); 1634eb1d1641SHerbert Xu setup_timer(&br->multicast_querier_timer, 1635bb63f1f8SHerbert Xu br_multicast_querier_expired, (unsigned long)br); 1636eb1d1641SHerbert Xu setup_timer(&br->multicast_query_timer, br_multicast_query_expired, 1637eb1d1641SHerbert Xu (unsigned long)br); 1638eb1d1641SHerbert Xu } 1639eb1d1641SHerbert Xu 1640eb1d1641SHerbert Xu void br_multicast_open(struct net_bridge *br) 1641eb1d1641SHerbert Xu { 1642eb1d1641SHerbert Xu br->multicast_startup_queries_sent = 0; 1643eb1d1641SHerbert Xu 1644eb1d1641SHerbert Xu if (br->multicast_disabled) 1645eb1d1641SHerbert Xu return; 1646eb1d1641SHerbert Xu 1647eb1d1641SHerbert Xu mod_timer(&br->multicast_query_timer, jiffies); 1648eb1d1641SHerbert Xu } 1649eb1d1641SHerbert Xu 1650eb1d1641SHerbert Xu void br_multicast_stop(struct net_bridge *br) 1651eb1d1641SHerbert Xu { 1652eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 1653eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1654b67bfe0dSSasha Levin struct hlist_node *n; 1655eb1d1641SHerbert Xu u32 ver; 1656eb1d1641SHerbert Xu int i; 1657eb1d1641SHerbert Xu 1658eb1d1641SHerbert Xu del_timer_sync(&br->multicast_router_timer); 1659eb1d1641SHerbert Xu del_timer_sync(&br->multicast_querier_timer); 1660eb1d1641SHerbert Xu del_timer_sync(&br->multicast_query_timer); 1661eb1d1641SHerbert Xu 1662eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1663e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1664eb1d1641SHerbert Xu if (!mdb) 1665eb1d1641SHerbert Xu goto out; 1666eb1d1641SHerbert Xu 1667eb1d1641SHerbert Xu br->mdb = NULL; 1668eb1d1641SHerbert Xu 1669eb1d1641SHerbert Xu ver = mdb->ver; 1670eb1d1641SHerbert Xu for (i = 0; i < mdb->max; i++) { 1671b67bfe0dSSasha Levin hlist_for_each_entry_safe(mp, n, &mdb->mhash[i], 1672eb1d1641SHerbert Xu hlist[ver]) { 1673eb1d1641SHerbert Xu del_timer(&mp->timer); 1674eb1d1641SHerbert Xu call_rcu_bh(&mp->rcu, br_multicast_free_group); 1675eb1d1641SHerbert Xu } 1676eb1d1641SHerbert Xu } 1677eb1d1641SHerbert Xu 1678eb1d1641SHerbert Xu if (mdb->old) { 1679eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 168010cc2b50SHerbert Xu rcu_barrier_bh(); 1681eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1682eb1d1641SHerbert Xu WARN_ON(mdb->old); 1683eb1d1641SHerbert Xu } 1684eb1d1641SHerbert Xu 1685eb1d1641SHerbert Xu mdb->old = mdb; 1686eb1d1641SHerbert Xu call_rcu_bh(&mdb->rcu, br_mdb_free); 1687eb1d1641SHerbert Xu 1688eb1d1641SHerbert Xu out: 1689eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 1690eb1d1641SHerbert Xu } 16910909e117SHerbert Xu 16920909e117SHerbert Xu int br_multicast_set_router(struct net_bridge *br, unsigned long val) 16930909e117SHerbert Xu { 16940909e117SHerbert Xu int err = -ENOENT; 16950909e117SHerbert Xu 16960909e117SHerbert Xu spin_lock_bh(&br->multicast_lock); 16970909e117SHerbert Xu if (!netif_running(br->dev)) 16980909e117SHerbert Xu goto unlock; 16990909e117SHerbert Xu 17000909e117SHerbert Xu switch (val) { 17010909e117SHerbert Xu case 0: 17020909e117SHerbert Xu case 2: 17030909e117SHerbert Xu del_timer(&br->multicast_router_timer); 17040909e117SHerbert Xu /* fall through */ 17050909e117SHerbert Xu case 1: 17060909e117SHerbert Xu br->multicast_router = val; 17070909e117SHerbert Xu err = 0; 17080909e117SHerbert Xu break; 17090909e117SHerbert Xu 17100909e117SHerbert Xu default: 17110909e117SHerbert Xu err = -EINVAL; 17120909e117SHerbert Xu break; 17130909e117SHerbert Xu } 17140909e117SHerbert Xu 17150909e117SHerbert Xu unlock: 17160909e117SHerbert Xu spin_unlock_bh(&br->multicast_lock); 17170909e117SHerbert Xu 17180909e117SHerbert Xu return err; 17190909e117SHerbert Xu } 17200909e117SHerbert Xu 17210909e117SHerbert Xu int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val) 17220909e117SHerbert Xu { 17230909e117SHerbert Xu struct net_bridge *br = p->br; 17240909e117SHerbert Xu int err = -ENOENT; 17250909e117SHerbert Xu 17260909e117SHerbert Xu spin_lock(&br->multicast_lock); 17270909e117SHerbert Xu if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED) 17280909e117SHerbert Xu goto unlock; 17290909e117SHerbert Xu 17300909e117SHerbert Xu switch (val) { 17310909e117SHerbert Xu case 0: 17320909e117SHerbert Xu case 1: 17330909e117SHerbert Xu case 2: 17340909e117SHerbert Xu p->multicast_router = val; 17350909e117SHerbert Xu err = 0; 17360909e117SHerbert Xu 17370909e117SHerbert Xu if (val < 2 && !hlist_unhashed(&p->rlist)) 17380909e117SHerbert Xu hlist_del_init_rcu(&p->rlist); 17390909e117SHerbert Xu 17400909e117SHerbert Xu if (val == 1) 17410909e117SHerbert Xu break; 17420909e117SHerbert Xu 17430909e117SHerbert Xu del_timer(&p->multicast_router_timer); 17440909e117SHerbert Xu 17450909e117SHerbert Xu if (val == 0) 17460909e117SHerbert Xu break; 17470909e117SHerbert Xu 17480909e117SHerbert Xu br_multicast_add_router(br, p); 17490909e117SHerbert Xu break; 17500909e117SHerbert Xu 17510909e117SHerbert Xu default: 17520909e117SHerbert Xu err = -EINVAL; 17530909e117SHerbert Xu break; 17540909e117SHerbert Xu } 17550909e117SHerbert Xu 17560909e117SHerbert Xu unlock: 17570909e117SHerbert Xu spin_unlock(&br->multicast_lock); 17580909e117SHerbert Xu 17590909e117SHerbert Xu return err; 17600909e117SHerbert Xu } 1761561f1103SHerbert Xu 176274857216SHerbert Xu static void br_multicast_start_querier(struct net_bridge *br) 1763561f1103SHerbert Xu { 1764561f1103SHerbert Xu struct net_bridge_port *port; 176574857216SHerbert Xu 176674857216SHerbert Xu br_multicast_open(br); 176774857216SHerbert Xu 176874857216SHerbert Xu list_for_each_entry(port, &br->port_list, list) { 176974857216SHerbert Xu if (port->state == BR_STATE_DISABLED || 177074857216SHerbert Xu port->state == BR_STATE_BLOCKING) 177174857216SHerbert Xu continue; 177274857216SHerbert Xu 177374857216SHerbert Xu __br_multicast_enable_port(port); 177474857216SHerbert Xu } 177574857216SHerbert Xu } 177674857216SHerbert Xu 177774857216SHerbert Xu int br_multicast_toggle(struct net_bridge *br, unsigned long val) 177874857216SHerbert Xu { 17793a7fda06SHerbert Xu int err = 0; 1780e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 1781561f1103SHerbert Xu 1782ef5e0d82SAndrey Vagin spin_lock_bh(&br->multicast_lock); 1783561f1103SHerbert Xu if (br->multicast_disabled == !val) 1784561f1103SHerbert Xu goto unlock; 1785561f1103SHerbert Xu 1786561f1103SHerbert Xu br->multicast_disabled = !val; 1787561f1103SHerbert Xu if (br->multicast_disabled) 1788561f1103SHerbert Xu goto unlock; 1789561f1103SHerbert Xu 17903a7fda06SHerbert Xu if (!netif_running(br->dev)) 17913a7fda06SHerbert Xu goto unlock; 17923a7fda06SHerbert Xu 1793e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1794e8051688SEric Dumazet if (mdb) { 1795e8051688SEric Dumazet if (mdb->old) { 1796561f1103SHerbert Xu err = -EEXIST; 1797561f1103SHerbert Xu rollback: 1798561f1103SHerbert Xu br->multicast_disabled = !!val; 1799561f1103SHerbert Xu goto unlock; 1800561f1103SHerbert Xu } 1801561f1103SHerbert Xu 1802e8051688SEric Dumazet err = br_mdb_rehash(&br->mdb, mdb->max, 1803561f1103SHerbert Xu br->hash_elasticity); 1804561f1103SHerbert Xu if (err) 1805561f1103SHerbert Xu goto rollback; 1806561f1103SHerbert Xu } 1807561f1103SHerbert Xu 180874857216SHerbert Xu br_multicast_start_querier(br); 1809561f1103SHerbert Xu 1810561f1103SHerbert Xu unlock: 1811ef5e0d82SAndrey Vagin spin_unlock_bh(&br->multicast_lock); 1812561f1103SHerbert Xu 1813561f1103SHerbert Xu return err; 1814561f1103SHerbert Xu } 1815b195167fSHerbert Xu 1816c5c23260SHerbert Xu int br_multicast_set_querier(struct net_bridge *br, unsigned long val) 1817c5c23260SHerbert Xu { 1818c5c23260SHerbert Xu val = !!val; 1819c5c23260SHerbert Xu 1820c5c23260SHerbert Xu spin_lock_bh(&br->multicast_lock); 1821c5c23260SHerbert Xu if (br->multicast_querier == val) 1822c5c23260SHerbert Xu goto unlock; 1823c5c23260SHerbert Xu 1824c5c23260SHerbert Xu br->multicast_querier = val; 1825c5c23260SHerbert Xu if (val) 1826c5c23260SHerbert Xu br_multicast_start_querier(br); 1827c5c23260SHerbert Xu 1828c5c23260SHerbert Xu unlock: 1829c5c23260SHerbert Xu spin_unlock_bh(&br->multicast_lock); 1830c5c23260SHerbert Xu 1831c5c23260SHerbert Xu return 0; 1832c5c23260SHerbert Xu } 1833c5c23260SHerbert Xu 1834b195167fSHerbert Xu int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val) 1835b195167fSHerbert Xu { 1836b195167fSHerbert Xu int err = -ENOENT; 1837b195167fSHerbert Xu u32 old; 1838e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 1839b195167fSHerbert Xu 1840b195167fSHerbert Xu spin_lock(&br->multicast_lock); 1841b195167fSHerbert Xu if (!netif_running(br->dev)) 1842b195167fSHerbert Xu goto unlock; 1843b195167fSHerbert Xu 1844b195167fSHerbert Xu err = -EINVAL; 1845b195167fSHerbert Xu if (!is_power_of_2(val)) 1846b195167fSHerbert Xu goto unlock; 1847e8051688SEric Dumazet 1848e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1849e8051688SEric Dumazet if (mdb && val < mdb->size) 1850b195167fSHerbert Xu goto unlock; 1851b195167fSHerbert Xu 1852b195167fSHerbert Xu err = 0; 1853b195167fSHerbert Xu 1854b195167fSHerbert Xu old = br->hash_max; 1855b195167fSHerbert Xu br->hash_max = val; 1856b195167fSHerbert Xu 1857e8051688SEric Dumazet if (mdb) { 1858e8051688SEric Dumazet if (mdb->old) { 1859b195167fSHerbert Xu err = -EEXIST; 1860b195167fSHerbert Xu rollback: 1861b195167fSHerbert Xu br->hash_max = old; 1862b195167fSHerbert Xu goto unlock; 1863b195167fSHerbert Xu } 1864b195167fSHerbert Xu 1865b195167fSHerbert Xu err = br_mdb_rehash(&br->mdb, br->hash_max, 1866b195167fSHerbert Xu br->hash_elasticity); 1867b195167fSHerbert Xu if (err) 1868b195167fSHerbert Xu goto rollback; 1869b195167fSHerbert Xu } 1870b195167fSHerbert Xu 1871b195167fSHerbert Xu unlock: 1872b195167fSHerbert Xu spin_unlock(&br->multicast_lock); 1873b195167fSHerbert Xu 1874b195167fSHerbert Xu return err; 1875b195167fSHerbert Xu } 1876