12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2eb1d1641SHerbert Xu /* 3eb1d1641SHerbert Xu * Bridge multicast support. 4eb1d1641SHerbert Xu * 5eb1d1641SHerbert Xu * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au> 6eb1d1641SHerbert Xu */ 7eb1d1641SHerbert Xu 8eb1d1641SHerbert Xu #include <linux/err.h> 907f8ac4aSLinus Lüssing #include <linux/export.h> 10eb1d1641SHerbert Xu #include <linux/if_ether.h> 11eb1d1641SHerbert Xu #include <linux/igmp.h> 124b3087c7SLinus Lüssing #include <linux/in.h> 13eb1d1641SHerbert Xu #include <linux/jhash.h> 14eb1d1641SHerbert Xu #include <linux/kernel.h> 15b195167fSHerbert Xu #include <linux/log2.h> 16eb1d1641SHerbert Xu #include <linux/netdevice.h> 17eb1d1641SHerbert Xu #include <linux/netfilter_bridge.h> 18eb1d1641SHerbert Xu #include <linux/random.h> 19eb1d1641SHerbert Xu #include <linux/rculist.h> 20eb1d1641SHerbert Xu #include <linux/skbuff.h> 21eb1d1641SHerbert Xu #include <linux/slab.h> 22eb1d1641SHerbert Xu #include <linux/timer.h> 231c8ad5bfSCong Wang #include <linux/inetdevice.h> 2491b02d3dSNikolay Aleksandrov #include <linux/mroute.h> 25eb1d1641SHerbert Xu #include <net/ip.h> 26147c1e9bSNogah Frankel #include <net/switchdev.h> 27dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 284b3087c7SLinus Lüssing #include <linux/icmpv6.h> 2908b202b6SYOSHIFUJI Hideaki #include <net/ipv6.h> 3008b202b6SYOSHIFUJI Hideaki #include <net/mld.h> 31d4c4f07dSDavid S. Miller #include <net/ip6_checksum.h> 323c3769e6SLinus Lüssing #include <net/addrconf.h> 3308b202b6SYOSHIFUJI Hideaki #endif 34eb1d1641SHerbert Xu 35eb1d1641SHerbert Xu #include "br_private.h" 36eb1d1641SHerbert Xu 3719e3a9c9SNikolay Aleksandrov static const struct rhashtable_params br_mdb_rht_params = { 3819e3a9c9SNikolay Aleksandrov .head_offset = offsetof(struct net_bridge_mdb_entry, rhnode), 3919e3a9c9SNikolay Aleksandrov .key_offset = offsetof(struct net_bridge_mdb_entry, addr), 4019e3a9c9SNikolay Aleksandrov .key_len = sizeof(struct br_ip), 4119e3a9c9SNikolay Aleksandrov .automatic_shrinking = true, 4219e3a9c9SNikolay Aleksandrov }; 4319e3a9c9SNikolay Aleksandrov 44cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br, 4590010b36SLinus Lüssing struct bridge_mcast_own_query *query); 46754bc547SSatish Ashok static void br_multicast_add_router(struct net_bridge *br, 47754bc547SSatish Ashok struct net_bridge_port *port); 48bc8c20acSSatish Ashok static void br_ip4_multicast_leave_group(struct net_bridge *br, 49bc8c20acSSatish Ashok struct net_bridge_port *port, 50bc8c20acSSatish Ashok __be32 group, 516db6f0eaSFelix Fietkau __u16 vid, 526db6f0eaSFelix Fietkau const unsigned char *src); 5342c11ccfSNikolay Aleksandrov static void br_multicast_port_group_rexmit(struct timer_list *t); 546db6f0eaSFelix Fietkau 55f12e7d95SNogah Frankel static void __del_port_router(struct net_bridge_port *p); 56bc8c20acSSatish Ashok #if IS_ENABLED(CONFIG_IPV6) 57bc8c20acSSatish Ashok static void br_ip6_multicast_leave_group(struct net_bridge *br, 58bc8c20acSSatish Ashok struct net_bridge_port *port, 59bc8c20acSSatish Ashok const struct in6_addr *group, 606db6f0eaSFelix Fietkau __u16 vid, const unsigned char *src); 61bc8c20acSSatish Ashok #endif 62c83b8fabSHerbert Xu 6319e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip_get_rcu(struct net_bridge *br, 64cfd56754SCong Wang struct br_ip *dst) 657f285fa7SHerbert Xu { 6619e3a9c9SNikolay Aleksandrov return rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params); 677f285fa7SHerbert Xu } 687f285fa7SHerbert Xu 6919e3a9c9SNikolay Aleksandrov struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge *br, 7019e3a9c9SNikolay Aleksandrov struct br_ip *dst) 7119e3a9c9SNikolay Aleksandrov { 7219e3a9c9SNikolay Aleksandrov struct net_bridge_mdb_entry *ent; 7319e3a9c9SNikolay Aleksandrov 7419e3a9c9SNikolay Aleksandrov lockdep_assert_held_once(&br->multicast_lock); 7519e3a9c9SNikolay Aleksandrov 7619e3a9c9SNikolay Aleksandrov rcu_read_lock(); 7719e3a9c9SNikolay Aleksandrov ent = rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params); 7819e3a9c9SNikolay Aleksandrov rcu_read_unlock(); 7919e3a9c9SNikolay Aleksandrov 8019e3a9c9SNikolay Aleksandrov return ent; 8119e3a9c9SNikolay Aleksandrov } 8219e3a9c9SNikolay Aleksandrov 8319e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip4_get(struct net_bridge *br, 8419e3a9c9SNikolay Aleksandrov __be32 dst, __u16 vid) 85eb1d1641SHerbert Xu { 868ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_dst; 870821ec55SHerbert Xu 8819e3a9c9SNikolay Aleksandrov memset(&br_dst, 0, sizeof(br_dst)); 89eab3227bSNikolay Aleksandrov br_dst.dst.ip4 = dst; 908ef2a9a5SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IP); 91b0e9a30dSVlad Yasevich br_dst.vid = vid; 928ef2a9a5SYOSHIFUJI Hideaki 9319e3a9c9SNikolay Aleksandrov return br_mdb_ip_get(br, &br_dst); 948ef2a9a5SYOSHIFUJI Hideaki } 958ef2a9a5SYOSHIFUJI Hideaki 96dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 9719e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip6_get(struct net_bridge *br, 9819e3a9c9SNikolay Aleksandrov const struct in6_addr *dst, 99b0e9a30dSVlad Yasevich __u16 vid) 10008b202b6SYOSHIFUJI Hideaki { 10108b202b6SYOSHIFUJI Hideaki struct br_ip br_dst; 10208b202b6SYOSHIFUJI Hideaki 10319e3a9c9SNikolay Aleksandrov memset(&br_dst, 0, sizeof(br_dst)); 104eab3227bSNikolay Aleksandrov br_dst.dst.ip6 = *dst; 10508b202b6SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IPV6); 106b0e9a30dSVlad Yasevich br_dst.vid = vid; 10708b202b6SYOSHIFUJI Hideaki 10819e3a9c9SNikolay Aleksandrov return br_mdb_ip_get(br, &br_dst); 10908b202b6SYOSHIFUJI Hideaki } 11008b202b6SYOSHIFUJI Hideaki #endif 11108b202b6SYOSHIFUJI Hideaki 112eb1d1641SHerbert Xu struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br, 113fbca58a2SCong Wang struct sk_buff *skb, u16 vid) 114eb1d1641SHerbert Xu { 1158ef2a9a5SYOSHIFUJI Hideaki struct br_ip ip; 1168ef2a9a5SYOSHIFUJI Hideaki 11713cefad2SNikolay Aleksandrov if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) 118eb1d1641SHerbert Xu return NULL; 119eb1d1641SHerbert Xu 1208ef2a9a5SYOSHIFUJI Hideaki if (BR_INPUT_SKB_CB(skb)->igmp) 1218ef2a9a5SYOSHIFUJI Hideaki return NULL; 1228ef2a9a5SYOSHIFUJI Hideaki 12319e3a9c9SNikolay Aleksandrov memset(&ip, 0, sizeof(ip)); 1248ef2a9a5SYOSHIFUJI Hideaki ip.proto = skb->protocol; 125fbca58a2SCong Wang ip.vid = vid; 1268ef2a9a5SYOSHIFUJI Hideaki 127eb1d1641SHerbert Xu switch (skb->protocol) { 128eb1d1641SHerbert Xu case htons(ETH_P_IP): 129eab3227bSNikolay Aleksandrov ip.dst.ip4 = ip_hdr(skb)->daddr; 130*7d07a68cSNikolay Aleksandrov if (br->multicast_igmp_version == 3) { 131*7d07a68cSNikolay Aleksandrov struct net_bridge_mdb_entry *mdb; 132*7d07a68cSNikolay Aleksandrov 133*7d07a68cSNikolay Aleksandrov ip.src.ip4 = ip_hdr(skb)->saddr; 134*7d07a68cSNikolay Aleksandrov mdb = br_mdb_ip_get_rcu(br, &ip); 135*7d07a68cSNikolay Aleksandrov if (mdb) 136*7d07a68cSNikolay Aleksandrov return mdb; 137*7d07a68cSNikolay Aleksandrov ip.src.ip4 = 0; 138*7d07a68cSNikolay Aleksandrov } 139eb1d1641SHerbert Xu break; 140dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 14108b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 142eab3227bSNikolay Aleksandrov ip.dst.ip6 = ipv6_hdr(skb)->daddr; 143*7d07a68cSNikolay Aleksandrov if (br->multicast_mld_version == 2) { 144*7d07a68cSNikolay Aleksandrov struct net_bridge_mdb_entry *mdb; 145*7d07a68cSNikolay Aleksandrov 146*7d07a68cSNikolay Aleksandrov ip.src.ip6 = ipv6_hdr(skb)->saddr; 147*7d07a68cSNikolay Aleksandrov mdb = br_mdb_ip_get_rcu(br, &ip); 148*7d07a68cSNikolay Aleksandrov if (mdb) 149*7d07a68cSNikolay Aleksandrov return mdb; 150*7d07a68cSNikolay Aleksandrov memset(&ip.src.ip6, 0, sizeof(ip.src.ip6)); 151*7d07a68cSNikolay Aleksandrov } 15208b202b6SYOSHIFUJI Hideaki break; 15308b202b6SYOSHIFUJI Hideaki #endif 1548ef2a9a5SYOSHIFUJI Hideaki default: 1558ef2a9a5SYOSHIFUJI Hideaki return NULL; 156eb1d1641SHerbert Xu } 157eb1d1641SHerbert Xu 15819e3a9c9SNikolay Aleksandrov return br_mdb_ip_get_rcu(br, &ip); 159eb1d1641SHerbert Xu } 160eb1d1641SHerbert Xu 161e12cec65SNikolay Aleksandrov static void br_multicast_destroy_mdb_entry(struct net_bridge_mcast_gc *gc) 162e12cec65SNikolay Aleksandrov { 163e12cec65SNikolay Aleksandrov struct net_bridge_mdb_entry *mp; 164e12cec65SNikolay Aleksandrov 165e12cec65SNikolay Aleksandrov mp = container_of(gc, struct net_bridge_mdb_entry, mcast_gc); 166e12cec65SNikolay Aleksandrov WARN_ON(!hlist_unhashed(&mp->mdb_node)); 167e12cec65SNikolay Aleksandrov WARN_ON(mp->ports); 168e12cec65SNikolay Aleksandrov 169e12cec65SNikolay Aleksandrov del_timer_sync(&mp->timer); 170e12cec65SNikolay Aleksandrov kfree_rcu(mp, rcu); 171e12cec65SNikolay Aleksandrov } 172e12cec65SNikolay Aleksandrov 173e12cec65SNikolay Aleksandrov static void br_multicast_del_mdb_entry(struct net_bridge_mdb_entry *mp) 174e12cec65SNikolay Aleksandrov { 175e12cec65SNikolay Aleksandrov struct net_bridge *br = mp->br; 176e12cec65SNikolay Aleksandrov 177e12cec65SNikolay Aleksandrov rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode, 178e12cec65SNikolay Aleksandrov br_mdb_rht_params); 179e12cec65SNikolay Aleksandrov hlist_del_init_rcu(&mp->mdb_node); 180e12cec65SNikolay Aleksandrov hlist_add_head(&mp->mcast_gc.gc_node, &br->mcast_gc_list); 181e12cec65SNikolay Aleksandrov queue_work(system_long_wq, &br->mcast_gc_work); 182e12cec65SNikolay Aleksandrov } 183e12cec65SNikolay Aleksandrov 18488c1f37fSAllen Pais static void br_multicast_group_expired(struct timer_list *t) 185eb1d1641SHerbert Xu { 18688c1f37fSAllen Pais struct net_bridge_mdb_entry *mp = from_timer(mp, t, timer); 187eb1d1641SHerbert Xu struct net_bridge *br = mp->br; 188eb1d1641SHerbert Xu 189eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 190eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&mp->timer)) 191eb1d1641SHerbert Xu goto out; 192eb1d1641SHerbert Xu 1931bc844eeSNikolay Aleksandrov br_multicast_host_leave(mp, true); 194eb1d1641SHerbert Xu 195eb1d1641SHerbert Xu if (mp->ports) 196eb1d1641SHerbert Xu goto out; 197e12cec65SNikolay Aleksandrov br_multicast_del_mdb_entry(mp); 198eb1d1641SHerbert Xu out: 199eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 200eb1d1641SHerbert Xu } 201eb1d1641SHerbert Xu 202e12cec65SNikolay Aleksandrov static void br_multicast_destroy_group_src(struct net_bridge_mcast_gc *gc) 203e12cec65SNikolay Aleksandrov { 204e12cec65SNikolay Aleksandrov struct net_bridge_group_src *src; 205e12cec65SNikolay Aleksandrov 206e12cec65SNikolay Aleksandrov src = container_of(gc, struct net_bridge_group_src, mcast_gc); 207e12cec65SNikolay Aleksandrov WARN_ON(!hlist_unhashed(&src->node)); 208e12cec65SNikolay Aleksandrov 209e12cec65SNikolay Aleksandrov del_timer_sync(&src->timer); 210e12cec65SNikolay Aleksandrov kfree_rcu(src, rcu); 211e12cec65SNikolay Aleksandrov } 212e12cec65SNikolay Aleksandrov 2138b671779SNikolay Aleksandrov static void br_multicast_del_group_src(struct net_bridge_group_src *src) 2148b671779SNikolay Aleksandrov { 2158b671779SNikolay Aleksandrov struct net_bridge *br = src->pg->port->br; 2168b671779SNikolay Aleksandrov 2178b671779SNikolay Aleksandrov hlist_del_init_rcu(&src->node); 2188b671779SNikolay Aleksandrov src->pg->src_ents--; 219e12cec65SNikolay Aleksandrov hlist_add_head(&src->mcast_gc.gc_node, &br->mcast_gc_list); 220e12cec65SNikolay Aleksandrov queue_work(system_long_wq, &br->mcast_gc_work); 221e12cec65SNikolay Aleksandrov } 222e12cec65SNikolay Aleksandrov 223e12cec65SNikolay Aleksandrov static void br_multicast_destroy_port_group(struct net_bridge_mcast_gc *gc) 224e12cec65SNikolay Aleksandrov { 225e12cec65SNikolay Aleksandrov struct net_bridge_port_group *pg; 226e12cec65SNikolay Aleksandrov 227e12cec65SNikolay Aleksandrov pg = container_of(gc, struct net_bridge_port_group, mcast_gc); 228e12cec65SNikolay Aleksandrov WARN_ON(!hlist_unhashed(&pg->mglist)); 229e12cec65SNikolay Aleksandrov WARN_ON(!hlist_empty(&pg->src_list)); 230e12cec65SNikolay Aleksandrov 231e12cec65SNikolay Aleksandrov del_timer_sync(&pg->rexmit_timer); 232e12cec65SNikolay Aleksandrov del_timer_sync(&pg->timer); 233e12cec65SNikolay Aleksandrov kfree_rcu(pg, rcu); 2348b671779SNikolay Aleksandrov } 2358b671779SNikolay Aleksandrov 236681590bdSNikolay Aleksandrov void br_multicast_del_pg(struct net_bridge_mdb_entry *mp, 237681590bdSNikolay Aleksandrov struct net_bridge_port_group *pg, 238681590bdSNikolay Aleksandrov struct net_bridge_port_group __rcu **pp) 239681590bdSNikolay Aleksandrov { 240681590bdSNikolay Aleksandrov struct net_bridge *br = pg->port->br; 2418b671779SNikolay Aleksandrov struct net_bridge_group_src *ent; 2428b671779SNikolay Aleksandrov struct hlist_node *tmp; 243681590bdSNikolay Aleksandrov 244681590bdSNikolay Aleksandrov rcu_assign_pointer(*pp, pg->next); 245681590bdSNikolay Aleksandrov hlist_del_init(&pg->mglist); 2468b671779SNikolay Aleksandrov hlist_for_each_entry_safe(ent, tmp, &pg->src_list, node) 2478b671779SNikolay Aleksandrov br_multicast_del_group_src(ent); 24881f19838SNikolay Aleksandrov br_mdb_notify(br->dev, mp, pg, RTM_DELMDB); 249e12cec65SNikolay Aleksandrov hlist_add_head(&pg->mcast_gc.gc_node, &br->mcast_gc_list); 250e12cec65SNikolay Aleksandrov queue_work(system_long_wq, &br->mcast_gc_work); 251681590bdSNikolay Aleksandrov 252681590bdSNikolay Aleksandrov if (!mp->ports && !mp->host_joined && netif_running(br->dev)) 253681590bdSNikolay Aleksandrov mod_timer(&mp->timer, jiffies); 254681590bdSNikolay Aleksandrov } 255681590bdSNikolay Aleksandrov 256681590bdSNikolay Aleksandrov static void br_multicast_find_del_pg(struct net_bridge *br, 257eb1d1641SHerbert Xu struct net_bridge_port_group *pg) 258eb1d1641SHerbert Xu { 2598b671779SNikolay Aleksandrov struct net_bridge_port_group __rcu **pp; 260eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 261eb1d1641SHerbert Xu struct net_bridge_port_group *p; 262e8051688SEric Dumazet 26319e3a9c9SNikolay Aleksandrov mp = br_mdb_ip_get(br, &pg->addr); 264eb1d1641SHerbert Xu if (WARN_ON(!mp)) 265eb1d1641SHerbert Xu return; 266eb1d1641SHerbert Xu 267e8051688SEric Dumazet for (pp = &mp->ports; 268e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 269e8051688SEric Dumazet pp = &p->next) { 270eb1d1641SHerbert Xu if (p != pg) 271eb1d1641SHerbert Xu continue; 272eb1d1641SHerbert Xu 273681590bdSNikolay Aleksandrov br_multicast_del_pg(mp, pg, pp); 274eb1d1641SHerbert Xu return; 275eb1d1641SHerbert Xu } 276eb1d1641SHerbert Xu 277eb1d1641SHerbert Xu WARN_ON(1); 278eb1d1641SHerbert Xu } 279eb1d1641SHerbert Xu 28088c1f37fSAllen Pais static void br_multicast_port_group_expired(struct timer_list *t) 281eb1d1641SHerbert Xu { 28288c1f37fSAllen Pais struct net_bridge_port_group *pg = from_timer(pg, t, timer); 283d6c33d67SNikolay Aleksandrov struct net_bridge_group_src *src_ent; 284eb1d1641SHerbert Xu struct net_bridge *br = pg->port->br; 285d6c33d67SNikolay Aleksandrov struct hlist_node *tmp; 286d6c33d67SNikolay Aleksandrov bool changed; 287eb1d1641SHerbert Xu 288eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 289eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&pg->timer) || 2909d06b6d8SElad Raz hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT) 291eb1d1641SHerbert Xu goto out; 292eb1d1641SHerbert Xu 293d6c33d67SNikolay Aleksandrov changed = !!(pg->filter_mode == MCAST_EXCLUDE); 294d6c33d67SNikolay Aleksandrov pg->filter_mode = MCAST_INCLUDE; 295d6c33d67SNikolay Aleksandrov hlist_for_each_entry_safe(src_ent, tmp, &pg->src_list, node) { 296d6c33d67SNikolay Aleksandrov if (!timer_pending(&src_ent->timer)) { 297d6c33d67SNikolay Aleksandrov br_multicast_del_group_src(src_ent); 298d6c33d67SNikolay Aleksandrov changed = true; 299d6c33d67SNikolay Aleksandrov } 300d6c33d67SNikolay Aleksandrov } 301eb1d1641SHerbert Xu 302d6c33d67SNikolay Aleksandrov if (hlist_empty(&pg->src_list)) { 303d6c33d67SNikolay Aleksandrov br_multicast_find_del_pg(br, pg); 304d6c33d67SNikolay Aleksandrov } else if (changed) { 305d6c33d67SNikolay Aleksandrov struct net_bridge_mdb_entry *mp = br_mdb_ip_get(br, &pg->addr); 306d6c33d67SNikolay Aleksandrov 307d6c33d67SNikolay Aleksandrov if (WARN_ON(!mp)) 308d6c33d67SNikolay Aleksandrov goto out; 309d6c33d67SNikolay Aleksandrov br_mdb_notify(br->dev, mp, pg, RTM_NEWMDB); 310d6c33d67SNikolay Aleksandrov } 311eb1d1641SHerbert Xu out: 312eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 313eb1d1641SHerbert Xu } 314eb1d1641SHerbert Xu 315e12cec65SNikolay Aleksandrov static void br_multicast_gc(struct hlist_head *head) 316e12cec65SNikolay Aleksandrov { 317e12cec65SNikolay Aleksandrov struct net_bridge_mcast_gc *gcent; 318e12cec65SNikolay Aleksandrov struct hlist_node *tmp; 319e12cec65SNikolay Aleksandrov 320e12cec65SNikolay Aleksandrov hlist_for_each_entry_safe(gcent, tmp, head, gc_node) { 321e12cec65SNikolay Aleksandrov hlist_del_init(&gcent->gc_node); 322e12cec65SNikolay Aleksandrov gcent->destroy(gcent); 323e12cec65SNikolay Aleksandrov } 324e12cec65SNikolay Aleksandrov } 325e12cec65SNikolay Aleksandrov 3268ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br, 327438ef2d0SNikolay Aleksandrov struct net_bridge_port_group *pg, 328438ef2d0SNikolay Aleksandrov __be32 ip_dst, __be32 group, 329438ef2d0SNikolay Aleksandrov bool with_srcs, bool over_lmqt, 33042c11ccfSNikolay Aleksandrov u8 sflag, u8 *igmp_type, 33142c11ccfSNikolay Aleksandrov bool *need_rexmit) 332eb1d1641SHerbert Xu { 333438ef2d0SNikolay Aleksandrov struct net_bridge_port *p = pg ? pg->port : NULL; 334438ef2d0SNikolay Aleksandrov struct net_bridge_group_src *ent; 335438ef2d0SNikolay Aleksandrov size_t pkt_size, igmp_hdr_size; 336438ef2d0SNikolay Aleksandrov unsigned long now = jiffies; 3375e923585SNikolay Aleksandrov struct igmpv3_query *ihv3; 338438ef2d0SNikolay Aleksandrov void *csum_start = NULL; 339438ef2d0SNikolay Aleksandrov __sum16 *csum = NULL; 340eb1d1641SHerbert Xu struct sk_buff *skb; 341eb1d1641SHerbert Xu struct igmphdr *ih; 342eb1d1641SHerbert Xu struct ethhdr *eth; 343438ef2d0SNikolay Aleksandrov unsigned long lmqt; 344eb1d1641SHerbert Xu struct iphdr *iph; 345438ef2d0SNikolay Aleksandrov u16 lmqt_srcs = 0; 346eb1d1641SHerbert Xu 3475e923585SNikolay Aleksandrov igmp_hdr_size = sizeof(*ih); 348438ef2d0SNikolay Aleksandrov if (br->multicast_igmp_version == 3) { 3495e923585SNikolay Aleksandrov igmp_hdr_size = sizeof(*ihv3); 350438ef2d0SNikolay Aleksandrov if (pg && with_srcs) { 351438ef2d0SNikolay Aleksandrov lmqt = now + (br->multicast_last_member_interval * 352438ef2d0SNikolay Aleksandrov br->multicast_last_member_count); 353438ef2d0SNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) { 354438ef2d0SNikolay Aleksandrov if (over_lmqt == time_after(ent->timer.expires, 355438ef2d0SNikolay Aleksandrov lmqt) && 356438ef2d0SNikolay Aleksandrov ent->src_query_rexmit_cnt > 0) 357438ef2d0SNikolay Aleksandrov lmqt_srcs++; 358438ef2d0SNikolay Aleksandrov } 359438ef2d0SNikolay Aleksandrov 360438ef2d0SNikolay Aleksandrov if (!lmqt_srcs) 361438ef2d0SNikolay Aleksandrov return NULL; 362438ef2d0SNikolay Aleksandrov igmp_hdr_size += lmqt_srcs * sizeof(__be32); 363438ef2d0SNikolay Aleksandrov } 364438ef2d0SNikolay Aleksandrov } 365438ef2d0SNikolay Aleksandrov 366438ef2d0SNikolay Aleksandrov pkt_size = sizeof(*eth) + sizeof(*iph) + 4 + igmp_hdr_size; 367438ef2d0SNikolay Aleksandrov if ((p && pkt_size > p->dev->mtu) || 368438ef2d0SNikolay Aleksandrov pkt_size > br->dev->mtu) 369438ef2d0SNikolay Aleksandrov return NULL; 370438ef2d0SNikolay Aleksandrov 371438ef2d0SNikolay Aleksandrov skb = netdev_alloc_skb_ip_align(br->dev, pkt_size); 372eb1d1641SHerbert Xu if (!skb) 373eb1d1641SHerbert Xu goto out; 374eb1d1641SHerbert Xu 375eb1d1641SHerbert Xu skb->protocol = htons(ETH_P_IP); 376eb1d1641SHerbert Xu 377eb1d1641SHerbert Xu skb_reset_mac_header(skb); 378eb1d1641SHerbert Xu eth = eth_hdr(skb); 379eb1d1641SHerbert Xu 380e5a727f6SJoe Perches ether_addr_copy(eth->h_source, br->dev->dev_addr); 381438ef2d0SNikolay Aleksandrov ip_eth_mc_map(ip_dst, eth->h_dest); 382eb1d1641SHerbert Xu eth->h_proto = htons(ETH_P_IP); 383eb1d1641SHerbert Xu skb_put(skb, sizeof(*eth)); 384eb1d1641SHerbert Xu 385eb1d1641SHerbert Xu skb_set_network_header(skb, skb->len); 386eb1d1641SHerbert Xu iph = ip_hdr(skb); 387438ef2d0SNikolay Aleksandrov iph->tot_len = htons(pkt_size - sizeof(*eth)); 388eb1d1641SHerbert Xu 389eb1d1641SHerbert Xu iph->version = 4; 390eb1d1641SHerbert Xu iph->ihl = 6; 391eb1d1641SHerbert Xu iph->tos = 0xc0; 392eb1d1641SHerbert Xu iph->id = 0; 393eb1d1641SHerbert Xu iph->frag_off = htons(IP_DF); 394eb1d1641SHerbert Xu iph->ttl = 1; 395eb1d1641SHerbert Xu iph->protocol = IPPROTO_IGMP; 396675779adSNikolay Aleksandrov iph->saddr = br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR) ? 3971c8ad5bfSCong Wang inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0; 398438ef2d0SNikolay Aleksandrov iph->daddr = ip_dst; 399eb1d1641SHerbert Xu ((u8 *)&iph[1])[0] = IPOPT_RA; 400eb1d1641SHerbert Xu ((u8 *)&iph[1])[1] = 4; 401eb1d1641SHerbert Xu ((u8 *)&iph[1])[2] = 0; 402eb1d1641SHerbert Xu ((u8 *)&iph[1])[3] = 0; 403eb1d1641SHerbert Xu ip_send_check(iph); 404eb1d1641SHerbert Xu skb_put(skb, 24); 405eb1d1641SHerbert Xu 406eb1d1641SHerbert Xu skb_set_transport_header(skb, skb->len); 4071080ab95SNikolay Aleksandrov *igmp_type = IGMP_HOST_MEMBERSHIP_QUERY; 4085e923585SNikolay Aleksandrov 4095e923585SNikolay Aleksandrov switch (br->multicast_igmp_version) { 4105e923585SNikolay Aleksandrov case 2: 4115e923585SNikolay Aleksandrov ih = igmp_hdr(skb); 412eb1d1641SHerbert Xu ih->type = IGMP_HOST_MEMBERSHIP_QUERY; 413eb1d1641SHerbert Xu ih->code = (group ? br->multicast_last_member_interval : 414eb1d1641SHerbert Xu br->multicast_query_response_interval) / 415eb1d1641SHerbert Xu (HZ / IGMP_TIMER_SCALE); 416eb1d1641SHerbert Xu ih->group = group; 417eb1d1641SHerbert Xu ih->csum = 0; 418438ef2d0SNikolay Aleksandrov csum = &ih->csum; 419438ef2d0SNikolay Aleksandrov csum_start = (void *)ih; 4205e923585SNikolay Aleksandrov break; 4215e923585SNikolay Aleksandrov case 3: 4225e923585SNikolay Aleksandrov ihv3 = igmpv3_query_hdr(skb); 4235e923585SNikolay Aleksandrov ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY; 4245e923585SNikolay Aleksandrov ihv3->code = (group ? br->multicast_last_member_interval : 4255e923585SNikolay Aleksandrov br->multicast_query_response_interval) / 4265e923585SNikolay Aleksandrov (HZ / IGMP_TIMER_SCALE); 4275e923585SNikolay Aleksandrov ihv3->group = group; 4285e923585SNikolay Aleksandrov ihv3->qqic = br->multicast_query_interval / HZ; 429438ef2d0SNikolay Aleksandrov ihv3->nsrcs = htons(lmqt_srcs); 4305e923585SNikolay Aleksandrov ihv3->resv = 0; 431438ef2d0SNikolay Aleksandrov ihv3->suppress = sflag; 4325e923585SNikolay Aleksandrov ihv3->qrv = 2; 4335e923585SNikolay Aleksandrov ihv3->csum = 0; 434438ef2d0SNikolay Aleksandrov csum = &ihv3->csum; 435438ef2d0SNikolay Aleksandrov csum_start = (void *)ihv3; 436438ef2d0SNikolay Aleksandrov if (!pg || !with_srcs) 437438ef2d0SNikolay Aleksandrov break; 438438ef2d0SNikolay Aleksandrov 439438ef2d0SNikolay Aleksandrov lmqt_srcs = 0; 440438ef2d0SNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) { 441438ef2d0SNikolay Aleksandrov if (over_lmqt == time_after(ent->timer.expires, 442438ef2d0SNikolay Aleksandrov lmqt) && 443438ef2d0SNikolay Aleksandrov ent->src_query_rexmit_cnt > 0) { 444deb96566SNikolay Aleksandrov ihv3->srcs[lmqt_srcs++] = ent->addr.src.ip4; 445438ef2d0SNikolay Aleksandrov ent->src_query_rexmit_cnt--; 44642c11ccfSNikolay Aleksandrov if (need_rexmit && ent->src_query_rexmit_cnt) 44742c11ccfSNikolay Aleksandrov *need_rexmit = true; 448438ef2d0SNikolay Aleksandrov } 449438ef2d0SNikolay Aleksandrov } 450438ef2d0SNikolay Aleksandrov if (WARN_ON(lmqt_srcs != ntohs(ihv3->nsrcs))) { 451438ef2d0SNikolay Aleksandrov kfree_skb(skb); 452438ef2d0SNikolay Aleksandrov return NULL; 453438ef2d0SNikolay Aleksandrov } 4545e923585SNikolay Aleksandrov break; 4555e923585SNikolay Aleksandrov } 456eb1d1641SHerbert Xu 457438ef2d0SNikolay Aleksandrov if (WARN_ON(!csum || !csum_start)) { 458438ef2d0SNikolay Aleksandrov kfree_skb(skb); 459438ef2d0SNikolay Aleksandrov return NULL; 460438ef2d0SNikolay Aleksandrov } 461438ef2d0SNikolay Aleksandrov 462438ef2d0SNikolay Aleksandrov *csum = ip_compute_csum(csum_start, igmp_hdr_size); 4635e923585SNikolay Aleksandrov skb_put(skb, igmp_hdr_size); 464eb1d1641SHerbert Xu __skb_pull(skb, sizeof(*eth)); 465eb1d1641SHerbert Xu 466eb1d1641SHerbert Xu out: 467eb1d1641SHerbert Xu return skb; 468eb1d1641SHerbert Xu } 469eb1d1641SHerbert Xu 470dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 47108b202b6SYOSHIFUJI Hideaki static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br, 472438ef2d0SNikolay Aleksandrov struct net_bridge_port_group *pg, 473438ef2d0SNikolay Aleksandrov const struct in6_addr *ip6_dst, 474438ef2d0SNikolay Aleksandrov const struct in6_addr *group, 475438ef2d0SNikolay Aleksandrov bool with_srcs, bool over_llqt, 47642c11ccfSNikolay Aleksandrov u8 sflag, u8 *igmp_type, 47742c11ccfSNikolay Aleksandrov bool *need_rexmit) 47808b202b6SYOSHIFUJI Hideaki { 479438ef2d0SNikolay Aleksandrov struct net_bridge_port *p = pg ? pg->port : NULL; 480438ef2d0SNikolay Aleksandrov struct net_bridge_group_src *ent; 481438ef2d0SNikolay Aleksandrov size_t pkt_size, mld_hdr_size; 482438ef2d0SNikolay Aleksandrov unsigned long now = jiffies; 483aa2ae3e7SNikolay Aleksandrov struct mld2_query *mld2q; 484438ef2d0SNikolay Aleksandrov void *csum_start = NULL; 485aa2ae3e7SNikolay Aleksandrov unsigned long interval; 486438ef2d0SNikolay Aleksandrov __sum16 *csum = NULL; 48708b202b6SYOSHIFUJI Hideaki struct ipv6hdr *ip6h; 48808b202b6SYOSHIFUJI Hideaki struct mld_msg *mldq; 489aa2ae3e7SNikolay Aleksandrov struct sk_buff *skb; 490438ef2d0SNikolay Aleksandrov unsigned long llqt; 49108b202b6SYOSHIFUJI Hideaki struct ethhdr *eth; 492438ef2d0SNikolay Aleksandrov u16 llqt_srcs = 0; 49308b202b6SYOSHIFUJI Hideaki u8 *hopopt; 49408b202b6SYOSHIFUJI Hideaki 495aa2ae3e7SNikolay Aleksandrov mld_hdr_size = sizeof(*mldq); 496438ef2d0SNikolay Aleksandrov if (br->multicast_mld_version == 2) { 497aa2ae3e7SNikolay Aleksandrov mld_hdr_size = sizeof(*mld2q); 498438ef2d0SNikolay Aleksandrov if (pg && with_srcs) { 499438ef2d0SNikolay Aleksandrov llqt = now + (br->multicast_last_member_interval * 500438ef2d0SNikolay Aleksandrov br->multicast_last_member_count); 501438ef2d0SNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) { 502438ef2d0SNikolay Aleksandrov if (over_llqt == time_after(ent->timer.expires, 503438ef2d0SNikolay Aleksandrov llqt) && 504438ef2d0SNikolay Aleksandrov ent->src_query_rexmit_cnt > 0) 505438ef2d0SNikolay Aleksandrov llqt_srcs++; 506438ef2d0SNikolay Aleksandrov } 507438ef2d0SNikolay Aleksandrov 508438ef2d0SNikolay Aleksandrov if (!llqt_srcs) 509438ef2d0SNikolay Aleksandrov return NULL; 510438ef2d0SNikolay Aleksandrov mld_hdr_size += llqt_srcs * sizeof(struct in6_addr); 511438ef2d0SNikolay Aleksandrov } 512438ef2d0SNikolay Aleksandrov } 513438ef2d0SNikolay Aleksandrov 514438ef2d0SNikolay Aleksandrov pkt_size = sizeof(*eth) + sizeof(*ip6h) + 8 + mld_hdr_size; 515438ef2d0SNikolay Aleksandrov if ((p && pkt_size > p->dev->mtu) || 516438ef2d0SNikolay Aleksandrov pkt_size > br->dev->mtu) 517438ef2d0SNikolay Aleksandrov return NULL; 518438ef2d0SNikolay Aleksandrov 519438ef2d0SNikolay Aleksandrov skb = netdev_alloc_skb_ip_align(br->dev, pkt_size); 52008b202b6SYOSHIFUJI Hideaki if (!skb) 52108b202b6SYOSHIFUJI Hideaki goto out; 52208b202b6SYOSHIFUJI Hideaki 52308b202b6SYOSHIFUJI Hideaki skb->protocol = htons(ETH_P_IPV6); 52408b202b6SYOSHIFUJI Hideaki 52508b202b6SYOSHIFUJI Hideaki /* Ethernet header */ 52608b202b6SYOSHIFUJI Hideaki skb_reset_mac_header(skb); 52708b202b6SYOSHIFUJI Hideaki eth = eth_hdr(skb); 52808b202b6SYOSHIFUJI Hideaki 529e5a727f6SJoe Perches ether_addr_copy(eth->h_source, br->dev->dev_addr); 53008b202b6SYOSHIFUJI Hideaki eth->h_proto = htons(ETH_P_IPV6); 53108b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*eth)); 53208b202b6SYOSHIFUJI Hideaki 53308b202b6SYOSHIFUJI Hideaki /* IPv6 header + HbH option */ 53408b202b6SYOSHIFUJI Hideaki skb_set_network_header(skb, skb->len); 53508b202b6SYOSHIFUJI Hideaki ip6h = ipv6_hdr(skb); 53608b202b6SYOSHIFUJI Hideaki 53708b202b6SYOSHIFUJI Hideaki *(__force __be32 *)ip6h = htonl(0x60000000); 538aa2ae3e7SNikolay Aleksandrov ip6h->payload_len = htons(8 + mld_hdr_size); 53908b202b6SYOSHIFUJI Hideaki ip6h->nexthdr = IPPROTO_HOPOPTS; 54008b202b6SYOSHIFUJI Hideaki ip6h->hop_limit = 1; 541438ef2d0SNikolay Aleksandrov ip6h->daddr = *ip6_dst; 542d1d81d4cSUlrich Weber if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0, 543d1d81d4cSUlrich Weber &ip6h->saddr)) { 544d1d81d4cSUlrich Weber kfree_skb(skb); 545675779adSNikolay Aleksandrov br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, false); 546d1d81d4cSUlrich Weber return NULL; 547d1d81d4cSUlrich Weber } 5480888d5f3Sdaniel 549675779adSNikolay Aleksandrov br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true); 55036cff5a1SLinus Lüssing ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest); 55108b202b6SYOSHIFUJI Hideaki 55208b202b6SYOSHIFUJI Hideaki hopopt = (u8 *)(ip6h + 1); 55308b202b6SYOSHIFUJI Hideaki hopopt[0] = IPPROTO_ICMPV6; /* next hdr */ 55408b202b6SYOSHIFUJI Hideaki hopopt[1] = 0; /* length of HbH */ 55508b202b6SYOSHIFUJI Hideaki hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */ 55608b202b6SYOSHIFUJI Hideaki hopopt[3] = 2; /* Length of RA Option */ 55708b202b6SYOSHIFUJI Hideaki hopopt[4] = 0; /* Type = 0x0000 (MLD) */ 55808b202b6SYOSHIFUJI Hideaki hopopt[5] = 0; 5591de5a71cSEldad Zack hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */ 5601de5a71cSEldad Zack hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */ 56108b202b6SYOSHIFUJI Hideaki 56208b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*ip6h) + 8); 56308b202b6SYOSHIFUJI Hideaki 56408b202b6SYOSHIFUJI Hideaki /* ICMPv6 */ 56508b202b6SYOSHIFUJI Hideaki skb_set_transport_header(skb, skb->len); 566438ef2d0SNikolay Aleksandrov interval = ipv6_addr_any(group) ? 56732de868cSLinus Lüssing br->multicast_query_response_interval : 56832de868cSLinus Lüssing br->multicast_last_member_interval; 5691080ab95SNikolay Aleksandrov *igmp_type = ICMPV6_MGM_QUERY; 570aa2ae3e7SNikolay Aleksandrov switch (br->multicast_mld_version) { 571aa2ae3e7SNikolay Aleksandrov case 1: 572aa2ae3e7SNikolay Aleksandrov mldq = (struct mld_msg *)icmp6_hdr(skb); 57308b202b6SYOSHIFUJI Hideaki mldq->mld_type = ICMPV6_MGM_QUERY; 57408b202b6SYOSHIFUJI Hideaki mldq->mld_code = 0; 57508b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = 0; 57608b202b6SYOSHIFUJI Hideaki mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval)); 57708b202b6SYOSHIFUJI Hideaki mldq->mld_reserved = 0; 578438ef2d0SNikolay Aleksandrov mldq->mld_mca = *group; 579438ef2d0SNikolay Aleksandrov csum = &mldq->mld_cksum; 580438ef2d0SNikolay Aleksandrov csum_start = (void *)mldq; 581aa2ae3e7SNikolay Aleksandrov break; 582aa2ae3e7SNikolay Aleksandrov case 2: 583aa2ae3e7SNikolay Aleksandrov mld2q = (struct mld2_query *)icmp6_hdr(skb); 58453631a5fSLance Richardson mld2q->mld2q_mrc = htons((u16)jiffies_to_msecs(interval)); 585aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_type = ICMPV6_MGM_QUERY; 586aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_code = 0; 587aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_cksum = 0; 588aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_resv1 = 0; 589aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_resv2 = 0; 590438ef2d0SNikolay Aleksandrov mld2q->mld2q_suppress = sflag; 591aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_qrv = 2; 592438ef2d0SNikolay Aleksandrov mld2q->mld2q_nsrcs = htons(llqt_srcs); 593aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_qqic = br->multicast_query_interval / HZ; 594438ef2d0SNikolay Aleksandrov mld2q->mld2q_mca = *group; 595438ef2d0SNikolay Aleksandrov csum = &mld2q->mld2q_cksum; 596438ef2d0SNikolay Aleksandrov csum_start = (void *)mld2q; 597438ef2d0SNikolay Aleksandrov if (!pg || !with_srcs) 598438ef2d0SNikolay Aleksandrov break; 599438ef2d0SNikolay Aleksandrov 600438ef2d0SNikolay Aleksandrov llqt_srcs = 0; 601438ef2d0SNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) { 602438ef2d0SNikolay Aleksandrov if (over_llqt == time_after(ent->timer.expires, 603438ef2d0SNikolay Aleksandrov llqt) && 604438ef2d0SNikolay Aleksandrov ent->src_query_rexmit_cnt > 0) { 605deb96566SNikolay Aleksandrov mld2q->mld2q_srcs[llqt_srcs++] = ent->addr.src.ip6; 606438ef2d0SNikolay Aleksandrov ent->src_query_rexmit_cnt--; 60742c11ccfSNikolay Aleksandrov if (need_rexmit && ent->src_query_rexmit_cnt) 60842c11ccfSNikolay Aleksandrov *need_rexmit = true; 609438ef2d0SNikolay Aleksandrov } 610438ef2d0SNikolay Aleksandrov } 611438ef2d0SNikolay Aleksandrov if (WARN_ON(llqt_srcs != ntohs(mld2q->mld2q_nsrcs))) { 612438ef2d0SNikolay Aleksandrov kfree_skb(skb); 613438ef2d0SNikolay Aleksandrov return NULL; 614438ef2d0SNikolay Aleksandrov } 615aa2ae3e7SNikolay Aleksandrov break; 616aa2ae3e7SNikolay Aleksandrov } 61708b202b6SYOSHIFUJI Hideaki 618438ef2d0SNikolay Aleksandrov if (WARN_ON(!csum || !csum_start)) { 619438ef2d0SNikolay Aleksandrov kfree_skb(skb); 620438ef2d0SNikolay Aleksandrov return NULL; 621438ef2d0SNikolay Aleksandrov } 622438ef2d0SNikolay Aleksandrov 623438ef2d0SNikolay Aleksandrov *csum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, mld_hdr_size, 624438ef2d0SNikolay Aleksandrov IPPROTO_ICMPV6, 625438ef2d0SNikolay Aleksandrov csum_partial(csum_start, mld_hdr_size, 0)); 626438ef2d0SNikolay Aleksandrov skb_put(skb, mld_hdr_size); 62708b202b6SYOSHIFUJI Hideaki __skb_pull(skb, sizeof(*eth)); 62808b202b6SYOSHIFUJI Hideaki 62908b202b6SYOSHIFUJI Hideaki out: 63008b202b6SYOSHIFUJI Hideaki return skb; 63108b202b6SYOSHIFUJI Hideaki } 63208b202b6SYOSHIFUJI Hideaki #endif 63308b202b6SYOSHIFUJI Hideaki 6348ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br, 635438ef2d0SNikolay Aleksandrov struct net_bridge_port_group *pg, 636438ef2d0SNikolay Aleksandrov struct br_ip *ip_dst, 637438ef2d0SNikolay Aleksandrov struct br_ip *group, 638438ef2d0SNikolay Aleksandrov bool with_srcs, bool over_lmqt, 63942c11ccfSNikolay Aleksandrov u8 sflag, u8 *igmp_type, 64042c11ccfSNikolay Aleksandrov bool *need_rexmit) 6418ef2a9a5SYOSHIFUJI Hideaki { 642438ef2d0SNikolay Aleksandrov __be32 ip4_dst; 643438ef2d0SNikolay Aleksandrov 644438ef2d0SNikolay Aleksandrov switch (group->proto) { 6458ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 646eab3227bSNikolay Aleksandrov ip4_dst = ip_dst ? ip_dst->dst.ip4 : htonl(INADDR_ALLHOSTS_GROUP); 647438ef2d0SNikolay Aleksandrov return br_ip4_multicast_alloc_query(br, pg, 648eab3227bSNikolay Aleksandrov ip4_dst, group->dst.ip4, 649438ef2d0SNikolay Aleksandrov with_srcs, over_lmqt, 65042c11ccfSNikolay Aleksandrov sflag, igmp_type, 65142c11ccfSNikolay Aleksandrov need_rexmit); 652dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 653438ef2d0SNikolay Aleksandrov case htons(ETH_P_IPV6): { 654438ef2d0SNikolay Aleksandrov struct in6_addr ip6_dst; 655438ef2d0SNikolay Aleksandrov 656438ef2d0SNikolay Aleksandrov if (ip_dst) 657eab3227bSNikolay Aleksandrov ip6_dst = ip_dst->dst.ip6; 658438ef2d0SNikolay Aleksandrov else 659438ef2d0SNikolay Aleksandrov ipv6_addr_set(&ip6_dst, htonl(0xff020000), 0, 0, 660438ef2d0SNikolay Aleksandrov htonl(1)); 661438ef2d0SNikolay Aleksandrov 662438ef2d0SNikolay Aleksandrov return br_ip6_multicast_alloc_query(br, pg, 663eab3227bSNikolay Aleksandrov &ip6_dst, &group->dst.ip6, 664438ef2d0SNikolay Aleksandrov with_srcs, over_lmqt, 66542c11ccfSNikolay Aleksandrov sflag, igmp_type, 66642c11ccfSNikolay Aleksandrov need_rexmit); 667438ef2d0SNikolay Aleksandrov } 66808b202b6SYOSHIFUJI Hideaki #endif 6698ef2a9a5SYOSHIFUJI Hideaki } 6708ef2a9a5SYOSHIFUJI Hideaki return NULL; 6718ef2a9a5SYOSHIFUJI Hideaki } 6728ef2a9a5SYOSHIFUJI Hideaki 673cfd56754SCong Wang struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br, 6745e923585SNikolay Aleksandrov struct br_ip *group) 675eb1d1641SHerbert Xu { 676eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 6774c0833bcSTobias Klauser int err; 678eb1d1641SHerbert Xu 67919e3a9c9SNikolay Aleksandrov mp = br_mdb_ip_get(br, group); 68019e3a9c9SNikolay Aleksandrov if (mp) 68119e3a9c9SNikolay Aleksandrov return mp; 682eb1d1641SHerbert Xu 68319e3a9c9SNikolay Aleksandrov if (atomic_read(&br->mdb_hash_tbl.nelems) >= br->hash_max) { 68419e3a9c9SNikolay Aleksandrov br_opt_toggle(br, BROPT_MULTICAST_ENABLED, false); 68519e3a9c9SNikolay Aleksandrov return ERR_PTR(-E2BIG); 686eb1d1641SHerbert Xu } 687eb1d1641SHerbert Xu 688eb1d1641SHerbert Xu mp = kzalloc(sizeof(*mp), GFP_ATOMIC); 689eb1d1641SHerbert Xu if (unlikely(!mp)) 6904c0833bcSTobias Klauser return ERR_PTR(-ENOMEM); 691eb1d1641SHerbert Xu 692eb1d1641SHerbert Xu mp->br = br; 6938ef2a9a5SYOSHIFUJI Hideaki mp->addr = *group; 694e12cec65SNikolay Aleksandrov mp->mcast_gc.destroy = br_multicast_destroy_mdb_entry; 69588c1f37fSAllen Pais timer_setup(&mp->timer, br_multicast_group_expired, 0); 69619e3a9c9SNikolay Aleksandrov err = rhashtable_lookup_insert_fast(&br->mdb_hash_tbl, &mp->rhnode, 69719e3a9c9SNikolay Aleksandrov br_mdb_rht_params); 69819e3a9c9SNikolay Aleksandrov if (err) { 69919e3a9c9SNikolay Aleksandrov kfree(mp); 70019e3a9c9SNikolay Aleksandrov mp = ERR_PTR(err); 70119e3a9c9SNikolay Aleksandrov } else { 70219e3a9c9SNikolay Aleksandrov hlist_add_head_rcu(&mp->mdb_node, &br->mdb_list); 70319e3a9c9SNikolay Aleksandrov } 7041faabf2aSEric Dumazet 705eb1d1641SHerbert Xu return mp; 706eb1d1641SHerbert Xu } 707eb1d1641SHerbert Xu 7088b671779SNikolay Aleksandrov static void br_multicast_group_src_expired(struct timer_list *t) 7098b671779SNikolay Aleksandrov { 7108b671779SNikolay Aleksandrov struct net_bridge_group_src *src = from_timer(src, t, timer); 7118b671779SNikolay Aleksandrov struct net_bridge_port_group *pg; 7128b671779SNikolay Aleksandrov struct net_bridge *br = src->br; 7138b671779SNikolay Aleksandrov 7148b671779SNikolay Aleksandrov spin_lock(&br->multicast_lock); 7158b671779SNikolay Aleksandrov if (hlist_unhashed(&src->node) || !netif_running(br->dev) || 7168b671779SNikolay Aleksandrov timer_pending(&src->timer)) 7178b671779SNikolay Aleksandrov goto out; 7188b671779SNikolay Aleksandrov 7198b671779SNikolay Aleksandrov pg = src->pg; 7208b671779SNikolay Aleksandrov if (pg->filter_mode == MCAST_INCLUDE) { 7218b671779SNikolay Aleksandrov br_multicast_del_group_src(src); 7228b671779SNikolay Aleksandrov if (!hlist_empty(&pg->src_list)) 7238b671779SNikolay Aleksandrov goto out; 7248b671779SNikolay Aleksandrov br_multicast_find_del_pg(br, pg); 7258b671779SNikolay Aleksandrov } 7268b671779SNikolay Aleksandrov out: 7278b671779SNikolay Aleksandrov spin_unlock(&br->multicast_lock); 7288b671779SNikolay Aleksandrov } 7298b671779SNikolay Aleksandrov 7308b671779SNikolay Aleksandrov static struct net_bridge_group_src * 7318b671779SNikolay Aleksandrov br_multicast_find_group_src(struct net_bridge_port_group *pg, struct br_ip *ip) 7328b671779SNikolay Aleksandrov { 7338b671779SNikolay Aleksandrov struct net_bridge_group_src *ent; 7348b671779SNikolay Aleksandrov 7358b671779SNikolay Aleksandrov switch (ip->proto) { 7368b671779SNikolay Aleksandrov case htons(ETH_P_IP): 7378b671779SNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 738deb96566SNikolay Aleksandrov if (ip->src.ip4 == ent->addr.src.ip4) 7398b671779SNikolay Aleksandrov return ent; 7408b671779SNikolay Aleksandrov break; 7418b671779SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 7428b671779SNikolay Aleksandrov case htons(ETH_P_IPV6): 7438b671779SNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 744deb96566SNikolay Aleksandrov if (!ipv6_addr_cmp(&ent->addr.src.ip6, &ip->src.ip6)) 7458b671779SNikolay Aleksandrov return ent; 7468b671779SNikolay Aleksandrov break; 7478b671779SNikolay Aleksandrov #endif 7488b671779SNikolay Aleksandrov } 7498b671779SNikolay Aleksandrov 7508b671779SNikolay Aleksandrov return NULL; 7518b671779SNikolay Aleksandrov } 7528b671779SNikolay Aleksandrov 7538b671779SNikolay Aleksandrov static struct net_bridge_group_src * 7548b671779SNikolay Aleksandrov br_multicast_new_group_src(struct net_bridge_port_group *pg, struct br_ip *src_ip) 7558b671779SNikolay Aleksandrov { 7568b671779SNikolay Aleksandrov struct net_bridge_group_src *grp_src; 7578b671779SNikolay Aleksandrov 7588b671779SNikolay Aleksandrov if (unlikely(pg->src_ents >= PG_SRC_ENT_LIMIT)) 7598b671779SNikolay Aleksandrov return NULL; 7608b671779SNikolay Aleksandrov 7618b671779SNikolay Aleksandrov switch (src_ip->proto) { 7628b671779SNikolay Aleksandrov case htons(ETH_P_IP): 763deb96566SNikolay Aleksandrov if (ipv4_is_zeronet(src_ip->src.ip4) || 764deb96566SNikolay Aleksandrov ipv4_is_multicast(src_ip->src.ip4)) 7658b671779SNikolay Aleksandrov return NULL; 7668b671779SNikolay Aleksandrov break; 7678b671779SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 7688b671779SNikolay Aleksandrov case htons(ETH_P_IPV6): 769deb96566SNikolay Aleksandrov if (ipv6_addr_any(&src_ip->src.ip6) || 770deb96566SNikolay Aleksandrov ipv6_addr_is_multicast(&src_ip->src.ip6)) 7718b671779SNikolay Aleksandrov return NULL; 7728b671779SNikolay Aleksandrov break; 7738b671779SNikolay Aleksandrov #endif 7748b671779SNikolay Aleksandrov } 7758b671779SNikolay Aleksandrov 7768b671779SNikolay Aleksandrov grp_src = kzalloc(sizeof(*grp_src), GFP_ATOMIC); 7778b671779SNikolay Aleksandrov if (unlikely(!grp_src)) 7788b671779SNikolay Aleksandrov return NULL; 7798b671779SNikolay Aleksandrov 7808b671779SNikolay Aleksandrov grp_src->pg = pg; 7818b671779SNikolay Aleksandrov grp_src->br = pg->port->br; 7828b671779SNikolay Aleksandrov grp_src->addr = *src_ip; 783e12cec65SNikolay Aleksandrov grp_src->mcast_gc.destroy = br_multicast_destroy_group_src; 7848b671779SNikolay Aleksandrov timer_setup(&grp_src->timer, br_multicast_group_src_expired, 0); 7858b671779SNikolay Aleksandrov 7868b671779SNikolay Aleksandrov hlist_add_head_rcu(&grp_src->node, &pg->src_list); 7878b671779SNikolay Aleksandrov pg->src_ents++; 7888b671779SNikolay Aleksandrov 7898b671779SNikolay Aleksandrov return grp_src; 7908b671779SNikolay Aleksandrov } 7918b671779SNikolay Aleksandrov 792cfd56754SCong Wang struct net_bridge_port_group *br_multicast_new_port_group( 793cfd56754SCong Wang struct net_bridge_port *port, 794cfd56754SCong Wang struct br_ip *group, 795ccb1c31aSAmerigo Wang struct net_bridge_port_group __rcu *next, 7966db6f0eaSFelix Fietkau unsigned char flags, 7978b671779SNikolay Aleksandrov const unsigned char *src, 7988b671779SNikolay Aleksandrov u8 filter_mode) 799cfd56754SCong Wang { 800cfd56754SCong Wang struct net_bridge_port_group *p; 801cfd56754SCong Wang 802cfd56754SCong Wang p = kzalloc(sizeof(*p), GFP_ATOMIC); 803cfd56754SCong Wang if (unlikely(!p)) 804cfd56754SCong Wang return NULL; 805cfd56754SCong Wang 806cfd56754SCong Wang p->addr = *group; 807cfd56754SCong Wang p->port = port; 8089d06b6d8SElad Raz p->flags = flags; 8098b671779SNikolay Aleksandrov p->filter_mode = filter_mode; 810e12cec65SNikolay Aleksandrov p->mcast_gc.destroy = br_multicast_destroy_port_group; 8118b671779SNikolay Aleksandrov INIT_HLIST_HEAD(&p->src_list); 812eca2a43bSstephen hemminger rcu_assign_pointer(p->next, next); 81388c1f37fSAllen Pais timer_setup(&p->timer, br_multicast_port_group_expired, 0); 81442c11ccfSNikolay Aleksandrov timer_setup(&p->rexmit_timer, br_multicast_port_group_rexmit, 0); 81542c11ccfSNikolay Aleksandrov hlist_add_head(&p->mglist, &port->mglist); 8166db6f0eaSFelix Fietkau 8176db6f0eaSFelix Fietkau if (src) 8186db6f0eaSFelix Fietkau memcpy(p->eth_addr, src, ETH_ALEN); 8196db6f0eaSFelix Fietkau else 8201bfe45f4SMao Wenan eth_broadcast_addr(p->eth_addr); 8216db6f0eaSFelix Fietkau 822cfd56754SCong Wang return p; 823cfd56754SCong Wang } 824cfd56754SCong Wang 8256db6f0eaSFelix Fietkau static bool br_port_group_equal(struct net_bridge_port_group *p, 8266db6f0eaSFelix Fietkau struct net_bridge_port *port, 8276db6f0eaSFelix Fietkau const unsigned char *src) 8286db6f0eaSFelix Fietkau { 8296db6f0eaSFelix Fietkau if (p->port != port) 8306db6f0eaSFelix Fietkau return false; 8316db6f0eaSFelix Fietkau 8326db6f0eaSFelix Fietkau if (!(port->flags & BR_MULTICAST_TO_UNICAST)) 8336db6f0eaSFelix Fietkau return true; 8346db6f0eaSFelix Fietkau 8356db6f0eaSFelix Fietkau return ether_addr_equal(src, p->eth_addr); 8366db6f0eaSFelix Fietkau } 8376db6f0eaSFelix Fietkau 8381bc844eeSNikolay Aleksandrov void br_multicast_host_join(struct net_bridge_mdb_entry *mp, bool notify) 8391bc844eeSNikolay Aleksandrov { 8401bc844eeSNikolay Aleksandrov if (!mp->host_joined) { 8411bc844eeSNikolay Aleksandrov mp->host_joined = true; 8421bc844eeSNikolay Aleksandrov if (notify) 84381f19838SNikolay Aleksandrov br_mdb_notify(mp->br->dev, mp, NULL, RTM_NEWMDB); 8441bc844eeSNikolay Aleksandrov } 8451bc844eeSNikolay Aleksandrov mod_timer(&mp->timer, jiffies + mp->br->multicast_membership_interval); 8461bc844eeSNikolay Aleksandrov } 8471bc844eeSNikolay Aleksandrov 8481bc844eeSNikolay Aleksandrov void br_multicast_host_leave(struct net_bridge_mdb_entry *mp, bool notify) 8491bc844eeSNikolay Aleksandrov { 8501bc844eeSNikolay Aleksandrov if (!mp->host_joined) 8511bc844eeSNikolay Aleksandrov return; 8521bc844eeSNikolay Aleksandrov 8531bc844eeSNikolay Aleksandrov mp->host_joined = false; 8541bc844eeSNikolay Aleksandrov if (notify) 85581f19838SNikolay Aleksandrov br_mdb_notify(mp->br->dev, mp, NULL, RTM_DELMDB); 8561bc844eeSNikolay Aleksandrov } 8571bc844eeSNikolay Aleksandrov 858eb1d1641SHerbert Xu static int br_multicast_add_group(struct net_bridge *br, 8598ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 8606db6f0eaSFelix Fietkau struct br_ip *group, 8618b671779SNikolay Aleksandrov const unsigned char *src, 8620436862eSNikolay Aleksandrov u8 filter_mode, 8630436862eSNikolay Aleksandrov bool igmpv2_mldv1) 864eb1d1641SHerbert Xu { 865e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 8665e923585SNikolay Aleksandrov struct net_bridge_port_group *p; 8675e923585SNikolay Aleksandrov struct net_bridge_mdb_entry *mp; 868454594f3SLinus Lüssing unsigned long now = jiffies; 869eb1d1641SHerbert Xu int err; 870eb1d1641SHerbert Xu 871eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 872eb1d1641SHerbert Xu if (!netif_running(br->dev) || 873eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 874eb1d1641SHerbert Xu goto out; 875eb1d1641SHerbert Xu 87619e3a9c9SNikolay Aleksandrov mp = br_multicast_new_group(br, group); 877eb1d1641SHerbert Xu err = PTR_ERR(mp); 8784c0833bcSTobias Klauser if (IS_ERR(mp)) 879eb1d1641SHerbert Xu goto err; 880eb1d1641SHerbert Xu 881eb1d1641SHerbert Xu if (!port) { 8821bc844eeSNikolay Aleksandrov br_multicast_host_join(mp, true); 883eb1d1641SHerbert Xu goto out; 884eb1d1641SHerbert Xu } 885eb1d1641SHerbert Xu 886e8051688SEric Dumazet for (pp = &mp->ports; 887e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 888e8051688SEric Dumazet pp = &p->next) { 8896db6f0eaSFelix Fietkau if (br_port_group_equal(p, port, src)) 890454594f3SLinus Lüssing goto found; 891eb1d1641SHerbert Xu if ((unsigned long)p->port < (unsigned long)port) 892eb1d1641SHerbert Xu break; 893eb1d1641SHerbert Xu } 894eb1d1641SHerbert Xu 8958b671779SNikolay Aleksandrov p = br_multicast_new_port_group(port, group, *pp, 0, src, filter_mode); 896eb1d1641SHerbert Xu if (unlikely(!p)) 897eb1d1641SHerbert Xu goto err; 898eb1d1641SHerbert Xu rcu_assign_pointer(*pp, p); 89981f19838SNikolay Aleksandrov br_mdb_notify(br->dev, mp, p, RTM_NEWMDB); 900eb1d1641SHerbert Xu 901454594f3SLinus Lüssing found: 9020436862eSNikolay Aleksandrov if (igmpv2_mldv1) 903454594f3SLinus Lüssing mod_timer(&p->timer, now + br->multicast_membership_interval); 90481f19838SNikolay Aleksandrov 905eb1d1641SHerbert Xu out: 906eb1d1641SHerbert Xu err = 0; 907eb1d1641SHerbert Xu 908eb1d1641SHerbert Xu err: 909eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 910eb1d1641SHerbert Xu return err; 911eb1d1641SHerbert Xu } 912eb1d1641SHerbert Xu 9138ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_add_group(struct net_bridge *br, 9148ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 915b0e9a30dSVlad Yasevich __be32 group, 9166db6f0eaSFelix Fietkau __u16 vid, 9178b671779SNikolay Aleksandrov const unsigned char *src, 9188b671779SNikolay Aleksandrov bool igmpv2) 9198ef2a9a5SYOSHIFUJI Hideaki { 9208ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 9218b671779SNikolay Aleksandrov u8 filter_mode; 9228ef2a9a5SYOSHIFUJI Hideaki 9238ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 9248ef2a9a5SYOSHIFUJI Hideaki return 0; 9258ef2a9a5SYOSHIFUJI Hideaki 9261515a63fSNikolay Aleksandrov memset(&br_group, 0, sizeof(br_group)); 927eab3227bSNikolay Aleksandrov br_group.dst.ip4 = group; 9288ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 929b0e9a30dSVlad Yasevich br_group.vid = vid; 9308b671779SNikolay Aleksandrov filter_mode = igmpv2 ? MCAST_EXCLUDE : MCAST_INCLUDE; 9318ef2a9a5SYOSHIFUJI Hideaki 9320436862eSNikolay Aleksandrov return br_multicast_add_group(br, port, &br_group, src, filter_mode, 9330436862eSNikolay Aleksandrov igmpv2); 9348ef2a9a5SYOSHIFUJI Hideaki } 9358ef2a9a5SYOSHIFUJI Hideaki 936dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 93708b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_add_group(struct net_bridge *br, 93808b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 939b0e9a30dSVlad Yasevich const struct in6_addr *group, 9406db6f0eaSFelix Fietkau __u16 vid, 9418b671779SNikolay Aleksandrov const unsigned char *src, 9428b671779SNikolay Aleksandrov bool mldv1) 94308b202b6SYOSHIFUJI Hideaki { 94408b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 9458b671779SNikolay Aleksandrov u8 filter_mode; 94608b202b6SYOSHIFUJI Hideaki 9473c3769e6SLinus Lüssing if (ipv6_addr_is_ll_all_nodes(group)) 94808b202b6SYOSHIFUJI Hideaki return 0; 94908b202b6SYOSHIFUJI Hideaki 95019e3a9c9SNikolay Aleksandrov memset(&br_group, 0, sizeof(br_group)); 951eab3227bSNikolay Aleksandrov br_group.dst.ip6 = *group; 9529cc6e0c4SLinus Lüssing br_group.proto = htons(ETH_P_IPV6); 953b0e9a30dSVlad Yasevich br_group.vid = vid; 9548b671779SNikolay Aleksandrov filter_mode = mldv1 ? MCAST_EXCLUDE : MCAST_INCLUDE; 95508b202b6SYOSHIFUJI Hideaki 9560436862eSNikolay Aleksandrov return br_multicast_add_group(br, port, &br_group, src, filter_mode, 9570436862eSNikolay Aleksandrov mldv1); 95808b202b6SYOSHIFUJI Hideaki } 95908b202b6SYOSHIFUJI Hideaki #endif 96008b202b6SYOSHIFUJI Hideaki 96188c1f37fSAllen Pais static void br_multicast_router_expired(struct timer_list *t) 962eb1d1641SHerbert Xu { 96388c1f37fSAllen Pais struct net_bridge_port *port = 96488c1f37fSAllen Pais from_timer(port, t, multicast_router_timer); 965eb1d1641SHerbert Xu struct net_bridge *br = port->br; 966eb1d1641SHerbert Xu 967eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 968a55d8246SNikolay Aleksandrov if (port->multicast_router == MDB_RTR_TYPE_DISABLED || 969a55d8246SNikolay Aleksandrov port->multicast_router == MDB_RTR_TYPE_PERM || 970f12e7d95SNogah Frankel timer_pending(&port->multicast_router_timer)) 971eb1d1641SHerbert Xu goto out; 972eb1d1641SHerbert Xu 973f12e7d95SNogah Frankel __del_port_router(port); 974eb1d1641SHerbert Xu out: 975eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 976eb1d1641SHerbert Xu } 977eb1d1641SHerbert Xu 97877041420SYotam Gigi static void br_mc_router_state_change(struct net_bridge *p, 97977041420SYotam Gigi bool is_mc_router) 98077041420SYotam Gigi { 98177041420SYotam Gigi struct switchdev_attr attr = { 98277041420SYotam Gigi .orig_dev = p->dev, 98377041420SYotam Gigi .id = SWITCHDEV_ATTR_ID_BRIDGE_MROUTER, 98477041420SYotam Gigi .flags = SWITCHDEV_F_DEFER, 98577041420SYotam Gigi .u.mrouter = is_mc_router, 98677041420SYotam Gigi }; 98777041420SYotam Gigi 98877041420SYotam Gigi switchdev_port_attr_set(p->dev, &attr); 98977041420SYotam Gigi } 99077041420SYotam Gigi 99188c1f37fSAllen Pais static void br_multicast_local_router_expired(struct timer_list *t) 992eb1d1641SHerbert Xu { 99388c1f37fSAllen Pais struct net_bridge *br = from_timer(br, t, multicast_router_timer); 99477041420SYotam Gigi 99577041420SYotam Gigi spin_lock(&br->multicast_lock); 99677041420SYotam Gigi if (br->multicast_router == MDB_RTR_TYPE_DISABLED || 99777041420SYotam Gigi br->multicast_router == MDB_RTR_TYPE_PERM || 99877041420SYotam Gigi timer_pending(&br->multicast_router_timer)) 99977041420SYotam Gigi goto out; 100077041420SYotam Gigi 100177041420SYotam Gigi br_mc_router_state_change(br, false); 100277041420SYotam Gigi out: 100377041420SYotam Gigi spin_unlock(&br->multicast_lock); 1004eb1d1641SHerbert Xu } 1005eb1d1641SHerbert Xu 1006cc0fdd80SLinus Lüssing static void br_multicast_querier_expired(struct net_bridge *br, 100790010b36SLinus Lüssing struct bridge_mcast_own_query *query) 1008c83b8fabSHerbert Xu { 1009c83b8fabSHerbert Xu spin_lock(&br->multicast_lock); 101013cefad2SNikolay Aleksandrov if (!netif_running(br->dev) || !br_opt_get(br, BROPT_MULTICAST_ENABLED)) 1011c83b8fabSHerbert Xu goto out; 1012c83b8fabSHerbert Xu 1013cc0fdd80SLinus Lüssing br_multicast_start_querier(br, query); 1014c83b8fabSHerbert Xu 1015c83b8fabSHerbert Xu out: 1016c83b8fabSHerbert Xu spin_unlock(&br->multicast_lock); 1017c83b8fabSHerbert Xu } 1018c83b8fabSHerbert Xu 101988c1f37fSAllen Pais static void br_ip4_multicast_querier_expired(struct timer_list *t) 1020cc0fdd80SLinus Lüssing { 102188c1f37fSAllen Pais struct net_bridge *br = from_timer(br, t, ip4_other_query.timer); 1022cc0fdd80SLinus Lüssing 102390010b36SLinus Lüssing br_multicast_querier_expired(br, &br->ip4_own_query); 1024cc0fdd80SLinus Lüssing } 1025cc0fdd80SLinus Lüssing 1026cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 102788c1f37fSAllen Pais static void br_ip6_multicast_querier_expired(struct timer_list *t) 1028cc0fdd80SLinus Lüssing { 102988c1f37fSAllen Pais struct net_bridge *br = from_timer(br, t, ip6_other_query.timer); 1030cc0fdd80SLinus Lüssing 103190010b36SLinus Lüssing br_multicast_querier_expired(br, &br->ip6_own_query); 1032cc0fdd80SLinus Lüssing } 1033cc0fdd80SLinus Lüssing #endif 1034cc0fdd80SLinus Lüssing 1035dc4eb53aSLinus Lüssing static void br_multicast_select_own_querier(struct net_bridge *br, 1036dc4eb53aSLinus Lüssing struct br_ip *ip, 1037dc4eb53aSLinus Lüssing struct sk_buff *skb) 1038dc4eb53aSLinus Lüssing { 1039dc4eb53aSLinus Lüssing if (ip->proto == htons(ETH_P_IP)) 1040deb96566SNikolay Aleksandrov br->ip4_querier.addr.src.ip4 = ip_hdr(skb)->saddr; 1041dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1042dc4eb53aSLinus Lüssing else 1043deb96566SNikolay Aleksandrov br->ip6_querier.addr.src.ip6 = ipv6_hdr(skb)->saddr; 1044dc4eb53aSLinus Lüssing #endif 1045dc4eb53aSLinus Lüssing } 1046dc4eb53aSLinus Lüssing 10478ef2a9a5SYOSHIFUJI Hideaki static void __br_multicast_send_query(struct net_bridge *br, 10488ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 1049438ef2d0SNikolay Aleksandrov struct net_bridge_port_group *pg, 1050438ef2d0SNikolay Aleksandrov struct br_ip *ip_dst, 1051438ef2d0SNikolay Aleksandrov struct br_ip *group, 1052438ef2d0SNikolay Aleksandrov bool with_srcs, 105342c11ccfSNikolay Aleksandrov u8 sflag, 105442c11ccfSNikolay Aleksandrov bool *need_rexmit) 1055eb1d1641SHerbert Xu { 1056438ef2d0SNikolay Aleksandrov bool over_lmqt = !!sflag; 1057eb1d1641SHerbert Xu struct sk_buff *skb; 10581080ab95SNikolay Aleksandrov u8 igmp_type; 1059eb1d1641SHerbert Xu 1060438ef2d0SNikolay Aleksandrov again_under_lmqt: 1061438ef2d0SNikolay Aleksandrov skb = br_multicast_alloc_query(br, pg, ip_dst, group, with_srcs, 106242c11ccfSNikolay Aleksandrov over_lmqt, sflag, &igmp_type, 106342c11ccfSNikolay Aleksandrov need_rexmit); 1064eb1d1641SHerbert Xu if (!skb) 10658ef2a9a5SYOSHIFUJI Hideaki return; 1066eb1d1641SHerbert Xu 1067eb1d1641SHerbert Xu if (port) { 1068eb1d1641SHerbert Xu skb->dev = port->dev; 1069a65056ecSNikolay Aleksandrov br_multicast_count(br, port, skb, igmp_type, 10701080ab95SNikolay Aleksandrov BR_MCAST_DIR_TX); 107129a26a56SEric W. Biederman NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, 107229a26a56SEric W. Biederman dev_net(port->dev), NULL, skb, NULL, skb->dev, 1073f0b4eeceSLinus Lüssing br_dev_queue_push_xmit); 1074438ef2d0SNikolay Aleksandrov 1075438ef2d0SNikolay Aleksandrov if (over_lmqt && with_srcs && sflag) { 1076438ef2d0SNikolay Aleksandrov over_lmqt = false; 1077438ef2d0SNikolay Aleksandrov goto again_under_lmqt; 1078438ef2d0SNikolay Aleksandrov } 1079dc4eb53aSLinus Lüssing } else { 1080438ef2d0SNikolay Aleksandrov br_multicast_select_own_querier(br, group, skb); 1081a65056ecSNikolay Aleksandrov br_multicast_count(br, port, skb, igmp_type, 10821080ab95SNikolay Aleksandrov BR_MCAST_DIR_RX); 1083eb1d1641SHerbert Xu netif_rx(skb); 10848ef2a9a5SYOSHIFUJI Hideaki } 1085dc4eb53aSLinus Lüssing } 1086eb1d1641SHerbert Xu 10878ef2a9a5SYOSHIFUJI Hideaki static void br_multicast_send_query(struct net_bridge *br, 1088cc0fdd80SLinus Lüssing struct net_bridge_port *port, 108990010b36SLinus Lüssing struct bridge_mcast_own_query *own_query) 10908ef2a9a5SYOSHIFUJI Hideaki { 109190010b36SLinus Lüssing struct bridge_mcast_other_query *other_query = NULL; 10925e923585SNikolay Aleksandrov struct br_ip br_group; 10935e923585SNikolay Aleksandrov unsigned long time; 10948ef2a9a5SYOSHIFUJI Hideaki 109513cefad2SNikolay Aleksandrov if (!netif_running(br->dev) || 109613cefad2SNikolay Aleksandrov !br_opt_get(br, BROPT_MULTICAST_ENABLED) || 1097675779adSNikolay Aleksandrov !br_opt_get(br, BROPT_MULTICAST_QUERIER)) 10988ef2a9a5SYOSHIFUJI Hideaki return; 10998ef2a9a5SYOSHIFUJI Hideaki 1100eab3227bSNikolay Aleksandrov memset(&br_group.dst, 0, sizeof(br_group.dst)); 11018ef2a9a5SYOSHIFUJI Hideaki 110290010b36SLinus Lüssing if (port ? (own_query == &port->ip4_own_query) : 110390010b36SLinus Lüssing (own_query == &br->ip4_own_query)) { 110490010b36SLinus Lüssing other_query = &br->ip4_other_query; 110508b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 1106dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 1107cc0fdd80SLinus Lüssing } else { 110890010b36SLinus Lüssing other_query = &br->ip6_other_query; 110908b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 111008b202b6SYOSHIFUJI Hideaki #endif 1111eb1d1641SHerbert Xu } 1112eb1d1641SHerbert Xu 111390010b36SLinus Lüssing if (!other_query || timer_pending(&other_query->timer)) 1114cc0fdd80SLinus Lüssing return; 1115cc0fdd80SLinus Lüssing 111642c11ccfSNikolay Aleksandrov __br_multicast_send_query(br, port, NULL, NULL, &br_group, false, 0, 111742c11ccfSNikolay Aleksandrov NULL); 1118cc0fdd80SLinus Lüssing 1119cc0fdd80SLinus Lüssing time = jiffies; 112090010b36SLinus Lüssing time += own_query->startup_sent < br->multicast_startup_query_count ? 1121cc0fdd80SLinus Lüssing br->multicast_startup_query_interval : 1122cc0fdd80SLinus Lüssing br->multicast_query_interval; 112390010b36SLinus Lüssing mod_timer(&own_query->timer, time); 1124cc0fdd80SLinus Lüssing } 1125cc0fdd80SLinus Lüssing 112690010b36SLinus Lüssing static void 112790010b36SLinus Lüssing br_multicast_port_query_expired(struct net_bridge_port *port, 112890010b36SLinus Lüssing struct bridge_mcast_own_query *query) 1129eb1d1641SHerbert Xu { 1130eb1d1641SHerbert Xu struct net_bridge *br = port->br; 1131eb1d1641SHerbert Xu 1132eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 113302a780c0SDan Carpenter if (port->state == BR_STATE_DISABLED || 113402a780c0SDan Carpenter port->state == BR_STATE_BLOCKING) 1135eb1d1641SHerbert Xu goto out; 1136eb1d1641SHerbert Xu 1137cc0fdd80SLinus Lüssing if (query->startup_sent < br->multicast_startup_query_count) 1138cc0fdd80SLinus Lüssing query->startup_sent++; 1139eb1d1641SHerbert Xu 1140cc0fdd80SLinus Lüssing br_multicast_send_query(port->br, port, query); 1141eb1d1641SHerbert Xu 1142eb1d1641SHerbert Xu out: 1143eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1144eb1d1641SHerbert Xu } 1145eb1d1641SHerbert Xu 114688c1f37fSAllen Pais static void br_ip4_multicast_port_query_expired(struct timer_list *t) 1147cc0fdd80SLinus Lüssing { 114888c1f37fSAllen Pais struct net_bridge_port *port = from_timer(port, t, ip4_own_query.timer); 1149cc0fdd80SLinus Lüssing 115090010b36SLinus Lüssing br_multicast_port_query_expired(port, &port->ip4_own_query); 1151cc0fdd80SLinus Lüssing } 1152cc0fdd80SLinus Lüssing 1153cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 115488c1f37fSAllen Pais static void br_ip6_multicast_port_query_expired(struct timer_list *t) 1155cc0fdd80SLinus Lüssing { 115688c1f37fSAllen Pais struct net_bridge_port *port = from_timer(port, t, ip6_own_query.timer); 1157cc0fdd80SLinus Lüssing 115890010b36SLinus Lüssing br_multicast_port_query_expired(port, &port->ip6_own_query); 1159cc0fdd80SLinus Lüssing } 1160cc0fdd80SLinus Lüssing #endif 1161cc0fdd80SLinus Lüssing 116242c11ccfSNikolay Aleksandrov static void br_multicast_port_group_rexmit(struct timer_list *t) 116342c11ccfSNikolay Aleksandrov { 116442c11ccfSNikolay Aleksandrov struct net_bridge_port_group *pg = from_timer(pg, t, rexmit_timer); 116542c11ccfSNikolay Aleksandrov struct bridge_mcast_other_query *other_query = NULL; 116642c11ccfSNikolay Aleksandrov struct net_bridge *br = pg->port->br; 116742c11ccfSNikolay Aleksandrov bool need_rexmit = false; 116842c11ccfSNikolay Aleksandrov 116942c11ccfSNikolay Aleksandrov spin_lock(&br->multicast_lock); 117042c11ccfSNikolay Aleksandrov if (!netif_running(br->dev) || hlist_unhashed(&pg->mglist) || 117142c11ccfSNikolay Aleksandrov !br_opt_get(br, BROPT_MULTICAST_ENABLED) || 117242c11ccfSNikolay Aleksandrov !br_opt_get(br, BROPT_MULTICAST_QUERIER)) 117342c11ccfSNikolay Aleksandrov goto out; 117442c11ccfSNikolay Aleksandrov 117542c11ccfSNikolay Aleksandrov if (pg->addr.proto == htons(ETH_P_IP)) 117642c11ccfSNikolay Aleksandrov other_query = &br->ip4_other_query; 117742c11ccfSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 117842c11ccfSNikolay Aleksandrov else 117942c11ccfSNikolay Aleksandrov other_query = &br->ip6_other_query; 118042c11ccfSNikolay Aleksandrov #endif 118142c11ccfSNikolay Aleksandrov 118242c11ccfSNikolay Aleksandrov if (!other_query || timer_pending(&other_query->timer)) 118342c11ccfSNikolay Aleksandrov goto out; 118442c11ccfSNikolay Aleksandrov 118542c11ccfSNikolay Aleksandrov if (pg->grp_query_rexmit_cnt) { 118642c11ccfSNikolay Aleksandrov pg->grp_query_rexmit_cnt--; 118742c11ccfSNikolay Aleksandrov __br_multicast_send_query(br, pg->port, pg, &pg->addr, 118842c11ccfSNikolay Aleksandrov &pg->addr, false, 1, NULL); 118942c11ccfSNikolay Aleksandrov } 119042c11ccfSNikolay Aleksandrov __br_multicast_send_query(br, pg->port, pg, &pg->addr, 119142c11ccfSNikolay Aleksandrov &pg->addr, true, 0, &need_rexmit); 119242c11ccfSNikolay Aleksandrov 119342c11ccfSNikolay Aleksandrov if (pg->grp_query_rexmit_cnt || need_rexmit) 119442c11ccfSNikolay Aleksandrov mod_timer(&pg->rexmit_timer, jiffies + 119542c11ccfSNikolay Aleksandrov br->multicast_last_member_interval); 119642c11ccfSNikolay Aleksandrov out: 119742c11ccfSNikolay Aleksandrov spin_unlock(&br->multicast_lock); 119842c11ccfSNikolay Aleksandrov } 119942c11ccfSNikolay Aleksandrov 1200147c1e9bSNogah Frankel static void br_mc_disabled_update(struct net_device *dev, bool value) 1201147c1e9bSNogah Frankel { 1202147c1e9bSNogah Frankel struct switchdev_attr attr = { 1203147c1e9bSNogah Frankel .orig_dev = dev, 1204147c1e9bSNogah Frankel .id = SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED, 1205147c1e9bSNogah Frankel .flags = SWITCHDEV_F_DEFER, 120613cefad2SNikolay Aleksandrov .u.mc_disabled = !value, 1207147c1e9bSNogah Frankel }; 1208147c1e9bSNogah Frankel 1209147c1e9bSNogah Frankel switchdev_port_attr_set(dev, &attr); 1210147c1e9bSNogah Frankel } 1211147c1e9bSNogah Frankel 12121080ab95SNikolay Aleksandrov int br_multicast_add_port(struct net_bridge_port *port) 1213eb1d1641SHerbert Xu { 12147f0aec7aSNikolay Aleksandrov port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 1215eb1d1641SHerbert Xu 121688c1f37fSAllen Pais timer_setup(&port->multicast_router_timer, 121788c1f37fSAllen Pais br_multicast_router_expired, 0); 121888c1f37fSAllen Pais timer_setup(&port->ip4_own_query.timer, 121988c1f37fSAllen Pais br_ip4_multicast_port_query_expired, 0); 1220cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 122188c1f37fSAllen Pais timer_setup(&port->ip6_own_query.timer, 122288c1f37fSAllen Pais br_ip6_multicast_port_query_expired, 0); 1223cc0fdd80SLinus Lüssing #endif 122413cefad2SNikolay Aleksandrov br_mc_disabled_update(port->dev, 122513cefad2SNikolay Aleksandrov br_opt_get(port->br, BROPT_MULTICAST_ENABLED)); 1226147c1e9bSNogah Frankel 12271080ab95SNikolay Aleksandrov port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats); 12281080ab95SNikolay Aleksandrov if (!port->mcast_stats) 12291080ab95SNikolay Aleksandrov return -ENOMEM; 12301080ab95SNikolay Aleksandrov 12311080ab95SNikolay Aleksandrov return 0; 1232eb1d1641SHerbert Xu } 1233eb1d1641SHerbert Xu 1234eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port) 1235eb1d1641SHerbert Xu { 1236e10177abSSatish Ashok struct net_bridge *br = port->br; 1237e10177abSSatish Ashok struct net_bridge_port_group *pg; 1238e12cec65SNikolay Aleksandrov HLIST_HEAD(deleted_head); 1239e10177abSSatish Ashok struct hlist_node *n; 1240e10177abSSatish Ashok 1241e10177abSSatish Ashok /* Take care of the remaining groups, only perm ones should be left */ 1242e10177abSSatish Ashok spin_lock_bh(&br->multicast_lock); 1243e10177abSSatish Ashok hlist_for_each_entry_safe(pg, n, &port->mglist, mglist) 1244681590bdSNikolay Aleksandrov br_multicast_find_del_pg(br, pg); 1245e12cec65SNikolay Aleksandrov hlist_move_list(&br->mcast_gc_list, &deleted_head); 1246e10177abSSatish Ashok spin_unlock_bh(&br->multicast_lock); 1247e12cec65SNikolay Aleksandrov br_multicast_gc(&deleted_head); 1248eb1d1641SHerbert Xu del_timer_sync(&port->multicast_router_timer); 12491080ab95SNikolay Aleksandrov free_percpu(port->mcast_stats); 1250eb1d1641SHerbert Xu } 1251eb1d1641SHerbert Xu 125290010b36SLinus Lüssing static void br_multicast_enable(struct bridge_mcast_own_query *query) 1253561f1103SHerbert Xu { 1254cc0fdd80SLinus Lüssing query->startup_sent = 0; 1255561f1103SHerbert Xu 1256cc0fdd80SLinus Lüssing if (try_to_del_timer_sync(&query->timer) >= 0 || 1257cc0fdd80SLinus Lüssing del_timer(&query->timer)) 1258cc0fdd80SLinus Lüssing mod_timer(&query->timer, jiffies); 1259561f1103SHerbert Xu } 1260561f1103SHerbert Xu 12617cb3f921SNikolay Aleksandrov static void __br_multicast_enable_port(struct net_bridge_port *port) 1262eb1d1641SHerbert Xu { 1263eb1d1641SHerbert Xu struct net_bridge *br = port->br; 1264eb1d1641SHerbert Xu 126513cefad2SNikolay Aleksandrov if (!br_opt_get(br, BROPT_MULTICAST_ENABLED) || !netif_running(br->dev)) 12667cb3f921SNikolay Aleksandrov return; 1267eb1d1641SHerbert Xu 126890010b36SLinus Lüssing br_multicast_enable(&port->ip4_own_query); 1269cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 127090010b36SLinus Lüssing br_multicast_enable(&port->ip6_own_query); 1271cc0fdd80SLinus Lüssing #endif 12727f0aec7aSNikolay Aleksandrov if (port->multicast_router == MDB_RTR_TYPE_PERM && 12737f0aec7aSNikolay Aleksandrov hlist_unhashed(&port->rlist)) 1274754bc547SSatish Ashok br_multicast_add_router(br, port); 12757cb3f921SNikolay Aleksandrov } 1276eb1d1641SHerbert Xu 12777cb3f921SNikolay Aleksandrov void br_multicast_enable_port(struct net_bridge_port *port) 12787cb3f921SNikolay Aleksandrov { 12797cb3f921SNikolay Aleksandrov struct net_bridge *br = port->br; 12807cb3f921SNikolay Aleksandrov 12817cb3f921SNikolay Aleksandrov spin_lock(&br->multicast_lock); 12827cb3f921SNikolay Aleksandrov __br_multicast_enable_port(port); 1283eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1284eb1d1641SHerbert Xu } 1285eb1d1641SHerbert Xu 1286eb1d1641SHerbert Xu void br_multicast_disable_port(struct net_bridge_port *port) 1287eb1d1641SHerbert Xu { 1288eb1d1641SHerbert Xu struct net_bridge *br = port->br; 1289eb1d1641SHerbert Xu struct net_bridge_port_group *pg; 1290b67bfe0dSSasha Levin struct hlist_node *n; 1291eb1d1641SHerbert Xu 1292eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1293b67bfe0dSSasha Levin hlist_for_each_entry_safe(pg, n, &port->mglist, mglist) 12949d06b6d8SElad Raz if (!(pg->flags & MDB_PG_FLAGS_PERMANENT)) 1295681590bdSNikolay Aleksandrov br_multicast_find_del_pg(br, pg); 1296eb1d1641SHerbert Xu 1297f12e7d95SNogah Frankel __del_port_router(port); 1298f12e7d95SNogah Frankel 1299eb1d1641SHerbert Xu del_timer(&port->multicast_router_timer); 130090010b36SLinus Lüssing del_timer(&port->ip4_own_query.timer); 1301cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 130290010b36SLinus Lüssing del_timer(&port->ip6_own_query.timer); 1303cc0fdd80SLinus Lüssing #endif 1304eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1305eb1d1641SHerbert Xu } 1306eb1d1641SHerbert Xu 1307e6231bcaSNikolay Aleksandrov static int __grp_src_delete_marked(struct net_bridge_port_group *pg) 1308e6231bcaSNikolay Aleksandrov { 1309e6231bcaSNikolay Aleksandrov struct net_bridge_group_src *ent; 1310e6231bcaSNikolay Aleksandrov struct hlist_node *tmp; 1311e6231bcaSNikolay Aleksandrov int deleted = 0; 1312e6231bcaSNikolay Aleksandrov 1313e6231bcaSNikolay Aleksandrov hlist_for_each_entry_safe(ent, tmp, &pg->src_list, node) 1314e6231bcaSNikolay Aleksandrov if (ent->flags & BR_SGRP_F_DELETE) { 1315e6231bcaSNikolay Aleksandrov br_multicast_del_group_src(ent); 1316e6231bcaSNikolay Aleksandrov deleted++; 1317e6231bcaSNikolay Aleksandrov } 1318e6231bcaSNikolay Aleksandrov 1319e6231bcaSNikolay Aleksandrov return deleted; 1320e6231bcaSNikolay Aleksandrov } 1321e6231bcaSNikolay Aleksandrov 13225bf1e00bSNikolay Aleksandrov static void __grp_src_query_marked_and_rexmit(struct net_bridge_port_group *pg) 13235bf1e00bSNikolay Aleksandrov { 13245bf1e00bSNikolay Aleksandrov struct bridge_mcast_other_query *other_query = NULL; 13255bf1e00bSNikolay Aleksandrov struct net_bridge *br = pg->port->br; 13265bf1e00bSNikolay Aleksandrov u32 lmqc = br->multicast_last_member_count; 13275bf1e00bSNikolay Aleksandrov unsigned long lmqt, lmi, now = jiffies; 13285bf1e00bSNikolay Aleksandrov struct net_bridge_group_src *ent; 13295bf1e00bSNikolay Aleksandrov 13305bf1e00bSNikolay Aleksandrov if (!netif_running(br->dev) || 13315bf1e00bSNikolay Aleksandrov !br_opt_get(br, BROPT_MULTICAST_ENABLED)) 13325bf1e00bSNikolay Aleksandrov return; 13335bf1e00bSNikolay Aleksandrov 13345bf1e00bSNikolay Aleksandrov if (pg->addr.proto == htons(ETH_P_IP)) 13355bf1e00bSNikolay Aleksandrov other_query = &br->ip4_other_query; 13365bf1e00bSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 13375bf1e00bSNikolay Aleksandrov else 13385bf1e00bSNikolay Aleksandrov other_query = &br->ip6_other_query; 13395bf1e00bSNikolay Aleksandrov #endif 13405bf1e00bSNikolay Aleksandrov 13415bf1e00bSNikolay Aleksandrov lmqt = now + br_multicast_lmqt(br); 13425bf1e00bSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) { 13435bf1e00bSNikolay Aleksandrov if (ent->flags & BR_SGRP_F_SEND) { 13445bf1e00bSNikolay Aleksandrov ent->flags &= ~BR_SGRP_F_SEND; 13455bf1e00bSNikolay Aleksandrov if (ent->timer.expires > lmqt) { 13465bf1e00bSNikolay Aleksandrov if (br_opt_get(br, BROPT_MULTICAST_QUERIER) && 13475bf1e00bSNikolay Aleksandrov other_query && 13485bf1e00bSNikolay Aleksandrov !timer_pending(&other_query->timer)) 13495bf1e00bSNikolay Aleksandrov ent->src_query_rexmit_cnt = lmqc; 13505bf1e00bSNikolay Aleksandrov mod_timer(&ent->timer, lmqt); 13515bf1e00bSNikolay Aleksandrov } 13525bf1e00bSNikolay Aleksandrov } 13535bf1e00bSNikolay Aleksandrov } 13545bf1e00bSNikolay Aleksandrov 13555bf1e00bSNikolay Aleksandrov if (!br_opt_get(br, BROPT_MULTICAST_QUERIER) || 13565bf1e00bSNikolay Aleksandrov !other_query || timer_pending(&other_query->timer)) 13575bf1e00bSNikolay Aleksandrov return; 13585bf1e00bSNikolay Aleksandrov 13595bf1e00bSNikolay Aleksandrov __br_multicast_send_query(br, pg->port, pg, &pg->addr, 13605bf1e00bSNikolay Aleksandrov &pg->addr, true, 1, NULL); 13615bf1e00bSNikolay Aleksandrov 13625bf1e00bSNikolay Aleksandrov lmi = now + br->multicast_last_member_interval; 13635bf1e00bSNikolay Aleksandrov if (!timer_pending(&pg->rexmit_timer) || 13645bf1e00bSNikolay Aleksandrov time_after(pg->rexmit_timer.expires, lmi)) 13655bf1e00bSNikolay Aleksandrov mod_timer(&pg->rexmit_timer, lmi); 13665bf1e00bSNikolay Aleksandrov } 13675bf1e00bSNikolay Aleksandrov 13685bf1e00bSNikolay Aleksandrov static void __grp_send_query_and_rexmit(struct net_bridge_port_group *pg) 13695bf1e00bSNikolay Aleksandrov { 13705bf1e00bSNikolay Aleksandrov struct bridge_mcast_other_query *other_query = NULL; 13715bf1e00bSNikolay Aleksandrov struct net_bridge *br = pg->port->br; 13725bf1e00bSNikolay Aleksandrov unsigned long now = jiffies, lmi; 13735bf1e00bSNikolay Aleksandrov 13745bf1e00bSNikolay Aleksandrov if (!netif_running(br->dev) || 13755bf1e00bSNikolay Aleksandrov !br_opt_get(br, BROPT_MULTICAST_ENABLED)) 13765bf1e00bSNikolay Aleksandrov return; 13775bf1e00bSNikolay Aleksandrov 13785bf1e00bSNikolay Aleksandrov if (pg->addr.proto == htons(ETH_P_IP)) 13795bf1e00bSNikolay Aleksandrov other_query = &br->ip4_other_query; 13805bf1e00bSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 13815bf1e00bSNikolay Aleksandrov else 13825bf1e00bSNikolay Aleksandrov other_query = &br->ip6_other_query; 13835bf1e00bSNikolay Aleksandrov #endif 13845bf1e00bSNikolay Aleksandrov 13855bf1e00bSNikolay Aleksandrov if (br_opt_get(br, BROPT_MULTICAST_QUERIER) && 13865bf1e00bSNikolay Aleksandrov other_query && !timer_pending(&other_query->timer)) { 13875bf1e00bSNikolay Aleksandrov lmi = now + br->multicast_last_member_interval; 13885bf1e00bSNikolay Aleksandrov pg->grp_query_rexmit_cnt = br->multicast_last_member_count - 1; 13895bf1e00bSNikolay Aleksandrov __br_multicast_send_query(br, pg->port, pg, &pg->addr, 13905bf1e00bSNikolay Aleksandrov &pg->addr, false, 0, NULL); 13915bf1e00bSNikolay Aleksandrov if (!timer_pending(&pg->rexmit_timer) || 13925bf1e00bSNikolay Aleksandrov time_after(pg->rexmit_timer.expires, lmi)) 13935bf1e00bSNikolay Aleksandrov mod_timer(&pg->rexmit_timer, lmi); 13945bf1e00bSNikolay Aleksandrov } 13955bf1e00bSNikolay Aleksandrov 13965bf1e00bSNikolay Aleksandrov if (pg->filter_mode == MCAST_EXCLUDE && 13975bf1e00bSNikolay Aleksandrov (!timer_pending(&pg->timer) || 13985bf1e00bSNikolay Aleksandrov time_after(pg->timer.expires, now + br_multicast_lmqt(br)))) 13995bf1e00bSNikolay Aleksandrov mod_timer(&pg->timer, now + br_multicast_lmqt(br)); 14005bf1e00bSNikolay Aleksandrov } 14015bf1e00bSNikolay Aleksandrov 14020436862eSNikolay Aleksandrov /* State Msg type New state Actions 14030436862eSNikolay Aleksandrov * INCLUDE (A) IS_IN (B) INCLUDE (A+B) (B)=GMI 14040436862eSNikolay Aleksandrov * INCLUDE (A) ALLOW (B) INCLUDE (A+B) (B)=GMI 14050436862eSNikolay Aleksandrov * EXCLUDE (X,Y) ALLOW (A) EXCLUDE (X+A,Y-A) (A)=GMI 14060436862eSNikolay Aleksandrov */ 14070436862eSNikolay Aleksandrov static bool br_multicast_isinc_allow(struct net_bridge_port_group *pg, 14080436862eSNikolay Aleksandrov void *srcs, u32 nsrcs, size_t src_size) 14090436862eSNikolay Aleksandrov { 14100436862eSNikolay Aleksandrov struct net_bridge *br = pg->port->br; 14110436862eSNikolay Aleksandrov struct net_bridge_group_src *ent; 14120436862eSNikolay Aleksandrov unsigned long now = jiffies; 14130436862eSNikolay Aleksandrov bool changed = false; 14140436862eSNikolay Aleksandrov struct br_ip src_ip; 14150436862eSNikolay Aleksandrov u32 src_idx; 14160436862eSNikolay Aleksandrov 14170436862eSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 14180436862eSNikolay Aleksandrov src_ip.proto = pg->addr.proto; 14190436862eSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 1420deb96566SNikolay Aleksandrov memcpy(&src_ip.src, srcs, src_size); 14210436862eSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 14220436862eSNikolay Aleksandrov if (!ent) { 14230436862eSNikolay Aleksandrov ent = br_multicast_new_group_src(pg, &src_ip); 14240436862eSNikolay Aleksandrov if (ent) 14250436862eSNikolay Aleksandrov changed = true; 14260436862eSNikolay Aleksandrov } 14270436862eSNikolay Aleksandrov 14280436862eSNikolay Aleksandrov if (ent) 14290436862eSNikolay Aleksandrov mod_timer(&ent->timer, now + br_multicast_gmi(br)); 14300436862eSNikolay Aleksandrov srcs += src_size; 14310436862eSNikolay Aleksandrov } 14320436862eSNikolay Aleksandrov 14330436862eSNikolay Aleksandrov return changed; 14340436862eSNikolay Aleksandrov } 14350436862eSNikolay Aleksandrov 1436e6231bcaSNikolay Aleksandrov /* State Msg type New state Actions 1437e6231bcaSNikolay Aleksandrov * INCLUDE (A) IS_EX (B) EXCLUDE (A*B,B-A) (B-A)=0 1438e6231bcaSNikolay Aleksandrov * Delete (A-B) 1439e6231bcaSNikolay Aleksandrov * Group Timer=GMI 1440e6231bcaSNikolay Aleksandrov */ 1441e6231bcaSNikolay Aleksandrov static void __grp_src_isexc_incl(struct net_bridge_port_group *pg, 1442e6231bcaSNikolay Aleksandrov void *srcs, u32 nsrcs, size_t src_size) 1443e6231bcaSNikolay Aleksandrov { 1444e6231bcaSNikolay Aleksandrov struct net_bridge_group_src *ent; 1445e6231bcaSNikolay Aleksandrov struct br_ip src_ip; 1446e6231bcaSNikolay Aleksandrov u32 src_idx; 1447e6231bcaSNikolay Aleksandrov 1448e6231bcaSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 1449e6231bcaSNikolay Aleksandrov ent->flags |= BR_SGRP_F_DELETE; 1450e6231bcaSNikolay Aleksandrov 1451e6231bcaSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 1452e6231bcaSNikolay Aleksandrov src_ip.proto = pg->addr.proto; 1453e6231bcaSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 1454deb96566SNikolay Aleksandrov memcpy(&src_ip.src, srcs, src_size); 1455e6231bcaSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 1456e6231bcaSNikolay Aleksandrov if (ent) 1457e6231bcaSNikolay Aleksandrov ent->flags &= ~BR_SGRP_F_DELETE; 1458e6231bcaSNikolay Aleksandrov else 1459e6231bcaSNikolay Aleksandrov br_multicast_new_group_src(pg, &src_ip); 1460e6231bcaSNikolay Aleksandrov srcs += src_size; 1461e6231bcaSNikolay Aleksandrov } 1462e6231bcaSNikolay Aleksandrov 1463e6231bcaSNikolay Aleksandrov __grp_src_delete_marked(pg); 1464e6231bcaSNikolay Aleksandrov } 1465e6231bcaSNikolay Aleksandrov 1466e6231bcaSNikolay Aleksandrov /* State Msg type New state Actions 1467e6231bcaSNikolay Aleksandrov * EXCLUDE (X,Y) IS_EX (A) EXCLUDE (A-Y,Y*A) (A-X-Y)=GMI 1468e6231bcaSNikolay Aleksandrov * Delete (X-A) 1469e6231bcaSNikolay Aleksandrov * Delete (Y-A) 1470e6231bcaSNikolay Aleksandrov * Group Timer=GMI 1471e6231bcaSNikolay Aleksandrov */ 1472e6231bcaSNikolay Aleksandrov static bool __grp_src_isexc_excl(struct net_bridge_port_group *pg, 1473e6231bcaSNikolay Aleksandrov void *srcs, u32 nsrcs, size_t src_size) 1474e6231bcaSNikolay Aleksandrov { 1475e6231bcaSNikolay Aleksandrov struct net_bridge *br = pg->port->br; 1476e6231bcaSNikolay Aleksandrov struct net_bridge_group_src *ent; 1477e6231bcaSNikolay Aleksandrov unsigned long now = jiffies; 1478e6231bcaSNikolay Aleksandrov bool changed = false; 1479e6231bcaSNikolay Aleksandrov struct br_ip src_ip; 1480e6231bcaSNikolay Aleksandrov u32 src_idx; 1481e6231bcaSNikolay Aleksandrov 1482e6231bcaSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 1483e6231bcaSNikolay Aleksandrov ent->flags |= BR_SGRP_F_DELETE; 1484e6231bcaSNikolay Aleksandrov 1485e6231bcaSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 1486e6231bcaSNikolay Aleksandrov src_ip.proto = pg->addr.proto; 1487e6231bcaSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 1488deb96566SNikolay Aleksandrov memcpy(&src_ip.src, srcs, src_size); 1489e6231bcaSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 1490e6231bcaSNikolay Aleksandrov if (ent) { 1491e6231bcaSNikolay Aleksandrov ent->flags &= ~BR_SGRP_F_DELETE; 1492e6231bcaSNikolay Aleksandrov } else { 1493e6231bcaSNikolay Aleksandrov ent = br_multicast_new_group_src(pg, &src_ip); 1494e6231bcaSNikolay Aleksandrov if (ent) { 1495e6231bcaSNikolay Aleksandrov mod_timer(&ent->timer, 1496e6231bcaSNikolay Aleksandrov now + br_multicast_gmi(br)); 1497e6231bcaSNikolay Aleksandrov changed = true; 1498e6231bcaSNikolay Aleksandrov } 1499e6231bcaSNikolay Aleksandrov } 1500e6231bcaSNikolay Aleksandrov srcs += src_size; 1501e6231bcaSNikolay Aleksandrov } 1502e6231bcaSNikolay Aleksandrov 1503e6231bcaSNikolay Aleksandrov if (__grp_src_delete_marked(pg)) 1504e6231bcaSNikolay Aleksandrov changed = true; 1505e6231bcaSNikolay Aleksandrov 1506e6231bcaSNikolay Aleksandrov return changed; 1507e6231bcaSNikolay Aleksandrov } 1508e6231bcaSNikolay Aleksandrov 1509e6231bcaSNikolay Aleksandrov static bool br_multicast_isexc(struct net_bridge_port_group *pg, 1510e6231bcaSNikolay Aleksandrov void *srcs, u32 nsrcs, size_t src_size) 1511e6231bcaSNikolay Aleksandrov { 1512e6231bcaSNikolay Aleksandrov struct net_bridge *br = pg->port->br; 1513e6231bcaSNikolay Aleksandrov bool changed = false; 1514e6231bcaSNikolay Aleksandrov 1515e6231bcaSNikolay Aleksandrov switch (pg->filter_mode) { 1516e6231bcaSNikolay Aleksandrov case MCAST_INCLUDE: 1517e6231bcaSNikolay Aleksandrov __grp_src_isexc_incl(pg, srcs, nsrcs, src_size); 1518e6231bcaSNikolay Aleksandrov changed = true; 1519e6231bcaSNikolay Aleksandrov break; 1520e6231bcaSNikolay Aleksandrov case MCAST_EXCLUDE: 1521e6231bcaSNikolay Aleksandrov changed = __grp_src_isexc_excl(pg, srcs, nsrcs, src_size); 1522e6231bcaSNikolay Aleksandrov break; 1523e6231bcaSNikolay Aleksandrov } 1524e6231bcaSNikolay Aleksandrov 1525e6231bcaSNikolay Aleksandrov pg->filter_mode = MCAST_EXCLUDE; 1526e6231bcaSNikolay Aleksandrov mod_timer(&pg->timer, jiffies + br_multicast_gmi(br)); 1527e6231bcaSNikolay Aleksandrov 1528e6231bcaSNikolay Aleksandrov return changed; 1529e6231bcaSNikolay Aleksandrov } 1530e6231bcaSNikolay Aleksandrov 15315bf1e00bSNikolay Aleksandrov /* State Msg type New state Actions 15325bf1e00bSNikolay Aleksandrov * INCLUDE (A) TO_IN (B) INCLUDE (A+B) (B)=GMI 15335bf1e00bSNikolay Aleksandrov * Send Q(G,A-B) 15345bf1e00bSNikolay Aleksandrov */ 15355bf1e00bSNikolay Aleksandrov static bool __grp_src_toin_incl(struct net_bridge_port_group *pg, 15365bf1e00bSNikolay Aleksandrov void *srcs, u32 nsrcs, size_t src_size) 15375bf1e00bSNikolay Aleksandrov { 15385bf1e00bSNikolay Aleksandrov struct net_bridge *br = pg->port->br; 15395bf1e00bSNikolay Aleksandrov u32 src_idx, to_send = pg->src_ents; 15405bf1e00bSNikolay Aleksandrov struct net_bridge_group_src *ent; 15415bf1e00bSNikolay Aleksandrov unsigned long now = jiffies; 15425bf1e00bSNikolay Aleksandrov bool changed = false; 15435bf1e00bSNikolay Aleksandrov struct br_ip src_ip; 15445bf1e00bSNikolay Aleksandrov 15455bf1e00bSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 15465bf1e00bSNikolay Aleksandrov ent->flags |= BR_SGRP_F_SEND; 15475bf1e00bSNikolay Aleksandrov 15485bf1e00bSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 15495bf1e00bSNikolay Aleksandrov src_ip.proto = pg->addr.proto; 15505bf1e00bSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 1551deb96566SNikolay Aleksandrov memcpy(&src_ip.src, srcs, src_size); 15525bf1e00bSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 15535bf1e00bSNikolay Aleksandrov if (ent) { 15545bf1e00bSNikolay Aleksandrov ent->flags &= ~BR_SGRP_F_SEND; 15555bf1e00bSNikolay Aleksandrov to_send--; 15565bf1e00bSNikolay Aleksandrov } else { 15575bf1e00bSNikolay Aleksandrov ent = br_multicast_new_group_src(pg, &src_ip); 15585bf1e00bSNikolay Aleksandrov if (ent) 15595bf1e00bSNikolay Aleksandrov changed = true; 15605bf1e00bSNikolay Aleksandrov } 15615bf1e00bSNikolay Aleksandrov if (ent) 15625bf1e00bSNikolay Aleksandrov mod_timer(&ent->timer, now + br_multicast_gmi(br)); 15635bf1e00bSNikolay Aleksandrov srcs += src_size; 15645bf1e00bSNikolay Aleksandrov } 15655bf1e00bSNikolay Aleksandrov 15665bf1e00bSNikolay Aleksandrov if (to_send) 15675bf1e00bSNikolay Aleksandrov __grp_src_query_marked_and_rexmit(pg); 15685bf1e00bSNikolay Aleksandrov 15695bf1e00bSNikolay Aleksandrov return changed; 15705bf1e00bSNikolay Aleksandrov } 15715bf1e00bSNikolay Aleksandrov 15725bf1e00bSNikolay Aleksandrov /* State Msg type New state Actions 15735bf1e00bSNikolay Aleksandrov * EXCLUDE (X,Y) TO_IN (A) EXCLUDE (X+A,Y-A) (A)=GMI 15745bf1e00bSNikolay Aleksandrov * Send Q(G,X-A) 15755bf1e00bSNikolay Aleksandrov * Send Q(G) 15765bf1e00bSNikolay Aleksandrov */ 15775bf1e00bSNikolay Aleksandrov static bool __grp_src_toin_excl(struct net_bridge_port_group *pg, 15785bf1e00bSNikolay Aleksandrov void *srcs, u32 nsrcs, size_t src_size) 15795bf1e00bSNikolay Aleksandrov { 15805bf1e00bSNikolay Aleksandrov struct net_bridge *br = pg->port->br; 15815bf1e00bSNikolay Aleksandrov u32 src_idx, to_send = pg->src_ents; 15825bf1e00bSNikolay Aleksandrov struct net_bridge_group_src *ent; 15835bf1e00bSNikolay Aleksandrov unsigned long now = jiffies; 15845bf1e00bSNikolay Aleksandrov bool changed = false; 15855bf1e00bSNikolay Aleksandrov struct br_ip src_ip; 15865bf1e00bSNikolay Aleksandrov 15875bf1e00bSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 15885bf1e00bSNikolay Aleksandrov if (timer_pending(&ent->timer)) 15895bf1e00bSNikolay Aleksandrov ent->flags |= BR_SGRP_F_SEND; 15905bf1e00bSNikolay Aleksandrov 15915bf1e00bSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 15925bf1e00bSNikolay Aleksandrov src_ip.proto = pg->addr.proto; 15935bf1e00bSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 1594deb96566SNikolay Aleksandrov memcpy(&src_ip.src, srcs, src_size); 15955bf1e00bSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 15965bf1e00bSNikolay Aleksandrov if (ent) { 15975bf1e00bSNikolay Aleksandrov if (timer_pending(&ent->timer)) { 15985bf1e00bSNikolay Aleksandrov ent->flags &= ~BR_SGRP_F_SEND; 15995bf1e00bSNikolay Aleksandrov to_send--; 16005bf1e00bSNikolay Aleksandrov } 16015bf1e00bSNikolay Aleksandrov } else { 16025bf1e00bSNikolay Aleksandrov ent = br_multicast_new_group_src(pg, &src_ip); 16035bf1e00bSNikolay Aleksandrov if (ent) 16045bf1e00bSNikolay Aleksandrov changed = true; 16055bf1e00bSNikolay Aleksandrov } 16065bf1e00bSNikolay Aleksandrov if (ent) 16075bf1e00bSNikolay Aleksandrov mod_timer(&ent->timer, now + br_multicast_gmi(br)); 16085bf1e00bSNikolay Aleksandrov srcs += src_size; 16095bf1e00bSNikolay Aleksandrov } 16105bf1e00bSNikolay Aleksandrov 16115bf1e00bSNikolay Aleksandrov if (to_send) 16125bf1e00bSNikolay Aleksandrov __grp_src_query_marked_and_rexmit(pg); 16135bf1e00bSNikolay Aleksandrov 16145bf1e00bSNikolay Aleksandrov __grp_send_query_and_rexmit(pg); 16155bf1e00bSNikolay Aleksandrov 16165bf1e00bSNikolay Aleksandrov return changed; 16175bf1e00bSNikolay Aleksandrov } 16185bf1e00bSNikolay Aleksandrov 16195bf1e00bSNikolay Aleksandrov static bool br_multicast_toin(struct net_bridge_port_group *pg, 16205bf1e00bSNikolay Aleksandrov void *srcs, u32 nsrcs, size_t src_size) 16215bf1e00bSNikolay Aleksandrov { 16225bf1e00bSNikolay Aleksandrov bool changed = false; 16235bf1e00bSNikolay Aleksandrov 16245bf1e00bSNikolay Aleksandrov switch (pg->filter_mode) { 16255bf1e00bSNikolay Aleksandrov case MCAST_INCLUDE: 16265bf1e00bSNikolay Aleksandrov changed = __grp_src_toin_incl(pg, srcs, nsrcs, src_size); 16275bf1e00bSNikolay Aleksandrov break; 16285bf1e00bSNikolay Aleksandrov case MCAST_EXCLUDE: 16295bf1e00bSNikolay Aleksandrov changed = __grp_src_toin_excl(pg, srcs, nsrcs, src_size); 16305bf1e00bSNikolay Aleksandrov break; 16315bf1e00bSNikolay Aleksandrov } 16325bf1e00bSNikolay Aleksandrov 16335bf1e00bSNikolay Aleksandrov return changed; 16345bf1e00bSNikolay Aleksandrov } 16355bf1e00bSNikolay Aleksandrov 16365bf1e00bSNikolay Aleksandrov /* State Msg type New state Actions 16375bf1e00bSNikolay Aleksandrov * INCLUDE (A) TO_EX (B) EXCLUDE (A*B,B-A) (B-A)=0 16385bf1e00bSNikolay Aleksandrov * Delete (A-B) 16395bf1e00bSNikolay Aleksandrov * Send Q(G,A*B) 16405bf1e00bSNikolay Aleksandrov * Group Timer=GMI 16415bf1e00bSNikolay Aleksandrov */ 16425bf1e00bSNikolay Aleksandrov static void __grp_src_toex_incl(struct net_bridge_port_group *pg, 16435bf1e00bSNikolay Aleksandrov void *srcs, u32 nsrcs, size_t src_size) 16445bf1e00bSNikolay Aleksandrov { 16455bf1e00bSNikolay Aleksandrov struct net_bridge_group_src *ent; 16465bf1e00bSNikolay Aleksandrov u32 src_idx, to_send = 0; 16475bf1e00bSNikolay Aleksandrov struct br_ip src_ip; 16485bf1e00bSNikolay Aleksandrov 16495bf1e00bSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 16505bf1e00bSNikolay Aleksandrov ent->flags = (ent->flags & ~BR_SGRP_F_SEND) | BR_SGRP_F_DELETE; 16515bf1e00bSNikolay Aleksandrov 16525bf1e00bSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 16535bf1e00bSNikolay Aleksandrov src_ip.proto = pg->addr.proto; 16545bf1e00bSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 1655deb96566SNikolay Aleksandrov memcpy(&src_ip.src, srcs, src_size); 16565bf1e00bSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 16575bf1e00bSNikolay Aleksandrov if (ent) { 16585bf1e00bSNikolay Aleksandrov ent->flags = (ent->flags & ~BR_SGRP_F_DELETE) | 16595bf1e00bSNikolay Aleksandrov BR_SGRP_F_SEND; 16605bf1e00bSNikolay Aleksandrov to_send++; 16615bf1e00bSNikolay Aleksandrov } else { 16625bf1e00bSNikolay Aleksandrov br_multicast_new_group_src(pg, &src_ip); 16635bf1e00bSNikolay Aleksandrov } 16645bf1e00bSNikolay Aleksandrov srcs += src_size; 16655bf1e00bSNikolay Aleksandrov } 16665bf1e00bSNikolay Aleksandrov 16675bf1e00bSNikolay Aleksandrov __grp_src_delete_marked(pg); 16685bf1e00bSNikolay Aleksandrov if (to_send) 16695bf1e00bSNikolay Aleksandrov __grp_src_query_marked_and_rexmit(pg); 16705bf1e00bSNikolay Aleksandrov } 16715bf1e00bSNikolay Aleksandrov 16725bf1e00bSNikolay Aleksandrov /* State Msg type New state Actions 16735bf1e00bSNikolay Aleksandrov * EXCLUDE (X,Y) TO_EX (A) EXCLUDE (A-Y,Y*A) (A-X-Y)=Group Timer 16745bf1e00bSNikolay Aleksandrov * Delete (X-A) 16755bf1e00bSNikolay Aleksandrov * Delete (Y-A) 16765bf1e00bSNikolay Aleksandrov * Send Q(G,A-Y) 16775bf1e00bSNikolay Aleksandrov * Group Timer=GMI 16785bf1e00bSNikolay Aleksandrov */ 16795bf1e00bSNikolay Aleksandrov static bool __grp_src_toex_excl(struct net_bridge_port_group *pg, 16805bf1e00bSNikolay Aleksandrov void *srcs, u32 nsrcs, size_t src_size) 16815bf1e00bSNikolay Aleksandrov { 16825bf1e00bSNikolay Aleksandrov struct net_bridge_group_src *ent; 16835bf1e00bSNikolay Aleksandrov u32 src_idx, to_send = 0; 16845bf1e00bSNikolay Aleksandrov bool changed = false; 16855bf1e00bSNikolay Aleksandrov struct br_ip src_ip; 16865bf1e00bSNikolay Aleksandrov 16875bf1e00bSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 16885bf1e00bSNikolay Aleksandrov ent->flags = (ent->flags & ~BR_SGRP_F_SEND) | BR_SGRP_F_DELETE; 16895bf1e00bSNikolay Aleksandrov 16905bf1e00bSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 16915bf1e00bSNikolay Aleksandrov src_ip.proto = pg->addr.proto; 16925bf1e00bSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 1693deb96566SNikolay Aleksandrov memcpy(&src_ip.src, srcs, src_size); 16945bf1e00bSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 16955bf1e00bSNikolay Aleksandrov if (ent) { 16965bf1e00bSNikolay Aleksandrov ent->flags &= ~BR_SGRP_F_DELETE; 16975bf1e00bSNikolay Aleksandrov } else { 16985bf1e00bSNikolay Aleksandrov ent = br_multicast_new_group_src(pg, &src_ip); 16995bf1e00bSNikolay Aleksandrov if (ent) { 17005bf1e00bSNikolay Aleksandrov mod_timer(&ent->timer, pg->timer.expires); 17015bf1e00bSNikolay Aleksandrov changed = true; 17025bf1e00bSNikolay Aleksandrov } 17035bf1e00bSNikolay Aleksandrov } 17045bf1e00bSNikolay Aleksandrov if (ent && timer_pending(&ent->timer)) { 17055bf1e00bSNikolay Aleksandrov ent->flags |= BR_SGRP_F_SEND; 17065bf1e00bSNikolay Aleksandrov to_send++; 17075bf1e00bSNikolay Aleksandrov } 17085bf1e00bSNikolay Aleksandrov srcs += src_size; 17095bf1e00bSNikolay Aleksandrov } 17105bf1e00bSNikolay Aleksandrov 17115bf1e00bSNikolay Aleksandrov if (__grp_src_delete_marked(pg)) 17125bf1e00bSNikolay Aleksandrov changed = true; 17135bf1e00bSNikolay Aleksandrov if (to_send) 17145bf1e00bSNikolay Aleksandrov __grp_src_query_marked_and_rexmit(pg); 17155bf1e00bSNikolay Aleksandrov 17165bf1e00bSNikolay Aleksandrov return changed; 17175bf1e00bSNikolay Aleksandrov } 17185bf1e00bSNikolay Aleksandrov 17195bf1e00bSNikolay Aleksandrov static bool br_multicast_toex(struct net_bridge_port_group *pg, 17205bf1e00bSNikolay Aleksandrov void *srcs, u32 nsrcs, size_t src_size) 17215bf1e00bSNikolay Aleksandrov { 17225bf1e00bSNikolay Aleksandrov struct net_bridge *br = pg->port->br; 17235bf1e00bSNikolay Aleksandrov bool changed = false; 17245bf1e00bSNikolay Aleksandrov 17255bf1e00bSNikolay Aleksandrov switch (pg->filter_mode) { 17265bf1e00bSNikolay Aleksandrov case MCAST_INCLUDE: 17275bf1e00bSNikolay Aleksandrov __grp_src_toex_incl(pg, srcs, nsrcs, src_size); 17285bf1e00bSNikolay Aleksandrov changed = true; 17295bf1e00bSNikolay Aleksandrov break; 17305bf1e00bSNikolay Aleksandrov case MCAST_EXCLUDE: 1731d5bf31ddSNikolay Aleksandrov changed = __grp_src_toex_excl(pg, srcs, nsrcs, src_size); 17325bf1e00bSNikolay Aleksandrov break; 17335bf1e00bSNikolay Aleksandrov } 17345bf1e00bSNikolay Aleksandrov 17355bf1e00bSNikolay Aleksandrov pg->filter_mode = MCAST_EXCLUDE; 17365bf1e00bSNikolay Aleksandrov mod_timer(&pg->timer, jiffies + br_multicast_gmi(br)); 17375bf1e00bSNikolay Aleksandrov 17385bf1e00bSNikolay Aleksandrov return changed; 17395bf1e00bSNikolay Aleksandrov } 17405bf1e00bSNikolay Aleksandrov 1741109865feSNikolay Aleksandrov /* State Msg type New state Actions 1742109865feSNikolay Aleksandrov * INCLUDE (A) BLOCK (B) INCLUDE (A) Send Q(G,A*B) 1743109865feSNikolay Aleksandrov */ 1744109865feSNikolay Aleksandrov static void __grp_src_block_incl(struct net_bridge_port_group *pg, 1745109865feSNikolay Aleksandrov void *srcs, u32 nsrcs, size_t src_size) 1746109865feSNikolay Aleksandrov { 1747109865feSNikolay Aleksandrov struct net_bridge_group_src *ent; 1748109865feSNikolay Aleksandrov u32 src_idx, to_send = 0; 1749109865feSNikolay Aleksandrov struct br_ip src_ip; 1750109865feSNikolay Aleksandrov 1751109865feSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 1752109865feSNikolay Aleksandrov ent->flags &= ~BR_SGRP_F_SEND; 1753109865feSNikolay Aleksandrov 1754109865feSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 1755109865feSNikolay Aleksandrov src_ip.proto = pg->addr.proto; 1756109865feSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 1757deb96566SNikolay Aleksandrov memcpy(&src_ip.src, srcs, src_size); 1758109865feSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 1759109865feSNikolay Aleksandrov if (ent) { 1760109865feSNikolay Aleksandrov ent->flags |= BR_SGRP_F_SEND; 1761109865feSNikolay Aleksandrov to_send++; 1762109865feSNikolay Aleksandrov } 1763109865feSNikolay Aleksandrov srcs += src_size; 1764109865feSNikolay Aleksandrov } 1765109865feSNikolay Aleksandrov 1766109865feSNikolay Aleksandrov if (to_send) 1767109865feSNikolay Aleksandrov __grp_src_query_marked_and_rexmit(pg); 1768109865feSNikolay Aleksandrov 1769109865feSNikolay Aleksandrov if (pg->filter_mode == MCAST_INCLUDE && hlist_empty(&pg->src_list)) 1770109865feSNikolay Aleksandrov br_multicast_find_del_pg(pg->port->br, pg); 1771109865feSNikolay Aleksandrov } 1772109865feSNikolay Aleksandrov 1773109865feSNikolay Aleksandrov /* State Msg type New state Actions 1774109865feSNikolay Aleksandrov * EXCLUDE (X,Y) BLOCK (A) EXCLUDE (X+(A-Y),Y) (A-X-Y)=Group Timer 1775109865feSNikolay Aleksandrov * Send Q(G,A-Y) 1776109865feSNikolay Aleksandrov */ 1777109865feSNikolay Aleksandrov static bool __grp_src_block_excl(struct net_bridge_port_group *pg, 1778109865feSNikolay Aleksandrov void *srcs, u32 nsrcs, size_t src_size) 1779109865feSNikolay Aleksandrov { 1780109865feSNikolay Aleksandrov struct net_bridge_group_src *ent; 1781109865feSNikolay Aleksandrov u32 src_idx, to_send = 0; 1782109865feSNikolay Aleksandrov bool changed = false; 1783109865feSNikolay Aleksandrov struct br_ip src_ip; 1784109865feSNikolay Aleksandrov 1785109865feSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 1786109865feSNikolay Aleksandrov ent->flags &= ~BR_SGRP_F_SEND; 1787109865feSNikolay Aleksandrov 1788109865feSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 1789109865feSNikolay Aleksandrov src_ip.proto = pg->addr.proto; 1790109865feSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 1791deb96566SNikolay Aleksandrov memcpy(&src_ip.src, srcs, src_size); 1792109865feSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 1793109865feSNikolay Aleksandrov if (!ent) { 1794109865feSNikolay Aleksandrov ent = br_multicast_new_group_src(pg, &src_ip); 1795109865feSNikolay Aleksandrov if (ent) { 1796109865feSNikolay Aleksandrov mod_timer(&ent->timer, pg->timer.expires); 1797109865feSNikolay Aleksandrov changed = true; 1798109865feSNikolay Aleksandrov } 1799109865feSNikolay Aleksandrov } 1800109865feSNikolay Aleksandrov if (ent && timer_pending(&ent->timer)) { 1801109865feSNikolay Aleksandrov ent->flags |= BR_SGRP_F_SEND; 1802109865feSNikolay Aleksandrov to_send++; 1803109865feSNikolay Aleksandrov } 1804109865feSNikolay Aleksandrov srcs += src_size; 1805109865feSNikolay Aleksandrov } 1806109865feSNikolay Aleksandrov 1807109865feSNikolay Aleksandrov if (to_send) 1808109865feSNikolay Aleksandrov __grp_src_query_marked_and_rexmit(pg); 1809109865feSNikolay Aleksandrov 1810109865feSNikolay Aleksandrov return changed; 1811109865feSNikolay Aleksandrov } 1812109865feSNikolay Aleksandrov 1813109865feSNikolay Aleksandrov static bool br_multicast_block(struct net_bridge_port_group *pg, 1814109865feSNikolay Aleksandrov void *srcs, u32 nsrcs, size_t src_size) 1815109865feSNikolay Aleksandrov { 1816109865feSNikolay Aleksandrov bool changed = false; 1817109865feSNikolay Aleksandrov 1818109865feSNikolay Aleksandrov switch (pg->filter_mode) { 1819109865feSNikolay Aleksandrov case MCAST_INCLUDE: 1820109865feSNikolay Aleksandrov __grp_src_block_incl(pg, srcs, nsrcs, src_size); 1821109865feSNikolay Aleksandrov break; 1822109865feSNikolay Aleksandrov case MCAST_EXCLUDE: 1823109865feSNikolay Aleksandrov changed = __grp_src_block_excl(pg, srcs, nsrcs, src_size); 1824109865feSNikolay Aleksandrov break; 1825109865feSNikolay Aleksandrov } 1826109865feSNikolay Aleksandrov 1827109865feSNikolay Aleksandrov return changed; 1828109865feSNikolay Aleksandrov } 1829109865feSNikolay Aleksandrov 18300436862eSNikolay Aleksandrov static struct net_bridge_port_group * 18310436862eSNikolay Aleksandrov br_multicast_find_port(struct net_bridge_mdb_entry *mp, 18320436862eSNikolay Aleksandrov struct net_bridge_port *p, 18330436862eSNikolay Aleksandrov const unsigned char *src) 18340436862eSNikolay Aleksandrov { 1835071445c6SNikolay Aleksandrov struct net_bridge *br __maybe_unused = mp->br; 18360436862eSNikolay Aleksandrov struct net_bridge_port_group *pg; 18370436862eSNikolay Aleksandrov 18380436862eSNikolay Aleksandrov for (pg = mlock_dereference(mp->ports, br); 18390436862eSNikolay Aleksandrov pg; 18400436862eSNikolay Aleksandrov pg = mlock_dereference(pg->next, br)) 18410436862eSNikolay Aleksandrov if (br_port_group_equal(pg, p, src)) 18420436862eSNikolay Aleksandrov return pg; 18430436862eSNikolay Aleksandrov 18440436862eSNikolay Aleksandrov return NULL; 18450436862eSNikolay Aleksandrov } 18460436862eSNikolay Aleksandrov 18478ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_igmp3_report(struct net_bridge *br, 1848eb1d1641SHerbert Xu struct net_bridge_port *port, 184906499098SVlad Yasevich struct sk_buff *skb, 185006499098SVlad Yasevich u16 vid) 1851eb1d1641SHerbert Xu { 18520436862eSNikolay Aleksandrov bool igmpv2 = br->multicast_igmp_version == 2; 18530436862eSNikolay Aleksandrov struct net_bridge_mdb_entry *mdst; 18540436862eSNikolay Aleksandrov struct net_bridge_port_group *pg; 18556db6f0eaSFelix Fietkau const unsigned char *src; 1856eb1d1641SHerbert Xu struct igmpv3_report *ih; 1857eb1d1641SHerbert Xu struct igmpv3_grec *grec; 18580436862eSNikolay Aleksandrov int i, len, num, type; 18590436862eSNikolay Aleksandrov bool changed = false; 1860eb1d1641SHerbert Xu __be32 group; 18610436862eSNikolay Aleksandrov int err = 0; 1862e57f6185SNikolay Aleksandrov u16 nsrcs; 1863eb1d1641SHerbert Xu 1864eb1d1641SHerbert Xu ih = igmpv3_report_hdr(skb); 1865eb1d1641SHerbert Xu num = ntohs(ih->ngrec); 1866c2d4fbd2SLinus Lüssing len = skb_transport_offset(skb) + sizeof(*ih); 1867eb1d1641SHerbert Xu 1868eb1d1641SHerbert Xu for (i = 0; i < num; i++) { 1869eb1d1641SHerbert Xu len += sizeof(*grec); 1870ba5ea614SLinus Lüssing if (!ip_mc_may_pull(skb, len)) 1871eb1d1641SHerbert Xu return -EINVAL; 1872eb1d1641SHerbert Xu 1873fd218cf9SHerbert Xu grec = (void *)(skb->data + len - sizeof(*grec)); 1874eb1d1641SHerbert Xu group = grec->grec_mca; 1875eb1d1641SHerbert Xu type = grec->grec_type; 1876e57f6185SNikolay Aleksandrov nsrcs = ntohs(grec->grec_nsrcs); 1877eb1d1641SHerbert Xu 1878e57f6185SNikolay Aleksandrov len += nsrcs * 4; 1879ba5ea614SLinus Lüssing if (!ip_mc_may_pull(skb, len)) 1880eb1d1641SHerbert Xu return -EINVAL; 1881eb1d1641SHerbert Xu 1882eb1d1641SHerbert Xu switch (type) { 1883eb1d1641SHerbert Xu case IGMPV3_MODE_IS_INCLUDE: 1884eb1d1641SHerbert Xu case IGMPV3_MODE_IS_EXCLUDE: 1885eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_INCLUDE: 1886eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_EXCLUDE: 1887eb1d1641SHerbert Xu case IGMPV3_ALLOW_NEW_SOURCES: 1888eb1d1641SHerbert Xu case IGMPV3_BLOCK_OLD_SOURCES: 1889eb1d1641SHerbert Xu break; 1890eb1d1641SHerbert Xu 1891eb1d1641SHerbert Xu default: 1892eb1d1641SHerbert Xu continue; 1893eb1d1641SHerbert Xu } 1894eb1d1641SHerbert Xu 18956db6f0eaSFelix Fietkau src = eth_hdr(skb)->h_source; 18960436862eSNikolay Aleksandrov if (nsrcs == 0 && 18970436862eSNikolay Aleksandrov (type == IGMPV3_CHANGE_TO_INCLUDE || 18980436862eSNikolay Aleksandrov type == IGMPV3_MODE_IS_INCLUDE)) { 18990436862eSNikolay Aleksandrov if (!port || igmpv2) { 19006db6f0eaSFelix Fietkau br_ip4_multicast_leave_group(br, port, group, vid, src); 19010436862eSNikolay Aleksandrov continue; 19020436862eSNikolay Aleksandrov } 1903bc8c20acSSatish Ashok } else { 19046db6f0eaSFelix Fietkau err = br_ip4_multicast_add_group(br, port, group, vid, 19050436862eSNikolay Aleksandrov src, igmpv2); 1906eb1d1641SHerbert Xu if (err) 1907eb1d1641SHerbert Xu break; 1908eb1d1641SHerbert Xu } 19090436862eSNikolay Aleksandrov 19100436862eSNikolay Aleksandrov if (!port || igmpv2) 19110436862eSNikolay Aleksandrov continue; 19120436862eSNikolay Aleksandrov 19130436862eSNikolay Aleksandrov spin_lock_bh(&br->multicast_lock); 19140436862eSNikolay Aleksandrov mdst = br_mdb_ip4_get(br, group, vid); 19150436862eSNikolay Aleksandrov if (!mdst) 19160436862eSNikolay Aleksandrov goto unlock_continue; 19170436862eSNikolay Aleksandrov pg = br_multicast_find_port(mdst, port, src); 19180436862eSNikolay Aleksandrov if (!pg || (pg->flags & MDB_PG_FLAGS_PERMANENT)) 19190436862eSNikolay Aleksandrov goto unlock_continue; 19200436862eSNikolay Aleksandrov /* reload grec */ 19210436862eSNikolay Aleksandrov grec = (void *)(skb->data + len - sizeof(*grec) - (nsrcs * 4)); 19220436862eSNikolay Aleksandrov switch (type) { 19230436862eSNikolay Aleksandrov case IGMPV3_ALLOW_NEW_SOURCES: 19240436862eSNikolay Aleksandrov changed = br_multicast_isinc_allow(pg, grec->grec_src, 19250436862eSNikolay Aleksandrov nsrcs, sizeof(__be32)); 19260436862eSNikolay Aleksandrov break; 1927e6231bcaSNikolay Aleksandrov case IGMPV3_MODE_IS_INCLUDE: 1928e6231bcaSNikolay Aleksandrov changed = br_multicast_isinc_allow(pg, grec->grec_src, nsrcs, 1929e6231bcaSNikolay Aleksandrov sizeof(__be32)); 1930e6231bcaSNikolay Aleksandrov break; 1931e6231bcaSNikolay Aleksandrov case IGMPV3_MODE_IS_EXCLUDE: 1932e6231bcaSNikolay Aleksandrov changed = br_multicast_isexc(pg, grec->grec_src, nsrcs, 1933e6231bcaSNikolay Aleksandrov sizeof(__be32)); 1934e6231bcaSNikolay Aleksandrov break; 19355bf1e00bSNikolay Aleksandrov case IGMPV3_CHANGE_TO_INCLUDE: 19365bf1e00bSNikolay Aleksandrov changed = br_multicast_toin(pg, grec->grec_src, nsrcs, 19375bf1e00bSNikolay Aleksandrov sizeof(__be32)); 19385bf1e00bSNikolay Aleksandrov break; 19395bf1e00bSNikolay Aleksandrov case IGMPV3_CHANGE_TO_EXCLUDE: 19405bf1e00bSNikolay Aleksandrov changed = br_multicast_toex(pg, grec->grec_src, nsrcs, 19415bf1e00bSNikolay Aleksandrov sizeof(__be32)); 19425bf1e00bSNikolay Aleksandrov break; 1943109865feSNikolay Aleksandrov case IGMPV3_BLOCK_OLD_SOURCES: 1944109865feSNikolay Aleksandrov changed = br_multicast_block(pg, grec->grec_src, nsrcs, 1945109865feSNikolay Aleksandrov sizeof(__be32)); 1946109865feSNikolay Aleksandrov break; 19470436862eSNikolay Aleksandrov } 19480436862eSNikolay Aleksandrov if (changed) 19490436862eSNikolay Aleksandrov br_mdb_notify(br->dev, mdst, pg, RTM_NEWMDB); 19500436862eSNikolay Aleksandrov unlock_continue: 19510436862eSNikolay Aleksandrov spin_unlock_bh(&br->multicast_lock); 1952bc8c20acSSatish Ashok } 1953eb1d1641SHerbert Xu 1954eb1d1641SHerbert Xu return err; 1955eb1d1641SHerbert Xu } 1956eb1d1641SHerbert Xu 1957dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 195808b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_mld2_report(struct net_bridge *br, 195908b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 196006499098SVlad Yasevich struct sk_buff *skb, 196106499098SVlad Yasevich u16 vid) 196208b202b6SYOSHIFUJI Hideaki { 19630436862eSNikolay Aleksandrov bool mldv1 = br->multicast_mld_version == 1; 19640436862eSNikolay Aleksandrov struct net_bridge_mdb_entry *mdst; 19650436862eSNikolay Aleksandrov struct net_bridge_port_group *pg; 1966ba5ea614SLinus Lüssing unsigned int nsrcs_offset; 19676db6f0eaSFelix Fietkau const unsigned char *src; 196808b202b6SYOSHIFUJI Hideaki struct icmp6hdr *icmp6h; 196908b202b6SYOSHIFUJI Hideaki struct mld2_grec *grec; 1970ba5ea614SLinus Lüssing unsigned int grec_len; 19710436862eSNikolay Aleksandrov bool changed = false; 19720436862eSNikolay Aleksandrov int i, len, num; 197308b202b6SYOSHIFUJI Hideaki int err = 0; 197408b202b6SYOSHIFUJI Hideaki 1975ba5ea614SLinus Lüssing if (!ipv6_mc_may_pull(skb, sizeof(*icmp6h))) 197608b202b6SYOSHIFUJI Hideaki return -EINVAL; 197708b202b6SYOSHIFUJI Hideaki 197808b202b6SYOSHIFUJI Hideaki icmp6h = icmp6_hdr(skb); 197908b202b6SYOSHIFUJI Hideaki num = ntohs(icmp6h->icmp6_dataun.un_data16[1]); 1980c2d4fbd2SLinus Lüssing len = skb_transport_offset(skb) + sizeof(*icmp6h); 198108b202b6SYOSHIFUJI Hideaki 198208b202b6SYOSHIFUJI Hideaki for (i = 0; i < num; i++) { 1983e57f6185SNikolay Aleksandrov __be16 *_nsrcs, __nsrcs; 1984e57f6185SNikolay Aleksandrov u16 nsrcs; 198508b202b6SYOSHIFUJI Hideaki 1986ba5ea614SLinus Lüssing nsrcs_offset = len + offsetof(struct mld2_grec, grec_nsrcs); 1987ba5ea614SLinus Lüssing 1988ba5ea614SLinus Lüssing if (skb_transport_offset(skb) + ipv6_transport_len(skb) < 19895fc6266aSLinus Lüssing nsrcs_offset + sizeof(__nsrcs)) 1990ba5ea614SLinus Lüssing return -EINVAL; 1991ba5ea614SLinus Lüssing 1992e57f6185SNikolay Aleksandrov _nsrcs = skb_header_pointer(skb, nsrcs_offset, 1993e57f6185SNikolay Aleksandrov sizeof(__nsrcs), &__nsrcs); 1994e57f6185SNikolay Aleksandrov if (!_nsrcs) 199508b202b6SYOSHIFUJI Hideaki return -EINVAL; 199608b202b6SYOSHIFUJI Hideaki 1997e57f6185SNikolay Aleksandrov nsrcs = ntohs(*_nsrcs); 1998e57f6185SNikolay Aleksandrov grec_len = struct_size(grec, grec_src, nsrcs); 1999ba5ea614SLinus Lüssing 2000ba5ea614SLinus Lüssing if (!ipv6_mc_may_pull(skb, len + grec_len)) 200108b202b6SYOSHIFUJI Hideaki return -EINVAL; 200208b202b6SYOSHIFUJI Hideaki 200308b202b6SYOSHIFUJI Hideaki grec = (struct mld2_grec *)(skb->data + len); 2004ba5ea614SLinus Lüssing len += grec_len; 200508b202b6SYOSHIFUJI Hideaki 200608b202b6SYOSHIFUJI Hideaki switch (grec->grec_type) { 200708b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_INCLUDE: 200808b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_EXCLUDE: 200908b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_INCLUDE: 201008b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_EXCLUDE: 201108b202b6SYOSHIFUJI Hideaki case MLD2_ALLOW_NEW_SOURCES: 201208b202b6SYOSHIFUJI Hideaki case MLD2_BLOCK_OLD_SOURCES: 201308b202b6SYOSHIFUJI Hideaki break; 201408b202b6SYOSHIFUJI Hideaki 201508b202b6SYOSHIFUJI Hideaki default: 201608b202b6SYOSHIFUJI Hideaki continue; 201708b202b6SYOSHIFUJI Hideaki } 201808b202b6SYOSHIFUJI Hideaki 20196db6f0eaSFelix Fietkau src = eth_hdr(skb)->h_source; 2020bc8c20acSSatish Ashok if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE || 2021bc8c20acSSatish Ashok grec->grec_type == MLD2_MODE_IS_INCLUDE) && 2022e57f6185SNikolay Aleksandrov nsrcs == 0) { 20230436862eSNikolay Aleksandrov if (!port || mldv1) { 20240436862eSNikolay Aleksandrov br_ip6_multicast_leave_group(br, port, 20250436862eSNikolay Aleksandrov &grec->grec_mca, 20266db6f0eaSFelix Fietkau vid, src); 20270436862eSNikolay Aleksandrov continue; 20280436862eSNikolay Aleksandrov } 2029bc8c20acSSatish Ashok } else { 2030bc8c20acSSatish Ashok err = br_ip6_multicast_add_group(br, port, 20316db6f0eaSFelix Fietkau &grec->grec_mca, vid, 20320436862eSNikolay Aleksandrov src, mldv1); 20339264251eSDavide Caratti if (err) 203408b202b6SYOSHIFUJI Hideaki break; 203508b202b6SYOSHIFUJI Hideaki } 20360436862eSNikolay Aleksandrov 20370436862eSNikolay Aleksandrov if (!port || mldv1) 20380436862eSNikolay Aleksandrov continue; 20390436862eSNikolay Aleksandrov 20400436862eSNikolay Aleksandrov spin_lock_bh(&br->multicast_lock); 20410436862eSNikolay Aleksandrov mdst = br_mdb_ip6_get(br, &grec->grec_mca, vid); 20420436862eSNikolay Aleksandrov if (!mdst) 20430436862eSNikolay Aleksandrov goto unlock_continue; 20440436862eSNikolay Aleksandrov pg = br_multicast_find_port(mdst, port, src); 20450436862eSNikolay Aleksandrov if (!pg || (pg->flags & MDB_PG_FLAGS_PERMANENT)) 20460436862eSNikolay Aleksandrov goto unlock_continue; 20470436862eSNikolay Aleksandrov switch (grec->grec_type) { 20480436862eSNikolay Aleksandrov case MLD2_ALLOW_NEW_SOURCES: 20490436862eSNikolay Aleksandrov changed = br_multicast_isinc_allow(pg, grec->grec_src, 20500436862eSNikolay Aleksandrov nsrcs, 20510436862eSNikolay Aleksandrov sizeof(struct in6_addr)); 20520436862eSNikolay Aleksandrov break; 2053e6231bcaSNikolay Aleksandrov case MLD2_MODE_IS_INCLUDE: 2054e6231bcaSNikolay Aleksandrov changed = br_multicast_isinc_allow(pg, grec->grec_src, nsrcs, 2055e6231bcaSNikolay Aleksandrov sizeof(struct in6_addr)); 2056e6231bcaSNikolay Aleksandrov break; 2057e6231bcaSNikolay Aleksandrov case MLD2_MODE_IS_EXCLUDE: 2058e6231bcaSNikolay Aleksandrov changed = br_multicast_isexc(pg, grec->grec_src, nsrcs, 2059e6231bcaSNikolay Aleksandrov sizeof(struct in6_addr)); 2060e6231bcaSNikolay Aleksandrov break; 20615bf1e00bSNikolay Aleksandrov case MLD2_CHANGE_TO_INCLUDE: 20625bf1e00bSNikolay Aleksandrov changed = br_multicast_toin(pg, grec->grec_src, nsrcs, 20635bf1e00bSNikolay Aleksandrov sizeof(struct in6_addr)); 20645bf1e00bSNikolay Aleksandrov break; 20655bf1e00bSNikolay Aleksandrov case MLD2_CHANGE_TO_EXCLUDE: 20665bf1e00bSNikolay Aleksandrov changed = br_multicast_toex(pg, grec->grec_src, nsrcs, 20675bf1e00bSNikolay Aleksandrov sizeof(struct in6_addr)); 20685bf1e00bSNikolay Aleksandrov break; 2069109865feSNikolay Aleksandrov case MLD2_BLOCK_OLD_SOURCES: 2070109865feSNikolay Aleksandrov changed = br_multicast_block(pg, grec->grec_src, nsrcs, 2071109865feSNikolay Aleksandrov sizeof(struct in6_addr)); 2072109865feSNikolay Aleksandrov break; 20730436862eSNikolay Aleksandrov } 20740436862eSNikolay Aleksandrov if (changed) 20750436862eSNikolay Aleksandrov br_mdb_notify(br->dev, mdst, pg, RTM_NEWMDB); 20760436862eSNikolay Aleksandrov unlock_continue: 20770436862eSNikolay Aleksandrov spin_unlock_bh(&br->multicast_lock); 2078bc8c20acSSatish Ashok } 207908b202b6SYOSHIFUJI Hideaki 208008b202b6SYOSHIFUJI Hideaki return err; 208108b202b6SYOSHIFUJI Hideaki } 208208b202b6SYOSHIFUJI Hideaki #endif 208308b202b6SYOSHIFUJI Hideaki 2084dc4eb53aSLinus Lüssing static bool br_ip4_multicast_select_querier(struct net_bridge *br, 20852cd41431SLinus Lüssing struct net_bridge_port *port, 2086dc4eb53aSLinus Lüssing __be32 saddr) 2087dc4eb53aSLinus Lüssing { 2088dc4eb53aSLinus Lüssing if (!timer_pending(&br->ip4_own_query.timer) && 2089dc4eb53aSLinus Lüssing !timer_pending(&br->ip4_other_query.timer)) 2090dc4eb53aSLinus Lüssing goto update; 2091dc4eb53aSLinus Lüssing 2092deb96566SNikolay Aleksandrov if (!br->ip4_querier.addr.src.ip4) 2093dc4eb53aSLinus Lüssing goto update; 2094dc4eb53aSLinus Lüssing 2095deb96566SNikolay Aleksandrov if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.src.ip4)) 2096dc4eb53aSLinus Lüssing goto update; 2097dc4eb53aSLinus Lüssing 2098dc4eb53aSLinus Lüssing return false; 2099dc4eb53aSLinus Lüssing 2100dc4eb53aSLinus Lüssing update: 2101deb96566SNikolay Aleksandrov br->ip4_querier.addr.src.ip4 = saddr; 2102dc4eb53aSLinus Lüssing 21032cd41431SLinus Lüssing /* update protected by general multicast_lock by caller */ 21042cd41431SLinus Lüssing rcu_assign_pointer(br->ip4_querier.port, port); 21052cd41431SLinus Lüssing 2106dc4eb53aSLinus Lüssing return true; 2107dc4eb53aSLinus Lüssing } 2108dc4eb53aSLinus Lüssing 2109dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 2110dc4eb53aSLinus Lüssing static bool br_ip6_multicast_select_querier(struct net_bridge *br, 21112cd41431SLinus Lüssing struct net_bridge_port *port, 2112dc4eb53aSLinus Lüssing struct in6_addr *saddr) 2113dc4eb53aSLinus Lüssing { 2114dc4eb53aSLinus Lüssing if (!timer_pending(&br->ip6_own_query.timer) && 2115dc4eb53aSLinus Lüssing !timer_pending(&br->ip6_other_query.timer)) 2116dc4eb53aSLinus Lüssing goto update; 2117dc4eb53aSLinus Lüssing 2118deb96566SNikolay Aleksandrov if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.src.ip6) <= 0) 2119dc4eb53aSLinus Lüssing goto update; 2120dc4eb53aSLinus Lüssing 2121dc4eb53aSLinus Lüssing return false; 2122dc4eb53aSLinus Lüssing 2123dc4eb53aSLinus Lüssing update: 2124deb96566SNikolay Aleksandrov br->ip6_querier.addr.src.ip6 = *saddr; 2125dc4eb53aSLinus Lüssing 21262cd41431SLinus Lüssing /* update protected by general multicast_lock by caller */ 21272cd41431SLinus Lüssing rcu_assign_pointer(br->ip6_querier.port, port); 21282cd41431SLinus Lüssing 2129dc4eb53aSLinus Lüssing return true; 2130dc4eb53aSLinus Lüssing } 2131dc4eb53aSLinus Lüssing #endif 2132dc4eb53aSLinus Lüssing 2133dc4eb53aSLinus Lüssing static bool br_multicast_select_querier(struct net_bridge *br, 21342cd41431SLinus Lüssing struct net_bridge_port *port, 2135dc4eb53aSLinus Lüssing struct br_ip *saddr) 2136dc4eb53aSLinus Lüssing { 2137dc4eb53aSLinus Lüssing switch (saddr->proto) { 2138dc4eb53aSLinus Lüssing case htons(ETH_P_IP): 2139deb96566SNikolay Aleksandrov return br_ip4_multicast_select_querier(br, port, saddr->src.ip4); 2140dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 2141dc4eb53aSLinus Lüssing case htons(ETH_P_IPV6): 2142deb96566SNikolay Aleksandrov return br_ip6_multicast_select_querier(br, port, &saddr->src.ip6); 2143dc4eb53aSLinus Lüssing #endif 2144dc4eb53aSLinus Lüssing } 2145dc4eb53aSLinus Lüssing 2146dc4eb53aSLinus Lüssing return false; 2147dc4eb53aSLinus Lüssing } 2148dc4eb53aSLinus Lüssing 2149cc0fdd80SLinus Lüssing static void 215090010b36SLinus Lüssing br_multicast_update_query_timer(struct net_bridge *br, 215190010b36SLinus Lüssing struct bridge_mcast_other_query *query, 2152b00589afSLinus Lüssing unsigned long max_delay) 2153b00589afSLinus Lüssing { 215490010b36SLinus Lüssing if (!timer_pending(&query->timer)) 215590010b36SLinus Lüssing query->delay_time = jiffies + max_delay; 2156b00589afSLinus Lüssing 215790010b36SLinus Lüssing mod_timer(&query->timer, jiffies + br->multicast_querier_interval); 2158b00589afSLinus Lüssing } 2159b00589afSLinus Lüssing 21606d549648SNogah Frankel static void br_port_mc_router_state_change(struct net_bridge_port *p, 21616d549648SNogah Frankel bool is_mc_router) 21626d549648SNogah Frankel { 21636d549648SNogah Frankel struct switchdev_attr attr = { 21646d549648SNogah Frankel .orig_dev = p->dev, 21656d549648SNogah Frankel .id = SWITCHDEV_ATTR_ID_PORT_MROUTER, 21666d549648SNogah Frankel .flags = SWITCHDEV_F_DEFER, 21676d549648SNogah Frankel .u.mrouter = is_mc_router, 21686d549648SNogah Frankel }; 21696d549648SNogah Frankel 21706d549648SNogah Frankel switchdev_port_attr_set(p->dev, &attr); 21716d549648SNogah Frankel } 21726d549648SNogah Frankel 21737e80c124Sstephen hemminger /* 21747c77602fSCong Wang * Add port to router_list 21757e80c124Sstephen hemminger * list is maintained ordered by pointer value 21767e80c124Sstephen hemminger * and locked by br->multicast_lock and RCU 21777e80c124Sstephen hemminger */ 21780909e117SHerbert Xu static void br_multicast_add_router(struct net_bridge *br, 21790909e117SHerbert Xu struct net_bridge_port *port) 21800909e117SHerbert Xu { 2181dcdca2c4Sstephen hemminger struct net_bridge_port *p; 2182b67bfe0dSSasha Levin struct hlist_node *slot = NULL; 21830909e117SHerbert Xu 21841a040eacSNikolay Aleksandrov if (!hlist_unhashed(&port->rlist)) 21851a040eacSNikolay Aleksandrov return; 21861a040eacSNikolay Aleksandrov 2187b67bfe0dSSasha Levin hlist_for_each_entry(p, &br->router_list, rlist) { 21887e80c124Sstephen hemminger if ((unsigned long) port >= (unsigned long) p) 21897e80c124Sstephen hemminger break; 2190b67bfe0dSSasha Levin slot = &p->rlist; 2191dcdca2c4Sstephen hemminger } 21920909e117SHerbert Xu 21937e80c124Sstephen hemminger if (slot) 21941d023284SKen Helias hlist_add_behind_rcu(&port->rlist, slot); 2195dcdca2c4Sstephen hemminger else 2196dcdca2c4Sstephen hemminger hlist_add_head_rcu(&port->rlist, &br->router_list); 2197949f1e39SSatish Ashok br_rtr_notify(br->dev, port, RTM_NEWMDB); 21986d549648SNogah Frankel br_port_mc_router_state_change(port, true); 21990909e117SHerbert Xu } 22000909e117SHerbert Xu 2201eb1d1641SHerbert Xu static void br_multicast_mark_router(struct net_bridge *br, 2202eb1d1641SHerbert Xu struct net_bridge_port *port) 2203eb1d1641SHerbert Xu { 2204eb1d1641SHerbert Xu unsigned long now = jiffies; 2205eb1d1641SHerbert Xu 2206eb1d1641SHerbert Xu if (!port) { 220777041420SYotam Gigi if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) { 220877041420SYotam Gigi if (!timer_pending(&br->multicast_router_timer)) 220977041420SYotam Gigi br_mc_router_state_change(br, true); 2210eb1d1641SHerbert Xu mod_timer(&br->multicast_router_timer, 2211eb1d1641SHerbert Xu now + br->multicast_querier_interval); 221277041420SYotam Gigi } 2213eb1d1641SHerbert Xu return; 2214eb1d1641SHerbert Xu } 2215eb1d1641SHerbert Xu 2216a55d8246SNikolay Aleksandrov if (port->multicast_router == MDB_RTR_TYPE_DISABLED || 2217a55d8246SNikolay Aleksandrov port->multicast_router == MDB_RTR_TYPE_PERM) 2218eb1d1641SHerbert Xu return; 2219eb1d1641SHerbert Xu 22200909e117SHerbert Xu br_multicast_add_router(br, port); 2221eb1d1641SHerbert Xu 2222eb1d1641SHerbert Xu mod_timer(&port->multicast_router_timer, 2223eb1d1641SHerbert Xu now + br->multicast_querier_interval); 2224eb1d1641SHerbert Xu } 2225eb1d1641SHerbert Xu 2226eb1d1641SHerbert Xu static void br_multicast_query_received(struct net_bridge *br, 2227eb1d1641SHerbert Xu struct net_bridge_port *port, 222890010b36SLinus Lüssing struct bridge_mcast_other_query *query, 2229dc4eb53aSLinus Lüssing struct br_ip *saddr, 2230b00589afSLinus Lüssing unsigned long max_delay) 2231eb1d1641SHerbert Xu { 22322cd41431SLinus Lüssing if (!br_multicast_select_querier(br, port, saddr)) 2233eb1d1641SHerbert Xu return; 2234eb1d1641SHerbert Xu 2235dc4eb53aSLinus Lüssing br_multicast_update_query_timer(br, query, max_delay); 2236eb1d1641SHerbert Xu br_multicast_mark_router(br, port); 2237eb1d1641SHerbert Xu } 2238eb1d1641SHerbert Xu 22399c2e955cSzhong jiang static void br_ip4_multicast_query(struct net_bridge *br, 2240eb1d1641SHerbert Xu struct net_bridge_port *port, 224106499098SVlad Yasevich struct sk_buff *skb, 224206499098SVlad Yasevich u16 vid) 2243eb1d1641SHerbert Xu { 2244ba5ea614SLinus Lüssing unsigned int transport_len = ip_transport_len(skb); 2245b71d1d42SEric Dumazet const struct iphdr *iph = ip_hdr(skb); 2246eb1d1641SHerbert Xu struct igmphdr *ih = igmp_hdr(skb); 2247eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 2248eb1d1641SHerbert Xu struct igmpv3_query *ih3; 2249eb1d1641SHerbert Xu struct net_bridge_port_group *p; 2250e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 2251dc4eb53aSLinus Lüssing struct br_ip saddr; 2252eb1d1641SHerbert Xu unsigned long max_delay; 2253eb1d1641SHerbert Xu unsigned long now = jiffies; 2254eb1d1641SHerbert Xu __be32 group; 2255eb1d1641SHerbert Xu 2256eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 2257eb1d1641SHerbert Xu if (!netif_running(br->dev) || 2258eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 2259eb1d1641SHerbert Xu goto out; 2260eb1d1641SHerbert Xu 2261eb1d1641SHerbert Xu group = ih->group; 2262eb1d1641SHerbert Xu 2263ba5ea614SLinus Lüssing if (transport_len == sizeof(*ih)) { 2264eb1d1641SHerbert Xu max_delay = ih->code * (HZ / IGMP_TIMER_SCALE); 2265eb1d1641SHerbert Xu 2266eb1d1641SHerbert Xu if (!max_delay) { 2267eb1d1641SHerbert Xu max_delay = 10 * HZ; 2268eb1d1641SHerbert Xu group = 0; 2269eb1d1641SHerbert Xu } 2270ba5ea614SLinus Lüssing } else if (transport_len >= sizeof(*ih3)) { 2271eb1d1641SHerbert Xu ih3 = igmpv3_query_hdr(skb); 227223550b83SNikolay Aleksandrov if (ih3->nsrcs || 227323550b83SNikolay Aleksandrov (br->multicast_igmp_version == 3 && group && ih3->suppress)) 2274bec68ff1SYOSHIFUJI Hideaki goto out; 2275eb1d1641SHerbert Xu 22760ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 max_delay = ih3->code ? 22770ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1; 22789afd85c9SLinus Lüssing } else { 22799ed973ccSLinus Lüssing goto out; 22809ed973ccSLinus Lüssing } 22819ed973ccSLinus Lüssing 2282dc4eb53aSLinus Lüssing if (!group) { 2283dc4eb53aSLinus Lüssing saddr.proto = htons(ETH_P_IP); 2284deb96566SNikolay Aleksandrov saddr.src.ip4 = iph->saddr; 2285b00589afSLinus Lüssing 2286dc4eb53aSLinus Lüssing br_multicast_query_received(br, port, &br->ip4_other_query, 2287dc4eb53aSLinus Lüssing &saddr, max_delay); 2288eb1d1641SHerbert Xu goto out; 2289dc4eb53aSLinus Lüssing } 2290eb1d1641SHerbert Xu 229119e3a9c9SNikolay Aleksandrov mp = br_mdb_ip4_get(br, group, vid); 2292eb1d1641SHerbert Xu if (!mp) 2293eb1d1641SHerbert Xu goto out; 2294eb1d1641SHerbert Xu 2295eb1d1641SHerbert Xu max_delay *= br->multicast_last_member_count; 2296eb1d1641SHerbert Xu 2297ff0fd34eSAndrew Lunn if (mp->host_joined && 2298eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 2299eb1d1641SHerbert Xu time_after(mp->timer.expires, now + max_delay) : 2300eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) 2301eb1d1641SHerbert Xu mod_timer(&mp->timer, now + max_delay); 2302eb1d1641SHerbert Xu 2303e8051688SEric Dumazet for (pp = &mp->ports; 2304e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 2305e8051688SEric Dumazet pp = &p->next) { 2306eb1d1641SHerbert Xu if (timer_pending(&p->timer) ? 2307eb1d1641SHerbert Xu time_after(p->timer.expires, now + max_delay) : 230823550b83SNikolay Aleksandrov try_to_del_timer_sync(&p->timer) >= 0 && 230923550b83SNikolay Aleksandrov (br->multicast_igmp_version == 2 || 231023550b83SNikolay Aleksandrov p->filter_mode == MCAST_EXCLUDE)) 231124f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 2312eb1d1641SHerbert Xu } 2313eb1d1641SHerbert Xu 2314eb1d1641SHerbert Xu out: 2315eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 2316eb1d1641SHerbert Xu } 2317eb1d1641SHerbert Xu 2318dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 231908b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_query(struct net_bridge *br, 232008b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 232106499098SVlad Yasevich struct sk_buff *skb, 232206499098SVlad Yasevich u16 vid) 232308b202b6SYOSHIFUJI Hideaki { 2324ba5ea614SLinus Lüssing unsigned int transport_len = ipv6_transport_len(skb); 2325eca2a43bSstephen hemminger struct mld_msg *mld; 232608b202b6SYOSHIFUJI Hideaki struct net_bridge_mdb_entry *mp; 232708b202b6SYOSHIFUJI Hideaki struct mld2_query *mld2q; 2328e8051688SEric Dumazet struct net_bridge_port_group *p; 2329e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 2330dc4eb53aSLinus Lüssing struct br_ip saddr; 233108b202b6SYOSHIFUJI Hideaki unsigned long max_delay; 233208b202b6SYOSHIFUJI Hideaki unsigned long now = jiffies; 2333856ce5d0SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 2334b71d1d42SEric Dumazet const struct in6_addr *group = NULL; 23359ed973ccSLinus Lüssing bool is_general_query; 233608b202b6SYOSHIFUJI Hideaki int err = 0; 233708b202b6SYOSHIFUJI Hideaki 233808b202b6SYOSHIFUJI Hideaki spin_lock(&br->multicast_lock); 233908b202b6SYOSHIFUJI Hideaki if (!netif_running(br->dev) || 234008b202b6SYOSHIFUJI Hideaki (port && port->state == BR_STATE_DISABLED)) 234108b202b6SYOSHIFUJI Hideaki goto out; 234208b202b6SYOSHIFUJI Hideaki 2343ba5ea614SLinus Lüssing if (transport_len == sizeof(*mld)) { 2344856ce5d0SLinus Lüssing if (!pskb_may_pull(skb, offset + sizeof(*mld))) { 234508b202b6SYOSHIFUJI Hideaki err = -EINVAL; 234608b202b6SYOSHIFUJI Hideaki goto out; 234708b202b6SYOSHIFUJI Hideaki } 234808b202b6SYOSHIFUJI Hideaki mld = (struct mld_msg *) icmp6_hdr(skb); 23494715213dSLi RongQing max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay)); 235008b202b6SYOSHIFUJI Hideaki if (max_delay) 235108b202b6SYOSHIFUJI Hideaki group = &mld->mld_mca; 2352248ba8ecSLinus Lüssing } else { 2353856ce5d0SLinus Lüssing if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) { 235408b202b6SYOSHIFUJI Hideaki err = -EINVAL; 235508b202b6SYOSHIFUJI Hideaki goto out; 235608b202b6SYOSHIFUJI Hideaki } 235708b202b6SYOSHIFUJI Hideaki mld2q = (struct mld2_query *)icmp6_hdr(skb); 235808b202b6SYOSHIFUJI Hideaki if (!mld2q->mld2q_nsrcs) 235908b202b6SYOSHIFUJI Hideaki group = &mld2q->mld2q_mca; 236023550b83SNikolay Aleksandrov if (br->multicast_mld_version == 2 && 236123550b83SNikolay Aleksandrov !ipv6_addr_any(&mld2q->mld2q_mca) && 236223550b83SNikolay Aleksandrov mld2q->mld2q_suppress) 236323550b83SNikolay Aleksandrov goto out; 2364e3f5b170SDaniel Borkmann 2365e3f5b170SDaniel Borkmann max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL); 236608b202b6SYOSHIFUJI Hideaki } 236708b202b6SYOSHIFUJI Hideaki 23689ed973ccSLinus Lüssing is_general_query = group && ipv6_addr_any(group); 23699ed973ccSLinus Lüssing 2370dc4eb53aSLinus Lüssing if (is_general_query) { 2371dc4eb53aSLinus Lüssing saddr.proto = htons(ETH_P_IPV6); 2372deb96566SNikolay Aleksandrov saddr.src.ip6 = ipv6_hdr(skb)->saddr; 2373b00589afSLinus Lüssing 2374dc4eb53aSLinus Lüssing br_multicast_query_received(br, port, &br->ip6_other_query, 2375dc4eb53aSLinus Lüssing &saddr, max_delay); 237608b202b6SYOSHIFUJI Hideaki goto out; 23776c03ee8bSLinus Lüssing } else if (!group) { 23786c03ee8bSLinus Lüssing goto out; 2379dc4eb53aSLinus Lüssing } 238008b202b6SYOSHIFUJI Hideaki 238119e3a9c9SNikolay Aleksandrov mp = br_mdb_ip6_get(br, group, vid); 238208b202b6SYOSHIFUJI Hideaki if (!mp) 238308b202b6SYOSHIFUJI Hideaki goto out; 238408b202b6SYOSHIFUJI Hideaki 238508b202b6SYOSHIFUJI Hideaki max_delay *= br->multicast_last_member_count; 2386ff0fd34eSAndrew Lunn if (mp->host_joined && 238708b202b6SYOSHIFUJI Hideaki (timer_pending(&mp->timer) ? 238808b202b6SYOSHIFUJI Hideaki time_after(mp->timer.expires, now + max_delay) : 238908b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&mp->timer) >= 0)) 239008b202b6SYOSHIFUJI Hideaki mod_timer(&mp->timer, now + max_delay); 239108b202b6SYOSHIFUJI Hideaki 2392e8051688SEric Dumazet for (pp = &mp->ports; 2393e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 2394e8051688SEric Dumazet pp = &p->next) { 239508b202b6SYOSHIFUJI Hideaki if (timer_pending(&p->timer) ? 239608b202b6SYOSHIFUJI Hideaki time_after(p->timer.expires, now + max_delay) : 239723550b83SNikolay Aleksandrov try_to_del_timer_sync(&p->timer) >= 0 && 239823550b83SNikolay Aleksandrov (br->multicast_mld_version == 1 || 239923550b83SNikolay Aleksandrov p->filter_mode == MCAST_EXCLUDE)) 240024f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 240108b202b6SYOSHIFUJI Hideaki } 240208b202b6SYOSHIFUJI Hideaki 240308b202b6SYOSHIFUJI Hideaki out: 240408b202b6SYOSHIFUJI Hideaki spin_unlock(&br->multicast_lock); 240508b202b6SYOSHIFUJI Hideaki return err; 240608b202b6SYOSHIFUJI Hideaki } 240708b202b6SYOSHIFUJI Hideaki #endif 240808b202b6SYOSHIFUJI Hideaki 240990010b36SLinus Lüssing static void 241090010b36SLinus Lüssing br_multicast_leave_group(struct net_bridge *br, 2411eb1d1641SHerbert Xu struct net_bridge_port *port, 2412cc0fdd80SLinus Lüssing struct br_ip *group, 241390010b36SLinus Lüssing struct bridge_mcast_other_query *other_query, 24146db6f0eaSFelix Fietkau struct bridge_mcast_own_query *own_query, 24156db6f0eaSFelix Fietkau const unsigned char *src) 2416eb1d1641SHerbert Xu { 2417eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 2418eb1d1641SHerbert Xu struct net_bridge_port_group *p; 2419eb1d1641SHerbert Xu unsigned long now; 2420eb1d1641SHerbert Xu unsigned long time; 2421eb1d1641SHerbert Xu 2422eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 2423eb1d1641SHerbert Xu if (!netif_running(br->dev) || 2424544586f7SSatish Ashok (port && port->state == BR_STATE_DISABLED)) 2425eb1d1641SHerbert Xu goto out; 2426eb1d1641SHerbert Xu 242719e3a9c9SNikolay Aleksandrov mp = br_mdb_ip_get(br, group); 2428eb1d1641SHerbert Xu if (!mp) 2429eb1d1641SHerbert Xu goto out; 2430eb1d1641SHerbert Xu 2431544586f7SSatish Ashok if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) { 2432544586f7SSatish Ashok struct net_bridge_port_group __rcu **pp; 2433544586f7SSatish Ashok 2434544586f7SSatish Ashok for (pp = &mp->ports; 2435544586f7SSatish Ashok (p = mlock_dereference(*pp, br)) != NULL; 2436544586f7SSatish Ashok pp = &p->next) { 24376db6f0eaSFelix Fietkau if (!br_port_group_equal(p, port, src)) 2438544586f7SSatish Ashok continue; 2439544586f7SSatish Ashok 24405c725b6bSNikolay Aleksandrov if (p->flags & MDB_PG_FLAGS_PERMANENT) 24415c725b6bSNikolay Aleksandrov break; 24425c725b6bSNikolay Aleksandrov 2443681590bdSNikolay Aleksandrov p->flags |= MDB_PG_FLAGS_FAST_LEAVE; 2444681590bdSNikolay Aleksandrov br_multicast_del_pg(mp, p, pp); 2445544586f7SSatish Ashok } 2446544586f7SSatish Ashok goto out; 2447544586f7SSatish Ashok } 2448544586f7SSatish Ashok 2449544586f7SSatish Ashok if (timer_pending(&other_query->timer)) 2450544586f7SSatish Ashok goto out; 2451544586f7SSatish Ashok 2452675779adSNikolay Aleksandrov if (br_opt_get(br, BROPT_MULTICAST_QUERIER)) { 2453438ef2d0SNikolay Aleksandrov __br_multicast_send_query(br, port, NULL, NULL, &mp->addr, 245442c11ccfSNikolay Aleksandrov false, 0, NULL); 24556b7df111SCong Wang 24566b7df111SCong Wang time = jiffies + br->multicast_last_member_count * 24576b7df111SCong Wang br->multicast_last_member_interval; 2458cc0fdd80SLinus Lüssing 245990010b36SLinus Lüssing mod_timer(&own_query->timer, time); 24606b7df111SCong Wang 24616b7df111SCong Wang for (p = mlock_dereference(mp->ports, br); 24626b7df111SCong Wang p != NULL; 24636b7df111SCong Wang p = mlock_dereference(p->next, br)) { 24646db6f0eaSFelix Fietkau if (!br_port_group_equal(p, port, src)) 24656b7df111SCong Wang continue; 24666b7df111SCong Wang 24676b7df111SCong Wang if (!hlist_unhashed(&p->mglist) && 24686b7df111SCong Wang (timer_pending(&p->timer) ? 24696b7df111SCong Wang time_after(p->timer.expires, time) : 24706b7df111SCong Wang try_to_del_timer_sync(&p->timer) >= 0)) { 24716b7df111SCong Wang mod_timer(&p->timer, time); 24726b7df111SCong Wang } 24736b7df111SCong Wang 24746b7df111SCong Wang break; 24756b7df111SCong Wang } 24766b7df111SCong Wang } 24776b7df111SCong Wang 2478eb1d1641SHerbert Xu now = jiffies; 2479eb1d1641SHerbert Xu time = now + br->multicast_last_member_count * 2480eb1d1641SHerbert Xu br->multicast_last_member_interval; 2481eb1d1641SHerbert Xu 2482eb1d1641SHerbert Xu if (!port) { 2483ff0fd34eSAndrew Lunn if (mp->host_joined && 2484eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 2485eb1d1641SHerbert Xu time_after(mp->timer.expires, time) : 2486eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) { 2487eb1d1641SHerbert Xu mod_timer(&mp->timer, time); 2488eb1d1641SHerbert Xu } 2489454594f3SLinus Lüssing 2490454594f3SLinus Lüssing goto out; 2491454594f3SLinus Lüssing } 2492454594f3SLinus Lüssing 2493454594f3SLinus Lüssing for (p = mlock_dereference(mp->ports, br); 2494454594f3SLinus Lüssing p != NULL; 2495454594f3SLinus Lüssing p = mlock_dereference(p->next, br)) { 2496454594f3SLinus Lüssing if (p->port != port) 2497454594f3SLinus Lüssing continue; 2498454594f3SLinus Lüssing 2499454594f3SLinus Lüssing if (!hlist_unhashed(&p->mglist) && 2500454594f3SLinus Lüssing (timer_pending(&p->timer) ? 2501454594f3SLinus Lüssing time_after(p->timer.expires, time) : 2502454594f3SLinus Lüssing try_to_del_timer_sync(&p->timer) >= 0)) { 2503454594f3SLinus Lüssing mod_timer(&p->timer, time); 2504454594f3SLinus Lüssing } 2505454594f3SLinus Lüssing 2506454594f3SLinus Lüssing break; 2507eb1d1641SHerbert Xu } 2508eb1d1641SHerbert Xu out: 2509eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 2510eb1d1641SHerbert Xu } 2511eb1d1641SHerbert Xu 25128ef2a9a5SYOSHIFUJI Hideaki static void br_ip4_multicast_leave_group(struct net_bridge *br, 25138ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 2514b0e9a30dSVlad Yasevich __be32 group, 25156db6f0eaSFelix Fietkau __u16 vid, 25166db6f0eaSFelix Fietkau const unsigned char *src) 25178ef2a9a5SYOSHIFUJI Hideaki { 25188ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 251990010b36SLinus Lüssing struct bridge_mcast_own_query *own_query; 25208ef2a9a5SYOSHIFUJI Hideaki 25218ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 25228ef2a9a5SYOSHIFUJI Hideaki return; 25238ef2a9a5SYOSHIFUJI Hideaki 252490010b36SLinus Lüssing own_query = port ? &port->ip4_own_query : &br->ip4_own_query; 252590010b36SLinus Lüssing 25261515a63fSNikolay Aleksandrov memset(&br_group, 0, sizeof(br_group)); 2527eab3227bSNikolay Aleksandrov br_group.dst.ip4 = group; 25288ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 2529b0e9a30dSVlad Yasevich br_group.vid = vid; 25308ef2a9a5SYOSHIFUJI Hideaki 253190010b36SLinus Lüssing br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query, 25326db6f0eaSFelix Fietkau own_query, src); 25338ef2a9a5SYOSHIFUJI Hideaki } 25348ef2a9a5SYOSHIFUJI Hideaki 2535dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 253608b202b6SYOSHIFUJI Hideaki static void br_ip6_multicast_leave_group(struct net_bridge *br, 253708b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 2538b0e9a30dSVlad Yasevich const struct in6_addr *group, 25396db6f0eaSFelix Fietkau __u16 vid, 25406db6f0eaSFelix Fietkau const unsigned char *src) 254108b202b6SYOSHIFUJI Hideaki { 254208b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 254390010b36SLinus Lüssing struct bridge_mcast_own_query *own_query; 254408b202b6SYOSHIFUJI Hideaki 25453c3769e6SLinus Lüssing if (ipv6_addr_is_ll_all_nodes(group)) 254608b202b6SYOSHIFUJI Hideaki return; 254708b202b6SYOSHIFUJI Hideaki 254890010b36SLinus Lüssing own_query = port ? &port->ip6_own_query : &br->ip6_own_query; 254990010b36SLinus Lüssing 25501515a63fSNikolay Aleksandrov memset(&br_group, 0, sizeof(br_group)); 2551eab3227bSNikolay Aleksandrov br_group.dst.ip6 = *group; 255208b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 2553b0e9a30dSVlad Yasevich br_group.vid = vid; 255408b202b6SYOSHIFUJI Hideaki 255590010b36SLinus Lüssing br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query, 25566db6f0eaSFelix Fietkau own_query, src); 255708b202b6SYOSHIFUJI Hideaki } 255808b202b6SYOSHIFUJI Hideaki #endif 25598ef2a9a5SYOSHIFUJI Hideaki 25601080ab95SNikolay Aleksandrov static void br_multicast_err_count(const struct net_bridge *br, 25611080ab95SNikolay Aleksandrov const struct net_bridge_port *p, 25621080ab95SNikolay Aleksandrov __be16 proto) 25631080ab95SNikolay Aleksandrov { 25641080ab95SNikolay Aleksandrov struct bridge_mcast_stats __percpu *stats; 25651080ab95SNikolay Aleksandrov struct bridge_mcast_stats *pstats; 25661080ab95SNikolay Aleksandrov 2567675779adSNikolay Aleksandrov if (!br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)) 25681080ab95SNikolay Aleksandrov return; 25691080ab95SNikolay Aleksandrov 25701080ab95SNikolay Aleksandrov if (p) 25711080ab95SNikolay Aleksandrov stats = p->mcast_stats; 25721080ab95SNikolay Aleksandrov else 25731080ab95SNikolay Aleksandrov stats = br->mcast_stats; 25741080ab95SNikolay Aleksandrov if (WARN_ON(!stats)) 25751080ab95SNikolay Aleksandrov return; 25761080ab95SNikolay Aleksandrov 25771080ab95SNikolay Aleksandrov pstats = this_cpu_ptr(stats); 25781080ab95SNikolay Aleksandrov 25791080ab95SNikolay Aleksandrov u64_stats_update_begin(&pstats->syncp); 25801080ab95SNikolay Aleksandrov switch (proto) { 25811080ab95SNikolay Aleksandrov case htons(ETH_P_IP): 25821080ab95SNikolay Aleksandrov pstats->mstats.igmp_parse_errors++; 25831080ab95SNikolay Aleksandrov break; 25841080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 25851080ab95SNikolay Aleksandrov case htons(ETH_P_IPV6): 25861080ab95SNikolay Aleksandrov pstats->mstats.mld_parse_errors++; 25871080ab95SNikolay Aleksandrov break; 25881080ab95SNikolay Aleksandrov #endif 25891080ab95SNikolay Aleksandrov } 25901080ab95SNikolay Aleksandrov u64_stats_update_end(&pstats->syncp); 25911080ab95SNikolay Aleksandrov } 25921080ab95SNikolay Aleksandrov 259391b02d3dSNikolay Aleksandrov static void br_multicast_pim(struct net_bridge *br, 259491b02d3dSNikolay Aleksandrov struct net_bridge_port *port, 259591b02d3dSNikolay Aleksandrov const struct sk_buff *skb) 259691b02d3dSNikolay Aleksandrov { 259791b02d3dSNikolay Aleksandrov unsigned int offset = skb_transport_offset(skb); 259891b02d3dSNikolay Aleksandrov struct pimhdr *pimhdr, _pimhdr; 259991b02d3dSNikolay Aleksandrov 260091b02d3dSNikolay Aleksandrov pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr); 260191b02d3dSNikolay Aleksandrov if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION || 260291b02d3dSNikolay Aleksandrov pim_hdr_type(pimhdr) != PIM_TYPE_HELLO) 260391b02d3dSNikolay Aleksandrov return; 260491b02d3dSNikolay Aleksandrov 260591b02d3dSNikolay Aleksandrov br_multicast_mark_router(br, port); 260691b02d3dSNikolay Aleksandrov } 260791b02d3dSNikolay Aleksandrov 26084b3087c7SLinus Lüssing static int br_ip4_multicast_mrd_rcv(struct net_bridge *br, 26094b3087c7SLinus Lüssing struct net_bridge_port *port, 26104b3087c7SLinus Lüssing struct sk_buff *skb) 26114b3087c7SLinus Lüssing { 26124b3087c7SLinus Lüssing if (ip_hdr(skb)->protocol != IPPROTO_IGMP || 26134b3087c7SLinus Lüssing igmp_hdr(skb)->type != IGMP_MRDISC_ADV) 26144b3087c7SLinus Lüssing return -ENOMSG; 26154b3087c7SLinus Lüssing 26164b3087c7SLinus Lüssing br_multicast_mark_router(br, port); 26174b3087c7SLinus Lüssing 26184b3087c7SLinus Lüssing return 0; 26194b3087c7SLinus Lüssing } 26204b3087c7SLinus Lüssing 2621eb1d1641SHerbert Xu static int br_multicast_ipv4_rcv(struct net_bridge *br, 2622eb1d1641SHerbert Xu struct net_bridge_port *port, 262306499098SVlad Yasevich struct sk_buff *skb, 262406499098SVlad Yasevich u16 vid) 2625eb1d1641SHerbert Xu { 26266db6f0eaSFelix Fietkau const unsigned char *src; 2627eb1d1641SHerbert Xu struct igmphdr *ih; 2628eb1d1641SHerbert Xu int err; 2629eb1d1641SHerbert Xu 2630ba5ea614SLinus Lüssing err = ip_mc_check_igmp(skb); 2631eb1d1641SHerbert Xu 26329afd85c9SLinus Lüssing if (err == -ENOMSG) { 263391b02d3dSNikolay Aleksandrov if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) { 2634bd4265feSHerbert Xu BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 263591b02d3dSNikolay Aleksandrov } else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) { 263691b02d3dSNikolay Aleksandrov if (ip_hdr(skb)->protocol == IPPROTO_PIM) 263791b02d3dSNikolay Aleksandrov br_multicast_pim(br, port, skb); 26384b3087c7SLinus Lüssing } else if (ipv4_is_all_snoopers(ip_hdr(skb)->daddr)) { 263908e71623SLi RongQing br_ip4_multicast_mrd_rcv(br, port, skb); 26404b3087c7SLinus Lüssing } 26414b3087c7SLinus Lüssing 2642eb1d1641SHerbert Xu return 0; 26439afd85c9SLinus Lüssing } else if (err < 0) { 26441080ab95SNikolay Aleksandrov br_multicast_err_count(br, port, skb->protocol); 26459afd85c9SLinus Lüssing return err; 2646bd4265feSHerbert Xu } 2647eb1d1641SHerbert Xu 26489afd85c9SLinus Lüssing ih = igmp_hdr(skb); 26496db6f0eaSFelix Fietkau src = eth_hdr(skb)->h_source; 26501080ab95SNikolay Aleksandrov BR_INPUT_SKB_CB(skb)->igmp = ih->type; 2651eb1d1641SHerbert Xu 2652eb1d1641SHerbert Xu switch (ih->type) { 2653eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_REPORT: 2654eb1d1641SHerbert Xu case IGMPV2_HOST_MEMBERSHIP_REPORT: 265562b2bcb4SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 26568b671779SNikolay Aleksandrov err = br_ip4_multicast_add_group(br, port, ih->group, vid, src, 26578b671779SNikolay Aleksandrov true); 2658eb1d1641SHerbert Xu break; 2659eb1d1641SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 2660ba5ea614SLinus Lüssing err = br_ip4_multicast_igmp3_report(br, port, skb, vid); 2661eb1d1641SHerbert Xu break; 2662eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_QUERY: 2663ba5ea614SLinus Lüssing br_ip4_multicast_query(br, port, skb, vid); 2664eb1d1641SHerbert Xu break; 2665eb1d1641SHerbert Xu case IGMP_HOST_LEAVE_MESSAGE: 26666db6f0eaSFelix Fietkau br_ip4_multicast_leave_group(br, port, ih->group, vid, src); 2667eb1d1641SHerbert Xu break; 2668eb1d1641SHerbert Xu } 2669eb1d1641SHerbert Xu 2670a65056ecSNikolay Aleksandrov br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp, 26711080ab95SNikolay Aleksandrov BR_MCAST_DIR_RX); 26721080ab95SNikolay Aleksandrov 2673eb1d1641SHerbert Xu return err; 2674eb1d1641SHerbert Xu } 2675eb1d1641SHerbert Xu 2676dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 26774b3087c7SLinus Lüssing static int br_ip6_multicast_mrd_rcv(struct net_bridge *br, 26784b3087c7SLinus Lüssing struct net_bridge_port *port, 26794b3087c7SLinus Lüssing struct sk_buff *skb) 26804b3087c7SLinus Lüssing { 26814b3087c7SLinus Lüssing int ret; 26824b3087c7SLinus Lüssing 26834b3087c7SLinus Lüssing if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6) 26844b3087c7SLinus Lüssing return -ENOMSG; 26854b3087c7SLinus Lüssing 26864b3087c7SLinus Lüssing ret = ipv6_mc_check_icmpv6(skb); 26874b3087c7SLinus Lüssing if (ret < 0) 26884b3087c7SLinus Lüssing return ret; 26894b3087c7SLinus Lüssing 26904b3087c7SLinus Lüssing if (icmp6_hdr(skb)->icmp6_type != ICMPV6_MRDISC_ADV) 26914b3087c7SLinus Lüssing return -ENOMSG; 26924b3087c7SLinus Lüssing 26934b3087c7SLinus Lüssing br_multicast_mark_router(br, port); 26944b3087c7SLinus Lüssing 26954b3087c7SLinus Lüssing return 0; 26964b3087c7SLinus Lüssing } 26974b3087c7SLinus Lüssing 269808b202b6SYOSHIFUJI Hideaki static int br_multicast_ipv6_rcv(struct net_bridge *br, 269908b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 270006499098SVlad Yasevich struct sk_buff *skb, 270106499098SVlad Yasevich u16 vid) 270208b202b6SYOSHIFUJI Hideaki { 27036db6f0eaSFelix Fietkau const unsigned char *src; 27049afd85c9SLinus Lüssing struct mld_msg *mld; 270508b202b6SYOSHIFUJI Hideaki int err; 270608b202b6SYOSHIFUJI Hideaki 2707ba5ea614SLinus Lüssing err = ipv6_mc_check_mld(skb); 270808b202b6SYOSHIFUJI Hideaki 27099afd85c9SLinus Lüssing if (err == -ENOMSG) { 27109afd85c9SLinus Lüssing if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr)) 27118fad9c39SLinus Lüssing BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 27124b3087c7SLinus Lüssing 27134b3087c7SLinus Lüssing if (ipv6_addr_is_all_snoopers(&ipv6_hdr(skb)->daddr)) { 27144b3087c7SLinus Lüssing err = br_ip6_multicast_mrd_rcv(br, port, skb); 27154b3087c7SLinus Lüssing 27164b3087c7SLinus Lüssing if (err < 0 && err != -ENOMSG) { 27174b3087c7SLinus Lüssing br_multicast_err_count(br, port, skb->protocol); 27184b3087c7SLinus Lüssing return err; 27194b3087c7SLinus Lüssing } 27204b3087c7SLinus Lüssing } 27214b3087c7SLinus Lüssing 272208b202b6SYOSHIFUJI Hideaki return 0; 27239afd85c9SLinus Lüssing } else if (err < 0) { 27241080ab95SNikolay Aleksandrov br_multicast_err_count(br, port, skb->protocol); 27259afd85c9SLinus Lüssing return err; 272608b202b6SYOSHIFUJI Hideaki } 272708b202b6SYOSHIFUJI Hideaki 27289afd85c9SLinus Lüssing mld = (struct mld_msg *)skb_transport_header(skb); 27291080ab95SNikolay Aleksandrov BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type; 273008b202b6SYOSHIFUJI Hideaki 27319afd85c9SLinus Lüssing switch (mld->mld_type) { 273208b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 27336db6f0eaSFelix Fietkau src = eth_hdr(skb)->h_source; 2734fc2af6c7SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 27356db6f0eaSFelix Fietkau err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid, 27368b671779SNikolay Aleksandrov src, true); 273708b202b6SYOSHIFUJI Hideaki break; 273808b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 2739ba5ea614SLinus Lüssing err = br_ip6_multicast_mld2_report(br, port, skb, vid); 274008b202b6SYOSHIFUJI Hideaki break; 274108b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 2742ba5ea614SLinus Lüssing err = br_ip6_multicast_query(br, port, skb, vid); 274308b202b6SYOSHIFUJI Hideaki break; 274408b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 27456db6f0eaSFelix Fietkau src = eth_hdr(skb)->h_source; 27466db6f0eaSFelix Fietkau br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src); 27479afd85c9SLinus Lüssing break; 274808b202b6SYOSHIFUJI Hideaki } 274908b202b6SYOSHIFUJI Hideaki 2750a65056ecSNikolay Aleksandrov br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp, 27511080ab95SNikolay Aleksandrov BR_MCAST_DIR_RX); 27521080ab95SNikolay Aleksandrov 275308b202b6SYOSHIFUJI Hideaki return err; 275408b202b6SYOSHIFUJI Hideaki } 275508b202b6SYOSHIFUJI Hideaki #endif 275608b202b6SYOSHIFUJI Hideaki 2757eb1d1641SHerbert Xu int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port, 275806499098SVlad Yasevich struct sk_buff *skb, u16 vid) 2759eb1d1641SHerbert Xu { 27601080ab95SNikolay Aleksandrov int ret = 0; 27611080ab95SNikolay Aleksandrov 27621fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->igmp = 0; 27631fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->mrouters_only = 0; 27641fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 276513cefad2SNikolay Aleksandrov if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) 2766eb1d1641SHerbert Xu return 0; 2767eb1d1641SHerbert Xu 2768eb1d1641SHerbert Xu switch (skb->protocol) { 2769eb1d1641SHerbert Xu case htons(ETH_P_IP): 27701080ab95SNikolay Aleksandrov ret = br_multicast_ipv4_rcv(br, port, skb, vid); 27711080ab95SNikolay Aleksandrov break; 2772dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 277308b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 27741080ab95SNikolay Aleksandrov ret = br_multicast_ipv6_rcv(br, port, skb, vid); 27751080ab95SNikolay Aleksandrov break; 277608b202b6SYOSHIFUJI Hideaki #endif 2777eb1d1641SHerbert Xu } 2778eb1d1641SHerbert Xu 27791080ab95SNikolay Aleksandrov return ret; 2780eb1d1641SHerbert Xu } 2781eb1d1641SHerbert Xu 2782cc0fdd80SLinus Lüssing static void br_multicast_query_expired(struct net_bridge *br, 27832cd41431SLinus Lüssing struct bridge_mcast_own_query *query, 27842cd41431SLinus Lüssing struct bridge_mcast_querier *querier) 2785cc0fdd80SLinus Lüssing { 2786cc0fdd80SLinus Lüssing spin_lock(&br->multicast_lock); 2787cc0fdd80SLinus Lüssing if (query->startup_sent < br->multicast_startup_query_count) 2788cc0fdd80SLinus Lüssing query->startup_sent++; 2789cc0fdd80SLinus Lüssing 279071d9f614SEric Dumazet RCU_INIT_POINTER(querier->port, NULL); 2791cc0fdd80SLinus Lüssing br_multicast_send_query(br, NULL, query); 2792cc0fdd80SLinus Lüssing spin_unlock(&br->multicast_lock); 2793cc0fdd80SLinus Lüssing } 2794cc0fdd80SLinus Lüssing 279588c1f37fSAllen Pais static void br_ip4_multicast_query_expired(struct timer_list *t) 2796eb1d1641SHerbert Xu { 279788c1f37fSAllen Pais struct net_bridge *br = from_timer(br, t, ip4_own_query.timer); 2798eb1d1641SHerbert Xu 27992cd41431SLinus Lüssing br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier); 2800eb1d1641SHerbert Xu } 2801eb1d1641SHerbert Xu 2802cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 280388c1f37fSAllen Pais static void br_ip6_multicast_query_expired(struct timer_list *t) 2804cc0fdd80SLinus Lüssing { 280588c1f37fSAllen Pais struct net_bridge *br = from_timer(br, t, ip6_own_query.timer); 2806cc0fdd80SLinus Lüssing 28072cd41431SLinus Lüssing br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier); 2808cc0fdd80SLinus Lüssing } 2809cc0fdd80SLinus Lüssing #endif 2810cc0fdd80SLinus Lüssing 2811e12cec65SNikolay Aleksandrov static void br_multicast_gc_work(struct work_struct *work) 28128b671779SNikolay Aleksandrov { 28138b671779SNikolay Aleksandrov struct net_bridge *br = container_of(work, struct net_bridge, 2814e12cec65SNikolay Aleksandrov mcast_gc_work); 28158b671779SNikolay Aleksandrov HLIST_HEAD(deleted_head); 28168b671779SNikolay Aleksandrov 28178b671779SNikolay Aleksandrov spin_lock_bh(&br->multicast_lock); 2818e12cec65SNikolay Aleksandrov hlist_move_list(&br->mcast_gc_list, &deleted_head); 28198b671779SNikolay Aleksandrov spin_unlock_bh(&br->multicast_lock); 28208b671779SNikolay Aleksandrov 2821e12cec65SNikolay Aleksandrov br_multicast_gc(&deleted_head); 28228b671779SNikolay Aleksandrov } 28238b671779SNikolay Aleksandrov 2824eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br) 2825eb1d1641SHerbert Xu { 2826d08c6bc0SNikolay Aleksandrov br->hash_max = BR_MULTICAST_DEFAULT_HASH_MAX; 2827eb1d1641SHerbert Xu 28287f0aec7aSNikolay Aleksandrov br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 2829eb1d1641SHerbert Xu br->multicast_last_member_count = 2; 2830eb1d1641SHerbert Xu br->multicast_startup_query_count = 2; 2831eb1d1641SHerbert Xu 2832eb1d1641SHerbert Xu br->multicast_last_member_interval = HZ; 2833eb1d1641SHerbert Xu br->multicast_query_response_interval = 10 * HZ; 2834eb1d1641SHerbert Xu br->multicast_startup_query_interval = 125 * HZ / 4; 2835eb1d1641SHerbert Xu br->multicast_query_interval = 125 * HZ; 2836eb1d1641SHerbert Xu br->multicast_querier_interval = 255 * HZ; 2837eb1d1641SHerbert Xu br->multicast_membership_interval = 260 * HZ; 2838eb1d1641SHerbert Xu 283990010b36SLinus Lüssing br->ip4_other_query.delay_time = 0; 28402cd41431SLinus Lüssing br->ip4_querier.port = NULL; 2841aa2ae3e7SNikolay Aleksandrov br->multicast_igmp_version = 2; 2842cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 2843aa2ae3e7SNikolay Aleksandrov br->multicast_mld_version = 1; 284490010b36SLinus Lüssing br->ip6_other_query.delay_time = 0; 28452cd41431SLinus Lüssing br->ip6_querier.port = NULL; 2846cc0fdd80SLinus Lüssing #endif 28476919622aSIdo Schimmel br_opt_toggle(br, BROPT_MULTICAST_ENABLED, true); 2848675779adSNikolay Aleksandrov br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true); 2849b00589afSLinus Lüssing 2850eb1d1641SHerbert Xu spin_lock_init(&br->multicast_lock); 285188c1f37fSAllen Pais timer_setup(&br->multicast_router_timer, 285288c1f37fSAllen Pais br_multicast_local_router_expired, 0); 285388c1f37fSAllen Pais timer_setup(&br->ip4_other_query.timer, 285488c1f37fSAllen Pais br_ip4_multicast_querier_expired, 0); 285588c1f37fSAllen Pais timer_setup(&br->ip4_own_query.timer, 285688c1f37fSAllen Pais br_ip4_multicast_query_expired, 0); 2857cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 285888c1f37fSAllen Pais timer_setup(&br->ip6_other_query.timer, 285988c1f37fSAllen Pais br_ip6_multicast_querier_expired, 0); 286088c1f37fSAllen Pais timer_setup(&br->ip6_own_query.timer, 286188c1f37fSAllen Pais br_ip6_multicast_query_expired, 0); 2862cc0fdd80SLinus Lüssing #endif 286319e3a9c9SNikolay Aleksandrov INIT_HLIST_HEAD(&br->mdb_list); 2864e12cec65SNikolay Aleksandrov INIT_HLIST_HEAD(&br->mcast_gc_list); 2865e12cec65SNikolay Aleksandrov INIT_WORK(&br->mcast_gc_work, br_multicast_gc_work); 2866eb1d1641SHerbert Xu } 2867eb1d1641SHerbert Xu 28684effd28cSLinus Lüssing static void br_ip4_multicast_join_snoopers(struct net_bridge *br) 28694effd28cSLinus Lüssing { 28704effd28cSLinus Lüssing struct in_device *in_dev = in_dev_get(br->dev); 28714effd28cSLinus Lüssing 28724effd28cSLinus Lüssing if (!in_dev) 28734effd28cSLinus Lüssing return; 28744effd28cSLinus Lüssing 28759fb20801SFlorian Fainelli __ip_mc_inc_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC); 28764effd28cSLinus Lüssing in_dev_put(in_dev); 28774effd28cSLinus Lüssing } 28784effd28cSLinus Lüssing 28794effd28cSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 28804effd28cSLinus Lüssing static void br_ip6_multicast_join_snoopers(struct net_bridge *br) 28814effd28cSLinus Lüssing { 28824effd28cSLinus Lüssing struct in6_addr addr; 28834effd28cSLinus Lüssing 28844effd28cSLinus Lüssing ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a)); 28854effd28cSLinus Lüssing ipv6_dev_mc_inc(br->dev, &addr); 28864effd28cSLinus Lüssing } 28874effd28cSLinus Lüssing #else 28884effd28cSLinus Lüssing static inline void br_ip6_multicast_join_snoopers(struct net_bridge *br) 28894effd28cSLinus Lüssing { 28904effd28cSLinus Lüssing } 28914effd28cSLinus Lüssing #endif 28924effd28cSLinus Lüssing 28934effd28cSLinus Lüssing static void br_multicast_join_snoopers(struct net_bridge *br) 28944effd28cSLinus Lüssing { 28954effd28cSLinus Lüssing br_ip4_multicast_join_snoopers(br); 28964effd28cSLinus Lüssing br_ip6_multicast_join_snoopers(br); 28974effd28cSLinus Lüssing } 28984effd28cSLinus Lüssing 28994effd28cSLinus Lüssing static void br_ip4_multicast_leave_snoopers(struct net_bridge *br) 29004effd28cSLinus Lüssing { 29014effd28cSLinus Lüssing struct in_device *in_dev = in_dev_get(br->dev); 29024effd28cSLinus Lüssing 29034effd28cSLinus Lüssing if (WARN_ON(!in_dev)) 29044effd28cSLinus Lüssing return; 29054effd28cSLinus Lüssing 29069fb20801SFlorian Fainelli __ip_mc_dec_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC); 29074effd28cSLinus Lüssing in_dev_put(in_dev); 29084effd28cSLinus Lüssing } 29094effd28cSLinus Lüssing 29104effd28cSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 29114effd28cSLinus Lüssing static void br_ip6_multicast_leave_snoopers(struct net_bridge *br) 29124effd28cSLinus Lüssing { 29134effd28cSLinus Lüssing struct in6_addr addr; 29144effd28cSLinus Lüssing 29154effd28cSLinus Lüssing ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a)); 29164effd28cSLinus Lüssing ipv6_dev_mc_dec(br->dev, &addr); 29174effd28cSLinus Lüssing } 29184effd28cSLinus Lüssing #else 29194effd28cSLinus Lüssing static inline void br_ip6_multicast_leave_snoopers(struct net_bridge *br) 29204effd28cSLinus Lüssing { 29214effd28cSLinus Lüssing } 29224effd28cSLinus Lüssing #endif 29234effd28cSLinus Lüssing 29244effd28cSLinus Lüssing static void br_multicast_leave_snoopers(struct net_bridge *br) 29254effd28cSLinus Lüssing { 29264effd28cSLinus Lüssing br_ip4_multicast_leave_snoopers(br); 29274effd28cSLinus Lüssing br_ip6_multicast_leave_snoopers(br); 29284effd28cSLinus Lüssing } 29294effd28cSLinus Lüssing 2930cc0fdd80SLinus Lüssing static void __br_multicast_open(struct net_bridge *br, 293190010b36SLinus Lüssing struct bridge_mcast_own_query *query) 2932eb1d1641SHerbert Xu { 2933cc0fdd80SLinus Lüssing query->startup_sent = 0; 2934eb1d1641SHerbert Xu 293513cefad2SNikolay Aleksandrov if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) 2936eb1d1641SHerbert Xu return; 2937eb1d1641SHerbert Xu 2938cc0fdd80SLinus Lüssing mod_timer(&query->timer, jiffies); 2939cc0fdd80SLinus Lüssing } 2940cc0fdd80SLinus Lüssing 2941cc0fdd80SLinus Lüssing void br_multicast_open(struct net_bridge *br) 2942cc0fdd80SLinus Lüssing { 29434effd28cSLinus Lüssing if (br_opt_get(br, BROPT_MULTICAST_ENABLED)) 29444effd28cSLinus Lüssing br_multicast_join_snoopers(br); 29454effd28cSLinus Lüssing 294690010b36SLinus Lüssing __br_multicast_open(br, &br->ip4_own_query); 2947cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 294890010b36SLinus Lüssing __br_multicast_open(br, &br->ip6_own_query); 2949cc0fdd80SLinus Lüssing #endif 2950eb1d1641SHerbert Xu } 2951eb1d1641SHerbert Xu 2952eb1d1641SHerbert Xu void br_multicast_stop(struct net_bridge *br) 2953eb1d1641SHerbert Xu { 2954eb1d1641SHerbert Xu del_timer_sync(&br->multicast_router_timer); 295590010b36SLinus Lüssing del_timer_sync(&br->ip4_other_query.timer); 295690010b36SLinus Lüssing del_timer_sync(&br->ip4_own_query.timer); 2957cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 295890010b36SLinus Lüssing del_timer_sync(&br->ip6_other_query.timer); 295990010b36SLinus Lüssing del_timer_sync(&br->ip6_own_query.timer); 2960cc0fdd80SLinus Lüssing #endif 29614effd28cSLinus Lüssing 29624effd28cSLinus Lüssing if (br_opt_get(br, BROPT_MULTICAST_ENABLED)) 29634effd28cSLinus Lüssing br_multicast_leave_snoopers(br); 2964e10177abSSatish Ashok } 2965e10177abSSatish Ashok 2966e10177abSSatish Ashok void br_multicast_dev_del(struct net_bridge *br) 2967e10177abSSatish Ashok { 2968e10177abSSatish Ashok struct net_bridge_mdb_entry *mp; 29698b671779SNikolay Aleksandrov HLIST_HEAD(deleted_head); 297019e3a9c9SNikolay Aleksandrov struct hlist_node *tmp; 2971eb1d1641SHerbert Xu 2972eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 2973e12cec65SNikolay Aleksandrov hlist_for_each_entry_safe(mp, tmp, &br->mdb_list, mdb_node) 2974e12cec65SNikolay Aleksandrov br_multicast_del_mdb_entry(mp); 2975e12cec65SNikolay Aleksandrov hlist_move_list(&br->mcast_gc_list, &deleted_head); 2976eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 297719e3a9c9SNikolay Aleksandrov 2978e12cec65SNikolay Aleksandrov br_multicast_gc(&deleted_head); 2979e12cec65SNikolay Aleksandrov cancel_work_sync(&br->mcast_gc_work); 29808b671779SNikolay Aleksandrov 29814329596cSNikolay Aleksandrov rcu_barrier(); 2982eb1d1641SHerbert Xu } 29830909e117SHerbert Xu 29840909e117SHerbert Xu int br_multicast_set_router(struct net_bridge *br, unsigned long val) 29850909e117SHerbert Xu { 29866ae4ae8eSLinus Lüssing int err = -EINVAL; 29870909e117SHerbert Xu 29880909e117SHerbert Xu spin_lock_bh(&br->multicast_lock); 29890909e117SHerbert Xu 29900909e117SHerbert Xu switch (val) { 29917f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_DISABLED: 29927f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_PERM: 299377041420SYotam Gigi br_mc_router_state_change(br, val == MDB_RTR_TYPE_PERM); 29940909e117SHerbert Xu del_timer(&br->multicast_router_timer); 299577041420SYotam Gigi br->multicast_router = val; 299677041420SYotam Gigi err = 0; 299777041420SYotam Gigi break; 29987f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_TEMP_QUERY: 299977041420SYotam Gigi if (br->multicast_router != MDB_RTR_TYPE_TEMP_QUERY) 300077041420SYotam Gigi br_mc_router_state_change(br, false); 30010909e117SHerbert Xu br->multicast_router = val; 30020909e117SHerbert Xu err = 0; 30030909e117SHerbert Xu break; 30040909e117SHerbert Xu } 30050909e117SHerbert Xu 30060909e117SHerbert Xu spin_unlock_bh(&br->multicast_lock); 30070909e117SHerbert Xu 30080909e117SHerbert Xu return err; 30090909e117SHerbert Xu } 30100909e117SHerbert Xu 30117f0aec7aSNikolay Aleksandrov static void __del_port_router(struct net_bridge_port *p) 30127f0aec7aSNikolay Aleksandrov { 30137f0aec7aSNikolay Aleksandrov if (hlist_unhashed(&p->rlist)) 30147f0aec7aSNikolay Aleksandrov return; 30157f0aec7aSNikolay Aleksandrov hlist_del_init_rcu(&p->rlist); 30167f0aec7aSNikolay Aleksandrov br_rtr_notify(p->br->dev, p, RTM_DELMDB); 30176d549648SNogah Frankel br_port_mc_router_state_change(p, false); 3018f12e7d95SNogah Frankel 3019f12e7d95SNogah Frankel /* don't allow timer refresh */ 3020f12e7d95SNogah Frankel if (p->multicast_router == MDB_RTR_TYPE_TEMP) 3021f12e7d95SNogah Frankel p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 30227f0aec7aSNikolay Aleksandrov } 30237f0aec7aSNikolay Aleksandrov 30240909e117SHerbert Xu int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val) 30250909e117SHerbert Xu { 30260909e117SHerbert Xu struct net_bridge *br = p->br; 3027a55d8246SNikolay Aleksandrov unsigned long now = jiffies; 30286ae4ae8eSLinus Lüssing int err = -EINVAL; 30290909e117SHerbert Xu 30300909e117SHerbert Xu spin_lock(&br->multicast_lock); 30314950cfd1SNikolay Aleksandrov if (p->multicast_router == val) { 3032a55d8246SNikolay Aleksandrov /* Refresh the temp router port timer */ 3033a55d8246SNikolay Aleksandrov if (p->multicast_router == MDB_RTR_TYPE_TEMP) 3034a55d8246SNikolay Aleksandrov mod_timer(&p->multicast_router_timer, 3035a55d8246SNikolay Aleksandrov now + br->multicast_querier_interval); 30364950cfd1SNikolay Aleksandrov err = 0; 30374950cfd1SNikolay Aleksandrov goto unlock; 30384950cfd1SNikolay Aleksandrov } 30390909e117SHerbert Xu switch (val) { 30407f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_DISABLED: 30417f0aec7aSNikolay Aleksandrov p->multicast_router = MDB_RTR_TYPE_DISABLED; 30427f0aec7aSNikolay Aleksandrov __del_port_router(p); 30430909e117SHerbert Xu del_timer(&p->multicast_router_timer); 30440909e117SHerbert Xu break; 30457f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_TEMP_QUERY: 30467f0aec7aSNikolay Aleksandrov p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 30477f0aec7aSNikolay Aleksandrov __del_port_router(p); 30487f0aec7aSNikolay Aleksandrov break; 30497f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_PERM: 30507f0aec7aSNikolay Aleksandrov p->multicast_router = MDB_RTR_TYPE_PERM; 30517f0aec7aSNikolay Aleksandrov del_timer(&p->multicast_router_timer); 30520909e117SHerbert Xu br_multicast_add_router(br, p); 30530909e117SHerbert Xu break; 3054a55d8246SNikolay Aleksandrov case MDB_RTR_TYPE_TEMP: 3055a55d8246SNikolay Aleksandrov p->multicast_router = MDB_RTR_TYPE_TEMP; 3056a55d8246SNikolay Aleksandrov br_multicast_mark_router(br, p); 3057a55d8246SNikolay Aleksandrov break; 30587f0aec7aSNikolay Aleksandrov default: 30597f0aec7aSNikolay Aleksandrov goto unlock; 30600909e117SHerbert Xu } 30617f0aec7aSNikolay Aleksandrov err = 0; 30627f0aec7aSNikolay Aleksandrov unlock: 30630909e117SHerbert Xu spin_unlock(&br->multicast_lock); 30640909e117SHerbert Xu 30650909e117SHerbert Xu return err; 30660909e117SHerbert Xu } 3067561f1103SHerbert Xu 3068cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br, 306990010b36SLinus Lüssing struct bridge_mcast_own_query *query) 3070561f1103SHerbert Xu { 3071561f1103SHerbert Xu struct net_bridge_port *port; 307274857216SHerbert Xu 3073cc0fdd80SLinus Lüssing __br_multicast_open(br, query); 307474857216SHerbert Xu 3075c5b493ceSNikolay Aleksandrov rcu_read_lock(); 3076c5b493ceSNikolay Aleksandrov list_for_each_entry_rcu(port, &br->port_list, list) { 307774857216SHerbert Xu if (port->state == BR_STATE_DISABLED || 307874857216SHerbert Xu port->state == BR_STATE_BLOCKING) 307974857216SHerbert Xu continue; 308074857216SHerbert Xu 308190010b36SLinus Lüssing if (query == &br->ip4_own_query) 308290010b36SLinus Lüssing br_multicast_enable(&port->ip4_own_query); 3083cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 3084cc0fdd80SLinus Lüssing else 308590010b36SLinus Lüssing br_multicast_enable(&port->ip6_own_query); 3086cc0fdd80SLinus Lüssing #endif 308774857216SHerbert Xu } 3088c5b493ceSNikolay Aleksandrov rcu_read_unlock(); 308974857216SHerbert Xu } 309074857216SHerbert Xu 309174857216SHerbert Xu int br_multicast_toggle(struct net_bridge *br, unsigned long val) 309274857216SHerbert Xu { 30937cb3f921SNikolay Aleksandrov struct net_bridge_port *port; 3094561f1103SHerbert Xu 3095ef5e0d82SAndrey Vagin spin_lock_bh(&br->multicast_lock); 309613cefad2SNikolay Aleksandrov if (!!br_opt_get(br, BROPT_MULTICAST_ENABLED) == !!val) 3097561f1103SHerbert Xu goto unlock; 3098561f1103SHerbert Xu 309913cefad2SNikolay Aleksandrov br_mc_disabled_update(br->dev, val); 310013cefad2SNikolay Aleksandrov br_opt_toggle(br, BROPT_MULTICAST_ENABLED, !!val); 31014effd28cSLinus Lüssing if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) { 31024effd28cSLinus Lüssing br_multicast_leave_snoopers(br); 3103561f1103SHerbert Xu goto unlock; 31044effd28cSLinus Lüssing } 3105561f1103SHerbert Xu 31063a7fda06SHerbert Xu if (!netif_running(br->dev)) 31073a7fda06SHerbert Xu goto unlock; 31083a7fda06SHerbert Xu 31097cb3f921SNikolay Aleksandrov br_multicast_open(br); 31107cb3f921SNikolay Aleksandrov list_for_each_entry(port, &br->port_list, list) 31117cb3f921SNikolay Aleksandrov __br_multicast_enable_port(port); 3112561f1103SHerbert Xu 3113561f1103SHerbert Xu unlock: 3114ef5e0d82SAndrey Vagin spin_unlock_bh(&br->multicast_lock); 3115561f1103SHerbert Xu 3116a26d94bfSYueHaibing return 0; 3117561f1103SHerbert Xu } 3118b195167fSHerbert Xu 31199341b988SIdo Schimmel bool br_multicast_enabled(const struct net_device *dev) 31209341b988SIdo Schimmel { 31219341b988SIdo Schimmel struct net_bridge *br = netdev_priv(dev); 31229341b988SIdo Schimmel 312313cefad2SNikolay Aleksandrov return !!br_opt_get(br, BROPT_MULTICAST_ENABLED); 31249341b988SIdo Schimmel } 31259341b988SIdo Schimmel EXPORT_SYMBOL_GPL(br_multicast_enabled); 31269341b988SIdo Schimmel 31270912bda4SYotam Gigi bool br_multicast_router(const struct net_device *dev) 31280912bda4SYotam Gigi { 31290912bda4SYotam Gigi struct net_bridge *br = netdev_priv(dev); 31300912bda4SYotam Gigi bool is_router; 31310912bda4SYotam Gigi 31320912bda4SYotam Gigi spin_lock_bh(&br->multicast_lock); 31330912bda4SYotam Gigi is_router = br_multicast_is_router(br); 31340912bda4SYotam Gigi spin_unlock_bh(&br->multicast_lock); 31350912bda4SYotam Gigi return is_router; 31360912bda4SYotam Gigi } 31370912bda4SYotam Gigi EXPORT_SYMBOL_GPL(br_multicast_router); 31380912bda4SYotam Gigi 3139c5c23260SHerbert Xu int br_multicast_set_querier(struct net_bridge *br, unsigned long val) 3140c5c23260SHerbert Xu { 3141b00589afSLinus Lüssing unsigned long max_delay; 3142b00589afSLinus Lüssing 3143c5c23260SHerbert Xu val = !!val; 3144c5c23260SHerbert Xu 3145c5c23260SHerbert Xu spin_lock_bh(&br->multicast_lock); 3146675779adSNikolay Aleksandrov if (br_opt_get(br, BROPT_MULTICAST_QUERIER) == val) 3147c5c23260SHerbert Xu goto unlock; 3148c5c23260SHerbert Xu 3149675779adSNikolay Aleksandrov br_opt_toggle(br, BROPT_MULTICAST_QUERIER, !!val); 3150b00589afSLinus Lüssing if (!val) 3151b00589afSLinus Lüssing goto unlock; 3152b00589afSLinus Lüssing 3153b00589afSLinus Lüssing max_delay = br->multicast_query_response_interval; 3154b00589afSLinus Lüssing 315590010b36SLinus Lüssing if (!timer_pending(&br->ip4_other_query.timer)) 315690010b36SLinus Lüssing br->ip4_other_query.delay_time = jiffies + max_delay; 3157cc0fdd80SLinus Lüssing 315890010b36SLinus Lüssing br_multicast_start_querier(br, &br->ip4_own_query); 3159cc0fdd80SLinus Lüssing 3160cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 316190010b36SLinus Lüssing if (!timer_pending(&br->ip6_other_query.timer)) 316290010b36SLinus Lüssing br->ip6_other_query.delay_time = jiffies + max_delay; 3163cc0fdd80SLinus Lüssing 316490010b36SLinus Lüssing br_multicast_start_querier(br, &br->ip6_own_query); 3165cc0fdd80SLinus Lüssing #endif 3166c5c23260SHerbert Xu 3167c5c23260SHerbert Xu unlock: 3168c5c23260SHerbert Xu spin_unlock_bh(&br->multicast_lock); 3169c5c23260SHerbert Xu 3170c5c23260SHerbert Xu return 0; 3171c5c23260SHerbert Xu } 3172c5c23260SHerbert Xu 31735e923585SNikolay Aleksandrov int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val) 31745e923585SNikolay Aleksandrov { 31755e923585SNikolay Aleksandrov /* Currently we support only version 2 and 3 */ 31765e923585SNikolay Aleksandrov switch (val) { 31775e923585SNikolay Aleksandrov case 2: 31785e923585SNikolay Aleksandrov case 3: 31795e923585SNikolay Aleksandrov break; 31805e923585SNikolay Aleksandrov default: 31815e923585SNikolay Aleksandrov return -EINVAL; 31825e923585SNikolay Aleksandrov } 31835e923585SNikolay Aleksandrov 31845e923585SNikolay Aleksandrov spin_lock_bh(&br->multicast_lock); 31855e923585SNikolay Aleksandrov br->multicast_igmp_version = val; 31865e923585SNikolay Aleksandrov spin_unlock_bh(&br->multicast_lock); 31875e923585SNikolay Aleksandrov 31885e923585SNikolay Aleksandrov return 0; 31895e923585SNikolay Aleksandrov } 31905e923585SNikolay Aleksandrov 3191aa2ae3e7SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 3192aa2ae3e7SNikolay Aleksandrov int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val) 3193aa2ae3e7SNikolay Aleksandrov { 3194aa2ae3e7SNikolay Aleksandrov /* Currently we support version 1 and 2 */ 3195aa2ae3e7SNikolay Aleksandrov switch (val) { 3196aa2ae3e7SNikolay Aleksandrov case 1: 3197aa2ae3e7SNikolay Aleksandrov case 2: 3198aa2ae3e7SNikolay Aleksandrov break; 3199aa2ae3e7SNikolay Aleksandrov default: 3200aa2ae3e7SNikolay Aleksandrov return -EINVAL; 3201aa2ae3e7SNikolay Aleksandrov } 3202aa2ae3e7SNikolay Aleksandrov 3203aa2ae3e7SNikolay Aleksandrov spin_lock_bh(&br->multicast_lock); 3204aa2ae3e7SNikolay Aleksandrov br->multicast_mld_version = val; 3205aa2ae3e7SNikolay Aleksandrov spin_unlock_bh(&br->multicast_lock); 3206aa2ae3e7SNikolay Aleksandrov 3207aa2ae3e7SNikolay Aleksandrov return 0; 3208aa2ae3e7SNikolay Aleksandrov } 3209aa2ae3e7SNikolay Aleksandrov #endif 3210aa2ae3e7SNikolay Aleksandrov 321107f8ac4aSLinus Lüssing /** 321207f8ac4aSLinus Lüssing * br_multicast_list_adjacent - Returns snooped multicast addresses 321307f8ac4aSLinus Lüssing * @dev: The bridge port adjacent to which to retrieve addresses 321407f8ac4aSLinus Lüssing * @br_ip_list: The list to store found, snooped multicast IP addresses in 321507f8ac4aSLinus Lüssing * 321607f8ac4aSLinus Lüssing * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast 321707f8ac4aSLinus Lüssing * snooping feature on all bridge ports of dev's bridge device, excluding 321807f8ac4aSLinus Lüssing * the addresses from dev itself. 321907f8ac4aSLinus Lüssing * 322007f8ac4aSLinus Lüssing * Returns the number of items added to br_ip_list. 322107f8ac4aSLinus Lüssing * 322207f8ac4aSLinus Lüssing * Notes: 322307f8ac4aSLinus Lüssing * - br_ip_list needs to be initialized by caller 322407f8ac4aSLinus Lüssing * - br_ip_list might contain duplicates in the end 322507f8ac4aSLinus Lüssing * (needs to be taken care of by caller) 322607f8ac4aSLinus Lüssing * - br_ip_list needs to be freed by caller 322707f8ac4aSLinus Lüssing */ 322807f8ac4aSLinus Lüssing int br_multicast_list_adjacent(struct net_device *dev, 322907f8ac4aSLinus Lüssing struct list_head *br_ip_list) 323007f8ac4aSLinus Lüssing { 323107f8ac4aSLinus Lüssing struct net_bridge *br; 323207f8ac4aSLinus Lüssing struct net_bridge_port *port; 323307f8ac4aSLinus Lüssing struct net_bridge_port_group *group; 323407f8ac4aSLinus Lüssing struct br_ip_list *entry; 323507f8ac4aSLinus Lüssing int count = 0; 323607f8ac4aSLinus Lüssing 323707f8ac4aSLinus Lüssing rcu_read_lock(); 323835f861e3SJulian Wiedmann if (!br_ip_list || !netif_is_bridge_port(dev)) 323907f8ac4aSLinus Lüssing goto unlock; 324007f8ac4aSLinus Lüssing 324107f8ac4aSLinus Lüssing port = br_port_get_rcu(dev); 324207f8ac4aSLinus Lüssing if (!port || !port->br) 324307f8ac4aSLinus Lüssing goto unlock; 324407f8ac4aSLinus Lüssing 324507f8ac4aSLinus Lüssing br = port->br; 324607f8ac4aSLinus Lüssing 324707f8ac4aSLinus Lüssing list_for_each_entry_rcu(port, &br->port_list, list) { 324807f8ac4aSLinus Lüssing if (!port->dev || port->dev == dev) 324907f8ac4aSLinus Lüssing continue; 325007f8ac4aSLinus Lüssing 325107f8ac4aSLinus Lüssing hlist_for_each_entry_rcu(group, &port->mglist, mglist) { 325207f8ac4aSLinus Lüssing entry = kmalloc(sizeof(*entry), GFP_ATOMIC); 325307f8ac4aSLinus Lüssing if (!entry) 325407f8ac4aSLinus Lüssing goto unlock; 325507f8ac4aSLinus Lüssing 325607f8ac4aSLinus Lüssing entry->addr = group->addr; 325707f8ac4aSLinus Lüssing list_add(&entry->list, br_ip_list); 325807f8ac4aSLinus Lüssing count++; 325907f8ac4aSLinus Lüssing } 326007f8ac4aSLinus Lüssing } 326107f8ac4aSLinus Lüssing 326207f8ac4aSLinus Lüssing unlock: 326307f8ac4aSLinus Lüssing rcu_read_unlock(); 326407f8ac4aSLinus Lüssing return count; 326507f8ac4aSLinus Lüssing } 326607f8ac4aSLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_list_adjacent); 32672cd41431SLinus Lüssing 32682cd41431SLinus Lüssing /** 3269c34963e2SLinus Lüssing * br_multicast_has_querier_anywhere - Checks for a querier on a bridge 3270c34963e2SLinus Lüssing * @dev: The bridge port providing the bridge on which to check for a querier 3271c34963e2SLinus Lüssing * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 3272c34963e2SLinus Lüssing * 3273c34963e2SLinus Lüssing * Checks whether the given interface has a bridge on top and if so returns 3274c34963e2SLinus Lüssing * true if a valid querier exists anywhere on the bridged link layer. 3275c34963e2SLinus Lüssing * Otherwise returns false. 3276c34963e2SLinus Lüssing */ 3277c34963e2SLinus Lüssing bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto) 3278c34963e2SLinus Lüssing { 3279c34963e2SLinus Lüssing struct net_bridge *br; 3280c34963e2SLinus Lüssing struct net_bridge_port *port; 3281c34963e2SLinus Lüssing struct ethhdr eth; 3282c34963e2SLinus Lüssing bool ret = false; 3283c34963e2SLinus Lüssing 3284c34963e2SLinus Lüssing rcu_read_lock(); 328535f861e3SJulian Wiedmann if (!netif_is_bridge_port(dev)) 3286c34963e2SLinus Lüssing goto unlock; 3287c34963e2SLinus Lüssing 3288c34963e2SLinus Lüssing port = br_port_get_rcu(dev); 3289c34963e2SLinus Lüssing if (!port || !port->br) 3290c34963e2SLinus Lüssing goto unlock; 3291c34963e2SLinus Lüssing 3292c34963e2SLinus Lüssing br = port->br; 3293c34963e2SLinus Lüssing 3294c34963e2SLinus Lüssing memset(ð, 0, sizeof(eth)); 3295c34963e2SLinus Lüssing eth.h_proto = htons(proto); 3296c34963e2SLinus Lüssing 3297c34963e2SLinus Lüssing ret = br_multicast_querier_exists(br, ð); 3298c34963e2SLinus Lüssing 3299c34963e2SLinus Lüssing unlock: 3300c34963e2SLinus Lüssing rcu_read_unlock(); 3301c34963e2SLinus Lüssing return ret; 3302c34963e2SLinus Lüssing } 3303c34963e2SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere); 3304c34963e2SLinus Lüssing 3305c34963e2SLinus Lüssing /** 33062cd41431SLinus Lüssing * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port 33072cd41431SLinus Lüssing * @dev: The bridge port adjacent to which to check for a querier 33082cd41431SLinus Lüssing * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 33092cd41431SLinus Lüssing * 33102cd41431SLinus Lüssing * Checks whether the given interface has a bridge on top and if so returns 33112cd41431SLinus Lüssing * true if a selected querier is behind one of the other ports of this 33122cd41431SLinus Lüssing * bridge. Otherwise returns false. 33132cd41431SLinus Lüssing */ 33142cd41431SLinus Lüssing bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto) 33152cd41431SLinus Lüssing { 33162cd41431SLinus Lüssing struct net_bridge *br; 33172cd41431SLinus Lüssing struct net_bridge_port *port; 33182cd41431SLinus Lüssing bool ret = false; 33192cd41431SLinus Lüssing 33202cd41431SLinus Lüssing rcu_read_lock(); 332135f861e3SJulian Wiedmann if (!netif_is_bridge_port(dev)) 33222cd41431SLinus Lüssing goto unlock; 33232cd41431SLinus Lüssing 33242cd41431SLinus Lüssing port = br_port_get_rcu(dev); 33252cd41431SLinus Lüssing if (!port || !port->br) 33262cd41431SLinus Lüssing goto unlock; 33272cd41431SLinus Lüssing 33282cd41431SLinus Lüssing br = port->br; 33292cd41431SLinus Lüssing 33302cd41431SLinus Lüssing switch (proto) { 33312cd41431SLinus Lüssing case ETH_P_IP: 33322cd41431SLinus Lüssing if (!timer_pending(&br->ip4_other_query.timer) || 33332cd41431SLinus Lüssing rcu_dereference(br->ip4_querier.port) == port) 33342cd41431SLinus Lüssing goto unlock; 33352cd41431SLinus Lüssing break; 33363993c4e1SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 33372cd41431SLinus Lüssing case ETH_P_IPV6: 33382cd41431SLinus Lüssing if (!timer_pending(&br->ip6_other_query.timer) || 33392cd41431SLinus Lüssing rcu_dereference(br->ip6_querier.port) == port) 33402cd41431SLinus Lüssing goto unlock; 33412cd41431SLinus Lüssing break; 33423993c4e1SLinus Lüssing #endif 33432cd41431SLinus Lüssing default: 33442cd41431SLinus Lüssing goto unlock; 33452cd41431SLinus Lüssing } 33462cd41431SLinus Lüssing 33472cd41431SLinus Lüssing ret = true; 33482cd41431SLinus Lüssing unlock: 33492cd41431SLinus Lüssing rcu_read_unlock(); 33502cd41431SLinus Lüssing return ret; 33512cd41431SLinus Lüssing } 33522cd41431SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent); 33531080ab95SNikolay Aleksandrov 33541080ab95SNikolay Aleksandrov static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats, 3355a65056ecSNikolay Aleksandrov const struct sk_buff *skb, u8 type, u8 dir) 33561080ab95SNikolay Aleksandrov { 33571080ab95SNikolay Aleksandrov struct bridge_mcast_stats *pstats = this_cpu_ptr(stats); 3358a65056ecSNikolay Aleksandrov __be16 proto = skb->protocol; 3359a65056ecSNikolay Aleksandrov unsigned int t_len; 33601080ab95SNikolay Aleksandrov 33611080ab95SNikolay Aleksandrov u64_stats_update_begin(&pstats->syncp); 33621080ab95SNikolay Aleksandrov switch (proto) { 33631080ab95SNikolay Aleksandrov case htons(ETH_P_IP): 3364a65056ecSNikolay Aleksandrov t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb); 33651080ab95SNikolay Aleksandrov switch (type) { 33661080ab95SNikolay Aleksandrov case IGMP_HOST_MEMBERSHIP_REPORT: 33671080ab95SNikolay Aleksandrov pstats->mstats.igmp_v1reports[dir]++; 33681080ab95SNikolay Aleksandrov break; 33691080ab95SNikolay Aleksandrov case IGMPV2_HOST_MEMBERSHIP_REPORT: 33701080ab95SNikolay Aleksandrov pstats->mstats.igmp_v2reports[dir]++; 33711080ab95SNikolay Aleksandrov break; 33721080ab95SNikolay Aleksandrov case IGMPV3_HOST_MEMBERSHIP_REPORT: 33731080ab95SNikolay Aleksandrov pstats->mstats.igmp_v3reports[dir]++; 33741080ab95SNikolay Aleksandrov break; 33751080ab95SNikolay Aleksandrov case IGMP_HOST_MEMBERSHIP_QUERY: 3376a65056ecSNikolay Aleksandrov if (t_len != sizeof(struct igmphdr)) { 3377a65056ecSNikolay Aleksandrov pstats->mstats.igmp_v3queries[dir]++; 3378a65056ecSNikolay Aleksandrov } else { 3379a65056ecSNikolay Aleksandrov unsigned int offset = skb_transport_offset(skb); 3380a65056ecSNikolay Aleksandrov struct igmphdr *ih, _ihdr; 3381a65056ecSNikolay Aleksandrov 3382a65056ecSNikolay Aleksandrov ih = skb_header_pointer(skb, offset, 3383a65056ecSNikolay Aleksandrov sizeof(_ihdr), &_ihdr); 3384a65056ecSNikolay Aleksandrov if (!ih) 3385a65056ecSNikolay Aleksandrov break; 3386a65056ecSNikolay Aleksandrov if (!ih->code) 3387a65056ecSNikolay Aleksandrov pstats->mstats.igmp_v1queries[dir]++; 3388a65056ecSNikolay Aleksandrov else 3389a65056ecSNikolay Aleksandrov pstats->mstats.igmp_v2queries[dir]++; 3390a65056ecSNikolay Aleksandrov } 33911080ab95SNikolay Aleksandrov break; 33921080ab95SNikolay Aleksandrov case IGMP_HOST_LEAVE_MESSAGE: 33931080ab95SNikolay Aleksandrov pstats->mstats.igmp_leaves[dir]++; 33941080ab95SNikolay Aleksandrov break; 33951080ab95SNikolay Aleksandrov } 33961080ab95SNikolay Aleksandrov break; 33971080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 33981080ab95SNikolay Aleksandrov case htons(ETH_P_IPV6): 3399a65056ecSNikolay Aleksandrov t_len = ntohs(ipv6_hdr(skb)->payload_len) + 3400a65056ecSNikolay Aleksandrov sizeof(struct ipv6hdr); 3401a65056ecSNikolay Aleksandrov t_len -= skb_network_header_len(skb); 34021080ab95SNikolay Aleksandrov switch (type) { 34031080ab95SNikolay Aleksandrov case ICMPV6_MGM_REPORT: 34041080ab95SNikolay Aleksandrov pstats->mstats.mld_v1reports[dir]++; 34051080ab95SNikolay Aleksandrov break; 34061080ab95SNikolay Aleksandrov case ICMPV6_MLD2_REPORT: 34071080ab95SNikolay Aleksandrov pstats->mstats.mld_v2reports[dir]++; 34081080ab95SNikolay Aleksandrov break; 34091080ab95SNikolay Aleksandrov case ICMPV6_MGM_QUERY: 3410a65056ecSNikolay Aleksandrov if (t_len != sizeof(struct mld_msg)) 3411a65056ecSNikolay Aleksandrov pstats->mstats.mld_v2queries[dir]++; 3412a65056ecSNikolay Aleksandrov else 3413a65056ecSNikolay Aleksandrov pstats->mstats.mld_v1queries[dir]++; 34141080ab95SNikolay Aleksandrov break; 34151080ab95SNikolay Aleksandrov case ICMPV6_MGM_REDUCTION: 34161080ab95SNikolay Aleksandrov pstats->mstats.mld_leaves[dir]++; 34171080ab95SNikolay Aleksandrov break; 34181080ab95SNikolay Aleksandrov } 34191080ab95SNikolay Aleksandrov break; 34201080ab95SNikolay Aleksandrov #endif /* CONFIG_IPV6 */ 34211080ab95SNikolay Aleksandrov } 34221080ab95SNikolay Aleksandrov u64_stats_update_end(&pstats->syncp); 34231080ab95SNikolay Aleksandrov } 34241080ab95SNikolay Aleksandrov 34251080ab95SNikolay Aleksandrov void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p, 3426a65056ecSNikolay Aleksandrov const struct sk_buff *skb, u8 type, u8 dir) 34271080ab95SNikolay Aleksandrov { 34281080ab95SNikolay Aleksandrov struct bridge_mcast_stats __percpu *stats; 34291080ab95SNikolay Aleksandrov 34301080ab95SNikolay Aleksandrov /* if multicast_disabled is true then igmp type can't be set */ 3431675779adSNikolay Aleksandrov if (!type || !br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)) 34321080ab95SNikolay Aleksandrov return; 34331080ab95SNikolay Aleksandrov 34341080ab95SNikolay Aleksandrov if (p) 34351080ab95SNikolay Aleksandrov stats = p->mcast_stats; 34361080ab95SNikolay Aleksandrov else 34371080ab95SNikolay Aleksandrov stats = br->mcast_stats; 34381080ab95SNikolay Aleksandrov if (WARN_ON(!stats)) 34391080ab95SNikolay Aleksandrov return; 34401080ab95SNikolay Aleksandrov 3441a65056ecSNikolay Aleksandrov br_mcast_stats_add(stats, skb, type, dir); 34421080ab95SNikolay Aleksandrov } 34431080ab95SNikolay Aleksandrov 34441080ab95SNikolay Aleksandrov int br_multicast_init_stats(struct net_bridge *br) 34451080ab95SNikolay Aleksandrov { 34461080ab95SNikolay Aleksandrov br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats); 34471080ab95SNikolay Aleksandrov if (!br->mcast_stats) 34481080ab95SNikolay Aleksandrov return -ENOMEM; 34491080ab95SNikolay Aleksandrov 34501080ab95SNikolay Aleksandrov return 0; 34511080ab95SNikolay Aleksandrov } 34521080ab95SNikolay Aleksandrov 3453b6fe0440SIdo Schimmel void br_multicast_uninit_stats(struct net_bridge *br) 3454b6fe0440SIdo Schimmel { 3455b6fe0440SIdo Schimmel free_percpu(br->mcast_stats); 3456b6fe0440SIdo Schimmel } 3457b6fe0440SIdo Schimmel 3458b3b6a84cSArnd Bergmann /* noinline for https://bugs.llvm.org/show_bug.cgi?id=45802#c9 */ 3459b3b6a84cSArnd Bergmann static noinline_for_stack void mcast_stats_add_dir(u64 *dst, u64 *src) 34601080ab95SNikolay Aleksandrov { 34611080ab95SNikolay Aleksandrov dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX]; 34621080ab95SNikolay Aleksandrov dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX]; 34631080ab95SNikolay Aleksandrov } 34641080ab95SNikolay Aleksandrov 34651080ab95SNikolay Aleksandrov void br_multicast_get_stats(const struct net_bridge *br, 34661080ab95SNikolay Aleksandrov const struct net_bridge_port *p, 34671080ab95SNikolay Aleksandrov struct br_mcast_stats *dest) 34681080ab95SNikolay Aleksandrov { 34691080ab95SNikolay Aleksandrov struct bridge_mcast_stats __percpu *stats; 34701080ab95SNikolay Aleksandrov struct br_mcast_stats tdst; 34711080ab95SNikolay Aleksandrov int i; 34721080ab95SNikolay Aleksandrov 34731080ab95SNikolay Aleksandrov memset(dest, 0, sizeof(*dest)); 34741080ab95SNikolay Aleksandrov if (p) 34751080ab95SNikolay Aleksandrov stats = p->mcast_stats; 34761080ab95SNikolay Aleksandrov else 34771080ab95SNikolay Aleksandrov stats = br->mcast_stats; 34781080ab95SNikolay Aleksandrov if (WARN_ON(!stats)) 34791080ab95SNikolay Aleksandrov return; 34801080ab95SNikolay Aleksandrov 34811080ab95SNikolay Aleksandrov memset(&tdst, 0, sizeof(tdst)); 34821080ab95SNikolay Aleksandrov for_each_possible_cpu(i) { 34831080ab95SNikolay Aleksandrov struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i); 34841080ab95SNikolay Aleksandrov struct br_mcast_stats temp; 34851080ab95SNikolay Aleksandrov unsigned int start; 34861080ab95SNikolay Aleksandrov 34871080ab95SNikolay Aleksandrov do { 34881080ab95SNikolay Aleksandrov start = u64_stats_fetch_begin_irq(&cpu_stats->syncp); 34891080ab95SNikolay Aleksandrov memcpy(&temp, &cpu_stats->mstats, sizeof(temp)); 34901080ab95SNikolay Aleksandrov } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start)); 34911080ab95SNikolay Aleksandrov 3492a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries); 3493a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries); 3494a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries); 34951080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves); 34961080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports); 34971080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports); 34981080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports); 34991080ab95SNikolay Aleksandrov tdst.igmp_parse_errors += temp.igmp_parse_errors; 35001080ab95SNikolay Aleksandrov 3501a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries); 3502a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries); 35031080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves); 35041080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports); 35051080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports); 35061080ab95SNikolay Aleksandrov tdst.mld_parse_errors += temp.mld_parse_errors; 35071080ab95SNikolay Aleksandrov } 35081080ab95SNikolay Aleksandrov memcpy(dest, &tdst, sizeof(*dest)); 35091080ab95SNikolay Aleksandrov } 351019e3a9c9SNikolay Aleksandrov 351119e3a9c9SNikolay Aleksandrov int br_mdb_hash_init(struct net_bridge *br) 351219e3a9c9SNikolay Aleksandrov { 351319e3a9c9SNikolay Aleksandrov return rhashtable_init(&br->mdb_hash_tbl, &br_mdb_rht_params); 351419e3a9c9SNikolay Aleksandrov } 351519e3a9c9SNikolay Aleksandrov 351619e3a9c9SNikolay Aleksandrov void br_mdb_hash_fini(struct net_bridge *br) 351719e3a9c9SNikolay Aleksandrov { 351819e3a9c9SNikolay Aleksandrov rhashtable_destroy(&br->mdb_hash_tbl); 351919e3a9c9SNikolay Aleksandrov } 3520