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> 1407f8ac4aSLinus Lüssing #include <linux/export.h> 15eb1d1641SHerbert Xu #include <linux/if_ether.h> 16eb1d1641SHerbert Xu #include <linux/igmp.h> 17eb1d1641SHerbert Xu #include <linux/jhash.h> 18eb1d1641SHerbert Xu #include <linux/kernel.h> 19b195167fSHerbert Xu #include <linux/log2.h> 20eb1d1641SHerbert Xu #include <linux/netdevice.h> 21eb1d1641SHerbert Xu #include <linux/netfilter_bridge.h> 22eb1d1641SHerbert Xu #include <linux/random.h> 23eb1d1641SHerbert Xu #include <linux/rculist.h> 24eb1d1641SHerbert Xu #include <linux/skbuff.h> 25eb1d1641SHerbert Xu #include <linux/slab.h> 26eb1d1641SHerbert Xu #include <linux/timer.h> 271c8ad5bfSCong Wang #include <linux/inetdevice.h> 28eb1d1641SHerbert Xu #include <net/ip.h> 29dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 3008b202b6SYOSHIFUJI Hideaki #include <net/ipv6.h> 3108b202b6SYOSHIFUJI Hideaki #include <net/mld.h> 32d4c4f07dSDavid S. Miller #include <net/ip6_checksum.h> 333c3769e6SLinus Lüssing #include <net/addrconf.h> 3408b202b6SYOSHIFUJI Hideaki #endif 35eb1d1641SHerbert Xu 36eb1d1641SHerbert Xu #include "br_private.h" 37eb1d1641SHerbert Xu 38cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br, 3990010b36SLinus Lüssing struct bridge_mcast_own_query *query); 40754bc547SSatish Ashok static void br_multicast_add_router(struct net_bridge *br, 41754bc547SSatish Ashok struct net_bridge_port *port); 422ce297fcSCong Wang unsigned int br_mdb_rehash_seq; 43c83b8fabSHerbert Xu 448ef2a9a5SYOSHIFUJI Hideaki static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b) 458ef2a9a5SYOSHIFUJI Hideaki { 468ef2a9a5SYOSHIFUJI Hideaki if (a->proto != b->proto) 478ef2a9a5SYOSHIFUJI Hideaki return 0; 48b0e9a30dSVlad Yasevich if (a->vid != b->vid) 49b0e9a30dSVlad Yasevich return 0; 508ef2a9a5SYOSHIFUJI Hideaki switch (a->proto) { 518ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 528ef2a9a5SYOSHIFUJI Hideaki return a->u.ip4 == b->u.ip4; 53dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 5408b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 5508b202b6SYOSHIFUJI Hideaki return ipv6_addr_equal(&a->u.ip6, &b->u.ip6); 5608b202b6SYOSHIFUJI Hideaki #endif 578ef2a9a5SYOSHIFUJI Hideaki } 588ef2a9a5SYOSHIFUJI Hideaki return 0; 598ef2a9a5SYOSHIFUJI Hideaki } 608ef2a9a5SYOSHIFUJI Hideaki 61b0e9a30dSVlad Yasevich static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip, 62b0e9a30dSVlad Yasevich __u16 vid) 63eb1d1641SHerbert Xu { 64b0e9a30dSVlad Yasevich return jhash_2words((__force u32)ip, vid, mdb->secret) & (mdb->max - 1); 65eb1d1641SHerbert Xu } 66eb1d1641SHerbert Xu 67dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 6808b202b6SYOSHIFUJI Hideaki static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb, 69b0e9a30dSVlad Yasevich const struct in6_addr *ip, 70b0e9a30dSVlad Yasevich __u16 vid) 7108b202b6SYOSHIFUJI Hideaki { 72b0e9a30dSVlad Yasevich return jhash_2words(ipv6_addr_hash(ip), vid, 73b0e9a30dSVlad Yasevich mdb->secret) & (mdb->max - 1); 7408b202b6SYOSHIFUJI Hideaki } 7508b202b6SYOSHIFUJI Hideaki #endif 7608b202b6SYOSHIFUJI Hideaki 778ef2a9a5SYOSHIFUJI Hideaki static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb, 788ef2a9a5SYOSHIFUJI Hideaki struct br_ip *ip) 798ef2a9a5SYOSHIFUJI Hideaki { 808ef2a9a5SYOSHIFUJI Hideaki switch (ip->proto) { 818ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 82b0e9a30dSVlad Yasevich return __br_ip4_hash(mdb, ip->u.ip4, ip->vid); 83dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 8408b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 85b0e9a30dSVlad Yasevich return __br_ip6_hash(mdb, &ip->u.ip6, ip->vid); 8608b202b6SYOSHIFUJI Hideaki #endif 878ef2a9a5SYOSHIFUJI Hideaki } 888ef2a9a5SYOSHIFUJI Hideaki return 0; 89eb1d1641SHerbert Xu } 90eb1d1641SHerbert Xu 91eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *__br_mdb_ip_get( 928ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash) 93eb1d1641SHerbert Xu { 94eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 95eb1d1641SHerbert Xu 96b67bfe0dSSasha Levin hlist_for_each_entry_rcu(mp, &mdb->mhash[hash], hlist[mdb->ver]) { 978ef2a9a5SYOSHIFUJI Hideaki if (br_ip_equal(&mp->addr, dst)) 98eb1d1641SHerbert Xu return mp; 99eb1d1641SHerbert Xu } 100eb1d1641SHerbert Xu 101eb1d1641SHerbert Xu return NULL; 102eb1d1641SHerbert Xu } 103eb1d1641SHerbert Xu 104cfd56754SCong Wang struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge_mdb_htable *mdb, 105cfd56754SCong Wang struct br_ip *dst) 1067f285fa7SHerbert Xu { 1077f285fa7SHerbert Xu if (!mdb) 1087f285fa7SHerbert Xu return NULL; 1097f285fa7SHerbert Xu 1107f285fa7SHerbert Xu return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst)); 1117f285fa7SHerbert Xu } 1127f285fa7SHerbert Xu 1138ef2a9a5SYOSHIFUJI Hideaki static struct net_bridge_mdb_entry *br_mdb_ip4_get( 114b0e9a30dSVlad Yasevich struct net_bridge_mdb_htable *mdb, __be32 dst, __u16 vid) 115eb1d1641SHerbert Xu { 1168ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_dst; 1170821ec55SHerbert Xu 1188ef2a9a5SYOSHIFUJI Hideaki br_dst.u.ip4 = dst; 1198ef2a9a5SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IP); 120b0e9a30dSVlad Yasevich br_dst.vid = vid; 1218ef2a9a5SYOSHIFUJI Hideaki 1227f285fa7SHerbert Xu return br_mdb_ip_get(mdb, &br_dst); 1238ef2a9a5SYOSHIFUJI Hideaki } 1248ef2a9a5SYOSHIFUJI Hideaki 125dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 12608b202b6SYOSHIFUJI Hideaki static struct net_bridge_mdb_entry *br_mdb_ip6_get( 127b0e9a30dSVlad Yasevich struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst, 128b0e9a30dSVlad Yasevich __u16 vid) 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); 134b0e9a30dSVlad Yasevich br_dst.vid = vid; 13508b202b6SYOSHIFUJI Hideaki 1367f285fa7SHerbert Xu return br_mdb_ip_get(mdb, &br_dst); 13708b202b6SYOSHIFUJI Hideaki } 13808b202b6SYOSHIFUJI Hideaki #endif 13908b202b6SYOSHIFUJI Hideaki 140eb1d1641SHerbert Xu struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br, 141fbca58a2SCong Wang struct sk_buff *skb, u16 vid) 142eb1d1641SHerbert Xu { 143e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb); 1448ef2a9a5SYOSHIFUJI Hideaki struct br_ip ip; 1458ef2a9a5SYOSHIFUJI Hideaki 1467f285fa7SHerbert Xu if (br->multicast_disabled) 147eb1d1641SHerbert Xu return NULL; 148eb1d1641SHerbert Xu 1498ef2a9a5SYOSHIFUJI Hideaki if (BR_INPUT_SKB_CB(skb)->igmp) 1508ef2a9a5SYOSHIFUJI Hideaki return NULL; 1518ef2a9a5SYOSHIFUJI Hideaki 1528ef2a9a5SYOSHIFUJI Hideaki ip.proto = skb->protocol; 153fbca58a2SCong Wang ip.vid = vid; 1548ef2a9a5SYOSHIFUJI Hideaki 155eb1d1641SHerbert Xu switch (skb->protocol) { 156eb1d1641SHerbert Xu case htons(ETH_P_IP): 1578ef2a9a5SYOSHIFUJI Hideaki ip.u.ip4 = ip_hdr(skb)->daddr; 158eb1d1641SHerbert Xu break; 159dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 16008b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 1614e3fd7a0SAlexey Dobriyan ip.u.ip6 = ipv6_hdr(skb)->daddr; 16208b202b6SYOSHIFUJI Hideaki break; 16308b202b6SYOSHIFUJI Hideaki #endif 1648ef2a9a5SYOSHIFUJI Hideaki default: 1658ef2a9a5SYOSHIFUJI Hideaki return NULL; 166eb1d1641SHerbert Xu } 167eb1d1641SHerbert Xu 1688ef2a9a5SYOSHIFUJI Hideaki return br_mdb_ip_get(mdb, &ip); 169eb1d1641SHerbert Xu } 170eb1d1641SHerbert Xu 171eb1d1641SHerbert Xu static void br_mdb_free(struct rcu_head *head) 172eb1d1641SHerbert Xu { 173eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb = 174eb1d1641SHerbert Xu container_of(head, struct net_bridge_mdb_htable, rcu); 175eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old = mdb->old; 176eb1d1641SHerbert Xu 177eb1d1641SHerbert Xu mdb->old = NULL; 178eb1d1641SHerbert Xu kfree(old->mhash); 179eb1d1641SHerbert Xu kfree(old); 180eb1d1641SHerbert Xu } 181eb1d1641SHerbert Xu 182eb1d1641SHerbert Xu static int br_mdb_copy(struct net_bridge_mdb_htable *new, 183eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old, 184eb1d1641SHerbert Xu int elasticity) 185eb1d1641SHerbert Xu { 186eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 187eb1d1641SHerbert Xu int maxlen; 188eb1d1641SHerbert Xu int len; 189eb1d1641SHerbert Xu int i; 190eb1d1641SHerbert Xu 191eb1d1641SHerbert Xu for (i = 0; i < old->max; i++) 192b67bfe0dSSasha Levin hlist_for_each_entry(mp, &old->mhash[i], hlist[old->ver]) 193eb1d1641SHerbert Xu hlist_add_head(&mp->hlist[new->ver], 1948ef2a9a5SYOSHIFUJI Hideaki &new->mhash[br_ip_hash(new, &mp->addr)]); 195eb1d1641SHerbert Xu 196eb1d1641SHerbert Xu if (!elasticity) 197eb1d1641SHerbert Xu return 0; 198eb1d1641SHerbert Xu 199eb1d1641SHerbert Xu maxlen = 0; 200eb1d1641SHerbert Xu for (i = 0; i < new->max; i++) { 201eb1d1641SHerbert Xu len = 0; 202b67bfe0dSSasha Levin hlist_for_each_entry(mp, &new->mhash[i], hlist[new->ver]) 203eb1d1641SHerbert Xu len++; 204eb1d1641SHerbert Xu if (len > maxlen) 205eb1d1641SHerbert Xu maxlen = len; 206eb1d1641SHerbert Xu } 207eb1d1641SHerbert Xu 208eb1d1641SHerbert Xu return maxlen > elasticity ? -EINVAL : 0; 209eb1d1641SHerbert Xu } 210eb1d1641SHerbert Xu 211cfd56754SCong Wang void br_multicast_free_pg(struct rcu_head *head) 212eb1d1641SHerbert Xu { 213eb1d1641SHerbert Xu struct net_bridge_port_group *p = 214eb1d1641SHerbert Xu container_of(head, struct net_bridge_port_group, rcu); 215eb1d1641SHerbert Xu 216eb1d1641SHerbert Xu kfree(p); 217eb1d1641SHerbert Xu } 218eb1d1641SHerbert Xu 219eb1d1641SHerbert Xu static void br_multicast_free_group(struct rcu_head *head) 220eb1d1641SHerbert Xu { 221eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = 222eb1d1641SHerbert Xu container_of(head, struct net_bridge_mdb_entry, rcu); 223eb1d1641SHerbert Xu 224eb1d1641SHerbert Xu kfree(mp); 225eb1d1641SHerbert Xu } 226eb1d1641SHerbert Xu 227eb1d1641SHerbert Xu static void br_multicast_group_expired(unsigned long data) 228eb1d1641SHerbert Xu { 229eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = (void *)data; 230eb1d1641SHerbert Xu struct net_bridge *br = mp->br; 231eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 232eb1d1641SHerbert Xu 233eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 234eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&mp->timer)) 235eb1d1641SHerbert Xu goto out; 236eb1d1641SHerbert Xu 2378a870178SHerbert Xu mp->mglist = false; 238eb1d1641SHerbert Xu 239eb1d1641SHerbert Xu if (mp->ports) 240eb1d1641SHerbert Xu goto out; 241eb1d1641SHerbert Xu 242e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 243e8051688SEric Dumazet 244eb1d1641SHerbert Xu hlist_del_rcu(&mp->hlist[mdb->ver]); 245eb1d1641SHerbert Xu mdb->size--; 246eb1d1641SHerbert Xu 247eb1d1641SHerbert Xu call_rcu_bh(&mp->rcu, br_multicast_free_group); 248eb1d1641SHerbert Xu 249eb1d1641SHerbert Xu out: 250eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 251eb1d1641SHerbert Xu } 252eb1d1641SHerbert Xu 253eb1d1641SHerbert Xu static void br_multicast_del_pg(struct net_bridge *br, 254eb1d1641SHerbert Xu struct net_bridge_port_group *pg) 255eb1d1641SHerbert Xu { 256e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 257eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 258eb1d1641SHerbert Xu struct net_bridge_port_group *p; 259e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 260e8051688SEric Dumazet 261e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 262eb1d1641SHerbert Xu 2638ef2a9a5SYOSHIFUJI Hideaki mp = br_mdb_ip_get(mdb, &pg->addr); 264eb1d1641SHerbert Xu if (WARN_ON(!mp)) 265eb1d1641SHerbert Xu return; 266eb1d1641SHerbert Xu 267e8051688SEric Dumazet for (pp = &mp->ports; 268e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 269e8051688SEric Dumazet pp = &p->next) { 270eb1d1641SHerbert Xu if (p != pg) 271eb1d1641SHerbert Xu continue; 272eb1d1641SHerbert Xu 27383f6a740Sstephen hemminger rcu_assign_pointer(*pp, p->next); 274eb1d1641SHerbert Xu hlist_del_init(&p->mglist); 275eb1d1641SHerbert Xu del_timer(&p->timer); 276ef8299deSNikolay Aleksandrov br_mdb_notify(br->dev, p->port, &pg->addr, RTM_DELMDB, 277ef8299deSNikolay Aleksandrov p->state); 278eb1d1641SHerbert Xu call_rcu_bh(&p->rcu, br_multicast_free_pg); 279eb1d1641SHerbert Xu 280454594f3SLinus Lüssing if (!mp->ports && !mp->mglist && 281eb1d1641SHerbert Xu netif_running(br->dev)) 282eb1d1641SHerbert Xu mod_timer(&mp->timer, jiffies); 283eb1d1641SHerbert Xu 284eb1d1641SHerbert Xu return; 285eb1d1641SHerbert Xu } 286eb1d1641SHerbert Xu 287eb1d1641SHerbert Xu WARN_ON(1); 288eb1d1641SHerbert Xu } 289eb1d1641SHerbert Xu 290eb1d1641SHerbert Xu static void br_multicast_port_group_expired(unsigned long data) 291eb1d1641SHerbert Xu { 292eb1d1641SHerbert Xu struct net_bridge_port_group *pg = (void *)data; 293eb1d1641SHerbert Xu struct net_bridge *br = pg->port->br; 294eb1d1641SHerbert Xu 295eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 296eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&pg->timer) || 297ccb1c31aSAmerigo Wang hlist_unhashed(&pg->mglist) || pg->state & MDB_PERMANENT) 298eb1d1641SHerbert Xu goto out; 299eb1d1641SHerbert Xu 300eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 301eb1d1641SHerbert Xu 302eb1d1641SHerbert Xu out: 303eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 304eb1d1641SHerbert Xu } 305eb1d1641SHerbert Xu 306e8051688SEric Dumazet static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max, 307eb1d1641SHerbert Xu int elasticity) 308eb1d1641SHerbert Xu { 309e8051688SEric Dumazet struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1); 310eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 311eb1d1641SHerbert Xu int err; 312eb1d1641SHerbert Xu 313eb1d1641SHerbert Xu mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC); 314eb1d1641SHerbert Xu if (!mdb) 315eb1d1641SHerbert Xu return -ENOMEM; 316eb1d1641SHerbert Xu 317eb1d1641SHerbert Xu mdb->max = max; 318eb1d1641SHerbert Xu mdb->old = old; 319eb1d1641SHerbert Xu 320eb1d1641SHerbert Xu mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC); 321eb1d1641SHerbert Xu if (!mdb->mhash) { 322eb1d1641SHerbert Xu kfree(mdb); 323eb1d1641SHerbert Xu return -ENOMEM; 324eb1d1641SHerbert Xu } 325eb1d1641SHerbert Xu 326eb1d1641SHerbert Xu mdb->size = old ? old->size : 0; 327eb1d1641SHerbert Xu mdb->ver = old ? old->ver ^ 1 : 0; 328eb1d1641SHerbert Xu 329eb1d1641SHerbert Xu if (!old || elasticity) 330eb1d1641SHerbert Xu get_random_bytes(&mdb->secret, sizeof(mdb->secret)); 331eb1d1641SHerbert Xu else 332eb1d1641SHerbert Xu mdb->secret = old->secret; 333eb1d1641SHerbert Xu 334eb1d1641SHerbert Xu if (!old) 335eb1d1641SHerbert Xu goto out; 336eb1d1641SHerbert Xu 337eb1d1641SHerbert Xu err = br_mdb_copy(mdb, old, elasticity); 338eb1d1641SHerbert Xu if (err) { 339eb1d1641SHerbert Xu kfree(mdb->mhash); 340eb1d1641SHerbert Xu kfree(mdb); 341eb1d1641SHerbert Xu return err; 342eb1d1641SHerbert Xu } 343eb1d1641SHerbert Xu 3442ce297fcSCong Wang br_mdb_rehash_seq++; 345eb1d1641SHerbert Xu call_rcu_bh(&mdb->rcu, br_mdb_free); 346eb1d1641SHerbert Xu 347eb1d1641SHerbert Xu out: 348eb1d1641SHerbert Xu rcu_assign_pointer(*mdbp, mdb); 349eb1d1641SHerbert Xu 350eb1d1641SHerbert Xu return 0; 351eb1d1641SHerbert Xu } 352eb1d1641SHerbert Xu 3538ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br, 354eb1d1641SHerbert Xu __be32 group) 355eb1d1641SHerbert Xu { 356eb1d1641SHerbert Xu struct sk_buff *skb; 357eb1d1641SHerbert Xu struct igmphdr *ih; 358eb1d1641SHerbert Xu struct ethhdr *eth; 359eb1d1641SHerbert Xu struct iphdr *iph; 360eb1d1641SHerbert Xu 361eb1d1641SHerbert Xu skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) + 362eb1d1641SHerbert Xu sizeof(*ih) + 4); 363eb1d1641SHerbert Xu if (!skb) 364eb1d1641SHerbert Xu goto out; 365eb1d1641SHerbert Xu 366eb1d1641SHerbert Xu skb->protocol = htons(ETH_P_IP); 367eb1d1641SHerbert Xu 368eb1d1641SHerbert Xu skb_reset_mac_header(skb); 369eb1d1641SHerbert Xu eth = eth_hdr(skb); 370eb1d1641SHerbert Xu 371e5a727f6SJoe Perches ether_addr_copy(eth->h_source, br->dev->dev_addr); 372eb1d1641SHerbert Xu eth->h_dest[0] = 1; 373eb1d1641SHerbert Xu eth->h_dest[1] = 0; 374eb1d1641SHerbert Xu eth->h_dest[2] = 0x5e; 375eb1d1641SHerbert Xu eth->h_dest[3] = 0; 376eb1d1641SHerbert Xu eth->h_dest[4] = 0; 377eb1d1641SHerbert Xu eth->h_dest[5] = 1; 378eb1d1641SHerbert Xu eth->h_proto = htons(ETH_P_IP); 379eb1d1641SHerbert Xu skb_put(skb, sizeof(*eth)); 380eb1d1641SHerbert Xu 381eb1d1641SHerbert Xu skb_set_network_header(skb, skb->len); 382eb1d1641SHerbert Xu iph = ip_hdr(skb); 383eb1d1641SHerbert Xu 384eb1d1641SHerbert Xu iph->version = 4; 385eb1d1641SHerbert Xu iph->ihl = 6; 386eb1d1641SHerbert Xu iph->tos = 0xc0; 387eb1d1641SHerbert Xu iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4); 388eb1d1641SHerbert Xu iph->id = 0; 389eb1d1641SHerbert Xu iph->frag_off = htons(IP_DF); 390eb1d1641SHerbert Xu iph->ttl = 1; 391eb1d1641SHerbert Xu iph->protocol = IPPROTO_IGMP; 3921c8ad5bfSCong Wang iph->saddr = br->multicast_query_use_ifaddr ? 3931c8ad5bfSCong Wang inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0; 394eb1d1641SHerbert Xu iph->daddr = htonl(INADDR_ALLHOSTS_GROUP); 395eb1d1641SHerbert Xu ((u8 *)&iph[1])[0] = IPOPT_RA; 396eb1d1641SHerbert Xu ((u8 *)&iph[1])[1] = 4; 397eb1d1641SHerbert Xu ((u8 *)&iph[1])[2] = 0; 398eb1d1641SHerbert Xu ((u8 *)&iph[1])[3] = 0; 399eb1d1641SHerbert Xu ip_send_check(iph); 400eb1d1641SHerbert Xu skb_put(skb, 24); 401eb1d1641SHerbert Xu 402eb1d1641SHerbert Xu skb_set_transport_header(skb, skb->len); 403eb1d1641SHerbert Xu ih = igmp_hdr(skb); 404eb1d1641SHerbert Xu ih->type = IGMP_HOST_MEMBERSHIP_QUERY; 405eb1d1641SHerbert Xu ih->code = (group ? br->multicast_last_member_interval : 406eb1d1641SHerbert Xu br->multicast_query_response_interval) / 407eb1d1641SHerbert Xu (HZ / IGMP_TIMER_SCALE); 408eb1d1641SHerbert Xu ih->group = group; 409eb1d1641SHerbert Xu ih->csum = 0; 410eb1d1641SHerbert Xu ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr)); 411eb1d1641SHerbert Xu skb_put(skb, sizeof(*ih)); 412eb1d1641SHerbert Xu 413eb1d1641SHerbert Xu __skb_pull(skb, sizeof(*eth)); 414eb1d1641SHerbert Xu 415eb1d1641SHerbert Xu out: 416eb1d1641SHerbert Xu return skb; 417eb1d1641SHerbert Xu } 418eb1d1641SHerbert Xu 419dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 42008b202b6SYOSHIFUJI Hideaki static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br, 421b71d1d42SEric Dumazet const struct in6_addr *group) 42208b202b6SYOSHIFUJI Hideaki { 42308b202b6SYOSHIFUJI Hideaki struct sk_buff *skb; 42408b202b6SYOSHIFUJI Hideaki struct ipv6hdr *ip6h; 42508b202b6SYOSHIFUJI Hideaki struct mld_msg *mldq; 42608b202b6SYOSHIFUJI Hideaki struct ethhdr *eth; 42708b202b6SYOSHIFUJI Hideaki u8 *hopopt; 42808b202b6SYOSHIFUJI Hideaki unsigned long interval; 42908b202b6SYOSHIFUJI Hideaki 43008b202b6SYOSHIFUJI Hideaki skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) + 43108b202b6SYOSHIFUJI Hideaki 8 + sizeof(*mldq)); 43208b202b6SYOSHIFUJI Hideaki if (!skb) 43308b202b6SYOSHIFUJI Hideaki goto out; 43408b202b6SYOSHIFUJI Hideaki 43508b202b6SYOSHIFUJI Hideaki skb->protocol = htons(ETH_P_IPV6); 43608b202b6SYOSHIFUJI Hideaki 43708b202b6SYOSHIFUJI Hideaki /* Ethernet header */ 43808b202b6SYOSHIFUJI Hideaki skb_reset_mac_header(skb); 43908b202b6SYOSHIFUJI Hideaki eth = eth_hdr(skb); 44008b202b6SYOSHIFUJI Hideaki 441e5a727f6SJoe Perches ether_addr_copy(eth->h_source, br->dev->dev_addr); 44208b202b6SYOSHIFUJI Hideaki eth->h_proto = htons(ETH_P_IPV6); 44308b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*eth)); 44408b202b6SYOSHIFUJI Hideaki 44508b202b6SYOSHIFUJI Hideaki /* IPv6 header + HbH option */ 44608b202b6SYOSHIFUJI Hideaki skb_set_network_header(skb, skb->len); 44708b202b6SYOSHIFUJI Hideaki ip6h = ipv6_hdr(skb); 44808b202b6SYOSHIFUJI Hideaki 44908b202b6SYOSHIFUJI Hideaki *(__force __be32 *)ip6h = htonl(0x60000000); 45076d66158SDavid Stevens ip6h->payload_len = htons(8 + sizeof(*mldq)); 45108b202b6SYOSHIFUJI Hideaki ip6h->nexthdr = IPPROTO_HOPOPTS; 45208b202b6SYOSHIFUJI Hideaki ip6h->hop_limit = 1; 453a7bff75bSLinus Lüssing ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1)); 454d1d81d4cSUlrich Weber if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0, 455d1d81d4cSUlrich Weber &ip6h->saddr)) { 456d1d81d4cSUlrich Weber kfree_skb(skb); 457d1d81d4cSUlrich Weber return NULL; 458d1d81d4cSUlrich Weber } 45936cff5a1SLinus Lüssing ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest); 46008b202b6SYOSHIFUJI Hideaki 46108b202b6SYOSHIFUJI Hideaki hopopt = (u8 *)(ip6h + 1); 46208b202b6SYOSHIFUJI Hideaki hopopt[0] = IPPROTO_ICMPV6; /* next hdr */ 46308b202b6SYOSHIFUJI Hideaki hopopt[1] = 0; /* length of HbH */ 46408b202b6SYOSHIFUJI Hideaki hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */ 46508b202b6SYOSHIFUJI Hideaki hopopt[3] = 2; /* Length of RA Option */ 46608b202b6SYOSHIFUJI Hideaki hopopt[4] = 0; /* Type = 0x0000 (MLD) */ 46708b202b6SYOSHIFUJI Hideaki hopopt[5] = 0; 4681de5a71cSEldad Zack hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */ 4691de5a71cSEldad Zack hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */ 47008b202b6SYOSHIFUJI Hideaki 47108b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*ip6h) + 8); 47208b202b6SYOSHIFUJI Hideaki 47308b202b6SYOSHIFUJI Hideaki /* ICMPv6 */ 47408b202b6SYOSHIFUJI Hideaki skb_set_transport_header(skb, skb->len); 47508b202b6SYOSHIFUJI Hideaki mldq = (struct mld_msg *) icmp6_hdr(skb); 47608b202b6SYOSHIFUJI Hideaki 47732de868cSLinus Lüssing interval = ipv6_addr_any(group) ? 47832de868cSLinus Lüssing br->multicast_query_response_interval : 47932de868cSLinus Lüssing br->multicast_last_member_interval; 48008b202b6SYOSHIFUJI Hideaki 48108b202b6SYOSHIFUJI Hideaki mldq->mld_type = ICMPV6_MGM_QUERY; 48208b202b6SYOSHIFUJI Hideaki mldq->mld_code = 0; 48308b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = 0; 48408b202b6SYOSHIFUJI Hideaki mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval)); 48508b202b6SYOSHIFUJI Hideaki mldq->mld_reserved = 0; 4864e3fd7a0SAlexey Dobriyan mldq->mld_mca = *group; 48708b202b6SYOSHIFUJI Hideaki 48808b202b6SYOSHIFUJI Hideaki /* checksum */ 48908b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, 49008b202b6SYOSHIFUJI Hideaki sizeof(*mldq), IPPROTO_ICMPV6, 49108b202b6SYOSHIFUJI Hideaki csum_partial(mldq, 49208b202b6SYOSHIFUJI Hideaki sizeof(*mldq), 0)); 49308b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*mldq)); 49408b202b6SYOSHIFUJI Hideaki 49508b202b6SYOSHIFUJI Hideaki __skb_pull(skb, sizeof(*eth)); 49608b202b6SYOSHIFUJI Hideaki 49708b202b6SYOSHIFUJI Hideaki out: 49808b202b6SYOSHIFUJI Hideaki return skb; 49908b202b6SYOSHIFUJI Hideaki } 50008b202b6SYOSHIFUJI Hideaki #endif 50108b202b6SYOSHIFUJI Hideaki 5028ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br, 5038ef2a9a5SYOSHIFUJI Hideaki struct br_ip *addr) 5048ef2a9a5SYOSHIFUJI Hideaki { 5058ef2a9a5SYOSHIFUJI Hideaki switch (addr->proto) { 5068ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 5078ef2a9a5SYOSHIFUJI Hideaki return br_ip4_multicast_alloc_query(br, addr->u.ip4); 508dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 50908b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 51008b202b6SYOSHIFUJI Hideaki return br_ip6_multicast_alloc_query(br, &addr->u.ip6); 51108b202b6SYOSHIFUJI Hideaki #endif 5128ef2a9a5SYOSHIFUJI Hideaki } 5138ef2a9a5SYOSHIFUJI Hideaki return NULL; 5148ef2a9a5SYOSHIFUJI Hideaki } 5158ef2a9a5SYOSHIFUJI Hideaki 516eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *br_multicast_get_group( 5178ef2a9a5SYOSHIFUJI Hideaki struct net_bridge *br, struct net_bridge_port *port, 5188ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group, int hash) 519eb1d1641SHerbert Xu { 520e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 521eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 52295c96174SEric Dumazet unsigned int count = 0; 52395c96174SEric Dumazet unsigned int max; 524eb1d1641SHerbert Xu int elasticity; 525eb1d1641SHerbert Xu int err; 526eb1d1641SHerbert Xu 527e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 528b67bfe0dSSasha Levin hlist_for_each_entry(mp, &mdb->mhash[hash], hlist[mdb->ver]) { 529eb1d1641SHerbert Xu count++; 5308ef2a9a5SYOSHIFUJI Hideaki if (unlikely(br_ip_equal(group, &mp->addr))) 531eb1d1641SHerbert Xu return mp; 532eb1d1641SHerbert Xu } 533eb1d1641SHerbert Xu 534eb1d1641SHerbert Xu elasticity = 0; 535eb1d1641SHerbert Xu max = mdb->max; 536eb1d1641SHerbert Xu 537eb1d1641SHerbert Xu if (unlikely(count > br->hash_elasticity && count)) { 538eb1d1641SHerbert Xu if (net_ratelimit()) 53928a16c97Sstephen hemminger br_info(br, "Multicast hash table " 540eb1d1641SHerbert Xu "chain limit reached: %s\n", 54128a16c97Sstephen hemminger port ? port->dev->name : br->dev->name); 542eb1d1641SHerbert Xu 543eb1d1641SHerbert Xu elasticity = br->hash_elasticity; 544eb1d1641SHerbert Xu } 545eb1d1641SHerbert Xu 546eb1d1641SHerbert Xu if (mdb->size >= max) { 547eb1d1641SHerbert Xu max *= 2; 548036be6dbSThomas Graf if (unlikely(max > br->hash_max)) { 549036be6dbSThomas Graf br_warn(br, "Multicast hash table maximum of %d " 550036be6dbSThomas Graf "reached, disabling snooping: %s\n", 551036be6dbSThomas Graf br->hash_max, 552036be6dbSThomas Graf port ? port->dev->name : br->dev->name); 553eb1d1641SHerbert Xu err = -E2BIG; 554eb1d1641SHerbert Xu disable: 555eb1d1641SHerbert Xu br->multicast_disabled = 1; 556eb1d1641SHerbert Xu goto err; 557eb1d1641SHerbert Xu } 558eb1d1641SHerbert Xu } 559eb1d1641SHerbert Xu 560eb1d1641SHerbert Xu if (max > mdb->max || elasticity) { 561eb1d1641SHerbert Xu if (mdb->old) { 562eb1d1641SHerbert Xu if (net_ratelimit()) 56328a16c97Sstephen hemminger br_info(br, "Multicast hash table " 564eb1d1641SHerbert Xu "on fire: %s\n", 56528a16c97Sstephen hemminger port ? port->dev->name : br->dev->name); 566eb1d1641SHerbert Xu err = -EEXIST; 567eb1d1641SHerbert Xu goto err; 568eb1d1641SHerbert Xu } 569eb1d1641SHerbert Xu 570eb1d1641SHerbert Xu err = br_mdb_rehash(&br->mdb, max, elasticity); 571eb1d1641SHerbert Xu if (err) { 57228a16c97Sstephen hemminger br_warn(br, "Cannot rehash multicast " 57328a16c97Sstephen hemminger "hash table, disabling snooping: %s, %d, %d\n", 57428a16c97Sstephen hemminger port ? port->dev->name : br->dev->name, 575eb1d1641SHerbert Xu mdb->size, err); 576eb1d1641SHerbert Xu goto disable; 577eb1d1641SHerbert Xu } 578eb1d1641SHerbert Xu 579eb1d1641SHerbert Xu err = -EAGAIN; 580eb1d1641SHerbert Xu goto err; 581eb1d1641SHerbert Xu } 582eb1d1641SHerbert Xu 583eb1d1641SHerbert Xu return NULL; 584eb1d1641SHerbert Xu 585eb1d1641SHerbert Xu err: 586eb1d1641SHerbert Xu mp = ERR_PTR(err); 587eb1d1641SHerbert Xu return mp; 588eb1d1641SHerbert Xu } 589eb1d1641SHerbert Xu 590cfd56754SCong Wang struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br, 591cfd56754SCong Wang struct net_bridge_port *port, struct br_ip *group) 592eb1d1641SHerbert Xu { 593e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 594eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 595eb1d1641SHerbert Xu int hash; 5964c0833bcSTobias Klauser int err; 597eb1d1641SHerbert Xu 598e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 599eb1d1641SHerbert Xu if (!mdb) { 6004c0833bcSTobias Klauser err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0); 6014c0833bcSTobias Klauser if (err) 6024c0833bcSTobias Klauser return ERR_PTR(err); 603eb1d1641SHerbert Xu goto rehash; 604eb1d1641SHerbert Xu } 605eb1d1641SHerbert Xu 606eb1d1641SHerbert Xu hash = br_ip_hash(mdb, group); 607eb1d1641SHerbert Xu mp = br_multicast_get_group(br, port, group, hash); 608eb1d1641SHerbert Xu switch (PTR_ERR(mp)) { 609eb1d1641SHerbert Xu case 0: 610eb1d1641SHerbert Xu break; 611eb1d1641SHerbert Xu 612eb1d1641SHerbert Xu case -EAGAIN: 613eb1d1641SHerbert Xu rehash: 614e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 615eb1d1641SHerbert Xu hash = br_ip_hash(mdb, group); 616eb1d1641SHerbert Xu break; 617eb1d1641SHerbert Xu 618eb1d1641SHerbert Xu default: 619eb1d1641SHerbert Xu goto out; 620eb1d1641SHerbert Xu } 621eb1d1641SHerbert Xu 622eb1d1641SHerbert Xu mp = kzalloc(sizeof(*mp), GFP_ATOMIC); 623eb1d1641SHerbert Xu if (unlikely(!mp)) 6244c0833bcSTobias Klauser return ERR_PTR(-ENOMEM); 625eb1d1641SHerbert Xu 626eb1d1641SHerbert Xu mp->br = br; 6278ef2a9a5SYOSHIFUJI Hideaki mp->addr = *group; 6281faabf2aSEric Dumazet setup_timer(&mp->timer, br_multicast_group_expired, 6291faabf2aSEric Dumazet (unsigned long)mp); 6301faabf2aSEric Dumazet 631eb1d1641SHerbert Xu hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]); 632eb1d1641SHerbert Xu mdb->size++; 633eb1d1641SHerbert Xu 634eb1d1641SHerbert Xu out: 635eb1d1641SHerbert Xu return mp; 636eb1d1641SHerbert Xu } 637eb1d1641SHerbert Xu 638cfd56754SCong Wang struct net_bridge_port_group *br_multicast_new_port_group( 639cfd56754SCong Wang struct net_bridge_port *port, 640cfd56754SCong Wang struct br_ip *group, 641ccb1c31aSAmerigo Wang struct net_bridge_port_group __rcu *next, 642ccb1c31aSAmerigo Wang unsigned char state) 643cfd56754SCong Wang { 644cfd56754SCong Wang struct net_bridge_port_group *p; 645cfd56754SCong Wang 646cfd56754SCong Wang p = kzalloc(sizeof(*p), GFP_ATOMIC); 647cfd56754SCong Wang if (unlikely(!p)) 648cfd56754SCong Wang return NULL; 649cfd56754SCong Wang 650cfd56754SCong Wang p->addr = *group; 651cfd56754SCong Wang p->port = port; 652ccb1c31aSAmerigo Wang p->state = state; 653eca2a43bSstephen hemminger rcu_assign_pointer(p->next, next); 654cfd56754SCong Wang hlist_add_head(&p->mglist, &port->mglist); 655cfd56754SCong Wang setup_timer(&p->timer, br_multicast_port_group_expired, 656cfd56754SCong Wang (unsigned long)p); 657cfd56754SCong Wang return p; 658cfd56754SCong Wang } 659cfd56754SCong Wang 660eb1d1641SHerbert Xu static int br_multicast_add_group(struct net_bridge *br, 6618ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 6628ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group) 663eb1d1641SHerbert Xu { 664eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 665eb1d1641SHerbert Xu struct net_bridge_port_group *p; 666e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 667454594f3SLinus Lüssing unsigned long now = jiffies; 668eb1d1641SHerbert Xu int err; 669eb1d1641SHerbert Xu 670eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 671eb1d1641SHerbert Xu if (!netif_running(br->dev) || 672eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 673eb1d1641SHerbert Xu goto out; 674eb1d1641SHerbert Xu 675eb1d1641SHerbert Xu mp = br_multicast_new_group(br, port, group); 676eb1d1641SHerbert Xu err = PTR_ERR(mp); 6774c0833bcSTobias Klauser if (IS_ERR(mp)) 678eb1d1641SHerbert Xu goto err; 679eb1d1641SHerbert Xu 680eb1d1641SHerbert Xu if (!port) { 6818a870178SHerbert Xu mp->mglist = true; 682454594f3SLinus Lüssing mod_timer(&mp->timer, now + br->multicast_membership_interval); 683eb1d1641SHerbert Xu goto out; 684eb1d1641SHerbert Xu } 685eb1d1641SHerbert Xu 686e8051688SEric Dumazet for (pp = &mp->ports; 687e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 688e8051688SEric Dumazet pp = &p->next) { 689eb1d1641SHerbert Xu if (p->port == port) 690454594f3SLinus Lüssing goto found; 691eb1d1641SHerbert Xu if ((unsigned long)p->port < (unsigned long)port) 692eb1d1641SHerbert Xu break; 693eb1d1641SHerbert Xu } 694eb1d1641SHerbert Xu 695ccb1c31aSAmerigo Wang p = br_multicast_new_port_group(port, group, *pp, MDB_TEMPORARY); 696eb1d1641SHerbert Xu if (unlikely(!p)) 697eb1d1641SHerbert Xu goto err; 698eb1d1641SHerbert Xu rcu_assign_pointer(*pp, p); 69909cf0211SNikolay Aleksandrov br_mdb_notify(br->dev, port, group, RTM_NEWMDB, MDB_TEMPORARY); 700eb1d1641SHerbert Xu 701454594f3SLinus Lüssing found: 702454594f3SLinus Lüssing mod_timer(&p->timer, now + br->multicast_membership_interval); 703eb1d1641SHerbert Xu out: 704eb1d1641SHerbert Xu err = 0; 705eb1d1641SHerbert Xu 706eb1d1641SHerbert Xu err: 707eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 708eb1d1641SHerbert Xu return err; 709eb1d1641SHerbert Xu } 710eb1d1641SHerbert Xu 7118ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_add_group(struct net_bridge *br, 7128ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 713b0e9a30dSVlad Yasevich __be32 group, 714b0e9a30dSVlad Yasevich __u16 vid) 7158ef2a9a5SYOSHIFUJI Hideaki { 7168ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 7178ef2a9a5SYOSHIFUJI Hideaki 7188ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 7198ef2a9a5SYOSHIFUJI Hideaki return 0; 7208ef2a9a5SYOSHIFUJI Hideaki 7218ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 7228ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 723b0e9a30dSVlad Yasevich br_group.vid = vid; 7248ef2a9a5SYOSHIFUJI Hideaki 7258ef2a9a5SYOSHIFUJI Hideaki return br_multicast_add_group(br, port, &br_group); 7268ef2a9a5SYOSHIFUJI Hideaki } 7278ef2a9a5SYOSHIFUJI Hideaki 728dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 72908b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_add_group(struct net_bridge *br, 73008b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 731b0e9a30dSVlad Yasevich const struct in6_addr *group, 732b0e9a30dSVlad Yasevich __u16 vid) 73308b202b6SYOSHIFUJI Hideaki { 73408b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 73508b202b6SYOSHIFUJI Hideaki 7363c3769e6SLinus Lüssing if (ipv6_addr_is_ll_all_nodes(group)) 73708b202b6SYOSHIFUJI Hideaki return 0; 73808b202b6SYOSHIFUJI Hideaki 7394e3fd7a0SAlexey Dobriyan br_group.u.ip6 = *group; 7409cc6e0c4SLinus Lüssing br_group.proto = htons(ETH_P_IPV6); 741b0e9a30dSVlad Yasevich br_group.vid = vid; 74208b202b6SYOSHIFUJI Hideaki 74308b202b6SYOSHIFUJI Hideaki return br_multicast_add_group(br, port, &br_group); 74408b202b6SYOSHIFUJI Hideaki } 74508b202b6SYOSHIFUJI Hideaki #endif 74608b202b6SYOSHIFUJI Hideaki 747eb1d1641SHerbert Xu static void br_multicast_router_expired(unsigned long data) 748eb1d1641SHerbert Xu { 749eb1d1641SHerbert Xu struct net_bridge_port *port = (void *)data; 750eb1d1641SHerbert Xu struct net_bridge *br = port->br; 751eb1d1641SHerbert Xu 752eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 753eb1d1641SHerbert Xu if (port->multicast_router != 1 || 754eb1d1641SHerbert Xu timer_pending(&port->multicast_router_timer) || 755eb1d1641SHerbert Xu hlist_unhashed(&port->rlist)) 756eb1d1641SHerbert Xu goto out; 757eb1d1641SHerbert Xu 758eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 759eb1d1641SHerbert Xu 760eb1d1641SHerbert Xu out: 761eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 762eb1d1641SHerbert Xu } 763eb1d1641SHerbert Xu 764eb1d1641SHerbert Xu static void br_multicast_local_router_expired(unsigned long data) 765eb1d1641SHerbert Xu { 766eb1d1641SHerbert Xu } 767eb1d1641SHerbert Xu 768cc0fdd80SLinus Lüssing static void br_multicast_querier_expired(struct net_bridge *br, 76990010b36SLinus Lüssing struct bridge_mcast_own_query *query) 770c83b8fabSHerbert Xu { 771c83b8fabSHerbert Xu spin_lock(&br->multicast_lock); 772c83b8fabSHerbert Xu if (!netif_running(br->dev) || br->multicast_disabled) 773c83b8fabSHerbert Xu goto out; 774c83b8fabSHerbert Xu 775cc0fdd80SLinus Lüssing br_multicast_start_querier(br, query); 776c83b8fabSHerbert Xu 777c83b8fabSHerbert Xu out: 778c83b8fabSHerbert Xu spin_unlock(&br->multicast_lock); 779c83b8fabSHerbert Xu } 780c83b8fabSHerbert Xu 781cc0fdd80SLinus Lüssing static void br_ip4_multicast_querier_expired(unsigned long data) 782cc0fdd80SLinus Lüssing { 783cc0fdd80SLinus Lüssing struct net_bridge *br = (void *)data; 784cc0fdd80SLinus Lüssing 78590010b36SLinus Lüssing br_multicast_querier_expired(br, &br->ip4_own_query); 786cc0fdd80SLinus Lüssing } 787cc0fdd80SLinus Lüssing 788cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 789cc0fdd80SLinus Lüssing static void br_ip6_multicast_querier_expired(unsigned long data) 790cc0fdd80SLinus Lüssing { 791cc0fdd80SLinus Lüssing struct net_bridge *br = (void *)data; 792cc0fdd80SLinus Lüssing 79390010b36SLinus Lüssing br_multicast_querier_expired(br, &br->ip6_own_query); 794cc0fdd80SLinus Lüssing } 795cc0fdd80SLinus Lüssing #endif 796cc0fdd80SLinus Lüssing 797dc4eb53aSLinus Lüssing static void br_multicast_select_own_querier(struct net_bridge *br, 798dc4eb53aSLinus Lüssing struct br_ip *ip, 799dc4eb53aSLinus Lüssing struct sk_buff *skb) 800dc4eb53aSLinus Lüssing { 801dc4eb53aSLinus Lüssing if (ip->proto == htons(ETH_P_IP)) 802dc4eb53aSLinus Lüssing br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr; 803dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 804dc4eb53aSLinus Lüssing else 805dc4eb53aSLinus Lüssing br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr; 806dc4eb53aSLinus Lüssing #endif 807dc4eb53aSLinus Lüssing } 808dc4eb53aSLinus Lüssing 8098ef2a9a5SYOSHIFUJI Hideaki static void __br_multicast_send_query(struct net_bridge *br, 8108ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 8118ef2a9a5SYOSHIFUJI Hideaki struct br_ip *ip) 812eb1d1641SHerbert Xu { 813eb1d1641SHerbert Xu struct sk_buff *skb; 814eb1d1641SHerbert Xu 8158ef2a9a5SYOSHIFUJI Hideaki skb = br_multicast_alloc_query(br, ip); 816eb1d1641SHerbert Xu if (!skb) 8178ef2a9a5SYOSHIFUJI Hideaki return; 818eb1d1641SHerbert Xu 819eb1d1641SHerbert Xu if (port) { 820eb1d1641SHerbert Xu skb->dev = port->dev; 8217026b1ddSDavid Miller NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, NULL, skb, 8227026b1ddSDavid Miller NULL, skb->dev, 823f0b4eeceSLinus Lüssing br_dev_queue_push_xmit); 824dc4eb53aSLinus Lüssing } else { 825dc4eb53aSLinus Lüssing br_multicast_select_own_querier(br, ip, skb); 826eb1d1641SHerbert Xu netif_rx(skb); 8278ef2a9a5SYOSHIFUJI Hideaki } 828dc4eb53aSLinus Lüssing } 829eb1d1641SHerbert Xu 8308ef2a9a5SYOSHIFUJI Hideaki static void br_multicast_send_query(struct net_bridge *br, 831cc0fdd80SLinus Lüssing struct net_bridge_port *port, 83290010b36SLinus Lüssing struct bridge_mcast_own_query *own_query) 8338ef2a9a5SYOSHIFUJI Hideaki { 8348ef2a9a5SYOSHIFUJI Hideaki unsigned long time; 8358ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 83690010b36SLinus Lüssing struct bridge_mcast_other_query *other_query = NULL; 8378ef2a9a5SYOSHIFUJI Hideaki 8388ef2a9a5SYOSHIFUJI Hideaki if (!netif_running(br->dev) || br->multicast_disabled || 839cc0fdd80SLinus Lüssing !br->multicast_querier) 8408ef2a9a5SYOSHIFUJI Hideaki return; 8418ef2a9a5SYOSHIFUJI Hideaki 84208b202b6SYOSHIFUJI Hideaki memset(&br_group.u, 0, sizeof(br_group.u)); 8438ef2a9a5SYOSHIFUJI Hideaki 84490010b36SLinus Lüssing if (port ? (own_query == &port->ip4_own_query) : 84590010b36SLinus Lüssing (own_query == &br->ip4_own_query)) { 84690010b36SLinus Lüssing other_query = &br->ip4_other_query; 84708b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 848dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 849cc0fdd80SLinus Lüssing } else { 85090010b36SLinus Lüssing other_query = &br->ip6_other_query; 85108b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 85208b202b6SYOSHIFUJI Hideaki #endif 853eb1d1641SHerbert Xu } 854eb1d1641SHerbert Xu 85590010b36SLinus Lüssing if (!other_query || timer_pending(&other_query->timer)) 856cc0fdd80SLinus Lüssing return; 857cc0fdd80SLinus Lüssing 858cc0fdd80SLinus Lüssing __br_multicast_send_query(br, port, &br_group); 859cc0fdd80SLinus Lüssing 860cc0fdd80SLinus Lüssing time = jiffies; 86190010b36SLinus Lüssing time += own_query->startup_sent < br->multicast_startup_query_count ? 862cc0fdd80SLinus Lüssing br->multicast_startup_query_interval : 863cc0fdd80SLinus Lüssing br->multicast_query_interval; 86490010b36SLinus Lüssing mod_timer(&own_query->timer, time); 865cc0fdd80SLinus Lüssing } 866cc0fdd80SLinus Lüssing 86790010b36SLinus Lüssing static void 86890010b36SLinus Lüssing br_multicast_port_query_expired(struct net_bridge_port *port, 86990010b36SLinus Lüssing struct bridge_mcast_own_query *query) 870eb1d1641SHerbert Xu { 871eb1d1641SHerbert Xu struct net_bridge *br = port->br; 872eb1d1641SHerbert Xu 873eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 87402a780c0SDan Carpenter if (port->state == BR_STATE_DISABLED || 87502a780c0SDan Carpenter port->state == BR_STATE_BLOCKING) 876eb1d1641SHerbert Xu goto out; 877eb1d1641SHerbert Xu 878cc0fdd80SLinus Lüssing if (query->startup_sent < br->multicast_startup_query_count) 879cc0fdd80SLinus Lüssing query->startup_sent++; 880eb1d1641SHerbert Xu 881cc0fdd80SLinus Lüssing br_multicast_send_query(port->br, port, query); 882eb1d1641SHerbert Xu 883eb1d1641SHerbert Xu out: 884eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 885eb1d1641SHerbert Xu } 886eb1d1641SHerbert Xu 887cc0fdd80SLinus Lüssing static void br_ip4_multicast_port_query_expired(unsigned long data) 888cc0fdd80SLinus Lüssing { 889cc0fdd80SLinus Lüssing struct net_bridge_port *port = (void *)data; 890cc0fdd80SLinus Lüssing 89190010b36SLinus Lüssing br_multicast_port_query_expired(port, &port->ip4_own_query); 892cc0fdd80SLinus Lüssing } 893cc0fdd80SLinus Lüssing 894cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 895cc0fdd80SLinus Lüssing static void br_ip6_multicast_port_query_expired(unsigned long data) 896cc0fdd80SLinus Lüssing { 897cc0fdd80SLinus Lüssing struct net_bridge_port *port = (void *)data; 898cc0fdd80SLinus Lüssing 89990010b36SLinus Lüssing br_multicast_port_query_expired(port, &port->ip6_own_query); 900cc0fdd80SLinus Lüssing } 901cc0fdd80SLinus Lüssing #endif 902cc0fdd80SLinus Lüssing 903eb1d1641SHerbert Xu void br_multicast_add_port(struct net_bridge_port *port) 904eb1d1641SHerbert Xu { 905eb1d1641SHerbert Xu port->multicast_router = 1; 906eb1d1641SHerbert Xu 907eb1d1641SHerbert Xu setup_timer(&port->multicast_router_timer, br_multicast_router_expired, 908eb1d1641SHerbert Xu (unsigned long)port); 90990010b36SLinus Lüssing setup_timer(&port->ip4_own_query.timer, 91090010b36SLinus Lüssing br_ip4_multicast_port_query_expired, (unsigned long)port); 911cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 91290010b36SLinus Lüssing setup_timer(&port->ip6_own_query.timer, 91390010b36SLinus Lüssing br_ip6_multicast_port_query_expired, (unsigned long)port); 914cc0fdd80SLinus Lüssing #endif 915eb1d1641SHerbert Xu } 916eb1d1641SHerbert Xu 917eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port) 918eb1d1641SHerbert Xu { 919*e10177abSSatish Ashok struct net_bridge *br = port->br; 920*e10177abSSatish Ashok struct net_bridge_port_group *pg; 921*e10177abSSatish Ashok struct hlist_node *n; 922*e10177abSSatish Ashok 923*e10177abSSatish Ashok /* Take care of the remaining groups, only perm ones should be left */ 924*e10177abSSatish Ashok spin_lock_bh(&br->multicast_lock); 925*e10177abSSatish Ashok hlist_for_each_entry_safe(pg, n, &port->mglist, mglist) 926*e10177abSSatish Ashok br_multicast_del_pg(br, pg); 927*e10177abSSatish Ashok spin_unlock_bh(&br->multicast_lock); 928eb1d1641SHerbert Xu del_timer_sync(&port->multicast_router_timer); 929eb1d1641SHerbert Xu } 930eb1d1641SHerbert Xu 93190010b36SLinus Lüssing static void br_multicast_enable(struct bridge_mcast_own_query *query) 932561f1103SHerbert Xu { 933cc0fdd80SLinus Lüssing query->startup_sent = 0; 934561f1103SHerbert Xu 935cc0fdd80SLinus Lüssing if (try_to_del_timer_sync(&query->timer) >= 0 || 936cc0fdd80SLinus Lüssing del_timer(&query->timer)) 937cc0fdd80SLinus Lüssing mod_timer(&query->timer, jiffies); 938561f1103SHerbert Xu } 939561f1103SHerbert Xu 940eb1d1641SHerbert Xu void br_multicast_enable_port(struct net_bridge_port *port) 941eb1d1641SHerbert Xu { 942eb1d1641SHerbert Xu struct net_bridge *br = port->br; 943eb1d1641SHerbert Xu 944eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 945eb1d1641SHerbert Xu if (br->multicast_disabled || !netif_running(br->dev)) 946eb1d1641SHerbert Xu goto out; 947eb1d1641SHerbert Xu 94890010b36SLinus Lüssing br_multicast_enable(&port->ip4_own_query); 949cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 95090010b36SLinus Lüssing br_multicast_enable(&port->ip6_own_query); 951cc0fdd80SLinus Lüssing #endif 952754bc547SSatish Ashok if (port->multicast_router == 2 && hlist_unhashed(&port->rlist)) 953754bc547SSatish Ashok br_multicast_add_router(br, port); 954eb1d1641SHerbert Xu 955eb1d1641SHerbert Xu out: 956eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 957eb1d1641SHerbert Xu } 958eb1d1641SHerbert Xu 959eb1d1641SHerbert Xu void br_multicast_disable_port(struct net_bridge_port *port) 960eb1d1641SHerbert Xu { 961eb1d1641SHerbert Xu struct net_bridge *br = port->br; 962eb1d1641SHerbert Xu struct net_bridge_port_group *pg; 963b67bfe0dSSasha Levin struct hlist_node *n; 964eb1d1641SHerbert Xu 965eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 966b67bfe0dSSasha Levin hlist_for_each_entry_safe(pg, n, &port->mglist, mglist) 967*e10177abSSatish Ashok if (pg->state == MDB_TEMPORARY) 968eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 969eb1d1641SHerbert Xu 970eb1d1641SHerbert Xu if (!hlist_unhashed(&port->rlist)) 971eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 972eb1d1641SHerbert Xu del_timer(&port->multicast_router_timer); 97390010b36SLinus Lüssing del_timer(&port->ip4_own_query.timer); 974cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 97590010b36SLinus Lüssing del_timer(&port->ip6_own_query.timer); 976cc0fdd80SLinus Lüssing #endif 977eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 978eb1d1641SHerbert Xu } 979eb1d1641SHerbert Xu 9808ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_igmp3_report(struct net_bridge *br, 981eb1d1641SHerbert Xu struct net_bridge_port *port, 98206499098SVlad Yasevich struct sk_buff *skb, 98306499098SVlad Yasevich u16 vid) 984eb1d1641SHerbert Xu { 985eb1d1641SHerbert Xu struct igmpv3_report *ih; 986eb1d1641SHerbert Xu struct igmpv3_grec *grec; 987eb1d1641SHerbert Xu int i; 988eb1d1641SHerbert Xu int len; 989eb1d1641SHerbert Xu int num; 990eb1d1641SHerbert Xu int type; 991eb1d1641SHerbert Xu int err = 0; 992eb1d1641SHerbert Xu __be32 group; 993eb1d1641SHerbert Xu 994eb1d1641SHerbert Xu ih = igmpv3_report_hdr(skb); 995eb1d1641SHerbert Xu num = ntohs(ih->ngrec); 996eb1d1641SHerbert Xu len = sizeof(*ih); 997eb1d1641SHerbert Xu 998eb1d1641SHerbert Xu for (i = 0; i < num; i++) { 999eb1d1641SHerbert Xu len += sizeof(*grec); 1000eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 1001eb1d1641SHerbert Xu return -EINVAL; 1002eb1d1641SHerbert Xu 1003fd218cf9SHerbert Xu grec = (void *)(skb->data + len - sizeof(*grec)); 1004eb1d1641SHerbert Xu group = grec->grec_mca; 1005eb1d1641SHerbert Xu type = grec->grec_type; 1006eb1d1641SHerbert Xu 10078eabf95cSEric Dumazet len += ntohs(grec->grec_nsrcs) * 4; 1008eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 1009eb1d1641SHerbert Xu return -EINVAL; 1010eb1d1641SHerbert Xu 1011eb1d1641SHerbert Xu /* We treat this as an IGMPv2 report for now. */ 1012eb1d1641SHerbert Xu switch (type) { 1013eb1d1641SHerbert Xu case IGMPV3_MODE_IS_INCLUDE: 1014eb1d1641SHerbert Xu case IGMPV3_MODE_IS_EXCLUDE: 1015eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_INCLUDE: 1016eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_EXCLUDE: 1017eb1d1641SHerbert Xu case IGMPV3_ALLOW_NEW_SOURCES: 1018eb1d1641SHerbert Xu case IGMPV3_BLOCK_OLD_SOURCES: 1019eb1d1641SHerbert Xu break; 1020eb1d1641SHerbert Xu 1021eb1d1641SHerbert Xu default: 1022eb1d1641SHerbert Xu continue; 1023eb1d1641SHerbert Xu } 1024eb1d1641SHerbert Xu 1025b0e9a30dSVlad Yasevich err = br_ip4_multicast_add_group(br, port, group, vid); 1026eb1d1641SHerbert Xu if (err) 1027eb1d1641SHerbert Xu break; 1028eb1d1641SHerbert Xu } 1029eb1d1641SHerbert Xu 1030eb1d1641SHerbert Xu return err; 1031eb1d1641SHerbert Xu } 1032eb1d1641SHerbert Xu 1033dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 103408b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_mld2_report(struct net_bridge *br, 103508b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 103606499098SVlad Yasevich struct sk_buff *skb, 103706499098SVlad Yasevich u16 vid) 103808b202b6SYOSHIFUJI Hideaki { 103908b202b6SYOSHIFUJI Hideaki struct icmp6hdr *icmp6h; 104008b202b6SYOSHIFUJI Hideaki struct mld2_grec *grec; 104108b202b6SYOSHIFUJI Hideaki int i; 104208b202b6SYOSHIFUJI Hideaki int len; 104308b202b6SYOSHIFUJI Hideaki int num; 104408b202b6SYOSHIFUJI Hideaki int err = 0; 104508b202b6SYOSHIFUJI Hideaki 104608b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*icmp6h))) 104708b202b6SYOSHIFUJI Hideaki return -EINVAL; 104808b202b6SYOSHIFUJI Hideaki 104908b202b6SYOSHIFUJI Hideaki icmp6h = icmp6_hdr(skb); 105008b202b6SYOSHIFUJI Hideaki num = ntohs(icmp6h->icmp6_dataun.un_data16[1]); 105108b202b6SYOSHIFUJI Hideaki len = sizeof(*icmp6h); 105208b202b6SYOSHIFUJI Hideaki 105308b202b6SYOSHIFUJI Hideaki for (i = 0; i < num; i++) { 105408b202b6SYOSHIFUJI Hideaki __be16 *nsrcs, _nsrcs; 105508b202b6SYOSHIFUJI Hideaki 105608b202b6SYOSHIFUJI Hideaki nsrcs = skb_header_pointer(skb, 105708b202b6SYOSHIFUJI Hideaki len + offsetof(struct mld2_grec, 1058649e984dSLinus Lüssing grec_nsrcs), 105908b202b6SYOSHIFUJI Hideaki sizeof(_nsrcs), &_nsrcs); 106008b202b6SYOSHIFUJI Hideaki if (!nsrcs) 106108b202b6SYOSHIFUJI Hideaki return -EINVAL; 106208b202b6SYOSHIFUJI Hideaki 106308b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, 106408b202b6SYOSHIFUJI Hideaki len + sizeof(*grec) + 1065d41db9f3SLinus Lüssing sizeof(struct in6_addr) * ntohs(*nsrcs))) 106608b202b6SYOSHIFUJI Hideaki return -EINVAL; 106708b202b6SYOSHIFUJI Hideaki 106808b202b6SYOSHIFUJI Hideaki grec = (struct mld2_grec *)(skb->data + len); 1069d41db9f3SLinus Lüssing len += sizeof(*grec) + 1070d41db9f3SLinus Lüssing sizeof(struct in6_addr) * ntohs(*nsrcs); 107108b202b6SYOSHIFUJI Hideaki 107208b202b6SYOSHIFUJI Hideaki /* We treat these as MLDv1 reports for now. */ 107308b202b6SYOSHIFUJI Hideaki switch (grec->grec_type) { 107408b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_INCLUDE: 107508b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_EXCLUDE: 107608b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_INCLUDE: 107708b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_EXCLUDE: 107808b202b6SYOSHIFUJI Hideaki case MLD2_ALLOW_NEW_SOURCES: 107908b202b6SYOSHIFUJI Hideaki case MLD2_BLOCK_OLD_SOURCES: 108008b202b6SYOSHIFUJI Hideaki break; 108108b202b6SYOSHIFUJI Hideaki 108208b202b6SYOSHIFUJI Hideaki default: 108308b202b6SYOSHIFUJI Hideaki continue; 108408b202b6SYOSHIFUJI Hideaki } 108508b202b6SYOSHIFUJI Hideaki 1086b0e9a30dSVlad Yasevich err = br_ip6_multicast_add_group(br, port, &grec->grec_mca, 1087b0e9a30dSVlad Yasevich vid); 108847cc84ceSThadeu Lima de Souza Cascardo if (err) 108908b202b6SYOSHIFUJI Hideaki break; 109008b202b6SYOSHIFUJI Hideaki } 109108b202b6SYOSHIFUJI Hideaki 109208b202b6SYOSHIFUJI Hideaki return err; 109308b202b6SYOSHIFUJI Hideaki } 109408b202b6SYOSHIFUJI Hideaki #endif 109508b202b6SYOSHIFUJI Hideaki 1096dc4eb53aSLinus Lüssing static bool br_ip4_multicast_select_querier(struct net_bridge *br, 10972cd41431SLinus Lüssing struct net_bridge_port *port, 1098dc4eb53aSLinus Lüssing __be32 saddr) 1099dc4eb53aSLinus Lüssing { 1100dc4eb53aSLinus Lüssing if (!timer_pending(&br->ip4_own_query.timer) && 1101dc4eb53aSLinus Lüssing !timer_pending(&br->ip4_other_query.timer)) 1102dc4eb53aSLinus Lüssing goto update; 1103dc4eb53aSLinus Lüssing 1104dc4eb53aSLinus Lüssing if (!br->ip4_querier.addr.u.ip4) 1105dc4eb53aSLinus Lüssing goto update; 1106dc4eb53aSLinus Lüssing 1107dc4eb53aSLinus Lüssing if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4)) 1108dc4eb53aSLinus Lüssing goto update; 1109dc4eb53aSLinus Lüssing 1110dc4eb53aSLinus Lüssing return false; 1111dc4eb53aSLinus Lüssing 1112dc4eb53aSLinus Lüssing update: 1113dc4eb53aSLinus Lüssing br->ip4_querier.addr.u.ip4 = saddr; 1114dc4eb53aSLinus Lüssing 11152cd41431SLinus Lüssing /* update protected by general multicast_lock by caller */ 11162cd41431SLinus Lüssing rcu_assign_pointer(br->ip4_querier.port, port); 11172cd41431SLinus Lüssing 1118dc4eb53aSLinus Lüssing return true; 1119dc4eb53aSLinus Lüssing } 1120dc4eb53aSLinus Lüssing 1121dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1122dc4eb53aSLinus Lüssing static bool br_ip6_multicast_select_querier(struct net_bridge *br, 11232cd41431SLinus Lüssing struct net_bridge_port *port, 1124dc4eb53aSLinus Lüssing struct in6_addr *saddr) 1125dc4eb53aSLinus Lüssing { 1126dc4eb53aSLinus Lüssing if (!timer_pending(&br->ip6_own_query.timer) && 1127dc4eb53aSLinus Lüssing !timer_pending(&br->ip6_other_query.timer)) 1128dc4eb53aSLinus Lüssing goto update; 1129dc4eb53aSLinus Lüssing 1130dc4eb53aSLinus Lüssing if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0) 1131dc4eb53aSLinus Lüssing goto update; 1132dc4eb53aSLinus Lüssing 1133dc4eb53aSLinus Lüssing return false; 1134dc4eb53aSLinus Lüssing 1135dc4eb53aSLinus Lüssing update: 1136dc4eb53aSLinus Lüssing br->ip6_querier.addr.u.ip6 = *saddr; 1137dc4eb53aSLinus Lüssing 11382cd41431SLinus Lüssing /* update protected by general multicast_lock by caller */ 11392cd41431SLinus Lüssing rcu_assign_pointer(br->ip6_querier.port, port); 11402cd41431SLinus Lüssing 1141dc4eb53aSLinus Lüssing return true; 1142dc4eb53aSLinus Lüssing } 1143dc4eb53aSLinus Lüssing #endif 1144dc4eb53aSLinus Lüssing 1145dc4eb53aSLinus Lüssing static bool br_multicast_select_querier(struct net_bridge *br, 11462cd41431SLinus Lüssing struct net_bridge_port *port, 1147dc4eb53aSLinus Lüssing struct br_ip *saddr) 1148dc4eb53aSLinus Lüssing { 1149dc4eb53aSLinus Lüssing switch (saddr->proto) { 1150dc4eb53aSLinus Lüssing case htons(ETH_P_IP): 11512cd41431SLinus Lüssing return br_ip4_multicast_select_querier(br, port, saddr->u.ip4); 1152dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1153dc4eb53aSLinus Lüssing case htons(ETH_P_IPV6): 11542cd41431SLinus Lüssing return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6); 1155dc4eb53aSLinus Lüssing #endif 1156dc4eb53aSLinus Lüssing } 1157dc4eb53aSLinus Lüssing 1158dc4eb53aSLinus Lüssing return false; 1159dc4eb53aSLinus Lüssing } 1160dc4eb53aSLinus Lüssing 1161cc0fdd80SLinus Lüssing static void 116290010b36SLinus Lüssing br_multicast_update_query_timer(struct net_bridge *br, 116390010b36SLinus Lüssing struct bridge_mcast_other_query *query, 1164b00589afSLinus Lüssing unsigned long max_delay) 1165b00589afSLinus Lüssing { 116690010b36SLinus Lüssing if (!timer_pending(&query->timer)) 116790010b36SLinus Lüssing query->delay_time = jiffies + max_delay; 1168b00589afSLinus Lüssing 116990010b36SLinus Lüssing mod_timer(&query->timer, jiffies + br->multicast_querier_interval); 1170b00589afSLinus Lüssing } 1171b00589afSLinus Lüssing 11727e80c124Sstephen hemminger /* 11737c77602fSCong Wang * Add port to router_list 11747e80c124Sstephen hemminger * list is maintained ordered by pointer value 11757e80c124Sstephen hemminger * and locked by br->multicast_lock and RCU 11767e80c124Sstephen hemminger */ 11770909e117SHerbert Xu static void br_multicast_add_router(struct net_bridge *br, 11780909e117SHerbert Xu struct net_bridge_port *port) 11790909e117SHerbert Xu { 1180dcdca2c4Sstephen hemminger struct net_bridge_port *p; 1181b67bfe0dSSasha Levin struct hlist_node *slot = NULL; 11820909e117SHerbert Xu 11831a040eacSNikolay Aleksandrov if (!hlist_unhashed(&port->rlist)) 11841a040eacSNikolay Aleksandrov return; 11851a040eacSNikolay Aleksandrov 1186b67bfe0dSSasha Levin hlist_for_each_entry(p, &br->router_list, rlist) { 11877e80c124Sstephen hemminger if ((unsigned long) port >= (unsigned long) p) 11887e80c124Sstephen hemminger break; 1189b67bfe0dSSasha Levin slot = &p->rlist; 1190dcdca2c4Sstephen hemminger } 11910909e117SHerbert Xu 11927e80c124Sstephen hemminger if (slot) 11931d023284SKen Helias hlist_add_behind_rcu(&port->rlist, slot); 1194dcdca2c4Sstephen hemminger else 1195dcdca2c4Sstephen hemminger hlist_add_head_rcu(&port->rlist, &br->router_list); 11960909e117SHerbert Xu } 11970909e117SHerbert Xu 1198eb1d1641SHerbert Xu static void br_multicast_mark_router(struct net_bridge *br, 1199eb1d1641SHerbert Xu struct net_bridge_port *port) 1200eb1d1641SHerbert Xu { 1201eb1d1641SHerbert Xu unsigned long now = jiffies; 1202eb1d1641SHerbert Xu 1203eb1d1641SHerbert Xu if (!port) { 1204eb1d1641SHerbert Xu if (br->multicast_router == 1) 1205eb1d1641SHerbert Xu mod_timer(&br->multicast_router_timer, 1206eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1207eb1d1641SHerbert Xu return; 1208eb1d1641SHerbert Xu } 1209eb1d1641SHerbert Xu 1210eb1d1641SHerbert Xu if (port->multicast_router != 1) 1211eb1d1641SHerbert Xu return; 1212eb1d1641SHerbert Xu 12130909e117SHerbert Xu br_multicast_add_router(br, port); 1214eb1d1641SHerbert Xu 1215eb1d1641SHerbert Xu mod_timer(&port->multicast_router_timer, 1216eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1217eb1d1641SHerbert Xu } 1218eb1d1641SHerbert Xu 1219eb1d1641SHerbert Xu static void br_multicast_query_received(struct net_bridge *br, 1220eb1d1641SHerbert Xu struct net_bridge_port *port, 122190010b36SLinus Lüssing struct bridge_mcast_other_query *query, 1222dc4eb53aSLinus Lüssing struct br_ip *saddr, 1223b00589afSLinus Lüssing unsigned long max_delay) 1224eb1d1641SHerbert Xu { 12252cd41431SLinus Lüssing if (!br_multicast_select_querier(br, port, saddr)) 1226eb1d1641SHerbert Xu return; 1227eb1d1641SHerbert Xu 1228dc4eb53aSLinus Lüssing br_multicast_update_query_timer(br, query, max_delay); 1229eb1d1641SHerbert Xu br_multicast_mark_router(br, port); 1230eb1d1641SHerbert Xu } 1231eb1d1641SHerbert Xu 12328ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_query(struct net_bridge *br, 1233eb1d1641SHerbert Xu struct net_bridge_port *port, 123406499098SVlad Yasevich struct sk_buff *skb, 123506499098SVlad Yasevich u16 vid) 1236eb1d1641SHerbert Xu { 1237b71d1d42SEric Dumazet const struct iphdr *iph = ip_hdr(skb); 1238eb1d1641SHerbert Xu struct igmphdr *ih = igmp_hdr(skb); 1239eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1240eb1d1641SHerbert Xu struct igmpv3_query *ih3; 1241eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1242e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 1243dc4eb53aSLinus Lüssing struct br_ip saddr; 1244eb1d1641SHerbert Xu unsigned long max_delay; 1245eb1d1641SHerbert Xu unsigned long now = jiffies; 1246eb1d1641SHerbert Xu __be32 group; 1247bec68ff1SYOSHIFUJI Hideaki int err = 0; 1248eb1d1641SHerbert Xu 1249eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1250eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1251eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 1252eb1d1641SHerbert Xu goto out; 1253eb1d1641SHerbert Xu 1254eb1d1641SHerbert Xu group = ih->group; 1255eb1d1641SHerbert Xu 1256eb1d1641SHerbert Xu if (skb->len == sizeof(*ih)) { 1257eb1d1641SHerbert Xu max_delay = ih->code * (HZ / IGMP_TIMER_SCALE); 1258eb1d1641SHerbert Xu 1259eb1d1641SHerbert Xu if (!max_delay) { 1260eb1d1641SHerbert Xu max_delay = 10 * HZ; 1261eb1d1641SHerbert Xu group = 0; 1262eb1d1641SHerbert Xu } 12639afd85c9SLinus Lüssing } else if (skb->len >= sizeof(*ih3)) { 1264eb1d1641SHerbert Xu ih3 = igmpv3_query_hdr(skb); 1265eb1d1641SHerbert Xu if (ih3->nsrcs) 1266bec68ff1SYOSHIFUJI Hideaki goto out; 1267eb1d1641SHerbert Xu 12680ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 max_delay = ih3->code ? 12690ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1; 12709afd85c9SLinus Lüssing } else { 12719ed973ccSLinus Lüssing goto out; 12729ed973ccSLinus Lüssing } 12739ed973ccSLinus Lüssing 1274dc4eb53aSLinus Lüssing if (!group) { 1275dc4eb53aSLinus Lüssing saddr.proto = htons(ETH_P_IP); 1276dc4eb53aSLinus Lüssing saddr.u.ip4 = iph->saddr; 1277b00589afSLinus Lüssing 1278dc4eb53aSLinus Lüssing br_multicast_query_received(br, port, &br->ip4_other_query, 1279dc4eb53aSLinus Lüssing &saddr, max_delay); 1280eb1d1641SHerbert Xu goto out; 1281dc4eb53aSLinus Lüssing } 1282eb1d1641SHerbert Xu 1283b0e9a30dSVlad Yasevich mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid); 1284eb1d1641SHerbert Xu if (!mp) 1285eb1d1641SHerbert Xu goto out; 1286eb1d1641SHerbert Xu 1287eb1d1641SHerbert Xu max_delay *= br->multicast_last_member_count; 1288eb1d1641SHerbert Xu 12898a870178SHerbert Xu if (mp->mglist && 1290eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1291eb1d1641SHerbert Xu time_after(mp->timer.expires, now + max_delay) : 1292eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) 1293eb1d1641SHerbert Xu mod_timer(&mp->timer, now + max_delay); 1294eb1d1641SHerbert Xu 1295e8051688SEric Dumazet for (pp = &mp->ports; 1296e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 1297e8051688SEric Dumazet pp = &p->next) { 1298eb1d1641SHerbert Xu if (timer_pending(&p->timer) ? 1299eb1d1641SHerbert Xu time_after(p->timer.expires, now + max_delay) : 1300eb1d1641SHerbert Xu try_to_del_timer_sync(&p->timer) >= 0) 130124f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 1302eb1d1641SHerbert Xu } 1303eb1d1641SHerbert Xu 1304eb1d1641SHerbert Xu out: 1305eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1306bec68ff1SYOSHIFUJI Hideaki return err; 1307eb1d1641SHerbert Xu } 1308eb1d1641SHerbert Xu 1309dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 131008b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_query(struct net_bridge *br, 131108b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 131206499098SVlad Yasevich struct sk_buff *skb, 131306499098SVlad Yasevich u16 vid) 131408b202b6SYOSHIFUJI Hideaki { 1315b71d1d42SEric Dumazet const struct ipv6hdr *ip6h = ipv6_hdr(skb); 1316eca2a43bSstephen hemminger struct mld_msg *mld; 131708b202b6SYOSHIFUJI Hideaki struct net_bridge_mdb_entry *mp; 131808b202b6SYOSHIFUJI Hideaki struct mld2_query *mld2q; 1319e8051688SEric Dumazet struct net_bridge_port_group *p; 1320e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 1321dc4eb53aSLinus Lüssing struct br_ip saddr; 132208b202b6SYOSHIFUJI Hideaki unsigned long max_delay; 132308b202b6SYOSHIFUJI Hideaki unsigned long now = jiffies; 1324b71d1d42SEric Dumazet const struct in6_addr *group = NULL; 13259ed973ccSLinus Lüssing bool is_general_query; 132608b202b6SYOSHIFUJI Hideaki int err = 0; 132708b202b6SYOSHIFUJI Hideaki 132808b202b6SYOSHIFUJI Hideaki spin_lock(&br->multicast_lock); 132908b202b6SYOSHIFUJI Hideaki if (!netif_running(br->dev) || 133008b202b6SYOSHIFUJI Hideaki (port && port->state == BR_STATE_DISABLED)) 133108b202b6SYOSHIFUJI Hideaki goto out; 133208b202b6SYOSHIFUJI Hideaki 133308b202b6SYOSHIFUJI Hideaki if (skb->len == sizeof(*mld)) { 133408b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*mld))) { 133508b202b6SYOSHIFUJI Hideaki err = -EINVAL; 133608b202b6SYOSHIFUJI Hideaki goto out; 133708b202b6SYOSHIFUJI Hideaki } 133808b202b6SYOSHIFUJI Hideaki mld = (struct mld_msg *) icmp6_hdr(skb); 13394715213dSLi RongQing max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay)); 134008b202b6SYOSHIFUJI Hideaki if (max_delay) 134108b202b6SYOSHIFUJI Hideaki group = &mld->mld_mca; 1342248ba8ecSLinus Lüssing } else { 134308b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*mld2q))) { 134408b202b6SYOSHIFUJI Hideaki err = -EINVAL; 134508b202b6SYOSHIFUJI Hideaki goto out; 134608b202b6SYOSHIFUJI Hideaki } 134708b202b6SYOSHIFUJI Hideaki mld2q = (struct mld2_query *)icmp6_hdr(skb); 134808b202b6SYOSHIFUJI Hideaki if (!mld2q->mld2q_nsrcs) 134908b202b6SYOSHIFUJI Hideaki group = &mld2q->mld2q_mca; 1350e3f5b170SDaniel Borkmann 1351e3f5b170SDaniel Borkmann max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL); 135208b202b6SYOSHIFUJI Hideaki } 135308b202b6SYOSHIFUJI Hideaki 13549ed973ccSLinus Lüssing is_general_query = group && ipv6_addr_any(group); 13559ed973ccSLinus Lüssing 1356dc4eb53aSLinus Lüssing if (is_general_query) { 1357dc4eb53aSLinus Lüssing saddr.proto = htons(ETH_P_IPV6); 1358dc4eb53aSLinus Lüssing saddr.u.ip6 = ip6h->saddr; 1359b00589afSLinus Lüssing 1360dc4eb53aSLinus Lüssing br_multicast_query_received(br, port, &br->ip6_other_query, 1361dc4eb53aSLinus Lüssing &saddr, max_delay); 136208b202b6SYOSHIFUJI Hideaki goto out; 13636c03ee8bSLinus Lüssing } else if (!group) { 13646c03ee8bSLinus Lüssing goto out; 1365dc4eb53aSLinus Lüssing } 136608b202b6SYOSHIFUJI Hideaki 1367b0e9a30dSVlad Yasevich mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid); 136808b202b6SYOSHIFUJI Hideaki if (!mp) 136908b202b6SYOSHIFUJI Hideaki goto out; 137008b202b6SYOSHIFUJI Hideaki 137108b202b6SYOSHIFUJI Hideaki max_delay *= br->multicast_last_member_count; 13728a870178SHerbert Xu if (mp->mglist && 137308b202b6SYOSHIFUJI Hideaki (timer_pending(&mp->timer) ? 137408b202b6SYOSHIFUJI Hideaki time_after(mp->timer.expires, now + max_delay) : 137508b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&mp->timer) >= 0)) 137608b202b6SYOSHIFUJI Hideaki mod_timer(&mp->timer, now + max_delay); 137708b202b6SYOSHIFUJI Hideaki 1378e8051688SEric Dumazet for (pp = &mp->ports; 1379e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 1380e8051688SEric Dumazet pp = &p->next) { 138108b202b6SYOSHIFUJI Hideaki if (timer_pending(&p->timer) ? 138208b202b6SYOSHIFUJI Hideaki time_after(p->timer.expires, now + max_delay) : 138308b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&p->timer) >= 0) 138424f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 138508b202b6SYOSHIFUJI Hideaki } 138608b202b6SYOSHIFUJI Hideaki 138708b202b6SYOSHIFUJI Hideaki out: 138808b202b6SYOSHIFUJI Hideaki spin_unlock(&br->multicast_lock); 138908b202b6SYOSHIFUJI Hideaki return err; 139008b202b6SYOSHIFUJI Hideaki } 139108b202b6SYOSHIFUJI Hideaki #endif 139208b202b6SYOSHIFUJI Hideaki 139390010b36SLinus Lüssing static void 139490010b36SLinus Lüssing br_multicast_leave_group(struct net_bridge *br, 1395eb1d1641SHerbert Xu struct net_bridge_port *port, 1396cc0fdd80SLinus Lüssing struct br_ip *group, 139790010b36SLinus Lüssing struct bridge_mcast_other_query *other_query, 139890010b36SLinus Lüssing struct bridge_mcast_own_query *own_query) 1399eb1d1641SHerbert Xu { 1400eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 1401eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1402eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1403eb1d1641SHerbert Xu unsigned long now; 1404eb1d1641SHerbert Xu unsigned long time; 1405eb1d1641SHerbert Xu 1406eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1407eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1408eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED) || 140990010b36SLinus Lüssing timer_pending(&other_query->timer)) 1410eb1d1641SHerbert Xu goto out; 1411eb1d1641SHerbert Xu 1412e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1413eb1d1641SHerbert Xu mp = br_mdb_ip_get(mdb, group); 1414eb1d1641SHerbert Xu if (!mp) 1415eb1d1641SHerbert Xu goto out; 1416eb1d1641SHerbert Xu 1417cc0fdd80SLinus Lüssing if (br->multicast_querier) { 14186b7df111SCong Wang __br_multicast_send_query(br, port, &mp->addr); 14196b7df111SCong Wang 14206b7df111SCong Wang time = jiffies + br->multicast_last_member_count * 14216b7df111SCong Wang br->multicast_last_member_interval; 1422cc0fdd80SLinus Lüssing 142390010b36SLinus Lüssing mod_timer(&own_query->timer, time); 14246b7df111SCong Wang 14256b7df111SCong Wang for (p = mlock_dereference(mp->ports, br); 14266b7df111SCong Wang p != NULL; 14276b7df111SCong Wang p = mlock_dereference(p->next, br)) { 14286b7df111SCong Wang if (p->port != port) 14296b7df111SCong Wang continue; 14306b7df111SCong Wang 14316b7df111SCong Wang if (!hlist_unhashed(&p->mglist) && 14326b7df111SCong Wang (timer_pending(&p->timer) ? 14336b7df111SCong Wang time_after(p->timer.expires, time) : 14346b7df111SCong Wang try_to_del_timer_sync(&p->timer) >= 0)) { 14356b7df111SCong Wang mod_timer(&p->timer, time); 14366b7df111SCong Wang } 14376b7df111SCong Wang 14386b7df111SCong Wang break; 14396b7df111SCong Wang } 14406b7df111SCong Wang } 14416b7df111SCong Wang 1442c2d3babfSDavid S. Miller if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) { 144350426b59SAmerigo Wang struct net_bridge_port_group __rcu **pp; 144450426b59SAmerigo Wang 144550426b59SAmerigo Wang for (pp = &mp->ports; 144650426b59SAmerigo Wang (p = mlock_dereference(*pp, br)) != NULL; 144750426b59SAmerigo Wang pp = &p->next) { 144850426b59SAmerigo Wang if (p->port != port) 144950426b59SAmerigo Wang continue; 145050426b59SAmerigo Wang 145150426b59SAmerigo Wang rcu_assign_pointer(*pp, p->next); 145250426b59SAmerigo Wang hlist_del_init(&p->mglist); 145350426b59SAmerigo Wang del_timer(&p->timer); 145409cf0211SNikolay Aleksandrov br_mdb_notify(br->dev, port, group, RTM_DELMDB, 145509cf0211SNikolay Aleksandrov p->state); 145650426b59SAmerigo Wang call_rcu_bh(&p->rcu, br_multicast_free_pg); 145750426b59SAmerigo Wang 1458454594f3SLinus Lüssing if (!mp->ports && !mp->mglist && 145950426b59SAmerigo Wang netif_running(br->dev)) 146050426b59SAmerigo Wang mod_timer(&mp->timer, jiffies); 146150426b59SAmerigo Wang } 146250426b59SAmerigo Wang goto out; 146350426b59SAmerigo Wang } 146450426b59SAmerigo Wang 1465eb1d1641SHerbert Xu now = jiffies; 1466eb1d1641SHerbert Xu time = now + br->multicast_last_member_count * 1467eb1d1641SHerbert Xu br->multicast_last_member_interval; 1468eb1d1641SHerbert Xu 1469eb1d1641SHerbert Xu if (!port) { 1470454594f3SLinus Lüssing if (mp->mglist && 1471eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1472eb1d1641SHerbert Xu time_after(mp->timer.expires, time) : 1473eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) { 1474eb1d1641SHerbert Xu mod_timer(&mp->timer, time); 1475eb1d1641SHerbert Xu } 1476454594f3SLinus Lüssing 1477454594f3SLinus Lüssing goto out; 1478454594f3SLinus Lüssing } 1479454594f3SLinus Lüssing 1480454594f3SLinus Lüssing for (p = mlock_dereference(mp->ports, br); 1481454594f3SLinus Lüssing p != NULL; 1482454594f3SLinus Lüssing p = mlock_dereference(p->next, br)) { 1483454594f3SLinus Lüssing if (p->port != port) 1484454594f3SLinus Lüssing continue; 1485454594f3SLinus Lüssing 1486454594f3SLinus Lüssing if (!hlist_unhashed(&p->mglist) && 1487454594f3SLinus Lüssing (timer_pending(&p->timer) ? 1488454594f3SLinus Lüssing time_after(p->timer.expires, time) : 1489454594f3SLinus Lüssing try_to_del_timer_sync(&p->timer) >= 0)) { 1490454594f3SLinus Lüssing mod_timer(&p->timer, time); 1491454594f3SLinus Lüssing } 1492454594f3SLinus Lüssing 1493454594f3SLinus Lüssing break; 1494eb1d1641SHerbert Xu } 1495eb1d1641SHerbert Xu out: 1496eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1497eb1d1641SHerbert Xu } 1498eb1d1641SHerbert Xu 14998ef2a9a5SYOSHIFUJI Hideaki static void br_ip4_multicast_leave_group(struct net_bridge *br, 15008ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 1501b0e9a30dSVlad Yasevich __be32 group, 1502b0e9a30dSVlad Yasevich __u16 vid) 15038ef2a9a5SYOSHIFUJI Hideaki { 15048ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 150590010b36SLinus Lüssing struct bridge_mcast_own_query *own_query; 15068ef2a9a5SYOSHIFUJI Hideaki 15078ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 15088ef2a9a5SYOSHIFUJI Hideaki return; 15098ef2a9a5SYOSHIFUJI Hideaki 151090010b36SLinus Lüssing own_query = port ? &port->ip4_own_query : &br->ip4_own_query; 151190010b36SLinus Lüssing 15128ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 15138ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 1514b0e9a30dSVlad Yasevich br_group.vid = vid; 15158ef2a9a5SYOSHIFUJI Hideaki 151690010b36SLinus Lüssing br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query, 151790010b36SLinus Lüssing own_query); 15188ef2a9a5SYOSHIFUJI Hideaki } 15198ef2a9a5SYOSHIFUJI Hideaki 1520dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 152108b202b6SYOSHIFUJI Hideaki static void br_ip6_multicast_leave_group(struct net_bridge *br, 152208b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 1523b0e9a30dSVlad Yasevich const struct in6_addr *group, 1524b0e9a30dSVlad Yasevich __u16 vid) 152508b202b6SYOSHIFUJI Hideaki { 152608b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 152790010b36SLinus Lüssing struct bridge_mcast_own_query *own_query; 152808b202b6SYOSHIFUJI Hideaki 15293c3769e6SLinus Lüssing if (ipv6_addr_is_ll_all_nodes(group)) 153008b202b6SYOSHIFUJI Hideaki return; 153108b202b6SYOSHIFUJI Hideaki 153290010b36SLinus Lüssing own_query = port ? &port->ip6_own_query : &br->ip6_own_query; 153390010b36SLinus Lüssing 15344e3fd7a0SAlexey Dobriyan br_group.u.ip6 = *group; 153508b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 1536b0e9a30dSVlad Yasevich br_group.vid = vid; 153708b202b6SYOSHIFUJI Hideaki 153890010b36SLinus Lüssing br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query, 153990010b36SLinus Lüssing own_query); 154008b202b6SYOSHIFUJI Hideaki } 154108b202b6SYOSHIFUJI Hideaki #endif 15428ef2a9a5SYOSHIFUJI Hideaki 1543eb1d1641SHerbert Xu static int br_multicast_ipv4_rcv(struct net_bridge *br, 1544eb1d1641SHerbert Xu struct net_bridge_port *port, 154506499098SVlad Yasevich struct sk_buff *skb, 154606499098SVlad Yasevich u16 vid) 1547eb1d1641SHerbert Xu { 15489afd85c9SLinus Lüssing struct sk_buff *skb_trimmed = NULL; 1549eb1d1641SHerbert Xu struct igmphdr *ih; 1550eb1d1641SHerbert Xu int err; 1551eb1d1641SHerbert Xu 15529afd85c9SLinus Lüssing err = ip_mc_check_igmp(skb, &skb_trimmed); 1553eb1d1641SHerbert Xu 15549afd85c9SLinus Lüssing if (err == -ENOMSG) { 15559afd85c9SLinus Lüssing if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) 1556bd4265feSHerbert Xu BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 1557eb1d1641SHerbert Xu return 0; 15589afd85c9SLinus Lüssing } else if (err < 0) { 15599afd85c9SLinus Lüssing return err; 1560bd4265feSHerbert Xu } 1561eb1d1641SHerbert Xu 1562eb1d1641SHerbert Xu BR_INPUT_SKB_CB(skb)->igmp = 1; 15639afd85c9SLinus Lüssing ih = igmp_hdr(skb); 1564eb1d1641SHerbert Xu 1565eb1d1641SHerbert Xu switch (ih->type) { 1566eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_REPORT: 1567eb1d1641SHerbert Xu case IGMPV2_HOST_MEMBERSHIP_REPORT: 156862b2bcb4SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 1569b0e9a30dSVlad Yasevich err = br_ip4_multicast_add_group(br, port, ih->group, vid); 1570eb1d1641SHerbert Xu break; 1571eb1d1641SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 15729afd85c9SLinus Lüssing err = br_ip4_multicast_igmp3_report(br, port, skb_trimmed, vid); 1573eb1d1641SHerbert Xu break; 1574eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_QUERY: 15759afd85c9SLinus Lüssing err = br_ip4_multicast_query(br, port, skb_trimmed, vid); 1576eb1d1641SHerbert Xu break; 1577eb1d1641SHerbert Xu case IGMP_HOST_LEAVE_MESSAGE: 1578b0e9a30dSVlad Yasevich br_ip4_multicast_leave_group(br, port, ih->group, vid); 1579eb1d1641SHerbert Xu break; 1580eb1d1641SHerbert Xu } 1581eb1d1641SHerbert Xu 15829afd85c9SLinus Lüssing if (skb_trimmed) 15839afd85c9SLinus Lüssing kfree_skb(skb_trimmed); 15849afd85c9SLinus Lüssing 1585eb1d1641SHerbert Xu return err; 1586eb1d1641SHerbert Xu } 1587eb1d1641SHerbert Xu 1588dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 158908b202b6SYOSHIFUJI Hideaki static int br_multicast_ipv6_rcv(struct net_bridge *br, 159008b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 159106499098SVlad Yasevich struct sk_buff *skb, 159206499098SVlad Yasevich u16 vid) 159308b202b6SYOSHIFUJI Hideaki { 15949afd85c9SLinus Lüssing struct sk_buff *skb_trimmed = NULL; 15959afd85c9SLinus Lüssing struct mld_msg *mld; 159608b202b6SYOSHIFUJI Hideaki int err; 159708b202b6SYOSHIFUJI Hideaki 15989afd85c9SLinus Lüssing err = ipv6_mc_check_mld(skb, &skb_trimmed); 159908b202b6SYOSHIFUJI Hideaki 16009afd85c9SLinus Lüssing if (err == -ENOMSG) { 16019afd85c9SLinus Lüssing if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr)) 16028fad9c39SLinus Lüssing BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 160308b202b6SYOSHIFUJI Hideaki return 0; 16049afd85c9SLinus Lüssing } else if (err < 0) { 16059afd85c9SLinus Lüssing return err; 160608b202b6SYOSHIFUJI Hideaki } 160708b202b6SYOSHIFUJI Hideaki 160808b202b6SYOSHIFUJI Hideaki BR_INPUT_SKB_CB(skb)->igmp = 1; 16099afd85c9SLinus Lüssing mld = (struct mld_msg *)skb_transport_header(skb); 161008b202b6SYOSHIFUJI Hideaki 16119afd85c9SLinus Lüssing switch (mld->mld_type) { 161208b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 1613fc2af6c7SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 1614b0e9a30dSVlad Yasevich err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid); 161508b202b6SYOSHIFUJI Hideaki break; 161608b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 16179afd85c9SLinus Lüssing err = br_ip6_multicast_mld2_report(br, port, skb_trimmed, vid); 161808b202b6SYOSHIFUJI Hideaki break; 161908b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 16209afd85c9SLinus Lüssing err = br_ip6_multicast_query(br, port, skb_trimmed, vid); 162108b202b6SYOSHIFUJI Hideaki break; 162208b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 1623b0e9a30dSVlad Yasevich br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid); 16249afd85c9SLinus Lüssing break; 162508b202b6SYOSHIFUJI Hideaki } 162608b202b6SYOSHIFUJI Hideaki 16279afd85c9SLinus Lüssing if (skb_trimmed) 16289afd85c9SLinus Lüssing kfree_skb(skb_trimmed); 16299afd85c9SLinus Lüssing 163008b202b6SYOSHIFUJI Hideaki return err; 163108b202b6SYOSHIFUJI Hideaki } 163208b202b6SYOSHIFUJI Hideaki #endif 163308b202b6SYOSHIFUJI Hideaki 1634eb1d1641SHerbert Xu int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port, 163506499098SVlad Yasevich struct sk_buff *skb, u16 vid) 1636eb1d1641SHerbert Xu { 16371fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->igmp = 0; 16381fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->mrouters_only = 0; 16391fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 1640eb1d1641SHerbert Xu if (br->multicast_disabled) 1641eb1d1641SHerbert Xu return 0; 1642eb1d1641SHerbert Xu 1643eb1d1641SHerbert Xu switch (skb->protocol) { 1644eb1d1641SHerbert Xu case htons(ETH_P_IP): 164506499098SVlad Yasevich return br_multicast_ipv4_rcv(br, port, skb, vid); 1646dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 164708b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 164806499098SVlad Yasevich return br_multicast_ipv6_rcv(br, port, skb, vid); 164908b202b6SYOSHIFUJI Hideaki #endif 1650eb1d1641SHerbert Xu } 1651eb1d1641SHerbert Xu 1652eb1d1641SHerbert Xu return 0; 1653eb1d1641SHerbert Xu } 1654eb1d1641SHerbert Xu 1655cc0fdd80SLinus Lüssing static void br_multicast_query_expired(struct net_bridge *br, 16562cd41431SLinus Lüssing struct bridge_mcast_own_query *query, 16572cd41431SLinus Lüssing struct bridge_mcast_querier *querier) 1658cc0fdd80SLinus Lüssing { 1659cc0fdd80SLinus Lüssing spin_lock(&br->multicast_lock); 1660cc0fdd80SLinus Lüssing if (query->startup_sent < br->multicast_startup_query_count) 1661cc0fdd80SLinus Lüssing query->startup_sent++; 1662cc0fdd80SLinus Lüssing 166371d9f614SEric Dumazet RCU_INIT_POINTER(querier->port, NULL); 1664cc0fdd80SLinus Lüssing br_multicast_send_query(br, NULL, query); 1665cc0fdd80SLinus Lüssing spin_unlock(&br->multicast_lock); 1666cc0fdd80SLinus Lüssing } 1667cc0fdd80SLinus Lüssing 1668cc0fdd80SLinus Lüssing static void br_ip4_multicast_query_expired(unsigned long data) 1669eb1d1641SHerbert Xu { 1670eb1d1641SHerbert Xu struct net_bridge *br = (void *)data; 1671eb1d1641SHerbert Xu 16722cd41431SLinus Lüssing br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier); 1673eb1d1641SHerbert Xu } 1674eb1d1641SHerbert Xu 1675cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1676cc0fdd80SLinus Lüssing static void br_ip6_multicast_query_expired(unsigned long data) 1677cc0fdd80SLinus Lüssing { 1678cc0fdd80SLinus Lüssing struct net_bridge *br = (void *)data; 1679cc0fdd80SLinus Lüssing 16802cd41431SLinus Lüssing br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier); 1681cc0fdd80SLinus Lüssing } 1682cc0fdd80SLinus Lüssing #endif 1683cc0fdd80SLinus Lüssing 1684eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br) 1685eb1d1641SHerbert Xu { 1686eb1d1641SHerbert Xu br->hash_elasticity = 4; 1687eb1d1641SHerbert Xu br->hash_max = 512; 1688eb1d1641SHerbert Xu 1689eb1d1641SHerbert Xu br->multicast_router = 1; 1690c5c23260SHerbert Xu br->multicast_querier = 0; 16911c8ad5bfSCong Wang br->multicast_query_use_ifaddr = 0; 1692eb1d1641SHerbert Xu br->multicast_last_member_count = 2; 1693eb1d1641SHerbert Xu br->multicast_startup_query_count = 2; 1694eb1d1641SHerbert Xu 1695eb1d1641SHerbert Xu br->multicast_last_member_interval = HZ; 1696eb1d1641SHerbert Xu br->multicast_query_response_interval = 10 * HZ; 1697eb1d1641SHerbert Xu br->multicast_startup_query_interval = 125 * HZ / 4; 1698eb1d1641SHerbert Xu br->multicast_query_interval = 125 * HZ; 1699eb1d1641SHerbert Xu br->multicast_querier_interval = 255 * HZ; 1700eb1d1641SHerbert Xu br->multicast_membership_interval = 260 * HZ; 1701eb1d1641SHerbert Xu 170290010b36SLinus Lüssing br->ip4_other_query.delay_time = 0; 17032cd41431SLinus Lüssing br->ip4_querier.port = NULL; 1704cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 170590010b36SLinus Lüssing br->ip6_other_query.delay_time = 0; 17062cd41431SLinus Lüssing br->ip6_querier.port = NULL; 1707cc0fdd80SLinus Lüssing #endif 1708b00589afSLinus Lüssing 1709eb1d1641SHerbert Xu spin_lock_init(&br->multicast_lock); 1710eb1d1641SHerbert Xu setup_timer(&br->multicast_router_timer, 1711eb1d1641SHerbert Xu br_multicast_local_router_expired, 0); 171290010b36SLinus Lüssing setup_timer(&br->ip4_other_query.timer, 171390010b36SLinus Lüssing br_ip4_multicast_querier_expired, (unsigned long)br); 171490010b36SLinus Lüssing setup_timer(&br->ip4_own_query.timer, br_ip4_multicast_query_expired, 1715cc0fdd80SLinus Lüssing (unsigned long)br); 1716cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 171790010b36SLinus Lüssing setup_timer(&br->ip6_other_query.timer, 171890010b36SLinus Lüssing br_ip6_multicast_querier_expired, (unsigned long)br); 171990010b36SLinus Lüssing setup_timer(&br->ip6_own_query.timer, br_ip6_multicast_query_expired, 1720cc0fdd80SLinus Lüssing (unsigned long)br); 1721cc0fdd80SLinus Lüssing #endif 1722eb1d1641SHerbert Xu } 1723eb1d1641SHerbert Xu 1724cc0fdd80SLinus Lüssing static void __br_multicast_open(struct net_bridge *br, 172590010b36SLinus Lüssing struct bridge_mcast_own_query *query) 1726eb1d1641SHerbert Xu { 1727cc0fdd80SLinus Lüssing query->startup_sent = 0; 1728eb1d1641SHerbert Xu 1729eb1d1641SHerbert Xu if (br->multicast_disabled) 1730eb1d1641SHerbert Xu return; 1731eb1d1641SHerbert Xu 1732cc0fdd80SLinus Lüssing mod_timer(&query->timer, jiffies); 1733cc0fdd80SLinus Lüssing } 1734cc0fdd80SLinus Lüssing 1735cc0fdd80SLinus Lüssing void br_multicast_open(struct net_bridge *br) 1736cc0fdd80SLinus Lüssing { 173790010b36SLinus Lüssing __br_multicast_open(br, &br->ip4_own_query); 1738cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 173990010b36SLinus Lüssing __br_multicast_open(br, &br->ip6_own_query); 1740cc0fdd80SLinus Lüssing #endif 1741eb1d1641SHerbert Xu } 1742eb1d1641SHerbert Xu 1743eb1d1641SHerbert Xu void br_multicast_stop(struct net_bridge *br) 1744eb1d1641SHerbert Xu { 1745eb1d1641SHerbert Xu del_timer_sync(&br->multicast_router_timer); 174690010b36SLinus Lüssing del_timer_sync(&br->ip4_other_query.timer); 174790010b36SLinus Lüssing del_timer_sync(&br->ip4_own_query.timer); 1748cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 174990010b36SLinus Lüssing del_timer_sync(&br->ip6_other_query.timer); 175090010b36SLinus Lüssing del_timer_sync(&br->ip6_own_query.timer); 1751cc0fdd80SLinus Lüssing #endif 1752*e10177abSSatish Ashok } 1753*e10177abSSatish Ashok 1754*e10177abSSatish Ashok void br_multicast_dev_del(struct net_bridge *br) 1755*e10177abSSatish Ashok { 1756*e10177abSSatish Ashok struct net_bridge_mdb_htable *mdb; 1757*e10177abSSatish Ashok struct net_bridge_mdb_entry *mp; 1758*e10177abSSatish Ashok struct hlist_node *n; 1759*e10177abSSatish Ashok u32 ver; 1760*e10177abSSatish Ashok int i; 1761eb1d1641SHerbert Xu 1762eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1763e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1764eb1d1641SHerbert Xu if (!mdb) 1765eb1d1641SHerbert Xu goto out; 1766eb1d1641SHerbert Xu 1767eb1d1641SHerbert Xu br->mdb = NULL; 1768eb1d1641SHerbert Xu 1769eb1d1641SHerbert Xu ver = mdb->ver; 1770eb1d1641SHerbert Xu for (i = 0; i < mdb->max; i++) { 1771b67bfe0dSSasha Levin hlist_for_each_entry_safe(mp, n, &mdb->mhash[i], 1772eb1d1641SHerbert Xu hlist[ver]) { 1773eb1d1641SHerbert Xu del_timer(&mp->timer); 1774eb1d1641SHerbert Xu call_rcu_bh(&mp->rcu, br_multicast_free_group); 1775eb1d1641SHerbert Xu } 1776eb1d1641SHerbert Xu } 1777eb1d1641SHerbert Xu 1778eb1d1641SHerbert Xu if (mdb->old) { 1779eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 178010cc2b50SHerbert Xu rcu_barrier_bh(); 1781eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1782eb1d1641SHerbert Xu WARN_ON(mdb->old); 1783eb1d1641SHerbert Xu } 1784eb1d1641SHerbert Xu 1785eb1d1641SHerbert Xu mdb->old = mdb; 1786eb1d1641SHerbert Xu call_rcu_bh(&mdb->rcu, br_mdb_free); 1787eb1d1641SHerbert Xu 1788eb1d1641SHerbert Xu out: 1789eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 1790eb1d1641SHerbert Xu } 17910909e117SHerbert Xu 17920909e117SHerbert Xu int br_multicast_set_router(struct net_bridge *br, unsigned long val) 17930909e117SHerbert Xu { 17946ae4ae8eSLinus Lüssing int err = -EINVAL; 17950909e117SHerbert Xu 17960909e117SHerbert Xu spin_lock_bh(&br->multicast_lock); 17970909e117SHerbert Xu 17980909e117SHerbert Xu switch (val) { 17990909e117SHerbert Xu case 0: 18000909e117SHerbert Xu case 2: 18010909e117SHerbert Xu del_timer(&br->multicast_router_timer); 18020909e117SHerbert Xu /* fall through */ 18030909e117SHerbert Xu case 1: 18040909e117SHerbert Xu br->multicast_router = val; 18050909e117SHerbert Xu err = 0; 18060909e117SHerbert Xu break; 18070909e117SHerbert Xu } 18080909e117SHerbert Xu 18090909e117SHerbert Xu spin_unlock_bh(&br->multicast_lock); 18100909e117SHerbert Xu 18110909e117SHerbert Xu return err; 18120909e117SHerbert Xu } 18130909e117SHerbert Xu 18140909e117SHerbert Xu int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val) 18150909e117SHerbert Xu { 18160909e117SHerbert Xu struct net_bridge *br = p->br; 18176ae4ae8eSLinus Lüssing int err = -EINVAL; 18180909e117SHerbert Xu 18190909e117SHerbert Xu spin_lock(&br->multicast_lock); 18200909e117SHerbert Xu 18210909e117SHerbert Xu switch (val) { 18220909e117SHerbert Xu case 0: 18230909e117SHerbert Xu case 1: 18240909e117SHerbert Xu case 2: 18250909e117SHerbert Xu p->multicast_router = val; 18260909e117SHerbert Xu err = 0; 18270909e117SHerbert Xu 18280909e117SHerbert Xu if (val < 2 && !hlist_unhashed(&p->rlist)) 18290909e117SHerbert Xu hlist_del_init_rcu(&p->rlist); 18300909e117SHerbert Xu 18310909e117SHerbert Xu if (val == 1) 18320909e117SHerbert Xu break; 18330909e117SHerbert Xu 18340909e117SHerbert Xu del_timer(&p->multicast_router_timer); 18350909e117SHerbert Xu 18360909e117SHerbert Xu if (val == 0) 18370909e117SHerbert Xu break; 18380909e117SHerbert Xu 18390909e117SHerbert Xu br_multicast_add_router(br, p); 18400909e117SHerbert Xu break; 18410909e117SHerbert Xu } 18420909e117SHerbert Xu 18430909e117SHerbert Xu spin_unlock(&br->multicast_lock); 18440909e117SHerbert Xu 18450909e117SHerbert Xu return err; 18460909e117SHerbert Xu } 1847561f1103SHerbert Xu 1848cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br, 184990010b36SLinus Lüssing struct bridge_mcast_own_query *query) 1850561f1103SHerbert Xu { 1851561f1103SHerbert Xu struct net_bridge_port *port; 185274857216SHerbert Xu 1853cc0fdd80SLinus Lüssing __br_multicast_open(br, query); 185474857216SHerbert Xu 185574857216SHerbert Xu list_for_each_entry(port, &br->port_list, list) { 185674857216SHerbert Xu if (port->state == BR_STATE_DISABLED || 185774857216SHerbert Xu port->state == BR_STATE_BLOCKING) 185874857216SHerbert Xu continue; 185974857216SHerbert Xu 186090010b36SLinus Lüssing if (query == &br->ip4_own_query) 186190010b36SLinus Lüssing br_multicast_enable(&port->ip4_own_query); 1862cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1863cc0fdd80SLinus Lüssing else 186490010b36SLinus Lüssing br_multicast_enable(&port->ip6_own_query); 1865cc0fdd80SLinus Lüssing #endif 186674857216SHerbert Xu } 186774857216SHerbert Xu } 186874857216SHerbert Xu 186974857216SHerbert Xu int br_multicast_toggle(struct net_bridge *br, unsigned long val) 187074857216SHerbert Xu { 18713a7fda06SHerbert Xu int err = 0; 1872e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 1873561f1103SHerbert Xu 1874ef5e0d82SAndrey Vagin spin_lock_bh(&br->multicast_lock); 1875561f1103SHerbert Xu if (br->multicast_disabled == !val) 1876561f1103SHerbert Xu goto unlock; 1877561f1103SHerbert Xu 1878561f1103SHerbert Xu br->multicast_disabled = !val; 1879561f1103SHerbert Xu if (br->multicast_disabled) 1880561f1103SHerbert Xu goto unlock; 1881561f1103SHerbert Xu 18823a7fda06SHerbert Xu if (!netif_running(br->dev)) 18833a7fda06SHerbert Xu goto unlock; 18843a7fda06SHerbert Xu 1885e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1886e8051688SEric Dumazet if (mdb) { 1887e8051688SEric Dumazet if (mdb->old) { 1888561f1103SHerbert Xu err = -EEXIST; 1889561f1103SHerbert Xu rollback: 1890561f1103SHerbert Xu br->multicast_disabled = !!val; 1891561f1103SHerbert Xu goto unlock; 1892561f1103SHerbert Xu } 1893561f1103SHerbert Xu 1894e8051688SEric Dumazet err = br_mdb_rehash(&br->mdb, mdb->max, 1895561f1103SHerbert Xu br->hash_elasticity); 1896561f1103SHerbert Xu if (err) 1897561f1103SHerbert Xu goto rollback; 1898561f1103SHerbert Xu } 1899561f1103SHerbert Xu 190090010b36SLinus Lüssing br_multicast_start_querier(br, &br->ip4_own_query); 1901cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 190290010b36SLinus Lüssing br_multicast_start_querier(br, &br->ip6_own_query); 1903cc0fdd80SLinus Lüssing #endif 1904561f1103SHerbert Xu 1905561f1103SHerbert Xu unlock: 1906ef5e0d82SAndrey Vagin spin_unlock_bh(&br->multicast_lock); 1907561f1103SHerbert Xu 1908561f1103SHerbert Xu return err; 1909561f1103SHerbert Xu } 1910b195167fSHerbert Xu 1911c5c23260SHerbert Xu int br_multicast_set_querier(struct net_bridge *br, unsigned long val) 1912c5c23260SHerbert Xu { 1913b00589afSLinus Lüssing unsigned long max_delay; 1914b00589afSLinus Lüssing 1915c5c23260SHerbert Xu val = !!val; 1916c5c23260SHerbert Xu 1917c5c23260SHerbert Xu spin_lock_bh(&br->multicast_lock); 1918c5c23260SHerbert Xu if (br->multicast_querier == val) 1919c5c23260SHerbert Xu goto unlock; 1920c5c23260SHerbert Xu 1921c5c23260SHerbert Xu br->multicast_querier = val; 1922b00589afSLinus Lüssing if (!val) 1923b00589afSLinus Lüssing goto unlock; 1924b00589afSLinus Lüssing 1925b00589afSLinus Lüssing max_delay = br->multicast_query_response_interval; 1926b00589afSLinus Lüssing 192790010b36SLinus Lüssing if (!timer_pending(&br->ip4_other_query.timer)) 192890010b36SLinus Lüssing br->ip4_other_query.delay_time = jiffies + max_delay; 1929cc0fdd80SLinus Lüssing 193090010b36SLinus Lüssing br_multicast_start_querier(br, &br->ip4_own_query); 1931cc0fdd80SLinus Lüssing 1932cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 193390010b36SLinus Lüssing if (!timer_pending(&br->ip6_other_query.timer)) 193490010b36SLinus Lüssing br->ip6_other_query.delay_time = jiffies + max_delay; 1935cc0fdd80SLinus Lüssing 193690010b36SLinus Lüssing br_multicast_start_querier(br, &br->ip6_own_query); 1937cc0fdd80SLinus Lüssing #endif 1938c5c23260SHerbert Xu 1939c5c23260SHerbert Xu unlock: 1940c5c23260SHerbert Xu spin_unlock_bh(&br->multicast_lock); 1941c5c23260SHerbert Xu 1942c5c23260SHerbert Xu return 0; 1943c5c23260SHerbert Xu } 1944c5c23260SHerbert Xu 1945b195167fSHerbert Xu int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val) 1946b195167fSHerbert Xu { 19476ae4ae8eSLinus Lüssing int err = -EINVAL; 1948b195167fSHerbert Xu u32 old; 1949e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 1950b195167fSHerbert Xu 1951fe0d692bSCurt Brune spin_lock_bh(&br->multicast_lock); 1952b195167fSHerbert Xu if (!is_power_of_2(val)) 1953b195167fSHerbert Xu goto unlock; 1954e8051688SEric Dumazet 1955e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1956e8051688SEric Dumazet if (mdb && val < mdb->size) 1957b195167fSHerbert Xu goto unlock; 1958b195167fSHerbert Xu 1959b195167fSHerbert Xu err = 0; 1960b195167fSHerbert Xu 1961b195167fSHerbert Xu old = br->hash_max; 1962b195167fSHerbert Xu br->hash_max = val; 1963b195167fSHerbert Xu 1964e8051688SEric Dumazet if (mdb) { 1965e8051688SEric Dumazet if (mdb->old) { 1966b195167fSHerbert Xu err = -EEXIST; 1967b195167fSHerbert Xu rollback: 1968b195167fSHerbert Xu br->hash_max = old; 1969b195167fSHerbert Xu goto unlock; 1970b195167fSHerbert Xu } 1971b195167fSHerbert Xu 1972b195167fSHerbert Xu err = br_mdb_rehash(&br->mdb, br->hash_max, 1973b195167fSHerbert Xu br->hash_elasticity); 1974b195167fSHerbert Xu if (err) 1975b195167fSHerbert Xu goto rollback; 1976b195167fSHerbert Xu } 1977b195167fSHerbert Xu 1978b195167fSHerbert Xu unlock: 1979fe0d692bSCurt Brune spin_unlock_bh(&br->multicast_lock); 1980b195167fSHerbert Xu 1981b195167fSHerbert Xu return err; 1982b195167fSHerbert Xu } 198307f8ac4aSLinus Lüssing 198407f8ac4aSLinus Lüssing /** 198507f8ac4aSLinus Lüssing * br_multicast_list_adjacent - Returns snooped multicast addresses 198607f8ac4aSLinus Lüssing * @dev: The bridge port adjacent to which to retrieve addresses 198707f8ac4aSLinus Lüssing * @br_ip_list: The list to store found, snooped multicast IP addresses in 198807f8ac4aSLinus Lüssing * 198907f8ac4aSLinus Lüssing * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast 199007f8ac4aSLinus Lüssing * snooping feature on all bridge ports of dev's bridge device, excluding 199107f8ac4aSLinus Lüssing * the addresses from dev itself. 199207f8ac4aSLinus Lüssing * 199307f8ac4aSLinus Lüssing * Returns the number of items added to br_ip_list. 199407f8ac4aSLinus Lüssing * 199507f8ac4aSLinus Lüssing * Notes: 199607f8ac4aSLinus Lüssing * - br_ip_list needs to be initialized by caller 199707f8ac4aSLinus Lüssing * - br_ip_list might contain duplicates in the end 199807f8ac4aSLinus Lüssing * (needs to be taken care of by caller) 199907f8ac4aSLinus Lüssing * - br_ip_list needs to be freed by caller 200007f8ac4aSLinus Lüssing */ 200107f8ac4aSLinus Lüssing int br_multicast_list_adjacent(struct net_device *dev, 200207f8ac4aSLinus Lüssing struct list_head *br_ip_list) 200307f8ac4aSLinus Lüssing { 200407f8ac4aSLinus Lüssing struct net_bridge *br; 200507f8ac4aSLinus Lüssing struct net_bridge_port *port; 200607f8ac4aSLinus Lüssing struct net_bridge_port_group *group; 200707f8ac4aSLinus Lüssing struct br_ip_list *entry; 200807f8ac4aSLinus Lüssing int count = 0; 200907f8ac4aSLinus Lüssing 201007f8ac4aSLinus Lüssing rcu_read_lock(); 201107f8ac4aSLinus Lüssing if (!br_ip_list || !br_port_exists(dev)) 201207f8ac4aSLinus Lüssing goto unlock; 201307f8ac4aSLinus Lüssing 201407f8ac4aSLinus Lüssing port = br_port_get_rcu(dev); 201507f8ac4aSLinus Lüssing if (!port || !port->br) 201607f8ac4aSLinus Lüssing goto unlock; 201707f8ac4aSLinus Lüssing 201807f8ac4aSLinus Lüssing br = port->br; 201907f8ac4aSLinus Lüssing 202007f8ac4aSLinus Lüssing list_for_each_entry_rcu(port, &br->port_list, list) { 202107f8ac4aSLinus Lüssing if (!port->dev || port->dev == dev) 202207f8ac4aSLinus Lüssing continue; 202307f8ac4aSLinus Lüssing 202407f8ac4aSLinus Lüssing hlist_for_each_entry_rcu(group, &port->mglist, mglist) { 202507f8ac4aSLinus Lüssing entry = kmalloc(sizeof(*entry), GFP_ATOMIC); 202607f8ac4aSLinus Lüssing if (!entry) 202707f8ac4aSLinus Lüssing goto unlock; 202807f8ac4aSLinus Lüssing 202907f8ac4aSLinus Lüssing entry->addr = group->addr; 203007f8ac4aSLinus Lüssing list_add(&entry->list, br_ip_list); 203107f8ac4aSLinus Lüssing count++; 203207f8ac4aSLinus Lüssing } 203307f8ac4aSLinus Lüssing } 203407f8ac4aSLinus Lüssing 203507f8ac4aSLinus Lüssing unlock: 203607f8ac4aSLinus Lüssing rcu_read_unlock(); 203707f8ac4aSLinus Lüssing return count; 203807f8ac4aSLinus Lüssing } 203907f8ac4aSLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_list_adjacent); 20402cd41431SLinus Lüssing 20412cd41431SLinus Lüssing /** 2042c34963e2SLinus Lüssing * br_multicast_has_querier_anywhere - Checks for a querier on a bridge 2043c34963e2SLinus Lüssing * @dev: The bridge port providing the bridge on which to check for a querier 2044c34963e2SLinus Lüssing * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 2045c34963e2SLinus Lüssing * 2046c34963e2SLinus Lüssing * Checks whether the given interface has a bridge on top and if so returns 2047c34963e2SLinus Lüssing * true if a valid querier exists anywhere on the bridged link layer. 2048c34963e2SLinus Lüssing * Otherwise returns false. 2049c34963e2SLinus Lüssing */ 2050c34963e2SLinus Lüssing bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto) 2051c34963e2SLinus Lüssing { 2052c34963e2SLinus Lüssing struct net_bridge *br; 2053c34963e2SLinus Lüssing struct net_bridge_port *port; 2054c34963e2SLinus Lüssing struct ethhdr eth; 2055c34963e2SLinus Lüssing bool ret = false; 2056c34963e2SLinus Lüssing 2057c34963e2SLinus Lüssing rcu_read_lock(); 2058c34963e2SLinus Lüssing if (!br_port_exists(dev)) 2059c34963e2SLinus Lüssing goto unlock; 2060c34963e2SLinus Lüssing 2061c34963e2SLinus Lüssing port = br_port_get_rcu(dev); 2062c34963e2SLinus Lüssing if (!port || !port->br) 2063c34963e2SLinus Lüssing goto unlock; 2064c34963e2SLinus Lüssing 2065c34963e2SLinus Lüssing br = port->br; 2066c34963e2SLinus Lüssing 2067c34963e2SLinus Lüssing memset(ð, 0, sizeof(eth)); 2068c34963e2SLinus Lüssing eth.h_proto = htons(proto); 2069c34963e2SLinus Lüssing 2070c34963e2SLinus Lüssing ret = br_multicast_querier_exists(br, ð); 2071c34963e2SLinus Lüssing 2072c34963e2SLinus Lüssing unlock: 2073c34963e2SLinus Lüssing rcu_read_unlock(); 2074c34963e2SLinus Lüssing return ret; 2075c34963e2SLinus Lüssing } 2076c34963e2SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere); 2077c34963e2SLinus Lüssing 2078c34963e2SLinus Lüssing /** 20792cd41431SLinus Lüssing * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port 20802cd41431SLinus Lüssing * @dev: The bridge port adjacent to which to check for a querier 20812cd41431SLinus Lüssing * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 20822cd41431SLinus Lüssing * 20832cd41431SLinus Lüssing * Checks whether the given interface has a bridge on top and if so returns 20842cd41431SLinus Lüssing * true if a selected querier is behind one of the other ports of this 20852cd41431SLinus Lüssing * bridge. Otherwise returns false. 20862cd41431SLinus Lüssing */ 20872cd41431SLinus Lüssing bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto) 20882cd41431SLinus Lüssing { 20892cd41431SLinus Lüssing struct net_bridge *br; 20902cd41431SLinus Lüssing struct net_bridge_port *port; 20912cd41431SLinus Lüssing bool ret = false; 20922cd41431SLinus Lüssing 20932cd41431SLinus Lüssing rcu_read_lock(); 20942cd41431SLinus Lüssing if (!br_port_exists(dev)) 20952cd41431SLinus Lüssing goto unlock; 20962cd41431SLinus Lüssing 20972cd41431SLinus Lüssing port = br_port_get_rcu(dev); 20982cd41431SLinus Lüssing if (!port || !port->br) 20992cd41431SLinus Lüssing goto unlock; 21002cd41431SLinus Lüssing 21012cd41431SLinus Lüssing br = port->br; 21022cd41431SLinus Lüssing 21032cd41431SLinus Lüssing switch (proto) { 21042cd41431SLinus Lüssing case ETH_P_IP: 21052cd41431SLinus Lüssing if (!timer_pending(&br->ip4_other_query.timer) || 21062cd41431SLinus Lüssing rcu_dereference(br->ip4_querier.port) == port) 21072cd41431SLinus Lüssing goto unlock; 21082cd41431SLinus Lüssing break; 21093993c4e1SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 21102cd41431SLinus Lüssing case ETH_P_IPV6: 21112cd41431SLinus Lüssing if (!timer_pending(&br->ip6_other_query.timer) || 21122cd41431SLinus Lüssing rcu_dereference(br->ip6_querier.port) == port) 21132cd41431SLinus Lüssing goto unlock; 21142cd41431SLinus Lüssing break; 21153993c4e1SLinus Lüssing #endif 21162cd41431SLinus Lüssing default: 21172cd41431SLinus Lüssing goto unlock; 21182cd41431SLinus Lüssing } 21192cd41431SLinus Lüssing 21202cd41431SLinus Lüssing ret = true; 21212cd41431SLinus Lüssing unlock: 21222cd41431SLinus Lüssing rcu_read_unlock(); 21232cd41431SLinus Lüssing return ret; 21242cd41431SLinus Lüssing } 21252cd41431SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent); 2126