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 85085b53c8SNikolay Aleksandrov static struct net_bridge_port_group * 86085b53c8SNikolay Aleksandrov br_sg_port_find(struct net_bridge *br, 87085b53c8SNikolay Aleksandrov struct net_bridge_port_group_sg_key *sg_p) 88085b53c8SNikolay Aleksandrov { 89085b53c8SNikolay Aleksandrov lockdep_assert_held_once(&br->multicast_lock); 90085b53c8SNikolay Aleksandrov 91085b53c8SNikolay Aleksandrov return rhashtable_lookup_fast(&br->sg_port_tbl, sg_p, 92085b53c8SNikolay Aleksandrov br_sg_port_rht_params); 93085b53c8SNikolay Aleksandrov } 94085b53c8SNikolay Aleksandrov 9519e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip_get_rcu(struct net_bridge *br, 96cfd56754SCong Wang struct br_ip *dst) 977f285fa7SHerbert Xu { 9819e3a9c9SNikolay Aleksandrov return rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params); 997f285fa7SHerbert Xu } 1007f285fa7SHerbert Xu 10119e3a9c9SNikolay Aleksandrov struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge *br, 10219e3a9c9SNikolay Aleksandrov struct br_ip *dst) 10319e3a9c9SNikolay Aleksandrov { 10419e3a9c9SNikolay Aleksandrov struct net_bridge_mdb_entry *ent; 10519e3a9c9SNikolay Aleksandrov 10619e3a9c9SNikolay Aleksandrov lockdep_assert_held_once(&br->multicast_lock); 10719e3a9c9SNikolay Aleksandrov 10819e3a9c9SNikolay Aleksandrov rcu_read_lock(); 10919e3a9c9SNikolay Aleksandrov ent = rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params); 11019e3a9c9SNikolay Aleksandrov rcu_read_unlock(); 11119e3a9c9SNikolay Aleksandrov 11219e3a9c9SNikolay Aleksandrov return ent; 11319e3a9c9SNikolay Aleksandrov } 11419e3a9c9SNikolay Aleksandrov 11519e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip4_get(struct net_bridge *br, 11619e3a9c9SNikolay Aleksandrov __be32 dst, __u16 vid) 117eb1d1641SHerbert Xu { 1188ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_dst; 1190821ec55SHerbert Xu 12019e3a9c9SNikolay Aleksandrov memset(&br_dst, 0, sizeof(br_dst)); 121eab3227bSNikolay Aleksandrov br_dst.dst.ip4 = dst; 1228ef2a9a5SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IP); 123b0e9a30dSVlad Yasevich br_dst.vid = vid; 1248ef2a9a5SYOSHIFUJI Hideaki 12519e3a9c9SNikolay Aleksandrov return br_mdb_ip_get(br, &br_dst); 1268ef2a9a5SYOSHIFUJI Hideaki } 1278ef2a9a5SYOSHIFUJI Hideaki 128dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 12919e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip6_get(struct net_bridge *br, 13019e3a9c9SNikolay Aleksandrov const struct in6_addr *dst, 131b0e9a30dSVlad Yasevich __u16 vid) 13208b202b6SYOSHIFUJI Hideaki { 13308b202b6SYOSHIFUJI Hideaki struct br_ip br_dst; 13408b202b6SYOSHIFUJI Hideaki 13519e3a9c9SNikolay Aleksandrov memset(&br_dst, 0, sizeof(br_dst)); 136eab3227bSNikolay Aleksandrov br_dst.dst.ip6 = *dst; 13708b202b6SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IPV6); 138b0e9a30dSVlad Yasevich br_dst.vid = vid; 13908b202b6SYOSHIFUJI Hideaki 14019e3a9c9SNikolay Aleksandrov return br_mdb_ip_get(br, &br_dst); 14108b202b6SYOSHIFUJI Hideaki } 14208b202b6SYOSHIFUJI Hideaki #endif 14308b202b6SYOSHIFUJI Hideaki 144adc47037SNikolay Aleksandrov struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge_mcast *brmctx, 145fbca58a2SCong Wang struct sk_buff *skb, u16 vid) 146eb1d1641SHerbert Xu { 147adc47037SNikolay Aleksandrov struct net_bridge *br = brmctx->br; 1488ef2a9a5SYOSHIFUJI Hideaki struct br_ip ip; 1498ef2a9a5SYOSHIFUJI Hideaki 1504cdd0d10SNikolay Aleksandrov if (!br_opt_get(br, BROPT_MULTICAST_ENABLED) || 1514cdd0d10SNikolay Aleksandrov br_multicast_ctx_vlan_global_disabled(brmctx)) 152eb1d1641SHerbert Xu return NULL; 153eb1d1641SHerbert Xu 1548ef2a9a5SYOSHIFUJI Hideaki if (BR_INPUT_SKB_CB(skb)->igmp) 1558ef2a9a5SYOSHIFUJI Hideaki return NULL; 1568ef2a9a5SYOSHIFUJI Hideaki 15719e3a9c9SNikolay Aleksandrov memset(&ip, 0, sizeof(ip)); 1588ef2a9a5SYOSHIFUJI Hideaki ip.proto = skb->protocol; 159fbca58a2SCong Wang ip.vid = vid; 1608ef2a9a5SYOSHIFUJI Hideaki 161eb1d1641SHerbert Xu switch (skb->protocol) { 162eb1d1641SHerbert Xu case htons(ETH_P_IP): 163eab3227bSNikolay Aleksandrov ip.dst.ip4 = ip_hdr(skb)->daddr; 164adc47037SNikolay Aleksandrov if (brmctx->multicast_igmp_version == 3) { 1657d07a68cSNikolay Aleksandrov struct net_bridge_mdb_entry *mdb; 1667d07a68cSNikolay Aleksandrov 1677d07a68cSNikolay Aleksandrov ip.src.ip4 = ip_hdr(skb)->saddr; 1687d07a68cSNikolay Aleksandrov mdb = br_mdb_ip_get_rcu(br, &ip); 1697d07a68cSNikolay Aleksandrov if (mdb) 1707d07a68cSNikolay Aleksandrov return mdb; 1717d07a68cSNikolay Aleksandrov ip.src.ip4 = 0; 1727d07a68cSNikolay Aleksandrov } 173eb1d1641SHerbert Xu break; 174dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 17508b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 176eab3227bSNikolay Aleksandrov ip.dst.ip6 = ipv6_hdr(skb)->daddr; 177adc47037SNikolay Aleksandrov if (brmctx->multicast_mld_version == 2) { 1787d07a68cSNikolay Aleksandrov struct net_bridge_mdb_entry *mdb; 1797d07a68cSNikolay Aleksandrov 1807d07a68cSNikolay Aleksandrov ip.src.ip6 = ipv6_hdr(skb)->saddr; 1817d07a68cSNikolay Aleksandrov mdb = br_mdb_ip_get_rcu(br, &ip); 1827d07a68cSNikolay Aleksandrov if (mdb) 1837d07a68cSNikolay Aleksandrov return mdb; 1847d07a68cSNikolay Aleksandrov memset(&ip.src.ip6, 0, sizeof(ip.src.ip6)); 1857d07a68cSNikolay Aleksandrov } 18608b202b6SYOSHIFUJI Hideaki break; 18708b202b6SYOSHIFUJI Hideaki #endif 1888ef2a9a5SYOSHIFUJI Hideaki default: 189955062b0SNikolay Aleksandrov ip.proto = 0; 190955062b0SNikolay Aleksandrov ether_addr_copy(ip.dst.mac_addr, eth_hdr(skb)->h_dest); 191eb1d1641SHerbert Xu } 192eb1d1641SHerbert Xu 19319e3a9c9SNikolay Aleksandrov return br_mdb_ip_get_rcu(br, &ip); 194eb1d1641SHerbert Xu } 195eb1d1641SHerbert Xu 19674edfd48SNikolay Aleksandrov /* IMPORTANT: this function must be used only when the contexts cannot be 19774edfd48SNikolay Aleksandrov * passed down (e.g. timer) and must be used for read-only purposes because 19874edfd48SNikolay Aleksandrov * the vlan snooping option can change, so it can return any context 19974edfd48SNikolay Aleksandrov * (non-vlan or vlan). Its initial intended purpose is to read timer values 20074edfd48SNikolay Aleksandrov * from the *current* context based on the option. At worst that could lead 20174edfd48SNikolay Aleksandrov * to inconsistent timers when the contexts are changed, i.e. src timer 20274edfd48SNikolay Aleksandrov * which needs to re-arm with a specific delay taken from the old context 20374edfd48SNikolay Aleksandrov */ 20474edfd48SNikolay Aleksandrov static struct net_bridge_mcast_port * 20574edfd48SNikolay Aleksandrov br_multicast_pg_to_port_ctx(const struct net_bridge_port_group *pg) 20674edfd48SNikolay Aleksandrov { 20774edfd48SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx = &pg->key.port->multicast_ctx; 20874edfd48SNikolay Aleksandrov struct net_bridge_vlan *vlan; 20974edfd48SNikolay Aleksandrov 21074edfd48SNikolay Aleksandrov lockdep_assert_held_once(&pg->key.port->br->multicast_lock); 21174edfd48SNikolay Aleksandrov 21274edfd48SNikolay Aleksandrov /* if vlan snooping is disabled use the port's multicast context */ 21374edfd48SNikolay Aleksandrov if (!pg->key.addr.vid || 21474edfd48SNikolay Aleksandrov !br_opt_get(pg->key.port->br, BROPT_MCAST_VLAN_SNOOPING_ENABLED)) 21574edfd48SNikolay Aleksandrov goto out; 21674edfd48SNikolay Aleksandrov 21774edfd48SNikolay Aleksandrov /* locking is tricky here, due to different rules for multicast and 21874edfd48SNikolay Aleksandrov * vlans we need to take rcu to find the vlan and make sure it has 21974edfd48SNikolay Aleksandrov * the BR_VLFLAG_MCAST_ENABLED flag set, it can only change under 22074edfd48SNikolay Aleksandrov * multicast_lock which must be already held here, so the vlan's pmctx 22174edfd48SNikolay Aleksandrov * can safely be used on return 22274edfd48SNikolay Aleksandrov */ 22374edfd48SNikolay Aleksandrov rcu_read_lock(); 22474edfd48SNikolay Aleksandrov vlan = br_vlan_find(nbp_vlan_group(pg->key.port), pg->key.addr.vid); 22574edfd48SNikolay Aleksandrov if (vlan && !br_multicast_port_ctx_vlan_disabled(&vlan->port_mcast_ctx)) 22674edfd48SNikolay Aleksandrov pmctx = &vlan->port_mcast_ctx; 22774edfd48SNikolay Aleksandrov else 22874edfd48SNikolay Aleksandrov pmctx = NULL; 22974edfd48SNikolay Aleksandrov rcu_read_unlock(); 23074edfd48SNikolay Aleksandrov out: 23174edfd48SNikolay Aleksandrov return pmctx; 23274edfd48SNikolay Aleksandrov } 23374edfd48SNikolay Aleksandrov 2344cdd0d10SNikolay Aleksandrov /* when snooping we need to check if the contexts should be used 2354cdd0d10SNikolay Aleksandrov * in the following order: 2364cdd0d10SNikolay Aleksandrov * - if pmctx is non-NULL (port), check if it should be used 2374cdd0d10SNikolay Aleksandrov * - if pmctx is NULL (bridge), check if brmctx should be used 2384cdd0d10SNikolay Aleksandrov */ 2394cdd0d10SNikolay Aleksandrov static bool 2404cdd0d10SNikolay Aleksandrov br_multicast_ctx_should_use(const struct net_bridge_mcast *brmctx, 2414cdd0d10SNikolay Aleksandrov const struct net_bridge_mcast_port *pmctx) 2424cdd0d10SNikolay Aleksandrov { 2434cdd0d10SNikolay Aleksandrov if (!netif_running(brmctx->br->dev)) 2444cdd0d10SNikolay Aleksandrov return false; 2454cdd0d10SNikolay Aleksandrov 2464cdd0d10SNikolay Aleksandrov if (pmctx) 2474cdd0d10SNikolay Aleksandrov return !br_multicast_port_ctx_state_disabled(pmctx); 2484cdd0d10SNikolay Aleksandrov else 2494cdd0d10SNikolay Aleksandrov return !br_multicast_ctx_vlan_disabled(brmctx); 2504cdd0d10SNikolay Aleksandrov } 2514cdd0d10SNikolay Aleksandrov 252b0812368SNikolay Aleksandrov static bool br_port_group_equal(struct net_bridge_port_group *p, 253b0812368SNikolay Aleksandrov struct net_bridge_port *port, 254b0812368SNikolay Aleksandrov const unsigned char *src) 255b0812368SNikolay Aleksandrov { 256b0812368SNikolay Aleksandrov if (p->key.port != port) 257b0812368SNikolay Aleksandrov return false; 258b0812368SNikolay Aleksandrov 259b0812368SNikolay Aleksandrov if (!(port->flags & BR_MULTICAST_TO_UNICAST)) 260b0812368SNikolay Aleksandrov return true; 261b0812368SNikolay Aleksandrov 262b0812368SNikolay Aleksandrov return ether_addr_equal(src, p->eth_addr); 263b0812368SNikolay Aleksandrov } 264b0812368SNikolay Aleksandrov 265adc47037SNikolay Aleksandrov static void __fwd_add_star_excl(struct net_bridge_mcast_port *pmctx, 266adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, 2678266a049SNikolay Aleksandrov struct br_ip *sg_ip) 2688266a049SNikolay Aleksandrov { 2698266a049SNikolay Aleksandrov struct net_bridge_port_group_sg_key sg_key; 2708266a049SNikolay Aleksandrov struct net_bridge_port_group *src_pg; 271adc47037SNikolay Aleksandrov struct net_bridge_mcast *brmctx; 2728266a049SNikolay Aleksandrov 2738266a049SNikolay Aleksandrov memset(&sg_key, 0, sizeof(sg_key)); 2747b54aaafSNikolay Aleksandrov brmctx = br_multicast_port_ctx_get_global(pmctx); 2758266a049SNikolay Aleksandrov sg_key.port = pg->key.port; 2768266a049SNikolay Aleksandrov sg_key.addr = *sg_ip; 277adc47037SNikolay Aleksandrov if (br_sg_port_find(brmctx->br, &sg_key)) 2788266a049SNikolay Aleksandrov return; 2798266a049SNikolay Aleksandrov 280adc47037SNikolay Aleksandrov src_pg = __br_multicast_add_group(brmctx, pmctx, 281adc47037SNikolay Aleksandrov sg_ip, pg->eth_addr, 2829116ffbfSNikolay Aleksandrov MCAST_INCLUDE, false, false); 2838266a049SNikolay Aleksandrov if (IS_ERR_OR_NULL(src_pg) || 2848266a049SNikolay Aleksandrov src_pg->rt_protocol != RTPROT_KERNEL) 2858266a049SNikolay Aleksandrov return; 2868266a049SNikolay Aleksandrov 2878266a049SNikolay Aleksandrov src_pg->flags |= MDB_PG_FLAGS_STAR_EXCL; 2888266a049SNikolay Aleksandrov } 2898266a049SNikolay Aleksandrov 2908266a049SNikolay Aleksandrov static void __fwd_del_star_excl(struct net_bridge_port_group *pg, 2918266a049SNikolay Aleksandrov struct br_ip *sg_ip) 2928266a049SNikolay Aleksandrov { 2938266a049SNikolay Aleksandrov struct net_bridge_port_group_sg_key sg_key; 2948266a049SNikolay Aleksandrov struct net_bridge *br = pg->key.port->br; 2958266a049SNikolay Aleksandrov struct net_bridge_port_group *src_pg; 2968266a049SNikolay Aleksandrov 2978266a049SNikolay Aleksandrov memset(&sg_key, 0, sizeof(sg_key)); 2988266a049SNikolay Aleksandrov sg_key.port = pg->key.port; 2998266a049SNikolay Aleksandrov sg_key.addr = *sg_ip; 3008266a049SNikolay Aleksandrov src_pg = br_sg_port_find(br, &sg_key); 3018266a049SNikolay Aleksandrov if (!src_pg || !(src_pg->flags & MDB_PG_FLAGS_STAR_EXCL) || 3028266a049SNikolay Aleksandrov src_pg->rt_protocol != RTPROT_KERNEL) 3038266a049SNikolay Aleksandrov return; 3048266a049SNikolay Aleksandrov 3058266a049SNikolay Aleksandrov br_multicast_find_del_pg(br, src_pg); 3068266a049SNikolay Aleksandrov } 3078266a049SNikolay Aleksandrov 3088266a049SNikolay Aleksandrov /* When a port group transitions to (or is added as) EXCLUDE we need to add it 3098266a049SNikolay Aleksandrov * to all other ports' S,G entries which are not blocked by the current group 3108266a049SNikolay Aleksandrov * for proper replication, the assumption is that any S,G blocked entries 3118266a049SNikolay Aleksandrov * are already added so the S,G,port lookup should skip them. 3128266a049SNikolay Aleksandrov * When a port group transitions from EXCLUDE -> INCLUDE mode or is being 3138266a049SNikolay Aleksandrov * deleted we need to remove it from all ports' S,G entries where it was 3148266a049SNikolay Aleksandrov * automatically installed before (i.e. where it's MDB_PG_FLAGS_STAR_EXCL). 3158266a049SNikolay Aleksandrov */ 3168266a049SNikolay Aleksandrov void br_multicast_star_g_handle_mode(struct net_bridge_port_group *pg, 3178266a049SNikolay Aleksandrov u8 filter_mode) 3188266a049SNikolay Aleksandrov { 3198266a049SNikolay Aleksandrov struct net_bridge *br = pg->key.port->br; 3208266a049SNikolay Aleksandrov struct net_bridge_port_group *pg_lst; 321adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx; 3228266a049SNikolay Aleksandrov struct net_bridge_mdb_entry *mp; 3238266a049SNikolay Aleksandrov struct br_ip sg_ip; 3248266a049SNikolay Aleksandrov 3258266a049SNikolay Aleksandrov if (WARN_ON(!br_multicast_is_star_g(&pg->key.addr))) 3268266a049SNikolay Aleksandrov return; 3278266a049SNikolay Aleksandrov 3288266a049SNikolay Aleksandrov mp = br_mdb_ip_get(br, &pg->key.addr); 3298266a049SNikolay Aleksandrov if (!mp) 3308266a049SNikolay Aleksandrov return; 331eb1593a0SNikolay Aleksandrov pmctx = br_multicast_pg_to_port_ctx(pg); 332eb1593a0SNikolay Aleksandrov if (!pmctx) 333eb1593a0SNikolay Aleksandrov return; 3348266a049SNikolay Aleksandrov 3358266a049SNikolay Aleksandrov memset(&sg_ip, 0, sizeof(sg_ip)); 3368266a049SNikolay Aleksandrov sg_ip = pg->key.addr; 3377b54aaafSNikolay Aleksandrov 3388266a049SNikolay Aleksandrov for (pg_lst = mlock_dereference(mp->ports, br); 3398266a049SNikolay Aleksandrov pg_lst; 3408266a049SNikolay Aleksandrov pg_lst = mlock_dereference(pg_lst->next, br)) { 3418266a049SNikolay Aleksandrov struct net_bridge_group_src *src_ent; 3428266a049SNikolay Aleksandrov 3438266a049SNikolay Aleksandrov if (pg_lst == pg) 3448266a049SNikolay Aleksandrov continue; 3458266a049SNikolay Aleksandrov hlist_for_each_entry(src_ent, &pg_lst->src_list, node) { 3468266a049SNikolay Aleksandrov if (!(src_ent->flags & BR_SGRP_F_INSTALLED)) 3478266a049SNikolay Aleksandrov continue; 3488266a049SNikolay Aleksandrov sg_ip.src = src_ent->addr.src; 3498266a049SNikolay Aleksandrov switch (filter_mode) { 3508266a049SNikolay Aleksandrov case MCAST_INCLUDE: 3518266a049SNikolay Aleksandrov __fwd_del_star_excl(pg, &sg_ip); 3528266a049SNikolay Aleksandrov break; 3538266a049SNikolay Aleksandrov case MCAST_EXCLUDE: 354adc47037SNikolay Aleksandrov __fwd_add_star_excl(pmctx, pg, &sg_ip); 3558266a049SNikolay Aleksandrov break; 3568266a049SNikolay Aleksandrov } 3578266a049SNikolay Aleksandrov } 3588266a049SNikolay Aleksandrov } 3598266a049SNikolay Aleksandrov } 3608266a049SNikolay Aleksandrov 361094b82fdSNikolay Aleksandrov /* called when adding a new S,G with host_joined == false by default */ 362094b82fdSNikolay Aleksandrov static void br_multicast_sg_host_state(struct net_bridge_mdb_entry *star_mp, 363094b82fdSNikolay Aleksandrov struct net_bridge_port_group *sg) 364094b82fdSNikolay Aleksandrov { 365094b82fdSNikolay Aleksandrov struct net_bridge_mdb_entry *sg_mp; 366094b82fdSNikolay Aleksandrov 367094b82fdSNikolay Aleksandrov if (WARN_ON(!br_multicast_is_star_g(&star_mp->addr))) 368094b82fdSNikolay Aleksandrov return; 369094b82fdSNikolay Aleksandrov if (!star_mp->host_joined) 370094b82fdSNikolay Aleksandrov return; 371094b82fdSNikolay Aleksandrov 372094b82fdSNikolay Aleksandrov sg_mp = br_mdb_ip_get(star_mp->br, &sg->key.addr); 373094b82fdSNikolay Aleksandrov if (!sg_mp) 374094b82fdSNikolay Aleksandrov return; 375094b82fdSNikolay Aleksandrov sg_mp->host_joined = true; 376094b82fdSNikolay Aleksandrov } 377094b82fdSNikolay Aleksandrov 378094b82fdSNikolay Aleksandrov /* set the host_joined state of all of *,G's S,G entries */ 379094b82fdSNikolay Aleksandrov static void br_multicast_star_g_host_state(struct net_bridge_mdb_entry *star_mp) 380094b82fdSNikolay Aleksandrov { 381094b82fdSNikolay Aleksandrov struct net_bridge *br = star_mp->br; 382094b82fdSNikolay Aleksandrov struct net_bridge_mdb_entry *sg_mp; 383094b82fdSNikolay Aleksandrov struct net_bridge_port_group *pg; 384094b82fdSNikolay Aleksandrov struct br_ip sg_ip; 385094b82fdSNikolay Aleksandrov 386094b82fdSNikolay Aleksandrov if (WARN_ON(!br_multicast_is_star_g(&star_mp->addr))) 387094b82fdSNikolay Aleksandrov return; 388094b82fdSNikolay Aleksandrov 389094b82fdSNikolay Aleksandrov memset(&sg_ip, 0, sizeof(sg_ip)); 390094b82fdSNikolay Aleksandrov sg_ip = star_mp->addr; 391094b82fdSNikolay Aleksandrov for (pg = mlock_dereference(star_mp->ports, br); 392094b82fdSNikolay Aleksandrov pg; 393094b82fdSNikolay Aleksandrov pg = mlock_dereference(pg->next, br)) { 394094b82fdSNikolay Aleksandrov struct net_bridge_group_src *src_ent; 395094b82fdSNikolay Aleksandrov 396094b82fdSNikolay Aleksandrov hlist_for_each_entry(src_ent, &pg->src_list, node) { 397094b82fdSNikolay Aleksandrov if (!(src_ent->flags & BR_SGRP_F_INSTALLED)) 398094b82fdSNikolay Aleksandrov continue; 399094b82fdSNikolay Aleksandrov sg_ip.src = src_ent->addr.src; 400094b82fdSNikolay Aleksandrov sg_mp = br_mdb_ip_get(br, &sg_ip); 401094b82fdSNikolay Aleksandrov if (!sg_mp) 402094b82fdSNikolay Aleksandrov continue; 403094b82fdSNikolay Aleksandrov sg_mp->host_joined = star_mp->host_joined; 404094b82fdSNikolay Aleksandrov } 405094b82fdSNikolay Aleksandrov } 406094b82fdSNikolay Aleksandrov } 407094b82fdSNikolay Aleksandrov 4088266a049SNikolay Aleksandrov static void br_multicast_sg_del_exclude_ports(struct net_bridge_mdb_entry *sgmp) 4098266a049SNikolay Aleksandrov { 4108266a049SNikolay Aleksandrov struct net_bridge_port_group __rcu **pp; 4118266a049SNikolay Aleksandrov struct net_bridge_port_group *p; 4128266a049SNikolay Aleksandrov 4138266a049SNikolay Aleksandrov /* *,G exclude ports are only added to S,G entries */ 4148266a049SNikolay Aleksandrov if (WARN_ON(br_multicast_is_star_g(&sgmp->addr))) 4158266a049SNikolay Aleksandrov return; 4168266a049SNikolay Aleksandrov 4178266a049SNikolay Aleksandrov /* we need the STAR_EXCLUDE ports if there are non-STAR_EXCLUDE ports 4188266a049SNikolay Aleksandrov * we should ignore perm entries since they're managed by user-space 4198266a049SNikolay Aleksandrov */ 4208266a049SNikolay Aleksandrov for (pp = &sgmp->ports; 4218266a049SNikolay Aleksandrov (p = mlock_dereference(*pp, sgmp->br)) != NULL; 4228266a049SNikolay Aleksandrov pp = &p->next) 4238266a049SNikolay Aleksandrov if (!(p->flags & (MDB_PG_FLAGS_STAR_EXCL | 4248266a049SNikolay Aleksandrov MDB_PG_FLAGS_PERMANENT))) 4258266a049SNikolay Aleksandrov return; 4268266a049SNikolay Aleksandrov 427094b82fdSNikolay Aleksandrov /* currently the host can only have joined the *,G which means 428094b82fdSNikolay Aleksandrov * we treat it as EXCLUDE {}, so for an S,G it's considered a 429094b82fdSNikolay Aleksandrov * STAR_EXCLUDE entry and we can safely leave it 430094b82fdSNikolay Aleksandrov */ 431094b82fdSNikolay Aleksandrov sgmp->host_joined = false; 432094b82fdSNikolay Aleksandrov 4338266a049SNikolay Aleksandrov for (pp = &sgmp->ports; 4348266a049SNikolay Aleksandrov (p = mlock_dereference(*pp, sgmp->br)) != NULL;) { 4358266a049SNikolay Aleksandrov if (!(p->flags & MDB_PG_FLAGS_PERMANENT)) 4368266a049SNikolay Aleksandrov br_multicast_del_pg(sgmp, p, pp); 4378266a049SNikolay Aleksandrov else 4388266a049SNikolay Aleksandrov pp = &p->next; 4398266a049SNikolay Aleksandrov } 4408266a049SNikolay Aleksandrov } 4418266a049SNikolay Aleksandrov 4428266a049SNikolay Aleksandrov void br_multicast_sg_add_exclude_ports(struct net_bridge_mdb_entry *star_mp, 4438266a049SNikolay Aleksandrov struct net_bridge_port_group *sg) 4448266a049SNikolay Aleksandrov { 4458266a049SNikolay Aleksandrov struct net_bridge_port_group_sg_key sg_key; 4468266a049SNikolay Aleksandrov struct net_bridge *br = star_mp->br; 447adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx; 4488266a049SNikolay Aleksandrov struct net_bridge_port_group *pg; 449adc47037SNikolay Aleksandrov struct net_bridge_mcast *brmctx; 4508266a049SNikolay Aleksandrov 4518266a049SNikolay Aleksandrov if (WARN_ON(br_multicast_is_star_g(&sg->key.addr))) 4528266a049SNikolay Aleksandrov return; 4538266a049SNikolay Aleksandrov if (WARN_ON(!br_multicast_is_star_g(&star_mp->addr))) 4548266a049SNikolay Aleksandrov return; 4558266a049SNikolay Aleksandrov 456094b82fdSNikolay Aleksandrov br_multicast_sg_host_state(star_mp, sg); 4578266a049SNikolay Aleksandrov memset(&sg_key, 0, sizeof(sg_key)); 4588266a049SNikolay Aleksandrov sg_key.addr = sg->key.addr; 4598266a049SNikolay Aleksandrov /* we need to add all exclude ports to the S,G */ 4608266a049SNikolay Aleksandrov for (pg = mlock_dereference(star_mp->ports, br); 4618266a049SNikolay Aleksandrov pg; 4628266a049SNikolay Aleksandrov pg = mlock_dereference(pg->next, br)) { 4638266a049SNikolay Aleksandrov struct net_bridge_port_group *src_pg; 4648266a049SNikolay Aleksandrov 4658266a049SNikolay Aleksandrov if (pg == sg || pg->filter_mode == MCAST_INCLUDE) 4668266a049SNikolay Aleksandrov continue; 4678266a049SNikolay Aleksandrov 4688266a049SNikolay Aleksandrov sg_key.port = pg->key.port; 4698266a049SNikolay Aleksandrov if (br_sg_port_find(br, &sg_key)) 4708266a049SNikolay Aleksandrov continue; 4718266a049SNikolay Aleksandrov 472eb1593a0SNikolay Aleksandrov pmctx = br_multicast_pg_to_port_ctx(pg); 473eb1593a0SNikolay Aleksandrov if (!pmctx) 474eb1593a0SNikolay Aleksandrov continue; 475eb1593a0SNikolay Aleksandrov brmctx = br_multicast_port_ctx_get_global(pmctx); 476eb1593a0SNikolay Aleksandrov 477adc47037SNikolay Aleksandrov src_pg = __br_multicast_add_group(brmctx, pmctx, 4788266a049SNikolay Aleksandrov &sg->key.addr, 4798266a049SNikolay Aleksandrov sg->eth_addr, 4809116ffbfSNikolay Aleksandrov MCAST_INCLUDE, false, false); 4818266a049SNikolay Aleksandrov if (IS_ERR_OR_NULL(src_pg) || 4828266a049SNikolay Aleksandrov src_pg->rt_protocol != RTPROT_KERNEL) 4838266a049SNikolay Aleksandrov continue; 4848266a049SNikolay Aleksandrov src_pg->flags |= MDB_PG_FLAGS_STAR_EXCL; 4858266a049SNikolay Aleksandrov } 4868266a049SNikolay Aleksandrov } 4878266a049SNikolay Aleksandrov 488b0812368SNikolay Aleksandrov static void br_multicast_fwd_src_add(struct net_bridge_group_src *src) 489b0812368SNikolay Aleksandrov { 4908266a049SNikolay Aleksandrov struct net_bridge_mdb_entry *star_mp; 491adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx; 492b0812368SNikolay Aleksandrov struct net_bridge_port_group *sg; 493adc47037SNikolay Aleksandrov struct net_bridge_mcast *brmctx; 494b0812368SNikolay Aleksandrov struct br_ip sg_ip; 495b0812368SNikolay Aleksandrov 496b0812368SNikolay Aleksandrov if (src->flags & BR_SGRP_F_INSTALLED) 497b0812368SNikolay Aleksandrov return; 498b0812368SNikolay Aleksandrov 499b0812368SNikolay Aleksandrov memset(&sg_ip, 0, sizeof(sg_ip)); 500eb1593a0SNikolay Aleksandrov pmctx = br_multicast_pg_to_port_ctx(src->pg); 501eb1593a0SNikolay Aleksandrov if (!pmctx) 502eb1593a0SNikolay Aleksandrov return; 5037b54aaafSNikolay Aleksandrov brmctx = br_multicast_port_ctx_get_global(pmctx); 504b0812368SNikolay Aleksandrov sg_ip = src->pg->key.addr; 505b0812368SNikolay Aleksandrov sg_ip.src = src->addr.src; 506adc47037SNikolay Aleksandrov 507adc47037SNikolay Aleksandrov sg = __br_multicast_add_group(brmctx, pmctx, &sg_ip, 5089116ffbfSNikolay Aleksandrov src->pg->eth_addr, MCAST_INCLUDE, false, 5099116ffbfSNikolay Aleksandrov !timer_pending(&src->timer)); 510b0812368SNikolay Aleksandrov if (IS_ERR_OR_NULL(sg)) 511b0812368SNikolay Aleksandrov return; 512b0812368SNikolay Aleksandrov src->flags |= BR_SGRP_F_INSTALLED; 5138266a049SNikolay Aleksandrov sg->flags &= ~MDB_PG_FLAGS_STAR_EXCL; 514b0812368SNikolay Aleksandrov 515b0812368SNikolay Aleksandrov /* if it was added by user-space as perm we can skip next steps */ 516b0812368SNikolay Aleksandrov if (sg->rt_protocol != RTPROT_KERNEL && 517b0812368SNikolay Aleksandrov (sg->flags & MDB_PG_FLAGS_PERMANENT)) 518b0812368SNikolay Aleksandrov return; 519b0812368SNikolay Aleksandrov 520b0812368SNikolay Aleksandrov /* the kernel is now responsible for removing this S,G */ 521b0812368SNikolay Aleksandrov del_timer(&sg->timer); 5228266a049SNikolay Aleksandrov star_mp = br_mdb_ip_get(src->br, &src->pg->key.addr); 5238266a049SNikolay Aleksandrov if (!star_mp) 5248266a049SNikolay Aleksandrov return; 5258266a049SNikolay Aleksandrov 5268266a049SNikolay Aleksandrov br_multicast_sg_add_exclude_ports(star_mp, sg); 527b0812368SNikolay Aleksandrov } 528b0812368SNikolay Aleksandrov 529d5a10222SNikolay Aleksandrov static void br_multicast_fwd_src_remove(struct net_bridge_group_src *src, 530d5a10222SNikolay Aleksandrov bool fastleave) 531b0812368SNikolay Aleksandrov { 532b0812368SNikolay Aleksandrov struct net_bridge_port_group *p, *pg = src->pg; 533b0812368SNikolay Aleksandrov struct net_bridge_port_group __rcu **pp; 534b0812368SNikolay Aleksandrov struct net_bridge_mdb_entry *mp; 535b0812368SNikolay Aleksandrov struct br_ip sg_ip; 536b0812368SNikolay Aleksandrov 537b0812368SNikolay Aleksandrov memset(&sg_ip, 0, sizeof(sg_ip)); 538b0812368SNikolay Aleksandrov sg_ip = pg->key.addr; 539b0812368SNikolay Aleksandrov sg_ip.src = src->addr.src; 540b0812368SNikolay Aleksandrov 541b0812368SNikolay Aleksandrov mp = br_mdb_ip_get(src->br, &sg_ip); 542b0812368SNikolay Aleksandrov if (!mp) 543b0812368SNikolay Aleksandrov return; 544b0812368SNikolay Aleksandrov 545b0812368SNikolay Aleksandrov for (pp = &mp->ports; 546b0812368SNikolay Aleksandrov (p = mlock_dereference(*pp, src->br)) != NULL; 547b0812368SNikolay Aleksandrov pp = &p->next) { 548b0812368SNikolay Aleksandrov if (!br_port_group_equal(p, pg->key.port, pg->eth_addr)) 549b0812368SNikolay Aleksandrov continue; 550b0812368SNikolay Aleksandrov 551b0812368SNikolay Aleksandrov if (p->rt_protocol != RTPROT_KERNEL && 552b0812368SNikolay Aleksandrov (p->flags & MDB_PG_FLAGS_PERMANENT)) 553b0812368SNikolay Aleksandrov break; 554b0812368SNikolay Aleksandrov 555d5a10222SNikolay Aleksandrov if (fastleave) 556d5a10222SNikolay Aleksandrov p->flags |= MDB_PG_FLAGS_FAST_LEAVE; 557b0812368SNikolay Aleksandrov br_multicast_del_pg(mp, p, pp); 558b0812368SNikolay Aleksandrov break; 559b0812368SNikolay Aleksandrov } 560b0812368SNikolay Aleksandrov src->flags &= ~BR_SGRP_F_INSTALLED; 561b0812368SNikolay Aleksandrov } 562b0812368SNikolay Aleksandrov 5639116ffbfSNikolay Aleksandrov /* install S,G and based on src's timer enable or disable forwarding */ 564b0812368SNikolay Aleksandrov static void br_multicast_fwd_src_handle(struct net_bridge_group_src *src) 565b0812368SNikolay Aleksandrov { 5669116ffbfSNikolay Aleksandrov struct net_bridge_port_group_sg_key sg_key; 5679116ffbfSNikolay Aleksandrov struct net_bridge_port_group *sg; 5689116ffbfSNikolay Aleksandrov u8 old_flags; 5699116ffbfSNikolay Aleksandrov 570b0812368SNikolay Aleksandrov br_multicast_fwd_src_add(src); 5719116ffbfSNikolay Aleksandrov 5729116ffbfSNikolay Aleksandrov memset(&sg_key, 0, sizeof(sg_key)); 5739116ffbfSNikolay Aleksandrov sg_key.addr = src->pg->key.addr; 5749116ffbfSNikolay Aleksandrov sg_key.addr.src = src->addr.src; 5759116ffbfSNikolay Aleksandrov sg_key.port = src->pg->key.port; 5769116ffbfSNikolay Aleksandrov 5779116ffbfSNikolay Aleksandrov sg = br_sg_port_find(src->br, &sg_key); 5789116ffbfSNikolay Aleksandrov if (!sg || (sg->flags & MDB_PG_FLAGS_PERMANENT)) 5799116ffbfSNikolay Aleksandrov return; 5809116ffbfSNikolay Aleksandrov 5819116ffbfSNikolay Aleksandrov old_flags = sg->flags; 5829116ffbfSNikolay Aleksandrov if (timer_pending(&src->timer)) 5839116ffbfSNikolay Aleksandrov sg->flags &= ~MDB_PG_FLAGS_BLOCKED; 5849116ffbfSNikolay Aleksandrov else 5859116ffbfSNikolay Aleksandrov sg->flags |= MDB_PG_FLAGS_BLOCKED; 5869116ffbfSNikolay Aleksandrov 5879116ffbfSNikolay Aleksandrov if (old_flags != sg->flags) { 5889116ffbfSNikolay Aleksandrov struct net_bridge_mdb_entry *sg_mp; 5899116ffbfSNikolay Aleksandrov 5909116ffbfSNikolay Aleksandrov sg_mp = br_mdb_ip_get(src->br, &sg_key.addr); 5919116ffbfSNikolay Aleksandrov if (!sg_mp) 5929116ffbfSNikolay Aleksandrov return; 5939116ffbfSNikolay Aleksandrov br_mdb_notify(src->br->dev, sg_mp, sg, RTM_NEWMDB); 5949116ffbfSNikolay Aleksandrov } 595b0812368SNikolay Aleksandrov } 596b0812368SNikolay Aleksandrov 597e12cec65SNikolay Aleksandrov static void br_multicast_destroy_mdb_entry(struct net_bridge_mcast_gc *gc) 598e12cec65SNikolay Aleksandrov { 599e12cec65SNikolay Aleksandrov struct net_bridge_mdb_entry *mp; 600e12cec65SNikolay Aleksandrov 601e12cec65SNikolay Aleksandrov mp = container_of(gc, struct net_bridge_mdb_entry, mcast_gc); 602e12cec65SNikolay Aleksandrov WARN_ON(!hlist_unhashed(&mp->mdb_node)); 603e12cec65SNikolay Aleksandrov WARN_ON(mp->ports); 604e12cec65SNikolay Aleksandrov 605e12cec65SNikolay Aleksandrov del_timer_sync(&mp->timer); 606e12cec65SNikolay Aleksandrov kfree_rcu(mp, rcu); 607e12cec65SNikolay Aleksandrov } 608e12cec65SNikolay Aleksandrov 609e12cec65SNikolay Aleksandrov static void br_multicast_del_mdb_entry(struct net_bridge_mdb_entry *mp) 610e12cec65SNikolay Aleksandrov { 611e12cec65SNikolay Aleksandrov struct net_bridge *br = mp->br; 612e12cec65SNikolay Aleksandrov 613e12cec65SNikolay Aleksandrov rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode, 614e12cec65SNikolay Aleksandrov br_mdb_rht_params); 615e12cec65SNikolay Aleksandrov hlist_del_init_rcu(&mp->mdb_node); 616e12cec65SNikolay Aleksandrov hlist_add_head(&mp->mcast_gc.gc_node, &br->mcast_gc_list); 617e12cec65SNikolay Aleksandrov queue_work(system_long_wq, &br->mcast_gc_work); 618e12cec65SNikolay Aleksandrov } 619e12cec65SNikolay Aleksandrov 62088c1f37fSAllen Pais static void br_multicast_group_expired(struct timer_list *t) 621eb1d1641SHerbert Xu { 62288c1f37fSAllen Pais struct net_bridge_mdb_entry *mp = from_timer(mp, t, timer); 623eb1d1641SHerbert Xu struct net_bridge *br = mp->br; 624eb1d1641SHerbert Xu 625eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 626b0812368SNikolay Aleksandrov if (hlist_unhashed(&mp->mdb_node) || !netif_running(br->dev) || 627b0812368SNikolay Aleksandrov timer_pending(&mp->timer)) 628eb1d1641SHerbert Xu goto out; 629eb1d1641SHerbert Xu 6301bc844eeSNikolay Aleksandrov br_multicast_host_leave(mp, true); 631eb1d1641SHerbert Xu 632eb1d1641SHerbert Xu if (mp->ports) 633eb1d1641SHerbert Xu goto out; 634e12cec65SNikolay Aleksandrov br_multicast_del_mdb_entry(mp); 635eb1d1641SHerbert Xu out: 636eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 637eb1d1641SHerbert Xu } 638eb1d1641SHerbert Xu 639e12cec65SNikolay Aleksandrov static void br_multicast_destroy_group_src(struct net_bridge_mcast_gc *gc) 640e12cec65SNikolay Aleksandrov { 641e12cec65SNikolay Aleksandrov struct net_bridge_group_src *src; 642e12cec65SNikolay Aleksandrov 643e12cec65SNikolay Aleksandrov src = container_of(gc, struct net_bridge_group_src, mcast_gc); 644e12cec65SNikolay Aleksandrov WARN_ON(!hlist_unhashed(&src->node)); 645e12cec65SNikolay Aleksandrov 646e12cec65SNikolay Aleksandrov del_timer_sync(&src->timer); 647e12cec65SNikolay Aleksandrov kfree_rcu(src, rcu); 648e12cec65SNikolay Aleksandrov } 649e12cec65SNikolay Aleksandrov 650d5a10222SNikolay Aleksandrov void br_multicast_del_group_src(struct net_bridge_group_src *src, 651d5a10222SNikolay Aleksandrov bool fastleave) 6528b671779SNikolay Aleksandrov { 653085b53c8SNikolay Aleksandrov struct net_bridge *br = src->pg->key.port->br; 6548b671779SNikolay Aleksandrov 655d5a10222SNikolay Aleksandrov br_multicast_fwd_src_remove(src, fastleave); 6568b671779SNikolay Aleksandrov hlist_del_init_rcu(&src->node); 6578b671779SNikolay Aleksandrov src->pg->src_ents--; 658e12cec65SNikolay Aleksandrov hlist_add_head(&src->mcast_gc.gc_node, &br->mcast_gc_list); 659e12cec65SNikolay Aleksandrov queue_work(system_long_wq, &br->mcast_gc_work); 660e12cec65SNikolay Aleksandrov } 661e12cec65SNikolay Aleksandrov 662e12cec65SNikolay Aleksandrov static void br_multicast_destroy_port_group(struct net_bridge_mcast_gc *gc) 663e12cec65SNikolay Aleksandrov { 664e12cec65SNikolay Aleksandrov struct net_bridge_port_group *pg; 665e12cec65SNikolay Aleksandrov 666e12cec65SNikolay Aleksandrov pg = container_of(gc, struct net_bridge_port_group, mcast_gc); 667e12cec65SNikolay Aleksandrov WARN_ON(!hlist_unhashed(&pg->mglist)); 668e12cec65SNikolay Aleksandrov WARN_ON(!hlist_empty(&pg->src_list)); 669e12cec65SNikolay Aleksandrov 670e12cec65SNikolay Aleksandrov del_timer_sync(&pg->rexmit_timer); 671e12cec65SNikolay Aleksandrov del_timer_sync(&pg->timer); 672e12cec65SNikolay Aleksandrov kfree_rcu(pg, rcu); 6738b671779SNikolay Aleksandrov } 6748b671779SNikolay Aleksandrov 675681590bdSNikolay Aleksandrov void br_multicast_del_pg(struct net_bridge_mdb_entry *mp, 676681590bdSNikolay Aleksandrov struct net_bridge_port_group *pg, 677681590bdSNikolay Aleksandrov struct net_bridge_port_group __rcu **pp) 678681590bdSNikolay Aleksandrov { 679085b53c8SNikolay Aleksandrov struct net_bridge *br = pg->key.port->br; 6808b671779SNikolay Aleksandrov struct net_bridge_group_src *ent; 6818b671779SNikolay Aleksandrov struct hlist_node *tmp; 682681590bdSNikolay Aleksandrov 683681590bdSNikolay Aleksandrov rcu_assign_pointer(*pp, pg->next); 684681590bdSNikolay Aleksandrov hlist_del_init(&pg->mglist); 685baa74d39SNikolay Aleksandrov br_multicast_eht_clean_sets(pg); 6868b671779SNikolay Aleksandrov hlist_for_each_entry_safe(ent, tmp, &pg->src_list, node) 687d5a10222SNikolay Aleksandrov br_multicast_del_group_src(ent, false); 68881f19838SNikolay Aleksandrov br_mdb_notify(br->dev, mp, pg, RTM_DELMDB); 68974705582SNikolay Aleksandrov if (!br_multicast_is_star_g(&mp->addr)) { 69074705582SNikolay Aleksandrov rhashtable_remove_fast(&br->sg_port_tbl, &pg->rhnode, 69174705582SNikolay Aleksandrov br_sg_port_rht_params); 6928266a049SNikolay Aleksandrov br_multicast_sg_del_exclude_ports(mp); 69374705582SNikolay Aleksandrov } else { 6948266a049SNikolay Aleksandrov br_multicast_star_g_handle_mode(pg, MCAST_INCLUDE); 69574705582SNikolay Aleksandrov } 696e12cec65SNikolay Aleksandrov hlist_add_head(&pg->mcast_gc.gc_node, &br->mcast_gc_list); 697e12cec65SNikolay Aleksandrov queue_work(system_long_wq, &br->mcast_gc_work); 698681590bdSNikolay Aleksandrov 699681590bdSNikolay Aleksandrov if (!mp->ports && !mp->host_joined && netif_running(br->dev)) 700681590bdSNikolay Aleksandrov mod_timer(&mp->timer, jiffies); 701681590bdSNikolay Aleksandrov } 702681590bdSNikolay Aleksandrov 703681590bdSNikolay Aleksandrov static void br_multicast_find_del_pg(struct net_bridge *br, 704eb1d1641SHerbert Xu struct net_bridge_port_group *pg) 705eb1d1641SHerbert Xu { 7068b671779SNikolay Aleksandrov struct net_bridge_port_group __rcu **pp; 707eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 708eb1d1641SHerbert Xu struct net_bridge_port_group *p; 709e8051688SEric Dumazet 710085b53c8SNikolay Aleksandrov mp = br_mdb_ip_get(br, &pg->key.addr); 711eb1d1641SHerbert Xu if (WARN_ON(!mp)) 712eb1d1641SHerbert Xu return; 713eb1d1641SHerbert Xu 714e8051688SEric Dumazet for (pp = &mp->ports; 715e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 716e8051688SEric Dumazet pp = &p->next) { 717eb1d1641SHerbert Xu if (p != pg) 718eb1d1641SHerbert Xu continue; 719eb1d1641SHerbert Xu 720681590bdSNikolay Aleksandrov br_multicast_del_pg(mp, pg, pp); 721eb1d1641SHerbert Xu return; 722eb1d1641SHerbert Xu } 723eb1d1641SHerbert Xu 724eb1d1641SHerbert Xu WARN_ON(1); 725eb1d1641SHerbert Xu } 726eb1d1641SHerbert Xu 72788c1f37fSAllen Pais static void br_multicast_port_group_expired(struct timer_list *t) 728eb1d1641SHerbert Xu { 72988c1f37fSAllen Pais struct net_bridge_port_group *pg = from_timer(pg, t, timer); 730d6c33d67SNikolay Aleksandrov struct net_bridge_group_src *src_ent; 731085b53c8SNikolay Aleksandrov struct net_bridge *br = pg->key.port->br; 732d6c33d67SNikolay Aleksandrov struct hlist_node *tmp; 733d6c33d67SNikolay Aleksandrov bool changed; 734eb1d1641SHerbert Xu 735eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 736eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&pg->timer) || 7379d06b6d8SElad Raz hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT) 738eb1d1641SHerbert Xu goto out; 739eb1d1641SHerbert Xu 740d6c33d67SNikolay Aleksandrov changed = !!(pg->filter_mode == MCAST_EXCLUDE); 741d6c33d67SNikolay Aleksandrov pg->filter_mode = MCAST_INCLUDE; 742d6c33d67SNikolay Aleksandrov hlist_for_each_entry_safe(src_ent, tmp, &pg->src_list, node) { 743d6c33d67SNikolay Aleksandrov if (!timer_pending(&src_ent->timer)) { 744d5a10222SNikolay Aleksandrov br_multicast_del_group_src(src_ent, false); 745d6c33d67SNikolay Aleksandrov changed = true; 746d6c33d67SNikolay Aleksandrov } 747d6c33d67SNikolay Aleksandrov } 748eb1d1641SHerbert Xu 749d6c33d67SNikolay Aleksandrov if (hlist_empty(&pg->src_list)) { 750d6c33d67SNikolay Aleksandrov br_multicast_find_del_pg(br, pg); 751d6c33d67SNikolay Aleksandrov } else if (changed) { 752085b53c8SNikolay Aleksandrov struct net_bridge_mdb_entry *mp = br_mdb_ip_get(br, &pg->key.addr); 753d6c33d67SNikolay Aleksandrov 7548266a049SNikolay Aleksandrov if (changed && br_multicast_is_star_g(&pg->key.addr)) 7558266a049SNikolay Aleksandrov br_multicast_star_g_handle_mode(pg, MCAST_INCLUDE); 7568266a049SNikolay Aleksandrov 757d6c33d67SNikolay Aleksandrov if (WARN_ON(!mp)) 758d6c33d67SNikolay Aleksandrov goto out; 759d6c33d67SNikolay Aleksandrov br_mdb_notify(br->dev, mp, pg, RTM_NEWMDB); 760d6c33d67SNikolay Aleksandrov } 761eb1d1641SHerbert Xu out: 762eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 763eb1d1641SHerbert Xu } 764eb1d1641SHerbert Xu 765e12cec65SNikolay Aleksandrov static void br_multicast_gc(struct hlist_head *head) 766e12cec65SNikolay Aleksandrov { 767e12cec65SNikolay Aleksandrov struct net_bridge_mcast_gc *gcent; 768e12cec65SNikolay Aleksandrov struct hlist_node *tmp; 769e12cec65SNikolay Aleksandrov 770e12cec65SNikolay Aleksandrov hlist_for_each_entry_safe(gcent, tmp, head, gc_node) { 771e12cec65SNikolay Aleksandrov hlist_del_init(&gcent->gc_node); 772e12cec65SNikolay Aleksandrov gcent->destroy(gcent); 773e12cec65SNikolay Aleksandrov } 774e12cec65SNikolay Aleksandrov } 775e12cec65SNikolay Aleksandrov 776615cc23eSNikolay Aleksandrov static void __br_multicast_query_handle_vlan(struct net_bridge_mcast *brmctx, 777615cc23eSNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 778615cc23eSNikolay Aleksandrov struct sk_buff *skb) 779615cc23eSNikolay Aleksandrov { 780615cc23eSNikolay Aleksandrov struct net_bridge_vlan *vlan = NULL; 781615cc23eSNikolay Aleksandrov 782615cc23eSNikolay Aleksandrov if (pmctx && br_multicast_port_ctx_is_vlan(pmctx)) 783615cc23eSNikolay Aleksandrov vlan = pmctx->vlan; 784615cc23eSNikolay Aleksandrov else if (br_multicast_ctx_is_vlan(brmctx)) 785615cc23eSNikolay Aleksandrov vlan = brmctx->vlan; 786615cc23eSNikolay Aleksandrov 787615cc23eSNikolay Aleksandrov if (vlan && !(vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED)) { 788615cc23eSNikolay Aleksandrov u16 vlan_proto; 789615cc23eSNikolay Aleksandrov 790615cc23eSNikolay Aleksandrov if (br_vlan_get_proto(brmctx->br->dev, &vlan_proto) != 0) 791615cc23eSNikolay Aleksandrov return; 792615cc23eSNikolay Aleksandrov __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vlan->vid); 793615cc23eSNikolay Aleksandrov } 794615cc23eSNikolay Aleksandrov } 795615cc23eSNikolay Aleksandrov 796adc47037SNikolay Aleksandrov static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge_mcast *brmctx, 797615cc23eSNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 798438ef2d0SNikolay Aleksandrov struct net_bridge_port_group *pg, 799438ef2d0SNikolay Aleksandrov __be32 ip_dst, __be32 group, 800438ef2d0SNikolay Aleksandrov bool with_srcs, bool over_lmqt, 80142c11ccfSNikolay Aleksandrov u8 sflag, u8 *igmp_type, 80242c11ccfSNikolay Aleksandrov bool *need_rexmit) 803eb1d1641SHerbert Xu { 804085b53c8SNikolay Aleksandrov struct net_bridge_port *p = pg ? pg->key.port : NULL; 805438ef2d0SNikolay Aleksandrov struct net_bridge_group_src *ent; 806438ef2d0SNikolay Aleksandrov size_t pkt_size, igmp_hdr_size; 807438ef2d0SNikolay Aleksandrov unsigned long now = jiffies; 8085e923585SNikolay Aleksandrov struct igmpv3_query *ihv3; 809438ef2d0SNikolay Aleksandrov void *csum_start = NULL; 810438ef2d0SNikolay Aleksandrov __sum16 *csum = NULL; 811eb1d1641SHerbert Xu struct sk_buff *skb; 812eb1d1641SHerbert Xu struct igmphdr *ih; 813eb1d1641SHerbert Xu struct ethhdr *eth; 814438ef2d0SNikolay Aleksandrov unsigned long lmqt; 815eb1d1641SHerbert Xu struct iphdr *iph; 816438ef2d0SNikolay Aleksandrov u16 lmqt_srcs = 0; 817eb1d1641SHerbert Xu 8185e923585SNikolay Aleksandrov igmp_hdr_size = sizeof(*ih); 819d3d065c0SNikolay Aleksandrov if (brmctx->multicast_igmp_version == 3) { 8205e923585SNikolay Aleksandrov igmp_hdr_size = sizeof(*ihv3); 821438ef2d0SNikolay Aleksandrov if (pg && with_srcs) { 822d3d065c0SNikolay Aleksandrov lmqt = now + (brmctx->multicast_last_member_interval * 823d3d065c0SNikolay Aleksandrov brmctx->multicast_last_member_count); 824438ef2d0SNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) { 825438ef2d0SNikolay Aleksandrov if (over_lmqt == time_after(ent->timer.expires, 826438ef2d0SNikolay Aleksandrov lmqt) && 827438ef2d0SNikolay Aleksandrov ent->src_query_rexmit_cnt > 0) 828438ef2d0SNikolay Aleksandrov lmqt_srcs++; 829438ef2d0SNikolay Aleksandrov } 830438ef2d0SNikolay Aleksandrov 831438ef2d0SNikolay Aleksandrov if (!lmqt_srcs) 832438ef2d0SNikolay Aleksandrov return NULL; 833438ef2d0SNikolay Aleksandrov igmp_hdr_size += lmqt_srcs * sizeof(__be32); 834438ef2d0SNikolay Aleksandrov } 835438ef2d0SNikolay Aleksandrov } 836438ef2d0SNikolay Aleksandrov 837438ef2d0SNikolay Aleksandrov pkt_size = sizeof(*eth) + sizeof(*iph) + 4 + igmp_hdr_size; 838438ef2d0SNikolay Aleksandrov if ((p && pkt_size > p->dev->mtu) || 839adc47037SNikolay Aleksandrov pkt_size > brmctx->br->dev->mtu) 840438ef2d0SNikolay Aleksandrov return NULL; 841438ef2d0SNikolay Aleksandrov 842adc47037SNikolay Aleksandrov skb = netdev_alloc_skb_ip_align(brmctx->br->dev, pkt_size); 843eb1d1641SHerbert Xu if (!skb) 844eb1d1641SHerbert Xu goto out; 845eb1d1641SHerbert Xu 846615cc23eSNikolay Aleksandrov __br_multicast_query_handle_vlan(brmctx, pmctx, skb); 847eb1d1641SHerbert Xu skb->protocol = htons(ETH_P_IP); 848eb1d1641SHerbert Xu 849eb1d1641SHerbert Xu skb_reset_mac_header(skb); 850eb1d1641SHerbert Xu eth = eth_hdr(skb); 851eb1d1641SHerbert Xu 852adc47037SNikolay Aleksandrov ether_addr_copy(eth->h_source, brmctx->br->dev->dev_addr); 853438ef2d0SNikolay Aleksandrov ip_eth_mc_map(ip_dst, eth->h_dest); 854eb1d1641SHerbert Xu eth->h_proto = htons(ETH_P_IP); 855eb1d1641SHerbert Xu skb_put(skb, sizeof(*eth)); 856eb1d1641SHerbert Xu 857eb1d1641SHerbert Xu skb_set_network_header(skb, skb->len); 858eb1d1641SHerbert Xu iph = ip_hdr(skb); 859438ef2d0SNikolay Aleksandrov iph->tot_len = htons(pkt_size - sizeof(*eth)); 860eb1d1641SHerbert Xu 861eb1d1641SHerbert Xu iph->version = 4; 862eb1d1641SHerbert Xu iph->ihl = 6; 863eb1d1641SHerbert Xu iph->tos = 0xc0; 864eb1d1641SHerbert Xu iph->id = 0; 865eb1d1641SHerbert Xu iph->frag_off = htons(IP_DF); 866eb1d1641SHerbert Xu iph->ttl = 1; 867eb1d1641SHerbert Xu iph->protocol = IPPROTO_IGMP; 868adc47037SNikolay Aleksandrov iph->saddr = br_opt_get(brmctx->br, BROPT_MULTICAST_QUERY_USE_IFADDR) ? 869adc47037SNikolay Aleksandrov inet_select_addr(brmctx->br->dev, 0, RT_SCOPE_LINK) : 0; 870438ef2d0SNikolay Aleksandrov iph->daddr = ip_dst; 871eb1d1641SHerbert Xu ((u8 *)&iph[1])[0] = IPOPT_RA; 872eb1d1641SHerbert Xu ((u8 *)&iph[1])[1] = 4; 873eb1d1641SHerbert Xu ((u8 *)&iph[1])[2] = 0; 874eb1d1641SHerbert Xu ((u8 *)&iph[1])[3] = 0; 875eb1d1641SHerbert Xu ip_send_check(iph); 876eb1d1641SHerbert Xu skb_put(skb, 24); 877eb1d1641SHerbert Xu 878eb1d1641SHerbert Xu skb_set_transport_header(skb, skb->len); 8791080ab95SNikolay Aleksandrov *igmp_type = IGMP_HOST_MEMBERSHIP_QUERY; 8805e923585SNikolay Aleksandrov 881d3d065c0SNikolay Aleksandrov switch (brmctx->multicast_igmp_version) { 8825e923585SNikolay Aleksandrov case 2: 8835e923585SNikolay Aleksandrov ih = igmp_hdr(skb); 884eb1d1641SHerbert Xu ih->type = IGMP_HOST_MEMBERSHIP_QUERY; 885d3d065c0SNikolay Aleksandrov ih->code = (group ? brmctx->multicast_last_member_interval : 886d3d065c0SNikolay Aleksandrov brmctx->multicast_query_response_interval) / 887eb1d1641SHerbert Xu (HZ / IGMP_TIMER_SCALE); 888eb1d1641SHerbert Xu ih->group = group; 889eb1d1641SHerbert Xu ih->csum = 0; 890438ef2d0SNikolay Aleksandrov csum = &ih->csum; 891438ef2d0SNikolay Aleksandrov csum_start = (void *)ih; 8925e923585SNikolay Aleksandrov break; 8935e923585SNikolay Aleksandrov case 3: 8945e923585SNikolay Aleksandrov ihv3 = igmpv3_query_hdr(skb); 8955e923585SNikolay Aleksandrov ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY; 896d3d065c0SNikolay Aleksandrov ihv3->code = (group ? brmctx->multicast_last_member_interval : 897d3d065c0SNikolay Aleksandrov brmctx->multicast_query_response_interval) / 8985e923585SNikolay Aleksandrov (HZ / IGMP_TIMER_SCALE); 8995e923585SNikolay Aleksandrov ihv3->group = group; 900d3d065c0SNikolay Aleksandrov ihv3->qqic = brmctx->multicast_query_interval / HZ; 901438ef2d0SNikolay Aleksandrov ihv3->nsrcs = htons(lmqt_srcs); 9025e923585SNikolay Aleksandrov ihv3->resv = 0; 903438ef2d0SNikolay Aleksandrov ihv3->suppress = sflag; 9045e923585SNikolay Aleksandrov ihv3->qrv = 2; 9055e923585SNikolay Aleksandrov ihv3->csum = 0; 906438ef2d0SNikolay Aleksandrov csum = &ihv3->csum; 907438ef2d0SNikolay Aleksandrov csum_start = (void *)ihv3; 908438ef2d0SNikolay Aleksandrov if (!pg || !with_srcs) 909438ef2d0SNikolay Aleksandrov break; 910438ef2d0SNikolay Aleksandrov 911438ef2d0SNikolay Aleksandrov lmqt_srcs = 0; 912438ef2d0SNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) { 913438ef2d0SNikolay Aleksandrov if (over_lmqt == time_after(ent->timer.expires, 914438ef2d0SNikolay Aleksandrov lmqt) && 915438ef2d0SNikolay Aleksandrov ent->src_query_rexmit_cnt > 0) { 916deb96566SNikolay Aleksandrov ihv3->srcs[lmqt_srcs++] = ent->addr.src.ip4; 917438ef2d0SNikolay Aleksandrov ent->src_query_rexmit_cnt--; 91842c11ccfSNikolay Aleksandrov if (need_rexmit && ent->src_query_rexmit_cnt) 91942c11ccfSNikolay Aleksandrov *need_rexmit = true; 920438ef2d0SNikolay Aleksandrov } 921438ef2d0SNikolay Aleksandrov } 922438ef2d0SNikolay Aleksandrov if (WARN_ON(lmqt_srcs != ntohs(ihv3->nsrcs))) { 923438ef2d0SNikolay Aleksandrov kfree_skb(skb); 924438ef2d0SNikolay Aleksandrov return NULL; 925438ef2d0SNikolay Aleksandrov } 9265e923585SNikolay Aleksandrov break; 9275e923585SNikolay Aleksandrov } 928eb1d1641SHerbert Xu 929438ef2d0SNikolay Aleksandrov if (WARN_ON(!csum || !csum_start)) { 930438ef2d0SNikolay Aleksandrov kfree_skb(skb); 931438ef2d0SNikolay Aleksandrov return NULL; 932438ef2d0SNikolay Aleksandrov } 933438ef2d0SNikolay Aleksandrov 934438ef2d0SNikolay Aleksandrov *csum = ip_compute_csum(csum_start, igmp_hdr_size); 9355e923585SNikolay Aleksandrov skb_put(skb, igmp_hdr_size); 936eb1d1641SHerbert Xu __skb_pull(skb, sizeof(*eth)); 937eb1d1641SHerbert Xu 938eb1d1641SHerbert Xu out: 939eb1d1641SHerbert Xu return skb; 940eb1d1641SHerbert Xu } 941eb1d1641SHerbert Xu 942dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 943adc47037SNikolay Aleksandrov static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge_mcast *brmctx, 944615cc23eSNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 945438ef2d0SNikolay Aleksandrov struct net_bridge_port_group *pg, 946438ef2d0SNikolay Aleksandrov const struct in6_addr *ip6_dst, 947438ef2d0SNikolay Aleksandrov const struct in6_addr *group, 948438ef2d0SNikolay Aleksandrov bool with_srcs, bool over_llqt, 94942c11ccfSNikolay Aleksandrov u8 sflag, u8 *igmp_type, 95042c11ccfSNikolay Aleksandrov bool *need_rexmit) 95108b202b6SYOSHIFUJI Hideaki { 952085b53c8SNikolay Aleksandrov struct net_bridge_port *p = pg ? pg->key.port : NULL; 953438ef2d0SNikolay Aleksandrov struct net_bridge_group_src *ent; 954438ef2d0SNikolay Aleksandrov size_t pkt_size, mld_hdr_size; 955438ef2d0SNikolay Aleksandrov unsigned long now = jiffies; 956aa2ae3e7SNikolay Aleksandrov struct mld2_query *mld2q; 957438ef2d0SNikolay Aleksandrov void *csum_start = NULL; 958aa2ae3e7SNikolay Aleksandrov unsigned long interval; 959438ef2d0SNikolay Aleksandrov __sum16 *csum = NULL; 96008b202b6SYOSHIFUJI Hideaki struct ipv6hdr *ip6h; 96108b202b6SYOSHIFUJI Hideaki struct mld_msg *mldq; 962aa2ae3e7SNikolay Aleksandrov struct sk_buff *skb; 963438ef2d0SNikolay Aleksandrov unsigned long llqt; 96408b202b6SYOSHIFUJI Hideaki struct ethhdr *eth; 965438ef2d0SNikolay Aleksandrov u16 llqt_srcs = 0; 96608b202b6SYOSHIFUJI Hideaki u8 *hopopt; 96708b202b6SYOSHIFUJI Hideaki 968aa2ae3e7SNikolay Aleksandrov mld_hdr_size = sizeof(*mldq); 969d3d065c0SNikolay Aleksandrov if (brmctx->multicast_mld_version == 2) { 970aa2ae3e7SNikolay Aleksandrov mld_hdr_size = sizeof(*mld2q); 971438ef2d0SNikolay Aleksandrov if (pg && with_srcs) { 972d3d065c0SNikolay Aleksandrov llqt = now + (brmctx->multicast_last_member_interval * 973d3d065c0SNikolay Aleksandrov brmctx->multicast_last_member_count); 974438ef2d0SNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) { 975438ef2d0SNikolay Aleksandrov if (over_llqt == time_after(ent->timer.expires, 976438ef2d0SNikolay Aleksandrov llqt) && 977438ef2d0SNikolay Aleksandrov ent->src_query_rexmit_cnt > 0) 978438ef2d0SNikolay Aleksandrov llqt_srcs++; 979438ef2d0SNikolay Aleksandrov } 980438ef2d0SNikolay Aleksandrov 981438ef2d0SNikolay Aleksandrov if (!llqt_srcs) 982438ef2d0SNikolay Aleksandrov return NULL; 983438ef2d0SNikolay Aleksandrov mld_hdr_size += llqt_srcs * sizeof(struct in6_addr); 984438ef2d0SNikolay Aleksandrov } 985438ef2d0SNikolay Aleksandrov } 986438ef2d0SNikolay Aleksandrov 987438ef2d0SNikolay Aleksandrov pkt_size = sizeof(*eth) + sizeof(*ip6h) + 8 + mld_hdr_size; 988438ef2d0SNikolay Aleksandrov if ((p && pkt_size > p->dev->mtu) || 989adc47037SNikolay Aleksandrov pkt_size > brmctx->br->dev->mtu) 990438ef2d0SNikolay Aleksandrov return NULL; 991438ef2d0SNikolay Aleksandrov 992adc47037SNikolay Aleksandrov skb = netdev_alloc_skb_ip_align(brmctx->br->dev, pkt_size); 99308b202b6SYOSHIFUJI Hideaki if (!skb) 99408b202b6SYOSHIFUJI Hideaki goto out; 99508b202b6SYOSHIFUJI Hideaki 996615cc23eSNikolay Aleksandrov __br_multicast_query_handle_vlan(brmctx, pmctx, skb); 99708b202b6SYOSHIFUJI Hideaki skb->protocol = htons(ETH_P_IPV6); 99808b202b6SYOSHIFUJI Hideaki 99908b202b6SYOSHIFUJI Hideaki /* Ethernet header */ 100008b202b6SYOSHIFUJI Hideaki skb_reset_mac_header(skb); 100108b202b6SYOSHIFUJI Hideaki eth = eth_hdr(skb); 100208b202b6SYOSHIFUJI Hideaki 1003adc47037SNikolay Aleksandrov ether_addr_copy(eth->h_source, brmctx->br->dev->dev_addr); 100408b202b6SYOSHIFUJI Hideaki eth->h_proto = htons(ETH_P_IPV6); 100508b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*eth)); 100608b202b6SYOSHIFUJI Hideaki 100708b202b6SYOSHIFUJI Hideaki /* IPv6 header + HbH option */ 100808b202b6SYOSHIFUJI Hideaki skb_set_network_header(skb, skb->len); 100908b202b6SYOSHIFUJI Hideaki ip6h = ipv6_hdr(skb); 101008b202b6SYOSHIFUJI Hideaki 101108b202b6SYOSHIFUJI Hideaki *(__force __be32 *)ip6h = htonl(0x60000000); 1012aa2ae3e7SNikolay Aleksandrov ip6h->payload_len = htons(8 + mld_hdr_size); 101308b202b6SYOSHIFUJI Hideaki ip6h->nexthdr = IPPROTO_HOPOPTS; 101408b202b6SYOSHIFUJI Hideaki ip6h->hop_limit = 1; 1015438ef2d0SNikolay Aleksandrov ip6h->daddr = *ip6_dst; 1016adc47037SNikolay Aleksandrov if (ipv6_dev_get_saddr(dev_net(brmctx->br->dev), brmctx->br->dev, 1017adc47037SNikolay Aleksandrov &ip6h->daddr, 0, &ip6h->saddr)) { 1018d1d81d4cSUlrich Weber kfree_skb(skb); 1019adc47037SNikolay Aleksandrov br_opt_toggle(brmctx->br, BROPT_HAS_IPV6_ADDR, false); 1020d1d81d4cSUlrich Weber return NULL; 1021d1d81d4cSUlrich Weber } 10220888d5f3Sdaniel 1023adc47037SNikolay Aleksandrov br_opt_toggle(brmctx->br, BROPT_HAS_IPV6_ADDR, true); 102436cff5a1SLinus Lüssing ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest); 102508b202b6SYOSHIFUJI Hideaki 102608b202b6SYOSHIFUJI Hideaki hopopt = (u8 *)(ip6h + 1); 102708b202b6SYOSHIFUJI Hideaki hopopt[0] = IPPROTO_ICMPV6; /* next hdr */ 102808b202b6SYOSHIFUJI Hideaki hopopt[1] = 0; /* length of HbH */ 102908b202b6SYOSHIFUJI Hideaki hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */ 103008b202b6SYOSHIFUJI Hideaki hopopt[3] = 2; /* Length of RA Option */ 103108b202b6SYOSHIFUJI Hideaki hopopt[4] = 0; /* Type = 0x0000 (MLD) */ 103208b202b6SYOSHIFUJI Hideaki hopopt[5] = 0; 10331de5a71cSEldad Zack hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */ 10341de5a71cSEldad Zack hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */ 103508b202b6SYOSHIFUJI Hideaki 103608b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*ip6h) + 8); 103708b202b6SYOSHIFUJI Hideaki 103808b202b6SYOSHIFUJI Hideaki /* ICMPv6 */ 103908b202b6SYOSHIFUJI Hideaki skb_set_transport_header(skb, skb->len); 1040438ef2d0SNikolay Aleksandrov interval = ipv6_addr_any(group) ? 1041d3d065c0SNikolay Aleksandrov brmctx->multicast_query_response_interval : 1042d3d065c0SNikolay Aleksandrov brmctx->multicast_last_member_interval; 10431080ab95SNikolay Aleksandrov *igmp_type = ICMPV6_MGM_QUERY; 1044d3d065c0SNikolay Aleksandrov switch (brmctx->multicast_mld_version) { 1045aa2ae3e7SNikolay Aleksandrov case 1: 1046aa2ae3e7SNikolay Aleksandrov mldq = (struct mld_msg *)icmp6_hdr(skb); 104708b202b6SYOSHIFUJI Hideaki mldq->mld_type = ICMPV6_MGM_QUERY; 104808b202b6SYOSHIFUJI Hideaki mldq->mld_code = 0; 104908b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = 0; 105008b202b6SYOSHIFUJI Hideaki mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval)); 105108b202b6SYOSHIFUJI Hideaki mldq->mld_reserved = 0; 1052438ef2d0SNikolay Aleksandrov mldq->mld_mca = *group; 1053438ef2d0SNikolay Aleksandrov csum = &mldq->mld_cksum; 1054438ef2d0SNikolay Aleksandrov csum_start = (void *)mldq; 1055aa2ae3e7SNikolay Aleksandrov break; 1056aa2ae3e7SNikolay Aleksandrov case 2: 1057aa2ae3e7SNikolay Aleksandrov mld2q = (struct mld2_query *)icmp6_hdr(skb); 105853631a5fSLance Richardson mld2q->mld2q_mrc = htons((u16)jiffies_to_msecs(interval)); 1059aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_type = ICMPV6_MGM_QUERY; 1060aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_code = 0; 1061aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_cksum = 0; 1062aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_resv1 = 0; 1063aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_resv2 = 0; 1064438ef2d0SNikolay Aleksandrov mld2q->mld2q_suppress = sflag; 1065aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_qrv = 2; 1066438ef2d0SNikolay Aleksandrov mld2q->mld2q_nsrcs = htons(llqt_srcs); 1067d3d065c0SNikolay Aleksandrov mld2q->mld2q_qqic = brmctx->multicast_query_interval / HZ; 1068438ef2d0SNikolay Aleksandrov mld2q->mld2q_mca = *group; 1069438ef2d0SNikolay Aleksandrov csum = &mld2q->mld2q_cksum; 1070438ef2d0SNikolay Aleksandrov csum_start = (void *)mld2q; 1071438ef2d0SNikolay Aleksandrov if (!pg || !with_srcs) 1072438ef2d0SNikolay Aleksandrov break; 1073438ef2d0SNikolay Aleksandrov 1074438ef2d0SNikolay Aleksandrov llqt_srcs = 0; 1075438ef2d0SNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) { 1076438ef2d0SNikolay Aleksandrov if (over_llqt == time_after(ent->timer.expires, 1077438ef2d0SNikolay Aleksandrov llqt) && 1078438ef2d0SNikolay Aleksandrov ent->src_query_rexmit_cnt > 0) { 1079deb96566SNikolay Aleksandrov mld2q->mld2q_srcs[llqt_srcs++] = ent->addr.src.ip6; 1080438ef2d0SNikolay Aleksandrov ent->src_query_rexmit_cnt--; 108142c11ccfSNikolay Aleksandrov if (need_rexmit && ent->src_query_rexmit_cnt) 108242c11ccfSNikolay Aleksandrov *need_rexmit = true; 1083438ef2d0SNikolay Aleksandrov } 1084438ef2d0SNikolay Aleksandrov } 1085438ef2d0SNikolay Aleksandrov if (WARN_ON(llqt_srcs != ntohs(mld2q->mld2q_nsrcs))) { 1086438ef2d0SNikolay Aleksandrov kfree_skb(skb); 1087438ef2d0SNikolay Aleksandrov return NULL; 1088438ef2d0SNikolay Aleksandrov } 1089aa2ae3e7SNikolay Aleksandrov break; 1090aa2ae3e7SNikolay Aleksandrov } 109108b202b6SYOSHIFUJI Hideaki 1092438ef2d0SNikolay Aleksandrov if (WARN_ON(!csum || !csum_start)) { 1093438ef2d0SNikolay Aleksandrov kfree_skb(skb); 1094438ef2d0SNikolay Aleksandrov return NULL; 1095438ef2d0SNikolay Aleksandrov } 1096438ef2d0SNikolay Aleksandrov 1097438ef2d0SNikolay Aleksandrov *csum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, mld_hdr_size, 1098438ef2d0SNikolay Aleksandrov IPPROTO_ICMPV6, 1099438ef2d0SNikolay Aleksandrov csum_partial(csum_start, mld_hdr_size, 0)); 1100438ef2d0SNikolay Aleksandrov skb_put(skb, mld_hdr_size); 110108b202b6SYOSHIFUJI Hideaki __skb_pull(skb, sizeof(*eth)); 110208b202b6SYOSHIFUJI Hideaki 110308b202b6SYOSHIFUJI Hideaki out: 110408b202b6SYOSHIFUJI Hideaki return skb; 110508b202b6SYOSHIFUJI Hideaki } 110608b202b6SYOSHIFUJI Hideaki #endif 110708b202b6SYOSHIFUJI Hideaki 1108adc47037SNikolay Aleksandrov static struct sk_buff *br_multicast_alloc_query(struct net_bridge_mcast *brmctx, 1109615cc23eSNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 1110438ef2d0SNikolay Aleksandrov struct net_bridge_port_group *pg, 1111438ef2d0SNikolay Aleksandrov struct br_ip *ip_dst, 1112438ef2d0SNikolay Aleksandrov struct br_ip *group, 1113438ef2d0SNikolay Aleksandrov bool with_srcs, bool over_lmqt, 111442c11ccfSNikolay Aleksandrov u8 sflag, u8 *igmp_type, 111542c11ccfSNikolay Aleksandrov bool *need_rexmit) 11168ef2a9a5SYOSHIFUJI Hideaki { 1117438ef2d0SNikolay Aleksandrov __be32 ip4_dst; 1118438ef2d0SNikolay Aleksandrov 1119438ef2d0SNikolay Aleksandrov switch (group->proto) { 11208ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 1121eab3227bSNikolay Aleksandrov ip4_dst = ip_dst ? ip_dst->dst.ip4 : htonl(INADDR_ALLHOSTS_GROUP); 1122615cc23eSNikolay Aleksandrov return br_ip4_multicast_alloc_query(brmctx, pmctx, pg, 1123eab3227bSNikolay Aleksandrov ip4_dst, group->dst.ip4, 1124438ef2d0SNikolay Aleksandrov with_srcs, over_lmqt, 112542c11ccfSNikolay Aleksandrov sflag, igmp_type, 112642c11ccfSNikolay Aleksandrov need_rexmit); 1127dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 1128438ef2d0SNikolay Aleksandrov case htons(ETH_P_IPV6): { 1129438ef2d0SNikolay Aleksandrov struct in6_addr ip6_dst; 1130438ef2d0SNikolay Aleksandrov 1131438ef2d0SNikolay Aleksandrov if (ip_dst) 1132eab3227bSNikolay Aleksandrov ip6_dst = ip_dst->dst.ip6; 1133438ef2d0SNikolay Aleksandrov else 1134438ef2d0SNikolay Aleksandrov ipv6_addr_set(&ip6_dst, htonl(0xff020000), 0, 0, 1135438ef2d0SNikolay Aleksandrov htonl(1)); 1136438ef2d0SNikolay Aleksandrov 1137615cc23eSNikolay Aleksandrov return br_ip6_multicast_alloc_query(brmctx, pmctx, pg, 1138eab3227bSNikolay Aleksandrov &ip6_dst, &group->dst.ip6, 1139438ef2d0SNikolay Aleksandrov with_srcs, over_lmqt, 114042c11ccfSNikolay Aleksandrov sflag, igmp_type, 114142c11ccfSNikolay Aleksandrov need_rexmit); 1142438ef2d0SNikolay Aleksandrov } 114308b202b6SYOSHIFUJI Hideaki #endif 11448ef2a9a5SYOSHIFUJI Hideaki } 11458ef2a9a5SYOSHIFUJI Hideaki return NULL; 11468ef2a9a5SYOSHIFUJI Hideaki } 11478ef2a9a5SYOSHIFUJI Hideaki 1148cfd56754SCong Wang struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br, 11495e923585SNikolay Aleksandrov struct br_ip *group) 1150eb1d1641SHerbert Xu { 1151eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 11524c0833bcSTobias Klauser int err; 1153eb1d1641SHerbert Xu 115419e3a9c9SNikolay Aleksandrov mp = br_mdb_ip_get(br, group); 115519e3a9c9SNikolay Aleksandrov if (mp) 115619e3a9c9SNikolay Aleksandrov return mp; 1157eb1d1641SHerbert Xu 115819e3a9c9SNikolay Aleksandrov if (atomic_read(&br->mdb_hash_tbl.nelems) >= br->hash_max) { 115919e3a9c9SNikolay Aleksandrov br_opt_toggle(br, BROPT_MULTICAST_ENABLED, false); 116019e3a9c9SNikolay Aleksandrov return ERR_PTR(-E2BIG); 1161eb1d1641SHerbert Xu } 1162eb1d1641SHerbert Xu 1163eb1d1641SHerbert Xu mp = kzalloc(sizeof(*mp), GFP_ATOMIC); 1164eb1d1641SHerbert Xu if (unlikely(!mp)) 11654c0833bcSTobias Klauser return ERR_PTR(-ENOMEM); 1166eb1d1641SHerbert Xu 1167eb1d1641SHerbert Xu mp->br = br; 11688ef2a9a5SYOSHIFUJI Hideaki mp->addr = *group; 1169e12cec65SNikolay Aleksandrov mp->mcast_gc.destroy = br_multicast_destroy_mdb_entry; 117088c1f37fSAllen Pais timer_setup(&mp->timer, br_multicast_group_expired, 0); 117119e3a9c9SNikolay Aleksandrov err = rhashtable_lookup_insert_fast(&br->mdb_hash_tbl, &mp->rhnode, 117219e3a9c9SNikolay Aleksandrov br_mdb_rht_params); 117319e3a9c9SNikolay Aleksandrov if (err) { 117419e3a9c9SNikolay Aleksandrov kfree(mp); 117519e3a9c9SNikolay Aleksandrov mp = ERR_PTR(err); 117619e3a9c9SNikolay Aleksandrov } else { 117719e3a9c9SNikolay Aleksandrov hlist_add_head_rcu(&mp->mdb_node, &br->mdb_list); 117819e3a9c9SNikolay Aleksandrov } 11791faabf2aSEric Dumazet 1180eb1d1641SHerbert Xu return mp; 1181eb1d1641SHerbert Xu } 1182eb1d1641SHerbert Xu 11838b671779SNikolay Aleksandrov static void br_multicast_group_src_expired(struct timer_list *t) 11848b671779SNikolay Aleksandrov { 11858b671779SNikolay Aleksandrov struct net_bridge_group_src *src = from_timer(src, t, timer); 11868b671779SNikolay Aleksandrov struct net_bridge_port_group *pg; 11878b671779SNikolay Aleksandrov struct net_bridge *br = src->br; 11888b671779SNikolay Aleksandrov 11898b671779SNikolay Aleksandrov spin_lock(&br->multicast_lock); 11908b671779SNikolay Aleksandrov if (hlist_unhashed(&src->node) || !netif_running(br->dev) || 11918b671779SNikolay Aleksandrov timer_pending(&src->timer)) 11928b671779SNikolay Aleksandrov goto out; 11938b671779SNikolay Aleksandrov 11948b671779SNikolay Aleksandrov pg = src->pg; 11958b671779SNikolay Aleksandrov if (pg->filter_mode == MCAST_INCLUDE) { 1196d5a10222SNikolay Aleksandrov br_multicast_del_group_src(src, false); 11978b671779SNikolay Aleksandrov if (!hlist_empty(&pg->src_list)) 11988b671779SNikolay Aleksandrov goto out; 11998b671779SNikolay Aleksandrov br_multicast_find_del_pg(br, pg); 12009116ffbfSNikolay Aleksandrov } else { 12019116ffbfSNikolay Aleksandrov br_multicast_fwd_src_handle(src); 12028b671779SNikolay Aleksandrov } 12039116ffbfSNikolay Aleksandrov 12048b671779SNikolay Aleksandrov out: 12058b671779SNikolay Aleksandrov spin_unlock(&br->multicast_lock); 12068b671779SNikolay Aleksandrov } 12078b671779SNikolay Aleksandrov 1208474ddb37SNikolay Aleksandrov struct net_bridge_group_src * 12098b671779SNikolay Aleksandrov br_multicast_find_group_src(struct net_bridge_port_group *pg, struct br_ip *ip) 12108b671779SNikolay Aleksandrov { 12118b671779SNikolay Aleksandrov struct net_bridge_group_src *ent; 12128b671779SNikolay Aleksandrov 12138b671779SNikolay Aleksandrov switch (ip->proto) { 12148b671779SNikolay Aleksandrov case htons(ETH_P_IP): 12158b671779SNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 1216deb96566SNikolay Aleksandrov if (ip->src.ip4 == ent->addr.src.ip4) 12178b671779SNikolay Aleksandrov return ent; 12188b671779SNikolay Aleksandrov break; 12198b671779SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 12208b671779SNikolay Aleksandrov case htons(ETH_P_IPV6): 12218b671779SNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 1222deb96566SNikolay Aleksandrov if (!ipv6_addr_cmp(&ent->addr.src.ip6, &ip->src.ip6)) 12238b671779SNikolay Aleksandrov return ent; 12248b671779SNikolay Aleksandrov break; 12258b671779SNikolay Aleksandrov #endif 12268b671779SNikolay Aleksandrov } 12278b671779SNikolay Aleksandrov 12288b671779SNikolay Aleksandrov return NULL; 12298b671779SNikolay Aleksandrov } 12308b671779SNikolay Aleksandrov 12318b671779SNikolay Aleksandrov static struct net_bridge_group_src * 12328b671779SNikolay Aleksandrov br_multicast_new_group_src(struct net_bridge_port_group *pg, struct br_ip *src_ip) 12338b671779SNikolay Aleksandrov { 12348b671779SNikolay Aleksandrov struct net_bridge_group_src *grp_src; 12358b671779SNikolay Aleksandrov 12368b671779SNikolay Aleksandrov if (unlikely(pg->src_ents >= PG_SRC_ENT_LIMIT)) 12378b671779SNikolay Aleksandrov return NULL; 12388b671779SNikolay Aleksandrov 12398b671779SNikolay Aleksandrov switch (src_ip->proto) { 12408b671779SNikolay Aleksandrov case htons(ETH_P_IP): 1241deb96566SNikolay Aleksandrov if (ipv4_is_zeronet(src_ip->src.ip4) || 1242deb96566SNikolay Aleksandrov ipv4_is_multicast(src_ip->src.ip4)) 12438b671779SNikolay Aleksandrov return NULL; 12448b671779SNikolay Aleksandrov break; 12458b671779SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 12468b671779SNikolay Aleksandrov case htons(ETH_P_IPV6): 1247deb96566SNikolay Aleksandrov if (ipv6_addr_any(&src_ip->src.ip6) || 1248deb96566SNikolay Aleksandrov ipv6_addr_is_multicast(&src_ip->src.ip6)) 12498b671779SNikolay Aleksandrov return NULL; 12508b671779SNikolay Aleksandrov break; 12518b671779SNikolay Aleksandrov #endif 12528b671779SNikolay Aleksandrov } 12538b671779SNikolay Aleksandrov 12548b671779SNikolay Aleksandrov grp_src = kzalloc(sizeof(*grp_src), GFP_ATOMIC); 12558b671779SNikolay Aleksandrov if (unlikely(!grp_src)) 12568b671779SNikolay Aleksandrov return NULL; 12578b671779SNikolay Aleksandrov 12588b671779SNikolay Aleksandrov grp_src->pg = pg; 1259085b53c8SNikolay Aleksandrov grp_src->br = pg->key.port->br; 12608b671779SNikolay Aleksandrov grp_src->addr = *src_ip; 1261e12cec65SNikolay Aleksandrov grp_src->mcast_gc.destroy = br_multicast_destroy_group_src; 12628b671779SNikolay Aleksandrov timer_setup(&grp_src->timer, br_multicast_group_src_expired, 0); 12638b671779SNikolay Aleksandrov 12648b671779SNikolay Aleksandrov hlist_add_head_rcu(&grp_src->node, &pg->src_list); 12658b671779SNikolay Aleksandrov pg->src_ents++; 12668b671779SNikolay Aleksandrov 12678b671779SNikolay Aleksandrov return grp_src; 12688b671779SNikolay Aleksandrov } 12698b671779SNikolay Aleksandrov 1270cfd56754SCong Wang struct net_bridge_port_group *br_multicast_new_port_group( 1271cfd56754SCong Wang struct net_bridge_port *port, 1272cfd56754SCong Wang struct br_ip *group, 1273ccb1c31aSAmerigo Wang struct net_bridge_port_group __rcu *next, 12746db6f0eaSFelix Fietkau unsigned char flags, 12758b671779SNikolay Aleksandrov const unsigned char *src, 12768f8cb77eSNikolay Aleksandrov u8 filter_mode, 12778f8cb77eSNikolay Aleksandrov u8 rt_protocol) 1278cfd56754SCong Wang { 1279cfd56754SCong Wang struct net_bridge_port_group *p; 1280cfd56754SCong Wang 1281cfd56754SCong Wang p = kzalloc(sizeof(*p), GFP_ATOMIC); 1282cfd56754SCong Wang if (unlikely(!p)) 1283cfd56754SCong Wang return NULL; 1284cfd56754SCong Wang 1285085b53c8SNikolay Aleksandrov p->key.addr = *group; 1286085b53c8SNikolay Aleksandrov p->key.port = port; 12879d06b6d8SElad Raz p->flags = flags; 12888b671779SNikolay Aleksandrov p->filter_mode = filter_mode; 12898f8cb77eSNikolay Aleksandrov p->rt_protocol = rt_protocol; 12905b163288SNikolay Aleksandrov p->eht_host_tree = RB_ROOT; 1291baa74d39SNikolay Aleksandrov p->eht_set_tree = RB_ROOT; 1292e12cec65SNikolay Aleksandrov p->mcast_gc.destroy = br_multicast_destroy_port_group; 12938b671779SNikolay Aleksandrov INIT_HLIST_HEAD(&p->src_list); 1294085b53c8SNikolay Aleksandrov 1295085b53c8SNikolay Aleksandrov if (!br_multicast_is_star_g(group) && 1296085b53c8SNikolay Aleksandrov rhashtable_lookup_insert_fast(&port->br->sg_port_tbl, &p->rhnode, 1297085b53c8SNikolay Aleksandrov br_sg_port_rht_params)) { 1298085b53c8SNikolay Aleksandrov kfree(p); 1299085b53c8SNikolay Aleksandrov return NULL; 1300085b53c8SNikolay Aleksandrov } 1301085b53c8SNikolay Aleksandrov 1302eca2a43bSstephen hemminger rcu_assign_pointer(p->next, next); 130388c1f37fSAllen Pais timer_setup(&p->timer, br_multicast_port_group_expired, 0); 130442c11ccfSNikolay Aleksandrov timer_setup(&p->rexmit_timer, br_multicast_port_group_rexmit, 0); 130542c11ccfSNikolay Aleksandrov hlist_add_head(&p->mglist, &port->mglist); 13066db6f0eaSFelix Fietkau 13076db6f0eaSFelix Fietkau if (src) 13086db6f0eaSFelix Fietkau memcpy(p->eth_addr, src, ETH_ALEN); 13096db6f0eaSFelix Fietkau else 13101bfe45f4SMao Wenan eth_broadcast_addr(p->eth_addr); 13116db6f0eaSFelix Fietkau 1312cfd56754SCong Wang return p; 1313cfd56754SCong Wang } 1314cfd56754SCong Wang 131558d913a3SNikolay Aleksandrov void br_multicast_host_join(const struct net_bridge_mcast *brmctx, 131658d913a3SNikolay Aleksandrov struct net_bridge_mdb_entry *mp, bool notify) 13171bc844eeSNikolay Aleksandrov { 13181bc844eeSNikolay Aleksandrov if (!mp->host_joined) { 13191bc844eeSNikolay Aleksandrov mp->host_joined = true; 1320094b82fdSNikolay Aleksandrov if (br_multicast_is_star_g(&mp->addr)) 1321094b82fdSNikolay Aleksandrov br_multicast_star_g_host_state(mp); 13221bc844eeSNikolay Aleksandrov if (notify) 132381f19838SNikolay Aleksandrov br_mdb_notify(mp->br->dev, mp, NULL, RTM_NEWMDB); 13241bc844eeSNikolay Aleksandrov } 1325955062b0SNikolay Aleksandrov 1326955062b0SNikolay Aleksandrov if (br_group_is_l2(&mp->addr)) 1327955062b0SNikolay Aleksandrov return; 1328955062b0SNikolay Aleksandrov 132958d913a3SNikolay Aleksandrov mod_timer(&mp->timer, jiffies + brmctx->multicast_membership_interval); 13301bc844eeSNikolay Aleksandrov } 13311bc844eeSNikolay Aleksandrov 13321bc844eeSNikolay Aleksandrov void br_multicast_host_leave(struct net_bridge_mdb_entry *mp, bool notify) 13331bc844eeSNikolay Aleksandrov { 13341bc844eeSNikolay Aleksandrov if (!mp->host_joined) 13351bc844eeSNikolay Aleksandrov return; 13361bc844eeSNikolay Aleksandrov 13371bc844eeSNikolay Aleksandrov mp->host_joined = false; 1338094b82fdSNikolay Aleksandrov if (br_multicast_is_star_g(&mp->addr)) 1339094b82fdSNikolay Aleksandrov br_multicast_star_g_host_state(mp); 13401bc844eeSNikolay Aleksandrov if (notify) 134181f19838SNikolay Aleksandrov br_mdb_notify(mp->br->dev, mp, NULL, RTM_DELMDB); 13421bc844eeSNikolay Aleksandrov } 13431bc844eeSNikolay Aleksandrov 1344b0812368SNikolay Aleksandrov static struct net_bridge_port_group * 1345adc47037SNikolay Aleksandrov __br_multicast_add_group(struct net_bridge_mcast *brmctx, 1346adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 13476db6f0eaSFelix Fietkau struct br_ip *group, 13488b671779SNikolay Aleksandrov const unsigned char *src, 13490436862eSNikolay Aleksandrov u8 filter_mode, 13509116ffbfSNikolay Aleksandrov bool igmpv2_mldv1, 13519116ffbfSNikolay Aleksandrov bool blocked) 1352eb1d1641SHerbert Xu { 1353e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 1354b0812368SNikolay Aleksandrov struct net_bridge_port_group *p = NULL; 13555e923585SNikolay Aleksandrov struct net_bridge_mdb_entry *mp; 1356454594f3SLinus Lüssing unsigned long now = jiffies; 1357eb1d1641SHerbert Xu 13584cdd0d10SNikolay Aleksandrov if (!br_multicast_ctx_should_use(brmctx, pmctx)) 1359eb1d1641SHerbert Xu goto out; 1360eb1d1641SHerbert Xu 1361adc47037SNikolay Aleksandrov mp = br_multicast_new_group(brmctx->br, group); 13624c0833bcSTobias Klauser if (IS_ERR(mp)) 13631697291dSXu Wang return ERR_CAST(mp); 1364eb1d1641SHerbert Xu 1365adc47037SNikolay Aleksandrov if (!pmctx) { 136658d913a3SNikolay Aleksandrov br_multicast_host_join(brmctx, mp, true); 1367eb1d1641SHerbert Xu goto out; 1368eb1d1641SHerbert Xu } 1369eb1d1641SHerbert Xu 1370e8051688SEric Dumazet for (pp = &mp->ports; 1371adc47037SNikolay Aleksandrov (p = mlock_dereference(*pp, brmctx->br)) != NULL; 1372e8051688SEric Dumazet pp = &p->next) { 1373adc47037SNikolay Aleksandrov if (br_port_group_equal(p, pmctx->port, src)) 1374454594f3SLinus Lüssing goto found; 1375adc47037SNikolay Aleksandrov if ((unsigned long)p->key.port < (unsigned long)pmctx->port) 1376eb1d1641SHerbert Xu break; 1377eb1d1641SHerbert Xu } 1378eb1d1641SHerbert Xu 1379adc47037SNikolay Aleksandrov p = br_multicast_new_port_group(pmctx->port, group, *pp, 0, src, 1380955062b0SNikolay Aleksandrov filter_mode, RTPROT_KERNEL); 1381b0812368SNikolay Aleksandrov if (unlikely(!p)) { 1382b0812368SNikolay Aleksandrov p = ERR_PTR(-ENOMEM); 1383b0812368SNikolay Aleksandrov goto out; 1384b0812368SNikolay Aleksandrov } 1385eb1d1641SHerbert Xu rcu_assign_pointer(*pp, p); 13869116ffbfSNikolay Aleksandrov if (blocked) 13879116ffbfSNikolay Aleksandrov p->flags |= MDB_PG_FLAGS_BLOCKED; 1388adc47037SNikolay Aleksandrov br_mdb_notify(brmctx->br->dev, mp, p, RTM_NEWMDB); 1389eb1d1641SHerbert Xu 1390454594f3SLinus Lüssing found: 13910436862eSNikolay Aleksandrov if (igmpv2_mldv1) 1392d3d065c0SNikolay Aleksandrov mod_timer(&p->timer, 1393adc47037SNikolay Aleksandrov now + brmctx->multicast_membership_interval); 139481f19838SNikolay Aleksandrov 1395eb1d1641SHerbert Xu out: 1396b0812368SNikolay Aleksandrov return p; 1397b0812368SNikolay Aleksandrov } 1398eb1d1641SHerbert Xu 1399adc47037SNikolay Aleksandrov static int br_multicast_add_group(struct net_bridge_mcast *brmctx, 1400adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 1401b0812368SNikolay Aleksandrov struct br_ip *group, 1402b0812368SNikolay Aleksandrov const unsigned char *src, 1403b0812368SNikolay Aleksandrov u8 filter_mode, 1404b0812368SNikolay Aleksandrov bool igmpv2_mldv1) 1405b0812368SNikolay Aleksandrov { 1406b0812368SNikolay Aleksandrov struct net_bridge_port_group *pg; 1407b0812368SNikolay Aleksandrov int err; 1408b0812368SNikolay Aleksandrov 1409adc47037SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 1410adc47037SNikolay Aleksandrov pg = __br_multicast_add_group(brmctx, pmctx, group, src, filter_mode, 14119116ffbfSNikolay Aleksandrov igmpv2_mldv1, false); 1412b0812368SNikolay Aleksandrov /* NULL is considered valid for host joined groups */ 14138d21c882SJiapeng Zhong err = PTR_ERR_OR_ZERO(pg); 1414adc47037SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 1415b0812368SNikolay Aleksandrov 1416eb1d1641SHerbert Xu return err; 1417eb1d1641SHerbert Xu } 1418eb1d1641SHerbert Xu 1419adc47037SNikolay Aleksandrov static int br_ip4_multicast_add_group(struct net_bridge_mcast *brmctx, 1420adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 1421b0e9a30dSVlad Yasevich __be32 group, 14226db6f0eaSFelix Fietkau __u16 vid, 14238b671779SNikolay Aleksandrov const unsigned char *src, 14248b671779SNikolay Aleksandrov bool igmpv2) 14258ef2a9a5SYOSHIFUJI Hideaki { 14268ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 14278b671779SNikolay Aleksandrov u8 filter_mode; 14288ef2a9a5SYOSHIFUJI Hideaki 14298ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 14308ef2a9a5SYOSHIFUJI Hideaki return 0; 14318ef2a9a5SYOSHIFUJI Hideaki 14321515a63fSNikolay Aleksandrov memset(&br_group, 0, sizeof(br_group)); 1433eab3227bSNikolay Aleksandrov br_group.dst.ip4 = group; 14348ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 1435b0e9a30dSVlad Yasevich br_group.vid = vid; 14368b671779SNikolay Aleksandrov filter_mode = igmpv2 ? MCAST_EXCLUDE : MCAST_INCLUDE; 14378ef2a9a5SYOSHIFUJI Hideaki 1438adc47037SNikolay Aleksandrov return br_multicast_add_group(brmctx, pmctx, &br_group, src, 1439adc47037SNikolay Aleksandrov filter_mode, igmpv2); 14408ef2a9a5SYOSHIFUJI Hideaki } 14418ef2a9a5SYOSHIFUJI Hideaki 1442dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 1443adc47037SNikolay Aleksandrov static int br_ip6_multicast_add_group(struct net_bridge_mcast *brmctx, 1444adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 1445b0e9a30dSVlad Yasevich const struct in6_addr *group, 14466db6f0eaSFelix Fietkau __u16 vid, 14478b671779SNikolay Aleksandrov const unsigned char *src, 14488b671779SNikolay Aleksandrov bool mldv1) 144908b202b6SYOSHIFUJI Hideaki { 145008b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 14518b671779SNikolay Aleksandrov u8 filter_mode; 145208b202b6SYOSHIFUJI Hideaki 14533c3769e6SLinus Lüssing if (ipv6_addr_is_ll_all_nodes(group)) 145408b202b6SYOSHIFUJI Hideaki return 0; 145508b202b6SYOSHIFUJI Hideaki 145619e3a9c9SNikolay Aleksandrov memset(&br_group, 0, sizeof(br_group)); 1457eab3227bSNikolay Aleksandrov br_group.dst.ip6 = *group; 14589cc6e0c4SLinus Lüssing br_group.proto = htons(ETH_P_IPV6); 1459b0e9a30dSVlad Yasevich br_group.vid = vid; 14608b671779SNikolay Aleksandrov filter_mode = mldv1 ? MCAST_EXCLUDE : MCAST_INCLUDE; 146108b202b6SYOSHIFUJI Hideaki 1462adc47037SNikolay Aleksandrov return br_multicast_add_group(brmctx, pmctx, &br_group, src, 1463adc47037SNikolay Aleksandrov filter_mode, mldv1); 146408b202b6SYOSHIFUJI Hideaki } 146508b202b6SYOSHIFUJI Hideaki #endif 146608b202b6SYOSHIFUJI Hideaki 1467ed2d3597SLinus Lüssing static bool br_multicast_rport_del(struct hlist_node *rlist) 1468ed2d3597SLinus Lüssing { 1469ed2d3597SLinus Lüssing if (hlist_unhashed(rlist)) 1470ed2d3597SLinus Lüssing return false; 1471ed2d3597SLinus Lüssing 1472ed2d3597SLinus Lüssing hlist_del_init_rcu(rlist); 1473ed2d3597SLinus Lüssing return true; 1474ed2d3597SLinus Lüssing } 1475ed2d3597SLinus Lüssing 1476adc47037SNikolay Aleksandrov static bool br_ip4_multicast_rport_del(struct net_bridge_mcast_port *pmctx) 1477ed2d3597SLinus Lüssing { 1478adc47037SNikolay Aleksandrov return br_multicast_rport_del(&pmctx->ip4_rlist); 1479ed2d3597SLinus Lüssing } 1480ed2d3597SLinus Lüssing 1481adc47037SNikolay Aleksandrov static bool br_ip6_multicast_rport_del(struct net_bridge_mcast_port *pmctx) 1482a3c02e76SLinus Lüssing { 1483a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1484adc47037SNikolay Aleksandrov return br_multicast_rport_del(&pmctx->ip6_rlist); 1485a3c02e76SLinus Lüssing #else 1486a3c02e76SLinus Lüssing return false; 1487a3c02e76SLinus Lüssing #endif 1488a3c02e76SLinus Lüssing } 1489a3c02e76SLinus Lüssing 14909632233eSNikolay Aleksandrov static void br_multicast_router_expired(struct net_bridge_mcast_port *pmctx, 1491ee5fb222SLinus Lüssing struct timer_list *t, 1492ee5fb222SLinus Lüssing struct hlist_node *rlist) 1493eb1d1641SHerbert Xu { 14949632233eSNikolay Aleksandrov struct net_bridge *br = pmctx->port->br; 1495ed2d3597SLinus Lüssing bool del; 1496eb1d1641SHerbert Xu 1497eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 14989632233eSNikolay Aleksandrov if (pmctx->multicast_router == MDB_RTR_TYPE_DISABLED || 14999632233eSNikolay Aleksandrov pmctx->multicast_router == MDB_RTR_TYPE_PERM || 1500ee5fb222SLinus Lüssing timer_pending(t)) 1501eb1d1641SHerbert Xu goto out; 1502eb1d1641SHerbert Xu 1503ed2d3597SLinus Lüssing del = br_multicast_rport_del(rlist); 1504adc47037SNikolay Aleksandrov br_multicast_rport_del_notify(pmctx, del); 1505eb1d1641SHerbert Xu out: 1506eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1507eb1d1641SHerbert Xu } 1508eb1d1641SHerbert Xu 1509ee5fb222SLinus Lüssing static void br_ip4_multicast_router_expired(struct timer_list *t) 1510ee5fb222SLinus Lüssing { 15119632233eSNikolay Aleksandrov struct net_bridge_mcast_port *pmctx = from_timer(pmctx, t, 15129632233eSNikolay Aleksandrov ip4_mc_router_timer); 1513ee5fb222SLinus Lüssing 15149632233eSNikolay Aleksandrov br_multicast_router_expired(pmctx, t, &pmctx->ip4_rlist); 1515ee5fb222SLinus Lüssing } 1516ee5fb222SLinus Lüssing 1517a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1518a3c02e76SLinus Lüssing static void br_ip6_multicast_router_expired(struct timer_list *t) 1519a3c02e76SLinus Lüssing { 15209632233eSNikolay Aleksandrov struct net_bridge_mcast_port *pmctx = from_timer(pmctx, t, 15219632233eSNikolay Aleksandrov ip6_mc_router_timer); 1522a3c02e76SLinus Lüssing 15239632233eSNikolay Aleksandrov br_multicast_router_expired(pmctx, t, &pmctx->ip6_rlist); 1524a3c02e76SLinus Lüssing } 1525a3c02e76SLinus Lüssing #endif 1526a3c02e76SLinus Lüssing 152777041420SYotam Gigi static void br_mc_router_state_change(struct net_bridge *p, 152877041420SYotam Gigi bool is_mc_router) 152977041420SYotam Gigi { 153077041420SYotam Gigi struct switchdev_attr attr = { 153177041420SYotam Gigi .orig_dev = p->dev, 153277041420SYotam Gigi .id = SWITCHDEV_ATTR_ID_BRIDGE_MROUTER, 153377041420SYotam Gigi .flags = SWITCHDEV_F_DEFER, 153477041420SYotam Gigi .u.mrouter = is_mc_router, 153577041420SYotam Gigi }; 153677041420SYotam Gigi 1537dcbdf135SVladimir Oltean switchdev_port_attr_set(p->dev, &attr, NULL); 153877041420SYotam Gigi } 153977041420SYotam Gigi 1540d3d065c0SNikolay Aleksandrov static void br_multicast_local_router_expired(struct net_bridge_mcast *brmctx, 1541ee5fb222SLinus Lüssing struct timer_list *timer) 1542eb1d1641SHerbert Xu { 1543d3d065c0SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 1544d3d065c0SNikolay Aleksandrov if (brmctx->multicast_router == MDB_RTR_TYPE_DISABLED || 1545d3d065c0SNikolay Aleksandrov brmctx->multicast_router == MDB_RTR_TYPE_PERM || 1546d3d065c0SNikolay Aleksandrov br_ip4_multicast_is_router(brmctx) || 1547d3d065c0SNikolay Aleksandrov br_ip6_multicast_is_router(brmctx)) 154877041420SYotam Gigi goto out; 154977041420SYotam Gigi 1550d3d065c0SNikolay Aleksandrov br_mc_router_state_change(brmctx->br, false); 155177041420SYotam Gigi out: 1552d3d065c0SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 1553eb1d1641SHerbert Xu } 1554eb1d1641SHerbert Xu 1555ee5fb222SLinus Lüssing static void br_ip4_multicast_local_router_expired(struct timer_list *t) 1556ee5fb222SLinus Lüssing { 1557d3d065c0SNikolay Aleksandrov struct net_bridge_mcast *brmctx = from_timer(brmctx, t, 1558d3d065c0SNikolay Aleksandrov ip4_mc_router_timer); 1559ee5fb222SLinus Lüssing 1560d3d065c0SNikolay Aleksandrov br_multicast_local_router_expired(brmctx, t); 1561ee5fb222SLinus Lüssing } 1562ee5fb222SLinus Lüssing 1563a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1564a3c02e76SLinus Lüssing static void br_ip6_multicast_local_router_expired(struct timer_list *t) 1565a3c02e76SLinus Lüssing { 1566d3d065c0SNikolay Aleksandrov struct net_bridge_mcast *brmctx = from_timer(brmctx, t, 1567d3d065c0SNikolay Aleksandrov ip6_mc_router_timer); 1568a3c02e76SLinus Lüssing 1569d3d065c0SNikolay Aleksandrov br_multicast_local_router_expired(brmctx, t); 1570a3c02e76SLinus Lüssing } 1571a3c02e76SLinus Lüssing #endif 1572a3c02e76SLinus Lüssing 1573d3d065c0SNikolay Aleksandrov static void br_multicast_querier_expired(struct net_bridge_mcast *brmctx, 157490010b36SLinus Lüssing struct bridge_mcast_own_query *query) 1575c83b8fabSHerbert Xu { 1576d3d065c0SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 1577d3d065c0SNikolay Aleksandrov if (!netif_running(brmctx->br->dev) || 15784cdd0d10SNikolay Aleksandrov br_multicast_ctx_vlan_global_disabled(brmctx) || 1579d3d065c0SNikolay Aleksandrov !br_opt_get(brmctx->br, BROPT_MULTICAST_ENABLED)) 1580c83b8fabSHerbert Xu goto out; 1581c83b8fabSHerbert Xu 1582adc47037SNikolay Aleksandrov br_multicast_start_querier(brmctx, query); 1583c83b8fabSHerbert Xu 1584c83b8fabSHerbert Xu out: 1585d3d065c0SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 1586c83b8fabSHerbert Xu } 1587c83b8fabSHerbert Xu 158888c1f37fSAllen Pais static void br_ip4_multicast_querier_expired(struct timer_list *t) 1589cc0fdd80SLinus Lüssing { 1590d3d065c0SNikolay Aleksandrov struct net_bridge_mcast *brmctx = from_timer(brmctx, t, 1591d3d065c0SNikolay Aleksandrov ip4_other_query.timer); 1592cc0fdd80SLinus Lüssing 1593d3d065c0SNikolay Aleksandrov br_multicast_querier_expired(brmctx, &brmctx->ip4_own_query); 1594cc0fdd80SLinus Lüssing } 1595cc0fdd80SLinus Lüssing 1596cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 159788c1f37fSAllen Pais static void br_ip6_multicast_querier_expired(struct timer_list *t) 1598cc0fdd80SLinus Lüssing { 1599d3d065c0SNikolay Aleksandrov struct net_bridge_mcast *brmctx = from_timer(brmctx, t, 1600d3d065c0SNikolay Aleksandrov ip6_other_query.timer); 1601cc0fdd80SLinus Lüssing 1602d3d065c0SNikolay Aleksandrov br_multicast_querier_expired(brmctx, &brmctx->ip6_own_query); 1603cc0fdd80SLinus Lüssing } 1604cc0fdd80SLinus Lüssing #endif 1605cc0fdd80SLinus Lüssing 1606adc47037SNikolay Aleksandrov static void br_multicast_select_own_querier(struct net_bridge_mcast *brmctx, 1607dc4eb53aSLinus Lüssing struct br_ip *ip, 1608dc4eb53aSLinus Lüssing struct sk_buff *skb) 1609dc4eb53aSLinus Lüssing { 1610dc4eb53aSLinus Lüssing if (ip->proto == htons(ETH_P_IP)) 1611d3d065c0SNikolay Aleksandrov brmctx->ip4_querier.addr.src.ip4 = ip_hdr(skb)->saddr; 1612dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1613dc4eb53aSLinus Lüssing else 1614d3d065c0SNikolay Aleksandrov brmctx->ip6_querier.addr.src.ip6 = ipv6_hdr(skb)->saddr; 1615dc4eb53aSLinus Lüssing #endif 1616dc4eb53aSLinus Lüssing } 1617dc4eb53aSLinus Lüssing 1618adc47037SNikolay Aleksandrov static void __br_multicast_send_query(struct net_bridge_mcast *brmctx, 1619adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 1620438ef2d0SNikolay Aleksandrov struct net_bridge_port_group *pg, 1621438ef2d0SNikolay Aleksandrov struct br_ip *ip_dst, 1622438ef2d0SNikolay Aleksandrov struct br_ip *group, 1623438ef2d0SNikolay Aleksandrov bool with_srcs, 162442c11ccfSNikolay Aleksandrov u8 sflag, 162542c11ccfSNikolay Aleksandrov bool *need_rexmit) 1626eb1d1641SHerbert Xu { 1627438ef2d0SNikolay Aleksandrov bool over_lmqt = !!sflag; 1628eb1d1641SHerbert Xu struct sk_buff *skb; 16291080ab95SNikolay Aleksandrov u8 igmp_type; 1630eb1d1641SHerbert Xu 1631cb486ce9SNikolay Aleksandrov if (!br_multicast_ctx_should_use(brmctx, pmctx) || 1632cb486ce9SNikolay Aleksandrov !br_multicast_ctx_matches_vlan_snooping(brmctx)) 1633615cc23eSNikolay Aleksandrov return; 1634615cc23eSNikolay Aleksandrov 1635438ef2d0SNikolay Aleksandrov again_under_lmqt: 1636615cc23eSNikolay Aleksandrov skb = br_multicast_alloc_query(brmctx, pmctx, pg, ip_dst, group, 1637615cc23eSNikolay Aleksandrov with_srcs, over_lmqt, sflag, &igmp_type, 163842c11ccfSNikolay Aleksandrov need_rexmit); 1639eb1d1641SHerbert Xu if (!skb) 16408ef2a9a5SYOSHIFUJI Hideaki return; 1641eb1d1641SHerbert Xu 1642adc47037SNikolay Aleksandrov if (pmctx) { 1643adc47037SNikolay Aleksandrov skb->dev = pmctx->port->dev; 1644adc47037SNikolay Aleksandrov br_multicast_count(brmctx->br, pmctx->port, skb, igmp_type, 16451080ab95SNikolay Aleksandrov BR_MCAST_DIR_TX); 164629a26a56SEric W. Biederman NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, 1647adc47037SNikolay Aleksandrov dev_net(pmctx->port->dev), NULL, skb, NULL, skb->dev, 1648f0b4eeceSLinus Lüssing br_dev_queue_push_xmit); 1649438ef2d0SNikolay Aleksandrov 1650438ef2d0SNikolay Aleksandrov if (over_lmqt && with_srcs && sflag) { 1651438ef2d0SNikolay Aleksandrov over_lmqt = false; 1652438ef2d0SNikolay Aleksandrov goto again_under_lmqt; 1653438ef2d0SNikolay Aleksandrov } 1654dc4eb53aSLinus Lüssing } else { 1655adc47037SNikolay Aleksandrov br_multicast_select_own_querier(brmctx, group, skb); 1656adc47037SNikolay Aleksandrov br_multicast_count(brmctx->br, NULL, skb, igmp_type, 16571080ab95SNikolay Aleksandrov BR_MCAST_DIR_RX); 1658eb1d1641SHerbert Xu netif_rx(skb); 16598ef2a9a5SYOSHIFUJI Hideaki } 1660dc4eb53aSLinus Lüssing } 1661eb1d1641SHerbert Xu 166267b746f9SNikolay Aleksandrov static void br_multicast_read_querier(const struct bridge_mcast_querier *querier, 166367b746f9SNikolay Aleksandrov struct bridge_mcast_querier *dest) 166467b746f9SNikolay Aleksandrov { 166567b746f9SNikolay Aleksandrov unsigned int seq; 166667b746f9SNikolay Aleksandrov 166767b746f9SNikolay Aleksandrov memset(dest, 0, sizeof(*dest)); 166867b746f9SNikolay Aleksandrov do { 166967b746f9SNikolay Aleksandrov seq = read_seqcount_begin(&querier->seq); 167067b746f9SNikolay Aleksandrov dest->port_ifidx = querier->port_ifidx; 167167b746f9SNikolay Aleksandrov memcpy(&dest->addr, &querier->addr, sizeof(struct br_ip)); 167267b746f9SNikolay Aleksandrov } while (read_seqcount_retry(&querier->seq, seq)); 167367b746f9SNikolay Aleksandrov } 167467b746f9SNikolay Aleksandrov 167567b746f9SNikolay Aleksandrov static void br_multicast_update_querier(struct net_bridge_mcast *brmctx, 167667b746f9SNikolay Aleksandrov struct bridge_mcast_querier *querier, 167767b746f9SNikolay Aleksandrov int ifindex, 167867b746f9SNikolay Aleksandrov struct br_ip *saddr) 167967b746f9SNikolay Aleksandrov { 168067b746f9SNikolay Aleksandrov lockdep_assert_held_once(&brmctx->br->multicast_lock); 168167b746f9SNikolay Aleksandrov 168267b746f9SNikolay Aleksandrov write_seqcount_begin(&querier->seq); 168367b746f9SNikolay Aleksandrov querier->port_ifidx = ifindex; 168467b746f9SNikolay Aleksandrov memcpy(&querier->addr, saddr, sizeof(*saddr)); 168567b746f9SNikolay Aleksandrov write_seqcount_end(&querier->seq); 168667b746f9SNikolay Aleksandrov } 168767b746f9SNikolay Aleksandrov 1688adc47037SNikolay Aleksandrov static void br_multicast_send_query(struct net_bridge_mcast *brmctx, 1689adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 169090010b36SLinus Lüssing struct bridge_mcast_own_query *own_query) 16918ef2a9a5SYOSHIFUJI Hideaki { 169290010b36SLinus Lüssing struct bridge_mcast_other_query *other_query = NULL; 169367b746f9SNikolay Aleksandrov struct bridge_mcast_querier *querier; 16945e923585SNikolay Aleksandrov struct br_ip br_group; 16955e923585SNikolay Aleksandrov unsigned long time; 16968ef2a9a5SYOSHIFUJI Hideaki 16974cdd0d10SNikolay Aleksandrov if (!br_multicast_ctx_should_use(brmctx, pmctx) || 1698adc47037SNikolay Aleksandrov !br_opt_get(brmctx->br, BROPT_MULTICAST_ENABLED) || 16994d5b4e84SNikolay Aleksandrov !brmctx->multicast_querier) 17008ef2a9a5SYOSHIFUJI Hideaki return; 17018ef2a9a5SYOSHIFUJI Hideaki 1702eab3227bSNikolay Aleksandrov memset(&br_group.dst, 0, sizeof(br_group.dst)); 17038ef2a9a5SYOSHIFUJI Hideaki 1704adc47037SNikolay Aleksandrov if (pmctx ? (own_query == &pmctx->ip4_own_query) : 1705d3d065c0SNikolay Aleksandrov (own_query == &brmctx->ip4_own_query)) { 170667b746f9SNikolay Aleksandrov querier = &brmctx->ip4_querier; 1707d3d065c0SNikolay Aleksandrov other_query = &brmctx->ip4_other_query; 170808b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 1709dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 1710cc0fdd80SLinus Lüssing } else { 171167b746f9SNikolay Aleksandrov querier = &brmctx->ip6_querier; 1712d3d065c0SNikolay Aleksandrov other_query = &brmctx->ip6_other_query; 171308b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 171408b202b6SYOSHIFUJI Hideaki #endif 1715eb1d1641SHerbert Xu } 1716eb1d1641SHerbert Xu 171790010b36SLinus Lüssing if (!other_query || timer_pending(&other_query->timer)) 1718cc0fdd80SLinus Lüssing return; 1719cc0fdd80SLinus Lüssing 172067b746f9SNikolay Aleksandrov /* we're about to select ourselves as querier */ 172167b746f9SNikolay Aleksandrov if (!pmctx && querier->port_ifidx) { 172267b746f9SNikolay Aleksandrov struct br_ip zeroip = {}; 172367b746f9SNikolay Aleksandrov 172467b746f9SNikolay Aleksandrov br_multicast_update_querier(brmctx, querier, 0, &zeroip); 172567b746f9SNikolay Aleksandrov } 172667b746f9SNikolay Aleksandrov 1727adc47037SNikolay Aleksandrov __br_multicast_send_query(brmctx, pmctx, NULL, NULL, &br_group, false, 1728adc47037SNikolay Aleksandrov 0, NULL); 1729cc0fdd80SLinus Lüssing 1730cc0fdd80SLinus Lüssing time = jiffies; 1731d3d065c0SNikolay Aleksandrov time += own_query->startup_sent < brmctx->multicast_startup_query_count ? 1732d3d065c0SNikolay Aleksandrov brmctx->multicast_startup_query_interval : 1733d3d065c0SNikolay Aleksandrov brmctx->multicast_query_interval; 173490010b36SLinus Lüssing mod_timer(&own_query->timer, time); 1735cc0fdd80SLinus Lüssing } 1736cc0fdd80SLinus Lüssing 173790010b36SLinus Lüssing static void 17389632233eSNikolay Aleksandrov br_multicast_port_query_expired(struct net_bridge_mcast_port *pmctx, 173990010b36SLinus Lüssing struct bridge_mcast_own_query *query) 1740eb1d1641SHerbert Xu { 17419632233eSNikolay Aleksandrov struct net_bridge *br = pmctx->port->br; 17424cdd0d10SNikolay Aleksandrov struct net_bridge_mcast *brmctx; 1743eb1d1641SHerbert Xu 1744eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 17454cdd0d10SNikolay Aleksandrov if (br_multicast_port_ctx_state_stopped(pmctx)) 1746eb1d1641SHerbert Xu goto out; 1747615cc23eSNikolay Aleksandrov 17484cdd0d10SNikolay Aleksandrov brmctx = br_multicast_port_ctx_get_global(pmctx); 17494cdd0d10SNikolay Aleksandrov if (query->startup_sent < brmctx->multicast_startup_query_count) 1750cc0fdd80SLinus Lüssing query->startup_sent++; 1751eb1d1641SHerbert Xu 17524cdd0d10SNikolay Aleksandrov br_multicast_send_query(brmctx, pmctx, query); 1753eb1d1641SHerbert Xu 1754eb1d1641SHerbert Xu out: 1755eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1756eb1d1641SHerbert Xu } 1757eb1d1641SHerbert Xu 175888c1f37fSAllen Pais static void br_ip4_multicast_port_query_expired(struct timer_list *t) 1759cc0fdd80SLinus Lüssing { 17609632233eSNikolay Aleksandrov struct net_bridge_mcast_port *pmctx = from_timer(pmctx, t, 17619632233eSNikolay Aleksandrov ip4_own_query.timer); 1762cc0fdd80SLinus Lüssing 17639632233eSNikolay Aleksandrov br_multicast_port_query_expired(pmctx, &pmctx->ip4_own_query); 1764cc0fdd80SLinus Lüssing } 1765cc0fdd80SLinus Lüssing 1766cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 176788c1f37fSAllen Pais static void br_ip6_multicast_port_query_expired(struct timer_list *t) 1768cc0fdd80SLinus Lüssing { 17699632233eSNikolay Aleksandrov struct net_bridge_mcast_port *pmctx = from_timer(pmctx, t, 17709632233eSNikolay Aleksandrov ip6_own_query.timer); 1771cc0fdd80SLinus Lüssing 17729632233eSNikolay Aleksandrov br_multicast_port_query_expired(pmctx, &pmctx->ip6_own_query); 1773cc0fdd80SLinus Lüssing } 1774cc0fdd80SLinus Lüssing #endif 1775cc0fdd80SLinus Lüssing 177642c11ccfSNikolay Aleksandrov static void br_multicast_port_group_rexmit(struct timer_list *t) 177742c11ccfSNikolay Aleksandrov { 177842c11ccfSNikolay Aleksandrov struct net_bridge_port_group *pg = from_timer(pg, t, rexmit_timer); 177942c11ccfSNikolay Aleksandrov struct bridge_mcast_other_query *other_query = NULL; 1780085b53c8SNikolay Aleksandrov struct net_bridge *br = pg->key.port->br; 1781adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx; 1782adc47037SNikolay Aleksandrov struct net_bridge_mcast *brmctx; 178342c11ccfSNikolay Aleksandrov bool need_rexmit = false; 178442c11ccfSNikolay Aleksandrov 178542c11ccfSNikolay Aleksandrov spin_lock(&br->multicast_lock); 178642c11ccfSNikolay Aleksandrov if (!netif_running(br->dev) || hlist_unhashed(&pg->mglist) || 17874d5b4e84SNikolay Aleksandrov !br_opt_get(br, BROPT_MULTICAST_ENABLED)) 178842c11ccfSNikolay Aleksandrov goto out; 178942c11ccfSNikolay Aleksandrov 1790eb1593a0SNikolay Aleksandrov pmctx = br_multicast_pg_to_port_ctx(pg); 1791eb1593a0SNikolay Aleksandrov if (!pmctx) 1792eb1593a0SNikolay Aleksandrov goto out; 1793eb1593a0SNikolay Aleksandrov brmctx = br_multicast_port_ctx_get_global(pmctx); 17944d5b4e84SNikolay Aleksandrov if (!brmctx->multicast_querier) 17954d5b4e84SNikolay Aleksandrov goto out; 17964d5b4e84SNikolay Aleksandrov 1797085b53c8SNikolay Aleksandrov if (pg->key.addr.proto == htons(ETH_P_IP)) 1798d3d065c0SNikolay Aleksandrov other_query = &brmctx->ip4_other_query; 179942c11ccfSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 180042c11ccfSNikolay Aleksandrov else 1801d3d065c0SNikolay Aleksandrov other_query = &brmctx->ip6_other_query; 180242c11ccfSNikolay Aleksandrov #endif 180342c11ccfSNikolay Aleksandrov 180442c11ccfSNikolay Aleksandrov if (!other_query || timer_pending(&other_query->timer)) 180542c11ccfSNikolay Aleksandrov goto out; 180642c11ccfSNikolay Aleksandrov 180742c11ccfSNikolay Aleksandrov if (pg->grp_query_rexmit_cnt) { 180842c11ccfSNikolay Aleksandrov pg->grp_query_rexmit_cnt--; 1809adc47037SNikolay Aleksandrov __br_multicast_send_query(brmctx, pmctx, pg, &pg->key.addr, 1810085b53c8SNikolay Aleksandrov &pg->key.addr, false, 1, NULL); 181142c11ccfSNikolay Aleksandrov } 1812adc47037SNikolay Aleksandrov __br_multicast_send_query(brmctx, pmctx, pg, &pg->key.addr, 1813085b53c8SNikolay Aleksandrov &pg->key.addr, true, 0, &need_rexmit); 181442c11ccfSNikolay Aleksandrov 181542c11ccfSNikolay Aleksandrov if (pg->grp_query_rexmit_cnt || need_rexmit) 181642c11ccfSNikolay Aleksandrov mod_timer(&pg->rexmit_timer, jiffies + 1817d3d065c0SNikolay Aleksandrov brmctx->multicast_last_member_interval); 181842c11ccfSNikolay Aleksandrov out: 181942c11ccfSNikolay Aleksandrov spin_unlock(&br->multicast_lock); 182042c11ccfSNikolay Aleksandrov } 182142c11ccfSNikolay Aleksandrov 1822ae1ea84bSFlorian Fainelli static int br_mc_disabled_update(struct net_device *dev, bool value, 1823ae1ea84bSFlorian Fainelli struct netlink_ext_ack *extack) 1824147c1e9bSNogah Frankel { 1825147c1e9bSNogah Frankel struct switchdev_attr attr = { 1826147c1e9bSNogah Frankel .orig_dev = dev, 1827147c1e9bSNogah Frankel .id = SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED, 1828147c1e9bSNogah Frankel .flags = SWITCHDEV_F_DEFER, 182913cefad2SNikolay Aleksandrov .u.mc_disabled = !value, 1830147c1e9bSNogah Frankel }; 1831147c1e9bSNogah Frankel 1832ae1ea84bSFlorian Fainelli return switchdev_port_attr_set(dev, &attr, extack); 1833147c1e9bSNogah Frankel } 1834147c1e9bSNogah Frankel 1835613d61dbSNikolay Aleksandrov void br_multicast_port_ctx_init(struct net_bridge_port *port, 1836613d61dbSNikolay Aleksandrov struct net_bridge_vlan *vlan, 18379632233eSNikolay Aleksandrov struct net_bridge_mcast_port *pmctx) 18389632233eSNikolay Aleksandrov { 18399632233eSNikolay Aleksandrov pmctx->port = port; 1840613d61dbSNikolay Aleksandrov pmctx->vlan = vlan; 18419632233eSNikolay Aleksandrov pmctx->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 18429632233eSNikolay Aleksandrov timer_setup(&pmctx->ip4_mc_router_timer, 18439632233eSNikolay Aleksandrov br_ip4_multicast_router_expired, 0); 18449632233eSNikolay Aleksandrov timer_setup(&pmctx->ip4_own_query.timer, 18459632233eSNikolay Aleksandrov br_ip4_multicast_port_query_expired, 0); 18469632233eSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 18479632233eSNikolay Aleksandrov timer_setup(&pmctx->ip6_mc_router_timer, 18489632233eSNikolay Aleksandrov br_ip6_multicast_router_expired, 0); 18499632233eSNikolay Aleksandrov timer_setup(&pmctx->ip6_own_query.timer, 18509632233eSNikolay Aleksandrov br_ip6_multicast_port_query_expired, 0); 18519632233eSNikolay Aleksandrov #endif 18529632233eSNikolay Aleksandrov } 18539632233eSNikolay Aleksandrov 1854613d61dbSNikolay Aleksandrov void br_multicast_port_ctx_deinit(struct net_bridge_mcast_port *pmctx) 18559632233eSNikolay Aleksandrov { 18569632233eSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 18579632233eSNikolay Aleksandrov del_timer_sync(&pmctx->ip6_mc_router_timer); 18589632233eSNikolay Aleksandrov #endif 18599632233eSNikolay Aleksandrov del_timer_sync(&pmctx->ip4_mc_router_timer); 18609632233eSNikolay Aleksandrov } 18619632233eSNikolay Aleksandrov 18621080ab95SNikolay Aleksandrov int br_multicast_add_port(struct net_bridge_port *port) 1863eb1d1641SHerbert Xu { 1864ae1ea84bSFlorian Fainelli int err; 1865ae1ea84bSFlorian Fainelli 186689268b05SNikolay Aleksandrov port->multicast_eht_hosts_limit = BR_MCAST_DEFAULT_EHT_HOSTS_LIMIT; 1867613d61dbSNikolay Aleksandrov br_multicast_port_ctx_init(port, NULL, &port->multicast_ctx); 1868eb1d1641SHerbert Xu 1869ae1ea84bSFlorian Fainelli err = br_mc_disabled_update(port->dev, 1870ae1ea84bSFlorian Fainelli br_opt_get(port->br, 1871ae1ea84bSFlorian Fainelli BROPT_MULTICAST_ENABLED), 1872ae1ea84bSFlorian Fainelli NULL); 187368f5c12aSVladimir Oltean if (err && err != -EOPNOTSUPP) 1874ae1ea84bSFlorian Fainelli return err; 1875147c1e9bSNogah Frankel 18761080ab95SNikolay Aleksandrov port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats); 18771080ab95SNikolay Aleksandrov if (!port->mcast_stats) 18781080ab95SNikolay Aleksandrov return -ENOMEM; 18791080ab95SNikolay Aleksandrov 18801080ab95SNikolay Aleksandrov return 0; 1881eb1d1641SHerbert Xu } 1882eb1d1641SHerbert Xu 1883eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port) 1884eb1d1641SHerbert Xu { 1885e10177abSSatish Ashok struct net_bridge *br = port->br; 1886e10177abSSatish Ashok struct net_bridge_port_group *pg; 1887e12cec65SNikolay Aleksandrov HLIST_HEAD(deleted_head); 1888e10177abSSatish Ashok struct hlist_node *n; 1889e10177abSSatish Ashok 1890e10177abSSatish Ashok /* Take care of the remaining groups, only perm ones should be left */ 1891e10177abSSatish Ashok spin_lock_bh(&br->multicast_lock); 1892e10177abSSatish Ashok hlist_for_each_entry_safe(pg, n, &port->mglist, mglist) 1893681590bdSNikolay Aleksandrov br_multicast_find_del_pg(br, pg); 1894e12cec65SNikolay Aleksandrov hlist_move_list(&br->mcast_gc_list, &deleted_head); 1895e10177abSSatish Ashok spin_unlock_bh(&br->multicast_lock); 1896e12cec65SNikolay Aleksandrov br_multicast_gc(&deleted_head); 18979632233eSNikolay Aleksandrov br_multicast_port_ctx_deinit(&port->multicast_ctx); 18981080ab95SNikolay Aleksandrov free_percpu(port->mcast_stats); 1899eb1d1641SHerbert Xu } 1900eb1d1641SHerbert Xu 190190010b36SLinus Lüssing static void br_multicast_enable(struct bridge_mcast_own_query *query) 1902561f1103SHerbert Xu { 1903cc0fdd80SLinus Lüssing query->startup_sent = 0; 1904561f1103SHerbert Xu 1905cc0fdd80SLinus Lüssing if (try_to_del_timer_sync(&query->timer) >= 0 || 1906cc0fdd80SLinus Lüssing del_timer(&query->timer)) 1907cc0fdd80SLinus Lüssing mod_timer(&query->timer, jiffies); 1908561f1103SHerbert Xu } 1909561f1103SHerbert Xu 1910adc47037SNikolay Aleksandrov static void __br_multicast_enable_port_ctx(struct net_bridge_mcast_port *pmctx) 1911eb1d1641SHerbert Xu { 1912adc47037SNikolay Aleksandrov struct net_bridge *br = pmctx->port->br; 19137b54aaafSNikolay Aleksandrov struct net_bridge_mcast *brmctx; 1914eb1d1641SHerbert Xu 19157b54aaafSNikolay Aleksandrov brmctx = br_multicast_port_ctx_get_global(pmctx); 19167b54aaafSNikolay Aleksandrov if (!br_opt_get(br, BROPT_MULTICAST_ENABLED) || 19177b54aaafSNikolay Aleksandrov !netif_running(br->dev)) 19187cb3f921SNikolay Aleksandrov return; 1919eb1d1641SHerbert Xu 1920adc47037SNikolay Aleksandrov br_multicast_enable(&pmctx->ip4_own_query); 1921cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1922adc47037SNikolay Aleksandrov br_multicast_enable(&pmctx->ip6_own_query); 1923cc0fdd80SLinus Lüssing #endif 1924adc47037SNikolay Aleksandrov if (pmctx->multicast_router == MDB_RTR_TYPE_PERM) { 1925adc47037SNikolay Aleksandrov br_ip4_multicast_add_router(brmctx, pmctx); 1926adc47037SNikolay Aleksandrov br_ip6_multicast_add_router(brmctx, pmctx); 1927a3c02e76SLinus Lüssing } 19287cb3f921SNikolay Aleksandrov } 1929eb1d1641SHerbert Xu 19307cb3f921SNikolay Aleksandrov void br_multicast_enable_port(struct net_bridge_port *port) 19317cb3f921SNikolay Aleksandrov { 19327cb3f921SNikolay Aleksandrov struct net_bridge *br = port->br; 19337cb3f921SNikolay Aleksandrov 1934f4b7002aSNikolay Aleksandrov spin_lock_bh(&br->multicast_lock); 1935adc47037SNikolay Aleksandrov __br_multicast_enable_port_ctx(&port->multicast_ctx); 1936f4b7002aSNikolay Aleksandrov spin_unlock_bh(&br->multicast_lock); 1937eb1d1641SHerbert Xu } 1938eb1d1641SHerbert Xu 19397b54aaafSNikolay Aleksandrov static void __br_multicast_disable_port_ctx(struct net_bridge_mcast_port *pmctx) 1940eb1d1641SHerbert Xu { 1941eb1d1641SHerbert Xu struct net_bridge_port_group *pg; 1942b67bfe0dSSasha Levin struct hlist_node *n; 1943ed2d3597SLinus Lüssing bool del = false; 1944eb1d1641SHerbert Xu 19457b54aaafSNikolay Aleksandrov hlist_for_each_entry_safe(pg, n, &pmctx->port->mglist, mglist) 19467b54aaafSNikolay Aleksandrov if (!(pg->flags & MDB_PG_FLAGS_PERMANENT) && 19477b54aaafSNikolay Aleksandrov (!br_multicast_port_ctx_is_vlan(pmctx) || 19487b54aaafSNikolay Aleksandrov pg->key.addr.vid == pmctx->vlan->vid)) 19497b54aaafSNikolay Aleksandrov br_multicast_find_del_pg(pmctx->port->br, pg); 1950eb1d1641SHerbert Xu 1951adc47037SNikolay Aleksandrov del |= br_ip4_multicast_rport_del(pmctx); 1952adc47037SNikolay Aleksandrov del_timer(&pmctx->ip4_mc_router_timer); 1953adc47037SNikolay Aleksandrov del_timer(&pmctx->ip4_own_query.timer); 1954adc47037SNikolay Aleksandrov del |= br_ip6_multicast_rport_del(pmctx); 1955cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1956adc47037SNikolay Aleksandrov del_timer(&pmctx->ip6_mc_router_timer); 1957adc47037SNikolay Aleksandrov del_timer(&pmctx->ip6_own_query.timer); 1958cc0fdd80SLinus Lüssing #endif 1959adc47037SNikolay Aleksandrov br_multicast_rport_del_notify(pmctx, del); 19607b54aaafSNikolay Aleksandrov } 19617b54aaafSNikolay Aleksandrov 19627b54aaafSNikolay Aleksandrov void br_multicast_disable_port(struct net_bridge_port *port) 19637b54aaafSNikolay Aleksandrov { 1964f4b7002aSNikolay Aleksandrov spin_lock_bh(&port->br->multicast_lock); 19657b54aaafSNikolay Aleksandrov __br_multicast_disable_port_ctx(&port->multicast_ctx); 1966f4b7002aSNikolay Aleksandrov spin_unlock_bh(&port->br->multicast_lock); 1967eb1d1641SHerbert Xu } 1968eb1d1641SHerbert Xu 1969e6231bcaSNikolay Aleksandrov static int __grp_src_delete_marked(struct net_bridge_port_group *pg) 1970e6231bcaSNikolay Aleksandrov { 1971e6231bcaSNikolay Aleksandrov struct net_bridge_group_src *ent; 1972e6231bcaSNikolay Aleksandrov struct hlist_node *tmp; 1973e6231bcaSNikolay Aleksandrov int deleted = 0; 1974e6231bcaSNikolay Aleksandrov 1975e6231bcaSNikolay Aleksandrov hlist_for_each_entry_safe(ent, tmp, &pg->src_list, node) 1976e6231bcaSNikolay Aleksandrov if (ent->flags & BR_SGRP_F_DELETE) { 1977d5a10222SNikolay Aleksandrov br_multicast_del_group_src(ent, false); 1978e6231bcaSNikolay Aleksandrov deleted++; 1979e6231bcaSNikolay Aleksandrov } 1980e6231bcaSNikolay Aleksandrov 1981e6231bcaSNikolay Aleksandrov return deleted; 1982e6231bcaSNikolay Aleksandrov } 1983e6231bcaSNikolay Aleksandrov 1984b0812368SNikolay Aleksandrov static void __grp_src_mod_timer(struct net_bridge_group_src *src, 1985b0812368SNikolay Aleksandrov unsigned long expires) 1986b0812368SNikolay Aleksandrov { 1987b0812368SNikolay Aleksandrov mod_timer(&src->timer, expires); 1988b0812368SNikolay Aleksandrov br_multicast_fwd_src_handle(src); 1989b0812368SNikolay Aleksandrov } 1990b0812368SNikolay Aleksandrov 1991adc47037SNikolay Aleksandrov static void __grp_src_query_marked_and_rexmit(struct net_bridge_mcast *brmctx, 1992adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 1993adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg) 19945bf1e00bSNikolay Aleksandrov { 19955bf1e00bSNikolay Aleksandrov struct bridge_mcast_other_query *other_query = NULL; 1996d3d065c0SNikolay Aleksandrov u32 lmqc = brmctx->multicast_last_member_count; 19975bf1e00bSNikolay Aleksandrov unsigned long lmqt, lmi, now = jiffies; 19985bf1e00bSNikolay Aleksandrov struct net_bridge_group_src *ent; 19995bf1e00bSNikolay Aleksandrov 2000adc47037SNikolay Aleksandrov if (!netif_running(brmctx->br->dev) || 2001adc47037SNikolay Aleksandrov !br_opt_get(brmctx->br, BROPT_MULTICAST_ENABLED)) 20025bf1e00bSNikolay Aleksandrov return; 20035bf1e00bSNikolay Aleksandrov 2004085b53c8SNikolay Aleksandrov if (pg->key.addr.proto == htons(ETH_P_IP)) 2005d3d065c0SNikolay Aleksandrov other_query = &brmctx->ip4_other_query; 20065bf1e00bSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 20075bf1e00bSNikolay Aleksandrov else 2008d3d065c0SNikolay Aleksandrov other_query = &brmctx->ip6_other_query; 20095bf1e00bSNikolay Aleksandrov #endif 20105bf1e00bSNikolay Aleksandrov 2011adc47037SNikolay Aleksandrov lmqt = now + br_multicast_lmqt(brmctx); 20125bf1e00bSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) { 20135bf1e00bSNikolay Aleksandrov if (ent->flags & BR_SGRP_F_SEND) { 20145bf1e00bSNikolay Aleksandrov ent->flags &= ~BR_SGRP_F_SEND; 20155bf1e00bSNikolay Aleksandrov if (ent->timer.expires > lmqt) { 20164d5b4e84SNikolay Aleksandrov if (brmctx->multicast_querier && 20175bf1e00bSNikolay Aleksandrov other_query && 20185bf1e00bSNikolay Aleksandrov !timer_pending(&other_query->timer)) 20195bf1e00bSNikolay Aleksandrov ent->src_query_rexmit_cnt = lmqc; 2020b0812368SNikolay Aleksandrov __grp_src_mod_timer(ent, lmqt); 20215bf1e00bSNikolay Aleksandrov } 20225bf1e00bSNikolay Aleksandrov } 20235bf1e00bSNikolay Aleksandrov } 20245bf1e00bSNikolay Aleksandrov 20254d5b4e84SNikolay Aleksandrov if (!brmctx->multicast_querier || 20265bf1e00bSNikolay Aleksandrov !other_query || timer_pending(&other_query->timer)) 20275bf1e00bSNikolay Aleksandrov return; 20285bf1e00bSNikolay Aleksandrov 2029adc47037SNikolay Aleksandrov __br_multicast_send_query(brmctx, pmctx, pg, &pg->key.addr, 2030085b53c8SNikolay Aleksandrov &pg->key.addr, true, 1, NULL); 20315bf1e00bSNikolay Aleksandrov 2032d3d065c0SNikolay Aleksandrov lmi = now + brmctx->multicast_last_member_interval; 20335bf1e00bSNikolay Aleksandrov if (!timer_pending(&pg->rexmit_timer) || 20345bf1e00bSNikolay Aleksandrov time_after(pg->rexmit_timer.expires, lmi)) 20355bf1e00bSNikolay Aleksandrov mod_timer(&pg->rexmit_timer, lmi); 20365bf1e00bSNikolay Aleksandrov } 20375bf1e00bSNikolay Aleksandrov 2038adc47037SNikolay Aleksandrov static void __grp_send_query_and_rexmit(struct net_bridge_mcast *brmctx, 2039adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 2040adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg) 20415bf1e00bSNikolay Aleksandrov { 20425bf1e00bSNikolay Aleksandrov struct bridge_mcast_other_query *other_query = NULL; 20435bf1e00bSNikolay Aleksandrov unsigned long now = jiffies, lmi; 20445bf1e00bSNikolay Aleksandrov 2045adc47037SNikolay Aleksandrov if (!netif_running(brmctx->br->dev) || 2046adc47037SNikolay Aleksandrov !br_opt_get(brmctx->br, BROPT_MULTICAST_ENABLED)) 20475bf1e00bSNikolay Aleksandrov return; 20485bf1e00bSNikolay Aleksandrov 2049085b53c8SNikolay Aleksandrov if (pg->key.addr.proto == htons(ETH_P_IP)) 2050d3d065c0SNikolay Aleksandrov other_query = &brmctx->ip4_other_query; 20515bf1e00bSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 20525bf1e00bSNikolay Aleksandrov else 2053d3d065c0SNikolay Aleksandrov other_query = &brmctx->ip6_other_query; 20545bf1e00bSNikolay Aleksandrov #endif 20555bf1e00bSNikolay Aleksandrov 20564d5b4e84SNikolay Aleksandrov if (brmctx->multicast_querier && 20575bf1e00bSNikolay Aleksandrov other_query && !timer_pending(&other_query->timer)) { 2058d3d065c0SNikolay Aleksandrov lmi = now + brmctx->multicast_last_member_interval; 2059d3d065c0SNikolay Aleksandrov pg->grp_query_rexmit_cnt = brmctx->multicast_last_member_count - 1; 2060adc47037SNikolay Aleksandrov __br_multicast_send_query(brmctx, pmctx, pg, &pg->key.addr, 2061085b53c8SNikolay Aleksandrov &pg->key.addr, false, 0, NULL); 20625bf1e00bSNikolay Aleksandrov if (!timer_pending(&pg->rexmit_timer) || 20635bf1e00bSNikolay Aleksandrov time_after(pg->rexmit_timer.expires, lmi)) 20645bf1e00bSNikolay Aleksandrov mod_timer(&pg->rexmit_timer, lmi); 20655bf1e00bSNikolay Aleksandrov } 20665bf1e00bSNikolay Aleksandrov 20675bf1e00bSNikolay Aleksandrov if (pg->filter_mode == MCAST_EXCLUDE && 20685bf1e00bSNikolay Aleksandrov (!timer_pending(&pg->timer) || 2069adc47037SNikolay Aleksandrov time_after(pg->timer.expires, now + br_multicast_lmqt(brmctx)))) 2070adc47037SNikolay Aleksandrov mod_timer(&pg->timer, now + br_multicast_lmqt(brmctx)); 20715bf1e00bSNikolay Aleksandrov } 20725bf1e00bSNikolay Aleksandrov 20730436862eSNikolay Aleksandrov /* State Msg type New state Actions 20740436862eSNikolay Aleksandrov * INCLUDE (A) IS_IN (B) INCLUDE (A+B) (B)=GMI 20750436862eSNikolay Aleksandrov * INCLUDE (A) ALLOW (B) INCLUDE (A+B) (B)=GMI 20760436862eSNikolay Aleksandrov * EXCLUDE (X,Y) ALLOW (A) EXCLUDE (X+A,Y-A) (A)=GMI 20770436862eSNikolay Aleksandrov */ 2078adc47037SNikolay Aleksandrov static bool br_multicast_isinc_allow(const struct net_bridge_mcast *brmctx, 2079adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, void *h_addr, 2080474ddb37SNikolay Aleksandrov void *srcs, u32 nsrcs, size_t addr_size, 2081474ddb37SNikolay Aleksandrov int grec_type) 20820436862eSNikolay Aleksandrov { 20830436862eSNikolay Aleksandrov struct net_bridge_group_src *ent; 20840436862eSNikolay Aleksandrov unsigned long now = jiffies; 20850436862eSNikolay Aleksandrov bool changed = false; 20860436862eSNikolay Aleksandrov struct br_ip src_ip; 20870436862eSNikolay Aleksandrov u32 src_idx; 20880436862eSNikolay Aleksandrov 20890436862eSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 2090085b53c8SNikolay Aleksandrov src_ip.proto = pg->key.addr.proto; 20910436862eSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 2092e7cfcf2cSNikolay Aleksandrov memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size); 20930436862eSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 20940436862eSNikolay Aleksandrov if (!ent) { 20950436862eSNikolay Aleksandrov ent = br_multicast_new_group_src(pg, &src_ip); 20960436862eSNikolay Aleksandrov if (ent) 20970436862eSNikolay Aleksandrov changed = true; 20980436862eSNikolay Aleksandrov } 20990436862eSNikolay Aleksandrov 21000436862eSNikolay Aleksandrov if (ent) 2101adc47037SNikolay Aleksandrov __grp_src_mod_timer(ent, now + br_multicast_gmi(brmctx)); 21020436862eSNikolay Aleksandrov } 21030436862eSNikolay Aleksandrov 2104adc47037SNikolay Aleksandrov if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size, 2105adc47037SNikolay Aleksandrov grec_type)) 2106474ddb37SNikolay Aleksandrov changed = true; 2107474ddb37SNikolay Aleksandrov 21080436862eSNikolay Aleksandrov return changed; 21090436862eSNikolay Aleksandrov } 21100436862eSNikolay Aleksandrov 2111e6231bcaSNikolay Aleksandrov /* State Msg type New state Actions 2112e6231bcaSNikolay Aleksandrov * INCLUDE (A) IS_EX (B) EXCLUDE (A*B,B-A) (B-A)=0 2113e6231bcaSNikolay Aleksandrov * Delete (A-B) 2114e6231bcaSNikolay Aleksandrov * Group Timer=GMI 2115e6231bcaSNikolay Aleksandrov */ 2116adc47037SNikolay Aleksandrov static void __grp_src_isexc_incl(const struct net_bridge_mcast *brmctx, 2117adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, void *h_addr, 2118ddc255d9SNikolay Aleksandrov void *srcs, u32 nsrcs, size_t addr_size, 2119ddc255d9SNikolay Aleksandrov int grec_type) 2120e6231bcaSNikolay Aleksandrov { 2121e6231bcaSNikolay Aleksandrov struct net_bridge_group_src *ent; 2122e6231bcaSNikolay Aleksandrov struct br_ip src_ip; 2123e6231bcaSNikolay Aleksandrov u32 src_idx; 2124e6231bcaSNikolay Aleksandrov 2125e6231bcaSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 2126e6231bcaSNikolay Aleksandrov ent->flags |= BR_SGRP_F_DELETE; 2127e6231bcaSNikolay Aleksandrov 2128e6231bcaSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 2129085b53c8SNikolay Aleksandrov src_ip.proto = pg->key.addr.proto; 2130e6231bcaSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 2131e7cfcf2cSNikolay Aleksandrov memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size); 2132e6231bcaSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 2133e6231bcaSNikolay Aleksandrov if (ent) 2134e6231bcaSNikolay Aleksandrov ent->flags &= ~BR_SGRP_F_DELETE; 2135e6231bcaSNikolay Aleksandrov else 2136b0812368SNikolay Aleksandrov ent = br_multicast_new_group_src(pg, &src_ip); 2137b0812368SNikolay Aleksandrov if (ent) 2138b0812368SNikolay Aleksandrov br_multicast_fwd_src_handle(ent); 2139e6231bcaSNikolay Aleksandrov } 2140e6231bcaSNikolay Aleksandrov 2141adc47037SNikolay Aleksandrov br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size, 2142adc47037SNikolay Aleksandrov grec_type); 2143ddc255d9SNikolay Aleksandrov 2144e6231bcaSNikolay Aleksandrov __grp_src_delete_marked(pg); 2145e6231bcaSNikolay Aleksandrov } 2146e6231bcaSNikolay Aleksandrov 2147e6231bcaSNikolay Aleksandrov /* State Msg type New state Actions 2148e6231bcaSNikolay Aleksandrov * EXCLUDE (X,Y) IS_EX (A) EXCLUDE (A-Y,Y*A) (A-X-Y)=GMI 2149e6231bcaSNikolay Aleksandrov * Delete (X-A) 2150e6231bcaSNikolay Aleksandrov * Delete (Y-A) 2151e6231bcaSNikolay Aleksandrov * Group Timer=GMI 2152e6231bcaSNikolay Aleksandrov */ 2153adc47037SNikolay Aleksandrov static bool __grp_src_isexc_excl(const struct net_bridge_mcast *brmctx, 2154adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, void *h_addr, 2155ddc255d9SNikolay Aleksandrov void *srcs, u32 nsrcs, size_t addr_size, 2156ddc255d9SNikolay Aleksandrov int grec_type) 2157e6231bcaSNikolay Aleksandrov { 2158e6231bcaSNikolay Aleksandrov struct net_bridge_group_src *ent; 2159e6231bcaSNikolay Aleksandrov unsigned long now = jiffies; 2160e6231bcaSNikolay Aleksandrov bool changed = false; 2161e6231bcaSNikolay Aleksandrov struct br_ip src_ip; 2162e6231bcaSNikolay Aleksandrov u32 src_idx; 2163e6231bcaSNikolay Aleksandrov 2164e6231bcaSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 2165e6231bcaSNikolay Aleksandrov ent->flags |= BR_SGRP_F_DELETE; 2166e6231bcaSNikolay Aleksandrov 2167e6231bcaSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 2168085b53c8SNikolay Aleksandrov src_ip.proto = pg->key.addr.proto; 2169e6231bcaSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 2170e7cfcf2cSNikolay Aleksandrov memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size); 2171e6231bcaSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 2172e6231bcaSNikolay Aleksandrov if (ent) { 2173e6231bcaSNikolay Aleksandrov ent->flags &= ~BR_SGRP_F_DELETE; 2174e6231bcaSNikolay Aleksandrov } else { 2175e6231bcaSNikolay Aleksandrov ent = br_multicast_new_group_src(pg, &src_ip); 2176e6231bcaSNikolay Aleksandrov if (ent) { 2177b0812368SNikolay Aleksandrov __grp_src_mod_timer(ent, 2178adc47037SNikolay Aleksandrov now + br_multicast_gmi(brmctx)); 2179e6231bcaSNikolay Aleksandrov changed = true; 2180e6231bcaSNikolay Aleksandrov } 2181e6231bcaSNikolay Aleksandrov } 2182e6231bcaSNikolay Aleksandrov } 2183e6231bcaSNikolay Aleksandrov 2184adc47037SNikolay Aleksandrov if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size, 2185adc47037SNikolay Aleksandrov grec_type)) 2186ddc255d9SNikolay Aleksandrov changed = true; 2187ddc255d9SNikolay Aleksandrov 2188e6231bcaSNikolay Aleksandrov if (__grp_src_delete_marked(pg)) 2189e6231bcaSNikolay Aleksandrov changed = true; 2190e6231bcaSNikolay Aleksandrov 2191e6231bcaSNikolay Aleksandrov return changed; 2192e6231bcaSNikolay Aleksandrov } 2193e6231bcaSNikolay Aleksandrov 2194adc47037SNikolay Aleksandrov static bool br_multicast_isexc(const struct net_bridge_mcast *brmctx, 2195adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, void *h_addr, 2196ddc255d9SNikolay Aleksandrov void *srcs, u32 nsrcs, size_t addr_size, 2197ddc255d9SNikolay Aleksandrov int grec_type) 2198e6231bcaSNikolay Aleksandrov { 2199e6231bcaSNikolay Aleksandrov bool changed = false; 2200e6231bcaSNikolay Aleksandrov 2201e6231bcaSNikolay Aleksandrov switch (pg->filter_mode) { 2202e6231bcaSNikolay Aleksandrov case MCAST_INCLUDE: 2203adc47037SNikolay Aleksandrov __grp_src_isexc_incl(brmctx, pg, h_addr, srcs, nsrcs, addr_size, 2204ddc255d9SNikolay Aleksandrov grec_type); 22058266a049SNikolay Aleksandrov br_multicast_star_g_handle_mode(pg, MCAST_EXCLUDE); 2206e6231bcaSNikolay Aleksandrov changed = true; 2207e6231bcaSNikolay Aleksandrov break; 2208e6231bcaSNikolay Aleksandrov case MCAST_EXCLUDE: 2209adc47037SNikolay Aleksandrov changed = __grp_src_isexc_excl(brmctx, pg, h_addr, srcs, nsrcs, 2210adc47037SNikolay Aleksandrov addr_size, grec_type); 2211e6231bcaSNikolay Aleksandrov break; 2212e6231bcaSNikolay Aleksandrov } 2213e6231bcaSNikolay Aleksandrov 2214e6231bcaSNikolay Aleksandrov pg->filter_mode = MCAST_EXCLUDE; 2215adc47037SNikolay Aleksandrov mod_timer(&pg->timer, jiffies + br_multicast_gmi(brmctx)); 2216e6231bcaSNikolay Aleksandrov 2217e6231bcaSNikolay Aleksandrov return changed; 2218e6231bcaSNikolay Aleksandrov } 2219e6231bcaSNikolay Aleksandrov 22205bf1e00bSNikolay Aleksandrov /* State Msg type New state Actions 22215bf1e00bSNikolay Aleksandrov * INCLUDE (A) TO_IN (B) INCLUDE (A+B) (B)=GMI 22225bf1e00bSNikolay Aleksandrov * Send Q(G,A-B) 22235bf1e00bSNikolay Aleksandrov */ 2224adc47037SNikolay Aleksandrov static bool __grp_src_toin_incl(struct net_bridge_mcast *brmctx, 2225adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 2226adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, void *h_addr, 2227ddc255d9SNikolay Aleksandrov void *srcs, u32 nsrcs, size_t addr_size, 2228ddc255d9SNikolay Aleksandrov int grec_type) 22295bf1e00bSNikolay Aleksandrov { 22305bf1e00bSNikolay Aleksandrov u32 src_idx, to_send = pg->src_ents; 22315bf1e00bSNikolay Aleksandrov struct net_bridge_group_src *ent; 22325bf1e00bSNikolay Aleksandrov unsigned long now = jiffies; 22335bf1e00bSNikolay Aleksandrov bool changed = false; 22345bf1e00bSNikolay Aleksandrov struct br_ip src_ip; 22355bf1e00bSNikolay Aleksandrov 22365bf1e00bSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 22375bf1e00bSNikolay Aleksandrov ent->flags |= BR_SGRP_F_SEND; 22385bf1e00bSNikolay Aleksandrov 22395bf1e00bSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 2240085b53c8SNikolay Aleksandrov src_ip.proto = pg->key.addr.proto; 22415bf1e00bSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 2242e7cfcf2cSNikolay Aleksandrov memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size); 22435bf1e00bSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 22445bf1e00bSNikolay Aleksandrov if (ent) { 22455bf1e00bSNikolay Aleksandrov ent->flags &= ~BR_SGRP_F_SEND; 22465bf1e00bSNikolay Aleksandrov to_send--; 22475bf1e00bSNikolay Aleksandrov } else { 22485bf1e00bSNikolay Aleksandrov ent = br_multicast_new_group_src(pg, &src_ip); 22495bf1e00bSNikolay Aleksandrov if (ent) 22505bf1e00bSNikolay Aleksandrov changed = true; 22515bf1e00bSNikolay Aleksandrov } 22525bf1e00bSNikolay Aleksandrov if (ent) 2253adc47037SNikolay Aleksandrov __grp_src_mod_timer(ent, now + br_multicast_gmi(brmctx)); 22545bf1e00bSNikolay Aleksandrov } 22555bf1e00bSNikolay Aleksandrov 2256adc47037SNikolay Aleksandrov if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size, 2257adc47037SNikolay Aleksandrov grec_type)) 2258ddc255d9SNikolay Aleksandrov changed = true; 2259ddc255d9SNikolay Aleksandrov 22605bf1e00bSNikolay Aleksandrov if (to_send) 2261adc47037SNikolay Aleksandrov __grp_src_query_marked_and_rexmit(brmctx, pmctx, pg); 22625bf1e00bSNikolay Aleksandrov 22635bf1e00bSNikolay Aleksandrov return changed; 22645bf1e00bSNikolay Aleksandrov } 22655bf1e00bSNikolay Aleksandrov 22665bf1e00bSNikolay Aleksandrov /* State Msg type New state Actions 22675bf1e00bSNikolay Aleksandrov * EXCLUDE (X,Y) TO_IN (A) EXCLUDE (X+A,Y-A) (A)=GMI 22685bf1e00bSNikolay Aleksandrov * Send Q(G,X-A) 22695bf1e00bSNikolay Aleksandrov * Send Q(G) 22705bf1e00bSNikolay Aleksandrov */ 2271adc47037SNikolay Aleksandrov static bool __grp_src_toin_excl(struct net_bridge_mcast *brmctx, 2272adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 2273adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, void *h_addr, 2274ddc255d9SNikolay Aleksandrov void *srcs, u32 nsrcs, size_t addr_size, 2275ddc255d9SNikolay Aleksandrov int grec_type) 22765bf1e00bSNikolay Aleksandrov { 22775bf1e00bSNikolay Aleksandrov u32 src_idx, to_send = pg->src_ents; 22785bf1e00bSNikolay Aleksandrov struct net_bridge_group_src *ent; 22795bf1e00bSNikolay Aleksandrov unsigned long now = jiffies; 22805bf1e00bSNikolay Aleksandrov bool changed = false; 22815bf1e00bSNikolay Aleksandrov struct br_ip src_ip; 22825bf1e00bSNikolay Aleksandrov 22835bf1e00bSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 22845bf1e00bSNikolay Aleksandrov if (timer_pending(&ent->timer)) 22855bf1e00bSNikolay Aleksandrov ent->flags |= BR_SGRP_F_SEND; 22865bf1e00bSNikolay Aleksandrov 22875bf1e00bSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 2288085b53c8SNikolay Aleksandrov src_ip.proto = pg->key.addr.proto; 22895bf1e00bSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 2290e7cfcf2cSNikolay Aleksandrov memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size); 22915bf1e00bSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 22925bf1e00bSNikolay Aleksandrov if (ent) { 22935bf1e00bSNikolay Aleksandrov if (timer_pending(&ent->timer)) { 22945bf1e00bSNikolay Aleksandrov ent->flags &= ~BR_SGRP_F_SEND; 22955bf1e00bSNikolay Aleksandrov to_send--; 22965bf1e00bSNikolay Aleksandrov } 22975bf1e00bSNikolay Aleksandrov } else { 22985bf1e00bSNikolay Aleksandrov ent = br_multicast_new_group_src(pg, &src_ip); 22995bf1e00bSNikolay Aleksandrov if (ent) 23005bf1e00bSNikolay Aleksandrov changed = true; 23015bf1e00bSNikolay Aleksandrov } 23025bf1e00bSNikolay Aleksandrov if (ent) 2303adc47037SNikolay Aleksandrov __grp_src_mod_timer(ent, now + br_multicast_gmi(brmctx)); 23045bf1e00bSNikolay Aleksandrov } 23055bf1e00bSNikolay Aleksandrov 2306adc47037SNikolay Aleksandrov if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size, 2307adc47037SNikolay Aleksandrov grec_type)) 2308ddc255d9SNikolay Aleksandrov changed = true; 2309ddc255d9SNikolay Aleksandrov 23105bf1e00bSNikolay Aleksandrov if (to_send) 2311adc47037SNikolay Aleksandrov __grp_src_query_marked_and_rexmit(brmctx, pmctx, pg); 23125bf1e00bSNikolay Aleksandrov 2313adc47037SNikolay Aleksandrov __grp_send_query_and_rexmit(brmctx, pmctx, pg); 23145bf1e00bSNikolay Aleksandrov 23155bf1e00bSNikolay Aleksandrov return changed; 23165bf1e00bSNikolay Aleksandrov } 23175bf1e00bSNikolay Aleksandrov 2318adc47037SNikolay Aleksandrov static bool br_multicast_toin(struct net_bridge_mcast *brmctx, 2319adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 2320adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, void *h_addr, 2321ddc255d9SNikolay Aleksandrov void *srcs, u32 nsrcs, size_t addr_size, 2322ddc255d9SNikolay Aleksandrov int grec_type) 23235bf1e00bSNikolay Aleksandrov { 23245bf1e00bSNikolay Aleksandrov bool changed = false; 23255bf1e00bSNikolay Aleksandrov 23265bf1e00bSNikolay Aleksandrov switch (pg->filter_mode) { 23275bf1e00bSNikolay Aleksandrov case MCAST_INCLUDE: 2328adc47037SNikolay Aleksandrov changed = __grp_src_toin_incl(brmctx, pmctx, pg, h_addr, srcs, 2329adc47037SNikolay Aleksandrov nsrcs, addr_size, grec_type); 23305bf1e00bSNikolay Aleksandrov break; 23315bf1e00bSNikolay Aleksandrov case MCAST_EXCLUDE: 2332adc47037SNikolay Aleksandrov changed = __grp_src_toin_excl(brmctx, pmctx, pg, h_addr, srcs, 2333adc47037SNikolay Aleksandrov nsrcs, addr_size, grec_type); 23345bf1e00bSNikolay Aleksandrov break; 23355bf1e00bSNikolay Aleksandrov } 23365bf1e00bSNikolay Aleksandrov 2337ddc255d9SNikolay Aleksandrov if (br_multicast_eht_should_del_pg(pg)) { 2338d5a10222SNikolay Aleksandrov pg->flags |= MDB_PG_FLAGS_FAST_LEAVE; 2339ddc255d9SNikolay Aleksandrov br_multicast_find_del_pg(pg->key.port->br, pg); 2340ddc255d9SNikolay Aleksandrov /* a notification has already been sent and we shouldn't 2341ddc255d9SNikolay Aleksandrov * access pg after the delete so we have to return false 2342ddc255d9SNikolay Aleksandrov */ 2343ddc255d9SNikolay Aleksandrov changed = false; 2344ddc255d9SNikolay Aleksandrov } 2345ddc255d9SNikolay Aleksandrov 23465bf1e00bSNikolay Aleksandrov return changed; 23475bf1e00bSNikolay Aleksandrov } 23485bf1e00bSNikolay Aleksandrov 23495bf1e00bSNikolay Aleksandrov /* State Msg type New state Actions 23505bf1e00bSNikolay Aleksandrov * INCLUDE (A) TO_EX (B) EXCLUDE (A*B,B-A) (B-A)=0 23515bf1e00bSNikolay Aleksandrov * Delete (A-B) 23525bf1e00bSNikolay Aleksandrov * Send Q(G,A*B) 23535bf1e00bSNikolay Aleksandrov * Group Timer=GMI 23545bf1e00bSNikolay Aleksandrov */ 2355adc47037SNikolay Aleksandrov static void __grp_src_toex_incl(struct net_bridge_mcast *brmctx, 2356adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 2357adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, void *h_addr, 2358ddc255d9SNikolay Aleksandrov void *srcs, u32 nsrcs, size_t addr_size, 2359ddc255d9SNikolay Aleksandrov int grec_type) 23605bf1e00bSNikolay Aleksandrov { 23615bf1e00bSNikolay Aleksandrov struct net_bridge_group_src *ent; 23625bf1e00bSNikolay Aleksandrov u32 src_idx, to_send = 0; 23635bf1e00bSNikolay Aleksandrov struct br_ip src_ip; 23645bf1e00bSNikolay Aleksandrov 23655bf1e00bSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 23665bf1e00bSNikolay Aleksandrov ent->flags = (ent->flags & ~BR_SGRP_F_SEND) | BR_SGRP_F_DELETE; 23675bf1e00bSNikolay Aleksandrov 23685bf1e00bSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 2369085b53c8SNikolay Aleksandrov src_ip.proto = pg->key.addr.proto; 23705bf1e00bSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 2371e7cfcf2cSNikolay Aleksandrov memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size); 23725bf1e00bSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 23735bf1e00bSNikolay Aleksandrov if (ent) { 23745bf1e00bSNikolay Aleksandrov ent->flags = (ent->flags & ~BR_SGRP_F_DELETE) | 23755bf1e00bSNikolay Aleksandrov BR_SGRP_F_SEND; 23765bf1e00bSNikolay Aleksandrov to_send++; 23775bf1e00bSNikolay Aleksandrov } else { 2378b0812368SNikolay Aleksandrov ent = br_multicast_new_group_src(pg, &src_ip); 23795bf1e00bSNikolay Aleksandrov } 2380b0812368SNikolay Aleksandrov if (ent) 2381b0812368SNikolay Aleksandrov br_multicast_fwd_src_handle(ent); 23825bf1e00bSNikolay Aleksandrov } 23835bf1e00bSNikolay Aleksandrov 2384adc47037SNikolay Aleksandrov br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size, 2385adc47037SNikolay Aleksandrov grec_type); 2386ddc255d9SNikolay Aleksandrov 23875bf1e00bSNikolay Aleksandrov __grp_src_delete_marked(pg); 23885bf1e00bSNikolay Aleksandrov if (to_send) 2389adc47037SNikolay Aleksandrov __grp_src_query_marked_and_rexmit(brmctx, pmctx, pg); 23905bf1e00bSNikolay Aleksandrov } 23915bf1e00bSNikolay Aleksandrov 23925bf1e00bSNikolay Aleksandrov /* State Msg type New state Actions 23935bf1e00bSNikolay Aleksandrov * EXCLUDE (X,Y) TO_EX (A) EXCLUDE (A-Y,Y*A) (A-X-Y)=Group Timer 23945bf1e00bSNikolay Aleksandrov * Delete (X-A) 23955bf1e00bSNikolay Aleksandrov * Delete (Y-A) 23965bf1e00bSNikolay Aleksandrov * Send Q(G,A-Y) 23975bf1e00bSNikolay Aleksandrov * Group Timer=GMI 23985bf1e00bSNikolay Aleksandrov */ 2399adc47037SNikolay Aleksandrov static bool __grp_src_toex_excl(struct net_bridge_mcast *brmctx, 2400adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 2401adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, void *h_addr, 2402ddc255d9SNikolay Aleksandrov void *srcs, u32 nsrcs, size_t addr_size, 2403ddc255d9SNikolay Aleksandrov int grec_type) 24045bf1e00bSNikolay Aleksandrov { 24055bf1e00bSNikolay Aleksandrov struct net_bridge_group_src *ent; 24065bf1e00bSNikolay Aleksandrov u32 src_idx, to_send = 0; 24075bf1e00bSNikolay Aleksandrov bool changed = false; 24085bf1e00bSNikolay Aleksandrov struct br_ip src_ip; 24095bf1e00bSNikolay Aleksandrov 24105bf1e00bSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 24115bf1e00bSNikolay Aleksandrov ent->flags = (ent->flags & ~BR_SGRP_F_SEND) | BR_SGRP_F_DELETE; 24125bf1e00bSNikolay Aleksandrov 24135bf1e00bSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 2414085b53c8SNikolay Aleksandrov src_ip.proto = pg->key.addr.proto; 24155bf1e00bSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 2416e7cfcf2cSNikolay Aleksandrov memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size); 24175bf1e00bSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 24185bf1e00bSNikolay Aleksandrov if (ent) { 24195bf1e00bSNikolay Aleksandrov ent->flags &= ~BR_SGRP_F_DELETE; 24205bf1e00bSNikolay Aleksandrov } else { 24215bf1e00bSNikolay Aleksandrov ent = br_multicast_new_group_src(pg, &src_ip); 24225bf1e00bSNikolay Aleksandrov if (ent) { 2423b0812368SNikolay Aleksandrov __grp_src_mod_timer(ent, pg->timer.expires); 24245bf1e00bSNikolay Aleksandrov changed = true; 24255bf1e00bSNikolay Aleksandrov } 24265bf1e00bSNikolay Aleksandrov } 24275bf1e00bSNikolay Aleksandrov if (ent && timer_pending(&ent->timer)) { 24285bf1e00bSNikolay Aleksandrov ent->flags |= BR_SGRP_F_SEND; 24295bf1e00bSNikolay Aleksandrov to_send++; 24305bf1e00bSNikolay Aleksandrov } 24315bf1e00bSNikolay Aleksandrov } 24325bf1e00bSNikolay Aleksandrov 2433adc47037SNikolay Aleksandrov if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size, 2434adc47037SNikolay Aleksandrov grec_type)) 2435ddc255d9SNikolay Aleksandrov changed = true; 2436ddc255d9SNikolay Aleksandrov 24375bf1e00bSNikolay Aleksandrov if (__grp_src_delete_marked(pg)) 24385bf1e00bSNikolay Aleksandrov changed = true; 24395bf1e00bSNikolay Aleksandrov if (to_send) 2440adc47037SNikolay Aleksandrov __grp_src_query_marked_and_rexmit(brmctx, pmctx, pg); 24415bf1e00bSNikolay Aleksandrov 24425bf1e00bSNikolay Aleksandrov return changed; 24435bf1e00bSNikolay Aleksandrov } 24445bf1e00bSNikolay Aleksandrov 2445adc47037SNikolay Aleksandrov static bool br_multicast_toex(struct net_bridge_mcast *brmctx, 2446adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 2447adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, void *h_addr, 2448ddc255d9SNikolay Aleksandrov void *srcs, u32 nsrcs, size_t addr_size, 2449ddc255d9SNikolay Aleksandrov int grec_type) 24505bf1e00bSNikolay Aleksandrov { 24515bf1e00bSNikolay Aleksandrov bool changed = false; 24525bf1e00bSNikolay Aleksandrov 24535bf1e00bSNikolay Aleksandrov switch (pg->filter_mode) { 24545bf1e00bSNikolay Aleksandrov case MCAST_INCLUDE: 2455adc47037SNikolay Aleksandrov __grp_src_toex_incl(brmctx, pmctx, pg, h_addr, srcs, nsrcs, 2456adc47037SNikolay Aleksandrov addr_size, grec_type); 24578266a049SNikolay Aleksandrov br_multicast_star_g_handle_mode(pg, MCAST_EXCLUDE); 24585bf1e00bSNikolay Aleksandrov changed = true; 24595bf1e00bSNikolay Aleksandrov break; 24605bf1e00bSNikolay Aleksandrov case MCAST_EXCLUDE: 2461adc47037SNikolay Aleksandrov changed = __grp_src_toex_excl(brmctx, pmctx, pg, h_addr, srcs, 2462adc47037SNikolay Aleksandrov nsrcs, addr_size, grec_type); 24635bf1e00bSNikolay Aleksandrov break; 24645bf1e00bSNikolay Aleksandrov } 24655bf1e00bSNikolay Aleksandrov 24665bf1e00bSNikolay Aleksandrov pg->filter_mode = MCAST_EXCLUDE; 2467adc47037SNikolay Aleksandrov mod_timer(&pg->timer, jiffies + br_multicast_gmi(brmctx)); 24685bf1e00bSNikolay Aleksandrov 24695bf1e00bSNikolay Aleksandrov return changed; 24705bf1e00bSNikolay Aleksandrov } 24715bf1e00bSNikolay Aleksandrov 2472109865feSNikolay Aleksandrov /* State Msg type New state Actions 2473109865feSNikolay Aleksandrov * INCLUDE (A) BLOCK (B) INCLUDE (A) Send Q(G,A*B) 2474109865feSNikolay Aleksandrov */ 2475adc47037SNikolay Aleksandrov static bool __grp_src_block_incl(struct net_bridge_mcast *brmctx, 2476adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 2477adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, void *h_addr, 2478474ddb37SNikolay Aleksandrov void *srcs, u32 nsrcs, size_t addr_size, int grec_type) 2479109865feSNikolay Aleksandrov { 2480109865feSNikolay Aleksandrov struct net_bridge_group_src *ent; 2481109865feSNikolay Aleksandrov u32 src_idx, to_send = 0; 24820ad57c99SNikolay Aleksandrov bool changed = false; 2483109865feSNikolay Aleksandrov struct br_ip src_ip; 2484109865feSNikolay Aleksandrov 2485109865feSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 2486109865feSNikolay Aleksandrov ent->flags &= ~BR_SGRP_F_SEND; 2487109865feSNikolay Aleksandrov 2488109865feSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 2489085b53c8SNikolay Aleksandrov src_ip.proto = pg->key.addr.proto; 2490109865feSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 2491e7cfcf2cSNikolay Aleksandrov memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size); 2492109865feSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 2493109865feSNikolay Aleksandrov if (ent) { 2494109865feSNikolay Aleksandrov ent->flags |= BR_SGRP_F_SEND; 2495109865feSNikolay Aleksandrov to_send++; 2496109865feSNikolay Aleksandrov } 2497109865feSNikolay Aleksandrov } 2498109865feSNikolay Aleksandrov 2499adc47037SNikolay Aleksandrov if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size, 2500adc47037SNikolay Aleksandrov grec_type)) 2501474ddb37SNikolay Aleksandrov changed = true; 2502474ddb37SNikolay Aleksandrov 2503109865feSNikolay Aleksandrov if (to_send) 2504adc47037SNikolay Aleksandrov __grp_src_query_marked_and_rexmit(brmctx, pmctx, pg); 2505109865feSNikolay Aleksandrov 25060ad57c99SNikolay Aleksandrov return changed; 2507109865feSNikolay Aleksandrov } 2508109865feSNikolay Aleksandrov 2509109865feSNikolay Aleksandrov /* State Msg type New state Actions 2510109865feSNikolay Aleksandrov * EXCLUDE (X,Y) BLOCK (A) EXCLUDE (X+(A-Y),Y) (A-X-Y)=Group Timer 2511109865feSNikolay Aleksandrov * Send Q(G,A-Y) 2512109865feSNikolay Aleksandrov */ 2513adc47037SNikolay Aleksandrov static bool __grp_src_block_excl(struct net_bridge_mcast *brmctx, 2514adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 2515adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, void *h_addr, 2516474ddb37SNikolay Aleksandrov void *srcs, u32 nsrcs, size_t addr_size, int grec_type) 2517109865feSNikolay Aleksandrov { 2518109865feSNikolay Aleksandrov struct net_bridge_group_src *ent; 2519109865feSNikolay Aleksandrov u32 src_idx, to_send = 0; 2520109865feSNikolay Aleksandrov bool changed = false; 2521109865feSNikolay Aleksandrov struct br_ip src_ip; 2522109865feSNikolay Aleksandrov 2523109865feSNikolay Aleksandrov hlist_for_each_entry(ent, &pg->src_list, node) 2524109865feSNikolay Aleksandrov ent->flags &= ~BR_SGRP_F_SEND; 2525109865feSNikolay Aleksandrov 2526109865feSNikolay Aleksandrov memset(&src_ip, 0, sizeof(src_ip)); 2527085b53c8SNikolay Aleksandrov src_ip.proto = pg->key.addr.proto; 2528109865feSNikolay Aleksandrov for (src_idx = 0; src_idx < nsrcs; src_idx++) { 2529e7cfcf2cSNikolay Aleksandrov memcpy(&src_ip.src, srcs + (src_idx * addr_size), addr_size); 2530109865feSNikolay Aleksandrov ent = br_multicast_find_group_src(pg, &src_ip); 2531109865feSNikolay Aleksandrov if (!ent) { 2532109865feSNikolay Aleksandrov ent = br_multicast_new_group_src(pg, &src_ip); 2533109865feSNikolay Aleksandrov if (ent) { 2534b0812368SNikolay Aleksandrov __grp_src_mod_timer(ent, pg->timer.expires); 2535109865feSNikolay Aleksandrov changed = true; 2536109865feSNikolay Aleksandrov } 2537109865feSNikolay Aleksandrov } 2538109865feSNikolay Aleksandrov if (ent && timer_pending(&ent->timer)) { 2539109865feSNikolay Aleksandrov ent->flags |= BR_SGRP_F_SEND; 2540109865feSNikolay Aleksandrov to_send++; 2541109865feSNikolay Aleksandrov } 2542109865feSNikolay Aleksandrov } 2543109865feSNikolay Aleksandrov 2544adc47037SNikolay Aleksandrov if (br_multicast_eht_handle(brmctx, pg, h_addr, srcs, nsrcs, addr_size, 2545adc47037SNikolay Aleksandrov grec_type)) 2546474ddb37SNikolay Aleksandrov changed = true; 2547474ddb37SNikolay Aleksandrov 2548109865feSNikolay Aleksandrov if (to_send) 2549adc47037SNikolay Aleksandrov __grp_src_query_marked_and_rexmit(brmctx, pmctx, pg); 2550109865feSNikolay Aleksandrov 2551109865feSNikolay Aleksandrov return changed; 2552109865feSNikolay Aleksandrov } 2553109865feSNikolay Aleksandrov 2554adc47037SNikolay Aleksandrov static bool br_multicast_block(struct net_bridge_mcast *brmctx, 2555adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 2556adc47037SNikolay Aleksandrov struct net_bridge_port_group *pg, void *h_addr, 2557474ddb37SNikolay Aleksandrov void *srcs, u32 nsrcs, size_t addr_size, int grec_type) 2558109865feSNikolay Aleksandrov { 2559109865feSNikolay Aleksandrov bool changed = false; 2560109865feSNikolay Aleksandrov 2561109865feSNikolay Aleksandrov switch (pg->filter_mode) { 2562109865feSNikolay Aleksandrov case MCAST_INCLUDE: 2563adc47037SNikolay Aleksandrov changed = __grp_src_block_incl(brmctx, pmctx, pg, h_addr, srcs, 2564adc47037SNikolay Aleksandrov nsrcs, addr_size, grec_type); 2565109865feSNikolay Aleksandrov break; 2566109865feSNikolay Aleksandrov case MCAST_EXCLUDE: 2567adc47037SNikolay Aleksandrov changed = __grp_src_block_excl(brmctx, pmctx, pg, h_addr, srcs, 2568adc47037SNikolay Aleksandrov nsrcs, addr_size, grec_type); 2569109865feSNikolay Aleksandrov break; 2570109865feSNikolay Aleksandrov } 2571109865feSNikolay Aleksandrov 2572e87e4b5cSNikolay Aleksandrov if ((pg->filter_mode == MCAST_INCLUDE && hlist_empty(&pg->src_list)) || 2573e87e4b5cSNikolay Aleksandrov br_multicast_eht_should_del_pg(pg)) { 2574d5a10222SNikolay Aleksandrov if (br_multicast_eht_should_del_pg(pg)) 2575d5a10222SNikolay Aleksandrov pg->flags |= MDB_PG_FLAGS_FAST_LEAVE; 2576e87e4b5cSNikolay Aleksandrov br_multicast_find_del_pg(pg->key.port->br, pg); 2577e87e4b5cSNikolay Aleksandrov /* a notification has already been sent and we shouldn't 2578e87e4b5cSNikolay Aleksandrov * access pg after the delete so we have to return false 2579e87e4b5cSNikolay Aleksandrov */ 2580e87e4b5cSNikolay Aleksandrov changed = false; 2581e87e4b5cSNikolay Aleksandrov } 2582e87e4b5cSNikolay Aleksandrov 2583109865feSNikolay Aleksandrov return changed; 2584109865feSNikolay Aleksandrov } 2585109865feSNikolay Aleksandrov 25860436862eSNikolay Aleksandrov static struct net_bridge_port_group * 25870436862eSNikolay Aleksandrov br_multicast_find_port(struct net_bridge_mdb_entry *mp, 25880436862eSNikolay Aleksandrov struct net_bridge_port *p, 25890436862eSNikolay Aleksandrov const unsigned char *src) 25900436862eSNikolay Aleksandrov { 2591071445c6SNikolay Aleksandrov struct net_bridge *br __maybe_unused = mp->br; 25920436862eSNikolay Aleksandrov struct net_bridge_port_group *pg; 25930436862eSNikolay Aleksandrov 25940436862eSNikolay Aleksandrov for (pg = mlock_dereference(mp->ports, br); 25950436862eSNikolay Aleksandrov pg; 25960436862eSNikolay Aleksandrov pg = mlock_dereference(pg->next, br)) 25970436862eSNikolay Aleksandrov if (br_port_group_equal(pg, p, src)) 25980436862eSNikolay Aleksandrov return pg; 25990436862eSNikolay Aleksandrov 26000436862eSNikolay Aleksandrov return NULL; 26010436862eSNikolay Aleksandrov } 26020436862eSNikolay Aleksandrov 2603adc47037SNikolay Aleksandrov static int br_ip4_multicast_igmp3_report(struct net_bridge_mcast *brmctx, 2604adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 260506499098SVlad Yasevich struct sk_buff *skb, 260606499098SVlad Yasevich u16 vid) 2607eb1d1641SHerbert Xu { 2608adc47037SNikolay Aleksandrov bool igmpv2 = brmctx->multicast_igmp_version == 2; 26090436862eSNikolay Aleksandrov struct net_bridge_mdb_entry *mdst; 26100436862eSNikolay Aleksandrov struct net_bridge_port_group *pg; 26116db6f0eaSFelix Fietkau const unsigned char *src; 2612eb1d1641SHerbert Xu struct igmpv3_report *ih; 2613eb1d1641SHerbert Xu struct igmpv3_grec *grec; 26140436862eSNikolay Aleksandrov int i, len, num, type; 261554bea721SNikolay Aleksandrov __be32 group, *h_addr; 26160436862eSNikolay Aleksandrov bool changed = false; 26170436862eSNikolay Aleksandrov int err = 0; 2618e57f6185SNikolay Aleksandrov u16 nsrcs; 2619eb1d1641SHerbert Xu 2620eb1d1641SHerbert Xu ih = igmpv3_report_hdr(skb); 2621eb1d1641SHerbert Xu num = ntohs(ih->ngrec); 2622c2d4fbd2SLinus Lüssing len = skb_transport_offset(skb) + sizeof(*ih); 2623eb1d1641SHerbert Xu 2624eb1d1641SHerbert Xu for (i = 0; i < num; i++) { 2625eb1d1641SHerbert Xu len += sizeof(*grec); 2626ba5ea614SLinus Lüssing if (!ip_mc_may_pull(skb, len)) 2627eb1d1641SHerbert Xu return -EINVAL; 2628eb1d1641SHerbert Xu 2629fd218cf9SHerbert Xu grec = (void *)(skb->data + len - sizeof(*grec)); 2630eb1d1641SHerbert Xu group = grec->grec_mca; 2631eb1d1641SHerbert Xu type = grec->grec_type; 2632e57f6185SNikolay Aleksandrov nsrcs = ntohs(grec->grec_nsrcs); 2633eb1d1641SHerbert Xu 2634e57f6185SNikolay Aleksandrov len += nsrcs * 4; 2635ba5ea614SLinus Lüssing if (!ip_mc_may_pull(skb, len)) 2636eb1d1641SHerbert Xu return -EINVAL; 2637eb1d1641SHerbert Xu 2638eb1d1641SHerbert Xu switch (type) { 2639eb1d1641SHerbert Xu case IGMPV3_MODE_IS_INCLUDE: 2640eb1d1641SHerbert Xu case IGMPV3_MODE_IS_EXCLUDE: 2641eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_INCLUDE: 2642eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_EXCLUDE: 2643eb1d1641SHerbert Xu case IGMPV3_ALLOW_NEW_SOURCES: 2644eb1d1641SHerbert Xu case IGMPV3_BLOCK_OLD_SOURCES: 2645eb1d1641SHerbert Xu break; 2646eb1d1641SHerbert Xu 2647eb1d1641SHerbert Xu default: 2648eb1d1641SHerbert Xu continue; 2649eb1d1641SHerbert Xu } 2650eb1d1641SHerbert Xu 26516db6f0eaSFelix Fietkau src = eth_hdr(skb)->h_source; 26520436862eSNikolay Aleksandrov if (nsrcs == 0 && 26530436862eSNikolay Aleksandrov (type == IGMPV3_CHANGE_TO_INCLUDE || 26540436862eSNikolay Aleksandrov type == IGMPV3_MODE_IS_INCLUDE)) { 2655adc47037SNikolay Aleksandrov if (!pmctx || igmpv2) { 2656adc47037SNikolay Aleksandrov br_ip4_multicast_leave_group(brmctx, pmctx, 2657adc47037SNikolay Aleksandrov group, vid, src); 26580436862eSNikolay Aleksandrov continue; 26590436862eSNikolay Aleksandrov } 2660bc8c20acSSatish Ashok } else { 2661adc47037SNikolay Aleksandrov err = br_ip4_multicast_add_group(brmctx, pmctx, group, 2662adc47037SNikolay Aleksandrov vid, src, igmpv2); 2663eb1d1641SHerbert Xu if (err) 2664eb1d1641SHerbert Xu break; 2665eb1d1641SHerbert Xu } 26660436862eSNikolay Aleksandrov 2667adc47037SNikolay Aleksandrov if (!pmctx || igmpv2) 26680436862eSNikolay Aleksandrov continue; 26690436862eSNikolay Aleksandrov 2670adc47037SNikolay Aleksandrov spin_lock_bh(&brmctx->br->multicast_lock); 26714cdd0d10SNikolay Aleksandrov if (!br_multicast_ctx_should_use(brmctx, pmctx)) 26724cdd0d10SNikolay Aleksandrov goto unlock_continue; 26734cdd0d10SNikolay Aleksandrov 2674adc47037SNikolay Aleksandrov mdst = br_mdb_ip4_get(brmctx->br, group, vid); 26750436862eSNikolay Aleksandrov if (!mdst) 26760436862eSNikolay Aleksandrov goto unlock_continue; 2677adc47037SNikolay Aleksandrov pg = br_multicast_find_port(mdst, pmctx->port, src); 26780436862eSNikolay Aleksandrov if (!pg || (pg->flags & MDB_PG_FLAGS_PERMANENT)) 26790436862eSNikolay Aleksandrov goto unlock_continue; 268054bea721SNikolay Aleksandrov /* reload grec and host addr */ 26810436862eSNikolay Aleksandrov grec = (void *)(skb->data + len - sizeof(*grec) - (nsrcs * 4)); 268254bea721SNikolay Aleksandrov h_addr = &ip_hdr(skb)->saddr; 26830436862eSNikolay Aleksandrov switch (type) { 26840436862eSNikolay Aleksandrov case IGMPV3_ALLOW_NEW_SOURCES: 2685adc47037SNikolay Aleksandrov changed = br_multicast_isinc_allow(brmctx, pg, h_addr, 2686adc47037SNikolay Aleksandrov grec->grec_src, 2687474ddb37SNikolay Aleksandrov nsrcs, sizeof(__be32), type); 26880436862eSNikolay Aleksandrov break; 2689e6231bcaSNikolay Aleksandrov case IGMPV3_MODE_IS_INCLUDE: 2690adc47037SNikolay Aleksandrov changed = br_multicast_isinc_allow(brmctx, pg, h_addr, 2691adc47037SNikolay Aleksandrov grec->grec_src, 2692474ddb37SNikolay Aleksandrov nsrcs, sizeof(__be32), type); 2693e6231bcaSNikolay Aleksandrov break; 2694e6231bcaSNikolay Aleksandrov case IGMPV3_MODE_IS_EXCLUDE: 2695adc47037SNikolay Aleksandrov changed = br_multicast_isexc(brmctx, pg, h_addr, 2696adc47037SNikolay Aleksandrov grec->grec_src, 2697ddc255d9SNikolay Aleksandrov nsrcs, sizeof(__be32), type); 2698e6231bcaSNikolay Aleksandrov break; 26995bf1e00bSNikolay Aleksandrov case IGMPV3_CHANGE_TO_INCLUDE: 2700adc47037SNikolay Aleksandrov changed = br_multicast_toin(brmctx, pmctx, pg, h_addr, 2701adc47037SNikolay Aleksandrov grec->grec_src, 2702ddc255d9SNikolay Aleksandrov nsrcs, sizeof(__be32), type); 27035bf1e00bSNikolay Aleksandrov break; 27045bf1e00bSNikolay Aleksandrov case IGMPV3_CHANGE_TO_EXCLUDE: 2705adc47037SNikolay Aleksandrov changed = br_multicast_toex(brmctx, pmctx, pg, h_addr, 2706adc47037SNikolay Aleksandrov grec->grec_src, 2707ddc255d9SNikolay Aleksandrov nsrcs, sizeof(__be32), type); 27085bf1e00bSNikolay Aleksandrov break; 2709109865feSNikolay Aleksandrov case IGMPV3_BLOCK_OLD_SOURCES: 2710adc47037SNikolay Aleksandrov changed = br_multicast_block(brmctx, pmctx, pg, h_addr, 2711adc47037SNikolay Aleksandrov grec->grec_src, 2712474ddb37SNikolay Aleksandrov nsrcs, sizeof(__be32), type); 2713109865feSNikolay Aleksandrov break; 27140436862eSNikolay Aleksandrov } 27150436862eSNikolay Aleksandrov if (changed) 2716adc47037SNikolay Aleksandrov br_mdb_notify(brmctx->br->dev, mdst, pg, RTM_NEWMDB); 27170436862eSNikolay Aleksandrov unlock_continue: 2718adc47037SNikolay Aleksandrov spin_unlock_bh(&brmctx->br->multicast_lock); 2719bc8c20acSSatish Ashok } 2720eb1d1641SHerbert Xu 2721eb1d1641SHerbert Xu return err; 2722eb1d1641SHerbert Xu } 2723eb1d1641SHerbert Xu 2724dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 2725adc47037SNikolay Aleksandrov static int br_ip6_multicast_mld2_report(struct net_bridge_mcast *brmctx, 2726adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 272706499098SVlad Yasevich struct sk_buff *skb, 272806499098SVlad Yasevich u16 vid) 272908b202b6SYOSHIFUJI Hideaki { 2730adc47037SNikolay Aleksandrov bool mldv1 = brmctx->multicast_mld_version == 1; 27310436862eSNikolay Aleksandrov struct net_bridge_mdb_entry *mdst; 27320436862eSNikolay Aleksandrov struct net_bridge_port_group *pg; 2733ba5ea614SLinus Lüssing unsigned int nsrcs_offset; 27346db6f0eaSFelix Fietkau const unsigned char *src; 273508b202b6SYOSHIFUJI Hideaki struct icmp6hdr *icmp6h; 273654bea721SNikolay Aleksandrov struct in6_addr *h_addr; 273708b202b6SYOSHIFUJI Hideaki struct mld2_grec *grec; 2738ba5ea614SLinus Lüssing unsigned int grec_len; 27390436862eSNikolay Aleksandrov bool changed = false; 27400436862eSNikolay Aleksandrov int i, len, num; 274108b202b6SYOSHIFUJI Hideaki int err = 0; 274208b202b6SYOSHIFUJI Hideaki 2743ba5ea614SLinus Lüssing if (!ipv6_mc_may_pull(skb, sizeof(*icmp6h))) 274408b202b6SYOSHIFUJI Hideaki return -EINVAL; 274508b202b6SYOSHIFUJI Hideaki 274608b202b6SYOSHIFUJI Hideaki icmp6h = icmp6_hdr(skb); 274708b202b6SYOSHIFUJI Hideaki num = ntohs(icmp6h->icmp6_dataun.un_data16[1]); 2748c2d4fbd2SLinus Lüssing len = skb_transport_offset(skb) + sizeof(*icmp6h); 274908b202b6SYOSHIFUJI Hideaki 275008b202b6SYOSHIFUJI Hideaki for (i = 0; i < num; i++) { 2751e57f6185SNikolay Aleksandrov __be16 *_nsrcs, __nsrcs; 2752e57f6185SNikolay Aleksandrov u16 nsrcs; 275308b202b6SYOSHIFUJI Hideaki 2754ba5ea614SLinus Lüssing nsrcs_offset = len + offsetof(struct mld2_grec, grec_nsrcs); 2755ba5ea614SLinus Lüssing 2756ba5ea614SLinus Lüssing if (skb_transport_offset(skb) + ipv6_transport_len(skb) < 27575fc6266aSLinus Lüssing nsrcs_offset + sizeof(__nsrcs)) 2758ba5ea614SLinus Lüssing return -EINVAL; 2759ba5ea614SLinus Lüssing 2760e57f6185SNikolay Aleksandrov _nsrcs = skb_header_pointer(skb, nsrcs_offset, 2761e57f6185SNikolay Aleksandrov sizeof(__nsrcs), &__nsrcs); 2762e57f6185SNikolay Aleksandrov if (!_nsrcs) 276308b202b6SYOSHIFUJI Hideaki return -EINVAL; 276408b202b6SYOSHIFUJI Hideaki 2765e57f6185SNikolay Aleksandrov nsrcs = ntohs(*_nsrcs); 2766e57f6185SNikolay Aleksandrov grec_len = struct_size(grec, grec_src, nsrcs); 2767ba5ea614SLinus Lüssing 2768ba5ea614SLinus Lüssing if (!ipv6_mc_may_pull(skb, len + grec_len)) 276908b202b6SYOSHIFUJI Hideaki return -EINVAL; 277008b202b6SYOSHIFUJI Hideaki 277108b202b6SYOSHIFUJI Hideaki grec = (struct mld2_grec *)(skb->data + len); 2772ba5ea614SLinus Lüssing len += grec_len; 277308b202b6SYOSHIFUJI Hideaki 277408b202b6SYOSHIFUJI Hideaki switch (grec->grec_type) { 277508b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_INCLUDE: 277608b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_EXCLUDE: 277708b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_INCLUDE: 277808b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_EXCLUDE: 277908b202b6SYOSHIFUJI Hideaki case MLD2_ALLOW_NEW_SOURCES: 278008b202b6SYOSHIFUJI Hideaki case MLD2_BLOCK_OLD_SOURCES: 278108b202b6SYOSHIFUJI Hideaki break; 278208b202b6SYOSHIFUJI Hideaki 278308b202b6SYOSHIFUJI Hideaki default: 278408b202b6SYOSHIFUJI Hideaki continue; 278508b202b6SYOSHIFUJI Hideaki } 278608b202b6SYOSHIFUJI Hideaki 27876db6f0eaSFelix Fietkau src = eth_hdr(skb)->h_source; 2788bc8c20acSSatish Ashok if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE || 2789bc8c20acSSatish Ashok grec->grec_type == MLD2_MODE_IS_INCLUDE) && 2790e57f6185SNikolay Aleksandrov nsrcs == 0) { 2791adc47037SNikolay Aleksandrov if (!pmctx || mldv1) { 2792adc47037SNikolay Aleksandrov br_ip6_multicast_leave_group(brmctx, pmctx, 27930436862eSNikolay Aleksandrov &grec->grec_mca, 27946db6f0eaSFelix Fietkau vid, src); 27950436862eSNikolay Aleksandrov continue; 27960436862eSNikolay Aleksandrov } 2797bc8c20acSSatish Ashok } else { 2798adc47037SNikolay Aleksandrov err = br_ip6_multicast_add_group(brmctx, pmctx, 27996db6f0eaSFelix Fietkau &grec->grec_mca, vid, 28000436862eSNikolay Aleksandrov src, mldv1); 28019264251eSDavide Caratti if (err) 280208b202b6SYOSHIFUJI Hideaki break; 280308b202b6SYOSHIFUJI Hideaki } 28040436862eSNikolay Aleksandrov 2805adc47037SNikolay Aleksandrov if (!pmctx || mldv1) 28060436862eSNikolay Aleksandrov continue; 28070436862eSNikolay Aleksandrov 2808adc47037SNikolay Aleksandrov spin_lock_bh(&brmctx->br->multicast_lock); 28094cdd0d10SNikolay Aleksandrov if (!br_multicast_ctx_should_use(brmctx, pmctx)) 28104cdd0d10SNikolay Aleksandrov goto unlock_continue; 28114cdd0d10SNikolay Aleksandrov 2812adc47037SNikolay Aleksandrov mdst = br_mdb_ip6_get(brmctx->br, &grec->grec_mca, vid); 28130436862eSNikolay Aleksandrov if (!mdst) 28140436862eSNikolay Aleksandrov goto unlock_continue; 2815adc47037SNikolay Aleksandrov pg = br_multicast_find_port(mdst, pmctx->port, src); 28160436862eSNikolay Aleksandrov if (!pg || (pg->flags & MDB_PG_FLAGS_PERMANENT)) 28170436862eSNikolay Aleksandrov goto unlock_continue; 281854bea721SNikolay Aleksandrov h_addr = &ipv6_hdr(skb)->saddr; 28190436862eSNikolay Aleksandrov switch (grec->grec_type) { 28200436862eSNikolay Aleksandrov case MLD2_ALLOW_NEW_SOURCES: 2821adc47037SNikolay Aleksandrov changed = br_multicast_isinc_allow(brmctx, pg, h_addr, 282254bea721SNikolay Aleksandrov grec->grec_src, nsrcs, 2823474ddb37SNikolay Aleksandrov sizeof(struct in6_addr), 2824474ddb37SNikolay Aleksandrov grec->grec_type); 28250436862eSNikolay Aleksandrov break; 2826e6231bcaSNikolay Aleksandrov case MLD2_MODE_IS_INCLUDE: 2827adc47037SNikolay Aleksandrov changed = br_multicast_isinc_allow(brmctx, pg, h_addr, 282854bea721SNikolay Aleksandrov grec->grec_src, nsrcs, 2829474ddb37SNikolay Aleksandrov sizeof(struct in6_addr), 2830474ddb37SNikolay Aleksandrov grec->grec_type); 2831e6231bcaSNikolay Aleksandrov break; 2832e6231bcaSNikolay Aleksandrov case MLD2_MODE_IS_EXCLUDE: 2833adc47037SNikolay Aleksandrov changed = br_multicast_isexc(brmctx, pg, h_addr, 283454bea721SNikolay Aleksandrov grec->grec_src, nsrcs, 2835ddc255d9SNikolay Aleksandrov sizeof(struct in6_addr), 2836ddc255d9SNikolay Aleksandrov grec->grec_type); 2837e6231bcaSNikolay Aleksandrov break; 28385bf1e00bSNikolay Aleksandrov case MLD2_CHANGE_TO_INCLUDE: 2839adc47037SNikolay Aleksandrov changed = br_multicast_toin(brmctx, pmctx, pg, h_addr, 284054bea721SNikolay Aleksandrov grec->grec_src, nsrcs, 2841ddc255d9SNikolay Aleksandrov sizeof(struct in6_addr), 2842ddc255d9SNikolay Aleksandrov grec->grec_type); 28435bf1e00bSNikolay Aleksandrov break; 28445bf1e00bSNikolay Aleksandrov case MLD2_CHANGE_TO_EXCLUDE: 2845adc47037SNikolay Aleksandrov changed = br_multicast_toex(brmctx, pmctx, pg, h_addr, 284654bea721SNikolay Aleksandrov grec->grec_src, nsrcs, 2847ddc255d9SNikolay Aleksandrov sizeof(struct in6_addr), 2848ddc255d9SNikolay Aleksandrov grec->grec_type); 28495bf1e00bSNikolay Aleksandrov break; 2850109865feSNikolay Aleksandrov case MLD2_BLOCK_OLD_SOURCES: 2851adc47037SNikolay Aleksandrov changed = br_multicast_block(brmctx, pmctx, pg, h_addr, 285254bea721SNikolay Aleksandrov grec->grec_src, nsrcs, 2853474ddb37SNikolay Aleksandrov sizeof(struct in6_addr), 2854474ddb37SNikolay Aleksandrov grec->grec_type); 2855109865feSNikolay Aleksandrov break; 28560436862eSNikolay Aleksandrov } 28570436862eSNikolay Aleksandrov if (changed) 2858adc47037SNikolay Aleksandrov br_mdb_notify(brmctx->br->dev, mdst, pg, RTM_NEWMDB); 28590436862eSNikolay Aleksandrov unlock_continue: 2860adc47037SNikolay Aleksandrov spin_unlock_bh(&brmctx->br->multicast_lock); 2861bc8c20acSSatish Ashok } 286208b202b6SYOSHIFUJI Hideaki 286308b202b6SYOSHIFUJI Hideaki return err; 286408b202b6SYOSHIFUJI Hideaki } 286508b202b6SYOSHIFUJI Hideaki #endif 286608b202b6SYOSHIFUJI Hideaki 2867c3fb3698SNikolay Aleksandrov static bool br_multicast_select_querier(struct net_bridge_mcast *brmctx, 286854cb4319SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 286967b746f9SNikolay Aleksandrov struct br_ip *saddr) 2870dc4eb53aSLinus Lüssing { 287167b746f9SNikolay Aleksandrov int port_ifidx = pmctx ? pmctx->port->dev->ifindex : 0; 2872c3fb3698SNikolay Aleksandrov struct timer_list *own_timer, *other_timer; 2873c3fb3698SNikolay Aleksandrov struct bridge_mcast_querier *querier; 287454cb4319SNikolay Aleksandrov 2875c3fb3698SNikolay Aleksandrov switch (saddr->proto) { 2876c3fb3698SNikolay Aleksandrov case htons(ETH_P_IP): 2877c3fb3698SNikolay Aleksandrov querier = &brmctx->ip4_querier; 2878c3fb3698SNikolay Aleksandrov own_timer = &brmctx->ip4_own_query.timer; 2879c3fb3698SNikolay Aleksandrov other_timer = &brmctx->ip4_other_query.timer; 2880c3fb3698SNikolay Aleksandrov if (!querier->addr.src.ip4 || 2881c3fb3698SNikolay Aleksandrov ntohl(saddr->src.ip4) <= ntohl(querier->addr.src.ip4)) 2882dc4eb53aSLinus Lüssing goto update; 2883c3fb3698SNikolay Aleksandrov break; 2884dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 2885c3fb3698SNikolay Aleksandrov case htons(ETH_P_IPV6): 2886c3fb3698SNikolay Aleksandrov querier = &brmctx->ip6_querier; 2887c3fb3698SNikolay Aleksandrov own_timer = &brmctx->ip6_own_query.timer; 2888c3fb3698SNikolay Aleksandrov other_timer = &brmctx->ip6_other_query.timer; 2889c3fb3698SNikolay Aleksandrov if (ipv6_addr_cmp(&saddr->src.ip6, &querier->addr.src.ip6) <= 0) 2890dc4eb53aSLinus Lüssing goto update; 2891c3fb3698SNikolay Aleksandrov break; 2892c3fb3698SNikolay Aleksandrov #endif 2893c3fb3698SNikolay Aleksandrov default: 2894c3fb3698SNikolay Aleksandrov return false; 2895c3fb3698SNikolay Aleksandrov } 2896dc4eb53aSLinus Lüssing 2897c3fb3698SNikolay Aleksandrov if (!timer_pending(own_timer) && !timer_pending(other_timer)) 2898dc4eb53aSLinus Lüssing goto update; 2899dc4eb53aSLinus Lüssing 2900dc4eb53aSLinus Lüssing return false; 2901dc4eb53aSLinus Lüssing 2902dc4eb53aSLinus Lüssing update: 2903c3fb3698SNikolay Aleksandrov br_multicast_update_querier(brmctx, querier, port_ifidx, saddr); 29042cd41431SLinus Lüssing 2905dc4eb53aSLinus Lüssing return true; 2906dc4eb53aSLinus Lüssing } 2907dc4eb53aSLinus Lüssing 2908c7fa1d9bSNikolay Aleksandrov static struct net_bridge_port * 2909c7fa1d9bSNikolay Aleksandrov __br_multicast_get_querier_port(struct net_bridge *br, 2910c7fa1d9bSNikolay Aleksandrov const struct bridge_mcast_querier *querier) 2911c7fa1d9bSNikolay Aleksandrov { 2912c7fa1d9bSNikolay Aleksandrov int port_ifidx = READ_ONCE(querier->port_ifidx); 2913c7fa1d9bSNikolay Aleksandrov struct net_bridge_port *p; 2914c7fa1d9bSNikolay Aleksandrov struct net_device *dev; 2915c7fa1d9bSNikolay Aleksandrov 2916c7fa1d9bSNikolay Aleksandrov if (port_ifidx == 0) 2917c7fa1d9bSNikolay Aleksandrov return NULL; 2918c7fa1d9bSNikolay Aleksandrov 2919c7fa1d9bSNikolay Aleksandrov dev = dev_get_by_index_rcu(dev_net(br->dev), port_ifidx); 2920c7fa1d9bSNikolay Aleksandrov if (!dev) 2921c7fa1d9bSNikolay Aleksandrov return NULL; 2922c7fa1d9bSNikolay Aleksandrov p = br_port_get_rtnl_rcu(dev); 2923c7fa1d9bSNikolay Aleksandrov if (!p || p->br != br) 2924c7fa1d9bSNikolay Aleksandrov return NULL; 2925c7fa1d9bSNikolay Aleksandrov 2926c7fa1d9bSNikolay Aleksandrov return p; 2927c7fa1d9bSNikolay Aleksandrov } 2928c7fa1d9bSNikolay Aleksandrov 2929c7fa1d9bSNikolay Aleksandrov size_t br_multicast_querier_state_size(void) 2930c7fa1d9bSNikolay Aleksandrov { 2931*cdda378bSNikolay Aleksandrov return nla_total_size(0) + /* nest attribute */ 2932c7fa1d9bSNikolay Aleksandrov nla_total_size(sizeof(__be32)) + /* BRIDGE_QUERIER_IP_ADDRESS */ 2933c7fa1d9bSNikolay Aleksandrov nla_total_size(sizeof(int)) + /* BRIDGE_QUERIER_IP_PORT */ 2934c7fa1d9bSNikolay Aleksandrov nla_total_size_64bit(sizeof(u64)); /* BRIDGE_QUERIER_IP_OTHER_TIMER */ 2935c7fa1d9bSNikolay Aleksandrov } 2936c7fa1d9bSNikolay Aleksandrov 2937c7fa1d9bSNikolay Aleksandrov /* protected by rtnl or rcu */ 2938c7fa1d9bSNikolay Aleksandrov int br_multicast_dump_querier_state(struct sk_buff *skb, 2939c7fa1d9bSNikolay Aleksandrov const struct net_bridge_mcast *brmctx, 2940c7fa1d9bSNikolay Aleksandrov int nest_attr) 2941c7fa1d9bSNikolay Aleksandrov { 2942c7fa1d9bSNikolay Aleksandrov struct bridge_mcast_querier querier = {}; 2943c7fa1d9bSNikolay Aleksandrov struct net_bridge_port *p; 2944c7fa1d9bSNikolay Aleksandrov struct nlattr *nest; 2945c7fa1d9bSNikolay Aleksandrov 2946f137b7d4SNikolay Aleksandrov if (!br_opt_get(brmctx->br, BROPT_MULTICAST_ENABLED) || 2947f137b7d4SNikolay Aleksandrov br_multicast_ctx_vlan_global_disabled(brmctx)) 2948f137b7d4SNikolay Aleksandrov return 0; 2949f137b7d4SNikolay Aleksandrov 2950c7fa1d9bSNikolay Aleksandrov nest = nla_nest_start(skb, nest_attr); 2951c7fa1d9bSNikolay Aleksandrov if (!nest) 2952c7fa1d9bSNikolay Aleksandrov return -EMSGSIZE; 2953c7fa1d9bSNikolay Aleksandrov 2954c7fa1d9bSNikolay Aleksandrov rcu_read_lock(); 295585b41082SNikolay Aleksandrov if (!brmctx->multicast_querier && 295685b41082SNikolay Aleksandrov !timer_pending(&brmctx->ip4_other_query.timer)) 295785b41082SNikolay Aleksandrov goto out_v6; 295885b41082SNikolay Aleksandrov 2959c7fa1d9bSNikolay Aleksandrov br_multicast_read_querier(&brmctx->ip4_querier, &querier); 2960c7fa1d9bSNikolay Aleksandrov if (nla_put_in_addr(skb, BRIDGE_QUERIER_IP_ADDRESS, 2961c7fa1d9bSNikolay Aleksandrov querier.addr.src.ip4)) { 2962c7fa1d9bSNikolay Aleksandrov rcu_read_unlock(); 2963c7fa1d9bSNikolay Aleksandrov goto out_err; 2964c7fa1d9bSNikolay Aleksandrov } 2965c7fa1d9bSNikolay Aleksandrov 2966c7fa1d9bSNikolay Aleksandrov p = __br_multicast_get_querier_port(brmctx->br, &querier); 2967c7fa1d9bSNikolay Aleksandrov if (timer_pending(&brmctx->ip4_other_query.timer) && 2968c7fa1d9bSNikolay Aleksandrov (nla_put_u64_64bit(skb, BRIDGE_QUERIER_IP_OTHER_TIMER, 2969c7fa1d9bSNikolay Aleksandrov br_timer_value(&brmctx->ip4_other_query.timer), 2970c7fa1d9bSNikolay Aleksandrov BRIDGE_QUERIER_PAD) || 2971c7fa1d9bSNikolay Aleksandrov (p && nla_put_u32(skb, BRIDGE_QUERIER_IP_PORT, p->dev->ifindex)))) { 2972c7fa1d9bSNikolay Aleksandrov rcu_read_unlock(); 2973c7fa1d9bSNikolay Aleksandrov goto out_err; 2974c7fa1d9bSNikolay Aleksandrov } 297585b41082SNikolay Aleksandrov 297685b41082SNikolay Aleksandrov out_v6: 297785b41082SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 297885b41082SNikolay Aleksandrov if (!brmctx->multicast_querier && 297985b41082SNikolay Aleksandrov !timer_pending(&brmctx->ip6_other_query.timer)) 298085b41082SNikolay Aleksandrov goto out; 298185b41082SNikolay Aleksandrov 298285b41082SNikolay Aleksandrov br_multicast_read_querier(&brmctx->ip6_querier, &querier); 298385b41082SNikolay Aleksandrov if (nla_put_in6_addr(skb, BRIDGE_QUERIER_IPV6_ADDRESS, 298485b41082SNikolay Aleksandrov &querier.addr.src.ip6)) { 298585b41082SNikolay Aleksandrov rcu_read_unlock(); 298685b41082SNikolay Aleksandrov goto out_err; 298785b41082SNikolay Aleksandrov } 298885b41082SNikolay Aleksandrov 298985b41082SNikolay Aleksandrov p = __br_multicast_get_querier_port(brmctx->br, &querier); 299085b41082SNikolay Aleksandrov if (timer_pending(&brmctx->ip6_other_query.timer) && 299185b41082SNikolay Aleksandrov (nla_put_u64_64bit(skb, BRIDGE_QUERIER_IPV6_OTHER_TIMER, 299285b41082SNikolay Aleksandrov br_timer_value(&brmctx->ip6_other_query.timer), 299385b41082SNikolay Aleksandrov BRIDGE_QUERIER_PAD) || 299485b41082SNikolay Aleksandrov (p && nla_put_u32(skb, BRIDGE_QUERIER_IPV6_PORT, 299585b41082SNikolay Aleksandrov p->dev->ifindex)))) { 299685b41082SNikolay Aleksandrov rcu_read_unlock(); 299785b41082SNikolay Aleksandrov goto out_err; 299885b41082SNikolay Aleksandrov } 299985b41082SNikolay Aleksandrov out: 300085b41082SNikolay Aleksandrov #endif 3001c7fa1d9bSNikolay Aleksandrov rcu_read_unlock(); 3002c7fa1d9bSNikolay Aleksandrov nla_nest_end(skb, nest); 300385b41082SNikolay Aleksandrov if (!nla_len(nest)) 300485b41082SNikolay Aleksandrov nla_nest_cancel(skb, nest); 3005c7fa1d9bSNikolay Aleksandrov 3006c7fa1d9bSNikolay Aleksandrov return 0; 3007c7fa1d9bSNikolay Aleksandrov 3008c7fa1d9bSNikolay Aleksandrov out_err: 3009c7fa1d9bSNikolay Aleksandrov nla_nest_cancel(skb, nest); 3010c7fa1d9bSNikolay Aleksandrov return -EMSGSIZE; 3011c7fa1d9bSNikolay Aleksandrov } 3012c7fa1d9bSNikolay Aleksandrov 3013cc0fdd80SLinus Lüssing static void 3014adc47037SNikolay Aleksandrov br_multicast_update_query_timer(struct net_bridge_mcast *brmctx, 301590010b36SLinus Lüssing struct bridge_mcast_other_query *query, 3016b00589afSLinus Lüssing unsigned long max_delay) 3017b00589afSLinus Lüssing { 301890010b36SLinus Lüssing if (!timer_pending(&query->timer)) 301990010b36SLinus Lüssing query->delay_time = jiffies + max_delay; 3020b00589afSLinus Lüssing 3021adc47037SNikolay Aleksandrov mod_timer(&query->timer, jiffies + brmctx->multicast_querier_interval); 3022b00589afSLinus Lüssing } 3023b00589afSLinus Lüssing 30246d549648SNogah Frankel static void br_port_mc_router_state_change(struct net_bridge_port *p, 30256d549648SNogah Frankel bool is_mc_router) 30266d549648SNogah Frankel { 30276d549648SNogah Frankel struct switchdev_attr attr = { 30286d549648SNogah Frankel .orig_dev = p->dev, 30296d549648SNogah Frankel .id = SWITCHDEV_ATTR_ID_PORT_MROUTER, 30306d549648SNogah Frankel .flags = SWITCHDEV_F_DEFER, 30316d549648SNogah Frankel .u.mrouter = is_mc_router, 30326d549648SNogah Frankel }; 30336d549648SNogah Frankel 3034dcbdf135SVladimir Oltean switchdev_port_attr_set(p->dev, &attr, NULL); 30356d549648SNogah Frankel } 30366d549648SNogah Frankel 3037d9b8c4d8SLinus Lüssing static struct net_bridge_port * 3038d3d065c0SNikolay Aleksandrov br_multicast_rport_from_node(struct net_bridge_mcast *brmctx, 3039d9b8c4d8SLinus Lüssing struct hlist_head *mc_router_list, 3040d9b8c4d8SLinus Lüssing struct hlist_node *rlist) 3041d9b8c4d8SLinus Lüssing { 30429632233eSNikolay Aleksandrov struct net_bridge_mcast_port *pmctx; 30439632233eSNikolay Aleksandrov 3044a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 3045d3d065c0SNikolay Aleksandrov if (mc_router_list == &brmctx->ip6_mc_router_list) 30469632233eSNikolay Aleksandrov pmctx = hlist_entry(rlist, struct net_bridge_mcast_port, 30479632233eSNikolay Aleksandrov ip6_rlist); 30489632233eSNikolay Aleksandrov else 3049a3c02e76SLinus Lüssing #endif 30509632233eSNikolay Aleksandrov pmctx = hlist_entry(rlist, struct net_bridge_mcast_port, 30519632233eSNikolay Aleksandrov ip4_rlist); 30529632233eSNikolay Aleksandrov 30539632233eSNikolay Aleksandrov return pmctx->port; 3054d9b8c4d8SLinus Lüssing } 3055d9b8c4d8SLinus Lüssing 3056d9b8c4d8SLinus Lüssing static struct hlist_node * 3057d3d065c0SNikolay Aleksandrov br_multicast_get_rport_slot(struct net_bridge_mcast *brmctx, 3058d9b8c4d8SLinus Lüssing struct net_bridge_port *port, 3059d9b8c4d8SLinus Lüssing struct hlist_head *mc_router_list) 3060d9b8c4d8SLinus Lüssing 3061d9b8c4d8SLinus Lüssing { 3062d9b8c4d8SLinus Lüssing struct hlist_node *slot = NULL; 3063d9b8c4d8SLinus Lüssing struct net_bridge_port *p; 3064d9b8c4d8SLinus Lüssing struct hlist_node *rlist; 3065d9b8c4d8SLinus Lüssing 3066d9b8c4d8SLinus Lüssing hlist_for_each(rlist, mc_router_list) { 3067d3d065c0SNikolay Aleksandrov p = br_multicast_rport_from_node(brmctx, mc_router_list, rlist); 3068d9b8c4d8SLinus Lüssing 3069d9b8c4d8SLinus Lüssing if ((unsigned long)port >= (unsigned long)p) 3070d9b8c4d8SLinus Lüssing break; 3071d9b8c4d8SLinus Lüssing 3072d9b8c4d8SLinus Lüssing slot = rlist; 3073d9b8c4d8SLinus Lüssing } 3074d9b8c4d8SLinus Lüssing 3075d9b8c4d8SLinus Lüssing return slot; 3076d9b8c4d8SLinus Lüssing } 3077d9b8c4d8SLinus Lüssing 3078adc47037SNikolay Aleksandrov static bool br_multicast_no_router_otherpf(struct net_bridge_mcast_port *pmctx, 3079a3c02e76SLinus Lüssing struct hlist_node *rnode) 3080a3c02e76SLinus Lüssing { 3081a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 3082adc47037SNikolay Aleksandrov if (rnode != &pmctx->ip6_rlist) 3083adc47037SNikolay Aleksandrov return hlist_unhashed(&pmctx->ip6_rlist); 3084a3c02e76SLinus Lüssing else 3085adc47037SNikolay Aleksandrov return hlist_unhashed(&pmctx->ip4_rlist); 3086a3c02e76SLinus Lüssing #else 3087a3c02e76SLinus Lüssing return true; 3088a3c02e76SLinus Lüssing #endif 3089a3c02e76SLinus Lüssing } 3090a3c02e76SLinus Lüssing 3091d9b8c4d8SLinus Lüssing /* Add port to router_list 30927e80c124Sstephen hemminger * list is maintained ordered by pointer value 30937e80c124Sstephen hemminger * and locked by br->multicast_lock and RCU 30947e80c124Sstephen hemminger */ 3095d3d065c0SNikolay Aleksandrov static void br_multicast_add_router(struct net_bridge_mcast *brmctx, 3096adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 3097d9b8c4d8SLinus Lüssing struct hlist_node *rlist, 3098d9b8c4d8SLinus Lüssing struct hlist_head *mc_router_list) 30990909e117SHerbert Xu { 3100d9b8c4d8SLinus Lüssing struct hlist_node *slot; 31010909e117SHerbert Xu 3102d9b8c4d8SLinus Lüssing if (!hlist_unhashed(rlist)) 31031a040eacSNikolay Aleksandrov return; 31041a040eacSNikolay Aleksandrov 3105adc47037SNikolay Aleksandrov slot = br_multicast_get_rport_slot(brmctx, pmctx->port, mc_router_list); 31060909e117SHerbert Xu 31077e80c124Sstephen hemminger if (slot) 3108d9b8c4d8SLinus Lüssing hlist_add_behind_rcu(rlist, slot); 3109dcdca2c4Sstephen hemminger else 3110d9b8c4d8SLinus Lüssing hlist_add_head_rcu(rlist, mc_router_list); 3111d9b8c4d8SLinus Lüssing 3112a3c02e76SLinus Lüssing /* For backwards compatibility for now, only notify if we 3113a3c02e76SLinus Lüssing * switched from no IPv4/IPv6 multicast router to a new 3114a3c02e76SLinus Lüssing * IPv4 or IPv6 multicast router. 3115a3c02e76SLinus Lüssing */ 3116adc47037SNikolay Aleksandrov if (br_multicast_no_router_otherpf(pmctx, rlist)) { 31171e9ca456SNikolay Aleksandrov br_rtr_notify(pmctx->port->br->dev, pmctx, RTM_NEWMDB); 3118adc47037SNikolay Aleksandrov br_port_mc_router_state_change(pmctx->port, true); 31190909e117SHerbert Xu } 3120a3c02e76SLinus Lüssing } 31210909e117SHerbert Xu 3122d9b8c4d8SLinus Lüssing /* Add port to router_list 3123d9b8c4d8SLinus Lüssing * list is maintained ordered by pointer value 3124d9b8c4d8SLinus Lüssing * and locked by br->multicast_lock and RCU 3125d9b8c4d8SLinus Lüssing */ 3126adc47037SNikolay Aleksandrov static void br_ip4_multicast_add_router(struct net_bridge_mcast *brmctx, 3127adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx) 3128eb1d1641SHerbert Xu { 3129adc47037SNikolay Aleksandrov br_multicast_add_router(brmctx, pmctx, &pmctx->ip4_rlist, 3130adc47037SNikolay Aleksandrov &brmctx->ip4_mc_router_list); 3131d9b8c4d8SLinus Lüssing } 3132d9b8c4d8SLinus Lüssing 3133a3c02e76SLinus Lüssing /* Add port to router_list 3134a3c02e76SLinus Lüssing * list is maintained ordered by pointer value 3135a3c02e76SLinus Lüssing * and locked by br->multicast_lock and RCU 3136a3c02e76SLinus Lüssing */ 3137adc47037SNikolay Aleksandrov static void br_ip6_multicast_add_router(struct net_bridge_mcast *brmctx, 3138adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx) 3139a3c02e76SLinus Lüssing { 3140a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 3141adc47037SNikolay Aleksandrov br_multicast_add_router(brmctx, pmctx, &pmctx->ip6_rlist, 3142adc47037SNikolay Aleksandrov &brmctx->ip6_mc_router_list); 3143a3c02e76SLinus Lüssing #endif 3144a3c02e76SLinus Lüssing } 3145a3c02e76SLinus Lüssing 3146adc47037SNikolay Aleksandrov static void br_multicast_mark_router(struct net_bridge_mcast *brmctx, 3147adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 3148d9b8c4d8SLinus Lüssing struct timer_list *timer, 3149d9b8c4d8SLinus Lüssing struct hlist_node *rlist, 3150d9b8c4d8SLinus Lüssing struct hlist_head *mc_router_list) 3151d9b8c4d8SLinus Lüssing { 3152eb1d1641SHerbert Xu unsigned long now = jiffies; 3153eb1d1641SHerbert Xu 31544cdd0d10SNikolay Aleksandrov if (!br_multicast_ctx_should_use(brmctx, pmctx)) 31554cdd0d10SNikolay Aleksandrov return; 31564cdd0d10SNikolay Aleksandrov 3157adc47037SNikolay Aleksandrov if (!pmctx) { 3158d3d065c0SNikolay Aleksandrov if (brmctx->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) { 3159d3d065c0SNikolay Aleksandrov if (!br_ip4_multicast_is_router(brmctx) && 3160d3d065c0SNikolay Aleksandrov !br_ip6_multicast_is_router(brmctx)) 3161adc47037SNikolay Aleksandrov br_mc_router_state_change(brmctx->br, true); 3162d3d065c0SNikolay Aleksandrov mod_timer(timer, now + brmctx->multicast_querier_interval); 316377041420SYotam Gigi } 3164eb1d1641SHerbert Xu return; 3165eb1d1641SHerbert Xu } 3166eb1d1641SHerbert Xu 3167adc47037SNikolay Aleksandrov if (pmctx->multicast_router == MDB_RTR_TYPE_DISABLED || 3168adc47037SNikolay Aleksandrov pmctx->multicast_router == MDB_RTR_TYPE_PERM) 3169eb1d1641SHerbert Xu return; 3170eb1d1641SHerbert Xu 3171adc47037SNikolay Aleksandrov br_multicast_add_router(brmctx, pmctx, rlist, mc_router_list); 3172d3d065c0SNikolay Aleksandrov mod_timer(timer, now + brmctx->multicast_querier_interval); 3173d9b8c4d8SLinus Lüssing } 3174eb1d1641SHerbert Xu 3175adc47037SNikolay Aleksandrov static void br_ip4_multicast_mark_router(struct net_bridge_mcast *brmctx, 3176adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx) 3177d9b8c4d8SLinus Lüssing { 3178adc47037SNikolay Aleksandrov struct timer_list *timer = &brmctx->ip4_mc_router_timer; 3179d9b8c4d8SLinus Lüssing struct hlist_node *rlist = NULL; 3180d9b8c4d8SLinus Lüssing 3181adc47037SNikolay Aleksandrov if (pmctx) { 3182adc47037SNikolay Aleksandrov timer = &pmctx->ip4_mc_router_timer; 3183adc47037SNikolay Aleksandrov rlist = &pmctx->ip4_rlist; 3184d9b8c4d8SLinus Lüssing } 3185d9b8c4d8SLinus Lüssing 3186adc47037SNikolay Aleksandrov br_multicast_mark_router(brmctx, pmctx, timer, rlist, 3187adc47037SNikolay Aleksandrov &brmctx->ip4_mc_router_list); 3188eb1d1641SHerbert Xu } 3189eb1d1641SHerbert Xu 3190adc47037SNikolay Aleksandrov static void br_ip6_multicast_mark_router(struct net_bridge_mcast *brmctx, 3191adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx) 3192a3c02e76SLinus Lüssing { 3193a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 3194adc47037SNikolay Aleksandrov struct timer_list *timer = &brmctx->ip6_mc_router_timer; 3195a3c02e76SLinus Lüssing struct hlist_node *rlist = NULL; 3196a3c02e76SLinus Lüssing 3197adc47037SNikolay Aleksandrov if (pmctx) { 3198adc47037SNikolay Aleksandrov timer = &pmctx->ip6_mc_router_timer; 3199adc47037SNikolay Aleksandrov rlist = &pmctx->ip6_rlist; 3200a3c02e76SLinus Lüssing } 3201a3c02e76SLinus Lüssing 3202adc47037SNikolay Aleksandrov br_multicast_mark_router(brmctx, pmctx, timer, rlist, 3203adc47037SNikolay Aleksandrov &brmctx->ip6_mc_router_list); 3204a3c02e76SLinus Lüssing #endif 3205a3c02e76SLinus Lüssing } 3206a3c02e76SLinus Lüssing 3207b19232efSLinus Lüssing static void 3208adc47037SNikolay Aleksandrov br_ip4_multicast_query_received(struct net_bridge_mcast *brmctx, 3209adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 321090010b36SLinus Lüssing struct bridge_mcast_other_query *query, 3211dc4eb53aSLinus Lüssing struct br_ip *saddr, 3212b00589afSLinus Lüssing unsigned long max_delay) 3213eb1d1641SHerbert Xu { 3214c3fb3698SNikolay Aleksandrov if (!br_multicast_select_querier(brmctx, pmctx, saddr)) 3215eb1d1641SHerbert Xu return; 3216eb1d1641SHerbert Xu 3217adc47037SNikolay Aleksandrov br_multicast_update_query_timer(brmctx, query, max_delay); 3218adc47037SNikolay Aleksandrov br_ip4_multicast_mark_router(brmctx, pmctx); 3219eb1d1641SHerbert Xu } 3220eb1d1641SHerbert Xu 3221b19232efSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 3222b19232efSLinus Lüssing static void 3223adc47037SNikolay Aleksandrov br_ip6_multicast_query_received(struct net_bridge_mcast *brmctx, 3224adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 3225b19232efSLinus Lüssing struct bridge_mcast_other_query *query, 3226b19232efSLinus Lüssing struct br_ip *saddr, 3227b19232efSLinus Lüssing unsigned long max_delay) 3228b19232efSLinus Lüssing { 3229c3fb3698SNikolay Aleksandrov if (!br_multicast_select_querier(brmctx, pmctx, saddr)) 3230b19232efSLinus Lüssing return; 3231b19232efSLinus Lüssing 3232adc47037SNikolay Aleksandrov br_multicast_update_query_timer(brmctx, query, max_delay); 3233adc47037SNikolay Aleksandrov br_ip6_multicast_mark_router(brmctx, pmctx); 3234b19232efSLinus Lüssing } 3235b19232efSLinus Lüssing #endif 3236b19232efSLinus Lüssing 3237adc47037SNikolay Aleksandrov static void br_ip4_multicast_query(struct net_bridge_mcast *brmctx, 3238adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 323906499098SVlad Yasevich struct sk_buff *skb, 324006499098SVlad Yasevich u16 vid) 3241eb1d1641SHerbert Xu { 3242ba5ea614SLinus Lüssing unsigned int transport_len = ip_transport_len(skb); 3243b71d1d42SEric Dumazet const struct iphdr *iph = ip_hdr(skb); 3244eb1d1641SHerbert Xu struct igmphdr *ih = igmp_hdr(skb); 3245eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 3246eb1d1641SHerbert Xu struct igmpv3_query *ih3; 3247eb1d1641SHerbert Xu struct net_bridge_port_group *p; 3248e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 324967b746f9SNikolay Aleksandrov struct br_ip saddr = {}; 3250eb1d1641SHerbert Xu unsigned long max_delay; 3251eb1d1641SHerbert Xu unsigned long now = jiffies; 3252eb1d1641SHerbert Xu __be32 group; 3253eb1d1641SHerbert Xu 3254adc47037SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 32554cdd0d10SNikolay Aleksandrov if (!br_multicast_ctx_should_use(brmctx, pmctx)) 3256eb1d1641SHerbert Xu goto out; 3257eb1d1641SHerbert Xu 3258eb1d1641SHerbert Xu group = ih->group; 3259eb1d1641SHerbert Xu 3260ba5ea614SLinus Lüssing if (transport_len == sizeof(*ih)) { 3261eb1d1641SHerbert Xu max_delay = ih->code * (HZ / IGMP_TIMER_SCALE); 3262eb1d1641SHerbert Xu 3263eb1d1641SHerbert Xu if (!max_delay) { 3264eb1d1641SHerbert Xu max_delay = 10 * HZ; 3265eb1d1641SHerbert Xu group = 0; 3266eb1d1641SHerbert Xu } 3267ba5ea614SLinus Lüssing } else if (transport_len >= sizeof(*ih3)) { 3268eb1d1641SHerbert Xu ih3 = igmpv3_query_hdr(skb); 326923550b83SNikolay Aleksandrov if (ih3->nsrcs || 3270d3d065c0SNikolay Aleksandrov (brmctx->multicast_igmp_version == 3 && group && 3271d3d065c0SNikolay Aleksandrov ih3->suppress)) 3272bec68ff1SYOSHIFUJI Hideaki goto out; 3273eb1d1641SHerbert Xu 32740ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 max_delay = ih3->code ? 32750ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1; 32769afd85c9SLinus Lüssing } else { 32779ed973ccSLinus Lüssing goto out; 32789ed973ccSLinus Lüssing } 32799ed973ccSLinus Lüssing 3280dc4eb53aSLinus Lüssing if (!group) { 3281dc4eb53aSLinus Lüssing saddr.proto = htons(ETH_P_IP); 3282deb96566SNikolay Aleksandrov saddr.src.ip4 = iph->saddr; 3283b00589afSLinus Lüssing 3284adc47037SNikolay Aleksandrov br_ip4_multicast_query_received(brmctx, pmctx, 3285d3d065c0SNikolay Aleksandrov &brmctx->ip4_other_query, 3286dc4eb53aSLinus Lüssing &saddr, max_delay); 3287eb1d1641SHerbert Xu goto out; 3288dc4eb53aSLinus Lüssing } 3289eb1d1641SHerbert Xu 3290adc47037SNikolay Aleksandrov mp = br_mdb_ip4_get(brmctx->br, group, vid); 3291eb1d1641SHerbert Xu if (!mp) 3292eb1d1641SHerbert Xu goto out; 3293eb1d1641SHerbert Xu 3294d3d065c0SNikolay Aleksandrov max_delay *= brmctx->multicast_last_member_count; 3295eb1d1641SHerbert Xu 3296ff0fd34eSAndrew Lunn if (mp->host_joined && 3297eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 3298eb1d1641SHerbert Xu time_after(mp->timer.expires, now + max_delay) : 3299eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) 3300eb1d1641SHerbert Xu mod_timer(&mp->timer, now + max_delay); 3301eb1d1641SHerbert Xu 3302e8051688SEric Dumazet for (pp = &mp->ports; 3303adc47037SNikolay Aleksandrov (p = mlock_dereference(*pp, brmctx->br)) != NULL; 3304e8051688SEric Dumazet pp = &p->next) { 3305eb1d1641SHerbert Xu if (timer_pending(&p->timer) ? 3306eb1d1641SHerbert Xu time_after(p->timer.expires, now + max_delay) : 330723550b83SNikolay Aleksandrov try_to_del_timer_sync(&p->timer) >= 0 && 3308d3d065c0SNikolay Aleksandrov (brmctx->multicast_igmp_version == 2 || 330923550b83SNikolay Aleksandrov p->filter_mode == MCAST_EXCLUDE)) 331024f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 3311eb1d1641SHerbert Xu } 3312eb1d1641SHerbert Xu 3313eb1d1641SHerbert Xu out: 3314adc47037SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 3315eb1d1641SHerbert Xu } 3316eb1d1641SHerbert Xu 3317dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 3318adc47037SNikolay Aleksandrov static int br_ip6_multicast_query(struct net_bridge_mcast *brmctx, 3319adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 332006499098SVlad Yasevich struct sk_buff *skb, 332106499098SVlad Yasevich u16 vid) 332208b202b6SYOSHIFUJI Hideaki { 3323ba5ea614SLinus Lüssing unsigned int transport_len = ipv6_transport_len(skb); 3324eca2a43bSstephen hemminger struct mld_msg *mld; 332508b202b6SYOSHIFUJI Hideaki struct net_bridge_mdb_entry *mp; 332608b202b6SYOSHIFUJI Hideaki struct mld2_query *mld2q; 3327e8051688SEric Dumazet struct net_bridge_port_group *p; 3328e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 332967b746f9SNikolay Aleksandrov struct br_ip saddr = {}; 333008b202b6SYOSHIFUJI Hideaki unsigned long max_delay; 333108b202b6SYOSHIFUJI Hideaki unsigned long now = jiffies; 3332856ce5d0SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 3333b71d1d42SEric Dumazet const struct in6_addr *group = NULL; 33349ed973ccSLinus Lüssing bool is_general_query; 333508b202b6SYOSHIFUJI Hideaki int err = 0; 333608b202b6SYOSHIFUJI Hideaki 3337adc47037SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 33384cdd0d10SNikolay Aleksandrov if (!br_multicast_ctx_should_use(brmctx, pmctx)) 333908b202b6SYOSHIFUJI Hideaki goto out; 334008b202b6SYOSHIFUJI Hideaki 3341ba5ea614SLinus Lüssing if (transport_len == sizeof(*mld)) { 3342856ce5d0SLinus Lüssing if (!pskb_may_pull(skb, offset + sizeof(*mld))) { 334308b202b6SYOSHIFUJI Hideaki err = -EINVAL; 334408b202b6SYOSHIFUJI Hideaki goto out; 334508b202b6SYOSHIFUJI Hideaki } 334608b202b6SYOSHIFUJI Hideaki mld = (struct mld_msg *) icmp6_hdr(skb); 33474715213dSLi RongQing max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay)); 334808b202b6SYOSHIFUJI Hideaki if (max_delay) 334908b202b6SYOSHIFUJI Hideaki group = &mld->mld_mca; 3350248ba8ecSLinus Lüssing } else { 3351856ce5d0SLinus Lüssing if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) { 335208b202b6SYOSHIFUJI Hideaki err = -EINVAL; 335308b202b6SYOSHIFUJI Hideaki goto out; 335408b202b6SYOSHIFUJI Hideaki } 335508b202b6SYOSHIFUJI Hideaki mld2q = (struct mld2_query *)icmp6_hdr(skb); 335608b202b6SYOSHIFUJI Hideaki if (!mld2q->mld2q_nsrcs) 335708b202b6SYOSHIFUJI Hideaki group = &mld2q->mld2q_mca; 3358d3d065c0SNikolay Aleksandrov if (brmctx->multicast_mld_version == 2 && 335923550b83SNikolay Aleksandrov !ipv6_addr_any(&mld2q->mld2q_mca) && 336023550b83SNikolay Aleksandrov mld2q->mld2q_suppress) 336123550b83SNikolay Aleksandrov goto out; 3362e3f5b170SDaniel Borkmann 3363e3f5b170SDaniel Borkmann max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL); 336408b202b6SYOSHIFUJI Hideaki } 336508b202b6SYOSHIFUJI Hideaki 33669ed973ccSLinus Lüssing is_general_query = group && ipv6_addr_any(group); 33679ed973ccSLinus Lüssing 3368dc4eb53aSLinus Lüssing if (is_general_query) { 3369dc4eb53aSLinus Lüssing saddr.proto = htons(ETH_P_IPV6); 3370deb96566SNikolay Aleksandrov saddr.src.ip6 = ipv6_hdr(skb)->saddr; 3371b00589afSLinus Lüssing 3372adc47037SNikolay Aleksandrov br_ip6_multicast_query_received(brmctx, pmctx, 3373d3d065c0SNikolay Aleksandrov &brmctx->ip6_other_query, 3374dc4eb53aSLinus Lüssing &saddr, max_delay); 337508b202b6SYOSHIFUJI Hideaki goto out; 33766c03ee8bSLinus Lüssing } else if (!group) { 33776c03ee8bSLinus Lüssing goto out; 3378dc4eb53aSLinus Lüssing } 337908b202b6SYOSHIFUJI Hideaki 3380adc47037SNikolay Aleksandrov mp = br_mdb_ip6_get(brmctx->br, group, vid); 338108b202b6SYOSHIFUJI Hideaki if (!mp) 338208b202b6SYOSHIFUJI Hideaki goto out; 338308b202b6SYOSHIFUJI Hideaki 3384d3d065c0SNikolay Aleksandrov max_delay *= brmctx->multicast_last_member_count; 3385ff0fd34eSAndrew Lunn if (mp->host_joined && 338608b202b6SYOSHIFUJI Hideaki (timer_pending(&mp->timer) ? 338708b202b6SYOSHIFUJI Hideaki time_after(mp->timer.expires, now + max_delay) : 338808b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&mp->timer) >= 0)) 338908b202b6SYOSHIFUJI Hideaki mod_timer(&mp->timer, now + max_delay); 339008b202b6SYOSHIFUJI Hideaki 3391e8051688SEric Dumazet for (pp = &mp->ports; 3392adc47037SNikolay Aleksandrov (p = mlock_dereference(*pp, brmctx->br)) != NULL; 3393e8051688SEric Dumazet pp = &p->next) { 339408b202b6SYOSHIFUJI Hideaki if (timer_pending(&p->timer) ? 339508b202b6SYOSHIFUJI Hideaki time_after(p->timer.expires, now + max_delay) : 339623550b83SNikolay Aleksandrov try_to_del_timer_sync(&p->timer) >= 0 && 3397d3d065c0SNikolay Aleksandrov (brmctx->multicast_mld_version == 1 || 339823550b83SNikolay Aleksandrov p->filter_mode == MCAST_EXCLUDE)) 339924f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 340008b202b6SYOSHIFUJI Hideaki } 340108b202b6SYOSHIFUJI Hideaki 340208b202b6SYOSHIFUJI Hideaki out: 3403adc47037SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 340408b202b6SYOSHIFUJI Hideaki return err; 340508b202b6SYOSHIFUJI Hideaki } 340608b202b6SYOSHIFUJI Hideaki #endif 340708b202b6SYOSHIFUJI Hideaki 340890010b36SLinus Lüssing static void 3409adc47037SNikolay Aleksandrov br_multicast_leave_group(struct net_bridge_mcast *brmctx, 3410adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 3411cc0fdd80SLinus Lüssing struct br_ip *group, 341290010b36SLinus Lüssing struct bridge_mcast_other_query *other_query, 34136db6f0eaSFelix Fietkau struct bridge_mcast_own_query *own_query, 34146db6f0eaSFelix Fietkau const unsigned char *src) 3415eb1d1641SHerbert Xu { 3416eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 3417eb1d1641SHerbert Xu struct net_bridge_port_group *p; 3418eb1d1641SHerbert Xu unsigned long now; 3419eb1d1641SHerbert Xu unsigned long time; 3420eb1d1641SHerbert Xu 3421adc47037SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 34224cdd0d10SNikolay Aleksandrov if (!br_multicast_ctx_should_use(brmctx, pmctx)) 3423eb1d1641SHerbert Xu goto out; 3424eb1d1641SHerbert Xu 3425adc47037SNikolay Aleksandrov mp = br_mdb_ip_get(brmctx->br, group); 3426eb1d1641SHerbert Xu if (!mp) 3427eb1d1641SHerbert Xu goto out; 3428eb1d1641SHerbert Xu 3429adc47037SNikolay Aleksandrov if (pmctx && (pmctx->port->flags & BR_MULTICAST_FAST_LEAVE)) { 3430544586f7SSatish Ashok struct net_bridge_port_group __rcu **pp; 3431544586f7SSatish Ashok 3432544586f7SSatish Ashok for (pp = &mp->ports; 3433adc47037SNikolay Aleksandrov (p = mlock_dereference(*pp, brmctx->br)) != NULL; 3434544586f7SSatish Ashok pp = &p->next) { 3435adc47037SNikolay Aleksandrov if (!br_port_group_equal(p, pmctx->port, src)) 3436544586f7SSatish Ashok continue; 3437544586f7SSatish Ashok 34385c725b6bSNikolay Aleksandrov if (p->flags & MDB_PG_FLAGS_PERMANENT) 34395c725b6bSNikolay Aleksandrov break; 34405c725b6bSNikolay Aleksandrov 3441681590bdSNikolay Aleksandrov p->flags |= MDB_PG_FLAGS_FAST_LEAVE; 3442681590bdSNikolay Aleksandrov br_multicast_del_pg(mp, p, pp); 3443544586f7SSatish Ashok } 3444544586f7SSatish Ashok goto out; 3445544586f7SSatish Ashok } 3446544586f7SSatish Ashok 3447544586f7SSatish Ashok if (timer_pending(&other_query->timer)) 3448544586f7SSatish Ashok goto out; 3449544586f7SSatish Ashok 34504d5b4e84SNikolay Aleksandrov if (brmctx->multicast_querier) { 3451adc47037SNikolay Aleksandrov __br_multicast_send_query(brmctx, pmctx, NULL, NULL, &mp->addr, 345242c11ccfSNikolay Aleksandrov false, 0, NULL); 34536b7df111SCong Wang 3454d3d065c0SNikolay Aleksandrov time = jiffies + brmctx->multicast_last_member_count * 3455d3d065c0SNikolay Aleksandrov brmctx->multicast_last_member_interval; 3456cc0fdd80SLinus Lüssing 345790010b36SLinus Lüssing mod_timer(&own_query->timer, time); 34586b7df111SCong Wang 3459adc47037SNikolay Aleksandrov for (p = mlock_dereference(mp->ports, brmctx->br); 346054cb4319SNikolay Aleksandrov p != NULL && pmctx != NULL; 3461adc47037SNikolay Aleksandrov p = mlock_dereference(p->next, brmctx->br)) { 3462adc47037SNikolay Aleksandrov if (!br_port_group_equal(p, pmctx->port, src)) 34636b7df111SCong Wang continue; 34646b7df111SCong Wang 34656b7df111SCong Wang if (!hlist_unhashed(&p->mglist) && 34666b7df111SCong Wang (timer_pending(&p->timer) ? 34676b7df111SCong Wang time_after(p->timer.expires, time) : 34686b7df111SCong Wang try_to_del_timer_sync(&p->timer) >= 0)) { 34696b7df111SCong Wang mod_timer(&p->timer, time); 34706b7df111SCong Wang } 34716b7df111SCong Wang 34726b7df111SCong Wang break; 34736b7df111SCong Wang } 34746b7df111SCong Wang } 34756b7df111SCong Wang 3476eb1d1641SHerbert Xu now = jiffies; 3477d3d065c0SNikolay Aleksandrov time = now + brmctx->multicast_last_member_count * 3478d3d065c0SNikolay Aleksandrov brmctx->multicast_last_member_interval; 3479eb1d1641SHerbert Xu 3480adc47037SNikolay Aleksandrov if (!pmctx) { 3481ff0fd34eSAndrew Lunn if (mp->host_joined && 3482eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 3483eb1d1641SHerbert Xu time_after(mp->timer.expires, time) : 3484eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) { 3485eb1d1641SHerbert Xu mod_timer(&mp->timer, time); 3486eb1d1641SHerbert Xu } 3487454594f3SLinus Lüssing 3488454594f3SLinus Lüssing goto out; 3489454594f3SLinus Lüssing } 3490454594f3SLinus Lüssing 3491adc47037SNikolay Aleksandrov for (p = mlock_dereference(mp->ports, brmctx->br); 3492454594f3SLinus Lüssing p != NULL; 3493adc47037SNikolay Aleksandrov p = mlock_dereference(p->next, brmctx->br)) { 3494adc47037SNikolay Aleksandrov if (p->key.port != pmctx->port) 3495454594f3SLinus Lüssing continue; 3496454594f3SLinus Lüssing 3497454594f3SLinus Lüssing if (!hlist_unhashed(&p->mglist) && 3498454594f3SLinus Lüssing (timer_pending(&p->timer) ? 3499454594f3SLinus Lüssing time_after(p->timer.expires, time) : 3500454594f3SLinus Lüssing try_to_del_timer_sync(&p->timer) >= 0)) { 3501454594f3SLinus Lüssing mod_timer(&p->timer, time); 3502454594f3SLinus Lüssing } 3503454594f3SLinus Lüssing 3504454594f3SLinus Lüssing break; 3505eb1d1641SHerbert Xu } 3506eb1d1641SHerbert Xu out: 3507adc47037SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 3508eb1d1641SHerbert Xu } 3509eb1d1641SHerbert Xu 3510adc47037SNikolay Aleksandrov static void br_ip4_multicast_leave_group(struct net_bridge_mcast *brmctx, 3511adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 3512b0e9a30dSVlad Yasevich __be32 group, 35136db6f0eaSFelix Fietkau __u16 vid, 35146db6f0eaSFelix Fietkau const unsigned char *src) 35158ef2a9a5SYOSHIFUJI Hideaki { 35168ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 351790010b36SLinus Lüssing struct bridge_mcast_own_query *own_query; 35188ef2a9a5SYOSHIFUJI Hideaki 35198ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 35208ef2a9a5SYOSHIFUJI Hideaki return; 35218ef2a9a5SYOSHIFUJI Hideaki 3522adc47037SNikolay Aleksandrov own_query = pmctx ? &pmctx->ip4_own_query : &brmctx->ip4_own_query; 352390010b36SLinus Lüssing 35241515a63fSNikolay Aleksandrov memset(&br_group, 0, sizeof(br_group)); 3525eab3227bSNikolay Aleksandrov br_group.dst.ip4 = group; 35268ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 3527b0e9a30dSVlad Yasevich br_group.vid = vid; 35288ef2a9a5SYOSHIFUJI Hideaki 3529adc47037SNikolay Aleksandrov br_multicast_leave_group(brmctx, pmctx, &br_group, 3530adc47037SNikolay Aleksandrov &brmctx->ip4_other_query, 35316db6f0eaSFelix Fietkau own_query, src); 35328ef2a9a5SYOSHIFUJI Hideaki } 35338ef2a9a5SYOSHIFUJI Hideaki 3534dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 3535adc47037SNikolay Aleksandrov static void br_ip6_multicast_leave_group(struct net_bridge_mcast *brmctx, 3536adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 3537b0e9a30dSVlad Yasevich const struct in6_addr *group, 35386db6f0eaSFelix Fietkau __u16 vid, 35396db6f0eaSFelix Fietkau const unsigned char *src) 354008b202b6SYOSHIFUJI Hideaki { 354108b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 354290010b36SLinus Lüssing struct bridge_mcast_own_query *own_query; 354308b202b6SYOSHIFUJI Hideaki 35443c3769e6SLinus Lüssing if (ipv6_addr_is_ll_all_nodes(group)) 354508b202b6SYOSHIFUJI Hideaki return; 354608b202b6SYOSHIFUJI Hideaki 3547adc47037SNikolay Aleksandrov own_query = pmctx ? &pmctx->ip6_own_query : &brmctx->ip6_own_query; 354890010b36SLinus Lüssing 35491515a63fSNikolay Aleksandrov memset(&br_group, 0, sizeof(br_group)); 3550eab3227bSNikolay Aleksandrov br_group.dst.ip6 = *group; 355108b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 3552b0e9a30dSVlad Yasevich br_group.vid = vid; 355308b202b6SYOSHIFUJI Hideaki 3554adc47037SNikolay Aleksandrov br_multicast_leave_group(brmctx, pmctx, &br_group, 3555adc47037SNikolay Aleksandrov &brmctx->ip6_other_query, 35566db6f0eaSFelix Fietkau own_query, src); 355708b202b6SYOSHIFUJI Hideaki } 355808b202b6SYOSHIFUJI Hideaki #endif 35598ef2a9a5SYOSHIFUJI Hideaki 35601080ab95SNikolay Aleksandrov static void br_multicast_err_count(const struct net_bridge *br, 35611080ab95SNikolay Aleksandrov const struct net_bridge_port *p, 35621080ab95SNikolay Aleksandrov __be16 proto) 35631080ab95SNikolay Aleksandrov { 35641080ab95SNikolay Aleksandrov struct bridge_mcast_stats __percpu *stats; 35651080ab95SNikolay Aleksandrov struct bridge_mcast_stats *pstats; 35661080ab95SNikolay Aleksandrov 3567675779adSNikolay Aleksandrov if (!br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)) 35681080ab95SNikolay Aleksandrov return; 35691080ab95SNikolay Aleksandrov 35701080ab95SNikolay Aleksandrov if (p) 35711080ab95SNikolay Aleksandrov stats = p->mcast_stats; 35721080ab95SNikolay Aleksandrov else 35731080ab95SNikolay Aleksandrov stats = br->mcast_stats; 35741080ab95SNikolay Aleksandrov if (WARN_ON(!stats)) 35751080ab95SNikolay Aleksandrov return; 35761080ab95SNikolay Aleksandrov 35771080ab95SNikolay Aleksandrov pstats = this_cpu_ptr(stats); 35781080ab95SNikolay Aleksandrov 35791080ab95SNikolay Aleksandrov u64_stats_update_begin(&pstats->syncp); 35801080ab95SNikolay Aleksandrov switch (proto) { 35811080ab95SNikolay Aleksandrov case htons(ETH_P_IP): 35821080ab95SNikolay Aleksandrov pstats->mstats.igmp_parse_errors++; 35831080ab95SNikolay Aleksandrov break; 35841080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 35851080ab95SNikolay Aleksandrov case htons(ETH_P_IPV6): 35861080ab95SNikolay Aleksandrov pstats->mstats.mld_parse_errors++; 35871080ab95SNikolay Aleksandrov break; 35881080ab95SNikolay Aleksandrov #endif 35891080ab95SNikolay Aleksandrov } 35901080ab95SNikolay Aleksandrov u64_stats_update_end(&pstats->syncp); 35911080ab95SNikolay Aleksandrov } 35921080ab95SNikolay Aleksandrov 3593adc47037SNikolay Aleksandrov static void br_multicast_pim(struct net_bridge_mcast *brmctx, 3594adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 359591b02d3dSNikolay Aleksandrov const struct sk_buff *skb) 359691b02d3dSNikolay Aleksandrov { 359791b02d3dSNikolay Aleksandrov unsigned int offset = skb_transport_offset(skb); 359891b02d3dSNikolay Aleksandrov struct pimhdr *pimhdr, _pimhdr; 359991b02d3dSNikolay Aleksandrov 360091b02d3dSNikolay Aleksandrov pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr); 360191b02d3dSNikolay Aleksandrov if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION || 360291b02d3dSNikolay Aleksandrov pim_hdr_type(pimhdr) != PIM_TYPE_HELLO) 360391b02d3dSNikolay Aleksandrov return; 360491b02d3dSNikolay Aleksandrov 3605adc47037SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 3606adc47037SNikolay Aleksandrov br_ip4_multicast_mark_router(brmctx, pmctx); 3607adc47037SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 360891b02d3dSNikolay Aleksandrov } 360991b02d3dSNikolay Aleksandrov 3610adc47037SNikolay Aleksandrov static int br_ip4_multicast_mrd_rcv(struct net_bridge_mcast *brmctx, 3611adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 36124b3087c7SLinus Lüssing struct sk_buff *skb) 36134b3087c7SLinus Lüssing { 36144b3087c7SLinus Lüssing if (ip_hdr(skb)->protocol != IPPROTO_IGMP || 36154b3087c7SLinus Lüssing igmp_hdr(skb)->type != IGMP_MRDISC_ADV) 36164b3087c7SLinus Lüssing return -ENOMSG; 36174b3087c7SLinus Lüssing 3618adc47037SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 3619adc47037SNikolay Aleksandrov br_ip4_multicast_mark_router(brmctx, pmctx); 3620adc47037SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 36214b3087c7SLinus Lüssing 36224b3087c7SLinus Lüssing return 0; 36234b3087c7SLinus Lüssing } 36244b3087c7SLinus Lüssing 3625adc47037SNikolay Aleksandrov static int br_multicast_ipv4_rcv(struct net_bridge_mcast *brmctx, 3626adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 362706499098SVlad Yasevich struct sk_buff *skb, 362806499098SVlad Yasevich u16 vid) 3629eb1d1641SHerbert Xu { 3630adc47037SNikolay Aleksandrov struct net_bridge_port *p = pmctx ? pmctx->port : NULL; 36316db6f0eaSFelix Fietkau const unsigned char *src; 3632eb1d1641SHerbert Xu struct igmphdr *ih; 3633eb1d1641SHerbert Xu int err; 3634eb1d1641SHerbert Xu 3635ba5ea614SLinus Lüssing err = ip_mc_check_igmp(skb); 3636eb1d1641SHerbert Xu 36379afd85c9SLinus Lüssing if (err == -ENOMSG) { 363891b02d3dSNikolay Aleksandrov if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) { 3639bd4265feSHerbert Xu BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 364091b02d3dSNikolay Aleksandrov } else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) { 364191b02d3dSNikolay Aleksandrov if (ip_hdr(skb)->protocol == IPPROTO_PIM) 3642adc47037SNikolay Aleksandrov br_multicast_pim(brmctx, pmctx, skb); 36434b3087c7SLinus Lüssing } else if (ipv4_is_all_snoopers(ip_hdr(skb)->daddr)) { 3644adc47037SNikolay Aleksandrov br_ip4_multicast_mrd_rcv(brmctx, pmctx, skb); 36454b3087c7SLinus Lüssing } 36464b3087c7SLinus Lüssing 3647eb1d1641SHerbert Xu return 0; 36489afd85c9SLinus Lüssing } else if (err < 0) { 3649adc47037SNikolay Aleksandrov br_multicast_err_count(brmctx->br, p, skb->protocol); 36509afd85c9SLinus Lüssing return err; 3651bd4265feSHerbert Xu } 3652eb1d1641SHerbert Xu 36539afd85c9SLinus Lüssing ih = igmp_hdr(skb); 36546db6f0eaSFelix Fietkau src = eth_hdr(skb)->h_source; 36551080ab95SNikolay Aleksandrov BR_INPUT_SKB_CB(skb)->igmp = ih->type; 3656eb1d1641SHerbert Xu 3657eb1d1641SHerbert Xu switch (ih->type) { 3658eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_REPORT: 3659eb1d1641SHerbert Xu case IGMPV2_HOST_MEMBERSHIP_REPORT: 366062b2bcb4SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 3661adc47037SNikolay Aleksandrov err = br_ip4_multicast_add_group(brmctx, pmctx, ih->group, vid, 3662adc47037SNikolay Aleksandrov src, true); 3663eb1d1641SHerbert Xu break; 3664eb1d1641SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 3665adc47037SNikolay Aleksandrov err = br_ip4_multicast_igmp3_report(brmctx, pmctx, skb, vid); 3666eb1d1641SHerbert Xu break; 3667eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_QUERY: 3668adc47037SNikolay Aleksandrov br_ip4_multicast_query(brmctx, pmctx, skb, vid); 3669eb1d1641SHerbert Xu break; 3670eb1d1641SHerbert Xu case IGMP_HOST_LEAVE_MESSAGE: 3671adc47037SNikolay Aleksandrov br_ip4_multicast_leave_group(brmctx, pmctx, ih->group, vid, src); 3672eb1d1641SHerbert Xu break; 3673eb1d1641SHerbert Xu } 3674eb1d1641SHerbert Xu 3675adc47037SNikolay Aleksandrov br_multicast_count(brmctx->br, p, skb, BR_INPUT_SKB_CB(skb)->igmp, 36761080ab95SNikolay Aleksandrov BR_MCAST_DIR_RX); 36771080ab95SNikolay Aleksandrov 3678eb1d1641SHerbert Xu return err; 3679eb1d1641SHerbert Xu } 3680eb1d1641SHerbert Xu 3681dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 3682adc47037SNikolay Aleksandrov static void br_ip6_multicast_mrd_rcv(struct net_bridge_mcast *brmctx, 3683adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 36844b3087c7SLinus Lüssing struct sk_buff *skb) 36854b3087c7SLinus Lüssing { 36864b3087c7SLinus Lüssing if (icmp6_hdr(skb)->icmp6_type != ICMPV6_MRDISC_ADV) 368799014088SLinus Lüssing return; 36884b3087c7SLinus Lüssing 3689adc47037SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 3690adc47037SNikolay Aleksandrov br_ip6_multicast_mark_router(brmctx, pmctx); 3691adc47037SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 36924b3087c7SLinus Lüssing } 36934b3087c7SLinus Lüssing 3694adc47037SNikolay Aleksandrov static int br_multicast_ipv6_rcv(struct net_bridge_mcast *brmctx, 3695adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 369606499098SVlad Yasevich struct sk_buff *skb, 369706499098SVlad Yasevich u16 vid) 369808b202b6SYOSHIFUJI Hideaki { 3699adc47037SNikolay Aleksandrov struct net_bridge_port *p = pmctx ? pmctx->port : NULL; 37006db6f0eaSFelix Fietkau const unsigned char *src; 37019afd85c9SLinus Lüssing struct mld_msg *mld; 370208b202b6SYOSHIFUJI Hideaki int err; 370308b202b6SYOSHIFUJI Hideaki 3704ba5ea614SLinus Lüssing err = ipv6_mc_check_mld(skb); 370508b202b6SYOSHIFUJI Hideaki 370699014088SLinus Lüssing if (err == -ENOMSG || err == -ENODATA) { 37079afd85c9SLinus Lüssing if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr)) 37088fad9c39SLinus Lüssing BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 370999014088SLinus Lüssing if (err == -ENODATA && 371099014088SLinus Lüssing ipv6_addr_is_all_snoopers(&ipv6_hdr(skb)->daddr)) 3711adc47037SNikolay Aleksandrov br_ip6_multicast_mrd_rcv(brmctx, pmctx, skb); 37124b3087c7SLinus Lüssing 371308b202b6SYOSHIFUJI Hideaki return 0; 37149afd85c9SLinus Lüssing } else if (err < 0) { 3715adc47037SNikolay Aleksandrov br_multicast_err_count(brmctx->br, p, skb->protocol); 37169afd85c9SLinus Lüssing return err; 371708b202b6SYOSHIFUJI Hideaki } 371808b202b6SYOSHIFUJI Hideaki 37199afd85c9SLinus Lüssing mld = (struct mld_msg *)skb_transport_header(skb); 37201080ab95SNikolay Aleksandrov BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type; 372108b202b6SYOSHIFUJI Hideaki 37229afd85c9SLinus Lüssing switch (mld->mld_type) { 372308b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 37246db6f0eaSFelix Fietkau src = eth_hdr(skb)->h_source; 3725fc2af6c7SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 3726adc47037SNikolay Aleksandrov err = br_ip6_multicast_add_group(brmctx, pmctx, &mld->mld_mca, 3727adc47037SNikolay Aleksandrov vid, src, true); 372808b202b6SYOSHIFUJI Hideaki break; 372908b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 3730adc47037SNikolay Aleksandrov err = br_ip6_multicast_mld2_report(brmctx, pmctx, skb, vid); 373108b202b6SYOSHIFUJI Hideaki break; 373208b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 3733adc47037SNikolay Aleksandrov err = br_ip6_multicast_query(brmctx, pmctx, skb, vid); 373408b202b6SYOSHIFUJI Hideaki break; 373508b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 37366db6f0eaSFelix Fietkau src = eth_hdr(skb)->h_source; 3737adc47037SNikolay Aleksandrov br_ip6_multicast_leave_group(brmctx, pmctx, &mld->mld_mca, vid, 3738adc47037SNikolay Aleksandrov src); 37399afd85c9SLinus Lüssing break; 374008b202b6SYOSHIFUJI Hideaki } 374108b202b6SYOSHIFUJI Hideaki 3742adc47037SNikolay Aleksandrov br_multicast_count(brmctx->br, p, skb, BR_INPUT_SKB_CB(skb)->igmp, 37431080ab95SNikolay Aleksandrov BR_MCAST_DIR_RX); 37441080ab95SNikolay Aleksandrov 374508b202b6SYOSHIFUJI Hideaki return err; 374608b202b6SYOSHIFUJI Hideaki } 374708b202b6SYOSHIFUJI Hideaki #endif 374808b202b6SYOSHIFUJI Hideaki 3749f4b7002aSNikolay Aleksandrov int br_multicast_rcv(struct net_bridge_mcast **brmctx, 3750f4b7002aSNikolay Aleksandrov struct net_bridge_mcast_port **pmctx, 3751f4b7002aSNikolay Aleksandrov struct net_bridge_vlan *vlan, 375206499098SVlad Yasevich struct sk_buff *skb, u16 vid) 3753eb1d1641SHerbert Xu { 37541080ab95SNikolay Aleksandrov int ret = 0; 37551080ab95SNikolay Aleksandrov 37561fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->igmp = 0; 37571fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->mrouters_only = 0; 37581fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 3759f4b7002aSNikolay Aleksandrov if (!br_opt_get((*brmctx)->br, BROPT_MULTICAST_ENABLED)) 3760eb1d1641SHerbert Xu return 0; 3761eb1d1641SHerbert Xu 3762f4b7002aSNikolay Aleksandrov if (br_opt_get((*brmctx)->br, BROPT_MCAST_VLAN_SNOOPING_ENABLED) && vlan) { 3763f4b7002aSNikolay Aleksandrov const struct net_bridge_vlan *masterv; 3764f4b7002aSNikolay Aleksandrov 3765f4b7002aSNikolay Aleksandrov /* the vlan has the master flag set only when transmitting 3766f4b7002aSNikolay Aleksandrov * through the bridge device 3767f4b7002aSNikolay Aleksandrov */ 3768f4b7002aSNikolay Aleksandrov if (br_vlan_is_master(vlan)) { 3769f4b7002aSNikolay Aleksandrov masterv = vlan; 3770f4b7002aSNikolay Aleksandrov *brmctx = &vlan->br_mcast_ctx; 3771f4b7002aSNikolay Aleksandrov *pmctx = NULL; 3772f4b7002aSNikolay Aleksandrov } else { 3773f4b7002aSNikolay Aleksandrov masterv = vlan->brvlan; 3774f4b7002aSNikolay Aleksandrov *brmctx = &vlan->brvlan->br_mcast_ctx; 3775f4b7002aSNikolay Aleksandrov *pmctx = &vlan->port_mcast_ctx; 3776f4b7002aSNikolay Aleksandrov } 3777f4b7002aSNikolay Aleksandrov 3778f4b7002aSNikolay Aleksandrov if (!(masterv->priv_flags & BR_VLFLAG_GLOBAL_MCAST_ENABLED)) 3779f4b7002aSNikolay Aleksandrov return 0; 3780f4b7002aSNikolay Aleksandrov } 3781f4b7002aSNikolay Aleksandrov 3782eb1d1641SHerbert Xu switch (skb->protocol) { 3783eb1d1641SHerbert Xu case htons(ETH_P_IP): 3784f4b7002aSNikolay Aleksandrov ret = br_multicast_ipv4_rcv(*brmctx, *pmctx, skb, vid); 37851080ab95SNikolay Aleksandrov break; 3786dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 378708b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 3788f4b7002aSNikolay Aleksandrov ret = br_multicast_ipv6_rcv(*brmctx, *pmctx, skb, vid); 37891080ab95SNikolay Aleksandrov break; 379008b202b6SYOSHIFUJI Hideaki #endif 3791eb1d1641SHerbert Xu } 3792eb1d1641SHerbert Xu 37931080ab95SNikolay Aleksandrov return ret; 3794eb1d1641SHerbert Xu } 3795eb1d1641SHerbert Xu 3796adc47037SNikolay Aleksandrov static void br_multicast_query_expired(struct net_bridge_mcast *brmctx, 37972cd41431SLinus Lüssing struct bridge_mcast_own_query *query, 37982cd41431SLinus Lüssing struct bridge_mcast_querier *querier) 3799cc0fdd80SLinus Lüssing { 3800adc47037SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 38014cdd0d10SNikolay Aleksandrov if (br_multicast_ctx_vlan_disabled(brmctx)) 38024cdd0d10SNikolay Aleksandrov goto out; 38034cdd0d10SNikolay Aleksandrov 3804adc47037SNikolay Aleksandrov if (query->startup_sent < brmctx->multicast_startup_query_count) 3805cc0fdd80SLinus Lüssing query->startup_sent++; 3806cc0fdd80SLinus Lüssing 3807adc47037SNikolay Aleksandrov br_multicast_send_query(brmctx, NULL, query); 38084cdd0d10SNikolay Aleksandrov out: 3809adc47037SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 3810cc0fdd80SLinus Lüssing } 3811cc0fdd80SLinus Lüssing 381288c1f37fSAllen Pais static void br_ip4_multicast_query_expired(struct timer_list *t) 3813eb1d1641SHerbert Xu { 3814d3d065c0SNikolay Aleksandrov struct net_bridge_mcast *brmctx = from_timer(brmctx, t, 3815d3d065c0SNikolay Aleksandrov ip4_own_query.timer); 3816eb1d1641SHerbert Xu 3817adc47037SNikolay Aleksandrov br_multicast_query_expired(brmctx, &brmctx->ip4_own_query, 3818d3d065c0SNikolay Aleksandrov &brmctx->ip4_querier); 3819eb1d1641SHerbert Xu } 3820eb1d1641SHerbert Xu 3821cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 382288c1f37fSAllen Pais static void br_ip6_multicast_query_expired(struct timer_list *t) 3823cc0fdd80SLinus Lüssing { 3824d3d065c0SNikolay Aleksandrov struct net_bridge_mcast *brmctx = from_timer(brmctx, t, 3825d3d065c0SNikolay Aleksandrov ip6_own_query.timer); 3826cc0fdd80SLinus Lüssing 3827adc47037SNikolay Aleksandrov br_multicast_query_expired(brmctx, &brmctx->ip6_own_query, 3828d3d065c0SNikolay Aleksandrov &brmctx->ip6_querier); 3829cc0fdd80SLinus Lüssing } 3830cc0fdd80SLinus Lüssing #endif 3831cc0fdd80SLinus Lüssing 3832e12cec65SNikolay Aleksandrov static void br_multicast_gc_work(struct work_struct *work) 38338b671779SNikolay Aleksandrov { 38348b671779SNikolay Aleksandrov struct net_bridge *br = container_of(work, struct net_bridge, 3835e12cec65SNikolay Aleksandrov mcast_gc_work); 38368b671779SNikolay Aleksandrov HLIST_HEAD(deleted_head); 38378b671779SNikolay Aleksandrov 38388b671779SNikolay Aleksandrov spin_lock_bh(&br->multicast_lock); 3839e12cec65SNikolay Aleksandrov hlist_move_list(&br->mcast_gc_list, &deleted_head); 38408b671779SNikolay Aleksandrov spin_unlock_bh(&br->multicast_lock); 38418b671779SNikolay Aleksandrov 3842e12cec65SNikolay Aleksandrov br_multicast_gc(&deleted_head); 38438b671779SNikolay Aleksandrov } 38448b671779SNikolay Aleksandrov 3845613d61dbSNikolay Aleksandrov void br_multicast_ctx_init(struct net_bridge *br, 3846613d61dbSNikolay Aleksandrov struct net_bridge_vlan *vlan, 3847613d61dbSNikolay Aleksandrov struct net_bridge_mcast *brmctx) 3848613d61dbSNikolay Aleksandrov { 3849613d61dbSNikolay Aleksandrov brmctx->br = br; 3850613d61dbSNikolay Aleksandrov brmctx->vlan = vlan; 3851613d61dbSNikolay Aleksandrov brmctx->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 3852613d61dbSNikolay Aleksandrov brmctx->multicast_last_member_count = 2; 3853613d61dbSNikolay Aleksandrov brmctx->multicast_startup_query_count = 2; 3854613d61dbSNikolay Aleksandrov 3855613d61dbSNikolay Aleksandrov brmctx->multicast_last_member_interval = HZ; 3856613d61dbSNikolay Aleksandrov brmctx->multicast_query_response_interval = 10 * HZ; 3857613d61dbSNikolay Aleksandrov brmctx->multicast_startup_query_interval = 125 * HZ / 4; 3858613d61dbSNikolay Aleksandrov brmctx->multicast_query_interval = 125 * HZ; 3859613d61dbSNikolay Aleksandrov brmctx->multicast_querier_interval = 255 * HZ; 3860613d61dbSNikolay Aleksandrov brmctx->multicast_membership_interval = 260 * HZ; 3861613d61dbSNikolay Aleksandrov 3862613d61dbSNikolay Aleksandrov brmctx->ip4_other_query.delay_time = 0; 3863bb18ef8eSNikolay Aleksandrov brmctx->ip4_querier.port_ifidx = 0; 386467b746f9SNikolay Aleksandrov seqcount_init(&brmctx->ip4_querier.seq); 3865613d61dbSNikolay Aleksandrov brmctx->multicast_igmp_version = 2; 3866613d61dbSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 3867613d61dbSNikolay Aleksandrov brmctx->multicast_mld_version = 1; 3868613d61dbSNikolay Aleksandrov brmctx->ip6_other_query.delay_time = 0; 3869bb18ef8eSNikolay Aleksandrov brmctx->ip6_querier.port_ifidx = 0; 387067b746f9SNikolay Aleksandrov seqcount_init(&brmctx->ip6_querier.seq); 3871613d61dbSNikolay Aleksandrov #endif 3872613d61dbSNikolay Aleksandrov 3873613d61dbSNikolay Aleksandrov timer_setup(&brmctx->ip4_mc_router_timer, 3874613d61dbSNikolay Aleksandrov br_ip4_multicast_local_router_expired, 0); 3875613d61dbSNikolay Aleksandrov timer_setup(&brmctx->ip4_other_query.timer, 3876613d61dbSNikolay Aleksandrov br_ip4_multicast_querier_expired, 0); 3877613d61dbSNikolay Aleksandrov timer_setup(&brmctx->ip4_own_query.timer, 3878613d61dbSNikolay Aleksandrov br_ip4_multicast_query_expired, 0); 3879613d61dbSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 3880613d61dbSNikolay Aleksandrov timer_setup(&brmctx->ip6_mc_router_timer, 3881613d61dbSNikolay Aleksandrov br_ip6_multicast_local_router_expired, 0); 3882613d61dbSNikolay Aleksandrov timer_setup(&brmctx->ip6_other_query.timer, 3883613d61dbSNikolay Aleksandrov br_ip6_multicast_querier_expired, 0); 3884613d61dbSNikolay Aleksandrov timer_setup(&brmctx->ip6_own_query.timer, 3885613d61dbSNikolay Aleksandrov br_ip6_multicast_query_expired, 0); 3886613d61dbSNikolay Aleksandrov #endif 3887613d61dbSNikolay Aleksandrov } 3888613d61dbSNikolay Aleksandrov 3889613d61dbSNikolay Aleksandrov void br_multicast_ctx_deinit(struct net_bridge_mcast *brmctx) 3890613d61dbSNikolay Aleksandrov { 3891613d61dbSNikolay Aleksandrov __br_multicast_stop(brmctx); 3892613d61dbSNikolay Aleksandrov } 3893613d61dbSNikolay Aleksandrov 3894eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br) 3895eb1d1641SHerbert Xu { 3896d08c6bc0SNikolay Aleksandrov br->hash_max = BR_MULTICAST_DEFAULT_HASH_MAX; 3897eb1d1641SHerbert Xu 3898613d61dbSNikolay Aleksandrov br_multicast_ctx_init(br, NULL, &br->multicast_ctx); 3899eb1d1641SHerbert Xu 39006919622aSIdo Schimmel br_opt_toggle(br, BROPT_MULTICAST_ENABLED, true); 3901675779adSNikolay Aleksandrov br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true); 3902b00589afSLinus Lüssing 3903eb1d1641SHerbert Xu spin_lock_init(&br->multicast_lock); 390419e3a9c9SNikolay Aleksandrov INIT_HLIST_HEAD(&br->mdb_list); 3905e12cec65SNikolay Aleksandrov INIT_HLIST_HEAD(&br->mcast_gc_list); 3906e12cec65SNikolay Aleksandrov INIT_WORK(&br->mcast_gc_work, br_multicast_gc_work); 3907eb1d1641SHerbert Xu } 3908eb1d1641SHerbert Xu 39094effd28cSLinus Lüssing static void br_ip4_multicast_join_snoopers(struct net_bridge *br) 39104effd28cSLinus Lüssing { 39114effd28cSLinus Lüssing struct in_device *in_dev = in_dev_get(br->dev); 39124effd28cSLinus Lüssing 39134effd28cSLinus Lüssing if (!in_dev) 39144effd28cSLinus Lüssing return; 39154effd28cSLinus Lüssing 39169fb20801SFlorian Fainelli __ip_mc_inc_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC); 39174effd28cSLinus Lüssing in_dev_put(in_dev); 39184effd28cSLinus Lüssing } 39194effd28cSLinus Lüssing 39204effd28cSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 39214effd28cSLinus Lüssing static void br_ip6_multicast_join_snoopers(struct net_bridge *br) 39224effd28cSLinus Lüssing { 39234effd28cSLinus Lüssing struct in6_addr addr; 39244effd28cSLinus Lüssing 39254effd28cSLinus Lüssing ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a)); 39264effd28cSLinus Lüssing ipv6_dev_mc_inc(br->dev, &addr); 39274effd28cSLinus Lüssing } 39284effd28cSLinus Lüssing #else 39294effd28cSLinus Lüssing static inline void br_ip6_multicast_join_snoopers(struct net_bridge *br) 39304effd28cSLinus Lüssing { 39314effd28cSLinus Lüssing } 39324effd28cSLinus Lüssing #endif 39334effd28cSLinus Lüssing 3934851d0a73SJoseph Huang void br_multicast_join_snoopers(struct net_bridge *br) 39354effd28cSLinus Lüssing { 39364effd28cSLinus Lüssing br_ip4_multicast_join_snoopers(br); 39374effd28cSLinus Lüssing br_ip6_multicast_join_snoopers(br); 39384effd28cSLinus Lüssing } 39394effd28cSLinus Lüssing 39404effd28cSLinus Lüssing static void br_ip4_multicast_leave_snoopers(struct net_bridge *br) 39414effd28cSLinus Lüssing { 39424effd28cSLinus Lüssing struct in_device *in_dev = in_dev_get(br->dev); 39434effd28cSLinus Lüssing 39444effd28cSLinus Lüssing if (WARN_ON(!in_dev)) 39454effd28cSLinus Lüssing return; 39464effd28cSLinus Lüssing 39479fb20801SFlorian Fainelli __ip_mc_dec_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC); 39484effd28cSLinus Lüssing in_dev_put(in_dev); 39494effd28cSLinus Lüssing } 39504effd28cSLinus Lüssing 39514effd28cSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 39524effd28cSLinus Lüssing static void br_ip6_multicast_leave_snoopers(struct net_bridge *br) 39534effd28cSLinus Lüssing { 39544effd28cSLinus Lüssing struct in6_addr addr; 39554effd28cSLinus Lüssing 39564effd28cSLinus Lüssing ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a)); 39574effd28cSLinus Lüssing ipv6_dev_mc_dec(br->dev, &addr); 39584effd28cSLinus Lüssing } 39594effd28cSLinus Lüssing #else 39604effd28cSLinus Lüssing static inline void br_ip6_multicast_leave_snoopers(struct net_bridge *br) 39614effd28cSLinus Lüssing { 39624effd28cSLinus Lüssing } 39634effd28cSLinus Lüssing #endif 39644effd28cSLinus Lüssing 3965851d0a73SJoseph Huang void br_multicast_leave_snoopers(struct net_bridge *br) 39664effd28cSLinus Lüssing { 39674effd28cSLinus Lüssing br_ip4_multicast_leave_snoopers(br); 39684effd28cSLinus Lüssing br_ip6_multicast_leave_snoopers(br); 39694effd28cSLinus Lüssing } 39704effd28cSLinus Lüssing 39717b54aaafSNikolay Aleksandrov static void __br_multicast_open_query(struct net_bridge *br, 397290010b36SLinus Lüssing struct bridge_mcast_own_query *query) 3973eb1d1641SHerbert Xu { 3974cc0fdd80SLinus Lüssing query->startup_sent = 0; 3975eb1d1641SHerbert Xu 397613cefad2SNikolay Aleksandrov if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) 3977eb1d1641SHerbert Xu return; 3978eb1d1641SHerbert Xu 3979cc0fdd80SLinus Lüssing mod_timer(&query->timer, jiffies); 3980cc0fdd80SLinus Lüssing } 3981cc0fdd80SLinus Lüssing 39827b54aaafSNikolay Aleksandrov static void __br_multicast_open(struct net_bridge_mcast *brmctx) 39837b54aaafSNikolay Aleksandrov { 39847b54aaafSNikolay Aleksandrov __br_multicast_open_query(brmctx->br, &brmctx->ip4_own_query); 39857b54aaafSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 39867b54aaafSNikolay Aleksandrov __br_multicast_open_query(brmctx->br, &brmctx->ip6_own_query); 39877b54aaafSNikolay Aleksandrov #endif 39887b54aaafSNikolay Aleksandrov } 39897b54aaafSNikolay Aleksandrov 3990cc0fdd80SLinus Lüssing void br_multicast_open(struct net_bridge *br) 3991cc0fdd80SLinus Lüssing { 3992f4b7002aSNikolay Aleksandrov ASSERT_RTNL(); 3993f4b7002aSNikolay Aleksandrov 3994f4b7002aSNikolay Aleksandrov if (br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED)) { 39957b54aaafSNikolay Aleksandrov struct net_bridge_vlan_group *vg; 39967b54aaafSNikolay Aleksandrov struct net_bridge_vlan *vlan; 39977b54aaafSNikolay Aleksandrov 39987b54aaafSNikolay Aleksandrov vg = br_vlan_group(br); 39997b54aaafSNikolay Aleksandrov if (vg) { 40007b54aaafSNikolay Aleksandrov list_for_each_entry(vlan, &vg->vlan_list, vlist) { 40017b54aaafSNikolay Aleksandrov struct net_bridge_mcast *brmctx; 40027b54aaafSNikolay Aleksandrov 40037b54aaafSNikolay Aleksandrov brmctx = &vlan->br_mcast_ctx; 40047b54aaafSNikolay Aleksandrov if (br_vlan_is_brentry(vlan) && 40057b54aaafSNikolay Aleksandrov !br_multicast_ctx_vlan_disabled(brmctx)) 40067b54aaafSNikolay Aleksandrov __br_multicast_open(&vlan->br_mcast_ctx); 40077b54aaafSNikolay Aleksandrov } 40087b54aaafSNikolay Aleksandrov } 4009cb486ce9SNikolay Aleksandrov } else { 40107b54aaafSNikolay Aleksandrov __br_multicast_open(&br->multicast_ctx); 4011eb1d1641SHerbert Xu } 4012cb486ce9SNikolay Aleksandrov } 4013eb1d1641SHerbert Xu 4014613d61dbSNikolay Aleksandrov static void __br_multicast_stop(struct net_bridge_mcast *brmctx) 4015613d61dbSNikolay Aleksandrov { 4016613d61dbSNikolay Aleksandrov del_timer_sync(&brmctx->ip4_mc_router_timer); 4017613d61dbSNikolay Aleksandrov del_timer_sync(&brmctx->ip4_other_query.timer); 4018613d61dbSNikolay Aleksandrov del_timer_sync(&brmctx->ip4_own_query.timer); 4019613d61dbSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 4020613d61dbSNikolay Aleksandrov del_timer_sync(&brmctx->ip6_mc_router_timer); 4021613d61dbSNikolay Aleksandrov del_timer_sync(&brmctx->ip6_other_query.timer); 4022613d61dbSNikolay Aleksandrov del_timer_sync(&brmctx->ip6_own_query.timer); 4023613d61dbSNikolay Aleksandrov #endif 4024613d61dbSNikolay Aleksandrov } 4025613d61dbSNikolay Aleksandrov 40267b54aaafSNikolay Aleksandrov void br_multicast_toggle_one_vlan(struct net_bridge_vlan *vlan, bool on) 40277b54aaafSNikolay Aleksandrov { 40287b54aaafSNikolay Aleksandrov struct net_bridge *br; 40297b54aaafSNikolay Aleksandrov 40307b54aaafSNikolay Aleksandrov /* it's okay to check for the flag without the multicast lock because it 40317b54aaafSNikolay Aleksandrov * can only change under RTNL -> multicast_lock, we need the latter to 40327b54aaafSNikolay Aleksandrov * sync with timers and packets 40337b54aaafSNikolay Aleksandrov */ 40347b54aaafSNikolay Aleksandrov if (on == !!(vlan->priv_flags & BR_VLFLAG_MCAST_ENABLED)) 40357b54aaafSNikolay Aleksandrov return; 40367b54aaafSNikolay Aleksandrov 40377b54aaafSNikolay Aleksandrov if (br_vlan_is_master(vlan)) { 40387b54aaafSNikolay Aleksandrov br = vlan->br; 40397b54aaafSNikolay Aleksandrov 40407b54aaafSNikolay Aleksandrov if (!br_vlan_is_brentry(vlan) || 40417b54aaafSNikolay Aleksandrov (on && 40427b54aaafSNikolay Aleksandrov br_multicast_ctx_vlan_global_disabled(&vlan->br_mcast_ctx))) 40437b54aaafSNikolay Aleksandrov return; 40447b54aaafSNikolay Aleksandrov 40457b54aaafSNikolay Aleksandrov spin_lock_bh(&br->multicast_lock); 40467b54aaafSNikolay Aleksandrov vlan->priv_flags ^= BR_VLFLAG_MCAST_ENABLED; 40477b54aaafSNikolay Aleksandrov spin_unlock_bh(&br->multicast_lock); 40487b54aaafSNikolay Aleksandrov 40497b54aaafSNikolay Aleksandrov if (on) 40507b54aaafSNikolay Aleksandrov __br_multicast_open(&vlan->br_mcast_ctx); 40517b54aaafSNikolay Aleksandrov else 40527b54aaafSNikolay Aleksandrov __br_multicast_stop(&vlan->br_mcast_ctx); 40537b54aaafSNikolay Aleksandrov } else { 40547b54aaafSNikolay Aleksandrov struct net_bridge_mcast *brmctx; 40557b54aaafSNikolay Aleksandrov 40567b54aaafSNikolay Aleksandrov brmctx = br_multicast_port_ctx_get_global(&vlan->port_mcast_ctx); 40577b54aaafSNikolay Aleksandrov if (on && br_multicast_ctx_vlan_global_disabled(brmctx)) 40587b54aaafSNikolay Aleksandrov return; 40597b54aaafSNikolay Aleksandrov 40607b54aaafSNikolay Aleksandrov br = vlan->port->br; 40617b54aaafSNikolay Aleksandrov spin_lock_bh(&br->multicast_lock); 40627b54aaafSNikolay Aleksandrov vlan->priv_flags ^= BR_VLFLAG_MCAST_ENABLED; 40637b54aaafSNikolay Aleksandrov if (on) 40647b54aaafSNikolay Aleksandrov __br_multicast_enable_port_ctx(&vlan->port_mcast_ctx); 40657b54aaafSNikolay Aleksandrov else 40667b54aaafSNikolay Aleksandrov __br_multicast_disable_port_ctx(&vlan->port_mcast_ctx); 40677b54aaafSNikolay Aleksandrov spin_unlock_bh(&br->multicast_lock); 40687b54aaafSNikolay Aleksandrov } 40697b54aaafSNikolay Aleksandrov } 40707b54aaafSNikolay Aleksandrov 4071f4b7002aSNikolay Aleksandrov void br_multicast_toggle_vlan(struct net_bridge_vlan *vlan, bool on) 4072f4b7002aSNikolay Aleksandrov { 4073f4b7002aSNikolay Aleksandrov struct net_bridge_port *p; 4074f4b7002aSNikolay Aleksandrov 4075f4b7002aSNikolay Aleksandrov if (WARN_ON_ONCE(!br_vlan_is_master(vlan))) 4076f4b7002aSNikolay Aleksandrov return; 4077f4b7002aSNikolay Aleksandrov 4078f4b7002aSNikolay Aleksandrov list_for_each_entry(p, &vlan->br->port_list, list) { 4079f4b7002aSNikolay Aleksandrov struct net_bridge_vlan *vport; 4080f4b7002aSNikolay Aleksandrov 4081f4b7002aSNikolay Aleksandrov vport = br_vlan_find(nbp_vlan_group(p), vlan->vid); 4082f4b7002aSNikolay Aleksandrov if (!vport) 4083f4b7002aSNikolay Aleksandrov continue; 4084f4b7002aSNikolay Aleksandrov br_multicast_toggle_one_vlan(vport, on); 4085f4b7002aSNikolay Aleksandrov } 4086f4b7002aSNikolay Aleksandrov } 4087f4b7002aSNikolay Aleksandrov 4088f4b7002aSNikolay Aleksandrov int br_multicast_toggle_vlan_snooping(struct net_bridge *br, bool on, 4089f4b7002aSNikolay Aleksandrov struct netlink_ext_ack *extack) 4090eb1d1641SHerbert Xu { 40917b54aaafSNikolay Aleksandrov struct net_bridge_vlan_group *vg; 40927b54aaafSNikolay Aleksandrov struct net_bridge_vlan *vlan; 4093f4b7002aSNikolay Aleksandrov struct net_bridge_port *p; 40947b54aaafSNikolay Aleksandrov 4095f4b7002aSNikolay Aleksandrov if (br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED) == on) 4096f4b7002aSNikolay Aleksandrov return 0; 4097f4b7002aSNikolay Aleksandrov 4098f4b7002aSNikolay Aleksandrov if (on && !br_opt_get(br, BROPT_VLAN_ENABLED)) { 4099f4b7002aSNikolay Aleksandrov NL_SET_ERR_MSG_MOD(extack, "Cannot enable multicast vlan snooping with vlan filtering disabled"); 4100f4b7002aSNikolay Aleksandrov return -EINVAL; 4101f4b7002aSNikolay Aleksandrov } 4102f4b7002aSNikolay Aleksandrov 4103f4b7002aSNikolay Aleksandrov vg = br_vlan_group(br); 4104f4b7002aSNikolay Aleksandrov if (!vg) 4105f4b7002aSNikolay Aleksandrov return 0; 4106f4b7002aSNikolay Aleksandrov 4107f4b7002aSNikolay Aleksandrov br_opt_toggle(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED, on); 4108f4b7002aSNikolay Aleksandrov 4109f4b7002aSNikolay Aleksandrov /* disable/enable non-vlan mcast contexts based on vlan snooping */ 4110f4b7002aSNikolay Aleksandrov if (on) 4111f4b7002aSNikolay Aleksandrov __br_multicast_stop(&br->multicast_ctx); 4112f4b7002aSNikolay Aleksandrov else 4113f4b7002aSNikolay Aleksandrov __br_multicast_open(&br->multicast_ctx); 4114f4b7002aSNikolay Aleksandrov list_for_each_entry(p, &br->port_list, list) { 4115f4b7002aSNikolay Aleksandrov if (on) 4116f4b7002aSNikolay Aleksandrov br_multicast_disable_port(p); 4117f4b7002aSNikolay Aleksandrov else 4118f4b7002aSNikolay Aleksandrov br_multicast_enable_port(p); 4119f4b7002aSNikolay Aleksandrov } 4120f4b7002aSNikolay Aleksandrov 4121f4b7002aSNikolay Aleksandrov list_for_each_entry(vlan, &vg->vlan_list, vlist) 4122f4b7002aSNikolay Aleksandrov br_multicast_toggle_vlan(vlan, on); 4123f4b7002aSNikolay Aleksandrov 4124f4b7002aSNikolay Aleksandrov return 0; 4125f4b7002aSNikolay Aleksandrov } 4126f4b7002aSNikolay Aleksandrov 41279dee572cSNikolay Aleksandrov bool br_multicast_toggle_global_vlan(struct net_bridge_vlan *vlan, bool on) 41289dee572cSNikolay Aleksandrov { 41299dee572cSNikolay Aleksandrov ASSERT_RTNL(); 41309dee572cSNikolay Aleksandrov 41319dee572cSNikolay Aleksandrov /* BR_VLFLAG_GLOBAL_MCAST_ENABLED relies on eventual consistency and 41329dee572cSNikolay Aleksandrov * requires only RTNL to change 41339dee572cSNikolay Aleksandrov */ 41349dee572cSNikolay Aleksandrov if (on == !!(vlan->priv_flags & BR_VLFLAG_GLOBAL_MCAST_ENABLED)) 41359dee572cSNikolay Aleksandrov return false; 41369dee572cSNikolay Aleksandrov 41379dee572cSNikolay Aleksandrov vlan->priv_flags ^= BR_VLFLAG_GLOBAL_MCAST_ENABLED; 41389dee572cSNikolay Aleksandrov br_multicast_toggle_vlan(vlan, on); 41399dee572cSNikolay Aleksandrov 41409dee572cSNikolay Aleksandrov return true; 41419dee572cSNikolay Aleksandrov } 41429dee572cSNikolay Aleksandrov 4143f4b7002aSNikolay Aleksandrov void br_multicast_stop(struct net_bridge *br) 4144f4b7002aSNikolay Aleksandrov { 41457b54aaafSNikolay Aleksandrov ASSERT_RTNL(); 41467b54aaafSNikolay Aleksandrov 4147f4b7002aSNikolay Aleksandrov if (br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED)) { 4148f4b7002aSNikolay Aleksandrov struct net_bridge_vlan_group *vg; 4149f4b7002aSNikolay Aleksandrov struct net_bridge_vlan *vlan; 4150f4b7002aSNikolay Aleksandrov 41517b54aaafSNikolay Aleksandrov vg = br_vlan_group(br); 41527b54aaafSNikolay Aleksandrov if (vg) { 41537b54aaafSNikolay Aleksandrov list_for_each_entry(vlan, &vg->vlan_list, vlist) { 41547b54aaafSNikolay Aleksandrov struct net_bridge_mcast *brmctx; 41557b54aaafSNikolay Aleksandrov 41567b54aaafSNikolay Aleksandrov brmctx = &vlan->br_mcast_ctx; 41577b54aaafSNikolay Aleksandrov if (br_vlan_is_brentry(vlan) && 41587b54aaafSNikolay Aleksandrov !br_multicast_ctx_vlan_disabled(brmctx)) 41597b54aaafSNikolay Aleksandrov __br_multicast_stop(&vlan->br_mcast_ctx); 41607b54aaafSNikolay Aleksandrov } 41617b54aaafSNikolay Aleksandrov } 4162cb486ce9SNikolay Aleksandrov } else { 4163613d61dbSNikolay Aleksandrov __br_multicast_stop(&br->multicast_ctx); 4164e10177abSSatish Ashok } 4165cb486ce9SNikolay Aleksandrov } 4166e10177abSSatish Ashok 4167e10177abSSatish Ashok void br_multicast_dev_del(struct net_bridge *br) 4168e10177abSSatish Ashok { 4169e10177abSSatish Ashok struct net_bridge_mdb_entry *mp; 41708b671779SNikolay Aleksandrov HLIST_HEAD(deleted_head); 417119e3a9c9SNikolay Aleksandrov struct hlist_node *tmp; 4172eb1d1641SHerbert Xu 4173eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 4174e12cec65SNikolay Aleksandrov hlist_for_each_entry_safe(mp, tmp, &br->mdb_list, mdb_node) 4175e12cec65SNikolay Aleksandrov br_multicast_del_mdb_entry(mp); 4176e12cec65SNikolay Aleksandrov hlist_move_list(&br->mcast_gc_list, &deleted_head); 4177eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 417819e3a9c9SNikolay Aleksandrov 4179613d61dbSNikolay Aleksandrov br_multicast_ctx_deinit(&br->multicast_ctx); 4180e12cec65SNikolay Aleksandrov br_multicast_gc(&deleted_head); 4181e12cec65SNikolay Aleksandrov cancel_work_sync(&br->mcast_gc_work); 41828b671779SNikolay Aleksandrov 41834329596cSNikolay Aleksandrov rcu_barrier(); 4184eb1d1641SHerbert Xu } 41850909e117SHerbert Xu 4186a97df080SNikolay Aleksandrov int br_multicast_set_router(struct net_bridge_mcast *brmctx, unsigned long val) 41870909e117SHerbert Xu { 41886ae4ae8eSLinus Lüssing int err = -EINVAL; 41890909e117SHerbert Xu 4190a97df080SNikolay Aleksandrov spin_lock_bh(&brmctx->br->multicast_lock); 41910909e117SHerbert Xu 41920909e117SHerbert Xu switch (val) { 41937f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_DISABLED: 41947f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_PERM: 4195a97df080SNikolay Aleksandrov br_mc_router_state_change(brmctx->br, val == MDB_RTR_TYPE_PERM); 4196d3d065c0SNikolay Aleksandrov del_timer(&brmctx->ip4_mc_router_timer); 4197a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 4198d3d065c0SNikolay Aleksandrov del_timer(&brmctx->ip6_mc_router_timer); 4199a3c02e76SLinus Lüssing #endif 4200d3d065c0SNikolay Aleksandrov brmctx->multicast_router = val; 420177041420SYotam Gigi err = 0; 420277041420SYotam Gigi break; 42037f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_TEMP_QUERY: 4204d3d065c0SNikolay Aleksandrov if (brmctx->multicast_router != MDB_RTR_TYPE_TEMP_QUERY) 4205a97df080SNikolay Aleksandrov br_mc_router_state_change(brmctx->br, false); 4206d3d065c0SNikolay Aleksandrov brmctx->multicast_router = val; 42070909e117SHerbert Xu err = 0; 42080909e117SHerbert Xu break; 42090909e117SHerbert Xu } 42100909e117SHerbert Xu 4211a97df080SNikolay Aleksandrov spin_unlock_bh(&brmctx->br->multicast_lock); 42120909e117SHerbert Xu 42130909e117SHerbert Xu return err; 42140909e117SHerbert Xu } 42150909e117SHerbert Xu 4216ed2d3597SLinus Lüssing static void 4217adc47037SNikolay Aleksandrov br_multicast_rport_del_notify(struct net_bridge_mcast_port *pmctx, bool deleted) 42187f0aec7aSNikolay Aleksandrov { 4219ed2d3597SLinus Lüssing if (!deleted) 42207f0aec7aSNikolay Aleksandrov return; 4221ed2d3597SLinus Lüssing 4222a3c02e76SLinus Lüssing /* For backwards compatibility for now, only notify if there is 4223a3c02e76SLinus Lüssing * no multicast router anymore for both IPv4 and IPv6. 4224a3c02e76SLinus Lüssing */ 4225adc47037SNikolay Aleksandrov if (!hlist_unhashed(&pmctx->ip4_rlist)) 4226a3c02e76SLinus Lüssing return; 4227a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 4228adc47037SNikolay Aleksandrov if (!hlist_unhashed(&pmctx->ip6_rlist)) 4229a3c02e76SLinus Lüssing return; 4230a3c02e76SLinus Lüssing #endif 4231a3c02e76SLinus Lüssing 42321e9ca456SNikolay Aleksandrov br_rtr_notify(pmctx->port->br->dev, pmctx, RTM_DELMDB); 4233adc47037SNikolay Aleksandrov br_port_mc_router_state_change(pmctx->port, false); 4234f12e7d95SNogah Frankel 4235f12e7d95SNogah Frankel /* don't allow timer refresh */ 4236adc47037SNikolay Aleksandrov if (pmctx->multicast_router == MDB_RTR_TYPE_TEMP) 4237adc47037SNikolay Aleksandrov pmctx->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 42387f0aec7aSNikolay Aleksandrov } 42397f0aec7aSNikolay Aleksandrov 42400909e117SHerbert Xu int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val) 42410909e117SHerbert Xu { 4242d3d065c0SNikolay Aleksandrov struct net_bridge_mcast *brmctx = &p->br->multicast_ctx; 4243adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx = &p->multicast_ctx; 4244a55d8246SNikolay Aleksandrov unsigned long now = jiffies; 42456ae4ae8eSLinus Lüssing int err = -EINVAL; 4246ed2d3597SLinus Lüssing bool del = false; 42470909e117SHerbert Xu 4248d3d065c0SNikolay Aleksandrov spin_lock(&p->br->multicast_lock); 4249adc47037SNikolay Aleksandrov if (pmctx->multicast_router == val) { 4250a55d8246SNikolay Aleksandrov /* Refresh the temp router port timer */ 4251adc47037SNikolay Aleksandrov if (pmctx->multicast_router == MDB_RTR_TYPE_TEMP) { 4252adc47037SNikolay Aleksandrov mod_timer(&pmctx->ip4_mc_router_timer, 4253d3d065c0SNikolay Aleksandrov now + brmctx->multicast_querier_interval); 4254a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 4255adc47037SNikolay Aleksandrov mod_timer(&pmctx->ip6_mc_router_timer, 4256d3d065c0SNikolay Aleksandrov now + brmctx->multicast_querier_interval); 4257a3c02e76SLinus Lüssing #endif 4258a3c02e76SLinus Lüssing } 42594950cfd1SNikolay Aleksandrov err = 0; 42604950cfd1SNikolay Aleksandrov goto unlock; 42614950cfd1SNikolay Aleksandrov } 42620909e117SHerbert Xu switch (val) { 42637f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_DISABLED: 4264adc47037SNikolay Aleksandrov pmctx->multicast_router = MDB_RTR_TYPE_DISABLED; 4265adc47037SNikolay Aleksandrov del |= br_ip4_multicast_rport_del(pmctx); 4266adc47037SNikolay Aleksandrov del_timer(&pmctx->ip4_mc_router_timer); 4267adc47037SNikolay Aleksandrov del |= br_ip6_multicast_rport_del(pmctx); 4268a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 4269adc47037SNikolay Aleksandrov del_timer(&pmctx->ip6_mc_router_timer); 4270a3c02e76SLinus Lüssing #endif 4271adc47037SNikolay Aleksandrov br_multicast_rport_del_notify(pmctx, del); 42720909e117SHerbert Xu break; 42737f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_TEMP_QUERY: 4274adc47037SNikolay Aleksandrov pmctx->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 4275adc47037SNikolay Aleksandrov del |= br_ip4_multicast_rport_del(pmctx); 4276adc47037SNikolay Aleksandrov del |= br_ip6_multicast_rport_del(pmctx); 4277adc47037SNikolay Aleksandrov br_multicast_rport_del_notify(pmctx, del); 42787f0aec7aSNikolay Aleksandrov break; 42797f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_PERM: 4280adc47037SNikolay Aleksandrov pmctx->multicast_router = MDB_RTR_TYPE_PERM; 4281adc47037SNikolay Aleksandrov del_timer(&pmctx->ip4_mc_router_timer); 4282adc47037SNikolay Aleksandrov br_ip4_multicast_add_router(brmctx, pmctx); 4283a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 4284adc47037SNikolay Aleksandrov del_timer(&pmctx->ip6_mc_router_timer); 4285a3c02e76SLinus Lüssing #endif 4286adc47037SNikolay Aleksandrov br_ip6_multicast_add_router(brmctx, pmctx); 42870909e117SHerbert Xu break; 4288a55d8246SNikolay Aleksandrov case MDB_RTR_TYPE_TEMP: 4289adc47037SNikolay Aleksandrov pmctx->multicast_router = MDB_RTR_TYPE_TEMP; 4290adc47037SNikolay Aleksandrov br_ip4_multicast_mark_router(brmctx, pmctx); 4291adc47037SNikolay Aleksandrov br_ip6_multicast_mark_router(brmctx, pmctx); 4292a55d8246SNikolay Aleksandrov break; 42937f0aec7aSNikolay Aleksandrov default: 42947f0aec7aSNikolay Aleksandrov goto unlock; 42950909e117SHerbert Xu } 42967f0aec7aSNikolay Aleksandrov err = 0; 42977f0aec7aSNikolay Aleksandrov unlock: 4298d3d065c0SNikolay Aleksandrov spin_unlock(&p->br->multicast_lock); 42990909e117SHerbert Xu 43000909e117SHerbert Xu return err; 43010909e117SHerbert Xu } 4302561f1103SHerbert Xu 4303adc47037SNikolay Aleksandrov static void br_multicast_start_querier(struct net_bridge_mcast *brmctx, 430490010b36SLinus Lüssing struct bridge_mcast_own_query *query) 4305561f1103SHerbert Xu { 4306561f1103SHerbert Xu struct net_bridge_port *port; 430774857216SHerbert Xu 4308cb486ce9SNikolay Aleksandrov if (!br_multicast_ctx_matches_vlan_snooping(brmctx)) 4309cb486ce9SNikolay Aleksandrov return; 4310cb486ce9SNikolay Aleksandrov 43117b54aaafSNikolay Aleksandrov __br_multicast_open_query(brmctx->br, query); 431274857216SHerbert Xu 4313c5b493ceSNikolay Aleksandrov rcu_read_lock(); 4314adc47037SNikolay Aleksandrov list_for_each_entry_rcu(port, &brmctx->br->port_list, list) { 4315615cc23eSNikolay Aleksandrov struct bridge_mcast_own_query *ip4_own_query; 4316615cc23eSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 4317615cc23eSNikolay Aleksandrov struct bridge_mcast_own_query *ip6_own_query; 4318615cc23eSNikolay Aleksandrov #endif 4319615cc23eSNikolay Aleksandrov 4320615cc23eSNikolay Aleksandrov if (br_multicast_port_ctx_state_stopped(&port->multicast_ctx)) 432174857216SHerbert Xu continue; 432274857216SHerbert Xu 4323615cc23eSNikolay Aleksandrov if (br_multicast_ctx_is_vlan(brmctx)) { 4324615cc23eSNikolay Aleksandrov struct net_bridge_vlan *vlan; 4325615cc23eSNikolay Aleksandrov 4326615cc23eSNikolay Aleksandrov vlan = br_vlan_find(nbp_vlan_group(port), brmctx->vlan->vid); 4327615cc23eSNikolay Aleksandrov if (!vlan || 4328615cc23eSNikolay Aleksandrov br_multicast_port_ctx_state_stopped(&vlan->port_mcast_ctx)) 4329615cc23eSNikolay Aleksandrov continue; 4330615cc23eSNikolay Aleksandrov 4331615cc23eSNikolay Aleksandrov ip4_own_query = &vlan->port_mcast_ctx.ip4_own_query; 4332615cc23eSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 4333615cc23eSNikolay Aleksandrov ip6_own_query = &vlan->port_mcast_ctx.ip6_own_query; 4334615cc23eSNikolay Aleksandrov #endif 4335615cc23eSNikolay Aleksandrov } else { 4336615cc23eSNikolay Aleksandrov ip4_own_query = &port->multicast_ctx.ip4_own_query; 4337615cc23eSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 4338615cc23eSNikolay Aleksandrov ip6_own_query = &port->multicast_ctx.ip6_own_query; 4339615cc23eSNikolay Aleksandrov #endif 4340615cc23eSNikolay Aleksandrov } 4341615cc23eSNikolay Aleksandrov 4342adc47037SNikolay Aleksandrov if (query == &brmctx->ip4_own_query) 4343615cc23eSNikolay Aleksandrov br_multicast_enable(ip4_own_query); 4344cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 4345cc0fdd80SLinus Lüssing else 4346615cc23eSNikolay Aleksandrov br_multicast_enable(ip6_own_query); 4347cc0fdd80SLinus Lüssing #endif 434874857216SHerbert Xu } 4349c5b493ceSNikolay Aleksandrov rcu_read_unlock(); 435074857216SHerbert Xu } 435174857216SHerbert Xu 4352ae1ea84bSFlorian Fainelli int br_multicast_toggle(struct net_bridge *br, unsigned long val, 4353ae1ea84bSFlorian Fainelli struct netlink_ext_ack *extack) 435474857216SHerbert Xu { 43557cb3f921SNikolay Aleksandrov struct net_bridge_port *port; 4356851d0a73SJoseph Huang bool change_snoopers = false; 4357ae1ea84bSFlorian Fainelli int err = 0; 4358561f1103SHerbert Xu 4359ef5e0d82SAndrey Vagin spin_lock_bh(&br->multicast_lock); 436013cefad2SNikolay Aleksandrov if (!!br_opt_get(br, BROPT_MULTICAST_ENABLED) == !!val) 4361561f1103SHerbert Xu goto unlock; 4362561f1103SHerbert Xu 4363ae1ea84bSFlorian Fainelli err = br_mc_disabled_update(br->dev, val, extack); 4364ae1ea84bSFlorian Fainelli if (err == -EOPNOTSUPP) 4365ae1ea84bSFlorian Fainelli err = 0; 4366ae1ea84bSFlorian Fainelli if (err) 4367ae1ea84bSFlorian Fainelli goto unlock; 4368ae1ea84bSFlorian Fainelli 436913cefad2SNikolay Aleksandrov br_opt_toggle(br, BROPT_MULTICAST_ENABLED, !!val); 43704effd28cSLinus Lüssing if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) { 4371851d0a73SJoseph Huang change_snoopers = true; 4372561f1103SHerbert Xu goto unlock; 43734effd28cSLinus Lüssing } 4374561f1103SHerbert Xu 43753a7fda06SHerbert Xu if (!netif_running(br->dev)) 43763a7fda06SHerbert Xu goto unlock; 43773a7fda06SHerbert Xu 43787cb3f921SNikolay Aleksandrov br_multicast_open(br); 43797cb3f921SNikolay Aleksandrov list_for_each_entry(port, &br->port_list, list) 4380adc47037SNikolay Aleksandrov __br_multicast_enable_port_ctx(&port->multicast_ctx); 4381561f1103SHerbert Xu 4382851d0a73SJoseph Huang change_snoopers = true; 4383851d0a73SJoseph Huang 4384561f1103SHerbert Xu unlock: 4385ef5e0d82SAndrey Vagin spin_unlock_bh(&br->multicast_lock); 4386561f1103SHerbert Xu 4387851d0a73SJoseph Huang /* br_multicast_join_snoopers has the potential to cause 4388851d0a73SJoseph Huang * an MLD Report/Leave to be delivered to br_multicast_rcv, 4389851d0a73SJoseph Huang * which would in turn call br_multicast_add_group, which would 4390851d0a73SJoseph Huang * attempt to acquire multicast_lock. This function should be 4391851d0a73SJoseph Huang * called after the lock has been released to avoid deadlocks on 4392851d0a73SJoseph Huang * multicast_lock. 4393851d0a73SJoseph Huang * 4394851d0a73SJoseph Huang * br_multicast_leave_snoopers does not have the problem since 4395851d0a73SJoseph Huang * br_multicast_rcv first checks BROPT_MULTICAST_ENABLED, and 4396851d0a73SJoseph Huang * returns without calling br_multicast_ipv4/6_rcv if it's not 4397851d0a73SJoseph Huang * enabled. Moved both functions out just for symmetry. 4398851d0a73SJoseph Huang */ 4399851d0a73SJoseph Huang if (change_snoopers) { 4400851d0a73SJoseph Huang if (br_opt_get(br, BROPT_MULTICAST_ENABLED)) 4401851d0a73SJoseph Huang br_multicast_join_snoopers(br); 4402851d0a73SJoseph Huang else 4403851d0a73SJoseph Huang br_multicast_leave_snoopers(br); 4404851d0a73SJoseph Huang } 4405851d0a73SJoseph Huang 4406ae1ea84bSFlorian Fainelli return err; 4407561f1103SHerbert Xu } 4408b195167fSHerbert Xu 44099341b988SIdo Schimmel bool br_multicast_enabled(const struct net_device *dev) 44109341b988SIdo Schimmel { 44119341b988SIdo Schimmel struct net_bridge *br = netdev_priv(dev); 44129341b988SIdo Schimmel 441313cefad2SNikolay Aleksandrov return !!br_opt_get(br, BROPT_MULTICAST_ENABLED); 44149341b988SIdo Schimmel } 44159341b988SIdo Schimmel EXPORT_SYMBOL_GPL(br_multicast_enabled); 44169341b988SIdo Schimmel 44170912bda4SYotam Gigi bool br_multicast_router(const struct net_device *dev) 44180912bda4SYotam Gigi { 44190912bda4SYotam Gigi struct net_bridge *br = netdev_priv(dev); 44200912bda4SYotam Gigi bool is_router; 44210912bda4SYotam Gigi 44220912bda4SYotam Gigi spin_lock_bh(&br->multicast_lock); 4423adc47037SNikolay Aleksandrov is_router = br_multicast_is_router(&br->multicast_ctx, NULL); 44240912bda4SYotam Gigi spin_unlock_bh(&br->multicast_lock); 44250912bda4SYotam Gigi return is_router; 44260912bda4SYotam Gigi } 44270912bda4SYotam Gigi EXPORT_SYMBOL_GPL(br_multicast_router); 44280912bda4SYotam Gigi 442962938182SNikolay Aleksandrov int br_multicast_set_querier(struct net_bridge_mcast *brmctx, unsigned long val) 4430c5c23260SHerbert Xu { 4431b00589afSLinus Lüssing unsigned long max_delay; 4432b00589afSLinus Lüssing 4433c5c23260SHerbert Xu val = !!val; 4434c5c23260SHerbert Xu 443562938182SNikolay Aleksandrov spin_lock_bh(&brmctx->br->multicast_lock); 44364d5b4e84SNikolay Aleksandrov if (brmctx->multicast_querier == val) 4437c5c23260SHerbert Xu goto unlock; 4438c5c23260SHerbert Xu 44394d5b4e84SNikolay Aleksandrov WRITE_ONCE(brmctx->multicast_querier, val); 4440b00589afSLinus Lüssing if (!val) 4441b00589afSLinus Lüssing goto unlock; 4442b00589afSLinus Lüssing 4443d3d065c0SNikolay Aleksandrov max_delay = brmctx->multicast_query_response_interval; 4444b00589afSLinus Lüssing 4445d3d065c0SNikolay Aleksandrov if (!timer_pending(&brmctx->ip4_other_query.timer)) 4446d3d065c0SNikolay Aleksandrov brmctx->ip4_other_query.delay_time = jiffies + max_delay; 4447cc0fdd80SLinus Lüssing 4448adc47037SNikolay Aleksandrov br_multicast_start_querier(brmctx, &brmctx->ip4_own_query); 4449cc0fdd80SLinus Lüssing 4450cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 4451d3d065c0SNikolay Aleksandrov if (!timer_pending(&brmctx->ip6_other_query.timer)) 4452d3d065c0SNikolay Aleksandrov brmctx->ip6_other_query.delay_time = jiffies + max_delay; 4453cc0fdd80SLinus Lüssing 4454adc47037SNikolay Aleksandrov br_multicast_start_querier(brmctx, &brmctx->ip6_own_query); 4455cc0fdd80SLinus Lüssing #endif 4456c5c23260SHerbert Xu 4457c5c23260SHerbert Xu unlock: 445862938182SNikolay Aleksandrov spin_unlock_bh(&brmctx->br->multicast_lock); 4459c5c23260SHerbert Xu 4460c5c23260SHerbert Xu return 0; 4461c5c23260SHerbert Xu } 4462c5c23260SHerbert Xu 4463df271cd6SNikolay Aleksandrov int br_multicast_set_igmp_version(struct net_bridge_mcast *brmctx, 4464df271cd6SNikolay Aleksandrov unsigned long val) 44655e923585SNikolay Aleksandrov { 44665e923585SNikolay Aleksandrov /* Currently we support only version 2 and 3 */ 44675e923585SNikolay Aleksandrov switch (val) { 44685e923585SNikolay Aleksandrov case 2: 44695e923585SNikolay Aleksandrov case 3: 44705e923585SNikolay Aleksandrov break; 44715e923585SNikolay Aleksandrov default: 44725e923585SNikolay Aleksandrov return -EINVAL; 44735e923585SNikolay Aleksandrov } 44745e923585SNikolay Aleksandrov 4475df271cd6SNikolay Aleksandrov spin_lock_bh(&brmctx->br->multicast_lock); 4476df271cd6SNikolay Aleksandrov brmctx->multicast_igmp_version = val; 4477df271cd6SNikolay Aleksandrov spin_unlock_bh(&brmctx->br->multicast_lock); 44785e923585SNikolay Aleksandrov 44795e923585SNikolay Aleksandrov return 0; 44805e923585SNikolay Aleksandrov } 44815e923585SNikolay Aleksandrov 4482aa2ae3e7SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 4483df271cd6SNikolay Aleksandrov int br_multicast_set_mld_version(struct net_bridge_mcast *brmctx, 4484df271cd6SNikolay Aleksandrov unsigned long val) 4485aa2ae3e7SNikolay Aleksandrov { 4486aa2ae3e7SNikolay Aleksandrov /* Currently we support version 1 and 2 */ 4487aa2ae3e7SNikolay Aleksandrov switch (val) { 4488aa2ae3e7SNikolay Aleksandrov case 1: 4489aa2ae3e7SNikolay Aleksandrov case 2: 4490aa2ae3e7SNikolay Aleksandrov break; 4491aa2ae3e7SNikolay Aleksandrov default: 4492aa2ae3e7SNikolay Aleksandrov return -EINVAL; 4493aa2ae3e7SNikolay Aleksandrov } 4494aa2ae3e7SNikolay Aleksandrov 4495df271cd6SNikolay Aleksandrov spin_lock_bh(&brmctx->br->multicast_lock); 4496df271cd6SNikolay Aleksandrov brmctx->multicast_mld_version = val; 4497df271cd6SNikolay Aleksandrov spin_unlock_bh(&brmctx->br->multicast_lock); 4498aa2ae3e7SNikolay Aleksandrov 4499aa2ae3e7SNikolay Aleksandrov return 0; 4500aa2ae3e7SNikolay Aleksandrov } 4501aa2ae3e7SNikolay Aleksandrov #endif 4502aa2ae3e7SNikolay Aleksandrov 450307f8ac4aSLinus Lüssing /** 450407f8ac4aSLinus Lüssing * br_multicast_list_adjacent - Returns snooped multicast addresses 450507f8ac4aSLinus Lüssing * @dev: The bridge port adjacent to which to retrieve addresses 450607f8ac4aSLinus Lüssing * @br_ip_list: The list to store found, snooped multicast IP addresses in 450707f8ac4aSLinus Lüssing * 450807f8ac4aSLinus Lüssing * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast 450907f8ac4aSLinus Lüssing * snooping feature on all bridge ports of dev's bridge device, excluding 451007f8ac4aSLinus Lüssing * the addresses from dev itself. 451107f8ac4aSLinus Lüssing * 451207f8ac4aSLinus Lüssing * Returns the number of items added to br_ip_list. 451307f8ac4aSLinus Lüssing * 451407f8ac4aSLinus Lüssing * Notes: 451507f8ac4aSLinus Lüssing * - br_ip_list needs to be initialized by caller 451607f8ac4aSLinus Lüssing * - br_ip_list might contain duplicates in the end 451707f8ac4aSLinus Lüssing * (needs to be taken care of by caller) 451807f8ac4aSLinus Lüssing * - br_ip_list needs to be freed by caller 451907f8ac4aSLinus Lüssing */ 452007f8ac4aSLinus Lüssing int br_multicast_list_adjacent(struct net_device *dev, 452107f8ac4aSLinus Lüssing struct list_head *br_ip_list) 452207f8ac4aSLinus Lüssing { 452307f8ac4aSLinus Lüssing struct net_bridge *br; 452407f8ac4aSLinus Lüssing struct net_bridge_port *port; 452507f8ac4aSLinus Lüssing struct net_bridge_port_group *group; 452607f8ac4aSLinus Lüssing struct br_ip_list *entry; 452707f8ac4aSLinus Lüssing int count = 0; 452807f8ac4aSLinus Lüssing 452907f8ac4aSLinus Lüssing rcu_read_lock(); 453035f861e3SJulian Wiedmann if (!br_ip_list || !netif_is_bridge_port(dev)) 453107f8ac4aSLinus Lüssing goto unlock; 453207f8ac4aSLinus Lüssing 453307f8ac4aSLinus Lüssing port = br_port_get_rcu(dev); 453407f8ac4aSLinus Lüssing if (!port || !port->br) 453507f8ac4aSLinus Lüssing goto unlock; 453607f8ac4aSLinus Lüssing 453707f8ac4aSLinus Lüssing br = port->br; 453807f8ac4aSLinus Lüssing 453907f8ac4aSLinus Lüssing list_for_each_entry_rcu(port, &br->port_list, list) { 454007f8ac4aSLinus Lüssing if (!port->dev || port->dev == dev) 454107f8ac4aSLinus Lüssing continue; 454207f8ac4aSLinus Lüssing 454307f8ac4aSLinus Lüssing hlist_for_each_entry_rcu(group, &port->mglist, mglist) { 454407f8ac4aSLinus Lüssing entry = kmalloc(sizeof(*entry), GFP_ATOMIC); 454507f8ac4aSLinus Lüssing if (!entry) 454607f8ac4aSLinus Lüssing goto unlock; 454707f8ac4aSLinus Lüssing 4548085b53c8SNikolay Aleksandrov entry->addr = group->key.addr; 454907f8ac4aSLinus Lüssing list_add(&entry->list, br_ip_list); 455007f8ac4aSLinus Lüssing count++; 455107f8ac4aSLinus Lüssing } 455207f8ac4aSLinus Lüssing } 455307f8ac4aSLinus Lüssing 455407f8ac4aSLinus Lüssing unlock: 455507f8ac4aSLinus Lüssing rcu_read_unlock(); 455607f8ac4aSLinus Lüssing return count; 455707f8ac4aSLinus Lüssing } 455807f8ac4aSLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_list_adjacent); 45592cd41431SLinus Lüssing 45602cd41431SLinus Lüssing /** 4561c34963e2SLinus Lüssing * br_multicast_has_querier_anywhere - Checks for a querier on a bridge 4562c34963e2SLinus Lüssing * @dev: The bridge port providing the bridge on which to check for a querier 4563c34963e2SLinus Lüssing * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 4564c34963e2SLinus Lüssing * 4565c34963e2SLinus Lüssing * Checks whether the given interface has a bridge on top and if so returns 4566c34963e2SLinus Lüssing * true if a valid querier exists anywhere on the bridged link layer. 4567c34963e2SLinus Lüssing * Otherwise returns false. 4568c34963e2SLinus Lüssing */ 4569c34963e2SLinus Lüssing bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto) 4570c34963e2SLinus Lüssing { 4571c34963e2SLinus Lüssing struct net_bridge *br; 4572c34963e2SLinus Lüssing struct net_bridge_port *port; 4573c34963e2SLinus Lüssing struct ethhdr eth; 4574c34963e2SLinus Lüssing bool ret = false; 4575c34963e2SLinus Lüssing 4576c34963e2SLinus Lüssing rcu_read_lock(); 457735f861e3SJulian Wiedmann if (!netif_is_bridge_port(dev)) 4578c34963e2SLinus Lüssing goto unlock; 4579c34963e2SLinus Lüssing 4580c34963e2SLinus Lüssing port = br_port_get_rcu(dev); 4581c34963e2SLinus Lüssing if (!port || !port->br) 4582c34963e2SLinus Lüssing goto unlock; 4583c34963e2SLinus Lüssing 4584c34963e2SLinus Lüssing br = port->br; 4585c34963e2SLinus Lüssing 4586c34963e2SLinus Lüssing memset(ð, 0, sizeof(eth)); 4587c34963e2SLinus Lüssing eth.h_proto = htons(proto); 4588c34963e2SLinus Lüssing 4589adc47037SNikolay Aleksandrov ret = br_multicast_querier_exists(&br->multicast_ctx, ð, NULL); 4590c34963e2SLinus Lüssing 4591c34963e2SLinus Lüssing unlock: 4592c34963e2SLinus Lüssing rcu_read_unlock(); 4593c34963e2SLinus Lüssing return ret; 4594c34963e2SLinus Lüssing } 4595c34963e2SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere); 4596c34963e2SLinus Lüssing 4597c34963e2SLinus Lüssing /** 45982cd41431SLinus Lüssing * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port 45992cd41431SLinus Lüssing * @dev: The bridge port adjacent to which to check for a querier 46002cd41431SLinus Lüssing * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 46012cd41431SLinus Lüssing * 46022cd41431SLinus Lüssing * Checks whether the given interface has a bridge on top and if so returns 46032cd41431SLinus Lüssing * true if a selected querier is behind one of the other ports of this 46042cd41431SLinus Lüssing * bridge. Otherwise returns false. 46052cd41431SLinus Lüssing */ 46062cd41431SLinus Lüssing bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto) 46072cd41431SLinus Lüssing { 4608d3d065c0SNikolay Aleksandrov struct net_bridge_mcast *brmctx; 46092cd41431SLinus Lüssing struct net_bridge *br; 46102cd41431SLinus Lüssing struct net_bridge_port *port; 46112cd41431SLinus Lüssing bool ret = false; 4612bb18ef8eSNikolay Aleksandrov int port_ifidx; 46132cd41431SLinus Lüssing 46142cd41431SLinus Lüssing rcu_read_lock(); 461535f861e3SJulian Wiedmann if (!netif_is_bridge_port(dev)) 46162cd41431SLinus Lüssing goto unlock; 46172cd41431SLinus Lüssing 46182cd41431SLinus Lüssing port = br_port_get_rcu(dev); 46192cd41431SLinus Lüssing if (!port || !port->br) 46202cd41431SLinus Lüssing goto unlock; 46212cd41431SLinus Lüssing 46222cd41431SLinus Lüssing br = port->br; 4623d3d065c0SNikolay Aleksandrov brmctx = &br->multicast_ctx; 46242cd41431SLinus Lüssing 46252cd41431SLinus Lüssing switch (proto) { 46262cd41431SLinus Lüssing case ETH_P_IP: 4627bb18ef8eSNikolay Aleksandrov port_ifidx = brmctx->ip4_querier.port_ifidx; 4628d3d065c0SNikolay Aleksandrov if (!timer_pending(&brmctx->ip4_other_query.timer) || 4629bb18ef8eSNikolay Aleksandrov port_ifidx == port->dev->ifindex) 46302cd41431SLinus Lüssing goto unlock; 46312cd41431SLinus Lüssing break; 46323993c4e1SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 46332cd41431SLinus Lüssing case ETH_P_IPV6: 4634bb18ef8eSNikolay Aleksandrov port_ifidx = brmctx->ip6_querier.port_ifidx; 4635d3d065c0SNikolay Aleksandrov if (!timer_pending(&brmctx->ip6_other_query.timer) || 4636bb18ef8eSNikolay Aleksandrov port_ifidx == port->dev->ifindex) 46372cd41431SLinus Lüssing goto unlock; 46382cd41431SLinus Lüssing break; 46393993c4e1SLinus Lüssing #endif 46402cd41431SLinus Lüssing default: 46412cd41431SLinus Lüssing goto unlock; 46422cd41431SLinus Lüssing } 46432cd41431SLinus Lüssing 46442cd41431SLinus Lüssing ret = true; 46452cd41431SLinus Lüssing unlock: 46462cd41431SLinus Lüssing rcu_read_unlock(); 46472cd41431SLinus Lüssing return ret; 46482cd41431SLinus Lüssing } 46492cd41431SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent); 46501080ab95SNikolay Aleksandrov 46513b85f9baSLinus Lüssing /** 46523b85f9baSLinus Lüssing * br_multicast_has_router_adjacent - Checks for a router behind a bridge port 46533b85f9baSLinus Lüssing * @dev: The bridge port adjacent to which to check for a multicast router 46543b85f9baSLinus Lüssing * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 46553b85f9baSLinus Lüssing * 46563b85f9baSLinus Lüssing * Checks whether the given interface has a bridge on top and if so returns 46573b85f9baSLinus Lüssing * true if a multicast router is behind one of the other ports of this 46583b85f9baSLinus Lüssing * bridge. Otherwise returns false. 46593b85f9baSLinus Lüssing */ 46603b85f9baSLinus Lüssing bool br_multicast_has_router_adjacent(struct net_device *dev, int proto) 46613b85f9baSLinus Lüssing { 46629632233eSNikolay Aleksandrov struct net_bridge_mcast_port *pmctx; 4663d3d065c0SNikolay Aleksandrov struct net_bridge_mcast *brmctx; 46649632233eSNikolay Aleksandrov struct net_bridge_port *port; 46653b85f9baSLinus Lüssing bool ret = false; 46663b85f9baSLinus Lüssing 46673b85f9baSLinus Lüssing rcu_read_lock(); 46683b85f9baSLinus Lüssing port = br_port_get_check_rcu(dev); 46693b85f9baSLinus Lüssing if (!port) 46703b85f9baSLinus Lüssing goto unlock; 46713b85f9baSLinus Lüssing 4672d3d065c0SNikolay Aleksandrov brmctx = &port->br->multicast_ctx; 46733b85f9baSLinus Lüssing switch (proto) { 46743b85f9baSLinus Lüssing case ETH_P_IP: 4675d3d065c0SNikolay Aleksandrov hlist_for_each_entry_rcu(pmctx, &brmctx->ip4_mc_router_list, 46763b85f9baSLinus Lüssing ip4_rlist) { 46779632233eSNikolay Aleksandrov if (pmctx->port == port) 46783b85f9baSLinus Lüssing continue; 46793b85f9baSLinus Lüssing 46803b85f9baSLinus Lüssing ret = true; 46813b85f9baSLinus Lüssing goto unlock; 46823b85f9baSLinus Lüssing } 46833b85f9baSLinus Lüssing break; 46843b85f9baSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 46853b85f9baSLinus Lüssing case ETH_P_IPV6: 4686d3d065c0SNikolay Aleksandrov hlist_for_each_entry_rcu(pmctx, &brmctx->ip6_mc_router_list, 46873b85f9baSLinus Lüssing ip6_rlist) { 46889632233eSNikolay Aleksandrov if (pmctx->port == port) 46893b85f9baSLinus Lüssing continue; 46903b85f9baSLinus Lüssing 46913b85f9baSLinus Lüssing ret = true; 46923b85f9baSLinus Lüssing goto unlock; 46933b85f9baSLinus Lüssing } 46943b85f9baSLinus Lüssing break; 46953b85f9baSLinus Lüssing #endif 46963b85f9baSLinus Lüssing default: 46973b85f9baSLinus Lüssing /* when compiled without IPv6 support, be conservative and 46983b85f9baSLinus Lüssing * always assume presence of an IPv6 multicast router 46993b85f9baSLinus Lüssing */ 47003b85f9baSLinus Lüssing ret = true; 47013b85f9baSLinus Lüssing } 47023b85f9baSLinus Lüssing 47033b85f9baSLinus Lüssing unlock: 47043b85f9baSLinus Lüssing rcu_read_unlock(); 47053b85f9baSLinus Lüssing return ret; 47063b85f9baSLinus Lüssing } 47073b85f9baSLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_router_adjacent); 47083b85f9baSLinus Lüssing 47091080ab95SNikolay Aleksandrov static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats, 4710a65056ecSNikolay Aleksandrov const struct sk_buff *skb, u8 type, u8 dir) 47111080ab95SNikolay Aleksandrov { 47121080ab95SNikolay Aleksandrov struct bridge_mcast_stats *pstats = this_cpu_ptr(stats); 4713a65056ecSNikolay Aleksandrov __be16 proto = skb->protocol; 4714a65056ecSNikolay Aleksandrov unsigned int t_len; 47151080ab95SNikolay Aleksandrov 47161080ab95SNikolay Aleksandrov u64_stats_update_begin(&pstats->syncp); 47171080ab95SNikolay Aleksandrov switch (proto) { 47181080ab95SNikolay Aleksandrov case htons(ETH_P_IP): 4719a65056ecSNikolay Aleksandrov t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb); 47201080ab95SNikolay Aleksandrov switch (type) { 47211080ab95SNikolay Aleksandrov case IGMP_HOST_MEMBERSHIP_REPORT: 47221080ab95SNikolay Aleksandrov pstats->mstats.igmp_v1reports[dir]++; 47231080ab95SNikolay Aleksandrov break; 47241080ab95SNikolay Aleksandrov case IGMPV2_HOST_MEMBERSHIP_REPORT: 47251080ab95SNikolay Aleksandrov pstats->mstats.igmp_v2reports[dir]++; 47261080ab95SNikolay Aleksandrov break; 47271080ab95SNikolay Aleksandrov case IGMPV3_HOST_MEMBERSHIP_REPORT: 47281080ab95SNikolay Aleksandrov pstats->mstats.igmp_v3reports[dir]++; 47291080ab95SNikolay Aleksandrov break; 47301080ab95SNikolay Aleksandrov case IGMP_HOST_MEMBERSHIP_QUERY: 4731a65056ecSNikolay Aleksandrov if (t_len != sizeof(struct igmphdr)) { 4732a65056ecSNikolay Aleksandrov pstats->mstats.igmp_v3queries[dir]++; 4733a65056ecSNikolay Aleksandrov } else { 4734a65056ecSNikolay Aleksandrov unsigned int offset = skb_transport_offset(skb); 4735a65056ecSNikolay Aleksandrov struct igmphdr *ih, _ihdr; 4736a65056ecSNikolay Aleksandrov 4737a65056ecSNikolay Aleksandrov ih = skb_header_pointer(skb, offset, 4738a65056ecSNikolay Aleksandrov sizeof(_ihdr), &_ihdr); 4739a65056ecSNikolay Aleksandrov if (!ih) 4740a65056ecSNikolay Aleksandrov break; 4741a65056ecSNikolay Aleksandrov if (!ih->code) 4742a65056ecSNikolay Aleksandrov pstats->mstats.igmp_v1queries[dir]++; 4743a65056ecSNikolay Aleksandrov else 4744a65056ecSNikolay Aleksandrov pstats->mstats.igmp_v2queries[dir]++; 4745a65056ecSNikolay Aleksandrov } 47461080ab95SNikolay Aleksandrov break; 47471080ab95SNikolay Aleksandrov case IGMP_HOST_LEAVE_MESSAGE: 47481080ab95SNikolay Aleksandrov pstats->mstats.igmp_leaves[dir]++; 47491080ab95SNikolay Aleksandrov break; 47501080ab95SNikolay Aleksandrov } 47511080ab95SNikolay Aleksandrov break; 47521080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 47531080ab95SNikolay Aleksandrov case htons(ETH_P_IPV6): 4754a65056ecSNikolay Aleksandrov t_len = ntohs(ipv6_hdr(skb)->payload_len) + 4755a65056ecSNikolay Aleksandrov sizeof(struct ipv6hdr); 4756a65056ecSNikolay Aleksandrov t_len -= skb_network_header_len(skb); 47571080ab95SNikolay Aleksandrov switch (type) { 47581080ab95SNikolay Aleksandrov case ICMPV6_MGM_REPORT: 47591080ab95SNikolay Aleksandrov pstats->mstats.mld_v1reports[dir]++; 47601080ab95SNikolay Aleksandrov break; 47611080ab95SNikolay Aleksandrov case ICMPV6_MLD2_REPORT: 47621080ab95SNikolay Aleksandrov pstats->mstats.mld_v2reports[dir]++; 47631080ab95SNikolay Aleksandrov break; 47641080ab95SNikolay Aleksandrov case ICMPV6_MGM_QUERY: 4765a65056ecSNikolay Aleksandrov if (t_len != sizeof(struct mld_msg)) 4766a65056ecSNikolay Aleksandrov pstats->mstats.mld_v2queries[dir]++; 4767a65056ecSNikolay Aleksandrov else 4768a65056ecSNikolay Aleksandrov pstats->mstats.mld_v1queries[dir]++; 47691080ab95SNikolay Aleksandrov break; 47701080ab95SNikolay Aleksandrov case ICMPV6_MGM_REDUCTION: 47711080ab95SNikolay Aleksandrov pstats->mstats.mld_leaves[dir]++; 47721080ab95SNikolay Aleksandrov break; 47731080ab95SNikolay Aleksandrov } 47741080ab95SNikolay Aleksandrov break; 47751080ab95SNikolay Aleksandrov #endif /* CONFIG_IPV6 */ 47761080ab95SNikolay Aleksandrov } 47771080ab95SNikolay Aleksandrov u64_stats_update_end(&pstats->syncp); 47781080ab95SNikolay Aleksandrov } 47791080ab95SNikolay Aleksandrov 4780adc47037SNikolay Aleksandrov void br_multicast_count(struct net_bridge *br, 4781adc47037SNikolay Aleksandrov const struct net_bridge_port *p, 4782a65056ecSNikolay Aleksandrov const struct sk_buff *skb, u8 type, u8 dir) 47831080ab95SNikolay Aleksandrov { 47841080ab95SNikolay Aleksandrov struct bridge_mcast_stats __percpu *stats; 47851080ab95SNikolay Aleksandrov 47861080ab95SNikolay Aleksandrov /* if multicast_disabled is true then igmp type can't be set */ 4787675779adSNikolay Aleksandrov if (!type || !br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)) 47881080ab95SNikolay Aleksandrov return; 47891080ab95SNikolay Aleksandrov 47901080ab95SNikolay Aleksandrov if (p) 47911080ab95SNikolay Aleksandrov stats = p->mcast_stats; 47921080ab95SNikolay Aleksandrov else 47931080ab95SNikolay Aleksandrov stats = br->mcast_stats; 47941080ab95SNikolay Aleksandrov if (WARN_ON(!stats)) 47951080ab95SNikolay Aleksandrov return; 47961080ab95SNikolay Aleksandrov 4797a65056ecSNikolay Aleksandrov br_mcast_stats_add(stats, skb, type, dir); 47981080ab95SNikolay Aleksandrov } 47991080ab95SNikolay Aleksandrov 48001080ab95SNikolay Aleksandrov int br_multicast_init_stats(struct net_bridge *br) 48011080ab95SNikolay Aleksandrov { 48021080ab95SNikolay Aleksandrov br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats); 48031080ab95SNikolay Aleksandrov if (!br->mcast_stats) 48041080ab95SNikolay Aleksandrov return -ENOMEM; 48051080ab95SNikolay Aleksandrov 48061080ab95SNikolay Aleksandrov return 0; 48071080ab95SNikolay Aleksandrov } 48081080ab95SNikolay Aleksandrov 4809b6fe0440SIdo Schimmel void br_multicast_uninit_stats(struct net_bridge *br) 4810b6fe0440SIdo Schimmel { 4811b6fe0440SIdo Schimmel free_percpu(br->mcast_stats); 4812b6fe0440SIdo Schimmel } 4813b6fe0440SIdo Schimmel 4814b3b6a84cSArnd Bergmann /* noinline for https://bugs.llvm.org/show_bug.cgi?id=45802#c9 */ 4815b3b6a84cSArnd Bergmann static noinline_for_stack void mcast_stats_add_dir(u64 *dst, u64 *src) 48161080ab95SNikolay Aleksandrov { 48171080ab95SNikolay Aleksandrov dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX]; 48181080ab95SNikolay Aleksandrov dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX]; 48191080ab95SNikolay Aleksandrov } 48201080ab95SNikolay Aleksandrov 48211080ab95SNikolay Aleksandrov void br_multicast_get_stats(const struct net_bridge *br, 48221080ab95SNikolay Aleksandrov const struct net_bridge_port *p, 48231080ab95SNikolay Aleksandrov struct br_mcast_stats *dest) 48241080ab95SNikolay Aleksandrov { 48251080ab95SNikolay Aleksandrov struct bridge_mcast_stats __percpu *stats; 48261080ab95SNikolay Aleksandrov struct br_mcast_stats tdst; 48271080ab95SNikolay Aleksandrov int i; 48281080ab95SNikolay Aleksandrov 48291080ab95SNikolay Aleksandrov memset(dest, 0, sizeof(*dest)); 48301080ab95SNikolay Aleksandrov if (p) 48311080ab95SNikolay Aleksandrov stats = p->mcast_stats; 48321080ab95SNikolay Aleksandrov else 48331080ab95SNikolay Aleksandrov stats = br->mcast_stats; 48341080ab95SNikolay Aleksandrov if (WARN_ON(!stats)) 48351080ab95SNikolay Aleksandrov return; 48361080ab95SNikolay Aleksandrov 48371080ab95SNikolay Aleksandrov memset(&tdst, 0, sizeof(tdst)); 48381080ab95SNikolay Aleksandrov for_each_possible_cpu(i) { 48391080ab95SNikolay Aleksandrov struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i); 48401080ab95SNikolay Aleksandrov struct br_mcast_stats temp; 48411080ab95SNikolay Aleksandrov unsigned int start; 48421080ab95SNikolay Aleksandrov 48431080ab95SNikolay Aleksandrov do { 48441080ab95SNikolay Aleksandrov start = u64_stats_fetch_begin_irq(&cpu_stats->syncp); 48451080ab95SNikolay Aleksandrov memcpy(&temp, &cpu_stats->mstats, sizeof(temp)); 48461080ab95SNikolay Aleksandrov } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start)); 48471080ab95SNikolay Aleksandrov 4848a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries); 4849a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries); 4850a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries); 48511080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves); 48521080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports); 48531080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports); 48541080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports); 48551080ab95SNikolay Aleksandrov tdst.igmp_parse_errors += temp.igmp_parse_errors; 48561080ab95SNikolay Aleksandrov 4857a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries); 4858a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries); 48591080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves); 48601080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports); 48611080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports); 48621080ab95SNikolay Aleksandrov tdst.mld_parse_errors += temp.mld_parse_errors; 48631080ab95SNikolay Aleksandrov } 48641080ab95SNikolay Aleksandrov memcpy(dest, &tdst, sizeof(*dest)); 48651080ab95SNikolay Aleksandrov } 486619e3a9c9SNikolay Aleksandrov 486719e3a9c9SNikolay Aleksandrov int br_mdb_hash_init(struct net_bridge *br) 486819e3a9c9SNikolay Aleksandrov { 4869085b53c8SNikolay Aleksandrov int err; 4870085b53c8SNikolay Aleksandrov 4871085b53c8SNikolay Aleksandrov err = rhashtable_init(&br->sg_port_tbl, &br_sg_port_rht_params); 4872085b53c8SNikolay Aleksandrov if (err) 4873085b53c8SNikolay Aleksandrov return err; 4874085b53c8SNikolay Aleksandrov 4875085b53c8SNikolay Aleksandrov err = rhashtable_init(&br->mdb_hash_tbl, &br_mdb_rht_params); 4876085b53c8SNikolay Aleksandrov if (err) { 4877085b53c8SNikolay Aleksandrov rhashtable_destroy(&br->sg_port_tbl); 4878085b53c8SNikolay Aleksandrov return err; 4879085b53c8SNikolay Aleksandrov } 4880085b53c8SNikolay Aleksandrov 4881085b53c8SNikolay Aleksandrov return 0; 488219e3a9c9SNikolay Aleksandrov } 488319e3a9c9SNikolay Aleksandrov 488419e3a9c9SNikolay Aleksandrov void br_mdb_hash_fini(struct net_bridge *br) 488519e3a9c9SNikolay Aleksandrov { 4886085b53c8SNikolay Aleksandrov rhashtable_destroy(&br->sg_port_tbl); 488719e3a9c9SNikolay Aleksandrov rhashtable_destroy(&br->mdb_hash_tbl); 488819e3a9c9SNikolay Aleksandrov } 4889