mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / net / bridge / br_multicast.c
blob91fed8147c39bdd2795d9835654ab4748d62822a
1 /*
2 * Bridge multicast support.
4 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
13 #include <linux/err.h>
14 #include <linux/if_ether.h>
15 #include <linux/igmp.h>
16 #include <linux/jhash.h>
17 #include <linux/kernel.h>
18 #include <linux/log2.h>
19 #include <linux/netdevice.h>
20 #include <linux/netfilter_bridge.h>
21 #include <linux/random.h>
22 #include <linux/rculist.h>
23 #include <linux/skbuff.h>
24 #include <linux/slab.h>
25 #include <linux/timer.h>
26 #include <linux/inetdevice.h>
27 #include <net/ip.h>
28 #if IS_ENABLED(CONFIG_IPV6)
29 #include <net/ipv6.h>
30 #include <net/mld.h>
31 #include <net/ip6_checksum.h>
32 #include <net/addrconf.h>
33 #endif
35 #include "br_private.h"
37 static void br_multicast_start_querier(struct net_bridge *br,
38 struct bridge_mcast_query *query);
39 unsigned int br_mdb_rehash_seq;
41 static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
43 if (a->proto != b->proto)
44 return 0;
45 if (a->vid != b->vid)
46 return 0;
47 switch (a->proto) {
48 case htons(ETH_P_IP):
49 return a->u.ip4 == b->u.ip4;
50 #if IS_ENABLED(CONFIG_IPV6)
51 case htons(ETH_P_IPV6):
52 return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
53 #endif
55 return 0;
58 static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip,
59 __u16 vid)
61 return jhash_2words((__force u32)ip, vid, mdb->secret) & (mdb->max - 1);
64 #if IS_ENABLED(CONFIG_IPV6)
65 static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb,
66 const struct in6_addr *ip,
67 __u16 vid)
69 return jhash_2words(ipv6_addr_hash(ip), vid,
70 mdb->secret) & (mdb->max - 1);
72 #endif
74 static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb,
75 struct br_ip *ip)
77 switch (ip->proto) {
78 case htons(ETH_P_IP):
79 return __br_ip4_hash(mdb, ip->u.ip4, ip->vid);
80 #if IS_ENABLED(CONFIG_IPV6)
81 case htons(ETH_P_IPV6):
82 return __br_ip6_hash(mdb, &ip->u.ip6, ip->vid);
83 #endif
85 return 0;
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;
93 hlist_for_each_entry_rcu(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
94 if (br_ip_equal(&mp->addr, dst))
95 return mp;
98 return NULL;
101 struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge_mdb_htable *mdb,
102 struct br_ip *dst)
104 if (!mdb)
105 return NULL;
107 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
110 static struct net_bridge_mdb_entry *br_mdb_ip4_get(
111 struct net_bridge_mdb_htable *mdb, __be32 dst, __u16 vid)
113 struct br_ip br_dst;
115 br_dst.u.ip4 = dst;
116 br_dst.proto = htons(ETH_P_IP);
117 br_dst.vid = vid;
119 return br_mdb_ip_get(mdb, &br_dst);
122 #if IS_ENABLED(CONFIG_IPV6)
123 static struct net_bridge_mdb_entry *br_mdb_ip6_get(
124 struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst,
125 __u16 vid)
127 struct br_ip br_dst;
129 br_dst.u.ip6 = *dst;
130 br_dst.proto = htons(ETH_P_IPV6);
131 br_dst.vid = vid;
133 return br_mdb_ip_get(mdb, &br_dst);
135 #endif
137 struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
138 struct sk_buff *skb, u16 vid)
140 struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb);
141 struct br_ip ip;
143 if (br->multicast_disabled)
144 return NULL;
146 if (BR_INPUT_SKB_CB(skb)->igmp)
147 return NULL;
149 ip.proto = skb->protocol;
150 ip.vid = vid;
152 switch (skb->protocol) {
153 case htons(ETH_P_IP):
154 ip.u.ip4 = ip_hdr(skb)->daddr;
155 break;
156 #if IS_ENABLED(CONFIG_IPV6)
157 case htons(ETH_P_IPV6):
158 ip.u.ip6 = ipv6_hdr(skb)->daddr;
159 break;
160 #endif
161 default:
162 return NULL;
165 return br_mdb_ip_get(mdb, &ip);
168 static void br_mdb_free(struct rcu_head *head)
170 struct net_bridge_mdb_htable *mdb =
171 container_of(head, struct net_bridge_mdb_htable, rcu);
172 struct net_bridge_mdb_htable *old = mdb->old;
174 mdb->old = NULL;
175 kfree(old->mhash);
176 kfree(old);
179 static int br_mdb_copy(struct net_bridge_mdb_htable *new,
180 struct net_bridge_mdb_htable *old,
181 int elasticity)
183 struct net_bridge_mdb_entry *mp;
184 int maxlen;
185 int len;
186 int i;
188 for (i = 0; i < old->max; i++)
189 hlist_for_each_entry(mp, &old->mhash[i], hlist[old->ver])
190 hlist_add_head(&mp->hlist[new->ver],
191 &new->mhash[br_ip_hash(new, &mp->addr)]);
193 if (!elasticity)
194 return 0;
196 maxlen = 0;
197 for (i = 0; i < new->max; i++) {
198 len = 0;
199 hlist_for_each_entry(mp, &new->mhash[i], hlist[new->ver])
200 len++;
201 if (len > maxlen)
202 maxlen = len;
205 return maxlen > elasticity ? -EINVAL : 0;
208 void br_multicast_free_pg(struct rcu_head *head)
210 struct net_bridge_port_group *p =
211 container_of(head, struct net_bridge_port_group, rcu);
213 kfree(p);
216 static void br_multicast_free_group(struct rcu_head *head)
218 struct net_bridge_mdb_entry *mp =
219 container_of(head, struct net_bridge_mdb_entry, rcu);
221 kfree(mp);
224 static void br_multicast_group_expired(unsigned long data)
226 struct net_bridge_mdb_entry *mp = (void *)data;
227 struct net_bridge *br = mp->br;
228 struct net_bridge_mdb_htable *mdb;
230 spin_lock(&br->multicast_lock);
231 if (!netif_running(br->dev) || timer_pending(&mp->timer))
232 goto out;
234 mp->mglist = false;
236 if (mp->ports)
237 goto out;
239 mdb = mlock_dereference(br->mdb, br);
241 hlist_del_rcu(&mp->hlist[mdb->ver]);
242 mdb->size--;
244 call_rcu_bh(&mp->rcu, br_multicast_free_group);
246 out:
247 spin_unlock(&br->multicast_lock);
250 static void br_multicast_del_pg(struct net_bridge *br,
251 struct net_bridge_port_group *pg)
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;
258 mdb = mlock_dereference(br->mdb, br);
260 mp = br_mdb_ip_get(mdb, &pg->addr);
261 if (WARN_ON(!mp))
262 return;
264 for (pp = &mp->ports;
265 (p = mlock_dereference(*pp, br)) != NULL;
266 pp = &p->next) {
267 if (p != pg)
268 continue;
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);
275 if (!mp->ports && !mp->mglist &&
276 netif_running(br->dev))
277 mod_timer(&mp->timer, jiffies);
279 return;
282 WARN_ON(1);
285 static void br_multicast_port_group_expired(unsigned long data)
287 struct net_bridge_port_group *pg = (void *)data;
288 struct net_bridge *br = pg->port->br;
290 spin_lock(&br->multicast_lock);
291 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
292 hlist_unhashed(&pg->mglist) || pg->state & MDB_PERMANENT)
293 goto out;
295 br_multicast_del_pg(br, pg);
297 out:
298 spin_unlock(&br->multicast_lock);
301 static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max,
302 int elasticity)
304 struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1);
305 struct net_bridge_mdb_htable *mdb;
306 int err;
308 mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
309 if (!mdb)
310 return -ENOMEM;
312 mdb->max = max;
313 mdb->old = old;
315 mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
316 if (!mdb->mhash) {
317 kfree(mdb);
318 return -ENOMEM;
321 mdb->size = old ? old->size : 0;
322 mdb->ver = old ? old->ver ^ 1 : 0;
324 if (!old || elasticity)
325 get_random_bytes(&mdb->secret, sizeof(mdb->secret));
326 else
327 mdb->secret = old->secret;
329 if (!old)
330 goto out;
332 err = br_mdb_copy(mdb, old, elasticity);
333 if (err) {
334 kfree(mdb->mhash);
335 kfree(mdb);
336 return err;
339 br_mdb_rehash_seq++;
340 call_rcu_bh(&mdb->rcu, br_mdb_free);
342 out:
343 rcu_assign_pointer(*mdbp, mdb);
345 return 0;
348 static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
349 __be32 group)
351 struct sk_buff *skb;
352 struct igmphdr *ih;
353 struct ethhdr *eth;
354 struct iphdr *iph;
356 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
357 sizeof(*ih) + 4);
358 if (!skb)
359 goto out;
361 skb->protocol = htons(ETH_P_IP);
363 skb_reset_mac_header(skb);
364 eth = eth_hdr(skb);
366 memcpy(eth->h_source, br->dev->dev_addr, 6);
367 eth->h_dest[0] = 1;
368 eth->h_dest[1] = 0;
369 eth->h_dest[2] = 0x5e;
370 eth->h_dest[3] = 0;
371 eth->h_dest[4] = 0;
372 eth->h_dest[5] = 1;
373 eth->h_proto = htons(ETH_P_IP);
374 skb_put(skb, sizeof(*eth));
376 skb_set_network_header(skb, skb->len);
377 iph = ip_hdr(skb);
379 iph->version = 4;
380 iph->ihl = 6;
381 iph->tos = 0xc0;
382 iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4);
383 iph->id = 0;
384 iph->frag_off = htons(IP_DF);
385 iph->ttl = 1;
386 iph->protocol = IPPROTO_IGMP;
387 iph->saddr = br->multicast_query_use_ifaddr ?
388 inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0;
389 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
390 ((u8 *)&iph[1])[0] = IPOPT_RA;
391 ((u8 *)&iph[1])[1] = 4;
392 ((u8 *)&iph[1])[2] = 0;
393 ((u8 *)&iph[1])[3] = 0;
394 ip_send_check(iph);
395 skb_put(skb, 24);
397 skb_set_transport_header(skb, skb->len);
398 ih = igmp_hdr(skb);
399 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
400 ih->code = (group ? br->multicast_last_member_interval :
401 br->multicast_query_response_interval) /
402 (HZ / IGMP_TIMER_SCALE);
403 ih->group = group;
404 ih->csum = 0;
405 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
406 skb_put(skb, sizeof(*ih));
408 __skb_pull(skb, sizeof(*eth));
410 out:
411 return skb;
414 #if IS_ENABLED(CONFIG_IPV6)
415 static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
416 const struct in6_addr *group)
418 struct sk_buff *skb;
419 struct ipv6hdr *ip6h;
420 struct mld_msg *mldq;
421 struct ethhdr *eth;
422 u8 *hopopt;
423 unsigned long interval;
425 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
426 8 + sizeof(*mldq));
427 if (!skb)
428 goto out;
430 skb->protocol = htons(ETH_P_IPV6);
432 /* Ethernet header */
433 skb_reset_mac_header(skb);
434 eth = eth_hdr(skb);
436 memcpy(eth->h_source, br->dev->dev_addr, 6);
437 eth->h_proto = htons(ETH_P_IPV6);
438 skb_put(skb, sizeof(*eth));
440 /* IPv6 header + HbH option */
441 skb_set_network_header(skb, skb->len);
442 ip6h = ipv6_hdr(skb);
444 *(__force __be32 *)ip6h = htonl(0x60000000);
445 ip6h->payload_len = htons(8 + sizeof(*mldq));
446 ip6h->nexthdr = IPPROTO_HOPOPTS;
447 ip6h->hop_limit = 1;
448 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
449 if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
450 &ip6h->saddr)) {
451 kfree_skb(skb);
452 br->has_ipv6_addr = 0;
453 return NULL;
456 br->has_ipv6_addr = 1;
457 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
459 hopopt = (u8 *)(ip6h + 1);
460 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
461 hopopt[1] = 0; /* length of HbH */
462 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
463 hopopt[3] = 2; /* Length of RA Option */
464 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
465 hopopt[5] = 0;
466 hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */
467 hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */
469 skb_put(skb, sizeof(*ip6h) + 8);
471 /* ICMPv6 */
472 skb_set_transport_header(skb, skb->len);
473 mldq = (struct mld_msg *) icmp6_hdr(skb);
475 interval = ipv6_addr_any(group) ?
476 br->multicast_query_response_interval :
477 br->multicast_last_member_interval;
479 mldq->mld_type = ICMPV6_MGM_QUERY;
480 mldq->mld_code = 0;
481 mldq->mld_cksum = 0;
482 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
483 mldq->mld_reserved = 0;
484 mldq->mld_mca = *group;
486 /* checksum */
487 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
488 sizeof(*mldq), IPPROTO_ICMPV6,
489 csum_partial(mldq,
490 sizeof(*mldq), 0));
491 skb_put(skb, sizeof(*mldq));
493 __skb_pull(skb, sizeof(*eth));
495 out:
496 return skb;
498 #endif
500 static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
501 struct br_ip *addr)
503 switch (addr->proto) {
504 case htons(ETH_P_IP):
505 return br_ip4_multicast_alloc_query(br, addr->u.ip4);
506 #if IS_ENABLED(CONFIG_IPV6)
507 case htons(ETH_P_IPV6):
508 return br_ip6_multicast_alloc_query(br, &addr->u.ip6);
509 #endif
511 return NULL;
514 static struct net_bridge_mdb_entry *br_multicast_get_group(
515 struct net_bridge *br, struct net_bridge_port *port,
516 struct br_ip *group, int hash)
518 struct net_bridge_mdb_htable *mdb;
519 struct net_bridge_mdb_entry *mp;
520 unsigned int count = 0;
521 unsigned int max;
522 int elasticity;
523 int err;
525 mdb = rcu_dereference_protected(br->mdb, 1);
526 hlist_for_each_entry(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
527 count++;
528 if (unlikely(br_ip_equal(group, &mp->addr)))
529 return mp;
532 elasticity = 0;
533 max = mdb->max;
535 if (unlikely(count > br->hash_elasticity && count)) {
536 if (net_ratelimit())
537 br_info(br, "Multicast hash table "
538 "chain limit reached: %s\n",
539 port ? port->dev->name : br->dev->name);
541 elasticity = br->hash_elasticity;
544 if (mdb->size >= max) {
545 max *= 2;
546 if (unlikely(max > br->hash_max)) {
547 br_warn(br, "Multicast hash table maximum of %d "
548 "reached, disabling snooping: %s\n",
549 br->hash_max,
550 port ? port->dev->name : br->dev->name);
551 err = -E2BIG;
552 disable:
553 br->multicast_disabled = 1;
554 goto err;
558 if (max > mdb->max || elasticity) {
559 if (mdb->old) {
560 if (net_ratelimit())
561 br_info(br, "Multicast hash table "
562 "on fire: %s\n",
563 port ? port->dev->name : br->dev->name);
564 err = -EEXIST;
565 goto err;
568 err = br_mdb_rehash(&br->mdb, max, elasticity);
569 if (err) {
570 br_warn(br, "Cannot rehash multicast "
571 "hash table, disabling snooping: %s, %d, %d\n",
572 port ? port->dev->name : br->dev->name,
573 mdb->size, err);
574 goto disable;
577 err = -EAGAIN;
578 goto err;
581 return NULL;
583 err:
584 mp = ERR_PTR(err);
585 return mp;
588 struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
589 struct net_bridge_port *port, struct br_ip *group)
591 struct net_bridge_mdb_htable *mdb;
592 struct net_bridge_mdb_entry *mp;
593 int hash;
594 int err;
596 mdb = rcu_dereference_protected(br->mdb, 1);
597 if (!mdb) {
598 err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0);
599 if (err)
600 return ERR_PTR(err);
601 goto rehash;
604 hash = br_ip_hash(mdb, group);
605 mp = br_multicast_get_group(br, port, group, hash);
606 switch (PTR_ERR(mp)) {
607 case 0:
608 break;
610 case -EAGAIN:
611 rehash:
612 mdb = rcu_dereference_protected(br->mdb, 1);
613 hash = br_ip_hash(mdb, group);
614 break;
616 default:
617 goto out;
620 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
621 if (unlikely(!mp))
622 return ERR_PTR(-ENOMEM);
624 mp->br = br;
625 mp->addr = *group;
626 setup_timer(&mp->timer, br_multicast_group_expired,
627 (unsigned long)mp);
629 hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
630 mdb->size++;
632 out:
633 return mp;
636 struct net_bridge_port_group *br_multicast_new_port_group(
637 struct net_bridge_port *port,
638 struct br_ip *group,
639 struct net_bridge_port_group __rcu *next,
640 unsigned char state)
642 struct net_bridge_port_group *p;
644 p = kzalloc(sizeof(*p), GFP_ATOMIC);
645 if (unlikely(!p))
646 return NULL;
648 p->addr = *group;
649 p->port = port;
650 p->state = state;
651 rcu_assign_pointer(p->next, next);
652 hlist_add_head(&p->mglist, &port->mglist);
653 setup_timer(&p->timer, br_multicast_port_group_expired,
654 (unsigned long)p);
655 return p;
658 static int br_multicast_add_group(struct net_bridge *br,
659 struct net_bridge_port *port,
660 struct br_ip *group)
662 struct net_bridge_mdb_entry *mp;
663 struct net_bridge_port_group *p;
664 struct net_bridge_port_group __rcu **pp;
665 unsigned long now = jiffies;
666 int err;
668 spin_lock(&br->multicast_lock);
669 if (!netif_running(br->dev) ||
670 (port && port->state == BR_STATE_DISABLED))
671 goto out;
673 mp = br_multicast_new_group(br, port, group);
674 err = PTR_ERR(mp);
675 if (IS_ERR(mp))
676 goto err;
678 if (!port) {
679 mp->mglist = true;
680 mod_timer(&mp->timer, now + br->multicast_membership_interval);
681 goto out;
684 for (pp = &mp->ports;
685 (p = mlock_dereference(*pp, br)) != NULL;
686 pp = &p->next) {
687 if (p->port == port)
688 goto found;
689 if ((unsigned long)p->port < (unsigned long)port)
690 break;
693 p = br_multicast_new_port_group(port, group, *pp, MDB_TEMPORARY);
694 if (unlikely(!p))
695 goto err;
696 rcu_assign_pointer(*pp, p);
697 br_mdb_notify(br->dev, port, group, RTM_NEWMDB);
699 found:
700 mod_timer(&p->timer, now + br->multicast_membership_interval);
701 out:
702 err = 0;
704 err:
705 spin_unlock(&br->multicast_lock);
706 return err;
709 static int br_ip4_multicast_add_group(struct net_bridge *br,
710 struct net_bridge_port *port,
711 __be32 group,
712 __u16 vid)
714 struct br_ip br_group;
716 if (ipv4_is_local_multicast(group))
717 return 0;
719 br_group.u.ip4 = group;
720 br_group.proto = htons(ETH_P_IP);
721 br_group.vid = vid;
723 return br_multicast_add_group(br, port, &br_group);
726 #if IS_ENABLED(CONFIG_IPV6)
727 static int br_ip6_multicast_add_group(struct net_bridge *br,
728 struct net_bridge_port *port,
729 const struct in6_addr *group,
730 __u16 vid)
732 struct br_ip br_group;
734 if (ipv6_addr_is_ll_all_nodes(group))
735 return 0;
737 br_group.u.ip6 = *group;
738 br_group.proto = htons(ETH_P_IPV6);
739 br_group.vid = vid;
741 return br_multicast_add_group(br, port, &br_group);
743 #endif
745 static void br_multicast_router_expired(unsigned long data)
747 struct net_bridge_port *port = (void *)data;
748 struct net_bridge *br = port->br;
750 spin_lock(&br->multicast_lock);
751 if (port->multicast_router != 1 ||
752 timer_pending(&port->multicast_router_timer) ||
753 hlist_unhashed(&port->rlist))
754 goto out;
756 hlist_del_init_rcu(&port->rlist);
758 out:
759 spin_unlock(&br->multicast_lock);
762 static void br_multicast_local_router_expired(unsigned long data)
766 static void br_multicast_querier_expired(struct net_bridge *br,
767 struct bridge_mcast_query *query)
769 spin_lock(&br->multicast_lock);
770 if (!netif_running(br->dev) || br->multicast_disabled)
771 goto out;
773 br_multicast_start_querier(br, query);
775 out:
776 spin_unlock(&br->multicast_lock);
779 static void br_ip4_multicast_querier_expired(unsigned long data)
781 struct net_bridge *br = (void *)data;
783 br_multicast_querier_expired(br, &br->ip4_query);
786 #if IS_ENABLED(CONFIG_IPV6)
787 static void br_ip6_multicast_querier_expired(unsigned long data)
789 struct net_bridge *br = (void *)data;
791 br_multicast_querier_expired(br, &br->ip6_query);
793 #endif
795 static void __br_multicast_send_query(struct net_bridge *br,
796 struct net_bridge_port *port,
797 struct br_ip *ip)
799 struct sk_buff *skb;
801 skb = br_multicast_alloc_query(br, ip);
802 if (!skb)
803 return;
805 if (port) {
806 __skb_push(skb, sizeof(struct ethhdr));
807 skb->dev = port->dev;
808 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
809 dev_queue_xmit);
810 } else
811 netif_rx(skb);
814 static void br_multicast_send_query(struct net_bridge *br,
815 struct net_bridge_port *port,
816 struct bridge_mcast_query *query)
818 unsigned long time;
819 struct br_ip br_group;
820 struct bridge_mcast_querier *querier = NULL;
822 if (!netif_running(br->dev) || br->multicast_disabled ||
823 !br->multicast_querier)
824 return;
826 memset(&br_group.u, 0, sizeof(br_group.u));
828 if (port ? (query == &port->ip4_query) :
829 (query == &br->ip4_query)) {
830 querier = &br->ip4_querier;
831 br_group.proto = htons(ETH_P_IP);
832 #if IS_ENABLED(CONFIG_IPV6)
833 } else {
834 querier = &br->ip6_querier;
835 br_group.proto = htons(ETH_P_IPV6);
836 #endif
839 if (!querier || timer_pending(&querier->timer))
840 return;
842 __br_multicast_send_query(br, port, &br_group);
844 time = jiffies;
845 time += query->startup_sent < br->multicast_startup_query_count ?
846 br->multicast_startup_query_interval :
847 br->multicast_query_interval;
848 mod_timer(&query->timer, time);
851 static void br_multicast_port_query_expired(struct net_bridge_port *port,
852 struct bridge_mcast_query *query)
854 struct net_bridge *br = port->br;
856 spin_lock(&br->multicast_lock);
857 if (port->state == BR_STATE_DISABLED ||
858 port->state == BR_STATE_BLOCKING)
859 goto out;
861 if (query->startup_sent < br->multicast_startup_query_count)
862 query->startup_sent++;
864 br_multicast_send_query(port->br, port, query);
866 out:
867 spin_unlock(&br->multicast_lock);
870 static void br_ip4_multicast_port_query_expired(unsigned long data)
872 struct net_bridge_port *port = (void *)data;
874 br_multicast_port_query_expired(port, &port->ip4_query);
877 #if IS_ENABLED(CONFIG_IPV6)
878 static void br_ip6_multicast_port_query_expired(unsigned long data)
880 struct net_bridge_port *port = (void *)data;
882 br_multicast_port_query_expired(port, &port->ip6_query);
884 #endif
886 void br_multicast_add_port(struct net_bridge_port *port)
888 port->multicast_router = 1;
890 setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
891 (unsigned long)port);
892 setup_timer(&port->ip4_query.timer, br_ip4_multicast_port_query_expired,
893 (unsigned long)port);
894 #if IS_ENABLED(CONFIG_IPV6)
895 setup_timer(&port->ip6_query.timer, br_ip6_multicast_port_query_expired,
896 (unsigned long)port);
897 #endif
900 void br_multicast_del_port(struct net_bridge_port *port)
902 del_timer_sync(&port->multicast_router_timer);
905 static void br_multicast_enable(struct bridge_mcast_query *query)
907 query->startup_sent = 0;
909 if (try_to_del_timer_sync(&query->timer) >= 0 ||
910 del_timer(&query->timer))
911 mod_timer(&query->timer, jiffies);
914 void br_multicast_enable_port(struct net_bridge_port *port)
916 struct net_bridge *br = port->br;
918 spin_lock(&br->multicast_lock);
919 if (br->multicast_disabled || !netif_running(br->dev))
920 goto out;
922 br_multicast_enable(&port->ip4_query);
923 #if IS_ENABLED(CONFIG_IPV6)
924 br_multicast_enable(&port->ip6_query);
925 #endif
927 out:
928 spin_unlock(&br->multicast_lock);
931 void br_multicast_disable_port(struct net_bridge_port *port)
933 struct net_bridge *br = port->br;
934 struct net_bridge_port_group *pg;
935 struct hlist_node *n;
937 spin_lock(&br->multicast_lock);
938 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
939 br_multicast_del_pg(br, pg);
941 if (!hlist_unhashed(&port->rlist))
942 hlist_del_init_rcu(&port->rlist);
943 del_timer(&port->multicast_router_timer);
944 del_timer(&port->ip4_query.timer);
945 #if IS_ENABLED(CONFIG_IPV6)
946 del_timer(&port->ip6_query.timer);
947 #endif
948 spin_unlock(&br->multicast_lock);
951 static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
952 struct net_bridge_port *port,
953 struct sk_buff *skb)
955 struct igmpv3_report *ih;
956 struct igmpv3_grec *grec;
957 int i;
958 int len;
959 int num;
960 int type;
961 int err = 0;
962 __be32 group;
963 u16 vid = 0;
965 if (!pskb_may_pull(skb, sizeof(*ih)))
966 return -EINVAL;
968 br_vlan_get_tag(skb, &vid);
969 ih = igmpv3_report_hdr(skb);
970 num = ntohs(ih->ngrec);
971 len = sizeof(*ih);
973 for (i = 0; i < num; i++) {
974 len += sizeof(*grec);
975 if (!pskb_may_pull(skb, len))
976 return -EINVAL;
978 grec = (void *)(skb->data + len - sizeof(*grec));
979 group = grec->grec_mca;
980 type = grec->grec_type;
982 len += ntohs(grec->grec_nsrcs) * 4;
983 if (!pskb_may_pull(skb, len))
984 return -EINVAL;
986 /* We treat this as an IGMPv2 report for now. */
987 switch (type) {
988 case IGMPV3_MODE_IS_INCLUDE:
989 case IGMPV3_MODE_IS_EXCLUDE:
990 case IGMPV3_CHANGE_TO_INCLUDE:
991 case IGMPV3_CHANGE_TO_EXCLUDE:
992 case IGMPV3_ALLOW_NEW_SOURCES:
993 case IGMPV3_BLOCK_OLD_SOURCES:
994 break;
996 default:
997 continue;
1000 err = br_ip4_multicast_add_group(br, port, group, vid);
1001 if (err)
1002 break;
1005 return err;
1008 #if IS_ENABLED(CONFIG_IPV6)
1009 static int br_ip6_multicast_mld2_report(struct net_bridge *br,
1010 struct net_bridge_port *port,
1011 struct sk_buff *skb)
1013 struct icmp6hdr *icmp6h;
1014 struct mld2_grec *grec;
1015 int i;
1016 int len;
1017 int num;
1018 int err = 0;
1019 u16 vid = 0;
1021 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
1022 return -EINVAL;
1024 br_vlan_get_tag(skb, &vid);
1025 icmp6h = icmp6_hdr(skb);
1026 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
1027 len = sizeof(*icmp6h);
1029 for (i = 0; i < num; i++) {
1030 __be16 *nsrcs, _nsrcs;
1032 nsrcs = skb_header_pointer(skb,
1033 len + offsetof(struct mld2_grec,
1034 grec_nsrcs),
1035 sizeof(_nsrcs), &_nsrcs);
1036 if (!nsrcs)
1037 return -EINVAL;
1039 if (!pskb_may_pull(skb,
1040 len + sizeof(*grec) +
1041 sizeof(struct in6_addr) * ntohs(*nsrcs)))
1042 return -EINVAL;
1044 grec = (struct mld2_grec *)(skb->data + len);
1045 len += sizeof(*grec) +
1046 sizeof(struct in6_addr) * ntohs(*nsrcs);
1048 /* We treat these as MLDv1 reports for now. */
1049 switch (grec->grec_type) {
1050 case MLD2_MODE_IS_INCLUDE:
1051 case MLD2_MODE_IS_EXCLUDE:
1052 case MLD2_CHANGE_TO_INCLUDE:
1053 case MLD2_CHANGE_TO_EXCLUDE:
1054 case MLD2_ALLOW_NEW_SOURCES:
1055 case MLD2_BLOCK_OLD_SOURCES:
1056 break;
1058 default:
1059 continue;
1062 err = br_ip6_multicast_add_group(br, port, &grec->grec_mca,
1063 vid);
1064 if (err)
1065 break;
1068 return err;
1070 #endif
1072 static void
1073 br_multicast_update_querier_timer(struct net_bridge *br,
1074 struct bridge_mcast_querier *querier,
1075 unsigned long max_delay)
1077 if (!timer_pending(&querier->timer))
1078 querier->delay_time = jiffies + max_delay;
1080 mod_timer(&querier->timer, jiffies + br->multicast_querier_interval);
1084 * Add port to router_list
1085 * list is maintained ordered by pointer value
1086 * and locked by br->multicast_lock and RCU
1088 static void br_multicast_add_router(struct net_bridge *br,
1089 struct net_bridge_port *port)
1091 struct net_bridge_port *p;
1092 struct hlist_node *slot = NULL;
1094 if (!hlist_unhashed(&port->rlist))
1095 return;
1097 hlist_for_each_entry(p, &br->router_list, rlist) {
1098 if ((unsigned long) port >= (unsigned long) p)
1099 break;
1100 slot = &p->rlist;
1103 if (slot)
1104 hlist_add_after_rcu(slot, &port->rlist);
1105 else
1106 hlist_add_head_rcu(&port->rlist, &br->router_list);
1109 static void br_multicast_mark_router(struct net_bridge *br,
1110 struct net_bridge_port *port)
1112 unsigned long now = jiffies;
1114 if (!port) {
1115 if (br->multicast_router == 1)
1116 mod_timer(&br->multicast_router_timer,
1117 now + br->multicast_querier_interval);
1118 return;
1121 if (port->multicast_router != 1)
1122 return;
1124 br_multicast_add_router(br, port);
1126 mod_timer(&port->multicast_router_timer,
1127 now + br->multicast_querier_interval);
1130 static void br_multicast_query_received(struct net_bridge *br,
1131 struct net_bridge_port *port,
1132 struct bridge_mcast_querier *querier,
1133 int saddr,
1134 bool is_general_query,
1135 unsigned long max_delay)
1137 if (saddr && is_general_query)
1138 br_multicast_update_querier_timer(br, querier, max_delay);
1139 else if (timer_pending(&querier->timer))
1140 return;
1142 br_multicast_mark_router(br, port);
1145 static int br_ip4_multicast_query(struct net_bridge *br,
1146 struct net_bridge_port *port,
1147 struct sk_buff *skb)
1149 const struct iphdr *iph = ip_hdr(skb);
1150 struct igmphdr *ih = igmp_hdr(skb);
1151 struct net_bridge_mdb_entry *mp;
1152 struct igmpv3_query *ih3;
1153 struct net_bridge_port_group *p;
1154 struct net_bridge_port_group __rcu **pp;
1155 unsigned long max_delay;
1156 unsigned long now = jiffies;
1157 __be32 group;
1158 int err = 0;
1159 u16 vid = 0;
1161 spin_lock(&br->multicast_lock);
1162 if (!netif_running(br->dev) ||
1163 (port && port->state == BR_STATE_DISABLED))
1164 goto out;
1166 group = ih->group;
1168 if (skb->len == sizeof(*ih)) {
1169 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1171 if (!max_delay) {
1172 max_delay = 10 * HZ;
1173 group = 0;
1175 } else {
1176 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) {
1177 err = -EINVAL;
1178 goto out;
1181 ih3 = igmpv3_query_hdr(skb);
1182 if (ih3->nsrcs)
1183 goto out;
1185 max_delay = ih3->code ?
1186 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
1189 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1190 * all-systems destination addresses (224.0.0.1) for general queries
1192 if (!group && iph->daddr != htonl(INADDR_ALLHOSTS_GROUP)) {
1193 err = -EINVAL;
1194 goto out;
1197 br_multicast_query_received(br, port, &br->ip4_querier, !!iph->saddr,
1198 !group, max_delay);
1200 if (!group)
1201 goto out;
1203 br_vlan_get_tag(skb, &vid);
1204 mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid);
1205 if (!mp)
1206 goto out;
1208 max_delay *= br->multicast_last_member_count;
1210 if (mp->mglist &&
1211 (timer_pending(&mp->timer) ?
1212 time_after(mp->timer.expires, now + max_delay) :
1213 try_to_del_timer_sync(&mp->timer) >= 0))
1214 mod_timer(&mp->timer, now + max_delay);
1216 for (pp = &mp->ports;
1217 (p = mlock_dereference(*pp, br)) != NULL;
1218 pp = &p->next) {
1219 if (timer_pending(&p->timer) ?
1220 time_after(p->timer.expires, now + max_delay) :
1221 try_to_del_timer_sync(&p->timer) >= 0)
1222 mod_timer(&p->timer, now + max_delay);
1225 out:
1226 spin_unlock(&br->multicast_lock);
1227 return err;
1230 #if IS_ENABLED(CONFIG_IPV6)
1231 static int br_ip6_multicast_query(struct net_bridge *br,
1232 struct net_bridge_port *port,
1233 struct sk_buff *skb)
1235 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
1236 struct mld_msg *mld;
1237 struct net_bridge_mdb_entry *mp;
1238 struct mld2_query *mld2q;
1239 struct net_bridge_port_group *p;
1240 struct net_bridge_port_group __rcu **pp;
1241 unsigned long max_delay;
1242 unsigned long now = jiffies;
1243 const struct in6_addr *group = NULL;
1244 bool is_general_query;
1245 int err = 0;
1246 u16 vid = 0;
1248 spin_lock(&br->multicast_lock);
1249 if (!netif_running(br->dev) ||
1250 (port && port->state == BR_STATE_DISABLED))
1251 goto out;
1253 /* RFC2710+RFC3810 (MLDv1+MLDv2) require link-local source addresses */
1254 if (!(ipv6_addr_type(&ip6h->saddr) & IPV6_ADDR_LINKLOCAL)) {
1255 err = -EINVAL;
1256 goto out;
1259 if (skb->len == sizeof(*mld)) {
1260 if (!pskb_may_pull(skb, sizeof(*mld))) {
1261 err = -EINVAL;
1262 goto out;
1264 mld = (struct mld_msg *) icmp6_hdr(skb);
1265 max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
1266 if (max_delay)
1267 group = &mld->mld_mca;
1268 } else {
1269 if (!pskb_may_pull(skb, sizeof(*mld2q))) {
1270 err = -EINVAL;
1271 goto out;
1273 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1274 if (!mld2q->mld2q_nsrcs)
1275 group = &mld2q->mld2q_mca;
1277 max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
1280 is_general_query = group && ipv6_addr_any(group);
1282 /* RFC2710+RFC3810 (MLDv1+MLDv2) require the multicast link layer
1283 * all-nodes destination address (ff02::1) for general queries
1285 if (is_general_query && !ipv6_addr_is_ll_all_nodes(&ip6h->daddr)) {
1286 err = -EINVAL;
1287 goto out;
1290 br_multicast_query_received(br, port, &br->ip6_querier,
1291 !ipv6_addr_any(&ip6h->saddr),
1292 is_general_query, max_delay);
1294 if (!group)
1295 goto out;
1297 br_vlan_get_tag(skb, &vid);
1298 mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid);
1299 if (!mp)
1300 goto out;
1302 max_delay *= br->multicast_last_member_count;
1303 if (mp->mglist &&
1304 (timer_pending(&mp->timer) ?
1305 time_after(mp->timer.expires, now + max_delay) :
1306 try_to_del_timer_sync(&mp->timer) >= 0))
1307 mod_timer(&mp->timer, now + max_delay);
1309 for (pp = &mp->ports;
1310 (p = mlock_dereference(*pp, br)) != NULL;
1311 pp = &p->next) {
1312 if (timer_pending(&p->timer) ?
1313 time_after(p->timer.expires, now + max_delay) :
1314 try_to_del_timer_sync(&p->timer) >= 0)
1315 mod_timer(&p->timer, now + max_delay);
1318 out:
1319 spin_unlock(&br->multicast_lock);
1320 return err;
1322 #endif
1324 static void br_multicast_leave_group(struct net_bridge *br,
1325 struct net_bridge_port *port,
1326 struct br_ip *group,
1327 struct bridge_mcast_querier *querier,
1328 struct bridge_mcast_query *query)
1330 struct net_bridge_mdb_htable *mdb;
1331 struct net_bridge_mdb_entry *mp;
1332 struct net_bridge_port_group *p;
1333 unsigned long now;
1334 unsigned long time;
1336 spin_lock(&br->multicast_lock);
1337 if (!netif_running(br->dev) ||
1338 (port && port->state == BR_STATE_DISABLED) ||
1339 timer_pending(&querier->timer))
1340 goto out;
1342 mdb = mlock_dereference(br->mdb, br);
1343 mp = br_mdb_ip_get(mdb, group);
1344 if (!mp)
1345 goto out;
1347 if (br->multicast_querier) {
1348 __br_multicast_send_query(br, port, &mp->addr);
1350 time = jiffies + br->multicast_last_member_count *
1351 br->multicast_last_member_interval;
1353 mod_timer(&query->timer, time);
1355 for (p = mlock_dereference(mp->ports, br);
1356 p != NULL;
1357 p = mlock_dereference(p->next, br)) {
1358 if (p->port != port)
1359 continue;
1361 if (!hlist_unhashed(&p->mglist) &&
1362 (timer_pending(&p->timer) ?
1363 time_after(p->timer.expires, time) :
1364 try_to_del_timer_sync(&p->timer) >= 0)) {
1365 mod_timer(&p->timer, time);
1368 break;
1372 if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
1373 struct net_bridge_port_group __rcu **pp;
1375 for (pp = &mp->ports;
1376 (p = mlock_dereference(*pp, br)) != NULL;
1377 pp = &p->next) {
1378 if (p->port != port)
1379 continue;
1381 rcu_assign_pointer(*pp, p->next);
1382 hlist_del_init(&p->mglist);
1383 del_timer(&p->timer);
1384 call_rcu_bh(&p->rcu, br_multicast_free_pg);
1385 br_mdb_notify(br->dev, port, group, RTM_DELMDB);
1387 if (!mp->ports && !mp->mglist &&
1388 netif_running(br->dev))
1389 mod_timer(&mp->timer, jiffies);
1391 goto out;
1394 now = jiffies;
1395 time = now + br->multicast_last_member_count *
1396 br->multicast_last_member_interval;
1398 if (!port) {
1399 if (mp->mglist &&
1400 (timer_pending(&mp->timer) ?
1401 time_after(mp->timer.expires, time) :
1402 try_to_del_timer_sync(&mp->timer) >= 0)) {
1403 mod_timer(&mp->timer, time);
1406 goto out;
1409 for (p = mlock_dereference(mp->ports, br);
1410 p != NULL;
1411 p = mlock_dereference(p->next, br)) {
1412 if (p->port != port)
1413 continue;
1415 if (!hlist_unhashed(&p->mglist) &&
1416 (timer_pending(&p->timer) ?
1417 time_after(p->timer.expires, time) :
1418 try_to_del_timer_sync(&p->timer) >= 0)) {
1419 mod_timer(&p->timer, time);
1422 break;
1424 out:
1425 spin_unlock(&br->multicast_lock);
1428 static void br_ip4_multicast_leave_group(struct net_bridge *br,
1429 struct net_bridge_port *port,
1430 __be32 group,
1431 __u16 vid)
1433 struct br_ip br_group;
1434 struct bridge_mcast_query *query = port ? &port->ip4_query :
1435 &br->ip4_query;
1437 if (ipv4_is_local_multicast(group))
1438 return;
1440 br_group.u.ip4 = group;
1441 br_group.proto = htons(ETH_P_IP);
1442 br_group.vid = vid;
1444 br_multicast_leave_group(br, port, &br_group, &br->ip4_querier, query);
1447 #if IS_ENABLED(CONFIG_IPV6)
1448 static void br_ip6_multicast_leave_group(struct net_bridge *br,
1449 struct net_bridge_port *port,
1450 const struct in6_addr *group,
1451 __u16 vid)
1453 struct br_ip br_group;
1454 struct bridge_mcast_query *query = port ? &port->ip6_query :
1455 &br->ip6_query;
1458 if (ipv6_addr_is_ll_all_nodes(group))
1459 return;
1461 br_group.u.ip6 = *group;
1462 br_group.proto = htons(ETH_P_IPV6);
1463 br_group.vid = vid;
1465 br_multicast_leave_group(br, port, &br_group, &br->ip6_querier, query);
1467 #endif
1469 static int br_multicast_ipv4_rcv(struct net_bridge *br,
1470 struct net_bridge_port *port,
1471 struct sk_buff *skb)
1473 struct sk_buff *skb2 = skb;
1474 const struct iphdr *iph;
1475 struct igmphdr *ih;
1476 unsigned int len;
1477 unsigned int offset;
1478 int err;
1479 u16 vid = 0;
1481 /* We treat OOM as packet loss for now. */
1482 if (!pskb_may_pull(skb, sizeof(*iph)))
1483 return -EINVAL;
1485 iph = ip_hdr(skb);
1487 if (iph->ihl < 5 || iph->version != 4)
1488 return -EINVAL;
1490 if (!pskb_may_pull(skb, ip_hdrlen(skb)))
1491 return -EINVAL;
1493 iph = ip_hdr(skb);
1495 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1496 return -EINVAL;
1498 if (iph->protocol != IPPROTO_IGMP) {
1499 if (!ipv4_is_local_multicast(iph->daddr))
1500 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1501 return 0;
1504 len = ntohs(iph->tot_len);
1505 if (skb->len < len || len < ip_hdrlen(skb))
1506 return -EINVAL;
1508 if (skb->len > len) {
1509 skb2 = skb_clone(skb, GFP_ATOMIC);
1510 if (!skb2)
1511 return -ENOMEM;
1513 err = pskb_trim_rcsum(skb2, len);
1514 if (err)
1515 goto err_out;
1518 len -= ip_hdrlen(skb2);
1519 offset = skb_network_offset(skb2) + ip_hdrlen(skb2);
1520 __skb_pull(skb2, offset);
1521 skb_reset_transport_header(skb2);
1523 err = -EINVAL;
1524 if (!pskb_may_pull(skb2, sizeof(*ih)))
1525 goto out;
1527 switch (skb2->ip_summed) {
1528 case CHECKSUM_COMPLETE:
1529 if (!csum_fold(skb2->csum))
1530 break;
1531 /* fall through */
1532 case CHECKSUM_NONE:
1533 skb2->csum = 0;
1534 if (skb_checksum_complete(skb2))
1535 goto out;
1538 err = 0;
1540 br_vlan_get_tag(skb2, &vid);
1541 BR_INPUT_SKB_CB(skb)->igmp = 1;
1542 ih = igmp_hdr(skb2);
1544 switch (ih->type) {
1545 case IGMP_HOST_MEMBERSHIP_REPORT:
1546 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1547 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1548 err = br_ip4_multicast_add_group(br, port, ih->group, vid);
1549 break;
1550 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1551 err = br_ip4_multicast_igmp3_report(br, port, skb2);
1552 break;
1553 case IGMP_HOST_MEMBERSHIP_QUERY:
1554 err = br_ip4_multicast_query(br, port, skb2);
1555 break;
1556 case IGMP_HOST_LEAVE_MESSAGE:
1557 br_ip4_multicast_leave_group(br, port, ih->group, vid);
1558 break;
1561 out:
1562 __skb_push(skb2, offset);
1563 err_out:
1564 if (skb2 != skb)
1565 kfree_skb(skb2);
1566 return err;
1569 #if IS_ENABLED(CONFIG_IPV6)
1570 static int br_multicast_ipv6_rcv(struct net_bridge *br,
1571 struct net_bridge_port *port,
1572 struct sk_buff *skb)
1574 struct sk_buff *skb2;
1575 const struct ipv6hdr *ip6h;
1576 u8 icmp6_type;
1577 u8 nexthdr;
1578 __be16 frag_off;
1579 unsigned int len;
1580 int offset;
1581 int err;
1582 u16 vid = 0;
1584 if (!pskb_may_pull(skb, sizeof(*ip6h)))
1585 return -EINVAL;
1587 ip6h = ipv6_hdr(skb);
1590 * We're interested in MLD messages only.
1591 * - Version is 6
1592 * - MLD has always Router Alert hop-by-hop option
1593 * - But we do not support jumbrograms.
1595 if (ip6h->version != 6)
1596 return 0;
1598 /* Prevent flooding this packet if there is no listener present */
1599 if (!ipv6_addr_is_ll_all_nodes(&ip6h->daddr))
1600 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1602 if (ip6h->nexthdr != IPPROTO_HOPOPTS ||
1603 ip6h->payload_len == 0)
1604 return 0;
1606 len = ntohs(ip6h->payload_len) + sizeof(*ip6h);
1607 if (skb->len < len)
1608 return -EINVAL;
1610 nexthdr = ip6h->nexthdr;
1611 offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr, &frag_off);
1613 if (offset < 0 || nexthdr != IPPROTO_ICMPV6)
1614 return 0;
1616 /* Okay, we found ICMPv6 header */
1617 skb2 = skb_clone(skb, GFP_ATOMIC);
1618 if (!skb2)
1619 return -ENOMEM;
1621 err = -EINVAL;
1622 if (!pskb_may_pull(skb2, offset + sizeof(struct icmp6hdr)))
1623 goto out;
1625 len -= offset - skb_network_offset(skb2);
1627 __skb_pull(skb2, offset);
1628 skb_reset_transport_header(skb2);
1629 skb_postpull_rcsum(skb2, skb_network_header(skb2),
1630 skb_network_header_len(skb2));
1632 icmp6_type = icmp6_hdr(skb2)->icmp6_type;
1634 switch (icmp6_type) {
1635 case ICMPV6_MGM_QUERY:
1636 case ICMPV6_MGM_REPORT:
1637 case ICMPV6_MGM_REDUCTION:
1638 case ICMPV6_MLD2_REPORT:
1639 break;
1640 default:
1641 err = 0;
1642 goto out;
1645 /* Okay, we found MLD message. Check further. */
1646 if (skb2->len > len) {
1647 err = pskb_trim_rcsum(skb2, len);
1648 if (err)
1649 goto out;
1650 err = -EINVAL;
1653 ip6h = ipv6_hdr(skb2);
1655 switch (skb2->ip_summed) {
1656 case CHECKSUM_COMPLETE:
1657 if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, skb2->len,
1658 IPPROTO_ICMPV6, skb2->csum))
1659 break;
1660 /*FALLTHROUGH*/
1661 case CHECKSUM_NONE:
1662 skb2->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr,
1663 &ip6h->daddr,
1664 skb2->len,
1665 IPPROTO_ICMPV6, 0));
1666 if (__skb_checksum_complete(skb2))
1667 goto out;
1670 err = 0;
1672 br_vlan_get_tag(skb, &vid);
1673 BR_INPUT_SKB_CB(skb)->igmp = 1;
1675 switch (icmp6_type) {
1676 case ICMPV6_MGM_REPORT:
1678 struct mld_msg *mld;
1679 if (!pskb_may_pull(skb2, sizeof(*mld))) {
1680 err = -EINVAL;
1681 goto out;
1683 mld = (struct mld_msg *)skb_transport_header(skb2);
1684 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1685 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid);
1686 break;
1688 case ICMPV6_MLD2_REPORT:
1689 err = br_ip6_multicast_mld2_report(br, port, skb2);
1690 break;
1691 case ICMPV6_MGM_QUERY:
1692 err = br_ip6_multicast_query(br, port, skb2);
1693 break;
1694 case ICMPV6_MGM_REDUCTION:
1696 struct mld_msg *mld;
1697 if (!pskb_may_pull(skb2, sizeof(*mld))) {
1698 err = -EINVAL;
1699 goto out;
1701 mld = (struct mld_msg *)skb_transport_header(skb2);
1702 br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid);
1706 out:
1707 kfree_skb(skb2);
1708 return err;
1710 #endif
1712 int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1713 struct sk_buff *skb)
1715 BR_INPUT_SKB_CB(skb)->igmp = 0;
1716 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1718 if (br->multicast_disabled)
1719 return 0;
1721 switch (skb->protocol) {
1722 case htons(ETH_P_IP):
1723 return br_multicast_ipv4_rcv(br, port, skb);
1724 #if IS_ENABLED(CONFIG_IPV6)
1725 case htons(ETH_P_IPV6):
1726 return br_multicast_ipv6_rcv(br, port, skb);
1727 #endif
1730 return 0;
1733 static void br_multicast_query_expired(struct net_bridge *br,
1734 struct bridge_mcast_query *query)
1736 spin_lock(&br->multicast_lock);
1737 if (query->startup_sent < br->multicast_startup_query_count)
1738 query->startup_sent++;
1740 br_multicast_send_query(br, NULL, query);
1741 spin_unlock(&br->multicast_lock);
1744 static void br_ip4_multicast_query_expired(unsigned long data)
1746 struct net_bridge *br = (void *)data;
1748 br_multicast_query_expired(br, &br->ip4_query);
1751 #if IS_ENABLED(CONFIG_IPV6)
1752 static void br_ip6_multicast_query_expired(unsigned long data)
1754 struct net_bridge *br = (void *)data;
1756 br_multicast_query_expired(br, &br->ip6_query);
1758 #endif
1760 void br_multicast_init(struct net_bridge *br)
1762 br->hash_elasticity = 4;
1763 br->hash_max = 512;
1765 br->multicast_router = 1;
1766 br->multicast_querier = 0;
1767 br->multicast_query_use_ifaddr = 0;
1768 br->multicast_last_member_count = 2;
1769 br->multicast_startup_query_count = 2;
1771 br->multicast_last_member_interval = HZ;
1772 br->multicast_query_response_interval = 10 * HZ;
1773 br->multicast_startup_query_interval = 125 * HZ / 4;
1774 br->multicast_query_interval = 125 * HZ;
1775 br->multicast_querier_interval = 255 * HZ;
1776 br->multicast_membership_interval = 260 * HZ;
1778 br->ip4_querier.delay_time = 0;
1779 #if IS_ENABLED(CONFIG_IPV6)
1780 br->ip6_querier.delay_time = 0;
1781 #endif
1782 br->has_ipv6_addr = 1;
1784 spin_lock_init(&br->multicast_lock);
1785 setup_timer(&br->multicast_router_timer,
1786 br_multicast_local_router_expired, 0);
1787 setup_timer(&br->ip4_querier.timer, br_ip4_multicast_querier_expired,
1788 (unsigned long)br);
1789 setup_timer(&br->ip4_query.timer, br_ip4_multicast_query_expired,
1790 (unsigned long)br);
1791 #if IS_ENABLED(CONFIG_IPV6)
1792 setup_timer(&br->ip6_querier.timer, br_ip6_multicast_querier_expired,
1793 (unsigned long)br);
1794 setup_timer(&br->ip6_query.timer, br_ip6_multicast_query_expired,
1795 (unsigned long)br);
1796 #endif
1799 static void __br_multicast_open(struct net_bridge *br,
1800 struct bridge_mcast_query *query)
1802 query->startup_sent = 0;
1804 if (br->multicast_disabled)
1805 return;
1807 mod_timer(&query->timer, jiffies);
1810 void br_multicast_open(struct net_bridge *br)
1812 __br_multicast_open(br, &br->ip4_query);
1813 #if IS_ENABLED(CONFIG_IPV6)
1814 __br_multicast_open(br, &br->ip6_query);
1815 #endif
1818 void br_multicast_stop(struct net_bridge *br)
1820 struct net_bridge_mdb_htable *mdb;
1821 struct net_bridge_mdb_entry *mp;
1822 struct hlist_node *n;
1823 u32 ver;
1824 int i;
1826 del_timer_sync(&br->multicast_router_timer);
1827 del_timer_sync(&br->ip4_querier.timer);
1828 del_timer_sync(&br->ip4_query.timer);
1829 #if IS_ENABLED(CONFIG_IPV6)
1830 del_timer_sync(&br->ip6_querier.timer);
1831 del_timer_sync(&br->ip6_query.timer);
1832 #endif
1834 spin_lock_bh(&br->multicast_lock);
1835 mdb = mlock_dereference(br->mdb, br);
1836 if (!mdb)
1837 goto out;
1839 br->mdb = NULL;
1841 ver = mdb->ver;
1842 for (i = 0; i < mdb->max; i++) {
1843 hlist_for_each_entry_safe(mp, n, &mdb->mhash[i],
1844 hlist[ver]) {
1845 del_timer(&mp->timer);
1846 call_rcu_bh(&mp->rcu, br_multicast_free_group);
1850 if (mdb->old) {
1851 spin_unlock_bh(&br->multicast_lock);
1852 rcu_barrier_bh();
1853 spin_lock_bh(&br->multicast_lock);
1854 WARN_ON(mdb->old);
1857 mdb->old = mdb;
1858 call_rcu_bh(&mdb->rcu, br_mdb_free);
1860 out:
1861 spin_unlock_bh(&br->multicast_lock);
1864 int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1866 int err = -ENOENT;
1868 spin_lock_bh(&br->multicast_lock);
1869 if (!netif_running(br->dev))
1870 goto unlock;
1872 switch (val) {
1873 case 0:
1874 case 2:
1875 del_timer(&br->multicast_router_timer);
1876 /* fall through */
1877 case 1:
1878 br->multicast_router = val;
1879 err = 0;
1880 break;
1882 default:
1883 err = -EINVAL;
1884 break;
1887 unlock:
1888 spin_unlock_bh(&br->multicast_lock);
1890 return err;
1893 int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1895 struct net_bridge *br = p->br;
1896 int err = -ENOENT;
1898 spin_lock(&br->multicast_lock);
1899 if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED)
1900 goto unlock;
1902 switch (val) {
1903 case 0:
1904 case 1:
1905 case 2:
1906 p->multicast_router = val;
1907 err = 0;
1909 if (val < 2 && !hlist_unhashed(&p->rlist))
1910 hlist_del_init_rcu(&p->rlist);
1912 if (val == 1)
1913 break;
1915 del_timer(&p->multicast_router_timer);
1917 if (val == 0)
1918 break;
1920 br_multicast_add_router(br, p);
1921 break;
1923 default:
1924 err = -EINVAL;
1925 break;
1928 unlock:
1929 spin_unlock(&br->multicast_lock);
1931 return err;
1934 static void br_multicast_start_querier(struct net_bridge *br,
1935 struct bridge_mcast_query *query)
1937 struct net_bridge_port *port;
1939 __br_multicast_open(br, query);
1941 list_for_each_entry(port, &br->port_list, list) {
1942 if (port->state == BR_STATE_DISABLED ||
1943 port->state == BR_STATE_BLOCKING)
1944 continue;
1946 if (query == &br->ip4_query)
1947 br_multicast_enable(&port->ip4_query);
1948 #if IS_ENABLED(CONFIG_IPV6)
1949 else
1950 br_multicast_enable(&port->ip6_query);
1951 #endif
1955 int br_multicast_toggle(struct net_bridge *br, unsigned long val)
1957 int err = 0;
1958 struct net_bridge_mdb_htable *mdb;
1960 spin_lock_bh(&br->multicast_lock);
1961 if (br->multicast_disabled == !val)
1962 goto unlock;
1964 br->multicast_disabled = !val;
1965 if (br->multicast_disabled)
1966 goto unlock;
1968 if (!netif_running(br->dev))
1969 goto unlock;
1971 mdb = mlock_dereference(br->mdb, br);
1972 if (mdb) {
1973 if (mdb->old) {
1974 err = -EEXIST;
1975 rollback:
1976 br->multicast_disabled = !!val;
1977 goto unlock;
1980 err = br_mdb_rehash(&br->mdb, mdb->max,
1981 br->hash_elasticity);
1982 if (err)
1983 goto rollback;
1986 br_multicast_start_querier(br, &br->ip4_query);
1987 #if IS_ENABLED(CONFIG_IPV6)
1988 br_multicast_start_querier(br, &br->ip6_query);
1989 #endif
1991 unlock:
1992 spin_unlock_bh(&br->multicast_lock);
1994 return err;
1997 int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
1999 unsigned long max_delay;
2001 val = !!val;
2003 spin_lock_bh(&br->multicast_lock);
2004 if (br->multicast_querier == val)
2005 goto unlock;
2007 br->multicast_querier = val;
2008 if (!val)
2009 goto unlock;
2011 max_delay = br->multicast_query_response_interval;
2013 if (!timer_pending(&br->ip4_querier.timer))
2014 br->ip4_querier.delay_time = jiffies + max_delay;
2016 br_multicast_start_querier(br, &br->ip4_query);
2018 #if IS_ENABLED(CONFIG_IPV6)
2019 if (!timer_pending(&br->ip6_querier.timer))
2020 br->ip6_querier.delay_time = jiffies + max_delay;
2022 br_multicast_start_querier(br, &br->ip6_query);
2023 #endif
2025 unlock:
2026 spin_unlock_bh(&br->multicast_lock);
2028 return 0;
2031 int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
2033 int err = -ENOENT;
2034 u32 old;
2035 struct net_bridge_mdb_htable *mdb;
2037 spin_lock_bh(&br->multicast_lock);
2038 if (!netif_running(br->dev))
2039 goto unlock;
2041 err = -EINVAL;
2042 if (!is_power_of_2(val))
2043 goto unlock;
2045 mdb = mlock_dereference(br->mdb, br);
2046 if (mdb && val < mdb->size)
2047 goto unlock;
2049 err = 0;
2051 old = br->hash_max;
2052 br->hash_max = val;
2054 if (mdb) {
2055 if (mdb->old) {
2056 err = -EEXIST;
2057 rollback:
2058 br->hash_max = old;
2059 goto unlock;
2062 err = br_mdb_rehash(&br->mdb, br->hash_max,
2063 br->hash_elasticity);
2064 if (err)
2065 goto rollback;
2068 unlock:
2069 spin_unlock_bh(&br->multicast_lock);
2071 return err;