blob: faa7261a992fa6df54afd4656b0e810102d59c30 [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>
17#include <linux/jhash.h>
18#include <linux/kernel.h>
Herbert Xub1951672010-02-27 19:41:51 +000019#include <linux/log2.h>
Herbert Xueb1d1642010-02-27 19:41:45 +000020#include <linux/netdevice.h>
21#include <linux/netfilter_bridge.h>
22#include <linux/random.h>
23#include <linux/rculist.h>
24#include <linux/skbuff.h>
25#include <linux/slab.h>
26#include <linux/timer.h>
Cong Wang1c8ad5b2013-05-21 21:52:54 +000027#include <linux/inetdevice.h>
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +010028#include <linux/mroute.h>
Herbert Xueb1d1642010-02-27 19:41:45 +000029#include <net/ip.h>
Nogah Frankel147c1e92017-02-09 14:54:40 +010030#include <net/switchdev.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000031#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090032#include <net/ipv6.h>
33#include <net/mld.h>
David S. Millerd4c4f072010-04-27 10:16:54 -070034#include <net/ip6_checksum.h>
Linus Lüssing3c3769e2013-09-04 02:13:39 +020035#include <net/addrconf.h>
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090036#endif
Herbert Xueb1d1642010-02-27 19:41:45 +000037
38#include "br_private.h"
39
Linus Lüssingcc0fdd82013-08-30 17:28:17 +020040static void br_multicast_start_querier(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +020041 struct bridge_mcast_own_query *query);
Satish Ashok754bc542015-06-19 01:22:57 -070042static void br_multicast_add_router(struct net_bridge *br,
43 struct net_bridge_port *port);
Satish Ashokbc8c20a2015-07-13 05:28:37 -070044static void br_ip4_multicast_leave_group(struct net_bridge *br,
45 struct net_bridge_port *port,
46 __be32 group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +010047 __u16 vid,
48 const unsigned char *src);
49
Nogah Frankelf12e7d92017-02-09 14:54:41 +010050static void __del_port_router(struct net_bridge_port *p);
Satish Ashokbc8c20a2015-07-13 05:28:37 -070051#if IS_ENABLED(CONFIG_IPV6)
52static void br_ip6_multicast_leave_group(struct net_bridge *br,
53 struct net_bridge_port *port,
54 const struct in6_addr *group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +010055 __u16 vid, const unsigned char *src);
Satish Ashokbc8c20a2015-07-13 05:28:37 -070056#endif
Cong Wang2ce297f2012-12-10 02:15:35 +000057unsigned int br_mdb_rehash_seq;
Herbert Xuc83b8fa2012-04-13 02:37:42 +000058
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090059static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
60{
61 if (a->proto != b->proto)
62 return 0;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000063 if (a->vid != b->vid)
64 return 0;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090065 switch (a->proto) {
66 case htons(ETH_P_IP):
67 return a->u.ip4 == b->u.ip4;
Eric Dumazetdfd56b82011-12-10 09:48:31 +000068#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090069 case htons(ETH_P_IPV6):
70 return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
71#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090072 }
73 return 0;
74}
75
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000076static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip,
77 __u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +000078{
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000079 return jhash_2words((__force u32)ip, vid, mdb->secret) & (mdb->max - 1);
Herbert Xueb1d1642010-02-27 19:41:45 +000080}
81
Eric Dumazetdfd56b82011-12-10 09:48:31 +000082#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090083static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000084 const struct in6_addr *ip,
85 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090086{
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000087 return jhash_2words(ipv6_addr_hash(ip), vid,
88 mdb->secret) & (mdb->max - 1);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090089}
90#endif
91
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090092static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb,
93 struct br_ip *ip)
94{
95 switch (ip->proto) {
96 case htons(ETH_P_IP):
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000097 return __br_ip4_hash(mdb, ip->u.ip4, ip->vid);
Eric Dumazetdfd56b82011-12-10 09:48:31 +000098#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090099 case htons(ETH_P_IPV6):
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000100 return __br_ip6_hash(mdb, &ip->u.ip6, ip->vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900101#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900102 }
103 return 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000104}
105
106static struct net_bridge_mdb_entry *__br_mdb_ip_get(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900107 struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +0000108{
109 struct net_bridge_mdb_entry *mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000110
Sasha Levinb67bfe02013-02-27 17:06:00 -0800111 hlist_for_each_entry_rcu(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900112 if (br_ip_equal(&mp->addr, dst))
Herbert Xueb1d1642010-02-27 19:41:45 +0000113 return mp;
114 }
115
116 return NULL;
117}
118
Cong Wangcfd56752012-12-11 22:23:08 +0000119struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge_mdb_htable *mdb,
120 struct br_ip *dst)
Herbert Xu7f285fa2010-07-05 14:50:08 +0000121{
122 if (!mdb)
123 return NULL;
124
125 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
126}
127
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900128static struct net_bridge_mdb_entry *br_mdb_ip4_get(
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000129 struct net_bridge_mdb_htable *mdb, __be32 dst, __u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000130{
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900131 struct br_ip br_dst;
Herbert Xu0821ec52010-03-15 20:38:25 -0700132
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900133 br_dst.u.ip4 = dst;
134 br_dst.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000135 br_dst.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900136
Herbert Xu7f285fa2010-07-05 14:50:08 +0000137 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900138}
139
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000140#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900141static struct net_bridge_mdb_entry *br_mdb_ip6_get(
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000142 struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst,
143 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900144{
145 struct br_ip br_dst;
146
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000147 br_dst.u.ip6 = *dst;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900148 br_dst.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000149 br_dst.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900150
Herbert Xu7f285fa2010-07-05 14:50:08 +0000151 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900152}
153#endif
154
Herbert Xueb1d1642010-02-27 19:41:45 +0000155struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
Cong Wangfbca58a2013-03-07 03:05:33 +0000156 struct sk_buff *skb, u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000157{
Eric Dumazete8051682010-11-15 06:38:10 +0000158 struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900159 struct br_ip ip;
160
Herbert Xu7f285fa2010-07-05 14:50:08 +0000161 if (br->multicast_disabled)
Herbert Xueb1d1642010-02-27 19:41:45 +0000162 return NULL;
163
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900164 if (BR_INPUT_SKB_CB(skb)->igmp)
165 return NULL;
166
167 ip.proto = skb->protocol;
Cong Wangfbca58a2013-03-07 03:05:33 +0000168 ip.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900169
Herbert Xueb1d1642010-02-27 19:41:45 +0000170 switch (skb->protocol) {
171 case htons(ETH_P_IP):
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900172 ip.u.ip4 = ip_hdr(skb)->daddr;
173 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000174#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900175 case htons(ETH_P_IPV6):
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000176 ip.u.ip6 = ipv6_hdr(skb)->daddr;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900177 break;
178#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900179 default:
180 return NULL;
Herbert Xueb1d1642010-02-27 19:41:45 +0000181 }
182
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900183 return br_mdb_ip_get(mdb, &ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000184}
185
186static void br_mdb_free(struct rcu_head *head)
187{
188 struct net_bridge_mdb_htable *mdb =
189 container_of(head, struct net_bridge_mdb_htable, rcu);
190 struct net_bridge_mdb_htable *old = mdb->old;
191
192 mdb->old = NULL;
193 kfree(old->mhash);
194 kfree(old);
195}
196
197static int br_mdb_copy(struct net_bridge_mdb_htable *new,
198 struct net_bridge_mdb_htable *old,
199 int elasticity)
200{
201 struct net_bridge_mdb_entry *mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000202 int maxlen;
203 int len;
204 int i;
205
206 for (i = 0; i < old->max; i++)
Sasha Levinb67bfe02013-02-27 17:06:00 -0800207 hlist_for_each_entry(mp, &old->mhash[i], hlist[old->ver])
Herbert Xueb1d1642010-02-27 19:41:45 +0000208 hlist_add_head(&mp->hlist[new->ver],
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900209 &new->mhash[br_ip_hash(new, &mp->addr)]);
Herbert Xueb1d1642010-02-27 19:41:45 +0000210
211 if (!elasticity)
212 return 0;
213
214 maxlen = 0;
215 for (i = 0; i < new->max; i++) {
216 len = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800217 hlist_for_each_entry(mp, &new->mhash[i], hlist[new->ver])
Herbert Xueb1d1642010-02-27 19:41:45 +0000218 len++;
219 if (len > maxlen)
220 maxlen = len;
221 }
222
223 return maxlen > elasticity ? -EINVAL : 0;
224}
225
Cong Wangcfd56752012-12-11 22:23:08 +0000226void br_multicast_free_pg(struct rcu_head *head)
Herbert Xueb1d1642010-02-27 19:41:45 +0000227{
228 struct net_bridge_port_group *p =
229 container_of(head, struct net_bridge_port_group, rcu);
230
231 kfree(p);
232}
233
234static void br_multicast_free_group(struct rcu_head *head)
235{
236 struct net_bridge_mdb_entry *mp =
237 container_of(head, struct net_bridge_mdb_entry, rcu);
238
239 kfree(mp);
240}
241
242static void br_multicast_group_expired(unsigned long data)
243{
244 struct net_bridge_mdb_entry *mp = (void *)data;
245 struct net_bridge *br = mp->br;
246 struct net_bridge_mdb_htable *mdb;
247
248 spin_lock(&br->multicast_lock);
249 if (!netif_running(br->dev) || timer_pending(&mp->timer))
250 goto out;
251
Herbert Xu8a870172011-02-12 01:05:42 -0800252 mp->mglist = false;
Herbert Xueb1d1642010-02-27 19:41:45 +0000253
254 if (mp->ports)
255 goto out;
256
Eric Dumazete8051682010-11-15 06:38:10 +0000257 mdb = mlock_dereference(br->mdb, br);
258
Herbert Xueb1d1642010-02-27 19:41:45 +0000259 hlist_del_rcu(&mp->hlist[mdb->ver]);
260 mdb->size--;
261
Herbert Xueb1d1642010-02-27 19:41:45 +0000262 call_rcu_bh(&mp->rcu, br_multicast_free_group);
263
264out:
265 spin_unlock(&br->multicast_lock);
266}
267
268static void br_multicast_del_pg(struct net_bridge *br,
269 struct net_bridge_port_group *pg)
270{
Eric Dumazete8051682010-11-15 06:38:10 +0000271 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000272 struct net_bridge_mdb_entry *mp;
273 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +0000274 struct net_bridge_port_group __rcu **pp;
275
276 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +0000277
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900278 mp = br_mdb_ip_get(mdb, &pg->addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000279 if (WARN_ON(!mp))
280 return;
281
Eric Dumazete8051682010-11-15 06:38:10 +0000282 for (pp = &mp->ports;
283 (p = mlock_dereference(*pp, br)) != NULL;
284 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000285 if (p != pg)
286 continue;
287
stephen hemminger83f6a742010-04-27 15:01:06 +0000288 rcu_assign_pointer(*pp, p->next);
Herbert Xueb1d1642010-02-27 19:41:45 +0000289 hlist_del_init(&p->mglist);
290 del_timer(&p->timer);
Elad Raz45ebcce2016-04-21 12:52:45 +0200291 br_mdb_notify(br->dev, p->port, &pg->addr, RTM_DELMDB,
292 p->flags);
Herbert Xueb1d1642010-02-27 19:41:45 +0000293 call_rcu_bh(&p->rcu, br_multicast_free_pg);
294
Linus Lüssing454594f2013-10-20 00:58:57 +0200295 if (!mp->ports && !mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +0000296 netif_running(br->dev))
297 mod_timer(&mp->timer, jiffies);
298
299 return;
300 }
301
302 WARN_ON(1);
303}
304
305static void br_multicast_port_group_expired(unsigned long data)
306{
307 struct net_bridge_port_group *pg = (void *)data;
308 struct net_bridge *br = pg->port->br;
309
310 spin_lock(&br->multicast_lock);
311 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
Elad Raz9d06b6d2016-02-03 09:57:05 +0100312 hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT)
Herbert Xueb1d1642010-02-27 19:41:45 +0000313 goto out;
314
315 br_multicast_del_pg(br, pg);
316
317out:
318 spin_unlock(&br->multicast_lock);
319}
320
Eric Dumazete8051682010-11-15 06:38:10 +0000321static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max,
Herbert Xueb1d1642010-02-27 19:41:45 +0000322 int elasticity)
323{
Eric Dumazete8051682010-11-15 06:38:10 +0000324 struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000325 struct net_bridge_mdb_htable *mdb;
326 int err;
327
328 mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
329 if (!mdb)
330 return -ENOMEM;
331
332 mdb->max = max;
333 mdb->old = old;
334
335 mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
336 if (!mdb->mhash) {
337 kfree(mdb);
338 return -ENOMEM;
339 }
340
341 mdb->size = old ? old->size : 0;
342 mdb->ver = old ? old->ver ^ 1 : 0;
343
344 if (!old || elasticity)
345 get_random_bytes(&mdb->secret, sizeof(mdb->secret));
346 else
347 mdb->secret = old->secret;
348
349 if (!old)
350 goto out;
351
352 err = br_mdb_copy(mdb, old, elasticity);
353 if (err) {
354 kfree(mdb->mhash);
355 kfree(mdb);
356 return err;
357 }
358
Cong Wang2ce297f2012-12-10 02:15:35 +0000359 br_mdb_rehash_seq++;
Herbert Xueb1d1642010-02-27 19:41:45 +0000360 call_rcu_bh(&mdb->rcu, br_mdb_free);
361
362out:
363 rcu_assign_pointer(*mdbp, mdb);
364
365 return 0;
366}
367
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900368static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200369 __be32 group,
370 u8 *igmp_type)
Herbert Xueb1d1642010-02-27 19:41:45 +0000371{
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100372 struct igmpv3_query *ihv3;
373 size_t igmp_hdr_size;
Herbert Xueb1d1642010-02-27 19:41:45 +0000374 struct sk_buff *skb;
375 struct igmphdr *ih;
376 struct ethhdr *eth;
377 struct iphdr *iph;
378
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100379 igmp_hdr_size = sizeof(*ih);
380 if (br->multicast_igmp_version == 3)
381 igmp_hdr_size = sizeof(*ihv3);
Herbert Xueb1d1642010-02-27 19:41:45 +0000382 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100383 igmp_hdr_size + 4);
Herbert Xueb1d1642010-02-27 19:41:45 +0000384 if (!skb)
385 goto out;
386
387 skb->protocol = htons(ETH_P_IP);
388
389 skb_reset_mac_header(skb);
390 eth = eth_hdr(skb);
391
Joe Perchese5a727f2014-02-23 00:05:25 -0800392 ether_addr_copy(eth->h_source, br->dev->dev_addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000393 eth->h_dest[0] = 1;
394 eth->h_dest[1] = 0;
395 eth->h_dest[2] = 0x5e;
396 eth->h_dest[3] = 0;
397 eth->h_dest[4] = 0;
398 eth->h_dest[5] = 1;
399 eth->h_proto = htons(ETH_P_IP);
400 skb_put(skb, sizeof(*eth));
401
402 skb_set_network_header(skb, skb->len);
403 iph = ip_hdr(skb);
404
405 iph->version = 4;
406 iph->ihl = 6;
407 iph->tos = 0xc0;
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100408 iph->tot_len = htons(sizeof(*iph) + igmp_hdr_size + 4);
Herbert Xueb1d1642010-02-27 19:41:45 +0000409 iph->id = 0;
410 iph->frag_off = htons(IP_DF);
411 iph->ttl = 1;
412 iph->protocol = IPPROTO_IGMP;
Cong Wang1c8ad5b2013-05-21 21:52:54 +0000413 iph->saddr = br->multicast_query_use_ifaddr ?
414 inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000415 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
416 ((u8 *)&iph[1])[0] = IPOPT_RA;
417 ((u8 *)&iph[1])[1] = 4;
418 ((u8 *)&iph[1])[2] = 0;
419 ((u8 *)&iph[1])[3] = 0;
420 ip_send_check(iph);
421 skb_put(skb, 24);
422
423 skb_set_transport_header(skb, skb->len);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200424 *igmp_type = IGMP_HOST_MEMBERSHIP_QUERY;
Herbert Xueb1d1642010-02-27 19:41:45 +0000425
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100426 switch (br->multicast_igmp_version) {
427 case 2:
428 ih = igmp_hdr(skb);
429 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
430 ih->code = (group ? br->multicast_last_member_interval :
431 br->multicast_query_response_interval) /
432 (HZ / IGMP_TIMER_SCALE);
433 ih->group = group;
434 ih->csum = 0;
435 ih->csum = ip_compute_csum((void *)ih, sizeof(*ih));
436 break;
437 case 3:
438 ihv3 = igmpv3_query_hdr(skb);
439 ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY;
440 ihv3->code = (group ? br->multicast_last_member_interval :
441 br->multicast_query_response_interval) /
442 (HZ / IGMP_TIMER_SCALE);
443 ihv3->group = group;
444 ihv3->qqic = br->multicast_query_interval / HZ;
445 ihv3->nsrcs = 0;
446 ihv3->resv = 0;
447 ihv3->suppress = 0;
448 ihv3->qrv = 2;
449 ihv3->csum = 0;
450 ihv3->csum = ip_compute_csum((void *)ihv3, sizeof(*ihv3));
451 break;
452 }
453
454 skb_put(skb, igmp_hdr_size);
Herbert Xueb1d1642010-02-27 19:41:45 +0000455 __skb_pull(skb, sizeof(*eth));
456
457out:
458 return skb;
459}
460
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000461#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900462static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200463 const struct in6_addr *grp,
464 u8 *igmp_type)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900465{
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100466 struct mld2_query *mld2q;
467 unsigned long interval;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900468 struct ipv6hdr *ip6h;
469 struct mld_msg *mldq;
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100470 size_t mld_hdr_size;
471 struct sk_buff *skb;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900472 struct ethhdr *eth;
473 u8 *hopopt;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900474
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100475 mld_hdr_size = sizeof(*mldq);
476 if (br->multicast_mld_version == 2)
477 mld_hdr_size = sizeof(*mld2q);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900478 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100479 8 + mld_hdr_size);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900480 if (!skb)
481 goto out;
482
483 skb->protocol = htons(ETH_P_IPV6);
484
485 /* Ethernet header */
486 skb_reset_mac_header(skb);
487 eth = eth_hdr(skb);
488
Joe Perchese5a727f2014-02-23 00:05:25 -0800489 ether_addr_copy(eth->h_source, br->dev->dev_addr);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900490 eth->h_proto = htons(ETH_P_IPV6);
491 skb_put(skb, sizeof(*eth));
492
493 /* IPv6 header + HbH option */
494 skb_set_network_header(skb, skb->len);
495 ip6h = ipv6_hdr(skb);
496
497 *(__force __be32 *)ip6h = htonl(0x60000000);
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100498 ip6h->payload_len = htons(8 + mld_hdr_size);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900499 ip6h->nexthdr = IPPROTO_HOPOPTS;
500 ip6h->hop_limit = 1;
Linus Lüssinga7bff752011-03-22 11:40:32 +0000501 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
Ulrich Weberd1d81d42012-03-05 04:52:44 +0000502 if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
503 &ip6h->saddr)) {
504 kfree_skb(skb);
daniel0888d5f2016-06-24 12:35:18 +0200505 br->has_ipv6_addr = 0;
Ulrich Weberd1d81d42012-03-05 04:52:44 +0000506 return NULL;
507 }
daniel0888d5f2016-06-24 12:35:18 +0200508
509 br->has_ipv6_addr = 1;
Linus Lüssing36cff5a2011-02-17 08:17:51 +0000510 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900511
512 hopopt = (u8 *)(ip6h + 1);
513 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
514 hopopt[1] = 0; /* length of HbH */
515 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
516 hopopt[3] = 2; /* Length of RA Option */
517 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
518 hopopt[5] = 0;
Eldad Zack1de5a712012-05-17 06:00:25 +0000519 hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */
520 hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900521
522 skb_put(skb, sizeof(*ip6h) + 8);
523
524 /* ICMPv6 */
525 skb_set_transport_header(skb, skb->len);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200526 interval = ipv6_addr_any(grp) ?
Linus Lüssing32de8682013-06-16 23:20:34 +0200527 br->multicast_query_response_interval :
528 br->multicast_last_member_interval;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200529 *igmp_type = ICMPV6_MGM_QUERY;
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100530 switch (br->multicast_mld_version) {
531 case 1:
532 mldq = (struct mld_msg *)icmp6_hdr(skb);
533 mldq->mld_type = ICMPV6_MGM_QUERY;
534 mldq->mld_code = 0;
535 mldq->mld_cksum = 0;
536 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
537 mldq->mld_reserved = 0;
538 mldq->mld_mca = *grp;
539 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
540 sizeof(*mldq), IPPROTO_ICMPV6,
541 csum_partial(mldq,
542 sizeof(*mldq),
543 0));
544 break;
545 case 2:
546 mld2q = (struct mld2_query *)icmp6_hdr(skb);
Lance Richardson53631a52017-01-16 18:11:35 -0500547 mld2q->mld2q_mrc = htons((u16)jiffies_to_msecs(interval));
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100548 mld2q->mld2q_type = ICMPV6_MGM_QUERY;
549 mld2q->mld2q_code = 0;
550 mld2q->mld2q_cksum = 0;
551 mld2q->mld2q_resv1 = 0;
552 mld2q->mld2q_resv2 = 0;
553 mld2q->mld2q_suppress = 0;
554 mld2q->mld2q_qrv = 2;
555 mld2q->mld2q_nsrcs = 0;
556 mld2q->mld2q_qqic = br->multicast_query_interval / HZ;
557 mld2q->mld2q_mca = *grp;
558 mld2q->mld2q_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
559 sizeof(*mld2q),
560 IPPROTO_ICMPV6,
561 csum_partial(mld2q,
562 sizeof(*mld2q),
563 0));
564 break;
565 }
566 skb_put(skb, mld_hdr_size);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900567
568 __skb_pull(skb, sizeof(*eth));
569
570out:
571 return skb;
572}
573#endif
574
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900575static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200576 struct br_ip *addr,
577 u8 *igmp_type)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900578{
579 switch (addr->proto) {
580 case htons(ETH_P_IP):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200581 return br_ip4_multicast_alloc_query(br, addr->u.ip4, igmp_type);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000582#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900583 case htons(ETH_P_IPV6):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200584 return br_ip6_multicast_alloc_query(br, &addr->u.ip6,
585 igmp_type);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900586#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900587 }
588 return NULL;
589}
590
Herbert Xueb1d1642010-02-27 19:41:45 +0000591static struct net_bridge_mdb_entry *br_multicast_get_group(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900592 struct net_bridge *br, struct net_bridge_port *port,
593 struct br_ip *group, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +0000594{
Eric Dumazete8051682010-11-15 06:38:10 +0000595 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000596 struct net_bridge_mdb_entry *mp;
Eric Dumazet95c96172012-04-15 05:58:06 +0000597 unsigned int count = 0;
598 unsigned int max;
Herbert Xueb1d1642010-02-27 19:41:45 +0000599 int elasticity;
600 int err;
601
Eric Dumazete8051682010-11-15 06:38:10 +0000602 mdb = rcu_dereference_protected(br->mdb, 1);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800603 hlist_for_each_entry(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000604 count++;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900605 if (unlikely(br_ip_equal(group, &mp->addr)))
Herbert Xueb1d1642010-02-27 19:41:45 +0000606 return mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000607 }
608
609 elasticity = 0;
610 max = mdb->max;
611
612 if (unlikely(count > br->hash_elasticity && count)) {
613 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000614 br_info(br, "Multicast hash table "
615 "chain limit reached: %s\n",
616 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000617
618 elasticity = br->hash_elasticity;
619 }
620
621 if (mdb->size >= max) {
622 max *= 2;
Thomas Graf036be6d2012-07-10 22:29:19 +0000623 if (unlikely(max > br->hash_max)) {
624 br_warn(br, "Multicast hash table maximum of %d "
625 "reached, disabling snooping: %s\n",
626 br->hash_max,
627 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000628 err = -E2BIG;
629disable:
630 br->multicast_disabled = 1;
631 goto err;
632 }
633 }
634
635 if (max > mdb->max || elasticity) {
636 if (mdb->old) {
637 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000638 br_info(br, "Multicast hash table "
639 "on fire: %s\n",
640 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000641 err = -EEXIST;
642 goto err;
643 }
644
645 err = br_mdb_rehash(&br->mdb, max, elasticity);
646 if (err) {
stephen hemminger28a16c92010-05-10 09:31:09 +0000647 br_warn(br, "Cannot rehash multicast "
648 "hash table, disabling snooping: %s, %d, %d\n",
649 port ? port->dev->name : br->dev->name,
650 mdb->size, err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000651 goto disable;
652 }
653
654 err = -EAGAIN;
655 goto err;
656 }
657
658 return NULL;
659
660err:
661 mp = ERR_PTR(err);
662 return mp;
663}
664
Cong Wangcfd56752012-12-11 22:23:08 +0000665struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100666 struct net_bridge_port *p,
667 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000668{
Eric Dumazete8051682010-11-15 06:38:10 +0000669 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000670 struct net_bridge_mdb_entry *mp;
671 int hash;
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000672 int err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000673
Eric Dumazete8051682010-11-15 06:38:10 +0000674 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000675 if (!mdb) {
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000676 err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0);
677 if (err)
678 return ERR_PTR(err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000679 goto rehash;
680 }
681
682 hash = br_ip_hash(mdb, group);
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100683 mp = br_multicast_get_group(br, p, group, hash);
Herbert Xueb1d1642010-02-27 19:41:45 +0000684 switch (PTR_ERR(mp)) {
685 case 0:
686 break;
687
688 case -EAGAIN:
689rehash:
Eric Dumazete8051682010-11-15 06:38:10 +0000690 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000691 hash = br_ip_hash(mdb, group);
692 break;
693
694 default:
695 goto out;
696 }
697
698 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
699 if (unlikely(!mp))
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000700 return ERR_PTR(-ENOMEM);
Herbert Xueb1d1642010-02-27 19:41:45 +0000701
702 mp->br = br;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900703 mp->addr = *group;
Eric Dumazet1faabf22013-07-19 20:07:16 -0700704 setup_timer(&mp->timer, br_multicast_group_expired,
705 (unsigned long)mp);
706
Herbert Xueb1d1642010-02-27 19:41:45 +0000707 hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
708 mdb->size++;
709
710out:
711 return mp;
712}
713
Cong Wangcfd56752012-12-11 22:23:08 +0000714struct net_bridge_port_group *br_multicast_new_port_group(
715 struct net_bridge_port *port,
716 struct br_ip *group,
Amerigo Wangccb1c312012-12-14 22:09:51 +0000717 struct net_bridge_port_group __rcu *next,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100718 unsigned char flags,
719 const unsigned char *src)
Cong Wangcfd56752012-12-11 22:23:08 +0000720{
721 struct net_bridge_port_group *p;
722
723 p = kzalloc(sizeof(*p), GFP_ATOMIC);
724 if (unlikely(!p))
725 return NULL;
726
727 p->addr = *group;
728 p->port = port;
Elad Raz9d06b6d2016-02-03 09:57:05 +0100729 p->flags = flags;
stephen hemmingereca2a432012-12-13 06:51:28 +0000730 rcu_assign_pointer(p->next, next);
Cong Wangcfd56752012-12-11 22:23:08 +0000731 hlist_add_head(&p->mglist, &port->mglist);
732 setup_timer(&p->timer, br_multicast_port_group_expired,
733 (unsigned long)p);
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100734
735 if (src)
736 memcpy(p->eth_addr, src, ETH_ALEN);
737 else
738 memset(p->eth_addr, 0xff, ETH_ALEN);
739
Cong Wangcfd56752012-12-11 22:23:08 +0000740 return p;
741}
742
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100743static bool br_port_group_equal(struct net_bridge_port_group *p,
744 struct net_bridge_port *port,
745 const unsigned char *src)
746{
747 if (p->port != port)
748 return false;
749
750 if (!(port->flags & BR_MULTICAST_TO_UNICAST))
751 return true;
752
753 return ether_addr_equal(src, p->eth_addr);
754}
755
Herbert Xueb1d1642010-02-27 19:41:45 +0000756static int br_multicast_add_group(struct net_bridge *br,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900757 struct net_bridge_port *port,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100758 struct br_ip *group,
759 const unsigned char *src)
Herbert Xueb1d1642010-02-27 19:41:45 +0000760{
Eric Dumazete8051682010-11-15 06:38:10 +0000761 struct net_bridge_port_group __rcu **pp;
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100762 struct net_bridge_port_group *p;
763 struct net_bridge_mdb_entry *mp;
Linus Lüssing454594f2013-10-20 00:58:57 +0200764 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +0000765 int err;
766
Herbert Xueb1d1642010-02-27 19:41:45 +0000767 spin_lock(&br->multicast_lock);
768 if (!netif_running(br->dev) ||
769 (port && port->state == BR_STATE_DISABLED))
770 goto out;
771
772 mp = br_multicast_new_group(br, port, group);
773 err = PTR_ERR(mp);
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000774 if (IS_ERR(mp))
Herbert Xueb1d1642010-02-27 19:41:45 +0000775 goto err;
776
777 if (!port) {
Herbert Xu8a870172011-02-12 01:05:42 -0800778 mp->mglist = true;
Linus Lüssing454594f2013-10-20 00:58:57 +0200779 mod_timer(&mp->timer, now + br->multicast_membership_interval);
Herbert Xueb1d1642010-02-27 19:41:45 +0000780 goto out;
781 }
782
Eric Dumazete8051682010-11-15 06:38:10 +0000783 for (pp = &mp->ports;
784 (p = mlock_dereference(*pp, br)) != NULL;
785 pp = &p->next) {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100786 if (br_port_group_equal(p, port, src))
Linus Lüssing454594f2013-10-20 00:58:57 +0200787 goto found;
Herbert Xueb1d1642010-02-27 19:41:45 +0000788 if ((unsigned long)p->port < (unsigned long)port)
789 break;
790 }
791
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100792 p = br_multicast_new_port_group(port, group, *pp, 0, src);
Herbert Xueb1d1642010-02-27 19:41:45 +0000793 if (unlikely(!p))
794 goto err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000795 rcu_assign_pointer(*pp, p);
Elad Raz45ebcce2016-04-21 12:52:45 +0200796 br_mdb_notify(br->dev, port, group, RTM_NEWMDB, 0);
Herbert Xueb1d1642010-02-27 19:41:45 +0000797
Linus Lüssing454594f2013-10-20 00:58:57 +0200798found:
799 mod_timer(&p->timer, now + br->multicast_membership_interval);
Herbert Xueb1d1642010-02-27 19:41:45 +0000800out:
801 err = 0;
802
803err:
804 spin_unlock(&br->multicast_lock);
805 return err;
806}
807
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900808static int br_ip4_multicast_add_group(struct net_bridge *br,
809 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000810 __be32 group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100811 __u16 vid,
812 const unsigned char *src)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900813{
814 struct br_ip br_group;
815
816 if (ipv4_is_local_multicast(group))
817 return 0;
818
819 br_group.u.ip4 = group;
820 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000821 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900822
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100823 return br_multicast_add_group(br, port, &br_group, src);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900824}
825
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000826#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900827static int br_ip6_multicast_add_group(struct net_bridge *br,
828 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000829 const struct in6_addr *group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100830 __u16 vid,
831 const unsigned char *src)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900832{
833 struct br_ip br_group;
834
Linus Lüssing3c3769e2013-09-04 02:13:39 +0200835 if (ipv6_addr_is_ll_all_nodes(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900836 return 0;
837
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000838 br_group.u.ip6 = *group;
Linus Lüssing9cc6e0c2011-02-15 13:19:17 +0000839 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000840 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900841
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100842 return br_multicast_add_group(br, port, &br_group, src);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900843}
844#endif
845
Herbert Xueb1d1642010-02-27 19:41:45 +0000846static void br_multicast_router_expired(unsigned long data)
847{
848 struct net_bridge_port *port = (void *)data;
849 struct net_bridge *br = port->br;
850
851 spin_lock(&br->multicast_lock);
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +0100852 if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
853 port->multicast_router == MDB_RTR_TYPE_PERM ||
Nogah Frankelf12e7d92017-02-09 14:54:41 +0100854 timer_pending(&port->multicast_router_timer))
Herbert Xueb1d1642010-02-27 19:41:45 +0000855 goto out;
856
Nogah Frankelf12e7d92017-02-09 14:54:41 +0100857 __del_port_router(port);
Herbert Xueb1d1642010-02-27 19:41:45 +0000858out:
859 spin_unlock(&br->multicast_lock);
860}
861
862static void br_multicast_local_router_expired(unsigned long data)
863{
864}
865
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200866static void br_multicast_querier_expired(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +0200867 struct bridge_mcast_own_query *query)
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000868{
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000869 spin_lock(&br->multicast_lock);
870 if (!netif_running(br->dev) || br->multicast_disabled)
871 goto out;
872
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200873 br_multicast_start_querier(br, query);
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000874
875out:
876 spin_unlock(&br->multicast_lock);
877}
878
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200879static void br_ip4_multicast_querier_expired(unsigned long data)
880{
881 struct net_bridge *br = (void *)data;
882
Linus Lüssing90010b32014-06-07 18:26:26 +0200883 br_multicast_querier_expired(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200884}
885
886#if IS_ENABLED(CONFIG_IPV6)
887static void br_ip6_multicast_querier_expired(unsigned long data)
888{
889 struct net_bridge *br = (void *)data;
890
Linus Lüssing90010b32014-06-07 18:26:26 +0200891 br_multicast_querier_expired(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200892}
893#endif
894
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200895static void br_multicast_select_own_querier(struct net_bridge *br,
896 struct br_ip *ip,
897 struct sk_buff *skb)
898{
899 if (ip->proto == htons(ETH_P_IP))
900 br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr;
901#if IS_ENABLED(CONFIG_IPV6)
902 else
903 br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr;
904#endif
905}
906
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900907static void __br_multicast_send_query(struct net_bridge *br,
908 struct net_bridge_port *port,
909 struct br_ip *ip)
Herbert Xueb1d1642010-02-27 19:41:45 +0000910{
Herbert Xueb1d1642010-02-27 19:41:45 +0000911 struct sk_buff *skb;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200912 u8 igmp_type;
Herbert Xueb1d1642010-02-27 19:41:45 +0000913
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200914 skb = br_multicast_alloc_query(br, ip, &igmp_type);
Herbert Xueb1d1642010-02-27 19:41:45 +0000915 if (!skb)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900916 return;
Herbert Xueb1d1642010-02-27 19:41:45 +0000917
918 if (port) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000919 skb->dev = port->dev;
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -0700920 br_multicast_count(br, port, skb, igmp_type,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200921 BR_MCAST_DIR_TX);
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500922 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
923 dev_net(port->dev), NULL, skb, NULL, skb->dev,
Linus Lüssingf0b4eec2014-11-17 12:20:28 +0100924 br_dev_queue_push_xmit);
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200925 } else {
926 br_multicast_select_own_querier(br, ip, skb);
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -0700927 br_multicast_count(br, port, skb, igmp_type,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200928 BR_MCAST_DIR_RX);
Herbert Xueb1d1642010-02-27 19:41:45 +0000929 netif_rx(skb);
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200930 }
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900931}
Herbert Xueb1d1642010-02-27 19:41:45 +0000932
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900933static void br_multicast_send_query(struct net_bridge *br,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200934 struct net_bridge_port *port,
Linus Lüssing90010b32014-06-07 18:26:26 +0200935 struct bridge_mcast_own_query *own_query)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900936{
Linus Lüssing90010b32014-06-07 18:26:26 +0200937 struct bridge_mcast_other_query *other_query = NULL;
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100938 struct br_ip br_group;
939 unsigned long time;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900940
941 if (!netif_running(br->dev) || br->multicast_disabled ||
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200942 !br->multicast_querier)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900943 return;
944
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900945 memset(&br_group.u, 0, sizeof(br_group.u));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900946
Linus Lüssing90010b32014-06-07 18:26:26 +0200947 if (port ? (own_query == &port->ip4_own_query) :
948 (own_query == &br->ip4_own_query)) {
949 other_query = &br->ip4_other_query;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200950 br_group.proto = htons(ETH_P_IP);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000951#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200952 } else {
Linus Lüssing90010b32014-06-07 18:26:26 +0200953 other_query = &br->ip6_other_query;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200954 br_group.proto = htons(ETH_P_IPV6);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900955#endif
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200956 }
957
Linus Lüssing90010b32014-06-07 18:26:26 +0200958 if (!other_query || timer_pending(&other_query->timer))
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200959 return;
960
961 __br_multicast_send_query(br, port, &br_group);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900962
Herbert Xueb1d1642010-02-27 19:41:45 +0000963 time = jiffies;
Linus Lüssing90010b32014-06-07 18:26:26 +0200964 time += own_query->startup_sent < br->multicast_startup_query_count ?
Herbert Xueb1d1642010-02-27 19:41:45 +0000965 br->multicast_startup_query_interval :
966 br->multicast_query_interval;
Linus Lüssing90010b32014-06-07 18:26:26 +0200967 mod_timer(&own_query->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +0000968}
969
Linus Lüssing90010b32014-06-07 18:26:26 +0200970static void
971br_multicast_port_query_expired(struct net_bridge_port *port,
972 struct bridge_mcast_own_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +0000973{
Herbert Xueb1d1642010-02-27 19:41:45 +0000974 struct net_bridge *br = port->br;
975
976 spin_lock(&br->multicast_lock);
Dan Carpenter02a780c2010-03-06 01:14:09 +0000977 if (port->state == BR_STATE_DISABLED ||
978 port->state == BR_STATE_BLOCKING)
Herbert Xueb1d1642010-02-27 19:41:45 +0000979 goto out;
980
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200981 if (query->startup_sent < br->multicast_startup_query_count)
982 query->startup_sent++;
Herbert Xueb1d1642010-02-27 19:41:45 +0000983
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200984 br_multicast_send_query(port->br, port, query);
Herbert Xueb1d1642010-02-27 19:41:45 +0000985
986out:
987 spin_unlock(&br->multicast_lock);
988}
989
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200990static void br_ip4_multicast_port_query_expired(unsigned long data)
991{
992 struct net_bridge_port *port = (void *)data;
993
Linus Lüssing90010b32014-06-07 18:26:26 +0200994 br_multicast_port_query_expired(port, &port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200995}
996
997#if IS_ENABLED(CONFIG_IPV6)
998static void br_ip6_multicast_port_query_expired(unsigned long data)
999{
1000 struct net_bridge_port *port = (void *)data;
1001
Linus Lüssing90010b32014-06-07 18:26:26 +02001002 br_multicast_port_query_expired(port, &port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001003}
1004#endif
1005
Nogah Frankel147c1e92017-02-09 14:54:40 +01001006static void br_mc_disabled_update(struct net_device *dev, bool value)
1007{
1008 struct switchdev_attr attr = {
1009 .orig_dev = dev,
1010 .id = SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED,
1011 .flags = SWITCHDEV_F_DEFER,
1012 .u.mc_disabled = value,
1013 };
1014
1015 switchdev_port_attr_set(dev, &attr);
1016}
1017
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001018int br_multicast_add_port(struct net_bridge_port *port)
Herbert Xueb1d1642010-02-27 19:41:45 +00001019{
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001020 port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Herbert Xueb1d1642010-02-27 19:41:45 +00001021
1022 setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
1023 (unsigned long)port);
Linus Lüssing90010b32014-06-07 18:26:26 +02001024 setup_timer(&port->ip4_own_query.timer,
1025 br_ip4_multicast_port_query_expired, (unsigned long)port);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001026#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001027 setup_timer(&port->ip6_own_query.timer,
1028 br_ip6_multicast_port_query_expired, (unsigned long)port);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001029#endif
Nogah Frankel147c1e92017-02-09 14:54:40 +01001030 br_mc_disabled_update(port->dev, port->br->multicast_disabled);
1031
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001032 port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
1033 if (!port->mcast_stats)
1034 return -ENOMEM;
1035
1036 return 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001037}
1038
1039void br_multicast_del_port(struct net_bridge_port *port)
1040{
Satish Ashoke10177a2015-07-15 07:16:51 -07001041 struct net_bridge *br = port->br;
1042 struct net_bridge_port_group *pg;
1043 struct hlist_node *n;
1044
1045 /* Take care of the remaining groups, only perm ones should be left */
1046 spin_lock_bh(&br->multicast_lock);
1047 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
1048 br_multicast_del_pg(br, pg);
1049 spin_unlock_bh(&br->multicast_lock);
Herbert Xueb1d1642010-02-27 19:41:45 +00001050 del_timer_sync(&port->multicast_router_timer);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001051 free_percpu(port->mcast_stats);
Herbert Xueb1d1642010-02-27 19:41:45 +00001052}
1053
Linus Lüssing90010b32014-06-07 18:26:26 +02001054static void br_multicast_enable(struct bridge_mcast_own_query *query)
Herbert Xu561f1102010-02-27 19:41:50 +00001055{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001056 query->startup_sent = 0;
Herbert Xu561f1102010-02-27 19:41:50 +00001057
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001058 if (try_to_del_timer_sync(&query->timer) >= 0 ||
1059 del_timer(&query->timer))
1060 mod_timer(&query->timer, jiffies);
Herbert Xu561f1102010-02-27 19:41:50 +00001061}
1062
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02001063static void __br_multicast_enable_port(struct net_bridge_port *port)
Herbert Xueb1d1642010-02-27 19:41:45 +00001064{
1065 struct net_bridge *br = port->br;
1066
Herbert Xueb1d1642010-02-27 19:41:45 +00001067 if (br->multicast_disabled || !netif_running(br->dev))
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02001068 return;
Herbert Xueb1d1642010-02-27 19:41:45 +00001069
Linus Lüssing90010b32014-06-07 18:26:26 +02001070 br_multicast_enable(&port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001071#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001072 br_multicast_enable(&port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001073#endif
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001074 if (port->multicast_router == MDB_RTR_TYPE_PERM &&
1075 hlist_unhashed(&port->rlist))
Satish Ashok754bc542015-06-19 01:22:57 -07001076 br_multicast_add_router(br, port);
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02001077}
Herbert Xueb1d1642010-02-27 19:41:45 +00001078
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02001079void br_multicast_enable_port(struct net_bridge_port *port)
1080{
1081 struct net_bridge *br = port->br;
1082
1083 spin_lock(&br->multicast_lock);
1084 __br_multicast_enable_port(port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001085 spin_unlock(&br->multicast_lock);
1086}
1087
1088void br_multicast_disable_port(struct net_bridge_port *port)
1089{
1090 struct net_bridge *br = port->br;
1091 struct net_bridge_port_group *pg;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001092 struct hlist_node *n;
Herbert Xueb1d1642010-02-27 19:41:45 +00001093
1094 spin_lock(&br->multicast_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001095 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
Elad Raz9d06b6d2016-02-03 09:57:05 +01001096 if (!(pg->flags & MDB_PG_FLAGS_PERMANENT))
Satish Ashoke10177a2015-07-15 07:16:51 -07001097 br_multicast_del_pg(br, pg);
Herbert Xueb1d1642010-02-27 19:41:45 +00001098
Nogah Frankelf12e7d92017-02-09 14:54:41 +01001099 __del_port_router(port);
1100
Herbert Xueb1d1642010-02-27 19:41:45 +00001101 del_timer(&port->multicast_router_timer);
Linus Lüssing90010b32014-06-07 18:26:26 +02001102 del_timer(&port->ip4_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001103#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001104 del_timer(&port->ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001105#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001106 spin_unlock(&br->multicast_lock);
1107}
1108
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001109static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
1110 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001111 struct sk_buff *skb,
1112 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001113{
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001114 const unsigned char *src;
Herbert Xueb1d1642010-02-27 19:41:45 +00001115 struct igmpv3_report *ih;
1116 struct igmpv3_grec *grec;
1117 int i;
1118 int len;
1119 int num;
1120 int type;
1121 int err = 0;
1122 __be32 group;
1123
Herbert Xueb1d1642010-02-27 19:41:45 +00001124 ih = igmpv3_report_hdr(skb);
1125 num = ntohs(ih->ngrec);
Linus Lüssingc2d4fbd2015-09-11 18:39:48 +02001126 len = skb_transport_offset(skb) + sizeof(*ih);
Herbert Xueb1d1642010-02-27 19:41:45 +00001127
1128 for (i = 0; i < num; i++) {
1129 len += sizeof(*grec);
1130 if (!pskb_may_pull(skb, len))
1131 return -EINVAL;
1132
Herbert Xufd218cf2010-04-07 21:20:47 -07001133 grec = (void *)(skb->data + len - sizeof(*grec));
Herbert Xueb1d1642010-02-27 19:41:45 +00001134 group = grec->grec_mca;
1135 type = grec->grec_type;
1136
Eric Dumazet8eabf952010-04-20 03:20:05 +00001137 len += ntohs(grec->grec_nsrcs) * 4;
Herbert Xueb1d1642010-02-27 19:41:45 +00001138 if (!pskb_may_pull(skb, len))
1139 return -EINVAL;
1140
1141 /* We treat this as an IGMPv2 report for now. */
1142 switch (type) {
1143 case IGMPV3_MODE_IS_INCLUDE:
1144 case IGMPV3_MODE_IS_EXCLUDE:
1145 case IGMPV3_CHANGE_TO_INCLUDE:
1146 case IGMPV3_CHANGE_TO_EXCLUDE:
1147 case IGMPV3_ALLOW_NEW_SOURCES:
1148 case IGMPV3_BLOCK_OLD_SOURCES:
1149 break;
1150
1151 default:
1152 continue;
1153 }
1154
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001155 src = eth_hdr(skb)->h_source;
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001156 if ((type == IGMPV3_CHANGE_TO_INCLUDE ||
1157 type == IGMPV3_MODE_IS_INCLUDE) &&
1158 ntohs(grec->grec_nsrcs) == 0) {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001159 br_ip4_multicast_leave_group(br, port, group, vid, src);
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001160 } else {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001161 err = br_ip4_multicast_add_group(br, port, group, vid,
1162 src);
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001163 if (err)
1164 break;
1165 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001166 }
1167
1168 return err;
1169}
1170
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001171#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001172static int br_ip6_multicast_mld2_report(struct net_bridge *br,
1173 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001174 struct sk_buff *skb,
1175 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001176{
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001177 const unsigned char *src;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001178 struct icmp6hdr *icmp6h;
1179 struct mld2_grec *grec;
1180 int i;
1181 int len;
1182 int num;
1183 int err = 0;
1184
1185 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
1186 return -EINVAL;
1187
1188 icmp6h = icmp6_hdr(skb);
1189 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
Linus Lüssingc2d4fbd2015-09-11 18:39:48 +02001190 len = skb_transport_offset(skb) + sizeof(*icmp6h);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001191
1192 for (i = 0; i < num; i++) {
1193 __be16 *nsrcs, _nsrcs;
1194
1195 nsrcs = skb_header_pointer(skb,
1196 len + offsetof(struct mld2_grec,
Linus Lüssing649e9842011-02-15 13:19:18 +00001197 grec_nsrcs),
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001198 sizeof(_nsrcs), &_nsrcs);
1199 if (!nsrcs)
1200 return -EINVAL;
1201
1202 if (!pskb_may_pull(skb,
1203 len + sizeof(*grec) +
Linus Lüssingd41db9f2011-02-15 13:19:19 +00001204 sizeof(struct in6_addr) * ntohs(*nsrcs)))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001205 return -EINVAL;
1206
1207 grec = (struct mld2_grec *)(skb->data + len);
Linus Lüssingd41db9f2011-02-15 13:19:19 +00001208 len += sizeof(*grec) +
1209 sizeof(struct in6_addr) * ntohs(*nsrcs);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001210
1211 /* We treat these as MLDv1 reports for now. */
1212 switch (grec->grec_type) {
1213 case MLD2_MODE_IS_INCLUDE:
1214 case MLD2_MODE_IS_EXCLUDE:
1215 case MLD2_CHANGE_TO_INCLUDE:
1216 case MLD2_CHANGE_TO_EXCLUDE:
1217 case MLD2_ALLOW_NEW_SOURCES:
1218 case MLD2_BLOCK_OLD_SOURCES:
1219 break;
1220
1221 default:
1222 continue;
1223 }
1224
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001225 src = eth_hdr(skb)->h_source;
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001226 if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE ||
1227 grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
1228 ntohs(*nsrcs) == 0) {
1229 br_ip6_multicast_leave_group(br, port, &grec->grec_mca,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001230 vid, src);
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001231 } else {
1232 err = br_ip6_multicast_add_group(br, port,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001233 &grec->grec_mca, vid,
1234 src);
Davide Caratti92642512016-08-31 14:16:44 +02001235 if (err)
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001236 break;
1237 }
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001238 }
1239
1240 return err;
1241}
1242#endif
1243
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001244static bool br_ip4_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001245 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001246 __be32 saddr)
1247{
1248 if (!timer_pending(&br->ip4_own_query.timer) &&
1249 !timer_pending(&br->ip4_other_query.timer))
1250 goto update;
1251
1252 if (!br->ip4_querier.addr.u.ip4)
1253 goto update;
1254
1255 if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4))
1256 goto update;
1257
1258 return false;
1259
1260update:
1261 br->ip4_querier.addr.u.ip4 = saddr;
1262
Linus Lüssing2cd41432014-06-07 18:26:29 +02001263 /* update protected by general multicast_lock by caller */
1264 rcu_assign_pointer(br->ip4_querier.port, port);
1265
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001266 return true;
1267}
1268
1269#if IS_ENABLED(CONFIG_IPV6)
1270static bool br_ip6_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001271 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001272 struct in6_addr *saddr)
1273{
1274 if (!timer_pending(&br->ip6_own_query.timer) &&
1275 !timer_pending(&br->ip6_other_query.timer))
1276 goto update;
1277
1278 if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0)
1279 goto update;
1280
1281 return false;
1282
1283update:
1284 br->ip6_querier.addr.u.ip6 = *saddr;
1285
Linus Lüssing2cd41432014-06-07 18:26:29 +02001286 /* update protected by general multicast_lock by caller */
1287 rcu_assign_pointer(br->ip6_querier.port, port);
1288
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001289 return true;
1290}
1291#endif
1292
1293static bool br_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001294 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001295 struct br_ip *saddr)
1296{
1297 switch (saddr->proto) {
1298 case htons(ETH_P_IP):
Linus Lüssing2cd41432014-06-07 18:26:29 +02001299 return br_ip4_multicast_select_querier(br, port, saddr->u.ip4);
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001300#if IS_ENABLED(CONFIG_IPV6)
1301 case htons(ETH_P_IPV6):
Linus Lüssing2cd41432014-06-07 18:26:29 +02001302 return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6);
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001303#endif
1304 }
1305
1306 return false;
1307}
1308
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001309static void
Linus Lüssing90010b32014-06-07 18:26:26 +02001310br_multicast_update_query_timer(struct net_bridge *br,
1311 struct bridge_mcast_other_query *query,
1312 unsigned long max_delay)
Linus Lüssingb00589a2013-08-01 01:06:20 +02001313{
Linus Lüssing90010b32014-06-07 18:26:26 +02001314 if (!timer_pending(&query->timer))
1315 query->delay_time = jiffies + max_delay;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001316
Linus Lüssing90010b32014-06-07 18:26:26 +02001317 mod_timer(&query->timer, jiffies + br->multicast_querier_interval);
Linus Lüssingb00589a2013-08-01 01:06:20 +02001318}
1319
Nogah Frankel6d549642017-02-09 14:54:42 +01001320static void br_port_mc_router_state_change(struct net_bridge_port *p,
1321 bool is_mc_router)
1322{
1323 struct switchdev_attr attr = {
1324 .orig_dev = p->dev,
1325 .id = SWITCHDEV_ATTR_ID_PORT_MROUTER,
1326 .flags = SWITCHDEV_F_DEFER,
1327 .u.mrouter = is_mc_router,
1328 };
1329
1330 switchdev_port_attr_set(p->dev, &attr);
1331}
1332
stephen hemminger7e80c122010-04-27 15:01:04 +00001333/*
Cong Wang7c776022013-06-21 15:37:25 +08001334 * Add port to router_list
stephen hemminger7e80c122010-04-27 15:01:04 +00001335 * list is maintained ordered by pointer value
1336 * and locked by br->multicast_lock and RCU
1337 */
Herbert Xu0909e112010-02-27 19:41:49 +00001338static void br_multicast_add_router(struct net_bridge *br,
1339 struct net_bridge_port *port)
1340{
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001341 struct net_bridge_port *p;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001342 struct hlist_node *slot = NULL;
Herbert Xu0909e112010-02-27 19:41:49 +00001343
Nikolay Aleksandrov1a040ea2015-06-09 10:23:57 -07001344 if (!hlist_unhashed(&port->rlist))
1345 return;
1346
Sasha Levinb67bfe02013-02-27 17:06:00 -08001347 hlist_for_each_entry(p, &br->router_list, rlist) {
stephen hemminger7e80c122010-04-27 15:01:04 +00001348 if ((unsigned long) port >= (unsigned long) p)
1349 break;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001350 slot = &p->rlist;
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001351 }
Herbert Xu0909e112010-02-27 19:41:49 +00001352
stephen hemminger7e80c122010-04-27 15:01:04 +00001353 if (slot)
Ken Helias1d023282014-08-06 16:09:16 -07001354 hlist_add_behind_rcu(&port->rlist, slot);
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001355 else
1356 hlist_add_head_rcu(&port->rlist, &br->router_list);
Satish Ashok949f1e32015-07-23 05:00:53 -07001357 br_rtr_notify(br->dev, port, RTM_NEWMDB);
Nogah Frankel6d549642017-02-09 14:54:42 +01001358 br_port_mc_router_state_change(port, true);
Herbert Xu0909e112010-02-27 19:41:49 +00001359}
1360
Herbert Xueb1d1642010-02-27 19:41:45 +00001361static void br_multicast_mark_router(struct net_bridge *br,
1362 struct net_bridge_port *port)
1363{
1364 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +00001365
1366 if (!port) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001367 if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY)
Herbert Xueb1d1642010-02-27 19:41:45 +00001368 mod_timer(&br->multicast_router_timer,
1369 now + br->multicast_querier_interval);
1370 return;
1371 }
1372
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001373 if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
1374 port->multicast_router == MDB_RTR_TYPE_PERM)
Herbert Xueb1d1642010-02-27 19:41:45 +00001375 return;
1376
Herbert Xu0909e112010-02-27 19:41:49 +00001377 br_multicast_add_router(br, port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001378
Herbert Xueb1d1642010-02-27 19:41:45 +00001379 mod_timer(&port->multicast_router_timer,
1380 now + br->multicast_querier_interval);
1381}
1382
1383static void br_multicast_query_received(struct net_bridge *br,
1384 struct net_bridge_port *port,
Linus Lüssing90010b32014-06-07 18:26:26 +02001385 struct bridge_mcast_other_query *query,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001386 struct br_ip *saddr,
Linus Lüssingb00589a2013-08-01 01:06:20 +02001387 unsigned long max_delay)
Herbert Xueb1d1642010-02-27 19:41:45 +00001388{
Linus Lüssing2cd41432014-06-07 18:26:29 +02001389 if (!br_multicast_select_querier(br, port, saddr))
Herbert Xueb1d1642010-02-27 19:41:45 +00001390 return;
1391
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001392 br_multicast_update_query_timer(br, query, max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001393 br_multicast_mark_router(br, port);
1394}
1395
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001396static int br_ip4_multicast_query(struct net_bridge *br,
1397 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001398 struct sk_buff *skb,
1399 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001400{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001401 const struct iphdr *iph = ip_hdr(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001402 struct igmphdr *ih = igmp_hdr(skb);
1403 struct net_bridge_mdb_entry *mp;
1404 struct igmpv3_query *ih3;
1405 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +00001406 struct net_bridge_port_group __rcu **pp;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001407 struct br_ip saddr;
Herbert Xueb1d1642010-02-27 19:41:45 +00001408 unsigned long max_delay;
1409 unsigned long now = jiffies;
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001410 unsigned int offset = skb_transport_offset(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001411 __be32 group;
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001412 int err = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001413
1414 spin_lock(&br->multicast_lock);
1415 if (!netif_running(br->dev) ||
1416 (port && port->state == BR_STATE_DISABLED))
1417 goto out;
1418
Herbert Xueb1d1642010-02-27 19:41:45 +00001419 group = ih->group;
1420
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001421 if (skb->len == offset + sizeof(*ih)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001422 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1423
1424 if (!max_delay) {
1425 max_delay = 10 * HZ;
1426 group = 0;
1427 }
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001428 } else if (skb->len >= offset + sizeof(*ih3)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001429 ih3 = igmpv3_query_hdr(skb);
1430 if (ih3->nsrcs)
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001431 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001432
YOSHIFUJI Hideaki / 吉藤英明0ba8c9e2010-03-15 19:27:00 +00001433 max_delay = ih3->code ?
1434 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001435 } else {
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001436 goto out;
1437 }
1438
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001439 if (!group) {
1440 saddr.proto = htons(ETH_P_IP);
1441 saddr.u.ip4 = iph->saddr;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001442
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001443 br_multicast_query_received(br, port, &br->ip4_other_query,
1444 &saddr, max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001445 goto out;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001446 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001447
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001448 mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001449 if (!mp)
1450 goto out;
1451
1452 max_delay *= br->multicast_last_member_count;
1453
Herbert Xu8a870172011-02-12 01:05:42 -08001454 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001455 (timer_pending(&mp->timer) ?
1456 time_after(mp->timer.expires, now + max_delay) :
1457 try_to_del_timer_sync(&mp->timer) >= 0))
1458 mod_timer(&mp->timer, now + max_delay);
1459
Eric Dumazete8051682010-11-15 06:38:10 +00001460 for (pp = &mp->ports;
1461 (p = mlock_dereference(*pp, br)) != NULL;
1462 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001463 if (timer_pending(&p->timer) ?
1464 time_after(p->timer.expires, now + max_delay) :
1465 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001466 mod_timer(&p->timer, now + max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001467 }
1468
1469out:
1470 spin_unlock(&br->multicast_lock);
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001471 return err;
Herbert Xueb1d1642010-02-27 19:41:45 +00001472}
1473
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001474#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001475static int br_ip6_multicast_query(struct net_bridge *br,
1476 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001477 struct sk_buff *skb,
1478 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001479{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001480 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
stephen hemmingereca2a432012-12-13 06:51:28 +00001481 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001482 struct net_bridge_mdb_entry *mp;
1483 struct mld2_query *mld2q;
Eric Dumazete8051682010-11-15 06:38:10 +00001484 struct net_bridge_port_group *p;
1485 struct net_bridge_port_group __rcu **pp;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001486 struct br_ip saddr;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001487 unsigned long max_delay;
1488 unsigned long now = jiffies;
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001489 unsigned int offset = skb_transport_offset(skb);
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001490 const struct in6_addr *group = NULL;
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001491 bool is_general_query;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001492 int err = 0;
1493
1494 spin_lock(&br->multicast_lock);
1495 if (!netif_running(br->dev) ||
1496 (port && port->state == BR_STATE_DISABLED))
1497 goto out;
1498
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001499 if (skb->len == offset + sizeof(*mld)) {
1500 if (!pskb_may_pull(skb, offset + sizeof(*mld))) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001501 err = -EINVAL;
1502 goto out;
1503 }
1504 mld = (struct mld_msg *) icmp6_hdr(skb);
Li RongQing47152132012-07-09 23:56:12 +00001505 max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001506 if (max_delay)
1507 group = &mld->mld_mca;
Linus Lüssing248ba8e2013-08-06 00:32:05 +02001508 } else {
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001509 if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001510 err = -EINVAL;
1511 goto out;
1512 }
1513 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1514 if (!mld2q->mld2q_nsrcs)
1515 group = &mld2q->mld2q_mca;
Daniel Borkmanne3f5b172013-09-04 00:19:39 +02001516
1517 max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001518 }
1519
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001520 is_general_query = group && ipv6_addr_any(group);
1521
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001522 if (is_general_query) {
1523 saddr.proto = htons(ETH_P_IPV6);
1524 saddr.u.ip6 = ip6h->saddr;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001525
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001526 br_multicast_query_received(br, port, &br->ip6_other_query,
1527 &saddr, max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001528 goto out;
Linus Lüssing6c03ee82014-06-12 01:41:23 +02001529 } else if (!group) {
1530 goto out;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001531 }
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001532
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001533 mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001534 if (!mp)
1535 goto out;
1536
1537 max_delay *= br->multicast_last_member_count;
Herbert Xu8a870172011-02-12 01:05:42 -08001538 if (mp->mglist &&
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001539 (timer_pending(&mp->timer) ?
1540 time_after(mp->timer.expires, now + max_delay) :
1541 try_to_del_timer_sync(&mp->timer) >= 0))
1542 mod_timer(&mp->timer, now + max_delay);
1543
Eric Dumazete8051682010-11-15 06:38:10 +00001544 for (pp = &mp->ports;
1545 (p = mlock_dereference(*pp, br)) != NULL;
1546 pp = &p->next) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001547 if (timer_pending(&p->timer) ?
1548 time_after(p->timer.expires, now + max_delay) :
1549 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001550 mod_timer(&p->timer, now + max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001551 }
1552
1553out:
1554 spin_unlock(&br->multicast_lock);
1555 return err;
1556}
1557#endif
1558
Linus Lüssing90010b32014-06-07 18:26:26 +02001559static void
1560br_multicast_leave_group(struct net_bridge *br,
1561 struct net_bridge_port *port,
1562 struct br_ip *group,
1563 struct bridge_mcast_other_query *other_query,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001564 struct bridge_mcast_own_query *own_query,
1565 const unsigned char *src)
Herbert Xueb1d1642010-02-27 19:41:45 +00001566{
1567 struct net_bridge_mdb_htable *mdb;
1568 struct net_bridge_mdb_entry *mp;
1569 struct net_bridge_port_group *p;
1570 unsigned long now;
1571 unsigned long time;
1572
Herbert Xueb1d1642010-02-27 19:41:45 +00001573 spin_lock(&br->multicast_lock);
1574 if (!netif_running(br->dev) ||
Satish Ashok544586f2015-07-28 03:28:27 -07001575 (port && port->state == BR_STATE_DISABLED))
Herbert Xueb1d1642010-02-27 19:41:45 +00001576 goto out;
1577
Eric Dumazete8051682010-11-15 06:38:10 +00001578 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001579 mp = br_mdb_ip_get(mdb, group);
1580 if (!mp)
1581 goto out;
1582
Satish Ashok544586f2015-07-28 03:28:27 -07001583 if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
1584 struct net_bridge_port_group __rcu **pp;
1585
1586 for (pp = &mp->ports;
1587 (p = mlock_dereference(*pp, br)) != NULL;
1588 pp = &p->next) {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001589 if (!br_port_group_equal(p, port, src))
Satish Ashok544586f2015-07-28 03:28:27 -07001590 continue;
1591
1592 rcu_assign_pointer(*pp, p->next);
1593 hlist_del_init(&p->mglist);
1594 del_timer(&p->timer);
1595 call_rcu_bh(&p->rcu, br_multicast_free_pg);
Elad Raz45ebcce2016-04-21 12:52:45 +02001596 br_mdb_notify(br->dev, port, group, RTM_DELMDB,
1597 p->flags);
Satish Ashok544586f2015-07-28 03:28:27 -07001598
1599 if (!mp->ports && !mp->mglist &&
1600 netif_running(br->dev))
1601 mod_timer(&mp->timer, jiffies);
1602 }
1603 goto out;
1604 }
1605
1606 if (timer_pending(&other_query->timer))
1607 goto out;
1608
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001609 if (br->multicast_querier) {
Cong Wang6b7df112013-05-21 21:52:56 +00001610 __br_multicast_send_query(br, port, &mp->addr);
1611
1612 time = jiffies + br->multicast_last_member_count *
1613 br->multicast_last_member_interval;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001614
Linus Lüssing90010b32014-06-07 18:26:26 +02001615 mod_timer(&own_query->timer, time);
Cong Wang6b7df112013-05-21 21:52:56 +00001616
1617 for (p = mlock_dereference(mp->ports, br);
1618 p != NULL;
1619 p = mlock_dereference(p->next, br)) {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001620 if (!br_port_group_equal(p, port, src))
Cong Wang6b7df112013-05-21 21:52:56 +00001621 continue;
1622
1623 if (!hlist_unhashed(&p->mglist) &&
1624 (timer_pending(&p->timer) ?
1625 time_after(p->timer.expires, time) :
1626 try_to_del_timer_sync(&p->timer) >= 0)) {
1627 mod_timer(&p->timer, time);
1628 }
1629
1630 break;
1631 }
1632 }
1633
Herbert Xueb1d1642010-02-27 19:41:45 +00001634 now = jiffies;
1635 time = now + br->multicast_last_member_count *
1636 br->multicast_last_member_interval;
1637
1638 if (!port) {
Linus Lüssing454594f2013-10-20 00:58:57 +02001639 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001640 (timer_pending(&mp->timer) ?
1641 time_after(mp->timer.expires, time) :
1642 try_to_del_timer_sync(&mp->timer) >= 0)) {
1643 mod_timer(&mp->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +00001644 }
Linus Lüssing454594f2013-10-20 00:58:57 +02001645
1646 goto out;
1647 }
1648
1649 for (p = mlock_dereference(mp->ports, br);
1650 p != NULL;
1651 p = mlock_dereference(p->next, br)) {
1652 if (p->port != port)
1653 continue;
1654
1655 if (!hlist_unhashed(&p->mglist) &&
1656 (timer_pending(&p->timer) ?
1657 time_after(p->timer.expires, time) :
1658 try_to_del_timer_sync(&p->timer) >= 0)) {
1659 mod_timer(&p->timer, time);
1660 }
1661
1662 break;
Herbert Xueb1d1642010-02-27 19:41:45 +00001663 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001664out:
1665 spin_unlock(&br->multicast_lock);
1666}
1667
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001668static void br_ip4_multicast_leave_group(struct net_bridge *br,
1669 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001670 __be32 group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001671 __u16 vid,
1672 const unsigned char *src)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001673{
1674 struct br_ip br_group;
Linus Lüssing90010b32014-06-07 18:26:26 +02001675 struct bridge_mcast_own_query *own_query;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001676
1677 if (ipv4_is_local_multicast(group))
1678 return;
1679
Linus Lüssing90010b32014-06-07 18:26:26 +02001680 own_query = port ? &port->ip4_own_query : &br->ip4_own_query;
1681
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001682 br_group.u.ip4 = group;
1683 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001684 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001685
Linus Lüssing90010b32014-06-07 18:26:26 +02001686 br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001687 own_query, src);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001688}
1689
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001690#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001691static void br_ip6_multicast_leave_group(struct net_bridge *br,
1692 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001693 const struct in6_addr *group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001694 __u16 vid,
1695 const unsigned char *src)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001696{
1697 struct br_ip br_group;
Linus Lüssing90010b32014-06-07 18:26:26 +02001698 struct bridge_mcast_own_query *own_query;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001699
Linus Lüssing3c3769e2013-09-04 02:13:39 +02001700 if (ipv6_addr_is_ll_all_nodes(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001701 return;
1702
Linus Lüssing90010b32014-06-07 18:26:26 +02001703 own_query = port ? &port->ip6_own_query : &br->ip6_own_query;
1704
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001705 br_group.u.ip6 = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001706 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001707 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001708
Linus Lüssing90010b32014-06-07 18:26:26 +02001709 br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001710 own_query, src);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001711}
1712#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001713
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001714static void br_multicast_err_count(const struct net_bridge *br,
1715 const struct net_bridge_port *p,
1716 __be16 proto)
1717{
1718 struct bridge_mcast_stats __percpu *stats;
1719 struct bridge_mcast_stats *pstats;
1720
1721 if (!br->multicast_stats_enabled)
1722 return;
1723
1724 if (p)
1725 stats = p->mcast_stats;
1726 else
1727 stats = br->mcast_stats;
1728 if (WARN_ON(!stats))
1729 return;
1730
1731 pstats = this_cpu_ptr(stats);
1732
1733 u64_stats_update_begin(&pstats->syncp);
1734 switch (proto) {
1735 case htons(ETH_P_IP):
1736 pstats->mstats.igmp_parse_errors++;
1737 break;
1738#if IS_ENABLED(CONFIG_IPV6)
1739 case htons(ETH_P_IPV6):
1740 pstats->mstats.mld_parse_errors++;
1741 break;
1742#endif
1743 }
1744 u64_stats_update_end(&pstats->syncp);
1745}
1746
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001747static void br_multicast_pim(struct net_bridge *br,
1748 struct net_bridge_port *port,
1749 const struct sk_buff *skb)
1750{
1751 unsigned int offset = skb_transport_offset(skb);
1752 struct pimhdr *pimhdr, _pimhdr;
1753
1754 pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr);
1755 if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION ||
1756 pim_hdr_type(pimhdr) != PIM_TYPE_HELLO)
1757 return;
1758
1759 br_multicast_mark_router(br, port);
1760}
1761
Herbert Xueb1d1642010-02-27 19:41:45 +00001762static int br_multicast_ipv4_rcv(struct net_bridge *br,
1763 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001764 struct sk_buff *skb,
1765 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001766{
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001767 struct sk_buff *skb_trimmed = NULL;
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001768 const unsigned char *src;
Herbert Xueb1d1642010-02-27 19:41:45 +00001769 struct igmphdr *ih;
Herbert Xueb1d1642010-02-27 19:41:45 +00001770 int err;
1771
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001772 err = ip_mc_check_igmp(skb, &skb_trimmed);
Herbert Xueb1d1642010-02-27 19:41:45 +00001773
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001774 if (err == -ENOMSG) {
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001775 if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) {
Herbert Xubd4265f2011-06-23 02:39:12 +00001776 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001777 } else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) {
1778 if (ip_hdr(skb)->protocol == IPPROTO_PIM)
1779 br_multicast_pim(br, port, skb);
1780 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001781 return 0;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001782 } else if (err < 0) {
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001783 br_multicast_err_count(br, port, skb->protocol);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001784 return err;
Herbert Xubd4265f2011-06-23 02:39:12 +00001785 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001786
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001787 ih = igmp_hdr(skb);
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001788 src = eth_hdr(skb)->h_source;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001789 BR_INPUT_SKB_CB(skb)->igmp = ih->type;
Herbert Xueb1d1642010-02-27 19:41:45 +00001790
1791 switch (ih->type) {
1792 case IGMP_HOST_MEMBERSHIP_REPORT:
1793 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Fernando Luis Vázquez Cao62b2bcb2011-06-13 15:04:43 +00001794 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001795 err = br_ip4_multicast_add_group(br, port, ih->group, vid, src);
Herbert Xueb1d1642010-02-27 19:41:45 +00001796 break;
1797 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001798 err = br_ip4_multicast_igmp3_report(br, port, skb_trimmed, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001799 break;
1800 case IGMP_HOST_MEMBERSHIP_QUERY:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001801 err = br_ip4_multicast_query(br, port, skb_trimmed, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001802 break;
1803 case IGMP_HOST_LEAVE_MESSAGE:
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001804 br_ip4_multicast_leave_group(br, port, ih->group, vid, src);
Herbert Xueb1d1642010-02-27 19:41:45 +00001805 break;
1806 }
1807
Linus Lüssinga5169932015-08-13 05:54:07 +02001808 if (skb_trimmed && skb_trimmed != skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001809 kfree_skb(skb_trimmed);
1810
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07001811 br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001812 BR_MCAST_DIR_RX);
1813
Herbert Xueb1d1642010-02-27 19:41:45 +00001814 return err;
1815}
1816
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001817#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001818static int br_multicast_ipv6_rcv(struct net_bridge *br,
1819 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001820 struct sk_buff *skb,
1821 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001822{
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001823 struct sk_buff *skb_trimmed = NULL;
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001824 const unsigned char *src;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001825 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001826 int err;
1827
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001828 err = ipv6_mc_check_mld(skb, &skb_trimmed);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001829
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001830 if (err == -ENOMSG) {
1831 if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
1832 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Linus Lüssing8fad9c32013-09-04 02:13:38 +02001833 return 0;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001834 } else if (err < 0) {
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001835 br_multicast_err_count(br, port, skb->protocol);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001836 return err;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001837 }
1838
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001839 mld = (struct mld_msg *)skb_transport_header(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001840 BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001841
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001842 switch (mld->mld_type) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001843 case ICMPV6_MGM_REPORT:
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001844 src = eth_hdr(skb)->h_source;
Fernando Luis Vázquez Caofc2af6c2011-06-13 15:06:58 +00001845 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001846 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid,
1847 src);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001848 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001849 case ICMPV6_MLD2_REPORT:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001850 err = br_ip6_multicast_mld2_report(br, port, skb_trimmed, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001851 break;
1852 case ICMPV6_MGM_QUERY:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001853 err = br_ip6_multicast_query(br, port, skb_trimmed, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001854 break;
1855 case ICMPV6_MGM_REDUCTION:
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001856 src = eth_hdr(skb)->h_source;
1857 br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001858 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001859 }
1860
Linus Lüssinga5169932015-08-13 05:54:07 +02001861 if (skb_trimmed && skb_trimmed != skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001862 kfree_skb(skb_trimmed);
1863
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07001864 br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001865 BR_MCAST_DIR_RX);
1866
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001867 return err;
1868}
1869#endif
1870
Herbert Xueb1d1642010-02-27 19:41:45 +00001871int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001872 struct sk_buff *skb, u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001873{
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001874 int ret = 0;
1875
YOSHIFUJI Hideaki / 吉藤英明1fafc7a2010-04-25 08:06:40 +00001876 BR_INPUT_SKB_CB(skb)->igmp = 0;
1877 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1878
Herbert Xueb1d1642010-02-27 19:41:45 +00001879 if (br->multicast_disabled)
1880 return 0;
1881
1882 switch (skb->protocol) {
1883 case htons(ETH_P_IP):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001884 ret = br_multicast_ipv4_rcv(br, port, skb, vid);
1885 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001886#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001887 case htons(ETH_P_IPV6):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001888 ret = br_multicast_ipv6_rcv(br, port, skb, vid);
1889 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001890#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001891 }
1892
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001893 return ret;
Herbert Xueb1d1642010-02-27 19:41:45 +00001894}
1895
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001896static void br_multicast_query_expired(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001897 struct bridge_mcast_own_query *query,
1898 struct bridge_mcast_querier *querier)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001899{
1900 spin_lock(&br->multicast_lock);
1901 if (query->startup_sent < br->multicast_startup_query_count)
1902 query->startup_sent++;
1903
Eric Dumazet71d9f612015-05-28 04:42:54 -07001904 RCU_INIT_POINTER(querier->port, NULL);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001905 br_multicast_send_query(br, NULL, query);
1906 spin_unlock(&br->multicast_lock);
1907}
1908
1909static void br_ip4_multicast_query_expired(unsigned long data)
Herbert Xueb1d1642010-02-27 19:41:45 +00001910{
1911 struct net_bridge *br = (void *)data;
1912
Linus Lüssing2cd41432014-06-07 18:26:29 +02001913 br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier);
Herbert Xueb1d1642010-02-27 19:41:45 +00001914}
1915
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001916#if IS_ENABLED(CONFIG_IPV6)
1917static void br_ip6_multicast_query_expired(unsigned long data)
1918{
1919 struct net_bridge *br = (void *)data;
1920
Linus Lüssing2cd41432014-06-07 18:26:29 +02001921 br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001922}
1923#endif
1924
Herbert Xueb1d1642010-02-27 19:41:45 +00001925void br_multicast_init(struct net_bridge *br)
1926{
1927 br->hash_elasticity = 4;
1928 br->hash_max = 512;
1929
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001930 br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Herbert Xuc5c23262012-04-13 02:37:42 +00001931 br->multicast_querier = 0;
Cong Wang1c8ad5b2013-05-21 21:52:54 +00001932 br->multicast_query_use_ifaddr = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001933 br->multicast_last_member_count = 2;
1934 br->multicast_startup_query_count = 2;
1935
1936 br->multicast_last_member_interval = HZ;
1937 br->multicast_query_response_interval = 10 * HZ;
1938 br->multicast_startup_query_interval = 125 * HZ / 4;
1939 br->multicast_query_interval = 125 * HZ;
1940 br->multicast_querier_interval = 255 * HZ;
1941 br->multicast_membership_interval = 260 * HZ;
1942
Linus Lüssing90010b32014-06-07 18:26:26 +02001943 br->ip4_other_query.delay_time = 0;
Linus Lüssing2cd41432014-06-07 18:26:29 +02001944 br->ip4_querier.port = NULL;
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001945 br->multicast_igmp_version = 2;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001946#if IS_ENABLED(CONFIG_IPV6)
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001947 br->multicast_mld_version = 1;
Linus Lüssing90010b32014-06-07 18:26:26 +02001948 br->ip6_other_query.delay_time = 0;
Linus Lüssing2cd41432014-06-07 18:26:29 +02001949 br->ip6_querier.port = NULL;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001950#endif
daniel0888d5f2016-06-24 12:35:18 +02001951 br->has_ipv6_addr = 1;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001952
Herbert Xueb1d1642010-02-27 19:41:45 +00001953 spin_lock_init(&br->multicast_lock);
1954 setup_timer(&br->multicast_router_timer,
1955 br_multicast_local_router_expired, 0);
Linus Lüssing90010b32014-06-07 18:26:26 +02001956 setup_timer(&br->ip4_other_query.timer,
1957 br_ip4_multicast_querier_expired, (unsigned long)br);
1958 setup_timer(&br->ip4_own_query.timer, br_ip4_multicast_query_expired,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001959 (unsigned long)br);
1960#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001961 setup_timer(&br->ip6_other_query.timer,
1962 br_ip6_multicast_querier_expired, (unsigned long)br);
1963 setup_timer(&br->ip6_own_query.timer, br_ip6_multicast_query_expired,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001964 (unsigned long)br);
1965#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001966}
1967
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001968static void __br_multicast_open(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +02001969 struct bridge_mcast_own_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +00001970{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001971 query->startup_sent = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001972
1973 if (br->multicast_disabled)
1974 return;
1975
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001976 mod_timer(&query->timer, jiffies);
1977}
1978
1979void br_multicast_open(struct net_bridge *br)
1980{
Linus Lüssing90010b32014-06-07 18:26:26 +02001981 __br_multicast_open(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001982#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001983 __br_multicast_open(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001984#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001985}
1986
1987void br_multicast_stop(struct net_bridge *br)
1988{
Herbert Xueb1d1642010-02-27 19:41:45 +00001989 del_timer_sync(&br->multicast_router_timer);
Linus Lüssing90010b32014-06-07 18:26:26 +02001990 del_timer_sync(&br->ip4_other_query.timer);
1991 del_timer_sync(&br->ip4_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001992#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001993 del_timer_sync(&br->ip6_other_query.timer);
1994 del_timer_sync(&br->ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001995#endif
Satish Ashoke10177a2015-07-15 07:16:51 -07001996}
1997
1998void br_multicast_dev_del(struct net_bridge *br)
1999{
2000 struct net_bridge_mdb_htable *mdb;
2001 struct net_bridge_mdb_entry *mp;
2002 struct hlist_node *n;
2003 u32 ver;
2004 int i;
Herbert Xueb1d1642010-02-27 19:41:45 +00002005
2006 spin_lock_bh(&br->multicast_lock);
Eric Dumazete8051682010-11-15 06:38:10 +00002007 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00002008 if (!mdb)
2009 goto out;
2010
2011 br->mdb = NULL;
2012
2013 ver = mdb->ver;
2014 for (i = 0; i < mdb->max; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08002015 hlist_for_each_entry_safe(mp, n, &mdb->mhash[i],
Herbert Xueb1d1642010-02-27 19:41:45 +00002016 hlist[ver]) {
2017 del_timer(&mp->timer);
Herbert Xueb1d1642010-02-27 19:41:45 +00002018 call_rcu_bh(&mp->rcu, br_multicast_free_group);
2019 }
2020 }
2021
2022 if (mdb->old) {
2023 spin_unlock_bh(&br->multicast_lock);
Herbert Xu10cc2b52010-03-05 21:03:35 +00002024 rcu_barrier_bh();
Herbert Xueb1d1642010-02-27 19:41:45 +00002025 spin_lock_bh(&br->multicast_lock);
2026 WARN_ON(mdb->old);
2027 }
2028
2029 mdb->old = mdb;
2030 call_rcu_bh(&mdb->rcu, br_mdb_free);
2031
2032out:
2033 spin_unlock_bh(&br->multicast_lock);
2034}
Herbert Xu0909e112010-02-27 19:41:49 +00002035
2036int br_multicast_set_router(struct net_bridge *br, unsigned long val)
2037{
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02002038 int err = -EINVAL;
Herbert Xu0909e112010-02-27 19:41:49 +00002039
2040 spin_lock_bh(&br->multicast_lock);
Herbert Xu0909e112010-02-27 19:41:49 +00002041
2042 switch (val) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002043 case MDB_RTR_TYPE_DISABLED:
2044 case MDB_RTR_TYPE_PERM:
Herbert Xu0909e112010-02-27 19:41:49 +00002045 del_timer(&br->multicast_router_timer);
2046 /* fall through */
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002047 case MDB_RTR_TYPE_TEMP_QUERY:
Herbert Xu0909e112010-02-27 19:41:49 +00002048 br->multicast_router = val;
2049 err = 0;
2050 break;
Herbert Xu0909e112010-02-27 19:41:49 +00002051 }
2052
Herbert Xu0909e112010-02-27 19:41:49 +00002053 spin_unlock_bh(&br->multicast_lock);
2054
2055 return err;
2056}
2057
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002058static void __del_port_router(struct net_bridge_port *p)
2059{
2060 if (hlist_unhashed(&p->rlist))
2061 return;
2062 hlist_del_init_rcu(&p->rlist);
2063 br_rtr_notify(p->br->dev, p, RTM_DELMDB);
Nogah Frankel6d549642017-02-09 14:54:42 +01002064 br_port_mc_router_state_change(p, false);
Nogah Frankelf12e7d92017-02-09 14:54:41 +01002065
2066 /* don't allow timer refresh */
2067 if (p->multicast_router == MDB_RTR_TYPE_TEMP)
2068 p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002069}
2070
Herbert Xu0909e112010-02-27 19:41:49 +00002071int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
2072{
2073 struct net_bridge *br = p->br;
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01002074 unsigned long now = jiffies;
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02002075 int err = -EINVAL;
Herbert Xu0909e112010-02-27 19:41:49 +00002076
2077 spin_lock(&br->multicast_lock);
Nikolay Aleksandrov4950cfd2016-02-26 21:20:02 +01002078 if (p->multicast_router == val) {
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01002079 /* Refresh the temp router port timer */
2080 if (p->multicast_router == MDB_RTR_TYPE_TEMP)
2081 mod_timer(&p->multicast_router_timer,
2082 now + br->multicast_querier_interval);
Nikolay Aleksandrov4950cfd2016-02-26 21:20:02 +01002083 err = 0;
2084 goto unlock;
2085 }
Herbert Xu0909e112010-02-27 19:41:49 +00002086 switch (val) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002087 case MDB_RTR_TYPE_DISABLED:
2088 p->multicast_router = MDB_RTR_TYPE_DISABLED;
2089 __del_port_router(p);
Herbert Xu0909e112010-02-27 19:41:49 +00002090 del_timer(&p->multicast_router_timer);
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002091 break;
2092 case MDB_RTR_TYPE_TEMP_QUERY:
2093 p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
2094 __del_port_router(p);
2095 break;
2096 case MDB_RTR_TYPE_PERM:
2097 p->multicast_router = MDB_RTR_TYPE_PERM;
2098 del_timer(&p->multicast_router_timer);
Herbert Xu0909e112010-02-27 19:41:49 +00002099 br_multicast_add_router(br, p);
2100 break;
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01002101 case MDB_RTR_TYPE_TEMP:
2102 p->multicast_router = MDB_RTR_TYPE_TEMP;
2103 br_multicast_mark_router(br, p);
2104 break;
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002105 default:
2106 goto unlock;
Herbert Xu0909e112010-02-27 19:41:49 +00002107 }
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002108 err = 0;
2109unlock:
Herbert Xu0909e112010-02-27 19:41:49 +00002110 spin_unlock(&br->multicast_lock);
2111
2112 return err;
2113}
Herbert Xu561f1102010-02-27 19:41:50 +00002114
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002115static void br_multicast_start_querier(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +02002116 struct bridge_mcast_own_query *query)
Herbert Xu561f1102010-02-27 19:41:50 +00002117{
2118 struct net_bridge_port *port;
Herbert Xu74857212012-04-13 02:37:42 +00002119
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002120 __br_multicast_open(br, query);
Herbert Xu74857212012-04-13 02:37:42 +00002121
2122 list_for_each_entry(port, &br->port_list, list) {
2123 if (port->state == BR_STATE_DISABLED ||
2124 port->state == BR_STATE_BLOCKING)
2125 continue;
2126
Linus Lüssing90010b32014-06-07 18:26:26 +02002127 if (query == &br->ip4_own_query)
2128 br_multicast_enable(&port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002129#if IS_ENABLED(CONFIG_IPV6)
2130 else
Linus Lüssing90010b32014-06-07 18:26:26 +02002131 br_multicast_enable(&port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002132#endif
Herbert Xu74857212012-04-13 02:37:42 +00002133 }
2134}
2135
2136int br_multicast_toggle(struct net_bridge *br, unsigned long val)
2137{
Eric Dumazete8051682010-11-15 06:38:10 +00002138 struct net_bridge_mdb_htable *mdb;
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02002139 struct net_bridge_port *port;
2140 int err = 0;
Herbert Xu561f1102010-02-27 19:41:50 +00002141
Andrey Vaginef5e0d82011-11-10 05:48:03 +00002142 spin_lock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00002143 if (br->multicast_disabled == !val)
2144 goto unlock;
2145
Nogah Frankel147c1e92017-02-09 14:54:40 +01002146 br_mc_disabled_update(br->dev, !val);
Herbert Xu561f1102010-02-27 19:41:50 +00002147 br->multicast_disabled = !val;
2148 if (br->multicast_disabled)
2149 goto unlock;
2150
Herbert Xu3a7fda02010-07-29 00:45:30 +00002151 if (!netif_running(br->dev))
2152 goto unlock;
2153
Eric Dumazete8051682010-11-15 06:38:10 +00002154 mdb = mlock_dereference(br->mdb, br);
2155 if (mdb) {
2156 if (mdb->old) {
Herbert Xu561f1102010-02-27 19:41:50 +00002157 err = -EEXIST;
2158rollback:
2159 br->multicast_disabled = !!val;
2160 goto unlock;
2161 }
2162
Eric Dumazete8051682010-11-15 06:38:10 +00002163 err = br_mdb_rehash(&br->mdb, mdb->max,
Herbert Xu561f1102010-02-27 19:41:50 +00002164 br->hash_elasticity);
2165 if (err)
2166 goto rollback;
2167 }
2168
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02002169 br_multicast_open(br);
2170 list_for_each_entry(port, &br->port_list, list)
2171 __br_multicast_enable_port(port);
Herbert Xu561f1102010-02-27 19:41:50 +00002172
2173unlock:
Andrey Vaginef5e0d82011-11-10 05:48:03 +00002174 spin_unlock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00002175
2176 return err;
2177}
Herbert Xub1951672010-02-27 19:41:51 +00002178
Herbert Xuc5c23262012-04-13 02:37:42 +00002179int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
2180{
Linus Lüssingb00589a2013-08-01 01:06:20 +02002181 unsigned long max_delay;
2182
Herbert Xuc5c23262012-04-13 02:37:42 +00002183 val = !!val;
2184
2185 spin_lock_bh(&br->multicast_lock);
2186 if (br->multicast_querier == val)
2187 goto unlock;
2188
2189 br->multicast_querier = val;
Linus Lüssingb00589a2013-08-01 01:06:20 +02002190 if (!val)
2191 goto unlock;
2192
2193 max_delay = br->multicast_query_response_interval;
Linus Lüssingb00589a2013-08-01 01:06:20 +02002194
Linus Lüssing90010b32014-06-07 18:26:26 +02002195 if (!timer_pending(&br->ip4_other_query.timer))
2196 br->ip4_other_query.delay_time = jiffies + max_delay;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002197
Linus Lüssing90010b32014-06-07 18:26:26 +02002198 br_multicast_start_querier(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002199
2200#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02002201 if (!timer_pending(&br->ip6_other_query.timer))
2202 br->ip6_other_query.delay_time = jiffies + max_delay;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002203
Linus Lüssing90010b32014-06-07 18:26:26 +02002204 br_multicast_start_querier(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002205#endif
Herbert Xuc5c23262012-04-13 02:37:42 +00002206
2207unlock:
2208 spin_unlock_bh(&br->multicast_lock);
2209
2210 return 0;
2211}
2212
Herbert Xub1951672010-02-27 19:41:51 +00002213int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
2214{
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02002215 int err = -EINVAL;
Herbert Xub1951672010-02-27 19:41:51 +00002216 u32 old;
Eric Dumazete8051682010-11-15 06:38:10 +00002217 struct net_bridge_mdb_htable *mdb;
Herbert Xub1951672010-02-27 19:41:51 +00002218
Curt Brunefe0d6922014-01-06 11:00:32 -08002219 spin_lock_bh(&br->multicast_lock);
Herbert Xub1951672010-02-27 19:41:51 +00002220 if (!is_power_of_2(val))
2221 goto unlock;
Eric Dumazete8051682010-11-15 06:38:10 +00002222
2223 mdb = mlock_dereference(br->mdb, br);
2224 if (mdb && val < mdb->size)
Herbert Xub1951672010-02-27 19:41:51 +00002225 goto unlock;
2226
2227 err = 0;
2228
2229 old = br->hash_max;
2230 br->hash_max = val;
2231
Eric Dumazete8051682010-11-15 06:38:10 +00002232 if (mdb) {
2233 if (mdb->old) {
Herbert Xub1951672010-02-27 19:41:51 +00002234 err = -EEXIST;
2235rollback:
2236 br->hash_max = old;
2237 goto unlock;
2238 }
2239
2240 err = br_mdb_rehash(&br->mdb, br->hash_max,
2241 br->hash_elasticity);
2242 if (err)
2243 goto rollback;
2244 }
2245
2246unlock:
Curt Brunefe0d6922014-01-06 11:00:32 -08002247 spin_unlock_bh(&br->multicast_lock);
Herbert Xub1951672010-02-27 19:41:51 +00002248
2249 return err;
2250}
Linus Lüssing07f8ac42014-06-07 18:26:28 +02002251
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01002252int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val)
2253{
2254 /* Currently we support only version 2 and 3 */
2255 switch (val) {
2256 case 2:
2257 case 3:
2258 break;
2259 default:
2260 return -EINVAL;
2261 }
2262
2263 spin_lock_bh(&br->multicast_lock);
2264 br->multicast_igmp_version = val;
2265 spin_unlock_bh(&br->multicast_lock);
2266
2267 return 0;
2268}
2269
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01002270#if IS_ENABLED(CONFIG_IPV6)
2271int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val)
2272{
2273 /* Currently we support version 1 and 2 */
2274 switch (val) {
2275 case 1:
2276 case 2:
2277 break;
2278 default:
2279 return -EINVAL;
2280 }
2281
2282 spin_lock_bh(&br->multicast_lock);
2283 br->multicast_mld_version = val;
2284 spin_unlock_bh(&br->multicast_lock);
2285
2286 return 0;
2287}
2288#endif
2289
Linus Lüssing07f8ac42014-06-07 18:26:28 +02002290/**
2291 * br_multicast_list_adjacent - Returns snooped multicast addresses
2292 * @dev: The bridge port adjacent to which to retrieve addresses
2293 * @br_ip_list: The list to store found, snooped multicast IP addresses in
2294 *
2295 * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
2296 * snooping feature on all bridge ports of dev's bridge device, excluding
2297 * the addresses from dev itself.
2298 *
2299 * Returns the number of items added to br_ip_list.
2300 *
2301 * Notes:
2302 * - br_ip_list needs to be initialized by caller
2303 * - br_ip_list might contain duplicates in the end
2304 * (needs to be taken care of by caller)
2305 * - br_ip_list needs to be freed by caller
2306 */
2307int br_multicast_list_adjacent(struct net_device *dev,
2308 struct list_head *br_ip_list)
2309{
2310 struct net_bridge *br;
2311 struct net_bridge_port *port;
2312 struct net_bridge_port_group *group;
2313 struct br_ip_list *entry;
2314 int count = 0;
2315
2316 rcu_read_lock();
2317 if (!br_ip_list || !br_port_exists(dev))
2318 goto unlock;
2319
2320 port = br_port_get_rcu(dev);
2321 if (!port || !port->br)
2322 goto unlock;
2323
2324 br = port->br;
2325
2326 list_for_each_entry_rcu(port, &br->port_list, list) {
2327 if (!port->dev || port->dev == dev)
2328 continue;
2329
2330 hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
2331 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
2332 if (!entry)
2333 goto unlock;
2334
2335 entry->addr = group->addr;
2336 list_add(&entry->list, br_ip_list);
2337 count++;
2338 }
2339 }
2340
2341unlock:
2342 rcu_read_unlock();
2343 return count;
2344}
2345EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
Linus Lüssing2cd41432014-06-07 18:26:29 +02002346
2347/**
Linus Lüssingc34963e2014-07-07 05:41:17 +02002348 * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2349 * @dev: The bridge port providing the bridge on which to check for a querier
2350 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2351 *
2352 * Checks whether the given interface has a bridge on top and if so returns
2353 * true if a valid querier exists anywhere on the bridged link layer.
2354 * Otherwise returns false.
2355 */
2356bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
2357{
2358 struct net_bridge *br;
2359 struct net_bridge_port *port;
2360 struct ethhdr eth;
2361 bool ret = false;
2362
2363 rcu_read_lock();
2364 if (!br_port_exists(dev))
2365 goto unlock;
2366
2367 port = br_port_get_rcu(dev);
2368 if (!port || !port->br)
2369 goto unlock;
2370
2371 br = port->br;
2372
2373 memset(&eth, 0, sizeof(eth));
2374 eth.h_proto = htons(proto);
2375
2376 ret = br_multicast_querier_exists(br, &eth);
2377
2378unlock:
2379 rcu_read_unlock();
2380 return ret;
2381}
2382EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
2383
2384/**
Linus Lüssing2cd41432014-06-07 18:26:29 +02002385 * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
2386 * @dev: The bridge port adjacent to which to check for a querier
2387 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2388 *
2389 * Checks whether the given interface has a bridge on top and if so returns
2390 * true if a selected querier is behind one of the other ports of this
2391 * bridge. Otherwise returns false.
2392 */
2393bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
2394{
2395 struct net_bridge *br;
2396 struct net_bridge_port *port;
2397 bool ret = false;
2398
2399 rcu_read_lock();
2400 if (!br_port_exists(dev))
2401 goto unlock;
2402
2403 port = br_port_get_rcu(dev);
2404 if (!port || !port->br)
2405 goto unlock;
2406
2407 br = port->br;
2408
2409 switch (proto) {
2410 case ETH_P_IP:
2411 if (!timer_pending(&br->ip4_other_query.timer) ||
2412 rcu_dereference(br->ip4_querier.port) == port)
2413 goto unlock;
2414 break;
Linus Lüssing3993c4e2014-06-12 01:41:24 +02002415#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing2cd41432014-06-07 18:26:29 +02002416 case ETH_P_IPV6:
2417 if (!timer_pending(&br->ip6_other_query.timer) ||
2418 rcu_dereference(br->ip6_querier.port) == port)
2419 goto unlock;
2420 break;
Linus Lüssing3993c4e2014-06-12 01:41:24 +02002421#endif
Linus Lüssing2cd41432014-06-07 18:26:29 +02002422 default:
2423 goto unlock;
2424 }
2425
2426 ret = true;
2427unlock:
2428 rcu_read_unlock();
2429 return ret;
2430}
2431EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002432
2433static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002434 const struct sk_buff *skb, u8 type, u8 dir)
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002435{
2436 struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002437 __be16 proto = skb->protocol;
2438 unsigned int t_len;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002439
2440 u64_stats_update_begin(&pstats->syncp);
2441 switch (proto) {
2442 case htons(ETH_P_IP):
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002443 t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002444 switch (type) {
2445 case IGMP_HOST_MEMBERSHIP_REPORT:
2446 pstats->mstats.igmp_v1reports[dir]++;
2447 break;
2448 case IGMPV2_HOST_MEMBERSHIP_REPORT:
2449 pstats->mstats.igmp_v2reports[dir]++;
2450 break;
2451 case IGMPV3_HOST_MEMBERSHIP_REPORT:
2452 pstats->mstats.igmp_v3reports[dir]++;
2453 break;
2454 case IGMP_HOST_MEMBERSHIP_QUERY:
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002455 if (t_len != sizeof(struct igmphdr)) {
2456 pstats->mstats.igmp_v3queries[dir]++;
2457 } else {
2458 unsigned int offset = skb_transport_offset(skb);
2459 struct igmphdr *ih, _ihdr;
2460
2461 ih = skb_header_pointer(skb, offset,
2462 sizeof(_ihdr), &_ihdr);
2463 if (!ih)
2464 break;
2465 if (!ih->code)
2466 pstats->mstats.igmp_v1queries[dir]++;
2467 else
2468 pstats->mstats.igmp_v2queries[dir]++;
2469 }
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002470 break;
2471 case IGMP_HOST_LEAVE_MESSAGE:
2472 pstats->mstats.igmp_leaves[dir]++;
2473 break;
2474 }
2475 break;
2476#if IS_ENABLED(CONFIG_IPV6)
2477 case htons(ETH_P_IPV6):
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002478 t_len = ntohs(ipv6_hdr(skb)->payload_len) +
2479 sizeof(struct ipv6hdr);
2480 t_len -= skb_network_header_len(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002481 switch (type) {
2482 case ICMPV6_MGM_REPORT:
2483 pstats->mstats.mld_v1reports[dir]++;
2484 break;
2485 case ICMPV6_MLD2_REPORT:
2486 pstats->mstats.mld_v2reports[dir]++;
2487 break;
2488 case ICMPV6_MGM_QUERY:
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002489 if (t_len != sizeof(struct mld_msg))
2490 pstats->mstats.mld_v2queries[dir]++;
2491 else
2492 pstats->mstats.mld_v1queries[dir]++;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002493 break;
2494 case ICMPV6_MGM_REDUCTION:
2495 pstats->mstats.mld_leaves[dir]++;
2496 break;
2497 }
2498 break;
2499#endif /* CONFIG_IPV6 */
2500 }
2501 u64_stats_update_end(&pstats->syncp);
2502}
2503
2504void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002505 const struct sk_buff *skb, u8 type, u8 dir)
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002506{
2507 struct bridge_mcast_stats __percpu *stats;
2508
2509 /* if multicast_disabled is true then igmp type can't be set */
2510 if (!type || !br->multicast_stats_enabled)
2511 return;
2512
2513 if (p)
2514 stats = p->mcast_stats;
2515 else
2516 stats = br->mcast_stats;
2517 if (WARN_ON(!stats))
2518 return;
2519
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002520 br_mcast_stats_add(stats, skb, type, dir);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002521}
2522
2523int br_multicast_init_stats(struct net_bridge *br)
2524{
2525 br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
2526 if (!br->mcast_stats)
2527 return -ENOMEM;
2528
2529 return 0;
2530}
2531
Ido Schimmelb6fe0442017-04-10 14:59:27 +03002532void br_multicast_uninit_stats(struct net_bridge *br)
2533{
2534 free_percpu(br->mcast_stats);
2535}
2536
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002537static void mcast_stats_add_dir(u64 *dst, u64 *src)
2538{
2539 dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
2540 dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
2541}
2542
2543void br_multicast_get_stats(const struct net_bridge *br,
2544 const struct net_bridge_port *p,
2545 struct br_mcast_stats *dest)
2546{
2547 struct bridge_mcast_stats __percpu *stats;
2548 struct br_mcast_stats tdst;
2549 int i;
2550
2551 memset(dest, 0, sizeof(*dest));
2552 if (p)
2553 stats = p->mcast_stats;
2554 else
2555 stats = br->mcast_stats;
2556 if (WARN_ON(!stats))
2557 return;
2558
2559 memset(&tdst, 0, sizeof(tdst));
2560 for_each_possible_cpu(i) {
2561 struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
2562 struct br_mcast_stats temp;
2563 unsigned int start;
2564
2565 do {
2566 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
2567 memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
2568 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
2569
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002570 mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
2571 mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
2572 mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002573 mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
2574 mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
2575 mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
2576 mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
2577 tdst.igmp_parse_errors += temp.igmp_parse_errors;
2578
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002579 mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
2580 mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002581 mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
2582 mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
2583 mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
2584 tdst.mld_parse_errors += temp.mld_parse_errors;
2585 }
2586 memcpy(dest, &tdst, sizeof(*dest));
2587}