12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2eb1d1641SHerbert Xu /* 3eb1d1641SHerbert Xu * Bridge multicast support. 4eb1d1641SHerbert Xu * 5eb1d1641SHerbert Xu * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au> 6eb1d1641SHerbert Xu */ 7eb1d1641SHerbert Xu 8eb1d1641SHerbert Xu #include <linux/err.h> 907f8ac4aSLinus Lüssing #include <linux/export.h> 10eb1d1641SHerbert Xu #include <linux/if_ether.h> 11eb1d1641SHerbert Xu #include <linux/igmp.h> 124b3087c7SLinus Lüssing #include <linux/in.h> 13eb1d1641SHerbert Xu #include <linux/jhash.h> 14eb1d1641SHerbert Xu #include <linux/kernel.h> 15b195167fSHerbert Xu #include <linux/log2.h> 16eb1d1641SHerbert Xu #include <linux/netdevice.h> 17eb1d1641SHerbert Xu #include <linux/netfilter_bridge.h> 18eb1d1641SHerbert Xu #include <linux/random.h> 19eb1d1641SHerbert Xu #include <linux/rculist.h> 20eb1d1641SHerbert Xu #include <linux/skbuff.h> 21eb1d1641SHerbert Xu #include <linux/slab.h> 22eb1d1641SHerbert Xu #include <linux/timer.h> 231c8ad5bfSCong Wang #include <linux/inetdevice.h> 2491b02d3dSNikolay Aleksandrov #include <linux/mroute.h> 25eb1d1641SHerbert Xu #include <net/ip.h> 26147c1e9bSNogah Frankel #include <net/switchdev.h> 27dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 284b3087c7SLinus Lüssing #include <linux/icmpv6.h> 2908b202b6SYOSHIFUJI Hideaki #include <net/ipv6.h> 3008b202b6SYOSHIFUJI Hideaki #include <net/mld.h> 31d4c4f07dSDavid S. Miller #include <net/ip6_checksum.h> 323c3769e6SLinus Lüssing #include <net/addrconf.h> 3308b202b6SYOSHIFUJI Hideaki #endif 34eb1d1641SHerbert Xu 35eb1d1641SHerbert Xu #include "br_private.h" 36eb1d1641SHerbert Xu 3719e3a9c9SNikolay Aleksandrov static const struct rhashtable_params br_mdb_rht_params = { 3819e3a9c9SNikolay Aleksandrov .head_offset = offsetof(struct net_bridge_mdb_entry, rhnode), 3919e3a9c9SNikolay Aleksandrov .key_offset = offsetof(struct net_bridge_mdb_entry, addr), 4019e3a9c9SNikolay Aleksandrov .key_len = sizeof(struct br_ip), 4119e3a9c9SNikolay Aleksandrov .automatic_shrinking = true, 4219e3a9c9SNikolay Aleksandrov }; 4319e3a9c9SNikolay Aleksandrov 44cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br, 4590010b36SLinus Lüssing struct bridge_mcast_own_query *query); 46754bc547SSatish Ashok static void br_multicast_add_router(struct net_bridge *br, 47754bc547SSatish Ashok struct net_bridge_port *port); 48bc8c20acSSatish Ashok static void br_ip4_multicast_leave_group(struct net_bridge *br, 49bc8c20acSSatish Ashok struct net_bridge_port *port, 50bc8c20acSSatish Ashok __be32 group, 516db6f0eaSFelix Fietkau __u16 vid, 526db6f0eaSFelix Fietkau const unsigned char *src); 536db6f0eaSFelix Fietkau 54f12e7d95SNogah Frankel static void __del_port_router(struct net_bridge_port *p); 55bc8c20acSSatish Ashok #if IS_ENABLED(CONFIG_IPV6) 56bc8c20acSSatish Ashok static void br_ip6_multicast_leave_group(struct net_bridge *br, 57bc8c20acSSatish Ashok struct net_bridge_port *port, 58bc8c20acSSatish Ashok const struct in6_addr *group, 596db6f0eaSFelix Fietkau __u16 vid, const unsigned char *src); 60bc8c20acSSatish Ashok #endif 61c83b8fabSHerbert Xu 6219e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip_get_rcu(struct net_bridge *br, 63cfd56754SCong Wang struct br_ip *dst) 647f285fa7SHerbert Xu { 6519e3a9c9SNikolay Aleksandrov return rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params); 667f285fa7SHerbert Xu } 677f285fa7SHerbert Xu 6819e3a9c9SNikolay Aleksandrov struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge *br, 6919e3a9c9SNikolay Aleksandrov struct br_ip *dst) 7019e3a9c9SNikolay Aleksandrov { 7119e3a9c9SNikolay Aleksandrov struct net_bridge_mdb_entry *ent; 7219e3a9c9SNikolay Aleksandrov 7319e3a9c9SNikolay Aleksandrov lockdep_assert_held_once(&br->multicast_lock); 7419e3a9c9SNikolay Aleksandrov 7519e3a9c9SNikolay Aleksandrov rcu_read_lock(); 7619e3a9c9SNikolay Aleksandrov ent = rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params); 7719e3a9c9SNikolay Aleksandrov rcu_read_unlock(); 7819e3a9c9SNikolay Aleksandrov 7919e3a9c9SNikolay Aleksandrov return ent; 8019e3a9c9SNikolay Aleksandrov } 8119e3a9c9SNikolay Aleksandrov 8219e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip4_get(struct net_bridge *br, 8319e3a9c9SNikolay Aleksandrov __be32 dst, __u16 vid) 84eb1d1641SHerbert Xu { 858ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_dst; 860821ec55SHerbert Xu 8719e3a9c9SNikolay Aleksandrov memset(&br_dst, 0, sizeof(br_dst)); 888ef2a9a5SYOSHIFUJI Hideaki br_dst.u.ip4 = dst; 898ef2a9a5SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IP); 90b0e9a30dSVlad Yasevich br_dst.vid = vid; 918ef2a9a5SYOSHIFUJI Hideaki 9219e3a9c9SNikolay Aleksandrov return br_mdb_ip_get(br, &br_dst); 938ef2a9a5SYOSHIFUJI Hideaki } 948ef2a9a5SYOSHIFUJI Hideaki 95dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 9619e3a9c9SNikolay Aleksandrov static struct net_bridge_mdb_entry *br_mdb_ip6_get(struct net_bridge *br, 9719e3a9c9SNikolay Aleksandrov const struct in6_addr *dst, 98b0e9a30dSVlad Yasevich __u16 vid) 9908b202b6SYOSHIFUJI Hideaki { 10008b202b6SYOSHIFUJI Hideaki struct br_ip br_dst; 10108b202b6SYOSHIFUJI Hideaki 10219e3a9c9SNikolay Aleksandrov memset(&br_dst, 0, sizeof(br_dst)); 1034e3fd7a0SAlexey Dobriyan br_dst.u.ip6 = *dst; 10408b202b6SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IPV6); 105b0e9a30dSVlad Yasevich br_dst.vid = vid; 10608b202b6SYOSHIFUJI Hideaki 10719e3a9c9SNikolay Aleksandrov return br_mdb_ip_get(br, &br_dst); 10808b202b6SYOSHIFUJI Hideaki } 10908b202b6SYOSHIFUJI Hideaki #endif 11008b202b6SYOSHIFUJI Hideaki 111eb1d1641SHerbert Xu struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br, 112fbca58a2SCong Wang struct sk_buff *skb, u16 vid) 113eb1d1641SHerbert Xu { 1148ef2a9a5SYOSHIFUJI Hideaki struct br_ip ip; 1158ef2a9a5SYOSHIFUJI Hideaki 11613cefad2SNikolay Aleksandrov if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) 117eb1d1641SHerbert Xu return NULL; 118eb1d1641SHerbert Xu 1198ef2a9a5SYOSHIFUJI Hideaki if (BR_INPUT_SKB_CB(skb)->igmp) 1208ef2a9a5SYOSHIFUJI Hideaki return NULL; 1218ef2a9a5SYOSHIFUJI Hideaki 12219e3a9c9SNikolay Aleksandrov memset(&ip, 0, sizeof(ip)); 1238ef2a9a5SYOSHIFUJI Hideaki ip.proto = skb->protocol; 124fbca58a2SCong Wang ip.vid = vid; 1258ef2a9a5SYOSHIFUJI Hideaki 126eb1d1641SHerbert Xu switch (skb->protocol) { 127eb1d1641SHerbert Xu case htons(ETH_P_IP): 1288ef2a9a5SYOSHIFUJI Hideaki ip.u.ip4 = ip_hdr(skb)->daddr; 129eb1d1641SHerbert Xu break; 130dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 13108b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 1324e3fd7a0SAlexey Dobriyan ip.u.ip6 = ipv6_hdr(skb)->daddr; 13308b202b6SYOSHIFUJI Hideaki break; 13408b202b6SYOSHIFUJI Hideaki #endif 1358ef2a9a5SYOSHIFUJI Hideaki default: 1368ef2a9a5SYOSHIFUJI Hideaki return NULL; 137eb1d1641SHerbert Xu } 138eb1d1641SHerbert Xu 13919e3a9c9SNikolay Aleksandrov return br_mdb_ip_get_rcu(br, &ip); 140eb1d1641SHerbert Xu } 141eb1d1641SHerbert Xu 14288c1f37fSAllen Pais static void br_multicast_group_expired(struct timer_list *t) 143eb1d1641SHerbert Xu { 14488c1f37fSAllen Pais struct net_bridge_mdb_entry *mp = from_timer(mp, t, timer); 145eb1d1641SHerbert Xu struct net_bridge *br = mp->br; 146eb1d1641SHerbert Xu 147eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 148eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&mp->timer)) 149eb1d1641SHerbert Xu goto out; 150eb1d1641SHerbert Xu 151*1bc844eeSNikolay Aleksandrov br_multicast_host_leave(mp, true); 152eb1d1641SHerbert Xu 153eb1d1641SHerbert Xu if (mp->ports) 154eb1d1641SHerbert Xu goto out; 155eb1d1641SHerbert Xu 15619e3a9c9SNikolay Aleksandrov rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode, 15719e3a9c9SNikolay Aleksandrov br_mdb_rht_params); 15819e3a9c9SNikolay Aleksandrov hlist_del_rcu(&mp->mdb_node); 159eb1d1641SHerbert Xu 1604329596cSNikolay Aleksandrov kfree_rcu(mp, rcu); 161eb1d1641SHerbert Xu 162eb1d1641SHerbert Xu out: 163eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 164eb1d1641SHerbert Xu } 165eb1d1641SHerbert Xu 166eb1d1641SHerbert Xu static void br_multicast_del_pg(struct net_bridge *br, 167eb1d1641SHerbert Xu struct net_bridge_port_group *pg) 168eb1d1641SHerbert Xu { 169eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 170eb1d1641SHerbert Xu struct net_bridge_port_group *p; 171e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 172e8051688SEric Dumazet 17319e3a9c9SNikolay Aleksandrov mp = br_mdb_ip_get(br, &pg->addr); 174eb1d1641SHerbert Xu if (WARN_ON(!mp)) 175eb1d1641SHerbert Xu return; 176eb1d1641SHerbert Xu 177e8051688SEric Dumazet for (pp = &mp->ports; 178e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 179e8051688SEric Dumazet pp = &p->next) { 180eb1d1641SHerbert Xu if (p != pg) 181eb1d1641SHerbert Xu continue; 182eb1d1641SHerbert Xu 18383f6a740Sstephen hemminger rcu_assign_pointer(*pp, p->next); 184eb1d1641SHerbert Xu hlist_del_init(&p->mglist); 185eb1d1641SHerbert Xu del_timer(&p->timer); 18645ebcce5SElad Raz br_mdb_notify(br->dev, p->port, &pg->addr, RTM_DELMDB, 18745ebcce5SElad Raz p->flags); 1884329596cSNikolay Aleksandrov kfree_rcu(p, rcu); 189eb1d1641SHerbert Xu 190ff0fd34eSAndrew Lunn if (!mp->ports && !mp->host_joined && 191eb1d1641SHerbert Xu netif_running(br->dev)) 192eb1d1641SHerbert Xu mod_timer(&mp->timer, jiffies); 193eb1d1641SHerbert Xu 194eb1d1641SHerbert Xu return; 195eb1d1641SHerbert Xu } 196eb1d1641SHerbert Xu 197eb1d1641SHerbert Xu WARN_ON(1); 198eb1d1641SHerbert Xu } 199eb1d1641SHerbert Xu 20088c1f37fSAllen Pais static void br_multicast_port_group_expired(struct timer_list *t) 201eb1d1641SHerbert Xu { 20288c1f37fSAllen Pais struct net_bridge_port_group *pg = from_timer(pg, t, timer); 203eb1d1641SHerbert Xu struct net_bridge *br = pg->port->br; 204eb1d1641SHerbert Xu 205eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 206eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&pg->timer) || 2079d06b6d8SElad Raz hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT) 208eb1d1641SHerbert Xu goto out; 209eb1d1641SHerbert Xu 210eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 211eb1d1641SHerbert Xu 212eb1d1641SHerbert Xu out: 213eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 214eb1d1641SHerbert Xu } 215eb1d1641SHerbert Xu 2168ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br, 2171080ab95SNikolay Aleksandrov __be32 group, 2181080ab95SNikolay Aleksandrov u8 *igmp_type) 219eb1d1641SHerbert Xu { 2205e923585SNikolay Aleksandrov struct igmpv3_query *ihv3; 2215e923585SNikolay Aleksandrov size_t igmp_hdr_size; 222eb1d1641SHerbert Xu struct sk_buff *skb; 223eb1d1641SHerbert Xu struct igmphdr *ih; 224eb1d1641SHerbert Xu struct ethhdr *eth; 225eb1d1641SHerbert Xu struct iphdr *iph; 226eb1d1641SHerbert Xu 2275e923585SNikolay Aleksandrov igmp_hdr_size = sizeof(*ih); 2285e923585SNikolay Aleksandrov if (br->multicast_igmp_version == 3) 2295e923585SNikolay Aleksandrov igmp_hdr_size = sizeof(*ihv3); 230eb1d1641SHerbert Xu skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) + 2315e923585SNikolay Aleksandrov igmp_hdr_size + 4); 232eb1d1641SHerbert Xu if (!skb) 233eb1d1641SHerbert Xu goto out; 234eb1d1641SHerbert Xu 235eb1d1641SHerbert Xu skb->protocol = htons(ETH_P_IP); 236eb1d1641SHerbert Xu 237eb1d1641SHerbert Xu skb_reset_mac_header(skb); 238eb1d1641SHerbert Xu eth = eth_hdr(skb); 239eb1d1641SHerbert Xu 240e5a727f6SJoe Perches ether_addr_copy(eth->h_source, br->dev->dev_addr); 241eb1d1641SHerbert Xu eth->h_dest[0] = 1; 242eb1d1641SHerbert Xu eth->h_dest[1] = 0; 243eb1d1641SHerbert Xu eth->h_dest[2] = 0x5e; 244eb1d1641SHerbert Xu eth->h_dest[3] = 0; 245eb1d1641SHerbert Xu eth->h_dest[4] = 0; 246eb1d1641SHerbert Xu eth->h_dest[5] = 1; 247eb1d1641SHerbert Xu eth->h_proto = htons(ETH_P_IP); 248eb1d1641SHerbert Xu skb_put(skb, sizeof(*eth)); 249eb1d1641SHerbert Xu 250eb1d1641SHerbert Xu skb_set_network_header(skb, skb->len); 251eb1d1641SHerbert Xu iph = ip_hdr(skb); 252eb1d1641SHerbert Xu 253eb1d1641SHerbert Xu iph->version = 4; 254eb1d1641SHerbert Xu iph->ihl = 6; 255eb1d1641SHerbert Xu iph->tos = 0xc0; 2565e923585SNikolay Aleksandrov iph->tot_len = htons(sizeof(*iph) + igmp_hdr_size + 4); 257eb1d1641SHerbert Xu iph->id = 0; 258eb1d1641SHerbert Xu iph->frag_off = htons(IP_DF); 259eb1d1641SHerbert Xu iph->ttl = 1; 260eb1d1641SHerbert Xu iph->protocol = IPPROTO_IGMP; 261675779adSNikolay Aleksandrov iph->saddr = br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR) ? 2621c8ad5bfSCong Wang inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0; 263eb1d1641SHerbert Xu iph->daddr = htonl(INADDR_ALLHOSTS_GROUP); 264eb1d1641SHerbert Xu ((u8 *)&iph[1])[0] = IPOPT_RA; 265eb1d1641SHerbert Xu ((u8 *)&iph[1])[1] = 4; 266eb1d1641SHerbert Xu ((u8 *)&iph[1])[2] = 0; 267eb1d1641SHerbert Xu ((u8 *)&iph[1])[3] = 0; 268eb1d1641SHerbert Xu ip_send_check(iph); 269eb1d1641SHerbert Xu skb_put(skb, 24); 270eb1d1641SHerbert Xu 271eb1d1641SHerbert Xu skb_set_transport_header(skb, skb->len); 2721080ab95SNikolay Aleksandrov *igmp_type = IGMP_HOST_MEMBERSHIP_QUERY; 2735e923585SNikolay Aleksandrov 2745e923585SNikolay Aleksandrov switch (br->multicast_igmp_version) { 2755e923585SNikolay Aleksandrov case 2: 2765e923585SNikolay Aleksandrov ih = igmp_hdr(skb); 277eb1d1641SHerbert Xu ih->type = IGMP_HOST_MEMBERSHIP_QUERY; 278eb1d1641SHerbert Xu ih->code = (group ? br->multicast_last_member_interval : 279eb1d1641SHerbert Xu br->multicast_query_response_interval) / 280eb1d1641SHerbert Xu (HZ / IGMP_TIMER_SCALE); 281eb1d1641SHerbert Xu ih->group = group; 282eb1d1641SHerbert Xu ih->csum = 0; 2835e923585SNikolay Aleksandrov ih->csum = ip_compute_csum((void *)ih, sizeof(*ih)); 2845e923585SNikolay Aleksandrov break; 2855e923585SNikolay Aleksandrov case 3: 2865e923585SNikolay Aleksandrov ihv3 = igmpv3_query_hdr(skb); 2875e923585SNikolay Aleksandrov ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY; 2885e923585SNikolay Aleksandrov ihv3->code = (group ? br->multicast_last_member_interval : 2895e923585SNikolay Aleksandrov br->multicast_query_response_interval) / 2905e923585SNikolay Aleksandrov (HZ / IGMP_TIMER_SCALE); 2915e923585SNikolay Aleksandrov ihv3->group = group; 2925e923585SNikolay Aleksandrov ihv3->qqic = br->multicast_query_interval / HZ; 2935e923585SNikolay Aleksandrov ihv3->nsrcs = 0; 2945e923585SNikolay Aleksandrov ihv3->resv = 0; 2955e923585SNikolay Aleksandrov ihv3->suppress = 0; 2965e923585SNikolay Aleksandrov ihv3->qrv = 2; 2975e923585SNikolay Aleksandrov ihv3->csum = 0; 2985e923585SNikolay Aleksandrov ihv3->csum = ip_compute_csum((void *)ihv3, sizeof(*ihv3)); 2995e923585SNikolay Aleksandrov break; 3005e923585SNikolay Aleksandrov } 301eb1d1641SHerbert Xu 3025e923585SNikolay Aleksandrov skb_put(skb, igmp_hdr_size); 303eb1d1641SHerbert Xu __skb_pull(skb, sizeof(*eth)); 304eb1d1641SHerbert Xu 305eb1d1641SHerbert Xu out: 306eb1d1641SHerbert Xu return skb; 307eb1d1641SHerbert Xu } 308eb1d1641SHerbert Xu 309dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 31008b202b6SYOSHIFUJI Hideaki static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br, 3111080ab95SNikolay Aleksandrov const struct in6_addr *grp, 3121080ab95SNikolay Aleksandrov u8 *igmp_type) 31308b202b6SYOSHIFUJI Hideaki { 314aa2ae3e7SNikolay Aleksandrov struct mld2_query *mld2q; 315aa2ae3e7SNikolay Aleksandrov unsigned long interval; 31608b202b6SYOSHIFUJI Hideaki struct ipv6hdr *ip6h; 31708b202b6SYOSHIFUJI Hideaki struct mld_msg *mldq; 318aa2ae3e7SNikolay Aleksandrov size_t mld_hdr_size; 319aa2ae3e7SNikolay Aleksandrov struct sk_buff *skb; 32008b202b6SYOSHIFUJI Hideaki struct ethhdr *eth; 32108b202b6SYOSHIFUJI Hideaki u8 *hopopt; 32208b202b6SYOSHIFUJI Hideaki 323aa2ae3e7SNikolay Aleksandrov mld_hdr_size = sizeof(*mldq); 324aa2ae3e7SNikolay Aleksandrov if (br->multicast_mld_version == 2) 325aa2ae3e7SNikolay Aleksandrov mld_hdr_size = sizeof(*mld2q); 32608b202b6SYOSHIFUJI Hideaki skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) + 327aa2ae3e7SNikolay Aleksandrov 8 + mld_hdr_size); 32808b202b6SYOSHIFUJI Hideaki if (!skb) 32908b202b6SYOSHIFUJI Hideaki goto out; 33008b202b6SYOSHIFUJI Hideaki 33108b202b6SYOSHIFUJI Hideaki skb->protocol = htons(ETH_P_IPV6); 33208b202b6SYOSHIFUJI Hideaki 33308b202b6SYOSHIFUJI Hideaki /* Ethernet header */ 33408b202b6SYOSHIFUJI Hideaki skb_reset_mac_header(skb); 33508b202b6SYOSHIFUJI Hideaki eth = eth_hdr(skb); 33608b202b6SYOSHIFUJI Hideaki 337e5a727f6SJoe Perches ether_addr_copy(eth->h_source, br->dev->dev_addr); 33808b202b6SYOSHIFUJI Hideaki eth->h_proto = htons(ETH_P_IPV6); 33908b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*eth)); 34008b202b6SYOSHIFUJI Hideaki 34108b202b6SYOSHIFUJI Hideaki /* IPv6 header + HbH option */ 34208b202b6SYOSHIFUJI Hideaki skb_set_network_header(skb, skb->len); 34308b202b6SYOSHIFUJI Hideaki ip6h = ipv6_hdr(skb); 34408b202b6SYOSHIFUJI Hideaki 34508b202b6SYOSHIFUJI Hideaki *(__force __be32 *)ip6h = htonl(0x60000000); 346aa2ae3e7SNikolay Aleksandrov ip6h->payload_len = htons(8 + mld_hdr_size); 34708b202b6SYOSHIFUJI Hideaki ip6h->nexthdr = IPPROTO_HOPOPTS; 34808b202b6SYOSHIFUJI Hideaki ip6h->hop_limit = 1; 349a7bff75bSLinus Lüssing ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1)); 350d1d81d4cSUlrich Weber if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0, 351d1d81d4cSUlrich Weber &ip6h->saddr)) { 352d1d81d4cSUlrich Weber kfree_skb(skb); 353675779adSNikolay Aleksandrov br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, false); 354d1d81d4cSUlrich Weber return NULL; 355d1d81d4cSUlrich Weber } 3560888d5f3Sdaniel 357675779adSNikolay Aleksandrov br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true); 35836cff5a1SLinus Lüssing ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest); 35908b202b6SYOSHIFUJI Hideaki 36008b202b6SYOSHIFUJI Hideaki hopopt = (u8 *)(ip6h + 1); 36108b202b6SYOSHIFUJI Hideaki hopopt[0] = IPPROTO_ICMPV6; /* next hdr */ 36208b202b6SYOSHIFUJI Hideaki hopopt[1] = 0; /* length of HbH */ 36308b202b6SYOSHIFUJI Hideaki hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */ 36408b202b6SYOSHIFUJI Hideaki hopopt[3] = 2; /* Length of RA Option */ 36508b202b6SYOSHIFUJI Hideaki hopopt[4] = 0; /* Type = 0x0000 (MLD) */ 36608b202b6SYOSHIFUJI Hideaki hopopt[5] = 0; 3671de5a71cSEldad Zack hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */ 3681de5a71cSEldad Zack hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */ 36908b202b6SYOSHIFUJI Hideaki 37008b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*ip6h) + 8); 37108b202b6SYOSHIFUJI Hideaki 37208b202b6SYOSHIFUJI Hideaki /* ICMPv6 */ 37308b202b6SYOSHIFUJI Hideaki skb_set_transport_header(skb, skb->len); 3741080ab95SNikolay Aleksandrov interval = ipv6_addr_any(grp) ? 37532de868cSLinus Lüssing br->multicast_query_response_interval : 37632de868cSLinus Lüssing br->multicast_last_member_interval; 3771080ab95SNikolay Aleksandrov *igmp_type = ICMPV6_MGM_QUERY; 378aa2ae3e7SNikolay Aleksandrov switch (br->multicast_mld_version) { 379aa2ae3e7SNikolay Aleksandrov case 1: 380aa2ae3e7SNikolay Aleksandrov mldq = (struct mld_msg *)icmp6_hdr(skb); 38108b202b6SYOSHIFUJI Hideaki mldq->mld_type = ICMPV6_MGM_QUERY; 38208b202b6SYOSHIFUJI Hideaki mldq->mld_code = 0; 38308b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = 0; 38408b202b6SYOSHIFUJI Hideaki mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval)); 38508b202b6SYOSHIFUJI Hideaki mldq->mld_reserved = 0; 3861080ab95SNikolay Aleksandrov mldq->mld_mca = *grp; 38708b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, 38808b202b6SYOSHIFUJI Hideaki sizeof(*mldq), IPPROTO_ICMPV6, 38908b202b6SYOSHIFUJI Hideaki csum_partial(mldq, 390aa2ae3e7SNikolay Aleksandrov sizeof(*mldq), 391aa2ae3e7SNikolay Aleksandrov 0)); 392aa2ae3e7SNikolay Aleksandrov break; 393aa2ae3e7SNikolay Aleksandrov case 2: 394aa2ae3e7SNikolay Aleksandrov mld2q = (struct mld2_query *)icmp6_hdr(skb); 39553631a5fSLance Richardson mld2q->mld2q_mrc = htons((u16)jiffies_to_msecs(interval)); 396aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_type = ICMPV6_MGM_QUERY; 397aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_code = 0; 398aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_cksum = 0; 399aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_resv1 = 0; 400aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_resv2 = 0; 401aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_suppress = 0; 402aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_qrv = 2; 403aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_nsrcs = 0; 404aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_qqic = br->multicast_query_interval / HZ; 405aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_mca = *grp; 406aa2ae3e7SNikolay Aleksandrov mld2q->mld2q_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, 407aa2ae3e7SNikolay Aleksandrov sizeof(*mld2q), 408aa2ae3e7SNikolay Aleksandrov IPPROTO_ICMPV6, 409aa2ae3e7SNikolay Aleksandrov csum_partial(mld2q, 410aa2ae3e7SNikolay Aleksandrov sizeof(*mld2q), 411aa2ae3e7SNikolay Aleksandrov 0)); 412aa2ae3e7SNikolay Aleksandrov break; 413aa2ae3e7SNikolay Aleksandrov } 414aa2ae3e7SNikolay Aleksandrov skb_put(skb, mld_hdr_size); 41508b202b6SYOSHIFUJI Hideaki 41608b202b6SYOSHIFUJI Hideaki __skb_pull(skb, sizeof(*eth)); 41708b202b6SYOSHIFUJI Hideaki 41808b202b6SYOSHIFUJI Hideaki out: 41908b202b6SYOSHIFUJI Hideaki return skb; 42008b202b6SYOSHIFUJI Hideaki } 42108b202b6SYOSHIFUJI Hideaki #endif 42208b202b6SYOSHIFUJI Hideaki 4238ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br, 4241080ab95SNikolay Aleksandrov struct br_ip *addr, 4251080ab95SNikolay Aleksandrov u8 *igmp_type) 4268ef2a9a5SYOSHIFUJI Hideaki { 4278ef2a9a5SYOSHIFUJI Hideaki switch (addr->proto) { 4288ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 4291080ab95SNikolay Aleksandrov return br_ip4_multicast_alloc_query(br, addr->u.ip4, igmp_type); 430dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 43108b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 4321080ab95SNikolay Aleksandrov return br_ip6_multicast_alloc_query(br, &addr->u.ip6, 4331080ab95SNikolay Aleksandrov igmp_type); 43408b202b6SYOSHIFUJI Hideaki #endif 4358ef2a9a5SYOSHIFUJI Hideaki } 4368ef2a9a5SYOSHIFUJI Hideaki return NULL; 4378ef2a9a5SYOSHIFUJI Hideaki } 4388ef2a9a5SYOSHIFUJI Hideaki 439cfd56754SCong Wang struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br, 4405e923585SNikolay Aleksandrov struct br_ip *group) 441eb1d1641SHerbert Xu { 442eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 4434c0833bcSTobias Klauser int err; 444eb1d1641SHerbert Xu 44519e3a9c9SNikolay Aleksandrov mp = br_mdb_ip_get(br, group); 44619e3a9c9SNikolay Aleksandrov if (mp) 44719e3a9c9SNikolay Aleksandrov return mp; 448eb1d1641SHerbert Xu 44919e3a9c9SNikolay Aleksandrov if (atomic_read(&br->mdb_hash_tbl.nelems) >= br->hash_max) { 45019e3a9c9SNikolay Aleksandrov br_opt_toggle(br, BROPT_MULTICAST_ENABLED, false); 45119e3a9c9SNikolay Aleksandrov return ERR_PTR(-E2BIG); 452eb1d1641SHerbert Xu } 453eb1d1641SHerbert Xu 454eb1d1641SHerbert Xu mp = kzalloc(sizeof(*mp), GFP_ATOMIC); 455eb1d1641SHerbert Xu if (unlikely(!mp)) 4564c0833bcSTobias Klauser return ERR_PTR(-ENOMEM); 457eb1d1641SHerbert Xu 458eb1d1641SHerbert Xu mp->br = br; 4598ef2a9a5SYOSHIFUJI Hideaki mp->addr = *group; 46088c1f37fSAllen Pais timer_setup(&mp->timer, br_multicast_group_expired, 0); 46119e3a9c9SNikolay Aleksandrov err = rhashtable_lookup_insert_fast(&br->mdb_hash_tbl, &mp->rhnode, 46219e3a9c9SNikolay Aleksandrov br_mdb_rht_params); 46319e3a9c9SNikolay Aleksandrov if (err) { 46419e3a9c9SNikolay Aleksandrov kfree(mp); 46519e3a9c9SNikolay Aleksandrov mp = ERR_PTR(err); 46619e3a9c9SNikolay Aleksandrov } else { 46719e3a9c9SNikolay Aleksandrov hlist_add_head_rcu(&mp->mdb_node, &br->mdb_list); 46819e3a9c9SNikolay Aleksandrov } 4691faabf2aSEric Dumazet 470eb1d1641SHerbert Xu return mp; 471eb1d1641SHerbert Xu } 472eb1d1641SHerbert Xu 473cfd56754SCong Wang struct net_bridge_port_group *br_multicast_new_port_group( 474cfd56754SCong Wang struct net_bridge_port *port, 475cfd56754SCong Wang struct br_ip *group, 476ccb1c31aSAmerigo Wang struct net_bridge_port_group __rcu *next, 4776db6f0eaSFelix Fietkau unsigned char flags, 4786db6f0eaSFelix Fietkau const unsigned char *src) 479cfd56754SCong Wang { 480cfd56754SCong Wang struct net_bridge_port_group *p; 481cfd56754SCong Wang 482cfd56754SCong Wang p = kzalloc(sizeof(*p), GFP_ATOMIC); 483cfd56754SCong Wang if (unlikely(!p)) 484cfd56754SCong Wang return NULL; 485cfd56754SCong Wang 486cfd56754SCong Wang p->addr = *group; 487cfd56754SCong Wang p->port = port; 4889d06b6d8SElad Raz p->flags = flags; 489eca2a43bSstephen hemminger rcu_assign_pointer(p->next, next); 490cfd56754SCong Wang hlist_add_head(&p->mglist, &port->mglist); 49188c1f37fSAllen Pais timer_setup(&p->timer, br_multicast_port_group_expired, 0); 4926db6f0eaSFelix Fietkau 4936db6f0eaSFelix Fietkau if (src) 4946db6f0eaSFelix Fietkau memcpy(p->eth_addr, src, ETH_ALEN); 4956db6f0eaSFelix Fietkau else 4961bfe45f4SMao Wenan eth_broadcast_addr(p->eth_addr); 4976db6f0eaSFelix Fietkau 498cfd56754SCong Wang return p; 499cfd56754SCong Wang } 500cfd56754SCong Wang 5016db6f0eaSFelix Fietkau static bool br_port_group_equal(struct net_bridge_port_group *p, 5026db6f0eaSFelix Fietkau struct net_bridge_port *port, 5036db6f0eaSFelix Fietkau const unsigned char *src) 5046db6f0eaSFelix Fietkau { 5056db6f0eaSFelix Fietkau if (p->port != port) 5066db6f0eaSFelix Fietkau return false; 5076db6f0eaSFelix Fietkau 5086db6f0eaSFelix Fietkau if (!(port->flags & BR_MULTICAST_TO_UNICAST)) 5096db6f0eaSFelix Fietkau return true; 5106db6f0eaSFelix Fietkau 5116db6f0eaSFelix Fietkau return ether_addr_equal(src, p->eth_addr); 5126db6f0eaSFelix Fietkau } 5136db6f0eaSFelix Fietkau 514*1bc844eeSNikolay Aleksandrov void br_multicast_host_join(struct net_bridge_mdb_entry *mp, bool notify) 515*1bc844eeSNikolay Aleksandrov { 516*1bc844eeSNikolay Aleksandrov if (!mp->host_joined) { 517*1bc844eeSNikolay Aleksandrov mp->host_joined = true; 518*1bc844eeSNikolay Aleksandrov if (notify) 519*1bc844eeSNikolay Aleksandrov br_mdb_notify(mp->br->dev, NULL, &mp->addr, 520*1bc844eeSNikolay Aleksandrov RTM_NEWMDB, 0); 521*1bc844eeSNikolay Aleksandrov } 522*1bc844eeSNikolay Aleksandrov mod_timer(&mp->timer, jiffies + mp->br->multicast_membership_interval); 523*1bc844eeSNikolay Aleksandrov } 524*1bc844eeSNikolay Aleksandrov 525*1bc844eeSNikolay Aleksandrov void br_multicast_host_leave(struct net_bridge_mdb_entry *mp, bool notify) 526*1bc844eeSNikolay Aleksandrov { 527*1bc844eeSNikolay Aleksandrov if (!mp->host_joined) 528*1bc844eeSNikolay Aleksandrov return; 529*1bc844eeSNikolay Aleksandrov 530*1bc844eeSNikolay Aleksandrov mp->host_joined = false; 531*1bc844eeSNikolay Aleksandrov if (notify) 532*1bc844eeSNikolay Aleksandrov br_mdb_notify(mp->br->dev, NULL, &mp->addr, RTM_DELMDB, 0); 533*1bc844eeSNikolay Aleksandrov } 534*1bc844eeSNikolay Aleksandrov 535eb1d1641SHerbert Xu static int br_multicast_add_group(struct net_bridge *br, 5368ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 5376db6f0eaSFelix Fietkau struct br_ip *group, 5386db6f0eaSFelix Fietkau const unsigned char *src) 539eb1d1641SHerbert Xu { 540e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 5415e923585SNikolay Aleksandrov struct net_bridge_port_group *p; 5425e923585SNikolay Aleksandrov struct net_bridge_mdb_entry *mp; 543454594f3SLinus Lüssing unsigned long now = jiffies; 544eb1d1641SHerbert Xu int err; 545eb1d1641SHerbert Xu 546eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 547eb1d1641SHerbert Xu if (!netif_running(br->dev) || 548eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 549eb1d1641SHerbert Xu goto out; 550eb1d1641SHerbert Xu 55119e3a9c9SNikolay Aleksandrov mp = br_multicast_new_group(br, group); 552eb1d1641SHerbert Xu err = PTR_ERR(mp); 5534c0833bcSTobias Klauser if (IS_ERR(mp)) 554eb1d1641SHerbert Xu goto err; 555eb1d1641SHerbert Xu 556eb1d1641SHerbert Xu if (!port) { 557*1bc844eeSNikolay Aleksandrov br_multicast_host_join(mp, true); 558eb1d1641SHerbert Xu goto out; 559eb1d1641SHerbert Xu } 560eb1d1641SHerbert Xu 561e8051688SEric Dumazet for (pp = &mp->ports; 562e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 563e8051688SEric Dumazet pp = &p->next) { 5646db6f0eaSFelix Fietkau if (br_port_group_equal(p, port, src)) 565454594f3SLinus Lüssing goto found; 566eb1d1641SHerbert Xu if ((unsigned long)p->port < (unsigned long)port) 567eb1d1641SHerbert Xu break; 568eb1d1641SHerbert Xu } 569eb1d1641SHerbert Xu 5706db6f0eaSFelix Fietkau p = br_multicast_new_port_group(port, group, *pp, 0, src); 571eb1d1641SHerbert Xu if (unlikely(!p)) 572eb1d1641SHerbert Xu goto err; 573eb1d1641SHerbert Xu rcu_assign_pointer(*pp, p); 57445ebcce5SElad Raz br_mdb_notify(br->dev, port, group, RTM_NEWMDB, 0); 575eb1d1641SHerbert Xu 576454594f3SLinus Lüssing found: 577454594f3SLinus Lüssing mod_timer(&p->timer, now + br->multicast_membership_interval); 578eb1d1641SHerbert Xu out: 579eb1d1641SHerbert Xu err = 0; 580eb1d1641SHerbert Xu 581eb1d1641SHerbert Xu err: 582eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 583eb1d1641SHerbert Xu return err; 584eb1d1641SHerbert Xu } 585eb1d1641SHerbert Xu 5868ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_add_group(struct net_bridge *br, 5878ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 588b0e9a30dSVlad Yasevich __be32 group, 5896db6f0eaSFelix Fietkau __u16 vid, 5906db6f0eaSFelix Fietkau const unsigned char *src) 5918ef2a9a5SYOSHIFUJI Hideaki { 5928ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 5938ef2a9a5SYOSHIFUJI Hideaki 5948ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 5958ef2a9a5SYOSHIFUJI Hideaki return 0; 5968ef2a9a5SYOSHIFUJI Hideaki 5971515a63fSNikolay Aleksandrov memset(&br_group, 0, sizeof(br_group)); 5988ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 5998ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 600b0e9a30dSVlad Yasevich br_group.vid = vid; 6018ef2a9a5SYOSHIFUJI Hideaki 6026db6f0eaSFelix Fietkau return br_multicast_add_group(br, port, &br_group, src); 6038ef2a9a5SYOSHIFUJI Hideaki } 6048ef2a9a5SYOSHIFUJI Hideaki 605dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 60608b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_add_group(struct net_bridge *br, 60708b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 608b0e9a30dSVlad Yasevich const struct in6_addr *group, 6096db6f0eaSFelix Fietkau __u16 vid, 6106db6f0eaSFelix Fietkau const unsigned char *src) 61108b202b6SYOSHIFUJI Hideaki { 61208b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 61308b202b6SYOSHIFUJI Hideaki 6143c3769e6SLinus Lüssing if (ipv6_addr_is_ll_all_nodes(group)) 61508b202b6SYOSHIFUJI Hideaki return 0; 61608b202b6SYOSHIFUJI Hideaki 61719e3a9c9SNikolay Aleksandrov memset(&br_group, 0, sizeof(br_group)); 6184e3fd7a0SAlexey Dobriyan br_group.u.ip6 = *group; 6199cc6e0c4SLinus Lüssing br_group.proto = htons(ETH_P_IPV6); 620b0e9a30dSVlad Yasevich br_group.vid = vid; 62108b202b6SYOSHIFUJI Hideaki 6226db6f0eaSFelix Fietkau return br_multicast_add_group(br, port, &br_group, src); 62308b202b6SYOSHIFUJI Hideaki } 62408b202b6SYOSHIFUJI Hideaki #endif 62508b202b6SYOSHIFUJI Hideaki 62688c1f37fSAllen Pais static void br_multicast_router_expired(struct timer_list *t) 627eb1d1641SHerbert Xu { 62888c1f37fSAllen Pais struct net_bridge_port *port = 62988c1f37fSAllen Pais from_timer(port, t, multicast_router_timer); 630eb1d1641SHerbert Xu struct net_bridge *br = port->br; 631eb1d1641SHerbert Xu 632eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 633a55d8246SNikolay Aleksandrov if (port->multicast_router == MDB_RTR_TYPE_DISABLED || 634a55d8246SNikolay Aleksandrov port->multicast_router == MDB_RTR_TYPE_PERM || 635f12e7d95SNogah Frankel timer_pending(&port->multicast_router_timer)) 636eb1d1641SHerbert Xu goto out; 637eb1d1641SHerbert Xu 638f12e7d95SNogah Frankel __del_port_router(port); 639eb1d1641SHerbert Xu out: 640eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 641eb1d1641SHerbert Xu } 642eb1d1641SHerbert Xu 64377041420SYotam Gigi static void br_mc_router_state_change(struct net_bridge *p, 64477041420SYotam Gigi bool is_mc_router) 64577041420SYotam Gigi { 64677041420SYotam Gigi struct switchdev_attr attr = { 64777041420SYotam Gigi .orig_dev = p->dev, 64877041420SYotam Gigi .id = SWITCHDEV_ATTR_ID_BRIDGE_MROUTER, 64977041420SYotam Gigi .flags = SWITCHDEV_F_DEFER, 65077041420SYotam Gigi .u.mrouter = is_mc_router, 65177041420SYotam Gigi }; 65277041420SYotam Gigi 65377041420SYotam Gigi switchdev_port_attr_set(p->dev, &attr); 65477041420SYotam Gigi } 65577041420SYotam Gigi 65688c1f37fSAllen Pais static void br_multicast_local_router_expired(struct timer_list *t) 657eb1d1641SHerbert Xu { 65888c1f37fSAllen Pais struct net_bridge *br = from_timer(br, t, multicast_router_timer); 65977041420SYotam Gigi 66077041420SYotam Gigi spin_lock(&br->multicast_lock); 66177041420SYotam Gigi if (br->multicast_router == MDB_RTR_TYPE_DISABLED || 66277041420SYotam Gigi br->multicast_router == MDB_RTR_TYPE_PERM || 66377041420SYotam Gigi timer_pending(&br->multicast_router_timer)) 66477041420SYotam Gigi goto out; 66577041420SYotam Gigi 66677041420SYotam Gigi br_mc_router_state_change(br, false); 66777041420SYotam Gigi out: 66877041420SYotam Gigi spin_unlock(&br->multicast_lock); 669eb1d1641SHerbert Xu } 670eb1d1641SHerbert Xu 671cc0fdd80SLinus Lüssing static void br_multicast_querier_expired(struct net_bridge *br, 67290010b36SLinus Lüssing struct bridge_mcast_own_query *query) 673c83b8fabSHerbert Xu { 674c83b8fabSHerbert Xu spin_lock(&br->multicast_lock); 67513cefad2SNikolay Aleksandrov if (!netif_running(br->dev) || !br_opt_get(br, BROPT_MULTICAST_ENABLED)) 676c83b8fabSHerbert Xu goto out; 677c83b8fabSHerbert Xu 678cc0fdd80SLinus Lüssing br_multicast_start_querier(br, query); 679c83b8fabSHerbert Xu 680c83b8fabSHerbert Xu out: 681c83b8fabSHerbert Xu spin_unlock(&br->multicast_lock); 682c83b8fabSHerbert Xu } 683c83b8fabSHerbert Xu 68488c1f37fSAllen Pais static void br_ip4_multicast_querier_expired(struct timer_list *t) 685cc0fdd80SLinus Lüssing { 68688c1f37fSAllen Pais struct net_bridge *br = from_timer(br, t, ip4_other_query.timer); 687cc0fdd80SLinus Lüssing 68890010b36SLinus Lüssing br_multicast_querier_expired(br, &br->ip4_own_query); 689cc0fdd80SLinus Lüssing } 690cc0fdd80SLinus Lüssing 691cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 69288c1f37fSAllen Pais static void br_ip6_multicast_querier_expired(struct timer_list *t) 693cc0fdd80SLinus Lüssing { 69488c1f37fSAllen Pais struct net_bridge *br = from_timer(br, t, ip6_other_query.timer); 695cc0fdd80SLinus Lüssing 69690010b36SLinus Lüssing br_multicast_querier_expired(br, &br->ip6_own_query); 697cc0fdd80SLinus Lüssing } 698cc0fdd80SLinus Lüssing #endif 699cc0fdd80SLinus Lüssing 700dc4eb53aSLinus Lüssing static void br_multicast_select_own_querier(struct net_bridge *br, 701dc4eb53aSLinus Lüssing struct br_ip *ip, 702dc4eb53aSLinus Lüssing struct sk_buff *skb) 703dc4eb53aSLinus Lüssing { 704dc4eb53aSLinus Lüssing if (ip->proto == htons(ETH_P_IP)) 705dc4eb53aSLinus Lüssing br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr; 706dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 707dc4eb53aSLinus Lüssing else 708dc4eb53aSLinus Lüssing br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr; 709dc4eb53aSLinus Lüssing #endif 710dc4eb53aSLinus Lüssing } 711dc4eb53aSLinus Lüssing 7128ef2a9a5SYOSHIFUJI Hideaki static void __br_multicast_send_query(struct net_bridge *br, 7138ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 7148ef2a9a5SYOSHIFUJI Hideaki struct br_ip *ip) 715eb1d1641SHerbert Xu { 716eb1d1641SHerbert Xu struct sk_buff *skb; 7171080ab95SNikolay Aleksandrov u8 igmp_type; 718eb1d1641SHerbert Xu 7191080ab95SNikolay Aleksandrov skb = br_multicast_alloc_query(br, ip, &igmp_type); 720eb1d1641SHerbert Xu if (!skb) 7218ef2a9a5SYOSHIFUJI Hideaki return; 722eb1d1641SHerbert Xu 723eb1d1641SHerbert Xu if (port) { 724eb1d1641SHerbert Xu skb->dev = port->dev; 725a65056ecSNikolay Aleksandrov br_multicast_count(br, port, skb, igmp_type, 7261080ab95SNikolay Aleksandrov BR_MCAST_DIR_TX); 72729a26a56SEric W. Biederman NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, 72829a26a56SEric W. Biederman dev_net(port->dev), NULL, skb, NULL, skb->dev, 729f0b4eeceSLinus Lüssing br_dev_queue_push_xmit); 730dc4eb53aSLinus Lüssing } else { 731dc4eb53aSLinus Lüssing br_multicast_select_own_querier(br, ip, skb); 732a65056ecSNikolay Aleksandrov br_multicast_count(br, port, skb, igmp_type, 7331080ab95SNikolay Aleksandrov BR_MCAST_DIR_RX); 734eb1d1641SHerbert Xu netif_rx(skb); 7358ef2a9a5SYOSHIFUJI Hideaki } 736dc4eb53aSLinus Lüssing } 737eb1d1641SHerbert Xu 7388ef2a9a5SYOSHIFUJI Hideaki static void br_multicast_send_query(struct net_bridge *br, 739cc0fdd80SLinus Lüssing struct net_bridge_port *port, 74090010b36SLinus Lüssing struct bridge_mcast_own_query *own_query) 7418ef2a9a5SYOSHIFUJI Hideaki { 74290010b36SLinus Lüssing struct bridge_mcast_other_query *other_query = NULL; 7435e923585SNikolay Aleksandrov struct br_ip br_group; 7445e923585SNikolay Aleksandrov unsigned long time; 7458ef2a9a5SYOSHIFUJI Hideaki 74613cefad2SNikolay Aleksandrov if (!netif_running(br->dev) || 74713cefad2SNikolay Aleksandrov !br_opt_get(br, BROPT_MULTICAST_ENABLED) || 748675779adSNikolay Aleksandrov !br_opt_get(br, BROPT_MULTICAST_QUERIER)) 7498ef2a9a5SYOSHIFUJI Hideaki return; 7508ef2a9a5SYOSHIFUJI Hideaki 75108b202b6SYOSHIFUJI Hideaki memset(&br_group.u, 0, sizeof(br_group.u)); 7528ef2a9a5SYOSHIFUJI Hideaki 75390010b36SLinus Lüssing if (port ? (own_query == &port->ip4_own_query) : 75490010b36SLinus Lüssing (own_query == &br->ip4_own_query)) { 75590010b36SLinus Lüssing other_query = &br->ip4_other_query; 75608b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 757dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 758cc0fdd80SLinus Lüssing } else { 75990010b36SLinus Lüssing other_query = &br->ip6_other_query; 76008b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 76108b202b6SYOSHIFUJI Hideaki #endif 762eb1d1641SHerbert Xu } 763eb1d1641SHerbert Xu 76490010b36SLinus Lüssing if (!other_query || timer_pending(&other_query->timer)) 765cc0fdd80SLinus Lüssing return; 766cc0fdd80SLinus Lüssing 767cc0fdd80SLinus Lüssing __br_multicast_send_query(br, port, &br_group); 768cc0fdd80SLinus Lüssing 769cc0fdd80SLinus Lüssing time = jiffies; 77090010b36SLinus Lüssing time += own_query->startup_sent < br->multicast_startup_query_count ? 771cc0fdd80SLinus Lüssing br->multicast_startup_query_interval : 772cc0fdd80SLinus Lüssing br->multicast_query_interval; 77390010b36SLinus Lüssing mod_timer(&own_query->timer, time); 774cc0fdd80SLinus Lüssing } 775cc0fdd80SLinus Lüssing 77690010b36SLinus Lüssing static void 77790010b36SLinus Lüssing br_multicast_port_query_expired(struct net_bridge_port *port, 77890010b36SLinus Lüssing struct bridge_mcast_own_query *query) 779eb1d1641SHerbert Xu { 780eb1d1641SHerbert Xu struct net_bridge *br = port->br; 781eb1d1641SHerbert Xu 782eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 78302a780c0SDan Carpenter if (port->state == BR_STATE_DISABLED || 78402a780c0SDan Carpenter port->state == BR_STATE_BLOCKING) 785eb1d1641SHerbert Xu goto out; 786eb1d1641SHerbert Xu 787cc0fdd80SLinus Lüssing if (query->startup_sent < br->multicast_startup_query_count) 788cc0fdd80SLinus Lüssing query->startup_sent++; 789eb1d1641SHerbert Xu 790cc0fdd80SLinus Lüssing br_multicast_send_query(port->br, port, query); 791eb1d1641SHerbert Xu 792eb1d1641SHerbert Xu out: 793eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 794eb1d1641SHerbert Xu } 795eb1d1641SHerbert Xu 79688c1f37fSAllen Pais static void br_ip4_multicast_port_query_expired(struct timer_list *t) 797cc0fdd80SLinus Lüssing { 79888c1f37fSAllen Pais struct net_bridge_port *port = from_timer(port, t, ip4_own_query.timer); 799cc0fdd80SLinus Lüssing 80090010b36SLinus Lüssing br_multicast_port_query_expired(port, &port->ip4_own_query); 801cc0fdd80SLinus Lüssing } 802cc0fdd80SLinus Lüssing 803cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 80488c1f37fSAllen Pais static void br_ip6_multicast_port_query_expired(struct timer_list *t) 805cc0fdd80SLinus Lüssing { 80688c1f37fSAllen Pais struct net_bridge_port *port = from_timer(port, t, ip6_own_query.timer); 807cc0fdd80SLinus Lüssing 80890010b36SLinus Lüssing br_multicast_port_query_expired(port, &port->ip6_own_query); 809cc0fdd80SLinus Lüssing } 810cc0fdd80SLinus Lüssing #endif 811cc0fdd80SLinus Lüssing 812147c1e9bSNogah Frankel static void br_mc_disabled_update(struct net_device *dev, bool value) 813147c1e9bSNogah Frankel { 814147c1e9bSNogah Frankel struct switchdev_attr attr = { 815147c1e9bSNogah Frankel .orig_dev = dev, 816147c1e9bSNogah Frankel .id = SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED, 817147c1e9bSNogah Frankel .flags = SWITCHDEV_F_DEFER, 81813cefad2SNikolay Aleksandrov .u.mc_disabled = !value, 819147c1e9bSNogah Frankel }; 820147c1e9bSNogah Frankel 821147c1e9bSNogah Frankel switchdev_port_attr_set(dev, &attr); 822147c1e9bSNogah Frankel } 823147c1e9bSNogah Frankel 8241080ab95SNikolay Aleksandrov int br_multicast_add_port(struct net_bridge_port *port) 825eb1d1641SHerbert Xu { 8267f0aec7aSNikolay Aleksandrov port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 827eb1d1641SHerbert Xu 82888c1f37fSAllen Pais timer_setup(&port->multicast_router_timer, 82988c1f37fSAllen Pais br_multicast_router_expired, 0); 83088c1f37fSAllen Pais timer_setup(&port->ip4_own_query.timer, 83188c1f37fSAllen Pais br_ip4_multicast_port_query_expired, 0); 832cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 83388c1f37fSAllen Pais timer_setup(&port->ip6_own_query.timer, 83488c1f37fSAllen Pais br_ip6_multicast_port_query_expired, 0); 835cc0fdd80SLinus Lüssing #endif 83613cefad2SNikolay Aleksandrov br_mc_disabled_update(port->dev, 83713cefad2SNikolay Aleksandrov br_opt_get(port->br, BROPT_MULTICAST_ENABLED)); 838147c1e9bSNogah Frankel 8391080ab95SNikolay Aleksandrov port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats); 8401080ab95SNikolay Aleksandrov if (!port->mcast_stats) 8411080ab95SNikolay Aleksandrov return -ENOMEM; 8421080ab95SNikolay Aleksandrov 8431080ab95SNikolay Aleksandrov return 0; 844eb1d1641SHerbert Xu } 845eb1d1641SHerbert Xu 846eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port) 847eb1d1641SHerbert Xu { 848e10177abSSatish Ashok struct net_bridge *br = port->br; 849e10177abSSatish Ashok struct net_bridge_port_group *pg; 850e10177abSSatish Ashok struct hlist_node *n; 851e10177abSSatish Ashok 852e10177abSSatish Ashok /* Take care of the remaining groups, only perm ones should be left */ 853e10177abSSatish Ashok spin_lock_bh(&br->multicast_lock); 854e10177abSSatish Ashok hlist_for_each_entry_safe(pg, n, &port->mglist, mglist) 855e10177abSSatish Ashok br_multicast_del_pg(br, pg); 856e10177abSSatish Ashok spin_unlock_bh(&br->multicast_lock); 857eb1d1641SHerbert Xu del_timer_sync(&port->multicast_router_timer); 8581080ab95SNikolay Aleksandrov free_percpu(port->mcast_stats); 859eb1d1641SHerbert Xu } 860eb1d1641SHerbert Xu 86190010b36SLinus Lüssing static void br_multicast_enable(struct bridge_mcast_own_query *query) 862561f1103SHerbert Xu { 863cc0fdd80SLinus Lüssing query->startup_sent = 0; 864561f1103SHerbert Xu 865cc0fdd80SLinus Lüssing if (try_to_del_timer_sync(&query->timer) >= 0 || 866cc0fdd80SLinus Lüssing del_timer(&query->timer)) 867cc0fdd80SLinus Lüssing mod_timer(&query->timer, jiffies); 868561f1103SHerbert Xu } 869561f1103SHerbert Xu 8707cb3f921SNikolay Aleksandrov static void __br_multicast_enable_port(struct net_bridge_port *port) 871eb1d1641SHerbert Xu { 872eb1d1641SHerbert Xu struct net_bridge *br = port->br; 873eb1d1641SHerbert Xu 87413cefad2SNikolay Aleksandrov if (!br_opt_get(br, BROPT_MULTICAST_ENABLED) || !netif_running(br->dev)) 8757cb3f921SNikolay Aleksandrov return; 876eb1d1641SHerbert Xu 87790010b36SLinus Lüssing br_multicast_enable(&port->ip4_own_query); 878cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 87990010b36SLinus Lüssing br_multicast_enable(&port->ip6_own_query); 880cc0fdd80SLinus Lüssing #endif 8817f0aec7aSNikolay Aleksandrov if (port->multicast_router == MDB_RTR_TYPE_PERM && 8827f0aec7aSNikolay Aleksandrov hlist_unhashed(&port->rlist)) 883754bc547SSatish Ashok br_multicast_add_router(br, port); 8847cb3f921SNikolay Aleksandrov } 885eb1d1641SHerbert Xu 8867cb3f921SNikolay Aleksandrov void br_multicast_enable_port(struct net_bridge_port *port) 8877cb3f921SNikolay Aleksandrov { 8887cb3f921SNikolay Aleksandrov struct net_bridge *br = port->br; 8897cb3f921SNikolay Aleksandrov 8907cb3f921SNikolay Aleksandrov spin_lock(&br->multicast_lock); 8917cb3f921SNikolay Aleksandrov __br_multicast_enable_port(port); 892eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 893eb1d1641SHerbert Xu } 894eb1d1641SHerbert Xu 895eb1d1641SHerbert Xu void br_multicast_disable_port(struct net_bridge_port *port) 896eb1d1641SHerbert Xu { 897eb1d1641SHerbert Xu struct net_bridge *br = port->br; 898eb1d1641SHerbert Xu struct net_bridge_port_group *pg; 899b67bfe0dSSasha Levin struct hlist_node *n; 900eb1d1641SHerbert Xu 901eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 902b67bfe0dSSasha Levin hlist_for_each_entry_safe(pg, n, &port->mglist, mglist) 9039d06b6d8SElad Raz if (!(pg->flags & MDB_PG_FLAGS_PERMANENT)) 904eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 905eb1d1641SHerbert Xu 906f12e7d95SNogah Frankel __del_port_router(port); 907f12e7d95SNogah Frankel 908eb1d1641SHerbert Xu del_timer(&port->multicast_router_timer); 90990010b36SLinus Lüssing del_timer(&port->ip4_own_query.timer); 910cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 91190010b36SLinus Lüssing del_timer(&port->ip6_own_query.timer); 912cc0fdd80SLinus Lüssing #endif 913eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 914eb1d1641SHerbert Xu } 915eb1d1641SHerbert Xu 9168ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_igmp3_report(struct net_bridge *br, 917eb1d1641SHerbert Xu struct net_bridge_port *port, 91806499098SVlad Yasevich struct sk_buff *skb, 91906499098SVlad Yasevich u16 vid) 920eb1d1641SHerbert Xu { 9216db6f0eaSFelix Fietkau const unsigned char *src; 922eb1d1641SHerbert Xu struct igmpv3_report *ih; 923eb1d1641SHerbert Xu struct igmpv3_grec *grec; 924eb1d1641SHerbert Xu int i; 925eb1d1641SHerbert Xu int len; 926eb1d1641SHerbert Xu int num; 927eb1d1641SHerbert Xu int type; 928eb1d1641SHerbert Xu int err = 0; 929eb1d1641SHerbert Xu __be32 group; 930e57f6185SNikolay Aleksandrov u16 nsrcs; 931eb1d1641SHerbert Xu 932eb1d1641SHerbert Xu ih = igmpv3_report_hdr(skb); 933eb1d1641SHerbert Xu num = ntohs(ih->ngrec); 934c2d4fbd2SLinus Lüssing len = skb_transport_offset(skb) + sizeof(*ih); 935eb1d1641SHerbert Xu 936eb1d1641SHerbert Xu for (i = 0; i < num; i++) { 937eb1d1641SHerbert Xu len += sizeof(*grec); 938ba5ea614SLinus Lüssing if (!ip_mc_may_pull(skb, len)) 939eb1d1641SHerbert Xu return -EINVAL; 940eb1d1641SHerbert Xu 941fd218cf9SHerbert Xu grec = (void *)(skb->data + len - sizeof(*grec)); 942eb1d1641SHerbert Xu group = grec->grec_mca; 943eb1d1641SHerbert Xu type = grec->grec_type; 944e57f6185SNikolay Aleksandrov nsrcs = ntohs(grec->grec_nsrcs); 945eb1d1641SHerbert Xu 946e57f6185SNikolay Aleksandrov len += nsrcs * 4; 947ba5ea614SLinus Lüssing if (!ip_mc_may_pull(skb, len)) 948eb1d1641SHerbert Xu return -EINVAL; 949eb1d1641SHerbert Xu 950eb1d1641SHerbert Xu /* We treat this as an IGMPv2 report for now. */ 951eb1d1641SHerbert Xu switch (type) { 952eb1d1641SHerbert Xu case IGMPV3_MODE_IS_INCLUDE: 953eb1d1641SHerbert Xu case IGMPV3_MODE_IS_EXCLUDE: 954eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_INCLUDE: 955eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_EXCLUDE: 956eb1d1641SHerbert Xu case IGMPV3_ALLOW_NEW_SOURCES: 957eb1d1641SHerbert Xu case IGMPV3_BLOCK_OLD_SOURCES: 958eb1d1641SHerbert Xu break; 959eb1d1641SHerbert Xu 960eb1d1641SHerbert Xu default: 961eb1d1641SHerbert Xu continue; 962eb1d1641SHerbert Xu } 963eb1d1641SHerbert Xu 9646db6f0eaSFelix Fietkau src = eth_hdr(skb)->h_source; 965bc8c20acSSatish Ashok if ((type == IGMPV3_CHANGE_TO_INCLUDE || 966bc8c20acSSatish Ashok type == IGMPV3_MODE_IS_INCLUDE) && 967e57f6185SNikolay Aleksandrov nsrcs == 0) { 9686db6f0eaSFelix Fietkau br_ip4_multicast_leave_group(br, port, group, vid, src); 969bc8c20acSSatish Ashok } else { 9706db6f0eaSFelix Fietkau err = br_ip4_multicast_add_group(br, port, group, vid, 9716db6f0eaSFelix Fietkau src); 972eb1d1641SHerbert Xu if (err) 973eb1d1641SHerbert Xu break; 974eb1d1641SHerbert Xu } 975bc8c20acSSatish Ashok } 976eb1d1641SHerbert Xu 977eb1d1641SHerbert Xu return err; 978eb1d1641SHerbert Xu } 979eb1d1641SHerbert Xu 980dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 98108b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_mld2_report(struct net_bridge *br, 98208b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 98306499098SVlad Yasevich struct sk_buff *skb, 98406499098SVlad Yasevich u16 vid) 98508b202b6SYOSHIFUJI Hideaki { 986ba5ea614SLinus Lüssing unsigned int nsrcs_offset; 9876db6f0eaSFelix Fietkau const unsigned char *src; 98808b202b6SYOSHIFUJI Hideaki struct icmp6hdr *icmp6h; 98908b202b6SYOSHIFUJI Hideaki struct mld2_grec *grec; 990ba5ea614SLinus Lüssing unsigned int grec_len; 99108b202b6SYOSHIFUJI Hideaki int i; 99208b202b6SYOSHIFUJI Hideaki int len; 99308b202b6SYOSHIFUJI Hideaki int num; 99408b202b6SYOSHIFUJI Hideaki int err = 0; 99508b202b6SYOSHIFUJI Hideaki 996ba5ea614SLinus Lüssing if (!ipv6_mc_may_pull(skb, sizeof(*icmp6h))) 99708b202b6SYOSHIFUJI Hideaki return -EINVAL; 99808b202b6SYOSHIFUJI Hideaki 99908b202b6SYOSHIFUJI Hideaki icmp6h = icmp6_hdr(skb); 100008b202b6SYOSHIFUJI Hideaki num = ntohs(icmp6h->icmp6_dataun.un_data16[1]); 1001c2d4fbd2SLinus Lüssing len = skb_transport_offset(skb) + sizeof(*icmp6h); 100208b202b6SYOSHIFUJI Hideaki 100308b202b6SYOSHIFUJI Hideaki for (i = 0; i < num; i++) { 1004e57f6185SNikolay Aleksandrov __be16 *_nsrcs, __nsrcs; 1005e57f6185SNikolay Aleksandrov u16 nsrcs; 100608b202b6SYOSHIFUJI Hideaki 1007ba5ea614SLinus Lüssing nsrcs_offset = len + offsetof(struct mld2_grec, grec_nsrcs); 1008ba5ea614SLinus Lüssing 1009ba5ea614SLinus Lüssing if (skb_transport_offset(skb) + ipv6_transport_len(skb) < 1010ba5ea614SLinus Lüssing nsrcs_offset + sizeof(_nsrcs)) 1011ba5ea614SLinus Lüssing return -EINVAL; 1012ba5ea614SLinus Lüssing 1013e57f6185SNikolay Aleksandrov _nsrcs = skb_header_pointer(skb, nsrcs_offset, 1014e57f6185SNikolay Aleksandrov sizeof(__nsrcs), &__nsrcs); 1015e57f6185SNikolay Aleksandrov if (!_nsrcs) 101608b202b6SYOSHIFUJI Hideaki return -EINVAL; 101708b202b6SYOSHIFUJI Hideaki 1018e57f6185SNikolay Aleksandrov nsrcs = ntohs(*_nsrcs); 1019e57f6185SNikolay Aleksandrov grec_len = struct_size(grec, grec_src, nsrcs); 1020ba5ea614SLinus Lüssing 1021ba5ea614SLinus Lüssing if (!ipv6_mc_may_pull(skb, len + grec_len)) 102208b202b6SYOSHIFUJI Hideaki return -EINVAL; 102308b202b6SYOSHIFUJI Hideaki 102408b202b6SYOSHIFUJI Hideaki grec = (struct mld2_grec *)(skb->data + len); 1025ba5ea614SLinus Lüssing len += grec_len; 102608b202b6SYOSHIFUJI Hideaki 102708b202b6SYOSHIFUJI Hideaki /* We treat these as MLDv1 reports for now. */ 102808b202b6SYOSHIFUJI Hideaki switch (grec->grec_type) { 102908b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_INCLUDE: 103008b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_EXCLUDE: 103108b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_INCLUDE: 103208b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_EXCLUDE: 103308b202b6SYOSHIFUJI Hideaki case MLD2_ALLOW_NEW_SOURCES: 103408b202b6SYOSHIFUJI Hideaki case MLD2_BLOCK_OLD_SOURCES: 103508b202b6SYOSHIFUJI Hideaki break; 103608b202b6SYOSHIFUJI Hideaki 103708b202b6SYOSHIFUJI Hideaki default: 103808b202b6SYOSHIFUJI Hideaki continue; 103908b202b6SYOSHIFUJI Hideaki } 104008b202b6SYOSHIFUJI Hideaki 10416db6f0eaSFelix Fietkau src = eth_hdr(skb)->h_source; 1042bc8c20acSSatish Ashok if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE || 1043bc8c20acSSatish Ashok grec->grec_type == MLD2_MODE_IS_INCLUDE) && 1044e57f6185SNikolay Aleksandrov nsrcs == 0) { 1045bc8c20acSSatish Ashok br_ip6_multicast_leave_group(br, port, &grec->grec_mca, 10466db6f0eaSFelix Fietkau vid, src); 1047bc8c20acSSatish Ashok } else { 1048bc8c20acSSatish Ashok err = br_ip6_multicast_add_group(br, port, 10496db6f0eaSFelix Fietkau &grec->grec_mca, vid, 10506db6f0eaSFelix Fietkau src); 10519264251eSDavide Caratti if (err) 105208b202b6SYOSHIFUJI Hideaki break; 105308b202b6SYOSHIFUJI Hideaki } 1054bc8c20acSSatish Ashok } 105508b202b6SYOSHIFUJI Hideaki 105608b202b6SYOSHIFUJI Hideaki return err; 105708b202b6SYOSHIFUJI Hideaki } 105808b202b6SYOSHIFUJI Hideaki #endif 105908b202b6SYOSHIFUJI Hideaki 1060dc4eb53aSLinus Lüssing static bool br_ip4_multicast_select_querier(struct net_bridge *br, 10612cd41431SLinus Lüssing struct net_bridge_port *port, 1062dc4eb53aSLinus Lüssing __be32 saddr) 1063dc4eb53aSLinus Lüssing { 1064dc4eb53aSLinus Lüssing if (!timer_pending(&br->ip4_own_query.timer) && 1065dc4eb53aSLinus Lüssing !timer_pending(&br->ip4_other_query.timer)) 1066dc4eb53aSLinus Lüssing goto update; 1067dc4eb53aSLinus Lüssing 1068dc4eb53aSLinus Lüssing if (!br->ip4_querier.addr.u.ip4) 1069dc4eb53aSLinus Lüssing goto update; 1070dc4eb53aSLinus Lüssing 1071dc4eb53aSLinus Lüssing if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4)) 1072dc4eb53aSLinus Lüssing goto update; 1073dc4eb53aSLinus Lüssing 1074dc4eb53aSLinus Lüssing return false; 1075dc4eb53aSLinus Lüssing 1076dc4eb53aSLinus Lüssing update: 1077dc4eb53aSLinus Lüssing br->ip4_querier.addr.u.ip4 = saddr; 1078dc4eb53aSLinus Lüssing 10792cd41431SLinus Lüssing /* update protected by general multicast_lock by caller */ 10802cd41431SLinus Lüssing rcu_assign_pointer(br->ip4_querier.port, port); 10812cd41431SLinus Lüssing 1082dc4eb53aSLinus Lüssing return true; 1083dc4eb53aSLinus Lüssing } 1084dc4eb53aSLinus Lüssing 1085dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1086dc4eb53aSLinus Lüssing static bool br_ip6_multicast_select_querier(struct net_bridge *br, 10872cd41431SLinus Lüssing struct net_bridge_port *port, 1088dc4eb53aSLinus Lüssing struct in6_addr *saddr) 1089dc4eb53aSLinus Lüssing { 1090dc4eb53aSLinus Lüssing if (!timer_pending(&br->ip6_own_query.timer) && 1091dc4eb53aSLinus Lüssing !timer_pending(&br->ip6_other_query.timer)) 1092dc4eb53aSLinus Lüssing goto update; 1093dc4eb53aSLinus Lüssing 1094dc4eb53aSLinus Lüssing if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0) 1095dc4eb53aSLinus Lüssing goto update; 1096dc4eb53aSLinus Lüssing 1097dc4eb53aSLinus Lüssing return false; 1098dc4eb53aSLinus Lüssing 1099dc4eb53aSLinus Lüssing update: 1100dc4eb53aSLinus Lüssing br->ip6_querier.addr.u.ip6 = *saddr; 1101dc4eb53aSLinus Lüssing 11022cd41431SLinus Lüssing /* update protected by general multicast_lock by caller */ 11032cd41431SLinus Lüssing rcu_assign_pointer(br->ip6_querier.port, port); 11042cd41431SLinus Lüssing 1105dc4eb53aSLinus Lüssing return true; 1106dc4eb53aSLinus Lüssing } 1107dc4eb53aSLinus Lüssing #endif 1108dc4eb53aSLinus Lüssing 1109dc4eb53aSLinus Lüssing static bool br_multicast_select_querier(struct net_bridge *br, 11102cd41431SLinus Lüssing struct net_bridge_port *port, 1111dc4eb53aSLinus Lüssing struct br_ip *saddr) 1112dc4eb53aSLinus Lüssing { 1113dc4eb53aSLinus Lüssing switch (saddr->proto) { 1114dc4eb53aSLinus Lüssing case htons(ETH_P_IP): 11152cd41431SLinus Lüssing return br_ip4_multicast_select_querier(br, port, saddr->u.ip4); 1116dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1117dc4eb53aSLinus Lüssing case htons(ETH_P_IPV6): 11182cd41431SLinus Lüssing return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6); 1119dc4eb53aSLinus Lüssing #endif 1120dc4eb53aSLinus Lüssing } 1121dc4eb53aSLinus Lüssing 1122dc4eb53aSLinus Lüssing return false; 1123dc4eb53aSLinus Lüssing } 1124dc4eb53aSLinus Lüssing 1125cc0fdd80SLinus Lüssing static void 112690010b36SLinus Lüssing br_multicast_update_query_timer(struct net_bridge *br, 112790010b36SLinus Lüssing struct bridge_mcast_other_query *query, 1128b00589afSLinus Lüssing unsigned long max_delay) 1129b00589afSLinus Lüssing { 113090010b36SLinus Lüssing if (!timer_pending(&query->timer)) 113190010b36SLinus Lüssing query->delay_time = jiffies + max_delay; 1132b00589afSLinus Lüssing 113390010b36SLinus Lüssing mod_timer(&query->timer, jiffies + br->multicast_querier_interval); 1134b00589afSLinus Lüssing } 1135b00589afSLinus Lüssing 11366d549648SNogah Frankel static void br_port_mc_router_state_change(struct net_bridge_port *p, 11376d549648SNogah Frankel bool is_mc_router) 11386d549648SNogah Frankel { 11396d549648SNogah Frankel struct switchdev_attr attr = { 11406d549648SNogah Frankel .orig_dev = p->dev, 11416d549648SNogah Frankel .id = SWITCHDEV_ATTR_ID_PORT_MROUTER, 11426d549648SNogah Frankel .flags = SWITCHDEV_F_DEFER, 11436d549648SNogah Frankel .u.mrouter = is_mc_router, 11446d549648SNogah Frankel }; 11456d549648SNogah Frankel 11466d549648SNogah Frankel switchdev_port_attr_set(p->dev, &attr); 11476d549648SNogah Frankel } 11486d549648SNogah Frankel 11497e80c124Sstephen hemminger /* 11507c77602fSCong Wang * Add port to router_list 11517e80c124Sstephen hemminger * list is maintained ordered by pointer value 11527e80c124Sstephen hemminger * and locked by br->multicast_lock and RCU 11537e80c124Sstephen hemminger */ 11540909e117SHerbert Xu static void br_multicast_add_router(struct net_bridge *br, 11550909e117SHerbert Xu struct net_bridge_port *port) 11560909e117SHerbert Xu { 1157dcdca2c4Sstephen hemminger struct net_bridge_port *p; 1158b67bfe0dSSasha Levin struct hlist_node *slot = NULL; 11590909e117SHerbert Xu 11601a040eacSNikolay Aleksandrov if (!hlist_unhashed(&port->rlist)) 11611a040eacSNikolay Aleksandrov return; 11621a040eacSNikolay Aleksandrov 1163b67bfe0dSSasha Levin hlist_for_each_entry(p, &br->router_list, rlist) { 11647e80c124Sstephen hemminger if ((unsigned long) port >= (unsigned long) p) 11657e80c124Sstephen hemminger break; 1166b67bfe0dSSasha Levin slot = &p->rlist; 1167dcdca2c4Sstephen hemminger } 11680909e117SHerbert Xu 11697e80c124Sstephen hemminger if (slot) 11701d023284SKen Helias hlist_add_behind_rcu(&port->rlist, slot); 1171dcdca2c4Sstephen hemminger else 1172dcdca2c4Sstephen hemminger hlist_add_head_rcu(&port->rlist, &br->router_list); 1173949f1e39SSatish Ashok br_rtr_notify(br->dev, port, RTM_NEWMDB); 11746d549648SNogah Frankel br_port_mc_router_state_change(port, true); 11750909e117SHerbert Xu } 11760909e117SHerbert Xu 1177eb1d1641SHerbert Xu static void br_multicast_mark_router(struct net_bridge *br, 1178eb1d1641SHerbert Xu struct net_bridge_port *port) 1179eb1d1641SHerbert Xu { 1180eb1d1641SHerbert Xu unsigned long now = jiffies; 1181eb1d1641SHerbert Xu 1182eb1d1641SHerbert Xu if (!port) { 118377041420SYotam Gigi if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) { 118477041420SYotam Gigi if (!timer_pending(&br->multicast_router_timer)) 118577041420SYotam Gigi br_mc_router_state_change(br, true); 1186eb1d1641SHerbert Xu mod_timer(&br->multicast_router_timer, 1187eb1d1641SHerbert Xu now + br->multicast_querier_interval); 118877041420SYotam Gigi } 1189eb1d1641SHerbert Xu return; 1190eb1d1641SHerbert Xu } 1191eb1d1641SHerbert Xu 1192a55d8246SNikolay Aleksandrov if (port->multicast_router == MDB_RTR_TYPE_DISABLED || 1193a55d8246SNikolay Aleksandrov port->multicast_router == MDB_RTR_TYPE_PERM) 1194eb1d1641SHerbert Xu return; 1195eb1d1641SHerbert Xu 11960909e117SHerbert Xu br_multicast_add_router(br, port); 1197eb1d1641SHerbert Xu 1198eb1d1641SHerbert Xu mod_timer(&port->multicast_router_timer, 1199eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1200eb1d1641SHerbert Xu } 1201eb1d1641SHerbert Xu 1202eb1d1641SHerbert Xu static void br_multicast_query_received(struct net_bridge *br, 1203eb1d1641SHerbert Xu struct net_bridge_port *port, 120490010b36SLinus Lüssing struct bridge_mcast_other_query *query, 1205dc4eb53aSLinus Lüssing struct br_ip *saddr, 1206b00589afSLinus Lüssing unsigned long max_delay) 1207eb1d1641SHerbert Xu { 12082cd41431SLinus Lüssing if (!br_multicast_select_querier(br, port, saddr)) 1209eb1d1641SHerbert Xu return; 1210eb1d1641SHerbert Xu 1211dc4eb53aSLinus Lüssing br_multicast_update_query_timer(br, query, max_delay); 1212eb1d1641SHerbert Xu br_multicast_mark_router(br, port); 1213eb1d1641SHerbert Xu } 1214eb1d1641SHerbert Xu 12159c2e955cSzhong jiang static void br_ip4_multicast_query(struct net_bridge *br, 1216eb1d1641SHerbert Xu struct net_bridge_port *port, 121706499098SVlad Yasevich struct sk_buff *skb, 121806499098SVlad Yasevich u16 vid) 1219eb1d1641SHerbert Xu { 1220ba5ea614SLinus Lüssing unsigned int transport_len = ip_transport_len(skb); 1221b71d1d42SEric Dumazet const struct iphdr *iph = ip_hdr(skb); 1222eb1d1641SHerbert Xu struct igmphdr *ih = igmp_hdr(skb); 1223eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1224eb1d1641SHerbert Xu struct igmpv3_query *ih3; 1225eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1226e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 1227dc4eb53aSLinus Lüssing struct br_ip saddr; 1228eb1d1641SHerbert Xu unsigned long max_delay; 1229eb1d1641SHerbert Xu unsigned long now = jiffies; 1230eb1d1641SHerbert Xu __be32 group; 1231eb1d1641SHerbert Xu 1232eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1233eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1234eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 1235eb1d1641SHerbert Xu goto out; 1236eb1d1641SHerbert Xu 1237eb1d1641SHerbert Xu group = ih->group; 1238eb1d1641SHerbert Xu 1239ba5ea614SLinus Lüssing if (transport_len == sizeof(*ih)) { 1240eb1d1641SHerbert Xu max_delay = ih->code * (HZ / IGMP_TIMER_SCALE); 1241eb1d1641SHerbert Xu 1242eb1d1641SHerbert Xu if (!max_delay) { 1243eb1d1641SHerbert Xu max_delay = 10 * HZ; 1244eb1d1641SHerbert Xu group = 0; 1245eb1d1641SHerbert Xu } 1246ba5ea614SLinus Lüssing } else if (transport_len >= sizeof(*ih3)) { 1247eb1d1641SHerbert Xu ih3 = igmpv3_query_hdr(skb); 1248eb1d1641SHerbert Xu if (ih3->nsrcs) 1249bec68ff1SYOSHIFUJI Hideaki goto out; 1250eb1d1641SHerbert Xu 12510ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 max_delay = ih3->code ? 12520ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1; 12539afd85c9SLinus Lüssing } else { 12549ed973ccSLinus Lüssing goto out; 12559ed973ccSLinus Lüssing } 12569ed973ccSLinus Lüssing 1257dc4eb53aSLinus Lüssing if (!group) { 1258dc4eb53aSLinus Lüssing saddr.proto = htons(ETH_P_IP); 1259dc4eb53aSLinus Lüssing saddr.u.ip4 = iph->saddr; 1260b00589afSLinus Lüssing 1261dc4eb53aSLinus Lüssing br_multicast_query_received(br, port, &br->ip4_other_query, 1262dc4eb53aSLinus Lüssing &saddr, max_delay); 1263eb1d1641SHerbert Xu goto out; 1264dc4eb53aSLinus Lüssing } 1265eb1d1641SHerbert Xu 126619e3a9c9SNikolay Aleksandrov mp = br_mdb_ip4_get(br, group, vid); 1267eb1d1641SHerbert Xu if (!mp) 1268eb1d1641SHerbert Xu goto out; 1269eb1d1641SHerbert Xu 1270eb1d1641SHerbert Xu max_delay *= br->multicast_last_member_count; 1271eb1d1641SHerbert Xu 1272ff0fd34eSAndrew Lunn if (mp->host_joined && 1273eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1274eb1d1641SHerbert Xu time_after(mp->timer.expires, now + max_delay) : 1275eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) 1276eb1d1641SHerbert Xu mod_timer(&mp->timer, now + max_delay); 1277eb1d1641SHerbert Xu 1278e8051688SEric Dumazet for (pp = &mp->ports; 1279e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 1280e8051688SEric Dumazet pp = &p->next) { 1281eb1d1641SHerbert Xu if (timer_pending(&p->timer) ? 1282eb1d1641SHerbert Xu time_after(p->timer.expires, now + max_delay) : 1283eb1d1641SHerbert Xu try_to_del_timer_sync(&p->timer) >= 0) 128424f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 1285eb1d1641SHerbert Xu } 1286eb1d1641SHerbert Xu 1287eb1d1641SHerbert Xu out: 1288eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1289eb1d1641SHerbert Xu } 1290eb1d1641SHerbert Xu 1291dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 129208b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_query(struct net_bridge *br, 129308b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 129406499098SVlad Yasevich struct sk_buff *skb, 129506499098SVlad Yasevich u16 vid) 129608b202b6SYOSHIFUJI Hideaki { 1297ba5ea614SLinus Lüssing unsigned int transport_len = ipv6_transport_len(skb); 1298eca2a43bSstephen hemminger struct mld_msg *mld; 129908b202b6SYOSHIFUJI Hideaki struct net_bridge_mdb_entry *mp; 130008b202b6SYOSHIFUJI Hideaki struct mld2_query *mld2q; 1301e8051688SEric Dumazet struct net_bridge_port_group *p; 1302e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 1303dc4eb53aSLinus Lüssing struct br_ip saddr; 130408b202b6SYOSHIFUJI Hideaki unsigned long max_delay; 130508b202b6SYOSHIFUJI Hideaki unsigned long now = jiffies; 1306856ce5d0SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 1307b71d1d42SEric Dumazet const struct in6_addr *group = NULL; 13089ed973ccSLinus Lüssing bool is_general_query; 130908b202b6SYOSHIFUJI Hideaki int err = 0; 131008b202b6SYOSHIFUJI Hideaki 131108b202b6SYOSHIFUJI Hideaki spin_lock(&br->multicast_lock); 131208b202b6SYOSHIFUJI Hideaki if (!netif_running(br->dev) || 131308b202b6SYOSHIFUJI Hideaki (port && port->state == BR_STATE_DISABLED)) 131408b202b6SYOSHIFUJI Hideaki goto out; 131508b202b6SYOSHIFUJI Hideaki 1316ba5ea614SLinus Lüssing if (transport_len == sizeof(*mld)) { 1317856ce5d0SLinus Lüssing if (!pskb_may_pull(skb, offset + sizeof(*mld))) { 131808b202b6SYOSHIFUJI Hideaki err = -EINVAL; 131908b202b6SYOSHIFUJI Hideaki goto out; 132008b202b6SYOSHIFUJI Hideaki } 132108b202b6SYOSHIFUJI Hideaki mld = (struct mld_msg *) icmp6_hdr(skb); 13224715213dSLi RongQing max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay)); 132308b202b6SYOSHIFUJI Hideaki if (max_delay) 132408b202b6SYOSHIFUJI Hideaki group = &mld->mld_mca; 1325248ba8ecSLinus Lüssing } else { 1326856ce5d0SLinus Lüssing if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) { 132708b202b6SYOSHIFUJI Hideaki err = -EINVAL; 132808b202b6SYOSHIFUJI Hideaki goto out; 132908b202b6SYOSHIFUJI Hideaki } 133008b202b6SYOSHIFUJI Hideaki mld2q = (struct mld2_query *)icmp6_hdr(skb); 133108b202b6SYOSHIFUJI Hideaki if (!mld2q->mld2q_nsrcs) 133208b202b6SYOSHIFUJI Hideaki group = &mld2q->mld2q_mca; 1333e3f5b170SDaniel Borkmann 1334e3f5b170SDaniel Borkmann max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL); 133508b202b6SYOSHIFUJI Hideaki } 133608b202b6SYOSHIFUJI Hideaki 13379ed973ccSLinus Lüssing is_general_query = group && ipv6_addr_any(group); 13389ed973ccSLinus Lüssing 1339dc4eb53aSLinus Lüssing if (is_general_query) { 1340dc4eb53aSLinus Lüssing saddr.proto = htons(ETH_P_IPV6); 13413b26a5d0SNikolay Aleksandrov saddr.u.ip6 = ipv6_hdr(skb)->saddr; 1342b00589afSLinus Lüssing 1343dc4eb53aSLinus Lüssing br_multicast_query_received(br, port, &br->ip6_other_query, 1344dc4eb53aSLinus Lüssing &saddr, max_delay); 134508b202b6SYOSHIFUJI Hideaki goto out; 13466c03ee8bSLinus Lüssing } else if (!group) { 13476c03ee8bSLinus Lüssing goto out; 1348dc4eb53aSLinus Lüssing } 134908b202b6SYOSHIFUJI Hideaki 135019e3a9c9SNikolay Aleksandrov mp = br_mdb_ip6_get(br, group, vid); 135108b202b6SYOSHIFUJI Hideaki if (!mp) 135208b202b6SYOSHIFUJI Hideaki goto out; 135308b202b6SYOSHIFUJI Hideaki 135408b202b6SYOSHIFUJI Hideaki max_delay *= br->multicast_last_member_count; 1355ff0fd34eSAndrew Lunn if (mp->host_joined && 135608b202b6SYOSHIFUJI Hideaki (timer_pending(&mp->timer) ? 135708b202b6SYOSHIFUJI Hideaki time_after(mp->timer.expires, now + max_delay) : 135808b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&mp->timer) >= 0)) 135908b202b6SYOSHIFUJI Hideaki mod_timer(&mp->timer, now + max_delay); 136008b202b6SYOSHIFUJI Hideaki 1361e8051688SEric Dumazet for (pp = &mp->ports; 1362e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 1363e8051688SEric Dumazet pp = &p->next) { 136408b202b6SYOSHIFUJI Hideaki if (timer_pending(&p->timer) ? 136508b202b6SYOSHIFUJI Hideaki time_after(p->timer.expires, now + max_delay) : 136608b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&p->timer) >= 0) 136724f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 136808b202b6SYOSHIFUJI Hideaki } 136908b202b6SYOSHIFUJI Hideaki 137008b202b6SYOSHIFUJI Hideaki out: 137108b202b6SYOSHIFUJI Hideaki spin_unlock(&br->multicast_lock); 137208b202b6SYOSHIFUJI Hideaki return err; 137308b202b6SYOSHIFUJI Hideaki } 137408b202b6SYOSHIFUJI Hideaki #endif 137508b202b6SYOSHIFUJI Hideaki 137690010b36SLinus Lüssing static void 137790010b36SLinus Lüssing br_multicast_leave_group(struct net_bridge *br, 1378eb1d1641SHerbert Xu struct net_bridge_port *port, 1379cc0fdd80SLinus Lüssing struct br_ip *group, 138090010b36SLinus Lüssing struct bridge_mcast_other_query *other_query, 13816db6f0eaSFelix Fietkau struct bridge_mcast_own_query *own_query, 13826db6f0eaSFelix Fietkau const unsigned char *src) 1383eb1d1641SHerbert Xu { 1384eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1385eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1386eb1d1641SHerbert Xu unsigned long now; 1387eb1d1641SHerbert Xu unsigned long time; 1388eb1d1641SHerbert Xu 1389eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1390eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1391544586f7SSatish Ashok (port && port->state == BR_STATE_DISABLED)) 1392eb1d1641SHerbert Xu goto out; 1393eb1d1641SHerbert Xu 139419e3a9c9SNikolay Aleksandrov mp = br_mdb_ip_get(br, group); 1395eb1d1641SHerbert Xu if (!mp) 1396eb1d1641SHerbert Xu goto out; 1397eb1d1641SHerbert Xu 1398544586f7SSatish Ashok if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) { 1399544586f7SSatish Ashok struct net_bridge_port_group __rcu **pp; 1400544586f7SSatish Ashok 1401544586f7SSatish Ashok for (pp = &mp->ports; 1402544586f7SSatish Ashok (p = mlock_dereference(*pp, br)) != NULL; 1403544586f7SSatish Ashok pp = &p->next) { 14046db6f0eaSFelix Fietkau if (!br_port_group_equal(p, port, src)) 1405544586f7SSatish Ashok continue; 1406544586f7SSatish Ashok 14075c725b6bSNikolay Aleksandrov if (p->flags & MDB_PG_FLAGS_PERMANENT) 14085c725b6bSNikolay Aleksandrov break; 14095c725b6bSNikolay Aleksandrov 1410544586f7SSatish Ashok rcu_assign_pointer(*pp, p->next); 1411544586f7SSatish Ashok hlist_del_init(&p->mglist); 1412544586f7SSatish Ashok del_timer(&p->timer); 14134329596cSNikolay Aleksandrov kfree_rcu(p, rcu); 141445ebcce5SElad Raz br_mdb_notify(br->dev, port, group, RTM_DELMDB, 14153247b272SNikolay Aleksandrov p->flags | MDB_PG_FLAGS_FAST_LEAVE); 1416544586f7SSatish Ashok 1417ff0fd34eSAndrew Lunn if (!mp->ports && !mp->host_joined && 1418544586f7SSatish Ashok netif_running(br->dev)) 1419544586f7SSatish Ashok mod_timer(&mp->timer, jiffies); 1420544586f7SSatish Ashok } 1421544586f7SSatish Ashok goto out; 1422544586f7SSatish Ashok } 1423544586f7SSatish Ashok 1424544586f7SSatish Ashok if (timer_pending(&other_query->timer)) 1425544586f7SSatish Ashok goto out; 1426544586f7SSatish Ashok 1427675779adSNikolay Aleksandrov if (br_opt_get(br, BROPT_MULTICAST_QUERIER)) { 14286b7df111SCong Wang __br_multicast_send_query(br, port, &mp->addr); 14296b7df111SCong Wang 14306b7df111SCong Wang time = jiffies + br->multicast_last_member_count * 14316b7df111SCong Wang br->multicast_last_member_interval; 1432cc0fdd80SLinus Lüssing 143390010b36SLinus Lüssing mod_timer(&own_query->timer, time); 14346b7df111SCong Wang 14356b7df111SCong Wang for (p = mlock_dereference(mp->ports, br); 14366b7df111SCong Wang p != NULL; 14376b7df111SCong Wang p = mlock_dereference(p->next, br)) { 14386db6f0eaSFelix Fietkau if (!br_port_group_equal(p, port, src)) 14396b7df111SCong Wang continue; 14406b7df111SCong Wang 14416b7df111SCong Wang if (!hlist_unhashed(&p->mglist) && 14426b7df111SCong Wang (timer_pending(&p->timer) ? 14436b7df111SCong Wang time_after(p->timer.expires, time) : 14446b7df111SCong Wang try_to_del_timer_sync(&p->timer) >= 0)) { 14456b7df111SCong Wang mod_timer(&p->timer, time); 14466b7df111SCong Wang } 14476b7df111SCong Wang 14486b7df111SCong Wang break; 14496b7df111SCong Wang } 14506b7df111SCong Wang } 14516b7df111SCong Wang 1452eb1d1641SHerbert Xu now = jiffies; 1453eb1d1641SHerbert Xu time = now + br->multicast_last_member_count * 1454eb1d1641SHerbert Xu br->multicast_last_member_interval; 1455eb1d1641SHerbert Xu 1456eb1d1641SHerbert Xu if (!port) { 1457ff0fd34eSAndrew Lunn if (mp->host_joined && 1458eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1459eb1d1641SHerbert Xu time_after(mp->timer.expires, time) : 1460eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) { 1461eb1d1641SHerbert Xu mod_timer(&mp->timer, time); 1462eb1d1641SHerbert Xu } 1463454594f3SLinus Lüssing 1464454594f3SLinus Lüssing goto out; 1465454594f3SLinus Lüssing } 1466454594f3SLinus Lüssing 1467454594f3SLinus Lüssing for (p = mlock_dereference(mp->ports, br); 1468454594f3SLinus Lüssing p != NULL; 1469454594f3SLinus Lüssing p = mlock_dereference(p->next, br)) { 1470454594f3SLinus Lüssing if (p->port != port) 1471454594f3SLinus Lüssing continue; 1472454594f3SLinus Lüssing 1473454594f3SLinus Lüssing if (!hlist_unhashed(&p->mglist) && 1474454594f3SLinus Lüssing (timer_pending(&p->timer) ? 1475454594f3SLinus Lüssing time_after(p->timer.expires, time) : 1476454594f3SLinus Lüssing try_to_del_timer_sync(&p->timer) >= 0)) { 1477454594f3SLinus Lüssing mod_timer(&p->timer, time); 1478454594f3SLinus Lüssing } 1479454594f3SLinus Lüssing 1480454594f3SLinus Lüssing break; 1481eb1d1641SHerbert Xu } 1482eb1d1641SHerbert Xu out: 1483eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1484eb1d1641SHerbert Xu } 1485eb1d1641SHerbert Xu 14868ef2a9a5SYOSHIFUJI Hideaki static void br_ip4_multicast_leave_group(struct net_bridge *br, 14878ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 1488b0e9a30dSVlad Yasevich __be32 group, 14896db6f0eaSFelix Fietkau __u16 vid, 14906db6f0eaSFelix Fietkau const unsigned char *src) 14918ef2a9a5SYOSHIFUJI Hideaki { 14928ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 149390010b36SLinus Lüssing struct bridge_mcast_own_query *own_query; 14948ef2a9a5SYOSHIFUJI Hideaki 14958ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 14968ef2a9a5SYOSHIFUJI Hideaki return; 14978ef2a9a5SYOSHIFUJI Hideaki 149890010b36SLinus Lüssing own_query = port ? &port->ip4_own_query : &br->ip4_own_query; 149990010b36SLinus Lüssing 15001515a63fSNikolay Aleksandrov memset(&br_group, 0, sizeof(br_group)); 15018ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 15028ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 1503b0e9a30dSVlad Yasevich br_group.vid = vid; 15048ef2a9a5SYOSHIFUJI Hideaki 150590010b36SLinus Lüssing br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query, 15066db6f0eaSFelix Fietkau own_query, src); 15078ef2a9a5SYOSHIFUJI Hideaki } 15088ef2a9a5SYOSHIFUJI Hideaki 1509dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 151008b202b6SYOSHIFUJI Hideaki static void br_ip6_multicast_leave_group(struct net_bridge *br, 151108b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 1512b0e9a30dSVlad Yasevich const struct in6_addr *group, 15136db6f0eaSFelix Fietkau __u16 vid, 15146db6f0eaSFelix Fietkau const unsigned char *src) 151508b202b6SYOSHIFUJI Hideaki { 151608b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 151790010b36SLinus Lüssing struct bridge_mcast_own_query *own_query; 151808b202b6SYOSHIFUJI Hideaki 15193c3769e6SLinus Lüssing if (ipv6_addr_is_ll_all_nodes(group)) 152008b202b6SYOSHIFUJI Hideaki return; 152108b202b6SYOSHIFUJI Hideaki 152290010b36SLinus Lüssing own_query = port ? &port->ip6_own_query : &br->ip6_own_query; 152390010b36SLinus Lüssing 15241515a63fSNikolay Aleksandrov memset(&br_group, 0, sizeof(br_group)); 15254e3fd7a0SAlexey Dobriyan br_group.u.ip6 = *group; 152608b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 1527b0e9a30dSVlad Yasevich br_group.vid = vid; 152808b202b6SYOSHIFUJI Hideaki 152990010b36SLinus Lüssing br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query, 15306db6f0eaSFelix Fietkau own_query, src); 153108b202b6SYOSHIFUJI Hideaki } 153208b202b6SYOSHIFUJI Hideaki #endif 15338ef2a9a5SYOSHIFUJI Hideaki 15341080ab95SNikolay Aleksandrov static void br_multicast_err_count(const struct net_bridge *br, 15351080ab95SNikolay Aleksandrov const struct net_bridge_port *p, 15361080ab95SNikolay Aleksandrov __be16 proto) 15371080ab95SNikolay Aleksandrov { 15381080ab95SNikolay Aleksandrov struct bridge_mcast_stats __percpu *stats; 15391080ab95SNikolay Aleksandrov struct bridge_mcast_stats *pstats; 15401080ab95SNikolay Aleksandrov 1541675779adSNikolay Aleksandrov if (!br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)) 15421080ab95SNikolay Aleksandrov return; 15431080ab95SNikolay Aleksandrov 15441080ab95SNikolay Aleksandrov if (p) 15451080ab95SNikolay Aleksandrov stats = p->mcast_stats; 15461080ab95SNikolay Aleksandrov else 15471080ab95SNikolay Aleksandrov stats = br->mcast_stats; 15481080ab95SNikolay Aleksandrov if (WARN_ON(!stats)) 15491080ab95SNikolay Aleksandrov return; 15501080ab95SNikolay Aleksandrov 15511080ab95SNikolay Aleksandrov pstats = this_cpu_ptr(stats); 15521080ab95SNikolay Aleksandrov 15531080ab95SNikolay Aleksandrov u64_stats_update_begin(&pstats->syncp); 15541080ab95SNikolay Aleksandrov switch (proto) { 15551080ab95SNikolay Aleksandrov case htons(ETH_P_IP): 15561080ab95SNikolay Aleksandrov pstats->mstats.igmp_parse_errors++; 15571080ab95SNikolay Aleksandrov break; 15581080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 15591080ab95SNikolay Aleksandrov case htons(ETH_P_IPV6): 15601080ab95SNikolay Aleksandrov pstats->mstats.mld_parse_errors++; 15611080ab95SNikolay Aleksandrov break; 15621080ab95SNikolay Aleksandrov #endif 15631080ab95SNikolay Aleksandrov } 15641080ab95SNikolay Aleksandrov u64_stats_update_end(&pstats->syncp); 15651080ab95SNikolay Aleksandrov } 15661080ab95SNikolay Aleksandrov 156791b02d3dSNikolay Aleksandrov static void br_multicast_pim(struct net_bridge *br, 156891b02d3dSNikolay Aleksandrov struct net_bridge_port *port, 156991b02d3dSNikolay Aleksandrov const struct sk_buff *skb) 157091b02d3dSNikolay Aleksandrov { 157191b02d3dSNikolay Aleksandrov unsigned int offset = skb_transport_offset(skb); 157291b02d3dSNikolay Aleksandrov struct pimhdr *pimhdr, _pimhdr; 157391b02d3dSNikolay Aleksandrov 157491b02d3dSNikolay Aleksandrov pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr); 157591b02d3dSNikolay Aleksandrov if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION || 157691b02d3dSNikolay Aleksandrov pim_hdr_type(pimhdr) != PIM_TYPE_HELLO) 157791b02d3dSNikolay Aleksandrov return; 157891b02d3dSNikolay Aleksandrov 157991b02d3dSNikolay Aleksandrov br_multicast_mark_router(br, port); 158091b02d3dSNikolay Aleksandrov } 158191b02d3dSNikolay Aleksandrov 15824b3087c7SLinus Lüssing static int br_ip4_multicast_mrd_rcv(struct net_bridge *br, 15834b3087c7SLinus Lüssing struct net_bridge_port *port, 15844b3087c7SLinus Lüssing struct sk_buff *skb) 15854b3087c7SLinus Lüssing { 15864b3087c7SLinus Lüssing if (ip_hdr(skb)->protocol != IPPROTO_IGMP || 15874b3087c7SLinus Lüssing igmp_hdr(skb)->type != IGMP_MRDISC_ADV) 15884b3087c7SLinus Lüssing return -ENOMSG; 15894b3087c7SLinus Lüssing 15904b3087c7SLinus Lüssing br_multicast_mark_router(br, port); 15914b3087c7SLinus Lüssing 15924b3087c7SLinus Lüssing return 0; 15934b3087c7SLinus Lüssing } 15944b3087c7SLinus Lüssing 1595eb1d1641SHerbert Xu static int br_multicast_ipv4_rcv(struct net_bridge *br, 1596eb1d1641SHerbert Xu struct net_bridge_port *port, 159706499098SVlad Yasevich struct sk_buff *skb, 159806499098SVlad Yasevich u16 vid) 1599eb1d1641SHerbert Xu { 16006db6f0eaSFelix Fietkau const unsigned char *src; 1601eb1d1641SHerbert Xu struct igmphdr *ih; 1602eb1d1641SHerbert Xu int err; 1603eb1d1641SHerbert Xu 1604ba5ea614SLinus Lüssing err = ip_mc_check_igmp(skb); 1605eb1d1641SHerbert Xu 16069afd85c9SLinus Lüssing if (err == -ENOMSG) { 160791b02d3dSNikolay Aleksandrov if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) { 1608bd4265feSHerbert Xu BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 160991b02d3dSNikolay Aleksandrov } else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) { 161091b02d3dSNikolay Aleksandrov if (ip_hdr(skb)->protocol == IPPROTO_PIM) 161191b02d3dSNikolay Aleksandrov br_multicast_pim(br, port, skb); 16124b3087c7SLinus Lüssing } else if (ipv4_is_all_snoopers(ip_hdr(skb)->daddr)) { 161308e71623SLi RongQing br_ip4_multicast_mrd_rcv(br, port, skb); 16144b3087c7SLinus Lüssing } 16154b3087c7SLinus Lüssing 1616eb1d1641SHerbert Xu return 0; 16179afd85c9SLinus Lüssing } else if (err < 0) { 16181080ab95SNikolay Aleksandrov br_multicast_err_count(br, port, skb->protocol); 16199afd85c9SLinus Lüssing return err; 1620bd4265feSHerbert Xu } 1621eb1d1641SHerbert Xu 16229afd85c9SLinus Lüssing ih = igmp_hdr(skb); 16236db6f0eaSFelix Fietkau src = eth_hdr(skb)->h_source; 16241080ab95SNikolay Aleksandrov BR_INPUT_SKB_CB(skb)->igmp = ih->type; 1625eb1d1641SHerbert Xu 1626eb1d1641SHerbert Xu switch (ih->type) { 1627eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_REPORT: 1628eb1d1641SHerbert Xu case IGMPV2_HOST_MEMBERSHIP_REPORT: 162962b2bcb4SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 16306db6f0eaSFelix Fietkau err = br_ip4_multicast_add_group(br, port, ih->group, vid, src); 1631eb1d1641SHerbert Xu break; 1632eb1d1641SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 1633ba5ea614SLinus Lüssing err = br_ip4_multicast_igmp3_report(br, port, skb, vid); 1634eb1d1641SHerbert Xu break; 1635eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_QUERY: 1636ba5ea614SLinus Lüssing br_ip4_multicast_query(br, port, skb, vid); 1637eb1d1641SHerbert Xu break; 1638eb1d1641SHerbert Xu case IGMP_HOST_LEAVE_MESSAGE: 16396db6f0eaSFelix Fietkau br_ip4_multicast_leave_group(br, port, ih->group, vid, src); 1640eb1d1641SHerbert Xu break; 1641eb1d1641SHerbert Xu } 1642eb1d1641SHerbert Xu 1643a65056ecSNikolay Aleksandrov br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp, 16441080ab95SNikolay Aleksandrov BR_MCAST_DIR_RX); 16451080ab95SNikolay Aleksandrov 1646eb1d1641SHerbert Xu return err; 1647eb1d1641SHerbert Xu } 1648eb1d1641SHerbert Xu 1649dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 16504b3087c7SLinus Lüssing static int br_ip6_multicast_mrd_rcv(struct net_bridge *br, 16514b3087c7SLinus Lüssing struct net_bridge_port *port, 16524b3087c7SLinus Lüssing struct sk_buff *skb) 16534b3087c7SLinus Lüssing { 16544b3087c7SLinus Lüssing int ret; 16554b3087c7SLinus Lüssing 16564b3087c7SLinus Lüssing if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6) 16574b3087c7SLinus Lüssing return -ENOMSG; 16584b3087c7SLinus Lüssing 16594b3087c7SLinus Lüssing ret = ipv6_mc_check_icmpv6(skb); 16604b3087c7SLinus Lüssing if (ret < 0) 16614b3087c7SLinus Lüssing return ret; 16624b3087c7SLinus Lüssing 16634b3087c7SLinus Lüssing if (icmp6_hdr(skb)->icmp6_type != ICMPV6_MRDISC_ADV) 16644b3087c7SLinus Lüssing return -ENOMSG; 16654b3087c7SLinus Lüssing 16664b3087c7SLinus Lüssing br_multicast_mark_router(br, port); 16674b3087c7SLinus Lüssing 16684b3087c7SLinus Lüssing return 0; 16694b3087c7SLinus Lüssing } 16704b3087c7SLinus Lüssing 167108b202b6SYOSHIFUJI Hideaki static int br_multicast_ipv6_rcv(struct net_bridge *br, 167208b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 167306499098SVlad Yasevich struct sk_buff *skb, 167406499098SVlad Yasevich u16 vid) 167508b202b6SYOSHIFUJI Hideaki { 16766db6f0eaSFelix Fietkau const unsigned char *src; 16779afd85c9SLinus Lüssing struct mld_msg *mld; 167808b202b6SYOSHIFUJI Hideaki int err; 167908b202b6SYOSHIFUJI Hideaki 1680ba5ea614SLinus Lüssing err = ipv6_mc_check_mld(skb); 168108b202b6SYOSHIFUJI Hideaki 16829afd85c9SLinus Lüssing if (err == -ENOMSG) { 16839afd85c9SLinus Lüssing if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr)) 16848fad9c39SLinus Lüssing BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 16854b3087c7SLinus Lüssing 16864b3087c7SLinus Lüssing if (ipv6_addr_is_all_snoopers(&ipv6_hdr(skb)->daddr)) { 16874b3087c7SLinus Lüssing err = br_ip6_multicast_mrd_rcv(br, port, skb); 16884b3087c7SLinus Lüssing 16894b3087c7SLinus Lüssing if (err < 0 && err != -ENOMSG) { 16904b3087c7SLinus Lüssing br_multicast_err_count(br, port, skb->protocol); 16914b3087c7SLinus Lüssing return err; 16924b3087c7SLinus Lüssing } 16934b3087c7SLinus Lüssing } 16944b3087c7SLinus Lüssing 169508b202b6SYOSHIFUJI Hideaki return 0; 16969afd85c9SLinus Lüssing } else if (err < 0) { 16971080ab95SNikolay Aleksandrov br_multicast_err_count(br, port, skb->protocol); 16989afd85c9SLinus Lüssing return err; 169908b202b6SYOSHIFUJI Hideaki } 170008b202b6SYOSHIFUJI Hideaki 17019afd85c9SLinus Lüssing mld = (struct mld_msg *)skb_transport_header(skb); 17021080ab95SNikolay Aleksandrov BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type; 170308b202b6SYOSHIFUJI Hideaki 17049afd85c9SLinus Lüssing switch (mld->mld_type) { 170508b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 17066db6f0eaSFelix Fietkau src = eth_hdr(skb)->h_source; 1707fc2af6c7SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 17086db6f0eaSFelix Fietkau err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid, 17096db6f0eaSFelix Fietkau src); 171008b202b6SYOSHIFUJI Hideaki break; 171108b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 1712ba5ea614SLinus Lüssing err = br_ip6_multicast_mld2_report(br, port, skb, vid); 171308b202b6SYOSHIFUJI Hideaki break; 171408b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 1715ba5ea614SLinus Lüssing err = br_ip6_multicast_query(br, port, skb, vid); 171608b202b6SYOSHIFUJI Hideaki break; 171708b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 17186db6f0eaSFelix Fietkau src = eth_hdr(skb)->h_source; 17196db6f0eaSFelix Fietkau br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src); 17209afd85c9SLinus Lüssing break; 172108b202b6SYOSHIFUJI Hideaki } 172208b202b6SYOSHIFUJI Hideaki 1723a65056ecSNikolay Aleksandrov br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp, 17241080ab95SNikolay Aleksandrov BR_MCAST_DIR_RX); 17251080ab95SNikolay Aleksandrov 172608b202b6SYOSHIFUJI Hideaki return err; 172708b202b6SYOSHIFUJI Hideaki } 172808b202b6SYOSHIFUJI Hideaki #endif 172908b202b6SYOSHIFUJI Hideaki 1730eb1d1641SHerbert Xu int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port, 173106499098SVlad Yasevich struct sk_buff *skb, u16 vid) 1732eb1d1641SHerbert Xu { 17331080ab95SNikolay Aleksandrov int ret = 0; 17341080ab95SNikolay Aleksandrov 17351fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->igmp = 0; 17361fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->mrouters_only = 0; 17371fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 173813cefad2SNikolay Aleksandrov if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) 1739eb1d1641SHerbert Xu return 0; 1740eb1d1641SHerbert Xu 1741eb1d1641SHerbert Xu switch (skb->protocol) { 1742eb1d1641SHerbert Xu case htons(ETH_P_IP): 17431080ab95SNikolay Aleksandrov ret = br_multicast_ipv4_rcv(br, port, skb, vid); 17441080ab95SNikolay Aleksandrov break; 1745dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 174608b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 17471080ab95SNikolay Aleksandrov ret = br_multicast_ipv6_rcv(br, port, skb, vid); 17481080ab95SNikolay Aleksandrov break; 174908b202b6SYOSHIFUJI Hideaki #endif 1750eb1d1641SHerbert Xu } 1751eb1d1641SHerbert Xu 17521080ab95SNikolay Aleksandrov return ret; 1753eb1d1641SHerbert Xu } 1754eb1d1641SHerbert Xu 1755cc0fdd80SLinus Lüssing static void br_multicast_query_expired(struct net_bridge *br, 17562cd41431SLinus Lüssing struct bridge_mcast_own_query *query, 17572cd41431SLinus Lüssing struct bridge_mcast_querier *querier) 1758cc0fdd80SLinus Lüssing { 1759cc0fdd80SLinus Lüssing spin_lock(&br->multicast_lock); 1760cc0fdd80SLinus Lüssing if (query->startup_sent < br->multicast_startup_query_count) 1761cc0fdd80SLinus Lüssing query->startup_sent++; 1762cc0fdd80SLinus Lüssing 176371d9f614SEric Dumazet RCU_INIT_POINTER(querier->port, NULL); 1764cc0fdd80SLinus Lüssing br_multicast_send_query(br, NULL, query); 1765cc0fdd80SLinus Lüssing spin_unlock(&br->multicast_lock); 1766cc0fdd80SLinus Lüssing } 1767cc0fdd80SLinus Lüssing 176888c1f37fSAllen Pais static void br_ip4_multicast_query_expired(struct timer_list *t) 1769eb1d1641SHerbert Xu { 177088c1f37fSAllen Pais struct net_bridge *br = from_timer(br, t, ip4_own_query.timer); 1771eb1d1641SHerbert Xu 17722cd41431SLinus Lüssing br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier); 1773eb1d1641SHerbert Xu } 1774eb1d1641SHerbert Xu 1775cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 177688c1f37fSAllen Pais static void br_ip6_multicast_query_expired(struct timer_list *t) 1777cc0fdd80SLinus Lüssing { 177888c1f37fSAllen Pais struct net_bridge *br = from_timer(br, t, ip6_own_query.timer); 1779cc0fdd80SLinus Lüssing 17802cd41431SLinus Lüssing br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier); 1781cc0fdd80SLinus Lüssing } 1782cc0fdd80SLinus Lüssing #endif 1783cc0fdd80SLinus Lüssing 1784eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br) 1785eb1d1641SHerbert Xu { 1786d08c6bc0SNikolay Aleksandrov br->hash_max = BR_MULTICAST_DEFAULT_HASH_MAX; 1787eb1d1641SHerbert Xu 17887f0aec7aSNikolay Aleksandrov br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 1789eb1d1641SHerbert Xu br->multicast_last_member_count = 2; 1790eb1d1641SHerbert Xu br->multicast_startup_query_count = 2; 1791eb1d1641SHerbert Xu 1792eb1d1641SHerbert Xu br->multicast_last_member_interval = HZ; 1793eb1d1641SHerbert Xu br->multicast_query_response_interval = 10 * HZ; 1794eb1d1641SHerbert Xu br->multicast_startup_query_interval = 125 * HZ / 4; 1795eb1d1641SHerbert Xu br->multicast_query_interval = 125 * HZ; 1796eb1d1641SHerbert Xu br->multicast_querier_interval = 255 * HZ; 1797eb1d1641SHerbert Xu br->multicast_membership_interval = 260 * HZ; 1798eb1d1641SHerbert Xu 179990010b36SLinus Lüssing br->ip4_other_query.delay_time = 0; 18002cd41431SLinus Lüssing br->ip4_querier.port = NULL; 1801aa2ae3e7SNikolay Aleksandrov br->multicast_igmp_version = 2; 1802cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1803aa2ae3e7SNikolay Aleksandrov br->multicast_mld_version = 1; 180490010b36SLinus Lüssing br->ip6_other_query.delay_time = 0; 18052cd41431SLinus Lüssing br->ip6_querier.port = NULL; 1806cc0fdd80SLinus Lüssing #endif 18076919622aSIdo Schimmel br_opt_toggle(br, BROPT_MULTICAST_ENABLED, true); 1808675779adSNikolay Aleksandrov br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true); 1809b00589afSLinus Lüssing 1810eb1d1641SHerbert Xu spin_lock_init(&br->multicast_lock); 181188c1f37fSAllen Pais timer_setup(&br->multicast_router_timer, 181288c1f37fSAllen Pais br_multicast_local_router_expired, 0); 181388c1f37fSAllen Pais timer_setup(&br->ip4_other_query.timer, 181488c1f37fSAllen Pais br_ip4_multicast_querier_expired, 0); 181588c1f37fSAllen Pais timer_setup(&br->ip4_own_query.timer, 181688c1f37fSAllen Pais br_ip4_multicast_query_expired, 0); 1817cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 181888c1f37fSAllen Pais timer_setup(&br->ip6_other_query.timer, 181988c1f37fSAllen Pais br_ip6_multicast_querier_expired, 0); 182088c1f37fSAllen Pais timer_setup(&br->ip6_own_query.timer, 182188c1f37fSAllen Pais br_ip6_multicast_query_expired, 0); 1822cc0fdd80SLinus Lüssing #endif 182319e3a9c9SNikolay Aleksandrov INIT_HLIST_HEAD(&br->mdb_list); 1824eb1d1641SHerbert Xu } 1825eb1d1641SHerbert Xu 18264effd28cSLinus Lüssing static void br_ip4_multicast_join_snoopers(struct net_bridge *br) 18274effd28cSLinus Lüssing { 18284effd28cSLinus Lüssing struct in_device *in_dev = in_dev_get(br->dev); 18294effd28cSLinus Lüssing 18304effd28cSLinus Lüssing if (!in_dev) 18314effd28cSLinus Lüssing return; 18324effd28cSLinus Lüssing 18339fb20801SFlorian Fainelli __ip_mc_inc_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC); 18344effd28cSLinus Lüssing in_dev_put(in_dev); 18354effd28cSLinus Lüssing } 18364effd28cSLinus Lüssing 18374effd28cSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 18384effd28cSLinus Lüssing static void br_ip6_multicast_join_snoopers(struct net_bridge *br) 18394effd28cSLinus Lüssing { 18404effd28cSLinus Lüssing struct in6_addr addr; 18414effd28cSLinus Lüssing 18424effd28cSLinus Lüssing ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a)); 18434effd28cSLinus Lüssing ipv6_dev_mc_inc(br->dev, &addr); 18444effd28cSLinus Lüssing } 18454effd28cSLinus Lüssing #else 18464effd28cSLinus Lüssing static inline void br_ip6_multicast_join_snoopers(struct net_bridge *br) 18474effd28cSLinus Lüssing { 18484effd28cSLinus Lüssing } 18494effd28cSLinus Lüssing #endif 18504effd28cSLinus Lüssing 18514effd28cSLinus Lüssing static void br_multicast_join_snoopers(struct net_bridge *br) 18524effd28cSLinus Lüssing { 18534effd28cSLinus Lüssing br_ip4_multicast_join_snoopers(br); 18544effd28cSLinus Lüssing br_ip6_multicast_join_snoopers(br); 18554effd28cSLinus Lüssing } 18564effd28cSLinus Lüssing 18574effd28cSLinus Lüssing static void br_ip4_multicast_leave_snoopers(struct net_bridge *br) 18584effd28cSLinus Lüssing { 18594effd28cSLinus Lüssing struct in_device *in_dev = in_dev_get(br->dev); 18604effd28cSLinus Lüssing 18614effd28cSLinus Lüssing if (WARN_ON(!in_dev)) 18624effd28cSLinus Lüssing return; 18634effd28cSLinus Lüssing 18649fb20801SFlorian Fainelli __ip_mc_dec_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC); 18654effd28cSLinus Lüssing in_dev_put(in_dev); 18664effd28cSLinus Lüssing } 18674effd28cSLinus Lüssing 18684effd28cSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 18694effd28cSLinus Lüssing static void br_ip6_multicast_leave_snoopers(struct net_bridge *br) 18704effd28cSLinus Lüssing { 18714effd28cSLinus Lüssing struct in6_addr addr; 18724effd28cSLinus Lüssing 18734effd28cSLinus Lüssing ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a)); 18744effd28cSLinus Lüssing ipv6_dev_mc_dec(br->dev, &addr); 18754effd28cSLinus Lüssing } 18764effd28cSLinus Lüssing #else 18774effd28cSLinus Lüssing static inline void br_ip6_multicast_leave_snoopers(struct net_bridge *br) 18784effd28cSLinus Lüssing { 18794effd28cSLinus Lüssing } 18804effd28cSLinus Lüssing #endif 18814effd28cSLinus Lüssing 18824effd28cSLinus Lüssing static void br_multicast_leave_snoopers(struct net_bridge *br) 18834effd28cSLinus Lüssing { 18844effd28cSLinus Lüssing br_ip4_multicast_leave_snoopers(br); 18854effd28cSLinus Lüssing br_ip6_multicast_leave_snoopers(br); 18864effd28cSLinus Lüssing } 18874effd28cSLinus Lüssing 1888cc0fdd80SLinus Lüssing static void __br_multicast_open(struct net_bridge *br, 188990010b36SLinus Lüssing struct bridge_mcast_own_query *query) 1890eb1d1641SHerbert Xu { 1891cc0fdd80SLinus Lüssing query->startup_sent = 0; 1892eb1d1641SHerbert Xu 189313cefad2SNikolay Aleksandrov if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) 1894eb1d1641SHerbert Xu return; 1895eb1d1641SHerbert Xu 1896cc0fdd80SLinus Lüssing mod_timer(&query->timer, jiffies); 1897cc0fdd80SLinus Lüssing } 1898cc0fdd80SLinus Lüssing 1899cc0fdd80SLinus Lüssing void br_multicast_open(struct net_bridge *br) 1900cc0fdd80SLinus Lüssing { 19014effd28cSLinus Lüssing if (br_opt_get(br, BROPT_MULTICAST_ENABLED)) 19024effd28cSLinus Lüssing br_multicast_join_snoopers(br); 19034effd28cSLinus Lüssing 190490010b36SLinus Lüssing __br_multicast_open(br, &br->ip4_own_query); 1905cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 190690010b36SLinus Lüssing __br_multicast_open(br, &br->ip6_own_query); 1907cc0fdd80SLinus Lüssing #endif 1908eb1d1641SHerbert Xu } 1909eb1d1641SHerbert Xu 1910eb1d1641SHerbert Xu void br_multicast_stop(struct net_bridge *br) 1911eb1d1641SHerbert Xu { 1912eb1d1641SHerbert Xu del_timer_sync(&br->multicast_router_timer); 191390010b36SLinus Lüssing del_timer_sync(&br->ip4_other_query.timer); 191490010b36SLinus Lüssing del_timer_sync(&br->ip4_own_query.timer); 1915cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 191690010b36SLinus Lüssing del_timer_sync(&br->ip6_other_query.timer); 191790010b36SLinus Lüssing del_timer_sync(&br->ip6_own_query.timer); 1918cc0fdd80SLinus Lüssing #endif 19194effd28cSLinus Lüssing 19204effd28cSLinus Lüssing if (br_opt_get(br, BROPT_MULTICAST_ENABLED)) 19214effd28cSLinus Lüssing br_multicast_leave_snoopers(br); 1922e10177abSSatish Ashok } 1923e10177abSSatish Ashok 1924e10177abSSatish Ashok void br_multicast_dev_del(struct net_bridge *br) 1925e10177abSSatish Ashok { 1926e10177abSSatish Ashok struct net_bridge_mdb_entry *mp; 192719e3a9c9SNikolay Aleksandrov struct hlist_node *tmp; 1928eb1d1641SHerbert Xu 1929eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 193019e3a9c9SNikolay Aleksandrov hlist_for_each_entry_safe(mp, tmp, &br->mdb_list, mdb_node) { 1931eb1d1641SHerbert Xu del_timer(&mp->timer); 193219e3a9c9SNikolay Aleksandrov rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode, 193319e3a9c9SNikolay Aleksandrov br_mdb_rht_params); 193419e3a9c9SNikolay Aleksandrov hlist_del_rcu(&mp->mdb_node); 19354329596cSNikolay Aleksandrov kfree_rcu(mp, rcu); 1936eb1d1641SHerbert Xu } 1937eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 193819e3a9c9SNikolay Aleksandrov 19394329596cSNikolay Aleksandrov rcu_barrier(); 1940eb1d1641SHerbert Xu } 19410909e117SHerbert Xu 19420909e117SHerbert Xu int br_multicast_set_router(struct net_bridge *br, unsigned long val) 19430909e117SHerbert Xu { 19446ae4ae8eSLinus Lüssing int err = -EINVAL; 19450909e117SHerbert Xu 19460909e117SHerbert Xu spin_lock_bh(&br->multicast_lock); 19470909e117SHerbert Xu 19480909e117SHerbert Xu switch (val) { 19497f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_DISABLED: 19507f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_PERM: 195177041420SYotam Gigi br_mc_router_state_change(br, val == MDB_RTR_TYPE_PERM); 19520909e117SHerbert Xu del_timer(&br->multicast_router_timer); 195377041420SYotam Gigi br->multicast_router = val; 195477041420SYotam Gigi err = 0; 195577041420SYotam Gigi break; 19567f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_TEMP_QUERY: 195777041420SYotam Gigi if (br->multicast_router != MDB_RTR_TYPE_TEMP_QUERY) 195877041420SYotam Gigi br_mc_router_state_change(br, false); 19590909e117SHerbert Xu br->multicast_router = val; 19600909e117SHerbert Xu err = 0; 19610909e117SHerbert Xu break; 19620909e117SHerbert Xu } 19630909e117SHerbert Xu 19640909e117SHerbert Xu spin_unlock_bh(&br->multicast_lock); 19650909e117SHerbert Xu 19660909e117SHerbert Xu return err; 19670909e117SHerbert Xu } 19680909e117SHerbert Xu 19697f0aec7aSNikolay Aleksandrov static void __del_port_router(struct net_bridge_port *p) 19707f0aec7aSNikolay Aleksandrov { 19717f0aec7aSNikolay Aleksandrov if (hlist_unhashed(&p->rlist)) 19727f0aec7aSNikolay Aleksandrov return; 19737f0aec7aSNikolay Aleksandrov hlist_del_init_rcu(&p->rlist); 19747f0aec7aSNikolay Aleksandrov br_rtr_notify(p->br->dev, p, RTM_DELMDB); 19756d549648SNogah Frankel br_port_mc_router_state_change(p, false); 1976f12e7d95SNogah Frankel 1977f12e7d95SNogah Frankel /* don't allow timer refresh */ 1978f12e7d95SNogah Frankel if (p->multicast_router == MDB_RTR_TYPE_TEMP) 1979f12e7d95SNogah Frankel p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 19807f0aec7aSNikolay Aleksandrov } 19817f0aec7aSNikolay Aleksandrov 19820909e117SHerbert Xu int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val) 19830909e117SHerbert Xu { 19840909e117SHerbert Xu struct net_bridge *br = p->br; 1985a55d8246SNikolay Aleksandrov unsigned long now = jiffies; 19866ae4ae8eSLinus Lüssing int err = -EINVAL; 19870909e117SHerbert Xu 19880909e117SHerbert Xu spin_lock(&br->multicast_lock); 19894950cfd1SNikolay Aleksandrov if (p->multicast_router == val) { 1990a55d8246SNikolay Aleksandrov /* Refresh the temp router port timer */ 1991a55d8246SNikolay Aleksandrov if (p->multicast_router == MDB_RTR_TYPE_TEMP) 1992a55d8246SNikolay Aleksandrov mod_timer(&p->multicast_router_timer, 1993a55d8246SNikolay Aleksandrov now + br->multicast_querier_interval); 19944950cfd1SNikolay Aleksandrov err = 0; 19954950cfd1SNikolay Aleksandrov goto unlock; 19964950cfd1SNikolay Aleksandrov } 19970909e117SHerbert Xu switch (val) { 19987f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_DISABLED: 19997f0aec7aSNikolay Aleksandrov p->multicast_router = MDB_RTR_TYPE_DISABLED; 20007f0aec7aSNikolay Aleksandrov __del_port_router(p); 20010909e117SHerbert Xu del_timer(&p->multicast_router_timer); 20020909e117SHerbert Xu break; 20037f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_TEMP_QUERY: 20047f0aec7aSNikolay Aleksandrov p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; 20057f0aec7aSNikolay Aleksandrov __del_port_router(p); 20067f0aec7aSNikolay Aleksandrov break; 20077f0aec7aSNikolay Aleksandrov case MDB_RTR_TYPE_PERM: 20087f0aec7aSNikolay Aleksandrov p->multicast_router = MDB_RTR_TYPE_PERM; 20097f0aec7aSNikolay Aleksandrov del_timer(&p->multicast_router_timer); 20100909e117SHerbert Xu br_multicast_add_router(br, p); 20110909e117SHerbert Xu break; 2012a55d8246SNikolay Aleksandrov case MDB_RTR_TYPE_TEMP: 2013a55d8246SNikolay Aleksandrov p->multicast_router = MDB_RTR_TYPE_TEMP; 2014a55d8246SNikolay Aleksandrov br_multicast_mark_router(br, p); 2015a55d8246SNikolay Aleksandrov break; 20167f0aec7aSNikolay Aleksandrov default: 20177f0aec7aSNikolay Aleksandrov goto unlock; 20180909e117SHerbert Xu } 20197f0aec7aSNikolay Aleksandrov err = 0; 20207f0aec7aSNikolay Aleksandrov unlock: 20210909e117SHerbert Xu spin_unlock(&br->multicast_lock); 20220909e117SHerbert Xu 20230909e117SHerbert Xu return err; 20240909e117SHerbert Xu } 2025561f1103SHerbert Xu 2026cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br, 202790010b36SLinus Lüssing struct bridge_mcast_own_query *query) 2028561f1103SHerbert Xu { 2029561f1103SHerbert Xu struct net_bridge_port *port; 203074857216SHerbert Xu 2031cc0fdd80SLinus Lüssing __br_multicast_open(br, query); 203274857216SHerbert Xu 2033c5b493ceSNikolay Aleksandrov rcu_read_lock(); 2034c5b493ceSNikolay Aleksandrov list_for_each_entry_rcu(port, &br->port_list, list) { 203574857216SHerbert Xu if (port->state == BR_STATE_DISABLED || 203674857216SHerbert Xu port->state == BR_STATE_BLOCKING) 203774857216SHerbert Xu continue; 203874857216SHerbert Xu 203990010b36SLinus Lüssing if (query == &br->ip4_own_query) 204090010b36SLinus Lüssing br_multicast_enable(&port->ip4_own_query); 2041cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 2042cc0fdd80SLinus Lüssing else 204390010b36SLinus Lüssing br_multicast_enable(&port->ip6_own_query); 2044cc0fdd80SLinus Lüssing #endif 204574857216SHerbert Xu } 2046c5b493ceSNikolay Aleksandrov rcu_read_unlock(); 204774857216SHerbert Xu } 204874857216SHerbert Xu 204974857216SHerbert Xu int br_multicast_toggle(struct net_bridge *br, unsigned long val) 205074857216SHerbert Xu { 20517cb3f921SNikolay Aleksandrov struct net_bridge_port *port; 2052561f1103SHerbert Xu 2053ef5e0d82SAndrey Vagin spin_lock_bh(&br->multicast_lock); 205413cefad2SNikolay Aleksandrov if (!!br_opt_get(br, BROPT_MULTICAST_ENABLED) == !!val) 2055561f1103SHerbert Xu goto unlock; 2056561f1103SHerbert Xu 205713cefad2SNikolay Aleksandrov br_mc_disabled_update(br->dev, val); 205813cefad2SNikolay Aleksandrov br_opt_toggle(br, BROPT_MULTICAST_ENABLED, !!val); 20594effd28cSLinus Lüssing if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) { 20604effd28cSLinus Lüssing br_multicast_leave_snoopers(br); 2061561f1103SHerbert Xu goto unlock; 20624effd28cSLinus Lüssing } 2063561f1103SHerbert Xu 20643a7fda06SHerbert Xu if (!netif_running(br->dev)) 20653a7fda06SHerbert Xu goto unlock; 20663a7fda06SHerbert Xu 20677cb3f921SNikolay Aleksandrov br_multicast_open(br); 20687cb3f921SNikolay Aleksandrov list_for_each_entry(port, &br->port_list, list) 20697cb3f921SNikolay Aleksandrov __br_multicast_enable_port(port); 2070561f1103SHerbert Xu 2071561f1103SHerbert Xu unlock: 2072ef5e0d82SAndrey Vagin spin_unlock_bh(&br->multicast_lock); 2073561f1103SHerbert Xu 2074a26d94bfSYueHaibing return 0; 2075561f1103SHerbert Xu } 2076b195167fSHerbert Xu 20779341b988SIdo Schimmel bool br_multicast_enabled(const struct net_device *dev) 20789341b988SIdo Schimmel { 20799341b988SIdo Schimmel struct net_bridge *br = netdev_priv(dev); 20809341b988SIdo Schimmel 208113cefad2SNikolay Aleksandrov return !!br_opt_get(br, BROPT_MULTICAST_ENABLED); 20829341b988SIdo Schimmel } 20839341b988SIdo Schimmel EXPORT_SYMBOL_GPL(br_multicast_enabled); 20849341b988SIdo Schimmel 20850912bda4SYotam Gigi bool br_multicast_router(const struct net_device *dev) 20860912bda4SYotam Gigi { 20870912bda4SYotam Gigi struct net_bridge *br = netdev_priv(dev); 20880912bda4SYotam Gigi bool is_router; 20890912bda4SYotam Gigi 20900912bda4SYotam Gigi spin_lock_bh(&br->multicast_lock); 20910912bda4SYotam Gigi is_router = br_multicast_is_router(br); 20920912bda4SYotam Gigi spin_unlock_bh(&br->multicast_lock); 20930912bda4SYotam Gigi return is_router; 20940912bda4SYotam Gigi } 20950912bda4SYotam Gigi EXPORT_SYMBOL_GPL(br_multicast_router); 20960912bda4SYotam Gigi 2097c5c23260SHerbert Xu int br_multicast_set_querier(struct net_bridge *br, unsigned long val) 2098c5c23260SHerbert Xu { 2099b00589afSLinus Lüssing unsigned long max_delay; 2100b00589afSLinus Lüssing 2101c5c23260SHerbert Xu val = !!val; 2102c5c23260SHerbert Xu 2103c5c23260SHerbert Xu spin_lock_bh(&br->multicast_lock); 2104675779adSNikolay Aleksandrov if (br_opt_get(br, BROPT_MULTICAST_QUERIER) == val) 2105c5c23260SHerbert Xu goto unlock; 2106c5c23260SHerbert Xu 2107675779adSNikolay Aleksandrov br_opt_toggle(br, BROPT_MULTICAST_QUERIER, !!val); 2108b00589afSLinus Lüssing if (!val) 2109b00589afSLinus Lüssing goto unlock; 2110b00589afSLinus Lüssing 2111b00589afSLinus Lüssing max_delay = br->multicast_query_response_interval; 2112b00589afSLinus Lüssing 211390010b36SLinus Lüssing if (!timer_pending(&br->ip4_other_query.timer)) 211490010b36SLinus Lüssing br->ip4_other_query.delay_time = jiffies + max_delay; 2115cc0fdd80SLinus Lüssing 211690010b36SLinus Lüssing br_multicast_start_querier(br, &br->ip4_own_query); 2117cc0fdd80SLinus Lüssing 2118cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 211990010b36SLinus Lüssing if (!timer_pending(&br->ip6_other_query.timer)) 212090010b36SLinus Lüssing br->ip6_other_query.delay_time = jiffies + max_delay; 2121cc0fdd80SLinus Lüssing 212290010b36SLinus Lüssing br_multicast_start_querier(br, &br->ip6_own_query); 2123cc0fdd80SLinus Lüssing #endif 2124c5c23260SHerbert Xu 2125c5c23260SHerbert Xu unlock: 2126c5c23260SHerbert Xu spin_unlock_bh(&br->multicast_lock); 2127c5c23260SHerbert Xu 2128c5c23260SHerbert Xu return 0; 2129c5c23260SHerbert Xu } 2130c5c23260SHerbert Xu 21315e923585SNikolay Aleksandrov int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val) 21325e923585SNikolay Aleksandrov { 21335e923585SNikolay Aleksandrov /* Currently we support only version 2 and 3 */ 21345e923585SNikolay Aleksandrov switch (val) { 21355e923585SNikolay Aleksandrov case 2: 21365e923585SNikolay Aleksandrov case 3: 21375e923585SNikolay Aleksandrov break; 21385e923585SNikolay Aleksandrov default: 21395e923585SNikolay Aleksandrov return -EINVAL; 21405e923585SNikolay Aleksandrov } 21415e923585SNikolay Aleksandrov 21425e923585SNikolay Aleksandrov spin_lock_bh(&br->multicast_lock); 21435e923585SNikolay Aleksandrov br->multicast_igmp_version = val; 21445e923585SNikolay Aleksandrov spin_unlock_bh(&br->multicast_lock); 21455e923585SNikolay Aleksandrov 21465e923585SNikolay Aleksandrov return 0; 21475e923585SNikolay Aleksandrov } 21485e923585SNikolay Aleksandrov 2149aa2ae3e7SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 2150aa2ae3e7SNikolay Aleksandrov int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val) 2151aa2ae3e7SNikolay Aleksandrov { 2152aa2ae3e7SNikolay Aleksandrov /* Currently we support version 1 and 2 */ 2153aa2ae3e7SNikolay Aleksandrov switch (val) { 2154aa2ae3e7SNikolay Aleksandrov case 1: 2155aa2ae3e7SNikolay Aleksandrov case 2: 2156aa2ae3e7SNikolay Aleksandrov break; 2157aa2ae3e7SNikolay Aleksandrov default: 2158aa2ae3e7SNikolay Aleksandrov return -EINVAL; 2159aa2ae3e7SNikolay Aleksandrov } 2160aa2ae3e7SNikolay Aleksandrov 2161aa2ae3e7SNikolay Aleksandrov spin_lock_bh(&br->multicast_lock); 2162aa2ae3e7SNikolay Aleksandrov br->multicast_mld_version = val; 2163aa2ae3e7SNikolay Aleksandrov spin_unlock_bh(&br->multicast_lock); 2164aa2ae3e7SNikolay Aleksandrov 2165aa2ae3e7SNikolay Aleksandrov return 0; 2166aa2ae3e7SNikolay Aleksandrov } 2167aa2ae3e7SNikolay Aleksandrov #endif 2168aa2ae3e7SNikolay Aleksandrov 216907f8ac4aSLinus Lüssing /** 217007f8ac4aSLinus Lüssing * br_multicast_list_adjacent - Returns snooped multicast addresses 217107f8ac4aSLinus Lüssing * @dev: The bridge port adjacent to which to retrieve addresses 217207f8ac4aSLinus Lüssing * @br_ip_list: The list to store found, snooped multicast IP addresses in 217307f8ac4aSLinus Lüssing * 217407f8ac4aSLinus Lüssing * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast 217507f8ac4aSLinus Lüssing * snooping feature on all bridge ports of dev's bridge device, excluding 217607f8ac4aSLinus Lüssing * the addresses from dev itself. 217707f8ac4aSLinus Lüssing * 217807f8ac4aSLinus Lüssing * Returns the number of items added to br_ip_list. 217907f8ac4aSLinus Lüssing * 218007f8ac4aSLinus Lüssing * Notes: 218107f8ac4aSLinus Lüssing * - br_ip_list needs to be initialized by caller 218207f8ac4aSLinus Lüssing * - br_ip_list might contain duplicates in the end 218307f8ac4aSLinus Lüssing * (needs to be taken care of by caller) 218407f8ac4aSLinus Lüssing * - br_ip_list needs to be freed by caller 218507f8ac4aSLinus Lüssing */ 218607f8ac4aSLinus Lüssing int br_multicast_list_adjacent(struct net_device *dev, 218707f8ac4aSLinus Lüssing struct list_head *br_ip_list) 218807f8ac4aSLinus Lüssing { 218907f8ac4aSLinus Lüssing struct net_bridge *br; 219007f8ac4aSLinus Lüssing struct net_bridge_port *port; 219107f8ac4aSLinus Lüssing struct net_bridge_port_group *group; 219207f8ac4aSLinus Lüssing struct br_ip_list *entry; 219307f8ac4aSLinus Lüssing int count = 0; 219407f8ac4aSLinus Lüssing 219507f8ac4aSLinus Lüssing rcu_read_lock(); 219635f861e3SJulian Wiedmann if (!br_ip_list || !netif_is_bridge_port(dev)) 219707f8ac4aSLinus Lüssing goto unlock; 219807f8ac4aSLinus Lüssing 219907f8ac4aSLinus Lüssing port = br_port_get_rcu(dev); 220007f8ac4aSLinus Lüssing if (!port || !port->br) 220107f8ac4aSLinus Lüssing goto unlock; 220207f8ac4aSLinus Lüssing 220307f8ac4aSLinus Lüssing br = port->br; 220407f8ac4aSLinus Lüssing 220507f8ac4aSLinus Lüssing list_for_each_entry_rcu(port, &br->port_list, list) { 220607f8ac4aSLinus Lüssing if (!port->dev || port->dev == dev) 220707f8ac4aSLinus Lüssing continue; 220807f8ac4aSLinus Lüssing 220907f8ac4aSLinus Lüssing hlist_for_each_entry_rcu(group, &port->mglist, mglist) { 221007f8ac4aSLinus Lüssing entry = kmalloc(sizeof(*entry), GFP_ATOMIC); 221107f8ac4aSLinus Lüssing if (!entry) 221207f8ac4aSLinus Lüssing goto unlock; 221307f8ac4aSLinus Lüssing 221407f8ac4aSLinus Lüssing entry->addr = group->addr; 221507f8ac4aSLinus Lüssing list_add(&entry->list, br_ip_list); 221607f8ac4aSLinus Lüssing count++; 221707f8ac4aSLinus Lüssing } 221807f8ac4aSLinus Lüssing } 221907f8ac4aSLinus Lüssing 222007f8ac4aSLinus Lüssing unlock: 222107f8ac4aSLinus Lüssing rcu_read_unlock(); 222207f8ac4aSLinus Lüssing return count; 222307f8ac4aSLinus Lüssing } 222407f8ac4aSLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_list_adjacent); 22252cd41431SLinus Lüssing 22262cd41431SLinus Lüssing /** 2227c34963e2SLinus Lüssing * br_multicast_has_querier_anywhere - Checks for a querier on a bridge 2228c34963e2SLinus Lüssing * @dev: The bridge port providing the bridge on which to check for a querier 2229c34963e2SLinus Lüssing * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 2230c34963e2SLinus Lüssing * 2231c34963e2SLinus Lüssing * Checks whether the given interface has a bridge on top and if so returns 2232c34963e2SLinus Lüssing * true if a valid querier exists anywhere on the bridged link layer. 2233c34963e2SLinus Lüssing * Otherwise returns false. 2234c34963e2SLinus Lüssing */ 2235c34963e2SLinus Lüssing bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto) 2236c34963e2SLinus Lüssing { 2237c34963e2SLinus Lüssing struct net_bridge *br; 2238c34963e2SLinus Lüssing struct net_bridge_port *port; 2239c34963e2SLinus Lüssing struct ethhdr eth; 2240c34963e2SLinus Lüssing bool ret = false; 2241c34963e2SLinus Lüssing 2242c34963e2SLinus Lüssing rcu_read_lock(); 224335f861e3SJulian Wiedmann if (!netif_is_bridge_port(dev)) 2244c34963e2SLinus Lüssing goto unlock; 2245c34963e2SLinus Lüssing 2246c34963e2SLinus Lüssing port = br_port_get_rcu(dev); 2247c34963e2SLinus Lüssing if (!port || !port->br) 2248c34963e2SLinus Lüssing goto unlock; 2249c34963e2SLinus Lüssing 2250c34963e2SLinus Lüssing br = port->br; 2251c34963e2SLinus Lüssing 2252c34963e2SLinus Lüssing memset(ð, 0, sizeof(eth)); 2253c34963e2SLinus Lüssing eth.h_proto = htons(proto); 2254c34963e2SLinus Lüssing 2255c34963e2SLinus Lüssing ret = br_multicast_querier_exists(br, ð); 2256c34963e2SLinus Lüssing 2257c34963e2SLinus Lüssing unlock: 2258c34963e2SLinus Lüssing rcu_read_unlock(); 2259c34963e2SLinus Lüssing return ret; 2260c34963e2SLinus Lüssing } 2261c34963e2SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere); 2262c34963e2SLinus Lüssing 2263c34963e2SLinus Lüssing /** 22642cd41431SLinus Lüssing * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port 22652cd41431SLinus Lüssing * @dev: The bridge port adjacent to which to check for a querier 22662cd41431SLinus Lüssing * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 22672cd41431SLinus Lüssing * 22682cd41431SLinus Lüssing * Checks whether the given interface has a bridge on top and if so returns 22692cd41431SLinus Lüssing * true if a selected querier is behind one of the other ports of this 22702cd41431SLinus Lüssing * bridge. Otherwise returns false. 22712cd41431SLinus Lüssing */ 22722cd41431SLinus Lüssing bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto) 22732cd41431SLinus Lüssing { 22742cd41431SLinus Lüssing struct net_bridge *br; 22752cd41431SLinus Lüssing struct net_bridge_port *port; 22762cd41431SLinus Lüssing bool ret = false; 22772cd41431SLinus Lüssing 22782cd41431SLinus Lüssing rcu_read_lock(); 227935f861e3SJulian Wiedmann if (!netif_is_bridge_port(dev)) 22802cd41431SLinus Lüssing goto unlock; 22812cd41431SLinus Lüssing 22822cd41431SLinus Lüssing port = br_port_get_rcu(dev); 22832cd41431SLinus Lüssing if (!port || !port->br) 22842cd41431SLinus Lüssing goto unlock; 22852cd41431SLinus Lüssing 22862cd41431SLinus Lüssing br = port->br; 22872cd41431SLinus Lüssing 22882cd41431SLinus Lüssing switch (proto) { 22892cd41431SLinus Lüssing case ETH_P_IP: 22902cd41431SLinus Lüssing if (!timer_pending(&br->ip4_other_query.timer) || 22912cd41431SLinus Lüssing rcu_dereference(br->ip4_querier.port) == port) 22922cd41431SLinus Lüssing goto unlock; 22932cd41431SLinus Lüssing break; 22943993c4e1SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 22952cd41431SLinus Lüssing case ETH_P_IPV6: 22962cd41431SLinus Lüssing if (!timer_pending(&br->ip6_other_query.timer) || 22972cd41431SLinus Lüssing rcu_dereference(br->ip6_querier.port) == port) 22982cd41431SLinus Lüssing goto unlock; 22992cd41431SLinus Lüssing break; 23003993c4e1SLinus Lüssing #endif 23012cd41431SLinus Lüssing default: 23022cd41431SLinus Lüssing goto unlock; 23032cd41431SLinus Lüssing } 23042cd41431SLinus Lüssing 23052cd41431SLinus Lüssing ret = true; 23062cd41431SLinus Lüssing unlock: 23072cd41431SLinus Lüssing rcu_read_unlock(); 23082cd41431SLinus Lüssing return ret; 23092cd41431SLinus Lüssing } 23102cd41431SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent); 23111080ab95SNikolay Aleksandrov 23121080ab95SNikolay Aleksandrov static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats, 2313a65056ecSNikolay Aleksandrov const struct sk_buff *skb, u8 type, u8 dir) 23141080ab95SNikolay Aleksandrov { 23151080ab95SNikolay Aleksandrov struct bridge_mcast_stats *pstats = this_cpu_ptr(stats); 2316a65056ecSNikolay Aleksandrov __be16 proto = skb->protocol; 2317a65056ecSNikolay Aleksandrov unsigned int t_len; 23181080ab95SNikolay Aleksandrov 23191080ab95SNikolay Aleksandrov u64_stats_update_begin(&pstats->syncp); 23201080ab95SNikolay Aleksandrov switch (proto) { 23211080ab95SNikolay Aleksandrov case htons(ETH_P_IP): 2322a65056ecSNikolay Aleksandrov t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb); 23231080ab95SNikolay Aleksandrov switch (type) { 23241080ab95SNikolay Aleksandrov case IGMP_HOST_MEMBERSHIP_REPORT: 23251080ab95SNikolay Aleksandrov pstats->mstats.igmp_v1reports[dir]++; 23261080ab95SNikolay Aleksandrov break; 23271080ab95SNikolay Aleksandrov case IGMPV2_HOST_MEMBERSHIP_REPORT: 23281080ab95SNikolay Aleksandrov pstats->mstats.igmp_v2reports[dir]++; 23291080ab95SNikolay Aleksandrov break; 23301080ab95SNikolay Aleksandrov case IGMPV3_HOST_MEMBERSHIP_REPORT: 23311080ab95SNikolay Aleksandrov pstats->mstats.igmp_v3reports[dir]++; 23321080ab95SNikolay Aleksandrov break; 23331080ab95SNikolay Aleksandrov case IGMP_HOST_MEMBERSHIP_QUERY: 2334a65056ecSNikolay Aleksandrov if (t_len != sizeof(struct igmphdr)) { 2335a65056ecSNikolay Aleksandrov pstats->mstats.igmp_v3queries[dir]++; 2336a65056ecSNikolay Aleksandrov } else { 2337a65056ecSNikolay Aleksandrov unsigned int offset = skb_transport_offset(skb); 2338a65056ecSNikolay Aleksandrov struct igmphdr *ih, _ihdr; 2339a65056ecSNikolay Aleksandrov 2340a65056ecSNikolay Aleksandrov ih = skb_header_pointer(skb, offset, 2341a65056ecSNikolay Aleksandrov sizeof(_ihdr), &_ihdr); 2342a65056ecSNikolay Aleksandrov if (!ih) 2343a65056ecSNikolay Aleksandrov break; 2344a65056ecSNikolay Aleksandrov if (!ih->code) 2345a65056ecSNikolay Aleksandrov pstats->mstats.igmp_v1queries[dir]++; 2346a65056ecSNikolay Aleksandrov else 2347a65056ecSNikolay Aleksandrov pstats->mstats.igmp_v2queries[dir]++; 2348a65056ecSNikolay Aleksandrov } 23491080ab95SNikolay Aleksandrov break; 23501080ab95SNikolay Aleksandrov case IGMP_HOST_LEAVE_MESSAGE: 23511080ab95SNikolay Aleksandrov pstats->mstats.igmp_leaves[dir]++; 23521080ab95SNikolay Aleksandrov break; 23531080ab95SNikolay Aleksandrov } 23541080ab95SNikolay Aleksandrov break; 23551080ab95SNikolay Aleksandrov #if IS_ENABLED(CONFIG_IPV6) 23561080ab95SNikolay Aleksandrov case htons(ETH_P_IPV6): 2357a65056ecSNikolay Aleksandrov t_len = ntohs(ipv6_hdr(skb)->payload_len) + 2358a65056ecSNikolay Aleksandrov sizeof(struct ipv6hdr); 2359a65056ecSNikolay Aleksandrov t_len -= skb_network_header_len(skb); 23601080ab95SNikolay Aleksandrov switch (type) { 23611080ab95SNikolay Aleksandrov case ICMPV6_MGM_REPORT: 23621080ab95SNikolay Aleksandrov pstats->mstats.mld_v1reports[dir]++; 23631080ab95SNikolay Aleksandrov break; 23641080ab95SNikolay Aleksandrov case ICMPV6_MLD2_REPORT: 23651080ab95SNikolay Aleksandrov pstats->mstats.mld_v2reports[dir]++; 23661080ab95SNikolay Aleksandrov break; 23671080ab95SNikolay Aleksandrov case ICMPV6_MGM_QUERY: 2368a65056ecSNikolay Aleksandrov if (t_len != sizeof(struct mld_msg)) 2369a65056ecSNikolay Aleksandrov pstats->mstats.mld_v2queries[dir]++; 2370a65056ecSNikolay Aleksandrov else 2371a65056ecSNikolay Aleksandrov pstats->mstats.mld_v1queries[dir]++; 23721080ab95SNikolay Aleksandrov break; 23731080ab95SNikolay Aleksandrov case ICMPV6_MGM_REDUCTION: 23741080ab95SNikolay Aleksandrov pstats->mstats.mld_leaves[dir]++; 23751080ab95SNikolay Aleksandrov break; 23761080ab95SNikolay Aleksandrov } 23771080ab95SNikolay Aleksandrov break; 23781080ab95SNikolay Aleksandrov #endif /* CONFIG_IPV6 */ 23791080ab95SNikolay Aleksandrov } 23801080ab95SNikolay Aleksandrov u64_stats_update_end(&pstats->syncp); 23811080ab95SNikolay Aleksandrov } 23821080ab95SNikolay Aleksandrov 23831080ab95SNikolay Aleksandrov void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p, 2384a65056ecSNikolay Aleksandrov const struct sk_buff *skb, u8 type, u8 dir) 23851080ab95SNikolay Aleksandrov { 23861080ab95SNikolay Aleksandrov struct bridge_mcast_stats __percpu *stats; 23871080ab95SNikolay Aleksandrov 23881080ab95SNikolay Aleksandrov /* if multicast_disabled is true then igmp type can't be set */ 2389675779adSNikolay Aleksandrov if (!type || !br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)) 23901080ab95SNikolay Aleksandrov return; 23911080ab95SNikolay Aleksandrov 23921080ab95SNikolay Aleksandrov if (p) 23931080ab95SNikolay Aleksandrov stats = p->mcast_stats; 23941080ab95SNikolay Aleksandrov else 23951080ab95SNikolay Aleksandrov stats = br->mcast_stats; 23961080ab95SNikolay Aleksandrov if (WARN_ON(!stats)) 23971080ab95SNikolay Aleksandrov return; 23981080ab95SNikolay Aleksandrov 2399a65056ecSNikolay Aleksandrov br_mcast_stats_add(stats, skb, type, dir); 24001080ab95SNikolay Aleksandrov } 24011080ab95SNikolay Aleksandrov 24021080ab95SNikolay Aleksandrov int br_multicast_init_stats(struct net_bridge *br) 24031080ab95SNikolay Aleksandrov { 24041080ab95SNikolay Aleksandrov br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats); 24051080ab95SNikolay Aleksandrov if (!br->mcast_stats) 24061080ab95SNikolay Aleksandrov return -ENOMEM; 24071080ab95SNikolay Aleksandrov 24081080ab95SNikolay Aleksandrov return 0; 24091080ab95SNikolay Aleksandrov } 24101080ab95SNikolay Aleksandrov 2411b6fe0440SIdo Schimmel void br_multicast_uninit_stats(struct net_bridge *br) 2412b6fe0440SIdo Schimmel { 2413b6fe0440SIdo Schimmel free_percpu(br->mcast_stats); 2414b6fe0440SIdo Schimmel } 2415b6fe0440SIdo Schimmel 24161080ab95SNikolay Aleksandrov static void mcast_stats_add_dir(u64 *dst, u64 *src) 24171080ab95SNikolay Aleksandrov { 24181080ab95SNikolay Aleksandrov dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX]; 24191080ab95SNikolay Aleksandrov dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX]; 24201080ab95SNikolay Aleksandrov } 24211080ab95SNikolay Aleksandrov 24221080ab95SNikolay Aleksandrov void br_multicast_get_stats(const struct net_bridge *br, 24231080ab95SNikolay Aleksandrov const struct net_bridge_port *p, 24241080ab95SNikolay Aleksandrov struct br_mcast_stats *dest) 24251080ab95SNikolay Aleksandrov { 24261080ab95SNikolay Aleksandrov struct bridge_mcast_stats __percpu *stats; 24271080ab95SNikolay Aleksandrov struct br_mcast_stats tdst; 24281080ab95SNikolay Aleksandrov int i; 24291080ab95SNikolay Aleksandrov 24301080ab95SNikolay Aleksandrov memset(dest, 0, sizeof(*dest)); 24311080ab95SNikolay Aleksandrov if (p) 24321080ab95SNikolay Aleksandrov stats = p->mcast_stats; 24331080ab95SNikolay Aleksandrov else 24341080ab95SNikolay Aleksandrov stats = br->mcast_stats; 24351080ab95SNikolay Aleksandrov if (WARN_ON(!stats)) 24361080ab95SNikolay Aleksandrov return; 24371080ab95SNikolay Aleksandrov 24381080ab95SNikolay Aleksandrov memset(&tdst, 0, sizeof(tdst)); 24391080ab95SNikolay Aleksandrov for_each_possible_cpu(i) { 24401080ab95SNikolay Aleksandrov struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i); 24411080ab95SNikolay Aleksandrov struct br_mcast_stats temp; 24421080ab95SNikolay Aleksandrov unsigned int start; 24431080ab95SNikolay Aleksandrov 24441080ab95SNikolay Aleksandrov do { 24451080ab95SNikolay Aleksandrov start = u64_stats_fetch_begin_irq(&cpu_stats->syncp); 24461080ab95SNikolay Aleksandrov memcpy(&temp, &cpu_stats->mstats, sizeof(temp)); 24471080ab95SNikolay Aleksandrov } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start)); 24481080ab95SNikolay Aleksandrov 2449a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries); 2450a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries); 2451a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries); 24521080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves); 24531080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports); 24541080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports); 24551080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports); 24561080ab95SNikolay Aleksandrov tdst.igmp_parse_errors += temp.igmp_parse_errors; 24571080ab95SNikolay Aleksandrov 2458a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries); 2459a65056ecSNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries); 24601080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves); 24611080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports); 24621080ab95SNikolay Aleksandrov mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports); 24631080ab95SNikolay Aleksandrov tdst.mld_parse_errors += temp.mld_parse_errors; 24641080ab95SNikolay Aleksandrov } 24651080ab95SNikolay Aleksandrov memcpy(dest, &tdst, sizeof(*dest)); 24661080ab95SNikolay Aleksandrov } 246719e3a9c9SNikolay Aleksandrov 246819e3a9c9SNikolay Aleksandrov int br_mdb_hash_init(struct net_bridge *br) 246919e3a9c9SNikolay Aleksandrov { 247019e3a9c9SNikolay Aleksandrov return rhashtable_init(&br->mdb_hash_tbl, &br_mdb_rht_params); 247119e3a9c9SNikolay Aleksandrov } 247219e3a9c9SNikolay Aleksandrov 247319e3a9c9SNikolay Aleksandrov void br_mdb_hash_fini(struct net_bridge *br) 247419e3a9c9SNikolay Aleksandrov { 247519e3a9c9SNikolay Aleksandrov rhashtable_destroy(&br->mdb_hash_tbl); 247619e3a9c9SNikolay Aleksandrov } 2477