1eb1d1641SHerbert Xu /* 2eb1d1641SHerbert Xu * Bridge multicast support. 3eb1d1641SHerbert Xu * 4eb1d1641SHerbert Xu * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au> 5eb1d1641SHerbert Xu * 6eb1d1641SHerbert Xu * This program is free software; you can redistribute it and/or modify it 7eb1d1641SHerbert Xu * under the terms of the GNU General Public License as published by the Free 8eb1d1641SHerbert Xu * Software Foundation; either version 2 of the License, or (at your option) 9eb1d1641SHerbert Xu * any later version. 10eb1d1641SHerbert Xu * 11eb1d1641SHerbert Xu */ 12eb1d1641SHerbert Xu 13eb1d1641SHerbert Xu #include <linux/err.h> 1407f8ac4aSLinus Lüssing #include <linux/export.h> 15eb1d1641SHerbert Xu #include <linux/if_ether.h> 16eb1d1641SHerbert Xu #include <linux/igmp.h> 17eb1d1641SHerbert Xu #include <linux/jhash.h> 18eb1d1641SHerbert Xu #include <linux/kernel.h> 19b195167fSHerbert Xu #include <linux/log2.h> 20eb1d1641SHerbert Xu #include <linux/netdevice.h> 21eb1d1641SHerbert Xu #include <linux/netfilter_bridge.h> 22eb1d1641SHerbert Xu #include <linux/random.h> 23eb1d1641SHerbert Xu #include <linux/rculist.h> 24eb1d1641SHerbert Xu #include <linux/skbuff.h> 25eb1d1641SHerbert Xu #include <linux/slab.h> 26eb1d1641SHerbert Xu #include <linux/timer.h> 271c8ad5bfSCong Wang #include <linux/inetdevice.h> 28eb1d1641SHerbert Xu #include <net/ip.h> 29dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 3008b202b6SYOSHIFUJI Hideaki #include <net/ipv6.h> 3108b202b6SYOSHIFUJI Hideaki #include <net/mld.h> 32d4c4f07dSDavid S. Miller #include <net/ip6_checksum.h> 333c3769e6SLinus Lüssing #include <net/addrconf.h> 3408b202b6SYOSHIFUJI Hideaki #endif 35eb1d1641SHerbert Xu 36eb1d1641SHerbert Xu #include "br_private.h" 37eb1d1641SHerbert Xu 38cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br, 3990010b36SLinus Lüssing struct bridge_mcast_own_query *query); 40754bc547SSatish Ashok static void br_multicast_add_router(struct net_bridge *br, 41754bc547SSatish Ashok struct net_bridge_port *port); 42bc8c20acSSatish Ashok static void br_ip4_multicast_leave_group(struct net_bridge *br, 43bc8c20acSSatish Ashok struct net_bridge_port *port, 44bc8c20acSSatish Ashok __be32 group, 45bc8c20acSSatish Ashok __u16 vid); 46bc8c20acSSatish Ashok #if IS_ENABLED(CONFIG_IPV6) 47bc8c20acSSatish Ashok static void br_ip6_multicast_leave_group(struct net_bridge *br, 48bc8c20acSSatish Ashok struct net_bridge_port *port, 49bc8c20acSSatish Ashok const struct in6_addr *group, 50bc8c20acSSatish Ashok __u16 vid); 51bc8c20acSSatish Ashok #endif 522ce297fcSCong Wang unsigned int br_mdb_rehash_seq; 53c83b8fabSHerbert Xu 548ef2a9a5SYOSHIFUJI Hideaki static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b) 558ef2a9a5SYOSHIFUJI Hideaki { 568ef2a9a5SYOSHIFUJI Hideaki if (a->proto != b->proto) 578ef2a9a5SYOSHIFUJI Hideaki return 0; 58b0e9a30dSVlad Yasevich if (a->vid != b->vid) 59b0e9a30dSVlad Yasevich return 0; 608ef2a9a5SYOSHIFUJI Hideaki switch (a->proto) { 618ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 628ef2a9a5SYOSHIFUJI Hideaki return a->u.ip4 == b->u.ip4; 63dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 6408b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 6508b202b6SYOSHIFUJI Hideaki return ipv6_addr_equal(&a->u.ip6, &b->u.ip6); 6608b202b6SYOSHIFUJI Hideaki #endif 678ef2a9a5SYOSHIFUJI Hideaki } 688ef2a9a5SYOSHIFUJI Hideaki return 0; 698ef2a9a5SYOSHIFUJI Hideaki } 708ef2a9a5SYOSHIFUJI Hideaki 71b0e9a30dSVlad Yasevich static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip, 72b0e9a30dSVlad Yasevich __u16 vid) 73eb1d1641SHerbert Xu { 74b0e9a30dSVlad Yasevich return jhash_2words((__force u32)ip, vid, mdb->secret) & (mdb->max - 1); 75eb1d1641SHerbert Xu } 76eb1d1641SHerbert Xu 77dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 7808b202b6SYOSHIFUJI Hideaki static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb, 79b0e9a30dSVlad Yasevich const struct in6_addr *ip, 80b0e9a30dSVlad Yasevich __u16 vid) 8108b202b6SYOSHIFUJI Hideaki { 82b0e9a30dSVlad Yasevich return jhash_2words(ipv6_addr_hash(ip), vid, 83b0e9a30dSVlad Yasevich mdb->secret) & (mdb->max - 1); 8408b202b6SYOSHIFUJI Hideaki } 8508b202b6SYOSHIFUJI Hideaki #endif 8608b202b6SYOSHIFUJI Hideaki 878ef2a9a5SYOSHIFUJI Hideaki static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb, 888ef2a9a5SYOSHIFUJI Hideaki struct br_ip *ip) 898ef2a9a5SYOSHIFUJI Hideaki { 908ef2a9a5SYOSHIFUJI Hideaki switch (ip->proto) { 918ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 92b0e9a30dSVlad Yasevich return __br_ip4_hash(mdb, ip->u.ip4, ip->vid); 93dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 9408b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 95b0e9a30dSVlad Yasevich return __br_ip6_hash(mdb, &ip->u.ip6, ip->vid); 9608b202b6SYOSHIFUJI Hideaki #endif 978ef2a9a5SYOSHIFUJI Hideaki } 988ef2a9a5SYOSHIFUJI Hideaki return 0; 99eb1d1641SHerbert Xu } 100eb1d1641SHerbert Xu 101eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *__br_mdb_ip_get( 1028ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash) 103eb1d1641SHerbert Xu { 104eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 105eb1d1641SHerbert Xu 106b67bfe0dSSasha Levin hlist_for_each_entry_rcu(mp, &mdb->mhash[hash], hlist[mdb->ver]) { 1078ef2a9a5SYOSHIFUJI Hideaki if (br_ip_equal(&mp->addr, dst)) 108eb1d1641SHerbert Xu return mp; 109eb1d1641SHerbert Xu } 110eb1d1641SHerbert Xu 111eb1d1641SHerbert Xu return NULL; 112eb1d1641SHerbert Xu } 113eb1d1641SHerbert Xu 114cfd56754SCong Wang struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge_mdb_htable *mdb, 115cfd56754SCong Wang struct br_ip *dst) 1167f285fa7SHerbert Xu { 1177f285fa7SHerbert Xu if (!mdb) 1187f285fa7SHerbert Xu return NULL; 1197f285fa7SHerbert Xu 1207f285fa7SHerbert Xu return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst)); 1217f285fa7SHerbert Xu } 1227f285fa7SHerbert Xu 1238ef2a9a5SYOSHIFUJI Hideaki static struct net_bridge_mdb_entry *br_mdb_ip4_get( 124b0e9a30dSVlad Yasevich struct net_bridge_mdb_htable *mdb, __be32 dst, __u16 vid) 125eb1d1641SHerbert Xu { 1268ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_dst; 1270821ec55SHerbert Xu 1288ef2a9a5SYOSHIFUJI Hideaki br_dst.u.ip4 = dst; 1298ef2a9a5SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IP); 130b0e9a30dSVlad Yasevich br_dst.vid = vid; 1318ef2a9a5SYOSHIFUJI Hideaki 1327f285fa7SHerbert Xu return br_mdb_ip_get(mdb, &br_dst); 1338ef2a9a5SYOSHIFUJI Hideaki } 1348ef2a9a5SYOSHIFUJI Hideaki 135dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 13608b202b6SYOSHIFUJI Hideaki static struct net_bridge_mdb_entry *br_mdb_ip6_get( 137b0e9a30dSVlad Yasevich struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst, 138b0e9a30dSVlad Yasevich __u16 vid) 13908b202b6SYOSHIFUJI Hideaki { 14008b202b6SYOSHIFUJI Hideaki struct br_ip br_dst; 14108b202b6SYOSHIFUJI Hideaki 1424e3fd7a0SAlexey Dobriyan br_dst.u.ip6 = *dst; 14308b202b6SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IPV6); 144b0e9a30dSVlad Yasevich br_dst.vid = vid; 14508b202b6SYOSHIFUJI Hideaki 1467f285fa7SHerbert Xu return br_mdb_ip_get(mdb, &br_dst); 14708b202b6SYOSHIFUJI Hideaki } 14808b202b6SYOSHIFUJI Hideaki #endif 14908b202b6SYOSHIFUJI Hideaki 150eb1d1641SHerbert Xu struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br, 151fbca58a2SCong Wang struct sk_buff *skb, u16 vid) 152eb1d1641SHerbert Xu { 153e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb); 1548ef2a9a5SYOSHIFUJI Hideaki struct br_ip ip; 1558ef2a9a5SYOSHIFUJI Hideaki 1567f285fa7SHerbert Xu if (br->multicast_disabled) 157eb1d1641SHerbert Xu return NULL; 158eb1d1641SHerbert Xu 1598ef2a9a5SYOSHIFUJI Hideaki if (BR_INPUT_SKB_CB(skb)->igmp) 1608ef2a9a5SYOSHIFUJI Hideaki return NULL; 1618ef2a9a5SYOSHIFUJI Hideaki 1628ef2a9a5SYOSHIFUJI Hideaki ip.proto = skb->protocol; 163fbca58a2SCong Wang ip.vid = vid; 1648ef2a9a5SYOSHIFUJI Hideaki 165eb1d1641SHerbert Xu switch (skb->protocol) { 166eb1d1641SHerbert Xu case htons(ETH_P_IP): 1678ef2a9a5SYOSHIFUJI Hideaki ip.u.ip4 = ip_hdr(skb)->daddr; 168eb1d1641SHerbert Xu break; 169dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 17008b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 1714e3fd7a0SAlexey Dobriyan ip.u.ip6 = ipv6_hdr(skb)->daddr; 17208b202b6SYOSHIFUJI Hideaki break; 17308b202b6SYOSHIFUJI Hideaki #endif 1748ef2a9a5SYOSHIFUJI Hideaki default: 1758ef2a9a5SYOSHIFUJI Hideaki return NULL; 176eb1d1641SHerbert Xu } 177eb1d1641SHerbert Xu 1788ef2a9a5SYOSHIFUJI Hideaki return br_mdb_ip_get(mdb, &ip); 179eb1d1641SHerbert Xu } 180eb1d1641SHerbert Xu 181eb1d1641SHerbert Xu static void br_mdb_free(struct rcu_head *head) 182eb1d1641SHerbert Xu { 183eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb = 184eb1d1641SHerbert Xu container_of(head, struct net_bridge_mdb_htable, rcu); 185eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old = mdb->old; 186eb1d1641SHerbert Xu 187eb1d1641SHerbert Xu mdb->old = NULL; 188eb1d1641SHerbert Xu kfree(old->mhash); 189eb1d1641SHerbert Xu kfree(old); 190eb1d1641SHerbert Xu } 191eb1d1641SHerbert Xu 192eb1d1641SHerbert Xu static int br_mdb_copy(struct net_bridge_mdb_htable *new, 193eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old, 194eb1d1641SHerbert Xu int elasticity) 195eb1d1641SHerbert Xu { 196eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 197eb1d1641SHerbert Xu int maxlen; 198eb1d1641SHerbert Xu int len; 199eb1d1641SHerbert Xu int i; 200eb1d1641SHerbert Xu 201eb1d1641SHerbert Xu for (i = 0; i < old->max; i++) 202b67bfe0dSSasha Levin hlist_for_each_entry(mp, &old->mhash[i], hlist[old->ver]) 203eb1d1641SHerbert Xu hlist_add_head(&mp->hlist[new->ver], 2048ef2a9a5SYOSHIFUJI Hideaki &new->mhash[br_ip_hash(new, &mp->addr)]); 205eb1d1641SHerbert Xu 206eb1d1641SHerbert Xu if (!elasticity) 207eb1d1641SHerbert Xu return 0; 208eb1d1641SHerbert Xu 209eb1d1641SHerbert Xu maxlen = 0; 210eb1d1641SHerbert Xu for (i = 0; i < new->max; i++) { 211eb1d1641SHerbert Xu len = 0; 212b67bfe0dSSasha Levin hlist_for_each_entry(mp, &new->mhash[i], hlist[new->ver]) 213eb1d1641SHerbert Xu len++; 214eb1d1641SHerbert Xu if (len > maxlen) 215eb1d1641SHerbert Xu maxlen = len; 216eb1d1641SHerbert Xu } 217eb1d1641SHerbert Xu 218eb1d1641SHerbert Xu return maxlen > elasticity ? -EINVAL : 0; 219eb1d1641SHerbert Xu } 220eb1d1641SHerbert Xu 221cfd56754SCong Wang void br_multicast_free_pg(struct rcu_head *head) 222eb1d1641SHerbert Xu { 223eb1d1641SHerbert Xu struct net_bridge_port_group *p = 224eb1d1641SHerbert Xu container_of(head, struct net_bridge_port_group, rcu); 225eb1d1641SHerbert Xu 226eb1d1641SHerbert Xu kfree(p); 227eb1d1641SHerbert Xu } 228eb1d1641SHerbert Xu 229eb1d1641SHerbert Xu static void br_multicast_free_group(struct rcu_head *head) 230eb1d1641SHerbert Xu { 231eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = 232eb1d1641SHerbert Xu container_of(head, struct net_bridge_mdb_entry, rcu); 233eb1d1641SHerbert Xu 234eb1d1641SHerbert Xu kfree(mp); 235eb1d1641SHerbert Xu } 236eb1d1641SHerbert Xu 237eb1d1641SHerbert Xu static void br_multicast_group_expired(unsigned long data) 238eb1d1641SHerbert Xu { 239eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = (void *)data; 240eb1d1641SHerbert Xu struct net_bridge *br = mp->br; 241eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 242eb1d1641SHerbert Xu 243eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 244eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&mp->timer)) 245eb1d1641SHerbert Xu goto out; 246eb1d1641SHerbert Xu 2478a870178SHerbert Xu mp->mglist = false; 248eb1d1641SHerbert Xu 249eb1d1641SHerbert Xu if (mp->ports) 250eb1d1641SHerbert Xu goto out; 251eb1d1641SHerbert Xu 252e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 253e8051688SEric Dumazet 254eb1d1641SHerbert Xu hlist_del_rcu(&mp->hlist[mdb->ver]); 255eb1d1641SHerbert Xu mdb->size--; 256eb1d1641SHerbert Xu 257eb1d1641SHerbert Xu call_rcu_bh(&mp->rcu, br_multicast_free_group); 258eb1d1641SHerbert Xu 259eb1d1641SHerbert Xu out: 260eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 261eb1d1641SHerbert Xu } 262eb1d1641SHerbert Xu 263eb1d1641SHerbert Xu static void br_multicast_del_pg(struct net_bridge *br, 264eb1d1641SHerbert Xu struct net_bridge_port_group *pg) 265eb1d1641SHerbert Xu { 266e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 267eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 268eb1d1641SHerbert Xu struct net_bridge_port_group *p; 269e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 270e8051688SEric Dumazet 271e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 272eb1d1641SHerbert Xu 2738ef2a9a5SYOSHIFUJI Hideaki mp = br_mdb_ip_get(mdb, &pg->addr); 274eb1d1641SHerbert Xu if (WARN_ON(!mp)) 275eb1d1641SHerbert Xu return; 276eb1d1641SHerbert Xu 277e8051688SEric Dumazet for (pp = &mp->ports; 278e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 279e8051688SEric Dumazet pp = &p->next) { 280eb1d1641SHerbert Xu if (p != pg) 281eb1d1641SHerbert Xu continue; 282eb1d1641SHerbert Xu 28383f6a740Sstephen hemminger rcu_assign_pointer(*pp, p->next); 284eb1d1641SHerbert Xu hlist_del_init(&p->mglist); 285eb1d1641SHerbert Xu del_timer(&p->timer); 286ef8299deSNikolay Aleksandrov br_mdb_notify(br->dev, p->port, &pg->addr, RTM_DELMDB, 287*9d06b6d8SElad Raz p->flags); 288eb1d1641SHerbert Xu call_rcu_bh(&p->rcu, br_multicast_free_pg); 289eb1d1641SHerbert Xu 290454594f3SLinus Lüssing if (!mp->ports && !mp->mglist && 291eb1d1641SHerbert Xu netif_running(br->dev)) 292eb1d1641SHerbert Xu mod_timer(&mp->timer, jiffies); 293eb1d1641SHerbert Xu 294eb1d1641SHerbert Xu return; 295eb1d1641SHerbert Xu } 296eb1d1641SHerbert Xu 297eb1d1641SHerbert Xu WARN_ON(1); 298eb1d1641SHerbert Xu } 299eb1d1641SHerbert Xu 300eb1d1641SHerbert Xu static void br_multicast_port_group_expired(unsigned long data) 301eb1d1641SHerbert Xu { 302eb1d1641SHerbert Xu struct net_bridge_port_group *pg = (void *)data; 303eb1d1641SHerbert Xu struct net_bridge *br = pg->port->br; 304eb1d1641SHerbert Xu 305eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 306eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&pg->timer) || 307*9d06b6d8SElad Raz hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT) 308eb1d1641SHerbert Xu goto out; 309eb1d1641SHerbert Xu 310eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 311eb1d1641SHerbert Xu 312eb1d1641SHerbert Xu out: 313eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 314eb1d1641SHerbert Xu } 315eb1d1641SHerbert Xu 316e8051688SEric Dumazet static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max, 317eb1d1641SHerbert Xu int elasticity) 318eb1d1641SHerbert Xu { 319e8051688SEric Dumazet struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1); 320eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 321eb1d1641SHerbert Xu int err; 322eb1d1641SHerbert Xu 323eb1d1641SHerbert Xu mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC); 324eb1d1641SHerbert Xu if (!mdb) 325eb1d1641SHerbert Xu return -ENOMEM; 326eb1d1641SHerbert Xu 327eb1d1641SHerbert Xu mdb->max = max; 328eb1d1641SHerbert Xu mdb->old = old; 329eb1d1641SHerbert Xu 330eb1d1641SHerbert Xu mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC); 331eb1d1641SHerbert Xu if (!mdb->mhash) { 332eb1d1641SHerbert Xu kfree(mdb); 333eb1d1641SHerbert Xu return -ENOMEM; 334eb1d1641SHerbert Xu } 335eb1d1641SHerbert Xu 336eb1d1641SHerbert Xu mdb->size = old ? old->size : 0; 337eb1d1641SHerbert Xu mdb->ver = old ? old->ver ^ 1 : 0; 338eb1d1641SHerbert Xu 339eb1d1641SHerbert Xu if (!old || elasticity) 340eb1d1641SHerbert Xu get_random_bytes(&mdb->secret, sizeof(mdb->secret)); 341eb1d1641SHerbert Xu else 342eb1d1641SHerbert Xu mdb->secret = old->secret; 343eb1d1641SHerbert Xu 344eb1d1641SHerbert Xu if (!old) 345eb1d1641SHerbert Xu goto out; 346eb1d1641SHerbert Xu 347eb1d1641SHerbert Xu err = br_mdb_copy(mdb, old, elasticity); 348eb1d1641SHerbert Xu if (err) { 349eb1d1641SHerbert Xu kfree(mdb->mhash); 350eb1d1641SHerbert Xu kfree(mdb); 351eb1d1641SHerbert Xu return err; 352eb1d1641SHerbert Xu } 353eb1d1641SHerbert Xu 3542ce297fcSCong Wang br_mdb_rehash_seq++; 355eb1d1641SHerbert Xu call_rcu_bh(&mdb->rcu, br_mdb_free); 356eb1d1641SHerbert Xu 357eb1d1641SHerbert Xu out: 358eb1d1641SHerbert Xu rcu_assign_pointer(*mdbp, mdb); 359eb1d1641SHerbert Xu 360eb1d1641SHerbert Xu return 0; 361eb1d1641SHerbert Xu } 362eb1d1641SHerbert Xu 3638ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br, 364eb1d1641SHerbert Xu __be32 group) 365eb1d1641SHerbert Xu { 366eb1d1641SHerbert Xu struct sk_buff *skb; 367eb1d1641SHerbert Xu struct igmphdr *ih; 368eb1d1641SHerbert Xu struct ethhdr *eth; 369eb1d1641SHerbert Xu struct iphdr *iph; 370eb1d1641SHerbert Xu 371eb1d1641SHerbert Xu skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) + 372eb1d1641SHerbert Xu sizeof(*ih) + 4); 373eb1d1641SHerbert Xu if (!skb) 374eb1d1641SHerbert Xu goto out; 375eb1d1641SHerbert Xu 376eb1d1641SHerbert Xu skb->protocol = htons(ETH_P_IP); 377eb1d1641SHerbert Xu 378eb1d1641SHerbert Xu skb_reset_mac_header(skb); 379eb1d1641SHerbert Xu eth = eth_hdr(skb); 380eb1d1641SHerbert Xu 381e5a727f6SJoe Perches ether_addr_copy(eth->h_source, br->dev->dev_addr); 382eb1d1641SHerbert Xu eth->h_dest[0] = 1; 383eb1d1641SHerbert Xu eth->h_dest[1] = 0; 384eb1d1641SHerbert Xu eth->h_dest[2] = 0x5e; 385eb1d1641SHerbert Xu eth->h_dest[3] = 0; 386eb1d1641SHerbert Xu eth->h_dest[4] = 0; 387eb1d1641SHerbert Xu eth->h_dest[5] = 1; 388eb1d1641SHerbert Xu eth->h_proto = htons(ETH_P_IP); 389eb1d1641SHerbert Xu skb_put(skb, sizeof(*eth)); 390eb1d1641SHerbert Xu 391eb1d1641SHerbert Xu skb_set_network_header(skb, skb->len); 392eb1d1641SHerbert Xu iph = ip_hdr(skb); 393eb1d1641SHerbert Xu 394eb1d1641SHerbert Xu iph->version = 4; 395eb1d1641SHerbert Xu iph->ihl = 6; 396eb1d1641SHerbert Xu iph->tos = 0xc0; 397eb1d1641SHerbert Xu iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4); 398eb1d1641SHerbert Xu iph->id = 0; 399eb1d1641SHerbert Xu iph->frag_off = htons(IP_DF); 400eb1d1641SHerbert Xu iph->ttl = 1; 401eb1d1641SHerbert Xu iph->protocol = IPPROTO_IGMP; 4021c8ad5bfSCong Wang iph->saddr = br->multicast_query_use_ifaddr ? 4031c8ad5bfSCong Wang inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0; 404eb1d1641SHerbert Xu iph->daddr = htonl(INADDR_ALLHOSTS_GROUP); 405eb1d1641SHerbert Xu ((u8 *)&iph[1])[0] = IPOPT_RA; 406eb1d1641SHerbert Xu ((u8 *)&iph[1])[1] = 4; 407eb1d1641SHerbert Xu ((u8 *)&iph[1])[2] = 0; 408eb1d1641SHerbert Xu ((u8 *)&iph[1])[3] = 0; 409eb1d1641SHerbert Xu ip_send_check(iph); 410eb1d1641SHerbert Xu skb_put(skb, 24); 411eb1d1641SHerbert Xu 412eb1d1641SHerbert Xu skb_set_transport_header(skb, skb->len); 413eb1d1641SHerbert Xu ih = igmp_hdr(skb); 414eb1d1641SHerbert Xu ih->type = IGMP_HOST_MEMBERSHIP_QUERY; 415eb1d1641SHerbert Xu ih->code = (group ? br->multicast_last_member_interval : 416eb1d1641SHerbert Xu br->multicast_query_response_interval) / 417eb1d1641SHerbert Xu (HZ / IGMP_TIMER_SCALE); 418eb1d1641SHerbert Xu ih->group = group; 419eb1d1641SHerbert Xu ih->csum = 0; 420eb1d1641SHerbert Xu ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr)); 421eb1d1641SHerbert Xu skb_put(skb, sizeof(*ih)); 422eb1d1641SHerbert Xu 423eb1d1641SHerbert Xu __skb_pull(skb, sizeof(*eth)); 424eb1d1641SHerbert Xu 425eb1d1641SHerbert Xu out: 426eb1d1641SHerbert Xu return skb; 427eb1d1641SHerbert Xu } 428eb1d1641SHerbert Xu 429dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 43008b202b6SYOSHIFUJI Hideaki static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br, 431b71d1d42SEric Dumazet const struct in6_addr *group) 43208b202b6SYOSHIFUJI Hideaki { 43308b202b6SYOSHIFUJI Hideaki struct sk_buff *skb; 43408b202b6SYOSHIFUJI Hideaki struct ipv6hdr *ip6h; 43508b202b6SYOSHIFUJI Hideaki struct mld_msg *mldq; 43608b202b6SYOSHIFUJI Hideaki struct ethhdr *eth; 43708b202b6SYOSHIFUJI Hideaki u8 *hopopt; 43808b202b6SYOSHIFUJI Hideaki unsigned long interval; 43908b202b6SYOSHIFUJI Hideaki 44008b202b6SYOSHIFUJI Hideaki skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) + 44108b202b6SYOSHIFUJI Hideaki 8 + sizeof(*mldq)); 44208b202b6SYOSHIFUJI Hideaki if (!skb) 44308b202b6SYOSHIFUJI Hideaki goto out; 44408b202b6SYOSHIFUJI Hideaki 44508b202b6SYOSHIFUJI Hideaki skb->protocol = htons(ETH_P_IPV6); 44608b202b6SYOSHIFUJI Hideaki 44708b202b6SYOSHIFUJI Hideaki /* Ethernet header */ 44808b202b6SYOSHIFUJI Hideaki skb_reset_mac_header(skb); 44908b202b6SYOSHIFUJI Hideaki eth = eth_hdr(skb); 45008b202b6SYOSHIFUJI Hideaki 451e5a727f6SJoe Perches ether_addr_copy(eth->h_source, br->dev->dev_addr); 45208b202b6SYOSHIFUJI Hideaki eth->h_proto = htons(ETH_P_IPV6); 45308b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*eth)); 45408b202b6SYOSHIFUJI Hideaki 45508b202b6SYOSHIFUJI Hideaki /* IPv6 header + HbH option */ 45608b202b6SYOSHIFUJI Hideaki skb_set_network_header(skb, skb->len); 45708b202b6SYOSHIFUJI Hideaki ip6h = ipv6_hdr(skb); 45808b202b6SYOSHIFUJI Hideaki 45908b202b6SYOSHIFUJI Hideaki *(__force __be32 *)ip6h = htonl(0x60000000); 46076d66158SDavid Stevens ip6h->payload_len = htons(8 + sizeof(*mldq)); 46108b202b6SYOSHIFUJI Hideaki ip6h->nexthdr = IPPROTO_HOPOPTS; 46208b202b6SYOSHIFUJI Hideaki ip6h->hop_limit = 1; 463a7bff75bSLinus Lüssing ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1)); 464d1d81d4cSUlrich Weber if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0, 465d1d81d4cSUlrich Weber &ip6h->saddr)) { 466d1d81d4cSUlrich Weber kfree_skb(skb); 467d1d81d4cSUlrich Weber return NULL; 468d1d81d4cSUlrich Weber } 46936cff5a1SLinus Lüssing ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest); 47008b202b6SYOSHIFUJI Hideaki 47108b202b6SYOSHIFUJI Hideaki hopopt = (u8 *)(ip6h + 1); 47208b202b6SYOSHIFUJI Hideaki hopopt[0] = IPPROTO_ICMPV6; /* next hdr */ 47308b202b6SYOSHIFUJI Hideaki hopopt[1] = 0; /* length of HbH */ 47408b202b6SYOSHIFUJI Hideaki hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */ 47508b202b6SYOSHIFUJI Hideaki hopopt[3] = 2; /* Length of RA Option */ 47608b202b6SYOSHIFUJI Hideaki hopopt[4] = 0; /* Type = 0x0000 (MLD) */ 47708b202b6SYOSHIFUJI Hideaki hopopt[5] = 0; 4781de5a71cSEldad Zack hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */ 4791de5a71cSEldad Zack hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */ 48008b202b6SYOSHIFUJI Hideaki 48108b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*ip6h) + 8); 48208b202b6SYOSHIFUJI Hideaki 48308b202b6SYOSHIFUJI Hideaki /* ICMPv6 */ 48408b202b6SYOSHIFUJI Hideaki skb_set_transport_header(skb, skb->len); 48508b202b6SYOSHIFUJI Hideaki mldq = (struct mld_msg *) icmp6_hdr(skb); 48608b202b6SYOSHIFUJI Hideaki 48732de868cSLinus Lüssing interval = ipv6_addr_any(group) ? 48832de868cSLinus Lüssing br->multicast_query_response_interval : 48932de868cSLinus Lüssing br->multicast_last_member_interval; 49008b202b6SYOSHIFUJI Hideaki 49108b202b6SYOSHIFUJI Hideaki mldq->mld_type = ICMPV6_MGM_QUERY; 49208b202b6SYOSHIFUJI Hideaki mldq->mld_code = 0; 49308b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = 0; 49408b202b6SYOSHIFUJI Hideaki mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval)); 49508b202b6SYOSHIFUJI Hideaki mldq->mld_reserved = 0; 4964e3fd7a0SAlexey Dobriyan mldq->mld_mca = *group; 49708b202b6SYOSHIFUJI Hideaki 49808b202b6SYOSHIFUJI Hideaki /* checksum */ 49908b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, 50008b202b6SYOSHIFUJI Hideaki sizeof(*mldq), IPPROTO_ICMPV6, 50108b202b6SYOSHIFUJI Hideaki csum_partial(mldq, 50208b202b6SYOSHIFUJI Hideaki sizeof(*mldq), 0)); 50308b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*mldq)); 50408b202b6SYOSHIFUJI Hideaki 50508b202b6SYOSHIFUJI Hideaki __skb_pull(skb, sizeof(*eth)); 50608b202b6SYOSHIFUJI Hideaki 50708b202b6SYOSHIFUJI Hideaki out: 50808b202b6SYOSHIFUJI Hideaki return skb; 50908b202b6SYOSHIFUJI Hideaki } 51008b202b6SYOSHIFUJI Hideaki #endif 51108b202b6SYOSHIFUJI Hideaki 5128ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br, 5138ef2a9a5SYOSHIFUJI Hideaki struct br_ip *addr) 5148ef2a9a5SYOSHIFUJI Hideaki { 5158ef2a9a5SYOSHIFUJI Hideaki switch (addr->proto) { 5168ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 5178ef2a9a5SYOSHIFUJI Hideaki return br_ip4_multicast_alloc_query(br, addr->u.ip4); 518dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 51908b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 52008b202b6SYOSHIFUJI Hideaki return br_ip6_multicast_alloc_query(br, &addr->u.ip6); 52108b202b6SYOSHIFUJI Hideaki #endif 5228ef2a9a5SYOSHIFUJI Hideaki } 5238ef2a9a5SYOSHIFUJI Hideaki return NULL; 5248ef2a9a5SYOSHIFUJI Hideaki } 5258ef2a9a5SYOSHIFUJI Hideaki 526eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *br_multicast_get_group( 5278ef2a9a5SYOSHIFUJI Hideaki struct net_bridge *br, struct net_bridge_port *port, 5288ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group, int hash) 529eb1d1641SHerbert Xu { 530e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 531eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 53295c96174SEric Dumazet unsigned int count = 0; 53395c96174SEric Dumazet unsigned int max; 534eb1d1641SHerbert Xu int elasticity; 535eb1d1641SHerbert Xu int err; 536eb1d1641SHerbert Xu 537e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 538b67bfe0dSSasha Levin hlist_for_each_entry(mp, &mdb->mhash[hash], hlist[mdb->ver]) { 539eb1d1641SHerbert Xu count++; 5408ef2a9a5SYOSHIFUJI Hideaki if (unlikely(br_ip_equal(group, &mp->addr))) 541eb1d1641SHerbert Xu return mp; 542eb1d1641SHerbert Xu } 543eb1d1641SHerbert Xu 544eb1d1641SHerbert Xu elasticity = 0; 545eb1d1641SHerbert Xu max = mdb->max; 546eb1d1641SHerbert Xu 547eb1d1641SHerbert Xu if (unlikely(count > br->hash_elasticity && count)) { 548eb1d1641SHerbert Xu if (net_ratelimit()) 54928a16c97Sstephen hemminger br_info(br, "Multicast hash table " 550eb1d1641SHerbert Xu "chain limit reached: %s\n", 55128a16c97Sstephen hemminger port ? port->dev->name : br->dev->name); 552eb1d1641SHerbert Xu 553eb1d1641SHerbert Xu elasticity = br->hash_elasticity; 554eb1d1641SHerbert Xu } 555eb1d1641SHerbert Xu 556eb1d1641SHerbert Xu if (mdb->size >= max) { 557eb1d1641SHerbert Xu max *= 2; 558036be6dbSThomas Graf if (unlikely(max > br->hash_max)) { 559036be6dbSThomas Graf br_warn(br, "Multicast hash table maximum of %d " 560036be6dbSThomas Graf "reached, disabling snooping: %s\n", 561036be6dbSThomas Graf br->hash_max, 562036be6dbSThomas Graf port ? port->dev->name : br->dev->name); 563eb1d1641SHerbert Xu err = -E2BIG; 564eb1d1641SHerbert Xu disable: 565eb1d1641SHerbert Xu br->multicast_disabled = 1; 566eb1d1641SHerbert Xu goto err; 567eb1d1641SHerbert Xu } 568eb1d1641SHerbert Xu } 569eb1d1641SHerbert Xu 570eb1d1641SHerbert Xu if (max > mdb->max || elasticity) { 571eb1d1641SHerbert Xu if (mdb->old) { 572eb1d1641SHerbert Xu if (net_ratelimit()) 57328a16c97Sstephen hemminger br_info(br, "Multicast hash table " 574eb1d1641SHerbert Xu "on fire: %s\n", 57528a16c97Sstephen hemminger port ? port->dev->name : br->dev->name); 576eb1d1641SHerbert Xu err = -EEXIST; 577eb1d1641SHerbert Xu goto err; 578eb1d1641SHerbert Xu } 579eb1d1641SHerbert Xu 580eb1d1641SHerbert Xu err = br_mdb_rehash(&br->mdb, max, elasticity); 581eb1d1641SHerbert Xu if (err) { 58228a16c97Sstephen hemminger br_warn(br, "Cannot rehash multicast " 58328a16c97Sstephen hemminger "hash table, disabling snooping: %s, %d, %d\n", 58428a16c97Sstephen hemminger port ? port->dev->name : br->dev->name, 585eb1d1641SHerbert Xu mdb->size, err); 586eb1d1641SHerbert Xu goto disable; 587eb1d1641SHerbert Xu } 588eb1d1641SHerbert Xu 589eb1d1641SHerbert Xu err = -EAGAIN; 590eb1d1641SHerbert Xu goto err; 591eb1d1641SHerbert Xu } 592eb1d1641SHerbert Xu 593eb1d1641SHerbert Xu return NULL; 594eb1d1641SHerbert Xu 595eb1d1641SHerbert Xu err: 596eb1d1641SHerbert Xu mp = ERR_PTR(err); 597eb1d1641SHerbert Xu return mp; 598eb1d1641SHerbert Xu } 599eb1d1641SHerbert Xu 600cfd56754SCong Wang struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br, 601cfd56754SCong Wang struct net_bridge_port *port, struct br_ip *group) 602eb1d1641SHerbert Xu { 603e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 604eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 605eb1d1641SHerbert Xu int hash; 6064c0833bcSTobias Klauser int err; 607eb1d1641SHerbert Xu 608e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 609eb1d1641SHerbert Xu if (!mdb) { 6104c0833bcSTobias Klauser err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0); 6114c0833bcSTobias Klauser if (err) 6124c0833bcSTobias Klauser return ERR_PTR(err); 613eb1d1641SHerbert Xu goto rehash; 614eb1d1641SHerbert Xu } 615eb1d1641SHerbert Xu 616eb1d1641SHerbert Xu hash = br_ip_hash(mdb, group); 617eb1d1641SHerbert Xu mp = br_multicast_get_group(br, port, group, hash); 618eb1d1641SHerbert Xu switch (PTR_ERR(mp)) { 619eb1d1641SHerbert Xu case 0: 620eb1d1641SHerbert Xu break; 621eb1d1641SHerbert Xu 622eb1d1641SHerbert Xu case -EAGAIN: 623eb1d1641SHerbert Xu rehash: 624e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 625eb1d1641SHerbert Xu hash = br_ip_hash(mdb, group); 626eb1d1641SHerbert Xu break; 627eb1d1641SHerbert Xu 628eb1d1641SHerbert Xu default: 629eb1d1641SHerbert Xu goto out; 630eb1d1641SHerbert Xu } 631eb1d1641SHerbert Xu 632eb1d1641SHerbert Xu mp = kzalloc(sizeof(*mp), GFP_ATOMIC); 633eb1d1641SHerbert Xu if (unlikely(!mp)) 6344c0833bcSTobias Klauser return ERR_PTR(-ENOMEM); 635eb1d1641SHerbert Xu 636eb1d1641SHerbert Xu mp->br = br; 6378ef2a9a5SYOSHIFUJI Hideaki mp->addr = *group; 6381faabf2aSEric Dumazet setup_timer(&mp->timer, br_multicast_group_expired, 6391faabf2aSEric Dumazet (unsigned long)mp); 6401faabf2aSEric Dumazet 641eb1d1641SHerbert Xu hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]); 642eb1d1641SHerbert Xu mdb->size++; 643eb1d1641SHerbert Xu 644eb1d1641SHerbert Xu out: 645eb1d1641SHerbert Xu return mp; 646eb1d1641SHerbert Xu } 647eb1d1641SHerbert Xu 648cfd56754SCong Wang struct net_bridge_port_group *br_multicast_new_port_group( 649cfd56754SCong Wang struct net_bridge_port *port, 650cfd56754SCong Wang struct br_ip *group, 651ccb1c31aSAmerigo Wang struct net_bridge_port_group __rcu *next, 652*9d06b6d8SElad Raz unsigned char flags) 653cfd56754SCong Wang { 654cfd56754SCong Wang struct net_bridge_port_group *p; 655cfd56754SCong Wang 656cfd56754SCong Wang p = kzalloc(sizeof(*p), GFP_ATOMIC); 657cfd56754SCong Wang if (unlikely(!p)) 658cfd56754SCong Wang return NULL; 659cfd56754SCong Wang 660cfd56754SCong Wang p->addr = *group; 661cfd56754SCong Wang p->port = port; 662*9d06b6d8SElad Raz p->flags = flags; 663eca2a43bSstephen hemminger rcu_assign_pointer(p->next, next); 664cfd56754SCong Wang hlist_add_head(&p->mglist, &port->mglist); 665cfd56754SCong Wang setup_timer(&p->timer, br_multicast_port_group_expired, 666cfd56754SCong Wang (unsigned long)p); 667cfd56754SCong Wang return p; 668cfd56754SCong Wang } 669cfd56754SCong Wang 670eb1d1641SHerbert Xu static int br_multicast_add_group(struct net_bridge *br, 6718ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 6728ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group) 673eb1d1641SHerbert Xu { 674eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 675eb1d1641SHerbert Xu struct net_bridge_port_group *p; 676e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 677454594f3SLinus Lüssing unsigned long now = jiffies; 678eb1d1641SHerbert Xu int err; 679eb1d1641SHerbert Xu 680eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 681eb1d1641SHerbert Xu if (!netif_running(br->dev) || 682eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 683eb1d1641SHerbert Xu goto out; 684eb1d1641SHerbert Xu 685eb1d1641SHerbert Xu mp = br_multicast_new_group(br, port, group); 686eb1d1641SHerbert Xu err = PTR_ERR(mp); 6874c0833bcSTobias Klauser if (IS_ERR(mp)) 688eb1d1641SHerbert Xu goto err; 689eb1d1641SHerbert Xu 690eb1d1641SHerbert Xu if (!port) { 6918a870178SHerbert Xu mp->mglist = true; 692454594f3SLinus Lüssing mod_timer(&mp->timer, now + br->multicast_membership_interval); 693eb1d1641SHerbert Xu goto out; 694eb1d1641SHerbert Xu } 695eb1d1641SHerbert Xu 696e8051688SEric Dumazet for (pp = &mp->ports; 697e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 698e8051688SEric Dumazet pp = &p->next) { 699eb1d1641SHerbert Xu if (p->port == port) 700454594f3SLinus Lüssing goto found; 701eb1d1641SHerbert Xu if ((unsigned long)p->port < (unsigned long)port) 702eb1d1641SHerbert Xu break; 703eb1d1641SHerbert Xu } 704eb1d1641SHerbert Xu 705*9d06b6d8SElad Raz p = br_multicast_new_port_group(port, group, *pp, 0); 706eb1d1641SHerbert Xu if (unlikely(!p)) 707eb1d1641SHerbert Xu goto err; 708eb1d1641SHerbert Xu rcu_assign_pointer(*pp, p); 709*9d06b6d8SElad Raz br_mdb_notify(br->dev, port, group, RTM_NEWMDB, 0); 710eb1d1641SHerbert Xu 711454594f3SLinus Lüssing found: 712454594f3SLinus Lüssing mod_timer(&p->timer, now + br->multicast_membership_interval); 713eb1d1641SHerbert Xu out: 714eb1d1641SHerbert Xu err = 0; 715eb1d1641SHerbert Xu 716eb1d1641SHerbert Xu err: 717eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 718eb1d1641SHerbert Xu return err; 719eb1d1641SHerbert Xu } 720eb1d1641SHerbert Xu 7218ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_add_group(struct net_bridge *br, 7228ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 723b0e9a30dSVlad Yasevich __be32 group, 724b0e9a30dSVlad Yasevich __u16 vid) 7258ef2a9a5SYOSHIFUJI Hideaki { 7268ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 7278ef2a9a5SYOSHIFUJI Hideaki 7288ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 7298ef2a9a5SYOSHIFUJI Hideaki return 0; 7308ef2a9a5SYOSHIFUJI Hideaki 7318ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 7328ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 733b0e9a30dSVlad Yasevich br_group.vid = vid; 7348ef2a9a5SYOSHIFUJI Hideaki 7358ef2a9a5SYOSHIFUJI Hideaki return br_multicast_add_group(br, port, &br_group); 7368ef2a9a5SYOSHIFUJI Hideaki } 7378ef2a9a5SYOSHIFUJI Hideaki 738dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 73908b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_add_group(struct net_bridge *br, 74008b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 741b0e9a30dSVlad Yasevich const struct in6_addr *group, 742b0e9a30dSVlad Yasevich __u16 vid) 74308b202b6SYOSHIFUJI Hideaki { 74408b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 74508b202b6SYOSHIFUJI Hideaki 7463c3769e6SLinus Lüssing if (ipv6_addr_is_ll_all_nodes(group)) 74708b202b6SYOSHIFUJI Hideaki return 0; 74808b202b6SYOSHIFUJI Hideaki 7494e3fd7a0SAlexey Dobriyan br_group.u.ip6 = *group; 7509cc6e0c4SLinus Lüssing br_group.proto = htons(ETH_P_IPV6); 751b0e9a30dSVlad Yasevich br_group.vid = vid; 75208b202b6SYOSHIFUJI Hideaki 75308b202b6SYOSHIFUJI Hideaki return br_multicast_add_group(br, port, &br_group); 75408b202b6SYOSHIFUJI Hideaki } 75508b202b6SYOSHIFUJI Hideaki #endif 75608b202b6SYOSHIFUJI Hideaki 757eb1d1641SHerbert Xu static void br_multicast_router_expired(unsigned long data) 758eb1d1641SHerbert Xu { 759eb1d1641SHerbert Xu struct net_bridge_port *port = (void *)data; 760eb1d1641SHerbert Xu struct net_bridge *br = port->br; 761eb1d1641SHerbert Xu 762eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 763eb1d1641SHerbert Xu if (port->multicast_router != 1 || 764eb1d1641SHerbert Xu timer_pending(&port->multicast_router_timer) || 765eb1d1641SHerbert Xu hlist_unhashed(&port->rlist)) 766eb1d1641SHerbert Xu goto out; 767eb1d1641SHerbert Xu 768eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 769949f1e39SSatish Ashok br_rtr_notify(br->dev, port, RTM_DELMDB); 770eb1d1641SHerbert Xu 771eb1d1641SHerbert Xu out: 772eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 773eb1d1641SHerbert Xu } 774eb1d1641SHerbert Xu 775eb1d1641SHerbert Xu static void br_multicast_local_router_expired(unsigned long data) 776eb1d1641SHerbert Xu { 777eb1d1641SHerbert Xu } 778eb1d1641SHerbert Xu 779cc0fdd80SLinus Lüssing static void br_multicast_querier_expired(struct net_bridge *br, 78090010b36SLinus Lüssing struct bridge_mcast_own_query *query) 781c83b8fabSHerbert Xu { 782c83b8fabSHerbert Xu spin_lock(&br->multicast_lock); 783c83b8fabSHerbert Xu if (!netif_running(br->dev) || br->multicast_disabled) 784c83b8fabSHerbert Xu goto out; 785c83b8fabSHerbert Xu 786cc0fdd80SLinus Lüssing br_multicast_start_querier(br, query); 787c83b8fabSHerbert Xu 788c83b8fabSHerbert Xu out: 789c83b8fabSHerbert Xu spin_unlock(&br->multicast_lock); 790c83b8fabSHerbert Xu } 791c83b8fabSHerbert Xu 792cc0fdd80SLinus Lüssing static void br_ip4_multicast_querier_expired(unsigned long data) 793cc0fdd80SLinus Lüssing { 794cc0fdd80SLinus Lüssing struct net_bridge *br = (void *)data; 795cc0fdd80SLinus Lüssing 79690010b36SLinus Lüssing br_multicast_querier_expired(br, &br->ip4_own_query); 797cc0fdd80SLinus Lüssing } 798cc0fdd80SLinus Lüssing 799cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 800cc0fdd80SLinus Lüssing static void br_ip6_multicast_querier_expired(unsigned long data) 801cc0fdd80SLinus Lüssing { 802cc0fdd80SLinus Lüssing struct net_bridge *br = (void *)data; 803cc0fdd80SLinus Lüssing 80490010b36SLinus Lüssing br_multicast_querier_expired(br, &br->ip6_own_query); 805cc0fdd80SLinus Lüssing } 806cc0fdd80SLinus Lüssing #endif 807cc0fdd80SLinus Lüssing 808dc4eb53aSLinus Lüssing static void br_multicast_select_own_querier(struct net_bridge *br, 809dc4eb53aSLinus Lüssing struct br_ip *ip, 810dc4eb53aSLinus Lüssing struct sk_buff *skb) 811dc4eb53aSLinus Lüssing { 812dc4eb53aSLinus Lüssing if (ip->proto == htons(ETH_P_IP)) 813dc4eb53aSLinus Lüssing br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr; 814dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 815dc4eb53aSLinus Lüssing else 816dc4eb53aSLinus Lüssing br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr; 817dc4eb53aSLinus Lüssing #endif 818dc4eb53aSLinus Lüssing } 819dc4eb53aSLinus Lüssing 8208ef2a9a5SYOSHIFUJI Hideaki static void __br_multicast_send_query(struct net_bridge *br, 8218ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 8228ef2a9a5SYOSHIFUJI Hideaki struct br_ip *ip) 823eb1d1641SHerbert Xu { 824eb1d1641SHerbert Xu struct sk_buff *skb; 825eb1d1641SHerbert Xu 8268ef2a9a5SYOSHIFUJI Hideaki skb = br_multicast_alloc_query(br, ip); 827eb1d1641SHerbert Xu if (!skb) 8288ef2a9a5SYOSHIFUJI Hideaki return; 829eb1d1641SHerbert Xu 830eb1d1641SHerbert Xu if (port) { 831eb1d1641SHerbert Xu skb->dev = port->dev; 83229a26a56SEric W. Biederman NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, 83329a26a56SEric W. Biederman dev_net(port->dev), NULL, skb, NULL, skb->dev, 834f0b4eeceSLinus Lüssing br_dev_queue_push_xmit); 835dc4eb53aSLinus Lüssing } else { 836dc4eb53aSLinus Lüssing br_multicast_select_own_querier(br, ip, skb); 837eb1d1641SHerbert Xu netif_rx(skb); 8388ef2a9a5SYOSHIFUJI Hideaki } 839dc4eb53aSLinus Lüssing } 840eb1d1641SHerbert Xu 8418ef2a9a5SYOSHIFUJI Hideaki static void br_multicast_send_query(struct net_bridge *br, 842cc0fdd80SLinus Lüssing struct net_bridge_port *port, 84390010b36SLinus Lüssing struct bridge_mcast_own_query *own_query) 8448ef2a9a5SYOSHIFUJI Hideaki { 8458ef2a9a5SYOSHIFUJI Hideaki unsigned long time; 8468ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 84790010b36SLinus Lüssing struct bridge_mcast_other_query *other_query = NULL; 8488ef2a9a5SYOSHIFUJI Hideaki 8498ef2a9a5SYOSHIFUJI Hideaki if (!netif_running(br->dev) || br->multicast_disabled || 850cc0fdd80SLinus Lüssing !br->multicast_querier) 8518ef2a9a5SYOSHIFUJI Hideaki return; 8528ef2a9a5SYOSHIFUJI Hideaki 85308b202b6SYOSHIFUJI Hideaki memset(&br_group.u, 0, sizeof(br_group.u)); 8548ef2a9a5SYOSHIFUJI Hideaki 85590010b36SLinus Lüssing if (port ? (own_query == &port->ip4_own_query) : 85690010b36SLinus Lüssing (own_query == &br->ip4_own_query)) { 85790010b36SLinus Lüssing other_query = &br->ip4_other_query; 85808b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 859dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 860cc0fdd80SLinus Lüssing } else { 86190010b36SLinus Lüssing other_query = &br->ip6_other_query; 86208b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 86308b202b6SYOSHIFUJI Hideaki #endif 864eb1d1641SHerbert Xu } 865eb1d1641SHerbert Xu 86690010b36SLinus Lüssing if (!other_query || timer_pending(&other_query->timer)) 867cc0fdd80SLinus Lüssing return; 868cc0fdd80SLinus Lüssing 869cc0fdd80SLinus Lüssing __br_multicast_send_query(br, port, &br_group); 870cc0fdd80SLinus Lüssing 871cc0fdd80SLinus Lüssing time = jiffies; 87290010b36SLinus Lüssing time += own_query->startup_sent < br->multicast_startup_query_count ? 873cc0fdd80SLinus Lüssing br->multicast_startup_query_interval : 874cc0fdd80SLinus Lüssing br->multicast_query_interval; 87590010b36SLinus Lüssing mod_timer(&own_query->timer, time); 876cc0fdd80SLinus Lüssing } 877cc0fdd80SLinus Lüssing 87890010b36SLinus Lüssing static void 87990010b36SLinus Lüssing br_multicast_port_query_expired(struct net_bridge_port *port, 88090010b36SLinus Lüssing struct bridge_mcast_own_query *query) 881eb1d1641SHerbert Xu { 882eb1d1641SHerbert Xu struct net_bridge *br = port->br; 883eb1d1641SHerbert Xu 884eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 88502a780c0SDan Carpenter if (port->state == BR_STATE_DISABLED || 88602a780c0SDan Carpenter port->state == BR_STATE_BLOCKING) 887eb1d1641SHerbert Xu goto out; 888eb1d1641SHerbert Xu 889cc0fdd80SLinus Lüssing if (query->startup_sent < br->multicast_startup_query_count) 890cc0fdd80SLinus Lüssing query->startup_sent++; 891eb1d1641SHerbert Xu 892cc0fdd80SLinus Lüssing br_multicast_send_query(port->br, port, query); 893eb1d1641SHerbert Xu 894eb1d1641SHerbert Xu out: 895eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 896eb1d1641SHerbert Xu } 897eb1d1641SHerbert Xu 898cc0fdd80SLinus Lüssing static void br_ip4_multicast_port_query_expired(unsigned long data) 899cc0fdd80SLinus Lüssing { 900cc0fdd80SLinus Lüssing struct net_bridge_port *port = (void *)data; 901cc0fdd80SLinus Lüssing 90290010b36SLinus Lüssing br_multicast_port_query_expired(port, &port->ip4_own_query); 903cc0fdd80SLinus Lüssing } 904cc0fdd80SLinus Lüssing 905cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 906cc0fdd80SLinus Lüssing static void br_ip6_multicast_port_query_expired(unsigned long data) 907cc0fdd80SLinus Lüssing { 908cc0fdd80SLinus Lüssing struct net_bridge_port *port = (void *)data; 909cc0fdd80SLinus Lüssing 91090010b36SLinus Lüssing br_multicast_port_query_expired(port, &port->ip6_own_query); 911cc0fdd80SLinus Lüssing } 912cc0fdd80SLinus Lüssing #endif 913cc0fdd80SLinus Lüssing 914eb1d1641SHerbert Xu void br_multicast_add_port(struct net_bridge_port *port) 915eb1d1641SHerbert Xu { 916eb1d1641SHerbert Xu port->multicast_router = 1; 917eb1d1641SHerbert Xu 918eb1d1641SHerbert Xu setup_timer(&port->multicast_router_timer, br_multicast_router_expired, 919eb1d1641SHerbert Xu (unsigned long)port); 92090010b36SLinus Lüssing setup_timer(&port->ip4_own_query.timer, 92190010b36SLinus Lüssing br_ip4_multicast_port_query_expired, (unsigned long)port); 922cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 92390010b36SLinus Lüssing setup_timer(&port->ip6_own_query.timer, 92490010b36SLinus Lüssing br_ip6_multicast_port_query_expired, (unsigned long)port); 925cc0fdd80SLinus Lüssing #endif 926eb1d1641SHerbert Xu } 927eb1d1641SHerbert Xu 928eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port) 929eb1d1641SHerbert Xu { 930e10177abSSatish Ashok struct net_bridge *br = port->br; 931e10177abSSatish Ashok struct net_bridge_port_group *pg; 932e10177abSSatish Ashok struct hlist_node *n; 933e10177abSSatish Ashok 934e10177abSSatish Ashok /* Take care of the remaining groups, only perm ones should be left */ 935e10177abSSatish Ashok spin_lock_bh(&br->multicast_lock); 936e10177abSSatish Ashok hlist_for_each_entry_safe(pg, n, &port->mglist, mglist) 937e10177abSSatish Ashok br_multicast_del_pg(br, pg); 938e10177abSSatish Ashok spin_unlock_bh(&br->multicast_lock); 939eb1d1641SHerbert Xu del_timer_sync(&port->multicast_router_timer); 940eb1d1641SHerbert Xu } 941eb1d1641SHerbert Xu 94290010b36SLinus Lüssing static void br_multicast_enable(struct bridge_mcast_own_query *query) 943561f1103SHerbert Xu { 944cc0fdd80SLinus Lüssing query->startup_sent = 0; 945561f1103SHerbert Xu 946cc0fdd80SLinus Lüssing if (try_to_del_timer_sync(&query->timer) >= 0 || 947cc0fdd80SLinus Lüssing del_timer(&query->timer)) 948cc0fdd80SLinus Lüssing mod_timer(&query->timer, jiffies); 949561f1103SHerbert Xu } 950561f1103SHerbert Xu 951eb1d1641SHerbert Xu void br_multicast_enable_port(struct net_bridge_port *port) 952eb1d1641SHerbert Xu { 953eb1d1641SHerbert Xu struct net_bridge *br = port->br; 954eb1d1641SHerbert Xu 955eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 956eb1d1641SHerbert Xu if (br->multicast_disabled || !netif_running(br->dev)) 957eb1d1641SHerbert Xu goto out; 958eb1d1641SHerbert Xu 95990010b36SLinus Lüssing br_multicast_enable(&port->ip4_own_query); 960cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 96190010b36SLinus Lüssing br_multicast_enable(&port->ip6_own_query); 962cc0fdd80SLinus Lüssing #endif 963754bc547SSatish Ashok if (port->multicast_router == 2 && hlist_unhashed(&port->rlist)) 964754bc547SSatish Ashok br_multicast_add_router(br, port); 965eb1d1641SHerbert Xu 966eb1d1641SHerbert Xu out: 967eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 968eb1d1641SHerbert Xu } 969eb1d1641SHerbert Xu 970eb1d1641SHerbert Xu void br_multicast_disable_port(struct net_bridge_port *port) 971eb1d1641SHerbert Xu { 972eb1d1641SHerbert Xu struct net_bridge *br = port->br; 973eb1d1641SHerbert Xu struct net_bridge_port_group *pg; 974b67bfe0dSSasha Levin struct hlist_node *n; 975eb1d1641SHerbert Xu 976eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 977b67bfe0dSSasha Levin hlist_for_each_entry_safe(pg, n, &port->mglist, mglist) 978*9d06b6d8SElad Raz if (!(pg->flags & MDB_PG_FLAGS_PERMANENT)) 979eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 980eb1d1641SHerbert Xu 981949f1e39SSatish Ashok if (!hlist_unhashed(&port->rlist)) { 982eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 983949f1e39SSatish Ashok br_rtr_notify(br->dev, port, RTM_DELMDB); 984949f1e39SSatish Ashok } 985eb1d1641SHerbert Xu del_timer(&port->multicast_router_timer); 98690010b36SLinus Lüssing del_timer(&port->ip4_own_query.timer); 987cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 98890010b36SLinus Lüssing del_timer(&port->ip6_own_query.timer); 989cc0fdd80SLinus Lüssing #endif 990eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 991eb1d1641SHerbert Xu } 992eb1d1641SHerbert Xu 9938ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_igmp3_report(struct net_bridge *br, 994eb1d1641SHerbert Xu struct net_bridge_port *port, 99506499098SVlad Yasevich struct sk_buff *skb, 99606499098SVlad Yasevich u16 vid) 997eb1d1641SHerbert Xu { 998eb1d1641SHerbert Xu struct igmpv3_report *ih; 999eb1d1641SHerbert Xu struct igmpv3_grec *grec; 1000eb1d1641SHerbert Xu int i; 1001eb1d1641SHerbert Xu int len; 1002eb1d1641SHerbert Xu int num; 1003eb1d1641SHerbert Xu int type; 1004eb1d1641SHerbert Xu int err = 0; 1005eb1d1641SHerbert Xu __be32 group; 1006eb1d1641SHerbert Xu 1007eb1d1641SHerbert Xu ih = igmpv3_report_hdr(skb); 1008eb1d1641SHerbert Xu num = ntohs(ih->ngrec); 1009c2d4fbd2SLinus Lüssing len = skb_transport_offset(skb) + sizeof(*ih); 1010eb1d1641SHerbert Xu 1011eb1d1641SHerbert Xu for (i = 0; i < num; i++) { 1012eb1d1641SHerbert Xu len += sizeof(*grec); 1013eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 1014eb1d1641SHerbert Xu return -EINVAL; 1015eb1d1641SHerbert Xu 1016fd218cf9SHerbert Xu grec = (void *)(skb->data + len - sizeof(*grec)); 1017eb1d1641SHerbert Xu group = grec->grec_mca; 1018eb1d1641SHerbert Xu type = grec->grec_type; 1019eb1d1641SHerbert Xu 10208eabf95cSEric Dumazet len += ntohs(grec->grec_nsrcs) * 4; 1021eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 1022eb1d1641SHerbert Xu return -EINVAL; 1023eb1d1641SHerbert Xu 1024eb1d1641SHerbert Xu /* We treat this as an IGMPv2 report for now. */ 1025eb1d1641SHerbert Xu switch (type) { 1026eb1d1641SHerbert Xu case IGMPV3_MODE_IS_INCLUDE: 1027eb1d1641SHerbert Xu case IGMPV3_MODE_IS_EXCLUDE: 1028eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_INCLUDE: 1029eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_EXCLUDE: 1030eb1d1641SHerbert Xu case IGMPV3_ALLOW_NEW_SOURCES: 1031eb1d1641SHerbert Xu case IGMPV3_BLOCK_OLD_SOURCES: 1032eb1d1641SHerbert Xu break; 1033eb1d1641SHerbert Xu 1034eb1d1641SHerbert Xu default: 1035eb1d1641SHerbert Xu continue; 1036eb1d1641SHerbert Xu } 1037eb1d1641SHerbert Xu 1038bc8c20acSSatish Ashok if ((type == IGMPV3_CHANGE_TO_INCLUDE || 1039bc8c20acSSatish Ashok type == IGMPV3_MODE_IS_INCLUDE) && 1040bc8c20acSSatish Ashok ntohs(grec->grec_nsrcs) == 0) { 1041bc8c20acSSatish Ashok br_ip4_multicast_leave_group(br, port, group, vid); 1042bc8c20acSSatish Ashok } else { 1043b0e9a30dSVlad Yasevich err = br_ip4_multicast_add_group(br, port, group, vid); 1044eb1d1641SHerbert Xu if (err) 1045eb1d1641SHerbert Xu break; 1046eb1d1641SHerbert Xu } 1047bc8c20acSSatish Ashok } 1048eb1d1641SHerbert Xu 1049eb1d1641SHerbert Xu return err; 1050eb1d1641SHerbert Xu } 1051eb1d1641SHerbert Xu 1052dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 105308b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_mld2_report(struct net_bridge *br, 105408b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 105506499098SVlad Yasevich struct sk_buff *skb, 105606499098SVlad Yasevich u16 vid) 105708b202b6SYOSHIFUJI Hideaki { 105808b202b6SYOSHIFUJI Hideaki struct icmp6hdr *icmp6h; 105908b202b6SYOSHIFUJI Hideaki struct mld2_grec *grec; 106008b202b6SYOSHIFUJI Hideaki int i; 106108b202b6SYOSHIFUJI Hideaki int len; 106208b202b6SYOSHIFUJI Hideaki int num; 106308b202b6SYOSHIFUJI Hideaki int err = 0; 106408b202b6SYOSHIFUJI Hideaki 106508b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*icmp6h))) 106608b202b6SYOSHIFUJI Hideaki return -EINVAL; 106708b202b6SYOSHIFUJI Hideaki 106808b202b6SYOSHIFUJI Hideaki icmp6h = icmp6_hdr(skb); 106908b202b6SYOSHIFUJI Hideaki num = ntohs(icmp6h->icmp6_dataun.un_data16[1]); 1070c2d4fbd2SLinus Lüssing len = skb_transport_offset(skb) + sizeof(*icmp6h); 107108b202b6SYOSHIFUJI Hideaki 107208b202b6SYOSHIFUJI Hideaki for (i = 0; i < num; i++) { 107308b202b6SYOSHIFUJI Hideaki __be16 *nsrcs, _nsrcs; 107408b202b6SYOSHIFUJI Hideaki 107508b202b6SYOSHIFUJI Hideaki nsrcs = skb_header_pointer(skb, 107608b202b6SYOSHIFUJI Hideaki len + offsetof(struct mld2_grec, 1077649e984dSLinus Lüssing grec_nsrcs), 107808b202b6SYOSHIFUJI Hideaki sizeof(_nsrcs), &_nsrcs); 107908b202b6SYOSHIFUJI Hideaki if (!nsrcs) 108008b202b6SYOSHIFUJI Hideaki return -EINVAL; 108108b202b6SYOSHIFUJI Hideaki 108208b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, 108308b202b6SYOSHIFUJI Hideaki len + sizeof(*grec) + 1084d41db9f3SLinus Lüssing sizeof(struct in6_addr) * ntohs(*nsrcs))) 108508b202b6SYOSHIFUJI Hideaki return -EINVAL; 108608b202b6SYOSHIFUJI Hideaki 108708b202b6SYOSHIFUJI Hideaki grec = (struct mld2_grec *)(skb->data + len); 1088d41db9f3SLinus Lüssing len += sizeof(*grec) + 1089d41db9f3SLinus Lüssing sizeof(struct in6_addr) * ntohs(*nsrcs); 109008b202b6SYOSHIFUJI Hideaki 109108b202b6SYOSHIFUJI Hideaki /* We treat these as MLDv1 reports for now. */ 109208b202b6SYOSHIFUJI Hideaki switch (grec->grec_type) { 109308b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_INCLUDE: 109408b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_EXCLUDE: 109508b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_INCLUDE: 109608b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_EXCLUDE: 109708b202b6SYOSHIFUJI Hideaki case MLD2_ALLOW_NEW_SOURCES: 109808b202b6SYOSHIFUJI Hideaki case MLD2_BLOCK_OLD_SOURCES: 109908b202b6SYOSHIFUJI Hideaki break; 110008b202b6SYOSHIFUJI Hideaki 110108b202b6SYOSHIFUJI Hideaki default: 110208b202b6SYOSHIFUJI Hideaki continue; 110308b202b6SYOSHIFUJI Hideaki } 110408b202b6SYOSHIFUJI Hideaki 1105bc8c20acSSatish Ashok if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE || 1106bc8c20acSSatish Ashok grec->grec_type == MLD2_MODE_IS_INCLUDE) && 1107bc8c20acSSatish Ashok ntohs(*nsrcs) == 0) { 1108bc8c20acSSatish Ashok br_ip6_multicast_leave_group(br, port, &grec->grec_mca, 1109b0e9a30dSVlad Yasevich vid); 1110bc8c20acSSatish Ashok } else { 1111bc8c20acSSatish Ashok err = br_ip6_multicast_add_group(br, port, 1112bc8c20acSSatish Ashok &grec->grec_mca, vid); 1113bc8c20acSSatish Ashok if (!err) 111408b202b6SYOSHIFUJI Hideaki break; 111508b202b6SYOSHIFUJI Hideaki } 1116bc8c20acSSatish Ashok } 111708b202b6SYOSHIFUJI Hideaki 111808b202b6SYOSHIFUJI Hideaki return err; 111908b202b6SYOSHIFUJI Hideaki } 112008b202b6SYOSHIFUJI Hideaki #endif 112108b202b6SYOSHIFUJI Hideaki 1122dc4eb53aSLinus Lüssing static bool br_ip4_multicast_select_querier(struct net_bridge *br, 11232cd41431SLinus Lüssing struct net_bridge_port *port, 1124dc4eb53aSLinus Lüssing __be32 saddr) 1125dc4eb53aSLinus Lüssing { 1126dc4eb53aSLinus Lüssing if (!timer_pending(&br->ip4_own_query.timer) && 1127dc4eb53aSLinus Lüssing !timer_pending(&br->ip4_other_query.timer)) 1128dc4eb53aSLinus Lüssing goto update; 1129dc4eb53aSLinus Lüssing 1130dc4eb53aSLinus Lüssing if (!br->ip4_querier.addr.u.ip4) 1131dc4eb53aSLinus Lüssing goto update; 1132dc4eb53aSLinus Lüssing 1133dc4eb53aSLinus Lüssing if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4)) 1134dc4eb53aSLinus Lüssing goto update; 1135dc4eb53aSLinus Lüssing 1136dc4eb53aSLinus Lüssing return false; 1137dc4eb53aSLinus Lüssing 1138dc4eb53aSLinus Lüssing update: 1139dc4eb53aSLinus Lüssing br->ip4_querier.addr.u.ip4 = saddr; 1140dc4eb53aSLinus Lüssing 11412cd41431SLinus Lüssing /* update protected by general multicast_lock by caller */ 11422cd41431SLinus Lüssing rcu_assign_pointer(br->ip4_querier.port, port); 11432cd41431SLinus Lüssing 1144dc4eb53aSLinus Lüssing return true; 1145dc4eb53aSLinus Lüssing } 1146dc4eb53aSLinus Lüssing 1147dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1148dc4eb53aSLinus Lüssing static bool br_ip6_multicast_select_querier(struct net_bridge *br, 11492cd41431SLinus Lüssing struct net_bridge_port *port, 1150dc4eb53aSLinus Lüssing struct in6_addr *saddr) 1151dc4eb53aSLinus Lüssing { 1152dc4eb53aSLinus Lüssing if (!timer_pending(&br->ip6_own_query.timer) && 1153dc4eb53aSLinus Lüssing !timer_pending(&br->ip6_other_query.timer)) 1154dc4eb53aSLinus Lüssing goto update; 1155dc4eb53aSLinus Lüssing 1156dc4eb53aSLinus Lüssing if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0) 1157dc4eb53aSLinus Lüssing goto update; 1158dc4eb53aSLinus Lüssing 1159dc4eb53aSLinus Lüssing return false; 1160dc4eb53aSLinus Lüssing 1161dc4eb53aSLinus Lüssing update: 1162dc4eb53aSLinus Lüssing br->ip6_querier.addr.u.ip6 = *saddr; 1163dc4eb53aSLinus Lüssing 11642cd41431SLinus Lüssing /* update protected by general multicast_lock by caller */ 11652cd41431SLinus Lüssing rcu_assign_pointer(br->ip6_querier.port, port); 11662cd41431SLinus Lüssing 1167dc4eb53aSLinus Lüssing return true; 1168dc4eb53aSLinus Lüssing } 1169dc4eb53aSLinus Lüssing #endif 1170dc4eb53aSLinus Lüssing 1171dc4eb53aSLinus Lüssing static bool br_multicast_select_querier(struct net_bridge *br, 11722cd41431SLinus Lüssing struct net_bridge_port *port, 1173dc4eb53aSLinus Lüssing struct br_ip *saddr) 1174dc4eb53aSLinus Lüssing { 1175dc4eb53aSLinus Lüssing switch (saddr->proto) { 1176dc4eb53aSLinus Lüssing case htons(ETH_P_IP): 11772cd41431SLinus Lüssing return br_ip4_multicast_select_querier(br, port, saddr->u.ip4); 1178dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1179dc4eb53aSLinus Lüssing case htons(ETH_P_IPV6): 11802cd41431SLinus Lüssing return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6); 1181dc4eb53aSLinus Lüssing #endif 1182dc4eb53aSLinus Lüssing } 1183dc4eb53aSLinus Lüssing 1184dc4eb53aSLinus Lüssing return false; 1185dc4eb53aSLinus Lüssing } 1186dc4eb53aSLinus Lüssing 1187cc0fdd80SLinus Lüssing static void 118890010b36SLinus Lüssing br_multicast_update_query_timer(struct net_bridge *br, 118990010b36SLinus Lüssing struct bridge_mcast_other_query *query, 1190b00589afSLinus Lüssing unsigned long max_delay) 1191b00589afSLinus Lüssing { 119290010b36SLinus Lüssing if (!timer_pending(&query->timer)) 119390010b36SLinus Lüssing query->delay_time = jiffies + max_delay; 1194b00589afSLinus Lüssing 119590010b36SLinus Lüssing mod_timer(&query->timer, jiffies + br->multicast_querier_interval); 1196b00589afSLinus Lüssing } 1197b00589afSLinus Lüssing 11987e80c124Sstephen hemminger /* 11997c77602fSCong Wang * Add port to router_list 12007e80c124Sstephen hemminger * list is maintained ordered by pointer value 12017e80c124Sstephen hemminger * and locked by br->multicast_lock and RCU 12027e80c124Sstephen hemminger */ 12030909e117SHerbert Xu static void br_multicast_add_router(struct net_bridge *br, 12040909e117SHerbert Xu struct net_bridge_port *port) 12050909e117SHerbert Xu { 1206dcdca2c4Sstephen hemminger struct net_bridge_port *p; 1207b67bfe0dSSasha Levin struct hlist_node *slot = NULL; 12080909e117SHerbert Xu 12091a040eacSNikolay Aleksandrov if (!hlist_unhashed(&port->rlist)) 12101a040eacSNikolay Aleksandrov return; 12111a040eacSNikolay Aleksandrov 1212b67bfe0dSSasha Levin hlist_for_each_entry(p, &br->router_list, rlist) { 12137e80c124Sstephen hemminger if ((unsigned long) port >= (unsigned long) p) 12147e80c124Sstephen hemminger break; 1215b67bfe0dSSasha Levin slot = &p->rlist; 1216dcdca2c4Sstephen hemminger } 12170909e117SHerbert Xu 12187e80c124Sstephen hemminger if (slot) 12191d023284SKen Helias hlist_add_behind_rcu(&port->rlist, slot); 1220dcdca2c4Sstephen hemminger else 1221dcdca2c4Sstephen hemminger hlist_add_head_rcu(&port->rlist, &br->router_list); 1222949f1e39SSatish Ashok br_rtr_notify(br->dev, port, RTM_NEWMDB); 12230909e117SHerbert Xu } 12240909e117SHerbert Xu 1225eb1d1641SHerbert Xu static void br_multicast_mark_router(struct net_bridge *br, 1226eb1d1641SHerbert Xu struct net_bridge_port *port) 1227eb1d1641SHerbert Xu { 1228eb1d1641SHerbert Xu unsigned long now = jiffies; 1229eb1d1641SHerbert Xu 1230eb1d1641SHerbert Xu if (!port) { 1231eb1d1641SHerbert Xu if (br->multicast_router == 1) 1232eb1d1641SHerbert Xu mod_timer(&br->multicast_router_timer, 1233eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1234eb1d1641SHerbert Xu return; 1235eb1d1641SHerbert Xu } 1236eb1d1641SHerbert Xu 1237eb1d1641SHerbert Xu if (port->multicast_router != 1) 1238eb1d1641SHerbert Xu return; 1239eb1d1641SHerbert Xu 12400909e117SHerbert Xu br_multicast_add_router(br, port); 1241eb1d1641SHerbert Xu 1242eb1d1641SHerbert Xu mod_timer(&port->multicast_router_timer, 1243eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1244eb1d1641SHerbert Xu } 1245eb1d1641SHerbert Xu 1246eb1d1641SHerbert Xu static void br_multicast_query_received(struct net_bridge *br, 1247eb1d1641SHerbert Xu struct net_bridge_port *port, 124890010b36SLinus Lüssing struct bridge_mcast_other_query *query, 1249dc4eb53aSLinus Lüssing struct br_ip *saddr, 1250b00589afSLinus Lüssing unsigned long max_delay) 1251eb1d1641SHerbert Xu { 12522cd41431SLinus Lüssing if (!br_multicast_select_querier(br, port, saddr)) 1253eb1d1641SHerbert Xu return; 1254eb1d1641SHerbert Xu 1255dc4eb53aSLinus Lüssing br_multicast_update_query_timer(br, query, max_delay); 1256eb1d1641SHerbert Xu br_multicast_mark_router(br, port); 1257eb1d1641SHerbert Xu } 1258eb1d1641SHerbert Xu 12598ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_query(struct net_bridge *br, 1260eb1d1641SHerbert Xu struct net_bridge_port *port, 126106499098SVlad Yasevich struct sk_buff *skb, 126206499098SVlad Yasevich u16 vid) 1263eb1d1641SHerbert Xu { 1264b71d1d42SEric Dumazet const struct iphdr *iph = ip_hdr(skb); 1265eb1d1641SHerbert Xu struct igmphdr *ih = igmp_hdr(skb); 1266eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1267eb1d1641SHerbert Xu struct igmpv3_query *ih3; 1268eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1269e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 1270dc4eb53aSLinus Lüssing struct br_ip saddr; 1271eb1d1641SHerbert Xu unsigned long max_delay; 1272eb1d1641SHerbert Xu unsigned long now = jiffies; 1273eb1d1641SHerbert Xu __be32 group; 1274bec68ff1SYOSHIFUJI Hideaki int err = 0; 1275eb1d1641SHerbert Xu 1276eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1277eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1278eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 1279eb1d1641SHerbert Xu goto out; 1280eb1d1641SHerbert Xu 1281eb1d1641SHerbert Xu group = ih->group; 1282eb1d1641SHerbert Xu 1283eb1d1641SHerbert Xu if (skb->len == sizeof(*ih)) { 1284eb1d1641SHerbert Xu max_delay = ih->code * (HZ / IGMP_TIMER_SCALE); 1285eb1d1641SHerbert Xu 1286eb1d1641SHerbert Xu if (!max_delay) { 1287eb1d1641SHerbert Xu max_delay = 10 * HZ; 1288eb1d1641SHerbert Xu group = 0; 1289eb1d1641SHerbert Xu } 12909afd85c9SLinus Lüssing } else if (skb->len >= sizeof(*ih3)) { 1291eb1d1641SHerbert Xu ih3 = igmpv3_query_hdr(skb); 1292eb1d1641SHerbert Xu if (ih3->nsrcs) 1293bec68ff1SYOSHIFUJI Hideaki goto out; 1294eb1d1641SHerbert Xu 12950ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 max_delay = ih3->code ? 12960ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1; 12979afd85c9SLinus Lüssing } else { 12989ed973ccSLinus Lüssing goto out; 12999ed973ccSLinus Lüssing } 13009ed973ccSLinus Lüssing 1301dc4eb53aSLinus Lüssing if (!group) { 1302dc4eb53aSLinus Lüssing saddr.proto = htons(ETH_P_IP); 1303dc4eb53aSLinus Lüssing saddr.u.ip4 = iph->saddr; 1304b00589afSLinus Lüssing 1305dc4eb53aSLinus Lüssing br_multicast_query_received(br, port, &br->ip4_other_query, 1306dc4eb53aSLinus Lüssing &saddr, max_delay); 1307eb1d1641SHerbert Xu goto out; 1308dc4eb53aSLinus Lüssing } 1309eb1d1641SHerbert Xu 1310b0e9a30dSVlad Yasevich mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid); 1311eb1d1641SHerbert Xu if (!mp) 1312eb1d1641SHerbert Xu goto out; 1313eb1d1641SHerbert Xu 1314eb1d1641SHerbert Xu max_delay *= br->multicast_last_member_count; 1315eb1d1641SHerbert Xu 13168a870178SHerbert Xu if (mp->mglist && 1317eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1318eb1d1641SHerbert Xu time_after(mp->timer.expires, now + max_delay) : 1319eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) 1320eb1d1641SHerbert Xu mod_timer(&mp->timer, now + max_delay); 1321eb1d1641SHerbert Xu 1322e8051688SEric Dumazet for (pp = &mp->ports; 1323e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 1324e8051688SEric Dumazet pp = &p->next) { 1325eb1d1641SHerbert Xu if (timer_pending(&p->timer) ? 1326eb1d1641SHerbert Xu time_after(p->timer.expires, now + max_delay) : 1327eb1d1641SHerbert Xu try_to_del_timer_sync(&p->timer) >= 0) 132824f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 1329eb1d1641SHerbert Xu } 1330eb1d1641SHerbert Xu 1331eb1d1641SHerbert Xu out: 1332eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1333bec68ff1SYOSHIFUJI Hideaki return err; 1334eb1d1641SHerbert Xu } 1335eb1d1641SHerbert Xu 1336dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 133708b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_query(struct net_bridge *br, 133808b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 133906499098SVlad Yasevich struct sk_buff *skb, 134006499098SVlad Yasevich u16 vid) 134108b202b6SYOSHIFUJI Hideaki { 1342b71d1d42SEric Dumazet const struct ipv6hdr *ip6h = ipv6_hdr(skb); 1343eca2a43bSstephen hemminger struct mld_msg *mld; 134408b202b6SYOSHIFUJI Hideaki struct net_bridge_mdb_entry *mp; 134508b202b6SYOSHIFUJI Hideaki struct mld2_query *mld2q; 1346e8051688SEric Dumazet struct net_bridge_port_group *p; 1347e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 1348dc4eb53aSLinus Lüssing struct br_ip saddr; 134908b202b6SYOSHIFUJI Hideaki unsigned long max_delay; 135008b202b6SYOSHIFUJI Hideaki unsigned long now = jiffies; 1351b71d1d42SEric Dumazet const struct in6_addr *group = NULL; 13529ed973ccSLinus Lüssing bool is_general_query; 135308b202b6SYOSHIFUJI Hideaki int err = 0; 135408b202b6SYOSHIFUJI Hideaki 135508b202b6SYOSHIFUJI Hideaki spin_lock(&br->multicast_lock); 135608b202b6SYOSHIFUJI Hideaki if (!netif_running(br->dev) || 135708b202b6SYOSHIFUJI Hideaki (port && port->state == BR_STATE_DISABLED)) 135808b202b6SYOSHIFUJI Hideaki goto out; 135908b202b6SYOSHIFUJI Hideaki 136008b202b6SYOSHIFUJI Hideaki if (skb->len == sizeof(*mld)) { 136108b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*mld))) { 136208b202b6SYOSHIFUJI Hideaki err = -EINVAL; 136308b202b6SYOSHIFUJI Hideaki goto out; 136408b202b6SYOSHIFUJI Hideaki } 136508b202b6SYOSHIFUJI Hideaki mld = (struct mld_msg *) icmp6_hdr(skb); 13664715213dSLi RongQing max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay)); 136708b202b6SYOSHIFUJI Hideaki if (max_delay) 136808b202b6SYOSHIFUJI Hideaki group = &mld->mld_mca; 1369248ba8ecSLinus Lüssing } else { 137008b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*mld2q))) { 137108b202b6SYOSHIFUJI Hideaki err = -EINVAL; 137208b202b6SYOSHIFUJI Hideaki goto out; 137308b202b6SYOSHIFUJI Hideaki } 137408b202b6SYOSHIFUJI Hideaki mld2q = (struct mld2_query *)icmp6_hdr(skb); 137508b202b6SYOSHIFUJI Hideaki if (!mld2q->mld2q_nsrcs) 137608b202b6SYOSHIFUJI Hideaki group = &mld2q->mld2q_mca; 1377e3f5b170SDaniel Borkmann 1378e3f5b170SDaniel Borkmann max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL); 137908b202b6SYOSHIFUJI Hideaki } 138008b202b6SYOSHIFUJI Hideaki 13819ed973ccSLinus Lüssing is_general_query = group && ipv6_addr_any(group); 13829ed973ccSLinus Lüssing 1383dc4eb53aSLinus Lüssing if (is_general_query) { 1384dc4eb53aSLinus Lüssing saddr.proto = htons(ETH_P_IPV6); 1385dc4eb53aSLinus Lüssing saddr.u.ip6 = ip6h->saddr; 1386b00589afSLinus Lüssing 1387dc4eb53aSLinus Lüssing br_multicast_query_received(br, port, &br->ip6_other_query, 1388dc4eb53aSLinus Lüssing &saddr, max_delay); 138908b202b6SYOSHIFUJI Hideaki goto out; 13906c03ee8bSLinus Lüssing } else if (!group) { 13916c03ee8bSLinus Lüssing goto out; 1392dc4eb53aSLinus Lüssing } 139308b202b6SYOSHIFUJI Hideaki 1394b0e9a30dSVlad Yasevich mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid); 139508b202b6SYOSHIFUJI Hideaki if (!mp) 139608b202b6SYOSHIFUJI Hideaki goto out; 139708b202b6SYOSHIFUJI Hideaki 139808b202b6SYOSHIFUJI Hideaki max_delay *= br->multicast_last_member_count; 13998a870178SHerbert Xu if (mp->mglist && 140008b202b6SYOSHIFUJI Hideaki (timer_pending(&mp->timer) ? 140108b202b6SYOSHIFUJI Hideaki time_after(mp->timer.expires, now + max_delay) : 140208b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&mp->timer) >= 0)) 140308b202b6SYOSHIFUJI Hideaki mod_timer(&mp->timer, now + max_delay); 140408b202b6SYOSHIFUJI Hideaki 1405e8051688SEric Dumazet for (pp = &mp->ports; 1406e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 1407e8051688SEric Dumazet pp = &p->next) { 140808b202b6SYOSHIFUJI Hideaki if (timer_pending(&p->timer) ? 140908b202b6SYOSHIFUJI Hideaki time_after(p->timer.expires, now + max_delay) : 141008b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&p->timer) >= 0) 141124f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 141208b202b6SYOSHIFUJI Hideaki } 141308b202b6SYOSHIFUJI Hideaki 141408b202b6SYOSHIFUJI Hideaki out: 141508b202b6SYOSHIFUJI Hideaki spin_unlock(&br->multicast_lock); 141608b202b6SYOSHIFUJI Hideaki return err; 141708b202b6SYOSHIFUJI Hideaki } 141808b202b6SYOSHIFUJI Hideaki #endif 141908b202b6SYOSHIFUJI Hideaki 142090010b36SLinus Lüssing static void 142190010b36SLinus Lüssing br_multicast_leave_group(struct net_bridge *br, 1422eb1d1641SHerbert Xu struct net_bridge_port *port, 1423cc0fdd80SLinus Lüssing struct br_ip *group, 142490010b36SLinus Lüssing struct bridge_mcast_other_query *other_query, 142590010b36SLinus Lüssing struct bridge_mcast_own_query *own_query) 1426eb1d1641SHerbert Xu { 1427eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 1428eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1429eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1430eb1d1641SHerbert Xu unsigned long now; 1431eb1d1641SHerbert Xu unsigned long time; 1432eb1d1641SHerbert Xu 1433eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1434eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1435544586f7SSatish Ashok (port && port->state == BR_STATE_DISABLED)) 1436eb1d1641SHerbert Xu goto out; 1437eb1d1641SHerbert Xu 1438e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1439eb1d1641SHerbert Xu mp = br_mdb_ip_get(mdb, group); 1440eb1d1641SHerbert Xu if (!mp) 1441eb1d1641SHerbert Xu goto out; 1442eb1d1641SHerbert Xu 1443544586f7SSatish Ashok if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) { 1444544586f7SSatish Ashok struct net_bridge_port_group __rcu **pp; 1445544586f7SSatish Ashok 1446544586f7SSatish Ashok for (pp = &mp->ports; 1447544586f7SSatish Ashok (p = mlock_dereference(*pp, br)) != NULL; 1448544586f7SSatish Ashok pp = &p->next) { 1449544586f7SSatish Ashok if (p->port != port) 1450544586f7SSatish Ashok continue; 1451544586f7SSatish Ashok 1452544586f7SSatish Ashok rcu_assign_pointer(*pp, p->next); 1453544586f7SSatish Ashok hlist_del_init(&p->mglist); 1454544586f7SSatish Ashok del_timer(&p->timer); 1455544586f7SSatish Ashok call_rcu_bh(&p->rcu, br_multicast_free_pg); 14565510b3c2SDavid S. Miller br_mdb_notify(br->dev, port, group, RTM_DELMDB, 1457*9d06b6d8SElad Raz p->flags); 1458544586f7SSatish Ashok 1459544586f7SSatish Ashok if (!mp->ports && !mp->mglist && 1460544586f7SSatish Ashok netif_running(br->dev)) 1461544586f7SSatish Ashok mod_timer(&mp->timer, jiffies); 1462544586f7SSatish Ashok } 1463544586f7SSatish Ashok goto out; 1464544586f7SSatish Ashok } 1465544586f7SSatish Ashok 1466544586f7SSatish Ashok if (timer_pending(&other_query->timer)) 1467544586f7SSatish Ashok goto out; 1468544586f7SSatish Ashok 1469cc0fdd80SLinus Lüssing if (br->multicast_querier) { 14706b7df111SCong Wang __br_multicast_send_query(br, port, &mp->addr); 14716b7df111SCong Wang 14726b7df111SCong Wang time = jiffies + br->multicast_last_member_count * 14736b7df111SCong Wang br->multicast_last_member_interval; 1474cc0fdd80SLinus Lüssing 147590010b36SLinus Lüssing mod_timer(&own_query->timer, time); 14766b7df111SCong Wang 14776b7df111SCong Wang for (p = mlock_dereference(mp->ports, br); 14786b7df111SCong Wang p != NULL; 14796b7df111SCong Wang p = mlock_dereference(p->next, br)) { 14806b7df111SCong Wang if (p->port != port) 14816b7df111SCong Wang continue; 14826b7df111SCong Wang 14836b7df111SCong Wang if (!hlist_unhashed(&p->mglist) && 14846b7df111SCong Wang (timer_pending(&p->timer) ? 14856b7df111SCong Wang time_after(p->timer.expires, time) : 14866b7df111SCong Wang try_to_del_timer_sync(&p->timer) >= 0)) { 14876b7df111SCong Wang mod_timer(&p->timer, time); 14886b7df111SCong Wang } 14896b7df111SCong Wang 14906b7df111SCong Wang break; 14916b7df111SCong Wang } 14926b7df111SCong Wang } 14936b7df111SCong Wang 1494eb1d1641SHerbert Xu now = jiffies; 1495eb1d1641SHerbert Xu time = now + br->multicast_last_member_count * 1496eb1d1641SHerbert Xu br->multicast_last_member_interval; 1497eb1d1641SHerbert Xu 1498eb1d1641SHerbert Xu if (!port) { 1499454594f3SLinus Lüssing if (mp->mglist && 1500eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1501eb1d1641SHerbert Xu time_after(mp->timer.expires, time) : 1502eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) { 1503eb1d1641SHerbert Xu mod_timer(&mp->timer, time); 1504eb1d1641SHerbert Xu } 1505454594f3SLinus Lüssing 1506454594f3SLinus Lüssing goto out; 1507454594f3SLinus Lüssing } 1508454594f3SLinus Lüssing 1509454594f3SLinus Lüssing for (p = mlock_dereference(mp->ports, br); 1510454594f3SLinus Lüssing p != NULL; 1511454594f3SLinus Lüssing p = mlock_dereference(p->next, br)) { 1512454594f3SLinus Lüssing if (p->port != port) 1513454594f3SLinus Lüssing continue; 1514454594f3SLinus Lüssing 1515454594f3SLinus Lüssing if (!hlist_unhashed(&p->mglist) && 1516454594f3SLinus Lüssing (timer_pending(&p->timer) ? 1517454594f3SLinus Lüssing time_after(p->timer.expires, time) : 1518454594f3SLinus Lüssing try_to_del_timer_sync(&p->timer) >= 0)) { 1519454594f3SLinus Lüssing mod_timer(&p->timer, time); 1520454594f3SLinus Lüssing } 1521454594f3SLinus Lüssing 1522454594f3SLinus Lüssing break; 1523eb1d1641SHerbert Xu } 1524eb1d1641SHerbert Xu out: 1525eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1526eb1d1641SHerbert Xu } 1527eb1d1641SHerbert Xu 15288ef2a9a5SYOSHIFUJI Hideaki static void br_ip4_multicast_leave_group(struct net_bridge *br, 15298ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 1530b0e9a30dSVlad Yasevich __be32 group, 1531b0e9a30dSVlad Yasevich __u16 vid) 15328ef2a9a5SYOSHIFUJI Hideaki { 15338ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 153490010b36SLinus Lüssing struct bridge_mcast_own_query *own_query; 15358ef2a9a5SYOSHIFUJI Hideaki 15368ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 15378ef2a9a5SYOSHIFUJI Hideaki return; 15388ef2a9a5SYOSHIFUJI Hideaki 153990010b36SLinus Lüssing own_query = port ? &port->ip4_own_query : &br->ip4_own_query; 154090010b36SLinus Lüssing 15418ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 15428ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 1543b0e9a30dSVlad Yasevich br_group.vid = vid; 15448ef2a9a5SYOSHIFUJI Hideaki 154590010b36SLinus Lüssing br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query, 154690010b36SLinus Lüssing own_query); 15478ef2a9a5SYOSHIFUJI Hideaki } 15488ef2a9a5SYOSHIFUJI Hideaki 1549dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 155008b202b6SYOSHIFUJI Hideaki static void br_ip6_multicast_leave_group(struct net_bridge *br, 155108b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 1552b0e9a30dSVlad Yasevich const struct in6_addr *group, 1553b0e9a30dSVlad Yasevich __u16 vid) 155408b202b6SYOSHIFUJI Hideaki { 155508b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 155690010b36SLinus Lüssing struct bridge_mcast_own_query *own_query; 155708b202b6SYOSHIFUJI Hideaki 15583c3769e6SLinus Lüssing if (ipv6_addr_is_ll_all_nodes(group)) 155908b202b6SYOSHIFUJI Hideaki return; 156008b202b6SYOSHIFUJI Hideaki 156190010b36SLinus Lüssing own_query = port ? &port->ip6_own_query : &br->ip6_own_query; 156290010b36SLinus Lüssing 15634e3fd7a0SAlexey Dobriyan br_group.u.ip6 = *group; 156408b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 1565b0e9a30dSVlad Yasevich br_group.vid = vid; 156608b202b6SYOSHIFUJI Hideaki 156790010b36SLinus Lüssing br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query, 156890010b36SLinus Lüssing own_query); 156908b202b6SYOSHIFUJI Hideaki } 157008b202b6SYOSHIFUJI Hideaki #endif 15718ef2a9a5SYOSHIFUJI Hideaki 1572eb1d1641SHerbert Xu static int br_multicast_ipv4_rcv(struct net_bridge *br, 1573eb1d1641SHerbert Xu struct net_bridge_port *port, 157406499098SVlad Yasevich struct sk_buff *skb, 157506499098SVlad Yasevich u16 vid) 1576eb1d1641SHerbert Xu { 15779afd85c9SLinus Lüssing struct sk_buff *skb_trimmed = NULL; 1578eb1d1641SHerbert Xu struct igmphdr *ih; 1579eb1d1641SHerbert Xu int err; 1580eb1d1641SHerbert Xu 15819afd85c9SLinus Lüssing err = ip_mc_check_igmp(skb, &skb_trimmed); 1582eb1d1641SHerbert Xu 15839afd85c9SLinus Lüssing if (err == -ENOMSG) { 15849afd85c9SLinus Lüssing if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) 1585bd4265feSHerbert Xu BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 1586eb1d1641SHerbert Xu return 0; 15879afd85c9SLinus Lüssing } else if (err < 0) { 15889afd85c9SLinus Lüssing return err; 1589bd4265feSHerbert Xu } 1590eb1d1641SHerbert Xu 1591eb1d1641SHerbert Xu BR_INPUT_SKB_CB(skb)->igmp = 1; 15929afd85c9SLinus Lüssing ih = igmp_hdr(skb); 1593eb1d1641SHerbert Xu 1594eb1d1641SHerbert Xu switch (ih->type) { 1595eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_REPORT: 1596eb1d1641SHerbert Xu case IGMPV2_HOST_MEMBERSHIP_REPORT: 159762b2bcb4SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 1598b0e9a30dSVlad Yasevich err = br_ip4_multicast_add_group(br, port, ih->group, vid); 1599eb1d1641SHerbert Xu break; 1600eb1d1641SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 16019afd85c9SLinus Lüssing err = br_ip4_multicast_igmp3_report(br, port, skb_trimmed, vid); 1602eb1d1641SHerbert Xu break; 1603eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_QUERY: 16049afd85c9SLinus Lüssing err = br_ip4_multicast_query(br, port, skb_trimmed, vid); 1605eb1d1641SHerbert Xu break; 1606eb1d1641SHerbert Xu case IGMP_HOST_LEAVE_MESSAGE: 1607b0e9a30dSVlad Yasevich br_ip4_multicast_leave_group(br, port, ih->group, vid); 1608eb1d1641SHerbert Xu break; 1609eb1d1641SHerbert Xu } 1610eb1d1641SHerbert Xu 1611a516993fSLinus Lüssing if (skb_trimmed && skb_trimmed != skb) 16129afd85c9SLinus Lüssing kfree_skb(skb_trimmed); 16139afd85c9SLinus Lüssing 1614eb1d1641SHerbert Xu return err; 1615eb1d1641SHerbert Xu } 1616eb1d1641SHerbert Xu 1617dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 161808b202b6SYOSHIFUJI Hideaki static int br_multicast_ipv6_rcv(struct net_bridge *br, 161908b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 162006499098SVlad Yasevich struct sk_buff *skb, 162106499098SVlad Yasevich u16 vid) 162208b202b6SYOSHIFUJI Hideaki { 16239afd85c9SLinus Lüssing struct sk_buff *skb_trimmed = NULL; 16249afd85c9SLinus Lüssing struct mld_msg *mld; 162508b202b6SYOSHIFUJI Hideaki int err; 162608b202b6SYOSHIFUJI Hideaki 16279afd85c9SLinus Lüssing err = ipv6_mc_check_mld(skb, &skb_trimmed); 162808b202b6SYOSHIFUJI Hideaki 16299afd85c9SLinus Lüssing if (err == -ENOMSG) { 16309afd85c9SLinus Lüssing if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr)) 16318fad9c39SLinus Lüssing BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 163208b202b6SYOSHIFUJI Hideaki return 0; 16339afd85c9SLinus Lüssing } else if (err < 0) { 16349afd85c9SLinus Lüssing return err; 163508b202b6SYOSHIFUJI Hideaki } 163608b202b6SYOSHIFUJI Hideaki 163708b202b6SYOSHIFUJI Hideaki BR_INPUT_SKB_CB(skb)->igmp = 1; 16389afd85c9SLinus Lüssing mld = (struct mld_msg *)skb_transport_header(skb); 163908b202b6SYOSHIFUJI Hideaki 16409afd85c9SLinus Lüssing switch (mld->mld_type) { 164108b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 1642fc2af6c7SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 1643b0e9a30dSVlad Yasevich err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid); 164408b202b6SYOSHIFUJI Hideaki break; 164508b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 16469afd85c9SLinus Lüssing err = br_ip6_multicast_mld2_report(br, port, skb_trimmed, vid); 164708b202b6SYOSHIFUJI Hideaki break; 164808b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 16499afd85c9SLinus Lüssing err = br_ip6_multicast_query(br, port, skb_trimmed, vid); 165008b202b6SYOSHIFUJI Hideaki break; 165108b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 1652b0e9a30dSVlad Yasevich br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid); 16539afd85c9SLinus Lüssing break; 165408b202b6SYOSHIFUJI Hideaki } 165508b202b6SYOSHIFUJI Hideaki 1656a516993fSLinus Lüssing if (skb_trimmed && skb_trimmed != skb) 16579afd85c9SLinus Lüssing kfree_skb(skb_trimmed); 16589afd85c9SLinus Lüssing 165908b202b6SYOSHIFUJI Hideaki return err; 166008b202b6SYOSHIFUJI Hideaki } 166108b202b6SYOSHIFUJI Hideaki #endif 166208b202b6SYOSHIFUJI Hideaki 1663eb1d1641SHerbert Xu int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port, 166406499098SVlad Yasevich struct sk_buff *skb, u16 vid) 1665eb1d1641SHerbert Xu { 16661fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->igmp = 0; 16671fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->mrouters_only = 0; 16681fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 1669eb1d1641SHerbert Xu if (br->multicast_disabled) 1670eb1d1641SHerbert Xu return 0; 1671eb1d1641SHerbert Xu 1672eb1d1641SHerbert Xu switch (skb->protocol) { 1673eb1d1641SHerbert Xu case htons(ETH_P_IP): 167406499098SVlad Yasevich return br_multicast_ipv4_rcv(br, port, skb, vid); 1675dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 167608b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 167706499098SVlad Yasevich return br_multicast_ipv6_rcv(br, port, skb, vid); 167808b202b6SYOSHIFUJI Hideaki #endif 1679eb1d1641SHerbert Xu } 1680eb1d1641SHerbert Xu 1681eb1d1641SHerbert Xu return 0; 1682eb1d1641SHerbert Xu } 1683eb1d1641SHerbert Xu 1684cc0fdd80SLinus Lüssing static void br_multicast_query_expired(struct net_bridge *br, 16852cd41431SLinus Lüssing struct bridge_mcast_own_query *query, 16862cd41431SLinus Lüssing struct bridge_mcast_querier *querier) 1687cc0fdd80SLinus Lüssing { 1688cc0fdd80SLinus Lüssing spin_lock(&br->multicast_lock); 1689cc0fdd80SLinus Lüssing if (query->startup_sent < br->multicast_startup_query_count) 1690cc0fdd80SLinus Lüssing query->startup_sent++; 1691cc0fdd80SLinus Lüssing 169271d9f614SEric Dumazet RCU_INIT_POINTER(querier->port, NULL); 1693cc0fdd80SLinus Lüssing br_multicast_send_query(br, NULL, query); 1694cc0fdd80SLinus Lüssing spin_unlock(&br->multicast_lock); 1695cc0fdd80SLinus Lüssing } 1696cc0fdd80SLinus Lüssing 1697cc0fdd80SLinus Lüssing static void br_ip4_multicast_query_expired(unsigned long data) 1698eb1d1641SHerbert Xu { 1699eb1d1641SHerbert Xu struct net_bridge *br = (void *)data; 1700eb1d1641SHerbert Xu 17012cd41431SLinus Lüssing br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier); 1702eb1d1641SHerbert Xu } 1703eb1d1641SHerbert Xu 1704cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1705cc0fdd80SLinus Lüssing static void br_ip6_multicast_query_expired(unsigned long data) 1706cc0fdd80SLinus Lüssing { 1707cc0fdd80SLinus Lüssing struct net_bridge *br = (void *)data; 1708cc0fdd80SLinus Lüssing 17092cd41431SLinus Lüssing br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier); 1710cc0fdd80SLinus Lüssing } 1711cc0fdd80SLinus Lüssing #endif 1712cc0fdd80SLinus Lüssing 1713eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br) 1714eb1d1641SHerbert Xu { 1715eb1d1641SHerbert Xu br->hash_elasticity = 4; 1716eb1d1641SHerbert Xu br->hash_max = 512; 1717eb1d1641SHerbert Xu 1718eb1d1641SHerbert Xu br->multicast_router = 1; 1719c5c23260SHerbert Xu br->multicast_querier = 0; 17201c8ad5bfSCong Wang br->multicast_query_use_ifaddr = 0; 1721eb1d1641SHerbert Xu br->multicast_last_member_count = 2; 1722eb1d1641SHerbert Xu br->multicast_startup_query_count = 2; 1723eb1d1641SHerbert Xu 1724eb1d1641SHerbert Xu br->multicast_last_member_interval = HZ; 1725eb1d1641SHerbert Xu br->multicast_query_response_interval = 10 * HZ; 1726eb1d1641SHerbert Xu br->multicast_startup_query_interval = 125 * HZ / 4; 1727eb1d1641SHerbert Xu br->multicast_query_interval = 125 * HZ; 1728eb1d1641SHerbert Xu br->multicast_querier_interval = 255 * HZ; 1729eb1d1641SHerbert Xu br->multicast_membership_interval = 260 * HZ; 1730eb1d1641SHerbert Xu 173190010b36SLinus Lüssing br->ip4_other_query.delay_time = 0; 17322cd41431SLinus Lüssing br->ip4_querier.port = NULL; 1733cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 173490010b36SLinus Lüssing br->ip6_other_query.delay_time = 0; 17352cd41431SLinus Lüssing br->ip6_querier.port = NULL; 1736cc0fdd80SLinus Lüssing #endif 1737b00589afSLinus Lüssing 1738eb1d1641SHerbert Xu spin_lock_init(&br->multicast_lock); 1739eb1d1641SHerbert Xu setup_timer(&br->multicast_router_timer, 1740eb1d1641SHerbert Xu br_multicast_local_router_expired, 0); 174190010b36SLinus Lüssing setup_timer(&br->ip4_other_query.timer, 174290010b36SLinus Lüssing br_ip4_multicast_querier_expired, (unsigned long)br); 174390010b36SLinus Lüssing setup_timer(&br->ip4_own_query.timer, br_ip4_multicast_query_expired, 1744cc0fdd80SLinus Lüssing (unsigned long)br); 1745cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 174690010b36SLinus Lüssing setup_timer(&br->ip6_other_query.timer, 174790010b36SLinus Lüssing br_ip6_multicast_querier_expired, (unsigned long)br); 174890010b36SLinus Lüssing setup_timer(&br->ip6_own_query.timer, br_ip6_multicast_query_expired, 1749cc0fdd80SLinus Lüssing (unsigned long)br); 1750cc0fdd80SLinus Lüssing #endif 1751eb1d1641SHerbert Xu } 1752eb1d1641SHerbert Xu 1753cc0fdd80SLinus Lüssing static void __br_multicast_open(struct net_bridge *br, 175490010b36SLinus Lüssing struct bridge_mcast_own_query *query) 1755eb1d1641SHerbert Xu { 1756cc0fdd80SLinus Lüssing query->startup_sent = 0; 1757eb1d1641SHerbert Xu 1758eb1d1641SHerbert Xu if (br->multicast_disabled) 1759eb1d1641SHerbert Xu return; 1760eb1d1641SHerbert Xu 1761cc0fdd80SLinus Lüssing mod_timer(&query->timer, jiffies); 1762cc0fdd80SLinus Lüssing } 1763cc0fdd80SLinus Lüssing 1764cc0fdd80SLinus Lüssing void br_multicast_open(struct net_bridge *br) 1765cc0fdd80SLinus Lüssing { 176690010b36SLinus Lüssing __br_multicast_open(br, &br->ip4_own_query); 1767cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 176890010b36SLinus Lüssing __br_multicast_open(br, &br->ip6_own_query); 1769cc0fdd80SLinus Lüssing #endif 1770eb1d1641SHerbert Xu } 1771eb1d1641SHerbert Xu 1772eb1d1641SHerbert Xu void br_multicast_stop(struct net_bridge *br) 1773eb1d1641SHerbert Xu { 1774eb1d1641SHerbert Xu del_timer_sync(&br->multicast_router_timer); 177590010b36SLinus Lüssing del_timer_sync(&br->ip4_other_query.timer); 177690010b36SLinus Lüssing del_timer_sync(&br->ip4_own_query.timer); 1777cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 177890010b36SLinus Lüssing del_timer_sync(&br->ip6_other_query.timer); 177990010b36SLinus Lüssing del_timer_sync(&br->ip6_own_query.timer); 1780cc0fdd80SLinus Lüssing #endif 1781e10177abSSatish Ashok } 1782e10177abSSatish Ashok 1783e10177abSSatish Ashok void br_multicast_dev_del(struct net_bridge *br) 1784e10177abSSatish Ashok { 1785e10177abSSatish Ashok struct net_bridge_mdb_htable *mdb; 1786e10177abSSatish Ashok struct net_bridge_mdb_entry *mp; 1787e10177abSSatish Ashok struct hlist_node *n; 1788e10177abSSatish Ashok u32 ver; 1789e10177abSSatish Ashok int i; 1790eb1d1641SHerbert Xu 1791eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1792e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1793eb1d1641SHerbert Xu if (!mdb) 1794eb1d1641SHerbert Xu goto out; 1795eb1d1641SHerbert Xu 1796eb1d1641SHerbert Xu br->mdb = NULL; 1797eb1d1641SHerbert Xu 1798eb1d1641SHerbert Xu ver = mdb->ver; 1799eb1d1641SHerbert Xu for (i = 0; i < mdb->max; i++) { 1800b67bfe0dSSasha Levin hlist_for_each_entry_safe(mp, n, &mdb->mhash[i], 1801eb1d1641SHerbert Xu hlist[ver]) { 1802eb1d1641SHerbert Xu del_timer(&mp->timer); 1803eb1d1641SHerbert Xu call_rcu_bh(&mp->rcu, br_multicast_free_group); 1804eb1d1641SHerbert Xu } 1805eb1d1641SHerbert Xu } 1806eb1d1641SHerbert Xu 1807eb1d1641SHerbert Xu if (mdb->old) { 1808eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 180910cc2b50SHerbert Xu rcu_barrier_bh(); 1810eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1811eb1d1641SHerbert Xu WARN_ON(mdb->old); 1812eb1d1641SHerbert Xu } 1813eb1d1641SHerbert Xu 1814eb1d1641SHerbert Xu mdb->old = mdb; 1815eb1d1641SHerbert Xu call_rcu_bh(&mdb->rcu, br_mdb_free); 1816eb1d1641SHerbert Xu 1817eb1d1641SHerbert Xu out: 1818eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 1819eb1d1641SHerbert Xu } 18200909e117SHerbert Xu 18210909e117SHerbert Xu int br_multicast_set_router(struct net_bridge *br, unsigned long val) 18220909e117SHerbert Xu { 18236ae4ae8eSLinus Lüssing int err = -EINVAL; 18240909e117SHerbert Xu 18250909e117SHerbert Xu spin_lock_bh(&br->multicast_lock); 18260909e117SHerbert Xu 18270909e117SHerbert Xu switch (val) { 18280909e117SHerbert Xu case 0: 18290909e117SHerbert Xu case 2: 18300909e117SHerbert Xu del_timer(&br->multicast_router_timer); 18310909e117SHerbert Xu /* fall through */ 18320909e117SHerbert Xu case 1: 18330909e117SHerbert Xu br->multicast_router = val; 18340909e117SHerbert Xu err = 0; 18350909e117SHerbert Xu break; 18360909e117SHerbert Xu } 18370909e117SHerbert Xu 18380909e117SHerbert Xu spin_unlock_bh(&br->multicast_lock); 18390909e117SHerbert Xu 18400909e117SHerbert Xu return err; 18410909e117SHerbert Xu } 18420909e117SHerbert Xu 18430909e117SHerbert Xu int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val) 18440909e117SHerbert Xu { 18450909e117SHerbert Xu struct net_bridge *br = p->br; 18466ae4ae8eSLinus Lüssing int err = -EINVAL; 18470909e117SHerbert Xu 18480909e117SHerbert Xu spin_lock(&br->multicast_lock); 18490909e117SHerbert Xu 18500909e117SHerbert Xu switch (val) { 18510909e117SHerbert Xu case 0: 18520909e117SHerbert Xu case 1: 18530909e117SHerbert Xu case 2: 18540909e117SHerbert Xu p->multicast_router = val; 18550909e117SHerbert Xu err = 0; 18560909e117SHerbert Xu 1857949f1e39SSatish Ashok if (val < 2 && !hlist_unhashed(&p->rlist)) { 18580909e117SHerbert Xu hlist_del_init_rcu(&p->rlist); 1859949f1e39SSatish Ashok br_rtr_notify(br->dev, p, RTM_DELMDB); 1860949f1e39SSatish Ashok } 18610909e117SHerbert Xu 18620909e117SHerbert Xu if (val == 1) 18630909e117SHerbert Xu break; 18640909e117SHerbert Xu 18650909e117SHerbert Xu del_timer(&p->multicast_router_timer); 18660909e117SHerbert Xu 18670909e117SHerbert Xu if (val == 0) 18680909e117SHerbert Xu break; 18690909e117SHerbert Xu 18700909e117SHerbert Xu br_multicast_add_router(br, p); 18710909e117SHerbert Xu break; 18720909e117SHerbert Xu } 18730909e117SHerbert Xu 18740909e117SHerbert Xu spin_unlock(&br->multicast_lock); 18750909e117SHerbert Xu 18760909e117SHerbert Xu return err; 18770909e117SHerbert Xu } 1878561f1103SHerbert Xu 1879cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br, 188090010b36SLinus Lüssing struct bridge_mcast_own_query *query) 1881561f1103SHerbert Xu { 1882561f1103SHerbert Xu struct net_bridge_port *port; 188374857216SHerbert Xu 1884cc0fdd80SLinus Lüssing __br_multicast_open(br, query); 188574857216SHerbert Xu 188674857216SHerbert Xu list_for_each_entry(port, &br->port_list, list) { 188774857216SHerbert Xu if (port->state == BR_STATE_DISABLED || 188874857216SHerbert Xu port->state == BR_STATE_BLOCKING) 188974857216SHerbert Xu continue; 189074857216SHerbert Xu 189190010b36SLinus Lüssing if (query == &br->ip4_own_query) 189290010b36SLinus Lüssing br_multicast_enable(&port->ip4_own_query); 1893cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1894cc0fdd80SLinus Lüssing else 189590010b36SLinus Lüssing br_multicast_enable(&port->ip6_own_query); 1896cc0fdd80SLinus Lüssing #endif 189774857216SHerbert Xu } 189874857216SHerbert Xu } 189974857216SHerbert Xu 190074857216SHerbert Xu int br_multicast_toggle(struct net_bridge *br, unsigned long val) 190174857216SHerbert Xu { 19023a7fda06SHerbert Xu int err = 0; 1903e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 1904561f1103SHerbert Xu 1905ef5e0d82SAndrey Vagin spin_lock_bh(&br->multicast_lock); 1906561f1103SHerbert Xu if (br->multicast_disabled == !val) 1907561f1103SHerbert Xu goto unlock; 1908561f1103SHerbert Xu 1909561f1103SHerbert Xu br->multicast_disabled = !val; 1910561f1103SHerbert Xu if (br->multicast_disabled) 1911561f1103SHerbert Xu goto unlock; 1912561f1103SHerbert Xu 19133a7fda06SHerbert Xu if (!netif_running(br->dev)) 19143a7fda06SHerbert Xu goto unlock; 19153a7fda06SHerbert Xu 1916e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1917e8051688SEric Dumazet if (mdb) { 1918e8051688SEric Dumazet if (mdb->old) { 1919561f1103SHerbert Xu err = -EEXIST; 1920561f1103SHerbert Xu rollback: 1921561f1103SHerbert Xu br->multicast_disabled = !!val; 1922561f1103SHerbert Xu goto unlock; 1923561f1103SHerbert Xu } 1924561f1103SHerbert Xu 1925e8051688SEric Dumazet err = br_mdb_rehash(&br->mdb, mdb->max, 1926561f1103SHerbert Xu br->hash_elasticity); 1927561f1103SHerbert Xu if (err) 1928561f1103SHerbert Xu goto rollback; 1929561f1103SHerbert Xu } 1930561f1103SHerbert Xu 193190010b36SLinus Lüssing br_multicast_start_querier(br, &br->ip4_own_query); 1932cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 193390010b36SLinus Lüssing br_multicast_start_querier(br, &br->ip6_own_query); 1934cc0fdd80SLinus Lüssing #endif 1935561f1103SHerbert Xu 1936561f1103SHerbert Xu unlock: 1937ef5e0d82SAndrey Vagin spin_unlock_bh(&br->multicast_lock); 1938561f1103SHerbert Xu 1939561f1103SHerbert Xu return err; 1940561f1103SHerbert Xu } 1941b195167fSHerbert Xu 1942c5c23260SHerbert Xu int br_multicast_set_querier(struct net_bridge *br, unsigned long val) 1943c5c23260SHerbert Xu { 1944b00589afSLinus Lüssing unsigned long max_delay; 1945b00589afSLinus Lüssing 1946c5c23260SHerbert Xu val = !!val; 1947c5c23260SHerbert Xu 1948c5c23260SHerbert Xu spin_lock_bh(&br->multicast_lock); 1949c5c23260SHerbert Xu if (br->multicast_querier == val) 1950c5c23260SHerbert Xu goto unlock; 1951c5c23260SHerbert Xu 1952c5c23260SHerbert Xu br->multicast_querier = val; 1953b00589afSLinus Lüssing if (!val) 1954b00589afSLinus Lüssing goto unlock; 1955b00589afSLinus Lüssing 1956b00589afSLinus Lüssing max_delay = br->multicast_query_response_interval; 1957b00589afSLinus Lüssing 195890010b36SLinus Lüssing if (!timer_pending(&br->ip4_other_query.timer)) 195990010b36SLinus Lüssing br->ip4_other_query.delay_time = jiffies + max_delay; 1960cc0fdd80SLinus Lüssing 196190010b36SLinus Lüssing br_multicast_start_querier(br, &br->ip4_own_query); 1962cc0fdd80SLinus Lüssing 1963cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 196490010b36SLinus Lüssing if (!timer_pending(&br->ip6_other_query.timer)) 196590010b36SLinus Lüssing br->ip6_other_query.delay_time = jiffies + max_delay; 1966cc0fdd80SLinus Lüssing 196790010b36SLinus Lüssing br_multicast_start_querier(br, &br->ip6_own_query); 1968cc0fdd80SLinus Lüssing #endif 1969c5c23260SHerbert Xu 1970c5c23260SHerbert Xu unlock: 1971c5c23260SHerbert Xu spin_unlock_bh(&br->multicast_lock); 1972c5c23260SHerbert Xu 1973c5c23260SHerbert Xu return 0; 1974c5c23260SHerbert Xu } 1975c5c23260SHerbert Xu 1976b195167fSHerbert Xu int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val) 1977b195167fSHerbert Xu { 19786ae4ae8eSLinus Lüssing int err = -EINVAL; 1979b195167fSHerbert Xu u32 old; 1980e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 1981b195167fSHerbert Xu 1982fe0d692bSCurt Brune spin_lock_bh(&br->multicast_lock); 1983b195167fSHerbert Xu if (!is_power_of_2(val)) 1984b195167fSHerbert Xu goto unlock; 1985e8051688SEric Dumazet 1986e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1987e8051688SEric Dumazet if (mdb && val < mdb->size) 1988b195167fSHerbert Xu goto unlock; 1989b195167fSHerbert Xu 1990b195167fSHerbert Xu err = 0; 1991b195167fSHerbert Xu 1992b195167fSHerbert Xu old = br->hash_max; 1993b195167fSHerbert Xu br->hash_max = val; 1994b195167fSHerbert Xu 1995e8051688SEric Dumazet if (mdb) { 1996e8051688SEric Dumazet if (mdb->old) { 1997b195167fSHerbert Xu err = -EEXIST; 1998b195167fSHerbert Xu rollback: 1999b195167fSHerbert Xu br->hash_max = old; 2000b195167fSHerbert Xu goto unlock; 2001b195167fSHerbert Xu } 2002b195167fSHerbert Xu 2003b195167fSHerbert Xu err = br_mdb_rehash(&br->mdb, br->hash_max, 2004b195167fSHerbert Xu br->hash_elasticity); 2005b195167fSHerbert Xu if (err) 2006b195167fSHerbert Xu goto rollback; 2007b195167fSHerbert Xu } 2008b195167fSHerbert Xu 2009b195167fSHerbert Xu unlock: 2010fe0d692bSCurt Brune spin_unlock_bh(&br->multicast_lock); 2011b195167fSHerbert Xu 2012b195167fSHerbert Xu return err; 2013b195167fSHerbert Xu } 201407f8ac4aSLinus Lüssing 201507f8ac4aSLinus Lüssing /** 201607f8ac4aSLinus Lüssing * br_multicast_list_adjacent - Returns snooped multicast addresses 201707f8ac4aSLinus Lüssing * @dev: The bridge port adjacent to which to retrieve addresses 201807f8ac4aSLinus Lüssing * @br_ip_list: The list to store found, snooped multicast IP addresses in 201907f8ac4aSLinus Lüssing * 202007f8ac4aSLinus Lüssing * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast 202107f8ac4aSLinus Lüssing * snooping feature on all bridge ports of dev's bridge device, excluding 202207f8ac4aSLinus Lüssing * the addresses from dev itself. 202307f8ac4aSLinus Lüssing * 202407f8ac4aSLinus Lüssing * Returns the number of items added to br_ip_list. 202507f8ac4aSLinus Lüssing * 202607f8ac4aSLinus Lüssing * Notes: 202707f8ac4aSLinus Lüssing * - br_ip_list needs to be initialized by caller 202807f8ac4aSLinus Lüssing * - br_ip_list might contain duplicates in the end 202907f8ac4aSLinus Lüssing * (needs to be taken care of by caller) 203007f8ac4aSLinus Lüssing * - br_ip_list needs to be freed by caller 203107f8ac4aSLinus Lüssing */ 203207f8ac4aSLinus Lüssing int br_multicast_list_adjacent(struct net_device *dev, 203307f8ac4aSLinus Lüssing struct list_head *br_ip_list) 203407f8ac4aSLinus Lüssing { 203507f8ac4aSLinus Lüssing struct net_bridge *br; 203607f8ac4aSLinus Lüssing struct net_bridge_port *port; 203707f8ac4aSLinus Lüssing struct net_bridge_port_group *group; 203807f8ac4aSLinus Lüssing struct br_ip_list *entry; 203907f8ac4aSLinus Lüssing int count = 0; 204007f8ac4aSLinus Lüssing 204107f8ac4aSLinus Lüssing rcu_read_lock(); 204207f8ac4aSLinus Lüssing if (!br_ip_list || !br_port_exists(dev)) 204307f8ac4aSLinus Lüssing goto unlock; 204407f8ac4aSLinus Lüssing 204507f8ac4aSLinus Lüssing port = br_port_get_rcu(dev); 204607f8ac4aSLinus Lüssing if (!port || !port->br) 204707f8ac4aSLinus Lüssing goto unlock; 204807f8ac4aSLinus Lüssing 204907f8ac4aSLinus Lüssing br = port->br; 205007f8ac4aSLinus Lüssing 205107f8ac4aSLinus Lüssing list_for_each_entry_rcu(port, &br->port_list, list) { 205207f8ac4aSLinus Lüssing if (!port->dev || port->dev == dev) 205307f8ac4aSLinus Lüssing continue; 205407f8ac4aSLinus Lüssing 205507f8ac4aSLinus Lüssing hlist_for_each_entry_rcu(group, &port->mglist, mglist) { 205607f8ac4aSLinus Lüssing entry = kmalloc(sizeof(*entry), GFP_ATOMIC); 205707f8ac4aSLinus Lüssing if (!entry) 205807f8ac4aSLinus Lüssing goto unlock; 205907f8ac4aSLinus Lüssing 206007f8ac4aSLinus Lüssing entry->addr = group->addr; 206107f8ac4aSLinus Lüssing list_add(&entry->list, br_ip_list); 206207f8ac4aSLinus Lüssing count++; 206307f8ac4aSLinus Lüssing } 206407f8ac4aSLinus Lüssing } 206507f8ac4aSLinus Lüssing 206607f8ac4aSLinus Lüssing unlock: 206707f8ac4aSLinus Lüssing rcu_read_unlock(); 206807f8ac4aSLinus Lüssing return count; 206907f8ac4aSLinus Lüssing } 207007f8ac4aSLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_list_adjacent); 20712cd41431SLinus Lüssing 20722cd41431SLinus Lüssing /** 2073c34963e2SLinus Lüssing * br_multicast_has_querier_anywhere - Checks for a querier on a bridge 2074c34963e2SLinus Lüssing * @dev: The bridge port providing the bridge on which to check for a querier 2075c34963e2SLinus Lüssing * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 2076c34963e2SLinus Lüssing * 2077c34963e2SLinus Lüssing * Checks whether the given interface has a bridge on top and if so returns 2078c34963e2SLinus Lüssing * true if a valid querier exists anywhere on the bridged link layer. 2079c34963e2SLinus Lüssing * Otherwise returns false. 2080c34963e2SLinus Lüssing */ 2081c34963e2SLinus Lüssing bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto) 2082c34963e2SLinus Lüssing { 2083c34963e2SLinus Lüssing struct net_bridge *br; 2084c34963e2SLinus Lüssing struct net_bridge_port *port; 2085c34963e2SLinus Lüssing struct ethhdr eth; 2086c34963e2SLinus Lüssing bool ret = false; 2087c34963e2SLinus Lüssing 2088c34963e2SLinus Lüssing rcu_read_lock(); 2089c34963e2SLinus Lüssing if (!br_port_exists(dev)) 2090c34963e2SLinus Lüssing goto unlock; 2091c34963e2SLinus Lüssing 2092c34963e2SLinus Lüssing port = br_port_get_rcu(dev); 2093c34963e2SLinus Lüssing if (!port || !port->br) 2094c34963e2SLinus Lüssing goto unlock; 2095c34963e2SLinus Lüssing 2096c34963e2SLinus Lüssing br = port->br; 2097c34963e2SLinus Lüssing 2098c34963e2SLinus Lüssing memset(ð, 0, sizeof(eth)); 2099c34963e2SLinus Lüssing eth.h_proto = htons(proto); 2100c34963e2SLinus Lüssing 2101c34963e2SLinus Lüssing ret = br_multicast_querier_exists(br, ð); 2102c34963e2SLinus Lüssing 2103c34963e2SLinus Lüssing unlock: 2104c34963e2SLinus Lüssing rcu_read_unlock(); 2105c34963e2SLinus Lüssing return ret; 2106c34963e2SLinus Lüssing } 2107c34963e2SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere); 2108c34963e2SLinus Lüssing 2109c34963e2SLinus Lüssing /** 21102cd41431SLinus Lüssing * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port 21112cd41431SLinus Lüssing * @dev: The bridge port adjacent to which to check for a querier 21122cd41431SLinus Lüssing * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 21132cd41431SLinus Lüssing * 21142cd41431SLinus Lüssing * Checks whether the given interface has a bridge on top and if so returns 21152cd41431SLinus Lüssing * true if a selected querier is behind one of the other ports of this 21162cd41431SLinus Lüssing * bridge. Otherwise returns false. 21172cd41431SLinus Lüssing */ 21182cd41431SLinus Lüssing bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto) 21192cd41431SLinus Lüssing { 21202cd41431SLinus Lüssing struct net_bridge *br; 21212cd41431SLinus Lüssing struct net_bridge_port *port; 21222cd41431SLinus Lüssing bool ret = false; 21232cd41431SLinus Lüssing 21242cd41431SLinus Lüssing rcu_read_lock(); 21252cd41431SLinus Lüssing if (!br_port_exists(dev)) 21262cd41431SLinus Lüssing goto unlock; 21272cd41431SLinus Lüssing 21282cd41431SLinus Lüssing port = br_port_get_rcu(dev); 21292cd41431SLinus Lüssing if (!port || !port->br) 21302cd41431SLinus Lüssing goto unlock; 21312cd41431SLinus Lüssing 21322cd41431SLinus Lüssing br = port->br; 21332cd41431SLinus Lüssing 21342cd41431SLinus Lüssing switch (proto) { 21352cd41431SLinus Lüssing case ETH_P_IP: 21362cd41431SLinus Lüssing if (!timer_pending(&br->ip4_other_query.timer) || 21372cd41431SLinus Lüssing rcu_dereference(br->ip4_querier.port) == port) 21382cd41431SLinus Lüssing goto unlock; 21392cd41431SLinus Lüssing break; 21403993c4e1SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 21412cd41431SLinus Lüssing case ETH_P_IPV6: 21422cd41431SLinus Lüssing if (!timer_pending(&br->ip6_other_query.timer) || 21432cd41431SLinus Lüssing rcu_dereference(br->ip6_querier.port) == port) 21442cd41431SLinus Lüssing goto unlock; 21452cd41431SLinus Lüssing break; 21463993c4e1SLinus Lüssing #endif 21472cd41431SLinus Lüssing default: 21482cd41431SLinus Lüssing goto unlock; 21492cd41431SLinus Lüssing } 21502cd41431SLinus Lüssing 21512cd41431SLinus Lüssing ret = true; 21522cd41431SLinus Lüssing unlock: 21532cd41431SLinus Lüssing rcu_read_unlock(); 21542cd41431SLinus Lüssing return ret; 21552cd41431SLinus Lüssing } 21562cd41431SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent); 2157