net: bridge: convert MLDv2 Query MRC into msecs_to_jiffies for max_delay
[pandora-kernel.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/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>
27 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
28 #include <net/ipv6.h>
29 #include <net/mld.h>
30 #include <net/addrconf.h>
31 #include <net/ip6_checksum.h>
32 #endif
33
34 #include "br_private.h"
35
36 #define mlock_dereference(X, br) \
37         rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock))
38
39 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
40 static inline int ipv6_is_transient_multicast(const struct in6_addr *addr)
41 {
42         if (ipv6_addr_is_multicast(addr) && IPV6_ADDR_MC_FLAG_TRANSIENT(addr))
43                 return 1;
44         return 0;
45 }
46 #endif
47
48 static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
49 {
50         if (a->proto != b->proto)
51                 return 0;
52         switch (a->proto) {
53         case htons(ETH_P_IP):
54                 return a->u.ip4 == b->u.ip4;
55 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
56         case htons(ETH_P_IPV6):
57                 return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
58 #endif
59         }
60         return 0;
61 }
62
63 static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip)
64 {
65         return jhash_1word(mdb->secret, (__force u32)ip) & (mdb->max - 1);
66 }
67
68 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
69 static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb,
70                                 const struct in6_addr *ip)
71 {
72         return jhash2((__force u32 *)ip->s6_addr32, 4, mdb->secret) & (mdb->max - 1);
73 }
74 #endif
75
76 static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb,
77                              struct br_ip *ip)
78 {
79         switch (ip->proto) {
80         case htons(ETH_P_IP):
81                 return __br_ip4_hash(mdb, ip->u.ip4);
82 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
83         case htons(ETH_P_IPV6):
84                 return __br_ip6_hash(mdb, &ip->u.ip6);
85 #endif
86         }
87         return 0;
88 }
89
90 static struct net_bridge_mdb_entry *__br_mdb_ip_get(
91         struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash)
92 {
93         struct net_bridge_mdb_entry *mp;
94         struct hlist_node *p;
95
96         hlist_for_each_entry_rcu(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) {
97                 if (br_ip_equal(&mp->addr, dst))
98                         return mp;
99         }
100
101         return NULL;
102 }
103
104 static struct net_bridge_mdb_entry *br_mdb_ip_get(
105         struct net_bridge_mdb_htable *mdb, struct br_ip *dst)
106 {
107         if (!mdb)
108                 return NULL;
109
110         return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
111 }
112
113 static struct net_bridge_mdb_entry *br_mdb_ip4_get(
114         struct net_bridge_mdb_htable *mdb, __be32 dst)
115 {
116         struct br_ip br_dst;
117
118         br_dst.u.ip4 = dst;
119         br_dst.proto = htons(ETH_P_IP);
120
121         return br_mdb_ip_get(mdb, &br_dst);
122 }
123
124 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
125 static struct net_bridge_mdb_entry *br_mdb_ip6_get(
126         struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst)
127 {
128         struct br_ip br_dst;
129
130         ipv6_addr_copy(&br_dst.u.ip6, dst);
131         br_dst.proto = htons(ETH_P_IPV6);
132
133         return br_mdb_ip_get(mdb, &br_dst);
134 }
135 #endif
136
137 struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
138                                         struct sk_buff *skb)
139 {
140         struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb);
141         struct br_ip ip;
142
143         if (br->multicast_disabled)
144                 return NULL;
145
146         if (BR_INPUT_SKB_CB(skb)->igmp)
147                 return NULL;
148
149         ip.proto = skb->protocol;
150
151         switch (skb->protocol) {
152         case htons(ETH_P_IP):
153                 ip.u.ip4 = ip_hdr(skb)->daddr;
154                 break;
155 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
156         case htons(ETH_P_IPV6):
157                 ipv6_addr_copy(&ip.u.ip6, &ipv6_hdr(skb)->daddr);
158                 break;
159 #endif
160         default:
161                 return NULL;
162         }
163
164         return br_mdb_ip_get(mdb, &ip);
165 }
166
167 static void br_mdb_free(struct rcu_head *head)
168 {
169         struct net_bridge_mdb_htable *mdb =
170                 container_of(head, struct net_bridge_mdb_htable, rcu);
171         struct net_bridge_mdb_htable *old = mdb->old;
172
173         mdb->old = NULL;
174         kfree(old->mhash);
175         kfree(old);
176 }
177
178 static int br_mdb_copy(struct net_bridge_mdb_htable *new,
179                        struct net_bridge_mdb_htable *old,
180                        int elasticity)
181 {
182         struct net_bridge_mdb_entry *mp;
183         struct hlist_node *p;
184         int maxlen;
185         int len;
186         int i;
187
188         for (i = 0; i < old->max; i++)
189                 hlist_for_each_entry(mp, p, &old->mhash[i], hlist[old->ver])
190                         hlist_add_head(&mp->hlist[new->ver],
191                                        &new->mhash[br_ip_hash(new, &mp->addr)]);
192
193         if (!elasticity)
194                 return 0;
195
196         maxlen = 0;
197         for (i = 0; i < new->max; i++) {
198                 len = 0;
199                 hlist_for_each_entry(mp, p, &new->mhash[i], hlist[new->ver])
200                         len++;
201                 if (len > maxlen)
202                         maxlen = len;
203         }
204
205         return maxlen > elasticity ? -EINVAL : 0;
206 }
207
208 static void br_multicast_free_pg(struct rcu_head *head)
209 {
210         struct net_bridge_port_group *p =
211                 container_of(head, struct net_bridge_port_group, rcu);
212
213         kfree(p);
214 }
215
216 static void br_multicast_free_group(struct rcu_head *head)
217 {
218         struct net_bridge_mdb_entry *mp =
219                 container_of(head, struct net_bridge_mdb_entry, rcu);
220
221         kfree(mp);
222 }
223
224 static void br_multicast_group_expired(unsigned long data)
225 {
226         struct net_bridge_mdb_entry *mp = (void *)data;
227         struct net_bridge *br = mp->br;
228         struct net_bridge_mdb_htable *mdb;
229
230         spin_lock(&br->multicast_lock);
231         if (!netif_running(br->dev) || timer_pending(&mp->timer))
232                 goto out;
233
234         mp->mglist = false;
235
236         if (mp->ports)
237                 goto out;
238
239         mdb = mlock_dereference(br->mdb, br);
240
241         hlist_del_rcu(&mp->hlist[mdb->ver]);
242         mdb->size--;
243
244         call_rcu_bh(&mp->rcu, br_multicast_free_group);
245
246 out:
247         spin_unlock(&br->multicast_lock);
248 }
249
250 static void br_multicast_del_pg(struct net_bridge *br,
251                                 struct net_bridge_port_group *pg)
252 {
253         struct net_bridge_mdb_htable *mdb;
254         struct net_bridge_mdb_entry *mp;
255         struct net_bridge_port_group *p;
256         struct net_bridge_port_group __rcu **pp;
257
258         mdb = mlock_dereference(br->mdb, br);
259
260         mp = br_mdb_ip_get(mdb, &pg->addr);
261         if (WARN_ON(!mp))
262                 return;
263
264         for (pp = &mp->ports;
265              (p = mlock_dereference(*pp, br)) != NULL;
266              pp = &p->next) {
267                 if (p != pg)
268                         continue;
269
270                 rcu_assign_pointer(*pp, p->next);
271                 hlist_del_init(&p->mglist);
272                 del_timer(&p->timer);
273                 call_rcu_bh(&p->rcu, br_multicast_free_pg);
274
275                 if (!mp->ports && !mp->mglist &&
276                     netif_running(br->dev))
277                         mod_timer(&mp->timer, jiffies);
278
279                 return;
280         }
281
282         WARN_ON(1);
283 }
284
285 static void br_multicast_port_group_expired(unsigned long data)
286 {
287         struct net_bridge_port_group *pg = (void *)data;
288         struct net_bridge *br = pg->port->br;
289
290         spin_lock(&br->multicast_lock);
291         if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
292             hlist_unhashed(&pg->mglist))
293                 goto out;
294
295         br_multicast_del_pg(br, pg);
296
297 out:
298         spin_unlock(&br->multicast_lock);
299 }
300
301 static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max,
302                          int elasticity)
303 {
304         struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1);
305         struct net_bridge_mdb_htable *mdb;
306         int err;
307
308         mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
309         if (!mdb)
310                 return -ENOMEM;
311
312         mdb->max = max;
313         mdb->old = old;
314
315         mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
316         if (!mdb->mhash) {
317                 kfree(mdb);
318                 return -ENOMEM;
319         }
320
321         mdb->size = old ? old->size : 0;
322         mdb->ver = old ? old->ver ^ 1 : 0;
323
324         if (!old || elasticity)
325                 get_random_bytes(&mdb->secret, sizeof(mdb->secret));
326         else
327                 mdb->secret = old->secret;
328
329         if (!old)
330                 goto out;
331
332         err = br_mdb_copy(mdb, old, elasticity);
333         if (err) {
334                 kfree(mdb->mhash);
335                 kfree(mdb);
336                 return err;
337         }
338
339         call_rcu_bh(&mdb->rcu, br_mdb_free);
340
341 out:
342         rcu_assign_pointer(*mdbp, mdb);
343
344         return 0;
345 }
346
347 static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
348                                                     __be32 group)
349 {
350         struct sk_buff *skb;
351         struct igmphdr *ih;
352         struct ethhdr *eth;
353         struct iphdr *iph;
354
355         skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
356                                                  sizeof(*ih) + 4);
357         if (!skb)
358                 goto out;
359
360         skb->protocol = htons(ETH_P_IP);
361
362         skb_reset_mac_header(skb);
363         eth = eth_hdr(skb);
364
365         memcpy(eth->h_source, br->dev->dev_addr, 6);
366         eth->h_dest[0] = 1;
367         eth->h_dest[1] = 0;
368         eth->h_dest[2] = 0x5e;
369         eth->h_dest[3] = 0;
370         eth->h_dest[4] = 0;
371         eth->h_dest[5] = 1;
372         eth->h_proto = htons(ETH_P_IP);
373         skb_put(skb, sizeof(*eth));
374
375         skb_set_network_header(skb, skb->len);
376         iph = ip_hdr(skb);
377
378         iph->version = 4;
379         iph->ihl = 6;
380         iph->tos = 0xc0;
381         iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4);
382         iph->id = 0;
383         iph->frag_off = htons(IP_DF);
384         iph->ttl = 1;
385         iph->protocol = IPPROTO_IGMP;
386         iph->saddr = 0;
387         iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
388         ((u8 *)&iph[1])[0] = IPOPT_RA;
389         ((u8 *)&iph[1])[1] = 4;
390         ((u8 *)&iph[1])[2] = 0;
391         ((u8 *)&iph[1])[3] = 0;
392         ip_send_check(iph);
393         skb_put(skb, 24);
394
395         skb_set_transport_header(skb, skb->len);
396         ih = igmp_hdr(skb);
397         ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
398         ih->code = (group ? br->multicast_last_member_interval :
399                             br->multicast_query_response_interval) /
400                    (HZ / IGMP_TIMER_SCALE);
401         ih->group = group;
402         ih->csum = 0;
403         ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
404         skb_put(skb, sizeof(*ih));
405
406         __skb_pull(skb, sizeof(*eth));
407
408 out:
409         return skb;
410 }
411
412 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
413 static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
414                                                     const struct in6_addr *group)
415 {
416         struct sk_buff *skb;
417         struct ipv6hdr *ip6h;
418         struct mld_msg *mldq;
419         struct ethhdr *eth;
420         u8 *hopopt;
421         unsigned long interval;
422
423         skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
424                                                  8 + sizeof(*mldq));
425         if (!skb)
426                 goto out;
427
428         skb->protocol = htons(ETH_P_IPV6);
429
430         /* Ethernet header */
431         skb_reset_mac_header(skb);
432         eth = eth_hdr(skb);
433
434         memcpy(eth->h_source, br->dev->dev_addr, 6);
435         eth->h_proto = htons(ETH_P_IPV6);
436         skb_put(skb, sizeof(*eth));
437
438         /* IPv6 header + HbH option */
439         skb_set_network_header(skb, skb->len);
440         ip6h = ipv6_hdr(skb);
441
442         *(__force __be32 *)ip6h = htonl(0x60000000);
443         ip6h->payload_len = htons(8 + sizeof(*mldq));
444         ip6h->nexthdr = IPPROTO_HOPOPTS;
445         ip6h->hop_limit = 1;
446         ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
447         if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
448                                &ip6h->saddr)) {
449                 kfree_skb(skb);
450                 return NULL;
451         }
452         ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
453
454         hopopt = (u8 *)(ip6h + 1);
455         hopopt[0] = IPPROTO_ICMPV6;             /* next hdr */
456         hopopt[1] = 0;                          /* length of HbH */
457         hopopt[2] = IPV6_TLV_ROUTERALERT;       /* Router Alert */
458         hopopt[3] = 2;                          /* Length of RA Option */
459         hopopt[4] = 0;                          /* Type = 0x0000 (MLD) */
460         hopopt[5] = 0;
461         hopopt[6] = IPV6_TLV_PAD0;              /* Pad0 */
462         hopopt[7] = IPV6_TLV_PAD0;              /* Pad0 */
463
464         skb_put(skb, sizeof(*ip6h) + 8);
465
466         /* ICMPv6 */
467         skb_set_transport_header(skb, skb->len);
468         mldq = (struct mld_msg *) icmp6_hdr(skb);
469
470         interval = ipv6_addr_any(group) ?
471                         br->multicast_query_response_interval :
472                         br->multicast_last_member_interval;
473
474         mldq->mld_type = ICMPV6_MGM_QUERY;
475         mldq->mld_code = 0;
476         mldq->mld_cksum = 0;
477         mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
478         mldq->mld_reserved = 0;
479         ipv6_addr_copy(&mldq->mld_mca, group);
480
481         /* checksum */
482         mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
483                                           sizeof(*mldq), IPPROTO_ICMPV6,
484                                           csum_partial(mldq,
485                                                        sizeof(*mldq), 0));
486         skb_put(skb, sizeof(*mldq));
487
488         __skb_pull(skb, sizeof(*eth));
489
490 out:
491         return skb;
492 }
493 #endif
494
495 static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
496                                                 struct br_ip *addr)
497 {
498         switch (addr->proto) {
499         case htons(ETH_P_IP):
500                 return br_ip4_multicast_alloc_query(br, addr->u.ip4);
501 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
502         case htons(ETH_P_IPV6):
503                 return br_ip6_multicast_alloc_query(br, &addr->u.ip6);
504 #endif
505         }
506         return NULL;
507 }
508
509 static struct net_bridge_mdb_entry *br_multicast_get_group(
510         struct net_bridge *br, struct net_bridge_port *port,
511         struct br_ip *group, int hash)
512 {
513         struct net_bridge_mdb_htable *mdb;
514         struct net_bridge_mdb_entry *mp;
515         struct hlist_node *p;
516         unsigned count = 0;
517         unsigned max;
518         int elasticity;
519         int err;
520
521         mdb = rcu_dereference_protected(br->mdb, 1);
522         hlist_for_each_entry(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) {
523                 count++;
524                 if (unlikely(br_ip_equal(group, &mp->addr)))
525                         return mp;
526         }
527
528         elasticity = 0;
529         max = mdb->max;
530
531         if (unlikely(count > br->hash_elasticity && count)) {
532                 if (net_ratelimit())
533                         br_info(br, "Multicast hash table "
534                                 "chain limit reached: %s\n",
535                                 port ? port->dev->name : br->dev->name);
536
537                 elasticity = br->hash_elasticity;
538         }
539
540         if (mdb->size >= max) {
541                 max *= 2;
542                 if (unlikely(max >= br->hash_max)) {
543                         br_warn(br, "Multicast hash table maximum "
544                                 "reached, disabling snooping: %s, %d\n",
545                                 port ? port->dev->name : br->dev->name, max);
546                         err = -E2BIG;
547 disable:
548                         br->multicast_disabled = 1;
549                         goto err;
550                 }
551         }
552
553         if (max > mdb->max || elasticity) {
554                 if (mdb->old) {
555                         if (net_ratelimit())
556                                 br_info(br, "Multicast hash table "
557                                         "on fire: %s\n",
558                                         port ? port->dev->name : br->dev->name);
559                         err = -EEXIST;
560                         goto err;
561                 }
562
563                 err = br_mdb_rehash(&br->mdb, max, elasticity);
564                 if (err) {
565                         br_warn(br, "Cannot rehash multicast "
566                                 "hash table, disabling snooping: %s, %d, %d\n",
567                                 port ? port->dev->name : br->dev->name,
568                                 mdb->size, err);
569                         goto disable;
570                 }
571
572                 err = -EAGAIN;
573                 goto err;
574         }
575
576         return NULL;
577
578 err:
579         mp = ERR_PTR(err);
580         return mp;
581 }
582
583 static struct net_bridge_mdb_entry *br_multicast_new_group(
584         struct net_bridge *br, struct net_bridge_port *port,
585         struct br_ip *group)
586 {
587         struct net_bridge_mdb_htable *mdb;
588         struct net_bridge_mdb_entry *mp;
589         int hash;
590         int err;
591
592         mdb = rcu_dereference_protected(br->mdb, 1);
593         if (!mdb) {
594                 err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0);
595                 if (err)
596                         return ERR_PTR(err);
597                 goto rehash;
598         }
599
600         hash = br_ip_hash(mdb, group);
601         mp = br_multicast_get_group(br, port, group, hash);
602         switch (PTR_ERR(mp)) {
603         case 0:
604                 break;
605
606         case -EAGAIN:
607 rehash:
608                 mdb = rcu_dereference_protected(br->mdb, 1);
609                 hash = br_ip_hash(mdb, group);
610                 break;
611
612         default:
613                 goto out;
614         }
615
616         mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
617         if (unlikely(!mp))
618                 return ERR_PTR(-ENOMEM);
619
620         mp->br = br;
621         mp->addr = *group;
622         setup_timer(&mp->timer, br_multicast_group_expired,
623                     (unsigned long)mp);
624
625         hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
626         mdb->size++;
627
628 out:
629         return mp;
630 }
631
632 static int br_multicast_add_group(struct net_bridge *br,
633                                   struct net_bridge_port *port,
634                                   struct br_ip *group)
635 {
636         struct net_bridge_mdb_entry *mp;
637         struct net_bridge_port_group *p;
638         struct net_bridge_port_group __rcu **pp;
639         unsigned long now = jiffies;
640         int err;
641
642         spin_lock(&br->multicast_lock);
643         if (!netif_running(br->dev) ||
644             (port && port->state == BR_STATE_DISABLED))
645                 goto out;
646
647         mp = br_multicast_new_group(br, port, group);
648         err = PTR_ERR(mp);
649         if (IS_ERR(mp))
650                 goto err;
651
652         if (!port) {
653                 mp->mglist = true;
654                 mod_timer(&mp->timer, now + br->multicast_membership_interval);
655                 goto out;
656         }
657
658         for (pp = &mp->ports;
659              (p = mlock_dereference(*pp, br)) != NULL;
660              pp = &p->next) {
661                 if (p->port == port)
662                         goto found;
663                 if ((unsigned long)p->port < (unsigned long)port)
664                         break;
665         }
666
667         p = kzalloc(sizeof(*p), GFP_ATOMIC);
668         err = -ENOMEM;
669         if (unlikely(!p))
670                 goto err;
671
672         p->addr = *group;
673         p->port = port;
674         p->next = *pp;
675         hlist_add_head(&p->mglist, &port->mglist);
676         setup_timer(&p->timer, br_multicast_port_group_expired,
677                     (unsigned long)p);
678
679         rcu_assign_pointer(*pp, p);
680
681 found:
682         mod_timer(&p->timer, now + br->multicast_membership_interval);
683 out:
684         err = 0;
685
686 err:
687         spin_unlock(&br->multicast_lock);
688         return err;
689 }
690
691 static int br_ip4_multicast_add_group(struct net_bridge *br,
692                                       struct net_bridge_port *port,
693                                       __be32 group)
694 {
695         struct br_ip br_group;
696
697         if (ipv4_is_local_multicast(group))
698                 return 0;
699
700         br_group.u.ip4 = group;
701         br_group.proto = htons(ETH_P_IP);
702
703         return br_multicast_add_group(br, port, &br_group);
704 }
705
706 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
707 static int br_ip6_multicast_add_group(struct net_bridge *br,
708                                       struct net_bridge_port *port,
709                                       const struct in6_addr *group)
710 {
711         struct br_ip br_group;
712
713         if (!ipv6_is_transient_multicast(group))
714                 return 0;
715
716         ipv6_addr_copy(&br_group.u.ip6, group);
717         br_group.proto = htons(ETH_P_IPV6);
718
719         return br_multicast_add_group(br, port, &br_group);
720 }
721 #endif
722
723 static void br_multicast_router_expired(unsigned long data)
724 {
725         struct net_bridge_port *port = (void *)data;
726         struct net_bridge *br = port->br;
727
728         spin_lock(&br->multicast_lock);
729         if (port->multicast_router != 1 ||
730             timer_pending(&port->multicast_router_timer) ||
731             hlist_unhashed(&port->rlist))
732                 goto out;
733
734         hlist_del_init_rcu(&port->rlist);
735
736 out:
737         spin_unlock(&br->multicast_lock);
738 }
739
740 static void br_multicast_local_router_expired(unsigned long data)
741 {
742 }
743
744 static void __br_multicast_send_query(struct net_bridge *br,
745                                       struct net_bridge_port *port,
746                                       struct br_ip *ip)
747 {
748         struct sk_buff *skb;
749
750         skb = br_multicast_alloc_query(br, ip);
751         if (!skb)
752                 return;
753
754         if (port) {
755                 __skb_push(skb, sizeof(struct ethhdr));
756                 skb->dev = port->dev;
757                 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
758                         dev_queue_xmit);
759         } else
760                 netif_rx(skb);
761 }
762
763 static void br_multicast_send_query(struct net_bridge *br,
764                                     struct net_bridge_port *port, u32 sent)
765 {
766         unsigned long time;
767         struct br_ip br_group;
768
769         if (!netif_running(br->dev) || br->multicast_disabled ||
770             timer_pending(&br->multicast_querier_timer))
771                 return;
772
773         memset(&br_group.u, 0, sizeof(br_group.u));
774
775         br_group.proto = htons(ETH_P_IP);
776         __br_multicast_send_query(br, port, &br_group);
777
778 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
779         br_group.proto = htons(ETH_P_IPV6);
780         __br_multicast_send_query(br, port, &br_group);
781 #endif
782
783         time = jiffies;
784         time += sent < br->multicast_startup_query_count ?
785                 br->multicast_startup_query_interval :
786                 br->multicast_query_interval;
787         mod_timer(port ? &port->multicast_query_timer :
788                          &br->multicast_query_timer, time);
789 }
790
791 static void br_multicast_port_query_expired(unsigned long data)
792 {
793         struct net_bridge_port *port = (void *)data;
794         struct net_bridge *br = port->br;
795
796         spin_lock(&br->multicast_lock);
797         if (port->state == BR_STATE_DISABLED ||
798             port->state == BR_STATE_BLOCKING)
799                 goto out;
800
801         if (port->multicast_startup_queries_sent <
802             br->multicast_startup_query_count)
803                 port->multicast_startup_queries_sent++;
804
805         br_multicast_send_query(port->br, port,
806                                 port->multicast_startup_queries_sent);
807
808 out:
809         spin_unlock(&br->multicast_lock);
810 }
811
812 void br_multicast_add_port(struct net_bridge_port *port)
813 {
814         port->multicast_router = 1;
815
816         setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
817                     (unsigned long)port);
818         setup_timer(&port->multicast_query_timer,
819                     br_multicast_port_query_expired, (unsigned long)port);
820 }
821
822 void br_multicast_del_port(struct net_bridge_port *port)
823 {
824         del_timer_sync(&port->multicast_router_timer);
825 }
826
827 static void __br_multicast_enable_port(struct net_bridge_port *port)
828 {
829         port->multicast_startup_queries_sent = 0;
830
831         if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 ||
832             del_timer(&port->multicast_query_timer))
833                 mod_timer(&port->multicast_query_timer, jiffies);
834 }
835
836 void br_multicast_enable_port(struct net_bridge_port *port)
837 {
838         struct net_bridge *br = port->br;
839
840         spin_lock(&br->multicast_lock);
841         if (br->multicast_disabled || !netif_running(br->dev))
842                 goto out;
843
844         __br_multicast_enable_port(port);
845
846 out:
847         spin_unlock(&br->multicast_lock);
848 }
849
850 void br_multicast_disable_port(struct net_bridge_port *port)
851 {
852         struct net_bridge *br = port->br;
853         struct net_bridge_port_group *pg;
854         struct hlist_node *p, *n;
855
856         spin_lock(&br->multicast_lock);
857         hlist_for_each_entry_safe(pg, p, n, &port->mglist, mglist)
858                 br_multicast_del_pg(br, pg);
859
860         if (!hlist_unhashed(&port->rlist))
861                 hlist_del_init_rcu(&port->rlist);
862         del_timer(&port->multicast_router_timer);
863         del_timer(&port->multicast_query_timer);
864         spin_unlock(&br->multicast_lock);
865 }
866
867 static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
868                                          struct net_bridge_port *port,
869                                          struct sk_buff *skb)
870 {
871         struct igmpv3_report *ih;
872         struct igmpv3_grec *grec;
873         int i;
874         int len;
875         int num;
876         int type;
877         int err = 0;
878         __be32 group;
879
880         if (!pskb_may_pull(skb, sizeof(*ih)))
881                 return -EINVAL;
882
883         ih = igmpv3_report_hdr(skb);
884         num = ntohs(ih->ngrec);
885         len = sizeof(*ih);
886
887         for (i = 0; i < num; i++) {
888                 len += sizeof(*grec);
889                 if (!pskb_may_pull(skb, len))
890                         return -EINVAL;
891
892                 grec = (void *)(skb->data + len - sizeof(*grec));
893                 group = grec->grec_mca;
894                 type = grec->grec_type;
895
896                 len += ntohs(grec->grec_nsrcs) * 4;
897                 if (!pskb_may_pull(skb, len))
898                         return -EINVAL;
899
900                 /* We treat this as an IGMPv2 report for now. */
901                 switch (type) {
902                 case IGMPV3_MODE_IS_INCLUDE:
903                 case IGMPV3_MODE_IS_EXCLUDE:
904                 case IGMPV3_CHANGE_TO_INCLUDE:
905                 case IGMPV3_CHANGE_TO_EXCLUDE:
906                 case IGMPV3_ALLOW_NEW_SOURCES:
907                 case IGMPV3_BLOCK_OLD_SOURCES:
908                         break;
909
910                 default:
911                         continue;
912                 }
913
914                 err = br_ip4_multicast_add_group(br, port, group);
915                 if (err)
916                         break;
917         }
918
919         return err;
920 }
921
922 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
923 static int br_ip6_multicast_mld2_report(struct net_bridge *br,
924                                         struct net_bridge_port *port,
925                                         struct sk_buff *skb)
926 {
927         struct icmp6hdr *icmp6h;
928         struct mld2_grec *grec;
929         int i;
930         int len;
931         int num;
932         int err = 0;
933
934         if (!pskb_may_pull(skb, sizeof(*icmp6h)))
935                 return -EINVAL;
936
937         icmp6h = icmp6_hdr(skb);
938         num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
939         len = sizeof(*icmp6h);
940
941         for (i = 0; i < num; i++) {
942                 __be16 *nsrcs, _nsrcs;
943
944                 nsrcs = skb_header_pointer(skb,
945                                            len + offsetof(struct mld2_grec,
946                                                           grec_nsrcs),
947                                            sizeof(_nsrcs), &_nsrcs);
948                 if (!nsrcs)
949                         return -EINVAL;
950
951                 if (!pskb_may_pull(skb,
952                                    len + sizeof(*grec) +
953                                    sizeof(struct in6_addr) * ntohs(*nsrcs)))
954                         return -EINVAL;
955
956                 grec = (struct mld2_grec *)(skb->data + len);
957                 len += sizeof(*grec) +
958                        sizeof(struct in6_addr) * ntohs(*nsrcs);
959
960                 /* We treat these as MLDv1 reports for now. */
961                 switch (grec->grec_type) {
962                 case MLD2_MODE_IS_INCLUDE:
963                 case MLD2_MODE_IS_EXCLUDE:
964                 case MLD2_CHANGE_TO_INCLUDE:
965                 case MLD2_CHANGE_TO_EXCLUDE:
966                 case MLD2_ALLOW_NEW_SOURCES:
967                 case MLD2_BLOCK_OLD_SOURCES:
968                         break;
969
970                 default:
971                         continue;
972                 }
973
974                 err = br_ip6_multicast_add_group(br, port, &grec->grec_mca);
975                 if (!err)
976                         break;
977         }
978
979         return err;
980 }
981 #endif
982
983 /*
984  * Add port to rotuer_list
985  *  list is maintained ordered by pointer value
986  *  and locked by br->multicast_lock and RCU
987  */
988 static void br_multicast_add_router(struct net_bridge *br,
989                                     struct net_bridge_port *port)
990 {
991         struct net_bridge_port *p;
992         struct hlist_node *n, *slot = NULL;
993
994         hlist_for_each_entry(p, n, &br->router_list, rlist) {
995                 if ((unsigned long) port >= (unsigned long) p)
996                         break;
997                 slot = n;
998         }
999
1000         if (slot)
1001                 hlist_add_after_rcu(slot, &port->rlist);
1002         else
1003                 hlist_add_head_rcu(&port->rlist, &br->router_list);
1004 }
1005
1006 static void br_multicast_mark_router(struct net_bridge *br,
1007                                      struct net_bridge_port *port)
1008 {
1009         unsigned long now = jiffies;
1010
1011         if (!port) {
1012                 if (br->multicast_router == 1)
1013                         mod_timer(&br->multicast_router_timer,
1014                                   now + br->multicast_querier_interval);
1015                 return;
1016         }
1017
1018         if (port->multicast_router != 1)
1019                 return;
1020
1021         if (!hlist_unhashed(&port->rlist))
1022                 goto timer;
1023
1024         br_multicast_add_router(br, port);
1025
1026 timer:
1027         mod_timer(&port->multicast_router_timer,
1028                   now + br->multicast_querier_interval);
1029 }
1030
1031 static void br_multicast_query_received(struct net_bridge *br,
1032                                         struct net_bridge_port *port,
1033                                         int saddr)
1034 {
1035         if (saddr)
1036                 mod_timer(&br->multicast_querier_timer,
1037                           jiffies + br->multicast_querier_interval);
1038         else if (timer_pending(&br->multicast_querier_timer))
1039                 return;
1040
1041         br_multicast_mark_router(br, port);
1042 }
1043
1044 static int br_ip4_multicast_query(struct net_bridge *br,
1045                                   struct net_bridge_port *port,
1046                                   struct sk_buff *skb)
1047 {
1048         const struct iphdr *iph = ip_hdr(skb);
1049         struct igmphdr *ih = igmp_hdr(skb);
1050         struct net_bridge_mdb_entry *mp;
1051         struct igmpv3_query *ih3;
1052         struct net_bridge_port_group *p;
1053         struct net_bridge_port_group __rcu **pp;
1054         unsigned long max_delay;
1055         unsigned long now = jiffies;
1056         __be32 group;
1057         int err = 0;
1058
1059         spin_lock(&br->multicast_lock);
1060         if (!netif_running(br->dev) ||
1061             (port && port->state == BR_STATE_DISABLED))
1062                 goto out;
1063
1064         br_multicast_query_received(br, port, !!iph->saddr);
1065
1066         group = ih->group;
1067
1068         if (skb->len == sizeof(*ih)) {
1069                 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1070
1071                 if (!max_delay) {
1072                         max_delay = 10 * HZ;
1073                         group = 0;
1074                 }
1075         } else {
1076                 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) {
1077                         err = -EINVAL;
1078                         goto out;
1079                 }
1080
1081                 ih3 = igmpv3_query_hdr(skb);
1082                 if (ih3->nsrcs)
1083                         goto out;
1084
1085                 max_delay = ih3->code ?
1086                             IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
1087         }
1088
1089         if (!group)
1090                 goto out;
1091
1092         mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group);
1093         if (!mp)
1094                 goto out;
1095
1096         max_delay *= br->multicast_last_member_count;
1097
1098         if (mp->mglist &&
1099             (timer_pending(&mp->timer) ?
1100              time_after(mp->timer.expires, now + max_delay) :
1101              try_to_del_timer_sync(&mp->timer) >= 0))
1102                 mod_timer(&mp->timer, now + max_delay);
1103
1104         for (pp = &mp->ports;
1105              (p = mlock_dereference(*pp, br)) != NULL;
1106              pp = &p->next) {
1107                 if (timer_pending(&p->timer) ?
1108                     time_after(p->timer.expires, now + max_delay) :
1109                     try_to_del_timer_sync(&p->timer) >= 0)
1110                         mod_timer(&p->timer, now + max_delay);
1111         }
1112
1113 out:
1114         spin_unlock(&br->multicast_lock);
1115         return err;
1116 }
1117
1118 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1119 static int br_ip6_multicast_query(struct net_bridge *br,
1120                                   struct net_bridge_port *port,
1121                                   struct sk_buff *skb)
1122 {
1123         const struct ipv6hdr *ip6h = ipv6_hdr(skb);
1124         struct mld_msg *mld = (struct mld_msg *) icmp6_hdr(skb);
1125         struct net_bridge_mdb_entry *mp;
1126         struct mld2_query *mld2q;
1127         struct net_bridge_port_group *p;
1128         struct net_bridge_port_group __rcu **pp;
1129         unsigned long max_delay;
1130         unsigned long now = jiffies;
1131         const struct in6_addr *group = NULL;
1132         int err = 0;
1133
1134         spin_lock(&br->multicast_lock);
1135         if (!netif_running(br->dev) ||
1136             (port && port->state == BR_STATE_DISABLED))
1137                 goto out;
1138
1139         br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->saddr));
1140
1141         if (skb->len == sizeof(*mld)) {
1142                 if (!pskb_may_pull(skb, sizeof(*mld))) {
1143                         err = -EINVAL;
1144                         goto out;
1145                 }
1146                 mld = (struct mld_msg *) icmp6_hdr(skb);
1147                 max_delay = msecs_to_jiffies(htons(mld->mld_maxdelay));
1148                 if (max_delay)
1149                         group = &mld->mld_mca;
1150         } else if (skb->len >= sizeof(*mld2q)) {
1151                 if (!pskb_may_pull(skb, sizeof(*mld2q))) {
1152                         err = -EINVAL;
1153                         goto out;
1154                 }
1155                 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1156                 if (!mld2q->mld2q_nsrcs)
1157                         group = &mld2q->mld2q_mca;
1158
1159                 max_delay = max(msecs_to_jiffies(MLDV2_MRC(ntohs(mld2q->mld2q_mrc))), 1UL);
1160         }
1161
1162         if (!group)
1163                 goto out;
1164
1165         mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group);
1166         if (!mp)
1167                 goto out;
1168
1169         max_delay *= br->multicast_last_member_count;
1170         if (mp->mglist &&
1171             (timer_pending(&mp->timer) ?
1172              time_after(mp->timer.expires, now + max_delay) :
1173              try_to_del_timer_sync(&mp->timer) >= 0))
1174                 mod_timer(&mp->timer, now + max_delay);
1175
1176         for (pp = &mp->ports;
1177              (p = mlock_dereference(*pp, br)) != NULL;
1178              pp = &p->next) {
1179                 if (timer_pending(&p->timer) ?
1180                     time_after(p->timer.expires, now + max_delay) :
1181                     try_to_del_timer_sync(&p->timer) >= 0)
1182                         mod_timer(&p->timer, now + max_delay);
1183         }
1184
1185 out:
1186         spin_unlock(&br->multicast_lock);
1187         return err;
1188 }
1189 #endif
1190
1191 static void br_multicast_leave_group(struct net_bridge *br,
1192                                      struct net_bridge_port *port,
1193                                      struct br_ip *group)
1194 {
1195         struct net_bridge_mdb_htable *mdb;
1196         struct net_bridge_mdb_entry *mp;
1197         struct net_bridge_port_group *p;
1198         unsigned long now;
1199         unsigned long time;
1200
1201         spin_lock(&br->multicast_lock);
1202         if (!netif_running(br->dev) ||
1203             (port && port->state == BR_STATE_DISABLED) ||
1204             timer_pending(&br->multicast_querier_timer))
1205                 goto out;
1206
1207         mdb = mlock_dereference(br->mdb, br);
1208         mp = br_mdb_ip_get(mdb, group);
1209         if (!mp)
1210                 goto out;
1211
1212         now = jiffies;
1213         time = now + br->multicast_last_member_count *
1214                      br->multicast_last_member_interval;
1215
1216         if (!port) {
1217                 if (mp->mglist &&
1218                     (timer_pending(&mp->timer) ?
1219                      time_after(mp->timer.expires, time) :
1220                      try_to_del_timer_sync(&mp->timer) >= 0)) {
1221                         mod_timer(&mp->timer, time);
1222                 }
1223
1224                 goto out;
1225         }
1226
1227         for (p = mlock_dereference(mp->ports, br);
1228              p != NULL;
1229              p = mlock_dereference(p->next, br)) {
1230                 if (p->port != port)
1231                         continue;
1232
1233                 if (!hlist_unhashed(&p->mglist) &&
1234                     (timer_pending(&p->timer) ?
1235                      time_after(p->timer.expires, time) :
1236                      try_to_del_timer_sync(&p->timer) >= 0)) {
1237                         mod_timer(&p->timer, time);
1238                 }
1239
1240                 break;
1241         }
1242
1243 out:
1244         spin_unlock(&br->multicast_lock);
1245 }
1246
1247 static void br_ip4_multicast_leave_group(struct net_bridge *br,
1248                                          struct net_bridge_port *port,
1249                                          __be32 group)
1250 {
1251         struct br_ip br_group;
1252
1253         if (ipv4_is_local_multicast(group))
1254                 return;
1255
1256         br_group.u.ip4 = group;
1257         br_group.proto = htons(ETH_P_IP);
1258
1259         br_multicast_leave_group(br, port, &br_group);
1260 }
1261
1262 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1263 static void br_ip6_multicast_leave_group(struct net_bridge *br,
1264                                          struct net_bridge_port *port,
1265                                          const struct in6_addr *group)
1266 {
1267         struct br_ip br_group;
1268
1269         if (!ipv6_is_transient_multicast(group))
1270                 return;
1271
1272         ipv6_addr_copy(&br_group.u.ip6, group);
1273         br_group.proto = htons(ETH_P_IPV6);
1274
1275         br_multicast_leave_group(br, port, &br_group);
1276 }
1277 #endif
1278
1279 static int br_multicast_ipv4_rcv(struct net_bridge *br,
1280                                  struct net_bridge_port *port,
1281                                  struct sk_buff *skb)
1282 {
1283         struct sk_buff *skb2 = skb;
1284         const struct iphdr *iph;
1285         struct igmphdr *ih;
1286         unsigned len;
1287         unsigned offset;
1288         int err;
1289
1290         /* We treat OOM as packet loss for now. */
1291         if (!pskb_may_pull(skb, sizeof(*iph)))
1292                 return -EINVAL;
1293
1294         iph = ip_hdr(skb);
1295
1296         if (iph->ihl < 5 || iph->version != 4)
1297                 return -EINVAL;
1298
1299         if (!pskb_may_pull(skb, ip_hdrlen(skb)))
1300                 return -EINVAL;
1301
1302         iph = ip_hdr(skb);
1303
1304         if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1305                 return -EINVAL;
1306
1307         if (iph->protocol != IPPROTO_IGMP) {
1308                 if ((iph->daddr & IGMP_LOCAL_GROUP_MASK) != IGMP_LOCAL_GROUP)
1309                         BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1310                 return 0;
1311         }
1312
1313         len = ntohs(iph->tot_len);
1314         if (skb->len < len || len < ip_hdrlen(skb))
1315                 return -EINVAL;
1316
1317         if (skb->len > len) {
1318                 skb2 = skb_clone(skb, GFP_ATOMIC);
1319                 if (!skb2)
1320                         return -ENOMEM;
1321
1322                 err = pskb_trim_rcsum(skb2, len);
1323                 if (err)
1324                         goto err_out;
1325         }
1326
1327         len -= ip_hdrlen(skb2);
1328         offset = skb_network_offset(skb2) + ip_hdrlen(skb2);
1329         __skb_pull(skb2, offset);
1330         skb_reset_transport_header(skb2);
1331
1332         err = -EINVAL;
1333         if (!pskb_may_pull(skb2, sizeof(*ih)))
1334                 goto out;
1335
1336         switch (skb2->ip_summed) {
1337         case CHECKSUM_COMPLETE:
1338                 if (!csum_fold(skb2->csum))
1339                         break;
1340                 /* fall through */
1341         case CHECKSUM_NONE:
1342                 skb2->csum = 0;
1343                 if (skb_checksum_complete(skb2))
1344                         goto out;
1345         }
1346
1347         err = 0;
1348
1349         BR_INPUT_SKB_CB(skb)->igmp = 1;
1350         ih = igmp_hdr(skb2);
1351
1352         switch (ih->type) {
1353         case IGMP_HOST_MEMBERSHIP_REPORT:
1354         case IGMPV2_HOST_MEMBERSHIP_REPORT:
1355                 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1356                 err = br_ip4_multicast_add_group(br, port, ih->group);
1357                 break;
1358         case IGMPV3_HOST_MEMBERSHIP_REPORT:
1359                 err = br_ip4_multicast_igmp3_report(br, port, skb2);
1360                 break;
1361         case IGMP_HOST_MEMBERSHIP_QUERY:
1362                 err = br_ip4_multicast_query(br, port, skb2);
1363                 break;
1364         case IGMP_HOST_LEAVE_MESSAGE:
1365                 br_ip4_multicast_leave_group(br, port, ih->group);
1366                 break;
1367         }
1368
1369 out:
1370         __skb_push(skb2, offset);
1371 err_out:
1372         if (skb2 != skb)
1373                 kfree_skb(skb2);
1374         return err;
1375 }
1376
1377 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1378 static int br_multicast_ipv6_rcv(struct net_bridge *br,
1379                                  struct net_bridge_port *port,
1380                                  struct sk_buff *skb)
1381 {
1382         struct sk_buff *skb2;
1383         const struct ipv6hdr *ip6h;
1384         u8 icmp6_type;
1385         u8 nexthdr;
1386         unsigned len;
1387         int offset;
1388         int err;
1389
1390         if (!pskb_may_pull(skb, sizeof(*ip6h)))
1391                 return -EINVAL;
1392
1393         ip6h = ipv6_hdr(skb);
1394
1395         /*
1396          * We're interested in MLD messages only.
1397          *  - Version is 6
1398          *  - MLD has always Router Alert hop-by-hop option
1399          *  - But we do not support jumbrograms.
1400          */
1401         if (ip6h->version != 6 ||
1402             ip6h->nexthdr != IPPROTO_HOPOPTS ||
1403             ip6h->payload_len == 0)
1404                 return 0;
1405
1406         len = ntohs(ip6h->payload_len) + sizeof(*ip6h);
1407         if (skb->len < len)
1408                 return -EINVAL;
1409
1410         nexthdr = ip6h->nexthdr;
1411         offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr);
1412
1413         if (offset < 0 || nexthdr != IPPROTO_ICMPV6)
1414                 return 0;
1415
1416         /* Okay, we found ICMPv6 header */
1417         skb2 = skb_clone(skb, GFP_ATOMIC);
1418         if (!skb2)
1419                 return -ENOMEM;
1420
1421         err = -EINVAL;
1422         if (!pskb_may_pull(skb2, offset + sizeof(struct icmp6hdr)))
1423                 goto out;
1424
1425         len -= offset - skb_network_offset(skb2);
1426
1427         __skb_pull(skb2, offset);
1428         skb_reset_transport_header(skb2);
1429         skb_postpull_rcsum(skb2, skb_network_header(skb2),
1430                            skb_network_header_len(skb2));
1431
1432         icmp6_type = icmp6_hdr(skb2)->icmp6_type;
1433
1434         switch (icmp6_type) {
1435         case ICMPV6_MGM_QUERY:
1436         case ICMPV6_MGM_REPORT:
1437         case ICMPV6_MGM_REDUCTION:
1438         case ICMPV6_MLD2_REPORT:
1439                 break;
1440         default:
1441                 err = 0;
1442                 goto out;
1443         }
1444
1445         /* Okay, we found MLD message. Check further. */
1446         if (skb2->len > len) {
1447                 err = pskb_trim_rcsum(skb2, len);
1448                 if (err)
1449                         goto out;
1450                 err = -EINVAL;
1451         }
1452
1453         ip6h = ipv6_hdr(skb2);
1454
1455         switch (skb2->ip_summed) {
1456         case CHECKSUM_COMPLETE:
1457                 if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, skb2->len,
1458                                         IPPROTO_ICMPV6, skb2->csum))
1459                         break;
1460                 /*FALLTHROUGH*/
1461         case CHECKSUM_NONE:
1462                 skb2->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr,
1463                                                         &ip6h->daddr,
1464                                                         skb2->len,
1465                                                         IPPROTO_ICMPV6, 0));
1466                 if (__skb_checksum_complete(skb2))
1467                         goto out;
1468         }
1469
1470         err = 0;
1471
1472         BR_INPUT_SKB_CB(skb)->igmp = 1;
1473
1474         switch (icmp6_type) {
1475         case ICMPV6_MGM_REPORT:
1476             {
1477                 struct mld_msg *mld;
1478                 if (!pskb_may_pull(skb2, sizeof(*mld))) {
1479                         err = -EINVAL;
1480                         goto out;
1481                 }
1482                 mld = (struct mld_msg *)skb_transport_header(skb2);
1483                 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1484                 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca);
1485                 break;
1486             }
1487         case ICMPV6_MLD2_REPORT:
1488                 err = br_ip6_multicast_mld2_report(br, port, skb2);
1489                 break;
1490         case ICMPV6_MGM_QUERY:
1491                 err = br_ip6_multicast_query(br, port, skb2);
1492                 break;
1493         case ICMPV6_MGM_REDUCTION:
1494             {
1495                 struct mld_msg *mld;
1496                 if (!pskb_may_pull(skb2, sizeof(*mld))) {
1497                         err = -EINVAL;
1498                         goto out;
1499                 }
1500                 mld = (struct mld_msg *)skb_transport_header(skb2);
1501                 br_ip6_multicast_leave_group(br, port, &mld->mld_mca);
1502             }
1503         }
1504
1505 out:
1506         kfree_skb(skb2);
1507         return err;
1508 }
1509 #endif
1510
1511 int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1512                      struct sk_buff *skb)
1513 {
1514         BR_INPUT_SKB_CB(skb)->igmp = 0;
1515         BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1516
1517         if (br->multicast_disabled)
1518                 return 0;
1519
1520         switch (skb->protocol) {
1521         case htons(ETH_P_IP):
1522                 return br_multicast_ipv4_rcv(br, port, skb);
1523 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1524         case htons(ETH_P_IPV6):
1525                 return br_multicast_ipv6_rcv(br, port, skb);
1526 #endif
1527         }
1528
1529         return 0;
1530 }
1531
1532 static void br_multicast_query_expired(unsigned long data)
1533 {
1534         struct net_bridge *br = (void *)data;
1535
1536         spin_lock(&br->multicast_lock);
1537         if (br->multicast_startup_queries_sent <
1538             br->multicast_startup_query_count)
1539                 br->multicast_startup_queries_sent++;
1540
1541         br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent);
1542
1543         spin_unlock(&br->multicast_lock);
1544 }
1545
1546 void br_multicast_init(struct net_bridge *br)
1547 {
1548         br->hash_elasticity = 4;
1549         br->hash_max = 512;
1550
1551         br->multicast_router = 1;
1552         br->multicast_last_member_count = 2;
1553         br->multicast_startup_query_count = 2;
1554
1555         br->multicast_last_member_interval = HZ;
1556         br->multicast_query_response_interval = 10 * HZ;
1557         br->multicast_startup_query_interval = 125 * HZ / 4;
1558         br->multicast_query_interval = 125 * HZ;
1559         br->multicast_querier_interval = 255 * HZ;
1560         br->multicast_membership_interval = 260 * HZ;
1561
1562         spin_lock_init(&br->multicast_lock);
1563         setup_timer(&br->multicast_router_timer,
1564                     br_multicast_local_router_expired, 0);
1565         setup_timer(&br->multicast_querier_timer,
1566                     br_multicast_local_router_expired, 0);
1567         setup_timer(&br->multicast_query_timer, br_multicast_query_expired,
1568                     (unsigned long)br);
1569 }
1570
1571 void br_multicast_open(struct net_bridge *br)
1572 {
1573         br->multicast_startup_queries_sent = 0;
1574
1575         if (br->multicast_disabled)
1576                 return;
1577
1578         mod_timer(&br->multicast_query_timer, jiffies);
1579 }
1580
1581 void br_multicast_stop(struct net_bridge *br)
1582 {
1583         struct net_bridge_mdb_htable *mdb;
1584         struct net_bridge_mdb_entry *mp;
1585         struct hlist_node *p, *n;
1586         u32 ver;
1587         int i;
1588
1589         del_timer_sync(&br->multicast_router_timer);
1590         del_timer_sync(&br->multicast_querier_timer);
1591         del_timer_sync(&br->multicast_query_timer);
1592
1593         spin_lock_bh(&br->multicast_lock);
1594         mdb = mlock_dereference(br->mdb, br);
1595         if (!mdb)
1596                 goto out;
1597
1598         br->mdb = NULL;
1599
1600         ver = mdb->ver;
1601         for (i = 0; i < mdb->max; i++) {
1602                 hlist_for_each_entry_safe(mp, p, n, &mdb->mhash[i],
1603                                           hlist[ver]) {
1604                         del_timer(&mp->timer);
1605                         call_rcu_bh(&mp->rcu, br_multicast_free_group);
1606                 }
1607         }
1608
1609         if (mdb->old) {
1610                 spin_unlock_bh(&br->multicast_lock);
1611                 rcu_barrier_bh();
1612                 spin_lock_bh(&br->multicast_lock);
1613                 WARN_ON(mdb->old);
1614         }
1615
1616         mdb->old = mdb;
1617         call_rcu_bh(&mdb->rcu, br_mdb_free);
1618
1619 out:
1620         spin_unlock_bh(&br->multicast_lock);
1621 }
1622
1623 int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1624 {
1625         int err = -ENOENT;
1626
1627         spin_lock_bh(&br->multicast_lock);
1628         if (!netif_running(br->dev))
1629                 goto unlock;
1630
1631         switch (val) {
1632         case 0:
1633         case 2:
1634                 del_timer(&br->multicast_router_timer);
1635                 /* fall through */
1636         case 1:
1637                 br->multicast_router = val;
1638                 err = 0;
1639                 break;
1640
1641         default:
1642                 err = -EINVAL;
1643                 break;
1644         }
1645
1646 unlock:
1647         spin_unlock_bh(&br->multicast_lock);
1648
1649         return err;
1650 }
1651
1652 int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1653 {
1654         struct net_bridge *br = p->br;
1655         int err = -ENOENT;
1656
1657         spin_lock(&br->multicast_lock);
1658         if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED)
1659                 goto unlock;
1660
1661         switch (val) {
1662         case 0:
1663         case 1:
1664         case 2:
1665                 p->multicast_router = val;
1666                 err = 0;
1667
1668                 if (val < 2 && !hlist_unhashed(&p->rlist))
1669                         hlist_del_init_rcu(&p->rlist);
1670
1671                 if (val == 1)
1672                         break;
1673
1674                 del_timer(&p->multicast_router_timer);
1675
1676                 if (val == 0)
1677                         break;
1678
1679                 br_multicast_add_router(br, p);
1680                 break;
1681
1682         default:
1683                 err = -EINVAL;
1684                 break;
1685         }
1686
1687 unlock:
1688         spin_unlock(&br->multicast_lock);
1689
1690         return err;
1691 }
1692
1693 int br_multicast_toggle(struct net_bridge *br, unsigned long val)
1694 {
1695         struct net_bridge_port *port;
1696         int err = 0;
1697         struct net_bridge_mdb_htable *mdb;
1698
1699         spin_lock_bh(&br->multicast_lock);
1700         if (br->multicast_disabled == !val)
1701                 goto unlock;
1702
1703         br->multicast_disabled = !val;
1704         if (br->multicast_disabled)
1705                 goto unlock;
1706
1707         if (!netif_running(br->dev))
1708                 goto unlock;
1709
1710         mdb = mlock_dereference(br->mdb, br);
1711         if (mdb) {
1712                 if (mdb->old) {
1713                         err = -EEXIST;
1714 rollback:
1715                         br->multicast_disabled = !!val;
1716                         goto unlock;
1717                 }
1718
1719                 err = br_mdb_rehash(&br->mdb, mdb->max,
1720                                     br->hash_elasticity);
1721                 if (err)
1722                         goto rollback;
1723         }
1724
1725         br_multicast_open(br);
1726         list_for_each_entry(port, &br->port_list, list) {
1727                 if (port->state == BR_STATE_DISABLED ||
1728                     port->state == BR_STATE_BLOCKING)
1729                         continue;
1730
1731                 __br_multicast_enable_port(port);
1732         }
1733
1734 unlock:
1735         spin_unlock_bh(&br->multicast_lock);
1736
1737         return err;
1738 }
1739
1740 int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
1741 {
1742         int err = -ENOENT;
1743         u32 old;
1744         struct net_bridge_mdb_htable *mdb;
1745
1746         spin_lock(&br->multicast_lock);
1747         if (!netif_running(br->dev))
1748                 goto unlock;
1749
1750         err = -EINVAL;
1751         if (!is_power_of_2(val))
1752                 goto unlock;
1753
1754         mdb = mlock_dereference(br->mdb, br);
1755         if (mdb && val < mdb->size)
1756                 goto unlock;
1757
1758         err = 0;
1759
1760         old = br->hash_max;
1761         br->hash_max = val;
1762
1763         if (mdb) {
1764                 if (mdb->old) {
1765                         err = -EEXIST;
1766 rollback:
1767                         br->hash_max = old;
1768                         goto unlock;
1769                 }
1770
1771                 err = br_mdb_rehash(&br->mdb, br->hash_max,
1772                                     br->hash_elasticity);
1773                 if (err)
1774                         goto rollback;
1775         }
1776
1777 unlock:
1778         spin_unlock(&br->multicast_lock);
1779
1780         return err;
1781 }