blob: 02da21d771c9cc7d93c442f0310ad2ab5d9b66f6 [file] [log] [blame]
Herbert Xueb1d1642010-02-27 19:41:45 +00001/*
2 * Bridge multicast support.
3 *
4 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 */
12
13#include <linux/err.h>
Linus Lüssing07f8ac42014-06-07 18:26:28 +020014#include <linux/export.h>
Herbert Xueb1d1642010-02-27 19:41:45 +000015#include <linux/if_ether.h>
16#include <linux/igmp.h>
Linus Lüssing4b3087c2019-01-21 07:26:28 +010017#include <linux/in.h>
Herbert Xueb1d1642010-02-27 19:41:45 +000018#include <linux/jhash.h>
19#include <linux/kernel.h>
Herbert Xub1951672010-02-27 19:41:51 +000020#include <linux/log2.h>
Herbert Xueb1d1642010-02-27 19:41:45 +000021#include <linux/netdevice.h>
22#include <linux/netfilter_bridge.h>
23#include <linux/random.h>
24#include <linux/rculist.h>
25#include <linux/skbuff.h>
26#include <linux/slab.h>
27#include <linux/timer.h>
Cong Wang1c8ad5b2013-05-21 21:52:54 +000028#include <linux/inetdevice.h>
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +010029#include <linux/mroute.h>
Herbert Xueb1d1642010-02-27 19:41:45 +000030#include <net/ip.h>
Nogah Frankel147c1e92017-02-09 14:54:40 +010031#include <net/switchdev.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000032#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing4b3087c2019-01-21 07:26:28 +010033#include <linux/icmpv6.h>
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090034#include <net/ipv6.h>
35#include <net/mld.h>
David S. Millerd4c4f072010-04-27 10:16:54 -070036#include <net/ip6_checksum.h>
Linus Lüssing3c3769e2013-09-04 02:13:39 +020037#include <net/addrconf.h>
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090038#endif
Herbert Xueb1d1642010-02-27 19:41:45 +000039
40#include "br_private.h"
41
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +020042static const struct rhashtable_params br_mdb_rht_params = {
43 .head_offset = offsetof(struct net_bridge_mdb_entry, rhnode),
44 .key_offset = offsetof(struct net_bridge_mdb_entry, addr),
45 .key_len = sizeof(struct br_ip),
46 .automatic_shrinking = true,
47 .locks_mul = 1,
48};
49
Linus Lüssingcc0fdd82013-08-30 17:28:17 +020050static void br_multicast_start_querier(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +020051 struct bridge_mcast_own_query *query);
Satish Ashok754bc542015-06-19 01:22:57 -070052static void br_multicast_add_router(struct net_bridge *br,
53 struct net_bridge_port *port);
Satish Ashokbc8c20a2015-07-13 05:28:37 -070054static void br_ip4_multicast_leave_group(struct net_bridge *br,
55 struct net_bridge_port *port,
56 __be32 group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +010057 __u16 vid,
58 const unsigned char *src);
59
Nogah Frankelf12e7d92017-02-09 14:54:41 +010060static void __del_port_router(struct net_bridge_port *p);
Satish Ashokbc8c20a2015-07-13 05:28:37 -070061#if IS_ENABLED(CONFIG_IPV6)
62static void br_ip6_multicast_leave_group(struct net_bridge *br,
63 struct net_bridge_port *port,
64 const struct in6_addr *group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +010065 __u16 vid, const unsigned char *src);
Satish Ashokbc8c20a2015-07-13 05:28:37 -070066#endif
Herbert Xuc83b8fa2012-04-13 02:37:42 +000067
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090068static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
69{
70 if (a->proto != b->proto)
71 return 0;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000072 if (a->vid != b->vid)
73 return 0;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090074 switch (a->proto) {
75 case htons(ETH_P_IP):
76 return a->u.ip4 == b->u.ip4;
Eric Dumazetdfd56b82011-12-10 09:48:31 +000077#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090078 case htons(ETH_P_IPV6):
79 return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
80#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090081 }
82 return 0;
83}
84
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +020085static struct net_bridge_mdb_entry *br_mdb_ip_get_rcu(struct net_bridge *br,
86 struct br_ip *dst)
Herbert Xueb1d1642010-02-27 19:41:45 +000087{
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +020088 return rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params);
Herbert Xueb1d1642010-02-27 19:41:45 +000089}
90
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +020091struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge *br,
Cong Wangcfd56752012-12-11 22:23:08 +000092 struct br_ip *dst)
Herbert Xu7f285fa2010-07-05 14:50:08 +000093{
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +020094 struct net_bridge_mdb_entry *ent;
Herbert Xu7f285fa2010-07-05 14:50:08 +000095
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +020096 lockdep_assert_held_once(&br->multicast_lock);
97
98 rcu_read_lock();
99 ent = rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params);
100 rcu_read_unlock();
101
102 return ent;
Herbert Xu7f285fa2010-07-05 14:50:08 +0000103}
104
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200105static struct net_bridge_mdb_entry *br_mdb_ip4_get(struct net_bridge *br,
106 __be32 dst, __u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000107{
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900108 struct br_ip br_dst;
Herbert Xu0821ec52010-03-15 20:38:25 -0700109
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200110 memset(&br_dst, 0, sizeof(br_dst));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900111 br_dst.u.ip4 = dst;
112 br_dst.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000113 br_dst.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900114
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200115 return br_mdb_ip_get(br, &br_dst);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900116}
117
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000118#if IS_ENABLED(CONFIG_IPV6)
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200119static struct net_bridge_mdb_entry *br_mdb_ip6_get(struct net_bridge *br,
120 const struct in6_addr *dst,
121 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900122{
123 struct br_ip br_dst;
124
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200125 memset(&br_dst, 0, sizeof(br_dst));
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000126 br_dst.u.ip6 = *dst;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900127 br_dst.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000128 br_dst.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900129
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200130 return br_mdb_ip_get(br, &br_dst);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900131}
132#endif
133
Herbert Xueb1d1642010-02-27 19:41:45 +0000134struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
Cong Wangfbca58a2013-03-07 03:05:33 +0000135 struct sk_buff *skb, u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000136{
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900137 struct br_ip ip;
138
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +0300139 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
Herbert Xueb1d1642010-02-27 19:41:45 +0000140 return NULL;
141
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900142 if (BR_INPUT_SKB_CB(skb)->igmp)
143 return NULL;
144
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200145 memset(&ip, 0, sizeof(ip));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900146 ip.proto = skb->protocol;
Cong Wangfbca58a2013-03-07 03:05:33 +0000147 ip.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900148
Herbert Xueb1d1642010-02-27 19:41:45 +0000149 switch (skb->protocol) {
150 case htons(ETH_P_IP):
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900151 ip.u.ip4 = ip_hdr(skb)->daddr;
152 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000153#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900154 case htons(ETH_P_IPV6):
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000155 ip.u.ip6 = ipv6_hdr(skb)->daddr;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900156 break;
157#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900158 default:
159 return NULL;
Herbert Xueb1d1642010-02-27 19:41:45 +0000160 }
161
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200162 return br_mdb_ip_get_rcu(br, &ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000163}
164
Allen Pais88c1f372017-11-03 11:51:10 +0530165static void br_multicast_group_expired(struct timer_list *t)
Herbert Xueb1d1642010-02-27 19:41:45 +0000166{
Allen Pais88c1f372017-11-03 11:51:10 +0530167 struct net_bridge_mdb_entry *mp = from_timer(mp, t, timer);
Herbert Xueb1d1642010-02-27 19:41:45 +0000168 struct net_bridge *br = mp->br;
Herbert Xueb1d1642010-02-27 19:41:45 +0000169
170 spin_lock(&br->multicast_lock);
171 if (!netif_running(br->dev) || timer_pending(&mp->timer))
172 goto out;
173
Andrew Lunnff0fd342017-11-09 23:10:57 +0100174 mp->host_joined = false;
Andrew Lunn2a260282017-11-09 23:10:58 +0100175 br_mdb_notify(br->dev, NULL, &mp->addr, RTM_DELMDB, 0);
Herbert Xueb1d1642010-02-27 19:41:45 +0000176
177 if (mp->ports)
178 goto out;
179
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200180 rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode,
181 br_mdb_rht_params);
182 hlist_del_rcu(&mp->mdb_node);
Herbert Xueb1d1642010-02-27 19:41:45 +0000183
Nikolay Aleksandrov43295962018-12-05 15:14:25 +0200184 kfree_rcu(mp, rcu);
Herbert Xueb1d1642010-02-27 19:41:45 +0000185
186out:
187 spin_unlock(&br->multicast_lock);
188}
189
190static void br_multicast_del_pg(struct net_bridge *br,
191 struct net_bridge_port_group *pg)
192{
Herbert Xueb1d1642010-02-27 19:41:45 +0000193 struct net_bridge_mdb_entry *mp;
194 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +0000195 struct net_bridge_port_group __rcu **pp;
196
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200197 mp = br_mdb_ip_get(br, &pg->addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000198 if (WARN_ON(!mp))
199 return;
200
Eric Dumazete8051682010-11-15 06:38:10 +0000201 for (pp = &mp->ports;
202 (p = mlock_dereference(*pp, br)) != NULL;
203 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000204 if (p != pg)
205 continue;
206
stephen hemminger83f6a742010-04-27 15:01:06 +0000207 rcu_assign_pointer(*pp, p->next);
Herbert Xueb1d1642010-02-27 19:41:45 +0000208 hlist_del_init(&p->mglist);
209 del_timer(&p->timer);
Elad Raz45ebcce2016-04-21 12:52:45 +0200210 br_mdb_notify(br->dev, p->port, &pg->addr, RTM_DELMDB,
211 p->flags);
Nikolay Aleksandrov43295962018-12-05 15:14:25 +0200212 kfree_rcu(p, rcu);
Herbert Xueb1d1642010-02-27 19:41:45 +0000213
Andrew Lunnff0fd342017-11-09 23:10:57 +0100214 if (!mp->ports && !mp->host_joined &&
Herbert Xueb1d1642010-02-27 19:41:45 +0000215 netif_running(br->dev))
216 mod_timer(&mp->timer, jiffies);
217
218 return;
219 }
220
221 WARN_ON(1);
222}
223
Allen Pais88c1f372017-11-03 11:51:10 +0530224static void br_multicast_port_group_expired(struct timer_list *t)
Herbert Xueb1d1642010-02-27 19:41:45 +0000225{
Allen Pais88c1f372017-11-03 11:51:10 +0530226 struct net_bridge_port_group *pg = from_timer(pg, t, timer);
Herbert Xueb1d1642010-02-27 19:41:45 +0000227 struct net_bridge *br = pg->port->br;
228
229 spin_lock(&br->multicast_lock);
230 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
Elad Raz9d06b6d2016-02-03 09:57:05 +0100231 hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT)
Herbert Xueb1d1642010-02-27 19:41:45 +0000232 goto out;
233
234 br_multicast_del_pg(br, pg);
235
236out:
237 spin_unlock(&br->multicast_lock);
238}
239
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900240static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200241 __be32 group,
242 u8 *igmp_type)
Herbert Xueb1d1642010-02-27 19:41:45 +0000243{
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100244 struct igmpv3_query *ihv3;
245 size_t igmp_hdr_size;
Herbert Xueb1d1642010-02-27 19:41:45 +0000246 struct sk_buff *skb;
247 struct igmphdr *ih;
248 struct ethhdr *eth;
249 struct iphdr *iph;
250
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100251 igmp_hdr_size = sizeof(*ih);
252 if (br->multicast_igmp_version == 3)
253 igmp_hdr_size = sizeof(*ihv3);
Herbert Xueb1d1642010-02-27 19:41:45 +0000254 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100255 igmp_hdr_size + 4);
Herbert Xueb1d1642010-02-27 19:41:45 +0000256 if (!skb)
257 goto out;
258
259 skb->protocol = htons(ETH_P_IP);
260
261 skb_reset_mac_header(skb);
262 eth = eth_hdr(skb);
263
Joe Perchese5a727f2014-02-23 00:05:25 -0800264 ether_addr_copy(eth->h_source, br->dev->dev_addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000265 eth->h_dest[0] = 1;
266 eth->h_dest[1] = 0;
267 eth->h_dest[2] = 0x5e;
268 eth->h_dest[3] = 0;
269 eth->h_dest[4] = 0;
270 eth->h_dest[5] = 1;
271 eth->h_proto = htons(ETH_P_IP);
272 skb_put(skb, sizeof(*eth));
273
274 skb_set_network_header(skb, skb->len);
275 iph = ip_hdr(skb);
276
277 iph->version = 4;
278 iph->ihl = 6;
279 iph->tos = 0xc0;
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100280 iph->tot_len = htons(sizeof(*iph) + igmp_hdr_size + 4);
Herbert Xueb1d1642010-02-27 19:41:45 +0000281 iph->id = 0;
282 iph->frag_off = htons(IP_DF);
283 iph->ttl = 1;
284 iph->protocol = IPPROTO_IGMP;
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +0300285 iph->saddr = br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR) ?
Cong Wang1c8ad5b2013-05-21 21:52:54 +0000286 inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000287 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
288 ((u8 *)&iph[1])[0] = IPOPT_RA;
289 ((u8 *)&iph[1])[1] = 4;
290 ((u8 *)&iph[1])[2] = 0;
291 ((u8 *)&iph[1])[3] = 0;
292 ip_send_check(iph);
293 skb_put(skb, 24);
294
295 skb_set_transport_header(skb, skb->len);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200296 *igmp_type = IGMP_HOST_MEMBERSHIP_QUERY;
Herbert Xueb1d1642010-02-27 19:41:45 +0000297
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100298 switch (br->multicast_igmp_version) {
299 case 2:
300 ih = igmp_hdr(skb);
301 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
302 ih->code = (group ? br->multicast_last_member_interval :
303 br->multicast_query_response_interval) /
304 (HZ / IGMP_TIMER_SCALE);
305 ih->group = group;
306 ih->csum = 0;
307 ih->csum = ip_compute_csum((void *)ih, sizeof(*ih));
308 break;
309 case 3:
310 ihv3 = igmpv3_query_hdr(skb);
311 ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY;
312 ihv3->code = (group ? br->multicast_last_member_interval :
313 br->multicast_query_response_interval) /
314 (HZ / IGMP_TIMER_SCALE);
315 ihv3->group = group;
316 ihv3->qqic = br->multicast_query_interval / HZ;
317 ihv3->nsrcs = 0;
318 ihv3->resv = 0;
319 ihv3->suppress = 0;
320 ihv3->qrv = 2;
321 ihv3->csum = 0;
322 ihv3->csum = ip_compute_csum((void *)ihv3, sizeof(*ihv3));
323 break;
324 }
325
326 skb_put(skb, igmp_hdr_size);
Herbert Xueb1d1642010-02-27 19:41:45 +0000327 __skb_pull(skb, sizeof(*eth));
328
329out:
330 return skb;
331}
332
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000333#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900334static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200335 const struct in6_addr *grp,
336 u8 *igmp_type)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900337{
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100338 struct mld2_query *mld2q;
339 unsigned long interval;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900340 struct ipv6hdr *ip6h;
341 struct mld_msg *mldq;
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100342 size_t mld_hdr_size;
343 struct sk_buff *skb;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900344 struct ethhdr *eth;
345 u8 *hopopt;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900346
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100347 mld_hdr_size = sizeof(*mldq);
348 if (br->multicast_mld_version == 2)
349 mld_hdr_size = sizeof(*mld2q);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900350 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100351 8 + mld_hdr_size);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900352 if (!skb)
353 goto out;
354
355 skb->protocol = htons(ETH_P_IPV6);
356
357 /* Ethernet header */
358 skb_reset_mac_header(skb);
359 eth = eth_hdr(skb);
360
Joe Perchese5a727f2014-02-23 00:05:25 -0800361 ether_addr_copy(eth->h_source, br->dev->dev_addr);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900362 eth->h_proto = htons(ETH_P_IPV6);
363 skb_put(skb, sizeof(*eth));
364
365 /* IPv6 header + HbH option */
366 skb_set_network_header(skb, skb->len);
367 ip6h = ipv6_hdr(skb);
368
369 *(__force __be32 *)ip6h = htonl(0x60000000);
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100370 ip6h->payload_len = htons(8 + mld_hdr_size);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900371 ip6h->nexthdr = IPPROTO_HOPOPTS;
372 ip6h->hop_limit = 1;
Linus Lüssinga7bff752011-03-22 11:40:32 +0000373 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
Ulrich Weberd1d81d42012-03-05 04:52:44 +0000374 if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
375 &ip6h->saddr)) {
376 kfree_skb(skb);
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +0300377 br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, false);
Ulrich Weberd1d81d42012-03-05 04:52:44 +0000378 return NULL;
379 }
daniel0888d5f2016-06-24 12:35:18 +0200380
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +0300381 br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true);
Linus Lüssing36cff5a2011-02-17 08:17:51 +0000382 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900383
384 hopopt = (u8 *)(ip6h + 1);
385 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
386 hopopt[1] = 0; /* length of HbH */
387 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
388 hopopt[3] = 2; /* Length of RA Option */
389 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
390 hopopt[5] = 0;
Eldad Zack1de5a712012-05-17 06:00:25 +0000391 hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */
392 hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900393
394 skb_put(skb, sizeof(*ip6h) + 8);
395
396 /* ICMPv6 */
397 skb_set_transport_header(skb, skb->len);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200398 interval = ipv6_addr_any(grp) ?
Linus Lüssing32de8682013-06-16 23:20:34 +0200399 br->multicast_query_response_interval :
400 br->multicast_last_member_interval;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200401 *igmp_type = ICMPV6_MGM_QUERY;
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100402 switch (br->multicast_mld_version) {
403 case 1:
404 mldq = (struct mld_msg *)icmp6_hdr(skb);
405 mldq->mld_type = ICMPV6_MGM_QUERY;
406 mldq->mld_code = 0;
407 mldq->mld_cksum = 0;
408 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
409 mldq->mld_reserved = 0;
410 mldq->mld_mca = *grp;
411 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
412 sizeof(*mldq), IPPROTO_ICMPV6,
413 csum_partial(mldq,
414 sizeof(*mldq),
415 0));
416 break;
417 case 2:
418 mld2q = (struct mld2_query *)icmp6_hdr(skb);
Lance Richardson53631a52017-01-16 18:11:35 -0500419 mld2q->mld2q_mrc = htons((u16)jiffies_to_msecs(interval));
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100420 mld2q->mld2q_type = ICMPV6_MGM_QUERY;
421 mld2q->mld2q_code = 0;
422 mld2q->mld2q_cksum = 0;
423 mld2q->mld2q_resv1 = 0;
424 mld2q->mld2q_resv2 = 0;
425 mld2q->mld2q_suppress = 0;
426 mld2q->mld2q_qrv = 2;
427 mld2q->mld2q_nsrcs = 0;
428 mld2q->mld2q_qqic = br->multicast_query_interval / HZ;
429 mld2q->mld2q_mca = *grp;
430 mld2q->mld2q_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
431 sizeof(*mld2q),
432 IPPROTO_ICMPV6,
433 csum_partial(mld2q,
434 sizeof(*mld2q),
435 0));
436 break;
437 }
438 skb_put(skb, mld_hdr_size);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900439
440 __skb_pull(skb, sizeof(*eth));
441
442out:
443 return skb;
444}
445#endif
446
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900447static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200448 struct br_ip *addr,
449 u8 *igmp_type)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900450{
451 switch (addr->proto) {
452 case htons(ETH_P_IP):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200453 return br_ip4_multicast_alloc_query(br, addr->u.ip4, igmp_type);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000454#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900455 case htons(ETH_P_IPV6):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200456 return br_ip6_multicast_alloc_query(br, &addr->u.ip6,
457 igmp_type);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900458#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900459 }
460 return NULL;
461}
462
Cong Wangcfd56752012-12-11 22:23:08 +0000463struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100464 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000465{
Herbert Xueb1d1642010-02-27 19:41:45 +0000466 struct net_bridge_mdb_entry *mp;
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000467 int err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000468
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200469 mp = br_mdb_ip_get(br, group);
470 if (mp)
471 return mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000472
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200473 if (atomic_read(&br->mdb_hash_tbl.nelems) >= br->hash_max) {
474 br_opt_toggle(br, BROPT_MULTICAST_ENABLED, false);
475 return ERR_PTR(-E2BIG);
Herbert Xueb1d1642010-02-27 19:41:45 +0000476 }
477
478 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
479 if (unlikely(!mp))
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000480 return ERR_PTR(-ENOMEM);
Herbert Xueb1d1642010-02-27 19:41:45 +0000481
482 mp->br = br;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900483 mp->addr = *group;
Allen Pais88c1f372017-11-03 11:51:10 +0530484 timer_setup(&mp->timer, br_multicast_group_expired, 0);
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200485 err = rhashtable_lookup_insert_fast(&br->mdb_hash_tbl, &mp->rhnode,
486 br_mdb_rht_params);
487 if (err) {
488 kfree(mp);
489 mp = ERR_PTR(err);
490 } else {
491 hlist_add_head_rcu(&mp->mdb_node, &br->mdb_list);
492 }
Eric Dumazet1faabf22013-07-19 20:07:16 -0700493
Herbert Xueb1d1642010-02-27 19:41:45 +0000494 return mp;
495}
496
Cong Wangcfd56752012-12-11 22:23:08 +0000497struct net_bridge_port_group *br_multicast_new_port_group(
498 struct net_bridge_port *port,
499 struct br_ip *group,
Amerigo Wangccb1c312012-12-14 22:09:51 +0000500 struct net_bridge_port_group __rcu *next,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100501 unsigned char flags,
502 const unsigned char *src)
Cong Wangcfd56752012-12-11 22:23:08 +0000503{
504 struct net_bridge_port_group *p;
505
506 p = kzalloc(sizeof(*p), GFP_ATOMIC);
507 if (unlikely(!p))
508 return NULL;
509
510 p->addr = *group;
511 p->port = port;
Elad Raz9d06b6d2016-02-03 09:57:05 +0100512 p->flags = flags;
stephen hemmingereca2a432012-12-13 06:51:28 +0000513 rcu_assign_pointer(p->next, next);
Cong Wangcfd56752012-12-11 22:23:08 +0000514 hlist_add_head(&p->mglist, &port->mglist);
Allen Pais88c1f372017-11-03 11:51:10 +0530515 timer_setup(&p->timer, br_multicast_port_group_expired, 0);
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100516
517 if (src)
518 memcpy(p->eth_addr, src, ETH_ALEN);
519 else
520 memset(p->eth_addr, 0xff, ETH_ALEN);
521
Cong Wangcfd56752012-12-11 22:23:08 +0000522 return p;
523}
524
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100525static bool br_port_group_equal(struct net_bridge_port_group *p,
526 struct net_bridge_port *port,
527 const unsigned char *src)
528{
529 if (p->port != port)
530 return false;
531
532 if (!(port->flags & BR_MULTICAST_TO_UNICAST))
533 return true;
534
535 return ether_addr_equal(src, p->eth_addr);
536}
537
Herbert Xueb1d1642010-02-27 19:41:45 +0000538static int br_multicast_add_group(struct net_bridge *br,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900539 struct net_bridge_port *port,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100540 struct br_ip *group,
541 const unsigned char *src)
Herbert Xueb1d1642010-02-27 19:41:45 +0000542{
Eric Dumazete8051682010-11-15 06:38:10 +0000543 struct net_bridge_port_group __rcu **pp;
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100544 struct net_bridge_port_group *p;
545 struct net_bridge_mdb_entry *mp;
Linus Lüssing454594f2013-10-20 00:58:57 +0200546 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +0000547 int err;
548
Herbert Xueb1d1642010-02-27 19:41:45 +0000549 spin_lock(&br->multicast_lock);
550 if (!netif_running(br->dev) ||
551 (port && port->state == BR_STATE_DISABLED))
552 goto out;
553
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200554 mp = br_multicast_new_group(br, group);
Herbert Xueb1d1642010-02-27 19:41:45 +0000555 err = PTR_ERR(mp);
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000556 if (IS_ERR(mp))
Herbert Xueb1d1642010-02-27 19:41:45 +0000557 goto err;
558
559 if (!port) {
Andrew Lunn2a260282017-11-09 23:10:58 +0100560 if (!mp->host_joined) {
561 mp->host_joined = true;
562 br_mdb_notify(br->dev, NULL, &mp->addr, RTM_NEWMDB, 0);
563 }
Linus Lüssing454594f2013-10-20 00:58:57 +0200564 mod_timer(&mp->timer, now + br->multicast_membership_interval);
Herbert Xueb1d1642010-02-27 19:41:45 +0000565 goto out;
566 }
567
Eric Dumazete8051682010-11-15 06:38:10 +0000568 for (pp = &mp->ports;
569 (p = mlock_dereference(*pp, br)) != NULL;
570 pp = &p->next) {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100571 if (br_port_group_equal(p, port, src))
Linus Lüssing454594f2013-10-20 00:58:57 +0200572 goto found;
Herbert Xueb1d1642010-02-27 19:41:45 +0000573 if ((unsigned long)p->port < (unsigned long)port)
574 break;
575 }
576
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100577 p = br_multicast_new_port_group(port, group, *pp, 0, src);
Herbert Xueb1d1642010-02-27 19:41:45 +0000578 if (unlikely(!p))
579 goto err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000580 rcu_assign_pointer(*pp, p);
Elad Raz45ebcce2016-04-21 12:52:45 +0200581 br_mdb_notify(br->dev, port, group, RTM_NEWMDB, 0);
Herbert Xueb1d1642010-02-27 19:41:45 +0000582
Linus Lüssing454594f2013-10-20 00:58:57 +0200583found:
584 mod_timer(&p->timer, now + br->multicast_membership_interval);
Herbert Xueb1d1642010-02-27 19:41:45 +0000585out:
586 err = 0;
587
588err:
589 spin_unlock(&br->multicast_lock);
590 return err;
591}
592
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900593static int br_ip4_multicast_add_group(struct net_bridge *br,
594 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000595 __be32 group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100596 __u16 vid,
597 const unsigned char *src)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900598{
599 struct br_ip br_group;
600
601 if (ipv4_is_local_multicast(group))
602 return 0;
603
Nikolay Aleksandrov1515a63f2019-04-03 23:27:24 +0300604 memset(&br_group, 0, sizeof(br_group));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900605 br_group.u.ip4 = group;
606 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000607 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900608
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100609 return br_multicast_add_group(br, port, &br_group, src);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900610}
611
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000612#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900613static int br_ip6_multicast_add_group(struct net_bridge *br,
614 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000615 const struct in6_addr *group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100616 __u16 vid,
617 const unsigned char *src)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900618{
619 struct br_ip br_group;
620
Linus Lüssing3c3769e2013-09-04 02:13:39 +0200621 if (ipv6_addr_is_ll_all_nodes(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900622 return 0;
623
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200624 memset(&br_group, 0, sizeof(br_group));
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000625 br_group.u.ip6 = *group;
Linus Lüssing9cc6e0c2011-02-15 13:19:17 +0000626 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000627 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900628
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100629 return br_multicast_add_group(br, port, &br_group, src);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900630}
631#endif
632
Allen Pais88c1f372017-11-03 11:51:10 +0530633static void br_multicast_router_expired(struct timer_list *t)
Herbert Xueb1d1642010-02-27 19:41:45 +0000634{
Allen Pais88c1f372017-11-03 11:51:10 +0530635 struct net_bridge_port *port =
636 from_timer(port, t, multicast_router_timer);
Herbert Xueb1d1642010-02-27 19:41:45 +0000637 struct net_bridge *br = port->br;
638
639 spin_lock(&br->multicast_lock);
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +0100640 if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
641 port->multicast_router == MDB_RTR_TYPE_PERM ||
Nogah Frankelf12e7d92017-02-09 14:54:41 +0100642 timer_pending(&port->multicast_router_timer))
Herbert Xueb1d1642010-02-27 19:41:45 +0000643 goto out;
644
Nogah Frankelf12e7d92017-02-09 14:54:41 +0100645 __del_port_router(port);
Herbert Xueb1d1642010-02-27 19:41:45 +0000646out:
647 spin_unlock(&br->multicast_lock);
648}
649
Yotam Gigi77041422017-10-09 11:15:31 +0200650static void br_mc_router_state_change(struct net_bridge *p,
651 bool is_mc_router)
652{
653 struct switchdev_attr attr = {
654 .orig_dev = p->dev,
655 .id = SWITCHDEV_ATTR_ID_BRIDGE_MROUTER,
656 .flags = SWITCHDEV_F_DEFER,
657 .u.mrouter = is_mc_router,
658 };
659
660 switchdev_port_attr_set(p->dev, &attr);
661}
662
Allen Pais88c1f372017-11-03 11:51:10 +0530663static void br_multicast_local_router_expired(struct timer_list *t)
Herbert Xueb1d1642010-02-27 19:41:45 +0000664{
Allen Pais88c1f372017-11-03 11:51:10 +0530665 struct net_bridge *br = from_timer(br, t, multicast_router_timer);
Yotam Gigi77041422017-10-09 11:15:31 +0200666
667 spin_lock(&br->multicast_lock);
668 if (br->multicast_router == MDB_RTR_TYPE_DISABLED ||
669 br->multicast_router == MDB_RTR_TYPE_PERM ||
670 timer_pending(&br->multicast_router_timer))
671 goto out;
672
673 br_mc_router_state_change(br, false);
674out:
675 spin_unlock(&br->multicast_lock);
Herbert Xueb1d1642010-02-27 19:41:45 +0000676}
677
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200678static void br_multicast_querier_expired(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +0200679 struct bridge_mcast_own_query *query)
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000680{
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000681 spin_lock(&br->multicast_lock);
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +0300682 if (!netif_running(br->dev) || !br_opt_get(br, BROPT_MULTICAST_ENABLED))
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000683 goto out;
684
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200685 br_multicast_start_querier(br, query);
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000686
687out:
688 spin_unlock(&br->multicast_lock);
689}
690
Allen Pais88c1f372017-11-03 11:51:10 +0530691static void br_ip4_multicast_querier_expired(struct timer_list *t)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200692{
Allen Pais88c1f372017-11-03 11:51:10 +0530693 struct net_bridge *br = from_timer(br, t, ip4_other_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200694
Linus Lüssing90010b32014-06-07 18:26:26 +0200695 br_multicast_querier_expired(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200696}
697
698#if IS_ENABLED(CONFIG_IPV6)
Allen Pais88c1f372017-11-03 11:51:10 +0530699static void br_ip6_multicast_querier_expired(struct timer_list *t)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200700{
Allen Pais88c1f372017-11-03 11:51:10 +0530701 struct net_bridge *br = from_timer(br, t, ip6_other_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200702
Linus Lüssing90010b32014-06-07 18:26:26 +0200703 br_multicast_querier_expired(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200704}
705#endif
706
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200707static void br_multicast_select_own_querier(struct net_bridge *br,
708 struct br_ip *ip,
709 struct sk_buff *skb)
710{
711 if (ip->proto == htons(ETH_P_IP))
712 br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr;
713#if IS_ENABLED(CONFIG_IPV6)
714 else
715 br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr;
716#endif
717}
718
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900719static void __br_multicast_send_query(struct net_bridge *br,
720 struct net_bridge_port *port,
721 struct br_ip *ip)
Herbert Xueb1d1642010-02-27 19:41:45 +0000722{
Herbert Xueb1d1642010-02-27 19:41:45 +0000723 struct sk_buff *skb;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200724 u8 igmp_type;
Herbert Xueb1d1642010-02-27 19:41:45 +0000725
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200726 skb = br_multicast_alloc_query(br, ip, &igmp_type);
Herbert Xueb1d1642010-02-27 19:41:45 +0000727 if (!skb)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900728 return;
Herbert Xueb1d1642010-02-27 19:41:45 +0000729
730 if (port) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000731 skb->dev = port->dev;
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_TX);
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500734 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
735 dev_net(port->dev), NULL, skb, NULL, skb->dev,
Linus Lüssingf0b4eec2014-11-17 12:20:28 +0100736 br_dev_queue_push_xmit);
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200737 } else {
738 br_multicast_select_own_querier(br, ip, skb);
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -0700739 br_multicast_count(br, port, skb, igmp_type,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200740 BR_MCAST_DIR_RX);
Herbert Xueb1d1642010-02-27 19:41:45 +0000741 netif_rx(skb);
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200742 }
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900743}
Herbert Xueb1d1642010-02-27 19:41:45 +0000744
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900745static void br_multicast_send_query(struct net_bridge *br,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200746 struct net_bridge_port *port,
Linus Lüssing90010b32014-06-07 18:26:26 +0200747 struct bridge_mcast_own_query *own_query)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900748{
Linus Lüssing90010b32014-06-07 18:26:26 +0200749 struct bridge_mcast_other_query *other_query = NULL;
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100750 struct br_ip br_group;
751 unsigned long time;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900752
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +0300753 if (!netif_running(br->dev) ||
754 !br_opt_get(br, BROPT_MULTICAST_ENABLED) ||
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +0300755 !br_opt_get(br, BROPT_MULTICAST_QUERIER))
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900756 return;
757
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900758 memset(&br_group.u, 0, sizeof(br_group.u));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900759
Linus Lüssing90010b32014-06-07 18:26:26 +0200760 if (port ? (own_query == &port->ip4_own_query) :
761 (own_query == &br->ip4_own_query)) {
762 other_query = &br->ip4_other_query;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200763 br_group.proto = htons(ETH_P_IP);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000764#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200765 } else {
Linus Lüssing90010b32014-06-07 18:26:26 +0200766 other_query = &br->ip6_other_query;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200767 br_group.proto = htons(ETH_P_IPV6);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900768#endif
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200769 }
770
Linus Lüssing90010b32014-06-07 18:26:26 +0200771 if (!other_query || timer_pending(&other_query->timer))
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200772 return;
773
774 __br_multicast_send_query(br, port, &br_group);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900775
Herbert Xueb1d1642010-02-27 19:41:45 +0000776 time = jiffies;
Linus Lüssing90010b32014-06-07 18:26:26 +0200777 time += own_query->startup_sent < br->multicast_startup_query_count ?
Herbert Xueb1d1642010-02-27 19:41:45 +0000778 br->multicast_startup_query_interval :
779 br->multicast_query_interval;
Linus Lüssing90010b32014-06-07 18:26:26 +0200780 mod_timer(&own_query->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +0000781}
782
Linus Lüssing90010b32014-06-07 18:26:26 +0200783static void
784br_multicast_port_query_expired(struct net_bridge_port *port,
785 struct bridge_mcast_own_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +0000786{
Herbert Xueb1d1642010-02-27 19:41:45 +0000787 struct net_bridge *br = port->br;
788
789 spin_lock(&br->multicast_lock);
Dan Carpenter02a780c2010-03-06 01:14:09 +0000790 if (port->state == BR_STATE_DISABLED ||
791 port->state == BR_STATE_BLOCKING)
Herbert Xueb1d1642010-02-27 19:41:45 +0000792 goto out;
793
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200794 if (query->startup_sent < br->multicast_startup_query_count)
795 query->startup_sent++;
Herbert Xueb1d1642010-02-27 19:41:45 +0000796
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200797 br_multicast_send_query(port->br, port, query);
Herbert Xueb1d1642010-02-27 19:41:45 +0000798
799out:
800 spin_unlock(&br->multicast_lock);
801}
802
Allen Pais88c1f372017-11-03 11:51:10 +0530803static void br_ip4_multicast_port_query_expired(struct timer_list *t)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200804{
Allen Pais88c1f372017-11-03 11:51:10 +0530805 struct net_bridge_port *port = from_timer(port, t, ip4_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200806
Linus Lüssing90010b32014-06-07 18:26:26 +0200807 br_multicast_port_query_expired(port, &port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200808}
809
810#if IS_ENABLED(CONFIG_IPV6)
Allen Pais88c1f372017-11-03 11:51:10 +0530811static void br_ip6_multicast_port_query_expired(struct timer_list *t)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200812{
Allen Pais88c1f372017-11-03 11:51:10 +0530813 struct net_bridge_port *port = from_timer(port, t, ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200814
Linus Lüssing90010b32014-06-07 18:26:26 +0200815 br_multicast_port_query_expired(port, &port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200816}
817#endif
818
Nogah Frankel147c1e92017-02-09 14:54:40 +0100819static void br_mc_disabled_update(struct net_device *dev, bool value)
820{
821 struct switchdev_attr attr = {
822 .orig_dev = dev,
823 .id = SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED,
824 .flags = SWITCHDEV_F_DEFER,
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +0300825 .u.mc_disabled = !value,
Nogah Frankel147c1e92017-02-09 14:54:40 +0100826 };
827
828 switchdev_port_attr_set(dev, &attr);
829}
830
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200831int br_multicast_add_port(struct net_bridge_port *port)
Herbert Xueb1d1642010-02-27 19:41:45 +0000832{
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +0100833 port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Herbert Xueb1d1642010-02-27 19:41:45 +0000834
Allen Pais88c1f372017-11-03 11:51:10 +0530835 timer_setup(&port->multicast_router_timer,
836 br_multicast_router_expired, 0);
837 timer_setup(&port->ip4_own_query.timer,
838 br_ip4_multicast_port_query_expired, 0);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200839#if IS_ENABLED(CONFIG_IPV6)
Allen Pais88c1f372017-11-03 11:51:10 +0530840 timer_setup(&port->ip6_own_query.timer,
841 br_ip6_multicast_port_query_expired, 0);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200842#endif
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +0300843 br_mc_disabled_update(port->dev,
844 br_opt_get(port->br, BROPT_MULTICAST_ENABLED));
Nogah Frankel147c1e92017-02-09 14:54:40 +0100845
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200846 port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
847 if (!port->mcast_stats)
848 return -ENOMEM;
849
850 return 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000851}
852
853void br_multicast_del_port(struct net_bridge_port *port)
854{
Satish Ashoke10177a2015-07-15 07:16:51 -0700855 struct net_bridge *br = port->br;
856 struct net_bridge_port_group *pg;
857 struct hlist_node *n;
858
859 /* Take care of the remaining groups, only perm ones should be left */
860 spin_lock_bh(&br->multicast_lock);
861 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
862 br_multicast_del_pg(br, pg);
863 spin_unlock_bh(&br->multicast_lock);
Herbert Xueb1d1642010-02-27 19:41:45 +0000864 del_timer_sync(&port->multicast_router_timer);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200865 free_percpu(port->mcast_stats);
Herbert Xueb1d1642010-02-27 19:41:45 +0000866}
867
Linus Lüssing90010b32014-06-07 18:26:26 +0200868static void br_multicast_enable(struct bridge_mcast_own_query *query)
Herbert Xu561f1102010-02-27 19:41:50 +0000869{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200870 query->startup_sent = 0;
Herbert Xu561f1102010-02-27 19:41:50 +0000871
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200872 if (try_to_del_timer_sync(&query->timer) >= 0 ||
873 del_timer(&query->timer))
874 mod_timer(&query->timer, jiffies);
Herbert Xu561f1102010-02-27 19:41:50 +0000875}
876
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +0200877static void __br_multicast_enable_port(struct net_bridge_port *port)
Herbert Xueb1d1642010-02-27 19:41:45 +0000878{
879 struct net_bridge *br = port->br;
880
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +0300881 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED) || !netif_running(br->dev))
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +0200882 return;
Herbert Xueb1d1642010-02-27 19:41:45 +0000883
Linus Lüssing90010b32014-06-07 18:26:26 +0200884 br_multicast_enable(&port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200885#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +0200886 br_multicast_enable(&port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200887#endif
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +0100888 if (port->multicast_router == MDB_RTR_TYPE_PERM &&
889 hlist_unhashed(&port->rlist))
Satish Ashok754bc542015-06-19 01:22:57 -0700890 br_multicast_add_router(br, port);
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +0200891}
Herbert Xueb1d1642010-02-27 19:41:45 +0000892
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +0200893void br_multicast_enable_port(struct net_bridge_port *port)
894{
895 struct net_bridge *br = port->br;
896
897 spin_lock(&br->multicast_lock);
898 __br_multicast_enable_port(port);
Herbert Xueb1d1642010-02-27 19:41:45 +0000899 spin_unlock(&br->multicast_lock);
900}
901
902void br_multicast_disable_port(struct net_bridge_port *port)
903{
904 struct net_bridge *br = port->br;
905 struct net_bridge_port_group *pg;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800906 struct hlist_node *n;
Herbert Xueb1d1642010-02-27 19:41:45 +0000907
908 spin_lock(&br->multicast_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800909 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
Elad Raz9d06b6d2016-02-03 09:57:05 +0100910 if (!(pg->flags & MDB_PG_FLAGS_PERMANENT))
Satish Ashoke10177a2015-07-15 07:16:51 -0700911 br_multicast_del_pg(br, pg);
Herbert Xueb1d1642010-02-27 19:41:45 +0000912
Nogah Frankelf12e7d92017-02-09 14:54:41 +0100913 __del_port_router(port);
914
Herbert Xueb1d1642010-02-27 19:41:45 +0000915 del_timer(&port->multicast_router_timer);
Linus Lüssing90010b32014-06-07 18:26:26 +0200916 del_timer(&port->ip4_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200917#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +0200918 del_timer(&port->ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200919#endif
Herbert Xueb1d1642010-02-27 19:41:45 +0000920 spin_unlock(&br->multicast_lock);
921}
922
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900923static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
924 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -0400925 struct sk_buff *skb,
926 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000927{
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100928 const unsigned char *src;
Herbert Xueb1d1642010-02-27 19:41:45 +0000929 struct igmpv3_report *ih;
930 struct igmpv3_grec *grec;
931 int i;
932 int len;
933 int num;
934 int type;
935 int err = 0;
936 __be32 group;
937
Herbert Xueb1d1642010-02-27 19:41:45 +0000938 ih = igmpv3_report_hdr(skb);
939 num = ntohs(ih->ngrec);
Linus Lüssingc2d4fbd2015-09-11 18:39:48 +0200940 len = skb_transport_offset(skb) + sizeof(*ih);
Herbert Xueb1d1642010-02-27 19:41:45 +0000941
942 for (i = 0; i < num; i++) {
943 len += sizeof(*grec);
Linus Lüssingba5ea6142019-01-21 07:26:25 +0100944 if (!ip_mc_may_pull(skb, len))
Herbert Xueb1d1642010-02-27 19:41:45 +0000945 return -EINVAL;
946
Herbert Xufd218cf2010-04-07 21:20:47 -0700947 grec = (void *)(skb->data + len - sizeof(*grec));
Herbert Xueb1d1642010-02-27 19:41:45 +0000948 group = grec->grec_mca;
949 type = grec->grec_type;
950
Eric Dumazet8eabf952010-04-20 03:20:05 +0000951 len += ntohs(grec->grec_nsrcs) * 4;
Linus Lüssingba5ea6142019-01-21 07:26:25 +0100952 if (!ip_mc_may_pull(skb, len))
Herbert Xueb1d1642010-02-27 19:41:45 +0000953 return -EINVAL;
954
955 /* We treat this as an IGMPv2 report for now. */
956 switch (type) {
957 case IGMPV3_MODE_IS_INCLUDE:
958 case IGMPV3_MODE_IS_EXCLUDE:
959 case IGMPV3_CHANGE_TO_INCLUDE:
960 case IGMPV3_CHANGE_TO_EXCLUDE:
961 case IGMPV3_ALLOW_NEW_SOURCES:
962 case IGMPV3_BLOCK_OLD_SOURCES:
963 break;
964
965 default:
966 continue;
967 }
968
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100969 src = eth_hdr(skb)->h_source;
Satish Ashokbc8c20a2015-07-13 05:28:37 -0700970 if ((type == IGMPV3_CHANGE_TO_INCLUDE ||
971 type == IGMPV3_MODE_IS_INCLUDE) &&
972 ntohs(grec->grec_nsrcs) == 0) {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100973 br_ip4_multicast_leave_group(br, port, group, vid, src);
Satish Ashokbc8c20a2015-07-13 05:28:37 -0700974 } else {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100975 err = br_ip4_multicast_add_group(br, port, group, vid,
976 src);
Satish Ashokbc8c20a2015-07-13 05:28:37 -0700977 if (err)
978 break;
979 }
Herbert Xueb1d1642010-02-27 19:41:45 +0000980 }
981
982 return err;
983}
984
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000985#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900986static int br_ip6_multicast_mld2_report(struct net_bridge *br,
987 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -0400988 struct sk_buff *skb,
989 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900990{
Linus Lüssingba5ea6142019-01-21 07:26:25 +0100991 unsigned int nsrcs_offset;
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100992 const unsigned char *src;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900993 struct icmp6hdr *icmp6h;
994 struct mld2_grec *grec;
Linus Lüssingba5ea6142019-01-21 07:26:25 +0100995 unsigned int grec_len;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900996 int i;
997 int len;
998 int num;
999 int err = 0;
1000
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001001 if (!ipv6_mc_may_pull(skb, sizeof(*icmp6h)))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001002 return -EINVAL;
1003
1004 icmp6h = icmp6_hdr(skb);
1005 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
Linus Lüssingc2d4fbd2015-09-11 18:39:48 +02001006 len = skb_transport_offset(skb) + sizeof(*icmp6h);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001007
1008 for (i = 0; i < num; i++) {
1009 __be16 *nsrcs, _nsrcs;
1010
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001011 nsrcs_offset = len + offsetof(struct mld2_grec, grec_nsrcs);
1012
1013 if (skb_transport_offset(skb) + ipv6_transport_len(skb) <
1014 nsrcs_offset + sizeof(_nsrcs))
1015 return -EINVAL;
1016
1017 nsrcs = skb_header_pointer(skb, nsrcs_offset,
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001018 sizeof(_nsrcs), &_nsrcs);
1019 if (!nsrcs)
1020 return -EINVAL;
1021
Gustavo A. R. Silva4154b562019-02-07 18:58:56 -06001022 grec_len = struct_size(grec, grec_src, ntohs(*nsrcs));
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001023
1024 if (!ipv6_mc_may_pull(skb, len + grec_len))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001025 return -EINVAL;
1026
1027 grec = (struct mld2_grec *)(skb->data + len);
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001028 len += grec_len;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001029
1030 /* We treat these as MLDv1 reports for now. */
1031 switch (grec->grec_type) {
1032 case MLD2_MODE_IS_INCLUDE:
1033 case MLD2_MODE_IS_EXCLUDE:
1034 case MLD2_CHANGE_TO_INCLUDE:
1035 case MLD2_CHANGE_TO_EXCLUDE:
1036 case MLD2_ALLOW_NEW_SOURCES:
1037 case MLD2_BLOCK_OLD_SOURCES:
1038 break;
1039
1040 default:
1041 continue;
1042 }
1043
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001044 src = eth_hdr(skb)->h_source;
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001045 if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE ||
1046 grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
1047 ntohs(*nsrcs) == 0) {
1048 br_ip6_multicast_leave_group(br, port, &grec->grec_mca,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001049 vid, src);
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001050 } else {
1051 err = br_ip6_multicast_add_group(br, port,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001052 &grec->grec_mca, vid,
1053 src);
Davide Caratti92642512016-08-31 14:16:44 +02001054 if (err)
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001055 break;
1056 }
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001057 }
1058
1059 return err;
1060}
1061#endif
1062
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001063static bool br_ip4_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001064 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001065 __be32 saddr)
1066{
1067 if (!timer_pending(&br->ip4_own_query.timer) &&
1068 !timer_pending(&br->ip4_other_query.timer))
1069 goto update;
1070
1071 if (!br->ip4_querier.addr.u.ip4)
1072 goto update;
1073
1074 if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4))
1075 goto update;
1076
1077 return false;
1078
1079update:
1080 br->ip4_querier.addr.u.ip4 = saddr;
1081
Linus Lüssing2cd41432014-06-07 18:26:29 +02001082 /* update protected by general multicast_lock by caller */
1083 rcu_assign_pointer(br->ip4_querier.port, port);
1084
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001085 return true;
1086}
1087
1088#if IS_ENABLED(CONFIG_IPV6)
1089static bool br_ip6_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001090 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001091 struct in6_addr *saddr)
1092{
1093 if (!timer_pending(&br->ip6_own_query.timer) &&
1094 !timer_pending(&br->ip6_other_query.timer))
1095 goto update;
1096
1097 if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0)
1098 goto update;
1099
1100 return false;
1101
1102update:
1103 br->ip6_querier.addr.u.ip6 = *saddr;
1104
Linus Lüssing2cd41432014-06-07 18:26:29 +02001105 /* update protected by general multicast_lock by caller */
1106 rcu_assign_pointer(br->ip6_querier.port, port);
1107
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001108 return true;
1109}
1110#endif
1111
1112static bool br_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001113 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001114 struct br_ip *saddr)
1115{
1116 switch (saddr->proto) {
1117 case htons(ETH_P_IP):
Linus Lüssing2cd41432014-06-07 18:26:29 +02001118 return br_ip4_multicast_select_querier(br, port, saddr->u.ip4);
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001119#if IS_ENABLED(CONFIG_IPV6)
1120 case htons(ETH_P_IPV6):
Linus Lüssing2cd41432014-06-07 18:26:29 +02001121 return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6);
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001122#endif
1123 }
1124
1125 return false;
1126}
1127
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001128static void
Linus Lüssing90010b32014-06-07 18:26:26 +02001129br_multicast_update_query_timer(struct net_bridge *br,
1130 struct bridge_mcast_other_query *query,
1131 unsigned long max_delay)
Linus Lüssingb00589a2013-08-01 01:06:20 +02001132{
Linus Lüssing90010b32014-06-07 18:26:26 +02001133 if (!timer_pending(&query->timer))
1134 query->delay_time = jiffies + max_delay;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001135
Linus Lüssing90010b32014-06-07 18:26:26 +02001136 mod_timer(&query->timer, jiffies + br->multicast_querier_interval);
Linus Lüssingb00589a2013-08-01 01:06:20 +02001137}
1138
Nogah Frankel6d549642017-02-09 14:54:42 +01001139static void br_port_mc_router_state_change(struct net_bridge_port *p,
1140 bool is_mc_router)
1141{
1142 struct switchdev_attr attr = {
1143 .orig_dev = p->dev,
1144 .id = SWITCHDEV_ATTR_ID_PORT_MROUTER,
1145 .flags = SWITCHDEV_F_DEFER,
1146 .u.mrouter = is_mc_router,
1147 };
1148
1149 switchdev_port_attr_set(p->dev, &attr);
1150}
1151
stephen hemminger7e80c122010-04-27 15:01:04 +00001152/*
Cong Wang7c776022013-06-21 15:37:25 +08001153 * Add port to router_list
stephen hemminger7e80c122010-04-27 15:01:04 +00001154 * list is maintained ordered by pointer value
1155 * and locked by br->multicast_lock and RCU
1156 */
Herbert Xu0909e112010-02-27 19:41:49 +00001157static void br_multicast_add_router(struct net_bridge *br,
1158 struct net_bridge_port *port)
1159{
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001160 struct net_bridge_port *p;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001161 struct hlist_node *slot = NULL;
Herbert Xu0909e112010-02-27 19:41:49 +00001162
Nikolay Aleksandrov1a040ea2015-06-09 10:23:57 -07001163 if (!hlist_unhashed(&port->rlist))
1164 return;
1165
Sasha Levinb67bfe02013-02-27 17:06:00 -08001166 hlist_for_each_entry(p, &br->router_list, rlist) {
stephen hemminger7e80c122010-04-27 15:01:04 +00001167 if ((unsigned long) port >= (unsigned long) p)
1168 break;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001169 slot = &p->rlist;
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001170 }
Herbert Xu0909e112010-02-27 19:41:49 +00001171
stephen hemminger7e80c122010-04-27 15:01:04 +00001172 if (slot)
Ken Helias1d023282014-08-06 16:09:16 -07001173 hlist_add_behind_rcu(&port->rlist, slot);
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001174 else
1175 hlist_add_head_rcu(&port->rlist, &br->router_list);
Satish Ashok949f1e32015-07-23 05:00:53 -07001176 br_rtr_notify(br->dev, port, RTM_NEWMDB);
Nogah Frankel6d549642017-02-09 14:54:42 +01001177 br_port_mc_router_state_change(port, true);
Herbert Xu0909e112010-02-27 19:41:49 +00001178}
1179
Herbert Xueb1d1642010-02-27 19:41:45 +00001180static void br_multicast_mark_router(struct net_bridge *br,
1181 struct net_bridge_port *port)
1182{
1183 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +00001184
1185 if (!port) {
Yotam Gigi77041422017-10-09 11:15:31 +02001186 if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) {
1187 if (!timer_pending(&br->multicast_router_timer))
1188 br_mc_router_state_change(br, true);
Herbert Xueb1d1642010-02-27 19:41:45 +00001189 mod_timer(&br->multicast_router_timer,
1190 now + br->multicast_querier_interval);
Yotam Gigi77041422017-10-09 11:15:31 +02001191 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001192 return;
1193 }
1194
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001195 if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
1196 port->multicast_router == MDB_RTR_TYPE_PERM)
Herbert Xueb1d1642010-02-27 19:41:45 +00001197 return;
1198
Herbert Xu0909e112010-02-27 19:41:49 +00001199 br_multicast_add_router(br, port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001200
Herbert Xueb1d1642010-02-27 19:41:45 +00001201 mod_timer(&port->multicast_router_timer,
1202 now + br->multicast_querier_interval);
1203}
1204
1205static void br_multicast_query_received(struct net_bridge *br,
1206 struct net_bridge_port *port,
Linus Lüssing90010b32014-06-07 18:26:26 +02001207 struct bridge_mcast_other_query *query,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001208 struct br_ip *saddr,
Linus Lüssingb00589a2013-08-01 01:06:20 +02001209 unsigned long max_delay)
Herbert Xueb1d1642010-02-27 19:41:45 +00001210{
Linus Lüssing2cd41432014-06-07 18:26:29 +02001211 if (!br_multicast_select_querier(br, port, saddr))
Herbert Xueb1d1642010-02-27 19:41:45 +00001212 return;
1213
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001214 br_multicast_update_query_timer(br, query, max_delay);
Hangbin Liu278e2142019-02-22 21:22:32 +08001215 br_multicast_mark_router(br, port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001216}
1217
zhong jiang9c2e9552018-08-06 11:07:23 +08001218static void br_ip4_multicast_query(struct net_bridge *br,
1219 struct net_bridge_port *port,
1220 struct sk_buff *skb,
1221 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001222{
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001223 unsigned int transport_len = ip_transport_len(skb);
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001224 const struct iphdr *iph = ip_hdr(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001225 struct igmphdr *ih = igmp_hdr(skb);
1226 struct net_bridge_mdb_entry *mp;
1227 struct igmpv3_query *ih3;
1228 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +00001229 struct net_bridge_port_group __rcu **pp;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001230 struct br_ip saddr;
Herbert Xueb1d1642010-02-27 19:41:45 +00001231 unsigned long max_delay;
1232 unsigned long now = jiffies;
1233 __be32 group;
1234
1235 spin_lock(&br->multicast_lock);
1236 if (!netif_running(br->dev) ||
1237 (port && port->state == BR_STATE_DISABLED))
1238 goto out;
1239
Herbert Xueb1d1642010-02-27 19:41:45 +00001240 group = ih->group;
1241
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001242 if (transport_len == sizeof(*ih)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001243 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1244
1245 if (!max_delay) {
1246 max_delay = 10 * HZ;
1247 group = 0;
1248 }
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001249 } else if (transport_len >= sizeof(*ih3)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001250 ih3 = igmpv3_query_hdr(skb);
1251 if (ih3->nsrcs)
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001252 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001253
YOSHIFUJI Hideaki / 吉藤英明0ba8c9e2010-03-15 19:27:00 +00001254 max_delay = ih3->code ?
1255 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001256 } else {
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001257 goto out;
1258 }
1259
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001260 if (!group) {
1261 saddr.proto = htons(ETH_P_IP);
1262 saddr.u.ip4 = iph->saddr;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001263
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001264 br_multicast_query_received(br, port, &br->ip4_other_query,
1265 &saddr, max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001266 goto out;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001267 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001268
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02001269 mp = br_mdb_ip4_get(br, group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001270 if (!mp)
1271 goto out;
1272
1273 max_delay *= br->multicast_last_member_count;
1274
Andrew Lunnff0fd342017-11-09 23:10:57 +01001275 if (mp->host_joined &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001276 (timer_pending(&mp->timer) ?
1277 time_after(mp->timer.expires, now + max_delay) :
1278 try_to_del_timer_sync(&mp->timer) >= 0))
1279 mod_timer(&mp->timer, now + max_delay);
1280
Eric Dumazete8051682010-11-15 06:38:10 +00001281 for (pp = &mp->ports;
1282 (p = mlock_dereference(*pp, br)) != NULL;
1283 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001284 if (timer_pending(&p->timer) ?
1285 time_after(p->timer.expires, now + max_delay) :
1286 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001287 mod_timer(&p->timer, now + max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001288 }
1289
1290out:
1291 spin_unlock(&br->multicast_lock);
Herbert Xueb1d1642010-02-27 19:41:45 +00001292}
1293
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001294#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001295static int br_ip6_multicast_query(struct net_bridge *br,
1296 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001297 struct sk_buff *skb,
1298 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001299{
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001300 unsigned int transport_len = ipv6_transport_len(skb);
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001301 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
stephen hemmingereca2a432012-12-13 06:51:28 +00001302 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001303 struct net_bridge_mdb_entry *mp;
1304 struct mld2_query *mld2q;
Eric Dumazete8051682010-11-15 06:38:10 +00001305 struct net_bridge_port_group *p;
1306 struct net_bridge_port_group __rcu **pp;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001307 struct br_ip saddr;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001308 unsigned long max_delay;
1309 unsigned long now = jiffies;
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001310 unsigned int offset = skb_transport_offset(skb);
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001311 const struct in6_addr *group = NULL;
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001312 bool is_general_query;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001313 int err = 0;
1314
1315 spin_lock(&br->multicast_lock);
1316 if (!netif_running(br->dev) ||
1317 (port && port->state == BR_STATE_DISABLED))
1318 goto out;
1319
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001320 if (transport_len == sizeof(*mld)) {
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001321 if (!pskb_may_pull(skb, offset + sizeof(*mld))) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001322 err = -EINVAL;
1323 goto out;
1324 }
1325 mld = (struct mld_msg *) icmp6_hdr(skb);
Li RongQing47152132012-07-09 23:56:12 +00001326 max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001327 if (max_delay)
1328 group = &mld->mld_mca;
Linus Lüssing248ba8e2013-08-06 00:32:05 +02001329 } else {
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001330 if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001331 err = -EINVAL;
1332 goto out;
1333 }
1334 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1335 if (!mld2q->mld2q_nsrcs)
1336 group = &mld2q->mld2q_mca;
Daniel Borkmanne3f5b172013-09-04 00:19:39 +02001337
1338 max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001339 }
1340
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001341 is_general_query = group && ipv6_addr_any(group);
1342
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001343 if (is_general_query) {
1344 saddr.proto = htons(ETH_P_IPV6);
1345 saddr.u.ip6 = ip6h->saddr;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001346
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001347 br_multicast_query_received(br, port, &br->ip6_other_query,
1348 &saddr, max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001349 goto out;
Linus Lüssing6c03ee82014-06-12 01:41:23 +02001350 } else if (!group) {
1351 goto out;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001352 }
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001353
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02001354 mp = br_mdb_ip6_get(br, group, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001355 if (!mp)
1356 goto out;
1357
1358 max_delay *= br->multicast_last_member_count;
Andrew Lunnff0fd342017-11-09 23:10:57 +01001359 if (mp->host_joined &&
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001360 (timer_pending(&mp->timer) ?
1361 time_after(mp->timer.expires, now + max_delay) :
1362 try_to_del_timer_sync(&mp->timer) >= 0))
1363 mod_timer(&mp->timer, now + max_delay);
1364
Eric Dumazete8051682010-11-15 06:38:10 +00001365 for (pp = &mp->ports;
1366 (p = mlock_dereference(*pp, br)) != NULL;
1367 pp = &p->next) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001368 if (timer_pending(&p->timer) ?
1369 time_after(p->timer.expires, now + max_delay) :
1370 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001371 mod_timer(&p->timer, now + max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001372 }
1373
1374out:
1375 spin_unlock(&br->multicast_lock);
1376 return err;
1377}
1378#endif
1379
Linus Lüssing90010b32014-06-07 18:26:26 +02001380static void
1381br_multicast_leave_group(struct net_bridge *br,
1382 struct net_bridge_port *port,
1383 struct br_ip *group,
1384 struct bridge_mcast_other_query *other_query,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001385 struct bridge_mcast_own_query *own_query,
1386 const unsigned char *src)
Herbert Xueb1d1642010-02-27 19:41:45 +00001387{
Herbert Xueb1d1642010-02-27 19:41:45 +00001388 struct net_bridge_mdb_entry *mp;
1389 struct net_bridge_port_group *p;
1390 unsigned long now;
1391 unsigned long time;
1392
Herbert Xueb1d1642010-02-27 19:41:45 +00001393 spin_lock(&br->multicast_lock);
1394 if (!netif_running(br->dev) ||
Satish Ashok544586f2015-07-28 03:28:27 -07001395 (port && port->state == BR_STATE_DISABLED))
Herbert Xueb1d1642010-02-27 19:41:45 +00001396 goto out;
1397
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02001398 mp = br_mdb_ip_get(br, group);
Herbert Xueb1d1642010-02-27 19:41:45 +00001399 if (!mp)
1400 goto out;
1401
Satish Ashok544586f2015-07-28 03:28:27 -07001402 if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
1403 struct net_bridge_port_group __rcu **pp;
1404
1405 for (pp = &mp->ports;
1406 (p = mlock_dereference(*pp, br)) != NULL;
1407 pp = &p->next) {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001408 if (!br_port_group_equal(p, port, src))
Satish Ashok544586f2015-07-28 03:28:27 -07001409 continue;
1410
1411 rcu_assign_pointer(*pp, p->next);
1412 hlist_del_init(&p->mglist);
1413 del_timer(&p->timer);
Nikolay Aleksandrov43295962018-12-05 15:14:25 +02001414 kfree_rcu(p, rcu);
Elad Raz45ebcce2016-04-21 12:52:45 +02001415 br_mdb_notify(br->dev, port, group, RTM_DELMDB,
1416 p->flags);
Satish Ashok544586f2015-07-28 03:28:27 -07001417
Andrew Lunnff0fd342017-11-09 23:10:57 +01001418 if (!mp->ports && !mp->host_joined &&
Satish Ashok544586f2015-07-28 03:28:27 -07001419 netif_running(br->dev))
1420 mod_timer(&mp->timer, jiffies);
1421 }
1422 goto out;
1423 }
1424
1425 if (timer_pending(&other_query->timer))
1426 goto out;
1427
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03001428 if (br_opt_get(br, BROPT_MULTICAST_QUERIER)) {
Cong Wang6b7df112013-05-21 21:52:56 +00001429 __br_multicast_send_query(br, port, &mp->addr);
1430
1431 time = jiffies + br->multicast_last_member_count *
1432 br->multicast_last_member_interval;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001433
Linus Lüssing90010b32014-06-07 18:26:26 +02001434 mod_timer(&own_query->timer, time);
Cong Wang6b7df112013-05-21 21:52:56 +00001435
1436 for (p = mlock_dereference(mp->ports, br);
1437 p != NULL;
1438 p = mlock_dereference(p->next, br)) {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001439 if (!br_port_group_equal(p, port, src))
Cong Wang6b7df112013-05-21 21:52:56 +00001440 continue;
1441
1442 if (!hlist_unhashed(&p->mglist) &&
1443 (timer_pending(&p->timer) ?
1444 time_after(p->timer.expires, time) :
1445 try_to_del_timer_sync(&p->timer) >= 0)) {
1446 mod_timer(&p->timer, time);
1447 }
1448
1449 break;
1450 }
1451 }
1452
Herbert Xueb1d1642010-02-27 19:41:45 +00001453 now = jiffies;
1454 time = now + br->multicast_last_member_count *
1455 br->multicast_last_member_interval;
1456
1457 if (!port) {
Andrew Lunnff0fd342017-11-09 23:10:57 +01001458 if (mp->host_joined &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001459 (timer_pending(&mp->timer) ?
1460 time_after(mp->timer.expires, time) :
1461 try_to_del_timer_sync(&mp->timer) >= 0)) {
1462 mod_timer(&mp->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +00001463 }
Linus Lüssing454594f2013-10-20 00:58:57 +02001464
1465 goto out;
1466 }
1467
1468 for (p = mlock_dereference(mp->ports, br);
1469 p != NULL;
1470 p = mlock_dereference(p->next, br)) {
1471 if (p->port != port)
1472 continue;
1473
1474 if (!hlist_unhashed(&p->mglist) &&
1475 (timer_pending(&p->timer) ?
1476 time_after(p->timer.expires, time) :
1477 try_to_del_timer_sync(&p->timer) >= 0)) {
1478 mod_timer(&p->timer, time);
1479 }
1480
1481 break;
Herbert Xueb1d1642010-02-27 19:41:45 +00001482 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001483out:
1484 spin_unlock(&br->multicast_lock);
1485}
1486
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001487static void br_ip4_multicast_leave_group(struct net_bridge *br,
1488 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001489 __be32 group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001490 __u16 vid,
1491 const unsigned char *src)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001492{
1493 struct br_ip br_group;
Linus Lüssing90010b32014-06-07 18:26:26 +02001494 struct bridge_mcast_own_query *own_query;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001495
1496 if (ipv4_is_local_multicast(group))
1497 return;
1498
Linus Lüssing90010b32014-06-07 18:26:26 +02001499 own_query = port ? &port->ip4_own_query : &br->ip4_own_query;
1500
Nikolay Aleksandrov1515a63f2019-04-03 23:27:24 +03001501 memset(&br_group, 0, sizeof(br_group));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001502 br_group.u.ip4 = group;
1503 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001504 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001505
Linus Lüssing90010b32014-06-07 18:26:26 +02001506 br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001507 own_query, src);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001508}
1509
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001510#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001511static void br_ip6_multicast_leave_group(struct net_bridge *br,
1512 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001513 const struct in6_addr *group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001514 __u16 vid,
1515 const unsigned char *src)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001516{
1517 struct br_ip br_group;
Linus Lüssing90010b32014-06-07 18:26:26 +02001518 struct bridge_mcast_own_query *own_query;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001519
Linus Lüssing3c3769e2013-09-04 02:13:39 +02001520 if (ipv6_addr_is_ll_all_nodes(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001521 return;
1522
Linus Lüssing90010b32014-06-07 18:26:26 +02001523 own_query = port ? &port->ip6_own_query : &br->ip6_own_query;
1524
Nikolay Aleksandrov1515a63f2019-04-03 23:27:24 +03001525 memset(&br_group, 0, sizeof(br_group));
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001526 br_group.u.ip6 = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001527 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001528 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001529
Linus Lüssing90010b32014-06-07 18:26:26 +02001530 br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001531 own_query, src);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001532}
1533#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001534
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001535static void br_multicast_err_count(const struct net_bridge *br,
1536 const struct net_bridge_port *p,
1537 __be16 proto)
1538{
1539 struct bridge_mcast_stats __percpu *stats;
1540 struct bridge_mcast_stats *pstats;
1541
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03001542 if (!br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001543 return;
1544
1545 if (p)
1546 stats = p->mcast_stats;
1547 else
1548 stats = br->mcast_stats;
1549 if (WARN_ON(!stats))
1550 return;
1551
1552 pstats = this_cpu_ptr(stats);
1553
1554 u64_stats_update_begin(&pstats->syncp);
1555 switch (proto) {
1556 case htons(ETH_P_IP):
1557 pstats->mstats.igmp_parse_errors++;
1558 break;
1559#if IS_ENABLED(CONFIG_IPV6)
1560 case htons(ETH_P_IPV6):
1561 pstats->mstats.mld_parse_errors++;
1562 break;
1563#endif
1564 }
1565 u64_stats_update_end(&pstats->syncp);
1566}
1567
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001568static void br_multicast_pim(struct net_bridge *br,
1569 struct net_bridge_port *port,
1570 const struct sk_buff *skb)
1571{
1572 unsigned int offset = skb_transport_offset(skb);
1573 struct pimhdr *pimhdr, _pimhdr;
1574
1575 pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr);
1576 if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION ||
1577 pim_hdr_type(pimhdr) != PIM_TYPE_HELLO)
1578 return;
1579
1580 br_multicast_mark_router(br, port);
1581}
1582
Linus Lüssing4b3087c2019-01-21 07:26:28 +01001583static int br_ip4_multicast_mrd_rcv(struct net_bridge *br,
1584 struct net_bridge_port *port,
1585 struct sk_buff *skb)
1586{
1587 if (ip_hdr(skb)->protocol != IPPROTO_IGMP ||
1588 igmp_hdr(skb)->type != IGMP_MRDISC_ADV)
1589 return -ENOMSG;
1590
1591 br_multicast_mark_router(br, port);
1592
1593 return 0;
1594}
1595
Herbert Xueb1d1642010-02-27 19:41:45 +00001596static int br_multicast_ipv4_rcv(struct net_bridge *br,
1597 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001598 struct sk_buff *skb,
1599 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001600{
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001601 const unsigned char *src;
Herbert Xueb1d1642010-02-27 19:41:45 +00001602 struct igmphdr *ih;
Herbert Xueb1d1642010-02-27 19:41:45 +00001603 int err;
1604
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001605 err = ip_mc_check_igmp(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001606
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001607 if (err == -ENOMSG) {
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001608 if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) {
Herbert Xubd4265f2011-06-23 02:39:12 +00001609 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001610 } else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) {
1611 if (ip_hdr(skb)->protocol == IPPROTO_PIM)
1612 br_multicast_pim(br, port, skb);
Linus Lüssing4b3087c2019-01-21 07:26:28 +01001613 } else if (ipv4_is_all_snoopers(ip_hdr(skb)->daddr)) {
Li RongQing08e71622019-02-19 10:17:09 +08001614 br_ip4_multicast_mrd_rcv(br, port, skb);
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001615 }
Linus Lüssing4b3087c2019-01-21 07:26:28 +01001616
Herbert Xueb1d1642010-02-27 19:41:45 +00001617 return 0;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001618 } else if (err < 0) {
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001619 br_multicast_err_count(br, port, skb->protocol);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001620 return err;
Herbert Xubd4265f2011-06-23 02:39:12 +00001621 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001622
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001623 ih = igmp_hdr(skb);
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001624 src = eth_hdr(skb)->h_source;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001625 BR_INPUT_SKB_CB(skb)->igmp = ih->type;
Herbert Xueb1d1642010-02-27 19:41:45 +00001626
1627 switch (ih->type) {
1628 case IGMP_HOST_MEMBERSHIP_REPORT:
1629 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Fernando Luis Vázquez Cao62b2bcb2011-06-13 15:04:43 +00001630 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001631 err = br_ip4_multicast_add_group(br, port, ih->group, vid, src);
Herbert Xueb1d1642010-02-27 19:41:45 +00001632 break;
1633 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001634 err = br_ip4_multicast_igmp3_report(br, port, skb, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001635 break;
1636 case IGMP_HOST_MEMBERSHIP_QUERY:
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001637 br_ip4_multicast_query(br, port, skb, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001638 break;
1639 case IGMP_HOST_LEAVE_MESSAGE:
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001640 br_ip4_multicast_leave_group(br, port, ih->group, vid, src);
Herbert Xueb1d1642010-02-27 19:41:45 +00001641 break;
1642 }
1643
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07001644 br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001645 BR_MCAST_DIR_RX);
1646
Herbert Xueb1d1642010-02-27 19:41:45 +00001647 return err;
1648}
1649
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001650#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing4b3087c2019-01-21 07:26:28 +01001651static int br_ip6_multicast_mrd_rcv(struct net_bridge *br,
1652 struct net_bridge_port *port,
1653 struct sk_buff *skb)
1654{
1655 int ret;
1656
1657 if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6)
1658 return -ENOMSG;
1659
1660 ret = ipv6_mc_check_icmpv6(skb);
1661 if (ret < 0)
1662 return ret;
1663
1664 if (icmp6_hdr(skb)->icmp6_type != ICMPV6_MRDISC_ADV)
1665 return -ENOMSG;
1666
1667 br_multicast_mark_router(br, port);
1668
1669 return 0;
1670}
1671
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001672static int br_multicast_ipv6_rcv(struct net_bridge *br,
1673 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001674 struct sk_buff *skb,
1675 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001676{
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001677 const unsigned char *src;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001678 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001679 int err;
1680
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001681 err = ipv6_mc_check_mld(skb);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001682
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001683 if (err == -ENOMSG) {
1684 if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
1685 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Linus Lüssing4b3087c2019-01-21 07:26:28 +01001686
1687 if (ipv6_addr_is_all_snoopers(&ipv6_hdr(skb)->daddr)) {
1688 err = br_ip6_multicast_mrd_rcv(br, port, skb);
1689
1690 if (err < 0 && err != -ENOMSG) {
1691 br_multicast_err_count(br, port, skb->protocol);
1692 return err;
1693 }
1694 }
1695
Linus Lüssing8fad9c32013-09-04 02:13:38 +02001696 return 0;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001697 } else if (err < 0) {
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001698 br_multicast_err_count(br, port, skb->protocol);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001699 return err;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001700 }
1701
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001702 mld = (struct mld_msg *)skb_transport_header(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001703 BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001704
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001705 switch (mld->mld_type) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001706 case ICMPV6_MGM_REPORT:
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001707 src = eth_hdr(skb)->h_source;
Fernando Luis Vázquez Caofc2af6c2011-06-13 15:06:58 +00001708 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001709 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid,
1710 src);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001711 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001712 case ICMPV6_MLD2_REPORT:
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001713 err = br_ip6_multicast_mld2_report(br, port, skb, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001714 break;
1715 case ICMPV6_MGM_QUERY:
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001716 err = br_ip6_multicast_query(br, port, skb, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001717 break;
1718 case ICMPV6_MGM_REDUCTION:
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001719 src = eth_hdr(skb)->h_source;
1720 br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001721 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001722 }
1723
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07001724 br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001725 BR_MCAST_DIR_RX);
1726
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001727 return err;
1728}
1729#endif
1730
Herbert Xueb1d1642010-02-27 19:41:45 +00001731int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001732 struct sk_buff *skb, u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001733{
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001734 int ret = 0;
1735
YOSHIFUJI Hideaki / 吉藤英明1fafc7a2010-04-25 08:06:40 +00001736 BR_INPUT_SKB_CB(skb)->igmp = 0;
1737 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1738
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +03001739 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
Herbert Xueb1d1642010-02-27 19:41:45 +00001740 return 0;
1741
1742 switch (skb->protocol) {
1743 case htons(ETH_P_IP):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001744 ret = br_multicast_ipv4_rcv(br, port, skb, vid);
1745 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001746#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001747 case htons(ETH_P_IPV6):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001748 ret = br_multicast_ipv6_rcv(br, port, skb, vid);
1749 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001750#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001751 }
1752
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001753 return ret;
Herbert Xueb1d1642010-02-27 19:41:45 +00001754}
1755
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001756static void br_multicast_query_expired(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001757 struct bridge_mcast_own_query *query,
1758 struct bridge_mcast_querier *querier)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001759{
1760 spin_lock(&br->multicast_lock);
1761 if (query->startup_sent < br->multicast_startup_query_count)
1762 query->startup_sent++;
1763
Eric Dumazet71d9f612015-05-28 04:42:54 -07001764 RCU_INIT_POINTER(querier->port, NULL);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001765 br_multicast_send_query(br, NULL, query);
1766 spin_unlock(&br->multicast_lock);
1767}
1768
Allen Pais88c1f372017-11-03 11:51:10 +05301769static void br_ip4_multicast_query_expired(struct timer_list *t)
Herbert Xueb1d1642010-02-27 19:41:45 +00001770{
Allen Pais88c1f372017-11-03 11:51:10 +05301771 struct net_bridge *br = from_timer(br, t, ip4_own_query.timer);
Herbert Xueb1d1642010-02-27 19:41:45 +00001772
Linus Lüssing2cd41432014-06-07 18:26:29 +02001773 br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier);
Herbert Xueb1d1642010-02-27 19:41:45 +00001774}
1775
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001776#if IS_ENABLED(CONFIG_IPV6)
Allen Pais88c1f372017-11-03 11:51:10 +05301777static void br_ip6_multicast_query_expired(struct timer_list *t)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001778{
Allen Pais88c1f372017-11-03 11:51:10 +05301779 struct net_bridge *br = from_timer(br, t, ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001780
Linus Lüssing2cd41432014-06-07 18:26:29 +02001781 br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001782}
1783#endif
1784
Herbert Xueb1d1642010-02-27 19:41:45 +00001785void br_multicast_init(struct net_bridge *br)
1786{
Nikolay Aleksandrovd08c6bc2018-12-05 15:14:27 +02001787 br->hash_max = BR_MULTICAST_DEFAULT_HASH_MAX;
Herbert Xueb1d1642010-02-27 19:41:45 +00001788
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001789 br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Herbert Xueb1d1642010-02-27 19:41:45 +00001790 br->multicast_last_member_count = 2;
1791 br->multicast_startup_query_count = 2;
1792
1793 br->multicast_last_member_interval = HZ;
1794 br->multicast_query_response_interval = 10 * HZ;
1795 br->multicast_startup_query_interval = 125 * HZ / 4;
1796 br->multicast_query_interval = 125 * HZ;
1797 br->multicast_querier_interval = 255 * HZ;
1798 br->multicast_membership_interval = 260 * HZ;
1799
Linus Lüssing90010b32014-06-07 18:26:26 +02001800 br->ip4_other_query.delay_time = 0;
Linus Lüssing2cd41432014-06-07 18:26:29 +02001801 br->ip4_querier.port = NULL;
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001802 br->multicast_igmp_version = 2;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001803#if IS_ENABLED(CONFIG_IPV6)
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001804 br->multicast_mld_version = 1;
Linus Lüssing90010b32014-06-07 18:26:26 +02001805 br->ip6_other_query.delay_time = 0;
Linus Lüssing2cd41432014-06-07 18:26:29 +02001806 br->ip6_querier.port = NULL;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001807#endif
Ido Schimmel6919622a2018-10-01 11:57:01 +03001808 br_opt_toggle(br, BROPT_MULTICAST_ENABLED, true);
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03001809 br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true);
Linus Lüssingb00589a2013-08-01 01:06:20 +02001810
Herbert Xueb1d1642010-02-27 19:41:45 +00001811 spin_lock_init(&br->multicast_lock);
Allen Pais88c1f372017-11-03 11:51:10 +05301812 timer_setup(&br->multicast_router_timer,
1813 br_multicast_local_router_expired, 0);
1814 timer_setup(&br->ip4_other_query.timer,
1815 br_ip4_multicast_querier_expired, 0);
1816 timer_setup(&br->ip4_own_query.timer,
1817 br_ip4_multicast_query_expired, 0);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001818#if IS_ENABLED(CONFIG_IPV6)
Allen Pais88c1f372017-11-03 11:51:10 +05301819 timer_setup(&br->ip6_other_query.timer,
1820 br_ip6_multicast_querier_expired, 0);
1821 timer_setup(&br->ip6_own_query.timer,
1822 br_ip6_multicast_query_expired, 0);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001823#endif
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02001824 INIT_HLIST_HEAD(&br->mdb_list);
Herbert Xueb1d1642010-02-27 19:41:45 +00001825}
1826
Linus Lüssing4effd282019-01-21 07:26:27 +01001827static void br_ip4_multicast_join_snoopers(struct net_bridge *br)
1828{
1829 struct in_device *in_dev = in_dev_get(br->dev);
1830
1831 if (!in_dev)
1832 return;
1833
Florian Fainelli9fb20802019-02-01 20:20:52 -08001834 __ip_mc_inc_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
Linus Lüssing4effd282019-01-21 07:26:27 +01001835 in_dev_put(in_dev);
1836}
1837
1838#if IS_ENABLED(CONFIG_IPV6)
1839static void br_ip6_multicast_join_snoopers(struct net_bridge *br)
1840{
1841 struct in6_addr addr;
1842
1843 ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
1844 ipv6_dev_mc_inc(br->dev, &addr);
1845}
1846#else
1847static inline void br_ip6_multicast_join_snoopers(struct net_bridge *br)
1848{
1849}
1850#endif
1851
1852static void br_multicast_join_snoopers(struct net_bridge *br)
1853{
1854 br_ip4_multicast_join_snoopers(br);
1855 br_ip6_multicast_join_snoopers(br);
1856}
1857
1858static void br_ip4_multicast_leave_snoopers(struct net_bridge *br)
1859{
1860 struct in_device *in_dev = in_dev_get(br->dev);
1861
1862 if (WARN_ON(!in_dev))
1863 return;
1864
Florian Fainelli9fb20802019-02-01 20:20:52 -08001865 __ip_mc_dec_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC);
Linus Lüssing4effd282019-01-21 07:26:27 +01001866 in_dev_put(in_dev);
1867}
1868
1869#if IS_ENABLED(CONFIG_IPV6)
1870static void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
1871{
1872 struct in6_addr addr;
1873
1874 ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a));
1875 ipv6_dev_mc_dec(br->dev, &addr);
1876}
1877#else
1878static inline void br_ip6_multicast_leave_snoopers(struct net_bridge *br)
1879{
1880}
1881#endif
1882
1883static void br_multicast_leave_snoopers(struct net_bridge *br)
1884{
1885 br_ip4_multicast_leave_snoopers(br);
1886 br_ip6_multicast_leave_snoopers(br);
1887}
1888
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001889static void __br_multicast_open(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +02001890 struct bridge_mcast_own_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +00001891{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001892 query->startup_sent = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001893
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +03001894 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
Herbert Xueb1d1642010-02-27 19:41:45 +00001895 return;
1896
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001897 mod_timer(&query->timer, jiffies);
1898}
1899
1900void br_multicast_open(struct net_bridge *br)
1901{
Linus Lüssing4effd282019-01-21 07:26:27 +01001902 if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
1903 br_multicast_join_snoopers(br);
1904
Linus Lüssing90010b32014-06-07 18:26:26 +02001905 __br_multicast_open(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001906#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001907 __br_multicast_open(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001908#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001909}
1910
1911void br_multicast_stop(struct net_bridge *br)
1912{
Herbert Xueb1d1642010-02-27 19:41:45 +00001913 del_timer_sync(&br->multicast_router_timer);
Linus Lüssing90010b32014-06-07 18:26:26 +02001914 del_timer_sync(&br->ip4_other_query.timer);
1915 del_timer_sync(&br->ip4_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001916#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001917 del_timer_sync(&br->ip6_other_query.timer);
1918 del_timer_sync(&br->ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001919#endif
Linus Lüssing4effd282019-01-21 07:26:27 +01001920
1921 if (br_opt_get(br, BROPT_MULTICAST_ENABLED))
1922 br_multicast_leave_snoopers(br);
Satish Ashoke10177a2015-07-15 07:16:51 -07001923}
1924
1925void br_multicast_dev_del(struct net_bridge *br)
1926{
Satish Ashoke10177a2015-07-15 07:16:51 -07001927 struct net_bridge_mdb_entry *mp;
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02001928 struct hlist_node *tmp;
Herbert Xueb1d1642010-02-27 19:41:45 +00001929
1930 spin_lock_bh(&br->multicast_lock);
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02001931 hlist_for_each_entry_safe(mp, tmp, &br->mdb_list, mdb_node) {
1932 del_timer(&mp->timer);
1933 rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode,
1934 br_mdb_rht_params);
1935 hlist_del_rcu(&mp->mdb_node);
Nikolay Aleksandrov43295962018-12-05 15:14:25 +02001936 kfree_rcu(mp, rcu);
Herbert Xueb1d1642010-02-27 19:41:45 +00001937 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001938 spin_unlock_bh(&br->multicast_lock);
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02001939
Nikolay Aleksandrov43295962018-12-05 15:14:25 +02001940 rcu_barrier();
Herbert Xueb1d1642010-02-27 19:41:45 +00001941}
Herbert Xu0909e112010-02-27 19:41:49 +00001942
1943int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1944{
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02001945 int err = -EINVAL;
Herbert Xu0909e112010-02-27 19:41:49 +00001946
1947 spin_lock_bh(&br->multicast_lock);
Herbert Xu0909e112010-02-27 19:41:49 +00001948
1949 switch (val) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001950 case MDB_RTR_TYPE_DISABLED:
1951 case MDB_RTR_TYPE_PERM:
Yotam Gigi77041422017-10-09 11:15:31 +02001952 br_mc_router_state_change(br, val == MDB_RTR_TYPE_PERM);
Herbert Xu0909e112010-02-27 19:41:49 +00001953 del_timer(&br->multicast_router_timer);
Yotam Gigi77041422017-10-09 11:15:31 +02001954 br->multicast_router = val;
1955 err = 0;
1956 break;
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001957 case MDB_RTR_TYPE_TEMP_QUERY:
Yotam Gigi77041422017-10-09 11:15:31 +02001958 if (br->multicast_router != MDB_RTR_TYPE_TEMP_QUERY)
1959 br_mc_router_state_change(br, false);
Herbert Xu0909e112010-02-27 19:41:49 +00001960 br->multicast_router = val;
1961 err = 0;
1962 break;
Herbert Xu0909e112010-02-27 19:41:49 +00001963 }
1964
Herbert Xu0909e112010-02-27 19:41:49 +00001965 spin_unlock_bh(&br->multicast_lock);
1966
1967 return err;
1968}
1969
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001970static void __del_port_router(struct net_bridge_port *p)
1971{
1972 if (hlist_unhashed(&p->rlist))
1973 return;
1974 hlist_del_init_rcu(&p->rlist);
1975 br_rtr_notify(p->br->dev, p, RTM_DELMDB);
Nogah Frankel6d549642017-02-09 14:54:42 +01001976 br_port_mc_router_state_change(p, false);
Nogah Frankelf12e7d92017-02-09 14:54:41 +01001977
1978 /* don't allow timer refresh */
1979 if (p->multicast_router == MDB_RTR_TYPE_TEMP)
1980 p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001981}
1982
Herbert Xu0909e112010-02-27 19:41:49 +00001983int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1984{
1985 struct net_bridge *br = p->br;
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001986 unsigned long now = jiffies;
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02001987 int err = -EINVAL;
Herbert Xu0909e112010-02-27 19:41:49 +00001988
1989 spin_lock(&br->multicast_lock);
Nikolay Aleksandrov4950cfd2016-02-26 21:20:02 +01001990 if (p->multicast_router == val) {
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001991 /* Refresh the temp router port timer */
1992 if (p->multicast_router == MDB_RTR_TYPE_TEMP)
1993 mod_timer(&p->multicast_router_timer,
1994 now + br->multicast_querier_interval);
Nikolay Aleksandrov4950cfd2016-02-26 21:20:02 +01001995 err = 0;
1996 goto unlock;
1997 }
Herbert Xu0909e112010-02-27 19:41:49 +00001998 switch (val) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001999 case MDB_RTR_TYPE_DISABLED:
2000 p->multicast_router = MDB_RTR_TYPE_DISABLED;
2001 __del_port_router(p);
Herbert Xu0909e112010-02-27 19:41:49 +00002002 del_timer(&p->multicast_router_timer);
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002003 break;
2004 case MDB_RTR_TYPE_TEMP_QUERY:
2005 p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
2006 __del_port_router(p);
2007 break;
2008 case MDB_RTR_TYPE_PERM:
2009 p->multicast_router = MDB_RTR_TYPE_PERM;
2010 del_timer(&p->multicast_router_timer);
Herbert Xu0909e112010-02-27 19:41:49 +00002011 br_multicast_add_router(br, p);
2012 break;
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01002013 case MDB_RTR_TYPE_TEMP:
2014 p->multicast_router = MDB_RTR_TYPE_TEMP;
2015 br_multicast_mark_router(br, p);
2016 break;
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002017 default:
2018 goto unlock;
Herbert Xu0909e112010-02-27 19:41:49 +00002019 }
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002020 err = 0;
2021unlock:
Herbert Xu0909e112010-02-27 19:41:49 +00002022 spin_unlock(&br->multicast_lock);
2023
2024 return err;
2025}
Herbert Xu561f1102010-02-27 19:41:50 +00002026
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002027static void br_multicast_start_querier(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +02002028 struct bridge_mcast_own_query *query)
Herbert Xu561f1102010-02-27 19:41:50 +00002029{
2030 struct net_bridge_port *port;
Herbert Xu74857212012-04-13 02:37:42 +00002031
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002032 __br_multicast_open(br, query);
Herbert Xu74857212012-04-13 02:37:42 +00002033
2034 list_for_each_entry(port, &br->port_list, list) {
2035 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 }
2046}
2047
2048int br_multicast_toggle(struct net_bridge *br, unsigned long val)
2049{
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02002050 struct net_bridge_port *port;
Herbert Xu561f1102010-02-27 19:41:50 +00002051
Andrey Vaginef5e0d82011-11-10 05:48:03 +00002052 spin_lock_bh(&br->multicast_lock);
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +03002053 if (!!br_opt_get(br, BROPT_MULTICAST_ENABLED) == !!val)
Herbert Xu561f1102010-02-27 19:41:50 +00002054 goto unlock;
2055
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +03002056 br_mc_disabled_update(br->dev, val);
2057 br_opt_toggle(br, BROPT_MULTICAST_ENABLED, !!val);
Linus Lüssing4effd282019-01-21 07:26:27 +01002058 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) {
2059 br_multicast_leave_snoopers(br);
Herbert Xu561f1102010-02-27 19:41:50 +00002060 goto unlock;
Linus Lüssing4effd282019-01-21 07:26:27 +01002061 }
Herbert Xu561f1102010-02-27 19:41:50 +00002062
Herbert Xu3a7fda02010-07-29 00:45:30 +00002063 if (!netif_running(br->dev))
2064 goto unlock;
2065
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02002066 br_multicast_open(br);
2067 list_for_each_entry(port, &br->port_list, list)
2068 __br_multicast_enable_port(port);
Herbert Xu561f1102010-02-27 19:41:50 +00002069
2070unlock:
Andrey Vaginef5e0d82011-11-10 05:48:03 +00002071 spin_unlock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00002072
YueHaibinga26d94b2018-12-17 17:46:23 +08002073 return 0;
Herbert Xu561f1102010-02-27 19:41:50 +00002074}
Herbert Xub1951672010-02-27 19:41:51 +00002075
Ido Schimmel9341b982017-05-26 08:37:24 +02002076bool br_multicast_enabled(const struct net_device *dev)
2077{
2078 struct net_bridge *br = netdev_priv(dev);
2079
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +03002080 return !!br_opt_get(br, BROPT_MULTICAST_ENABLED);
Ido Schimmel9341b982017-05-26 08:37:24 +02002081}
2082EXPORT_SYMBOL_GPL(br_multicast_enabled);
2083
Yotam Gigi0912bda2017-10-09 11:15:32 +02002084bool br_multicast_router(const struct net_device *dev)
2085{
2086 struct net_bridge *br = netdev_priv(dev);
2087 bool is_router;
2088
2089 spin_lock_bh(&br->multicast_lock);
2090 is_router = br_multicast_is_router(br);
2091 spin_unlock_bh(&br->multicast_lock);
2092 return is_router;
2093}
2094EXPORT_SYMBOL_GPL(br_multicast_router);
2095
Herbert Xuc5c23262012-04-13 02:37:42 +00002096int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
2097{
Linus Lüssingb00589a2013-08-01 01:06:20 +02002098 unsigned long max_delay;
2099
Herbert Xuc5c23262012-04-13 02:37:42 +00002100 val = !!val;
2101
2102 spin_lock_bh(&br->multicast_lock);
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03002103 if (br_opt_get(br, BROPT_MULTICAST_QUERIER) == val)
Herbert Xuc5c23262012-04-13 02:37:42 +00002104 goto unlock;
2105
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03002106 br_opt_toggle(br, BROPT_MULTICAST_QUERIER, !!val);
Linus Lüssingb00589a2013-08-01 01:06:20 +02002107 if (!val)
2108 goto unlock;
2109
2110 max_delay = br->multicast_query_response_interval;
Linus Lüssingb00589a2013-08-01 01:06:20 +02002111
Linus Lüssing90010b32014-06-07 18:26:26 +02002112 if (!timer_pending(&br->ip4_other_query.timer))
2113 br->ip4_other_query.delay_time = jiffies + max_delay;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002114
Linus Lüssing90010b32014-06-07 18:26:26 +02002115 br_multicast_start_querier(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002116
2117#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02002118 if (!timer_pending(&br->ip6_other_query.timer))
2119 br->ip6_other_query.delay_time = jiffies + max_delay;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002120
Linus Lüssing90010b32014-06-07 18:26:26 +02002121 br_multicast_start_querier(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002122#endif
Herbert Xuc5c23262012-04-13 02:37:42 +00002123
2124unlock:
2125 spin_unlock_bh(&br->multicast_lock);
2126
2127 return 0;
2128}
2129
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01002130int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val)
2131{
2132 /* Currently we support only version 2 and 3 */
2133 switch (val) {
2134 case 2:
2135 case 3:
2136 break;
2137 default:
2138 return -EINVAL;
2139 }
2140
2141 spin_lock_bh(&br->multicast_lock);
2142 br->multicast_igmp_version = val;
2143 spin_unlock_bh(&br->multicast_lock);
2144
2145 return 0;
2146}
2147
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01002148#if IS_ENABLED(CONFIG_IPV6)
2149int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val)
2150{
2151 /* Currently we support version 1 and 2 */
2152 switch (val) {
2153 case 1:
2154 case 2:
2155 break;
2156 default:
2157 return -EINVAL;
2158 }
2159
2160 spin_lock_bh(&br->multicast_lock);
2161 br->multicast_mld_version = val;
2162 spin_unlock_bh(&br->multicast_lock);
2163
2164 return 0;
2165}
2166#endif
2167
Linus Lüssing07f8ac42014-06-07 18:26:28 +02002168/**
2169 * br_multicast_list_adjacent - Returns snooped multicast addresses
2170 * @dev: The bridge port adjacent to which to retrieve addresses
2171 * @br_ip_list: The list to store found, snooped multicast IP addresses in
2172 *
2173 * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
2174 * snooping feature on all bridge ports of dev's bridge device, excluding
2175 * the addresses from dev itself.
2176 *
2177 * Returns the number of items added to br_ip_list.
2178 *
2179 * Notes:
2180 * - br_ip_list needs to be initialized by caller
2181 * - br_ip_list might contain duplicates in the end
2182 * (needs to be taken care of by caller)
2183 * - br_ip_list needs to be freed by caller
2184 */
2185int br_multicast_list_adjacent(struct net_device *dev,
2186 struct list_head *br_ip_list)
2187{
2188 struct net_bridge *br;
2189 struct net_bridge_port *port;
2190 struct net_bridge_port_group *group;
2191 struct br_ip_list *entry;
2192 int count = 0;
2193
2194 rcu_read_lock();
2195 if (!br_ip_list || !br_port_exists(dev))
2196 goto unlock;
2197
2198 port = br_port_get_rcu(dev);
2199 if (!port || !port->br)
2200 goto unlock;
2201
2202 br = port->br;
2203
2204 list_for_each_entry_rcu(port, &br->port_list, list) {
2205 if (!port->dev || port->dev == dev)
2206 continue;
2207
2208 hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
2209 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
2210 if (!entry)
2211 goto unlock;
2212
2213 entry->addr = group->addr;
2214 list_add(&entry->list, br_ip_list);
2215 count++;
2216 }
2217 }
2218
2219unlock:
2220 rcu_read_unlock();
2221 return count;
2222}
2223EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
Linus Lüssing2cd41432014-06-07 18:26:29 +02002224
2225/**
Linus Lüssingc34963e2014-07-07 05:41:17 +02002226 * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2227 * @dev: The bridge port providing the bridge on which to check for a querier
2228 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2229 *
2230 * Checks whether the given interface has a bridge on top and if so returns
2231 * true if a valid querier exists anywhere on the bridged link layer.
2232 * Otherwise returns false.
2233 */
2234bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
2235{
2236 struct net_bridge *br;
2237 struct net_bridge_port *port;
2238 struct ethhdr eth;
2239 bool ret = false;
2240
2241 rcu_read_lock();
2242 if (!br_port_exists(dev))
2243 goto unlock;
2244
2245 port = br_port_get_rcu(dev);
2246 if (!port || !port->br)
2247 goto unlock;
2248
2249 br = port->br;
2250
2251 memset(&eth, 0, sizeof(eth));
2252 eth.h_proto = htons(proto);
2253
2254 ret = br_multicast_querier_exists(br, &eth);
2255
2256unlock:
2257 rcu_read_unlock();
2258 return ret;
2259}
2260EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
2261
2262/**
Linus Lüssing2cd41432014-06-07 18:26:29 +02002263 * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
2264 * @dev: The bridge port adjacent to which to check for a querier
2265 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2266 *
2267 * Checks whether the given interface has a bridge on top and if so returns
2268 * true if a selected querier is behind one of the other ports of this
2269 * bridge. Otherwise returns false.
2270 */
2271bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
2272{
2273 struct net_bridge *br;
2274 struct net_bridge_port *port;
2275 bool ret = false;
2276
2277 rcu_read_lock();
2278 if (!br_port_exists(dev))
2279 goto unlock;
2280
2281 port = br_port_get_rcu(dev);
2282 if (!port || !port->br)
2283 goto unlock;
2284
2285 br = port->br;
2286
2287 switch (proto) {
2288 case ETH_P_IP:
2289 if (!timer_pending(&br->ip4_other_query.timer) ||
2290 rcu_dereference(br->ip4_querier.port) == port)
2291 goto unlock;
2292 break;
Linus Lüssing3993c4e2014-06-12 01:41:24 +02002293#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing2cd41432014-06-07 18:26:29 +02002294 case ETH_P_IPV6:
2295 if (!timer_pending(&br->ip6_other_query.timer) ||
2296 rcu_dereference(br->ip6_querier.port) == port)
2297 goto unlock;
2298 break;
Linus Lüssing3993c4e2014-06-12 01:41:24 +02002299#endif
Linus Lüssing2cd41432014-06-07 18:26:29 +02002300 default:
2301 goto unlock;
2302 }
2303
2304 ret = true;
2305unlock:
2306 rcu_read_unlock();
2307 return ret;
2308}
2309EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002310
2311static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002312 const struct sk_buff *skb, u8 type, u8 dir)
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002313{
2314 struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002315 __be16 proto = skb->protocol;
2316 unsigned int t_len;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002317
2318 u64_stats_update_begin(&pstats->syncp);
2319 switch (proto) {
2320 case htons(ETH_P_IP):
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002321 t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002322 switch (type) {
2323 case IGMP_HOST_MEMBERSHIP_REPORT:
2324 pstats->mstats.igmp_v1reports[dir]++;
2325 break;
2326 case IGMPV2_HOST_MEMBERSHIP_REPORT:
2327 pstats->mstats.igmp_v2reports[dir]++;
2328 break;
2329 case IGMPV3_HOST_MEMBERSHIP_REPORT:
2330 pstats->mstats.igmp_v3reports[dir]++;
2331 break;
2332 case IGMP_HOST_MEMBERSHIP_QUERY:
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002333 if (t_len != sizeof(struct igmphdr)) {
2334 pstats->mstats.igmp_v3queries[dir]++;
2335 } else {
2336 unsigned int offset = skb_transport_offset(skb);
2337 struct igmphdr *ih, _ihdr;
2338
2339 ih = skb_header_pointer(skb, offset,
2340 sizeof(_ihdr), &_ihdr);
2341 if (!ih)
2342 break;
2343 if (!ih->code)
2344 pstats->mstats.igmp_v1queries[dir]++;
2345 else
2346 pstats->mstats.igmp_v2queries[dir]++;
2347 }
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002348 break;
2349 case IGMP_HOST_LEAVE_MESSAGE:
2350 pstats->mstats.igmp_leaves[dir]++;
2351 break;
2352 }
2353 break;
2354#if IS_ENABLED(CONFIG_IPV6)
2355 case htons(ETH_P_IPV6):
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002356 t_len = ntohs(ipv6_hdr(skb)->payload_len) +
2357 sizeof(struct ipv6hdr);
2358 t_len -= skb_network_header_len(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002359 switch (type) {
2360 case ICMPV6_MGM_REPORT:
2361 pstats->mstats.mld_v1reports[dir]++;
2362 break;
2363 case ICMPV6_MLD2_REPORT:
2364 pstats->mstats.mld_v2reports[dir]++;
2365 break;
2366 case ICMPV6_MGM_QUERY:
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002367 if (t_len != sizeof(struct mld_msg))
2368 pstats->mstats.mld_v2queries[dir]++;
2369 else
2370 pstats->mstats.mld_v1queries[dir]++;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002371 break;
2372 case ICMPV6_MGM_REDUCTION:
2373 pstats->mstats.mld_leaves[dir]++;
2374 break;
2375 }
2376 break;
2377#endif /* CONFIG_IPV6 */
2378 }
2379 u64_stats_update_end(&pstats->syncp);
2380}
2381
2382void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002383 const struct sk_buff *skb, u8 type, u8 dir)
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002384{
2385 struct bridge_mcast_stats __percpu *stats;
2386
2387 /* if multicast_disabled is true then igmp type can't be set */
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03002388 if (!type || !br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED))
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002389 return;
2390
2391 if (p)
2392 stats = p->mcast_stats;
2393 else
2394 stats = br->mcast_stats;
2395 if (WARN_ON(!stats))
2396 return;
2397
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002398 br_mcast_stats_add(stats, skb, type, dir);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002399}
2400
2401int br_multicast_init_stats(struct net_bridge *br)
2402{
2403 br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
2404 if (!br->mcast_stats)
2405 return -ENOMEM;
2406
2407 return 0;
2408}
2409
Ido Schimmelb6fe0442017-04-10 14:59:27 +03002410void br_multicast_uninit_stats(struct net_bridge *br)
2411{
2412 free_percpu(br->mcast_stats);
2413}
2414
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002415static void mcast_stats_add_dir(u64 *dst, u64 *src)
2416{
2417 dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
2418 dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
2419}
2420
2421void br_multicast_get_stats(const struct net_bridge *br,
2422 const struct net_bridge_port *p,
2423 struct br_mcast_stats *dest)
2424{
2425 struct bridge_mcast_stats __percpu *stats;
2426 struct br_mcast_stats tdst;
2427 int i;
2428
2429 memset(dest, 0, sizeof(*dest));
2430 if (p)
2431 stats = p->mcast_stats;
2432 else
2433 stats = br->mcast_stats;
2434 if (WARN_ON(!stats))
2435 return;
2436
2437 memset(&tdst, 0, sizeof(tdst));
2438 for_each_possible_cpu(i) {
2439 struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
2440 struct br_mcast_stats temp;
2441 unsigned int start;
2442
2443 do {
2444 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
2445 memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
2446 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
2447
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002448 mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
2449 mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
2450 mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002451 mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
2452 mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
2453 mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
2454 mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
2455 tdst.igmp_parse_errors += temp.igmp_parse_errors;
2456
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002457 mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
2458 mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002459 mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
2460 mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
2461 mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
2462 tdst.mld_parse_errors += temp.mld_parse_errors;
2463 }
2464 memcpy(dest, &tdst, sizeof(*dest));
2465}
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02002466
2467int br_mdb_hash_init(struct net_bridge *br)
2468{
2469 return rhashtable_init(&br->mdb_hash_tbl, &br_mdb_rht_params);
2470}
2471
2472void br_mdb_hash_fini(struct net_bridge *br)
2473{
2474 rhashtable_destroy(&br->mdb_hash_tbl);
2475}