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> 26eb1d1641SHerbert Xu #include <net/ip.h> 27eb1d1641SHerbert Xu 28eb1d1641SHerbert Xu #include "br_private.h" 29eb1d1641SHerbert Xu 30eb1d1641SHerbert Xu static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb, __be32 ip) 31eb1d1641SHerbert Xu { 32eb1d1641SHerbert Xu return jhash_1word(mdb->secret, (u32)ip) & (mdb->max - 1); 33eb1d1641SHerbert Xu } 34eb1d1641SHerbert Xu 35eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *__br_mdb_ip_get( 36eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb, __be32 dst, int hash) 37eb1d1641SHerbert Xu { 38eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 39eb1d1641SHerbert Xu struct hlist_node *p; 40eb1d1641SHerbert Xu 4149f5fcfdSHerbert Xu hlist_for_each_entry_rcu(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) { 42eb1d1641SHerbert Xu if (dst == mp->addr) 43eb1d1641SHerbert Xu return mp; 44eb1d1641SHerbert Xu } 45eb1d1641SHerbert Xu 46eb1d1641SHerbert Xu return NULL; 47eb1d1641SHerbert Xu } 48eb1d1641SHerbert Xu 49eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *br_mdb_ip_get( 50eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb, __be32 dst) 51eb1d1641SHerbert Xu { 52eb1d1641SHerbert Xu return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst)); 53eb1d1641SHerbert Xu } 54eb1d1641SHerbert Xu 55eb1d1641SHerbert Xu struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br, 56eb1d1641SHerbert Xu struct sk_buff *skb) 57eb1d1641SHerbert Xu { 58eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb = br->mdb; 59eb1d1641SHerbert Xu 60eb1d1641SHerbert Xu if (!mdb || br->multicast_disabled) 61eb1d1641SHerbert Xu return NULL; 62eb1d1641SHerbert Xu 63eb1d1641SHerbert Xu switch (skb->protocol) { 64eb1d1641SHerbert Xu case htons(ETH_P_IP): 65eb1d1641SHerbert Xu if (BR_INPUT_SKB_CB(skb)->igmp) 66eb1d1641SHerbert Xu break; 67eb1d1641SHerbert Xu return br_mdb_ip_get(mdb, ip_hdr(skb)->daddr); 68eb1d1641SHerbert Xu } 69eb1d1641SHerbert Xu 70eb1d1641SHerbert Xu return NULL; 71eb1d1641SHerbert Xu } 72eb1d1641SHerbert Xu 73eb1d1641SHerbert Xu static void br_mdb_free(struct rcu_head *head) 74eb1d1641SHerbert Xu { 75eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb = 76eb1d1641SHerbert Xu container_of(head, struct net_bridge_mdb_htable, rcu); 77eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old = mdb->old; 78eb1d1641SHerbert Xu 79eb1d1641SHerbert Xu mdb->old = NULL; 80eb1d1641SHerbert Xu kfree(old->mhash); 81eb1d1641SHerbert Xu kfree(old); 82eb1d1641SHerbert Xu } 83eb1d1641SHerbert Xu 84eb1d1641SHerbert Xu static int br_mdb_copy(struct net_bridge_mdb_htable *new, 85eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old, 86eb1d1641SHerbert Xu int elasticity) 87eb1d1641SHerbert Xu { 88eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 89eb1d1641SHerbert Xu struct hlist_node *p; 90eb1d1641SHerbert Xu int maxlen; 91eb1d1641SHerbert Xu int len; 92eb1d1641SHerbert Xu int i; 93eb1d1641SHerbert Xu 94eb1d1641SHerbert Xu for (i = 0; i < old->max; i++) 95eb1d1641SHerbert Xu hlist_for_each_entry(mp, p, &old->mhash[i], hlist[old->ver]) 96eb1d1641SHerbert Xu hlist_add_head(&mp->hlist[new->ver], 97eb1d1641SHerbert Xu &new->mhash[br_ip_hash(new, mp->addr)]); 98eb1d1641SHerbert Xu 99eb1d1641SHerbert Xu if (!elasticity) 100eb1d1641SHerbert Xu return 0; 101eb1d1641SHerbert Xu 102eb1d1641SHerbert Xu maxlen = 0; 103eb1d1641SHerbert Xu for (i = 0; i < new->max; i++) { 104eb1d1641SHerbert Xu len = 0; 105eb1d1641SHerbert Xu hlist_for_each_entry(mp, p, &new->mhash[i], hlist[new->ver]) 106eb1d1641SHerbert Xu len++; 107eb1d1641SHerbert Xu if (len > maxlen) 108eb1d1641SHerbert Xu maxlen = len; 109eb1d1641SHerbert Xu } 110eb1d1641SHerbert Xu 111eb1d1641SHerbert Xu return maxlen > elasticity ? -EINVAL : 0; 112eb1d1641SHerbert Xu } 113eb1d1641SHerbert Xu 114eb1d1641SHerbert Xu static void br_multicast_free_pg(struct rcu_head *head) 115eb1d1641SHerbert Xu { 116eb1d1641SHerbert Xu struct net_bridge_port_group *p = 117eb1d1641SHerbert Xu container_of(head, struct net_bridge_port_group, rcu); 118eb1d1641SHerbert Xu 119eb1d1641SHerbert Xu kfree(p); 120eb1d1641SHerbert Xu } 121eb1d1641SHerbert Xu 122eb1d1641SHerbert Xu static void br_multicast_free_group(struct rcu_head *head) 123eb1d1641SHerbert Xu { 124eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = 125eb1d1641SHerbert Xu container_of(head, struct net_bridge_mdb_entry, rcu); 126eb1d1641SHerbert Xu 127eb1d1641SHerbert Xu kfree(mp); 128eb1d1641SHerbert Xu } 129eb1d1641SHerbert Xu 130eb1d1641SHerbert Xu static void br_multicast_group_expired(unsigned long data) 131eb1d1641SHerbert Xu { 132eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = (void *)data; 133eb1d1641SHerbert Xu struct net_bridge *br = mp->br; 134eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 135eb1d1641SHerbert Xu 136eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 137eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&mp->timer)) 138eb1d1641SHerbert Xu goto out; 139eb1d1641SHerbert Xu 140eb1d1641SHerbert Xu if (!hlist_unhashed(&mp->mglist)) 141eb1d1641SHerbert Xu hlist_del_init(&mp->mglist); 142eb1d1641SHerbert Xu 143eb1d1641SHerbert Xu if (mp->ports) 144eb1d1641SHerbert Xu goto out; 145eb1d1641SHerbert Xu 146eb1d1641SHerbert Xu mdb = br->mdb; 147eb1d1641SHerbert Xu hlist_del_rcu(&mp->hlist[mdb->ver]); 148eb1d1641SHerbert Xu mdb->size--; 149eb1d1641SHerbert Xu 150eb1d1641SHerbert Xu del_timer(&mp->query_timer); 151eb1d1641SHerbert Xu call_rcu_bh(&mp->rcu, br_multicast_free_group); 152eb1d1641SHerbert Xu 153eb1d1641SHerbert Xu out: 154eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 155eb1d1641SHerbert Xu } 156eb1d1641SHerbert Xu 157eb1d1641SHerbert Xu static void br_multicast_del_pg(struct net_bridge *br, 158eb1d1641SHerbert Xu struct net_bridge_port_group *pg) 159eb1d1641SHerbert Xu { 160eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb = br->mdb; 161eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 162eb1d1641SHerbert Xu struct net_bridge_port_group *p; 163eb1d1641SHerbert Xu struct net_bridge_port_group **pp; 164eb1d1641SHerbert Xu 165eb1d1641SHerbert Xu mp = br_mdb_ip_get(mdb, pg->addr); 166eb1d1641SHerbert Xu if (WARN_ON(!mp)) 167eb1d1641SHerbert Xu return; 168eb1d1641SHerbert Xu 169eb1d1641SHerbert Xu for (pp = &mp->ports; (p = *pp); pp = &p->next) { 170eb1d1641SHerbert Xu if (p != pg) 171eb1d1641SHerbert Xu continue; 172eb1d1641SHerbert Xu 173eb1d1641SHerbert Xu *pp = p->next; 174eb1d1641SHerbert Xu hlist_del_init(&p->mglist); 175eb1d1641SHerbert Xu del_timer(&p->timer); 176eb1d1641SHerbert Xu del_timer(&p->query_timer); 177eb1d1641SHerbert Xu call_rcu_bh(&p->rcu, br_multicast_free_pg); 178eb1d1641SHerbert Xu 179eb1d1641SHerbert Xu if (!mp->ports && hlist_unhashed(&mp->mglist) && 180eb1d1641SHerbert Xu netif_running(br->dev)) 181eb1d1641SHerbert Xu mod_timer(&mp->timer, jiffies); 182eb1d1641SHerbert Xu 183eb1d1641SHerbert Xu return; 184eb1d1641SHerbert Xu } 185eb1d1641SHerbert Xu 186eb1d1641SHerbert Xu WARN_ON(1); 187eb1d1641SHerbert Xu } 188eb1d1641SHerbert Xu 189eb1d1641SHerbert Xu static void br_multicast_port_group_expired(unsigned long data) 190eb1d1641SHerbert Xu { 191eb1d1641SHerbert Xu struct net_bridge_port_group *pg = (void *)data; 192eb1d1641SHerbert Xu struct net_bridge *br = pg->port->br; 193eb1d1641SHerbert Xu 194eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 195eb1d1641SHerbert Xu if (!netif_running(br->dev) || timer_pending(&pg->timer) || 196eb1d1641SHerbert Xu hlist_unhashed(&pg->mglist)) 197eb1d1641SHerbert Xu goto out; 198eb1d1641SHerbert Xu 199eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 200eb1d1641SHerbert Xu 201eb1d1641SHerbert Xu out: 202eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 203eb1d1641SHerbert Xu } 204eb1d1641SHerbert Xu 205eb1d1641SHerbert Xu static int br_mdb_rehash(struct net_bridge_mdb_htable **mdbp, int max, 206eb1d1641SHerbert Xu int elasticity) 207eb1d1641SHerbert Xu { 208eb1d1641SHerbert Xu struct net_bridge_mdb_htable *old = *mdbp; 209eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 210eb1d1641SHerbert Xu int err; 211eb1d1641SHerbert Xu 212eb1d1641SHerbert Xu mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC); 213eb1d1641SHerbert Xu if (!mdb) 214eb1d1641SHerbert Xu return -ENOMEM; 215eb1d1641SHerbert Xu 216eb1d1641SHerbert Xu mdb->max = max; 217eb1d1641SHerbert Xu mdb->old = old; 218eb1d1641SHerbert Xu 219eb1d1641SHerbert Xu mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC); 220eb1d1641SHerbert Xu if (!mdb->mhash) { 221eb1d1641SHerbert Xu kfree(mdb); 222eb1d1641SHerbert Xu return -ENOMEM; 223eb1d1641SHerbert Xu } 224eb1d1641SHerbert Xu 225eb1d1641SHerbert Xu mdb->size = old ? old->size : 0; 226eb1d1641SHerbert Xu mdb->ver = old ? old->ver ^ 1 : 0; 227eb1d1641SHerbert Xu 228eb1d1641SHerbert Xu if (!old || elasticity) 229eb1d1641SHerbert Xu get_random_bytes(&mdb->secret, sizeof(mdb->secret)); 230eb1d1641SHerbert Xu else 231eb1d1641SHerbert Xu mdb->secret = old->secret; 232eb1d1641SHerbert Xu 233eb1d1641SHerbert Xu if (!old) 234eb1d1641SHerbert Xu goto out; 235eb1d1641SHerbert Xu 236eb1d1641SHerbert Xu err = br_mdb_copy(mdb, old, elasticity); 237eb1d1641SHerbert Xu if (err) { 238eb1d1641SHerbert Xu kfree(mdb->mhash); 239eb1d1641SHerbert Xu kfree(mdb); 240eb1d1641SHerbert Xu return err; 241eb1d1641SHerbert Xu } 242eb1d1641SHerbert Xu 243eb1d1641SHerbert Xu call_rcu_bh(&mdb->rcu, br_mdb_free); 244eb1d1641SHerbert Xu 245eb1d1641SHerbert Xu out: 246eb1d1641SHerbert Xu rcu_assign_pointer(*mdbp, mdb); 247eb1d1641SHerbert Xu 248eb1d1641SHerbert Xu return 0; 249eb1d1641SHerbert Xu } 250eb1d1641SHerbert Xu 251eb1d1641SHerbert Xu static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br, 252eb1d1641SHerbert Xu __be32 group) 253eb1d1641SHerbert Xu { 254eb1d1641SHerbert Xu struct sk_buff *skb; 255eb1d1641SHerbert Xu struct igmphdr *ih; 256eb1d1641SHerbert Xu struct ethhdr *eth; 257eb1d1641SHerbert Xu struct iphdr *iph; 258eb1d1641SHerbert Xu 259eb1d1641SHerbert Xu skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) + 260eb1d1641SHerbert Xu sizeof(*ih) + 4); 261eb1d1641SHerbert Xu if (!skb) 262eb1d1641SHerbert Xu goto out; 263eb1d1641SHerbert Xu 264eb1d1641SHerbert Xu skb->protocol = htons(ETH_P_IP); 265eb1d1641SHerbert Xu 266eb1d1641SHerbert Xu skb_reset_mac_header(skb); 267eb1d1641SHerbert Xu eth = eth_hdr(skb); 268eb1d1641SHerbert Xu 269eb1d1641SHerbert Xu memcpy(eth->h_source, br->dev->dev_addr, 6); 270eb1d1641SHerbert Xu eth->h_dest[0] = 1; 271eb1d1641SHerbert Xu eth->h_dest[1] = 0; 272eb1d1641SHerbert Xu eth->h_dest[2] = 0x5e; 273eb1d1641SHerbert Xu eth->h_dest[3] = 0; 274eb1d1641SHerbert Xu eth->h_dest[4] = 0; 275eb1d1641SHerbert Xu eth->h_dest[5] = 1; 276eb1d1641SHerbert Xu eth->h_proto = htons(ETH_P_IP); 277eb1d1641SHerbert Xu skb_put(skb, sizeof(*eth)); 278eb1d1641SHerbert Xu 279eb1d1641SHerbert Xu skb_set_network_header(skb, skb->len); 280eb1d1641SHerbert Xu iph = ip_hdr(skb); 281eb1d1641SHerbert Xu 282eb1d1641SHerbert Xu iph->version = 4; 283eb1d1641SHerbert Xu iph->ihl = 6; 284eb1d1641SHerbert Xu iph->tos = 0xc0; 285eb1d1641SHerbert Xu iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4); 286eb1d1641SHerbert Xu iph->id = 0; 287eb1d1641SHerbert Xu iph->frag_off = htons(IP_DF); 288eb1d1641SHerbert Xu iph->ttl = 1; 289eb1d1641SHerbert Xu iph->protocol = IPPROTO_IGMP; 290eb1d1641SHerbert Xu iph->saddr = 0; 291eb1d1641SHerbert Xu iph->daddr = htonl(INADDR_ALLHOSTS_GROUP); 292eb1d1641SHerbert Xu ((u8 *)&iph[1])[0] = IPOPT_RA; 293eb1d1641SHerbert Xu ((u8 *)&iph[1])[1] = 4; 294eb1d1641SHerbert Xu ((u8 *)&iph[1])[2] = 0; 295eb1d1641SHerbert Xu ((u8 *)&iph[1])[3] = 0; 296eb1d1641SHerbert Xu ip_send_check(iph); 297eb1d1641SHerbert Xu skb_put(skb, 24); 298eb1d1641SHerbert Xu 299eb1d1641SHerbert Xu skb_set_transport_header(skb, skb->len); 300eb1d1641SHerbert Xu ih = igmp_hdr(skb); 301eb1d1641SHerbert Xu ih->type = IGMP_HOST_MEMBERSHIP_QUERY; 302eb1d1641SHerbert Xu ih->code = (group ? br->multicast_last_member_interval : 303eb1d1641SHerbert Xu br->multicast_query_response_interval) / 304eb1d1641SHerbert Xu (HZ / IGMP_TIMER_SCALE); 305eb1d1641SHerbert Xu ih->group = group; 306eb1d1641SHerbert Xu ih->csum = 0; 307eb1d1641SHerbert Xu ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr)); 308eb1d1641SHerbert Xu skb_put(skb, sizeof(*ih)); 309eb1d1641SHerbert Xu 310eb1d1641SHerbert Xu __skb_pull(skb, sizeof(*eth)); 311eb1d1641SHerbert Xu 312eb1d1641SHerbert Xu out: 313eb1d1641SHerbert Xu return skb; 314eb1d1641SHerbert Xu } 315eb1d1641SHerbert Xu 316eb1d1641SHerbert Xu static void br_multicast_send_group_query(struct net_bridge_mdb_entry *mp) 317eb1d1641SHerbert Xu { 318eb1d1641SHerbert Xu struct net_bridge *br = mp->br; 319eb1d1641SHerbert Xu struct sk_buff *skb; 320eb1d1641SHerbert Xu 321eb1d1641SHerbert Xu skb = br_multicast_alloc_query(br, mp->addr); 322eb1d1641SHerbert Xu if (!skb) 323eb1d1641SHerbert Xu goto timer; 324eb1d1641SHerbert Xu 325eb1d1641SHerbert Xu netif_rx(skb); 326eb1d1641SHerbert Xu 327eb1d1641SHerbert Xu timer: 328eb1d1641SHerbert Xu if (++mp->queries_sent < br->multicast_last_member_count) 329eb1d1641SHerbert Xu mod_timer(&mp->query_timer, 330eb1d1641SHerbert Xu jiffies + br->multicast_last_member_interval); 331eb1d1641SHerbert Xu } 332eb1d1641SHerbert Xu 333eb1d1641SHerbert Xu static void br_multicast_group_query_expired(unsigned long data) 334eb1d1641SHerbert Xu { 335eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp = (void *)data; 336eb1d1641SHerbert Xu struct net_bridge *br = mp->br; 337eb1d1641SHerbert Xu 338eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 339eb1d1641SHerbert Xu if (!netif_running(br->dev) || hlist_unhashed(&mp->mglist) || 340eb1d1641SHerbert Xu mp->queries_sent >= br->multicast_last_member_count) 341eb1d1641SHerbert Xu goto out; 342eb1d1641SHerbert Xu 343eb1d1641SHerbert Xu br_multicast_send_group_query(mp); 344eb1d1641SHerbert Xu 345eb1d1641SHerbert Xu out: 346eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 347eb1d1641SHerbert Xu } 348eb1d1641SHerbert Xu 349eb1d1641SHerbert Xu static void br_multicast_send_port_group_query(struct net_bridge_port_group *pg) 350eb1d1641SHerbert Xu { 351eb1d1641SHerbert Xu struct net_bridge_port *port = pg->port; 352eb1d1641SHerbert Xu struct net_bridge *br = port->br; 353eb1d1641SHerbert Xu struct sk_buff *skb; 354eb1d1641SHerbert Xu 355eb1d1641SHerbert Xu skb = br_multicast_alloc_query(br, pg->addr); 356eb1d1641SHerbert Xu if (!skb) 357eb1d1641SHerbert Xu goto timer; 358eb1d1641SHerbert Xu 359eb1d1641SHerbert Xu br_deliver(port, skb); 360eb1d1641SHerbert Xu 361eb1d1641SHerbert Xu timer: 362eb1d1641SHerbert Xu if (++pg->queries_sent < br->multicast_last_member_count) 363eb1d1641SHerbert Xu mod_timer(&pg->query_timer, 364eb1d1641SHerbert Xu jiffies + br->multicast_last_member_interval); 365eb1d1641SHerbert Xu } 366eb1d1641SHerbert Xu 367eb1d1641SHerbert Xu static void br_multicast_port_group_query_expired(unsigned long data) 368eb1d1641SHerbert Xu { 369eb1d1641SHerbert Xu struct net_bridge_port_group *pg = (void *)data; 370eb1d1641SHerbert Xu struct net_bridge_port *port = pg->port; 371eb1d1641SHerbert Xu struct net_bridge *br = port->br; 372eb1d1641SHerbert Xu 373eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 374eb1d1641SHerbert Xu if (!netif_running(br->dev) || hlist_unhashed(&pg->mglist) || 375eb1d1641SHerbert Xu pg->queries_sent >= br->multicast_last_member_count) 376eb1d1641SHerbert Xu goto out; 377eb1d1641SHerbert Xu 378eb1d1641SHerbert Xu br_multicast_send_port_group_query(pg); 379eb1d1641SHerbert Xu 380eb1d1641SHerbert Xu out: 381eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 382eb1d1641SHerbert Xu } 383eb1d1641SHerbert Xu 384eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *br_multicast_get_group( 385eb1d1641SHerbert Xu struct net_bridge *br, struct net_bridge_port *port, __be32 group, 386eb1d1641SHerbert Xu int hash) 387eb1d1641SHerbert Xu { 388eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb = br->mdb; 389eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 390eb1d1641SHerbert Xu struct hlist_node *p; 391eb1d1641SHerbert Xu unsigned count = 0; 392eb1d1641SHerbert Xu unsigned max; 393eb1d1641SHerbert Xu int elasticity; 394eb1d1641SHerbert Xu int err; 395eb1d1641SHerbert Xu 396eb1d1641SHerbert Xu hlist_for_each_entry(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) { 397eb1d1641SHerbert Xu count++; 398eb1d1641SHerbert Xu if (unlikely(group == mp->addr)) { 399eb1d1641SHerbert Xu return mp; 400eb1d1641SHerbert Xu } 401eb1d1641SHerbert Xu } 402eb1d1641SHerbert Xu 403eb1d1641SHerbert Xu elasticity = 0; 404eb1d1641SHerbert Xu max = mdb->max; 405eb1d1641SHerbert Xu 406eb1d1641SHerbert Xu if (unlikely(count > br->hash_elasticity && count)) { 407eb1d1641SHerbert Xu if (net_ratelimit()) 408eb1d1641SHerbert Xu printk(KERN_INFO "%s: Multicast hash table " 409eb1d1641SHerbert Xu "chain limit reached: %s\n", 410eb1d1641SHerbert Xu br->dev->name, port ? port->dev->name : 411eb1d1641SHerbert Xu br->dev->name); 412eb1d1641SHerbert Xu 413eb1d1641SHerbert Xu elasticity = br->hash_elasticity; 414eb1d1641SHerbert Xu } 415eb1d1641SHerbert Xu 416eb1d1641SHerbert Xu if (mdb->size >= max) { 417eb1d1641SHerbert Xu max *= 2; 418eb1d1641SHerbert Xu if (unlikely(max >= br->hash_max)) { 419eb1d1641SHerbert Xu printk(KERN_WARNING "%s: Multicast hash table maximum " 420eb1d1641SHerbert Xu "reached, disabling snooping: %s, %d\n", 421eb1d1641SHerbert Xu br->dev->name, port ? port->dev->name : 422eb1d1641SHerbert Xu br->dev->name, 423eb1d1641SHerbert Xu max); 424eb1d1641SHerbert Xu err = -E2BIG; 425eb1d1641SHerbert Xu disable: 426eb1d1641SHerbert Xu br->multicast_disabled = 1; 427eb1d1641SHerbert Xu goto err; 428eb1d1641SHerbert Xu } 429eb1d1641SHerbert Xu } 430eb1d1641SHerbert Xu 431eb1d1641SHerbert Xu if (max > mdb->max || elasticity) { 432eb1d1641SHerbert Xu if (mdb->old) { 433eb1d1641SHerbert Xu if (net_ratelimit()) 434eb1d1641SHerbert Xu printk(KERN_INFO "%s: Multicast hash table " 435eb1d1641SHerbert Xu "on fire: %s\n", 436eb1d1641SHerbert Xu br->dev->name, port ? port->dev->name : 437eb1d1641SHerbert Xu br->dev->name); 438eb1d1641SHerbert Xu err = -EEXIST; 439eb1d1641SHerbert Xu goto err; 440eb1d1641SHerbert Xu } 441eb1d1641SHerbert Xu 442eb1d1641SHerbert Xu err = br_mdb_rehash(&br->mdb, max, elasticity); 443eb1d1641SHerbert Xu if (err) { 444eb1d1641SHerbert Xu printk(KERN_WARNING "%s: Cannot rehash multicast " 445eb1d1641SHerbert Xu "hash table, disabling snooping: " 446eb1d1641SHerbert Xu "%s, %d, %d\n", 447eb1d1641SHerbert Xu br->dev->name, port ? port->dev->name : 448eb1d1641SHerbert Xu br->dev->name, 449eb1d1641SHerbert Xu mdb->size, err); 450eb1d1641SHerbert Xu goto disable; 451eb1d1641SHerbert Xu } 452eb1d1641SHerbert Xu 453eb1d1641SHerbert Xu err = -EAGAIN; 454eb1d1641SHerbert Xu goto err; 455eb1d1641SHerbert Xu } 456eb1d1641SHerbert Xu 457eb1d1641SHerbert Xu return NULL; 458eb1d1641SHerbert Xu 459eb1d1641SHerbert Xu err: 460eb1d1641SHerbert Xu mp = ERR_PTR(err); 461eb1d1641SHerbert Xu return mp; 462eb1d1641SHerbert Xu } 463eb1d1641SHerbert Xu 464eb1d1641SHerbert Xu static struct net_bridge_mdb_entry *br_multicast_new_group( 465eb1d1641SHerbert Xu struct net_bridge *br, struct net_bridge_port *port, __be32 group) 466eb1d1641SHerbert Xu { 467eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb = br->mdb; 468eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 469eb1d1641SHerbert Xu int hash; 470eb1d1641SHerbert Xu 471eb1d1641SHerbert Xu if (!mdb) { 472eb1d1641SHerbert Xu if (br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0)) 473eb1d1641SHerbert Xu return NULL; 474eb1d1641SHerbert Xu goto rehash; 475eb1d1641SHerbert Xu } 476eb1d1641SHerbert Xu 477eb1d1641SHerbert Xu hash = br_ip_hash(mdb, group); 478eb1d1641SHerbert Xu mp = br_multicast_get_group(br, port, group, hash); 479eb1d1641SHerbert Xu switch (PTR_ERR(mp)) { 480eb1d1641SHerbert Xu case 0: 481eb1d1641SHerbert Xu break; 482eb1d1641SHerbert Xu 483eb1d1641SHerbert Xu case -EAGAIN: 484eb1d1641SHerbert Xu rehash: 485eb1d1641SHerbert Xu mdb = br->mdb; 486eb1d1641SHerbert Xu hash = br_ip_hash(mdb, group); 487eb1d1641SHerbert Xu break; 488eb1d1641SHerbert Xu 489eb1d1641SHerbert Xu default: 490eb1d1641SHerbert Xu goto out; 491eb1d1641SHerbert Xu } 492eb1d1641SHerbert Xu 493eb1d1641SHerbert Xu mp = kzalloc(sizeof(*mp), GFP_ATOMIC); 494eb1d1641SHerbert Xu if (unlikely(!mp)) 495eb1d1641SHerbert Xu goto out; 496eb1d1641SHerbert Xu 497eb1d1641SHerbert Xu mp->br = br; 498eb1d1641SHerbert Xu mp->addr = group; 499eb1d1641SHerbert Xu setup_timer(&mp->timer, br_multicast_group_expired, 500eb1d1641SHerbert Xu (unsigned long)mp); 501eb1d1641SHerbert Xu setup_timer(&mp->query_timer, br_multicast_group_query_expired, 502eb1d1641SHerbert Xu (unsigned long)mp); 503eb1d1641SHerbert Xu 504eb1d1641SHerbert Xu hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]); 505eb1d1641SHerbert Xu mdb->size++; 506eb1d1641SHerbert Xu 507eb1d1641SHerbert Xu out: 508eb1d1641SHerbert Xu return mp; 509eb1d1641SHerbert Xu } 510eb1d1641SHerbert Xu 511eb1d1641SHerbert Xu static int br_multicast_add_group(struct net_bridge *br, 512eb1d1641SHerbert Xu struct net_bridge_port *port, __be32 group) 513eb1d1641SHerbert Xu { 514eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 515eb1d1641SHerbert Xu struct net_bridge_port_group *p; 516eb1d1641SHerbert Xu struct net_bridge_port_group **pp; 517eb1d1641SHerbert Xu unsigned long now = jiffies; 518eb1d1641SHerbert Xu int err; 519eb1d1641SHerbert Xu 520eb1d1641SHerbert Xu if (ipv4_is_local_multicast(group)) 521eb1d1641SHerbert Xu return 0; 522eb1d1641SHerbert Xu 523eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 524eb1d1641SHerbert Xu if (!netif_running(br->dev) || 525eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 526eb1d1641SHerbert Xu goto out; 527eb1d1641SHerbert Xu 528eb1d1641SHerbert Xu mp = br_multicast_new_group(br, port, group); 529eb1d1641SHerbert Xu err = PTR_ERR(mp); 530eb1d1641SHerbert Xu if (unlikely(IS_ERR(mp) || !mp)) 531eb1d1641SHerbert Xu goto err; 532eb1d1641SHerbert Xu 533eb1d1641SHerbert Xu if (!port) { 534eb1d1641SHerbert Xu hlist_add_head(&mp->mglist, &br->mglist); 535eb1d1641SHerbert Xu mod_timer(&mp->timer, now + br->multicast_membership_interval); 536eb1d1641SHerbert Xu goto out; 537eb1d1641SHerbert Xu } 538eb1d1641SHerbert Xu 539eb1d1641SHerbert Xu for (pp = &mp->ports; (p = *pp); pp = &p->next) { 540eb1d1641SHerbert Xu if (p->port == port) 541eb1d1641SHerbert Xu goto found; 542eb1d1641SHerbert Xu if ((unsigned long)p->port < (unsigned long)port) 543eb1d1641SHerbert Xu break; 544eb1d1641SHerbert Xu } 545eb1d1641SHerbert Xu 546eb1d1641SHerbert Xu p = kzalloc(sizeof(*p), GFP_ATOMIC); 547eb1d1641SHerbert Xu err = -ENOMEM; 548eb1d1641SHerbert Xu if (unlikely(!p)) 549eb1d1641SHerbert Xu goto err; 550eb1d1641SHerbert Xu 551eb1d1641SHerbert Xu p->addr = group; 552eb1d1641SHerbert Xu p->port = port; 553eb1d1641SHerbert Xu p->next = *pp; 554eb1d1641SHerbert Xu hlist_add_head(&p->mglist, &port->mglist); 555eb1d1641SHerbert Xu setup_timer(&p->timer, br_multicast_port_group_expired, 556eb1d1641SHerbert Xu (unsigned long)p); 557eb1d1641SHerbert Xu setup_timer(&p->query_timer, br_multicast_port_group_query_expired, 558eb1d1641SHerbert Xu (unsigned long)p); 559eb1d1641SHerbert Xu 560eb1d1641SHerbert Xu rcu_assign_pointer(*pp, p); 561eb1d1641SHerbert Xu 562eb1d1641SHerbert Xu found: 563eb1d1641SHerbert Xu mod_timer(&p->timer, now + br->multicast_membership_interval); 564eb1d1641SHerbert Xu out: 565eb1d1641SHerbert Xu err = 0; 566eb1d1641SHerbert Xu 567eb1d1641SHerbert Xu err: 568eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 569eb1d1641SHerbert Xu return err; 570eb1d1641SHerbert Xu } 571eb1d1641SHerbert Xu 572eb1d1641SHerbert Xu static void br_multicast_router_expired(unsigned long data) 573eb1d1641SHerbert Xu { 574eb1d1641SHerbert Xu struct net_bridge_port *port = (void *)data; 575eb1d1641SHerbert Xu struct net_bridge *br = port->br; 576eb1d1641SHerbert Xu 577eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 578eb1d1641SHerbert Xu if (port->multicast_router != 1 || 579eb1d1641SHerbert Xu timer_pending(&port->multicast_router_timer) || 580eb1d1641SHerbert Xu hlist_unhashed(&port->rlist)) 581eb1d1641SHerbert Xu goto out; 582eb1d1641SHerbert Xu 583eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 584eb1d1641SHerbert Xu 585eb1d1641SHerbert Xu out: 586eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 587eb1d1641SHerbert Xu } 588eb1d1641SHerbert Xu 589eb1d1641SHerbert Xu static void br_multicast_local_router_expired(unsigned long data) 590eb1d1641SHerbert Xu { 591eb1d1641SHerbert Xu } 592eb1d1641SHerbert Xu 593eb1d1641SHerbert Xu static void br_multicast_send_query(struct net_bridge *br, 594eb1d1641SHerbert Xu struct net_bridge_port *port, u32 sent) 595eb1d1641SHerbert Xu { 596eb1d1641SHerbert Xu unsigned long time; 597eb1d1641SHerbert Xu struct sk_buff *skb; 598eb1d1641SHerbert Xu 599eb1d1641SHerbert Xu if (!netif_running(br->dev) || br->multicast_disabled || 600eb1d1641SHerbert Xu timer_pending(&br->multicast_querier_timer)) 601eb1d1641SHerbert Xu return; 602eb1d1641SHerbert Xu 603eb1d1641SHerbert Xu skb = br_multicast_alloc_query(br, 0); 604eb1d1641SHerbert Xu if (!skb) 605eb1d1641SHerbert Xu goto timer; 606eb1d1641SHerbert Xu 607eb1d1641SHerbert Xu if (port) { 608eb1d1641SHerbert Xu __skb_push(skb, sizeof(struct ethhdr)); 609eb1d1641SHerbert Xu skb->dev = port->dev; 610eb1d1641SHerbert Xu NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev, 611eb1d1641SHerbert Xu dev_queue_xmit); 612eb1d1641SHerbert Xu } else 613eb1d1641SHerbert Xu netif_rx(skb); 614eb1d1641SHerbert Xu 615eb1d1641SHerbert Xu timer: 616eb1d1641SHerbert Xu time = jiffies; 617eb1d1641SHerbert Xu time += sent < br->multicast_startup_query_count ? 618eb1d1641SHerbert Xu br->multicast_startup_query_interval : 619eb1d1641SHerbert Xu br->multicast_query_interval; 620eb1d1641SHerbert Xu mod_timer(port ? &port->multicast_query_timer : 621eb1d1641SHerbert Xu &br->multicast_query_timer, time); 622eb1d1641SHerbert Xu } 623eb1d1641SHerbert Xu 624eb1d1641SHerbert Xu static void br_multicast_port_query_expired(unsigned long data) 625eb1d1641SHerbert Xu { 626eb1d1641SHerbert Xu struct net_bridge_port *port = (void *)data; 627eb1d1641SHerbert Xu struct net_bridge *br = port->br; 628eb1d1641SHerbert Xu 629eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 63002a780c0SDan Carpenter if (port->state == BR_STATE_DISABLED || 63102a780c0SDan Carpenter port->state == BR_STATE_BLOCKING) 632eb1d1641SHerbert Xu goto out; 633eb1d1641SHerbert Xu 634eb1d1641SHerbert Xu if (port->multicast_startup_queries_sent < 635eb1d1641SHerbert Xu br->multicast_startup_query_count) 636eb1d1641SHerbert Xu port->multicast_startup_queries_sent++; 637eb1d1641SHerbert Xu 638eb1d1641SHerbert Xu br_multicast_send_query(port->br, port, 639eb1d1641SHerbert Xu port->multicast_startup_queries_sent); 640eb1d1641SHerbert Xu 641eb1d1641SHerbert Xu out: 642eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 643eb1d1641SHerbert Xu } 644eb1d1641SHerbert Xu 645eb1d1641SHerbert Xu void br_multicast_add_port(struct net_bridge_port *port) 646eb1d1641SHerbert Xu { 647eb1d1641SHerbert Xu port->multicast_router = 1; 648eb1d1641SHerbert Xu 649eb1d1641SHerbert Xu setup_timer(&port->multicast_router_timer, br_multicast_router_expired, 650eb1d1641SHerbert Xu (unsigned long)port); 651eb1d1641SHerbert Xu setup_timer(&port->multicast_query_timer, 652eb1d1641SHerbert Xu br_multicast_port_query_expired, (unsigned long)port); 653eb1d1641SHerbert Xu } 654eb1d1641SHerbert Xu 655eb1d1641SHerbert Xu void br_multicast_del_port(struct net_bridge_port *port) 656eb1d1641SHerbert Xu { 657eb1d1641SHerbert Xu del_timer_sync(&port->multicast_router_timer); 658eb1d1641SHerbert Xu } 659eb1d1641SHerbert Xu 660561f1103SHerbert Xu static void __br_multicast_enable_port(struct net_bridge_port *port) 661561f1103SHerbert Xu { 662561f1103SHerbert Xu port->multicast_startup_queries_sent = 0; 663561f1103SHerbert Xu 664561f1103SHerbert Xu if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 || 665561f1103SHerbert Xu del_timer(&port->multicast_query_timer)) 666561f1103SHerbert Xu mod_timer(&port->multicast_query_timer, jiffies); 667561f1103SHerbert Xu } 668561f1103SHerbert Xu 669eb1d1641SHerbert Xu void br_multicast_enable_port(struct net_bridge_port *port) 670eb1d1641SHerbert Xu { 671eb1d1641SHerbert Xu struct net_bridge *br = port->br; 672eb1d1641SHerbert Xu 673eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 674eb1d1641SHerbert Xu if (br->multicast_disabled || !netif_running(br->dev)) 675eb1d1641SHerbert Xu goto out; 676eb1d1641SHerbert Xu 677561f1103SHerbert Xu __br_multicast_enable_port(port); 678eb1d1641SHerbert Xu 679eb1d1641SHerbert Xu out: 680eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 681eb1d1641SHerbert Xu } 682eb1d1641SHerbert Xu 683eb1d1641SHerbert Xu void br_multicast_disable_port(struct net_bridge_port *port) 684eb1d1641SHerbert Xu { 685eb1d1641SHerbert Xu struct net_bridge *br = port->br; 686eb1d1641SHerbert Xu struct net_bridge_port_group *pg; 687eb1d1641SHerbert Xu struct hlist_node *p, *n; 688eb1d1641SHerbert Xu 689eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 690eb1d1641SHerbert Xu hlist_for_each_entry_safe(pg, p, n, &port->mglist, mglist) 691eb1d1641SHerbert Xu br_multicast_del_pg(br, pg); 692eb1d1641SHerbert Xu 693eb1d1641SHerbert Xu if (!hlist_unhashed(&port->rlist)) 694eb1d1641SHerbert Xu hlist_del_init_rcu(&port->rlist); 695eb1d1641SHerbert Xu del_timer(&port->multicast_router_timer); 696eb1d1641SHerbert Xu del_timer(&port->multicast_query_timer); 697eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 698eb1d1641SHerbert Xu } 699eb1d1641SHerbert Xu 700eb1d1641SHerbert Xu static int br_multicast_igmp3_report(struct net_bridge *br, 701eb1d1641SHerbert Xu struct net_bridge_port *port, 702eb1d1641SHerbert Xu struct sk_buff *skb) 703eb1d1641SHerbert Xu { 704eb1d1641SHerbert Xu struct igmpv3_report *ih; 705eb1d1641SHerbert Xu struct igmpv3_grec *grec; 706eb1d1641SHerbert Xu int i; 707eb1d1641SHerbert Xu int len; 708eb1d1641SHerbert Xu int num; 709eb1d1641SHerbert Xu int type; 710eb1d1641SHerbert Xu int err = 0; 711eb1d1641SHerbert Xu __be32 group; 712eb1d1641SHerbert Xu 713eb1d1641SHerbert Xu if (!pskb_may_pull(skb, sizeof(*ih))) 714eb1d1641SHerbert Xu return -EINVAL; 715eb1d1641SHerbert Xu 716eb1d1641SHerbert Xu ih = igmpv3_report_hdr(skb); 717eb1d1641SHerbert Xu num = ntohs(ih->ngrec); 718eb1d1641SHerbert Xu len = sizeof(*ih); 719eb1d1641SHerbert Xu 720eb1d1641SHerbert Xu for (i = 0; i < num; i++) { 721eb1d1641SHerbert Xu len += sizeof(*grec); 722eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 723eb1d1641SHerbert Xu return -EINVAL; 724eb1d1641SHerbert Xu 725eb1d1641SHerbert Xu grec = (void *)(skb->data + len); 726eb1d1641SHerbert Xu group = grec->grec_mca; 727eb1d1641SHerbert Xu type = grec->grec_type; 728eb1d1641SHerbert Xu 729eb1d1641SHerbert Xu len += grec->grec_nsrcs * 4; 730eb1d1641SHerbert Xu if (!pskb_may_pull(skb, len)) 731eb1d1641SHerbert Xu return -EINVAL; 732eb1d1641SHerbert Xu 733eb1d1641SHerbert Xu /* We treat this as an IGMPv2 report for now. */ 734eb1d1641SHerbert Xu switch (type) { 735eb1d1641SHerbert Xu case IGMPV3_MODE_IS_INCLUDE: 736eb1d1641SHerbert Xu case IGMPV3_MODE_IS_EXCLUDE: 737eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_INCLUDE: 738eb1d1641SHerbert Xu case IGMPV3_CHANGE_TO_EXCLUDE: 739eb1d1641SHerbert Xu case IGMPV3_ALLOW_NEW_SOURCES: 740eb1d1641SHerbert Xu case IGMPV3_BLOCK_OLD_SOURCES: 741eb1d1641SHerbert Xu break; 742eb1d1641SHerbert Xu 743eb1d1641SHerbert Xu default: 744eb1d1641SHerbert Xu continue; 745eb1d1641SHerbert Xu } 746eb1d1641SHerbert Xu 747eb1d1641SHerbert Xu err = br_multicast_add_group(br, port, group); 748eb1d1641SHerbert Xu if (err) 749eb1d1641SHerbert Xu break; 750eb1d1641SHerbert Xu } 751eb1d1641SHerbert Xu 752eb1d1641SHerbert Xu return err; 753eb1d1641SHerbert Xu } 754eb1d1641SHerbert Xu 7550909e117SHerbert Xu static void br_multicast_add_router(struct net_bridge *br, 7560909e117SHerbert Xu struct net_bridge_port *port) 7570909e117SHerbert Xu { 7580909e117SHerbert Xu struct hlist_node *p; 7590909e117SHerbert Xu struct hlist_node **h; 7600909e117SHerbert Xu 7610909e117SHerbert Xu for (h = &br->router_list.first; 7620909e117SHerbert Xu (p = *h) && 7630909e117SHerbert Xu (unsigned long)container_of(p, struct net_bridge_port, rlist) > 7640909e117SHerbert Xu (unsigned long)port; 7650909e117SHerbert Xu h = &p->next) 7660909e117SHerbert Xu ; 7670909e117SHerbert Xu 7680909e117SHerbert Xu port->rlist.pprev = h; 7690909e117SHerbert Xu port->rlist.next = p; 7700909e117SHerbert Xu rcu_assign_pointer(*h, &port->rlist); 7710909e117SHerbert Xu if (p) 7720909e117SHerbert Xu p->pprev = &port->rlist.next; 7730909e117SHerbert Xu } 7740909e117SHerbert Xu 775eb1d1641SHerbert Xu static void br_multicast_mark_router(struct net_bridge *br, 776eb1d1641SHerbert Xu struct net_bridge_port *port) 777eb1d1641SHerbert Xu { 778eb1d1641SHerbert Xu unsigned long now = jiffies; 779eb1d1641SHerbert Xu 780eb1d1641SHerbert Xu if (!port) { 781eb1d1641SHerbert Xu if (br->multicast_router == 1) 782eb1d1641SHerbert Xu mod_timer(&br->multicast_router_timer, 783eb1d1641SHerbert Xu now + br->multicast_querier_interval); 784eb1d1641SHerbert Xu return; 785eb1d1641SHerbert Xu } 786eb1d1641SHerbert Xu 787eb1d1641SHerbert Xu if (port->multicast_router != 1) 788eb1d1641SHerbert Xu return; 789eb1d1641SHerbert Xu 790eb1d1641SHerbert Xu if (!hlist_unhashed(&port->rlist)) 791eb1d1641SHerbert Xu goto timer; 792eb1d1641SHerbert Xu 7930909e117SHerbert Xu br_multicast_add_router(br, port); 794eb1d1641SHerbert Xu 795eb1d1641SHerbert Xu timer: 796eb1d1641SHerbert Xu mod_timer(&port->multicast_router_timer, 797eb1d1641SHerbert Xu now + br->multicast_querier_interval); 798eb1d1641SHerbert Xu } 799eb1d1641SHerbert Xu 800eb1d1641SHerbert Xu static void br_multicast_query_received(struct net_bridge *br, 801eb1d1641SHerbert Xu struct net_bridge_port *port, 802eb1d1641SHerbert Xu __be32 saddr) 803eb1d1641SHerbert Xu { 804eb1d1641SHerbert Xu if (saddr) 805eb1d1641SHerbert Xu mod_timer(&br->multicast_querier_timer, 806eb1d1641SHerbert Xu jiffies + br->multicast_querier_interval); 807eb1d1641SHerbert Xu else if (timer_pending(&br->multicast_querier_timer)) 808eb1d1641SHerbert Xu return; 809eb1d1641SHerbert Xu 810eb1d1641SHerbert Xu br_multicast_mark_router(br, port); 811eb1d1641SHerbert Xu } 812eb1d1641SHerbert Xu 813eb1d1641SHerbert Xu static int br_multicast_query(struct net_bridge *br, 814eb1d1641SHerbert Xu struct net_bridge_port *port, 815eb1d1641SHerbert Xu struct sk_buff *skb) 816eb1d1641SHerbert Xu { 817eb1d1641SHerbert Xu struct iphdr *iph = ip_hdr(skb); 818eb1d1641SHerbert Xu struct igmphdr *ih = igmp_hdr(skb); 819eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 820eb1d1641SHerbert Xu struct igmpv3_query *ih3; 821eb1d1641SHerbert Xu struct net_bridge_port_group *p; 822eb1d1641SHerbert Xu struct net_bridge_port_group **pp; 823eb1d1641SHerbert Xu unsigned long max_delay; 824eb1d1641SHerbert Xu unsigned long now = jiffies; 825eb1d1641SHerbert Xu __be32 group; 826*bec68ff1SYOSHIFUJI Hideaki int err = 0; 827eb1d1641SHerbert Xu 828eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 829eb1d1641SHerbert Xu if (!netif_running(br->dev) || 830eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED)) 831eb1d1641SHerbert Xu goto out; 832eb1d1641SHerbert Xu 833eb1d1641SHerbert Xu br_multicast_query_received(br, port, iph->saddr); 834eb1d1641SHerbert Xu 835eb1d1641SHerbert Xu group = ih->group; 836eb1d1641SHerbert Xu 837eb1d1641SHerbert Xu if (skb->len == sizeof(*ih)) { 838eb1d1641SHerbert Xu max_delay = ih->code * (HZ / IGMP_TIMER_SCALE); 839eb1d1641SHerbert Xu 840eb1d1641SHerbert Xu if (!max_delay) { 841eb1d1641SHerbert Xu max_delay = 10 * HZ; 842eb1d1641SHerbert Xu group = 0; 843eb1d1641SHerbert Xu } 844eb1d1641SHerbert Xu } else { 845*bec68ff1SYOSHIFUJI Hideaki if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) { 846*bec68ff1SYOSHIFUJI Hideaki err = -EINVAL; 847*bec68ff1SYOSHIFUJI Hideaki goto out; 848*bec68ff1SYOSHIFUJI Hideaki } 849eb1d1641SHerbert Xu 850eb1d1641SHerbert Xu ih3 = igmpv3_query_hdr(skb); 851eb1d1641SHerbert Xu if (ih3->nsrcs) 852*bec68ff1SYOSHIFUJI Hideaki goto out; 853eb1d1641SHerbert Xu 854eb1d1641SHerbert Xu max_delay = ih3->code ? 1 : 855eb1d1641SHerbert Xu IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE); 856eb1d1641SHerbert Xu } 857eb1d1641SHerbert Xu 858eb1d1641SHerbert Xu if (!group) 859eb1d1641SHerbert Xu goto out; 860eb1d1641SHerbert Xu 861eb1d1641SHerbert Xu mp = br_mdb_ip_get(br->mdb, group); 862eb1d1641SHerbert Xu if (!mp) 863eb1d1641SHerbert Xu goto out; 864eb1d1641SHerbert Xu 865eb1d1641SHerbert Xu max_delay *= br->multicast_last_member_count; 866eb1d1641SHerbert Xu 867eb1d1641SHerbert Xu if (!hlist_unhashed(&mp->mglist) && 868eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 869eb1d1641SHerbert Xu time_after(mp->timer.expires, now + max_delay) : 870eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) 871eb1d1641SHerbert Xu mod_timer(&mp->timer, now + max_delay); 872eb1d1641SHerbert Xu 873eb1d1641SHerbert Xu for (pp = &mp->ports; (p = *pp); pp = &p->next) { 874eb1d1641SHerbert Xu if (timer_pending(&p->timer) ? 875eb1d1641SHerbert Xu time_after(p->timer.expires, now + max_delay) : 876eb1d1641SHerbert Xu try_to_del_timer_sync(&p->timer) >= 0) 877eb1d1641SHerbert Xu mod_timer(&mp->timer, now + max_delay); 878eb1d1641SHerbert Xu } 879eb1d1641SHerbert Xu 880eb1d1641SHerbert Xu out: 881eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 882*bec68ff1SYOSHIFUJI Hideaki return err; 883eb1d1641SHerbert Xu } 884eb1d1641SHerbert Xu 885eb1d1641SHerbert Xu static void br_multicast_leave_group(struct net_bridge *br, 886eb1d1641SHerbert Xu struct net_bridge_port *port, 887eb1d1641SHerbert Xu __be32 group) 888eb1d1641SHerbert Xu { 889eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 890eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 891eb1d1641SHerbert Xu struct net_bridge_port_group *p; 892eb1d1641SHerbert Xu unsigned long now; 893eb1d1641SHerbert Xu unsigned long time; 894eb1d1641SHerbert Xu 895eb1d1641SHerbert Xu if (ipv4_is_local_multicast(group)) 896eb1d1641SHerbert Xu return; 897eb1d1641SHerbert Xu 898eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 899eb1d1641SHerbert Xu if (!netif_running(br->dev) || 900eb1d1641SHerbert Xu (port && port->state == BR_STATE_DISABLED) || 901eb1d1641SHerbert Xu timer_pending(&br->multicast_querier_timer)) 902eb1d1641SHerbert Xu goto out; 903eb1d1641SHerbert Xu 904eb1d1641SHerbert Xu mdb = br->mdb; 905eb1d1641SHerbert Xu mp = br_mdb_ip_get(mdb, group); 906eb1d1641SHerbert Xu if (!mp) 907eb1d1641SHerbert Xu goto out; 908eb1d1641SHerbert Xu 909eb1d1641SHerbert Xu now = jiffies; 910eb1d1641SHerbert Xu time = now + br->multicast_last_member_count * 911eb1d1641SHerbert Xu br->multicast_last_member_interval; 912eb1d1641SHerbert Xu 913eb1d1641SHerbert Xu if (!port) { 914eb1d1641SHerbert Xu if (!hlist_unhashed(&mp->mglist) && 915eb1d1641SHerbert Xu (timer_pending(&mp->timer) ? 916eb1d1641SHerbert Xu time_after(mp->timer.expires, time) : 917eb1d1641SHerbert Xu try_to_del_timer_sync(&mp->timer) >= 0)) { 918eb1d1641SHerbert Xu mod_timer(&mp->timer, time); 919eb1d1641SHerbert Xu 920eb1d1641SHerbert Xu mp->queries_sent = 0; 921eb1d1641SHerbert Xu mod_timer(&mp->query_timer, now); 922eb1d1641SHerbert Xu } 923eb1d1641SHerbert Xu 924eb1d1641SHerbert Xu goto out; 925eb1d1641SHerbert Xu } 926eb1d1641SHerbert Xu 927eb1d1641SHerbert Xu for (p = mp->ports; p; p = p->next) { 928eb1d1641SHerbert Xu if (p->port != port) 929eb1d1641SHerbert Xu continue; 930eb1d1641SHerbert Xu 931eb1d1641SHerbert Xu if (!hlist_unhashed(&p->mglist) && 932eb1d1641SHerbert Xu (timer_pending(&p->timer) ? 933eb1d1641SHerbert Xu time_after(p->timer.expires, time) : 934eb1d1641SHerbert Xu try_to_del_timer_sync(&p->timer) >= 0)) { 935eb1d1641SHerbert Xu mod_timer(&p->timer, time); 936eb1d1641SHerbert Xu 937eb1d1641SHerbert Xu p->queries_sent = 0; 938eb1d1641SHerbert Xu mod_timer(&p->query_timer, now); 939eb1d1641SHerbert Xu } 940eb1d1641SHerbert Xu 941eb1d1641SHerbert Xu break; 942eb1d1641SHerbert Xu } 943eb1d1641SHerbert Xu 944eb1d1641SHerbert Xu out: 945eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 946eb1d1641SHerbert Xu } 947eb1d1641SHerbert Xu 948eb1d1641SHerbert Xu static int br_multicast_ipv4_rcv(struct net_bridge *br, 949eb1d1641SHerbert Xu struct net_bridge_port *port, 950eb1d1641SHerbert Xu struct sk_buff *skb) 951eb1d1641SHerbert Xu { 952eb1d1641SHerbert Xu struct sk_buff *skb2 = skb; 953eb1d1641SHerbert Xu struct iphdr *iph; 954eb1d1641SHerbert Xu struct igmphdr *ih; 955eb1d1641SHerbert Xu unsigned len; 956eb1d1641SHerbert Xu unsigned offset; 957eb1d1641SHerbert Xu int err; 958eb1d1641SHerbert Xu 959eb1d1641SHerbert Xu BR_INPUT_SKB_CB(skb)->igmp = 0; 960eb1d1641SHerbert Xu BR_INPUT_SKB_CB(skb)->mrouters_only = 0; 961eb1d1641SHerbert Xu 962eb1d1641SHerbert Xu /* We treat OOM as packet loss for now. */ 963eb1d1641SHerbert Xu if (!pskb_may_pull(skb, sizeof(*iph))) 964eb1d1641SHerbert Xu return -EINVAL; 965eb1d1641SHerbert Xu 966eb1d1641SHerbert Xu iph = ip_hdr(skb); 967eb1d1641SHerbert Xu 968eb1d1641SHerbert Xu if (iph->ihl < 5 || iph->version != 4) 969eb1d1641SHerbert Xu return -EINVAL; 970eb1d1641SHerbert Xu 971eb1d1641SHerbert Xu if (!pskb_may_pull(skb, ip_hdrlen(skb))) 972eb1d1641SHerbert Xu return -EINVAL; 973eb1d1641SHerbert Xu 974eb1d1641SHerbert Xu iph = ip_hdr(skb); 975eb1d1641SHerbert Xu 976eb1d1641SHerbert Xu if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl))) 977eb1d1641SHerbert Xu return -EINVAL; 978eb1d1641SHerbert Xu 979eb1d1641SHerbert Xu if (iph->protocol != IPPROTO_IGMP) 980eb1d1641SHerbert Xu return 0; 981eb1d1641SHerbert Xu 982eb1d1641SHerbert Xu len = ntohs(iph->tot_len); 983eb1d1641SHerbert Xu if (skb->len < len || len < ip_hdrlen(skb)) 984eb1d1641SHerbert Xu return -EINVAL; 985eb1d1641SHerbert Xu 986eb1d1641SHerbert Xu if (skb->len > len) { 987eb1d1641SHerbert Xu skb2 = skb_clone(skb, GFP_ATOMIC); 988eb1d1641SHerbert Xu if (!skb2) 989eb1d1641SHerbert Xu return -ENOMEM; 990eb1d1641SHerbert Xu 991eb1d1641SHerbert Xu err = pskb_trim_rcsum(skb2, len); 992eb1d1641SHerbert Xu if (err) 993eb1d1641SHerbert Xu return err; 994eb1d1641SHerbert Xu } 995eb1d1641SHerbert Xu 996eb1d1641SHerbert Xu len -= ip_hdrlen(skb2); 997eb1d1641SHerbert Xu offset = skb_network_offset(skb2) + ip_hdrlen(skb2); 998eb1d1641SHerbert Xu __skb_pull(skb2, offset); 999eb1d1641SHerbert Xu skb_reset_transport_header(skb2); 1000eb1d1641SHerbert Xu 1001eb1d1641SHerbert Xu err = -EINVAL; 1002eb1d1641SHerbert Xu if (!pskb_may_pull(skb2, sizeof(*ih))) 1003eb1d1641SHerbert Xu goto out; 1004eb1d1641SHerbert Xu 1005eb1d1641SHerbert Xu iph = ip_hdr(skb2); 1006eb1d1641SHerbert Xu 1007eb1d1641SHerbert Xu switch (skb2->ip_summed) { 1008eb1d1641SHerbert Xu case CHECKSUM_COMPLETE: 1009eb1d1641SHerbert Xu if (!csum_fold(skb2->csum)) 1010eb1d1641SHerbert Xu break; 1011eb1d1641SHerbert Xu /* fall through */ 1012eb1d1641SHerbert Xu case CHECKSUM_NONE: 1013eb1d1641SHerbert Xu skb2->csum = 0; 1014eb1d1641SHerbert Xu if (skb_checksum_complete(skb2)) 1015eb1d1641SHerbert Xu return -EINVAL; 1016eb1d1641SHerbert Xu } 1017eb1d1641SHerbert Xu 1018eb1d1641SHerbert Xu err = 0; 1019eb1d1641SHerbert Xu 1020eb1d1641SHerbert Xu BR_INPUT_SKB_CB(skb)->igmp = 1; 1021eb1d1641SHerbert Xu ih = igmp_hdr(skb2); 1022eb1d1641SHerbert Xu 1023eb1d1641SHerbert Xu switch (ih->type) { 1024eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_REPORT: 1025eb1d1641SHerbert Xu case IGMPV2_HOST_MEMBERSHIP_REPORT: 1026eb1d1641SHerbert Xu BR_INPUT_SKB_CB(skb2)->mrouters_only = 1; 1027eb1d1641SHerbert Xu err = br_multicast_add_group(br, port, ih->group); 1028eb1d1641SHerbert Xu break; 1029eb1d1641SHerbert Xu case IGMPV3_HOST_MEMBERSHIP_REPORT: 1030eb1d1641SHerbert Xu err = br_multicast_igmp3_report(br, port, skb2); 1031eb1d1641SHerbert Xu break; 1032eb1d1641SHerbert Xu case IGMP_HOST_MEMBERSHIP_QUERY: 1033eb1d1641SHerbert Xu err = br_multicast_query(br, port, skb2); 1034eb1d1641SHerbert Xu break; 1035eb1d1641SHerbert Xu case IGMP_HOST_LEAVE_MESSAGE: 1036eb1d1641SHerbert Xu br_multicast_leave_group(br, port, ih->group); 1037eb1d1641SHerbert Xu break; 1038eb1d1641SHerbert Xu } 1039eb1d1641SHerbert Xu 1040eb1d1641SHerbert Xu out: 1041eb1d1641SHerbert Xu __skb_push(skb2, offset); 1042eb1d1641SHerbert Xu if (skb2 != skb) 1043eb1d1641SHerbert Xu kfree_skb(skb2); 1044eb1d1641SHerbert Xu return err; 1045eb1d1641SHerbert Xu } 1046eb1d1641SHerbert Xu 1047eb1d1641SHerbert Xu int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port, 1048eb1d1641SHerbert Xu struct sk_buff *skb) 1049eb1d1641SHerbert Xu { 1050eb1d1641SHerbert Xu if (br->multicast_disabled) 1051eb1d1641SHerbert Xu return 0; 1052eb1d1641SHerbert Xu 1053eb1d1641SHerbert Xu switch (skb->protocol) { 1054eb1d1641SHerbert Xu case htons(ETH_P_IP): 1055eb1d1641SHerbert Xu return br_multicast_ipv4_rcv(br, port, skb); 1056eb1d1641SHerbert Xu } 1057eb1d1641SHerbert Xu 1058eb1d1641SHerbert Xu return 0; 1059eb1d1641SHerbert Xu } 1060eb1d1641SHerbert Xu 1061eb1d1641SHerbert Xu static void br_multicast_query_expired(unsigned long data) 1062eb1d1641SHerbert Xu { 1063eb1d1641SHerbert Xu struct net_bridge *br = (void *)data; 1064eb1d1641SHerbert Xu 1065eb1d1641SHerbert Xu spin_lock(&br->multicast_lock); 1066eb1d1641SHerbert Xu if (br->multicast_startup_queries_sent < 1067eb1d1641SHerbert Xu br->multicast_startup_query_count) 1068eb1d1641SHerbert Xu br->multicast_startup_queries_sent++; 1069eb1d1641SHerbert Xu 1070eb1d1641SHerbert Xu br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent); 1071eb1d1641SHerbert Xu 1072eb1d1641SHerbert Xu spin_unlock(&br->multicast_lock); 1073eb1d1641SHerbert Xu } 1074eb1d1641SHerbert Xu 1075eb1d1641SHerbert Xu void br_multicast_init(struct net_bridge *br) 1076eb1d1641SHerbert Xu { 1077eb1d1641SHerbert Xu br->hash_elasticity = 4; 1078eb1d1641SHerbert Xu br->hash_max = 512; 1079eb1d1641SHerbert Xu 1080eb1d1641SHerbert Xu br->multicast_router = 1; 1081eb1d1641SHerbert Xu br->multicast_last_member_count = 2; 1082eb1d1641SHerbert Xu br->multicast_startup_query_count = 2; 1083eb1d1641SHerbert Xu 1084eb1d1641SHerbert Xu br->multicast_last_member_interval = HZ; 1085eb1d1641SHerbert Xu br->multicast_query_response_interval = 10 * HZ; 1086eb1d1641SHerbert Xu br->multicast_startup_query_interval = 125 * HZ / 4; 1087eb1d1641SHerbert Xu br->multicast_query_interval = 125 * HZ; 1088eb1d1641SHerbert Xu br->multicast_querier_interval = 255 * HZ; 1089eb1d1641SHerbert Xu br->multicast_membership_interval = 260 * HZ; 1090eb1d1641SHerbert Xu 1091eb1d1641SHerbert Xu spin_lock_init(&br->multicast_lock); 1092eb1d1641SHerbert Xu setup_timer(&br->multicast_router_timer, 1093eb1d1641SHerbert Xu br_multicast_local_router_expired, 0); 1094eb1d1641SHerbert Xu setup_timer(&br->multicast_querier_timer, 1095eb1d1641SHerbert Xu br_multicast_local_router_expired, 0); 1096eb1d1641SHerbert Xu setup_timer(&br->multicast_query_timer, br_multicast_query_expired, 1097eb1d1641SHerbert Xu (unsigned long)br); 1098eb1d1641SHerbert Xu } 1099eb1d1641SHerbert Xu 1100eb1d1641SHerbert Xu void br_multicast_open(struct net_bridge *br) 1101eb1d1641SHerbert Xu { 1102eb1d1641SHerbert Xu br->multicast_startup_queries_sent = 0; 1103eb1d1641SHerbert Xu 1104eb1d1641SHerbert Xu if (br->multicast_disabled) 1105eb1d1641SHerbert Xu return; 1106eb1d1641SHerbert Xu 1107eb1d1641SHerbert Xu mod_timer(&br->multicast_query_timer, jiffies); 1108eb1d1641SHerbert Xu } 1109eb1d1641SHerbert Xu 1110eb1d1641SHerbert Xu void br_multicast_stop(struct net_bridge *br) 1111eb1d1641SHerbert Xu { 1112eb1d1641SHerbert Xu struct net_bridge_mdb_htable *mdb; 1113eb1d1641SHerbert Xu struct net_bridge_mdb_entry *mp; 1114eb1d1641SHerbert Xu struct hlist_node *p, *n; 1115eb1d1641SHerbert Xu u32 ver; 1116eb1d1641SHerbert Xu int i; 1117eb1d1641SHerbert Xu 1118eb1d1641SHerbert Xu del_timer_sync(&br->multicast_router_timer); 1119eb1d1641SHerbert Xu del_timer_sync(&br->multicast_querier_timer); 1120eb1d1641SHerbert Xu del_timer_sync(&br->multicast_query_timer); 1121eb1d1641SHerbert Xu 1122eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1123eb1d1641SHerbert Xu mdb = br->mdb; 1124eb1d1641SHerbert Xu if (!mdb) 1125eb1d1641SHerbert Xu goto out; 1126eb1d1641SHerbert Xu 1127eb1d1641SHerbert Xu br->mdb = NULL; 1128eb1d1641SHerbert Xu 1129eb1d1641SHerbert Xu ver = mdb->ver; 1130eb1d1641SHerbert Xu for (i = 0; i < mdb->max; i++) { 1131eb1d1641SHerbert Xu hlist_for_each_entry_safe(mp, p, n, &mdb->mhash[i], 1132eb1d1641SHerbert Xu hlist[ver]) { 1133eb1d1641SHerbert Xu del_timer(&mp->timer); 1134eb1d1641SHerbert Xu del_timer(&mp->query_timer); 1135eb1d1641SHerbert Xu call_rcu_bh(&mp->rcu, br_multicast_free_group); 1136eb1d1641SHerbert Xu } 1137eb1d1641SHerbert Xu } 1138eb1d1641SHerbert Xu 1139eb1d1641SHerbert Xu if (mdb->old) { 1140eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 114110cc2b50SHerbert Xu rcu_barrier_bh(); 1142eb1d1641SHerbert Xu spin_lock_bh(&br->multicast_lock); 1143eb1d1641SHerbert Xu WARN_ON(mdb->old); 1144eb1d1641SHerbert Xu } 1145eb1d1641SHerbert Xu 1146eb1d1641SHerbert Xu mdb->old = mdb; 1147eb1d1641SHerbert Xu call_rcu_bh(&mdb->rcu, br_mdb_free); 1148eb1d1641SHerbert Xu 1149eb1d1641SHerbert Xu out: 1150eb1d1641SHerbert Xu spin_unlock_bh(&br->multicast_lock); 1151eb1d1641SHerbert Xu } 11520909e117SHerbert Xu 11530909e117SHerbert Xu int br_multicast_set_router(struct net_bridge *br, unsigned long val) 11540909e117SHerbert Xu { 11550909e117SHerbert Xu int err = -ENOENT; 11560909e117SHerbert Xu 11570909e117SHerbert Xu spin_lock_bh(&br->multicast_lock); 11580909e117SHerbert Xu if (!netif_running(br->dev)) 11590909e117SHerbert Xu goto unlock; 11600909e117SHerbert Xu 11610909e117SHerbert Xu switch (val) { 11620909e117SHerbert Xu case 0: 11630909e117SHerbert Xu case 2: 11640909e117SHerbert Xu del_timer(&br->multicast_router_timer); 11650909e117SHerbert Xu /* fall through */ 11660909e117SHerbert Xu case 1: 11670909e117SHerbert Xu br->multicast_router = val; 11680909e117SHerbert Xu err = 0; 11690909e117SHerbert Xu break; 11700909e117SHerbert Xu 11710909e117SHerbert Xu default: 11720909e117SHerbert Xu err = -EINVAL; 11730909e117SHerbert Xu break; 11740909e117SHerbert Xu } 11750909e117SHerbert Xu 11760909e117SHerbert Xu unlock: 11770909e117SHerbert Xu spin_unlock_bh(&br->multicast_lock); 11780909e117SHerbert Xu 11790909e117SHerbert Xu return err; 11800909e117SHerbert Xu } 11810909e117SHerbert Xu 11820909e117SHerbert Xu int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val) 11830909e117SHerbert Xu { 11840909e117SHerbert Xu struct net_bridge *br = p->br; 11850909e117SHerbert Xu int err = -ENOENT; 11860909e117SHerbert Xu 11870909e117SHerbert Xu spin_lock(&br->multicast_lock); 11880909e117SHerbert Xu if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED) 11890909e117SHerbert Xu goto unlock; 11900909e117SHerbert Xu 11910909e117SHerbert Xu switch (val) { 11920909e117SHerbert Xu case 0: 11930909e117SHerbert Xu case 1: 11940909e117SHerbert Xu case 2: 11950909e117SHerbert Xu p->multicast_router = val; 11960909e117SHerbert Xu err = 0; 11970909e117SHerbert Xu 11980909e117SHerbert Xu if (val < 2 && !hlist_unhashed(&p->rlist)) 11990909e117SHerbert Xu hlist_del_init_rcu(&p->rlist); 12000909e117SHerbert Xu 12010909e117SHerbert Xu if (val == 1) 12020909e117SHerbert Xu break; 12030909e117SHerbert Xu 12040909e117SHerbert Xu del_timer(&p->multicast_router_timer); 12050909e117SHerbert Xu 12060909e117SHerbert Xu if (val == 0) 12070909e117SHerbert Xu break; 12080909e117SHerbert Xu 12090909e117SHerbert Xu br_multicast_add_router(br, p); 12100909e117SHerbert Xu break; 12110909e117SHerbert Xu 12120909e117SHerbert Xu default: 12130909e117SHerbert Xu err = -EINVAL; 12140909e117SHerbert Xu break; 12150909e117SHerbert Xu } 12160909e117SHerbert Xu 12170909e117SHerbert Xu unlock: 12180909e117SHerbert Xu spin_unlock(&br->multicast_lock); 12190909e117SHerbert Xu 12200909e117SHerbert Xu return err; 12210909e117SHerbert Xu } 1222561f1103SHerbert Xu 1223561f1103SHerbert Xu int br_multicast_toggle(struct net_bridge *br, unsigned long val) 1224561f1103SHerbert Xu { 1225561f1103SHerbert Xu struct net_bridge_port *port; 1226561f1103SHerbert Xu int err = -ENOENT; 1227561f1103SHerbert Xu 1228561f1103SHerbert Xu spin_lock(&br->multicast_lock); 1229561f1103SHerbert Xu if (!netif_running(br->dev)) 1230561f1103SHerbert Xu goto unlock; 1231561f1103SHerbert Xu 1232561f1103SHerbert Xu err = 0; 1233561f1103SHerbert Xu if (br->multicast_disabled == !val) 1234561f1103SHerbert Xu goto unlock; 1235561f1103SHerbert Xu 1236561f1103SHerbert Xu br->multicast_disabled = !val; 1237561f1103SHerbert Xu if (br->multicast_disabled) 1238561f1103SHerbert Xu goto unlock; 1239561f1103SHerbert Xu 1240561f1103SHerbert Xu if (br->mdb) { 1241561f1103SHerbert Xu if (br->mdb->old) { 1242561f1103SHerbert Xu err = -EEXIST; 1243561f1103SHerbert Xu rollback: 1244561f1103SHerbert Xu br->multicast_disabled = !!val; 1245561f1103SHerbert Xu goto unlock; 1246561f1103SHerbert Xu } 1247561f1103SHerbert Xu 1248561f1103SHerbert Xu err = br_mdb_rehash(&br->mdb, br->mdb->max, 1249561f1103SHerbert Xu br->hash_elasticity); 1250561f1103SHerbert Xu if (err) 1251561f1103SHerbert Xu goto rollback; 1252561f1103SHerbert Xu } 1253561f1103SHerbert Xu 1254561f1103SHerbert Xu br_multicast_open(br); 1255561f1103SHerbert Xu list_for_each_entry(port, &br->port_list, list) { 1256561f1103SHerbert Xu if (port->state == BR_STATE_DISABLED || 1257561f1103SHerbert Xu port->state == BR_STATE_BLOCKING) 1258561f1103SHerbert Xu continue; 1259561f1103SHerbert Xu 1260561f1103SHerbert Xu __br_multicast_enable_port(port); 1261561f1103SHerbert Xu } 1262561f1103SHerbert Xu 1263561f1103SHerbert Xu unlock: 1264561f1103SHerbert Xu spin_unlock(&br->multicast_lock); 1265561f1103SHerbert Xu 1266561f1103SHerbert Xu return err; 1267561f1103SHerbert Xu } 1268b195167fSHerbert Xu 1269b195167fSHerbert Xu int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val) 1270b195167fSHerbert Xu { 1271b195167fSHerbert Xu int err = -ENOENT; 1272b195167fSHerbert Xu u32 old; 1273b195167fSHerbert Xu 1274b195167fSHerbert Xu spin_lock(&br->multicast_lock); 1275b195167fSHerbert Xu if (!netif_running(br->dev)) 1276b195167fSHerbert Xu goto unlock; 1277b195167fSHerbert Xu 1278b195167fSHerbert Xu err = -EINVAL; 1279b195167fSHerbert Xu if (!is_power_of_2(val)) 1280b195167fSHerbert Xu goto unlock; 1281b195167fSHerbert Xu if (br->mdb && val < br->mdb->size) 1282b195167fSHerbert Xu goto unlock; 1283b195167fSHerbert Xu 1284b195167fSHerbert Xu err = 0; 1285b195167fSHerbert Xu 1286b195167fSHerbert Xu old = br->hash_max; 1287b195167fSHerbert Xu br->hash_max = val; 1288b195167fSHerbert Xu 1289b195167fSHerbert Xu if (br->mdb) { 1290b195167fSHerbert Xu if (br->mdb->old) { 1291b195167fSHerbert Xu err = -EEXIST; 1292b195167fSHerbert Xu rollback: 1293b195167fSHerbert Xu br->hash_max = old; 1294b195167fSHerbert Xu goto unlock; 1295b195167fSHerbert Xu } 1296b195167fSHerbert Xu 1297b195167fSHerbert Xu err = br_mdb_rehash(&br->mdb, br->hash_max, 1298b195167fSHerbert Xu br->hash_elasticity); 1299b195167fSHerbert Xu if (err) 1300b195167fSHerbert Xu goto rollback; 1301b195167fSHerbert Xu } 1302b195167fSHerbert Xu 1303b195167fSHerbert Xu unlock: 1304b195167fSHerbert Xu spin_unlock(&br->multicast_lock); 1305b195167fSHerbert Xu 1306b195167fSHerbert Xu return err; 1307b195167fSHerbert Xu } 1308