blob: eb5b256ffc8801ff7e187c64ee3dde5da268f570 [file] [log] [blame]
Herbert Xueb1d1642010-02-27 19:41:45 +00001/*
2 * Bridge multicast support.
3 *
4 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 */
12
13#include <linux/err.h>
14#include <linux/if_ether.h>
15#include <linux/igmp.h>
16#include <linux/jhash.h>
17#include <linux/kernel.h>
Herbert Xub1951672010-02-27 19:41:51 +000018#include <linux/log2.h>
Herbert Xueb1d1642010-02-27 19:41:45 +000019#include <linux/netdevice.h>
20#include <linux/netfilter_bridge.h>
21#include <linux/random.h>
22#include <linux/rculist.h>
23#include <linux/skbuff.h>
24#include <linux/slab.h>
25#include <linux/timer.h>
26#include <net/ip.h>
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090027#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
28#include <net/ipv6.h>
29#include <net/mld.h>
30#include <net/addrconf.h>
David S. Millerd4c4f072010-04-27 10:16:54 -070031#include <net/ip6_checksum.h>
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090032#endif
Herbert Xueb1d1642010-02-27 19:41:45 +000033
34#include "br_private.h"
35
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090036#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
37static inline int ipv6_is_local_multicast(const struct in6_addr *addr)
Herbert Xueb1d1642010-02-27 19:41:45 +000038{
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090039 if (ipv6_addr_is_multicast(addr) &&
40 IPV6_ADDR_MC_SCOPE(addr) <= IPV6_ADDR_SCOPE_LINKLOCAL)
41 return 1;
42 return 0;
43}
44#endif
45
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090046static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
47{
48 if (a->proto != b->proto)
49 return 0;
50 switch (a->proto) {
51 case htons(ETH_P_IP):
52 return a->u.ip4 == b->u.ip4;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090053#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
54 case htons(ETH_P_IPV6):
55 return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
56#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090057 }
58 return 0;
59}
60
61static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip)
Herbert Xueb1d1642010-02-27 19:41:45 +000062{
Eric Dumazet0eae88f2010-04-20 19:06:52 -070063 return jhash_1word(mdb->secret, (__force u32)ip) & (mdb->max - 1);
Herbert Xueb1d1642010-02-27 19:41:45 +000064}
65
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090066#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
67static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb,
68 const struct in6_addr *ip)
69{
70 return jhash2((__force u32 *)ip->s6_addr32, 4, mdb->secret) & (mdb->max - 1);
71}
72#endif
73
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090074static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb,
75 struct br_ip *ip)
76{
77 switch (ip->proto) {
78 case htons(ETH_P_IP):
79 return __br_ip4_hash(mdb, ip->u.ip4);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090080#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
81 case htons(ETH_P_IPV6):
82 return __br_ip6_hash(mdb, &ip->u.ip6);
83#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090084 }
85 return 0;
Herbert Xueb1d1642010-02-27 19:41:45 +000086}
87
88static struct net_bridge_mdb_entry *__br_mdb_ip_get(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090089 struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +000090{
91 struct net_bridge_mdb_entry *mp;
92 struct hlist_node *p;
93
Herbert Xu49f5fcf2010-03-05 21:07:39 +000094 hlist_for_each_entry_rcu(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) {
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090095 if (br_ip_equal(&mp->addr, dst))
Herbert Xueb1d1642010-02-27 19:41:45 +000096 return mp;
97 }
98
99 return NULL;
100}
101
Herbert Xu7f285fa2010-07-05 14:50:08 +0000102static struct net_bridge_mdb_entry *br_mdb_ip_get(
103 struct net_bridge_mdb_htable *mdb, struct br_ip *dst)
104{
105 if (!mdb)
106 return NULL;
107
108 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
109}
110
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900111static struct net_bridge_mdb_entry *br_mdb_ip4_get(
Herbert Xueb1d1642010-02-27 19:41:45 +0000112 struct net_bridge_mdb_htable *mdb, __be32 dst)
113{
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900114 struct br_ip br_dst;
Herbert Xu0821ec52010-03-15 20:38:25 -0700115
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900116 br_dst.u.ip4 = dst;
117 br_dst.proto = htons(ETH_P_IP);
118
Herbert Xu7f285fa2010-07-05 14:50:08 +0000119 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900120}
121
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900122#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
123static struct net_bridge_mdb_entry *br_mdb_ip6_get(
124 struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst)
125{
126 struct br_ip br_dst;
127
128 ipv6_addr_copy(&br_dst.u.ip6, dst);
129 br_dst.proto = htons(ETH_P_IPV6);
130
Herbert Xu7f285fa2010-07-05 14:50:08 +0000131 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900132}
133#endif
134
Herbert Xueb1d1642010-02-27 19:41:45 +0000135struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
136 struct sk_buff *skb)
137{
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900138 struct net_bridge_mdb_htable *mdb = br->mdb;
139 struct br_ip ip;
140
Herbert Xu7f285fa2010-07-05 14:50:08 +0000141 if (br->multicast_disabled)
Herbert Xueb1d1642010-02-27 19:41:45 +0000142 return NULL;
143
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900144 if (BR_INPUT_SKB_CB(skb)->igmp)
145 return NULL;
146
147 ip.proto = skb->protocol;
148
Herbert Xueb1d1642010-02-27 19:41:45 +0000149 switch (skb->protocol) {
150 case htons(ETH_P_IP):
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900151 ip.u.ip4 = ip_hdr(skb)->daddr;
152 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900153#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
154 case htons(ETH_P_IPV6):
155 ipv6_addr_copy(&ip.u.ip6, &ipv6_hdr(skb)->daddr);
156 break;
157#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900158 default:
159 return NULL;
Herbert Xueb1d1642010-02-27 19:41:45 +0000160 }
161
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900162 return br_mdb_ip_get(mdb, &ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000163}
164
165static void br_mdb_free(struct rcu_head *head)
166{
167 struct net_bridge_mdb_htable *mdb =
168 container_of(head, struct net_bridge_mdb_htable, rcu);
169 struct net_bridge_mdb_htable *old = mdb->old;
170
171 mdb->old = NULL;
172 kfree(old->mhash);
173 kfree(old);
174}
175
176static int br_mdb_copy(struct net_bridge_mdb_htable *new,
177 struct net_bridge_mdb_htable *old,
178 int elasticity)
179{
180 struct net_bridge_mdb_entry *mp;
181 struct hlist_node *p;
182 int maxlen;
183 int len;
184 int i;
185
186 for (i = 0; i < old->max; i++)
187 hlist_for_each_entry(mp, p, &old->mhash[i], hlist[old->ver])
188 hlist_add_head(&mp->hlist[new->ver],
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900189 &new->mhash[br_ip_hash(new, &mp->addr)]);
Herbert Xueb1d1642010-02-27 19:41:45 +0000190
191 if (!elasticity)
192 return 0;
193
194 maxlen = 0;
195 for (i = 0; i < new->max; i++) {
196 len = 0;
197 hlist_for_each_entry(mp, p, &new->mhash[i], hlist[new->ver])
198 len++;
199 if (len > maxlen)
200 maxlen = len;
201 }
202
203 return maxlen > elasticity ? -EINVAL : 0;
204}
205
206static void br_multicast_free_pg(struct rcu_head *head)
207{
208 struct net_bridge_port_group *p =
209 container_of(head, struct net_bridge_port_group, rcu);
210
211 kfree(p);
212}
213
214static void br_multicast_free_group(struct rcu_head *head)
215{
216 struct net_bridge_mdb_entry *mp =
217 container_of(head, struct net_bridge_mdb_entry, rcu);
218
219 kfree(mp);
220}
221
222static void br_multicast_group_expired(unsigned long data)
223{
224 struct net_bridge_mdb_entry *mp = (void *)data;
225 struct net_bridge *br = mp->br;
226 struct net_bridge_mdb_htable *mdb;
227
228 spin_lock(&br->multicast_lock);
229 if (!netif_running(br->dev) || timer_pending(&mp->timer))
230 goto out;
231
232 if (!hlist_unhashed(&mp->mglist))
233 hlist_del_init(&mp->mglist);
234
235 if (mp->ports)
236 goto out;
237
238 mdb = br->mdb;
239 hlist_del_rcu(&mp->hlist[mdb->ver]);
240 mdb->size--;
241
242 del_timer(&mp->query_timer);
243 call_rcu_bh(&mp->rcu, br_multicast_free_group);
244
245out:
246 spin_unlock(&br->multicast_lock);
247}
248
249static void br_multicast_del_pg(struct net_bridge *br,
250 struct net_bridge_port_group *pg)
251{
252 struct net_bridge_mdb_htable *mdb = br->mdb;
253 struct net_bridge_mdb_entry *mp;
254 struct net_bridge_port_group *p;
255 struct net_bridge_port_group **pp;
256
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900257 mp = br_mdb_ip_get(mdb, &pg->addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000258 if (WARN_ON(!mp))
259 return;
260
261 for (pp = &mp->ports; (p = *pp); pp = &p->next) {
262 if (p != pg)
263 continue;
264
stephen hemminger83f6a742010-04-27 15:01:06 +0000265 rcu_assign_pointer(*pp, p->next);
Herbert Xueb1d1642010-02-27 19:41:45 +0000266 hlist_del_init(&p->mglist);
267 del_timer(&p->timer);
268 del_timer(&p->query_timer);
269 call_rcu_bh(&p->rcu, br_multicast_free_pg);
270
271 if (!mp->ports && hlist_unhashed(&mp->mglist) &&
272 netif_running(br->dev))
273 mod_timer(&mp->timer, jiffies);
274
275 return;
276 }
277
278 WARN_ON(1);
279}
280
281static void br_multicast_port_group_expired(unsigned long data)
282{
283 struct net_bridge_port_group *pg = (void *)data;
284 struct net_bridge *br = pg->port->br;
285
286 spin_lock(&br->multicast_lock);
287 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
288 hlist_unhashed(&pg->mglist))
289 goto out;
290
291 br_multicast_del_pg(br, pg);
292
293out:
294 spin_unlock(&br->multicast_lock);
295}
296
297static int br_mdb_rehash(struct net_bridge_mdb_htable **mdbp, int max,
298 int elasticity)
299{
300 struct net_bridge_mdb_htable *old = *mdbp;
301 struct net_bridge_mdb_htable *mdb;
302 int err;
303
304 mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
305 if (!mdb)
306 return -ENOMEM;
307
308 mdb->max = max;
309 mdb->old = old;
310
311 mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
312 if (!mdb->mhash) {
313 kfree(mdb);
314 return -ENOMEM;
315 }
316
317 mdb->size = old ? old->size : 0;
318 mdb->ver = old ? old->ver ^ 1 : 0;
319
320 if (!old || elasticity)
321 get_random_bytes(&mdb->secret, sizeof(mdb->secret));
322 else
323 mdb->secret = old->secret;
324
325 if (!old)
326 goto out;
327
328 err = br_mdb_copy(mdb, old, elasticity);
329 if (err) {
330 kfree(mdb->mhash);
331 kfree(mdb);
332 return err;
333 }
334
335 call_rcu_bh(&mdb->rcu, br_mdb_free);
336
337out:
338 rcu_assign_pointer(*mdbp, mdb);
339
340 return 0;
341}
342
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900343static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
344 __be32 group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000345{
346 struct sk_buff *skb;
347 struct igmphdr *ih;
348 struct ethhdr *eth;
349 struct iphdr *iph;
350
351 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
352 sizeof(*ih) + 4);
353 if (!skb)
354 goto out;
355
356 skb->protocol = htons(ETH_P_IP);
357
358 skb_reset_mac_header(skb);
359 eth = eth_hdr(skb);
360
361 memcpy(eth->h_source, br->dev->dev_addr, 6);
362 eth->h_dest[0] = 1;
363 eth->h_dest[1] = 0;
364 eth->h_dest[2] = 0x5e;
365 eth->h_dest[3] = 0;
366 eth->h_dest[4] = 0;
367 eth->h_dest[5] = 1;
368 eth->h_proto = htons(ETH_P_IP);
369 skb_put(skb, sizeof(*eth));
370
371 skb_set_network_header(skb, skb->len);
372 iph = ip_hdr(skb);
373
374 iph->version = 4;
375 iph->ihl = 6;
376 iph->tos = 0xc0;
377 iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4);
378 iph->id = 0;
379 iph->frag_off = htons(IP_DF);
380 iph->ttl = 1;
381 iph->protocol = IPPROTO_IGMP;
382 iph->saddr = 0;
383 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
384 ((u8 *)&iph[1])[0] = IPOPT_RA;
385 ((u8 *)&iph[1])[1] = 4;
386 ((u8 *)&iph[1])[2] = 0;
387 ((u8 *)&iph[1])[3] = 0;
388 ip_send_check(iph);
389 skb_put(skb, 24);
390
391 skb_set_transport_header(skb, skb->len);
392 ih = igmp_hdr(skb);
393 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
394 ih->code = (group ? br->multicast_last_member_interval :
395 br->multicast_query_response_interval) /
396 (HZ / IGMP_TIMER_SCALE);
397 ih->group = group;
398 ih->csum = 0;
399 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
400 skb_put(skb, sizeof(*ih));
401
402 __skb_pull(skb, sizeof(*eth));
403
404out:
405 return skb;
406}
407
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900408#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
409static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
410 struct in6_addr *group)
411{
412 struct sk_buff *skb;
413 struct ipv6hdr *ip6h;
414 struct mld_msg *mldq;
415 struct ethhdr *eth;
416 u8 *hopopt;
417 unsigned long interval;
418
419 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
420 8 + sizeof(*mldq));
421 if (!skb)
422 goto out;
423
424 skb->protocol = htons(ETH_P_IPV6);
425
426 /* Ethernet header */
427 skb_reset_mac_header(skb);
428 eth = eth_hdr(skb);
429
430 memcpy(eth->h_source, br->dev->dev_addr, 6);
431 ipv6_eth_mc_map(group, eth->h_dest);
432 eth->h_proto = htons(ETH_P_IPV6);
433 skb_put(skb, sizeof(*eth));
434
435 /* IPv6 header + HbH option */
436 skb_set_network_header(skb, skb->len);
437 ip6h = ipv6_hdr(skb);
438
439 *(__force __be32 *)ip6h = htonl(0x60000000);
440 ip6h->payload_len = 8 + sizeof(*mldq);
441 ip6h->nexthdr = IPPROTO_HOPOPTS;
442 ip6h->hop_limit = 1;
443 ipv6_addr_set(&ip6h->saddr, 0, 0, 0, 0);
444 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
445
446 hopopt = (u8 *)(ip6h + 1);
447 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
448 hopopt[1] = 0; /* length of HbH */
449 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
450 hopopt[3] = 2; /* Length of RA Option */
451 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
452 hopopt[5] = 0;
453 hopopt[6] = IPV6_TLV_PAD0; /* Pad0 */
454 hopopt[7] = IPV6_TLV_PAD0; /* Pad0 */
455
456 skb_put(skb, sizeof(*ip6h) + 8);
457
458 /* ICMPv6 */
459 skb_set_transport_header(skb, skb->len);
460 mldq = (struct mld_msg *) icmp6_hdr(skb);
461
462 interval = ipv6_addr_any(group) ? br->multicast_last_member_interval :
463 br->multicast_query_response_interval;
464
465 mldq->mld_type = ICMPV6_MGM_QUERY;
466 mldq->mld_code = 0;
467 mldq->mld_cksum = 0;
468 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
469 mldq->mld_reserved = 0;
470 ipv6_addr_copy(&mldq->mld_mca, group);
471
472 /* checksum */
473 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
474 sizeof(*mldq), IPPROTO_ICMPV6,
475 csum_partial(mldq,
476 sizeof(*mldq), 0));
477 skb_put(skb, sizeof(*mldq));
478
479 __skb_pull(skb, sizeof(*eth));
480
481out:
482 return skb;
483}
484#endif
485
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900486static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
487 struct br_ip *addr)
488{
489 switch (addr->proto) {
490 case htons(ETH_P_IP):
491 return br_ip4_multicast_alloc_query(br, addr->u.ip4);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900492#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
493 case htons(ETH_P_IPV6):
494 return br_ip6_multicast_alloc_query(br, &addr->u.ip6);
495#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900496 }
497 return NULL;
498}
499
Herbert Xueb1d1642010-02-27 19:41:45 +0000500static void br_multicast_send_group_query(struct net_bridge_mdb_entry *mp)
501{
502 struct net_bridge *br = mp->br;
503 struct sk_buff *skb;
504
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900505 skb = br_multicast_alloc_query(br, &mp->addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000506 if (!skb)
507 goto timer;
508
509 netif_rx(skb);
510
511timer:
512 if (++mp->queries_sent < br->multicast_last_member_count)
513 mod_timer(&mp->query_timer,
514 jiffies + br->multicast_last_member_interval);
515}
516
517static void br_multicast_group_query_expired(unsigned long data)
518{
519 struct net_bridge_mdb_entry *mp = (void *)data;
520 struct net_bridge *br = mp->br;
521
522 spin_lock(&br->multicast_lock);
523 if (!netif_running(br->dev) || hlist_unhashed(&mp->mglist) ||
524 mp->queries_sent >= br->multicast_last_member_count)
525 goto out;
526
527 br_multicast_send_group_query(mp);
528
529out:
530 spin_unlock(&br->multicast_lock);
531}
532
533static void br_multicast_send_port_group_query(struct net_bridge_port_group *pg)
534{
535 struct net_bridge_port *port = pg->port;
536 struct net_bridge *br = port->br;
537 struct sk_buff *skb;
538
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900539 skb = br_multicast_alloc_query(br, &pg->addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000540 if (!skb)
541 goto timer;
542
543 br_deliver(port, skb);
544
545timer:
546 if (++pg->queries_sent < br->multicast_last_member_count)
547 mod_timer(&pg->query_timer,
548 jiffies + br->multicast_last_member_interval);
549}
550
551static void br_multicast_port_group_query_expired(unsigned long data)
552{
553 struct net_bridge_port_group *pg = (void *)data;
554 struct net_bridge_port *port = pg->port;
555 struct net_bridge *br = port->br;
556
557 spin_lock(&br->multicast_lock);
558 if (!netif_running(br->dev) || hlist_unhashed(&pg->mglist) ||
559 pg->queries_sent >= br->multicast_last_member_count)
560 goto out;
561
562 br_multicast_send_port_group_query(pg);
563
564out:
565 spin_unlock(&br->multicast_lock);
566}
567
568static struct net_bridge_mdb_entry *br_multicast_get_group(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900569 struct net_bridge *br, struct net_bridge_port *port,
570 struct br_ip *group, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +0000571{
572 struct net_bridge_mdb_htable *mdb = br->mdb;
573 struct net_bridge_mdb_entry *mp;
574 struct hlist_node *p;
575 unsigned count = 0;
576 unsigned max;
577 int elasticity;
578 int err;
579
580 hlist_for_each_entry(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) {
581 count++;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900582 if (unlikely(br_ip_equal(group, &mp->addr)))
Herbert Xueb1d1642010-02-27 19:41:45 +0000583 return mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000584 }
585
586 elasticity = 0;
587 max = mdb->max;
588
589 if (unlikely(count > br->hash_elasticity && count)) {
590 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000591 br_info(br, "Multicast hash table "
592 "chain limit reached: %s\n",
593 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000594
595 elasticity = br->hash_elasticity;
596 }
597
598 if (mdb->size >= max) {
599 max *= 2;
600 if (unlikely(max >= br->hash_max)) {
stephen hemminger28a16c92010-05-10 09:31:09 +0000601 br_warn(br, "Multicast hash table maximum "
602 "reached, disabling snooping: %s, %d\n",
603 port ? port->dev->name : br->dev->name, max);
Herbert Xueb1d1642010-02-27 19:41:45 +0000604 err = -E2BIG;
605disable:
606 br->multicast_disabled = 1;
607 goto err;
608 }
609 }
610
611 if (max > mdb->max || elasticity) {
612 if (mdb->old) {
613 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000614 br_info(br, "Multicast hash table "
615 "on fire: %s\n",
616 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000617 err = -EEXIST;
618 goto err;
619 }
620
621 err = br_mdb_rehash(&br->mdb, max, elasticity);
622 if (err) {
stephen hemminger28a16c92010-05-10 09:31:09 +0000623 br_warn(br, "Cannot rehash multicast "
624 "hash table, disabling snooping: %s, %d, %d\n",
625 port ? port->dev->name : br->dev->name,
626 mdb->size, err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000627 goto disable;
628 }
629
630 err = -EAGAIN;
631 goto err;
632 }
633
634 return NULL;
635
636err:
637 mp = ERR_PTR(err);
638 return mp;
639}
640
641static struct net_bridge_mdb_entry *br_multicast_new_group(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900642 struct net_bridge *br, struct net_bridge_port *port,
643 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000644{
645 struct net_bridge_mdb_htable *mdb = br->mdb;
646 struct net_bridge_mdb_entry *mp;
647 int hash;
648
649 if (!mdb) {
650 if (br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0))
651 return NULL;
652 goto rehash;
653 }
654
655 hash = br_ip_hash(mdb, group);
656 mp = br_multicast_get_group(br, port, group, hash);
657 switch (PTR_ERR(mp)) {
658 case 0:
659 break;
660
661 case -EAGAIN:
662rehash:
663 mdb = br->mdb;
664 hash = br_ip_hash(mdb, group);
665 break;
666
667 default:
668 goto out;
669 }
670
671 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
672 if (unlikely(!mp))
673 goto out;
674
675 mp->br = br;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900676 mp->addr = *group;
Herbert Xueb1d1642010-02-27 19:41:45 +0000677 setup_timer(&mp->timer, br_multicast_group_expired,
678 (unsigned long)mp);
679 setup_timer(&mp->query_timer, br_multicast_group_query_expired,
680 (unsigned long)mp);
681
682 hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
683 mdb->size++;
684
685out:
686 return mp;
687}
688
689static int br_multicast_add_group(struct net_bridge *br,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900690 struct net_bridge_port *port,
691 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000692{
693 struct net_bridge_mdb_entry *mp;
694 struct net_bridge_port_group *p;
695 struct net_bridge_port_group **pp;
696 unsigned long now = jiffies;
697 int err;
698
Herbert Xueb1d1642010-02-27 19:41:45 +0000699 spin_lock(&br->multicast_lock);
700 if (!netif_running(br->dev) ||
701 (port && port->state == BR_STATE_DISABLED))
702 goto out;
703
704 mp = br_multicast_new_group(br, port, group);
705 err = PTR_ERR(mp);
706 if (unlikely(IS_ERR(mp) || !mp))
707 goto err;
708
709 if (!port) {
710 hlist_add_head(&mp->mglist, &br->mglist);
711 mod_timer(&mp->timer, now + br->multicast_membership_interval);
712 goto out;
713 }
714
715 for (pp = &mp->ports; (p = *pp); pp = &p->next) {
716 if (p->port == port)
717 goto found;
718 if ((unsigned long)p->port < (unsigned long)port)
719 break;
720 }
721
722 p = kzalloc(sizeof(*p), GFP_ATOMIC);
723 err = -ENOMEM;
724 if (unlikely(!p))
725 goto err;
726
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900727 p->addr = *group;
Herbert Xueb1d1642010-02-27 19:41:45 +0000728 p->port = port;
729 p->next = *pp;
730 hlist_add_head(&p->mglist, &port->mglist);
731 setup_timer(&p->timer, br_multicast_port_group_expired,
732 (unsigned long)p);
733 setup_timer(&p->query_timer, br_multicast_port_group_query_expired,
734 (unsigned long)p);
735
736 rcu_assign_pointer(*pp, p);
737
738found:
739 mod_timer(&p->timer, now + br->multicast_membership_interval);
740out:
741 err = 0;
742
743err:
744 spin_unlock(&br->multicast_lock);
745 return err;
746}
747
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900748static int br_ip4_multicast_add_group(struct net_bridge *br,
749 struct net_bridge_port *port,
750 __be32 group)
751{
752 struct br_ip br_group;
753
754 if (ipv4_is_local_multicast(group))
755 return 0;
756
757 br_group.u.ip4 = group;
758 br_group.proto = htons(ETH_P_IP);
759
760 return br_multicast_add_group(br, port, &br_group);
761}
762
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900763#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
764static int br_ip6_multicast_add_group(struct net_bridge *br,
765 struct net_bridge_port *port,
766 const struct in6_addr *group)
767{
768 struct br_ip br_group;
769
770 if (ipv6_is_local_multicast(group))
771 return 0;
772
773 ipv6_addr_copy(&br_group.u.ip6, group);
774 br_group.proto = htons(ETH_P_IP);
775
776 return br_multicast_add_group(br, port, &br_group);
777}
778#endif
779
Herbert Xueb1d1642010-02-27 19:41:45 +0000780static void br_multicast_router_expired(unsigned long data)
781{
782 struct net_bridge_port *port = (void *)data;
783 struct net_bridge *br = port->br;
784
785 spin_lock(&br->multicast_lock);
786 if (port->multicast_router != 1 ||
787 timer_pending(&port->multicast_router_timer) ||
788 hlist_unhashed(&port->rlist))
789 goto out;
790
791 hlist_del_init_rcu(&port->rlist);
792
793out:
794 spin_unlock(&br->multicast_lock);
795}
796
797static void br_multicast_local_router_expired(unsigned long data)
798{
799}
800
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900801static void __br_multicast_send_query(struct net_bridge *br,
802 struct net_bridge_port *port,
803 struct br_ip *ip)
Herbert Xueb1d1642010-02-27 19:41:45 +0000804{
Herbert Xueb1d1642010-02-27 19:41:45 +0000805 struct sk_buff *skb;
806
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900807 skb = br_multicast_alloc_query(br, ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000808 if (!skb)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900809 return;
Herbert Xueb1d1642010-02-27 19:41:45 +0000810
811 if (port) {
812 __skb_push(skb, sizeof(struct ethhdr));
813 skb->dev = port->dev;
Jan Engelhardt713aefa2010-03-23 04:07:21 +0100814 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
Herbert Xueb1d1642010-02-27 19:41:45 +0000815 dev_queue_xmit);
816 } else
817 netif_rx(skb);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900818}
Herbert Xueb1d1642010-02-27 19:41:45 +0000819
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900820static void br_multicast_send_query(struct net_bridge *br,
821 struct net_bridge_port *port, u32 sent)
822{
823 unsigned long time;
824 struct br_ip br_group;
825
826 if (!netif_running(br->dev) || br->multicast_disabled ||
827 timer_pending(&br->multicast_querier_timer))
828 return;
829
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900830 memset(&br_group.u, 0, sizeof(br_group.u));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900831
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900832 br_group.proto = htons(ETH_P_IP);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900833 __br_multicast_send_query(br, port, &br_group);
834
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900835#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
836 br_group.proto = htons(ETH_P_IPV6);
837 __br_multicast_send_query(br, port, &br_group);
838#endif
839
Herbert Xueb1d1642010-02-27 19:41:45 +0000840 time = jiffies;
841 time += sent < br->multicast_startup_query_count ?
842 br->multicast_startup_query_interval :
843 br->multicast_query_interval;
844 mod_timer(port ? &port->multicast_query_timer :
845 &br->multicast_query_timer, time);
846}
847
848static void br_multicast_port_query_expired(unsigned long data)
849{
850 struct net_bridge_port *port = (void *)data;
851 struct net_bridge *br = port->br;
852
853 spin_lock(&br->multicast_lock);
Dan Carpenter02a780c2010-03-06 01:14:09 +0000854 if (port->state == BR_STATE_DISABLED ||
855 port->state == BR_STATE_BLOCKING)
Herbert Xueb1d1642010-02-27 19:41:45 +0000856 goto out;
857
858 if (port->multicast_startup_queries_sent <
859 br->multicast_startup_query_count)
860 port->multicast_startup_queries_sent++;
861
862 br_multicast_send_query(port->br, port,
863 port->multicast_startup_queries_sent);
864
865out:
866 spin_unlock(&br->multicast_lock);
867}
868
869void br_multicast_add_port(struct net_bridge_port *port)
870{
871 port->multicast_router = 1;
872
873 setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
874 (unsigned long)port);
875 setup_timer(&port->multicast_query_timer,
876 br_multicast_port_query_expired, (unsigned long)port);
877}
878
879void br_multicast_del_port(struct net_bridge_port *port)
880{
881 del_timer_sync(&port->multicast_router_timer);
882}
883
Herbert Xu561f1102010-02-27 19:41:50 +0000884static void __br_multicast_enable_port(struct net_bridge_port *port)
885{
886 port->multicast_startup_queries_sent = 0;
887
888 if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 ||
889 del_timer(&port->multicast_query_timer))
890 mod_timer(&port->multicast_query_timer, jiffies);
891}
892
Herbert Xueb1d1642010-02-27 19:41:45 +0000893void br_multicast_enable_port(struct net_bridge_port *port)
894{
895 struct net_bridge *br = port->br;
896
897 spin_lock(&br->multicast_lock);
898 if (br->multicast_disabled || !netif_running(br->dev))
899 goto out;
900
Herbert Xu561f1102010-02-27 19:41:50 +0000901 __br_multicast_enable_port(port);
Herbert Xueb1d1642010-02-27 19:41:45 +0000902
903out:
904 spin_unlock(&br->multicast_lock);
905}
906
907void br_multicast_disable_port(struct net_bridge_port *port)
908{
909 struct net_bridge *br = port->br;
910 struct net_bridge_port_group *pg;
911 struct hlist_node *p, *n;
912
913 spin_lock(&br->multicast_lock);
914 hlist_for_each_entry_safe(pg, p, n, &port->mglist, mglist)
915 br_multicast_del_pg(br, pg);
916
917 if (!hlist_unhashed(&port->rlist))
918 hlist_del_init_rcu(&port->rlist);
919 del_timer(&port->multicast_router_timer);
920 del_timer(&port->multicast_query_timer);
921 spin_unlock(&br->multicast_lock);
922}
923
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900924static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
925 struct net_bridge_port *port,
926 struct sk_buff *skb)
Herbert Xueb1d1642010-02-27 19:41:45 +0000927{
928 struct igmpv3_report *ih;
929 struct igmpv3_grec *grec;
930 int i;
931 int len;
932 int num;
933 int type;
934 int err = 0;
935 __be32 group;
936
937 if (!pskb_may_pull(skb, sizeof(*ih)))
938 return -EINVAL;
939
940 ih = igmpv3_report_hdr(skb);
941 num = ntohs(ih->ngrec);
942 len = sizeof(*ih);
943
944 for (i = 0; i < num; i++) {
945 len += sizeof(*grec);
946 if (!pskb_may_pull(skb, len))
947 return -EINVAL;
948
Herbert Xufd218cf2010-04-07 21:20:47 -0700949 grec = (void *)(skb->data + len - sizeof(*grec));
Herbert Xueb1d1642010-02-27 19:41:45 +0000950 group = grec->grec_mca;
951 type = grec->grec_type;
952
Eric Dumazet8eabf952010-04-20 03:20:05 +0000953 len += ntohs(grec->grec_nsrcs) * 4;
Herbert Xueb1d1642010-02-27 19:41:45 +0000954 if (!pskb_may_pull(skb, len))
955 return -EINVAL;
956
957 /* We treat this as an IGMPv2 report for now. */
958 switch (type) {
959 case IGMPV3_MODE_IS_INCLUDE:
960 case IGMPV3_MODE_IS_EXCLUDE:
961 case IGMPV3_CHANGE_TO_INCLUDE:
962 case IGMPV3_CHANGE_TO_EXCLUDE:
963 case IGMPV3_ALLOW_NEW_SOURCES:
964 case IGMPV3_BLOCK_OLD_SOURCES:
965 break;
966
967 default:
968 continue;
969 }
970
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900971 err = br_ip4_multicast_add_group(br, port, group);
Herbert Xueb1d1642010-02-27 19:41:45 +0000972 if (err)
973 break;
974 }
975
976 return err;
977}
978
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900979#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
980static int br_ip6_multicast_mld2_report(struct net_bridge *br,
981 struct net_bridge_port *port,
982 struct sk_buff *skb)
983{
984 struct icmp6hdr *icmp6h;
985 struct mld2_grec *grec;
986 int i;
987 int len;
988 int num;
989 int err = 0;
990
991 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
992 return -EINVAL;
993
994 icmp6h = icmp6_hdr(skb);
995 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
996 len = sizeof(*icmp6h);
997
998 for (i = 0; i < num; i++) {
999 __be16 *nsrcs, _nsrcs;
1000
1001 nsrcs = skb_header_pointer(skb,
1002 len + offsetof(struct mld2_grec,
1003 grec_mca),
1004 sizeof(_nsrcs), &_nsrcs);
1005 if (!nsrcs)
1006 return -EINVAL;
1007
1008 if (!pskb_may_pull(skb,
1009 len + sizeof(*grec) +
1010 sizeof(struct in6_addr) * (*nsrcs)))
1011 return -EINVAL;
1012
1013 grec = (struct mld2_grec *)(skb->data + len);
1014 len += sizeof(*grec) + sizeof(struct in6_addr) * (*nsrcs);
1015
1016 /* We treat these as MLDv1 reports for now. */
1017 switch (grec->grec_type) {
1018 case MLD2_MODE_IS_INCLUDE:
1019 case MLD2_MODE_IS_EXCLUDE:
1020 case MLD2_CHANGE_TO_INCLUDE:
1021 case MLD2_CHANGE_TO_EXCLUDE:
1022 case MLD2_ALLOW_NEW_SOURCES:
1023 case MLD2_BLOCK_OLD_SOURCES:
1024 break;
1025
1026 default:
1027 continue;
1028 }
1029
1030 err = br_ip6_multicast_add_group(br, port, &grec->grec_mca);
1031 if (!err)
1032 break;
1033 }
1034
1035 return err;
1036}
1037#endif
1038
stephen hemminger7e80c122010-04-27 15:01:04 +00001039/*
1040 * Add port to rotuer_list
1041 * list is maintained ordered by pointer value
1042 * and locked by br->multicast_lock and RCU
1043 */
Herbert Xu0909e112010-02-27 19:41:49 +00001044static void br_multicast_add_router(struct net_bridge *br,
1045 struct net_bridge_port *port)
1046{
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001047 struct net_bridge_port *p;
stephen hemminger7e80c122010-04-27 15:01:04 +00001048 struct hlist_node *n, *slot = NULL;
Herbert Xu0909e112010-02-27 19:41:49 +00001049
David S. Miller709b9322010-04-27 16:49:58 -07001050 hlist_for_each_entry(p, n, &br->router_list, rlist) {
stephen hemminger7e80c122010-04-27 15:01:04 +00001051 if ((unsigned long) port >= (unsigned long) p)
1052 break;
1053 slot = n;
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001054 }
Herbert Xu0909e112010-02-27 19:41:49 +00001055
stephen hemminger7e80c122010-04-27 15:01:04 +00001056 if (slot)
1057 hlist_add_after_rcu(slot, &port->rlist);
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001058 else
1059 hlist_add_head_rcu(&port->rlist, &br->router_list);
Herbert Xu0909e112010-02-27 19:41:49 +00001060}
1061
Herbert Xueb1d1642010-02-27 19:41:45 +00001062static void br_multicast_mark_router(struct net_bridge *br,
1063 struct net_bridge_port *port)
1064{
1065 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +00001066
1067 if (!port) {
1068 if (br->multicast_router == 1)
1069 mod_timer(&br->multicast_router_timer,
1070 now + br->multicast_querier_interval);
1071 return;
1072 }
1073
1074 if (port->multicast_router != 1)
1075 return;
1076
1077 if (!hlist_unhashed(&port->rlist))
1078 goto timer;
1079
Herbert Xu0909e112010-02-27 19:41:49 +00001080 br_multicast_add_router(br, port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001081
1082timer:
1083 mod_timer(&port->multicast_router_timer,
1084 now + br->multicast_querier_interval);
1085}
1086
1087static void br_multicast_query_received(struct net_bridge *br,
1088 struct net_bridge_port *port,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001089 int saddr)
Herbert Xueb1d1642010-02-27 19:41:45 +00001090{
1091 if (saddr)
1092 mod_timer(&br->multicast_querier_timer,
1093 jiffies + br->multicast_querier_interval);
1094 else if (timer_pending(&br->multicast_querier_timer))
1095 return;
1096
1097 br_multicast_mark_router(br, port);
1098}
1099
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001100static int br_ip4_multicast_query(struct net_bridge *br,
1101 struct net_bridge_port *port,
1102 struct sk_buff *skb)
Herbert Xueb1d1642010-02-27 19:41:45 +00001103{
1104 struct iphdr *iph = ip_hdr(skb);
1105 struct igmphdr *ih = igmp_hdr(skb);
1106 struct net_bridge_mdb_entry *mp;
1107 struct igmpv3_query *ih3;
1108 struct net_bridge_port_group *p;
1109 struct net_bridge_port_group **pp;
1110 unsigned long max_delay;
1111 unsigned long now = jiffies;
1112 __be32 group;
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001113 int err = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001114
1115 spin_lock(&br->multicast_lock);
1116 if (!netif_running(br->dev) ||
1117 (port && port->state == BR_STATE_DISABLED))
1118 goto out;
1119
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001120 br_multicast_query_received(br, port, !!iph->saddr);
Herbert Xueb1d1642010-02-27 19:41:45 +00001121
1122 group = ih->group;
1123
1124 if (skb->len == sizeof(*ih)) {
1125 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1126
1127 if (!max_delay) {
1128 max_delay = 10 * HZ;
1129 group = 0;
1130 }
1131 } else {
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001132 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) {
1133 err = -EINVAL;
1134 goto out;
1135 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001136
1137 ih3 = igmpv3_query_hdr(skb);
1138 if (ih3->nsrcs)
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001139 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001140
YOSHIFUJI Hideaki / 吉藤英明0ba8c9e2010-03-15 19:27:00 +00001141 max_delay = ih3->code ?
1142 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
Herbert Xueb1d1642010-02-27 19:41:45 +00001143 }
1144
1145 if (!group)
1146 goto out;
1147
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001148 mp = br_mdb_ip4_get(br->mdb, group);
Herbert Xueb1d1642010-02-27 19:41:45 +00001149 if (!mp)
1150 goto out;
1151
1152 max_delay *= br->multicast_last_member_count;
1153
1154 if (!hlist_unhashed(&mp->mglist) &&
1155 (timer_pending(&mp->timer) ?
1156 time_after(mp->timer.expires, now + max_delay) :
1157 try_to_del_timer_sync(&mp->timer) >= 0))
1158 mod_timer(&mp->timer, now + max_delay);
1159
1160 for (pp = &mp->ports; (p = *pp); pp = &p->next) {
1161 if (timer_pending(&p->timer) ?
1162 time_after(p->timer.expires, now + max_delay) :
1163 try_to_del_timer_sync(&p->timer) >= 0)
1164 mod_timer(&mp->timer, now + max_delay);
1165 }
1166
1167out:
1168 spin_unlock(&br->multicast_lock);
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001169 return err;
Herbert Xueb1d1642010-02-27 19:41:45 +00001170}
1171
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001172#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1173static int br_ip6_multicast_query(struct net_bridge *br,
1174 struct net_bridge_port *port,
1175 struct sk_buff *skb)
1176{
1177 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1178 struct mld_msg *mld = (struct mld_msg *) icmp6_hdr(skb);
1179 struct net_bridge_mdb_entry *mp;
1180 struct mld2_query *mld2q;
1181 struct net_bridge_port_group *p, **pp;
1182 unsigned long max_delay;
1183 unsigned long now = jiffies;
1184 struct in6_addr *group = NULL;
1185 int err = 0;
1186
1187 spin_lock(&br->multicast_lock);
1188 if (!netif_running(br->dev) ||
1189 (port && port->state == BR_STATE_DISABLED))
1190 goto out;
1191
1192 br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->saddr));
1193
1194 if (skb->len == sizeof(*mld)) {
1195 if (!pskb_may_pull(skb, sizeof(*mld))) {
1196 err = -EINVAL;
1197 goto out;
1198 }
1199 mld = (struct mld_msg *) icmp6_hdr(skb);
1200 max_delay = msecs_to_jiffies(htons(mld->mld_maxdelay));
1201 if (max_delay)
1202 group = &mld->mld_mca;
1203 } else if (skb->len >= sizeof(*mld2q)) {
1204 if (!pskb_may_pull(skb, sizeof(*mld2q))) {
1205 err = -EINVAL;
1206 goto out;
1207 }
1208 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1209 if (!mld2q->mld2q_nsrcs)
1210 group = &mld2q->mld2q_mca;
1211 max_delay = mld2q->mld2q_mrc ? MLDV2_MRC(mld2q->mld2q_mrc) : 1;
1212 }
1213
1214 if (!group)
1215 goto out;
1216
1217 mp = br_mdb_ip6_get(br->mdb, group);
1218 if (!mp)
1219 goto out;
1220
1221 max_delay *= br->multicast_last_member_count;
1222 if (!hlist_unhashed(&mp->mglist) &&
1223 (timer_pending(&mp->timer) ?
1224 time_after(mp->timer.expires, now + max_delay) :
1225 try_to_del_timer_sync(&mp->timer) >= 0))
1226 mod_timer(&mp->timer, now + max_delay);
1227
1228 for (pp = &mp->ports; (p = *pp); pp = &p->next) {
1229 if (timer_pending(&p->timer) ?
1230 time_after(p->timer.expires, now + max_delay) :
1231 try_to_del_timer_sync(&p->timer) >= 0)
1232 mod_timer(&mp->timer, now + max_delay);
1233 }
1234
1235out:
1236 spin_unlock(&br->multicast_lock);
1237 return err;
1238}
1239#endif
1240
Herbert Xueb1d1642010-02-27 19:41:45 +00001241static void br_multicast_leave_group(struct net_bridge *br,
1242 struct net_bridge_port *port,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001243 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +00001244{
1245 struct net_bridge_mdb_htable *mdb;
1246 struct net_bridge_mdb_entry *mp;
1247 struct net_bridge_port_group *p;
1248 unsigned long now;
1249 unsigned long time;
1250
Herbert Xueb1d1642010-02-27 19:41:45 +00001251 spin_lock(&br->multicast_lock);
1252 if (!netif_running(br->dev) ||
1253 (port && port->state == BR_STATE_DISABLED) ||
1254 timer_pending(&br->multicast_querier_timer))
1255 goto out;
1256
1257 mdb = br->mdb;
1258 mp = br_mdb_ip_get(mdb, group);
1259 if (!mp)
1260 goto out;
1261
1262 now = jiffies;
1263 time = now + br->multicast_last_member_count *
1264 br->multicast_last_member_interval;
1265
1266 if (!port) {
1267 if (!hlist_unhashed(&mp->mglist) &&
1268 (timer_pending(&mp->timer) ?
1269 time_after(mp->timer.expires, time) :
1270 try_to_del_timer_sync(&mp->timer) >= 0)) {
1271 mod_timer(&mp->timer, time);
1272
1273 mp->queries_sent = 0;
1274 mod_timer(&mp->query_timer, now);
1275 }
1276
1277 goto out;
1278 }
1279
1280 for (p = mp->ports; p; p = p->next) {
1281 if (p->port != port)
1282 continue;
1283
1284 if (!hlist_unhashed(&p->mglist) &&
1285 (timer_pending(&p->timer) ?
1286 time_after(p->timer.expires, time) :
1287 try_to_del_timer_sync(&p->timer) >= 0)) {
1288 mod_timer(&p->timer, time);
1289
1290 p->queries_sent = 0;
1291 mod_timer(&p->query_timer, now);
1292 }
1293
1294 break;
1295 }
1296
1297out:
1298 spin_unlock(&br->multicast_lock);
1299}
1300
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001301static void br_ip4_multicast_leave_group(struct net_bridge *br,
1302 struct net_bridge_port *port,
1303 __be32 group)
1304{
1305 struct br_ip br_group;
1306
1307 if (ipv4_is_local_multicast(group))
1308 return;
1309
1310 br_group.u.ip4 = group;
1311 br_group.proto = htons(ETH_P_IP);
1312
1313 br_multicast_leave_group(br, port, &br_group);
1314}
1315
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001316#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1317static void br_ip6_multicast_leave_group(struct net_bridge *br,
1318 struct net_bridge_port *port,
1319 const struct in6_addr *group)
1320{
1321 struct br_ip br_group;
1322
1323 if (ipv6_is_local_multicast(group))
1324 return;
1325
1326 ipv6_addr_copy(&br_group.u.ip6, group);
1327 br_group.proto = htons(ETH_P_IPV6);
1328
1329 br_multicast_leave_group(br, port, &br_group);
1330}
1331#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001332
Herbert Xueb1d1642010-02-27 19:41:45 +00001333static int br_multicast_ipv4_rcv(struct net_bridge *br,
1334 struct net_bridge_port *port,
1335 struct sk_buff *skb)
1336{
1337 struct sk_buff *skb2 = skb;
1338 struct iphdr *iph;
1339 struct igmphdr *ih;
1340 unsigned len;
1341 unsigned offset;
1342 int err;
1343
Herbert Xueb1d1642010-02-27 19:41:45 +00001344 /* We treat OOM as packet loss for now. */
1345 if (!pskb_may_pull(skb, sizeof(*iph)))
1346 return -EINVAL;
1347
1348 iph = ip_hdr(skb);
1349
1350 if (iph->ihl < 5 || iph->version != 4)
1351 return -EINVAL;
1352
1353 if (!pskb_may_pull(skb, ip_hdrlen(skb)))
1354 return -EINVAL;
1355
1356 iph = ip_hdr(skb);
1357
1358 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1359 return -EINVAL;
1360
1361 if (iph->protocol != IPPROTO_IGMP)
1362 return 0;
1363
1364 len = ntohs(iph->tot_len);
1365 if (skb->len < len || len < ip_hdrlen(skb))
1366 return -EINVAL;
1367
1368 if (skb->len > len) {
1369 skb2 = skb_clone(skb, GFP_ATOMIC);
1370 if (!skb2)
1371 return -ENOMEM;
1372
1373 err = pskb_trim_rcsum(skb2, len);
1374 if (err)
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001375 goto err_out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001376 }
1377
1378 len -= ip_hdrlen(skb2);
1379 offset = skb_network_offset(skb2) + ip_hdrlen(skb2);
1380 __skb_pull(skb2, offset);
1381 skb_reset_transport_header(skb2);
1382
1383 err = -EINVAL;
1384 if (!pskb_may_pull(skb2, sizeof(*ih)))
1385 goto out;
1386
Herbert Xueb1d1642010-02-27 19:41:45 +00001387 switch (skb2->ip_summed) {
1388 case CHECKSUM_COMPLETE:
1389 if (!csum_fold(skb2->csum))
1390 break;
1391 /* fall through */
1392 case CHECKSUM_NONE:
1393 skb2->csum = 0;
1394 if (skb_checksum_complete(skb2))
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001395 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001396 }
1397
1398 err = 0;
1399
1400 BR_INPUT_SKB_CB(skb)->igmp = 1;
1401 ih = igmp_hdr(skb2);
1402
1403 switch (ih->type) {
1404 case IGMP_HOST_MEMBERSHIP_REPORT:
1405 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1406 BR_INPUT_SKB_CB(skb2)->mrouters_only = 1;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001407 err = br_ip4_multicast_add_group(br, port, ih->group);
Herbert Xueb1d1642010-02-27 19:41:45 +00001408 break;
1409 case IGMPV3_HOST_MEMBERSHIP_REPORT:
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001410 err = br_ip4_multicast_igmp3_report(br, port, skb2);
Herbert Xueb1d1642010-02-27 19:41:45 +00001411 break;
1412 case IGMP_HOST_MEMBERSHIP_QUERY:
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001413 err = br_ip4_multicast_query(br, port, skb2);
Herbert Xueb1d1642010-02-27 19:41:45 +00001414 break;
1415 case IGMP_HOST_LEAVE_MESSAGE:
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001416 br_ip4_multicast_leave_group(br, port, ih->group);
Herbert Xueb1d1642010-02-27 19:41:45 +00001417 break;
1418 }
1419
1420out:
1421 __skb_push(skb2, offset);
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001422err_out:
Herbert Xueb1d1642010-02-27 19:41:45 +00001423 if (skb2 != skb)
1424 kfree_skb(skb2);
1425 return err;
1426}
1427
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001428#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1429static int br_multicast_ipv6_rcv(struct net_bridge *br,
1430 struct net_bridge_port *port,
1431 struct sk_buff *skb)
1432{
1433 struct sk_buff *skb2 = skb;
1434 struct ipv6hdr *ip6h;
1435 struct icmp6hdr *icmp6h;
1436 u8 nexthdr;
1437 unsigned len;
Kulikov Vasiliybb7a0bd2010-07-15 08:47:33 +00001438 int offset;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001439 int err;
1440
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001441 if (!pskb_may_pull(skb, sizeof(*ip6h)))
1442 return -EINVAL;
1443
1444 ip6h = ipv6_hdr(skb);
1445
1446 /*
1447 * We're interested in MLD messages only.
1448 * - Version is 6
1449 * - MLD has always Router Alert hop-by-hop option
1450 * - But we do not support jumbrograms.
1451 */
1452 if (ip6h->version != 6 ||
1453 ip6h->nexthdr != IPPROTO_HOPOPTS ||
1454 ip6h->payload_len == 0)
1455 return 0;
1456
1457 len = ntohs(ip6h->payload_len);
1458 if (skb->len < len)
1459 return -EINVAL;
1460
1461 nexthdr = ip6h->nexthdr;
1462 offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr);
1463
1464 if (offset < 0 || nexthdr != IPPROTO_ICMPV6)
1465 return 0;
1466
1467 /* Okay, we found ICMPv6 header */
1468 skb2 = skb_clone(skb, GFP_ATOMIC);
1469 if (!skb2)
1470 return -ENOMEM;
1471
1472 len -= offset - skb_network_offset(skb2);
1473
1474 __skb_pull(skb2, offset);
1475 skb_reset_transport_header(skb2);
1476
1477 err = -EINVAL;
1478 if (!pskb_may_pull(skb2, sizeof(*icmp6h)))
1479 goto out;
1480
1481 icmp6h = icmp6_hdr(skb2);
1482
1483 switch (icmp6h->icmp6_type) {
1484 case ICMPV6_MGM_QUERY:
1485 case ICMPV6_MGM_REPORT:
1486 case ICMPV6_MGM_REDUCTION:
1487 case ICMPV6_MLD2_REPORT:
1488 break;
1489 default:
1490 err = 0;
1491 goto out;
1492 }
1493
1494 /* Okay, we found MLD message. Check further. */
1495 if (skb2->len > len) {
1496 err = pskb_trim_rcsum(skb2, len);
1497 if (err)
1498 goto out;
1499 }
1500
1501 switch (skb2->ip_summed) {
1502 case CHECKSUM_COMPLETE:
1503 if (!csum_fold(skb2->csum))
1504 break;
1505 /*FALLTHROUGH*/
1506 case CHECKSUM_NONE:
1507 skb2->csum = 0;
1508 if (skb_checksum_complete(skb2))
1509 goto out;
1510 }
1511
1512 err = 0;
1513
1514 BR_INPUT_SKB_CB(skb)->igmp = 1;
1515
1516 switch (icmp6h->icmp6_type) {
1517 case ICMPV6_MGM_REPORT:
1518 {
1519 struct mld_msg *mld = (struct mld_msg *)icmp6h;
1520 BR_INPUT_SKB_CB(skb2)->mrouters_only = 1;
1521 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca);
1522 break;
1523 }
1524 case ICMPV6_MLD2_REPORT:
1525 err = br_ip6_multicast_mld2_report(br, port, skb2);
1526 break;
1527 case ICMPV6_MGM_QUERY:
1528 err = br_ip6_multicast_query(br, port, skb2);
1529 break;
1530 case ICMPV6_MGM_REDUCTION:
1531 {
1532 struct mld_msg *mld = (struct mld_msg *)icmp6h;
1533 br_ip6_multicast_leave_group(br, port, &mld->mld_mca);
1534 }
1535 }
1536
1537out:
1538 __skb_push(skb2, offset);
1539 if (skb2 != skb)
1540 kfree_skb(skb2);
1541 return err;
1542}
1543#endif
1544
Herbert Xueb1d1642010-02-27 19:41:45 +00001545int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1546 struct sk_buff *skb)
1547{
YOSHIFUJI Hideaki / 吉藤英明1fafc7a2010-04-25 08:06:40 +00001548 BR_INPUT_SKB_CB(skb)->igmp = 0;
1549 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1550
Herbert Xueb1d1642010-02-27 19:41:45 +00001551 if (br->multicast_disabled)
1552 return 0;
1553
1554 switch (skb->protocol) {
1555 case htons(ETH_P_IP):
1556 return br_multicast_ipv4_rcv(br, port, skb);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001557#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1558 case htons(ETH_P_IPV6):
1559 return br_multicast_ipv6_rcv(br, port, skb);
1560#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001561 }
1562
1563 return 0;
1564}
1565
1566static void br_multicast_query_expired(unsigned long data)
1567{
1568 struct net_bridge *br = (void *)data;
1569
1570 spin_lock(&br->multicast_lock);
1571 if (br->multicast_startup_queries_sent <
1572 br->multicast_startup_query_count)
1573 br->multicast_startup_queries_sent++;
1574
1575 br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent);
1576
1577 spin_unlock(&br->multicast_lock);
1578}
1579
1580void br_multicast_init(struct net_bridge *br)
1581{
1582 br->hash_elasticity = 4;
1583 br->hash_max = 512;
1584
1585 br->multicast_router = 1;
1586 br->multicast_last_member_count = 2;
1587 br->multicast_startup_query_count = 2;
1588
1589 br->multicast_last_member_interval = HZ;
1590 br->multicast_query_response_interval = 10 * HZ;
1591 br->multicast_startup_query_interval = 125 * HZ / 4;
1592 br->multicast_query_interval = 125 * HZ;
1593 br->multicast_querier_interval = 255 * HZ;
1594 br->multicast_membership_interval = 260 * HZ;
1595
1596 spin_lock_init(&br->multicast_lock);
1597 setup_timer(&br->multicast_router_timer,
1598 br_multicast_local_router_expired, 0);
1599 setup_timer(&br->multicast_querier_timer,
1600 br_multicast_local_router_expired, 0);
1601 setup_timer(&br->multicast_query_timer, br_multicast_query_expired,
1602 (unsigned long)br);
1603}
1604
1605void br_multicast_open(struct net_bridge *br)
1606{
1607 br->multicast_startup_queries_sent = 0;
1608
1609 if (br->multicast_disabled)
1610 return;
1611
1612 mod_timer(&br->multicast_query_timer, jiffies);
1613}
1614
1615void br_multicast_stop(struct net_bridge *br)
1616{
1617 struct net_bridge_mdb_htable *mdb;
1618 struct net_bridge_mdb_entry *mp;
1619 struct hlist_node *p, *n;
1620 u32 ver;
1621 int i;
1622
1623 del_timer_sync(&br->multicast_router_timer);
1624 del_timer_sync(&br->multicast_querier_timer);
1625 del_timer_sync(&br->multicast_query_timer);
1626
1627 spin_lock_bh(&br->multicast_lock);
1628 mdb = br->mdb;
1629 if (!mdb)
1630 goto out;
1631
1632 br->mdb = NULL;
1633
1634 ver = mdb->ver;
1635 for (i = 0; i < mdb->max; i++) {
1636 hlist_for_each_entry_safe(mp, p, n, &mdb->mhash[i],
1637 hlist[ver]) {
1638 del_timer(&mp->timer);
1639 del_timer(&mp->query_timer);
1640 call_rcu_bh(&mp->rcu, br_multicast_free_group);
1641 }
1642 }
1643
1644 if (mdb->old) {
1645 spin_unlock_bh(&br->multicast_lock);
Herbert Xu10cc2b52010-03-05 21:03:35 +00001646 rcu_barrier_bh();
Herbert Xueb1d1642010-02-27 19:41:45 +00001647 spin_lock_bh(&br->multicast_lock);
1648 WARN_ON(mdb->old);
1649 }
1650
1651 mdb->old = mdb;
1652 call_rcu_bh(&mdb->rcu, br_mdb_free);
1653
1654out:
1655 spin_unlock_bh(&br->multicast_lock);
1656}
Herbert Xu0909e112010-02-27 19:41:49 +00001657
1658int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1659{
1660 int err = -ENOENT;
1661
1662 spin_lock_bh(&br->multicast_lock);
1663 if (!netif_running(br->dev))
1664 goto unlock;
1665
1666 switch (val) {
1667 case 0:
1668 case 2:
1669 del_timer(&br->multicast_router_timer);
1670 /* fall through */
1671 case 1:
1672 br->multicast_router = val;
1673 err = 0;
1674 break;
1675
1676 default:
1677 err = -EINVAL;
1678 break;
1679 }
1680
1681unlock:
1682 spin_unlock_bh(&br->multicast_lock);
1683
1684 return err;
1685}
1686
1687int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1688{
1689 struct net_bridge *br = p->br;
1690 int err = -ENOENT;
1691
1692 spin_lock(&br->multicast_lock);
1693 if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED)
1694 goto unlock;
1695
1696 switch (val) {
1697 case 0:
1698 case 1:
1699 case 2:
1700 p->multicast_router = val;
1701 err = 0;
1702
1703 if (val < 2 && !hlist_unhashed(&p->rlist))
1704 hlist_del_init_rcu(&p->rlist);
1705
1706 if (val == 1)
1707 break;
1708
1709 del_timer(&p->multicast_router_timer);
1710
1711 if (val == 0)
1712 break;
1713
1714 br_multicast_add_router(br, p);
1715 break;
1716
1717 default:
1718 err = -EINVAL;
1719 break;
1720 }
1721
1722unlock:
1723 spin_unlock(&br->multicast_lock);
1724
1725 return err;
1726}
Herbert Xu561f1102010-02-27 19:41:50 +00001727
1728int br_multicast_toggle(struct net_bridge *br, unsigned long val)
1729{
1730 struct net_bridge_port *port;
Herbert Xu3a7fda02010-07-29 00:45:30 +00001731 int err = 0;
Herbert Xu561f1102010-02-27 19:41:50 +00001732
1733 spin_lock(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00001734 if (br->multicast_disabled == !val)
1735 goto unlock;
1736
1737 br->multicast_disabled = !val;
1738 if (br->multicast_disabled)
1739 goto unlock;
1740
Herbert Xu3a7fda02010-07-29 00:45:30 +00001741 if (!netif_running(br->dev))
1742 goto unlock;
1743
Herbert Xu561f1102010-02-27 19:41:50 +00001744 if (br->mdb) {
1745 if (br->mdb->old) {
1746 err = -EEXIST;
1747rollback:
1748 br->multicast_disabled = !!val;
1749 goto unlock;
1750 }
1751
1752 err = br_mdb_rehash(&br->mdb, br->mdb->max,
1753 br->hash_elasticity);
1754 if (err)
1755 goto rollback;
1756 }
1757
1758 br_multicast_open(br);
1759 list_for_each_entry(port, &br->port_list, list) {
1760 if (port->state == BR_STATE_DISABLED ||
1761 port->state == BR_STATE_BLOCKING)
1762 continue;
1763
1764 __br_multicast_enable_port(port);
1765 }
1766
1767unlock:
1768 spin_unlock(&br->multicast_lock);
1769
1770 return err;
1771}
Herbert Xub1951672010-02-27 19:41:51 +00001772
1773int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
1774{
1775 int err = -ENOENT;
1776 u32 old;
1777
1778 spin_lock(&br->multicast_lock);
1779 if (!netif_running(br->dev))
1780 goto unlock;
1781
1782 err = -EINVAL;
1783 if (!is_power_of_2(val))
1784 goto unlock;
1785 if (br->mdb && val < br->mdb->size)
1786 goto unlock;
1787
1788 err = 0;
1789
1790 old = br->hash_max;
1791 br->hash_max = val;
1792
1793 if (br->mdb) {
1794 if (br->mdb->old) {
1795 err = -EEXIST;
1796rollback:
1797 br->hash_max = old;
1798 goto unlock;
1799 }
1800
1801 err = br_mdb_rehash(&br->mdb, br->hash_max,
1802 br->hash_elasticity);
1803 if (err)
1804 goto rollback;
1805 }
1806
1807unlock:
1808 spin_unlock(&br->multicast_lock);
1809
1810 return err;
1811}