blob: 7128abdce45f1a60546ea970505e9b772af04cc5 [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)
38{
39 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;
86}
87
Herbert Xueb1d1642010-02-27 19:41:45 +000088static 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())
588 printk(KERN_INFO "%s: Multicast hash table "
589 "chain limit reached: %s\n",
590 br->dev->name, port ? port->dev->name :
591 br->dev->name);
592
593 elasticity = br->hash_elasticity;
594 }
595
596 if (mdb->size >= max) {
597 max *= 2;
598 if (unlikely(max >= br->hash_max)) {
599 printk(KERN_WARNING "%s: Multicast hash table maximum "
600 "reached, disabling snooping: %s, %d\n",
601 br->dev->name, port ? port->dev->name :
602 br->dev->name,
603 max);
604 err = -E2BIG;
605disable:
606 br->multicast_disabled = 1;
607 goto err;
608 }
609 }
610
611 if (max > mdb->max || elasticity) {
612 if (mdb->old) {
613 if (net_ratelimit())
614 printk(KERN_INFO "%s: Multicast hash table "
615 "on fire: %s\n",
616 br->dev->name, port ? port->dev->name :
617 br->dev->name);
618 err = -EEXIST;
619 goto err;
620 }
621
622 err = br_mdb_rehash(&br->mdb, max, elasticity);
623 if (err) {
624 printk(KERN_WARNING "%s: Cannot rehash multicast "
625 "hash table, disabling snooping: "
626 "%s, %d, %d\n",
627 br->dev->name, port ? port->dev->name :
628 br->dev->name,
629 mdb->size, err);
630 goto disable;
631 }
632
633 err = -EAGAIN;
634 goto err;
635 }
636
637 return NULL;
638
639err:
640 mp = ERR_PTR(err);
641 return mp;
642}
643
644static struct net_bridge_mdb_entry *br_multicast_new_group(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900645 struct net_bridge *br, struct net_bridge_port *port,
646 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000647{
648 struct net_bridge_mdb_htable *mdb = br->mdb;
649 struct net_bridge_mdb_entry *mp;
650 int hash;
651
652 if (!mdb) {
653 if (br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0))
654 return NULL;
655 goto rehash;
656 }
657
658 hash = br_ip_hash(mdb, group);
659 mp = br_multicast_get_group(br, port, group, hash);
660 switch (PTR_ERR(mp)) {
661 case 0:
662 break;
663
664 case -EAGAIN:
665rehash:
666 mdb = br->mdb;
667 hash = br_ip_hash(mdb, group);
668 break;
669
670 default:
671 goto out;
672 }
673
674 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
675 if (unlikely(!mp))
676 goto out;
677
678 mp->br = br;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900679 mp->addr = *group;
Herbert Xueb1d1642010-02-27 19:41:45 +0000680 setup_timer(&mp->timer, br_multicast_group_expired,
681 (unsigned long)mp);
682 setup_timer(&mp->query_timer, br_multicast_group_query_expired,
683 (unsigned long)mp);
684
685 hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
686 mdb->size++;
687
688out:
689 return mp;
690}
691
692static int br_multicast_add_group(struct net_bridge *br,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900693 struct net_bridge_port *port,
694 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000695{
696 struct net_bridge_mdb_entry *mp;
697 struct net_bridge_port_group *p;
698 struct net_bridge_port_group **pp;
699 unsigned long now = jiffies;
700 int err;
701
Herbert Xueb1d1642010-02-27 19:41:45 +0000702 spin_lock(&br->multicast_lock);
703 if (!netif_running(br->dev) ||
704 (port && port->state == BR_STATE_DISABLED))
705 goto out;
706
707 mp = br_multicast_new_group(br, port, group);
708 err = PTR_ERR(mp);
709 if (unlikely(IS_ERR(mp) || !mp))
710 goto err;
711
712 if (!port) {
713 hlist_add_head(&mp->mglist, &br->mglist);
714 mod_timer(&mp->timer, now + br->multicast_membership_interval);
715 goto out;
716 }
717
718 for (pp = &mp->ports; (p = *pp); pp = &p->next) {
719 if (p->port == port)
720 goto found;
721 if ((unsigned long)p->port < (unsigned long)port)
722 break;
723 }
724
725 p = kzalloc(sizeof(*p), GFP_ATOMIC);
726 err = -ENOMEM;
727 if (unlikely(!p))
728 goto err;
729
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900730 p->addr = *group;
Herbert Xueb1d1642010-02-27 19:41:45 +0000731 p->port = port;
732 p->next = *pp;
733 hlist_add_head(&p->mglist, &port->mglist);
734 setup_timer(&p->timer, br_multicast_port_group_expired,
735 (unsigned long)p);
736 setup_timer(&p->query_timer, br_multicast_port_group_query_expired,
737 (unsigned long)p);
738
739 rcu_assign_pointer(*pp, p);
740
741found:
742 mod_timer(&p->timer, now + br->multicast_membership_interval);
743out:
744 err = 0;
745
746err:
747 spin_unlock(&br->multicast_lock);
748 return err;
749}
750
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900751static int br_ip4_multicast_add_group(struct net_bridge *br,
752 struct net_bridge_port *port,
753 __be32 group)
754{
755 struct br_ip br_group;
756
757 if (ipv4_is_local_multicast(group))
758 return 0;
759
760 br_group.u.ip4 = group;
761 br_group.proto = htons(ETH_P_IP);
762
763 return br_multicast_add_group(br, port, &br_group);
764}
765
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900766#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
767static int br_ip6_multicast_add_group(struct net_bridge *br,
768 struct net_bridge_port *port,
769 const struct in6_addr *group)
770{
771 struct br_ip br_group;
772
773 if (ipv6_is_local_multicast(group))
774 return 0;
775
776 ipv6_addr_copy(&br_group.u.ip6, group);
777 br_group.proto = htons(ETH_P_IP);
778
779 return br_multicast_add_group(br, port, &br_group);
780}
781#endif
782
Herbert Xueb1d1642010-02-27 19:41:45 +0000783static void br_multicast_router_expired(unsigned long data)
784{
785 struct net_bridge_port *port = (void *)data;
786 struct net_bridge *br = port->br;
787
788 spin_lock(&br->multicast_lock);
789 if (port->multicast_router != 1 ||
790 timer_pending(&port->multicast_router_timer) ||
791 hlist_unhashed(&port->rlist))
792 goto out;
793
794 hlist_del_init_rcu(&port->rlist);
795
796out:
797 spin_unlock(&br->multicast_lock);
798}
799
800static void br_multicast_local_router_expired(unsigned long data)
801{
802}
803
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900804static void __br_multicast_send_query(struct net_bridge *br,
805 struct net_bridge_port *port,
806 struct br_ip *ip)
Herbert Xueb1d1642010-02-27 19:41:45 +0000807{
Herbert Xueb1d1642010-02-27 19:41:45 +0000808 struct sk_buff *skb;
809
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900810 skb = br_multicast_alloc_query(br, ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000811 if (!skb)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900812 return;
Herbert Xueb1d1642010-02-27 19:41:45 +0000813
814 if (port) {
815 __skb_push(skb, sizeof(struct ethhdr));
816 skb->dev = port->dev;
817 NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
818 dev_queue_xmit);
819 } else
820 netif_rx(skb);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900821}
Herbert Xueb1d1642010-02-27 19:41:45 +0000822
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900823static void br_multicast_send_query(struct net_bridge *br,
824 struct net_bridge_port *port, u32 sent)
825{
826 unsigned long time;
827 struct br_ip br_group;
828
829 if (!netif_running(br->dev) || br->multicast_disabled ||
830 timer_pending(&br->multicast_querier_timer))
831 return;
832
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900833 memset(&br_group.u, 0, sizeof(br_group.u));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900834
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900835 br_group.proto = htons(ETH_P_IP);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900836 __br_multicast_send_query(br, port, &br_group);
837
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900838#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
839 br_group.proto = htons(ETH_P_IPV6);
840 __br_multicast_send_query(br, port, &br_group);
841#endif
842
Herbert Xueb1d1642010-02-27 19:41:45 +0000843 time = jiffies;
844 time += sent < br->multicast_startup_query_count ?
845 br->multicast_startup_query_interval :
846 br->multicast_query_interval;
847 mod_timer(port ? &port->multicast_query_timer :
848 &br->multicast_query_timer, time);
849}
850
851static void br_multicast_port_query_expired(unsigned long data)
852{
853 struct net_bridge_port *port = (void *)data;
854 struct net_bridge *br = port->br;
855
856 spin_lock(&br->multicast_lock);
Dan Carpenter02a780c2010-03-06 01:14:09 +0000857 if (port->state == BR_STATE_DISABLED ||
858 port->state == BR_STATE_BLOCKING)
Herbert Xueb1d1642010-02-27 19:41:45 +0000859 goto out;
860
861 if (port->multicast_startup_queries_sent <
862 br->multicast_startup_query_count)
863 port->multicast_startup_queries_sent++;
864
865 br_multicast_send_query(port->br, port,
866 port->multicast_startup_queries_sent);
867
868out:
869 spin_unlock(&br->multicast_lock);
870}
871
872void br_multicast_add_port(struct net_bridge_port *port)
873{
874 port->multicast_router = 1;
875
876 setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
877 (unsigned long)port);
878 setup_timer(&port->multicast_query_timer,
879 br_multicast_port_query_expired, (unsigned long)port);
880}
881
882void br_multicast_del_port(struct net_bridge_port *port)
883{
884 del_timer_sync(&port->multicast_router_timer);
885}
886
Herbert Xu561f11032010-02-27 19:41:50 +0000887static void __br_multicast_enable_port(struct net_bridge_port *port)
888{
889 port->multicast_startup_queries_sent = 0;
890
891 if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 ||
892 del_timer(&port->multicast_query_timer))
893 mod_timer(&port->multicast_query_timer, jiffies);
894}
895
Herbert Xueb1d1642010-02-27 19:41:45 +0000896void br_multicast_enable_port(struct net_bridge_port *port)
897{
898 struct net_bridge *br = port->br;
899
900 spin_lock(&br->multicast_lock);
901 if (br->multicast_disabled || !netif_running(br->dev))
902 goto out;
903
Herbert Xu561f11032010-02-27 19:41:50 +0000904 __br_multicast_enable_port(port);
Herbert Xueb1d1642010-02-27 19:41:45 +0000905
906out:
907 spin_unlock(&br->multicast_lock);
908}
909
910void br_multicast_disable_port(struct net_bridge_port *port)
911{
912 struct net_bridge *br = port->br;
913 struct net_bridge_port_group *pg;
914 struct hlist_node *p, *n;
915
916 spin_lock(&br->multicast_lock);
917 hlist_for_each_entry_safe(pg, p, n, &port->mglist, mglist)
918 br_multicast_del_pg(br, pg);
919
920 if (!hlist_unhashed(&port->rlist))
921 hlist_del_init_rcu(&port->rlist);
922 del_timer(&port->multicast_router_timer);
923 del_timer(&port->multicast_query_timer);
924 spin_unlock(&br->multicast_lock);
925}
926
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900927static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
928 struct net_bridge_port *port,
929 struct sk_buff *skb)
Herbert Xueb1d1642010-02-27 19:41:45 +0000930{
931 struct igmpv3_report *ih;
932 struct igmpv3_grec *grec;
933 int i;
934 int len;
935 int num;
936 int type;
937 int err = 0;
938 __be32 group;
939
940 if (!pskb_may_pull(skb, sizeof(*ih)))
941 return -EINVAL;
942
943 ih = igmpv3_report_hdr(skb);
944 num = ntohs(ih->ngrec);
945 len = sizeof(*ih);
946
947 for (i = 0; i < num; i++) {
948 len += sizeof(*grec);
949 if (!pskb_may_pull(skb, len))
950 return -EINVAL;
951
Herbert Xufd218cf2010-04-07 21:20:47 -0700952 grec = (void *)(skb->data + len - sizeof(*grec));
Herbert Xueb1d1642010-02-27 19:41:45 +0000953 group = grec->grec_mca;
954 type = grec->grec_type;
955
Eric Dumazet8eabf952010-04-20 03:20:05 +0000956 len += ntohs(grec->grec_nsrcs) * 4;
Herbert Xueb1d1642010-02-27 19:41:45 +0000957 if (!pskb_may_pull(skb, len))
958 return -EINVAL;
959
960 /* We treat this as an IGMPv2 report for now. */
961 switch (type) {
962 case IGMPV3_MODE_IS_INCLUDE:
963 case IGMPV3_MODE_IS_EXCLUDE:
964 case IGMPV3_CHANGE_TO_INCLUDE:
965 case IGMPV3_CHANGE_TO_EXCLUDE:
966 case IGMPV3_ALLOW_NEW_SOURCES:
967 case IGMPV3_BLOCK_OLD_SOURCES:
968 break;
969
970 default:
971 continue;
972 }
973
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900974 err = br_ip4_multicast_add_group(br, port, group);
Herbert Xueb1d1642010-02-27 19:41:45 +0000975 if (err)
976 break;
977 }
978
979 return err;
980}
981
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900982#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
983static int br_ip6_multicast_mld2_report(struct net_bridge *br,
984 struct net_bridge_port *port,
985 struct sk_buff *skb)
986{
987 struct icmp6hdr *icmp6h;
988 struct mld2_grec *grec;
989 int i;
990 int len;
991 int num;
992 int err = 0;
993
994 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
995 return -EINVAL;
996
997 icmp6h = icmp6_hdr(skb);
998 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
999 len = sizeof(*icmp6h);
1000
1001 for (i = 0; i < num; i++) {
1002 __be16 *nsrcs, _nsrcs;
1003
1004 nsrcs = skb_header_pointer(skb,
1005 len + offsetof(struct mld2_grec,
1006 grec_mca),
1007 sizeof(_nsrcs), &_nsrcs);
1008 if (!nsrcs)
1009 return -EINVAL;
1010
1011 if (!pskb_may_pull(skb,
1012 len + sizeof(*grec) +
1013 sizeof(struct in6_addr) * (*nsrcs)))
1014 return -EINVAL;
1015
1016 grec = (struct mld2_grec *)(skb->data + len);
1017 len += sizeof(*grec) + sizeof(struct in6_addr) * (*nsrcs);
1018
1019 /* We treat these as MLDv1 reports for now. */
1020 switch (grec->grec_type) {
1021 case MLD2_MODE_IS_INCLUDE:
1022 case MLD2_MODE_IS_EXCLUDE:
1023 case MLD2_CHANGE_TO_INCLUDE:
1024 case MLD2_CHANGE_TO_EXCLUDE:
1025 case MLD2_ALLOW_NEW_SOURCES:
1026 case MLD2_BLOCK_OLD_SOURCES:
1027 break;
1028
1029 default:
1030 continue;
1031 }
1032
1033 err = br_ip6_multicast_add_group(br, port, &grec->grec_mca);
1034 if (!err)
1035 break;
1036 }
1037
1038 return err;
1039}
1040#endif
1041
stephen hemminger7e80c122010-04-27 15:01:04 +00001042/*
1043 * Add port to rotuer_list
1044 * list is maintained ordered by pointer value
1045 * and locked by br->multicast_lock and RCU
1046 */
Herbert Xu0909e112010-02-27 19:41:49 +00001047static void br_multicast_add_router(struct net_bridge *br,
1048 struct net_bridge_port *port)
1049{
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001050 struct net_bridge_port *p;
stephen hemminger7e80c122010-04-27 15:01:04 +00001051 struct hlist_node *n, *slot = NULL;
Herbert Xu0909e112010-02-27 19:41:49 +00001052
David S. Miller709b9322010-04-27 16:49:58 -07001053 hlist_for_each_entry(p, n, &br->router_list, rlist) {
stephen hemminger7e80c122010-04-27 15:01:04 +00001054 if ((unsigned long) port >= (unsigned long) p)
1055 break;
1056 slot = n;
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001057 }
Herbert Xu0909e112010-02-27 19:41:49 +00001058
stephen hemminger7e80c122010-04-27 15:01:04 +00001059 if (slot)
1060 hlist_add_after_rcu(slot, &port->rlist);
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001061 else
1062 hlist_add_head_rcu(&port->rlist, &br->router_list);
Herbert Xu0909e112010-02-27 19:41:49 +00001063}
1064
Herbert Xueb1d1642010-02-27 19:41:45 +00001065static void br_multicast_mark_router(struct net_bridge *br,
1066 struct net_bridge_port *port)
1067{
1068 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +00001069
1070 if (!port) {
1071 if (br->multicast_router == 1)
1072 mod_timer(&br->multicast_router_timer,
1073 now + br->multicast_querier_interval);
1074 return;
1075 }
1076
1077 if (port->multicast_router != 1)
1078 return;
1079
1080 if (!hlist_unhashed(&port->rlist))
1081 goto timer;
1082
Herbert Xu0909e112010-02-27 19:41:49 +00001083 br_multicast_add_router(br, port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001084
1085timer:
1086 mod_timer(&port->multicast_router_timer,
1087 now + br->multicast_querier_interval);
1088}
1089
1090static void br_multicast_query_received(struct net_bridge *br,
1091 struct net_bridge_port *port,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001092 int saddr)
Herbert Xueb1d1642010-02-27 19:41:45 +00001093{
1094 if (saddr)
1095 mod_timer(&br->multicast_querier_timer,
1096 jiffies + br->multicast_querier_interval);
1097 else if (timer_pending(&br->multicast_querier_timer))
1098 return;
1099
1100 br_multicast_mark_router(br, port);
1101}
1102
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001103static int br_ip4_multicast_query(struct net_bridge *br,
1104 struct net_bridge_port *port,
1105 struct sk_buff *skb)
Herbert Xueb1d1642010-02-27 19:41:45 +00001106{
1107 struct iphdr *iph = ip_hdr(skb);
1108 struct igmphdr *ih = igmp_hdr(skb);
1109 struct net_bridge_mdb_entry *mp;
1110 struct igmpv3_query *ih3;
1111 struct net_bridge_port_group *p;
1112 struct net_bridge_port_group **pp;
1113 unsigned long max_delay;
1114 unsigned long now = jiffies;
1115 __be32 group;
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001116 int err = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001117
1118 spin_lock(&br->multicast_lock);
1119 if (!netif_running(br->dev) ||
1120 (port && port->state == BR_STATE_DISABLED))
1121 goto out;
1122
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001123 br_multicast_query_received(br, port, !!iph->saddr);
Herbert Xueb1d1642010-02-27 19:41:45 +00001124
1125 group = ih->group;
1126
1127 if (skb->len == sizeof(*ih)) {
1128 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1129
1130 if (!max_delay) {
1131 max_delay = 10 * HZ;
1132 group = 0;
1133 }
1134 } else {
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001135 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) {
1136 err = -EINVAL;
1137 goto out;
1138 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001139
1140 ih3 = igmpv3_query_hdr(skb);
1141 if (ih3->nsrcs)
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001142 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001143
YOSHIFUJI Hideaki / 吉藤英明0ba8c9e2010-03-15 19:27:00 +00001144 max_delay = ih3->code ?
1145 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
Herbert Xueb1d1642010-02-27 19:41:45 +00001146 }
1147
1148 if (!group)
1149 goto out;
1150
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001151 mp = br_mdb_ip4_get(br->mdb, group);
Herbert Xueb1d1642010-02-27 19:41:45 +00001152 if (!mp)
1153 goto out;
1154
1155 max_delay *= br->multicast_last_member_count;
1156
1157 if (!hlist_unhashed(&mp->mglist) &&
1158 (timer_pending(&mp->timer) ?
1159 time_after(mp->timer.expires, now + max_delay) :
1160 try_to_del_timer_sync(&mp->timer) >= 0))
1161 mod_timer(&mp->timer, now + max_delay);
1162
1163 for (pp = &mp->ports; (p = *pp); pp = &p->next) {
1164 if (timer_pending(&p->timer) ?
1165 time_after(p->timer.expires, now + max_delay) :
1166 try_to_del_timer_sync(&p->timer) >= 0)
1167 mod_timer(&mp->timer, now + max_delay);
1168 }
1169
1170out:
1171 spin_unlock(&br->multicast_lock);
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001172 return err;
Herbert Xueb1d1642010-02-27 19:41:45 +00001173}
1174
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001175#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1176static int br_ip6_multicast_query(struct net_bridge *br,
1177 struct net_bridge_port *port,
1178 struct sk_buff *skb)
1179{
1180 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1181 struct mld_msg *mld = (struct mld_msg *) icmp6_hdr(skb);
1182 struct net_bridge_mdb_entry *mp;
1183 struct mld2_query *mld2q;
1184 struct net_bridge_port_group *p, **pp;
1185 unsigned long max_delay;
1186 unsigned long now = jiffies;
1187 struct in6_addr *group = NULL;
1188 int err = 0;
1189
1190 spin_lock(&br->multicast_lock);
1191 if (!netif_running(br->dev) ||
1192 (port && port->state == BR_STATE_DISABLED))
1193 goto out;
1194
1195 br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->saddr));
1196
1197 if (skb->len == sizeof(*mld)) {
1198 if (!pskb_may_pull(skb, sizeof(*mld))) {
1199 err = -EINVAL;
1200 goto out;
1201 }
1202 mld = (struct mld_msg *) icmp6_hdr(skb);
1203 max_delay = msecs_to_jiffies(htons(mld->mld_maxdelay));
1204 if (max_delay)
1205 group = &mld->mld_mca;
1206 } else if (skb->len >= sizeof(*mld2q)) {
1207 if (!pskb_may_pull(skb, sizeof(*mld2q))) {
1208 err = -EINVAL;
1209 goto out;
1210 }
1211 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1212 if (!mld2q->mld2q_nsrcs)
1213 group = &mld2q->mld2q_mca;
1214 max_delay = mld2q->mld2q_mrc ? MLDV2_MRC(mld2q->mld2q_mrc) : 1;
1215 }
1216
1217 if (!group)
1218 goto out;
1219
1220 mp = br_mdb_ip6_get(br->mdb, group);
1221 if (!mp)
1222 goto out;
1223
1224 max_delay *= br->multicast_last_member_count;
1225 if (!hlist_unhashed(&mp->mglist) &&
1226 (timer_pending(&mp->timer) ?
1227 time_after(mp->timer.expires, now + max_delay) :
1228 try_to_del_timer_sync(&mp->timer) >= 0))
1229 mod_timer(&mp->timer, now + max_delay);
1230
1231 for (pp = &mp->ports; (p = *pp); pp = &p->next) {
1232 if (timer_pending(&p->timer) ?
1233 time_after(p->timer.expires, now + max_delay) :
1234 try_to_del_timer_sync(&p->timer) >= 0)
1235 mod_timer(&mp->timer, now + max_delay);
1236 }
1237
1238out:
1239 spin_unlock(&br->multicast_lock);
1240 return err;
1241}
1242#endif
1243
Herbert Xueb1d1642010-02-27 19:41:45 +00001244static void br_multicast_leave_group(struct net_bridge *br,
1245 struct net_bridge_port *port,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001246 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +00001247{
1248 struct net_bridge_mdb_htable *mdb;
1249 struct net_bridge_mdb_entry *mp;
1250 struct net_bridge_port_group *p;
1251 unsigned long now;
1252 unsigned long time;
1253
Herbert Xueb1d1642010-02-27 19:41:45 +00001254 spin_lock(&br->multicast_lock);
1255 if (!netif_running(br->dev) ||
1256 (port && port->state == BR_STATE_DISABLED) ||
1257 timer_pending(&br->multicast_querier_timer))
1258 goto out;
1259
1260 mdb = br->mdb;
1261 mp = br_mdb_ip_get(mdb, group);
1262 if (!mp)
1263 goto out;
1264
1265 now = jiffies;
1266 time = now + br->multicast_last_member_count *
1267 br->multicast_last_member_interval;
1268
1269 if (!port) {
1270 if (!hlist_unhashed(&mp->mglist) &&
1271 (timer_pending(&mp->timer) ?
1272 time_after(mp->timer.expires, time) :
1273 try_to_del_timer_sync(&mp->timer) >= 0)) {
1274 mod_timer(&mp->timer, time);
1275
1276 mp->queries_sent = 0;
1277 mod_timer(&mp->query_timer, now);
1278 }
1279
1280 goto out;
1281 }
1282
1283 for (p = mp->ports; p; p = p->next) {
1284 if (p->port != port)
1285 continue;
1286
1287 if (!hlist_unhashed(&p->mglist) &&
1288 (timer_pending(&p->timer) ?
1289 time_after(p->timer.expires, time) :
1290 try_to_del_timer_sync(&p->timer) >= 0)) {
1291 mod_timer(&p->timer, time);
1292
1293 p->queries_sent = 0;
1294 mod_timer(&p->query_timer, now);
1295 }
1296
1297 break;
1298 }
1299
1300out:
1301 spin_unlock(&br->multicast_lock);
1302}
1303
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001304static void br_ip4_multicast_leave_group(struct net_bridge *br,
1305 struct net_bridge_port *port,
1306 __be32 group)
1307{
1308 struct br_ip br_group;
1309
1310 if (ipv4_is_local_multicast(group))
1311 return;
1312
1313 br_group.u.ip4 = group;
1314 br_group.proto = htons(ETH_P_IP);
1315
1316 br_multicast_leave_group(br, port, &br_group);
1317}
1318
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001319#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1320static void br_ip6_multicast_leave_group(struct net_bridge *br,
1321 struct net_bridge_port *port,
1322 const struct in6_addr *group)
1323{
1324 struct br_ip br_group;
1325
1326 if (ipv6_is_local_multicast(group))
1327 return;
1328
1329 ipv6_addr_copy(&br_group.u.ip6, group);
1330 br_group.proto = htons(ETH_P_IPV6);
1331
1332 br_multicast_leave_group(br, port, &br_group);
1333}
1334#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001335
Herbert Xueb1d1642010-02-27 19:41:45 +00001336static int br_multicast_ipv4_rcv(struct net_bridge *br,
1337 struct net_bridge_port *port,
1338 struct sk_buff *skb)
1339{
1340 struct sk_buff *skb2 = skb;
1341 struct iphdr *iph;
1342 struct igmphdr *ih;
1343 unsigned len;
1344 unsigned offset;
1345 int err;
1346
Herbert Xueb1d1642010-02-27 19:41:45 +00001347 /* We treat OOM as packet loss for now. */
1348 if (!pskb_may_pull(skb, sizeof(*iph)))
1349 return -EINVAL;
1350
1351 iph = ip_hdr(skb);
1352
1353 if (iph->ihl < 5 || iph->version != 4)
1354 return -EINVAL;
1355
1356 if (!pskb_may_pull(skb, ip_hdrlen(skb)))
1357 return -EINVAL;
1358
1359 iph = ip_hdr(skb);
1360
1361 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1362 return -EINVAL;
1363
1364 if (iph->protocol != IPPROTO_IGMP)
1365 return 0;
1366
1367 len = ntohs(iph->tot_len);
1368 if (skb->len < len || len < ip_hdrlen(skb))
1369 return -EINVAL;
1370
1371 if (skb->len > len) {
1372 skb2 = skb_clone(skb, GFP_ATOMIC);
1373 if (!skb2)
1374 return -ENOMEM;
1375
1376 err = pskb_trim_rcsum(skb2, len);
1377 if (err)
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001378 goto err_out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001379 }
1380
1381 len -= ip_hdrlen(skb2);
1382 offset = skb_network_offset(skb2) + ip_hdrlen(skb2);
1383 __skb_pull(skb2, offset);
1384 skb_reset_transport_header(skb2);
1385
1386 err = -EINVAL;
1387 if (!pskb_may_pull(skb2, sizeof(*ih)))
1388 goto out;
1389
Herbert Xueb1d1642010-02-27 19:41:45 +00001390 switch (skb2->ip_summed) {
1391 case CHECKSUM_COMPLETE:
1392 if (!csum_fold(skb2->csum))
1393 break;
1394 /* fall through */
1395 case CHECKSUM_NONE:
1396 skb2->csum = 0;
1397 if (skb_checksum_complete(skb2))
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001398 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001399 }
1400
1401 err = 0;
1402
1403 BR_INPUT_SKB_CB(skb)->igmp = 1;
1404 ih = igmp_hdr(skb2);
1405
1406 switch (ih->type) {
1407 case IGMP_HOST_MEMBERSHIP_REPORT:
1408 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1409 BR_INPUT_SKB_CB(skb2)->mrouters_only = 1;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001410 err = br_ip4_multicast_add_group(br, port, ih->group);
Herbert Xueb1d1642010-02-27 19:41:45 +00001411 break;
1412 case IGMPV3_HOST_MEMBERSHIP_REPORT:
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001413 err = br_ip4_multicast_igmp3_report(br, port, skb2);
Herbert Xueb1d1642010-02-27 19:41:45 +00001414 break;
1415 case IGMP_HOST_MEMBERSHIP_QUERY:
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001416 err = br_ip4_multicast_query(br, port, skb2);
Herbert Xueb1d1642010-02-27 19:41:45 +00001417 break;
1418 case IGMP_HOST_LEAVE_MESSAGE:
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001419 br_ip4_multicast_leave_group(br, port, ih->group);
Herbert Xueb1d1642010-02-27 19:41:45 +00001420 break;
1421 }
1422
1423out:
1424 __skb_push(skb2, offset);
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001425err_out:
Herbert Xueb1d1642010-02-27 19:41:45 +00001426 if (skb2 != skb)
1427 kfree_skb(skb2);
1428 return err;
1429}
1430
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001431#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1432static int br_multicast_ipv6_rcv(struct net_bridge *br,
1433 struct net_bridge_port *port,
1434 struct sk_buff *skb)
1435{
1436 struct sk_buff *skb2 = skb;
1437 struct ipv6hdr *ip6h;
1438 struct icmp6hdr *icmp6h;
1439 u8 nexthdr;
1440 unsigned len;
1441 unsigned offset;
1442 int err;
1443
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001444 if (!pskb_may_pull(skb, sizeof(*ip6h)))
1445 return -EINVAL;
1446
1447 ip6h = ipv6_hdr(skb);
1448
1449 /*
1450 * We're interested in MLD messages only.
1451 * - Version is 6
1452 * - MLD has always Router Alert hop-by-hop option
1453 * - But we do not support jumbrograms.
1454 */
1455 if (ip6h->version != 6 ||
1456 ip6h->nexthdr != IPPROTO_HOPOPTS ||
1457 ip6h->payload_len == 0)
1458 return 0;
1459
1460 len = ntohs(ip6h->payload_len);
1461 if (skb->len < len)
1462 return -EINVAL;
1463
1464 nexthdr = ip6h->nexthdr;
1465 offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr);
1466
1467 if (offset < 0 || nexthdr != IPPROTO_ICMPV6)
1468 return 0;
1469
1470 /* Okay, we found ICMPv6 header */
1471 skb2 = skb_clone(skb, GFP_ATOMIC);
1472 if (!skb2)
1473 return -ENOMEM;
1474
1475 len -= offset - skb_network_offset(skb2);
1476
1477 __skb_pull(skb2, offset);
1478 skb_reset_transport_header(skb2);
1479
1480 err = -EINVAL;
1481 if (!pskb_may_pull(skb2, sizeof(*icmp6h)))
1482 goto out;
1483
1484 icmp6h = icmp6_hdr(skb2);
1485
1486 switch (icmp6h->icmp6_type) {
1487 case ICMPV6_MGM_QUERY:
1488 case ICMPV6_MGM_REPORT:
1489 case ICMPV6_MGM_REDUCTION:
1490 case ICMPV6_MLD2_REPORT:
1491 break;
1492 default:
1493 err = 0;
1494 goto out;
1495 }
1496
1497 /* Okay, we found MLD message. Check further. */
1498 if (skb2->len > len) {
1499 err = pskb_trim_rcsum(skb2, len);
1500 if (err)
1501 goto out;
1502 }
1503
1504 switch (skb2->ip_summed) {
1505 case CHECKSUM_COMPLETE:
1506 if (!csum_fold(skb2->csum))
1507 break;
1508 /*FALLTHROUGH*/
1509 case CHECKSUM_NONE:
1510 skb2->csum = 0;
1511 if (skb_checksum_complete(skb2))
1512 goto out;
1513 }
1514
1515 err = 0;
1516
1517 BR_INPUT_SKB_CB(skb)->igmp = 1;
1518
1519 switch (icmp6h->icmp6_type) {
1520 case ICMPV6_MGM_REPORT:
1521 {
1522 struct mld_msg *mld = (struct mld_msg *)icmp6h;
1523 BR_INPUT_SKB_CB(skb2)->mrouters_only = 1;
1524 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca);
1525 break;
1526 }
1527 case ICMPV6_MLD2_REPORT:
1528 err = br_ip6_multicast_mld2_report(br, port, skb2);
1529 break;
1530 case ICMPV6_MGM_QUERY:
1531 err = br_ip6_multicast_query(br, port, skb2);
1532 break;
1533 case ICMPV6_MGM_REDUCTION:
1534 {
1535 struct mld_msg *mld = (struct mld_msg *)icmp6h;
1536 br_ip6_multicast_leave_group(br, port, &mld->mld_mca);
1537 }
1538 }
1539
1540out:
1541 __skb_push(skb2, offset);
1542 if (skb2 != skb)
1543 kfree_skb(skb2);
1544 return err;
1545}
1546#endif
1547
Herbert Xueb1d1642010-02-27 19:41:45 +00001548int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1549 struct sk_buff *skb)
1550{
YOSHIFUJI Hideaki / 吉藤英明1fafc7a2010-04-25 08:06:40 +00001551 BR_INPUT_SKB_CB(skb)->igmp = 0;
1552 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1553
Herbert Xueb1d1642010-02-27 19:41:45 +00001554 if (br->multicast_disabled)
1555 return 0;
1556
1557 switch (skb->protocol) {
1558 case htons(ETH_P_IP):
1559 return br_multicast_ipv4_rcv(br, port, skb);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001560#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1561 case htons(ETH_P_IPV6):
1562 return br_multicast_ipv6_rcv(br, port, skb);
1563#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001564 }
1565
1566 return 0;
1567}
1568
1569static void br_multicast_query_expired(unsigned long data)
1570{
1571 struct net_bridge *br = (void *)data;
1572
1573 spin_lock(&br->multicast_lock);
1574 if (br->multicast_startup_queries_sent <
1575 br->multicast_startup_query_count)
1576 br->multicast_startup_queries_sent++;
1577
1578 br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent);
1579
1580 spin_unlock(&br->multicast_lock);
1581}
1582
1583void br_multicast_init(struct net_bridge *br)
1584{
1585 br->hash_elasticity = 4;
1586 br->hash_max = 512;
1587
1588 br->multicast_router = 1;
1589 br->multicast_last_member_count = 2;
1590 br->multicast_startup_query_count = 2;
1591
1592 br->multicast_last_member_interval = HZ;
1593 br->multicast_query_response_interval = 10 * HZ;
1594 br->multicast_startup_query_interval = 125 * HZ / 4;
1595 br->multicast_query_interval = 125 * HZ;
1596 br->multicast_querier_interval = 255 * HZ;
1597 br->multicast_membership_interval = 260 * HZ;
1598
1599 spin_lock_init(&br->multicast_lock);
1600 setup_timer(&br->multicast_router_timer,
1601 br_multicast_local_router_expired, 0);
1602 setup_timer(&br->multicast_querier_timer,
1603 br_multicast_local_router_expired, 0);
1604 setup_timer(&br->multicast_query_timer, br_multicast_query_expired,
1605 (unsigned long)br);
1606}
1607
1608void br_multicast_open(struct net_bridge *br)
1609{
1610 br->multicast_startup_queries_sent = 0;
1611
1612 if (br->multicast_disabled)
1613 return;
1614
1615 mod_timer(&br->multicast_query_timer, jiffies);
1616}
1617
1618void br_multicast_stop(struct net_bridge *br)
1619{
1620 struct net_bridge_mdb_htable *mdb;
1621 struct net_bridge_mdb_entry *mp;
1622 struct hlist_node *p, *n;
1623 u32 ver;
1624 int i;
1625
1626 del_timer_sync(&br->multicast_router_timer);
1627 del_timer_sync(&br->multicast_querier_timer);
1628 del_timer_sync(&br->multicast_query_timer);
1629
1630 spin_lock_bh(&br->multicast_lock);
1631 mdb = br->mdb;
1632 if (!mdb)
1633 goto out;
1634
1635 br->mdb = NULL;
1636
1637 ver = mdb->ver;
1638 for (i = 0; i < mdb->max; i++) {
1639 hlist_for_each_entry_safe(mp, p, n, &mdb->mhash[i],
1640 hlist[ver]) {
1641 del_timer(&mp->timer);
1642 del_timer(&mp->query_timer);
1643 call_rcu_bh(&mp->rcu, br_multicast_free_group);
1644 }
1645 }
1646
1647 if (mdb->old) {
1648 spin_unlock_bh(&br->multicast_lock);
Herbert Xu10cc2b52010-03-05 21:03:35 +00001649 rcu_barrier_bh();
Herbert Xueb1d1642010-02-27 19:41:45 +00001650 spin_lock_bh(&br->multicast_lock);
1651 WARN_ON(mdb->old);
1652 }
1653
1654 mdb->old = mdb;
1655 call_rcu_bh(&mdb->rcu, br_mdb_free);
1656
1657out:
1658 spin_unlock_bh(&br->multicast_lock);
1659}
Herbert Xu0909e112010-02-27 19:41:49 +00001660
1661int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1662{
1663 int err = -ENOENT;
1664
1665 spin_lock_bh(&br->multicast_lock);
1666 if (!netif_running(br->dev))
1667 goto unlock;
1668
1669 switch (val) {
1670 case 0:
1671 case 2:
1672 del_timer(&br->multicast_router_timer);
1673 /* fall through */
1674 case 1:
1675 br->multicast_router = val;
1676 err = 0;
1677 break;
1678
1679 default:
1680 err = -EINVAL;
1681 break;
1682 }
1683
1684unlock:
1685 spin_unlock_bh(&br->multicast_lock);
1686
1687 return err;
1688}
1689
1690int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1691{
1692 struct net_bridge *br = p->br;
1693 int err = -ENOENT;
1694
1695 spin_lock(&br->multicast_lock);
1696 if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED)
1697 goto unlock;
1698
1699 switch (val) {
1700 case 0:
1701 case 1:
1702 case 2:
1703 p->multicast_router = val;
1704 err = 0;
1705
1706 if (val < 2 && !hlist_unhashed(&p->rlist))
1707 hlist_del_init_rcu(&p->rlist);
1708
1709 if (val == 1)
1710 break;
1711
1712 del_timer(&p->multicast_router_timer);
1713
1714 if (val == 0)
1715 break;
1716
1717 br_multicast_add_router(br, p);
1718 break;
1719
1720 default:
1721 err = -EINVAL;
1722 break;
1723 }
1724
1725unlock:
1726 spin_unlock(&br->multicast_lock);
1727
1728 return err;
1729}
Herbert Xu561f11032010-02-27 19:41:50 +00001730
1731int br_multicast_toggle(struct net_bridge *br, unsigned long val)
1732{
1733 struct net_bridge_port *port;
1734 int err = -ENOENT;
1735
1736 spin_lock(&br->multicast_lock);
1737 if (!netif_running(br->dev))
1738 goto unlock;
1739
1740 err = 0;
1741 if (br->multicast_disabled == !val)
1742 goto unlock;
1743
1744 br->multicast_disabled = !val;
1745 if (br->multicast_disabled)
1746 goto unlock;
1747
1748 if (br->mdb) {
1749 if (br->mdb->old) {
1750 err = -EEXIST;
1751rollback:
1752 br->multicast_disabled = !!val;
1753 goto unlock;
1754 }
1755
1756 err = br_mdb_rehash(&br->mdb, br->mdb->max,
1757 br->hash_elasticity);
1758 if (err)
1759 goto rollback;
1760 }
1761
1762 br_multicast_open(br);
1763 list_for_each_entry(port, &br->port_list, list) {
1764 if (port->state == BR_STATE_DISABLED ||
1765 port->state == BR_STATE_BLOCKING)
1766 continue;
1767
1768 __br_multicast_enable_port(port);
1769 }
1770
1771unlock:
1772 spin_unlock(&br->multicast_lock);
1773
1774 return err;
1775}
Herbert Xub1951672010-02-27 19:41:51 +00001776
1777int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
1778{
1779 int err = -ENOENT;
1780 u32 old;
1781
1782 spin_lock(&br->multicast_lock);
1783 if (!netif_running(br->dev))
1784 goto unlock;
1785
1786 err = -EINVAL;
1787 if (!is_power_of_2(val))
1788 goto unlock;
1789 if (br->mdb && val < br->mdb->size)
1790 goto unlock;
1791
1792 err = 0;
1793
1794 old = br->hash_max;
1795 br->hash_max = val;
1796
1797 if (br->mdb) {
1798 if (br->mdb->old) {
1799 err = -EEXIST;
1800rollback:
1801 br->hash_max = old;
1802 goto unlock;
1803 }
1804
1805 err = br_mdb_rehash(&br->mdb, br->hash_max,
1806 br->hash_elasticity);
1807 if (err)
1808 goto rollback;
1809 }
1810
1811unlock:
1812 spin_unlock(&br->multicast_lock);
1813
1814 return err;
1815}