blob: ad12fe3fca8cfb4888e500b11cd6ade9fb67ed53 [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
Nikolay Aleksandrov1bc844e2019-08-17 14:22:13 +0300151 br_multicast_host_leave(mp, true);
Herbert Xueb1d1642010-02-27 19:41:45 +0000152
153 if (mp->ports)
154 goto out;
155
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200156 rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode,
157 br_mdb_rht_params);
158 hlist_del_rcu(&mp->mdb_node);
Herbert Xueb1d1642010-02-27 19:41:45 +0000159
Nikolay Aleksandrov43295962018-12-05 15:14:25 +0200160 kfree_rcu(mp, rcu);
Herbert Xueb1d1642010-02-27 19:41:45 +0000161
162out:
163 spin_unlock(&br->multicast_lock);
164}
165
166static void br_multicast_del_pg(struct net_bridge *br,
167 struct net_bridge_port_group *pg)
168{
Herbert Xueb1d1642010-02-27 19:41:45 +0000169 struct net_bridge_mdb_entry *mp;
170 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +0000171 struct net_bridge_port_group __rcu **pp;
172
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200173 mp = br_mdb_ip_get(br, &pg->addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000174 if (WARN_ON(!mp))
175 return;
176
Eric Dumazete8051682010-11-15 06:38:10 +0000177 for (pp = &mp->ports;
178 (p = mlock_dereference(*pp, br)) != NULL;
179 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000180 if (p != pg)
181 continue;
182
stephen hemminger83f6a742010-04-27 15:01:06 +0000183 rcu_assign_pointer(*pp, p->next);
Herbert Xueb1d1642010-02-27 19:41:45 +0000184 hlist_del_init(&p->mglist);
185 del_timer(&p->timer);
Elad Raz45ebcce2016-04-21 12:52:45 +0200186 br_mdb_notify(br->dev, p->port, &pg->addr, RTM_DELMDB,
187 p->flags);
Nikolay Aleksandrov43295962018-12-05 15:14:25 +0200188 kfree_rcu(p, rcu);
Herbert Xueb1d1642010-02-27 19:41:45 +0000189
Andrew Lunnff0fd342017-11-09 23:10:57 +0100190 if (!mp->ports && !mp->host_joined &&
Herbert Xueb1d1642010-02-27 19:41:45 +0000191 netif_running(br->dev))
192 mod_timer(&mp->timer, jiffies);
193
194 return;
195 }
196
197 WARN_ON(1);
198}
199
Allen Pais88c1f372017-11-03 11:51:10 +0530200static void br_multicast_port_group_expired(struct timer_list *t)
Herbert Xueb1d1642010-02-27 19:41:45 +0000201{
Allen Pais88c1f372017-11-03 11:51:10 +0530202 struct net_bridge_port_group *pg = from_timer(pg, t, timer);
Herbert Xueb1d1642010-02-27 19:41:45 +0000203 struct net_bridge *br = pg->port->br;
204
205 spin_lock(&br->multicast_lock);
206 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
Elad Raz9d06b6d2016-02-03 09:57:05 +0100207 hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT)
Herbert Xueb1d1642010-02-27 19:41:45 +0000208 goto out;
209
210 br_multicast_del_pg(br, pg);
211
212out:
213 spin_unlock(&br->multicast_lock);
214}
215
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900216static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200217 __be32 group,
218 u8 *igmp_type)
Herbert Xueb1d1642010-02-27 19:41:45 +0000219{
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100220 struct igmpv3_query *ihv3;
221 size_t igmp_hdr_size;
Herbert Xueb1d1642010-02-27 19:41:45 +0000222 struct sk_buff *skb;
223 struct igmphdr *ih;
224 struct ethhdr *eth;
225 struct iphdr *iph;
226
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100227 igmp_hdr_size = sizeof(*ih);
228 if (br->multicast_igmp_version == 3)
229 igmp_hdr_size = sizeof(*ihv3);
Herbert Xueb1d1642010-02-27 19:41:45 +0000230 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100231 igmp_hdr_size + 4);
Herbert Xueb1d1642010-02-27 19:41:45 +0000232 if (!skb)
233 goto out;
234
235 skb->protocol = htons(ETH_P_IP);
236
237 skb_reset_mac_header(skb);
238 eth = eth_hdr(skb);
239
Joe Perchese5a727f2014-02-23 00:05:25 -0800240 ether_addr_copy(eth->h_source, br->dev->dev_addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000241 eth->h_dest[0] = 1;
242 eth->h_dest[1] = 0;
243 eth->h_dest[2] = 0x5e;
244 eth->h_dest[3] = 0;
245 eth->h_dest[4] = 0;
246 eth->h_dest[5] = 1;
247 eth->h_proto = htons(ETH_P_IP);
248 skb_put(skb, sizeof(*eth));
249
250 skb_set_network_header(skb, skb->len);
251 iph = ip_hdr(skb);
252
253 iph->version = 4;
254 iph->ihl = 6;
255 iph->tos = 0xc0;
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100256 iph->tot_len = htons(sizeof(*iph) + igmp_hdr_size + 4);
Herbert Xueb1d1642010-02-27 19:41:45 +0000257 iph->id = 0;
258 iph->frag_off = htons(IP_DF);
259 iph->ttl = 1;
260 iph->protocol = IPPROTO_IGMP;
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +0300261 iph->saddr = br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR) ?
Cong Wang1c8ad5b2013-05-21 21:52:54 +0000262 inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000263 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
264 ((u8 *)&iph[1])[0] = IPOPT_RA;
265 ((u8 *)&iph[1])[1] = 4;
266 ((u8 *)&iph[1])[2] = 0;
267 ((u8 *)&iph[1])[3] = 0;
268 ip_send_check(iph);
269 skb_put(skb, 24);
270
271 skb_set_transport_header(skb, skb->len);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200272 *igmp_type = IGMP_HOST_MEMBERSHIP_QUERY;
Herbert Xueb1d1642010-02-27 19:41:45 +0000273
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100274 switch (br->multicast_igmp_version) {
275 case 2:
276 ih = igmp_hdr(skb);
277 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
278 ih->code = (group ? br->multicast_last_member_interval :
279 br->multicast_query_response_interval) /
280 (HZ / IGMP_TIMER_SCALE);
281 ih->group = group;
282 ih->csum = 0;
283 ih->csum = ip_compute_csum((void *)ih, sizeof(*ih));
284 break;
285 case 3:
286 ihv3 = igmpv3_query_hdr(skb);
287 ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY;
288 ihv3->code = (group ? br->multicast_last_member_interval :
289 br->multicast_query_response_interval) /
290 (HZ / IGMP_TIMER_SCALE);
291 ihv3->group = group;
292 ihv3->qqic = br->multicast_query_interval / HZ;
293 ihv3->nsrcs = 0;
294 ihv3->resv = 0;
295 ihv3->suppress = 0;
296 ihv3->qrv = 2;
297 ihv3->csum = 0;
298 ihv3->csum = ip_compute_csum((void *)ihv3, sizeof(*ihv3));
299 break;
300 }
301
302 skb_put(skb, igmp_hdr_size);
Herbert Xueb1d1642010-02-27 19:41:45 +0000303 __skb_pull(skb, sizeof(*eth));
304
305out:
306 return skb;
307}
308
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000309#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900310static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200311 const struct in6_addr *grp,
312 u8 *igmp_type)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900313{
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100314 struct mld2_query *mld2q;
315 unsigned long interval;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900316 struct ipv6hdr *ip6h;
317 struct mld_msg *mldq;
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100318 size_t mld_hdr_size;
319 struct sk_buff *skb;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900320 struct ethhdr *eth;
321 u8 *hopopt;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900322
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100323 mld_hdr_size = sizeof(*mldq);
324 if (br->multicast_mld_version == 2)
325 mld_hdr_size = sizeof(*mld2q);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900326 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100327 8 + mld_hdr_size);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900328 if (!skb)
329 goto out;
330
331 skb->protocol = htons(ETH_P_IPV6);
332
333 /* Ethernet header */
334 skb_reset_mac_header(skb);
335 eth = eth_hdr(skb);
336
Joe Perchese5a727f2014-02-23 00:05:25 -0800337 ether_addr_copy(eth->h_source, br->dev->dev_addr);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900338 eth->h_proto = htons(ETH_P_IPV6);
339 skb_put(skb, sizeof(*eth));
340
341 /* IPv6 header + HbH option */
342 skb_set_network_header(skb, skb->len);
343 ip6h = ipv6_hdr(skb);
344
345 *(__force __be32 *)ip6h = htonl(0x60000000);
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100346 ip6h->payload_len = htons(8 + mld_hdr_size);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900347 ip6h->nexthdr = IPPROTO_HOPOPTS;
348 ip6h->hop_limit = 1;
Linus Lüssinga7bff752011-03-22 11:40:32 +0000349 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
Ulrich Weberd1d81d42012-03-05 04:52:44 +0000350 if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
351 &ip6h->saddr)) {
352 kfree_skb(skb);
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +0300353 br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, false);
Ulrich Weberd1d81d42012-03-05 04:52:44 +0000354 return NULL;
355 }
daniel0888d5f2016-06-24 12:35:18 +0200356
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +0300357 br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true);
Linus Lüssing36cff5a2011-02-17 08:17:51 +0000358 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900359
360 hopopt = (u8 *)(ip6h + 1);
361 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
362 hopopt[1] = 0; /* length of HbH */
363 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
364 hopopt[3] = 2; /* Length of RA Option */
365 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
366 hopopt[5] = 0;
Eldad Zack1de5a712012-05-17 06:00:25 +0000367 hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */
368 hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900369
370 skb_put(skb, sizeof(*ip6h) + 8);
371
372 /* ICMPv6 */
373 skb_set_transport_header(skb, skb->len);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200374 interval = ipv6_addr_any(grp) ?
Linus Lüssing32de8682013-06-16 23:20:34 +0200375 br->multicast_query_response_interval :
376 br->multicast_last_member_interval;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200377 *igmp_type = ICMPV6_MGM_QUERY;
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100378 switch (br->multicast_mld_version) {
379 case 1:
380 mldq = (struct mld_msg *)icmp6_hdr(skb);
381 mldq->mld_type = ICMPV6_MGM_QUERY;
382 mldq->mld_code = 0;
383 mldq->mld_cksum = 0;
384 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
385 mldq->mld_reserved = 0;
386 mldq->mld_mca = *grp;
387 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
388 sizeof(*mldq), IPPROTO_ICMPV6,
389 csum_partial(mldq,
390 sizeof(*mldq),
391 0));
392 break;
393 case 2:
394 mld2q = (struct mld2_query *)icmp6_hdr(skb);
Lance Richardson53631a52017-01-16 18:11:35 -0500395 mld2q->mld2q_mrc = htons((u16)jiffies_to_msecs(interval));
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100396 mld2q->mld2q_type = ICMPV6_MGM_QUERY;
397 mld2q->mld2q_code = 0;
398 mld2q->mld2q_cksum = 0;
399 mld2q->mld2q_resv1 = 0;
400 mld2q->mld2q_resv2 = 0;
401 mld2q->mld2q_suppress = 0;
402 mld2q->mld2q_qrv = 2;
403 mld2q->mld2q_nsrcs = 0;
404 mld2q->mld2q_qqic = br->multicast_query_interval / HZ;
405 mld2q->mld2q_mca = *grp;
406 mld2q->mld2q_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
407 sizeof(*mld2q),
408 IPPROTO_ICMPV6,
409 csum_partial(mld2q,
410 sizeof(*mld2q),
411 0));
412 break;
413 }
414 skb_put(skb, mld_hdr_size);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900415
416 __skb_pull(skb, sizeof(*eth));
417
418out:
419 return skb;
420}
421#endif
422
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900423static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200424 struct br_ip *addr,
425 u8 *igmp_type)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900426{
427 switch (addr->proto) {
428 case htons(ETH_P_IP):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200429 return br_ip4_multicast_alloc_query(br, addr->u.ip4, igmp_type);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000430#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900431 case htons(ETH_P_IPV6):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200432 return br_ip6_multicast_alloc_query(br, &addr->u.ip6,
433 igmp_type);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900434#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900435 }
436 return NULL;
437}
438
Cong Wangcfd56752012-12-11 22:23:08 +0000439struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100440 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000441{
Herbert Xueb1d1642010-02-27 19:41:45 +0000442 struct net_bridge_mdb_entry *mp;
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000443 int err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000444
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200445 mp = br_mdb_ip_get(br, group);
446 if (mp)
447 return mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000448
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200449 if (atomic_read(&br->mdb_hash_tbl.nelems) >= br->hash_max) {
450 br_opt_toggle(br, BROPT_MULTICAST_ENABLED, false);
451 return ERR_PTR(-E2BIG);
Herbert Xueb1d1642010-02-27 19:41:45 +0000452 }
453
454 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
455 if (unlikely(!mp))
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000456 return ERR_PTR(-ENOMEM);
Herbert Xueb1d1642010-02-27 19:41:45 +0000457
458 mp->br = br;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900459 mp->addr = *group;
Allen Pais88c1f372017-11-03 11:51:10 +0530460 timer_setup(&mp->timer, br_multicast_group_expired, 0);
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200461 err = rhashtable_lookup_insert_fast(&br->mdb_hash_tbl, &mp->rhnode,
462 br_mdb_rht_params);
463 if (err) {
464 kfree(mp);
465 mp = ERR_PTR(err);
466 } else {
467 hlist_add_head_rcu(&mp->mdb_node, &br->mdb_list);
468 }
Eric Dumazet1faabf22013-07-19 20:07:16 -0700469
Herbert Xueb1d1642010-02-27 19:41:45 +0000470 return mp;
471}
472
Cong Wangcfd56752012-12-11 22:23:08 +0000473struct net_bridge_port_group *br_multicast_new_port_group(
474 struct net_bridge_port *port,
475 struct br_ip *group,
Amerigo Wangccb1c312012-12-14 22:09:51 +0000476 struct net_bridge_port_group __rcu *next,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100477 unsigned char flags,
478 const unsigned char *src)
Cong Wangcfd56752012-12-11 22:23:08 +0000479{
480 struct net_bridge_port_group *p;
481
482 p = kzalloc(sizeof(*p), GFP_ATOMIC);
483 if (unlikely(!p))
484 return NULL;
485
486 p->addr = *group;
487 p->port = port;
Elad Raz9d06b6d2016-02-03 09:57:05 +0100488 p->flags = flags;
stephen hemmingereca2a432012-12-13 06:51:28 +0000489 rcu_assign_pointer(p->next, next);
Cong Wangcfd56752012-12-11 22:23:08 +0000490 hlist_add_head(&p->mglist, &port->mglist);
Allen Pais88c1f372017-11-03 11:51:10 +0530491 timer_setup(&p->timer, br_multicast_port_group_expired, 0);
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100492
493 if (src)
494 memcpy(p->eth_addr, src, ETH_ALEN);
495 else
Mao Wenan1bfe45f2019-03-20 10:06:57 +0800496 eth_broadcast_addr(p->eth_addr);
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100497
Cong Wangcfd56752012-12-11 22:23:08 +0000498 return p;
499}
500
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100501static bool br_port_group_equal(struct net_bridge_port_group *p,
502 struct net_bridge_port *port,
503 const unsigned char *src)
504{
505 if (p->port != port)
506 return false;
507
508 if (!(port->flags & BR_MULTICAST_TO_UNICAST))
509 return true;
510
511 return ether_addr_equal(src, p->eth_addr);
512}
513
Nikolay Aleksandrov1bc844e2019-08-17 14:22:13 +0300514void br_multicast_host_join(struct net_bridge_mdb_entry *mp, bool notify)
515{
516 if (!mp->host_joined) {
517 mp->host_joined = true;
518 if (notify)
519 br_mdb_notify(mp->br->dev, NULL, &mp->addr,
520 RTM_NEWMDB, 0);
521 }
522 mod_timer(&mp->timer, jiffies + mp->br->multicast_membership_interval);
523}
524
525void br_multicast_host_leave(struct net_bridge_mdb_entry *mp, bool notify)
526{
527 if (!mp->host_joined)
528 return;
529
530 mp->host_joined = false;
531 if (notify)
532 br_mdb_notify(mp->br->dev, NULL, &mp->addr, RTM_DELMDB, 0);
533}
534
Herbert Xueb1d1642010-02-27 19:41:45 +0000535static int br_multicast_add_group(struct net_bridge *br,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900536 struct net_bridge_port *port,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100537 struct br_ip *group,
538 const unsigned char *src)
Herbert Xueb1d1642010-02-27 19:41:45 +0000539{
Eric Dumazete8051682010-11-15 06:38:10 +0000540 struct net_bridge_port_group __rcu **pp;
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100541 struct net_bridge_port_group *p;
542 struct net_bridge_mdb_entry *mp;
Linus Lüssing454594f2013-10-20 00:58:57 +0200543 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +0000544 int err;
545
Herbert Xueb1d1642010-02-27 19:41:45 +0000546 spin_lock(&br->multicast_lock);
547 if (!netif_running(br->dev) ||
548 (port && port->state == BR_STATE_DISABLED))
549 goto out;
550
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200551 mp = br_multicast_new_group(br, group);
Herbert Xueb1d1642010-02-27 19:41:45 +0000552 err = PTR_ERR(mp);
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000553 if (IS_ERR(mp))
Herbert Xueb1d1642010-02-27 19:41:45 +0000554 goto err;
555
556 if (!port) {
Nikolay Aleksandrov1bc844e2019-08-17 14:22:13 +0300557 br_multicast_host_join(mp, true);
Herbert Xueb1d1642010-02-27 19:41:45 +0000558 goto out;
559 }
560
Eric Dumazete8051682010-11-15 06:38:10 +0000561 for (pp = &mp->ports;
562 (p = mlock_dereference(*pp, br)) != NULL;
563 pp = &p->next) {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100564 if (br_port_group_equal(p, port, src))
Linus Lüssing454594f2013-10-20 00:58:57 +0200565 goto found;
Herbert Xueb1d1642010-02-27 19:41:45 +0000566 if ((unsigned long)p->port < (unsigned long)port)
567 break;
568 }
569
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100570 p = br_multicast_new_port_group(port, group, *pp, 0, src);
Herbert Xueb1d1642010-02-27 19:41:45 +0000571 if (unlikely(!p))
572 goto err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000573 rcu_assign_pointer(*pp, p);
Elad Raz45ebcce2016-04-21 12:52:45 +0200574 br_mdb_notify(br->dev, port, group, RTM_NEWMDB, 0);
Herbert Xueb1d1642010-02-27 19:41:45 +0000575
Linus Lüssing454594f2013-10-20 00:58:57 +0200576found:
577 mod_timer(&p->timer, now + br->multicast_membership_interval);
Herbert Xueb1d1642010-02-27 19:41:45 +0000578out:
579 err = 0;
580
581err:
582 spin_unlock(&br->multicast_lock);
583 return err;
584}
585
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900586static int br_ip4_multicast_add_group(struct net_bridge *br,
587 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000588 __be32 group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100589 __u16 vid,
590 const unsigned char *src)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900591{
592 struct br_ip br_group;
593
594 if (ipv4_is_local_multicast(group))
595 return 0;
596
Nikolay Aleksandrov1515a63f2019-04-03 23:27:24 +0300597 memset(&br_group, 0, sizeof(br_group));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900598 br_group.u.ip4 = group;
599 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000600 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900601
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100602 return br_multicast_add_group(br, port, &br_group, src);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900603}
604
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000605#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900606static int br_ip6_multicast_add_group(struct net_bridge *br,
607 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000608 const struct in6_addr *group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100609 __u16 vid,
610 const unsigned char *src)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900611{
612 struct br_ip br_group;
613
Linus Lüssing3c3769e2013-09-04 02:13:39 +0200614 if (ipv6_addr_is_ll_all_nodes(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900615 return 0;
616
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200617 memset(&br_group, 0, sizeof(br_group));
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000618 br_group.u.ip6 = *group;
Linus Lüssing9cc6e0c2011-02-15 13:19:17 +0000619 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000620 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900621
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100622 return br_multicast_add_group(br, port, &br_group, src);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900623}
624#endif
625
Allen Pais88c1f372017-11-03 11:51:10 +0530626static void br_multicast_router_expired(struct timer_list *t)
Herbert Xueb1d1642010-02-27 19:41:45 +0000627{
Allen Pais88c1f372017-11-03 11:51:10 +0530628 struct net_bridge_port *port =
629 from_timer(port, t, multicast_router_timer);
Herbert Xueb1d1642010-02-27 19:41:45 +0000630 struct net_bridge *br = port->br;
631
632 spin_lock(&br->multicast_lock);
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +0100633 if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
634 port->multicast_router == MDB_RTR_TYPE_PERM ||
Nogah Frankelf12e7d92017-02-09 14:54:41 +0100635 timer_pending(&port->multicast_router_timer))
Herbert Xueb1d1642010-02-27 19:41:45 +0000636 goto out;
637
Nogah Frankelf12e7d92017-02-09 14:54:41 +0100638 __del_port_router(port);
Herbert Xueb1d1642010-02-27 19:41:45 +0000639out:
640 spin_unlock(&br->multicast_lock);
641}
642
Yotam Gigi77041422017-10-09 11:15:31 +0200643static void br_mc_router_state_change(struct net_bridge *p,
644 bool is_mc_router)
645{
646 struct switchdev_attr attr = {
647 .orig_dev = p->dev,
648 .id = SWITCHDEV_ATTR_ID_BRIDGE_MROUTER,
649 .flags = SWITCHDEV_F_DEFER,
650 .u.mrouter = is_mc_router,
651 };
652
653 switchdev_port_attr_set(p->dev, &attr);
654}
655
Allen Pais88c1f372017-11-03 11:51:10 +0530656static void br_multicast_local_router_expired(struct timer_list *t)
Herbert Xueb1d1642010-02-27 19:41:45 +0000657{
Allen Pais88c1f372017-11-03 11:51:10 +0530658 struct net_bridge *br = from_timer(br, t, multicast_router_timer);
Yotam Gigi77041422017-10-09 11:15:31 +0200659
660 spin_lock(&br->multicast_lock);
661 if (br->multicast_router == MDB_RTR_TYPE_DISABLED ||
662 br->multicast_router == MDB_RTR_TYPE_PERM ||
663 timer_pending(&br->multicast_router_timer))
664 goto out;
665
666 br_mc_router_state_change(br, false);
667out:
668 spin_unlock(&br->multicast_lock);
Herbert Xueb1d1642010-02-27 19:41:45 +0000669}
670
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200671static void br_multicast_querier_expired(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +0200672 struct bridge_mcast_own_query *query)
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000673{
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000674 spin_lock(&br->multicast_lock);
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +0300675 if (!netif_running(br->dev) || !br_opt_get(br, BROPT_MULTICAST_ENABLED))
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000676 goto out;
677
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200678 br_multicast_start_querier(br, query);
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000679
680out:
681 spin_unlock(&br->multicast_lock);
682}
683
Allen Pais88c1f372017-11-03 11:51:10 +0530684static void br_ip4_multicast_querier_expired(struct timer_list *t)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200685{
Allen Pais88c1f372017-11-03 11:51:10 +0530686 struct net_bridge *br = from_timer(br, t, ip4_other_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200687
Linus Lüssing90010b32014-06-07 18:26:26 +0200688 br_multicast_querier_expired(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200689}
690
691#if IS_ENABLED(CONFIG_IPV6)
Allen Pais88c1f372017-11-03 11:51:10 +0530692static void br_ip6_multicast_querier_expired(struct timer_list *t)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200693{
Allen Pais88c1f372017-11-03 11:51:10 +0530694 struct net_bridge *br = from_timer(br, t, ip6_other_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200695
Linus Lüssing90010b32014-06-07 18:26:26 +0200696 br_multicast_querier_expired(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200697}
698#endif
699
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200700static void br_multicast_select_own_querier(struct net_bridge *br,
701 struct br_ip *ip,
702 struct sk_buff *skb)
703{
704 if (ip->proto == htons(ETH_P_IP))
705 br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr;
706#if IS_ENABLED(CONFIG_IPV6)
707 else
708 br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr;
709#endif
710}
711
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900712static void __br_multicast_send_query(struct net_bridge *br,
713 struct net_bridge_port *port,
714 struct br_ip *ip)
Herbert Xueb1d1642010-02-27 19:41:45 +0000715{
Herbert Xueb1d1642010-02-27 19:41:45 +0000716 struct sk_buff *skb;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200717 u8 igmp_type;
Herbert Xueb1d1642010-02-27 19:41:45 +0000718
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200719 skb = br_multicast_alloc_query(br, ip, &igmp_type);
Herbert Xueb1d1642010-02-27 19:41:45 +0000720 if (!skb)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900721 return;
Herbert Xueb1d1642010-02-27 19:41:45 +0000722
723 if (port) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000724 skb->dev = port->dev;
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -0700725 br_multicast_count(br, port, skb, igmp_type,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200726 BR_MCAST_DIR_TX);
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500727 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
728 dev_net(port->dev), NULL, skb, NULL, skb->dev,
Linus Lüssingf0b4eec2014-11-17 12:20:28 +0100729 br_dev_queue_push_xmit);
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200730 } else {
731 br_multicast_select_own_querier(br, ip, skb);
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -0700732 br_multicast_count(br, port, skb, igmp_type,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200733 BR_MCAST_DIR_RX);
Herbert Xueb1d1642010-02-27 19:41:45 +0000734 netif_rx(skb);
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200735 }
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900736}
Herbert Xueb1d1642010-02-27 19:41:45 +0000737
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900738static void br_multicast_send_query(struct net_bridge *br,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200739 struct net_bridge_port *port,
Linus Lüssing90010b32014-06-07 18:26:26 +0200740 struct bridge_mcast_own_query *own_query)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900741{
Linus Lüssing90010b32014-06-07 18:26:26 +0200742 struct bridge_mcast_other_query *other_query = NULL;
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100743 struct br_ip br_group;
744 unsigned long time;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900745
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +0300746 if (!netif_running(br->dev) ||
747 !br_opt_get(br, BROPT_MULTICAST_ENABLED) ||
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +0300748 !br_opt_get(br, BROPT_MULTICAST_QUERIER))
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900749 return;
750
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900751 memset(&br_group.u, 0, sizeof(br_group.u));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900752
Linus Lüssing90010b32014-06-07 18:26:26 +0200753 if (port ? (own_query == &port->ip4_own_query) :
754 (own_query == &br->ip4_own_query)) {
755 other_query = &br->ip4_other_query;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200756 br_group.proto = htons(ETH_P_IP);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000757#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200758 } else {
Linus Lüssing90010b32014-06-07 18:26:26 +0200759 other_query = &br->ip6_other_query;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200760 br_group.proto = htons(ETH_P_IPV6);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900761#endif
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200762 }
763
Linus Lüssing90010b32014-06-07 18:26:26 +0200764 if (!other_query || timer_pending(&other_query->timer))
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200765 return;
766
767 __br_multicast_send_query(br, port, &br_group);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900768
Herbert Xueb1d1642010-02-27 19:41:45 +0000769 time = jiffies;
Linus Lüssing90010b32014-06-07 18:26:26 +0200770 time += own_query->startup_sent < br->multicast_startup_query_count ?
Herbert Xueb1d1642010-02-27 19:41:45 +0000771 br->multicast_startup_query_interval :
772 br->multicast_query_interval;
Linus Lüssing90010b32014-06-07 18:26:26 +0200773 mod_timer(&own_query->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +0000774}
775
Linus Lüssing90010b32014-06-07 18:26:26 +0200776static void
777br_multicast_port_query_expired(struct net_bridge_port *port,
778 struct bridge_mcast_own_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +0000779{
Herbert Xueb1d1642010-02-27 19:41:45 +0000780 struct net_bridge *br = port->br;
781
782 spin_lock(&br->multicast_lock);
Dan Carpenter02a780c2010-03-06 01:14:09 +0000783 if (port->state == BR_STATE_DISABLED ||
784 port->state == BR_STATE_BLOCKING)
Herbert Xueb1d1642010-02-27 19:41:45 +0000785 goto out;
786
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200787 if (query->startup_sent < br->multicast_startup_query_count)
788 query->startup_sent++;
Herbert Xueb1d1642010-02-27 19:41:45 +0000789
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200790 br_multicast_send_query(port->br, port, query);
Herbert Xueb1d1642010-02-27 19:41:45 +0000791
792out:
793 spin_unlock(&br->multicast_lock);
794}
795
Allen Pais88c1f372017-11-03 11:51:10 +0530796static void br_ip4_multicast_port_query_expired(struct timer_list *t)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200797{
Allen Pais88c1f372017-11-03 11:51:10 +0530798 struct net_bridge_port *port = from_timer(port, t, ip4_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200799
Linus Lüssing90010b32014-06-07 18:26:26 +0200800 br_multicast_port_query_expired(port, &port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200801}
802
803#if IS_ENABLED(CONFIG_IPV6)
Allen Pais88c1f372017-11-03 11:51:10 +0530804static void br_ip6_multicast_port_query_expired(struct timer_list *t)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200805{
Allen Pais88c1f372017-11-03 11:51:10 +0530806 struct net_bridge_port *port = from_timer(port, t, ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200807
Linus Lüssing90010b32014-06-07 18:26:26 +0200808 br_multicast_port_query_expired(port, &port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200809}
810#endif
811
Nogah Frankel147c1e92017-02-09 14:54:40 +0100812static void br_mc_disabled_update(struct net_device *dev, bool value)
813{
814 struct switchdev_attr attr = {
815 .orig_dev = dev,
816 .id = SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED,
817 .flags = SWITCHDEV_F_DEFER,
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +0300818 .u.mc_disabled = !value,
Nogah Frankel147c1e92017-02-09 14:54:40 +0100819 };
820
821 switchdev_port_attr_set(dev, &attr);
822}
823
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200824int br_multicast_add_port(struct net_bridge_port *port)
Herbert Xueb1d1642010-02-27 19:41:45 +0000825{
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +0100826 port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Herbert Xueb1d1642010-02-27 19:41:45 +0000827
Allen Pais88c1f372017-11-03 11:51:10 +0530828 timer_setup(&port->multicast_router_timer,
829 br_multicast_router_expired, 0);
830 timer_setup(&port->ip4_own_query.timer,
831 br_ip4_multicast_port_query_expired, 0);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200832#if IS_ENABLED(CONFIG_IPV6)
Allen Pais88c1f372017-11-03 11:51:10 +0530833 timer_setup(&port->ip6_own_query.timer,
834 br_ip6_multicast_port_query_expired, 0);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200835#endif
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +0300836 br_mc_disabled_update(port->dev,
837 br_opt_get(port->br, BROPT_MULTICAST_ENABLED));
Nogah Frankel147c1e92017-02-09 14:54:40 +0100838
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200839 port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
840 if (!port->mcast_stats)
841 return -ENOMEM;
842
843 return 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000844}
845
846void br_multicast_del_port(struct net_bridge_port *port)
847{
Satish Ashoke10177a2015-07-15 07:16:51 -0700848 struct net_bridge *br = port->br;
849 struct net_bridge_port_group *pg;
850 struct hlist_node *n;
851
852 /* Take care of the remaining groups, only perm ones should be left */
853 spin_lock_bh(&br->multicast_lock);
854 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
855 br_multicast_del_pg(br, pg);
856 spin_unlock_bh(&br->multicast_lock);
Herbert Xueb1d1642010-02-27 19:41:45 +0000857 del_timer_sync(&port->multicast_router_timer);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200858 free_percpu(port->mcast_stats);
Herbert Xueb1d1642010-02-27 19:41:45 +0000859}
860
Linus Lüssing90010b32014-06-07 18:26:26 +0200861static void br_multicast_enable(struct bridge_mcast_own_query *query)
Herbert Xu561f1102010-02-27 19:41:50 +0000862{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200863 query->startup_sent = 0;
Herbert Xu561f1102010-02-27 19:41:50 +0000864
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200865 if (try_to_del_timer_sync(&query->timer) >= 0 ||
866 del_timer(&query->timer))
867 mod_timer(&query->timer, jiffies);
Herbert Xu561f1102010-02-27 19:41:50 +0000868}
869
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +0200870static void __br_multicast_enable_port(struct net_bridge_port *port)
Herbert Xueb1d1642010-02-27 19:41:45 +0000871{
872 struct net_bridge *br = port->br;
873
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +0300874 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED) || !netif_running(br->dev))
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +0200875 return;
Herbert Xueb1d1642010-02-27 19:41:45 +0000876
Linus Lüssing90010b32014-06-07 18:26:26 +0200877 br_multicast_enable(&port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200878#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +0200879 br_multicast_enable(&port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200880#endif
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +0100881 if (port->multicast_router == MDB_RTR_TYPE_PERM &&
882 hlist_unhashed(&port->rlist))
Satish Ashok754bc542015-06-19 01:22:57 -0700883 br_multicast_add_router(br, port);
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +0200884}
Herbert Xueb1d1642010-02-27 19:41:45 +0000885
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +0200886void br_multicast_enable_port(struct net_bridge_port *port)
887{
888 struct net_bridge *br = port->br;
889
890 spin_lock(&br->multicast_lock);
891 __br_multicast_enable_port(port);
Herbert Xueb1d1642010-02-27 19:41:45 +0000892 spin_unlock(&br->multicast_lock);
893}
894
895void br_multicast_disable_port(struct net_bridge_port *port)
896{
897 struct net_bridge *br = port->br;
898 struct net_bridge_port_group *pg;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800899 struct hlist_node *n;
Herbert Xueb1d1642010-02-27 19:41:45 +0000900
901 spin_lock(&br->multicast_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800902 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
Elad Raz9d06b6d2016-02-03 09:57:05 +0100903 if (!(pg->flags & MDB_PG_FLAGS_PERMANENT))
Satish Ashoke10177a2015-07-15 07:16:51 -0700904 br_multicast_del_pg(br, pg);
Herbert Xueb1d1642010-02-27 19:41:45 +0000905
Nogah Frankelf12e7d92017-02-09 14:54:41 +0100906 __del_port_router(port);
907
Herbert Xueb1d1642010-02-27 19:41:45 +0000908 del_timer(&port->multicast_router_timer);
Linus Lüssing90010b32014-06-07 18:26:26 +0200909 del_timer(&port->ip4_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200910#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +0200911 del_timer(&port->ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200912#endif
Herbert Xueb1d1642010-02-27 19:41:45 +0000913 spin_unlock(&br->multicast_lock);
914}
915
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900916static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
917 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -0400918 struct sk_buff *skb,
919 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000920{
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100921 const unsigned char *src;
Herbert Xueb1d1642010-02-27 19:41:45 +0000922 struct igmpv3_report *ih;
923 struct igmpv3_grec *grec;
924 int i;
925 int len;
926 int num;
927 int type;
928 int err = 0;
929 __be32 group;
Nikolay Aleksandrove57f6182019-07-02 15:00:18 +0300930 u16 nsrcs;
Herbert Xueb1d1642010-02-27 19:41:45 +0000931
Herbert Xueb1d1642010-02-27 19:41:45 +0000932 ih = igmpv3_report_hdr(skb);
933 num = ntohs(ih->ngrec);
Linus Lüssingc2d4fbd2015-09-11 18:39:48 +0200934 len = skb_transport_offset(skb) + sizeof(*ih);
Herbert Xueb1d1642010-02-27 19:41:45 +0000935
936 for (i = 0; i < num; i++) {
937 len += sizeof(*grec);
Linus Lüssingba5ea6142019-01-21 07:26:25 +0100938 if (!ip_mc_may_pull(skb, len))
Herbert Xueb1d1642010-02-27 19:41:45 +0000939 return -EINVAL;
940
Herbert Xufd218cf2010-04-07 21:20:47 -0700941 grec = (void *)(skb->data + len - sizeof(*grec));
Herbert Xueb1d1642010-02-27 19:41:45 +0000942 group = grec->grec_mca;
943 type = grec->grec_type;
Nikolay Aleksandrove57f6182019-07-02 15:00:18 +0300944 nsrcs = ntohs(grec->grec_nsrcs);
Herbert Xueb1d1642010-02-27 19:41:45 +0000945
Nikolay Aleksandrove57f6182019-07-02 15:00:18 +0300946 len += nsrcs * 4;
Linus Lüssingba5ea6142019-01-21 07:26:25 +0100947 if (!ip_mc_may_pull(skb, len))
Herbert Xueb1d1642010-02-27 19:41:45 +0000948 return -EINVAL;
949
950 /* We treat this as an IGMPv2 report for now. */
951 switch (type) {
952 case IGMPV3_MODE_IS_INCLUDE:
953 case IGMPV3_MODE_IS_EXCLUDE:
954 case IGMPV3_CHANGE_TO_INCLUDE:
955 case IGMPV3_CHANGE_TO_EXCLUDE:
956 case IGMPV3_ALLOW_NEW_SOURCES:
957 case IGMPV3_BLOCK_OLD_SOURCES:
958 break;
959
960 default:
961 continue;
962 }
963
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100964 src = eth_hdr(skb)->h_source;
Satish Ashokbc8c20a2015-07-13 05:28:37 -0700965 if ((type == IGMPV3_CHANGE_TO_INCLUDE ||
966 type == IGMPV3_MODE_IS_INCLUDE) &&
Nikolay Aleksandrove57f6182019-07-02 15:00:18 +0300967 nsrcs == 0) {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100968 br_ip4_multicast_leave_group(br, port, group, vid, src);
Satish Ashokbc8c20a2015-07-13 05:28:37 -0700969 } else {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100970 err = br_ip4_multicast_add_group(br, port, group, vid,
971 src);
Satish Ashokbc8c20a2015-07-13 05:28:37 -0700972 if (err)
973 break;
974 }
Herbert Xueb1d1642010-02-27 19:41:45 +0000975 }
976
977 return err;
978}
979
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000980#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900981static int br_ip6_multicast_mld2_report(struct net_bridge *br,
982 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -0400983 struct sk_buff *skb,
984 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900985{
Linus Lüssingba5ea6142019-01-21 07:26:25 +0100986 unsigned int nsrcs_offset;
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100987 const unsigned char *src;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900988 struct icmp6hdr *icmp6h;
989 struct mld2_grec *grec;
Linus Lüssingba5ea6142019-01-21 07:26:25 +0100990 unsigned int grec_len;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900991 int i;
992 int len;
993 int num;
994 int err = 0;
995
Linus Lüssingba5ea6142019-01-21 07:26:25 +0100996 if (!ipv6_mc_may_pull(skb, sizeof(*icmp6h)))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900997 return -EINVAL;
998
999 icmp6h = icmp6_hdr(skb);
1000 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
Linus Lüssingc2d4fbd2015-09-11 18:39:48 +02001001 len = skb_transport_offset(skb) + sizeof(*icmp6h);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001002
1003 for (i = 0; i < num; i++) {
Nikolay Aleksandrove57f6182019-07-02 15:00:18 +03001004 __be16 *_nsrcs, __nsrcs;
1005 u16 nsrcs;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001006
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001007 nsrcs_offset = len + offsetof(struct mld2_grec, grec_nsrcs);
1008
1009 if (skb_transport_offset(skb) + ipv6_transport_len(skb) <
1010 nsrcs_offset + sizeof(_nsrcs))
1011 return -EINVAL;
1012
Nikolay Aleksandrove57f6182019-07-02 15:00:18 +03001013 _nsrcs = skb_header_pointer(skb, nsrcs_offset,
1014 sizeof(__nsrcs), &__nsrcs);
1015 if (!_nsrcs)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001016 return -EINVAL;
1017
Nikolay Aleksandrove57f6182019-07-02 15:00:18 +03001018 nsrcs = ntohs(*_nsrcs);
1019 grec_len = struct_size(grec, grec_src, nsrcs);
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001020
1021 if (!ipv6_mc_may_pull(skb, len + grec_len))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001022 return -EINVAL;
1023
1024 grec = (struct mld2_grec *)(skb->data + len);
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001025 len += grec_len;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001026
1027 /* We treat these as MLDv1 reports for now. */
1028 switch (grec->grec_type) {
1029 case MLD2_MODE_IS_INCLUDE:
1030 case MLD2_MODE_IS_EXCLUDE:
1031 case MLD2_CHANGE_TO_INCLUDE:
1032 case MLD2_CHANGE_TO_EXCLUDE:
1033 case MLD2_ALLOW_NEW_SOURCES:
1034 case MLD2_BLOCK_OLD_SOURCES:
1035 break;
1036
1037 default:
1038 continue;
1039 }
1040
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001041 src = eth_hdr(skb)->h_source;
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001042 if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE ||
1043 grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
Nikolay Aleksandrove57f6182019-07-02 15:00:18 +03001044 nsrcs == 0) {
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001045 br_ip6_multicast_leave_group(br, port, &grec->grec_mca,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001046 vid, src);
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001047 } else {
1048 err = br_ip6_multicast_add_group(br, port,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001049 &grec->grec_mca, vid,
1050 src);
Davide Caratti92642512016-08-31 14:16:44 +02001051 if (err)
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001052 break;
1053 }
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001054 }
1055
1056 return err;
1057}
1058#endif
1059
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001060static bool br_ip4_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001061 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001062 __be32 saddr)
1063{
1064 if (!timer_pending(&br->ip4_own_query.timer) &&
1065 !timer_pending(&br->ip4_other_query.timer))
1066 goto update;
1067
1068 if (!br->ip4_querier.addr.u.ip4)
1069 goto update;
1070
1071 if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4))
1072 goto update;
1073
1074 return false;
1075
1076update:
1077 br->ip4_querier.addr.u.ip4 = saddr;
1078
Linus Lüssing2cd41432014-06-07 18:26:29 +02001079 /* update protected by general multicast_lock by caller */
1080 rcu_assign_pointer(br->ip4_querier.port, port);
1081
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001082 return true;
1083}
1084
1085#if IS_ENABLED(CONFIG_IPV6)
1086static bool br_ip6_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001087 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001088 struct in6_addr *saddr)
1089{
1090 if (!timer_pending(&br->ip6_own_query.timer) &&
1091 !timer_pending(&br->ip6_other_query.timer))
1092 goto update;
1093
1094 if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0)
1095 goto update;
1096
1097 return false;
1098
1099update:
1100 br->ip6_querier.addr.u.ip6 = *saddr;
1101
Linus Lüssing2cd41432014-06-07 18:26:29 +02001102 /* update protected by general multicast_lock by caller */
1103 rcu_assign_pointer(br->ip6_querier.port, port);
1104
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001105 return true;
1106}
1107#endif
1108
1109static bool br_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001110 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001111 struct br_ip *saddr)
1112{
1113 switch (saddr->proto) {
1114 case htons(ETH_P_IP):
Linus Lüssing2cd41432014-06-07 18:26:29 +02001115 return br_ip4_multicast_select_querier(br, port, saddr->u.ip4);
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001116#if IS_ENABLED(CONFIG_IPV6)
1117 case htons(ETH_P_IPV6):
Linus Lüssing2cd41432014-06-07 18:26:29 +02001118 return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6);
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001119#endif
1120 }
1121
1122 return false;
1123}
1124
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001125static void
Linus Lüssing90010b32014-06-07 18:26:26 +02001126br_multicast_update_query_timer(struct net_bridge *br,
1127 struct bridge_mcast_other_query *query,
1128 unsigned long max_delay)
Linus Lüssingb00589a2013-08-01 01:06:20 +02001129{
Linus Lüssing90010b32014-06-07 18:26:26 +02001130 if (!timer_pending(&query->timer))
1131 query->delay_time = jiffies + max_delay;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001132
Linus Lüssing90010b32014-06-07 18:26:26 +02001133 mod_timer(&query->timer, jiffies + br->multicast_querier_interval);
Linus Lüssingb00589a2013-08-01 01:06:20 +02001134}
1135
Nogah Frankel6d549642017-02-09 14:54:42 +01001136static void br_port_mc_router_state_change(struct net_bridge_port *p,
1137 bool is_mc_router)
1138{
1139 struct switchdev_attr attr = {
1140 .orig_dev = p->dev,
1141 .id = SWITCHDEV_ATTR_ID_PORT_MROUTER,
1142 .flags = SWITCHDEV_F_DEFER,
1143 .u.mrouter = is_mc_router,
1144 };
1145
1146 switchdev_port_attr_set(p->dev, &attr);
1147}
1148
stephen hemminger7e80c122010-04-27 15:01:04 +00001149/*
Cong Wang7c776022013-06-21 15:37:25 +08001150 * Add port to router_list
stephen hemminger7e80c122010-04-27 15:01:04 +00001151 * list is maintained ordered by pointer value
1152 * and locked by br->multicast_lock and RCU
1153 */
Herbert Xu0909e112010-02-27 19:41:49 +00001154static void br_multicast_add_router(struct net_bridge *br,
1155 struct net_bridge_port *port)
1156{
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001157 struct net_bridge_port *p;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001158 struct hlist_node *slot = NULL;
Herbert Xu0909e112010-02-27 19:41:49 +00001159
Nikolay Aleksandrov1a040ea2015-06-09 10:23:57 -07001160 if (!hlist_unhashed(&port->rlist))
1161 return;
1162
Sasha Levinb67bfe02013-02-27 17:06:00 -08001163 hlist_for_each_entry(p, &br->router_list, rlist) {
stephen hemminger7e80c122010-04-27 15:01:04 +00001164 if ((unsigned long) port >= (unsigned long) p)
1165 break;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001166 slot = &p->rlist;
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001167 }
Herbert Xu0909e112010-02-27 19:41:49 +00001168
stephen hemminger7e80c122010-04-27 15:01:04 +00001169 if (slot)
Ken Helias1d023282014-08-06 16:09:16 -07001170 hlist_add_behind_rcu(&port->rlist, slot);
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001171 else
1172 hlist_add_head_rcu(&port->rlist, &br->router_list);
Satish Ashok949f1e32015-07-23 05:00:53 -07001173 br_rtr_notify(br->dev, port, RTM_NEWMDB);
Nogah Frankel6d549642017-02-09 14:54:42 +01001174 br_port_mc_router_state_change(port, true);
Herbert Xu0909e112010-02-27 19:41:49 +00001175}
1176
Herbert Xueb1d1642010-02-27 19:41:45 +00001177static void br_multicast_mark_router(struct net_bridge *br,
1178 struct net_bridge_port *port)
1179{
1180 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +00001181
1182 if (!port) {
Yotam Gigi77041422017-10-09 11:15:31 +02001183 if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) {
1184 if (!timer_pending(&br->multicast_router_timer))
1185 br_mc_router_state_change(br, true);
Herbert Xueb1d1642010-02-27 19:41:45 +00001186 mod_timer(&br->multicast_router_timer,
1187 now + br->multicast_querier_interval);
Yotam Gigi77041422017-10-09 11:15:31 +02001188 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001189 return;
1190 }
1191
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001192 if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
1193 port->multicast_router == MDB_RTR_TYPE_PERM)
Herbert Xueb1d1642010-02-27 19:41:45 +00001194 return;
1195
Herbert Xu0909e112010-02-27 19:41:49 +00001196 br_multicast_add_router(br, port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001197
Herbert Xueb1d1642010-02-27 19:41:45 +00001198 mod_timer(&port->multicast_router_timer,
1199 now + br->multicast_querier_interval);
1200}
1201
1202static void br_multicast_query_received(struct net_bridge *br,
1203 struct net_bridge_port *port,
Linus Lüssing90010b32014-06-07 18:26:26 +02001204 struct bridge_mcast_other_query *query,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001205 struct br_ip *saddr,
Linus Lüssingb00589a2013-08-01 01:06:20 +02001206 unsigned long max_delay)
Herbert Xueb1d1642010-02-27 19:41:45 +00001207{
Linus Lüssing2cd41432014-06-07 18:26:29 +02001208 if (!br_multicast_select_querier(br, port, saddr))
Herbert Xueb1d1642010-02-27 19:41:45 +00001209 return;
1210
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001211 br_multicast_update_query_timer(br, query, max_delay);
Hangbin Liu278e2142019-02-22 21:22:32 +08001212 br_multicast_mark_router(br, port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001213}
1214
zhong jiang9c2e9552018-08-06 11:07:23 +08001215static void br_ip4_multicast_query(struct net_bridge *br,
1216 struct net_bridge_port *port,
1217 struct sk_buff *skb,
1218 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001219{
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001220 unsigned int transport_len = ip_transport_len(skb);
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001221 const struct iphdr *iph = ip_hdr(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001222 struct igmphdr *ih = igmp_hdr(skb);
1223 struct net_bridge_mdb_entry *mp;
1224 struct igmpv3_query *ih3;
1225 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +00001226 struct net_bridge_port_group __rcu **pp;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001227 struct br_ip saddr;
Herbert Xueb1d1642010-02-27 19:41:45 +00001228 unsigned long max_delay;
1229 unsigned long now = jiffies;
1230 __be32 group;
1231
1232 spin_lock(&br->multicast_lock);
1233 if (!netif_running(br->dev) ||
1234 (port && port->state == BR_STATE_DISABLED))
1235 goto out;
1236
Herbert Xueb1d1642010-02-27 19:41:45 +00001237 group = ih->group;
1238
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001239 if (transport_len == sizeof(*ih)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001240 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1241
1242 if (!max_delay) {
1243 max_delay = 10 * HZ;
1244 group = 0;
1245 }
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001246 } else if (transport_len >= sizeof(*ih3)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001247 ih3 = igmpv3_query_hdr(skb);
1248 if (ih3->nsrcs)
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001249 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001250
YOSHIFUJI Hideaki / 吉藤英明0ba8c9e2010-03-15 19:27:00 +00001251 max_delay = ih3->code ?
1252 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001253 } else {
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001254 goto out;
1255 }
1256
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001257 if (!group) {
1258 saddr.proto = htons(ETH_P_IP);
1259 saddr.u.ip4 = iph->saddr;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001260
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001261 br_multicast_query_received(br, port, &br->ip4_other_query,
1262 &saddr, max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001263 goto out;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001264 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001265
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02001266 mp = br_mdb_ip4_get(br, group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001267 if (!mp)
1268 goto out;
1269
1270 max_delay *= br->multicast_last_member_count;
1271
Andrew Lunnff0fd342017-11-09 23:10:57 +01001272 if (mp->host_joined &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001273 (timer_pending(&mp->timer) ?
1274 time_after(mp->timer.expires, now + max_delay) :
1275 try_to_del_timer_sync(&mp->timer) >= 0))
1276 mod_timer(&mp->timer, now + max_delay);
1277
Eric Dumazete8051682010-11-15 06:38:10 +00001278 for (pp = &mp->ports;
1279 (p = mlock_dereference(*pp, br)) != NULL;
1280 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001281 if (timer_pending(&p->timer) ?
1282 time_after(p->timer.expires, now + max_delay) :
1283 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001284 mod_timer(&p->timer, now + max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001285 }
1286
1287out:
1288 spin_unlock(&br->multicast_lock);
Herbert Xueb1d1642010-02-27 19:41:45 +00001289}
1290
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001291#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001292static int br_ip6_multicast_query(struct net_bridge *br,
1293 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001294 struct sk_buff *skb,
1295 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001296{
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001297 unsigned int transport_len = ipv6_transport_len(skb);
stephen hemmingereca2a432012-12-13 06:51:28 +00001298 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001299 struct net_bridge_mdb_entry *mp;
1300 struct mld2_query *mld2q;
Eric Dumazete8051682010-11-15 06:38:10 +00001301 struct net_bridge_port_group *p;
1302 struct net_bridge_port_group __rcu **pp;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001303 struct br_ip saddr;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001304 unsigned long max_delay;
1305 unsigned long now = jiffies;
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001306 unsigned int offset = skb_transport_offset(skb);
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001307 const struct in6_addr *group = NULL;
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001308 bool is_general_query;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001309 int err = 0;
1310
1311 spin_lock(&br->multicast_lock);
1312 if (!netif_running(br->dev) ||
1313 (port && port->state == BR_STATE_DISABLED))
1314 goto out;
1315
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001316 if (transport_len == sizeof(*mld)) {
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001317 if (!pskb_may_pull(skb, offset + sizeof(*mld))) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001318 err = -EINVAL;
1319 goto out;
1320 }
1321 mld = (struct mld_msg *) icmp6_hdr(skb);
Li RongQing47152132012-07-09 23:56:12 +00001322 max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001323 if (max_delay)
1324 group = &mld->mld_mca;
Linus Lüssing248ba8e2013-08-06 00:32:05 +02001325 } else {
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001326 if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001327 err = -EINVAL;
1328 goto out;
1329 }
1330 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1331 if (!mld2q->mld2q_nsrcs)
1332 group = &mld2q->mld2q_mca;
Daniel Borkmanne3f5b172013-09-04 00:19:39 +02001333
1334 max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001335 }
1336
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001337 is_general_query = group && ipv6_addr_any(group);
1338
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001339 if (is_general_query) {
1340 saddr.proto = htons(ETH_P_IPV6);
Nikolay Aleksandrov3b26a5d2019-07-02 15:00:19 +03001341 saddr.u.ip6 = ipv6_hdr(skb)->saddr;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001342
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001343 br_multicast_query_received(br, port, &br->ip6_other_query,
1344 &saddr, max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001345 goto out;
Linus Lüssing6c03ee82014-06-12 01:41:23 +02001346 } else if (!group) {
1347 goto out;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001348 }
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001349
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02001350 mp = br_mdb_ip6_get(br, group, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001351 if (!mp)
1352 goto out;
1353
1354 max_delay *= br->multicast_last_member_count;
Andrew Lunnff0fd342017-11-09 23:10:57 +01001355 if (mp->host_joined &&
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001356 (timer_pending(&mp->timer) ?
1357 time_after(mp->timer.expires, now + max_delay) :
1358 try_to_del_timer_sync(&mp->timer) >= 0))
1359 mod_timer(&mp->timer, now + max_delay);
1360
Eric Dumazete8051682010-11-15 06:38:10 +00001361 for (pp = &mp->ports;
1362 (p = mlock_dereference(*pp, br)) != NULL;
1363 pp = &p->next) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001364 if (timer_pending(&p->timer) ?
1365 time_after(p->timer.expires, now + max_delay) :
1366 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001367 mod_timer(&p->timer, now + max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001368 }
1369
1370out:
1371 spin_unlock(&br->multicast_lock);
1372 return err;
1373}
1374#endif
1375
Linus Lüssing90010b32014-06-07 18:26:26 +02001376static void
1377br_multicast_leave_group(struct net_bridge *br,
1378 struct net_bridge_port *port,
1379 struct br_ip *group,
1380 struct bridge_mcast_other_query *other_query,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001381 struct bridge_mcast_own_query *own_query,
1382 const unsigned char *src)
Herbert Xueb1d1642010-02-27 19:41:45 +00001383{
Herbert Xueb1d1642010-02-27 19:41:45 +00001384 struct net_bridge_mdb_entry *mp;
1385 struct net_bridge_port_group *p;
1386 unsigned long now;
1387 unsigned long time;
1388
Herbert Xueb1d1642010-02-27 19:41:45 +00001389 spin_lock(&br->multicast_lock);
1390 if (!netif_running(br->dev) ||
Satish Ashok544586f2015-07-28 03:28:27 -07001391 (port && port->state == BR_STATE_DISABLED))
Herbert Xueb1d1642010-02-27 19:41:45 +00001392 goto out;
1393
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02001394 mp = br_mdb_ip_get(br, group);
Herbert Xueb1d1642010-02-27 19:41:45 +00001395 if (!mp)
1396 goto out;
1397
Satish Ashok544586f2015-07-28 03:28:27 -07001398 if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
1399 struct net_bridge_port_group __rcu **pp;
1400
1401 for (pp = &mp->ports;
1402 (p = mlock_dereference(*pp, br)) != NULL;
1403 pp = &p->next) {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001404 if (!br_port_group_equal(p, port, src))
Satish Ashok544586f2015-07-28 03:28:27 -07001405 continue;
1406
Nikolay Aleksandrov5c725b62019-07-30 14:21:00 +03001407 if (p->flags & MDB_PG_FLAGS_PERMANENT)
1408 break;
1409
Satish Ashok544586f2015-07-28 03:28:27 -07001410 rcu_assign_pointer(*pp, p->next);
1411 hlist_del_init(&p->mglist);
1412 del_timer(&p->timer);
Nikolay Aleksandrov43295962018-12-05 15:14:25 +02001413 kfree_rcu(p, rcu);
Elad Raz45ebcce2016-04-21 12:52:45 +02001414 br_mdb_notify(br->dev, port, group, RTM_DELMDB,
Nikolay Aleksandrov3247b272019-07-30 15:20:41 +03001415 p->flags | MDB_PG_FLAGS_FAST_LEAVE);
Satish Ashok544586f2015-07-28 03:28:27 -07001416
Andrew Lunnff0fd342017-11-09 23:10:57 +01001417 if (!mp->ports && !mp->host_joined &&
Satish Ashok544586f2015-07-28 03:28:27 -07001418 netif_running(br->dev))
1419 mod_timer(&mp->timer, jiffies);
1420 }
1421 goto out;
1422 }
1423
1424 if (timer_pending(&other_query->timer))
1425 goto out;
1426
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03001427 if (br_opt_get(br, BROPT_MULTICAST_QUERIER)) {
Cong Wang6b7df112013-05-21 21:52:56 +00001428 __br_multicast_send_query(br, port, &mp->addr);
1429
1430 time = jiffies + br->multicast_last_member_count *
1431 br->multicast_last_member_interval;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001432
Linus Lüssing90010b32014-06-07 18:26:26 +02001433 mod_timer(&own_query->timer, time);
Cong Wang6b7df112013-05-21 21:52:56 +00001434
1435 for (p = mlock_dereference(mp->ports, br);
1436 p != NULL;
1437 p = mlock_dereference(p->next, br)) {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001438 if (!br_port_group_equal(p, port, src))
Cong Wang6b7df112013-05-21 21:52:56 +00001439 continue;
1440
1441 if (!hlist_unhashed(&p->mglist) &&
1442 (timer_pending(&p->timer) ?
1443 time_after(p->timer.expires, time) :
1444 try_to_del_timer_sync(&p->timer) >= 0)) {
1445 mod_timer(&p->timer, time);
1446 }
1447
1448 break;
1449 }
1450 }
1451
Herbert Xueb1d1642010-02-27 19:41:45 +00001452 now = jiffies;
1453 time = now + br->multicast_last_member_count *
1454 br->multicast_last_member_interval;
1455
1456 if (!port) {
Andrew Lunnff0fd342017-11-09 23:10:57 +01001457 if (mp->host_joined &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001458 (timer_pending(&mp->timer) ?
1459 time_after(mp->timer.expires, time) :
1460 try_to_del_timer_sync(&mp->timer) >= 0)) {
1461 mod_timer(&mp->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +00001462 }
Linus Lüssing454594f2013-10-20 00:58:57 +02001463
1464 goto out;
1465 }
1466
1467 for (p = mlock_dereference(mp->ports, br);
1468 p != NULL;
1469 p = mlock_dereference(p->next, br)) {
1470 if (p->port != port)
1471 continue;
1472
1473 if (!hlist_unhashed(&p->mglist) &&
1474 (timer_pending(&p->timer) ?
1475 time_after(p->timer.expires, time) :
1476 try_to_del_timer_sync(&p->timer) >= 0)) {
1477 mod_timer(&p->timer, time);
1478 }
1479
1480 break;
Herbert Xueb1d1642010-02-27 19:41:45 +00001481 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001482out:
1483 spin_unlock(&br->multicast_lock);
1484}
1485
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001486static void br_ip4_multicast_leave_group(struct net_bridge *br,
1487 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001488 __be32 group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001489 __u16 vid,
1490 const unsigned char *src)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001491{
1492 struct br_ip br_group;
Linus Lüssing90010b32014-06-07 18:26:26 +02001493 struct bridge_mcast_own_query *own_query;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001494
1495 if (ipv4_is_local_multicast(group))
1496 return;
1497
Linus Lüssing90010b32014-06-07 18:26:26 +02001498 own_query = port ? &port->ip4_own_query : &br->ip4_own_query;
1499
Nikolay Aleksandrov1515a63f2019-04-03 23:27:24 +03001500 memset(&br_group, 0, sizeof(br_group));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001501 br_group.u.ip4 = group;
1502 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001503 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001504
Linus Lüssing90010b32014-06-07 18:26:26 +02001505 br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001506 own_query, src);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001507}
1508
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001509#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001510static void br_ip6_multicast_leave_group(struct net_bridge *br,
1511 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001512 const struct in6_addr *group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001513 __u16 vid,
1514 const unsigned char *src)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001515{
1516 struct br_ip br_group;
Linus Lüssing90010b32014-06-07 18:26:26 +02001517 struct bridge_mcast_own_query *own_query;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001518
Linus Lüssing3c3769e2013-09-04 02:13:39 +02001519 if (ipv6_addr_is_ll_all_nodes(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001520 return;
1521
Linus Lüssing90010b32014-06-07 18:26:26 +02001522 own_query = port ? &port->ip6_own_query : &br->ip6_own_query;
1523
Nikolay Aleksandrov1515a63f2019-04-03 23:27:24 +03001524 memset(&br_group, 0, sizeof(br_group));
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001525 br_group.u.ip6 = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001526 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001527 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001528
Linus Lüssing90010b32014-06-07 18:26:26 +02001529 br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001530 own_query, src);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001531}
1532#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001533
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001534static void br_multicast_err_count(const struct net_bridge *br,
1535 const struct net_bridge_port *p,
1536 __be16 proto)
1537{
1538 struct bridge_mcast_stats __percpu *stats;
1539 struct bridge_mcast_stats *pstats;
1540
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03001541 if (!br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001542 return;
1543
1544 if (p)
1545 stats = p->mcast_stats;
1546 else
1547 stats = br->mcast_stats;
1548 if (WARN_ON(!stats))
1549 return;
1550
1551 pstats = this_cpu_ptr(stats);
1552
1553 u64_stats_update_begin(&pstats->syncp);
1554 switch (proto) {
1555 case htons(ETH_P_IP):
1556 pstats->mstats.igmp_parse_errors++;
1557 break;
1558#if IS_ENABLED(CONFIG_IPV6)
1559 case htons(ETH_P_IPV6):
1560 pstats->mstats.mld_parse_errors++;
1561 break;
1562#endif
1563 }
1564 u64_stats_update_end(&pstats->syncp);
1565}
1566
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001567static void br_multicast_pim(struct net_bridge *br,
1568 struct net_bridge_port *port,
1569 const struct sk_buff *skb)
1570{
1571 unsigned int offset = skb_transport_offset(skb);
1572 struct pimhdr *pimhdr, _pimhdr;
1573
1574 pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr);
1575 if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION ||
1576 pim_hdr_type(pimhdr) != PIM_TYPE_HELLO)
1577 return;
1578
1579 br_multicast_mark_router(br, port);
1580}
1581
Linus Lüssing4b3087c2019-01-21 07:26:28 +01001582static int br_ip4_multicast_mrd_rcv(struct net_bridge *br,
1583 struct net_bridge_port *port,
1584 struct sk_buff *skb)
1585{
1586 if (ip_hdr(skb)->protocol != IPPROTO_IGMP ||
1587 igmp_hdr(skb)->type != IGMP_MRDISC_ADV)
1588 return -ENOMSG;
1589
1590 br_multicast_mark_router(br, port);
1591
1592 return 0;
1593}
1594
Herbert Xueb1d1642010-02-27 19:41:45 +00001595static int br_multicast_ipv4_rcv(struct net_bridge *br,
1596 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001597 struct sk_buff *skb,
1598 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001599{
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001600 const unsigned char *src;
Herbert Xueb1d1642010-02-27 19:41:45 +00001601 struct igmphdr *ih;
Herbert Xueb1d1642010-02-27 19:41:45 +00001602 int err;
1603
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001604 err = ip_mc_check_igmp(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001605
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001606 if (err == -ENOMSG) {
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001607 if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) {
Herbert Xubd4265f2011-06-23 02:39:12 +00001608 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001609 } else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) {
1610 if (ip_hdr(skb)->protocol == IPPROTO_PIM)
1611 br_multicast_pim(br, port, skb);
Linus Lüssing4b3087c2019-01-21 07:26:28 +01001612 } else if (ipv4_is_all_snoopers(ip_hdr(skb)->daddr)) {
Li RongQing08e71622019-02-19 10:17:09 +08001613 br_ip4_multicast_mrd_rcv(br, port, skb);
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001614 }
Linus Lüssing4b3087c2019-01-21 07:26:28 +01001615
Herbert Xueb1d1642010-02-27 19:41:45 +00001616 return 0;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001617 } else if (err < 0) {
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001618 br_multicast_err_count(br, port, skb->protocol);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001619 return err;
Herbert Xubd4265f2011-06-23 02:39:12 +00001620 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001621
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001622 ih = igmp_hdr(skb);
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001623 src = eth_hdr(skb)->h_source;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001624 BR_INPUT_SKB_CB(skb)->igmp = ih->type;
Herbert Xueb1d1642010-02-27 19:41:45 +00001625
1626 switch (ih->type) {
1627 case IGMP_HOST_MEMBERSHIP_REPORT:
1628 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Fernando Luis Vázquez Cao62b2bcb2011-06-13 15:04:43 +00001629 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001630 err = br_ip4_multicast_add_group(br, port, ih->group, vid, src);
Herbert Xueb1d1642010-02-27 19:41:45 +00001631 break;
1632 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001633 err = br_ip4_multicast_igmp3_report(br, port, skb, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001634 break;
1635 case IGMP_HOST_MEMBERSHIP_QUERY:
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001636 br_ip4_multicast_query(br, port, skb, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001637 break;
1638 case IGMP_HOST_LEAVE_MESSAGE:
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001639 br_ip4_multicast_leave_group(br, port, ih->group, vid, src);
Herbert Xueb1d1642010-02-27 19:41:45 +00001640 break;
1641 }
1642
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07001643 br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001644 BR_MCAST_DIR_RX);
1645
Herbert Xueb1d1642010-02-27 19:41:45 +00001646 return err;
1647}
1648
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001649#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing4b3087c2019-01-21 07:26:28 +01001650static int br_ip6_multicast_mrd_rcv(struct net_bridge *br,
1651 struct net_bridge_port *port,
1652 struct sk_buff *skb)
1653{
1654 int ret;
1655
1656 if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6)
1657 return -ENOMSG;
1658
1659 ret = ipv6_mc_check_icmpv6(skb);
1660 if (ret < 0)
1661 return ret;
1662
1663 if (icmp6_hdr(skb)->icmp6_type != ICMPV6_MRDISC_ADV)
1664 return -ENOMSG;
1665
1666 br_multicast_mark_router(br, port);
1667
1668 return 0;
1669}
1670
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001671static int br_multicast_ipv6_rcv(struct net_bridge *br,
1672 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001673 struct sk_buff *skb,
1674 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001675{
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001676 const unsigned char *src;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001677 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001678 int err;
1679
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001680 err = ipv6_mc_check_mld(skb);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001681
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001682 if (err == -ENOMSG) {
1683 if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
1684 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Linus Lüssing4b3087c2019-01-21 07:26:28 +01001685
1686 if (ipv6_addr_is_all_snoopers(&ipv6_hdr(skb)->daddr)) {
1687 err = br_ip6_multicast_mrd_rcv(br, port, skb);
1688
1689 if (err < 0 && err != -ENOMSG) {
1690 br_multicast_err_count(br, port, skb->protocol);
1691 return err;
1692 }
1693 }
1694
Linus Lüssing8fad9c32013-09-04 02:13:38 +02001695 return 0;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001696 } else if (err < 0) {
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001697 br_multicast_err_count(br, port, skb->protocol);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001698 return err;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001699 }
1700
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001701 mld = (struct mld_msg *)skb_transport_header(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001702 BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001703
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001704 switch (mld->mld_type) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001705 case ICMPV6_MGM_REPORT:
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001706 src = eth_hdr(skb)->h_source;
Fernando Luis Vázquez Caofc2af6c2011-06-13 15:06:58 +00001707 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001708 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid,
1709 src);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001710 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001711 case ICMPV6_MLD2_REPORT:
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001712 err = br_ip6_multicast_mld2_report(br, port, skb, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001713 break;
1714 case ICMPV6_MGM_QUERY:
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001715 err = br_ip6_multicast_query(br, port, skb, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001716 break;
1717 case ICMPV6_MGM_REDUCTION:
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001718 src = eth_hdr(skb)->h_source;
1719 br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001720 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001721 }
1722
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07001723 br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001724 BR_MCAST_DIR_RX);
1725
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001726 return err;
1727}
1728#endif
1729
Herbert Xueb1d1642010-02-27 19:41:45 +00001730int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001731 struct sk_buff *skb, u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001732{
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001733 int ret = 0;
1734
YOSHIFUJI Hideaki / 吉藤英明1fafc7a2010-04-25 08:06:40 +00001735 BR_INPUT_SKB_CB(skb)->igmp = 0;
1736 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1737
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +03001738 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
Herbert Xueb1d1642010-02-27 19:41:45 +00001739 return 0;
1740
1741 switch (skb->protocol) {
1742 case htons(ETH_P_IP):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001743 ret = br_multicast_ipv4_rcv(br, port, skb, vid);
1744 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001745#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001746 case htons(ETH_P_IPV6):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001747 ret = br_multicast_ipv6_rcv(br, port, skb, vid);
1748 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001749#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001750 }
1751
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001752 return ret;
Herbert Xueb1d1642010-02-27 19:41:45 +00001753}
1754
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001755static void br_multicast_query_expired(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001756 struct bridge_mcast_own_query *query,
1757 struct bridge_mcast_querier *querier)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001758{
1759 spin_lock(&br->multicast_lock);
1760 if (query->startup_sent < br->multicast_startup_query_count)
1761 query->startup_sent++;
1762
Eric Dumazet71d9f612015-05-28 04:42:54 -07001763 RCU_INIT_POINTER(querier->port, NULL);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001764 br_multicast_send_query(br, NULL, query);
1765 spin_unlock(&br->multicast_lock);
1766}
1767
Allen Pais88c1f372017-11-03 11:51:10 +05301768static void br_ip4_multicast_query_expired(struct timer_list *t)
Herbert Xueb1d1642010-02-27 19:41:45 +00001769{
Allen Pais88c1f372017-11-03 11:51:10 +05301770 struct net_bridge *br = from_timer(br, t, ip4_own_query.timer);
Herbert Xueb1d1642010-02-27 19:41:45 +00001771
Linus Lüssing2cd41432014-06-07 18:26:29 +02001772 br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier);
Herbert Xueb1d1642010-02-27 19:41:45 +00001773}
1774
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001775#if IS_ENABLED(CONFIG_IPV6)
Allen Pais88c1f372017-11-03 11:51:10 +05301776static void br_ip6_multicast_query_expired(struct timer_list *t)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001777{
Allen Pais88c1f372017-11-03 11:51:10 +05301778 struct net_bridge *br = from_timer(br, t, ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001779
Linus Lüssing2cd41432014-06-07 18:26:29 +02001780 br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001781}
1782#endif
1783
Herbert Xueb1d1642010-02-27 19:41:45 +00001784void br_multicast_init(struct net_bridge *br)
1785{
Nikolay Aleksandrovd08c6bc2018-12-05 15:14:27 +02001786 br->hash_max = BR_MULTICAST_DEFAULT_HASH_MAX;
Herbert Xueb1d1642010-02-27 19:41:45 +00001787
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001788 br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Herbert Xueb1d1642010-02-27 19:41:45 +00001789 br->multicast_last_member_count = 2;
1790 br->multicast_startup_query_count = 2;
1791
1792 br->multicast_last_member_interval = HZ;
1793 br->multicast_query_response_interval = 10 * HZ;
1794 br->multicast_startup_query_interval = 125 * HZ / 4;
1795 br->multicast_query_interval = 125 * HZ;
1796 br->multicast_querier_interval = 255 * HZ;
1797 br->multicast_membership_interval = 260 * HZ;
1798
Linus Lüssing90010b32014-06-07 18:26:26 +02001799 br->ip4_other_query.delay_time = 0;
Linus Lüssing2cd41432014-06-07 18:26:29 +02001800 br->ip4_querier.port = NULL;
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001801 br->multicast_igmp_version = 2;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001802#if IS_ENABLED(CONFIG_IPV6)
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001803 br->multicast_mld_version = 1;
Linus Lüssing90010b32014-06-07 18:26:26 +02001804 br->ip6_other_query.delay_time = 0;
Linus Lüssing2cd41432014-06-07 18:26:29 +02001805 br->ip6_querier.port = NULL;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001806#endif
Ido Schimmel6919622a2018-10-01 11:57:01 +03001807 br_opt_toggle(br, BROPT_MULTICAST_ENABLED, true);
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03001808 br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true);
Linus Lüssingb00589a2013-08-01 01:06:20 +02001809
Herbert Xueb1d1642010-02-27 19:41:45 +00001810 spin_lock_init(&br->multicast_lock);
Allen Pais88c1f372017-11-03 11:51:10 +05301811 timer_setup(&br->multicast_router_timer,
1812 br_multicast_local_router_expired, 0);
1813 timer_setup(&br->ip4_other_query.timer,
1814 br_ip4_multicast_querier_expired, 0);
1815 timer_setup(&br->ip4_own_query.timer,
1816 br_ip4_multicast_query_expired, 0);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001817#if IS_ENABLED(CONFIG_IPV6)
Allen Pais88c1f372017-11-03 11:51:10 +05301818 timer_setup(&br->ip6_other_query.timer,
1819 br_ip6_multicast_querier_expired, 0);
1820 timer_setup(&br->ip6_own_query.timer,
1821 br_ip6_multicast_query_expired, 0);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001822#endif
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02001823 INIT_HLIST_HEAD(&br->mdb_list);
Herbert Xueb1d1642010-02-27 19:41:45 +00001824}
1825
Linus Lüssing4effd282019-01-21 07:26:27 +01001826static void br_ip4_multicast_join_snoopers(struct net_bridge *br)
1827{
1828 struct in_device *in_dev = in_dev_get(br->dev);
1829
1830 if (!in_dev)
1831 return;
1832
Florian Fainelli9fb20802019-02-01 20:20:52 -08001833 __ip_mc_inc_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
Linus Lüssing4effd282019-01-21 07:26:27 +01001834 in_dev_put(in_dev);
1835}
1836
1837#if IS_ENABLED(CONFIG_IPV6)
1838static void br_ip6_multicast_join_snoopers(struct net_bridge *br)
1839{
1840 struct in6_addr addr;
1841
1842 ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
1843 ipv6_dev_mc_inc(br->dev, &addr);
1844}
1845#else
1846static inline void br_ip6_multicast_join_snoopers(struct net_bridge *br)
1847{
1848}
1849#endif
1850
1851static void br_multicast_join_snoopers(struct net_bridge *br)
1852{
1853 br_ip4_multicast_join_snoopers(br);
1854 br_ip6_multicast_join_snoopers(br);
1855}
1856
1857static void br_ip4_multicast_leave_snoopers(struct net_bridge *br)
1858{
1859 struct in_device *in_dev = in_dev_get(br->dev);
1860
1861 if (WARN_ON(!in_dev))
1862 return;
1863
Florian Fainelli9fb20802019-02-01 20:20:52 -08001864 __ip_mc_dec_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
Linus Lüssing4effd282019-01-21 07:26:27 +01001865 in_dev_put(in_dev);
1866}
1867
1868#if IS_ENABLED(CONFIG_IPV6)
1869static void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
1870{
1871 struct in6_addr addr;
1872
1873 ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
1874 ipv6_dev_mc_dec(br->dev, &addr);
1875}
1876#else
1877static inline void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
1878{
1879}
1880#endif
1881
1882static void br_multicast_leave_snoopers(struct net_bridge *br)
1883{
1884 br_ip4_multicast_leave_snoopers(br);
1885 br_ip6_multicast_leave_snoopers(br);
1886}
1887
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001888static void __br_multicast_open(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +02001889 struct bridge_mcast_own_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +00001890{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001891 query->startup_sent = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001892
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +03001893 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
Herbert Xueb1d1642010-02-27 19:41:45 +00001894 return;
1895
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001896 mod_timer(&query->timer, jiffies);
1897}
1898
1899void br_multicast_open(struct net_bridge *br)
1900{
Linus Lüssing4effd282019-01-21 07:26:27 +01001901 if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
1902 br_multicast_join_snoopers(br);
1903
Linus Lüssing90010b32014-06-07 18:26:26 +02001904 __br_multicast_open(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001905#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001906 __br_multicast_open(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001907#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001908}
1909
1910void br_multicast_stop(struct net_bridge *br)
1911{
Herbert Xueb1d1642010-02-27 19:41:45 +00001912 del_timer_sync(&br->multicast_router_timer);
Linus Lüssing90010b32014-06-07 18:26:26 +02001913 del_timer_sync(&br->ip4_other_query.timer);
1914 del_timer_sync(&br->ip4_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001915#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001916 del_timer_sync(&br->ip6_other_query.timer);
1917 del_timer_sync(&br->ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001918#endif
Linus Lüssing4effd282019-01-21 07:26:27 +01001919
1920 if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
1921 br_multicast_leave_snoopers(br);
Satish Ashoke10177a2015-07-15 07:16:51 -07001922}
1923
1924void br_multicast_dev_del(struct net_bridge *br)
1925{
Satish Ashoke10177a2015-07-15 07:16:51 -07001926 struct net_bridge_mdb_entry *mp;
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02001927 struct hlist_node *tmp;
Herbert Xueb1d1642010-02-27 19:41:45 +00001928
1929 spin_lock_bh(&br->multicast_lock);
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02001930 hlist_for_each_entry_safe(mp, tmp, &br->mdb_list, mdb_node) {
1931 del_timer(&mp->timer);
1932 rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode,
1933 br_mdb_rht_params);
1934 hlist_del_rcu(&mp->mdb_node);
Nikolay Aleksandrov43295962018-12-05 15:14:25 +02001935 kfree_rcu(mp, rcu);
Herbert Xueb1d1642010-02-27 19:41:45 +00001936 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001937 spin_unlock_bh(&br->multicast_lock);
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02001938
Nikolay Aleksandrov43295962018-12-05 15:14:25 +02001939 rcu_barrier();
Herbert Xueb1d1642010-02-27 19:41:45 +00001940}
Herbert Xu0909e112010-02-27 19:41:49 +00001941
1942int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1943{
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02001944 int err = -EINVAL;
Herbert Xu0909e112010-02-27 19:41:49 +00001945
1946 spin_lock_bh(&br->multicast_lock);
Herbert Xu0909e112010-02-27 19:41:49 +00001947
1948 switch (val) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001949 case MDB_RTR_TYPE_DISABLED:
1950 case MDB_RTR_TYPE_PERM:
Yotam Gigi77041422017-10-09 11:15:31 +02001951 br_mc_router_state_change(br, val == MDB_RTR_TYPE_PERM);
Herbert Xu0909e112010-02-27 19:41:49 +00001952 del_timer(&br->multicast_router_timer);
Yotam Gigi77041422017-10-09 11:15:31 +02001953 br->multicast_router = val;
1954 err = 0;
1955 break;
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001956 case MDB_RTR_TYPE_TEMP_QUERY:
Yotam Gigi77041422017-10-09 11:15:31 +02001957 if (br->multicast_router != MDB_RTR_TYPE_TEMP_QUERY)
1958 br_mc_router_state_change(br, false);
Herbert Xu0909e112010-02-27 19:41:49 +00001959 br->multicast_router = val;
1960 err = 0;
1961 break;
Herbert Xu0909e112010-02-27 19:41:49 +00001962 }
1963
Herbert Xu0909e112010-02-27 19:41:49 +00001964 spin_unlock_bh(&br->multicast_lock);
1965
1966 return err;
1967}
1968
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001969static void __del_port_router(struct net_bridge_port *p)
1970{
1971 if (hlist_unhashed(&p->rlist))
1972 return;
1973 hlist_del_init_rcu(&p->rlist);
1974 br_rtr_notify(p->br->dev, p, RTM_DELMDB);
Nogah Frankel6d549642017-02-09 14:54:42 +01001975 br_port_mc_router_state_change(p, false);
Nogah Frankelf12e7d92017-02-09 14:54:41 +01001976
1977 /* don't allow timer refresh */
1978 if (p->multicast_router == MDB_RTR_TYPE_TEMP)
1979 p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001980}
1981
Herbert Xu0909e112010-02-27 19:41:49 +00001982int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1983{
1984 struct net_bridge *br = p->br;
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001985 unsigned long now = jiffies;
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02001986 int err = -EINVAL;
Herbert Xu0909e112010-02-27 19:41:49 +00001987
1988 spin_lock(&br->multicast_lock);
Nikolay Aleksandrov4950cfd2016-02-26 21:20:02 +01001989 if (p->multicast_router == val) {
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001990 /* Refresh the temp router port timer */
1991 if (p->multicast_router == MDB_RTR_TYPE_TEMP)
1992 mod_timer(&p->multicast_router_timer,
1993 now + br->multicast_querier_interval);
Nikolay Aleksandrov4950cfd2016-02-26 21:20:02 +01001994 err = 0;
1995 goto unlock;
1996 }
Herbert Xu0909e112010-02-27 19:41:49 +00001997 switch (val) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001998 case MDB_RTR_TYPE_DISABLED:
1999 p->multicast_router = MDB_RTR_TYPE_DISABLED;
2000 __del_port_router(p);
Herbert Xu0909e112010-02-27 19:41:49 +00002001 del_timer(&p->multicast_router_timer);
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002002 break;
2003 case MDB_RTR_TYPE_TEMP_QUERY:
2004 p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
2005 __del_port_router(p);
2006 break;
2007 case MDB_RTR_TYPE_PERM:
2008 p->multicast_router = MDB_RTR_TYPE_PERM;
2009 del_timer(&p->multicast_router_timer);
Herbert Xu0909e112010-02-27 19:41:49 +00002010 br_multicast_add_router(br, p);
2011 break;
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01002012 case MDB_RTR_TYPE_TEMP:
2013 p->multicast_router = MDB_RTR_TYPE_TEMP;
2014 br_multicast_mark_router(br, p);
2015 break;
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002016 default:
2017 goto unlock;
Herbert Xu0909e112010-02-27 19:41:49 +00002018 }
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002019 err = 0;
2020unlock:
Herbert Xu0909e112010-02-27 19:41:49 +00002021 spin_unlock(&br->multicast_lock);
2022
2023 return err;
2024}
Herbert Xu561f1102010-02-27 19:41:50 +00002025
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002026static void br_multicast_start_querier(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +02002027 struct bridge_mcast_own_query *query)
Herbert Xu561f1102010-02-27 19:41:50 +00002028{
2029 struct net_bridge_port *port;
Herbert Xu74857212012-04-13 02:37:42 +00002030
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002031 __br_multicast_open(br, query);
Herbert Xu74857212012-04-13 02:37:42 +00002032
Nikolay Aleksandrovc5b493c2019-04-11 15:08:25 +03002033 rcu_read_lock();
2034 list_for_each_entry_rcu(port, &br->port_list, list) {
Herbert Xu74857212012-04-13 02:37:42 +00002035 if (port->state == BR_STATE_DISABLED ||
2036 port->state == BR_STATE_BLOCKING)
2037 continue;
2038
Linus Lüssing90010b32014-06-07 18:26:26 +02002039 if (query == &br->ip4_own_query)
2040 br_multicast_enable(&port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002041#if IS_ENABLED(CONFIG_IPV6)
2042 else
Linus Lüssing90010b32014-06-07 18:26:26 +02002043 br_multicast_enable(&port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002044#endif
Herbert Xu74857212012-04-13 02:37:42 +00002045 }
Nikolay Aleksandrovc5b493c2019-04-11 15:08:25 +03002046 rcu_read_unlock();
Herbert Xu74857212012-04-13 02:37:42 +00002047}
2048
2049int br_multicast_toggle(struct net_bridge *br, unsigned long val)
2050{
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02002051 struct net_bridge_port *port;
Herbert Xu561f1102010-02-27 19:41:50 +00002052
Andrey Vaginef5e0d82011-11-10 05:48:03 +00002053 spin_lock_bh(&br->multicast_lock);
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +03002054 if (!!br_opt_get(br, BROPT_MULTICAST_ENABLED) == !!val)
Herbert Xu561f1102010-02-27 19:41:50 +00002055 goto unlock;
2056
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +03002057 br_mc_disabled_update(br->dev, val);
2058 br_opt_toggle(br, BROPT_MULTICAST_ENABLED, !!val);
Linus Lüssing4effd282019-01-21 07:26:27 +01002059 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) {
2060 br_multicast_leave_snoopers(br);
Herbert Xu561f1102010-02-27 19:41:50 +00002061 goto unlock;
Linus Lüssing4effd282019-01-21 07:26:27 +01002062 }
Herbert Xu561f1102010-02-27 19:41:50 +00002063
Herbert Xu3a7fda02010-07-29 00:45:30 +00002064 if (!netif_running(br->dev))
2065 goto unlock;
2066
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02002067 br_multicast_open(br);
2068 list_for_each_entry(port, &br->port_list, list)
2069 __br_multicast_enable_port(port);
Herbert Xu561f1102010-02-27 19:41:50 +00002070
2071unlock:
Andrey Vaginef5e0d82011-11-10 05:48:03 +00002072 spin_unlock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00002073
YueHaibinga26d94b2018-12-17 17:46:23 +08002074 return 0;
Herbert Xu561f1102010-02-27 19:41:50 +00002075}
Herbert Xub1951672010-02-27 19:41:51 +00002076
Ido Schimmel9341b982017-05-26 08:37:24 +02002077bool br_multicast_enabled(const struct net_device *dev)
2078{
2079 struct net_bridge *br = netdev_priv(dev);
2080
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +03002081 return !!br_opt_get(br, BROPT_MULTICAST_ENABLED);
Ido Schimmel9341b982017-05-26 08:37:24 +02002082}
2083EXPORT_SYMBOL_GPL(br_multicast_enabled);
2084
Yotam Gigi0912bda2017-10-09 11:15:32 +02002085bool br_multicast_router(const struct net_device *dev)
2086{
2087 struct net_bridge *br = netdev_priv(dev);
2088 bool is_router;
2089
2090 spin_lock_bh(&br->multicast_lock);
2091 is_router = br_multicast_is_router(br);
2092 spin_unlock_bh(&br->multicast_lock);
2093 return is_router;
2094}
2095EXPORT_SYMBOL_GPL(br_multicast_router);
2096
Herbert Xuc5c23262012-04-13 02:37:42 +00002097int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
2098{
Linus Lüssingb00589a2013-08-01 01:06:20 +02002099 unsigned long max_delay;
2100
Herbert Xuc5c23262012-04-13 02:37:42 +00002101 val = !!val;
2102
2103 spin_lock_bh(&br->multicast_lock);
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03002104 if (br_opt_get(br, BROPT_MULTICAST_QUERIER) == val)
Herbert Xuc5c23262012-04-13 02:37:42 +00002105 goto unlock;
2106
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03002107 br_opt_toggle(br, BROPT_MULTICAST_QUERIER, !!val);
Linus Lüssingb00589a2013-08-01 01:06:20 +02002108 if (!val)
2109 goto unlock;
2110
2111 max_delay = br->multicast_query_response_interval;
Linus Lüssingb00589a2013-08-01 01:06:20 +02002112
Linus Lüssing90010b32014-06-07 18:26:26 +02002113 if (!timer_pending(&br->ip4_other_query.timer))
2114 br->ip4_other_query.delay_time = jiffies + max_delay;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002115
Linus Lüssing90010b32014-06-07 18:26:26 +02002116 br_multicast_start_querier(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002117
2118#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02002119 if (!timer_pending(&br->ip6_other_query.timer))
2120 br->ip6_other_query.delay_time = jiffies + max_delay;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002121
Linus Lüssing90010b32014-06-07 18:26:26 +02002122 br_multicast_start_querier(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002123#endif
Herbert Xuc5c23262012-04-13 02:37:42 +00002124
2125unlock:
2126 spin_unlock_bh(&br->multicast_lock);
2127
2128 return 0;
2129}
2130
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01002131int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val)
2132{
2133 /* Currently we support only version 2 and 3 */
2134 switch (val) {
2135 case 2:
2136 case 3:
2137 break;
2138 default:
2139 return -EINVAL;
2140 }
2141
2142 spin_lock_bh(&br->multicast_lock);
2143 br->multicast_igmp_version = val;
2144 spin_unlock_bh(&br->multicast_lock);
2145
2146 return 0;
2147}
2148
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01002149#if IS_ENABLED(CONFIG_IPV6)
2150int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val)
2151{
2152 /* Currently we support version 1 and 2 */
2153 switch (val) {
2154 case 1:
2155 case 2:
2156 break;
2157 default:
2158 return -EINVAL;
2159 }
2160
2161 spin_lock_bh(&br->multicast_lock);
2162 br->multicast_mld_version = val;
2163 spin_unlock_bh(&br->multicast_lock);
2164
2165 return 0;
2166}
2167#endif
2168
Linus Lüssing07f8ac42014-06-07 18:26:28 +02002169/**
2170 * br_multicast_list_adjacent - Returns snooped multicast addresses
2171 * @dev: The bridge port adjacent to which to retrieve addresses
2172 * @br_ip_list: The list to store found, snooped multicast IP addresses in
2173 *
2174 * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
2175 * snooping feature on all bridge ports of dev's bridge device, excluding
2176 * the addresses from dev itself.
2177 *
2178 * Returns the number of items added to br_ip_list.
2179 *
2180 * Notes:
2181 * - br_ip_list needs to be initialized by caller
2182 * - br_ip_list might contain duplicates in the end
2183 * (needs to be taken care of by caller)
2184 * - br_ip_list needs to be freed by caller
2185 */
2186int br_multicast_list_adjacent(struct net_device *dev,
2187 struct list_head *br_ip_list)
2188{
2189 struct net_bridge *br;
2190 struct net_bridge_port *port;
2191 struct net_bridge_port_group *group;
2192 struct br_ip_list *entry;
2193 int count = 0;
2194
2195 rcu_read_lock();
Julian Wiedmann35f861e2019-03-29 14:38:19 +01002196 if (!br_ip_list || !netif_is_bridge_port(dev))
Linus Lüssing07f8ac42014-06-07 18:26:28 +02002197 goto unlock;
2198
2199 port = br_port_get_rcu(dev);
2200 if (!port || !port->br)
2201 goto unlock;
2202
2203 br = port->br;
2204
2205 list_for_each_entry_rcu(port, &br->port_list, list) {
2206 if (!port->dev || port->dev == dev)
2207 continue;
2208
2209 hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
2210 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
2211 if (!entry)
2212 goto unlock;
2213
2214 entry->addr = group->addr;
2215 list_add(&entry->list, br_ip_list);
2216 count++;
2217 }
2218 }
2219
2220unlock:
2221 rcu_read_unlock();
2222 return count;
2223}
2224EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
Linus Lüssing2cd41432014-06-07 18:26:29 +02002225
2226/**
Linus Lüssingc34963e2014-07-07 05:41:17 +02002227 * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2228 * @dev: The bridge port providing the bridge on which to check for a querier
2229 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2230 *
2231 * Checks whether the given interface has a bridge on top and if so returns
2232 * true if a valid querier exists anywhere on the bridged link layer.
2233 * Otherwise returns false.
2234 */
2235bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
2236{
2237 struct net_bridge *br;
2238 struct net_bridge_port *port;
2239 struct ethhdr eth;
2240 bool ret = false;
2241
2242 rcu_read_lock();
Julian Wiedmann35f861e2019-03-29 14:38:19 +01002243 if (!netif_is_bridge_port(dev))
Linus Lüssingc34963e2014-07-07 05:41:17 +02002244 goto unlock;
2245
2246 port = br_port_get_rcu(dev);
2247 if (!port || !port->br)
2248 goto unlock;
2249
2250 br = port->br;
2251
2252 memset(&eth, 0, sizeof(eth));
2253 eth.h_proto = htons(proto);
2254
2255 ret = br_multicast_querier_exists(br, &eth);
2256
2257unlock:
2258 rcu_read_unlock();
2259 return ret;
2260}
2261EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
2262
2263/**
Linus Lüssing2cd41432014-06-07 18:26:29 +02002264 * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
2265 * @dev: The bridge port adjacent to which to check for a querier
2266 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2267 *
2268 * Checks whether the given interface has a bridge on top and if so returns
2269 * true if a selected querier is behind one of the other ports of this
2270 * bridge. Otherwise returns false.
2271 */
2272bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
2273{
2274 struct net_bridge *br;
2275 struct net_bridge_port *port;
2276 bool ret = false;
2277
2278 rcu_read_lock();
Julian Wiedmann35f861e2019-03-29 14:38:19 +01002279 if (!netif_is_bridge_port(dev))
Linus Lüssing2cd41432014-06-07 18:26:29 +02002280 goto unlock;
2281
2282 port = br_port_get_rcu(dev);
2283 if (!port || !port->br)
2284 goto unlock;
2285
2286 br = port->br;
2287
2288 switch (proto) {
2289 case ETH_P_IP:
2290 if (!timer_pending(&br->ip4_other_query.timer) ||
2291 rcu_dereference(br->ip4_querier.port) == port)
2292 goto unlock;
2293 break;
Linus Lüssing3993c4e2014-06-12 01:41:24 +02002294#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing2cd41432014-06-07 18:26:29 +02002295 case ETH_P_IPV6:
2296 if (!timer_pending(&br->ip6_other_query.timer) ||
2297 rcu_dereference(br->ip6_querier.port) == port)
2298 goto unlock;
2299 break;
Linus Lüssing3993c4e2014-06-12 01:41:24 +02002300#endif
Linus Lüssing2cd41432014-06-07 18:26:29 +02002301 default:
2302 goto unlock;
2303 }
2304
2305 ret = true;
2306unlock:
2307 rcu_read_unlock();
2308 return ret;
2309}
2310EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002311
2312static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002313 const struct sk_buff *skb, u8 type, u8 dir)
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002314{
2315 struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002316 __be16 proto = skb->protocol;
2317 unsigned int t_len;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002318
2319 u64_stats_update_begin(&pstats->syncp);
2320 switch (proto) {
2321 case htons(ETH_P_IP):
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002322 t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002323 switch (type) {
2324 case IGMP_HOST_MEMBERSHIP_REPORT:
2325 pstats->mstats.igmp_v1reports[dir]++;
2326 break;
2327 case IGMPV2_HOST_MEMBERSHIP_REPORT:
2328 pstats->mstats.igmp_v2reports[dir]++;
2329 break;
2330 case IGMPV3_HOST_MEMBERSHIP_REPORT:
2331 pstats->mstats.igmp_v3reports[dir]++;
2332 break;
2333 case IGMP_HOST_MEMBERSHIP_QUERY:
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002334 if (t_len != sizeof(struct igmphdr)) {
2335 pstats->mstats.igmp_v3queries[dir]++;
2336 } else {
2337 unsigned int offset = skb_transport_offset(skb);
2338 struct igmphdr *ih, _ihdr;
2339
2340 ih = skb_header_pointer(skb, offset,
2341 sizeof(_ihdr), &_ihdr);
2342 if (!ih)
2343 break;
2344 if (!ih->code)
2345 pstats->mstats.igmp_v1queries[dir]++;
2346 else
2347 pstats->mstats.igmp_v2queries[dir]++;
2348 }
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002349 break;
2350 case IGMP_HOST_LEAVE_MESSAGE:
2351 pstats->mstats.igmp_leaves[dir]++;
2352 break;
2353 }
2354 break;
2355#if IS_ENABLED(CONFIG_IPV6)
2356 case htons(ETH_P_IPV6):
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002357 t_len = ntohs(ipv6_hdr(skb)->payload_len) +
2358 sizeof(struct ipv6hdr);
2359 t_len -= skb_network_header_len(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002360 switch (type) {
2361 case ICMPV6_MGM_REPORT:
2362 pstats->mstats.mld_v1reports[dir]++;
2363 break;
2364 case ICMPV6_MLD2_REPORT:
2365 pstats->mstats.mld_v2reports[dir]++;
2366 break;
2367 case ICMPV6_MGM_QUERY:
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002368 if (t_len != sizeof(struct mld_msg))
2369 pstats->mstats.mld_v2queries[dir]++;
2370 else
2371 pstats->mstats.mld_v1queries[dir]++;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002372 break;
2373 case ICMPV6_MGM_REDUCTION:
2374 pstats->mstats.mld_leaves[dir]++;
2375 break;
2376 }
2377 break;
2378#endif /* CONFIG_IPV6 */
2379 }
2380 u64_stats_update_end(&pstats->syncp);
2381}
2382
2383void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002384 const struct sk_buff *skb, u8 type, u8 dir)
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002385{
2386 struct bridge_mcast_stats __percpu *stats;
2387
2388 /* if multicast_disabled is true then igmp type can't be set */
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03002389 if (!type || !br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002390 return;
2391
2392 if (p)
2393 stats = p->mcast_stats;
2394 else
2395 stats = br->mcast_stats;
2396 if (WARN_ON(!stats))
2397 return;
2398
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002399 br_mcast_stats_add(stats, skb, type, dir);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002400}
2401
2402int br_multicast_init_stats(struct net_bridge *br)
2403{
2404 br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
2405 if (!br->mcast_stats)
2406 return -ENOMEM;
2407
2408 return 0;
2409}
2410
Ido Schimmelb6fe0442017-04-10 14:59:27 +03002411void br_multicast_uninit_stats(struct net_bridge *br)
2412{
2413 free_percpu(br->mcast_stats);
2414}
2415
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002416static void mcast_stats_add_dir(u64 *dst, u64 *src)
2417{
2418 dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
2419 dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
2420}
2421
2422void br_multicast_get_stats(const struct net_bridge *br,
2423 const struct net_bridge_port *p,
2424 struct br_mcast_stats *dest)
2425{
2426 struct bridge_mcast_stats __percpu *stats;
2427 struct br_mcast_stats tdst;
2428 int i;
2429
2430 memset(dest, 0, sizeof(*dest));
2431 if (p)
2432 stats = p->mcast_stats;
2433 else
2434 stats = br->mcast_stats;
2435 if (WARN_ON(!stats))
2436 return;
2437
2438 memset(&tdst, 0, sizeof(tdst));
2439 for_each_possible_cpu(i) {
2440 struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
2441 struct br_mcast_stats temp;
2442 unsigned int start;
2443
2444 do {
2445 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
2446 memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
2447 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
2448
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002449 mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
2450 mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
2451 mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002452 mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
2453 mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
2454 mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
2455 mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
2456 tdst.igmp_parse_errors += temp.igmp_parse_errors;
2457
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002458 mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
2459 mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002460 mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
2461 mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
2462 mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
2463 tdst.mld_parse_errors += temp.mld_parse_errors;
2464 }
2465 memcpy(dest, &tdst, sizeof(*dest));
2466}
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02002467
2468int br_mdb_hash_init(struct net_bridge *br)
2469{
2470 return rhashtable_init(&br->mdb_hash_tbl, &br_mdb_rht_params);
2471}
2472
2473void br_mdb_hash_fini(struct net_bridge *br)
2474{
2475 rhashtable_destroy(&br->mdb_hash_tbl);
2476}