blob: 17708fccf1ee91f2c9d563e2a19f2eadd79d0c9a [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
Eric Dumazete8051682010-11-15 06:38:10 +000036#define mlock_dereference(X, br) \
37 rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock))
38
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090039#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
40static inline int ipv6_is_local_multicast(const struct in6_addr *addr)
Herbert Xueb1d1642010-02-27 19:41:45 +000041{
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090042 if (ipv6_addr_is_multicast(addr) &&
43 IPV6_ADDR_MC_SCOPE(addr) <= IPV6_ADDR_SCOPE_LINKLOCAL)
44 return 1;
45 return 0;
46}
47#endif
48
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090049static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
50{
51 if (a->proto != b->proto)
52 return 0;
53 switch (a->proto) {
54 case htons(ETH_P_IP):
55 return a->u.ip4 == b->u.ip4;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090056#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
57 case htons(ETH_P_IPV6):
58 return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
59#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090060 }
61 return 0;
62}
63
64static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip)
Herbert Xueb1d1642010-02-27 19:41:45 +000065{
Eric Dumazet0eae88f2010-04-20 19:06:52 -070066 return jhash_1word(mdb->secret, (__force u32)ip) & (mdb->max - 1);
Herbert Xueb1d1642010-02-27 19:41:45 +000067}
68
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090069#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
70static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb,
71 const struct in6_addr *ip)
72{
73 return jhash2((__force u32 *)ip->s6_addr32, 4, mdb->secret) & (mdb->max - 1);
74}
75#endif
76
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090077static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb,
78 struct br_ip *ip)
79{
80 switch (ip->proto) {
81 case htons(ETH_P_IP):
82 return __br_ip4_hash(mdb, ip->u.ip4);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090083#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
84 case htons(ETH_P_IPV6):
85 return __br_ip6_hash(mdb, &ip->u.ip6);
86#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090087 }
88 return 0;
Herbert Xueb1d1642010-02-27 19:41:45 +000089}
90
91static struct net_bridge_mdb_entry *__br_mdb_ip_get(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090092 struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +000093{
94 struct net_bridge_mdb_entry *mp;
95 struct hlist_node *p;
96
Herbert Xu49f5fcf2010-03-05 21:07:39 +000097 hlist_for_each_entry_rcu(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) {
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090098 if (br_ip_equal(&mp->addr, dst))
Herbert Xueb1d1642010-02-27 19:41:45 +000099 return mp;
100 }
101
102 return NULL;
103}
104
Herbert Xu7f285fa2010-07-05 14:50:08 +0000105static struct net_bridge_mdb_entry *br_mdb_ip_get(
106 struct net_bridge_mdb_htable *mdb, struct br_ip *dst)
107{
108 if (!mdb)
109 return NULL;
110
111 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
112}
113
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900114static struct net_bridge_mdb_entry *br_mdb_ip4_get(
Herbert Xueb1d1642010-02-27 19:41:45 +0000115 struct net_bridge_mdb_htable *mdb, __be32 dst)
116{
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900117 struct br_ip br_dst;
Herbert Xu0821ec52010-03-15 20:38:25 -0700118
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900119 br_dst.u.ip4 = dst;
120 br_dst.proto = htons(ETH_P_IP);
121
Herbert Xu7f285fa2010-07-05 14:50:08 +0000122 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900123}
124
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900125#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
126static struct net_bridge_mdb_entry *br_mdb_ip6_get(
127 struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst)
128{
129 struct br_ip br_dst;
130
131 ipv6_addr_copy(&br_dst.u.ip6, dst);
132 br_dst.proto = htons(ETH_P_IPV6);
133
Herbert Xu7f285fa2010-07-05 14:50:08 +0000134 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900135}
136#endif
137
Herbert Xueb1d1642010-02-27 19:41:45 +0000138struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
139 struct sk_buff *skb)
140{
Eric Dumazete8051682010-11-15 06:38:10 +0000141 struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900142 struct br_ip ip;
143
Herbert Xu7f285fa2010-07-05 14:50:08 +0000144 if (br->multicast_disabled)
Herbert Xueb1d1642010-02-27 19:41:45 +0000145 return NULL;
146
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900147 if (BR_INPUT_SKB_CB(skb)->igmp)
148 return NULL;
149
150 ip.proto = skb->protocol;
151
Herbert Xueb1d1642010-02-27 19:41:45 +0000152 switch (skb->protocol) {
153 case htons(ETH_P_IP):
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900154 ip.u.ip4 = ip_hdr(skb)->daddr;
155 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900156#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
157 case htons(ETH_P_IPV6):
158 ipv6_addr_copy(&ip.u.ip6, &ipv6_hdr(skb)->daddr);
159 break;
160#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900161 default:
162 return NULL;
Herbert Xueb1d1642010-02-27 19:41:45 +0000163 }
164
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900165 return br_mdb_ip_get(mdb, &ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000166}
167
168static void br_mdb_free(struct rcu_head *head)
169{
170 struct net_bridge_mdb_htable *mdb =
171 container_of(head, struct net_bridge_mdb_htable, rcu);
172 struct net_bridge_mdb_htable *old = mdb->old;
173
174 mdb->old = NULL;
175 kfree(old->mhash);
176 kfree(old);
177}
178
179static int br_mdb_copy(struct net_bridge_mdb_htable *new,
180 struct net_bridge_mdb_htable *old,
181 int elasticity)
182{
183 struct net_bridge_mdb_entry *mp;
184 struct hlist_node *p;
185 int maxlen;
186 int len;
187 int i;
188
189 for (i = 0; i < old->max; i++)
190 hlist_for_each_entry(mp, p, &old->mhash[i], hlist[old->ver])
191 hlist_add_head(&mp->hlist[new->ver],
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900192 &new->mhash[br_ip_hash(new, &mp->addr)]);
Herbert Xueb1d1642010-02-27 19:41:45 +0000193
194 if (!elasticity)
195 return 0;
196
197 maxlen = 0;
198 for (i = 0; i < new->max; i++) {
199 len = 0;
200 hlist_for_each_entry(mp, p, &new->mhash[i], hlist[new->ver])
201 len++;
202 if (len > maxlen)
203 maxlen = len;
204 }
205
206 return maxlen > elasticity ? -EINVAL : 0;
207}
208
209static void br_multicast_free_pg(struct rcu_head *head)
210{
211 struct net_bridge_port_group *p =
212 container_of(head, struct net_bridge_port_group, rcu);
213
214 kfree(p);
215}
216
217static void br_multicast_free_group(struct rcu_head *head)
218{
219 struct net_bridge_mdb_entry *mp =
220 container_of(head, struct net_bridge_mdb_entry, rcu);
221
222 kfree(mp);
223}
224
225static void br_multicast_group_expired(unsigned long data)
226{
227 struct net_bridge_mdb_entry *mp = (void *)data;
228 struct net_bridge *br = mp->br;
229 struct net_bridge_mdb_htable *mdb;
230
231 spin_lock(&br->multicast_lock);
232 if (!netif_running(br->dev) || timer_pending(&mp->timer))
233 goto out;
234
Herbert Xu8a870172011-02-12 01:05:42 -0800235 mp->mglist = false;
Herbert Xueb1d1642010-02-27 19:41:45 +0000236
237 if (mp->ports)
238 goto out;
239
Eric Dumazete8051682010-11-15 06:38:10 +0000240 mdb = mlock_dereference(br->mdb, br);
241
Herbert Xueb1d1642010-02-27 19:41:45 +0000242 hlist_del_rcu(&mp->hlist[mdb->ver]);
243 mdb->size--;
244
245 del_timer(&mp->query_timer);
246 call_rcu_bh(&mp->rcu, br_multicast_free_group);
247
248out:
249 spin_unlock(&br->multicast_lock);
250}
251
252static void br_multicast_del_pg(struct net_bridge *br,
253 struct net_bridge_port_group *pg)
254{
Eric Dumazete8051682010-11-15 06:38:10 +0000255 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000256 struct net_bridge_mdb_entry *mp;
257 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +0000258 struct net_bridge_port_group __rcu **pp;
259
260 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +0000261
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900262 mp = br_mdb_ip_get(mdb, &pg->addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000263 if (WARN_ON(!mp))
264 return;
265
Eric Dumazete8051682010-11-15 06:38:10 +0000266 for (pp = &mp->ports;
267 (p = mlock_dereference(*pp, br)) != NULL;
268 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000269 if (p != pg)
270 continue;
271
stephen hemminger83f6a742010-04-27 15:01:06 +0000272 rcu_assign_pointer(*pp, p->next);
Herbert Xueb1d1642010-02-27 19:41:45 +0000273 hlist_del_init(&p->mglist);
274 del_timer(&p->timer);
275 del_timer(&p->query_timer);
276 call_rcu_bh(&p->rcu, br_multicast_free_pg);
277
Herbert Xu8a870172011-02-12 01:05:42 -0800278 if (!mp->ports && !mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +0000279 netif_running(br->dev))
280 mod_timer(&mp->timer, jiffies);
281
282 return;
283 }
284
285 WARN_ON(1);
286}
287
288static void br_multicast_port_group_expired(unsigned long data)
289{
290 struct net_bridge_port_group *pg = (void *)data;
291 struct net_bridge *br = pg->port->br;
292
293 spin_lock(&br->multicast_lock);
294 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
295 hlist_unhashed(&pg->mglist))
296 goto out;
297
298 br_multicast_del_pg(br, pg);
299
300out:
301 spin_unlock(&br->multicast_lock);
302}
303
Eric Dumazete8051682010-11-15 06:38:10 +0000304static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max,
Herbert Xueb1d1642010-02-27 19:41:45 +0000305 int elasticity)
306{
Eric Dumazete8051682010-11-15 06:38:10 +0000307 struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000308 struct net_bridge_mdb_htable *mdb;
309 int err;
310
311 mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
312 if (!mdb)
313 return -ENOMEM;
314
315 mdb->max = max;
316 mdb->old = old;
317
318 mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
319 if (!mdb->mhash) {
320 kfree(mdb);
321 return -ENOMEM;
322 }
323
324 mdb->size = old ? old->size : 0;
325 mdb->ver = old ? old->ver ^ 1 : 0;
326
327 if (!old || elasticity)
328 get_random_bytes(&mdb->secret, sizeof(mdb->secret));
329 else
330 mdb->secret = old->secret;
331
332 if (!old)
333 goto out;
334
335 err = br_mdb_copy(mdb, old, elasticity);
336 if (err) {
337 kfree(mdb->mhash);
338 kfree(mdb);
339 return err;
340 }
341
342 call_rcu_bh(&mdb->rcu, br_mdb_free);
343
344out:
345 rcu_assign_pointer(*mdbp, mdb);
346
347 return 0;
348}
349
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900350static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
351 __be32 group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000352{
353 struct sk_buff *skb;
354 struct igmphdr *ih;
355 struct ethhdr *eth;
356 struct iphdr *iph;
357
358 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
359 sizeof(*ih) + 4);
360 if (!skb)
361 goto out;
362
363 skb->protocol = htons(ETH_P_IP);
364
365 skb_reset_mac_header(skb);
366 eth = eth_hdr(skb);
367
368 memcpy(eth->h_source, br->dev->dev_addr, 6);
369 eth->h_dest[0] = 1;
370 eth->h_dest[1] = 0;
371 eth->h_dest[2] = 0x5e;
372 eth->h_dest[3] = 0;
373 eth->h_dest[4] = 0;
374 eth->h_dest[5] = 1;
375 eth->h_proto = htons(ETH_P_IP);
376 skb_put(skb, sizeof(*eth));
377
378 skb_set_network_header(skb, skb->len);
379 iph = ip_hdr(skb);
380
381 iph->version = 4;
382 iph->ihl = 6;
383 iph->tos = 0xc0;
384 iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4);
385 iph->id = 0;
386 iph->frag_off = htons(IP_DF);
387 iph->ttl = 1;
388 iph->protocol = IPPROTO_IGMP;
389 iph->saddr = 0;
390 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
391 ((u8 *)&iph[1])[0] = IPOPT_RA;
392 ((u8 *)&iph[1])[1] = 4;
393 ((u8 *)&iph[1])[2] = 0;
394 ((u8 *)&iph[1])[3] = 0;
395 ip_send_check(iph);
396 skb_put(skb, 24);
397
398 skb_set_transport_header(skb, skb->len);
399 ih = igmp_hdr(skb);
400 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
401 ih->code = (group ? br->multicast_last_member_interval :
402 br->multicast_query_response_interval) /
403 (HZ / IGMP_TIMER_SCALE);
404 ih->group = group;
405 ih->csum = 0;
406 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
407 skb_put(skb, sizeof(*ih));
408
409 __skb_pull(skb, sizeof(*eth));
410
411out:
412 return skb;
413}
414
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900415#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
416static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
417 struct in6_addr *group)
418{
419 struct sk_buff *skb;
420 struct ipv6hdr *ip6h;
421 struct mld_msg *mldq;
422 struct ethhdr *eth;
423 u8 *hopopt;
424 unsigned long interval;
425
426 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
427 8 + sizeof(*mldq));
428 if (!skb)
429 goto out;
430
431 skb->protocol = htons(ETH_P_IPV6);
432
433 /* Ethernet header */
434 skb_reset_mac_header(skb);
435 eth = eth_hdr(skb);
436
437 memcpy(eth->h_source, br->dev->dev_addr, 6);
438 ipv6_eth_mc_map(group, eth->h_dest);
439 eth->h_proto = htons(ETH_P_IPV6);
440 skb_put(skb, sizeof(*eth));
441
442 /* IPv6 header + HbH option */
443 skb_set_network_header(skb, skb->len);
444 ip6h = ipv6_hdr(skb);
445
446 *(__force __be32 *)ip6h = htonl(0x60000000);
David Stevens76d66152010-12-14 08:42:16 +0000447 ip6h->payload_len = htons(8 + sizeof(*mldq));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900448 ip6h->nexthdr = IPPROTO_HOPOPTS;
449 ip6h->hop_limit = 1;
450 ipv6_addr_set(&ip6h->saddr, 0, 0, 0, 0);
451 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
452
453 hopopt = (u8 *)(ip6h + 1);
454 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
455 hopopt[1] = 0; /* length of HbH */
456 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
457 hopopt[3] = 2; /* Length of RA Option */
458 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
459 hopopt[5] = 0;
460 hopopt[6] = IPV6_TLV_PAD0; /* Pad0 */
461 hopopt[7] = IPV6_TLV_PAD0; /* Pad0 */
462
463 skb_put(skb, sizeof(*ip6h) + 8);
464
465 /* ICMPv6 */
466 skb_set_transport_header(skb, skb->len);
467 mldq = (struct mld_msg *) icmp6_hdr(skb);
468
469 interval = ipv6_addr_any(group) ? br->multicast_last_member_interval :
470 br->multicast_query_response_interval;
471
472 mldq->mld_type = ICMPV6_MGM_QUERY;
473 mldq->mld_code = 0;
474 mldq->mld_cksum = 0;
475 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
476 mldq->mld_reserved = 0;
477 ipv6_addr_copy(&mldq->mld_mca, group);
478
479 /* checksum */
480 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
481 sizeof(*mldq), IPPROTO_ICMPV6,
482 csum_partial(mldq,
483 sizeof(*mldq), 0));
484 skb_put(skb, sizeof(*mldq));
485
486 __skb_pull(skb, sizeof(*eth));
487
488out:
489 return skb;
490}
491#endif
492
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900493static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
494 struct br_ip *addr)
495{
496 switch (addr->proto) {
497 case htons(ETH_P_IP):
498 return br_ip4_multicast_alloc_query(br, addr->u.ip4);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900499#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
500 case htons(ETH_P_IPV6):
501 return br_ip6_multicast_alloc_query(br, &addr->u.ip6);
502#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900503 }
504 return NULL;
505}
506
Herbert Xueb1d1642010-02-27 19:41:45 +0000507static void br_multicast_send_group_query(struct net_bridge_mdb_entry *mp)
508{
509 struct net_bridge *br = mp->br;
510 struct sk_buff *skb;
511
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900512 skb = br_multicast_alloc_query(br, &mp->addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000513 if (!skb)
514 goto timer;
515
516 netif_rx(skb);
517
518timer:
519 if (++mp->queries_sent < br->multicast_last_member_count)
520 mod_timer(&mp->query_timer,
521 jiffies + br->multicast_last_member_interval);
522}
523
524static void br_multicast_group_query_expired(unsigned long data)
525{
526 struct net_bridge_mdb_entry *mp = (void *)data;
527 struct net_bridge *br = mp->br;
528
529 spin_lock(&br->multicast_lock);
Herbert Xu8a870172011-02-12 01:05:42 -0800530 if (!netif_running(br->dev) || !mp->mglist ||
Herbert Xueb1d1642010-02-27 19:41:45 +0000531 mp->queries_sent >= br->multicast_last_member_count)
532 goto out;
533
534 br_multicast_send_group_query(mp);
535
536out:
537 spin_unlock(&br->multicast_lock);
538}
539
540static void br_multicast_send_port_group_query(struct net_bridge_port_group *pg)
541{
542 struct net_bridge_port *port = pg->port;
543 struct net_bridge *br = port->br;
544 struct sk_buff *skb;
545
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900546 skb = br_multicast_alloc_query(br, &pg->addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000547 if (!skb)
548 goto timer;
549
550 br_deliver(port, skb);
551
552timer:
553 if (++pg->queries_sent < br->multicast_last_member_count)
554 mod_timer(&pg->query_timer,
555 jiffies + br->multicast_last_member_interval);
556}
557
558static void br_multicast_port_group_query_expired(unsigned long data)
559{
560 struct net_bridge_port_group *pg = (void *)data;
561 struct net_bridge_port *port = pg->port;
562 struct net_bridge *br = port->br;
563
564 spin_lock(&br->multicast_lock);
565 if (!netif_running(br->dev) || hlist_unhashed(&pg->mglist) ||
566 pg->queries_sent >= br->multicast_last_member_count)
567 goto out;
568
569 br_multicast_send_port_group_query(pg);
570
571out:
572 spin_unlock(&br->multicast_lock);
573}
574
575static struct net_bridge_mdb_entry *br_multicast_get_group(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900576 struct net_bridge *br, struct net_bridge_port *port,
577 struct br_ip *group, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +0000578{
Eric Dumazete8051682010-11-15 06:38:10 +0000579 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000580 struct net_bridge_mdb_entry *mp;
581 struct hlist_node *p;
582 unsigned count = 0;
583 unsigned max;
584 int elasticity;
585 int err;
586
Eric Dumazete8051682010-11-15 06:38:10 +0000587 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000588 hlist_for_each_entry(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) {
589 count++;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900590 if (unlikely(br_ip_equal(group, &mp->addr)))
Herbert Xueb1d1642010-02-27 19:41:45 +0000591 return mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000592 }
593
594 elasticity = 0;
595 max = mdb->max;
596
597 if (unlikely(count > br->hash_elasticity && count)) {
598 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000599 br_info(br, "Multicast hash table "
600 "chain limit reached: %s\n",
601 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000602
603 elasticity = br->hash_elasticity;
604 }
605
606 if (mdb->size >= max) {
607 max *= 2;
608 if (unlikely(max >= br->hash_max)) {
stephen hemminger28a16c92010-05-10 09:31:09 +0000609 br_warn(br, "Multicast hash table maximum "
610 "reached, disabling snooping: %s, %d\n",
611 port ? port->dev->name : br->dev->name, max);
Herbert Xueb1d1642010-02-27 19:41:45 +0000612 err = -E2BIG;
613disable:
614 br->multicast_disabled = 1;
615 goto err;
616 }
617 }
618
619 if (max > mdb->max || elasticity) {
620 if (mdb->old) {
621 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000622 br_info(br, "Multicast hash table "
623 "on fire: %s\n",
624 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000625 err = -EEXIST;
626 goto err;
627 }
628
629 err = br_mdb_rehash(&br->mdb, max, elasticity);
630 if (err) {
stephen hemminger28a16c92010-05-10 09:31:09 +0000631 br_warn(br, "Cannot rehash multicast "
632 "hash table, disabling snooping: %s, %d, %d\n",
633 port ? port->dev->name : br->dev->name,
634 mdb->size, err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000635 goto disable;
636 }
637
638 err = -EAGAIN;
639 goto err;
640 }
641
642 return NULL;
643
644err:
645 mp = ERR_PTR(err);
646 return mp;
647}
648
649static struct net_bridge_mdb_entry *br_multicast_new_group(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900650 struct net_bridge *br, struct net_bridge_port *port,
651 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000652{
Eric Dumazete8051682010-11-15 06:38:10 +0000653 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000654 struct net_bridge_mdb_entry *mp;
655 int hash;
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000656 int err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000657
Eric Dumazete8051682010-11-15 06:38:10 +0000658 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000659 if (!mdb) {
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000660 err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0);
661 if (err)
662 return ERR_PTR(err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000663 goto rehash;
664 }
665
666 hash = br_ip_hash(mdb, group);
667 mp = br_multicast_get_group(br, port, group, hash);
668 switch (PTR_ERR(mp)) {
669 case 0:
670 break;
671
672 case -EAGAIN:
673rehash:
Eric Dumazete8051682010-11-15 06:38:10 +0000674 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000675 hash = br_ip_hash(mdb, group);
676 break;
677
678 default:
679 goto out;
680 }
681
682 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
683 if (unlikely(!mp))
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000684 return ERR_PTR(-ENOMEM);
Herbert Xueb1d1642010-02-27 19:41:45 +0000685
686 mp->br = br;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900687 mp->addr = *group;
Herbert Xueb1d1642010-02-27 19:41:45 +0000688 setup_timer(&mp->timer, br_multicast_group_expired,
689 (unsigned long)mp);
690 setup_timer(&mp->query_timer, br_multicast_group_query_expired,
691 (unsigned long)mp);
692
693 hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
694 mdb->size++;
695
696out:
697 return mp;
698}
699
700static int br_multicast_add_group(struct net_bridge *br,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900701 struct net_bridge_port *port,
702 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000703{
704 struct net_bridge_mdb_entry *mp;
705 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +0000706 struct net_bridge_port_group __rcu **pp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000707 unsigned long now = jiffies;
708 int err;
709
Herbert Xueb1d1642010-02-27 19:41:45 +0000710 spin_lock(&br->multicast_lock);
711 if (!netif_running(br->dev) ||
712 (port && port->state == BR_STATE_DISABLED))
713 goto out;
714
715 mp = br_multicast_new_group(br, port, group);
716 err = PTR_ERR(mp);
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000717 if (IS_ERR(mp))
Herbert Xueb1d1642010-02-27 19:41:45 +0000718 goto err;
719
720 if (!port) {
Herbert Xu8a870172011-02-12 01:05:42 -0800721 mp->mglist = true;
Herbert Xueb1d1642010-02-27 19:41:45 +0000722 mod_timer(&mp->timer, now + br->multicast_membership_interval);
723 goto out;
724 }
725
Eric Dumazete8051682010-11-15 06:38:10 +0000726 for (pp = &mp->ports;
727 (p = mlock_dereference(*pp, br)) != NULL;
728 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000729 if (p->port == port)
730 goto found;
731 if ((unsigned long)p->port < (unsigned long)port)
732 break;
733 }
734
735 p = kzalloc(sizeof(*p), GFP_ATOMIC);
736 err = -ENOMEM;
737 if (unlikely(!p))
738 goto err;
739
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900740 p->addr = *group;
Herbert Xueb1d1642010-02-27 19:41:45 +0000741 p->port = port;
742 p->next = *pp;
743 hlist_add_head(&p->mglist, &port->mglist);
744 setup_timer(&p->timer, br_multicast_port_group_expired,
745 (unsigned long)p);
746 setup_timer(&p->query_timer, br_multicast_port_group_query_expired,
747 (unsigned long)p);
748
749 rcu_assign_pointer(*pp, p);
750
751found:
752 mod_timer(&p->timer, now + br->multicast_membership_interval);
753out:
754 err = 0;
755
756err:
757 spin_unlock(&br->multicast_lock);
758 return err;
759}
760
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900761static int br_ip4_multicast_add_group(struct net_bridge *br,
762 struct net_bridge_port *port,
763 __be32 group)
764{
765 struct br_ip br_group;
766
767 if (ipv4_is_local_multicast(group))
768 return 0;
769
770 br_group.u.ip4 = group;
771 br_group.proto = htons(ETH_P_IP);
772
773 return br_multicast_add_group(br, port, &br_group);
774}
775
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900776#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
777static int br_ip6_multicast_add_group(struct net_bridge *br,
778 struct net_bridge_port *port,
779 const struct in6_addr *group)
780{
781 struct br_ip br_group;
782
783 if (ipv6_is_local_multicast(group))
784 return 0;
785
786 ipv6_addr_copy(&br_group.u.ip6, group);
Linus Lüssing9cc6e0c2011-02-15 13:19:17 +0000787 br_group.proto = htons(ETH_P_IPV6);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900788
789 return br_multicast_add_group(br, port, &br_group);
790}
791#endif
792
Herbert Xueb1d1642010-02-27 19:41:45 +0000793static void br_multicast_router_expired(unsigned long data)
794{
795 struct net_bridge_port *port = (void *)data;
796 struct net_bridge *br = port->br;
797
798 spin_lock(&br->multicast_lock);
799 if (port->multicast_router != 1 ||
800 timer_pending(&port->multicast_router_timer) ||
801 hlist_unhashed(&port->rlist))
802 goto out;
803
804 hlist_del_init_rcu(&port->rlist);
805
806out:
807 spin_unlock(&br->multicast_lock);
808}
809
810static void br_multicast_local_router_expired(unsigned long data)
811{
812}
813
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900814static void __br_multicast_send_query(struct net_bridge *br,
815 struct net_bridge_port *port,
816 struct br_ip *ip)
Herbert Xueb1d1642010-02-27 19:41:45 +0000817{
Herbert Xueb1d1642010-02-27 19:41:45 +0000818 struct sk_buff *skb;
819
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900820 skb = br_multicast_alloc_query(br, ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000821 if (!skb)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900822 return;
Herbert Xueb1d1642010-02-27 19:41:45 +0000823
824 if (port) {
825 __skb_push(skb, sizeof(struct ethhdr));
826 skb->dev = port->dev;
Jan Engelhardt713aefa2010-03-23 04:07:21 +0100827 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
Herbert Xueb1d1642010-02-27 19:41:45 +0000828 dev_queue_xmit);
829 } else
830 netif_rx(skb);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900831}
Herbert Xueb1d1642010-02-27 19:41:45 +0000832
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900833static void br_multicast_send_query(struct net_bridge *br,
834 struct net_bridge_port *port, u32 sent)
835{
836 unsigned long time;
837 struct br_ip br_group;
838
839 if (!netif_running(br->dev) || br->multicast_disabled ||
840 timer_pending(&br->multicast_querier_timer))
841 return;
842
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900843 memset(&br_group.u, 0, sizeof(br_group.u));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900844
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900845 br_group.proto = htons(ETH_P_IP);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900846 __br_multicast_send_query(br, port, &br_group);
847
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900848#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
849 br_group.proto = htons(ETH_P_IPV6);
850 __br_multicast_send_query(br, port, &br_group);
851#endif
852
Herbert Xueb1d1642010-02-27 19:41:45 +0000853 time = jiffies;
854 time += sent < br->multicast_startup_query_count ?
855 br->multicast_startup_query_interval :
856 br->multicast_query_interval;
857 mod_timer(port ? &port->multicast_query_timer :
858 &br->multicast_query_timer, time);
859}
860
861static void br_multicast_port_query_expired(unsigned long data)
862{
863 struct net_bridge_port *port = (void *)data;
864 struct net_bridge *br = port->br;
865
866 spin_lock(&br->multicast_lock);
Dan Carpenter02a780c2010-03-06 01:14:09 +0000867 if (port->state == BR_STATE_DISABLED ||
868 port->state == BR_STATE_BLOCKING)
Herbert Xueb1d1642010-02-27 19:41:45 +0000869 goto out;
870
871 if (port->multicast_startup_queries_sent <
872 br->multicast_startup_query_count)
873 port->multicast_startup_queries_sent++;
874
875 br_multicast_send_query(port->br, port,
876 port->multicast_startup_queries_sent);
877
878out:
879 spin_unlock(&br->multicast_lock);
880}
881
882void br_multicast_add_port(struct net_bridge_port *port)
883{
884 port->multicast_router = 1;
885
886 setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
887 (unsigned long)port);
888 setup_timer(&port->multicast_query_timer,
889 br_multicast_port_query_expired, (unsigned long)port);
890}
891
892void br_multicast_del_port(struct net_bridge_port *port)
893{
894 del_timer_sync(&port->multicast_router_timer);
895}
896
Herbert Xu561f1102010-02-27 19:41:50 +0000897static void __br_multicast_enable_port(struct net_bridge_port *port)
898{
899 port->multicast_startup_queries_sent = 0;
900
901 if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 ||
902 del_timer(&port->multicast_query_timer))
903 mod_timer(&port->multicast_query_timer, jiffies);
904}
905
Herbert Xueb1d1642010-02-27 19:41:45 +0000906void br_multicast_enable_port(struct net_bridge_port *port)
907{
908 struct net_bridge *br = port->br;
909
910 spin_lock(&br->multicast_lock);
911 if (br->multicast_disabled || !netif_running(br->dev))
912 goto out;
913
Herbert Xu561f1102010-02-27 19:41:50 +0000914 __br_multicast_enable_port(port);
Herbert Xueb1d1642010-02-27 19:41:45 +0000915
916out:
917 spin_unlock(&br->multicast_lock);
918}
919
920void br_multicast_disable_port(struct net_bridge_port *port)
921{
922 struct net_bridge *br = port->br;
923 struct net_bridge_port_group *pg;
924 struct hlist_node *p, *n;
925
926 spin_lock(&br->multicast_lock);
927 hlist_for_each_entry_safe(pg, p, n, &port->mglist, mglist)
928 br_multicast_del_pg(br, pg);
929
930 if (!hlist_unhashed(&port->rlist))
931 hlist_del_init_rcu(&port->rlist);
932 del_timer(&port->multicast_router_timer);
933 del_timer(&port->multicast_query_timer);
934 spin_unlock(&br->multicast_lock);
935}
936
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900937static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
938 struct net_bridge_port *port,
939 struct sk_buff *skb)
Herbert Xueb1d1642010-02-27 19:41:45 +0000940{
941 struct igmpv3_report *ih;
942 struct igmpv3_grec *grec;
943 int i;
944 int len;
945 int num;
946 int type;
947 int err = 0;
948 __be32 group;
949
950 if (!pskb_may_pull(skb, sizeof(*ih)))
951 return -EINVAL;
952
953 ih = igmpv3_report_hdr(skb);
954 num = ntohs(ih->ngrec);
955 len = sizeof(*ih);
956
957 for (i = 0; i < num; i++) {
958 len += sizeof(*grec);
959 if (!pskb_may_pull(skb, len))
960 return -EINVAL;
961
Herbert Xufd218cf2010-04-07 21:20:47 -0700962 grec = (void *)(skb->data + len - sizeof(*grec));
Herbert Xueb1d1642010-02-27 19:41:45 +0000963 group = grec->grec_mca;
964 type = grec->grec_type;
965
Eric Dumazet8eabf952010-04-20 03:20:05 +0000966 len += ntohs(grec->grec_nsrcs) * 4;
Herbert Xueb1d1642010-02-27 19:41:45 +0000967 if (!pskb_may_pull(skb, len))
968 return -EINVAL;
969
970 /* We treat this as an IGMPv2 report for now. */
971 switch (type) {
972 case IGMPV3_MODE_IS_INCLUDE:
973 case IGMPV3_MODE_IS_EXCLUDE:
974 case IGMPV3_CHANGE_TO_INCLUDE:
975 case IGMPV3_CHANGE_TO_EXCLUDE:
976 case IGMPV3_ALLOW_NEW_SOURCES:
977 case IGMPV3_BLOCK_OLD_SOURCES:
978 break;
979
980 default:
981 continue;
982 }
983
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900984 err = br_ip4_multicast_add_group(br, port, group);
Herbert Xueb1d1642010-02-27 19:41:45 +0000985 if (err)
986 break;
987 }
988
989 return err;
990}
991
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900992#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
993static int br_ip6_multicast_mld2_report(struct net_bridge *br,
994 struct net_bridge_port *port,
995 struct sk_buff *skb)
996{
997 struct icmp6hdr *icmp6h;
998 struct mld2_grec *grec;
999 int i;
1000 int len;
1001 int num;
1002 int err = 0;
1003
1004 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
1005 return -EINVAL;
1006
1007 icmp6h = icmp6_hdr(skb);
1008 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
1009 len = sizeof(*icmp6h);
1010
1011 for (i = 0; i < num; i++) {
1012 __be16 *nsrcs, _nsrcs;
1013
1014 nsrcs = skb_header_pointer(skb,
1015 len + offsetof(struct mld2_grec,
1016 grec_mca),
1017 sizeof(_nsrcs), &_nsrcs);
1018 if (!nsrcs)
1019 return -EINVAL;
1020
1021 if (!pskb_may_pull(skb,
1022 len + sizeof(*grec) +
1023 sizeof(struct in6_addr) * (*nsrcs)))
1024 return -EINVAL;
1025
1026 grec = (struct mld2_grec *)(skb->data + len);
1027 len += sizeof(*grec) + sizeof(struct in6_addr) * (*nsrcs);
1028
1029 /* We treat these as MLDv1 reports for now. */
1030 switch (grec->grec_type) {
1031 case MLD2_MODE_IS_INCLUDE:
1032 case MLD2_MODE_IS_EXCLUDE:
1033 case MLD2_CHANGE_TO_INCLUDE:
1034 case MLD2_CHANGE_TO_EXCLUDE:
1035 case MLD2_ALLOW_NEW_SOURCES:
1036 case MLD2_BLOCK_OLD_SOURCES:
1037 break;
1038
1039 default:
1040 continue;
1041 }
1042
1043 err = br_ip6_multicast_add_group(br, port, &grec->grec_mca);
1044 if (!err)
1045 break;
1046 }
1047
1048 return err;
1049}
1050#endif
1051
stephen hemminger7e80c122010-04-27 15:01:04 +00001052/*
1053 * Add port to rotuer_list
1054 * list is maintained ordered by pointer value
1055 * and locked by br->multicast_lock and RCU
1056 */
Herbert Xu0909e112010-02-27 19:41:49 +00001057static void br_multicast_add_router(struct net_bridge *br,
1058 struct net_bridge_port *port)
1059{
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001060 struct net_bridge_port *p;
stephen hemminger7e80c122010-04-27 15:01:04 +00001061 struct hlist_node *n, *slot = NULL;
Herbert Xu0909e112010-02-27 19:41:49 +00001062
David S. Miller709b9322010-04-27 16:49:58 -07001063 hlist_for_each_entry(p, n, &br->router_list, rlist) {
stephen hemminger7e80c122010-04-27 15:01:04 +00001064 if ((unsigned long) port >= (unsigned long) p)
1065 break;
1066 slot = n;
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001067 }
Herbert Xu0909e112010-02-27 19:41:49 +00001068
stephen hemminger7e80c122010-04-27 15:01:04 +00001069 if (slot)
1070 hlist_add_after_rcu(slot, &port->rlist);
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001071 else
1072 hlist_add_head_rcu(&port->rlist, &br->router_list);
Herbert Xu0909e112010-02-27 19:41:49 +00001073}
1074
Herbert Xueb1d1642010-02-27 19:41:45 +00001075static void br_multicast_mark_router(struct net_bridge *br,
1076 struct net_bridge_port *port)
1077{
1078 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +00001079
1080 if (!port) {
1081 if (br->multicast_router == 1)
1082 mod_timer(&br->multicast_router_timer,
1083 now + br->multicast_querier_interval);
1084 return;
1085 }
1086
1087 if (port->multicast_router != 1)
1088 return;
1089
1090 if (!hlist_unhashed(&port->rlist))
1091 goto timer;
1092
Herbert Xu0909e112010-02-27 19:41:49 +00001093 br_multicast_add_router(br, port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001094
1095timer:
1096 mod_timer(&port->multicast_router_timer,
1097 now + br->multicast_querier_interval);
1098}
1099
1100static void br_multicast_query_received(struct net_bridge *br,
1101 struct net_bridge_port *port,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001102 int saddr)
Herbert Xueb1d1642010-02-27 19:41:45 +00001103{
1104 if (saddr)
1105 mod_timer(&br->multicast_querier_timer,
1106 jiffies + br->multicast_querier_interval);
1107 else if (timer_pending(&br->multicast_querier_timer))
1108 return;
1109
1110 br_multicast_mark_router(br, port);
1111}
1112
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001113static int br_ip4_multicast_query(struct net_bridge *br,
1114 struct net_bridge_port *port,
1115 struct sk_buff *skb)
Herbert Xueb1d1642010-02-27 19:41:45 +00001116{
1117 struct iphdr *iph = ip_hdr(skb);
1118 struct igmphdr *ih = igmp_hdr(skb);
1119 struct net_bridge_mdb_entry *mp;
1120 struct igmpv3_query *ih3;
1121 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +00001122 struct net_bridge_port_group __rcu **pp;
Herbert Xueb1d1642010-02-27 19:41:45 +00001123 unsigned long max_delay;
1124 unsigned long now = jiffies;
1125 __be32 group;
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001126 int err = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001127
1128 spin_lock(&br->multicast_lock);
1129 if (!netif_running(br->dev) ||
1130 (port && port->state == BR_STATE_DISABLED))
1131 goto out;
1132
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001133 br_multicast_query_received(br, port, !!iph->saddr);
Herbert Xueb1d1642010-02-27 19:41:45 +00001134
1135 group = ih->group;
1136
1137 if (skb->len == sizeof(*ih)) {
1138 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1139
1140 if (!max_delay) {
1141 max_delay = 10 * HZ;
1142 group = 0;
1143 }
1144 } else {
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001145 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) {
1146 err = -EINVAL;
1147 goto out;
1148 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001149
1150 ih3 = igmpv3_query_hdr(skb);
1151 if (ih3->nsrcs)
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001152 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001153
YOSHIFUJI Hideaki / 吉藤英明0ba8c9e2010-03-15 19:27:00 +00001154 max_delay = ih3->code ?
1155 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
Herbert Xueb1d1642010-02-27 19:41:45 +00001156 }
1157
1158 if (!group)
1159 goto out;
1160
Eric Dumazete8051682010-11-15 06:38:10 +00001161 mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group);
Herbert Xueb1d1642010-02-27 19:41:45 +00001162 if (!mp)
1163 goto out;
1164
1165 max_delay *= br->multicast_last_member_count;
1166
Herbert Xu8a870172011-02-12 01:05:42 -08001167 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001168 (timer_pending(&mp->timer) ?
1169 time_after(mp->timer.expires, now + max_delay) :
1170 try_to_del_timer_sync(&mp->timer) >= 0))
1171 mod_timer(&mp->timer, now + max_delay);
1172
Eric Dumazete8051682010-11-15 06:38:10 +00001173 for (pp = &mp->ports;
1174 (p = mlock_dereference(*pp, br)) != NULL;
1175 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001176 if (timer_pending(&p->timer) ?
1177 time_after(p->timer.expires, now + max_delay) :
1178 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001179 mod_timer(&p->timer, now + max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001180 }
1181
1182out:
1183 spin_unlock(&br->multicast_lock);
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001184 return err;
Herbert Xueb1d1642010-02-27 19:41:45 +00001185}
1186
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001187#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1188static int br_ip6_multicast_query(struct net_bridge *br,
1189 struct net_bridge_port *port,
1190 struct sk_buff *skb)
1191{
1192 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1193 struct mld_msg *mld = (struct mld_msg *) icmp6_hdr(skb);
1194 struct net_bridge_mdb_entry *mp;
1195 struct mld2_query *mld2q;
Eric Dumazete8051682010-11-15 06:38:10 +00001196 struct net_bridge_port_group *p;
1197 struct net_bridge_port_group __rcu **pp;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001198 unsigned long max_delay;
1199 unsigned long now = jiffies;
1200 struct in6_addr *group = NULL;
1201 int err = 0;
1202
1203 spin_lock(&br->multicast_lock);
1204 if (!netif_running(br->dev) ||
1205 (port && port->state == BR_STATE_DISABLED))
1206 goto out;
1207
1208 br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->saddr));
1209
1210 if (skb->len == sizeof(*mld)) {
1211 if (!pskb_may_pull(skb, sizeof(*mld))) {
1212 err = -EINVAL;
1213 goto out;
1214 }
1215 mld = (struct mld_msg *) icmp6_hdr(skb);
1216 max_delay = msecs_to_jiffies(htons(mld->mld_maxdelay));
1217 if (max_delay)
1218 group = &mld->mld_mca;
1219 } else if (skb->len >= sizeof(*mld2q)) {
1220 if (!pskb_may_pull(skb, sizeof(*mld2q))) {
1221 err = -EINVAL;
1222 goto out;
1223 }
1224 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1225 if (!mld2q->mld2q_nsrcs)
1226 group = &mld2q->mld2q_mca;
1227 max_delay = mld2q->mld2q_mrc ? MLDV2_MRC(mld2q->mld2q_mrc) : 1;
1228 }
1229
1230 if (!group)
1231 goto out;
1232
Eric Dumazete8051682010-11-15 06:38:10 +00001233 mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001234 if (!mp)
1235 goto out;
1236
1237 max_delay *= br->multicast_last_member_count;
Herbert Xu8a870172011-02-12 01:05:42 -08001238 if (mp->mglist &&
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001239 (timer_pending(&mp->timer) ?
1240 time_after(mp->timer.expires, now + max_delay) :
1241 try_to_del_timer_sync(&mp->timer) >= 0))
1242 mod_timer(&mp->timer, now + max_delay);
1243
Eric Dumazete8051682010-11-15 06:38:10 +00001244 for (pp = &mp->ports;
1245 (p = mlock_dereference(*pp, br)) != NULL;
1246 pp = &p->next) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001247 if (timer_pending(&p->timer) ?
1248 time_after(p->timer.expires, now + max_delay) :
1249 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001250 mod_timer(&p->timer, now + max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001251 }
1252
1253out:
1254 spin_unlock(&br->multicast_lock);
1255 return err;
1256}
1257#endif
1258
Herbert Xueb1d1642010-02-27 19:41:45 +00001259static void br_multicast_leave_group(struct net_bridge *br,
1260 struct net_bridge_port *port,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001261 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +00001262{
1263 struct net_bridge_mdb_htable *mdb;
1264 struct net_bridge_mdb_entry *mp;
1265 struct net_bridge_port_group *p;
1266 unsigned long now;
1267 unsigned long time;
1268
Herbert Xueb1d1642010-02-27 19:41:45 +00001269 spin_lock(&br->multicast_lock);
1270 if (!netif_running(br->dev) ||
1271 (port && port->state == BR_STATE_DISABLED) ||
1272 timer_pending(&br->multicast_querier_timer))
1273 goto out;
1274
Eric Dumazete8051682010-11-15 06:38:10 +00001275 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001276 mp = br_mdb_ip_get(mdb, group);
1277 if (!mp)
1278 goto out;
1279
1280 now = jiffies;
1281 time = now + br->multicast_last_member_count *
1282 br->multicast_last_member_interval;
1283
1284 if (!port) {
Herbert Xu8a870172011-02-12 01:05:42 -08001285 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001286 (timer_pending(&mp->timer) ?
1287 time_after(mp->timer.expires, time) :
1288 try_to_del_timer_sync(&mp->timer) >= 0)) {
1289 mod_timer(&mp->timer, time);
1290
1291 mp->queries_sent = 0;
1292 mod_timer(&mp->query_timer, now);
1293 }
1294
1295 goto out;
1296 }
1297
Eric Dumazete8051682010-11-15 06:38:10 +00001298 for (p = mlock_dereference(mp->ports, br);
1299 p != NULL;
1300 p = mlock_dereference(p->next, br)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001301 if (p->port != port)
1302 continue;
1303
1304 if (!hlist_unhashed(&p->mglist) &&
1305 (timer_pending(&p->timer) ?
1306 time_after(p->timer.expires, time) :
1307 try_to_del_timer_sync(&p->timer) >= 0)) {
1308 mod_timer(&p->timer, time);
1309
1310 p->queries_sent = 0;
1311 mod_timer(&p->query_timer, now);
1312 }
1313
1314 break;
1315 }
1316
1317out:
1318 spin_unlock(&br->multicast_lock);
1319}
1320
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001321static void br_ip4_multicast_leave_group(struct net_bridge *br,
1322 struct net_bridge_port *port,
1323 __be32 group)
1324{
1325 struct br_ip br_group;
1326
1327 if (ipv4_is_local_multicast(group))
1328 return;
1329
1330 br_group.u.ip4 = group;
1331 br_group.proto = htons(ETH_P_IP);
1332
1333 br_multicast_leave_group(br, port, &br_group);
1334}
1335
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001336#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1337static void br_ip6_multicast_leave_group(struct net_bridge *br,
1338 struct net_bridge_port *port,
1339 const struct in6_addr *group)
1340{
1341 struct br_ip br_group;
1342
1343 if (ipv6_is_local_multicast(group))
1344 return;
1345
1346 ipv6_addr_copy(&br_group.u.ip6, group);
1347 br_group.proto = htons(ETH_P_IPV6);
1348
1349 br_multicast_leave_group(br, port, &br_group);
1350}
1351#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001352
Herbert Xueb1d1642010-02-27 19:41:45 +00001353static int br_multicast_ipv4_rcv(struct net_bridge *br,
1354 struct net_bridge_port *port,
1355 struct sk_buff *skb)
1356{
1357 struct sk_buff *skb2 = skb;
1358 struct iphdr *iph;
1359 struct igmphdr *ih;
1360 unsigned len;
1361 unsigned offset;
1362 int err;
1363
Herbert Xueb1d1642010-02-27 19:41:45 +00001364 /* We treat OOM as packet loss for now. */
1365 if (!pskb_may_pull(skb, sizeof(*iph)))
1366 return -EINVAL;
1367
1368 iph = ip_hdr(skb);
1369
1370 if (iph->ihl < 5 || iph->version != 4)
1371 return -EINVAL;
1372
1373 if (!pskb_may_pull(skb, ip_hdrlen(skb)))
1374 return -EINVAL;
1375
1376 iph = ip_hdr(skb);
1377
1378 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1379 return -EINVAL;
1380
1381 if (iph->protocol != IPPROTO_IGMP)
1382 return 0;
1383
1384 len = ntohs(iph->tot_len);
1385 if (skb->len < len || len < ip_hdrlen(skb))
1386 return -EINVAL;
1387
1388 if (skb->len > len) {
1389 skb2 = skb_clone(skb, GFP_ATOMIC);
1390 if (!skb2)
1391 return -ENOMEM;
1392
1393 err = pskb_trim_rcsum(skb2, len);
1394 if (err)
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001395 goto err_out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001396 }
1397
1398 len -= ip_hdrlen(skb2);
1399 offset = skb_network_offset(skb2) + ip_hdrlen(skb2);
1400 __skb_pull(skb2, offset);
1401 skb_reset_transport_header(skb2);
1402
1403 err = -EINVAL;
1404 if (!pskb_may_pull(skb2, sizeof(*ih)))
1405 goto out;
1406
Herbert Xueb1d1642010-02-27 19:41:45 +00001407 switch (skb2->ip_summed) {
1408 case CHECKSUM_COMPLETE:
1409 if (!csum_fold(skb2->csum))
1410 break;
1411 /* fall through */
1412 case CHECKSUM_NONE:
1413 skb2->csum = 0;
1414 if (skb_checksum_complete(skb2))
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001415 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001416 }
1417
1418 err = 0;
1419
1420 BR_INPUT_SKB_CB(skb)->igmp = 1;
1421 ih = igmp_hdr(skb2);
1422
1423 switch (ih->type) {
1424 case IGMP_HOST_MEMBERSHIP_REPORT:
1425 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1426 BR_INPUT_SKB_CB(skb2)->mrouters_only = 1;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001427 err = br_ip4_multicast_add_group(br, port, ih->group);
Herbert Xueb1d1642010-02-27 19:41:45 +00001428 break;
1429 case IGMPV3_HOST_MEMBERSHIP_REPORT:
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001430 err = br_ip4_multicast_igmp3_report(br, port, skb2);
Herbert Xueb1d1642010-02-27 19:41:45 +00001431 break;
1432 case IGMP_HOST_MEMBERSHIP_QUERY:
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001433 err = br_ip4_multicast_query(br, port, skb2);
Herbert Xueb1d1642010-02-27 19:41:45 +00001434 break;
1435 case IGMP_HOST_LEAVE_MESSAGE:
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001436 br_ip4_multicast_leave_group(br, port, ih->group);
Herbert Xueb1d1642010-02-27 19:41:45 +00001437 break;
1438 }
1439
1440out:
1441 __skb_push(skb2, offset);
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001442err_out:
Herbert Xueb1d1642010-02-27 19:41:45 +00001443 if (skb2 != skb)
1444 kfree_skb(skb2);
1445 return err;
1446}
1447
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001448#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1449static int br_multicast_ipv6_rcv(struct net_bridge *br,
1450 struct net_bridge_port *port,
1451 struct sk_buff *skb)
1452{
Tomas Winkler9d890812011-01-03 11:26:08 -08001453 struct sk_buff *skb2;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001454 struct ipv6hdr *ip6h;
1455 struct icmp6hdr *icmp6h;
1456 u8 nexthdr;
1457 unsigned len;
Kulikov Vasiliybb7a0bd2010-07-15 08:47:33 +00001458 int offset;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001459 int err;
1460
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001461 if (!pskb_may_pull(skb, sizeof(*ip6h)))
1462 return -EINVAL;
1463
1464 ip6h = ipv6_hdr(skb);
1465
1466 /*
1467 * We're interested in MLD messages only.
1468 * - Version is 6
1469 * - MLD has always Router Alert hop-by-hop option
1470 * - But we do not support jumbrograms.
1471 */
1472 if (ip6h->version != 6 ||
1473 ip6h->nexthdr != IPPROTO_HOPOPTS ||
1474 ip6h->payload_len == 0)
1475 return 0;
1476
1477 len = ntohs(ip6h->payload_len);
1478 if (skb->len < len)
1479 return -EINVAL;
1480
1481 nexthdr = ip6h->nexthdr;
1482 offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr);
1483
1484 if (offset < 0 || nexthdr != IPPROTO_ICMPV6)
1485 return 0;
1486
1487 /* Okay, we found ICMPv6 header */
1488 skb2 = skb_clone(skb, GFP_ATOMIC);
1489 if (!skb2)
1490 return -ENOMEM;
1491
Tomas Winkler9d890812011-01-03 11:26:08 -08001492 err = -EINVAL;
1493 if (!pskb_may_pull(skb2, offset + sizeof(struct icmp6hdr)))
1494 goto out;
1495
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001496 len -= offset - skb_network_offset(skb2);
1497
1498 __skb_pull(skb2, offset);
1499 skb_reset_transport_header(skb2);
1500
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001501 icmp6h = icmp6_hdr(skb2);
1502
1503 switch (icmp6h->icmp6_type) {
1504 case ICMPV6_MGM_QUERY:
1505 case ICMPV6_MGM_REPORT:
1506 case ICMPV6_MGM_REDUCTION:
1507 case ICMPV6_MLD2_REPORT:
1508 break;
1509 default:
1510 err = 0;
1511 goto out;
1512 }
1513
1514 /* Okay, we found MLD message. Check further. */
1515 if (skb2->len > len) {
1516 err = pskb_trim_rcsum(skb2, len);
1517 if (err)
1518 goto out;
1519 }
1520
1521 switch (skb2->ip_summed) {
1522 case CHECKSUM_COMPLETE:
1523 if (!csum_fold(skb2->csum))
1524 break;
1525 /*FALLTHROUGH*/
1526 case CHECKSUM_NONE:
1527 skb2->csum = 0;
1528 if (skb_checksum_complete(skb2))
1529 goto out;
1530 }
1531
1532 err = 0;
1533
1534 BR_INPUT_SKB_CB(skb)->igmp = 1;
1535
1536 switch (icmp6h->icmp6_type) {
1537 case ICMPV6_MGM_REPORT:
1538 {
Tomas Winkler9d890812011-01-03 11:26:08 -08001539 struct mld_msg *mld;
1540 if (!pskb_may_pull(skb2, sizeof(*mld))) {
1541 err = -EINVAL;
1542 goto out;
1543 }
1544 mld = (struct mld_msg *)skb_transport_header(skb2);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001545 BR_INPUT_SKB_CB(skb2)->mrouters_only = 1;
1546 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca);
1547 break;
1548 }
1549 case ICMPV6_MLD2_REPORT:
1550 err = br_ip6_multicast_mld2_report(br, port, skb2);
1551 break;
1552 case ICMPV6_MGM_QUERY:
1553 err = br_ip6_multicast_query(br, port, skb2);
1554 break;
1555 case ICMPV6_MGM_REDUCTION:
1556 {
Tomas Winkler9d890812011-01-03 11:26:08 -08001557 struct mld_msg *mld;
1558 if (!pskb_may_pull(skb2, sizeof(*mld))) {
1559 err = -EINVAL;
1560 goto out;
1561 }
1562 mld = (struct mld_msg *)skb_transport_header(skb2);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001563 br_ip6_multicast_leave_group(br, port, &mld->mld_mca);
1564 }
1565 }
1566
1567out:
Tomas Winkler9d890812011-01-03 11:26:08 -08001568 kfree_skb(skb2);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001569 return err;
1570}
1571#endif
1572
Herbert Xueb1d1642010-02-27 19:41:45 +00001573int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1574 struct sk_buff *skb)
1575{
YOSHIFUJI Hideaki / 吉藤英明1fafc7a2010-04-25 08:06:40 +00001576 BR_INPUT_SKB_CB(skb)->igmp = 0;
1577 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1578
Herbert Xueb1d1642010-02-27 19:41:45 +00001579 if (br->multicast_disabled)
1580 return 0;
1581
1582 switch (skb->protocol) {
1583 case htons(ETH_P_IP):
1584 return br_multicast_ipv4_rcv(br, port, skb);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001585#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1586 case htons(ETH_P_IPV6):
1587 return br_multicast_ipv6_rcv(br, port, skb);
1588#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001589 }
1590
1591 return 0;
1592}
1593
1594static void br_multicast_query_expired(unsigned long data)
1595{
1596 struct net_bridge *br = (void *)data;
1597
1598 spin_lock(&br->multicast_lock);
1599 if (br->multicast_startup_queries_sent <
1600 br->multicast_startup_query_count)
1601 br->multicast_startup_queries_sent++;
1602
1603 br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent);
1604
1605 spin_unlock(&br->multicast_lock);
1606}
1607
1608void br_multicast_init(struct net_bridge *br)
1609{
1610 br->hash_elasticity = 4;
1611 br->hash_max = 512;
1612
1613 br->multicast_router = 1;
1614 br->multicast_last_member_count = 2;
1615 br->multicast_startup_query_count = 2;
1616
1617 br->multicast_last_member_interval = HZ;
1618 br->multicast_query_response_interval = 10 * HZ;
1619 br->multicast_startup_query_interval = 125 * HZ / 4;
1620 br->multicast_query_interval = 125 * HZ;
1621 br->multicast_querier_interval = 255 * HZ;
1622 br->multicast_membership_interval = 260 * HZ;
1623
1624 spin_lock_init(&br->multicast_lock);
1625 setup_timer(&br->multicast_router_timer,
1626 br_multicast_local_router_expired, 0);
1627 setup_timer(&br->multicast_querier_timer,
1628 br_multicast_local_router_expired, 0);
1629 setup_timer(&br->multicast_query_timer, br_multicast_query_expired,
1630 (unsigned long)br);
1631}
1632
1633void br_multicast_open(struct net_bridge *br)
1634{
1635 br->multicast_startup_queries_sent = 0;
1636
1637 if (br->multicast_disabled)
1638 return;
1639
1640 mod_timer(&br->multicast_query_timer, jiffies);
1641}
1642
1643void br_multicast_stop(struct net_bridge *br)
1644{
1645 struct net_bridge_mdb_htable *mdb;
1646 struct net_bridge_mdb_entry *mp;
1647 struct hlist_node *p, *n;
1648 u32 ver;
1649 int i;
1650
1651 del_timer_sync(&br->multicast_router_timer);
1652 del_timer_sync(&br->multicast_querier_timer);
1653 del_timer_sync(&br->multicast_query_timer);
1654
1655 spin_lock_bh(&br->multicast_lock);
Eric Dumazete8051682010-11-15 06:38:10 +00001656 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001657 if (!mdb)
1658 goto out;
1659
1660 br->mdb = NULL;
1661
1662 ver = mdb->ver;
1663 for (i = 0; i < mdb->max; i++) {
1664 hlist_for_each_entry_safe(mp, p, n, &mdb->mhash[i],
1665 hlist[ver]) {
1666 del_timer(&mp->timer);
1667 del_timer(&mp->query_timer);
1668 call_rcu_bh(&mp->rcu, br_multicast_free_group);
1669 }
1670 }
1671
1672 if (mdb->old) {
1673 spin_unlock_bh(&br->multicast_lock);
Herbert Xu10cc2b52010-03-05 21:03:35 +00001674 rcu_barrier_bh();
Herbert Xueb1d1642010-02-27 19:41:45 +00001675 spin_lock_bh(&br->multicast_lock);
1676 WARN_ON(mdb->old);
1677 }
1678
1679 mdb->old = mdb;
1680 call_rcu_bh(&mdb->rcu, br_mdb_free);
1681
1682out:
1683 spin_unlock_bh(&br->multicast_lock);
1684}
Herbert Xu0909e112010-02-27 19:41:49 +00001685
1686int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1687{
1688 int err = -ENOENT;
1689
1690 spin_lock_bh(&br->multicast_lock);
1691 if (!netif_running(br->dev))
1692 goto unlock;
1693
1694 switch (val) {
1695 case 0:
1696 case 2:
1697 del_timer(&br->multicast_router_timer);
1698 /* fall through */
1699 case 1:
1700 br->multicast_router = val;
1701 err = 0;
1702 break;
1703
1704 default:
1705 err = -EINVAL;
1706 break;
1707 }
1708
1709unlock:
1710 spin_unlock_bh(&br->multicast_lock);
1711
1712 return err;
1713}
1714
1715int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1716{
1717 struct net_bridge *br = p->br;
1718 int err = -ENOENT;
1719
1720 spin_lock(&br->multicast_lock);
1721 if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED)
1722 goto unlock;
1723
1724 switch (val) {
1725 case 0:
1726 case 1:
1727 case 2:
1728 p->multicast_router = val;
1729 err = 0;
1730
1731 if (val < 2 && !hlist_unhashed(&p->rlist))
1732 hlist_del_init_rcu(&p->rlist);
1733
1734 if (val == 1)
1735 break;
1736
1737 del_timer(&p->multicast_router_timer);
1738
1739 if (val == 0)
1740 break;
1741
1742 br_multicast_add_router(br, p);
1743 break;
1744
1745 default:
1746 err = -EINVAL;
1747 break;
1748 }
1749
1750unlock:
1751 spin_unlock(&br->multicast_lock);
1752
1753 return err;
1754}
Herbert Xu561f1102010-02-27 19:41:50 +00001755
1756int br_multicast_toggle(struct net_bridge *br, unsigned long val)
1757{
1758 struct net_bridge_port *port;
Herbert Xu3a7fda02010-07-29 00:45:30 +00001759 int err = 0;
Eric Dumazete8051682010-11-15 06:38:10 +00001760 struct net_bridge_mdb_htable *mdb;
Herbert Xu561f1102010-02-27 19:41:50 +00001761
1762 spin_lock(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00001763 if (br->multicast_disabled == !val)
1764 goto unlock;
1765
1766 br->multicast_disabled = !val;
1767 if (br->multicast_disabled)
1768 goto unlock;
1769
Herbert Xu3a7fda02010-07-29 00:45:30 +00001770 if (!netif_running(br->dev))
1771 goto unlock;
1772
Eric Dumazete8051682010-11-15 06:38:10 +00001773 mdb = mlock_dereference(br->mdb, br);
1774 if (mdb) {
1775 if (mdb->old) {
Herbert Xu561f1102010-02-27 19:41:50 +00001776 err = -EEXIST;
1777rollback:
1778 br->multicast_disabled = !!val;
1779 goto unlock;
1780 }
1781
Eric Dumazete8051682010-11-15 06:38:10 +00001782 err = br_mdb_rehash(&br->mdb, mdb->max,
Herbert Xu561f1102010-02-27 19:41:50 +00001783 br->hash_elasticity);
1784 if (err)
1785 goto rollback;
1786 }
1787
1788 br_multicast_open(br);
1789 list_for_each_entry(port, &br->port_list, list) {
1790 if (port->state == BR_STATE_DISABLED ||
1791 port->state == BR_STATE_BLOCKING)
1792 continue;
1793
1794 __br_multicast_enable_port(port);
1795 }
1796
1797unlock:
1798 spin_unlock(&br->multicast_lock);
1799
1800 return err;
1801}
Herbert Xub1951672010-02-27 19:41:51 +00001802
1803int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
1804{
1805 int err = -ENOENT;
1806 u32 old;
Eric Dumazete8051682010-11-15 06:38:10 +00001807 struct net_bridge_mdb_htable *mdb;
Herbert Xub1951672010-02-27 19:41:51 +00001808
1809 spin_lock(&br->multicast_lock);
1810 if (!netif_running(br->dev))
1811 goto unlock;
1812
1813 err = -EINVAL;
1814 if (!is_power_of_2(val))
1815 goto unlock;
Eric Dumazete8051682010-11-15 06:38:10 +00001816
1817 mdb = mlock_dereference(br->mdb, br);
1818 if (mdb && val < mdb->size)
Herbert Xub1951672010-02-27 19:41:51 +00001819 goto unlock;
1820
1821 err = 0;
1822
1823 old = br->hash_max;
1824 br->hash_max = val;
1825
Eric Dumazete8051682010-11-15 06:38:10 +00001826 if (mdb) {
1827 if (mdb->old) {
Herbert Xub1951672010-02-27 19:41:51 +00001828 err = -EEXIST;
1829rollback:
1830 br->hash_max = old;
1831 goto unlock;
1832 }
1833
1834 err = br_mdb_rehash(&br->mdb, br->hash_max,
1835 br->hash_elasticity);
1836 if (err)
1837 goto rollback;
1838 }
1839
1840unlock:
1841 spin_unlock(&br->multicast_lock);
1842
1843 return err;
1844}