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> 2891b02d3dSNikolay Aleksandrov #include <linux/mroute.h> 29eb1d1641SHerbert Xu #include <net/ip.h> 30dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 3108b202b6SYOSHIFUJI Hideaki #include <net/ipv6.h> 3208b202b6SYOSHIFUJI Hideaki #include <net/mld.h> 33d4c4f07dSDavid S. Miller #include <net/ip6_checksum.h> 343c3769e6SLinus Lüssing #include <net/addrconf.h> 3508b202b6SYOSHIFUJI Hideaki #endif 36eb1d1641SHerbert Xu 37eb1d1641SHerbert Xu #include "br_private.h" 38eb1d1641SHerbert Xu 39cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br, 4090010b36SLinus Lüssing struct bridge_mcast_own_query *query); 41754bc547SSatish Ashok static void br_multicast_add_router(struct net_bridge *br, 42754bc547SSatish Ashok struct net_bridge_port *port); 43bc8c20acSSatish Ashok static void br_ip4_multicast_leave_group(struct net_bridge *br, 44bc8c20acSSatish Ashok struct net_bridge_port *port, 45bc8c20acSSatish Ashok __be32 group, 46bc8c20acSSatish Ashok __u16 vid); 47bc8c20acSSatish Ashok #if IS_ENABLED(CONFIG_IPV6) 48bc8c20acSSatish Ashok static void br_ip6_multicast_leave_group(struct net_bridge *br, 49bc8c20acSSatish Ashok struct net_bridge_port *port, 50bc8c20acSSatish Ashok const struct in6_addr *group, 51bc8c20acSSatish Ashok __u16 vid); 52bc8c20acSSatish Ashok #endif 532ce297fcSCong Wang unsigned int br_mdb_rehash_seq; 54c83b8fabSHerbert Xu 558ef2a9a5SYOSHIFUJI Hideaki static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b) 568ef2a9a5SYOSHIFUJI Hideaki { 578ef2a9a5SYOSHIFUJI Hideaki if (a->proto != b->proto) 588ef2a9a5SYOSHIFUJI Hideaki return 0; 59b0e9a30dSVlad Yasevich if (a->vid != b->vid) 60b0e9a30dSVlad Yasevich return 0; 618ef2a9a5SYOSHIFUJI Hideaki switch (a->proto) { 628ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 638ef2a9a5SYOSHIFUJI Hideaki return a->u.ip4 == b->u.ip4; 64dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 6508b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 6608b202b6SYOSHIFUJI Hideaki return ipv6_addr_equal(&a->u.ip6, &b->u.ip6); 6708b202b6SYOSHIFUJI Hideaki #endif 688ef2a9a5SYOSHIFUJI Hideaki } 698ef2a9a5SYOSHIFUJI Hideaki return 0; 708ef2a9a5SYOSHIFUJI Hideaki } 718ef2a9a5SYOSHIFUJI Hideaki 72b0e9a30dSVlad Yasevich static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip, 73b0e9a30dSVlad Yasevich __u16 vid) 74eb1d1641SHerbert Xu { 75b0e9a30dSVlad Yasevich return jhash_2words((__force u32)ip, vid, mdb->secret) & (mdb->max - 1); 76eb1d1641SHerbert Xu } 77eb1d1641SHerbert Xu 78dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 7908b202b6SYOSHIFUJI Hideaki static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb, 80b0e9a30dSVlad Yasevich const struct in6_addr *ip, 81b0e9a30dSVlad Yasevich __u16 vid) 8208b202b6SYOSHIFUJI Hideaki { 83b0e9a30dSVlad Yasevich return jhash_2words(ipv6_addr_hash(ip), vid, 84b0e9a30dSVlad Yasevich mdb->secret) & (mdb->max - 1); 8508b202b6SYOSHIFUJI Hideaki } 8608b202b6SYOSHIFUJI Hideaki #endif 8708b202b6SYOSHIFUJI Hideaki 888ef2a9a5SYOSHIFUJI Hideaki static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb, 898ef2a9a5SYOSHIFUJI Hideaki struct br_ip *ip) 908ef2a9a5SYOSHIFUJI Hideaki { 918ef2a9a5SYOSHIFUJI Hideaki switch (ip->proto) { 928ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 93b0e9a30dSVlad Yasevich return __br_ip4_hash(mdb, ip->u.ip4, ip->vid); 94dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 9508b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 96b0e9a30dSVlad Yasevich return __br_ip6_hash(mdb, &ip->u.ip6, ip->vid); 9708b202b6SYOSHIFUJI Hideaki #endif 988ef2a9a5SYOSHIFUJI Hideaki } 998ef2a9a5SYOSHIFUJI Hideaki return 0; 100eb1d1641SHerbert Xu } 101eb1d1641SHerbert Xu 102eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *__br_mdb_ip_get( 1038ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash) 104eb1d1641SHerbert Xu { 105eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 106eb1d1641SHerbert Xu 107b67bfe0dSSasha Levin hlist_for_each_entry_rcu(mp, &mdb->mhash[hash], hlist[mdb->ver]) { 1088ef2a9a5SYOSHIFUJI Hideaki if (br_ip_equal(&mp->addr, dst)) 109eb1d1641SHerbert Xu return mp; 110eb1d1641SHerbert Xu } 111eb1d1641SHerbert Xu 112eb1d1641SHerbert Xu return NULL; 113eb1d1641SHerbert Xu } 114eb1d1641SHerbert Xu 115cfd56754SCong Wang struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge_mdb_htable *mdb, 116cfd56754SCong Wang struct br_ip *dst) 1177f285fa7SHerbert Xu { 1187f285fa7SHerbert Xu if (!mdb) 1197f285fa7SHerbert Xu return NULL; 1207f285fa7SHerbert Xu 1217f285fa7SHerbert Xu return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst)); 1227f285fa7SHerbert Xu } 1237f285fa7SHerbert Xu 1248ef2a9a5SYOSHIFUJI Hideaki static struct net_bridge_mdb_entry *br_mdb_ip4_get( 125b0e9a30dSVlad Yasevich struct net_bridge_mdb_htable *mdb, __be32 dst, __u16 vid) 126eb1d1641SHerbert Xu { 1278ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_dst; 1280821ec55SHerbert Xu 1298ef2a9a5SYOSHIFUJI Hideaki br_dst.u.ip4 = dst; 1308ef2a9a5SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IP); 131b0e9a30dSVlad Yasevich br_dst.vid = vid; 1328ef2a9a5SYOSHIFUJI Hideaki 1337f285fa7SHerbert Xu return br_mdb_ip_get(mdb, &br_dst); 1348ef2a9a5SYOSHIFUJI Hideaki } 1358ef2a9a5SYOSHIFUJI Hideaki 136dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 13708b202b6SYOSHIFUJI Hideaki static struct net_bridge_mdb_entry *br_mdb_ip6_get( 138b0e9a30dSVlad Yasevich struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst, 139b0e9a30dSVlad Yasevich __u16 vid) 14008b202b6SYOSHIFUJI Hideaki { 14108b202b6SYOSHIFUJI Hideaki struct br_ip br_dst; 14208b202b6SYOSHIFUJI Hideaki 1434e3fd7a0SAlexey Dobriyan br_dst.u.ip6 = *dst; 14408b202b6SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IPV6); 145b0e9a30dSVlad Yasevich br_dst.vid = vid; 14608b202b6SYOSHIFUJI Hideaki 1477f285fa7SHerbert Xu return br_mdb_ip_get(mdb, &br_dst); 14808b202b6SYOSHIFUJI Hideaki } 14908b202b6SYOSHIFUJI Hideaki #endif 15008b202b6SYOSHIFUJI Hideaki 151eb1d1641SHerbert Xu struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br, 152fbca58a2SCong Wang struct sk_buff *skb, u16 vid) 153eb1d1641SHerbert Xu { 154e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb); 1558ef2a9a5SYOSHIFUJI Hideaki struct br_ip ip; 1568ef2a9a5SYOSHIFUJI Hideaki 1577f285fa7SHerbert Xu if (br->multicast_disabled) 158eb1d1641SHerbert Xu return NULL; 159eb1d1641SHerbert Xu 1608ef2a9a5SYOSHIFUJI Hideaki if (BR_INPUT_SKB_CB(skb)->igmp) 1618ef2a9a5SYOSHIFUJI Hideaki return NULL; 1628ef2a9a5SYOSHIFUJI Hideaki 1638ef2a9a5SYOSHIFUJI Hideaki ip.proto = skb->protocol; 164fbca58a2SCong Wang ip.vid = vid; 1658ef2a9a5SYOSHIFUJI Hideaki 166eb1d1641SHerbert Xu switch (skb->protocol) { 167eb1d1641SHerbert Xu case htons(ETH_P_IP): 1688ef2a9a5SYOSHIFUJI Hideaki ip.u.ip4 = ip_hdr(skb)->daddr; 169eb1d1641SHerbert Xu break; 170dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 17108b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 1724e3fd7a0SAlexey Dobriyan ip.u.ip6 = ipv6_hdr(skb)->daddr; 17308b202b6SYOSHIFUJI Hideaki break; 17408b202b6SYOSHIFUJI Hideaki #endif 1758ef2a9a5SYOSHIFUJI Hideaki default: 1768ef2a9a5SYOSHIFUJI Hideaki return NULL; 177eb1d1641SHerbert Xu } 178eb1d1641SHerbert Xu 1798ef2a9a5SYOSHIFUJI Hideaki return br_mdb_ip_get(mdb, &ip); 180eb1d1641SHerbert Xu } 181eb1d1641SHerbert Xu 182eb1d1641SHerbert Xu static void br_mdb_free(struct rcu_head *head) 183eb1d1641SHerbert Xu { 184eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb = 185eb1d1641SHerbert Xu container_of(head, struct net_bridge_mdb_htable, rcu); 186eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old = mdb->old; 187eb1d1641SHerbert Xu 188eb1d1641SHerbert Xu mdb->old = NULL; 189eb1d1641SHerbert Xu kfree(old->mhash); 190eb1d1641SHerbert Xu kfree(old); 191eb1d1641SHerbert Xu } 192eb1d1641SHerbert Xu 193eb1d1641SHerbert Xu static int br_mdb_copy(struct net_bridge_mdb_htable *new, 194eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old, 195eb1d1641SHerbert Xu int elasticity) 196eb1d1641SHerbert Xu { 197eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 198eb1d1641SHerbert Xu int maxlen; 199eb1d1641SHerbert Xu int len; 200eb1d1641SHerbert Xu int i; 201eb1d1641SHerbert Xu 202eb1d1641SHerbert Xu for (i = 0; i < old->max; i++) 203b67bfe0dSSasha Levin hlist_for_each_entry(mp, &old->mhash[i], hlist[old->ver]) 204eb1d1641SHerbert Xu hlist_add_head(&mp->hlist[new->ver], 2058ef2a9a5SYOSHIFUJI Hideaki &new->mhash[br_ip_hash(new, &mp->addr)]); 206eb1d1641SHerbert Xu 207eb1d1641SHerbert Xu if (!elasticity) 208eb1d1641SHerbert Xu return 0; 209eb1d1641SHerbert Xu 210eb1d1641SHerbert Xu maxlen = 0; 211eb1d1641SHerbert Xu for (i = 0; i < new->max; i++) { 212eb1d1641SHerbert Xu len = 0; 213b67bfe0dSSasha Levin hlist_for_each_entry(mp, &new->mhash[i], hlist[new->ver]) 214eb1d1641SHerbert Xu len++; 215eb1d1641SHerbert Xu if (len > maxlen) 216eb1d1641SHerbert Xu maxlen = len; 217eb1d1641SHerbert Xu } 218eb1d1641SHerbert Xu 219eb1d1641SHerbert Xu return maxlen > elasticity ? -EINVAL : 0; 220eb1d1641SHerbert Xu } 221eb1d1641SHerbert Xu 222cfd56754SCong Wang void br_multicast_free_pg(struct rcu_head *head) 223eb1d1641SHerbert Xu { 224eb1d1641SHerbert Xu struct net_bridge_port_group *p = 225eb1d1641SHerbert Xu container_of(head, struct net_bridge_port_group, rcu); 226eb1d1641SHerbert Xu 227eb1d1641SHerbert Xu kfree(p); 228eb1d1641SHerbert Xu } 229eb1d1641SHerbert Xu 230eb1d1641SHerbert Xu static void br_multicast_free_group(struct rcu_head *head) 231eb1d1641SHerbert Xu { 232eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = 233eb1d1641SHerbert Xu container_of(head, struct net_bridge_mdb_entry, rcu); 234eb1d1641SHerbert Xu 235eb1d1641SHerbert Xu kfree(mp); 236eb1d1641SHerbert Xu } 237eb1d1641SHerbert Xu 238eb1d1641SHerbert Xu static void br_multicast_group_expired(unsigned long data) 239eb1d1641SHerbert Xu { 240eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = (void *)data; 241eb1d1641SHerbert Xu struct net_bridge *br = mp->br; 242eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 243eb1d1641SHerbert Xu 244eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 245eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&mp->timer)) 246eb1d1641SHerbert Xu goto out; 247eb1d1641SHerbert Xu 2488a870178SHerbert Xu mp->mglist = false; 249eb1d1641SHerbert Xu 250eb1d1641SHerbert Xu if (mp->ports) 251eb1d1641SHerbert Xu goto out; 252eb1d1641SHerbert Xu 253e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 254e8051688SEric Dumazet 255eb1d1641SHerbert Xu hlist_del_rcu(&mp->hlist[mdb->ver]); 256eb1d1641SHerbert Xu mdb->size--; 257eb1d1641SHerbert Xu 258eb1d1641SHerbert Xu call_rcu_bh(&mp->rcu, br_multicast_free_group); 259eb1d1641SHerbert Xu 260eb1d1641SHerbert Xu out: 261eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 262eb1d1641SHerbert Xu } 263eb1d1641SHerbert Xu 264eb1d1641SHerbert Xu static void br_multicast_del_pg(struct net_bridge *br, 265eb1d1641SHerbert Xu struct net_bridge_port_group *pg) 266eb1d1641SHerbert Xu { 267e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 268eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 269eb1d1641SHerbert Xu struct net_bridge_port_group *p; 270e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 271e8051688SEric Dumazet 272e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 273eb1d1641SHerbert Xu 2748ef2a9a5SYOSHIFUJI Hideaki mp = br_mdb_ip_get(mdb, &pg->addr); 275eb1d1641SHerbert Xu if (WARN_ON(!mp)) 276eb1d1641SHerbert Xu return; 277eb1d1641SHerbert Xu 278e8051688SEric Dumazet for (pp = &mp->ports; 279e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 280e8051688SEric Dumazet pp = &p->next) { 281eb1d1641SHerbert Xu if (p != pg) 282eb1d1641SHerbert Xu continue; 283eb1d1641SHerbert Xu 28483f6a740Sstephen hemminger rcu_assign_pointer(*pp, p->next); 285eb1d1641SHerbert Xu hlist_del_init(&p->mglist); 286eb1d1641SHerbert Xu del_timer(&p->timer); 28745ebcce5SElad Raz br_mdb_notify(br->dev, p->port, &pg->addr, RTM_DELMDB, 28845ebcce5SElad Raz p->flags); 289eb1d1641SHerbert Xu call_rcu_bh(&p->rcu, br_multicast_free_pg); 290eb1d1641SHerbert Xu 291454594f3SLinus Lüssing if (!mp->ports && !mp->mglist && 292eb1d1641SHerbert Xu netif_running(br->dev)) 293eb1d1641SHerbert Xu mod_timer(&mp->timer, jiffies); 294eb1d1641SHerbert Xu 295eb1d1641SHerbert Xu return; 296eb1d1641SHerbert Xu } 297eb1d1641SHerbert Xu 298eb1d1641SHerbert Xu WARN_ON(1); 299eb1d1641SHerbert Xu } 300eb1d1641SHerbert Xu 301eb1d1641SHerbert Xu static void br_multicast_port_group_expired(unsigned long data) 302eb1d1641SHerbert Xu { 303eb1d1641SHerbert Xu struct net_bridge_port_group *pg = (void *)data; 304eb1d1641SHerbert Xu struct net_bridge *br = pg->port->br; 305eb1d1641SHerbert Xu 306eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 307eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&pg->timer) || 3089d06b6d8SElad Raz hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT) 309eb1d1641SHerbert Xu goto out; 310eb1d1641SHerbert Xu 311eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 312eb1d1641SHerbert Xu 313eb1d1641SHerbert Xu out: 314eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 315eb1d1641SHerbert Xu } 316eb1d1641SHerbert Xu 317e8051688SEric Dumazet static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max, 318eb1d1641SHerbert Xu int elasticity) 319eb1d1641SHerbert Xu { 320e8051688SEric Dumazet struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1); 321eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 322eb1d1641SHerbert Xu int err; 323eb1d1641SHerbert Xu 324eb1d1641SHerbert Xu mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC); 325eb1d1641SHerbert Xu if (!mdb) 326eb1d1641SHerbert Xu return -ENOMEM; 327eb1d1641SHerbert Xu 328eb1d1641SHerbert Xu mdb->max = max; 329eb1d1641SHerbert Xu mdb->old = old; 330eb1d1641SHerbert Xu 331eb1d1641SHerbert Xu mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC); 332eb1d1641SHerbert Xu if (!mdb->mhash) { 333eb1d1641SHerbert Xu kfree(mdb); 334eb1d1641SHerbert Xu return -ENOMEM; 335eb1d1641SHerbert Xu } 336eb1d1641SHerbert Xu 337eb1d1641SHerbert Xu mdb->size = old ? old->size : 0; 338eb1d1641SHerbert Xu mdb->ver = old ? old->ver ^ 1 : 0; 339eb1d1641SHerbert Xu 340eb1d1641SHerbert Xu if (!old || elasticity) 341eb1d1641SHerbert Xu get_random_bytes(&mdb->secret, sizeof(mdb->secret)); 342eb1d1641SHerbert Xu else 343eb1d1641SHerbert Xu mdb->secret = old->secret; 344eb1d1641SHerbert Xu 345eb1d1641SHerbert Xu if (!old) 346eb1d1641SHerbert Xu goto out; 347eb1d1641SHerbert Xu 348eb1d1641SHerbert Xu err = br_mdb_copy(mdb, old, elasticity); 349eb1d1641SHerbert Xu if (err) { 350eb1d1641SHerbert Xu kfree(mdb->mhash); 351eb1d1641SHerbert Xu kfree(mdb); 352eb1d1641SHerbert Xu return err; 353eb1d1641SHerbert Xu } 354eb1d1641SHerbert Xu 3552ce297fcSCong Wang br_mdb_rehash_seq++; 356eb1d1641SHerbert Xu call_rcu_bh(&mdb->rcu, br_mdb_free); 357eb1d1641SHerbert Xu 358eb1d1641SHerbert Xu out: 359eb1d1641SHerbert Xu rcu_assign_pointer(*mdbp, mdb); 360eb1d1641SHerbert Xu 361eb1d1641SHerbert Xu return 0; 362eb1d1641SHerbert Xu } 363eb1d1641SHerbert Xu 3648ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br, 3651080ab95SNikolay Aleksandrov __be32 group, 3661080ab95SNikolay Aleksandrov u8 *igmp_type) 367eb1d1641SHerbert Xu { 3685e923585SNikolay Aleksandrov struct igmpv3_query *ihv3; 3695e923585SNikolay Aleksandrov size_t igmp_hdr_size; 370eb1d1641SHerbert Xu struct sk_buff *skb; 371eb1d1641SHerbert Xu struct igmphdr *ih; 372eb1d1641SHerbert Xu struct ethhdr *eth; 373eb1d1641SHerbert Xu struct iphdr *iph; 374eb1d1641SHerbert Xu 3755e923585SNikolay Aleksandrov igmp_hdr_size = sizeof(*ih); 3765e923585SNikolay Aleksandrov if (br->multicast_igmp_version == 3) 3775e923585SNikolay Aleksandrov igmp_hdr_size = sizeof(*ihv3); 378eb1d1641SHerbert Xu skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) + 3795e923585SNikolay Aleksandrov igmp_hdr_size + 4); 380eb1d1641SHerbert Xu if (!skb) 381eb1d1641SHerbert Xu goto out; 382eb1d1641SHerbert Xu 383eb1d1641SHerbert Xu skb->protocol = htons(ETH_P_IP); 384eb1d1641SHerbert Xu 385eb1d1641SHerbert Xu skb_reset_mac_header(skb); 386eb1d1641SHerbert Xu eth = eth_hdr(skb); 387eb1d1641SHerbert Xu 388e5a727f6SJoe Perches ether_addr_copy(eth->h_source, br->dev->dev_addr); 389eb1d1641SHerbert Xu eth->h_dest[0] = 1; 390eb1d1641SHerbert Xu eth->h_dest[1] = 0; 391eb1d1641SHerbert Xu eth->h_dest[2] = 0x5e; 392eb1d1641SHerbert Xu eth->h_dest[3] = 0; 393eb1d1641SHerbert Xu eth->h_dest[4] = 0; 394eb1d1641SHerbert Xu eth->h_dest[5] = 1; 395eb1d1641SHerbert Xu eth->h_proto = htons(ETH_P_IP); 396eb1d1641SHerbert Xu skb_put(skb, sizeof(*eth)); 397eb1d1641SHerbert Xu 398eb1d1641SHerbert Xu skb_set_network_header(skb, skb->len); 399eb1d1641SHerbert Xu iph = ip_hdr(skb); 400eb1d1641SHerbert Xu 401eb1d1641SHerbert Xu iph->version = 4; 402eb1d1641SHerbert Xu iph->ihl = 6; 403eb1d1641SHerbert Xu iph->tos = 0xc0; 4045e923585SNikolay Aleksandrov iph->tot_len = htons(sizeof(*iph) + igmp_hdr_size + 4); 405eb1d1641SHerbert Xu iph->id = 0; 406eb1d1641SHerbert Xu iph->frag_off = htons(IP_DF); 407eb1d1641SHerbert Xu iph->ttl = 1; 408eb1d1641SHerbert Xu iph->protocol = IPPROTO_IGMP; 4091c8ad5bfSCong Wang iph->saddr = br->multicast_query_use_ifaddr ? 4101c8ad5bfSCong Wang inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0; 411eb1d1641SHerbert Xu iph->daddr = htonl(INADDR_ALLHOSTS_GROUP); 412eb1d1641SHerbert Xu ((u8 *)&iph[1])[0] = IPOPT_RA; 413eb1d1641SHerbert Xu ((u8 *)&iph[1])[1] = 4; 414eb1d1641SHerbert Xu ((u8 *)&iph[1])[2] = 0; 415eb1d1641SHerbert Xu ((u8 *)&iph[1])[3] = 0; 416eb1d1641SHerbert Xu ip_send_check(iph); 417eb1d1641SHerbert Xu skb_put(skb, 24); 418eb1d1641SHerbert Xu 419eb1d1641SHerbert Xu skb_set_transport_header(skb, skb->len); 4201080ab95SNikolay Aleksandrov *igmp_type = IGMP_HOST_MEMBERSHIP_QUERY; 4215e923585SNikolay Aleksandrov 4225e923585SNikolay Aleksandrov switch (br->multicast_igmp_version) { 4235e923585SNikolay Aleksandrov case 2: 4245e923585SNikolay Aleksandrov ih = igmp_hdr(skb); 425eb1d1641SHerbert Xu ih->type = IGMP_HOST_MEMBERSHIP_QUERY; 426eb1d1641SHerbert Xu ih->code = (group ? br->multicast_last_member_interval : 427eb1d1641SHerbert Xu br->multicast_query_response_interval) / 428eb1d1641SHerbert Xu (HZ / IGMP_TIMER_SCALE); 429eb1d1641SHerbert Xu ih->group = group; 430eb1d1641SHerbert Xu ih->csum = 0; 4315e923585SNikolay Aleksandrov ih->csum = ip_compute_csum((void *)ih, sizeof(*ih)); 4325e923585SNikolay Aleksandrov break; 4335e923585SNikolay Aleksandrov case 3: 4345e923585SNikolay Aleksandrov ihv3 = igmpv3_query_hdr(skb); 4355e923585SNikolay Aleksandrov ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY; 4365e923585SNikolay Aleksandrov ihv3->code = (group ? br->multicast_last_member_interval : 4375e923585SNikolay Aleksandrov br->multicast_query_response_interval) / 4385e923585SNikolay Aleksandrov (HZ / IGMP_TIMER_SCALE); 4395e923585SNikolay Aleksandrov ihv3->group = group; 4405e923585SNikolay Aleksandrov ihv3->qqic = br->multicast_query_interval / HZ; 4415e923585SNikolay Aleksandrov ihv3->nsrcs = 0; 4425e923585SNikolay Aleksandrov ihv3->resv = 0; 4435e923585SNikolay Aleksandrov ihv3->suppress = 0; 4445e923585SNikolay Aleksandrov ihv3->qrv = 2; 4455e923585SNikolay Aleksandrov ihv3->csum = 0; 4465e923585SNikolay Aleksandrov ihv3->csum = ip_compute_csum((void *)ihv3, sizeof(*ihv3)); 4475e923585SNikolay Aleksandrov break; 4485e923585SNikolay Aleksandrov } 449eb1d1641SHerbert Xu 4505e923585SNikolay Aleksandrov skb_put(skb, igmp_hdr_size); 451eb1d1641SHerbert Xu __skb_pull(skb, sizeof(*eth)); 452eb1d1641SHerbert Xu 453eb1d1641SHerbert Xu out: 454eb1d1641SHerbert Xu return skb; 455eb1d1641SHerbert Xu } 456eb1d1641SHerbert Xu 457dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 45808b202b6SYOSHIFUJI Hideaki static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br, 4591080ab95SNikolay Aleksandrov const struct in6_addr *grp, 4601080ab95SNikolay Aleksandrov u8 *igmp_type) 46108b202b6SYOSHIFUJI Hideaki { 462*aa2ae3e7SNikolay Aleksandrov struct mld2_query *mld2q; 463*aa2ae3e7SNikolay Aleksandrov unsigned long interval; 46408b202b6SYOSHIFUJI Hideaki struct ipv6hdr *ip6h; 46508b202b6SYOSHIFUJI Hideaki struct mld_msg *mldq; 466*aa2ae3e7SNikolay Aleksandrov size_t mld_hdr_size; 467*aa2ae3e7SNikolay Aleksandrov struct sk_buff *skb; 46808b202b6SYOSHIFUJI Hideaki struct ethhdr *eth; 46908b202b6SYOSHIFUJI Hideaki u8 *hopopt; 47008b202b6SYOSHIFUJI Hideaki 471*aa2ae3e7SNikolay Aleksandrov mld_hdr_size = sizeof(*mldq); 472*aa2ae3e7SNikolay Aleksandrov if (br->multicast_mld_version == 2) 473*aa2ae3e7SNikolay Aleksandrov mld_hdr_size = sizeof(*mld2q); 47408b202b6SYOSHIFUJI Hideaki skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) + 475*aa2ae3e7SNikolay Aleksandrov 8 + mld_hdr_size); 47608b202b6SYOSHIFUJI Hideaki if (!skb) 47708b202b6SYOSHIFUJI Hideaki goto out; 47808b202b6SYOSHIFUJI Hideaki 47908b202b6SYOSHIFUJI Hideaki skb->protocol = htons(ETH_P_IPV6); 48008b202b6SYOSHIFUJI Hideaki 48108b202b6SYOSHIFUJI Hideaki /* Ethernet header */ 48208b202b6SYOSHIFUJI Hideaki skb_reset_mac_header(skb); 48308b202b6SYOSHIFUJI Hideaki eth = eth_hdr(skb); 48408b202b6SYOSHIFUJI Hideaki 485e5a727f6SJoe Perches ether_addr_copy(eth->h_source, br->dev->dev_addr); 48608b202b6SYOSHIFUJI Hideaki eth->h_proto = htons(ETH_P_IPV6); 48708b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*eth)); 48808b202b6SYOSHIFUJI Hideaki 48908b202b6SYOSHIFUJI Hideaki /* IPv6 header + HbH option */ 49008b202b6SYOSHIFUJI Hideaki skb_set_network_header(skb, skb->len); 49108b202b6SYOSHIFUJI Hideaki ip6h = ipv6_hdr(skb); 49208b202b6SYOSHIFUJI Hideaki 49308b202b6SYOSHIFUJI Hideaki *(__force __be32 *)ip6h = htonl(0x60000000); 494*aa2ae3e7SNikolay Aleksandrov ip6h->payload_len = htons(8 + mld_hdr_size); 49508b202b6SYOSHIFUJI Hideaki ip6h->nexthdr = IPPROTO_HOPOPTS; 49608b202b6SYOSHIFUJI Hideaki ip6h->hop_limit = 1; 497a7bff75bSLinus Lüssing ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1)); 498d1d81d4cSUlrich Weber if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0, 499d1d81d4cSUlrich Weber &ip6h->saddr)) { 500d1d81d4cSUlrich Weber kfree_skb(skb); 5010888d5f3Sdaniel br->has_ipv6_addr = 0; 502d1d81d4cSUlrich Weber return NULL; 503d1d81d4cSUlrich Weber } 5040888d5f3Sdaniel 5050888d5f3Sdaniel br->has_ipv6_addr = 1; 50636cff5a1SLinus Lüssing ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest); 50708b202b6SYOSHIFUJI Hideaki 50808b202b6SYOSHIFUJI Hideaki hopopt = (u8 *)(ip6h + 1); 50908b202b6SYOSHIFUJI Hideaki hopopt[0] = IPPROTO_ICMPV6; /* next hdr */ 51008b202b6SYOSHIFUJI Hideaki hopopt[1] = 0; /* length of HbH */ 51108b202b6SYOSHIFUJI Hideaki hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */ 51208b202b6SYOSHIFUJI Hideaki hopopt[3] = 2; /* Length of RA Option */ 51308b202b6SYOSHIFUJI Hideaki hopopt[4] = 0; /* Type = 0x0000 (MLD) */ 51408b202b6SYOSHIFUJI Hideaki hopopt[5] = 0; 5151de5a71cSEldad Zack hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */ 5161de5a71cSEldad Zack hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */ 51708b202b6SYOSHIFUJI Hideaki 51808b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*ip6h) + 8); 51908b202b6SYOSHIFUJI Hideaki 52008b202b6SYOSHIFUJI Hideaki /* ICMPv6 */ 52108b202b6SYOSHIFUJI Hideaki skb_set_transport_header(skb, skb->len); 5221080ab95SNikolay Aleksandrov interval = ipv6_addr_any(grp) ? 52332de868cSLinus Lüssing br->multicast_query_response_interval : 52432de868cSLinus Lüssing br->multicast_last_member_interval; 5251080ab95SNikolay Aleksandrov *igmp_type = ICMPV6_MGM_QUERY; 526*aa2ae3e7SNikolay Aleksandrov switch (br->multicast_mld_version) { 527*aa2ae3e7SNikolay Aleksandrov case 1: 528*aa2ae3e7SNikolay Aleksandrov mldq = (struct mld_msg *)icmp6_hdr(skb); 52908b202b6SYOSHIFUJI Hideaki mldq->mld_type = ICMPV6_MGM_QUERY; 53008b202b6SYOSHIFUJI Hideaki mldq->mld_code = 0; 53108b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = 0; 53208b202b6SYOSHIFUJI Hideaki mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval)); 53308b202b6SYOSHIFUJI Hideaki mldq->mld_reserved = 0; 5341080ab95SNikolay Aleksandrov mldq->mld_mca = *grp; 53508b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, 53608b202b6SYOSHIFUJI Hideaki sizeof(*mldq), IPPROTO_ICMPV6, 53708b202b6SYOSHIFUJI Hideaki csum_partial(mldq, 538*aa2ae3e7SNikolay Aleksandrov sizeof(*mldq), 539*aa2ae3e7SNikolay Aleksandrov 0)); 540*aa2ae3e7SNikolay Aleksandrov break; 541*aa2ae3e7SNikolay Aleksandrov case 2: 542*aa2ae3e7SNikolay Aleksandrov mld2q = (struct mld2_query *)icmp6_hdr(skb); 543*aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_mrc = ntohs((u16)jiffies_to_msecs(interval)); 544*aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_type = ICMPV6_MGM_QUERY; 545*aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_code = 0; 546*aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_cksum = 0; 547*aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_resv1 = 0; 548*aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_resv2 = 0; 549*aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_suppress = 0; 550*aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_qrv = 2; 551*aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_nsrcs = 0; 552*aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_qqic = br->multicast_query_interval / HZ; 553*aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_mca = *grp; 554*aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, 555*aa2ae3e7SNikolay Aleksandrov sizeof(*mld2q), 556*aa2ae3e7SNikolay Aleksandrov IPPROTO_ICMPV6, 557*aa2ae3e7SNikolay Aleksandrov csum_partial(mld2q, 558*aa2ae3e7SNikolay Aleksandrov sizeof(*mld2q), 559*aa2ae3e7SNikolay Aleksandrov 0)); 560*aa2ae3e7SNikolay Aleksandrov break; 561*aa2ae3e7SNikolay Aleksandrov } 562*aa2ae3e7SNikolay Aleksandrov skb_put(skb, mld_hdr_size); 56308b202b6SYOSHIFUJI Hideaki 56408b202b6SYOSHIFUJI Hideaki __skb_pull(skb, sizeof(*eth)); 56508b202b6SYOSHIFUJI Hideaki 56608b202b6SYOSHIFUJI Hideaki out: 56708b202b6SYOSHIFUJI Hideaki return skb; 56808b202b6SYOSHIFUJI Hideaki } 56908b202b6SYOSHIFUJI Hideaki #endif 57008b202b6SYOSHIFUJI Hideaki 5718ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br, 5721080ab95SNikolay Aleksandrov struct br_ip *addr, 5731080ab95SNikolay Aleksandrov u8 *igmp_type) 5748ef2a9a5SYOSHIFUJI Hideaki { 5758ef2a9a5SYOSHIFUJI Hideaki switch (addr->proto) { 5768ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 5771080ab95SNikolay Aleksandrov return br_ip4_multicast_alloc_query(br, addr->u.ip4, igmp_type); 578dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 57908b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 5801080ab95SNikolay Aleksandrov return br_ip6_multicast_alloc_query(br, &addr->u.ip6, 5811080ab95SNikolay Aleksandrov igmp_type); 58208b202b6SYOSHIFUJI Hideaki #endif 5838ef2a9a5SYOSHIFUJI Hideaki } 5848ef2a9a5SYOSHIFUJI Hideaki return NULL; 5858ef2a9a5SYOSHIFUJI Hideaki } 5868ef2a9a5SYOSHIFUJI Hideaki 587eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *br_multicast_get_group( 5888ef2a9a5SYOSHIFUJI Hideaki struct net_bridge *br, struct net_bridge_port *port, 5898ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group, int hash) 590eb1d1641SHerbert Xu { 591e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 592eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 59395c96174SEric Dumazet unsigned int count = 0; 59495c96174SEric Dumazet unsigned int max; 595eb1d1641SHerbert Xu int elasticity; 596eb1d1641SHerbert Xu int err; 597eb1d1641SHerbert Xu 598e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 599b67bfe0dSSasha Levin hlist_for_each_entry(mp, &mdb->mhash[hash], hlist[mdb->ver]) { 600eb1d1641SHerbert Xu count++; 6018ef2a9a5SYOSHIFUJI Hideaki if (unlikely(br_ip_equal(group, &mp->addr))) 602eb1d1641SHerbert Xu return mp; 603eb1d1641SHerbert Xu } 604eb1d1641SHerbert Xu 605eb1d1641SHerbert Xu elasticity = 0; 606eb1d1641SHerbert Xu max = mdb->max; 607eb1d1641SHerbert Xu 608eb1d1641SHerbert Xu if (unlikely(count > br->hash_elasticity && count)) { 609eb1d1641SHerbert Xu if (net_ratelimit()) 61028a16c97Sstephen hemminger br_info(br, "Multicast hash table " 611eb1d1641SHerbert Xu "chain limit reached: %s\n", 61228a16c97Sstephen hemminger port ? port->dev->name : br->dev->name); 613eb1d1641SHerbert Xu 614eb1d1641SHerbert Xu elasticity = br->hash_elasticity; 615eb1d1641SHerbert Xu } 616eb1d1641SHerbert Xu 617eb1d1641SHerbert Xu if (mdb->size >= max) { 618eb1d1641SHerbert Xu max *= 2; 619036be6dbSThomas Graf if (unlikely(max > br->hash_max)) { 620036be6dbSThomas Graf br_warn(br, "Multicast hash table maximum of %d " 621036be6dbSThomas Graf "reached, disabling snooping: %s\n", 622036be6dbSThomas Graf br->hash_max, 623036be6dbSThomas Graf port ? port->dev->name : br->dev->name); 624eb1d1641SHerbert Xu err = -E2BIG; 625eb1d1641SHerbert Xu disable: 626eb1d1641SHerbert Xu br->multicast_disabled = 1; 627eb1d1641SHerbert Xu goto err; 628eb1d1641SHerbert Xu } 629eb1d1641SHerbert Xu } 630eb1d1641SHerbert Xu 631eb1d1641SHerbert Xu if (max > mdb->max || elasticity) { 632eb1d1641SHerbert Xu if (mdb->old) { 633eb1d1641SHerbert Xu if (net_ratelimit()) 63428a16c97Sstephen hemminger br_info(br, "Multicast hash table " 635eb1d1641SHerbert Xu "on fire: %s\n", 63628a16c97Sstephen hemminger port ? port->dev->name : br->dev->name); 637eb1d1641SHerbert Xu err = -EEXIST; 638eb1d1641SHerbert Xu goto err; 639eb1d1641SHerbert Xu } 640eb1d1641SHerbert Xu 641eb1d1641SHerbert Xu err = br_mdb_rehash(&br->mdb, max, elasticity); 642eb1d1641SHerbert Xu if (err) { 64328a16c97Sstephen hemminger br_warn(br, "Cannot rehash multicast " 64428a16c97Sstephen hemminger "hash table, disabling snooping: %s, %d, %d\n", 64528a16c97Sstephen hemminger port ? port->dev->name : br->dev->name, 646eb1d1641SHerbert Xu mdb->size, err); 647eb1d1641SHerbert Xu goto disable; 648eb1d1641SHerbert Xu } 649eb1d1641SHerbert Xu 650eb1d1641SHerbert Xu err = -EAGAIN; 651eb1d1641SHerbert Xu goto err; 652eb1d1641SHerbert Xu } 653eb1d1641SHerbert Xu 654eb1d1641SHerbert Xu return NULL; 655eb1d1641SHerbert Xu 656eb1d1641SHerbert Xu err: 657eb1d1641SHerbert Xu mp = ERR_PTR(err); 658eb1d1641SHerbert Xu return mp; 659eb1d1641SHerbert Xu } 660eb1d1641SHerbert Xu 661cfd56754SCong Wang struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br, 6625e923585SNikolay Aleksandrov struct net_bridge_port *p, 6635e923585SNikolay Aleksandrov struct br_ip *group) 664eb1d1641SHerbert Xu { 665e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 666eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 667eb1d1641SHerbert Xu int hash; 6684c0833bcSTobias Klauser int err; 669eb1d1641SHerbert Xu 670e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 671eb1d1641SHerbert Xu if (!mdb) { 6724c0833bcSTobias Klauser err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0); 6734c0833bcSTobias Klauser if (err) 6744c0833bcSTobias Klauser return ERR_PTR(err); 675eb1d1641SHerbert Xu goto rehash; 676eb1d1641SHerbert Xu } 677eb1d1641SHerbert Xu 678eb1d1641SHerbert Xu hash = br_ip_hash(mdb, group); 6795e923585SNikolay Aleksandrov mp = br_multicast_get_group(br, p, group, hash); 680eb1d1641SHerbert Xu switch (PTR_ERR(mp)) { 681eb1d1641SHerbert Xu case 0: 682eb1d1641SHerbert Xu break; 683eb1d1641SHerbert Xu 684eb1d1641SHerbert Xu case -EAGAIN: 685eb1d1641SHerbert Xu rehash: 686e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 687eb1d1641SHerbert Xu hash = br_ip_hash(mdb, group); 688eb1d1641SHerbert Xu break; 689eb1d1641SHerbert Xu 690eb1d1641SHerbert Xu default: 691eb1d1641SHerbert Xu goto out; 692eb1d1641SHerbert Xu } 693eb1d1641SHerbert Xu 694eb1d1641SHerbert Xu mp = kzalloc(sizeof(*mp), GFP_ATOMIC); 695eb1d1641SHerbert Xu if (unlikely(!mp)) 6964c0833bcSTobias Klauser return ERR_PTR(-ENOMEM); 697eb1d1641SHerbert Xu 698eb1d1641SHerbert Xu mp->br = br; 6998ef2a9a5SYOSHIFUJI Hideaki mp->addr = *group; 7001faabf2aSEric Dumazet setup_timer(&mp->timer, br_multicast_group_expired, 7011faabf2aSEric Dumazet (unsigned long)mp); 7021faabf2aSEric Dumazet 703eb1d1641SHerbert Xu hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]); 704eb1d1641SHerbert Xu mdb->size++; 705eb1d1641SHerbert Xu 706eb1d1641SHerbert Xu out: 707eb1d1641SHerbert Xu return mp; 708eb1d1641SHerbert Xu } 709eb1d1641SHerbert Xu 710cfd56754SCong Wang struct net_bridge_port_group *br_multicast_new_port_group( 711cfd56754SCong Wang struct net_bridge_port *port, 712cfd56754SCong Wang struct br_ip *group, 713ccb1c31aSAmerigo Wang struct net_bridge_port_group __rcu *next, 7149d06b6d8SElad Raz unsigned char flags) 715cfd56754SCong Wang { 716cfd56754SCong Wang struct net_bridge_port_group *p; 717cfd56754SCong Wang 718cfd56754SCong Wang p = kzalloc(sizeof(*p), GFP_ATOMIC); 719cfd56754SCong Wang if (unlikely(!p)) 720cfd56754SCong Wang return NULL; 721cfd56754SCong Wang 722cfd56754SCong Wang p->addr = *group; 723cfd56754SCong Wang p->port = port; 7249d06b6d8SElad Raz p->flags = flags; 725eca2a43bSstephen hemminger rcu_assign_pointer(p->next, next); 726cfd56754SCong Wang hlist_add_head(&p->mglist, &port->mglist); 727cfd56754SCong Wang setup_timer(&p->timer, br_multicast_port_group_expired, 728cfd56754SCong Wang (unsigned long)p); 729cfd56754SCong Wang return p; 730cfd56754SCong Wang } 731cfd56754SCong Wang 732eb1d1641SHerbert Xu static int br_multicast_add_group(struct net_bridge *br, 7338ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 7348ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group) 735eb1d1641SHerbert Xu { 736e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 7375e923585SNikolay Aleksandrov struct net_bridge_port_group *p; 7385e923585SNikolay Aleksandrov struct net_bridge_mdb_entry *mp; 739454594f3SLinus Lüssing unsigned long now = jiffies; 740eb1d1641SHerbert Xu int err; 741eb1d1641SHerbert Xu 742eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 743eb1d1641SHerbert Xu if (!netif_running(br->dev) || 744eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 745eb1d1641SHerbert Xu goto out; 746eb1d1641SHerbert Xu 747eb1d1641SHerbert Xu mp = br_multicast_new_group(br, port, group); 748eb1d1641SHerbert Xu err = PTR_ERR(mp); 7494c0833bcSTobias Klauser if (IS_ERR(mp)) 750eb1d1641SHerbert Xu goto err; 751eb1d1641SHerbert Xu 752eb1d1641SHerbert Xu if (!port) { 7538a870178SHerbert Xu mp->mglist = true; 754454594f3SLinus Lüssing mod_timer(&mp->timer, now + br->multicast_membership_interval); 755eb1d1641SHerbert Xu goto out; 756eb1d1641SHerbert Xu } 757eb1d1641SHerbert Xu 758e8051688SEric Dumazet for (pp = &mp->ports; 759e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 760e8051688SEric Dumazet pp = &p->next) { 761eb1d1641SHerbert Xu if (p->port == port) 762454594f3SLinus Lüssing goto found; 763eb1d1641SHerbert Xu if ((unsigned long)p->port < (unsigned long)port) 764eb1d1641SHerbert Xu break; 765eb1d1641SHerbert Xu } 766eb1d1641SHerbert Xu 7679d06b6d8SElad Raz p = br_multicast_new_port_group(port, group, *pp, 0); 768eb1d1641SHerbert Xu if (unlikely(!p)) 769eb1d1641SHerbert Xu goto err; 770eb1d1641SHerbert Xu rcu_assign_pointer(*pp, p); 77145ebcce5SElad Raz br_mdb_notify(br->dev, port, group, RTM_NEWMDB, 0); 772eb1d1641SHerbert Xu 773454594f3SLinus Lüssing found: 774454594f3SLinus Lüssing mod_timer(&p->timer, now + br->multicast_membership_interval); 775eb1d1641SHerbert Xu out: 776eb1d1641SHerbert Xu err = 0; 777eb1d1641SHerbert Xu 778eb1d1641SHerbert Xu err: 779eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 780eb1d1641SHerbert Xu return err; 781eb1d1641SHerbert Xu } 782eb1d1641SHerbert Xu 7838ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_add_group(struct net_bridge *br, 7848ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 785b0e9a30dSVlad Yasevich __be32 group, 786b0e9a30dSVlad Yasevich __u16 vid) 7878ef2a9a5SYOSHIFUJI Hideaki { 7888ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 7898ef2a9a5SYOSHIFUJI Hideaki 7908ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 7918ef2a9a5SYOSHIFUJI Hideaki return 0; 7928ef2a9a5SYOSHIFUJI Hideaki 7938ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 7948ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 795b0e9a30dSVlad Yasevich br_group.vid = vid; 7968ef2a9a5SYOSHIFUJI Hideaki 7978ef2a9a5SYOSHIFUJI Hideaki return br_multicast_add_group(br, port, &br_group); 7988ef2a9a5SYOSHIFUJI Hideaki } 7998ef2a9a5SYOSHIFUJI Hideaki 800dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 80108b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_add_group(struct net_bridge *br, 80208b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 803b0e9a30dSVlad Yasevich const struct in6_addr *group, 804b0e9a30dSVlad Yasevich __u16 vid) 80508b202b6SYOSHIFUJI Hideaki { 80608b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 80708b202b6SYOSHIFUJI Hideaki 8083c3769e6SLinus Lüssing if (ipv6_addr_is_ll_all_nodes(group)) 80908b202b6SYOSHIFUJI Hideaki return 0; 81008b202b6SYOSHIFUJI Hideaki 8114e3fd7a0SAlexey Dobriyan br_group.u.ip6 = *group; 8129cc6e0c4SLinus Lüssing br_group.proto = htons(ETH_P_IPV6); 813b0e9a30dSVlad Yasevich br_group.vid = vid; 81408b202b6SYOSHIFUJI Hideaki 81508b202b6SYOSHIFUJI Hideaki return br_multicast_add_group(br, port, &br_group); 81608b202b6SYOSHIFUJI Hideaki } 81708b202b6SYOSHIFUJI Hideaki #endif 81808b202b6SYOSHIFUJI Hideaki 819eb1d1641SHerbert Xu static void br_multicast_router_expired(unsigned long data) 820eb1d1641SHerbert Xu { 821eb1d1641SHerbert Xu struct net_bridge_port *port = (void *)data; 822eb1d1641SHerbert Xu struct net_bridge *br = port->br; 823eb1d1641SHerbert Xu 824eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 825a55d8246SNikolay Aleksandrov if (port->multicast_router == MDB_RTR_TYPE_DISABLED || 826a55d8246SNikolay Aleksandrov port->multicast_router == MDB_RTR_TYPE_PERM || 827eb1d1641SHerbert Xu timer_pending(&port->multicast_router_timer) || 828eb1d1641SHerbert Xu hlist_unhashed(&port->rlist)) 829eb1d1641SHerbert Xu goto out; 830eb1d1641SHerbert Xu 831eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 832949f1e39SSatish Ashok br_rtr_notify(br->dev, port, RTM_DELMDB); 833a55d8246SNikolay Aleksandrov /* Don't allow timer refresh if the router expired */ 834a55d8246SNikolay Aleksandrov if (port->multicast_router == MDB_RTR_TYPE_TEMP) 835a55d8246SNikolay Aleksandrov port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 836eb1d1641SHerbert Xu 837eb1d1641SHerbert Xu out: 838eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 839eb1d1641SHerbert Xu } 840eb1d1641SHerbert Xu 841eb1d1641SHerbert Xu static void br_multicast_local_router_expired(unsigned long data) 842eb1d1641SHerbert Xu { 843eb1d1641SHerbert Xu } 844eb1d1641SHerbert Xu 845cc0fdd80SLinus Lüssing static void br_multicast_querier_expired(struct net_bridge *br, 84690010b36SLinus Lüssing struct bridge_mcast_own_query *query) 847c83b8fabSHerbert Xu { 848c83b8fabSHerbert Xu spin_lock(&br->multicast_lock); 849c83b8fabSHerbert Xu if (!netif_running(br->dev) || br->multicast_disabled) 850c83b8fabSHerbert Xu goto out; 851c83b8fabSHerbert Xu 852cc0fdd80SLinus Lüssing br_multicast_start_querier(br, query); 853c83b8fabSHerbert Xu 854c83b8fabSHerbert Xu out: 855c83b8fabSHerbert Xu spin_unlock(&br->multicast_lock); 856c83b8fabSHerbert Xu } 857c83b8fabSHerbert Xu 858cc0fdd80SLinus Lüssing static void br_ip4_multicast_querier_expired(unsigned long data) 859cc0fdd80SLinus Lüssing { 860cc0fdd80SLinus Lüssing struct net_bridge *br = (void *)data; 861cc0fdd80SLinus Lüssing 86290010b36SLinus Lüssing br_multicast_querier_expired(br, &br->ip4_own_query); 863cc0fdd80SLinus Lüssing } 864cc0fdd80SLinus Lüssing 865cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 866cc0fdd80SLinus Lüssing static void br_ip6_multicast_querier_expired(unsigned long data) 867cc0fdd80SLinus Lüssing { 868cc0fdd80SLinus Lüssing struct net_bridge *br = (void *)data; 869cc0fdd80SLinus Lüssing 87090010b36SLinus Lüssing br_multicast_querier_expired(br, &br->ip6_own_query); 871cc0fdd80SLinus Lüssing } 872cc0fdd80SLinus Lüssing #endif 873cc0fdd80SLinus Lüssing 874dc4eb53aSLinus Lüssing static void br_multicast_select_own_querier(struct net_bridge *br, 875dc4eb53aSLinus Lüssing struct br_ip *ip, 876dc4eb53aSLinus Lüssing struct sk_buff *skb) 877dc4eb53aSLinus Lüssing { 878dc4eb53aSLinus Lüssing if (ip->proto == htons(ETH_P_IP)) 879dc4eb53aSLinus Lüssing br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr; 880dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 881dc4eb53aSLinus Lüssing else 882dc4eb53aSLinus Lüssing br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr; 883dc4eb53aSLinus Lüssing #endif 884dc4eb53aSLinus Lüssing } 885dc4eb53aSLinus Lüssing 8868ef2a9a5SYOSHIFUJI Hideaki static void __br_multicast_send_query(struct net_bridge *br, 8878ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 8888ef2a9a5SYOSHIFUJI Hideaki struct br_ip *ip) 889eb1d1641SHerbert Xu { 890eb1d1641SHerbert Xu struct sk_buff *skb; 8911080ab95SNikolay Aleksandrov u8 igmp_type; 892eb1d1641SHerbert Xu 8931080ab95SNikolay Aleksandrov skb = br_multicast_alloc_query(br, ip, &igmp_type); 894eb1d1641SHerbert Xu if (!skb) 8958ef2a9a5SYOSHIFUJI Hideaki return; 896eb1d1641SHerbert Xu 897eb1d1641SHerbert Xu if (port) { 898eb1d1641SHerbert Xu skb->dev = port->dev; 899a65056ecSNikolay Aleksandrov br_multicast_count(br, port, skb, igmp_type, 9001080ab95SNikolay Aleksandrov BR_MCAST_DIR_TX); 90129a26a56SEric W. Biederman NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, 90229a26a56SEric W. Biederman dev_net(port->dev), NULL, skb, NULL, skb->dev, 903f0b4eeceSLinus Lüssing br_dev_queue_push_xmit); 904dc4eb53aSLinus Lüssing } else { 905dc4eb53aSLinus Lüssing br_multicast_select_own_querier(br, ip, skb); 906a65056ecSNikolay Aleksandrov br_multicast_count(br, port, skb, igmp_type, 9071080ab95SNikolay Aleksandrov BR_MCAST_DIR_RX); 908eb1d1641SHerbert Xu netif_rx(skb); 9098ef2a9a5SYOSHIFUJI Hideaki } 910dc4eb53aSLinus Lüssing } 911eb1d1641SHerbert Xu 9128ef2a9a5SYOSHIFUJI Hideaki static void br_multicast_send_query(struct net_bridge *br, 913cc0fdd80SLinus Lüssing struct net_bridge_port *port, 91490010b36SLinus Lüssing struct bridge_mcast_own_query *own_query) 9158ef2a9a5SYOSHIFUJI Hideaki { 91690010b36SLinus Lüssing struct bridge_mcast_other_query *other_query = NULL; 9175e923585SNikolay Aleksandrov struct br_ip br_group; 9185e923585SNikolay Aleksandrov unsigned long time; 9198ef2a9a5SYOSHIFUJI Hideaki 9208ef2a9a5SYOSHIFUJI Hideaki if (!netif_running(br->dev) || br->multicast_disabled || 921cc0fdd80SLinus Lüssing !br->multicast_querier) 9228ef2a9a5SYOSHIFUJI Hideaki return; 9238ef2a9a5SYOSHIFUJI Hideaki 92408b202b6SYOSHIFUJI Hideaki memset(&br_group.u, 0, sizeof(br_group.u)); 9258ef2a9a5SYOSHIFUJI Hideaki 92690010b36SLinus Lüssing if (port ? (own_query == &port->ip4_own_query) : 92790010b36SLinus Lüssing (own_query == &br->ip4_own_query)) { 92890010b36SLinus Lüssing other_query = &br->ip4_other_query; 92908b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 930dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 931cc0fdd80SLinus Lüssing } else { 93290010b36SLinus Lüssing other_query = &br->ip6_other_query; 93308b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 93408b202b6SYOSHIFUJI Hideaki #endif 935eb1d1641SHerbert Xu } 936eb1d1641SHerbert Xu 93790010b36SLinus Lüssing if (!other_query || timer_pending(&other_query->timer)) 938cc0fdd80SLinus Lüssing return; 939cc0fdd80SLinus Lüssing 940cc0fdd80SLinus Lüssing __br_multicast_send_query(br, port, &br_group); 941cc0fdd80SLinus Lüssing 942cc0fdd80SLinus Lüssing time = jiffies; 94390010b36SLinus Lüssing time += own_query->startup_sent < br->multicast_startup_query_count ? 944cc0fdd80SLinus Lüssing br->multicast_startup_query_interval : 945cc0fdd80SLinus Lüssing br->multicast_query_interval; 94690010b36SLinus Lüssing mod_timer(&own_query->timer, time); 947cc0fdd80SLinus Lüssing } 948cc0fdd80SLinus Lüssing 94990010b36SLinus Lüssing static void 95090010b36SLinus Lüssing br_multicast_port_query_expired(struct net_bridge_port *port, 95190010b36SLinus Lüssing struct bridge_mcast_own_query *query) 952eb1d1641SHerbert Xu { 953eb1d1641SHerbert Xu struct net_bridge *br = port->br; 954eb1d1641SHerbert Xu 955eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 95602a780c0SDan Carpenter if (port->state == BR_STATE_DISABLED || 95702a780c0SDan Carpenter port->state == BR_STATE_BLOCKING) 958eb1d1641SHerbert Xu goto out; 959eb1d1641SHerbert Xu 960cc0fdd80SLinus Lüssing if (query->startup_sent < br->multicast_startup_query_count) 961cc0fdd80SLinus Lüssing query->startup_sent++; 962eb1d1641SHerbert Xu 963cc0fdd80SLinus Lüssing br_multicast_send_query(port->br, port, query); 964eb1d1641SHerbert Xu 965eb1d1641SHerbert Xu out: 966eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 967eb1d1641SHerbert Xu } 968eb1d1641SHerbert Xu 969cc0fdd80SLinus Lüssing static void br_ip4_multicast_port_query_expired(unsigned long data) 970cc0fdd80SLinus Lüssing { 971cc0fdd80SLinus Lüssing struct net_bridge_port *port = (void *)data; 972cc0fdd80SLinus Lüssing 97390010b36SLinus Lüssing br_multicast_port_query_expired(port, &port->ip4_own_query); 974cc0fdd80SLinus Lüssing } 975cc0fdd80SLinus Lüssing 976cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 977cc0fdd80SLinus Lüssing static void br_ip6_multicast_port_query_expired(unsigned long data) 978cc0fdd80SLinus Lüssing { 979cc0fdd80SLinus Lüssing struct net_bridge_port *port = (void *)data; 980cc0fdd80SLinus Lüssing 98190010b36SLinus Lüssing br_multicast_port_query_expired(port, &port->ip6_own_query); 982cc0fdd80SLinus Lüssing } 983cc0fdd80SLinus Lüssing #endif 984cc0fdd80SLinus Lüssing 9851080ab95SNikolay Aleksandrov int br_multicast_add_port(struct net_bridge_port *port) 986eb1d1641SHerbert Xu { 9877f0aec7aSNikolay Aleksandrov port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 988eb1d1641SHerbert Xu 989eb1d1641SHerbert Xu setup_timer(&port->multicast_router_timer, br_multicast_router_expired, 990eb1d1641SHerbert Xu (unsigned long)port); 99190010b36SLinus Lüssing setup_timer(&port->ip4_own_query.timer, 99290010b36SLinus Lüssing br_ip4_multicast_port_query_expired, (unsigned long)port); 993cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 99490010b36SLinus Lüssing setup_timer(&port->ip6_own_query.timer, 99590010b36SLinus Lüssing br_ip6_multicast_port_query_expired, (unsigned long)port); 996cc0fdd80SLinus Lüssing #endif 9971080ab95SNikolay Aleksandrov port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats); 9981080ab95SNikolay Aleksandrov if (!port->mcast_stats) 9991080ab95SNikolay Aleksandrov return -ENOMEM; 10001080ab95SNikolay Aleksandrov 10011080ab95SNikolay Aleksandrov return 0; 1002eb1d1641SHerbert Xu } 1003eb1d1641SHerbert Xu 1004eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port) 1005eb1d1641SHerbert Xu { 1006e10177abSSatish Ashok struct net_bridge *br = port->br; 1007e10177abSSatish Ashok struct net_bridge_port_group *pg; 1008e10177abSSatish Ashok struct hlist_node *n; 1009e10177abSSatish Ashok 1010e10177abSSatish Ashok /* Take care of the remaining groups, only perm ones should be left */ 1011e10177abSSatish Ashok spin_lock_bh(&br->multicast_lock); 1012e10177abSSatish Ashok hlist_for_each_entry_safe(pg, n, &port->mglist, mglist) 1013e10177abSSatish Ashok br_multicast_del_pg(br, pg); 1014e10177abSSatish Ashok spin_unlock_bh(&br->multicast_lock); 1015eb1d1641SHerbert Xu del_timer_sync(&port->multicast_router_timer); 10161080ab95SNikolay Aleksandrov free_percpu(port->mcast_stats); 1017eb1d1641SHerbert Xu } 1018eb1d1641SHerbert Xu 101990010b36SLinus Lüssing static void br_multicast_enable(struct bridge_mcast_own_query *query) 1020561f1103SHerbert Xu { 1021cc0fdd80SLinus Lüssing query->startup_sent = 0; 1022561f1103SHerbert Xu 1023cc0fdd80SLinus Lüssing if (try_to_del_timer_sync(&query->timer) >= 0 || 1024cc0fdd80SLinus Lüssing del_timer(&query->timer)) 1025cc0fdd80SLinus Lüssing mod_timer(&query->timer, jiffies); 1026561f1103SHerbert Xu } 1027561f1103SHerbert Xu 10287cb3f921SNikolay Aleksandrov static void __br_multicast_enable_port(struct net_bridge_port *port) 1029eb1d1641SHerbert Xu { 1030eb1d1641SHerbert Xu struct net_bridge *br = port->br; 1031eb1d1641SHerbert Xu 1032eb1d1641SHerbert Xu if (br->multicast_disabled || !netif_running(br->dev)) 10337cb3f921SNikolay Aleksandrov return; 1034eb1d1641SHerbert Xu 103590010b36SLinus Lüssing br_multicast_enable(&port->ip4_own_query); 1036cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 103790010b36SLinus Lüssing br_multicast_enable(&port->ip6_own_query); 1038cc0fdd80SLinus Lüssing #endif 10397f0aec7aSNikolay Aleksandrov if (port->multicast_router == MDB_RTR_TYPE_PERM && 10407f0aec7aSNikolay Aleksandrov hlist_unhashed(&port->rlist)) 1041754bc547SSatish Ashok br_multicast_add_router(br, port); 10427cb3f921SNikolay Aleksandrov } 1043eb1d1641SHerbert Xu 10447cb3f921SNikolay Aleksandrov void br_multicast_enable_port(struct net_bridge_port *port) 10457cb3f921SNikolay Aleksandrov { 10467cb3f921SNikolay Aleksandrov struct net_bridge *br = port->br; 10477cb3f921SNikolay Aleksandrov 10487cb3f921SNikolay Aleksandrov spin_lock(&br->multicast_lock); 10497cb3f921SNikolay Aleksandrov __br_multicast_enable_port(port); 1050eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1051eb1d1641SHerbert Xu } 1052eb1d1641SHerbert Xu 1053eb1d1641SHerbert Xu void br_multicast_disable_port(struct net_bridge_port *port) 1054eb1d1641SHerbert Xu { 1055eb1d1641SHerbert Xu struct net_bridge *br = port->br; 1056eb1d1641SHerbert Xu struct net_bridge_port_group *pg; 1057b67bfe0dSSasha Levin struct hlist_node *n; 1058eb1d1641SHerbert Xu 1059eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1060b67bfe0dSSasha Levin hlist_for_each_entry_safe(pg, n, &port->mglist, mglist) 10619d06b6d8SElad Raz if (!(pg->flags & MDB_PG_FLAGS_PERMANENT)) 1062eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 1063eb1d1641SHerbert Xu 1064949f1e39SSatish Ashok if (!hlist_unhashed(&port->rlist)) { 1065eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 1066949f1e39SSatish Ashok br_rtr_notify(br->dev, port, RTM_DELMDB); 1067a55d8246SNikolay Aleksandrov /* Don't allow timer refresh if disabling */ 1068a55d8246SNikolay Aleksandrov if (port->multicast_router == MDB_RTR_TYPE_TEMP) 1069a55d8246SNikolay Aleksandrov port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 1070949f1e39SSatish Ashok } 1071eb1d1641SHerbert Xu del_timer(&port->multicast_router_timer); 107290010b36SLinus Lüssing del_timer(&port->ip4_own_query.timer); 1073cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 107490010b36SLinus Lüssing del_timer(&port->ip6_own_query.timer); 1075cc0fdd80SLinus Lüssing #endif 1076eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1077eb1d1641SHerbert Xu } 1078eb1d1641SHerbert Xu 10798ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_igmp3_report(struct net_bridge *br, 1080eb1d1641SHerbert Xu struct net_bridge_port *port, 108106499098SVlad Yasevich struct sk_buff *skb, 108206499098SVlad Yasevich u16 vid) 1083eb1d1641SHerbert Xu { 1084eb1d1641SHerbert Xu struct igmpv3_report *ih; 1085eb1d1641SHerbert Xu struct igmpv3_grec *grec; 1086eb1d1641SHerbert Xu int i; 1087eb1d1641SHerbert Xu int len; 1088eb1d1641SHerbert Xu int num; 1089eb1d1641SHerbert Xu int type; 1090eb1d1641SHerbert Xu int err = 0; 1091eb1d1641SHerbert Xu __be32 group; 1092eb1d1641SHerbert Xu 1093eb1d1641SHerbert Xu ih = igmpv3_report_hdr(skb); 1094eb1d1641SHerbert Xu num = ntohs(ih->ngrec); 1095c2d4fbd2SLinus Lüssing len = skb_transport_offset(skb) + sizeof(*ih); 1096eb1d1641SHerbert Xu 1097eb1d1641SHerbert Xu for (i = 0; i < num; i++) { 1098eb1d1641SHerbert Xu len += sizeof(*grec); 1099eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 1100eb1d1641SHerbert Xu return -EINVAL; 1101eb1d1641SHerbert Xu 1102fd218cf9SHerbert Xu grec = (void *)(skb->data + len - sizeof(*grec)); 1103eb1d1641SHerbert Xu group = grec->grec_mca; 1104eb1d1641SHerbert Xu type = grec->grec_type; 1105eb1d1641SHerbert Xu 11068eabf95cSEric Dumazet len += ntohs(grec->grec_nsrcs) * 4; 1107eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 1108eb1d1641SHerbert Xu return -EINVAL; 1109eb1d1641SHerbert Xu 1110eb1d1641SHerbert Xu /* We treat this as an IGMPv2 report for now. */ 1111eb1d1641SHerbert Xu switch (type) { 1112eb1d1641SHerbert Xu case IGMPV3_MODE_IS_INCLUDE: 1113eb1d1641SHerbert Xu case IGMPV3_MODE_IS_EXCLUDE: 1114eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_INCLUDE: 1115eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_EXCLUDE: 1116eb1d1641SHerbert Xu case IGMPV3_ALLOW_NEW_SOURCES: 1117eb1d1641SHerbert Xu case IGMPV3_BLOCK_OLD_SOURCES: 1118eb1d1641SHerbert Xu break; 1119eb1d1641SHerbert Xu 1120eb1d1641SHerbert Xu default: 1121eb1d1641SHerbert Xu continue; 1122eb1d1641SHerbert Xu } 1123eb1d1641SHerbert Xu 1124bc8c20acSSatish Ashok if ((type == IGMPV3_CHANGE_TO_INCLUDE || 1125bc8c20acSSatish Ashok type == IGMPV3_MODE_IS_INCLUDE) && 1126bc8c20acSSatish Ashok ntohs(grec->grec_nsrcs) == 0) { 1127bc8c20acSSatish Ashok br_ip4_multicast_leave_group(br, port, group, vid); 1128bc8c20acSSatish Ashok } else { 1129b0e9a30dSVlad Yasevich err = br_ip4_multicast_add_group(br, port, group, vid); 1130eb1d1641SHerbert Xu if (err) 1131eb1d1641SHerbert Xu break; 1132eb1d1641SHerbert Xu } 1133bc8c20acSSatish Ashok } 1134eb1d1641SHerbert Xu 1135eb1d1641SHerbert Xu return err; 1136eb1d1641SHerbert Xu } 1137eb1d1641SHerbert Xu 1138dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 113908b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_mld2_report(struct net_bridge *br, 114008b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 114106499098SVlad Yasevich struct sk_buff *skb, 114206499098SVlad Yasevich u16 vid) 114308b202b6SYOSHIFUJI Hideaki { 114408b202b6SYOSHIFUJI Hideaki struct icmp6hdr *icmp6h; 114508b202b6SYOSHIFUJI Hideaki struct mld2_grec *grec; 114608b202b6SYOSHIFUJI Hideaki int i; 114708b202b6SYOSHIFUJI Hideaki int len; 114808b202b6SYOSHIFUJI Hideaki int num; 114908b202b6SYOSHIFUJI Hideaki int err = 0; 115008b202b6SYOSHIFUJI Hideaki 115108b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*icmp6h))) 115208b202b6SYOSHIFUJI Hideaki return -EINVAL; 115308b202b6SYOSHIFUJI Hideaki 115408b202b6SYOSHIFUJI Hideaki icmp6h = icmp6_hdr(skb); 115508b202b6SYOSHIFUJI Hideaki num = ntohs(icmp6h->icmp6_dataun.un_data16[1]); 1156c2d4fbd2SLinus Lüssing len = skb_transport_offset(skb) + sizeof(*icmp6h); 115708b202b6SYOSHIFUJI Hideaki 115808b202b6SYOSHIFUJI Hideaki for (i = 0; i < num; i++) { 115908b202b6SYOSHIFUJI Hideaki __be16 *nsrcs, _nsrcs; 116008b202b6SYOSHIFUJI Hideaki 116108b202b6SYOSHIFUJI Hideaki nsrcs = skb_header_pointer(skb, 116208b202b6SYOSHIFUJI Hideaki len + offsetof(struct mld2_grec, 1163649e984dSLinus Lüssing grec_nsrcs), 116408b202b6SYOSHIFUJI Hideaki sizeof(_nsrcs), &_nsrcs); 116508b202b6SYOSHIFUJI Hideaki if (!nsrcs) 116608b202b6SYOSHIFUJI Hideaki return -EINVAL; 116708b202b6SYOSHIFUJI Hideaki 116808b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, 116908b202b6SYOSHIFUJI Hideaki len + sizeof(*grec) + 1170d41db9f3SLinus Lüssing sizeof(struct in6_addr) * ntohs(*nsrcs))) 117108b202b6SYOSHIFUJI Hideaki return -EINVAL; 117208b202b6SYOSHIFUJI Hideaki 117308b202b6SYOSHIFUJI Hideaki grec = (struct mld2_grec *)(skb->data + len); 1174d41db9f3SLinus Lüssing len += sizeof(*grec) + 1175d41db9f3SLinus Lüssing sizeof(struct in6_addr) * ntohs(*nsrcs); 117608b202b6SYOSHIFUJI Hideaki 117708b202b6SYOSHIFUJI Hideaki /* We treat these as MLDv1 reports for now. */ 117808b202b6SYOSHIFUJI Hideaki switch (grec->grec_type) { 117908b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_INCLUDE: 118008b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_EXCLUDE: 118108b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_INCLUDE: 118208b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_EXCLUDE: 118308b202b6SYOSHIFUJI Hideaki case MLD2_ALLOW_NEW_SOURCES: 118408b202b6SYOSHIFUJI Hideaki case MLD2_BLOCK_OLD_SOURCES: 118508b202b6SYOSHIFUJI Hideaki break; 118608b202b6SYOSHIFUJI Hideaki 118708b202b6SYOSHIFUJI Hideaki default: 118808b202b6SYOSHIFUJI Hideaki continue; 118908b202b6SYOSHIFUJI Hideaki } 119008b202b6SYOSHIFUJI Hideaki 1191bc8c20acSSatish Ashok if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE || 1192bc8c20acSSatish Ashok grec->grec_type == MLD2_MODE_IS_INCLUDE) && 1193bc8c20acSSatish Ashok ntohs(*nsrcs) == 0) { 1194bc8c20acSSatish Ashok br_ip6_multicast_leave_group(br, port, &grec->grec_mca, 1195b0e9a30dSVlad Yasevich vid); 1196bc8c20acSSatish Ashok } else { 1197bc8c20acSSatish Ashok err = br_ip6_multicast_add_group(br, port, 1198bc8c20acSSatish Ashok &grec->grec_mca, vid); 11999264251eSDavide Caratti if (err) 120008b202b6SYOSHIFUJI Hideaki break; 120108b202b6SYOSHIFUJI Hideaki } 1202bc8c20acSSatish Ashok } 120308b202b6SYOSHIFUJI Hideaki 120408b202b6SYOSHIFUJI Hideaki return err; 120508b202b6SYOSHIFUJI Hideaki } 120608b202b6SYOSHIFUJI Hideaki #endif 120708b202b6SYOSHIFUJI Hideaki 1208dc4eb53aSLinus Lüssing static bool br_ip4_multicast_select_querier(struct net_bridge *br, 12092cd41431SLinus Lüssing struct net_bridge_port *port, 1210dc4eb53aSLinus Lüssing __be32 saddr) 1211dc4eb53aSLinus Lüssing { 1212dc4eb53aSLinus Lüssing if (!timer_pending(&br->ip4_own_query.timer) && 1213dc4eb53aSLinus Lüssing !timer_pending(&br->ip4_other_query.timer)) 1214dc4eb53aSLinus Lüssing goto update; 1215dc4eb53aSLinus Lüssing 1216dc4eb53aSLinus Lüssing if (!br->ip4_querier.addr.u.ip4) 1217dc4eb53aSLinus Lüssing goto update; 1218dc4eb53aSLinus Lüssing 1219dc4eb53aSLinus Lüssing if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4)) 1220dc4eb53aSLinus Lüssing goto update; 1221dc4eb53aSLinus Lüssing 1222dc4eb53aSLinus Lüssing return false; 1223dc4eb53aSLinus Lüssing 1224dc4eb53aSLinus Lüssing update: 1225dc4eb53aSLinus Lüssing br->ip4_querier.addr.u.ip4 = saddr; 1226dc4eb53aSLinus Lüssing 12272cd41431SLinus Lüssing /* update protected by general multicast_lock by caller */ 12282cd41431SLinus Lüssing rcu_assign_pointer(br->ip4_querier.port, port); 12292cd41431SLinus Lüssing 1230dc4eb53aSLinus Lüssing return true; 1231dc4eb53aSLinus Lüssing } 1232dc4eb53aSLinus Lüssing 1233dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1234dc4eb53aSLinus Lüssing static bool br_ip6_multicast_select_querier(struct net_bridge *br, 12352cd41431SLinus Lüssing struct net_bridge_port *port, 1236dc4eb53aSLinus Lüssing struct in6_addr *saddr) 1237dc4eb53aSLinus Lüssing { 1238dc4eb53aSLinus Lüssing if (!timer_pending(&br->ip6_own_query.timer) && 1239dc4eb53aSLinus Lüssing !timer_pending(&br->ip6_other_query.timer)) 1240dc4eb53aSLinus Lüssing goto update; 1241dc4eb53aSLinus Lüssing 1242dc4eb53aSLinus Lüssing if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0) 1243dc4eb53aSLinus Lüssing goto update; 1244dc4eb53aSLinus Lüssing 1245dc4eb53aSLinus Lüssing return false; 1246dc4eb53aSLinus Lüssing 1247dc4eb53aSLinus Lüssing update: 1248dc4eb53aSLinus Lüssing br->ip6_querier.addr.u.ip6 = *saddr; 1249dc4eb53aSLinus Lüssing 12502cd41431SLinus Lüssing /* update protected by general multicast_lock by caller */ 12512cd41431SLinus Lüssing rcu_assign_pointer(br->ip6_querier.port, port); 12522cd41431SLinus Lüssing 1253dc4eb53aSLinus Lüssing return true; 1254dc4eb53aSLinus Lüssing } 1255dc4eb53aSLinus Lüssing #endif 1256dc4eb53aSLinus Lüssing 1257dc4eb53aSLinus Lüssing static bool br_multicast_select_querier(struct net_bridge *br, 12582cd41431SLinus Lüssing struct net_bridge_port *port, 1259dc4eb53aSLinus Lüssing struct br_ip *saddr) 1260dc4eb53aSLinus Lüssing { 1261dc4eb53aSLinus Lüssing switch (saddr->proto) { 1262dc4eb53aSLinus Lüssing case htons(ETH_P_IP): 12632cd41431SLinus Lüssing return br_ip4_multicast_select_querier(br, port, saddr->u.ip4); 1264dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1265dc4eb53aSLinus Lüssing case htons(ETH_P_IPV6): 12662cd41431SLinus Lüssing return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6); 1267dc4eb53aSLinus Lüssing #endif 1268dc4eb53aSLinus Lüssing } 1269dc4eb53aSLinus Lüssing 1270dc4eb53aSLinus Lüssing return false; 1271dc4eb53aSLinus Lüssing } 1272dc4eb53aSLinus Lüssing 1273cc0fdd80SLinus Lüssing static void 127490010b36SLinus Lüssing br_multicast_update_query_timer(struct net_bridge *br, 127590010b36SLinus Lüssing struct bridge_mcast_other_query *query, 1276b00589afSLinus Lüssing unsigned long max_delay) 1277b00589afSLinus Lüssing { 127890010b36SLinus Lüssing if (!timer_pending(&query->timer)) 127990010b36SLinus Lüssing query->delay_time = jiffies + max_delay; 1280b00589afSLinus Lüssing 128190010b36SLinus Lüssing mod_timer(&query->timer, jiffies + br->multicast_querier_interval); 1282b00589afSLinus Lüssing } 1283b00589afSLinus Lüssing 12847e80c124Sstephen hemminger /* 12857c77602fSCong Wang * Add port to router_list 12867e80c124Sstephen hemminger * list is maintained ordered by pointer value 12877e80c124Sstephen hemminger * and locked by br->multicast_lock and RCU 12887e80c124Sstephen hemminger */ 12890909e117SHerbert Xu static void br_multicast_add_router(struct net_bridge *br, 12900909e117SHerbert Xu struct net_bridge_port *port) 12910909e117SHerbert Xu { 1292dcdca2c4Sstephen hemminger struct net_bridge_port *p; 1293b67bfe0dSSasha Levin struct hlist_node *slot = NULL; 12940909e117SHerbert Xu 12951a040eacSNikolay Aleksandrov if (!hlist_unhashed(&port->rlist)) 12961a040eacSNikolay Aleksandrov return; 12971a040eacSNikolay Aleksandrov 1298b67bfe0dSSasha Levin hlist_for_each_entry(p, &br->router_list, rlist) { 12997e80c124Sstephen hemminger if ((unsigned long) port >= (unsigned long) p) 13007e80c124Sstephen hemminger break; 1301b67bfe0dSSasha Levin slot = &p->rlist; 1302dcdca2c4Sstephen hemminger } 13030909e117SHerbert Xu 13047e80c124Sstephen hemminger if (slot) 13051d023284SKen Helias hlist_add_behind_rcu(&port->rlist, slot); 1306dcdca2c4Sstephen hemminger else 1307dcdca2c4Sstephen hemminger hlist_add_head_rcu(&port->rlist, &br->router_list); 1308949f1e39SSatish Ashok br_rtr_notify(br->dev, port, RTM_NEWMDB); 13090909e117SHerbert Xu } 13100909e117SHerbert Xu 1311eb1d1641SHerbert Xu static void br_multicast_mark_router(struct net_bridge *br, 1312eb1d1641SHerbert Xu struct net_bridge_port *port) 1313eb1d1641SHerbert Xu { 1314eb1d1641SHerbert Xu unsigned long now = jiffies; 1315eb1d1641SHerbert Xu 1316eb1d1641SHerbert Xu if (!port) { 13177f0aec7aSNikolay Aleksandrov if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) 1318eb1d1641SHerbert Xu mod_timer(&br->multicast_router_timer, 1319eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1320eb1d1641SHerbert Xu return; 1321eb1d1641SHerbert Xu } 1322eb1d1641SHerbert Xu 1323a55d8246SNikolay Aleksandrov if (port->multicast_router == MDB_RTR_TYPE_DISABLED || 1324a55d8246SNikolay Aleksandrov port->multicast_router == MDB_RTR_TYPE_PERM) 1325eb1d1641SHerbert Xu return; 1326eb1d1641SHerbert Xu 13270909e117SHerbert Xu br_multicast_add_router(br, port); 1328eb1d1641SHerbert Xu 1329eb1d1641SHerbert Xu mod_timer(&port->multicast_router_timer, 1330eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1331eb1d1641SHerbert Xu } 1332eb1d1641SHerbert Xu 1333eb1d1641SHerbert Xu static void br_multicast_query_received(struct net_bridge *br, 1334eb1d1641SHerbert Xu struct net_bridge_port *port, 133590010b36SLinus Lüssing struct bridge_mcast_other_query *query, 1336dc4eb53aSLinus Lüssing struct br_ip *saddr, 1337b00589afSLinus Lüssing unsigned long max_delay) 1338eb1d1641SHerbert Xu { 13392cd41431SLinus Lüssing if (!br_multicast_select_querier(br, port, saddr)) 1340eb1d1641SHerbert Xu return; 1341eb1d1641SHerbert Xu 1342dc4eb53aSLinus Lüssing br_multicast_update_query_timer(br, query, max_delay); 1343eb1d1641SHerbert Xu br_multicast_mark_router(br, port); 1344eb1d1641SHerbert Xu } 1345eb1d1641SHerbert Xu 13468ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_query(struct net_bridge *br, 1347eb1d1641SHerbert Xu struct net_bridge_port *port, 134806499098SVlad Yasevich struct sk_buff *skb, 134906499098SVlad Yasevich u16 vid) 1350eb1d1641SHerbert Xu { 1351b71d1d42SEric Dumazet const struct iphdr *iph = ip_hdr(skb); 1352eb1d1641SHerbert Xu struct igmphdr *ih = igmp_hdr(skb); 1353eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1354eb1d1641SHerbert Xu struct igmpv3_query *ih3; 1355eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1356e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 1357dc4eb53aSLinus Lüssing struct br_ip saddr; 1358eb1d1641SHerbert Xu unsigned long max_delay; 1359eb1d1641SHerbert Xu unsigned long now = jiffies; 1360856ce5d0SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 1361eb1d1641SHerbert Xu __be32 group; 1362bec68ff1SYOSHIFUJI Hideaki int err = 0; 1363eb1d1641SHerbert Xu 1364eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1365eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1366eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 1367eb1d1641SHerbert Xu goto out; 1368eb1d1641SHerbert Xu 1369eb1d1641SHerbert Xu group = ih->group; 1370eb1d1641SHerbert Xu 1371856ce5d0SLinus Lüssing if (skb->len == offset + sizeof(*ih)) { 1372eb1d1641SHerbert Xu max_delay = ih->code * (HZ / IGMP_TIMER_SCALE); 1373eb1d1641SHerbert Xu 1374eb1d1641SHerbert Xu if (!max_delay) { 1375eb1d1641SHerbert Xu max_delay = 10 * HZ; 1376eb1d1641SHerbert Xu group = 0; 1377eb1d1641SHerbert Xu } 1378856ce5d0SLinus Lüssing } else if (skb->len >= offset + sizeof(*ih3)) { 1379eb1d1641SHerbert Xu ih3 = igmpv3_query_hdr(skb); 1380eb1d1641SHerbert Xu if (ih3->nsrcs) 1381bec68ff1SYOSHIFUJI Hideaki goto out; 1382eb1d1641SHerbert Xu 13830ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 max_delay = ih3->code ? 13840ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1; 13859afd85c9SLinus Lüssing } else { 13869ed973ccSLinus Lüssing goto out; 13879ed973ccSLinus Lüssing } 13889ed973ccSLinus Lüssing 1389dc4eb53aSLinus Lüssing if (!group) { 1390dc4eb53aSLinus Lüssing saddr.proto = htons(ETH_P_IP); 1391dc4eb53aSLinus Lüssing saddr.u.ip4 = iph->saddr; 1392b00589afSLinus Lüssing 1393dc4eb53aSLinus Lüssing br_multicast_query_received(br, port, &br->ip4_other_query, 1394dc4eb53aSLinus Lüssing &saddr, max_delay); 1395eb1d1641SHerbert Xu goto out; 1396dc4eb53aSLinus Lüssing } 1397eb1d1641SHerbert Xu 1398b0e9a30dSVlad Yasevich mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid); 1399eb1d1641SHerbert Xu if (!mp) 1400eb1d1641SHerbert Xu goto out; 1401eb1d1641SHerbert Xu 1402eb1d1641SHerbert Xu max_delay *= br->multicast_last_member_count; 1403eb1d1641SHerbert Xu 14048a870178SHerbert Xu if (mp->mglist && 1405eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1406eb1d1641SHerbert Xu time_after(mp->timer.expires, now + max_delay) : 1407eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) 1408eb1d1641SHerbert Xu mod_timer(&mp->timer, now + max_delay); 1409eb1d1641SHerbert Xu 1410e8051688SEric Dumazet for (pp = &mp->ports; 1411e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 1412e8051688SEric Dumazet pp = &p->next) { 1413eb1d1641SHerbert Xu if (timer_pending(&p->timer) ? 1414eb1d1641SHerbert Xu time_after(p->timer.expires, now + max_delay) : 1415eb1d1641SHerbert Xu try_to_del_timer_sync(&p->timer) >= 0) 141624f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 1417eb1d1641SHerbert Xu } 1418eb1d1641SHerbert Xu 1419eb1d1641SHerbert Xu out: 1420eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1421bec68ff1SYOSHIFUJI Hideaki return err; 1422eb1d1641SHerbert Xu } 1423eb1d1641SHerbert Xu 1424dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 142508b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_query(struct net_bridge *br, 142608b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 142706499098SVlad Yasevich struct sk_buff *skb, 142806499098SVlad Yasevich u16 vid) 142908b202b6SYOSHIFUJI Hideaki { 1430b71d1d42SEric Dumazet const struct ipv6hdr *ip6h = ipv6_hdr(skb); 1431eca2a43bSstephen hemminger struct mld_msg *mld; 143208b202b6SYOSHIFUJI Hideaki struct net_bridge_mdb_entry *mp; 143308b202b6SYOSHIFUJI Hideaki struct mld2_query *mld2q; 1434e8051688SEric Dumazet struct net_bridge_port_group *p; 1435e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 1436dc4eb53aSLinus Lüssing struct br_ip saddr; 143708b202b6SYOSHIFUJI Hideaki unsigned long max_delay; 143808b202b6SYOSHIFUJI Hideaki unsigned long now = jiffies; 1439856ce5d0SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 1440b71d1d42SEric Dumazet const struct in6_addr *group = NULL; 14419ed973ccSLinus Lüssing bool is_general_query; 144208b202b6SYOSHIFUJI Hideaki int err = 0; 144308b202b6SYOSHIFUJI Hideaki 144408b202b6SYOSHIFUJI Hideaki spin_lock(&br->multicast_lock); 144508b202b6SYOSHIFUJI Hideaki if (!netif_running(br->dev) || 144608b202b6SYOSHIFUJI Hideaki (port && port->state == BR_STATE_DISABLED)) 144708b202b6SYOSHIFUJI Hideaki goto out; 144808b202b6SYOSHIFUJI Hideaki 1449856ce5d0SLinus Lüssing if (skb->len == offset + sizeof(*mld)) { 1450856ce5d0SLinus Lüssing if (!pskb_may_pull(skb, offset + sizeof(*mld))) { 145108b202b6SYOSHIFUJI Hideaki err = -EINVAL; 145208b202b6SYOSHIFUJI Hideaki goto out; 145308b202b6SYOSHIFUJI Hideaki } 145408b202b6SYOSHIFUJI Hideaki mld = (struct mld_msg *) icmp6_hdr(skb); 14554715213dSLi RongQing max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay)); 145608b202b6SYOSHIFUJI Hideaki if (max_delay) 145708b202b6SYOSHIFUJI Hideaki group = &mld->mld_mca; 1458248ba8ecSLinus Lüssing } else { 1459856ce5d0SLinus Lüssing if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) { 146008b202b6SYOSHIFUJI Hideaki err = -EINVAL; 146108b202b6SYOSHIFUJI Hideaki goto out; 146208b202b6SYOSHIFUJI Hideaki } 146308b202b6SYOSHIFUJI Hideaki mld2q = (struct mld2_query *)icmp6_hdr(skb); 146408b202b6SYOSHIFUJI Hideaki if (!mld2q->mld2q_nsrcs) 146508b202b6SYOSHIFUJI Hideaki group = &mld2q->mld2q_mca; 1466e3f5b170SDaniel Borkmann 1467e3f5b170SDaniel Borkmann max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL); 146808b202b6SYOSHIFUJI Hideaki } 146908b202b6SYOSHIFUJI Hideaki 14709ed973ccSLinus Lüssing is_general_query = group && ipv6_addr_any(group); 14719ed973ccSLinus Lüssing 1472dc4eb53aSLinus Lüssing if (is_general_query) { 1473dc4eb53aSLinus Lüssing saddr.proto = htons(ETH_P_IPV6); 1474dc4eb53aSLinus Lüssing saddr.u.ip6 = ip6h->saddr; 1475b00589afSLinus Lüssing 1476dc4eb53aSLinus Lüssing br_multicast_query_received(br, port, &br->ip6_other_query, 1477dc4eb53aSLinus Lüssing &saddr, max_delay); 147808b202b6SYOSHIFUJI Hideaki goto out; 14796c03ee8bSLinus Lüssing } else if (!group) { 14806c03ee8bSLinus Lüssing goto out; 1481dc4eb53aSLinus Lüssing } 148208b202b6SYOSHIFUJI Hideaki 1483b0e9a30dSVlad Yasevich mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid); 148408b202b6SYOSHIFUJI Hideaki if (!mp) 148508b202b6SYOSHIFUJI Hideaki goto out; 148608b202b6SYOSHIFUJI Hideaki 148708b202b6SYOSHIFUJI Hideaki max_delay *= br->multicast_last_member_count; 14888a870178SHerbert Xu if (mp->mglist && 148908b202b6SYOSHIFUJI Hideaki (timer_pending(&mp->timer) ? 149008b202b6SYOSHIFUJI Hideaki time_after(mp->timer.expires, now + max_delay) : 149108b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&mp->timer) >= 0)) 149208b202b6SYOSHIFUJI Hideaki mod_timer(&mp->timer, now + max_delay); 149308b202b6SYOSHIFUJI Hideaki 1494e8051688SEric Dumazet for (pp = &mp->ports; 1495e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 1496e8051688SEric Dumazet pp = &p->next) { 149708b202b6SYOSHIFUJI Hideaki if (timer_pending(&p->timer) ? 149808b202b6SYOSHIFUJI Hideaki time_after(p->timer.expires, now + max_delay) : 149908b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&p->timer) >= 0) 150024f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 150108b202b6SYOSHIFUJI Hideaki } 150208b202b6SYOSHIFUJI Hideaki 150308b202b6SYOSHIFUJI Hideaki out: 150408b202b6SYOSHIFUJI Hideaki spin_unlock(&br->multicast_lock); 150508b202b6SYOSHIFUJI Hideaki return err; 150608b202b6SYOSHIFUJI Hideaki } 150708b202b6SYOSHIFUJI Hideaki #endif 150808b202b6SYOSHIFUJI Hideaki 150990010b36SLinus Lüssing static void 151090010b36SLinus Lüssing br_multicast_leave_group(struct net_bridge *br, 1511eb1d1641SHerbert Xu struct net_bridge_port *port, 1512cc0fdd80SLinus Lüssing struct br_ip *group, 151390010b36SLinus Lüssing struct bridge_mcast_other_query *other_query, 151490010b36SLinus Lüssing struct bridge_mcast_own_query *own_query) 1515eb1d1641SHerbert Xu { 1516eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 1517eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1518eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1519eb1d1641SHerbert Xu unsigned long now; 1520eb1d1641SHerbert Xu unsigned long time; 1521eb1d1641SHerbert Xu 1522eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1523eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1524544586f7SSatish Ashok (port && port->state == BR_STATE_DISABLED)) 1525eb1d1641SHerbert Xu goto out; 1526eb1d1641SHerbert Xu 1527e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1528eb1d1641SHerbert Xu mp = br_mdb_ip_get(mdb, group); 1529eb1d1641SHerbert Xu if (!mp) 1530eb1d1641SHerbert Xu goto out; 1531eb1d1641SHerbert Xu 1532544586f7SSatish Ashok if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) { 1533544586f7SSatish Ashok struct net_bridge_port_group __rcu **pp; 1534544586f7SSatish Ashok 1535544586f7SSatish Ashok for (pp = &mp->ports; 1536544586f7SSatish Ashok (p = mlock_dereference(*pp, br)) != NULL; 1537544586f7SSatish Ashok pp = &p->next) { 1538544586f7SSatish Ashok if (p->port != port) 1539544586f7SSatish Ashok continue; 1540544586f7SSatish Ashok 1541544586f7SSatish Ashok rcu_assign_pointer(*pp, p->next); 1542544586f7SSatish Ashok hlist_del_init(&p->mglist); 1543544586f7SSatish Ashok del_timer(&p->timer); 1544544586f7SSatish Ashok call_rcu_bh(&p->rcu, br_multicast_free_pg); 154545ebcce5SElad Raz br_mdb_notify(br->dev, port, group, RTM_DELMDB, 154645ebcce5SElad Raz p->flags); 1547544586f7SSatish Ashok 1548544586f7SSatish Ashok if (!mp->ports && !mp->mglist && 1549544586f7SSatish Ashok netif_running(br->dev)) 1550544586f7SSatish Ashok mod_timer(&mp->timer, jiffies); 1551544586f7SSatish Ashok } 1552544586f7SSatish Ashok goto out; 1553544586f7SSatish Ashok } 1554544586f7SSatish Ashok 1555544586f7SSatish Ashok if (timer_pending(&other_query->timer)) 1556544586f7SSatish Ashok goto out; 1557544586f7SSatish Ashok 1558cc0fdd80SLinus Lüssing if (br->multicast_querier) { 15596b7df111SCong Wang __br_multicast_send_query(br, port, &mp->addr); 15606b7df111SCong Wang 15616b7df111SCong Wang time = jiffies + br->multicast_last_member_count * 15626b7df111SCong Wang br->multicast_last_member_interval; 1563cc0fdd80SLinus Lüssing 156490010b36SLinus Lüssing mod_timer(&own_query->timer, time); 15656b7df111SCong Wang 15666b7df111SCong Wang for (p = mlock_dereference(mp->ports, br); 15676b7df111SCong Wang p != NULL; 15686b7df111SCong Wang p = mlock_dereference(p->next, br)) { 15696b7df111SCong Wang if (p->port != port) 15706b7df111SCong Wang continue; 15716b7df111SCong Wang 15726b7df111SCong Wang if (!hlist_unhashed(&p->mglist) && 15736b7df111SCong Wang (timer_pending(&p->timer) ? 15746b7df111SCong Wang time_after(p->timer.expires, time) : 15756b7df111SCong Wang try_to_del_timer_sync(&p->timer) >= 0)) { 15766b7df111SCong Wang mod_timer(&p->timer, time); 15776b7df111SCong Wang } 15786b7df111SCong Wang 15796b7df111SCong Wang break; 15806b7df111SCong Wang } 15816b7df111SCong Wang } 15826b7df111SCong Wang 1583eb1d1641SHerbert Xu now = jiffies; 1584eb1d1641SHerbert Xu time = now + br->multicast_last_member_count * 1585eb1d1641SHerbert Xu br->multicast_last_member_interval; 1586eb1d1641SHerbert Xu 1587eb1d1641SHerbert Xu if (!port) { 1588454594f3SLinus Lüssing if (mp->mglist && 1589eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1590eb1d1641SHerbert Xu time_after(mp->timer.expires, time) : 1591eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) { 1592eb1d1641SHerbert Xu mod_timer(&mp->timer, time); 1593eb1d1641SHerbert Xu } 1594454594f3SLinus Lüssing 1595454594f3SLinus Lüssing goto out; 1596454594f3SLinus Lüssing } 1597454594f3SLinus Lüssing 1598454594f3SLinus Lüssing for (p = mlock_dereference(mp->ports, br); 1599454594f3SLinus Lüssing p != NULL; 1600454594f3SLinus Lüssing p = mlock_dereference(p->next, br)) { 1601454594f3SLinus Lüssing if (p->port != port) 1602454594f3SLinus Lüssing continue; 1603454594f3SLinus Lüssing 1604454594f3SLinus Lüssing if (!hlist_unhashed(&p->mglist) && 1605454594f3SLinus Lüssing (timer_pending(&p->timer) ? 1606454594f3SLinus Lüssing time_after(p->timer.expires, time) : 1607454594f3SLinus Lüssing try_to_del_timer_sync(&p->timer) >= 0)) { 1608454594f3SLinus Lüssing mod_timer(&p->timer, time); 1609454594f3SLinus Lüssing } 1610454594f3SLinus Lüssing 1611454594f3SLinus Lüssing break; 1612eb1d1641SHerbert Xu } 1613eb1d1641SHerbert Xu out: 1614eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1615eb1d1641SHerbert Xu } 1616eb1d1641SHerbert Xu 16178ef2a9a5SYOSHIFUJI Hideaki static void br_ip4_multicast_leave_group(struct net_bridge *br, 16188ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 1619b0e9a30dSVlad Yasevich __be32 group, 1620b0e9a30dSVlad Yasevich __u16 vid) 16218ef2a9a5SYOSHIFUJI Hideaki { 16228ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 162390010b36SLinus Lüssing struct bridge_mcast_own_query *own_query; 16248ef2a9a5SYOSHIFUJI Hideaki 16258ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 16268ef2a9a5SYOSHIFUJI Hideaki return; 16278ef2a9a5SYOSHIFUJI Hideaki 162890010b36SLinus Lüssing own_query = port ? &port->ip4_own_query : &br->ip4_own_query; 162990010b36SLinus Lüssing 16308ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 16318ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 1632b0e9a30dSVlad Yasevich br_group.vid = vid; 16338ef2a9a5SYOSHIFUJI Hideaki 163490010b36SLinus Lüssing br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query, 163590010b36SLinus Lüssing own_query); 16368ef2a9a5SYOSHIFUJI Hideaki } 16378ef2a9a5SYOSHIFUJI Hideaki 1638dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 163908b202b6SYOSHIFUJI Hideaki static void br_ip6_multicast_leave_group(struct net_bridge *br, 164008b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 1641b0e9a30dSVlad Yasevich const struct in6_addr *group, 1642b0e9a30dSVlad Yasevich __u16 vid) 164308b202b6SYOSHIFUJI Hideaki { 164408b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 164590010b36SLinus Lüssing struct bridge_mcast_own_query *own_query; 164608b202b6SYOSHIFUJI Hideaki 16473c3769e6SLinus Lüssing if (ipv6_addr_is_ll_all_nodes(group)) 164808b202b6SYOSHIFUJI Hideaki return; 164908b202b6SYOSHIFUJI Hideaki 165090010b36SLinus Lüssing own_query = port ? &port->ip6_own_query : &br->ip6_own_query; 165190010b36SLinus Lüssing 16524e3fd7a0SAlexey Dobriyan br_group.u.ip6 = *group; 165308b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 1654b0e9a30dSVlad Yasevich br_group.vid = vid; 165508b202b6SYOSHIFUJI Hideaki 165690010b36SLinus Lüssing br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query, 165790010b36SLinus Lüssing own_query); 165808b202b6SYOSHIFUJI Hideaki } 165908b202b6SYOSHIFUJI Hideaki #endif 16608ef2a9a5SYOSHIFUJI Hideaki 16611080ab95SNikolay Aleksandrov static void br_multicast_err_count(const struct net_bridge *br, 16621080ab95SNikolay Aleksandrov const struct net_bridge_port *p, 16631080ab95SNikolay Aleksandrov __be16 proto) 16641080ab95SNikolay Aleksandrov { 16651080ab95SNikolay Aleksandrov struct bridge_mcast_stats __percpu *stats; 16661080ab95SNikolay Aleksandrov struct bridge_mcast_stats *pstats; 16671080ab95SNikolay Aleksandrov 16681080ab95SNikolay Aleksandrov if (!br->multicast_stats_enabled) 16691080ab95SNikolay Aleksandrov return; 16701080ab95SNikolay Aleksandrov 16711080ab95SNikolay Aleksandrov if (p) 16721080ab95SNikolay Aleksandrov stats = p->mcast_stats; 16731080ab95SNikolay Aleksandrov else 16741080ab95SNikolay Aleksandrov stats = br->mcast_stats; 16751080ab95SNikolay Aleksandrov if (WARN_ON(!stats)) 16761080ab95SNikolay Aleksandrov return; 16771080ab95SNikolay Aleksandrov 16781080ab95SNikolay Aleksandrov pstats = this_cpu_ptr(stats); 16791080ab95SNikolay Aleksandrov 16801080ab95SNikolay Aleksandrov u64_stats_update_begin(&pstats->syncp); 16811080ab95SNikolay Aleksandrov switch (proto) { 16821080ab95SNikolay Aleksandrov case htons(ETH_P_IP): 16831080ab95SNikolay Aleksandrov pstats->mstats.igmp_parse_errors++; 16841080ab95SNikolay Aleksandrov break; 16851080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 16861080ab95SNikolay Aleksandrov case htons(ETH_P_IPV6): 16871080ab95SNikolay Aleksandrov pstats->mstats.mld_parse_errors++; 16881080ab95SNikolay Aleksandrov break; 16891080ab95SNikolay Aleksandrov #endif 16901080ab95SNikolay Aleksandrov } 16911080ab95SNikolay Aleksandrov u64_stats_update_end(&pstats->syncp); 16921080ab95SNikolay Aleksandrov } 16931080ab95SNikolay Aleksandrov 169491b02d3dSNikolay Aleksandrov static void br_multicast_pim(struct net_bridge *br, 169591b02d3dSNikolay Aleksandrov struct net_bridge_port *port, 169691b02d3dSNikolay Aleksandrov const struct sk_buff *skb) 169791b02d3dSNikolay Aleksandrov { 169891b02d3dSNikolay Aleksandrov unsigned int offset = skb_transport_offset(skb); 169991b02d3dSNikolay Aleksandrov struct pimhdr *pimhdr, _pimhdr; 170091b02d3dSNikolay Aleksandrov 170191b02d3dSNikolay Aleksandrov pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr); 170291b02d3dSNikolay Aleksandrov if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION || 170391b02d3dSNikolay Aleksandrov pim_hdr_type(pimhdr) != PIM_TYPE_HELLO) 170491b02d3dSNikolay Aleksandrov return; 170591b02d3dSNikolay Aleksandrov 170691b02d3dSNikolay Aleksandrov br_multicast_mark_router(br, port); 170791b02d3dSNikolay Aleksandrov } 170891b02d3dSNikolay Aleksandrov 1709eb1d1641SHerbert Xu static int br_multicast_ipv4_rcv(struct net_bridge *br, 1710eb1d1641SHerbert Xu struct net_bridge_port *port, 171106499098SVlad Yasevich struct sk_buff *skb, 171206499098SVlad Yasevich u16 vid) 1713eb1d1641SHerbert Xu { 17149afd85c9SLinus Lüssing struct sk_buff *skb_trimmed = NULL; 1715eb1d1641SHerbert Xu struct igmphdr *ih; 1716eb1d1641SHerbert Xu int err; 1717eb1d1641SHerbert Xu 17189afd85c9SLinus Lüssing err = ip_mc_check_igmp(skb, &skb_trimmed); 1719eb1d1641SHerbert Xu 17209afd85c9SLinus Lüssing if (err == -ENOMSG) { 172191b02d3dSNikolay Aleksandrov if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) { 1722bd4265feSHerbert Xu BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 172391b02d3dSNikolay Aleksandrov } else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) { 172491b02d3dSNikolay Aleksandrov if (ip_hdr(skb)->protocol == IPPROTO_PIM) 172591b02d3dSNikolay Aleksandrov br_multicast_pim(br, port, skb); 172691b02d3dSNikolay Aleksandrov } 1727eb1d1641SHerbert Xu return 0; 17289afd85c9SLinus Lüssing } else if (err < 0) { 17291080ab95SNikolay Aleksandrov br_multicast_err_count(br, port, skb->protocol); 17309afd85c9SLinus Lüssing return err; 1731bd4265feSHerbert Xu } 1732eb1d1641SHerbert Xu 17339afd85c9SLinus Lüssing ih = igmp_hdr(skb); 17341080ab95SNikolay Aleksandrov BR_INPUT_SKB_CB(skb)->igmp = ih->type; 1735eb1d1641SHerbert Xu 1736eb1d1641SHerbert Xu switch (ih->type) { 1737eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_REPORT: 1738eb1d1641SHerbert Xu case IGMPV2_HOST_MEMBERSHIP_REPORT: 173962b2bcb4SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 1740b0e9a30dSVlad Yasevich err = br_ip4_multicast_add_group(br, port, ih->group, vid); 1741eb1d1641SHerbert Xu break; 1742eb1d1641SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 17439afd85c9SLinus Lüssing err = br_ip4_multicast_igmp3_report(br, port, skb_trimmed, vid); 1744eb1d1641SHerbert Xu break; 1745eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_QUERY: 17469afd85c9SLinus Lüssing err = br_ip4_multicast_query(br, port, skb_trimmed, vid); 1747eb1d1641SHerbert Xu break; 1748eb1d1641SHerbert Xu case IGMP_HOST_LEAVE_MESSAGE: 1749b0e9a30dSVlad Yasevich br_ip4_multicast_leave_group(br, port, ih->group, vid); 1750eb1d1641SHerbert Xu break; 1751eb1d1641SHerbert Xu } 1752eb1d1641SHerbert Xu 1753a516993fSLinus Lüssing if (skb_trimmed && skb_trimmed != skb) 17549afd85c9SLinus Lüssing kfree_skb(skb_trimmed); 17559afd85c9SLinus Lüssing 1756a65056ecSNikolay Aleksandrov br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp, 17571080ab95SNikolay Aleksandrov BR_MCAST_DIR_RX); 17581080ab95SNikolay Aleksandrov 1759eb1d1641SHerbert Xu return err; 1760eb1d1641SHerbert Xu } 1761eb1d1641SHerbert Xu 1762dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 176308b202b6SYOSHIFUJI Hideaki static int br_multicast_ipv6_rcv(struct net_bridge *br, 176408b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 176506499098SVlad Yasevich struct sk_buff *skb, 176606499098SVlad Yasevich u16 vid) 176708b202b6SYOSHIFUJI Hideaki { 17689afd85c9SLinus Lüssing struct sk_buff *skb_trimmed = NULL; 17699afd85c9SLinus Lüssing struct mld_msg *mld; 177008b202b6SYOSHIFUJI Hideaki int err; 177108b202b6SYOSHIFUJI Hideaki 17729afd85c9SLinus Lüssing err = ipv6_mc_check_mld(skb, &skb_trimmed); 177308b202b6SYOSHIFUJI Hideaki 17749afd85c9SLinus Lüssing if (err == -ENOMSG) { 17759afd85c9SLinus Lüssing if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr)) 17768fad9c39SLinus Lüssing BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 177708b202b6SYOSHIFUJI Hideaki return 0; 17789afd85c9SLinus Lüssing } else if (err < 0) { 17791080ab95SNikolay Aleksandrov br_multicast_err_count(br, port, skb->protocol); 17809afd85c9SLinus Lüssing return err; 178108b202b6SYOSHIFUJI Hideaki } 178208b202b6SYOSHIFUJI Hideaki 17839afd85c9SLinus Lüssing mld = (struct mld_msg *)skb_transport_header(skb); 17841080ab95SNikolay Aleksandrov BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type; 178508b202b6SYOSHIFUJI Hideaki 17869afd85c9SLinus Lüssing switch (mld->mld_type) { 178708b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 1788fc2af6c7SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 1789b0e9a30dSVlad Yasevich err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid); 179008b202b6SYOSHIFUJI Hideaki break; 179108b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 17929afd85c9SLinus Lüssing err = br_ip6_multicast_mld2_report(br, port, skb_trimmed, vid); 179308b202b6SYOSHIFUJI Hideaki break; 179408b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 17959afd85c9SLinus Lüssing err = br_ip6_multicast_query(br, port, skb_trimmed, vid); 179608b202b6SYOSHIFUJI Hideaki break; 179708b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 1798b0e9a30dSVlad Yasevich br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid); 17999afd85c9SLinus Lüssing break; 180008b202b6SYOSHIFUJI Hideaki } 180108b202b6SYOSHIFUJI Hideaki 1802a516993fSLinus Lüssing if (skb_trimmed && skb_trimmed != skb) 18039afd85c9SLinus Lüssing kfree_skb(skb_trimmed); 18049afd85c9SLinus Lüssing 1805a65056ecSNikolay Aleksandrov br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp, 18061080ab95SNikolay Aleksandrov BR_MCAST_DIR_RX); 18071080ab95SNikolay Aleksandrov 180808b202b6SYOSHIFUJI Hideaki return err; 180908b202b6SYOSHIFUJI Hideaki } 181008b202b6SYOSHIFUJI Hideaki #endif 181108b202b6SYOSHIFUJI Hideaki 1812eb1d1641SHerbert Xu int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port, 181306499098SVlad Yasevich struct sk_buff *skb, u16 vid) 1814eb1d1641SHerbert Xu { 18151080ab95SNikolay Aleksandrov int ret = 0; 18161080ab95SNikolay Aleksandrov 18171fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->igmp = 0; 18181fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->mrouters_only = 0; 18191fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 1820eb1d1641SHerbert Xu if (br->multicast_disabled) 1821eb1d1641SHerbert Xu return 0; 1822eb1d1641SHerbert Xu 1823eb1d1641SHerbert Xu switch (skb->protocol) { 1824eb1d1641SHerbert Xu case htons(ETH_P_IP): 18251080ab95SNikolay Aleksandrov ret = br_multicast_ipv4_rcv(br, port, skb, vid); 18261080ab95SNikolay Aleksandrov break; 1827dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 182808b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 18291080ab95SNikolay Aleksandrov ret = br_multicast_ipv6_rcv(br, port, skb, vid); 18301080ab95SNikolay Aleksandrov break; 183108b202b6SYOSHIFUJI Hideaki #endif 1832eb1d1641SHerbert Xu } 1833eb1d1641SHerbert Xu 18341080ab95SNikolay Aleksandrov return ret; 1835eb1d1641SHerbert Xu } 1836eb1d1641SHerbert Xu 1837cc0fdd80SLinus Lüssing static void br_multicast_query_expired(struct net_bridge *br, 18382cd41431SLinus Lüssing struct bridge_mcast_own_query *query, 18392cd41431SLinus Lüssing struct bridge_mcast_querier *querier) 1840cc0fdd80SLinus Lüssing { 1841cc0fdd80SLinus Lüssing spin_lock(&br->multicast_lock); 1842cc0fdd80SLinus Lüssing if (query->startup_sent < br->multicast_startup_query_count) 1843cc0fdd80SLinus Lüssing query->startup_sent++; 1844cc0fdd80SLinus Lüssing 184571d9f614SEric Dumazet RCU_INIT_POINTER(querier->port, NULL); 1846cc0fdd80SLinus Lüssing br_multicast_send_query(br, NULL, query); 1847cc0fdd80SLinus Lüssing spin_unlock(&br->multicast_lock); 1848cc0fdd80SLinus Lüssing } 1849cc0fdd80SLinus Lüssing 1850cc0fdd80SLinus Lüssing static void br_ip4_multicast_query_expired(unsigned long data) 1851eb1d1641SHerbert Xu { 1852eb1d1641SHerbert Xu struct net_bridge *br = (void *)data; 1853eb1d1641SHerbert Xu 18542cd41431SLinus Lüssing br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier); 1855eb1d1641SHerbert Xu } 1856eb1d1641SHerbert Xu 1857cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1858cc0fdd80SLinus Lüssing static void br_ip6_multicast_query_expired(unsigned long data) 1859cc0fdd80SLinus Lüssing { 1860cc0fdd80SLinus Lüssing struct net_bridge *br = (void *)data; 1861cc0fdd80SLinus Lüssing 18622cd41431SLinus Lüssing br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier); 1863cc0fdd80SLinus Lüssing } 1864cc0fdd80SLinus Lüssing #endif 1865cc0fdd80SLinus Lüssing 1866eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br) 1867eb1d1641SHerbert Xu { 1868eb1d1641SHerbert Xu br->hash_elasticity = 4; 1869eb1d1641SHerbert Xu br->hash_max = 512; 1870eb1d1641SHerbert Xu 18717f0aec7aSNikolay Aleksandrov br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 1872c5c23260SHerbert Xu br->multicast_querier = 0; 18731c8ad5bfSCong Wang br->multicast_query_use_ifaddr = 0; 1874eb1d1641SHerbert Xu br->multicast_last_member_count = 2; 1875eb1d1641SHerbert Xu br->multicast_startup_query_count = 2; 1876eb1d1641SHerbert Xu 1877eb1d1641SHerbert Xu br->multicast_last_member_interval = HZ; 1878eb1d1641SHerbert Xu br->multicast_query_response_interval = 10 * HZ; 1879eb1d1641SHerbert Xu br->multicast_startup_query_interval = 125 * HZ / 4; 1880eb1d1641SHerbert Xu br->multicast_query_interval = 125 * HZ; 1881eb1d1641SHerbert Xu br->multicast_querier_interval = 255 * HZ; 1882eb1d1641SHerbert Xu br->multicast_membership_interval = 260 * HZ; 1883eb1d1641SHerbert Xu 188490010b36SLinus Lüssing br->ip4_other_query.delay_time = 0; 18852cd41431SLinus Lüssing br->ip4_querier.port = NULL; 1886*aa2ae3e7SNikolay Aleksandrov br->multicast_igmp_version = 2; 1887cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1888*aa2ae3e7SNikolay Aleksandrov br->multicast_mld_version = 1; 188990010b36SLinus Lüssing br->ip6_other_query.delay_time = 0; 18902cd41431SLinus Lüssing br->ip6_querier.port = NULL; 1891cc0fdd80SLinus Lüssing #endif 18920888d5f3Sdaniel br->has_ipv6_addr = 1; 1893b00589afSLinus Lüssing 1894eb1d1641SHerbert Xu spin_lock_init(&br->multicast_lock); 1895eb1d1641SHerbert Xu setup_timer(&br->multicast_router_timer, 1896eb1d1641SHerbert Xu br_multicast_local_router_expired, 0); 189790010b36SLinus Lüssing setup_timer(&br->ip4_other_query.timer, 189890010b36SLinus Lüssing br_ip4_multicast_querier_expired, (unsigned long)br); 189990010b36SLinus Lüssing setup_timer(&br->ip4_own_query.timer, br_ip4_multicast_query_expired, 1900cc0fdd80SLinus Lüssing (unsigned long)br); 1901cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 190290010b36SLinus Lüssing setup_timer(&br->ip6_other_query.timer, 190390010b36SLinus Lüssing br_ip6_multicast_querier_expired, (unsigned long)br); 190490010b36SLinus Lüssing setup_timer(&br->ip6_own_query.timer, br_ip6_multicast_query_expired, 1905cc0fdd80SLinus Lüssing (unsigned long)br); 1906cc0fdd80SLinus Lüssing #endif 1907eb1d1641SHerbert Xu } 1908eb1d1641SHerbert Xu 1909cc0fdd80SLinus Lüssing static void __br_multicast_open(struct net_bridge *br, 191090010b36SLinus Lüssing struct bridge_mcast_own_query *query) 1911eb1d1641SHerbert Xu { 1912cc0fdd80SLinus Lüssing query->startup_sent = 0; 1913eb1d1641SHerbert Xu 1914eb1d1641SHerbert Xu if (br->multicast_disabled) 1915eb1d1641SHerbert Xu return; 1916eb1d1641SHerbert Xu 1917cc0fdd80SLinus Lüssing mod_timer(&query->timer, jiffies); 1918cc0fdd80SLinus Lüssing } 1919cc0fdd80SLinus Lüssing 1920cc0fdd80SLinus Lüssing void br_multicast_open(struct net_bridge *br) 1921cc0fdd80SLinus Lüssing { 192290010b36SLinus Lüssing __br_multicast_open(br, &br->ip4_own_query); 1923cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 192490010b36SLinus Lüssing __br_multicast_open(br, &br->ip6_own_query); 1925cc0fdd80SLinus Lüssing #endif 1926eb1d1641SHerbert Xu } 1927eb1d1641SHerbert Xu 1928eb1d1641SHerbert Xu void br_multicast_stop(struct net_bridge *br) 1929eb1d1641SHerbert Xu { 1930eb1d1641SHerbert Xu del_timer_sync(&br->multicast_router_timer); 193190010b36SLinus Lüssing del_timer_sync(&br->ip4_other_query.timer); 193290010b36SLinus Lüssing del_timer_sync(&br->ip4_own_query.timer); 1933cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 193490010b36SLinus Lüssing del_timer_sync(&br->ip6_other_query.timer); 193590010b36SLinus Lüssing del_timer_sync(&br->ip6_own_query.timer); 1936cc0fdd80SLinus Lüssing #endif 1937e10177abSSatish Ashok } 1938e10177abSSatish Ashok 1939e10177abSSatish Ashok void br_multicast_dev_del(struct net_bridge *br) 1940e10177abSSatish Ashok { 1941e10177abSSatish Ashok struct net_bridge_mdb_htable *mdb; 1942e10177abSSatish Ashok struct net_bridge_mdb_entry *mp; 1943e10177abSSatish Ashok struct hlist_node *n; 1944e10177abSSatish Ashok u32 ver; 1945e10177abSSatish Ashok int i; 1946eb1d1641SHerbert Xu 1947eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1948e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1949eb1d1641SHerbert Xu if (!mdb) 1950eb1d1641SHerbert Xu goto out; 1951eb1d1641SHerbert Xu 1952eb1d1641SHerbert Xu br->mdb = NULL; 1953eb1d1641SHerbert Xu 1954eb1d1641SHerbert Xu ver = mdb->ver; 1955eb1d1641SHerbert Xu for (i = 0; i < mdb->max; i++) { 1956b67bfe0dSSasha Levin hlist_for_each_entry_safe(mp, n, &mdb->mhash[i], 1957eb1d1641SHerbert Xu hlist[ver]) { 1958eb1d1641SHerbert Xu del_timer(&mp->timer); 1959eb1d1641SHerbert Xu call_rcu_bh(&mp->rcu, br_multicast_free_group); 1960eb1d1641SHerbert Xu } 1961eb1d1641SHerbert Xu } 1962eb1d1641SHerbert Xu 1963eb1d1641SHerbert Xu if (mdb->old) { 1964eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 196510cc2b50SHerbert Xu rcu_barrier_bh(); 1966eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1967eb1d1641SHerbert Xu WARN_ON(mdb->old); 1968eb1d1641SHerbert Xu } 1969eb1d1641SHerbert Xu 1970eb1d1641SHerbert Xu mdb->old = mdb; 1971eb1d1641SHerbert Xu call_rcu_bh(&mdb->rcu, br_mdb_free); 1972eb1d1641SHerbert Xu 1973eb1d1641SHerbert Xu out: 1974eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 19751080ab95SNikolay Aleksandrov 19761080ab95SNikolay Aleksandrov free_percpu(br->mcast_stats); 1977eb1d1641SHerbert Xu } 19780909e117SHerbert Xu 19790909e117SHerbert Xu int br_multicast_set_router(struct net_bridge *br, unsigned long val) 19800909e117SHerbert Xu { 19816ae4ae8eSLinus Lüssing int err = -EINVAL; 19820909e117SHerbert Xu 19830909e117SHerbert Xu spin_lock_bh(&br->multicast_lock); 19840909e117SHerbert Xu 19850909e117SHerbert Xu switch (val) { 19867f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_DISABLED: 19877f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_PERM: 19880909e117SHerbert Xu del_timer(&br->multicast_router_timer); 19890909e117SHerbert Xu /* fall through */ 19907f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_TEMP_QUERY: 19910909e117SHerbert Xu br->multicast_router = val; 19920909e117SHerbert Xu err = 0; 19930909e117SHerbert Xu break; 19940909e117SHerbert Xu } 19950909e117SHerbert Xu 19960909e117SHerbert Xu spin_unlock_bh(&br->multicast_lock); 19970909e117SHerbert Xu 19980909e117SHerbert Xu return err; 19990909e117SHerbert Xu } 20000909e117SHerbert Xu 20017f0aec7aSNikolay Aleksandrov static void __del_port_router(struct net_bridge_port *p) 20027f0aec7aSNikolay Aleksandrov { 20037f0aec7aSNikolay Aleksandrov if (hlist_unhashed(&p->rlist)) 20047f0aec7aSNikolay Aleksandrov return; 20057f0aec7aSNikolay Aleksandrov hlist_del_init_rcu(&p->rlist); 20067f0aec7aSNikolay Aleksandrov br_rtr_notify(p->br->dev, p, RTM_DELMDB); 20077f0aec7aSNikolay Aleksandrov } 20087f0aec7aSNikolay Aleksandrov 20090909e117SHerbert Xu int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val) 20100909e117SHerbert Xu { 20110909e117SHerbert Xu struct net_bridge *br = p->br; 2012a55d8246SNikolay Aleksandrov unsigned long now = jiffies; 20136ae4ae8eSLinus Lüssing int err = -EINVAL; 20140909e117SHerbert Xu 20150909e117SHerbert Xu spin_lock(&br->multicast_lock); 20164950cfd1SNikolay Aleksandrov if (p->multicast_router == val) { 2017a55d8246SNikolay Aleksandrov /* Refresh the temp router port timer */ 2018a55d8246SNikolay Aleksandrov if (p->multicast_router == MDB_RTR_TYPE_TEMP) 2019a55d8246SNikolay Aleksandrov mod_timer(&p->multicast_router_timer, 2020a55d8246SNikolay Aleksandrov now + br->multicast_querier_interval); 20214950cfd1SNikolay Aleksandrov err = 0; 20224950cfd1SNikolay Aleksandrov goto unlock; 20234950cfd1SNikolay Aleksandrov } 20240909e117SHerbert Xu switch (val) { 20257f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_DISABLED: 20267f0aec7aSNikolay Aleksandrov p->multicast_router = MDB_RTR_TYPE_DISABLED; 20277f0aec7aSNikolay Aleksandrov __del_port_router(p); 20280909e117SHerbert Xu del_timer(&p->multicast_router_timer); 20290909e117SHerbert Xu break; 20307f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_TEMP_QUERY: 20317f0aec7aSNikolay Aleksandrov p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 20327f0aec7aSNikolay Aleksandrov __del_port_router(p); 20337f0aec7aSNikolay Aleksandrov break; 20347f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_PERM: 20357f0aec7aSNikolay Aleksandrov p->multicast_router = MDB_RTR_TYPE_PERM; 20367f0aec7aSNikolay Aleksandrov del_timer(&p->multicast_router_timer); 20370909e117SHerbert Xu br_multicast_add_router(br, p); 20380909e117SHerbert Xu break; 2039a55d8246SNikolay Aleksandrov case MDB_RTR_TYPE_TEMP: 2040a55d8246SNikolay Aleksandrov p->multicast_router = MDB_RTR_TYPE_TEMP; 2041a55d8246SNikolay Aleksandrov br_multicast_mark_router(br, p); 2042a55d8246SNikolay Aleksandrov break; 20437f0aec7aSNikolay Aleksandrov default: 20447f0aec7aSNikolay Aleksandrov goto unlock; 20450909e117SHerbert Xu } 20467f0aec7aSNikolay Aleksandrov err = 0; 20477f0aec7aSNikolay Aleksandrov unlock: 20480909e117SHerbert Xu spin_unlock(&br->multicast_lock); 20490909e117SHerbert Xu 20500909e117SHerbert Xu return err; 20510909e117SHerbert Xu } 2052561f1103SHerbert Xu 2053cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br, 205490010b36SLinus Lüssing struct bridge_mcast_own_query *query) 2055561f1103SHerbert Xu { 2056561f1103SHerbert Xu struct net_bridge_port *port; 205774857216SHerbert Xu 2058cc0fdd80SLinus Lüssing __br_multicast_open(br, query); 205974857216SHerbert Xu 206074857216SHerbert Xu list_for_each_entry(port, &br->port_list, list) { 206174857216SHerbert Xu if (port->state == BR_STATE_DISABLED || 206274857216SHerbert Xu port->state == BR_STATE_BLOCKING) 206374857216SHerbert Xu continue; 206474857216SHerbert Xu 206590010b36SLinus Lüssing if (query == &br->ip4_own_query) 206690010b36SLinus Lüssing br_multicast_enable(&port->ip4_own_query); 2067cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 2068cc0fdd80SLinus Lüssing else 206990010b36SLinus Lüssing br_multicast_enable(&port->ip6_own_query); 2070cc0fdd80SLinus Lüssing #endif 207174857216SHerbert Xu } 207274857216SHerbert Xu } 207374857216SHerbert Xu 207474857216SHerbert Xu int br_multicast_toggle(struct net_bridge *br, unsigned long val) 207574857216SHerbert Xu { 2076e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 20777cb3f921SNikolay Aleksandrov struct net_bridge_port *port; 20787cb3f921SNikolay Aleksandrov int err = 0; 2079561f1103SHerbert Xu 2080ef5e0d82SAndrey Vagin spin_lock_bh(&br->multicast_lock); 2081561f1103SHerbert Xu if (br->multicast_disabled == !val) 2082561f1103SHerbert Xu goto unlock; 2083561f1103SHerbert Xu 2084561f1103SHerbert Xu br->multicast_disabled = !val; 2085561f1103SHerbert Xu if (br->multicast_disabled) 2086561f1103SHerbert Xu goto unlock; 2087561f1103SHerbert Xu 20883a7fda06SHerbert Xu if (!netif_running(br->dev)) 20893a7fda06SHerbert Xu goto unlock; 20903a7fda06SHerbert Xu 2091e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 2092e8051688SEric Dumazet if (mdb) { 2093e8051688SEric Dumazet if (mdb->old) { 2094561f1103SHerbert Xu err = -EEXIST; 2095561f1103SHerbert Xu rollback: 2096561f1103SHerbert Xu br->multicast_disabled = !!val; 2097561f1103SHerbert Xu goto unlock; 2098561f1103SHerbert Xu } 2099561f1103SHerbert Xu 2100e8051688SEric Dumazet err = br_mdb_rehash(&br->mdb, mdb->max, 2101561f1103SHerbert Xu br->hash_elasticity); 2102561f1103SHerbert Xu if (err) 2103561f1103SHerbert Xu goto rollback; 2104561f1103SHerbert Xu } 2105561f1103SHerbert Xu 21067cb3f921SNikolay Aleksandrov br_multicast_open(br); 21077cb3f921SNikolay Aleksandrov list_for_each_entry(port, &br->port_list, list) 21087cb3f921SNikolay Aleksandrov __br_multicast_enable_port(port); 2109561f1103SHerbert Xu 2110561f1103SHerbert Xu unlock: 2111ef5e0d82SAndrey Vagin spin_unlock_bh(&br->multicast_lock); 2112561f1103SHerbert Xu 2113561f1103SHerbert Xu return err; 2114561f1103SHerbert Xu } 2115b195167fSHerbert Xu 2116c5c23260SHerbert Xu int br_multicast_set_querier(struct net_bridge *br, unsigned long val) 2117c5c23260SHerbert Xu { 2118b00589afSLinus Lüssing unsigned long max_delay; 2119b00589afSLinus Lüssing 2120c5c23260SHerbert Xu val = !!val; 2121c5c23260SHerbert Xu 2122c5c23260SHerbert Xu spin_lock_bh(&br->multicast_lock); 2123c5c23260SHerbert Xu if (br->multicast_querier == val) 2124c5c23260SHerbert Xu goto unlock; 2125c5c23260SHerbert Xu 2126c5c23260SHerbert Xu br->multicast_querier = val; 2127b00589afSLinus Lüssing if (!val) 2128b00589afSLinus Lüssing goto unlock; 2129b00589afSLinus Lüssing 2130b00589afSLinus Lüssing max_delay = br->multicast_query_response_interval; 2131b00589afSLinus Lüssing 213290010b36SLinus Lüssing if (!timer_pending(&br->ip4_other_query.timer)) 213390010b36SLinus Lüssing br->ip4_other_query.delay_time = jiffies + max_delay; 2134cc0fdd80SLinus Lüssing 213590010b36SLinus Lüssing br_multicast_start_querier(br, &br->ip4_own_query); 2136cc0fdd80SLinus Lüssing 2137cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 213890010b36SLinus Lüssing if (!timer_pending(&br->ip6_other_query.timer)) 213990010b36SLinus Lüssing br->ip6_other_query.delay_time = jiffies + max_delay; 2140cc0fdd80SLinus Lüssing 214190010b36SLinus Lüssing br_multicast_start_querier(br, &br->ip6_own_query); 2142cc0fdd80SLinus Lüssing #endif 2143c5c23260SHerbert Xu 2144c5c23260SHerbert Xu unlock: 2145c5c23260SHerbert Xu spin_unlock_bh(&br->multicast_lock); 2146c5c23260SHerbert Xu 2147c5c23260SHerbert Xu return 0; 2148c5c23260SHerbert Xu } 2149c5c23260SHerbert Xu 2150b195167fSHerbert Xu int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val) 2151b195167fSHerbert Xu { 21526ae4ae8eSLinus Lüssing int err = -EINVAL; 2153b195167fSHerbert Xu u32 old; 2154e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 2155b195167fSHerbert Xu 2156fe0d692bSCurt Brune spin_lock_bh(&br->multicast_lock); 2157b195167fSHerbert Xu if (!is_power_of_2(val)) 2158b195167fSHerbert Xu goto unlock; 2159e8051688SEric Dumazet 2160e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 2161e8051688SEric Dumazet if (mdb && val < mdb->size) 2162b195167fSHerbert Xu goto unlock; 2163b195167fSHerbert Xu 2164b195167fSHerbert Xu err = 0; 2165b195167fSHerbert Xu 2166b195167fSHerbert Xu old = br->hash_max; 2167b195167fSHerbert Xu br->hash_max = val; 2168b195167fSHerbert Xu 2169e8051688SEric Dumazet if (mdb) { 2170e8051688SEric Dumazet if (mdb->old) { 2171b195167fSHerbert Xu err = -EEXIST; 2172b195167fSHerbert Xu rollback: 2173b195167fSHerbert Xu br->hash_max = old; 2174b195167fSHerbert Xu goto unlock; 2175b195167fSHerbert Xu } 2176b195167fSHerbert Xu 2177b195167fSHerbert Xu err = br_mdb_rehash(&br->mdb, br->hash_max, 2178b195167fSHerbert Xu br->hash_elasticity); 2179b195167fSHerbert Xu if (err) 2180b195167fSHerbert Xu goto rollback; 2181b195167fSHerbert Xu } 2182b195167fSHerbert Xu 2183b195167fSHerbert Xu unlock: 2184fe0d692bSCurt Brune spin_unlock_bh(&br->multicast_lock); 2185b195167fSHerbert Xu 2186b195167fSHerbert Xu return err; 2187b195167fSHerbert Xu } 218807f8ac4aSLinus Lüssing 21895e923585SNikolay Aleksandrov int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val) 21905e923585SNikolay Aleksandrov { 21915e923585SNikolay Aleksandrov /* Currently we support only version 2 and 3 */ 21925e923585SNikolay Aleksandrov switch (val) { 21935e923585SNikolay Aleksandrov case 2: 21945e923585SNikolay Aleksandrov case 3: 21955e923585SNikolay Aleksandrov break; 21965e923585SNikolay Aleksandrov default: 21975e923585SNikolay Aleksandrov return -EINVAL; 21985e923585SNikolay Aleksandrov } 21995e923585SNikolay Aleksandrov 22005e923585SNikolay Aleksandrov spin_lock_bh(&br->multicast_lock); 22015e923585SNikolay Aleksandrov br->multicast_igmp_version = val; 22025e923585SNikolay Aleksandrov spin_unlock_bh(&br->multicast_lock); 22035e923585SNikolay Aleksandrov 22045e923585SNikolay Aleksandrov return 0; 22055e923585SNikolay Aleksandrov } 22065e923585SNikolay Aleksandrov 2207*aa2ae3e7SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 2208*aa2ae3e7SNikolay Aleksandrov int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val) 2209*aa2ae3e7SNikolay Aleksandrov { 2210*aa2ae3e7SNikolay Aleksandrov /* Currently we support version 1 and 2 */ 2211*aa2ae3e7SNikolay Aleksandrov switch (val) { 2212*aa2ae3e7SNikolay Aleksandrov case 1: 2213*aa2ae3e7SNikolay Aleksandrov case 2: 2214*aa2ae3e7SNikolay Aleksandrov break; 2215*aa2ae3e7SNikolay Aleksandrov default: 2216*aa2ae3e7SNikolay Aleksandrov return -EINVAL; 2217*aa2ae3e7SNikolay Aleksandrov } 2218*aa2ae3e7SNikolay Aleksandrov 2219*aa2ae3e7SNikolay Aleksandrov spin_lock_bh(&br->multicast_lock); 2220*aa2ae3e7SNikolay Aleksandrov br->multicast_mld_version = val; 2221*aa2ae3e7SNikolay Aleksandrov spin_unlock_bh(&br->multicast_lock); 2222*aa2ae3e7SNikolay Aleksandrov 2223*aa2ae3e7SNikolay Aleksandrov return 0; 2224*aa2ae3e7SNikolay Aleksandrov } 2225*aa2ae3e7SNikolay Aleksandrov #endif 2226*aa2ae3e7SNikolay Aleksandrov 222707f8ac4aSLinus Lüssing /** 222807f8ac4aSLinus Lüssing * br_multicast_list_adjacent - Returns snooped multicast addresses 222907f8ac4aSLinus Lüssing * @dev: The bridge port adjacent to which to retrieve addresses 223007f8ac4aSLinus Lüssing * @br_ip_list: The list to store found, snooped multicast IP addresses in 223107f8ac4aSLinus Lüssing * 223207f8ac4aSLinus Lüssing * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast 223307f8ac4aSLinus Lüssing * snooping feature on all bridge ports of dev's bridge device, excluding 223407f8ac4aSLinus Lüssing * the addresses from dev itself. 223507f8ac4aSLinus Lüssing * 223607f8ac4aSLinus Lüssing * Returns the number of items added to br_ip_list. 223707f8ac4aSLinus Lüssing * 223807f8ac4aSLinus Lüssing * Notes: 223907f8ac4aSLinus Lüssing * - br_ip_list needs to be initialized by caller 224007f8ac4aSLinus Lüssing * - br_ip_list might contain duplicates in the end 224107f8ac4aSLinus Lüssing * (needs to be taken care of by caller) 224207f8ac4aSLinus Lüssing * - br_ip_list needs to be freed by caller 224307f8ac4aSLinus Lüssing */ 224407f8ac4aSLinus Lüssing int br_multicast_list_adjacent(struct net_device *dev, 224507f8ac4aSLinus Lüssing struct list_head *br_ip_list) 224607f8ac4aSLinus Lüssing { 224707f8ac4aSLinus Lüssing struct net_bridge *br; 224807f8ac4aSLinus Lüssing struct net_bridge_port *port; 224907f8ac4aSLinus Lüssing struct net_bridge_port_group *group; 225007f8ac4aSLinus Lüssing struct br_ip_list *entry; 225107f8ac4aSLinus Lüssing int count = 0; 225207f8ac4aSLinus Lüssing 225307f8ac4aSLinus Lüssing rcu_read_lock(); 225407f8ac4aSLinus Lüssing if (!br_ip_list || !br_port_exists(dev)) 225507f8ac4aSLinus Lüssing goto unlock; 225607f8ac4aSLinus Lüssing 225707f8ac4aSLinus Lüssing port = br_port_get_rcu(dev); 225807f8ac4aSLinus Lüssing if (!port || !port->br) 225907f8ac4aSLinus Lüssing goto unlock; 226007f8ac4aSLinus Lüssing 226107f8ac4aSLinus Lüssing br = port->br; 226207f8ac4aSLinus Lüssing 226307f8ac4aSLinus Lüssing list_for_each_entry_rcu(port, &br->port_list, list) { 226407f8ac4aSLinus Lüssing if (!port->dev || port->dev == dev) 226507f8ac4aSLinus Lüssing continue; 226607f8ac4aSLinus Lüssing 226707f8ac4aSLinus Lüssing hlist_for_each_entry_rcu(group, &port->mglist, mglist) { 226807f8ac4aSLinus Lüssing entry = kmalloc(sizeof(*entry), GFP_ATOMIC); 226907f8ac4aSLinus Lüssing if (!entry) 227007f8ac4aSLinus Lüssing goto unlock; 227107f8ac4aSLinus Lüssing 227207f8ac4aSLinus Lüssing entry->addr = group->addr; 227307f8ac4aSLinus Lüssing list_add(&entry->list, br_ip_list); 227407f8ac4aSLinus Lüssing count++; 227507f8ac4aSLinus Lüssing } 227607f8ac4aSLinus Lüssing } 227707f8ac4aSLinus Lüssing 227807f8ac4aSLinus Lüssing unlock: 227907f8ac4aSLinus Lüssing rcu_read_unlock(); 228007f8ac4aSLinus Lüssing return count; 228107f8ac4aSLinus Lüssing } 228207f8ac4aSLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_list_adjacent); 22832cd41431SLinus Lüssing 22842cd41431SLinus Lüssing /** 2285c34963e2SLinus Lüssing * br_multicast_has_querier_anywhere - Checks for a querier on a bridge 2286c34963e2SLinus Lüssing * @dev: The bridge port providing the bridge on which to check for a querier 2287c34963e2SLinus Lüssing * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 2288c34963e2SLinus Lüssing * 2289c34963e2SLinus Lüssing * Checks whether the given interface has a bridge on top and if so returns 2290c34963e2SLinus Lüssing * true if a valid querier exists anywhere on the bridged link layer. 2291c34963e2SLinus Lüssing * Otherwise returns false. 2292c34963e2SLinus Lüssing */ 2293c34963e2SLinus Lüssing bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto) 2294c34963e2SLinus Lüssing { 2295c34963e2SLinus Lüssing struct net_bridge *br; 2296c34963e2SLinus Lüssing struct net_bridge_port *port; 2297c34963e2SLinus Lüssing struct ethhdr eth; 2298c34963e2SLinus Lüssing bool ret = false; 2299c34963e2SLinus Lüssing 2300c34963e2SLinus Lüssing rcu_read_lock(); 2301c34963e2SLinus Lüssing if (!br_port_exists(dev)) 2302c34963e2SLinus Lüssing goto unlock; 2303c34963e2SLinus Lüssing 2304c34963e2SLinus Lüssing port = br_port_get_rcu(dev); 2305c34963e2SLinus Lüssing if (!port || !port->br) 2306c34963e2SLinus Lüssing goto unlock; 2307c34963e2SLinus Lüssing 2308c34963e2SLinus Lüssing br = port->br; 2309c34963e2SLinus Lüssing 2310c34963e2SLinus Lüssing memset(ð, 0, sizeof(eth)); 2311c34963e2SLinus Lüssing eth.h_proto = htons(proto); 2312c34963e2SLinus Lüssing 2313c34963e2SLinus Lüssing ret = br_multicast_querier_exists(br, ð); 2314c34963e2SLinus Lüssing 2315c34963e2SLinus Lüssing unlock: 2316c34963e2SLinus Lüssing rcu_read_unlock(); 2317c34963e2SLinus Lüssing return ret; 2318c34963e2SLinus Lüssing } 2319c34963e2SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere); 2320c34963e2SLinus Lüssing 2321c34963e2SLinus Lüssing /** 23222cd41431SLinus Lüssing * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port 23232cd41431SLinus Lüssing * @dev: The bridge port adjacent to which to check for a querier 23242cd41431SLinus Lüssing * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 23252cd41431SLinus Lüssing * 23262cd41431SLinus Lüssing * Checks whether the given interface has a bridge on top and if so returns 23272cd41431SLinus Lüssing * true if a selected querier is behind one of the other ports of this 23282cd41431SLinus Lüssing * bridge. Otherwise returns false. 23292cd41431SLinus Lüssing */ 23302cd41431SLinus Lüssing bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto) 23312cd41431SLinus Lüssing { 23322cd41431SLinus Lüssing struct net_bridge *br; 23332cd41431SLinus Lüssing struct net_bridge_port *port; 23342cd41431SLinus Lüssing bool ret = false; 23352cd41431SLinus Lüssing 23362cd41431SLinus Lüssing rcu_read_lock(); 23372cd41431SLinus Lüssing if (!br_port_exists(dev)) 23382cd41431SLinus Lüssing goto unlock; 23392cd41431SLinus Lüssing 23402cd41431SLinus Lüssing port = br_port_get_rcu(dev); 23412cd41431SLinus Lüssing if (!port || !port->br) 23422cd41431SLinus Lüssing goto unlock; 23432cd41431SLinus Lüssing 23442cd41431SLinus Lüssing br = port->br; 23452cd41431SLinus Lüssing 23462cd41431SLinus Lüssing switch (proto) { 23472cd41431SLinus Lüssing case ETH_P_IP: 23482cd41431SLinus Lüssing if (!timer_pending(&br->ip4_other_query.timer) || 23492cd41431SLinus Lüssing rcu_dereference(br->ip4_querier.port) == port) 23502cd41431SLinus Lüssing goto unlock; 23512cd41431SLinus Lüssing break; 23523993c4e1SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 23532cd41431SLinus Lüssing case ETH_P_IPV6: 23542cd41431SLinus Lüssing if (!timer_pending(&br->ip6_other_query.timer) || 23552cd41431SLinus Lüssing rcu_dereference(br->ip6_querier.port) == port) 23562cd41431SLinus Lüssing goto unlock; 23572cd41431SLinus Lüssing break; 23583993c4e1SLinus Lüssing #endif 23592cd41431SLinus Lüssing default: 23602cd41431SLinus Lüssing goto unlock; 23612cd41431SLinus Lüssing } 23622cd41431SLinus Lüssing 23632cd41431SLinus Lüssing ret = true; 23642cd41431SLinus Lüssing unlock: 23652cd41431SLinus Lüssing rcu_read_unlock(); 23662cd41431SLinus Lüssing return ret; 23672cd41431SLinus Lüssing } 23682cd41431SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent); 23691080ab95SNikolay Aleksandrov 23701080ab95SNikolay Aleksandrov static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats, 2371a65056ecSNikolay Aleksandrov const struct sk_buff *skb, u8 type, u8 dir) 23721080ab95SNikolay Aleksandrov { 23731080ab95SNikolay Aleksandrov struct bridge_mcast_stats *pstats = this_cpu_ptr(stats); 2374a65056ecSNikolay Aleksandrov __be16 proto = skb->protocol; 2375a65056ecSNikolay Aleksandrov unsigned int t_len; 23761080ab95SNikolay Aleksandrov 23771080ab95SNikolay Aleksandrov u64_stats_update_begin(&pstats->syncp); 23781080ab95SNikolay Aleksandrov switch (proto) { 23791080ab95SNikolay Aleksandrov case htons(ETH_P_IP): 2380a65056ecSNikolay Aleksandrov t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb); 23811080ab95SNikolay Aleksandrov switch (type) { 23821080ab95SNikolay Aleksandrov case IGMP_HOST_MEMBERSHIP_REPORT: 23831080ab95SNikolay Aleksandrov pstats->mstats.igmp_v1reports[dir]++; 23841080ab95SNikolay Aleksandrov break; 23851080ab95SNikolay Aleksandrov case IGMPV2_HOST_MEMBERSHIP_REPORT: 23861080ab95SNikolay Aleksandrov pstats->mstats.igmp_v2reports[dir]++; 23871080ab95SNikolay Aleksandrov break; 23881080ab95SNikolay Aleksandrov case IGMPV3_HOST_MEMBERSHIP_REPORT: 23891080ab95SNikolay Aleksandrov pstats->mstats.igmp_v3reports[dir]++; 23901080ab95SNikolay Aleksandrov break; 23911080ab95SNikolay Aleksandrov case IGMP_HOST_MEMBERSHIP_QUERY: 2392a65056ecSNikolay Aleksandrov if (t_len != sizeof(struct igmphdr)) { 2393a65056ecSNikolay Aleksandrov pstats->mstats.igmp_v3queries[dir]++; 2394a65056ecSNikolay Aleksandrov } else { 2395a65056ecSNikolay Aleksandrov unsigned int offset = skb_transport_offset(skb); 2396a65056ecSNikolay Aleksandrov struct igmphdr *ih, _ihdr; 2397a65056ecSNikolay Aleksandrov 2398a65056ecSNikolay Aleksandrov ih = skb_header_pointer(skb, offset, 2399a65056ecSNikolay Aleksandrov sizeof(_ihdr), &_ihdr); 2400a65056ecSNikolay Aleksandrov if (!ih) 2401a65056ecSNikolay Aleksandrov break; 2402a65056ecSNikolay Aleksandrov if (!ih->code) 2403a65056ecSNikolay Aleksandrov pstats->mstats.igmp_v1queries[dir]++; 2404a65056ecSNikolay Aleksandrov else 2405a65056ecSNikolay Aleksandrov pstats->mstats.igmp_v2queries[dir]++; 2406a65056ecSNikolay Aleksandrov } 24071080ab95SNikolay Aleksandrov break; 24081080ab95SNikolay Aleksandrov case IGMP_HOST_LEAVE_MESSAGE: 24091080ab95SNikolay Aleksandrov pstats->mstats.igmp_leaves[dir]++; 24101080ab95SNikolay Aleksandrov break; 24111080ab95SNikolay Aleksandrov } 24121080ab95SNikolay Aleksandrov break; 24131080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 24141080ab95SNikolay Aleksandrov case htons(ETH_P_IPV6): 2415a65056ecSNikolay Aleksandrov t_len = ntohs(ipv6_hdr(skb)->payload_len) + 2416a65056ecSNikolay Aleksandrov sizeof(struct ipv6hdr); 2417a65056ecSNikolay Aleksandrov t_len -= skb_network_header_len(skb); 24181080ab95SNikolay Aleksandrov switch (type) { 24191080ab95SNikolay Aleksandrov case ICMPV6_MGM_REPORT: 24201080ab95SNikolay Aleksandrov pstats->mstats.mld_v1reports[dir]++; 24211080ab95SNikolay Aleksandrov break; 24221080ab95SNikolay Aleksandrov case ICMPV6_MLD2_REPORT: 24231080ab95SNikolay Aleksandrov pstats->mstats.mld_v2reports[dir]++; 24241080ab95SNikolay Aleksandrov break; 24251080ab95SNikolay Aleksandrov case ICMPV6_MGM_QUERY: 2426a65056ecSNikolay Aleksandrov if (t_len != sizeof(struct mld_msg)) 2427a65056ecSNikolay Aleksandrov pstats->mstats.mld_v2queries[dir]++; 2428a65056ecSNikolay Aleksandrov else 2429a65056ecSNikolay Aleksandrov pstats->mstats.mld_v1queries[dir]++; 24301080ab95SNikolay Aleksandrov break; 24311080ab95SNikolay Aleksandrov case ICMPV6_MGM_REDUCTION: 24321080ab95SNikolay Aleksandrov pstats->mstats.mld_leaves[dir]++; 24331080ab95SNikolay Aleksandrov break; 24341080ab95SNikolay Aleksandrov } 24351080ab95SNikolay Aleksandrov break; 24361080ab95SNikolay Aleksandrov #endif /* CONFIG_IPV6 */ 24371080ab95SNikolay Aleksandrov } 24381080ab95SNikolay Aleksandrov u64_stats_update_end(&pstats->syncp); 24391080ab95SNikolay Aleksandrov } 24401080ab95SNikolay Aleksandrov 24411080ab95SNikolay Aleksandrov void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p, 2442a65056ecSNikolay Aleksandrov const struct sk_buff *skb, u8 type, u8 dir) 24431080ab95SNikolay Aleksandrov { 24441080ab95SNikolay Aleksandrov struct bridge_mcast_stats __percpu *stats; 24451080ab95SNikolay Aleksandrov 24461080ab95SNikolay Aleksandrov /* if multicast_disabled is true then igmp type can't be set */ 24471080ab95SNikolay Aleksandrov if (!type || !br->multicast_stats_enabled) 24481080ab95SNikolay Aleksandrov return; 24491080ab95SNikolay Aleksandrov 24501080ab95SNikolay Aleksandrov if (p) 24511080ab95SNikolay Aleksandrov stats = p->mcast_stats; 24521080ab95SNikolay Aleksandrov else 24531080ab95SNikolay Aleksandrov stats = br->mcast_stats; 24541080ab95SNikolay Aleksandrov if (WARN_ON(!stats)) 24551080ab95SNikolay Aleksandrov return; 24561080ab95SNikolay Aleksandrov 2457a65056ecSNikolay Aleksandrov br_mcast_stats_add(stats, skb, type, dir); 24581080ab95SNikolay Aleksandrov } 24591080ab95SNikolay Aleksandrov 24601080ab95SNikolay Aleksandrov int br_multicast_init_stats(struct net_bridge *br) 24611080ab95SNikolay Aleksandrov { 24621080ab95SNikolay Aleksandrov br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats); 24631080ab95SNikolay Aleksandrov if (!br->mcast_stats) 24641080ab95SNikolay Aleksandrov return -ENOMEM; 24651080ab95SNikolay Aleksandrov 24661080ab95SNikolay Aleksandrov return 0; 24671080ab95SNikolay Aleksandrov } 24681080ab95SNikolay Aleksandrov 24691080ab95SNikolay Aleksandrov static void mcast_stats_add_dir(u64 *dst, u64 *src) 24701080ab95SNikolay Aleksandrov { 24711080ab95SNikolay Aleksandrov dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX]; 24721080ab95SNikolay Aleksandrov dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX]; 24731080ab95SNikolay Aleksandrov } 24741080ab95SNikolay Aleksandrov 24751080ab95SNikolay Aleksandrov void br_multicast_get_stats(const struct net_bridge *br, 24761080ab95SNikolay Aleksandrov const struct net_bridge_port *p, 24771080ab95SNikolay Aleksandrov struct br_mcast_stats *dest) 24781080ab95SNikolay Aleksandrov { 24791080ab95SNikolay Aleksandrov struct bridge_mcast_stats __percpu *stats; 24801080ab95SNikolay Aleksandrov struct br_mcast_stats tdst; 24811080ab95SNikolay Aleksandrov int i; 24821080ab95SNikolay Aleksandrov 24831080ab95SNikolay Aleksandrov memset(dest, 0, sizeof(*dest)); 24841080ab95SNikolay Aleksandrov if (p) 24851080ab95SNikolay Aleksandrov stats = p->mcast_stats; 24861080ab95SNikolay Aleksandrov else 24871080ab95SNikolay Aleksandrov stats = br->mcast_stats; 24881080ab95SNikolay Aleksandrov if (WARN_ON(!stats)) 24891080ab95SNikolay Aleksandrov return; 24901080ab95SNikolay Aleksandrov 24911080ab95SNikolay Aleksandrov memset(&tdst, 0, sizeof(tdst)); 24921080ab95SNikolay Aleksandrov for_each_possible_cpu(i) { 24931080ab95SNikolay Aleksandrov struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i); 24941080ab95SNikolay Aleksandrov struct br_mcast_stats temp; 24951080ab95SNikolay Aleksandrov unsigned int start; 24961080ab95SNikolay Aleksandrov 24971080ab95SNikolay Aleksandrov do { 24981080ab95SNikolay Aleksandrov start = u64_stats_fetch_begin_irq(&cpu_stats->syncp); 24991080ab95SNikolay Aleksandrov memcpy(&temp, &cpu_stats->mstats, sizeof(temp)); 25001080ab95SNikolay Aleksandrov } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start)); 25011080ab95SNikolay Aleksandrov 2502a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries); 2503a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries); 2504a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries); 25051080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves); 25061080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports); 25071080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports); 25081080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports); 25091080ab95SNikolay Aleksandrov tdst.igmp_parse_errors += temp.igmp_parse_errors; 25101080ab95SNikolay Aleksandrov 2511a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries); 2512a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries); 25131080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves); 25141080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports); 25151080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports); 25161080ab95SNikolay Aleksandrov tdst.mld_parse_errors += temp.mld_parse_errors; 25171080ab95SNikolay Aleksandrov } 25181080ab95SNikolay Aleksandrov memcpy(dest, &tdst, sizeof(*dest)); 25191080ab95SNikolay Aleksandrov } 2520