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); 402ce297fcSCong Wang unsigned int br_mdb_rehash_seq; 41c83b8fabSHerbert Xu 428ef2a9a5SYOSHIFUJI Hideaki static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b) 438ef2a9a5SYOSHIFUJI Hideaki { 448ef2a9a5SYOSHIFUJI Hideaki if (a->proto != b->proto) 458ef2a9a5SYOSHIFUJI Hideaki return 0; 46b0e9a30dSVlad Yasevich if (a->vid != b->vid) 47b0e9a30dSVlad Yasevich return 0; 488ef2a9a5SYOSHIFUJI Hideaki switch (a->proto) { 498ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 508ef2a9a5SYOSHIFUJI Hideaki return a->u.ip4 == b->u.ip4; 51dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 5208b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 5308b202b6SYOSHIFUJI Hideaki return ipv6_addr_equal(&a->u.ip6, &b->u.ip6); 5408b202b6SYOSHIFUJI Hideaki #endif 558ef2a9a5SYOSHIFUJI Hideaki } 568ef2a9a5SYOSHIFUJI Hideaki return 0; 578ef2a9a5SYOSHIFUJI Hideaki } 588ef2a9a5SYOSHIFUJI Hideaki 59b0e9a30dSVlad Yasevich static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip, 60b0e9a30dSVlad Yasevich __u16 vid) 61eb1d1641SHerbert Xu { 62b0e9a30dSVlad Yasevich return jhash_2words((__force u32)ip, vid, mdb->secret) & (mdb->max - 1); 63eb1d1641SHerbert Xu } 64eb1d1641SHerbert Xu 65dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 6608b202b6SYOSHIFUJI Hideaki static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb, 67b0e9a30dSVlad Yasevich const struct in6_addr *ip, 68b0e9a30dSVlad Yasevich __u16 vid) 6908b202b6SYOSHIFUJI Hideaki { 70b0e9a30dSVlad Yasevich return jhash_2words(ipv6_addr_hash(ip), vid, 71b0e9a30dSVlad Yasevich mdb->secret) & (mdb->max - 1); 7208b202b6SYOSHIFUJI Hideaki } 7308b202b6SYOSHIFUJI Hideaki #endif 7408b202b6SYOSHIFUJI Hideaki 758ef2a9a5SYOSHIFUJI Hideaki static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb, 768ef2a9a5SYOSHIFUJI Hideaki struct br_ip *ip) 778ef2a9a5SYOSHIFUJI Hideaki { 788ef2a9a5SYOSHIFUJI Hideaki switch (ip->proto) { 798ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 80b0e9a30dSVlad Yasevich return __br_ip4_hash(mdb, ip->u.ip4, ip->vid); 81dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 8208b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 83b0e9a30dSVlad Yasevich return __br_ip6_hash(mdb, &ip->u.ip6, ip->vid); 8408b202b6SYOSHIFUJI Hideaki #endif 858ef2a9a5SYOSHIFUJI Hideaki } 868ef2a9a5SYOSHIFUJI Hideaki return 0; 87eb1d1641SHerbert Xu } 88eb1d1641SHerbert Xu 89eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *__br_mdb_ip_get( 908ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash) 91eb1d1641SHerbert Xu { 92eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 93eb1d1641SHerbert Xu 94b67bfe0dSSasha Levin hlist_for_each_entry_rcu(mp, &mdb->mhash[hash], hlist[mdb->ver]) { 958ef2a9a5SYOSHIFUJI Hideaki if (br_ip_equal(&mp->addr, dst)) 96eb1d1641SHerbert Xu return mp; 97eb1d1641SHerbert Xu } 98eb1d1641SHerbert Xu 99eb1d1641SHerbert Xu return NULL; 100eb1d1641SHerbert Xu } 101eb1d1641SHerbert Xu 102cfd56754SCong Wang struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge_mdb_htable *mdb, 103cfd56754SCong Wang struct br_ip *dst) 1047f285fa7SHerbert Xu { 1057f285fa7SHerbert Xu if (!mdb) 1067f285fa7SHerbert Xu return NULL; 1077f285fa7SHerbert Xu 1087f285fa7SHerbert Xu return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst)); 1097f285fa7SHerbert Xu } 1107f285fa7SHerbert Xu 1118ef2a9a5SYOSHIFUJI Hideaki static struct net_bridge_mdb_entry *br_mdb_ip4_get( 112b0e9a30dSVlad Yasevich struct net_bridge_mdb_htable *mdb, __be32 dst, __u16 vid) 113eb1d1641SHerbert Xu { 1148ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_dst; 1150821ec55SHerbert Xu 1168ef2a9a5SYOSHIFUJI Hideaki br_dst.u.ip4 = dst; 1178ef2a9a5SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IP); 118b0e9a30dSVlad Yasevich br_dst.vid = vid; 1198ef2a9a5SYOSHIFUJI Hideaki 1207f285fa7SHerbert Xu return br_mdb_ip_get(mdb, &br_dst); 1218ef2a9a5SYOSHIFUJI Hideaki } 1228ef2a9a5SYOSHIFUJI Hideaki 123dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 12408b202b6SYOSHIFUJI Hideaki static struct net_bridge_mdb_entry *br_mdb_ip6_get( 125b0e9a30dSVlad Yasevich struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst, 126b0e9a30dSVlad Yasevich __u16 vid) 12708b202b6SYOSHIFUJI Hideaki { 12808b202b6SYOSHIFUJI Hideaki struct br_ip br_dst; 12908b202b6SYOSHIFUJI Hideaki 1304e3fd7a0SAlexey Dobriyan br_dst.u.ip6 = *dst; 13108b202b6SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IPV6); 132b0e9a30dSVlad Yasevich br_dst.vid = vid; 13308b202b6SYOSHIFUJI Hideaki 1347f285fa7SHerbert Xu return br_mdb_ip_get(mdb, &br_dst); 13508b202b6SYOSHIFUJI Hideaki } 13608b202b6SYOSHIFUJI Hideaki #endif 13708b202b6SYOSHIFUJI Hideaki 138eb1d1641SHerbert Xu struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br, 139fbca58a2SCong Wang struct sk_buff *skb, u16 vid) 140eb1d1641SHerbert Xu { 141e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb); 1428ef2a9a5SYOSHIFUJI Hideaki struct br_ip ip; 1438ef2a9a5SYOSHIFUJI Hideaki 1447f285fa7SHerbert Xu if (br->multicast_disabled) 145eb1d1641SHerbert Xu return NULL; 146eb1d1641SHerbert Xu 1478ef2a9a5SYOSHIFUJI Hideaki if (BR_INPUT_SKB_CB(skb)->igmp) 1488ef2a9a5SYOSHIFUJI Hideaki return NULL; 1498ef2a9a5SYOSHIFUJI Hideaki 1508ef2a9a5SYOSHIFUJI Hideaki ip.proto = skb->protocol; 151fbca58a2SCong Wang ip.vid = vid; 1528ef2a9a5SYOSHIFUJI Hideaki 153eb1d1641SHerbert Xu switch (skb->protocol) { 154eb1d1641SHerbert Xu case htons(ETH_P_IP): 1558ef2a9a5SYOSHIFUJI Hideaki ip.u.ip4 = ip_hdr(skb)->daddr; 156eb1d1641SHerbert Xu break; 157dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 15808b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 1594e3fd7a0SAlexey Dobriyan ip.u.ip6 = ipv6_hdr(skb)->daddr; 16008b202b6SYOSHIFUJI Hideaki break; 16108b202b6SYOSHIFUJI Hideaki #endif 1628ef2a9a5SYOSHIFUJI Hideaki default: 1638ef2a9a5SYOSHIFUJI Hideaki return NULL; 164eb1d1641SHerbert Xu } 165eb1d1641SHerbert Xu 1668ef2a9a5SYOSHIFUJI Hideaki return br_mdb_ip_get(mdb, &ip); 167eb1d1641SHerbert Xu } 168eb1d1641SHerbert Xu 169eb1d1641SHerbert Xu static void br_mdb_free(struct rcu_head *head) 170eb1d1641SHerbert Xu { 171eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb = 172eb1d1641SHerbert Xu container_of(head, struct net_bridge_mdb_htable, rcu); 173eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old = mdb->old; 174eb1d1641SHerbert Xu 175eb1d1641SHerbert Xu mdb->old = NULL; 176eb1d1641SHerbert Xu kfree(old->mhash); 177eb1d1641SHerbert Xu kfree(old); 178eb1d1641SHerbert Xu } 179eb1d1641SHerbert Xu 180eb1d1641SHerbert Xu static int br_mdb_copy(struct net_bridge_mdb_htable *new, 181eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old, 182eb1d1641SHerbert Xu int elasticity) 183eb1d1641SHerbert Xu { 184eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 185eb1d1641SHerbert Xu int maxlen; 186eb1d1641SHerbert Xu int len; 187eb1d1641SHerbert Xu int i; 188eb1d1641SHerbert Xu 189eb1d1641SHerbert Xu for (i = 0; i < old->max; i++) 190b67bfe0dSSasha Levin hlist_for_each_entry(mp, &old->mhash[i], hlist[old->ver]) 191eb1d1641SHerbert Xu hlist_add_head(&mp->hlist[new->ver], 1928ef2a9a5SYOSHIFUJI Hideaki &new->mhash[br_ip_hash(new, &mp->addr)]); 193eb1d1641SHerbert Xu 194eb1d1641SHerbert Xu if (!elasticity) 195eb1d1641SHerbert Xu return 0; 196eb1d1641SHerbert Xu 197eb1d1641SHerbert Xu maxlen = 0; 198eb1d1641SHerbert Xu for (i = 0; i < new->max; i++) { 199eb1d1641SHerbert Xu len = 0; 200b67bfe0dSSasha Levin hlist_for_each_entry(mp, &new->mhash[i], hlist[new->ver]) 201eb1d1641SHerbert Xu len++; 202eb1d1641SHerbert Xu if (len > maxlen) 203eb1d1641SHerbert Xu maxlen = len; 204eb1d1641SHerbert Xu } 205eb1d1641SHerbert Xu 206eb1d1641SHerbert Xu return maxlen > elasticity ? -EINVAL : 0; 207eb1d1641SHerbert Xu } 208eb1d1641SHerbert Xu 209cfd56754SCong Wang void br_multicast_free_pg(struct rcu_head *head) 210eb1d1641SHerbert Xu { 211eb1d1641SHerbert Xu struct net_bridge_port_group *p = 212eb1d1641SHerbert Xu container_of(head, struct net_bridge_port_group, rcu); 213eb1d1641SHerbert Xu 214eb1d1641SHerbert Xu kfree(p); 215eb1d1641SHerbert Xu } 216eb1d1641SHerbert Xu 217eb1d1641SHerbert Xu static void br_multicast_free_group(struct rcu_head *head) 218eb1d1641SHerbert Xu { 219eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = 220eb1d1641SHerbert Xu container_of(head, struct net_bridge_mdb_entry, rcu); 221eb1d1641SHerbert Xu 222eb1d1641SHerbert Xu kfree(mp); 223eb1d1641SHerbert Xu } 224eb1d1641SHerbert Xu 225eb1d1641SHerbert Xu static void br_multicast_group_expired(unsigned long data) 226eb1d1641SHerbert Xu { 227eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = (void *)data; 228eb1d1641SHerbert Xu struct net_bridge *br = mp->br; 229eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 230eb1d1641SHerbert Xu 231eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 232eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&mp->timer)) 233eb1d1641SHerbert Xu goto out; 234eb1d1641SHerbert Xu 2358a870178SHerbert Xu mp->mglist = false; 236eb1d1641SHerbert Xu 237eb1d1641SHerbert Xu if (mp->ports) 238eb1d1641SHerbert Xu goto out; 239eb1d1641SHerbert Xu 240e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 241e8051688SEric Dumazet 242eb1d1641SHerbert Xu hlist_del_rcu(&mp->hlist[mdb->ver]); 243eb1d1641SHerbert Xu mdb->size--; 244eb1d1641SHerbert Xu 245eb1d1641SHerbert Xu call_rcu_bh(&mp->rcu, br_multicast_free_group); 246eb1d1641SHerbert Xu 247eb1d1641SHerbert Xu out: 248eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 249eb1d1641SHerbert Xu } 250eb1d1641SHerbert Xu 251eb1d1641SHerbert Xu static void br_multicast_del_pg(struct net_bridge *br, 252eb1d1641SHerbert Xu struct net_bridge_port_group *pg) 253eb1d1641SHerbert Xu { 254e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 255eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 256eb1d1641SHerbert Xu struct net_bridge_port_group *p; 257e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 258e8051688SEric Dumazet 259e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 260eb1d1641SHerbert Xu 2618ef2a9a5SYOSHIFUJI Hideaki mp = br_mdb_ip_get(mdb, &pg->addr); 262eb1d1641SHerbert Xu if (WARN_ON(!mp)) 263eb1d1641SHerbert Xu return; 264eb1d1641SHerbert Xu 265e8051688SEric Dumazet for (pp = &mp->ports; 266e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 267e8051688SEric Dumazet pp = &p->next) { 268eb1d1641SHerbert Xu if (p != pg) 269eb1d1641SHerbert Xu continue; 270eb1d1641SHerbert Xu 27183f6a740Sstephen hemminger rcu_assign_pointer(*pp, p->next); 272eb1d1641SHerbert Xu hlist_del_init(&p->mglist); 273eb1d1641SHerbert Xu del_timer(&p->timer); 274eb1d1641SHerbert Xu call_rcu_bh(&p->rcu, br_multicast_free_pg); 275eb1d1641SHerbert Xu 276454594f3SLinus Lüssing if (!mp->ports && !mp->mglist && 277eb1d1641SHerbert Xu netif_running(br->dev)) 278eb1d1641SHerbert Xu mod_timer(&mp->timer, jiffies); 279eb1d1641SHerbert Xu 280eb1d1641SHerbert Xu return; 281eb1d1641SHerbert Xu } 282eb1d1641SHerbert Xu 283eb1d1641SHerbert Xu WARN_ON(1); 284eb1d1641SHerbert Xu } 285eb1d1641SHerbert Xu 286eb1d1641SHerbert Xu static void br_multicast_port_group_expired(unsigned long data) 287eb1d1641SHerbert Xu { 288eb1d1641SHerbert Xu struct net_bridge_port_group *pg = (void *)data; 289eb1d1641SHerbert Xu struct net_bridge *br = pg->port->br; 290eb1d1641SHerbert Xu 291eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 292eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&pg->timer) || 293ccb1c31aSAmerigo Wang hlist_unhashed(&pg->mglist) || pg->state & MDB_PERMANENT) 294eb1d1641SHerbert Xu goto out; 295eb1d1641SHerbert Xu 296eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 297eb1d1641SHerbert Xu 298eb1d1641SHerbert Xu out: 299eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 300eb1d1641SHerbert Xu } 301eb1d1641SHerbert Xu 302e8051688SEric Dumazet static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max, 303eb1d1641SHerbert Xu int elasticity) 304eb1d1641SHerbert Xu { 305e8051688SEric Dumazet struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1); 306eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 307eb1d1641SHerbert Xu int err; 308eb1d1641SHerbert Xu 309eb1d1641SHerbert Xu mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC); 310eb1d1641SHerbert Xu if (!mdb) 311eb1d1641SHerbert Xu return -ENOMEM; 312eb1d1641SHerbert Xu 313eb1d1641SHerbert Xu mdb->max = max; 314eb1d1641SHerbert Xu mdb->old = old; 315eb1d1641SHerbert Xu 316eb1d1641SHerbert Xu mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC); 317eb1d1641SHerbert Xu if (!mdb->mhash) { 318eb1d1641SHerbert Xu kfree(mdb); 319eb1d1641SHerbert Xu return -ENOMEM; 320eb1d1641SHerbert Xu } 321eb1d1641SHerbert Xu 322eb1d1641SHerbert Xu mdb->size = old ? old->size : 0; 323eb1d1641SHerbert Xu mdb->ver = old ? old->ver ^ 1 : 0; 324eb1d1641SHerbert Xu 325eb1d1641SHerbert Xu if (!old || elasticity) 326eb1d1641SHerbert Xu get_random_bytes(&mdb->secret, sizeof(mdb->secret)); 327eb1d1641SHerbert Xu else 328eb1d1641SHerbert Xu mdb->secret = old->secret; 329eb1d1641SHerbert Xu 330eb1d1641SHerbert Xu if (!old) 331eb1d1641SHerbert Xu goto out; 332eb1d1641SHerbert Xu 333eb1d1641SHerbert Xu err = br_mdb_copy(mdb, old, elasticity); 334eb1d1641SHerbert Xu if (err) { 335eb1d1641SHerbert Xu kfree(mdb->mhash); 336eb1d1641SHerbert Xu kfree(mdb); 337eb1d1641SHerbert Xu return err; 338eb1d1641SHerbert Xu } 339eb1d1641SHerbert Xu 3402ce297fcSCong Wang br_mdb_rehash_seq++; 341eb1d1641SHerbert Xu call_rcu_bh(&mdb->rcu, br_mdb_free); 342eb1d1641SHerbert Xu 343eb1d1641SHerbert Xu out: 344eb1d1641SHerbert Xu rcu_assign_pointer(*mdbp, mdb); 345eb1d1641SHerbert Xu 346eb1d1641SHerbert Xu return 0; 347eb1d1641SHerbert Xu } 348eb1d1641SHerbert Xu 3498ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br, 350eb1d1641SHerbert Xu __be32 group) 351eb1d1641SHerbert Xu { 352eb1d1641SHerbert Xu struct sk_buff *skb; 353eb1d1641SHerbert Xu struct igmphdr *ih; 354eb1d1641SHerbert Xu struct ethhdr *eth; 355eb1d1641SHerbert Xu struct iphdr *iph; 356eb1d1641SHerbert Xu 357eb1d1641SHerbert Xu skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) + 358eb1d1641SHerbert Xu sizeof(*ih) + 4); 359eb1d1641SHerbert Xu if (!skb) 360eb1d1641SHerbert Xu goto out; 361eb1d1641SHerbert Xu 362eb1d1641SHerbert Xu skb->protocol = htons(ETH_P_IP); 363eb1d1641SHerbert Xu 364eb1d1641SHerbert Xu skb_reset_mac_header(skb); 365eb1d1641SHerbert Xu eth = eth_hdr(skb); 366eb1d1641SHerbert Xu 367e5a727f6SJoe Perches ether_addr_copy(eth->h_source, br->dev->dev_addr); 368eb1d1641SHerbert Xu eth->h_dest[0] = 1; 369eb1d1641SHerbert Xu eth->h_dest[1] = 0; 370eb1d1641SHerbert Xu eth->h_dest[2] = 0x5e; 371eb1d1641SHerbert Xu eth->h_dest[3] = 0; 372eb1d1641SHerbert Xu eth->h_dest[4] = 0; 373eb1d1641SHerbert Xu eth->h_dest[5] = 1; 374eb1d1641SHerbert Xu eth->h_proto = htons(ETH_P_IP); 375eb1d1641SHerbert Xu skb_put(skb, sizeof(*eth)); 376eb1d1641SHerbert Xu 377eb1d1641SHerbert Xu skb_set_network_header(skb, skb->len); 378eb1d1641SHerbert Xu iph = ip_hdr(skb); 379eb1d1641SHerbert Xu 380eb1d1641SHerbert Xu iph->version = 4; 381eb1d1641SHerbert Xu iph->ihl = 6; 382eb1d1641SHerbert Xu iph->tos = 0xc0; 383eb1d1641SHerbert Xu iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4); 384eb1d1641SHerbert Xu iph->id = 0; 385eb1d1641SHerbert Xu iph->frag_off = htons(IP_DF); 386eb1d1641SHerbert Xu iph->ttl = 1; 387eb1d1641SHerbert Xu iph->protocol = IPPROTO_IGMP; 3881c8ad5bfSCong Wang iph->saddr = br->multicast_query_use_ifaddr ? 3891c8ad5bfSCong Wang inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0; 390eb1d1641SHerbert Xu iph->daddr = htonl(INADDR_ALLHOSTS_GROUP); 391eb1d1641SHerbert Xu ((u8 *)&iph[1])[0] = IPOPT_RA; 392eb1d1641SHerbert Xu ((u8 *)&iph[1])[1] = 4; 393eb1d1641SHerbert Xu ((u8 *)&iph[1])[2] = 0; 394eb1d1641SHerbert Xu ((u8 *)&iph[1])[3] = 0; 395eb1d1641SHerbert Xu ip_send_check(iph); 396eb1d1641SHerbert Xu skb_put(skb, 24); 397eb1d1641SHerbert Xu 398eb1d1641SHerbert Xu skb_set_transport_header(skb, skb->len); 399eb1d1641SHerbert Xu ih = igmp_hdr(skb); 400eb1d1641SHerbert Xu ih->type = IGMP_HOST_MEMBERSHIP_QUERY; 401eb1d1641SHerbert Xu ih->code = (group ? br->multicast_last_member_interval : 402eb1d1641SHerbert Xu br->multicast_query_response_interval) / 403eb1d1641SHerbert Xu (HZ / IGMP_TIMER_SCALE); 404eb1d1641SHerbert Xu ih->group = group; 405eb1d1641SHerbert Xu ih->csum = 0; 406eb1d1641SHerbert Xu ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr)); 407eb1d1641SHerbert Xu skb_put(skb, sizeof(*ih)); 408eb1d1641SHerbert Xu 409eb1d1641SHerbert Xu __skb_pull(skb, sizeof(*eth)); 410eb1d1641SHerbert Xu 411eb1d1641SHerbert Xu out: 412eb1d1641SHerbert Xu return skb; 413eb1d1641SHerbert Xu } 414eb1d1641SHerbert Xu 415dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 41608b202b6SYOSHIFUJI Hideaki static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br, 417b71d1d42SEric Dumazet const struct in6_addr *group) 41808b202b6SYOSHIFUJI Hideaki { 41908b202b6SYOSHIFUJI Hideaki struct sk_buff *skb; 42008b202b6SYOSHIFUJI Hideaki struct ipv6hdr *ip6h; 42108b202b6SYOSHIFUJI Hideaki struct mld_msg *mldq; 42208b202b6SYOSHIFUJI Hideaki struct ethhdr *eth; 42308b202b6SYOSHIFUJI Hideaki u8 *hopopt; 42408b202b6SYOSHIFUJI Hideaki unsigned long interval; 42508b202b6SYOSHIFUJI Hideaki 42608b202b6SYOSHIFUJI Hideaki skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) + 42708b202b6SYOSHIFUJI Hideaki 8 + sizeof(*mldq)); 42808b202b6SYOSHIFUJI Hideaki if (!skb) 42908b202b6SYOSHIFUJI Hideaki goto out; 43008b202b6SYOSHIFUJI Hideaki 43108b202b6SYOSHIFUJI Hideaki skb->protocol = htons(ETH_P_IPV6); 43208b202b6SYOSHIFUJI Hideaki 43308b202b6SYOSHIFUJI Hideaki /* Ethernet header */ 43408b202b6SYOSHIFUJI Hideaki skb_reset_mac_header(skb); 43508b202b6SYOSHIFUJI Hideaki eth = eth_hdr(skb); 43608b202b6SYOSHIFUJI Hideaki 437e5a727f6SJoe Perches ether_addr_copy(eth->h_source, br->dev->dev_addr); 43808b202b6SYOSHIFUJI Hideaki eth->h_proto = htons(ETH_P_IPV6); 43908b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*eth)); 44008b202b6SYOSHIFUJI Hideaki 44108b202b6SYOSHIFUJI Hideaki /* IPv6 header + HbH option */ 44208b202b6SYOSHIFUJI Hideaki skb_set_network_header(skb, skb->len); 44308b202b6SYOSHIFUJI Hideaki ip6h = ipv6_hdr(skb); 44408b202b6SYOSHIFUJI Hideaki 44508b202b6SYOSHIFUJI Hideaki *(__force __be32 *)ip6h = htonl(0x60000000); 44676d66158SDavid Stevens ip6h->payload_len = htons(8 + sizeof(*mldq)); 44708b202b6SYOSHIFUJI Hideaki ip6h->nexthdr = IPPROTO_HOPOPTS; 44808b202b6SYOSHIFUJI Hideaki ip6h->hop_limit = 1; 449a7bff75bSLinus Lüssing ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1)); 450d1d81d4cSUlrich Weber if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0, 451d1d81d4cSUlrich Weber &ip6h->saddr)) { 452d1d81d4cSUlrich Weber kfree_skb(skb); 453d1d81d4cSUlrich Weber return NULL; 454d1d81d4cSUlrich Weber } 45536cff5a1SLinus Lüssing ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest); 45608b202b6SYOSHIFUJI Hideaki 45708b202b6SYOSHIFUJI Hideaki hopopt = (u8 *)(ip6h + 1); 45808b202b6SYOSHIFUJI Hideaki hopopt[0] = IPPROTO_ICMPV6; /* next hdr */ 45908b202b6SYOSHIFUJI Hideaki hopopt[1] = 0; /* length of HbH */ 46008b202b6SYOSHIFUJI Hideaki hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */ 46108b202b6SYOSHIFUJI Hideaki hopopt[3] = 2; /* Length of RA Option */ 46208b202b6SYOSHIFUJI Hideaki hopopt[4] = 0; /* Type = 0x0000 (MLD) */ 46308b202b6SYOSHIFUJI Hideaki hopopt[5] = 0; 4641de5a71cSEldad Zack hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */ 4651de5a71cSEldad Zack hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */ 46608b202b6SYOSHIFUJI Hideaki 46708b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*ip6h) + 8); 46808b202b6SYOSHIFUJI Hideaki 46908b202b6SYOSHIFUJI Hideaki /* ICMPv6 */ 47008b202b6SYOSHIFUJI Hideaki skb_set_transport_header(skb, skb->len); 47108b202b6SYOSHIFUJI Hideaki mldq = (struct mld_msg *) icmp6_hdr(skb); 47208b202b6SYOSHIFUJI Hideaki 47332de868cSLinus Lüssing interval = ipv6_addr_any(group) ? 47432de868cSLinus Lüssing br->multicast_query_response_interval : 47532de868cSLinus Lüssing br->multicast_last_member_interval; 47608b202b6SYOSHIFUJI Hideaki 47708b202b6SYOSHIFUJI Hideaki mldq->mld_type = ICMPV6_MGM_QUERY; 47808b202b6SYOSHIFUJI Hideaki mldq->mld_code = 0; 47908b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = 0; 48008b202b6SYOSHIFUJI Hideaki mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval)); 48108b202b6SYOSHIFUJI Hideaki mldq->mld_reserved = 0; 4824e3fd7a0SAlexey Dobriyan mldq->mld_mca = *group; 48308b202b6SYOSHIFUJI Hideaki 48408b202b6SYOSHIFUJI Hideaki /* checksum */ 48508b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, 48608b202b6SYOSHIFUJI Hideaki sizeof(*mldq), IPPROTO_ICMPV6, 48708b202b6SYOSHIFUJI Hideaki csum_partial(mldq, 48808b202b6SYOSHIFUJI Hideaki sizeof(*mldq), 0)); 48908b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*mldq)); 49008b202b6SYOSHIFUJI Hideaki 49108b202b6SYOSHIFUJI Hideaki __skb_pull(skb, sizeof(*eth)); 49208b202b6SYOSHIFUJI Hideaki 49308b202b6SYOSHIFUJI Hideaki out: 49408b202b6SYOSHIFUJI Hideaki return skb; 49508b202b6SYOSHIFUJI Hideaki } 49608b202b6SYOSHIFUJI Hideaki #endif 49708b202b6SYOSHIFUJI Hideaki 4988ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br, 4998ef2a9a5SYOSHIFUJI Hideaki struct br_ip *addr) 5008ef2a9a5SYOSHIFUJI Hideaki { 5018ef2a9a5SYOSHIFUJI Hideaki switch (addr->proto) { 5028ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 5038ef2a9a5SYOSHIFUJI Hideaki return br_ip4_multicast_alloc_query(br, addr->u.ip4); 504dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 50508b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 50608b202b6SYOSHIFUJI Hideaki return br_ip6_multicast_alloc_query(br, &addr->u.ip6); 50708b202b6SYOSHIFUJI Hideaki #endif 5088ef2a9a5SYOSHIFUJI Hideaki } 5098ef2a9a5SYOSHIFUJI Hideaki return NULL; 5108ef2a9a5SYOSHIFUJI Hideaki } 5118ef2a9a5SYOSHIFUJI Hideaki 512eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *br_multicast_get_group( 5138ef2a9a5SYOSHIFUJI Hideaki struct net_bridge *br, struct net_bridge_port *port, 5148ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group, int hash) 515eb1d1641SHerbert Xu { 516e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 517eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 51895c96174SEric Dumazet unsigned int count = 0; 51995c96174SEric Dumazet unsigned int max; 520eb1d1641SHerbert Xu int elasticity; 521eb1d1641SHerbert Xu int err; 522eb1d1641SHerbert Xu 523e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 524b67bfe0dSSasha Levin hlist_for_each_entry(mp, &mdb->mhash[hash], hlist[mdb->ver]) { 525eb1d1641SHerbert Xu count++; 5268ef2a9a5SYOSHIFUJI Hideaki if (unlikely(br_ip_equal(group, &mp->addr))) 527eb1d1641SHerbert Xu return mp; 528eb1d1641SHerbert Xu } 529eb1d1641SHerbert Xu 530eb1d1641SHerbert Xu elasticity = 0; 531eb1d1641SHerbert Xu max = mdb->max; 532eb1d1641SHerbert Xu 533eb1d1641SHerbert Xu if (unlikely(count > br->hash_elasticity && count)) { 534eb1d1641SHerbert Xu if (net_ratelimit()) 53528a16c97Sstephen hemminger br_info(br, "Multicast hash table " 536eb1d1641SHerbert Xu "chain limit reached: %s\n", 53728a16c97Sstephen hemminger port ? port->dev->name : br->dev->name); 538eb1d1641SHerbert Xu 539eb1d1641SHerbert Xu elasticity = br->hash_elasticity; 540eb1d1641SHerbert Xu } 541eb1d1641SHerbert Xu 542eb1d1641SHerbert Xu if (mdb->size >= max) { 543eb1d1641SHerbert Xu max *= 2; 544036be6dbSThomas Graf if (unlikely(max > br->hash_max)) { 545036be6dbSThomas Graf br_warn(br, "Multicast hash table maximum of %d " 546036be6dbSThomas Graf "reached, disabling snooping: %s\n", 547036be6dbSThomas Graf br->hash_max, 548036be6dbSThomas Graf port ? port->dev->name : br->dev->name); 549eb1d1641SHerbert Xu err = -E2BIG; 550eb1d1641SHerbert Xu disable: 551eb1d1641SHerbert Xu br->multicast_disabled = 1; 552eb1d1641SHerbert Xu goto err; 553eb1d1641SHerbert Xu } 554eb1d1641SHerbert Xu } 555eb1d1641SHerbert Xu 556eb1d1641SHerbert Xu if (max > mdb->max || elasticity) { 557eb1d1641SHerbert Xu if (mdb->old) { 558eb1d1641SHerbert Xu if (net_ratelimit()) 55928a16c97Sstephen hemminger br_info(br, "Multicast hash table " 560eb1d1641SHerbert Xu "on fire: %s\n", 56128a16c97Sstephen hemminger port ? port->dev->name : br->dev->name); 562eb1d1641SHerbert Xu err = -EEXIST; 563eb1d1641SHerbert Xu goto err; 564eb1d1641SHerbert Xu } 565eb1d1641SHerbert Xu 566eb1d1641SHerbert Xu err = br_mdb_rehash(&br->mdb, max, elasticity); 567eb1d1641SHerbert Xu if (err) { 56828a16c97Sstephen hemminger br_warn(br, "Cannot rehash multicast " 56928a16c97Sstephen hemminger "hash table, disabling snooping: %s, %d, %d\n", 57028a16c97Sstephen hemminger port ? port->dev->name : br->dev->name, 571eb1d1641SHerbert Xu mdb->size, err); 572eb1d1641SHerbert Xu goto disable; 573eb1d1641SHerbert Xu } 574eb1d1641SHerbert Xu 575eb1d1641SHerbert Xu err = -EAGAIN; 576eb1d1641SHerbert Xu goto err; 577eb1d1641SHerbert Xu } 578eb1d1641SHerbert Xu 579eb1d1641SHerbert Xu return NULL; 580eb1d1641SHerbert Xu 581eb1d1641SHerbert Xu err: 582eb1d1641SHerbert Xu mp = ERR_PTR(err); 583eb1d1641SHerbert Xu return mp; 584eb1d1641SHerbert Xu } 585eb1d1641SHerbert Xu 586cfd56754SCong Wang struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br, 587cfd56754SCong Wang struct net_bridge_port *port, struct br_ip *group) 588eb1d1641SHerbert Xu { 589e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 590eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 591eb1d1641SHerbert Xu int hash; 5924c0833bcSTobias Klauser int err; 593eb1d1641SHerbert Xu 594e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 595eb1d1641SHerbert Xu if (!mdb) { 5964c0833bcSTobias Klauser err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0); 5974c0833bcSTobias Klauser if (err) 5984c0833bcSTobias Klauser return ERR_PTR(err); 599eb1d1641SHerbert Xu goto rehash; 600eb1d1641SHerbert Xu } 601eb1d1641SHerbert Xu 602eb1d1641SHerbert Xu hash = br_ip_hash(mdb, group); 603eb1d1641SHerbert Xu mp = br_multicast_get_group(br, port, group, hash); 604eb1d1641SHerbert Xu switch (PTR_ERR(mp)) { 605eb1d1641SHerbert Xu case 0: 606eb1d1641SHerbert Xu break; 607eb1d1641SHerbert Xu 608eb1d1641SHerbert Xu case -EAGAIN: 609eb1d1641SHerbert Xu rehash: 610e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 611eb1d1641SHerbert Xu hash = br_ip_hash(mdb, group); 612eb1d1641SHerbert Xu break; 613eb1d1641SHerbert Xu 614eb1d1641SHerbert Xu default: 615eb1d1641SHerbert Xu goto out; 616eb1d1641SHerbert Xu } 617eb1d1641SHerbert Xu 618eb1d1641SHerbert Xu mp = kzalloc(sizeof(*mp), GFP_ATOMIC); 619eb1d1641SHerbert Xu if (unlikely(!mp)) 6204c0833bcSTobias Klauser return ERR_PTR(-ENOMEM); 621eb1d1641SHerbert Xu 622eb1d1641SHerbert Xu mp->br = br; 6238ef2a9a5SYOSHIFUJI Hideaki mp->addr = *group; 6241faabf2aSEric Dumazet setup_timer(&mp->timer, br_multicast_group_expired, 6251faabf2aSEric Dumazet (unsigned long)mp); 6261faabf2aSEric Dumazet 627eb1d1641SHerbert Xu hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]); 628eb1d1641SHerbert Xu mdb->size++; 629eb1d1641SHerbert Xu 630eb1d1641SHerbert Xu out: 631eb1d1641SHerbert Xu return mp; 632eb1d1641SHerbert Xu } 633eb1d1641SHerbert Xu 634cfd56754SCong Wang struct net_bridge_port_group *br_multicast_new_port_group( 635cfd56754SCong Wang struct net_bridge_port *port, 636cfd56754SCong Wang struct br_ip *group, 637ccb1c31aSAmerigo Wang struct net_bridge_port_group __rcu *next, 638ccb1c31aSAmerigo Wang unsigned char state) 639cfd56754SCong Wang { 640cfd56754SCong Wang struct net_bridge_port_group *p; 641cfd56754SCong Wang 642cfd56754SCong Wang p = kzalloc(sizeof(*p), GFP_ATOMIC); 643cfd56754SCong Wang if (unlikely(!p)) 644cfd56754SCong Wang return NULL; 645cfd56754SCong Wang 646cfd56754SCong Wang p->addr = *group; 647cfd56754SCong Wang p->port = port; 648ccb1c31aSAmerigo Wang p->state = state; 649eca2a43bSstephen hemminger rcu_assign_pointer(p->next, next); 650cfd56754SCong Wang hlist_add_head(&p->mglist, &port->mglist); 651cfd56754SCong Wang setup_timer(&p->timer, br_multicast_port_group_expired, 652cfd56754SCong Wang (unsigned long)p); 653cfd56754SCong Wang return p; 654cfd56754SCong Wang } 655cfd56754SCong Wang 656eb1d1641SHerbert Xu static int br_multicast_add_group(struct net_bridge *br, 6578ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 6588ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group) 659eb1d1641SHerbert Xu { 660eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 661eb1d1641SHerbert Xu struct net_bridge_port_group *p; 662e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 663454594f3SLinus Lüssing unsigned long now = jiffies; 664eb1d1641SHerbert Xu int err; 665eb1d1641SHerbert Xu 666eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 667eb1d1641SHerbert Xu if (!netif_running(br->dev) || 668eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 669eb1d1641SHerbert Xu goto out; 670eb1d1641SHerbert Xu 671eb1d1641SHerbert Xu mp = br_multicast_new_group(br, port, group); 672eb1d1641SHerbert Xu err = PTR_ERR(mp); 6734c0833bcSTobias Klauser if (IS_ERR(mp)) 674eb1d1641SHerbert Xu goto err; 675eb1d1641SHerbert Xu 676eb1d1641SHerbert Xu if (!port) { 6778a870178SHerbert Xu mp->mglist = true; 678454594f3SLinus Lüssing mod_timer(&mp->timer, now + br->multicast_membership_interval); 679eb1d1641SHerbert Xu goto out; 680eb1d1641SHerbert Xu } 681eb1d1641SHerbert Xu 682e8051688SEric Dumazet for (pp = &mp->ports; 683e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 684e8051688SEric Dumazet pp = &p->next) { 685eb1d1641SHerbert Xu if (p->port == port) 686454594f3SLinus Lüssing goto found; 687eb1d1641SHerbert Xu if ((unsigned long)p->port < (unsigned long)port) 688eb1d1641SHerbert Xu break; 689eb1d1641SHerbert Xu } 690eb1d1641SHerbert Xu 691ccb1c31aSAmerigo Wang p = br_multicast_new_port_group(port, group, *pp, MDB_TEMPORARY); 692eb1d1641SHerbert Xu if (unlikely(!p)) 693eb1d1641SHerbert Xu goto err; 694eb1d1641SHerbert Xu rcu_assign_pointer(*pp, p); 69537a393bcSCong Wang br_mdb_notify(br->dev, port, group, RTM_NEWMDB); 696eb1d1641SHerbert Xu 697454594f3SLinus Lüssing found: 698454594f3SLinus Lüssing mod_timer(&p->timer, now + br->multicast_membership_interval); 699eb1d1641SHerbert Xu out: 700eb1d1641SHerbert Xu err = 0; 701eb1d1641SHerbert Xu 702eb1d1641SHerbert Xu err: 703eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 704eb1d1641SHerbert Xu return err; 705eb1d1641SHerbert Xu } 706eb1d1641SHerbert Xu 7078ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_add_group(struct net_bridge *br, 7088ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 709b0e9a30dSVlad Yasevich __be32 group, 710b0e9a30dSVlad Yasevich __u16 vid) 7118ef2a9a5SYOSHIFUJI Hideaki { 7128ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 7138ef2a9a5SYOSHIFUJI Hideaki 7148ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 7158ef2a9a5SYOSHIFUJI Hideaki return 0; 7168ef2a9a5SYOSHIFUJI Hideaki 7178ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 7188ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 719b0e9a30dSVlad Yasevich br_group.vid = vid; 7208ef2a9a5SYOSHIFUJI Hideaki 7218ef2a9a5SYOSHIFUJI Hideaki return br_multicast_add_group(br, port, &br_group); 7228ef2a9a5SYOSHIFUJI Hideaki } 7238ef2a9a5SYOSHIFUJI Hideaki 724dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 72508b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_add_group(struct net_bridge *br, 72608b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 727b0e9a30dSVlad Yasevich const struct in6_addr *group, 728b0e9a30dSVlad Yasevich __u16 vid) 72908b202b6SYOSHIFUJI Hideaki { 73008b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 73108b202b6SYOSHIFUJI Hideaki 7323c3769e6SLinus Lüssing if (ipv6_addr_is_ll_all_nodes(group)) 73308b202b6SYOSHIFUJI Hideaki return 0; 73408b202b6SYOSHIFUJI Hideaki 7354e3fd7a0SAlexey Dobriyan br_group.u.ip6 = *group; 7369cc6e0c4SLinus Lüssing br_group.proto = htons(ETH_P_IPV6); 737b0e9a30dSVlad Yasevich br_group.vid = vid; 73808b202b6SYOSHIFUJI Hideaki 73908b202b6SYOSHIFUJI Hideaki return br_multicast_add_group(br, port, &br_group); 74008b202b6SYOSHIFUJI Hideaki } 74108b202b6SYOSHIFUJI Hideaki #endif 74208b202b6SYOSHIFUJI Hideaki 743eb1d1641SHerbert Xu static void br_multicast_router_expired(unsigned long data) 744eb1d1641SHerbert Xu { 745eb1d1641SHerbert Xu struct net_bridge_port *port = (void *)data; 746eb1d1641SHerbert Xu struct net_bridge *br = port->br; 747eb1d1641SHerbert Xu 748eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 749eb1d1641SHerbert Xu if (port->multicast_router != 1 || 750eb1d1641SHerbert Xu timer_pending(&port->multicast_router_timer) || 751eb1d1641SHerbert Xu hlist_unhashed(&port->rlist)) 752eb1d1641SHerbert Xu goto out; 753eb1d1641SHerbert Xu 754eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 755eb1d1641SHerbert Xu 756eb1d1641SHerbert Xu out: 757eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 758eb1d1641SHerbert Xu } 759eb1d1641SHerbert Xu 760eb1d1641SHerbert Xu static void br_multicast_local_router_expired(unsigned long data) 761eb1d1641SHerbert Xu { 762eb1d1641SHerbert Xu } 763eb1d1641SHerbert Xu 764cc0fdd80SLinus Lüssing static void br_multicast_querier_expired(struct net_bridge *br, 76590010b36SLinus Lüssing struct bridge_mcast_own_query *query) 766c83b8fabSHerbert Xu { 767c83b8fabSHerbert Xu spin_lock(&br->multicast_lock); 768c83b8fabSHerbert Xu if (!netif_running(br->dev) || br->multicast_disabled) 769c83b8fabSHerbert Xu goto out; 770c83b8fabSHerbert Xu 771cc0fdd80SLinus Lüssing br_multicast_start_querier(br, query); 772c83b8fabSHerbert Xu 773c83b8fabSHerbert Xu out: 774c83b8fabSHerbert Xu spin_unlock(&br->multicast_lock); 775c83b8fabSHerbert Xu } 776c83b8fabSHerbert Xu 777cc0fdd80SLinus Lüssing static void br_ip4_multicast_querier_expired(unsigned long data) 778cc0fdd80SLinus Lüssing { 779cc0fdd80SLinus Lüssing struct net_bridge *br = (void *)data; 780cc0fdd80SLinus Lüssing 78190010b36SLinus Lüssing br_multicast_querier_expired(br, &br->ip4_own_query); 782cc0fdd80SLinus Lüssing } 783cc0fdd80SLinus Lüssing 784cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 785cc0fdd80SLinus Lüssing static void br_ip6_multicast_querier_expired(unsigned long data) 786cc0fdd80SLinus Lüssing { 787cc0fdd80SLinus Lüssing struct net_bridge *br = (void *)data; 788cc0fdd80SLinus Lüssing 78990010b36SLinus Lüssing br_multicast_querier_expired(br, &br->ip6_own_query); 790cc0fdd80SLinus Lüssing } 791cc0fdd80SLinus Lüssing #endif 792cc0fdd80SLinus Lüssing 793dc4eb53aSLinus Lüssing static void br_multicast_select_own_querier(struct net_bridge *br, 794dc4eb53aSLinus Lüssing struct br_ip *ip, 795dc4eb53aSLinus Lüssing struct sk_buff *skb) 796dc4eb53aSLinus Lüssing { 797dc4eb53aSLinus Lüssing if (ip->proto == htons(ETH_P_IP)) 798dc4eb53aSLinus Lüssing br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr; 799dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 800dc4eb53aSLinus Lüssing else 801dc4eb53aSLinus Lüssing br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr; 802dc4eb53aSLinus Lüssing #endif 803dc4eb53aSLinus Lüssing } 804dc4eb53aSLinus Lüssing 8058ef2a9a5SYOSHIFUJI Hideaki static void __br_multicast_send_query(struct net_bridge *br, 8068ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 8078ef2a9a5SYOSHIFUJI Hideaki struct br_ip *ip) 808eb1d1641SHerbert Xu { 809eb1d1641SHerbert Xu struct sk_buff *skb; 810eb1d1641SHerbert Xu 8118ef2a9a5SYOSHIFUJI Hideaki skb = br_multicast_alloc_query(br, ip); 812eb1d1641SHerbert Xu if (!skb) 8138ef2a9a5SYOSHIFUJI Hideaki return; 814eb1d1641SHerbert Xu 815eb1d1641SHerbert Xu if (port) { 816eb1d1641SHerbert Xu skb->dev = port->dev; 8177026b1ddSDavid Miller NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, NULL, skb, 8187026b1ddSDavid Miller NULL, skb->dev, 819f0b4eeceSLinus Lüssing br_dev_queue_push_xmit); 820dc4eb53aSLinus Lüssing } else { 821dc4eb53aSLinus Lüssing br_multicast_select_own_querier(br, ip, skb); 822eb1d1641SHerbert Xu netif_rx(skb); 8238ef2a9a5SYOSHIFUJI Hideaki } 824dc4eb53aSLinus Lüssing } 825eb1d1641SHerbert Xu 8268ef2a9a5SYOSHIFUJI Hideaki static void br_multicast_send_query(struct net_bridge *br, 827cc0fdd80SLinus Lüssing struct net_bridge_port *port, 82890010b36SLinus Lüssing struct bridge_mcast_own_query *own_query) 8298ef2a9a5SYOSHIFUJI Hideaki { 8308ef2a9a5SYOSHIFUJI Hideaki unsigned long time; 8318ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 83290010b36SLinus Lüssing struct bridge_mcast_other_query *other_query = NULL; 8338ef2a9a5SYOSHIFUJI Hideaki 8348ef2a9a5SYOSHIFUJI Hideaki if (!netif_running(br->dev) || br->multicast_disabled || 835cc0fdd80SLinus Lüssing !br->multicast_querier) 8368ef2a9a5SYOSHIFUJI Hideaki return; 8378ef2a9a5SYOSHIFUJI Hideaki 83808b202b6SYOSHIFUJI Hideaki memset(&br_group.u, 0, sizeof(br_group.u)); 8398ef2a9a5SYOSHIFUJI Hideaki 84090010b36SLinus Lüssing if (port ? (own_query == &port->ip4_own_query) : 84190010b36SLinus Lüssing (own_query == &br->ip4_own_query)) { 84290010b36SLinus Lüssing other_query = &br->ip4_other_query; 84308b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 844dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 845cc0fdd80SLinus Lüssing } else { 84690010b36SLinus Lüssing other_query = &br->ip6_other_query; 84708b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 84808b202b6SYOSHIFUJI Hideaki #endif 849eb1d1641SHerbert Xu } 850eb1d1641SHerbert Xu 85190010b36SLinus Lüssing if (!other_query || timer_pending(&other_query->timer)) 852cc0fdd80SLinus Lüssing return; 853cc0fdd80SLinus Lüssing 854cc0fdd80SLinus Lüssing __br_multicast_send_query(br, port, &br_group); 855cc0fdd80SLinus Lüssing 856cc0fdd80SLinus Lüssing time = jiffies; 85790010b36SLinus Lüssing time += own_query->startup_sent < br->multicast_startup_query_count ? 858cc0fdd80SLinus Lüssing br->multicast_startup_query_interval : 859cc0fdd80SLinus Lüssing br->multicast_query_interval; 86090010b36SLinus Lüssing mod_timer(&own_query->timer, time); 861cc0fdd80SLinus Lüssing } 862cc0fdd80SLinus Lüssing 86390010b36SLinus Lüssing static void 86490010b36SLinus Lüssing br_multicast_port_query_expired(struct net_bridge_port *port, 86590010b36SLinus Lüssing struct bridge_mcast_own_query *query) 866eb1d1641SHerbert Xu { 867eb1d1641SHerbert Xu struct net_bridge *br = port->br; 868eb1d1641SHerbert Xu 869eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 87002a780c0SDan Carpenter if (port->state == BR_STATE_DISABLED || 87102a780c0SDan Carpenter port->state == BR_STATE_BLOCKING) 872eb1d1641SHerbert Xu goto out; 873eb1d1641SHerbert Xu 874cc0fdd80SLinus Lüssing if (query->startup_sent < br->multicast_startup_query_count) 875cc0fdd80SLinus Lüssing query->startup_sent++; 876eb1d1641SHerbert Xu 877cc0fdd80SLinus Lüssing br_multicast_send_query(port->br, port, query); 878eb1d1641SHerbert Xu 879eb1d1641SHerbert Xu out: 880eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 881eb1d1641SHerbert Xu } 882eb1d1641SHerbert Xu 883cc0fdd80SLinus Lüssing static void br_ip4_multicast_port_query_expired(unsigned long data) 884cc0fdd80SLinus Lüssing { 885cc0fdd80SLinus Lüssing struct net_bridge_port *port = (void *)data; 886cc0fdd80SLinus Lüssing 88790010b36SLinus Lüssing br_multicast_port_query_expired(port, &port->ip4_own_query); 888cc0fdd80SLinus Lüssing } 889cc0fdd80SLinus Lüssing 890cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 891cc0fdd80SLinus Lüssing static void br_ip6_multicast_port_query_expired(unsigned long data) 892cc0fdd80SLinus Lüssing { 893cc0fdd80SLinus Lüssing struct net_bridge_port *port = (void *)data; 894cc0fdd80SLinus Lüssing 89590010b36SLinus Lüssing br_multicast_port_query_expired(port, &port->ip6_own_query); 896cc0fdd80SLinus Lüssing } 897cc0fdd80SLinus Lüssing #endif 898cc0fdd80SLinus Lüssing 899eb1d1641SHerbert Xu void br_multicast_add_port(struct net_bridge_port *port) 900eb1d1641SHerbert Xu { 901eb1d1641SHerbert Xu port->multicast_router = 1; 902eb1d1641SHerbert Xu 903eb1d1641SHerbert Xu setup_timer(&port->multicast_router_timer, br_multicast_router_expired, 904eb1d1641SHerbert Xu (unsigned long)port); 90590010b36SLinus Lüssing setup_timer(&port->ip4_own_query.timer, 90690010b36SLinus Lüssing br_ip4_multicast_port_query_expired, (unsigned long)port); 907cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 90890010b36SLinus Lüssing setup_timer(&port->ip6_own_query.timer, 90990010b36SLinus Lüssing br_ip6_multicast_port_query_expired, (unsigned long)port); 910cc0fdd80SLinus Lüssing #endif 911eb1d1641SHerbert Xu } 912eb1d1641SHerbert Xu 913eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port) 914eb1d1641SHerbert Xu { 915eb1d1641SHerbert Xu del_timer_sync(&port->multicast_router_timer); 916eb1d1641SHerbert Xu } 917eb1d1641SHerbert Xu 91890010b36SLinus Lüssing static void br_multicast_enable(struct bridge_mcast_own_query *query) 919561f1103SHerbert Xu { 920cc0fdd80SLinus Lüssing query->startup_sent = 0; 921561f1103SHerbert Xu 922cc0fdd80SLinus Lüssing if (try_to_del_timer_sync(&query->timer) >= 0 || 923cc0fdd80SLinus Lüssing del_timer(&query->timer)) 924cc0fdd80SLinus Lüssing mod_timer(&query->timer, jiffies); 925561f1103SHerbert Xu } 926561f1103SHerbert Xu 927eb1d1641SHerbert Xu void br_multicast_enable_port(struct net_bridge_port *port) 928eb1d1641SHerbert Xu { 929eb1d1641SHerbert Xu struct net_bridge *br = port->br; 930eb1d1641SHerbert Xu 931eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 932eb1d1641SHerbert Xu if (br->multicast_disabled || !netif_running(br->dev)) 933eb1d1641SHerbert Xu goto out; 934eb1d1641SHerbert Xu 93590010b36SLinus Lüssing br_multicast_enable(&port->ip4_own_query); 936cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 93790010b36SLinus Lüssing br_multicast_enable(&port->ip6_own_query); 938cc0fdd80SLinus Lüssing #endif 939eb1d1641SHerbert Xu 940eb1d1641SHerbert Xu out: 941eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 942eb1d1641SHerbert Xu } 943eb1d1641SHerbert Xu 944eb1d1641SHerbert Xu void br_multicast_disable_port(struct net_bridge_port *port) 945eb1d1641SHerbert Xu { 946eb1d1641SHerbert Xu struct net_bridge *br = port->br; 947eb1d1641SHerbert Xu struct net_bridge_port_group *pg; 948b67bfe0dSSasha Levin struct hlist_node *n; 949eb1d1641SHerbert Xu 950eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 951b67bfe0dSSasha Levin hlist_for_each_entry_safe(pg, n, &port->mglist, mglist) 952eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 953eb1d1641SHerbert Xu 954eb1d1641SHerbert Xu if (!hlist_unhashed(&port->rlist)) 955eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 956eb1d1641SHerbert Xu del_timer(&port->multicast_router_timer); 95790010b36SLinus Lüssing del_timer(&port->ip4_own_query.timer); 958cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 95990010b36SLinus Lüssing del_timer(&port->ip6_own_query.timer); 960cc0fdd80SLinus Lüssing #endif 961eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 962eb1d1641SHerbert Xu } 963eb1d1641SHerbert Xu 9648ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_igmp3_report(struct net_bridge *br, 965eb1d1641SHerbert Xu struct net_bridge_port *port, 96606499098SVlad Yasevich struct sk_buff *skb, 96706499098SVlad Yasevich u16 vid) 968eb1d1641SHerbert Xu { 969eb1d1641SHerbert Xu struct igmpv3_report *ih; 970eb1d1641SHerbert Xu struct igmpv3_grec *grec; 971eb1d1641SHerbert Xu int i; 972eb1d1641SHerbert Xu int len; 973eb1d1641SHerbert Xu int num; 974eb1d1641SHerbert Xu int type; 975eb1d1641SHerbert Xu int err = 0; 976eb1d1641SHerbert Xu __be32 group; 977eb1d1641SHerbert Xu 978eb1d1641SHerbert Xu if (!pskb_may_pull(skb, sizeof(*ih))) 979eb1d1641SHerbert Xu return -EINVAL; 980eb1d1641SHerbert Xu 981eb1d1641SHerbert Xu ih = igmpv3_report_hdr(skb); 982eb1d1641SHerbert Xu num = ntohs(ih->ngrec); 983eb1d1641SHerbert Xu len = sizeof(*ih); 984eb1d1641SHerbert Xu 985eb1d1641SHerbert Xu for (i = 0; i < num; i++) { 986eb1d1641SHerbert Xu len += sizeof(*grec); 987eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 988eb1d1641SHerbert Xu return -EINVAL; 989eb1d1641SHerbert Xu 990fd218cf9SHerbert Xu grec = (void *)(skb->data + len - sizeof(*grec)); 991eb1d1641SHerbert Xu group = grec->grec_mca; 992eb1d1641SHerbert Xu type = grec->grec_type; 993eb1d1641SHerbert Xu 9948eabf95cSEric Dumazet len += ntohs(grec->grec_nsrcs) * 4; 995eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 996eb1d1641SHerbert Xu return -EINVAL; 997eb1d1641SHerbert Xu 998eb1d1641SHerbert Xu /* We treat this as an IGMPv2 report for now. */ 999eb1d1641SHerbert Xu switch (type) { 1000eb1d1641SHerbert Xu case IGMPV3_MODE_IS_INCLUDE: 1001eb1d1641SHerbert Xu case IGMPV3_MODE_IS_EXCLUDE: 1002eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_INCLUDE: 1003eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_EXCLUDE: 1004eb1d1641SHerbert Xu case IGMPV3_ALLOW_NEW_SOURCES: 1005eb1d1641SHerbert Xu case IGMPV3_BLOCK_OLD_SOURCES: 1006eb1d1641SHerbert Xu break; 1007eb1d1641SHerbert Xu 1008eb1d1641SHerbert Xu default: 1009eb1d1641SHerbert Xu continue; 1010eb1d1641SHerbert Xu } 1011eb1d1641SHerbert Xu 1012b0e9a30dSVlad Yasevich err = br_ip4_multicast_add_group(br, port, group, vid); 1013eb1d1641SHerbert Xu if (err) 1014eb1d1641SHerbert Xu break; 1015eb1d1641SHerbert Xu } 1016eb1d1641SHerbert Xu 1017eb1d1641SHerbert Xu return err; 1018eb1d1641SHerbert Xu } 1019eb1d1641SHerbert Xu 1020dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 102108b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_mld2_report(struct net_bridge *br, 102208b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 102306499098SVlad Yasevich struct sk_buff *skb, 102406499098SVlad Yasevich u16 vid) 102508b202b6SYOSHIFUJI Hideaki { 102608b202b6SYOSHIFUJI Hideaki struct icmp6hdr *icmp6h; 102708b202b6SYOSHIFUJI Hideaki struct mld2_grec *grec; 102808b202b6SYOSHIFUJI Hideaki int i; 102908b202b6SYOSHIFUJI Hideaki int len; 103008b202b6SYOSHIFUJI Hideaki int num; 103108b202b6SYOSHIFUJI Hideaki int err = 0; 103208b202b6SYOSHIFUJI Hideaki 103308b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*icmp6h))) 103408b202b6SYOSHIFUJI Hideaki return -EINVAL; 103508b202b6SYOSHIFUJI Hideaki 103608b202b6SYOSHIFUJI Hideaki icmp6h = icmp6_hdr(skb); 103708b202b6SYOSHIFUJI Hideaki num = ntohs(icmp6h->icmp6_dataun.un_data16[1]); 103808b202b6SYOSHIFUJI Hideaki len = sizeof(*icmp6h); 103908b202b6SYOSHIFUJI Hideaki 104008b202b6SYOSHIFUJI Hideaki for (i = 0; i < num; i++) { 104108b202b6SYOSHIFUJI Hideaki __be16 *nsrcs, _nsrcs; 104208b202b6SYOSHIFUJI Hideaki 104308b202b6SYOSHIFUJI Hideaki nsrcs = skb_header_pointer(skb, 104408b202b6SYOSHIFUJI Hideaki len + offsetof(struct mld2_grec, 1045649e984dSLinus Lüssing grec_nsrcs), 104608b202b6SYOSHIFUJI Hideaki sizeof(_nsrcs), &_nsrcs); 104708b202b6SYOSHIFUJI Hideaki if (!nsrcs) 104808b202b6SYOSHIFUJI Hideaki return -EINVAL; 104908b202b6SYOSHIFUJI Hideaki 105008b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, 105108b202b6SYOSHIFUJI Hideaki len + sizeof(*grec) + 1052d41db9f3SLinus Lüssing sizeof(struct in6_addr) * ntohs(*nsrcs))) 105308b202b6SYOSHIFUJI Hideaki return -EINVAL; 105408b202b6SYOSHIFUJI Hideaki 105508b202b6SYOSHIFUJI Hideaki grec = (struct mld2_grec *)(skb->data + len); 1056d41db9f3SLinus Lüssing len += sizeof(*grec) + 1057d41db9f3SLinus Lüssing sizeof(struct in6_addr) * ntohs(*nsrcs); 105808b202b6SYOSHIFUJI Hideaki 105908b202b6SYOSHIFUJI Hideaki /* We treat these as MLDv1 reports for now. */ 106008b202b6SYOSHIFUJI Hideaki switch (grec->grec_type) { 106108b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_INCLUDE: 106208b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_EXCLUDE: 106308b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_INCLUDE: 106408b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_EXCLUDE: 106508b202b6SYOSHIFUJI Hideaki case MLD2_ALLOW_NEW_SOURCES: 106608b202b6SYOSHIFUJI Hideaki case MLD2_BLOCK_OLD_SOURCES: 106708b202b6SYOSHIFUJI Hideaki break; 106808b202b6SYOSHIFUJI Hideaki 106908b202b6SYOSHIFUJI Hideaki default: 107008b202b6SYOSHIFUJI Hideaki continue; 107108b202b6SYOSHIFUJI Hideaki } 107208b202b6SYOSHIFUJI Hideaki 1073b0e9a30dSVlad Yasevich err = br_ip6_multicast_add_group(br, port, &grec->grec_mca, 1074b0e9a30dSVlad Yasevich vid); 107547cc84ceSThadeu Lima de Souza Cascardo if (err) 107608b202b6SYOSHIFUJI Hideaki break; 107708b202b6SYOSHIFUJI Hideaki } 107808b202b6SYOSHIFUJI Hideaki 107908b202b6SYOSHIFUJI Hideaki return err; 108008b202b6SYOSHIFUJI Hideaki } 108108b202b6SYOSHIFUJI Hideaki #endif 108208b202b6SYOSHIFUJI Hideaki 1083dc4eb53aSLinus Lüssing static bool br_ip4_multicast_select_querier(struct net_bridge *br, 10842cd41431SLinus Lüssing struct net_bridge_port *port, 1085dc4eb53aSLinus Lüssing __be32 saddr) 1086dc4eb53aSLinus Lüssing { 1087dc4eb53aSLinus Lüssing if (!timer_pending(&br->ip4_own_query.timer) && 1088dc4eb53aSLinus Lüssing !timer_pending(&br->ip4_other_query.timer)) 1089dc4eb53aSLinus Lüssing goto update; 1090dc4eb53aSLinus Lüssing 1091dc4eb53aSLinus Lüssing if (!br->ip4_querier.addr.u.ip4) 1092dc4eb53aSLinus Lüssing goto update; 1093dc4eb53aSLinus Lüssing 1094dc4eb53aSLinus Lüssing if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4)) 1095dc4eb53aSLinus Lüssing goto update; 1096dc4eb53aSLinus Lüssing 1097dc4eb53aSLinus Lüssing return false; 1098dc4eb53aSLinus Lüssing 1099dc4eb53aSLinus Lüssing update: 1100dc4eb53aSLinus Lüssing br->ip4_querier.addr.u.ip4 = saddr; 1101dc4eb53aSLinus Lüssing 11022cd41431SLinus Lüssing /* update protected by general multicast_lock by caller */ 11032cd41431SLinus Lüssing rcu_assign_pointer(br->ip4_querier.port, port); 11042cd41431SLinus Lüssing 1105dc4eb53aSLinus Lüssing return true; 1106dc4eb53aSLinus Lüssing } 1107dc4eb53aSLinus Lüssing 1108dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1109dc4eb53aSLinus Lüssing static bool br_ip6_multicast_select_querier(struct net_bridge *br, 11102cd41431SLinus Lüssing struct net_bridge_port *port, 1111dc4eb53aSLinus Lüssing struct in6_addr *saddr) 1112dc4eb53aSLinus Lüssing { 1113dc4eb53aSLinus Lüssing if (!timer_pending(&br->ip6_own_query.timer) && 1114dc4eb53aSLinus Lüssing !timer_pending(&br->ip6_other_query.timer)) 1115dc4eb53aSLinus Lüssing goto update; 1116dc4eb53aSLinus Lüssing 1117dc4eb53aSLinus Lüssing if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0) 1118dc4eb53aSLinus Lüssing goto update; 1119dc4eb53aSLinus Lüssing 1120dc4eb53aSLinus Lüssing return false; 1121dc4eb53aSLinus Lüssing 1122dc4eb53aSLinus Lüssing update: 1123dc4eb53aSLinus Lüssing br->ip6_querier.addr.u.ip6 = *saddr; 1124dc4eb53aSLinus Lüssing 11252cd41431SLinus Lüssing /* update protected by general multicast_lock by caller */ 11262cd41431SLinus Lüssing rcu_assign_pointer(br->ip6_querier.port, port); 11272cd41431SLinus Lüssing 1128dc4eb53aSLinus Lüssing return true; 1129dc4eb53aSLinus Lüssing } 1130dc4eb53aSLinus Lüssing #endif 1131dc4eb53aSLinus Lüssing 1132dc4eb53aSLinus Lüssing static bool br_multicast_select_querier(struct net_bridge *br, 11332cd41431SLinus Lüssing struct net_bridge_port *port, 1134dc4eb53aSLinus Lüssing struct br_ip *saddr) 1135dc4eb53aSLinus Lüssing { 1136dc4eb53aSLinus Lüssing switch (saddr->proto) { 1137dc4eb53aSLinus Lüssing case htons(ETH_P_IP): 11382cd41431SLinus Lüssing return br_ip4_multicast_select_querier(br, port, saddr->u.ip4); 1139dc4eb53aSLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1140dc4eb53aSLinus Lüssing case htons(ETH_P_IPV6): 11412cd41431SLinus Lüssing return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6); 1142dc4eb53aSLinus Lüssing #endif 1143dc4eb53aSLinus Lüssing } 1144dc4eb53aSLinus Lüssing 1145dc4eb53aSLinus Lüssing return false; 1146dc4eb53aSLinus Lüssing } 1147dc4eb53aSLinus Lüssing 1148cc0fdd80SLinus Lüssing static void 114990010b36SLinus Lüssing br_multicast_update_query_timer(struct net_bridge *br, 115090010b36SLinus Lüssing struct bridge_mcast_other_query *query, 1151b00589afSLinus Lüssing unsigned long max_delay) 1152b00589afSLinus Lüssing { 115390010b36SLinus Lüssing if (!timer_pending(&query->timer)) 115490010b36SLinus Lüssing query->delay_time = jiffies + max_delay; 1155b00589afSLinus Lüssing 115690010b36SLinus Lüssing mod_timer(&query->timer, jiffies + br->multicast_querier_interval); 1157b00589afSLinus Lüssing } 1158b00589afSLinus Lüssing 11597e80c124Sstephen hemminger /* 11607c77602fSCong Wang * Add port to router_list 11617e80c124Sstephen hemminger * list is maintained ordered by pointer value 11627e80c124Sstephen hemminger * and locked by br->multicast_lock and RCU 11637e80c124Sstephen hemminger */ 11640909e117SHerbert Xu static void br_multicast_add_router(struct net_bridge *br, 11650909e117SHerbert Xu struct net_bridge_port *port) 11660909e117SHerbert Xu { 1167dcdca2c4Sstephen hemminger struct net_bridge_port *p; 1168b67bfe0dSSasha Levin struct hlist_node *slot = NULL; 11690909e117SHerbert Xu 1170b67bfe0dSSasha Levin hlist_for_each_entry(p, &br->router_list, rlist) { 11717e80c124Sstephen hemminger if ((unsigned long) port >= (unsigned long) p) 11727e80c124Sstephen hemminger break; 1173b67bfe0dSSasha Levin slot = &p->rlist; 1174dcdca2c4Sstephen hemminger } 11750909e117SHerbert Xu 11767e80c124Sstephen hemminger if (slot) 11771d023284SKen Helias hlist_add_behind_rcu(&port->rlist, slot); 1178dcdca2c4Sstephen hemminger else 1179dcdca2c4Sstephen hemminger hlist_add_head_rcu(&port->rlist, &br->router_list); 11800909e117SHerbert Xu } 11810909e117SHerbert Xu 1182eb1d1641SHerbert Xu static void br_multicast_mark_router(struct net_bridge *br, 1183eb1d1641SHerbert Xu struct net_bridge_port *port) 1184eb1d1641SHerbert Xu { 1185eb1d1641SHerbert Xu unsigned long now = jiffies; 1186eb1d1641SHerbert Xu 1187eb1d1641SHerbert Xu if (!port) { 1188eb1d1641SHerbert Xu if (br->multicast_router == 1) 1189eb1d1641SHerbert Xu mod_timer(&br->multicast_router_timer, 1190eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1191eb1d1641SHerbert Xu return; 1192eb1d1641SHerbert Xu } 1193eb1d1641SHerbert Xu 1194eb1d1641SHerbert Xu if (port->multicast_router != 1) 1195eb1d1641SHerbert Xu return; 1196eb1d1641SHerbert Xu 1197eb1d1641SHerbert Xu if (!hlist_unhashed(&port->rlist)) 1198eb1d1641SHerbert Xu goto timer; 1199eb1d1641SHerbert Xu 12000909e117SHerbert Xu br_multicast_add_router(br, port); 1201eb1d1641SHerbert Xu 1202eb1d1641SHerbert Xu timer: 1203eb1d1641SHerbert Xu mod_timer(&port->multicast_router_timer, 1204eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1205eb1d1641SHerbert Xu } 1206eb1d1641SHerbert Xu 1207eb1d1641SHerbert Xu static void br_multicast_query_received(struct net_bridge *br, 1208eb1d1641SHerbert Xu struct net_bridge_port *port, 120990010b36SLinus Lüssing struct bridge_mcast_other_query *query, 1210dc4eb53aSLinus Lüssing struct br_ip *saddr, 1211b00589afSLinus Lüssing unsigned long max_delay) 1212eb1d1641SHerbert Xu { 12132cd41431SLinus Lüssing if (!br_multicast_select_querier(br, port, saddr)) 1214eb1d1641SHerbert Xu return; 1215eb1d1641SHerbert Xu 1216dc4eb53aSLinus Lüssing br_multicast_update_query_timer(br, query, max_delay); 1217eb1d1641SHerbert Xu br_multicast_mark_router(br, port); 1218eb1d1641SHerbert Xu } 1219eb1d1641SHerbert Xu 12208ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_query(struct net_bridge *br, 1221eb1d1641SHerbert Xu struct net_bridge_port *port, 122206499098SVlad Yasevich struct sk_buff *skb, 122306499098SVlad Yasevich u16 vid) 1224eb1d1641SHerbert Xu { 1225b71d1d42SEric Dumazet const struct iphdr *iph = ip_hdr(skb); 1226eb1d1641SHerbert Xu struct igmphdr *ih = igmp_hdr(skb); 1227eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1228eb1d1641SHerbert Xu struct igmpv3_query *ih3; 1229eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1230e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 1231dc4eb53aSLinus Lüssing struct br_ip saddr; 1232eb1d1641SHerbert Xu unsigned long max_delay; 1233eb1d1641SHerbert Xu unsigned long now = jiffies; 1234eb1d1641SHerbert Xu __be32 group; 1235bec68ff1SYOSHIFUJI Hideaki int err = 0; 1236eb1d1641SHerbert Xu 1237eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1238eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1239eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 1240eb1d1641SHerbert Xu goto out; 1241eb1d1641SHerbert Xu 1242eb1d1641SHerbert Xu group = ih->group; 1243eb1d1641SHerbert Xu 1244eb1d1641SHerbert Xu if (skb->len == sizeof(*ih)) { 1245eb1d1641SHerbert Xu max_delay = ih->code * (HZ / IGMP_TIMER_SCALE); 1246eb1d1641SHerbert Xu 1247eb1d1641SHerbert Xu if (!max_delay) { 1248eb1d1641SHerbert Xu max_delay = 10 * HZ; 1249eb1d1641SHerbert Xu group = 0; 1250eb1d1641SHerbert Xu } 1251eb1d1641SHerbert Xu } else { 1252bec68ff1SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) { 1253bec68ff1SYOSHIFUJI Hideaki err = -EINVAL; 1254bec68ff1SYOSHIFUJI Hideaki goto out; 1255bec68ff1SYOSHIFUJI Hideaki } 1256eb1d1641SHerbert Xu 1257eb1d1641SHerbert Xu ih3 = igmpv3_query_hdr(skb); 1258eb1d1641SHerbert Xu if (ih3->nsrcs) 1259bec68ff1SYOSHIFUJI Hideaki goto out; 1260eb1d1641SHerbert Xu 12610ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 max_delay = ih3->code ? 12620ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1; 1263eb1d1641SHerbert Xu } 1264eb1d1641SHerbert Xu 12659ed973ccSLinus Lüssing /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer 12669ed973ccSLinus Lüssing * all-systems destination addresses (224.0.0.1) for general queries 12679ed973ccSLinus Lüssing */ 12689ed973ccSLinus Lüssing if (!group && iph->daddr != htonl(INADDR_ALLHOSTS_GROUP)) { 12699ed973ccSLinus Lüssing err = -EINVAL; 12709ed973ccSLinus Lüssing goto out; 12719ed973ccSLinus Lüssing } 12729ed973ccSLinus Lüssing 1273dc4eb53aSLinus Lüssing if (!group) { 1274dc4eb53aSLinus Lüssing saddr.proto = htons(ETH_P_IP); 1275dc4eb53aSLinus Lüssing saddr.u.ip4 = iph->saddr; 1276b00589afSLinus Lüssing 1277dc4eb53aSLinus Lüssing br_multicast_query_received(br, port, &br->ip4_other_query, 1278dc4eb53aSLinus Lüssing &saddr, max_delay); 1279eb1d1641SHerbert Xu goto out; 1280dc4eb53aSLinus Lüssing } 1281eb1d1641SHerbert Xu 1282b0e9a30dSVlad Yasevich mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid); 1283eb1d1641SHerbert Xu if (!mp) 1284eb1d1641SHerbert Xu goto out; 1285eb1d1641SHerbert Xu 1286eb1d1641SHerbert Xu max_delay *= br->multicast_last_member_count; 1287eb1d1641SHerbert Xu 12888a870178SHerbert Xu if (mp->mglist && 1289eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1290eb1d1641SHerbert Xu time_after(mp->timer.expires, now + max_delay) : 1291eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) 1292eb1d1641SHerbert Xu mod_timer(&mp->timer, now + max_delay); 1293eb1d1641SHerbert Xu 1294e8051688SEric Dumazet for (pp = &mp->ports; 1295e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 1296e8051688SEric Dumazet pp = &p->next) { 1297eb1d1641SHerbert Xu if (timer_pending(&p->timer) ? 1298eb1d1641SHerbert Xu time_after(p->timer.expires, now + max_delay) : 1299eb1d1641SHerbert Xu try_to_del_timer_sync(&p->timer) >= 0) 130024f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 1301eb1d1641SHerbert Xu } 1302eb1d1641SHerbert Xu 1303eb1d1641SHerbert Xu out: 1304eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1305bec68ff1SYOSHIFUJI Hideaki return err; 1306eb1d1641SHerbert Xu } 1307eb1d1641SHerbert Xu 1308dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 130908b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_query(struct net_bridge *br, 131008b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 131106499098SVlad Yasevich struct sk_buff *skb, 131206499098SVlad Yasevich u16 vid) 131308b202b6SYOSHIFUJI Hideaki { 1314b71d1d42SEric Dumazet const struct ipv6hdr *ip6h = ipv6_hdr(skb); 1315eca2a43bSstephen hemminger struct mld_msg *mld; 131608b202b6SYOSHIFUJI Hideaki struct net_bridge_mdb_entry *mp; 131708b202b6SYOSHIFUJI Hideaki struct mld2_query *mld2q; 1318e8051688SEric Dumazet struct net_bridge_port_group *p; 1319e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 1320dc4eb53aSLinus Lüssing struct br_ip saddr; 132108b202b6SYOSHIFUJI Hideaki unsigned long max_delay; 132208b202b6SYOSHIFUJI Hideaki unsigned long now = jiffies; 1323b71d1d42SEric Dumazet const struct in6_addr *group = NULL; 13249ed973ccSLinus Lüssing bool is_general_query; 132508b202b6SYOSHIFUJI Hideaki int err = 0; 132608b202b6SYOSHIFUJI Hideaki 132708b202b6SYOSHIFUJI Hideaki spin_lock(&br->multicast_lock); 132808b202b6SYOSHIFUJI Hideaki if (!netif_running(br->dev) || 132908b202b6SYOSHIFUJI Hideaki (port && port->state == BR_STATE_DISABLED)) 133008b202b6SYOSHIFUJI Hideaki goto out; 133108b202b6SYOSHIFUJI Hideaki 13326565b9eeSLinus Lüssing /* RFC2710+RFC3810 (MLDv1+MLDv2) require link-local source addresses */ 13336565b9eeSLinus Lüssing if (!(ipv6_addr_type(&ip6h->saddr) & IPV6_ADDR_LINKLOCAL)) { 13346565b9eeSLinus Lüssing err = -EINVAL; 13356565b9eeSLinus Lüssing goto out; 13366565b9eeSLinus Lüssing } 13376565b9eeSLinus Lüssing 133808b202b6SYOSHIFUJI Hideaki if (skb->len == sizeof(*mld)) { 133908b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*mld))) { 134008b202b6SYOSHIFUJI Hideaki err = -EINVAL; 134108b202b6SYOSHIFUJI Hideaki goto out; 134208b202b6SYOSHIFUJI Hideaki } 134308b202b6SYOSHIFUJI Hideaki mld = (struct mld_msg *) icmp6_hdr(skb); 13444715213dSLi RongQing max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay)); 134508b202b6SYOSHIFUJI Hideaki if (max_delay) 134608b202b6SYOSHIFUJI Hideaki group = &mld->mld_mca; 1347248ba8ecSLinus Lüssing } else { 134808b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*mld2q))) { 134908b202b6SYOSHIFUJI Hideaki err = -EINVAL; 135008b202b6SYOSHIFUJI Hideaki goto out; 135108b202b6SYOSHIFUJI Hideaki } 135208b202b6SYOSHIFUJI Hideaki mld2q = (struct mld2_query *)icmp6_hdr(skb); 135308b202b6SYOSHIFUJI Hideaki if (!mld2q->mld2q_nsrcs) 135408b202b6SYOSHIFUJI Hideaki group = &mld2q->mld2q_mca; 1355e3f5b170SDaniel Borkmann 1356e3f5b170SDaniel Borkmann max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL); 135708b202b6SYOSHIFUJI Hideaki } 135808b202b6SYOSHIFUJI Hideaki 13599ed973ccSLinus Lüssing is_general_query = group && ipv6_addr_any(group); 13609ed973ccSLinus Lüssing 13619ed973ccSLinus Lüssing /* RFC2710+RFC3810 (MLDv1+MLDv2) require the multicast link layer 13629ed973ccSLinus Lüssing * all-nodes destination address (ff02::1) for general queries 13639ed973ccSLinus Lüssing */ 13649ed973ccSLinus Lüssing if (is_general_query && !ipv6_addr_is_ll_all_nodes(&ip6h->daddr)) { 13659ed973ccSLinus Lüssing err = -EINVAL; 13669ed973ccSLinus Lüssing goto out; 13679ed973ccSLinus Lüssing } 13689ed973ccSLinus Lüssing 1369dc4eb53aSLinus Lüssing if (is_general_query) { 1370dc4eb53aSLinus Lüssing saddr.proto = htons(ETH_P_IPV6); 1371dc4eb53aSLinus Lüssing saddr.u.ip6 = ip6h->saddr; 1372b00589afSLinus Lüssing 1373dc4eb53aSLinus Lüssing br_multicast_query_received(br, port, &br->ip6_other_query, 1374dc4eb53aSLinus Lüssing &saddr, max_delay); 137508b202b6SYOSHIFUJI Hideaki goto out; 13766c03ee8bSLinus Lüssing } else if (!group) { 13776c03ee8bSLinus Lüssing goto out; 1378dc4eb53aSLinus Lüssing } 137908b202b6SYOSHIFUJI Hideaki 1380b0e9a30dSVlad Yasevich mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid); 138108b202b6SYOSHIFUJI Hideaki if (!mp) 138208b202b6SYOSHIFUJI Hideaki goto out; 138308b202b6SYOSHIFUJI Hideaki 138408b202b6SYOSHIFUJI Hideaki max_delay *= br->multicast_last_member_count; 13858a870178SHerbert Xu if (mp->mglist && 138608b202b6SYOSHIFUJI Hideaki (timer_pending(&mp->timer) ? 138708b202b6SYOSHIFUJI Hideaki time_after(mp->timer.expires, now + max_delay) : 138808b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&mp->timer) >= 0)) 138908b202b6SYOSHIFUJI Hideaki mod_timer(&mp->timer, now + max_delay); 139008b202b6SYOSHIFUJI Hideaki 1391e8051688SEric Dumazet for (pp = &mp->ports; 1392e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 1393e8051688SEric Dumazet pp = &p->next) { 139408b202b6SYOSHIFUJI Hideaki if (timer_pending(&p->timer) ? 139508b202b6SYOSHIFUJI Hideaki time_after(p->timer.expires, now + max_delay) : 139608b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&p->timer) >= 0) 139724f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 139808b202b6SYOSHIFUJI Hideaki } 139908b202b6SYOSHIFUJI Hideaki 140008b202b6SYOSHIFUJI Hideaki out: 140108b202b6SYOSHIFUJI Hideaki spin_unlock(&br->multicast_lock); 140208b202b6SYOSHIFUJI Hideaki return err; 140308b202b6SYOSHIFUJI Hideaki } 140408b202b6SYOSHIFUJI Hideaki #endif 140508b202b6SYOSHIFUJI Hideaki 140690010b36SLinus Lüssing static void 140790010b36SLinus Lüssing br_multicast_leave_group(struct net_bridge *br, 1408eb1d1641SHerbert Xu struct net_bridge_port *port, 1409cc0fdd80SLinus Lüssing struct br_ip *group, 141090010b36SLinus Lüssing struct bridge_mcast_other_query *other_query, 141190010b36SLinus Lüssing struct bridge_mcast_own_query *own_query) 1412eb1d1641SHerbert Xu { 1413eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 1414eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1415eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1416eb1d1641SHerbert Xu unsigned long now; 1417eb1d1641SHerbert Xu unsigned long time; 1418eb1d1641SHerbert Xu 1419eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1420eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1421eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED) || 142290010b36SLinus Lüssing timer_pending(&other_query->timer)) 1423eb1d1641SHerbert Xu goto out; 1424eb1d1641SHerbert Xu 1425e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1426eb1d1641SHerbert Xu mp = br_mdb_ip_get(mdb, group); 1427eb1d1641SHerbert Xu if (!mp) 1428eb1d1641SHerbert Xu goto out; 1429eb1d1641SHerbert Xu 1430cc0fdd80SLinus Lüssing if (br->multicast_querier) { 14316b7df111SCong Wang __br_multicast_send_query(br, port, &mp->addr); 14326b7df111SCong Wang 14336b7df111SCong Wang time = jiffies + br->multicast_last_member_count * 14346b7df111SCong Wang br->multicast_last_member_interval; 1435cc0fdd80SLinus Lüssing 143690010b36SLinus Lüssing mod_timer(&own_query->timer, time); 14376b7df111SCong Wang 14386b7df111SCong Wang for (p = mlock_dereference(mp->ports, br); 14396b7df111SCong Wang p != NULL; 14406b7df111SCong Wang p = mlock_dereference(p->next, br)) { 14416b7df111SCong Wang if (p->port != port) 14426b7df111SCong Wang continue; 14436b7df111SCong Wang 14446b7df111SCong Wang if (!hlist_unhashed(&p->mglist) && 14456b7df111SCong Wang (timer_pending(&p->timer) ? 14466b7df111SCong Wang time_after(p->timer.expires, time) : 14476b7df111SCong Wang try_to_del_timer_sync(&p->timer) >= 0)) { 14486b7df111SCong Wang mod_timer(&p->timer, time); 14496b7df111SCong Wang } 14506b7df111SCong Wang 14516b7df111SCong Wang break; 14526b7df111SCong Wang } 14536b7df111SCong Wang } 14546b7df111SCong Wang 1455c2d3babfSDavid S. Miller if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) { 145650426b59SAmerigo Wang struct net_bridge_port_group __rcu **pp; 145750426b59SAmerigo Wang 145850426b59SAmerigo Wang for (pp = &mp->ports; 145950426b59SAmerigo Wang (p = mlock_dereference(*pp, br)) != NULL; 146050426b59SAmerigo Wang pp = &p->next) { 146150426b59SAmerigo Wang if (p->port != port) 146250426b59SAmerigo Wang continue; 146350426b59SAmerigo Wang 146450426b59SAmerigo Wang rcu_assign_pointer(*pp, p->next); 146550426b59SAmerigo Wang hlist_del_init(&p->mglist); 146650426b59SAmerigo Wang del_timer(&p->timer); 146750426b59SAmerigo Wang call_rcu_bh(&p->rcu, br_multicast_free_pg); 146837a393bcSCong Wang br_mdb_notify(br->dev, port, group, RTM_DELMDB); 146950426b59SAmerigo Wang 1470454594f3SLinus Lüssing if (!mp->ports && !mp->mglist && 147150426b59SAmerigo Wang netif_running(br->dev)) 147250426b59SAmerigo Wang mod_timer(&mp->timer, jiffies); 147350426b59SAmerigo Wang } 147450426b59SAmerigo Wang goto out; 147550426b59SAmerigo Wang } 147650426b59SAmerigo Wang 1477eb1d1641SHerbert Xu now = jiffies; 1478eb1d1641SHerbert Xu time = now + br->multicast_last_member_count * 1479eb1d1641SHerbert Xu br->multicast_last_member_interval; 1480eb1d1641SHerbert Xu 1481eb1d1641SHerbert Xu if (!port) { 1482454594f3SLinus Lüssing if (mp->mglist && 1483eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1484eb1d1641SHerbert Xu time_after(mp->timer.expires, time) : 1485eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) { 1486eb1d1641SHerbert Xu mod_timer(&mp->timer, time); 1487eb1d1641SHerbert Xu } 1488454594f3SLinus Lüssing 1489454594f3SLinus Lüssing goto out; 1490454594f3SLinus Lüssing } 1491454594f3SLinus Lüssing 1492454594f3SLinus Lüssing for (p = mlock_dereference(mp->ports, br); 1493454594f3SLinus Lüssing p != NULL; 1494454594f3SLinus Lüssing p = mlock_dereference(p->next, br)) { 1495454594f3SLinus Lüssing if (p->port != port) 1496454594f3SLinus Lüssing continue; 1497454594f3SLinus Lüssing 1498454594f3SLinus Lüssing if (!hlist_unhashed(&p->mglist) && 1499454594f3SLinus Lüssing (timer_pending(&p->timer) ? 1500454594f3SLinus Lüssing time_after(p->timer.expires, time) : 1501454594f3SLinus Lüssing try_to_del_timer_sync(&p->timer) >= 0)) { 1502454594f3SLinus Lüssing mod_timer(&p->timer, time); 1503454594f3SLinus Lüssing } 1504454594f3SLinus Lüssing 1505454594f3SLinus Lüssing break; 1506eb1d1641SHerbert Xu } 1507eb1d1641SHerbert Xu out: 1508eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1509eb1d1641SHerbert Xu } 1510eb1d1641SHerbert Xu 15118ef2a9a5SYOSHIFUJI Hideaki static void br_ip4_multicast_leave_group(struct net_bridge *br, 15128ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 1513b0e9a30dSVlad Yasevich __be32 group, 1514b0e9a30dSVlad Yasevich __u16 vid) 15158ef2a9a5SYOSHIFUJI Hideaki { 15168ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 151790010b36SLinus Lüssing struct bridge_mcast_own_query *own_query; 15188ef2a9a5SYOSHIFUJI Hideaki 15198ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 15208ef2a9a5SYOSHIFUJI Hideaki return; 15218ef2a9a5SYOSHIFUJI Hideaki 152290010b36SLinus Lüssing own_query = port ? &port->ip4_own_query : &br->ip4_own_query; 152390010b36SLinus Lüssing 15248ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 15258ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 1526b0e9a30dSVlad Yasevich br_group.vid = vid; 15278ef2a9a5SYOSHIFUJI Hideaki 152890010b36SLinus Lüssing br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query, 152990010b36SLinus Lüssing own_query); 15308ef2a9a5SYOSHIFUJI Hideaki } 15318ef2a9a5SYOSHIFUJI Hideaki 1532dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 153308b202b6SYOSHIFUJI Hideaki static void br_ip6_multicast_leave_group(struct net_bridge *br, 153408b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 1535b0e9a30dSVlad Yasevich const struct in6_addr *group, 1536b0e9a30dSVlad Yasevich __u16 vid) 153708b202b6SYOSHIFUJI Hideaki { 153808b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 153990010b36SLinus Lüssing struct bridge_mcast_own_query *own_query; 154008b202b6SYOSHIFUJI Hideaki 15413c3769e6SLinus Lüssing if (ipv6_addr_is_ll_all_nodes(group)) 154208b202b6SYOSHIFUJI Hideaki return; 154308b202b6SYOSHIFUJI Hideaki 154490010b36SLinus Lüssing own_query = port ? &port->ip6_own_query : &br->ip6_own_query; 154590010b36SLinus Lüssing 15464e3fd7a0SAlexey Dobriyan br_group.u.ip6 = *group; 154708b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 1548b0e9a30dSVlad Yasevich br_group.vid = vid; 154908b202b6SYOSHIFUJI Hideaki 155090010b36SLinus Lüssing br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query, 155190010b36SLinus Lüssing own_query); 155208b202b6SYOSHIFUJI Hideaki } 155308b202b6SYOSHIFUJI Hideaki #endif 15548ef2a9a5SYOSHIFUJI Hideaki 1555eb1d1641SHerbert Xu static int br_multicast_ipv4_rcv(struct net_bridge *br, 1556eb1d1641SHerbert Xu struct net_bridge_port *port, 155706499098SVlad Yasevich struct sk_buff *skb, 155806499098SVlad Yasevich u16 vid) 1559eb1d1641SHerbert Xu { 1560eb1d1641SHerbert Xu struct sk_buff *skb2 = skb; 1561b71d1d42SEric Dumazet const struct iphdr *iph; 1562eb1d1641SHerbert Xu struct igmphdr *ih; 156395c96174SEric Dumazet unsigned int len; 156495c96174SEric Dumazet unsigned int offset; 1565eb1d1641SHerbert Xu int err; 1566eb1d1641SHerbert Xu 1567eb1d1641SHerbert Xu /* We treat OOM as packet loss for now. */ 1568eb1d1641SHerbert Xu if (!pskb_may_pull(skb, sizeof(*iph))) 1569eb1d1641SHerbert Xu return -EINVAL; 1570eb1d1641SHerbert Xu 1571eb1d1641SHerbert Xu iph = ip_hdr(skb); 1572eb1d1641SHerbert Xu 1573eb1d1641SHerbert Xu if (iph->ihl < 5 || iph->version != 4) 1574eb1d1641SHerbert Xu return -EINVAL; 1575eb1d1641SHerbert Xu 1576eb1d1641SHerbert Xu if (!pskb_may_pull(skb, ip_hdrlen(skb))) 1577eb1d1641SHerbert Xu return -EINVAL; 1578eb1d1641SHerbert Xu 1579eb1d1641SHerbert Xu iph = ip_hdr(skb); 1580eb1d1641SHerbert Xu 1581eb1d1641SHerbert Xu if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl))) 1582eb1d1641SHerbert Xu return -EINVAL; 1583eb1d1641SHerbert Xu 1584bd4265feSHerbert Xu if (iph->protocol != IPPROTO_IGMP) { 1585bf5e4dd6SAmerigo Wang if (!ipv4_is_local_multicast(iph->daddr)) 1586bd4265feSHerbert Xu BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 1587eb1d1641SHerbert Xu return 0; 1588bd4265feSHerbert Xu } 1589eb1d1641SHerbert Xu 1590eb1d1641SHerbert Xu len = ntohs(iph->tot_len); 1591eb1d1641SHerbert Xu if (skb->len < len || len < ip_hdrlen(skb)) 1592eb1d1641SHerbert Xu return -EINVAL; 1593eb1d1641SHerbert Xu 1594eb1d1641SHerbert Xu if (skb->len > len) { 1595eb1d1641SHerbert Xu skb2 = skb_clone(skb, GFP_ATOMIC); 1596eb1d1641SHerbert Xu if (!skb2) 1597eb1d1641SHerbert Xu return -ENOMEM; 1598eb1d1641SHerbert Xu 1599eb1d1641SHerbert Xu err = pskb_trim_rcsum(skb2, len); 1600eb1d1641SHerbert Xu if (err) 16018440853bSYOSHIFUJI Hideaki / 吉藤英明 goto err_out; 1602eb1d1641SHerbert Xu } 1603eb1d1641SHerbert Xu 1604eb1d1641SHerbert Xu len -= ip_hdrlen(skb2); 1605eb1d1641SHerbert Xu offset = skb_network_offset(skb2) + ip_hdrlen(skb2); 1606eb1d1641SHerbert Xu __skb_pull(skb2, offset); 1607eb1d1641SHerbert Xu skb_reset_transport_header(skb2); 1608eb1d1641SHerbert Xu 1609eb1d1641SHerbert Xu err = -EINVAL; 1610eb1d1641SHerbert Xu if (!pskb_may_pull(skb2, sizeof(*ih))) 1611eb1d1641SHerbert Xu goto out; 1612eb1d1641SHerbert Xu 1613eb1d1641SHerbert Xu switch (skb2->ip_summed) { 1614eb1d1641SHerbert Xu case CHECKSUM_COMPLETE: 1615eb1d1641SHerbert Xu if (!csum_fold(skb2->csum)) 1616eb1d1641SHerbert Xu break; 1617eb1d1641SHerbert Xu /* fall through */ 1618eb1d1641SHerbert Xu case CHECKSUM_NONE: 1619eb1d1641SHerbert Xu skb2->csum = 0; 1620eb1d1641SHerbert Xu if (skb_checksum_complete(skb2)) 16218440853bSYOSHIFUJI Hideaki / 吉藤英明 goto out; 1622eb1d1641SHerbert Xu } 1623eb1d1641SHerbert Xu 1624eb1d1641SHerbert Xu err = 0; 1625eb1d1641SHerbert Xu 1626eb1d1641SHerbert Xu BR_INPUT_SKB_CB(skb)->igmp = 1; 1627eb1d1641SHerbert Xu ih = igmp_hdr(skb2); 1628eb1d1641SHerbert Xu 1629eb1d1641SHerbert Xu switch (ih->type) { 1630eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_REPORT: 1631eb1d1641SHerbert Xu case IGMPV2_HOST_MEMBERSHIP_REPORT: 163262b2bcb4SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 1633b0e9a30dSVlad Yasevich err = br_ip4_multicast_add_group(br, port, ih->group, vid); 1634eb1d1641SHerbert Xu break; 1635eb1d1641SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 163606499098SVlad Yasevich err = br_ip4_multicast_igmp3_report(br, port, skb2, vid); 1637eb1d1641SHerbert Xu break; 1638eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_QUERY: 163906499098SVlad Yasevich err = br_ip4_multicast_query(br, port, skb2, vid); 1640eb1d1641SHerbert Xu break; 1641eb1d1641SHerbert Xu case IGMP_HOST_LEAVE_MESSAGE: 1642b0e9a30dSVlad Yasevich br_ip4_multicast_leave_group(br, port, ih->group, vid); 1643eb1d1641SHerbert Xu break; 1644eb1d1641SHerbert Xu } 1645eb1d1641SHerbert Xu 1646eb1d1641SHerbert Xu out: 1647eb1d1641SHerbert Xu __skb_push(skb2, offset); 16488440853bSYOSHIFUJI Hideaki / 吉藤英明 err_out: 1649eb1d1641SHerbert Xu if (skb2 != skb) 1650eb1d1641SHerbert Xu kfree_skb(skb2); 1651eb1d1641SHerbert Xu return err; 1652eb1d1641SHerbert Xu } 1653eb1d1641SHerbert Xu 1654dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 165508b202b6SYOSHIFUJI Hideaki static int br_multicast_ipv6_rcv(struct net_bridge *br, 165608b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 165706499098SVlad Yasevich struct sk_buff *skb, 165806499098SVlad Yasevich u16 vid) 165908b202b6SYOSHIFUJI Hideaki { 16609d89081dSTomas Winkler struct sk_buff *skb2; 1661b71d1d42SEric Dumazet const struct ipv6hdr *ip6h; 166222df1331SEric Dumazet u8 icmp6_type; 166308b202b6SYOSHIFUJI Hideaki u8 nexthdr; 166475f2811cSJesse Gross __be16 frag_off; 166595c96174SEric Dumazet unsigned int len; 1666bb7a0bd6SKulikov Vasiliy int offset; 166708b202b6SYOSHIFUJI Hideaki int err; 166808b202b6SYOSHIFUJI Hideaki 166908b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*ip6h))) 167008b202b6SYOSHIFUJI Hideaki return -EINVAL; 167108b202b6SYOSHIFUJI Hideaki 167208b202b6SYOSHIFUJI Hideaki ip6h = ipv6_hdr(skb); 167308b202b6SYOSHIFUJI Hideaki 167408b202b6SYOSHIFUJI Hideaki /* 167508b202b6SYOSHIFUJI Hideaki * We're interested in MLD messages only. 167608b202b6SYOSHIFUJI Hideaki * - Version is 6 167708b202b6SYOSHIFUJI Hideaki * - MLD has always Router Alert hop-by-hop option 167808b202b6SYOSHIFUJI Hideaki * - But we do not support jumbrograms. 167908b202b6SYOSHIFUJI Hideaki */ 16808fad9c39SLinus Lüssing if (ip6h->version != 6) 16818fad9c39SLinus Lüssing return 0; 16828fad9c39SLinus Lüssing 16838fad9c39SLinus Lüssing /* Prevent flooding this packet if there is no listener present */ 16843c3769e6SLinus Lüssing if (!ipv6_addr_is_ll_all_nodes(&ip6h->daddr)) 16858fad9c39SLinus Lüssing BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 16868fad9c39SLinus Lüssing 16878fad9c39SLinus Lüssing if (ip6h->nexthdr != IPPROTO_HOPOPTS || 168808b202b6SYOSHIFUJI Hideaki ip6h->payload_len == 0) 168908b202b6SYOSHIFUJI Hideaki return 0; 169008b202b6SYOSHIFUJI Hideaki 1691ff9a57a6SLinus Lüssing len = ntohs(ip6h->payload_len) + sizeof(*ip6h); 169208b202b6SYOSHIFUJI Hideaki if (skb->len < len) 169308b202b6SYOSHIFUJI Hideaki return -EINVAL; 169408b202b6SYOSHIFUJI Hideaki 169508b202b6SYOSHIFUJI Hideaki nexthdr = ip6h->nexthdr; 169675f2811cSJesse Gross offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr, &frag_off); 169708b202b6SYOSHIFUJI Hideaki 169808b202b6SYOSHIFUJI Hideaki if (offset < 0 || nexthdr != IPPROTO_ICMPV6) 169908b202b6SYOSHIFUJI Hideaki return 0; 170008b202b6SYOSHIFUJI Hideaki 170108b202b6SYOSHIFUJI Hideaki /* Okay, we found ICMPv6 header */ 170208b202b6SYOSHIFUJI Hideaki skb2 = skb_clone(skb, GFP_ATOMIC); 170308b202b6SYOSHIFUJI Hideaki if (!skb2) 170408b202b6SYOSHIFUJI Hideaki return -ENOMEM; 170508b202b6SYOSHIFUJI Hideaki 17069d89081dSTomas Winkler err = -EINVAL; 17079d89081dSTomas Winkler if (!pskb_may_pull(skb2, offset + sizeof(struct icmp6hdr))) 17089d89081dSTomas Winkler goto out; 17099d89081dSTomas Winkler 171008b202b6SYOSHIFUJI Hideaki len -= offset - skb_network_offset(skb2); 171108b202b6SYOSHIFUJI Hideaki 171208b202b6SYOSHIFUJI Hideaki __skb_pull(skb2, offset); 171308b202b6SYOSHIFUJI Hideaki skb_reset_transport_header(skb2); 1714fa2da8cdSstephen hemminger skb_postpull_rcsum(skb2, skb_network_header(skb2), 1715fa2da8cdSstephen hemminger skb_network_header_len(skb2)); 171608b202b6SYOSHIFUJI Hideaki 171722df1331SEric Dumazet icmp6_type = icmp6_hdr(skb2)->icmp6_type; 171808b202b6SYOSHIFUJI Hideaki 171922df1331SEric Dumazet switch (icmp6_type) { 172008b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 172108b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 172208b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 172308b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 172408b202b6SYOSHIFUJI Hideaki break; 172508b202b6SYOSHIFUJI Hideaki default: 172608b202b6SYOSHIFUJI Hideaki err = 0; 172708b202b6SYOSHIFUJI Hideaki goto out; 172808b202b6SYOSHIFUJI Hideaki } 172908b202b6SYOSHIFUJI Hideaki 173008b202b6SYOSHIFUJI Hideaki /* Okay, we found MLD message. Check further. */ 173108b202b6SYOSHIFUJI Hideaki if (skb2->len > len) { 173208b202b6SYOSHIFUJI Hideaki err = pskb_trim_rcsum(skb2, len); 173308b202b6SYOSHIFUJI Hideaki if (err) 173408b202b6SYOSHIFUJI Hideaki goto out; 17354b275d7eSYan, Zheng err = -EINVAL; 173608b202b6SYOSHIFUJI Hideaki } 173708b202b6SYOSHIFUJI Hideaki 17384b275d7eSYan, Zheng ip6h = ipv6_hdr(skb2); 17394b275d7eSYan, Zheng 174008b202b6SYOSHIFUJI Hideaki switch (skb2->ip_summed) { 174108b202b6SYOSHIFUJI Hideaki case CHECKSUM_COMPLETE: 17424b275d7eSYan, Zheng if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, skb2->len, 17434b275d7eSYan, Zheng IPPROTO_ICMPV6, skb2->csum)) 174408b202b6SYOSHIFUJI Hideaki break; 174508b202b6SYOSHIFUJI Hideaki /*FALLTHROUGH*/ 174608b202b6SYOSHIFUJI Hideaki case CHECKSUM_NONE: 17474b275d7eSYan, Zheng skb2->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr, 17484b275d7eSYan, Zheng &ip6h->daddr, 17494b275d7eSYan, Zheng skb2->len, 17504b275d7eSYan, Zheng IPPROTO_ICMPV6, 0)); 17514b275d7eSYan, Zheng if (__skb_checksum_complete(skb2)) 175208b202b6SYOSHIFUJI Hideaki goto out; 175308b202b6SYOSHIFUJI Hideaki } 175408b202b6SYOSHIFUJI Hideaki 175508b202b6SYOSHIFUJI Hideaki err = 0; 175608b202b6SYOSHIFUJI Hideaki 175708b202b6SYOSHIFUJI Hideaki BR_INPUT_SKB_CB(skb)->igmp = 1; 175808b202b6SYOSHIFUJI Hideaki 175922df1331SEric Dumazet switch (icmp6_type) { 176008b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 176108b202b6SYOSHIFUJI Hideaki { 17629d89081dSTomas Winkler struct mld_msg *mld; 17639d89081dSTomas Winkler if (!pskb_may_pull(skb2, sizeof(*mld))) { 17649d89081dSTomas Winkler err = -EINVAL; 17659d89081dSTomas Winkler goto out; 17669d89081dSTomas Winkler } 17679d89081dSTomas Winkler mld = (struct mld_msg *)skb_transport_header(skb2); 1768fc2af6c7SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 1769b0e9a30dSVlad Yasevich err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid); 177008b202b6SYOSHIFUJI Hideaki break; 177108b202b6SYOSHIFUJI Hideaki } 177208b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 177306499098SVlad Yasevich err = br_ip6_multicast_mld2_report(br, port, skb2, vid); 177408b202b6SYOSHIFUJI Hideaki break; 177508b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 177606499098SVlad Yasevich err = br_ip6_multicast_query(br, port, skb2, vid); 177708b202b6SYOSHIFUJI Hideaki break; 177808b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 177908b202b6SYOSHIFUJI Hideaki { 17809d89081dSTomas Winkler struct mld_msg *mld; 17819d89081dSTomas Winkler if (!pskb_may_pull(skb2, sizeof(*mld))) { 17829d89081dSTomas Winkler err = -EINVAL; 17839d89081dSTomas Winkler goto out; 17849d89081dSTomas Winkler } 17859d89081dSTomas Winkler mld = (struct mld_msg *)skb_transport_header(skb2); 1786b0e9a30dSVlad Yasevich br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid); 178708b202b6SYOSHIFUJI Hideaki } 178808b202b6SYOSHIFUJI Hideaki } 178908b202b6SYOSHIFUJI Hideaki 179008b202b6SYOSHIFUJI Hideaki out: 179108b202b6SYOSHIFUJI Hideaki kfree_skb(skb2); 179208b202b6SYOSHIFUJI Hideaki return err; 179308b202b6SYOSHIFUJI Hideaki } 179408b202b6SYOSHIFUJI Hideaki #endif 179508b202b6SYOSHIFUJI Hideaki 1796eb1d1641SHerbert Xu int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port, 179706499098SVlad Yasevich struct sk_buff *skb, u16 vid) 1798eb1d1641SHerbert Xu { 17991fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->igmp = 0; 18001fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->mrouters_only = 0; 18011fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 1802eb1d1641SHerbert Xu if (br->multicast_disabled) 1803eb1d1641SHerbert Xu return 0; 1804eb1d1641SHerbert Xu 1805eb1d1641SHerbert Xu switch (skb->protocol) { 1806eb1d1641SHerbert Xu case htons(ETH_P_IP): 180706499098SVlad Yasevich return br_multicast_ipv4_rcv(br, port, skb, vid); 1808dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 180908b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 181006499098SVlad Yasevich return br_multicast_ipv6_rcv(br, port, skb, vid); 181108b202b6SYOSHIFUJI Hideaki #endif 1812eb1d1641SHerbert Xu } 1813eb1d1641SHerbert Xu 1814eb1d1641SHerbert Xu return 0; 1815eb1d1641SHerbert Xu } 1816eb1d1641SHerbert Xu 1817cc0fdd80SLinus Lüssing static void br_multicast_query_expired(struct net_bridge *br, 18182cd41431SLinus Lüssing struct bridge_mcast_own_query *query, 18192cd41431SLinus Lüssing struct bridge_mcast_querier *querier) 1820cc0fdd80SLinus Lüssing { 1821cc0fdd80SLinus Lüssing spin_lock(&br->multicast_lock); 1822cc0fdd80SLinus Lüssing if (query->startup_sent < br->multicast_startup_query_count) 1823cc0fdd80SLinus Lüssing query->startup_sent++; 1824cc0fdd80SLinus Lüssing 1825*71d9f614SEric Dumazet RCU_INIT_POINTER(querier->port, NULL); 1826cc0fdd80SLinus Lüssing br_multicast_send_query(br, NULL, query); 1827cc0fdd80SLinus Lüssing spin_unlock(&br->multicast_lock); 1828cc0fdd80SLinus Lüssing } 1829cc0fdd80SLinus Lüssing 1830cc0fdd80SLinus Lüssing static void br_ip4_multicast_query_expired(unsigned long data) 1831eb1d1641SHerbert Xu { 1832eb1d1641SHerbert Xu struct net_bridge *br = (void *)data; 1833eb1d1641SHerbert Xu 18342cd41431SLinus Lüssing br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier); 1835eb1d1641SHerbert Xu } 1836eb1d1641SHerbert Xu 1837cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1838cc0fdd80SLinus Lüssing static void br_ip6_multicast_query_expired(unsigned long data) 1839cc0fdd80SLinus Lüssing { 1840cc0fdd80SLinus Lüssing struct net_bridge *br = (void *)data; 1841cc0fdd80SLinus Lüssing 18422cd41431SLinus Lüssing br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier); 1843cc0fdd80SLinus Lüssing } 1844cc0fdd80SLinus Lüssing #endif 1845cc0fdd80SLinus Lüssing 1846eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br) 1847eb1d1641SHerbert Xu { 1848eb1d1641SHerbert Xu br->hash_elasticity = 4; 1849eb1d1641SHerbert Xu br->hash_max = 512; 1850eb1d1641SHerbert Xu 1851eb1d1641SHerbert Xu br->multicast_router = 1; 1852c5c23260SHerbert Xu br->multicast_querier = 0; 18531c8ad5bfSCong Wang br->multicast_query_use_ifaddr = 0; 1854eb1d1641SHerbert Xu br->multicast_last_member_count = 2; 1855eb1d1641SHerbert Xu br->multicast_startup_query_count = 2; 1856eb1d1641SHerbert Xu 1857eb1d1641SHerbert Xu br->multicast_last_member_interval = HZ; 1858eb1d1641SHerbert Xu br->multicast_query_response_interval = 10 * HZ; 1859eb1d1641SHerbert Xu br->multicast_startup_query_interval = 125 * HZ / 4; 1860eb1d1641SHerbert Xu br->multicast_query_interval = 125 * HZ; 1861eb1d1641SHerbert Xu br->multicast_querier_interval = 255 * HZ; 1862eb1d1641SHerbert Xu br->multicast_membership_interval = 260 * HZ; 1863eb1d1641SHerbert Xu 186490010b36SLinus Lüssing br->ip4_other_query.delay_time = 0; 18652cd41431SLinus Lüssing br->ip4_querier.port = NULL; 1866cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 186790010b36SLinus Lüssing br->ip6_other_query.delay_time = 0; 18682cd41431SLinus Lüssing br->ip6_querier.port = NULL; 1869cc0fdd80SLinus Lüssing #endif 1870b00589afSLinus Lüssing 1871eb1d1641SHerbert Xu spin_lock_init(&br->multicast_lock); 1872eb1d1641SHerbert Xu setup_timer(&br->multicast_router_timer, 1873eb1d1641SHerbert Xu br_multicast_local_router_expired, 0); 187490010b36SLinus Lüssing setup_timer(&br->ip4_other_query.timer, 187590010b36SLinus Lüssing br_ip4_multicast_querier_expired, (unsigned long)br); 187690010b36SLinus Lüssing setup_timer(&br->ip4_own_query.timer, br_ip4_multicast_query_expired, 1877cc0fdd80SLinus Lüssing (unsigned long)br); 1878cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 187990010b36SLinus Lüssing setup_timer(&br->ip6_other_query.timer, 188090010b36SLinus Lüssing br_ip6_multicast_querier_expired, (unsigned long)br); 188190010b36SLinus Lüssing setup_timer(&br->ip6_own_query.timer, br_ip6_multicast_query_expired, 1882cc0fdd80SLinus Lüssing (unsigned long)br); 1883cc0fdd80SLinus Lüssing #endif 1884eb1d1641SHerbert Xu } 1885eb1d1641SHerbert Xu 1886cc0fdd80SLinus Lüssing static void __br_multicast_open(struct net_bridge *br, 188790010b36SLinus Lüssing struct bridge_mcast_own_query *query) 1888eb1d1641SHerbert Xu { 1889cc0fdd80SLinus Lüssing query->startup_sent = 0; 1890eb1d1641SHerbert Xu 1891eb1d1641SHerbert Xu if (br->multicast_disabled) 1892eb1d1641SHerbert Xu return; 1893eb1d1641SHerbert Xu 1894cc0fdd80SLinus Lüssing mod_timer(&query->timer, jiffies); 1895cc0fdd80SLinus Lüssing } 1896cc0fdd80SLinus Lüssing 1897cc0fdd80SLinus Lüssing void br_multicast_open(struct net_bridge *br) 1898cc0fdd80SLinus Lüssing { 189990010b36SLinus Lüssing __br_multicast_open(br, &br->ip4_own_query); 1900cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 190190010b36SLinus Lüssing __br_multicast_open(br, &br->ip6_own_query); 1902cc0fdd80SLinus Lüssing #endif 1903eb1d1641SHerbert Xu } 1904eb1d1641SHerbert Xu 1905eb1d1641SHerbert Xu void br_multicast_stop(struct net_bridge *br) 1906eb1d1641SHerbert Xu { 1907eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 1908eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1909b67bfe0dSSasha Levin struct hlist_node *n; 1910eb1d1641SHerbert Xu u32 ver; 1911eb1d1641SHerbert Xu int i; 1912eb1d1641SHerbert Xu 1913eb1d1641SHerbert Xu del_timer_sync(&br->multicast_router_timer); 191490010b36SLinus Lüssing del_timer_sync(&br->ip4_other_query.timer); 191590010b36SLinus Lüssing del_timer_sync(&br->ip4_own_query.timer); 1916cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 191790010b36SLinus Lüssing del_timer_sync(&br->ip6_other_query.timer); 191890010b36SLinus Lüssing del_timer_sync(&br->ip6_own_query.timer); 1919cc0fdd80SLinus Lüssing #endif 1920eb1d1641SHerbert Xu 1921eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1922e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1923eb1d1641SHerbert Xu if (!mdb) 1924eb1d1641SHerbert Xu goto out; 1925eb1d1641SHerbert Xu 1926eb1d1641SHerbert Xu br->mdb = NULL; 1927eb1d1641SHerbert Xu 1928eb1d1641SHerbert Xu ver = mdb->ver; 1929eb1d1641SHerbert Xu for (i = 0; i < mdb->max; i++) { 1930b67bfe0dSSasha Levin hlist_for_each_entry_safe(mp, n, &mdb->mhash[i], 1931eb1d1641SHerbert Xu hlist[ver]) { 1932eb1d1641SHerbert Xu del_timer(&mp->timer); 1933eb1d1641SHerbert Xu call_rcu_bh(&mp->rcu, br_multicast_free_group); 1934eb1d1641SHerbert Xu } 1935eb1d1641SHerbert Xu } 1936eb1d1641SHerbert Xu 1937eb1d1641SHerbert Xu if (mdb->old) { 1938eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 193910cc2b50SHerbert Xu rcu_barrier_bh(); 1940eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1941eb1d1641SHerbert Xu WARN_ON(mdb->old); 1942eb1d1641SHerbert Xu } 1943eb1d1641SHerbert Xu 1944eb1d1641SHerbert Xu mdb->old = mdb; 1945eb1d1641SHerbert Xu call_rcu_bh(&mdb->rcu, br_mdb_free); 1946eb1d1641SHerbert Xu 1947eb1d1641SHerbert Xu out: 1948eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 1949eb1d1641SHerbert Xu } 19500909e117SHerbert Xu 19510909e117SHerbert Xu int br_multicast_set_router(struct net_bridge *br, unsigned long val) 19520909e117SHerbert Xu { 19530909e117SHerbert Xu int err = -ENOENT; 19540909e117SHerbert Xu 19550909e117SHerbert Xu spin_lock_bh(&br->multicast_lock); 19560909e117SHerbert Xu if (!netif_running(br->dev)) 19570909e117SHerbert Xu goto unlock; 19580909e117SHerbert Xu 19590909e117SHerbert Xu switch (val) { 19600909e117SHerbert Xu case 0: 19610909e117SHerbert Xu case 2: 19620909e117SHerbert Xu del_timer(&br->multicast_router_timer); 19630909e117SHerbert Xu /* fall through */ 19640909e117SHerbert Xu case 1: 19650909e117SHerbert Xu br->multicast_router = val; 19660909e117SHerbert Xu err = 0; 19670909e117SHerbert Xu break; 19680909e117SHerbert Xu 19690909e117SHerbert Xu default: 19700909e117SHerbert Xu err = -EINVAL; 19710909e117SHerbert Xu break; 19720909e117SHerbert Xu } 19730909e117SHerbert Xu 19740909e117SHerbert Xu unlock: 19750909e117SHerbert Xu spin_unlock_bh(&br->multicast_lock); 19760909e117SHerbert Xu 19770909e117SHerbert Xu return err; 19780909e117SHerbert Xu } 19790909e117SHerbert Xu 19800909e117SHerbert Xu int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val) 19810909e117SHerbert Xu { 19820909e117SHerbert Xu struct net_bridge *br = p->br; 19830909e117SHerbert Xu int err = -ENOENT; 19840909e117SHerbert Xu 19850909e117SHerbert Xu spin_lock(&br->multicast_lock); 19860909e117SHerbert Xu if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED) 19870909e117SHerbert Xu goto unlock; 19880909e117SHerbert Xu 19890909e117SHerbert Xu switch (val) { 19900909e117SHerbert Xu case 0: 19910909e117SHerbert Xu case 1: 19920909e117SHerbert Xu case 2: 19930909e117SHerbert Xu p->multicast_router = val; 19940909e117SHerbert Xu err = 0; 19950909e117SHerbert Xu 19960909e117SHerbert Xu if (val < 2 && !hlist_unhashed(&p->rlist)) 19970909e117SHerbert Xu hlist_del_init_rcu(&p->rlist); 19980909e117SHerbert Xu 19990909e117SHerbert Xu if (val == 1) 20000909e117SHerbert Xu break; 20010909e117SHerbert Xu 20020909e117SHerbert Xu del_timer(&p->multicast_router_timer); 20030909e117SHerbert Xu 20040909e117SHerbert Xu if (val == 0) 20050909e117SHerbert Xu break; 20060909e117SHerbert Xu 20070909e117SHerbert Xu br_multicast_add_router(br, p); 20080909e117SHerbert Xu break; 20090909e117SHerbert Xu 20100909e117SHerbert Xu default: 20110909e117SHerbert Xu err = -EINVAL; 20120909e117SHerbert Xu break; 20130909e117SHerbert Xu } 20140909e117SHerbert Xu 20150909e117SHerbert Xu unlock: 20160909e117SHerbert Xu spin_unlock(&br->multicast_lock); 20170909e117SHerbert Xu 20180909e117SHerbert Xu return err; 20190909e117SHerbert Xu } 2020561f1103SHerbert Xu 2021cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br, 202290010b36SLinus Lüssing struct bridge_mcast_own_query *query) 2023561f1103SHerbert Xu { 2024561f1103SHerbert Xu struct net_bridge_port *port; 202574857216SHerbert Xu 2026cc0fdd80SLinus Lüssing __br_multicast_open(br, query); 202774857216SHerbert Xu 202874857216SHerbert Xu list_for_each_entry(port, &br->port_list, list) { 202974857216SHerbert Xu if (port->state == BR_STATE_DISABLED || 203074857216SHerbert Xu port->state == BR_STATE_BLOCKING) 203174857216SHerbert Xu continue; 203274857216SHerbert Xu 203390010b36SLinus Lüssing if (query == &br->ip4_own_query) 203490010b36SLinus Lüssing br_multicast_enable(&port->ip4_own_query); 2035cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 2036cc0fdd80SLinus Lüssing else 203790010b36SLinus Lüssing br_multicast_enable(&port->ip6_own_query); 2038cc0fdd80SLinus Lüssing #endif 203974857216SHerbert Xu } 204074857216SHerbert Xu } 204174857216SHerbert Xu 204274857216SHerbert Xu int br_multicast_toggle(struct net_bridge *br, unsigned long val) 204374857216SHerbert Xu { 20443a7fda06SHerbert Xu int err = 0; 2045e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 2046561f1103SHerbert Xu 2047ef5e0d82SAndrey Vagin spin_lock_bh(&br->multicast_lock); 2048561f1103SHerbert Xu if (br->multicast_disabled == !val) 2049561f1103SHerbert Xu goto unlock; 2050561f1103SHerbert Xu 2051561f1103SHerbert Xu br->multicast_disabled = !val; 2052561f1103SHerbert Xu if (br->multicast_disabled) 2053561f1103SHerbert Xu goto unlock; 2054561f1103SHerbert Xu 20553a7fda06SHerbert Xu if (!netif_running(br->dev)) 20563a7fda06SHerbert Xu goto unlock; 20573a7fda06SHerbert Xu 2058e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 2059e8051688SEric Dumazet if (mdb) { 2060e8051688SEric Dumazet if (mdb->old) { 2061561f1103SHerbert Xu err = -EEXIST; 2062561f1103SHerbert Xu rollback: 2063561f1103SHerbert Xu br->multicast_disabled = !!val; 2064561f1103SHerbert Xu goto unlock; 2065561f1103SHerbert Xu } 2066561f1103SHerbert Xu 2067e8051688SEric Dumazet err = br_mdb_rehash(&br->mdb, mdb->max, 2068561f1103SHerbert Xu br->hash_elasticity); 2069561f1103SHerbert Xu if (err) 2070561f1103SHerbert Xu goto rollback; 2071561f1103SHerbert Xu } 2072561f1103SHerbert Xu 207390010b36SLinus Lüssing br_multicast_start_querier(br, &br->ip4_own_query); 2074cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 207590010b36SLinus Lüssing br_multicast_start_querier(br, &br->ip6_own_query); 2076cc0fdd80SLinus Lüssing #endif 2077561f1103SHerbert Xu 2078561f1103SHerbert Xu unlock: 2079ef5e0d82SAndrey Vagin spin_unlock_bh(&br->multicast_lock); 2080561f1103SHerbert Xu 2081561f1103SHerbert Xu return err; 2082561f1103SHerbert Xu } 2083b195167fSHerbert Xu 2084c5c23260SHerbert Xu int br_multicast_set_querier(struct net_bridge *br, unsigned long val) 2085c5c23260SHerbert Xu { 2086b00589afSLinus Lüssing unsigned long max_delay; 2087b00589afSLinus Lüssing 2088c5c23260SHerbert Xu val = !!val; 2089c5c23260SHerbert Xu 2090c5c23260SHerbert Xu spin_lock_bh(&br->multicast_lock); 2091c5c23260SHerbert Xu if (br->multicast_querier == val) 2092c5c23260SHerbert Xu goto unlock; 2093c5c23260SHerbert Xu 2094c5c23260SHerbert Xu br->multicast_querier = val; 2095b00589afSLinus Lüssing if (!val) 2096b00589afSLinus Lüssing goto unlock; 2097b00589afSLinus Lüssing 2098b00589afSLinus Lüssing max_delay = br->multicast_query_response_interval; 2099b00589afSLinus Lüssing 210090010b36SLinus Lüssing if (!timer_pending(&br->ip4_other_query.timer)) 210190010b36SLinus Lüssing br->ip4_other_query.delay_time = jiffies + max_delay; 2102cc0fdd80SLinus Lüssing 210390010b36SLinus Lüssing br_multicast_start_querier(br, &br->ip4_own_query); 2104cc0fdd80SLinus Lüssing 2105cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 210690010b36SLinus Lüssing if (!timer_pending(&br->ip6_other_query.timer)) 210790010b36SLinus Lüssing br->ip6_other_query.delay_time = jiffies + max_delay; 2108cc0fdd80SLinus Lüssing 210990010b36SLinus Lüssing br_multicast_start_querier(br, &br->ip6_own_query); 2110cc0fdd80SLinus Lüssing #endif 2111c5c23260SHerbert Xu 2112c5c23260SHerbert Xu unlock: 2113c5c23260SHerbert Xu spin_unlock_bh(&br->multicast_lock); 2114c5c23260SHerbert Xu 2115c5c23260SHerbert Xu return 0; 2116c5c23260SHerbert Xu } 2117c5c23260SHerbert Xu 2118b195167fSHerbert Xu int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val) 2119b195167fSHerbert Xu { 2120b195167fSHerbert Xu int err = -ENOENT; 2121b195167fSHerbert Xu u32 old; 2122e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 2123b195167fSHerbert Xu 2124fe0d692bSCurt Brune spin_lock_bh(&br->multicast_lock); 2125b195167fSHerbert Xu if (!netif_running(br->dev)) 2126b195167fSHerbert Xu goto unlock; 2127b195167fSHerbert Xu 2128b195167fSHerbert Xu err = -EINVAL; 2129b195167fSHerbert Xu if (!is_power_of_2(val)) 2130b195167fSHerbert Xu goto unlock; 2131e8051688SEric Dumazet 2132e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 2133e8051688SEric Dumazet if (mdb && val < mdb->size) 2134b195167fSHerbert Xu goto unlock; 2135b195167fSHerbert Xu 2136b195167fSHerbert Xu err = 0; 2137b195167fSHerbert Xu 2138b195167fSHerbert Xu old = br->hash_max; 2139b195167fSHerbert Xu br->hash_max = val; 2140b195167fSHerbert Xu 2141e8051688SEric Dumazet if (mdb) { 2142e8051688SEric Dumazet if (mdb->old) { 2143b195167fSHerbert Xu err = -EEXIST; 2144b195167fSHerbert Xu rollback: 2145b195167fSHerbert Xu br->hash_max = old; 2146b195167fSHerbert Xu goto unlock; 2147b195167fSHerbert Xu } 2148b195167fSHerbert Xu 2149b195167fSHerbert Xu err = br_mdb_rehash(&br->mdb, br->hash_max, 2150b195167fSHerbert Xu br->hash_elasticity); 2151b195167fSHerbert Xu if (err) 2152b195167fSHerbert Xu goto rollback; 2153b195167fSHerbert Xu } 2154b195167fSHerbert Xu 2155b195167fSHerbert Xu unlock: 2156fe0d692bSCurt Brune spin_unlock_bh(&br->multicast_lock); 2157b195167fSHerbert Xu 2158b195167fSHerbert Xu return err; 2159b195167fSHerbert Xu } 216007f8ac4aSLinus Lüssing 216107f8ac4aSLinus Lüssing /** 216207f8ac4aSLinus Lüssing * br_multicast_list_adjacent - Returns snooped multicast addresses 216307f8ac4aSLinus Lüssing * @dev: The bridge port adjacent to which to retrieve addresses 216407f8ac4aSLinus Lüssing * @br_ip_list: The list to store found, snooped multicast IP addresses in 216507f8ac4aSLinus Lüssing * 216607f8ac4aSLinus Lüssing * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast 216707f8ac4aSLinus Lüssing * snooping feature on all bridge ports of dev's bridge device, excluding 216807f8ac4aSLinus Lüssing * the addresses from dev itself. 216907f8ac4aSLinus Lüssing * 217007f8ac4aSLinus Lüssing * Returns the number of items added to br_ip_list. 217107f8ac4aSLinus Lüssing * 217207f8ac4aSLinus Lüssing * Notes: 217307f8ac4aSLinus Lüssing * - br_ip_list needs to be initialized by caller 217407f8ac4aSLinus Lüssing * - br_ip_list might contain duplicates in the end 217507f8ac4aSLinus Lüssing * (needs to be taken care of by caller) 217607f8ac4aSLinus Lüssing * - br_ip_list needs to be freed by caller 217707f8ac4aSLinus Lüssing */ 217807f8ac4aSLinus Lüssing int br_multicast_list_adjacent(struct net_device *dev, 217907f8ac4aSLinus Lüssing struct list_head *br_ip_list) 218007f8ac4aSLinus Lüssing { 218107f8ac4aSLinus Lüssing struct net_bridge *br; 218207f8ac4aSLinus Lüssing struct net_bridge_port *port; 218307f8ac4aSLinus Lüssing struct net_bridge_port_group *group; 218407f8ac4aSLinus Lüssing struct br_ip_list *entry; 218507f8ac4aSLinus Lüssing int count = 0; 218607f8ac4aSLinus Lüssing 218707f8ac4aSLinus Lüssing rcu_read_lock(); 218807f8ac4aSLinus Lüssing if (!br_ip_list || !br_port_exists(dev)) 218907f8ac4aSLinus Lüssing goto unlock; 219007f8ac4aSLinus Lüssing 219107f8ac4aSLinus Lüssing port = br_port_get_rcu(dev); 219207f8ac4aSLinus Lüssing if (!port || !port->br) 219307f8ac4aSLinus Lüssing goto unlock; 219407f8ac4aSLinus Lüssing 219507f8ac4aSLinus Lüssing br = port->br; 219607f8ac4aSLinus Lüssing 219707f8ac4aSLinus Lüssing list_for_each_entry_rcu(port, &br->port_list, list) { 219807f8ac4aSLinus Lüssing if (!port->dev || port->dev == dev) 219907f8ac4aSLinus Lüssing continue; 220007f8ac4aSLinus Lüssing 220107f8ac4aSLinus Lüssing hlist_for_each_entry_rcu(group, &port->mglist, mglist) { 220207f8ac4aSLinus Lüssing entry = kmalloc(sizeof(*entry), GFP_ATOMIC); 220307f8ac4aSLinus Lüssing if (!entry) 220407f8ac4aSLinus Lüssing goto unlock; 220507f8ac4aSLinus Lüssing 220607f8ac4aSLinus Lüssing entry->addr = group->addr; 220707f8ac4aSLinus Lüssing list_add(&entry->list, br_ip_list); 220807f8ac4aSLinus Lüssing count++; 220907f8ac4aSLinus Lüssing } 221007f8ac4aSLinus Lüssing } 221107f8ac4aSLinus Lüssing 221207f8ac4aSLinus Lüssing unlock: 221307f8ac4aSLinus Lüssing rcu_read_unlock(); 221407f8ac4aSLinus Lüssing return count; 221507f8ac4aSLinus Lüssing } 221607f8ac4aSLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_list_adjacent); 22172cd41431SLinus Lüssing 22182cd41431SLinus Lüssing /** 2219c34963e2SLinus Lüssing * br_multicast_has_querier_anywhere - Checks for a querier on a bridge 2220c34963e2SLinus Lüssing * @dev: The bridge port providing the bridge on which to check for a querier 2221c34963e2SLinus Lüssing * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 2222c34963e2SLinus Lüssing * 2223c34963e2SLinus Lüssing * Checks whether the given interface has a bridge on top and if so returns 2224c34963e2SLinus Lüssing * true if a valid querier exists anywhere on the bridged link layer. 2225c34963e2SLinus Lüssing * Otherwise returns false. 2226c34963e2SLinus Lüssing */ 2227c34963e2SLinus Lüssing bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto) 2228c34963e2SLinus Lüssing { 2229c34963e2SLinus Lüssing struct net_bridge *br; 2230c34963e2SLinus Lüssing struct net_bridge_port *port; 2231c34963e2SLinus Lüssing struct ethhdr eth; 2232c34963e2SLinus Lüssing bool ret = false; 2233c34963e2SLinus Lüssing 2234c34963e2SLinus Lüssing rcu_read_lock(); 2235c34963e2SLinus Lüssing if (!br_port_exists(dev)) 2236c34963e2SLinus Lüssing goto unlock; 2237c34963e2SLinus Lüssing 2238c34963e2SLinus Lüssing port = br_port_get_rcu(dev); 2239c34963e2SLinus Lüssing if (!port || !port->br) 2240c34963e2SLinus Lüssing goto unlock; 2241c34963e2SLinus Lüssing 2242c34963e2SLinus Lüssing br = port->br; 2243c34963e2SLinus Lüssing 2244c34963e2SLinus Lüssing memset(ð, 0, sizeof(eth)); 2245c34963e2SLinus Lüssing eth.h_proto = htons(proto); 2246c34963e2SLinus Lüssing 2247c34963e2SLinus Lüssing ret = br_multicast_querier_exists(br, ð); 2248c34963e2SLinus Lüssing 2249c34963e2SLinus Lüssing unlock: 2250c34963e2SLinus Lüssing rcu_read_unlock(); 2251c34963e2SLinus Lüssing return ret; 2252c34963e2SLinus Lüssing } 2253c34963e2SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere); 2254c34963e2SLinus Lüssing 2255c34963e2SLinus Lüssing /** 22562cd41431SLinus Lüssing * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port 22572cd41431SLinus Lüssing * @dev: The bridge port adjacent to which to check for a querier 22582cd41431SLinus Lüssing * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 22592cd41431SLinus Lüssing * 22602cd41431SLinus Lüssing * Checks whether the given interface has a bridge on top and if so returns 22612cd41431SLinus Lüssing * true if a selected querier is behind one of the other ports of this 22622cd41431SLinus Lüssing * bridge. Otherwise returns false. 22632cd41431SLinus Lüssing */ 22642cd41431SLinus Lüssing bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto) 22652cd41431SLinus Lüssing { 22662cd41431SLinus Lüssing struct net_bridge *br; 22672cd41431SLinus Lüssing struct net_bridge_port *port; 22682cd41431SLinus Lüssing bool ret = false; 22692cd41431SLinus Lüssing 22702cd41431SLinus Lüssing rcu_read_lock(); 22712cd41431SLinus Lüssing if (!br_port_exists(dev)) 22722cd41431SLinus Lüssing goto unlock; 22732cd41431SLinus Lüssing 22742cd41431SLinus Lüssing port = br_port_get_rcu(dev); 22752cd41431SLinus Lüssing if (!port || !port->br) 22762cd41431SLinus Lüssing goto unlock; 22772cd41431SLinus Lüssing 22782cd41431SLinus Lüssing br = port->br; 22792cd41431SLinus Lüssing 22802cd41431SLinus Lüssing switch (proto) { 22812cd41431SLinus Lüssing case ETH_P_IP: 22822cd41431SLinus Lüssing if (!timer_pending(&br->ip4_other_query.timer) || 22832cd41431SLinus Lüssing rcu_dereference(br->ip4_querier.port) == port) 22842cd41431SLinus Lüssing goto unlock; 22852cd41431SLinus Lüssing break; 22863993c4e1SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 22872cd41431SLinus Lüssing case ETH_P_IPV6: 22882cd41431SLinus Lüssing if (!timer_pending(&br->ip6_other_query.timer) || 22892cd41431SLinus Lüssing rcu_dereference(br->ip6_querier.port) == port) 22902cd41431SLinus Lüssing goto unlock; 22912cd41431SLinus Lüssing break; 22923993c4e1SLinus Lüssing #endif 22932cd41431SLinus Lüssing default: 22942cd41431SLinus Lüssing goto unlock; 22952cd41431SLinus Lüssing } 22962cd41431SLinus Lüssing 22972cd41431SLinus Lüssing ret = true; 22982cd41431SLinus Lüssing unlock: 22992cd41431SLinus Lüssing rcu_read_unlock(); 23002cd41431SLinus Lüssing return ret; 23012cd41431SLinus Lüssing } 23022cd41431SLinus Lüssing EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent); 2303