blob: 3d8deac2353d045964c52981a1eaac96d6f23025 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Herbert Xueb1d1642010-02-27 19:41:45 +00002/*
3 * Bridge multicast support.
4 *
5 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
Herbert Xueb1d1642010-02-27 19:41:45 +00006 */
7
8#include <linux/err.h>
Linus Lüssing07f8ac42014-06-07 18:26:28 +02009#include <linux/export.h>
Herbert Xueb1d1642010-02-27 19:41:45 +000010#include <linux/if_ether.h>
11#include <linux/igmp.h>
Linus Lüssing4b3087c2019-01-21 07:26:28 +010012#include <linux/in.h>
Herbert Xueb1d1642010-02-27 19:41:45 +000013#include <linux/jhash.h>
14#include <linux/kernel.h>
Herbert Xub1951672010-02-27 19:41:51 +000015#include <linux/log2.h>
Herbert Xueb1d1642010-02-27 19:41:45 +000016#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 Wang1c8ad5b2013-05-21 21:52:54 +000023#include <linux/inetdevice.h>
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +010024#include <linux/mroute.h>
Herbert Xueb1d1642010-02-27 19:41:45 +000025#include <net/ip.h>
Nogah Frankel147c1e92017-02-09 14:54:40 +010026#include <net/switchdev.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000027#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing4b3087c2019-01-21 07:26:28 +010028#include <linux/icmpv6.h>
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090029#include <net/ipv6.h>
30#include <net/mld.h>
David S. Millerd4c4f072010-04-27 10:16:54 -070031#include <net/ip6_checksum.h>
Linus Lüssing3c3769e2013-09-04 02:13:39 +020032#include <net/addrconf.h>
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090033#endif
Herbert Xueb1d1642010-02-27 19:41:45 +000034
35#include "br_private.h"
36
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +020037static 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 Aleksandrov19e3a9c2018-12-05 15:14:24 +020042};
43
Linus Lüssingcc0fdd82013-08-30 17:28:17 +020044static void br_multicast_start_querier(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +020045 struct bridge_mcast_own_query *query);
Satish Ashok754bc542015-06-19 01:22:57 -070046static void br_multicast_add_router(struct net_bridge *br,
47 struct net_bridge_port *port);
Satish Ashokbc8c20a2015-07-13 05:28:37 -070048static void br_ip4_multicast_leave_group(struct net_bridge *br,
49 struct net_bridge_port *port,
50 __be32 group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +010051 __u16 vid,
52 const unsigned char *src);
53
Nogah Frankelf12e7d92017-02-09 14:54:41 +010054static void __del_port_router(struct net_bridge_port *p);
Satish Ashokbc8c20a2015-07-13 05:28:37 -070055#if IS_ENABLED(CONFIG_IPV6)
56static void br_ip6_multicast_leave_group(struct net_bridge *br,
57 struct net_bridge_port *port,
58 const struct in6_addr *group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +010059 __u16 vid, const unsigned char *src);
Satish Ashokbc8c20a2015-07-13 05:28:37 -070060#endif
Herbert Xuc83b8fa2012-04-13 02:37:42 +000061
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +020062static struct net_bridge_mdb_entry *br_mdb_ip_get_rcu(struct net_bridge *br,
63 struct br_ip *dst)
Herbert Xueb1d1642010-02-27 19:41:45 +000064{
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +020065 return rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params);
Herbert Xueb1d1642010-02-27 19:41:45 +000066}
67
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +020068struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge *br,
Cong Wangcfd56752012-12-11 22:23:08 +000069 struct br_ip *dst)
Herbert Xu7f285fa2010-07-05 14:50:08 +000070{
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +020071 struct net_bridge_mdb_entry *ent;
Herbert Xu7f285fa2010-07-05 14:50:08 +000072
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +020073 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 Xu7f285fa2010-07-05 14:50:08 +000080}
81
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +020082static struct net_bridge_mdb_entry *br_mdb_ip4_get(struct net_bridge *br,
83 __be32 dst, __u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +000084{
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090085 struct br_ip br_dst;
Herbert Xu0821ec52010-03-15 20:38:25 -070086
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +020087 memset(&br_dst, 0, sizeof(br_dst));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090088 br_dst.u.ip4 = dst;
89 br_dst.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000090 br_dst.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090091
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +020092 return br_mdb_ip_get(br, &br_dst);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090093}
94
Eric Dumazetdfd56b82011-12-10 09:48:31 +000095#if IS_ENABLED(CONFIG_IPV6)
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +020096static struct net_bridge_mdb_entry *br_mdb_ip6_get(struct net_bridge *br,
97 const struct in6_addr *dst,
98 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090099{
100 struct br_ip br_dst;
101
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200102 memset(&br_dst, 0, sizeof(br_dst));
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000103 br_dst.u.ip6 = *dst;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900104 br_dst.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000105 br_dst.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900106
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200107 return br_mdb_ip_get(br, &br_dst);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900108}
109#endif
110
Herbert Xueb1d1642010-02-27 19:41:45 +0000111struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
Cong Wangfbca58a2013-03-07 03:05:33 +0000112 struct sk_buff *skb, u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000113{
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900114 struct br_ip ip;
115
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +0300116 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
Herbert Xueb1d1642010-02-27 19:41:45 +0000117 return NULL;
118
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900119 if (BR_INPUT_SKB_CB(skb)->igmp)
120 return NULL;
121
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200122 memset(&ip, 0, sizeof(ip));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900123 ip.proto = skb->protocol;
Cong Wangfbca58a2013-03-07 03:05:33 +0000124 ip.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900125
Herbert Xueb1d1642010-02-27 19:41:45 +0000126 switch (skb->protocol) {
127 case htons(ETH_P_IP):
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900128 ip.u.ip4 = ip_hdr(skb)->daddr;
129 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000130#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900131 case htons(ETH_P_IPV6):
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000132 ip.u.ip6 = ipv6_hdr(skb)->daddr;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900133 break;
134#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900135 default:
136 return NULL;
Herbert Xueb1d1642010-02-27 19:41:45 +0000137 }
138
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200139 return br_mdb_ip_get_rcu(br, &ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000140}
141
Allen Pais88c1f372017-11-03 11:51:10 +0530142static void br_multicast_group_expired(struct timer_list *t)
Herbert Xueb1d1642010-02-27 19:41:45 +0000143{
Allen Pais88c1f372017-11-03 11:51:10 +0530144 struct net_bridge_mdb_entry *mp = from_timer(mp, t, timer);
Herbert Xueb1d1642010-02-27 19:41:45 +0000145 struct net_bridge *br = mp->br;
Herbert Xueb1d1642010-02-27 19:41:45 +0000146
147 spin_lock(&br->multicast_lock);
148 if (!netif_running(br->dev) || timer_pending(&mp->timer))
149 goto out;
150
Andrew Lunnff0fd342017-11-09 23:10:57 +0100151 mp->host_joined = false;
Andrew Lunn2a260282017-11-09 23:10:58 +0100152 br_mdb_notify(br->dev, NULL, &mp->addr, RTM_DELMDB, 0);
Herbert Xueb1d1642010-02-27 19:41:45 +0000153
154 if (mp->ports)
155 goto out;
156
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200157 rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode,
158 br_mdb_rht_params);
159 hlist_del_rcu(&mp->mdb_node);
Herbert Xueb1d1642010-02-27 19:41:45 +0000160
Nikolay Aleksandrov43295962018-12-05 15:14:25 +0200161 kfree_rcu(mp, rcu);
Herbert Xueb1d1642010-02-27 19:41:45 +0000162
163out:
164 spin_unlock(&br->multicast_lock);
165}
166
167static void br_multicast_del_pg(struct net_bridge *br,
168 struct net_bridge_port_group *pg)
169{
Herbert Xueb1d1642010-02-27 19:41:45 +0000170 struct net_bridge_mdb_entry *mp;
171 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +0000172 struct net_bridge_port_group __rcu **pp;
173
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200174 mp = br_mdb_ip_get(br, &pg->addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000175 if (WARN_ON(!mp))
176 return;
177
Eric Dumazete8051682010-11-15 06:38:10 +0000178 for (pp = &mp->ports;
179 (p = mlock_dereference(*pp, br)) != NULL;
180 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000181 if (p != pg)
182 continue;
183
stephen hemminger83f6a742010-04-27 15:01:06 +0000184 rcu_assign_pointer(*pp, p->next);
Herbert Xueb1d1642010-02-27 19:41:45 +0000185 hlist_del_init(&p->mglist);
186 del_timer(&p->timer);
Elad Raz45ebcce2016-04-21 12:52:45 +0200187 br_mdb_notify(br->dev, p->port, &pg->addr, RTM_DELMDB,
188 p->flags);
Nikolay Aleksandrov43295962018-12-05 15:14:25 +0200189 kfree_rcu(p, rcu);
Herbert Xueb1d1642010-02-27 19:41:45 +0000190
Andrew Lunnff0fd342017-11-09 23:10:57 +0100191 if (!mp->ports && !mp->host_joined &&
Herbert Xueb1d1642010-02-27 19:41:45 +0000192 netif_running(br->dev))
193 mod_timer(&mp->timer, jiffies);
194
195 return;
196 }
197
198 WARN_ON(1);
199}
200
Allen Pais88c1f372017-11-03 11:51:10 +0530201static void br_multicast_port_group_expired(struct timer_list *t)
Herbert Xueb1d1642010-02-27 19:41:45 +0000202{
Allen Pais88c1f372017-11-03 11:51:10 +0530203 struct net_bridge_port_group *pg = from_timer(pg, t, timer);
Herbert Xueb1d1642010-02-27 19:41:45 +0000204 struct net_bridge *br = pg->port->br;
205
206 spin_lock(&br->multicast_lock);
207 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
Elad Raz9d06b6d2016-02-03 09:57:05 +0100208 hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT)
Herbert Xueb1d1642010-02-27 19:41:45 +0000209 goto out;
210
211 br_multicast_del_pg(br, pg);
212
213out:
214 spin_unlock(&br->multicast_lock);
215}
216
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900217static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200218 __be32 group,
219 u8 *igmp_type)
Herbert Xueb1d1642010-02-27 19:41:45 +0000220{
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100221 struct igmpv3_query *ihv3;
222 size_t igmp_hdr_size;
Herbert Xueb1d1642010-02-27 19:41:45 +0000223 struct sk_buff *skb;
224 struct igmphdr *ih;
225 struct ethhdr *eth;
226 struct iphdr *iph;
227
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100228 igmp_hdr_size = sizeof(*ih);
229 if (br->multicast_igmp_version == 3)
230 igmp_hdr_size = sizeof(*ihv3);
Herbert Xueb1d1642010-02-27 19:41:45 +0000231 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100232 igmp_hdr_size + 4);
Herbert Xueb1d1642010-02-27 19:41:45 +0000233 if (!skb)
234 goto out;
235
236 skb->protocol = htons(ETH_P_IP);
237
238 skb_reset_mac_header(skb);
239 eth = eth_hdr(skb);
240
Joe Perchese5a727f2014-02-23 00:05:25 -0800241 ether_addr_copy(eth->h_source, br->dev->dev_addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000242 eth->h_dest[0] = 1;
243 eth->h_dest[1] = 0;
244 eth->h_dest[2] = 0x5e;
245 eth->h_dest[3] = 0;
246 eth->h_dest[4] = 0;
247 eth->h_dest[5] = 1;
248 eth->h_proto = htons(ETH_P_IP);
249 skb_put(skb, sizeof(*eth));
250
251 skb_set_network_header(skb, skb->len);
252 iph = ip_hdr(skb);
253
254 iph->version = 4;
255 iph->ihl = 6;
256 iph->tos = 0xc0;
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100257 iph->tot_len = htons(sizeof(*iph) + igmp_hdr_size + 4);
Herbert Xueb1d1642010-02-27 19:41:45 +0000258 iph->id = 0;
259 iph->frag_off = htons(IP_DF);
260 iph->ttl = 1;
261 iph->protocol = IPPROTO_IGMP;
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +0300262 iph->saddr = br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR) ?
Cong Wang1c8ad5b2013-05-21 21:52:54 +0000263 inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000264 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
265 ((u8 *)&iph[1])[0] = IPOPT_RA;
266 ((u8 *)&iph[1])[1] = 4;
267 ((u8 *)&iph[1])[2] = 0;
268 ((u8 *)&iph[1])[3] = 0;
269 ip_send_check(iph);
270 skb_put(skb, 24);
271
272 skb_set_transport_header(skb, skb->len);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200273 *igmp_type = IGMP_HOST_MEMBERSHIP_QUERY;
Herbert Xueb1d1642010-02-27 19:41:45 +0000274
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100275 switch (br->multicast_igmp_version) {
276 case 2:
277 ih = igmp_hdr(skb);
278 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
279 ih->code = (group ? br->multicast_last_member_interval :
280 br->multicast_query_response_interval) /
281 (HZ / IGMP_TIMER_SCALE);
282 ih->group = group;
283 ih->csum = 0;
284 ih->csum = ip_compute_csum((void *)ih, sizeof(*ih));
285 break;
286 case 3:
287 ihv3 = igmpv3_query_hdr(skb);
288 ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY;
289 ihv3->code = (group ? br->multicast_last_member_interval :
290 br->multicast_query_response_interval) /
291 (HZ / IGMP_TIMER_SCALE);
292 ihv3->group = group;
293 ihv3->qqic = br->multicast_query_interval / HZ;
294 ihv3->nsrcs = 0;
295 ihv3->resv = 0;
296 ihv3->suppress = 0;
297 ihv3->qrv = 2;
298 ihv3->csum = 0;
299 ihv3->csum = ip_compute_csum((void *)ihv3, sizeof(*ihv3));
300 break;
301 }
302
303 skb_put(skb, igmp_hdr_size);
Herbert Xueb1d1642010-02-27 19:41:45 +0000304 __skb_pull(skb, sizeof(*eth));
305
306out:
307 return skb;
308}
309
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000310#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900311static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200312 const struct in6_addr *grp,
313 u8 *igmp_type)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900314{
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100315 struct mld2_query *mld2q;
316 unsigned long interval;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900317 struct ipv6hdr *ip6h;
318 struct mld_msg *mldq;
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100319 size_t mld_hdr_size;
320 struct sk_buff *skb;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900321 struct ethhdr *eth;
322 u8 *hopopt;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900323
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100324 mld_hdr_size = sizeof(*mldq);
325 if (br->multicast_mld_version == 2)
326 mld_hdr_size = sizeof(*mld2q);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900327 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100328 8 + mld_hdr_size);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900329 if (!skb)
330 goto out;
331
332 skb->protocol = htons(ETH_P_IPV6);
333
334 /* Ethernet header */
335 skb_reset_mac_header(skb);
336 eth = eth_hdr(skb);
337
Joe Perchese5a727f2014-02-23 00:05:25 -0800338 ether_addr_copy(eth->h_source, br->dev->dev_addr);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900339 eth->h_proto = htons(ETH_P_IPV6);
340 skb_put(skb, sizeof(*eth));
341
342 /* IPv6 header + HbH option */
343 skb_set_network_header(skb, skb->len);
344 ip6h = ipv6_hdr(skb);
345
346 *(__force __be32 *)ip6h = htonl(0x60000000);
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100347 ip6h->payload_len = htons(8 + mld_hdr_size);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900348 ip6h->nexthdr = IPPROTO_HOPOPTS;
349 ip6h->hop_limit = 1;
Linus Lüssinga7bff752011-03-22 11:40:32 +0000350 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
Ulrich Weberd1d81d42012-03-05 04:52:44 +0000351 if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
352 &ip6h->saddr)) {
353 kfree_skb(skb);
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +0300354 br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, false);
Ulrich Weberd1d81d42012-03-05 04:52:44 +0000355 return NULL;
356 }
daniel0888d5f2016-06-24 12:35:18 +0200357
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +0300358 br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true);
Linus Lüssing36cff5a2011-02-17 08:17:51 +0000359 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900360
361 hopopt = (u8 *)(ip6h + 1);
362 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
363 hopopt[1] = 0; /* length of HbH */
364 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
365 hopopt[3] = 2; /* Length of RA Option */
366 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
367 hopopt[5] = 0;
Eldad Zack1de5a712012-05-17 06:00:25 +0000368 hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */
369 hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900370
371 skb_put(skb, sizeof(*ip6h) + 8);
372
373 /* ICMPv6 */
374 skb_set_transport_header(skb, skb->len);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200375 interval = ipv6_addr_any(grp) ?
Linus Lüssing32de8682013-06-16 23:20:34 +0200376 br->multicast_query_response_interval :
377 br->multicast_last_member_interval;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200378 *igmp_type = ICMPV6_MGM_QUERY;
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100379 switch (br->multicast_mld_version) {
380 case 1:
381 mldq = (struct mld_msg *)icmp6_hdr(skb);
382 mldq->mld_type = ICMPV6_MGM_QUERY;
383 mldq->mld_code = 0;
384 mldq->mld_cksum = 0;
385 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
386 mldq->mld_reserved = 0;
387 mldq->mld_mca = *grp;
388 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
389 sizeof(*mldq), IPPROTO_ICMPV6,
390 csum_partial(mldq,
391 sizeof(*mldq),
392 0));
393 break;
394 case 2:
395 mld2q = (struct mld2_query *)icmp6_hdr(skb);
Lance Richardson53631a52017-01-16 18:11:35 -0500396 mld2q->mld2q_mrc = htons((u16)jiffies_to_msecs(interval));
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100397 mld2q->mld2q_type = ICMPV6_MGM_QUERY;
398 mld2q->mld2q_code = 0;
399 mld2q->mld2q_cksum = 0;
400 mld2q->mld2q_resv1 = 0;
401 mld2q->mld2q_resv2 = 0;
402 mld2q->mld2q_suppress = 0;
403 mld2q->mld2q_qrv = 2;
404 mld2q->mld2q_nsrcs = 0;
405 mld2q->mld2q_qqic = br->multicast_query_interval / HZ;
406 mld2q->mld2q_mca = *grp;
407 mld2q->mld2q_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
408 sizeof(*mld2q),
409 IPPROTO_ICMPV6,
410 csum_partial(mld2q,
411 sizeof(*mld2q),
412 0));
413 break;
414 }
415 skb_put(skb, mld_hdr_size);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900416
417 __skb_pull(skb, sizeof(*eth));
418
419out:
420 return skb;
421}
422#endif
423
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900424static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200425 struct br_ip *addr,
426 u8 *igmp_type)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900427{
428 switch (addr->proto) {
429 case htons(ETH_P_IP):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200430 return br_ip4_multicast_alloc_query(br, addr->u.ip4, igmp_type);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000431#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900432 case htons(ETH_P_IPV6):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200433 return br_ip6_multicast_alloc_query(br, &addr->u.ip6,
434 igmp_type);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900435#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900436 }
437 return NULL;
438}
439
Cong Wangcfd56752012-12-11 22:23:08 +0000440struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100441 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000442{
Herbert Xueb1d1642010-02-27 19:41:45 +0000443 struct net_bridge_mdb_entry *mp;
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000444 int err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000445
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200446 mp = br_mdb_ip_get(br, group);
447 if (mp)
448 return mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000449
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200450 if (atomic_read(&br->mdb_hash_tbl.nelems) >= br->hash_max) {
451 br_opt_toggle(br, BROPT_MULTICAST_ENABLED, false);
452 return ERR_PTR(-E2BIG);
Herbert Xueb1d1642010-02-27 19:41:45 +0000453 }
454
455 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
456 if (unlikely(!mp))
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000457 return ERR_PTR(-ENOMEM);
Herbert Xueb1d1642010-02-27 19:41:45 +0000458
459 mp->br = br;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900460 mp->addr = *group;
Allen Pais88c1f372017-11-03 11:51:10 +0530461 timer_setup(&mp->timer, br_multicast_group_expired, 0);
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200462 err = rhashtable_lookup_insert_fast(&br->mdb_hash_tbl, &mp->rhnode,
463 br_mdb_rht_params);
464 if (err) {
465 kfree(mp);
466 mp = ERR_PTR(err);
467 } else {
468 hlist_add_head_rcu(&mp->mdb_node, &br->mdb_list);
469 }
Eric Dumazet1faabf22013-07-19 20:07:16 -0700470
Herbert Xueb1d1642010-02-27 19:41:45 +0000471 return mp;
472}
473
Cong Wangcfd56752012-12-11 22:23:08 +0000474struct net_bridge_port_group *br_multicast_new_port_group(
475 struct net_bridge_port *port,
476 struct br_ip *group,
Amerigo Wangccb1c312012-12-14 22:09:51 +0000477 struct net_bridge_port_group __rcu *next,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100478 unsigned char flags,
479 const unsigned char *src)
Cong Wangcfd56752012-12-11 22:23:08 +0000480{
481 struct net_bridge_port_group *p;
482
483 p = kzalloc(sizeof(*p), GFP_ATOMIC);
484 if (unlikely(!p))
485 return NULL;
486
487 p->addr = *group;
488 p->port = port;
Elad Raz9d06b6d2016-02-03 09:57:05 +0100489 p->flags = flags;
stephen hemmingereca2a432012-12-13 06:51:28 +0000490 rcu_assign_pointer(p->next, next);
Cong Wangcfd56752012-12-11 22:23:08 +0000491 hlist_add_head(&p->mglist, &port->mglist);
Allen Pais88c1f372017-11-03 11:51:10 +0530492 timer_setup(&p->timer, br_multicast_port_group_expired, 0);
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100493
494 if (src)
495 memcpy(p->eth_addr, src, ETH_ALEN);
496 else
Mao Wenan1bfe45f2019-03-20 10:06:57 +0800497 eth_broadcast_addr(p->eth_addr);
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100498
Cong Wangcfd56752012-12-11 22:23:08 +0000499 return p;
500}
501
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100502static bool br_port_group_equal(struct net_bridge_port_group *p,
503 struct net_bridge_port *port,
504 const unsigned char *src)
505{
506 if (p->port != port)
507 return false;
508
509 if (!(port->flags & BR_MULTICAST_TO_UNICAST))
510 return true;
511
512 return ether_addr_equal(src, p->eth_addr);
513}
514
Herbert Xueb1d1642010-02-27 19:41:45 +0000515static int br_multicast_add_group(struct net_bridge *br,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900516 struct net_bridge_port *port,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100517 struct br_ip *group,
518 const unsigned char *src)
Herbert Xueb1d1642010-02-27 19:41:45 +0000519{
Eric Dumazete8051682010-11-15 06:38:10 +0000520 struct net_bridge_port_group __rcu **pp;
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100521 struct net_bridge_port_group *p;
522 struct net_bridge_mdb_entry *mp;
Linus Lüssing454594f2013-10-20 00:58:57 +0200523 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +0000524 int err;
525
Herbert Xueb1d1642010-02-27 19:41:45 +0000526 spin_lock(&br->multicast_lock);
527 if (!netif_running(br->dev) ||
528 (port && port->state == BR_STATE_DISABLED))
529 goto out;
530
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200531 mp = br_multicast_new_group(br, group);
Herbert Xueb1d1642010-02-27 19:41:45 +0000532 err = PTR_ERR(mp);
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000533 if (IS_ERR(mp))
Herbert Xueb1d1642010-02-27 19:41:45 +0000534 goto err;
535
536 if (!port) {
Andrew Lunn2a260282017-11-09 23:10:58 +0100537 if (!mp->host_joined) {
538 mp->host_joined = true;
539 br_mdb_notify(br->dev, NULL, &mp->addr, RTM_NEWMDB, 0);
540 }
Linus Lüssing454594f2013-10-20 00:58:57 +0200541 mod_timer(&mp->timer, now + br->multicast_membership_interval);
Herbert Xueb1d1642010-02-27 19:41:45 +0000542 goto out;
543 }
544
Eric Dumazete8051682010-11-15 06:38:10 +0000545 for (pp = &mp->ports;
546 (p = mlock_dereference(*pp, br)) != NULL;
547 pp = &p->next) {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100548 if (br_port_group_equal(p, port, src))
Linus Lüssing454594f2013-10-20 00:58:57 +0200549 goto found;
Herbert Xueb1d1642010-02-27 19:41:45 +0000550 if ((unsigned long)p->port < (unsigned long)port)
551 break;
552 }
553
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100554 p = br_multicast_new_port_group(port, group, *pp, 0, src);
Herbert Xueb1d1642010-02-27 19:41:45 +0000555 if (unlikely(!p))
556 goto err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000557 rcu_assign_pointer(*pp, p);
Elad Raz45ebcce2016-04-21 12:52:45 +0200558 br_mdb_notify(br->dev, port, group, RTM_NEWMDB, 0);
Herbert Xueb1d1642010-02-27 19:41:45 +0000559
Linus Lüssing454594f2013-10-20 00:58:57 +0200560found:
561 mod_timer(&p->timer, now + br->multicast_membership_interval);
Herbert Xueb1d1642010-02-27 19:41:45 +0000562out:
563 err = 0;
564
565err:
566 spin_unlock(&br->multicast_lock);
567 return err;
568}
569
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900570static int br_ip4_multicast_add_group(struct net_bridge *br,
571 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000572 __be32 group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100573 __u16 vid,
574 const unsigned char *src)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900575{
576 struct br_ip br_group;
577
578 if (ipv4_is_local_multicast(group))
579 return 0;
580
Nikolay Aleksandrov1515a63f2019-04-03 23:27:24 +0300581 memset(&br_group, 0, sizeof(br_group));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900582 br_group.u.ip4 = group;
583 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000584 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900585
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100586 return br_multicast_add_group(br, port, &br_group, src);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900587}
588
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000589#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900590static int br_ip6_multicast_add_group(struct net_bridge *br,
591 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000592 const struct in6_addr *group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100593 __u16 vid,
594 const unsigned char *src)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900595{
596 struct br_ip br_group;
597
Linus Lüssing3c3769e2013-09-04 02:13:39 +0200598 if (ipv6_addr_is_ll_all_nodes(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900599 return 0;
600
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200601 memset(&br_group, 0, sizeof(br_group));
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000602 br_group.u.ip6 = *group;
Linus Lüssing9cc6e0c2011-02-15 13:19:17 +0000603 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000604 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900605
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100606 return br_multicast_add_group(br, port, &br_group, src);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900607}
608#endif
609
Allen Pais88c1f372017-11-03 11:51:10 +0530610static void br_multicast_router_expired(struct timer_list *t)
Herbert Xueb1d1642010-02-27 19:41:45 +0000611{
Allen Pais88c1f372017-11-03 11:51:10 +0530612 struct net_bridge_port *port =
613 from_timer(port, t, multicast_router_timer);
Herbert Xueb1d1642010-02-27 19:41:45 +0000614 struct net_bridge *br = port->br;
615
616 spin_lock(&br->multicast_lock);
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +0100617 if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
618 port->multicast_router == MDB_RTR_TYPE_PERM ||
Nogah Frankelf12e7d92017-02-09 14:54:41 +0100619 timer_pending(&port->multicast_router_timer))
Herbert Xueb1d1642010-02-27 19:41:45 +0000620 goto out;
621
Nogah Frankelf12e7d92017-02-09 14:54:41 +0100622 __del_port_router(port);
Herbert Xueb1d1642010-02-27 19:41:45 +0000623out:
624 spin_unlock(&br->multicast_lock);
625}
626
Yotam Gigi77041422017-10-09 11:15:31 +0200627static void br_mc_router_state_change(struct net_bridge *p,
628 bool is_mc_router)
629{
630 struct switchdev_attr attr = {
631 .orig_dev = p->dev,
632 .id = SWITCHDEV_ATTR_ID_BRIDGE_MROUTER,
633 .flags = SWITCHDEV_F_DEFER,
634 .u.mrouter = is_mc_router,
635 };
636
637 switchdev_port_attr_set(p->dev, &attr);
638}
639
Allen Pais88c1f372017-11-03 11:51:10 +0530640static void br_multicast_local_router_expired(struct timer_list *t)
Herbert Xueb1d1642010-02-27 19:41:45 +0000641{
Allen Pais88c1f372017-11-03 11:51:10 +0530642 struct net_bridge *br = from_timer(br, t, multicast_router_timer);
Yotam Gigi77041422017-10-09 11:15:31 +0200643
644 spin_lock(&br->multicast_lock);
645 if (br->multicast_router == MDB_RTR_TYPE_DISABLED ||
646 br->multicast_router == MDB_RTR_TYPE_PERM ||
647 timer_pending(&br->multicast_router_timer))
648 goto out;
649
650 br_mc_router_state_change(br, false);
651out:
652 spin_unlock(&br->multicast_lock);
Herbert Xueb1d1642010-02-27 19:41:45 +0000653}
654
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200655static void br_multicast_querier_expired(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +0200656 struct bridge_mcast_own_query *query)
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000657{
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000658 spin_lock(&br->multicast_lock);
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +0300659 if (!netif_running(br->dev) || !br_opt_get(br, BROPT_MULTICAST_ENABLED))
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000660 goto out;
661
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200662 br_multicast_start_querier(br, query);
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000663
664out:
665 spin_unlock(&br->multicast_lock);
666}
667
Allen Pais88c1f372017-11-03 11:51:10 +0530668static void br_ip4_multicast_querier_expired(struct timer_list *t)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200669{
Allen Pais88c1f372017-11-03 11:51:10 +0530670 struct net_bridge *br = from_timer(br, t, ip4_other_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200671
Linus Lüssing90010b32014-06-07 18:26:26 +0200672 br_multicast_querier_expired(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200673}
674
675#if IS_ENABLED(CONFIG_IPV6)
Allen Pais88c1f372017-11-03 11:51:10 +0530676static void br_ip6_multicast_querier_expired(struct timer_list *t)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200677{
Allen Pais88c1f372017-11-03 11:51:10 +0530678 struct net_bridge *br = from_timer(br, t, ip6_other_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200679
Linus Lüssing90010b32014-06-07 18:26:26 +0200680 br_multicast_querier_expired(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200681}
682#endif
683
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200684static void br_multicast_select_own_querier(struct net_bridge *br,
685 struct br_ip *ip,
686 struct sk_buff *skb)
687{
688 if (ip->proto == htons(ETH_P_IP))
689 br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr;
690#if IS_ENABLED(CONFIG_IPV6)
691 else
692 br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr;
693#endif
694}
695
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900696static void __br_multicast_send_query(struct net_bridge *br,
697 struct net_bridge_port *port,
698 struct br_ip *ip)
Herbert Xueb1d1642010-02-27 19:41:45 +0000699{
Herbert Xueb1d1642010-02-27 19:41:45 +0000700 struct sk_buff *skb;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200701 u8 igmp_type;
Herbert Xueb1d1642010-02-27 19:41:45 +0000702
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200703 skb = br_multicast_alloc_query(br, ip, &igmp_type);
Herbert Xueb1d1642010-02-27 19:41:45 +0000704 if (!skb)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900705 return;
Herbert Xueb1d1642010-02-27 19:41:45 +0000706
707 if (port) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000708 skb->dev = port->dev;
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -0700709 br_multicast_count(br, port, skb, igmp_type,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200710 BR_MCAST_DIR_TX);
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500711 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
712 dev_net(port->dev), NULL, skb, NULL, skb->dev,
Linus Lüssingf0b4eec2014-11-17 12:20:28 +0100713 br_dev_queue_push_xmit);
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200714 } else {
715 br_multicast_select_own_querier(br, ip, skb);
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -0700716 br_multicast_count(br, port, skb, igmp_type,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200717 BR_MCAST_DIR_RX);
Herbert Xueb1d1642010-02-27 19:41:45 +0000718 netif_rx(skb);
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200719 }
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900720}
Herbert Xueb1d1642010-02-27 19:41:45 +0000721
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900722static void br_multicast_send_query(struct net_bridge *br,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200723 struct net_bridge_port *port,
Linus Lüssing90010b32014-06-07 18:26:26 +0200724 struct bridge_mcast_own_query *own_query)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900725{
Linus Lüssing90010b32014-06-07 18:26:26 +0200726 struct bridge_mcast_other_query *other_query = NULL;
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100727 struct br_ip br_group;
728 unsigned long time;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900729
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +0300730 if (!netif_running(br->dev) ||
731 !br_opt_get(br, BROPT_MULTICAST_ENABLED) ||
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +0300732 !br_opt_get(br, BROPT_MULTICAST_QUERIER))
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900733 return;
734
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900735 memset(&br_group.u, 0, sizeof(br_group.u));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900736
Linus Lüssing90010b32014-06-07 18:26:26 +0200737 if (port ? (own_query == &port->ip4_own_query) :
738 (own_query == &br->ip4_own_query)) {
739 other_query = &br->ip4_other_query;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200740 br_group.proto = htons(ETH_P_IP);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000741#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200742 } else {
Linus Lüssing90010b32014-06-07 18:26:26 +0200743 other_query = &br->ip6_other_query;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200744 br_group.proto = htons(ETH_P_IPV6);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900745#endif
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200746 }
747
Linus Lüssing90010b32014-06-07 18:26:26 +0200748 if (!other_query || timer_pending(&other_query->timer))
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200749 return;
750
751 __br_multicast_send_query(br, port, &br_group);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900752
Herbert Xueb1d1642010-02-27 19:41:45 +0000753 time = jiffies;
Linus Lüssing90010b32014-06-07 18:26:26 +0200754 time += own_query->startup_sent < br->multicast_startup_query_count ?
Herbert Xueb1d1642010-02-27 19:41:45 +0000755 br->multicast_startup_query_interval :
756 br->multicast_query_interval;
Linus Lüssing90010b32014-06-07 18:26:26 +0200757 mod_timer(&own_query->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +0000758}
759
Linus Lüssing90010b32014-06-07 18:26:26 +0200760static void
761br_multicast_port_query_expired(struct net_bridge_port *port,
762 struct bridge_mcast_own_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +0000763{
Herbert Xueb1d1642010-02-27 19:41:45 +0000764 struct net_bridge *br = port->br;
765
766 spin_lock(&br->multicast_lock);
Dan Carpenter02a780c2010-03-06 01:14:09 +0000767 if (port->state == BR_STATE_DISABLED ||
768 port->state == BR_STATE_BLOCKING)
Herbert Xueb1d1642010-02-27 19:41:45 +0000769 goto out;
770
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200771 if (query->startup_sent < br->multicast_startup_query_count)
772 query->startup_sent++;
Herbert Xueb1d1642010-02-27 19:41:45 +0000773
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200774 br_multicast_send_query(port->br, port, query);
Herbert Xueb1d1642010-02-27 19:41:45 +0000775
776out:
777 spin_unlock(&br->multicast_lock);
778}
779
Allen Pais88c1f372017-11-03 11:51:10 +0530780static void br_ip4_multicast_port_query_expired(struct timer_list *t)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200781{
Allen Pais88c1f372017-11-03 11:51:10 +0530782 struct net_bridge_port *port = from_timer(port, t, ip4_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200783
Linus Lüssing90010b32014-06-07 18:26:26 +0200784 br_multicast_port_query_expired(port, &port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200785}
786
787#if IS_ENABLED(CONFIG_IPV6)
Allen Pais88c1f372017-11-03 11:51:10 +0530788static void br_ip6_multicast_port_query_expired(struct timer_list *t)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200789{
Allen Pais88c1f372017-11-03 11:51:10 +0530790 struct net_bridge_port *port = from_timer(port, t, ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200791
Linus Lüssing90010b32014-06-07 18:26:26 +0200792 br_multicast_port_query_expired(port, &port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200793}
794#endif
795
Nogah Frankel147c1e92017-02-09 14:54:40 +0100796static void br_mc_disabled_update(struct net_device *dev, bool value)
797{
798 struct switchdev_attr attr = {
799 .orig_dev = dev,
800 .id = SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED,
801 .flags = SWITCHDEV_F_DEFER,
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +0300802 .u.mc_disabled = !value,
Nogah Frankel147c1e92017-02-09 14:54:40 +0100803 };
804
805 switchdev_port_attr_set(dev, &attr);
806}
807
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200808int br_multicast_add_port(struct net_bridge_port *port)
Herbert Xueb1d1642010-02-27 19:41:45 +0000809{
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +0100810 port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Herbert Xueb1d1642010-02-27 19:41:45 +0000811
Allen Pais88c1f372017-11-03 11:51:10 +0530812 timer_setup(&port->multicast_router_timer,
813 br_multicast_router_expired, 0);
814 timer_setup(&port->ip4_own_query.timer,
815 br_ip4_multicast_port_query_expired, 0);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200816#if IS_ENABLED(CONFIG_IPV6)
Allen Pais88c1f372017-11-03 11:51:10 +0530817 timer_setup(&port->ip6_own_query.timer,
818 br_ip6_multicast_port_query_expired, 0);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200819#endif
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +0300820 br_mc_disabled_update(port->dev,
821 br_opt_get(port->br, BROPT_MULTICAST_ENABLED));
Nogah Frankel147c1e92017-02-09 14:54:40 +0100822
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200823 port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
824 if (!port->mcast_stats)
825 return -ENOMEM;
826
827 return 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000828}
829
830void br_multicast_del_port(struct net_bridge_port *port)
831{
Satish Ashoke10177a2015-07-15 07:16:51 -0700832 struct net_bridge *br = port->br;
833 struct net_bridge_port_group *pg;
834 struct hlist_node *n;
835
836 /* Take care of the remaining groups, only perm ones should be left */
837 spin_lock_bh(&br->multicast_lock);
838 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
839 br_multicast_del_pg(br, pg);
840 spin_unlock_bh(&br->multicast_lock);
Herbert Xueb1d1642010-02-27 19:41:45 +0000841 del_timer_sync(&port->multicast_router_timer);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200842 free_percpu(port->mcast_stats);
Herbert Xueb1d1642010-02-27 19:41:45 +0000843}
844
Linus Lüssing90010b32014-06-07 18:26:26 +0200845static void br_multicast_enable(struct bridge_mcast_own_query *query)
Herbert Xu561f1102010-02-27 19:41:50 +0000846{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200847 query->startup_sent = 0;
Herbert Xu561f1102010-02-27 19:41:50 +0000848
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200849 if (try_to_del_timer_sync(&query->timer) >= 0 ||
850 del_timer(&query->timer))
851 mod_timer(&query->timer, jiffies);
Herbert Xu561f1102010-02-27 19:41:50 +0000852}
853
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +0200854static void __br_multicast_enable_port(struct net_bridge_port *port)
Herbert Xueb1d1642010-02-27 19:41:45 +0000855{
856 struct net_bridge *br = port->br;
857
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +0300858 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED) || !netif_running(br->dev))
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +0200859 return;
Herbert Xueb1d1642010-02-27 19:41:45 +0000860
Linus Lüssing90010b32014-06-07 18:26:26 +0200861 br_multicast_enable(&port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200862#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +0200863 br_multicast_enable(&port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200864#endif
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +0100865 if (port->multicast_router == MDB_RTR_TYPE_PERM &&
866 hlist_unhashed(&port->rlist))
Satish Ashok754bc542015-06-19 01:22:57 -0700867 br_multicast_add_router(br, port);
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +0200868}
Herbert Xueb1d1642010-02-27 19:41:45 +0000869
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +0200870void br_multicast_enable_port(struct net_bridge_port *port)
871{
872 struct net_bridge *br = port->br;
873
874 spin_lock(&br->multicast_lock);
875 __br_multicast_enable_port(port);
Herbert Xueb1d1642010-02-27 19:41:45 +0000876 spin_unlock(&br->multicast_lock);
877}
878
879void br_multicast_disable_port(struct net_bridge_port *port)
880{
881 struct net_bridge *br = port->br;
882 struct net_bridge_port_group *pg;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800883 struct hlist_node *n;
Herbert Xueb1d1642010-02-27 19:41:45 +0000884
885 spin_lock(&br->multicast_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800886 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
Elad Raz9d06b6d2016-02-03 09:57:05 +0100887 if (!(pg->flags & MDB_PG_FLAGS_PERMANENT))
Satish Ashoke10177a2015-07-15 07:16:51 -0700888 br_multicast_del_pg(br, pg);
Herbert Xueb1d1642010-02-27 19:41:45 +0000889
Nogah Frankelf12e7d92017-02-09 14:54:41 +0100890 __del_port_router(port);
891
Herbert Xueb1d1642010-02-27 19:41:45 +0000892 del_timer(&port->multicast_router_timer);
Linus Lüssing90010b32014-06-07 18:26:26 +0200893 del_timer(&port->ip4_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200894#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +0200895 del_timer(&port->ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200896#endif
Herbert Xueb1d1642010-02-27 19:41:45 +0000897 spin_unlock(&br->multicast_lock);
898}
899
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900900static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
901 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -0400902 struct sk_buff *skb,
903 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000904{
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100905 const unsigned char *src;
Herbert Xueb1d1642010-02-27 19:41:45 +0000906 struct igmpv3_report *ih;
907 struct igmpv3_grec *grec;
908 int i;
909 int len;
910 int num;
911 int type;
912 int err = 0;
913 __be32 group;
Nikolay Aleksandrove57f6182019-07-02 15:00:18 +0300914 u16 nsrcs;
Herbert Xueb1d1642010-02-27 19:41:45 +0000915
Herbert Xueb1d1642010-02-27 19:41:45 +0000916 ih = igmpv3_report_hdr(skb);
917 num = ntohs(ih->ngrec);
Linus Lüssingc2d4fbd2015-09-11 18:39:48 +0200918 len = skb_transport_offset(skb) + sizeof(*ih);
Herbert Xueb1d1642010-02-27 19:41:45 +0000919
920 for (i = 0; i < num; i++) {
921 len += sizeof(*grec);
Linus Lüssingba5ea6142019-01-21 07:26:25 +0100922 if (!ip_mc_may_pull(skb, len))
Herbert Xueb1d1642010-02-27 19:41:45 +0000923 return -EINVAL;
924
Herbert Xufd218cf2010-04-07 21:20:47 -0700925 grec = (void *)(skb->data + len - sizeof(*grec));
Herbert Xueb1d1642010-02-27 19:41:45 +0000926 group = grec->grec_mca;
927 type = grec->grec_type;
Nikolay Aleksandrove57f6182019-07-02 15:00:18 +0300928 nsrcs = ntohs(grec->grec_nsrcs);
Herbert Xueb1d1642010-02-27 19:41:45 +0000929
Nikolay Aleksandrove57f6182019-07-02 15:00:18 +0300930 len += nsrcs * 4;
Linus Lüssingba5ea6142019-01-21 07:26:25 +0100931 if (!ip_mc_may_pull(skb, len))
Herbert Xueb1d1642010-02-27 19:41:45 +0000932 return -EINVAL;
933
934 /* We treat this as an IGMPv2 report for now. */
935 switch (type) {
936 case IGMPV3_MODE_IS_INCLUDE:
937 case IGMPV3_MODE_IS_EXCLUDE:
938 case IGMPV3_CHANGE_TO_INCLUDE:
939 case IGMPV3_CHANGE_TO_EXCLUDE:
940 case IGMPV3_ALLOW_NEW_SOURCES:
941 case IGMPV3_BLOCK_OLD_SOURCES:
942 break;
943
944 default:
945 continue;
946 }
947
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100948 src = eth_hdr(skb)->h_source;
Satish Ashokbc8c20a2015-07-13 05:28:37 -0700949 if ((type == IGMPV3_CHANGE_TO_INCLUDE ||
950 type == IGMPV3_MODE_IS_INCLUDE) &&
Nikolay Aleksandrove57f6182019-07-02 15:00:18 +0300951 nsrcs == 0) {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100952 br_ip4_multicast_leave_group(br, port, group, vid, src);
Satish Ashokbc8c20a2015-07-13 05:28:37 -0700953 } else {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100954 err = br_ip4_multicast_add_group(br, port, group, vid,
955 src);
Satish Ashokbc8c20a2015-07-13 05:28:37 -0700956 if (err)
957 break;
958 }
Herbert Xueb1d1642010-02-27 19:41:45 +0000959 }
960
961 return err;
962}
963
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000964#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900965static int br_ip6_multicast_mld2_report(struct net_bridge *br,
966 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -0400967 struct sk_buff *skb,
968 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900969{
Linus Lüssingba5ea6142019-01-21 07:26:25 +0100970 unsigned int nsrcs_offset;
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100971 const unsigned char *src;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900972 struct icmp6hdr *icmp6h;
973 struct mld2_grec *grec;
Linus Lüssingba5ea6142019-01-21 07:26:25 +0100974 unsigned int grec_len;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900975 int i;
976 int len;
977 int num;
978 int err = 0;
979
Linus Lüssingba5ea6142019-01-21 07:26:25 +0100980 if (!ipv6_mc_may_pull(skb, sizeof(*icmp6h)))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900981 return -EINVAL;
982
983 icmp6h = icmp6_hdr(skb);
984 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
Linus Lüssingc2d4fbd2015-09-11 18:39:48 +0200985 len = skb_transport_offset(skb) + sizeof(*icmp6h);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900986
987 for (i = 0; i < num; i++) {
Nikolay Aleksandrove57f6182019-07-02 15:00:18 +0300988 __be16 *_nsrcs, __nsrcs;
989 u16 nsrcs;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900990
Linus Lüssingba5ea6142019-01-21 07:26:25 +0100991 nsrcs_offset = len + offsetof(struct mld2_grec, grec_nsrcs);
992
993 if (skb_transport_offset(skb) + ipv6_transport_len(skb) <
994 nsrcs_offset + sizeof(_nsrcs))
995 return -EINVAL;
996
Nikolay Aleksandrove57f6182019-07-02 15:00:18 +0300997 _nsrcs = skb_header_pointer(skb, nsrcs_offset,
998 sizeof(__nsrcs), &__nsrcs);
999 if (!_nsrcs)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001000 return -EINVAL;
1001
Nikolay Aleksandrove57f6182019-07-02 15:00:18 +03001002 nsrcs = ntohs(*_nsrcs);
1003 grec_len = struct_size(grec, grec_src, nsrcs);
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001004
1005 if (!ipv6_mc_may_pull(skb, len + grec_len))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001006 return -EINVAL;
1007
1008 grec = (struct mld2_grec *)(skb->data + len);
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001009 len += grec_len;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001010
1011 /* We treat these as MLDv1 reports for now. */
1012 switch (grec->grec_type) {
1013 case MLD2_MODE_IS_INCLUDE:
1014 case MLD2_MODE_IS_EXCLUDE:
1015 case MLD2_CHANGE_TO_INCLUDE:
1016 case MLD2_CHANGE_TO_EXCLUDE:
1017 case MLD2_ALLOW_NEW_SOURCES:
1018 case MLD2_BLOCK_OLD_SOURCES:
1019 break;
1020
1021 default:
1022 continue;
1023 }
1024
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001025 src = eth_hdr(skb)->h_source;
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001026 if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE ||
1027 grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
Nikolay Aleksandrove57f6182019-07-02 15:00:18 +03001028 nsrcs == 0) {
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001029 br_ip6_multicast_leave_group(br, port, &grec->grec_mca,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001030 vid, src);
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001031 } else {
1032 err = br_ip6_multicast_add_group(br, port,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001033 &grec->grec_mca, vid,
1034 src);
Davide Caratti92642512016-08-31 14:16:44 +02001035 if (err)
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001036 break;
1037 }
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001038 }
1039
1040 return err;
1041}
1042#endif
1043
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001044static bool br_ip4_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001045 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001046 __be32 saddr)
1047{
1048 if (!timer_pending(&br->ip4_own_query.timer) &&
1049 !timer_pending(&br->ip4_other_query.timer))
1050 goto update;
1051
1052 if (!br->ip4_querier.addr.u.ip4)
1053 goto update;
1054
1055 if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4))
1056 goto update;
1057
1058 return false;
1059
1060update:
1061 br->ip4_querier.addr.u.ip4 = saddr;
1062
Linus Lüssing2cd41432014-06-07 18:26:29 +02001063 /* update protected by general multicast_lock by caller */
1064 rcu_assign_pointer(br->ip4_querier.port, port);
1065
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001066 return true;
1067}
1068
1069#if IS_ENABLED(CONFIG_IPV6)
1070static bool br_ip6_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001071 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001072 struct in6_addr *saddr)
1073{
1074 if (!timer_pending(&br->ip6_own_query.timer) &&
1075 !timer_pending(&br->ip6_other_query.timer))
1076 goto update;
1077
1078 if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0)
1079 goto update;
1080
1081 return false;
1082
1083update:
1084 br->ip6_querier.addr.u.ip6 = *saddr;
1085
Linus Lüssing2cd41432014-06-07 18:26:29 +02001086 /* update protected by general multicast_lock by caller */
1087 rcu_assign_pointer(br->ip6_querier.port, port);
1088
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001089 return true;
1090}
1091#endif
1092
1093static bool br_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001094 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001095 struct br_ip *saddr)
1096{
1097 switch (saddr->proto) {
1098 case htons(ETH_P_IP):
Linus Lüssing2cd41432014-06-07 18:26:29 +02001099 return br_ip4_multicast_select_querier(br, port, saddr->u.ip4);
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001100#if IS_ENABLED(CONFIG_IPV6)
1101 case htons(ETH_P_IPV6):
Linus Lüssing2cd41432014-06-07 18:26:29 +02001102 return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6);
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001103#endif
1104 }
1105
1106 return false;
1107}
1108
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001109static void
Linus Lüssing90010b32014-06-07 18:26:26 +02001110br_multicast_update_query_timer(struct net_bridge *br,
1111 struct bridge_mcast_other_query *query,
1112 unsigned long max_delay)
Linus Lüssingb00589a2013-08-01 01:06:20 +02001113{
Linus Lüssing90010b32014-06-07 18:26:26 +02001114 if (!timer_pending(&query->timer))
1115 query->delay_time = jiffies + max_delay;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001116
Linus Lüssing90010b32014-06-07 18:26:26 +02001117 mod_timer(&query->timer, jiffies + br->multicast_querier_interval);
Linus Lüssingb00589a2013-08-01 01:06:20 +02001118}
1119
Nogah Frankel6d549642017-02-09 14:54:42 +01001120static void br_port_mc_router_state_change(struct net_bridge_port *p,
1121 bool is_mc_router)
1122{
1123 struct switchdev_attr attr = {
1124 .orig_dev = p->dev,
1125 .id = SWITCHDEV_ATTR_ID_PORT_MROUTER,
1126 .flags = SWITCHDEV_F_DEFER,
1127 .u.mrouter = is_mc_router,
1128 };
1129
1130 switchdev_port_attr_set(p->dev, &attr);
1131}
1132
stephen hemminger7e80c122010-04-27 15:01:04 +00001133/*
Cong Wang7c776022013-06-21 15:37:25 +08001134 * Add port to router_list
stephen hemminger7e80c122010-04-27 15:01:04 +00001135 * list is maintained ordered by pointer value
1136 * and locked by br->multicast_lock and RCU
1137 */
Herbert Xu0909e112010-02-27 19:41:49 +00001138static void br_multicast_add_router(struct net_bridge *br,
1139 struct net_bridge_port *port)
1140{
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001141 struct net_bridge_port *p;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001142 struct hlist_node *slot = NULL;
Herbert Xu0909e112010-02-27 19:41:49 +00001143
Nikolay Aleksandrov1a040ea2015-06-09 10:23:57 -07001144 if (!hlist_unhashed(&port->rlist))
1145 return;
1146
Sasha Levinb67bfe02013-02-27 17:06:00 -08001147 hlist_for_each_entry(p, &br->router_list, rlist) {
stephen hemminger7e80c122010-04-27 15:01:04 +00001148 if ((unsigned long) port >= (unsigned long) p)
1149 break;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001150 slot = &p->rlist;
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001151 }
Herbert Xu0909e112010-02-27 19:41:49 +00001152
stephen hemminger7e80c122010-04-27 15:01:04 +00001153 if (slot)
Ken Helias1d023282014-08-06 16:09:16 -07001154 hlist_add_behind_rcu(&port->rlist, slot);
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001155 else
1156 hlist_add_head_rcu(&port->rlist, &br->router_list);
Satish Ashok949f1e32015-07-23 05:00:53 -07001157 br_rtr_notify(br->dev, port, RTM_NEWMDB);
Nogah Frankel6d549642017-02-09 14:54:42 +01001158 br_port_mc_router_state_change(port, true);
Herbert Xu0909e112010-02-27 19:41:49 +00001159}
1160
Herbert Xueb1d1642010-02-27 19:41:45 +00001161static void br_multicast_mark_router(struct net_bridge *br,
1162 struct net_bridge_port *port)
1163{
1164 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +00001165
1166 if (!port) {
Yotam Gigi77041422017-10-09 11:15:31 +02001167 if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) {
1168 if (!timer_pending(&br->multicast_router_timer))
1169 br_mc_router_state_change(br, true);
Herbert Xueb1d1642010-02-27 19:41:45 +00001170 mod_timer(&br->multicast_router_timer,
1171 now + br->multicast_querier_interval);
Yotam Gigi77041422017-10-09 11:15:31 +02001172 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001173 return;
1174 }
1175
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001176 if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
1177 port->multicast_router == MDB_RTR_TYPE_PERM)
Herbert Xueb1d1642010-02-27 19:41:45 +00001178 return;
1179
Herbert Xu0909e112010-02-27 19:41:49 +00001180 br_multicast_add_router(br, port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001181
Herbert Xueb1d1642010-02-27 19:41:45 +00001182 mod_timer(&port->multicast_router_timer,
1183 now + br->multicast_querier_interval);
1184}
1185
1186static void br_multicast_query_received(struct net_bridge *br,
1187 struct net_bridge_port *port,
Linus Lüssing90010b32014-06-07 18:26:26 +02001188 struct bridge_mcast_other_query *query,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001189 struct br_ip *saddr,
Linus Lüssingb00589a2013-08-01 01:06:20 +02001190 unsigned long max_delay)
Herbert Xueb1d1642010-02-27 19:41:45 +00001191{
Linus Lüssing2cd41432014-06-07 18:26:29 +02001192 if (!br_multicast_select_querier(br, port, saddr))
Herbert Xueb1d1642010-02-27 19:41:45 +00001193 return;
1194
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001195 br_multicast_update_query_timer(br, query, max_delay);
Hangbin Liu278e2142019-02-22 21:22:32 +08001196 br_multicast_mark_router(br, port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001197}
1198
zhong jiang9c2e9552018-08-06 11:07:23 +08001199static void br_ip4_multicast_query(struct net_bridge *br,
1200 struct net_bridge_port *port,
1201 struct sk_buff *skb,
1202 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001203{
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001204 unsigned int transport_len = ip_transport_len(skb);
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001205 const struct iphdr *iph = ip_hdr(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001206 struct igmphdr *ih = igmp_hdr(skb);
1207 struct net_bridge_mdb_entry *mp;
1208 struct igmpv3_query *ih3;
1209 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +00001210 struct net_bridge_port_group __rcu **pp;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001211 struct br_ip saddr;
Herbert Xueb1d1642010-02-27 19:41:45 +00001212 unsigned long max_delay;
1213 unsigned long now = jiffies;
1214 __be32 group;
1215
1216 spin_lock(&br->multicast_lock);
1217 if (!netif_running(br->dev) ||
1218 (port && port->state == BR_STATE_DISABLED))
1219 goto out;
1220
Herbert Xueb1d1642010-02-27 19:41:45 +00001221 group = ih->group;
1222
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001223 if (transport_len == sizeof(*ih)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001224 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1225
1226 if (!max_delay) {
1227 max_delay = 10 * HZ;
1228 group = 0;
1229 }
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001230 } else if (transport_len >= sizeof(*ih3)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001231 ih3 = igmpv3_query_hdr(skb);
1232 if (ih3->nsrcs)
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001233 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001234
YOSHIFUJI Hideaki / 吉藤英明0ba8c9e2010-03-15 19:27:00 +00001235 max_delay = ih3->code ?
1236 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001237 } else {
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001238 goto out;
1239 }
1240
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001241 if (!group) {
1242 saddr.proto = htons(ETH_P_IP);
1243 saddr.u.ip4 = iph->saddr;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001244
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001245 br_multicast_query_received(br, port, &br->ip4_other_query,
1246 &saddr, max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001247 goto out;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001248 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001249
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02001250 mp = br_mdb_ip4_get(br, group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001251 if (!mp)
1252 goto out;
1253
1254 max_delay *= br->multicast_last_member_count;
1255
Andrew Lunnff0fd342017-11-09 23:10:57 +01001256 if (mp->host_joined &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001257 (timer_pending(&mp->timer) ?
1258 time_after(mp->timer.expires, now + max_delay) :
1259 try_to_del_timer_sync(&mp->timer) >= 0))
1260 mod_timer(&mp->timer, now + max_delay);
1261
Eric Dumazete8051682010-11-15 06:38:10 +00001262 for (pp = &mp->ports;
1263 (p = mlock_dereference(*pp, br)) != NULL;
1264 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001265 if (timer_pending(&p->timer) ?
1266 time_after(p->timer.expires, now + max_delay) :
1267 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001268 mod_timer(&p->timer, now + max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001269 }
1270
1271out:
1272 spin_unlock(&br->multicast_lock);
Herbert Xueb1d1642010-02-27 19:41:45 +00001273}
1274
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001275#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001276static int br_ip6_multicast_query(struct net_bridge *br,
1277 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001278 struct sk_buff *skb,
1279 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001280{
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001281 unsigned int transport_len = ipv6_transport_len(skb);
stephen hemmingereca2a432012-12-13 06:51:28 +00001282 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001283 struct net_bridge_mdb_entry *mp;
1284 struct mld2_query *mld2q;
Eric Dumazete8051682010-11-15 06:38:10 +00001285 struct net_bridge_port_group *p;
1286 struct net_bridge_port_group __rcu **pp;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001287 struct br_ip saddr;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001288 unsigned long max_delay;
1289 unsigned long now = jiffies;
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001290 unsigned int offset = skb_transport_offset(skb);
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001291 const struct in6_addr *group = NULL;
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001292 bool is_general_query;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001293 int err = 0;
1294
1295 spin_lock(&br->multicast_lock);
1296 if (!netif_running(br->dev) ||
1297 (port && port->state == BR_STATE_DISABLED))
1298 goto out;
1299
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001300 if (transport_len == sizeof(*mld)) {
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001301 if (!pskb_may_pull(skb, offset + sizeof(*mld))) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001302 err = -EINVAL;
1303 goto out;
1304 }
1305 mld = (struct mld_msg *) icmp6_hdr(skb);
Li RongQing47152132012-07-09 23:56:12 +00001306 max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001307 if (max_delay)
1308 group = &mld->mld_mca;
Linus Lüssing248ba8e2013-08-06 00:32:05 +02001309 } else {
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001310 if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001311 err = -EINVAL;
1312 goto out;
1313 }
1314 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1315 if (!mld2q->mld2q_nsrcs)
1316 group = &mld2q->mld2q_mca;
Daniel Borkmanne3f5b172013-09-04 00:19:39 +02001317
1318 max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001319 }
1320
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001321 is_general_query = group && ipv6_addr_any(group);
1322
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001323 if (is_general_query) {
1324 saddr.proto = htons(ETH_P_IPV6);
Nikolay Aleksandrov3b26a5d2019-07-02 15:00:19 +03001325 saddr.u.ip6 = ipv6_hdr(skb)->saddr;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001326
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001327 br_multicast_query_received(br, port, &br->ip6_other_query,
1328 &saddr, max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001329 goto out;
Linus Lüssing6c03ee82014-06-12 01:41:23 +02001330 } else if (!group) {
1331 goto out;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001332 }
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001333
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02001334 mp = br_mdb_ip6_get(br, group, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001335 if (!mp)
1336 goto out;
1337
1338 max_delay *= br->multicast_last_member_count;
Andrew Lunnff0fd342017-11-09 23:10:57 +01001339 if (mp->host_joined &&
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001340 (timer_pending(&mp->timer) ?
1341 time_after(mp->timer.expires, now + max_delay) :
1342 try_to_del_timer_sync(&mp->timer) >= 0))
1343 mod_timer(&mp->timer, now + max_delay);
1344
Eric Dumazete8051682010-11-15 06:38:10 +00001345 for (pp = &mp->ports;
1346 (p = mlock_dereference(*pp, br)) != NULL;
1347 pp = &p->next) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001348 if (timer_pending(&p->timer) ?
1349 time_after(p->timer.expires, now + max_delay) :
1350 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001351 mod_timer(&p->timer, now + max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001352 }
1353
1354out:
1355 spin_unlock(&br->multicast_lock);
1356 return err;
1357}
1358#endif
1359
Linus Lüssing90010b32014-06-07 18:26:26 +02001360static void
1361br_multicast_leave_group(struct net_bridge *br,
1362 struct net_bridge_port *port,
1363 struct br_ip *group,
1364 struct bridge_mcast_other_query *other_query,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001365 struct bridge_mcast_own_query *own_query,
1366 const unsigned char *src)
Herbert Xueb1d1642010-02-27 19:41:45 +00001367{
Herbert Xueb1d1642010-02-27 19:41:45 +00001368 struct net_bridge_mdb_entry *mp;
1369 struct net_bridge_port_group *p;
1370 unsigned long now;
1371 unsigned long time;
1372
Herbert Xueb1d1642010-02-27 19:41:45 +00001373 spin_lock(&br->multicast_lock);
1374 if (!netif_running(br->dev) ||
Satish Ashok544586f2015-07-28 03:28:27 -07001375 (port && port->state == BR_STATE_DISABLED))
Herbert Xueb1d1642010-02-27 19:41:45 +00001376 goto out;
1377
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02001378 mp = br_mdb_ip_get(br, group);
Herbert Xueb1d1642010-02-27 19:41:45 +00001379 if (!mp)
1380 goto out;
1381
Satish Ashok544586f2015-07-28 03:28:27 -07001382 if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
1383 struct net_bridge_port_group __rcu **pp;
1384
1385 for (pp = &mp->ports;
1386 (p = mlock_dereference(*pp, br)) != NULL;
1387 pp = &p->next) {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001388 if (!br_port_group_equal(p, port, src))
Satish Ashok544586f2015-07-28 03:28:27 -07001389 continue;
1390
1391 rcu_assign_pointer(*pp, p->next);
1392 hlist_del_init(&p->mglist);
1393 del_timer(&p->timer);
Nikolay Aleksandrov43295962018-12-05 15:14:25 +02001394 kfree_rcu(p, rcu);
Elad Raz45ebcce2016-04-21 12:52:45 +02001395 br_mdb_notify(br->dev, port, group, RTM_DELMDB,
1396 p->flags);
Satish Ashok544586f2015-07-28 03:28:27 -07001397
Andrew Lunnff0fd342017-11-09 23:10:57 +01001398 if (!mp->ports && !mp->host_joined &&
Satish Ashok544586f2015-07-28 03:28:27 -07001399 netif_running(br->dev))
1400 mod_timer(&mp->timer, jiffies);
1401 }
1402 goto out;
1403 }
1404
1405 if (timer_pending(&other_query->timer))
1406 goto out;
1407
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03001408 if (br_opt_get(br, BROPT_MULTICAST_QUERIER)) {
Cong Wang6b7df112013-05-21 21:52:56 +00001409 __br_multicast_send_query(br, port, &mp->addr);
1410
1411 time = jiffies + br->multicast_last_member_count *
1412 br->multicast_last_member_interval;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001413
Linus Lüssing90010b32014-06-07 18:26:26 +02001414 mod_timer(&own_query->timer, time);
Cong Wang6b7df112013-05-21 21:52:56 +00001415
1416 for (p = mlock_dereference(mp->ports, br);
1417 p != NULL;
1418 p = mlock_dereference(p->next, br)) {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001419 if (!br_port_group_equal(p, port, src))
Cong Wang6b7df112013-05-21 21:52:56 +00001420 continue;
1421
1422 if (!hlist_unhashed(&p->mglist) &&
1423 (timer_pending(&p->timer) ?
1424 time_after(p->timer.expires, time) :
1425 try_to_del_timer_sync(&p->timer) >= 0)) {
1426 mod_timer(&p->timer, time);
1427 }
1428
1429 break;
1430 }
1431 }
1432
Herbert Xueb1d1642010-02-27 19:41:45 +00001433 now = jiffies;
1434 time = now + br->multicast_last_member_count *
1435 br->multicast_last_member_interval;
1436
1437 if (!port) {
Andrew Lunnff0fd342017-11-09 23:10:57 +01001438 if (mp->host_joined &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001439 (timer_pending(&mp->timer) ?
1440 time_after(mp->timer.expires, time) :
1441 try_to_del_timer_sync(&mp->timer) >= 0)) {
1442 mod_timer(&mp->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +00001443 }
Linus Lüssing454594f2013-10-20 00:58:57 +02001444
1445 goto out;
1446 }
1447
1448 for (p = mlock_dereference(mp->ports, br);
1449 p != NULL;
1450 p = mlock_dereference(p->next, br)) {
1451 if (p->port != port)
1452 continue;
1453
1454 if (!hlist_unhashed(&p->mglist) &&
1455 (timer_pending(&p->timer) ?
1456 time_after(p->timer.expires, time) :
1457 try_to_del_timer_sync(&p->timer) >= 0)) {
1458 mod_timer(&p->timer, time);
1459 }
1460
1461 break;
Herbert Xueb1d1642010-02-27 19:41:45 +00001462 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001463out:
1464 spin_unlock(&br->multicast_lock);
1465}
1466
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001467static void br_ip4_multicast_leave_group(struct net_bridge *br,
1468 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001469 __be32 group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001470 __u16 vid,
1471 const unsigned char *src)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001472{
1473 struct br_ip br_group;
Linus Lüssing90010b32014-06-07 18:26:26 +02001474 struct bridge_mcast_own_query *own_query;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001475
1476 if (ipv4_is_local_multicast(group))
1477 return;
1478
Linus Lüssing90010b32014-06-07 18:26:26 +02001479 own_query = port ? &port->ip4_own_query : &br->ip4_own_query;
1480
Nikolay Aleksandrov1515a63f2019-04-03 23:27:24 +03001481 memset(&br_group, 0, sizeof(br_group));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001482 br_group.u.ip4 = group;
1483 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001484 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001485
Linus Lüssing90010b32014-06-07 18:26:26 +02001486 br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001487 own_query, src);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001488}
1489
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001490#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001491static void br_ip6_multicast_leave_group(struct net_bridge *br,
1492 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001493 const struct in6_addr *group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001494 __u16 vid,
1495 const unsigned char *src)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001496{
1497 struct br_ip br_group;
Linus Lüssing90010b32014-06-07 18:26:26 +02001498 struct bridge_mcast_own_query *own_query;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001499
Linus Lüssing3c3769e2013-09-04 02:13:39 +02001500 if (ipv6_addr_is_ll_all_nodes(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001501 return;
1502
Linus Lüssing90010b32014-06-07 18:26:26 +02001503 own_query = port ? &port->ip6_own_query : &br->ip6_own_query;
1504
Nikolay Aleksandrov1515a63f2019-04-03 23:27:24 +03001505 memset(&br_group, 0, sizeof(br_group));
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001506 br_group.u.ip6 = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001507 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001508 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001509
Linus Lüssing90010b32014-06-07 18:26:26 +02001510 br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001511 own_query, src);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001512}
1513#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001514
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001515static void br_multicast_err_count(const struct net_bridge *br,
1516 const struct net_bridge_port *p,
1517 __be16 proto)
1518{
1519 struct bridge_mcast_stats __percpu *stats;
1520 struct bridge_mcast_stats *pstats;
1521
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03001522 if (!br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001523 return;
1524
1525 if (p)
1526 stats = p->mcast_stats;
1527 else
1528 stats = br->mcast_stats;
1529 if (WARN_ON(!stats))
1530 return;
1531
1532 pstats = this_cpu_ptr(stats);
1533
1534 u64_stats_update_begin(&pstats->syncp);
1535 switch (proto) {
1536 case htons(ETH_P_IP):
1537 pstats->mstats.igmp_parse_errors++;
1538 break;
1539#if IS_ENABLED(CONFIG_IPV6)
1540 case htons(ETH_P_IPV6):
1541 pstats->mstats.mld_parse_errors++;
1542 break;
1543#endif
1544 }
1545 u64_stats_update_end(&pstats->syncp);
1546}
1547
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001548static void br_multicast_pim(struct net_bridge *br,
1549 struct net_bridge_port *port,
1550 const struct sk_buff *skb)
1551{
1552 unsigned int offset = skb_transport_offset(skb);
1553 struct pimhdr *pimhdr, _pimhdr;
1554
1555 pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr);
1556 if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION ||
1557 pim_hdr_type(pimhdr) != PIM_TYPE_HELLO)
1558 return;
1559
1560 br_multicast_mark_router(br, port);
1561}
1562
Linus Lüssing4b3087c2019-01-21 07:26:28 +01001563static int br_ip4_multicast_mrd_rcv(struct net_bridge *br,
1564 struct net_bridge_port *port,
1565 struct sk_buff *skb)
1566{
1567 if (ip_hdr(skb)->protocol != IPPROTO_IGMP ||
1568 igmp_hdr(skb)->type != IGMP_MRDISC_ADV)
1569 return -ENOMSG;
1570
1571 br_multicast_mark_router(br, port);
1572
1573 return 0;
1574}
1575
Herbert Xueb1d1642010-02-27 19:41:45 +00001576static int br_multicast_ipv4_rcv(struct net_bridge *br,
1577 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001578 struct sk_buff *skb,
1579 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001580{
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001581 const unsigned char *src;
Herbert Xueb1d1642010-02-27 19:41:45 +00001582 struct igmphdr *ih;
Herbert Xueb1d1642010-02-27 19:41:45 +00001583 int err;
1584
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001585 err = ip_mc_check_igmp(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001586
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001587 if (err == -ENOMSG) {
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001588 if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) {
Herbert Xubd4265f2011-06-23 02:39:12 +00001589 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001590 } else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) {
1591 if (ip_hdr(skb)->protocol == IPPROTO_PIM)
1592 br_multicast_pim(br, port, skb);
Linus Lüssing4b3087c2019-01-21 07:26:28 +01001593 } else if (ipv4_is_all_snoopers(ip_hdr(skb)->daddr)) {
Li RongQing08e71622019-02-19 10:17:09 +08001594 br_ip4_multicast_mrd_rcv(br, port, skb);
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001595 }
Linus Lüssing4b3087c2019-01-21 07:26:28 +01001596
Herbert Xueb1d1642010-02-27 19:41:45 +00001597 return 0;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001598 } else if (err < 0) {
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001599 br_multicast_err_count(br, port, skb->protocol);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001600 return err;
Herbert Xubd4265f2011-06-23 02:39:12 +00001601 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001602
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001603 ih = igmp_hdr(skb);
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001604 src = eth_hdr(skb)->h_source;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001605 BR_INPUT_SKB_CB(skb)->igmp = ih->type;
Herbert Xueb1d1642010-02-27 19:41:45 +00001606
1607 switch (ih->type) {
1608 case IGMP_HOST_MEMBERSHIP_REPORT:
1609 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Fernando Luis Vázquez Cao62b2bcb2011-06-13 15:04:43 +00001610 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001611 err = br_ip4_multicast_add_group(br, port, ih->group, vid, src);
Herbert Xueb1d1642010-02-27 19:41:45 +00001612 break;
1613 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001614 err = br_ip4_multicast_igmp3_report(br, port, skb, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001615 break;
1616 case IGMP_HOST_MEMBERSHIP_QUERY:
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001617 br_ip4_multicast_query(br, port, skb, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001618 break;
1619 case IGMP_HOST_LEAVE_MESSAGE:
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001620 br_ip4_multicast_leave_group(br, port, ih->group, vid, src);
Herbert Xueb1d1642010-02-27 19:41:45 +00001621 break;
1622 }
1623
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07001624 br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001625 BR_MCAST_DIR_RX);
1626
Herbert Xueb1d1642010-02-27 19:41:45 +00001627 return err;
1628}
1629
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001630#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing4b3087c2019-01-21 07:26:28 +01001631static int br_ip6_multicast_mrd_rcv(struct net_bridge *br,
1632 struct net_bridge_port *port,
1633 struct sk_buff *skb)
1634{
1635 int ret;
1636
1637 if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6)
1638 return -ENOMSG;
1639
1640 ret = ipv6_mc_check_icmpv6(skb);
1641 if (ret < 0)
1642 return ret;
1643
1644 if (icmp6_hdr(skb)->icmp6_type != ICMPV6_MRDISC_ADV)
1645 return -ENOMSG;
1646
1647 br_multicast_mark_router(br, port);
1648
1649 return 0;
1650}
1651
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001652static int br_multicast_ipv6_rcv(struct net_bridge *br,
1653 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001654 struct sk_buff *skb,
1655 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001656{
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001657 const unsigned char *src;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001658 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001659 int err;
1660
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001661 err = ipv6_mc_check_mld(skb);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001662
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001663 if (err == -ENOMSG) {
1664 if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
1665 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Linus Lüssing4b3087c2019-01-21 07:26:28 +01001666
1667 if (ipv6_addr_is_all_snoopers(&ipv6_hdr(skb)->daddr)) {
1668 err = br_ip6_multicast_mrd_rcv(br, port, skb);
1669
1670 if (err < 0 && err != -ENOMSG) {
1671 br_multicast_err_count(br, port, skb->protocol);
1672 return err;
1673 }
1674 }
1675
Linus Lüssing8fad9c32013-09-04 02:13:38 +02001676 return 0;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001677 } else if (err < 0) {
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001678 br_multicast_err_count(br, port, skb->protocol);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001679 return err;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001680 }
1681
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001682 mld = (struct mld_msg *)skb_transport_header(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001683 BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001684
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001685 switch (mld->mld_type) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001686 case ICMPV6_MGM_REPORT:
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001687 src = eth_hdr(skb)->h_source;
Fernando Luis Vázquez Caofc2af6c2011-06-13 15:06:58 +00001688 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001689 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid,
1690 src);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001691 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001692 case ICMPV6_MLD2_REPORT:
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001693 err = br_ip6_multicast_mld2_report(br, port, skb, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001694 break;
1695 case ICMPV6_MGM_QUERY:
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001696 err = br_ip6_multicast_query(br, port, skb, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001697 break;
1698 case ICMPV6_MGM_REDUCTION:
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001699 src = eth_hdr(skb)->h_source;
1700 br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001701 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001702 }
1703
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07001704 br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001705 BR_MCAST_DIR_RX);
1706
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001707 return err;
1708}
1709#endif
1710
Herbert Xueb1d1642010-02-27 19:41:45 +00001711int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001712 struct sk_buff *skb, u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001713{
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001714 int ret = 0;
1715
YOSHIFUJI Hideaki / 吉藤英明1fafc7a2010-04-25 08:06:40 +00001716 BR_INPUT_SKB_CB(skb)->igmp = 0;
1717 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1718
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +03001719 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
Herbert Xueb1d1642010-02-27 19:41:45 +00001720 return 0;
1721
1722 switch (skb->protocol) {
1723 case htons(ETH_P_IP):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001724 ret = br_multicast_ipv4_rcv(br, port, skb, vid);
1725 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001726#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001727 case htons(ETH_P_IPV6):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001728 ret = br_multicast_ipv6_rcv(br, port, skb, vid);
1729 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001730#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001731 }
1732
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001733 return ret;
Herbert Xueb1d1642010-02-27 19:41:45 +00001734}
1735
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001736static void br_multicast_query_expired(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001737 struct bridge_mcast_own_query *query,
1738 struct bridge_mcast_querier *querier)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001739{
1740 spin_lock(&br->multicast_lock);
1741 if (query->startup_sent < br->multicast_startup_query_count)
1742 query->startup_sent++;
1743
Eric Dumazet71d9f612015-05-28 04:42:54 -07001744 RCU_INIT_POINTER(querier->port, NULL);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001745 br_multicast_send_query(br, NULL, query);
1746 spin_unlock(&br->multicast_lock);
1747}
1748
Allen Pais88c1f372017-11-03 11:51:10 +05301749static void br_ip4_multicast_query_expired(struct timer_list *t)
Herbert Xueb1d1642010-02-27 19:41:45 +00001750{
Allen Pais88c1f372017-11-03 11:51:10 +05301751 struct net_bridge *br = from_timer(br, t, ip4_own_query.timer);
Herbert Xueb1d1642010-02-27 19:41:45 +00001752
Linus Lüssing2cd41432014-06-07 18:26:29 +02001753 br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier);
Herbert Xueb1d1642010-02-27 19:41:45 +00001754}
1755
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001756#if IS_ENABLED(CONFIG_IPV6)
Allen Pais88c1f372017-11-03 11:51:10 +05301757static void br_ip6_multicast_query_expired(struct timer_list *t)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001758{
Allen Pais88c1f372017-11-03 11:51:10 +05301759 struct net_bridge *br = from_timer(br, t, ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001760
Linus Lüssing2cd41432014-06-07 18:26:29 +02001761 br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001762}
1763#endif
1764
Herbert Xueb1d1642010-02-27 19:41:45 +00001765void br_multicast_init(struct net_bridge *br)
1766{
Nikolay Aleksandrovd08c6bc2018-12-05 15:14:27 +02001767 br->hash_max = BR_MULTICAST_DEFAULT_HASH_MAX;
Herbert Xueb1d1642010-02-27 19:41:45 +00001768
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001769 br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Herbert Xueb1d1642010-02-27 19:41:45 +00001770 br->multicast_last_member_count = 2;
1771 br->multicast_startup_query_count = 2;
1772
1773 br->multicast_last_member_interval = HZ;
1774 br->multicast_query_response_interval = 10 * HZ;
1775 br->multicast_startup_query_interval = 125 * HZ / 4;
1776 br->multicast_query_interval = 125 * HZ;
1777 br->multicast_querier_interval = 255 * HZ;
1778 br->multicast_membership_interval = 260 * HZ;
1779
Linus Lüssing90010b32014-06-07 18:26:26 +02001780 br->ip4_other_query.delay_time = 0;
Linus Lüssing2cd41432014-06-07 18:26:29 +02001781 br->ip4_querier.port = NULL;
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001782 br->multicast_igmp_version = 2;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001783#if IS_ENABLED(CONFIG_IPV6)
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001784 br->multicast_mld_version = 1;
Linus Lüssing90010b32014-06-07 18:26:26 +02001785 br->ip6_other_query.delay_time = 0;
Linus Lüssing2cd41432014-06-07 18:26:29 +02001786 br->ip6_querier.port = NULL;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001787#endif
Ido Schimmel6919622a2018-10-01 11:57:01 +03001788 br_opt_toggle(br, BROPT_MULTICAST_ENABLED, true);
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03001789 br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true);
Linus Lüssingb00589a2013-08-01 01:06:20 +02001790
Herbert Xueb1d1642010-02-27 19:41:45 +00001791 spin_lock_init(&br->multicast_lock);
Allen Pais88c1f372017-11-03 11:51:10 +05301792 timer_setup(&br->multicast_router_timer,
1793 br_multicast_local_router_expired, 0);
1794 timer_setup(&br->ip4_other_query.timer,
1795 br_ip4_multicast_querier_expired, 0);
1796 timer_setup(&br->ip4_own_query.timer,
1797 br_ip4_multicast_query_expired, 0);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001798#if IS_ENABLED(CONFIG_IPV6)
Allen Pais88c1f372017-11-03 11:51:10 +05301799 timer_setup(&br->ip6_other_query.timer,
1800 br_ip6_multicast_querier_expired, 0);
1801 timer_setup(&br->ip6_own_query.timer,
1802 br_ip6_multicast_query_expired, 0);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001803#endif
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02001804 INIT_HLIST_HEAD(&br->mdb_list);
Herbert Xueb1d1642010-02-27 19:41:45 +00001805}
1806
Linus Lüssing4effd282019-01-21 07:26:27 +01001807static void br_ip4_multicast_join_snoopers(struct net_bridge *br)
1808{
1809 struct in_device *in_dev = in_dev_get(br->dev);
1810
1811 if (!in_dev)
1812 return;
1813
Florian Fainelli9fb20802019-02-01 20:20:52 -08001814 __ip_mc_inc_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
Linus Lüssing4effd282019-01-21 07:26:27 +01001815 in_dev_put(in_dev);
1816}
1817
1818#if IS_ENABLED(CONFIG_IPV6)
1819static void br_ip6_multicast_join_snoopers(struct net_bridge *br)
1820{
1821 struct in6_addr addr;
1822
1823 ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
1824 ipv6_dev_mc_inc(br->dev, &addr);
1825}
1826#else
1827static inline void br_ip6_multicast_join_snoopers(struct net_bridge *br)
1828{
1829}
1830#endif
1831
1832static void br_multicast_join_snoopers(struct net_bridge *br)
1833{
1834 br_ip4_multicast_join_snoopers(br);
1835 br_ip6_multicast_join_snoopers(br);
1836}
1837
1838static void br_ip4_multicast_leave_snoopers(struct net_bridge *br)
1839{
1840 struct in_device *in_dev = in_dev_get(br->dev);
1841
1842 if (WARN_ON(!in_dev))
1843 return;
1844
Florian Fainelli9fb20802019-02-01 20:20:52 -08001845 __ip_mc_dec_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
Linus Lüssing4effd282019-01-21 07:26:27 +01001846 in_dev_put(in_dev);
1847}
1848
1849#if IS_ENABLED(CONFIG_IPV6)
1850static void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
1851{
1852 struct in6_addr addr;
1853
1854 ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
1855 ipv6_dev_mc_dec(br->dev, &addr);
1856}
1857#else
1858static inline void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
1859{
1860}
1861#endif
1862
1863static void br_multicast_leave_snoopers(struct net_bridge *br)
1864{
1865 br_ip4_multicast_leave_snoopers(br);
1866 br_ip6_multicast_leave_snoopers(br);
1867}
1868
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001869static void __br_multicast_open(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +02001870 struct bridge_mcast_own_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +00001871{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001872 query->startup_sent = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001873
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +03001874 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
Herbert Xueb1d1642010-02-27 19:41:45 +00001875 return;
1876
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001877 mod_timer(&query->timer, jiffies);
1878}
1879
1880void br_multicast_open(struct net_bridge *br)
1881{
Linus Lüssing4effd282019-01-21 07:26:27 +01001882 if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
1883 br_multicast_join_snoopers(br);
1884
Linus Lüssing90010b32014-06-07 18:26:26 +02001885 __br_multicast_open(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001886#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001887 __br_multicast_open(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001888#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001889}
1890
1891void br_multicast_stop(struct net_bridge *br)
1892{
Herbert Xueb1d1642010-02-27 19:41:45 +00001893 del_timer_sync(&br->multicast_router_timer);
Linus Lüssing90010b32014-06-07 18:26:26 +02001894 del_timer_sync(&br->ip4_other_query.timer);
1895 del_timer_sync(&br->ip4_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001896#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001897 del_timer_sync(&br->ip6_other_query.timer);
1898 del_timer_sync(&br->ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001899#endif
Linus Lüssing4effd282019-01-21 07:26:27 +01001900
1901 if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
1902 br_multicast_leave_snoopers(br);
Satish Ashoke10177a2015-07-15 07:16:51 -07001903}
1904
1905void br_multicast_dev_del(struct net_bridge *br)
1906{
Satish Ashoke10177a2015-07-15 07:16:51 -07001907 struct net_bridge_mdb_entry *mp;
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02001908 struct hlist_node *tmp;
Herbert Xueb1d1642010-02-27 19:41:45 +00001909
1910 spin_lock_bh(&br->multicast_lock);
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02001911 hlist_for_each_entry_safe(mp, tmp, &br->mdb_list, mdb_node) {
1912 del_timer(&mp->timer);
1913 rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode,
1914 br_mdb_rht_params);
1915 hlist_del_rcu(&mp->mdb_node);
Nikolay Aleksandrov43295962018-12-05 15:14:25 +02001916 kfree_rcu(mp, rcu);
Herbert Xueb1d1642010-02-27 19:41:45 +00001917 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001918 spin_unlock_bh(&br->multicast_lock);
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02001919
Nikolay Aleksandrov43295962018-12-05 15:14:25 +02001920 rcu_barrier();
Herbert Xueb1d1642010-02-27 19:41:45 +00001921}
Herbert Xu0909e112010-02-27 19:41:49 +00001922
1923int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1924{
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02001925 int err = -EINVAL;
Herbert Xu0909e112010-02-27 19:41:49 +00001926
1927 spin_lock_bh(&br->multicast_lock);
Herbert Xu0909e112010-02-27 19:41:49 +00001928
1929 switch (val) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001930 case MDB_RTR_TYPE_DISABLED:
1931 case MDB_RTR_TYPE_PERM:
Yotam Gigi77041422017-10-09 11:15:31 +02001932 br_mc_router_state_change(br, val == MDB_RTR_TYPE_PERM);
Herbert Xu0909e112010-02-27 19:41:49 +00001933 del_timer(&br->multicast_router_timer);
Yotam Gigi77041422017-10-09 11:15:31 +02001934 br->multicast_router = val;
1935 err = 0;
1936 break;
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001937 case MDB_RTR_TYPE_TEMP_QUERY:
Yotam Gigi77041422017-10-09 11:15:31 +02001938 if (br->multicast_router != MDB_RTR_TYPE_TEMP_QUERY)
1939 br_mc_router_state_change(br, false);
Herbert Xu0909e112010-02-27 19:41:49 +00001940 br->multicast_router = val;
1941 err = 0;
1942 break;
Herbert Xu0909e112010-02-27 19:41:49 +00001943 }
1944
Herbert Xu0909e112010-02-27 19:41:49 +00001945 spin_unlock_bh(&br->multicast_lock);
1946
1947 return err;
1948}
1949
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001950static void __del_port_router(struct net_bridge_port *p)
1951{
1952 if (hlist_unhashed(&p->rlist))
1953 return;
1954 hlist_del_init_rcu(&p->rlist);
1955 br_rtr_notify(p->br->dev, p, RTM_DELMDB);
Nogah Frankel6d549642017-02-09 14:54:42 +01001956 br_port_mc_router_state_change(p, false);
Nogah Frankelf12e7d92017-02-09 14:54:41 +01001957
1958 /* don't allow timer refresh */
1959 if (p->multicast_router == MDB_RTR_TYPE_TEMP)
1960 p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001961}
1962
Herbert Xu0909e112010-02-27 19:41:49 +00001963int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1964{
1965 struct net_bridge *br = p->br;
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001966 unsigned long now = jiffies;
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02001967 int err = -EINVAL;
Herbert Xu0909e112010-02-27 19:41:49 +00001968
1969 spin_lock(&br->multicast_lock);
Nikolay Aleksandrov4950cfd2016-02-26 21:20:02 +01001970 if (p->multicast_router == val) {
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001971 /* Refresh the temp router port timer */
1972 if (p->multicast_router == MDB_RTR_TYPE_TEMP)
1973 mod_timer(&p->multicast_router_timer,
1974 now + br->multicast_querier_interval);
Nikolay Aleksandrov4950cfd2016-02-26 21:20:02 +01001975 err = 0;
1976 goto unlock;
1977 }
Herbert Xu0909e112010-02-27 19:41:49 +00001978 switch (val) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001979 case MDB_RTR_TYPE_DISABLED:
1980 p->multicast_router = MDB_RTR_TYPE_DISABLED;
1981 __del_port_router(p);
Herbert Xu0909e112010-02-27 19:41:49 +00001982 del_timer(&p->multicast_router_timer);
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001983 break;
1984 case MDB_RTR_TYPE_TEMP_QUERY:
1985 p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
1986 __del_port_router(p);
1987 break;
1988 case MDB_RTR_TYPE_PERM:
1989 p->multicast_router = MDB_RTR_TYPE_PERM;
1990 del_timer(&p->multicast_router_timer);
Herbert Xu0909e112010-02-27 19:41:49 +00001991 br_multicast_add_router(br, p);
1992 break;
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001993 case MDB_RTR_TYPE_TEMP:
1994 p->multicast_router = MDB_RTR_TYPE_TEMP;
1995 br_multicast_mark_router(br, p);
1996 break;
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001997 default:
1998 goto unlock;
Herbert Xu0909e112010-02-27 19:41:49 +00001999 }
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002000 err = 0;
2001unlock:
Herbert Xu0909e112010-02-27 19:41:49 +00002002 spin_unlock(&br->multicast_lock);
2003
2004 return err;
2005}
Herbert Xu561f1102010-02-27 19:41:50 +00002006
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002007static void br_multicast_start_querier(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +02002008 struct bridge_mcast_own_query *query)
Herbert Xu561f1102010-02-27 19:41:50 +00002009{
2010 struct net_bridge_port *port;
Herbert Xu74857212012-04-13 02:37:42 +00002011
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002012 __br_multicast_open(br, query);
Herbert Xu74857212012-04-13 02:37:42 +00002013
Nikolay Aleksandrovc5b493c2019-04-11 15:08:25 +03002014 rcu_read_lock();
2015 list_for_each_entry_rcu(port, &br->port_list, list) {
Herbert Xu74857212012-04-13 02:37:42 +00002016 if (port->state == BR_STATE_DISABLED ||
2017 port->state == BR_STATE_BLOCKING)
2018 continue;
2019
Linus Lüssing90010b32014-06-07 18:26:26 +02002020 if (query == &br->ip4_own_query)
2021 br_multicast_enable(&port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002022#if IS_ENABLED(CONFIG_IPV6)
2023 else
Linus Lüssing90010b32014-06-07 18:26:26 +02002024 br_multicast_enable(&port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002025#endif
Herbert Xu74857212012-04-13 02:37:42 +00002026 }
Nikolay Aleksandrovc5b493c2019-04-11 15:08:25 +03002027 rcu_read_unlock();
Herbert Xu74857212012-04-13 02:37:42 +00002028}
2029
2030int br_multicast_toggle(struct net_bridge *br, unsigned long val)
2031{
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02002032 struct net_bridge_port *port;
Herbert Xu561f1102010-02-27 19:41:50 +00002033
Andrey Vaginef5e0d82011-11-10 05:48:03 +00002034 spin_lock_bh(&br->multicast_lock);
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +03002035 if (!!br_opt_get(br, BROPT_MULTICAST_ENABLED) == !!val)
Herbert Xu561f1102010-02-27 19:41:50 +00002036 goto unlock;
2037
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +03002038 br_mc_disabled_update(br->dev, val);
2039 br_opt_toggle(br, BROPT_MULTICAST_ENABLED, !!val);
Linus Lüssing4effd282019-01-21 07:26:27 +01002040 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) {
2041 br_multicast_leave_snoopers(br);
Herbert Xu561f1102010-02-27 19:41:50 +00002042 goto unlock;
Linus Lüssing4effd282019-01-21 07:26:27 +01002043 }
Herbert Xu561f1102010-02-27 19:41:50 +00002044
Herbert Xu3a7fda02010-07-29 00:45:30 +00002045 if (!netif_running(br->dev))
2046 goto unlock;
2047
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02002048 br_multicast_open(br);
2049 list_for_each_entry(port, &br->port_list, list)
2050 __br_multicast_enable_port(port);
Herbert Xu561f1102010-02-27 19:41:50 +00002051
2052unlock:
Andrey Vaginef5e0d82011-11-10 05:48:03 +00002053 spin_unlock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00002054
YueHaibinga26d94b2018-12-17 17:46:23 +08002055 return 0;
Herbert Xu561f1102010-02-27 19:41:50 +00002056}
Herbert Xub1951672010-02-27 19:41:51 +00002057
Ido Schimmel9341b982017-05-26 08:37:24 +02002058bool br_multicast_enabled(const struct net_device *dev)
2059{
2060 struct net_bridge *br = netdev_priv(dev);
2061
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +03002062 return !!br_opt_get(br, BROPT_MULTICAST_ENABLED);
Ido Schimmel9341b982017-05-26 08:37:24 +02002063}
2064EXPORT_SYMBOL_GPL(br_multicast_enabled);
2065
Yotam Gigi0912bda2017-10-09 11:15:32 +02002066bool br_multicast_router(const struct net_device *dev)
2067{
2068 struct net_bridge *br = netdev_priv(dev);
2069 bool is_router;
2070
2071 spin_lock_bh(&br->multicast_lock);
2072 is_router = br_multicast_is_router(br);
2073 spin_unlock_bh(&br->multicast_lock);
2074 return is_router;
2075}
2076EXPORT_SYMBOL_GPL(br_multicast_router);
2077
Herbert Xuc5c23262012-04-13 02:37:42 +00002078int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
2079{
Linus Lüssingb00589a2013-08-01 01:06:20 +02002080 unsigned long max_delay;
2081
Herbert Xuc5c23262012-04-13 02:37:42 +00002082 val = !!val;
2083
2084 spin_lock_bh(&br->multicast_lock);
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03002085 if (br_opt_get(br, BROPT_MULTICAST_QUERIER) == val)
Herbert Xuc5c23262012-04-13 02:37:42 +00002086 goto unlock;
2087
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03002088 br_opt_toggle(br, BROPT_MULTICAST_QUERIER, !!val);
Linus Lüssingb00589a2013-08-01 01:06:20 +02002089 if (!val)
2090 goto unlock;
2091
2092 max_delay = br->multicast_query_response_interval;
Linus Lüssingb00589a2013-08-01 01:06:20 +02002093
Linus Lüssing90010b32014-06-07 18:26:26 +02002094 if (!timer_pending(&br->ip4_other_query.timer))
2095 br->ip4_other_query.delay_time = jiffies + max_delay;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002096
Linus Lüssing90010b32014-06-07 18:26:26 +02002097 br_multicast_start_querier(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002098
2099#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02002100 if (!timer_pending(&br->ip6_other_query.timer))
2101 br->ip6_other_query.delay_time = jiffies + max_delay;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002102
Linus Lüssing90010b32014-06-07 18:26:26 +02002103 br_multicast_start_querier(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002104#endif
Herbert Xuc5c23262012-04-13 02:37:42 +00002105
2106unlock:
2107 spin_unlock_bh(&br->multicast_lock);
2108
2109 return 0;
2110}
2111
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01002112int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val)
2113{
2114 /* Currently we support only version 2 and 3 */
2115 switch (val) {
2116 case 2:
2117 case 3:
2118 break;
2119 default:
2120 return -EINVAL;
2121 }
2122
2123 spin_lock_bh(&br->multicast_lock);
2124 br->multicast_igmp_version = val;
2125 spin_unlock_bh(&br->multicast_lock);
2126
2127 return 0;
2128}
2129
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01002130#if IS_ENABLED(CONFIG_IPV6)
2131int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val)
2132{
2133 /* Currently we support version 1 and 2 */
2134 switch (val) {
2135 case 1:
2136 case 2:
2137 break;
2138 default:
2139 return -EINVAL;
2140 }
2141
2142 spin_lock_bh(&br->multicast_lock);
2143 br->multicast_mld_version = val;
2144 spin_unlock_bh(&br->multicast_lock);
2145
2146 return 0;
2147}
2148#endif
2149
Linus Lüssing07f8ac42014-06-07 18:26:28 +02002150/**
2151 * br_multicast_list_adjacent - Returns snooped multicast addresses
2152 * @dev: The bridge port adjacent to which to retrieve addresses
2153 * @br_ip_list: The list to store found, snooped multicast IP addresses in
2154 *
2155 * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
2156 * snooping feature on all bridge ports of dev's bridge device, excluding
2157 * the addresses from dev itself.
2158 *
2159 * Returns the number of items added to br_ip_list.
2160 *
2161 * Notes:
2162 * - br_ip_list needs to be initialized by caller
2163 * - br_ip_list might contain duplicates in the end
2164 * (needs to be taken care of by caller)
2165 * - br_ip_list needs to be freed by caller
2166 */
2167int br_multicast_list_adjacent(struct net_device *dev,
2168 struct list_head *br_ip_list)
2169{
2170 struct net_bridge *br;
2171 struct net_bridge_port *port;
2172 struct net_bridge_port_group *group;
2173 struct br_ip_list *entry;
2174 int count = 0;
2175
2176 rcu_read_lock();
Julian Wiedmann35f861e2019-03-29 14:38:19 +01002177 if (!br_ip_list || !netif_is_bridge_port(dev))
Linus Lüssing07f8ac42014-06-07 18:26:28 +02002178 goto unlock;
2179
2180 port = br_port_get_rcu(dev);
2181 if (!port || !port->br)
2182 goto unlock;
2183
2184 br = port->br;
2185
2186 list_for_each_entry_rcu(port, &br->port_list, list) {
2187 if (!port->dev || port->dev == dev)
2188 continue;
2189
2190 hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
2191 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
2192 if (!entry)
2193 goto unlock;
2194
2195 entry->addr = group->addr;
2196 list_add(&entry->list, br_ip_list);
2197 count++;
2198 }
2199 }
2200
2201unlock:
2202 rcu_read_unlock();
2203 return count;
2204}
2205EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
Linus Lüssing2cd41432014-06-07 18:26:29 +02002206
2207/**
Linus Lüssingc34963e2014-07-07 05:41:17 +02002208 * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2209 * @dev: The bridge port providing the bridge on which to check for a querier
2210 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2211 *
2212 * Checks whether the given interface has a bridge on top and if so returns
2213 * true if a valid querier exists anywhere on the bridged link layer.
2214 * Otherwise returns false.
2215 */
2216bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
2217{
2218 struct net_bridge *br;
2219 struct net_bridge_port *port;
2220 struct ethhdr eth;
2221 bool ret = false;
2222
2223 rcu_read_lock();
Julian Wiedmann35f861e2019-03-29 14:38:19 +01002224 if (!netif_is_bridge_port(dev))
Linus Lüssingc34963e2014-07-07 05:41:17 +02002225 goto unlock;
2226
2227 port = br_port_get_rcu(dev);
2228 if (!port || !port->br)
2229 goto unlock;
2230
2231 br = port->br;
2232
2233 memset(&eth, 0, sizeof(eth));
2234 eth.h_proto = htons(proto);
2235
2236 ret = br_multicast_querier_exists(br, &eth);
2237
2238unlock:
2239 rcu_read_unlock();
2240 return ret;
2241}
2242EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
2243
2244/**
Linus Lüssing2cd41432014-06-07 18:26:29 +02002245 * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
2246 * @dev: The bridge port adjacent to which to check for a querier
2247 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2248 *
2249 * Checks whether the given interface has a bridge on top and if so returns
2250 * true if a selected querier is behind one of the other ports of this
2251 * bridge. Otherwise returns false.
2252 */
2253bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
2254{
2255 struct net_bridge *br;
2256 struct net_bridge_port *port;
2257 bool ret = false;
2258
2259 rcu_read_lock();
Julian Wiedmann35f861e2019-03-29 14:38:19 +01002260 if (!netif_is_bridge_port(dev))
Linus Lüssing2cd41432014-06-07 18:26:29 +02002261 goto unlock;
2262
2263 port = br_port_get_rcu(dev);
2264 if (!port || !port->br)
2265 goto unlock;
2266
2267 br = port->br;
2268
2269 switch (proto) {
2270 case ETH_P_IP:
2271 if (!timer_pending(&br->ip4_other_query.timer) ||
2272 rcu_dereference(br->ip4_querier.port) == port)
2273 goto unlock;
2274 break;
Linus Lüssing3993c4e2014-06-12 01:41:24 +02002275#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing2cd41432014-06-07 18:26:29 +02002276 case ETH_P_IPV6:
2277 if (!timer_pending(&br->ip6_other_query.timer) ||
2278 rcu_dereference(br->ip6_querier.port) == port)
2279 goto unlock;
2280 break;
Linus Lüssing3993c4e2014-06-12 01:41:24 +02002281#endif
Linus Lüssing2cd41432014-06-07 18:26:29 +02002282 default:
2283 goto unlock;
2284 }
2285
2286 ret = true;
2287unlock:
2288 rcu_read_unlock();
2289 return ret;
2290}
2291EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002292
2293static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002294 const struct sk_buff *skb, u8 type, u8 dir)
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002295{
2296 struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002297 __be16 proto = skb->protocol;
2298 unsigned int t_len;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002299
2300 u64_stats_update_begin(&pstats->syncp);
2301 switch (proto) {
2302 case htons(ETH_P_IP):
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002303 t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002304 switch (type) {
2305 case IGMP_HOST_MEMBERSHIP_REPORT:
2306 pstats->mstats.igmp_v1reports[dir]++;
2307 break;
2308 case IGMPV2_HOST_MEMBERSHIP_REPORT:
2309 pstats->mstats.igmp_v2reports[dir]++;
2310 break;
2311 case IGMPV3_HOST_MEMBERSHIP_REPORT:
2312 pstats->mstats.igmp_v3reports[dir]++;
2313 break;
2314 case IGMP_HOST_MEMBERSHIP_QUERY:
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002315 if (t_len != sizeof(struct igmphdr)) {
2316 pstats->mstats.igmp_v3queries[dir]++;
2317 } else {
2318 unsigned int offset = skb_transport_offset(skb);
2319 struct igmphdr *ih, _ihdr;
2320
2321 ih = skb_header_pointer(skb, offset,
2322 sizeof(_ihdr), &_ihdr);
2323 if (!ih)
2324 break;
2325 if (!ih->code)
2326 pstats->mstats.igmp_v1queries[dir]++;
2327 else
2328 pstats->mstats.igmp_v2queries[dir]++;
2329 }
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002330 break;
2331 case IGMP_HOST_LEAVE_MESSAGE:
2332 pstats->mstats.igmp_leaves[dir]++;
2333 break;
2334 }
2335 break;
2336#if IS_ENABLED(CONFIG_IPV6)
2337 case htons(ETH_P_IPV6):
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002338 t_len = ntohs(ipv6_hdr(skb)->payload_len) +
2339 sizeof(struct ipv6hdr);
2340 t_len -= skb_network_header_len(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002341 switch (type) {
2342 case ICMPV6_MGM_REPORT:
2343 pstats->mstats.mld_v1reports[dir]++;
2344 break;
2345 case ICMPV6_MLD2_REPORT:
2346 pstats->mstats.mld_v2reports[dir]++;
2347 break;
2348 case ICMPV6_MGM_QUERY:
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002349 if (t_len != sizeof(struct mld_msg))
2350 pstats->mstats.mld_v2queries[dir]++;
2351 else
2352 pstats->mstats.mld_v1queries[dir]++;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002353 break;
2354 case ICMPV6_MGM_REDUCTION:
2355 pstats->mstats.mld_leaves[dir]++;
2356 break;
2357 }
2358 break;
2359#endif /* CONFIG_IPV6 */
2360 }
2361 u64_stats_update_end(&pstats->syncp);
2362}
2363
2364void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002365 const struct sk_buff *skb, u8 type, u8 dir)
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002366{
2367 struct bridge_mcast_stats __percpu *stats;
2368
2369 /* if multicast_disabled is true then igmp type can't be set */
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03002370 if (!type || !br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002371 return;
2372
2373 if (p)
2374 stats = p->mcast_stats;
2375 else
2376 stats = br->mcast_stats;
2377 if (WARN_ON(!stats))
2378 return;
2379
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002380 br_mcast_stats_add(stats, skb, type, dir);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002381}
2382
2383int br_multicast_init_stats(struct net_bridge *br)
2384{
2385 br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
2386 if (!br->mcast_stats)
2387 return -ENOMEM;
2388
2389 return 0;
2390}
2391
Ido Schimmelb6fe0442017-04-10 14:59:27 +03002392void br_multicast_uninit_stats(struct net_bridge *br)
2393{
2394 free_percpu(br->mcast_stats);
2395}
2396
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002397static void mcast_stats_add_dir(u64 *dst, u64 *src)
2398{
2399 dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
2400 dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
2401}
2402
2403void br_multicast_get_stats(const struct net_bridge *br,
2404 const struct net_bridge_port *p,
2405 struct br_mcast_stats *dest)
2406{
2407 struct bridge_mcast_stats __percpu *stats;
2408 struct br_mcast_stats tdst;
2409 int i;
2410
2411 memset(dest, 0, sizeof(*dest));
2412 if (p)
2413 stats = p->mcast_stats;
2414 else
2415 stats = br->mcast_stats;
2416 if (WARN_ON(!stats))
2417 return;
2418
2419 memset(&tdst, 0, sizeof(tdst));
2420 for_each_possible_cpu(i) {
2421 struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
2422 struct br_mcast_stats temp;
2423 unsigned int start;
2424
2425 do {
2426 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
2427 memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
2428 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
2429
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002430 mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
2431 mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
2432 mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002433 mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
2434 mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
2435 mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
2436 mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
2437 tdst.igmp_parse_errors += temp.igmp_parse_errors;
2438
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002439 mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
2440 mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002441 mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
2442 mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
2443 mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
2444 tdst.mld_parse_errors += temp.mld_parse_errors;
2445 }
2446 memcpy(dest, &tdst, sizeof(*dest));
2447}
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02002448
2449int br_mdb_hash_init(struct net_bridge *br)
2450{
2451 return rhashtable_init(&br->mdb_hash_tbl, &br_mdb_rht_params);
2452}
2453
2454void br_mdb_hash_fini(struct net_bridge *br)
2455{
2456 rhashtable_destroy(&br->mdb_hash_tbl);
2457}