bridge br_multicast: Fix skb leakage in error path.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / bridge / br_multicast.c
blob6980625537caa15e5f0a479eccca15c3341073a3
1 /*
2 * Bridge multicast support.
4 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
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.
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>
18 #include <linux/log2.h>
19 #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>
28 #include "br_private.h"
30 static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb, __be32 ip)
32 return jhash_1word(mdb->secret, (u32)ip) & (mdb->max - 1);
35 static struct net_bridge_mdb_entry *__br_mdb_ip_get(
36 struct net_bridge_mdb_htable *mdb, __be32 dst, int hash)
38 struct net_bridge_mdb_entry *mp;
39 struct hlist_node *p;
41 hlist_for_each_entry_rcu(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) {
42 if (dst == mp->addr)
43 return mp;
46 return NULL;
49 static struct net_bridge_mdb_entry *br_mdb_ip_get(
50 struct net_bridge_mdb_htable *mdb, __be32 dst)
52 if (!mdb)
53 return NULL;
55 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
58 struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
59 struct sk_buff *skb)
61 if (br->multicast_disabled)
62 return NULL;
64 switch (skb->protocol) {
65 case htons(ETH_P_IP):
66 if (BR_INPUT_SKB_CB(skb)->igmp)
67 break;
68 return br_mdb_ip_get(br->mdb, ip_hdr(skb)->daddr);
71 return NULL;
74 static void br_mdb_free(struct rcu_head *head)
76 struct net_bridge_mdb_htable *mdb =
77 container_of(head, struct net_bridge_mdb_htable, rcu);
78 struct net_bridge_mdb_htable *old = mdb->old;
80 mdb->old = NULL;
81 kfree(old->mhash);
82 kfree(old);
85 static int br_mdb_copy(struct net_bridge_mdb_htable *new,
86 struct net_bridge_mdb_htable *old,
87 int elasticity)
89 struct net_bridge_mdb_entry *mp;
90 struct hlist_node *p;
91 int maxlen;
92 int len;
93 int i;
95 for (i = 0; i < old->max; i++)
96 hlist_for_each_entry(mp, p, &old->mhash[i], hlist[old->ver])
97 hlist_add_head(&mp->hlist[new->ver],
98 &new->mhash[br_ip_hash(new, mp->addr)]);
100 if (!elasticity)
101 return 0;
103 maxlen = 0;
104 for (i = 0; i < new->max; i++) {
105 len = 0;
106 hlist_for_each_entry(mp, p, &new->mhash[i], hlist[new->ver])
107 len++;
108 if (len > maxlen)
109 maxlen = len;
112 return maxlen > elasticity ? -EINVAL : 0;
115 static void br_multicast_free_pg(struct rcu_head *head)
117 struct net_bridge_port_group *p =
118 container_of(head, struct net_bridge_port_group, rcu);
120 kfree(p);
123 static void br_multicast_free_group(struct rcu_head *head)
125 struct net_bridge_mdb_entry *mp =
126 container_of(head, struct net_bridge_mdb_entry, rcu);
128 kfree(mp);
131 static void br_multicast_group_expired(unsigned long data)
133 struct net_bridge_mdb_entry *mp = (void *)data;
134 struct net_bridge *br = mp->br;
135 struct net_bridge_mdb_htable *mdb;
137 spin_lock(&br->multicast_lock);
138 if (!netif_running(br->dev) || timer_pending(&mp->timer))
139 goto out;
141 if (!hlist_unhashed(&mp->mglist))
142 hlist_del_init(&mp->mglist);
144 if (mp->ports)
145 goto out;
147 mdb = br->mdb;
148 hlist_del_rcu(&mp->hlist[mdb->ver]);
149 mdb->size--;
151 del_timer(&mp->query_timer);
152 call_rcu_bh(&mp->rcu, br_multicast_free_group);
154 out:
155 spin_unlock(&br->multicast_lock);
158 static void br_multicast_del_pg(struct net_bridge *br,
159 struct net_bridge_port_group *pg)
161 struct net_bridge_mdb_htable *mdb = br->mdb;
162 struct net_bridge_mdb_entry *mp;
163 struct net_bridge_port_group *p;
164 struct net_bridge_port_group **pp;
166 mp = br_mdb_ip_get(mdb, pg->addr);
167 if (WARN_ON(!mp))
168 return;
170 for (pp = &mp->ports; (p = *pp); pp = &p->next) {
171 if (p != pg)
172 continue;
174 *pp = p->next;
175 hlist_del_init(&p->mglist);
176 del_timer(&p->timer);
177 del_timer(&p->query_timer);
178 call_rcu_bh(&p->rcu, br_multicast_free_pg);
180 if (!mp->ports && hlist_unhashed(&mp->mglist) &&
181 netif_running(br->dev))
182 mod_timer(&mp->timer, jiffies);
184 return;
187 WARN_ON(1);
190 static void br_multicast_port_group_expired(unsigned long data)
192 struct net_bridge_port_group *pg = (void *)data;
193 struct net_bridge *br = pg->port->br;
195 spin_lock(&br->multicast_lock);
196 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
197 hlist_unhashed(&pg->mglist))
198 goto out;
200 br_multicast_del_pg(br, pg);
202 out:
203 spin_unlock(&br->multicast_lock);
206 static int br_mdb_rehash(struct net_bridge_mdb_htable **mdbp, int max,
207 int elasticity)
209 struct net_bridge_mdb_htable *old = *mdbp;
210 struct net_bridge_mdb_htable *mdb;
211 int err;
213 mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
214 if (!mdb)
215 return -ENOMEM;
217 mdb->max = max;
218 mdb->old = old;
220 mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
221 if (!mdb->mhash) {
222 kfree(mdb);
223 return -ENOMEM;
226 mdb->size = old ? old->size : 0;
227 mdb->ver = old ? old->ver ^ 1 : 0;
229 if (!old || elasticity)
230 get_random_bytes(&mdb->secret, sizeof(mdb->secret));
231 else
232 mdb->secret = old->secret;
234 if (!old)
235 goto out;
237 err = br_mdb_copy(mdb, old, elasticity);
238 if (err) {
239 kfree(mdb->mhash);
240 kfree(mdb);
241 return err;
244 call_rcu_bh(&mdb->rcu, br_mdb_free);
246 out:
247 rcu_assign_pointer(*mdbp, mdb);
249 return 0;
252 static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
253 __be32 group)
255 struct sk_buff *skb;
256 struct igmphdr *ih;
257 struct ethhdr *eth;
258 struct iphdr *iph;
260 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
261 sizeof(*ih) + 4);
262 if (!skb)
263 goto out;
265 skb->protocol = htons(ETH_P_IP);
267 skb_reset_mac_header(skb);
268 eth = eth_hdr(skb);
270 memcpy(eth->h_source, br->dev->dev_addr, 6);
271 eth->h_dest[0] = 1;
272 eth->h_dest[1] = 0;
273 eth->h_dest[2] = 0x5e;
274 eth->h_dest[3] = 0;
275 eth->h_dest[4] = 0;
276 eth->h_dest[5] = 1;
277 eth->h_proto = htons(ETH_P_IP);
278 skb_put(skb, sizeof(*eth));
280 skb_set_network_header(skb, skb->len);
281 iph = ip_hdr(skb);
283 iph->version = 4;
284 iph->ihl = 6;
285 iph->tos = 0xc0;
286 iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4);
287 iph->id = 0;
288 iph->frag_off = htons(IP_DF);
289 iph->ttl = 1;
290 iph->protocol = IPPROTO_IGMP;
291 iph->saddr = 0;
292 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
293 ((u8 *)&iph[1])[0] = IPOPT_RA;
294 ((u8 *)&iph[1])[1] = 4;
295 ((u8 *)&iph[1])[2] = 0;
296 ((u8 *)&iph[1])[3] = 0;
297 ip_send_check(iph);
298 skb_put(skb, 24);
300 skb_set_transport_header(skb, skb->len);
301 ih = igmp_hdr(skb);
302 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
303 ih->code = (group ? br->multicast_last_member_interval :
304 br->multicast_query_response_interval) /
305 (HZ / IGMP_TIMER_SCALE);
306 ih->group = group;
307 ih->csum = 0;
308 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
309 skb_put(skb, sizeof(*ih));
311 __skb_pull(skb, sizeof(*eth));
313 out:
314 return skb;
317 static void br_multicast_send_group_query(struct net_bridge_mdb_entry *mp)
319 struct net_bridge *br = mp->br;
320 struct sk_buff *skb;
322 skb = br_multicast_alloc_query(br, mp->addr);
323 if (!skb)
324 goto timer;
326 netif_rx(skb);
328 timer:
329 if (++mp->queries_sent < br->multicast_last_member_count)
330 mod_timer(&mp->query_timer,
331 jiffies + br->multicast_last_member_interval);
334 static void br_multicast_group_query_expired(unsigned long data)
336 struct net_bridge_mdb_entry *mp = (void *)data;
337 struct net_bridge *br = mp->br;
339 spin_lock(&br->multicast_lock);
340 if (!netif_running(br->dev) || hlist_unhashed(&mp->mglist) ||
341 mp->queries_sent >= br->multicast_last_member_count)
342 goto out;
344 br_multicast_send_group_query(mp);
346 out:
347 spin_unlock(&br->multicast_lock);
350 static void br_multicast_send_port_group_query(struct net_bridge_port_group *pg)
352 struct net_bridge_port *port = pg->port;
353 struct net_bridge *br = port->br;
354 struct sk_buff *skb;
356 skb = br_multicast_alloc_query(br, pg->addr);
357 if (!skb)
358 goto timer;
360 br_deliver(port, skb);
362 timer:
363 if (++pg->queries_sent < br->multicast_last_member_count)
364 mod_timer(&pg->query_timer,
365 jiffies + br->multicast_last_member_interval);
368 static void br_multicast_port_group_query_expired(unsigned long data)
370 struct net_bridge_port_group *pg = (void *)data;
371 struct net_bridge_port *port = pg->port;
372 struct net_bridge *br = port->br;
374 spin_lock(&br->multicast_lock);
375 if (!netif_running(br->dev) || hlist_unhashed(&pg->mglist) ||
376 pg->queries_sent >= br->multicast_last_member_count)
377 goto out;
379 br_multicast_send_port_group_query(pg);
381 out:
382 spin_unlock(&br->multicast_lock);
385 static struct net_bridge_mdb_entry *br_multicast_get_group(
386 struct net_bridge *br, struct net_bridge_port *port, __be32 group,
387 int hash)
389 struct net_bridge_mdb_htable *mdb = br->mdb;
390 struct net_bridge_mdb_entry *mp;
391 struct hlist_node *p;
392 unsigned count = 0;
393 unsigned max;
394 int elasticity;
395 int err;
397 hlist_for_each_entry(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) {
398 count++;
399 if (unlikely(group == mp->addr)) {
400 return mp;
404 elasticity = 0;
405 max = mdb->max;
407 if (unlikely(count > br->hash_elasticity && count)) {
408 if (net_ratelimit())
409 printk(KERN_INFO "%s: Multicast hash table "
410 "chain limit reached: %s\n",
411 br->dev->name, port ? port->dev->name :
412 br->dev->name);
414 elasticity = br->hash_elasticity;
417 if (mdb->size >= max) {
418 max *= 2;
419 if (unlikely(max >= br->hash_max)) {
420 printk(KERN_WARNING "%s: Multicast hash table maximum "
421 "reached, disabling snooping: %s, %d\n",
422 br->dev->name, port ? port->dev->name :
423 br->dev->name,
424 max);
425 err = -E2BIG;
426 disable:
427 br->multicast_disabled = 1;
428 goto err;
432 if (max > mdb->max || elasticity) {
433 if (mdb->old) {
434 if (net_ratelimit())
435 printk(KERN_INFO "%s: Multicast hash table "
436 "on fire: %s\n",
437 br->dev->name, port ? port->dev->name :
438 br->dev->name);
439 err = -EEXIST;
440 goto err;
443 err = br_mdb_rehash(&br->mdb, max, elasticity);
444 if (err) {
445 printk(KERN_WARNING "%s: Cannot rehash multicast "
446 "hash table, disabling snooping: "
447 "%s, %d, %d\n",
448 br->dev->name, port ? port->dev->name :
449 br->dev->name,
450 mdb->size, err);
451 goto disable;
454 err = -EAGAIN;
455 goto err;
458 return NULL;
460 err:
461 mp = ERR_PTR(err);
462 return mp;
465 static struct net_bridge_mdb_entry *br_multicast_new_group(
466 struct net_bridge *br, struct net_bridge_port *port, __be32 group)
468 struct net_bridge_mdb_htable *mdb = br->mdb;
469 struct net_bridge_mdb_entry *mp;
470 int hash;
472 if (!mdb) {
473 if (br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0))
474 return NULL;
475 goto rehash;
478 hash = br_ip_hash(mdb, group);
479 mp = br_multicast_get_group(br, port, group, hash);
480 switch (PTR_ERR(mp)) {
481 case 0:
482 break;
484 case -EAGAIN:
485 rehash:
486 mdb = br->mdb;
487 hash = br_ip_hash(mdb, group);
488 break;
490 default:
491 goto out;
494 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
495 if (unlikely(!mp))
496 goto out;
498 mp->br = br;
499 mp->addr = group;
500 setup_timer(&mp->timer, br_multicast_group_expired,
501 (unsigned long)mp);
502 setup_timer(&mp->query_timer, br_multicast_group_query_expired,
503 (unsigned long)mp);
505 hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
506 mdb->size++;
508 out:
509 return mp;
512 static int br_multicast_add_group(struct net_bridge *br,
513 struct net_bridge_port *port, __be32 group)
515 struct net_bridge_mdb_entry *mp;
516 struct net_bridge_port_group *p;
517 struct net_bridge_port_group **pp;
518 unsigned long now = jiffies;
519 int err;
521 if (ipv4_is_local_multicast(group))
522 return 0;
524 spin_lock(&br->multicast_lock);
525 if (!netif_running(br->dev) ||
526 (port && port->state == BR_STATE_DISABLED))
527 goto out;
529 mp = br_multicast_new_group(br, port, group);
530 err = PTR_ERR(mp);
531 if (unlikely(IS_ERR(mp) || !mp))
532 goto err;
534 if (!port) {
535 hlist_add_head(&mp->mglist, &br->mglist);
536 mod_timer(&mp->timer, now + br->multicast_membership_interval);
537 goto out;
540 for (pp = &mp->ports; (p = *pp); pp = &p->next) {
541 if (p->port == port)
542 goto found;
543 if ((unsigned long)p->port < (unsigned long)port)
544 break;
547 p = kzalloc(sizeof(*p), GFP_ATOMIC);
548 err = -ENOMEM;
549 if (unlikely(!p))
550 goto err;
552 p->addr = group;
553 p->port = port;
554 p->next = *pp;
555 hlist_add_head(&p->mglist, &port->mglist);
556 setup_timer(&p->timer, br_multicast_port_group_expired,
557 (unsigned long)p);
558 setup_timer(&p->query_timer, br_multicast_port_group_query_expired,
559 (unsigned long)p);
561 rcu_assign_pointer(*pp, p);
563 found:
564 mod_timer(&p->timer, now + br->multicast_membership_interval);
565 out:
566 err = 0;
568 err:
569 spin_unlock(&br->multicast_lock);
570 return err;
573 static void br_multicast_router_expired(unsigned long data)
575 struct net_bridge_port *port = (void *)data;
576 struct net_bridge *br = port->br;
578 spin_lock(&br->multicast_lock);
579 if (port->multicast_router != 1 ||
580 timer_pending(&port->multicast_router_timer) ||
581 hlist_unhashed(&port->rlist))
582 goto out;
584 hlist_del_init_rcu(&port->rlist);
586 out:
587 spin_unlock(&br->multicast_lock);
590 static void br_multicast_local_router_expired(unsigned long data)
594 static void br_multicast_send_query(struct net_bridge *br,
595 struct net_bridge_port *port, u32 sent)
597 unsigned long time;
598 struct sk_buff *skb;
600 if (!netif_running(br->dev) || br->multicast_disabled ||
601 timer_pending(&br->multicast_querier_timer))
602 return;
604 skb = br_multicast_alloc_query(br, 0);
605 if (!skb)
606 goto timer;
608 if (port) {
609 __skb_push(skb, sizeof(struct ethhdr));
610 skb->dev = port->dev;
611 NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
612 dev_queue_xmit);
613 } else
614 netif_rx(skb);
616 timer:
617 time = jiffies;
618 time += sent < br->multicast_startup_query_count ?
619 br->multicast_startup_query_interval :
620 br->multicast_query_interval;
621 mod_timer(port ? &port->multicast_query_timer :
622 &br->multicast_query_timer, time);
625 static void br_multicast_port_query_expired(unsigned long data)
627 struct net_bridge_port *port = (void *)data;
628 struct net_bridge *br = port->br;
630 spin_lock(&br->multicast_lock);
631 if (port->state == BR_STATE_DISABLED ||
632 port->state == BR_STATE_BLOCKING)
633 goto out;
635 if (port->multicast_startup_queries_sent <
636 br->multicast_startup_query_count)
637 port->multicast_startup_queries_sent++;
639 br_multicast_send_query(port->br, port,
640 port->multicast_startup_queries_sent);
642 out:
643 spin_unlock(&br->multicast_lock);
646 void br_multicast_add_port(struct net_bridge_port *port)
648 port->multicast_router = 1;
650 setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
651 (unsigned long)port);
652 setup_timer(&port->multicast_query_timer,
653 br_multicast_port_query_expired, (unsigned long)port);
656 void br_multicast_del_port(struct net_bridge_port *port)
658 del_timer_sync(&port->multicast_router_timer);
661 static void __br_multicast_enable_port(struct net_bridge_port *port)
663 port->multicast_startup_queries_sent = 0;
665 if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 ||
666 del_timer(&port->multicast_query_timer))
667 mod_timer(&port->multicast_query_timer, jiffies);
670 void br_multicast_enable_port(struct net_bridge_port *port)
672 struct net_bridge *br = port->br;
674 spin_lock(&br->multicast_lock);
675 if (br->multicast_disabled || !netif_running(br->dev))
676 goto out;
678 __br_multicast_enable_port(port);
680 out:
681 spin_unlock(&br->multicast_lock);
684 void br_multicast_disable_port(struct net_bridge_port *port)
686 struct net_bridge *br = port->br;
687 struct net_bridge_port_group *pg;
688 struct hlist_node *p, *n;
690 spin_lock(&br->multicast_lock);
691 hlist_for_each_entry_safe(pg, p, n, &port->mglist, mglist)
692 br_multicast_del_pg(br, pg);
694 if (!hlist_unhashed(&port->rlist))
695 hlist_del_init_rcu(&port->rlist);
696 del_timer(&port->multicast_router_timer);
697 del_timer(&port->multicast_query_timer);
698 spin_unlock(&br->multicast_lock);
701 static int br_multicast_igmp3_report(struct net_bridge *br,
702 struct net_bridge_port *port,
703 struct sk_buff *skb)
705 struct igmpv3_report *ih;
706 struct igmpv3_grec *grec;
707 int i;
708 int len;
709 int num;
710 int type;
711 int err = 0;
712 __be32 group;
714 if (!pskb_may_pull(skb, sizeof(*ih)))
715 return -EINVAL;
717 ih = igmpv3_report_hdr(skb);
718 num = ntohs(ih->ngrec);
719 len = sizeof(*ih);
721 for (i = 0; i < num; i++) {
722 len += sizeof(*grec);
723 if (!pskb_may_pull(skb, len))
724 return -EINVAL;
726 grec = (void *)(skb->data + len);
727 group = grec->grec_mca;
728 type = grec->grec_type;
730 len += grec->grec_nsrcs * 4;
731 if (!pskb_may_pull(skb, len))
732 return -EINVAL;
734 /* We treat this as an IGMPv2 report for now. */
735 switch (type) {
736 case IGMPV3_MODE_IS_INCLUDE:
737 case IGMPV3_MODE_IS_EXCLUDE:
738 case IGMPV3_CHANGE_TO_INCLUDE:
739 case IGMPV3_CHANGE_TO_EXCLUDE:
740 case IGMPV3_ALLOW_NEW_SOURCES:
741 case IGMPV3_BLOCK_OLD_SOURCES:
742 break;
744 default:
745 continue;
748 err = br_multicast_add_group(br, port, group);
749 if (err)
750 break;
753 return err;
756 static void br_multicast_add_router(struct net_bridge *br,
757 struct net_bridge_port *port)
759 struct hlist_node *p;
760 struct hlist_node **h;
762 for (h = &br->router_list.first;
763 (p = *h) &&
764 (unsigned long)container_of(p, struct net_bridge_port, rlist) >
765 (unsigned long)port;
766 h = &p->next)
769 port->rlist.pprev = h;
770 port->rlist.next = p;
771 rcu_assign_pointer(*h, &port->rlist);
772 if (p)
773 p->pprev = &port->rlist.next;
776 static void br_multicast_mark_router(struct net_bridge *br,
777 struct net_bridge_port *port)
779 unsigned long now = jiffies;
781 if (!port) {
782 if (br->multicast_router == 1)
783 mod_timer(&br->multicast_router_timer,
784 now + br->multicast_querier_interval);
785 return;
788 if (port->multicast_router != 1)
789 return;
791 if (!hlist_unhashed(&port->rlist))
792 goto timer;
794 br_multicast_add_router(br, port);
796 timer:
797 mod_timer(&port->multicast_router_timer,
798 now + br->multicast_querier_interval);
801 static void br_multicast_query_received(struct net_bridge *br,
802 struct net_bridge_port *port,
803 __be32 saddr)
805 if (saddr)
806 mod_timer(&br->multicast_querier_timer,
807 jiffies + br->multicast_querier_interval);
808 else if (timer_pending(&br->multicast_querier_timer))
809 return;
811 br_multicast_mark_router(br, port);
814 static int br_multicast_query(struct net_bridge *br,
815 struct net_bridge_port *port,
816 struct sk_buff *skb)
818 struct iphdr *iph = ip_hdr(skb);
819 struct igmphdr *ih = igmp_hdr(skb);
820 struct net_bridge_mdb_entry *mp;
821 struct igmpv3_query *ih3;
822 struct net_bridge_port_group *p;
823 struct net_bridge_port_group **pp;
824 unsigned long max_delay;
825 unsigned long now = jiffies;
826 __be32 group;
827 int err = 0;
829 spin_lock(&br->multicast_lock);
830 if (!netif_running(br->dev) ||
831 (port && port->state == BR_STATE_DISABLED))
832 goto out;
834 br_multicast_query_received(br, port, iph->saddr);
836 group = ih->group;
838 if (skb->len == sizeof(*ih)) {
839 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
841 if (!max_delay) {
842 max_delay = 10 * HZ;
843 group = 0;
845 } else {
846 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) {
847 err = -EINVAL;
848 goto out;
851 ih3 = igmpv3_query_hdr(skb);
852 if (ih3->nsrcs)
853 goto out;
855 max_delay = ih3->code ?
856 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
859 if (!group)
860 goto out;
862 mp = br_mdb_ip_get(br->mdb, group);
863 if (!mp)
864 goto out;
866 max_delay *= br->multicast_last_member_count;
868 if (!hlist_unhashed(&mp->mglist) &&
869 (timer_pending(&mp->timer) ?
870 time_after(mp->timer.expires, now + max_delay) :
871 try_to_del_timer_sync(&mp->timer) >= 0))
872 mod_timer(&mp->timer, now + max_delay);
874 for (pp = &mp->ports; (p = *pp); pp = &p->next) {
875 if (timer_pending(&p->timer) ?
876 time_after(p->timer.expires, now + max_delay) :
877 try_to_del_timer_sync(&p->timer) >= 0)
878 mod_timer(&mp->timer, now + max_delay);
881 out:
882 spin_unlock(&br->multicast_lock);
883 return err;
886 static void br_multicast_leave_group(struct net_bridge *br,
887 struct net_bridge_port *port,
888 __be32 group)
890 struct net_bridge_mdb_htable *mdb;
891 struct net_bridge_mdb_entry *mp;
892 struct net_bridge_port_group *p;
893 unsigned long now;
894 unsigned long time;
896 if (ipv4_is_local_multicast(group))
897 return;
899 spin_lock(&br->multicast_lock);
900 if (!netif_running(br->dev) ||
901 (port && port->state == BR_STATE_DISABLED) ||
902 timer_pending(&br->multicast_querier_timer))
903 goto out;
905 mdb = br->mdb;
906 mp = br_mdb_ip_get(mdb, group);
907 if (!mp)
908 goto out;
910 now = jiffies;
911 time = now + br->multicast_last_member_count *
912 br->multicast_last_member_interval;
914 if (!port) {
915 if (!hlist_unhashed(&mp->mglist) &&
916 (timer_pending(&mp->timer) ?
917 time_after(mp->timer.expires, time) :
918 try_to_del_timer_sync(&mp->timer) >= 0)) {
919 mod_timer(&mp->timer, time);
921 mp->queries_sent = 0;
922 mod_timer(&mp->query_timer, now);
925 goto out;
928 for (p = mp->ports; p; p = p->next) {
929 if (p->port != port)
930 continue;
932 if (!hlist_unhashed(&p->mglist) &&
933 (timer_pending(&p->timer) ?
934 time_after(p->timer.expires, time) :
935 try_to_del_timer_sync(&p->timer) >= 0)) {
936 mod_timer(&p->timer, time);
938 p->queries_sent = 0;
939 mod_timer(&p->query_timer, now);
942 break;
945 out:
946 spin_unlock(&br->multicast_lock);
949 static int br_multicast_ipv4_rcv(struct net_bridge *br,
950 struct net_bridge_port *port,
951 struct sk_buff *skb)
953 struct sk_buff *skb2 = skb;
954 struct iphdr *iph;
955 struct igmphdr *ih;
956 unsigned len;
957 unsigned offset;
958 int err;
960 BR_INPUT_SKB_CB(skb)->igmp = 0;
961 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
963 /* We treat OOM as packet loss for now. */
964 if (!pskb_may_pull(skb, sizeof(*iph)))
965 return -EINVAL;
967 iph = ip_hdr(skb);
969 if (iph->ihl < 5 || iph->version != 4)
970 return -EINVAL;
972 if (!pskb_may_pull(skb, ip_hdrlen(skb)))
973 return -EINVAL;
975 iph = ip_hdr(skb);
977 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
978 return -EINVAL;
980 if (iph->protocol != IPPROTO_IGMP)
981 return 0;
983 len = ntohs(iph->tot_len);
984 if (skb->len < len || len < ip_hdrlen(skb))
985 return -EINVAL;
987 if (skb->len > len) {
988 skb2 = skb_clone(skb, GFP_ATOMIC);
989 if (!skb2)
990 return -ENOMEM;
992 err = pskb_trim_rcsum(skb2, len);
993 if (err)
994 goto err_out;
997 len -= ip_hdrlen(skb2);
998 offset = skb_network_offset(skb2) + ip_hdrlen(skb2);
999 __skb_pull(skb2, offset);
1000 skb_reset_transport_header(skb2);
1002 err = -EINVAL;
1003 if (!pskb_may_pull(skb2, sizeof(*ih)))
1004 goto out;
1006 iph = ip_hdr(skb2);
1008 switch (skb2->ip_summed) {
1009 case CHECKSUM_COMPLETE:
1010 if (!csum_fold(skb2->csum))
1011 break;
1012 /* fall through */
1013 case CHECKSUM_NONE:
1014 skb2->csum = 0;
1015 if (skb_checksum_complete(skb2))
1016 goto out;
1019 err = 0;
1021 BR_INPUT_SKB_CB(skb)->igmp = 1;
1022 ih = igmp_hdr(skb2);
1024 switch (ih->type) {
1025 case IGMP_HOST_MEMBERSHIP_REPORT:
1026 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1027 BR_INPUT_SKB_CB(skb2)->mrouters_only = 1;
1028 err = br_multicast_add_group(br, port, ih->group);
1029 break;
1030 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1031 err = br_multicast_igmp3_report(br, port, skb2);
1032 break;
1033 case IGMP_HOST_MEMBERSHIP_QUERY:
1034 err = br_multicast_query(br, port, skb2);
1035 break;
1036 case IGMP_HOST_LEAVE_MESSAGE:
1037 br_multicast_leave_group(br, port, ih->group);
1038 break;
1041 out:
1042 __skb_push(skb2, offset);
1043 err_out:
1044 if (skb2 != skb)
1045 kfree_skb(skb2);
1046 return err;
1049 int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1050 struct sk_buff *skb)
1052 if (br->multicast_disabled)
1053 return 0;
1055 switch (skb->protocol) {
1056 case htons(ETH_P_IP):
1057 return br_multicast_ipv4_rcv(br, port, skb);
1060 return 0;
1063 static void br_multicast_query_expired(unsigned long data)
1065 struct net_bridge *br = (void *)data;
1067 spin_lock(&br->multicast_lock);
1068 if (br->multicast_startup_queries_sent <
1069 br->multicast_startup_query_count)
1070 br->multicast_startup_queries_sent++;
1072 br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent);
1074 spin_unlock(&br->multicast_lock);
1077 void br_multicast_init(struct net_bridge *br)
1079 br->hash_elasticity = 4;
1080 br->hash_max = 512;
1082 br->multicast_router = 1;
1083 br->multicast_last_member_count = 2;
1084 br->multicast_startup_query_count = 2;
1086 br->multicast_last_member_interval = HZ;
1087 br->multicast_query_response_interval = 10 * HZ;
1088 br->multicast_startup_query_interval = 125 * HZ / 4;
1089 br->multicast_query_interval = 125 * HZ;
1090 br->multicast_querier_interval = 255 * HZ;
1091 br->multicast_membership_interval = 260 * HZ;
1093 spin_lock_init(&br->multicast_lock);
1094 setup_timer(&br->multicast_router_timer,
1095 br_multicast_local_router_expired, 0);
1096 setup_timer(&br->multicast_querier_timer,
1097 br_multicast_local_router_expired, 0);
1098 setup_timer(&br->multicast_query_timer, br_multicast_query_expired,
1099 (unsigned long)br);
1102 void br_multicast_open(struct net_bridge *br)
1104 br->multicast_startup_queries_sent = 0;
1106 if (br->multicast_disabled)
1107 return;
1109 mod_timer(&br->multicast_query_timer, jiffies);
1112 void br_multicast_stop(struct net_bridge *br)
1114 struct net_bridge_mdb_htable *mdb;
1115 struct net_bridge_mdb_entry *mp;
1116 struct hlist_node *p, *n;
1117 u32 ver;
1118 int i;
1120 del_timer_sync(&br->multicast_router_timer);
1121 del_timer_sync(&br->multicast_querier_timer);
1122 del_timer_sync(&br->multicast_query_timer);
1124 spin_lock_bh(&br->multicast_lock);
1125 mdb = br->mdb;
1126 if (!mdb)
1127 goto out;
1129 br->mdb = NULL;
1131 ver = mdb->ver;
1132 for (i = 0; i < mdb->max; i++) {
1133 hlist_for_each_entry_safe(mp, p, n, &mdb->mhash[i],
1134 hlist[ver]) {
1135 del_timer(&mp->timer);
1136 del_timer(&mp->query_timer);
1137 call_rcu_bh(&mp->rcu, br_multicast_free_group);
1141 if (mdb->old) {
1142 spin_unlock_bh(&br->multicast_lock);
1143 rcu_barrier_bh();
1144 spin_lock_bh(&br->multicast_lock);
1145 WARN_ON(mdb->old);
1148 mdb->old = mdb;
1149 call_rcu_bh(&mdb->rcu, br_mdb_free);
1151 out:
1152 spin_unlock_bh(&br->multicast_lock);
1155 int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1157 int err = -ENOENT;
1159 spin_lock_bh(&br->multicast_lock);
1160 if (!netif_running(br->dev))
1161 goto unlock;
1163 switch (val) {
1164 case 0:
1165 case 2:
1166 del_timer(&br->multicast_router_timer);
1167 /* fall through */
1168 case 1:
1169 br->multicast_router = val;
1170 err = 0;
1171 break;
1173 default:
1174 err = -EINVAL;
1175 break;
1178 unlock:
1179 spin_unlock_bh(&br->multicast_lock);
1181 return err;
1184 int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1186 struct net_bridge *br = p->br;
1187 int err = -ENOENT;
1189 spin_lock(&br->multicast_lock);
1190 if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED)
1191 goto unlock;
1193 switch (val) {
1194 case 0:
1195 case 1:
1196 case 2:
1197 p->multicast_router = val;
1198 err = 0;
1200 if (val < 2 && !hlist_unhashed(&p->rlist))
1201 hlist_del_init_rcu(&p->rlist);
1203 if (val == 1)
1204 break;
1206 del_timer(&p->multicast_router_timer);
1208 if (val == 0)
1209 break;
1211 br_multicast_add_router(br, p);
1212 break;
1214 default:
1215 err = -EINVAL;
1216 break;
1219 unlock:
1220 spin_unlock(&br->multicast_lock);
1222 return err;
1225 int br_multicast_toggle(struct net_bridge *br, unsigned long val)
1227 struct net_bridge_port *port;
1228 int err = -ENOENT;
1230 spin_lock(&br->multicast_lock);
1231 if (!netif_running(br->dev))
1232 goto unlock;
1234 err = 0;
1235 if (br->multicast_disabled == !val)
1236 goto unlock;
1238 br->multicast_disabled = !val;
1239 if (br->multicast_disabled)
1240 goto unlock;
1242 if (br->mdb) {
1243 if (br->mdb->old) {
1244 err = -EEXIST;
1245 rollback:
1246 br->multicast_disabled = !!val;
1247 goto unlock;
1250 err = br_mdb_rehash(&br->mdb, br->mdb->max,
1251 br->hash_elasticity);
1252 if (err)
1253 goto rollback;
1256 br_multicast_open(br);
1257 list_for_each_entry(port, &br->port_list, list) {
1258 if (port->state == BR_STATE_DISABLED ||
1259 port->state == BR_STATE_BLOCKING)
1260 continue;
1262 __br_multicast_enable_port(port);
1265 unlock:
1266 spin_unlock(&br->multicast_lock);
1268 return err;
1271 int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
1273 int err = -ENOENT;
1274 u32 old;
1276 spin_lock(&br->multicast_lock);
1277 if (!netif_running(br->dev))
1278 goto unlock;
1280 err = -EINVAL;
1281 if (!is_power_of_2(val))
1282 goto unlock;
1283 if (br->mdb && val < br->mdb->size)
1284 goto unlock;
1286 err = 0;
1288 old = br->hash_max;
1289 br->hash_max = val;
1291 if (br->mdb) {
1292 if (br->mdb->old) {
1293 err = -EEXIST;
1294 rollback:
1295 br->hash_max = old;
1296 goto unlock;
1299 err = br_mdb_rehash(&br->mdb, br->hash_max,
1300 br->hash_elasticity);
1301 if (err)
1302 goto rollback;
1305 unlock:
1306 spin_unlock(&br->multicast_lock);
1308 return err;