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 { 2931c7fa1d9bSNikolay Aleksandrov return nla_total_size(sizeof(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 2946c7fa1d9bSNikolay Aleksandrov nest = nla_nest_start(skb, nest_attr); 2947c7fa1d9bSNikolay Aleksandrov if (!nest) 2948c7fa1d9bSNikolay Aleksandrov return -EMSGSIZE; 2949c7fa1d9bSNikolay Aleksandrov 2950c7fa1d9bSNikolay Aleksandrov rcu_read_lock(); 2951*85b41082SNikolay Aleksandrov if (!brmctx->multicast_querier && 2952*85b41082SNikolay Aleksandrov !timer_pending(&brmctx->ip4_other_query.timer)) 2953*85b41082SNikolay Aleksandrov goto out_v6; 2954*85b41082SNikolay Aleksandrov 2955c7fa1d9bSNikolay Aleksandrov br_multicast_read_querier(&brmctx->ip4_querier, &querier); 2956c7fa1d9bSNikolay Aleksandrov if (nla_put_in_addr(skb, BRIDGE_QUERIER_IP_ADDRESS, 2957c7fa1d9bSNikolay Aleksandrov querier.addr.src.ip4)) { 2958c7fa1d9bSNikolay Aleksandrov rcu_read_unlock(); 2959c7fa1d9bSNikolay Aleksandrov goto out_err; 2960c7fa1d9bSNikolay Aleksandrov } 2961c7fa1d9bSNikolay Aleksandrov 2962c7fa1d9bSNikolay Aleksandrov p = __br_multicast_get_querier_port(brmctx->br, &querier); 2963c7fa1d9bSNikolay Aleksandrov if (timer_pending(&brmctx->ip4_other_query.timer) && 2964c7fa1d9bSNikolay Aleksandrov (nla_put_u64_64bit(skb, BRIDGE_QUERIER_IP_OTHER_TIMER, 2965c7fa1d9bSNikolay Aleksandrov br_timer_value(&brmctx->ip4_other_query.timer), 2966c7fa1d9bSNikolay Aleksandrov BRIDGE_QUERIER_PAD) || 2967c7fa1d9bSNikolay Aleksandrov (p && nla_put_u32(skb, BRIDGE_QUERIER_IP_PORT, p->dev->ifindex)))) { 2968c7fa1d9bSNikolay Aleksandrov rcu_read_unlock(); 2969c7fa1d9bSNikolay Aleksandrov goto out_err; 2970c7fa1d9bSNikolay Aleksandrov } 2971*85b41082SNikolay Aleksandrov 2972*85b41082SNikolay Aleksandrov out_v6: 2973*85b41082SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 2974*85b41082SNikolay Aleksandrov if (!brmctx->multicast_querier && 2975*85b41082SNikolay Aleksandrov !timer_pending(&brmctx->ip6_other_query.timer)) 2976*85b41082SNikolay Aleksandrov goto out; 2977*85b41082SNikolay Aleksandrov 2978*85b41082SNikolay Aleksandrov br_multicast_read_querier(&brmctx->ip6_querier, &querier); 2979*85b41082SNikolay Aleksandrov if (nla_put_in6_addr(skb, BRIDGE_QUERIER_IPV6_ADDRESS, 2980*85b41082SNikolay Aleksandrov &querier.addr.src.ip6)) { 2981*85b41082SNikolay Aleksandrov rcu_read_unlock(); 2982*85b41082SNikolay Aleksandrov goto out_err; 2983*85b41082SNikolay Aleksandrov } 2984*85b41082SNikolay Aleksandrov 2985*85b41082SNikolay Aleksandrov p = __br_multicast_get_querier_port(brmctx->br, &querier); 2986*85b41082SNikolay Aleksandrov if (timer_pending(&brmctx->ip6_other_query.timer) && 2987*85b41082SNikolay Aleksandrov (nla_put_u64_64bit(skb, BRIDGE_QUERIER_IPV6_OTHER_TIMER, 2988*85b41082SNikolay Aleksandrov br_timer_value(&brmctx->ip6_other_query.timer), 2989*85b41082SNikolay Aleksandrov BRIDGE_QUERIER_PAD) || 2990*85b41082SNikolay Aleksandrov (p && nla_put_u32(skb, BRIDGE_QUERIER_IPV6_PORT, 2991*85b41082SNikolay Aleksandrov p->dev->ifindex)))) { 2992*85b41082SNikolay Aleksandrov rcu_read_unlock(); 2993*85b41082SNikolay Aleksandrov goto out_err; 2994*85b41082SNikolay Aleksandrov } 2995*85b41082SNikolay Aleksandrov out: 2996*85b41082SNikolay Aleksandrov #endif 2997c7fa1d9bSNikolay Aleksandrov rcu_read_unlock(); 2998c7fa1d9bSNikolay Aleksandrov nla_nest_end(skb, nest); 2999*85b41082SNikolay Aleksandrov if (!nla_len(nest)) 3000*85b41082SNikolay Aleksandrov nla_nest_cancel(skb, nest); 3001c7fa1d9bSNikolay Aleksandrov 3002c7fa1d9bSNikolay Aleksandrov return 0; 3003c7fa1d9bSNikolay Aleksandrov 3004c7fa1d9bSNikolay Aleksandrov out_err: 3005c7fa1d9bSNikolay Aleksandrov nla_nest_cancel(skb, nest); 3006c7fa1d9bSNikolay Aleksandrov return -EMSGSIZE; 3007c7fa1d9bSNikolay Aleksandrov } 3008c7fa1d9bSNikolay Aleksandrov 3009cc0fdd80SLinus Lüssing static void 3010adc47037SNikolay Aleksandrov br_multicast_update_query_timer(struct net_bridge_mcast *brmctx, 301190010b36SLinus Lüssing struct bridge_mcast_other_query *query, 3012b00589afSLinus Lüssing unsigned long max_delay) 3013b00589afSLinus Lüssing { 301490010b36SLinus Lüssing if (!timer_pending(&query->timer)) 301590010b36SLinus Lüssing query->delay_time = jiffies + max_delay; 3016b00589afSLinus Lüssing 3017adc47037SNikolay Aleksandrov mod_timer(&query->timer, jiffies + brmctx->multicast_querier_interval); 3018b00589afSLinus Lüssing } 3019b00589afSLinus Lüssing 30206d549648SNogah Frankel static void br_port_mc_router_state_change(struct net_bridge_port *p, 30216d549648SNogah Frankel bool is_mc_router) 30226d549648SNogah Frankel { 30236d549648SNogah Frankel struct switchdev_attr attr = { 30246d549648SNogah Frankel .orig_dev = p->dev, 30256d549648SNogah Frankel .id = SWITCHDEV_ATTR_ID_PORT_MROUTER, 30266d549648SNogah Frankel .flags = SWITCHDEV_F_DEFER, 30276d549648SNogah Frankel .u.mrouter = is_mc_router, 30286d549648SNogah Frankel }; 30296d549648SNogah Frankel 3030dcbdf135SVladimir Oltean switchdev_port_attr_set(p->dev, &attr, NULL); 30316d549648SNogah Frankel } 30326d549648SNogah Frankel 3033d9b8c4d8SLinus Lüssing static struct net_bridge_port * 3034d3d065c0SNikolay Aleksandrov br_multicast_rport_from_node(struct net_bridge_mcast *brmctx, 3035d9b8c4d8SLinus Lüssing struct hlist_head *mc_router_list, 3036d9b8c4d8SLinus Lüssing struct hlist_node *rlist) 3037d9b8c4d8SLinus Lüssing { 30389632233eSNikolay Aleksandrov struct net_bridge_mcast_port *pmctx; 30399632233eSNikolay Aleksandrov 3040a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 3041d3d065c0SNikolay Aleksandrov if (mc_router_list == &brmctx->ip6_mc_router_list) 30429632233eSNikolay Aleksandrov pmctx = hlist_entry(rlist, struct net_bridge_mcast_port, 30439632233eSNikolay Aleksandrov ip6_rlist); 30449632233eSNikolay Aleksandrov else 3045a3c02e76SLinus Lüssing #endif 30469632233eSNikolay Aleksandrov pmctx = hlist_entry(rlist, struct net_bridge_mcast_port, 30479632233eSNikolay Aleksandrov ip4_rlist); 30489632233eSNikolay Aleksandrov 30499632233eSNikolay Aleksandrov return pmctx->port; 3050d9b8c4d8SLinus Lüssing } 3051d9b8c4d8SLinus Lüssing 3052d9b8c4d8SLinus Lüssing static struct hlist_node * 3053d3d065c0SNikolay Aleksandrov br_multicast_get_rport_slot(struct net_bridge_mcast *brmctx, 3054d9b8c4d8SLinus Lüssing struct net_bridge_port *port, 3055d9b8c4d8SLinus Lüssing struct hlist_head *mc_router_list) 3056d9b8c4d8SLinus Lüssing 3057d9b8c4d8SLinus Lüssing { 3058d9b8c4d8SLinus Lüssing struct hlist_node *slot = NULL; 3059d9b8c4d8SLinus Lüssing struct net_bridge_port *p; 3060d9b8c4d8SLinus Lüssing struct hlist_node *rlist; 3061d9b8c4d8SLinus Lüssing 3062d9b8c4d8SLinus Lüssing hlist_for_each(rlist, mc_router_list) { 3063d3d065c0SNikolay Aleksandrov p = br_multicast_rport_from_node(brmctx, mc_router_list, rlist); 3064d9b8c4d8SLinus Lüssing 3065d9b8c4d8SLinus Lüssing if ((unsigned long)port >= (unsigned long)p) 3066d9b8c4d8SLinus Lüssing break; 3067d9b8c4d8SLinus Lüssing 3068d9b8c4d8SLinus Lüssing slot = rlist; 3069d9b8c4d8SLinus Lüssing } 3070d9b8c4d8SLinus Lüssing 3071d9b8c4d8SLinus Lüssing return slot; 3072d9b8c4d8SLinus Lüssing } 3073d9b8c4d8SLinus Lüssing 3074adc47037SNikolay Aleksandrov static bool br_multicast_no_router_otherpf(struct net_bridge_mcast_port *pmctx, 3075a3c02e76SLinus Lüssing struct hlist_node *rnode) 3076a3c02e76SLinus Lüssing { 3077a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 3078adc47037SNikolay Aleksandrov if (rnode != &pmctx->ip6_rlist) 3079adc47037SNikolay Aleksandrov return hlist_unhashed(&pmctx->ip6_rlist); 3080a3c02e76SLinus Lüssing else 3081adc47037SNikolay Aleksandrov return hlist_unhashed(&pmctx->ip4_rlist); 3082a3c02e76SLinus Lüssing #else 3083a3c02e76SLinus Lüssing return true; 3084a3c02e76SLinus Lüssing #endif 3085a3c02e76SLinus Lüssing } 3086a3c02e76SLinus Lüssing 3087d9b8c4d8SLinus Lüssing /* Add port to router_list 30887e80c124Sstephen hemminger * list is maintained ordered by pointer value 30897e80c124Sstephen hemminger * and locked by br->multicast_lock and RCU 30907e80c124Sstephen hemminger */ 3091d3d065c0SNikolay Aleksandrov static void br_multicast_add_router(struct net_bridge_mcast *brmctx, 3092adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 3093d9b8c4d8SLinus Lüssing struct hlist_node *rlist, 3094d9b8c4d8SLinus Lüssing struct hlist_head *mc_router_list) 30950909e117SHerbert Xu { 3096d9b8c4d8SLinus Lüssing struct hlist_node *slot; 30970909e117SHerbert Xu 3098d9b8c4d8SLinus Lüssing if (!hlist_unhashed(rlist)) 30991a040eacSNikolay Aleksandrov return; 31001a040eacSNikolay Aleksandrov 3101adc47037SNikolay Aleksandrov slot = br_multicast_get_rport_slot(brmctx, pmctx->port, mc_router_list); 31020909e117SHerbert Xu 31037e80c124Sstephen hemminger if (slot) 3104d9b8c4d8SLinus Lüssing hlist_add_behind_rcu(rlist, slot); 3105dcdca2c4Sstephen hemminger else 3106d9b8c4d8SLinus Lüssing hlist_add_head_rcu(rlist, mc_router_list); 3107d9b8c4d8SLinus Lüssing 3108a3c02e76SLinus Lüssing /* For backwards compatibility for now, only notify if we 3109a3c02e76SLinus Lüssing * switched from no IPv4/IPv6 multicast router to a new 3110a3c02e76SLinus Lüssing * IPv4 or IPv6 multicast router. 3111a3c02e76SLinus Lüssing */ 3112adc47037SNikolay Aleksandrov if (br_multicast_no_router_otherpf(pmctx, rlist)) { 31131e9ca456SNikolay Aleksandrov br_rtr_notify(pmctx->port->br->dev, pmctx, RTM_NEWMDB); 3114adc47037SNikolay Aleksandrov br_port_mc_router_state_change(pmctx->port, true); 31150909e117SHerbert Xu } 3116a3c02e76SLinus Lüssing } 31170909e117SHerbert Xu 3118d9b8c4d8SLinus Lüssing /* Add port to router_list 3119d9b8c4d8SLinus Lüssing * list is maintained ordered by pointer value 3120d9b8c4d8SLinus Lüssing * and locked by br->multicast_lock and RCU 3121d9b8c4d8SLinus Lüssing */ 3122adc47037SNikolay Aleksandrov static void br_ip4_multicast_add_router(struct net_bridge_mcast *brmctx, 3123adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx) 3124eb1d1641SHerbert Xu { 3125adc47037SNikolay Aleksandrov br_multicast_add_router(brmctx, pmctx, &pmctx->ip4_rlist, 3126adc47037SNikolay Aleksandrov &brmctx->ip4_mc_router_list); 3127d9b8c4d8SLinus Lüssing } 3128d9b8c4d8SLinus Lüssing 3129a3c02e76SLinus Lüssing /* Add port to router_list 3130a3c02e76SLinus Lüssing * list is maintained ordered by pointer value 3131a3c02e76SLinus Lüssing * and locked by br->multicast_lock and RCU 3132a3c02e76SLinus Lüssing */ 3133adc47037SNikolay Aleksandrov static void br_ip6_multicast_add_router(struct net_bridge_mcast *brmctx, 3134adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx) 3135a3c02e76SLinus Lüssing { 3136a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 3137adc47037SNikolay Aleksandrov br_multicast_add_router(brmctx, pmctx, &pmctx->ip6_rlist, 3138adc47037SNikolay Aleksandrov &brmctx->ip6_mc_router_list); 3139a3c02e76SLinus Lüssing #endif 3140a3c02e76SLinus Lüssing } 3141a3c02e76SLinus Lüssing 3142adc47037SNikolay Aleksandrov static void br_multicast_mark_router(struct net_bridge_mcast *brmctx, 3143adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 3144d9b8c4d8SLinus Lüssing struct timer_list *timer, 3145d9b8c4d8SLinus Lüssing struct hlist_node *rlist, 3146d9b8c4d8SLinus Lüssing struct hlist_head *mc_router_list) 3147d9b8c4d8SLinus Lüssing { 3148eb1d1641SHerbert Xu unsigned long now = jiffies; 3149eb1d1641SHerbert Xu 31504cdd0d10SNikolay Aleksandrov if (!br_multicast_ctx_should_use(brmctx, pmctx)) 31514cdd0d10SNikolay Aleksandrov return; 31524cdd0d10SNikolay Aleksandrov 3153adc47037SNikolay Aleksandrov if (!pmctx) { 3154d3d065c0SNikolay Aleksandrov if (brmctx->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) { 3155d3d065c0SNikolay Aleksandrov if (!br_ip4_multicast_is_router(brmctx) && 3156d3d065c0SNikolay Aleksandrov !br_ip6_multicast_is_router(brmctx)) 3157adc47037SNikolay Aleksandrov br_mc_router_state_change(brmctx->br, true); 3158d3d065c0SNikolay Aleksandrov mod_timer(timer, now + brmctx->multicast_querier_interval); 315977041420SYotam Gigi } 3160eb1d1641SHerbert Xu return; 3161eb1d1641SHerbert Xu } 3162eb1d1641SHerbert Xu 3163adc47037SNikolay Aleksandrov if (pmctx->multicast_router == MDB_RTR_TYPE_DISABLED || 3164adc47037SNikolay Aleksandrov pmctx->multicast_router == MDB_RTR_TYPE_PERM) 3165eb1d1641SHerbert Xu return; 3166eb1d1641SHerbert Xu 3167adc47037SNikolay Aleksandrov br_multicast_add_router(brmctx, pmctx, rlist, mc_router_list); 3168d3d065c0SNikolay Aleksandrov mod_timer(timer, now + brmctx->multicast_querier_interval); 3169d9b8c4d8SLinus Lüssing } 3170eb1d1641SHerbert Xu 3171adc47037SNikolay Aleksandrov static void br_ip4_multicast_mark_router(struct net_bridge_mcast *brmctx, 3172adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx) 3173d9b8c4d8SLinus Lüssing { 3174adc47037SNikolay Aleksandrov struct timer_list *timer = &brmctx->ip4_mc_router_timer; 3175d9b8c4d8SLinus Lüssing struct hlist_node *rlist = NULL; 3176d9b8c4d8SLinus Lüssing 3177adc47037SNikolay Aleksandrov if (pmctx) { 3178adc47037SNikolay Aleksandrov timer = &pmctx->ip4_mc_router_timer; 3179adc47037SNikolay Aleksandrov rlist = &pmctx->ip4_rlist; 3180d9b8c4d8SLinus Lüssing } 3181d9b8c4d8SLinus Lüssing 3182adc47037SNikolay Aleksandrov br_multicast_mark_router(brmctx, pmctx, timer, rlist, 3183adc47037SNikolay Aleksandrov &brmctx->ip4_mc_router_list); 3184eb1d1641SHerbert Xu } 3185eb1d1641SHerbert Xu 3186adc47037SNikolay Aleksandrov static void br_ip6_multicast_mark_router(struct net_bridge_mcast *brmctx, 3187adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx) 3188a3c02e76SLinus Lüssing { 3189a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 3190adc47037SNikolay Aleksandrov struct timer_list *timer = &brmctx->ip6_mc_router_timer; 3191a3c02e76SLinus Lüssing struct hlist_node *rlist = NULL; 3192a3c02e76SLinus Lüssing 3193adc47037SNikolay Aleksandrov if (pmctx) { 3194adc47037SNikolay Aleksandrov timer = &pmctx->ip6_mc_router_timer; 3195adc47037SNikolay Aleksandrov rlist = &pmctx->ip6_rlist; 3196a3c02e76SLinus Lüssing } 3197a3c02e76SLinus Lüssing 3198adc47037SNikolay Aleksandrov br_multicast_mark_router(brmctx, pmctx, timer, rlist, 3199adc47037SNikolay Aleksandrov &brmctx->ip6_mc_router_list); 3200a3c02e76SLinus Lüssing #endif 3201a3c02e76SLinus Lüssing } 3202a3c02e76SLinus Lüssing 3203b19232efSLinus Lüssing static void 3204adc47037SNikolay Aleksandrov br_ip4_multicast_query_received(struct net_bridge_mcast *brmctx, 3205adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 320690010b36SLinus Lüssing struct bridge_mcast_other_query *query, 3207dc4eb53aSLinus Lüssing struct br_ip *saddr, 3208b00589afSLinus Lüssing unsigned long max_delay) 3209eb1d1641SHerbert Xu { 3210c3fb3698SNikolay Aleksandrov if (!br_multicast_select_querier(brmctx, pmctx, saddr)) 3211eb1d1641SHerbert Xu return; 3212eb1d1641SHerbert Xu 3213adc47037SNikolay Aleksandrov br_multicast_update_query_timer(brmctx, query, max_delay); 3214adc47037SNikolay Aleksandrov br_ip4_multicast_mark_router(brmctx, pmctx); 3215eb1d1641SHerbert Xu } 3216eb1d1641SHerbert Xu 3217b19232efSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 3218b19232efSLinus Lüssing static void 3219adc47037SNikolay Aleksandrov br_ip6_multicast_query_received(struct net_bridge_mcast *brmctx, 3220adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 3221b19232efSLinus Lüssing struct bridge_mcast_other_query *query, 3222b19232efSLinus Lüssing struct br_ip *saddr, 3223b19232efSLinus Lüssing unsigned long max_delay) 3224b19232efSLinus Lüssing { 3225c3fb3698SNikolay Aleksandrov if (!br_multicast_select_querier(brmctx, pmctx, saddr)) 3226b19232efSLinus Lüssing return; 3227b19232efSLinus Lüssing 3228adc47037SNikolay Aleksandrov br_multicast_update_query_timer(brmctx, query, max_delay); 3229adc47037SNikolay Aleksandrov br_ip6_multicast_mark_router(brmctx, pmctx); 3230b19232efSLinus Lüssing } 3231b19232efSLinus Lüssing #endif 3232b19232efSLinus Lüssing 3233adc47037SNikolay Aleksandrov static void br_ip4_multicast_query(struct net_bridge_mcast *brmctx, 3234adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 323506499098SVlad Yasevich struct sk_buff *skb, 323606499098SVlad Yasevich u16 vid) 3237eb1d1641SHerbert Xu { 3238ba5ea614SLinus Lüssing unsigned int transport_len = ip_transport_len(skb); 3239b71d1d42SEric Dumazet const struct iphdr *iph = ip_hdr(skb); 3240eb1d1641SHerbert Xu struct igmphdr *ih = igmp_hdr(skb); 3241eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 3242eb1d1641SHerbert Xu struct igmpv3_query *ih3; 3243eb1d1641SHerbert Xu struct net_bridge_port_group *p; 3244e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 324567b746f9SNikolay Aleksandrov struct br_ip saddr = {}; 3246eb1d1641SHerbert Xu unsigned long max_delay; 3247eb1d1641SHerbert Xu unsigned long now = jiffies; 3248eb1d1641SHerbert Xu __be32 group; 3249eb1d1641SHerbert Xu 3250adc47037SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 32514cdd0d10SNikolay Aleksandrov if (!br_multicast_ctx_should_use(brmctx, pmctx)) 3252eb1d1641SHerbert Xu goto out; 3253eb1d1641SHerbert Xu 3254eb1d1641SHerbert Xu group = ih->group; 3255eb1d1641SHerbert Xu 3256ba5ea614SLinus Lüssing if (transport_len == sizeof(*ih)) { 3257eb1d1641SHerbert Xu max_delay = ih->code * (HZ / IGMP_TIMER_SCALE); 3258eb1d1641SHerbert Xu 3259eb1d1641SHerbert Xu if (!max_delay) { 3260eb1d1641SHerbert Xu max_delay = 10 * HZ; 3261eb1d1641SHerbert Xu group = 0; 3262eb1d1641SHerbert Xu } 3263ba5ea614SLinus Lüssing } else if (transport_len >= sizeof(*ih3)) { 3264eb1d1641SHerbert Xu ih3 = igmpv3_query_hdr(skb); 326523550b83SNikolay Aleksandrov if (ih3->nsrcs || 3266d3d065c0SNikolay Aleksandrov (brmctx->multicast_igmp_version == 3 && group && 3267d3d065c0SNikolay Aleksandrov ih3->suppress)) 3268bec68ff1SYOSHIFUJI Hideaki goto out; 3269eb1d1641SHerbert Xu 32700ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 max_delay = ih3->code ? 32710ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1; 32729afd85c9SLinus Lüssing } else { 32739ed973ccSLinus Lüssing goto out; 32749ed973ccSLinus Lüssing } 32759ed973ccSLinus Lüssing 3276dc4eb53aSLinus Lüssing if (!group) { 3277dc4eb53aSLinus Lüssing saddr.proto = htons(ETH_P_IP); 3278deb96566SNikolay Aleksandrov saddr.src.ip4 = iph->saddr; 3279b00589afSLinus Lüssing 3280adc47037SNikolay Aleksandrov br_ip4_multicast_query_received(brmctx, pmctx, 3281d3d065c0SNikolay Aleksandrov &brmctx->ip4_other_query, 3282dc4eb53aSLinus Lüssing &saddr, max_delay); 3283eb1d1641SHerbert Xu goto out; 3284dc4eb53aSLinus Lüssing } 3285eb1d1641SHerbert Xu 3286adc47037SNikolay Aleksandrov mp = br_mdb_ip4_get(brmctx->br, group, vid); 3287eb1d1641SHerbert Xu if (!mp) 3288eb1d1641SHerbert Xu goto out; 3289eb1d1641SHerbert Xu 3290d3d065c0SNikolay Aleksandrov max_delay *= brmctx->multicast_last_member_count; 3291eb1d1641SHerbert Xu 3292ff0fd34eSAndrew Lunn if (mp->host_joined && 3293eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 3294eb1d1641SHerbert Xu time_after(mp->timer.expires, now + max_delay) : 3295eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) 3296eb1d1641SHerbert Xu mod_timer(&mp->timer, now + max_delay); 3297eb1d1641SHerbert Xu 3298e8051688SEric Dumazet for (pp = &mp->ports; 3299adc47037SNikolay Aleksandrov (p = mlock_dereference(*pp, brmctx->br)) != NULL; 3300e8051688SEric Dumazet pp = &p->next) { 3301eb1d1641SHerbert Xu if (timer_pending(&p->timer) ? 3302eb1d1641SHerbert Xu time_after(p->timer.expires, now + max_delay) : 330323550b83SNikolay Aleksandrov try_to_del_timer_sync(&p->timer) >= 0 && 3304d3d065c0SNikolay Aleksandrov (brmctx->multicast_igmp_version == 2 || 330523550b83SNikolay Aleksandrov p->filter_mode == MCAST_EXCLUDE)) 330624f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 3307eb1d1641SHerbert Xu } 3308eb1d1641SHerbert Xu 3309eb1d1641SHerbert Xu out: 3310adc47037SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 3311eb1d1641SHerbert Xu } 3312eb1d1641SHerbert Xu 3313dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 3314adc47037SNikolay Aleksandrov static int br_ip6_multicast_query(struct net_bridge_mcast *brmctx, 3315adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 331606499098SVlad Yasevich struct sk_buff *skb, 331706499098SVlad Yasevich u16 vid) 331808b202b6SYOSHIFUJI Hideaki { 3319ba5ea614SLinus Lüssing unsigned int transport_len = ipv6_transport_len(skb); 3320eca2a43bSstephen hemminger struct mld_msg *mld; 332108b202b6SYOSHIFUJI Hideaki struct net_bridge_mdb_entry *mp; 332208b202b6SYOSHIFUJI Hideaki struct mld2_query *mld2q; 3323e8051688SEric Dumazet struct net_bridge_port_group *p; 3324e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 332567b746f9SNikolay Aleksandrov struct br_ip saddr = {}; 332608b202b6SYOSHIFUJI Hideaki unsigned long max_delay; 332708b202b6SYOSHIFUJI Hideaki unsigned long now = jiffies; 3328856ce5d0SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 3329b71d1d42SEric Dumazet const struct in6_addr *group = NULL; 33309ed973ccSLinus Lüssing bool is_general_query; 333108b202b6SYOSHIFUJI Hideaki int err = 0; 333208b202b6SYOSHIFUJI Hideaki 3333adc47037SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 33344cdd0d10SNikolay Aleksandrov if (!br_multicast_ctx_should_use(brmctx, pmctx)) 333508b202b6SYOSHIFUJI Hideaki goto out; 333608b202b6SYOSHIFUJI Hideaki 3337ba5ea614SLinus Lüssing if (transport_len == sizeof(*mld)) { 3338856ce5d0SLinus Lüssing if (!pskb_may_pull(skb, offset + sizeof(*mld))) { 333908b202b6SYOSHIFUJI Hideaki err = -EINVAL; 334008b202b6SYOSHIFUJI Hideaki goto out; 334108b202b6SYOSHIFUJI Hideaki } 334208b202b6SYOSHIFUJI Hideaki mld = (struct mld_msg *) icmp6_hdr(skb); 33434715213dSLi RongQing max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay)); 334408b202b6SYOSHIFUJI Hideaki if (max_delay) 334508b202b6SYOSHIFUJI Hideaki group = &mld->mld_mca; 3346248ba8ecSLinus Lüssing } else { 3347856ce5d0SLinus Lüssing if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) { 334808b202b6SYOSHIFUJI Hideaki err = -EINVAL; 334908b202b6SYOSHIFUJI Hideaki goto out; 335008b202b6SYOSHIFUJI Hideaki } 335108b202b6SYOSHIFUJI Hideaki mld2q = (struct mld2_query *)icmp6_hdr(skb); 335208b202b6SYOSHIFUJI Hideaki if (!mld2q->mld2q_nsrcs) 335308b202b6SYOSHIFUJI Hideaki group = &mld2q->mld2q_mca; 3354d3d065c0SNikolay Aleksandrov if (brmctx->multicast_mld_version == 2 && 335523550b83SNikolay Aleksandrov !ipv6_addr_any(&mld2q->mld2q_mca) && 335623550b83SNikolay Aleksandrov mld2q->mld2q_suppress) 335723550b83SNikolay Aleksandrov goto out; 3358e3f5b170SDaniel Borkmann 3359e3f5b170SDaniel Borkmann max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL); 336008b202b6SYOSHIFUJI Hideaki } 336108b202b6SYOSHIFUJI Hideaki 33629ed973ccSLinus Lüssing is_general_query = group && ipv6_addr_any(group); 33639ed973ccSLinus Lüssing 3364dc4eb53aSLinus Lüssing if (is_general_query) { 3365dc4eb53aSLinus Lüssing saddr.proto = htons(ETH_P_IPV6); 3366deb96566SNikolay Aleksandrov saddr.src.ip6 = ipv6_hdr(skb)->saddr; 3367b00589afSLinus Lüssing 3368adc47037SNikolay Aleksandrov br_ip6_multicast_query_received(brmctx, pmctx, 3369d3d065c0SNikolay Aleksandrov &brmctx->ip6_other_query, 3370dc4eb53aSLinus Lüssing &saddr, max_delay); 337108b202b6SYOSHIFUJI Hideaki goto out; 33726c03ee8bSLinus Lüssing } else if (!group) { 33736c03ee8bSLinus Lüssing goto out; 3374dc4eb53aSLinus Lüssing } 337508b202b6SYOSHIFUJI Hideaki 3376adc47037SNikolay Aleksandrov mp = br_mdb_ip6_get(brmctx->br, group, vid); 337708b202b6SYOSHIFUJI Hideaki if (!mp) 337808b202b6SYOSHIFUJI Hideaki goto out; 337908b202b6SYOSHIFUJI Hideaki 3380d3d065c0SNikolay Aleksandrov max_delay *= brmctx->multicast_last_member_count; 3381ff0fd34eSAndrew Lunn if (mp->host_joined && 338208b202b6SYOSHIFUJI Hideaki (timer_pending(&mp->timer) ? 338308b202b6SYOSHIFUJI Hideaki time_after(mp->timer.expires, now + max_delay) : 338408b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&mp->timer) >= 0)) 338508b202b6SYOSHIFUJI Hideaki mod_timer(&mp->timer, now + max_delay); 338608b202b6SYOSHIFUJI Hideaki 3387e8051688SEric Dumazet for (pp = &mp->ports; 3388adc47037SNikolay Aleksandrov (p = mlock_dereference(*pp, brmctx->br)) != NULL; 3389e8051688SEric Dumazet pp = &p->next) { 339008b202b6SYOSHIFUJI Hideaki if (timer_pending(&p->timer) ? 339108b202b6SYOSHIFUJI Hideaki time_after(p->timer.expires, now + max_delay) : 339223550b83SNikolay Aleksandrov try_to_del_timer_sync(&p->timer) >= 0 && 3393d3d065c0SNikolay Aleksandrov (brmctx->multicast_mld_version == 1 || 339423550b83SNikolay Aleksandrov p->filter_mode == MCAST_EXCLUDE)) 339524f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 339608b202b6SYOSHIFUJI Hideaki } 339708b202b6SYOSHIFUJI Hideaki 339808b202b6SYOSHIFUJI Hideaki out: 3399adc47037SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 340008b202b6SYOSHIFUJI Hideaki return err; 340108b202b6SYOSHIFUJI Hideaki } 340208b202b6SYOSHIFUJI Hideaki #endif 340308b202b6SYOSHIFUJI Hideaki 340490010b36SLinus Lüssing static void 3405adc47037SNikolay Aleksandrov br_multicast_leave_group(struct net_bridge_mcast *brmctx, 3406adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 3407cc0fdd80SLinus Lüssing struct br_ip *group, 340890010b36SLinus Lüssing struct bridge_mcast_other_query *other_query, 34096db6f0eaSFelix Fietkau struct bridge_mcast_own_query *own_query, 34106db6f0eaSFelix Fietkau const unsigned char *src) 3411eb1d1641SHerbert Xu { 3412eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 3413eb1d1641SHerbert Xu struct net_bridge_port_group *p; 3414eb1d1641SHerbert Xu unsigned long now; 3415eb1d1641SHerbert Xu unsigned long time; 3416eb1d1641SHerbert Xu 3417adc47037SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 34184cdd0d10SNikolay Aleksandrov if (!br_multicast_ctx_should_use(brmctx, pmctx)) 3419eb1d1641SHerbert Xu goto out; 3420eb1d1641SHerbert Xu 3421adc47037SNikolay Aleksandrov mp = br_mdb_ip_get(brmctx->br, group); 3422eb1d1641SHerbert Xu if (!mp) 3423eb1d1641SHerbert Xu goto out; 3424eb1d1641SHerbert Xu 3425adc47037SNikolay Aleksandrov if (pmctx && (pmctx->port->flags & BR_MULTICAST_FAST_LEAVE)) { 3426544586f7SSatish Ashok struct net_bridge_port_group __rcu **pp; 3427544586f7SSatish Ashok 3428544586f7SSatish Ashok for (pp = &mp->ports; 3429adc47037SNikolay Aleksandrov (p = mlock_dereference(*pp, brmctx->br)) != NULL; 3430544586f7SSatish Ashok pp = &p->next) { 3431adc47037SNikolay Aleksandrov if (!br_port_group_equal(p, pmctx->port, src)) 3432544586f7SSatish Ashok continue; 3433544586f7SSatish Ashok 34345c725b6bSNikolay Aleksandrov if (p->flags & MDB_PG_FLAGS_PERMANENT) 34355c725b6bSNikolay Aleksandrov break; 34365c725b6bSNikolay Aleksandrov 3437681590bdSNikolay Aleksandrov p->flags |= MDB_PG_FLAGS_FAST_LEAVE; 3438681590bdSNikolay Aleksandrov br_multicast_del_pg(mp, p, pp); 3439544586f7SSatish Ashok } 3440544586f7SSatish Ashok goto out; 3441544586f7SSatish Ashok } 3442544586f7SSatish Ashok 3443544586f7SSatish Ashok if (timer_pending(&other_query->timer)) 3444544586f7SSatish Ashok goto out; 3445544586f7SSatish Ashok 34464d5b4e84SNikolay Aleksandrov if (brmctx->multicast_querier) { 3447adc47037SNikolay Aleksandrov __br_multicast_send_query(brmctx, pmctx, NULL, NULL, &mp->addr, 344842c11ccfSNikolay Aleksandrov false, 0, NULL); 34496b7df111SCong Wang 3450d3d065c0SNikolay Aleksandrov time = jiffies + brmctx->multicast_last_member_count * 3451d3d065c0SNikolay Aleksandrov brmctx->multicast_last_member_interval; 3452cc0fdd80SLinus Lüssing 345390010b36SLinus Lüssing mod_timer(&own_query->timer, time); 34546b7df111SCong Wang 3455adc47037SNikolay Aleksandrov for (p = mlock_dereference(mp->ports, brmctx->br); 345654cb4319SNikolay Aleksandrov p != NULL && pmctx != NULL; 3457adc47037SNikolay Aleksandrov p = mlock_dereference(p->next, brmctx->br)) { 3458adc47037SNikolay Aleksandrov if (!br_port_group_equal(p, pmctx->port, src)) 34596b7df111SCong Wang continue; 34606b7df111SCong Wang 34616b7df111SCong Wang if (!hlist_unhashed(&p->mglist) && 34626b7df111SCong Wang (timer_pending(&p->timer) ? 34636b7df111SCong Wang time_after(p->timer.expires, time) : 34646b7df111SCong Wang try_to_del_timer_sync(&p->timer) >= 0)) { 34656b7df111SCong Wang mod_timer(&p->timer, time); 34666b7df111SCong Wang } 34676b7df111SCong Wang 34686b7df111SCong Wang break; 34696b7df111SCong Wang } 34706b7df111SCong Wang } 34716b7df111SCong Wang 3472eb1d1641SHerbert Xu now = jiffies; 3473d3d065c0SNikolay Aleksandrov time = now + brmctx->multicast_last_member_count * 3474d3d065c0SNikolay Aleksandrov brmctx->multicast_last_member_interval; 3475eb1d1641SHerbert Xu 3476adc47037SNikolay Aleksandrov if (!pmctx) { 3477ff0fd34eSAndrew Lunn if (mp->host_joined && 3478eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 3479eb1d1641SHerbert Xu time_after(mp->timer.expires, time) : 3480eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) { 3481eb1d1641SHerbert Xu mod_timer(&mp->timer, time); 3482eb1d1641SHerbert Xu } 3483454594f3SLinus Lüssing 3484454594f3SLinus Lüssing goto out; 3485454594f3SLinus Lüssing } 3486454594f3SLinus Lüssing 3487adc47037SNikolay Aleksandrov for (p = mlock_dereference(mp->ports, brmctx->br); 3488454594f3SLinus Lüssing p != NULL; 3489adc47037SNikolay Aleksandrov p = mlock_dereference(p->next, brmctx->br)) { 3490adc47037SNikolay Aleksandrov if (p->key.port != pmctx->port) 3491454594f3SLinus Lüssing continue; 3492454594f3SLinus Lüssing 3493454594f3SLinus Lüssing if (!hlist_unhashed(&p->mglist) && 3494454594f3SLinus Lüssing (timer_pending(&p->timer) ? 3495454594f3SLinus Lüssing time_after(p->timer.expires, time) : 3496454594f3SLinus Lüssing try_to_del_timer_sync(&p->timer) >= 0)) { 3497454594f3SLinus Lüssing mod_timer(&p->timer, time); 3498454594f3SLinus Lüssing } 3499454594f3SLinus Lüssing 3500454594f3SLinus Lüssing break; 3501eb1d1641SHerbert Xu } 3502eb1d1641SHerbert Xu out: 3503adc47037SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 3504eb1d1641SHerbert Xu } 3505eb1d1641SHerbert Xu 3506adc47037SNikolay Aleksandrov static void br_ip4_multicast_leave_group(struct net_bridge_mcast *brmctx, 3507adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 3508b0e9a30dSVlad Yasevich __be32 group, 35096db6f0eaSFelix Fietkau __u16 vid, 35106db6f0eaSFelix Fietkau const unsigned char *src) 35118ef2a9a5SYOSHIFUJI Hideaki { 35128ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 351390010b36SLinus Lüssing struct bridge_mcast_own_query *own_query; 35148ef2a9a5SYOSHIFUJI Hideaki 35158ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 35168ef2a9a5SYOSHIFUJI Hideaki return; 35178ef2a9a5SYOSHIFUJI Hideaki 3518adc47037SNikolay Aleksandrov own_query = pmctx ? &pmctx->ip4_own_query : &brmctx->ip4_own_query; 351990010b36SLinus Lüssing 35201515a63fSNikolay Aleksandrov memset(&br_group, 0, sizeof(br_group)); 3521eab3227bSNikolay Aleksandrov br_group.dst.ip4 = group; 35228ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 3523b0e9a30dSVlad Yasevich br_group.vid = vid; 35248ef2a9a5SYOSHIFUJI Hideaki 3525adc47037SNikolay Aleksandrov br_multicast_leave_group(brmctx, pmctx, &br_group, 3526adc47037SNikolay Aleksandrov &brmctx->ip4_other_query, 35276db6f0eaSFelix Fietkau own_query, src); 35288ef2a9a5SYOSHIFUJI Hideaki } 35298ef2a9a5SYOSHIFUJI Hideaki 3530dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 3531adc47037SNikolay Aleksandrov static void br_ip6_multicast_leave_group(struct net_bridge_mcast *brmctx, 3532adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 3533b0e9a30dSVlad Yasevich const struct in6_addr *group, 35346db6f0eaSFelix Fietkau __u16 vid, 35356db6f0eaSFelix Fietkau const unsigned char *src) 353608b202b6SYOSHIFUJI Hideaki { 353708b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 353890010b36SLinus Lüssing struct bridge_mcast_own_query *own_query; 353908b202b6SYOSHIFUJI Hideaki 35403c3769e6SLinus Lüssing if (ipv6_addr_is_ll_all_nodes(group)) 354108b202b6SYOSHIFUJI Hideaki return; 354208b202b6SYOSHIFUJI Hideaki 3543adc47037SNikolay Aleksandrov own_query = pmctx ? &pmctx->ip6_own_query : &brmctx->ip6_own_query; 354490010b36SLinus Lüssing 35451515a63fSNikolay Aleksandrov memset(&br_group, 0, sizeof(br_group)); 3546eab3227bSNikolay Aleksandrov br_group.dst.ip6 = *group; 354708b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 3548b0e9a30dSVlad Yasevich br_group.vid = vid; 354908b202b6SYOSHIFUJI Hideaki 3550adc47037SNikolay Aleksandrov br_multicast_leave_group(brmctx, pmctx, &br_group, 3551adc47037SNikolay Aleksandrov &brmctx->ip6_other_query, 35526db6f0eaSFelix Fietkau own_query, src); 355308b202b6SYOSHIFUJI Hideaki } 355408b202b6SYOSHIFUJI Hideaki #endif 35558ef2a9a5SYOSHIFUJI Hideaki 35561080ab95SNikolay Aleksandrov static void br_multicast_err_count(const struct net_bridge *br, 35571080ab95SNikolay Aleksandrov const struct net_bridge_port *p, 35581080ab95SNikolay Aleksandrov __be16 proto) 35591080ab95SNikolay Aleksandrov { 35601080ab95SNikolay Aleksandrov struct bridge_mcast_stats __percpu *stats; 35611080ab95SNikolay Aleksandrov struct bridge_mcast_stats *pstats; 35621080ab95SNikolay Aleksandrov 3563675779adSNikolay Aleksandrov if (!br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)) 35641080ab95SNikolay Aleksandrov return; 35651080ab95SNikolay Aleksandrov 35661080ab95SNikolay Aleksandrov if (p) 35671080ab95SNikolay Aleksandrov stats = p->mcast_stats; 35681080ab95SNikolay Aleksandrov else 35691080ab95SNikolay Aleksandrov stats = br->mcast_stats; 35701080ab95SNikolay Aleksandrov if (WARN_ON(!stats)) 35711080ab95SNikolay Aleksandrov return; 35721080ab95SNikolay Aleksandrov 35731080ab95SNikolay Aleksandrov pstats = this_cpu_ptr(stats); 35741080ab95SNikolay Aleksandrov 35751080ab95SNikolay Aleksandrov u64_stats_update_begin(&pstats->syncp); 35761080ab95SNikolay Aleksandrov switch (proto) { 35771080ab95SNikolay Aleksandrov case htons(ETH_P_IP): 35781080ab95SNikolay Aleksandrov pstats->mstats.igmp_parse_errors++; 35791080ab95SNikolay Aleksandrov break; 35801080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 35811080ab95SNikolay Aleksandrov case htons(ETH_P_IPV6): 35821080ab95SNikolay Aleksandrov pstats->mstats.mld_parse_errors++; 35831080ab95SNikolay Aleksandrov break; 35841080ab95SNikolay Aleksandrov #endif 35851080ab95SNikolay Aleksandrov } 35861080ab95SNikolay Aleksandrov u64_stats_update_end(&pstats->syncp); 35871080ab95SNikolay Aleksandrov } 35881080ab95SNikolay Aleksandrov 3589adc47037SNikolay Aleksandrov static void br_multicast_pim(struct net_bridge_mcast *brmctx, 3590adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 359191b02d3dSNikolay Aleksandrov const struct sk_buff *skb) 359291b02d3dSNikolay Aleksandrov { 359391b02d3dSNikolay Aleksandrov unsigned int offset = skb_transport_offset(skb); 359491b02d3dSNikolay Aleksandrov struct pimhdr *pimhdr, _pimhdr; 359591b02d3dSNikolay Aleksandrov 359691b02d3dSNikolay Aleksandrov pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr); 359791b02d3dSNikolay Aleksandrov if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION || 359891b02d3dSNikolay Aleksandrov pim_hdr_type(pimhdr) != PIM_TYPE_HELLO) 359991b02d3dSNikolay Aleksandrov return; 360091b02d3dSNikolay Aleksandrov 3601adc47037SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 3602adc47037SNikolay Aleksandrov br_ip4_multicast_mark_router(brmctx, pmctx); 3603adc47037SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 360491b02d3dSNikolay Aleksandrov } 360591b02d3dSNikolay Aleksandrov 3606adc47037SNikolay Aleksandrov static int br_ip4_multicast_mrd_rcv(struct net_bridge_mcast *brmctx, 3607adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 36084b3087c7SLinus Lüssing struct sk_buff *skb) 36094b3087c7SLinus Lüssing { 36104b3087c7SLinus Lüssing if (ip_hdr(skb)->protocol != IPPROTO_IGMP || 36114b3087c7SLinus Lüssing igmp_hdr(skb)->type != IGMP_MRDISC_ADV) 36124b3087c7SLinus Lüssing return -ENOMSG; 36134b3087c7SLinus Lüssing 3614adc47037SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 3615adc47037SNikolay Aleksandrov br_ip4_multicast_mark_router(brmctx, pmctx); 3616adc47037SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 36174b3087c7SLinus Lüssing 36184b3087c7SLinus Lüssing return 0; 36194b3087c7SLinus Lüssing } 36204b3087c7SLinus Lüssing 3621adc47037SNikolay Aleksandrov static int br_multicast_ipv4_rcv(struct net_bridge_mcast *brmctx, 3622adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 362306499098SVlad Yasevich struct sk_buff *skb, 362406499098SVlad Yasevich u16 vid) 3625eb1d1641SHerbert Xu { 3626adc47037SNikolay Aleksandrov struct net_bridge_port *p = pmctx ? pmctx->port : NULL; 36276db6f0eaSFelix Fietkau const unsigned char *src; 3628eb1d1641SHerbert Xu struct igmphdr *ih; 3629eb1d1641SHerbert Xu int err; 3630eb1d1641SHerbert Xu 3631ba5ea614SLinus Lüssing err = ip_mc_check_igmp(skb); 3632eb1d1641SHerbert Xu 36339afd85c9SLinus Lüssing if (err == -ENOMSG) { 363491b02d3dSNikolay Aleksandrov if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) { 3635bd4265feSHerbert Xu BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 363691b02d3dSNikolay Aleksandrov } else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) { 363791b02d3dSNikolay Aleksandrov if (ip_hdr(skb)->protocol == IPPROTO_PIM) 3638adc47037SNikolay Aleksandrov br_multicast_pim(brmctx, pmctx, skb); 36394b3087c7SLinus Lüssing } else if (ipv4_is_all_snoopers(ip_hdr(skb)->daddr)) { 3640adc47037SNikolay Aleksandrov br_ip4_multicast_mrd_rcv(brmctx, pmctx, skb); 36414b3087c7SLinus Lüssing } 36424b3087c7SLinus Lüssing 3643eb1d1641SHerbert Xu return 0; 36449afd85c9SLinus Lüssing } else if (err < 0) { 3645adc47037SNikolay Aleksandrov br_multicast_err_count(brmctx->br, p, skb->protocol); 36469afd85c9SLinus Lüssing return err; 3647bd4265feSHerbert Xu } 3648eb1d1641SHerbert Xu 36499afd85c9SLinus Lüssing ih = igmp_hdr(skb); 36506db6f0eaSFelix Fietkau src = eth_hdr(skb)->h_source; 36511080ab95SNikolay Aleksandrov BR_INPUT_SKB_CB(skb)->igmp = ih->type; 3652eb1d1641SHerbert Xu 3653eb1d1641SHerbert Xu switch (ih->type) { 3654eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_REPORT: 3655eb1d1641SHerbert Xu case IGMPV2_HOST_MEMBERSHIP_REPORT: 365662b2bcb4SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 3657adc47037SNikolay Aleksandrov err = br_ip4_multicast_add_group(brmctx, pmctx, ih->group, vid, 3658adc47037SNikolay Aleksandrov src, true); 3659eb1d1641SHerbert Xu break; 3660eb1d1641SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 3661adc47037SNikolay Aleksandrov err = br_ip4_multicast_igmp3_report(brmctx, pmctx, skb, vid); 3662eb1d1641SHerbert Xu break; 3663eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_QUERY: 3664adc47037SNikolay Aleksandrov br_ip4_multicast_query(brmctx, pmctx, skb, vid); 3665eb1d1641SHerbert Xu break; 3666eb1d1641SHerbert Xu case IGMP_HOST_LEAVE_MESSAGE: 3667adc47037SNikolay Aleksandrov br_ip4_multicast_leave_group(brmctx, pmctx, ih->group, vid, src); 3668eb1d1641SHerbert Xu break; 3669eb1d1641SHerbert Xu } 3670eb1d1641SHerbert Xu 3671adc47037SNikolay Aleksandrov br_multicast_count(brmctx->br, p, skb, BR_INPUT_SKB_CB(skb)->igmp, 36721080ab95SNikolay Aleksandrov BR_MCAST_DIR_RX); 36731080ab95SNikolay Aleksandrov 3674eb1d1641SHerbert Xu return err; 3675eb1d1641SHerbert Xu } 3676eb1d1641SHerbert Xu 3677dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 3678adc47037SNikolay Aleksandrov static void br_ip6_multicast_mrd_rcv(struct net_bridge_mcast *brmctx, 3679adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 36804b3087c7SLinus Lüssing struct sk_buff *skb) 36814b3087c7SLinus Lüssing { 36824b3087c7SLinus Lüssing if (icmp6_hdr(skb)->icmp6_type != ICMPV6_MRDISC_ADV) 368399014088SLinus Lüssing return; 36844b3087c7SLinus Lüssing 3685adc47037SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 3686adc47037SNikolay Aleksandrov br_ip6_multicast_mark_router(brmctx, pmctx); 3687adc47037SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 36884b3087c7SLinus Lüssing } 36894b3087c7SLinus Lüssing 3690adc47037SNikolay Aleksandrov static int br_multicast_ipv6_rcv(struct net_bridge_mcast *brmctx, 3691adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx, 369206499098SVlad Yasevich struct sk_buff *skb, 369306499098SVlad Yasevich u16 vid) 369408b202b6SYOSHIFUJI Hideaki { 3695adc47037SNikolay Aleksandrov struct net_bridge_port *p = pmctx ? pmctx->port : NULL; 36966db6f0eaSFelix Fietkau const unsigned char *src; 36979afd85c9SLinus Lüssing struct mld_msg *mld; 369808b202b6SYOSHIFUJI Hideaki int err; 369908b202b6SYOSHIFUJI Hideaki 3700ba5ea614SLinus Lüssing err = ipv6_mc_check_mld(skb); 370108b202b6SYOSHIFUJI Hideaki 370299014088SLinus Lüssing if (err == -ENOMSG || err == -ENODATA) { 37039afd85c9SLinus Lüssing if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr)) 37048fad9c39SLinus Lüssing BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 370599014088SLinus Lüssing if (err == -ENODATA && 370699014088SLinus Lüssing ipv6_addr_is_all_snoopers(&ipv6_hdr(skb)->daddr)) 3707adc47037SNikolay Aleksandrov br_ip6_multicast_mrd_rcv(brmctx, pmctx, skb); 37084b3087c7SLinus Lüssing 370908b202b6SYOSHIFUJI Hideaki return 0; 37109afd85c9SLinus Lüssing } else if (err < 0) { 3711adc47037SNikolay Aleksandrov br_multicast_err_count(brmctx->br, p, skb->protocol); 37129afd85c9SLinus Lüssing return err; 371308b202b6SYOSHIFUJI Hideaki } 371408b202b6SYOSHIFUJI Hideaki 37159afd85c9SLinus Lüssing mld = (struct mld_msg *)skb_transport_header(skb); 37161080ab95SNikolay Aleksandrov BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type; 371708b202b6SYOSHIFUJI Hideaki 37189afd85c9SLinus Lüssing switch (mld->mld_type) { 371908b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 37206db6f0eaSFelix Fietkau src = eth_hdr(skb)->h_source; 3721fc2af6c7SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 3722adc47037SNikolay Aleksandrov err = br_ip6_multicast_add_group(brmctx, pmctx, &mld->mld_mca, 3723adc47037SNikolay Aleksandrov vid, src, true); 372408b202b6SYOSHIFUJI Hideaki break; 372508b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 3726adc47037SNikolay Aleksandrov err = br_ip6_multicast_mld2_report(brmctx, pmctx, skb, vid); 372708b202b6SYOSHIFUJI Hideaki break; 372808b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 3729adc47037SNikolay Aleksandrov err = br_ip6_multicast_query(brmctx, pmctx, skb, vid); 373008b202b6SYOSHIFUJI Hideaki break; 373108b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 37326db6f0eaSFelix Fietkau src = eth_hdr(skb)->h_source; 3733adc47037SNikolay Aleksandrov br_ip6_multicast_leave_group(brmctx, pmctx, &mld->mld_mca, vid, 3734adc47037SNikolay Aleksandrov src); 37359afd85c9SLinus Lüssing break; 373608b202b6SYOSHIFUJI Hideaki } 373708b202b6SYOSHIFUJI Hideaki 3738adc47037SNikolay Aleksandrov br_multicast_count(brmctx->br, p, skb, BR_INPUT_SKB_CB(skb)->igmp, 37391080ab95SNikolay Aleksandrov BR_MCAST_DIR_RX); 37401080ab95SNikolay Aleksandrov 374108b202b6SYOSHIFUJI Hideaki return err; 374208b202b6SYOSHIFUJI Hideaki } 374308b202b6SYOSHIFUJI Hideaki #endif 374408b202b6SYOSHIFUJI Hideaki 3745f4b7002aSNikolay Aleksandrov int br_multicast_rcv(struct net_bridge_mcast **brmctx, 3746f4b7002aSNikolay Aleksandrov struct net_bridge_mcast_port **pmctx, 3747f4b7002aSNikolay Aleksandrov struct net_bridge_vlan *vlan, 374806499098SVlad Yasevich struct sk_buff *skb, u16 vid) 3749eb1d1641SHerbert Xu { 37501080ab95SNikolay Aleksandrov int ret = 0; 37511080ab95SNikolay Aleksandrov 37521fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->igmp = 0; 37531fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->mrouters_only = 0; 37541fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 3755f4b7002aSNikolay Aleksandrov if (!br_opt_get((*brmctx)->br, BROPT_MULTICAST_ENABLED)) 3756eb1d1641SHerbert Xu return 0; 3757eb1d1641SHerbert Xu 3758f4b7002aSNikolay Aleksandrov if (br_opt_get((*brmctx)->br, BROPT_MCAST_VLAN_SNOOPING_ENABLED) && vlan) { 3759f4b7002aSNikolay Aleksandrov const struct net_bridge_vlan *masterv; 3760f4b7002aSNikolay Aleksandrov 3761f4b7002aSNikolay Aleksandrov /* the vlan has the master flag set only when transmitting 3762f4b7002aSNikolay Aleksandrov * through the bridge device 3763f4b7002aSNikolay Aleksandrov */ 3764f4b7002aSNikolay Aleksandrov if (br_vlan_is_master(vlan)) { 3765f4b7002aSNikolay Aleksandrov masterv = vlan; 3766f4b7002aSNikolay Aleksandrov *brmctx = &vlan->br_mcast_ctx; 3767f4b7002aSNikolay Aleksandrov *pmctx = NULL; 3768f4b7002aSNikolay Aleksandrov } else { 3769f4b7002aSNikolay Aleksandrov masterv = vlan->brvlan; 3770f4b7002aSNikolay Aleksandrov *brmctx = &vlan->brvlan->br_mcast_ctx; 3771f4b7002aSNikolay Aleksandrov *pmctx = &vlan->port_mcast_ctx; 3772f4b7002aSNikolay Aleksandrov } 3773f4b7002aSNikolay Aleksandrov 3774f4b7002aSNikolay Aleksandrov if (!(masterv->priv_flags & BR_VLFLAG_GLOBAL_MCAST_ENABLED)) 3775f4b7002aSNikolay Aleksandrov return 0; 3776f4b7002aSNikolay Aleksandrov } 3777f4b7002aSNikolay Aleksandrov 3778eb1d1641SHerbert Xu switch (skb->protocol) { 3779eb1d1641SHerbert Xu case htons(ETH_P_IP): 3780f4b7002aSNikolay Aleksandrov ret = br_multicast_ipv4_rcv(*brmctx, *pmctx, skb, vid); 37811080ab95SNikolay Aleksandrov break; 3782dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 378308b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 3784f4b7002aSNikolay Aleksandrov ret = br_multicast_ipv6_rcv(*brmctx, *pmctx, skb, vid); 37851080ab95SNikolay Aleksandrov break; 378608b202b6SYOSHIFUJI Hideaki #endif 3787eb1d1641SHerbert Xu } 3788eb1d1641SHerbert Xu 37891080ab95SNikolay Aleksandrov return ret; 3790eb1d1641SHerbert Xu } 3791eb1d1641SHerbert Xu 3792adc47037SNikolay Aleksandrov static void br_multicast_query_expired(struct net_bridge_mcast *brmctx, 37932cd41431SLinus Lüssing struct bridge_mcast_own_query *query, 37942cd41431SLinus Lüssing struct bridge_mcast_querier *querier) 3795cc0fdd80SLinus Lüssing { 3796adc47037SNikolay Aleksandrov spin_lock(&brmctx->br->multicast_lock); 37974cdd0d10SNikolay Aleksandrov if (br_multicast_ctx_vlan_disabled(brmctx)) 37984cdd0d10SNikolay Aleksandrov goto out; 37994cdd0d10SNikolay Aleksandrov 3800adc47037SNikolay Aleksandrov if (query->startup_sent < brmctx->multicast_startup_query_count) 3801cc0fdd80SLinus Lüssing query->startup_sent++; 3802cc0fdd80SLinus Lüssing 3803adc47037SNikolay Aleksandrov br_multicast_send_query(brmctx, NULL, query); 38044cdd0d10SNikolay Aleksandrov out: 3805adc47037SNikolay Aleksandrov spin_unlock(&brmctx->br->multicast_lock); 3806cc0fdd80SLinus Lüssing } 3807cc0fdd80SLinus Lüssing 380888c1f37fSAllen Pais static void br_ip4_multicast_query_expired(struct timer_list *t) 3809eb1d1641SHerbert Xu { 3810d3d065c0SNikolay Aleksandrov struct net_bridge_mcast *brmctx = from_timer(brmctx, t, 3811d3d065c0SNikolay Aleksandrov ip4_own_query.timer); 3812eb1d1641SHerbert Xu 3813adc47037SNikolay Aleksandrov br_multicast_query_expired(brmctx, &brmctx->ip4_own_query, 3814d3d065c0SNikolay Aleksandrov &brmctx->ip4_querier); 3815eb1d1641SHerbert Xu } 3816eb1d1641SHerbert Xu 3817cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 381888c1f37fSAllen Pais static void br_ip6_multicast_query_expired(struct timer_list *t) 3819cc0fdd80SLinus Lüssing { 3820d3d065c0SNikolay Aleksandrov struct net_bridge_mcast *brmctx = from_timer(brmctx, t, 3821d3d065c0SNikolay Aleksandrov ip6_own_query.timer); 3822cc0fdd80SLinus Lüssing 3823adc47037SNikolay Aleksandrov br_multicast_query_expired(brmctx, &brmctx->ip6_own_query, 3824d3d065c0SNikolay Aleksandrov &brmctx->ip6_querier); 3825cc0fdd80SLinus Lüssing } 3826cc0fdd80SLinus Lüssing #endif 3827cc0fdd80SLinus Lüssing 3828e12cec65SNikolay Aleksandrov static void br_multicast_gc_work(struct work_struct *work) 38298b671779SNikolay Aleksandrov { 38308b671779SNikolay Aleksandrov struct net_bridge *br = container_of(work, struct net_bridge, 3831e12cec65SNikolay Aleksandrov mcast_gc_work); 38328b671779SNikolay Aleksandrov HLIST_HEAD(deleted_head); 38338b671779SNikolay Aleksandrov 38348b671779SNikolay Aleksandrov spin_lock_bh(&br->multicast_lock); 3835e12cec65SNikolay Aleksandrov hlist_move_list(&br->mcast_gc_list, &deleted_head); 38368b671779SNikolay Aleksandrov spin_unlock_bh(&br->multicast_lock); 38378b671779SNikolay Aleksandrov 3838e12cec65SNikolay Aleksandrov br_multicast_gc(&deleted_head); 38398b671779SNikolay Aleksandrov } 38408b671779SNikolay Aleksandrov 3841613d61dbSNikolay Aleksandrov void br_multicast_ctx_init(struct net_bridge *br, 3842613d61dbSNikolay Aleksandrov struct net_bridge_vlan *vlan, 3843613d61dbSNikolay Aleksandrov struct net_bridge_mcast *brmctx) 3844613d61dbSNikolay Aleksandrov { 3845613d61dbSNikolay Aleksandrov brmctx->br = br; 3846613d61dbSNikolay Aleksandrov brmctx->vlan = vlan; 3847613d61dbSNikolay Aleksandrov brmctx->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 3848613d61dbSNikolay Aleksandrov brmctx->multicast_last_member_count = 2; 3849613d61dbSNikolay Aleksandrov brmctx->multicast_startup_query_count = 2; 3850613d61dbSNikolay Aleksandrov 3851613d61dbSNikolay Aleksandrov brmctx->multicast_last_member_interval = HZ; 3852613d61dbSNikolay Aleksandrov brmctx->multicast_query_response_interval = 10 * HZ; 3853613d61dbSNikolay Aleksandrov brmctx->multicast_startup_query_interval = 125 * HZ / 4; 3854613d61dbSNikolay Aleksandrov brmctx->multicast_query_interval = 125 * HZ; 3855613d61dbSNikolay Aleksandrov brmctx->multicast_querier_interval = 255 * HZ; 3856613d61dbSNikolay Aleksandrov brmctx->multicast_membership_interval = 260 * HZ; 3857613d61dbSNikolay Aleksandrov 3858613d61dbSNikolay Aleksandrov brmctx->ip4_other_query.delay_time = 0; 3859bb18ef8eSNikolay Aleksandrov brmctx->ip4_querier.port_ifidx = 0; 386067b746f9SNikolay Aleksandrov seqcount_init(&brmctx->ip4_querier.seq); 3861613d61dbSNikolay Aleksandrov brmctx->multicast_igmp_version = 2; 3862613d61dbSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 3863613d61dbSNikolay Aleksandrov brmctx->multicast_mld_version = 1; 3864613d61dbSNikolay Aleksandrov brmctx->ip6_other_query.delay_time = 0; 3865bb18ef8eSNikolay Aleksandrov brmctx->ip6_querier.port_ifidx = 0; 386667b746f9SNikolay Aleksandrov seqcount_init(&brmctx->ip6_querier.seq); 3867613d61dbSNikolay Aleksandrov #endif 3868613d61dbSNikolay Aleksandrov 3869613d61dbSNikolay Aleksandrov timer_setup(&brmctx->ip4_mc_router_timer, 3870613d61dbSNikolay Aleksandrov br_ip4_multicast_local_router_expired, 0); 3871613d61dbSNikolay Aleksandrov timer_setup(&brmctx->ip4_other_query.timer, 3872613d61dbSNikolay Aleksandrov br_ip4_multicast_querier_expired, 0); 3873613d61dbSNikolay Aleksandrov timer_setup(&brmctx->ip4_own_query.timer, 3874613d61dbSNikolay Aleksandrov br_ip4_multicast_query_expired, 0); 3875613d61dbSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 3876613d61dbSNikolay Aleksandrov timer_setup(&brmctx->ip6_mc_router_timer, 3877613d61dbSNikolay Aleksandrov br_ip6_multicast_local_router_expired, 0); 3878613d61dbSNikolay Aleksandrov timer_setup(&brmctx->ip6_other_query.timer, 3879613d61dbSNikolay Aleksandrov br_ip6_multicast_querier_expired, 0); 3880613d61dbSNikolay Aleksandrov timer_setup(&brmctx->ip6_own_query.timer, 3881613d61dbSNikolay Aleksandrov br_ip6_multicast_query_expired, 0); 3882613d61dbSNikolay Aleksandrov #endif 3883613d61dbSNikolay Aleksandrov } 3884613d61dbSNikolay Aleksandrov 3885613d61dbSNikolay Aleksandrov void br_multicast_ctx_deinit(struct net_bridge_mcast *brmctx) 3886613d61dbSNikolay Aleksandrov { 3887613d61dbSNikolay Aleksandrov __br_multicast_stop(brmctx); 3888613d61dbSNikolay Aleksandrov } 3889613d61dbSNikolay Aleksandrov 3890eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br) 3891eb1d1641SHerbert Xu { 3892d08c6bc0SNikolay Aleksandrov br->hash_max = BR_MULTICAST_DEFAULT_HASH_MAX; 3893eb1d1641SHerbert Xu 3894613d61dbSNikolay Aleksandrov br_multicast_ctx_init(br, NULL, &br->multicast_ctx); 3895eb1d1641SHerbert Xu 38966919622aSIdo Schimmel br_opt_toggle(br, BROPT_MULTICAST_ENABLED, true); 3897675779adSNikolay Aleksandrov br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true); 3898b00589afSLinus Lüssing 3899eb1d1641SHerbert Xu spin_lock_init(&br->multicast_lock); 390019e3a9c9SNikolay Aleksandrov INIT_HLIST_HEAD(&br->mdb_list); 3901e12cec65SNikolay Aleksandrov INIT_HLIST_HEAD(&br->mcast_gc_list); 3902e12cec65SNikolay Aleksandrov INIT_WORK(&br->mcast_gc_work, br_multicast_gc_work); 3903eb1d1641SHerbert Xu } 3904eb1d1641SHerbert Xu 39054effd28cSLinus Lüssing static void br_ip4_multicast_join_snoopers(struct net_bridge *br) 39064effd28cSLinus Lüssing { 39074effd28cSLinus Lüssing struct in_device *in_dev = in_dev_get(br->dev); 39084effd28cSLinus Lüssing 39094effd28cSLinus Lüssing if (!in_dev) 39104effd28cSLinus Lüssing return; 39114effd28cSLinus Lüssing 39129fb20801SFlorian Fainelli __ip_mc_inc_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC); 39134effd28cSLinus Lüssing in_dev_put(in_dev); 39144effd28cSLinus Lüssing } 39154effd28cSLinus Lüssing 39164effd28cSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 39174effd28cSLinus Lüssing static void br_ip6_multicast_join_snoopers(struct net_bridge *br) 39184effd28cSLinus Lüssing { 39194effd28cSLinus Lüssing struct in6_addr addr; 39204effd28cSLinus Lüssing 39214effd28cSLinus Lüssing ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a)); 39224effd28cSLinus Lüssing ipv6_dev_mc_inc(br->dev, &addr); 39234effd28cSLinus Lüssing } 39244effd28cSLinus Lüssing #else 39254effd28cSLinus Lüssing static inline void br_ip6_multicast_join_snoopers(struct net_bridge *br) 39264effd28cSLinus Lüssing { 39274effd28cSLinus Lüssing } 39284effd28cSLinus Lüssing #endif 39294effd28cSLinus Lüssing 3930851d0a73SJoseph Huang void br_multicast_join_snoopers(struct net_bridge *br) 39314effd28cSLinus Lüssing { 39324effd28cSLinus Lüssing br_ip4_multicast_join_snoopers(br); 39334effd28cSLinus Lüssing br_ip6_multicast_join_snoopers(br); 39344effd28cSLinus Lüssing } 39354effd28cSLinus Lüssing 39364effd28cSLinus Lüssing static void br_ip4_multicast_leave_snoopers(struct net_bridge *br) 39374effd28cSLinus Lüssing { 39384effd28cSLinus Lüssing struct in_device *in_dev = in_dev_get(br->dev); 39394effd28cSLinus Lüssing 39404effd28cSLinus Lüssing if (WARN_ON(!in_dev)) 39414effd28cSLinus Lüssing return; 39424effd28cSLinus Lüssing 39439fb20801SFlorian Fainelli __ip_mc_dec_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC); 39444effd28cSLinus Lüssing in_dev_put(in_dev); 39454effd28cSLinus Lüssing } 39464effd28cSLinus Lüssing 39474effd28cSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 39484effd28cSLinus Lüssing static void br_ip6_multicast_leave_snoopers(struct net_bridge *br) 39494effd28cSLinus Lüssing { 39504effd28cSLinus Lüssing struct in6_addr addr; 39514effd28cSLinus Lüssing 39524effd28cSLinus Lüssing ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a)); 39534effd28cSLinus Lüssing ipv6_dev_mc_dec(br->dev, &addr); 39544effd28cSLinus Lüssing } 39554effd28cSLinus Lüssing #else 39564effd28cSLinus Lüssing static inline void br_ip6_multicast_leave_snoopers(struct net_bridge *br) 39574effd28cSLinus Lüssing { 39584effd28cSLinus Lüssing } 39594effd28cSLinus Lüssing #endif 39604effd28cSLinus Lüssing 3961851d0a73SJoseph Huang void br_multicast_leave_snoopers(struct net_bridge *br) 39624effd28cSLinus Lüssing { 39634effd28cSLinus Lüssing br_ip4_multicast_leave_snoopers(br); 39644effd28cSLinus Lüssing br_ip6_multicast_leave_snoopers(br); 39654effd28cSLinus Lüssing } 39664effd28cSLinus Lüssing 39677b54aaafSNikolay Aleksandrov static void __br_multicast_open_query(struct net_bridge *br, 396890010b36SLinus Lüssing struct bridge_mcast_own_query *query) 3969eb1d1641SHerbert Xu { 3970cc0fdd80SLinus Lüssing query->startup_sent = 0; 3971eb1d1641SHerbert Xu 397213cefad2SNikolay Aleksandrov if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) 3973eb1d1641SHerbert Xu return; 3974eb1d1641SHerbert Xu 3975cc0fdd80SLinus Lüssing mod_timer(&query->timer, jiffies); 3976cc0fdd80SLinus Lüssing } 3977cc0fdd80SLinus Lüssing 39787b54aaafSNikolay Aleksandrov static void __br_multicast_open(struct net_bridge_mcast *brmctx) 39797b54aaafSNikolay Aleksandrov { 39807b54aaafSNikolay Aleksandrov __br_multicast_open_query(brmctx->br, &brmctx->ip4_own_query); 39817b54aaafSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 39827b54aaafSNikolay Aleksandrov __br_multicast_open_query(brmctx->br, &brmctx->ip6_own_query); 39837b54aaafSNikolay Aleksandrov #endif 39847b54aaafSNikolay Aleksandrov } 39857b54aaafSNikolay Aleksandrov 3986cc0fdd80SLinus Lüssing void br_multicast_open(struct net_bridge *br) 3987cc0fdd80SLinus Lüssing { 3988f4b7002aSNikolay Aleksandrov ASSERT_RTNL(); 3989f4b7002aSNikolay Aleksandrov 3990f4b7002aSNikolay Aleksandrov if (br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED)) { 39917b54aaafSNikolay Aleksandrov struct net_bridge_vlan_group *vg; 39927b54aaafSNikolay Aleksandrov struct net_bridge_vlan *vlan; 39937b54aaafSNikolay Aleksandrov 39947b54aaafSNikolay Aleksandrov vg = br_vlan_group(br); 39957b54aaafSNikolay Aleksandrov if (vg) { 39967b54aaafSNikolay Aleksandrov list_for_each_entry(vlan, &vg->vlan_list, vlist) { 39977b54aaafSNikolay Aleksandrov struct net_bridge_mcast *brmctx; 39987b54aaafSNikolay Aleksandrov 39997b54aaafSNikolay Aleksandrov brmctx = &vlan->br_mcast_ctx; 40007b54aaafSNikolay Aleksandrov if (br_vlan_is_brentry(vlan) && 40017b54aaafSNikolay Aleksandrov !br_multicast_ctx_vlan_disabled(brmctx)) 40027b54aaafSNikolay Aleksandrov __br_multicast_open(&vlan->br_mcast_ctx); 40037b54aaafSNikolay Aleksandrov } 40047b54aaafSNikolay Aleksandrov } 4005cb486ce9SNikolay Aleksandrov } else { 40067b54aaafSNikolay Aleksandrov __br_multicast_open(&br->multicast_ctx); 4007eb1d1641SHerbert Xu } 4008cb486ce9SNikolay Aleksandrov } 4009eb1d1641SHerbert Xu 4010613d61dbSNikolay Aleksandrov static void __br_multicast_stop(struct net_bridge_mcast *brmctx) 4011613d61dbSNikolay Aleksandrov { 4012613d61dbSNikolay Aleksandrov del_timer_sync(&brmctx->ip4_mc_router_timer); 4013613d61dbSNikolay Aleksandrov del_timer_sync(&brmctx->ip4_other_query.timer); 4014613d61dbSNikolay Aleksandrov del_timer_sync(&brmctx->ip4_own_query.timer); 4015613d61dbSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 4016613d61dbSNikolay Aleksandrov del_timer_sync(&brmctx->ip6_mc_router_timer); 4017613d61dbSNikolay Aleksandrov del_timer_sync(&brmctx->ip6_other_query.timer); 4018613d61dbSNikolay Aleksandrov del_timer_sync(&brmctx->ip6_own_query.timer); 4019613d61dbSNikolay Aleksandrov #endif 4020613d61dbSNikolay Aleksandrov } 4021613d61dbSNikolay Aleksandrov 40227b54aaafSNikolay Aleksandrov void br_multicast_toggle_one_vlan(struct net_bridge_vlan *vlan, bool on) 40237b54aaafSNikolay Aleksandrov { 40247b54aaafSNikolay Aleksandrov struct net_bridge *br; 40257b54aaafSNikolay Aleksandrov 40267b54aaafSNikolay Aleksandrov /* it's okay to check for the flag without the multicast lock because it 40277b54aaafSNikolay Aleksandrov * can only change under RTNL -> multicast_lock, we need the latter to 40287b54aaafSNikolay Aleksandrov * sync with timers and packets 40297b54aaafSNikolay Aleksandrov */ 40307b54aaafSNikolay Aleksandrov if (on == !!(vlan->priv_flags & BR_VLFLAG_MCAST_ENABLED)) 40317b54aaafSNikolay Aleksandrov return; 40327b54aaafSNikolay Aleksandrov 40337b54aaafSNikolay Aleksandrov if (br_vlan_is_master(vlan)) { 40347b54aaafSNikolay Aleksandrov br = vlan->br; 40357b54aaafSNikolay Aleksandrov 40367b54aaafSNikolay Aleksandrov if (!br_vlan_is_brentry(vlan) || 40377b54aaafSNikolay Aleksandrov (on && 40387b54aaafSNikolay Aleksandrov br_multicast_ctx_vlan_global_disabled(&vlan->br_mcast_ctx))) 40397b54aaafSNikolay Aleksandrov return; 40407b54aaafSNikolay Aleksandrov 40417b54aaafSNikolay Aleksandrov spin_lock_bh(&br->multicast_lock); 40427b54aaafSNikolay Aleksandrov vlan->priv_flags ^= BR_VLFLAG_MCAST_ENABLED; 40437b54aaafSNikolay Aleksandrov spin_unlock_bh(&br->multicast_lock); 40447b54aaafSNikolay Aleksandrov 40457b54aaafSNikolay Aleksandrov if (on) 40467b54aaafSNikolay Aleksandrov __br_multicast_open(&vlan->br_mcast_ctx); 40477b54aaafSNikolay Aleksandrov else 40487b54aaafSNikolay Aleksandrov __br_multicast_stop(&vlan->br_mcast_ctx); 40497b54aaafSNikolay Aleksandrov } else { 40507b54aaafSNikolay Aleksandrov struct net_bridge_mcast *brmctx; 40517b54aaafSNikolay Aleksandrov 40527b54aaafSNikolay Aleksandrov brmctx = br_multicast_port_ctx_get_global(&vlan->port_mcast_ctx); 40537b54aaafSNikolay Aleksandrov if (on && br_multicast_ctx_vlan_global_disabled(brmctx)) 40547b54aaafSNikolay Aleksandrov return; 40557b54aaafSNikolay Aleksandrov 40567b54aaafSNikolay Aleksandrov br = vlan->port->br; 40577b54aaafSNikolay Aleksandrov spin_lock_bh(&br->multicast_lock); 40587b54aaafSNikolay Aleksandrov vlan->priv_flags ^= BR_VLFLAG_MCAST_ENABLED; 40597b54aaafSNikolay Aleksandrov if (on) 40607b54aaafSNikolay Aleksandrov __br_multicast_enable_port_ctx(&vlan->port_mcast_ctx); 40617b54aaafSNikolay Aleksandrov else 40627b54aaafSNikolay Aleksandrov __br_multicast_disable_port_ctx(&vlan->port_mcast_ctx); 40637b54aaafSNikolay Aleksandrov spin_unlock_bh(&br->multicast_lock); 40647b54aaafSNikolay Aleksandrov } 40657b54aaafSNikolay Aleksandrov } 40667b54aaafSNikolay Aleksandrov 4067f4b7002aSNikolay Aleksandrov void br_multicast_toggle_vlan(struct net_bridge_vlan *vlan, bool on) 4068f4b7002aSNikolay Aleksandrov { 4069f4b7002aSNikolay Aleksandrov struct net_bridge_port *p; 4070f4b7002aSNikolay Aleksandrov 4071f4b7002aSNikolay Aleksandrov if (WARN_ON_ONCE(!br_vlan_is_master(vlan))) 4072f4b7002aSNikolay Aleksandrov return; 4073f4b7002aSNikolay Aleksandrov 4074f4b7002aSNikolay Aleksandrov list_for_each_entry(p, &vlan->br->port_list, list) { 4075f4b7002aSNikolay Aleksandrov struct net_bridge_vlan *vport; 4076f4b7002aSNikolay Aleksandrov 4077f4b7002aSNikolay Aleksandrov vport = br_vlan_find(nbp_vlan_group(p), vlan->vid); 4078f4b7002aSNikolay Aleksandrov if (!vport) 4079f4b7002aSNikolay Aleksandrov continue; 4080f4b7002aSNikolay Aleksandrov br_multicast_toggle_one_vlan(vport, on); 4081f4b7002aSNikolay Aleksandrov } 4082f4b7002aSNikolay Aleksandrov } 4083f4b7002aSNikolay Aleksandrov 4084f4b7002aSNikolay Aleksandrov int br_multicast_toggle_vlan_snooping(struct net_bridge *br, bool on, 4085f4b7002aSNikolay Aleksandrov struct netlink_ext_ack *extack) 4086eb1d1641SHerbert Xu { 40877b54aaafSNikolay Aleksandrov struct net_bridge_vlan_group *vg; 40887b54aaafSNikolay Aleksandrov struct net_bridge_vlan *vlan; 4089f4b7002aSNikolay Aleksandrov struct net_bridge_port *p; 40907b54aaafSNikolay Aleksandrov 4091f4b7002aSNikolay Aleksandrov if (br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED) == on) 4092f4b7002aSNikolay Aleksandrov return 0; 4093f4b7002aSNikolay Aleksandrov 4094f4b7002aSNikolay Aleksandrov if (on && !br_opt_get(br, BROPT_VLAN_ENABLED)) { 4095f4b7002aSNikolay Aleksandrov NL_SET_ERR_MSG_MOD(extack, "Cannot enable multicast vlan snooping with vlan filtering disabled"); 4096f4b7002aSNikolay Aleksandrov return -EINVAL; 4097f4b7002aSNikolay Aleksandrov } 4098f4b7002aSNikolay Aleksandrov 4099f4b7002aSNikolay Aleksandrov vg = br_vlan_group(br); 4100f4b7002aSNikolay Aleksandrov if (!vg) 4101f4b7002aSNikolay Aleksandrov return 0; 4102f4b7002aSNikolay Aleksandrov 4103f4b7002aSNikolay Aleksandrov br_opt_toggle(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED, on); 4104f4b7002aSNikolay Aleksandrov 4105f4b7002aSNikolay Aleksandrov /* disable/enable non-vlan mcast contexts based on vlan snooping */ 4106f4b7002aSNikolay Aleksandrov if (on) 4107f4b7002aSNikolay Aleksandrov __br_multicast_stop(&br->multicast_ctx); 4108f4b7002aSNikolay Aleksandrov else 4109f4b7002aSNikolay Aleksandrov __br_multicast_open(&br->multicast_ctx); 4110f4b7002aSNikolay Aleksandrov list_for_each_entry(p, &br->port_list, list) { 4111f4b7002aSNikolay Aleksandrov if (on) 4112f4b7002aSNikolay Aleksandrov br_multicast_disable_port(p); 4113f4b7002aSNikolay Aleksandrov else 4114f4b7002aSNikolay Aleksandrov br_multicast_enable_port(p); 4115f4b7002aSNikolay Aleksandrov } 4116f4b7002aSNikolay Aleksandrov 4117f4b7002aSNikolay Aleksandrov list_for_each_entry(vlan, &vg->vlan_list, vlist) 4118f4b7002aSNikolay Aleksandrov br_multicast_toggle_vlan(vlan, on); 4119f4b7002aSNikolay Aleksandrov 4120f4b7002aSNikolay Aleksandrov return 0; 4121f4b7002aSNikolay Aleksandrov } 4122f4b7002aSNikolay Aleksandrov 41239dee572cSNikolay Aleksandrov bool br_multicast_toggle_global_vlan(struct net_bridge_vlan *vlan, bool on) 41249dee572cSNikolay Aleksandrov { 41259dee572cSNikolay Aleksandrov ASSERT_RTNL(); 41269dee572cSNikolay Aleksandrov 41279dee572cSNikolay Aleksandrov /* BR_VLFLAG_GLOBAL_MCAST_ENABLED relies on eventual consistency and 41289dee572cSNikolay Aleksandrov * requires only RTNL to change 41299dee572cSNikolay Aleksandrov */ 41309dee572cSNikolay Aleksandrov if (on == !!(vlan->priv_flags & BR_VLFLAG_GLOBAL_MCAST_ENABLED)) 41319dee572cSNikolay Aleksandrov return false; 41329dee572cSNikolay Aleksandrov 41339dee572cSNikolay Aleksandrov vlan->priv_flags ^= BR_VLFLAG_GLOBAL_MCAST_ENABLED; 41349dee572cSNikolay Aleksandrov br_multicast_toggle_vlan(vlan, on); 41359dee572cSNikolay Aleksandrov 41369dee572cSNikolay Aleksandrov return true; 41379dee572cSNikolay Aleksandrov } 41389dee572cSNikolay Aleksandrov 4139f4b7002aSNikolay Aleksandrov void br_multicast_stop(struct net_bridge *br) 4140f4b7002aSNikolay Aleksandrov { 41417b54aaafSNikolay Aleksandrov ASSERT_RTNL(); 41427b54aaafSNikolay Aleksandrov 4143f4b7002aSNikolay Aleksandrov if (br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED)) { 4144f4b7002aSNikolay Aleksandrov struct net_bridge_vlan_group *vg; 4145f4b7002aSNikolay Aleksandrov struct net_bridge_vlan *vlan; 4146f4b7002aSNikolay Aleksandrov 41477b54aaafSNikolay Aleksandrov vg = br_vlan_group(br); 41487b54aaafSNikolay Aleksandrov if (vg) { 41497b54aaafSNikolay Aleksandrov list_for_each_entry(vlan, &vg->vlan_list, vlist) { 41507b54aaafSNikolay Aleksandrov struct net_bridge_mcast *brmctx; 41517b54aaafSNikolay Aleksandrov 41527b54aaafSNikolay Aleksandrov brmctx = &vlan->br_mcast_ctx; 41537b54aaafSNikolay Aleksandrov if (br_vlan_is_brentry(vlan) && 41547b54aaafSNikolay Aleksandrov !br_multicast_ctx_vlan_disabled(brmctx)) 41557b54aaafSNikolay Aleksandrov __br_multicast_stop(&vlan->br_mcast_ctx); 41567b54aaafSNikolay Aleksandrov } 41577b54aaafSNikolay Aleksandrov } 4158cb486ce9SNikolay Aleksandrov } else { 4159613d61dbSNikolay Aleksandrov __br_multicast_stop(&br->multicast_ctx); 4160e10177abSSatish Ashok } 4161cb486ce9SNikolay Aleksandrov } 4162e10177abSSatish Ashok 4163e10177abSSatish Ashok void br_multicast_dev_del(struct net_bridge *br) 4164e10177abSSatish Ashok { 4165e10177abSSatish Ashok struct net_bridge_mdb_entry *mp; 41668b671779SNikolay Aleksandrov HLIST_HEAD(deleted_head); 416719e3a9c9SNikolay Aleksandrov struct hlist_node *tmp; 4168eb1d1641SHerbert Xu 4169eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 4170e12cec65SNikolay Aleksandrov hlist_for_each_entry_safe(mp, tmp, &br->mdb_list, mdb_node) 4171e12cec65SNikolay Aleksandrov br_multicast_del_mdb_entry(mp); 4172e12cec65SNikolay Aleksandrov hlist_move_list(&br->mcast_gc_list, &deleted_head); 4173eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 417419e3a9c9SNikolay Aleksandrov 4175613d61dbSNikolay Aleksandrov br_multicast_ctx_deinit(&br->multicast_ctx); 4176e12cec65SNikolay Aleksandrov br_multicast_gc(&deleted_head); 4177e12cec65SNikolay Aleksandrov cancel_work_sync(&br->mcast_gc_work); 41788b671779SNikolay Aleksandrov 41794329596cSNikolay Aleksandrov rcu_barrier(); 4180eb1d1641SHerbert Xu } 41810909e117SHerbert Xu 4182a97df080SNikolay Aleksandrov int br_multicast_set_router(struct net_bridge_mcast *brmctx, unsigned long val) 41830909e117SHerbert Xu { 41846ae4ae8eSLinus Lüssing int err = -EINVAL; 41850909e117SHerbert Xu 4186a97df080SNikolay Aleksandrov spin_lock_bh(&brmctx->br->multicast_lock); 41870909e117SHerbert Xu 41880909e117SHerbert Xu switch (val) { 41897f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_DISABLED: 41907f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_PERM: 4191a97df080SNikolay Aleksandrov br_mc_router_state_change(brmctx->br, val == MDB_RTR_TYPE_PERM); 4192d3d065c0SNikolay Aleksandrov del_timer(&brmctx->ip4_mc_router_timer); 4193a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 4194d3d065c0SNikolay Aleksandrov del_timer(&brmctx->ip6_mc_router_timer); 4195a3c02e76SLinus Lüssing #endif 4196d3d065c0SNikolay Aleksandrov brmctx->multicast_router = val; 419777041420SYotam Gigi err = 0; 419877041420SYotam Gigi break; 41997f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_TEMP_QUERY: 4200d3d065c0SNikolay Aleksandrov if (brmctx->multicast_router != MDB_RTR_TYPE_TEMP_QUERY) 4201a97df080SNikolay Aleksandrov br_mc_router_state_change(brmctx->br, false); 4202d3d065c0SNikolay Aleksandrov brmctx->multicast_router = val; 42030909e117SHerbert Xu err = 0; 42040909e117SHerbert Xu break; 42050909e117SHerbert Xu } 42060909e117SHerbert Xu 4207a97df080SNikolay Aleksandrov spin_unlock_bh(&brmctx->br->multicast_lock); 42080909e117SHerbert Xu 42090909e117SHerbert Xu return err; 42100909e117SHerbert Xu } 42110909e117SHerbert Xu 4212ed2d3597SLinus Lüssing static void 4213adc47037SNikolay Aleksandrov br_multicast_rport_del_notify(struct net_bridge_mcast_port *pmctx, bool deleted) 42147f0aec7aSNikolay Aleksandrov { 4215ed2d3597SLinus Lüssing if (!deleted) 42167f0aec7aSNikolay Aleksandrov return; 4217ed2d3597SLinus Lüssing 4218a3c02e76SLinus Lüssing /* For backwards compatibility for now, only notify if there is 4219a3c02e76SLinus Lüssing * no multicast router anymore for both IPv4 and IPv6. 4220a3c02e76SLinus Lüssing */ 4221adc47037SNikolay Aleksandrov if (!hlist_unhashed(&pmctx->ip4_rlist)) 4222a3c02e76SLinus Lüssing return; 4223a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 4224adc47037SNikolay Aleksandrov if (!hlist_unhashed(&pmctx->ip6_rlist)) 4225a3c02e76SLinus Lüssing return; 4226a3c02e76SLinus Lüssing #endif 4227a3c02e76SLinus Lüssing 42281e9ca456SNikolay Aleksandrov br_rtr_notify(pmctx->port->br->dev, pmctx, RTM_DELMDB); 4229adc47037SNikolay Aleksandrov br_port_mc_router_state_change(pmctx->port, false); 4230f12e7d95SNogah Frankel 4231f12e7d95SNogah Frankel /* don't allow timer refresh */ 4232adc47037SNikolay Aleksandrov if (pmctx->multicast_router == MDB_RTR_TYPE_TEMP) 4233adc47037SNikolay Aleksandrov pmctx->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 42347f0aec7aSNikolay Aleksandrov } 42357f0aec7aSNikolay Aleksandrov 42360909e117SHerbert Xu int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val) 42370909e117SHerbert Xu { 4238d3d065c0SNikolay Aleksandrov struct net_bridge_mcast *brmctx = &p->br->multicast_ctx; 4239adc47037SNikolay Aleksandrov struct net_bridge_mcast_port *pmctx = &p->multicast_ctx; 4240a55d8246SNikolay Aleksandrov unsigned long now = jiffies; 42416ae4ae8eSLinus Lüssing int err = -EINVAL; 4242ed2d3597SLinus Lüssing bool del = false; 42430909e117SHerbert Xu 4244d3d065c0SNikolay Aleksandrov spin_lock(&p->br->multicast_lock); 4245adc47037SNikolay Aleksandrov if (pmctx->multicast_router == val) { 4246a55d8246SNikolay Aleksandrov /* Refresh the temp router port timer */ 4247adc47037SNikolay Aleksandrov if (pmctx->multicast_router == MDB_RTR_TYPE_TEMP) { 4248adc47037SNikolay Aleksandrov mod_timer(&pmctx->ip4_mc_router_timer, 4249d3d065c0SNikolay Aleksandrov now + brmctx->multicast_querier_interval); 4250a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 4251adc47037SNikolay Aleksandrov mod_timer(&pmctx->ip6_mc_router_timer, 4252d3d065c0SNikolay Aleksandrov now + brmctx->multicast_querier_interval); 4253a3c02e76SLinus Lüssing #endif 4254a3c02e76SLinus Lüssing } 42554950cfd1SNikolay Aleksandrov err = 0; 42564950cfd1SNikolay Aleksandrov goto unlock; 42574950cfd1SNikolay Aleksandrov } 42580909e117SHerbert Xu switch (val) { 42597f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_DISABLED: 4260adc47037SNikolay Aleksandrov pmctx->multicast_router = MDB_RTR_TYPE_DISABLED; 4261adc47037SNikolay Aleksandrov del |= br_ip4_multicast_rport_del(pmctx); 4262adc47037SNikolay Aleksandrov del_timer(&pmctx->ip4_mc_router_timer); 4263adc47037SNikolay Aleksandrov del |= br_ip6_multicast_rport_del(pmctx); 4264a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 4265adc47037SNikolay Aleksandrov del_timer(&pmctx->ip6_mc_router_timer); 4266a3c02e76SLinus Lüssing #endif 4267adc47037SNikolay Aleksandrov br_multicast_rport_del_notify(pmctx, del); 42680909e117SHerbert Xu break; 42697f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_TEMP_QUERY: 4270adc47037SNikolay Aleksandrov pmctx->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 4271adc47037SNikolay Aleksandrov del |= br_ip4_multicast_rport_del(pmctx); 4272adc47037SNikolay Aleksandrov del |= br_ip6_multicast_rport_del(pmctx); 4273adc47037SNikolay Aleksandrov br_multicast_rport_del_notify(pmctx, del); 42747f0aec7aSNikolay Aleksandrov break; 42757f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_PERM: 4276adc47037SNikolay Aleksandrov pmctx->multicast_router = MDB_RTR_TYPE_PERM; 4277adc47037SNikolay Aleksandrov del_timer(&pmctx->ip4_mc_router_timer); 4278adc47037SNikolay Aleksandrov br_ip4_multicast_add_router(brmctx, pmctx); 4279a3c02e76SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 4280adc47037SNikolay Aleksandrov del_timer(&pmctx->ip6_mc_router_timer); 4281a3c02e76SLinus Lüssing #endif 4282adc47037SNikolay Aleksandrov br_ip6_multicast_add_router(brmctx, pmctx); 42830909e117SHerbert Xu break; 4284a55d8246SNikolay Aleksandrov case MDB_RTR_TYPE_TEMP: 4285adc47037SNikolay Aleksandrov pmctx->multicast_router = MDB_RTR_TYPE_TEMP; 4286adc47037SNikolay Aleksandrov br_ip4_multicast_mark_router(brmctx, pmctx); 4287adc47037SNikolay Aleksandrov br_ip6_multicast_mark_router(brmctx, pmctx); 4288a55d8246SNikolay Aleksandrov break; 42897f0aec7aSNikolay Aleksandrov default: 42907f0aec7aSNikolay Aleksandrov goto unlock; 42910909e117SHerbert Xu } 42927f0aec7aSNikolay Aleksandrov err = 0; 42937f0aec7aSNikolay Aleksandrov unlock: 4294d3d065c0SNikolay Aleksandrov spin_unlock(&p->br->multicast_lock); 42950909e117SHerbert Xu 42960909e117SHerbert Xu return err; 42970909e117SHerbert Xu } 4298561f1103SHerbert Xu 4299adc47037SNikolay Aleksandrov static void br_multicast_start_querier(struct net_bridge_mcast *brmctx, 430090010b36SLinus Lüssing struct bridge_mcast_own_query *query) 4301561f1103SHerbert Xu { 4302561f1103SHerbert Xu struct net_bridge_port *port; 430374857216SHerbert Xu 4304cb486ce9SNikolay Aleksandrov if (!br_multicast_ctx_matches_vlan_snooping(brmctx)) 4305cb486ce9SNikolay Aleksandrov return; 4306cb486ce9SNikolay Aleksandrov 43077b54aaafSNikolay Aleksandrov __br_multicast_open_query(brmctx->br, query); 430874857216SHerbert Xu 4309c5b493ceSNikolay Aleksandrov rcu_read_lock(); 4310adc47037SNikolay Aleksandrov list_for_each_entry_rcu(port, &brmctx->br->port_list, list) { 4311615cc23eSNikolay Aleksandrov struct bridge_mcast_own_query *ip4_own_query; 4312615cc23eSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 4313615cc23eSNikolay Aleksandrov struct bridge_mcast_own_query *ip6_own_query; 4314615cc23eSNikolay Aleksandrov #endif 4315615cc23eSNikolay Aleksandrov 4316615cc23eSNikolay Aleksandrov if (br_multicast_port_ctx_state_stopped(&port->multicast_ctx)) 431774857216SHerbert Xu continue; 431874857216SHerbert Xu 4319615cc23eSNikolay Aleksandrov if (br_multicast_ctx_is_vlan(brmctx)) { 4320615cc23eSNikolay Aleksandrov struct net_bridge_vlan *vlan; 4321615cc23eSNikolay Aleksandrov 4322615cc23eSNikolay Aleksandrov vlan = br_vlan_find(nbp_vlan_group(port), brmctx->vlan->vid); 4323615cc23eSNikolay Aleksandrov if (!vlan || 4324615cc23eSNikolay Aleksandrov br_multicast_port_ctx_state_stopped(&vlan->port_mcast_ctx)) 4325615cc23eSNikolay Aleksandrov continue; 4326615cc23eSNikolay Aleksandrov 4327615cc23eSNikolay Aleksandrov ip4_own_query = &vlan->port_mcast_ctx.ip4_own_query; 4328615cc23eSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 4329615cc23eSNikolay Aleksandrov ip6_own_query = &vlan->port_mcast_ctx.ip6_own_query; 4330615cc23eSNikolay Aleksandrov #endif 4331615cc23eSNikolay Aleksandrov } else { 4332615cc23eSNikolay Aleksandrov ip4_own_query = &port->multicast_ctx.ip4_own_query; 4333615cc23eSNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 4334615cc23eSNikolay Aleksandrov ip6_own_query = &port->multicast_ctx.ip6_own_query; 4335615cc23eSNikolay Aleksandrov #endif 4336615cc23eSNikolay Aleksandrov } 4337615cc23eSNikolay Aleksandrov 4338adc47037SNikolay Aleksandrov if (query == &brmctx->ip4_own_query) 4339615cc23eSNikolay Aleksandrov br_multicast_enable(ip4_own_query); 4340cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 4341cc0fdd80SLinus Lüssing else 4342615cc23eSNikolay Aleksandrov br_multicast_enable(ip6_own_query); 4343cc0fdd80SLinus Lüssing #endif 434474857216SHerbert Xu } 4345c5b493ceSNikolay Aleksandrov rcu_read_unlock(); 434674857216SHerbert Xu } 434774857216SHerbert Xu 4348ae1ea84bSFlorian Fainelli int br_multicast_toggle(struct net_bridge *br, unsigned long val, 4349ae1ea84bSFlorian Fainelli struct netlink_ext_ack *extack) 435074857216SHerbert Xu { 43517cb3f921SNikolay Aleksandrov struct net_bridge_port *port; 4352851d0a73SJoseph Huang bool change_snoopers = false; 4353ae1ea84bSFlorian Fainelli int err = 0; 4354561f1103SHerbert Xu 4355ef5e0d82SAndrey Vagin spin_lock_bh(&br->multicast_lock); 435613cefad2SNikolay Aleksandrov if (!!br_opt_get(br, BROPT_MULTICAST_ENABLED) == !!val) 4357561f1103SHerbert Xu goto unlock; 4358561f1103SHerbert Xu 4359ae1ea84bSFlorian Fainelli err = br_mc_disabled_update(br->dev, val, extack); 4360ae1ea84bSFlorian Fainelli if (err == -EOPNOTSUPP) 4361ae1ea84bSFlorian Fainelli err = 0; 4362ae1ea84bSFlorian Fainelli if (err) 4363ae1ea84bSFlorian Fainelli goto unlock; 4364ae1ea84bSFlorian Fainelli 436513cefad2SNikolay Aleksandrov br_opt_toggle(br, BROPT_MULTICAST_ENABLED, !!val); 43664effd28cSLinus Lüssing if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) { 4367851d0a73SJoseph Huang change_snoopers = true; 4368561f1103SHerbert Xu goto unlock; 43694effd28cSLinus Lüssing } 4370561f1103SHerbert Xu 43713a7fda06SHerbert Xu if (!netif_running(br->dev)) 43723a7fda06SHerbert Xu goto unlock; 43733a7fda06SHerbert Xu 43747cb3f921SNikolay Aleksandrov br_multicast_open(br); 43757cb3f921SNikolay Aleksandrov list_for_each_entry(port, &br->port_list, list) 4376adc47037SNikolay Aleksandrov __br_multicast_enable_port_ctx(&port->multicast_ctx); 4377561f1103SHerbert Xu 4378851d0a73SJoseph Huang change_snoopers = true; 4379851d0a73SJoseph Huang 4380561f1103SHerbert Xu unlock: 4381ef5e0d82SAndrey Vagin spin_unlock_bh(&br->multicast_lock); 4382561f1103SHerbert Xu 4383851d0a73SJoseph Huang /* br_multicast_join_snoopers has the potential to cause 4384851d0a73SJoseph Huang * an MLD Report/Leave to be delivered to br_multicast_rcv, 4385851d0a73SJoseph Huang * which would in turn call br_multicast_add_group, which would 4386851d0a73SJoseph Huang * attempt to acquire multicast_lock. This function should be 4387851d0a73SJoseph Huang * called after the lock has been released to avoid deadlocks on 4388851d0a73SJoseph Huang * multicast_lock. 4389851d0a73SJoseph Huang * 4390851d0a73SJoseph Huang * br_multicast_leave_snoopers does not have the problem since 4391851d0a73SJoseph Huang * br_multicast_rcv first checks BROPT_MULTICAST_ENABLED, and 4392851d0a73SJoseph Huang * returns without calling br_multicast_ipv4/6_rcv if it's not 4393851d0a73SJoseph Huang * enabled. Moved both functions out just for symmetry. 4394851d0a73SJoseph Huang */ 4395851d0a73SJoseph Huang if (change_snoopers) { 4396851d0a73SJoseph Huang if (br_opt_get(br, BROPT_MULTICAST_ENABLED)) 4397851d0a73SJoseph Huang br_multicast_join_snoopers(br); 4398851d0a73SJoseph Huang else 4399851d0a73SJoseph Huang br_multicast_leave_snoopers(br); 4400851d0a73SJoseph Huang } 4401851d0a73SJoseph Huang 4402ae1ea84bSFlorian Fainelli return err; 4403561f1103SHerbert Xu } 4404b195167fSHerbert Xu 44059341b988SIdo Schimmel bool br_multicast_enabled(const struct net_device *dev) 44069341b988SIdo Schimmel { 44079341b988SIdo Schimmel struct net_bridge *br = netdev_priv(dev); 44089341b988SIdo Schimmel 440913cefad2SNikolay Aleksandrov return !!br_opt_get(br, BROPT_MULTICAST_ENABLED); 44109341b988SIdo Schimmel } 44119341b988SIdo Schimmel EXPORT_SYMBOL_GPL(br_multicast_enabled); 44129341b988SIdo Schimmel 44130912bda4SYotam Gigi bool br_multicast_router(const struct net_device *dev) 44140912bda4SYotam Gigi { 44150912bda4SYotam Gigi struct net_bridge *br = netdev_priv(dev); 44160912bda4SYotam Gigi bool is_router; 44170912bda4SYotam Gigi 44180912bda4SYotam Gigi spin_lock_bh(&br->multicast_lock); 4419adc47037SNikolay Aleksandrov is_router = br_multicast_is_router(&br->multicast_ctx, NULL); 44200912bda4SYotam Gigi spin_unlock_bh(&br->multicast_lock); 44210912bda4SYotam Gigi return is_router; 44220912bda4SYotam Gigi } 44230912bda4SYotam Gigi EXPORT_SYMBOL_GPL(br_multicast_router); 44240912bda4SYotam Gigi 442562938182SNikolay Aleksandrov int br_multicast_set_querier(struct net_bridge_mcast *brmctx, unsigned long val) 4426c5c23260SHerbert Xu { 4427b00589afSLinus Lüssing unsigned long max_delay; 4428b00589afSLinus Lüssing 4429c5c23260SHerbert Xu val = !!val; 4430c5c23260SHerbert Xu 443162938182SNikolay Aleksandrov spin_lock_bh(&brmctx->br->multicast_lock); 44324d5b4e84SNikolay Aleksandrov if (brmctx->multicast_querier == val) 4433c5c23260SHerbert Xu goto unlock; 4434c5c23260SHerbert Xu 44354d5b4e84SNikolay Aleksandrov WRITE_ONCE(brmctx->multicast_querier, val); 4436b00589afSLinus Lüssing if (!val) 4437b00589afSLinus Lüssing goto unlock; 4438b00589afSLinus Lüssing 4439d3d065c0SNikolay Aleksandrov max_delay = brmctx->multicast_query_response_interval; 4440b00589afSLinus Lüssing 4441d3d065c0SNikolay Aleksandrov if (!timer_pending(&brmctx->ip4_other_query.timer)) 4442d3d065c0SNikolay Aleksandrov brmctx->ip4_other_query.delay_time = jiffies + max_delay; 4443cc0fdd80SLinus Lüssing 4444adc47037SNikolay Aleksandrov br_multicast_start_querier(brmctx, &brmctx->ip4_own_query); 4445cc0fdd80SLinus Lüssing 4446cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 4447d3d065c0SNikolay Aleksandrov if (!timer_pending(&brmctx->ip6_other_query.timer)) 4448d3d065c0SNikolay Aleksandrov brmctx->ip6_other_query.delay_time = jiffies + max_delay; 4449cc0fdd80SLinus Lüssing 4450adc47037SNikolay Aleksandrov br_multicast_start_querier(brmctx, &brmctx->ip6_own_query); 4451cc0fdd80SLinus Lüssing #endif 4452c5c23260SHerbert Xu 4453c5c23260SHerbert Xu unlock: 445462938182SNikolay Aleksandrov spin_unlock_bh(&brmctx->br->multicast_lock); 4455c5c23260SHerbert Xu 4456c5c23260SHerbert Xu return 0; 4457c5c23260SHerbert Xu } 4458c5c23260SHerbert Xu 4459df271cd6SNikolay Aleksandrov int br_multicast_set_igmp_version(struct net_bridge_mcast *brmctx, 4460df271cd6SNikolay Aleksandrov unsigned long val) 44615e923585SNikolay Aleksandrov { 44625e923585SNikolay Aleksandrov /* Currently we support only version 2 and 3 */ 44635e923585SNikolay Aleksandrov switch (val) { 44645e923585SNikolay Aleksandrov case 2: 44655e923585SNikolay Aleksandrov case 3: 44665e923585SNikolay Aleksandrov break; 44675e923585SNikolay Aleksandrov default: 44685e923585SNikolay Aleksandrov return -EINVAL; 44695e923585SNikolay Aleksandrov } 44705e923585SNikolay Aleksandrov 4471df271cd6SNikolay Aleksandrov spin_lock_bh(&brmctx->br->multicast_lock); 4472df271cd6SNikolay Aleksandrov brmctx->multicast_igmp_version = val; 4473df271cd6SNikolay Aleksandrov spin_unlock_bh(&brmctx->br->multicast_lock); 44745e923585SNikolay Aleksandrov 44755e923585SNikolay Aleksandrov return 0; 44765e923585SNikolay Aleksandrov } 44775e923585SNikolay Aleksandrov 4478aa2ae3e7SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 4479df271cd6SNikolay Aleksandrov int br_multicast_set_mld_version(struct net_bridge_mcast *brmctx, 4480df271cd6SNikolay Aleksandrov unsigned long val) 4481aa2ae3e7SNikolay Aleksandrov { 4482aa2ae3e7SNikolay Aleksandrov /* Currently we support version 1 and 2 */ 4483aa2ae3e7SNikolay Aleksandrov switch (val) { 4484aa2ae3e7SNikolay Aleksandrov case 1: 4485aa2ae3e7SNikolay Aleksandrov case 2: 4486aa2ae3e7SNikolay Aleksandrov break; 4487aa2ae3e7SNikolay Aleksandrov default: 4488aa2ae3e7SNikolay Aleksandrov return -EINVAL; 4489aa2ae3e7SNikolay Aleksandrov } 4490aa2ae3e7SNikolay Aleksandrov 4491df271cd6SNikolay Aleksandrov spin_lock_bh(&brmctx->br->multicast_lock); 4492df271cd6SNikolay Aleksandrov brmctx->multicast_mld_version = val; 4493df271cd6SNikolay Aleksandrov spin_unlock_bh(&brmctx->br->multicast_lock); 4494aa2ae3e7SNikolay Aleksandrov 4495aa2ae3e7SNikolay Aleksandrov return 0; 4496aa2ae3e7SNikolay Aleksandrov } 4497aa2ae3e7SNikolay Aleksandrov #endif 4498aa2ae3e7SNikolay Aleksandrov 449907f8ac4aSLinus Lüssing /** 450007f8ac4aSLinus Lüssing * br_multicast_list_adjacent - Returns snooped multicast addresses 450107f8ac4aSLinus Lüssing * @dev: The bridge port adjacent to which to retrieve addresses 450207f8ac4aSLinus Lüssing * @br_ip_list: The list to store found, snooped multicast IP addresses in 450307f8ac4aSLinus Lüssing * 450407f8ac4aSLinus Lüssing * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast 450507f8ac4aSLinus Lüssing * snooping feature on all bridge ports of dev's bridge device, excluding 450607f8ac4aSLinus Lüssing * the addresses from dev itself. 450707f8ac4aSLinus Lüssing * 450807f8ac4aSLinus Lüssing * Returns the number of items added to br_ip_list. 450907f8ac4aSLinus Lüssing * 451007f8ac4aSLinus Lüssing * Notes: 451107f8ac4aSLinus Lüssing * - br_ip_list needs to be initialized by caller 451207f8ac4aSLinus Lüssing * - br_ip_list might contain duplicates in the end 451307f8ac4aSLinus Lüssing * (needs to be taken care of by caller) 451407f8ac4aSLinus Lüssing * - br_ip_list needs to be freed by caller 451507f8ac4aSLinus Lüssing */ 451607f8ac4aSLinus Lüssing int br_multicast_list_adjacent(struct net_device *dev, 451707f8ac4aSLinus Lüssing struct list_head *br_ip_list) 451807f8ac4aSLinus Lüssing { 451907f8ac4aSLinus Lüssing struct net_bridge *br; 452007f8ac4aSLinus Lüssing struct net_bridge_port *port; 452107f8ac4aSLinus Lüssing struct net_bridge_port_group *group; 452207f8ac4aSLinus Lüssing struct br_ip_list *entry; 452307f8ac4aSLinus Lüssing int count = 0; 452407f8ac4aSLinus Lüssing 452507f8ac4aSLinus Lüssing rcu_read_lock(); 452635f861e3SJulian Wiedmann if (!br_ip_list || !netif_is_bridge_port(dev)) 452707f8ac4aSLinus Lüssing goto unlock; 452807f8ac4aSLinus Lüssing 452907f8ac4aSLinus Lüssing port = br_port_get_rcu(dev); 453007f8ac4aSLinus Lüssing if (!port || !port->br) 453107f8ac4aSLinus Lüssing goto unlock; 453207f8ac4aSLinus Lüssing 453307f8ac4aSLinus Lüssing br = port->br; 453407f8ac4aSLinus Lüssing 453507f8ac4aSLinus Lüssing list_for_each_entry_rcu(port, &br->port_list, list) { 453607f8ac4aSLinus Lüssing if (!port->dev || port->dev == dev) 453707f8ac4aSLinus Lüssing continue; 453807f8ac4aSLinus Lüssing 453907f8ac4aSLinus Lüssing hlist_for_each_entry_rcu(group, &port->mglist, mglist) { 454007f8ac4aSLinus Lüssing entry = kmalloc(sizeof(*entry), GFP_ATOMIC); 454107f8ac4aSLinus Lüssing if (!entry) 454207f8ac4aSLinus Lüssing goto unlock; 454307f8ac4aSLinus Lüssing 4544085b53c8SNikolay Aleksandrov entry->addr = group->key.addr; 454507f8ac4aSLinus Lüssing list_add(&entry->list, br_ip_list); 454607f8ac4aSLinus Lüssing count++; 454707f8ac4aSLinus Lüssing } 454807f8ac4aSLinus Lüssing } 454907f8ac4aSLinus Lüssing 455007f8ac4aSLinus Lüssing unlock: 455107f8ac4aSLinus Lüssing rcu_read_unlock(); 455207f8ac4aSLinus Lüssing return count; 455307f8ac4aSLinus Lüssing } 455407f8ac4aSLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_list_adjacent); 45552cd41431SLinus Lüssing 45562cd41431SLinus Lüssing /** 4557c34963e2SLinus Lüssing * br_multicast_has_querier_anywhere - Checks for a querier on a bridge 4558c34963e2SLinus Lüssing * @dev: The bridge port providing the bridge on which to check for a querier 4559c34963e2SLinus Lüssing * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 4560c34963e2SLinus Lüssing * 4561c34963e2SLinus Lüssing * Checks whether the given interface has a bridge on top and if so returns 4562c34963e2SLinus Lüssing * true if a valid querier exists anywhere on the bridged link layer. 4563c34963e2SLinus Lüssing * Otherwise returns false. 4564c34963e2SLinus Lüssing */ 4565c34963e2SLinus Lüssing bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto) 4566c34963e2SLinus Lüssing { 4567c34963e2SLinus Lüssing struct net_bridge *br; 4568c34963e2SLinus Lüssing struct net_bridge_port *port; 4569c34963e2SLinus Lüssing struct ethhdr eth; 4570c34963e2SLinus Lüssing bool ret = false; 4571c34963e2SLinus Lüssing 4572c34963e2SLinus Lüssing rcu_read_lock(); 457335f861e3SJulian Wiedmann if (!netif_is_bridge_port(dev)) 4574c34963e2SLinus Lüssing goto unlock; 4575c34963e2SLinus Lüssing 4576c34963e2SLinus Lüssing port = br_port_get_rcu(dev); 4577c34963e2SLinus Lüssing if (!port || !port->br) 4578c34963e2SLinus Lüssing goto unlock; 4579c34963e2SLinus Lüssing 4580c34963e2SLinus Lüssing br = port->br; 4581c34963e2SLinus Lüssing 4582c34963e2SLinus Lüssing memset(ð, 0, sizeof(eth)); 4583c34963e2SLinus Lüssing eth.h_proto = htons(proto); 4584c34963e2SLinus Lüssing 4585adc47037SNikolay Aleksandrov ret = br_multicast_querier_exists(&br->multicast_ctx, ð, NULL); 4586c34963e2SLinus Lüssing 4587c34963e2SLinus Lüssing unlock: 4588c34963e2SLinus Lüssing rcu_read_unlock(); 4589c34963e2SLinus Lüssing return ret; 4590c34963e2SLinus Lüssing } 4591c34963e2SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere); 4592c34963e2SLinus Lüssing 4593c34963e2SLinus Lüssing /** 45942cd41431SLinus Lüssing * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port 45952cd41431SLinus Lüssing * @dev: The bridge port adjacent to which to check for a querier 45962cd41431SLinus Lüssing * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 45972cd41431SLinus Lüssing * 45982cd41431SLinus Lüssing * Checks whether the given interface has a bridge on top and if so returns 45992cd41431SLinus Lüssing * true if a selected querier is behind one of the other ports of this 46002cd41431SLinus Lüssing * bridge. Otherwise returns false. 46012cd41431SLinus Lüssing */ 46022cd41431SLinus Lüssing bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto) 46032cd41431SLinus Lüssing { 4604d3d065c0SNikolay Aleksandrov struct net_bridge_mcast *brmctx; 46052cd41431SLinus Lüssing struct net_bridge *br; 46062cd41431SLinus Lüssing struct net_bridge_port *port; 46072cd41431SLinus Lüssing bool ret = false; 4608bb18ef8eSNikolay Aleksandrov int port_ifidx; 46092cd41431SLinus Lüssing 46102cd41431SLinus Lüssing rcu_read_lock(); 461135f861e3SJulian Wiedmann if (!netif_is_bridge_port(dev)) 46122cd41431SLinus Lüssing goto unlock; 46132cd41431SLinus Lüssing 46142cd41431SLinus Lüssing port = br_port_get_rcu(dev); 46152cd41431SLinus Lüssing if (!port || !port->br) 46162cd41431SLinus Lüssing goto unlock; 46172cd41431SLinus Lüssing 46182cd41431SLinus Lüssing br = port->br; 4619d3d065c0SNikolay Aleksandrov brmctx = &br->multicast_ctx; 46202cd41431SLinus Lüssing 46212cd41431SLinus Lüssing switch (proto) { 46222cd41431SLinus Lüssing case ETH_P_IP: 4623bb18ef8eSNikolay Aleksandrov port_ifidx = brmctx->ip4_querier.port_ifidx; 4624d3d065c0SNikolay Aleksandrov if (!timer_pending(&brmctx->ip4_other_query.timer) || 4625bb18ef8eSNikolay Aleksandrov port_ifidx == port->dev->ifindex) 46262cd41431SLinus Lüssing goto unlock; 46272cd41431SLinus Lüssing break; 46283993c4e1SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 46292cd41431SLinus Lüssing case ETH_P_IPV6: 4630bb18ef8eSNikolay Aleksandrov port_ifidx = brmctx->ip6_querier.port_ifidx; 4631d3d065c0SNikolay Aleksandrov if (!timer_pending(&brmctx->ip6_other_query.timer) || 4632bb18ef8eSNikolay Aleksandrov port_ifidx == port->dev->ifindex) 46332cd41431SLinus Lüssing goto unlock; 46342cd41431SLinus Lüssing break; 46353993c4e1SLinus Lüssing #endif 46362cd41431SLinus Lüssing default: 46372cd41431SLinus Lüssing goto unlock; 46382cd41431SLinus Lüssing } 46392cd41431SLinus Lüssing 46402cd41431SLinus Lüssing ret = true; 46412cd41431SLinus Lüssing unlock: 46422cd41431SLinus Lüssing rcu_read_unlock(); 46432cd41431SLinus Lüssing return ret; 46442cd41431SLinus Lüssing } 46452cd41431SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent); 46461080ab95SNikolay Aleksandrov 46473b85f9baSLinus Lüssing /** 46483b85f9baSLinus Lüssing * br_multicast_has_router_adjacent - Checks for a router behind a bridge port 46493b85f9baSLinus Lüssing * @dev: The bridge port adjacent to which to check for a multicast router 46503b85f9baSLinus Lüssing * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 46513b85f9baSLinus Lüssing * 46523b85f9baSLinus Lüssing * Checks whether the given interface has a bridge on top and if so returns 46533b85f9baSLinus Lüssing * true if a multicast router is behind one of the other ports of this 46543b85f9baSLinus Lüssing * bridge. Otherwise returns false. 46553b85f9baSLinus Lüssing */ 46563b85f9baSLinus Lüssing bool br_multicast_has_router_adjacent(struct net_device *dev, int proto) 46573b85f9baSLinus Lüssing { 46589632233eSNikolay Aleksandrov struct net_bridge_mcast_port *pmctx; 4659d3d065c0SNikolay Aleksandrov struct net_bridge_mcast *brmctx; 46609632233eSNikolay Aleksandrov struct net_bridge_port *port; 46613b85f9baSLinus Lüssing bool ret = false; 46623b85f9baSLinus Lüssing 46633b85f9baSLinus Lüssing rcu_read_lock(); 46643b85f9baSLinus Lüssing port = br_port_get_check_rcu(dev); 46653b85f9baSLinus Lüssing if (!port) 46663b85f9baSLinus Lüssing goto unlock; 46673b85f9baSLinus Lüssing 4668d3d065c0SNikolay Aleksandrov brmctx = &port->br->multicast_ctx; 46693b85f9baSLinus Lüssing switch (proto) { 46703b85f9baSLinus Lüssing case ETH_P_IP: 4671d3d065c0SNikolay Aleksandrov hlist_for_each_entry_rcu(pmctx, &brmctx->ip4_mc_router_list, 46723b85f9baSLinus Lüssing ip4_rlist) { 46739632233eSNikolay Aleksandrov if (pmctx->port == port) 46743b85f9baSLinus Lüssing continue; 46753b85f9baSLinus Lüssing 46763b85f9baSLinus Lüssing ret = true; 46773b85f9baSLinus Lüssing goto unlock; 46783b85f9baSLinus Lüssing } 46793b85f9baSLinus Lüssing break; 46803b85f9baSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 46813b85f9baSLinus Lüssing case ETH_P_IPV6: 4682d3d065c0SNikolay Aleksandrov hlist_for_each_entry_rcu(pmctx, &brmctx->ip6_mc_router_list, 46833b85f9baSLinus Lüssing ip6_rlist) { 46849632233eSNikolay Aleksandrov if (pmctx->port == port) 46853b85f9baSLinus Lüssing continue; 46863b85f9baSLinus Lüssing 46873b85f9baSLinus Lüssing ret = true; 46883b85f9baSLinus Lüssing goto unlock; 46893b85f9baSLinus Lüssing } 46903b85f9baSLinus Lüssing break; 46913b85f9baSLinus Lüssing #endif 46923b85f9baSLinus Lüssing default: 46933b85f9baSLinus Lüssing /* when compiled without IPv6 support, be conservative and 46943b85f9baSLinus Lüssing * always assume presence of an IPv6 multicast router 46953b85f9baSLinus Lüssing */ 46963b85f9baSLinus Lüssing ret = true; 46973b85f9baSLinus Lüssing } 46983b85f9baSLinus Lüssing 46993b85f9baSLinus Lüssing unlock: 47003b85f9baSLinus Lüssing rcu_read_unlock(); 47013b85f9baSLinus Lüssing return ret; 47023b85f9baSLinus Lüssing } 47033b85f9baSLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_router_adjacent); 47043b85f9baSLinus Lüssing 47051080ab95SNikolay Aleksandrov static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats, 4706a65056ecSNikolay Aleksandrov const struct sk_buff *skb, u8 type, u8 dir) 47071080ab95SNikolay Aleksandrov { 47081080ab95SNikolay Aleksandrov struct bridge_mcast_stats *pstats = this_cpu_ptr(stats); 4709a65056ecSNikolay Aleksandrov __be16 proto = skb->protocol; 4710a65056ecSNikolay Aleksandrov unsigned int t_len; 47111080ab95SNikolay Aleksandrov 47121080ab95SNikolay Aleksandrov u64_stats_update_begin(&pstats->syncp); 47131080ab95SNikolay Aleksandrov switch (proto) { 47141080ab95SNikolay Aleksandrov case htons(ETH_P_IP): 4715a65056ecSNikolay Aleksandrov t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb); 47161080ab95SNikolay Aleksandrov switch (type) { 47171080ab95SNikolay Aleksandrov case IGMP_HOST_MEMBERSHIP_REPORT: 47181080ab95SNikolay Aleksandrov pstats->mstats.igmp_v1reports[dir]++; 47191080ab95SNikolay Aleksandrov break; 47201080ab95SNikolay Aleksandrov case IGMPV2_HOST_MEMBERSHIP_REPORT: 47211080ab95SNikolay Aleksandrov pstats->mstats.igmp_v2reports[dir]++; 47221080ab95SNikolay Aleksandrov break; 47231080ab95SNikolay Aleksandrov case IGMPV3_HOST_MEMBERSHIP_REPORT: 47241080ab95SNikolay Aleksandrov pstats->mstats.igmp_v3reports[dir]++; 47251080ab95SNikolay Aleksandrov break; 47261080ab95SNikolay Aleksandrov case IGMP_HOST_MEMBERSHIP_QUERY: 4727a65056ecSNikolay Aleksandrov if (t_len != sizeof(struct igmphdr)) { 4728a65056ecSNikolay Aleksandrov pstats->mstats.igmp_v3queries[dir]++; 4729a65056ecSNikolay Aleksandrov } else { 4730a65056ecSNikolay Aleksandrov unsigned int offset = skb_transport_offset(skb); 4731a65056ecSNikolay Aleksandrov struct igmphdr *ih, _ihdr; 4732a65056ecSNikolay Aleksandrov 4733a65056ecSNikolay Aleksandrov ih = skb_header_pointer(skb, offset, 4734a65056ecSNikolay Aleksandrov sizeof(_ihdr), &_ihdr); 4735a65056ecSNikolay Aleksandrov if (!ih) 4736a65056ecSNikolay Aleksandrov break; 4737a65056ecSNikolay Aleksandrov if (!ih->code) 4738a65056ecSNikolay Aleksandrov pstats->mstats.igmp_v1queries[dir]++; 4739a65056ecSNikolay Aleksandrov else 4740a65056ecSNikolay Aleksandrov pstats->mstats.igmp_v2queries[dir]++; 4741a65056ecSNikolay Aleksandrov } 47421080ab95SNikolay Aleksandrov break; 47431080ab95SNikolay Aleksandrov case IGMP_HOST_LEAVE_MESSAGE: 47441080ab95SNikolay Aleksandrov pstats->mstats.igmp_leaves[dir]++; 47451080ab95SNikolay Aleksandrov break; 47461080ab95SNikolay Aleksandrov } 47471080ab95SNikolay Aleksandrov break; 47481080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 47491080ab95SNikolay Aleksandrov case htons(ETH_P_IPV6): 4750a65056ecSNikolay Aleksandrov t_len = ntohs(ipv6_hdr(skb)->payload_len) + 4751a65056ecSNikolay Aleksandrov sizeof(struct ipv6hdr); 4752a65056ecSNikolay Aleksandrov t_len -= skb_network_header_len(skb); 47531080ab95SNikolay Aleksandrov switch (type) { 47541080ab95SNikolay Aleksandrov case ICMPV6_MGM_REPORT: 47551080ab95SNikolay Aleksandrov pstats->mstats.mld_v1reports[dir]++; 47561080ab95SNikolay Aleksandrov break; 47571080ab95SNikolay Aleksandrov case ICMPV6_MLD2_REPORT: 47581080ab95SNikolay Aleksandrov pstats->mstats.mld_v2reports[dir]++; 47591080ab95SNikolay Aleksandrov break; 47601080ab95SNikolay Aleksandrov case ICMPV6_MGM_QUERY: 4761a65056ecSNikolay Aleksandrov if (t_len != sizeof(struct mld_msg)) 4762a65056ecSNikolay Aleksandrov pstats->mstats.mld_v2queries[dir]++; 4763a65056ecSNikolay Aleksandrov else 4764a65056ecSNikolay Aleksandrov pstats->mstats.mld_v1queries[dir]++; 47651080ab95SNikolay Aleksandrov break; 47661080ab95SNikolay Aleksandrov case ICMPV6_MGM_REDUCTION: 47671080ab95SNikolay Aleksandrov pstats->mstats.mld_leaves[dir]++; 47681080ab95SNikolay Aleksandrov break; 47691080ab95SNikolay Aleksandrov } 47701080ab95SNikolay Aleksandrov break; 47711080ab95SNikolay Aleksandrov #endif /* CONFIG_IPV6 */ 47721080ab95SNikolay Aleksandrov } 47731080ab95SNikolay Aleksandrov u64_stats_update_end(&pstats->syncp); 47741080ab95SNikolay Aleksandrov } 47751080ab95SNikolay Aleksandrov 4776adc47037SNikolay Aleksandrov void br_multicast_count(struct net_bridge *br, 4777adc47037SNikolay Aleksandrov const struct net_bridge_port *p, 4778a65056ecSNikolay Aleksandrov const struct sk_buff *skb, u8 type, u8 dir) 47791080ab95SNikolay Aleksandrov { 47801080ab95SNikolay Aleksandrov struct bridge_mcast_stats __percpu *stats; 47811080ab95SNikolay Aleksandrov 47821080ab95SNikolay Aleksandrov /* if multicast_disabled is true then igmp type can't be set */ 4783675779adSNikolay Aleksandrov if (!type || !br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)) 47841080ab95SNikolay Aleksandrov return; 47851080ab95SNikolay Aleksandrov 47861080ab95SNikolay Aleksandrov if (p) 47871080ab95SNikolay Aleksandrov stats = p->mcast_stats; 47881080ab95SNikolay Aleksandrov else 47891080ab95SNikolay Aleksandrov stats = br->mcast_stats; 47901080ab95SNikolay Aleksandrov if (WARN_ON(!stats)) 47911080ab95SNikolay Aleksandrov return; 47921080ab95SNikolay Aleksandrov 4793a65056ecSNikolay Aleksandrov br_mcast_stats_add(stats, skb, type, dir); 47941080ab95SNikolay Aleksandrov } 47951080ab95SNikolay Aleksandrov 47961080ab95SNikolay Aleksandrov int br_multicast_init_stats(struct net_bridge *br) 47971080ab95SNikolay Aleksandrov { 47981080ab95SNikolay Aleksandrov br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats); 47991080ab95SNikolay Aleksandrov if (!br->mcast_stats) 48001080ab95SNikolay Aleksandrov return -ENOMEM; 48011080ab95SNikolay Aleksandrov 48021080ab95SNikolay Aleksandrov return 0; 48031080ab95SNikolay Aleksandrov } 48041080ab95SNikolay Aleksandrov 4805b6fe0440SIdo Schimmel void br_multicast_uninit_stats(struct net_bridge *br) 4806b6fe0440SIdo Schimmel { 4807b6fe0440SIdo Schimmel free_percpu(br->mcast_stats); 4808b6fe0440SIdo Schimmel } 4809b6fe0440SIdo Schimmel 4810b3b6a84cSArnd Bergmann /* noinline for https://bugs.llvm.org/show_bug.cgi?id=45802#c9 */ 4811b3b6a84cSArnd Bergmann static noinline_for_stack void mcast_stats_add_dir(u64 *dst, u64 *src) 48121080ab95SNikolay Aleksandrov { 48131080ab95SNikolay Aleksandrov dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX]; 48141080ab95SNikolay Aleksandrov dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX]; 48151080ab95SNikolay Aleksandrov } 48161080ab95SNikolay Aleksandrov 48171080ab95SNikolay Aleksandrov void br_multicast_get_stats(const struct net_bridge *br, 48181080ab95SNikolay Aleksandrov const struct net_bridge_port *p, 48191080ab95SNikolay Aleksandrov struct br_mcast_stats *dest) 48201080ab95SNikolay Aleksandrov { 48211080ab95SNikolay Aleksandrov struct bridge_mcast_stats __percpu *stats; 48221080ab95SNikolay Aleksandrov struct br_mcast_stats tdst; 48231080ab95SNikolay Aleksandrov int i; 48241080ab95SNikolay Aleksandrov 48251080ab95SNikolay Aleksandrov memset(dest, 0, sizeof(*dest)); 48261080ab95SNikolay Aleksandrov if (p) 48271080ab95SNikolay Aleksandrov stats = p->mcast_stats; 48281080ab95SNikolay Aleksandrov else 48291080ab95SNikolay Aleksandrov stats = br->mcast_stats; 48301080ab95SNikolay Aleksandrov if (WARN_ON(!stats)) 48311080ab95SNikolay Aleksandrov return; 48321080ab95SNikolay Aleksandrov 48331080ab95SNikolay Aleksandrov memset(&tdst, 0, sizeof(tdst)); 48341080ab95SNikolay Aleksandrov for_each_possible_cpu(i) { 48351080ab95SNikolay Aleksandrov struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i); 48361080ab95SNikolay Aleksandrov struct br_mcast_stats temp; 48371080ab95SNikolay Aleksandrov unsigned int start; 48381080ab95SNikolay Aleksandrov 48391080ab95SNikolay Aleksandrov do { 48401080ab95SNikolay Aleksandrov start = u64_stats_fetch_begin_irq(&cpu_stats->syncp); 48411080ab95SNikolay Aleksandrov memcpy(&temp, &cpu_stats->mstats, sizeof(temp)); 48421080ab95SNikolay Aleksandrov } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start)); 48431080ab95SNikolay Aleksandrov 4844a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries); 4845a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries); 4846a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries); 48471080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves); 48481080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports); 48491080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports); 48501080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports); 48511080ab95SNikolay Aleksandrov tdst.igmp_parse_errors += temp.igmp_parse_errors; 48521080ab95SNikolay Aleksandrov 4853a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries); 4854a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries); 48551080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves); 48561080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports); 48571080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports); 48581080ab95SNikolay Aleksandrov tdst.mld_parse_errors += temp.mld_parse_errors; 48591080ab95SNikolay Aleksandrov } 48601080ab95SNikolay Aleksandrov memcpy(dest, &tdst, sizeof(*dest)); 48611080ab95SNikolay Aleksandrov } 486219e3a9c9SNikolay Aleksandrov 486319e3a9c9SNikolay Aleksandrov int br_mdb_hash_init(struct net_bridge *br) 486419e3a9c9SNikolay Aleksandrov { 4865085b53c8SNikolay Aleksandrov int err; 4866085b53c8SNikolay Aleksandrov 4867085b53c8SNikolay Aleksandrov err = rhashtable_init(&br->sg_port_tbl, &br_sg_port_rht_params); 4868085b53c8SNikolay Aleksandrov if (err) 4869085b53c8SNikolay Aleksandrov return err; 4870085b53c8SNikolay Aleksandrov 4871085b53c8SNikolay Aleksandrov err = rhashtable_init(&br->mdb_hash_tbl, &br_mdb_rht_params); 4872085b53c8SNikolay Aleksandrov if (err) { 4873085b53c8SNikolay Aleksandrov rhashtable_destroy(&br->sg_port_tbl); 4874085b53c8SNikolay Aleksandrov return err; 4875085b53c8SNikolay Aleksandrov } 4876085b53c8SNikolay Aleksandrov 4877085b53c8SNikolay Aleksandrov return 0; 487819e3a9c9SNikolay Aleksandrov } 487919e3a9c9SNikolay Aleksandrov 488019e3a9c9SNikolay Aleksandrov void br_mdb_hash_fini(struct net_bridge *br) 488119e3a9c9SNikolay Aleksandrov { 4882085b53c8SNikolay Aleksandrov rhashtable_destroy(&br->sg_port_tbl); 488319e3a9c9SNikolay Aleksandrov rhashtable_destroy(&br->mdb_hash_tbl); 488419e3a9c9SNikolay Aleksandrov } 4885