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 { 368*5e923585SNikolay Aleksandrov struct igmpv3_query *ihv3; 369*5e923585SNikolay 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 375*5e923585SNikolay Aleksandrov igmp_hdr_size = sizeof(*ih); 376*5e923585SNikolay Aleksandrov if (br->multicast_igmp_version == 3) 377*5e923585SNikolay Aleksandrov igmp_hdr_size = sizeof(*ihv3); 378eb1d1641SHerbert Xu skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) + 379*5e923585SNikolay 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; 404*5e923585SNikolay 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; 421*5e923585SNikolay Aleksandrov 422*5e923585SNikolay Aleksandrov switch (br->multicast_igmp_version) { 423*5e923585SNikolay Aleksandrov case 2: 424*5e923585SNikolay 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; 431*5e923585SNikolay Aleksandrov ih->csum = ip_compute_csum((void *)ih, sizeof(*ih)); 432*5e923585SNikolay Aleksandrov break; 433*5e923585SNikolay Aleksandrov case 3: 434*5e923585SNikolay Aleksandrov ihv3 = igmpv3_query_hdr(skb); 435*5e923585SNikolay Aleksandrov ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY; 436*5e923585SNikolay Aleksandrov ihv3->code = (group ? br->multicast_last_member_interval : 437*5e923585SNikolay Aleksandrov br->multicast_query_response_interval) / 438*5e923585SNikolay Aleksandrov (HZ / IGMP_TIMER_SCALE); 439*5e923585SNikolay Aleksandrov ihv3->group = group; 440*5e923585SNikolay Aleksandrov ihv3->qqic = br->multicast_query_interval / HZ; 441*5e923585SNikolay Aleksandrov ihv3->nsrcs = 0; 442*5e923585SNikolay Aleksandrov ihv3->resv = 0; 443*5e923585SNikolay Aleksandrov ihv3->suppress = 0; 444*5e923585SNikolay Aleksandrov ihv3->qrv = 2; 445*5e923585SNikolay Aleksandrov ihv3->csum = 0; 446*5e923585SNikolay Aleksandrov ihv3->csum = ip_compute_csum((void *)ihv3, sizeof(*ihv3)); 447*5e923585SNikolay Aleksandrov break; 448*5e923585SNikolay Aleksandrov } 449eb1d1641SHerbert Xu 450*5e923585SNikolay 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 { 46208b202b6SYOSHIFUJI Hideaki struct sk_buff *skb; 46308b202b6SYOSHIFUJI Hideaki struct ipv6hdr *ip6h; 46408b202b6SYOSHIFUJI Hideaki struct mld_msg *mldq; 46508b202b6SYOSHIFUJI Hideaki struct ethhdr *eth; 46608b202b6SYOSHIFUJI Hideaki u8 *hopopt; 46708b202b6SYOSHIFUJI Hideaki unsigned long interval; 46808b202b6SYOSHIFUJI Hideaki 46908b202b6SYOSHIFUJI Hideaki skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) + 47008b202b6SYOSHIFUJI Hideaki 8 + sizeof(*mldq)); 47108b202b6SYOSHIFUJI Hideaki if (!skb) 47208b202b6SYOSHIFUJI Hideaki goto out; 47308b202b6SYOSHIFUJI Hideaki 47408b202b6SYOSHIFUJI Hideaki skb->protocol = htons(ETH_P_IPV6); 47508b202b6SYOSHIFUJI Hideaki 47608b202b6SYOSHIFUJI Hideaki /* Ethernet header */ 47708b202b6SYOSHIFUJI Hideaki skb_reset_mac_header(skb); 47808b202b6SYOSHIFUJI Hideaki eth = eth_hdr(skb); 47908b202b6SYOSHIFUJI Hideaki 480e5a727f6SJoe Perches ether_addr_copy(eth->h_source, br->dev->dev_addr); 48108b202b6SYOSHIFUJI Hideaki eth->h_proto = htons(ETH_P_IPV6); 48208b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*eth)); 48308b202b6SYOSHIFUJI Hideaki 48408b202b6SYOSHIFUJI Hideaki /* IPv6 header + HbH option */ 48508b202b6SYOSHIFUJI Hideaki skb_set_network_header(skb, skb->len); 48608b202b6SYOSHIFUJI Hideaki ip6h = ipv6_hdr(skb); 48708b202b6SYOSHIFUJI Hideaki 48808b202b6SYOSHIFUJI Hideaki *(__force __be32 *)ip6h = htonl(0x60000000); 48976d66158SDavid Stevens ip6h->payload_len = htons(8 + sizeof(*mldq)); 49008b202b6SYOSHIFUJI Hideaki ip6h->nexthdr = IPPROTO_HOPOPTS; 49108b202b6SYOSHIFUJI Hideaki ip6h->hop_limit = 1; 492a7bff75bSLinus Lüssing ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1)); 493d1d81d4cSUlrich Weber if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0, 494d1d81d4cSUlrich Weber &ip6h->saddr)) { 495d1d81d4cSUlrich Weber kfree_skb(skb); 4960888d5f3Sdaniel br->has_ipv6_addr = 0; 497d1d81d4cSUlrich Weber return NULL; 498d1d81d4cSUlrich Weber } 4990888d5f3Sdaniel 5000888d5f3Sdaniel br->has_ipv6_addr = 1; 50136cff5a1SLinus Lüssing ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest); 50208b202b6SYOSHIFUJI Hideaki 50308b202b6SYOSHIFUJI Hideaki hopopt = (u8 *)(ip6h + 1); 50408b202b6SYOSHIFUJI Hideaki hopopt[0] = IPPROTO_ICMPV6; /* next hdr */ 50508b202b6SYOSHIFUJI Hideaki hopopt[1] = 0; /* length of HbH */ 50608b202b6SYOSHIFUJI Hideaki hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */ 50708b202b6SYOSHIFUJI Hideaki hopopt[3] = 2; /* Length of RA Option */ 50808b202b6SYOSHIFUJI Hideaki hopopt[4] = 0; /* Type = 0x0000 (MLD) */ 50908b202b6SYOSHIFUJI Hideaki hopopt[5] = 0; 5101de5a71cSEldad Zack hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */ 5111de5a71cSEldad Zack hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */ 51208b202b6SYOSHIFUJI Hideaki 51308b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*ip6h) + 8); 51408b202b6SYOSHIFUJI Hideaki 51508b202b6SYOSHIFUJI Hideaki /* ICMPv6 */ 51608b202b6SYOSHIFUJI Hideaki skb_set_transport_header(skb, skb->len); 51708b202b6SYOSHIFUJI Hideaki mldq = (struct mld_msg *) icmp6_hdr(skb); 51808b202b6SYOSHIFUJI Hideaki 5191080ab95SNikolay Aleksandrov interval = ipv6_addr_any(grp) ? 52032de868cSLinus Lüssing br->multicast_query_response_interval : 52132de868cSLinus Lüssing br->multicast_last_member_interval; 52208b202b6SYOSHIFUJI Hideaki 5231080ab95SNikolay Aleksandrov *igmp_type = ICMPV6_MGM_QUERY; 52408b202b6SYOSHIFUJI Hideaki mldq->mld_type = ICMPV6_MGM_QUERY; 52508b202b6SYOSHIFUJI Hideaki mldq->mld_code = 0; 52608b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = 0; 52708b202b6SYOSHIFUJI Hideaki mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval)); 52808b202b6SYOSHIFUJI Hideaki mldq->mld_reserved = 0; 5291080ab95SNikolay Aleksandrov mldq->mld_mca = *grp; 53008b202b6SYOSHIFUJI Hideaki 53108b202b6SYOSHIFUJI Hideaki /* checksum */ 53208b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, 53308b202b6SYOSHIFUJI Hideaki sizeof(*mldq), IPPROTO_ICMPV6, 53408b202b6SYOSHIFUJI Hideaki csum_partial(mldq, 53508b202b6SYOSHIFUJI Hideaki sizeof(*mldq), 0)); 53608b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*mldq)); 53708b202b6SYOSHIFUJI Hideaki 53808b202b6SYOSHIFUJI Hideaki __skb_pull(skb, sizeof(*eth)); 53908b202b6SYOSHIFUJI Hideaki 54008b202b6SYOSHIFUJI Hideaki out: 54108b202b6SYOSHIFUJI Hideaki return skb; 54208b202b6SYOSHIFUJI Hideaki } 54308b202b6SYOSHIFUJI Hideaki #endif 54408b202b6SYOSHIFUJI Hideaki 5458ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br, 5461080ab95SNikolay Aleksandrov struct br_ip *addr, 5471080ab95SNikolay Aleksandrov u8 *igmp_type) 5488ef2a9a5SYOSHIFUJI Hideaki { 5498ef2a9a5SYOSHIFUJI Hideaki switch (addr->proto) { 5508ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 5511080ab95SNikolay Aleksandrov return br_ip4_multicast_alloc_query(br, addr->u.ip4, igmp_type); 552dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 55308b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 5541080ab95SNikolay Aleksandrov return br_ip6_multicast_alloc_query(br, &addr->u.ip6, 5551080ab95SNikolay Aleksandrov igmp_type); 55608b202b6SYOSHIFUJI Hideaki #endif 5578ef2a9a5SYOSHIFUJI Hideaki } 5588ef2a9a5SYOSHIFUJI Hideaki return NULL; 5598ef2a9a5SYOSHIFUJI Hideaki } 5608ef2a9a5SYOSHIFUJI Hideaki 561eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *br_multicast_get_group( 5628ef2a9a5SYOSHIFUJI Hideaki struct net_bridge *br, struct net_bridge_port *port, 5638ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group, int hash) 564eb1d1641SHerbert Xu { 565e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 566eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 56795c96174SEric Dumazet unsigned int count = 0; 56895c96174SEric Dumazet unsigned int max; 569eb1d1641SHerbert Xu int elasticity; 570eb1d1641SHerbert Xu int err; 571eb1d1641SHerbert Xu 572e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 573b67bfe0dSSasha Levin hlist_for_each_entry(mp, &mdb->mhash[hash], hlist[mdb->ver]) { 574eb1d1641SHerbert Xu count++; 5758ef2a9a5SYOSHIFUJI Hideaki if (unlikely(br_ip_equal(group, &mp->addr))) 576eb1d1641SHerbert Xu return mp; 577eb1d1641SHerbert Xu } 578eb1d1641SHerbert Xu 579eb1d1641SHerbert Xu elasticity = 0; 580eb1d1641SHerbert Xu max = mdb->max; 581eb1d1641SHerbert Xu 582eb1d1641SHerbert Xu if (unlikely(count > br->hash_elasticity && count)) { 583eb1d1641SHerbert Xu if (net_ratelimit()) 58428a16c97Sstephen hemminger br_info(br, "Multicast hash table " 585eb1d1641SHerbert Xu "chain limit reached: %s\n", 58628a16c97Sstephen hemminger port ? port->dev->name : br->dev->name); 587eb1d1641SHerbert Xu 588eb1d1641SHerbert Xu elasticity = br->hash_elasticity; 589eb1d1641SHerbert Xu } 590eb1d1641SHerbert Xu 591eb1d1641SHerbert Xu if (mdb->size >= max) { 592eb1d1641SHerbert Xu max *= 2; 593036be6dbSThomas Graf if (unlikely(max > br->hash_max)) { 594036be6dbSThomas Graf br_warn(br, "Multicast hash table maximum of %d " 595036be6dbSThomas Graf "reached, disabling snooping: %s\n", 596036be6dbSThomas Graf br->hash_max, 597036be6dbSThomas Graf port ? port->dev->name : br->dev->name); 598eb1d1641SHerbert Xu err = -E2BIG; 599eb1d1641SHerbert Xu disable: 600eb1d1641SHerbert Xu br->multicast_disabled = 1; 601eb1d1641SHerbert Xu goto err; 602eb1d1641SHerbert Xu } 603eb1d1641SHerbert Xu } 604eb1d1641SHerbert Xu 605eb1d1641SHerbert Xu if (max > mdb->max || elasticity) { 606eb1d1641SHerbert Xu if (mdb->old) { 607eb1d1641SHerbert Xu if (net_ratelimit()) 60828a16c97Sstephen hemminger br_info(br, "Multicast hash table " 609eb1d1641SHerbert Xu "on fire: %s\n", 61028a16c97Sstephen hemminger port ? port->dev->name : br->dev->name); 611eb1d1641SHerbert Xu err = -EEXIST; 612eb1d1641SHerbert Xu goto err; 613eb1d1641SHerbert Xu } 614eb1d1641SHerbert Xu 615eb1d1641SHerbert Xu err = br_mdb_rehash(&br->mdb, max, elasticity); 616eb1d1641SHerbert Xu if (err) { 61728a16c97Sstephen hemminger br_warn(br, "Cannot rehash multicast " 61828a16c97Sstephen hemminger "hash table, disabling snooping: %s, %d, %d\n", 61928a16c97Sstephen hemminger port ? port->dev->name : br->dev->name, 620eb1d1641SHerbert Xu mdb->size, err); 621eb1d1641SHerbert Xu goto disable; 622eb1d1641SHerbert Xu } 623eb1d1641SHerbert Xu 624eb1d1641SHerbert Xu err = -EAGAIN; 625eb1d1641SHerbert Xu goto err; 626eb1d1641SHerbert Xu } 627eb1d1641SHerbert Xu 628eb1d1641SHerbert Xu return NULL; 629eb1d1641SHerbert Xu 630eb1d1641SHerbert Xu err: 631eb1d1641SHerbert Xu mp = ERR_PTR(err); 632eb1d1641SHerbert Xu return mp; 633eb1d1641SHerbert Xu } 634eb1d1641SHerbert Xu 635cfd56754SCong Wang struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br, 636*5e923585SNikolay Aleksandrov struct net_bridge_port *p, 637*5e923585SNikolay Aleksandrov struct br_ip *group) 638eb1d1641SHerbert Xu { 639e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 640eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 641eb1d1641SHerbert Xu int hash; 6424c0833bcSTobias Klauser int err; 643eb1d1641SHerbert Xu 644e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 645eb1d1641SHerbert Xu if (!mdb) { 6464c0833bcSTobias Klauser err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0); 6474c0833bcSTobias Klauser if (err) 6484c0833bcSTobias Klauser return ERR_PTR(err); 649eb1d1641SHerbert Xu goto rehash; 650eb1d1641SHerbert Xu } 651eb1d1641SHerbert Xu 652eb1d1641SHerbert Xu hash = br_ip_hash(mdb, group); 653*5e923585SNikolay Aleksandrov mp = br_multicast_get_group(br, p, group, hash); 654eb1d1641SHerbert Xu switch (PTR_ERR(mp)) { 655eb1d1641SHerbert Xu case 0: 656eb1d1641SHerbert Xu break; 657eb1d1641SHerbert Xu 658eb1d1641SHerbert Xu case -EAGAIN: 659eb1d1641SHerbert Xu rehash: 660e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 661eb1d1641SHerbert Xu hash = br_ip_hash(mdb, group); 662eb1d1641SHerbert Xu break; 663eb1d1641SHerbert Xu 664eb1d1641SHerbert Xu default: 665eb1d1641SHerbert Xu goto out; 666eb1d1641SHerbert Xu } 667eb1d1641SHerbert Xu 668eb1d1641SHerbert Xu mp = kzalloc(sizeof(*mp), GFP_ATOMIC); 669eb1d1641SHerbert Xu if (unlikely(!mp)) 6704c0833bcSTobias Klauser return ERR_PTR(-ENOMEM); 671eb1d1641SHerbert Xu 672eb1d1641SHerbert Xu mp->br = br; 6738ef2a9a5SYOSHIFUJI Hideaki mp->addr = *group; 6741faabf2aSEric Dumazet setup_timer(&mp->timer, br_multicast_group_expired, 6751faabf2aSEric Dumazet (unsigned long)mp); 6761faabf2aSEric Dumazet 677eb1d1641SHerbert Xu hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]); 678eb1d1641SHerbert Xu mdb->size++; 679eb1d1641SHerbert Xu 680eb1d1641SHerbert Xu out: 681eb1d1641SHerbert Xu return mp; 682eb1d1641SHerbert Xu } 683eb1d1641SHerbert Xu 684cfd56754SCong Wang struct net_bridge_port_group *br_multicast_new_port_group( 685cfd56754SCong Wang struct net_bridge_port *port, 686cfd56754SCong Wang struct br_ip *group, 687ccb1c31aSAmerigo Wang struct net_bridge_port_group __rcu *next, 6889d06b6d8SElad Raz unsigned char flags) 689cfd56754SCong Wang { 690cfd56754SCong Wang struct net_bridge_port_group *p; 691cfd56754SCong Wang 692cfd56754SCong Wang p = kzalloc(sizeof(*p), GFP_ATOMIC); 693cfd56754SCong Wang if (unlikely(!p)) 694cfd56754SCong Wang return NULL; 695cfd56754SCong Wang 696cfd56754SCong Wang p->addr = *group; 697cfd56754SCong Wang p->port = port; 6989d06b6d8SElad Raz p->flags = flags; 699eca2a43bSstephen hemminger rcu_assign_pointer(p->next, next); 700cfd56754SCong Wang hlist_add_head(&p->mglist, &port->mglist); 701cfd56754SCong Wang setup_timer(&p->timer, br_multicast_port_group_expired, 702cfd56754SCong Wang (unsigned long)p); 703cfd56754SCong Wang return p; 704cfd56754SCong Wang } 705cfd56754SCong Wang 706eb1d1641SHerbert Xu static int br_multicast_add_group(struct net_bridge *br, 7078ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 7088ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group) 709eb1d1641SHerbert Xu { 710e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 711*5e923585SNikolay Aleksandrov struct net_bridge_port_group *p; 712*5e923585SNikolay Aleksandrov struct net_bridge_mdb_entry *mp; 713454594f3SLinus Lüssing unsigned long now = jiffies; 714eb1d1641SHerbert Xu int err; 715eb1d1641SHerbert Xu 716eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 717eb1d1641SHerbert Xu if (!netif_running(br->dev) || 718eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 719eb1d1641SHerbert Xu goto out; 720eb1d1641SHerbert Xu 721eb1d1641SHerbert Xu mp = br_multicast_new_group(br, port, group); 722eb1d1641SHerbert Xu err = PTR_ERR(mp); 7234c0833bcSTobias Klauser if (IS_ERR(mp)) 724eb1d1641SHerbert Xu goto err; 725eb1d1641SHerbert Xu 726eb1d1641SHerbert Xu if (!port) { 7278a870178SHerbert Xu mp->mglist = true; 728454594f3SLinus Lüssing mod_timer(&mp->timer, now + br->multicast_membership_interval); 729eb1d1641SHerbert Xu goto out; 730eb1d1641SHerbert Xu } 731eb1d1641SHerbert Xu 732e8051688SEric Dumazet for (pp = &mp->ports; 733e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 734e8051688SEric Dumazet pp = &p->next) { 735eb1d1641SHerbert Xu if (p->port == port) 736454594f3SLinus Lüssing goto found; 737eb1d1641SHerbert Xu if ((unsigned long)p->port < (unsigned long)port) 738eb1d1641SHerbert Xu break; 739eb1d1641SHerbert Xu } 740eb1d1641SHerbert Xu 7419d06b6d8SElad Raz p = br_multicast_new_port_group(port, group, *pp, 0); 742eb1d1641SHerbert Xu if (unlikely(!p)) 743eb1d1641SHerbert Xu goto err; 744eb1d1641SHerbert Xu rcu_assign_pointer(*pp, p); 74545ebcce5SElad Raz br_mdb_notify(br->dev, port, group, RTM_NEWMDB, 0); 746eb1d1641SHerbert Xu 747454594f3SLinus Lüssing found: 748454594f3SLinus Lüssing mod_timer(&p->timer, now + br->multicast_membership_interval); 749eb1d1641SHerbert Xu out: 750eb1d1641SHerbert Xu err = 0; 751eb1d1641SHerbert Xu 752eb1d1641SHerbert Xu err: 753eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 754eb1d1641SHerbert Xu return err; 755eb1d1641SHerbert Xu } 756eb1d1641SHerbert Xu 7578ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_add_group(struct net_bridge *br, 7588ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 759b0e9a30dSVlad Yasevich __be32 group, 760b0e9a30dSVlad Yasevich __u16 vid) 7618ef2a9a5SYOSHIFUJI Hideaki { 7628ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 7638ef2a9a5SYOSHIFUJI Hideaki 7648ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 7658ef2a9a5SYOSHIFUJI Hideaki return 0; 7668ef2a9a5SYOSHIFUJI Hideaki 7678ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 7688ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 769b0e9a30dSVlad Yasevich br_group.vid = vid; 7708ef2a9a5SYOSHIFUJI Hideaki 7718ef2a9a5SYOSHIFUJI Hideaki return br_multicast_add_group(br, port, &br_group); 7728ef2a9a5SYOSHIFUJI Hideaki } 7738ef2a9a5SYOSHIFUJI Hideaki 774dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 77508b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_add_group(struct net_bridge *br, 77608b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 777b0e9a30dSVlad Yasevich const struct in6_addr *group, 778b0e9a30dSVlad Yasevich __u16 vid) 77908b202b6SYOSHIFUJI Hideaki { 78008b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 78108b202b6SYOSHIFUJI Hideaki 7823c3769e6SLinus Lüssing if (ipv6_addr_is_ll_all_nodes(group)) 78308b202b6SYOSHIFUJI Hideaki return 0; 78408b202b6SYOSHIFUJI Hideaki 7854e3fd7a0SAlexey Dobriyan br_group.u.ip6 = *group; 7869cc6e0c4SLinus Lüssing br_group.proto = htons(ETH_P_IPV6); 787b0e9a30dSVlad Yasevich br_group.vid = vid; 78808b202b6SYOSHIFUJI Hideaki 78908b202b6SYOSHIFUJI Hideaki return br_multicast_add_group(br, port, &br_group); 79008b202b6SYOSHIFUJI Hideaki } 79108b202b6SYOSHIFUJI Hideaki #endif 79208b202b6SYOSHIFUJI Hideaki 793eb1d1641SHerbert Xu static void br_multicast_router_expired(unsigned long data) 794eb1d1641SHerbert Xu { 795eb1d1641SHerbert Xu struct net_bridge_port *port = (void *)data; 796eb1d1641SHerbert Xu struct net_bridge *br = port->br; 797eb1d1641SHerbert Xu 798eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 799a55d8246SNikolay Aleksandrov if (port->multicast_router == MDB_RTR_TYPE_DISABLED || 800a55d8246SNikolay Aleksandrov port->multicast_router == MDB_RTR_TYPE_PERM || 801eb1d1641SHerbert Xu timer_pending(&port->multicast_router_timer) || 802eb1d1641SHerbert Xu hlist_unhashed(&port->rlist)) 803eb1d1641SHerbert Xu goto out; 804eb1d1641SHerbert Xu 805eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 806949f1e39SSatish Ashok br_rtr_notify(br->dev, port, RTM_DELMDB); 807a55d8246SNikolay Aleksandrov /* Don't allow timer refresh if the router expired */ 808a55d8246SNikolay Aleksandrov if (port->multicast_router == MDB_RTR_TYPE_TEMP) 809a55d8246SNikolay Aleksandrov port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 810eb1d1641SHerbert Xu 811eb1d1641SHerbert Xu out: 812eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 813eb1d1641SHerbert Xu } 814eb1d1641SHerbert Xu 815eb1d1641SHerbert Xu static void br_multicast_local_router_expired(unsigned long data) 816eb1d1641SHerbert Xu { 817eb1d1641SHerbert Xu } 818eb1d1641SHerbert Xu 819cc0fdd80SLinus Lüssing static void br_multicast_querier_expired(struct net_bridge *br, 82090010b36SLinus Lüssing struct bridge_mcast_own_query *query) 821c83b8fabSHerbert Xu { 822c83b8fabSHerbert Xu spin_lock(&br->multicast_lock); 823c83b8fabSHerbert Xu if (!netif_running(br->dev) || br->multicast_disabled) 824c83b8fabSHerbert Xu goto out; 825c83b8fabSHerbert Xu 826cc0fdd80SLinus Lüssing br_multicast_start_querier(br, query); 827c83b8fabSHerbert Xu 828c83b8fabSHerbert Xu out: 829c83b8fabSHerbert Xu spin_unlock(&br->multicast_lock); 830c83b8fabSHerbert Xu } 831c83b8fabSHerbert Xu 832cc0fdd80SLinus Lüssing static void br_ip4_multicast_querier_expired(unsigned long data) 833cc0fdd80SLinus Lüssing { 834cc0fdd80SLinus Lüssing struct net_bridge *br = (void *)data; 835cc0fdd80SLinus Lüssing 83690010b36SLinus Lüssing br_multicast_querier_expired(br, &br->ip4_own_query); 837cc0fdd80SLinus Lüssing } 838cc0fdd80SLinus Lüssing 839cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 840cc0fdd80SLinus Lüssing static void br_ip6_multicast_querier_expired(unsigned long data) 841cc0fdd80SLinus Lüssing { 842cc0fdd80SLinus Lüssing struct net_bridge *br = (void *)data; 843cc0fdd80SLinus Lüssing 84490010b36SLinus Lüssing br_multicast_querier_expired(br, &br->ip6_own_query); 845cc0fdd80SLinus Lüssing } 846cc0fdd80SLinus Lüssing #endif 847cc0fdd80SLinus Lüssing 848dc4eb53aSLinus Lüssing static void br_multicast_select_own_querier(struct net_bridge *br, 849dc4eb53aSLinus Lüssing struct br_ip *ip, 850dc4eb53aSLinus Lüssing struct sk_buff *skb) 851dc4eb53aSLinus Lüssing { 852dc4eb53aSLinus Lüssing if (ip->proto == htons(ETH_P_IP)) 853dc4eb53aSLinus Lüssing br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr; 854dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 855dc4eb53aSLinus Lüssing else 856dc4eb53aSLinus Lüssing br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr; 857dc4eb53aSLinus Lüssing #endif 858dc4eb53aSLinus Lüssing } 859dc4eb53aSLinus Lüssing 8608ef2a9a5SYOSHIFUJI Hideaki static void __br_multicast_send_query(struct net_bridge *br, 8618ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 8628ef2a9a5SYOSHIFUJI Hideaki struct br_ip *ip) 863eb1d1641SHerbert Xu { 864eb1d1641SHerbert Xu struct sk_buff *skb; 8651080ab95SNikolay Aleksandrov u8 igmp_type; 866eb1d1641SHerbert Xu 8671080ab95SNikolay Aleksandrov skb = br_multicast_alloc_query(br, ip, &igmp_type); 868eb1d1641SHerbert Xu if (!skb) 8698ef2a9a5SYOSHIFUJI Hideaki return; 870eb1d1641SHerbert Xu 871eb1d1641SHerbert Xu if (port) { 872eb1d1641SHerbert Xu skb->dev = port->dev; 873a65056ecSNikolay Aleksandrov br_multicast_count(br, port, skb, igmp_type, 8741080ab95SNikolay Aleksandrov BR_MCAST_DIR_TX); 87529a26a56SEric W. Biederman NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, 87629a26a56SEric W. Biederman dev_net(port->dev), NULL, skb, NULL, skb->dev, 877f0b4eeceSLinus Lüssing br_dev_queue_push_xmit); 878dc4eb53aSLinus Lüssing } else { 879dc4eb53aSLinus Lüssing br_multicast_select_own_querier(br, ip, skb); 880a65056ecSNikolay Aleksandrov br_multicast_count(br, port, skb, igmp_type, 8811080ab95SNikolay Aleksandrov BR_MCAST_DIR_RX); 882eb1d1641SHerbert Xu netif_rx(skb); 8838ef2a9a5SYOSHIFUJI Hideaki } 884dc4eb53aSLinus Lüssing } 885eb1d1641SHerbert Xu 8868ef2a9a5SYOSHIFUJI Hideaki static void br_multicast_send_query(struct net_bridge *br, 887cc0fdd80SLinus Lüssing struct net_bridge_port *port, 88890010b36SLinus Lüssing struct bridge_mcast_own_query *own_query) 8898ef2a9a5SYOSHIFUJI Hideaki { 89090010b36SLinus Lüssing struct bridge_mcast_other_query *other_query = NULL; 891*5e923585SNikolay Aleksandrov struct br_ip br_group; 892*5e923585SNikolay Aleksandrov unsigned long time; 8938ef2a9a5SYOSHIFUJI Hideaki 8948ef2a9a5SYOSHIFUJI Hideaki if (!netif_running(br->dev) || br->multicast_disabled || 895cc0fdd80SLinus Lüssing !br->multicast_querier) 8968ef2a9a5SYOSHIFUJI Hideaki return; 8978ef2a9a5SYOSHIFUJI Hideaki 89808b202b6SYOSHIFUJI Hideaki memset(&br_group.u, 0, sizeof(br_group.u)); 8998ef2a9a5SYOSHIFUJI Hideaki 90090010b36SLinus Lüssing if (port ? (own_query == &port->ip4_own_query) : 90190010b36SLinus Lüssing (own_query == &br->ip4_own_query)) { 90290010b36SLinus Lüssing other_query = &br->ip4_other_query; 90308b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 904dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 905cc0fdd80SLinus Lüssing } else { 90690010b36SLinus Lüssing other_query = &br->ip6_other_query; 90708b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 90808b202b6SYOSHIFUJI Hideaki #endif 909eb1d1641SHerbert Xu } 910eb1d1641SHerbert Xu 91190010b36SLinus Lüssing if (!other_query || timer_pending(&other_query->timer)) 912cc0fdd80SLinus Lüssing return; 913cc0fdd80SLinus Lüssing 914cc0fdd80SLinus Lüssing __br_multicast_send_query(br, port, &br_group); 915cc0fdd80SLinus Lüssing 916cc0fdd80SLinus Lüssing time = jiffies; 91790010b36SLinus Lüssing time += own_query->startup_sent < br->multicast_startup_query_count ? 918cc0fdd80SLinus Lüssing br->multicast_startup_query_interval : 919cc0fdd80SLinus Lüssing br->multicast_query_interval; 92090010b36SLinus Lüssing mod_timer(&own_query->timer, time); 921cc0fdd80SLinus Lüssing } 922cc0fdd80SLinus Lüssing 92390010b36SLinus Lüssing static void 92490010b36SLinus Lüssing br_multicast_port_query_expired(struct net_bridge_port *port, 92590010b36SLinus Lüssing struct bridge_mcast_own_query *query) 926eb1d1641SHerbert Xu { 927eb1d1641SHerbert Xu struct net_bridge *br = port->br; 928eb1d1641SHerbert Xu 929eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 93002a780c0SDan Carpenter if (port->state == BR_STATE_DISABLED || 93102a780c0SDan Carpenter port->state == BR_STATE_BLOCKING) 932eb1d1641SHerbert Xu goto out; 933eb1d1641SHerbert Xu 934cc0fdd80SLinus Lüssing if (query->startup_sent < br->multicast_startup_query_count) 935cc0fdd80SLinus Lüssing query->startup_sent++; 936eb1d1641SHerbert Xu 937cc0fdd80SLinus Lüssing br_multicast_send_query(port->br, port, query); 938eb1d1641SHerbert Xu 939eb1d1641SHerbert Xu out: 940eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 941eb1d1641SHerbert Xu } 942eb1d1641SHerbert Xu 943cc0fdd80SLinus Lüssing static void br_ip4_multicast_port_query_expired(unsigned long data) 944cc0fdd80SLinus Lüssing { 945cc0fdd80SLinus Lüssing struct net_bridge_port *port = (void *)data; 946cc0fdd80SLinus Lüssing 94790010b36SLinus Lüssing br_multicast_port_query_expired(port, &port->ip4_own_query); 948cc0fdd80SLinus Lüssing } 949cc0fdd80SLinus Lüssing 950cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 951cc0fdd80SLinus Lüssing static void br_ip6_multicast_port_query_expired(unsigned long data) 952cc0fdd80SLinus Lüssing { 953cc0fdd80SLinus Lüssing struct net_bridge_port *port = (void *)data; 954cc0fdd80SLinus Lüssing 95590010b36SLinus Lüssing br_multicast_port_query_expired(port, &port->ip6_own_query); 956cc0fdd80SLinus Lüssing } 957cc0fdd80SLinus Lüssing #endif 958cc0fdd80SLinus Lüssing 9591080ab95SNikolay Aleksandrov int br_multicast_add_port(struct net_bridge_port *port) 960eb1d1641SHerbert Xu { 9617f0aec7aSNikolay Aleksandrov port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 962eb1d1641SHerbert Xu 963eb1d1641SHerbert Xu setup_timer(&port->multicast_router_timer, br_multicast_router_expired, 964eb1d1641SHerbert Xu (unsigned long)port); 96590010b36SLinus Lüssing setup_timer(&port->ip4_own_query.timer, 96690010b36SLinus Lüssing br_ip4_multicast_port_query_expired, (unsigned long)port); 967cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 96890010b36SLinus Lüssing setup_timer(&port->ip6_own_query.timer, 96990010b36SLinus Lüssing br_ip6_multicast_port_query_expired, (unsigned long)port); 970cc0fdd80SLinus Lüssing #endif 9711080ab95SNikolay Aleksandrov port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats); 9721080ab95SNikolay Aleksandrov if (!port->mcast_stats) 9731080ab95SNikolay Aleksandrov return -ENOMEM; 9741080ab95SNikolay Aleksandrov 9751080ab95SNikolay Aleksandrov return 0; 976eb1d1641SHerbert Xu } 977eb1d1641SHerbert Xu 978eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port) 979eb1d1641SHerbert Xu { 980e10177abSSatish Ashok struct net_bridge *br = port->br; 981e10177abSSatish Ashok struct net_bridge_port_group *pg; 982e10177abSSatish Ashok struct hlist_node *n; 983e10177abSSatish Ashok 984e10177abSSatish Ashok /* Take care of the remaining groups, only perm ones should be left */ 985e10177abSSatish Ashok spin_lock_bh(&br->multicast_lock); 986e10177abSSatish Ashok hlist_for_each_entry_safe(pg, n, &port->mglist, mglist) 987e10177abSSatish Ashok br_multicast_del_pg(br, pg); 988e10177abSSatish Ashok spin_unlock_bh(&br->multicast_lock); 989eb1d1641SHerbert Xu del_timer_sync(&port->multicast_router_timer); 9901080ab95SNikolay Aleksandrov free_percpu(port->mcast_stats); 991eb1d1641SHerbert Xu } 992eb1d1641SHerbert Xu 99390010b36SLinus Lüssing static void br_multicast_enable(struct bridge_mcast_own_query *query) 994561f1103SHerbert Xu { 995cc0fdd80SLinus Lüssing query->startup_sent = 0; 996561f1103SHerbert Xu 997cc0fdd80SLinus Lüssing if (try_to_del_timer_sync(&query->timer) >= 0 || 998cc0fdd80SLinus Lüssing del_timer(&query->timer)) 999cc0fdd80SLinus Lüssing mod_timer(&query->timer, jiffies); 1000561f1103SHerbert Xu } 1001561f1103SHerbert Xu 10027cb3f921SNikolay Aleksandrov static void __br_multicast_enable_port(struct net_bridge_port *port) 1003eb1d1641SHerbert Xu { 1004eb1d1641SHerbert Xu struct net_bridge *br = port->br; 1005eb1d1641SHerbert Xu 1006eb1d1641SHerbert Xu if (br->multicast_disabled || !netif_running(br->dev)) 10077cb3f921SNikolay Aleksandrov return; 1008eb1d1641SHerbert Xu 100990010b36SLinus Lüssing br_multicast_enable(&port->ip4_own_query); 1010cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 101190010b36SLinus Lüssing br_multicast_enable(&port->ip6_own_query); 1012cc0fdd80SLinus Lüssing #endif 10137f0aec7aSNikolay Aleksandrov if (port->multicast_router == MDB_RTR_TYPE_PERM && 10147f0aec7aSNikolay Aleksandrov hlist_unhashed(&port->rlist)) 1015754bc547SSatish Ashok br_multicast_add_router(br, port); 10167cb3f921SNikolay Aleksandrov } 1017eb1d1641SHerbert Xu 10187cb3f921SNikolay Aleksandrov void br_multicast_enable_port(struct net_bridge_port *port) 10197cb3f921SNikolay Aleksandrov { 10207cb3f921SNikolay Aleksandrov struct net_bridge *br = port->br; 10217cb3f921SNikolay Aleksandrov 10227cb3f921SNikolay Aleksandrov spin_lock(&br->multicast_lock); 10237cb3f921SNikolay Aleksandrov __br_multicast_enable_port(port); 1024eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1025eb1d1641SHerbert Xu } 1026eb1d1641SHerbert Xu 1027eb1d1641SHerbert Xu void br_multicast_disable_port(struct net_bridge_port *port) 1028eb1d1641SHerbert Xu { 1029eb1d1641SHerbert Xu struct net_bridge *br = port->br; 1030eb1d1641SHerbert Xu struct net_bridge_port_group *pg; 1031b67bfe0dSSasha Levin struct hlist_node *n; 1032eb1d1641SHerbert Xu 1033eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1034b67bfe0dSSasha Levin hlist_for_each_entry_safe(pg, n, &port->mglist, mglist) 10359d06b6d8SElad Raz if (!(pg->flags & MDB_PG_FLAGS_PERMANENT)) 1036eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 1037eb1d1641SHerbert Xu 1038949f1e39SSatish Ashok if (!hlist_unhashed(&port->rlist)) { 1039eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 1040949f1e39SSatish Ashok br_rtr_notify(br->dev, port, RTM_DELMDB); 1041a55d8246SNikolay Aleksandrov /* Don't allow timer refresh if disabling */ 1042a55d8246SNikolay Aleksandrov if (port->multicast_router == MDB_RTR_TYPE_TEMP) 1043a55d8246SNikolay Aleksandrov port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 1044949f1e39SSatish Ashok } 1045eb1d1641SHerbert Xu del_timer(&port->multicast_router_timer); 104690010b36SLinus Lüssing del_timer(&port->ip4_own_query.timer); 1047cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 104890010b36SLinus Lüssing del_timer(&port->ip6_own_query.timer); 1049cc0fdd80SLinus Lüssing #endif 1050eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1051eb1d1641SHerbert Xu } 1052eb1d1641SHerbert Xu 10538ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_igmp3_report(struct net_bridge *br, 1054eb1d1641SHerbert Xu struct net_bridge_port *port, 105506499098SVlad Yasevich struct sk_buff *skb, 105606499098SVlad Yasevich u16 vid) 1057eb1d1641SHerbert Xu { 1058eb1d1641SHerbert Xu struct igmpv3_report *ih; 1059eb1d1641SHerbert Xu struct igmpv3_grec *grec; 1060eb1d1641SHerbert Xu int i; 1061eb1d1641SHerbert Xu int len; 1062eb1d1641SHerbert Xu int num; 1063eb1d1641SHerbert Xu int type; 1064eb1d1641SHerbert Xu int err = 0; 1065eb1d1641SHerbert Xu __be32 group; 1066eb1d1641SHerbert Xu 1067eb1d1641SHerbert Xu ih = igmpv3_report_hdr(skb); 1068eb1d1641SHerbert Xu num = ntohs(ih->ngrec); 1069c2d4fbd2SLinus Lüssing len = skb_transport_offset(skb) + sizeof(*ih); 1070eb1d1641SHerbert Xu 1071eb1d1641SHerbert Xu for (i = 0; i < num; i++) { 1072eb1d1641SHerbert Xu len += sizeof(*grec); 1073eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 1074eb1d1641SHerbert Xu return -EINVAL; 1075eb1d1641SHerbert Xu 1076fd218cf9SHerbert Xu grec = (void *)(skb->data + len - sizeof(*grec)); 1077eb1d1641SHerbert Xu group = grec->grec_mca; 1078eb1d1641SHerbert Xu type = grec->grec_type; 1079eb1d1641SHerbert Xu 10808eabf95cSEric Dumazet len += ntohs(grec->grec_nsrcs) * 4; 1081eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 1082eb1d1641SHerbert Xu return -EINVAL; 1083eb1d1641SHerbert Xu 1084eb1d1641SHerbert Xu /* We treat this as an IGMPv2 report for now. */ 1085eb1d1641SHerbert Xu switch (type) { 1086eb1d1641SHerbert Xu case IGMPV3_MODE_IS_INCLUDE: 1087eb1d1641SHerbert Xu case IGMPV3_MODE_IS_EXCLUDE: 1088eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_INCLUDE: 1089eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_EXCLUDE: 1090eb1d1641SHerbert Xu case IGMPV3_ALLOW_NEW_SOURCES: 1091eb1d1641SHerbert Xu case IGMPV3_BLOCK_OLD_SOURCES: 1092eb1d1641SHerbert Xu break; 1093eb1d1641SHerbert Xu 1094eb1d1641SHerbert Xu default: 1095eb1d1641SHerbert Xu continue; 1096eb1d1641SHerbert Xu } 1097eb1d1641SHerbert Xu 1098bc8c20acSSatish Ashok if ((type == IGMPV3_CHANGE_TO_INCLUDE || 1099bc8c20acSSatish Ashok type == IGMPV3_MODE_IS_INCLUDE) && 1100bc8c20acSSatish Ashok ntohs(grec->grec_nsrcs) == 0) { 1101bc8c20acSSatish Ashok br_ip4_multicast_leave_group(br, port, group, vid); 1102bc8c20acSSatish Ashok } else { 1103b0e9a30dSVlad Yasevich err = br_ip4_multicast_add_group(br, port, group, vid); 1104eb1d1641SHerbert Xu if (err) 1105eb1d1641SHerbert Xu break; 1106eb1d1641SHerbert Xu } 1107bc8c20acSSatish Ashok } 1108eb1d1641SHerbert Xu 1109eb1d1641SHerbert Xu return err; 1110eb1d1641SHerbert Xu } 1111eb1d1641SHerbert Xu 1112dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 111308b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_mld2_report(struct net_bridge *br, 111408b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 111506499098SVlad Yasevich struct sk_buff *skb, 111606499098SVlad Yasevich u16 vid) 111708b202b6SYOSHIFUJI Hideaki { 111808b202b6SYOSHIFUJI Hideaki struct icmp6hdr *icmp6h; 111908b202b6SYOSHIFUJI Hideaki struct mld2_grec *grec; 112008b202b6SYOSHIFUJI Hideaki int i; 112108b202b6SYOSHIFUJI Hideaki int len; 112208b202b6SYOSHIFUJI Hideaki int num; 112308b202b6SYOSHIFUJI Hideaki int err = 0; 112408b202b6SYOSHIFUJI Hideaki 112508b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*icmp6h))) 112608b202b6SYOSHIFUJI Hideaki return -EINVAL; 112708b202b6SYOSHIFUJI Hideaki 112808b202b6SYOSHIFUJI Hideaki icmp6h = icmp6_hdr(skb); 112908b202b6SYOSHIFUJI Hideaki num = ntohs(icmp6h->icmp6_dataun.un_data16[1]); 1130c2d4fbd2SLinus Lüssing len = skb_transport_offset(skb) + sizeof(*icmp6h); 113108b202b6SYOSHIFUJI Hideaki 113208b202b6SYOSHIFUJI Hideaki for (i = 0; i < num; i++) { 113308b202b6SYOSHIFUJI Hideaki __be16 *nsrcs, _nsrcs; 113408b202b6SYOSHIFUJI Hideaki 113508b202b6SYOSHIFUJI Hideaki nsrcs = skb_header_pointer(skb, 113608b202b6SYOSHIFUJI Hideaki len + offsetof(struct mld2_grec, 1137649e984dSLinus Lüssing grec_nsrcs), 113808b202b6SYOSHIFUJI Hideaki sizeof(_nsrcs), &_nsrcs); 113908b202b6SYOSHIFUJI Hideaki if (!nsrcs) 114008b202b6SYOSHIFUJI Hideaki return -EINVAL; 114108b202b6SYOSHIFUJI Hideaki 114208b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, 114308b202b6SYOSHIFUJI Hideaki len + sizeof(*grec) + 1144d41db9f3SLinus Lüssing sizeof(struct in6_addr) * ntohs(*nsrcs))) 114508b202b6SYOSHIFUJI Hideaki return -EINVAL; 114608b202b6SYOSHIFUJI Hideaki 114708b202b6SYOSHIFUJI Hideaki grec = (struct mld2_grec *)(skb->data + len); 1148d41db9f3SLinus Lüssing len += sizeof(*grec) + 1149d41db9f3SLinus Lüssing sizeof(struct in6_addr) * ntohs(*nsrcs); 115008b202b6SYOSHIFUJI Hideaki 115108b202b6SYOSHIFUJI Hideaki /* We treat these as MLDv1 reports for now. */ 115208b202b6SYOSHIFUJI Hideaki switch (grec->grec_type) { 115308b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_INCLUDE: 115408b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_EXCLUDE: 115508b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_INCLUDE: 115608b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_EXCLUDE: 115708b202b6SYOSHIFUJI Hideaki case MLD2_ALLOW_NEW_SOURCES: 115808b202b6SYOSHIFUJI Hideaki case MLD2_BLOCK_OLD_SOURCES: 115908b202b6SYOSHIFUJI Hideaki break; 116008b202b6SYOSHIFUJI Hideaki 116108b202b6SYOSHIFUJI Hideaki default: 116208b202b6SYOSHIFUJI Hideaki continue; 116308b202b6SYOSHIFUJI Hideaki } 116408b202b6SYOSHIFUJI Hideaki 1165bc8c20acSSatish Ashok if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE || 1166bc8c20acSSatish Ashok grec->grec_type == MLD2_MODE_IS_INCLUDE) && 1167bc8c20acSSatish Ashok ntohs(*nsrcs) == 0) { 1168bc8c20acSSatish Ashok br_ip6_multicast_leave_group(br, port, &grec->grec_mca, 1169b0e9a30dSVlad Yasevich vid); 1170bc8c20acSSatish Ashok } else { 1171bc8c20acSSatish Ashok err = br_ip6_multicast_add_group(br, port, 1172bc8c20acSSatish Ashok &grec->grec_mca, vid); 11739264251eSDavide Caratti if (err) 117408b202b6SYOSHIFUJI Hideaki break; 117508b202b6SYOSHIFUJI Hideaki } 1176bc8c20acSSatish Ashok } 117708b202b6SYOSHIFUJI Hideaki 117808b202b6SYOSHIFUJI Hideaki return err; 117908b202b6SYOSHIFUJI Hideaki } 118008b202b6SYOSHIFUJI Hideaki #endif 118108b202b6SYOSHIFUJI Hideaki 1182dc4eb53aSLinus Lüssing static bool br_ip4_multicast_select_querier(struct net_bridge *br, 11832cd41431SLinus Lüssing struct net_bridge_port *port, 1184dc4eb53aSLinus Lüssing __be32 saddr) 1185dc4eb53aSLinus Lüssing { 1186dc4eb53aSLinus Lüssing if (!timer_pending(&br->ip4_own_query.timer) && 1187dc4eb53aSLinus Lüssing !timer_pending(&br->ip4_other_query.timer)) 1188dc4eb53aSLinus Lüssing goto update; 1189dc4eb53aSLinus Lüssing 1190dc4eb53aSLinus Lüssing if (!br->ip4_querier.addr.u.ip4) 1191dc4eb53aSLinus Lüssing goto update; 1192dc4eb53aSLinus Lüssing 1193dc4eb53aSLinus Lüssing if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4)) 1194dc4eb53aSLinus Lüssing goto update; 1195dc4eb53aSLinus Lüssing 1196dc4eb53aSLinus Lüssing return false; 1197dc4eb53aSLinus Lüssing 1198dc4eb53aSLinus Lüssing update: 1199dc4eb53aSLinus Lüssing br->ip4_querier.addr.u.ip4 = saddr; 1200dc4eb53aSLinus Lüssing 12012cd41431SLinus Lüssing /* update protected by general multicast_lock by caller */ 12022cd41431SLinus Lüssing rcu_assign_pointer(br->ip4_querier.port, port); 12032cd41431SLinus Lüssing 1204dc4eb53aSLinus Lüssing return true; 1205dc4eb53aSLinus Lüssing } 1206dc4eb53aSLinus Lüssing 1207dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1208dc4eb53aSLinus Lüssing static bool br_ip6_multicast_select_querier(struct net_bridge *br, 12092cd41431SLinus Lüssing struct net_bridge_port *port, 1210dc4eb53aSLinus Lüssing struct in6_addr *saddr) 1211dc4eb53aSLinus Lüssing { 1212dc4eb53aSLinus Lüssing if (!timer_pending(&br->ip6_own_query.timer) && 1213dc4eb53aSLinus Lüssing !timer_pending(&br->ip6_other_query.timer)) 1214dc4eb53aSLinus Lüssing goto update; 1215dc4eb53aSLinus Lüssing 1216dc4eb53aSLinus Lüssing if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0) 1217dc4eb53aSLinus Lüssing goto update; 1218dc4eb53aSLinus Lüssing 1219dc4eb53aSLinus Lüssing return false; 1220dc4eb53aSLinus Lüssing 1221dc4eb53aSLinus Lüssing update: 1222dc4eb53aSLinus Lüssing br->ip6_querier.addr.u.ip6 = *saddr; 1223dc4eb53aSLinus Lüssing 12242cd41431SLinus Lüssing /* update protected by general multicast_lock by caller */ 12252cd41431SLinus Lüssing rcu_assign_pointer(br->ip6_querier.port, port); 12262cd41431SLinus Lüssing 1227dc4eb53aSLinus Lüssing return true; 1228dc4eb53aSLinus Lüssing } 1229dc4eb53aSLinus Lüssing #endif 1230dc4eb53aSLinus Lüssing 1231dc4eb53aSLinus Lüssing static bool br_multicast_select_querier(struct net_bridge *br, 12322cd41431SLinus Lüssing struct net_bridge_port *port, 1233dc4eb53aSLinus Lüssing struct br_ip *saddr) 1234dc4eb53aSLinus Lüssing { 1235dc4eb53aSLinus Lüssing switch (saddr->proto) { 1236dc4eb53aSLinus Lüssing case htons(ETH_P_IP): 12372cd41431SLinus Lüssing return br_ip4_multicast_select_querier(br, port, saddr->u.ip4); 1238dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1239dc4eb53aSLinus Lüssing case htons(ETH_P_IPV6): 12402cd41431SLinus Lüssing return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6); 1241dc4eb53aSLinus Lüssing #endif 1242dc4eb53aSLinus Lüssing } 1243dc4eb53aSLinus Lüssing 1244dc4eb53aSLinus Lüssing return false; 1245dc4eb53aSLinus Lüssing } 1246dc4eb53aSLinus Lüssing 1247cc0fdd80SLinus Lüssing static void 124890010b36SLinus Lüssing br_multicast_update_query_timer(struct net_bridge *br, 124990010b36SLinus Lüssing struct bridge_mcast_other_query *query, 1250b00589afSLinus Lüssing unsigned long max_delay) 1251b00589afSLinus Lüssing { 125290010b36SLinus Lüssing if (!timer_pending(&query->timer)) 125390010b36SLinus Lüssing query->delay_time = jiffies + max_delay; 1254b00589afSLinus Lüssing 125590010b36SLinus Lüssing mod_timer(&query->timer, jiffies + br->multicast_querier_interval); 1256b00589afSLinus Lüssing } 1257b00589afSLinus Lüssing 12587e80c124Sstephen hemminger /* 12597c77602fSCong Wang * Add port to router_list 12607e80c124Sstephen hemminger * list is maintained ordered by pointer value 12617e80c124Sstephen hemminger * and locked by br->multicast_lock and RCU 12627e80c124Sstephen hemminger */ 12630909e117SHerbert Xu static void br_multicast_add_router(struct net_bridge *br, 12640909e117SHerbert Xu struct net_bridge_port *port) 12650909e117SHerbert Xu { 1266dcdca2c4Sstephen hemminger struct net_bridge_port *p; 1267b67bfe0dSSasha Levin struct hlist_node *slot = NULL; 12680909e117SHerbert Xu 12691a040eacSNikolay Aleksandrov if (!hlist_unhashed(&port->rlist)) 12701a040eacSNikolay Aleksandrov return; 12711a040eacSNikolay Aleksandrov 1272b67bfe0dSSasha Levin hlist_for_each_entry(p, &br->router_list, rlist) { 12737e80c124Sstephen hemminger if ((unsigned long) port >= (unsigned long) p) 12747e80c124Sstephen hemminger break; 1275b67bfe0dSSasha Levin slot = &p->rlist; 1276dcdca2c4Sstephen hemminger } 12770909e117SHerbert Xu 12787e80c124Sstephen hemminger if (slot) 12791d023284SKen Helias hlist_add_behind_rcu(&port->rlist, slot); 1280dcdca2c4Sstephen hemminger else 1281dcdca2c4Sstephen hemminger hlist_add_head_rcu(&port->rlist, &br->router_list); 1282949f1e39SSatish Ashok br_rtr_notify(br->dev, port, RTM_NEWMDB); 12830909e117SHerbert Xu } 12840909e117SHerbert Xu 1285eb1d1641SHerbert Xu static void br_multicast_mark_router(struct net_bridge *br, 1286eb1d1641SHerbert Xu struct net_bridge_port *port) 1287eb1d1641SHerbert Xu { 1288eb1d1641SHerbert Xu unsigned long now = jiffies; 1289eb1d1641SHerbert Xu 1290eb1d1641SHerbert Xu if (!port) { 12917f0aec7aSNikolay Aleksandrov if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) 1292eb1d1641SHerbert Xu mod_timer(&br->multicast_router_timer, 1293eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1294eb1d1641SHerbert Xu return; 1295eb1d1641SHerbert Xu } 1296eb1d1641SHerbert Xu 1297a55d8246SNikolay Aleksandrov if (port->multicast_router == MDB_RTR_TYPE_DISABLED || 1298a55d8246SNikolay Aleksandrov port->multicast_router == MDB_RTR_TYPE_PERM) 1299eb1d1641SHerbert Xu return; 1300eb1d1641SHerbert Xu 13010909e117SHerbert Xu br_multicast_add_router(br, port); 1302eb1d1641SHerbert Xu 1303eb1d1641SHerbert Xu mod_timer(&port->multicast_router_timer, 1304eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1305eb1d1641SHerbert Xu } 1306eb1d1641SHerbert Xu 1307eb1d1641SHerbert Xu static void br_multicast_query_received(struct net_bridge *br, 1308eb1d1641SHerbert Xu struct net_bridge_port *port, 130990010b36SLinus Lüssing struct bridge_mcast_other_query *query, 1310dc4eb53aSLinus Lüssing struct br_ip *saddr, 1311b00589afSLinus Lüssing unsigned long max_delay) 1312eb1d1641SHerbert Xu { 13132cd41431SLinus Lüssing if (!br_multicast_select_querier(br, port, saddr)) 1314eb1d1641SHerbert Xu return; 1315eb1d1641SHerbert Xu 1316dc4eb53aSLinus Lüssing br_multicast_update_query_timer(br, query, max_delay); 1317eb1d1641SHerbert Xu br_multicast_mark_router(br, port); 1318eb1d1641SHerbert Xu } 1319eb1d1641SHerbert Xu 13208ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_query(struct net_bridge *br, 1321eb1d1641SHerbert Xu struct net_bridge_port *port, 132206499098SVlad Yasevich struct sk_buff *skb, 132306499098SVlad Yasevich u16 vid) 1324eb1d1641SHerbert Xu { 1325b71d1d42SEric Dumazet const struct iphdr *iph = ip_hdr(skb); 1326eb1d1641SHerbert Xu struct igmphdr *ih = igmp_hdr(skb); 1327eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1328eb1d1641SHerbert Xu struct igmpv3_query *ih3; 1329eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1330e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 1331dc4eb53aSLinus Lüssing struct br_ip saddr; 1332eb1d1641SHerbert Xu unsigned long max_delay; 1333eb1d1641SHerbert Xu unsigned long now = jiffies; 1334856ce5d0SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 1335eb1d1641SHerbert Xu __be32 group; 1336bec68ff1SYOSHIFUJI Hideaki int err = 0; 1337eb1d1641SHerbert Xu 1338eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1339eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1340eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 1341eb1d1641SHerbert Xu goto out; 1342eb1d1641SHerbert Xu 1343eb1d1641SHerbert Xu group = ih->group; 1344eb1d1641SHerbert Xu 1345856ce5d0SLinus Lüssing if (skb->len == offset + sizeof(*ih)) { 1346eb1d1641SHerbert Xu max_delay = ih->code * (HZ / IGMP_TIMER_SCALE); 1347eb1d1641SHerbert Xu 1348eb1d1641SHerbert Xu if (!max_delay) { 1349eb1d1641SHerbert Xu max_delay = 10 * HZ; 1350eb1d1641SHerbert Xu group = 0; 1351eb1d1641SHerbert Xu } 1352856ce5d0SLinus Lüssing } else if (skb->len >= offset + sizeof(*ih3)) { 1353eb1d1641SHerbert Xu ih3 = igmpv3_query_hdr(skb); 1354eb1d1641SHerbert Xu if (ih3->nsrcs) 1355bec68ff1SYOSHIFUJI Hideaki goto out; 1356eb1d1641SHerbert Xu 13570ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 max_delay = ih3->code ? 13580ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1; 13599afd85c9SLinus Lüssing } else { 13609ed973ccSLinus Lüssing goto out; 13619ed973ccSLinus Lüssing } 13629ed973ccSLinus Lüssing 1363dc4eb53aSLinus Lüssing if (!group) { 1364dc4eb53aSLinus Lüssing saddr.proto = htons(ETH_P_IP); 1365dc4eb53aSLinus Lüssing saddr.u.ip4 = iph->saddr; 1366b00589afSLinus Lüssing 1367dc4eb53aSLinus Lüssing br_multicast_query_received(br, port, &br->ip4_other_query, 1368dc4eb53aSLinus Lüssing &saddr, max_delay); 1369eb1d1641SHerbert Xu goto out; 1370dc4eb53aSLinus Lüssing } 1371eb1d1641SHerbert Xu 1372b0e9a30dSVlad Yasevich mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid); 1373eb1d1641SHerbert Xu if (!mp) 1374eb1d1641SHerbert Xu goto out; 1375eb1d1641SHerbert Xu 1376eb1d1641SHerbert Xu max_delay *= br->multicast_last_member_count; 1377eb1d1641SHerbert Xu 13788a870178SHerbert Xu if (mp->mglist && 1379eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1380eb1d1641SHerbert Xu time_after(mp->timer.expires, now + max_delay) : 1381eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) 1382eb1d1641SHerbert Xu mod_timer(&mp->timer, now + max_delay); 1383eb1d1641SHerbert Xu 1384e8051688SEric Dumazet for (pp = &mp->ports; 1385e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 1386e8051688SEric Dumazet pp = &p->next) { 1387eb1d1641SHerbert Xu if (timer_pending(&p->timer) ? 1388eb1d1641SHerbert Xu time_after(p->timer.expires, now + max_delay) : 1389eb1d1641SHerbert Xu try_to_del_timer_sync(&p->timer) >= 0) 139024f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 1391eb1d1641SHerbert Xu } 1392eb1d1641SHerbert Xu 1393eb1d1641SHerbert Xu out: 1394eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1395bec68ff1SYOSHIFUJI Hideaki return err; 1396eb1d1641SHerbert Xu } 1397eb1d1641SHerbert Xu 1398dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 139908b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_query(struct net_bridge *br, 140008b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 140106499098SVlad Yasevich struct sk_buff *skb, 140206499098SVlad Yasevich u16 vid) 140308b202b6SYOSHIFUJI Hideaki { 1404b71d1d42SEric Dumazet const struct ipv6hdr *ip6h = ipv6_hdr(skb); 1405eca2a43bSstephen hemminger struct mld_msg *mld; 140608b202b6SYOSHIFUJI Hideaki struct net_bridge_mdb_entry *mp; 140708b202b6SYOSHIFUJI Hideaki struct mld2_query *mld2q; 1408e8051688SEric Dumazet struct net_bridge_port_group *p; 1409e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 1410dc4eb53aSLinus Lüssing struct br_ip saddr; 141108b202b6SYOSHIFUJI Hideaki unsigned long max_delay; 141208b202b6SYOSHIFUJI Hideaki unsigned long now = jiffies; 1413856ce5d0SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 1414b71d1d42SEric Dumazet const struct in6_addr *group = NULL; 14159ed973ccSLinus Lüssing bool is_general_query; 141608b202b6SYOSHIFUJI Hideaki int err = 0; 141708b202b6SYOSHIFUJI Hideaki 141808b202b6SYOSHIFUJI Hideaki spin_lock(&br->multicast_lock); 141908b202b6SYOSHIFUJI Hideaki if (!netif_running(br->dev) || 142008b202b6SYOSHIFUJI Hideaki (port && port->state == BR_STATE_DISABLED)) 142108b202b6SYOSHIFUJI Hideaki goto out; 142208b202b6SYOSHIFUJI Hideaki 1423856ce5d0SLinus Lüssing if (skb->len == offset + sizeof(*mld)) { 1424856ce5d0SLinus Lüssing if (!pskb_may_pull(skb, offset + sizeof(*mld))) { 142508b202b6SYOSHIFUJI Hideaki err = -EINVAL; 142608b202b6SYOSHIFUJI Hideaki goto out; 142708b202b6SYOSHIFUJI Hideaki } 142808b202b6SYOSHIFUJI Hideaki mld = (struct mld_msg *) icmp6_hdr(skb); 14294715213dSLi RongQing max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay)); 143008b202b6SYOSHIFUJI Hideaki if (max_delay) 143108b202b6SYOSHIFUJI Hideaki group = &mld->mld_mca; 1432248ba8ecSLinus Lüssing } else { 1433856ce5d0SLinus Lüssing if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) { 143408b202b6SYOSHIFUJI Hideaki err = -EINVAL; 143508b202b6SYOSHIFUJI Hideaki goto out; 143608b202b6SYOSHIFUJI Hideaki } 143708b202b6SYOSHIFUJI Hideaki mld2q = (struct mld2_query *)icmp6_hdr(skb); 143808b202b6SYOSHIFUJI Hideaki if (!mld2q->mld2q_nsrcs) 143908b202b6SYOSHIFUJI Hideaki group = &mld2q->mld2q_mca; 1440e3f5b170SDaniel Borkmann 1441e3f5b170SDaniel Borkmann max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL); 144208b202b6SYOSHIFUJI Hideaki } 144308b202b6SYOSHIFUJI Hideaki 14449ed973ccSLinus Lüssing is_general_query = group && ipv6_addr_any(group); 14459ed973ccSLinus Lüssing 1446dc4eb53aSLinus Lüssing if (is_general_query) { 1447dc4eb53aSLinus Lüssing saddr.proto = htons(ETH_P_IPV6); 1448dc4eb53aSLinus Lüssing saddr.u.ip6 = ip6h->saddr; 1449b00589afSLinus Lüssing 1450dc4eb53aSLinus Lüssing br_multicast_query_received(br, port, &br->ip6_other_query, 1451dc4eb53aSLinus Lüssing &saddr, max_delay); 145208b202b6SYOSHIFUJI Hideaki goto out; 14536c03ee8bSLinus Lüssing } else if (!group) { 14546c03ee8bSLinus Lüssing goto out; 1455dc4eb53aSLinus Lüssing } 145608b202b6SYOSHIFUJI Hideaki 1457b0e9a30dSVlad Yasevich mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid); 145808b202b6SYOSHIFUJI Hideaki if (!mp) 145908b202b6SYOSHIFUJI Hideaki goto out; 146008b202b6SYOSHIFUJI Hideaki 146108b202b6SYOSHIFUJI Hideaki max_delay *= br->multicast_last_member_count; 14628a870178SHerbert Xu if (mp->mglist && 146308b202b6SYOSHIFUJI Hideaki (timer_pending(&mp->timer) ? 146408b202b6SYOSHIFUJI Hideaki time_after(mp->timer.expires, now + max_delay) : 146508b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&mp->timer) >= 0)) 146608b202b6SYOSHIFUJI Hideaki mod_timer(&mp->timer, now + max_delay); 146708b202b6SYOSHIFUJI Hideaki 1468e8051688SEric Dumazet for (pp = &mp->ports; 1469e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 1470e8051688SEric Dumazet pp = &p->next) { 147108b202b6SYOSHIFUJI Hideaki if (timer_pending(&p->timer) ? 147208b202b6SYOSHIFUJI Hideaki time_after(p->timer.expires, now + max_delay) : 147308b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&p->timer) >= 0) 147424f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 147508b202b6SYOSHIFUJI Hideaki } 147608b202b6SYOSHIFUJI Hideaki 147708b202b6SYOSHIFUJI Hideaki out: 147808b202b6SYOSHIFUJI Hideaki spin_unlock(&br->multicast_lock); 147908b202b6SYOSHIFUJI Hideaki return err; 148008b202b6SYOSHIFUJI Hideaki } 148108b202b6SYOSHIFUJI Hideaki #endif 148208b202b6SYOSHIFUJI Hideaki 148390010b36SLinus Lüssing static void 148490010b36SLinus Lüssing br_multicast_leave_group(struct net_bridge *br, 1485eb1d1641SHerbert Xu struct net_bridge_port *port, 1486cc0fdd80SLinus Lüssing struct br_ip *group, 148790010b36SLinus Lüssing struct bridge_mcast_other_query *other_query, 148890010b36SLinus Lüssing struct bridge_mcast_own_query *own_query) 1489eb1d1641SHerbert Xu { 1490eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 1491eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1492eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1493eb1d1641SHerbert Xu unsigned long now; 1494eb1d1641SHerbert Xu unsigned long time; 1495eb1d1641SHerbert Xu 1496eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1497eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1498544586f7SSatish Ashok (port && port->state == BR_STATE_DISABLED)) 1499eb1d1641SHerbert Xu goto out; 1500eb1d1641SHerbert Xu 1501e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1502eb1d1641SHerbert Xu mp = br_mdb_ip_get(mdb, group); 1503eb1d1641SHerbert Xu if (!mp) 1504eb1d1641SHerbert Xu goto out; 1505eb1d1641SHerbert Xu 1506544586f7SSatish Ashok if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) { 1507544586f7SSatish Ashok struct net_bridge_port_group __rcu **pp; 1508544586f7SSatish Ashok 1509544586f7SSatish Ashok for (pp = &mp->ports; 1510544586f7SSatish Ashok (p = mlock_dereference(*pp, br)) != NULL; 1511544586f7SSatish Ashok pp = &p->next) { 1512544586f7SSatish Ashok if (p->port != port) 1513544586f7SSatish Ashok continue; 1514544586f7SSatish Ashok 1515544586f7SSatish Ashok rcu_assign_pointer(*pp, p->next); 1516544586f7SSatish Ashok hlist_del_init(&p->mglist); 1517544586f7SSatish Ashok del_timer(&p->timer); 1518544586f7SSatish Ashok call_rcu_bh(&p->rcu, br_multicast_free_pg); 151945ebcce5SElad Raz br_mdb_notify(br->dev, port, group, RTM_DELMDB, 152045ebcce5SElad Raz p->flags); 1521544586f7SSatish Ashok 1522544586f7SSatish Ashok if (!mp->ports && !mp->mglist && 1523544586f7SSatish Ashok netif_running(br->dev)) 1524544586f7SSatish Ashok mod_timer(&mp->timer, jiffies); 1525544586f7SSatish Ashok } 1526544586f7SSatish Ashok goto out; 1527544586f7SSatish Ashok } 1528544586f7SSatish Ashok 1529544586f7SSatish Ashok if (timer_pending(&other_query->timer)) 1530544586f7SSatish Ashok goto out; 1531544586f7SSatish Ashok 1532cc0fdd80SLinus Lüssing if (br->multicast_querier) { 15336b7df111SCong Wang __br_multicast_send_query(br, port, &mp->addr); 15346b7df111SCong Wang 15356b7df111SCong Wang time = jiffies + br->multicast_last_member_count * 15366b7df111SCong Wang br->multicast_last_member_interval; 1537cc0fdd80SLinus Lüssing 153890010b36SLinus Lüssing mod_timer(&own_query->timer, time); 15396b7df111SCong Wang 15406b7df111SCong Wang for (p = mlock_dereference(mp->ports, br); 15416b7df111SCong Wang p != NULL; 15426b7df111SCong Wang p = mlock_dereference(p->next, br)) { 15436b7df111SCong Wang if (p->port != port) 15446b7df111SCong Wang continue; 15456b7df111SCong Wang 15466b7df111SCong Wang if (!hlist_unhashed(&p->mglist) && 15476b7df111SCong Wang (timer_pending(&p->timer) ? 15486b7df111SCong Wang time_after(p->timer.expires, time) : 15496b7df111SCong Wang try_to_del_timer_sync(&p->timer) >= 0)) { 15506b7df111SCong Wang mod_timer(&p->timer, time); 15516b7df111SCong Wang } 15526b7df111SCong Wang 15536b7df111SCong Wang break; 15546b7df111SCong Wang } 15556b7df111SCong Wang } 15566b7df111SCong Wang 1557eb1d1641SHerbert Xu now = jiffies; 1558eb1d1641SHerbert Xu time = now + br->multicast_last_member_count * 1559eb1d1641SHerbert Xu br->multicast_last_member_interval; 1560eb1d1641SHerbert Xu 1561eb1d1641SHerbert Xu if (!port) { 1562454594f3SLinus Lüssing if (mp->mglist && 1563eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1564eb1d1641SHerbert Xu time_after(mp->timer.expires, time) : 1565eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) { 1566eb1d1641SHerbert Xu mod_timer(&mp->timer, time); 1567eb1d1641SHerbert Xu } 1568454594f3SLinus Lüssing 1569454594f3SLinus Lüssing goto out; 1570454594f3SLinus Lüssing } 1571454594f3SLinus Lüssing 1572454594f3SLinus Lüssing for (p = mlock_dereference(mp->ports, br); 1573454594f3SLinus Lüssing p != NULL; 1574454594f3SLinus Lüssing p = mlock_dereference(p->next, br)) { 1575454594f3SLinus Lüssing if (p->port != port) 1576454594f3SLinus Lüssing continue; 1577454594f3SLinus Lüssing 1578454594f3SLinus Lüssing if (!hlist_unhashed(&p->mglist) && 1579454594f3SLinus Lüssing (timer_pending(&p->timer) ? 1580454594f3SLinus Lüssing time_after(p->timer.expires, time) : 1581454594f3SLinus Lüssing try_to_del_timer_sync(&p->timer) >= 0)) { 1582454594f3SLinus Lüssing mod_timer(&p->timer, time); 1583454594f3SLinus Lüssing } 1584454594f3SLinus Lüssing 1585454594f3SLinus Lüssing break; 1586eb1d1641SHerbert Xu } 1587eb1d1641SHerbert Xu out: 1588eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1589eb1d1641SHerbert Xu } 1590eb1d1641SHerbert Xu 15918ef2a9a5SYOSHIFUJI Hideaki static void br_ip4_multicast_leave_group(struct net_bridge *br, 15928ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 1593b0e9a30dSVlad Yasevich __be32 group, 1594b0e9a30dSVlad Yasevich __u16 vid) 15958ef2a9a5SYOSHIFUJI Hideaki { 15968ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 159790010b36SLinus Lüssing struct bridge_mcast_own_query *own_query; 15988ef2a9a5SYOSHIFUJI Hideaki 15998ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 16008ef2a9a5SYOSHIFUJI Hideaki return; 16018ef2a9a5SYOSHIFUJI Hideaki 160290010b36SLinus Lüssing own_query = port ? &port->ip4_own_query : &br->ip4_own_query; 160390010b36SLinus Lüssing 16048ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 16058ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 1606b0e9a30dSVlad Yasevich br_group.vid = vid; 16078ef2a9a5SYOSHIFUJI Hideaki 160890010b36SLinus Lüssing br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query, 160990010b36SLinus Lüssing own_query); 16108ef2a9a5SYOSHIFUJI Hideaki } 16118ef2a9a5SYOSHIFUJI Hideaki 1612dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 161308b202b6SYOSHIFUJI Hideaki static void br_ip6_multicast_leave_group(struct net_bridge *br, 161408b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 1615b0e9a30dSVlad Yasevich const struct in6_addr *group, 1616b0e9a30dSVlad Yasevich __u16 vid) 161708b202b6SYOSHIFUJI Hideaki { 161808b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 161990010b36SLinus Lüssing struct bridge_mcast_own_query *own_query; 162008b202b6SYOSHIFUJI Hideaki 16213c3769e6SLinus Lüssing if (ipv6_addr_is_ll_all_nodes(group)) 162208b202b6SYOSHIFUJI Hideaki return; 162308b202b6SYOSHIFUJI Hideaki 162490010b36SLinus Lüssing own_query = port ? &port->ip6_own_query : &br->ip6_own_query; 162590010b36SLinus Lüssing 16264e3fd7a0SAlexey Dobriyan br_group.u.ip6 = *group; 162708b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 1628b0e9a30dSVlad Yasevich br_group.vid = vid; 162908b202b6SYOSHIFUJI Hideaki 163090010b36SLinus Lüssing br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query, 163190010b36SLinus Lüssing own_query); 163208b202b6SYOSHIFUJI Hideaki } 163308b202b6SYOSHIFUJI Hideaki #endif 16348ef2a9a5SYOSHIFUJI Hideaki 16351080ab95SNikolay Aleksandrov static void br_multicast_err_count(const struct net_bridge *br, 16361080ab95SNikolay Aleksandrov const struct net_bridge_port *p, 16371080ab95SNikolay Aleksandrov __be16 proto) 16381080ab95SNikolay Aleksandrov { 16391080ab95SNikolay Aleksandrov struct bridge_mcast_stats __percpu *stats; 16401080ab95SNikolay Aleksandrov struct bridge_mcast_stats *pstats; 16411080ab95SNikolay Aleksandrov 16421080ab95SNikolay Aleksandrov if (!br->multicast_stats_enabled) 16431080ab95SNikolay Aleksandrov return; 16441080ab95SNikolay Aleksandrov 16451080ab95SNikolay Aleksandrov if (p) 16461080ab95SNikolay Aleksandrov stats = p->mcast_stats; 16471080ab95SNikolay Aleksandrov else 16481080ab95SNikolay Aleksandrov stats = br->mcast_stats; 16491080ab95SNikolay Aleksandrov if (WARN_ON(!stats)) 16501080ab95SNikolay Aleksandrov return; 16511080ab95SNikolay Aleksandrov 16521080ab95SNikolay Aleksandrov pstats = this_cpu_ptr(stats); 16531080ab95SNikolay Aleksandrov 16541080ab95SNikolay Aleksandrov u64_stats_update_begin(&pstats->syncp); 16551080ab95SNikolay Aleksandrov switch (proto) { 16561080ab95SNikolay Aleksandrov case htons(ETH_P_IP): 16571080ab95SNikolay Aleksandrov pstats->mstats.igmp_parse_errors++; 16581080ab95SNikolay Aleksandrov break; 16591080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 16601080ab95SNikolay Aleksandrov case htons(ETH_P_IPV6): 16611080ab95SNikolay Aleksandrov pstats->mstats.mld_parse_errors++; 16621080ab95SNikolay Aleksandrov break; 16631080ab95SNikolay Aleksandrov #endif 16641080ab95SNikolay Aleksandrov } 16651080ab95SNikolay Aleksandrov u64_stats_update_end(&pstats->syncp); 16661080ab95SNikolay Aleksandrov } 16671080ab95SNikolay Aleksandrov 166891b02d3dSNikolay Aleksandrov static void br_multicast_pim(struct net_bridge *br, 166991b02d3dSNikolay Aleksandrov struct net_bridge_port *port, 167091b02d3dSNikolay Aleksandrov const struct sk_buff *skb) 167191b02d3dSNikolay Aleksandrov { 167291b02d3dSNikolay Aleksandrov unsigned int offset = skb_transport_offset(skb); 167391b02d3dSNikolay Aleksandrov struct pimhdr *pimhdr, _pimhdr; 167491b02d3dSNikolay Aleksandrov 167591b02d3dSNikolay Aleksandrov pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr); 167691b02d3dSNikolay Aleksandrov if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION || 167791b02d3dSNikolay Aleksandrov pim_hdr_type(pimhdr) != PIM_TYPE_HELLO) 167891b02d3dSNikolay Aleksandrov return; 167991b02d3dSNikolay Aleksandrov 168091b02d3dSNikolay Aleksandrov br_multicast_mark_router(br, port); 168191b02d3dSNikolay Aleksandrov } 168291b02d3dSNikolay Aleksandrov 1683eb1d1641SHerbert Xu static int br_multicast_ipv4_rcv(struct net_bridge *br, 1684eb1d1641SHerbert Xu struct net_bridge_port *port, 168506499098SVlad Yasevich struct sk_buff *skb, 168606499098SVlad Yasevich u16 vid) 1687eb1d1641SHerbert Xu { 16889afd85c9SLinus Lüssing struct sk_buff *skb_trimmed = NULL; 1689eb1d1641SHerbert Xu struct igmphdr *ih; 1690eb1d1641SHerbert Xu int err; 1691eb1d1641SHerbert Xu 16929afd85c9SLinus Lüssing err = ip_mc_check_igmp(skb, &skb_trimmed); 1693eb1d1641SHerbert Xu 16949afd85c9SLinus Lüssing if (err == -ENOMSG) { 169591b02d3dSNikolay Aleksandrov if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) { 1696bd4265feSHerbert Xu BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 169791b02d3dSNikolay Aleksandrov } else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) { 169891b02d3dSNikolay Aleksandrov if (ip_hdr(skb)->protocol == IPPROTO_PIM) 169991b02d3dSNikolay Aleksandrov br_multicast_pim(br, port, skb); 170091b02d3dSNikolay Aleksandrov } 1701eb1d1641SHerbert Xu return 0; 17029afd85c9SLinus Lüssing } else if (err < 0) { 17031080ab95SNikolay Aleksandrov br_multicast_err_count(br, port, skb->protocol); 17049afd85c9SLinus Lüssing return err; 1705bd4265feSHerbert Xu } 1706eb1d1641SHerbert Xu 17079afd85c9SLinus Lüssing ih = igmp_hdr(skb); 17081080ab95SNikolay Aleksandrov BR_INPUT_SKB_CB(skb)->igmp = ih->type; 1709eb1d1641SHerbert Xu 1710eb1d1641SHerbert Xu switch (ih->type) { 1711eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_REPORT: 1712eb1d1641SHerbert Xu case IGMPV2_HOST_MEMBERSHIP_REPORT: 171362b2bcb4SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 1714b0e9a30dSVlad Yasevich err = br_ip4_multicast_add_group(br, port, ih->group, vid); 1715eb1d1641SHerbert Xu break; 1716eb1d1641SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 17179afd85c9SLinus Lüssing err = br_ip4_multicast_igmp3_report(br, port, skb_trimmed, vid); 1718eb1d1641SHerbert Xu break; 1719eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_QUERY: 17209afd85c9SLinus Lüssing err = br_ip4_multicast_query(br, port, skb_trimmed, vid); 1721eb1d1641SHerbert Xu break; 1722eb1d1641SHerbert Xu case IGMP_HOST_LEAVE_MESSAGE: 1723b0e9a30dSVlad Yasevich br_ip4_multicast_leave_group(br, port, ih->group, vid); 1724eb1d1641SHerbert Xu break; 1725eb1d1641SHerbert Xu } 1726eb1d1641SHerbert Xu 1727a516993fSLinus Lüssing if (skb_trimmed && skb_trimmed != skb) 17289afd85c9SLinus Lüssing kfree_skb(skb_trimmed); 17299afd85c9SLinus Lüssing 1730a65056ecSNikolay Aleksandrov br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp, 17311080ab95SNikolay Aleksandrov BR_MCAST_DIR_RX); 17321080ab95SNikolay Aleksandrov 1733eb1d1641SHerbert Xu return err; 1734eb1d1641SHerbert Xu } 1735eb1d1641SHerbert Xu 1736dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 173708b202b6SYOSHIFUJI Hideaki static int br_multicast_ipv6_rcv(struct net_bridge *br, 173808b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 173906499098SVlad Yasevich struct sk_buff *skb, 174006499098SVlad Yasevich u16 vid) 174108b202b6SYOSHIFUJI Hideaki { 17429afd85c9SLinus Lüssing struct sk_buff *skb_trimmed = NULL; 17439afd85c9SLinus Lüssing struct mld_msg *mld; 174408b202b6SYOSHIFUJI Hideaki int err; 174508b202b6SYOSHIFUJI Hideaki 17469afd85c9SLinus Lüssing err = ipv6_mc_check_mld(skb, &skb_trimmed); 174708b202b6SYOSHIFUJI Hideaki 17489afd85c9SLinus Lüssing if (err == -ENOMSG) { 17499afd85c9SLinus Lüssing if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr)) 17508fad9c39SLinus Lüssing BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 175108b202b6SYOSHIFUJI Hideaki return 0; 17529afd85c9SLinus Lüssing } else if (err < 0) { 17531080ab95SNikolay Aleksandrov br_multicast_err_count(br, port, skb->protocol); 17549afd85c9SLinus Lüssing return err; 175508b202b6SYOSHIFUJI Hideaki } 175608b202b6SYOSHIFUJI Hideaki 17579afd85c9SLinus Lüssing mld = (struct mld_msg *)skb_transport_header(skb); 17581080ab95SNikolay Aleksandrov BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type; 175908b202b6SYOSHIFUJI Hideaki 17609afd85c9SLinus Lüssing switch (mld->mld_type) { 176108b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 1762fc2af6c7SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 1763b0e9a30dSVlad Yasevich err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid); 176408b202b6SYOSHIFUJI Hideaki break; 176508b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 17669afd85c9SLinus Lüssing err = br_ip6_multicast_mld2_report(br, port, skb_trimmed, vid); 176708b202b6SYOSHIFUJI Hideaki break; 176808b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 17699afd85c9SLinus Lüssing err = br_ip6_multicast_query(br, port, skb_trimmed, vid); 177008b202b6SYOSHIFUJI Hideaki break; 177108b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 1772b0e9a30dSVlad Yasevich br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid); 17739afd85c9SLinus Lüssing break; 177408b202b6SYOSHIFUJI Hideaki } 177508b202b6SYOSHIFUJI Hideaki 1776a516993fSLinus Lüssing if (skb_trimmed && skb_trimmed != skb) 17779afd85c9SLinus Lüssing kfree_skb(skb_trimmed); 17789afd85c9SLinus Lüssing 1779a65056ecSNikolay Aleksandrov br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp, 17801080ab95SNikolay Aleksandrov BR_MCAST_DIR_RX); 17811080ab95SNikolay Aleksandrov 178208b202b6SYOSHIFUJI Hideaki return err; 178308b202b6SYOSHIFUJI Hideaki } 178408b202b6SYOSHIFUJI Hideaki #endif 178508b202b6SYOSHIFUJI Hideaki 1786eb1d1641SHerbert Xu int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port, 178706499098SVlad Yasevich struct sk_buff *skb, u16 vid) 1788eb1d1641SHerbert Xu { 17891080ab95SNikolay Aleksandrov int ret = 0; 17901080ab95SNikolay Aleksandrov 17911fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->igmp = 0; 17921fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->mrouters_only = 0; 17931fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 1794eb1d1641SHerbert Xu if (br->multicast_disabled) 1795eb1d1641SHerbert Xu return 0; 1796eb1d1641SHerbert Xu 1797eb1d1641SHerbert Xu switch (skb->protocol) { 1798eb1d1641SHerbert Xu case htons(ETH_P_IP): 17991080ab95SNikolay Aleksandrov ret = br_multicast_ipv4_rcv(br, port, skb, vid); 18001080ab95SNikolay Aleksandrov break; 1801dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 180208b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 18031080ab95SNikolay Aleksandrov ret = br_multicast_ipv6_rcv(br, port, skb, vid); 18041080ab95SNikolay Aleksandrov break; 180508b202b6SYOSHIFUJI Hideaki #endif 1806eb1d1641SHerbert Xu } 1807eb1d1641SHerbert Xu 18081080ab95SNikolay Aleksandrov return ret; 1809eb1d1641SHerbert Xu } 1810eb1d1641SHerbert Xu 1811cc0fdd80SLinus Lüssing static void br_multicast_query_expired(struct net_bridge *br, 18122cd41431SLinus Lüssing struct bridge_mcast_own_query *query, 18132cd41431SLinus Lüssing struct bridge_mcast_querier *querier) 1814cc0fdd80SLinus Lüssing { 1815cc0fdd80SLinus Lüssing spin_lock(&br->multicast_lock); 1816cc0fdd80SLinus Lüssing if (query->startup_sent < br->multicast_startup_query_count) 1817cc0fdd80SLinus Lüssing query->startup_sent++; 1818cc0fdd80SLinus Lüssing 181971d9f614SEric Dumazet RCU_INIT_POINTER(querier->port, NULL); 1820cc0fdd80SLinus Lüssing br_multicast_send_query(br, NULL, query); 1821cc0fdd80SLinus Lüssing spin_unlock(&br->multicast_lock); 1822cc0fdd80SLinus Lüssing } 1823cc0fdd80SLinus Lüssing 1824cc0fdd80SLinus Lüssing static void br_ip4_multicast_query_expired(unsigned long data) 1825eb1d1641SHerbert Xu { 1826eb1d1641SHerbert Xu struct net_bridge *br = (void *)data; 1827eb1d1641SHerbert Xu 18282cd41431SLinus Lüssing br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier); 1829eb1d1641SHerbert Xu } 1830eb1d1641SHerbert Xu 1831cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1832cc0fdd80SLinus Lüssing static void br_ip6_multicast_query_expired(unsigned long data) 1833cc0fdd80SLinus Lüssing { 1834cc0fdd80SLinus Lüssing struct net_bridge *br = (void *)data; 1835cc0fdd80SLinus Lüssing 18362cd41431SLinus Lüssing br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier); 1837cc0fdd80SLinus Lüssing } 1838cc0fdd80SLinus Lüssing #endif 1839cc0fdd80SLinus Lüssing 1840eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br) 1841eb1d1641SHerbert Xu { 1842eb1d1641SHerbert Xu br->hash_elasticity = 4; 1843eb1d1641SHerbert Xu br->hash_max = 512; 1844eb1d1641SHerbert Xu 1845*5e923585SNikolay Aleksandrov br->multicast_igmp_version = 2; 18467f0aec7aSNikolay Aleksandrov br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 1847c5c23260SHerbert Xu br->multicast_querier = 0; 18481c8ad5bfSCong Wang br->multicast_query_use_ifaddr = 0; 1849eb1d1641SHerbert Xu br->multicast_last_member_count = 2; 1850eb1d1641SHerbert Xu br->multicast_startup_query_count = 2; 1851eb1d1641SHerbert Xu 1852eb1d1641SHerbert Xu br->multicast_last_member_interval = HZ; 1853eb1d1641SHerbert Xu br->multicast_query_response_interval = 10 * HZ; 1854eb1d1641SHerbert Xu br->multicast_startup_query_interval = 125 * HZ / 4; 1855eb1d1641SHerbert Xu br->multicast_query_interval = 125 * HZ; 1856eb1d1641SHerbert Xu br->multicast_querier_interval = 255 * HZ; 1857eb1d1641SHerbert Xu br->multicast_membership_interval = 260 * HZ; 1858eb1d1641SHerbert Xu 185990010b36SLinus Lüssing br->ip4_other_query.delay_time = 0; 18602cd41431SLinus Lüssing br->ip4_querier.port = NULL; 1861cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 186290010b36SLinus Lüssing br->ip6_other_query.delay_time = 0; 18632cd41431SLinus Lüssing br->ip6_querier.port = NULL; 1864cc0fdd80SLinus Lüssing #endif 18650888d5f3Sdaniel br->has_ipv6_addr = 1; 1866b00589afSLinus Lüssing 1867eb1d1641SHerbert Xu spin_lock_init(&br->multicast_lock); 1868eb1d1641SHerbert Xu setup_timer(&br->multicast_router_timer, 1869eb1d1641SHerbert Xu br_multicast_local_router_expired, 0); 187090010b36SLinus Lüssing setup_timer(&br->ip4_other_query.timer, 187190010b36SLinus Lüssing br_ip4_multicast_querier_expired, (unsigned long)br); 187290010b36SLinus Lüssing setup_timer(&br->ip4_own_query.timer, br_ip4_multicast_query_expired, 1873cc0fdd80SLinus Lüssing (unsigned long)br); 1874cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 187590010b36SLinus Lüssing setup_timer(&br->ip6_other_query.timer, 187690010b36SLinus Lüssing br_ip6_multicast_querier_expired, (unsigned long)br); 187790010b36SLinus Lüssing setup_timer(&br->ip6_own_query.timer, br_ip6_multicast_query_expired, 1878cc0fdd80SLinus Lüssing (unsigned long)br); 1879cc0fdd80SLinus Lüssing #endif 1880eb1d1641SHerbert Xu } 1881eb1d1641SHerbert Xu 1882cc0fdd80SLinus Lüssing static void __br_multicast_open(struct net_bridge *br, 188390010b36SLinus Lüssing struct bridge_mcast_own_query *query) 1884eb1d1641SHerbert Xu { 1885cc0fdd80SLinus Lüssing query->startup_sent = 0; 1886eb1d1641SHerbert Xu 1887eb1d1641SHerbert Xu if (br->multicast_disabled) 1888eb1d1641SHerbert Xu return; 1889eb1d1641SHerbert Xu 1890cc0fdd80SLinus Lüssing mod_timer(&query->timer, jiffies); 1891cc0fdd80SLinus Lüssing } 1892cc0fdd80SLinus Lüssing 1893cc0fdd80SLinus Lüssing void br_multicast_open(struct net_bridge *br) 1894cc0fdd80SLinus Lüssing { 189590010b36SLinus Lüssing __br_multicast_open(br, &br->ip4_own_query); 1896cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 189790010b36SLinus Lüssing __br_multicast_open(br, &br->ip6_own_query); 1898cc0fdd80SLinus Lüssing #endif 1899eb1d1641SHerbert Xu } 1900eb1d1641SHerbert Xu 1901eb1d1641SHerbert Xu void br_multicast_stop(struct net_bridge *br) 1902eb1d1641SHerbert Xu { 1903eb1d1641SHerbert Xu del_timer_sync(&br->multicast_router_timer); 190490010b36SLinus Lüssing del_timer_sync(&br->ip4_other_query.timer); 190590010b36SLinus Lüssing del_timer_sync(&br->ip4_own_query.timer); 1906cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 190790010b36SLinus Lüssing del_timer_sync(&br->ip6_other_query.timer); 190890010b36SLinus Lüssing del_timer_sync(&br->ip6_own_query.timer); 1909cc0fdd80SLinus Lüssing #endif 1910e10177abSSatish Ashok } 1911e10177abSSatish Ashok 1912e10177abSSatish Ashok void br_multicast_dev_del(struct net_bridge *br) 1913e10177abSSatish Ashok { 1914e10177abSSatish Ashok struct net_bridge_mdb_htable *mdb; 1915e10177abSSatish Ashok struct net_bridge_mdb_entry *mp; 1916e10177abSSatish Ashok struct hlist_node *n; 1917e10177abSSatish Ashok u32 ver; 1918e10177abSSatish Ashok int i; 1919eb1d1641SHerbert Xu 1920eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1921e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1922eb1d1641SHerbert Xu if (!mdb) 1923eb1d1641SHerbert Xu goto out; 1924eb1d1641SHerbert Xu 1925eb1d1641SHerbert Xu br->mdb = NULL; 1926eb1d1641SHerbert Xu 1927eb1d1641SHerbert Xu ver = mdb->ver; 1928eb1d1641SHerbert Xu for (i = 0; i < mdb->max; i++) { 1929b67bfe0dSSasha Levin hlist_for_each_entry_safe(mp, n, &mdb->mhash[i], 1930eb1d1641SHerbert Xu hlist[ver]) { 1931eb1d1641SHerbert Xu del_timer(&mp->timer); 1932eb1d1641SHerbert Xu call_rcu_bh(&mp->rcu, br_multicast_free_group); 1933eb1d1641SHerbert Xu } 1934eb1d1641SHerbert Xu } 1935eb1d1641SHerbert Xu 1936eb1d1641SHerbert Xu if (mdb->old) { 1937eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 193810cc2b50SHerbert Xu rcu_barrier_bh(); 1939eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1940eb1d1641SHerbert Xu WARN_ON(mdb->old); 1941eb1d1641SHerbert Xu } 1942eb1d1641SHerbert Xu 1943eb1d1641SHerbert Xu mdb->old = mdb; 1944eb1d1641SHerbert Xu call_rcu_bh(&mdb->rcu, br_mdb_free); 1945eb1d1641SHerbert Xu 1946eb1d1641SHerbert Xu out: 1947eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 19481080ab95SNikolay Aleksandrov 19491080ab95SNikolay Aleksandrov free_percpu(br->mcast_stats); 1950eb1d1641SHerbert Xu } 19510909e117SHerbert Xu 19520909e117SHerbert Xu int br_multicast_set_router(struct net_bridge *br, unsigned long val) 19530909e117SHerbert Xu { 19546ae4ae8eSLinus Lüssing int err = -EINVAL; 19550909e117SHerbert Xu 19560909e117SHerbert Xu spin_lock_bh(&br->multicast_lock); 19570909e117SHerbert Xu 19580909e117SHerbert Xu switch (val) { 19597f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_DISABLED: 19607f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_PERM: 19610909e117SHerbert Xu del_timer(&br->multicast_router_timer); 19620909e117SHerbert Xu /* fall through */ 19637f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_TEMP_QUERY: 19640909e117SHerbert Xu br->multicast_router = val; 19650909e117SHerbert Xu err = 0; 19660909e117SHerbert Xu break; 19670909e117SHerbert Xu } 19680909e117SHerbert Xu 19690909e117SHerbert Xu spin_unlock_bh(&br->multicast_lock); 19700909e117SHerbert Xu 19710909e117SHerbert Xu return err; 19720909e117SHerbert Xu } 19730909e117SHerbert Xu 19747f0aec7aSNikolay Aleksandrov static void __del_port_router(struct net_bridge_port *p) 19757f0aec7aSNikolay Aleksandrov { 19767f0aec7aSNikolay Aleksandrov if (hlist_unhashed(&p->rlist)) 19777f0aec7aSNikolay Aleksandrov return; 19787f0aec7aSNikolay Aleksandrov hlist_del_init_rcu(&p->rlist); 19797f0aec7aSNikolay Aleksandrov br_rtr_notify(p->br->dev, p, RTM_DELMDB); 19807f0aec7aSNikolay Aleksandrov } 19817f0aec7aSNikolay Aleksandrov 19820909e117SHerbert Xu int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val) 19830909e117SHerbert Xu { 19840909e117SHerbert Xu struct net_bridge *br = p->br; 1985a55d8246SNikolay Aleksandrov unsigned long now = jiffies; 19866ae4ae8eSLinus Lüssing int err = -EINVAL; 19870909e117SHerbert Xu 19880909e117SHerbert Xu spin_lock(&br->multicast_lock); 19894950cfd1SNikolay Aleksandrov if (p->multicast_router == val) { 1990a55d8246SNikolay Aleksandrov /* Refresh the temp router port timer */ 1991a55d8246SNikolay Aleksandrov if (p->multicast_router == MDB_RTR_TYPE_TEMP) 1992a55d8246SNikolay Aleksandrov mod_timer(&p->multicast_router_timer, 1993a55d8246SNikolay Aleksandrov now + br->multicast_querier_interval); 19944950cfd1SNikolay Aleksandrov err = 0; 19954950cfd1SNikolay Aleksandrov goto unlock; 19964950cfd1SNikolay Aleksandrov } 19970909e117SHerbert Xu switch (val) { 19987f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_DISABLED: 19997f0aec7aSNikolay Aleksandrov p->multicast_router = MDB_RTR_TYPE_DISABLED; 20007f0aec7aSNikolay Aleksandrov __del_port_router(p); 20010909e117SHerbert Xu del_timer(&p->multicast_router_timer); 20020909e117SHerbert Xu break; 20037f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_TEMP_QUERY: 20047f0aec7aSNikolay Aleksandrov p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 20057f0aec7aSNikolay Aleksandrov __del_port_router(p); 20067f0aec7aSNikolay Aleksandrov break; 20077f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_PERM: 20087f0aec7aSNikolay Aleksandrov p->multicast_router = MDB_RTR_TYPE_PERM; 20097f0aec7aSNikolay Aleksandrov del_timer(&p->multicast_router_timer); 20100909e117SHerbert Xu br_multicast_add_router(br, p); 20110909e117SHerbert Xu break; 2012a55d8246SNikolay Aleksandrov case MDB_RTR_TYPE_TEMP: 2013a55d8246SNikolay Aleksandrov p->multicast_router = MDB_RTR_TYPE_TEMP; 2014a55d8246SNikolay Aleksandrov br_multicast_mark_router(br, p); 2015a55d8246SNikolay Aleksandrov break; 20167f0aec7aSNikolay Aleksandrov default: 20177f0aec7aSNikolay Aleksandrov goto unlock; 20180909e117SHerbert Xu } 20197f0aec7aSNikolay Aleksandrov err = 0; 20207f0aec7aSNikolay Aleksandrov unlock: 20210909e117SHerbert Xu spin_unlock(&br->multicast_lock); 20220909e117SHerbert Xu 20230909e117SHerbert Xu return err; 20240909e117SHerbert Xu } 2025561f1103SHerbert Xu 2026cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br, 202790010b36SLinus Lüssing struct bridge_mcast_own_query *query) 2028561f1103SHerbert Xu { 2029561f1103SHerbert Xu struct net_bridge_port *port; 203074857216SHerbert Xu 2031cc0fdd80SLinus Lüssing __br_multicast_open(br, query); 203274857216SHerbert Xu 203374857216SHerbert Xu list_for_each_entry(port, &br->port_list, list) { 203474857216SHerbert Xu if (port->state == BR_STATE_DISABLED || 203574857216SHerbert Xu port->state == BR_STATE_BLOCKING) 203674857216SHerbert Xu continue; 203774857216SHerbert Xu 203890010b36SLinus Lüssing if (query == &br->ip4_own_query) 203990010b36SLinus Lüssing br_multicast_enable(&port->ip4_own_query); 2040cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 2041cc0fdd80SLinus Lüssing else 204290010b36SLinus Lüssing br_multicast_enable(&port->ip6_own_query); 2043cc0fdd80SLinus Lüssing #endif 204474857216SHerbert Xu } 204574857216SHerbert Xu } 204674857216SHerbert Xu 204774857216SHerbert Xu int br_multicast_toggle(struct net_bridge *br, unsigned long val) 204874857216SHerbert Xu { 2049e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 20507cb3f921SNikolay Aleksandrov struct net_bridge_port *port; 20517cb3f921SNikolay Aleksandrov int err = 0; 2052561f1103SHerbert Xu 2053ef5e0d82SAndrey Vagin spin_lock_bh(&br->multicast_lock); 2054561f1103SHerbert Xu if (br->multicast_disabled == !val) 2055561f1103SHerbert Xu goto unlock; 2056561f1103SHerbert Xu 2057561f1103SHerbert Xu br->multicast_disabled = !val; 2058561f1103SHerbert Xu if (br->multicast_disabled) 2059561f1103SHerbert Xu goto unlock; 2060561f1103SHerbert Xu 20613a7fda06SHerbert Xu if (!netif_running(br->dev)) 20623a7fda06SHerbert Xu goto unlock; 20633a7fda06SHerbert Xu 2064e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 2065e8051688SEric Dumazet if (mdb) { 2066e8051688SEric Dumazet if (mdb->old) { 2067561f1103SHerbert Xu err = -EEXIST; 2068561f1103SHerbert Xu rollback: 2069561f1103SHerbert Xu br->multicast_disabled = !!val; 2070561f1103SHerbert Xu goto unlock; 2071561f1103SHerbert Xu } 2072561f1103SHerbert Xu 2073e8051688SEric Dumazet err = br_mdb_rehash(&br->mdb, mdb->max, 2074561f1103SHerbert Xu br->hash_elasticity); 2075561f1103SHerbert Xu if (err) 2076561f1103SHerbert Xu goto rollback; 2077561f1103SHerbert Xu } 2078561f1103SHerbert Xu 20797cb3f921SNikolay Aleksandrov br_multicast_open(br); 20807cb3f921SNikolay Aleksandrov list_for_each_entry(port, &br->port_list, list) 20817cb3f921SNikolay Aleksandrov __br_multicast_enable_port(port); 2082561f1103SHerbert Xu 2083561f1103SHerbert Xu unlock: 2084ef5e0d82SAndrey Vagin spin_unlock_bh(&br->multicast_lock); 2085561f1103SHerbert Xu 2086561f1103SHerbert Xu return err; 2087561f1103SHerbert Xu } 2088b195167fSHerbert Xu 2089c5c23260SHerbert Xu int br_multicast_set_querier(struct net_bridge *br, unsigned long val) 2090c5c23260SHerbert Xu { 2091b00589afSLinus Lüssing unsigned long max_delay; 2092b00589afSLinus Lüssing 2093c5c23260SHerbert Xu val = !!val; 2094c5c23260SHerbert Xu 2095c5c23260SHerbert Xu spin_lock_bh(&br->multicast_lock); 2096c5c23260SHerbert Xu if (br->multicast_querier == val) 2097c5c23260SHerbert Xu goto unlock; 2098c5c23260SHerbert Xu 2099c5c23260SHerbert Xu br->multicast_querier = val; 2100b00589afSLinus Lüssing if (!val) 2101b00589afSLinus Lüssing goto unlock; 2102b00589afSLinus Lüssing 2103b00589afSLinus Lüssing max_delay = br->multicast_query_response_interval; 2104b00589afSLinus Lüssing 210590010b36SLinus Lüssing if (!timer_pending(&br->ip4_other_query.timer)) 210690010b36SLinus Lüssing br->ip4_other_query.delay_time = jiffies + max_delay; 2107cc0fdd80SLinus Lüssing 210890010b36SLinus Lüssing br_multicast_start_querier(br, &br->ip4_own_query); 2109cc0fdd80SLinus Lüssing 2110cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 211190010b36SLinus Lüssing if (!timer_pending(&br->ip6_other_query.timer)) 211290010b36SLinus Lüssing br->ip6_other_query.delay_time = jiffies + max_delay; 2113cc0fdd80SLinus Lüssing 211490010b36SLinus Lüssing br_multicast_start_querier(br, &br->ip6_own_query); 2115cc0fdd80SLinus Lüssing #endif 2116c5c23260SHerbert Xu 2117c5c23260SHerbert Xu unlock: 2118c5c23260SHerbert Xu spin_unlock_bh(&br->multicast_lock); 2119c5c23260SHerbert Xu 2120c5c23260SHerbert Xu return 0; 2121c5c23260SHerbert Xu } 2122c5c23260SHerbert Xu 2123b195167fSHerbert Xu int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val) 2124b195167fSHerbert Xu { 21256ae4ae8eSLinus Lüssing int err = -EINVAL; 2126b195167fSHerbert Xu u32 old; 2127e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 2128b195167fSHerbert Xu 2129fe0d692bSCurt Brune spin_lock_bh(&br->multicast_lock); 2130b195167fSHerbert Xu if (!is_power_of_2(val)) 2131b195167fSHerbert Xu goto unlock; 2132e8051688SEric Dumazet 2133e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 2134e8051688SEric Dumazet if (mdb && val < mdb->size) 2135b195167fSHerbert Xu goto unlock; 2136b195167fSHerbert Xu 2137b195167fSHerbert Xu err = 0; 2138b195167fSHerbert Xu 2139b195167fSHerbert Xu old = br->hash_max; 2140b195167fSHerbert Xu br->hash_max = val; 2141b195167fSHerbert Xu 2142e8051688SEric Dumazet if (mdb) { 2143e8051688SEric Dumazet if (mdb->old) { 2144b195167fSHerbert Xu err = -EEXIST; 2145b195167fSHerbert Xu rollback: 2146b195167fSHerbert Xu br->hash_max = old; 2147b195167fSHerbert Xu goto unlock; 2148b195167fSHerbert Xu } 2149b195167fSHerbert Xu 2150b195167fSHerbert Xu err = br_mdb_rehash(&br->mdb, br->hash_max, 2151b195167fSHerbert Xu br->hash_elasticity); 2152b195167fSHerbert Xu if (err) 2153b195167fSHerbert Xu goto rollback; 2154b195167fSHerbert Xu } 2155b195167fSHerbert Xu 2156b195167fSHerbert Xu unlock: 2157fe0d692bSCurt Brune spin_unlock_bh(&br->multicast_lock); 2158b195167fSHerbert Xu 2159b195167fSHerbert Xu return err; 2160b195167fSHerbert Xu } 216107f8ac4aSLinus Lüssing 2162*5e923585SNikolay Aleksandrov int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val) 2163*5e923585SNikolay Aleksandrov { 2164*5e923585SNikolay Aleksandrov /* Currently we support only version 2 and 3 */ 2165*5e923585SNikolay Aleksandrov switch (val) { 2166*5e923585SNikolay Aleksandrov case 2: 2167*5e923585SNikolay Aleksandrov case 3: 2168*5e923585SNikolay Aleksandrov break; 2169*5e923585SNikolay Aleksandrov default: 2170*5e923585SNikolay Aleksandrov return -EINVAL; 2171*5e923585SNikolay Aleksandrov } 2172*5e923585SNikolay Aleksandrov 2173*5e923585SNikolay Aleksandrov spin_lock_bh(&br->multicast_lock); 2174*5e923585SNikolay Aleksandrov br->multicast_igmp_version = val; 2175*5e923585SNikolay Aleksandrov spin_unlock_bh(&br->multicast_lock); 2176*5e923585SNikolay Aleksandrov 2177*5e923585SNikolay Aleksandrov return 0; 2178*5e923585SNikolay Aleksandrov } 2179*5e923585SNikolay Aleksandrov 218007f8ac4aSLinus Lüssing /** 218107f8ac4aSLinus Lüssing * br_multicast_list_adjacent - Returns snooped multicast addresses 218207f8ac4aSLinus Lüssing * @dev: The bridge port adjacent to which to retrieve addresses 218307f8ac4aSLinus Lüssing * @br_ip_list: The list to store found, snooped multicast IP addresses in 218407f8ac4aSLinus Lüssing * 218507f8ac4aSLinus Lüssing * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast 218607f8ac4aSLinus Lüssing * snooping feature on all bridge ports of dev's bridge device, excluding 218707f8ac4aSLinus Lüssing * the addresses from dev itself. 218807f8ac4aSLinus Lüssing * 218907f8ac4aSLinus Lüssing * Returns the number of items added to br_ip_list. 219007f8ac4aSLinus Lüssing * 219107f8ac4aSLinus Lüssing * Notes: 219207f8ac4aSLinus Lüssing * - br_ip_list needs to be initialized by caller 219307f8ac4aSLinus Lüssing * - br_ip_list might contain duplicates in the end 219407f8ac4aSLinus Lüssing * (needs to be taken care of by caller) 219507f8ac4aSLinus Lüssing * - br_ip_list needs to be freed by caller 219607f8ac4aSLinus Lüssing */ 219707f8ac4aSLinus Lüssing int br_multicast_list_adjacent(struct net_device *dev, 219807f8ac4aSLinus Lüssing struct list_head *br_ip_list) 219907f8ac4aSLinus Lüssing { 220007f8ac4aSLinus Lüssing struct net_bridge *br; 220107f8ac4aSLinus Lüssing struct net_bridge_port *port; 220207f8ac4aSLinus Lüssing struct net_bridge_port_group *group; 220307f8ac4aSLinus Lüssing struct br_ip_list *entry; 220407f8ac4aSLinus Lüssing int count = 0; 220507f8ac4aSLinus Lüssing 220607f8ac4aSLinus Lüssing rcu_read_lock(); 220707f8ac4aSLinus Lüssing if (!br_ip_list || !br_port_exists(dev)) 220807f8ac4aSLinus Lüssing goto unlock; 220907f8ac4aSLinus Lüssing 221007f8ac4aSLinus Lüssing port = br_port_get_rcu(dev); 221107f8ac4aSLinus Lüssing if (!port || !port->br) 221207f8ac4aSLinus Lüssing goto unlock; 221307f8ac4aSLinus Lüssing 221407f8ac4aSLinus Lüssing br = port->br; 221507f8ac4aSLinus Lüssing 221607f8ac4aSLinus Lüssing list_for_each_entry_rcu(port, &br->port_list, list) { 221707f8ac4aSLinus Lüssing if (!port->dev || port->dev == dev) 221807f8ac4aSLinus Lüssing continue; 221907f8ac4aSLinus Lüssing 222007f8ac4aSLinus Lüssing hlist_for_each_entry_rcu(group, &port->mglist, mglist) { 222107f8ac4aSLinus Lüssing entry = kmalloc(sizeof(*entry), GFP_ATOMIC); 222207f8ac4aSLinus Lüssing if (!entry) 222307f8ac4aSLinus Lüssing goto unlock; 222407f8ac4aSLinus Lüssing 222507f8ac4aSLinus Lüssing entry->addr = group->addr; 222607f8ac4aSLinus Lüssing list_add(&entry->list, br_ip_list); 222707f8ac4aSLinus Lüssing count++; 222807f8ac4aSLinus Lüssing } 222907f8ac4aSLinus Lüssing } 223007f8ac4aSLinus Lüssing 223107f8ac4aSLinus Lüssing unlock: 223207f8ac4aSLinus Lüssing rcu_read_unlock(); 223307f8ac4aSLinus Lüssing return count; 223407f8ac4aSLinus Lüssing } 223507f8ac4aSLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_list_adjacent); 22362cd41431SLinus Lüssing 22372cd41431SLinus Lüssing /** 2238c34963e2SLinus Lüssing * br_multicast_has_querier_anywhere - Checks for a querier on a bridge 2239c34963e2SLinus Lüssing * @dev: The bridge port providing the bridge on which to check for a querier 2240c34963e2SLinus Lüssing * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 2241c34963e2SLinus Lüssing * 2242c34963e2SLinus Lüssing * Checks whether the given interface has a bridge on top and if so returns 2243c34963e2SLinus Lüssing * true if a valid querier exists anywhere on the bridged link layer. 2244c34963e2SLinus Lüssing * Otherwise returns false. 2245c34963e2SLinus Lüssing */ 2246c34963e2SLinus Lüssing bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto) 2247c34963e2SLinus Lüssing { 2248c34963e2SLinus Lüssing struct net_bridge *br; 2249c34963e2SLinus Lüssing struct net_bridge_port *port; 2250c34963e2SLinus Lüssing struct ethhdr eth; 2251c34963e2SLinus Lüssing bool ret = false; 2252c34963e2SLinus Lüssing 2253c34963e2SLinus Lüssing rcu_read_lock(); 2254c34963e2SLinus Lüssing if (!br_port_exists(dev)) 2255c34963e2SLinus Lüssing goto unlock; 2256c34963e2SLinus Lüssing 2257c34963e2SLinus Lüssing port = br_port_get_rcu(dev); 2258c34963e2SLinus Lüssing if (!port || !port->br) 2259c34963e2SLinus Lüssing goto unlock; 2260c34963e2SLinus Lüssing 2261c34963e2SLinus Lüssing br = port->br; 2262c34963e2SLinus Lüssing 2263c34963e2SLinus Lüssing memset(ð, 0, sizeof(eth)); 2264c34963e2SLinus Lüssing eth.h_proto = htons(proto); 2265c34963e2SLinus Lüssing 2266c34963e2SLinus Lüssing ret = br_multicast_querier_exists(br, ð); 2267c34963e2SLinus Lüssing 2268c34963e2SLinus Lüssing unlock: 2269c34963e2SLinus Lüssing rcu_read_unlock(); 2270c34963e2SLinus Lüssing return ret; 2271c34963e2SLinus Lüssing } 2272c34963e2SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere); 2273c34963e2SLinus Lüssing 2274c34963e2SLinus Lüssing /** 22752cd41431SLinus Lüssing * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port 22762cd41431SLinus Lüssing * @dev: The bridge port adjacent to which to check for a querier 22772cd41431SLinus Lüssing * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 22782cd41431SLinus Lüssing * 22792cd41431SLinus Lüssing * Checks whether the given interface has a bridge on top and if so returns 22802cd41431SLinus Lüssing * true if a selected querier is behind one of the other ports of this 22812cd41431SLinus Lüssing * bridge. Otherwise returns false. 22822cd41431SLinus Lüssing */ 22832cd41431SLinus Lüssing bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto) 22842cd41431SLinus Lüssing { 22852cd41431SLinus Lüssing struct net_bridge *br; 22862cd41431SLinus Lüssing struct net_bridge_port *port; 22872cd41431SLinus Lüssing bool ret = false; 22882cd41431SLinus Lüssing 22892cd41431SLinus Lüssing rcu_read_lock(); 22902cd41431SLinus Lüssing if (!br_port_exists(dev)) 22912cd41431SLinus Lüssing goto unlock; 22922cd41431SLinus Lüssing 22932cd41431SLinus Lüssing port = br_port_get_rcu(dev); 22942cd41431SLinus Lüssing if (!port || !port->br) 22952cd41431SLinus Lüssing goto unlock; 22962cd41431SLinus Lüssing 22972cd41431SLinus Lüssing br = port->br; 22982cd41431SLinus Lüssing 22992cd41431SLinus Lüssing switch (proto) { 23002cd41431SLinus Lüssing case ETH_P_IP: 23012cd41431SLinus Lüssing if (!timer_pending(&br->ip4_other_query.timer) || 23022cd41431SLinus Lüssing rcu_dereference(br->ip4_querier.port) == port) 23032cd41431SLinus Lüssing goto unlock; 23042cd41431SLinus Lüssing break; 23053993c4e1SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 23062cd41431SLinus Lüssing case ETH_P_IPV6: 23072cd41431SLinus Lüssing if (!timer_pending(&br->ip6_other_query.timer) || 23082cd41431SLinus Lüssing rcu_dereference(br->ip6_querier.port) == port) 23092cd41431SLinus Lüssing goto unlock; 23102cd41431SLinus Lüssing break; 23113993c4e1SLinus Lüssing #endif 23122cd41431SLinus Lüssing default: 23132cd41431SLinus Lüssing goto unlock; 23142cd41431SLinus Lüssing } 23152cd41431SLinus Lüssing 23162cd41431SLinus Lüssing ret = true; 23172cd41431SLinus Lüssing unlock: 23182cd41431SLinus Lüssing rcu_read_unlock(); 23192cd41431SLinus Lüssing return ret; 23202cd41431SLinus Lüssing } 23212cd41431SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent); 23221080ab95SNikolay Aleksandrov 23231080ab95SNikolay Aleksandrov static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats, 2324a65056ecSNikolay Aleksandrov const struct sk_buff *skb, u8 type, u8 dir) 23251080ab95SNikolay Aleksandrov { 23261080ab95SNikolay Aleksandrov struct bridge_mcast_stats *pstats = this_cpu_ptr(stats); 2327a65056ecSNikolay Aleksandrov __be16 proto = skb->protocol; 2328a65056ecSNikolay Aleksandrov unsigned int t_len; 23291080ab95SNikolay Aleksandrov 23301080ab95SNikolay Aleksandrov u64_stats_update_begin(&pstats->syncp); 23311080ab95SNikolay Aleksandrov switch (proto) { 23321080ab95SNikolay Aleksandrov case htons(ETH_P_IP): 2333a65056ecSNikolay Aleksandrov t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb); 23341080ab95SNikolay Aleksandrov switch (type) { 23351080ab95SNikolay Aleksandrov case IGMP_HOST_MEMBERSHIP_REPORT: 23361080ab95SNikolay Aleksandrov pstats->mstats.igmp_v1reports[dir]++; 23371080ab95SNikolay Aleksandrov break; 23381080ab95SNikolay Aleksandrov case IGMPV2_HOST_MEMBERSHIP_REPORT: 23391080ab95SNikolay Aleksandrov pstats->mstats.igmp_v2reports[dir]++; 23401080ab95SNikolay Aleksandrov break; 23411080ab95SNikolay Aleksandrov case IGMPV3_HOST_MEMBERSHIP_REPORT: 23421080ab95SNikolay Aleksandrov pstats->mstats.igmp_v3reports[dir]++; 23431080ab95SNikolay Aleksandrov break; 23441080ab95SNikolay Aleksandrov case IGMP_HOST_MEMBERSHIP_QUERY: 2345a65056ecSNikolay Aleksandrov if (t_len != sizeof(struct igmphdr)) { 2346a65056ecSNikolay Aleksandrov pstats->mstats.igmp_v3queries[dir]++; 2347a65056ecSNikolay Aleksandrov } else { 2348a65056ecSNikolay Aleksandrov unsigned int offset = skb_transport_offset(skb); 2349a65056ecSNikolay Aleksandrov struct igmphdr *ih, _ihdr; 2350a65056ecSNikolay Aleksandrov 2351a65056ecSNikolay Aleksandrov ih = skb_header_pointer(skb, offset, 2352a65056ecSNikolay Aleksandrov sizeof(_ihdr), &_ihdr); 2353a65056ecSNikolay Aleksandrov if (!ih) 2354a65056ecSNikolay Aleksandrov break; 2355a65056ecSNikolay Aleksandrov if (!ih->code) 2356a65056ecSNikolay Aleksandrov pstats->mstats.igmp_v1queries[dir]++; 2357a65056ecSNikolay Aleksandrov else 2358a65056ecSNikolay Aleksandrov pstats->mstats.igmp_v2queries[dir]++; 2359a65056ecSNikolay Aleksandrov } 23601080ab95SNikolay Aleksandrov break; 23611080ab95SNikolay Aleksandrov case IGMP_HOST_LEAVE_MESSAGE: 23621080ab95SNikolay Aleksandrov pstats->mstats.igmp_leaves[dir]++; 23631080ab95SNikolay Aleksandrov break; 23641080ab95SNikolay Aleksandrov } 23651080ab95SNikolay Aleksandrov break; 23661080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 23671080ab95SNikolay Aleksandrov case htons(ETH_P_IPV6): 2368a65056ecSNikolay Aleksandrov t_len = ntohs(ipv6_hdr(skb)->payload_len) + 2369a65056ecSNikolay Aleksandrov sizeof(struct ipv6hdr); 2370a65056ecSNikolay Aleksandrov t_len -= skb_network_header_len(skb); 23711080ab95SNikolay Aleksandrov switch (type) { 23721080ab95SNikolay Aleksandrov case ICMPV6_MGM_REPORT: 23731080ab95SNikolay Aleksandrov pstats->mstats.mld_v1reports[dir]++; 23741080ab95SNikolay Aleksandrov break; 23751080ab95SNikolay Aleksandrov case ICMPV6_MLD2_REPORT: 23761080ab95SNikolay Aleksandrov pstats->mstats.mld_v2reports[dir]++; 23771080ab95SNikolay Aleksandrov break; 23781080ab95SNikolay Aleksandrov case ICMPV6_MGM_QUERY: 2379a65056ecSNikolay Aleksandrov if (t_len != sizeof(struct mld_msg)) 2380a65056ecSNikolay Aleksandrov pstats->mstats.mld_v2queries[dir]++; 2381a65056ecSNikolay Aleksandrov else 2382a65056ecSNikolay Aleksandrov pstats->mstats.mld_v1queries[dir]++; 23831080ab95SNikolay Aleksandrov break; 23841080ab95SNikolay Aleksandrov case ICMPV6_MGM_REDUCTION: 23851080ab95SNikolay Aleksandrov pstats->mstats.mld_leaves[dir]++; 23861080ab95SNikolay Aleksandrov break; 23871080ab95SNikolay Aleksandrov } 23881080ab95SNikolay Aleksandrov break; 23891080ab95SNikolay Aleksandrov #endif /* CONFIG_IPV6 */ 23901080ab95SNikolay Aleksandrov } 23911080ab95SNikolay Aleksandrov u64_stats_update_end(&pstats->syncp); 23921080ab95SNikolay Aleksandrov } 23931080ab95SNikolay Aleksandrov 23941080ab95SNikolay Aleksandrov void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p, 2395a65056ecSNikolay Aleksandrov const struct sk_buff *skb, u8 type, u8 dir) 23961080ab95SNikolay Aleksandrov { 23971080ab95SNikolay Aleksandrov struct bridge_mcast_stats __percpu *stats; 23981080ab95SNikolay Aleksandrov 23991080ab95SNikolay Aleksandrov /* if multicast_disabled is true then igmp type can't be set */ 24001080ab95SNikolay Aleksandrov if (!type || !br->multicast_stats_enabled) 24011080ab95SNikolay Aleksandrov return; 24021080ab95SNikolay Aleksandrov 24031080ab95SNikolay Aleksandrov if (p) 24041080ab95SNikolay Aleksandrov stats = p->mcast_stats; 24051080ab95SNikolay Aleksandrov else 24061080ab95SNikolay Aleksandrov stats = br->mcast_stats; 24071080ab95SNikolay Aleksandrov if (WARN_ON(!stats)) 24081080ab95SNikolay Aleksandrov return; 24091080ab95SNikolay Aleksandrov 2410a65056ecSNikolay Aleksandrov br_mcast_stats_add(stats, skb, type, dir); 24111080ab95SNikolay Aleksandrov } 24121080ab95SNikolay Aleksandrov 24131080ab95SNikolay Aleksandrov int br_multicast_init_stats(struct net_bridge *br) 24141080ab95SNikolay Aleksandrov { 24151080ab95SNikolay Aleksandrov br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats); 24161080ab95SNikolay Aleksandrov if (!br->mcast_stats) 24171080ab95SNikolay Aleksandrov return -ENOMEM; 24181080ab95SNikolay Aleksandrov 24191080ab95SNikolay Aleksandrov return 0; 24201080ab95SNikolay Aleksandrov } 24211080ab95SNikolay Aleksandrov 24221080ab95SNikolay Aleksandrov static void mcast_stats_add_dir(u64 *dst, u64 *src) 24231080ab95SNikolay Aleksandrov { 24241080ab95SNikolay Aleksandrov dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX]; 24251080ab95SNikolay Aleksandrov dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX]; 24261080ab95SNikolay Aleksandrov } 24271080ab95SNikolay Aleksandrov 24281080ab95SNikolay Aleksandrov void br_multicast_get_stats(const struct net_bridge *br, 24291080ab95SNikolay Aleksandrov const struct net_bridge_port *p, 24301080ab95SNikolay Aleksandrov struct br_mcast_stats *dest) 24311080ab95SNikolay Aleksandrov { 24321080ab95SNikolay Aleksandrov struct bridge_mcast_stats __percpu *stats; 24331080ab95SNikolay Aleksandrov struct br_mcast_stats tdst; 24341080ab95SNikolay Aleksandrov int i; 24351080ab95SNikolay Aleksandrov 24361080ab95SNikolay Aleksandrov memset(dest, 0, sizeof(*dest)); 24371080ab95SNikolay Aleksandrov if (p) 24381080ab95SNikolay Aleksandrov stats = p->mcast_stats; 24391080ab95SNikolay Aleksandrov else 24401080ab95SNikolay Aleksandrov stats = br->mcast_stats; 24411080ab95SNikolay Aleksandrov if (WARN_ON(!stats)) 24421080ab95SNikolay Aleksandrov return; 24431080ab95SNikolay Aleksandrov 24441080ab95SNikolay Aleksandrov memset(&tdst, 0, sizeof(tdst)); 24451080ab95SNikolay Aleksandrov for_each_possible_cpu(i) { 24461080ab95SNikolay Aleksandrov struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i); 24471080ab95SNikolay Aleksandrov struct br_mcast_stats temp; 24481080ab95SNikolay Aleksandrov unsigned int start; 24491080ab95SNikolay Aleksandrov 24501080ab95SNikolay Aleksandrov do { 24511080ab95SNikolay Aleksandrov start = u64_stats_fetch_begin_irq(&cpu_stats->syncp); 24521080ab95SNikolay Aleksandrov memcpy(&temp, &cpu_stats->mstats, sizeof(temp)); 24531080ab95SNikolay Aleksandrov } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start)); 24541080ab95SNikolay Aleksandrov 2455a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries); 2456a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries); 2457a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries); 24581080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves); 24591080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports); 24601080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports); 24611080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports); 24621080ab95SNikolay Aleksandrov tdst.igmp_parse_errors += temp.igmp_parse_errors; 24631080ab95SNikolay Aleksandrov 2464a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries); 2465a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries); 24661080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves); 24671080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports); 24681080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports); 24691080ab95SNikolay Aleksandrov tdst.mld_parse_errors += temp.mld_parse_errors; 24701080ab95SNikolay Aleksandrov } 24711080ab95SNikolay Aleksandrov memcpy(dest, &tdst, sizeof(*dest)); 24721080ab95SNikolay Aleksandrov } 2473