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_ip_get(
103 struct net_bridge_mdb_htable *mdb, struct br_ip *dst)
108 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
111 static struct net_bridge_mdb_entry *br_mdb_ip4_get(
112 struct net_bridge_mdb_htable *mdb, __be32 dst)
117 br_dst.proto = htons(ETH_P_IP);
119 return br_mdb_ip_get(mdb, &br_dst);
122 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
123 static struct net_bridge_mdb_entry *br_mdb_ip6_get(
124 struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst)
128 ipv6_addr_copy(&br_dst.u.ip6, dst);
129 br_dst.proto = htons(ETH_P_IPV6);
131 return br_mdb_ip_get(mdb, &br_dst);
135 struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
138 struct net_bridge_mdb_htable *mdb = br->mdb;
141 if (br->multicast_disabled)
144 if (BR_INPUT_SKB_CB(skb)->igmp)
147 ip.proto = skb->protocol;
149 switch (skb->protocol) {
150 case htons(ETH_P_IP):
151 ip.u.ip4 = ip_hdr(skb)->daddr;
153 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
154 case htons(ETH_P_IPV6):
155 ipv6_addr_copy(&ip.u.ip6, &ipv6_hdr(skb)->daddr);
162 return br_mdb_ip_get(mdb, &ip);
165 static void br_mdb_free(struct rcu_head *head)
167 struct net_bridge_mdb_htable *mdb =
168 container_of(head, struct net_bridge_mdb_htable, rcu);
169 struct net_bridge_mdb_htable *old = mdb->old;
176 static int br_mdb_copy(struct net_bridge_mdb_htable *new,
177 struct net_bridge_mdb_htable *old,
180 struct net_bridge_mdb_entry *mp;
181 struct hlist_node *p;
186 for (i = 0; i < old->max; i++)
187 hlist_for_each_entry(mp, p, &old->mhash[i], hlist[old->ver])
188 hlist_add_head(&mp->hlist[new->ver],
189 &new->mhash[br_ip_hash(new, &mp->addr)]);
195 for (i = 0; i < new->max; i++) {
197 hlist_for_each_entry(mp, p, &new->mhash[i], hlist[new->ver])
203 return maxlen > elasticity ? -EINVAL : 0;
206 static void br_multicast_free_pg(struct rcu_head *head)
208 struct net_bridge_port_group *p =
209 container_of(head, struct net_bridge_port_group, rcu);
214 static void br_multicast_free_group(struct rcu_head *head)
216 struct net_bridge_mdb_entry *mp =
217 container_of(head, struct net_bridge_mdb_entry, rcu);
222 static void br_multicast_group_expired(unsigned long data)
224 struct net_bridge_mdb_entry *mp = (void *)data;
225 struct net_bridge *br = mp->br;
226 struct net_bridge_mdb_htable *mdb;
228 spin_lock(&br->multicast_lock);
229 if (!netif_running(br->dev) || timer_pending(&mp->timer))
232 if (!hlist_unhashed(&mp->mglist))
233 hlist_del_init(&mp->mglist);
239 hlist_del_rcu(&mp->hlist[mdb->ver]);
242 del_timer(&mp->query_timer);
243 call_rcu_bh(&mp->rcu, br_multicast_free_group);
246 spin_unlock(&br->multicast_lock);
249 static void br_multicast_del_pg(struct net_bridge *br,
250 struct net_bridge_port_group *pg)
252 struct net_bridge_mdb_htable *mdb = br->mdb;
253 struct net_bridge_mdb_entry *mp;
254 struct net_bridge_port_group *p;
255 struct net_bridge_port_group **pp;
257 mp = br_mdb_ip_get(mdb, &pg->addr);
261 for (pp = &mp->ports; (p = *pp); pp = &p->next) {
265 rcu_assign_pointer(*pp, p->next);
266 hlist_del_init(&p->mglist);
267 del_timer(&p->timer);
268 del_timer(&p->query_timer);
269 call_rcu_bh(&p->rcu, br_multicast_free_pg);
271 if (!mp->ports && hlist_unhashed(&mp->mglist) &&
272 netif_running(br->dev))
273 mod_timer(&mp->timer, jiffies);
281 static void br_multicast_port_group_expired(unsigned long data)
283 struct net_bridge_port_group *pg = (void *)data;
284 struct net_bridge *br = pg->port->br;
286 spin_lock(&br->multicast_lock);
287 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
288 hlist_unhashed(&pg->mglist))
291 br_multicast_del_pg(br, pg);
294 spin_unlock(&br->multicast_lock);
297 static int br_mdb_rehash(struct net_bridge_mdb_htable **mdbp, int max,
300 struct net_bridge_mdb_htable *old = *mdbp;
301 struct net_bridge_mdb_htable *mdb;
304 mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
311 mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
317 mdb->size = old ? old->size : 0;
318 mdb->ver = old ? old->ver ^ 1 : 0;
320 if (!old || elasticity)
321 get_random_bytes(&mdb->secret, sizeof(mdb->secret));
323 mdb->secret = old->secret;
328 err = br_mdb_copy(mdb, old, elasticity);
335 call_rcu_bh(&mdb->rcu, br_mdb_free);
338 rcu_assign_pointer(*mdbp, mdb);
343 static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
351 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
356 skb->protocol = htons(ETH_P_IP);
358 skb_reset_mac_header(skb);
361 memcpy(eth->h_source, br->dev->dev_addr, 6);
364 eth->h_dest[2] = 0x5e;
368 eth->h_proto = htons(ETH_P_IP);
369 skb_put(skb, sizeof(*eth));
371 skb_set_network_header(skb, skb->len);
377 iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4);
379 iph->frag_off = htons(IP_DF);
381 iph->protocol = IPPROTO_IGMP;
383 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
384 ((u8 *)&iph[1])[0] = IPOPT_RA;
385 ((u8 *)&iph[1])[1] = 4;
386 ((u8 *)&iph[1])[2] = 0;
387 ((u8 *)&iph[1])[3] = 0;
391 skb_set_transport_header(skb, skb->len);
393 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
394 ih->code = (group ? br->multicast_last_member_interval :
395 br->multicast_query_response_interval) /
396 (HZ / IGMP_TIMER_SCALE);
399 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
400 skb_put(skb, sizeof(*ih));
402 __skb_pull(skb, sizeof(*eth));
408 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
409 static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
410 struct in6_addr *group)
413 struct ipv6hdr *ip6h;
414 struct mld_msg *mldq;
417 unsigned long interval;
419 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
424 skb->protocol = htons(ETH_P_IPV6);
426 /* Ethernet header */
427 skb_reset_mac_header(skb);
430 memcpy(eth->h_source, br->dev->dev_addr, 6);
431 ipv6_eth_mc_map(group, eth->h_dest);
432 eth->h_proto = htons(ETH_P_IPV6);
433 skb_put(skb, sizeof(*eth));
435 /* IPv6 header + HbH option */
436 skb_set_network_header(skb, skb->len);
437 ip6h = ipv6_hdr(skb);
439 *(__force __be32 *)ip6h = htonl(0x60000000);
440 ip6h->payload_len = 8 + sizeof(*mldq);
441 ip6h->nexthdr = IPPROTO_HOPOPTS;
443 ipv6_addr_set(&ip6h->saddr, 0, 0, 0, 0);
444 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
446 hopopt = (u8 *)(ip6h + 1);
447 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
448 hopopt[1] = 0; /* length of HbH */
449 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
450 hopopt[3] = 2; /* Length of RA Option */
451 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
453 hopopt[6] = IPV6_TLV_PAD0; /* Pad0 */
454 hopopt[7] = IPV6_TLV_PAD0; /* Pad0 */
456 skb_put(skb, sizeof(*ip6h) + 8);
459 skb_set_transport_header(skb, skb->len);
460 mldq = (struct mld_msg *) icmp6_hdr(skb);
462 interval = ipv6_addr_any(group) ? br->multicast_last_member_interval :
463 br->multicast_query_response_interval;
465 mldq->mld_type = ICMPV6_MGM_QUERY;
468 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
469 mldq->mld_reserved = 0;
470 ipv6_addr_copy(&mldq->mld_mca, group);
473 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
474 sizeof(*mldq), IPPROTO_ICMPV6,
477 skb_put(skb, sizeof(*mldq));
479 __skb_pull(skb, sizeof(*eth));
486 static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
489 switch (addr->proto) {
490 case htons(ETH_P_IP):
491 return br_ip4_multicast_alloc_query(br, addr->u.ip4);
492 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
493 case htons(ETH_P_IPV6):
494 return br_ip6_multicast_alloc_query(br, &addr->u.ip6);
500 static void br_multicast_send_group_query(struct net_bridge_mdb_entry *mp)
502 struct net_bridge *br = mp->br;
505 skb = br_multicast_alloc_query(br, &mp->addr);
512 if (++mp->queries_sent < br->multicast_last_member_count)
513 mod_timer(&mp->query_timer,
514 jiffies + br->multicast_last_member_interval);
517 static void br_multicast_group_query_expired(unsigned long data)
519 struct net_bridge_mdb_entry *mp = (void *)data;
520 struct net_bridge *br = mp->br;
522 spin_lock(&br->multicast_lock);
523 if (!netif_running(br->dev) || hlist_unhashed(&mp->mglist) ||
524 mp->queries_sent >= br->multicast_last_member_count)
527 br_multicast_send_group_query(mp);
530 spin_unlock(&br->multicast_lock);
533 static void br_multicast_send_port_group_query(struct net_bridge_port_group *pg)
535 struct net_bridge_port *port = pg->port;
536 struct net_bridge *br = port->br;
539 skb = br_multicast_alloc_query(br, &pg->addr);
543 br_deliver(port, skb);
546 if (++pg->queries_sent < br->multicast_last_member_count)
547 mod_timer(&pg->query_timer,
548 jiffies + br->multicast_last_member_interval);
551 static void br_multicast_port_group_query_expired(unsigned long data)
553 struct net_bridge_port_group *pg = (void *)data;
554 struct net_bridge_port *port = pg->port;
555 struct net_bridge *br = port->br;
557 spin_lock(&br->multicast_lock);
558 if (!netif_running(br->dev) || hlist_unhashed(&pg->mglist) ||
559 pg->queries_sent >= br->multicast_last_member_count)
562 br_multicast_send_port_group_query(pg);
565 spin_unlock(&br->multicast_lock);
568 static struct net_bridge_mdb_entry *br_multicast_get_group(
569 struct net_bridge *br, struct net_bridge_port *port,
570 struct br_ip *group, int hash)
572 struct net_bridge_mdb_htable *mdb = br->mdb;
573 struct net_bridge_mdb_entry *mp;
574 struct hlist_node *p;
580 hlist_for_each_entry(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) {
582 if (unlikely(br_ip_equal(group, &mp->addr)))
589 if (unlikely(count > br->hash_elasticity && count)) {
591 br_info(br, "Multicast hash table "
592 "chain limit reached: %s\n",
593 port ? port->dev->name : br->dev->name);
595 elasticity = br->hash_elasticity;
598 if (mdb->size >= max) {
600 if (unlikely(max >= br->hash_max)) {
601 br_warn(br, "Multicast hash table maximum "
602 "reached, disabling snooping: %s, %d\n",
603 port ? port->dev->name : br->dev->name, max);
606 br->multicast_disabled = 1;
611 if (max > mdb->max || elasticity) {
614 br_info(br, "Multicast hash table "
616 port ? port->dev->name : br->dev->name);
621 err = br_mdb_rehash(&br->mdb, max, elasticity);
623 br_warn(br, "Cannot rehash multicast "
624 "hash table, disabling snooping: %s, %d, %d\n",
625 port ? port->dev->name : br->dev->name,
641 static struct net_bridge_mdb_entry *br_multicast_new_group(
642 struct net_bridge *br, struct net_bridge_port *port,
645 struct net_bridge_mdb_htable *mdb = br->mdb;
646 struct net_bridge_mdb_entry *mp;
650 if (br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0))
655 hash = br_ip_hash(mdb, group);
656 mp = br_multicast_get_group(br, port, group, hash);
657 switch (PTR_ERR(mp)) {
664 hash = br_ip_hash(mdb, group);
671 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
677 setup_timer(&mp->timer, br_multicast_group_expired,
679 setup_timer(&mp->query_timer, br_multicast_group_query_expired,
682 hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
689 static int br_multicast_add_group(struct net_bridge *br,
690 struct net_bridge_port *port,
693 struct net_bridge_mdb_entry *mp;
694 struct net_bridge_port_group *p;
695 struct net_bridge_port_group **pp;
696 unsigned long now = jiffies;
699 spin_lock(&br->multicast_lock);
700 if (!netif_running(br->dev) ||
701 (port && port->state == BR_STATE_DISABLED))
704 mp = br_multicast_new_group(br, port, group);
706 if (unlikely(IS_ERR(mp) || !mp))
710 hlist_add_head(&mp->mglist, &br->mglist);
711 mod_timer(&mp->timer, now + br->multicast_membership_interval);
715 for (pp = &mp->ports; (p = *pp); pp = &p->next) {
718 if ((unsigned long)p->port < (unsigned long)port)
722 p = kzalloc(sizeof(*p), GFP_ATOMIC);
730 hlist_add_head(&p->mglist, &port->mglist);
731 setup_timer(&p->timer, br_multicast_port_group_expired,
733 setup_timer(&p->query_timer, br_multicast_port_group_query_expired,
736 rcu_assign_pointer(*pp, p);
739 mod_timer(&p->timer, now + br->multicast_membership_interval);
744 spin_unlock(&br->multicast_lock);
748 static int br_ip4_multicast_add_group(struct net_bridge *br,
749 struct net_bridge_port *port,
752 struct br_ip br_group;
754 if (ipv4_is_local_multicast(group))
757 br_group.u.ip4 = group;
758 br_group.proto = htons(ETH_P_IP);
760 return br_multicast_add_group(br, port, &br_group);
763 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
764 static int br_ip6_multicast_add_group(struct net_bridge *br,
765 struct net_bridge_port *port,
766 const struct in6_addr *group)
768 struct br_ip br_group;
770 if (ipv6_is_local_multicast(group))
773 ipv6_addr_copy(&br_group.u.ip6, group);
774 br_group.proto = htons(ETH_P_IP);
776 return br_multicast_add_group(br, port, &br_group);
780 static void br_multicast_router_expired(unsigned long data)
782 struct net_bridge_port *port = (void *)data;
783 struct net_bridge *br = port->br;
785 spin_lock(&br->multicast_lock);
786 if (port->multicast_router != 1 ||
787 timer_pending(&port->multicast_router_timer) ||
788 hlist_unhashed(&port->rlist))
791 hlist_del_init_rcu(&port->rlist);
794 spin_unlock(&br->multicast_lock);
797 static void br_multicast_local_router_expired(unsigned long data)
801 static void __br_multicast_send_query(struct net_bridge *br,
802 struct net_bridge_port *port,
807 skb = br_multicast_alloc_query(br, ip);
812 __skb_push(skb, sizeof(struct ethhdr));
813 skb->dev = port->dev;
814 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
820 static void br_multicast_send_query(struct net_bridge *br,
821 struct net_bridge_port *port, u32 sent)
824 struct br_ip br_group;
826 if (!netif_running(br->dev) || br->multicast_disabled ||
827 timer_pending(&br->multicast_querier_timer))
830 memset(&br_group.u, 0, sizeof(br_group.u));
832 br_group.proto = htons(ETH_P_IP);
833 __br_multicast_send_query(br, port, &br_group);
835 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
836 br_group.proto = htons(ETH_P_IPV6);
837 __br_multicast_send_query(br, port, &br_group);
841 time += sent < br->multicast_startup_query_count ?
842 br->multicast_startup_query_interval :
843 br->multicast_query_interval;
844 mod_timer(port ? &port->multicast_query_timer :
845 &br->multicast_query_timer, time);
848 static void br_multicast_port_query_expired(unsigned long data)
850 struct net_bridge_port *port = (void *)data;
851 struct net_bridge *br = port->br;
853 spin_lock(&br->multicast_lock);
854 if (port->state == BR_STATE_DISABLED ||
855 port->state == BR_STATE_BLOCKING)
858 if (port->multicast_startup_queries_sent <
859 br->multicast_startup_query_count)
860 port->multicast_startup_queries_sent++;
862 br_multicast_send_query(port->br, port,
863 port->multicast_startup_queries_sent);
866 spin_unlock(&br->multicast_lock);
869 void br_multicast_add_port(struct net_bridge_port *port)
871 port->multicast_router = 1;
873 setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
874 (unsigned long)port);
875 setup_timer(&port->multicast_query_timer,
876 br_multicast_port_query_expired, (unsigned long)port);
879 void br_multicast_del_port(struct net_bridge_port *port)
881 del_timer_sync(&port->multicast_router_timer);
884 static void __br_multicast_enable_port(struct net_bridge_port *port)
886 port->multicast_startup_queries_sent = 0;
888 if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 ||
889 del_timer(&port->multicast_query_timer))
890 mod_timer(&port->multicast_query_timer, jiffies);
893 void br_multicast_enable_port(struct net_bridge_port *port)
895 struct net_bridge *br = port->br;
897 spin_lock(&br->multicast_lock);
898 if (br->multicast_disabled || !netif_running(br->dev))
901 __br_multicast_enable_port(port);
904 spin_unlock(&br->multicast_lock);
907 void br_multicast_disable_port(struct net_bridge_port *port)
909 struct net_bridge *br = port->br;
910 struct net_bridge_port_group *pg;
911 struct hlist_node *p, *n;
913 spin_lock(&br->multicast_lock);
914 hlist_for_each_entry_safe(pg, p, n, &port->mglist, mglist)
915 br_multicast_del_pg(br, pg);
917 if (!hlist_unhashed(&port->rlist))
918 hlist_del_init_rcu(&port->rlist);
919 del_timer(&port->multicast_router_timer);
920 del_timer(&port->multicast_query_timer);
921 spin_unlock(&br->multicast_lock);
924 static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
925 struct net_bridge_port *port,
928 struct igmpv3_report *ih;
929 struct igmpv3_grec *grec;
937 if (!pskb_may_pull(skb, sizeof(*ih)))
940 ih = igmpv3_report_hdr(skb);
941 num = ntohs(ih->ngrec);
944 for (i = 0; i < num; i++) {
945 len += sizeof(*grec);
946 if (!pskb_may_pull(skb, len))
949 grec = (void *)(skb->data + len - sizeof(*grec));
950 group = grec->grec_mca;
951 type = grec->grec_type;
953 len += ntohs(grec->grec_nsrcs) * 4;
954 if (!pskb_may_pull(skb, len))
957 /* We treat this as an IGMPv2 report for now. */
959 case IGMPV3_MODE_IS_INCLUDE:
960 case IGMPV3_MODE_IS_EXCLUDE:
961 case IGMPV3_CHANGE_TO_INCLUDE:
962 case IGMPV3_CHANGE_TO_EXCLUDE:
963 case IGMPV3_ALLOW_NEW_SOURCES:
964 case IGMPV3_BLOCK_OLD_SOURCES:
971 err = br_ip4_multicast_add_group(br, port, group);
979 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
980 static int br_ip6_multicast_mld2_report(struct net_bridge *br,
981 struct net_bridge_port *port,
984 struct icmp6hdr *icmp6h;
985 struct mld2_grec *grec;
991 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
994 icmp6h = icmp6_hdr(skb);
995 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
996 len = sizeof(*icmp6h);
998 for (i = 0; i < num; i++) {
999 __be16 *nsrcs, _nsrcs;
1001 nsrcs = skb_header_pointer(skb,
1002 len + offsetof(struct mld2_grec,
1004 sizeof(_nsrcs), &_nsrcs);
1008 if (!pskb_may_pull(skb,
1009 len + sizeof(*grec) +
1010 sizeof(struct in6_addr) * (*nsrcs)))
1013 grec = (struct mld2_grec *)(skb->data + len);
1014 len += sizeof(*grec) + sizeof(struct in6_addr) * (*nsrcs);
1016 /* We treat these as MLDv1 reports for now. */
1017 switch (grec->grec_type) {
1018 case MLD2_MODE_IS_INCLUDE:
1019 case MLD2_MODE_IS_EXCLUDE:
1020 case MLD2_CHANGE_TO_INCLUDE:
1021 case MLD2_CHANGE_TO_EXCLUDE:
1022 case MLD2_ALLOW_NEW_SOURCES:
1023 case MLD2_BLOCK_OLD_SOURCES:
1030 err = br_ip6_multicast_add_group(br, port, &grec->grec_mca);
1040 * Add port to rotuer_list
1041 * list is maintained ordered by pointer value
1042 * and locked by br->multicast_lock and RCU
1044 static void br_multicast_add_router(struct net_bridge *br,
1045 struct net_bridge_port *port)
1047 struct net_bridge_port *p;
1048 struct hlist_node *n, *slot = NULL;
1050 hlist_for_each_entry(p, n, &br->router_list, rlist) {
1051 if ((unsigned long) port >= (unsigned long) p)
1057 hlist_add_after_rcu(slot, &port->rlist);
1059 hlist_add_head_rcu(&port->rlist, &br->router_list);
1062 static void br_multicast_mark_router(struct net_bridge *br,
1063 struct net_bridge_port *port)
1065 unsigned long now = jiffies;
1068 if (br->multicast_router == 1)
1069 mod_timer(&br->multicast_router_timer,
1070 now + br->multicast_querier_interval);
1074 if (port->multicast_router != 1)
1077 if (!hlist_unhashed(&port->rlist))
1080 br_multicast_add_router(br, port);
1083 mod_timer(&port->multicast_router_timer,
1084 now + br->multicast_querier_interval);
1087 static void br_multicast_query_received(struct net_bridge *br,
1088 struct net_bridge_port *port,
1092 mod_timer(&br->multicast_querier_timer,
1093 jiffies + br->multicast_querier_interval);
1094 else if (timer_pending(&br->multicast_querier_timer))
1097 br_multicast_mark_router(br, port);
1100 static int br_ip4_multicast_query(struct net_bridge *br,
1101 struct net_bridge_port *port,
1102 struct sk_buff *skb)
1104 struct iphdr *iph = ip_hdr(skb);
1105 struct igmphdr *ih = igmp_hdr(skb);
1106 struct net_bridge_mdb_entry *mp;
1107 struct igmpv3_query *ih3;
1108 struct net_bridge_port_group *p;
1109 struct net_bridge_port_group **pp;
1110 unsigned long max_delay;
1111 unsigned long now = jiffies;
1115 spin_lock(&br->multicast_lock);
1116 if (!netif_running(br->dev) ||
1117 (port && port->state == BR_STATE_DISABLED))
1120 br_multicast_query_received(br, port, !!iph->saddr);
1124 if (skb->len == sizeof(*ih)) {
1125 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1128 max_delay = 10 * HZ;
1132 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) {
1137 ih3 = igmpv3_query_hdr(skb);
1141 max_delay = ih3->code ?
1142 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
1148 mp = br_mdb_ip4_get(br->mdb, group);
1152 max_delay *= br->multicast_last_member_count;
1154 if (!hlist_unhashed(&mp->mglist) &&
1155 (timer_pending(&mp->timer) ?
1156 time_after(mp->timer.expires, now + max_delay) :
1157 try_to_del_timer_sync(&mp->timer) >= 0))
1158 mod_timer(&mp->timer, now + max_delay);
1160 for (pp = &mp->ports; (p = *pp); pp = &p->next) {
1161 if (timer_pending(&p->timer) ?
1162 time_after(p->timer.expires, now + max_delay) :
1163 try_to_del_timer_sync(&p->timer) >= 0)
1164 mod_timer(&mp->timer, now + max_delay);
1168 spin_unlock(&br->multicast_lock);
1172 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1173 static int br_ip6_multicast_query(struct net_bridge *br,
1174 struct net_bridge_port *port,
1175 struct sk_buff *skb)
1177 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1178 struct mld_msg *mld = (struct mld_msg *) icmp6_hdr(skb);
1179 struct net_bridge_mdb_entry *mp;
1180 struct mld2_query *mld2q;
1181 struct net_bridge_port_group *p, **pp;
1182 unsigned long max_delay;
1183 unsigned long now = jiffies;
1184 struct in6_addr *group = NULL;
1187 spin_lock(&br->multicast_lock);
1188 if (!netif_running(br->dev) ||
1189 (port && port->state == BR_STATE_DISABLED))
1192 br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->saddr));
1194 if (skb->len == sizeof(*mld)) {
1195 if (!pskb_may_pull(skb, sizeof(*mld))) {
1199 mld = (struct mld_msg *) icmp6_hdr(skb);
1200 max_delay = msecs_to_jiffies(htons(mld->mld_maxdelay));
1202 group = &mld->mld_mca;
1203 } else if (skb->len >= sizeof(*mld2q)) {
1204 if (!pskb_may_pull(skb, sizeof(*mld2q))) {
1208 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1209 if (!mld2q->mld2q_nsrcs)
1210 group = &mld2q->mld2q_mca;
1211 max_delay = mld2q->mld2q_mrc ? MLDV2_MRC(mld2q->mld2q_mrc) : 1;
1217 mp = br_mdb_ip6_get(br->mdb, group);
1221 max_delay *= br->multicast_last_member_count;
1222 if (!hlist_unhashed(&mp->mglist) &&
1223 (timer_pending(&mp->timer) ?
1224 time_after(mp->timer.expires, now + max_delay) :
1225 try_to_del_timer_sync(&mp->timer) >= 0))
1226 mod_timer(&mp->timer, now + max_delay);
1228 for (pp = &mp->ports; (p = *pp); pp = &p->next) {
1229 if (timer_pending(&p->timer) ?
1230 time_after(p->timer.expires, now + max_delay) :
1231 try_to_del_timer_sync(&p->timer) >= 0)
1232 mod_timer(&mp->timer, now + max_delay);
1236 spin_unlock(&br->multicast_lock);
1241 static void br_multicast_leave_group(struct net_bridge *br,
1242 struct net_bridge_port *port,
1243 struct br_ip *group)
1245 struct net_bridge_mdb_htable *mdb;
1246 struct net_bridge_mdb_entry *mp;
1247 struct net_bridge_port_group *p;
1251 spin_lock(&br->multicast_lock);
1252 if (!netif_running(br->dev) ||
1253 (port && port->state == BR_STATE_DISABLED) ||
1254 timer_pending(&br->multicast_querier_timer))
1258 mp = br_mdb_ip_get(mdb, group);
1263 time = now + br->multicast_last_member_count *
1264 br->multicast_last_member_interval;
1267 if (!hlist_unhashed(&mp->mglist) &&
1268 (timer_pending(&mp->timer) ?
1269 time_after(mp->timer.expires, time) :
1270 try_to_del_timer_sync(&mp->timer) >= 0)) {
1271 mod_timer(&mp->timer, time);
1273 mp->queries_sent = 0;
1274 mod_timer(&mp->query_timer, now);
1280 for (p = mp->ports; p; p = p->next) {
1281 if (p->port != port)
1284 if (!hlist_unhashed(&p->mglist) &&
1285 (timer_pending(&p->timer) ?
1286 time_after(p->timer.expires, time) :
1287 try_to_del_timer_sync(&p->timer) >= 0)) {
1288 mod_timer(&p->timer, time);
1290 p->queries_sent = 0;
1291 mod_timer(&p->query_timer, now);
1298 spin_unlock(&br->multicast_lock);
1301 static void br_ip4_multicast_leave_group(struct net_bridge *br,
1302 struct net_bridge_port *port,
1305 struct br_ip br_group;
1307 if (ipv4_is_local_multicast(group))
1310 br_group.u.ip4 = group;
1311 br_group.proto = htons(ETH_P_IP);
1313 br_multicast_leave_group(br, port, &br_group);
1316 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1317 static void br_ip6_multicast_leave_group(struct net_bridge *br,
1318 struct net_bridge_port *port,
1319 const struct in6_addr *group)
1321 struct br_ip br_group;
1323 if (ipv6_is_local_multicast(group))
1326 ipv6_addr_copy(&br_group.u.ip6, group);
1327 br_group.proto = htons(ETH_P_IPV6);
1329 br_multicast_leave_group(br, port, &br_group);
1333 static int br_multicast_ipv4_rcv(struct net_bridge *br,
1334 struct net_bridge_port *port,
1335 struct sk_buff *skb)
1337 struct sk_buff *skb2 = skb;
1344 /* We treat OOM as packet loss for now. */
1345 if (!pskb_may_pull(skb, sizeof(*iph)))
1350 if (iph->ihl < 5 || iph->version != 4)
1353 if (!pskb_may_pull(skb, ip_hdrlen(skb)))
1358 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1361 if (iph->protocol != IPPROTO_IGMP)
1364 len = ntohs(iph->tot_len);
1365 if (skb->len < len || len < ip_hdrlen(skb))
1368 if (skb->len > len) {
1369 skb2 = skb_clone(skb, GFP_ATOMIC);
1373 err = pskb_trim_rcsum(skb2, len);
1378 len -= ip_hdrlen(skb2);
1379 offset = skb_network_offset(skb2) + ip_hdrlen(skb2);
1380 __skb_pull(skb2, offset);
1381 skb_reset_transport_header(skb2);
1384 if (!pskb_may_pull(skb2, sizeof(*ih)))
1387 switch (skb2->ip_summed) {
1388 case CHECKSUM_COMPLETE:
1389 if (!csum_fold(skb2->csum))
1394 if (skb_checksum_complete(skb2))
1400 BR_INPUT_SKB_CB(skb)->igmp = 1;
1401 ih = igmp_hdr(skb2);
1404 case IGMP_HOST_MEMBERSHIP_REPORT:
1405 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1406 BR_INPUT_SKB_CB(skb2)->mrouters_only = 1;
1407 err = br_ip4_multicast_add_group(br, port, ih->group);
1409 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1410 err = br_ip4_multicast_igmp3_report(br, port, skb2);
1412 case IGMP_HOST_MEMBERSHIP_QUERY:
1413 err = br_ip4_multicast_query(br, port, skb2);
1415 case IGMP_HOST_LEAVE_MESSAGE:
1416 br_ip4_multicast_leave_group(br, port, ih->group);
1421 __skb_push(skb2, offset);
1428 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1429 static int br_multicast_ipv6_rcv(struct net_bridge *br,
1430 struct net_bridge_port *port,
1431 struct sk_buff *skb)
1433 struct sk_buff *skb2 = skb;
1434 struct ipv6hdr *ip6h;
1435 struct icmp6hdr *icmp6h;
1441 if (!pskb_may_pull(skb, sizeof(*ip6h)))
1444 ip6h = ipv6_hdr(skb);
1447 * We're interested in MLD messages only.
1449 * - MLD has always Router Alert hop-by-hop option
1450 * - But we do not support jumbrograms.
1452 if (ip6h->version != 6 ||
1453 ip6h->nexthdr != IPPROTO_HOPOPTS ||
1454 ip6h->payload_len == 0)
1457 len = ntohs(ip6h->payload_len);
1461 nexthdr = ip6h->nexthdr;
1462 offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr);
1464 if (offset < 0 || nexthdr != IPPROTO_ICMPV6)
1467 /* Okay, we found ICMPv6 header */
1468 skb2 = skb_clone(skb, GFP_ATOMIC);
1472 len -= offset - skb_network_offset(skb2);
1474 __skb_pull(skb2, offset);
1475 skb_reset_transport_header(skb2);
1478 if (!pskb_may_pull(skb2, sizeof(*icmp6h)))
1481 icmp6h = icmp6_hdr(skb2);
1483 switch (icmp6h->icmp6_type) {
1484 case ICMPV6_MGM_QUERY:
1485 case ICMPV6_MGM_REPORT:
1486 case ICMPV6_MGM_REDUCTION:
1487 case ICMPV6_MLD2_REPORT:
1494 /* Okay, we found MLD message. Check further. */
1495 if (skb2->len > len) {
1496 err = pskb_trim_rcsum(skb2, len);
1501 switch (skb2->ip_summed) {
1502 case CHECKSUM_COMPLETE:
1503 if (!csum_fold(skb2->csum))
1508 if (skb_checksum_complete(skb2))
1514 BR_INPUT_SKB_CB(skb)->igmp = 1;
1516 switch (icmp6h->icmp6_type) {
1517 case ICMPV6_MGM_REPORT:
1519 struct mld_msg *mld = (struct mld_msg *)icmp6h;
1520 BR_INPUT_SKB_CB(skb2)->mrouters_only = 1;
1521 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca);
1524 case ICMPV6_MLD2_REPORT:
1525 err = br_ip6_multicast_mld2_report(br, port, skb2);
1527 case ICMPV6_MGM_QUERY:
1528 err = br_ip6_multicast_query(br, port, skb2);
1530 case ICMPV6_MGM_REDUCTION:
1532 struct mld_msg *mld = (struct mld_msg *)icmp6h;
1533 br_ip6_multicast_leave_group(br, port, &mld->mld_mca);
1538 __skb_push(skb2, offset);
1545 int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1546 struct sk_buff *skb)
1548 BR_INPUT_SKB_CB(skb)->igmp = 0;
1549 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1551 if (br->multicast_disabled)
1554 switch (skb->protocol) {
1555 case htons(ETH_P_IP):
1556 return br_multicast_ipv4_rcv(br, port, skb);
1557 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1558 case htons(ETH_P_IPV6):
1559 return br_multicast_ipv6_rcv(br, port, skb);
1566 static void br_multicast_query_expired(unsigned long data)
1568 struct net_bridge *br = (void *)data;
1570 spin_lock(&br->multicast_lock);
1571 if (br->multicast_startup_queries_sent <
1572 br->multicast_startup_query_count)
1573 br->multicast_startup_queries_sent++;
1575 br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent);
1577 spin_unlock(&br->multicast_lock);
1580 void br_multicast_init(struct net_bridge *br)
1582 br->hash_elasticity = 4;
1585 br->multicast_router = 1;
1586 br->multicast_last_member_count = 2;
1587 br->multicast_startup_query_count = 2;
1589 br->multicast_last_member_interval = HZ;
1590 br->multicast_query_response_interval = 10 * HZ;
1591 br->multicast_startup_query_interval = 125 * HZ / 4;
1592 br->multicast_query_interval = 125 * HZ;
1593 br->multicast_querier_interval = 255 * HZ;
1594 br->multicast_membership_interval = 260 * HZ;
1596 spin_lock_init(&br->multicast_lock);
1597 setup_timer(&br->multicast_router_timer,
1598 br_multicast_local_router_expired, 0);
1599 setup_timer(&br->multicast_querier_timer,
1600 br_multicast_local_router_expired, 0);
1601 setup_timer(&br->multicast_query_timer, br_multicast_query_expired,
1605 void br_multicast_open(struct net_bridge *br)
1607 br->multicast_startup_queries_sent = 0;
1609 if (br->multicast_disabled)
1612 mod_timer(&br->multicast_query_timer, jiffies);
1615 void br_multicast_stop(struct net_bridge *br)
1617 struct net_bridge_mdb_htable *mdb;
1618 struct net_bridge_mdb_entry *mp;
1619 struct hlist_node *p, *n;
1623 del_timer_sync(&br->multicast_router_timer);
1624 del_timer_sync(&br->multicast_querier_timer);
1625 del_timer_sync(&br->multicast_query_timer);
1627 spin_lock_bh(&br->multicast_lock);
1635 for (i = 0; i < mdb->max; i++) {
1636 hlist_for_each_entry_safe(mp, p, n, &mdb->mhash[i],
1638 del_timer(&mp->timer);
1639 del_timer(&mp->query_timer);
1640 call_rcu_bh(&mp->rcu, br_multicast_free_group);
1645 spin_unlock_bh(&br->multicast_lock);
1647 spin_lock_bh(&br->multicast_lock);
1652 call_rcu_bh(&mdb->rcu, br_mdb_free);
1655 spin_unlock_bh(&br->multicast_lock);
1658 int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1662 spin_lock_bh(&br->multicast_lock);
1663 if (!netif_running(br->dev))
1669 del_timer(&br->multicast_router_timer);
1672 br->multicast_router = val;
1682 spin_unlock_bh(&br->multicast_lock);
1687 int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1689 struct net_bridge *br = p->br;
1692 spin_lock(&br->multicast_lock);
1693 if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED)
1700 p->multicast_router = val;
1703 if (val < 2 && !hlist_unhashed(&p->rlist))
1704 hlist_del_init_rcu(&p->rlist);
1709 del_timer(&p->multicast_router_timer);
1714 br_multicast_add_router(br, p);
1723 spin_unlock(&br->multicast_lock);
1728 int br_multicast_toggle(struct net_bridge *br, unsigned long val)
1730 struct net_bridge_port *port;
1733 spin_lock(&br->multicast_lock);
1734 if (br->multicast_disabled == !val)
1737 br->multicast_disabled = !val;
1738 if (br->multicast_disabled)
1741 if (!netif_running(br->dev))
1748 br->multicast_disabled = !!val;
1752 err = br_mdb_rehash(&br->mdb, br->mdb->max,
1753 br->hash_elasticity);
1758 br_multicast_open(br);
1759 list_for_each_entry(port, &br->port_list, list) {
1760 if (port->state == BR_STATE_DISABLED ||
1761 port->state == BR_STATE_BLOCKING)
1764 __br_multicast_enable_port(port);
1768 spin_unlock(&br->multicast_lock);
1773 int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
1778 spin_lock(&br->multicast_lock);
1779 if (!netif_running(br->dev))
1783 if (!is_power_of_2(val))
1785 if (br->mdb && val < br->mdb->size)
1801 err = br_mdb_rehash(&br->mdb, br->hash_max,
1802 br->hash_elasticity);
1808 spin_unlock(&br->multicast_lock);