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" 368f07b831SNikolay Aleksandrov #include "br_private_mcast_eht.h" 37eb1d1641SHerbert Xu 3819e3a9c9SNikolay Aleksandrov static const struct rhashtable_params br_mdb_rht_params = { 3919e3a9c9SNikolay Aleksandrov .head_offset = offsetof(struct net_bridge_mdb_entry, rhnode), 4019e3a9c9SNikolay Aleksandrov .key_offset = offsetof(struct net_bridge_mdb_entry, addr), 4119e3a9c9SNikolay Aleksandrov .key_len = sizeof(struct br_ip), 4219e3a9c9SNikolay Aleksandrov .automatic_shrinking = true, 4319e3a9c9SNikolay Aleksandrov }; 4419e3a9c9SNikolay Aleksandrov 45085b53c8SNikolay Aleksandrov static const struct rhashtable_params br_sg_port_rht_params = { 46085b53c8SNikolay Aleksandrov .head_offset = offsetof(struct net_bridge_port_group, rhnode), 47085b53c8SNikolay Aleksandrov .key_offset = offsetof(struct net_bridge_port_group, key), 48085b53c8SNikolay Aleksandrov .key_len = sizeof(struct net_bridge_port_group_sg_key), 49085b53c8SNikolay Aleksandrov .automatic_shrinking = true, 50085b53c8SNikolay Aleksandrov }; 51085b53c8SNikolay Aleksandrov 52adc47037SNikolay Aleksandrov static void br_multicast_start_querier(struct net_bridge_mcast *brmctx, 5390010b36SLinus Lüssing struct bridge_mcast_own_query *query); 54adc47037SNikolay Aleksandrov static void br_ip4_multicast_add_router(struct net_bridge_mcast *brmctx, 55adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx); 56adc47037SNikolay Aleksandrov static void br_ip4_multicast_leave_group(struct net_bridge_mcast *brmctx, 57adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 58bc8c20acSSatish Ashok __be32 group, 596db6f0eaSFelix Fietkau __u16 vid, 606db6f0eaSFelix Fietkau const unsigned char *src); 6142c11ccfSNikolay Aleksandrov static void br_multicast_port_group_rexmit(struct timer_list *t); 626db6f0eaSFelix Fietkau 63ed2d3597SLinus Lüssing static void 64adc47037SNikolay Aleksandrov br_multicast_rport_del_notify(struct net_bridge_mcast_port *pmctx, bool deleted); 65adc47037SNikolay Aleksandrov static void br_ip6_multicast_add_router(struct net_bridge_mcast *brmctx, 66adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx); 6730515832SMatteo Croce #if IS_ENABLED(CONFIG_IPV6) 68adc47037SNikolay Aleksandrov static void br_ip6_multicast_leave_group(struct net_bridge_mcast *brmctx, 69adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 70bc8c20acSSatish Ashok const struct in6_addr *group, 716db6f0eaSFelix Fietkau __u16 vid, const unsigned char *src); 72bc8c20acSSatish Ashok #endif 73b0812368SNikolay Aleksandrov static struct net_bridge_port_group * 74adc47037SNikolay Aleksandrov __br_multicast_add_group(struct net_bridge_mcast *brmctx, 75adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 76b0812368SNikolay Aleksandrov struct br_ip *group, 77b0812368SNikolay Aleksandrov const unsigned char *src, 78b0812368SNikolay Aleksandrov u8 filter_mode, 799116ffbfSNikolay Aleksandrov bool igmpv2_mldv1, 809116ffbfSNikolay Aleksandrov bool blocked); 818266a049SNikolay Aleksandrov static void br_multicast_find_del_pg(struct net_bridge *br, 828266a049SNikolay Aleksandrov struct net_bridge_port_group *pg); 83613d61dbSNikolay Aleksandrov static void __br_multicast_stop(struct net_bridge_mcast *brmctx); 84c83b8fabSHerbert Xu 85c832962aSOleksandr Mazur static int br_mc_disabled_update(struct net_device *dev, bool value, 86c832962aSOleksandr Mazur struct netlink_ext_ack *extack); 87c832962aSOleksandr Mazur 88085b53c8SNikolay Aleksandrov static struct net_bridge_port_group * 89085b53c8SNikolay Aleksandrov br_sg_port_find(struct net_bridge *br, 90085b53c8SNikolay Aleksandrov struct net_bridge_port_group_sg_key *sg_p) 91085b53c8SNikolay Aleksandrov { 92085b53c8SNikolay Aleksandrov lockdep_assert_held_once(&br->multicast_lock); 93085b53c8SNikolay Aleksandrov 94085b53c8SNikolay Aleksandrov return rhashtable_lookup_fast(&br->sg_port_tbl, sg_p, 95085b53c8SNikolay Aleksandrov br_sg_port_rht_params); 96085b53c8SNikolay Aleksandrov } 97085b53c8SNikolay Aleksandrov 9819e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip_get_rcu(struct net_bridge *br, 99cfd56754SCong Wang struct br_ip *dst) 1007f285fa7SHerbert Xu { 10119e3a9c9SNikolay Aleksandrov return rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params); 1027f285fa7SHerbert Xu } 1037f285fa7SHerbert Xu 10419e3a9c9SNikolay Aleksandrov struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge *br, 10519e3a9c9SNikolay Aleksandrov struct br_ip *dst) 10619e3a9c9SNikolay Aleksandrov { 10719e3a9c9SNikolay Aleksandrov struct net_bridge_mdb_entry *ent; 10819e3a9c9SNikolay Aleksandrov 10919e3a9c9SNikolay Aleksandrov lockdep_assert_held_once(&br->multicast_lock); 11019e3a9c9SNikolay Aleksandrov 11119e3a9c9SNikolay Aleksandrov rcu_read_lock(); 11219e3a9c9SNikolay Aleksandrov ent = rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params); 11319e3a9c9SNikolay Aleksandrov rcu_read_unlock(); 11419e3a9c9SNikolay Aleksandrov 11519e3a9c9SNikolay Aleksandrov return ent; 11619e3a9c9SNikolay Aleksandrov } 11719e3a9c9SNikolay Aleksandrov 11819e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip4_get(struct net_bridge *br, 11919e3a9c9SNikolay Aleksandrov __be32 dst, __u16 vid) 120eb1d1641SHerbert Xu { 1218ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_dst; 1220821ec55SHerbert Xu 12319e3a9c9SNikolay Aleksandrov memset(&br_dst, 0, sizeof(br_dst)); 124eab3227bSNikolay Aleksandrov br_dst.dst.ip4 = dst; 1258ef2a9a5SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IP); 126b0e9a30dSVlad Yasevich br_dst.vid = vid; 1278ef2a9a5SYOSHIFUJI Hideaki 12819e3a9c9SNikolay Aleksandrov return br_mdb_ip_get(br, &br_dst); 1298ef2a9a5SYOSHIFUJI Hideaki } 1308ef2a9a5SYOSHIFUJI Hideaki 131dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 13219e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip6_get(struct net_bridge *br, 13319e3a9c9SNikolay Aleksandrov const struct in6_addr *dst, 134b0e9a30dSVlad Yasevich __u16 vid) 13508b202b6SYOSHIFUJI Hideaki { 13608b202b6SYOSHIFUJI Hideaki struct br_ip br_dst; 13708b202b6SYOSHIFUJI Hideaki 13819e3a9c9SNikolay Aleksandrov memset(&br_dst, 0, sizeof(br_dst)); 139eab3227bSNikolay Aleksandrov br_dst.dst.ip6 = *dst; 14008b202b6SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IPV6); 141b0e9a30dSVlad Yasevich br_dst.vid = vid; 14208b202b6SYOSHIFUJI Hideaki 14319e3a9c9SNikolay Aleksandrov return br_mdb_ip_get(br, &br_dst); 14408b202b6SYOSHIFUJI Hideaki } 14508b202b6SYOSHIFUJI Hideaki #endif 14608b202b6SYOSHIFUJI Hideaki 147adc47037SNikolay Aleksandrov struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge_mcast *brmctx, 148fbca58a2SCong Wang struct sk_buff *skb, u16 vid) 149eb1d1641SHerbert Xu { 150adc47037SNikolay Aleksandrov struct net_bridge *br = brmctx->br; 1518ef2a9a5SYOSHIFUJI Hideaki struct br_ip ip; 1528ef2a9a5SYOSHIFUJI Hideaki 1534cdd0d10SNikolay Aleksandrov if (!br_opt_get(br, BROPT_MULTICAST_ENABLED) || 1544cdd0d10SNikolay Aleksandrov br_multicast_ctx_vlan_global_disabled(brmctx)) 155eb1d1641SHerbert Xu return NULL; 156eb1d1641SHerbert Xu 1578ef2a9a5SYOSHIFUJI Hideaki if (BR_INPUT_SKB_CB(skb)->igmp) 1588ef2a9a5SYOSHIFUJI Hideaki return NULL; 1598ef2a9a5SYOSHIFUJI Hideaki 16019e3a9c9SNikolay Aleksandrov memset(&ip, 0, sizeof(ip)); 1618ef2a9a5SYOSHIFUJI Hideaki ip.proto = skb->protocol; 162fbca58a2SCong Wang ip.vid = vid; 1638ef2a9a5SYOSHIFUJI Hideaki 164eb1d1641SHerbert Xu switch (skb->protocol) { 165eb1d1641SHerbert Xu case htons(ETH_P_IP): 166eab3227bSNikolay Aleksandrov ip.dst.ip4 = ip_hdr(skb)->daddr; 167adc47037SNikolay Aleksandrov if (brmctx->multicast_igmp_version == 3) { 1687d07a68cSNikolay Aleksandrov struct net_bridge_mdb_entry *mdb; 1697d07a68cSNikolay Aleksandrov 1707d07a68cSNikolay Aleksandrov ip.src.ip4 = ip_hdr(skb)->saddr; 1717d07a68cSNikolay Aleksandrov mdb = br_mdb_ip_get_rcu(br, &ip); 1727d07a68cSNikolay Aleksandrov if (mdb) 1737d07a68cSNikolay Aleksandrov return mdb; 1747d07a68cSNikolay Aleksandrov ip.src.ip4 = 0; 1757d07a68cSNikolay Aleksandrov } 176eb1d1641SHerbert Xu break; 177dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 17808b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 179eab3227bSNikolay Aleksandrov ip.dst.ip6 = ipv6_hdr(skb)->daddr; 180adc47037SNikolay Aleksandrov if (brmctx->multicast_mld_version == 2) { 1817d07a68cSNikolay Aleksandrov struct net_bridge_mdb_entry *mdb; 1827d07a68cSNikolay Aleksandrov 1837d07a68cSNikolay Aleksandrov ip.src.ip6 = ipv6_hdr(skb)->saddr; 1847d07a68cSNikolay Aleksandrov mdb = br_mdb_ip_get_rcu(br, &ip); 1857d07a68cSNikolay Aleksandrov if (mdb) 1867d07a68cSNikolay Aleksandrov return mdb; 1877d07a68cSNikolay Aleksandrov memset(&ip.src.ip6, 0, sizeof(ip.src.ip6)); 1887d07a68cSNikolay Aleksandrov } 18908b202b6SYOSHIFUJI Hideaki break; 19008b202b6SYOSHIFUJI Hideaki #endif 1918ef2a9a5SYOSHIFUJI Hideaki default: 192955062b0SNikolay Aleksandrov ip.proto = 0; 193955062b0SNikolay Aleksandrov ether_addr_copy(ip.dst.mac_addr, eth_hdr(skb)->h_dest); 194eb1d1641SHerbert Xu } 195eb1d1641SHerbert Xu 19619e3a9c9SNikolay Aleksandrov return br_mdb_ip_get_rcu(br, &ip); 197eb1d1641SHerbert Xu } 198eb1d1641SHerbert Xu 19974edfd48SNikolay Aleksandrov /* IMPORTANT: this function must be used only when the contexts cannot be 20074edfd48SNikolay Aleksandrov * passed down (e.g. timer) and must be used for read-only purposes because 20174edfd48SNikolay Aleksandrov * the vlan snooping option can change, so it can return any context 20274edfd48SNikolay Aleksandrov * (non-vlan or vlan). Its initial intended purpose is to read timer values 20374edfd48SNikolay Aleksandrov * from the *current* context based on the option. At worst that could lead 20474edfd48SNikolay Aleksandrov * to inconsistent timers when the contexts are changed, i.e. src timer 20574edfd48SNikolay Aleksandrov * which needs to re-arm with a specific delay taken from the old context 20674edfd48SNikolay Aleksandrov */ 20774edfd48SNikolay Aleksandrov static struct net_bridge_mcast_port * 20874edfd48SNikolay Aleksandrov br_multicast_pg_to_port_ctx(const struct net_bridge_port_group *pg) 20974edfd48SNikolay Aleksandrov { 21074edfd48SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx = &pg->key.port->multicast_ctx; 21174edfd48SNikolay Aleksandrov struct net_bridge_vlan *vlan; 21274edfd48SNikolay Aleksandrov 21374edfd48SNikolay Aleksandrov lockdep_assert_held_once(&pg->key.port->br->multicast_lock); 21474edfd48SNikolay Aleksandrov 21574edfd48SNikolay Aleksandrov /* if vlan snooping is disabled use the port's multicast context */ 21674edfd48SNikolay Aleksandrov if (!pg->key.addr.vid || 21774edfd48SNikolay Aleksandrov !br_opt_get(pg->key.port->br, BROPT_MCAST_VLAN_SNOOPING_ENABLED)) 21874edfd48SNikolay Aleksandrov goto out; 21974edfd48SNikolay Aleksandrov 22074edfd48SNikolay Aleksandrov /* locking is tricky here, due to different rules for multicast and 22174edfd48SNikolay Aleksandrov * vlans we need to take rcu to find the vlan and make sure it has 22274edfd48SNikolay Aleksandrov * the BR_VLFLAG_MCAST_ENABLED flag set, it can only change under 22374edfd48SNikolay Aleksandrov * multicast_lock which must be already held here, so the vlan's pmctx 22474edfd48SNikolay Aleksandrov * can safely be used on return 22574edfd48SNikolay Aleksandrov */ 22674edfd48SNikolay Aleksandrov rcu_read_lock(); 2273f0d14efSNikolay Aleksandrov vlan = br_vlan_find(nbp_vlan_group_rcu(pg->key.port), pg->key.addr.vid); 22874edfd48SNikolay Aleksandrov if (vlan && !br_multicast_port_ctx_vlan_disabled(&vlan->port_mcast_ctx)) 22974edfd48SNikolay Aleksandrov pmctx = &vlan->port_mcast_ctx; 23074edfd48SNikolay Aleksandrov else 23174edfd48SNikolay Aleksandrov pmctx = NULL; 23274edfd48SNikolay Aleksandrov rcu_read_unlock(); 23374edfd48SNikolay Aleksandrov out: 23474edfd48SNikolay Aleksandrov return pmctx; 23574edfd48SNikolay Aleksandrov } 23674edfd48SNikolay Aleksandrov 2374cdd0d10SNikolay Aleksandrov /* when snooping we need to check if the contexts should be used 2384cdd0d10SNikolay Aleksandrov * in the following order: 2394cdd0d10SNikolay Aleksandrov * - if pmctx is non-NULL (port), check if it should be used 2404cdd0d10SNikolay Aleksandrov * - if pmctx is NULL (bridge), check if brmctx should be used 2414cdd0d10SNikolay Aleksandrov */ 2424cdd0d10SNikolay Aleksandrov static bool 2434cdd0d10SNikolay Aleksandrov br_multicast_ctx_should_use(const struct net_bridge_mcast *brmctx, 2444cdd0d10SNikolay Aleksandrov const struct net_bridge_mcast_port *pmctx) 2454cdd0d10SNikolay Aleksandrov { 2464cdd0d10SNikolay Aleksandrov if (!netif_running(brmctx->br->dev)) 2474cdd0d10SNikolay Aleksandrov return false; 2484cdd0d10SNikolay Aleksandrov 2494cdd0d10SNikolay Aleksandrov if (pmctx) 2504cdd0d10SNikolay Aleksandrov return !br_multicast_port_ctx_state_disabled(pmctx); 2514cdd0d10SNikolay Aleksandrov else 2524cdd0d10SNikolay Aleksandrov return !br_multicast_ctx_vlan_disabled(brmctx); 2534cdd0d10SNikolay Aleksandrov } 2544cdd0d10SNikolay Aleksandrov 255b0812368SNikolay Aleksandrov static bool br_port_group_equal(struct net_bridge_port_group *p, 256b0812368SNikolay Aleksandrov struct net_bridge_port *port, 257b0812368SNikolay Aleksandrov const unsigned char *src) 258b0812368SNikolay Aleksandrov { 259b0812368SNikolay Aleksandrov if (p->key.port != port) 260b0812368SNikolay Aleksandrov return false; 261b0812368SNikolay Aleksandrov 262b0812368SNikolay Aleksandrov if (!(port->flags & BR_MULTICAST_TO_UNICAST)) 263b0812368SNikolay Aleksandrov return true; 264b0812368SNikolay Aleksandrov 265b0812368SNikolay Aleksandrov return ether_addr_equal(src, p->eth_addr); 266b0812368SNikolay Aleksandrov } 267b0812368SNikolay Aleksandrov 268adc47037SNikolay Aleksandrov static void __fwd_add_star_excl(struct net_bridge_mcast_port *pmctx, 269adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, 2708266a049SNikolay Aleksandrov struct br_ip *sg_ip) 2718266a049SNikolay Aleksandrov { 2728266a049SNikolay Aleksandrov struct net_bridge_port_group_sg_key sg_key; 2738266a049SNikolay Aleksandrov struct net_bridge_port_group *src_pg; 274adc47037SNikolay Aleksandrov struct net_bridge_mcast *brmctx; 2758266a049SNikolay Aleksandrov 2768266a049SNikolay Aleksandrov memset(&sg_key, 0, sizeof(sg_key)); 2777b54aaafSNikolay Aleksandrov brmctx = br_multicast_port_ctx_get_global(pmctx); 2788266a049SNikolay Aleksandrov sg_key.port = pg->key.port; 2798266a049SNikolay Aleksandrov sg_key.addr = *sg_ip; 280adc47037SNikolay Aleksandrov if (br_sg_port_find(brmctx->br, &sg_key)) 2818266a049SNikolay Aleksandrov return; 2828266a049SNikolay Aleksandrov 283adc47037SNikolay Aleksandrov src_pg = __br_multicast_add_group(brmctx, pmctx, 284adc47037SNikolay Aleksandrov sg_ip, pg->eth_addr, 2859116ffbfSNikolay Aleksandrov MCAST_INCLUDE, false, false); 2868266a049SNikolay Aleksandrov if (IS_ERR_OR_NULL(src_pg) || 2878266a049SNikolay Aleksandrov src_pg->rt_protocol != RTPROT_KERNEL) 2888266a049SNikolay Aleksandrov return; 2898266a049SNikolay Aleksandrov 2908266a049SNikolay Aleksandrov src_pg->flags |= MDB_PG_FLAGS_STAR_EXCL; 2918266a049SNikolay Aleksandrov } 2928266a049SNikolay Aleksandrov 2938266a049SNikolay Aleksandrov static void __fwd_del_star_excl(struct net_bridge_port_group *pg, 2948266a049SNikolay Aleksandrov struct br_ip *sg_ip) 2958266a049SNikolay Aleksandrov { 2968266a049SNikolay Aleksandrov struct net_bridge_port_group_sg_key sg_key; 2978266a049SNikolay Aleksandrov struct net_bridge *br = pg->key.port->br; 2988266a049SNikolay Aleksandrov struct net_bridge_port_group *src_pg; 2998266a049SNikolay Aleksandrov 3008266a049SNikolay Aleksandrov memset(&sg_key, 0, sizeof(sg_key)); 3018266a049SNikolay Aleksandrov sg_key.port = pg->key.port; 3028266a049SNikolay Aleksandrov sg_key.addr = *sg_ip; 3038266a049SNikolay Aleksandrov src_pg = br_sg_port_find(br, &sg_key); 3048266a049SNikolay Aleksandrov if (!src_pg || !(src_pg->flags & MDB_PG_FLAGS_STAR_EXCL) || 3058266a049SNikolay Aleksandrov src_pg->rt_protocol != RTPROT_KERNEL) 3068266a049SNikolay Aleksandrov return; 3078266a049SNikolay Aleksandrov 3088266a049SNikolay Aleksandrov br_multicast_find_del_pg(br, src_pg); 3098266a049SNikolay Aleksandrov } 3108266a049SNikolay Aleksandrov 3118266a049SNikolay Aleksandrov /* When a port group transitions to (or is added as) EXCLUDE we need to add it 3128266a049SNikolay Aleksandrov * to all other ports' S,G entries which are not blocked by the current group 3138266a049SNikolay Aleksandrov * for proper replication, the assumption is that any S,G blocked entries 3148266a049SNikolay Aleksandrov * are already added so the S,G,port lookup should skip them. 3158266a049SNikolay Aleksandrov * When a port group transitions from EXCLUDE -> INCLUDE mode or is being 3168266a049SNikolay Aleksandrov * deleted we need to remove it from all ports' S,G entries where it was 3178266a049SNikolay Aleksandrov * automatically installed before (i.e. where it's MDB_PG_FLAGS_STAR_EXCL). 3188266a049SNikolay Aleksandrov */ 3198266a049SNikolay Aleksandrov void br_multicast_star_g_handle_mode(struct net_bridge_port_group *pg, 3208266a049SNikolay Aleksandrov u8 filter_mode) 3218266a049SNikolay Aleksandrov { 3228266a049SNikolay Aleksandrov struct net_bridge *br = pg->key.port->br; 3238266a049SNikolay Aleksandrov struct net_bridge_port_group *pg_lst; 324adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx; 3258266a049SNikolay Aleksandrov struct net_bridge_mdb_entry *mp; 3268266a049SNikolay Aleksandrov struct br_ip sg_ip; 3278266a049SNikolay Aleksandrov 3288266a049SNikolay Aleksandrov if (WARN_ON(!br_multicast_is_star_g(&pg->key.addr))) 3298266a049SNikolay Aleksandrov return; 3308266a049SNikolay Aleksandrov 3318266a049SNikolay Aleksandrov mp = br_mdb_ip_get(br, &pg->key.addr); 3328266a049SNikolay Aleksandrov if (!mp) 3338266a049SNikolay Aleksandrov return; 334eb1593a0SNikolay Aleksandrov pmctx = br_multicast_pg_to_port_ctx(pg); 335eb1593a0SNikolay Aleksandrov if (!pmctx) 336eb1593a0SNikolay Aleksandrov return; 3378266a049SNikolay Aleksandrov 3388266a049SNikolay Aleksandrov memset(&sg_ip, 0, sizeof(sg_ip)); 3398266a049SNikolay Aleksandrov sg_ip = pg->key.addr; 3407b54aaafSNikolay Aleksandrov 3418266a049SNikolay Aleksandrov for (pg_lst = mlock_dereference(mp->ports, br); 3428266a049SNikolay Aleksandrov pg_lst; 3438266a049SNikolay Aleksandrov pg_lst = mlock_dereference(pg_lst->next, br)) { 3448266a049SNikolay Aleksandrov struct net_bridge_group_src *src_ent; 3458266a049SNikolay Aleksandrov 3468266a049SNikolay Aleksandrov if (pg_lst == pg) 3478266a049SNikolay Aleksandrov continue; 3488266a049SNikolay Aleksandrov hlist_for_each_entry(src_ent, &pg_lst->src_list, node) { 3498266a049SNikolay Aleksandrov if (!(src_ent->flags & BR_SGRP_F_INSTALLED)) 3508266a049SNikolay Aleksandrov continue; 3518266a049SNikolay Aleksandrov sg_ip.src = src_ent->addr.src; 3528266a049SNikolay Aleksandrov switch (filter_mode) { 3538266a049SNikolay Aleksandrov case MCAST_INCLUDE: 3548266a049SNikolay Aleksandrov __fwd_del_star_excl(pg, &sg_ip); 3558266a049SNikolay Aleksandrov break; 3568266a049SNikolay Aleksandrov case MCAST_EXCLUDE: 357adc47037SNikolay Aleksandrov __fwd_add_star_excl(pmctx, pg, &sg_ip); 3588266a049SNikolay Aleksandrov break; 3598266a049SNikolay Aleksandrov } 3608266a049SNikolay Aleksandrov } 3618266a049SNikolay Aleksandrov } 3628266a049SNikolay Aleksandrov } 3638266a049SNikolay Aleksandrov 364094b82fdSNikolay Aleksandrov /* called when adding a new S,G with host_joined == false by default */ 365094b82fdSNikolay Aleksandrov static void br_multicast_sg_host_state(struct net_bridge_mdb_entry *star_mp, 366094b82fdSNikolay Aleksandrov struct net_bridge_port_group *sg) 367094b82fdSNikolay Aleksandrov { 368094b82fdSNikolay Aleksandrov struct net_bridge_mdb_entry *sg_mp; 369094b82fdSNikolay Aleksandrov 370094b82fdSNikolay Aleksandrov if (WARN_ON(!br_multicast_is_star_g(&star_mp->addr))) 371094b82fdSNikolay Aleksandrov return; 372094b82fdSNikolay Aleksandrov if (!star_mp->host_joined) 373094b82fdSNikolay Aleksandrov return; 374094b82fdSNikolay Aleksandrov 375094b82fdSNikolay Aleksandrov sg_mp = br_mdb_ip_get(star_mp->br, &sg->key.addr); 376094b82fdSNikolay Aleksandrov if (!sg_mp) 377094b82fdSNikolay Aleksandrov return; 378094b82fdSNikolay Aleksandrov sg_mp->host_joined = true; 379094b82fdSNikolay Aleksandrov } 380094b82fdSNikolay Aleksandrov 381094b82fdSNikolay Aleksandrov /* set the host_joined state of all of *,G's S,G entries */ 382094b82fdSNikolay Aleksandrov static void br_multicast_star_g_host_state(struct net_bridge_mdb_entry *star_mp) 383094b82fdSNikolay Aleksandrov { 384094b82fdSNikolay Aleksandrov struct net_bridge *br = star_mp->br; 385094b82fdSNikolay Aleksandrov struct net_bridge_mdb_entry *sg_mp; 386094b82fdSNikolay Aleksandrov struct net_bridge_port_group *pg; 387094b82fdSNikolay Aleksandrov struct br_ip sg_ip; 388094b82fdSNikolay Aleksandrov 389094b82fdSNikolay Aleksandrov if (WARN_ON(!br_multicast_is_star_g(&star_mp->addr))) 390094b82fdSNikolay Aleksandrov return; 391094b82fdSNikolay Aleksandrov 392094b82fdSNikolay Aleksandrov memset(&sg_ip, 0, sizeof(sg_ip)); 393094b82fdSNikolay Aleksandrov sg_ip = star_mp->addr; 394094b82fdSNikolay Aleksandrov for (pg = mlock_dereference(star_mp->ports, br); 395094b82fdSNikolay Aleksandrov pg; 396094b82fdSNikolay Aleksandrov pg = mlock_dereference(pg->next, br)) { 397094b82fdSNikolay Aleksandrov struct net_bridge_group_src *src_ent; 398094b82fdSNikolay Aleksandrov 399094b82fdSNikolay Aleksandrov hlist_for_each_entry(src_ent, &pg->src_list, node) { 400094b82fdSNikolay Aleksandrov if (!(src_ent->flags & BR_SGRP_F_INSTALLED)) 401094b82fdSNikolay Aleksandrov continue; 402094b82fdSNikolay Aleksandrov sg_ip.src = src_ent->addr.src; 403094b82fdSNikolay Aleksandrov sg_mp = br_mdb_ip_get(br, &sg_ip); 404094b82fdSNikolay Aleksandrov if (!sg_mp) 405094b82fdSNikolay Aleksandrov continue; 406094b82fdSNikolay Aleksandrov sg_mp->host_joined = star_mp->host_joined; 407094b82fdSNikolay Aleksandrov } 408094b82fdSNikolay Aleksandrov } 409094b82fdSNikolay Aleksandrov } 410094b82fdSNikolay Aleksandrov 4118266a049SNikolay Aleksandrov static void br_multicast_sg_del_exclude_ports(struct net_bridge_mdb_entry *sgmp) 4128266a049SNikolay Aleksandrov { 4138266a049SNikolay Aleksandrov struct net_bridge_port_group __rcu **pp; 4148266a049SNikolay Aleksandrov struct net_bridge_port_group *p; 4158266a049SNikolay Aleksandrov 4168266a049SNikolay Aleksandrov /* *,G exclude ports are only added to S,G entries */ 4178266a049SNikolay Aleksandrov if (WARN_ON(br_multicast_is_star_g(&sgmp->addr))) 4188266a049SNikolay Aleksandrov return; 4198266a049SNikolay Aleksandrov 4208266a049SNikolay Aleksandrov /* we need the STAR_EXCLUDE ports if there are non-STAR_EXCLUDE ports 4218266a049SNikolay Aleksandrov * we should ignore perm entries since they're managed by user-space 4228266a049SNikolay Aleksandrov */ 4238266a049SNikolay Aleksandrov for (pp = &sgmp->ports; 4248266a049SNikolay Aleksandrov (p = mlock_dereference(*pp, sgmp->br)) != NULL; 4258266a049SNikolay Aleksandrov pp = &p->next) 4268266a049SNikolay Aleksandrov if (!(p->flags & (MDB_PG_FLAGS_STAR_EXCL | 4278266a049SNikolay Aleksandrov MDB_PG_FLAGS_PERMANENT))) 4288266a049SNikolay Aleksandrov return; 4298266a049SNikolay Aleksandrov 430094b82fdSNikolay Aleksandrov /* currently the host can only have joined the *,G which means 431094b82fdSNikolay Aleksandrov * we treat it as EXCLUDE {}, so for an S,G it's considered a 432094b82fdSNikolay Aleksandrov * STAR_EXCLUDE entry and we can safely leave it 433094b82fdSNikolay Aleksandrov */ 434094b82fdSNikolay Aleksandrov sgmp->host_joined = false; 435094b82fdSNikolay Aleksandrov 4368266a049SNikolay Aleksandrov for (pp = &sgmp->ports; 4378266a049SNikolay Aleksandrov (p = mlock_dereference(*pp, sgmp->br)) != NULL;) { 4388266a049SNikolay Aleksandrov if (!(p->flags & MDB_PG_FLAGS_PERMANENT)) 4398266a049SNikolay Aleksandrov br_multicast_del_pg(sgmp, p, pp); 4408266a049SNikolay Aleksandrov else 4418266a049SNikolay Aleksandrov pp = &p->next; 4428266a049SNikolay Aleksandrov } 4438266a049SNikolay Aleksandrov } 4448266a049SNikolay Aleksandrov 4458266a049SNikolay Aleksandrov void br_multicast_sg_add_exclude_ports(struct net_bridge_mdb_entry *star_mp, 4468266a049SNikolay Aleksandrov struct net_bridge_port_group *sg) 4478266a049SNikolay Aleksandrov { 4488266a049SNikolay Aleksandrov struct net_bridge_port_group_sg_key sg_key; 4498266a049SNikolay Aleksandrov struct net_bridge *br = star_mp->br; 450adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx; 4518266a049SNikolay Aleksandrov struct net_bridge_port_group *pg; 452adc47037SNikolay Aleksandrov struct net_bridge_mcast *brmctx; 4538266a049SNikolay Aleksandrov 4548266a049SNikolay Aleksandrov if (WARN_ON(br_multicast_is_star_g(&sg->key.addr))) 4558266a049SNikolay Aleksandrov return; 4568266a049SNikolay Aleksandrov if (WARN_ON(!br_multicast_is_star_g(&star_mp->addr))) 4578266a049SNikolay Aleksandrov return; 4588266a049SNikolay Aleksandrov 459094b82fdSNikolay Aleksandrov br_multicast_sg_host_state(star_mp, sg); 4608266a049SNikolay Aleksandrov memset(&sg_key, 0, sizeof(sg_key)); 4618266a049SNikolay Aleksandrov sg_key.addr = sg->key.addr; 4628266a049SNikolay Aleksandrov /* we need to add all exclude ports to the S,G */ 4638266a049SNikolay Aleksandrov for (pg = mlock_dereference(star_mp->ports, br); 4648266a049SNikolay Aleksandrov pg; 4658266a049SNikolay Aleksandrov pg = mlock_dereference(pg->next, br)) { 4668266a049SNikolay Aleksandrov struct net_bridge_port_group *src_pg; 4678266a049SNikolay Aleksandrov 4688266a049SNikolay Aleksandrov if (pg == sg || pg->filter_mode == MCAST_INCLUDE) 4698266a049SNikolay Aleksandrov continue; 4708266a049SNikolay Aleksandrov 4718266a049SNikolay Aleksandrov sg_key.port = pg->key.port; 4728266a049SNikolay Aleksandrov if (br_sg_port_find(br, &sg_key)) 4738266a049SNikolay Aleksandrov continue; 4748266a049SNikolay Aleksandrov 475eb1593a0SNikolay Aleksandrov pmctx = br_multicast_pg_to_port_ctx(pg); 476eb1593a0SNikolay Aleksandrov if (!pmctx) 477eb1593a0SNikolay Aleksandrov continue; 478eb1593a0SNikolay Aleksandrov brmctx = br_multicast_port_ctx_get_global(pmctx); 479eb1593a0SNikolay Aleksandrov 480adc47037SNikolay Aleksandrov src_pg = __br_multicast_add_group(brmctx, pmctx, 4818266a049SNikolay Aleksandrov &sg->key.addr, 4828266a049SNikolay Aleksandrov sg->eth_addr, 4839116ffbfSNikolay Aleksandrov MCAST_INCLUDE, false, false); 4848266a049SNikolay Aleksandrov if (IS_ERR_OR_NULL(src_pg) || 4858266a049SNikolay Aleksandrov src_pg->rt_protocol != RTPROT_KERNEL) 4868266a049SNikolay Aleksandrov continue; 4878266a049SNikolay Aleksandrov src_pg->flags |= MDB_PG_FLAGS_STAR_EXCL; 4888266a049SNikolay Aleksandrov } 4898266a049SNikolay Aleksandrov } 4908266a049SNikolay Aleksandrov 491b0812368SNikolay Aleksandrov static void br_multicast_fwd_src_add(struct net_bridge_group_src *src) 492b0812368SNikolay Aleksandrov { 4938266a049SNikolay Aleksandrov struct net_bridge_mdb_entry *star_mp; 494adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx; 495b0812368SNikolay Aleksandrov struct net_bridge_port_group *sg; 496adc47037SNikolay Aleksandrov struct net_bridge_mcast *brmctx; 497b0812368SNikolay Aleksandrov struct br_ip sg_ip; 498b0812368SNikolay Aleksandrov 499b0812368SNikolay Aleksandrov if (src->flags & BR_SGRP_F_INSTALLED) 500b0812368SNikolay Aleksandrov return; 501b0812368SNikolay Aleksandrov 502b0812368SNikolay Aleksandrov memset(&sg_ip, 0, sizeof(sg_ip)); 503eb1593a0SNikolay Aleksandrov pmctx = br_multicast_pg_to_port_ctx(src->pg); 504eb1593a0SNikolay Aleksandrov if (!pmctx) 505eb1593a0SNikolay Aleksandrov return; 5067b54aaafSNikolay Aleksandrov brmctx = br_multicast_port_ctx_get_global(pmctx); 507b0812368SNikolay Aleksandrov sg_ip = src->pg->key.addr; 508b0812368SNikolay Aleksandrov sg_ip.src = src->addr.src; 509adc47037SNikolay Aleksandrov 510adc47037SNikolay Aleksandrov sg = __br_multicast_add_group(brmctx, pmctx, &sg_ip, 5119116ffbfSNikolay Aleksandrov src->pg->eth_addr, MCAST_INCLUDE, false, 5129116ffbfSNikolay Aleksandrov !timer_pending(&src->timer)); 513b0812368SNikolay Aleksandrov if (IS_ERR_OR_NULL(sg)) 514b0812368SNikolay Aleksandrov return; 515b0812368SNikolay Aleksandrov src->flags |= BR_SGRP_F_INSTALLED; 5168266a049SNikolay Aleksandrov sg->flags &= ~MDB_PG_FLAGS_STAR_EXCL; 517b0812368SNikolay Aleksandrov 518b0812368SNikolay Aleksandrov /* if it was added by user-space as perm we can skip next steps */ 519b0812368SNikolay Aleksandrov if (sg->rt_protocol != RTPROT_KERNEL && 520b0812368SNikolay Aleksandrov (sg->flags & MDB_PG_FLAGS_PERMANENT)) 521b0812368SNikolay Aleksandrov return; 522b0812368SNikolay Aleksandrov 523b0812368SNikolay Aleksandrov /* the kernel is now responsible for removing this S,G */ 524b0812368SNikolay Aleksandrov del_timer(&sg->timer); 5258266a049SNikolay Aleksandrov star_mp = br_mdb_ip_get(src->br, &src->pg->key.addr); 5268266a049SNikolay Aleksandrov if (!star_mp) 5278266a049SNikolay Aleksandrov return; 5288266a049SNikolay Aleksandrov 5298266a049SNikolay Aleksandrov br_multicast_sg_add_exclude_ports(star_mp, sg); 530b0812368SNikolay Aleksandrov } 531b0812368SNikolay Aleksandrov 532d5a10222SNikolay Aleksandrov static void br_multicast_fwd_src_remove(struct net_bridge_group_src *src, 533d5a10222SNikolay Aleksandrov bool fastleave) 534b0812368SNikolay Aleksandrov { 535b0812368SNikolay Aleksandrov struct net_bridge_port_group *p, *pg = src->pg; 536b0812368SNikolay Aleksandrov struct net_bridge_port_group __rcu **pp; 537b0812368SNikolay Aleksandrov struct net_bridge_mdb_entry *mp; 538b0812368SNikolay Aleksandrov struct br_ip sg_ip; 539b0812368SNikolay Aleksandrov 540b0812368SNikolay Aleksandrov memset(&sg_ip, 0, sizeof(sg_ip)); 541b0812368SNikolay Aleksandrov sg_ip = pg->key.addr; 542b0812368SNikolay Aleksandrov sg_ip.src = src->addr.src; 543b0812368SNikolay Aleksandrov 544b0812368SNikolay Aleksandrov mp = br_mdb_ip_get(src->br, &sg_ip); 545b0812368SNikolay Aleksandrov if (!mp) 546b0812368SNikolay Aleksandrov return; 547b0812368SNikolay Aleksandrov 548b0812368SNikolay Aleksandrov for (pp = &mp->ports; 549b0812368SNikolay Aleksandrov (p = mlock_dereference(*pp, src->br)) != NULL; 550b0812368SNikolay Aleksandrov pp = &p->next) { 551b0812368SNikolay Aleksandrov if (!br_port_group_equal(p, pg->key.port, pg->eth_addr)) 552b0812368SNikolay Aleksandrov continue; 553b0812368SNikolay Aleksandrov 554b0812368SNikolay Aleksandrov if (p->rt_protocol != RTPROT_KERNEL && 555b0812368SNikolay Aleksandrov (p->flags & MDB_PG_FLAGS_PERMANENT)) 556b0812368SNikolay Aleksandrov break; 557b0812368SNikolay Aleksandrov 558d5a10222SNikolay Aleksandrov if (fastleave) 559d5a10222SNikolay Aleksandrov p->flags |= MDB_PG_FLAGS_FAST_LEAVE; 560b0812368SNikolay Aleksandrov br_multicast_del_pg(mp, p, pp); 561b0812368SNikolay Aleksandrov break; 562b0812368SNikolay Aleksandrov } 563b0812368SNikolay Aleksandrov src->flags &= ~BR_SGRP_F_INSTALLED; 564b0812368SNikolay Aleksandrov } 565b0812368SNikolay Aleksandrov 5669116ffbfSNikolay Aleksandrov /* install S,G and based on src's timer enable or disable forwarding */ 567b0812368SNikolay Aleksandrov static void br_multicast_fwd_src_handle(struct net_bridge_group_src *src) 568b0812368SNikolay Aleksandrov { 5699116ffbfSNikolay Aleksandrov struct net_bridge_port_group_sg_key sg_key; 5709116ffbfSNikolay Aleksandrov struct net_bridge_port_group *sg; 5719116ffbfSNikolay Aleksandrov u8 old_flags; 5729116ffbfSNikolay Aleksandrov 573b0812368SNikolay Aleksandrov br_multicast_fwd_src_add(src); 5749116ffbfSNikolay Aleksandrov 5759116ffbfSNikolay Aleksandrov memset(&sg_key, 0, sizeof(sg_key)); 5769116ffbfSNikolay Aleksandrov sg_key.addr = src->pg->key.addr; 5779116ffbfSNikolay Aleksandrov sg_key.addr.src = src->addr.src; 5789116ffbfSNikolay Aleksandrov sg_key.port = src->pg->key.port; 5799116ffbfSNikolay Aleksandrov 5809116ffbfSNikolay Aleksandrov sg = br_sg_port_find(src->br, &sg_key); 5819116ffbfSNikolay Aleksandrov if (!sg || (sg->flags & MDB_PG_FLAGS_PERMANENT)) 5829116ffbfSNikolay Aleksandrov return; 5839116ffbfSNikolay Aleksandrov 5849116ffbfSNikolay Aleksandrov old_flags = sg->flags; 5859116ffbfSNikolay Aleksandrov if (timer_pending(&src->timer)) 5869116ffbfSNikolay Aleksandrov sg->flags &= ~MDB_PG_FLAGS_BLOCKED; 5879116ffbfSNikolay Aleksandrov else 5889116ffbfSNikolay Aleksandrov sg->flags |= MDB_PG_FLAGS_BLOCKED; 5899116ffbfSNikolay Aleksandrov 5909116ffbfSNikolay Aleksandrov if (old_flags != sg->flags) { 5919116ffbfSNikolay Aleksandrov struct net_bridge_mdb_entry *sg_mp; 5929116ffbfSNikolay Aleksandrov 5939116ffbfSNikolay Aleksandrov sg_mp = br_mdb_ip_get(src->br, &sg_key.addr); 5949116ffbfSNikolay Aleksandrov if (!sg_mp) 5959116ffbfSNikolay Aleksandrov return; 5969116ffbfSNikolay Aleksandrov br_mdb_notify(src->br->dev, sg_mp, sg, RTM_NEWMDB); 5979116ffbfSNikolay Aleksandrov } 598b0812368SNikolay Aleksandrov } 599b0812368SNikolay Aleksandrov 600e12cec65SNikolay Aleksandrov static void br_multicast_destroy_mdb_entry(struct net_bridge_mcast_gc *gc) 601e12cec65SNikolay Aleksandrov { 602e12cec65SNikolay Aleksandrov struct net_bridge_mdb_entry *mp; 603e12cec65SNikolay Aleksandrov 604e12cec65SNikolay Aleksandrov mp = container_of(gc, struct net_bridge_mdb_entry, mcast_gc); 605e12cec65SNikolay Aleksandrov WARN_ON(!hlist_unhashed(&mp->mdb_node)); 606e12cec65SNikolay Aleksandrov WARN_ON(mp->ports); 607e12cec65SNikolay Aleksandrov 608e12cec65SNikolay Aleksandrov del_timer_sync(&mp->timer); 609e12cec65SNikolay Aleksandrov kfree_rcu(mp, rcu); 610e12cec65SNikolay Aleksandrov } 611e12cec65SNikolay Aleksandrov 612e12cec65SNikolay Aleksandrov static void br_multicast_del_mdb_entry(struct net_bridge_mdb_entry *mp) 613e12cec65SNikolay Aleksandrov { 614e12cec65SNikolay Aleksandrov struct net_bridge *br = mp->br; 615e12cec65SNikolay Aleksandrov 616e12cec65SNikolay Aleksandrov rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode, 617e12cec65SNikolay Aleksandrov br_mdb_rht_params); 618e12cec65SNikolay Aleksandrov hlist_del_init_rcu(&mp->mdb_node); 619e12cec65SNikolay Aleksandrov hlist_add_head(&mp->mcast_gc.gc_node, &br->mcast_gc_list); 620e12cec65SNikolay Aleksandrov queue_work(system_long_wq, &br->mcast_gc_work); 621e12cec65SNikolay Aleksandrov } 622e12cec65SNikolay Aleksandrov 62388c1f37fSAllen Pais static void br_multicast_group_expired(struct timer_list *t) 624eb1d1641SHerbert Xu { 62588c1f37fSAllen Pais struct net_bridge_mdb_entry *mp = from_timer(mp, t, timer); 626eb1d1641SHerbert Xu struct net_bridge *br = mp->br; 627eb1d1641SHerbert Xu 628eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 629b0812368SNikolay Aleksandrov if (hlist_unhashed(&mp->mdb_node) || !netif_running(br->dev) || 630b0812368SNikolay Aleksandrov timer_pending(&mp->timer)) 631eb1d1641SHerbert Xu goto out; 632eb1d1641SHerbert Xu 6331bc844eeSNikolay Aleksandrov br_multicast_host_leave(mp, true); 634eb1d1641SHerbert Xu 635eb1d1641SHerbert Xu if (mp->ports) 636eb1d1641SHerbert Xu goto out; 637e12cec65SNikolay Aleksandrov br_multicast_del_mdb_entry(mp); 638eb1d1641SHerbert Xu out: 639eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 640eb1d1641SHerbert Xu } 641eb1d1641SHerbert Xu 642e12cec65SNikolay Aleksandrov static void br_multicast_destroy_group_src(struct net_bridge_mcast_gc *gc) 643e12cec65SNikolay Aleksandrov { 644e12cec65SNikolay Aleksandrov struct net_bridge_group_src *src; 645e12cec65SNikolay Aleksandrov 646e12cec65SNikolay Aleksandrov src = container_of(gc, struct net_bridge_group_src, mcast_gc); 647e12cec65SNikolay Aleksandrov WARN_ON(!hlist_unhashed(&src->node)); 648e12cec65SNikolay Aleksandrov 649e12cec65SNikolay Aleksandrov del_timer_sync(&src->timer); 650e12cec65SNikolay Aleksandrov kfree_rcu(src, rcu); 651e12cec65SNikolay Aleksandrov } 652e12cec65SNikolay Aleksandrov 653d5a10222SNikolay Aleksandrov void br_multicast_del_group_src(struct net_bridge_group_src *src, 654d5a10222SNikolay Aleksandrov bool fastleave) 6558b671779SNikolay Aleksandrov { 656085b53c8SNikolay Aleksandrov struct net_bridge *br = src->pg->key.port->br; 6578b671779SNikolay Aleksandrov 658d5a10222SNikolay Aleksandrov br_multicast_fwd_src_remove(src, fastleave); 6598b671779SNikolay Aleksandrov hlist_del_init_rcu(&src->node); 6608b671779SNikolay Aleksandrov src->pg->src_ents--; 661e12cec65SNikolay Aleksandrov hlist_add_head(&src->mcast_gc.gc_node, &br->mcast_gc_list); 662e12cec65SNikolay Aleksandrov queue_work(system_long_wq, &br->mcast_gc_work); 663e12cec65SNikolay Aleksandrov } 664e12cec65SNikolay Aleksandrov 665e12cec65SNikolay Aleksandrov static void br_multicast_destroy_port_group(struct net_bridge_mcast_gc *gc) 666e12cec65SNikolay Aleksandrov { 667e12cec65SNikolay Aleksandrov struct net_bridge_port_group *pg; 668e12cec65SNikolay Aleksandrov 669e12cec65SNikolay Aleksandrov pg = container_of(gc, struct net_bridge_port_group, mcast_gc); 670e12cec65SNikolay Aleksandrov WARN_ON(!hlist_unhashed(&pg->mglist)); 671e12cec65SNikolay Aleksandrov WARN_ON(!hlist_empty(&pg->src_list)); 672e12cec65SNikolay Aleksandrov 673e12cec65SNikolay Aleksandrov del_timer_sync(&pg->rexmit_timer); 674e12cec65SNikolay Aleksandrov del_timer_sync(&pg->timer); 675e12cec65SNikolay Aleksandrov kfree_rcu(pg, rcu); 6768b671779SNikolay Aleksandrov } 6778b671779SNikolay Aleksandrov 678681590bdSNikolay Aleksandrov void br_multicast_del_pg(struct net_bridge_mdb_entry *mp, 679681590bdSNikolay Aleksandrov struct net_bridge_port_group *pg, 680681590bdSNikolay Aleksandrov struct net_bridge_port_group __rcu **pp) 681681590bdSNikolay Aleksandrov { 682085b53c8SNikolay Aleksandrov struct net_bridge *br = pg->key.port->br; 6838b671779SNikolay Aleksandrov struct net_bridge_group_src *ent; 6848b671779SNikolay Aleksandrov struct hlist_node *tmp; 685681590bdSNikolay Aleksandrov 686681590bdSNikolay Aleksandrov rcu_assign_pointer(*pp, pg->next); 687681590bdSNikolay Aleksandrov hlist_del_init(&pg->mglist); 688baa74d39SNikolay Aleksandrov br_multicast_eht_clean_sets(pg); 6898b671779SNikolay Aleksandrov hlist_for_each_entry_safe(ent, tmp, &pg->src_list, node) 690d5a10222SNikolay Aleksandrov br_multicast_del_group_src(ent, false); 69181f19838SNikolay Aleksandrov br_mdb_notify(br->dev, mp, pg, RTM_DELMDB); 69274705582SNikolay Aleksandrov if (!br_multicast_is_star_g(&mp->addr)) { 69374705582SNikolay Aleksandrov rhashtable_remove_fast(&br->sg_port_tbl, &pg->rhnode, 69474705582SNikolay Aleksandrov br_sg_port_rht_params); 6958266a049SNikolay Aleksandrov br_multicast_sg_del_exclude_ports(mp); 69674705582SNikolay Aleksandrov } else { 6978266a049SNikolay Aleksandrov br_multicast_star_g_handle_mode(pg, MCAST_INCLUDE); 69874705582SNikolay Aleksandrov } 699e12cec65SNikolay Aleksandrov hlist_add_head(&pg->mcast_gc.gc_node, &br->mcast_gc_list); 700e12cec65SNikolay Aleksandrov queue_work(system_long_wq, &br->mcast_gc_work); 701681590bdSNikolay Aleksandrov 702681590bdSNikolay Aleksandrov if (!mp->ports && !mp->host_joined && netif_running(br->dev)) 703681590bdSNikolay Aleksandrov mod_timer(&mp->timer, jiffies); 704681590bdSNikolay Aleksandrov } 705681590bdSNikolay Aleksandrov 706681590bdSNikolay Aleksandrov static void br_multicast_find_del_pg(struct net_bridge *br, 707eb1d1641SHerbert Xu struct net_bridge_port_group *pg) 708eb1d1641SHerbert Xu { 7098b671779SNikolay Aleksandrov struct net_bridge_port_group __rcu **pp; 710eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 711eb1d1641SHerbert Xu struct net_bridge_port_group *p; 712e8051688SEric Dumazet 713085b53c8SNikolay Aleksandrov mp = br_mdb_ip_get(br, &pg->key.addr); 714eb1d1641SHerbert Xu if (WARN_ON(!mp)) 715eb1d1641SHerbert Xu return; 716eb1d1641SHerbert Xu 717e8051688SEric Dumazet for (pp = &mp->ports; 718e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 719e8051688SEric Dumazet pp = &p->next) { 720eb1d1641SHerbert Xu if (p != pg) 721eb1d1641SHerbert Xu continue; 722eb1d1641SHerbert Xu 723681590bdSNikolay Aleksandrov br_multicast_del_pg(mp, pg, pp); 724eb1d1641SHerbert Xu return; 725eb1d1641SHerbert Xu } 726eb1d1641SHerbert Xu 727eb1d1641SHerbert Xu WARN_ON(1); 728eb1d1641SHerbert Xu } 729eb1d1641SHerbert Xu 73088c1f37fSAllen Pais static void br_multicast_port_group_expired(struct timer_list *t) 731eb1d1641SHerbert Xu { 73288c1f37fSAllen Pais struct net_bridge_port_group *pg = from_timer(pg, t, timer); 733d6c33d67SNikolay Aleksandrov struct net_bridge_group_src *src_ent; 734085b53c8SNikolay Aleksandrov struct net_bridge *br = pg->key.port->br; 735d6c33d67SNikolay Aleksandrov struct hlist_node *tmp; 736d6c33d67SNikolay Aleksandrov bool changed; 737eb1d1641SHerbert Xu 738eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 739eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&pg->timer) || 7409d06b6d8SElad Raz hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT) 741eb1d1641SHerbert Xu goto out; 742eb1d1641SHerbert Xu 743d6c33d67SNikolay Aleksandrov changed = !!(pg->filter_mode == MCAST_EXCLUDE); 744d6c33d67SNikolay Aleksandrov pg->filter_mode = MCAST_INCLUDE; 745d6c33d67SNikolay Aleksandrov hlist_for_each_entry_safe(src_ent, tmp, &pg->src_list, node) { 746d6c33d67SNikolay Aleksandrov if (!timer_pending(&src_ent->timer)) { 747d5a10222SNikolay Aleksandrov br_multicast_del_group_src(src_ent, false); 748d6c33d67SNikolay Aleksandrov changed = true; 749d6c33d67SNikolay Aleksandrov } 750d6c33d67SNikolay Aleksandrov } 751eb1d1641SHerbert Xu 752d6c33d67SNikolay Aleksandrov if (hlist_empty(&pg->src_list)) { 753d6c33d67SNikolay Aleksandrov br_multicast_find_del_pg(br, pg); 754d6c33d67SNikolay Aleksandrov } else if (changed) { 755085b53c8SNikolay Aleksandrov struct net_bridge_mdb_entry *mp = br_mdb_ip_get(br, &pg->key.addr); 756d6c33d67SNikolay Aleksandrov 7578266a049SNikolay Aleksandrov if (changed && br_multicast_is_star_g(&pg->key.addr)) 7588266a049SNikolay Aleksandrov br_multicast_star_g_handle_mode(pg, MCAST_INCLUDE); 7598266a049SNikolay Aleksandrov 760d6c33d67SNikolay Aleksandrov if (WARN_ON(!mp)) 761d6c33d67SNikolay Aleksandrov goto out; 762d6c33d67SNikolay Aleksandrov br_mdb_notify(br->dev, mp, pg, RTM_NEWMDB); 763d6c33d67SNikolay Aleksandrov } 764eb1d1641SHerbert Xu out: 765eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 766eb1d1641SHerbert Xu } 767eb1d1641SHerbert Xu 768e12cec65SNikolay Aleksandrov static void br_multicast_gc(struct hlist_head *head) 769e12cec65SNikolay Aleksandrov { 770e12cec65SNikolay Aleksandrov struct net_bridge_mcast_gc *gcent; 771e12cec65SNikolay Aleksandrov struct hlist_node *tmp; 772e12cec65SNikolay Aleksandrov 773e12cec65SNikolay Aleksandrov hlist_for_each_entry_safe(gcent, tmp, head, gc_node) { 774e12cec65SNikolay Aleksandrov hlist_del_init(&gcent->gc_node); 775e12cec65SNikolay Aleksandrov gcent->destroy(gcent); 776e12cec65SNikolay Aleksandrov } 777e12cec65SNikolay Aleksandrov } 778e12cec65SNikolay Aleksandrov 779615cc23eSNikolay Aleksandrov static void __br_multicast_query_handle_vlan(struct net_bridge_mcast *brmctx, 780615cc23eSNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 781615cc23eSNikolay Aleksandrov struct sk_buff *skb) 782615cc23eSNikolay Aleksandrov { 783615cc23eSNikolay Aleksandrov struct net_bridge_vlan *vlan = NULL; 784615cc23eSNikolay Aleksandrov 785615cc23eSNikolay Aleksandrov if (pmctx && br_multicast_port_ctx_is_vlan(pmctx)) 786615cc23eSNikolay Aleksandrov vlan = pmctx->vlan; 787615cc23eSNikolay Aleksandrov else if (br_multicast_ctx_is_vlan(brmctx)) 788615cc23eSNikolay Aleksandrov vlan = brmctx->vlan; 789615cc23eSNikolay Aleksandrov 790615cc23eSNikolay Aleksandrov if (vlan && !(vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED)) { 791615cc23eSNikolay Aleksandrov u16 vlan_proto; 792615cc23eSNikolay Aleksandrov 793615cc23eSNikolay Aleksandrov if (br_vlan_get_proto(brmctx->br->dev, &vlan_proto) != 0) 794615cc23eSNikolay Aleksandrov return; 795615cc23eSNikolay Aleksandrov __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vlan->vid); 796615cc23eSNikolay Aleksandrov } 797615cc23eSNikolay Aleksandrov } 798615cc23eSNikolay Aleksandrov 799adc47037SNikolay Aleksandrov static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge_mcast *brmctx, 800615cc23eSNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 801438ef2d0SNikolay Aleksandrov struct net_bridge_port_group *pg, 802438ef2d0SNikolay Aleksandrov __be32 ip_dst, __be32 group, 803438ef2d0SNikolay Aleksandrov bool with_srcs, bool over_lmqt, 80442c11ccfSNikolay Aleksandrov u8 sflag, u8 *igmp_type, 80542c11ccfSNikolay Aleksandrov bool *need_rexmit) 806eb1d1641SHerbert Xu { 807085b53c8SNikolay Aleksandrov struct net_bridge_port *p = pg ? pg->key.port : NULL; 808438ef2d0SNikolay Aleksandrov struct net_bridge_group_src *ent; 809438ef2d0SNikolay Aleksandrov size_t pkt_size, igmp_hdr_size; 810438ef2d0SNikolay Aleksandrov unsigned long now = jiffies; 8115e923585SNikolay Aleksandrov struct igmpv3_query *ihv3; 812438ef2d0SNikolay Aleksandrov void *csum_start = NULL; 813438ef2d0SNikolay Aleksandrov __sum16 *csum = NULL; 814eb1d1641SHerbert Xu struct sk_buff *skb; 815eb1d1641SHerbert Xu struct igmphdr *ih; 816eb1d1641SHerbert Xu struct ethhdr *eth; 817438ef2d0SNikolay Aleksandrov unsigned long lmqt; 818eb1d1641SHerbert Xu struct iphdr *iph; 819438ef2d0SNikolay Aleksandrov u16 lmqt_srcs = 0; 820eb1d1641SHerbert Xu 8215e923585SNikolay Aleksandrov igmp_hdr_size = sizeof(*ih); 822d3d065c0SNikolay Aleksandrov if (brmctx->multicast_igmp_version == 3) { 8235e923585SNikolay Aleksandrov igmp_hdr_size = sizeof(*ihv3); 824438ef2d0SNikolay Aleksandrov if (pg && with_srcs) { 825d3d065c0SNikolay Aleksandrov lmqt = now + (brmctx->multicast_last_member_interval * 826d3d065c0SNikolay Aleksandrov brmctx->multicast_last_member_count); 827438ef2d0SNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) { 828438ef2d0SNikolay Aleksandrov if (over_lmqt == time_after(ent->timer.expires, 829438ef2d0SNikolay Aleksandrov lmqt) && 830438ef2d0SNikolay Aleksandrov ent->src_query_rexmit_cnt > 0) 831438ef2d0SNikolay Aleksandrov lmqt_srcs++; 832438ef2d0SNikolay Aleksandrov } 833438ef2d0SNikolay Aleksandrov 834438ef2d0SNikolay Aleksandrov if (!lmqt_srcs) 835438ef2d0SNikolay Aleksandrov return NULL; 836438ef2d0SNikolay Aleksandrov igmp_hdr_size += lmqt_srcs * sizeof(__be32); 837438ef2d0SNikolay Aleksandrov } 838438ef2d0SNikolay Aleksandrov } 839438ef2d0SNikolay Aleksandrov 840438ef2d0SNikolay Aleksandrov pkt_size = sizeof(*eth) + sizeof(*iph) + 4 + igmp_hdr_size; 841438ef2d0SNikolay Aleksandrov if ((p && pkt_size > p->dev->mtu) || 842adc47037SNikolay Aleksandrov pkt_size > brmctx->br->dev->mtu) 843438ef2d0SNikolay Aleksandrov return NULL; 844438ef2d0SNikolay Aleksandrov 845adc47037SNikolay Aleksandrov skb = netdev_alloc_skb_ip_align(brmctx->br->dev, pkt_size); 846eb1d1641SHerbert Xu if (!skb) 847eb1d1641SHerbert Xu goto out; 848eb1d1641SHerbert Xu 849615cc23eSNikolay Aleksandrov __br_multicast_query_handle_vlan(brmctx, pmctx, skb); 850eb1d1641SHerbert Xu skb->protocol = htons(ETH_P_IP); 851eb1d1641SHerbert Xu 852eb1d1641SHerbert Xu skb_reset_mac_header(skb); 853eb1d1641SHerbert Xu eth = eth_hdr(skb); 854eb1d1641SHerbert Xu 855adc47037SNikolay Aleksandrov ether_addr_copy(eth->h_source, brmctx->br->dev->dev_addr); 856438ef2d0SNikolay Aleksandrov ip_eth_mc_map(ip_dst, eth->h_dest); 857eb1d1641SHerbert Xu eth->h_proto = htons(ETH_P_IP); 858eb1d1641SHerbert Xu skb_put(skb, sizeof(*eth)); 859eb1d1641SHerbert Xu 860eb1d1641SHerbert Xu skb_set_network_header(skb, skb->len); 861eb1d1641SHerbert Xu iph = ip_hdr(skb); 862438ef2d0SNikolay Aleksandrov iph->tot_len = htons(pkt_size - sizeof(*eth)); 863eb1d1641SHerbert Xu 864eb1d1641SHerbert Xu iph->version = 4; 865eb1d1641SHerbert Xu iph->ihl = 6; 866eb1d1641SHerbert Xu iph->tos = 0xc0; 867eb1d1641SHerbert Xu iph->id = 0; 868eb1d1641SHerbert Xu iph->frag_off = htons(IP_DF); 869eb1d1641SHerbert Xu iph->ttl = 1; 870eb1d1641SHerbert Xu iph->protocol = IPPROTO_IGMP; 871adc47037SNikolay Aleksandrov iph->saddr = br_opt_get(brmctx->br, BROPT_MULTICAST_QUERY_USE_IFADDR) ? 872adc47037SNikolay Aleksandrov inet_select_addr(brmctx->br->dev, 0, RT_SCOPE_LINK) : 0; 873438ef2d0SNikolay Aleksandrov iph->daddr = ip_dst; 874eb1d1641SHerbert Xu ((u8 *)&iph[1])[0] = IPOPT_RA; 875eb1d1641SHerbert Xu ((u8 *)&iph[1])[1] = 4; 876eb1d1641SHerbert Xu ((u8 *)&iph[1])[2] = 0; 877eb1d1641SHerbert Xu ((u8 *)&iph[1])[3] = 0; 878eb1d1641SHerbert Xu ip_send_check(iph); 879eb1d1641SHerbert Xu skb_put(skb, 24); 880eb1d1641SHerbert Xu 881eb1d1641SHerbert Xu skb_set_transport_header(skb, skb->len); 8821080ab95SNikolay Aleksandrov *igmp_type = IGMP_HOST_MEMBERSHIP_QUERY; 8835e923585SNikolay Aleksandrov 884d3d065c0SNikolay Aleksandrov switch (brmctx->multicast_igmp_version) { 8855e923585SNikolay Aleksandrov case 2: 8865e923585SNikolay Aleksandrov ih = igmp_hdr(skb); 887eb1d1641SHerbert Xu ih->type = IGMP_HOST_MEMBERSHIP_QUERY; 888d3d065c0SNikolay Aleksandrov ih->code = (group ? brmctx->multicast_last_member_interval : 889d3d065c0SNikolay Aleksandrov brmctx->multicast_query_response_interval) / 890eb1d1641SHerbert Xu (HZ / IGMP_TIMER_SCALE); 891eb1d1641SHerbert Xu ih->group = group; 892eb1d1641SHerbert Xu ih->csum = 0; 893438ef2d0SNikolay Aleksandrov csum = &ih->csum; 894438ef2d0SNikolay Aleksandrov csum_start = (void *)ih; 8955e923585SNikolay Aleksandrov break; 8965e923585SNikolay Aleksandrov case 3: 8975e923585SNikolay Aleksandrov ihv3 = igmpv3_query_hdr(skb); 8985e923585SNikolay Aleksandrov ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY; 899d3d065c0SNikolay Aleksandrov ihv3->code = (group ? brmctx->multicast_last_member_interval : 900d3d065c0SNikolay Aleksandrov brmctx->multicast_query_response_interval) / 9015e923585SNikolay Aleksandrov (HZ / IGMP_TIMER_SCALE); 9025e923585SNikolay Aleksandrov ihv3->group = group; 903d3d065c0SNikolay Aleksandrov ihv3->qqic = brmctx->multicast_query_interval / HZ; 904438ef2d0SNikolay Aleksandrov ihv3->nsrcs = htons(lmqt_srcs); 9055e923585SNikolay Aleksandrov ihv3->resv = 0; 906438ef2d0SNikolay Aleksandrov ihv3->suppress = sflag; 9075e923585SNikolay Aleksandrov ihv3->qrv = 2; 9085e923585SNikolay Aleksandrov ihv3->csum = 0; 909438ef2d0SNikolay Aleksandrov csum = &ihv3->csum; 910438ef2d0SNikolay Aleksandrov csum_start = (void *)ihv3; 911438ef2d0SNikolay Aleksandrov if (!pg || !with_srcs) 912438ef2d0SNikolay Aleksandrov break; 913438ef2d0SNikolay Aleksandrov 914438ef2d0SNikolay Aleksandrov lmqt_srcs = 0; 915438ef2d0SNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) { 916438ef2d0SNikolay Aleksandrov if (over_lmqt == time_after(ent->timer.expires, 917438ef2d0SNikolay Aleksandrov lmqt) && 918438ef2d0SNikolay Aleksandrov ent->src_query_rexmit_cnt > 0) { 919deb96566SNikolay Aleksandrov ihv3->srcs[lmqt_srcs++] = ent->addr.src.ip4; 920438ef2d0SNikolay Aleksandrov ent->src_query_rexmit_cnt--; 92142c11ccfSNikolay Aleksandrov if (need_rexmit && ent->src_query_rexmit_cnt) 92242c11ccfSNikolay Aleksandrov *need_rexmit = true; 923438ef2d0SNikolay Aleksandrov } 924438ef2d0SNikolay Aleksandrov } 925438ef2d0SNikolay Aleksandrov if (WARN_ON(lmqt_srcs != ntohs(ihv3->nsrcs))) { 926438ef2d0SNikolay Aleksandrov kfree_skb(skb); 927438ef2d0SNikolay Aleksandrov return NULL; 928438ef2d0SNikolay Aleksandrov } 9295e923585SNikolay Aleksandrov break; 9305e923585SNikolay Aleksandrov } 931eb1d1641SHerbert Xu 932438ef2d0SNikolay Aleksandrov if (WARN_ON(!csum || !csum_start)) { 933438ef2d0SNikolay Aleksandrov kfree_skb(skb); 934438ef2d0SNikolay Aleksandrov return NULL; 935438ef2d0SNikolay Aleksandrov } 936438ef2d0SNikolay Aleksandrov 937438ef2d0SNikolay Aleksandrov *csum = ip_compute_csum(csum_start, igmp_hdr_size); 9385e923585SNikolay Aleksandrov skb_put(skb, igmp_hdr_size); 939eb1d1641SHerbert Xu __skb_pull(skb, sizeof(*eth)); 940eb1d1641SHerbert Xu 941eb1d1641SHerbert Xu out: 942eb1d1641SHerbert Xu return skb; 943eb1d1641SHerbert Xu } 944eb1d1641SHerbert Xu 945dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 946adc47037SNikolay Aleksandrov static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge_mcast *brmctx, 947615cc23eSNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 948438ef2d0SNikolay Aleksandrov struct net_bridge_port_group *pg, 949438ef2d0SNikolay Aleksandrov const struct in6_addr *ip6_dst, 950438ef2d0SNikolay Aleksandrov const struct in6_addr *group, 951438ef2d0SNikolay Aleksandrov bool with_srcs, bool over_llqt, 95242c11ccfSNikolay Aleksandrov u8 sflag, u8 *igmp_type, 95342c11ccfSNikolay Aleksandrov bool *need_rexmit) 95408b202b6SYOSHIFUJI Hideaki { 955085b53c8SNikolay Aleksandrov struct net_bridge_port *p = pg ? pg->key.port : NULL; 956438ef2d0SNikolay Aleksandrov struct net_bridge_group_src *ent; 957438ef2d0SNikolay Aleksandrov size_t pkt_size, mld_hdr_size; 958438ef2d0SNikolay Aleksandrov unsigned long now = jiffies; 959aa2ae3e7SNikolay Aleksandrov struct mld2_query *mld2q; 960438ef2d0SNikolay Aleksandrov void *csum_start = NULL; 961aa2ae3e7SNikolay Aleksandrov unsigned long interval; 962438ef2d0SNikolay Aleksandrov __sum16 *csum = NULL; 96308b202b6SYOSHIFUJI Hideaki struct ipv6hdr *ip6h; 96408b202b6SYOSHIFUJI Hideaki struct mld_msg *mldq; 965aa2ae3e7SNikolay Aleksandrov struct sk_buff *skb; 966438ef2d0SNikolay Aleksandrov unsigned long llqt; 96708b202b6SYOSHIFUJI Hideaki struct ethhdr *eth; 968438ef2d0SNikolay Aleksandrov u16 llqt_srcs = 0; 96908b202b6SYOSHIFUJI Hideaki u8 *hopopt; 97008b202b6SYOSHIFUJI Hideaki 971aa2ae3e7SNikolay Aleksandrov mld_hdr_size = sizeof(*mldq); 972d3d065c0SNikolay Aleksandrov if (brmctx->multicast_mld_version == 2) { 973aa2ae3e7SNikolay Aleksandrov mld_hdr_size = sizeof(*mld2q); 974438ef2d0SNikolay Aleksandrov if (pg && with_srcs) { 975d3d065c0SNikolay Aleksandrov llqt = now + (brmctx->multicast_last_member_interval * 976d3d065c0SNikolay Aleksandrov brmctx->multicast_last_member_count); 977438ef2d0SNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) { 978438ef2d0SNikolay Aleksandrov if (over_llqt == time_after(ent->timer.expires, 979438ef2d0SNikolay Aleksandrov llqt) && 980438ef2d0SNikolay Aleksandrov ent->src_query_rexmit_cnt > 0) 981438ef2d0SNikolay Aleksandrov llqt_srcs++; 982438ef2d0SNikolay Aleksandrov } 983438ef2d0SNikolay Aleksandrov 984438ef2d0SNikolay Aleksandrov if (!llqt_srcs) 985438ef2d0SNikolay Aleksandrov return NULL; 986438ef2d0SNikolay Aleksandrov mld_hdr_size += llqt_srcs * sizeof(struct in6_addr); 987438ef2d0SNikolay Aleksandrov } 988438ef2d0SNikolay Aleksandrov } 989438ef2d0SNikolay Aleksandrov 990438ef2d0SNikolay Aleksandrov pkt_size = sizeof(*eth) + sizeof(*ip6h) + 8 + mld_hdr_size; 991438ef2d0SNikolay Aleksandrov if ((p && pkt_size > p->dev->mtu) || 992adc47037SNikolay Aleksandrov pkt_size > brmctx->br->dev->mtu) 993438ef2d0SNikolay Aleksandrov return NULL; 994438ef2d0SNikolay Aleksandrov 995adc47037SNikolay Aleksandrov skb = netdev_alloc_skb_ip_align(brmctx->br->dev, pkt_size); 99608b202b6SYOSHIFUJI Hideaki if (!skb) 99708b202b6SYOSHIFUJI Hideaki goto out; 99808b202b6SYOSHIFUJI Hideaki 999615cc23eSNikolay Aleksandrov __br_multicast_query_handle_vlan(brmctx, pmctx, skb); 100008b202b6SYOSHIFUJI Hideaki skb->protocol = htons(ETH_P_IPV6); 100108b202b6SYOSHIFUJI Hideaki 100208b202b6SYOSHIFUJI Hideaki /* Ethernet header */ 100308b202b6SYOSHIFUJI Hideaki skb_reset_mac_header(skb); 100408b202b6SYOSHIFUJI Hideaki eth = eth_hdr(skb); 100508b202b6SYOSHIFUJI Hideaki 1006adc47037SNikolay Aleksandrov ether_addr_copy(eth->h_source, brmctx->br->dev->dev_addr); 100708b202b6SYOSHIFUJI Hideaki eth->h_proto = htons(ETH_P_IPV6); 100808b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*eth)); 100908b202b6SYOSHIFUJI Hideaki 101008b202b6SYOSHIFUJI Hideaki /* IPv6 header + HbH option */ 101108b202b6SYOSHIFUJI Hideaki skb_set_network_header(skb, skb->len); 101208b202b6SYOSHIFUJI Hideaki ip6h = ipv6_hdr(skb); 101308b202b6SYOSHIFUJI Hideaki 101408b202b6SYOSHIFUJI Hideaki *(__force __be32 *)ip6h = htonl(0x60000000); 1015aa2ae3e7SNikolay Aleksandrov ip6h->payload_len = htons(8 + mld_hdr_size); 101608b202b6SYOSHIFUJI Hideaki ip6h->nexthdr = IPPROTO_HOPOPTS; 101708b202b6SYOSHIFUJI Hideaki ip6h->hop_limit = 1; 1018438ef2d0SNikolay Aleksandrov ip6h->daddr = *ip6_dst; 1019adc47037SNikolay Aleksandrov if (ipv6_dev_get_saddr(dev_net(brmctx->br->dev), brmctx->br->dev, 1020adc47037SNikolay Aleksandrov &ip6h->daddr, 0, &ip6h->saddr)) { 1021d1d81d4cSUlrich Weber kfree_skb(skb); 1022adc47037SNikolay Aleksandrov br_opt_toggle(brmctx->br, BROPT_HAS_IPV6_ADDR, false); 1023d1d81d4cSUlrich Weber return NULL; 1024d1d81d4cSUlrich Weber } 10250888d5f3Sdaniel 1026adc47037SNikolay Aleksandrov br_opt_toggle(brmctx->br, BROPT_HAS_IPV6_ADDR, true); 102736cff5a1SLinus Lüssing ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest); 102808b202b6SYOSHIFUJI Hideaki 102908b202b6SYOSHIFUJI Hideaki hopopt = (u8 *)(ip6h + 1); 103008b202b6SYOSHIFUJI Hideaki hopopt[0] = IPPROTO_ICMPV6; /* next hdr */ 103108b202b6SYOSHIFUJI Hideaki hopopt[1] = 0; /* length of HbH */ 103208b202b6SYOSHIFUJI Hideaki hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */ 103308b202b6SYOSHIFUJI Hideaki hopopt[3] = 2; /* Length of RA Option */ 103408b202b6SYOSHIFUJI Hideaki hopopt[4] = 0; /* Type = 0x0000 (MLD) */ 103508b202b6SYOSHIFUJI Hideaki hopopt[5] = 0; 10361de5a71cSEldad Zack hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */ 10371de5a71cSEldad Zack hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */ 103808b202b6SYOSHIFUJI Hideaki 103908b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*ip6h) + 8); 104008b202b6SYOSHIFUJI Hideaki 104108b202b6SYOSHIFUJI Hideaki /* ICMPv6 */ 104208b202b6SYOSHIFUJI Hideaki skb_set_transport_header(skb, skb->len); 1043438ef2d0SNikolay Aleksandrov interval = ipv6_addr_any(group) ? 1044d3d065c0SNikolay Aleksandrov brmctx->multicast_query_response_interval : 1045d3d065c0SNikolay Aleksandrov brmctx->multicast_last_member_interval; 10461080ab95SNikolay Aleksandrov *igmp_type = ICMPV6_MGM_QUERY; 1047d3d065c0SNikolay Aleksandrov switch (brmctx->multicast_mld_version) { 1048aa2ae3e7SNikolay Aleksandrov case 1: 1049aa2ae3e7SNikolay Aleksandrov mldq = (struct mld_msg *)icmp6_hdr(skb); 105008b202b6SYOSHIFUJI Hideaki mldq->mld_type = ICMPV6_MGM_QUERY; 105108b202b6SYOSHIFUJI Hideaki mldq->mld_code = 0; 105208b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = 0; 105308b202b6SYOSHIFUJI Hideaki mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval)); 105408b202b6SYOSHIFUJI Hideaki mldq->mld_reserved = 0; 1055438ef2d0SNikolay Aleksandrov mldq->mld_mca = *group; 1056438ef2d0SNikolay Aleksandrov csum = &mldq->mld_cksum; 1057438ef2d0SNikolay Aleksandrov csum_start = (void *)mldq; 1058aa2ae3e7SNikolay Aleksandrov break; 1059aa2ae3e7SNikolay Aleksandrov case 2: 1060aa2ae3e7SNikolay Aleksandrov mld2q = (struct mld2_query *)icmp6_hdr(skb); 106153631a5fSLance Richardson mld2q->mld2q_mrc = htons((u16)jiffies_to_msecs(interval)); 1062aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_type = ICMPV6_MGM_QUERY; 1063aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_code = 0; 1064aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_cksum = 0; 1065aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_resv1 = 0; 1066aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_resv2 = 0; 1067438ef2d0SNikolay Aleksandrov mld2q->mld2q_suppress = sflag; 1068aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_qrv = 2; 1069438ef2d0SNikolay Aleksandrov mld2q->mld2q_nsrcs = htons(llqt_srcs); 1070d3d065c0SNikolay Aleksandrov mld2q->mld2q_qqic = brmctx->multicast_query_interval / HZ; 1071438ef2d0SNikolay Aleksandrov mld2q->mld2q_mca = *group; 1072438ef2d0SNikolay Aleksandrov csum = &mld2q->mld2q_cksum; 1073438ef2d0SNikolay Aleksandrov csum_start = (void *)mld2q; 1074438ef2d0SNikolay Aleksandrov if (!pg || !with_srcs) 1075438ef2d0SNikolay Aleksandrov break; 1076438ef2d0SNikolay Aleksandrov 1077438ef2d0SNikolay Aleksandrov llqt_srcs = 0; 1078438ef2d0SNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) { 1079438ef2d0SNikolay Aleksandrov if (over_llqt == time_after(ent->timer.expires, 1080438ef2d0SNikolay Aleksandrov llqt) && 1081438ef2d0SNikolay Aleksandrov ent->src_query_rexmit_cnt > 0) { 1082deb96566SNikolay Aleksandrov mld2q->mld2q_srcs[llqt_srcs++] = ent->addr.src.ip6; 1083438ef2d0SNikolay Aleksandrov ent->src_query_rexmit_cnt--; 108442c11ccfSNikolay Aleksandrov if (need_rexmit && ent->src_query_rexmit_cnt) 108542c11ccfSNikolay Aleksandrov *need_rexmit = true; 1086438ef2d0SNikolay Aleksandrov } 1087438ef2d0SNikolay Aleksandrov } 1088438ef2d0SNikolay Aleksandrov if (WARN_ON(llqt_srcs != ntohs(mld2q->mld2q_nsrcs))) { 1089438ef2d0SNikolay Aleksandrov kfree_skb(skb); 1090438ef2d0SNikolay Aleksandrov return NULL; 1091438ef2d0SNikolay Aleksandrov } 1092aa2ae3e7SNikolay Aleksandrov break; 1093aa2ae3e7SNikolay Aleksandrov } 109408b202b6SYOSHIFUJI Hideaki 1095438ef2d0SNikolay Aleksandrov if (WARN_ON(!csum || !csum_start)) { 1096438ef2d0SNikolay Aleksandrov kfree_skb(skb); 1097438ef2d0SNikolay Aleksandrov return NULL; 1098438ef2d0SNikolay Aleksandrov } 1099438ef2d0SNikolay Aleksandrov 1100438ef2d0SNikolay Aleksandrov *csum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, mld_hdr_size, 1101438ef2d0SNikolay Aleksandrov IPPROTO_ICMPV6, 1102438ef2d0SNikolay Aleksandrov csum_partial(csum_start, mld_hdr_size, 0)); 1103438ef2d0SNikolay Aleksandrov skb_put(skb, mld_hdr_size); 110408b202b6SYOSHIFUJI Hideaki __skb_pull(skb, sizeof(*eth)); 110508b202b6SYOSHIFUJI Hideaki 110608b202b6SYOSHIFUJI Hideaki out: 110708b202b6SYOSHIFUJI Hideaki return skb; 110808b202b6SYOSHIFUJI Hideaki } 110908b202b6SYOSHIFUJI Hideaki #endif 111008b202b6SYOSHIFUJI Hideaki 1111adc47037SNikolay Aleksandrov static struct sk_buff *br_multicast_alloc_query(struct net_bridge_mcast *brmctx, 1112615cc23eSNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 1113438ef2d0SNikolay Aleksandrov struct net_bridge_port_group *pg, 1114438ef2d0SNikolay Aleksandrov struct br_ip *ip_dst, 1115438ef2d0SNikolay Aleksandrov struct br_ip *group, 1116438ef2d0SNikolay Aleksandrov bool with_srcs, bool over_lmqt, 111742c11ccfSNikolay Aleksandrov u8 sflag, u8 *igmp_type, 111842c11ccfSNikolay Aleksandrov bool *need_rexmit) 11198ef2a9a5SYOSHIFUJI Hideaki { 1120438ef2d0SNikolay Aleksandrov __be32 ip4_dst; 1121438ef2d0SNikolay Aleksandrov 1122438ef2d0SNikolay Aleksandrov switch (group->proto) { 11238ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 1124eab3227bSNikolay Aleksandrov ip4_dst = ip_dst ? ip_dst->dst.ip4 : htonl(INADDR_ALLHOSTS_GROUP); 1125615cc23eSNikolay Aleksandrov return br_ip4_multicast_alloc_query(brmctx, pmctx, pg, 1126eab3227bSNikolay Aleksandrov ip4_dst, group->dst.ip4, 1127438ef2d0SNikolay Aleksandrov with_srcs, over_lmqt, 112842c11ccfSNikolay Aleksandrov sflag, igmp_type, 112942c11ccfSNikolay Aleksandrov need_rexmit); 1130dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 1131438ef2d0SNikolay Aleksandrov case htons(ETH_P_IPV6): { 1132438ef2d0SNikolay Aleksandrov struct in6_addr ip6_dst; 1133438ef2d0SNikolay Aleksandrov 1134438ef2d0SNikolay Aleksandrov if (ip_dst) 1135eab3227bSNikolay Aleksandrov ip6_dst = ip_dst->dst.ip6; 1136438ef2d0SNikolay Aleksandrov else 1137438ef2d0SNikolay Aleksandrov ipv6_addr_set(&ip6_dst, htonl(0xff020000), 0, 0, 1138438ef2d0SNikolay Aleksandrov htonl(1)); 1139438ef2d0SNikolay Aleksandrov 1140615cc23eSNikolay Aleksandrov return br_ip6_multicast_alloc_query(brmctx, pmctx, pg, 1141eab3227bSNikolay Aleksandrov &ip6_dst, &group->dst.ip6, 1142438ef2d0SNikolay Aleksandrov with_srcs, over_lmqt, 114342c11ccfSNikolay Aleksandrov sflag, igmp_type, 114442c11ccfSNikolay Aleksandrov need_rexmit); 1145438ef2d0SNikolay Aleksandrov } 114608b202b6SYOSHIFUJI Hideaki #endif 11478ef2a9a5SYOSHIFUJI Hideaki } 11488ef2a9a5SYOSHIFUJI Hideaki return NULL; 11498ef2a9a5SYOSHIFUJI Hideaki } 11508ef2a9a5SYOSHIFUJI Hideaki 1151cfd56754SCong Wang struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br, 11525e923585SNikolay Aleksandrov struct br_ip *group) 1153eb1d1641SHerbert Xu { 1154eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 11554c0833bcSTobias Klauser int err; 1156eb1d1641SHerbert Xu 115719e3a9c9SNikolay Aleksandrov mp = br_mdb_ip_get(br, group); 115819e3a9c9SNikolay Aleksandrov if (mp) 115919e3a9c9SNikolay Aleksandrov return mp; 1160eb1d1641SHerbert Xu 116119e3a9c9SNikolay Aleksandrov if (atomic_read(&br->mdb_hash_tbl.nelems) >= br->hash_max) { 1162c832962aSOleksandr Mazur br_mc_disabled_update(br->dev, false, NULL); 116319e3a9c9SNikolay Aleksandrov br_opt_toggle(br, BROPT_MULTICAST_ENABLED, false); 116419e3a9c9SNikolay Aleksandrov return ERR_PTR(-E2BIG); 1165eb1d1641SHerbert Xu } 1166eb1d1641SHerbert Xu 1167eb1d1641SHerbert Xu mp = kzalloc(sizeof(*mp), GFP_ATOMIC); 1168eb1d1641SHerbert Xu if (unlikely(!mp)) 11694c0833bcSTobias Klauser return ERR_PTR(-ENOMEM); 1170eb1d1641SHerbert Xu 1171eb1d1641SHerbert Xu mp->br = br; 11728ef2a9a5SYOSHIFUJI Hideaki mp->addr = *group; 1173e12cec65SNikolay Aleksandrov mp->mcast_gc.destroy = br_multicast_destroy_mdb_entry; 117488c1f37fSAllen Pais timer_setup(&mp->timer, br_multicast_group_expired, 0); 117519e3a9c9SNikolay Aleksandrov err = rhashtable_lookup_insert_fast(&br->mdb_hash_tbl, &mp->rhnode, 117619e3a9c9SNikolay Aleksandrov br_mdb_rht_params); 117719e3a9c9SNikolay Aleksandrov if (err) { 117819e3a9c9SNikolay Aleksandrov kfree(mp); 117919e3a9c9SNikolay Aleksandrov mp = ERR_PTR(err); 118019e3a9c9SNikolay Aleksandrov } else { 118119e3a9c9SNikolay Aleksandrov hlist_add_head_rcu(&mp->mdb_node, &br->mdb_list); 118219e3a9c9SNikolay Aleksandrov } 11831faabf2aSEric Dumazet 1184eb1d1641SHerbert Xu return mp; 1185eb1d1641SHerbert Xu } 1186eb1d1641SHerbert Xu 11878b671779SNikolay Aleksandrov static void br_multicast_group_src_expired(struct timer_list *t) 11888b671779SNikolay Aleksandrov { 11898b671779SNikolay Aleksandrov struct net_bridge_group_src *src = from_timer(src, t, timer); 11908b671779SNikolay Aleksandrov struct net_bridge_port_group *pg; 11918b671779SNikolay Aleksandrov struct net_bridge *br = src->br; 11928b671779SNikolay Aleksandrov 11938b671779SNikolay Aleksandrov spin_lock(&br->multicast_lock); 11948b671779SNikolay Aleksandrov if (hlist_unhashed(&src->node) || !netif_running(br->dev) || 11958b671779SNikolay Aleksandrov timer_pending(&src->timer)) 11968b671779SNikolay Aleksandrov goto out; 11978b671779SNikolay Aleksandrov 11988b671779SNikolay Aleksandrov pg = src->pg; 11998b671779SNikolay Aleksandrov if (pg->filter_mode == MCAST_INCLUDE) { 1200d5a10222SNikolay Aleksandrov br_multicast_del_group_src(src, false); 12018b671779SNikolay Aleksandrov if (!hlist_empty(&pg->src_list)) 12028b671779SNikolay Aleksandrov goto out; 12038b671779SNikolay Aleksandrov br_multicast_find_del_pg(br, pg); 12049116ffbfSNikolay Aleksandrov } else { 12059116ffbfSNikolay Aleksandrov br_multicast_fwd_src_handle(src); 12068b671779SNikolay Aleksandrov } 12079116ffbfSNikolay Aleksandrov 12088b671779SNikolay Aleksandrov out: 12098b671779SNikolay Aleksandrov spin_unlock(&br->multicast_lock); 12108b671779SNikolay Aleksandrov } 12118b671779SNikolay Aleksandrov 1212474ddb37SNikolay Aleksandrov struct net_bridge_group_src * 12138b671779SNikolay Aleksandrov br_multicast_find_group_src(struct net_bridge_port_group *pg, struct br_ip *ip) 12148b671779SNikolay Aleksandrov { 12158b671779SNikolay Aleksandrov struct net_bridge_group_src *ent; 12168b671779SNikolay Aleksandrov 12178b671779SNikolay Aleksandrov switch (ip->proto) { 12188b671779SNikolay Aleksandrov case htons(ETH_P_IP): 12198b671779SNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 1220deb96566SNikolay Aleksandrov if (ip->src.ip4 == ent->addr.src.ip4) 12218b671779SNikolay Aleksandrov return ent; 12228b671779SNikolay Aleksandrov break; 12238b671779SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 12248b671779SNikolay Aleksandrov case htons(ETH_P_IPV6): 12258b671779SNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 1226deb96566SNikolay Aleksandrov if (!ipv6_addr_cmp(&ent->addr.src.ip6, &ip->src.ip6)) 12278b671779SNikolay Aleksandrov return ent; 12288b671779SNikolay Aleksandrov break; 12298b671779SNikolay Aleksandrov #endif 12308b671779SNikolay Aleksandrov } 12318b671779SNikolay Aleksandrov 12328b671779SNikolay Aleksandrov return NULL; 12338b671779SNikolay Aleksandrov } 12348b671779SNikolay Aleksandrov 1235*fd0c6961SIdo Schimmel struct net_bridge_group_src * 12368b671779SNikolay Aleksandrov br_multicast_new_group_src(struct net_bridge_port_group *pg, struct br_ip *src_ip) 12378b671779SNikolay Aleksandrov { 12388b671779SNikolay Aleksandrov struct net_bridge_group_src *grp_src; 12398b671779SNikolay Aleksandrov 12408b671779SNikolay Aleksandrov if (unlikely(pg->src_ents >= PG_SRC_ENT_LIMIT)) 12418b671779SNikolay Aleksandrov return NULL; 12428b671779SNikolay Aleksandrov 12438b671779SNikolay Aleksandrov switch (src_ip->proto) { 12448b671779SNikolay Aleksandrov case htons(ETH_P_IP): 1245deb96566SNikolay Aleksandrov if (ipv4_is_zeronet(src_ip->src.ip4) || 1246deb96566SNikolay Aleksandrov ipv4_is_multicast(src_ip->src.ip4)) 12478b671779SNikolay Aleksandrov return NULL; 12488b671779SNikolay Aleksandrov break; 12498b671779SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 12508b671779SNikolay Aleksandrov case htons(ETH_P_IPV6): 1251deb96566SNikolay Aleksandrov if (ipv6_addr_any(&src_ip->src.ip6) || 1252deb96566SNikolay Aleksandrov ipv6_addr_is_multicast(&src_ip->src.ip6)) 12538b671779SNikolay Aleksandrov return NULL; 12548b671779SNikolay Aleksandrov break; 12558b671779SNikolay Aleksandrov #endif 12568b671779SNikolay Aleksandrov } 12578b671779SNikolay Aleksandrov 12588b671779SNikolay Aleksandrov grp_src = kzalloc(sizeof(*grp_src), GFP_ATOMIC); 12598b671779SNikolay Aleksandrov if (unlikely(!grp_src)) 12608b671779SNikolay Aleksandrov return NULL; 12618b671779SNikolay Aleksandrov 12628b671779SNikolay Aleksandrov grp_src->pg = pg; 1263085b53c8SNikolay Aleksandrov grp_src->br = pg->key.port->br; 12648b671779SNikolay Aleksandrov grp_src->addr = *src_ip; 1265e12cec65SNikolay Aleksandrov grp_src->mcast_gc.destroy = br_multicast_destroy_group_src; 12668b671779SNikolay Aleksandrov timer_setup(&grp_src->timer, br_multicast_group_src_expired, 0); 12678b671779SNikolay Aleksandrov 12688b671779SNikolay Aleksandrov hlist_add_head_rcu(&grp_src->node, &pg->src_list); 12698b671779SNikolay Aleksandrov pg->src_ents++; 12708b671779SNikolay Aleksandrov 12718b671779SNikolay Aleksandrov return grp_src; 12728b671779SNikolay Aleksandrov } 12738b671779SNikolay Aleksandrov 1274cfd56754SCong Wang struct net_bridge_port_group *br_multicast_new_port_group( 1275cfd56754SCong Wang struct net_bridge_port *port, 1276f86c3e2cSIdo Schimmel const struct br_ip *group, 1277ccb1c31aSAmerigo Wang struct net_bridge_port_group __rcu *next, 12786db6f0eaSFelix Fietkau unsigned char flags, 12798b671779SNikolay Aleksandrov const unsigned char *src, 12808f8cb77eSNikolay Aleksandrov u8 filter_mode, 12818f8cb77eSNikolay Aleksandrov u8 rt_protocol) 1282cfd56754SCong Wang { 1283cfd56754SCong Wang struct net_bridge_port_group *p; 1284cfd56754SCong Wang 1285cfd56754SCong Wang p = kzalloc(sizeof(*p), GFP_ATOMIC); 1286cfd56754SCong Wang if (unlikely(!p)) 1287cfd56754SCong Wang return NULL; 1288cfd56754SCong Wang 1289085b53c8SNikolay Aleksandrov p->key.addr = *group; 1290085b53c8SNikolay Aleksandrov p->key.port = port; 12919d06b6d8SElad Raz p->flags = flags; 12928b671779SNikolay Aleksandrov p->filter_mode = filter_mode; 12938f8cb77eSNikolay Aleksandrov p->rt_protocol = rt_protocol; 12945b163288SNikolay Aleksandrov p->eht_host_tree = RB_ROOT; 1295baa74d39SNikolay Aleksandrov p->eht_set_tree = RB_ROOT; 1296e12cec65SNikolay Aleksandrov p->mcast_gc.destroy = br_multicast_destroy_port_group; 12978b671779SNikolay Aleksandrov INIT_HLIST_HEAD(&p->src_list); 1298085b53c8SNikolay Aleksandrov 1299085b53c8SNikolay Aleksandrov if (!br_multicast_is_star_g(group) && 1300085b53c8SNikolay Aleksandrov rhashtable_lookup_insert_fast(&port->br->sg_port_tbl, &p->rhnode, 1301085b53c8SNikolay Aleksandrov br_sg_port_rht_params)) { 1302085b53c8SNikolay Aleksandrov kfree(p); 1303085b53c8SNikolay Aleksandrov return NULL; 1304085b53c8SNikolay Aleksandrov } 1305085b53c8SNikolay Aleksandrov 1306eca2a43bSstephen hemminger rcu_assign_pointer(p->next, next); 130788c1f37fSAllen Pais timer_setup(&p->timer, br_multicast_port_group_expired, 0); 130842c11ccfSNikolay Aleksandrov timer_setup(&p->rexmit_timer, br_multicast_port_group_rexmit, 0); 130942c11ccfSNikolay Aleksandrov hlist_add_head(&p->mglist, &port->mglist); 13106db6f0eaSFelix Fietkau 13116db6f0eaSFelix Fietkau if (src) 13126db6f0eaSFelix Fietkau memcpy(p->eth_addr, src, ETH_ALEN); 13136db6f0eaSFelix Fietkau else 13141bfe45f4SMao Wenan eth_broadcast_addr(p->eth_addr); 13156db6f0eaSFelix Fietkau 1316cfd56754SCong Wang return p; 1317cfd56754SCong Wang } 1318cfd56754SCong Wang 131958d913a3SNikolay Aleksandrov void br_multicast_host_join(const struct net_bridge_mcast *brmctx, 132058d913a3SNikolay Aleksandrov struct net_bridge_mdb_entry *mp, bool notify) 13211bc844eeSNikolay Aleksandrov { 13221bc844eeSNikolay Aleksandrov if (!mp->host_joined) { 13231bc844eeSNikolay Aleksandrov mp->host_joined = true; 1324094b82fdSNikolay Aleksandrov if (br_multicast_is_star_g(&mp->addr)) 1325094b82fdSNikolay Aleksandrov br_multicast_star_g_host_state(mp); 13261bc844eeSNikolay Aleksandrov if (notify) 132781f19838SNikolay Aleksandrov br_mdb_notify(mp->br->dev, mp, NULL, RTM_NEWMDB); 13281bc844eeSNikolay Aleksandrov } 1329955062b0SNikolay Aleksandrov 1330955062b0SNikolay Aleksandrov if (br_group_is_l2(&mp->addr)) 1331955062b0SNikolay Aleksandrov return; 1332955062b0SNikolay Aleksandrov 133358d913a3SNikolay Aleksandrov mod_timer(&mp->timer, jiffies + brmctx->multicast_membership_interval); 13341bc844eeSNikolay Aleksandrov } 13351bc844eeSNikolay Aleksandrov 13361bc844eeSNikolay Aleksandrov void br_multicast_host_leave(struct net_bridge_mdb_entry *mp, bool notify) 13371bc844eeSNikolay Aleksandrov { 13381bc844eeSNikolay Aleksandrov if (!mp->host_joined) 13391bc844eeSNikolay Aleksandrov return; 13401bc844eeSNikolay Aleksandrov 13411bc844eeSNikolay Aleksandrov mp->host_joined = false; 1342094b82fdSNikolay Aleksandrov if (br_multicast_is_star_g(&mp->addr)) 1343094b82fdSNikolay Aleksandrov br_multicast_star_g_host_state(mp); 13441bc844eeSNikolay Aleksandrov if (notify) 134581f19838SNikolay Aleksandrov br_mdb_notify(mp->br->dev, mp, NULL, RTM_DELMDB); 13461bc844eeSNikolay Aleksandrov } 13471bc844eeSNikolay Aleksandrov 1348b0812368SNikolay Aleksandrov static struct net_bridge_port_group * 1349adc47037SNikolay Aleksandrov __br_multicast_add_group(struct net_bridge_mcast *brmctx, 1350adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 13516db6f0eaSFelix Fietkau struct br_ip *group, 13528b671779SNikolay Aleksandrov const unsigned char *src, 13530436862eSNikolay Aleksandrov u8 filter_mode, 13549116ffbfSNikolay Aleksandrov bool igmpv2_mldv1, 13559116ffbfSNikolay Aleksandrov bool blocked) 1356eb1d1641SHerbert Xu { 1357e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 1358b0812368SNikolay Aleksandrov struct net_bridge_port_group *p = NULL; 13595e923585SNikolay Aleksandrov struct net_bridge_mdb_entry *mp; 1360454594f3SLinus Lüssing unsigned long now = jiffies; 1361eb1d1641SHerbert Xu 13624cdd0d10SNikolay Aleksandrov if (!br_multicast_ctx_should_use(brmctx, pmctx)) 1363eb1d1641SHerbert Xu goto out; 1364eb1d1641SHerbert Xu 1365adc47037SNikolay Aleksandrov mp = br_multicast_new_group(brmctx->br, group); 13664c0833bcSTobias Klauser if (IS_ERR(mp)) 13671697291dSXu Wang return ERR_CAST(mp); 1368eb1d1641SHerbert Xu 1369adc47037SNikolay Aleksandrov if (!pmctx) { 137058d913a3SNikolay Aleksandrov br_multicast_host_join(brmctx, mp, true); 1371eb1d1641SHerbert Xu goto out; 1372eb1d1641SHerbert Xu } 1373eb1d1641SHerbert Xu 1374e8051688SEric Dumazet for (pp = &mp->ports; 1375adc47037SNikolay Aleksandrov (p = mlock_dereference(*pp, brmctx->br)) != NULL; 1376e8051688SEric Dumazet pp = &p->next) { 1377adc47037SNikolay Aleksandrov if (br_port_group_equal(p, pmctx->port, src)) 1378454594f3SLinus Lüssing goto found; 1379adc47037SNikolay Aleksandrov if ((unsigned long)p->key.port < (unsigned long)pmctx->port) 1380eb1d1641SHerbert Xu break; 1381eb1d1641SHerbert Xu } 1382eb1d1641SHerbert Xu 1383adc47037SNikolay Aleksandrov p = br_multicast_new_port_group(pmctx->port, group, *pp, 0, src, 1384955062b0SNikolay Aleksandrov filter_mode, RTPROT_KERNEL); 1385b0812368SNikolay Aleksandrov if (unlikely(!p)) { 1386b0812368SNikolay Aleksandrov p = ERR_PTR(-ENOMEM); 1387b0812368SNikolay Aleksandrov goto out; 1388b0812368SNikolay Aleksandrov } 1389eb1d1641SHerbert Xu rcu_assign_pointer(*pp, p); 13909116ffbfSNikolay Aleksandrov if (blocked) 13919116ffbfSNikolay Aleksandrov p->flags |= MDB_PG_FLAGS_BLOCKED; 1392adc47037SNikolay Aleksandrov br_mdb_notify(brmctx->br->dev, mp, p, RTM_NEWMDB); 1393eb1d1641SHerbert Xu 1394454594f3SLinus Lüssing found: 13950436862eSNikolay Aleksandrov if (igmpv2_mldv1) 1396d3d065c0SNikolay Aleksandrov mod_timer(&p->timer, 1397adc47037SNikolay Aleksandrov now + brmctx->multicast_membership_interval); 139881f19838SNikolay Aleksandrov 1399eb1d1641SHerbert Xu out: 1400b0812368SNikolay Aleksandrov return p; 1401b0812368SNikolay Aleksandrov } 1402eb1d1641SHerbert Xu 1403adc47037SNikolay Aleksandrov static int br_multicast_add_group(struct net_bridge_mcast *brmctx, 1404adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 1405b0812368SNikolay Aleksandrov struct br_ip *group, 1406b0812368SNikolay Aleksandrov const unsigned char *src, 1407b0812368SNikolay Aleksandrov u8 filter_mode, 1408b0812368SNikolay Aleksandrov bool igmpv2_mldv1) 1409b0812368SNikolay Aleksandrov { 1410b0812368SNikolay Aleksandrov struct net_bridge_port_group *pg; 1411b0812368SNikolay Aleksandrov int err; 1412b0812368SNikolay Aleksandrov 1413adc47037SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 1414adc47037SNikolay Aleksandrov pg = __br_multicast_add_group(brmctx, pmctx, group, src, filter_mode, 14159116ffbfSNikolay Aleksandrov igmpv2_mldv1, false); 1416b0812368SNikolay Aleksandrov /* NULL is considered valid for host joined groups */ 14178d21c882SJiapeng Zhong err = PTR_ERR_OR_ZERO(pg); 1418adc47037SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 1419b0812368SNikolay Aleksandrov 1420eb1d1641SHerbert Xu return err; 1421eb1d1641SHerbert Xu } 1422eb1d1641SHerbert Xu 1423adc47037SNikolay Aleksandrov static int br_ip4_multicast_add_group(struct net_bridge_mcast *brmctx, 1424adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 1425b0e9a30dSVlad Yasevich __be32 group, 14266db6f0eaSFelix Fietkau __u16 vid, 14278b671779SNikolay Aleksandrov const unsigned char *src, 14288b671779SNikolay Aleksandrov bool igmpv2) 14298ef2a9a5SYOSHIFUJI Hideaki { 14308ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 14318b671779SNikolay Aleksandrov u8 filter_mode; 14328ef2a9a5SYOSHIFUJI Hideaki 14338ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 14348ef2a9a5SYOSHIFUJI Hideaki return 0; 14358ef2a9a5SYOSHIFUJI Hideaki 14361515a63fSNikolay Aleksandrov memset(&br_group, 0, sizeof(br_group)); 1437eab3227bSNikolay Aleksandrov br_group.dst.ip4 = group; 14388ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 1439b0e9a30dSVlad Yasevich br_group.vid = vid; 14408b671779SNikolay Aleksandrov filter_mode = igmpv2 ? MCAST_EXCLUDE : MCAST_INCLUDE; 14418ef2a9a5SYOSHIFUJI Hideaki 1442adc47037SNikolay Aleksandrov return br_multicast_add_group(brmctx, pmctx, &br_group, src, 1443adc47037SNikolay Aleksandrov filter_mode, igmpv2); 14448ef2a9a5SYOSHIFUJI Hideaki } 14458ef2a9a5SYOSHIFUJI Hideaki 1446dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 1447adc47037SNikolay Aleksandrov static int br_ip6_multicast_add_group(struct net_bridge_mcast *brmctx, 1448adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 1449b0e9a30dSVlad Yasevich const struct in6_addr *group, 14506db6f0eaSFelix Fietkau __u16 vid, 14518b671779SNikolay Aleksandrov const unsigned char *src, 14528b671779SNikolay Aleksandrov bool mldv1) 145308b202b6SYOSHIFUJI Hideaki { 145408b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 14558b671779SNikolay Aleksandrov u8 filter_mode; 145608b202b6SYOSHIFUJI Hideaki 14573c3769e6SLinus Lüssing if (ipv6_addr_is_ll_all_nodes(group)) 145808b202b6SYOSHIFUJI Hideaki return 0; 145908b202b6SYOSHIFUJI Hideaki 146019e3a9c9SNikolay Aleksandrov memset(&br_group, 0, sizeof(br_group)); 1461eab3227bSNikolay Aleksandrov br_group.dst.ip6 = *group; 14629cc6e0c4SLinus Lüssing br_group.proto = htons(ETH_P_IPV6); 1463b0e9a30dSVlad Yasevich br_group.vid = vid; 14648b671779SNikolay Aleksandrov filter_mode = mldv1 ? MCAST_EXCLUDE : MCAST_INCLUDE; 146508b202b6SYOSHIFUJI Hideaki 1466adc47037SNikolay Aleksandrov return br_multicast_add_group(brmctx, pmctx, &br_group, src, 1467adc47037SNikolay Aleksandrov filter_mode, mldv1); 146808b202b6SYOSHIFUJI Hideaki } 146908b202b6SYOSHIFUJI Hideaki #endif 147008b202b6SYOSHIFUJI Hideaki 1471ed2d3597SLinus Lüssing static bool br_multicast_rport_del(struct hlist_node *rlist) 1472ed2d3597SLinus Lüssing { 1473ed2d3597SLinus Lüssing if (hlist_unhashed(rlist)) 1474ed2d3597SLinus Lüssing return false; 1475ed2d3597SLinus Lüssing 1476ed2d3597SLinus Lüssing hlist_del_init_rcu(rlist); 1477ed2d3597SLinus Lüssing return true; 1478ed2d3597SLinus Lüssing } 1479ed2d3597SLinus Lüssing 1480adc47037SNikolay Aleksandrov static bool br_ip4_multicast_rport_del(struct net_bridge_mcast_port *pmctx) 1481ed2d3597SLinus Lüssing { 1482adc47037SNikolay Aleksandrov return br_multicast_rport_del(&pmctx->ip4_rlist); 1483ed2d3597SLinus Lüssing } 1484ed2d3597SLinus Lüssing 1485adc47037SNikolay Aleksandrov static bool br_ip6_multicast_rport_del(struct net_bridge_mcast_port *pmctx) 1486a3c02e76SLinus Lüssing { 1487a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1488adc47037SNikolay Aleksandrov return br_multicast_rport_del(&pmctx->ip6_rlist); 1489a3c02e76SLinus Lüssing #else 1490a3c02e76SLinus Lüssing return false; 1491a3c02e76SLinus Lüssing #endif 1492a3c02e76SLinus Lüssing } 1493a3c02e76SLinus Lüssing 14949632233eSNikolay Aleksandrov static void br_multicast_router_expired(struct net_bridge_mcast_port *pmctx, 1495ee5fb222SLinus Lüssing struct timer_list *t, 1496ee5fb222SLinus Lüssing struct hlist_node *rlist) 1497eb1d1641SHerbert Xu { 14989632233eSNikolay Aleksandrov struct net_bridge *br = pmctx->port->br; 1499ed2d3597SLinus Lüssing bool del; 1500eb1d1641SHerbert Xu 1501eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 15029632233eSNikolay Aleksandrov if (pmctx->multicast_router == MDB_RTR_TYPE_DISABLED || 15039632233eSNikolay Aleksandrov pmctx->multicast_router == MDB_RTR_TYPE_PERM || 1504ee5fb222SLinus Lüssing timer_pending(t)) 1505eb1d1641SHerbert Xu goto out; 1506eb1d1641SHerbert Xu 1507ed2d3597SLinus Lüssing del = br_multicast_rport_del(rlist); 1508adc47037SNikolay Aleksandrov br_multicast_rport_del_notify(pmctx, del); 1509eb1d1641SHerbert Xu out: 1510eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1511eb1d1641SHerbert Xu } 1512eb1d1641SHerbert Xu 1513ee5fb222SLinus Lüssing static void br_ip4_multicast_router_expired(struct timer_list *t) 1514ee5fb222SLinus Lüssing { 15159632233eSNikolay Aleksandrov struct net_bridge_mcast_port *pmctx = from_timer(pmctx, t, 15169632233eSNikolay Aleksandrov ip4_mc_router_timer); 1517ee5fb222SLinus Lüssing 15189632233eSNikolay Aleksandrov br_multicast_router_expired(pmctx, t, &pmctx->ip4_rlist); 1519ee5fb222SLinus Lüssing } 1520ee5fb222SLinus Lüssing 1521a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1522a3c02e76SLinus Lüssing static void br_ip6_multicast_router_expired(struct timer_list *t) 1523a3c02e76SLinus Lüssing { 15249632233eSNikolay Aleksandrov struct net_bridge_mcast_port *pmctx = from_timer(pmctx, t, 15259632233eSNikolay Aleksandrov ip6_mc_router_timer); 1526a3c02e76SLinus Lüssing 15279632233eSNikolay Aleksandrov br_multicast_router_expired(pmctx, t, &pmctx->ip6_rlist); 1528a3c02e76SLinus Lüssing } 1529a3c02e76SLinus Lüssing #endif 1530a3c02e76SLinus Lüssing 153177041420SYotam Gigi static void br_mc_router_state_change(struct net_bridge *p, 153277041420SYotam Gigi bool is_mc_router) 153377041420SYotam Gigi { 153477041420SYotam Gigi struct switchdev_attr attr = { 153577041420SYotam Gigi .orig_dev = p->dev, 153677041420SYotam Gigi .id = SWITCHDEV_ATTR_ID_BRIDGE_MROUTER, 153777041420SYotam Gigi .flags = SWITCHDEV_F_DEFER, 153877041420SYotam Gigi .u.mrouter = is_mc_router, 153977041420SYotam Gigi }; 154077041420SYotam Gigi 1541dcbdf135SVladimir Oltean switchdev_port_attr_set(p->dev, &attr, NULL); 154277041420SYotam Gigi } 154377041420SYotam Gigi 1544d3d065c0SNikolay Aleksandrov static void br_multicast_local_router_expired(struct net_bridge_mcast *brmctx, 1545ee5fb222SLinus Lüssing struct timer_list *timer) 1546eb1d1641SHerbert Xu { 1547d3d065c0SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 1548d3d065c0SNikolay Aleksandrov if (brmctx->multicast_router == MDB_RTR_TYPE_DISABLED || 1549d3d065c0SNikolay Aleksandrov brmctx->multicast_router == MDB_RTR_TYPE_PERM || 1550d3d065c0SNikolay Aleksandrov br_ip4_multicast_is_router(brmctx) || 1551d3d065c0SNikolay Aleksandrov br_ip6_multicast_is_router(brmctx)) 155277041420SYotam Gigi goto out; 155377041420SYotam Gigi 1554d3d065c0SNikolay Aleksandrov br_mc_router_state_change(brmctx->br, false); 155577041420SYotam Gigi out: 1556d3d065c0SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 1557eb1d1641SHerbert Xu } 1558eb1d1641SHerbert Xu 1559ee5fb222SLinus Lüssing static void br_ip4_multicast_local_router_expired(struct timer_list *t) 1560ee5fb222SLinus Lüssing { 1561d3d065c0SNikolay Aleksandrov struct net_bridge_mcast *brmctx = from_timer(brmctx, t, 1562d3d065c0SNikolay Aleksandrov ip4_mc_router_timer); 1563ee5fb222SLinus Lüssing 1564d3d065c0SNikolay Aleksandrov br_multicast_local_router_expired(brmctx, t); 1565ee5fb222SLinus Lüssing } 1566ee5fb222SLinus Lüssing 1567a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1568a3c02e76SLinus Lüssing static void br_ip6_multicast_local_router_expired(struct timer_list *t) 1569a3c02e76SLinus Lüssing { 1570d3d065c0SNikolay Aleksandrov struct net_bridge_mcast *brmctx = from_timer(brmctx, t, 1571d3d065c0SNikolay Aleksandrov ip6_mc_router_timer); 1572a3c02e76SLinus Lüssing 1573d3d065c0SNikolay Aleksandrov br_multicast_local_router_expired(brmctx, t); 1574a3c02e76SLinus Lüssing } 1575a3c02e76SLinus Lüssing #endif 1576a3c02e76SLinus Lüssing 1577d3d065c0SNikolay Aleksandrov static void br_multicast_querier_expired(struct net_bridge_mcast *brmctx, 157890010b36SLinus Lüssing struct bridge_mcast_own_query *query) 1579c83b8fabSHerbert Xu { 1580d3d065c0SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 1581d3d065c0SNikolay Aleksandrov if (!netif_running(brmctx->br->dev) || 15824cdd0d10SNikolay Aleksandrov br_multicast_ctx_vlan_global_disabled(brmctx) || 1583d3d065c0SNikolay Aleksandrov !br_opt_get(brmctx->br, BROPT_MULTICAST_ENABLED)) 1584c83b8fabSHerbert Xu goto out; 1585c83b8fabSHerbert Xu 1586adc47037SNikolay Aleksandrov br_multicast_start_querier(brmctx, query); 1587c83b8fabSHerbert Xu 1588c83b8fabSHerbert Xu out: 1589d3d065c0SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 1590c83b8fabSHerbert Xu } 1591c83b8fabSHerbert Xu 159288c1f37fSAllen Pais static void br_ip4_multicast_querier_expired(struct timer_list *t) 1593cc0fdd80SLinus Lüssing { 1594d3d065c0SNikolay Aleksandrov struct net_bridge_mcast *brmctx = from_timer(brmctx, t, 1595d3d065c0SNikolay Aleksandrov ip4_other_query.timer); 1596cc0fdd80SLinus Lüssing 1597d3d065c0SNikolay Aleksandrov br_multicast_querier_expired(brmctx, &brmctx->ip4_own_query); 1598cc0fdd80SLinus Lüssing } 1599cc0fdd80SLinus Lüssing 1600cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 160188c1f37fSAllen Pais static void br_ip6_multicast_querier_expired(struct timer_list *t) 1602cc0fdd80SLinus Lüssing { 1603d3d065c0SNikolay Aleksandrov struct net_bridge_mcast *brmctx = from_timer(brmctx, t, 1604d3d065c0SNikolay Aleksandrov ip6_other_query.timer); 1605cc0fdd80SLinus Lüssing 1606d3d065c0SNikolay Aleksandrov br_multicast_querier_expired(brmctx, &brmctx->ip6_own_query); 1607cc0fdd80SLinus Lüssing } 1608cc0fdd80SLinus Lüssing #endif 1609cc0fdd80SLinus Lüssing 1610adc47037SNikolay Aleksandrov static void br_multicast_select_own_querier(struct net_bridge_mcast *brmctx, 1611dc4eb53aSLinus Lüssing struct br_ip *ip, 1612dc4eb53aSLinus Lüssing struct sk_buff *skb) 1613dc4eb53aSLinus Lüssing { 1614dc4eb53aSLinus Lüssing if (ip->proto == htons(ETH_P_IP)) 1615d3d065c0SNikolay Aleksandrov brmctx->ip4_querier.addr.src.ip4 = ip_hdr(skb)->saddr; 1616dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1617dc4eb53aSLinus Lüssing else 1618d3d065c0SNikolay Aleksandrov brmctx->ip6_querier.addr.src.ip6 = ipv6_hdr(skb)->saddr; 1619dc4eb53aSLinus Lüssing #endif 1620dc4eb53aSLinus Lüssing } 1621dc4eb53aSLinus Lüssing 1622adc47037SNikolay Aleksandrov static void __br_multicast_send_query(struct net_bridge_mcast *brmctx, 1623adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 1624438ef2d0SNikolay Aleksandrov struct net_bridge_port_group *pg, 1625438ef2d0SNikolay Aleksandrov struct br_ip *ip_dst, 1626438ef2d0SNikolay Aleksandrov struct br_ip *group, 1627438ef2d0SNikolay Aleksandrov bool with_srcs, 162842c11ccfSNikolay Aleksandrov u8 sflag, 162942c11ccfSNikolay Aleksandrov bool *need_rexmit) 1630eb1d1641SHerbert Xu { 1631438ef2d0SNikolay Aleksandrov bool over_lmqt = !!sflag; 1632eb1d1641SHerbert Xu struct sk_buff *skb; 16331080ab95SNikolay Aleksandrov u8 igmp_type; 1634eb1d1641SHerbert Xu 1635cb486ce9SNikolay Aleksandrov if (!br_multicast_ctx_should_use(brmctx, pmctx) || 1636cb486ce9SNikolay Aleksandrov !br_multicast_ctx_matches_vlan_snooping(brmctx)) 1637615cc23eSNikolay Aleksandrov return; 1638615cc23eSNikolay Aleksandrov 1639438ef2d0SNikolay Aleksandrov again_under_lmqt: 1640615cc23eSNikolay Aleksandrov skb = br_multicast_alloc_query(brmctx, pmctx, pg, ip_dst, group, 1641615cc23eSNikolay Aleksandrov with_srcs, over_lmqt, sflag, &igmp_type, 164242c11ccfSNikolay Aleksandrov need_rexmit); 1643eb1d1641SHerbert Xu if (!skb) 16448ef2a9a5SYOSHIFUJI Hideaki return; 1645eb1d1641SHerbert Xu 1646adc47037SNikolay Aleksandrov if (pmctx) { 1647adc47037SNikolay Aleksandrov skb->dev = pmctx->port->dev; 1648adc47037SNikolay Aleksandrov br_multicast_count(brmctx->br, pmctx->port, skb, igmp_type, 16491080ab95SNikolay Aleksandrov BR_MCAST_DIR_TX); 165029a26a56SEric W. Biederman NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, 1651adc47037SNikolay Aleksandrov dev_net(pmctx->port->dev), NULL, skb, NULL, skb->dev, 1652f0b4eeceSLinus Lüssing br_dev_queue_push_xmit); 1653438ef2d0SNikolay Aleksandrov 1654438ef2d0SNikolay Aleksandrov if (over_lmqt && with_srcs && sflag) { 1655438ef2d0SNikolay Aleksandrov over_lmqt = false; 1656438ef2d0SNikolay Aleksandrov goto again_under_lmqt; 1657438ef2d0SNikolay Aleksandrov } 1658dc4eb53aSLinus Lüssing } else { 1659adc47037SNikolay Aleksandrov br_multicast_select_own_querier(brmctx, group, skb); 1660adc47037SNikolay Aleksandrov br_multicast_count(brmctx->br, NULL, skb, igmp_type, 16611080ab95SNikolay Aleksandrov BR_MCAST_DIR_RX); 1662eb1d1641SHerbert Xu netif_rx(skb); 16638ef2a9a5SYOSHIFUJI Hideaki } 1664dc4eb53aSLinus Lüssing } 1665eb1d1641SHerbert Xu 166667b746f9SNikolay Aleksandrov static void br_multicast_read_querier(const struct bridge_mcast_querier *querier, 166767b746f9SNikolay Aleksandrov struct bridge_mcast_querier *dest) 166867b746f9SNikolay Aleksandrov { 166967b746f9SNikolay Aleksandrov unsigned int seq; 167067b746f9SNikolay Aleksandrov 167167b746f9SNikolay Aleksandrov memset(dest, 0, sizeof(*dest)); 167267b746f9SNikolay Aleksandrov do { 167367b746f9SNikolay Aleksandrov seq = read_seqcount_begin(&querier->seq); 167467b746f9SNikolay Aleksandrov dest->port_ifidx = querier->port_ifidx; 167567b746f9SNikolay Aleksandrov memcpy(&dest->addr, &querier->addr, sizeof(struct br_ip)); 167667b746f9SNikolay Aleksandrov } while (read_seqcount_retry(&querier->seq, seq)); 167767b746f9SNikolay Aleksandrov } 167867b746f9SNikolay Aleksandrov 167967b746f9SNikolay Aleksandrov static void br_multicast_update_querier(struct net_bridge_mcast *brmctx, 168067b746f9SNikolay Aleksandrov struct bridge_mcast_querier *querier, 168167b746f9SNikolay Aleksandrov int ifindex, 168267b746f9SNikolay Aleksandrov struct br_ip *saddr) 168367b746f9SNikolay Aleksandrov { 168467b746f9SNikolay Aleksandrov write_seqcount_begin(&querier->seq); 168567b746f9SNikolay Aleksandrov querier->port_ifidx = ifindex; 168667b746f9SNikolay Aleksandrov memcpy(&querier->addr, saddr, sizeof(*saddr)); 168767b746f9SNikolay Aleksandrov write_seqcount_end(&querier->seq); 168867b746f9SNikolay Aleksandrov } 168967b746f9SNikolay Aleksandrov 1690adc47037SNikolay Aleksandrov static void br_multicast_send_query(struct net_bridge_mcast *brmctx, 1691adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 169290010b36SLinus Lüssing struct bridge_mcast_own_query *own_query) 16938ef2a9a5SYOSHIFUJI Hideaki { 169490010b36SLinus Lüssing struct bridge_mcast_other_query *other_query = NULL; 169567b746f9SNikolay Aleksandrov struct bridge_mcast_querier *querier; 16965e923585SNikolay Aleksandrov struct br_ip br_group; 16975e923585SNikolay Aleksandrov unsigned long time; 16988ef2a9a5SYOSHIFUJI Hideaki 16994cdd0d10SNikolay Aleksandrov if (!br_multicast_ctx_should_use(brmctx, pmctx) || 1700adc47037SNikolay Aleksandrov !br_opt_get(brmctx->br, BROPT_MULTICAST_ENABLED) || 17014d5b4e84SNikolay Aleksandrov !brmctx->multicast_querier) 17028ef2a9a5SYOSHIFUJI Hideaki return; 17038ef2a9a5SYOSHIFUJI Hideaki 1704eab3227bSNikolay Aleksandrov memset(&br_group.dst, 0, sizeof(br_group.dst)); 17058ef2a9a5SYOSHIFUJI Hideaki 1706adc47037SNikolay Aleksandrov if (pmctx ? (own_query == &pmctx->ip4_own_query) : 1707d3d065c0SNikolay Aleksandrov (own_query == &brmctx->ip4_own_query)) { 170867b746f9SNikolay Aleksandrov querier = &brmctx->ip4_querier; 1709d3d065c0SNikolay Aleksandrov other_query = &brmctx->ip4_other_query; 171008b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 1711dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 1712cc0fdd80SLinus Lüssing } else { 171367b746f9SNikolay Aleksandrov querier = &brmctx->ip6_querier; 1714d3d065c0SNikolay Aleksandrov other_query = &brmctx->ip6_other_query; 171508b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 171608b202b6SYOSHIFUJI Hideaki #endif 1717eb1d1641SHerbert Xu } 1718eb1d1641SHerbert Xu 171990010b36SLinus Lüssing if (!other_query || timer_pending(&other_query->timer)) 1720cc0fdd80SLinus Lüssing return; 1721cc0fdd80SLinus Lüssing 172267b746f9SNikolay Aleksandrov /* we're about to select ourselves as querier */ 172367b746f9SNikolay Aleksandrov if (!pmctx && querier->port_ifidx) { 172467b746f9SNikolay Aleksandrov struct br_ip zeroip = {}; 172567b746f9SNikolay Aleksandrov 172667b746f9SNikolay Aleksandrov br_multicast_update_querier(brmctx, querier, 0, &zeroip); 172767b746f9SNikolay Aleksandrov } 172867b746f9SNikolay Aleksandrov 1729adc47037SNikolay Aleksandrov __br_multicast_send_query(brmctx, pmctx, NULL, NULL, &br_group, false, 1730adc47037SNikolay Aleksandrov 0, NULL); 1731cc0fdd80SLinus Lüssing 1732cc0fdd80SLinus Lüssing time = jiffies; 1733d3d065c0SNikolay Aleksandrov time += own_query->startup_sent < brmctx->multicast_startup_query_count ? 1734d3d065c0SNikolay Aleksandrov brmctx->multicast_startup_query_interval : 1735d3d065c0SNikolay Aleksandrov brmctx->multicast_query_interval; 173690010b36SLinus Lüssing mod_timer(&own_query->timer, time); 1737cc0fdd80SLinus Lüssing } 1738cc0fdd80SLinus Lüssing 173990010b36SLinus Lüssing static void 17409632233eSNikolay Aleksandrov br_multicast_port_query_expired(struct net_bridge_mcast_port *pmctx, 174190010b36SLinus Lüssing struct bridge_mcast_own_query *query) 1742eb1d1641SHerbert Xu { 17439632233eSNikolay Aleksandrov struct net_bridge *br = pmctx->port->br; 17444cdd0d10SNikolay Aleksandrov struct net_bridge_mcast *brmctx; 1745eb1d1641SHerbert Xu 1746eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 17474cdd0d10SNikolay Aleksandrov if (br_multicast_port_ctx_state_stopped(pmctx)) 1748eb1d1641SHerbert Xu goto out; 1749615cc23eSNikolay Aleksandrov 17504cdd0d10SNikolay Aleksandrov brmctx = br_multicast_port_ctx_get_global(pmctx); 17514cdd0d10SNikolay Aleksandrov if (query->startup_sent < brmctx->multicast_startup_query_count) 1752cc0fdd80SLinus Lüssing query->startup_sent++; 1753eb1d1641SHerbert Xu 17544cdd0d10SNikolay Aleksandrov br_multicast_send_query(brmctx, pmctx, query); 1755eb1d1641SHerbert Xu 1756eb1d1641SHerbert Xu out: 1757eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1758eb1d1641SHerbert Xu } 1759eb1d1641SHerbert Xu 176088c1f37fSAllen Pais static void br_ip4_multicast_port_query_expired(struct timer_list *t) 1761cc0fdd80SLinus Lüssing { 17629632233eSNikolay Aleksandrov struct net_bridge_mcast_port *pmctx = from_timer(pmctx, t, 17639632233eSNikolay Aleksandrov ip4_own_query.timer); 1764cc0fdd80SLinus Lüssing 17659632233eSNikolay Aleksandrov br_multicast_port_query_expired(pmctx, &pmctx->ip4_own_query); 1766cc0fdd80SLinus Lüssing } 1767cc0fdd80SLinus Lüssing 1768cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 176988c1f37fSAllen Pais static void br_ip6_multicast_port_query_expired(struct timer_list *t) 1770cc0fdd80SLinus Lüssing { 17719632233eSNikolay Aleksandrov struct net_bridge_mcast_port *pmctx = from_timer(pmctx, t, 17729632233eSNikolay Aleksandrov ip6_own_query.timer); 1773cc0fdd80SLinus Lüssing 17749632233eSNikolay Aleksandrov br_multicast_port_query_expired(pmctx, &pmctx->ip6_own_query); 1775cc0fdd80SLinus Lüssing } 1776cc0fdd80SLinus Lüssing #endif 1777cc0fdd80SLinus Lüssing 177842c11ccfSNikolay Aleksandrov static void br_multicast_port_group_rexmit(struct timer_list *t) 177942c11ccfSNikolay Aleksandrov { 178042c11ccfSNikolay Aleksandrov struct net_bridge_port_group *pg = from_timer(pg, t, rexmit_timer); 178142c11ccfSNikolay Aleksandrov struct bridge_mcast_other_query *other_query = NULL; 1782085b53c8SNikolay Aleksandrov struct net_bridge *br = pg->key.port->br; 1783adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx; 1784adc47037SNikolay Aleksandrov struct net_bridge_mcast *brmctx; 178542c11ccfSNikolay Aleksandrov bool need_rexmit = false; 178642c11ccfSNikolay Aleksandrov 178742c11ccfSNikolay Aleksandrov spin_lock(&br->multicast_lock); 178842c11ccfSNikolay Aleksandrov if (!netif_running(br->dev) || hlist_unhashed(&pg->mglist) || 17894d5b4e84SNikolay Aleksandrov !br_opt_get(br, BROPT_MULTICAST_ENABLED)) 179042c11ccfSNikolay Aleksandrov goto out; 179142c11ccfSNikolay Aleksandrov 1792eb1593a0SNikolay Aleksandrov pmctx = br_multicast_pg_to_port_ctx(pg); 1793eb1593a0SNikolay Aleksandrov if (!pmctx) 1794eb1593a0SNikolay Aleksandrov goto out; 1795eb1593a0SNikolay Aleksandrov brmctx = br_multicast_port_ctx_get_global(pmctx); 17964d5b4e84SNikolay Aleksandrov if (!brmctx->multicast_querier) 17974d5b4e84SNikolay Aleksandrov goto out; 17984d5b4e84SNikolay Aleksandrov 1799085b53c8SNikolay Aleksandrov if (pg->key.addr.proto == htons(ETH_P_IP)) 1800d3d065c0SNikolay Aleksandrov other_query = &brmctx->ip4_other_query; 180142c11ccfSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 180242c11ccfSNikolay Aleksandrov else 1803d3d065c0SNikolay Aleksandrov other_query = &brmctx->ip6_other_query; 180442c11ccfSNikolay Aleksandrov #endif 180542c11ccfSNikolay Aleksandrov 180642c11ccfSNikolay Aleksandrov if (!other_query || timer_pending(&other_query->timer)) 180742c11ccfSNikolay Aleksandrov goto out; 180842c11ccfSNikolay Aleksandrov 180942c11ccfSNikolay Aleksandrov if (pg->grp_query_rexmit_cnt) { 181042c11ccfSNikolay Aleksandrov pg->grp_query_rexmit_cnt--; 1811adc47037SNikolay Aleksandrov __br_multicast_send_query(brmctx, pmctx, pg, &pg->key.addr, 1812085b53c8SNikolay Aleksandrov &pg->key.addr, false, 1, NULL); 181342c11ccfSNikolay Aleksandrov } 1814adc47037SNikolay Aleksandrov __br_multicast_send_query(brmctx, pmctx, pg, &pg->key.addr, 1815085b53c8SNikolay Aleksandrov &pg->key.addr, true, 0, &need_rexmit); 181642c11ccfSNikolay Aleksandrov 181742c11ccfSNikolay Aleksandrov if (pg->grp_query_rexmit_cnt || need_rexmit) 181842c11ccfSNikolay Aleksandrov mod_timer(&pg->rexmit_timer, jiffies + 1819d3d065c0SNikolay Aleksandrov brmctx->multicast_last_member_interval); 182042c11ccfSNikolay Aleksandrov out: 182142c11ccfSNikolay Aleksandrov spin_unlock(&br->multicast_lock); 182242c11ccfSNikolay Aleksandrov } 182342c11ccfSNikolay Aleksandrov 1824ae1ea84bSFlorian Fainelli static int br_mc_disabled_update(struct net_device *dev, bool value, 1825ae1ea84bSFlorian Fainelli struct netlink_ext_ack *extack) 1826147c1e9bSNogah Frankel { 1827147c1e9bSNogah Frankel struct switchdev_attr attr = { 1828147c1e9bSNogah Frankel .orig_dev = dev, 1829147c1e9bSNogah Frankel .id = SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED, 1830147c1e9bSNogah Frankel .flags = SWITCHDEV_F_DEFER, 183113cefad2SNikolay Aleksandrov .u.mc_disabled = !value, 1832147c1e9bSNogah Frankel }; 1833147c1e9bSNogah Frankel 1834ae1ea84bSFlorian Fainelli return switchdev_port_attr_set(dev, &attr, extack); 1835147c1e9bSNogah Frankel } 1836147c1e9bSNogah Frankel 1837613d61dbSNikolay Aleksandrov void br_multicast_port_ctx_init(struct net_bridge_port *port, 1838613d61dbSNikolay Aleksandrov struct net_bridge_vlan *vlan, 18399632233eSNikolay Aleksandrov struct net_bridge_mcast_port *pmctx) 18409632233eSNikolay Aleksandrov { 18419632233eSNikolay Aleksandrov pmctx->port = port; 1842613d61dbSNikolay Aleksandrov pmctx->vlan = vlan; 18439632233eSNikolay Aleksandrov pmctx->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 18449632233eSNikolay Aleksandrov timer_setup(&pmctx->ip4_mc_router_timer, 18459632233eSNikolay Aleksandrov br_ip4_multicast_router_expired, 0); 18469632233eSNikolay Aleksandrov timer_setup(&pmctx->ip4_own_query.timer, 18479632233eSNikolay Aleksandrov br_ip4_multicast_port_query_expired, 0); 18489632233eSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 18499632233eSNikolay Aleksandrov timer_setup(&pmctx->ip6_mc_router_timer, 18509632233eSNikolay Aleksandrov br_ip6_multicast_router_expired, 0); 18519632233eSNikolay Aleksandrov timer_setup(&pmctx->ip6_own_query.timer, 18529632233eSNikolay Aleksandrov br_ip6_multicast_port_query_expired, 0); 18539632233eSNikolay Aleksandrov #endif 18549632233eSNikolay Aleksandrov } 18559632233eSNikolay Aleksandrov 1856613d61dbSNikolay Aleksandrov void br_multicast_port_ctx_deinit(struct net_bridge_mcast_port *pmctx) 18579632233eSNikolay Aleksandrov { 18589632233eSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 18599632233eSNikolay Aleksandrov del_timer_sync(&pmctx->ip6_mc_router_timer); 18609632233eSNikolay Aleksandrov #endif 18619632233eSNikolay Aleksandrov del_timer_sync(&pmctx->ip4_mc_router_timer); 18629632233eSNikolay Aleksandrov } 18639632233eSNikolay Aleksandrov 18641080ab95SNikolay Aleksandrov int br_multicast_add_port(struct net_bridge_port *port) 1865eb1d1641SHerbert Xu { 1866ae1ea84bSFlorian Fainelli int err; 1867ae1ea84bSFlorian Fainelli 186889268b05SNikolay Aleksandrov port->multicast_eht_hosts_limit = BR_MCAST_DEFAULT_EHT_HOSTS_LIMIT; 1869613d61dbSNikolay Aleksandrov br_multicast_port_ctx_init(port, NULL, &port->multicast_ctx); 1870eb1d1641SHerbert Xu 1871ae1ea84bSFlorian Fainelli err = br_mc_disabled_update(port->dev, 1872ae1ea84bSFlorian Fainelli br_opt_get(port->br, 1873ae1ea84bSFlorian Fainelli BROPT_MULTICAST_ENABLED), 1874ae1ea84bSFlorian Fainelli NULL); 187568f5c12aSVladimir Oltean if (err && err != -EOPNOTSUPP) 1876ae1ea84bSFlorian Fainelli return err; 1877147c1e9bSNogah Frankel 18781080ab95SNikolay Aleksandrov port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats); 18791080ab95SNikolay Aleksandrov if (!port->mcast_stats) 18801080ab95SNikolay Aleksandrov return -ENOMEM; 18811080ab95SNikolay Aleksandrov 18821080ab95SNikolay Aleksandrov return 0; 1883eb1d1641SHerbert Xu } 1884eb1d1641SHerbert Xu 1885eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port) 1886eb1d1641SHerbert Xu { 1887e10177abSSatish Ashok struct net_bridge *br = port->br; 1888e10177abSSatish Ashok struct net_bridge_port_group *pg; 1889e12cec65SNikolay Aleksandrov HLIST_HEAD(deleted_head); 1890e10177abSSatish Ashok struct hlist_node *n; 1891e10177abSSatish Ashok 1892e10177abSSatish Ashok /* Take care of the remaining groups, only perm ones should be left */ 1893e10177abSSatish Ashok spin_lock_bh(&br->multicast_lock); 1894e10177abSSatish Ashok hlist_for_each_entry_safe(pg, n, &port->mglist, mglist) 1895681590bdSNikolay Aleksandrov br_multicast_find_del_pg(br, pg); 1896e12cec65SNikolay Aleksandrov hlist_move_list(&br->mcast_gc_list, &deleted_head); 1897e10177abSSatish Ashok spin_unlock_bh(&br->multicast_lock); 1898e12cec65SNikolay Aleksandrov br_multicast_gc(&deleted_head); 18999632233eSNikolay Aleksandrov br_multicast_port_ctx_deinit(&port->multicast_ctx); 19001080ab95SNikolay Aleksandrov free_percpu(port->mcast_stats); 1901eb1d1641SHerbert Xu } 1902eb1d1641SHerbert Xu 190390010b36SLinus Lüssing static void br_multicast_enable(struct bridge_mcast_own_query *query) 1904561f1103SHerbert Xu { 1905cc0fdd80SLinus Lüssing query->startup_sent = 0; 1906561f1103SHerbert Xu 1907cc0fdd80SLinus Lüssing if (try_to_del_timer_sync(&query->timer) >= 0 || 1908cc0fdd80SLinus Lüssing del_timer(&query->timer)) 1909cc0fdd80SLinus Lüssing mod_timer(&query->timer, jiffies); 1910561f1103SHerbert Xu } 1911561f1103SHerbert Xu 1912adc47037SNikolay Aleksandrov static void __br_multicast_enable_port_ctx(struct net_bridge_mcast_port *pmctx) 1913eb1d1641SHerbert Xu { 1914adc47037SNikolay Aleksandrov struct net_bridge *br = pmctx->port->br; 19157b54aaafSNikolay Aleksandrov struct net_bridge_mcast *brmctx; 1916eb1d1641SHerbert Xu 19177b54aaafSNikolay Aleksandrov brmctx = br_multicast_port_ctx_get_global(pmctx); 19187b54aaafSNikolay Aleksandrov if (!br_opt_get(br, BROPT_MULTICAST_ENABLED) || 19197b54aaafSNikolay Aleksandrov !netif_running(br->dev)) 19207cb3f921SNikolay Aleksandrov return; 1921eb1d1641SHerbert Xu 1922adc47037SNikolay Aleksandrov br_multicast_enable(&pmctx->ip4_own_query); 1923cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1924adc47037SNikolay Aleksandrov br_multicast_enable(&pmctx->ip6_own_query); 1925cc0fdd80SLinus Lüssing #endif 1926adc47037SNikolay Aleksandrov if (pmctx->multicast_router == MDB_RTR_TYPE_PERM) { 1927adc47037SNikolay Aleksandrov br_ip4_multicast_add_router(brmctx, pmctx); 1928adc47037SNikolay Aleksandrov br_ip6_multicast_add_router(brmctx, pmctx); 1929a3c02e76SLinus Lüssing } 19307cb3f921SNikolay Aleksandrov } 1931eb1d1641SHerbert Xu 19327cb3f921SNikolay Aleksandrov void br_multicast_enable_port(struct net_bridge_port *port) 19337cb3f921SNikolay Aleksandrov { 19347cb3f921SNikolay Aleksandrov struct net_bridge *br = port->br; 19357cb3f921SNikolay Aleksandrov 1936f4b7002aSNikolay Aleksandrov spin_lock_bh(&br->multicast_lock); 1937adc47037SNikolay Aleksandrov __br_multicast_enable_port_ctx(&port->multicast_ctx); 1938f4b7002aSNikolay Aleksandrov spin_unlock_bh(&br->multicast_lock); 1939eb1d1641SHerbert Xu } 1940eb1d1641SHerbert Xu 19417b54aaafSNikolay Aleksandrov static void __br_multicast_disable_port_ctx(struct net_bridge_mcast_port *pmctx) 1942eb1d1641SHerbert Xu { 1943eb1d1641SHerbert Xu struct net_bridge_port_group *pg; 1944b67bfe0dSSasha Levin struct hlist_node *n; 1945ed2d3597SLinus Lüssing bool del = false; 1946eb1d1641SHerbert Xu 19477b54aaafSNikolay Aleksandrov hlist_for_each_entry_safe(pg, n, &pmctx->port->mglist, mglist) 19487b54aaafSNikolay Aleksandrov if (!(pg->flags & MDB_PG_FLAGS_PERMANENT) && 19497b54aaafSNikolay Aleksandrov (!br_multicast_port_ctx_is_vlan(pmctx) || 19507b54aaafSNikolay Aleksandrov pg->key.addr.vid == pmctx->vlan->vid)) 19517b54aaafSNikolay Aleksandrov br_multicast_find_del_pg(pmctx->port->br, pg); 1952eb1d1641SHerbert Xu 1953adc47037SNikolay Aleksandrov del |= br_ip4_multicast_rport_del(pmctx); 1954adc47037SNikolay Aleksandrov del_timer(&pmctx->ip4_mc_router_timer); 1955adc47037SNikolay Aleksandrov del_timer(&pmctx->ip4_own_query.timer); 1956adc47037SNikolay Aleksandrov del |= br_ip6_multicast_rport_del(pmctx); 1957cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1958adc47037SNikolay Aleksandrov del_timer(&pmctx->ip6_mc_router_timer); 1959adc47037SNikolay Aleksandrov del_timer(&pmctx->ip6_own_query.timer); 1960cc0fdd80SLinus Lüssing #endif 1961adc47037SNikolay Aleksandrov br_multicast_rport_del_notify(pmctx, del); 19627b54aaafSNikolay Aleksandrov } 19637b54aaafSNikolay Aleksandrov 19647b54aaafSNikolay Aleksandrov void br_multicast_disable_port(struct net_bridge_port *port) 19657b54aaafSNikolay Aleksandrov { 1966f4b7002aSNikolay Aleksandrov spin_lock_bh(&port->br->multicast_lock); 19677b54aaafSNikolay Aleksandrov __br_multicast_disable_port_ctx(&port->multicast_ctx); 1968f4b7002aSNikolay Aleksandrov spin_unlock_bh(&port->br->multicast_lock); 1969eb1d1641SHerbert Xu } 1970eb1d1641SHerbert Xu 1971e6231bcaSNikolay Aleksandrov static int __grp_src_delete_marked(struct net_bridge_port_group *pg) 1972e6231bcaSNikolay Aleksandrov { 1973e6231bcaSNikolay Aleksandrov struct net_bridge_group_src *ent; 1974e6231bcaSNikolay Aleksandrov struct hlist_node *tmp; 1975e6231bcaSNikolay Aleksandrov int deleted = 0; 1976e6231bcaSNikolay Aleksandrov 1977e6231bcaSNikolay Aleksandrov hlist_for_each_entry_safe(ent, tmp, &pg->src_list, node) 1978e6231bcaSNikolay Aleksandrov if (ent->flags & BR_SGRP_F_DELETE) { 1979d5a10222SNikolay Aleksandrov br_multicast_del_group_src(ent, false); 1980e6231bcaSNikolay Aleksandrov deleted++; 1981e6231bcaSNikolay Aleksandrov } 1982e6231bcaSNikolay Aleksandrov 1983e6231bcaSNikolay Aleksandrov return deleted; 1984e6231bcaSNikolay Aleksandrov } 1985e6231bcaSNikolay Aleksandrov 1986b0812368SNikolay Aleksandrov static void __grp_src_mod_timer(struct net_bridge_group_src *src, 1987b0812368SNikolay Aleksandrov unsigned long expires) 1988b0812368SNikolay Aleksandrov { 1989b0812368SNikolay Aleksandrov mod_timer(&src->timer, expires); 1990b0812368SNikolay Aleksandrov br_multicast_fwd_src_handle(src); 1991b0812368SNikolay Aleksandrov } 1992b0812368SNikolay Aleksandrov 1993adc47037SNikolay Aleksandrov static void __grp_src_query_marked_and_rexmit(struct net_bridge_mcast *brmctx, 1994adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 1995adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg) 19965bf1e00bSNikolay Aleksandrov { 19975bf1e00bSNikolay Aleksandrov struct bridge_mcast_other_query *other_query = NULL; 1998d3d065c0SNikolay Aleksandrov u32 lmqc = brmctx->multicast_last_member_count; 19995bf1e00bSNikolay Aleksandrov unsigned long lmqt, lmi, now = jiffies; 20005bf1e00bSNikolay Aleksandrov struct net_bridge_group_src *ent; 20015bf1e00bSNikolay Aleksandrov 2002adc47037SNikolay Aleksandrov if (!netif_running(brmctx->br->dev) || 2003adc47037SNikolay Aleksandrov !br_opt_get(brmctx->br, BROPT_MULTICAST_ENABLED)) 20045bf1e00bSNikolay Aleksandrov return; 20055bf1e00bSNikolay Aleksandrov 2006085b53c8SNikolay Aleksandrov if (pg->key.addr.proto == htons(ETH_P_IP)) 2007d3d065c0SNikolay Aleksandrov other_query = &brmctx->ip4_other_query; 20085bf1e00bSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 20095bf1e00bSNikolay Aleksandrov else 2010d3d065c0SNikolay Aleksandrov other_query = &brmctx->ip6_other_query; 20115bf1e00bSNikolay Aleksandrov #endif 20125bf1e00bSNikolay Aleksandrov 2013adc47037SNikolay Aleksandrov lmqt = now + br_multicast_lmqt(brmctx); 20145bf1e00bSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) { 20155bf1e00bSNikolay Aleksandrov if (ent->flags & BR_SGRP_F_SEND) { 20165bf1e00bSNikolay Aleksandrov ent->flags &= ~BR_SGRP_F_SEND; 20175bf1e00bSNikolay Aleksandrov if (ent->timer.expires > lmqt) { 20184d5b4e84SNikolay Aleksandrov if (brmctx->multicast_querier && 20195bf1e00bSNikolay Aleksandrov other_query && 20205bf1e00bSNikolay Aleksandrov !timer_pending(&other_query->timer)) 20215bf1e00bSNikolay Aleksandrov ent->src_query_rexmit_cnt = lmqc; 2022b0812368SNikolay Aleksandrov __grp_src_mod_timer(ent, lmqt); 20235bf1e00bSNikolay Aleksandrov } 20245bf1e00bSNikolay Aleksandrov } 20255bf1e00bSNikolay Aleksandrov } 20265bf1e00bSNikolay Aleksandrov 20274d5b4e84SNikolay Aleksandrov if (!brmctx->multicast_querier || 20285bf1e00bSNikolay Aleksandrov !other_query || timer_pending(&other_query->timer)) 20295bf1e00bSNikolay Aleksandrov return; 20305bf1e00bSNikolay Aleksandrov 2031adc47037SNikolay Aleksandrov __br_multicast_send_query(brmctx, pmctx, pg, &pg->key.addr, 2032085b53c8SNikolay Aleksandrov &pg->key.addr, true, 1, NULL); 20335bf1e00bSNikolay Aleksandrov 2034d3d065c0SNikolay Aleksandrov lmi = now + brmctx->multicast_last_member_interval; 20355bf1e00bSNikolay Aleksandrov if (!timer_pending(&pg->rexmit_timer) || 20365bf1e00bSNikolay Aleksandrov time_after(pg->rexmit_timer.expires, lmi)) 20375bf1e00bSNikolay Aleksandrov mod_timer(&pg->rexmit_timer, lmi); 20385bf1e00bSNikolay Aleksandrov } 20395bf1e00bSNikolay Aleksandrov 2040adc47037SNikolay Aleksandrov static void __grp_send_query_and_rexmit(struct net_bridge_mcast *brmctx, 2041adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 2042adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg) 20435bf1e00bSNikolay Aleksandrov { 20445bf1e00bSNikolay Aleksandrov struct bridge_mcast_other_query *other_query = NULL; 20455bf1e00bSNikolay Aleksandrov unsigned long now = jiffies, lmi; 20465bf1e00bSNikolay Aleksandrov 2047adc47037SNikolay Aleksandrov if (!netif_running(brmctx->br->dev) || 2048adc47037SNikolay Aleksandrov !br_opt_get(brmctx->br, BROPT_MULTICAST_ENABLED)) 20495bf1e00bSNikolay Aleksandrov return; 20505bf1e00bSNikolay Aleksandrov 2051085b53c8SNikolay Aleksandrov if (pg->key.addr.proto == htons(ETH_P_IP)) 2052d3d065c0SNikolay Aleksandrov other_query = &brmctx->ip4_other_query; 20535bf1e00bSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 20545bf1e00bSNikolay Aleksandrov else 2055d3d065c0SNikolay Aleksandrov other_query = &brmctx->ip6_other_query; 20565bf1e00bSNikolay Aleksandrov #endif 20575bf1e00bSNikolay Aleksandrov 20584d5b4e84SNikolay Aleksandrov if (brmctx->multicast_querier && 20595bf1e00bSNikolay Aleksandrov other_query && !timer_pending(&other_query->timer)) { 2060d3d065c0SNikolay Aleksandrov lmi = now + brmctx->multicast_last_member_interval; 2061d3d065c0SNikolay Aleksandrov pg->grp_query_rexmit_cnt = brmctx->multicast_last_member_count - 1; 2062adc47037SNikolay Aleksandrov __br_multicast_send_query(brmctx, pmctx, pg, &pg->key.addr, 2063085b53c8SNikolay Aleksandrov &pg->key.addr, false, 0, NULL); 20645bf1e00bSNikolay Aleksandrov if (!timer_pending(&pg->rexmit_timer) || 20655bf1e00bSNikolay Aleksandrov time_after(pg->rexmit_timer.expires, lmi)) 20665bf1e00bSNikolay Aleksandrov mod_timer(&pg->rexmit_timer, lmi); 20675bf1e00bSNikolay Aleksandrov } 20685bf1e00bSNikolay Aleksandrov 20695bf1e00bSNikolay Aleksandrov if (pg->filter_mode == MCAST_EXCLUDE && 20705bf1e00bSNikolay Aleksandrov (!timer_pending(&pg->timer) || 2071adc47037SNikolay Aleksandrov time_after(pg->timer.expires, now + br_multicast_lmqt(brmctx)))) 2072adc47037SNikolay Aleksandrov mod_timer(&pg->timer, now + br_multicast_lmqt(brmctx)); 20735bf1e00bSNikolay Aleksandrov } 20745bf1e00bSNikolay Aleksandrov 20750436862eSNikolay Aleksandrov /* State Msg type New state Actions 20760436862eSNikolay Aleksandrov * INCLUDE (A) IS_IN (B) INCLUDE (A+B) (B)=GMI 20770436862eSNikolay Aleksandrov * INCLUDE (A) ALLOW (B) INCLUDE (A+B) (B)=GMI 20780436862eSNikolay Aleksandrov * EXCLUDE (X,Y) ALLOW (A) EXCLUDE (X+A,Y-A) (A)=GMI 20790436862eSNikolay Aleksandrov */ 2080adc47037SNikolay Aleksandrov static bool br_multicast_isinc_allow(const struct net_bridge_mcast *brmctx, 2081adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, void *h_addr, 2082474ddb37SNikolay Aleksandrov void *srcs, u32 nsrcs, size_t addr_size, 2083474ddb37SNikolay Aleksandrov int grec_type) 20840436862eSNikolay Aleksandrov { 20850436862eSNikolay Aleksandrov struct net_bridge_group_src *ent; 20860436862eSNikolay Aleksandrov unsigned long now = jiffies; 20870436862eSNikolay Aleksandrov bool changed = false; 20880436862eSNikolay Aleksandrov struct br_ip src_ip; 20890436862eSNikolay Aleksandrov u32 src_idx; 20900436862eSNikolay Aleksandrov 20910436862eSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 2092085b53c8SNikolay Aleksandrov src_ip.proto = pg->key.addr.proto; 20930436862eSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 2094e7cfcf2cSNikolay Aleksandrov memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size); 20950436862eSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 20960436862eSNikolay Aleksandrov if (!ent) { 20970436862eSNikolay Aleksandrov ent = br_multicast_new_group_src(pg, &src_ip); 20980436862eSNikolay Aleksandrov if (ent) 20990436862eSNikolay Aleksandrov changed = true; 21000436862eSNikolay Aleksandrov } 21010436862eSNikolay Aleksandrov 21020436862eSNikolay Aleksandrov if (ent) 2103adc47037SNikolay Aleksandrov __grp_src_mod_timer(ent, now + br_multicast_gmi(brmctx)); 21040436862eSNikolay Aleksandrov } 21050436862eSNikolay Aleksandrov 2106adc47037SNikolay Aleksandrov if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size, 2107adc47037SNikolay Aleksandrov grec_type)) 2108474ddb37SNikolay Aleksandrov changed = true; 2109474ddb37SNikolay Aleksandrov 21100436862eSNikolay Aleksandrov return changed; 21110436862eSNikolay Aleksandrov } 21120436862eSNikolay Aleksandrov 2113e6231bcaSNikolay Aleksandrov /* State Msg type New state Actions 2114e6231bcaSNikolay Aleksandrov * INCLUDE (A) IS_EX (B) EXCLUDE (A*B,B-A) (B-A)=0 2115e6231bcaSNikolay Aleksandrov * Delete (A-B) 2116e6231bcaSNikolay Aleksandrov * Group Timer=GMI 2117e6231bcaSNikolay Aleksandrov */ 2118adc47037SNikolay Aleksandrov static void __grp_src_isexc_incl(const struct net_bridge_mcast *brmctx, 2119adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, void *h_addr, 2120ddc255d9SNikolay Aleksandrov void *srcs, u32 nsrcs, size_t addr_size, 2121ddc255d9SNikolay Aleksandrov int grec_type) 2122e6231bcaSNikolay Aleksandrov { 2123e6231bcaSNikolay Aleksandrov struct net_bridge_group_src *ent; 2124e6231bcaSNikolay Aleksandrov struct br_ip src_ip; 2125e6231bcaSNikolay Aleksandrov u32 src_idx; 2126e6231bcaSNikolay Aleksandrov 2127e6231bcaSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 2128e6231bcaSNikolay Aleksandrov ent->flags |= BR_SGRP_F_DELETE; 2129e6231bcaSNikolay Aleksandrov 2130e6231bcaSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 2131085b53c8SNikolay Aleksandrov src_ip.proto = pg->key.addr.proto; 2132e6231bcaSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 2133e7cfcf2cSNikolay Aleksandrov memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size); 2134e6231bcaSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 2135e6231bcaSNikolay Aleksandrov if (ent) 2136e6231bcaSNikolay Aleksandrov ent->flags &= ~BR_SGRP_F_DELETE; 2137e6231bcaSNikolay Aleksandrov else 2138b0812368SNikolay Aleksandrov ent = br_multicast_new_group_src(pg, &src_ip); 2139b0812368SNikolay Aleksandrov if (ent) 2140b0812368SNikolay Aleksandrov br_multicast_fwd_src_handle(ent); 2141e6231bcaSNikolay Aleksandrov } 2142e6231bcaSNikolay Aleksandrov 2143adc47037SNikolay Aleksandrov br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size, 2144adc47037SNikolay Aleksandrov grec_type); 2145ddc255d9SNikolay Aleksandrov 2146e6231bcaSNikolay Aleksandrov __grp_src_delete_marked(pg); 2147e6231bcaSNikolay Aleksandrov } 2148e6231bcaSNikolay Aleksandrov 2149e6231bcaSNikolay Aleksandrov /* State Msg type New state Actions 2150e6231bcaSNikolay Aleksandrov * EXCLUDE (X,Y) IS_EX (A) EXCLUDE (A-Y,Y*A) (A-X-Y)=GMI 2151e6231bcaSNikolay Aleksandrov * Delete (X-A) 2152e6231bcaSNikolay Aleksandrov * Delete (Y-A) 2153e6231bcaSNikolay Aleksandrov * Group Timer=GMI 2154e6231bcaSNikolay Aleksandrov */ 2155adc47037SNikolay Aleksandrov static bool __grp_src_isexc_excl(const struct net_bridge_mcast *brmctx, 2156adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, void *h_addr, 2157ddc255d9SNikolay Aleksandrov void *srcs, u32 nsrcs, size_t addr_size, 2158ddc255d9SNikolay Aleksandrov int grec_type) 2159e6231bcaSNikolay Aleksandrov { 2160e6231bcaSNikolay Aleksandrov struct net_bridge_group_src *ent; 2161e6231bcaSNikolay Aleksandrov unsigned long now = jiffies; 2162e6231bcaSNikolay Aleksandrov bool changed = false; 2163e6231bcaSNikolay Aleksandrov struct br_ip src_ip; 2164e6231bcaSNikolay Aleksandrov u32 src_idx; 2165e6231bcaSNikolay Aleksandrov 2166e6231bcaSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 2167e6231bcaSNikolay Aleksandrov ent->flags |= BR_SGRP_F_DELETE; 2168e6231bcaSNikolay Aleksandrov 2169e6231bcaSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 2170085b53c8SNikolay Aleksandrov src_ip.proto = pg->key.addr.proto; 2171e6231bcaSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 2172e7cfcf2cSNikolay Aleksandrov memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size); 2173e6231bcaSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 2174e6231bcaSNikolay Aleksandrov if (ent) { 2175e6231bcaSNikolay Aleksandrov ent->flags &= ~BR_SGRP_F_DELETE; 2176e6231bcaSNikolay Aleksandrov } else { 2177e6231bcaSNikolay Aleksandrov ent = br_multicast_new_group_src(pg, &src_ip); 2178e6231bcaSNikolay Aleksandrov if (ent) { 2179b0812368SNikolay Aleksandrov __grp_src_mod_timer(ent, 2180adc47037SNikolay Aleksandrov now + br_multicast_gmi(brmctx)); 2181e6231bcaSNikolay Aleksandrov changed = true; 2182e6231bcaSNikolay Aleksandrov } 2183e6231bcaSNikolay Aleksandrov } 2184e6231bcaSNikolay Aleksandrov } 2185e6231bcaSNikolay Aleksandrov 2186adc47037SNikolay Aleksandrov if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size, 2187adc47037SNikolay Aleksandrov grec_type)) 2188ddc255d9SNikolay Aleksandrov changed = true; 2189ddc255d9SNikolay Aleksandrov 2190e6231bcaSNikolay Aleksandrov if (__grp_src_delete_marked(pg)) 2191e6231bcaSNikolay Aleksandrov changed = true; 2192e6231bcaSNikolay Aleksandrov 2193e6231bcaSNikolay Aleksandrov return changed; 2194e6231bcaSNikolay Aleksandrov } 2195e6231bcaSNikolay Aleksandrov 2196adc47037SNikolay Aleksandrov static bool br_multicast_isexc(const struct net_bridge_mcast *brmctx, 2197adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, void *h_addr, 2198ddc255d9SNikolay Aleksandrov void *srcs, u32 nsrcs, size_t addr_size, 2199ddc255d9SNikolay Aleksandrov int grec_type) 2200e6231bcaSNikolay Aleksandrov { 2201e6231bcaSNikolay Aleksandrov bool changed = false; 2202e6231bcaSNikolay Aleksandrov 2203e6231bcaSNikolay Aleksandrov switch (pg->filter_mode) { 2204e6231bcaSNikolay Aleksandrov case MCAST_INCLUDE: 2205adc47037SNikolay Aleksandrov __grp_src_isexc_incl(brmctx, pg, h_addr, srcs, nsrcs, addr_size, 2206ddc255d9SNikolay Aleksandrov grec_type); 22078266a049SNikolay Aleksandrov br_multicast_star_g_handle_mode(pg, MCAST_EXCLUDE); 2208e6231bcaSNikolay Aleksandrov changed = true; 2209e6231bcaSNikolay Aleksandrov break; 2210e6231bcaSNikolay Aleksandrov case MCAST_EXCLUDE: 2211adc47037SNikolay Aleksandrov changed = __grp_src_isexc_excl(brmctx, pg, h_addr, srcs, nsrcs, 2212adc47037SNikolay Aleksandrov addr_size, grec_type); 2213e6231bcaSNikolay Aleksandrov break; 2214e6231bcaSNikolay Aleksandrov } 2215e6231bcaSNikolay Aleksandrov 2216e6231bcaSNikolay Aleksandrov pg->filter_mode = MCAST_EXCLUDE; 2217adc47037SNikolay Aleksandrov mod_timer(&pg->timer, jiffies + br_multicast_gmi(brmctx)); 2218e6231bcaSNikolay Aleksandrov 2219e6231bcaSNikolay Aleksandrov return changed; 2220e6231bcaSNikolay Aleksandrov } 2221e6231bcaSNikolay Aleksandrov 22225bf1e00bSNikolay Aleksandrov /* State Msg type New state Actions 22235bf1e00bSNikolay Aleksandrov * INCLUDE (A) TO_IN (B) INCLUDE (A+B) (B)=GMI 22245bf1e00bSNikolay Aleksandrov * Send Q(G,A-B) 22255bf1e00bSNikolay Aleksandrov */ 2226adc47037SNikolay Aleksandrov static bool __grp_src_toin_incl(struct net_bridge_mcast *brmctx, 2227adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 2228adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, void *h_addr, 2229ddc255d9SNikolay Aleksandrov void *srcs, u32 nsrcs, size_t addr_size, 2230ddc255d9SNikolay Aleksandrov int grec_type) 22315bf1e00bSNikolay Aleksandrov { 22325bf1e00bSNikolay Aleksandrov u32 src_idx, to_send = pg->src_ents; 22335bf1e00bSNikolay Aleksandrov struct net_bridge_group_src *ent; 22345bf1e00bSNikolay Aleksandrov unsigned long now = jiffies; 22355bf1e00bSNikolay Aleksandrov bool changed = false; 22365bf1e00bSNikolay Aleksandrov struct br_ip src_ip; 22375bf1e00bSNikolay Aleksandrov 22385bf1e00bSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 22395bf1e00bSNikolay Aleksandrov ent->flags |= BR_SGRP_F_SEND; 22405bf1e00bSNikolay Aleksandrov 22415bf1e00bSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 2242085b53c8SNikolay Aleksandrov src_ip.proto = pg->key.addr.proto; 22435bf1e00bSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 2244e7cfcf2cSNikolay Aleksandrov memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size); 22455bf1e00bSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 22465bf1e00bSNikolay Aleksandrov if (ent) { 22475bf1e00bSNikolay Aleksandrov ent->flags &= ~BR_SGRP_F_SEND; 22485bf1e00bSNikolay Aleksandrov to_send--; 22495bf1e00bSNikolay Aleksandrov } else { 22505bf1e00bSNikolay Aleksandrov ent = br_multicast_new_group_src(pg, &src_ip); 22515bf1e00bSNikolay Aleksandrov if (ent) 22525bf1e00bSNikolay Aleksandrov changed = true; 22535bf1e00bSNikolay Aleksandrov } 22545bf1e00bSNikolay Aleksandrov if (ent) 2255adc47037SNikolay Aleksandrov __grp_src_mod_timer(ent, now + br_multicast_gmi(brmctx)); 22565bf1e00bSNikolay Aleksandrov } 22575bf1e00bSNikolay Aleksandrov 2258adc47037SNikolay Aleksandrov if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size, 2259adc47037SNikolay Aleksandrov grec_type)) 2260ddc255d9SNikolay Aleksandrov changed = true; 2261ddc255d9SNikolay Aleksandrov 22625bf1e00bSNikolay Aleksandrov if (to_send) 2263adc47037SNikolay Aleksandrov __grp_src_query_marked_and_rexmit(brmctx, pmctx, pg); 22645bf1e00bSNikolay Aleksandrov 22655bf1e00bSNikolay Aleksandrov return changed; 22665bf1e00bSNikolay Aleksandrov } 22675bf1e00bSNikolay Aleksandrov 22685bf1e00bSNikolay Aleksandrov /* State Msg type New state Actions 22695bf1e00bSNikolay Aleksandrov * EXCLUDE (X,Y) TO_IN (A) EXCLUDE (X+A,Y-A) (A)=GMI 22705bf1e00bSNikolay Aleksandrov * Send Q(G,X-A) 22715bf1e00bSNikolay Aleksandrov * Send Q(G) 22725bf1e00bSNikolay Aleksandrov */ 2273adc47037SNikolay Aleksandrov static bool __grp_src_toin_excl(struct net_bridge_mcast *brmctx, 2274adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 2275adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, void *h_addr, 2276ddc255d9SNikolay Aleksandrov void *srcs, u32 nsrcs, size_t addr_size, 2277ddc255d9SNikolay Aleksandrov int grec_type) 22785bf1e00bSNikolay Aleksandrov { 22795bf1e00bSNikolay Aleksandrov u32 src_idx, to_send = pg->src_ents; 22805bf1e00bSNikolay Aleksandrov struct net_bridge_group_src *ent; 22815bf1e00bSNikolay Aleksandrov unsigned long now = jiffies; 22825bf1e00bSNikolay Aleksandrov bool changed = false; 22835bf1e00bSNikolay Aleksandrov struct br_ip src_ip; 22845bf1e00bSNikolay Aleksandrov 22855bf1e00bSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 22865bf1e00bSNikolay Aleksandrov if (timer_pending(&ent->timer)) 22875bf1e00bSNikolay Aleksandrov ent->flags |= BR_SGRP_F_SEND; 22885bf1e00bSNikolay Aleksandrov 22895bf1e00bSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 2290085b53c8SNikolay Aleksandrov src_ip.proto = pg->key.addr.proto; 22915bf1e00bSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 2292e7cfcf2cSNikolay Aleksandrov memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size); 22935bf1e00bSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 22945bf1e00bSNikolay Aleksandrov if (ent) { 22955bf1e00bSNikolay Aleksandrov if (timer_pending(&ent->timer)) { 22965bf1e00bSNikolay Aleksandrov ent->flags &= ~BR_SGRP_F_SEND; 22975bf1e00bSNikolay Aleksandrov to_send--; 22985bf1e00bSNikolay Aleksandrov } 22995bf1e00bSNikolay Aleksandrov } else { 23005bf1e00bSNikolay Aleksandrov ent = br_multicast_new_group_src(pg, &src_ip); 23015bf1e00bSNikolay Aleksandrov if (ent) 23025bf1e00bSNikolay Aleksandrov changed = true; 23035bf1e00bSNikolay Aleksandrov } 23045bf1e00bSNikolay Aleksandrov if (ent) 2305adc47037SNikolay Aleksandrov __grp_src_mod_timer(ent, now + br_multicast_gmi(brmctx)); 23065bf1e00bSNikolay Aleksandrov } 23075bf1e00bSNikolay Aleksandrov 2308adc47037SNikolay Aleksandrov if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size, 2309adc47037SNikolay Aleksandrov grec_type)) 2310ddc255d9SNikolay Aleksandrov changed = true; 2311ddc255d9SNikolay Aleksandrov 23125bf1e00bSNikolay Aleksandrov if (to_send) 2313adc47037SNikolay Aleksandrov __grp_src_query_marked_and_rexmit(brmctx, pmctx, pg); 23145bf1e00bSNikolay Aleksandrov 2315adc47037SNikolay Aleksandrov __grp_send_query_and_rexmit(brmctx, pmctx, pg); 23165bf1e00bSNikolay Aleksandrov 23175bf1e00bSNikolay Aleksandrov return changed; 23185bf1e00bSNikolay Aleksandrov } 23195bf1e00bSNikolay Aleksandrov 2320adc47037SNikolay Aleksandrov static bool br_multicast_toin(struct net_bridge_mcast *brmctx, 2321adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 2322adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, void *h_addr, 2323ddc255d9SNikolay Aleksandrov void *srcs, u32 nsrcs, size_t addr_size, 2324ddc255d9SNikolay Aleksandrov int grec_type) 23255bf1e00bSNikolay Aleksandrov { 23265bf1e00bSNikolay Aleksandrov bool changed = false; 23275bf1e00bSNikolay Aleksandrov 23285bf1e00bSNikolay Aleksandrov switch (pg->filter_mode) { 23295bf1e00bSNikolay Aleksandrov case MCAST_INCLUDE: 2330adc47037SNikolay Aleksandrov changed = __grp_src_toin_incl(brmctx, pmctx, pg, h_addr, srcs, 2331adc47037SNikolay Aleksandrov nsrcs, addr_size, grec_type); 23325bf1e00bSNikolay Aleksandrov break; 23335bf1e00bSNikolay Aleksandrov case MCAST_EXCLUDE: 2334adc47037SNikolay Aleksandrov changed = __grp_src_toin_excl(brmctx, pmctx, pg, h_addr, srcs, 2335adc47037SNikolay Aleksandrov nsrcs, addr_size, grec_type); 23365bf1e00bSNikolay Aleksandrov break; 23375bf1e00bSNikolay Aleksandrov } 23385bf1e00bSNikolay Aleksandrov 2339ddc255d9SNikolay Aleksandrov if (br_multicast_eht_should_del_pg(pg)) { 2340d5a10222SNikolay Aleksandrov pg->flags |= MDB_PG_FLAGS_FAST_LEAVE; 2341ddc255d9SNikolay Aleksandrov br_multicast_find_del_pg(pg->key.port->br, pg); 2342ddc255d9SNikolay Aleksandrov /* a notification has already been sent and we shouldn't 2343ddc255d9SNikolay Aleksandrov * access pg after the delete so we have to return false 2344ddc255d9SNikolay Aleksandrov */ 2345ddc255d9SNikolay Aleksandrov changed = false; 2346ddc255d9SNikolay Aleksandrov } 2347ddc255d9SNikolay Aleksandrov 23485bf1e00bSNikolay Aleksandrov return changed; 23495bf1e00bSNikolay Aleksandrov } 23505bf1e00bSNikolay Aleksandrov 23515bf1e00bSNikolay Aleksandrov /* State Msg type New state Actions 23525bf1e00bSNikolay Aleksandrov * INCLUDE (A) TO_EX (B) EXCLUDE (A*B,B-A) (B-A)=0 23535bf1e00bSNikolay Aleksandrov * Delete (A-B) 23545bf1e00bSNikolay Aleksandrov * Send Q(G,A*B) 23555bf1e00bSNikolay Aleksandrov * Group Timer=GMI 23565bf1e00bSNikolay Aleksandrov */ 2357adc47037SNikolay Aleksandrov static void __grp_src_toex_incl(struct net_bridge_mcast *brmctx, 2358adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 2359adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, void *h_addr, 2360ddc255d9SNikolay Aleksandrov void *srcs, u32 nsrcs, size_t addr_size, 2361ddc255d9SNikolay Aleksandrov int grec_type) 23625bf1e00bSNikolay Aleksandrov { 23635bf1e00bSNikolay Aleksandrov struct net_bridge_group_src *ent; 23645bf1e00bSNikolay Aleksandrov u32 src_idx, to_send = 0; 23655bf1e00bSNikolay Aleksandrov struct br_ip src_ip; 23665bf1e00bSNikolay Aleksandrov 23675bf1e00bSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 23685bf1e00bSNikolay Aleksandrov ent->flags = (ent->flags & ~BR_SGRP_F_SEND) | BR_SGRP_F_DELETE; 23695bf1e00bSNikolay Aleksandrov 23705bf1e00bSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 2371085b53c8SNikolay Aleksandrov src_ip.proto = pg->key.addr.proto; 23725bf1e00bSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 2373e7cfcf2cSNikolay Aleksandrov memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size); 23745bf1e00bSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 23755bf1e00bSNikolay Aleksandrov if (ent) { 23765bf1e00bSNikolay Aleksandrov ent->flags = (ent->flags & ~BR_SGRP_F_DELETE) | 23775bf1e00bSNikolay Aleksandrov BR_SGRP_F_SEND; 23785bf1e00bSNikolay Aleksandrov to_send++; 23795bf1e00bSNikolay Aleksandrov } else { 2380b0812368SNikolay Aleksandrov ent = br_multicast_new_group_src(pg, &src_ip); 23815bf1e00bSNikolay Aleksandrov } 2382b0812368SNikolay Aleksandrov if (ent) 2383b0812368SNikolay Aleksandrov br_multicast_fwd_src_handle(ent); 23845bf1e00bSNikolay Aleksandrov } 23855bf1e00bSNikolay Aleksandrov 2386adc47037SNikolay Aleksandrov br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size, 2387adc47037SNikolay Aleksandrov grec_type); 2388ddc255d9SNikolay Aleksandrov 23895bf1e00bSNikolay Aleksandrov __grp_src_delete_marked(pg); 23905bf1e00bSNikolay Aleksandrov if (to_send) 2391adc47037SNikolay Aleksandrov __grp_src_query_marked_and_rexmit(brmctx, pmctx, pg); 23925bf1e00bSNikolay Aleksandrov } 23935bf1e00bSNikolay Aleksandrov 23945bf1e00bSNikolay Aleksandrov /* State Msg type New state Actions 23955bf1e00bSNikolay Aleksandrov * EXCLUDE (X,Y) TO_EX (A) EXCLUDE (A-Y,Y*A) (A-X-Y)=Group Timer 23965bf1e00bSNikolay Aleksandrov * Delete (X-A) 23975bf1e00bSNikolay Aleksandrov * Delete (Y-A) 23985bf1e00bSNikolay Aleksandrov * Send Q(G,A-Y) 23995bf1e00bSNikolay Aleksandrov * Group Timer=GMI 24005bf1e00bSNikolay Aleksandrov */ 2401adc47037SNikolay Aleksandrov static bool __grp_src_toex_excl(struct net_bridge_mcast *brmctx, 2402adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 2403adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, void *h_addr, 2404ddc255d9SNikolay Aleksandrov void *srcs, u32 nsrcs, size_t addr_size, 2405ddc255d9SNikolay Aleksandrov int grec_type) 24065bf1e00bSNikolay Aleksandrov { 24075bf1e00bSNikolay Aleksandrov struct net_bridge_group_src *ent; 24085bf1e00bSNikolay Aleksandrov u32 src_idx, to_send = 0; 24095bf1e00bSNikolay Aleksandrov bool changed = false; 24105bf1e00bSNikolay Aleksandrov struct br_ip src_ip; 24115bf1e00bSNikolay Aleksandrov 24125bf1e00bSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 24135bf1e00bSNikolay Aleksandrov ent->flags = (ent->flags & ~BR_SGRP_F_SEND) | BR_SGRP_F_DELETE; 24145bf1e00bSNikolay Aleksandrov 24155bf1e00bSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 2416085b53c8SNikolay Aleksandrov src_ip.proto = pg->key.addr.proto; 24175bf1e00bSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 2418e7cfcf2cSNikolay Aleksandrov memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size); 24195bf1e00bSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 24205bf1e00bSNikolay Aleksandrov if (ent) { 24215bf1e00bSNikolay Aleksandrov ent->flags &= ~BR_SGRP_F_DELETE; 24225bf1e00bSNikolay Aleksandrov } else { 24235bf1e00bSNikolay Aleksandrov ent = br_multicast_new_group_src(pg, &src_ip); 24245bf1e00bSNikolay Aleksandrov if (ent) { 2425b0812368SNikolay Aleksandrov __grp_src_mod_timer(ent, pg->timer.expires); 24265bf1e00bSNikolay Aleksandrov changed = true; 24275bf1e00bSNikolay Aleksandrov } 24285bf1e00bSNikolay Aleksandrov } 24295bf1e00bSNikolay Aleksandrov if (ent && timer_pending(&ent->timer)) { 24305bf1e00bSNikolay Aleksandrov ent->flags |= BR_SGRP_F_SEND; 24315bf1e00bSNikolay Aleksandrov to_send++; 24325bf1e00bSNikolay Aleksandrov } 24335bf1e00bSNikolay Aleksandrov } 24345bf1e00bSNikolay Aleksandrov 2435adc47037SNikolay Aleksandrov if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size, 2436adc47037SNikolay Aleksandrov grec_type)) 2437ddc255d9SNikolay Aleksandrov changed = true; 2438ddc255d9SNikolay Aleksandrov 24395bf1e00bSNikolay Aleksandrov if (__grp_src_delete_marked(pg)) 24405bf1e00bSNikolay Aleksandrov changed = true; 24415bf1e00bSNikolay Aleksandrov if (to_send) 2442adc47037SNikolay Aleksandrov __grp_src_query_marked_and_rexmit(brmctx, pmctx, pg); 24435bf1e00bSNikolay Aleksandrov 24445bf1e00bSNikolay Aleksandrov return changed; 24455bf1e00bSNikolay Aleksandrov } 24465bf1e00bSNikolay Aleksandrov 2447adc47037SNikolay Aleksandrov static bool br_multicast_toex(struct net_bridge_mcast *brmctx, 2448adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 2449adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, void *h_addr, 2450ddc255d9SNikolay Aleksandrov void *srcs, u32 nsrcs, size_t addr_size, 2451ddc255d9SNikolay Aleksandrov int grec_type) 24525bf1e00bSNikolay Aleksandrov { 24535bf1e00bSNikolay Aleksandrov bool changed = false; 24545bf1e00bSNikolay Aleksandrov 24555bf1e00bSNikolay Aleksandrov switch (pg->filter_mode) { 24565bf1e00bSNikolay Aleksandrov case MCAST_INCLUDE: 2457adc47037SNikolay Aleksandrov __grp_src_toex_incl(brmctx, pmctx, pg, h_addr, srcs, nsrcs, 2458adc47037SNikolay Aleksandrov addr_size, grec_type); 24598266a049SNikolay Aleksandrov br_multicast_star_g_handle_mode(pg, MCAST_EXCLUDE); 24605bf1e00bSNikolay Aleksandrov changed = true; 24615bf1e00bSNikolay Aleksandrov break; 24625bf1e00bSNikolay Aleksandrov case MCAST_EXCLUDE: 2463adc47037SNikolay Aleksandrov changed = __grp_src_toex_excl(brmctx, pmctx, pg, h_addr, srcs, 2464adc47037SNikolay Aleksandrov nsrcs, addr_size, grec_type); 24655bf1e00bSNikolay Aleksandrov break; 24665bf1e00bSNikolay Aleksandrov } 24675bf1e00bSNikolay Aleksandrov 24685bf1e00bSNikolay Aleksandrov pg->filter_mode = MCAST_EXCLUDE; 2469adc47037SNikolay Aleksandrov mod_timer(&pg->timer, jiffies + br_multicast_gmi(brmctx)); 24705bf1e00bSNikolay Aleksandrov 24715bf1e00bSNikolay Aleksandrov return changed; 24725bf1e00bSNikolay Aleksandrov } 24735bf1e00bSNikolay Aleksandrov 2474109865feSNikolay Aleksandrov /* State Msg type New state Actions 2475109865feSNikolay Aleksandrov * INCLUDE (A) BLOCK (B) INCLUDE (A) Send Q(G,A*B) 2476109865feSNikolay Aleksandrov */ 2477adc47037SNikolay Aleksandrov static bool __grp_src_block_incl(struct net_bridge_mcast *brmctx, 2478adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 2479adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, void *h_addr, 2480474ddb37SNikolay Aleksandrov void *srcs, u32 nsrcs, size_t addr_size, int grec_type) 2481109865feSNikolay Aleksandrov { 2482109865feSNikolay Aleksandrov struct net_bridge_group_src *ent; 2483109865feSNikolay Aleksandrov u32 src_idx, to_send = 0; 24840ad57c99SNikolay Aleksandrov bool changed = false; 2485109865feSNikolay Aleksandrov struct br_ip src_ip; 2486109865feSNikolay Aleksandrov 2487109865feSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 2488109865feSNikolay Aleksandrov ent->flags &= ~BR_SGRP_F_SEND; 2489109865feSNikolay Aleksandrov 2490109865feSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 2491085b53c8SNikolay Aleksandrov src_ip.proto = pg->key.addr.proto; 2492109865feSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 2493e7cfcf2cSNikolay Aleksandrov memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size); 2494109865feSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 2495109865feSNikolay Aleksandrov if (ent) { 2496109865feSNikolay Aleksandrov ent->flags |= BR_SGRP_F_SEND; 2497109865feSNikolay Aleksandrov to_send++; 2498109865feSNikolay Aleksandrov } 2499109865feSNikolay Aleksandrov } 2500109865feSNikolay Aleksandrov 2501adc47037SNikolay Aleksandrov if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size, 2502adc47037SNikolay Aleksandrov grec_type)) 2503474ddb37SNikolay Aleksandrov changed = true; 2504474ddb37SNikolay Aleksandrov 2505109865feSNikolay Aleksandrov if (to_send) 2506adc47037SNikolay Aleksandrov __grp_src_query_marked_and_rexmit(brmctx, pmctx, pg); 2507109865feSNikolay Aleksandrov 25080ad57c99SNikolay Aleksandrov return changed; 2509109865feSNikolay Aleksandrov } 2510109865feSNikolay Aleksandrov 2511109865feSNikolay Aleksandrov /* State Msg type New state Actions 2512109865feSNikolay Aleksandrov * EXCLUDE (X,Y) BLOCK (A) EXCLUDE (X+(A-Y),Y) (A-X-Y)=Group Timer 2513109865feSNikolay Aleksandrov * Send Q(G,A-Y) 2514109865feSNikolay Aleksandrov */ 2515adc47037SNikolay Aleksandrov static bool __grp_src_block_excl(struct net_bridge_mcast *brmctx, 2516adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 2517adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, void *h_addr, 2518474ddb37SNikolay Aleksandrov void *srcs, u32 nsrcs, size_t addr_size, int grec_type) 2519109865feSNikolay Aleksandrov { 2520109865feSNikolay Aleksandrov struct net_bridge_group_src *ent; 2521109865feSNikolay Aleksandrov u32 src_idx, to_send = 0; 2522109865feSNikolay Aleksandrov bool changed = false; 2523109865feSNikolay Aleksandrov struct br_ip src_ip; 2524109865feSNikolay Aleksandrov 2525109865feSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 2526109865feSNikolay Aleksandrov ent->flags &= ~BR_SGRP_F_SEND; 2527109865feSNikolay Aleksandrov 2528109865feSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 2529085b53c8SNikolay Aleksandrov src_ip.proto = pg->key.addr.proto; 2530109865feSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 2531e7cfcf2cSNikolay Aleksandrov memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size); 2532109865feSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 2533109865feSNikolay Aleksandrov if (!ent) { 2534109865feSNikolay Aleksandrov ent = br_multicast_new_group_src(pg, &src_ip); 2535109865feSNikolay Aleksandrov if (ent) { 2536b0812368SNikolay Aleksandrov __grp_src_mod_timer(ent, pg->timer.expires); 2537109865feSNikolay Aleksandrov changed = true; 2538109865feSNikolay Aleksandrov } 2539109865feSNikolay Aleksandrov } 2540109865feSNikolay Aleksandrov if (ent && timer_pending(&ent->timer)) { 2541109865feSNikolay Aleksandrov ent->flags |= BR_SGRP_F_SEND; 2542109865feSNikolay Aleksandrov to_send++; 2543109865feSNikolay Aleksandrov } 2544109865feSNikolay Aleksandrov } 2545109865feSNikolay Aleksandrov 2546adc47037SNikolay Aleksandrov if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size, 2547adc47037SNikolay Aleksandrov grec_type)) 2548474ddb37SNikolay Aleksandrov changed = true; 2549474ddb37SNikolay Aleksandrov 2550109865feSNikolay Aleksandrov if (to_send) 2551adc47037SNikolay Aleksandrov __grp_src_query_marked_and_rexmit(brmctx, pmctx, pg); 2552109865feSNikolay Aleksandrov 2553109865feSNikolay Aleksandrov return changed; 2554109865feSNikolay Aleksandrov } 2555109865feSNikolay Aleksandrov 2556adc47037SNikolay Aleksandrov static bool br_multicast_block(struct net_bridge_mcast *brmctx, 2557adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 2558adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, void *h_addr, 2559474ddb37SNikolay Aleksandrov void *srcs, u32 nsrcs, size_t addr_size, int grec_type) 2560109865feSNikolay Aleksandrov { 2561109865feSNikolay Aleksandrov bool changed = false; 2562109865feSNikolay Aleksandrov 2563109865feSNikolay Aleksandrov switch (pg->filter_mode) { 2564109865feSNikolay Aleksandrov case MCAST_INCLUDE: 2565adc47037SNikolay Aleksandrov changed = __grp_src_block_incl(brmctx, pmctx, pg, h_addr, srcs, 2566adc47037SNikolay Aleksandrov nsrcs, addr_size, grec_type); 2567109865feSNikolay Aleksandrov break; 2568109865feSNikolay Aleksandrov case MCAST_EXCLUDE: 2569adc47037SNikolay Aleksandrov changed = __grp_src_block_excl(brmctx, pmctx, pg, h_addr, srcs, 2570adc47037SNikolay Aleksandrov nsrcs, addr_size, grec_type); 2571109865feSNikolay Aleksandrov break; 2572109865feSNikolay Aleksandrov } 2573109865feSNikolay Aleksandrov 2574e87e4b5cSNikolay Aleksandrov if ((pg->filter_mode == MCAST_INCLUDE && hlist_empty(&pg->src_list)) || 2575e87e4b5cSNikolay Aleksandrov br_multicast_eht_should_del_pg(pg)) { 2576d5a10222SNikolay Aleksandrov if (br_multicast_eht_should_del_pg(pg)) 2577d5a10222SNikolay Aleksandrov pg->flags |= MDB_PG_FLAGS_FAST_LEAVE; 2578e87e4b5cSNikolay Aleksandrov br_multicast_find_del_pg(pg->key.port->br, pg); 2579e87e4b5cSNikolay Aleksandrov /* a notification has already been sent and we shouldn't 2580e87e4b5cSNikolay Aleksandrov * access pg after the delete so we have to return false 2581e87e4b5cSNikolay Aleksandrov */ 2582e87e4b5cSNikolay Aleksandrov changed = false; 2583e87e4b5cSNikolay Aleksandrov } 2584e87e4b5cSNikolay Aleksandrov 2585109865feSNikolay Aleksandrov return changed; 2586109865feSNikolay Aleksandrov } 2587109865feSNikolay Aleksandrov 25880436862eSNikolay Aleksandrov static struct net_bridge_port_group * 25890436862eSNikolay Aleksandrov br_multicast_find_port(struct net_bridge_mdb_entry *mp, 25900436862eSNikolay Aleksandrov struct net_bridge_port *p, 25910436862eSNikolay Aleksandrov const unsigned char *src) 25920436862eSNikolay Aleksandrov { 2593071445c6SNikolay Aleksandrov struct net_bridge *br __maybe_unused = mp->br; 25940436862eSNikolay Aleksandrov struct net_bridge_port_group *pg; 25950436862eSNikolay Aleksandrov 25960436862eSNikolay Aleksandrov for (pg = mlock_dereference(mp->ports, br); 25970436862eSNikolay Aleksandrov pg; 25980436862eSNikolay Aleksandrov pg = mlock_dereference(pg->next, br)) 25990436862eSNikolay Aleksandrov if (br_port_group_equal(pg, p, src)) 26000436862eSNikolay Aleksandrov return pg; 26010436862eSNikolay Aleksandrov 26020436862eSNikolay Aleksandrov return NULL; 26030436862eSNikolay Aleksandrov } 26040436862eSNikolay Aleksandrov 2605adc47037SNikolay Aleksandrov static int br_ip4_multicast_igmp3_report(struct net_bridge_mcast *brmctx, 2606adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 260706499098SVlad Yasevich struct sk_buff *skb, 260806499098SVlad Yasevich u16 vid) 2609eb1d1641SHerbert Xu { 2610adc47037SNikolay Aleksandrov bool igmpv2 = brmctx->multicast_igmp_version == 2; 26110436862eSNikolay Aleksandrov struct net_bridge_mdb_entry *mdst; 26120436862eSNikolay Aleksandrov struct net_bridge_port_group *pg; 26136db6f0eaSFelix Fietkau const unsigned char *src; 2614eb1d1641SHerbert Xu struct igmpv3_report *ih; 2615eb1d1641SHerbert Xu struct igmpv3_grec *grec; 26160436862eSNikolay Aleksandrov int i, len, num, type; 261754bea721SNikolay Aleksandrov __be32 group, *h_addr; 26180436862eSNikolay Aleksandrov bool changed = false; 26190436862eSNikolay Aleksandrov int err = 0; 2620e57f6185SNikolay Aleksandrov u16 nsrcs; 2621eb1d1641SHerbert Xu 2622eb1d1641SHerbert Xu ih = igmpv3_report_hdr(skb); 2623eb1d1641SHerbert Xu num = ntohs(ih->ngrec); 2624c2d4fbd2SLinus Lüssing len = skb_transport_offset(skb) + sizeof(*ih); 2625eb1d1641SHerbert Xu 2626eb1d1641SHerbert Xu for (i = 0; i < num; i++) { 2627eb1d1641SHerbert Xu len += sizeof(*grec); 2628ba5ea614SLinus Lüssing if (!ip_mc_may_pull(skb, len)) 2629eb1d1641SHerbert Xu return -EINVAL; 2630eb1d1641SHerbert Xu 2631fd218cf9SHerbert Xu grec = (void *)(skb->data + len - sizeof(*grec)); 2632eb1d1641SHerbert Xu group = grec->grec_mca; 2633eb1d1641SHerbert Xu type = grec->grec_type; 2634e57f6185SNikolay Aleksandrov nsrcs = ntohs(grec->grec_nsrcs); 2635eb1d1641SHerbert Xu 2636e57f6185SNikolay Aleksandrov len += nsrcs * 4; 2637ba5ea614SLinus Lüssing if (!ip_mc_may_pull(skb, len)) 2638eb1d1641SHerbert Xu return -EINVAL; 2639eb1d1641SHerbert Xu 2640eb1d1641SHerbert Xu switch (type) { 2641eb1d1641SHerbert Xu case IGMPV3_MODE_IS_INCLUDE: 2642eb1d1641SHerbert Xu case IGMPV3_MODE_IS_EXCLUDE: 2643eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_INCLUDE: 2644eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_EXCLUDE: 2645eb1d1641SHerbert Xu case IGMPV3_ALLOW_NEW_SOURCES: 2646eb1d1641SHerbert Xu case IGMPV3_BLOCK_OLD_SOURCES: 2647eb1d1641SHerbert Xu break; 2648eb1d1641SHerbert Xu 2649eb1d1641SHerbert Xu default: 2650eb1d1641SHerbert Xu continue; 2651eb1d1641SHerbert Xu } 2652eb1d1641SHerbert Xu 26536db6f0eaSFelix Fietkau src = eth_hdr(skb)->h_source; 26540436862eSNikolay Aleksandrov if (nsrcs == 0 && 26550436862eSNikolay Aleksandrov (type == IGMPV3_CHANGE_TO_INCLUDE || 26560436862eSNikolay Aleksandrov type == IGMPV3_MODE_IS_INCLUDE)) { 2657adc47037SNikolay Aleksandrov if (!pmctx || igmpv2) { 2658adc47037SNikolay Aleksandrov br_ip4_multicast_leave_group(brmctx, pmctx, 2659adc47037SNikolay Aleksandrov group, vid, src); 26600436862eSNikolay Aleksandrov continue; 26610436862eSNikolay Aleksandrov } 2662bc8c20acSSatish Ashok } else { 2663adc47037SNikolay Aleksandrov err = br_ip4_multicast_add_group(brmctx, pmctx, group, 2664adc47037SNikolay Aleksandrov vid, src, igmpv2); 2665eb1d1641SHerbert Xu if (err) 2666eb1d1641SHerbert Xu break; 2667eb1d1641SHerbert Xu } 26680436862eSNikolay Aleksandrov 2669adc47037SNikolay Aleksandrov if (!pmctx || igmpv2) 26700436862eSNikolay Aleksandrov continue; 26710436862eSNikolay Aleksandrov 2672262985faSIdo Schimmel spin_lock(&brmctx->br->multicast_lock); 26734cdd0d10SNikolay Aleksandrov if (!br_multicast_ctx_should_use(brmctx, pmctx)) 26744cdd0d10SNikolay Aleksandrov goto unlock_continue; 26754cdd0d10SNikolay Aleksandrov 2676adc47037SNikolay Aleksandrov mdst = br_mdb_ip4_get(brmctx->br, group, vid); 26770436862eSNikolay Aleksandrov if (!mdst) 26780436862eSNikolay Aleksandrov goto unlock_continue; 2679adc47037SNikolay Aleksandrov pg = br_multicast_find_port(mdst, pmctx->port, src); 26800436862eSNikolay Aleksandrov if (!pg || (pg->flags & MDB_PG_FLAGS_PERMANENT)) 26810436862eSNikolay Aleksandrov goto unlock_continue; 268254bea721SNikolay Aleksandrov /* reload grec and host addr */ 26830436862eSNikolay Aleksandrov grec = (void *)(skb->data + len - sizeof(*grec) - (nsrcs * 4)); 268454bea721SNikolay Aleksandrov h_addr = &ip_hdr(skb)->saddr; 26850436862eSNikolay Aleksandrov switch (type) { 26860436862eSNikolay Aleksandrov case IGMPV3_ALLOW_NEW_SOURCES: 2687adc47037SNikolay Aleksandrov changed = br_multicast_isinc_allow(brmctx, pg, h_addr, 2688adc47037SNikolay Aleksandrov grec->grec_src, 2689474ddb37SNikolay Aleksandrov nsrcs, sizeof(__be32), type); 26900436862eSNikolay Aleksandrov break; 2691e6231bcaSNikolay Aleksandrov case IGMPV3_MODE_IS_INCLUDE: 2692adc47037SNikolay Aleksandrov changed = br_multicast_isinc_allow(brmctx, pg, h_addr, 2693adc47037SNikolay Aleksandrov grec->grec_src, 2694474ddb37SNikolay Aleksandrov nsrcs, sizeof(__be32), type); 2695e6231bcaSNikolay Aleksandrov break; 2696e6231bcaSNikolay Aleksandrov case IGMPV3_MODE_IS_EXCLUDE: 2697adc47037SNikolay Aleksandrov changed = br_multicast_isexc(brmctx, pg, h_addr, 2698adc47037SNikolay Aleksandrov grec->grec_src, 2699ddc255d9SNikolay Aleksandrov nsrcs, sizeof(__be32), type); 2700e6231bcaSNikolay Aleksandrov break; 27015bf1e00bSNikolay Aleksandrov case IGMPV3_CHANGE_TO_INCLUDE: 2702adc47037SNikolay Aleksandrov changed = br_multicast_toin(brmctx, pmctx, pg, h_addr, 2703adc47037SNikolay Aleksandrov grec->grec_src, 2704ddc255d9SNikolay Aleksandrov nsrcs, sizeof(__be32), type); 27055bf1e00bSNikolay Aleksandrov break; 27065bf1e00bSNikolay Aleksandrov case IGMPV3_CHANGE_TO_EXCLUDE: 2707adc47037SNikolay Aleksandrov changed = br_multicast_toex(brmctx, pmctx, pg, h_addr, 2708adc47037SNikolay Aleksandrov grec->grec_src, 2709ddc255d9SNikolay Aleksandrov nsrcs, sizeof(__be32), type); 27105bf1e00bSNikolay Aleksandrov break; 2711109865feSNikolay Aleksandrov case IGMPV3_BLOCK_OLD_SOURCES: 2712adc47037SNikolay Aleksandrov changed = br_multicast_block(brmctx, pmctx, pg, h_addr, 2713adc47037SNikolay Aleksandrov grec->grec_src, 2714474ddb37SNikolay Aleksandrov nsrcs, sizeof(__be32), type); 2715109865feSNikolay Aleksandrov break; 27160436862eSNikolay Aleksandrov } 27170436862eSNikolay Aleksandrov if (changed) 2718adc47037SNikolay Aleksandrov br_mdb_notify(brmctx->br->dev, mdst, pg, RTM_NEWMDB); 27190436862eSNikolay Aleksandrov unlock_continue: 2720262985faSIdo Schimmel spin_unlock(&brmctx->br->multicast_lock); 2721bc8c20acSSatish Ashok } 2722eb1d1641SHerbert Xu 2723eb1d1641SHerbert Xu return err; 2724eb1d1641SHerbert Xu } 2725eb1d1641SHerbert Xu 2726dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 2727adc47037SNikolay Aleksandrov static int br_ip6_multicast_mld2_report(struct net_bridge_mcast *brmctx, 2728adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 272906499098SVlad Yasevich struct sk_buff *skb, 273006499098SVlad Yasevich u16 vid) 273108b202b6SYOSHIFUJI Hideaki { 2732adc47037SNikolay Aleksandrov bool mldv1 = brmctx->multicast_mld_version == 1; 27330436862eSNikolay Aleksandrov struct net_bridge_mdb_entry *mdst; 27340436862eSNikolay Aleksandrov struct net_bridge_port_group *pg; 2735ba5ea614SLinus Lüssing unsigned int nsrcs_offset; 27366baeb395SMichelleJin struct mld2_report *mld2r; 27376db6f0eaSFelix Fietkau const unsigned char *src; 273854bea721SNikolay Aleksandrov struct in6_addr *h_addr; 273908b202b6SYOSHIFUJI Hideaki struct mld2_grec *grec; 2740ba5ea614SLinus Lüssing unsigned int grec_len; 27410436862eSNikolay Aleksandrov bool changed = false; 27420436862eSNikolay Aleksandrov int i, len, num; 274308b202b6SYOSHIFUJI Hideaki int err = 0; 274408b202b6SYOSHIFUJI Hideaki 27456baeb395SMichelleJin if (!ipv6_mc_may_pull(skb, sizeof(*mld2r))) 274608b202b6SYOSHIFUJI Hideaki return -EINVAL; 274708b202b6SYOSHIFUJI Hideaki 27486baeb395SMichelleJin mld2r = (struct mld2_report *)icmp6_hdr(skb); 27496baeb395SMichelleJin num = ntohs(mld2r->mld2r_ngrec); 27506baeb395SMichelleJin len = skb_transport_offset(skb) + sizeof(*mld2r); 275108b202b6SYOSHIFUJI Hideaki 275208b202b6SYOSHIFUJI Hideaki for (i = 0; i < num; i++) { 2753e57f6185SNikolay Aleksandrov __be16 *_nsrcs, __nsrcs; 2754e57f6185SNikolay Aleksandrov u16 nsrcs; 275508b202b6SYOSHIFUJI Hideaki 2756ba5ea614SLinus Lüssing nsrcs_offset = len + offsetof(struct mld2_grec, grec_nsrcs); 2757ba5ea614SLinus Lüssing 2758ba5ea614SLinus Lüssing if (skb_transport_offset(skb) + ipv6_transport_len(skb) < 27595fc6266aSLinus Lüssing nsrcs_offset + sizeof(__nsrcs)) 2760ba5ea614SLinus Lüssing return -EINVAL; 2761ba5ea614SLinus Lüssing 2762e57f6185SNikolay Aleksandrov _nsrcs = skb_header_pointer(skb, nsrcs_offset, 2763e57f6185SNikolay Aleksandrov sizeof(__nsrcs), &__nsrcs); 2764e57f6185SNikolay Aleksandrov if (!_nsrcs) 276508b202b6SYOSHIFUJI Hideaki return -EINVAL; 276608b202b6SYOSHIFUJI Hideaki 2767e57f6185SNikolay Aleksandrov nsrcs = ntohs(*_nsrcs); 2768e57f6185SNikolay Aleksandrov grec_len = struct_size(grec, grec_src, nsrcs); 2769ba5ea614SLinus Lüssing 2770ba5ea614SLinus Lüssing if (!ipv6_mc_may_pull(skb, len + grec_len)) 277108b202b6SYOSHIFUJI Hideaki return -EINVAL; 277208b202b6SYOSHIFUJI Hideaki 277308b202b6SYOSHIFUJI Hideaki grec = (struct mld2_grec *)(skb->data + len); 2774ba5ea614SLinus Lüssing len += grec_len; 277508b202b6SYOSHIFUJI Hideaki 277608b202b6SYOSHIFUJI Hideaki switch (grec->grec_type) { 277708b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_INCLUDE: 277808b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_EXCLUDE: 277908b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_INCLUDE: 278008b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_EXCLUDE: 278108b202b6SYOSHIFUJI Hideaki case MLD2_ALLOW_NEW_SOURCES: 278208b202b6SYOSHIFUJI Hideaki case MLD2_BLOCK_OLD_SOURCES: 278308b202b6SYOSHIFUJI Hideaki break; 278408b202b6SYOSHIFUJI Hideaki 278508b202b6SYOSHIFUJI Hideaki default: 278608b202b6SYOSHIFUJI Hideaki continue; 278708b202b6SYOSHIFUJI Hideaki } 278808b202b6SYOSHIFUJI Hideaki 27896db6f0eaSFelix Fietkau src = eth_hdr(skb)->h_source; 2790bc8c20acSSatish Ashok if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE || 2791bc8c20acSSatish Ashok grec->grec_type == MLD2_MODE_IS_INCLUDE) && 2792e57f6185SNikolay Aleksandrov nsrcs == 0) { 2793adc47037SNikolay Aleksandrov if (!pmctx || mldv1) { 2794adc47037SNikolay Aleksandrov br_ip6_multicast_leave_group(brmctx, pmctx, 27950436862eSNikolay Aleksandrov &grec->grec_mca, 27966db6f0eaSFelix Fietkau vid, src); 27970436862eSNikolay Aleksandrov continue; 27980436862eSNikolay Aleksandrov } 2799bc8c20acSSatish Ashok } else { 2800adc47037SNikolay Aleksandrov err = br_ip6_multicast_add_group(brmctx, pmctx, 28016db6f0eaSFelix Fietkau &grec->grec_mca, vid, 28020436862eSNikolay Aleksandrov src, mldv1); 28039264251eSDavide Caratti if (err) 280408b202b6SYOSHIFUJI Hideaki break; 280508b202b6SYOSHIFUJI Hideaki } 28060436862eSNikolay Aleksandrov 2807adc47037SNikolay Aleksandrov if (!pmctx || mldv1) 28080436862eSNikolay Aleksandrov continue; 28090436862eSNikolay Aleksandrov 2810262985faSIdo Schimmel spin_lock(&brmctx->br->multicast_lock); 28114cdd0d10SNikolay Aleksandrov if (!br_multicast_ctx_should_use(brmctx, pmctx)) 28124cdd0d10SNikolay Aleksandrov goto unlock_continue; 28134cdd0d10SNikolay Aleksandrov 2814adc47037SNikolay Aleksandrov mdst = br_mdb_ip6_get(brmctx->br, &grec->grec_mca, vid); 28150436862eSNikolay Aleksandrov if (!mdst) 28160436862eSNikolay Aleksandrov goto unlock_continue; 2817adc47037SNikolay Aleksandrov pg = br_multicast_find_port(mdst, pmctx->port, src); 28180436862eSNikolay Aleksandrov if (!pg || (pg->flags & MDB_PG_FLAGS_PERMANENT)) 28190436862eSNikolay Aleksandrov goto unlock_continue; 282054bea721SNikolay Aleksandrov h_addr = &ipv6_hdr(skb)->saddr; 28210436862eSNikolay Aleksandrov switch (grec->grec_type) { 28220436862eSNikolay Aleksandrov case MLD2_ALLOW_NEW_SOURCES: 2823adc47037SNikolay Aleksandrov changed = br_multicast_isinc_allow(brmctx, pg, h_addr, 282454bea721SNikolay Aleksandrov grec->grec_src, nsrcs, 2825474ddb37SNikolay Aleksandrov sizeof(struct in6_addr), 2826474ddb37SNikolay Aleksandrov grec->grec_type); 28270436862eSNikolay Aleksandrov break; 2828e6231bcaSNikolay Aleksandrov case MLD2_MODE_IS_INCLUDE: 2829adc47037SNikolay Aleksandrov changed = br_multicast_isinc_allow(brmctx, pg, h_addr, 283054bea721SNikolay Aleksandrov grec->grec_src, nsrcs, 2831474ddb37SNikolay Aleksandrov sizeof(struct in6_addr), 2832474ddb37SNikolay Aleksandrov grec->grec_type); 2833e6231bcaSNikolay Aleksandrov break; 2834e6231bcaSNikolay Aleksandrov case MLD2_MODE_IS_EXCLUDE: 2835adc47037SNikolay Aleksandrov changed = br_multicast_isexc(brmctx, pg, h_addr, 283654bea721SNikolay Aleksandrov grec->grec_src, nsrcs, 2837ddc255d9SNikolay Aleksandrov sizeof(struct in6_addr), 2838ddc255d9SNikolay Aleksandrov grec->grec_type); 2839e6231bcaSNikolay Aleksandrov break; 28405bf1e00bSNikolay Aleksandrov case MLD2_CHANGE_TO_INCLUDE: 2841adc47037SNikolay Aleksandrov changed = br_multicast_toin(brmctx, pmctx, pg, h_addr, 284254bea721SNikolay Aleksandrov grec->grec_src, nsrcs, 2843ddc255d9SNikolay Aleksandrov sizeof(struct in6_addr), 2844ddc255d9SNikolay Aleksandrov grec->grec_type); 28455bf1e00bSNikolay Aleksandrov break; 28465bf1e00bSNikolay Aleksandrov case MLD2_CHANGE_TO_EXCLUDE: 2847adc47037SNikolay Aleksandrov changed = br_multicast_toex(brmctx, pmctx, pg, h_addr, 284854bea721SNikolay Aleksandrov grec->grec_src, nsrcs, 2849ddc255d9SNikolay Aleksandrov sizeof(struct in6_addr), 2850ddc255d9SNikolay Aleksandrov grec->grec_type); 28515bf1e00bSNikolay Aleksandrov break; 2852109865feSNikolay Aleksandrov case MLD2_BLOCK_OLD_SOURCES: 2853adc47037SNikolay Aleksandrov changed = br_multicast_block(brmctx, pmctx, pg, h_addr, 285454bea721SNikolay Aleksandrov grec->grec_src, nsrcs, 2855474ddb37SNikolay Aleksandrov sizeof(struct in6_addr), 2856474ddb37SNikolay Aleksandrov grec->grec_type); 2857109865feSNikolay Aleksandrov break; 28580436862eSNikolay Aleksandrov } 28590436862eSNikolay Aleksandrov if (changed) 2860adc47037SNikolay Aleksandrov br_mdb_notify(brmctx->br->dev, mdst, pg, RTM_NEWMDB); 28610436862eSNikolay Aleksandrov unlock_continue: 2862262985faSIdo Schimmel spin_unlock(&brmctx->br->multicast_lock); 2863bc8c20acSSatish Ashok } 286408b202b6SYOSHIFUJI Hideaki 286508b202b6SYOSHIFUJI Hideaki return err; 286608b202b6SYOSHIFUJI Hideaki } 286708b202b6SYOSHIFUJI Hideaki #endif 286808b202b6SYOSHIFUJI Hideaki 2869c3fb3698SNikolay Aleksandrov static bool br_multicast_select_querier(struct net_bridge_mcast *brmctx, 287054cb4319SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 287167b746f9SNikolay Aleksandrov struct br_ip *saddr) 2872dc4eb53aSLinus Lüssing { 287367b746f9SNikolay Aleksandrov int port_ifidx = pmctx ? pmctx->port->dev->ifindex : 0; 2874c3fb3698SNikolay Aleksandrov struct timer_list *own_timer, *other_timer; 2875c3fb3698SNikolay Aleksandrov struct bridge_mcast_querier *querier; 287654cb4319SNikolay Aleksandrov 2877c3fb3698SNikolay Aleksandrov switch (saddr->proto) { 2878c3fb3698SNikolay Aleksandrov case htons(ETH_P_IP): 2879c3fb3698SNikolay Aleksandrov querier = &brmctx->ip4_querier; 2880c3fb3698SNikolay Aleksandrov own_timer = &brmctx->ip4_own_query.timer; 2881c3fb3698SNikolay Aleksandrov other_timer = &brmctx->ip4_other_query.timer; 2882c3fb3698SNikolay Aleksandrov if (!querier->addr.src.ip4 || 2883c3fb3698SNikolay Aleksandrov ntohl(saddr->src.ip4) <= ntohl(querier->addr.src.ip4)) 2884dc4eb53aSLinus Lüssing goto update; 2885c3fb3698SNikolay Aleksandrov break; 2886dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 2887c3fb3698SNikolay Aleksandrov case htons(ETH_P_IPV6): 2888c3fb3698SNikolay Aleksandrov querier = &brmctx->ip6_querier; 2889c3fb3698SNikolay Aleksandrov own_timer = &brmctx->ip6_own_query.timer; 2890c3fb3698SNikolay Aleksandrov other_timer = &brmctx->ip6_other_query.timer; 2891c3fb3698SNikolay Aleksandrov if (ipv6_addr_cmp(&saddr->src.ip6, &querier->addr.src.ip6) <= 0) 2892dc4eb53aSLinus Lüssing goto update; 2893c3fb3698SNikolay Aleksandrov break; 2894c3fb3698SNikolay Aleksandrov #endif 2895c3fb3698SNikolay Aleksandrov default: 2896c3fb3698SNikolay Aleksandrov return false; 2897c3fb3698SNikolay Aleksandrov } 2898dc4eb53aSLinus Lüssing 2899c3fb3698SNikolay Aleksandrov if (!timer_pending(own_timer) && !timer_pending(other_timer)) 2900dc4eb53aSLinus Lüssing goto update; 2901dc4eb53aSLinus Lüssing 2902dc4eb53aSLinus Lüssing return false; 2903dc4eb53aSLinus Lüssing 2904dc4eb53aSLinus Lüssing update: 2905c3fb3698SNikolay Aleksandrov br_multicast_update_querier(brmctx, querier, port_ifidx, saddr); 29062cd41431SLinus Lüssing 2907dc4eb53aSLinus Lüssing return true; 2908dc4eb53aSLinus Lüssing } 2909dc4eb53aSLinus Lüssing 2910c7fa1d9bSNikolay Aleksandrov static struct net_bridge_port * 2911c7fa1d9bSNikolay Aleksandrov __br_multicast_get_querier_port(struct net_bridge *br, 2912c7fa1d9bSNikolay Aleksandrov const struct bridge_mcast_querier *querier) 2913c7fa1d9bSNikolay Aleksandrov { 2914c7fa1d9bSNikolay Aleksandrov int port_ifidx = READ_ONCE(querier->port_ifidx); 2915c7fa1d9bSNikolay Aleksandrov struct net_bridge_port *p; 2916c7fa1d9bSNikolay Aleksandrov struct net_device *dev; 2917c7fa1d9bSNikolay Aleksandrov 2918c7fa1d9bSNikolay Aleksandrov if (port_ifidx == 0) 2919c7fa1d9bSNikolay Aleksandrov return NULL; 2920c7fa1d9bSNikolay Aleksandrov 2921c7fa1d9bSNikolay Aleksandrov dev = dev_get_by_index_rcu(dev_net(br->dev), port_ifidx); 2922c7fa1d9bSNikolay Aleksandrov if (!dev) 2923c7fa1d9bSNikolay Aleksandrov return NULL; 2924c7fa1d9bSNikolay Aleksandrov p = br_port_get_rtnl_rcu(dev); 2925c7fa1d9bSNikolay Aleksandrov if (!p || p->br != br) 2926c7fa1d9bSNikolay Aleksandrov return NULL; 2927c7fa1d9bSNikolay Aleksandrov 2928c7fa1d9bSNikolay Aleksandrov return p; 2929c7fa1d9bSNikolay Aleksandrov } 2930c7fa1d9bSNikolay Aleksandrov 2931c7fa1d9bSNikolay Aleksandrov size_t br_multicast_querier_state_size(void) 2932c7fa1d9bSNikolay Aleksandrov { 2933cdda378bSNikolay Aleksandrov return nla_total_size(0) + /* nest attribute */ 2934c7fa1d9bSNikolay Aleksandrov nla_total_size(sizeof(__be32)) + /* BRIDGE_QUERIER_IP_ADDRESS */ 2935c7fa1d9bSNikolay Aleksandrov nla_total_size(sizeof(int)) + /* BRIDGE_QUERIER_IP_PORT */ 2936175e6692SNikolay Aleksandrov nla_total_size_64bit(sizeof(u64)) + /* BRIDGE_QUERIER_IP_OTHER_TIMER */ 2937175e6692SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 2938175e6692SNikolay Aleksandrov nla_total_size(sizeof(struct in6_addr)) + /* BRIDGE_QUERIER_IPV6_ADDRESS */ 2939175e6692SNikolay Aleksandrov nla_total_size(sizeof(int)) + /* BRIDGE_QUERIER_IPV6_PORT */ 2940175e6692SNikolay Aleksandrov nla_total_size_64bit(sizeof(u64)) + /* BRIDGE_QUERIER_IPV6_OTHER_TIMER */ 2941175e6692SNikolay Aleksandrov #endif 2942175e6692SNikolay Aleksandrov 0; 2943c7fa1d9bSNikolay Aleksandrov } 2944c7fa1d9bSNikolay Aleksandrov 2945c7fa1d9bSNikolay Aleksandrov /* protected by rtnl or rcu */ 2946c7fa1d9bSNikolay Aleksandrov int br_multicast_dump_querier_state(struct sk_buff *skb, 2947c7fa1d9bSNikolay Aleksandrov const struct net_bridge_mcast *brmctx, 2948c7fa1d9bSNikolay Aleksandrov int nest_attr) 2949c7fa1d9bSNikolay Aleksandrov { 2950c7fa1d9bSNikolay Aleksandrov struct bridge_mcast_querier querier = {}; 2951c7fa1d9bSNikolay Aleksandrov struct net_bridge_port *p; 2952c7fa1d9bSNikolay Aleksandrov struct nlattr *nest; 2953c7fa1d9bSNikolay Aleksandrov 2954f137b7d4SNikolay Aleksandrov if (!br_opt_get(brmctx->br, BROPT_MULTICAST_ENABLED) || 2955f137b7d4SNikolay Aleksandrov br_multicast_ctx_vlan_global_disabled(brmctx)) 2956f137b7d4SNikolay Aleksandrov return 0; 2957f137b7d4SNikolay Aleksandrov 2958c7fa1d9bSNikolay Aleksandrov nest = nla_nest_start(skb, nest_attr); 2959c7fa1d9bSNikolay Aleksandrov if (!nest) 2960c7fa1d9bSNikolay Aleksandrov return -EMSGSIZE; 2961c7fa1d9bSNikolay Aleksandrov 2962c7fa1d9bSNikolay Aleksandrov rcu_read_lock(); 296385b41082SNikolay Aleksandrov if (!brmctx->multicast_querier && 296485b41082SNikolay Aleksandrov !timer_pending(&brmctx->ip4_other_query.timer)) 296585b41082SNikolay Aleksandrov goto out_v6; 296685b41082SNikolay Aleksandrov 2967c7fa1d9bSNikolay Aleksandrov br_multicast_read_querier(&brmctx->ip4_querier, &querier); 2968c7fa1d9bSNikolay Aleksandrov if (nla_put_in_addr(skb, BRIDGE_QUERIER_IP_ADDRESS, 2969c7fa1d9bSNikolay Aleksandrov querier.addr.src.ip4)) { 2970c7fa1d9bSNikolay Aleksandrov rcu_read_unlock(); 2971c7fa1d9bSNikolay Aleksandrov goto out_err; 2972c7fa1d9bSNikolay Aleksandrov } 2973c7fa1d9bSNikolay Aleksandrov 2974c7fa1d9bSNikolay Aleksandrov p = __br_multicast_get_querier_port(brmctx->br, &querier); 2975c7fa1d9bSNikolay Aleksandrov if (timer_pending(&brmctx->ip4_other_query.timer) && 2976c7fa1d9bSNikolay Aleksandrov (nla_put_u64_64bit(skb, BRIDGE_QUERIER_IP_OTHER_TIMER, 2977c7fa1d9bSNikolay Aleksandrov br_timer_value(&brmctx->ip4_other_query.timer), 2978c7fa1d9bSNikolay Aleksandrov BRIDGE_QUERIER_PAD) || 2979c7fa1d9bSNikolay Aleksandrov (p && nla_put_u32(skb, BRIDGE_QUERIER_IP_PORT, p->dev->ifindex)))) { 2980c7fa1d9bSNikolay Aleksandrov rcu_read_unlock(); 2981c7fa1d9bSNikolay Aleksandrov goto out_err; 2982c7fa1d9bSNikolay Aleksandrov } 298385b41082SNikolay Aleksandrov 298485b41082SNikolay Aleksandrov out_v6: 298585b41082SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 298685b41082SNikolay Aleksandrov if (!brmctx->multicast_querier && 298785b41082SNikolay Aleksandrov !timer_pending(&brmctx->ip6_other_query.timer)) 298885b41082SNikolay Aleksandrov goto out; 298985b41082SNikolay Aleksandrov 299085b41082SNikolay Aleksandrov br_multicast_read_querier(&brmctx->ip6_querier, &querier); 299185b41082SNikolay Aleksandrov if (nla_put_in6_addr(skb, BRIDGE_QUERIER_IPV6_ADDRESS, 299285b41082SNikolay Aleksandrov &querier.addr.src.ip6)) { 299385b41082SNikolay Aleksandrov rcu_read_unlock(); 299485b41082SNikolay Aleksandrov goto out_err; 299585b41082SNikolay Aleksandrov } 299685b41082SNikolay Aleksandrov 299785b41082SNikolay Aleksandrov p = __br_multicast_get_querier_port(brmctx->br, &querier); 299885b41082SNikolay Aleksandrov if (timer_pending(&brmctx->ip6_other_query.timer) && 299985b41082SNikolay Aleksandrov (nla_put_u64_64bit(skb, BRIDGE_QUERIER_IPV6_OTHER_TIMER, 300085b41082SNikolay Aleksandrov br_timer_value(&brmctx->ip6_other_query.timer), 300185b41082SNikolay Aleksandrov BRIDGE_QUERIER_PAD) || 300285b41082SNikolay Aleksandrov (p && nla_put_u32(skb, BRIDGE_QUERIER_IPV6_PORT, 300385b41082SNikolay Aleksandrov p->dev->ifindex)))) { 300485b41082SNikolay Aleksandrov rcu_read_unlock(); 300585b41082SNikolay Aleksandrov goto out_err; 300685b41082SNikolay Aleksandrov } 300785b41082SNikolay Aleksandrov out: 300885b41082SNikolay Aleksandrov #endif 3009c7fa1d9bSNikolay Aleksandrov rcu_read_unlock(); 3010c7fa1d9bSNikolay Aleksandrov nla_nest_end(skb, nest); 301185b41082SNikolay Aleksandrov if (!nla_len(nest)) 301285b41082SNikolay Aleksandrov nla_nest_cancel(skb, nest); 3013c7fa1d9bSNikolay Aleksandrov 3014c7fa1d9bSNikolay Aleksandrov return 0; 3015c7fa1d9bSNikolay Aleksandrov 3016c7fa1d9bSNikolay Aleksandrov out_err: 3017c7fa1d9bSNikolay Aleksandrov nla_nest_cancel(skb, nest); 3018c7fa1d9bSNikolay Aleksandrov return -EMSGSIZE; 3019c7fa1d9bSNikolay Aleksandrov } 3020c7fa1d9bSNikolay Aleksandrov 3021cc0fdd80SLinus Lüssing static void 3022adc47037SNikolay Aleksandrov br_multicast_update_query_timer(struct net_bridge_mcast *brmctx, 302390010b36SLinus Lüssing struct bridge_mcast_other_query *query, 3024b00589afSLinus Lüssing unsigned long max_delay) 3025b00589afSLinus Lüssing { 302690010b36SLinus Lüssing if (!timer_pending(&query->timer)) 302790010b36SLinus Lüssing query->delay_time = jiffies + max_delay; 3028b00589afSLinus Lüssing 3029adc47037SNikolay Aleksandrov mod_timer(&query->timer, jiffies + brmctx->multicast_querier_interval); 3030b00589afSLinus Lüssing } 3031b00589afSLinus Lüssing 30326d549648SNogah Frankel static void br_port_mc_router_state_change(struct net_bridge_port *p, 30336d549648SNogah Frankel bool is_mc_router) 30346d549648SNogah Frankel { 30356d549648SNogah Frankel struct switchdev_attr attr = { 30366d549648SNogah Frankel .orig_dev = p->dev, 30376d549648SNogah Frankel .id = SWITCHDEV_ATTR_ID_PORT_MROUTER, 30386d549648SNogah Frankel .flags = SWITCHDEV_F_DEFER, 30396d549648SNogah Frankel .u.mrouter = is_mc_router, 30406d549648SNogah Frankel }; 30416d549648SNogah Frankel 3042dcbdf135SVladimir Oltean switchdev_port_attr_set(p->dev, &attr, NULL); 30436d549648SNogah Frankel } 30446d549648SNogah Frankel 3045d9b8c4d8SLinus Lüssing static struct net_bridge_port * 3046d3d065c0SNikolay Aleksandrov br_multicast_rport_from_node(struct net_bridge_mcast *brmctx, 3047d9b8c4d8SLinus Lüssing struct hlist_head *mc_router_list, 3048d9b8c4d8SLinus Lüssing struct hlist_node *rlist) 3049d9b8c4d8SLinus Lüssing { 30509632233eSNikolay Aleksandrov struct net_bridge_mcast_port *pmctx; 30519632233eSNikolay Aleksandrov 3052a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 3053d3d065c0SNikolay Aleksandrov if (mc_router_list == &brmctx->ip6_mc_router_list) 30549632233eSNikolay Aleksandrov pmctx = hlist_entry(rlist, struct net_bridge_mcast_port, 30559632233eSNikolay Aleksandrov ip6_rlist); 30569632233eSNikolay Aleksandrov else 3057a3c02e76SLinus Lüssing #endif 30589632233eSNikolay Aleksandrov pmctx = hlist_entry(rlist, struct net_bridge_mcast_port, 30599632233eSNikolay Aleksandrov ip4_rlist); 30609632233eSNikolay Aleksandrov 30619632233eSNikolay Aleksandrov return pmctx->port; 3062d9b8c4d8SLinus Lüssing } 3063d9b8c4d8SLinus Lüssing 3064d9b8c4d8SLinus Lüssing static struct hlist_node * 3065d3d065c0SNikolay Aleksandrov br_multicast_get_rport_slot(struct net_bridge_mcast *brmctx, 3066d9b8c4d8SLinus Lüssing struct net_bridge_port *port, 3067d9b8c4d8SLinus Lüssing struct hlist_head *mc_router_list) 3068d9b8c4d8SLinus Lüssing 3069d9b8c4d8SLinus Lüssing { 3070d9b8c4d8SLinus Lüssing struct hlist_node *slot = NULL; 3071d9b8c4d8SLinus Lüssing struct net_bridge_port *p; 3072d9b8c4d8SLinus Lüssing struct hlist_node *rlist; 3073d9b8c4d8SLinus Lüssing 3074d9b8c4d8SLinus Lüssing hlist_for_each(rlist, mc_router_list) { 3075d3d065c0SNikolay Aleksandrov p = br_multicast_rport_from_node(brmctx, mc_router_list, rlist); 3076d9b8c4d8SLinus Lüssing 3077d9b8c4d8SLinus Lüssing if ((unsigned long)port >= (unsigned long)p) 3078d9b8c4d8SLinus Lüssing break; 3079d9b8c4d8SLinus Lüssing 3080d9b8c4d8SLinus Lüssing slot = rlist; 3081d9b8c4d8SLinus Lüssing } 3082d9b8c4d8SLinus Lüssing 3083d9b8c4d8SLinus Lüssing return slot; 3084d9b8c4d8SLinus Lüssing } 3085d9b8c4d8SLinus Lüssing 3086adc47037SNikolay Aleksandrov static bool br_multicast_no_router_otherpf(struct net_bridge_mcast_port *pmctx, 3087a3c02e76SLinus Lüssing struct hlist_node *rnode) 3088a3c02e76SLinus Lüssing { 3089a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 3090adc47037SNikolay Aleksandrov if (rnode != &pmctx->ip6_rlist) 3091adc47037SNikolay Aleksandrov return hlist_unhashed(&pmctx->ip6_rlist); 3092a3c02e76SLinus Lüssing else 3093adc47037SNikolay Aleksandrov return hlist_unhashed(&pmctx->ip4_rlist); 3094a3c02e76SLinus Lüssing #else 3095a3c02e76SLinus Lüssing return true; 3096a3c02e76SLinus Lüssing #endif 3097a3c02e76SLinus Lüssing } 3098a3c02e76SLinus Lüssing 3099d9b8c4d8SLinus Lüssing /* Add port to router_list 31007e80c124Sstephen hemminger * list is maintained ordered by pointer value 31017e80c124Sstephen hemminger * and locked by br->multicast_lock and RCU 31027e80c124Sstephen hemminger */ 3103d3d065c0SNikolay Aleksandrov static void br_multicast_add_router(struct net_bridge_mcast *brmctx, 3104adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 3105d9b8c4d8SLinus Lüssing struct hlist_node *rlist, 3106d9b8c4d8SLinus Lüssing struct hlist_head *mc_router_list) 31070909e117SHerbert Xu { 3108d9b8c4d8SLinus Lüssing struct hlist_node *slot; 31090909e117SHerbert Xu 3110d9b8c4d8SLinus Lüssing if (!hlist_unhashed(rlist)) 31111a040eacSNikolay Aleksandrov return; 31121a040eacSNikolay Aleksandrov 3113adc47037SNikolay Aleksandrov slot = br_multicast_get_rport_slot(brmctx, pmctx->port, mc_router_list); 31140909e117SHerbert Xu 31157e80c124Sstephen hemminger if (slot) 3116d9b8c4d8SLinus Lüssing hlist_add_behind_rcu(rlist, slot); 3117dcdca2c4Sstephen hemminger else 3118d9b8c4d8SLinus Lüssing hlist_add_head_rcu(rlist, mc_router_list); 3119d9b8c4d8SLinus Lüssing 3120a3c02e76SLinus Lüssing /* For backwards compatibility for now, only notify if we 3121a3c02e76SLinus Lüssing * switched from no IPv4/IPv6 multicast router to a new 3122a3c02e76SLinus Lüssing * IPv4 or IPv6 multicast router. 3123a3c02e76SLinus Lüssing */ 3124adc47037SNikolay Aleksandrov if (br_multicast_no_router_otherpf(pmctx, rlist)) { 31251e9ca456SNikolay Aleksandrov br_rtr_notify(pmctx->port->br->dev, pmctx, RTM_NEWMDB); 3126adc47037SNikolay Aleksandrov br_port_mc_router_state_change(pmctx->port, true); 31270909e117SHerbert Xu } 3128a3c02e76SLinus Lüssing } 31290909e117SHerbert Xu 3130d9b8c4d8SLinus Lüssing /* Add port to router_list 3131d9b8c4d8SLinus Lüssing * list is maintained ordered by pointer value 3132d9b8c4d8SLinus Lüssing * and locked by br->multicast_lock and RCU 3133d9b8c4d8SLinus Lüssing */ 3134adc47037SNikolay Aleksandrov static void br_ip4_multicast_add_router(struct net_bridge_mcast *brmctx, 3135adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx) 3136eb1d1641SHerbert Xu { 3137adc47037SNikolay Aleksandrov br_multicast_add_router(brmctx, pmctx, &pmctx->ip4_rlist, 3138adc47037SNikolay Aleksandrov &brmctx->ip4_mc_router_list); 3139d9b8c4d8SLinus Lüssing } 3140d9b8c4d8SLinus Lüssing 3141a3c02e76SLinus Lüssing /* Add port to router_list 3142a3c02e76SLinus Lüssing * list is maintained ordered by pointer value 3143a3c02e76SLinus Lüssing * and locked by br->multicast_lock and RCU 3144a3c02e76SLinus Lüssing */ 3145adc47037SNikolay Aleksandrov static void br_ip6_multicast_add_router(struct net_bridge_mcast *brmctx, 3146adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx) 3147a3c02e76SLinus Lüssing { 3148a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 3149adc47037SNikolay Aleksandrov br_multicast_add_router(brmctx, pmctx, &pmctx->ip6_rlist, 3150adc47037SNikolay Aleksandrov &brmctx->ip6_mc_router_list); 3151a3c02e76SLinus Lüssing #endif 3152a3c02e76SLinus Lüssing } 3153a3c02e76SLinus Lüssing 3154adc47037SNikolay Aleksandrov static void br_multicast_mark_router(struct net_bridge_mcast *brmctx, 3155adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 3156d9b8c4d8SLinus Lüssing struct timer_list *timer, 3157d9b8c4d8SLinus Lüssing struct hlist_node *rlist, 3158d9b8c4d8SLinus Lüssing struct hlist_head *mc_router_list) 3159d9b8c4d8SLinus Lüssing { 3160eb1d1641SHerbert Xu unsigned long now = jiffies; 3161eb1d1641SHerbert Xu 31624cdd0d10SNikolay Aleksandrov if (!br_multicast_ctx_should_use(brmctx, pmctx)) 31634cdd0d10SNikolay Aleksandrov return; 31644cdd0d10SNikolay Aleksandrov 3165adc47037SNikolay Aleksandrov if (!pmctx) { 3166d3d065c0SNikolay Aleksandrov if (brmctx->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) { 3167d3d065c0SNikolay Aleksandrov if (!br_ip4_multicast_is_router(brmctx) && 3168d3d065c0SNikolay Aleksandrov !br_ip6_multicast_is_router(brmctx)) 3169adc47037SNikolay Aleksandrov br_mc_router_state_change(brmctx->br, true); 3170d3d065c0SNikolay Aleksandrov mod_timer(timer, now + brmctx->multicast_querier_interval); 317177041420SYotam Gigi } 3172eb1d1641SHerbert Xu return; 3173eb1d1641SHerbert Xu } 3174eb1d1641SHerbert Xu 3175adc47037SNikolay Aleksandrov if (pmctx->multicast_router == MDB_RTR_TYPE_DISABLED || 3176adc47037SNikolay Aleksandrov pmctx->multicast_router == MDB_RTR_TYPE_PERM) 3177eb1d1641SHerbert Xu return; 3178eb1d1641SHerbert Xu 3179adc47037SNikolay Aleksandrov br_multicast_add_router(brmctx, pmctx, rlist, mc_router_list); 3180d3d065c0SNikolay Aleksandrov mod_timer(timer, now + brmctx->multicast_querier_interval); 3181d9b8c4d8SLinus Lüssing } 3182eb1d1641SHerbert Xu 3183adc47037SNikolay Aleksandrov static void br_ip4_multicast_mark_router(struct net_bridge_mcast *brmctx, 3184adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx) 3185d9b8c4d8SLinus Lüssing { 3186adc47037SNikolay Aleksandrov struct timer_list *timer = &brmctx->ip4_mc_router_timer; 3187d9b8c4d8SLinus Lüssing struct hlist_node *rlist = NULL; 3188d9b8c4d8SLinus Lüssing 3189adc47037SNikolay Aleksandrov if (pmctx) { 3190adc47037SNikolay Aleksandrov timer = &pmctx->ip4_mc_router_timer; 3191adc47037SNikolay Aleksandrov rlist = &pmctx->ip4_rlist; 3192d9b8c4d8SLinus Lüssing } 3193d9b8c4d8SLinus Lüssing 3194adc47037SNikolay Aleksandrov br_multicast_mark_router(brmctx, pmctx, timer, rlist, 3195adc47037SNikolay Aleksandrov &brmctx->ip4_mc_router_list); 3196eb1d1641SHerbert Xu } 3197eb1d1641SHerbert Xu 3198adc47037SNikolay Aleksandrov static void br_ip6_multicast_mark_router(struct net_bridge_mcast *brmctx, 3199adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx) 3200a3c02e76SLinus Lüssing { 3201a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 3202adc47037SNikolay Aleksandrov struct timer_list *timer = &brmctx->ip6_mc_router_timer; 3203a3c02e76SLinus Lüssing struct hlist_node *rlist = NULL; 3204a3c02e76SLinus Lüssing 3205adc47037SNikolay Aleksandrov if (pmctx) { 3206adc47037SNikolay Aleksandrov timer = &pmctx->ip6_mc_router_timer; 3207adc47037SNikolay Aleksandrov rlist = &pmctx->ip6_rlist; 3208a3c02e76SLinus Lüssing } 3209a3c02e76SLinus Lüssing 3210adc47037SNikolay Aleksandrov br_multicast_mark_router(brmctx, pmctx, timer, rlist, 3211adc47037SNikolay Aleksandrov &brmctx->ip6_mc_router_list); 3212a3c02e76SLinus Lüssing #endif 3213a3c02e76SLinus Lüssing } 3214a3c02e76SLinus Lüssing 3215b19232efSLinus Lüssing static void 3216adc47037SNikolay Aleksandrov br_ip4_multicast_query_received(struct net_bridge_mcast *brmctx, 3217adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 321890010b36SLinus Lüssing struct bridge_mcast_other_query *query, 3219dc4eb53aSLinus Lüssing struct br_ip *saddr, 3220b00589afSLinus Lüssing unsigned long max_delay) 3221eb1d1641SHerbert Xu { 3222c3fb3698SNikolay Aleksandrov if (!br_multicast_select_querier(brmctx, pmctx, saddr)) 3223eb1d1641SHerbert Xu return; 3224eb1d1641SHerbert Xu 3225adc47037SNikolay Aleksandrov br_multicast_update_query_timer(brmctx, query, max_delay); 3226adc47037SNikolay Aleksandrov br_ip4_multicast_mark_router(brmctx, pmctx); 3227eb1d1641SHerbert Xu } 3228eb1d1641SHerbert Xu 3229b19232efSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 3230b19232efSLinus Lüssing static void 3231adc47037SNikolay Aleksandrov br_ip6_multicast_query_received(struct net_bridge_mcast *brmctx, 3232adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 3233b19232efSLinus Lüssing struct bridge_mcast_other_query *query, 3234b19232efSLinus Lüssing struct br_ip *saddr, 3235b19232efSLinus Lüssing unsigned long max_delay) 3236b19232efSLinus Lüssing { 3237c3fb3698SNikolay Aleksandrov if (!br_multicast_select_querier(brmctx, pmctx, saddr)) 3238b19232efSLinus Lüssing return; 3239b19232efSLinus Lüssing 3240adc47037SNikolay Aleksandrov br_multicast_update_query_timer(brmctx, query, max_delay); 3241adc47037SNikolay Aleksandrov br_ip6_multicast_mark_router(brmctx, pmctx); 3242b19232efSLinus Lüssing } 3243b19232efSLinus Lüssing #endif 3244b19232efSLinus Lüssing 3245adc47037SNikolay Aleksandrov static void br_ip4_multicast_query(struct net_bridge_mcast *brmctx, 3246adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 324706499098SVlad Yasevich struct sk_buff *skb, 324806499098SVlad Yasevich u16 vid) 3249eb1d1641SHerbert Xu { 3250ba5ea614SLinus Lüssing unsigned int transport_len = ip_transport_len(skb); 3251b71d1d42SEric Dumazet const struct iphdr *iph = ip_hdr(skb); 3252eb1d1641SHerbert Xu struct igmphdr *ih = igmp_hdr(skb); 3253eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 3254eb1d1641SHerbert Xu struct igmpv3_query *ih3; 3255eb1d1641SHerbert Xu struct net_bridge_port_group *p; 3256e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 325767b746f9SNikolay Aleksandrov struct br_ip saddr = {}; 3258eb1d1641SHerbert Xu unsigned long max_delay; 3259eb1d1641SHerbert Xu unsigned long now = jiffies; 3260eb1d1641SHerbert Xu __be32 group; 3261eb1d1641SHerbert Xu 3262adc47037SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 32634cdd0d10SNikolay Aleksandrov if (!br_multicast_ctx_should_use(brmctx, pmctx)) 3264eb1d1641SHerbert Xu goto out; 3265eb1d1641SHerbert Xu 3266eb1d1641SHerbert Xu group = ih->group; 3267eb1d1641SHerbert Xu 3268ba5ea614SLinus Lüssing if (transport_len == sizeof(*ih)) { 3269eb1d1641SHerbert Xu max_delay = ih->code * (HZ / IGMP_TIMER_SCALE); 3270eb1d1641SHerbert Xu 3271eb1d1641SHerbert Xu if (!max_delay) { 3272eb1d1641SHerbert Xu max_delay = 10 * HZ; 3273eb1d1641SHerbert Xu group = 0; 3274eb1d1641SHerbert Xu } 3275ba5ea614SLinus Lüssing } else if (transport_len >= sizeof(*ih3)) { 3276eb1d1641SHerbert Xu ih3 = igmpv3_query_hdr(skb); 327723550b83SNikolay Aleksandrov if (ih3->nsrcs || 3278d3d065c0SNikolay Aleksandrov (brmctx->multicast_igmp_version == 3 && group && 3279d3d065c0SNikolay Aleksandrov ih3->suppress)) 3280bec68ff1SYOSHIFUJI Hideaki goto out; 3281eb1d1641SHerbert Xu 32820ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 max_delay = ih3->code ? 32830ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1; 32849afd85c9SLinus Lüssing } else { 32859ed973ccSLinus Lüssing goto out; 32869ed973ccSLinus Lüssing } 32879ed973ccSLinus Lüssing 3288dc4eb53aSLinus Lüssing if (!group) { 3289dc4eb53aSLinus Lüssing saddr.proto = htons(ETH_P_IP); 3290deb96566SNikolay Aleksandrov saddr.src.ip4 = iph->saddr; 3291b00589afSLinus Lüssing 3292adc47037SNikolay Aleksandrov br_ip4_multicast_query_received(brmctx, pmctx, 3293d3d065c0SNikolay Aleksandrov &brmctx->ip4_other_query, 3294dc4eb53aSLinus Lüssing &saddr, max_delay); 3295eb1d1641SHerbert Xu goto out; 3296dc4eb53aSLinus Lüssing } 3297eb1d1641SHerbert Xu 3298adc47037SNikolay Aleksandrov mp = br_mdb_ip4_get(brmctx->br, group, vid); 3299eb1d1641SHerbert Xu if (!mp) 3300eb1d1641SHerbert Xu goto out; 3301eb1d1641SHerbert Xu 3302d3d065c0SNikolay Aleksandrov max_delay *= brmctx->multicast_last_member_count; 3303eb1d1641SHerbert Xu 3304ff0fd34eSAndrew Lunn if (mp->host_joined && 3305eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 3306eb1d1641SHerbert Xu time_after(mp->timer.expires, now + max_delay) : 3307eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) 3308eb1d1641SHerbert Xu mod_timer(&mp->timer, now + max_delay); 3309eb1d1641SHerbert Xu 3310e8051688SEric Dumazet for (pp = &mp->ports; 3311adc47037SNikolay Aleksandrov (p = mlock_dereference(*pp, brmctx->br)) != NULL; 3312e8051688SEric Dumazet pp = &p->next) { 3313eb1d1641SHerbert Xu if (timer_pending(&p->timer) ? 3314eb1d1641SHerbert Xu time_after(p->timer.expires, now + max_delay) : 331523550b83SNikolay Aleksandrov try_to_del_timer_sync(&p->timer) >= 0 && 3316d3d065c0SNikolay Aleksandrov (brmctx->multicast_igmp_version == 2 || 331723550b83SNikolay Aleksandrov p->filter_mode == MCAST_EXCLUDE)) 331824f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 3319eb1d1641SHerbert Xu } 3320eb1d1641SHerbert Xu 3321eb1d1641SHerbert Xu out: 3322adc47037SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 3323eb1d1641SHerbert Xu } 3324eb1d1641SHerbert Xu 3325dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 3326adc47037SNikolay Aleksandrov static int br_ip6_multicast_query(struct net_bridge_mcast *brmctx, 3327adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 332806499098SVlad Yasevich struct sk_buff *skb, 332906499098SVlad Yasevich u16 vid) 333008b202b6SYOSHIFUJI Hideaki { 3331ba5ea614SLinus Lüssing unsigned int transport_len = ipv6_transport_len(skb); 3332eca2a43bSstephen hemminger struct mld_msg *mld; 333308b202b6SYOSHIFUJI Hideaki struct net_bridge_mdb_entry *mp; 333408b202b6SYOSHIFUJI Hideaki struct mld2_query *mld2q; 3335e8051688SEric Dumazet struct net_bridge_port_group *p; 3336e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 333767b746f9SNikolay Aleksandrov struct br_ip saddr = {}; 333808b202b6SYOSHIFUJI Hideaki unsigned long max_delay; 333908b202b6SYOSHIFUJI Hideaki unsigned long now = jiffies; 3340856ce5d0SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 3341b71d1d42SEric Dumazet const struct in6_addr *group = NULL; 33429ed973ccSLinus Lüssing bool is_general_query; 334308b202b6SYOSHIFUJI Hideaki int err = 0; 334408b202b6SYOSHIFUJI Hideaki 3345adc47037SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 33464cdd0d10SNikolay Aleksandrov if (!br_multicast_ctx_should_use(brmctx, pmctx)) 334708b202b6SYOSHIFUJI Hideaki goto out; 334808b202b6SYOSHIFUJI Hideaki 3349ba5ea614SLinus Lüssing if (transport_len == sizeof(*mld)) { 3350856ce5d0SLinus Lüssing if (!pskb_may_pull(skb, offset + sizeof(*mld))) { 335108b202b6SYOSHIFUJI Hideaki err = -EINVAL; 335208b202b6SYOSHIFUJI Hideaki goto out; 335308b202b6SYOSHIFUJI Hideaki } 335408b202b6SYOSHIFUJI Hideaki mld = (struct mld_msg *) icmp6_hdr(skb); 33554715213dSLi RongQing max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay)); 335608b202b6SYOSHIFUJI Hideaki if (max_delay) 335708b202b6SYOSHIFUJI Hideaki group = &mld->mld_mca; 3358248ba8ecSLinus Lüssing } else { 3359856ce5d0SLinus Lüssing if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) { 336008b202b6SYOSHIFUJI Hideaki err = -EINVAL; 336108b202b6SYOSHIFUJI Hideaki goto out; 336208b202b6SYOSHIFUJI Hideaki } 336308b202b6SYOSHIFUJI Hideaki mld2q = (struct mld2_query *)icmp6_hdr(skb); 336408b202b6SYOSHIFUJI Hideaki if (!mld2q->mld2q_nsrcs) 336508b202b6SYOSHIFUJI Hideaki group = &mld2q->mld2q_mca; 3366d3d065c0SNikolay Aleksandrov if (brmctx->multicast_mld_version == 2 && 336723550b83SNikolay Aleksandrov !ipv6_addr_any(&mld2q->mld2q_mca) && 336823550b83SNikolay Aleksandrov mld2q->mld2q_suppress) 336923550b83SNikolay Aleksandrov goto out; 3370e3f5b170SDaniel Borkmann 3371e3f5b170SDaniel Borkmann max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL); 337208b202b6SYOSHIFUJI Hideaki } 337308b202b6SYOSHIFUJI Hideaki 33749ed973ccSLinus Lüssing is_general_query = group && ipv6_addr_any(group); 33759ed973ccSLinus Lüssing 3376dc4eb53aSLinus Lüssing if (is_general_query) { 3377dc4eb53aSLinus Lüssing saddr.proto = htons(ETH_P_IPV6); 3378deb96566SNikolay Aleksandrov saddr.src.ip6 = ipv6_hdr(skb)->saddr; 3379b00589afSLinus Lüssing 3380adc47037SNikolay Aleksandrov br_ip6_multicast_query_received(brmctx, pmctx, 3381d3d065c0SNikolay Aleksandrov &brmctx->ip6_other_query, 3382dc4eb53aSLinus Lüssing &saddr, max_delay); 338308b202b6SYOSHIFUJI Hideaki goto out; 33846c03ee8bSLinus Lüssing } else if (!group) { 33856c03ee8bSLinus Lüssing goto out; 3386dc4eb53aSLinus Lüssing } 338708b202b6SYOSHIFUJI Hideaki 3388adc47037SNikolay Aleksandrov mp = br_mdb_ip6_get(brmctx->br, group, vid); 338908b202b6SYOSHIFUJI Hideaki if (!mp) 339008b202b6SYOSHIFUJI Hideaki goto out; 339108b202b6SYOSHIFUJI Hideaki 3392d3d065c0SNikolay Aleksandrov max_delay *= brmctx->multicast_last_member_count; 3393ff0fd34eSAndrew Lunn if (mp->host_joined && 339408b202b6SYOSHIFUJI Hideaki (timer_pending(&mp->timer) ? 339508b202b6SYOSHIFUJI Hideaki time_after(mp->timer.expires, now + max_delay) : 339608b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&mp->timer) >= 0)) 339708b202b6SYOSHIFUJI Hideaki mod_timer(&mp->timer, now + max_delay); 339808b202b6SYOSHIFUJI Hideaki 3399e8051688SEric Dumazet for (pp = &mp->ports; 3400adc47037SNikolay Aleksandrov (p = mlock_dereference(*pp, brmctx->br)) != NULL; 3401e8051688SEric Dumazet pp = &p->next) { 340208b202b6SYOSHIFUJI Hideaki if (timer_pending(&p->timer) ? 340308b202b6SYOSHIFUJI Hideaki time_after(p->timer.expires, now + max_delay) : 340423550b83SNikolay Aleksandrov try_to_del_timer_sync(&p->timer) >= 0 && 3405d3d065c0SNikolay Aleksandrov (brmctx->multicast_mld_version == 1 || 340623550b83SNikolay Aleksandrov p->filter_mode == MCAST_EXCLUDE)) 340724f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 340808b202b6SYOSHIFUJI Hideaki } 340908b202b6SYOSHIFUJI Hideaki 341008b202b6SYOSHIFUJI Hideaki out: 3411adc47037SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 341208b202b6SYOSHIFUJI Hideaki return err; 341308b202b6SYOSHIFUJI Hideaki } 341408b202b6SYOSHIFUJI Hideaki #endif 341508b202b6SYOSHIFUJI Hideaki 341690010b36SLinus Lüssing static void 3417adc47037SNikolay Aleksandrov br_multicast_leave_group(struct net_bridge_mcast *brmctx, 3418adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 3419cc0fdd80SLinus Lüssing struct br_ip *group, 342090010b36SLinus Lüssing struct bridge_mcast_other_query *other_query, 34216db6f0eaSFelix Fietkau struct bridge_mcast_own_query *own_query, 34226db6f0eaSFelix Fietkau const unsigned char *src) 3423eb1d1641SHerbert Xu { 3424eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 3425eb1d1641SHerbert Xu struct net_bridge_port_group *p; 3426eb1d1641SHerbert Xu unsigned long now; 3427eb1d1641SHerbert Xu unsigned long time; 3428eb1d1641SHerbert Xu 3429adc47037SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 34304cdd0d10SNikolay Aleksandrov if (!br_multicast_ctx_should_use(brmctx, pmctx)) 3431eb1d1641SHerbert Xu goto out; 3432eb1d1641SHerbert Xu 3433adc47037SNikolay Aleksandrov mp = br_mdb_ip_get(brmctx->br, group); 3434eb1d1641SHerbert Xu if (!mp) 3435eb1d1641SHerbert Xu goto out; 3436eb1d1641SHerbert Xu 3437adc47037SNikolay Aleksandrov if (pmctx && (pmctx->port->flags & BR_MULTICAST_FAST_LEAVE)) { 3438544586f7SSatish Ashok struct net_bridge_port_group __rcu **pp; 3439544586f7SSatish Ashok 3440544586f7SSatish Ashok for (pp = &mp->ports; 3441adc47037SNikolay Aleksandrov (p = mlock_dereference(*pp, brmctx->br)) != NULL; 3442544586f7SSatish Ashok pp = &p->next) { 3443adc47037SNikolay Aleksandrov if (!br_port_group_equal(p, pmctx->port, src)) 3444544586f7SSatish Ashok continue; 3445544586f7SSatish Ashok 34465c725b6bSNikolay Aleksandrov if (p->flags & MDB_PG_FLAGS_PERMANENT) 34475c725b6bSNikolay Aleksandrov break; 34485c725b6bSNikolay Aleksandrov 3449681590bdSNikolay Aleksandrov p->flags |= MDB_PG_FLAGS_FAST_LEAVE; 3450681590bdSNikolay Aleksandrov br_multicast_del_pg(mp, p, pp); 3451544586f7SSatish Ashok } 3452544586f7SSatish Ashok goto out; 3453544586f7SSatish Ashok } 3454544586f7SSatish Ashok 3455544586f7SSatish Ashok if (timer_pending(&other_query->timer)) 3456544586f7SSatish Ashok goto out; 3457544586f7SSatish Ashok 34584d5b4e84SNikolay Aleksandrov if (brmctx->multicast_querier) { 3459adc47037SNikolay Aleksandrov __br_multicast_send_query(brmctx, pmctx, NULL, NULL, &mp->addr, 346042c11ccfSNikolay Aleksandrov false, 0, NULL); 34616b7df111SCong Wang 3462d3d065c0SNikolay Aleksandrov time = jiffies + brmctx->multicast_last_member_count * 3463d3d065c0SNikolay Aleksandrov brmctx->multicast_last_member_interval; 3464cc0fdd80SLinus Lüssing 346590010b36SLinus Lüssing mod_timer(&own_query->timer, time); 34666b7df111SCong Wang 3467adc47037SNikolay Aleksandrov for (p = mlock_dereference(mp->ports, brmctx->br); 346854cb4319SNikolay Aleksandrov p != NULL && pmctx != NULL; 3469adc47037SNikolay Aleksandrov p = mlock_dereference(p->next, brmctx->br)) { 3470adc47037SNikolay Aleksandrov if (!br_port_group_equal(p, pmctx->port, src)) 34716b7df111SCong Wang continue; 34726b7df111SCong Wang 34736b7df111SCong Wang if (!hlist_unhashed(&p->mglist) && 34746b7df111SCong Wang (timer_pending(&p->timer) ? 34756b7df111SCong Wang time_after(p->timer.expires, time) : 34766b7df111SCong Wang try_to_del_timer_sync(&p->timer) >= 0)) { 34776b7df111SCong Wang mod_timer(&p->timer, time); 34786b7df111SCong Wang } 34796b7df111SCong Wang 34806b7df111SCong Wang break; 34816b7df111SCong Wang } 34826b7df111SCong Wang } 34836b7df111SCong Wang 3484eb1d1641SHerbert Xu now = jiffies; 3485d3d065c0SNikolay Aleksandrov time = now + brmctx->multicast_last_member_count * 3486d3d065c0SNikolay Aleksandrov brmctx->multicast_last_member_interval; 3487eb1d1641SHerbert Xu 3488adc47037SNikolay Aleksandrov if (!pmctx) { 3489ff0fd34eSAndrew Lunn if (mp->host_joined && 3490eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 3491eb1d1641SHerbert Xu time_after(mp->timer.expires, time) : 3492eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) { 3493eb1d1641SHerbert Xu mod_timer(&mp->timer, time); 3494eb1d1641SHerbert Xu } 3495454594f3SLinus Lüssing 3496454594f3SLinus Lüssing goto out; 3497454594f3SLinus Lüssing } 3498454594f3SLinus Lüssing 3499adc47037SNikolay Aleksandrov for (p = mlock_dereference(mp->ports, brmctx->br); 3500454594f3SLinus Lüssing p != NULL; 3501adc47037SNikolay Aleksandrov p = mlock_dereference(p->next, brmctx->br)) { 3502adc47037SNikolay Aleksandrov if (p->key.port != pmctx->port) 3503454594f3SLinus Lüssing continue; 3504454594f3SLinus Lüssing 3505454594f3SLinus Lüssing if (!hlist_unhashed(&p->mglist) && 3506454594f3SLinus Lüssing (timer_pending(&p->timer) ? 3507454594f3SLinus Lüssing time_after(p->timer.expires, time) : 3508454594f3SLinus Lüssing try_to_del_timer_sync(&p->timer) >= 0)) { 3509454594f3SLinus Lüssing mod_timer(&p->timer, time); 3510454594f3SLinus Lüssing } 3511454594f3SLinus Lüssing 3512454594f3SLinus Lüssing break; 3513eb1d1641SHerbert Xu } 3514eb1d1641SHerbert Xu out: 3515adc47037SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 3516eb1d1641SHerbert Xu } 3517eb1d1641SHerbert Xu 3518adc47037SNikolay Aleksandrov static void br_ip4_multicast_leave_group(struct net_bridge_mcast *brmctx, 3519adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 3520b0e9a30dSVlad Yasevich __be32 group, 35216db6f0eaSFelix Fietkau __u16 vid, 35226db6f0eaSFelix Fietkau const unsigned char *src) 35238ef2a9a5SYOSHIFUJI Hideaki { 35248ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 352590010b36SLinus Lüssing struct bridge_mcast_own_query *own_query; 35268ef2a9a5SYOSHIFUJI Hideaki 35278ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 35288ef2a9a5SYOSHIFUJI Hideaki return; 35298ef2a9a5SYOSHIFUJI Hideaki 3530adc47037SNikolay Aleksandrov own_query = pmctx ? &pmctx->ip4_own_query : &brmctx->ip4_own_query; 353190010b36SLinus Lüssing 35321515a63fSNikolay Aleksandrov memset(&br_group, 0, sizeof(br_group)); 3533eab3227bSNikolay Aleksandrov br_group.dst.ip4 = group; 35348ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 3535b0e9a30dSVlad Yasevich br_group.vid = vid; 35368ef2a9a5SYOSHIFUJI Hideaki 3537adc47037SNikolay Aleksandrov br_multicast_leave_group(brmctx, pmctx, &br_group, 3538adc47037SNikolay Aleksandrov &brmctx->ip4_other_query, 35396db6f0eaSFelix Fietkau own_query, src); 35408ef2a9a5SYOSHIFUJI Hideaki } 35418ef2a9a5SYOSHIFUJI Hideaki 3542dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 3543adc47037SNikolay Aleksandrov static void br_ip6_multicast_leave_group(struct net_bridge_mcast *brmctx, 3544adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 3545b0e9a30dSVlad Yasevich const struct in6_addr *group, 35466db6f0eaSFelix Fietkau __u16 vid, 35476db6f0eaSFelix Fietkau const unsigned char *src) 354808b202b6SYOSHIFUJI Hideaki { 354908b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 355090010b36SLinus Lüssing struct bridge_mcast_own_query *own_query; 355108b202b6SYOSHIFUJI Hideaki 35523c3769e6SLinus Lüssing if (ipv6_addr_is_ll_all_nodes(group)) 355308b202b6SYOSHIFUJI Hideaki return; 355408b202b6SYOSHIFUJI Hideaki 3555adc47037SNikolay Aleksandrov own_query = pmctx ? &pmctx->ip6_own_query : &brmctx->ip6_own_query; 355690010b36SLinus Lüssing 35571515a63fSNikolay Aleksandrov memset(&br_group, 0, sizeof(br_group)); 3558eab3227bSNikolay Aleksandrov br_group.dst.ip6 = *group; 355908b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 3560b0e9a30dSVlad Yasevich br_group.vid = vid; 356108b202b6SYOSHIFUJI Hideaki 3562adc47037SNikolay Aleksandrov br_multicast_leave_group(brmctx, pmctx, &br_group, 3563adc47037SNikolay Aleksandrov &brmctx->ip6_other_query, 35646db6f0eaSFelix Fietkau own_query, src); 356508b202b6SYOSHIFUJI Hideaki } 356608b202b6SYOSHIFUJI Hideaki #endif 35678ef2a9a5SYOSHIFUJI Hideaki 35681080ab95SNikolay Aleksandrov static void br_multicast_err_count(const struct net_bridge *br, 35691080ab95SNikolay Aleksandrov const struct net_bridge_port *p, 35701080ab95SNikolay Aleksandrov __be16 proto) 35711080ab95SNikolay Aleksandrov { 35721080ab95SNikolay Aleksandrov struct bridge_mcast_stats __percpu *stats; 35731080ab95SNikolay Aleksandrov struct bridge_mcast_stats *pstats; 35741080ab95SNikolay Aleksandrov 3575675779adSNikolay Aleksandrov if (!br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)) 35761080ab95SNikolay Aleksandrov return; 35771080ab95SNikolay Aleksandrov 35781080ab95SNikolay Aleksandrov if (p) 35791080ab95SNikolay Aleksandrov stats = p->mcast_stats; 35801080ab95SNikolay Aleksandrov else 35811080ab95SNikolay Aleksandrov stats = br->mcast_stats; 35821080ab95SNikolay Aleksandrov if (WARN_ON(!stats)) 35831080ab95SNikolay Aleksandrov return; 35841080ab95SNikolay Aleksandrov 35851080ab95SNikolay Aleksandrov pstats = this_cpu_ptr(stats); 35861080ab95SNikolay Aleksandrov 35871080ab95SNikolay Aleksandrov u64_stats_update_begin(&pstats->syncp); 35881080ab95SNikolay Aleksandrov switch (proto) { 35891080ab95SNikolay Aleksandrov case htons(ETH_P_IP): 35901080ab95SNikolay Aleksandrov pstats->mstats.igmp_parse_errors++; 35911080ab95SNikolay Aleksandrov break; 35921080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 35931080ab95SNikolay Aleksandrov case htons(ETH_P_IPV6): 35941080ab95SNikolay Aleksandrov pstats->mstats.mld_parse_errors++; 35951080ab95SNikolay Aleksandrov break; 35961080ab95SNikolay Aleksandrov #endif 35971080ab95SNikolay Aleksandrov } 35981080ab95SNikolay Aleksandrov u64_stats_update_end(&pstats->syncp); 35991080ab95SNikolay Aleksandrov } 36001080ab95SNikolay Aleksandrov 3601adc47037SNikolay Aleksandrov static void br_multicast_pim(struct net_bridge_mcast *brmctx, 3602adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 360391b02d3dSNikolay Aleksandrov const struct sk_buff *skb) 360491b02d3dSNikolay Aleksandrov { 360591b02d3dSNikolay Aleksandrov unsigned int offset = skb_transport_offset(skb); 360691b02d3dSNikolay Aleksandrov struct pimhdr *pimhdr, _pimhdr; 360791b02d3dSNikolay Aleksandrov 360891b02d3dSNikolay Aleksandrov pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr); 360991b02d3dSNikolay Aleksandrov if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION || 361091b02d3dSNikolay Aleksandrov pim_hdr_type(pimhdr) != PIM_TYPE_HELLO) 361191b02d3dSNikolay Aleksandrov return; 361291b02d3dSNikolay Aleksandrov 3613adc47037SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 3614adc47037SNikolay Aleksandrov br_ip4_multicast_mark_router(brmctx, pmctx); 3615adc47037SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 361691b02d3dSNikolay Aleksandrov } 361791b02d3dSNikolay Aleksandrov 3618adc47037SNikolay Aleksandrov static int br_ip4_multicast_mrd_rcv(struct net_bridge_mcast *brmctx, 3619adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 36204b3087c7SLinus Lüssing struct sk_buff *skb) 36214b3087c7SLinus Lüssing { 36224b3087c7SLinus Lüssing if (ip_hdr(skb)->protocol != IPPROTO_IGMP || 36234b3087c7SLinus Lüssing igmp_hdr(skb)->type != IGMP_MRDISC_ADV) 36244b3087c7SLinus Lüssing return -ENOMSG; 36254b3087c7SLinus Lüssing 3626adc47037SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 3627adc47037SNikolay Aleksandrov br_ip4_multicast_mark_router(brmctx, pmctx); 3628adc47037SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 36294b3087c7SLinus Lüssing 36304b3087c7SLinus Lüssing return 0; 36314b3087c7SLinus Lüssing } 36324b3087c7SLinus Lüssing 3633adc47037SNikolay Aleksandrov static int br_multicast_ipv4_rcv(struct net_bridge_mcast *brmctx, 3634adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 363506499098SVlad Yasevich struct sk_buff *skb, 363606499098SVlad Yasevich u16 vid) 3637eb1d1641SHerbert Xu { 3638adc47037SNikolay Aleksandrov struct net_bridge_port *p = pmctx ? pmctx->port : NULL; 36396db6f0eaSFelix Fietkau const unsigned char *src; 3640eb1d1641SHerbert Xu struct igmphdr *ih; 3641eb1d1641SHerbert Xu int err; 3642eb1d1641SHerbert Xu 3643ba5ea614SLinus Lüssing err = ip_mc_check_igmp(skb); 3644eb1d1641SHerbert Xu 36459afd85c9SLinus Lüssing if (err == -ENOMSG) { 364691b02d3dSNikolay Aleksandrov if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) { 3647bd4265feSHerbert Xu BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 364891b02d3dSNikolay Aleksandrov } else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) { 364991b02d3dSNikolay Aleksandrov if (ip_hdr(skb)->protocol == IPPROTO_PIM) 3650adc47037SNikolay Aleksandrov br_multicast_pim(brmctx, pmctx, skb); 36514b3087c7SLinus Lüssing } else if (ipv4_is_all_snoopers(ip_hdr(skb)->daddr)) { 3652adc47037SNikolay Aleksandrov br_ip4_multicast_mrd_rcv(brmctx, pmctx, skb); 36534b3087c7SLinus Lüssing } 36544b3087c7SLinus Lüssing 3655eb1d1641SHerbert Xu return 0; 36569afd85c9SLinus Lüssing } else if (err < 0) { 3657adc47037SNikolay Aleksandrov br_multicast_err_count(brmctx->br, p, skb->protocol); 36589afd85c9SLinus Lüssing return err; 3659bd4265feSHerbert Xu } 3660eb1d1641SHerbert Xu 36619afd85c9SLinus Lüssing ih = igmp_hdr(skb); 36626db6f0eaSFelix Fietkau src = eth_hdr(skb)->h_source; 36631080ab95SNikolay Aleksandrov BR_INPUT_SKB_CB(skb)->igmp = ih->type; 3664eb1d1641SHerbert Xu 3665eb1d1641SHerbert Xu switch (ih->type) { 3666eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_REPORT: 3667eb1d1641SHerbert Xu case IGMPV2_HOST_MEMBERSHIP_REPORT: 366862b2bcb4SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 3669adc47037SNikolay Aleksandrov err = br_ip4_multicast_add_group(brmctx, pmctx, ih->group, vid, 3670adc47037SNikolay Aleksandrov src, true); 3671eb1d1641SHerbert Xu break; 3672eb1d1641SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 3673adc47037SNikolay Aleksandrov err = br_ip4_multicast_igmp3_report(brmctx, pmctx, skb, vid); 3674eb1d1641SHerbert Xu break; 3675eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_QUERY: 3676adc47037SNikolay Aleksandrov br_ip4_multicast_query(brmctx, pmctx, skb, vid); 3677eb1d1641SHerbert Xu break; 3678eb1d1641SHerbert Xu case IGMP_HOST_LEAVE_MESSAGE: 3679adc47037SNikolay Aleksandrov br_ip4_multicast_leave_group(brmctx, pmctx, ih->group, vid, src); 3680eb1d1641SHerbert Xu break; 3681eb1d1641SHerbert Xu } 3682eb1d1641SHerbert Xu 3683adc47037SNikolay Aleksandrov br_multicast_count(brmctx->br, p, skb, BR_INPUT_SKB_CB(skb)->igmp, 36841080ab95SNikolay Aleksandrov BR_MCAST_DIR_RX); 36851080ab95SNikolay Aleksandrov 3686eb1d1641SHerbert Xu return err; 3687eb1d1641SHerbert Xu } 3688eb1d1641SHerbert Xu 3689dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 3690adc47037SNikolay Aleksandrov static void br_ip6_multicast_mrd_rcv(struct net_bridge_mcast *brmctx, 3691adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 36924b3087c7SLinus Lüssing struct sk_buff *skb) 36934b3087c7SLinus Lüssing { 36944b3087c7SLinus Lüssing if (icmp6_hdr(skb)->icmp6_type != ICMPV6_MRDISC_ADV) 369599014088SLinus Lüssing return; 36964b3087c7SLinus Lüssing 3697adc47037SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 3698adc47037SNikolay Aleksandrov br_ip6_multicast_mark_router(brmctx, pmctx); 3699adc47037SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 37004b3087c7SLinus Lüssing } 37014b3087c7SLinus Lüssing 3702adc47037SNikolay Aleksandrov static int br_multicast_ipv6_rcv(struct net_bridge_mcast *brmctx, 3703adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 370406499098SVlad Yasevich struct sk_buff *skb, 370506499098SVlad Yasevich u16 vid) 370608b202b6SYOSHIFUJI Hideaki { 3707adc47037SNikolay Aleksandrov struct net_bridge_port *p = pmctx ? pmctx->port : NULL; 37086db6f0eaSFelix Fietkau const unsigned char *src; 37099afd85c9SLinus Lüssing struct mld_msg *mld; 371008b202b6SYOSHIFUJI Hideaki int err; 371108b202b6SYOSHIFUJI Hideaki 3712ba5ea614SLinus Lüssing err = ipv6_mc_check_mld(skb); 371308b202b6SYOSHIFUJI Hideaki 371499014088SLinus Lüssing if (err == -ENOMSG || err == -ENODATA) { 37159afd85c9SLinus Lüssing if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr)) 37168fad9c39SLinus Lüssing BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 371799014088SLinus Lüssing if (err == -ENODATA && 371899014088SLinus Lüssing ipv6_addr_is_all_snoopers(&ipv6_hdr(skb)->daddr)) 3719adc47037SNikolay Aleksandrov br_ip6_multicast_mrd_rcv(brmctx, pmctx, skb); 37204b3087c7SLinus Lüssing 372108b202b6SYOSHIFUJI Hideaki return 0; 37229afd85c9SLinus Lüssing } else if (err < 0) { 3723adc47037SNikolay Aleksandrov br_multicast_err_count(brmctx->br, p, skb->protocol); 37249afd85c9SLinus Lüssing return err; 372508b202b6SYOSHIFUJI Hideaki } 372608b202b6SYOSHIFUJI Hideaki 37279afd85c9SLinus Lüssing mld = (struct mld_msg *)skb_transport_header(skb); 37281080ab95SNikolay Aleksandrov BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type; 372908b202b6SYOSHIFUJI Hideaki 37309afd85c9SLinus Lüssing switch (mld->mld_type) { 373108b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 37326db6f0eaSFelix Fietkau src = eth_hdr(skb)->h_source; 3733fc2af6c7SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 3734adc47037SNikolay Aleksandrov err = br_ip6_multicast_add_group(brmctx, pmctx, &mld->mld_mca, 3735adc47037SNikolay Aleksandrov vid, src, true); 373608b202b6SYOSHIFUJI Hideaki break; 373708b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 3738adc47037SNikolay Aleksandrov err = br_ip6_multicast_mld2_report(brmctx, pmctx, skb, vid); 373908b202b6SYOSHIFUJI Hideaki break; 374008b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 3741adc47037SNikolay Aleksandrov err = br_ip6_multicast_query(brmctx, pmctx, skb, vid); 374208b202b6SYOSHIFUJI Hideaki break; 374308b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 37446db6f0eaSFelix Fietkau src = eth_hdr(skb)->h_source; 3745adc47037SNikolay Aleksandrov br_ip6_multicast_leave_group(brmctx, pmctx, &mld->mld_mca, vid, 3746adc47037SNikolay Aleksandrov src); 37479afd85c9SLinus Lüssing break; 374808b202b6SYOSHIFUJI Hideaki } 374908b202b6SYOSHIFUJI Hideaki 3750adc47037SNikolay Aleksandrov br_multicast_count(brmctx->br, p, skb, BR_INPUT_SKB_CB(skb)->igmp, 37511080ab95SNikolay Aleksandrov BR_MCAST_DIR_RX); 37521080ab95SNikolay Aleksandrov 375308b202b6SYOSHIFUJI Hideaki return err; 375408b202b6SYOSHIFUJI Hideaki } 375508b202b6SYOSHIFUJI Hideaki #endif 375608b202b6SYOSHIFUJI Hideaki 3757f4b7002aSNikolay Aleksandrov int br_multicast_rcv(struct net_bridge_mcast **brmctx, 3758f4b7002aSNikolay Aleksandrov struct net_bridge_mcast_port **pmctx, 3759f4b7002aSNikolay Aleksandrov struct net_bridge_vlan *vlan, 376006499098SVlad Yasevich struct sk_buff *skb, u16 vid) 3761eb1d1641SHerbert Xu { 37621080ab95SNikolay Aleksandrov int ret = 0; 37631080ab95SNikolay Aleksandrov 37641fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->igmp = 0; 37651fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->mrouters_only = 0; 37661fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 3767f4b7002aSNikolay Aleksandrov if (!br_opt_get((*brmctx)->br, BROPT_MULTICAST_ENABLED)) 3768eb1d1641SHerbert Xu return 0; 3769eb1d1641SHerbert Xu 3770f4b7002aSNikolay Aleksandrov if (br_opt_get((*brmctx)->br, BROPT_MCAST_VLAN_SNOOPING_ENABLED) && vlan) { 3771f4b7002aSNikolay Aleksandrov const struct net_bridge_vlan *masterv; 3772f4b7002aSNikolay Aleksandrov 3773f4b7002aSNikolay Aleksandrov /* the vlan has the master flag set only when transmitting 3774f4b7002aSNikolay Aleksandrov * through the bridge device 3775f4b7002aSNikolay Aleksandrov */ 3776f4b7002aSNikolay Aleksandrov if (br_vlan_is_master(vlan)) { 3777f4b7002aSNikolay Aleksandrov masterv = vlan; 3778f4b7002aSNikolay Aleksandrov *brmctx = &vlan->br_mcast_ctx; 3779f4b7002aSNikolay Aleksandrov *pmctx = NULL; 3780f4b7002aSNikolay Aleksandrov } else { 3781f4b7002aSNikolay Aleksandrov masterv = vlan->brvlan; 3782f4b7002aSNikolay Aleksandrov *brmctx = &vlan->brvlan->br_mcast_ctx; 3783f4b7002aSNikolay Aleksandrov *pmctx = &vlan->port_mcast_ctx; 3784f4b7002aSNikolay Aleksandrov } 3785f4b7002aSNikolay Aleksandrov 3786f4b7002aSNikolay Aleksandrov if (!(masterv->priv_flags & BR_VLFLAG_GLOBAL_MCAST_ENABLED)) 3787f4b7002aSNikolay Aleksandrov return 0; 3788f4b7002aSNikolay Aleksandrov } 3789f4b7002aSNikolay Aleksandrov 3790eb1d1641SHerbert Xu switch (skb->protocol) { 3791eb1d1641SHerbert Xu case htons(ETH_P_IP): 3792f4b7002aSNikolay Aleksandrov ret = br_multicast_ipv4_rcv(*brmctx, *pmctx, skb, vid); 37931080ab95SNikolay Aleksandrov break; 3794dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 379508b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 3796f4b7002aSNikolay Aleksandrov ret = br_multicast_ipv6_rcv(*brmctx, *pmctx, skb, vid); 37971080ab95SNikolay Aleksandrov break; 379808b202b6SYOSHIFUJI Hideaki #endif 3799eb1d1641SHerbert Xu } 3800eb1d1641SHerbert Xu 38011080ab95SNikolay Aleksandrov return ret; 3802eb1d1641SHerbert Xu } 3803eb1d1641SHerbert Xu 3804adc47037SNikolay Aleksandrov static void br_multicast_query_expired(struct net_bridge_mcast *brmctx, 38052cd41431SLinus Lüssing struct bridge_mcast_own_query *query, 38062cd41431SLinus Lüssing struct bridge_mcast_querier *querier) 3807cc0fdd80SLinus Lüssing { 3808adc47037SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 38094cdd0d10SNikolay Aleksandrov if (br_multicast_ctx_vlan_disabled(brmctx)) 38104cdd0d10SNikolay Aleksandrov goto out; 38114cdd0d10SNikolay Aleksandrov 3812adc47037SNikolay Aleksandrov if (query->startup_sent < brmctx->multicast_startup_query_count) 3813cc0fdd80SLinus Lüssing query->startup_sent++; 3814cc0fdd80SLinus Lüssing 3815adc47037SNikolay Aleksandrov br_multicast_send_query(brmctx, NULL, query); 38164cdd0d10SNikolay Aleksandrov out: 3817adc47037SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 3818cc0fdd80SLinus Lüssing } 3819cc0fdd80SLinus Lüssing 382088c1f37fSAllen Pais static void br_ip4_multicast_query_expired(struct timer_list *t) 3821eb1d1641SHerbert Xu { 3822d3d065c0SNikolay Aleksandrov struct net_bridge_mcast *brmctx = from_timer(brmctx, t, 3823d3d065c0SNikolay Aleksandrov ip4_own_query.timer); 3824eb1d1641SHerbert Xu 3825adc47037SNikolay Aleksandrov br_multicast_query_expired(brmctx, &brmctx->ip4_own_query, 3826d3d065c0SNikolay Aleksandrov &brmctx->ip4_querier); 3827eb1d1641SHerbert Xu } 3828eb1d1641SHerbert Xu 3829cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 383088c1f37fSAllen Pais static void br_ip6_multicast_query_expired(struct timer_list *t) 3831cc0fdd80SLinus Lüssing { 3832d3d065c0SNikolay Aleksandrov struct net_bridge_mcast *brmctx = from_timer(brmctx, t, 3833d3d065c0SNikolay Aleksandrov ip6_own_query.timer); 3834cc0fdd80SLinus Lüssing 3835adc47037SNikolay Aleksandrov br_multicast_query_expired(brmctx, &brmctx->ip6_own_query, 3836d3d065c0SNikolay Aleksandrov &brmctx->ip6_querier); 3837cc0fdd80SLinus Lüssing } 3838cc0fdd80SLinus Lüssing #endif 3839cc0fdd80SLinus Lüssing 3840e12cec65SNikolay Aleksandrov static void br_multicast_gc_work(struct work_struct *work) 38418b671779SNikolay Aleksandrov { 38428b671779SNikolay Aleksandrov struct net_bridge *br = container_of(work, struct net_bridge, 3843e12cec65SNikolay Aleksandrov mcast_gc_work); 38448b671779SNikolay Aleksandrov HLIST_HEAD(deleted_head); 38458b671779SNikolay Aleksandrov 38468b671779SNikolay Aleksandrov spin_lock_bh(&br->multicast_lock); 3847e12cec65SNikolay Aleksandrov hlist_move_list(&br->mcast_gc_list, &deleted_head); 38488b671779SNikolay Aleksandrov spin_unlock_bh(&br->multicast_lock); 38498b671779SNikolay Aleksandrov 3850e12cec65SNikolay Aleksandrov br_multicast_gc(&deleted_head); 38518b671779SNikolay Aleksandrov } 38528b671779SNikolay Aleksandrov 3853613d61dbSNikolay Aleksandrov void br_multicast_ctx_init(struct net_bridge *br, 3854613d61dbSNikolay Aleksandrov struct net_bridge_vlan *vlan, 3855613d61dbSNikolay Aleksandrov struct net_bridge_mcast *brmctx) 3856613d61dbSNikolay Aleksandrov { 3857613d61dbSNikolay Aleksandrov brmctx->br = br; 3858613d61dbSNikolay Aleksandrov brmctx->vlan = vlan; 3859613d61dbSNikolay Aleksandrov brmctx->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 3860613d61dbSNikolay Aleksandrov brmctx->multicast_last_member_count = 2; 3861613d61dbSNikolay Aleksandrov brmctx->multicast_startup_query_count = 2; 3862613d61dbSNikolay Aleksandrov 3863613d61dbSNikolay Aleksandrov brmctx->multicast_last_member_interval = HZ; 3864613d61dbSNikolay Aleksandrov brmctx->multicast_query_response_interval = 10 * HZ; 3865613d61dbSNikolay Aleksandrov brmctx->multicast_startup_query_interval = 125 * HZ / 4; 3866613d61dbSNikolay Aleksandrov brmctx->multicast_query_interval = 125 * HZ; 3867613d61dbSNikolay Aleksandrov brmctx->multicast_querier_interval = 255 * HZ; 3868613d61dbSNikolay Aleksandrov brmctx->multicast_membership_interval = 260 * HZ; 3869613d61dbSNikolay Aleksandrov 3870613d61dbSNikolay Aleksandrov brmctx->ip4_other_query.delay_time = 0; 3871bb18ef8eSNikolay Aleksandrov brmctx->ip4_querier.port_ifidx = 0; 3872f936bb42SThomas Gleixner seqcount_spinlock_init(&brmctx->ip4_querier.seq, &br->multicast_lock); 3873613d61dbSNikolay Aleksandrov brmctx->multicast_igmp_version = 2; 3874613d61dbSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 3875613d61dbSNikolay Aleksandrov brmctx->multicast_mld_version = 1; 3876613d61dbSNikolay Aleksandrov brmctx->ip6_other_query.delay_time = 0; 3877bb18ef8eSNikolay Aleksandrov brmctx->ip6_querier.port_ifidx = 0; 3878f936bb42SThomas Gleixner seqcount_spinlock_init(&brmctx->ip6_querier.seq, &br->multicast_lock); 3879613d61dbSNikolay Aleksandrov #endif 3880613d61dbSNikolay Aleksandrov 3881613d61dbSNikolay Aleksandrov timer_setup(&brmctx->ip4_mc_router_timer, 3882613d61dbSNikolay Aleksandrov br_ip4_multicast_local_router_expired, 0); 3883613d61dbSNikolay Aleksandrov timer_setup(&brmctx->ip4_other_query.timer, 3884613d61dbSNikolay Aleksandrov br_ip4_multicast_querier_expired, 0); 3885613d61dbSNikolay Aleksandrov timer_setup(&brmctx->ip4_own_query.timer, 3886613d61dbSNikolay Aleksandrov br_ip4_multicast_query_expired, 0); 3887613d61dbSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 3888613d61dbSNikolay Aleksandrov timer_setup(&brmctx->ip6_mc_router_timer, 3889613d61dbSNikolay Aleksandrov br_ip6_multicast_local_router_expired, 0); 3890613d61dbSNikolay Aleksandrov timer_setup(&brmctx->ip6_other_query.timer, 3891613d61dbSNikolay Aleksandrov br_ip6_multicast_querier_expired, 0); 3892613d61dbSNikolay Aleksandrov timer_setup(&brmctx->ip6_own_query.timer, 3893613d61dbSNikolay Aleksandrov br_ip6_multicast_query_expired, 0); 3894613d61dbSNikolay Aleksandrov #endif 3895613d61dbSNikolay Aleksandrov } 3896613d61dbSNikolay Aleksandrov 3897613d61dbSNikolay Aleksandrov void br_multicast_ctx_deinit(struct net_bridge_mcast *brmctx) 3898613d61dbSNikolay Aleksandrov { 3899613d61dbSNikolay Aleksandrov __br_multicast_stop(brmctx); 3900613d61dbSNikolay Aleksandrov } 3901613d61dbSNikolay Aleksandrov 3902eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br) 3903eb1d1641SHerbert Xu { 3904d08c6bc0SNikolay Aleksandrov br->hash_max = BR_MULTICAST_DEFAULT_HASH_MAX; 3905eb1d1641SHerbert Xu 3906613d61dbSNikolay Aleksandrov br_multicast_ctx_init(br, NULL, &br->multicast_ctx); 3907eb1d1641SHerbert Xu 39086919622aSIdo Schimmel br_opt_toggle(br, BROPT_MULTICAST_ENABLED, true); 3909675779adSNikolay Aleksandrov br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true); 3910b00589afSLinus Lüssing 3911eb1d1641SHerbert Xu spin_lock_init(&br->multicast_lock); 391219e3a9c9SNikolay Aleksandrov INIT_HLIST_HEAD(&br->mdb_list); 3913e12cec65SNikolay Aleksandrov INIT_HLIST_HEAD(&br->mcast_gc_list); 3914e12cec65SNikolay Aleksandrov INIT_WORK(&br->mcast_gc_work, br_multicast_gc_work); 3915eb1d1641SHerbert Xu } 3916eb1d1641SHerbert Xu 39174effd28cSLinus Lüssing static void br_ip4_multicast_join_snoopers(struct net_bridge *br) 39184effd28cSLinus Lüssing { 39194effd28cSLinus Lüssing struct in_device *in_dev = in_dev_get(br->dev); 39204effd28cSLinus Lüssing 39214effd28cSLinus Lüssing if (!in_dev) 39224effd28cSLinus Lüssing return; 39234effd28cSLinus Lüssing 39249fb20801SFlorian Fainelli __ip_mc_inc_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC); 39254effd28cSLinus Lüssing in_dev_put(in_dev); 39264effd28cSLinus Lüssing } 39274effd28cSLinus Lüssing 39284effd28cSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 39294effd28cSLinus Lüssing static void br_ip6_multicast_join_snoopers(struct net_bridge *br) 39304effd28cSLinus Lüssing { 39314effd28cSLinus Lüssing struct in6_addr addr; 39324effd28cSLinus Lüssing 39334effd28cSLinus Lüssing ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a)); 39344effd28cSLinus Lüssing ipv6_dev_mc_inc(br->dev, &addr); 39354effd28cSLinus Lüssing } 39364effd28cSLinus Lüssing #else 39374effd28cSLinus Lüssing static inline void br_ip6_multicast_join_snoopers(struct net_bridge *br) 39384effd28cSLinus Lüssing { 39394effd28cSLinus Lüssing } 39404effd28cSLinus Lüssing #endif 39414effd28cSLinus Lüssing 3942851d0a73SJoseph Huang void br_multicast_join_snoopers(struct net_bridge *br) 39434effd28cSLinus Lüssing { 39444effd28cSLinus Lüssing br_ip4_multicast_join_snoopers(br); 39454effd28cSLinus Lüssing br_ip6_multicast_join_snoopers(br); 39464effd28cSLinus Lüssing } 39474effd28cSLinus Lüssing 39484effd28cSLinus Lüssing static void br_ip4_multicast_leave_snoopers(struct net_bridge *br) 39494effd28cSLinus Lüssing { 39504effd28cSLinus Lüssing struct in_device *in_dev = in_dev_get(br->dev); 39514effd28cSLinus Lüssing 39524effd28cSLinus Lüssing if (WARN_ON(!in_dev)) 39534effd28cSLinus Lüssing return; 39544effd28cSLinus Lüssing 39559fb20801SFlorian Fainelli __ip_mc_dec_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC); 39564effd28cSLinus Lüssing in_dev_put(in_dev); 39574effd28cSLinus Lüssing } 39584effd28cSLinus Lüssing 39594effd28cSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 39604effd28cSLinus Lüssing static void br_ip6_multicast_leave_snoopers(struct net_bridge *br) 39614effd28cSLinus Lüssing { 39624effd28cSLinus Lüssing struct in6_addr addr; 39634effd28cSLinus Lüssing 39644effd28cSLinus Lüssing ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a)); 39654effd28cSLinus Lüssing ipv6_dev_mc_dec(br->dev, &addr); 39664effd28cSLinus Lüssing } 39674effd28cSLinus Lüssing #else 39684effd28cSLinus Lüssing static inline void br_ip6_multicast_leave_snoopers(struct net_bridge *br) 39694effd28cSLinus Lüssing { 39704effd28cSLinus Lüssing } 39714effd28cSLinus Lüssing #endif 39724effd28cSLinus Lüssing 3973851d0a73SJoseph Huang void br_multicast_leave_snoopers(struct net_bridge *br) 39744effd28cSLinus Lüssing { 39754effd28cSLinus Lüssing br_ip4_multicast_leave_snoopers(br); 39764effd28cSLinus Lüssing br_ip6_multicast_leave_snoopers(br); 39774effd28cSLinus Lüssing } 39784effd28cSLinus Lüssing 39797b54aaafSNikolay Aleksandrov static void __br_multicast_open_query(struct net_bridge *br, 398090010b36SLinus Lüssing struct bridge_mcast_own_query *query) 3981eb1d1641SHerbert Xu { 3982cc0fdd80SLinus Lüssing query->startup_sent = 0; 3983eb1d1641SHerbert Xu 398413cefad2SNikolay Aleksandrov if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) 3985eb1d1641SHerbert Xu return; 3986eb1d1641SHerbert Xu 3987cc0fdd80SLinus Lüssing mod_timer(&query->timer, jiffies); 3988cc0fdd80SLinus Lüssing } 3989cc0fdd80SLinus Lüssing 39907b54aaafSNikolay Aleksandrov static void __br_multicast_open(struct net_bridge_mcast *brmctx) 39917b54aaafSNikolay Aleksandrov { 39927b54aaafSNikolay Aleksandrov __br_multicast_open_query(brmctx->br, &brmctx->ip4_own_query); 39937b54aaafSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 39947b54aaafSNikolay Aleksandrov __br_multicast_open_query(brmctx->br, &brmctx->ip6_own_query); 39957b54aaafSNikolay Aleksandrov #endif 39967b54aaafSNikolay Aleksandrov } 39977b54aaafSNikolay Aleksandrov 3998cc0fdd80SLinus Lüssing void br_multicast_open(struct net_bridge *br) 3999cc0fdd80SLinus Lüssing { 4000f4b7002aSNikolay Aleksandrov ASSERT_RTNL(); 4001f4b7002aSNikolay Aleksandrov 4002f4b7002aSNikolay Aleksandrov if (br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED)) { 40037b54aaafSNikolay Aleksandrov struct net_bridge_vlan_group *vg; 40047b54aaafSNikolay Aleksandrov struct net_bridge_vlan *vlan; 40057b54aaafSNikolay Aleksandrov 40067b54aaafSNikolay Aleksandrov vg = br_vlan_group(br); 40077b54aaafSNikolay Aleksandrov if (vg) { 40087b54aaafSNikolay Aleksandrov list_for_each_entry(vlan, &vg->vlan_list, vlist) { 40097b54aaafSNikolay Aleksandrov struct net_bridge_mcast *brmctx; 40107b54aaafSNikolay Aleksandrov 40117b54aaafSNikolay Aleksandrov brmctx = &vlan->br_mcast_ctx; 40127b54aaafSNikolay Aleksandrov if (br_vlan_is_brentry(vlan) && 40137b54aaafSNikolay Aleksandrov !br_multicast_ctx_vlan_disabled(brmctx)) 40147b54aaafSNikolay Aleksandrov __br_multicast_open(&vlan->br_mcast_ctx); 40157b54aaafSNikolay Aleksandrov } 40167b54aaafSNikolay Aleksandrov } 4017cb486ce9SNikolay Aleksandrov } else { 40187b54aaafSNikolay Aleksandrov __br_multicast_open(&br->multicast_ctx); 4019eb1d1641SHerbert Xu } 4020cb486ce9SNikolay Aleksandrov } 4021eb1d1641SHerbert Xu 4022613d61dbSNikolay Aleksandrov static void __br_multicast_stop(struct net_bridge_mcast *brmctx) 4023613d61dbSNikolay Aleksandrov { 4024613d61dbSNikolay Aleksandrov del_timer_sync(&brmctx->ip4_mc_router_timer); 4025613d61dbSNikolay Aleksandrov del_timer_sync(&brmctx->ip4_other_query.timer); 4026613d61dbSNikolay Aleksandrov del_timer_sync(&brmctx->ip4_own_query.timer); 4027613d61dbSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 4028613d61dbSNikolay Aleksandrov del_timer_sync(&brmctx->ip6_mc_router_timer); 4029613d61dbSNikolay Aleksandrov del_timer_sync(&brmctx->ip6_other_query.timer); 4030613d61dbSNikolay Aleksandrov del_timer_sync(&brmctx->ip6_own_query.timer); 4031613d61dbSNikolay Aleksandrov #endif 4032613d61dbSNikolay Aleksandrov } 4033613d61dbSNikolay Aleksandrov 40347b54aaafSNikolay Aleksandrov void br_multicast_toggle_one_vlan(struct net_bridge_vlan *vlan, bool on) 40357b54aaafSNikolay Aleksandrov { 40367b54aaafSNikolay Aleksandrov struct net_bridge *br; 40377b54aaafSNikolay Aleksandrov 40387b54aaafSNikolay Aleksandrov /* it's okay to check for the flag without the multicast lock because it 40397b54aaafSNikolay Aleksandrov * can only change under RTNL -> multicast_lock, we need the latter to 40407b54aaafSNikolay Aleksandrov * sync with timers and packets 40417b54aaafSNikolay Aleksandrov */ 40427b54aaafSNikolay Aleksandrov if (on == !!(vlan->priv_flags & BR_VLFLAG_MCAST_ENABLED)) 40437b54aaafSNikolay Aleksandrov return; 40447b54aaafSNikolay Aleksandrov 40457b54aaafSNikolay Aleksandrov if (br_vlan_is_master(vlan)) { 40467b54aaafSNikolay Aleksandrov br = vlan->br; 40477b54aaafSNikolay Aleksandrov 40487b54aaafSNikolay Aleksandrov if (!br_vlan_is_brentry(vlan) || 40497b54aaafSNikolay Aleksandrov (on && 40507b54aaafSNikolay Aleksandrov br_multicast_ctx_vlan_global_disabled(&vlan->br_mcast_ctx))) 40517b54aaafSNikolay Aleksandrov return; 40527b54aaafSNikolay Aleksandrov 40537b54aaafSNikolay Aleksandrov spin_lock_bh(&br->multicast_lock); 40547b54aaafSNikolay Aleksandrov vlan->priv_flags ^= BR_VLFLAG_MCAST_ENABLED; 40557b54aaafSNikolay Aleksandrov spin_unlock_bh(&br->multicast_lock); 40567b54aaafSNikolay Aleksandrov 40577b54aaafSNikolay Aleksandrov if (on) 40587b54aaafSNikolay Aleksandrov __br_multicast_open(&vlan->br_mcast_ctx); 40597b54aaafSNikolay Aleksandrov else 40607b54aaafSNikolay Aleksandrov __br_multicast_stop(&vlan->br_mcast_ctx); 40617b54aaafSNikolay Aleksandrov } else { 40627b54aaafSNikolay Aleksandrov struct net_bridge_mcast *brmctx; 40637b54aaafSNikolay Aleksandrov 40647b54aaafSNikolay Aleksandrov brmctx = br_multicast_port_ctx_get_global(&vlan->port_mcast_ctx); 40657b54aaafSNikolay Aleksandrov if (on && br_multicast_ctx_vlan_global_disabled(brmctx)) 40667b54aaafSNikolay Aleksandrov return; 40677b54aaafSNikolay Aleksandrov 40687b54aaafSNikolay Aleksandrov br = vlan->port->br; 40697b54aaafSNikolay Aleksandrov spin_lock_bh(&br->multicast_lock); 40707b54aaafSNikolay Aleksandrov vlan->priv_flags ^= BR_VLFLAG_MCAST_ENABLED; 40717b54aaafSNikolay Aleksandrov if (on) 40727b54aaafSNikolay Aleksandrov __br_multicast_enable_port_ctx(&vlan->port_mcast_ctx); 40737b54aaafSNikolay Aleksandrov else 40747b54aaafSNikolay Aleksandrov __br_multicast_disable_port_ctx(&vlan->port_mcast_ctx); 40757b54aaafSNikolay Aleksandrov spin_unlock_bh(&br->multicast_lock); 40767b54aaafSNikolay Aleksandrov } 40777b54aaafSNikolay Aleksandrov } 40787b54aaafSNikolay Aleksandrov 4079affce9a7SNikolay Aleksandrov static void br_multicast_toggle_vlan(struct net_bridge_vlan *vlan, bool on) 4080f4b7002aSNikolay Aleksandrov { 4081f4b7002aSNikolay Aleksandrov struct net_bridge_port *p; 4082f4b7002aSNikolay Aleksandrov 4083f4b7002aSNikolay Aleksandrov if (WARN_ON_ONCE(!br_vlan_is_master(vlan))) 4084f4b7002aSNikolay Aleksandrov return; 4085f4b7002aSNikolay Aleksandrov 4086f4b7002aSNikolay Aleksandrov list_for_each_entry(p, &vlan->br->port_list, list) { 4087f4b7002aSNikolay Aleksandrov struct net_bridge_vlan *vport; 4088f4b7002aSNikolay Aleksandrov 4089f4b7002aSNikolay Aleksandrov vport = br_vlan_find(nbp_vlan_group(p), vlan->vid); 4090f4b7002aSNikolay Aleksandrov if (!vport) 4091f4b7002aSNikolay Aleksandrov continue; 4092f4b7002aSNikolay Aleksandrov br_multicast_toggle_one_vlan(vport, on); 4093f4b7002aSNikolay Aleksandrov } 4094affce9a7SNikolay Aleksandrov 4095affce9a7SNikolay Aleksandrov if (br_vlan_is_brentry(vlan)) 4096affce9a7SNikolay Aleksandrov br_multicast_toggle_one_vlan(vlan, on); 4097f4b7002aSNikolay Aleksandrov } 4098f4b7002aSNikolay Aleksandrov 4099f4b7002aSNikolay Aleksandrov int br_multicast_toggle_vlan_snooping(struct net_bridge *br, bool on, 4100f4b7002aSNikolay Aleksandrov struct netlink_ext_ack *extack) 4101eb1d1641SHerbert Xu { 41027b54aaafSNikolay Aleksandrov struct net_bridge_vlan_group *vg; 41037b54aaafSNikolay Aleksandrov struct net_bridge_vlan *vlan; 4104f4b7002aSNikolay Aleksandrov struct net_bridge_port *p; 41057b54aaafSNikolay Aleksandrov 4106f4b7002aSNikolay Aleksandrov if (br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED) == on) 4107f4b7002aSNikolay Aleksandrov return 0; 4108f4b7002aSNikolay Aleksandrov 4109f4b7002aSNikolay Aleksandrov if (on && !br_opt_get(br, BROPT_VLAN_ENABLED)) { 4110f4b7002aSNikolay Aleksandrov NL_SET_ERR_MSG_MOD(extack, "Cannot enable multicast vlan snooping with vlan filtering disabled"); 4111f4b7002aSNikolay Aleksandrov return -EINVAL; 4112f4b7002aSNikolay Aleksandrov } 4113f4b7002aSNikolay Aleksandrov 4114f4b7002aSNikolay Aleksandrov vg = br_vlan_group(br); 4115f4b7002aSNikolay Aleksandrov if (!vg) 4116f4b7002aSNikolay Aleksandrov return 0; 4117f4b7002aSNikolay Aleksandrov 4118f4b7002aSNikolay Aleksandrov br_opt_toggle(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED, on); 4119f4b7002aSNikolay Aleksandrov 4120f4b7002aSNikolay Aleksandrov /* disable/enable non-vlan mcast contexts based on vlan snooping */ 4121f4b7002aSNikolay Aleksandrov if (on) 4122f4b7002aSNikolay Aleksandrov __br_multicast_stop(&br->multicast_ctx); 4123f4b7002aSNikolay Aleksandrov else 4124f4b7002aSNikolay Aleksandrov __br_multicast_open(&br->multicast_ctx); 4125f4b7002aSNikolay Aleksandrov list_for_each_entry(p, &br->port_list, list) { 4126f4b7002aSNikolay Aleksandrov if (on) 4127f4b7002aSNikolay Aleksandrov br_multicast_disable_port(p); 4128f4b7002aSNikolay Aleksandrov else 4129f4b7002aSNikolay Aleksandrov br_multicast_enable_port(p); 4130f4b7002aSNikolay Aleksandrov } 4131f4b7002aSNikolay Aleksandrov 4132f4b7002aSNikolay Aleksandrov list_for_each_entry(vlan, &vg->vlan_list, vlist) 4133f4b7002aSNikolay Aleksandrov br_multicast_toggle_vlan(vlan, on); 4134f4b7002aSNikolay Aleksandrov 4135f4b7002aSNikolay Aleksandrov return 0; 4136f4b7002aSNikolay Aleksandrov } 4137f4b7002aSNikolay Aleksandrov 41389dee572cSNikolay Aleksandrov bool br_multicast_toggle_global_vlan(struct net_bridge_vlan *vlan, bool on) 41399dee572cSNikolay Aleksandrov { 41409dee572cSNikolay Aleksandrov ASSERT_RTNL(); 41419dee572cSNikolay Aleksandrov 41429dee572cSNikolay Aleksandrov /* BR_VLFLAG_GLOBAL_MCAST_ENABLED relies on eventual consistency and 41439dee572cSNikolay Aleksandrov * requires only RTNL to change 41449dee572cSNikolay Aleksandrov */ 41459dee572cSNikolay Aleksandrov if (on == !!(vlan->priv_flags & BR_VLFLAG_GLOBAL_MCAST_ENABLED)) 41469dee572cSNikolay Aleksandrov return false; 41479dee572cSNikolay Aleksandrov 41489dee572cSNikolay Aleksandrov vlan->priv_flags ^= BR_VLFLAG_GLOBAL_MCAST_ENABLED; 41499dee572cSNikolay Aleksandrov br_multicast_toggle_vlan(vlan, on); 41509dee572cSNikolay Aleksandrov 41519dee572cSNikolay Aleksandrov return true; 41529dee572cSNikolay Aleksandrov } 41539dee572cSNikolay Aleksandrov 4154f4b7002aSNikolay Aleksandrov void br_multicast_stop(struct net_bridge *br) 4155f4b7002aSNikolay Aleksandrov { 41567b54aaafSNikolay Aleksandrov ASSERT_RTNL(); 41577b54aaafSNikolay Aleksandrov 4158f4b7002aSNikolay Aleksandrov if (br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED)) { 4159f4b7002aSNikolay Aleksandrov struct net_bridge_vlan_group *vg; 4160f4b7002aSNikolay Aleksandrov struct net_bridge_vlan *vlan; 4161f4b7002aSNikolay Aleksandrov 41627b54aaafSNikolay Aleksandrov vg = br_vlan_group(br); 41637b54aaafSNikolay Aleksandrov if (vg) { 41647b54aaafSNikolay Aleksandrov list_for_each_entry(vlan, &vg->vlan_list, vlist) { 41657b54aaafSNikolay Aleksandrov struct net_bridge_mcast *brmctx; 41667b54aaafSNikolay Aleksandrov 41677b54aaafSNikolay Aleksandrov brmctx = &vlan->br_mcast_ctx; 41687b54aaafSNikolay Aleksandrov if (br_vlan_is_brentry(vlan) && 41697b54aaafSNikolay Aleksandrov !br_multicast_ctx_vlan_disabled(brmctx)) 41707b54aaafSNikolay Aleksandrov __br_multicast_stop(&vlan->br_mcast_ctx); 41717b54aaafSNikolay Aleksandrov } 41727b54aaafSNikolay Aleksandrov } 4173cb486ce9SNikolay Aleksandrov } else { 4174613d61dbSNikolay Aleksandrov __br_multicast_stop(&br->multicast_ctx); 4175e10177abSSatish Ashok } 4176cb486ce9SNikolay Aleksandrov } 4177e10177abSSatish Ashok 4178e10177abSSatish Ashok void br_multicast_dev_del(struct net_bridge *br) 4179e10177abSSatish Ashok { 4180e10177abSSatish Ashok struct net_bridge_mdb_entry *mp; 41818b671779SNikolay Aleksandrov HLIST_HEAD(deleted_head); 418219e3a9c9SNikolay Aleksandrov struct hlist_node *tmp; 4183eb1d1641SHerbert Xu 4184eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 4185e12cec65SNikolay Aleksandrov hlist_for_each_entry_safe(mp, tmp, &br->mdb_list, mdb_node) 4186e12cec65SNikolay Aleksandrov br_multicast_del_mdb_entry(mp); 4187e12cec65SNikolay Aleksandrov hlist_move_list(&br->mcast_gc_list, &deleted_head); 4188eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 418919e3a9c9SNikolay Aleksandrov 4190613d61dbSNikolay Aleksandrov br_multicast_ctx_deinit(&br->multicast_ctx); 4191e12cec65SNikolay Aleksandrov br_multicast_gc(&deleted_head); 4192e12cec65SNikolay Aleksandrov cancel_work_sync(&br->mcast_gc_work); 41938b671779SNikolay Aleksandrov 41944329596cSNikolay Aleksandrov rcu_barrier(); 4195eb1d1641SHerbert Xu } 41960909e117SHerbert Xu 4197a97df080SNikolay Aleksandrov int br_multicast_set_router(struct net_bridge_mcast *brmctx, unsigned long val) 41980909e117SHerbert Xu { 41996ae4ae8eSLinus Lüssing int err = -EINVAL; 42000909e117SHerbert Xu 4201a97df080SNikolay Aleksandrov spin_lock_bh(&brmctx->br->multicast_lock); 42020909e117SHerbert Xu 42030909e117SHerbert Xu switch (val) { 42047f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_DISABLED: 42057f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_PERM: 4206a97df080SNikolay Aleksandrov br_mc_router_state_change(brmctx->br, val == MDB_RTR_TYPE_PERM); 4207d3d065c0SNikolay Aleksandrov del_timer(&brmctx->ip4_mc_router_timer); 4208a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 4209d3d065c0SNikolay Aleksandrov del_timer(&brmctx->ip6_mc_router_timer); 4210a3c02e76SLinus Lüssing #endif 4211d3d065c0SNikolay Aleksandrov brmctx->multicast_router = val; 421277041420SYotam Gigi err = 0; 421377041420SYotam Gigi break; 42147f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_TEMP_QUERY: 4215d3d065c0SNikolay Aleksandrov if (brmctx->multicast_router != MDB_RTR_TYPE_TEMP_QUERY) 4216a97df080SNikolay Aleksandrov br_mc_router_state_change(brmctx->br, false); 4217d3d065c0SNikolay Aleksandrov brmctx->multicast_router = val; 42180909e117SHerbert Xu err = 0; 42190909e117SHerbert Xu break; 42200909e117SHerbert Xu } 42210909e117SHerbert Xu 4222a97df080SNikolay Aleksandrov spin_unlock_bh(&brmctx->br->multicast_lock); 42230909e117SHerbert Xu 42240909e117SHerbert Xu return err; 42250909e117SHerbert Xu } 42260909e117SHerbert Xu 4227ed2d3597SLinus Lüssing static void 4228adc47037SNikolay Aleksandrov br_multicast_rport_del_notify(struct net_bridge_mcast_port *pmctx, bool deleted) 42297f0aec7aSNikolay Aleksandrov { 4230ed2d3597SLinus Lüssing if (!deleted) 42317f0aec7aSNikolay Aleksandrov return; 4232ed2d3597SLinus Lüssing 4233a3c02e76SLinus Lüssing /* For backwards compatibility for now, only notify if there is 4234a3c02e76SLinus Lüssing * no multicast router anymore for both IPv4 and IPv6. 4235a3c02e76SLinus Lüssing */ 4236adc47037SNikolay Aleksandrov if (!hlist_unhashed(&pmctx->ip4_rlist)) 4237a3c02e76SLinus Lüssing return; 4238a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 4239adc47037SNikolay Aleksandrov if (!hlist_unhashed(&pmctx->ip6_rlist)) 4240a3c02e76SLinus Lüssing return; 4241a3c02e76SLinus Lüssing #endif 4242a3c02e76SLinus Lüssing 42431e9ca456SNikolay Aleksandrov br_rtr_notify(pmctx->port->br->dev, pmctx, RTM_DELMDB); 4244adc47037SNikolay Aleksandrov br_port_mc_router_state_change(pmctx->port, false); 4245f12e7d95SNogah Frankel 4246f12e7d95SNogah Frankel /* don't allow timer refresh */ 4247adc47037SNikolay Aleksandrov if (pmctx->multicast_router == MDB_RTR_TYPE_TEMP) 4248adc47037SNikolay Aleksandrov pmctx->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 42497f0aec7aSNikolay Aleksandrov } 42507f0aec7aSNikolay Aleksandrov 4251a53581d5SNikolay Aleksandrov int br_multicast_set_port_router(struct net_bridge_mcast_port *pmctx, 4252a53581d5SNikolay Aleksandrov unsigned long val) 42530909e117SHerbert Xu { 4254a53581d5SNikolay Aleksandrov struct net_bridge_mcast *brmctx; 4255a55d8246SNikolay Aleksandrov unsigned long now = jiffies; 42566ae4ae8eSLinus Lüssing int err = -EINVAL; 4257ed2d3597SLinus Lüssing bool del = false; 42580909e117SHerbert Xu 4259a53581d5SNikolay Aleksandrov brmctx = br_multicast_port_ctx_get_global(pmctx); 4260ddd0d529SNikolay Aleksandrov spin_lock_bh(&brmctx->br->multicast_lock); 4261adc47037SNikolay Aleksandrov if (pmctx->multicast_router == val) { 4262a55d8246SNikolay Aleksandrov /* Refresh the temp router port timer */ 4263adc47037SNikolay Aleksandrov if (pmctx->multicast_router == MDB_RTR_TYPE_TEMP) { 4264adc47037SNikolay Aleksandrov mod_timer(&pmctx->ip4_mc_router_timer, 4265d3d065c0SNikolay Aleksandrov now + brmctx->multicast_querier_interval); 4266a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 4267adc47037SNikolay Aleksandrov mod_timer(&pmctx->ip6_mc_router_timer, 4268d3d065c0SNikolay Aleksandrov now + brmctx->multicast_querier_interval); 4269a3c02e76SLinus Lüssing #endif 4270a3c02e76SLinus Lüssing } 42714950cfd1SNikolay Aleksandrov err = 0; 42724950cfd1SNikolay Aleksandrov goto unlock; 42734950cfd1SNikolay Aleksandrov } 42740909e117SHerbert Xu switch (val) { 42757f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_DISABLED: 4276adc47037SNikolay Aleksandrov pmctx->multicast_router = MDB_RTR_TYPE_DISABLED; 4277adc47037SNikolay Aleksandrov del |= br_ip4_multicast_rport_del(pmctx); 4278adc47037SNikolay Aleksandrov del_timer(&pmctx->ip4_mc_router_timer); 4279adc47037SNikolay Aleksandrov del |= br_ip6_multicast_rport_del(pmctx); 4280a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 4281adc47037SNikolay Aleksandrov del_timer(&pmctx->ip6_mc_router_timer); 4282a3c02e76SLinus Lüssing #endif 4283adc47037SNikolay Aleksandrov br_multicast_rport_del_notify(pmctx, del); 42840909e117SHerbert Xu break; 42857f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_TEMP_QUERY: 4286adc47037SNikolay Aleksandrov pmctx->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 4287adc47037SNikolay Aleksandrov del |= br_ip4_multicast_rport_del(pmctx); 4288adc47037SNikolay Aleksandrov del |= br_ip6_multicast_rport_del(pmctx); 4289adc47037SNikolay Aleksandrov br_multicast_rport_del_notify(pmctx, del); 42907f0aec7aSNikolay Aleksandrov break; 42917f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_PERM: 4292adc47037SNikolay Aleksandrov pmctx->multicast_router = MDB_RTR_TYPE_PERM; 4293adc47037SNikolay Aleksandrov del_timer(&pmctx->ip4_mc_router_timer); 4294adc47037SNikolay Aleksandrov br_ip4_multicast_add_router(brmctx, pmctx); 4295a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 4296adc47037SNikolay Aleksandrov del_timer(&pmctx->ip6_mc_router_timer); 4297a3c02e76SLinus Lüssing #endif 4298adc47037SNikolay Aleksandrov br_ip6_multicast_add_router(brmctx, pmctx); 42990909e117SHerbert Xu break; 4300a55d8246SNikolay Aleksandrov case MDB_RTR_TYPE_TEMP: 4301adc47037SNikolay Aleksandrov pmctx->multicast_router = MDB_RTR_TYPE_TEMP; 4302adc47037SNikolay Aleksandrov br_ip4_multicast_mark_router(brmctx, pmctx); 4303adc47037SNikolay Aleksandrov br_ip6_multicast_mark_router(brmctx, pmctx); 4304a55d8246SNikolay Aleksandrov break; 43057f0aec7aSNikolay Aleksandrov default: 43067f0aec7aSNikolay Aleksandrov goto unlock; 43070909e117SHerbert Xu } 43087f0aec7aSNikolay Aleksandrov err = 0; 43097f0aec7aSNikolay Aleksandrov unlock: 4310ddd0d529SNikolay Aleksandrov spin_unlock_bh(&brmctx->br->multicast_lock); 43110909e117SHerbert Xu 43120909e117SHerbert Xu return err; 43130909e117SHerbert Xu } 4314561f1103SHerbert Xu 43152796d846SNikolay Aleksandrov int br_multicast_set_vlan_router(struct net_bridge_vlan *v, u8 mcast_router) 43162796d846SNikolay Aleksandrov { 43172796d846SNikolay Aleksandrov int err; 43182796d846SNikolay Aleksandrov 43192796d846SNikolay Aleksandrov if (br_vlan_is_master(v)) 43202796d846SNikolay Aleksandrov err = br_multicast_set_router(&v->br_mcast_ctx, mcast_router); 43212796d846SNikolay Aleksandrov else 43222796d846SNikolay Aleksandrov err = br_multicast_set_port_router(&v->port_mcast_ctx, 43232796d846SNikolay Aleksandrov mcast_router); 43242796d846SNikolay Aleksandrov 43252796d846SNikolay Aleksandrov return err; 43262796d846SNikolay Aleksandrov } 43272796d846SNikolay Aleksandrov 4328adc47037SNikolay Aleksandrov static void br_multicast_start_querier(struct net_bridge_mcast *brmctx, 432990010b36SLinus Lüssing struct bridge_mcast_own_query *query) 4330561f1103SHerbert Xu { 4331561f1103SHerbert Xu struct net_bridge_port *port; 433274857216SHerbert Xu 4333cb486ce9SNikolay Aleksandrov if (!br_multicast_ctx_matches_vlan_snooping(brmctx)) 4334cb486ce9SNikolay Aleksandrov return; 4335cb486ce9SNikolay Aleksandrov 43367b54aaafSNikolay Aleksandrov __br_multicast_open_query(brmctx->br, query); 433774857216SHerbert Xu 4338c5b493ceSNikolay Aleksandrov rcu_read_lock(); 4339adc47037SNikolay Aleksandrov list_for_each_entry_rcu(port, &brmctx->br->port_list, list) { 4340615cc23eSNikolay Aleksandrov struct bridge_mcast_own_query *ip4_own_query; 4341615cc23eSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 4342615cc23eSNikolay Aleksandrov struct bridge_mcast_own_query *ip6_own_query; 4343615cc23eSNikolay Aleksandrov #endif 4344615cc23eSNikolay Aleksandrov 4345615cc23eSNikolay Aleksandrov if (br_multicast_port_ctx_state_stopped(&port->multicast_ctx)) 434674857216SHerbert Xu continue; 434774857216SHerbert Xu 4348615cc23eSNikolay Aleksandrov if (br_multicast_ctx_is_vlan(brmctx)) { 4349615cc23eSNikolay Aleksandrov struct net_bridge_vlan *vlan; 4350615cc23eSNikolay Aleksandrov 43513f0d14efSNikolay Aleksandrov vlan = br_vlan_find(nbp_vlan_group_rcu(port), 43523f0d14efSNikolay Aleksandrov brmctx->vlan->vid); 4353615cc23eSNikolay Aleksandrov if (!vlan || 4354615cc23eSNikolay Aleksandrov br_multicast_port_ctx_state_stopped(&vlan->port_mcast_ctx)) 4355615cc23eSNikolay Aleksandrov continue; 4356615cc23eSNikolay Aleksandrov 4357615cc23eSNikolay Aleksandrov ip4_own_query = &vlan->port_mcast_ctx.ip4_own_query; 4358615cc23eSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 4359615cc23eSNikolay Aleksandrov ip6_own_query = &vlan->port_mcast_ctx.ip6_own_query; 4360615cc23eSNikolay Aleksandrov #endif 4361615cc23eSNikolay Aleksandrov } else { 4362615cc23eSNikolay Aleksandrov ip4_own_query = &port->multicast_ctx.ip4_own_query; 4363615cc23eSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 4364615cc23eSNikolay Aleksandrov ip6_own_query = &port->multicast_ctx.ip6_own_query; 4365615cc23eSNikolay Aleksandrov #endif 4366615cc23eSNikolay Aleksandrov } 4367615cc23eSNikolay Aleksandrov 4368adc47037SNikolay Aleksandrov if (query == &brmctx->ip4_own_query) 4369615cc23eSNikolay Aleksandrov br_multicast_enable(ip4_own_query); 4370cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 4371cc0fdd80SLinus Lüssing else 4372615cc23eSNikolay Aleksandrov br_multicast_enable(ip6_own_query); 4373cc0fdd80SLinus Lüssing #endif 437474857216SHerbert Xu } 4375c5b493ceSNikolay Aleksandrov rcu_read_unlock(); 437674857216SHerbert Xu } 437774857216SHerbert Xu 4378ae1ea84bSFlorian Fainelli int br_multicast_toggle(struct net_bridge *br, unsigned long val, 4379ae1ea84bSFlorian Fainelli struct netlink_ext_ack *extack) 438074857216SHerbert Xu { 43817cb3f921SNikolay Aleksandrov struct net_bridge_port *port; 4382851d0a73SJoseph Huang bool change_snoopers = false; 4383ae1ea84bSFlorian Fainelli int err = 0; 4384561f1103SHerbert Xu 4385ef5e0d82SAndrey Vagin spin_lock_bh(&br->multicast_lock); 438613cefad2SNikolay Aleksandrov if (!!br_opt_get(br, BROPT_MULTICAST_ENABLED) == !!val) 4387561f1103SHerbert Xu goto unlock; 4388561f1103SHerbert Xu 4389ae1ea84bSFlorian Fainelli err = br_mc_disabled_update(br->dev, val, extack); 4390ae1ea84bSFlorian Fainelli if (err == -EOPNOTSUPP) 4391ae1ea84bSFlorian Fainelli err = 0; 4392ae1ea84bSFlorian Fainelli if (err) 4393ae1ea84bSFlorian Fainelli goto unlock; 4394ae1ea84bSFlorian Fainelli 439513cefad2SNikolay Aleksandrov br_opt_toggle(br, BROPT_MULTICAST_ENABLED, !!val); 43964effd28cSLinus Lüssing if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) { 4397851d0a73SJoseph Huang change_snoopers = true; 4398561f1103SHerbert Xu goto unlock; 43994effd28cSLinus Lüssing } 4400561f1103SHerbert Xu 44013a7fda06SHerbert Xu if (!netif_running(br->dev)) 44023a7fda06SHerbert Xu goto unlock; 44033a7fda06SHerbert Xu 44047cb3f921SNikolay Aleksandrov br_multicast_open(br); 44057cb3f921SNikolay Aleksandrov list_for_each_entry(port, &br->port_list, list) 4406adc47037SNikolay Aleksandrov __br_multicast_enable_port_ctx(&port->multicast_ctx); 4407561f1103SHerbert Xu 4408851d0a73SJoseph Huang change_snoopers = true; 4409851d0a73SJoseph Huang 4410561f1103SHerbert Xu unlock: 4411ef5e0d82SAndrey Vagin spin_unlock_bh(&br->multicast_lock); 4412561f1103SHerbert Xu 4413851d0a73SJoseph Huang /* br_multicast_join_snoopers has the potential to cause 4414851d0a73SJoseph Huang * an MLD Report/Leave to be delivered to br_multicast_rcv, 4415851d0a73SJoseph Huang * which would in turn call br_multicast_add_group, which would 4416851d0a73SJoseph Huang * attempt to acquire multicast_lock. This function should be 4417851d0a73SJoseph Huang * called after the lock has been released to avoid deadlocks on 4418851d0a73SJoseph Huang * multicast_lock. 4419851d0a73SJoseph Huang * 4420851d0a73SJoseph Huang * br_multicast_leave_snoopers does not have the problem since 4421851d0a73SJoseph Huang * br_multicast_rcv first checks BROPT_MULTICAST_ENABLED, and 4422851d0a73SJoseph Huang * returns without calling br_multicast_ipv4/6_rcv if it's not 4423851d0a73SJoseph Huang * enabled. Moved both functions out just for symmetry. 4424851d0a73SJoseph Huang */ 4425851d0a73SJoseph Huang if (change_snoopers) { 4426851d0a73SJoseph Huang if (br_opt_get(br, BROPT_MULTICAST_ENABLED)) 4427851d0a73SJoseph Huang br_multicast_join_snoopers(br); 4428851d0a73SJoseph Huang else 4429851d0a73SJoseph Huang br_multicast_leave_snoopers(br); 4430851d0a73SJoseph Huang } 4431851d0a73SJoseph Huang 4432ae1ea84bSFlorian Fainelli return err; 4433561f1103SHerbert Xu } 4434b195167fSHerbert Xu 44359341b988SIdo Schimmel bool br_multicast_enabled(const struct net_device *dev) 44369341b988SIdo Schimmel { 44379341b988SIdo Schimmel struct net_bridge *br = netdev_priv(dev); 44389341b988SIdo Schimmel 443913cefad2SNikolay Aleksandrov return !!br_opt_get(br, BROPT_MULTICAST_ENABLED); 44409341b988SIdo Schimmel } 44419341b988SIdo Schimmel EXPORT_SYMBOL_GPL(br_multicast_enabled); 44429341b988SIdo Schimmel 44430912bda4SYotam Gigi bool br_multicast_router(const struct net_device *dev) 44440912bda4SYotam Gigi { 44450912bda4SYotam Gigi struct net_bridge *br = netdev_priv(dev); 44460912bda4SYotam Gigi bool is_router; 44470912bda4SYotam Gigi 44480912bda4SYotam Gigi spin_lock_bh(&br->multicast_lock); 4449adc47037SNikolay Aleksandrov is_router = br_multicast_is_router(&br->multicast_ctx, NULL); 44500912bda4SYotam Gigi spin_unlock_bh(&br->multicast_lock); 44510912bda4SYotam Gigi return is_router; 44520912bda4SYotam Gigi } 44530912bda4SYotam Gigi EXPORT_SYMBOL_GPL(br_multicast_router); 44540912bda4SYotam Gigi 445562938182SNikolay Aleksandrov int br_multicast_set_querier(struct net_bridge_mcast *brmctx, unsigned long val) 4456c5c23260SHerbert Xu { 4457b00589afSLinus Lüssing unsigned long max_delay; 4458b00589afSLinus Lüssing 4459c5c23260SHerbert Xu val = !!val; 4460c5c23260SHerbert Xu 446162938182SNikolay Aleksandrov spin_lock_bh(&brmctx->br->multicast_lock); 44624d5b4e84SNikolay Aleksandrov if (brmctx->multicast_querier == val) 4463c5c23260SHerbert Xu goto unlock; 4464c5c23260SHerbert Xu 44654d5b4e84SNikolay Aleksandrov WRITE_ONCE(brmctx->multicast_querier, val); 4466b00589afSLinus Lüssing if (!val) 4467b00589afSLinus Lüssing goto unlock; 4468b00589afSLinus Lüssing 4469d3d065c0SNikolay Aleksandrov max_delay = brmctx->multicast_query_response_interval; 4470b00589afSLinus Lüssing 4471d3d065c0SNikolay Aleksandrov if (!timer_pending(&brmctx->ip4_other_query.timer)) 4472d3d065c0SNikolay Aleksandrov brmctx->ip4_other_query.delay_time = jiffies + max_delay; 4473cc0fdd80SLinus Lüssing 4474adc47037SNikolay Aleksandrov br_multicast_start_querier(brmctx, &brmctx->ip4_own_query); 4475cc0fdd80SLinus Lüssing 4476cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 4477d3d065c0SNikolay Aleksandrov if (!timer_pending(&brmctx->ip6_other_query.timer)) 4478d3d065c0SNikolay Aleksandrov brmctx->ip6_other_query.delay_time = jiffies + max_delay; 4479cc0fdd80SLinus Lüssing 4480adc47037SNikolay Aleksandrov br_multicast_start_querier(brmctx, &brmctx->ip6_own_query); 4481cc0fdd80SLinus Lüssing #endif 4482c5c23260SHerbert Xu 4483c5c23260SHerbert Xu unlock: 448462938182SNikolay Aleksandrov spin_unlock_bh(&brmctx->br->multicast_lock); 4485c5c23260SHerbert Xu 4486c5c23260SHerbert Xu return 0; 4487c5c23260SHerbert Xu } 4488c5c23260SHerbert Xu 4489df271cd6SNikolay Aleksandrov int br_multicast_set_igmp_version(struct net_bridge_mcast *brmctx, 4490df271cd6SNikolay Aleksandrov unsigned long val) 44915e923585SNikolay Aleksandrov { 44925e923585SNikolay Aleksandrov /* Currently we support only version 2 and 3 */ 44935e923585SNikolay Aleksandrov switch (val) { 44945e923585SNikolay Aleksandrov case 2: 44955e923585SNikolay Aleksandrov case 3: 44965e923585SNikolay Aleksandrov break; 44975e923585SNikolay Aleksandrov default: 44985e923585SNikolay Aleksandrov return -EINVAL; 44995e923585SNikolay Aleksandrov } 45005e923585SNikolay Aleksandrov 4501df271cd6SNikolay Aleksandrov spin_lock_bh(&brmctx->br->multicast_lock); 4502df271cd6SNikolay Aleksandrov brmctx->multicast_igmp_version = val; 4503df271cd6SNikolay Aleksandrov spin_unlock_bh(&brmctx->br->multicast_lock); 45045e923585SNikolay Aleksandrov 45055e923585SNikolay Aleksandrov return 0; 45065e923585SNikolay Aleksandrov } 45075e923585SNikolay Aleksandrov 4508aa2ae3e7SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 4509df271cd6SNikolay Aleksandrov int br_multicast_set_mld_version(struct net_bridge_mcast *brmctx, 4510df271cd6SNikolay Aleksandrov unsigned long val) 4511aa2ae3e7SNikolay Aleksandrov { 4512aa2ae3e7SNikolay Aleksandrov /* Currently we support version 1 and 2 */ 4513aa2ae3e7SNikolay Aleksandrov switch (val) { 4514aa2ae3e7SNikolay Aleksandrov case 1: 4515aa2ae3e7SNikolay Aleksandrov case 2: 4516aa2ae3e7SNikolay Aleksandrov break; 4517aa2ae3e7SNikolay Aleksandrov default: 4518aa2ae3e7SNikolay Aleksandrov return -EINVAL; 4519aa2ae3e7SNikolay Aleksandrov } 4520aa2ae3e7SNikolay Aleksandrov 4521df271cd6SNikolay Aleksandrov spin_lock_bh(&brmctx->br->multicast_lock); 4522df271cd6SNikolay Aleksandrov brmctx->multicast_mld_version = val; 4523df271cd6SNikolay Aleksandrov spin_unlock_bh(&brmctx->br->multicast_lock); 4524aa2ae3e7SNikolay Aleksandrov 4525aa2ae3e7SNikolay Aleksandrov return 0; 4526aa2ae3e7SNikolay Aleksandrov } 4527aa2ae3e7SNikolay Aleksandrov #endif 4528aa2ae3e7SNikolay Aleksandrov 452999b40610SNikolay Aleksandrov void br_multicast_set_query_intvl(struct net_bridge_mcast *brmctx, 453099b40610SNikolay Aleksandrov unsigned long val) 453199b40610SNikolay Aleksandrov { 453299b40610SNikolay Aleksandrov unsigned long intvl_jiffies = clock_t_to_jiffies(val); 453399b40610SNikolay Aleksandrov 453499b40610SNikolay Aleksandrov if (intvl_jiffies < BR_MULTICAST_QUERY_INTVL_MIN) { 453599b40610SNikolay Aleksandrov br_info(brmctx->br, 453699b40610SNikolay Aleksandrov "trying to set multicast query interval below minimum, setting to %lu (%ums)\n", 453799b40610SNikolay Aleksandrov jiffies_to_clock_t(BR_MULTICAST_QUERY_INTVL_MIN), 453899b40610SNikolay Aleksandrov jiffies_to_msecs(BR_MULTICAST_QUERY_INTVL_MIN)); 453999b40610SNikolay Aleksandrov intvl_jiffies = BR_MULTICAST_QUERY_INTVL_MIN; 454099b40610SNikolay Aleksandrov } 454199b40610SNikolay Aleksandrov 454299b40610SNikolay Aleksandrov brmctx->multicast_query_interval = intvl_jiffies; 454399b40610SNikolay Aleksandrov } 454499b40610SNikolay Aleksandrov 4545f83a112bSNikolay Aleksandrov void br_multicast_set_startup_query_intvl(struct net_bridge_mcast *brmctx, 4546f83a112bSNikolay Aleksandrov unsigned long val) 4547f83a112bSNikolay Aleksandrov { 4548f83a112bSNikolay Aleksandrov unsigned long intvl_jiffies = clock_t_to_jiffies(val); 4549f83a112bSNikolay Aleksandrov 4550f83a112bSNikolay Aleksandrov if (intvl_jiffies < BR_MULTICAST_STARTUP_QUERY_INTVL_MIN) { 4551f83a112bSNikolay Aleksandrov br_info(brmctx->br, 4552f83a112bSNikolay Aleksandrov "trying to set multicast startup query interval below minimum, setting to %lu (%ums)\n", 4553f83a112bSNikolay Aleksandrov jiffies_to_clock_t(BR_MULTICAST_STARTUP_QUERY_INTVL_MIN), 4554f83a112bSNikolay Aleksandrov jiffies_to_msecs(BR_MULTICAST_STARTUP_QUERY_INTVL_MIN)); 4555f83a112bSNikolay Aleksandrov intvl_jiffies = BR_MULTICAST_STARTUP_QUERY_INTVL_MIN; 4556f83a112bSNikolay Aleksandrov } 4557f83a112bSNikolay Aleksandrov 4558f83a112bSNikolay Aleksandrov brmctx->multicast_startup_query_interval = intvl_jiffies; 4559f83a112bSNikolay Aleksandrov } 4560f83a112bSNikolay Aleksandrov 456107f8ac4aSLinus Lüssing /** 456207f8ac4aSLinus Lüssing * br_multicast_list_adjacent - Returns snooped multicast addresses 456307f8ac4aSLinus Lüssing * @dev: The bridge port adjacent to which to retrieve addresses 456407f8ac4aSLinus Lüssing * @br_ip_list: The list to store found, snooped multicast IP addresses in 456507f8ac4aSLinus Lüssing * 456607f8ac4aSLinus Lüssing * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast 456707f8ac4aSLinus Lüssing * snooping feature on all bridge ports of dev's bridge device, excluding 456807f8ac4aSLinus Lüssing * the addresses from dev itself. 456907f8ac4aSLinus Lüssing * 457007f8ac4aSLinus Lüssing * Returns the number of items added to br_ip_list. 457107f8ac4aSLinus Lüssing * 457207f8ac4aSLinus Lüssing * Notes: 457307f8ac4aSLinus Lüssing * - br_ip_list needs to be initialized by caller 457407f8ac4aSLinus Lüssing * - br_ip_list might contain duplicates in the end 457507f8ac4aSLinus Lüssing * (needs to be taken care of by caller) 457607f8ac4aSLinus Lüssing * - br_ip_list needs to be freed by caller 457707f8ac4aSLinus Lüssing */ 457807f8ac4aSLinus Lüssing int br_multicast_list_adjacent(struct net_device *dev, 457907f8ac4aSLinus Lüssing struct list_head *br_ip_list) 458007f8ac4aSLinus Lüssing { 458107f8ac4aSLinus Lüssing struct net_bridge *br; 458207f8ac4aSLinus Lüssing struct net_bridge_port *port; 458307f8ac4aSLinus Lüssing struct net_bridge_port_group *group; 458407f8ac4aSLinus Lüssing struct br_ip_list *entry; 458507f8ac4aSLinus Lüssing int count = 0; 458607f8ac4aSLinus Lüssing 458707f8ac4aSLinus Lüssing rcu_read_lock(); 458835f861e3SJulian Wiedmann if (!br_ip_list || !netif_is_bridge_port(dev)) 458907f8ac4aSLinus Lüssing goto unlock; 459007f8ac4aSLinus Lüssing 459107f8ac4aSLinus Lüssing port = br_port_get_rcu(dev); 459207f8ac4aSLinus Lüssing if (!port || !port->br) 459307f8ac4aSLinus Lüssing goto unlock; 459407f8ac4aSLinus Lüssing 459507f8ac4aSLinus Lüssing br = port->br; 459607f8ac4aSLinus Lüssing 459707f8ac4aSLinus Lüssing list_for_each_entry_rcu(port, &br->port_list, list) { 459807f8ac4aSLinus Lüssing if (!port->dev || port->dev == dev) 459907f8ac4aSLinus Lüssing continue; 460007f8ac4aSLinus Lüssing 460107f8ac4aSLinus Lüssing hlist_for_each_entry_rcu(group, &port->mglist, mglist) { 460207f8ac4aSLinus Lüssing entry = kmalloc(sizeof(*entry), GFP_ATOMIC); 460307f8ac4aSLinus Lüssing if (!entry) 460407f8ac4aSLinus Lüssing goto unlock; 460507f8ac4aSLinus Lüssing 4606085b53c8SNikolay Aleksandrov entry->addr = group->key.addr; 460707f8ac4aSLinus Lüssing list_add(&entry->list, br_ip_list); 460807f8ac4aSLinus Lüssing count++; 460907f8ac4aSLinus Lüssing } 461007f8ac4aSLinus Lüssing } 461107f8ac4aSLinus Lüssing 461207f8ac4aSLinus Lüssing unlock: 461307f8ac4aSLinus Lüssing rcu_read_unlock(); 461407f8ac4aSLinus Lüssing return count; 461507f8ac4aSLinus Lüssing } 461607f8ac4aSLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_list_adjacent); 46172cd41431SLinus Lüssing 46182cd41431SLinus Lüssing /** 4619c34963e2SLinus Lüssing * br_multicast_has_querier_anywhere - Checks for a querier on a bridge 4620c34963e2SLinus Lüssing * @dev: The bridge port providing the bridge on which to check for a querier 4621c34963e2SLinus Lüssing * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 4622c34963e2SLinus Lüssing * 4623c34963e2SLinus Lüssing * Checks whether the given interface has a bridge on top and if so returns 4624c34963e2SLinus Lüssing * true if a valid querier exists anywhere on the bridged link layer. 4625c34963e2SLinus Lüssing * Otherwise returns false. 4626c34963e2SLinus Lüssing */ 4627c34963e2SLinus Lüssing bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto) 4628c34963e2SLinus Lüssing { 4629c34963e2SLinus Lüssing struct net_bridge *br; 4630c34963e2SLinus Lüssing struct net_bridge_port *port; 4631c34963e2SLinus Lüssing struct ethhdr eth; 4632c34963e2SLinus Lüssing bool ret = false; 4633c34963e2SLinus Lüssing 4634c34963e2SLinus Lüssing rcu_read_lock(); 463535f861e3SJulian Wiedmann if (!netif_is_bridge_port(dev)) 4636c34963e2SLinus Lüssing goto unlock; 4637c34963e2SLinus Lüssing 4638c34963e2SLinus Lüssing port = br_port_get_rcu(dev); 4639c34963e2SLinus Lüssing if (!port || !port->br) 4640c34963e2SLinus Lüssing goto unlock; 4641c34963e2SLinus Lüssing 4642c34963e2SLinus Lüssing br = port->br; 4643c34963e2SLinus Lüssing 4644c34963e2SLinus Lüssing memset(ð, 0, sizeof(eth)); 4645c34963e2SLinus Lüssing eth.h_proto = htons(proto); 4646c34963e2SLinus Lüssing 4647adc47037SNikolay Aleksandrov ret = br_multicast_querier_exists(&br->multicast_ctx, ð, NULL); 4648c34963e2SLinus Lüssing 4649c34963e2SLinus Lüssing unlock: 4650c34963e2SLinus Lüssing rcu_read_unlock(); 4651c34963e2SLinus Lüssing return ret; 4652c34963e2SLinus Lüssing } 4653c34963e2SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere); 4654c34963e2SLinus Lüssing 4655c34963e2SLinus Lüssing /** 46562cd41431SLinus Lüssing * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port 46572cd41431SLinus Lüssing * @dev: The bridge port adjacent to which to check for a querier 46582cd41431SLinus Lüssing * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 46592cd41431SLinus Lüssing * 46602cd41431SLinus Lüssing * Checks whether the given interface has a bridge on top and if so returns 46612cd41431SLinus Lüssing * true if a selected querier is behind one of the other ports of this 46622cd41431SLinus Lüssing * bridge. Otherwise returns false. 46632cd41431SLinus Lüssing */ 46642cd41431SLinus Lüssing bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto) 46652cd41431SLinus Lüssing { 4666d3d065c0SNikolay Aleksandrov struct net_bridge_mcast *brmctx; 46672cd41431SLinus Lüssing struct net_bridge *br; 46682cd41431SLinus Lüssing struct net_bridge_port *port; 46692cd41431SLinus Lüssing bool ret = false; 4670bb18ef8eSNikolay Aleksandrov int port_ifidx; 46712cd41431SLinus Lüssing 46722cd41431SLinus Lüssing rcu_read_lock(); 467335f861e3SJulian Wiedmann if (!netif_is_bridge_port(dev)) 46742cd41431SLinus Lüssing goto unlock; 46752cd41431SLinus Lüssing 46762cd41431SLinus Lüssing port = br_port_get_rcu(dev); 46772cd41431SLinus Lüssing if (!port || !port->br) 46782cd41431SLinus Lüssing goto unlock; 46792cd41431SLinus Lüssing 46802cd41431SLinus Lüssing br = port->br; 4681d3d065c0SNikolay Aleksandrov brmctx = &br->multicast_ctx; 46822cd41431SLinus Lüssing 46832cd41431SLinus Lüssing switch (proto) { 46842cd41431SLinus Lüssing case ETH_P_IP: 4685bb18ef8eSNikolay Aleksandrov port_ifidx = brmctx->ip4_querier.port_ifidx; 4686d3d065c0SNikolay Aleksandrov if (!timer_pending(&brmctx->ip4_other_query.timer) || 4687bb18ef8eSNikolay Aleksandrov port_ifidx == port->dev->ifindex) 46882cd41431SLinus Lüssing goto unlock; 46892cd41431SLinus Lüssing break; 46903993c4e1SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 46912cd41431SLinus Lüssing case ETH_P_IPV6: 4692bb18ef8eSNikolay Aleksandrov port_ifidx = brmctx->ip6_querier.port_ifidx; 4693d3d065c0SNikolay Aleksandrov if (!timer_pending(&brmctx->ip6_other_query.timer) || 4694bb18ef8eSNikolay Aleksandrov port_ifidx == port->dev->ifindex) 46952cd41431SLinus Lüssing goto unlock; 46962cd41431SLinus Lüssing break; 46973993c4e1SLinus Lüssing #endif 46982cd41431SLinus Lüssing default: 46992cd41431SLinus Lüssing goto unlock; 47002cd41431SLinus Lüssing } 47012cd41431SLinus Lüssing 47022cd41431SLinus Lüssing ret = true; 47032cd41431SLinus Lüssing unlock: 47042cd41431SLinus Lüssing rcu_read_unlock(); 47052cd41431SLinus Lüssing return ret; 47062cd41431SLinus Lüssing } 47072cd41431SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent); 47081080ab95SNikolay Aleksandrov 47093b85f9baSLinus Lüssing /** 47103b85f9baSLinus Lüssing * br_multicast_has_router_adjacent - Checks for a router behind a bridge port 47113b85f9baSLinus Lüssing * @dev: The bridge port adjacent to which to check for a multicast router 47123b85f9baSLinus Lüssing * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 47133b85f9baSLinus Lüssing * 47143b85f9baSLinus Lüssing * Checks whether the given interface has a bridge on top and if so returns 47153b85f9baSLinus Lüssing * true if a multicast router is behind one of the other ports of this 47163b85f9baSLinus Lüssing * bridge. Otherwise returns false. 47173b85f9baSLinus Lüssing */ 47183b85f9baSLinus Lüssing bool br_multicast_has_router_adjacent(struct net_device *dev, int proto) 47193b85f9baSLinus Lüssing { 47209632233eSNikolay Aleksandrov struct net_bridge_mcast_port *pmctx; 4721d3d065c0SNikolay Aleksandrov struct net_bridge_mcast *brmctx; 47229632233eSNikolay Aleksandrov struct net_bridge_port *port; 47233b85f9baSLinus Lüssing bool ret = false; 47243b85f9baSLinus Lüssing 47253b85f9baSLinus Lüssing rcu_read_lock(); 47263b85f9baSLinus Lüssing port = br_port_get_check_rcu(dev); 47273b85f9baSLinus Lüssing if (!port) 47283b85f9baSLinus Lüssing goto unlock; 47293b85f9baSLinus Lüssing 4730d3d065c0SNikolay Aleksandrov brmctx = &port->br->multicast_ctx; 47313b85f9baSLinus Lüssing switch (proto) { 47323b85f9baSLinus Lüssing case ETH_P_IP: 4733d3d065c0SNikolay Aleksandrov hlist_for_each_entry_rcu(pmctx, &brmctx->ip4_mc_router_list, 47343b85f9baSLinus Lüssing ip4_rlist) { 47359632233eSNikolay Aleksandrov if (pmctx->port == port) 47363b85f9baSLinus Lüssing continue; 47373b85f9baSLinus Lüssing 47383b85f9baSLinus Lüssing ret = true; 47393b85f9baSLinus Lüssing goto unlock; 47403b85f9baSLinus Lüssing } 47413b85f9baSLinus Lüssing break; 47423b85f9baSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 47433b85f9baSLinus Lüssing case ETH_P_IPV6: 4744d3d065c0SNikolay Aleksandrov hlist_for_each_entry_rcu(pmctx, &brmctx->ip6_mc_router_list, 47453b85f9baSLinus Lüssing ip6_rlist) { 47469632233eSNikolay Aleksandrov if (pmctx->port == port) 47473b85f9baSLinus Lüssing continue; 47483b85f9baSLinus Lüssing 47493b85f9baSLinus Lüssing ret = true; 47503b85f9baSLinus Lüssing goto unlock; 47513b85f9baSLinus Lüssing } 47523b85f9baSLinus Lüssing break; 47533b85f9baSLinus Lüssing #endif 47543b85f9baSLinus Lüssing default: 47553b85f9baSLinus Lüssing /* when compiled without IPv6 support, be conservative and 47563b85f9baSLinus Lüssing * always assume presence of an IPv6 multicast router 47573b85f9baSLinus Lüssing */ 47583b85f9baSLinus Lüssing ret = true; 47593b85f9baSLinus Lüssing } 47603b85f9baSLinus Lüssing 47613b85f9baSLinus Lüssing unlock: 47623b85f9baSLinus Lüssing rcu_read_unlock(); 47633b85f9baSLinus Lüssing return ret; 47643b85f9baSLinus Lüssing } 47653b85f9baSLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_router_adjacent); 47663b85f9baSLinus Lüssing 47671080ab95SNikolay Aleksandrov static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats, 4768a65056ecSNikolay Aleksandrov const struct sk_buff *skb, u8 type, u8 dir) 47691080ab95SNikolay Aleksandrov { 47701080ab95SNikolay Aleksandrov struct bridge_mcast_stats *pstats = this_cpu_ptr(stats); 4771a65056ecSNikolay Aleksandrov __be16 proto = skb->protocol; 4772a65056ecSNikolay Aleksandrov unsigned int t_len; 47731080ab95SNikolay Aleksandrov 47741080ab95SNikolay Aleksandrov u64_stats_update_begin(&pstats->syncp); 47751080ab95SNikolay Aleksandrov switch (proto) { 47761080ab95SNikolay Aleksandrov case htons(ETH_P_IP): 4777a65056ecSNikolay Aleksandrov t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb); 47781080ab95SNikolay Aleksandrov switch (type) { 47791080ab95SNikolay Aleksandrov case IGMP_HOST_MEMBERSHIP_REPORT: 47801080ab95SNikolay Aleksandrov pstats->mstats.igmp_v1reports[dir]++; 47811080ab95SNikolay Aleksandrov break; 47821080ab95SNikolay Aleksandrov case IGMPV2_HOST_MEMBERSHIP_REPORT: 47831080ab95SNikolay Aleksandrov pstats->mstats.igmp_v2reports[dir]++; 47841080ab95SNikolay Aleksandrov break; 47851080ab95SNikolay Aleksandrov case IGMPV3_HOST_MEMBERSHIP_REPORT: 47861080ab95SNikolay Aleksandrov pstats->mstats.igmp_v3reports[dir]++; 47871080ab95SNikolay Aleksandrov break; 47881080ab95SNikolay Aleksandrov case IGMP_HOST_MEMBERSHIP_QUERY: 4789a65056ecSNikolay Aleksandrov if (t_len != sizeof(struct igmphdr)) { 4790a65056ecSNikolay Aleksandrov pstats->mstats.igmp_v3queries[dir]++; 4791a65056ecSNikolay Aleksandrov } else { 4792a65056ecSNikolay Aleksandrov unsigned int offset = skb_transport_offset(skb); 4793a65056ecSNikolay Aleksandrov struct igmphdr *ih, _ihdr; 4794a65056ecSNikolay Aleksandrov 4795a65056ecSNikolay Aleksandrov ih = skb_header_pointer(skb, offset, 4796a65056ecSNikolay Aleksandrov sizeof(_ihdr), &_ihdr); 4797a65056ecSNikolay Aleksandrov if (!ih) 4798a65056ecSNikolay Aleksandrov break; 4799a65056ecSNikolay Aleksandrov if (!ih->code) 4800a65056ecSNikolay Aleksandrov pstats->mstats.igmp_v1queries[dir]++; 4801a65056ecSNikolay Aleksandrov else 4802a65056ecSNikolay Aleksandrov pstats->mstats.igmp_v2queries[dir]++; 4803a65056ecSNikolay Aleksandrov } 48041080ab95SNikolay Aleksandrov break; 48051080ab95SNikolay Aleksandrov case IGMP_HOST_LEAVE_MESSAGE: 48061080ab95SNikolay Aleksandrov pstats->mstats.igmp_leaves[dir]++; 48071080ab95SNikolay Aleksandrov break; 48081080ab95SNikolay Aleksandrov } 48091080ab95SNikolay Aleksandrov break; 48101080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 48111080ab95SNikolay Aleksandrov case htons(ETH_P_IPV6): 4812a65056ecSNikolay Aleksandrov t_len = ntohs(ipv6_hdr(skb)->payload_len) + 4813a65056ecSNikolay Aleksandrov sizeof(struct ipv6hdr); 4814a65056ecSNikolay Aleksandrov t_len -= skb_network_header_len(skb); 48151080ab95SNikolay Aleksandrov switch (type) { 48161080ab95SNikolay Aleksandrov case ICMPV6_MGM_REPORT: 48171080ab95SNikolay Aleksandrov pstats->mstats.mld_v1reports[dir]++; 48181080ab95SNikolay Aleksandrov break; 48191080ab95SNikolay Aleksandrov case ICMPV6_MLD2_REPORT: 48201080ab95SNikolay Aleksandrov pstats->mstats.mld_v2reports[dir]++; 48211080ab95SNikolay Aleksandrov break; 48221080ab95SNikolay Aleksandrov case ICMPV6_MGM_QUERY: 4823a65056ecSNikolay Aleksandrov if (t_len != sizeof(struct mld_msg)) 4824a65056ecSNikolay Aleksandrov pstats->mstats.mld_v2queries[dir]++; 4825a65056ecSNikolay Aleksandrov else 4826a65056ecSNikolay Aleksandrov pstats->mstats.mld_v1queries[dir]++; 48271080ab95SNikolay Aleksandrov break; 48281080ab95SNikolay Aleksandrov case ICMPV6_MGM_REDUCTION: 48291080ab95SNikolay Aleksandrov pstats->mstats.mld_leaves[dir]++; 48301080ab95SNikolay Aleksandrov break; 48311080ab95SNikolay Aleksandrov } 48321080ab95SNikolay Aleksandrov break; 48331080ab95SNikolay Aleksandrov #endif /* CONFIG_IPV6 */ 48341080ab95SNikolay Aleksandrov } 48351080ab95SNikolay Aleksandrov u64_stats_update_end(&pstats->syncp); 48361080ab95SNikolay Aleksandrov } 48371080ab95SNikolay Aleksandrov 4838adc47037SNikolay Aleksandrov void br_multicast_count(struct net_bridge *br, 4839adc47037SNikolay Aleksandrov const struct net_bridge_port *p, 4840a65056ecSNikolay Aleksandrov const struct sk_buff *skb, u8 type, u8 dir) 48411080ab95SNikolay Aleksandrov { 48421080ab95SNikolay Aleksandrov struct bridge_mcast_stats __percpu *stats; 48431080ab95SNikolay Aleksandrov 48441080ab95SNikolay Aleksandrov /* if multicast_disabled is true then igmp type can't be set */ 4845675779adSNikolay Aleksandrov if (!type || !br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)) 48461080ab95SNikolay Aleksandrov return; 48471080ab95SNikolay Aleksandrov 48481080ab95SNikolay Aleksandrov if (p) 48491080ab95SNikolay Aleksandrov stats = p->mcast_stats; 48501080ab95SNikolay Aleksandrov else 48511080ab95SNikolay Aleksandrov stats = br->mcast_stats; 48521080ab95SNikolay Aleksandrov if (WARN_ON(!stats)) 48531080ab95SNikolay Aleksandrov return; 48541080ab95SNikolay Aleksandrov 4855a65056ecSNikolay Aleksandrov br_mcast_stats_add(stats, skb, type, dir); 48561080ab95SNikolay Aleksandrov } 48571080ab95SNikolay Aleksandrov 48581080ab95SNikolay Aleksandrov int br_multicast_init_stats(struct net_bridge *br) 48591080ab95SNikolay Aleksandrov { 48601080ab95SNikolay Aleksandrov br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats); 48611080ab95SNikolay Aleksandrov if (!br->mcast_stats) 48621080ab95SNikolay Aleksandrov return -ENOMEM; 48631080ab95SNikolay Aleksandrov 48641080ab95SNikolay Aleksandrov return 0; 48651080ab95SNikolay Aleksandrov } 48661080ab95SNikolay Aleksandrov 4867b6fe0440SIdo Schimmel void br_multicast_uninit_stats(struct net_bridge *br) 4868b6fe0440SIdo Schimmel { 4869b6fe0440SIdo Schimmel free_percpu(br->mcast_stats); 4870b6fe0440SIdo Schimmel } 4871b6fe0440SIdo Schimmel 4872b3b6a84cSArnd Bergmann /* noinline for https://bugs.llvm.org/show_bug.cgi?id=45802#c9 */ 4873b3b6a84cSArnd Bergmann static noinline_for_stack void mcast_stats_add_dir(u64 *dst, u64 *src) 48741080ab95SNikolay Aleksandrov { 48751080ab95SNikolay Aleksandrov dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX]; 48761080ab95SNikolay Aleksandrov dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX]; 48771080ab95SNikolay Aleksandrov } 48781080ab95SNikolay Aleksandrov 48791080ab95SNikolay Aleksandrov void br_multicast_get_stats(const struct net_bridge *br, 48801080ab95SNikolay Aleksandrov const struct net_bridge_port *p, 48811080ab95SNikolay Aleksandrov struct br_mcast_stats *dest) 48821080ab95SNikolay Aleksandrov { 48831080ab95SNikolay Aleksandrov struct bridge_mcast_stats __percpu *stats; 48841080ab95SNikolay Aleksandrov struct br_mcast_stats tdst; 48851080ab95SNikolay Aleksandrov int i; 48861080ab95SNikolay Aleksandrov 48871080ab95SNikolay Aleksandrov memset(dest, 0, sizeof(*dest)); 48881080ab95SNikolay Aleksandrov if (p) 48891080ab95SNikolay Aleksandrov stats = p->mcast_stats; 48901080ab95SNikolay Aleksandrov else 48911080ab95SNikolay Aleksandrov stats = br->mcast_stats; 48921080ab95SNikolay Aleksandrov if (WARN_ON(!stats)) 48931080ab95SNikolay Aleksandrov return; 48941080ab95SNikolay Aleksandrov 48951080ab95SNikolay Aleksandrov memset(&tdst, 0, sizeof(tdst)); 48961080ab95SNikolay Aleksandrov for_each_possible_cpu(i) { 48971080ab95SNikolay Aleksandrov struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i); 48981080ab95SNikolay Aleksandrov struct br_mcast_stats temp; 48991080ab95SNikolay Aleksandrov unsigned int start; 49001080ab95SNikolay Aleksandrov 49011080ab95SNikolay Aleksandrov do { 4902d120d1a6SThomas Gleixner start = u64_stats_fetch_begin(&cpu_stats->syncp); 49031080ab95SNikolay Aleksandrov memcpy(&temp, &cpu_stats->mstats, sizeof(temp)); 4904d120d1a6SThomas Gleixner } while (u64_stats_fetch_retry(&cpu_stats->syncp, start)); 49051080ab95SNikolay Aleksandrov 4906a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries); 4907a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries); 4908a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries); 49091080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves); 49101080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports); 49111080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports); 49121080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports); 49131080ab95SNikolay Aleksandrov tdst.igmp_parse_errors += temp.igmp_parse_errors; 49141080ab95SNikolay Aleksandrov 4915a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries); 4916a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries); 49171080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves); 49181080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports); 49191080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports); 49201080ab95SNikolay Aleksandrov tdst.mld_parse_errors += temp.mld_parse_errors; 49211080ab95SNikolay Aleksandrov } 49221080ab95SNikolay Aleksandrov memcpy(dest, &tdst, sizeof(*dest)); 49231080ab95SNikolay Aleksandrov } 492419e3a9c9SNikolay Aleksandrov 492519e3a9c9SNikolay Aleksandrov int br_mdb_hash_init(struct net_bridge *br) 492619e3a9c9SNikolay Aleksandrov { 4927085b53c8SNikolay Aleksandrov int err; 4928085b53c8SNikolay Aleksandrov 4929085b53c8SNikolay Aleksandrov err = rhashtable_init(&br->sg_port_tbl, &br_sg_port_rht_params); 4930085b53c8SNikolay Aleksandrov if (err) 4931085b53c8SNikolay Aleksandrov return err; 4932085b53c8SNikolay Aleksandrov 4933085b53c8SNikolay Aleksandrov err = rhashtable_init(&br->mdb_hash_tbl, &br_mdb_rht_params); 4934085b53c8SNikolay Aleksandrov if (err) { 4935085b53c8SNikolay Aleksandrov rhashtable_destroy(&br->sg_port_tbl); 4936085b53c8SNikolay Aleksandrov return err; 4937085b53c8SNikolay Aleksandrov } 4938085b53c8SNikolay Aleksandrov 4939085b53c8SNikolay Aleksandrov return 0; 494019e3a9c9SNikolay Aleksandrov } 494119e3a9c9SNikolay Aleksandrov 494219e3a9c9SNikolay Aleksandrov void br_mdb_hash_fini(struct net_bridge *br) 494319e3a9c9SNikolay Aleksandrov { 4944085b53c8SNikolay Aleksandrov rhashtable_destroy(&br->sg_port_tbl); 494519e3a9c9SNikolay Aleksandrov rhashtable_destroy(&br->mdb_hash_tbl); 494619e3a9c9SNikolay Aleksandrov } 4947