bridge: Add multicast_router sysfs entries
[linux-2.6.git] / net / bridge / br_multicast.c
1 /*
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>
18 #include <linux/netdevice.h>
19 #include <linux/netfilter_bridge.h>
20 #include <linux/random.h>
21 #include <linux/rculist.h>
22 #include <linux/skbuff.h>
23 #include <linux/slab.h>
24 #include <linux/timer.h>
25 #include <net/ip.h>
26
27 #include "br_private.h"
28
29 static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb, __be32 ip)
30 {
31         return jhash_1word(mdb->secret, (u32)ip) & (mdb->max - 1);
32 }
33
34 static struct net_bridge_mdb_entry *__br_mdb_ip_get(
35         struct net_bridge_mdb_htable *mdb, __be32 dst, int hash)
36 {
37         struct net_bridge_mdb_entry *mp;
38         struct hlist_node *p;
39
40         hlist_for_each_entry(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) {
41                 if (dst == mp->addr)
42                         return mp;
43         }
44
45         return NULL;
46 }
47
48 static struct net_bridge_mdb_entry *br_mdb_ip_get(
49         struct net_bridge_mdb_htable *mdb, __be32 dst)
50 {
51         return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
52 }
53
54 struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
55                                         struct sk_buff *skb)
56 {
57         struct net_bridge_mdb_htable *mdb = br->mdb;
58
59         if (!mdb || br->multicast_disabled)
60                 return NULL;
61
62         switch (skb->protocol) {
63         case htons(ETH_P_IP):
64                 if (BR_INPUT_SKB_CB(skb)->igmp)
65                         break;
66                 return br_mdb_ip_get(mdb, ip_hdr(skb)->daddr);
67         }
68
69         return NULL;
70 }
71
72 static void br_mdb_free(struct rcu_head *head)
73 {
74         struct net_bridge_mdb_htable *mdb =
75                 container_of(head, struct net_bridge_mdb_htable, rcu);
76         struct net_bridge_mdb_htable *old = mdb->old;
77
78         mdb->old = NULL;
79         kfree(old->mhash);
80         kfree(old);
81 }
82
83 static int br_mdb_copy(struct net_bridge_mdb_htable *new,
84                        struct net_bridge_mdb_htable *old,
85                        int elasticity)
86 {
87         struct net_bridge_mdb_entry *mp;
88         struct hlist_node *p;
89         int maxlen;
90         int len;
91         int i;
92
93         for (i = 0; i < old->max; i++)
94                 hlist_for_each_entry(mp, p, &old->mhash[i], hlist[old->ver])
95                         hlist_add_head(&mp->hlist[new->ver],
96                                        &new->mhash[br_ip_hash(new, mp->addr)]);
97
98         if (!elasticity)
99                 return 0;
100
101         maxlen = 0;
102         for (i = 0; i < new->max; i++) {
103                 len = 0;
104                 hlist_for_each_entry(mp, p, &new->mhash[i], hlist[new->ver])
105                         len++;
106                 if (len > maxlen)
107                         maxlen = len;
108         }
109
110         return maxlen > elasticity ? -EINVAL : 0;
111 }
112
113 static void br_multicast_free_pg(struct rcu_head *head)
114 {
115         struct net_bridge_port_group *p =
116                 container_of(head, struct net_bridge_port_group, rcu);
117
118         kfree(p);
119 }
120
121 static void br_multicast_free_group(struct rcu_head *head)
122 {
123         struct net_bridge_mdb_entry *mp =
124                 container_of(head, struct net_bridge_mdb_entry, rcu);
125
126         kfree(mp);
127 }
128
129 static void br_multicast_group_expired(unsigned long data)
130 {
131         struct net_bridge_mdb_entry *mp = (void *)data;
132         struct net_bridge *br = mp->br;
133         struct net_bridge_mdb_htable *mdb;
134
135         spin_lock(&br->multicast_lock);
136         if (!netif_running(br->dev) || timer_pending(&mp->timer))
137                 goto out;
138
139         if (!hlist_unhashed(&mp->mglist))
140                 hlist_del_init(&mp->mglist);
141
142         if (mp->ports)
143                 goto out;
144
145         mdb = br->mdb;
146         hlist_del_rcu(&mp->hlist[mdb->ver]);
147         mdb->size--;
148
149         del_timer(&mp->query_timer);
150         call_rcu_bh(&mp->rcu, br_multicast_free_group);
151
152 out:
153         spin_unlock(&br->multicast_lock);
154 }
155
156 static void br_multicast_del_pg(struct net_bridge *br,
157                                 struct net_bridge_port_group *pg)
158 {
159         struct net_bridge_mdb_htable *mdb = br->mdb;
160         struct net_bridge_mdb_entry *mp;
161         struct net_bridge_port_group *p;
162         struct net_bridge_port_group **pp;
163
164         mp = br_mdb_ip_get(mdb, pg->addr);
165         if (WARN_ON(!mp))
166                 return;
167
168         for (pp = &mp->ports; (p = *pp); pp = &p->next) {
169                 if (p != pg)
170                         continue;
171
172                 *pp = p->next;
173                 hlist_del_init(&p->mglist);
174                 del_timer(&p->timer);
175                 del_timer(&p->query_timer);
176                 call_rcu_bh(&p->rcu, br_multicast_free_pg);
177
178                 if (!mp->ports && hlist_unhashed(&mp->mglist) &&
179                     netif_running(br->dev))
180                         mod_timer(&mp->timer, jiffies);
181
182                 return;
183         }
184
185         WARN_ON(1);
186 }
187
188 static void br_multicast_port_group_expired(unsigned long data)
189 {
190         struct net_bridge_port_group *pg = (void *)data;
191         struct net_bridge *br = pg->port->br;
192
193         spin_lock(&br->multicast_lock);
194         if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
195             hlist_unhashed(&pg->mglist))
196                 goto out;
197
198         br_multicast_del_pg(br, pg);
199
200 out:
201         spin_unlock(&br->multicast_lock);
202 }
203
204 static int br_mdb_rehash(struct net_bridge_mdb_htable **mdbp, int max,
205                          int elasticity)
206 {
207         struct net_bridge_mdb_htable *old = *mdbp;
208         struct net_bridge_mdb_htable *mdb;
209         int err;
210
211         mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
212         if (!mdb)
213                 return -ENOMEM;
214
215         mdb->max = max;
216         mdb->old = old;
217
218         mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
219         if (!mdb->mhash) {
220                 kfree(mdb);
221                 return -ENOMEM;
222         }
223
224         mdb->size = old ? old->size : 0;
225         mdb->ver = old ? old->ver ^ 1 : 0;
226
227         if (!old || elasticity)
228                 get_random_bytes(&mdb->secret, sizeof(mdb->secret));
229         else
230                 mdb->secret = old->secret;
231
232         if (!old)
233                 goto out;
234
235         err = br_mdb_copy(mdb, old, elasticity);
236         if (err) {
237                 kfree(mdb->mhash);
238                 kfree(mdb);
239                 return err;
240         }
241
242         call_rcu_bh(&mdb->rcu, br_mdb_free);
243
244 out:
245         rcu_assign_pointer(*mdbp, mdb);
246
247         return 0;
248 }
249
250 static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
251                                                 __be32 group)
252 {
253         struct sk_buff *skb;
254         struct igmphdr *ih;
255         struct ethhdr *eth;
256         struct iphdr *iph;
257
258         skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
259                                                  sizeof(*ih) + 4);
260         if (!skb)
261                 goto out;
262
263         skb->protocol = htons(ETH_P_IP);
264
265         skb_reset_mac_header(skb);
266         eth = eth_hdr(skb);
267
268         memcpy(eth->h_source, br->dev->dev_addr, 6);
269         eth->h_dest[0] = 1;
270         eth->h_dest[1] = 0;
271         eth->h_dest[2] = 0x5e;
272         eth->h_dest[3] = 0;
273         eth->h_dest[4] = 0;
274         eth->h_dest[5] = 1;
275         eth->h_proto = htons(ETH_P_IP);
276         skb_put(skb, sizeof(*eth));
277
278         skb_set_network_header(skb, skb->len);
279         iph = ip_hdr(skb);
280
281         iph->version = 4;
282         iph->ihl = 6;
283         iph->tos = 0xc0;
284         iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4);
285         iph->id = 0;
286         iph->frag_off = htons(IP_DF);
287         iph->ttl = 1;
288         iph->protocol = IPPROTO_IGMP;
289         iph->saddr = 0;
290         iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
291         ((u8 *)&iph[1])[0] = IPOPT_RA;
292         ((u8 *)&iph[1])[1] = 4;
293         ((u8 *)&iph[1])[2] = 0;
294         ((u8 *)&iph[1])[3] = 0;
295         ip_send_check(iph);
296         skb_put(skb, 24);
297
298         skb_set_transport_header(skb, skb->len);
299         ih = igmp_hdr(skb);
300         ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
301         ih->code = (group ? br->multicast_last_member_interval :
302                             br->multicast_query_response_interval) /
303                    (HZ / IGMP_TIMER_SCALE);
304         ih->group = group;
305         ih->csum = 0;
306         ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
307         skb_put(skb, sizeof(*ih));
308
309         __skb_pull(skb, sizeof(*eth));
310
311 out:
312         return skb;
313 }
314
315 static void br_multicast_send_group_query(struct net_bridge_mdb_entry *mp)
316 {
317         struct net_bridge *br = mp->br;
318         struct sk_buff *skb;
319
320         skb = br_multicast_alloc_query(br, mp->addr);
321         if (!skb)
322                 goto timer;
323
324         netif_rx(skb);
325
326 timer:
327         if (++mp->queries_sent < br->multicast_last_member_count)
328                 mod_timer(&mp->query_timer,
329                           jiffies + br->multicast_last_member_interval);
330 }
331
332 static void br_multicast_group_query_expired(unsigned long data)
333 {
334         struct net_bridge_mdb_entry *mp = (void *)data;
335         struct net_bridge *br = mp->br;
336
337         spin_lock(&br->multicast_lock);
338         if (!netif_running(br->dev) || hlist_unhashed(&mp->mglist) ||
339             mp->queries_sent >= br->multicast_last_member_count)
340                 goto out;
341
342         br_multicast_send_group_query(mp);
343
344 out:
345         spin_unlock(&br->multicast_lock);
346 }
347
348 static void br_multicast_send_port_group_query(struct net_bridge_port_group *pg)
349 {
350         struct net_bridge_port *port = pg->port;
351         struct net_bridge *br = port->br;
352         struct sk_buff *skb;
353
354         skb = br_multicast_alloc_query(br, pg->addr);
355         if (!skb)
356                 goto timer;
357
358         br_deliver(port, skb);
359
360 timer:
361         if (++pg->queries_sent < br->multicast_last_member_count)
362                 mod_timer(&pg->query_timer,
363                           jiffies + br->multicast_last_member_interval);
364 }
365
366 static void br_multicast_port_group_query_expired(unsigned long data)
367 {
368         struct net_bridge_port_group *pg = (void *)data;
369         struct net_bridge_port *port = pg->port;
370         struct net_bridge *br = port->br;
371
372         spin_lock(&br->multicast_lock);
373         if (!netif_running(br->dev) || hlist_unhashed(&pg->mglist) ||
374             pg->queries_sent >= br->multicast_last_member_count)
375                 goto out;
376
377         br_multicast_send_port_group_query(pg);
378
379 out:
380         spin_unlock(&br->multicast_lock);
381 }
382
383 static struct net_bridge_mdb_entry *br_multicast_get_group(
384         struct net_bridge *br, struct net_bridge_port *port, __be32 group,
385         int hash)
386 {
387         struct net_bridge_mdb_htable *mdb = br->mdb;
388         struct net_bridge_mdb_entry *mp;
389         struct hlist_node *p;
390         unsigned count = 0;
391         unsigned max;
392         int elasticity;
393         int err;
394
395         hlist_for_each_entry(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) {
396                 count++;
397                 if (unlikely(group == mp->addr)) {
398                         return mp;
399                 }
400         }
401
402         elasticity = 0;
403         max = mdb->max;
404
405         if (unlikely(count > br->hash_elasticity && count)) {
406                 if (net_ratelimit())
407                         printk(KERN_INFO "%s: Multicast hash table "
408                                "chain limit reached: %s\n",
409                                br->dev->name, port ? port->dev->name :
410                                                      br->dev->name);
411
412                 elasticity = br->hash_elasticity;
413         }
414
415         if (mdb->size >= max) {
416                 max *= 2;
417                 if (unlikely(max >= br->hash_max)) {
418                         printk(KERN_WARNING "%s: Multicast hash table maximum "
419                                "reached, disabling snooping: %s, %d\n",
420                                br->dev->name, port ? port->dev->name :
421                                                      br->dev->name,
422                                max);
423                         err = -E2BIG;
424 disable:
425                         br->multicast_disabled = 1;
426                         goto err;
427                 }
428         }
429
430         if (max > mdb->max || elasticity) {
431                 if (mdb->old) {
432                         if (net_ratelimit())
433                                 printk(KERN_INFO "%s: Multicast hash table "
434                                        "on fire: %s\n",
435                                        br->dev->name, port ? port->dev->name :
436                                                              br->dev->name);
437                         err = -EEXIST;
438                         goto err;
439                 }
440
441                 err = br_mdb_rehash(&br->mdb, max, elasticity);
442                 if (err) {
443                         printk(KERN_WARNING "%s: Cannot rehash multicast "
444                                "hash table, disabling snooping: "
445                                "%s, %d, %d\n",
446                                br->dev->name, port ? port->dev->name :
447                                                      br->dev->name,
448                                mdb->size, err);
449                         goto disable;
450                 }
451
452                 err = -EAGAIN;
453                 goto err;
454         }
455
456         return NULL;
457
458 err:
459         mp = ERR_PTR(err);
460         return mp;
461 }
462
463 static struct net_bridge_mdb_entry *br_multicast_new_group(
464         struct net_bridge *br, struct net_bridge_port *port, __be32 group)
465 {
466         struct net_bridge_mdb_htable *mdb = br->mdb;
467         struct net_bridge_mdb_entry *mp;
468         int hash;
469
470         if (!mdb) {
471                 if (br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0))
472                         return NULL;
473                 goto rehash;
474         }
475
476         hash = br_ip_hash(mdb, group);
477         mp = br_multicast_get_group(br, port, group, hash);
478         switch (PTR_ERR(mp)) {
479         case 0:
480                 break;
481
482         case -EAGAIN:
483 rehash:
484                 mdb = br->mdb;
485                 hash = br_ip_hash(mdb, group);
486                 break;
487
488         default:
489                 goto out;
490         }
491
492         mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
493         if (unlikely(!mp))
494                 goto out;
495
496         mp->br = br;
497         mp->addr = group;
498         setup_timer(&mp->timer, br_multicast_group_expired,
499                     (unsigned long)mp);
500         setup_timer(&mp->query_timer, br_multicast_group_query_expired,
501                     (unsigned long)mp);
502
503         hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
504         mdb->size++;
505
506 out:
507         return mp;
508 }
509
510 static int br_multicast_add_group(struct net_bridge *br,
511                                   struct net_bridge_port *port, __be32 group)
512 {
513         struct net_bridge_mdb_entry *mp;
514         struct net_bridge_port_group *p;
515         struct net_bridge_port_group **pp;
516         unsigned long now = jiffies;
517         int err;
518
519         if (ipv4_is_local_multicast(group))
520                 return 0;
521
522         spin_lock(&br->multicast_lock);
523         if (!netif_running(br->dev) ||
524             (port && port->state == BR_STATE_DISABLED))
525                 goto out;
526
527         mp = br_multicast_new_group(br, port, group);
528         err = PTR_ERR(mp);
529         if (unlikely(IS_ERR(mp) || !mp))
530                 goto err;
531
532         if (!port) {
533                 hlist_add_head(&mp->mglist, &br->mglist);
534                 mod_timer(&mp->timer, now + br->multicast_membership_interval);
535                 goto out;
536         }
537
538         for (pp = &mp->ports; (p = *pp); pp = &p->next) {
539                 if (p->port == port)
540                         goto found;
541                 if ((unsigned long)p->port < (unsigned long)port)
542                         break;
543         }
544
545         p = kzalloc(sizeof(*p), GFP_ATOMIC);
546         err = -ENOMEM;
547         if (unlikely(!p))
548                 goto err;
549
550         p->addr = group;
551         p->port = port;
552         p->next = *pp;
553         hlist_add_head(&p->mglist, &port->mglist);
554         setup_timer(&p->timer, br_multicast_port_group_expired,
555                     (unsigned long)p);
556         setup_timer(&p->query_timer, br_multicast_port_group_query_expired,
557                     (unsigned long)p);
558
559         rcu_assign_pointer(*pp, p);
560
561 found:
562         mod_timer(&p->timer, now + br->multicast_membership_interval);
563 out:
564         err = 0;
565
566 err:
567         spin_unlock(&br->multicast_lock);
568         return err;
569 }
570
571 static void br_multicast_router_expired(unsigned long data)
572 {
573         struct net_bridge_port *port = (void *)data;
574         struct net_bridge *br = port->br;
575
576         spin_lock(&br->multicast_lock);
577         if (port->multicast_router != 1 ||
578             timer_pending(&port->multicast_router_timer) ||
579             hlist_unhashed(&port->rlist))
580                 goto out;
581
582         hlist_del_init_rcu(&port->rlist);
583
584 out:
585         spin_unlock(&br->multicast_lock);
586 }
587
588 static void br_multicast_local_router_expired(unsigned long data)
589 {
590 }
591
592 static void br_multicast_send_query(struct net_bridge *br,
593                                     struct net_bridge_port *port, u32 sent)
594 {
595         unsigned long time;
596         struct sk_buff *skb;
597
598         if (!netif_running(br->dev) || br->multicast_disabled ||
599             timer_pending(&br->multicast_querier_timer))
600                 return;
601
602         skb = br_multicast_alloc_query(br, 0);
603         if (!skb)
604                 goto timer;
605
606         if (port) {
607                 __skb_push(skb, sizeof(struct ethhdr));
608                 skb->dev = port->dev;
609                 NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
610                         dev_queue_xmit);
611         } else
612                 netif_rx(skb);
613
614 timer:
615         time = jiffies;
616         time += sent < br->multicast_startup_query_count ?
617                 br->multicast_startup_query_interval :
618                 br->multicast_query_interval;
619         mod_timer(port ? &port->multicast_query_timer :
620                          &br->multicast_query_timer, time);
621 }
622
623 static void br_multicast_port_query_expired(unsigned long data)
624 {
625         struct net_bridge_port *port = (void *)data;
626         struct net_bridge *br = port->br;
627
628         spin_lock(&br->multicast_lock);
629         if (port && (port->state == BR_STATE_DISABLED ||
630                      port->state == BR_STATE_BLOCKING))
631                 goto out;
632
633         if (port->multicast_startup_queries_sent <
634             br->multicast_startup_query_count)
635                 port->multicast_startup_queries_sent++;
636
637         br_multicast_send_query(port->br, port,
638                                 port->multicast_startup_queries_sent);
639
640 out:
641         spin_unlock(&br->multicast_lock);
642 }
643
644 void br_multicast_add_port(struct net_bridge_port *port)
645 {
646         port->multicast_router = 1;
647
648         setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
649                     (unsigned long)port);
650         setup_timer(&port->multicast_query_timer,
651                     br_multicast_port_query_expired, (unsigned long)port);
652 }
653
654 void br_multicast_del_port(struct net_bridge_port *port)
655 {
656         del_timer_sync(&port->multicast_router_timer);
657 }
658
659 void br_multicast_enable_port(struct net_bridge_port *port)
660 {
661         struct net_bridge *br = port->br;
662
663         spin_lock(&br->multicast_lock);
664         if (br->multicast_disabled || !netif_running(br->dev))
665                 goto out;
666
667         port->multicast_startup_queries_sent = 0;
668
669         if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 ||
670             del_timer(&port->multicast_query_timer))
671                 mod_timer(&port->multicast_query_timer, jiffies);
672
673 out:
674         spin_unlock(&br->multicast_lock);
675 }
676
677 void br_multicast_disable_port(struct net_bridge_port *port)
678 {
679         struct net_bridge *br = port->br;
680         struct net_bridge_port_group *pg;
681         struct hlist_node *p, *n;
682
683         spin_lock(&br->multicast_lock);
684         hlist_for_each_entry_safe(pg, p, n, &port->mglist, mglist)
685                 br_multicast_del_pg(br, pg);
686
687         if (!hlist_unhashed(&port->rlist))
688                 hlist_del_init_rcu(&port->rlist);
689         del_timer(&port->multicast_router_timer);
690         del_timer(&port->multicast_query_timer);
691         spin_unlock(&br->multicast_lock);
692 }
693
694 static int br_multicast_igmp3_report(struct net_bridge *br,
695                                      struct net_bridge_port *port,
696                                      struct sk_buff *skb)
697 {
698         struct igmpv3_report *ih;
699         struct igmpv3_grec *grec;
700         int i;
701         int len;
702         int num;
703         int type;
704         int err = 0;
705         __be32 group;
706
707         if (!pskb_may_pull(skb, sizeof(*ih)))
708                 return -EINVAL;
709
710         ih = igmpv3_report_hdr(skb);
711         num = ntohs(ih->ngrec);
712         len = sizeof(*ih);
713
714         for (i = 0; i < num; i++) {
715                 len += sizeof(*grec);
716                 if (!pskb_may_pull(skb, len))
717                         return -EINVAL;
718
719                 grec = (void *)(skb->data + len);
720                 group = grec->grec_mca;
721                 type = grec->grec_type;
722
723                 len += grec->grec_nsrcs * 4;
724                 if (!pskb_may_pull(skb, len))
725                         return -EINVAL;
726
727                 /* We treat this as an IGMPv2 report for now. */
728                 switch (type) {
729                 case IGMPV3_MODE_IS_INCLUDE:
730                 case IGMPV3_MODE_IS_EXCLUDE:
731                 case IGMPV3_CHANGE_TO_INCLUDE:
732                 case IGMPV3_CHANGE_TO_EXCLUDE:
733                 case IGMPV3_ALLOW_NEW_SOURCES:
734                 case IGMPV3_BLOCK_OLD_SOURCES:
735                         break;
736
737                 default:
738                         continue;
739                 }
740
741                 err = br_multicast_add_group(br, port, group);
742                 if (err)
743                         break;
744         }
745
746         return err;
747 }
748
749 static void br_multicast_add_router(struct net_bridge *br,
750                                     struct net_bridge_port *port)
751 {
752         struct hlist_node *p;
753         struct hlist_node **h;
754
755         for (h = &br->router_list.first;
756              (p = *h) &&
757              (unsigned long)container_of(p, struct net_bridge_port, rlist) >
758              (unsigned long)port;
759              h = &p->next)
760                 ;
761
762         port->rlist.pprev = h;
763         port->rlist.next = p;
764         rcu_assign_pointer(*h, &port->rlist);
765         if (p)
766                 p->pprev = &port->rlist.next;
767 }
768
769 static void br_multicast_mark_router(struct net_bridge *br,
770                                      struct net_bridge_port *port)
771 {
772         unsigned long now = jiffies;
773
774         if (!port) {
775                 if (br->multicast_router == 1)
776                         mod_timer(&br->multicast_router_timer,
777                                   now + br->multicast_querier_interval);
778                 return;
779         }
780
781         if (port->multicast_router != 1)
782                 return;
783
784         if (!hlist_unhashed(&port->rlist))
785                 goto timer;
786
787         br_multicast_add_router(br, port);
788
789 timer:
790         mod_timer(&port->multicast_router_timer,
791                   now + br->multicast_querier_interval);
792 }
793
794 static void br_multicast_query_received(struct net_bridge *br,
795                                         struct net_bridge_port *port,
796                                         __be32 saddr)
797 {
798         if (saddr)
799                 mod_timer(&br->multicast_querier_timer,
800                           jiffies + br->multicast_querier_interval);
801         else if (timer_pending(&br->multicast_querier_timer))
802                 return;
803
804         br_multicast_mark_router(br, port);
805 }
806
807 static int br_multicast_query(struct net_bridge *br,
808                               struct net_bridge_port *port,
809                               struct sk_buff *skb)
810 {
811         struct iphdr *iph = ip_hdr(skb);
812         struct igmphdr *ih = igmp_hdr(skb);
813         struct net_bridge_mdb_entry *mp;
814         struct igmpv3_query *ih3;
815         struct net_bridge_port_group *p;
816         struct net_bridge_port_group **pp;
817         unsigned long max_delay;
818         unsigned long now = jiffies;
819         __be32 group;
820
821         spin_lock(&br->multicast_lock);
822         if (!netif_running(br->dev) ||
823             (port && port->state == BR_STATE_DISABLED))
824                 goto out;
825
826         br_multicast_query_received(br, port, iph->saddr);
827
828         group = ih->group;
829
830         if (skb->len == sizeof(*ih)) {
831                 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
832
833                 if (!max_delay) {
834                         max_delay = 10 * HZ;
835                         group = 0;
836                 }
837         } else {
838                 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
839                         return -EINVAL;
840
841                 ih3 = igmpv3_query_hdr(skb);
842                 if (ih3->nsrcs)
843                         return 0;
844
845                 max_delay = ih3->code ? 1 :
846                             IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE);
847         }
848
849         if (!group)
850                 goto out;
851
852         mp = br_mdb_ip_get(br->mdb, group);
853         if (!mp)
854                 goto out;
855
856         max_delay *= br->multicast_last_member_count;
857
858         if (!hlist_unhashed(&mp->mglist) &&
859             (timer_pending(&mp->timer) ?
860              time_after(mp->timer.expires, now + max_delay) :
861              try_to_del_timer_sync(&mp->timer) >= 0))
862                 mod_timer(&mp->timer, now + max_delay);
863
864         for (pp = &mp->ports; (p = *pp); pp = &p->next) {
865                 if (timer_pending(&p->timer) ?
866                     time_after(p->timer.expires, now + max_delay) :
867                     try_to_del_timer_sync(&p->timer) >= 0)
868                         mod_timer(&mp->timer, now + max_delay);
869         }
870
871 out:
872         spin_unlock(&br->multicast_lock);
873         return 0;
874 }
875
876 static void br_multicast_leave_group(struct net_bridge *br,
877                                      struct net_bridge_port *port,
878                                      __be32 group)
879 {
880         struct net_bridge_mdb_htable *mdb;
881         struct net_bridge_mdb_entry *mp;
882         struct net_bridge_port_group *p;
883         unsigned long now;
884         unsigned long time;
885
886         if (ipv4_is_local_multicast(group))
887                 return;
888
889         spin_lock(&br->multicast_lock);
890         if (!netif_running(br->dev) ||
891             (port && port->state == BR_STATE_DISABLED) ||
892             timer_pending(&br->multicast_querier_timer))
893                 goto out;
894
895         mdb = br->mdb;
896         mp = br_mdb_ip_get(mdb, group);
897         if (!mp)
898                 goto out;
899
900         now = jiffies;
901         time = now + br->multicast_last_member_count *
902                      br->multicast_last_member_interval;
903
904         if (!port) {
905                 if (!hlist_unhashed(&mp->mglist) &&
906                     (timer_pending(&mp->timer) ?
907                      time_after(mp->timer.expires, time) :
908                      try_to_del_timer_sync(&mp->timer) >= 0)) {
909                         mod_timer(&mp->timer, time);
910
911                         mp->queries_sent = 0;
912                         mod_timer(&mp->query_timer, now);
913                 }
914
915                 goto out;
916         }
917
918         for (p = mp->ports; p; p = p->next) {
919                 if (p->port != port)
920                         continue;
921
922                 if (!hlist_unhashed(&p->mglist) &&
923                     (timer_pending(&p->timer) ?
924                      time_after(p->timer.expires, time) :
925                      try_to_del_timer_sync(&p->timer) >= 0)) {
926                         mod_timer(&p->timer, time);
927
928                         p->queries_sent = 0;
929                         mod_timer(&p->query_timer, now);
930                 }
931
932                 break;
933         }
934
935 out:
936         spin_unlock(&br->multicast_lock);
937 }
938
939 static int br_multicast_ipv4_rcv(struct net_bridge *br,
940                                  struct net_bridge_port *port,
941                                  struct sk_buff *skb)
942 {
943         struct sk_buff *skb2 = skb;
944         struct iphdr *iph;
945         struct igmphdr *ih;
946         unsigned len;
947         unsigned offset;
948         int err;
949
950         BR_INPUT_SKB_CB(skb)->igmp = 0;
951         BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
952
953         /* We treat OOM as packet loss for now. */
954         if (!pskb_may_pull(skb, sizeof(*iph)))
955                 return -EINVAL;
956
957         iph = ip_hdr(skb);
958
959         if (iph->ihl < 5 || iph->version != 4)
960                 return -EINVAL;
961
962         if (!pskb_may_pull(skb, ip_hdrlen(skb)))
963                 return -EINVAL;
964
965         iph = ip_hdr(skb);
966
967         if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
968                 return -EINVAL;
969
970         if (iph->protocol != IPPROTO_IGMP)
971                 return 0;
972
973         len = ntohs(iph->tot_len);
974         if (skb->len < len || len < ip_hdrlen(skb))
975                 return -EINVAL;
976
977         if (skb->len > len) {
978                 skb2 = skb_clone(skb, GFP_ATOMIC);
979                 if (!skb2)
980                         return -ENOMEM;
981
982                 err = pskb_trim_rcsum(skb2, len);
983                 if (err)
984                         return err;
985         }
986
987         len -= ip_hdrlen(skb2);
988         offset = skb_network_offset(skb2) + ip_hdrlen(skb2);
989         __skb_pull(skb2, offset);
990         skb_reset_transport_header(skb2);
991
992         err = -EINVAL;
993         if (!pskb_may_pull(skb2, sizeof(*ih)))
994                 goto out;
995
996         iph = ip_hdr(skb2);
997
998         switch (skb2->ip_summed) {
999         case CHECKSUM_COMPLETE:
1000                 if (!csum_fold(skb2->csum))
1001                         break;
1002                 /* fall through */
1003         case CHECKSUM_NONE:
1004                 skb2->csum = 0;
1005                 if (skb_checksum_complete(skb2))
1006                         return -EINVAL;
1007         }
1008
1009         err = 0;
1010
1011         BR_INPUT_SKB_CB(skb)->igmp = 1;
1012         ih = igmp_hdr(skb2);
1013
1014         switch (ih->type) {
1015         case IGMP_HOST_MEMBERSHIP_REPORT:
1016         case IGMPV2_HOST_MEMBERSHIP_REPORT:
1017                 BR_INPUT_SKB_CB(skb2)->mrouters_only = 1;
1018                 err = br_multicast_add_group(br, port, ih->group);
1019                 break;
1020         case IGMPV3_HOST_MEMBERSHIP_REPORT:
1021                 err = br_multicast_igmp3_report(br, port, skb2);
1022                 break;
1023         case IGMP_HOST_MEMBERSHIP_QUERY:
1024                 err = br_multicast_query(br, port, skb2);
1025                 break;
1026         case IGMP_HOST_LEAVE_MESSAGE:
1027                 br_multicast_leave_group(br, port, ih->group);
1028                 break;
1029         }
1030
1031 out:
1032         __skb_push(skb2, offset);
1033         if (skb2 != skb)
1034                 kfree_skb(skb2);
1035         return err;
1036 }
1037
1038 int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1039                      struct sk_buff *skb)
1040 {
1041         if (br->multicast_disabled)
1042                 return 0;
1043
1044         switch (skb->protocol) {
1045         case htons(ETH_P_IP):
1046                 return br_multicast_ipv4_rcv(br, port, skb);
1047         }
1048
1049         return 0;
1050 }
1051
1052 static void br_multicast_query_expired(unsigned long data)
1053 {
1054         struct net_bridge *br = (void *)data;
1055
1056         spin_lock(&br->multicast_lock);
1057         if (br->multicast_startup_queries_sent <
1058             br->multicast_startup_query_count)
1059                 br->multicast_startup_queries_sent++;
1060
1061         br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent);
1062
1063         spin_unlock(&br->multicast_lock);
1064 }
1065
1066 void br_multicast_init(struct net_bridge *br)
1067 {
1068         br->hash_elasticity = 4;
1069         br->hash_max = 512;
1070
1071         br->multicast_router = 1;
1072         br->multicast_last_member_count = 2;
1073         br->multicast_startup_query_count = 2;
1074
1075         br->multicast_last_member_interval = HZ;
1076         br->multicast_query_response_interval = 10 * HZ;
1077         br->multicast_startup_query_interval = 125 * HZ / 4;
1078         br->multicast_query_interval = 125 * HZ;
1079         br->multicast_querier_interval = 255 * HZ;
1080         br->multicast_membership_interval = 260 * HZ;
1081
1082         spin_lock_init(&br->multicast_lock);
1083         setup_timer(&br->multicast_router_timer,
1084                     br_multicast_local_router_expired, 0);
1085         setup_timer(&br->multicast_querier_timer,
1086                     br_multicast_local_router_expired, 0);
1087         setup_timer(&br->multicast_query_timer, br_multicast_query_expired,
1088                     (unsigned long)br);
1089 }
1090
1091 void br_multicast_open(struct net_bridge *br)
1092 {
1093         br->multicast_startup_queries_sent = 0;
1094
1095         if (br->multicast_disabled)
1096                 return;
1097
1098         mod_timer(&br->multicast_query_timer, jiffies);
1099 }
1100
1101 void br_multicast_stop(struct net_bridge *br)
1102 {
1103         struct net_bridge_mdb_htable *mdb;
1104         struct net_bridge_mdb_entry *mp;
1105         struct hlist_node *p, *n;
1106         u32 ver;
1107         int i;
1108
1109         del_timer_sync(&br->multicast_router_timer);
1110         del_timer_sync(&br->multicast_querier_timer);
1111         del_timer_sync(&br->multicast_query_timer);
1112
1113         spin_lock_bh(&br->multicast_lock);
1114         mdb = br->mdb;
1115         if (!mdb)
1116                 goto out;
1117
1118         br->mdb = NULL;
1119
1120         ver = mdb->ver;
1121         for (i = 0; i < mdb->max; i++) {
1122                 hlist_for_each_entry_safe(mp, p, n, &mdb->mhash[i],
1123                                           hlist[ver]) {
1124                         del_timer(&mp->timer);
1125                         del_timer(&mp->query_timer);
1126                         call_rcu_bh(&mp->rcu, br_multicast_free_group);
1127                 }
1128         }
1129
1130         if (mdb->old) {
1131                 spin_unlock_bh(&br->multicast_lock);
1132                 synchronize_rcu_bh();
1133                 spin_lock_bh(&br->multicast_lock);
1134                 WARN_ON(mdb->old);
1135         }
1136
1137         mdb->old = mdb;
1138         call_rcu_bh(&mdb->rcu, br_mdb_free);
1139
1140 out:
1141         spin_unlock_bh(&br->multicast_lock);
1142 }
1143
1144 int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1145 {
1146         int err = -ENOENT;
1147
1148         spin_lock_bh(&br->multicast_lock);
1149         if (!netif_running(br->dev))
1150                 goto unlock;
1151
1152         switch (val) {
1153         case 0:
1154         case 2:
1155                 del_timer(&br->multicast_router_timer);
1156                 /* fall through */
1157         case 1:
1158                 br->multicast_router = val;
1159                 err = 0;
1160                 break;
1161
1162         default:
1163                 err = -EINVAL;
1164                 break;
1165         }
1166
1167 unlock:
1168         spin_unlock_bh(&br->multicast_lock);
1169
1170         return err;
1171 }
1172
1173 int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1174 {
1175         struct net_bridge *br = p->br;
1176         int err = -ENOENT;
1177
1178         spin_lock(&br->multicast_lock);
1179         if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED)
1180                 goto unlock;
1181
1182         switch (val) {
1183         case 0:
1184         case 1:
1185         case 2:
1186                 p->multicast_router = val;
1187                 err = 0;
1188
1189                 if (val < 2 && !hlist_unhashed(&p->rlist))
1190                         hlist_del_init_rcu(&p->rlist);
1191
1192                 if (val == 1)
1193                         break;
1194
1195                 del_timer(&p->multicast_router_timer);
1196
1197                 if (val == 0)
1198                         break;
1199
1200                 br_multicast_add_router(br, p);
1201                 break;
1202
1203         default:
1204                 err = -EINVAL;
1205                 break;
1206         }
1207
1208 unlock:
1209         spin_unlock(&br->multicast_lock);
1210
1211         return err;
1212 }