14 #include <linux/if_ether.h>
15 #include <linux/igmp.h>
17 #include <linux/kernel.h>
19 #include <linux/netdevice.h>
20 #include <linux/netfilter_bridge.h>
21 #include <linux/random.h>
24 #include <linux/slab.h>
27 #if IS_ENABLED(CONFIG_IPV6)
36 #define mlock_dereference(X, br) \
37 rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock))
39 static void br_multicast_start_querier(
struct net_bridge *br);
41 #if IS_ENABLED(CONFIG_IPV6)
42 static inline int ipv6_is_transient_multicast(
const struct in6_addr *
addr)
50 static inline int br_ip_equal(
const struct br_ip *
a,
const struct br_ip *
b)
57 #if IS_ENABLED(CONFIG_IPV6)
70 #if IS_ENABLED(CONFIG_IPV6)
84 #if IS_ENABLED(CONFIG_IPV6)
98 hlist_for_each_entry_rcu(mp, p, &mdb->
mhash[hash],
hlist[mdb->
ver]) {
99 if (br_ip_equal(&mp->
addr, dst))
112 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
123 return br_mdb_ip_get(mdb, &br_dst);
126 #if IS_ENABLED(CONFIG_IPV6)
135 return br_mdb_ip_get(mdb, &br_dst);
145 if (br->multicast_disabled)
155 ip.
u.
ip4 = ip_hdr(skb)->daddr;
157 #if IS_ENABLED(CONFIG_IPV6)
159 ip.
u.ip6 = ipv6_hdr(skb)->daddr;
166 return br_mdb_ip_get(mdb, &ip);
190 for (i = 0; i < old->
max; i++)
192 hlist_add_head(&mp->
hlist[new->ver],
193 &new->mhash[br_ip_hash(
new, &mp->
addr)]);
199 for (i = 0; i <
new->max; i++) {
218 static void br_multicast_free_group(
struct rcu_head *
head)
226 static void br_multicast_group_expired(
unsigned long data)
232 spin_lock(&br->multicast_lock);
233 if (!netif_running(br->
dev) || timer_pending(&mp->
timer))
243 hlist_del_rcu(&mp->
hlist[mdb->
ver]);
249 spin_unlock(&br->multicast_lock);
252 static void br_multicast_del_pg(
struct net_bridge *br,
262 mp = br_mdb_ip_get(mdb, &pg->
addr);
266 for (pp = &mp->
ports;
273 hlist_del_init(&p->
mglist);
278 netif_running(br->
dev))
287 static void br_multicast_port_group_expired(
unsigned long data)
292 spin_lock(&br->multicast_lock);
293 if (!netif_running(br->
dev) || timer_pending(&pg->
timer) ||
294 hlist_unhashed(&pg->
mglist))
297 br_multicast_del_pg(br, pg);
300 spin_unlock(&br->multicast_lock);
324 mdb->
ver = old ? old->
ver ^ 1 : 0;
326 if (!old || elasticity)
334 err = br_mdb_copy(mdb, old, elasticity);
357 skb = netdev_alloc_skb_ip_align(br->
dev,
sizeof(*eth) +
sizeof(*iph) +
364 skb_reset_mac_header(skb);
377 skb_set_network_header(skb, skb->
len);
391 ((
u8 *)&iph[1])[1] = 4;
392 ((
u8 *)&iph[1])[2] = 0;
393 ((
u8 *)&iph[1])[3] = 0;
397 skb_set_transport_header(skb, skb->
len);
400 ih->
code = (group ? br->multicast_last_member_interval :
401 br->multicast_query_response_interval) /
408 __skb_pull(skb,
sizeof(*eth));
414 #if IS_ENABLED(CONFIG_IPV6)
425 skb = netdev_alloc_skb_ip_align(br->
dev,
sizeof(*eth) +
sizeof(*ip6h) +
433 skb_reset_mac_header(skb);
441 skb_set_network_header(skb, skb->
len);
442 ip6h = ipv6_hdr(skb);
456 hopopt = (
u8 *)(ip6h + 1);
466 skb_put(skb,
sizeof(*ip6h) + 8);
469 skb_set_transport_header(skb, skb->
len);
470 mldq = (
struct mld_msg *) icmp6_hdr(skb);
472 interval = ipv6_addr_any(group) ? br->multicast_last_member_interval :
473 br->multicast_query_response_interval;
479 mldq->mld_reserved = 0;
489 __skb_pull(skb,
sizeof(*eth));
499 switch (addr->
proto) {
501 return br_ip4_multicast_alloc_query(br, addr->
u.
ip4);
502 #if IS_ENABLED(CONFIG_IPV6)
504 return br_ip6_multicast_alloc_query(br, &addr->
u.
ip6);
512 struct br_ip *group,
int hash)
517 unsigned int count = 0;
532 if (
unlikely(count > br->hash_elasticity && count)) {
534 br_info(br,
"Multicast hash table "
535 "chain limit reached: %s\n",
536 port ? port->
dev->name : br->
dev->name);
538 elasticity = br->hash_elasticity;
541 if (mdb->
size >= max) {
544 br_warn(br,
"Multicast hash table maximum of %d "
545 "reached, disabling snooping: %s\n",
547 port ? port->
dev->name : br->
dev->name);
550 br->multicast_disabled = 1;
555 if (max > mdb->
max || elasticity) {
558 br_info(br,
"Multicast hash table "
560 port ? port->
dev->name : br->
dev->name);
565 err = br_mdb_rehash(&br->mdb, max, elasticity);
567 br_warn(br,
"Cannot rehash multicast "
568 "hash table, disabling snooping: %s, %d, %d\n",
569 port ? port->
dev->name : br->
dev->name,
602 hash = br_ip_hash(mdb, group);
603 mp = br_multicast_get_group(br, port, group, hash);
604 switch (PTR_ERR(mp)) {
611 hash = br_ip_hash(mdb, group);
634 static int br_multicast_add_group(
struct net_bridge *br,
644 spin_lock(&br->multicast_lock);
645 if (!netif_running(br->
dev) ||
649 mp = br_multicast_new_group(br, port, group);
660 for (pp = &mp->
ports;
665 if ((
unsigned long)p->
port < (
unsigned long)port)
677 hlist_add_head(&p->
mglist, &port->mglist);
689 spin_unlock(&br->multicast_lock);
693 static int br_ip4_multicast_add_group(
struct net_bridge *br,
697 struct br_ip br_group;
699 if (ipv4_is_local_multicast(group))
705 return br_multicast_add_group(br, port, &br_group);
708 #if IS_ENABLED(CONFIG_IPV6)
709 static int br_ip6_multicast_add_group(
struct net_bridge *br,
713 struct br_ip br_group;
715 if (!ipv6_is_transient_multicast(group))
721 return br_multicast_add_group(br, port, &br_group);
725 static void br_multicast_router_expired(
unsigned long data)
730 spin_lock(&br->multicast_lock);
731 if (port->multicast_router != 1 ||
732 timer_pending(&port->multicast_router_timer) ||
733 hlist_unhashed(&port->rlist))
736 hlist_del_init_rcu(&port->rlist);
739 spin_unlock(&br->multicast_lock);
742 static void br_multicast_local_router_expired(
unsigned long data)
746 static void br_multicast_querier_expired(
unsigned long data)
750 spin_lock(&br->multicast_lock);
751 if (!netif_running(br->
dev) || br->multicast_disabled)
754 br_multicast_start_querier(br);
757 spin_unlock(&br->multicast_lock);
760 static void __br_multicast_send_query(
struct net_bridge *br,
766 skb = br_multicast_alloc_query(br, ip);
771 __skb_push(skb,
sizeof(
struct ethhdr));
779 static void br_multicast_send_query(
struct net_bridge *br,
783 struct br_ip br_group;
785 if (!netif_running(br->
dev) || br->multicast_disabled ||
786 !br->multicast_querier ||
787 timer_pending(&br->multicast_querier_timer))
790 memset(&br_group.u, 0,
sizeof(br_group.u));
793 __br_multicast_send_query(br, port, &br_group);
795 #if IS_ENABLED(CONFIG_IPV6)
797 __br_multicast_send_query(br, port, &br_group);
801 time += sent < br->multicast_startup_query_count ?
802 br->multicast_startup_query_interval :
803 br->multicast_query_interval;
804 mod_timer(port ? &port->multicast_query_timer :
805 &br->multicast_query_timer, time);
808 static void br_multicast_port_query_expired(
unsigned long data)
813 spin_lock(&br->multicast_lock);
818 if (port->multicast_startup_queries_sent <
819 br->multicast_startup_query_count)
820 port->multicast_startup_queries_sent++;
822 br_multicast_send_query(port->
br, port,
823 port->multicast_startup_queries_sent);
826 spin_unlock(&br->multicast_lock);
831 port->multicast_router = 1;
833 setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
834 (
unsigned long)port);
836 br_multicast_port_query_expired, (
unsigned long)port);
846 port->multicast_startup_queries_sent = 0;
850 mod_timer(&port->multicast_query_timer, jiffies);
857 spin_lock(&br->multicast_lock);
858 if (br->multicast_disabled || !netif_running(br->
dev))
861 __br_multicast_enable_port(port);
864 spin_unlock(&br->multicast_lock);
873 spin_lock(&br->multicast_lock);
875 br_multicast_del_pg(br, pg);
877 if (!hlist_unhashed(&port->rlist))
878 hlist_del_init_rcu(&port->rlist);
879 del_timer(&port->multicast_router_timer);
881 spin_unlock(&br->multicast_lock);
884 static int br_ip4_multicast_igmp3_report(
struct net_bridge *br,
897 if (!pskb_may_pull(skb,
sizeof(*ih)))
900 ih = igmpv3_report_hdr(skb);
904 for (i = 0; i < num; i++) {
905 len +=
sizeof(*grec);
906 if (!pskb_may_pull(skb, len))
909 grec = (
void *)(skb->
data + len -
sizeof(*grec));
914 if (!pskb_may_pull(skb, len))
931 err = br_ip4_multicast_add_group(br, port, group);
939 #if IS_ENABLED(CONFIG_IPV6)
940 static int br_ip6_multicast_mld2_report(
struct net_bridge *br,
951 if (!pskb_may_pull(skb,
sizeof(*icmp6h)))
954 icmp6h = icmp6_hdr(skb);
956 len =
sizeof(*icmp6h);
958 for (i = 0; i < num; i++) {
961 nsrcs = skb_header_pointer(skb,
964 sizeof(_nsrcs), &_nsrcs);
968 if (!pskb_may_pull(skb,
969 len +
sizeof(*grec) +
974 len +=
sizeof(*grec) +
991 err = br_ip6_multicast_add_group(br, port, &grec->
grec_mca);
1005 static void br_multicast_add_router(
struct net_bridge *br,
1012 if ((
unsigned long) port >= (
unsigned long) p)
1018 hlist_add_after_rcu(slot, &port->rlist);
1020 hlist_add_head_rcu(&port->rlist, &br->router_list);
1023 static void br_multicast_mark_router(
struct net_bridge *br,
1029 if (br->multicast_router == 1)
1031 now + br->multicast_querier_interval);
1035 if (port->multicast_router != 1)
1038 if (!hlist_unhashed(&port->rlist))
1041 br_multicast_add_router(br, port);
1044 mod_timer(&port->multicast_router_timer,
1045 now + br->multicast_querier_interval);
1048 static void br_multicast_query_received(
struct net_bridge *br,
1054 jiffies + br->multicast_querier_interval);
1055 else if (timer_pending(&br->multicast_querier_timer))
1058 br_multicast_mark_router(br, port);
1061 static int br_ip4_multicast_query(
struct net_bridge *br,
1065 const struct iphdr *iph = ip_hdr(skb);
1066 struct igmphdr *ih = igmp_hdr(skb);
1071 unsigned long max_delay;
1076 spin_lock(&br->multicast_lock);
1077 if (!netif_running(br->
dev) ||
1081 br_multicast_query_received(br, port, !!iph->
saddr);
1085 if (skb->
len ==
sizeof(*ih)) {
1089 max_delay = 10 *
HZ;
1093 if (!pskb_may_pull(skb,
sizeof(
struct igmpv3_query))) {
1098 ih3 = igmpv3_query_hdr(skb);
1102 max_delay = ih3->
code ?
1113 max_delay *= br->multicast_last_member_count;
1116 (timer_pending(&mp->
timer) ?
1121 for (pp = &mp->
ports;
1124 if (timer_pending(&p->
timer) ?
1131 spin_unlock(&br->multicast_lock);
1135 #if IS_ENABLED(CONFIG_IPV6)
1136 static int br_ip6_multicast_query(
struct net_bridge *br,
1140 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
1146 unsigned long max_delay;
1151 spin_lock(&br->multicast_lock);
1152 if (!netif_running(br->
dev) ||
1156 br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->
saddr));
1158 if (skb->
len ==
sizeof(*mld)) {
1159 if (!pskb_may_pull(skb,
sizeof(*mld))) {
1163 mld = (
struct mld_msg *) icmp6_hdr(skb);
1167 }
else if (skb->
len >=
sizeof(*mld2q)) {
1168 if (!pskb_may_pull(skb,
sizeof(*mld2q))) {
1175 max_delay = mld2q->mld2q_mrc ?
MLDV2_MRC(mld2q->mld2q_mrc) : 1;
1185 max_delay *= br->multicast_last_member_count;
1187 (timer_pending(&mp->
timer) ?
1192 for (pp = &mp->
ports;
1195 if (timer_pending(&p->
timer) ?
1202 spin_unlock(&br->multicast_lock);
1207 static void br_multicast_leave_group(
struct net_bridge *br,
1209 struct br_ip *group)
1217 spin_lock(&br->multicast_lock);
1218 if (!netif_running(br->
dev) ||
1220 timer_pending(&br->multicast_querier_timer))
1224 mp = br_mdb_ip_get(mdb, group);
1229 time = now + br->multicast_last_member_count *
1230 br->multicast_last_member_interval;
1234 (timer_pending(&mp->
timer) ?
1246 if (p->
port != port)
1249 if (!hlist_unhashed(&p->
mglist) &&
1250 (timer_pending(&p->
timer) ?
1260 spin_unlock(&br->multicast_lock);
1263 static void br_ip4_multicast_leave_group(
struct net_bridge *br,
1267 struct br_ip br_group;
1269 if (ipv4_is_local_multicast(group))
1275 br_multicast_leave_group(br, port, &br_group);
1278 #if IS_ENABLED(CONFIG_IPV6)
1279 static void br_ip6_multicast_leave_group(
struct net_bridge *br,
1283 struct br_ip br_group;
1285 if (!ipv6_is_transient_multicast(group))
1291 br_multicast_leave_group(br, port, &br_group);
1295 static int br_multicast_ipv4_rcv(
struct net_bridge *br,
1300 const struct iphdr *iph;
1307 if (!pskb_may_pull(skb,
sizeof(*iph)))
1312 if (iph->ihl < 5 || iph->version != 4)
1315 if (!pskb_may_pull(skb, ip_hdrlen(skb)))
1330 if (skb->
len < len || len < ip_hdrlen(skb))
1333 if (skb->
len > len) {
1338 err = pskb_trim_rcsum(skb2, len);
1343 len -= ip_hdrlen(skb2);
1344 offset = skb_network_offset(skb2) + ip_hdrlen(skb2);
1345 __skb_pull(skb2, offset);
1346 skb_reset_transport_header(skb2);
1349 if (!pskb_may_pull(skb2,
sizeof(*ih)))
1354 if (!csum_fold(skb2->
csum))
1359 if (skb_checksum_complete(skb2))
1366 ih = igmp_hdr(skb2);
1372 err = br_ip4_multicast_add_group(br, port, ih->
group);
1375 err = br_ip4_multicast_igmp3_report(br, port, skb2);
1378 err = br_ip4_multicast_query(br, port, skb2);
1381 br_ip4_multicast_leave_group(br, port, ih->
group);
1386 __skb_push(skb2, offset);
1393 #if IS_ENABLED(CONFIG_IPV6)
1394 static int br_multicast_ipv6_rcv(
struct net_bridge *br,
1407 if (!pskb_may_pull(skb,
sizeof(*ip6h)))
1410 ip6h = ipv6_hdr(skb);
1418 if (ip6h->version != 6 ||
1439 if (!pskb_may_pull(skb2, offset +
sizeof(
struct icmp6hdr)))
1442 len -= offset - skb_network_offset(skb2);
1444 __skb_pull(skb2, offset);
1445 skb_reset_transport_header(skb2);
1446 skb_postpull_rcsum(skb2, skb_network_header(skb2),
1447 skb_network_header_len(skb2));
1449 icmp6_type = icmp6_hdr(skb2)->icmp6_type;
1451 switch (icmp6_type) {
1463 if (skb2->
len > len) {
1464 err = pskb_trim_rcsum(skb2, len);
1470 ip6h = ipv6_hdr(skb2);
1491 switch (icmp6_type) {
1495 if (!pskb_may_pull(skb2,
sizeof(*mld))) {
1499 mld = (
struct mld_msg *)skb_transport_header(skb2);
1501 err = br_ip6_multicast_add_group(br, port, &mld->
mld_mca);
1505 err = br_ip6_multicast_mld2_report(br, port, skb2);
1508 err = br_ip6_multicast_query(br, port, skb2);
1513 if (!pskb_may_pull(skb2,
sizeof(*mld))) {
1517 mld = (
struct mld_msg *)skb_transport_header(skb2);
1518 br_ip6_multicast_leave_group(br, port, &mld->
mld_mca);
1534 if (br->multicast_disabled)
1539 return br_multicast_ipv4_rcv(br, port, skb);
1540 #if IS_ENABLED(CONFIG_IPV6)
1542 return br_multicast_ipv6_rcv(br, port, skb);
1549 static void br_multicast_query_expired(
unsigned long data)
1553 spin_lock(&br->multicast_lock);
1554 if (br->multicast_startup_queries_sent <
1555 br->multicast_startup_query_count)
1556 br->multicast_startup_queries_sent++;
1558 br_multicast_send_query(br,
NULL, br->multicast_startup_queries_sent);
1560 spin_unlock(&br->multicast_lock);
1565 br->hash_elasticity = 4;
1568 br->multicast_router = 1;
1569 br->multicast_querier = 0;
1570 br->multicast_last_member_count = 2;
1571 br->multicast_startup_query_count = 2;
1573 br->multicast_last_member_interval =
HZ;
1574 br->multicast_query_response_interval = 10 *
HZ;
1575 br->multicast_startup_query_interval = 125 *
HZ / 4;
1576 br->multicast_query_interval = 125 *
HZ;
1577 br->multicast_querier_interval = 255 *
HZ;
1578 br->multicast_membership_interval = 260 *
HZ;
1582 br_multicast_local_router_expired, 0);
1584 br_multicast_querier_expired, (
unsigned long)br);
1585 setup_timer(&br->multicast_query_timer, br_multicast_query_expired,
1591 br->multicast_startup_queries_sent = 0;
1593 if (br->multicast_disabled)
1611 spin_lock_bh(&br->multicast_lock);
1619 for (i = 0; i < mdb->
max; i++) {
1628 spin_unlock_bh(&br->multicast_lock);
1630 spin_lock_bh(&br->multicast_lock);
1638 spin_unlock_bh(&br->multicast_lock);
1645 spin_lock_bh(&br->multicast_lock);
1646 if (!netif_running(br->
dev))
1655 br->multicast_router =
val;
1665 spin_unlock_bh(&br->multicast_lock);
1675 spin_lock(&br->multicast_lock);
1683 p->multicast_router =
val;
1686 if (val < 2 && !hlist_unhashed(&p->rlist))
1687 hlist_del_init_rcu(&p->rlist);
1697 br_multicast_add_router(br, p);
1706 spin_unlock(&br->multicast_lock);
1711 static void br_multicast_start_querier(
struct net_bridge *br)
1722 __br_multicast_enable_port(port);
1731 spin_lock_bh(&br->multicast_lock);
1732 if (br->multicast_disabled == !val)
1735 br->multicast_disabled = !
val;
1736 if (br->multicast_disabled)
1739 if (!netif_running(br->
dev))
1747 br->multicast_disabled = !!
val;
1751 err = br_mdb_rehash(&br->mdb, mdb->
max,
1752 br->hash_elasticity);
1757 br_multicast_start_querier(br);
1760 spin_unlock_bh(&br->multicast_lock);
1769 spin_lock_bh(&br->multicast_lock);
1770 if (br->multicast_querier == val)
1773 br->multicast_querier =
val;
1775 br_multicast_start_querier(br);
1778 spin_unlock_bh(&br->multicast_lock);
1789 spin_lock(&br->multicast_lock);
1790 if (!netif_running(br->
dev))
1798 if (mdb && val < mdb->
size)
1814 err = br_mdb_rehash(&br->mdb, br->hash_max,
1815 br->hash_elasticity);
1821 spin_unlock(&br->multicast_lock);