2 * Bridge multicast support.
4 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
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>
27 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
30 #include <net/addrconf.h>
31 #include <net/ip6_checksum.h>
34 #include "br_private.h"
36 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
37 static inline int ipv6_is_local_multicast(const struct in6_addr *addr)
39 if (ipv6_addr_is_multicast(addr) &&
40 IPV6_ADDR_MC_SCOPE(addr) <= IPV6_ADDR_SCOPE_LINKLOCAL)
46 static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
48 if (a->proto != b->proto)
52 return a->u.ip4 == b->u.ip4;
53 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
54 case htons(ETH_P_IPV6):
55 return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
61 static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip)
63 return jhash_1word(mdb->secret, (__force u32)ip) & (mdb->max - 1);
66 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
67 static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb,
68 const struct in6_addr *ip)
70 return jhash2((__force u32 *)ip->s6_addr32, 4, mdb->secret) & (mdb->max - 1);
74 static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb,
79 return __br_ip4_hash(mdb, ip->u.ip4);
80 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
81 case htons(ETH_P_IPV6):
82 return __br_ip6_hash(mdb, &ip->u.ip6);
88 static struct net_bridge_mdb_entry *__br_mdb_ip_get(
89 struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash)
91 struct net_bridge_mdb_entry *mp;
94 hlist_for_each_entry_rcu(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) {
95 if (br_ip_equal(&mp->addr, dst))
102 static struct net_bridge_mdb_entry *br_mdb_ip4_get(
103 struct net_bridge_mdb_htable *mdb, __be32 dst)
108 br_dst.proto = htons(ETH_P_IP);
110 return __br_mdb_ip_get(mdb, &br_dst, __br_ip4_hash(mdb, dst));
113 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
114 static struct net_bridge_mdb_entry *br_mdb_ip6_get(
115 struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst)
119 ipv6_addr_copy(&br_dst.u.ip6, dst);
120 br_dst.proto = htons(ETH_P_IPV6);
122 return __br_mdb_ip_get(mdb, &br_dst, __br_ip6_hash(mdb, dst));
126 static struct net_bridge_mdb_entry *br_mdb_ip_get(
127 struct net_bridge_mdb_htable *mdb, struct br_ip *dst)
129 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
132 struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
135 struct net_bridge_mdb_htable *mdb = br->mdb;
138 if (!mdb || br->multicast_disabled)
141 if (BR_INPUT_SKB_CB(skb)->igmp)
144 ip.proto = skb->protocol;
146 switch (skb->protocol) {
147 case htons(ETH_P_IP):
148 ip.u.ip4 = ip_hdr(skb)->daddr;
150 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
151 case htons(ETH_P_IPV6):
152 ipv6_addr_copy(&ip.u.ip6, &ipv6_hdr(skb)->daddr);
159 return br_mdb_ip_get(mdb, &ip);
162 static void br_mdb_free(struct rcu_head *head)
164 struct net_bridge_mdb_htable *mdb =
165 container_of(head, struct net_bridge_mdb_htable, rcu);
166 struct net_bridge_mdb_htable *old = mdb->old;
173 static int br_mdb_copy(struct net_bridge_mdb_htable *new,
174 struct net_bridge_mdb_htable *old,
177 struct net_bridge_mdb_entry *mp;
178 struct hlist_node *p;
183 for (i = 0; i < old->max; i++)
184 hlist_for_each_entry(mp, p, &old->mhash[i], hlist[old->ver])
185 hlist_add_head(&mp->hlist[new->ver],
186 &new->mhash[br_ip_hash(new, &mp->addr)]);
192 for (i = 0; i < new->max; i++) {
194 hlist_for_each_entry(mp, p, &new->mhash[i], hlist[new->ver])
200 return maxlen > elasticity ? -EINVAL : 0;
203 static void br_multicast_free_pg(struct rcu_head *head)
205 struct net_bridge_port_group *p =
206 container_of(head, struct net_bridge_port_group, rcu);
211 static void br_multicast_free_group(struct rcu_head *head)
213 struct net_bridge_mdb_entry *mp =
214 container_of(head, struct net_bridge_mdb_entry, rcu);
219 static void br_multicast_group_expired(unsigned long data)
221 struct net_bridge_mdb_entry *mp = (void *)data;
222 struct net_bridge *br = mp->br;
223 struct net_bridge_mdb_htable *mdb;
225 spin_lock(&br->multicast_lock);
226 if (!netif_running(br->dev) || timer_pending(&mp->timer))
229 if (!hlist_unhashed(&mp->mglist))
230 hlist_del_init(&mp->mglist);
236 hlist_del_rcu(&mp->hlist[mdb->ver]);
239 del_timer(&mp->query_timer);
240 call_rcu_bh(&mp->rcu, br_multicast_free_group);
243 spin_unlock(&br->multicast_lock);
246 static void br_multicast_del_pg(struct net_bridge *br,
247 struct net_bridge_port_group *pg)
249 struct net_bridge_mdb_htable *mdb = br->mdb;
250 struct net_bridge_mdb_entry *mp;
251 struct net_bridge_port_group *p;
252 struct net_bridge_port_group **pp;
254 mp = br_mdb_ip_get(mdb, &pg->addr);
258 for (pp = &mp->ports; (p = *pp); pp = &p->next) {
262 rcu_assign_pointer(*pp, p->next);
263 hlist_del_init(&p->mglist);
264 del_timer(&p->timer);
265 del_timer(&p->query_timer);
266 call_rcu_bh(&p->rcu, br_multicast_free_pg);
268 if (!mp->ports && hlist_unhashed(&mp->mglist) &&
269 netif_running(br->dev))
270 mod_timer(&mp->timer, jiffies);
278 static void br_multicast_port_group_expired(unsigned long data)
280 struct net_bridge_port_group *pg = (void *)data;
281 struct net_bridge *br = pg->port->br;
283 spin_lock(&br->multicast_lock);
284 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
285 hlist_unhashed(&pg->mglist))
288 br_multicast_del_pg(br, pg);
291 spin_unlock(&br->multicast_lock);
294 static int br_mdb_rehash(struct net_bridge_mdb_htable **mdbp, int max,
297 struct net_bridge_mdb_htable *old = *mdbp;
298 struct net_bridge_mdb_htable *mdb;
301 mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
308 mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
314 mdb->size = old ? old->size : 0;
315 mdb->ver = old ? old->ver ^ 1 : 0;
317 if (!old || elasticity)
318 get_random_bytes(&mdb->secret, sizeof(mdb->secret));
320 mdb->secret = old->secret;
325 err = br_mdb_copy(mdb, old, elasticity);
332 call_rcu_bh(&mdb->rcu, br_mdb_free);
335 rcu_assign_pointer(*mdbp, mdb);
340 static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
348 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
353 skb->protocol = htons(ETH_P_IP);
355 skb_reset_mac_header(skb);
358 memcpy(eth->h_source, br->dev->dev_addr, 6);
361 eth->h_dest[2] = 0x5e;
365 eth->h_proto = htons(ETH_P_IP);
366 skb_put(skb, sizeof(*eth));
368 skb_set_network_header(skb, skb->len);
374 iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4);
376 iph->frag_off = htons(IP_DF);
378 iph->protocol = IPPROTO_IGMP;
380 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
381 ((u8 *)&iph[1])[0] = IPOPT_RA;
382 ((u8 *)&iph[1])[1] = 4;
383 ((u8 *)&iph[1])[2] = 0;
384 ((u8 *)&iph[1])[3] = 0;
388 skb_set_transport_header(skb, skb->len);
390 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
391 ih->code = (group ? br->multicast_last_member_interval :
392 br->multicast_query_response_interval) /
393 (HZ / IGMP_TIMER_SCALE);
396 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
397 skb_put(skb, sizeof(*ih));
399 __skb_pull(skb, sizeof(*eth));
405 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
406 static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
407 struct in6_addr *group)
410 struct ipv6hdr *ip6h;
411 struct mld_msg *mldq;
414 unsigned long interval;
416 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
421 skb->protocol = htons(ETH_P_IPV6);
423 /* Ethernet header */
424 skb_reset_mac_header(skb);
427 memcpy(eth->h_source, br->dev->dev_addr, 6);
428 ipv6_eth_mc_map(group, eth->h_dest);
429 eth->h_proto = htons(ETH_P_IPV6);
430 skb_put(skb, sizeof(*eth));
432 /* IPv6 header + HbH option */
433 skb_set_network_header(skb, skb->len);
434 ip6h = ipv6_hdr(skb);
436 *(__force __be32 *)ip6h = htonl(0x60000000);
437 ip6h->payload_len = 8 + sizeof(*mldq);
438 ip6h->nexthdr = IPPROTO_HOPOPTS;
440 ipv6_addr_set(&ip6h->saddr, 0, 0, 0, 0);
441 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
443 hopopt = (u8 *)(ip6h + 1);
444 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
445 hopopt[1] = 0; /* length of HbH */
446 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
447 hopopt[3] = 2; /* Length of RA Option */
448 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
450 hopopt[6] = IPV6_TLV_PAD0; /* Pad0 */
451 hopopt[7] = IPV6_TLV_PAD0; /* Pad0 */
453 skb_put(skb, sizeof(*ip6h) + 8);
456 skb_set_transport_header(skb, skb->len);
457 mldq = (struct mld_msg *) icmp6_hdr(skb);
459 interval = ipv6_addr_any(group) ? br->multicast_last_member_interval :
460 br->multicast_query_response_interval;
462 mldq->mld_type = ICMPV6_MGM_QUERY;
465 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
466 mldq->mld_reserved = 0;
467 ipv6_addr_copy(&mldq->mld_mca, group);
470 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
471 sizeof(*mldq), IPPROTO_ICMPV6,
474 skb_put(skb, sizeof(*mldq));
476 __skb_pull(skb, sizeof(*eth));
483 static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
486 switch (addr->proto) {
487 case htons(ETH_P_IP):
488 return br_ip4_multicast_alloc_query(br, addr->u.ip4);
489 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
490 case htons(ETH_P_IPV6):
491 return br_ip6_multicast_alloc_query(br, &addr->u.ip6);
497 static void br_multicast_send_group_query(struct net_bridge_mdb_entry *mp)
499 struct net_bridge *br = mp->br;
502 skb = br_multicast_alloc_query(br, &mp->addr);
509 if (++mp->queries_sent < br->multicast_last_member_count)
510 mod_timer(&mp->query_timer,
511 jiffies + br->multicast_last_member_interval);
514 static void br_multicast_group_query_expired(unsigned long data)
516 struct net_bridge_mdb_entry *mp = (void *)data;
517 struct net_bridge *br = mp->br;
519 spin_lock(&br->multicast_lock);
520 if (!netif_running(br->dev) || hlist_unhashed(&mp->mglist) ||
521 mp->queries_sent >= br->multicast_last_member_count)
524 br_multicast_send_group_query(mp);
527 spin_unlock(&br->multicast_lock);
530 static void br_multicast_send_port_group_query(struct net_bridge_port_group *pg)
532 struct net_bridge_port *port = pg->port;
533 struct net_bridge *br = port->br;
536 skb = br_multicast_alloc_query(br, &pg->addr);
540 br_deliver(port, skb);
543 if (++pg->queries_sent < br->multicast_last_member_count)
544 mod_timer(&pg->query_timer,
545 jiffies + br->multicast_last_member_interval);
548 static void br_multicast_port_group_query_expired(unsigned long data)
550 struct net_bridge_port_group *pg = (void *)data;
551 struct net_bridge_port *port = pg->port;
552 struct net_bridge *br = port->br;
554 spin_lock(&br->multicast_lock);
555 if (!netif_running(br->dev) || hlist_unhashed(&pg->mglist) ||
556 pg->queries_sent >= br->multicast_last_member_count)
559 br_multicast_send_port_group_query(pg);
562 spin_unlock(&br->multicast_lock);
565 static struct net_bridge_mdb_entry *br_multicast_get_group(
566 struct net_bridge *br, struct net_bridge_port *port,
567 struct br_ip *group, int hash)
569 struct net_bridge_mdb_htable *mdb = br->mdb;
570 struct net_bridge_mdb_entry *mp;
571 struct hlist_node *p;
577 hlist_for_each_entry(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) {
579 if (unlikely(br_ip_equal(group, &mp->addr)))
586 if (unlikely(count > br->hash_elasticity && count)) {
588 br_info(br, "Multicast hash table "
589 "chain limit reached: %s\n",
590 port ? port->dev->name : br->dev->name);
592 elasticity = br->hash_elasticity;
595 if (mdb->size >= max) {
597 if (unlikely(max >= br->hash_max)) {
598 br_warn(br, "Multicast hash table maximum "
599 "reached, disabling snooping: %s, %d\n",
600 port ? port->dev->name : br->dev->name, max);
603 br->multicast_disabled = 1;
608 if (max > mdb->max || elasticity) {
611 br_info(br, "Multicast hash table "
613 port ? port->dev->name : br->dev->name);
618 err = br_mdb_rehash(&br->mdb, max, elasticity);
620 br_warn(br, "Cannot rehash multicast "
621 "hash table, disabling snooping: %s, %d, %d\n",
622 port ? port->dev->name : br->dev->name,
638 static struct net_bridge_mdb_entry *br_multicast_new_group(
639 struct net_bridge *br, struct net_bridge_port *port,
642 struct net_bridge_mdb_htable *mdb = br->mdb;
643 struct net_bridge_mdb_entry *mp;
647 if (br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0))
652 hash = br_ip_hash(mdb, group);
653 mp = br_multicast_get_group(br, port, group, hash);
654 switch (PTR_ERR(mp)) {
661 hash = br_ip_hash(mdb, group);
668 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
674 setup_timer(&mp->timer, br_multicast_group_expired,
676 setup_timer(&mp->query_timer, br_multicast_group_query_expired,
679 hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
686 static int br_multicast_add_group(struct net_bridge *br,
687 struct net_bridge_port *port,
690 struct net_bridge_mdb_entry *mp;
691 struct net_bridge_port_group *p;
692 struct net_bridge_port_group **pp;
693 unsigned long now = jiffies;
696 spin_lock(&br->multicast_lock);
697 if (!netif_running(br->dev) ||
698 (port && port->state == BR_STATE_DISABLED))
701 mp = br_multicast_new_group(br, port, group);
703 if (unlikely(IS_ERR(mp) || !mp))
707 hlist_add_head(&mp->mglist, &br->mglist);
708 mod_timer(&mp->timer, now + br->multicast_membership_interval);
712 for (pp = &mp->ports; (p = *pp); pp = &p->next) {
715 if ((unsigned long)p->port < (unsigned long)port)
719 p = kzalloc(sizeof(*p), GFP_ATOMIC);
727 hlist_add_head(&p->mglist, &port->mglist);
728 setup_timer(&p->timer, br_multicast_port_group_expired,
730 setup_timer(&p->query_timer, br_multicast_port_group_query_expired,
733 rcu_assign_pointer(*pp, p);
736 mod_timer(&p->timer, now + br->multicast_membership_interval);
741 spin_unlock(&br->multicast_lock);
745 static int br_ip4_multicast_add_group(struct net_bridge *br,
746 struct net_bridge_port *port,
749 struct br_ip br_group;
751 if (ipv4_is_local_multicast(group))
754 br_group.u.ip4 = group;
755 br_group.proto = htons(ETH_P_IP);
757 return br_multicast_add_group(br, port, &br_group);
760 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
761 static int br_ip6_multicast_add_group(struct net_bridge *br,
762 struct net_bridge_port *port,
763 const struct in6_addr *group)
765 struct br_ip br_group;
767 if (ipv6_is_local_multicast(group))
770 ipv6_addr_copy(&br_group.u.ip6, group);
771 br_group.proto = htons(ETH_P_IP);
773 return br_multicast_add_group(br, port, &br_group);
777 static void br_multicast_router_expired(unsigned long data)
779 struct net_bridge_port *port = (void *)data;
780 struct net_bridge *br = port->br;
782 spin_lock(&br->multicast_lock);
783 if (port->multicast_router != 1 ||
784 timer_pending(&port->multicast_router_timer) ||
785 hlist_unhashed(&port->rlist))
788 hlist_del_init_rcu(&port->rlist);
791 spin_unlock(&br->multicast_lock);
794 static void br_multicast_local_router_expired(unsigned long data)
798 static void __br_multicast_send_query(struct net_bridge *br,
799 struct net_bridge_port *port,
804 skb = br_multicast_alloc_query(br, ip);
809 __skb_push(skb, sizeof(struct ethhdr));
810 skb->dev = port->dev;
811 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
817 static void br_multicast_send_query(struct net_bridge *br,
818 struct net_bridge_port *port, u32 sent)
821 struct br_ip br_group;
823 if (!netif_running(br->dev) || br->multicast_disabled ||
824 timer_pending(&br->multicast_querier_timer))
827 memset(&br_group.u, 0, sizeof(br_group.u));
829 br_group.proto = htons(ETH_P_IP);
830 __br_multicast_send_query(br, port, &br_group);
832 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
833 br_group.proto = htons(ETH_P_IPV6);
834 __br_multicast_send_query(br, port, &br_group);
838 time += sent < br->multicast_startup_query_count ?
839 br->multicast_startup_query_interval :
840 br->multicast_query_interval;
841 mod_timer(port ? &port->multicast_query_timer :
842 &br->multicast_query_timer, time);
845 static void br_multicast_port_query_expired(unsigned long data)
847 struct net_bridge_port *port = (void *)data;
848 struct net_bridge *br = port->br;
850 spin_lock(&br->multicast_lock);
851 if (port->state == BR_STATE_DISABLED ||
852 port->state == BR_STATE_BLOCKING)
855 if (port->multicast_startup_queries_sent <
856 br->multicast_startup_query_count)
857 port->multicast_startup_queries_sent++;
859 br_multicast_send_query(port->br, port,
860 port->multicast_startup_queries_sent);
863 spin_unlock(&br->multicast_lock);
866 void br_multicast_add_port(struct net_bridge_port *port)
868 port->multicast_router = 1;
870 setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
871 (unsigned long)port);
872 setup_timer(&port->multicast_query_timer,
873 br_multicast_port_query_expired, (unsigned long)port);
876 void br_multicast_del_port(struct net_bridge_port *port)
878 del_timer_sync(&port->multicast_router_timer);
881 static void __br_multicast_enable_port(struct net_bridge_port *port)
883 port->multicast_startup_queries_sent = 0;
885 if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 ||
886 del_timer(&port->multicast_query_timer))
887 mod_timer(&port->multicast_query_timer, jiffies);
890 void br_multicast_enable_port(struct net_bridge_port *port)
892 struct net_bridge *br = port->br;
894 spin_lock(&br->multicast_lock);
895 if (br->multicast_disabled || !netif_running(br->dev))
898 __br_multicast_enable_port(port);
901 spin_unlock(&br->multicast_lock);
904 void br_multicast_disable_port(struct net_bridge_port *port)
906 struct net_bridge *br = port->br;
907 struct net_bridge_port_group *pg;
908 struct hlist_node *p, *n;
910 spin_lock(&br->multicast_lock);
911 hlist_for_each_entry_safe(pg, p, n, &port->mglist, mglist)
912 br_multicast_del_pg(br, pg);
914 if (!hlist_unhashed(&port->rlist))
915 hlist_del_init_rcu(&port->rlist);
916 del_timer(&port->multicast_router_timer);
917 del_timer(&port->multicast_query_timer);
918 spin_unlock(&br->multicast_lock);
921 static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
922 struct net_bridge_port *port,
925 struct igmpv3_report *ih;
926 struct igmpv3_grec *grec;
934 if (!pskb_may_pull(skb, sizeof(*ih)))
937 ih = igmpv3_report_hdr(skb);
938 num = ntohs(ih->ngrec);
941 for (i = 0; i < num; i++) {
942 len += sizeof(*grec);
943 if (!pskb_may_pull(skb, len))
946 grec = (void *)(skb->data + len - sizeof(*grec));
947 group = grec->grec_mca;
948 type = grec->grec_type;
950 len += ntohs(grec->grec_nsrcs) * 4;
951 if (!pskb_may_pull(skb, len))
954 /* We treat this as an IGMPv2 report for now. */
956 case IGMPV3_MODE_IS_INCLUDE:
957 case IGMPV3_MODE_IS_EXCLUDE:
958 case IGMPV3_CHANGE_TO_INCLUDE:
959 case IGMPV3_CHANGE_TO_EXCLUDE:
960 case IGMPV3_ALLOW_NEW_SOURCES:
961 case IGMPV3_BLOCK_OLD_SOURCES:
968 err = br_ip4_multicast_add_group(br, port, group);
976 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
977 static int br_ip6_multicast_mld2_report(struct net_bridge *br,
978 struct net_bridge_port *port,
981 struct icmp6hdr *icmp6h;
982 struct mld2_grec *grec;
988 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
991 icmp6h = icmp6_hdr(skb);
992 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
993 len = sizeof(*icmp6h);
995 for (i = 0; i < num; i++) {
996 __be16 *nsrcs, _nsrcs;
998 nsrcs = skb_header_pointer(skb,
999 len + offsetof(struct mld2_grec,
1001 sizeof(_nsrcs), &_nsrcs);
1005 if (!pskb_may_pull(skb,
1006 len + sizeof(*grec) +
1007 sizeof(struct in6_addr) * (*nsrcs)))
1010 grec = (struct mld2_grec *)(skb->data + len);
1011 len += sizeof(*grec) + sizeof(struct in6_addr) * (*nsrcs);
1013 /* We treat these as MLDv1 reports for now. */
1014 switch (grec->grec_type) {
1015 case MLD2_MODE_IS_INCLUDE:
1016 case MLD2_MODE_IS_EXCLUDE:
1017 case MLD2_CHANGE_TO_INCLUDE:
1018 case MLD2_CHANGE_TO_EXCLUDE:
1019 case MLD2_ALLOW_NEW_SOURCES:
1020 case MLD2_BLOCK_OLD_SOURCES:
1027 err = br_ip6_multicast_add_group(br, port, &grec->grec_mca);
1037 * Add port to rotuer_list
1038 * list is maintained ordered by pointer value
1039 * and locked by br->multicast_lock and RCU
1041 static void br_multicast_add_router(struct net_bridge *br,
1042 struct net_bridge_port *port)
1044 struct net_bridge_port *p;
1045 struct hlist_node *n, *slot = NULL;
1047 hlist_for_each_entry(p, n, &br->router_list, rlist) {
1048 if ((unsigned long) port >= (unsigned long) p)
1054 hlist_add_after_rcu(slot, &port->rlist);
1056 hlist_add_head_rcu(&port->rlist, &br->router_list);
1059 static void br_multicast_mark_router(struct net_bridge *br,
1060 struct net_bridge_port *port)
1062 unsigned long now = jiffies;
1065 if (br->multicast_router == 1)
1066 mod_timer(&br->multicast_router_timer,
1067 now + br->multicast_querier_interval);
1071 if (port->multicast_router != 1)
1074 if (!hlist_unhashed(&port->rlist))
1077 br_multicast_add_router(br, port);
1080 mod_timer(&port->multicast_router_timer,
1081 now + br->multicast_querier_interval);
1084 static void br_multicast_query_received(struct net_bridge *br,
1085 struct net_bridge_port *port,
1089 mod_timer(&br->multicast_querier_timer,
1090 jiffies + br->multicast_querier_interval);
1091 else if (timer_pending(&br->multicast_querier_timer))
1094 br_multicast_mark_router(br, port);
1097 static int br_ip4_multicast_query(struct net_bridge *br,
1098 struct net_bridge_port *port,
1099 struct sk_buff *skb)
1101 struct iphdr *iph = ip_hdr(skb);
1102 struct igmphdr *ih = igmp_hdr(skb);
1103 struct net_bridge_mdb_entry *mp;
1104 struct igmpv3_query *ih3;
1105 struct net_bridge_port_group *p;
1106 struct net_bridge_port_group **pp;
1107 unsigned long max_delay;
1108 unsigned long now = jiffies;
1112 spin_lock(&br->multicast_lock);
1113 if (!netif_running(br->dev) ||
1114 (port && port->state == BR_STATE_DISABLED))
1117 br_multicast_query_received(br, port, !!iph->saddr);
1121 if (skb->len == sizeof(*ih)) {
1122 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1125 max_delay = 10 * HZ;
1129 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) {
1134 ih3 = igmpv3_query_hdr(skb);
1138 max_delay = ih3->code ?
1139 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
1145 mp = br_mdb_ip4_get(br->mdb, group);
1149 max_delay *= br->multicast_last_member_count;
1151 if (!hlist_unhashed(&mp->mglist) &&
1152 (timer_pending(&mp->timer) ?
1153 time_after(mp->timer.expires, now + max_delay) :
1154 try_to_del_timer_sync(&mp->timer) >= 0))
1155 mod_timer(&mp->timer, now + max_delay);
1157 for (pp = &mp->ports; (p = *pp); pp = &p->next) {
1158 if (timer_pending(&p->timer) ?
1159 time_after(p->timer.expires, now + max_delay) :
1160 try_to_del_timer_sync(&p->timer) >= 0)
1161 mod_timer(&mp->timer, now + max_delay);
1165 spin_unlock(&br->multicast_lock);
1169 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1170 static int br_ip6_multicast_query(struct net_bridge *br,
1171 struct net_bridge_port *port,
1172 struct sk_buff *skb)
1174 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1175 struct mld_msg *mld = (struct mld_msg *) icmp6_hdr(skb);
1176 struct net_bridge_mdb_entry *mp;
1177 struct mld2_query *mld2q;
1178 struct net_bridge_port_group *p, **pp;
1179 unsigned long max_delay;
1180 unsigned long now = jiffies;
1181 struct in6_addr *group = NULL;
1184 spin_lock(&br->multicast_lock);
1185 if (!netif_running(br->dev) ||
1186 (port && port->state == BR_STATE_DISABLED))
1189 br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->saddr));
1191 if (skb->len == sizeof(*mld)) {
1192 if (!pskb_may_pull(skb, sizeof(*mld))) {
1196 mld = (struct mld_msg *) icmp6_hdr(skb);
1197 max_delay = msecs_to_jiffies(htons(mld->mld_maxdelay));
1199 group = &mld->mld_mca;
1200 } else if (skb->len >= sizeof(*mld2q)) {
1201 if (!pskb_may_pull(skb, sizeof(*mld2q))) {
1205 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1206 if (!mld2q->mld2q_nsrcs)
1207 group = &mld2q->mld2q_mca;
1208 max_delay = mld2q->mld2q_mrc ? MLDV2_MRC(mld2q->mld2q_mrc) : 1;
1214 mp = br_mdb_ip6_get(br->mdb, group);
1218 max_delay *= br->multicast_last_member_count;
1219 if (!hlist_unhashed(&mp->mglist) &&
1220 (timer_pending(&mp->timer) ?
1221 time_after(mp->timer.expires, now + max_delay) :
1222 try_to_del_timer_sync(&mp->timer) >= 0))
1223 mod_timer(&mp->timer, now + max_delay);
1225 for (pp = &mp->ports; (p = *pp); pp = &p->next) {
1226 if (timer_pending(&p->timer) ?
1227 time_after(p->timer.expires, now + max_delay) :
1228 try_to_del_timer_sync(&p->timer) >= 0)
1229 mod_timer(&mp->timer, now + max_delay);
1233 spin_unlock(&br->multicast_lock);
1238 static void br_multicast_leave_group(struct net_bridge *br,
1239 struct net_bridge_port *port,
1240 struct br_ip *group)
1242 struct net_bridge_mdb_htable *mdb;
1243 struct net_bridge_mdb_entry *mp;
1244 struct net_bridge_port_group *p;
1248 spin_lock(&br->multicast_lock);
1249 if (!netif_running(br->dev) ||
1250 (port && port->state == BR_STATE_DISABLED) ||
1251 timer_pending(&br->multicast_querier_timer))
1255 mp = br_mdb_ip_get(mdb, group);
1260 time = now + br->multicast_last_member_count *
1261 br->multicast_last_member_interval;
1264 if (!hlist_unhashed(&mp->mglist) &&
1265 (timer_pending(&mp->timer) ?
1266 time_after(mp->timer.expires, time) :
1267 try_to_del_timer_sync(&mp->timer) >= 0)) {
1268 mod_timer(&mp->timer, time);
1270 mp->queries_sent = 0;
1271 mod_timer(&mp->query_timer, now);
1277 for (p = mp->ports; p; p = p->next) {
1278 if (p->port != port)
1281 if (!hlist_unhashed(&p->mglist) &&
1282 (timer_pending(&p->timer) ?
1283 time_after(p->timer.expires, time) :
1284 try_to_del_timer_sync(&p->timer) >= 0)) {
1285 mod_timer(&p->timer, time);
1287 p->queries_sent = 0;
1288 mod_timer(&p->query_timer, now);
1295 spin_unlock(&br->multicast_lock);
1298 static void br_ip4_multicast_leave_group(struct net_bridge *br,
1299 struct net_bridge_port *port,
1302 struct br_ip br_group;
1304 if (ipv4_is_local_multicast(group))
1307 br_group.u.ip4 = group;
1308 br_group.proto = htons(ETH_P_IP);
1310 br_multicast_leave_group(br, port, &br_group);
1313 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1314 static void br_ip6_multicast_leave_group(struct net_bridge *br,
1315 struct net_bridge_port *port,
1316 const struct in6_addr *group)
1318 struct br_ip br_group;
1320 if (ipv6_is_local_multicast(group))
1323 ipv6_addr_copy(&br_group.u.ip6, group);
1324 br_group.proto = htons(ETH_P_IPV6);
1326 br_multicast_leave_group(br, port, &br_group);
1330 static int br_multicast_ipv4_rcv(struct net_bridge *br,
1331 struct net_bridge_port *port,
1332 struct sk_buff *skb)
1334 struct sk_buff *skb2 = skb;
1341 /* We treat OOM as packet loss for now. */
1342 if (!pskb_may_pull(skb, sizeof(*iph)))
1347 if (iph->ihl < 5 || iph->version != 4)
1350 if (!pskb_may_pull(skb, ip_hdrlen(skb)))
1355 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1358 if (iph->protocol != IPPROTO_IGMP)
1361 len = ntohs(iph->tot_len);
1362 if (skb->len < len || len < ip_hdrlen(skb))
1365 if (skb->len > len) {
1366 skb2 = skb_clone(skb, GFP_ATOMIC);
1370 err = pskb_trim_rcsum(skb2, len);
1375 len -= ip_hdrlen(skb2);
1376 offset = skb_network_offset(skb2) + ip_hdrlen(skb2);
1377 __skb_pull(skb2, offset);
1378 skb_reset_transport_header(skb2);
1381 if (!pskb_may_pull(skb2, sizeof(*ih)))
1384 switch (skb2->ip_summed) {
1385 case CHECKSUM_COMPLETE:
1386 if (!csum_fold(skb2->csum))
1391 if (skb_checksum_complete(skb2))
1397 BR_INPUT_SKB_CB(skb)->igmp = 1;
1398 ih = igmp_hdr(skb2);
1401 case IGMP_HOST_MEMBERSHIP_REPORT:
1402 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1403 BR_INPUT_SKB_CB(skb2)->mrouters_only = 1;
1404 err = br_ip4_multicast_add_group(br, port, ih->group);
1406 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1407 err = br_ip4_multicast_igmp3_report(br, port, skb2);
1409 case IGMP_HOST_MEMBERSHIP_QUERY:
1410 err = br_ip4_multicast_query(br, port, skb2);
1412 case IGMP_HOST_LEAVE_MESSAGE:
1413 br_ip4_multicast_leave_group(br, port, ih->group);
1418 __skb_push(skb2, offset);
1425 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1426 static int br_multicast_ipv6_rcv(struct net_bridge *br,
1427 struct net_bridge_port *port,
1428 struct sk_buff *skb)
1430 struct sk_buff *skb2 = skb;
1431 struct ipv6hdr *ip6h;
1432 struct icmp6hdr *icmp6h;
1438 if (!pskb_may_pull(skb, sizeof(*ip6h)))
1441 ip6h = ipv6_hdr(skb);
1444 * We're interested in MLD messages only.
1446 * - MLD has always Router Alert hop-by-hop option
1447 * - But we do not support jumbrograms.
1449 if (ip6h->version != 6 ||
1450 ip6h->nexthdr != IPPROTO_HOPOPTS ||
1451 ip6h->payload_len == 0)
1454 len = ntohs(ip6h->payload_len);
1458 nexthdr = ip6h->nexthdr;
1459 offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr);
1461 if (offset < 0 || nexthdr != IPPROTO_ICMPV6)
1464 /* Okay, we found ICMPv6 header */
1465 skb2 = skb_clone(skb, GFP_ATOMIC);
1469 len -= offset - skb_network_offset(skb2);
1471 __skb_pull(skb2, offset);
1472 skb_reset_transport_header(skb2);
1475 if (!pskb_may_pull(skb2, sizeof(*icmp6h)))
1478 icmp6h = icmp6_hdr(skb2);
1480 switch (icmp6h->icmp6_type) {
1481 case ICMPV6_MGM_QUERY:
1482 case ICMPV6_MGM_REPORT:
1483 case ICMPV6_MGM_REDUCTION:
1484 case ICMPV6_MLD2_REPORT:
1491 /* Okay, we found MLD message. Check further. */
1492 if (skb2->len > len) {
1493 err = pskb_trim_rcsum(skb2, len);
1498 switch (skb2->ip_summed) {
1499 case CHECKSUM_COMPLETE:
1500 if (!csum_fold(skb2->csum))
1505 if (skb_checksum_complete(skb2))
1511 BR_INPUT_SKB_CB(skb)->igmp = 1;
1513 switch (icmp6h->icmp6_type) {
1514 case ICMPV6_MGM_REPORT:
1516 struct mld_msg *mld = (struct mld_msg *)icmp6h;
1517 BR_INPUT_SKB_CB(skb2)->mrouters_only = 1;
1518 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca);
1521 case ICMPV6_MLD2_REPORT:
1522 err = br_ip6_multicast_mld2_report(br, port, skb2);
1524 case ICMPV6_MGM_QUERY:
1525 err = br_ip6_multicast_query(br, port, skb2);
1527 case ICMPV6_MGM_REDUCTION:
1529 struct mld_msg *mld = (struct mld_msg *)icmp6h;
1530 br_ip6_multicast_leave_group(br, port, &mld->mld_mca);
1535 __skb_push(skb2, offset);
1542 int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1543 struct sk_buff *skb)
1545 BR_INPUT_SKB_CB(skb)->igmp = 0;
1546 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1548 if (br->multicast_disabled)
1551 switch (skb->protocol) {
1552 case htons(ETH_P_IP):
1553 return br_multicast_ipv4_rcv(br, port, skb);
1554 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1555 case htons(ETH_P_IPV6):
1556 return br_multicast_ipv6_rcv(br, port, skb);
1563 static void br_multicast_query_expired(unsigned long data)
1565 struct net_bridge *br = (void *)data;
1567 spin_lock(&br->multicast_lock);
1568 if (br->multicast_startup_queries_sent <
1569 br->multicast_startup_query_count)
1570 br->multicast_startup_queries_sent++;
1572 br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent);
1574 spin_unlock(&br->multicast_lock);
1577 void br_multicast_init(struct net_bridge *br)
1579 br->hash_elasticity = 4;
1582 br->multicast_router = 1;
1583 br->multicast_last_member_count = 2;
1584 br->multicast_startup_query_count = 2;
1586 br->multicast_last_member_interval = HZ;
1587 br->multicast_query_response_interval = 10 * HZ;
1588 br->multicast_startup_query_interval = 125 * HZ / 4;
1589 br->multicast_query_interval = 125 * HZ;
1590 br->multicast_querier_interval = 255 * HZ;
1591 br->multicast_membership_interval = 260 * HZ;
1593 spin_lock_init(&br->multicast_lock);
1594 setup_timer(&br->multicast_router_timer,
1595 br_multicast_local_router_expired, 0);
1596 setup_timer(&br->multicast_querier_timer,
1597 br_multicast_local_router_expired, 0);
1598 setup_timer(&br->multicast_query_timer, br_multicast_query_expired,
1602 void br_multicast_open(struct net_bridge *br)
1604 br->multicast_startup_queries_sent = 0;
1606 if (br->multicast_disabled)
1609 mod_timer(&br->multicast_query_timer, jiffies);
1612 void br_multicast_stop(struct net_bridge *br)
1614 struct net_bridge_mdb_htable *mdb;
1615 struct net_bridge_mdb_entry *mp;
1616 struct hlist_node *p, *n;
1620 del_timer_sync(&br->multicast_router_timer);
1621 del_timer_sync(&br->multicast_querier_timer);
1622 del_timer_sync(&br->multicast_query_timer);
1624 spin_lock_bh(&br->multicast_lock);
1632 for (i = 0; i < mdb->max; i++) {
1633 hlist_for_each_entry_safe(mp, p, n, &mdb->mhash[i],
1635 del_timer(&mp->timer);
1636 del_timer(&mp->query_timer);
1637 call_rcu_bh(&mp->rcu, br_multicast_free_group);
1642 spin_unlock_bh(&br->multicast_lock);
1644 spin_lock_bh(&br->multicast_lock);
1649 call_rcu_bh(&mdb->rcu, br_mdb_free);
1652 spin_unlock_bh(&br->multicast_lock);
1655 int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1659 spin_lock_bh(&br->multicast_lock);
1660 if (!netif_running(br->dev))
1666 del_timer(&br->multicast_router_timer);
1669 br->multicast_router = val;
1679 spin_unlock_bh(&br->multicast_lock);
1684 int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1686 struct net_bridge *br = p->br;
1689 spin_lock(&br->multicast_lock);
1690 if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED)
1697 p->multicast_router = val;
1700 if (val < 2 && !hlist_unhashed(&p->rlist))
1701 hlist_del_init_rcu(&p->rlist);
1706 del_timer(&p->multicast_router_timer);
1711 br_multicast_add_router(br, p);
1720 spin_unlock(&br->multicast_lock);
1725 int br_multicast_toggle(struct net_bridge *br, unsigned long val)
1727 struct net_bridge_port *port;
1730 spin_lock(&br->multicast_lock);
1731 if (!netif_running(br->dev))
1735 if (br->multicast_disabled == !val)
1738 br->multicast_disabled = !val;
1739 if (br->multicast_disabled)
1746 br->multicast_disabled = !!val;
1750 err = br_mdb_rehash(&br->mdb, br->mdb->max,
1751 br->hash_elasticity);
1756 br_multicast_open(br);
1757 list_for_each_entry(port, &br->port_list, list) {
1758 if (port->state == BR_STATE_DISABLED ||
1759 port->state == BR_STATE_BLOCKING)
1762 __br_multicast_enable_port(port);
1766 spin_unlock(&br->multicast_lock);
1771 int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
1776 spin_lock(&br->multicast_lock);
1777 if (!netif_running(br->dev))
1781 if (!is_power_of_2(val))
1783 if (br->mdb && val < br->mdb->size)
1799 err = br_mdb_rehash(&br->mdb, br->hash_max,
1800 br->hash_elasticity);
1806 spin_unlock(&br->multicast_lock);