Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Bridge multicast support. |
| 4 | * |
| 5 | * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au> |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/err.h> |
Linus Lüssing | 07f8ac4 | 2014-06-07 18:26:28 +0200 | [diff] [blame] | 9 | #include <linux/export.h> |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 10 | #include <linux/if_ether.h> |
| 11 | #include <linux/igmp.h> |
Linus Lüssing | 4b3087c | 2019-01-21 07:26:28 +0100 | [diff] [blame] | 12 | #include <linux/in.h> |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 13 | #include <linux/jhash.h> |
| 14 | #include <linux/kernel.h> |
Herbert Xu | b195167 | 2010-02-27 19:41:51 +0000 | [diff] [blame] | 15 | #include <linux/log2.h> |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 16 | #include <linux/netdevice.h> |
| 17 | #include <linux/netfilter_bridge.h> |
| 18 | #include <linux/random.h> |
| 19 | #include <linux/rculist.h> |
| 20 | #include <linux/skbuff.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/timer.h> |
Cong Wang | 1c8ad5b | 2013-05-21 21:52:54 +0000 | [diff] [blame] | 23 | #include <linux/inetdevice.h> |
Nikolay Aleksandrov | 91b02d3 | 2016-10-31 13:21:05 +0100 | [diff] [blame] | 24 | #include <linux/mroute.h> |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 25 | #include <net/ip.h> |
Nogah Frankel | 147c1e9 | 2017-02-09 14:54:40 +0100 | [diff] [blame] | 26 | #include <net/switchdev.h> |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 27 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Lüssing | 4b3087c | 2019-01-21 07:26:28 +0100 | [diff] [blame] | 28 | #include <linux/icmpv6.h> |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 29 | #include <net/ipv6.h> |
| 30 | #include <net/mld.h> |
David S. Miller | d4c4f07 | 2010-04-27 10:16:54 -0700 | [diff] [blame] | 31 | #include <net/ip6_checksum.h> |
Linus Lüssing | 3c3769e | 2013-09-04 02:13:39 +0200 | [diff] [blame] | 32 | #include <net/addrconf.h> |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 33 | #endif |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 34 | |
| 35 | #include "br_private.h" |
| 36 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 37 | static const struct rhashtable_params br_mdb_rht_params = { |
| 38 | .head_offset = offsetof(struct net_bridge_mdb_entry, rhnode), |
| 39 | .key_offset = offsetof(struct net_bridge_mdb_entry, addr), |
| 40 | .key_len = sizeof(struct br_ip), |
| 41 | .automatic_shrinking = true, |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 42 | }; |
| 43 | |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 44 | static void br_multicast_start_querier(struct net_bridge *br, |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 45 | struct bridge_mcast_own_query *query); |
Satish Ashok | 754bc54 | 2015-06-19 01:22:57 -0700 | [diff] [blame] | 46 | static void br_multicast_add_router(struct net_bridge *br, |
| 47 | struct net_bridge_port *port); |
Satish Ashok | bc8c20a | 2015-07-13 05:28:37 -0700 | [diff] [blame] | 48 | static void br_ip4_multicast_leave_group(struct net_bridge *br, |
| 49 | struct net_bridge_port *port, |
| 50 | __be32 group, |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 51 | __u16 vid, |
| 52 | const unsigned char *src); |
| 53 | |
Nogah Frankel | f12e7d9 | 2017-02-09 14:54:41 +0100 | [diff] [blame] | 54 | static void __del_port_router(struct net_bridge_port *p); |
Satish Ashok | bc8c20a | 2015-07-13 05:28:37 -0700 | [diff] [blame] | 55 | #if IS_ENABLED(CONFIG_IPV6) |
| 56 | static void br_ip6_multicast_leave_group(struct net_bridge *br, |
| 57 | struct net_bridge_port *port, |
| 58 | const struct in6_addr *group, |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 59 | __u16 vid, const unsigned char *src); |
Satish Ashok | bc8c20a | 2015-07-13 05:28:37 -0700 | [diff] [blame] | 60 | #endif |
Herbert Xu | c83b8fa | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 61 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 62 | static struct net_bridge_mdb_entry *br_mdb_ip_get_rcu(struct net_bridge *br, |
| 63 | struct br_ip *dst) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 64 | { |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 65 | return rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 68 | struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge *br, |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 69 | struct br_ip *dst) |
Herbert Xu | 7f285fa | 2010-07-05 14:50:08 +0000 | [diff] [blame] | 70 | { |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 71 | struct net_bridge_mdb_entry *ent; |
Herbert Xu | 7f285fa | 2010-07-05 14:50:08 +0000 | [diff] [blame] | 72 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 73 | lockdep_assert_held_once(&br->multicast_lock); |
| 74 | |
| 75 | rcu_read_lock(); |
| 76 | ent = rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params); |
| 77 | rcu_read_unlock(); |
| 78 | |
| 79 | return ent; |
Herbert Xu | 7f285fa | 2010-07-05 14:50:08 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 82 | static struct net_bridge_mdb_entry *br_mdb_ip4_get(struct net_bridge *br, |
| 83 | __be32 dst, __u16 vid) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 84 | { |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 85 | struct br_ip br_dst; |
Herbert Xu | 0821ec5 | 2010-03-15 20:38:25 -0700 | [diff] [blame] | 86 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 87 | memset(&br_dst, 0, sizeof(br_dst)); |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 88 | br_dst.u.ip4 = dst; |
| 89 | br_dst.proto = htons(ETH_P_IP); |
Vlad Yasevich | b0e9a30 | 2013-02-13 12:00:17 +0000 | [diff] [blame] | 90 | br_dst.vid = vid; |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 91 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 92 | return br_mdb_ip_get(br, &br_dst); |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 93 | } |
| 94 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 95 | #if IS_ENABLED(CONFIG_IPV6) |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 96 | static struct net_bridge_mdb_entry *br_mdb_ip6_get(struct net_bridge *br, |
| 97 | const struct in6_addr *dst, |
| 98 | __u16 vid) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 99 | { |
| 100 | struct br_ip br_dst; |
| 101 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 102 | memset(&br_dst, 0, sizeof(br_dst)); |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 103 | br_dst.u.ip6 = *dst; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 104 | br_dst.proto = htons(ETH_P_IPV6); |
Vlad Yasevich | b0e9a30 | 2013-02-13 12:00:17 +0000 | [diff] [blame] | 105 | br_dst.vid = vid; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 106 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 107 | return br_mdb_ip_get(br, &br_dst); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 108 | } |
| 109 | #endif |
| 110 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 111 | struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br, |
Cong Wang | fbca58a | 2013-03-07 03:05:33 +0000 | [diff] [blame] | 112 | struct sk_buff *skb, u16 vid) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 113 | { |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 114 | struct br_ip ip; |
| 115 | |
Nikolay Aleksandrov | 13cefad | 2018-09-26 17:01:03 +0300 | [diff] [blame] | 116 | if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 117 | return NULL; |
| 118 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 119 | if (BR_INPUT_SKB_CB(skb)->igmp) |
| 120 | return NULL; |
| 121 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 122 | memset(&ip, 0, sizeof(ip)); |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 123 | ip.proto = skb->protocol; |
Cong Wang | fbca58a | 2013-03-07 03:05:33 +0000 | [diff] [blame] | 124 | ip.vid = vid; |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 125 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 126 | switch (skb->protocol) { |
| 127 | case htons(ETH_P_IP): |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 128 | ip.u.ip4 = ip_hdr(skb)->daddr; |
| 129 | break; |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 130 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 131 | case htons(ETH_P_IPV6): |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 132 | ip.u.ip6 = ipv6_hdr(skb)->daddr; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 133 | break; |
| 134 | #endif |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 135 | default: |
| 136 | return NULL; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 139 | return br_mdb_ip_get_rcu(br, &ip); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 142 | static void br_multicast_group_expired(struct timer_list *t) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 143 | { |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 144 | struct net_bridge_mdb_entry *mp = from_timer(mp, t, timer); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 145 | struct net_bridge *br = mp->br; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 146 | |
| 147 | spin_lock(&br->multicast_lock); |
| 148 | if (!netif_running(br->dev) || timer_pending(&mp->timer)) |
| 149 | goto out; |
| 150 | |
Nikolay Aleksandrov | 1bc844e | 2019-08-17 14:22:13 +0300 | [diff] [blame] | 151 | br_multicast_host_leave(mp, true); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 152 | |
| 153 | if (mp->ports) |
| 154 | goto out; |
| 155 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 156 | rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode, |
| 157 | br_mdb_rht_params); |
| 158 | hlist_del_rcu(&mp->mdb_node); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 159 | |
Nikolay Aleksandrov | 4329596 | 2018-12-05 15:14:25 +0200 | [diff] [blame] | 160 | kfree_rcu(mp, rcu); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 161 | |
| 162 | out: |
| 163 | spin_unlock(&br->multicast_lock); |
| 164 | } |
| 165 | |
| 166 | static void br_multicast_del_pg(struct net_bridge *br, |
| 167 | struct net_bridge_port_group *pg) |
| 168 | { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 169 | struct net_bridge_mdb_entry *mp; |
| 170 | struct net_bridge_port_group *p; |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 171 | struct net_bridge_port_group __rcu **pp; |
| 172 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 173 | mp = br_mdb_ip_get(br, &pg->addr); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 174 | if (WARN_ON(!mp)) |
| 175 | return; |
| 176 | |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 177 | for (pp = &mp->ports; |
| 178 | (p = mlock_dereference(*pp, br)) != NULL; |
| 179 | pp = &p->next) { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 180 | if (p != pg) |
| 181 | continue; |
| 182 | |
stephen hemminger | 83f6a74 | 2010-04-27 15:01:06 +0000 | [diff] [blame] | 183 | rcu_assign_pointer(*pp, p->next); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 184 | hlist_del_init(&p->mglist); |
| 185 | del_timer(&p->timer); |
Elad Raz | 45ebcce | 2016-04-21 12:52:45 +0200 | [diff] [blame] | 186 | br_mdb_notify(br->dev, p->port, &pg->addr, RTM_DELMDB, |
| 187 | p->flags); |
Nikolay Aleksandrov | 4329596 | 2018-12-05 15:14:25 +0200 | [diff] [blame] | 188 | kfree_rcu(p, rcu); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 189 | |
Andrew Lunn | ff0fd34 | 2017-11-09 23:10:57 +0100 | [diff] [blame] | 190 | if (!mp->ports && !mp->host_joined && |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 191 | netif_running(br->dev)) |
| 192 | mod_timer(&mp->timer, jiffies); |
| 193 | |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | WARN_ON(1); |
| 198 | } |
| 199 | |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 200 | static void br_multicast_port_group_expired(struct timer_list *t) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 201 | { |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 202 | struct net_bridge_port_group *pg = from_timer(pg, t, timer); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 203 | struct net_bridge *br = pg->port->br; |
| 204 | |
| 205 | spin_lock(&br->multicast_lock); |
| 206 | if (!netif_running(br->dev) || timer_pending(&pg->timer) || |
Elad Raz | 9d06b6d | 2016-02-03 09:57:05 +0100 | [diff] [blame] | 207 | hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 208 | goto out; |
| 209 | |
| 210 | br_multicast_del_pg(br, pg); |
| 211 | |
| 212 | out: |
| 213 | spin_unlock(&br->multicast_lock); |
| 214 | } |
| 215 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 216 | static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br, |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 217 | __be32 group, |
| 218 | u8 *igmp_type) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 219 | { |
Nikolay Aleksandrov | 5e92358 | 2016-11-21 13:03:24 +0100 | [diff] [blame] | 220 | struct igmpv3_query *ihv3; |
| 221 | size_t igmp_hdr_size; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 222 | struct sk_buff *skb; |
| 223 | struct igmphdr *ih; |
| 224 | struct ethhdr *eth; |
| 225 | struct iphdr *iph; |
| 226 | |
Nikolay Aleksandrov | 5e92358 | 2016-11-21 13:03:24 +0100 | [diff] [blame] | 227 | igmp_hdr_size = sizeof(*ih); |
| 228 | if (br->multicast_igmp_version == 3) |
| 229 | igmp_hdr_size = sizeof(*ihv3); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 230 | skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) + |
Nikolay Aleksandrov | 5e92358 | 2016-11-21 13:03:24 +0100 | [diff] [blame] | 231 | igmp_hdr_size + 4); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 232 | if (!skb) |
| 233 | goto out; |
| 234 | |
| 235 | skb->protocol = htons(ETH_P_IP); |
| 236 | |
| 237 | skb_reset_mac_header(skb); |
| 238 | eth = eth_hdr(skb); |
| 239 | |
Joe Perches | e5a727f | 2014-02-23 00:05:25 -0800 | [diff] [blame] | 240 | ether_addr_copy(eth->h_source, br->dev->dev_addr); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 241 | eth->h_dest[0] = 1; |
| 242 | eth->h_dest[1] = 0; |
| 243 | eth->h_dest[2] = 0x5e; |
| 244 | eth->h_dest[3] = 0; |
| 245 | eth->h_dest[4] = 0; |
| 246 | eth->h_dest[5] = 1; |
| 247 | eth->h_proto = htons(ETH_P_IP); |
| 248 | skb_put(skb, sizeof(*eth)); |
| 249 | |
| 250 | skb_set_network_header(skb, skb->len); |
| 251 | iph = ip_hdr(skb); |
| 252 | |
| 253 | iph->version = 4; |
| 254 | iph->ihl = 6; |
| 255 | iph->tos = 0xc0; |
Nikolay Aleksandrov | 5e92358 | 2016-11-21 13:03:24 +0100 | [diff] [blame] | 256 | iph->tot_len = htons(sizeof(*iph) + igmp_hdr_size + 4); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 257 | iph->id = 0; |
| 258 | iph->frag_off = htons(IP_DF); |
| 259 | iph->ttl = 1; |
| 260 | iph->protocol = IPPROTO_IGMP; |
Nikolay Aleksandrov | 675779a | 2018-09-26 17:01:04 +0300 | [diff] [blame] | 261 | iph->saddr = br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR) ? |
Cong Wang | 1c8ad5b | 2013-05-21 21:52:54 +0000 | [diff] [blame] | 262 | inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 263 | iph->daddr = htonl(INADDR_ALLHOSTS_GROUP); |
| 264 | ((u8 *)&iph[1])[0] = IPOPT_RA; |
| 265 | ((u8 *)&iph[1])[1] = 4; |
| 266 | ((u8 *)&iph[1])[2] = 0; |
| 267 | ((u8 *)&iph[1])[3] = 0; |
| 268 | ip_send_check(iph); |
| 269 | skb_put(skb, 24); |
| 270 | |
| 271 | skb_set_transport_header(skb, skb->len); |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 272 | *igmp_type = IGMP_HOST_MEMBERSHIP_QUERY; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 273 | |
Nikolay Aleksandrov | 5e92358 | 2016-11-21 13:03:24 +0100 | [diff] [blame] | 274 | switch (br->multicast_igmp_version) { |
| 275 | case 2: |
| 276 | ih = igmp_hdr(skb); |
| 277 | ih->type = IGMP_HOST_MEMBERSHIP_QUERY; |
| 278 | ih->code = (group ? br->multicast_last_member_interval : |
| 279 | br->multicast_query_response_interval) / |
| 280 | (HZ / IGMP_TIMER_SCALE); |
| 281 | ih->group = group; |
| 282 | ih->csum = 0; |
| 283 | ih->csum = ip_compute_csum((void *)ih, sizeof(*ih)); |
| 284 | break; |
| 285 | case 3: |
| 286 | ihv3 = igmpv3_query_hdr(skb); |
| 287 | ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY; |
| 288 | ihv3->code = (group ? br->multicast_last_member_interval : |
| 289 | br->multicast_query_response_interval) / |
| 290 | (HZ / IGMP_TIMER_SCALE); |
| 291 | ihv3->group = group; |
| 292 | ihv3->qqic = br->multicast_query_interval / HZ; |
| 293 | ihv3->nsrcs = 0; |
| 294 | ihv3->resv = 0; |
| 295 | ihv3->suppress = 0; |
| 296 | ihv3->qrv = 2; |
| 297 | ihv3->csum = 0; |
| 298 | ihv3->csum = ip_compute_csum((void *)ihv3, sizeof(*ihv3)); |
| 299 | break; |
| 300 | } |
| 301 | |
| 302 | skb_put(skb, igmp_hdr_size); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 303 | __skb_pull(skb, sizeof(*eth)); |
| 304 | |
| 305 | out: |
| 306 | return skb; |
| 307 | } |
| 308 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 309 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 310 | static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br, |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 311 | const struct in6_addr *grp, |
| 312 | u8 *igmp_type) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 313 | { |
Nikolay Aleksandrov | aa2ae3e | 2016-11-21 13:03:25 +0100 | [diff] [blame] | 314 | struct mld2_query *mld2q; |
| 315 | unsigned long interval; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 316 | struct ipv6hdr *ip6h; |
| 317 | struct mld_msg *mldq; |
Nikolay Aleksandrov | aa2ae3e | 2016-11-21 13:03:25 +0100 | [diff] [blame] | 318 | size_t mld_hdr_size; |
| 319 | struct sk_buff *skb; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 320 | struct ethhdr *eth; |
| 321 | u8 *hopopt; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 322 | |
Nikolay Aleksandrov | aa2ae3e | 2016-11-21 13:03:25 +0100 | [diff] [blame] | 323 | mld_hdr_size = sizeof(*mldq); |
| 324 | if (br->multicast_mld_version == 2) |
| 325 | mld_hdr_size = sizeof(*mld2q); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 326 | skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) + |
Nikolay Aleksandrov | aa2ae3e | 2016-11-21 13:03:25 +0100 | [diff] [blame] | 327 | 8 + mld_hdr_size); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 328 | if (!skb) |
| 329 | goto out; |
| 330 | |
| 331 | skb->protocol = htons(ETH_P_IPV6); |
| 332 | |
| 333 | /* Ethernet header */ |
| 334 | skb_reset_mac_header(skb); |
| 335 | eth = eth_hdr(skb); |
| 336 | |
Joe Perches | e5a727f | 2014-02-23 00:05:25 -0800 | [diff] [blame] | 337 | ether_addr_copy(eth->h_source, br->dev->dev_addr); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 338 | eth->h_proto = htons(ETH_P_IPV6); |
| 339 | skb_put(skb, sizeof(*eth)); |
| 340 | |
| 341 | /* IPv6 header + HbH option */ |
| 342 | skb_set_network_header(skb, skb->len); |
| 343 | ip6h = ipv6_hdr(skb); |
| 344 | |
| 345 | *(__force __be32 *)ip6h = htonl(0x60000000); |
Nikolay Aleksandrov | aa2ae3e | 2016-11-21 13:03:25 +0100 | [diff] [blame] | 346 | ip6h->payload_len = htons(8 + mld_hdr_size); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 347 | ip6h->nexthdr = IPPROTO_HOPOPTS; |
| 348 | ip6h->hop_limit = 1; |
Linus Lüssing | a7bff75 | 2011-03-22 11:40:32 +0000 | [diff] [blame] | 349 | ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1)); |
Ulrich Weber | d1d81d4 | 2012-03-05 04:52:44 +0000 | [diff] [blame] | 350 | if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0, |
| 351 | &ip6h->saddr)) { |
| 352 | kfree_skb(skb); |
Nikolay Aleksandrov | 675779a | 2018-09-26 17:01:04 +0300 | [diff] [blame] | 353 | br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, false); |
Ulrich Weber | d1d81d4 | 2012-03-05 04:52:44 +0000 | [diff] [blame] | 354 | return NULL; |
| 355 | } |
daniel | 0888d5f | 2016-06-24 12:35:18 +0200 | [diff] [blame] | 356 | |
Nikolay Aleksandrov | 675779a | 2018-09-26 17:01:04 +0300 | [diff] [blame] | 357 | br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true); |
Linus Lüssing | 36cff5a | 2011-02-17 08:17:51 +0000 | [diff] [blame] | 358 | ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 359 | |
| 360 | hopopt = (u8 *)(ip6h + 1); |
| 361 | hopopt[0] = IPPROTO_ICMPV6; /* next hdr */ |
| 362 | hopopt[1] = 0; /* length of HbH */ |
| 363 | hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */ |
| 364 | hopopt[3] = 2; /* Length of RA Option */ |
| 365 | hopopt[4] = 0; /* Type = 0x0000 (MLD) */ |
| 366 | hopopt[5] = 0; |
Eldad Zack | 1de5a71 | 2012-05-17 06:00:25 +0000 | [diff] [blame] | 367 | hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */ |
| 368 | hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */ |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 369 | |
| 370 | skb_put(skb, sizeof(*ip6h) + 8); |
| 371 | |
| 372 | /* ICMPv6 */ |
| 373 | skb_set_transport_header(skb, skb->len); |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 374 | interval = ipv6_addr_any(grp) ? |
Linus Lüssing | 32de868 | 2013-06-16 23:20:34 +0200 | [diff] [blame] | 375 | br->multicast_query_response_interval : |
| 376 | br->multicast_last_member_interval; |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 377 | *igmp_type = ICMPV6_MGM_QUERY; |
Nikolay Aleksandrov | aa2ae3e | 2016-11-21 13:03:25 +0100 | [diff] [blame] | 378 | switch (br->multicast_mld_version) { |
| 379 | case 1: |
| 380 | mldq = (struct mld_msg *)icmp6_hdr(skb); |
| 381 | mldq->mld_type = ICMPV6_MGM_QUERY; |
| 382 | mldq->mld_code = 0; |
| 383 | mldq->mld_cksum = 0; |
| 384 | mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval)); |
| 385 | mldq->mld_reserved = 0; |
| 386 | mldq->mld_mca = *grp; |
| 387 | mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, |
| 388 | sizeof(*mldq), IPPROTO_ICMPV6, |
| 389 | csum_partial(mldq, |
| 390 | sizeof(*mldq), |
| 391 | 0)); |
| 392 | break; |
| 393 | case 2: |
| 394 | mld2q = (struct mld2_query *)icmp6_hdr(skb); |
Lance Richardson | 53631a5 | 2017-01-16 18:11:35 -0500 | [diff] [blame] | 395 | mld2q->mld2q_mrc = htons((u16)jiffies_to_msecs(interval)); |
Nikolay Aleksandrov | aa2ae3e | 2016-11-21 13:03:25 +0100 | [diff] [blame] | 396 | mld2q->mld2q_type = ICMPV6_MGM_QUERY; |
| 397 | mld2q->mld2q_code = 0; |
| 398 | mld2q->mld2q_cksum = 0; |
| 399 | mld2q->mld2q_resv1 = 0; |
| 400 | mld2q->mld2q_resv2 = 0; |
| 401 | mld2q->mld2q_suppress = 0; |
| 402 | mld2q->mld2q_qrv = 2; |
| 403 | mld2q->mld2q_nsrcs = 0; |
| 404 | mld2q->mld2q_qqic = br->multicast_query_interval / HZ; |
| 405 | mld2q->mld2q_mca = *grp; |
| 406 | mld2q->mld2q_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, |
| 407 | sizeof(*mld2q), |
| 408 | IPPROTO_ICMPV6, |
| 409 | csum_partial(mld2q, |
| 410 | sizeof(*mld2q), |
| 411 | 0)); |
| 412 | break; |
| 413 | } |
| 414 | skb_put(skb, mld_hdr_size); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 415 | |
| 416 | __skb_pull(skb, sizeof(*eth)); |
| 417 | |
| 418 | out: |
| 419 | return skb; |
| 420 | } |
| 421 | #endif |
| 422 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 423 | static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br, |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 424 | struct br_ip *addr, |
| 425 | u8 *igmp_type) |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 426 | { |
| 427 | switch (addr->proto) { |
| 428 | case htons(ETH_P_IP): |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 429 | return br_ip4_multicast_alloc_query(br, addr->u.ip4, igmp_type); |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 430 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 431 | case htons(ETH_P_IPV6): |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 432 | return br_ip6_multicast_alloc_query(br, &addr->u.ip6, |
| 433 | igmp_type); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 434 | #endif |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 435 | } |
| 436 | return NULL; |
| 437 | } |
| 438 | |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 439 | struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br, |
Nikolay Aleksandrov | 5e92358 | 2016-11-21 13:03:24 +0100 | [diff] [blame] | 440 | struct br_ip *group) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 441 | { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 442 | struct net_bridge_mdb_entry *mp; |
Tobias Klauser | 4c0833b | 2010-12-10 03:18:04 +0000 | [diff] [blame] | 443 | int err; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 444 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 445 | mp = br_mdb_ip_get(br, group); |
| 446 | if (mp) |
| 447 | return mp; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 448 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 449 | if (atomic_read(&br->mdb_hash_tbl.nelems) >= br->hash_max) { |
| 450 | br_opt_toggle(br, BROPT_MULTICAST_ENABLED, false); |
| 451 | return ERR_PTR(-E2BIG); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | mp = kzalloc(sizeof(*mp), GFP_ATOMIC); |
| 455 | if (unlikely(!mp)) |
Tobias Klauser | 4c0833b | 2010-12-10 03:18:04 +0000 | [diff] [blame] | 456 | return ERR_PTR(-ENOMEM); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 457 | |
| 458 | mp->br = br; |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 459 | mp->addr = *group; |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 460 | timer_setup(&mp->timer, br_multicast_group_expired, 0); |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 461 | err = rhashtable_lookup_insert_fast(&br->mdb_hash_tbl, &mp->rhnode, |
| 462 | br_mdb_rht_params); |
| 463 | if (err) { |
| 464 | kfree(mp); |
| 465 | mp = ERR_PTR(err); |
| 466 | } else { |
| 467 | hlist_add_head_rcu(&mp->mdb_node, &br->mdb_list); |
| 468 | } |
Eric Dumazet | 1faabf2 | 2013-07-19 20:07:16 -0700 | [diff] [blame] | 469 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 470 | return mp; |
| 471 | } |
| 472 | |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 473 | struct net_bridge_port_group *br_multicast_new_port_group( |
| 474 | struct net_bridge_port *port, |
| 475 | struct br_ip *group, |
Amerigo Wang | ccb1c31 | 2012-12-14 22:09:51 +0000 | [diff] [blame] | 476 | struct net_bridge_port_group __rcu *next, |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 477 | unsigned char flags, |
| 478 | const unsigned char *src) |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 479 | { |
| 480 | struct net_bridge_port_group *p; |
| 481 | |
| 482 | p = kzalloc(sizeof(*p), GFP_ATOMIC); |
| 483 | if (unlikely(!p)) |
| 484 | return NULL; |
| 485 | |
| 486 | p->addr = *group; |
| 487 | p->port = port; |
Elad Raz | 9d06b6d | 2016-02-03 09:57:05 +0100 | [diff] [blame] | 488 | p->flags = flags; |
stephen hemminger | eca2a43 | 2012-12-13 06:51:28 +0000 | [diff] [blame] | 489 | rcu_assign_pointer(p->next, next); |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 490 | hlist_add_head(&p->mglist, &port->mglist); |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 491 | timer_setup(&p->timer, br_multicast_port_group_expired, 0); |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 492 | |
| 493 | if (src) |
| 494 | memcpy(p->eth_addr, src, ETH_ALEN); |
| 495 | else |
Mao Wenan | 1bfe45f | 2019-03-20 10:06:57 +0800 | [diff] [blame] | 496 | eth_broadcast_addr(p->eth_addr); |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 497 | |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 498 | return p; |
| 499 | } |
| 500 | |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 501 | static bool br_port_group_equal(struct net_bridge_port_group *p, |
| 502 | struct net_bridge_port *port, |
| 503 | const unsigned char *src) |
| 504 | { |
| 505 | if (p->port != port) |
| 506 | return false; |
| 507 | |
| 508 | if (!(port->flags & BR_MULTICAST_TO_UNICAST)) |
| 509 | return true; |
| 510 | |
| 511 | return ether_addr_equal(src, p->eth_addr); |
| 512 | } |
| 513 | |
Nikolay Aleksandrov | 1bc844e | 2019-08-17 14:22:13 +0300 | [diff] [blame] | 514 | void br_multicast_host_join(struct net_bridge_mdb_entry *mp, bool notify) |
| 515 | { |
| 516 | if (!mp->host_joined) { |
| 517 | mp->host_joined = true; |
| 518 | if (notify) |
| 519 | br_mdb_notify(mp->br->dev, NULL, &mp->addr, |
| 520 | RTM_NEWMDB, 0); |
| 521 | } |
| 522 | mod_timer(&mp->timer, jiffies + mp->br->multicast_membership_interval); |
| 523 | } |
| 524 | |
| 525 | void br_multicast_host_leave(struct net_bridge_mdb_entry *mp, bool notify) |
| 526 | { |
| 527 | if (!mp->host_joined) |
| 528 | return; |
| 529 | |
| 530 | mp->host_joined = false; |
| 531 | if (notify) |
| 532 | br_mdb_notify(mp->br->dev, NULL, &mp->addr, RTM_DELMDB, 0); |
| 533 | } |
| 534 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 535 | static int br_multicast_add_group(struct net_bridge *br, |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 536 | struct net_bridge_port *port, |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 537 | struct br_ip *group, |
| 538 | const unsigned char *src) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 539 | { |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 540 | struct net_bridge_port_group __rcu **pp; |
Nikolay Aleksandrov | 5e92358 | 2016-11-21 13:03:24 +0100 | [diff] [blame] | 541 | struct net_bridge_port_group *p; |
| 542 | struct net_bridge_mdb_entry *mp; |
Linus Lüssing | 454594f | 2013-10-20 00:58:57 +0200 | [diff] [blame] | 543 | unsigned long now = jiffies; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 544 | int err; |
| 545 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 546 | spin_lock(&br->multicast_lock); |
| 547 | if (!netif_running(br->dev) || |
| 548 | (port && port->state == BR_STATE_DISABLED)) |
| 549 | goto out; |
| 550 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 551 | mp = br_multicast_new_group(br, group); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 552 | err = PTR_ERR(mp); |
Tobias Klauser | 4c0833b | 2010-12-10 03:18:04 +0000 | [diff] [blame] | 553 | if (IS_ERR(mp)) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 554 | goto err; |
| 555 | |
| 556 | if (!port) { |
Nikolay Aleksandrov | 1bc844e | 2019-08-17 14:22:13 +0300 | [diff] [blame] | 557 | br_multicast_host_join(mp, true); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 558 | goto out; |
| 559 | } |
| 560 | |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 561 | for (pp = &mp->ports; |
| 562 | (p = mlock_dereference(*pp, br)) != NULL; |
| 563 | pp = &p->next) { |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 564 | if (br_port_group_equal(p, port, src)) |
Linus Lüssing | 454594f | 2013-10-20 00:58:57 +0200 | [diff] [blame] | 565 | goto found; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 566 | if ((unsigned long)p->port < (unsigned long)port) |
| 567 | break; |
| 568 | } |
| 569 | |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 570 | p = br_multicast_new_port_group(port, group, *pp, 0, src); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 571 | if (unlikely(!p)) |
| 572 | goto err; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 573 | rcu_assign_pointer(*pp, p); |
Elad Raz | 45ebcce | 2016-04-21 12:52:45 +0200 | [diff] [blame] | 574 | br_mdb_notify(br->dev, port, group, RTM_NEWMDB, 0); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 575 | |
Linus Lüssing | 454594f | 2013-10-20 00:58:57 +0200 | [diff] [blame] | 576 | found: |
| 577 | mod_timer(&p->timer, now + br->multicast_membership_interval); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 578 | out: |
| 579 | err = 0; |
| 580 | |
| 581 | err: |
| 582 | spin_unlock(&br->multicast_lock); |
| 583 | return err; |
| 584 | } |
| 585 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 586 | static int br_ip4_multicast_add_group(struct net_bridge *br, |
| 587 | struct net_bridge_port *port, |
Vlad Yasevich | b0e9a30 | 2013-02-13 12:00:17 +0000 | [diff] [blame] | 588 | __be32 group, |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 589 | __u16 vid, |
| 590 | const unsigned char *src) |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 591 | { |
| 592 | struct br_ip br_group; |
| 593 | |
| 594 | if (ipv4_is_local_multicast(group)) |
| 595 | return 0; |
| 596 | |
Nikolay Aleksandrov | 1515a63f | 2019-04-03 23:27:24 +0300 | [diff] [blame] | 597 | memset(&br_group, 0, sizeof(br_group)); |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 598 | br_group.u.ip4 = group; |
| 599 | br_group.proto = htons(ETH_P_IP); |
Vlad Yasevich | b0e9a30 | 2013-02-13 12:00:17 +0000 | [diff] [blame] | 600 | br_group.vid = vid; |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 601 | |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 602 | return br_multicast_add_group(br, port, &br_group, src); |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 603 | } |
| 604 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 605 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 606 | static int br_ip6_multicast_add_group(struct net_bridge *br, |
| 607 | struct net_bridge_port *port, |
Vlad Yasevich | b0e9a30 | 2013-02-13 12:00:17 +0000 | [diff] [blame] | 608 | const struct in6_addr *group, |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 609 | __u16 vid, |
| 610 | const unsigned char *src) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 611 | { |
| 612 | struct br_ip br_group; |
| 613 | |
Linus Lüssing | 3c3769e | 2013-09-04 02:13:39 +0200 | [diff] [blame] | 614 | if (ipv6_addr_is_ll_all_nodes(group)) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 615 | return 0; |
| 616 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 617 | memset(&br_group, 0, sizeof(br_group)); |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 618 | br_group.u.ip6 = *group; |
Linus Lüssing | 9cc6e0c | 2011-02-15 13:19:17 +0000 | [diff] [blame] | 619 | br_group.proto = htons(ETH_P_IPV6); |
Vlad Yasevich | b0e9a30 | 2013-02-13 12:00:17 +0000 | [diff] [blame] | 620 | br_group.vid = vid; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 621 | |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 622 | return br_multicast_add_group(br, port, &br_group, src); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 623 | } |
| 624 | #endif |
| 625 | |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 626 | static void br_multicast_router_expired(struct timer_list *t) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 627 | { |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 628 | struct net_bridge_port *port = |
| 629 | from_timer(port, t, multicast_router_timer); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 630 | struct net_bridge *br = port->br; |
| 631 | |
| 632 | spin_lock(&br->multicast_lock); |
Nikolay Aleksandrov | a55d824 | 2016-02-26 21:20:03 +0100 | [diff] [blame] | 633 | if (port->multicast_router == MDB_RTR_TYPE_DISABLED || |
| 634 | port->multicast_router == MDB_RTR_TYPE_PERM || |
Nogah Frankel | f12e7d9 | 2017-02-09 14:54:41 +0100 | [diff] [blame] | 635 | timer_pending(&port->multicast_router_timer)) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 636 | goto out; |
| 637 | |
Nogah Frankel | f12e7d9 | 2017-02-09 14:54:41 +0100 | [diff] [blame] | 638 | __del_port_router(port); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 639 | out: |
| 640 | spin_unlock(&br->multicast_lock); |
| 641 | } |
| 642 | |
Yotam Gigi | 7704142 | 2017-10-09 11:15:31 +0200 | [diff] [blame] | 643 | static void br_mc_router_state_change(struct net_bridge *p, |
| 644 | bool is_mc_router) |
| 645 | { |
| 646 | struct switchdev_attr attr = { |
| 647 | .orig_dev = p->dev, |
| 648 | .id = SWITCHDEV_ATTR_ID_BRIDGE_MROUTER, |
| 649 | .flags = SWITCHDEV_F_DEFER, |
| 650 | .u.mrouter = is_mc_router, |
| 651 | }; |
| 652 | |
| 653 | switchdev_port_attr_set(p->dev, &attr); |
| 654 | } |
| 655 | |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 656 | static void br_multicast_local_router_expired(struct timer_list *t) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 657 | { |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 658 | struct net_bridge *br = from_timer(br, t, multicast_router_timer); |
Yotam Gigi | 7704142 | 2017-10-09 11:15:31 +0200 | [diff] [blame] | 659 | |
| 660 | spin_lock(&br->multicast_lock); |
| 661 | if (br->multicast_router == MDB_RTR_TYPE_DISABLED || |
| 662 | br->multicast_router == MDB_RTR_TYPE_PERM || |
| 663 | timer_pending(&br->multicast_router_timer)) |
| 664 | goto out; |
| 665 | |
| 666 | br_mc_router_state_change(br, false); |
| 667 | out: |
| 668 | spin_unlock(&br->multicast_lock); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 671 | static void br_multicast_querier_expired(struct net_bridge *br, |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 672 | struct bridge_mcast_own_query *query) |
Herbert Xu | c83b8fa | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 673 | { |
Herbert Xu | c83b8fa | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 674 | spin_lock(&br->multicast_lock); |
Nikolay Aleksandrov | 13cefad | 2018-09-26 17:01:03 +0300 | [diff] [blame] | 675 | if (!netif_running(br->dev) || !br_opt_get(br, BROPT_MULTICAST_ENABLED)) |
Herbert Xu | c83b8fa | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 676 | goto out; |
| 677 | |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 678 | br_multicast_start_querier(br, query); |
Herbert Xu | c83b8fa | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 679 | |
| 680 | out: |
| 681 | spin_unlock(&br->multicast_lock); |
| 682 | } |
| 683 | |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 684 | static void br_ip4_multicast_querier_expired(struct timer_list *t) |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 685 | { |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 686 | struct net_bridge *br = from_timer(br, t, ip4_other_query.timer); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 687 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 688 | br_multicast_querier_expired(br, &br->ip4_own_query); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | #if IS_ENABLED(CONFIG_IPV6) |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 692 | static void br_ip6_multicast_querier_expired(struct timer_list *t) |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 693 | { |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 694 | struct net_bridge *br = from_timer(br, t, ip6_other_query.timer); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 695 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 696 | br_multicast_querier_expired(br, &br->ip6_own_query); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 697 | } |
| 698 | #endif |
| 699 | |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 700 | static void br_multicast_select_own_querier(struct net_bridge *br, |
| 701 | struct br_ip *ip, |
| 702 | struct sk_buff *skb) |
| 703 | { |
| 704 | if (ip->proto == htons(ETH_P_IP)) |
| 705 | br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr; |
| 706 | #if IS_ENABLED(CONFIG_IPV6) |
| 707 | else |
| 708 | br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr; |
| 709 | #endif |
| 710 | } |
| 711 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 712 | static void __br_multicast_send_query(struct net_bridge *br, |
| 713 | struct net_bridge_port *port, |
| 714 | struct br_ip *ip) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 715 | { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 716 | struct sk_buff *skb; |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 717 | u8 igmp_type; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 718 | |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 719 | skb = br_multicast_alloc_query(br, ip, &igmp_type); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 720 | if (!skb) |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 721 | return; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 722 | |
| 723 | if (port) { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 724 | skb->dev = port->dev; |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 725 | br_multicast_count(br, port, skb, igmp_type, |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 726 | BR_MCAST_DIR_TX); |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 727 | NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, |
| 728 | dev_net(port->dev), NULL, skb, NULL, skb->dev, |
Linus Lüssing | f0b4eec | 2014-11-17 12:20:28 +0100 | [diff] [blame] | 729 | br_dev_queue_push_xmit); |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 730 | } else { |
| 731 | br_multicast_select_own_querier(br, ip, skb); |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 732 | br_multicast_count(br, port, skb, igmp_type, |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 733 | BR_MCAST_DIR_RX); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 734 | netif_rx(skb); |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 735 | } |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 736 | } |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 737 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 738 | static void br_multicast_send_query(struct net_bridge *br, |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 739 | struct net_bridge_port *port, |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 740 | struct bridge_mcast_own_query *own_query) |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 741 | { |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 742 | struct bridge_mcast_other_query *other_query = NULL; |
Nikolay Aleksandrov | 5e92358 | 2016-11-21 13:03:24 +0100 | [diff] [blame] | 743 | struct br_ip br_group; |
| 744 | unsigned long time; |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 745 | |
Nikolay Aleksandrov | 13cefad | 2018-09-26 17:01:03 +0300 | [diff] [blame] | 746 | if (!netif_running(br->dev) || |
| 747 | !br_opt_get(br, BROPT_MULTICAST_ENABLED) || |
Nikolay Aleksandrov | 675779a | 2018-09-26 17:01:04 +0300 | [diff] [blame] | 748 | !br_opt_get(br, BROPT_MULTICAST_QUERIER)) |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 749 | return; |
| 750 | |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 751 | memset(&br_group.u, 0, sizeof(br_group.u)); |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 752 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 753 | if (port ? (own_query == &port->ip4_own_query) : |
| 754 | (own_query == &br->ip4_own_query)) { |
| 755 | other_query = &br->ip4_other_query; |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 756 | br_group.proto = htons(ETH_P_IP); |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 757 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 758 | } else { |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 759 | other_query = &br->ip6_other_query; |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 760 | br_group.proto = htons(ETH_P_IPV6); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 761 | #endif |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 762 | } |
| 763 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 764 | if (!other_query || timer_pending(&other_query->timer)) |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 765 | return; |
| 766 | |
| 767 | __br_multicast_send_query(br, port, &br_group); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 768 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 769 | time = jiffies; |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 770 | time += own_query->startup_sent < br->multicast_startup_query_count ? |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 771 | br->multicast_startup_query_interval : |
| 772 | br->multicast_query_interval; |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 773 | mod_timer(&own_query->timer, time); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 776 | static void |
| 777 | br_multicast_port_query_expired(struct net_bridge_port *port, |
| 778 | struct bridge_mcast_own_query *query) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 779 | { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 780 | struct net_bridge *br = port->br; |
| 781 | |
| 782 | spin_lock(&br->multicast_lock); |
Dan Carpenter | 02a780c | 2010-03-06 01:14:09 +0000 | [diff] [blame] | 783 | if (port->state == BR_STATE_DISABLED || |
| 784 | port->state == BR_STATE_BLOCKING) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 785 | goto out; |
| 786 | |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 787 | if (query->startup_sent < br->multicast_startup_query_count) |
| 788 | query->startup_sent++; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 789 | |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 790 | br_multicast_send_query(port->br, port, query); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 791 | |
| 792 | out: |
| 793 | spin_unlock(&br->multicast_lock); |
| 794 | } |
| 795 | |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 796 | static void br_ip4_multicast_port_query_expired(struct timer_list *t) |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 797 | { |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 798 | struct net_bridge_port *port = from_timer(port, t, ip4_own_query.timer); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 799 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 800 | br_multicast_port_query_expired(port, &port->ip4_own_query); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | #if IS_ENABLED(CONFIG_IPV6) |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 804 | static void br_ip6_multicast_port_query_expired(struct timer_list *t) |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 805 | { |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 806 | struct net_bridge_port *port = from_timer(port, t, ip6_own_query.timer); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 807 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 808 | br_multicast_port_query_expired(port, &port->ip6_own_query); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 809 | } |
| 810 | #endif |
| 811 | |
Nogah Frankel | 147c1e9 | 2017-02-09 14:54:40 +0100 | [diff] [blame] | 812 | static void br_mc_disabled_update(struct net_device *dev, bool value) |
| 813 | { |
| 814 | struct switchdev_attr attr = { |
| 815 | .orig_dev = dev, |
| 816 | .id = SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED, |
| 817 | .flags = SWITCHDEV_F_DEFER, |
Nikolay Aleksandrov | 13cefad | 2018-09-26 17:01:03 +0300 | [diff] [blame] | 818 | .u.mc_disabled = !value, |
Nogah Frankel | 147c1e9 | 2017-02-09 14:54:40 +0100 | [diff] [blame] | 819 | }; |
| 820 | |
| 821 | switchdev_port_attr_set(dev, &attr); |
| 822 | } |
| 823 | |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 824 | int br_multicast_add_port(struct net_bridge_port *port) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 825 | { |
Nikolay Aleksandrov | 7f0aec7 | 2016-02-26 21:20:01 +0100 | [diff] [blame] | 826 | port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 827 | |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 828 | timer_setup(&port->multicast_router_timer, |
| 829 | br_multicast_router_expired, 0); |
| 830 | timer_setup(&port->ip4_own_query.timer, |
| 831 | br_ip4_multicast_port_query_expired, 0); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 832 | #if IS_ENABLED(CONFIG_IPV6) |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 833 | timer_setup(&port->ip6_own_query.timer, |
| 834 | br_ip6_multicast_port_query_expired, 0); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 835 | #endif |
Nikolay Aleksandrov | 13cefad | 2018-09-26 17:01:03 +0300 | [diff] [blame] | 836 | br_mc_disabled_update(port->dev, |
| 837 | br_opt_get(port->br, BROPT_MULTICAST_ENABLED)); |
Nogah Frankel | 147c1e9 | 2017-02-09 14:54:40 +0100 | [diff] [blame] | 838 | |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 839 | port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats); |
| 840 | if (!port->mcast_stats) |
| 841 | return -ENOMEM; |
| 842 | |
| 843 | return 0; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | void br_multicast_del_port(struct net_bridge_port *port) |
| 847 | { |
Satish Ashok | e10177a | 2015-07-15 07:16:51 -0700 | [diff] [blame] | 848 | struct net_bridge *br = port->br; |
| 849 | struct net_bridge_port_group *pg; |
| 850 | struct hlist_node *n; |
| 851 | |
| 852 | /* Take care of the remaining groups, only perm ones should be left */ |
| 853 | spin_lock_bh(&br->multicast_lock); |
| 854 | hlist_for_each_entry_safe(pg, n, &port->mglist, mglist) |
| 855 | br_multicast_del_pg(br, pg); |
| 856 | spin_unlock_bh(&br->multicast_lock); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 857 | del_timer_sync(&port->multicast_router_timer); |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 858 | free_percpu(port->mcast_stats); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 861 | static void br_multicast_enable(struct bridge_mcast_own_query *query) |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 862 | { |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 863 | query->startup_sent = 0; |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 864 | |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 865 | if (try_to_del_timer_sync(&query->timer) >= 0 || |
| 866 | del_timer(&query->timer)) |
| 867 | mod_timer(&query->timer, jiffies); |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Nikolay Aleksandrov | 7cb3f92 | 2016-10-18 18:09:48 +0200 | [diff] [blame] | 870 | static void __br_multicast_enable_port(struct net_bridge_port *port) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 871 | { |
| 872 | struct net_bridge *br = port->br; |
| 873 | |
Nikolay Aleksandrov | 13cefad | 2018-09-26 17:01:03 +0300 | [diff] [blame] | 874 | if (!br_opt_get(br, BROPT_MULTICAST_ENABLED) || !netif_running(br->dev)) |
Nikolay Aleksandrov | 7cb3f92 | 2016-10-18 18:09:48 +0200 | [diff] [blame] | 875 | return; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 876 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 877 | br_multicast_enable(&port->ip4_own_query); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 878 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 879 | br_multicast_enable(&port->ip6_own_query); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 880 | #endif |
Nikolay Aleksandrov | 7f0aec7 | 2016-02-26 21:20:01 +0100 | [diff] [blame] | 881 | if (port->multicast_router == MDB_RTR_TYPE_PERM && |
| 882 | hlist_unhashed(&port->rlist)) |
Satish Ashok | 754bc54 | 2015-06-19 01:22:57 -0700 | [diff] [blame] | 883 | br_multicast_add_router(br, port); |
Nikolay Aleksandrov | 7cb3f92 | 2016-10-18 18:09:48 +0200 | [diff] [blame] | 884 | } |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 885 | |
Nikolay Aleksandrov | 7cb3f92 | 2016-10-18 18:09:48 +0200 | [diff] [blame] | 886 | void br_multicast_enable_port(struct net_bridge_port *port) |
| 887 | { |
| 888 | struct net_bridge *br = port->br; |
| 889 | |
| 890 | spin_lock(&br->multicast_lock); |
| 891 | __br_multicast_enable_port(port); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 892 | spin_unlock(&br->multicast_lock); |
| 893 | } |
| 894 | |
| 895 | void br_multicast_disable_port(struct net_bridge_port *port) |
| 896 | { |
| 897 | struct net_bridge *br = port->br; |
| 898 | struct net_bridge_port_group *pg; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 899 | struct hlist_node *n; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 900 | |
| 901 | spin_lock(&br->multicast_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 902 | hlist_for_each_entry_safe(pg, n, &port->mglist, mglist) |
Elad Raz | 9d06b6d | 2016-02-03 09:57:05 +0100 | [diff] [blame] | 903 | if (!(pg->flags & MDB_PG_FLAGS_PERMANENT)) |
Satish Ashok | e10177a | 2015-07-15 07:16:51 -0700 | [diff] [blame] | 904 | br_multicast_del_pg(br, pg); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 905 | |
Nogah Frankel | f12e7d9 | 2017-02-09 14:54:41 +0100 | [diff] [blame] | 906 | __del_port_router(port); |
| 907 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 908 | del_timer(&port->multicast_router_timer); |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 909 | del_timer(&port->ip4_own_query.timer); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 910 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 911 | del_timer(&port->ip6_own_query.timer); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 912 | #endif |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 913 | spin_unlock(&br->multicast_lock); |
| 914 | } |
| 915 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 916 | static int br_ip4_multicast_igmp3_report(struct net_bridge *br, |
| 917 | struct net_bridge_port *port, |
Vlad Yasevich | 0649909 | 2013-10-28 15:45:07 -0400 | [diff] [blame] | 918 | struct sk_buff *skb, |
| 919 | u16 vid) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 920 | { |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 921 | const unsigned char *src; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 922 | struct igmpv3_report *ih; |
| 923 | struct igmpv3_grec *grec; |
| 924 | int i; |
| 925 | int len; |
| 926 | int num; |
| 927 | int type; |
| 928 | int err = 0; |
| 929 | __be32 group; |
Nikolay Aleksandrov | e57f618 | 2019-07-02 15:00:18 +0300 | [diff] [blame] | 930 | u16 nsrcs; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 931 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 932 | ih = igmpv3_report_hdr(skb); |
| 933 | num = ntohs(ih->ngrec); |
Linus Lüssing | c2d4fbd | 2015-09-11 18:39:48 +0200 | [diff] [blame] | 934 | len = skb_transport_offset(skb) + sizeof(*ih); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 935 | |
| 936 | for (i = 0; i < num; i++) { |
| 937 | len += sizeof(*grec); |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 938 | if (!ip_mc_may_pull(skb, len)) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 939 | return -EINVAL; |
| 940 | |
Herbert Xu | fd218cf | 2010-04-07 21:20:47 -0700 | [diff] [blame] | 941 | grec = (void *)(skb->data + len - sizeof(*grec)); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 942 | group = grec->grec_mca; |
| 943 | type = grec->grec_type; |
Nikolay Aleksandrov | e57f618 | 2019-07-02 15:00:18 +0300 | [diff] [blame] | 944 | nsrcs = ntohs(grec->grec_nsrcs); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 945 | |
Nikolay Aleksandrov | e57f618 | 2019-07-02 15:00:18 +0300 | [diff] [blame] | 946 | len += nsrcs * 4; |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 947 | if (!ip_mc_may_pull(skb, len)) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 948 | return -EINVAL; |
| 949 | |
| 950 | /* We treat this as an IGMPv2 report for now. */ |
| 951 | switch (type) { |
| 952 | case IGMPV3_MODE_IS_INCLUDE: |
| 953 | case IGMPV3_MODE_IS_EXCLUDE: |
| 954 | case IGMPV3_CHANGE_TO_INCLUDE: |
| 955 | case IGMPV3_CHANGE_TO_EXCLUDE: |
| 956 | case IGMPV3_ALLOW_NEW_SOURCES: |
| 957 | case IGMPV3_BLOCK_OLD_SOURCES: |
| 958 | break; |
| 959 | |
| 960 | default: |
| 961 | continue; |
| 962 | } |
| 963 | |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 964 | src = eth_hdr(skb)->h_source; |
Satish Ashok | bc8c20a | 2015-07-13 05:28:37 -0700 | [diff] [blame] | 965 | if ((type == IGMPV3_CHANGE_TO_INCLUDE || |
| 966 | type == IGMPV3_MODE_IS_INCLUDE) && |
Nikolay Aleksandrov | e57f618 | 2019-07-02 15:00:18 +0300 | [diff] [blame] | 967 | nsrcs == 0) { |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 968 | br_ip4_multicast_leave_group(br, port, group, vid, src); |
Satish Ashok | bc8c20a | 2015-07-13 05:28:37 -0700 | [diff] [blame] | 969 | } else { |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 970 | err = br_ip4_multicast_add_group(br, port, group, vid, |
| 971 | src); |
Satish Ashok | bc8c20a | 2015-07-13 05:28:37 -0700 | [diff] [blame] | 972 | if (err) |
| 973 | break; |
| 974 | } |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | return err; |
| 978 | } |
| 979 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 980 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 981 | static int br_ip6_multicast_mld2_report(struct net_bridge *br, |
| 982 | struct net_bridge_port *port, |
Vlad Yasevich | 0649909 | 2013-10-28 15:45:07 -0400 | [diff] [blame] | 983 | struct sk_buff *skb, |
| 984 | u16 vid) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 985 | { |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 986 | unsigned int nsrcs_offset; |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 987 | const unsigned char *src; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 988 | struct icmp6hdr *icmp6h; |
| 989 | struct mld2_grec *grec; |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 990 | unsigned int grec_len; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 991 | int i; |
| 992 | int len; |
| 993 | int num; |
| 994 | int err = 0; |
| 995 | |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 996 | if (!ipv6_mc_may_pull(skb, sizeof(*icmp6h))) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 997 | return -EINVAL; |
| 998 | |
| 999 | icmp6h = icmp6_hdr(skb); |
| 1000 | num = ntohs(icmp6h->icmp6_dataun.un_data16[1]); |
Linus Lüssing | c2d4fbd | 2015-09-11 18:39:48 +0200 | [diff] [blame] | 1001 | len = skb_transport_offset(skb) + sizeof(*icmp6h); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1002 | |
| 1003 | for (i = 0; i < num; i++) { |
Nikolay Aleksandrov | e57f618 | 2019-07-02 15:00:18 +0300 | [diff] [blame] | 1004 | __be16 *_nsrcs, __nsrcs; |
| 1005 | u16 nsrcs; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1006 | |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1007 | nsrcs_offset = len + offsetof(struct mld2_grec, grec_nsrcs); |
| 1008 | |
| 1009 | if (skb_transport_offset(skb) + ipv6_transport_len(skb) < |
| 1010 | nsrcs_offset + sizeof(_nsrcs)) |
| 1011 | return -EINVAL; |
| 1012 | |
Nikolay Aleksandrov | e57f618 | 2019-07-02 15:00:18 +0300 | [diff] [blame] | 1013 | _nsrcs = skb_header_pointer(skb, nsrcs_offset, |
| 1014 | sizeof(__nsrcs), &__nsrcs); |
| 1015 | if (!_nsrcs) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1016 | return -EINVAL; |
| 1017 | |
Nikolay Aleksandrov | e57f618 | 2019-07-02 15:00:18 +0300 | [diff] [blame] | 1018 | nsrcs = ntohs(*_nsrcs); |
| 1019 | grec_len = struct_size(grec, grec_src, nsrcs); |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1020 | |
| 1021 | if (!ipv6_mc_may_pull(skb, len + grec_len)) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1022 | return -EINVAL; |
| 1023 | |
| 1024 | grec = (struct mld2_grec *)(skb->data + len); |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1025 | len += grec_len; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1026 | |
| 1027 | /* We treat these as MLDv1 reports for now. */ |
| 1028 | switch (grec->grec_type) { |
| 1029 | case MLD2_MODE_IS_INCLUDE: |
| 1030 | case MLD2_MODE_IS_EXCLUDE: |
| 1031 | case MLD2_CHANGE_TO_INCLUDE: |
| 1032 | case MLD2_CHANGE_TO_EXCLUDE: |
| 1033 | case MLD2_ALLOW_NEW_SOURCES: |
| 1034 | case MLD2_BLOCK_OLD_SOURCES: |
| 1035 | break; |
| 1036 | |
| 1037 | default: |
| 1038 | continue; |
| 1039 | } |
| 1040 | |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1041 | src = eth_hdr(skb)->h_source; |
Satish Ashok | bc8c20a | 2015-07-13 05:28:37 -0700 | [diff] [blame] | 1042 | if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE || |
| 1043 | grec->grec_type == MLD2_MODE_IS_INCLUDE) && |
Nikolay Aleksandrov | e57f618 | 2019-07-02 15:00:18 +0300 | [diff] [blame] | 1044 | nsrcs == 0) { |
Satish Ashok | bc8c20a | 2015-07-13 05:28:37 -0700 | [diff] [blame] | 1045 | br_ip6_multicast_leave_group(br, port, &grec->grec_mca, |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1046 | vid, src); |
Satish Ashok | bc8c20a | 2015-07-13 05:28:37 -0700 | [diff] [blame] | 1047 | } else { |
| 1048 | err = br_ip6_multicast_add_group(br, port, |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1049 | &grec->grec_mca, vid, |
| 1050 | src); |
Davide Caratti | 9264251 | 2016-08-31 14:16:44 +0200 | [diff] [blame] | 1051 | if (err) |
Satish Ashok | bc8c20a | 2015-07-13 05:28:37 -0700 | [diff] [blame] | 1052 | break; |
| 1053 | } |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | return err; |
| 1057 | } |
| 1058 | #endif |
| 1059 | |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1060 | static bool br_ip4_multicast_select_querier(struct net_bridge *br, |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 1061 | struct net_bridge_port *port, |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1062 | __be32 saddr) |
| 1063 | { |
| 1064 | if (!timer_pending(&br->ip4_own_query.timer) && |
| 1065 | !timer_pending(&br->ip4_other_query.timer)) |
| 1066 | goto update; |
| 1067 | |
| 1068 | if (!br->ip4_querier.addr.u.ip4) |
| 1069 | goto update; |
| 1070 | |
| 1071 | if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4)) |
| 1072 | goto update; |
| 1073 | |
| 1074 | return false; |
| 1075 | |
| 1076 | update: |
| 1077 | br->ip4_querier.addr.u.ip4 = saddr; |
| 1078 | |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 1079 | /* update protected by general multicast_lock by caller */ |
| 1080 | rcu_assign_pointer(br->ip4_querier.port, port); |
| 1081 | |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1082 | return true; |
| 1083 | } |
| 1084 | |
| 1085 | #if IS_ENABLED(CONFIG_IPV6) |
| 1086 | static bool br_ip6_multicast_select_querier(struct net_bridge *br, |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 1087 | struct net_bridge_port *port, |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1088 | struct in6_addr *saddr) |
| 1089 | { |
| 1090 | if (!timer_pending(&br->ip6_own_query.timer) && |
| 1091 | !timer_pending(&br->ip6_other_query.timer)) |
| 1092 | goto update; |
| 1093 | |
| 1094 | if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0) |
| 1095 | goto update; |
| 1096 | |
| 1097 | return false; |
| 1098 | |
| 1099 | update: |
| 1100 | br->ip6_querier.addr.u.ip6 = *saddr; |
| 1101 | |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 1102 | /* update protected by general multicast_lock by caller */ |
| 1103 | rcu_assign_pointer(br->ip6_querier.port, port); |
| 1104 | |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1105 | return true; |
| 1106 | } |
| 1107 | #endif |
| 1108 | |
| 1109 | static bool br_multicast_select_querier(struct net_bridge *br, |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 1110 | struct net_bridge_port *port, |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1111 | struct br_ip *saddr) |
| 1112 | { |
| 1113 | switch (saddr->proto) { |
| 1114 | case htons(ETH_P_IP): |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 1115 | return br_ip4_multicast_select_querier(br, port, saddr->u.ip4); |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1116 | #if IS_ENABLED(CONFIG_IPV6) |
| 1117 | case htons(ETH_P_IPV6): |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 1118 | return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6); |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1119 | #endif |
| 1120 | } |
| 1121 | |
| 1122 | return false; |
| 1123 | } |
| 1124 | |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1125 | static void |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1126 | br_multicast_update_query_timer(struct net_bridge *br, |
| 1127 | struct bridge_mcast_other_query *query, |
| 1128 | unsigned long max_delay) |
Linus Lüssing | b00589a | 2013-08-01 01:06:20 +0200 | [diff] [blame] | 1129 | { |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1130 | if (!timer_pending(&query->timer)) |
| 1131 | query->delay_time = jiffies + max_delay; |
Linus Lüssing | b00589a | 2013-08-01 01:06:20 +0200 | [diff] [blame] | 1132 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1133 | mod_timer(&query->timer, jiffies + br->multicast_querier_interval); |
Linus Lüssing | b00589a | 2013-08-01 01:06:20 +0200 | [diff] [blame] | 1134 | } |
| 1135 | |
Nogah Frankel | 6d54964 | 2017-02-09 14:54:42 +0100 | [diff] [blame] | 1136 | static void br_port_mc_router_state_change(struct net_bridge_port *p, |
| 1137 | bool is_mc_router) |
| 1138 | { |
| 1139 | struct switchdev_attr attr = { |
| 1140 | .orig_dev = p->dev, |
| 1141 | .id = SWITCHDEV_ATTR_ID_PORT_MROUTER, |
| 1142 | .flags = SWITCHDEV_F_DEFER, |
| 1143 | .u.mrouter = is_mc_router, |
| 1144 | }; |
| 1145 | |
| 1146 | switchdev_port_attr_set(p->dev, &attr); |
| 1147 | } |
| 1148 | |
stephen hemminger | 7e80c12 | 2010-04-27 15:01:04 +0000 | [diff] [blame] | 1149 | /* |
Cong Wang | 7c77602 | 2013-06-21 15:37:25 +0800 | [diff] [blame] | 1150 | * Add port to router_list |
stephen hemminger | 7e80c12 | 2010-04-27 15:01:04 +0000 | [diff] [blame] | 1151 | * list is maintained ordered by pointer value |
| 1152 | * and locked by br->multicast_lock and RCU |
| 1153 | */ |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1154 | static void br_multicast_add_router(struct net_bridge *br, |
| 1155 | struct net_bridge_port *port) |
| 1156 | { |
stephen hemminger | dcdca2c | 2010-04-27 07:13:11 +0000 | [diff] [blame] | 1157 | struct net_bridge_port *p; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1158 | struct hlist_node *slot = NULL; |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1159 | |
Nikolay Aleksandrov | 1a040ea | 2015-06-09 10:23:57 -0700 | [diff] [blame] | 1160 | if (!hlist_unhashed(&port->rlist)) |
| 1161 | return; |
| 1162 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1163 | hlist_for_each_entry(p, &br->router_list, rlist) { |
stephen hemminger | 7e80c12 | 2010-04-27 15:01:04 +0000 | [diff] [blame] | 1164 | if ((unsigned long) port >= (unsigned long) p) |
| 1165 | break; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1166 | slot = &p->rlist; |
stephen hemminger | dcdca2c | 2010-04-27 07:13:11 +0000 | [diff] [blame] | 1167 | } |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1168 | |
stephen hemminger | 7e80c12 | 2010-04-27 15:01:04 +0000 | [diff] [blame] | 1169 | if (slot) |
Ken Helias | 1d02328 | 2014-08-06 16:09:16 -0700 | [diff] [blame] | 1170 | hlist_add_behind_rcu(&port->rlist, slot); |
stephen hemminger | dcdca2c | 2010-04-27 07:13:11 +0000 | [diff] [blame] | 1171 | else |
| 1172 | hlist_add_head_rcu(&port->rlist, &br->router_list); |
Satish Ashok | 949f1e3 | 2015-07-23 05:00:53 -0700 | [diff] [blame] | 1173 | br_rtr_notify(br->dev, port, RTM_NEWMDB); |
Nogah Frankel | 6d54964 | 2017-02-09 14:54:42 +0100 | [diff] [blame] | 1174 | br_port_mc_router_state_change(port, true); |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1177 | static void br_multicast_mark_router(struct net_bridge *br, |
| 1178 | struct net_bridge_port *port) |
| 1179 | { |
| 1180 | unsigned long now = jiffies; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1181 | |
| 1182 | if (!port) { |
Yotam Gigi | 7704142 | 2017-10-09 11:15:31 +0200 | [diff] [blame] | 1183 | if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) { |
| 1184 | if (!timer_pending(&br->multicast_router_timer)) |
| 1185 | br_mc_router_state_change(br, true); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1186 | mod_timer(&br->multicast_router_timer, |
| 1187 | now + br->multicast_querier_interval); |
Yotam Gigi | 7704142 | 2017-10-09 11:15:31 +0200 | [diff] [blame] | 1188 | } |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1189 | return; |
| 1190 | } |
| 1191 | |
Nikolay Aleksandrov | a55d824 | 2016-02-26 21:20:03 +0100 | [diff] [blame] | 1192 | if (port->multicast_router == MDB_RTR_TYPE_DISABLED || |
| 1193 | port->multicast_router == MDB_RTR_TYPE_PERM) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1194 | return; |
| 1195 | |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1196 | br_multicast_add_router(br, port); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1197 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1198 | mod_timer(&port->multicast_router_timer, |
| 1199 | now + br->multicast_querier_interval); |
| 1200 | } |
| 1201 | |
| 1202 | static void br_multicast_query_received(struct net_bridge *br, |
| 1203 | struct net_bridge_port *port, |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1204 | struct bridge_mcast_other_query *query, |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1205 | struct br_ip *saddr, |
Linus Lüssing | b00589a | 2013-08-01 01:06:20 +0200 | [diff] [blame] | 1206 | unsigned long max_delay) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1207 | { |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 1208 | if (!br_multicast_select_querier(br, port, saddr)) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1209 | return; |
| 1210 | |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1211 | br_multicast_update_query_timer(br, query, max_delay); |
Hangbin Liu | 278e214 | 2019-02-22 21:22:32 +0800 | [diff] [blame] | 1212 | br_multicast_mark_router(br, port); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
zhong jiang | 9c2e955 | 2018-08-06 11:07:23 +0800 | [diff] [blame] | 1215 | static void br_ip4_multicast_query(struct net_bridge *br, |
| 1216 | struct net_bridge_port *port, |
| 1217 | struct sk_buff *skb, |
| 1218 | u16 vid) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1219 | { |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1220 | unsigned int transport_len = ip_transport_len(skb); |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1221 | const struct iphdr *iph = ip_hdr(skb); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1222 | struct igmphdr *ih = igmp_hdr(skb); |
| 1223 | struct net_bridge_mdb_entry *mp; |
| 1224 | struct igmpv3_query *ih3; |
| 1225 | struct net_bridge_port_group *p; |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 1226 | struct net_bridge_port_group __rcu **pp; |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1227 | struct br_ip saddr; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1228 | unsigned long max_delay; |
| 1229 | unsigned long now = jiffies; |
| 1230 | __be32 group; |
| 1231 | |
| 1232 | spin_lock(&br->multicast_lock); |
| 1233 | if (!netif_running(br->dev) || |
| 1234 | (port && port->state == BR_STATE_DISABLED)) |
| 1235 | goto out; |
| 1236 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1237 | group = ih->group; |
| 1238 | |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1239 | if (transport_len == sizeof(*ih)) { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1240 | max_delay = ih->code * (HZ / IGMP_TIMER_SCALE); |
| 1241 | |
| 1242 | if (!max_delay) { |
| 1243 | max_delay = 10 * HZ; |
| 1244 | group = 0; |
| 1245 | } |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1246 | } else if (transport_len >= sizeof(*ih3)) { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1247 | ih3 = igmpv3_query_hdr(skb); |
| 1248 | if (ih3->nsrcs) |
YOSHIFUJI Hideaki | bec68ff | 2010-03-13 12:27:21 -0800 | [diff] [blame] | 1249 | goto out; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1250 | |
YOSHIFUJI Hideaki / 吉藤英明 | 0ba8c9e | 2010-03-15 19:27:00 +0000 | [diff] [blame] | 1251 | max_delay = ih3->code ? |
| 1252 | IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1; |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 1253 | } else { |
Linus Lüssing | 9ed973c | 2014-03-10 22:25:24 +0100 | [diff] [blame] | 1254 | goto out; |
| 1255 | } |
| 1256 | |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1257 | if (!group) { |
| 1258 | saddr.proto = htons(ETH_P_IP); |
| 1259 | saddr.u.ip4 = iph->saddr; |
Linus Lüssing | b00589a | 2013-08-01 01:06:20 +0200 | [diff] [blame] | 1260 | |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1261 | br_multicast_query_received(br, port, &br->ip4_other_query, |
| 1262 | &saddr, max_delay); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1263 | goto out; |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1264 | } |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1265 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 1266 | mp = br_mdb_ip4_get(br, group, vid); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1267 | if (!mp) |
| 1268 | goto out; |
| 1269 | |
| 1270 | max_delay *= br->multicast_last_member_count; |
| 1271 | |
Andrew Lunn | ff0fd34 | 2017-11-09 23:10:57 +0100 | [diff] [blame] | 1272 | if (mp->host_joined && |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1273 | (timer_pending(&mp->timer) ? |
| 1274 | time_after(mp->timer.expires, now + max_delay) : |
| 1275 | try_to_del_timer_sync(&mp->timer) >= 0)) |
| 1276 | mod_timer(&mp->timer, now + max_delay); |
| 1277 | |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 1278 | for (pp = &mp->ports; |
| 1279 | (p = mlock_dereference(*pp, br)) != NULL; |
| 1280 | pp = &p->next) { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1281 | if (timer_pending(&p->timer) ? |
| 1282 | time_after(p->timer.expires, now + max_delay) : |
| 1283 | try_to_del_timer_sync(&p->timer) >= 0) |
Herbert Xu | 24f9cdc | 2011-02-11 12:42:07 +0000 | [diff] [blame] | 1284 | mod_timer(&p->timer, now + max_delay); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | out: |
| 1288 | spin_unlock(&br->multicast_lock); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 1291 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1292 | static int br_ip6_multicast_query(struct net_bridge *br, |
| 1293 | struct net_bridge_port *port, |
Vlad Yasevich | 0649909 | 2013-10-28 15:45:07 -0400 | [diff] [blame] | 1294 | struct sk_buff *skb, |
| 1295 | u16 vid) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1296 | { |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1297 | unsigned int transport_len = ipv6_transport_len(skb); |
stephen hemminger | eca2a43 | 2012-12-13 06:51:28 +0000 | [diff] [blame] | 1298 | struct mld_msg *mld; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1299 | struct net_bridge_mdb_entry *mp; |
| 1300 | struct mld2_query *mld2q; |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 1301 | struct net_bridge_port_group *p; |
| 1302 | struct net_bridge_port_group __rcu **pp; |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1303 | struct br_ip saddr; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1304 | unsigned long max_delay; |
| 1305 | unsigned long now = jiffies; |
Linus Lüssing | 856ce5d | 2016-05-04 17:25:02 +0200 | [diff] [blame] | 1306 | unsigned int offset = skb_transport_offset(skb); |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1307 | const struct in6_addr *group = NULL; |
Linus Lüssing | 9ed973c | 2014-03-10 22:25:24 +0100 | [diff] [blame] | 1308 | bool is_general_query; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1309 | int err = 0; |
| 1310 | |
| 1311 | spin_lock(&br->multicast_lock); |
| 1312 | if (!netif_running(br->dev) || |
| 1313 | (port && port->state == BR_STATE_DISABLED)) |
| 1314 | goto out; |
| 1315 | |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1316 | if (transport_len == sizeof(*mld)) { |
Linus Lüssing | 856ce5d | 2016-05-04 17:25:02 +0200 | [diff] [blame] | 1317 | if (!pskb_may_pull(skb, offset + sizeof(*mld))) { |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1318 | err = -EINVAL; |
| 1319 | goto out; |
| 1320 | } |
| 1321 | mld = (struct mld_msg *) icmp6_hdr(skb); |
Li RongQing | 4715213 | 2012-07-09 23:56:12 +0000 | [diff] [blame] | 1322 | max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay)); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1323 | if (max_delay) |
| 1324 | group = &mld->mld_mca; |
Linus Lüssing | 248ba8e | 2013-08-06 00:32:05 +0200 | [diff] [blame] | 1325 | } else { |
Linus Lüssing | 856ce5d | 2016-05-04 17:25:02 +0200 | [diff] [blame] | 1326 | if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) { |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1327 | err = -EINVAL; |
| 1328 | goto out; |
| 1329 | } |
| 1330 | mld2q = (struct mld2_query *)icmp6_hdr(skb); |
| 1331 | if (!mld2q->mld2q_nsrcs) |
| 1332 | group = &mld2q->mld2q_mca; |
Daniel Borkmann | e3f5b17 | 2013-09-04 00:19:39 +0200 | [diff] [blame] | 1333 | |
| 1334 | max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1335 | } |
| 1336 | |
Linus Lüssing | 9ed973c | 2014-03-10 22:25:24 +0100 | [diff] [blame] | 1337 | is_general_query = group && ipv6_addr_any(group); |
| 1338 | |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1339 | if (is_general_query) { |
| 1340 | saddr.proto = htons(ETH_P_IPV6); |
Nikolay Aleksandrov | 3b26a5d | 2019-07-02 15:00:19 +0300 | [diff] [blame] | 1341 | saddr.u.ip6 = ipv6_hdr(skb)->saddr; |
Linus Lüssing | b00589a | 2013-08-01 01:06:20 +0200 | [diff] [blame] | 1342 | |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1343 | br_multicast_query_received(br, port, &br->ip6_other_query, |
| 1344 | &saddr, max_delay); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1345 | goto out; |
Linus Lüssing | 6c03ee8 | 2014-06-12 01:41:23 +0200 | [diff] [blame] | 1346 | } else if (!group) { |
| 1347 | goto out; |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1348 | } |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1349 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 1350 | mp = br_mdb_ip6_get(br, group, vid); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1351 | if (!mp) |
| 1352 | goto out; |
| 1353 | |
| 1354 | max_delay *= br->multicast_last_member_count; |
Andrew Lunn | ff0fd34 | 2017-11-09 23:10:57 +0100 | [diff] [blame] | 1355 | if (mp->host_joined && |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1356 | (timer_pending(&mp->timer) ? |
| 1357 | time_after(mp->timer.expires, now + max_delay) : |
| 1358 | try_to_del_timer_sync(&mp->timer) >= 0)) |
| 1359 | mod_timer(&mp->timer, now + max_delay); |
| 1360 | |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 1361 | for (pp = &mp->ports; |
| 1362 | (p = mlock_dereference(*pp, br)) != NULL; |
| 1363 | pp = &p->next) { |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1364 | if (timer_pending(&p->timer) ? |
| 1365 | time_after(p->timer.expires, now + max_delay) : |
| 1366 | try_to_del_timer_sync(&p->timer) >= 0) |
Herbert Xu | 24f9cdc | 2011-02-11 12:42:07 +0000 | [diff] [blame] | 1367 | mod_timer(&p->timer, now + max_delay); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | out: |
| 1371 | spin_unlock(&br->multicast_lock); |
| 1372 | return err; |
| 1373 | } |
| 1374 | #endif |
| 1375 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1376 | static void |
| 1377 | br_multicast_leave_group(struct net_bridge *br, |
| 1378 | struct net_bridge_port *port, |
| 1379 | struct br_ip *group, |
| 1380 | struct bridge_mcast_other_query *other_query, |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1381 | struct bridge_mcast_own_query *own_query, |
| 1382 | const unsigned char *src) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1383 | { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1384 | struct net_bridge_mdb_entry *mp; |
| 1385 | struct net_bridge_port_group *p; |
| 1386 | unsigned long now; |
| 1387 | unsigned long time; |
| 1388 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1389 | spin_lock(&br->multicast_lock); |
| 1390 | if (!netif_running(br->dev) || |
Satish Ashok | 544586f | 2015-07-28 03:28:27 -0700 | [diff] [blame] | 1391 | (port && port->state == BR_STATE_DISABLED)) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1392 | goto out; |
| 1393 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 1394 | mp = br_mdb_ip_get(br, group); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1395 | if (!mp) |
| 1396 | goto out; |
| 1397 | |
Satish Ashok | 544586f | 2015-07-28 03:28:27 -0700 | [diff] [blame] | 1398 | if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) { |
| 1399 | struct net_bridge_port_group __rcu **pp; |
| 1400 | |
| 1401 | for (pp = &mp->ports; |
| 1402 | (p = mlock_dereference(*pp, br)) != NULL; |
| 1403 | pp = &p->next) { |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1404 | if (!br_port_group_equal(p, port, src)) |
Satish Ashok | 544586f | 2015-07-28 03:28:27 -0700 | [diff] [blame] | 1405 | continue; |
| 1406 | |
Nikolay Aleksandrov | 5c725b6 | 2019-07-30 14:21:00 +0300 | [diff] [blame] | 1407 | if (p->flags & MDB_PG_FLAGS_PERMANENT) |
| 1408 | break; |
| 1409 | |
Satish Ashok | 544586f | 2015-07-28 03:28:27 -0700 | [diff] [blame] | 1410 | rcu_assign_pointer(*pp, p->next); |
| 1411 | hlist_del_init(&p->mglist); |
| 1412 | del_timer(&p->timer); |
Nikolay Aleksandrov | 4329596 | 2018-12-05 15:14:25 +0200 | [diff] [blame] | 1413 | kfree_rcu(p, rcu); |
Elad Raz | 45ebcce | 2016-04-21 12:52:45 +0200 | [diff] [blame] | 1414 | br_mdb_notify(br->dev, port, group, RTM_DELMDB, |
Nikolay Aleksandrov | 3247b27 | 2019-07-30 15:20:41 +0300 | [diff] [blame] | 1415 | p->flags | MDB_PG_FLAGS_FAST_LEAVE); |
Satish Ashok | 544586f | 2015-07-28 03:28:27 -0700 | [diff] [blame] | 1416 | |
Andrew Lunn | ff0fd34 | 2017-11-09 23:10:57 +0100 | [diff] [blame] | 1417 | if (!mp->ports && !mp->host_joined && |
Satish Ashok | 544586f | 2015-07-28 03:28:27 -0700 | [diff] [blame] | 1418 | netif_running(br->dev)) |
| 1419 | mod_timer(&mp->timer, jiffies); |
| 1420 | } |
| 1421 | goto out; |
| 1422 | } |
| 1423 | |
| 1424 | if (timer_pending(&other_query->timer)) |
| 1425 | goto out; |
| 1426 | |
Nikolay Aleksandrov | 675779a | 2018-09-26 17:01:04 +0300 | [diff] [blame] | 1427 | if (br_opt_get(br, BROPT_MULTICAST_QUERIER)) { |
Cong Wang | 6b7df11 | 2013-05-21 21:52:56 +0000 | [diff] [blame] | 1428 | __br_multicast_send_query(br, port, &mp->addr); |
| 1429 | |
| 1430 | time = jiffies + br->multicast_last_member_count * |
| 1431 | br->multicast_last_member_interval; |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1432 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1433 | mod_timer(&own_query->timer, time); |
Cong Wang | 6b7df11 | 2013-05-21 21:52:56 +0000 | [diff] [blame] | 1434 | |
| 1435 | for (p = mlock_dereference(mp->ports, br); |
| 1436 | p != NULL; |
| 1437 | p = mlock_dereference(p->next, br)) { |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1438 | if (!br_port_group_equal(p, port, src)) |
Cong Wang | 6b7df11 | 2013-05-21 21:52:56 +0000 | [diff] [blame] | 1439 | continue; |
| 1440 | |
| 1441 | if (!hlist_unhashed(&p->mglist) && |
| 1442 | (timer_pending(&p->timer) ? |
| 1443 | time_after(p->timer.expires, time) : |
| 1444 | try_to_del_timer_sync(&p->timer) >= 0)) { |
| 1445 | mod_timer(&p->timer, time); |
| 1446 | } |
| 1447 | |
| 1448 | break; |
| 1449 | } |
| 1450 | } |
| 1451 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1452 | now = jiffies; |
| 1453 | time = now + br->multicast_last_member_count * |
| 1454 | br->multicast_last_member_interval; |
| 1455 | |
| 1456 | if (!port) { |
Andrew Lunn | ff0fd34 | 2017-11-09 23:10:57 +0100 | [diff] [blame] | 1457 | if (mp->host_joined && |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1458 | (timer_pending(&mp->timer) ? |
| 1459 | time_after(mp->timer.expires, time) : |
| 1460 | try_to_del_timer_sync(&mp->timer) >= 0)) { |
| 1461 | mod_timer(&mp->timer, time); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1462 | } |
Linus Lüssing | 454594f | 2013-10-20 00:58:57 +0200 | [diff] [blame] | 1463 | |
| 1464 | goto out; |
| 1465 | } |
| 1466 | |
| 1467 | for (p = mlock_dereference(mp->ports, br); |
| 1468 | p != NULL; |
| 1469 | p = mlock_dereference(p->next, br)) { |
| 1470 | if (p->port != port) |
| 1471 | continue; |
| 1472 | |
| 1473 | if (!hlist_unhashed(&p->mglist) && |
| 1474 | (timer_pending(&p->timer) ? |
| 1475 | time_after(p->timer.expires, time) : |
| 1476 | try_to_del_timer_sync(&p->timer) >= 0)) { |
| 1477 | mod_timer(&p->timer, time); |
| 1478 | } |
| 1479 | |
| 1480 | break; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1481 | } |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1482 | out: |
| 1483 | spin_unlock(&br->multicast_lock); |
| 1484 | } |
| 1485 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 1486 | static void br_ip4_multicast_leave_group(struct net_bridge *br, |
| 1487 | struct net_bridge_port *port, |
Vlad Yasevich | b0e9a30 | 2013-02-13 12:00:17 +0000 | [diff] [blame] | 1488 | __be32 group, |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1489 | __u16 vid, |
| 1490 | const unsigned char *src) |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 1491 | { |
| 1492 | struct br_ip br_group; |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1493 | struct bridge_mcast_own_query *own_query; |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 1494 | |
| 1495 | if (ipv4_is_local_multicast(group)) |
| 1496 | return; |
| 1497 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1498 | own_query = port ? &port->ip4_own_query : &br->ip4_own_query; |
| 1499 | |
Nikolay Aleksandrov | 1515a63f | 2019-04-03 23:27:24 +0300 | [diff] [blame] | 1500 | memset(&br_group, 0, sizeof(br_group)); |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 1501 | br_group.u.ip4 = group; |
| 1502 | br_group.proto = htons(ETH_P_IP); |
Vlad Yasevich | b0e9a30 | 2013-02-13 12:00:17 +0000 | [diff] [blame] | 1503 | br_group.vid = vid; |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 1504 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1505 | br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query, |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1506 | own_query, src); |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 1507 | } |
| 1508 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 1509 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1510 | static void br_ip6_multicast_leave_group(struct net_bridge *br, |
| 1511 | struct net_bridge_port *port, |
Vlad Yasevich | b0e9a30 | 2013-02-13 12:00:17 +0000 | [diff] [blame] | 1512 | const struct in6_addr *group, |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1513 | __u16 vid, |
| 1514 | const unsigned char *src) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1515 | { |
| 1516 | struct br_ip br_group; |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1517 | struct bridge_mcast_own_query *own_query; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1518 | |
Linus Lüssing | 3c3769e | 2013-09-04 02:13:39 +0200 | [diff] [blame] | 1519 | if (ipv6_addr_is_ll_all_nodes(group)) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1520 | return; |
| 1521 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1522 | own_query = port ? &port->ip6_own_query : &br->ip6_own_query; |
| 1523 | |
Nikolay Aleksandrov | 1515a63f | 2019-04-03 23:27:24 +0300 | [diff] [blame] | 1524 | memset(&br_group, 0, sizeof(br_group)); |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 1525 | br_group.u.ip6 = *group; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1526 | br_group.proto = htons(ETH_P_IPV6); |
Vlad Yasevich | b0e9a30 | 2013-02-13 12:00:17 +0000 | [diff] [blame] | 1527 | br_group.vid = vid; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1528 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1529 | br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query, |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1530 | own_query, src); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1531 | } |
| 1532 | #endif |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 1533 | |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 1534 | static void br_multicast_err_count(const struct net_bridge *br, |
| 1535 | const struct net_bridge_port *p, |
| 1536 | __be16 proto) |
| 1537 | { |
| 1538 | struct bridge_mcast_stats __percpu *stats; |
| 1539 | struct bridge_mcast_stats *pstats; |
| 1540 | |
Nikolay Aleksandrov | 675779a | 2018-09-26 17:01:04 +0300 | [diff] [blame] | 1541 | if (!br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)) |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 1542 | return; |
| 1543 | |
| 1544 | if (p) |
| 1545 | stats = p->mcast_stats; |
| 1546 | else |
| 1547 | stats = br->mcast_stats; |
| 1548 | if (WARN_ON(!stats)) |
| 1549 | return; |
| 1550 | |
| 1551 | pstats = this_cpu_ptr(stats); |
| 1552 | |
| 1553 | u64_stats_update_begin(&pstats->syncp); |
| 1554 | switch (proto) { |
| 1555 | case htons(ETH_P_IP): |
| 1556 | pstats->mstats.igmp_parse_errors++; |
| 1557 | break; |
| 1558 | #if IS_ENABLED(CONFIG_IPV6) |
| 1559 | case htons(ETH_P_IPV6): |
| 1560 | pstats->mstats.mld_parse_errors++; |
| 1561 | break; |
| 1562 | #endif |
| 1563 | } |
| 1564 | u64_stats_update_end(&pstats->syncp); |
| 1565 | } |
| 1566 | |
Nikolay Aleksandrov | 91b02d3 | 2016-10-31 13:21:05 +0100 | [diff] [blame] | 1567 | static void br_multicast_pim(struct net_bridge *br, |
| 1568 | struct net_bridge_port *port, |
| 1569 | const struct sk_buff *skb) |
| 1570 | { |
| 1571 | unsigned int offset = skb_transport_offset(skb); |
| 1572 | struct pimhdr *pimhdr, _pimhdr; |
| 1573 | |
| 1574 | pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr); |
| 1575 | if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION || |
| 1576 | pim_hdr_type(pimhdr) != PIM_TYPE_HELLO) |
| 1577 | return; |
| 1578 | |
| 1579 | br_multicast_mark_router(br, port); |
| 1580 | } |
| 1581 | |
Linus Lüssing | 4b3087c | 2019-01-21 07:26:28 +0100 | [diff] [blame] | 1582 | static int br_ip4_multicast_mrd_rcv(struct net_bridge *br, |
| 1583 | struct net_bridge_port *port, |
| 1584 | struct sk_buff *skb) |
| 1585 | { |
| 1586 | if (ip_hdr(skb)->protocol != IPPROTO_IGMP || |
| 1587 | igmp_hdr(skb)->type != IGMP_MRDISC_ADV) |
| 1588 | return -ENOMSG; |
| 1589 | |
| 1590 | br_multicast_mark_router(br, port); |
| 1591 | |
| 1592 | return 0; |
| 1593 | } |
| 1594 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1595 | static int br_multicast_ipv4_rcv(struct net_bridge *br, |
| 1596 | struct net_bridge_port *port, |
Vlad Yasevich | 0649909 | 2013-10-28 15:45:07 -0400 | [diff] [blame] | 1597 | struct sk_buff *skb, |
| 1598 | u16 vid) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1599 | { |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1600 | const unsigned char *src; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1601 | struct igmphdr *ih; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1602 | int err; |
| 1603 | |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1604 | err = ip_mc_check_igmp(skb); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1605 | |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 1606 | if (err == -ENOMSG) { |
Nikolay Aleksandrov | 91b02d3 | 2016-10-31 13:21:05 +0100 | [diff] [blame] | 1607 | if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) { |
Herbert Xu | bd4265f | 2011-06-23 02:39:12 +0000 | [diff] [blame] | 1608 | BR_INPUT_SKB_CB(skb)->mrouters_only = 1; |
Nikolay Aleksandrov | 91b02d3 | 2016-10-31 13:21:05 +0100 | [diff] [blame] | 1609 | } else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) { |
| 1610 | if (ip_hdr(skb)->protocol == IPPROTO_PIM) |
| 1611 | br_multicast_pim(br, port, skb); |
Linus Lüssing | 4b3087c | 2019-01-21 07:26:28 +0100 | [diff] [blame] | 1612 | } else if (ipv4_is_all_snoopers(ip_hdr(skb)->daddr)) { |
Li RongQing | 08e7162 | 2019-02-19 10:17:09 +0800 | [diff] [blame] | 1613 | br_ip4_multicast_mrd_rcv(br, port, skb); |
Nikolay Aleksandrov | 91b02d3 | 2016-10-31 13:21:05 +0100 | [diff] [blame] | 1614 | } |
Linus Lüssing | 4b3087c | 2019-01-21 07:26:28 +0100 | [diff] [blame] | 1615 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1616 | return 0; |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 1617 | } else if (err < 0) { |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 1618 | br_multicast_err_count(br, port, skb->protocol); |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 1619 | return err; |
Herbert Xu | bd4265f | 2011-06-23 02:39:12 +0000 | [diff] [blame] | 1620 | } |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1621 | |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 1622 | ih = igmp_hdr(skb); |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1623 | src = eth_hdr(skb)->h_source; |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 1624 | BR_INPUT_SKB_CB(skb)->igmp = ih->type; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1625 | |
| 1626 | switch (ih->type) { |
| 1627 | case IGMP_HOST_MEMBERSHIP_REPORT: |
| 1628 | case IGMPV2_HOST_MEMBERSHIP_REPORT: |
Fernando Luis Vázquez Cao | 62b2bcb | 2011-06-13 15:04:43 +0000 | [diff] [blame] | 1629 | BR_INPUT_SKB_CB(skb)->mrouters_only = 1; |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1630 | err = br_ip4_multicast_add_group(br, port, ih->group, vid, src); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1631 | break; |
| 1632 | case IGMPV3_HOST_MEMBERSHIP_REPORT: |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1633 | err = br_ip4_multicast_igmp3_report(br, port, skb, vid); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1634 | break; |
| 1635 | case IGMP_HOST_MEMBERSHIP_QUERY: |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1636 | br_ip4_multicast_query(br, port, skb, vid); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1637 | break; |
| 1638 | case IGMP_HOST_LEAVE_MESSAGE: |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1639 | br_ip4_multicast_leave_group(br, port, ih->group, vid, src); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1640 | break; |
| 1641 | } |
| 1642 | |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 1643 | br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp, |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 1644 | BR_MCAST_DIR_RX); |
| 1645 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1646 | return err; |
| 1647 | } |
| 1648 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 1649 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Lüssing | 4b3087c | 2019-01-21 07:26:28 +0100 | [diff] [blame] | 1650 | static int br_ip6_multicast_mrd_rcv(struct net_bridge *br, |
| 1651 | struct net_bridge_port *port, |
| 1652 | struct sk_buff *skb) |
| 1653 | { |
| 1654 | int ret; |
| 1655 | |
| 1656 | if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6) |
| 1657 | return -ENOMSG; |
| 1658 | |
| 1659 | ret = ipv6_mc_check_icmpv6(skb); |
| 1660 | if (ret < 0) |
| 1661 | return ret; |
| 1662 | |
| 1663 | if (icmp6_hdr(skb)->icmp6_type != ICMPV6_MRDISC_ADV) |
| 1664 | return -ENOMSG; |
| 1665 | |
| 1666 | br_multicast_mark_router(br, port); |
| 1667 | |
| 1668 | return 0; |
| 1669 | } |
| 1670 | |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1671 | static int br_multicast_ipv6_rcv(struct net_bridge *br, |
| 1672 | struct net_bridge_port *port, |
Vlad Yasevich | 0649909 | 2013-10-28 15:45:07 -0400 | [diff] [blame] | 1673 | struct sk_buff *skb, |
| 1674 | u16 vid) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1675 | { |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1676 | const unsigned char *src; |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 1677 | struct mld_msg *mld; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1678 | int err; |
| 1679 | |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1680 | err = ipv6_mc_check_mld(skb); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1681 | |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 1682 | if (err == -ENOMSG) { |
| 1683 | if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr)) |
| 1684 | BR_INPUT_SKB_CB(skb)->mrouters_only = 1; |
Linus Lüssing | 4b3087c | 2019-01-21 07:26:28 +0100 | [diff] [blame] | 1685 | |
| 1686 | if (ipv6_addr_is_all_snoopers(&ipv6_hdr(skb)->daddr)) { |
| 1687 | err = br_ip6_multicast_mrd_rcv(br, port, skb); |
| 1688 | |
| 1689 | if (err < 0 && err != -ENOMSG) { |
| 1690 | br_multicast_err_count(br, port, skb->protocol); |
| 1691 | return err; |
| 1692 | } |
| 1693 | } |
| 1694 | |
Linus Lüssing | 8fad9c3 | 2013-09-04 02:13:38 +0200 | [diff] [blame] | 1695 | return 0; |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 1696 | } else if (err < 0) { |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 1697 | br_multicast_err_count(br, port, skb->protocol); |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 1698 | return err; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1699 | } |
| 1700 | |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 1701 | mld = (struct mld_msg *)skb_transport_header(skb); |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 1702 | BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1703 | |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 1704 | switch (mld->mld_type) { |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1705 | case ICMPV6_MGM_REPORT: |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1706 | src = eth_hdr(skb)->h_source; |
Fernando Luis Vázquez Cao | fc2af6c | 2011-06-13 15:06:58 +0000 | [diff] [blame] | 1707 | BR_INPUT_SKB_CB(skb)->mrouters_only = 1; |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1708 | err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid, |
| 1709 | src); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1710 | break; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1711 | case ICMPV6_MLD2_REPORT: |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1712 | err = br_ip6_multicast_mld2_report(br, port, skb, vid); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1713 | break; |
| 1714 | case ICMPV6_MGM_QUERY: |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1715 | err = br_ip6_multicast_query(br, port, skb, vid); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1716 | break; |
| 1717 | case ICMPV6_MGM_REDUCTION: |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1718 | src = eth_hdr(skb)->h_source; |
| 1719 | br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src); |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 1720 | break; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1721 | } |
| 1722 | |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 1723 | br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp, |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 1724 | BR_MCAST_DIR_RX); |
| 1725 | |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1726 | return err; |
| 1727 | } |
| 1728 | #endif |
| 1729 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1730 | int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port, |
Vlad Yasevich | 0649909 | 2013-10-28 15:45:07 -0400 | [diff] [blame] | 1731 | struct sk_buff *skb, u16 vid) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1732 | { |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 1733 | int ret = 0; |
| 1734 | |
YOSHIFUJI Hideaki / 吉藤英明 | 1fafc7a | 2010-04-25 08:06:40 +0000 | [diff] [blame] | 1735 | BR_INPUT_SKB_CB(skb)->igmp = 0; |
| 1736 | BR_INPUT_SKB_CB(skb)->mrouters_only = 0; |
| 1737 | |
Nikolay Aleksandrov | 13cefad | 2018-09-26 17:01:03 +0300 | [diff] [blame] | 1738 | if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1739 | return 0; |
| 1740 | |
| 1741 | switch (skb->protocol) { |
| 1742 | case htons(ETH_P_IP): |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 1743 | ret = br_multicast_ipv4_rcv(br, port, skb, vid); |
| 1744 | break; |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 1745 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1746 | case htons(ETH_P_IPV6): |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 1747 | ret = br_multicast_ipv6_rcv(br, port, skb, vid); |
| 1748 | break; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1749 | #endif |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 1752 | return ret; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1755 | static void br_multicast_query_expired(struct net_bridge *br, |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 1756 | struct bridge_mcast_own_query *query, |
| 1757 | struct bridge_mcast_querier *querier) |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1758 | { |
| 1759 | spin_lock(&br->multicast_lock); |
| 1760 | if (query->startup_sent < br->multicast_startup_query_count) |
| 1761 | query->startup_sent++; |
| 1762 | |
Eric Dumazet | 71d9f61 | 2015-05-28 04:42:54 -0700 | [diff] [blame] | 1763 | RCU_INIT_POINTER(querier->port, NULL); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1764 | br_multicast_send_query(br, NULL, query); |
| 1765 | spin_unlock(&br->multicast_lock); |
| 1766 | } |
| 1767 | |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 1768 | static void br_ip4_multicast_query_expired(struct timer_list *t) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1769 | { |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 1770 | struct net_bridge *br = from_timer(br, t, ip4_own_query.timer); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1771 | |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 1772 | br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1775 | #if IS_ENABLED(CONFIG_IPV6) |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 1776 | static void br_ip6_multicast_query_expired(struct timer_list *t) |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1777 | { |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 1778 | struct net_bridge *br = from_timer(br, t, ip6_own_query.timer); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1779 | |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 1780 | br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1781 | } |
| 1782 | #endif |
| 1783 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1784 | void br_multicast_init(struct net_bridge *br) |
| 1785 | { |
Nikolay Aleksandrov | d08c6bc | 2018-12-05 15:14:27 +0200 | [diff] [blame] | 1786 | br->hash_max = BR_MULTICAST_DEFAULT_HASH_MAX; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1787 | |
Nikolay Aleksandrov | 7f0aec7 | 2016-02-26 21:20:01 +0100 | [diff] [blame] | 1788 | br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1789 | br->multicast_last_member_count = 2; |
| 1790 | br->multicast_startup_query_count = 2; |
| 1791 | |
| 1792 | br->multicast_last_member_interval = HZ; |
| 1793 | br->multicast_query_response_interval = 10 * HZ; |
| 1794 | br->multicast_startup_query_interval = 125 * HZ / 4; |
| 1795 | br->multicast_query_interval = 125 * HZ; |
| 1796 | br->multicast_querier_interval = 255 * HZ; |
| 1797 | br->multicast_membership_interval = 260 * HZ; |
| 1798 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1799 | br->ip4_other_query.delay_time = 0; |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 1800 | br->ip4_querier.port = NULL; |
Nikolay Aleksandrov | aa2ae3e | 2016-11-21 13:03:25 +0100 | [diff] [blame] | 1801 | br->multicast_igmp_version = 2; |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1802 | #if IS_ENABLED(CONFIG_IPV6) |
Nikolay Aleksandrov | aa2ae3e | 2016-11-21 13:03:25 +0100 | [diff] [blame] | 1803 | br->multicast_mld_version = 1; |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1804 | br->ip6_other_query.delay_time = 0; |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 1805 | br->ip6_querier.port = NULL; |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1806 | #endif |
Ido Schimmel | 6919622a | 2018-10-01 11:57:01 +0300 | [diff] [blame] | 1807 | br_opt_toggle(br, BROPT_MULTICAST_ENABLED, true); |
Nikolay Aleksandrov | 675779a | 2018-09-26 17:01:04 +0300 | [diff] [blame] | 1808 | br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true); |
Linus Lüssing | b00589a | 2013-08-01 01:06:20 +0200 | [diff] [blame] | 1809 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1810 | spin_lock_init(&br->multicast_lock); |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 1811 | timer_setup(&br->multicast_router_timer, |
| 1812 | br_multicast_local_router_expired, 0); |
| 1813 | timer_setup(&br->ip4_other_query.timer, |
| 1814 | br_ip4_multicast_querier_expired, 0); |
| 1815 | timer_setup(&br->ip4_own_query.timer, |
| 1816 | br_ip4_multicast_query_expired, 0); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1817 | #if IS_ENABLED(CONFIG_IPV6) |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 1818 | timer_setup(&br->ip6_other_query.timer, |
| 1819 | br_ip6_multicast_querier_expired, 0); |
| 1820 | timer_setup(&br->ip6_own_query.timer, |
| 1821 | br_ip6_multicast_query_expired, 0); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1822 | #endif |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 1823 | INIT_HLIST_HEAD(&br->mdb_list); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1824 | } |
| 1825 | |
Linus Lüssing | 4effd28 | 2019-01-21 07:26:27 +0100 | [diff] [blame] | 1826 | static void br_ip4_multicast_join_snoopers(struct net_bridge *br) |
| 1827 | { |
| 1828 | struct in_device *in_dev = in_dev_get(br->dev); |
| 1829 | |
| 1830 | if (!in_dev) |
| 1831 | return; |
| 1832 | |
Florian Fainelli | 9fb2080 | 2019-02-01 20:20:52 -0800 | [diff] [blame] | 1833 | __ip_mc_inc_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC); |
Linus Lüssing | 4effd28 | 2019-01-21 07:26:27 +0100 | [diff] [blame] | 1834 | in_dev_put(in_dev); |
| 1835 | } |
| 1836 | |
| 1837 | #if IS_ENABLED(CONFIG_IPV6) |
| 1838 | static void br_ip6_multicast_join_snoopers(struct net_bridge *br) |
| 1839 | { |
| 1840 | struct in6_addr addr; |
| 1841 | |
| 1842 | ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a)); |
| 1843 | ipv6_dev_mc_inc(br->dev, &addr); |
| 1844 | } |
| 1845 | #else |
| 1846 | static inline void br_ip6_multicast_join_snoopers(struct net_bridge *br) |
| 1847 | { |
| 1848 | } |
| 1849 | #endif |
| 1850 | |
| 1851 | static void br_multicast_join_snoopers(struct net_bridge *br) |
| 1852 | { |
| 1853 | br_ip4_multicast_join_snoopers(br); |
| 1854 | br_ip6_multicast_join_snoopers(br); |
| 1855 | } |
| 1856 | |
| 1857 | static void br_ip4_multicast_leave_snoopers(struct net_bridge *br) |
| 1858 | { |
| 1859 | struct in_device *in_dev = in_dev_get(br->dev); |
| 1860 | |
| 1861 | if (WARN_ON(!in_dev)) |
| 1862 | return; |
| 1863 | |
Florian Fainelli | 9fb2080 | 2019-02-01 20:20:52 -0800 | [diff] [blame] | 1864 | __ip_mc_dec_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC); |
Linus Lüssing | 4effd28 | 2019-01-21 07:26:27 +0100 | [diff] [blame] | 1865 | in_dev_put(in_dev); |
| 1866 | } |
| 1867 | |
| 1868 | #if IS_ENABLED(CONFIG_IPV6) |
| 1869 | static void br_ip6_multicast_leave_snoopers(struct net_bridge *br) |
| 1870 | { |
| 1871 | struct in6_addr addr; |
| 1872 | |
| 1873 | ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a)); |
| 1874 | ipv6_dev_mc_dec(br->dev, &addr); |
| 1875 | } |
| 1876 | #else |
| 1877 | static inline void br_ip6_multicast_leave_snoopers(struct net_bridge *br) |
| 1878 | { |
| 1879 | } |
| 1880 | #endif |
| 1881 | |
| 1882 | static void br_multicast_leave_snoopers(struct net_bridge *br) |
| 1883 | { |
| 1884 | br_ip4_multicast_leave_snoopers(br); |
| 1885 | br_ip6_multicast_leave_snoopers(br); |
| 1886 | } |
| 1887 | |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1888 | static void __br_multicast_open(struct net_bridge *br, |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1889 | struct bridge_mcast_own_query *query) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1890 | { |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1891 | query->startup_sent = 0; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1892 | |
Nikolay Aleksandrov | 13cefad | 2018-09-26 17:01:03 +0300 | [diff] [blame] | 1893 | if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1894 | return; |
| 1895 | |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1896 | mod_timer(&query->timer, jiffies); |
| 1897 | } |
| 1898 | |
| 1899 | void br_multicast_open(struct net_bridge *br) |
| 1900 | { |
Linus Lüssing | 4effd28 | 2019-01-21 07:26:27 +0100 | [diff] [blame] | 1901 | if (br_opt_get(br, BROPT_MULTICAST_ENABLED)) |
| 1902 | br_multicast_join_snoopers(br); |
| 1903 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1904 | __br_multicast_open(br, &br->ip4_own_query); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1905 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1906 | __br_multicast_open(br, &br->ip6_own_query); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1907 | #endif |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | void br_multicast_stop(struct net_bridge *br) |
| 1911 | { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1912 | del_timer_sync(&br->multicast_router_timer); |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1913 | del_timer_sync(&br->ip4_other_query.timer); |
| 1914 | del_timer_sync(&br->ip4_own_query.timer); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1915 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1916 | del_timer_sync(&br->ip6_other_query.timer); |
| 1917 | del_timer_sync(&br->ip6_own_query.timer); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1918 | #endif |
Linus Lüssing | 4effd28 | 2019-01-21 07:26:27 +0100 | [diff] [blame] | 1919 | |
| 1920 | if (br_opt_get(br, BROPT_MULTICAST_ENABLED)) |
| 1921 | br_multicast_leave_snoopers(br); |
Satish Ashok | e10177a | 2015-07-15 07:16:51 -0700 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | void br_multicast_dev_del(struct net_bridge *br) |
| 1925 | { |
Satish Ashok | e10177a | 2015-07-15 07:16:51 -0700 | [diff] [blame] | 1926 | struct net_bridge_mdb_entry *mp; |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 1927 | struct hlist_node *tmp; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1928 | |
| 1929 | spin_lock_bh(&br->multicast_lock); |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 1930 | hlist_for_each_entry_safe(mp, tmp, &br->mdb_list, mdb_node) { |
| 1931 | del_timer(&mp->timer); |
| 1932 | rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode, |
| 1933 | br_mdb_rht_params); |
| 1934 | hlist_del_rcu(&mp->mdb_node); |
Nikolay Aleksandrov | 4329596 | 2018-12-05 15:14:25 +0200 | [diff] [blame] | 1935 | kfree_rcu(mp, rcu); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1936 | } |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1937 | spin_unlock_bh(&br->multicast_lock); |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 1938 | |
Nikolay Aleksandrov | 4329596 | 2018-12-05 15:14:25 +0200 | [diff] [blame] | 1939 | rcu_barrier(); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1940 | } |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1941 | |
| 1942 | int br_multicast_set_router(struct net_bridge *br, unsigned long val) |
| 1943 | { |
Linus Lüssing | 6ae4ae8 | 2015-05-23 03:12:34 +0200 | [diff] [blame] | 1944 | int err = -EINVAL; |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1945 | |
| 1946 | spin_lock_bh(&br->multicast_lock); |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1947 | |
| 1948 | switch (val) { |
Nikolay Aleksandrov | 7f0aec7 | 2016-02-26 21:20:01 +0100 | [diff] [blame] | 1949 | case MDB_RTR_TYPE_DISABLED: |
| 1950 | case MDB_RTR_TYPE_PERM: |
Yotam Gigi | 7704142 | 2017-10-09 11:15:31 +0200 | [diff] [blame] | 1951 | br_mc_router_state_change(br, val == MDB_RTR_TYPE_PERM); |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1952 | del_timer(&br->multicast_router_timer); |
Yotam Gigi | 7704142 | 2017-10-09 11:15:31 +0200 | [diff] [blame] | 1953 | br->multicast_router = val; |
| 1954 | err = 0; |
| 1955 | break; |
Nikolay Aleksandrov | 7f0aec7 | 2016-02-26 21:20:01 +0100 | [diff] [blame] | 1956 | case MDB_RTR_TYPE_TEMP_QUERY: |
Yotam Gigi | 7704142 | 2017-10-09 11:15:31 +0200 | [diff] [blame] | 1957 | if (br->multicast_router != MDB_RTR_TYPE_TEMP_QUERY) |
| 1958 | br_mc_router_state_change(br, false); |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1959 | br->multicast_router = val; |
| 1960 | err = 0; |
| 1961 | break; |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1964 | spin_unlock_bh(&br->multicast_lock); |
| 1965 | |
| 1966 | return err; |
| 1967 | } |
| 1968 | |
Nikolay Aleksandrov | 7f0aec7 | 2016-02-26 21:20:01 +0100 | [diff] [blame] | 1969 | static void __del_port_router(struct net_bridge_port *p) |
| 1970 | { |
| 1971 | if (hlist_unhashed(&p->rlist)) |
| 1972 | return; |
| 1973 | hlist_del_init_rcu(&p->rlist); |
| 1974 | br_rtr_notify(p->br->dev, p, RTM_DELMDB); |
Nogah Frankel | 6d54964 | 2017-02-09 14:54:42 +0100 | [diff] [blame] | 1975 | br_port_mc_router_state_change(p, false); |
Nogah Frankel | f12e7d9 | 2017-02-09 14:54:41 +0100 | [diff] [blame] | 1976 | |
| 1977 | /* don't allow timer refresh */ |
| 1978 | if (p->multicast_router == MDB_RTR_TYPE_TEMP) |
| 1979 | p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; |
Nikolay Aleksandrov | 7f0aec7 | 2016-02-26 21:20:01 +0100 | [diff] [blame] | 1980 | } |
| 1981 | |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1982 | int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val) |
| 1983 | { |
| 1984 | struct net_bridge *br = p->br; |
Nikolay Aleksandrov | a55d824 | 2016-02-26 21:20:03 +0100 | [diff] [blame] | 1985 | unsigned long now = jiffies; |
Linus Lüssing | 6ae4ae8 | 2015-05-23 03:12:34 +0200 | [diff] [blame] | 1986 | int err = -EINVAL; |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1987 | |
| 1988 | spin_lock(&br->multicast_lock); |
Nikolay Aleksandrov | 4950cfd | 2016-02-26 21:20:02 +0100 | [diff] [blame] | 1989 | if (p->multicast_router == val) { |
Nikolay Aleksandrov | a55d824 | 2016-02-26 21:20:03 +0100 | [diff] [blame] | 1990 | /* Refresh the temp router port timer */ |
| 1991 | if (p->multicast_router == MDB_RTR_TYPE_TEMP) |
| 1992 | mod_timer(&p->multicast_router_timer, |
| 1993 | now + br->multicast_querier_interval); |
Nikolay Aleksandrov | 4950cfd | 2016-02-26 21:20:02 +0100 | [diff] [blame] | 1994 | err = 0; |
| 1995 | goto unlock; |
| 1996 | } |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1997 | switch (val) { |
Nikolay Aleksandrov | 7f0aec7 | 2016-02-26 21:20:01 +0100 | [diff] [blame] | 1998 | case MDB_RTR_TYPE_DISABLED: |
| 1999 | p->multicast_router = MDB_RTR_TYPE_DISABLED; |
| 2000 | __del_port_router(p); |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 2001 | del_timer(&p->multicast_router_timer); |
Nikolay Aleksandrov | 7f0aec7 | 2016-02-26 21:20:01 +0100 | [diff] [blame] | 2002 | break; |
| 2003 | case MDB_RTR_TYPE_TEMP_QUERY: |
| 2004 | p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; |
| 2005 | __del_port_router(p); |
| 2006 | break; |
| 2007 | case MDB_RTR_TYPE_PERM: |
| 2008 | p->multicast_router = MDB_RTR_TYPE_PERM; |
| 2009 | del_timer(&p->multicast_router_timer); |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 2010 | br_multicast_add_router(br, p); |
| 2011 | break; |
Nikolay Aleksandrov | a55d824 | 2016-02-26 21:20:03 +0100 | [diff] [blame] | 2012 | case MDB_RTR_TYPE_TEMP: |
| 2013 | p->multicast_router = MDB_RTR_TYPE_TEMP; |
| 2014 | br_multicast_mark_router(br, p); |
| 2015 | break; |
Nikolay Aleksandrov | 7f0aec7 | 2016-02-26 21:20:01 +0100 | [diff] [blame] | 2016 | default: |
| 2017 | goto unlock; |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 2018 | } |
Nikolay Aleksandrov | 7f0aec7 | 2016-02-26 21:20:01 +0100 | [diff] [blame] | 2019 | err = 0; |
| 2020 | unlock: |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 2021 | spin_unlock(&br->multicast_lock); |
| 2022 | |
| 2023 | return err; |
| 2024 | } |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 2025 | |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 2026 | static void br_multicast_start_querier(struct net_bridge *br, |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 2027 | struct bridge_mcast_own_query *query) |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 2028 | { |
| 2029 | struct net_bridge_port *port; |
Herbert Xu | 7485721 | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 2030 | |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 2031 | __br_multicast_open(br, query); |
Herbert Xu | 7485721 | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 2032 | |
Nikolay Aleksandrov | c5b493c | 2019-04-11 15:08:25 +0300 | [diff] [blame] | 2033 | rcu_read_lock(); |
| 2034 | list_for_each_entry_rcu(port, &br->port_list, list) { |
Herbert Xu | 7485721 | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 2035 | if (port->state == BR_STATE_DISABLED || |
| 2036 | port->state == BR_STATE_BLOCKING) |
| 2037 | continue; |
| 2038 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 2039 | if (query == &br->ip4_own_query) |
| 2040 | br_multicast_enable(&port->ip4_own_query); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 2041 | #if IS_ENABLED(CONFIG_IPV6) |
| 2042 | else |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 2043 | br_multicast_enable(&port->ip6_own_query); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 2044 | #endif |
Herbert Xu | 7485721 | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 2045 | } |
Nikolay Aleksandrov | c5b493c | 2019-04-11 15:08:25 +0300 | [diff] [blame] | 2046 | rcu_read_unlock(); |
Herbert Xu | 7485721 | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | int br_multicast_toggle(struct net_bridge *br, unsigned long val) |
| 2050 | { |
Nikolay Aleksandrov | 7cb3f92 | 2016-10-18 18:09:48 +0200 | [diff] [blame] | 2051 | struct net_bridge_port *port; |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 2052 | |
Andrey Vagin | ef5e0d8 | 2011-11-10 05:48:03 +0000 | [diff] [blame] | 2053 | spin_lock_bh(&br->multicast_lock); |
Nikolay Aleksandrov | 13cefad | 2018-09-26 17:01:03 +0300 | [diff] [blame] | 2054 | if (!!br_opt_get(br, BROPT_MULTICAST_ENABLED) == !!val) |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 2055 | goto unlock; |
| 2056 | |
Nikolay Aleksandrov | 13cefad | 2018-09-26 17:01:03 +0300 | [diff] [blame] | 2057 | br_mc_disabled_update(br->dev, val); |
| 2058 | br_opt_toggle(br, BROPT_MULTICAST_ENABLED, !!val); |
Linus Lüssing | 4effd28 | 2019-01-21 07:26:27 +0100 | [diff] [blame] | 2059 | if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) { |
| 2060 | br_multicast_leave_snoopers(br); |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 2061 | goto unlock; |
Linus Lüssing | 4effd28 | 2019-01-21 07:26:27 +0100 | [diff] [blame] | 2062 | } |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 2063 | |
Herbert Xu | 3a7fda0 | 2010-07-29 00:45:30 +0000 | [diff] [blame] | 2064 | if (!netif_running(br->dev)) |
| 2065 | goto unlock; |
| 2066 | |
Nikolay Aleksandrov | 7cb3f92 | 2016-10-18 18:09:48 +0200 | [diff] [blame] | 2067 | br_multicast_open(br); |
| 2068 | list_for_each_entry(port, &br->port_list, list) |
| 2069 | __br_multicast_enable_port(port); |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 2070 | |
| 2071 | unlock: |
Andrey Vagin | ef5e0d8 | 2011-11-10 05:48:03 +0000 | [diff] [blame] | 2072 | spin_unlock_bh(&br->multicast_lock); |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 2073 | |
YueHaibing | a26d94b | 2018-12-17 17:46:23 +0800 | [diff] [blame] | 2074 | return 0; |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 2075 | } |
Herbert Xu | b195167 | 2010-02-27 19:41:51 +0000 | [diff] [blame] | 2076 | |
Ido Schimmel | 9341b98 | 2017-05-26 08:37:24 +0200 | [diff] [blame] | 2077 | bool br_multicast_enabled(const struct net_device *dev) |
| 2078 | { |
| 2079 | struct net_bridge *br = netdev_priv(dev); |
| 2080 | |
Nikolay Aleksandrov | 13cefad | 2018-09-26 17:01:03 +0300 | [diff] [blame] | 2081 | return !!br_opt_get(br, BROPT_MULTICAST_ENABLED); |
Ido Schimmel | 9341b98 | 2017-05-26 08:37:24 +0200 | [diff] [blame] | 2082 | } |
| 2083 | EXPORT_SYMBOL_GPL(br_multicast_enabled); |
| 2084 | |
Yotam Gigi | 0912bda | 2017-10-09 11:15:32 +0200 | [diff] [blame] | 2085 | bool br_multicast_router(const struct net_device *dev) |
| 2086 | { |
| 2087 | struct net_bridge *br = netdev_priv(dev); |
| 2088 | bool is_router; |
| 2089 | |
| 2090 | spin_lock_bh(&br->multicast_lock); |
| 2091 | is_router = br_multicast_is_router(br); |
| 2092 | spin_unlock_bh(&br->multicast_lock); |
| 2093 | return is_router; |
| 2094 | } |
| 2095 | EXPORT_SYMBOL_GPL(br_multicast_router); |
| 2096 | |
Herbert Xu | c5c2326 | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 2097 | int br_multicast_set_querier(struct net_bridge *br, unsigned long val) |
| 2098 | { |
Linus Lüssing | b00589a | 2013-08-01 01:06:20 +0200 | [diff] [blame] | 2099 | unsigned long max_delay; |
| 2100 | |
Herbert Xu | c5c2326 | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 2101 | val = !!val; |
| 2102 | |
| 2103 | spin_lock_bh(&br->multicast_lock); |
Nikolay Aleksandrov | 675779a | 2018-09-26 17:01:04 +0300 | [diff] [blame] | 2104 | if (br_opt_get(br, BROPT_MULTICAST_QUERIER) == val) |
Herbert Xu | c5c2326 | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 2105 | goto unlock; |
| 2106 | |
Nikolay Aleksandrov | 675779a | 2018-09-26 17:01:04 +0300 | [diff] [blame] | 2107 | br_opt_toggle(br, BROPT_MULTICAST_QUERIER, !!val); |
Linus Lüssing | b00589a | 2013-08-01 01:06:20 +0200 | [diff] [blame] | 2108 | if (!val) |
| 2109 | goto unlock; |
| 2110 | |
| 2111 | max_delay = br->multicast_query_response_interval; |
Linus Lüssing | b00589a | 2013-08-01 01:06:20 +0200 | [diff] [blame] | 2112 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 2113 | if (!timer_pending(&br->ip4_other_query.timer)) |
| 2114 | br->ip4_other_query.delay_time = jiffies + max_delay; |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 2115 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 2116 | br_multicast_start_querier(br, &br->ip4_own_query); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 2117 | |
| 2118 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 2119 | if (!timer_pending(&br->ip6_other_query.timer)) |
| 2120 | br->ip6_other_query.delay_time = jiffies + max_delay; |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 2121 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 2122 | br_multicast_start_querier(br, &br->ip6_own_query); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 2123 | #endif |
Herbert Xu | c5c2326 | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 2124 | |
| 2125 | unlock: |
| 2126 | spin_unlock_bh(&br->multicast_lock); |
| 2127 | |
| 2128 | return 0; |
| 2129 | } |
| 2130 | |
Nikolay Aleksandrov | 5e92358 | 2016-11-21 13:03:24 +0100 | [diff] [blame] | 2131 | int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val) |
| 2132 | { |
| 2133 | /* Currently we support only version 2 and 3 */ |
| 2134 | switch (val) { |
| 2135 | case 2: |
| 2136 | case 3: |
| 2137 | break; |
| 2138 | default: |
| 2139 | return -EINVAL; |
| 2140 | } |
| 2141 | |
| 2142 | spin_lock_bh(&br->multicast_lock); |
| 2143 | br->multicast_igmp_version = val; |
| 2144 | spin_unlock_bh(&br->multicast_lock); |
| 2145 | |
| 2146 | return 0; |
| 2147 | } |
| 2148 | |
Nikolay Aleksandrov | aa2ae3e | 2016-11-21 13:03:25 +0100 | [diff] [blame] | 2149 | #if IS_ENABLED(CONFIG_IPV6) |
| 2150 | int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val) |
| 2151 | { |
| 2152 | /* Currently we support version 1 and 2 */ |
| 2153 | switch (val) { |
| 2154 | case 1: |
| 2155 | case 2: |
| 2156 | break; |
| 2157 | default: |
| 2158 | return -EINVAL; |
| 2159 | } |
| 2160 | |
| 2161 | spin_lock_bh(&br->multicast_lock); |
| 2162 | br->multicast_mld_version = val; |
| 2163 | spin_unlock_bh(&br->multicast_lock); |
| 2164 | |
| 2165 | return 0; |
| 2166 | } |
| 2167 | #endif |
| 2168 | |
Linus Lüssing | 07f8ac4 | 2014-06-07 18:26:28 +0200 | [diff] [blame] | 2169 | /** |
| 2170 | * br_multicast_list_adjacent - Returns snooped multicast addresses |
| 2171 | * @dev: The bridge port adjacent to which to retrieve addresses |
| 2172 | * @br_ip_list: The list to store found, snooped multicast IP addresses in |
| 2173 | * |
| 2174 | * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast |
| 2175 | * snooping feature on all bridge ports of dev's bridge device, excluding |
| 2176 | * the addresses from dev itself. |
| 2177 | * |
| 2178 | * Returns the number of items added to br_ip_list. |
| 2179 | * |
| 2180 | * Notes: |
| 2181 | * - br_ip_list needs to be initialized by caller |
| 2182 | * - br_ip_list might contain duplicates in the end |
| 2183 | * (needs to be taken care of by caller) |
| 2184 | * - br_ip_list needs to be freed by caller |
| 2185 | */ |
| 2186 | int br_multicast_list_adjacent(struct net_device *dev, |
| 2187 | struct list_head *br_ip_list) |
| 2188 | { |
| 2189 | struct net_bridge *br; |
| 2190 | struct net_bridge_port *port; |
| 2191 | struct net_bridge_port_group *group; |
| 2192 | struct br_ip_list *entry; |
| 2193 | int count = 0; |
| 2194 | |
| 2195 | rcu_read_lock(); |
Julian Wiedmann | 35f861e | 2019-03-29 14:38:19 +0100 | [diff] [blame] | 2196 | if (!br_ip_list || !netif_is_bridge_port(dev)) |
Linus Lüssing | 07f8ac4 | 2014-06-07 18:26:28 +0200 | [diff] [blame] | 2197 | goto unlock; |
| 2198 | |
| 2199 | port = br_port_get_rcu(dev); |
| 2200 | if (!port || !port->br) |
| 2201 | goto unlock; |
| 2202 | |
| 2203 | br = port->br; |
| 2204 | |
| 2205 | list_for_each_entry_rcu(port, &br->port_list, list) { |
| 2206 | if (!port->dev || port->dev == dev) |
| 2207 | continue; |
| 2208 | |
| 2209 | hlist_for_each_entry_rcu(group, &port->mglist, mglist) { |
| 2210 | entry = kmalloc(sizeof(*entry), GFP_ATOMIC); |
| 2211 | if (!entry) |
| 2212 | goto unlock; |
| 2213 | |
| 2214 | entry->addr = group->addr; |
| 2215 | list_add(&entry->list, br_ip_list); |
| 2216 | count++; |
| 2217 | } |
| 2218 | } |
| 2219 | |
| 2220 | unlock: |
| 2221 | rcu_read_unlock(); |
| 2222 | return count; |
| 2223 | } |
| 2224 | EXPORT_SYMBOL_GPL(br_multicast_list_adjacent); |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 2225 | |
| 2226 | /** |
Linus Lüssing | c34963e | 2014-07-07 05:41:17 +0200 | [diff] [blame] | 2227 | * br_multicast_has_querier_anywhere - Checks for a querier on a bridge |
| 2228 | * @dev: The bridge port providing the bridge on which to check for a querier |
| 2229 | * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 |
| 2230 | * |
| 2231 | * Checks whether the given interface has a bridge on top and if so returns |
| 2232 | * true if a valid querier exists anywhere on the bridged link layer. |
| 2233 | * Otherwise returns false. |
| 2234 | */ |
| 2235 | bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto) |
| 2236 | { |
| 2237 | struct net_bridge *br; |
| 2238 | struct net_bridge_port *port; |
| 2239 | struct ethhdr eth; |
| 2240 | bool ret = false; |
| 2241 | |
| 2242 | rcu_read_lock(); |
Julian Wiedmann | 35f861e | 2019-03-29 14:38:19 +0100 | [diff] [blame] | 2243 | if (!netif_is_bridge_port(dev)) |
Linus Lüssing | c34963e | 2014-07-07 05:41:17 +0200 | [diff] [blame] | 2244 | goto unlock; |
| 2245 | |
| 2246 | port = br_port_get_rcu(dev); |
| 2247 | if (!port || !port->br) |
| 2248 | goto unlock; |
| 2249 | |
| 2250 | br = port->br; |
| 2251 | |
| 2252 | memset(ð, 0, sizeof(eth)); |
| 2253 | eth.h_proto = htons(proto); |
| 2254 | |
| 2255 | ret = br_multicast_querier_exists(br, ð); |
| 2256 | |
| 2257 | unlock: |
| 2258 | rcu_read_unlock(); |
| 2259 | return ret; |
| 2260 | } |
| 2261 | EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere); |
| 2262 | |
| 2263 | /** |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 2264 | * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port |
| 2265 | * @dev: The bridge port adjacent to which to check for a querier |
| 2266 | * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 |
| 2267 | * |
| 2268 | * Checks whether the given interface has a bridge on top and if so returns |
| 2269 | * true if a selected querier is behind one of the other ports of this |
| 2270 | * bridge. Otherwise returns false. |
| 2271 | */ |
| 2272 | bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto) |
| 2273 | { |
| 2274 | struct net_bridge *br; |
| 2275 | struct net_bridge_port *port; |
| 2276 | bool ret = false; |
| 2277 | |
| 2278 | rcu_read_lock(); |
Julian Wiedmann | 35f861e | 2019-03-29 14:38:19 +0100 | [diff] [blame] | 2279 | if (!netif_is_bridge_port(dev)) |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 2280 | goto unlock; |
| 2281 | |
| 2282 | port = br_port_get_rcu(dev); |
| 2283 | if (!port || !port->br) |
| 2284 | goto unlock; |
| 2285 | |
| 2286 | br = port->br; |
| 2287 | |
| 2288 | switch (proto) { |
| 2289 | case ETH_P_IP: |
| 2290 | if (!timer_pending(&br->ip4_other_query.timer) || |
| 2291 | rcu_dereference(br->ip4_querier.port) == port) |
| 2292 | goto unlock; |
| 2293 | break; |
Linus Lüssing | 3993c4e | 2014-06-12 01:41:24 +0200 | [diff] [blame] | 2294 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 2295 | case ETH_P_IPV6: |
| 2296 | if (!timer_pending(&br->ip6_other_query.timer) || |
| 2297 | rcu_dereference(br->ip6_querier.port) == port) |
| 2298 | goto unlock; |
| 2299 | break; |
Linus Lüssing | 3993c4e | 2014-06-12 01:41:24 +0200 | [diff] [blame] | 2300 | #endif |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 2301 | default: |
| 2302 | goto unlock; |
| 2303 | } |
| 2304 | |
| 2305 | ret = true; |
| 2306 | unlock: |
| 2307 | rcu_read_unlock(); |
| 2308 | return ret; |
| 2309 | } |
| 2310 | EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent); |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 2311 | |
| 2312 | static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats, |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 2313 | const struct sk_buff *skb, u8 type, u8 dir) |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 2314 | { |
| 2315 | struct bridge_mcast_stats *pstats = this_cpu_ptr(stats); |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 2316 | __be16 proto = skb->protocol; |
| 2317 | unsigned int t_len; |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 2318 | |
| 2319 | u64_stats_update_begin(&pstats->syncp); |
| 2320 | switch (proto) { |
| 2321 | case htons(ETH_P_IP): |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 2322 | t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb); |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 2323 | switch (type) { |
| 2324 | case IGMP_HOST_MEMBERSHIP_REPORT: |
| 2325 | pstats->mstats.igmp_v1reports[dir]++; |
| 2326 | break; |
| 2327 | case IGMPV2_HOST_MEMBERSHIP_REPORT: |
| 2328 | pstats->mstats.igmp_v2reports[dir]++; |
| 2329 | break; |
| 2330 | case IGMPV3_HOST_MEMBERSHIP_REPORT: |
| 2331 | pstats->mstats.igmp_v3reports[dir]++; |
| 2332 | break; |
| 2333 | case IGMP_HOST_MEMBERSHIP_QUERY: |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 2334 | if (t_len != sizeof(struct igmphdr)) { |
| 2335 | pstats->mstats.igmp_v3queries[dir]++; |
| 2336 | } else { |
| 2337 | unsigned int offset = skb_transport_offset(skb); |
| 2338 | struct igmphdr *ih, _ihdr; |
| 2339 | |
| 2340 | ih = skb_header_pointer(skb, offset, |
| 2341 | sizeof(_ihdr), &_ihdr); |
| 2342 | if (!ih) |
| 2343 | break; |
| 2344 | if (!ih->code) |
| 2345 | pstats->mstats.igmp_v1queries[dir]++; |
| 2346 | else |
| 2347 | pstats->mstats.igmp_v2queries[dir]++; |
| 2348 | } |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 2349 | break; |
| 2350 | case IGMP_HOST_LEAVE_MESSAGE: |
| 2351 | pstats->mstats.igmp_leaves[dir]++; |
| 2352 | break; |
| 2353 | } |
| 2354 | break; |
| 2355 | #if IS_ENABLED(CONFIG_IPV6) |
| 2356 | case htons(ETH_P_IPV6): |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 2357 | t_len = ntohs(ipv6_hdr(skb)->payload_len) + |
| 2358 | sizeof(struct ipv6hdr); |
| 2359 | t_len -= skb_network_header_len(skb); |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 2360 | switch (type) { |
| 2361 | case ICMPV6_MGM_REPORT: |
| 2362 | pstats->mstats.mld_v1reports[dir]++; |
| 2363 | break; |
| 2364 | case ICMPV6_MLD2_REPORT: |
| 2365 | pstats->mstats.mld_v2reports[dir]++; |
| 2366 | break; |
| 2367 | case ICMPV6_MGM_QUERY: |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 2368 | if (t_len != sizeof(struct mld_msg)) |
| 2369 | pstats->mstats.mld_v2queries[dir]++; |
| 2370 | else |
| 2371 | pstats->mstats.mld_v1queries[dir]++; |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 2372 | break; |
| 2373 | case ICMPV6_MGM_REDUCTION: |
| 2374 | pstats->mstats.mld_leaves[dir]++; |
| 2375 | break; |
| 2376 | } |
| 2377 | break; |
| 2378 | #endif /* CONFIG_IPV6 */ |
| 2379 | } |
| 2380 | u64_stats_update_end(&pstats->syncp); |
| 2381 | } |
| 2382 | |
| 2383 | void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p, |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 2384 | const struct sk_buff *skb, u8 type, u8 dir) |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 2385 | { |
| 2386 | struct bridge_mcast_stats __percpu *stats; |
| 2387 | |
| 2388 | /* if multicast_disabled is true then igmp type can't be set */ |
Nikolay Aleksandrov | 675779a | 2018-09-26 17:01:04 +0300 | [diff] [blame] | 2389 | if (!type || !br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)) |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 2390 | return; |
| 2391 | |
| 2392 | if (p) |
| 2393 | stats = p->mcast_stats; |
| 2394 | else |
| 2395 | stats = br->mcast_stats; |
| 2396 | if (WARN_ON(!stats)) |
| 2397 | return; |
| 2398 | |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 2399 | br_mcast_stats_add(stats, skb, type, dir); |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | int br_multicast_init_stats(struct net_bridge *br) |
| 2403 | { |
| 2404 | br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats); |
| 2405 | if (!br->mcast_stats) |
| 2406 | return -ENOMEM; |
| 2407 | |
| 2408 | return 0; |
| 2409 | } |
| 2410 | |
Ido Schimmel | b6fe044 | 2017-04-10 14:59:27 +0300 | [diff] [blame] | 2411 | void br_multicast_uninit_stats(struct net_bridge *br) |
| 2412 | { |
| 2413 | free_percpu(br->mcast_stats); |
| 2414 | } |
| 2415 | |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 2416 | static void mcast_stats_add_dir(u64 *dst, u64 *src) |
| 2417 | { |
| 2418 | dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX]; |
| 2419 | dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX]; |
| 2420 | } |
| 2421 | |
| 2422 | void br_multicast_get_stats(const struct net_bridge *br, |
| 2423 | const struct net_bridge_port *p, |
| 2424 | struct br_mcast_stats *dest) |
| 2425 | { |
| 2426 | struct bridge_mcast_stats __percpu *stats; |
| 2427 | struct br_mcast_stats tdst; |
| 2428 | int i; |
| 2429 | |
| 2430 | memset(dest, 0, sizeof(*dest)); |
| 2431 | if (p) |
| 2432 | stats = p->mcast_stats; |
| 2433 | else |
| 2434 | stats = br->mcast_stats; |
| 2435 | if (WARN_ON(!stats)) |
| 2436 | return; |
| 2437 | |
| 2438 | memset(&tdst, 0, sizeof(tdst)); |
| 2439 | for_each_possible_cpu(i) { |
| 2440 | struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i); |
| 2441 | struct br_mcast_stats temp; |
| 2442 | unsigned int start; |
| 2443 | |
| 2444 | do { |
| 2445 | start = u64_stats_fetch_begin_irq(&cpu_stats->syncp); |
| 2446 | memcpy(&temp, &cpu_stats->mstats, sizeof(temp)); |
| 2447 | } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start)); |
| 2448 | |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 2449 | mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries); |
| 2450 | mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries); |
| 2451 | mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries); |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 2452 | mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves); |
| 2453 | mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports); |
| 2454 | mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports); |
| 2455 | mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports); |
| 2456 | tdst.igmp_parse_errors += temp.igmp_parse_errors; |
| 2457 | |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 2458 | mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries); |
| 2459 | mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries); |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 2460 | mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves); |
| 2461 | mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports); |
| 2462 | mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports); |
| 2463 | tdst.mld_parse_errors += temp.mld_parse_errors; |
| 2464 | } |
| 2465 | memcpy(dest, &tdst, sizeof(*dest)); |
| 2466 | } |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 2467 | |
| 2468 | int br_mdb_hash_init(struct net_bridge *br) |
| 2469 | { |
| 2470 | return rhashtable_init(&br->mdb_hash_tbl, &br_mdb_rht_params); |
| 2471 | } |
| 2472 | |
| 2473 | void br_mdb_hash_fini(struct net_bridge *br) |
| 2474 | { |
| 2475 | rhashtable_destroy(&br->mdb_hash_tbl); |
| 2476 | } |