blob: 2add6d417aa420fa47613a945bb50e7852116cdc [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
stephen hemminger7e80c122010-04-27 15:01:04 +00001320/*
Cong Wang7c776022013-06-21 15:37:25 +08001321 * Add port to router_list
stephen hemminger7e80c122010-04-27 15:01:04 +00001322 * list is maintained ordered by pointer value
1323 * and locked by br->multicast_lock and RCU
1324 */
Herbert Xu0909e112010-02-27 19:41:49 +00001325static void br_multicast_add_router(struct net_bridge *br,
1326 struct net_bridge_port *port)
1327{
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001328 struct net_bridge_port *p;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001329 struct hlist_node *slot = NULL;
Herbert Xu0909e112010-02-27 19:41:49 +00001330
Nikolay Aleksandrov1a040ea2015-06-09 10:23:57 -07001331 if (!hlist_unhashed(&port->rlist))
1332 return;
1333
Sasha Levinb67bfe02013-02-27 17:06:00 -08001334 hlist_for_each_entry(p, &br->router_list, rlist) {
stephen hemminger7e80c122010-04-27 15:01:04 +00001335 if ((unsigned long) port >= (unsigned long) p)
1336 break;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001337 slot = &p->rlist;
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001338 }
Herbert Xu0909e112010-02-27 19:41:49 +00001339
stephen hemminger7e80c122010-04-27 15:01:04 +00001340 if (slot)
Ken Helias1d023282014-08-06 16:09:16 -07001341 hlist_add_behind_rcu(&port->rlist, slot);
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001342 else
1343 hlist_add_head_rcu(&port->rlist, &br->router_list);
Satish Ashok949f1e32015-07-23 05:00:53 -07001344 br_rtr_notify(br->dev, port, RTM_NEWMDB);
Herbert Xu0909e112010-02-27 19:41:49 +00001345}
1346
Herbert Xueb1d1642010-02-27 19:41:45 +00001347static void br_multicast_mark_router(struct net_bridge *br,
1348 struct net_bridge_port *port)
1349{
1350 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +00001351
1352 if (!port) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001353 if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY)
Herbert Xueb1d1642010-02-27 19:41:45 +00001354 mod_timer(&br->multicast_router_timer,
1355 now + br->multicast_querier_interval);
1356 return;
1357 }
1358
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001359 if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
1360 port->multicast_router == MDB_RTR_TYPE_PERM)
Herbert Xueb1d1642010-02-27 19:41:45 +00001361 return;
1362
Herbert Xu0909e112010-02-27 19:41:49 +00001363 br_multicast_add_router(br, port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001364
Herbert Xueb1d1642010-02-27 19:41:45 +00001365 mod_timer(&port->multicast_router_timer,
1366 now + br->multicast_querier_interval);
1367}
1368
1369static void br_multicast_query_received(struct net_bridge *br,
1370 struct net_bridge_port *port,
Linus Lüssing90010b32014-06-07 18:26:26 +02001371 struct bridge_mcast_other_query *query,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001372 struct br_ip *saddr,
Linus Lüssingb00589a2013-08-01 01:06:20 +02001373 unsigned long max_delay)
Herbert Xueb1d1642010-02-27 19:41:45 +00001374{
Linus Lüssing2cd41432014-06-07 18:26:29 +02001375 if (!br_multicast_select_querier(br, port, saddr))
Herbert Xueb1d1642010-02-27 19:41:45 +00001376 return;
1377
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001378 br_multicast_update_query_timer(br, query, max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001379 br_multicast_mark_router(br, port);
1380}
1381
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001382static int br_ip4_multicast_query(struct net_bridge *br,
1383 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001384 struct sk_buff *skb,
1385 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001386{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001387 const struct iphdr *iph = ip_hdr(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001388 struct igmphdr *ih = igmp_hdr(skb);
1389 struct net_bridge_mdb_entry *mp;
1390 struct igmpv3_query *ih3;
1391 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +00001392 struct net_bridge_port_group __rcu **pp;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001393 struct br_ip saddr;
Herbert Xueb1d1642010-02-27 19:41:45 +00001394 unsigned long max_delay;
1395 unsigned long now = jiffies;
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001396 unsigned int offset = skb_transport_offset(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001397 __be32 group;
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001398 int err = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001399
1400 spin_lock(&br->multicast_lock);
1401 if (!netif_running(br->dev) ||
1402 (port && port->state == BR_STATE_DISABLED))
1403 goto out;
1404
Herbert Xueb1d1642010-02-27 19:41:45 +00001405 group = ih->group;
1406
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001407 if (skb->len == offset + sizeof(*ih)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001408 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1409
1410 if (!max_delay) {
1411 max_delay = 10 * HZ;
1412 group = 0;
1413 }
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001414 } else if (skb->len >= offset + sizeof(*ih3)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001415 ih3 = igmpv3_query_hdr(skb);
1416 if (ih3->nsrcs)
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001417 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001418
YOSHIFUJI Hideaki / 吉藤英明0ba8c9e2010-03-15 19:27:00 +00001419 max_delay = ih3->code ?
1420 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001421 } else {
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001422 goto out;
1423 }
1424
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001425 if (!group) {
1426 saddr.proto = htons(ETH_P_IP);
1427 saddr.u.ip4 = iph->saddr;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001428
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001429 br_multicast_query_received(br, port, &br->ip4_other_query,
1430 &saddr, max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001431 goto out;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001432 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001433
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001434 mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001435 if (!mp)
1436 goto out;
1437
1438 max_delay *= br->multicast_last_member_count;
1439
Herbert Xu8a870172011-02-12 01:05:42 -08001440 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001441 (timer_pending(&mp->timer) ?
1442 time_after(mp->timer.expires, now + max_delay) :
1443 try_to_del_timer_sync(&mp->timer) >= 0))
1444 mod_timer(&mp->timer, now + max_delay);
1445
Eric Dumazete8051682010-11-15 06:38:10 +00001446 for (pp = &mp->ports;
1447 (p = mlock_dereference(*pp, br)) != NULL;
1448 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001449 if (timer_pending(&p->timer) ?
1450 time_after(p->timer.expires, now + max_delay) :
1451 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001452 mod_timer(&p->timer, now + max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001453 }
1454
1455out:
1456 spin_unlock(&br->multicast_lock);
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001457 return err;
Herbert Xueb1d1642010-02-27 19:41:45 +00001458}
1459
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001460#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001461static int br_ip6_multicast_query(struct net_bridge *br,
1462 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001463 struct sk_buff *skb,
1464 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001465{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001466 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
stephen hemmingereca2a432012-12-13 06:51:28 +00001467 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001468 struct net_bridge_mdb_entry *mp;
1469 struct mld2_query *mld2q;
Eric Dumazete8051682010-11-15 06:38:10 +00001470 struct net_bridge_port_group *p;
1471 struct net_bridge_port_group __rcu **pp;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001472 struct br_ip saddr;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001473 unsigned long max_delay;
1474 unsigned long now = jiffies;
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001475 unsigned int offset = skb_transport_offset(skb);
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001476 const struct in6_addr *group = NULL;
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001477 bool is_general_query;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001478 int err = 0;
1479
1480 spin_lock(&br->multicast_lock);
1481 if (!netif_running(br->dev) ||
1482 (port && port->state == BR_STATE_DISABLED))
1483 goto out;
1484
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001485 if (skb->len == offset + sizeof(*mld)) {
1486 if (!pskb_may_pull(skb, offset + sizeof(*mld))) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001487 err = -EINVAL;
1488 goto out;
1489 }
1490 mld = (struct mld_msg *) icmp6_hdr(skb);
Li RongQing47152132012-07-09 23:56:12 +00001491 max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001492 if (max_delay)
1493 group = &mld->mld_mca;
Linus Lüssing248ba8e2013-08-06 00:32:05 +02001494 } else {
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001495 if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001496 err = -EINVAL;
1497 goto out;
1498 }
1499 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1500 if (!mld2q->mld2q_nsrcs)
1501 group = &mld2q->mld2q_mca;
Daniel Borkmanne3f5b172013-09-04 00:19:39 +02001502
1503 max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001504 }
1505
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001506 is_general_query = group && ipv6_addr_any(group);
1507
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001508 if (is_general_query) {
1509 saddr.proto = htons(ETH_P_IPV6);
1510 saddr.u.ip6 = ip6h->saddr;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001511
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001512 br_multicast_query_received(br, port, &br->ip6_other_query,
1513 &saddr, max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001514 goto out;
Linus Lüssing6c03ee82014-06-12 01:41:23 +02001515 } else if (!group) {
1516 goto out;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001517 }
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001518
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001519 mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001520 if (!mp)
1521 goto out;
1522
1523 max_delay *= br->multicast_last_member_count;
Herbert Xu8a870172011-02-12 01:05:42 -08001524 if (mp->mglist &&
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001525 (timer_pending(&mp->timer) ?
1526 time_after(mp->timer.expires, now + max_delay) :
1527 try_to_del_timer_sync(&mp->timer) >= 0))
1528 mod_timer(&mp->timer, now + max_delay);
1529
Eric Dumazete8051682010-11-15 06:38:10 +00001530 for (pp = &mp->ports;
1531 (p = mlock_dereference(*pp, br)) != NULL;
1532 pp = &p->next) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001533 if (timer_pending(&p->timer) ?
1534 time_after(p->timer.expires, now + max_delay) :
1535 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001536 mod_timer(&p->timer, now + max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001537 }
1538
1539out:
1540 spin_unlock(&br->multicast_lock);
1541 return err;
1542}
1543#endif
1544
Linus Lüssing90010b32014-06-07 18:26:26 +02001545static void
1546br_multicast_leave_group(struct net_bridge *br,
1547 struct net_bridge_port *port,
1548 struct br_ip *group,
1549 struct bridge_mcast_other_query *other_query,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001550 struct bridge_mcast_own_query *own_query,
1551 const unsigned char *src)
Herbert Xueb1d1642010-02-27 19:41:45 +00001552{
1553 struct net_bridge_mdb_htable *mdb;
1554 struct net_bridge_mdb_entry *mp;
1555 struct net_bridge_port_group *p;
1556 unsigned long now;
1557 unsigned long time;
1558
Herbert Xueb1d1642010-02-27 19:41:45 +00001559 spin_lock(&br->multicast_lock);
1560 if (!netif_running(br->dev) ||
Satish Ashok544586f2015-07-28 03:28:27 -07001561 (port && port->state == BR_STATE_DISABLED))
Herbert Xueb1d1642010-02-27 19:41:45 +00001562 goto out;
1563
Eric Dumazete8051682010-11-15 06:38:10 +00001564 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001565 mp = br_mdb_ip_get(mdb, group);
1566 if (!mp)
1567 goto out;
1568
Satish Ashok544586f2015-07-28 03:28:27 -07001569 if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
1570 struct net_bridge_port_group __rcu **pp;
1571
1572 for (pp = &mp->ports;
1573 (p = mlock_dereference(*pp, br)) != NULL;
1574 pp = &p->next) {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001575 if (!br_port_group_equal(p, port, src))
Satish Ashok544586f2015-07-28 03:28:27 -07001576 continue;
1577
1578 rcu_assign_pointer(*pp, p->next);
1579 hlist_del_init(&p->mglist);
1580 del_timer(&p->timer);
1581 call_rcu_bh(&p->rcu, br_multicast_free_pg);
Elad Raz45ebcce2016-04-21 12:52:45 +02001582 br_mdb_notify(br->dev, port, group, RTM_DELMDB,
1583 p->flags);
Satish Ashok544586f2015-07-28 03:28:27 -07001584
1585 if (!mp->ports && !mp->mglist &&
1586 netif_running(br->dev))
1587 mod_timer(&mp->timer, jiffies);
1588 }
1589 goto out;
1590 }
1591
1592 if (timer_pending(&other_query->timer))
1593 goto out;
1594
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001595 if (br->multicast_querier) {
Cong Wang6b7df112013-05-21 21:52:56 +00001596 __br_multicast_send_query(br, port, &mp->addr);
1597
1598 time = jiffies + br->multicast_last_member_count *
1599 br->multicast_last_member_interval;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001600
Linus Lüssing90010b32014-06-07 18:26:26 +02001601 mod_timer(&own_query->timer, time);
Cong Wang6b7df112013-05-21 21:52:56 +00001602
1603 for (p = mlock_dereference(mp->ports, br);
1604 p != NULL;
1605 p = mlock_dereference(p->next, br)) {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001606 if (!br_port_group_equal(p, port, src))
Cong Wang6b7df112013-05-21 21:52:56 +00001607 continue;
1608
1609 if (!hlist_unhashed(&p->mglist) &&
1610 (timer_pending(&p->timer) ?
1611 time_after(p->timer.expires, time) :
1612 try_to_del_timer_sync(&p->timer) >= 0)) {
1613 mod_timer(&p->timer, time);
1614 }
1615
1616 break;
1617 }
1618 }
1619
Herbert Xueb1d1642010-02-27 19:41:45 +00001620 now = jiffies;
1621 time = now + br->multicast_last_member_count *
1622 br->multicast_last_member_interval;
1623
1624 if (!port) {
Linus Lüssing454594f2013-10-20 00:58:57 +02001625 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001626 (timer_pending(&mp->timer) ?
1627 time_after(mp->timer.expires, time) :
1628 try_to_del_timer_sync(&mp->timer) >= 0)) {
1629 mod_timer(&mp->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +00001630 }
Linus Lüssing454594f2013-10-20 00:58:57 +02001631
1632 goto out;
1633 }
1634
1635 for (p = mlock_dereference(mp->ports, br);
1636 p != NULL;
1637 p = mlock_dereference(p->next, br)) {
1638 if (p->port != port)
1639 continue;
1640
1641 if (!hlist_unhashed(&p->mglist) &&
1642 (timer_pending(&p->timer) ?
1643 time_after(p->timer.expires, time) :
1644 try_to_del_timer_sync(&p->timer) >= 0)) {
1645 mod_timer(&p->timer, time);
1646 }
1647
1648 break;
Herbert Xueb1d1642010-02-27 19:41:45 +00001649 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001650out:
1651 spin_unlock(&br->multicast_lock);
1652}
1653
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001654static void br_ip4_multicast_leave_group(struct net_bridge *br,
1655 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001656 __be32 group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001657 __u16 vid,
1658 const unsigned char *src)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001659{
1660 struct br_ip br_group;
Linus Lüssing90010b32014-06-07 18:26:26 +02001661 struct bridge_mcast_own_query *own_query;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001662
1663 if (ipv4_is_local_multicast(group))
1664 return;
1665
Linus Lüssing90010b32014-06-07 18:26:26 +02001666 own_query = port ? &port->ip4_own_query : &br->ip4_own_query;
1667
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001668 br_group.u.ip4 = group;
1669 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001670 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001671
Linus Lüssing90010b32014-06-07 18:26:26 +02001672 br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001673 own_query, src);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001674}
1675
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001676#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001677static void br_ip6_multicast_leave_group(struct net_bridge *br,
1678 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001679 const struct in6_addr *group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001680 __u16 vid,
1681 const unsigned char *src)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001682{
1683 struct br_ip br_group;
Linus Lüssing90010b32014-06-07 18:26:26 +02001684 struct bridge_mcast_own_query *own_query;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001685
Linus Lüssing3c3769e2013-09-04 02:13:39 +02001686 if (ipv6_addr_is_ll_all_nodes(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001687 return;
1688
Linus Lüssing90010b32014-06-07 18:26:26 +02001689 own_query = port ? &port->ip6_own_query : &br->ip6_own_query;
1690
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001691 br_group.u.ip6 = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001692 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001693 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001694
Linus Lüssing90010b32014-06-07 18:26:26 +02001695 br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001696 own_query, src);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001697}
1698#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001699
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001700static void br_multicast_err_count(const struct net_bridge *br,
1701 const struct net_bridge_port *p,
1702 __be16 proto)
1703{
1704 struct bridge_mcast_stats __percpu *stats;
1705 struct bridge_mcast_stats *pstats;
1706
1707 if (!br->multicast_stats_enabled)
1708 return;
1709
1710 if (p)
1711 stats = p->mcast_stats;
1712 else
1713 stats = br->mcast_stats;
1714 if (WARN_ON(!stats))
1715 return;
1716
1717 pstats = this_cpu_ptr(stats);
1718
1719 u64_stats_update_begin(&pstats->syncp);
1720 switch (proto) {
1721 case htons(ETH_P_IP):
1722 pstats->mstats.igmp_parse_errors++;
1723 break;
1724#if IS_ENABLED(CONFIG_IPV6)
1725 case htons(ETH_P_IPV6):
1726 pstats->mstats.mld_parse_errors++;
1727 break;
1728#endif
1729 }
1730 u64_stats_update_end(&pstats->syncp);
1731}
1732
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001733static void br_multicast_pim(struct net_bridge *br,
1734 struct net_bridge_port *port,
1735 const struct sk_buff *skb)
1736{
1737 unsigned int offset = skb_transport_offset(skb);
1738 struct pimhdr *pimhdr, _pimhdr;
1739
1740 pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr);
1741 if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION ||
1742 pim_hdr_type(pimhdr) != PIM_TYPE_HELLO)
1743 return;
1744
1745 br_multicast_mark_router(br, port);
1746}
1747
Herbert Xueb1d1642010-02-27 19:41:45 +00001748static int br_multicast_ipv4_rcv(struct net_bridge *br,
1749 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001750 struct sk_buff *skb,
1751 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001752{
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001753 struct sk_buff *skb_trimmed = NULL;
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001754 const unsigned char *src;
Herbert Xueb1d1642010-02-27 19:41:45 +00001755 struct igmphdr *ih;
Herbert Xueb1d1642010-02-27 19:41:45 +00001756 int err;
1757
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001758 err = ip_mc_check_igmp(skb, &skb_trimmed);
Herbert Xueb1d1642010-02-27 19:41:45 +00001759
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001760 if (err == -ENOMSG) {
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001761 if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) {
Herbert Xubd4265f2011-06-23 02:39:12 +00001762 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001763 } else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) {
1764 if (ip_hdr(skb)->protocol == IPPROTO_PIM)
1765 br_multicast_pim(br, port, skb);
1766 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001767 return 0;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001768 } else if (err < 0) {
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001769 br_multicast_err_count(br, port, skb->protocol);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001770 return err;
Herbert Xubd4265f2011-06-23 02:39:12 +00001771 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001772
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001773 ih = igmp_hdr(skb);
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001774 src = eth_hdr(skb)->h_source;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001775 BR_INPUT_SKB_CB(skb)->igmp = ih->type;
Herbert Xueb1d1642010-02-27 19:41:45 +00001776
1777 switch (ih->type) {
1778 case IGMP_HOST_MEMBERSHIP_REPORT:
1779 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Fernando Luis Vázquez Cao62b2bcb2011-06-13 15:04:43 +00001780 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001781 err = br_ip4_multicast_add_group(br, port, ih->group, vid, src);
Herbert Xueb1d1642010-02-27 19:41:45 +00001782 break;
1783 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001784 err = br_ip4_multicast_igmp3_report(br, port, skb_trimmed, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001785 break;
1786 case IGMP_HOST_MEMBERSHIP_QUERY:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001787 err = br_ip4_multicast_query(br, port, skb_trimmed, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001788 break;
1789 case IGMP_HOST_LEAVE_MESSAGE:
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001790 br_ip4_multicast_leave_group(br, port, ih->group, vid, src);
Herbert Xueb1d1642010-02-27 19:41:45 +00001791 break;
1792 }
1793
Linus Lüssinga5169932015-08-13 05:54:07 +02001794 if (skb_trimmed && skb_trimmed != skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001795 kfree_skb(skb_trimmed);
1796
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07001797 br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001798 BR_MCAST_DIR_RX);
1799
Herbert Xueb1d1642010-02-27 19:41:45 +00001800 return err;
1801}
1802
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001803#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001804static int br_multicast_ipv6_rcv(struct net_bridge *br,
1805 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001806 struct sk_buff *skb,
1807 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001808{
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001809 struct sk_buff *skb_trimmed = NULL;
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001810 const unsigned char *src;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001811 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001812 int err;
1813
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001814 err = ipv6_mc_check_mld(skb, &skb_trimmed);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001815
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001816 if (err == -ENOMSG) {
1817 if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
1818 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Linus Lüssing8fad9c32013-09-04 02:13:38 +02001819 return 0;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001820 } else if (err < 0) {
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001821 br_multicast_err_count(br, port, skb->protocol);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001822 return err;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001823 }
1824
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001825 mld = (struct mld_msg *)skb_transport_header(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001826 BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001827
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001828 switch (mld->mld_type) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001829 case ICMPV6_MGM_REPORT:
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001830 src = eth_hdr(skb)->h_source;
Fernando Luis Vázquez Caofc2af6c2011-06-13 15:06:58 +00001831 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001832 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid,
1833 src);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001834 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001835 case ICMPV6_MLD2_REPORT:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001836 err = br_ip6_multicast_mld2_report(br, port, skb_trimmed, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001837 break;
1838 case ICMPV6_MGM_QUERY:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001839 err = br_ip6_multicast_query(br, port, skb_trimmed, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001840 break;
1841 case ICMPV6_MGM_REDUCTION:
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001842 src = eth_hdr(skb)->h_source;
1843 br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001844 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001845 }
1846
Linus Lüssinga5169932015-08-13 05:54:07 +02001847 if (skb_trimmed && skb_trimmed != skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001848 kfree_skb(skb_trimmed);
1849
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07001850 br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001851 BR_MCAST_DIR_RX);
1852
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001853 return err;
1854}
1855#endif
1856
Herbert Xueb1d1642010-02-27 19:41:45 +00001857int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001858 struct sk_buff *skb, u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001859{
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001860 int ret = 0;
1861
YOSHIFUJI Hideaki / 吉藤英明1fafc7a2010-04-25 08:06:40 +00001862 BR_INPUT_SKB_CB(skb)->igmp = 0;
1863 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1864
Herbert Xueb1d1642010-02-27 19:41:45 +00001865 if (br->multicast_disabled)
1866 return 0;
1867
1868 switch (skb->protocol) {
1869 case htons(ETH_P_IP):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001870 ret = br_multicast_ipv4_rcv(br, port, skb, vid);
1871 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001872#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001873 case htons(ETH_P_IPV6):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001874 ret = br_multicast_ipv6_rcv(br, port, skb, vid);
1875 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001876#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001877 }
1878
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001879 return ret;
Herbert Xueb1d1642010-02-27 19:41:45 +00001880}
1881
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001882static void br_multicast_query_expired(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001883 struct bridge_mcast_own_query *query,
1884 struct bridge_mcast_querier *querier)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001885{
1886 spin_lock(&br->multicast_lock);
1887 if (query->startup_sent < br->multicast_startup_query_count)
1888 query->startup_sent++;
1889
Eric Dumazet71d9f612015-05-28 04:42:54 -07001890 RCU_INIT_POINTER(querier->port, NULL);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001891 br_multicast_send_query(br, NULL, query);
1892 spin_unlock(&br->multicast_lock);
1893}
1894
1895static void br_ip4_multicast_query_expired(unsigned long data)
Herbert Xueb1d1642010-02-27 19:41:45 +00001896{
1897 struct net_bridge *br = (void *)data;
1898
Linus Lüssing2cd41432014-06-07 18:26:29 +02001899 br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier);
Herbert Xueb1d1642010-02-27 19:41:45 +00001900}
1901
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001902#if IS_ENABLED(CONFIG_IPV6)
1903static void br_ip6_multicast_query_expired(unsigned long data)
1904{
1905 struct net_bridge *br = (void *)data;
1906
Linus Lüssing2cd41432014-06-07 18:26:29 +02001907 br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001908}
1909#endif
1910
Herbert Xueb1d1642010-02-27 19:41:45 +00001911void br_multicast_init(struct net_bridge *br)
1912{
1913 br->hash_elasticity = 4;
1914 br->hash_max = 512;
1915
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001916 br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Herbert Xuc5c23262012-04-13 02:37:42 +00001917 br->multicast_querier = 0;
Cong Wang1c8ad5b2013-05-21 21:52:54 +00001918 br->multicast_query_use_ifaddr = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001919 br->multicast_last_member_count = 2;
1920 br->multicast_startup_query_count = 2;
1921
1922 br->multicast_last_member_interval = HZ;
1923 br->multicast_query_response_interval = 10 * HZ;
1924 br->multicast_startup_query_interval = 125 * HZ / 4;
1925 br->multicast_query_interval = 125 * HZ;
1926 br->multicast_querier_interval = 255 * HZ;
1927 br->multicast_membership_interval = 260 * HZ;
1928
Linus Lüssing90010b32014-06-07 18:26:26 +02001929 br->ip4_other_query.delay_time = 0;
Linus Lüssing2cd41432014-06-07 18:26:29 +02001930 br->ip4_querier.port = NULL;
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001931 br->multicast_igmp_version = 2;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001932#if IS_ENABLED(CONFIG_IPV6)
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001933 br->multicast_mld_version = 1;
Linus Lüssing90010b32014-06-07 18:26:26 +02001934 br->ip6_other_query.delay_time = 0;
Linus Lüssing2cd41432014-06-07 18:26:29 +02001935 br->ip6_querier.port = NULL;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001936#endif
daniel0888d5f2016-06-24 12:35:18 +02001937 br->has_ipv6_addr = 1;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001938
Herbert Xueb1d1642010-02-27 19:41:45 +00001939 spin_lock_init(&br->multicast_lock);
1940 setup_timer(&br->multicast_router_timer,
1941 br_multicast_local_router_expired, 0);
Linus Lüssing90010b32014-06-07 18:26:26 +02001942 setup_timer(&br->ip4_other_query.timer,
1943 br_ip4_multicast_querier_expired, (unsigned long)br);
1944 setup_timer(&br->ip4_own_query.timer, br_ip4_multicast_query_expired,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001945 (unsigned long)br);
1946#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001947 setup_timer(&br->ip6_other_query.timer,
1948 br_ip6_multicast_querier_expired, (unsigned long)br);
1949 setup_timer(&br->ip6_own_query.timer, br_ip6_multicast_query_expired,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001950 (unsigned long)br);
1951#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001952}
1953
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001954static void __br_multicast_open(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +02001955 struct bridge_mcast_own_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +00001956{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001957 query->startup_sent = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001958
1959 if (br->multicast_disabled)
1960 return;
1961
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001962 mod_timer(&query->timer, jiffies);
1963}
1964
1965void br_multicast_open(struct net_bridge *br)
1966{
Linus Lüssing90010b32014-06-07 18:26:26 +02001967 __br_multicast_open(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001968#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001969 __br_multicast_open(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001970#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001971}
1972
1973void br_multicast_stop(struct net_bridge *br)
1974{
Herbert Xueb1d1642010-02-27 19:41:45 +00001975 del_timer_sync(&br->multicast_router_timer);
Linus Lüssing90010b32014-06-07 18:26:26 +02001976 del_timer_sync(&br->ip4_other_query.timer);
1977 del_timer_sync(&br->ip4_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001978#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001979 del_timer_sync(&br->ip6_other_query.timer);
1980 del_timer_sync(&br->ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001981#endif
Satish Ashoke10177a2015-07-15 07:16:51 -07001982}
1983
1984void br_multicast_dev_del(struct net_bridge *br)
1985{
1986 struct net_bridge_mdb_htable *mdb;
1987 struct net_bridge_mdb_entry *mp;
1988 struct hlist_node *n;
1989 u32 ver;
1990 int i;
Herbert Xueb1d1642010-02-27 19:41:45 +00001991
1992 spin_lock_bh(&br->multicast_lock);
Eric Dumazete8051682010-11-15 06:38:10 +00001993 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001994 if (!mdb)
1995 goto out;
1996
1997 br->mdb = NULL;
1998
1999 ver = mdb->ver;
2000 for (i = 0; i < mdb->max; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08002001 hlist_for_each_entry_safe(mp, n, &mdb->mhash[i],
Herbert Xueb1d1642010-02-27 19:41:45 +00002002 hlist[ver]) {
2003 del_timer(&mp->timer);
Herbert Xueb1d1642010-02-27 19:41:45 +00002004 call_rcu_bh(&mp->rcu, br_multicast_free_group);
2005 }
2006 }
2007
2008 if (mdb->old) {
2009 spin_unlock_bh(&br->multicast_lock);
Herbert Xu10cc2b52010-03-05 21:03:35 +00002010 rcu_barrier_bh();
Herbert Xueb1d1642010-02-27 19:41:45 +00002011 spin_lock_bh(&br->multicast_lock);
2012 WARN_ON(mdb->old);
2013 }
2014
2015 mdb->old = mdb;
2016 call_rcu_bh(&mdb->rcu, br_mdb_free);
2017
2018out:
2019 spin_unlock_bh(&br->multicast_lock);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002020
2021 free_percpu(br->mcast_stats);
Herbert Xueb1d1642010-02-27 19:41:45 +00002022}
Herbert Xu0909e112010-02-27 19:41:49 +00002023
2024int br_multicast_set_router(struct net_bridge *br, unsigned long val)
2025{
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02002026 int err = -EINVAL;
Herbert Xu0909e112010-02-27 19:41:49 +00002027
2028 spin_lock_bh(&br->multicast_lock);
Herbert Xu0909e112010-02-27 19:41:49 +00002029
2030 switch (val) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002031 case MDB_RTR_TYPE_DISABLED:
2032 case MDB_RTR_TYPE_PERM:
Herbert Xu0909e112010-02-27 19:41:49 +00002033 del_timer(&br->multicast_router_timer);
2034 /* fall through */
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002035 case MDB_RTR_TYPE_TEMP_QUERY:
Herbert Xu0909e112010-02-27 19:41:49 +00002036 br->multicast_router = val;
2037 err = 0;
2038 break;
Herbert Xu0909e112010-02-27 19:41:49 +00002039 }
2040
Herbert Xu0909e112010-02-27 19:41:49 +00002041 spin_unlock_bh(&br->multicast_lock);
2042
2043 return err;
2044}
2045
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002046static void __del_port_router(struct net_bridge_port *p)
2047{
2048 if (hlist_unhashed(&p->rlist))
2049 return;
2050 hlist_del_init_rcu(&p->rlist);
2051 br_rtr_notify(p->br->dev, p, RTM_DELMDB);
Nogah Frankelf12e7d92017-02-09 14:54:41 +01002052
2053 /* don't allow timer refresh */
2054 if (p->multicast_router == MDB_RTR_TYPE_TEMP)
2055 p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002056}
2057
Herbert Xu0909e112010-02-27 19:41:49 +00002058int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
2059{
2060 struct net_bridge *br = p->br;
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01002061 unsigned long now = jiffies;
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02002062 int err = -EINVAL;
Herbert Xu0909e112010-02-27 19:41:49 +00002063
2064 spin_lock(&br->multicast_lock);
Nikolay Aleksandrov4950cfd2016-02-26 21:20:02 +01002065 if (p->multicast_router == val) {
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01002066 /* Refresh the temp router port timer */
2067 if (p->multicast_router == MDB_RTR_TYPE_TEMP)
2068 mod_timer(&p->multicast_router_timer,
2069 now + br->multicast_querier_interval);
Nikolay Aleksandrov4950cfd2016-02-26 21:20:02 +01002070 err = 0;
2071 goto unlock;
2072 }
Herbert Xu0909e112010-02-27 19:41:49 +00002073 switch (val) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002074 case MDB_RTR_TYPE_DISABLED:
2075 p->multicast_router = MDB_RTR_TYPE_DISABLED;
2076 __del_port_router(p);
Herbert Xu0909e112010-02-27 19:41:49 +00002077 del_timer(&p->multicast_router_timer);
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002078 break;
2079 case MDB_RTR_TYPE_TEMP_QUERY:
2080 p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
2081 __del_port_router(p);
2082 break;
2083 case MDB_RTR_TYPE_PERM:
2084 p->multicast_router = MDB_RTR_TYPE_PERM;
2085 del_timer(&p->multicast_router_timer);
Herbert Xu0909e112010-02-27 19:41:49 +00002086 br_multicast_add_router(br, p);
2087 break;
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01002088 case MDB_RTR_TYPE_TEMP:
2089 p->multicast_router = MDB_RTR_TYPE_TEMP;
2090 br_multicast_mark_router(br, p);
2091 break;
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002092 default:
2093 goto unlock;
Herbert Xu0909e112010-02-27 19:41:49 +00002094 }
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002095 err = 0;
2096unlock:
Herbert Xu0909e112010-02-27 19:41:49 +00002097 spin_unlock(&br->multicast_lock);
2098
2099 return err;
2100}
Herbert Xu561f1102010-02-27 19:41:50 +00002101
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002102static void br_multicast_start_querier(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +02002103 struct bridge_mcast_own_query *query)
Herbert Xu561f1102010-02-27 19:41:50 +00002104{
2105 struct net_bridge_port *port;
Herbert Xu74857212012-04-13 02:37:42 +00002106
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002107 __br_multicast_open(br, query);
Herbert Xu74857212012-04-13 02:37:42 +00002108
2109 list_for_each_entry(port, &br->port_list, list) {
2110 if (port->state == BR_STATE_DISABLED ||
2111 port->state == BR_STATE_BLOCKING)
2112 continue;
2113
Linus Lüssing90010b32014-06-07 18:26:26 +02002114 if (query == &br->ip4_own_query)
2115 br_multicast_enable(&port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002116#if IS_ENABLED(CONFIG_IPV6)
2117 else
Linus Lüssing90010b32014-06-07 18:26:26 +02002118 br_multicast_enable(&port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002119#endif
Herbert Xu74857212012-04-13 02:37:42 +00002120 }
2121}
2122
2123int br_multicast_toggle(struct net_bridge *br, unsigned long val)
2124{
Eric Dumazete8051682010-11-15 06:38:10 +00002125 struct net_bridge_mdb_htable *mdb;
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02002126 struct net_bridge_port *port;
2127 int err = 0;
Herbert Xu561f1102010-02-27 19:41:50 +00002128
Andrey Vaginef5e0d82011-11-10 05:48:03 +00002129 spin_lock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00002130 if (br->multicast_disabled == !val)
2131 goto unlock;
2132
Nogah Frankel147c1e92017-02-09 14:54:40 +01002133 br_mc_disabled_update(br->dev, !val);
Herbert Xu561f1102010-02-27 19:41:50 +00002134 br->multicast_disabled = !val;
2135 if (br->multicast_disabled)
2136 goto unlock;
2137
Herbert Xu3a7fda02010-07-29 00:45:30 +00002138 if (!netif_running(br->dev))
2139 goto unlock;
2140
Eric Dumazete8051682010-11-15 06:38:10 +00002141 mdb = mlock_dereference(br->mdb, br);
2142 if (mdb) {
2143 if (mdb->old) {
Herbert Xu561f1102010-02-27 19:41:50 +00002144 err = -EEXIST;
2145rollback:
2146 br->multicast_disabled = !!val;
2147 goto unlock;
2148 }
2149
Eric Dumazete8051682010-11-15 06:38:10 +00002150 err = br_mdb_rehash(&br->mdb, mdb->max,
Herbert Xu561f1102010-02-27 19:41:50 +00002151 br->hash_elasticity);
2152 if (err)
2153 goto rollback;
2154 }
2155
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02002156 br_multicast_open(br);
2157 list_for_each_entry(port, &br->port_list, list)
2158 __br_multicast_enable_port(port);
Herbert Xu561f1102010-02-27 19:41:50 +00002159
2160unlock:
Andrey Vaginef5e0d82011-11-10 05:48:03 +00002161 spin_unlock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00002162
2163 return err;
2164}
Herbert Xub1951672010-02-27 19:41:51 +00002165
Herbert Xuc5c23262012-04-13 02:37:42 +00002166int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
2167{
Linus Lüssingb00589a2013-08-01 01:06:20 +02002168 unsigned long max_delay;
2169
Herbert Xuc5c23262012-04-13 02:37:42 +00002170 val = !!val;
2171
2172 spin_lock_bh(&br->multicast_lock);
2173 if (br->multicast_querier == val)
2174 goto unlock;
2175
2176 br->multicast_querier = val;
Linus Lüssingb00589a2013-08-01 01:06:20 +02002177 if (!val)
2178 goto unlock;
2179
2180 max_delay = br->multicast_query_response_interval;
Linus Lüssingb00589a2013-08-01 01:06:20 +02002181
Linus Lüssing90010b32014-06-07 18:26:26 +02002182 if (!timer_pending(&br->ip4_other_query.timer))
2183 br->ip4_other_query.delay_time = jiffies + max_delay;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002184
Linus Lüssing90010b32014-06-07 18:26:26 +02002185 br_multicast_start_querier(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002186
2187#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02002188 if (!timer_pending(&br->ip6_other_query.timer))
2189 br->ip6_other_query.delay_time = jiffies + max_delay;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002190
Linus Lüssing90010b32014-06-07 18:26:26 +02002191 br_multicast_start_querier(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002192#endif
Herbert Xuc5c23262012-04-13 02:37:42 +00002193
2194unlock:
2195 spin_unlock_bh(&br->multicast_lock);
2196
2197 return 0;
2198}
2199
Herbert Xub1951672010-02-27 19:41:51 +00002200int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
2201{
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02002202 int err = -EINVAL;
Herbert Xub1951672010-02-27 19:41:51 +00002203 u32 old;
Eric Dumazete8051682010-11-15 06:38:10 +00002204 struct net_bridge_mdb_htable *mdb;
Herbert Xub1951672010-02-27 19:41:51 +00002205
Curt Brunefe0d6922014-01-06 11:00:32 -08002206 spin_lock_bh(&br->multicast_lock);
Herbert Xub1951672010-02-27 19:41:51 +00002207 if (!is_power_of_2(val))
2208 goto unlock;
Eric Dumazete8051682010-11-15 06:38:10 +00002209
2210 mdb = mlock_dereference(br->mdb, br);
2211 if (mdb && val < mdb->size)
Herbert Xub1951672010-02-27 19:41:51 +00002212 goto unlock;
2213
2214 err = 0;
2215
2216 old = br->hash_max;
2217 br->hash_max = val;
2218
Eric Dumazete8051682010-11-15 06:38:10 +00002219 if (mdb) {
2220 if (mdb->old) {
Herbert Xub1951672010-02-27 19:41:51 +00002221 err = -EEXIST;
2222rollback:
2223 br->hash_max = old;
2224 goto unlock;
2225 }
2226
2227 err = br_mdb_rehash(&br->mdb, br->hash_max,
2228 br->hash_elasticity);
2229 if (err)
2230 goto rollback;
2231 }
2232
2233unlock:
Curt Brunefe0d6922014-01-06 11:00:32 -08002234 spin_unlock_bh(&br->multicast_lock);
Herbert Xub1951672010-02-27 19:41:51 +00002235
2236 return err;
2237}
Linus Lüssing07f8ac42014-06-07 18:26:28 +02002238
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01002239int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val)
2240{
2241 /* Currently we support only version 2 and 3 */
2242 switch (val) {
2243 case 2:
2244 case 3:
2245 break;
2246 default:
2247 return -EINVAL;
2248 }
2249
2250 spin_lock_bh(&br->multicast_lock);
2251 br->multicast_igmp_version = val;
2252 spin_unlock_bh(&br->multicast_lock);
2253
2254 return 0;
2255}
2256
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01002257#if IS_ENABLED(CONFIG_IPV6)
2258int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val)
2259{
2260 /* Currently we support version 1 and 2 */
2261 switch (val) {
2262 case 1:
2263 case 2:
2264 break;
2265 default:
2266 return -EINVAL;
2267 }
2268
2269 spin_lock_bh(&br->multicast_lock);
2270 br->multicast_mld_version = val;
2271 spin_unlock_bh(&br->multicast_lock);
2272
2273 return 0;
2274}
2275#endif
2276
Linus Lüssing07f8ac42014-06-07 18:26:28 +02002277/**
2278 * br_multicast_list_adjacent - Returns snooped multicast addresses
2279 * @dev: The bridge port adjacent to which to retrieve addresses
2280 * @br_ip_list: The list to store found, snooped multicast IP addresses in
2281 *
2282 * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
2283 * snooping feature on all bridge ports of dev's bridge device, excluding
2284 * the addresses from dev itself.
2285 *
2286 * Returns the number of items added to br_ip_list.
2287 *
2288 * Notes:
2289 * - br_ip_list needs to be initialized by caller
2290 * - br_ip_list might contain duplicates in the end
2291 * (needs to be taken care of by caller)
2292 * - br_ip_list needs to be freed by caller
2293 */
2294int br_multicast_list_adjacent(struct net_device *dev,
2295 struct list_head *br_ip_list)
2296{
2297 struct net_bridge *br;
2298 struct net_bridge_port *port;
2299 struct net_bridge_port_group *group;
2300 struct br_ip_list *entry;
2301 int count = 0;
2302
2303 rcu_read_lock();
2304 if (!br_ip_list || !br_port_exists(dev))
2305 goto unlock;
2306
2307 port = br_port_get_rcu(dev);
2308 if (!port || !port->br)
2309 goto unlock;
2310
2311 br = port->br;
2312
2313 list_for_each_entry_rcu(port, &br->port_list, list) {
2314 if (!port->dev || port->dev == dev)
2315 continue;
2316
2317 hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
2318 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
2319 if (!entry)
2320 goto unlock;
2321
2322 entry->addr = group->addr;
2323 list_add(&entry->list, br_ip_list);
2324 count++;
2325 }
2326 }
2327
2328unlock:
2329 rcu_read_unlock();
2330 return count;
2331}
2332EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
Linus Lüssing2cd41432014-06-07 18:26:29 +02002333
2334/**
Linus Lüssingc34963e2014-07-07 05:41:17 +02002335 * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2336 * @dev: The bridge port providing the bridge on which to check for a querier
2337 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2338 *
2339 * Checks whether the given interface has a bridge on top and if so returns
2340 * true if a valid querier exists anywhere on the bridged link layer.
2341 * Otherwise returns false.
2342 */
2343bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
2344{
2345 struct net_bridge *br;
2346 struct net_bridge_port *port;
2347 struct ethhdr eth;
2348 bool ret = false;
2349
2350 rcu_read_lock();
2351 if (!br_port_exists(dev))
2352 goto unlock;
2353
2354 port = br_port_get_rcu(dev);
2355 if (!port || !port->br)
2356 goto unlock;
2357
2358 br = port->br;
2359
2360 memset(&eth, 0, sizeof(eth));
2361 eth.h_proto = htons(proto);
2362
2363 ret = br_multicast_querier_exists(br, &eth);
2364
2365unlock:
2366 rcu_read_unlock();
2367 return ret;
2368}
2369EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
2370
2371/**
Linus Lüssing2cd41432014-06-07 18:26:29 +02002372 * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
2373 * @dev: The bridge port adjacent to which to check for a querier
2374 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2375 *
2376 * Checks whether the given interface has a bridge on top and if so returns
2377 * true if a selected querier is behind one of the other ports of this
2378 * bridge. Otherwise returns false.
2379 */
2380bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
2381{
2382 struct net_bridge *br;
2383 struct net_bridge_port *port;
2384 bool ret = false;
2385
2386 rcu_read_lock();
2387 if (!br_port_exists(dev))
2388 goto unlock;
2389
2390 port = br_port_get_rcu(dev);
2391 if (!port || !port->br)
2392 goto unlock;
2393
2394 br = port->br;
2395
2396 switch (proto) {
2397 case ETH_P_IP:
2398 if (!timer_pending(&br->ip4_other_query.timer) ||
2399 rcu_dereference(br->ip4_querier.port) == port)
2400 goto unlock;
2401 break;
Linus Lüssing3993c4e2014-06-12 01:41:24 +02002402#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing2cd41432014-06-07 18:26:29 +02002403 case ETH_P_IPV6:
2404 if (!timer_pending(&br->ip6_other_query.timer) ||
2405 rcu_dereference(br->ip6_querier.port) == port)
2406 goto unlock;
2407 break;
Linus Lüssing3993c4e2014-06-12 01:41:24 +02002408#endif
Linus Lüssing2cd41432014-06-07 18:26:29 +02002409 default:
2410 goto unlock;
2411 }
2412
2413 ret = true;
2414unlock:
2415 rcu_read_unlock();
2416 return ret;
2417}
2418EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002419
2420static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002421 const struct sk_buff *skb, u8 type, u8 dir)
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002422{
2423 struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002424 __be16 proto = skb->protocol;
2425 unsigned int t_len;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002426
2427 u64_stats_update_begin(&pstats->syncp);
2428 switch (proto) {
2429 case htons(ETH_P_IP):
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002430 t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002431 switch (type) {
2432 case IGMP_HOST_MEMBERSHIP_REPORT:
2433 pstats->mstats.igmp_v1reports[dir]++;
2434 break;
2435 case IGMPV2_HOST_MEMBERSHIP_REPORT:
2436 pstats->mstats.igmp_v2reports[dir]++;
2437 break;
2438 case IGMPV3_HOST_MEMBERSHIP_REPORT:
2439 pstats->mstats.igmp_v3reports[dir]++;
2440 break;
2441 case IGMP_HOST_MEMBERSHIP_QUERY:
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002442 if (t_len != sizeof(struct igmphdr)) {
2443 pstats->mstats.igmp_v3queries[dir]++;
2444 } else {
2445 unsigned int offset = skb_transport_offset(skb);
2446 struct igmphdr *ih, _ihdr;
2447
2448 ih = skb_header_pointer(skb, offset,
2449 sizeof(_ihdr), &_ihdr);
2450 if (!ih)
2451 break;
2452 if (!ih->code)
2453 pstats->mstats.igmp_v1queries[dir]++;
2454 else
2455 pstats->mstats.igmp_v2queries[dir]++;
2456 }
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002457 break;
2458 case IGMP_HOST_LEAVE_MESSAGE:
2459 pstats->mstats.igmp_leaves[dir]++;
2460 break;
2461 }
2462 break;
2463#if IS_ENABLED(CONFIG_IPV6)
2464 case htons(ETH_P_IPV6):
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002465 t_len = ntohs(ipv6_hdr(skb)->payload_len) +
2466 sizeof(struct ipv6hdr);
2467 t_len -= skb_network_header_len(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002468 switch (type) {
2469 case ICMPV6_MGM_REPORT:
2470 pstats->mstats.mld_v1reports[dir]++;
2471 break;
2472 case ICMPV6_MLD2_REPORT:
2473 pstats->mstats.mld_v2reports[dir]++;
2474 break;
2475 case ICMPV6_MGM_QUERY:
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002476 if (t_len != sizeof(struct mld_msg))
2477 pstats->mstats.mld_v2queries[dir]++;
2478 else
2479 pstats->mstats.mld_v1queries[dir]++;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002480 break;
2481 case ICMPV6_MGM_REDUCTION:
2482 pstats->mstats.mld_leaves[dir]++;
2483 break;
2484 }
2485 break;
2486#endif /* CONFIG_IPV6 */
2487 }
2488 u64_stats_update_end(&pstats->syncp);
2489}
2490
2491void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002492 const struct sk_buff *skb, u8 type, u8 dir)
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002493{
2494 struct bridge_mcast_stats __percpu *stats;
2495
2496 /* if multicast_disabled is true then igmp type can't be set */
2497 if (!type || !br->multicast_stats_enabled)
2498 return;
2499
2500 if (p)
2501 stats = p->mcast_stats;
2502 else
2503 stats = br->mcast_stats;
2504 if (WARN_ON(!stats))
2505 return;
2506
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002507 br_mcast_stats_add(stats, skb, type, dir);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002508}
2509
2510int br_multicast_init_stats(struct net_bridge *br)
2511{
2512 br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
2513 if (!br->mcast_stats)
2514 return -ENOMEM;
2515
2516 return 0;
2517}
2518
2519static void mcast_stats_add_dir(u64 *dst, u64 *src)
2520{
2521 dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
2522 dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
2523}
2524
2525void br_multicast_get_stats(const struct net_bridge *br,
2526 const struct net_bridge_port *p,
2527 struct br_mcast_stats *dest)
2528{
2529 struct bridge_mcast_stats __percpu *stats;
2530 struct br_mcast_stats tdst;
2531 int i;
2532
2533 memset(dest, 0, sizeof(*dest));
2534 if (p)
2535 stats = p->mcast_stats;
2536 else
2537 stats = br->mcast_stats;
2538 if (WARN_ON(!stats))
2539 return;
2540
2541 memset(&tdst, 0, sizeof(tdst));
2542 for_each_possible_cpu(i) {
2543 struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
2544 struct br_mcast_stats temp;
2545 unsigned int start;
2546
2547 do {
2548 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
2549 memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
2550 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
2551
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002552 mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
2553 mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
2554 mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002555 mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
2556 mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
2557 mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
2558 mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
2559 tdst.igmp_parse_errors += temp.igmp_parse_errors;
2560
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002561 mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
2562 mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002563 mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
2564 mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
2565 mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
2566 tdst.mld_parse_errors += temp.mld_parse_errors;
2567 }
2568 memcpy(dest, &tdst, sizeof(*dest));
2569}