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> 14eb1d1641SHerbert Xu #include <linux/if_ether.h> 15eb1d1641SHerbert Xu #include <linux/igmp.h> 16eb1d1641SHerbert Xu #include <linux/jhash.h> 17eb1d1641SHerbert Xu #include <linux/kernel.h> 18b195167fSHerbert Xu #include <linux/log2.h> 19eb1d1641SHerbert Xu #include <linux/netdevice.h> 20eb1d1641SHerbert Xu #include <linux/netfilter_bridge.h> 21eb1d1641SHerbert Xu #include <linux/random.h> 22eb1d1641SHerbert Xu #include <linux/rculist.h> 23eb1d1641SHerbert Xu #include <linux/skbuff.h> 24eb1d1641SHerbert Xu #include <linux/slab.h> 25eb1d1641SHerbert Xu #include <linux/timer.h> 261c8ad5bfSCong Wang #include <linux/inetdevice.h> 27eb1d1641SHerbert Xu #include <net/ip.h> 28dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 2908b202b6SYOSHIFUJI Hideaki #include <net/ipv6.h> 3008b202b6SYOSHIFUJI Hideaki #include <net/mld.h> 31d4c4f07dSDavid S. Miller #include <net/ip6_checksum.h> 323c3769e6SLinus Lüssing #include <net/addrconf.h> 3308b202b6SYOSHIFUJI Hideaki #endif 34eb1d1641SHerbert Xu 35eb1d1641SHerbert Xu #include "br_private.h" 36eb1d1641SHerbert Xu 37cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br, 38cc0fdd80SLinus Lüssing struct bridge_mcast_query *query); 392ce297fcSCong Wang unsigned int br_mdb_rehash_seq; 40c83b8fabSHerbert Xu 418ef2a9a5SYOSHIFUJI Hideaki static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b) 428ef2a9a5SYOSHIFUJI Hideaki { 438ef2a9a5SYOSHIFUJI Hideaki if (a->proto != b->proto) 448ef2a9a5SYOSHIFUJI Hideaki return 0; 45b0e9a30dSVlad Yasevich if (a->vid != b->vid) 46b0e9a30dSVlad Yasevich return 0; 478ef2a9a5SYOSHIFUJI Hideaki switch (a->proto) { 488ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 498ef2a9a5SYOSHIFUJI Hideaki return a->u.ip4 == b->u.ip4; 50dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 5108b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 5208b202b6SYOSHIFUJI Hideaki return ipv6_addr_equal(&a->u.ip6, &b->u.ip6); 5308b202b6SYOSHIFUJI Hideaki #endif 548ef2a9a5SYOSHIFUJI Hideaki } 558ef2a9a5SYOSHIFUJI Hideaki return 0; 568ef2a9a5SYOSHIFUJI Hideaki } 578ef2a9a5SYOSHIFUJI Hideaki 58b0e9a30dSVlad Yasevich static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip, 59b0e9a30dSVlad Yasevich __u16 vid) 60eb1d1641SHerbert Xu { 61b0e9a30dSVlad Yasevich return jhash_2words((__force u32)ip, vid, mdb->secret) & (mdb->max - 1); 62eb1d1641SHerbert Xu } 63eb1d1641SHerbert Xu 64dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 6508b202b6SYOSHIFUJI Hideaki static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb, 66b0e9a30dSVlad Yasevich const struct in6_addr *ip, 67b0e9a30dSVlad Yasevich __u16 vid) 6808b202b6SYOSHIFUJI Hideaki { 69b0e9a30dSVlad Yasevich return jhash_2words(ipv6_addr_hash(ip), vid, 70b0e9a30dSVlad Yasevich mdb->secret) & (mdb->max - 1); 7108b202b6SYOSHIFUJI Hideaki } 7208b202b6SYOSHIFUJI Hideaki #endif 7308b202b6SYOSHIFUJI Hideaki 748ef2a9a5SYOSHIFUJI Hideaki static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb, 758ef2a9a5SYOSHIFUJI Hideaki struct br_ip *ip) 768ef2a9a5SYOSHIFUJI Hideaki { 778ef2a9a5SYOSHIFUJI Hideaki switch (ip->proto) { 788ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 79b0e9a30dSVlad Yasevich return __br_ip4_hash(mdb, ip->u.ip4, ip->vid); 80dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 8108b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 82b0e9a30dSVlad Yasevich return __br_ip6_hash(mdb, &ip->u.ip6, ip->vid); 8308b202b6SYOSHIFUJI Hideaki #endif 848ef2a9a5SYOSHIFUJI Hideaki } 858ef2a9a5SYOSHIFUJI Hideaki return 0; 86eb1d1641SHerbert Xu } 87eb1d1641SHerbert Xu 88eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *__br_mdb_ip_get( 898ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash) 90eb1d1641SHerbert Xu { 91eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 92eb1d1641SHerbert Xu 93b67bfe0dSSasha Levin hlist_for_each_entry_rcu(mp, &mdb->mhash[hash], hlist[mdb->ver]) { 948ef2a9a5SYOSHIFUJI Hideaki if (br_ip_equal(&mp->addr, dst)) 95eb1d1641SHerbert Xu return mp; 96eb1d1641SHerbert Xu } 97eb1d1641SHerbert Xu 98eb1d1641SHerbert Xu return NULL; 99eb1d1641SHerbert Xu } 100eb1d1641SHerbert Xu 101cfd56754SCong Wang struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge_mdb_htable *mdb, 102cfd56754SCong Wang struct br_ip *dst) 1037f285fa7SHerbert Xu { 1047f285fa7SHerbert Xu if (!mdb) 1057f285fa7SHerbert Xu return NULL; 1067f285fa7SHerbert Xu 1077f285fa7SHerbert Xu return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst)); 1087f285fa7SHerbert Xu } 1097f285fa7SHerbert Xu 1108ef2a9a5SYOSHIFUJI Hideaki static struct net_bridge_mdb_entry *br_mdb_ip4_get( 111b0e9a30dSVlad Yasevich struct net_bridge_mdb_htable *mdb, __be32 dst, __u16 vid) 112eb1d1641SHerbert Xu { 1138ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_dst; 1140821ec55SHerbert Xu 1158ef2a9a5SYOSHIFUJI Hideaki br_dst.u.ip4 = dst; 1168ef2a9a5SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IP); 117b0e9a30dSVlad Yasevich br_dst.vid = vid; 1188ef2a9a5SYOSHIFUJI Hideaki 1197f285fa7SHerbert Xu return br_mdb_ip_get(mdb, &br_dst); 1208ef2a9a5SYOSHIFUJI Hideaki } 1218ef2a9a5SYOSHIFUJI Hideaki 122dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 12308b202b6SYOSHIFUJI Hideaki static struct net_bridge_mdb_entry *br_mdb_ip6_get( 124b0e9a30dSVlad Yasevich struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst, 125b0e9a30dSVlad Yasevich __u16 vid) 12608b202b6SYOSHIFUJI Hideaki { 12708b202b6SYOSHIFUJI Hideaki struct br_ip br_dst; 12808b202b6SYOSHIFUJI Hideaki 1294e3fd7a0SAlexey Dobriyan br_dst.u.ip6 = *dst; 13008b202b6SYOSHIFUJI Hideaki br_dst.proto = htons(ETH_P_IPV6); 131b0e9a30dSVlad Yasevich br_dst.vid = vid; 13208b202b6SYOSHIFUJI Hideaki 1337f285fa7SHerbert Xu return br_mdb_ip_get(mdb, &br_dst); 13408b202b6SYOSHIFUJI Hideaki } 13508b202b6SYOSHIFUJI Hideaki #endif 13608b202b6SYOSHIFUJI Hideaki 137eb1d1641SHerbert Xu struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br, 138fbca58a2SCong Wang struct sk_buff *skb, u16 vid) 139eb1d1641SHerbert Xu { 140e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb); 1418ef2a9a5SYOSHIFUJI Hideaki struct br_ip ip; 1428ef2a9a5SYOSHIFUJI Hideaki 1437f285fa7SHerbert Xu if (br->multicast_disabled) 144eb1d1641SHerbert Xu return NULL; 145eb1d1641SHerbert Xu 1468ef2a9a5SYOSHIFUJI Hideaki if (BR_INPUT_SKB_CB(skb)->igmp) 1478ef2a9a5SYOSHIFUJI Hideaki return NULL; 1488ef2a9a5SYOSHIFUJI Hideaki 1498ef2a9a5SYOSHIFUJI Hideaki ip.proto = skb->protocol; 150fbca58a2SCong Wang ip.vid = vid; 1518ef2a9a5SYOSHIFUJI Hideaki 152eb1d1641SHerbert Xu switch (skb->protocol) { 153eb1d1641SHerbert Xu case htons(ETH_P_IP): 1548ef2a9a5SYOSHIFUJI Hideaki ip.u.ip4 = ip_hdr(skb)->daddr; 155eb1d1641SHerbert Xu break; 156dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 15708b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 1584e3fd7a0SAlexey Dobriyan ip.u.ip6 = ipv6_hdr(skb)->daddr; 15908b202b6SYOSHIFUJI Hideaki break; 16008b202b6SYOSHIFUJI Hideaki #endif 1618ef2a9a5SYOSHIFUJI Hideaki default: 1628ef2a9a5SYOSHIFUJI Hideaki return NULL; 163eb1d1641SHerbert Xu } 164eb1d1641SHerbert Xu 1658ef2a9a5SYOSHIFUJI Hideaki return br_mdb_ip_get(mdb, &ip); 166eb1d1641SHerbert Xu } 167eb1d1641SHerbert Xu 168eb1d1641SHerbert Xu static void br_mdb_free(struct rcu_head *head) 169eb1d1641SHerbert Xu { 170eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb = 171eb1d1641SHerbert Xu container_of(head, struct net_bridge_mdb_htable, rcu); 172eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old = mdb->old; 173eb1d1641SHerbert Xu 174eb1d1641SHerbert Xu mdb->old = NULL; 175eb1d1641SHerbert Xu kfree(old->mhash); 176eb1d1641SHerbert Xu kfree(old); 177eb1d1641SHerbert Xu } 178eb1d1641SHerbert Xu 179eb1d1641SHerbert Xu static int br_mdb_copy(struct net_bridge_mdb_htable *new, 180eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old, 181eb1d1641SHerbert Xu int elasticity) 182eb1d1641SHerbert Xu { 183eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 184eb1d1641SHerbert Xu int maxlen; 185eb1d1641SHerbert Xu int len; 186eb1d1641SHerbert Xu int i; 187eb1d1641SHerbert Xu 188eb1d1641SHerbert Xu for (i = 0; i < old->max; i++) 189b67bfe0dSSasha Levin hlist_for_each_entry(mp, &old->mhash[i], hlist[old->ver]) 190eb1d1641SHerbert Xu hlist_add_head(&mp->hlist[new->ver], 1918ef2a9a5SYOSHIFUJI Hideaki &new->mhash[br_ip_hash(new, &mp->addr)]); 192eb1d1641SHerbert Xu 193eb1d1641SHerbert Xu if (!elasticity) 194eb1d1641SHerbert Xu return 0; 195eb1d1641SHerbert Xu 196eb1d1641SHerbert Xu maxlen = 0; 197eb1d1641SHerbert Xu for (i = 0; i < new->max; i++) { 198eb1d1641SHerbert Xu len = 0; 199b67bfe0dSSasha Levin hlist_for_each_entry(mp, &new->mhash[i], hlist[new->ver]) 200eb1d1641SHerbert Xu len++; 201eb1d1641SHerbert Xu if (len > maxlen) 202eb1d1641SHerbert Xu maxlen = len; 203eb1d1641SHerbert Xu } 204eb1d1641SHerbert Xu 205eb1d1641SHerbert Xu return maxlen > elasticity ? -EINVAL : 0; 206eb1d1641SHerbert Xu } 207eb1d1641SHerbert Xu 208cfd56754SCong Wang void br_multicast_free_pg(struct rcu_head *head) 209eb1d1641SHerbert Xu { 210eb1d1641SHerbert Xu struct net_bridge_port_group *p = 211eb1d1641SHerbert Xu container_of(head, struct net_bridge_port_group, rcu); 212eb1d1641SHerbert Xu 213eb1d1641SHerbert Xu kfree(p); 214eb1d1641SHerbert Xu } 215eb1d1641SHerbert Xu 216eb1d1641SHerbert Xu static void br_multicast_free_group(struct rcu_head *head) 217eb1d1641SHerbert Xu { 218eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = 219eb1d1641SHerbert Xu container_of(head, struct net_bridge_mdb_entry, rcu); 220eb1d1641SHerbert Xu 221eb1d1641SHerbert Xu kfree(mp); 222eb1d1641SHerbert Xu } 223eb1d1641SHerbert Xu 224eb1d1641SHerbert Xu static void br_multicast_group_expired(unsigned long data) 225eb1d1641SHerbert Xu { 226eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = (void *)data; 227eb1d1641SHerbert Xu struct net_bridge *br = mp->br; 228eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 229eb1d1641SHerbert Xu 230eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 231eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&mp->timer)) 232eb1d1641SHerbert Xu goto out; 233eb1d1641SHerbert Xu 2348a870178SHerbert Xu mp->mglist = false; 235eb1d1641SHerbert Xu 236eb1d1641SHerbert Xu if (mp->ports) 237eb1d1641SHerbert Xu goto out; 238eb1d1641SHerbert Xu 239e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 240e8051688SEric Dumazet 241eb1d1641SHerbert Xu hlist_del_rcu(&mp->hlist[mdb->ver]); 242eb1d1641SHerbert Xu mdb->size--; 243eb1d1641SHerbert Xu 244eb1d1641SHerbert Xu call_rcu_bh(&mp->rcu, br_multicast_free_group); 245eb1d1641SHerbert Xu 246eb1d1641SHerbert Xu out: 247eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 248eb1d1641SHerbert Xu } 249eb1d1641SHerbert Xu 250eb1d1641SHerbert Xu static void br_multicast_del_pg(struct net_bridge *br, 251eb1d1641SHerbert Xu struct net_bridge_port_group *pg) 252eb1d1641SHerbert Xu { 253e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 254eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 255eb1d1641SHerbert Xu struct net_bridge_port_group *p; 256e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 257e8051688SEric Dumazet 258e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 259eb1d1641SHerbert Xu 2608ef2a9a5SYOSHIFUJI Hideaki mp = br_mdb_ip_get(mdb, &pg->addr); 261eb1d1641SHerbert Xu if (WARN_ON(!mp)) 262eb1d1641SHerbert Xu return; 263eb1d1641SHerbert Xu 264e8051688SEric Dumazet for (pp = &mp->ports; 265e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 266e8051688SEric Dumazet pp = &p->next) { 267eb1d1641SHerbert Xu if (p != pg) 268eb1d1641SHerbert Xu continue; 269eb1d1641SHerbert Xu 27083f6a740Sstephen hemminger rcu_assign_pointer(*pp, p->next); 271eb1d1641SHerbert Xu hlist_del_init(&p->mglist); 272eb1d1641SHerbert Xu del_timer(&p->timer); 273eb1d1641SHerbert Xu call_rcu_bh(&p->rcu, br_multicast_free_pg); 274eb1d1641SHerbert Xu 275454594f3SLinus Lüssing if (!mp->ports && !mp->mglist && 276eb1d1641SHerbert Xu netif_running(br->dev)) 277eb1d1641SHerbert Xu mod_timer(&mp->timer, jiffies); 278eb1d1641SHerbert Xu 279eb1d1641SHerbert Xu return; 280eb1d1641SHerbert Xu } 281eb1d1641SHerbert Xu 282eb1d1641SHerbert Xu WARN_ON(1); 283eb1d1641SHerbert Xu } 284eb1d1641SHerbert Xu 285eb1d1641SHerbert Xu static void br_multicast_port_group_expired(unsigned long data) 286eb1d1641SHerbert Xu { 287eb1d1641SHerbert Xu struct net_bridge_port_group *pg = (void *)data; 288eb1d1641SHerbert Xu struct net_bridge *br = pg->port->br; 289eb1d1641SHerbert Xu 290eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 291eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&pg->timer) || 292ccb1c31aSAmerigo Wang hlist_unhashed(&pg->mglist) || pg->state & MDB_PERMANENT) 293eb1d1641SHerbert Xu goto out; 294eb1d1641SHerbert Xu 295eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 296eb1d1641SHerbert Xu 297eb1d1641SHerbert Xu out: 298eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 299eb1d1641SHerbert Xu } 300eb1d1641SHerbert Xu 301e8051688SEric Dumazet static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max, 302eb1d1641SHerbert Xu int elasticity) 303eb1d1641SHerbert Xu { 304e8051688SEric Dumazet struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1); 305eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 306eb1d1641SHerbert Xu int err; 307eb1d1641SHerbert Xu 308eb1d1641SHerbert Xu mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC); 309eb1d1641SHerbert Xu if (!mdb) 310eb1d1641SHerbert Xu return -ENOMEM; 311eb1d1641SHerbert Xu 312eb1d1641SHerbert Xu mdb->max = max; 313eb1d1641SHerbert Xu mdb->old = old; 314eb1d1641SHerbert Xu 315eb1d1641SHerbert Xu mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC); 316eb1d1641SHerbert Xu if (!mdb->mhash) { 317eb1d1641SHerbert Xu kfree(mdb); 318eb1d1641SHerbert Xu return -ENOMEM; 319eb1d1641SHerbert Xu } 320eb1d1641SHerbert Xu 321eb1d1641SHerbert Xu mdb->size = old ? old->size : 0; 322eb1d1641SHerbert Xu mdb->ver = old ? old->ver ^ 1 : 0; 323eb1d1641SHerbert Xu 324eb1d1641SHerbert Xu if (!old || elasticity) 325eb1d1641SHerbert Xu get_random_bytes(&mdb->secret, sizeof(mdb->secret)); 326eb1d1641SHerbert Xu else 327eb1d1641SHerbert Xu mdb->secret = old->secret; 328eb1d1641SHerbert Xu 329eb1d1641SHerbert Xu if (!old) 330eb1d1641SHerbert Xu goto out; 331eb1d1641SHerbert Xu 332eb1d1641SHerbert Xu err = br_mdb_copy(mdb, old, elasticity); 333eb1d1641SHerbert Xu if (err) { 334eb1d1641SHerbert Xu kfree(mdb->mhash); 335eb1d1641SHerbert Xu kfree(mdb); 336eb1d1641SHerbert Xu return err; 337eb1d1641SHerbert Xu } 338eb1d1641SHerbert Xu 3392ce297fcSCong Wang br_mdb_rehash_seq++; 340eb1d1641SHerbert Xu call_rcu_bh(&mdb->rcu, br_mdb_free); 341eb1d1641SHerbert Xu 342eb1d1641SHerbert Xu out: 343eb1d1641SHerbert Xu rcu_assign_pointer(*mdbp, mdb); 344eb1d1641SHerbert Xu 345eb1d1641SHerbert Xu return 0; 346eb1d1641SHerbert Xu } 347eb1d1641SHerbert Xu 3488ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br, 349eb1d1641SHerbert Xu __be32 group) 350eb1d1641SHerbert Xu { 351eb1d1641SHerbert Xu struct sk_buff *skb; 352eb1d1641SHerbert Xu struct igmphdr *ih; 353eb1d1641SHerbert Xu struct ethhdr *eth; 354eb1d1641SHerbert Xu struct iphdr *iph; 355eb1d1641SHerbert Xu 356eb1d1641SHerbert Xu skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) + 357eb1d1641SHerbert Xu sizeof(*ih) + 4); 358eb1d1641SHerbert Xu if (!skb) 359eb1d1641SHerbert Xu goto out; 360eb1d1641SHerbert Xu 361eb1d1641SHerbert Xu skb->protocol = htons(ETH_P_IP); 362eb1d1641SHerbert Xu 363eb1d1641SHerbert Xu skb_reset_mac_header(skb); 364eb1d1641SHerbert Xu eth = eth_hdr(skb); 365eb1d1641SHerbert Xu 366d458cdf7SJoe Perches memcpy(eth->h_source, br->dev->dev_addr, ETH_ALEN); 367eb1d1641SHerbert Xu eth->h_dest[0] = 1; 368eb1d1641SHerbert Xu eth->h_dest[1] = 0; 369eb1d1641SHerbert Xu eth->h_dest[2] = 0x5e; 370eb1d1641SHerbert Xu eth->h_dest[3] = 0; 371eb1d1641SHerbert Xu eth->h_dest[4] = 0; 372eb1d1641SHerbert Xu eth->h_dest[5] = 1; 373eb1d1641SHerbert Xu eth->h_proto = htons(ETH_P_IP); 374eb1d1641SHerbert Xu skb_put(skb, sizeof(*eth)); 375eb1d1641SHerbert Xu 376eb1d1641SHerbert Xu skb_set_network_header(skb, skb->len); 377eb1d1641SHerbert Xu iph = ip_hdr(skb); 378eb1d1641SHerbert Xu 379eb1d1641SHerbert Xu iph->version = 4; 380eb1d1641SHerbert Xu iph->ihl = 6; 381eb1d1641SHerbert Xu iph->tos = 0xc0; 382eb1d1641SHerbert Xu iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4); 383eb1d1641SHerbert Xu iph->id = 0; 384eb1d1641SHerbert Xu iph->frag_off = htons(IP_DF); 385eb1d1641SHerbert Xu iph->ttl = 1; 386eb1d1641SHerbert Xu iph->protocol = IPPROTO_IGMP; 3871c8ad5bfSCong Wang iph->saddr = br->multicast_query_use_ifaddr ? 3881c8ad5bfSCong Wang inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0; 389eb1d1641SHerbert Xu iph->daddr = htonl(INADDR_ALLHOSTS_GROUP); 390eb1d1641SHerbert Xu ((u8 *)&iph[1])[0] = IPOPT_RA; 391eb1d1641SHerbert Xu ((u8 *)&iph[1])[1] = 4; 392eb1d1641SHerbert Xu ((u8 *)&iph[1])[2] = 0; 393eb1d1641SHerbert Xu ((u8 *)&iph[1])[3] = 0; 394eb1d1641SHerbert Xu ip_send_check(iph); 395eb1d1641SHerbert Xu skb_put(skb, 24); 396eb1d1641SHerbert Xu 397eb1d1641SHerbert Xu skb_set_transport_header(skb, skb->len); 398eb1d1641SHerbert Xu ih = igmp_hdr(skb); 399eb1d1641SHerbert Xu ih->type = IGMP_HOST_MEMBERSHIP_QUERY; 400eb1d1641SHerbert Xu ih->code = (group ? br->multicast_last_member_interval : 401eb1d1641SHerbert Xu br->multicast_query_response_interval) / 402eb1d1641SHerbert Xu (HZ / IGMP_TIMER_SCALE); 403eb1d1641SHerbert Xu ih->group = group; 404eb1d1641SHerbert Xu ih->csum = 0; 405eb1d1641SHerbert Xu ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr)); 406eb1d1641SHerbert Xu skb_put(skb, sizeof(*ih)); 407eb1d1641SHerbert Xu 408eb1d1641SHerbert Xu __skb_pull(skb, sizeof(*eth)); 409eb1d1641SHerbert Xu 410eb1d1641SHerbert Xu out: 411eb1d1641SHerbert Xu return skb; 412eb1d1641SHerbert Xu } 413eb1d1641SHerbert Xu 414dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 41508b202b6SYOSHIFUJI Hideaki static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br, 416b71d1d42SEric Dumazet const struct in6_addr *group) 41708b202b6SYOSHIFUJI Hideaki { 41808b202b6SYOSHIFUJI Hideaki struct sk_buff *skb; 41908b202b6SYOSHIFUJI Hideaki struct ipv6hdr *ip6h; 42008b202b6SYOSHIFUJI Hideaki struct mld_msg *mldq; 42108b202b6SYOSHIFUJI Hideaki struct ethhdr *eth; 42208b202b6SYOSHIFUJI Hideaki u8 *hopopt; 42308b202b6SYOSHIFUJI Hideaki unsigned long interval; 42408b202b6SYOSHIFUJI Hideaki 42508b202b6SYOSHIFUJI Hideaki skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) + 42608b202b6SYOSHIFUJI Hideaki 8 + sizeof(*mldq)); 42708b202b6SYOSHIFUJI Hideaki if (!skb) 42808b202b6SYOSHIFUJI Hideaki goto out; 42908b202b6SYOSHIFUJI Hideaki 43008b202b6SYOSHIFUJI Hideaki skb->protocol = htons(ETH_P_IPV6); 43108b202b6SYOSHIFUJI Hideaki 43208b202b6SYOSHIFUJI Hideaki /* Ethernet header */ 43308b202b6SYOSHIFUJI Hideaki skb_reset_mac_header(skb); 43408b202b6SYOSHIFUJI Hideaki eth = eth_hdr(skb); 43508b202b6SYOSHIFUJI Hideaki 436d458cdf7SJoe Perches memcpy(eth->h_source, br->dev->dev_addr, ETH_ALEN); 43708b202b6SYOSHIFUJI Hideaki eth->h_proto = htons(ETH_P_IPV6); 43808b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*eth)); 43908b202b6SYOSHIFUJI Hideaki 44008b202b6SYOSHIFUJI Hideaki /* IPv6 header + HbH option */ 44108b202b6SYOSHIFUJI Hideaki skb_set_network_header(skb, skb->len); 44208b202b6SYOSHIFUJI Hideaki ip6h = ipv6_hdr(skb); 44308b202b6SYOSHIFUJI Hideaki 44408b202b6SYOSHIFUJI Hideaki *(__force __be32 *)ip6h = htonl(0x60000000); 44576d66158SDavid Stevens ip6h->payload_len = htons(8 + sizeof(*mldq)); 44608b202b6SYOSHIFUJI Hideaki ip6h->nexthdr = IPPROTO_HOPOPTS; 44708b202b6SYOSHIFUJI Hideaki ip6h->hop_limit = 1; 448a7bff75bSLinus Lüssing ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1)); 449d1d81d4cSUlrich Weber if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0, 450d1d81d4cSUlrich Weber &ip6h->saddr)) { 451d1d81d4cSUlrich Weber kfree_skb(skb); 452d1d81d4cSUlrich Weber return NULL; 453d1d81d4cSUlrich Weber } 45436cff5a1SLinus Lüssing ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest); 45508b202b6SYOSHIFUJI Hideaki 45608b202b6SYOSHIFUJI Hideaki hopopt = (u8 *)(ip6h + 1); 45708b202b6SYOSHIFUJI Hideaki hopopt[0] = IPPROTO_ICMPV6; /* next hdr */ 45808b202b6SYOSHIFUJI Hideaki hopopt[1] = 0; /* length of HbH */ 45908b202b6SYOSHIFUJI Hideaki hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */ 46008b202b6SYOSHIFUJI Hideaki hopopt[3] = 2; /* Length of RA Option */ 46108b202b6SYOSHIFUJI Hideaki hopopt[4] = 0; /* Type = 0x0000 (MLD) */ 46208b202b6SYOSHIFUJI Hideaki hopopt[5] = 0; 4631de5a71cSEldad Zack hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */ 4641de5a71cSEldad Zack hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */ 46508b202b6SYOSHIFUJI Hideaki 46608b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*ip6h) + 8); 46708b202b6SYOSHIFUJI Hideaki 46808b202b6SYOSHIFUJI Hideaki /* ICMPv6 */ 46908b202b6SYOSHIFUJI Hideaki skb_set_transport_header(skb, skb->len); 47008b202b6SYOSHIFUJI Hideaki mldq = (struct mld_msg *) icmp6_hdr(skb); 47108b202b6SYOSHIFUJI Hideaki 47232de868cSLinus Lüssing interval = ipv6_addr_any(group) ? 47332de868cSLinus Lüssing br->multicast_query_response_interval : 47432de868cSLinus Lüssing br->multicast_last_member_interval; 47508b202b6SYOSHIFUJI Hideaki 47608b202b6SYOSHIFUJI Hideaki mldq->mld_type = ICMPV6_MGM_QUERY; 47708b202b6SYOSHIFUJI Hideaki mldq->mld_code = 0; 47808b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = 0; 47908b202b6SYOSHIFUJI Hideaki mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval)); 48008b202b6SYOSHIFUJI Hideaki mldq->mld_reserved = 0; 4814e3fd7a0SAlexey Dobriyan mldq->mld_mca = *group; 48208b202b6SYOSHIFUJI Hideaki 48308b202b6SYOSHIFUJI Hideaki /* checksum */ 48408b202b6SYOSHIFUJI Hideaki mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, 48508b202b6SYOSHIFUJI Hideaki sizeof(*mldq), IPPROTO_ICMPV6, 48608b202b6SYOSHIFUJI Hideaki csum_partial(mldq, 48708b202b6SYOSHIFUJI Hideaki sizeof(*mldq), 0)); 48808b202b6SYOSHIFUJI Hideaki skb_put(skb, sizeof(*mldq)); 48908b202b6SYOSHIFUJI Hideaki 49008b202b6SYOSHIFUJI Hideaki __skb_pull(skb, sizeof(*eth)); 49108b202b6SYOSHIFUJI Hideaki 49208b202b6SYOSHIFUJI Hideaki out: 49308b202b6SYOSHIFUJI Hideaki return skb; 49408b202b6SYOSHIFUJI Hideaki } 49508b202b6SYOSHIFUJI Hideaki #endif 49608b202b6SYOSHIFUJI Hideaki 4978ef2a9a5SYOSHIFUJI Hideaki static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br, 4988ef2a9a5SYOSHIFUJI Hideaki struct br_ip *addr) 4998ef2a9a5SYOSHIFUJI Hideaki { 5008ef2a9a5SYOSHIFUJI Hideaki switch (addr->proto) { 5018ef2a9a5SYOSHIFUJI Hideaki case htons(ETH_P_IP): 5028ef2a9a5SYOSHIFUJI Hideaki return br_ip4_multicast_alloc_query(br, addr->u.ip4); 503dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 50408b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 50508b202b6SYOSHIFUJI Hideaki return br_ip6_multicast_alloc_query(br, &addr->u.ip6); 50608b202b6SYOSHIFUJI Hideaki #endif 5078ef2a9a5SYOSHIFUJI Hideaki } 5088ef2a9a5SYOSHIFUJI Hideaki return NULL; 5098ef2a9a5SYOSHIFUJI Hideaki } 5108ef2a9a5SYOSHIFUJI Hideaki 511eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *br_multicast_get_group( 5128ef2a9a5SYOSHIFUJI Hideaki struct net_bridge *br, struct net_bridge_port *port, 5138ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group, int hash) 514eb1d1641SHerbert Xu { 515e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 516eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 51795c96174SEric Dumazet unsigned int count = 0; 51895c96174SEric Dumazet unsigned int max; 519eb1d1641SHerbert Xu int elasticity; 520eb1d1641SHerbert Xu int err; 521eb1d1641SHerbert Xu 522e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 523b67bfe0dSSasha Levin hlist_for_each_entry(mp, &mdb->mhash[hash], hlist[mdb->ver]) { 524eb1d1641SHerbert Xu count++; 5258ef2a9a5SYOSHIFUJI Hideaki if (unlikely(br_ip_equal(group, &mp->addr))) 526eb1d1641SHerbert Xu return mp; 527eb1d1641SHerbert Xu } 528eb1d1641SHerbert Xu 529eb1d1641SHerbert Xu elasticity = 0; 530eb1d1641SHerbert Xu max = mdb->max; 531eb1d1641SHerbert Xu 532eb1d1641SHerbert Xu if (unlikely(count > br->hash_elasticity && count)) { 533eb1d1641SHerbert Xu if (net_ratelimit()) 53428a16c97Sstephen hemminger br_info(br, "Multicast hash table " 535eb1d1641SHerbert Xu "chain limit reached: %s\n", 53628a16c97Sstephen hemminger port ? port->dev->name : br->dev->name); 537eb1d1641SHerbert Xu 538eb1d1641SHerbert Xu elasticity = br->hash_elasticity; 539eb1d1641SHerbert Xu } 540eb1d1641SHerbert Xu 541eb1d1641SHerbert Xu if (mdb->size >= max) { 542eb1d1641SHerbert Xu max *= 2; 543036be6dbSThomas Graf if (unlikely(max > br->hash_max)) { 544036be6dbSThomas Graf br_warn(br, "Multicast hash table maximum of %d " 545036be6dbSThomas Graf "reached, disabling snooping: %s\n", 546036be6dbSThomas Graf br->hash_max, 547036be6dbSThomas Graf port ? port->dev->name : br->dev->name); 548eb1d1641SHerbert Xu err = -E2BIG; 549eb1d1641SHerbert Xu disable: 550eb1d1641SHerbert Xu br->multicast_disabled = 1; 551eb1d1641SHerbert Xu goto err; 552eb1d1641SHerbert Xu } 553eb1d1641SHerbert Xu } 554eb1d1641SHerbert Xu 555eb1d1641SHerbert Xu if (max > mdb->max || elasticity) { 556eb1d1641SHerbert Xu if (mdb->old) { 557eb1d1641SHerbert Xu if (net_ratelimit()) 55828a16c97Sstephen hemminger br_info(br, "Multicast hash table " 559eb1d1641SHerbert Xu "on fire: %s\n", 56028a16c97Sstephen hemminger port ? port->dev->name : br->dev->name); 561eb1d1641SHerbert Xu err = -EEXIST; 562eb1d1641SHerbert Xu goto err; 563eb1d1641SHerbert Xu } 564eb1d1641SHerbert Xu 565eb1d1641SHerbert Xu err = br_mdb_rehash(&br->mdb, max, elasticity); 566eb1d1641SHerbert Xu if (err) { 56728a16c97Sstephen hemminger br_warn(br, "Cannot rehash multicast " 56828a16c97Sstephen hemminger "hash table, disabling snooping: %s, %d, %d\n", 56928a16c97Sstephen hemminger port ? port->dev->name : br->dev->name, 570eb1d1641SHerbert Xu mdb->size, err); 571eb1d1641SHerbert Xu goto disable; 572eb1d1641SHerbert Xu } 573eb1d1641SHerbert Xu 574eb1d1641SHerbert Xu err = -EAGAIN; 575eb1d1641SHerbert Xu goto err; 576eb1d1641SHerbert Xu } 577eb1d1641SHerbert Xu 578eb1d1641SHerbert Xu return NULL; 579eb1d1641SHerbert Xu 580eb1d1641SHerbert Xu err: 581eb1d1641SHerbert Xu mp = ERR_PTR(err); 582eb1d1641SHerbert Xu return mp; 583eb1d1641SHerbert Xu } 584eb1d1641SHerbert Xu 585cfd56754SCong Wang struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br, 586cfd56754SCong Wang struct net_bridge_port *port, struct br_ip *group) 587eb1d1641SHerbert Xu { 588e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 589eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 590eb1d1641SHerbert Xu int hash; 5914c0833bcSTobias Klauser int err; 592eb1d1641SHerbert Xu 593e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 594eb1d1641SHerbert Xu if (!mdb) { 5954c0833bcSTobias Klauser err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0); 5964c0833bcSTobias Klauser if (err) 5974c0833bcSTobias Klauser return ERR_PTR(err); 598eb1d1641SHerbert Xu goto rehash; 599eb1d1641SHerbert Xu } 600eb1d1641SHerbert Xu 601eb1d1641SHerbert Xu hash = br_ip_hash(mdb, group); 602eb1d1641SHerbert Xu mp = br_multicast_get_group(br, port, group, hash); 603eb1d1641SHerbert Xu switch (PTR_ERR(mp)) { 604eb1d1641SHerbert Xu case 0: 605eb1d1641SHerbert Xu break; 606eb1d1641SHerbert Xu 607eb1d1641SHerbert Xu case -EAGAIN: 608eb1d1641SHerbert Xu rehash: 609e8051688SEric Dumazet mdb = rcu_dereference_protected(br->mdb, 1); 610eb1d1641SHerbert Xu hash = br_ip_hash(mdb, group); 611eb1d1641SHerbert Xu break; 612eb1d1641SHerbert Xu 613eb1d1641SHerbert Xu default: 614eb1d1641SHerbert Xu goto out; 615eb1d1641SHerbert Xu } 616eb1d1641SHerbert Xu 617eb1d1641SHerbert Xu mp = kzalloc(sizeof(*mp), GFP_ATOMIC); 618eb1d1641SHerbert Xu if (unlikely(!mp)) 6194c0833bcSTobias Klauser return ERR_PTR(-ENOMEM); 620eb1d1641SHerbert Xu 621eb1d1641SHerbert Xu mp->br = br; 6228ef2a9a5SYOSHIFUJI Hideaki mp->addr = *group; 6231faabf2aSEric Dumazet setup_timer(&mp->timer, br_multicast_group_expired, 6241faabf2aSEric Dumazet (unsigned long)mp); 6251faabf2aSEric Dumazet 626eb1d1641SHerbert Xu hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]); 627eb1d1641SHerbert Xu mdb->size++; 628eb1d1641SHerbert Xu 629eb1d1641SHerbert Xu out: 630eb1d1641SHerbert Xu return mp; 631eb1d1641SHerbert Xu } 632eb1d1641SHerbert Xu 633cfd56754SCong Wang struct net_bridge_port_group *br_multicast_new_port_group( 634cfd56754SCong Wang struct net_bridge_port *port, 635cfd56754SCong Wang struct br_ip *group, 636ccb1c31aSAmerigo Wang struct net_bridge_port_group __rcu *next, 637ccb1c31aSAmerigo Wang unsigned char state) 638cfd56754SCong Wang { 639cfd56754SCong Wang struct net_bridge_port_group *p; 640cfd56754SCong Wang 641cfd56754SCong Wang p = kzalloc(sizeof(*p), GFP_ATOMIC); 642cfd56754SCong Wang if (unlikely(!p)) 643cfd56754SCong Wang return NULL; 644cfd56754SCong Wang 645cfd56754SCong Wang p->addr = *group; 646cfd56754SCong Wang p->port = port; 647ccb1c31aSAmerigo Wang p->state = state; 648eca2a43bSstephen hemminger rcu_assign_pointer(p->next, next); 649cfd56754SCong Wang hlist_add_head(&p->mglist, &port->mglist); 650cfd56754SCong Wang setup_timer(&p->timer, br_multicast_port_group_expired, 651cfd56754SCong Wang (unsigned long)p); 652cfd56754SCong Wang return p; 653cfd56754SCong Wang } 654cfd56754SCong Wang 655eb1d1641SHerbert Xu static int br_multicast_add_group(struct net_bridge *br, 6568ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 6578ef2a9a5SYOSHIFUJI Hideaki struct br_ip *group) 658eb1d1641SHerbert Xu { 659eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 660eb1d1641SHerbert Xu struct net_bridge_port_group *p; 661e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 662454594f3SLinus Lüssing unsigned long now = jiffies; 663eb1d1641SHerbert Xu int err; 664eb1d1641SHerbert Xu 665eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 666eb1d1641SHerbert Xu if (!netif_running(br->dev) || 667eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 668eb1d1641SHerbert Xu goto out; 669eb1d1641SHerbert Xu 670eb1d1641SHerbert Xu mp = br_multicast_new_group(br, port, group); 671eb1d1641SHerbert Xu err = PTR_ERR(mp); 6724c0833bcSTobias Klauser if (IS_ERR(mp)) 673eb1d1641SHerbert Xu goto err; 674eb1d1641SHerbert Xu 675eb1d1641SHerbert Xu if (!port) { 6768a870178SHerbert Xu mp->mglist = true; 677454594f3SLinus Lüssing mod_timer(&mp->timer, now + br->multicast_membership_interval); 678eb1d1641SHerbert Xu goto out; 679eb1d1641SHerbert Xu } 680eb1d1641SHerbert Xu 681e8051688SEric Dumazet for (pp = &mp->ports; 682e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 683e8051688SEric Dumazet pp = &p->next) { 684eb1d1641SHerbert Xu if (p->port == port) 685454594f3SLinus Lüssing goto found; 686eb1d1641SHerbert Xu if ((unsigned long)p->port < (unsigned long)port) 687eb1d1641SHerbert Xu break; 688eb1d1641SHerbert Xu } 689eb1d1641SHerbert Xu 690ccb1c31aSAmerigo Wang p = br_multicast_new_port_group(port, group, *pp, MDB_TEMPORARY); 691eb1d1641SHerbert Xu if (unlikely(!p)) 692eb1d1641SHerbert Xu goto err; 693eb1d1641SHerbert Xu rcu_assign_pointer(*pp, p); 69437a393bcSCong Wang br_mdb_notify(br->dev, port, group, RTM_NEWMDB); 695eb1d1641SHerbert Xu 696454594f3SLinus Lüssing found: 697454594f3SLinus Lüssing mod_timer(&p->timer, now + br->multicast_membership_interval); 698eb1d1641SHerbert Xu out: 699eb1d1641SHerbert Xu err = 0; 700eb1d1641SHerbert Xu 701eb1d1641SHerbert Xu err: 702eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 703eb1d1641SHerbert Xu return err; 704eb1d1641SHerbert Xu } 705eb1d1641SHerbert Xu 7068ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_add_group(struct net_bridge *br, 7078ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 708b0e9a30dSVlad Yasevich __be32 group, 709b0e9a30dSVlad Yasevich __u16 vid) 7108ef2a9a5SYOSHIFUJI Hideaki { 7118ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 7128ef2a9a5SYOSHIFUJI Hideaki 7138ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 7148ef2a9a5SYOSHIFUJI Hideaki return 0; 7158ef2a9a5SYOSHIFUJI Hideaki 7168ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 7178ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 718b0e9a30dSVlad Yasevich br_group.vid = vid; 7198ef2a9a5SYOSHIFUJI Hideaki 7208ef2a9a5SYOSHIFUJI Hideaki return br_multicast_add_group(br, port, &br_group); 7218ef2a9a5SYOSHIFUJI Hideaki } 7228ef2a9a5SYOSHIFUJI Hideaki 723dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 72408b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_add_group(struct net_bridge *br, 72508b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 726b0e9a30dSVlad Yasevich const struct in6_addr *group, 727b0e9a30dSVlad Yasevich __u16 vid) 72808b202b6SYOSHIFUJI Hideaki { 72908b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 73008b202b6SYOSHIFUJI Hideaki 7313c3769e6SLinus Lüssing if (ipv6_addr_is_ll_all_nodes(group)) 73208b202b6SYOSHIFUJI Hideaki return 0; 73308b202b6SYOSHIFUJI Hideaki 7344e3fd7a0SAlexey Dobriyan br_group.u.ip6 = *group; 7359cc6e0c4SLinus Lüssing br_group.proto = htons(ETH_P_IPV6); 736b0e9a30dSVlad Yasevich br_group.vid = vid; 73708b202b6SYOSHIFUJI Hideaki 73808b202b6SYOSHIFUJI Hideaki return br_multicast_add_group(br, port, &br_group); 73908b202b6SYOSHIFUJI Hideaki } 74008b202b6SYOSHIFUJI Hideaki #endif 74108b202b6SYOSHIFUJI Hideaki 742eb1d1641SHerbert Xu static void br_multicast_router_expired(unsigned long data) 743eb1d1641SHerbert Xu { 744eb1d1641SHerbert Xu struct net_bridge_port *port = (void *)data; 745eb1d1641SHerbert Xu struct net_bridge *br = port->br; 746eb1d1641SHerbert Xu 747eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 748eb1d1641SHerbert Xu if (port->multicast_router != 1 || 749eb1d1641SHerbert Xu timer_pending(&port->multicast_router_timer) || 750eb1d1641SHerbert Xu hlist_unhashed(&port->rlist)) 751eb1d1641SHerbert Xu goto out; 752eb1d1641SHerbert Xu 753eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 754eb1d1641SHerbert Xu 755eb1d1641SHerbert Xu out: 756eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 757eb1d1641SHerbert Xu } 758eb1d1641SHerbert Xu 759eb1d1641SHerbert Xu static void br_multicast_local_router_expired(unsigned long data) 760eb1d1641SHerbert Xu { 761eb1d1641SHerbert Xu } 762eb1d1641SHerbert Xu 763cc0fdd80SLinus Lüssing static void br_multicast_querier_expired(struct net_bridge *br, 764cc0fdd80SLinus Lüssing struct bridge_mcast_query *query) 765c83b8fabSHerbert Xu { 766c83b8fabSHerbert Xu spin_lock(&br->multicast_lock); 767c83b8fabSHerbert Xu if (!netif_running(br->dev) || br->multicast_disabled) 768c83b8fabSHerbert Xu goto out; 769c83b8fabSHerbert Xu 770cc0fdd80SLinus Lüssing br_multicast_start_querier(br, query); 771c83b8fabSHerbert Xu 772c83b8fabSHerbert Xu out: 773c83b8fabSHerbert Xu spin_unlock(&br->multicast_lock); 774c83b8fabSHerbert Xu } 775c83b8fabSHerbert Xu 776cc0fdd80SLinus Lüssing static void br_ip4_multicast_querier_expired(unsigned long data) 777cc0fdd80SLinus Lüssing { 778cc0fdd80SLinus Lüssing struct net_bridge *br = (void *)data; 779cc0fdd80SLinus Lüssing 780cc0fdd80SLinus Lüssing br_multicast_querier_expired(br, &br->ip4_query); 781cc0fdd80SLinus Lüssing } 782cc0fdd80SLinus Lüssing 783cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 784cc0fdd80SLinus Lüssing static void br_ip6_multicast_querier_expired(unsigned long data) 785cc0fdd80SLinus Lüssing { 786cc0fdd80SLinus Lüssing struct net_bridge *br = (void *)data; 787cc0fdd80SLinus Lüssing 788cc0fdd80SLinus Lüssing br_multicast_querier_expired(br, &br->ip6_query); 789cc0fdd80SLinus Lüssing } 790cc0fdd80SLinus Lüssing #endif 791cc0fdd80SLinus Lüssing 7928ef2a9a5SYOSHIFUJI Hideaki static void __br_multicast_send_query(struct net_bridge *br, 7938ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 7948ef2a9a5SYOSHIFUJI Hideaki struct br_ip *ip) 795eb1d1641SHerbert Xu { 796eb1d1641SHerbert Xu struct sk_buff *skb; 797eb1d1641SHerbert Xu 7988ef2a9a5SYOSHIFUJI Hideaki skb = br_multicast_alloc_query(br, ip); 799eb1d1641SHerbert Xu if (!skb) 8008ef2a9a5SYOSHIFUJI Hideaki return; 801eb1d1641SHerbert Xu 802eb1d1641SHerbert Xu if (port) { 803eb1d1641SHerbert Xu __skb_push(skb, sizeof(struct ethhdr)); 804eb1d1641SHerbert Xu skb->dev = port->dev; 805713aefa3SJan Engelhardt NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev, 806eb1d1641SHerbert Xu dev_queue_xmit); 807eb1d1641SHerbert Xu } else 808eb1d1641SHerbert Xu netif_rx(skb); 8098ef2a9a5SYOSHIFUJI Hideaki } 810eb1d1641SHerbert Xu 8118ef2a9a5SYOSHIFUJI Hideaki static void br_multicast_send_query(struct net_bridge *br, 812cc0fdd80SLinus Lüssing struct net_bridge_port *port, 813cc0fdd80SLinus Lüssing struct bridge_mcast_query *query) 8148ef2a9a5SYOSHIFUJI Hideaki { 8158ef2a9a5SYOSHIFUJI Hideaki unsigned long time; 8168ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 817cc0fdd80SLinus Lüssing struct bridge_mcast_querier *querier = NULL; 8188ef2a9a5SYOSHIFUJI Hideaki 8198ef2a9a5SYOSHIFUJI Hideaki if (!netif_running(br->dev) || br->multicast_disabled || 820cc0fdd80SLinus Lüssing !br->multicast_querier) 8218ef2a9a5SYOSHIFUJI Hideaki return; 8228ef2a9a5SYOSHIFUJI Hideaki 82308b202b6SYOSHIFUJI Hideaki memset(&br_group.u, 0, sizeof(br_group.u)); 8248ef2a9a5SYOSHIFUJI Hideaki 825cc0fdd80SLinus Lüssing if (port ? (query == &port->ip4_query) : 826cc0fdd80SLinus Lüssing (query == &br->ip4_query)) { 827cc0fdd80SLinus Lüssing querier = &br->ip4_querier; 82808b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 829dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 830cc0fdd80SLinus Lüssing } else { 831cc0fdd80SLinus Lüssing querier = &br->ip6_querier; 83208b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 83308b202b6SYOSHIFUJI Hideaki #endif 834eb1d1641SHerbert Xu } 835eb1d1641SHerbert Xu 836cc0fdd80SLinus Lüssing if (!querier || timer_pending(&querier->timer)) 837cc0fdd80SLinus Lüssing return; 838cc0fdd80SLinus Lüssing 839cc0fdd80SLinus Lüssing __br_multicast_send_query(br, port, &br_group); 840cc0fdd80SLinus Lüssing 841cc0fdd80SLinus Lüssing time = jiffies; 842cc0fdd80SLinus Lüssing time += query->startup_sent < br->multicast_startup_query_count ? 843cc0fdd80SLinus Lüssing br->multicast_startup_query_interval : 844cc0fdd80SLinus Lüssing br->multicast_query_interval; 845cc0fdd80SLinus Lüssing mod_timer(&query->timer, time); 846cc0fdd80SLinus Lüssing } 847cc0fdd80SLinus Lüssing 848cc0fdd80SLinus Lüssing static void br_multicast_port_query_expired(struct net_bridge_port *port, 849cc0fdd80SLinus Lüssing struct bridge_mcast_query *query) 850eb1d1641SHerbert Xu { 851eb1d1641SHerbert Xu struct net_bridge *br = port->br; 852eb1d1641SHerbert Xu 853eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 85402a780c0SDan Carpenter if (port->state == BR_STATE_DISABLED || 85502a780c0SDan Carpenter port->state == BR_STATE_BLOCKING) 856eb1d1641SHerbert Xu goto out; 857eb1d1641SHerbert Xu 858cc0fdd80SLinus Lüssing if (query->startup_sent < br->multicast_startup_query_count) 859cc0fdd80SLinus Lüssing query->startup_sent++; 860eb1d1641SHerbert Xu 861cc0fdd80SLinus Lüssing br_multicast_send_query(port->br, port, query); 862eb1d1641SHerbert Xu 863eb1d1641SHerbert Xu out: 864eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 865eb1d1641SHerbert Xu } 866eb1d1641SHerbert Xu 867cc0fdd80SLinus Lüssing static void br_ip4_multicast_port_query_expired(unsigned long data) 868cc0fdd80SLinus Lüssing { 869cc0fdd80SLinus Lüssing struct net_bridge_port *port = (void *)data; 870cc0fdd80SLinus Lüssing 871cc0fdd80SLinus Lüssing br_multicast_port_query_expired(port, &port->ip4_query); 872cc0fdd80SLinus Lüssing } 873cc0fdd80SLinus Lüssing 874cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 875cc0fdd80SLinus Lüssing static void br_ip6_multicast_port_query_expired(unsigned long data) 876cc0fdd80SLinus Lüssing { 877cc0fdd80SLinus Lüssing struct net_bridge_port *port = (void *)data; 878cc0fdd80SLinus Lüssing 879cc0fdd80SLinus Lüssing br_multicast_port_query_expired(port, &port->ip6_query); 880cc0fdd80SLinus Lüssing } 881cc0fdd80SLinus Lüssing #endif 882cc0fdd80SLinus Lüssing 883eb1d1641SHerbert Xu void br_multicast_add_port(struct net_bridge_port *port) 884eb1d1641SHerbert Xu { 885eb1d1641SHerbert Xu port->multicast_router = 1; 886eb1d1641SHerbert Xu 887eb1d1641SHerbert Xu setup_timer(&port->multicast_router_timer, br_multicast_router_expired, 888eb1d1641SHerbert Xu (unsigned long)port); 889cc0fdd80SLinus Lüssing setup_timer(&port->ip4_query.timer, br_ip4_multicast_port_query_expired, 890cc0fdd80SLinus Lüssing (unsigned long)port); 891cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 892cc0fdd80SLinus Lüssing setup_timer(&port->ip6_query.timer, br_ip6_multicast_port_query_expired, 893cc0fdd80SLinus Lüssing (unsigned long)port); 894cc0fdd80SLinus Lüssing #endif 895eb1d1641SHerbert Xu } 896eb1d1641SHerbert Xu 897eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port) 898eb1d1641SHerbert Xu { 899eb1d1641SHerbert Xu del_timer_sync(&port->multicast_router_timer); 900eb1d1641SHerbert Xu } 901eb1d1641SHerbert Xu 902cc0fdd80SLinus Lüssing static void br_multicast_enable(struct bridge_mcast_query *query) 903561f1103SHerbert Xu { 904cc0fdd80SLinus Lüssing query->startup_sent = 0; 905561f1103SHerbert Xu 906cc0fdd80SLinus Lüssing if (try_to_del_timer_sync(&query->timer) >= 0 || 907cc0fdd80SLinus Lüssing del_timer(&query->timer)) 908cc0fdd80SLinus Lüssing mod_timer(&query->timer, jiffies); 909561f1103SHerbert Xu } 910561f1103SHerbert Xu 911eb1d1641SHerbert Xu void br_multicast_enable_port(struct net_bridge_port *port) 912eb1d1641SHerbert Xu { 913eb1d1641SHerbert Xu struct net_bridge *br = port->br; 914eb1d1641SHerbert Xu 915eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 916eb1d1641SHerbert Xu if (br->multicast_disabled || !netif_running(br->dev)) 917eb1d1641SHerbert Xu goto out; 918eb1d1641SHerbert Xu 919cc0fdd80SLinus Lüssing br_multicast_enable(&port->ip4_query); 920cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 921cc0fdd80SLinus Lüssing br_multicast_enable(&port->ip6_query); 922cc0fdd80SLinus Lüssing #endif 923eb1d1641SHerbert Xu 924eb1d1641SHerbert Xu out: 925eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 926eb1d1641SHerbert Xu } 927eb1d1641SHerbert Xu 928eb1d1641SHerbert Xu void br_multicast_disable_port(struct net_bridge_port *port) 929eb1d1641SHerbert Xu { 930eb1d1641SHerbert Xu struct net_bridge *br = port->br; 931eb1d1641SHerbert Xu struct net_bridge_port_group *pg; 932b67bfe0dSSasha Levin struct hlist_node *n; 933eb1d1641SHerbert Xu 934eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 935b67bfe0dSSasha Levin hlist_for_each_entry_safe(pg, n, &port->mglist, mglist) 936eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 937eb1d1641SHerbert Xu 938eb1d1641SHerbert Xu if (!hlist_unhashed(&port->rlist)) 939eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 940eb1d1641SHerbert Xu del_timer(&port->multicast_router_timer); 941cc0fdd80SLinus Lüssing del_timer(&port->ip4_query.timer); 942cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 943cc0fdd80SLinus Lüssing del_timer(&port->ip6_query.timer); 944cc0fdd80SLinus Lüssing #endif 945eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 946eb1d1641SHerbert Xu } 947eb1d1641SHerbert Xu 9488ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_igmp3_report(struct net_bridge *br, 949eb1d1641SHerbert Xu struct net_bridge_port *port, 95006499098SVlad Yasevich struct sk_buff *skb, 95106499098SVlad Yasevich u16 vid) 952eb1d1641SHerbert Xu { 953eb1d1641SHerbert Xu struct igmpv3_report *ih; 954eb1d1641SHerbert Xu struct igmpv3_grec *grec; 955eb1d1641SHerbert Xu int i; 956eb1d1641SHerbert Xu int len; 957eb1d1641SHerbert Xu int num; 958eb1d1641SHerbert Xu int type; 959eb1d1641SHerbert Xu int err = 0; 960eb1d1641SHerbert Xu __be32 group; 961eb1d1641SHerbert Xu 962eb1d1641SHerbert Xu if (!pskb_may_pull(skb, sizeof(*ih))) 963eb1d1641SHerbert Xu return -EINVAL; 964eb1d1641SHerbert Xu 965eb1d1641SHerbert Xu ih = igmpv3_report_hdr(skb); 966eb1d1641SHerbert Xu num = ntohs(ih->ngrec); 967eb1d1641SHerbert Xu len = sizeof(*ih); 968eb1d1641SHerbert Xu 969eb1d1641SHerbert Xu for (i = 0; i < num; i++) { 970eb1d1641SHerbert Xu len += sizeof(*grec); 971eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 972eb1d1641SHerbert Xu return -EINVAL; 973eb1d1641SHerbert Xu 974fd218cf9SHerbert Xu grec = (void *)(skb->data + len - sizeof(*grec)); 975eb1d1641SHerbert Xu group = grec->grec_mca; 976eb1d1641SHerbert Xu type = grec->grec_type; 977eb1d1641SHerbert Xu 9788eabf95cSEric Dumazet len += ntohs(grec->grec_nsrcs) * 4; 979eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 980eb1d1641SHerbert Xu return -EINVAL; 981eb1d1641SHerbert Xu 982eb1d1641SHerbert Xu /* We treat this as an IGMPv2 report for now. */ 983eb1d1641SHerbert Xu switch (type) { 984eb1d1641SHerbert Xu case IGMPV3_MODE_IS_INCLUDE: 985eb1d1641SHerbert Xu case IGMPV3_MODE_IS_EXCLUDE: 986eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_INCLUDE: 987eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_EXCLUDE: 988eb1d1641SHerbert Xu case IGMPV3_ALLOW_NEW_SOURCES: 989eb1d1641SHerbert Xu case IGMPV3_BLOCK_OLD_SOURCES: 990eb1d1641SHerbert Xu break; 991eb1d1641SHerbert Xu 992eb1d1641SHerbert Xu default: 993eb1d1641SHerbert Xu continue; 994eb1d1641SHerbert Xu } 995eb1d1641SHerbert Xu 996b0e9a30dSVlad Yasevich err = br_ip4_multicast_add_group(br, port, group, vid); 997eb1d1641SHerbert Xu if (err) 998eb1d1641SHerbert Xu break; 999eb1d1641SHerbert Xu } 1000eb1d1641SHerbert Xu 1001eb1d1641SHerbert Xu return err; 1002eb1d1641SHerbert Xu } 1003eb1d1641SHerbert Xu 1004dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 100508b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_mld2_report(struct net_bridge *br, 100608b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 100706499098SVlad Yasevich struct sk_buff *skb, 100806499098SVlad Yasevich u16 vid) 100908b202b6SYOSHIFUJI Hideaki { 101008b202b6SYOSHIFUJI Hideaki struct icmp6hdr *icmp6h; 101108b202b6SYOSHIFUJI Hideaki struct mld2_grec *grec; 101208b202b6SYOSHIFUJI Hideaki int i; 101308b202b6SYOSHIFUJI Hideaki int len; 101408b202b6SYOSHIFUJI Hideaki int num; 101508b202b6SYOSHIFUJI Hideaki int err = 0; 101608b202b6SYOSHIFUJI Hideaki 101708b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*icmp6h))) 101808b202b6SYOSHIFUJI Hideaki return -EINVAL; 101908b202b6SYOSHIFUJI Hideaki 102008b202b6SYOSHIFUJI Hideaki icmp6h = icmp6_hdr(skb); 102108b202b6SYOSHIFUJI Hideaki num = ntohs(icmp6h->icmp6_dataun.un_data16[1]); 102208b202b6SYOSHIFUJI Hideaki len = sizeof(*icmp6h); 102308b202b6SYOSHIFUJI Hideaki 102408b202b6SYOSHIFUJI Hideaki for (i = 0; i < num; i++) { 102508b202b6SYOSHIFUJI Hideaki __be16 *nsrcs, _nsrcs; 102608b202b6SYOSHIFUJI Hideaki 102708b202b6SYOSHIFUJI Hideaki nsrcs = skb_header_pointer(skb, 102808b202b6SYOSHIFUJI Hideaki len + offsetof(struct mld2_grec, 1029649e984dSLinus Lüssing grec_nsrcs), 103008b202b6SYOSHIFUJI Hideaki sizeof(_nsrcs), &_nsrcs); 103108b202b6SYOSHIFUJI Hideaki if (!nsrcs) 103208b202b6SYOSHIFUJI Hideaki return -EINVAL; 103308b202b6SYOSHIFUJI Hideaki 103408b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, 103508b202b6SYOSHIFUJI Hideaki len + sizeof(*grec) + 1036d41db9f3SLinus Lüssing sizeof(struct in6_addr) * ntohs(*nsrcs))) 103708b202b6SYOSHIFUJI Hideaki return -EINVAL; 103808b202b6SYOSHIFUJI Hideaki 103908b202b6SYOSHIFUJI Hideaki grec = (struct mld2_grec *)(skb->data + len); 1040d41db9f3SLinus Lüssing len += sizeof(*grec) + 1041d41db9f3SLinus Lüssing sizeof(struct in6_addr) * ntohs(*nsrcs); 104208b202b6SYOSHIFUJI Hideaki 104308b202b6SYOSHIFUJI Hideaki /* We treat these as MLDv1 reports for now. */ 104408b202b6SYOSHIFUJI Hideaki switch (grec->grec_type) { 104508b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_INCLUDE: 104608b202b6SYOSHIFUJI Hideaki case MLD2_MODE_IS_EXCLUDE: 104708b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_INCLUDE: 104808b202b6SYOSHIFUJI Hideaki case MLD2_CHANGE_TO_EXCLUDE: 104908b202b6SYOSHIFUJI Hideaki case MLD2_ALLOW_NEW_SOURCES: 105008b202b6SYOSHIFUJI Hideaki case MLD2_BLOCK_OLD_SOURCES: 105108b202b6SYOSHIFUJI Hideaki break; 105208b202b6SYOSHIFUJI Hideaki 105308b202b6SYOSHIFUJI Hideaki default: 105408b202b6SYOSHIFUJI Hideaki continue; 105508b202b6SYOSHIFUJI Hideaki } 105608b202b6SYOSHIFUJI Hideaki 1057b0e9a30dSVlad Yasevich err = br_ip6_multicast_add_group(br, port, &grec->grec_mca, 1058b0e9a30dSVlad Yasevich vid); 105908b202b6SYOSHIFUJI Hideaki if (!err) 106008b202b6SYOSHIFUJI Hideaki break; 106108b202b6SYOSHIFUJI Hideaki } 106208b202b6SYOSHIFUJI Hideaki 106308b202b6SYOSHIFUJI Hideaki return err; 106408b202b6SYOSHIFUJI Hideaki } 106508b202b6SYOSHIFUJI Hideaki #endif 106608b202b6SYOSHIFUJI Hideaki 1067cc0fdd80SLinus Lüssing static void 1068cc0fdd80SLinus Lüssing br_multicast_update_querier_timer(struct net_bridge *br, 1069cc0fdd80SLinus Lüssing struct bridge_mcast_querier *querier, 1070b00589afSLinus Lüssing unsigned long max_delay) 1071b00589afSLinus Lüssing { 1072cc0fdd80SLinus Lüssing if (!timer_pending(&querier->timer)) 1073cc0fdd80SLinus Lüssing querier->delay_time = jiffies + max_delay; 1074b00589afSLinus Lüssing 1075cc0fdd80SLinus Lüssing mod_timer(&querier->timer, jiffies + br->multicast_querier_interval); 1076b00589afSLinus Lüssing } 1077b00589afSLinus Lüssing 10787e80c124Sstephen hemminger /* 10797c77602fSCong Wang * Add port to router_list 10807e80c124Sstephen hemminger * list is maintained ordered by pointer value 10817e80c124Sstephen hemminger * and locked by br->multicast_lock and RCU 10827e80c124Sstephen hemminger */ 10830909e117SHerbert Xu static void br_multicast_add_router(struct net_bridge *br, 10840909e117SHerbert Xu struct net_bridge_port *port) 10850909e117SHerbert Xu { 1086dcdca2c4Sstephen hemminger struct net_bridge_port *p; 1087b67bfe0dSSasha Levin struct hlist_node *slot = NULL; 10880909e117SHerbert Xu 1089b67bfe0dSSasha Levin hlist_for_each_entry(p, &br->router_list, rlist) { 10907e80c124Sstephen hemminger if ((unsigned long) port >= (unsigned long) p) 10917e80c124Sstephen hemminger break; 1092b67bfe0dSSasha Levin slot = &p->rlist; 1093dcdca2c4Sstephen hemminger } 10940909e117SHerbert Xu 10957e80c124Sstephen hemminger if (slot) 10967e80c124Sstephen hemminger hlist_add_after_rcu(slot, &port->rlist); 1097dcdca2c4Sstephen hemminger else 1098dcdca2c4Sstephen hemminger hlist_add_head_rcu(&port->rlist, &br->router_list); 10990909e117SHerbert Xu } 11000909e117SHerbert Xu 1101eb1d1641SHerbert Xu static void br_multicast_mark_router(struct net_bridge *br, 1102eb1d1641SHerbert Xu struct net_bridge_port *port) 1103eb1d1641SHerbert Xu { 1104eb1d1641SHerbert Xu unsigned long now = jiffies; 1105eb1d1641SHerbert Xu 1106eb1d1641SHerbert Xu if (!port) { 1107eb1d1641SHerbert Xu if (br->multicast_router == 1) 1108eb1d1641SHerbert Xu mod_timer(&br->multicast_router_timer, 1109eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1110eb1d1641SHerbert Xu return; 1111eb1d1641SHerbert Xu } 1112eb1d1641SHerbert Xu 1113eb1d1641SHerbert Xu if (port->multicast_router != 1) 1114eb1d1641SHerbert Xu return; 1115eb1d1641SHerbert Xu 1116eb1d1641SHerbert Xu if (!hlist_unhashed(&port->rlist)) 1117eb1d1641SHerbert Xu goto timer; 1118eb1d1641SHerbert Xu 11190909e117SHerbert Xu br_multicast_add_router(br, port); 1120eb1d1641SHerbert Xu 1121eb1d1641SHerbert Xu timer: 1122eb1d1641SHerbert Xu mod_timer(&port->multicast_router_timer, 1123eb1d1641SHerbert Xu now + br->multicast_querier_interval); 1124eb1d1641SHerbert Xu } 1125eb1d1641SHerbert Xu 1126eb1d1641SHerbert Xu static void br_multicast_query_received(struct net_bridge *br, 1127eb1d1641SHerbert Xu struct net_bridge_port *port, 1128cc0fdd80SLinus Lüssing struct bridge_mcast_querier *querier, 1129b00589afSLinus Lüssing int saddr, 1130b00589afSLinus Lüssing unsigned long max_delay) 1131eb1d1641SHerbert Xu { 1132eb1d1641SHerbert Xu if (saddr) 1133cc0fdd80SLinus Lüssing br_multicast_update_querier_timer(br, querier, max_delay); 1134cc0fdd80SLinus Lüssing else if (timer_pending(&querier->timer)) 1135eb1d1641SHerbert Xu return; 1136eb1d1641SHerbert Xu 1137eb1d1641SHerbert Xu br_multicast_mark_router(br, port); 1138eb1d1641SHerbert Xu } 1139eb1d1641SHerbert Xu 11408ef2a9a5SYOSHIFUJI Hideaki static int br_ip4_multicast_query(struct net_bridge *br, 1141eb1d1641SHerbert Xu struct net_bridge_port *port, 114206499098SVlad Yasevich struct sk_buff *skb, 114306499098SVlad Yasevich u16 vid) 1144eb1d1641SHerbert Xu { 1145b71d1d42SEric Dumazet const struct iphdr *iph = ip_hdr(skb); 1146eb1d1641SHerbert Xu struct igmphdr *ih = igmp_hdr(skb); 1147eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1148eb1d1641SHerbert Xu struct igmpv3_query *ih3; 1149eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1150e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 1151eb1d1641SHerbert Xu unsigned long max_delay; 1152eb1d1641SHerbert Xu unsigned long now = jiffies; 1153eb1d1641SHerbert Xu __be32 group; 1154bec68ff1SYOSHIFUJI Hideaki int err = 0; 1155eb1d1641SHerbert Xu 1156eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1157eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1158eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 1159eb1d1641SHerbert Xu goto out; 1160eb1d1641SHerbert Xu 1161eb1d1641SHerbert Xu group = ih->group; 1162eb1d1641SHerbert Xu 1163eb1d1641SHerbert Xu if (skb->len == sizeof(*ih)) { 1164eb1d1641SHerbert Xu max_delay = ih->code * (HZ / IGMP_TIMER_SCALE); 1165eb1d1641SHerbert Xu 1166eb1d1641SHerbert Xu if (!max_delay) { 1167eb1d1641SHerbert Xu max_delay = 10 * HZ; 1168eb1d1641SHerbert Xu group = 0; 1169eb1d1641SHerbert Xu } 1170eb1d1641SHerbert Xu } else { 1171bec68ff1SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) { 1172bec68ff1SYOSHIFUJI Hideaki err = -EINVAL; 1173bec68ff1SYOSHIFUJI Hideaki goto out; 1174bec68ff1SYOSHIFUJI Hideaki } 1175eb1d1641SHerbert Xu 1176eb1d1641SHerbert Xu ih3 = igmpv3_query_hdr(skb); 1177eb1d1641SHerbert Xu if (ih3->nsrcs) 1178bec68ff1SYOSHIFUJI Hideaki goto out; 1179eb1d1641SHerbert Xu 11800ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 max_delay = ih3->code ? 11810ba8c9ecSYOSHIFUJI Hideaki / 吉藤英明 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1; 1182eb1d1641SHerbert Xu } 1183eb1d1641SHerbert Xu 1184cc0fdd80SLinus Lüssing br_multicast_query_received(br, port, &br->ip4_querier, !!iph->saddr, 1185cc0fdd80SLinus Lüssing max_delay); 1186b00589afSLinus Lüssing 1187eb1d1641SHerbert Xu if (!group) 1188eb1d1641SHerbert Xu goto out; 1189eb1d1641SHerbert Xu 1190b0e9a30dSVlad Yasevich mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid); 1191eb1d1641SHerbert Xu if (!mp) 1192eb1d1641SHerbert Xu goto out; 1193eb1d1641SHerbert Xu 1194eb1d1641SHerbert Xu max_delay *= br->multicast_last_member_count; 1195eb1d1641SHerbert Xu 11968a870178SHerbert Xu if (mp->mglist && 1197eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1198eb1d1641SHerbert Xu time_after(mp->timer.expires, now + max_delay) : 1199eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) 1200eb1d1641SHerbert Xu mod_timer(&mp->timer, now + max_delay); 1201eb1d1641SHerbert Xu 1202e8051688SEric Dumazet for (pp = &mp->ports; 1203e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 1204e8051688SEric Dumazet pp = &p->next) { 1205eb1d1641SHerbert Xu if (timer_pending(&p->timer) ? 1206eb1d1641SHerbert Xu time_after(p->timer.expires, now + max_delay) : 1207eb1d1641SHerbert Xu try_to_del_timer_sync(&p->timer) >= 0) 120824f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 1209eb1d1641SHerbert Xu } 1210eb1d1641SHerbert Xu 1211eb1d1641SHerbert Xu out: 1212eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1213bec68ff1SYOSHIFUJI Hideaki return err; 1214eb1d1641SHerbert Xu } 1215eb1d1641SHerbert Xu 1216dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 121708b202b6SYOSHIFUJI Hideaki static int br_ip6_multicast_query(struct net_bridge *br, 121808b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 121906499098SVlad Yasevich struct sk_buff *skb, 122006499098SVlad Yasevich u16 vid) 122108b202b6SYOSHIFUJI Hideaki { 1222b71d1d42SEric Dumazet const struct ipv6hdr *ip6h = ipv6_hdr(skb); 1223eca2a43bSstephen hemminger struct mld_msg *mld; 122408b202b6SYOSHIFUJI Hideaki struct net_bridge_mdb_entry *mp; 122508b202b6SYOSHIFUJI Hideaki struct mld2_query *mld2q; 1226e8051688SEric Dumazet struct net_bridge_port_group *p; 1227e8051688SEric Dumazet struct net_bridge_port_group __rcu **pp; 122808b202b6SYOSHIFUJI Hideaki unsigned long max_delay; 122908b202b6SYOSHIFUJI Hideaki unsigned long now = jiffies; 1230b71d1d42SEric Dumazet const struct in6_addr *group = NULL; 123108b202b6SYOSHIFUJI Hideaki int err = 0; 123208b202b6SYOSHIFUJI Hideaki 123308b202b6SYOSHIFUJI Hideaki spin_lock(&br->multicast_lock); 123408b202b6SYOSHIFUJI Hideaki if (!netif_running(br->dev) || 123508b202b6SYOSHIFUJI Hideaki (port && port->state == BR_STATE_DISABLED)) 123608b202b6SYOSHIFUJI Hideaki goto out; 123708b202b6SYOSHIFUJI Hideaki 123808b202b6SYOSHIFUJI Hideaki if (skb->len == sizeof(*mld)) { 123908b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*mld))) { 124008b202b6SYOSHIFUJI Hideaki err = -EINVAL; 124108b202b6SYOSHIFUJI Hideaki goto out; 124208b202b6SYOSHIFUJI Hideaki } 124308b202b6SYOSHIFUJI Hideaki mld = (struct mld_msg *) icmp6_hdr(skb); 12444715213dSLi RongQing max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay)); 124508b202b6SYOSHIFUJI Hideaki if (max_delay) 124608b202b6SYOSHIFUJI Hideaki group = &mld->mld_mca; 1247248ba8ecSLinus Lüssing } else { 124808b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*mld2q))) { 124908b202b6SYOSHIFUJI Hideaki err = -EINVAL; 125008b202b6SYOSHIFUJI Hideaki goto out; 125108b202b6SYOSHIFUJI Hideaki } 125208b202b6SYOSHIFUJI Hideaki mld2q = (struct mld2_query *)icmp6_hdr(skb); 125308b202b6SYOSHIFUJI Hideaki if (!mld2q->mld2q_nsrcs) 125408b202b6SYOSHIFUJI Hideaki group = &mld2q->mld2q_mca; 1255e3f5b170SDaniel Borkmann 1256e3f5b170SDaniel Borkmann max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL); 125708b202b6SYOSHIFUJI Hideaki } 125808b202b6SYOSHIFUJI Hideaki 1259cc0fdd80SLinus Lüssing br_multicast_query_received(br, port, &br->ip6_querier, 1260cc0fdd80SLinus Lüssing !ipv6_addr_any(&ip6h->saddr), max_delay); 1261b00589afSLinus Lüssing 126208b202b6SYOSHIFUJI Hideaki if (!group) 126308b202b6SYOSHIFUJI Hideaki goto out; 126408b202b6SYOSHIFUJI Hideaki 1265b0e9a30dSVlad Yasevich mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid); 126608b202b6SYOSHIFUJI Hideaki if (!mp) 126708b202b6SYOSHIFUJI Hideaki goto out; 126808b202b6SYOSHIFUJI Hideaki 126908b202b6SYOSHIFUJI Hideaki max_delay *= br->multicast_last_member_count; 12708a870178SHerbert Xu if (mp->mglist && 127108b202b6SYOSHIFUJI Hideaki (timer_pending(&mp->timer) ? 127208b202b6SYOSHIFUJI Hideaki time_after(mp->timer.expires, now + max_delay) : 127308b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&mp->timer) >= 0)) 127408b202b6SYOSHIFUJI Hideaki mod_timer(&mp->timer, now + max_delay); 127508b202b6SYOSHIFUJI Hideaki 1276e8051688SEric Dumazet for (pp = &mp->ports; 1277e8051688SEric Dumazet (p = mlock_dereference(*pp, br)) != NULL; 1278e8051688SEric Dumazet pp = &p->next) { 127908b202b6SYOSHIFUJI Hideaki if (timer_pending(&p->timer) ? 128008b202b6SYOSHIFUJI Hideaki time_after(p->timer.expires, now + max_delay) : 128108b202b6SYOSHIFUJI Hideaki try_to_del_timer_sync(&p->timer) >= 0) 128224f9cdcbSHerbert Xu mod_timer(&p->timer, now + max_delay); 128308b202b6SYOSHIFUJI Hideaki } 128408b202b6SYOSHIFUJI Hideaki 128508b202b6SYOSHIFUJI Hideaki out: 128608b202b6SYOSHIFUJI Hideaki spin_unlock(&br->multicast_lock); 128708b202b6SYOSHIFUJI Hideaki return err; 128808b202b6SYOSHIFUJI Hideaki } 128908b202b6SYOSHIFUJI Hideaki #endif 129008b202b6SYOSHIFUJI Hideaki 1291eb1d1641SHerbert Xu static void br_multicast_leave_group(struct net_bridge *br, 1292eb1d1641SHerbert Xu struct net_bridge_port *port, 1293cc0fdd80SLinus Lüssing struct br_ip *group, 1294cc0fdd80SLinus Lüssing struct bridge_mcast_querier *querier, 1295cc0fdd80SLinus Lüssing struct bridge_mcast_query *query) 1296eb1d1641SHerbert Xu { 1297eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 1298eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1299eb1d1641SHerbert Xu struct net_bridge_port_group *p; 1300eb1d1641SHerbert Xu unsigned long now; 1301eb1d1641SHerbert Xu unsigned long time; 1302eb1d1641SHerbert Xu 1303eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1304eb1d1641SHerbert Xu if (!netif_running(br->dev) || 1305eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED) || 1306cc0fdd80SLinus Lüssing timer_pending(&querier->timer)) 1307eb1d1641SHerbert Xu goto out; 1308eb1d1641SHerbert Xu 1309e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1310eb1d1641SHerbert Xu mp = br_mdb_ip_get(mdb, group); 1311eb1d1641SHerbert Xu if (!mp) 1312eb1d1641SHerbert Xu goto out; 1313eb1d1641SHerbert Xu 1314cc0fdd80SLinus Lüssing if (br->multicast_querier) { 13156b7df111SCong Wang __br_multicast_send_query(br, port, &mp->addr); 13166b7df111SCong Wang 13176b7df111SCong Wang time = jiffies + br->multicast_last_member_count * 13186b7df111SCong Wang br->multicast_last_member_interval; 1319cc0fdd80SLinus Lüssing 1320cc0fdd80SLinus Lüssing mod_timer(&query->timer, time); 13216b7df111SCong Wang 13226b7df111SCong Wang for (p = mlock_dereference(mp->ports, br); 13236b7df111SCong Wang p != NULL; 13246b7df111SCong Wang p = mlock_dereference(p->next, br)) { 13256b7df111SCong Wang if (p->port != port) 13266b7df111SCong Wang continue; 13276b7df111SCong Wang 13286b7df111SCong Wang if (!hlist_unhashed(&p->mglist) && 13296b7df111SCong Wang (timer_pending(&p->timer) ? 13306b7df111SCong Wang time_after(p->timer.expires, time) : 13316b7df111SCong Wang try_to_del_timer_sync(&p->timer) >= 0)) { 13326b7df111SCong Wang mod_timer(&p->timer, time); 13336b7df111SCong Wang } 13346b7df111SCong Wang 13356b7df111SCong Wang break; 13366b7df111SCong Wang } 13376b7df111SCong Wang } 13386b7df111SCong Wang 1339c2d3babfSDavid S. Miller if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) { 134050426b59SAmerigo Wang struct net_bridge_port_group __rcu **pp; 134150426b59SAmerigo Wang 134250426b59SAmerigo Wang for (pp = &mp->ports; 134350426b59SAmerigo Wang (p = mlock_dereference(*pp, br)) != NULL; 134450426b59SAmerigo Wang pp = &p->next) { 134550426b59SAmerigo Wang if (p->port != port) 134650426b59SAmerigo Wang continue; 134750426b59SAmerigo Wang 134850426b59SAmerigo Wang rcu_assign_pointer(*pp, p->next); 134950426b59SAmerigo Wang hlist_del_init(&p->mglist); 135050426b59SAmerigo Wang del_timer(&p->timer); 135150426b59SAmerigo Wang call_rcu_bh(&p->rcu, br_multicast_free_pg); 135237a393bcSCong Wang br_mdb_notify(br->dev, port, group, RTM_DELMDB); 135350426b59SAmerigo Wang 1354454594f3SLinus Lüssing if (!mp->ports && !mp->mglist && 135550426b59SAmerigo Wang netif_running(br->dev)) 135650426b59SAmerigo Wang mod_timer(&mp->timer, jiffies); 135750426b59SAmerigo Wang } 135850426b59SAmerigo Wang goto out; 135950426b59SAmerigo Wang } 136050426b59SAmerigo Wang 1361eb1d1641SHerbert Xu now = jiffies; 1362eb1d1641SHerbert Xu time = now + br->multicast_last_member_count * 1363eb1d1641SHerbert Xu br->multicast_last_member_interval; 1364eb1d1641SHerbert Xu 1365eb1d1641SHerbert Xu if (!port) { 1366454594f3SLinus Lüssing if (mp->mglist && 1367eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 1368eb1d1641SHerbert Xu time_after(mp->timer.expires, time) : 1369eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) { 1370eb1d1641SHerbert Xu mod_timer(&mp->timer, time); 1371eb1d1641SHerbert Xu } 1372454594f3SLinus Lüssing 1373454594f3SLinus Lüssing goto out; 1374454594f3SLinus Lüssing } 1375454594f3SLinus Lüssing 1376454594f3SLinus Lüssing for (p = mlock_dereference(mp->ports, br); 1377454594f3SLinus Lüssing p != NULL; 1378454594f3SLinus Lüssing p = mlock_dereference(p->next, br)) { 1379454594f3SLinus Lüssing if (p->port != port) 1380454594f3SLinus Lüssing continue; 1381454594f3SLinus Lüssing 1382454594f3SLinus Lüssing if (!hlist_unhashed(&p->mglist) && 1383454594f3SLinus Lüssing (timer_pending(&p->timer) ? 1384454594f3SLinus Lüssing time_after(p->timer.expires, time) : 1385454594f3SLinus Lüssing try_to_del_timer_sync(&p->timer) >= 0)) { 1386454594f3SLinus Lüssing mod_timer(&p->timer, time); 1387454594f3SLinus Lüssing } 1388454594f3SLinus Lüssing 1389454594f3SLinus Lüssing break; 1390eb1d1641SHerbert Xu } 1391eb1d1641SHerbert Xu out: 1392eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1393eb1d1641SHerbert Xu } 1394eb1d1641SHerbert Xu 13958ef2a9a5SYOSHIFUJI Hideaki static void br_ip4_multicast_leave_group(struct net_bridge *br, 13968ef2a9a5SYOSHIFUJI Hideaki struct net_bridge_port *port, 1397b0e9a30dSVlad Yasevich __be32 group, 1398b0e9a30dSVlad Yasevich __u16 vid) 13998ef2a9a5SYOSHIFUJI Hideaki { 14008ef2a9a5SYOSHIFUJI Hideaki struct br_ip br_group; 1401cc0fdd80SLinus Lüssing struct bridge_mcast_query *query = port ? &port->ip4_query : 1402cc0fdd80SLinus Lüssing &br->ip4_query; 14038ef2a9a5SYOSHIFUJI Hideaki 14048ef2a9a5SYOSHIFUJI Hideaki if (ipv4_is_local_multicast(group)) 14058ef2a9a5SYOSHIFUJI Hideaki return; 14068ef2a9a5SYOSHIFUJI Hideaki 14078ef2a9a5SYOSHIFUJI Hideaki br_group.u.ip4 = group; 14088ef2a9a5SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IP); 1409b0e9a30dSVlad Yasevich br_group.vid = vid; 14108ef2a9a5SYOSHIFUJI Hideaki 1411cc0fdd80SLinus Lüssing br_multicast_leave_group(br, port, &br_group, &br->ip4_querier, query); 14128ef2a9a5SYOSHIFUJI Hideaki } 14138ef2a9a5SYOSHIFUJI Hideaki 1414dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 141508b202b6SYOSHIFUJI Hideaki static void br_ip6_multicast_leave_group(struct net_bridge *br, 141608b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 1417b0e9a30dSVlad Yasevich const struct in6_addr *group, 1418b0e9a30dSVlad Yasevich __u16 vid) 141908b202b6SYOSHIFUJI Hideaki { 142008b202b6SYOSHIFUJI Hideaki struct br_ip br_group; 1421cc0fdd80SLinus Lüssing struct bridge_mcast_query *query = port ? &port->ip6_query : 1422cc0fdd80SLinus Lüssing &br->ip6_query; 1423cc0fdd80SLinus Lüssing 142408b202b6SYOSHIFUJI Hideaki 14253c3769e6SLinus Lüssing if (ipv6_addr_is_ll_all_nodes(group)) 142608b202b6SYOSHIFUJI Hideaki return; 142708b202b6SYOSHIFUJI Hideaki 14284e3fd7a0SAlexey Dobriyan br_group.u.ip6 = *group; 142908b202b6SYOSHIFUJI Hideaki br_group.proto = htons(ETH_P_IPV6); 1430b0e9a30dSVlad Yasevich br_group.vid = vid; 143108b202b6SYOSHIFUJI Hideaki 1432cc0fdd80SLinus Lüssing br_multicast_leave_group(br, port, &br_group, &br->ip6_querier, query); 143308b202b6SYOSHIFUJI Hideaki } 143408b202b6SYOSHIFUJI Hideaki #endif 14358ef2a9a5SYOSHIFUJI Hideaki 1436eb1d1641SHerbert Xu static int br_multicast_ipv4_rcv(struct net_bridge *br, 1437eb1d1641SHerbert Xu struct net_bridge_port *port, 143806499098SVlad Yasevich struct sk_buff *skb, 143906499098SVlad Yasevich u16 vid) 1440eb1d1641SHerbert Xu { 1441eb1d1641SHerbert Xu struct sk_buff *skb2 = skb; 1442b71d1d42SEric Dumazet const struct iphdr *iph; 1443eb1d1641SHerbert Xu struct igmphdr *ih; 144495c96174SEric Dumazet unsigned int len; 144595c96174SEric Dumazet unsigned int offset; 1446eb1d1641SHerbert Xu int err; 1447eb1d1641SHerbert Xu 1448eb1d1641SHerbert Xu /* We treat OOM as packet loss for now. */ 1449eb1d1641SHerbert Xu if (!pskb_may_pull(skb, sizeof(*iph))) 1450eb1d1641SHerbert Xu return -EINVAL; 1451eb1d1641SHerbert Xu 1452eb1d1641SHerbert Xu iph = ip_hdr(skb); 1453eb1d1641SHerbert Xu 1454eb1d1641SHerbert Xu if (iph->ihl < 5 || iph->version != 4) 1455eb1d1641SHerbert Xu return -EINVAL; 1456eb1d1641SHerbert Xu 1457eb1d1641SHerbert Xu if (!pskb_may_pull(skb, ip_hdrlen(skb))) 1458eb1d1641SHerbert Xu return -EINVAL; 1459eb1d1641SHerbert Xu 1460eb1d1641SHerbert Xu iph = ip_hdr(skb); 1461eb1d1641SHerbert Xu 1462eb1d1641SHerbert Xu if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl))) 1463eb1d1641SHerbert Xu return -EINVAL; 1464eb1d1641SHerbert Xu 1465bd4265feSHerbert Xu if (iph->protocol != IPPROTO_IGMP) { 1466bf5e4dd6SAmerigo Wang if (!ipv4_is_local_multicast(iph->daddr)) 1467bd4265feSHerbert Xu BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 1468eb1d1641SHerbert Xu return 0; 1469bd4265feSHerbert Xu } 1470eb1d1641SHerbert Xu 1471eb1d1641SHerbert Xu len = ntohs(iph->tot_len); 1472eb1d1641SHerbert Xu if (skb->len < len || len < ip_hdrlen(skb)) 1473eb1d1641SHerbert Xu return -EINVAL; 1474eb1d1641SHerbert Xu 1475eb1d1641SHerbert Xu if (skb->len > len) { 1476eb1d1641SHerbert Xu skb2 = skb_clone(skb, GFP_ATOMIC); 1477eb1d1641SHerbert Xu if (!skb2) 1478eb1d1641SHerbert Xu return -ENOMEM; 1479eb1d1641SHerbert Xu 1480eb1d1641SHerbert Xu err = pskb_trim_rcsum(skb2, len); 1481eb1d1641SHerbert Xu if (err) 14828440853bSYOSHIFUJI Hideaki / 吉藤英明 goto err_out; 1483eb1d1641SHerbert Xu } 1484eb1d1641SHerbert Xu 1485eb1d1641SHerbert Xu len -= ip_hdrlen(skb2); 1486eb1d1641SHerbert Xu offset = skb_network_offset(skb2) + ip_hdrlen(skb2); 1487eb1d1641SHerbert Xu __skb_pull(skb2, offset); 1488eb1d1641SHerbert Xu skb_reset_transport_header(skb2); 1489eb1d1641SHerbert Xu 1490eb1d1641SHerbert Xu err = -EINVAL; 1491eb1d1641SHerbert Xu if (!pskb_may_pull(skb2, sizeof(*ih))) 1492eb1d1641SHerbert Xu goto out; 1493eb1d1641SHerbert Xu 1494eb1d1641SHerbert Xu switch (skb2->ip_summed) { 1495eb1d1641SHerbert Xu case CHECKSUM_COMPLETE: 1496eb1d1641SHerbert Xu if (!csum_fold(skb2->csum)) 1497eb1d1641SHerbert Xu break; 1498eb1d1641SHerbert Xu /* fall through */ 1499eb1d1641SHerbert Xu case CHECKSUM_NONE: 1500eb1d1641SHerbert Xu skb2->csum = 0; 1501eb1d1641SHerbert Xu if (skb_checksum_complete(skb2)) 15028440853bSYOSHIFUJI Hideaki / 吉藤英明 goto out; 1503eb1d1641SHerbert Xu } 1504eb1d1641SHerbert Xu 1505eb1d1641SHerbert Xu err = 0; 1506eb1d1641SHerbert Xu 1507eb1d1641SHerbert Xu BR_INPUT_SKB_CB(skb)->igmp = 1; 1508eb1d1641SHerbert Xu ih = igmp_hdr(skb2); 1509eb1d1641SHerbert Xu 1510eb1d1641SHerbert Xu switch (ih->type) { 1511eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_REPORT: 1512eb1d1641SHerbert Xu case IGMPV2_HOST_MEMBERSHIP_REPORT: 151362b2bcb4SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 1514b0e9a30dSVlad Yasevich err = br_ip4_multicast_add_group(br, port, ih->group, vid); 1515eb1d1641SHerbert Xu break; 1516eb1d1641SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 151706499098SVlad Yasevich err = br_ip4_multicast_igmp3_report(br, port, skb2, vid); 1518eb1d1641SHerbert Xu break; 1519eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_QUERY: 152006499098SVlad Yasevich err = br_ip4_multicast_query(br, port, skb2, vid); 1521eb1d1641SHerbert Xu break; 1522eb1d1641SHerbert Xu case IGMP_HOST_LEAVE_MESSAGE: 1523b0e9a30dSVlad Yasevich br_ip4_multicast_leave_group(br, port, ih->group, vid); 1524eb1d1641SHerbert Xu break; 1525eb1d1641SHerbert Xu } 1526eb1d1641SHerbert Xu 1527eb1d1641SHerbert Xu out: 1528eb1d1641SHerbert Xu __skb_push(skb2, offset); 15298440853bSYOSHIFUJI Hideaki / 吉藤英明 err_out: 1530eb1d1641SHerbert Xu if (skb2 != skb) 1531eb1d1641SHerbert Xu kfree_skb(skb2); 1532eb1d1641SHerbert Xu return err; 1533eb1d1641SHerbert Xu } 1534eb1d1641SHerbert Xu 1535dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 153608b202b6SYOSHIFUJI Hideaki static int br_multicast_ipv6_rcv(struct net_bridge *br, 153708b202b6SYOSHIFUJI Hideaki struct net_bridge_port *port, 153806499098SVlad Yasevich struct sk_buff *skb, 153906499098SVlad Yasevich u16 vid) 154008b202b6SYOSHIFUJI Hideaki { 15419d89081dSTomas Winkler struct sk_buff *skb2; 1542b71d1d42SEric Dumazet const struct ipv6hdr *ip6h; 154322df1331SEric Dumazet u8 icmp6_type; 154408b202b6SYOSHIFUJI Hideaki u8 nexthdr; 154575f2811cSJesse Gross __be16 frag_off; 154695c96174SEric Dumazet unsigned int len; 1547bb7a0bd6SKulikov Vasiliy int offset; 154808b202b6SYOSHIFUJI Hideaki int err; 154908b202b6SYOSHIFUJI Hideaki 155008b202b6SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(*ip6h))) 155108b202b6SYOSHIFUJI Hideaki return -EINVAL; 155208b202b6SYOSHIFUJI Hideaki 155308b202b6SYOSHIFUJI Hideaki ip6h = ipv6_hdr(skb); 155408b202b6SYOSHIFUJI Hideaki 155508b202b6SYOSHIFUJI Hideaki /* 155608b202b6SYOSHIFUJI Hideaki * We're interested in MLD messages only. 155708b202b6SYOSHIFUJI Hideaki * - Version is 6 155808b202b6SYOSHIFUJI Hideaki * - MLD has always Router Alert hop-by-hop option 155908b202b6SYOSHIFUJI Hideaki * - But we do not support jumbrograms. 156008b202b6SYOSHIFUJI Hideaki */ 15618fad9c39SLinus Lüssing if (ip6h->version != 6) 15628fad9c39SLinus Lüssing return 0; 15638fad9c39SLinus Lüssing 15648fad9c39SLinus Lüssing /* Prevent flooding this packet if there is no listener present */ 15653c3769e6SLinus Lüssing if (!ipv6_addr_is_ll_all_nodes(&ip6h->daddr)) 15668fad9c39SLinus Lüssing BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 15678fad9c39SLinus Lüssing 15688fad9c39SLinus Lüssing if (ip6h->nexthdr != IPPROTO_HOPOPTS || 156908b202b6SYOSHIFUJI Hideaki ip6h->payload_len == 0) 157008b202b6SYOSHIFUJI Hideaki return 0; 157108b202b6SYOSHIFUJI Hideaki 1572ff9a57a6SLinus Lüssing len = ntohs(ip6h->payload_len) + sizeof(*ip6h); 157308b202b6SYOSHIFUJI Hideaki if (skb->len < len) 157408b202b6SYOSHIFUJI Hideaki return -EINVAL; 157508b202b6SYOSHIFUJI Hideaki 157608b202b6SYOSHIFUJI Hideaki nexthdr = ip6h->nexthdr; 157775f2811cSJesse Gross offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr, &frag_off); 157808b202b6SYOSHIFUJI Hideaki 157908b202b6SYOSHIFUJI Hideaki if (offset < 0 || nexthdr != IPPROTO_ICMPV6) 158008b202b6SYOSHIFUJI Hideaki return 0; 158108b202b6SYOSHIFUJI Hideaki 158208b202b6SYOSHIFUJI Hideaki /* Okay, we found ICMPv6 header */ 158308b202b6SYOSHIFUJI Hideaki skb2 = skb_clone(skb, GFP_ATOMIC); 158408b202b6SYOSHIFUJI Hideaki if (!skb2) 158508b202b6SYOSHIFUJI Hideaki return -ENOMEM; 158608b202b6SYOSHIFUJI Hideaki 15879d89081dSTomas Winkler err = -EINVAL; 15889d89081dSTomas Winkler if (!pskb_may_pull(skb2, offset + sizeof(struct icmp6hdr))) 15899d89081dSTomas Winkler goto out; 15909d89081dSTomas Winkler 159108b202b6SYOSHIFUJI Hideaki len -= offset - skb_network_offset(skb2); 159208b202b6SYOSHIFUJI Hideaki 159308b202b6SYOSHIFUJI Hideaki __skb_pull(skb2, offset); 159408b202b6SYOSHIFUJI Hideaki skb_reset_transport_header(skb2); 1595fa2da8cdSstephen hemminger skb_postpull_rcsum(skb2, skb_network_header(skb2), 1596fa2da8cdSstephen hemminger skb_network_header_len(skb2)); 159708b202b6SYOSHIFUJI Hideaki 159822df1331SEric Dumazet icmp6_type = icmp6_hdr(skb2)->icmp6_type; 159908b202b6SYOSHIFUJI Hideaki 160022df1331SEric Dumazet switch (icmp6_type) { 160108b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 160208b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 160308b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 160408b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 160508b202b6SYOSHIFUJI Hideaki break; 160608b202b6SYOSHIFUJI Hideaki default: 160708b202b6SYOSHIFUJI Hideaki err = 0; 160808b202b6SYOSHIFUJI Hideaki goto out; 160908b202b6SYOSHIFUJI Hideaki } 161008b202b6SYOSHIFUJI Hideaki 161108b202b6SYOSHIFUJI Hideaki /* Okay, we found MLD message. Check further. */ 161208b202b6SYOSHIFUJI Hideaki if (skb2->len > len) { 161308b202b6SYOSHIFUJI Hideaki err = pskb_trim_rcsum(skb2, len); 161408b202b6SYOSHIFUJI Hideaki if (err) 161508b202b6SYOSHIFUJI Hideaki goto out; 16164b275d7eSYan, Zheng err = -EINVAL; 161708b202b6SYOSHIFUJI Hideaki } 161808b202b6SYOSHIFUJI Hideaki 16194b275d7eSYan, Zheng ip6h = ipv6_hdr(skb2); 16204b275d7eSYan, Zheng 162108b202b6SYOSHIFUJI Hideaki switch (skb2->ip_summed) { 162208b202b6SYOSHIFUJI Hideaki case CHECKSUM_COMPLETE: 16234b275d7eSYan, Zheng if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, skb2->len, 16244b275d7eSYan, Zheng IPPROTO_ICMPV6, skb2->csum)) 162508b202b6SYOSHIFUJI Hideaki break; 162608b202b6SYOSHIFUJI Hideaki /*FALLTHROUGH*/ 162708b202b6SYOSHIFUJI Hideaki case CHECKSUM_NONE: 16284b275d7eSYan, Zheng skb2->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr, 16294b275d7eSYan, Zheng &ip6h->daddr, 16304b275d7eSYan, Zheng skb2->len, 16314b275d7eSYan, Zheng IPPROTO_ICMPV6, 0)); 16324b275d7eSYan, Zheng if (__skb_checksum_complete(skb2)) 163308b202b6SYOSHIFUJI Hideaki goto out; 163408b202b6SYOSHIFUJI Hideaki } 163508b202b6SYOSHIFUJI Hideaki 163608b202b6SYOSHIFUJI Hideaki err = 0; 163708b202b6SYOSHIFUJI Hideaki 163808b202b6SYOSHIFUJI Hideaki BR_INPUT_SKB_CB(skb)->igmp = 1; 163908b202b6SYOSHIFUJI Hideaki 164022df1331SEric Dumazet switch (icmp6_type) { 164108b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REPORT: 164208b202b6SYOSHIFUJI Hideaki { 16439d89081dSTomas Winkler struct mld_msg *mld; 16449d89081dSTomas Winkler if (!pskb_may_pull(skb2, sizeof(*mld))) { 16459d89081dSTomas Winkler err = -EINVAL; 16469d89081dSTomas Winkler goto out; 16479d89081dSTomas Winkler } 16489d89081dSTomas Winkler mld = (struct mld_msg *)skb_transport_header(skb2); 1649fc2af6c7SFernando Luis Vázquez Cao BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 1650b0e9a30dSVlad Yasevich err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid); 165108b202b6SYOSHIFUJI Hideaki break; 165208b202b6SYOSHIFUJI Hideaki } 165308b202b6SYOSHIFUJI Hideaki case ICMPV6_MLD2_REPORT: 165406499098SVlad Yasevich err = br_ip6_multicast_mld2_report(br, port, skb2, vid); 165508b202b6SYOSHIFUJI Hideaki break; 165608b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_QUERY: 165706499098SVlad Yasevich err = br_ip6_multicast_query(br, port, skb2, vid); 165808b202b6SYOSHIFUJI Hideaki break; 165908b202b6SYOSHIFUJI Hideaki case ICMPV6_MGM_REDUCTION: 166008b202b6SYOSHIFUJI Hideaki { 16619d89081dSTomas Winkler struct mld_msg *mld; 16629d89081dSTomas Winkler if (!pskb_may_pull(skb2, sizeof(*mld))) { 16639d89081dSTomas Winkler err = -EINVAL; 16649d89081dSTomas Winkler goto out; 16659d89081dSTomas Winkler } 16669d89081dSTomas Winkler mld = (struct mld_msg *)skb_transport_header(skb2); 1667b0e9a30dSVlad Yasevich br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid); 166808b202b6SYOSHIFUJI Hideaki } 166908b202b6SYOSHIFUJI Hideaki } 167008b202b6SYOSHIFUJI Hideaki 167108b202b6SYOSHIFUJI Hideaki out: 167208b202b6SYOSHIFUJI Hideaki kfree_skb(skb2); 167308b202b6SYOSHIFUJI Hideaki return err; 167408b202b6SYOSHIFUJI Hideaki } 167508b202b6SYOSHIFUJI Hideaki #endif 167608b202b6SYOSHIFUJI Hideaki 1677eb1d1641SHerbert Xu int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port, 167806499098SVlad Yasevich struct sk_buff *skb, u16 vid) 1679eb1d1641SHerbert Xu { 16801fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->igmp = 0; 16811fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 BR_INPUT_SKB_CB(skb)->mrouters_only = 0; 16821fafc7a9SYOSHIFUJI Hideaki / 吉藤英明 1683eb1d1641SHerbert Xu if (br->multicast_disabled) 1684eb1d1641SHerbert Xu return 0; 1685eb1d1641SHerbert Xu 1686eb1d1641SHerbert Xu switch (skb->protocol) { 1687eb1d1641SHerbert Xu case htons(ETH_P_IP): 168806499098SVlad Yasevich return br_multicast_ipv4_rcv(br, port, skb, vid); 1689dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 169008b202b6SYOSHIFUJI Hideaki case htons(ETH_P_IPV6): 169106499098SVlad Yasevich return br_multicast_ipv6_rcv(br, port, skb, vid); 169208b202b6SYOSHIFUJI Hideaki #endif 1693eb1d1641SHerbert Xu } 1694eb1d1641SHerbert Xu 1695eb1d1641SHerbert Xu return 0; 1696eb1d1641SHerbert Xu } 1697eb1d1641SHerbert Xu 1698cc0fdd80SLinus Lüssing static void br_multicast_query_expired(struct net_bridge *br, 1699cc0fdd80SLinus Lüssing struct bridge_mcast_query *query) 1700cc0fdd80SLinus Lüssing { 1701cc0fdd80SLinus Lüssing spin_lock(&br->multicast_lock); 1702cc0fdd80SLinus Lüssing if (query->startup_sent < br->multicast_startup_query_count) 1703cc0fdd80SLinus Lüssing query->startup_sent++; 1704cc0fdd80SLinus Lüssing 1705cc0fdd80SLinus Lüssing br_multicast_send_query(br, NULL, query); 1706cc0fdd80SLinus Lüssing spin_unlock(&br->multicast_lock); 1707cc0fdd80SLinus Lüssing } 1708cc0fdd80SLinus Lüssing 1709cc0fdd80SLinus Lüssing static void br_ip4_multicast_query_expired(unsigned long data) 1710eb1d1641SHerbert Xu { 1711eb1d1641SHerbert Xu struct net_bridge *br = (void *)data; 1712eb1d1641SHerbert Xu 1713cc0fdd80SLinus Lüssing br_multicast_query_expired(br, &br->ip4_query); 1714eb1d1641SHerbert Xu } 1715eb1d1641SHerbert Xu 1716cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1717cc0fdd80SLinus Lüssing static void br_ip6_multicast_query_expired(unsigned long data) 1718cc0fdd80SLinus Lüssing { 1719cc0fdd80SLinus Lüssing struct net_bridge *br = (void *)data; 1720cc0fdd80SLinus Lüssing 1721cc0fdd80SLinus Lüssing br_multicast_query_expired(br, &br->ip6_query); 1722cc0fdd80SLinus Lüssing } 1723cc0fdd80SLinus Lüssing #endif 1724cc0fdd80SLinus Lüssing 1725eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br) 1726eb1d1641SHerbert Xu { 1727eb1d1641SHerbert Xu br->hash_elasticity = 4; 1728eb1d1641SHerbert Xu br->hash_max = 512; 1729eb1d1641SHerbert Xu 1730eb1d1641SHerbert Xu br->multicast_router = 1; 1731c5c23260SHerbert Xu br->multicast_querier = 0; 17321c8ad5bfSCong Wang br->multicast_query_use_ifaddr = 0; 1733eb1d1641SHerbert Xu br->multicast_last_member_count = 2; 1734eb1d1641SHerbert Xu br->multicast_startup_query_count = 2; 1735eb1d1641SHerbert Xu 1736eb1d1641SHerbert Xu br->multicast_last_member_interval = HZ; 1737eb1d1641SHerbert Xu br->multicast_query_response_interval = 10 * HZ; 1738eb1d1641SHerbert Xu br->multicast_startup_query_interval = 125 * HZ / 4; 1739eb1d1641SHerbert Xu br->multicast_query_interval = 125 * HZ; 1740eb1d1641SHerbert Xu br->multicast_querier_interval = 255 * HZ; 1741eb1d1641SHerbert Xu br->multicast_membership_interval = 260 * HZ; 1742eb1d1641SHerbert Xu 1743cc0fdd80SLinus Lüssing br->ip4_querier.delay_time = 0; 1744cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1745cc0fdd80SLinus Lüssing br->ip6_querier.delay_time = 0; 1746cc0fdd80SLinus Lüssing #endif 1747b00589afSLinus Lüssing 1748eb1d1641SHerbert Xu spin_lock_init(&br->multicast_lock); 1749eb1d1641SHerbert Xu setup_timer(&br->multicast_router_timer, 1750eb1d1641SHerbert Xu br_multicast_local_router_expired, 0); 1751cc0fdd80SLinus Lüssing setup_timer(&br->ip4_querier.timer, br_ip4_multicast_querier_expired, 1752eb1d1641SHerbert Xu (unsigned long)br); 1753cc0fdd80SLinus Lüssing setup_timer(&br->ip4_query.timer, br_ip4_multicast_query_expired, 1754cc0fdd80SLinus Lüssing (unsigned long)br); 1755cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1756cc0fdd80SLinus Lüssing setup_timer(&br->ip6_querier.timer, br_ip6_multicast_querier_expired, 1757cc0fdd80SLinus Lüssing (unsigned long)br); 1758cc0fdd80SLinus Lüssing setup_timer(&br->ip6_query.timer, br_ip6_multicast_query_expired, 1759cc0fdd80SLinus Lüssing (unsigned long)br); 1760cc0fdd80SLinus Lüssing #endif 1761eb1d1641SHerbert Xu } 1762eb1d1641SHerbert Xu 1763cc0fdd80SLinus Lüssing static void __br_multicast_open(struct net_bridge *br, 1764cc0fdd80SLinus Lüssing struct bridge_mcast_query *query) 1765eb1d1641SHerbert Xu { 1766cc0fdd80SLinus Lüssing query->startup_sent = 0; 1767eb1d1641SHerbert Xu 1768eb1d1641SHerbert Xu if (br->multicast_disabled) 1769eb1d1641SHerbert Xu return; 1770eb1d1641SHerbert Xu 1771cc0fdd80SLinus Lüssing mod_timer(&query->timer, jiffies); 1772cc0fdd80SLinus Lüssing } 1773cc0fdd80SLinus Lüssing 1774cc0fdd80SLinus Lüssing void br_multicast_open(struct net_bridge *br) 1775cc0fdd80SLinus Lüssing { 1776cc0fdd80SLinus Lüssing __br_multicast_open(br, &br->ip4_query); 1777cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1778cc0fdd80SLinus Lüssing __br_multicast_open(br, &br->ip6_query); 1779cc0fdd80SLinus Lüssing #endif 1780eb1d1641SHerbert Xu } 1781eb1d1641SHerbert Xu 1782eb1d1641SHerbert Xu void br_multicast_stop(struct net_bridge *br) 1783eb1d1641SHerbert Xu { 1784eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 1785eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1786b67bfe0dSSasha Levin struct hlist_node *n; 1787eb1d1641SHerbert Xu u32 ver; 1788eb1d1641SHerbert Xu int i; 1789eb1d1641SHerbert Xu 1790eb1d1641SHerbert Xu del_timer_sync(&br->multicast_router_timer); 1791cc0fdd80SLinus Lüssing del_timer_sync(&br->ip4_querier.timer); 1792cc0fdd80SLinus Lüssing del_timer_sync(&br->ip4_query.timer); 1793cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1794cc0fdd80SLinus Lüssing del_timer_sync(&br->ip6_querier.timer); 1795cc0fdd80SLinus Lüssing del_timer_sync(&br->ip6_query.timer); 1796cc0fdd80SLinus Lüssing #endif 1797eb1d1641SHerbert Xu 1798eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1799e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1800eb1d1641SHerbert Xu if (!mdb) 1801eb1d1641SHerbert Xu goto out; 1802eb1d1641SHerbert Xu 1803eb1d1641SHerbert Xu br->mdb = NULL; 1804eb1d1641SHerbert Xu 1805eb1d1641SHerbert Xu ver = mdb->ver; 1806eb1d1641SHerbert Xu for (i = 0; i < mdb->max; i++) { 1807b67bfe0dSSasha Levin hlist_for_each_entry_safe(mp, n, &mdb->mhash[i], 1808eb1d1641SHerbert Xu hlist[ver]) { 1809eb1d1641SHerbert Xu del_timer(&mp->timer); 1810eb1d1641SHerbert Xu call_rcu_bh(&mp->rcu, br_multicast_free_group); 1811eb1d1641SHerbert Xu } 1812eb1d1641SHerbert Xu } 1813eb1d1641SHerbert Xu 1814eb1d1641SHerbert Xu if (mdb->old) { 1815eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 181610cc2b50SHerbert Xu rcu_barrier_bh(); 1817eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1818eb1d1641SHerbert Xu WARN_ON(mdb->old); 1819eb1d1641SHerbert Xu } 1820eb1d1641SHerbert Xu 1821eb1d1641SHerbert Xu mdb->old = mdb; 1822eb1d1641SHerbert Xu call_rcu_bh(&mdb->rcu, br_mdb_free); 1823eb1d1641SHerbert Xu 1824eb1d1641SHerbert Xu out: 1825eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 1826eb1d1641SHerbert Xu } 18270909e117SHerbert Xu 18280909e117SHerbert Xu int br_multicast_set_router(struct net_bridge *br, unsigned long val) 18290909e117SHerbert Xu { 18300909e117SHerbert Xu int err = -ENOENT; 18310909e117SHerbert Xu 18320909e117SHerbert Xu spin_lock_bh(&br->multicast_lock); 18330909e117SHerbert Xu if (!netif_running(br->dev)) 18340909e117SHerbert Xu goto unlock; 18350909e117SHerbert Xu 18360909e117SHerbert Xu switch (val) { 18370909e117SHerbert Xu case 0: 18380909e117SHerbert Xu case 2: 18390909e117SHerbert Xu del_timer(&br->multicast_router_timer); 18400909e117SHerbert Xu /* fall through */ 18410909e117SHerbert Xu case 1: 18420909e117SHerbert Xu br->multicast_router = val; 18430909e117SHerbert Xu err = 0; 18440909e117SHerbert Xu break; 18450909e117SHerbert Xu 18460909e117SHerbert Xu default: 18470909e117SHerbert Xu err = -EINVAL; 18480909e117SHerbert Xu break; 18490909e117SHerbert Xu } 18500909e117SHerbert Xu 18510909e117SHerbert Xu unlock: 18520909e117SHerbert Xu spin_unlock_bh(&br->multicast_lock); 18530909e117SHerbert Xu 18540909e117SHerbert Xu return err; 18550909e117SHerbert Xu } 18560909e117SHerbert Xu 18570909e117SHerbert Xu int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val) 18580909e117SHerbert Xu { 18590909e117SHerbert Xu struct net_bridge *br = p->br; 18600909e117SHerbert Xu int err = -ENOENT; 18610909e117SHerbert Xu 18620909e117SHerbert Xu spin_lock(&br->multicast_lock); 18630909e117SHerbert Xu if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED) 18640909e117SHerbert Xu goto unlock; 18650909e117SHerbert Xu 18660909e117SHerbert Xu switch (val) { 18670909e117SHerbert Xu case 0: 18680909e117SHerbert Xu case 1: 18690909e117SHerbert Xu case 2: 18700909e117SHerbert Xu p->multicast_router = val; 18710909e117SHerbert Xu err = 0; 18720909e117SHerbert Xu 18730909e117SHerbert Xu if (val < 2 && !hlist_unhashed(&p->rlist)) 18740909e117SHerbert Xu hlist_del_init_rcu(&p->rlist); 18750909e117SHerbert Xu 18760909e117SHerbert Xu if (val == 1) 18770909e117SHerbert Xu break; 18780909e117SHerbert Xu 18790909e117SHerbert Xu del_timer(&p->multicast_router_timer); 18800909e117SHerbert Xu 18810909e117SHerbert Xu if (val == 0) 18820909e117SHerbert Xu break; 18830909e117SHerbert Xu 18840909e117SHerbert Xu br_multicast_add_router(br, p); 18850909e117SHerbert Xu break; 18860909e117SHerbert Xu 18870909e117SHerbert Xu default: 18880909e117SHerbert Xu err = -EINVAL; 18890909e117SHerbert Xu break; 18900909e117SHerbert Xu } 18910909e117SHerbert Xu 18920909e117SHerbert Xu unlock: 18930909e117SHerbert Xu spin_unlock(&br->multicast_lock); 18940909e117SHerbert Xu 18950909e117SHerbert Xu return err; 18960909e117SHerbert Xu } 1897561f1103SHerbert Xu 1898cc0fdd80SLinus Lüssing static void br_multicast_start_querier(struct net_bridge *br, 1899cc0fdd80SLinus Lüssing struct bridge_mcast_query *query) 1900561f1103SHerbert Xu { 1901561f1103SHerbert Xu struct net_bridge_port *port; 190274857216SHerbert Xu 1903cc0fdd80SLinus Lüssing __br_multicast_open(br, query); 190474857216SHerbert Xu 190574857216SHerbert Xu list_for_each_entry(port, &br->port_list, list) { 190674857216SHerbert Xu if (port->state == BR_STATE_DISABLED || 190774857216SHerbert Xu port->state == BR_STATE_BLOCKING) 190874857216SHerbert Xu continue; 190974857216SHerbert Xu 1910cc0fdd80SLinus Lüssing if (query == &br->ip4_query) 1911cc0fdd80SLinus Lüssing br_multicast_enable(&port->ip4_query); 1912cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1913cc0fdd80SLinus Lüssing else 1914cc0fdd80SLinus Lüssing br_multicast_enable(&port->ip6_query); 1915cc0fdd80SLinus Lüssing #endif 191674857216SHerbert Xu } 191774857216SHerbert Xu } 191874857216SHerbert Xu 191974857216SHerbert Xu int br_multicast_toggle(struct net_bridge *br, unsigned long val) 192074857216SHerbert Xu { 19213a7fda06SHerbert Xu int err = 0; 1922e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 1923561f1103SHerbert Xu 1924ef5e0d82SAndrey Vagin spin_lock_bh(&br->multicast_lock); 1925561f1103SHerbert Xu if (br->multicast_disabled == !val) 1926561f1103SHerbert Xu goto unlock; 1927561f1103SHerbert Xu 1928561f1103SHerbert Xu br->multicast_disabled = !val; 1929561f1103SHerbert Xu if (br->multicast_disabled) 1930561f1103SHerbert Xu goto unlock; 1931561f1103SHerbert Xu 19323a7fda06SHerbert Xu if (!netif_running(br->dev)) 19333a7fda06SHerbert Xu goto unlock; 19343a7fda06SHerbert Xu 1935e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 1936e8051688SEric Dumazet if (mdb) { 1937e8051688SEric Dumazet if (mdb->old) { 1938561f1103SHerbert Xu err = -EEXIST; 1939561f1103SHerbert Xu rollback: 1940561f1103SHerbert Xu br->multicast_disabled = !!val; 1941561f1103SHerbert Xu goto unlock; 1942561f1103SHerbert Xu } 1943561f1103SHerbert Xu 1944e8051688SEric Dumazet err = br_mdb_rehash(&br->mdb, mdb->max, 1945561f1103SHerbert Xu br->hash_elasticity); 1946561f1103SHerbert Xu if (err) 1947561f1103SHerbert Xu goto rollback; 1948561f1103SHerbert Xu } 1949561f1103SHerbert Xu 1950cc0fdd80SLinus Lüssing br_multicast_start_querier(br, &br->ip4_query); 1951cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1952cc0fdd80SLinus Lüssing br_multicast_start_querier(br, &br->ip6_query); 1953cc0fdd80SLinus Lüssing #endif 1954561f1103SHerbert Xu 1955561f1103SHerbert Xu unlock: 1956ef5e0d82SAndrey Vagin spin_unlock_bh(&br->multicast_lock); 1957561f1103SHerbert Xu 1958561f1103SHerbert Xu return err; 1959561f1103SHerbert Xu } 1960b195167fSHerbert Xu 1961c5c23260SHerbert Xu int br_multicast_set_querier(struct net_bridge *br, unsigned long val) 1962c5c23260SHerbert Xu { 1963b00589afSLinus Lüssing unsigned long max_delay; 1964b00589afSLinus Lüssing 1965c5c23260SHerbert Xu val = !!val; 1966c5c23260SHerbert Xu 1967c5c23260SHerbert Xu spin_lock_bh(&br->multicast_lock); 1968c5c23260SHerbert Xu if (br->multicast_querier == val) 1969c5c23260SHerbert Xu goto unlock; 1970c5c23260SHerbert Xu 1971c5c23260SHerbert Xu br->multicast_querier = val; 1972b00589afSLinus Lüssing if (!val) 1973b00589afSLinus Lüssing goto unlock; 1974b00589afSLinus Lüssing 1975b00589afSLinus Lüssing max_delay = br->multicast_query_response_interval; 1976b00589afSLinus Lüssing 1977cc0fdd80SLinus Lüssing if (!timer_pending(&br->ip4_querier.timer)) 1978cc0fdd80SLinus Lüssing br->ip4_querier.delay_time = jiffies + max_delay; 1979cc0fdd80SLinus Lüssing 1980cc0fdd80SLinus Lüssing br_multicast_start_querier(br, &br->ip4_query); 1981cc0fdd80SLinus Lüssing 1982cc0fdd80SLinus Lüssing #if IS_ENABLED(CONFIG_IPV6) 1983cc0fdd80SLinus Lüssing if (!timer_pending(&br->ip6_querier.timer)) 1984cc0fdd80SLinus Lüssing br->ip6_querier.delay_time = jiffies + max_delay; 1985cc0fdd80SLinus Lüssing 1986cc0fdd80SLinus Lüssing br_multicast_start_querier(br, &br->ip6_query); 1987cc0fdd80SLinus Lüssing #endif 1988c5c23260SHerbert Xu 1989c5c23260SHerbert Xu unlock: 1990c5c23260SHerbert Xu spin_unlock_bh(&br->multicast_lock); 1991c5c23260SHerbert Xu 1992c5c23260SHerbert Xu return 0; 1993c5c23260SHerbert Xu } 1994c5c23260SHerbert Xu 1995b195167fSHerbert Xu int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val) 1996b195167fSHerbert Xu { 1997b195167fSHerbert Xu int err = -ENOENT; 1998b195167fSHerbert Xu u32 old; 1999e8051688SEric Dumazet struct net_bridge_mdb_htable *mdb; 2000b195167fSHerbert Xu 2001*fe0d692bSCurt Brune spin_lock_bh(&br->multicast_lock); 2002b195167fSHerbert Xu if (!netif_running(br->dev)) 2003b195167fSHerbert Xu goto unlock; 2004b195167fSHerbert Xu 2005b195167fSHerbert Xu err = -EINVAL; 2006b195167fSHerbert Xu if (!is_power_of_2(val)) 2007b195167fSHerbert Xu goto unlock; 2008e8051688SEric Dumazet 2009e8051688SEric Dumazet mdb = mlock_dereference(br->mdb, br); 2010e8051688SEric Dumazet if (mdb && val < mdb->size) 2011b195167fSHerbert Xu goto unlock; 2012b195167fSHerbert Xu 2013b195167fSHerbert Xu err = 0; 2014b195167fSHerbert Xu 2015b195167fSHerbert Xu old = br->hash_max; 2016b195167fSHerbert Xu br->hash_max = val; 2017b195167fSHerbert Xu 2018e8051688SEric Dumazet if (mdb) { 2019e8051688SEric Dumazet if (mdb->old) { 2020b195167fSHerbert Xu err = -EEXIST; 2021b195167fSHerbert Xu rollback: 2022b195167fSHerbert Xu br->hash_max = old; 2023b195167fSHerbert Xu goto unlock; 2024b195167fSHerbert Xu } 2025b195167fSHerbert Xu 2026b195167fSHerbert Xu err = br_mdb_rehash(&br->mdb, br->hash_max, 2027b195167fSHerbert Xu br->hash_elasticity); 2028b195167fSHerbert Xu if (err) 2029b195167fSHerbert Xu goto rollback; 2030b195167fSHerbert Xu } 2031b195167fSHerbert Xu 2032b195167fSHerbert Xu unlock: 2033*fe0d692bSCurt Brune spin_unlock_bh(&br->multicast_lock); 2034b195167fSHerbert Xu 2035b195167fSHerbert Xu return err; 2036b195167fSHerbert Xu } 2037