blob: 9d21d98ae5fa98cc5d187bc6ae9b6ceb85f294cc [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
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900102static struct net_bridge_mdb_entry *br_mdb_ip4_get(
Herbert Xueb1d1642010-02-27 19:41:45 +0000103 struct net_bridge_mdb_htable *mdb, __be32 dst)
104{
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900105 struct br_ip br_dst;
Herbert Xu0821ec52010-03-15 20:38:25 -0700106
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900107 br_dst.u.ip4 = dst;
108 br_dst.proto = htons(ETH_P_IP);
109
110 return __br_mdb_ip_get(mdb, &br_dst, __br_ip4_hash(mdb, dst));
111}
112
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900113#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
114static struct net_bridge_mdb_entry *br_mdb_ip6_get(
115 struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst)
116{
117 struct br_ip br_dst;
118
119 ipv6_addr_copy(&br_dst.u.ip6, dst);
120 br_dst.proto = htons(ETH_P_IPV6);
121
122 return __br_mdb_ip_get(mdb, &br_dst, __br_ip6_hash(mdb, dst));
123}
124#endif
125
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900126static struct net_bridge_mdb_entry *br_mdb_ip_get(
127 struct net_bridge_mdb_htable *mdb, struct br_ip *dst)
128{
Herbert Xueb1d1642010-02-27 19:41:45 +0000129 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
130}
131
132struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
133 struct sk_buff *skb)
134{
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900135 struct net_bridge_mdb_htable *mdb = br->mdb;
136 struct br_ip ip;
137
138 if (!mdb || br->multicast_disabled)
Herbert Xueb1d1642010-02-27 19:41:45 +0000139 return NULL;
140
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900141 if (BR_INPUT_SKB_CB(skb)->igmp)
142 return NULL;
143
144 ip.proto = skb->protocol;
145
Herbert Xueb1d1642010-02-27 19:41:45 +0000146 switch (skb->protocol) {
147 case htons(ETH_P_IP):
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900148 ip.u.ip4 = ip_hdr(skb)->daddr;
149 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900150#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
151 case htons(ETH_P_IPV6):
152 ipv6_addr_copy(&ip.u.ip6, &ipv6_hdr(skb)->daddr);
153 break;
154#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900155 default:
156 return NULL;
Herbert Xueb1d1642010-02-27 19:41:45 +0000157 }
158
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900159 return br_mdb_ip_get(mdb, &ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000160}
161
162static void br_mdb_free(struct rcu_head *head)
163{
164 struct net_bridge_mdb_htable *mdb =
165 container_of(head, struct net_bridge_mdb_htable, rcu);
166 struct net_bridge_mdb_htable *old = mdb->old;
167
168 mdb->old = NULL;
169 kfree(old->mhash);
170 kfree(old);
171}
172
173static int br_mdb_copy(struct net_bridge_mdb_htable *new,
174 struct net_bridge_mdb_htable *old,
175 int elasticity)
176{
177 struct net_bridge_mdb_entry *mp;
178 struct hlist_node *p;
179 int maxlen;
180 int len;
181 int i;
182
183 for (i = 0; i < old->max; i++)
184 hlist_for_each_entry(mp, p, &old->mhash[i], hlist[old->ver])
185 hlist_add_head(&mp->hlist[new->ver],
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900186 &new->mhash[br_ip_hash(new, &mp->addr)]);
Herbert Xueb1d1642010-02-27 19:41:45 +0000187
188 if (!elasticity)
189 return 0;
190
191 maxlen = 0;
192 for (i = 0; i < new->max; i++) {
193 len = 0;
194 hlist_for_each_entry(mp, p, &new->mhash[i], hlist[new->ver])
195 len++;
196 if (len > maxlen)
197 maxlen = len;
198 }
199
200 return maxlen > elasticity ? -EINVAL : 0;
201}
202
203static void br_multicast_free_pg(struct rcu_head *head)
204{
205 struct net_bridge_port_group *p =
206 container_of(head, struct net_bridge_port_group, rcu);
207
208 kfree(p);
209}
210
211static void br_multicast_free_group(struct rcu_head *head)
212{
213 struct net_bridge_mdb_entry *mp =
214 container_of(head, struct net_bridge_mdb_entry, rcu);
215
216 kfree(mp);
217}
218
219static void br_multicast_group_expired(unsigned long data)
220{
221 struct net_bridge_mdb_entry *mp = (void *)data;
222 struct net_bridge *br = mp->br;
223 struct net_bridge_mdb_htable *mdb;
224
225 spin_lock(&br->multicast_lock);
226 if (!netif_running(br->dev) || timer_pending(&mp->timer))
227 goto out;
228
229 if (!hlist_unhashed(&mp->mglist))
230 hlist_del_init(&mp->mglist);
231
232 if (mp->ports)
233 goto out;
234
235 mdb = br->mdb;
236 hlist_del_rcu(&mp->hlist[mdb->ver]);
237 mdb->size--;
238
239 del_timer(&mp->query_timer);
240 call_rcu_bh(&mp->rcu, br_multicast_free_group);
241
242out:
243 spin_unlock(&br->multicast_lock);
244}
245
246static void br_multicast_del_pg(struct net_bridge *br,
247 struct net_bridge_port_group *pg)
248{
249 struct net_bridge_mdb_htable *mdb = br->mdb;
250 struct net_bridge_mdb_entry *mp;
251 struct net_bridge_port_group *p;
252 struct net_bridge_port_group **pp;
253
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900254 mp = br_mdb_ip_get(mdb, &pg->addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000255 if (WARN_ON(!mp))
256 return;
257
258 for (pp = &mp->ports; (p = *pp); pp = &p->next) {
259 if (p != pg)
260 continue;
261
stephen hemminger83f6a742010-04-27 15:01:06 +0000262 rcu_assign_pointer(*pp, p->next);
Herbert Xueb1d1642010-02-27 19:41:45 +0000263 hlist_del_init(&p->mglist);
264 del_timer(&p->timer);
265 del_timer(&p->query_timer);
266 call_rcu_bh(&p->rcu, br_multicast_free_pg);
267
268 if (!mp->ports && hlist_unhashed(&mp->mglist) &&
269 netif_running(br->dev))
270 mod_timer(&mp->timer, jiffies);
271
272 return;
273 }
274
275 WARN_ON(1);
276}
277
278static void br_multicast_port_group_expired(unsigned long data)
279{
280 struct net_bridge_port_group *pg = (void *)data;
281 struct net_bridge *br = pg->port->br;
282
283 spin_lock(&br->multicast_lock);
284 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
285 hlist_unhashed(&pg->mglist))
286 goto out;
287
288 br_multicast_del_pg(br, pg);
289
290out:
291 spin_unlock(&br->multicast_lock);
292}
293
294static int br_mdb_rehash(struct net_bridge_mdb_htable **mdbp, int max,
295 int elasticity)
296{
297 struct net_bridge_mdb_htable *old = *mdbp;
298 struct net_bridge_mdb_htable *mdb;
299 int err;
300
301 mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
302 if (!mdb)
303 return -ENOMEM;
304
305 mdb->max = max;
306 mdb->old = old;
307
308 mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
309 if (!mdb->mhash) {
310 kfree(mdb);
311 return -ENOMEM;
312 }
313
314 mdb->size = old ? old->size : 0;
315 mdb->ver = old ? old->ver ^ 1 : 0;
316
317 if (!old || elasticity)
318 get_random_bytes(&mdb->secret, sizeof(mdb->secret));
319 else
320 mdb->secret = old->secret;
321
322 if (!old)
323 goto out;
324
325 err = br_mdb_copy(mdb, old, elasticity);
326 if (err) {
327 kfree(mdb->mhash);
328 kfree(mdb);
329 return err;
330 }
331
332 call_rcu_bh(&mdb->rcu, br_mdb_free);
333
334out:
335 rcu_assign_pointer(*mdbp, mdb);
336
337 return 0;
338}
339
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900340static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
341 __be32 group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000342{
343 struct sk_buff *skb;
344 struct igmphdr *ih;
345 struct ethhdr *eth;
346 struct iphdr *iph;
347
348 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
349 sizeof(*ih) + 4);
350 if (!skb)
351 goto out;
352
353 skb->protocol = htons(ETH_P_IP);
354
355 skb_reset_mac_header(skb);
356 eth = eth_hdr(skb);
357
358 memcpy(eth->h_source, br->dev->dev_addr, 6);
359 eth->h_dest[0] = 1;
360 eth->h_dest[1] = 0;
361 eth->h_dest[2] = 0x5e;
362 eth->h_dest[3] = 0;
363 eth->h_dest[4] = 0;
364 eth->h_dest[5] = 1;
365 eth->h_proto = htons(ETH_P_IP);
366 skb_put(skb, sizeof(*eth));
367
368 skb_set_network_header(skb, skb->len);
369 iph = ip_hdr(skb);
370
371 iph->version = 4;
372 iph->ihl = 6;
373 iph->tos = 0xc0;
374 iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4);
375 iph->id = 0;
376 iph->frag_off = htons(IP_DF);
377 iph->ttl = 1;
378 iph->protocol = IPPROTO_IGMP;
379 iph->saddr = 0;
380 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
381 ((u8 *)&iph[1])[0] = IPOPT_RA;
382 ((u8 *)&iph[1])[1] = 4;
383 ((u8 *)&iph[1])[2] = 0;
384 ((u8 *)&iph[1])[3] = 0;
385 ip_send_check(iph);
386 skb_put(skb, 24);
387
388 skb_set_transport_header(skb, skb->len);
389 ih = igmp_hdr(skb);
390 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
391 ih->code = (group ? br->multicast_last_member_interval :
392 br->multicast_query_response_interval) /
393 (HZ / IGMP_TIMER_SCALE);
394 ih->group = group;
395 ih->csum = 0;
396 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
397 skb_put(skb, sizeof(*ih));
398
399 __skb_pull(skb, sizeof(*eth));
400
401out:
402 return skb;
403}
404
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900405#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
406static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
407 struct in6_addr *group)
408{
409 struct sk_buff *skb;
410 struct ipv6hdr *ip6h;
411 struct mld_msg *mldq;
412 struct ethhdr *eth;
413 u8 *hopopt;
414 unsigned long interval;
415
416 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
417 8 + sizeof(*mldq));
418 if (!skb)
419 goto out;
420
421 skb->protocol = htons(ETH_P_IPV6);
422
423 /* Ethernet header */
424 skb_reset_mac_header(skb);
425 eth = eth_hdr(skb);
426
427 memcpy(eth->h_source, br->dev->dev_addr, 6);
428 ipv6_eth_mc_map(group, eth->h_dest);
429 eth->h_proto = htons(ETH_P_IPV6);
430 skb_put(skb, sizeof(*eth));
431
432 /* IPv6 header + HbH option */
433 skb_set_network_header(skb, skb->len);
434 ip6h = ipv6_hdr(skb);
435
436 *(__force __be32 *)ip6h = htonl(0x60000000);
437 ip6h->payload_len = 8 + sizeof(*mldq);
438 ip6h->nexthdr = IPPROTO_HOPOPTS;
439 ip6h->hop_limit = 1;
440 ipv6_addr_set(&ip6h->saddr, 0, 0, 0, 0);
441 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
442
443 hopopt = (u8 *)(ip6h + 1);
444 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
445 hopopt[1] = 0; /* length of HbH */
446 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
447 hopopt[3] = 2; /* Length of RA Option */
448 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
449 hopopt[5] = 0;
450 hopopt[6] = IPV6_TLV_PAD0; /* Pad0 */
451 hopopt[7] = IPV6_TLV_PAD0; /* Pad0 */
452
453 skb_put(skb, sizeof(*ip6h) + 8);
454
455 /* ICMPv6 */
456 skb_set_transport_header(skb, skb->len);
457 mldq = (struct mld_msg *) icmp6_hdr(skb);
458
459 interval = ipv6_addr_any(group) ? br->multicast_last_member_interval :
460 br->multicast_query_response_interval;
461
462 mldq->mld_type = ICMPV6_MGM_QUERY;
463 mldq->mld_code = 0;
464 mldq->mld_cksum = 0;
465 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
466 mldq->mld_reserved = 0;
467 ipv6_addr_copy(&mldq->mld_mca, group);
468
469 /* checksum */
470 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
471 sizeof(*mldq), IPPROTO_ICMPV6,
472 csum_partial(mldq,
473 sizeof(*mldq), 0));
474 skb_put(skb, sizeof(*mldq));
475
476 __skb_pull(skb, sizeof(*eth));
477
478out:
479 return skb;
480}
481#endif
482
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900483static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
484 struct br_ip *addr)
485{
486 switch (addr->proto) {
487 case htons(ETH_P_IP):
488 return br_ip4_multicast_alloc_query(br, addr->u.ip4);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900489#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
490 case htons(ETH_P_IPV6):
491 return br_ip6_multicast_alloc_query(br, &addr->u.ip6);
492#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900493 }
494 return NULL;
495}
496
Herbert Xueb1d1642010-02-27 19:41:45 +0000497static void br_multicast_send_group_query(struct net_bridge_mdb_entry *mp)
498{
499 struct net_bridge *br = mp->br;
500 struct sk_buff *skb;
501
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900502 skb = br_multicast_alloc_query(br, &mp->addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000503 if (!skb)
504 goto timer;
505
506 netif_rx(skb);
507
508timer:
509 if (++mp->queries_sent < br->multicast_last_member_count)
510 mod_timer(&mp->query_timer,
511 jiffies + br->multicast_last_member_interval);
512}
513
514static void br_multicast_group_query_expired(unsigned long data)
515{
516 struct net_bridge_mdb_entry *mp = (void *)data;
517 struct net_bridge *br = mp->br;
518
519 spin_lock(&br->multicast_lock);
520 if (!netif_running(br->dev) || hlist_unhashed(&mp->mglist) ||
521 mp->queries_sent >= br->multicast_last_member_count)
522 goto out;
523
524 br_multicast_send_group_query(mp);
525
526out:
527 spin_unlock(&br->multicast_lock);
528}
529
530static void br_multicast_send_port_group_query(struct net_bridge_port_group *pg)
531{
532 struct net_bridge_port *port = pg->port;
533 struct net_bridge *br = port->br;
534 struct sk_buff *skb;
535
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900536 skb = br_multicast_alloc_query(br, &pg->addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000537 if (!skb)
538 goto timer;
539
540 br_deliver(port, skb);
541
542timer:
543 if (++pg->queries_sent < br->multicast_last_member_count)
544 mod_timer(&pg->query_timer,
545 jiffies + br->multicast_last_member_interval);
546}
547
548static void br_multicast_port_group_query_expired(unsigned long data)
549{
550 struct net_bridge_port_group *pg = (void *)data;
551 struct net_bridge_port *port = pg->port;
552 struct net_bridge *br = port->br;
553
554 spin_lock(&br->multicast_lock);
555 if (!netif_running(br->dev) || hlist_unhashed(&pg->mglist) ||
556 pg->queries_sent >= br->multicast_last_member_count)
557 goto out;
558
559 br_multicast_send_port_group_query(pg);
560
561out:
562 spin_unlock(&br->multicast_lock);
563}
564
565static struct net_bridge_mdb_entry *br_multicast_get_group(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900566 struct net_bridge *br, struct net_bridge_port *port,
567 struct br_ip *group, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +0000568{
569 struct net_bridge_mdb_htable *mdb = br->mdb;
570 struct net_bridge_mdb_entry *mp;
571 struct hlist_node *p;
572 unsigned count = 0;
573 unsigned max;
574 int elasticity;
575 int err;
576
577 hlist_for_each_entry(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) {
578 count++;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900579 if (unlikely(br_ip_equal(group, &mp->addr)))
Herbert Xueb1d1642010-02-27 19:41:45 +0000580 return mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000581 }
582
583 elasticity = 0;
584 max = mdb->max;
585
586 if (unlikely(count > br->hash_elasticity && count)) {
587 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000588 br_info(br, "Multicast hash table "
589 "chain limit reached: %s\n",
590 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000591
592 elasticity = br->hash_elasticity;
593 }
594
595 if (mdb->size >= max) {
596 max *= 2;
597 if (unlikely(max >= br->hash_max)) {
stephen hemminger28a16c92010-05-10 09:31:09 +0000598 br_warn(br, "Multicast hash table maximum "
599 "reached, disabling snooping: %s, %d\n",
600 port ? port->dev->name : br->dev->name, max);
Herbert Xueb1d1642010-02-27 19:41:45 +0000601 err = -E2BIG;
602disable:
603 br->multicast_disabled = 1;
604 goto err;
605 }
606 }
607
608 if (max > mdb->max || elasticity) {
609 if (mdb->old) {
610 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000611 br_info(br, "Multicast hash table "
612 "on fire: %s\n",
613 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000614 err = -EEXIST;
615 goto err;
616 }
617
618 err = br_mdb_rehash(&br->mdb, max, elasticity);
619 if (err) {
stephen hemminger28a16c92010-05-10 09:31:09 +0000620 br_warn(br, "Cannot rehash multicast "
621 "hash table, disabling snooping: %s, %d, %d\n",
622 port ? port->dev->name : br->dev->name,
623 mdb->size, err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000624 goto disable;
625 }
626
627 err = -EAGAIN;
628 goto err;
629 }
630
631 return NULL;
632
633err:
634 mp = ERR_PTR(err);
635 return mp;
636}
637
638static struct net_bridge_mdb_entry *br_multicast_new_group(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900639 struct net_bridge *br, struct net_bridge_port *port,
640 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000641{
642 struct net_bridge_mdb_htable *mdb = br->mdb;
643 struct net_bridge_mdb_entry *mp;
644 int hash;
645
646 if (!mdb) {
647 if (br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0))
648 return NULL;
649 goto rehash;
650 }
651
652 hash = br_ip_hash(mdb, group);
653 mp = br_multicast_get_group(br, port, group, hash);
654 switch (PTR_ERR(mp)) {
655 case 0:
656 break;
657
658 case -EAGAIN:
659rehash:
660 mdb = br->mdb;
661 hash = br_ip_hash(mdb, group);
662 break;
663
664 default:
665 goto out;
666 }
667
668 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
669 if (unlikely(!mp))
670 goto out;
671
672 mp->br = br;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900673 mp->addr = *group;
Herbert Xueb1d1642010-02-27 19:41:45 +0000674 setup_timer(&mp->timer, br_multicast_group_expired,
675 (unsigned long)mp);
676 setup_timer(&mp->query_timer, br_multicast_group_query_expired,
677 (unsigned long)mp);
678
679 hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
680 mdb->size++;
681
682out:
683 return mp;
684}
685
686static int br_multicast_add_group(struct net_bridge *br,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900687 struct net_bridge_port *port,
688 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000689{
690 struct net_bridge_mdb_entry *mp;
691 struct net_bridge_port_group *p;
692 struct net_bridge_port_group **pp;
693 unsigned long now = jiffies;
694 int err;
695
Herbert Xueb1d1642010-02-27 19:41:45 +0000696 spin_lock(&br->multicast_lock);
697 if (!netif_running(br->dev) ||
698 (port && port->state == BR_STATE_DISABLED))
699 goto out;
700
701 mp = br_multicast_new_group(br, port, group);
702 err = PTR_ERR(mp);
703 if (unlikely(IS_ERR(mp) || !mp))
704 goto err;
705
706 if (!port) {
707 hlist_add_head(&mp->mglist, &br->mglist);
708 mod_timer(&mp->timer, now + br->multicast_membership_interval);
709 goto out;
710 }
711
712 for (pp = &mp->ports; (p = *pp); pp = &p->next) {
713 if (p->port == port)
714 goto found;
715 if ((unsigned long)p->port < (unsigned long)port)
716 break;
717 }
718
719 p = kzalloc(sizeof(*p), GFP_ATOMIC);
720 err = -ENOMEM;
721 if (unlikely(!p))
722 goto err;
723
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900724 p->addr = *group;
Herbert Xueb1d1642010-02-27 19:41:45 +0000725 p->port = port;
726 p->next = *pp;
727 hlist_add_head(&p->mglist, &port->mglist);
728 setup_timer(&p->timer, br_multicast_port_group_expired,
729 (unsigned long)p);
730 setup_timer(&p->query_timer, br_multicast_port_group_query_expired,
731 (unsigned long)p);
732
733 rcu_assign_pointer(*pp, p);
734
735found:
736 mod_timer(&p->timer, now + br->multicast_membership_interval);
737out:
738 err = 0;
739
740err:
741 spin_unlock(&br->multicast_lock);
742 return err;
743}
744
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900745static int br_ip4_multicast_add_group(struct net_bridge *br,
746 struct net_bridge_port *port,
747 __be32 group)
748{
749 struct br_ip br_group;
750
751 if (ipv4_is_local_multicast(group))
752 return 0;
753
754 br_group.u.ip4 = group;
755 br_group.proto = htons(ETH_P_IP);
756
757 return br_multicast_add_group(br, port, &br_group);
758}
759
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900760#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
761static int br_ip6_multicast_add_group(struct net_bridge *br,
762 struct net_bridge_port *port,
763 const struct in6_addr *group)
764{
765 struct br_ip br_group;
766
767 if (ipv6_is_local_multicast(group))
768 return 0;
769
770 ipv6_addr_copy(&br_group.u.ip6, group);
771 br_group.proto = htons(ETH_P_IP);
772
773 return br_multicast_add_group(br, port, &br_group);
774}
775#endif
776
Herbert Xueb1d1642010-02-27 19:41:45 +0000777static void br_multicast_router_expired(unsigned long data)
778{
779 struct net_bridge_port *port = (void *)data;
780 struct net_bridge *br = port->br;
781
782 spin_lock(&br->multicast_lock);
783 if (port->multicast_router != 1 ||
784 timer_pending(&port->multicast_router_timer) ||
785 hlist_unhashed(&port->rlist))
786 goto out;
787
788 hlist_del_init_rcu(&port->rlist);
789
790out:
791 spin_unlock(&br->multicast_lock);
792}
793
794static void br_multicast_local_router_expired(unsigned long data)
795{
796}
797
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900798static void __br_multicast_send_query(struct net_bridge *br,
799 struct net_bridge_port *port,
800 struct br_ip *ip)
Herbert Xueb1d1642010-02-27 19:41:45 +0000801{
Herbert Xueb1d1642010-02-27 19:41:45 +0000802 struct sk_buff *skb;
803
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900804 skb = br_multicast_alloc_query(br, ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000805 if (!skb)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900806 return;
Herbert Xueb1d1642010-02-27 19:41:45 +0000807
808 if (port) {
809 __skb_push(skb, sizeof(struct ethhdr));
810 skb->dev = port->dev;
Jan Engelhardt713aefa2010-03-23 04:07:21 +0100811 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
Herbert Xueb1d1642010-02-27 19:41:45 +0000812 dev_queue_xmit);
813 } else
814 netif_rx(skb);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900815}
Herbert Xueb1d1642010-02-27 19:41:45 +0000816
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900817static void br_multicast_send_query(struct net_bridge *br,
818 struct net_bridge_port *port, u32 sent)
819{
820 unsigned long time;
821 struct br_ip br_group;
822
823 if (!netif_running(br->dev) || br->multicast_disabled ||
824 timer_pending(&br->multicast_querier_timer))
825 return;
826
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900827 memset(&br_group.u, 0, sizeof(br_group.u));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900828
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900829 br_group.proto = htons(ETH_P_IP);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900830 __br_multicast_send_query(br, port, &br_group);
831
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900832#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
833 br_group.proto = htons(ETH_P_IPV6);
834 __br_multicast_send_query(br, port, &br_group);
835#endif
836
Herbert Xueb1d1642010-02-27 19:41:45 +0000837 time = jiffies;
838 time += sent < br->multicast_startup_query_count ?
839 br->multicast_startup_query_interval :
840 br->multicast_query_interval;
841 mod_timer(port ? &port->multicast_query_timer :
842 &br->multicast_query_timer, time);
843}
844
845static void br_multicast_port_query_expired(unsigned long data)
846{
847 struct net_bridge_port *port = (void *)data;
848 struct net_bridge *br = port->br;
849
850 spin_lock(&br->multicast_lock);
Dan Carpenter02a780c2010-03-06 01:14:09 +0000851 if (port->state == BR_STATE_DISABLED ||
852 port->state == BR_STATE_BLOCKING)
Herbert Xueb1d1642010-02-27 19:41:45 +0000853 goto out;
854
855 if (port->multicast_startup_queries_sent <
856 br->multicast_startup_query_count)
857 port->multicast_startup_queries_sent++;
858
859 br_multicast_send_query(port->br, port,
860 port->multicast_startup_queries_sent);
861
862out:
863 spin_unlock(&br->multicast_lock);
864}
865
866void br_multicast_add_port(struct net_bridge_port *port)
867{
868 port->multicast_router = 1;
869
870 setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
871 (unsigned long)port);
872 setup_timer(&port->multicast_query_timer,
873 br_multicast_port_query_expired, (unsigned long)port);
874}
875
876void br_multicast_del_port(struct net_bridge_port *port)
877{
878 del_timer_sync(&port->multicast_router_timer);
879}
880
Herbert Xu561f1102010-02-27 19:41:50 +0000881static void __br_multicast_enable_port(struct net_bridge_port *port)
882{
883 port->multicast_startup_queries_sent = 0;
884
885 if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 ||
886 del_timer(&port->multicast_query_timer))
887 mod_timer(&port->multicast_query_timer, jiffies);
888}
889
Herbert Xueb1d1642010-02-27 19:41:45 +0000890void br_multicast_enable_port(struct net_bridge_port *port)
891{
892 struct net_bridge *br = port->br;
893
894 spin_lock(&br->multicast_lock);
895 if (br->multicast_disabled || !netif_running(br->dev))
896 goto out;
897
Herbert Xu561f1102010-02-27 19:41:50 +0000898 __br_multicast_enable_port(port);
Herbert Xueb1d1642010-02-27 19:41:45 +0000899
900out:
901 spin_unlock(&br->multicast_lock);
902}
903
904void br_multicast_disable_port(struct net_bridge_port *port)
905{
906 struct net_bridge *br = port->br;
907 struct net_bridge_port_group *pg;
908 struct hlist_node *p, *n;
909
910 spin_lock(&br->multicast_lock);
911 hlist_for_each_entry_safe(pg, p, n, &port->mglist, mglist)
912 br_multicast_del_pg(br, pg);
913
914 if (!hlist_unhashed(&port->rlist))
915 hlist_del_init_rcu(&port->rlist);
916 del_timer(&port->multicast_router_timer);
917 del_timer(&port->multicast_query_timer);
918 spin_unlock(&br->multicast_lock);
919}
920
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900921static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
922 struct net_bridge_port *port,
923 struct sk_buff *skb)
Herbert Xueb1d1642010-02-27 19:41:45 +0000924{
925 struct igmpv3_report *ih;
926 struct igmpv3_grec *grec;
927 int i;
928 int len;
929 int num;
930 int type;
931 int err = 0;
932 __be32 group;
933
934 if (!pskb_may_pull(skb, sizeof(*ih)))
935 return -EINVAL;
936
937 ih = igmpv3_report_hdr(skb);
938 num = ntohs(ih->ngrec);
939 len = sizeof(*ih);
940
941 for (i = 0; i < num; i++) {
942 len += sizeof(*grec);
943 if (!pskb_may_pull(skb, len))
944 return -EINVAL;
945
Herbert Xufd218cf2010-04-07 21:20:47 -0700946 grec = (void *)(skb->data + len - sizeof(*grec));
Herbert Xueb1d1642010-02-27 19:41:45 +0000947 group = grec->grec_mca;
948 type = grec->grec_type;
949
Eric Dumazet8eabf952010-04-20 03:20:05 +0000950 len += ntohs(grec->grec_nsrcs) * 4;
Herbert Xueb1d1642010-02-27 19:41:45 +0000951 if (!pskb_may_pull(skb, len))
952 return -EINVAL;
953
954 /* We treat this as an IGMPv2 report for now. */
955 switch (type) {
956 case IGMPV3_MODE_IS_INCLUDE:
957 case IGMPV3_MODE_IS_EXCLUDE:
958 case IGMPV3_CHANGE_TO_INCLUDE:
959 case IGMPV3_CHANGE_TO_EXCLUDE:
960 case IGMPV3_ALLOW_NEW_SOURCES:
961 case IGMPV3_BLOCK_OLD_SOURCES:
962 break;
963
964 default:
965 continue;
966 }
967
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900968 err = br_ip4_multicast_add_group(br, port, group);
Herbert Xueb1d1642010-02-27 19:41:45 +0000969 if (err)
970 break;
971 }
972
973 return err;
974}
975
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900976#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
977static int br_ip6_multicast_mld2_report(struct net_bridge *br,
978 struct net_bridge_port *port,
979 struct sk_buff *skb)
980{
981 struct icmp6hdr *icmp6h;
982 struct mld2_grec *grec;
983 int i;
984 int len;
985 int num;
986 int err = 0;
987
988 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
989 return -EINVAL;
990
991 icmp6h = icmp6_hdr(skb);
992 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
993 len = sizeof(*icmp6h);
994
995 for (i = 0; i < num; i++) {
996 __be16 *nsrcs, _nsrcs;
997
998 nsrcs = skb_header_pointer(skb,
999 len + offsetof(struct mld2_grec,
1000 grec_mca),
1001 sizeof(_nsrcs), &_nsrcs);
1002 if (!nsrcs)
1003 return -EINVAL;
1004
1005 if (!pskb_may_pull(skb,
1006 len + sizeof(*grec) +
1007 sizeof(struct in6_addr) * (*nsrcs)))
1008 return -EINVAL;
1009
1010 grec = (struct mld2_grec *)(skb->data + len);
1011 len += sizeof(*grec) + sizeof(struct in6_addr) * (*nsrcs);
1012
1013 /* We treat these as MLDv1 reports for now. */
1014 switch (grec->grec_type) {
1015 case MLD2_MODE_IS_INCLUDE:
1016 case MLD2_MODE_IS_EXCLUDE:
1017 case MLD2_CHANGE_TO_INCLUDE:
1018 case MLD2_CHANGE_TO_EXCLUDE:
1019 case MLD2_ALLOW_NEW_SOURCES:
1020 case MLD2_BLOCK_OLD_SOURCES:
1021 break;
1022
1023 default:
1024 continue;
1025 }
1026
1027 err = br_ip6_multicast_add_group(br, port, &grec->grec_mca);
1028 if (!err)
1029 break;
1030 }
1031
1032 return err;
1033}
1034#endif
1035
stephen hemminger7e80c122010-04-27 15:01:04 +00001036/*
1037 * Add port to rotuer_list
1038 * list is maintained ordered by pointer value
1039 * and locked by br->multicast_lock and RCU
1040 */
Herbert Xu0909e112010-02-27 19:41:49 +00001041static void br_multicast_add_router(struct net_bridge *br,
1042 struct net_bridge_port *port)
1043{
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001044 struct net_bridge_port *p;
stephen hemminger7e80c122010-04-27 15:01:04 +00001045 struct hlist_node *n, *slot = NULL;
Herbert Xu0909e112010-02-27 19:41:49 +00001046
David S. Miller709b9322010-04-27 16:49:58 -07001047 hlist_for_each_entry(p, n, &br->router_list, rlist) {
stephen hemminger7e80c122010-04-27 15:01:04 +00001048 if ((unsigned long) port >= (unsigned long) p)
1049 break;
1050 slot = n;
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001051 }
Herbert Xu0909e112010-02-27 19:41:49 +00001052
stephen hemminger7e80c122010-04-27 15:01:04 +00001053 if (slot)
1054 hlist_add_after_rcu(slot, &port->rlist);
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001055 else
1056 hlist_add_head_rcu(&port->rlist, &br->router_list);
Herbert Xu0909e112010-02-27 19:41:49 +00001057}
1058
Herbert Xueb1d1642010-02-27 19:41:45 +00001059static void br_multicast_mark_router(struct net_bridge *br,
1060 struct net_bridge_port *port)
1061{
1062 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +00001063
1064 if (!port) {
1065 if (br->multicast_router == 1)
1066 mod_timer(&br->multicast_router_timer,
1067 now + br->multicast_querier_interval);
1068 return;
1069 }
1070
1071 if (port->multicast_router != 1)
1072 return;
1073
1074 if (!hlist_unhashed(&port->rlist))
1075 goto timer;
1076
Herbert Xu0909e112010-02-27 19:41:49 +00001077 br_multicast_add_router(br, port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001078
1079timer:
1080 mod_timer(&port->multicast_router_timer,
1081 now + br->multicast_querier_interval);
1082}
1083
1084static void br_multicast_query_received(struct net_bridge *br,
1085 struct net_bridge_port *port,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001086 int saddr)
Herbert Xueb1d1642010-02-27 19:41:45 +00001087{
1088 if (saddr)
1089 mod_timer(&br->multicast_querier_timer,
1090 jiffies + br->multicast_querier_interval);
1091 else if (timer_pending(&br->multicast_querier_timer))
1092 return;
1093
1094 br_multicast_mark_router(br, port);
1095}
1096
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001097static int br_ip4_multicast_query(struct net_bridge *br,
1098 struct net_bridge_port *port,
1099 struct sk_buff *skb)
Herbert Xueb1d1642010-02-27 19:41:45 +00001100{
1101 struct iphdr *iph = ip_hdr(skb);
1102 struct igmphdr *ih = igmp_hdr(skb);
1103 struct net_bridge_mdb_entry *mp;
1104 struct igmpv3_query *ih3;
1105 struct net_bridge_port_group *p;
1106 struct net_bridge_port_group **pp;
1107 unsigned long max_delay;
1108 unsigned long now = jiffies;
1109 __be32 group;
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001110 int err = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001111
1112 spin_lock(&br->multicast_lock);
1113 if (!netif_running(br->dev) ||
1114 (port && port->state == BR_STATE_DISABLED))
1115 goto out;
1116
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001117 br_multicast_query_received(br, port, !!iph->saddr);
Herbert Xueb1d1642010-02-27 19:41:45 +00001118
1119 group = ih->group;
1120
1121 if (skb->len == sizeof(*ih)) {
1122 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1123
1124 if (!max_delay) {
1125 max_delay = 10 * HZ;
1126 group = 0;
1127 }
1128 } else {
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001129 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) {
1130 err = -EINVAL;
1131 goto out;
1132 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001133
1134 ih3 = igmpv3_query_hdr(skb);
1135 if (ih3->nsrcs)
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001136 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001137
YOSHIFUJI Hideaki / 吉藤英明0ba8c9e2010-03-15 19:27:00 +00001138 max_delay = ih3->code ?
1139 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
Herbert Xueb1d1642010-02-27 19:41:45 +00001140 }
1141
1142 if (!group)
1143 goto out;
1144
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001145 mp = br_mdb_ip4_get(br->mdb, group);
Herbert Xueb1d1642010-02-27 19:41:45 +00001146 if (!mp)
1147 goto out;
1148
1149 max_delay *= br->multicast_last_member_count;
1150
1151 if (!hlist_unhashed(&mp->mglist) &&
1152 (timer_pending(&mp->timer) ?
1153 time_after(mp->timer.expires, now + max_delay) :
1154 try_to_del_timer_sync(&mp->timer) >= 0))
1155 mod_timer(&mp->timer, now + max_delay);
1156
1157 for (pp = &mp->ports; (p = *pp); pp = &p->next) {
1158 if (timer_pending(&p->timer) ?
1159 time_after(p->timer.expires, now + max_delay) :
1160 try_to_del_timer_sync(&p->timer) >= 0)
1161 mod_timer(&mp->timer, now + max_delay);
1162 }
1163
1164out:
1165 spin_unlock(&br->multicast_lock);
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001166 return err;
Herbert Xueb1d1642010-02-27 19:41:45 +00001167}
1168
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001169#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1170static int br_ip6_multicast_query(struct net_bridge *br,
1171 struct net_bridge_port *port,
1172 struct sk_buff *skb)
1173{
1174 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1175 struct mld_msg *mld = (struct mld_msg *) icmp6_hdr(skb);
1176 struct net_bridge_mdb_entry *mp;
1177 struct mld2_query *mld2q;
1178 struct net_bridge_port_group *p, **pp;
1179 unsigned long max_delay;
1180 unsigned long now = jiffies;
1181 struct in6_addr *group = NULL;
1182 int err = 0;
1183
1184 spin_lock(&br->multicast_lock);
1185 if (!netif_running(br->dev) ||
1186 (port && port->state == BR_STATE_DISABLED))
1187 goto out;
1188
1189 br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->saddr));
1190
1191 if (skb->len == sizeof(*mld)) {
1192 if (!pskb_may_pull(skb, sizeof(*mld))) {
1193 err = -EINVAL;
1194 goto out;
1195 }
1196 mld = (struct mld_msg *) icmp6_hdr(skb);
1197 max_delay = msecs_to_jiffies(htons(mld->mld_maxdelay));
1198 if (max_delay)
1199 group = &mld->mld_mca;
1200 } else if (skb->len >= sizeof(*mld2q)) {
1201 if (!pskb_may_pull(skb, sizeof(*mld2q))) {
1202 err = -EINVAL;
1203 goto out;
1204 }
1205 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1206 if (!mld2q->mld2q_nsrcs)
1207 group = &mld2q->mld2q_mca;
1208 max_delay = mld2q->mld2q_mrc ? MLDV2_MRC(mld2q->mld2q_mrc) : 1;
1209 }
1210
1211 if (!group)
1212 goto out;
1213
1214 mp = br_mdb_ip6_get(br->mdb, group);
1215 if (!mp)
1216 goto out;
1217
1218 max_delay *= br->multicast_last_member_count;
1219 if (!hlist_unhashed(&mp->mglist) &&
1220 (timer_pending(&mp->timer) ?
1221 time_after(mp->timer.expires, now + max_delay) :
1222 try_to_del_timer_sync(&mp->timer) >= 0))
1223 mod_timer(&mp->timer, now + max_delay);
1224
1225 for (pp = &mp->ports; (p = *pp); pp = &p->next) {
1226 if (timer_pending(&p->timer) ?
1227 time_after(p->timer.expires, now + max_delay) :
1228 try_to_del_timer_sync(&p->timer) >= 0)
1229 mod_timer(&mp->timer, now + max_delay);
1230 }
1231
1232out:
1233 spin_unlock(&br->multicast_lock);
1234 return err;
1235}
1236#endif
1237
Herbert Xueb1d1642010-02-27 19:41:45 +00001238static void br_multicast_leave_group(struct net_bridge *br,
1239 struct net_bridge_port *port,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001240 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +00001241{
1242 struct net_bridge_mdb_htable *mdb;
1243 struct net_bridge_mdb_entry *mp;
1244 struct net_bridge_port_group *p;
1245 unsigned long now;
1246 unsigned long time;
1247
Herbert Xueb1d1642010-02-27 19:41:45 +00001248 spin_lock(&br->multicast_lock);
1249 if (!netif_running(br->dev) ||
1250 (port && port->state == BR_STATE_DISABLED) ||
1251 timer_pending(&br->multicast_querier_timer))
1252 goto out;
1253
1254 mdb = br->mdb;
1255 mp = br_mdb_ip_get(mdb, group);
1256 if (!mp)
1257 goto out;
1258
1259 now = jiffies;
1260 time = now + br->multicast_last_member_count *
1261 br->multicast_last_member_interval;
1262
1263 if (!port) {
1264 if (!hlist_unhashed(&mp->mglist) &&
1265 (timer_pending(&mp->timer) ?
1266 time_after(mp->timer.expires, time) :
1267 try_to_del_timer_sync(&mp->timer) >= 0)) {
1268 mod_timer(&mp->timer, time);
1269
1270 mp->queries_sent = 0;
1271 mod_timer(&mp->query_timer, now);
1272 }
1273
1274 goto out;
1275 }
1276
1277 for (p = mp->ports; p; p = p->next) {
1278 if (p->port != port)
1279 continue;
1280
1281 if (!hlist_unhashed(&p->mglist) &&
1282 (timer_pending(&p->timer) ?
1283 time_after(p->timer.expires, time) :
1284 try_to_del_timer_sync(&p->timer) >= 0)) {
1285 mod_timer(&p->timer, time);
1286
1287 p->queries_sent = 0;
1288 mod_timer(&p->query_timer, now);
1289 }
1290
1291 break;
1292 }
1293
1294out:
1295 spin_unlock(&br->multicast_lock);
1296}
1297
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001298static void br_ip4_multicast_leave_group(struct net_bridge *br,
1299 struct net_bridge_port *port,
1300 __be32 group)
1301{
1302 struct br_ip br_group;
1303
1304 if (ipv4_is_local_multicast(group))
1305 return;
1306
1307 br_group.u.ip4 = group;
1308 br_group.proto = htons(ETH_P_IP);
1309
1310 br_multicast_leave_group(br, port, &br_group);
1311}
1312
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001313#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1314static void br_ip6_multicast_leave_group(struct net_bridge *br,
1315 struct net_bridge_port *port,
1316 const struct in6_addr *group)
1317{
1318 struct br_ip br_group;
1319
1320 if (ipv6_is_local_multicast(group))
1321 return;
1322
1323 ipv6_addr_copy(&br_group.u.ip6, group);
1324 br_group.proto = htons(ETH_P_IPV6);
1325
1326 br_multicast_leave_group(br, port, &br_group);
1327}
1328#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001329
Herbert Xueb1d1642010-02-27 19:41:45 +00001330static int br_multicast_ipv4_rcv(struct net_bridge *br,
1331 struct net_bridge_port *port,
1332 struct sk_buff *skb)
1333{
1334 struct sk_buff *skb2 = skb;
1335 struct iphdr *iph;
1336 struct igmphdr *ih;
1337 unsigned len;
1338 unsigned offset;
1339 int err;
1340
Herbert Xueb1d1642010-02-27 19:41:45 +00001341 /* We treat OOM as packet loss for now. */
1342 if (!pskb_may_pull(skb, sizeof(*iph)))
1343 return -EINVAL;
1344
1345 iph = ip_hdr(skb);
1346
1347 if (iph->ihl < 5 || iph->version != 4)
1348 return -EINVAL;
1349
1350 if (!pskb_may_pull(skb, ip_hdrlen(skb)))
1351 return -EINVAL;
1352
1353 iph = ip_hdr(skb);
1354
1355 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1356 return -EINVAL;
1357
1358 if (iph->protocol != IPPROTO_IGMP)
1359 return 0;
1360
1361 len = ntohs(iph->tot_len);
1362 if (skb->len < len || len < ip_hdrlen(skb))
1363 return -EINVAL;
1364
1365 if (skb->len > len) {
1366 skb2 = skb_clone(skb, GFP_ATOMIC);
1367 if (!skb2)
1368 return -ENOMEM;
1369
1370 err = pskb_trim_rcsum(skb2, len);
1371 if (err)
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001372 goto err_out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001373 }
1374
1375 len -= ip_hdrlen(skb2);
1376 offset = skb_network_offset(skb2) + ip_hdrlen(skb2);
1377 __skb_pull(skb2, offset);
1378 skb_reset_transport_header(skb2);
1379
1380 err = -EINVAL;
1381 if (!pskb_may_pull(skb2, sizeof(*ih)))
1382 goto out;
1383
Herbert Xueb1d1642010-02-27 19:41:45 +00001384 switch (skb2->ip_summed) {
1385 case CHECKSUM_COMPLETE:
1386 if (!csum_fold(skb2->csum))
1387 break;
1388 /* fall through */
1389 case CHECKSUM_NONE:
1390 skb2->csum = 0;
1391 if (skb_checksum_complete(skb2))
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001392 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001393 }
1394
1395 err = 0;
1396
1397 BR_INPUT_SKB_CB(skb)->igmp = 1;
1398 ih = igmp_hdr(skb2);
1399
1400 switch (ih->type) {
1401 case IGMP_HOST_MEMBERSHIP_REPORT:
1402 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1403 BR_INPUT_SKB_CB(skb2)->mrouters_only = 1;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001404 err = br_ip4_multicast_add_group(br, port, ih->group);
Herbert Xueb1d1642010-02-27 19:41:45 +00001405 break;
1406 case IGMPV3_HOST_MEMBERSHIP_REPORT:
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001407 err = br_ip4_multicast_igmp3_report(br, port, skb2);
Herbert Xueb1d1642010-02-27 19:41:45 +00001408 break;
1409 case IGMP_HOST_MEMBERSHIP_QUERY:
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001410 err = br_ip4_multicast_query(br, port, skb2);
Herbert Xueb1d1642010-02-27 19:41:45 +00001411 break;
1412 case IGMP_HOST_LEAVE_MESSAGE:
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001413 br_ip4_multicast_leave_group(br, port, ih->group);
Herbert Xueb1d1642010-02-27 19:41:45 +00001414 break;
1415 }
1416
1417out:
1418 __skb_push(skb2, offset);
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001419err_out:
Herbert Xueb1d1642010-02-27 19:41:45 +00001420 if (skb2 != skb)
1421 kfree_skb(skb2);
1422 return err;
1423}
1424
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001425#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1426static int br_multicast_ipv6_rcv(struct net_bridge *br,
1427 struct net_bridge_port *port,
1428 struct sk_buff *skb)
1429{
1430 struct sk_buff *skb2 = skb;
1431 struct ipv6hdr *ip6h;
1432 struct icmp6hdr *icmp6h;
1433 u8 nexthdr;
1434 unsigned len;
1435 unsigned offset;
1436 int err;
1437
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001438 if (!pskb_may_pull(skb, sizeof(*ip6h)))
1439 return -EINVAL;
1440
1441 ip6h = ipv6_hdr(skb);
1442
1443 /*
1444 * We're interested in MLD messages only.
1445 * - Version is 6
1446 * - MLD has always Router Alert hop-by-hop option
1447 * - But we do not support jumbrograms.
1448 */
1449 if (ip6h->version != 6 ||
1450 ip6h->nexthdr != IPPROTO_HOPOPTS ||
1451 ip6h->payload_len == 0)
1452 return 0;
1453
1454 len = ntohs(ip6h->payload_len);
1455 if (skb->len < len)
1456 return -EINVAL;
1457
1458 nexthdr = ip6h->nexthdr;
1459 offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr);
1460
1461 if (offset < 0 || nexthdr != IPPROTO_ICMPV6)
1462 return 0;
1463
1464 /* Okay, we found ICMPv6 header */
1465 skb2 = skb_clone(skb, GFP_ATOMIC);
1466 if (!skb2)
1467 return -ENOMEM;
1468
1469 len -= offset - skb_network_offset(skb2);
1470
1471 __skb_pull(skb2, offset);
1472 skb_reset_transport_header(skb2);
1473
1474 err = -EINVAL;
1475 if (!pskb_may_pull(skb2, sizeof(*icmp6h)))
1476 goto out;
1477
1478 icmp6h = icmp6_hdr(skb2);
1479
1480 switch (icmp6h->icmp6_type) {
1481 case ICMPV6_MGM_QUERY:
1482 case ICMPV6_MGM_REPORT:
1483 case ICMPV6_MGM_REDUCTION:
1484 case ICMPV6_MLD2_REPORT:
1485 break;
1486 default:
1487 err = 0;
1488 goto out;
1489 }
1490
1491 /* Okay, we found MLD message. Check further. */
1492 if (skb2->len > len) {
1493 err = pskb_trim_rcsum(skb2, len);
1494 if (err)
1495 goto out;
1496 }
1497
1498 switch (skb2->ip_summed) {
1499 case CHECKSUM_COMPLETE:
1500 if (!csum_fold(skb2->csum))
1501 break;
1502 /*FALLTHROUGH*/
1503 case CHECKSUM_NONE:
1504 skb2->csum = 0;
1505 if (skb_checksum_complete(skb2))
1506 goto out;
1507 }
1508
1509 err = 0;
1510
1511 BR_INPUT_SKB_CB(skb)->igmp = 1;
1512
1513 switch (icmp6h->icmp6_type) {
1514 case ICMPV6_MGM_REPORT:
1515 {
1516 struct mld_msg *mld = (struct mld_msg *)icmp6h;
1517 BR_INPUT_SKB_CB(skb2)->mrouters_only = 1;
1518 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca);
1519 break;
1520 }
1521 case ICMPV6_MLD2_REPORT:
1522 err = br_ip6_multicast_mld2_report(br, port, skb2);
1523 break;
1524 case ICMPV6_MGM_QUERY:
1525 err = br_ip6_multicast_query(br, port, skb2);
1526 break;
1527 case ICMPV6_MGM_REDUCTION:
1528 {
1529 struct mld_msg *mld = (struct mld_msg *)icmp6h;
1530 br_ip6_multicast_leave_group(br, port, &mld->mld_mca);
1531 }
1532 }
1533
1534out:
1535 __skb_push(skb2, offset);
1536 if (skb2 != skb)
1537 kfree_skb(skb2);
1538 return err;
1539}
1540#endif
1541
Herbert Xueb1d1642010-02-27 19:41:45 +00001542int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1543 struct sk_buff *skb)
1544{
YOSHIFUJI Hideaki / 吉藤英明1fafc7a2010-04-25 08:06:40 +00001545 BR_INPUT_SKB_CB(skb)->igmp = 0;
1546 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1547
Herbert Xueb1d1642010-02-27 19:41:45 +00001548 if (br->multicast_disabled)
1549 return 0;
1550
1551 switch (skb->protocol) {
1552 case htons(ETH_P_IP):
1553 return br_multicast_ipv4_rcv(br, port, skb);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001554#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1555 case htons(ETH_P_IPV6):
1556 return br_multicast_ipv6_rcv(br, port, skb);
1557#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001558 }
1559
1560 return 0;
1561}
1562
1563static void br_multicast_query_expired(unsigned long data)
1564{
1565 struct net_bridge *br = (void *)data;
1566
1567 spin_lock(&br->multicast_lock);
1568 if (br->multicast_startup_queries_sent <
1569 br->multicast_startup_query_count)
1570 br->multicast_startup_queries_sent++;
1571
1572 br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent);
1573
1574 spin_unlock(&br->multicast_lock);
1575}
1576
1577void br_multicast_init(struct net_bridge *br)
1578{
1579 br->hash_elasticity = 4;
1580 br->hash_max = 512;
1581
1582 br->multicast_router = 1;
1583 br->multicast_last_member_count = 2;
1584 br->multicast_startup_query_count = 2;
1585
1586 br->multicast_last_member_interval = HZ;
1587 br->multicast_query_response_interval = 10 * HZ;
1588 br->multicast_startup_query_interval = 125 * HZ / 4;
1589 br->multicast_query_interval = 125 * HZ;
1590 br->multicast_querier_interval = 255 * HZ;
1591 br->multicast_membership_interval = 260 * HZ;
1592
1593 spin_lock_init(&br->multicast_lock);
1594 setup_timer(&br->multicast_router_timer,
1595 br_multicast_local_router_expired, 0);
1596 setup_timer(&br->multicast_querier_timer,
1597 br_multicast_local_router_expired, 0);
1598 setup_timer(&br->multicast_query_timer, br_multicast_query_expired,
1599 (unsigned long)br);
1600}
1601
1602void br_multicast_open(struct net_bridge *br)
1603{
1604 br->multicast_startup_queries_sent = 0;
1605
1606 if (br->multicast_disabled)
1607 return;
1608
1609 mod_timer(&br->multicast_query_timer, jiffies);
1610}
1611
1612void br_multicast_stop(struct net_bridge *br)
1613{
1614 struct net_bridge_mdb_htable *mdb;
1615 struct net_bridge_mdb_entry *mp;
1616 struct hlist_node *p, *n;
1617 u32 ver;
1618 int i;
1619
1620 del_timer_sync(&br->multicast_router_timer);
1621 del_timer_sync(&br->multicast_querier_timer);
1622 del_timer_sync(&br->multicast_query_timer);
1623
1624 spin_lock_bh(&br->multicast_lock);
1625 mdb = br->mdb;
1626 if (!mdb)
1627 goto out;
1628
1629 br->mdb = NULL;
1630
1631 ver = mdb->ver;
1632 for (i = 0; i < mdb->max; i++) {
1633 hlist_for_each_entry_safe(mp, p, n, &mdb->mhash[i],
1634 hlist[ver]) {
1635 del_timer(&mp->timer);
1636 del_timer(&mp->query_timer);
1637 call_rcu_bh(&mp->rcu, br_multicast_free_group);
1638 }
1639 }
1640
1641 if (mdb->old) {
1642 spin_unlock_bh(&br->multicast_lock);
Herbert Xu10cc2b52010-03-05 21:03:35 +00001643 rcu_barrier_bh();
Herbert Xueb1d1642010-02-27 19:41:45 +00001644 spin_lock_bh(&br->multicast_lock);
1645 WARN_ON(mdb->old);
1646 }
1647
1648 mdb->old = mdb;
1649 call_rcu_bh(&mdb->rcu, br_mdb_free);
1650
1651out:
1652 spin_unlock_bh(&br->multicast_lock);
1653}
Herbert Xu0909e112010-02-27 19:41:49 +00001654
1655int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1656{
1657 int err = -ENOENT;
1658
1659 spin_lock_bh(&br->multicast_lock);
1660 if (!netif_running(br->dev))
1661 goto unlock;
1662
1663 switch (val) {
1664 case 0:
1665 case 2:
1666 del_timer(&br->multicast_router_timer);
1667 /* fall through */
1668 case 1:
1669 br->multicast_router = val;
1670 err = 0;
1671 break;
1672
1673 default:
1674 err = -EINVAL;
1675 break;
1676 }
1677
1678unlock:
1679 spin_unlock_bh(&br->multicast_lock);
1680
1681 return err;
1682}
1683
1684int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1685{
1686 struct net_bridge *br = p->br;
1687 int err = -ENOENT;
1688
1689 spin_lock(&br->multicast_lock);
1690 if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED)
1691 goto unlock;
1692
1693 switch (val) {
1694 case 0:
1695 case 1:
1696 case 2:
1697 p->multicast_router = val;
1698 err = 0;
1699
1700 if (val < 2 && !hlist_unhashed(&p->rlist))
1701 hlist_del_init_rcu(&p->rlist);
1702
1703 if (val == 1)
1704 break;
1705
1706 del_timer(&p->multicast_router_timer);
1707
1708 if (val == 0)
1709 break;
1710
1711 br_multicast_add_router(br, p);
1712 break;
1713
1714 default:
1715 err = -EINVAL;
1716 break;
1717 }
1718
1719unlock:
1720 spin_unlock(&br->multicast_lock);
1721
1722 return err;
1723}
Herbert Xu561f1102010-02-27 19:41:50 +00001724
1725int br_multicast_toggle(struct net_bridge *br, unsigned long val)
1726{
1727 struct net_bridge_port *port;
1728 int err = -ENOENT;
1729
1730 spin_lock(&br->multicast_lock);
1731 if (!netif_running(br->dev))
1732 goto unlock;
1733
1734 err = 0;
1735 if (br->multicast_disabled == !val)
1736 goto unlock;
1737
1738 br->multicast_disabled = !val;
1739 if (br->multicast_disabled)
1740 goto unlock;
1741
1742 if (br->mdb) {
1743 if (br->mdb->old) {
1744 err = -EEXIST;
1745rollback:
1746 br->multicast_disabled = !!val;
1747 goto unlock;
1748 }
1749
1750 err = br_mdb_rehash(&br->mdb, br->mdb->max,
1751 br->hash_elasticity);
1752 if (err)
1753 goto rollback;
1754 }
1755
1756 br_multicast_open(br);
1757 list_for_each_entry(port, &br->port_list, list) {
1758 if (port->state == BR_STATE_DISABLED ||
1759 port->state == BR_STATE_BLOCKING)
1760 continue;
1761
1762 __br_multicast_enable_port(port);
1763 }
1764
1765unlock:
1766 spin_unlock(&br->multicast_lock);
1767
1768 return err;
1769}
Herbert Xub1951672010-02-27 19:41:51 +00001770
1771int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
1772{
1773 int err = -ENOENT;
1774 u32 old;
1775
1776 spin_lock(&br->multicast_lock);
1777 if (!netif_running(br->dev))
1778 goto unlock;
1779
1780 err = -EINVAL;
1781 if (!is_power_of_2(val))
1782 goto unlock;
1783 if (br->mdb && val < br->mdb->size)
1784 goto unlock;
1785
1786 err = 0;
1787
1788 old = br->hash_max;
1789 br->hash_max = val;
1790
1791 if (br->mdb) {
1792 if (br->mdb->old) {
1793 err = -EEXIST;
1794rollback:
1795 br->hash_max = old;
1796 goto unlock;
1797 }
1798
1799 err = br_mdb_rehash(&br->mdb, br->hash_max,
1800 br->hash_elasticity);
1801 if (err)
1802 goto rollback;
1803 }
1804
1805unlock:
1806 spin_unlock(&br->multicast_lock);
1807
1808 return err;
1809}