blob: b30e77e8427c54bffc6801e2f1139139807c439f [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>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000030#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090031#include <net/ipv6.h>
32#include <net/mld.h>
David S. Millerd4c4f072010-04-27 10:16:54 -070033#include <net/ip6_checksum.h>
Linus Lüssing3c3769e2013-09-04 02:13:39 +020034#include <net/addrconf.h>
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090035#endif
Herbert Xueb1d1642010-02-27 19:41:45 +000036
37#include "br_private.h"
38
Linus Lüssingcc0fdd82013-08-30 17:28:17 +020039static void br_multicast_start_querier(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +020040 struct bridge_mcast_own_query *query);
Satish Ashok754bc542015-06-19 01:22:57 -070041static void br_multicast_add_router(struct net_bridge *br,
42 struct net_bridge_port *port);
Satish Ashokbc8c20a2015-07-13 05:28:37 -070043static void br_ip4_multicast_leave_group(struct net_bridge *br,
44 struct net_bridge_port *port,
45 __be32 group,
46 __u16 vid);
47#if IS_ENABLED(CONFIG_IPV6)
48static void br_ip6_multicast_leave_group(struct net_bridge *br,
49 struct net_bridge_port *port,
50 const struct in6_addr *group,
51 __u16 vid);
52#endif
Cong Wang2ce297f2012-12-10 02:15:35 +000053unsigned int br_mdb_rehash_seq;
Herbert Xuc83b8fa2012-04-13 02:37:42 +000054
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090055static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
56{
57 if (a->proto != b->proto)
58 return 0;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000059 if (a->vid != b->vid)
60 return 0;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090061 switch (a->proto) {
62 case htons(ETH_P_IP):
63 return a->u.ip4 == b->u.ip4;
Eric Dumazetdfd56b82011-12-10 09:48:31 +000064#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090065 case htons(ETH_P_IPV6):
66 return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
67#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090068 }
69 return 0;
70}
71
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000072static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip,
73 __u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +000074{
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000075 return jhash_2words((__force u32)ip, vid, mdb->secret) & (mdb->max - 1);
Herbert Xueb1d1642010-02-27 19:41:45 +000076}
77
Eric Dumazetdfd56b82011-12-10 09:48:31 +000078#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090079static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000080 const struct in6_addr *ip,
81 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090082{
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000083 return jhash_2words(ipv6_addr_hash(ip), vid,
84 mdb->secret) & (mdb->max - 1);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090085}
86#endif
87
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090088static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb,
89 struct br_ip *ip)
90{
91 switch (ip->proto) {
92 case htons(ETH_P_IP):
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000093 return __br_ip4_hash(mdb, ip->u.ip4, ip->vid);
Eric Dumazetdfd56b82011-12-10 09:48:31 +000094#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090095 case htons(ETH_P_IPV6):
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000096 return __br_ip6_hash(mdb, &ip->u.ip6, ip->vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090097#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090098 }
99 return 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000100}
101
102static struct net_bridge_mdb_entry *__br_mdb_ip_get(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900103 struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +0000104{
105 struct net_bridge_mdb_entry *mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000106
Sasha Levinb67bfe02013-02-27 17:06:00 -0800107 hlist_for_each_entry_rcu(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900108 if (br_ip_equal(&mp->addr, dst))
Herbert Xueb1d1642010-02-27 19:41:45 +0000109 return mp;
110 }
111
112 return NULL;
113}
114
Cong Wangcfd56752012-12-11 22:23:08 +0000115struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge_mdb_htable *mdb,
116 struct br_ip *dst)
Herbert Xu7f285fa2010-07-05 14:50:08 +0000117{
118 if (!mdb)
119 return NULL;
120
121 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
122}
123
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900124static struct net_bridge_mdb_entry *br_mdb_ip4_get(
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000125 struct net_bridge_mdb_htable *mdb, __be32 dst, __u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000126{
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900127 struct br_ip br_dst;
Herbert Xu0821ec52010-03-15 20:38:25 -0700128
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900129 br_dst.u.ip4 = dst;
130 br_dst.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000131 br_dst.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900132
Herbert Xu7f285fa2010-07-05 14:50:08 +0000133 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900134}
135
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000136#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900137static struct net_bridge_mdb_entry *br_mdb_ip6_get(
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000138 struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst,
139 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900140{
141 struct br_ip br_dst;
142
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000143 br_dst.u.ip6 = *dst;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900144 br_dst.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000145 br_dst.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900146
Herbert Xu7f285fa2010-07-05 14:50:08 +0000147 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900148}
149#endif
150
Herbert Xueb1d1642010-02-27 19:41:45 +0000151struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
Cong Wangfbca58a2013-03-07 03:05:33 +0000152 struct sk_buff *skb, u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000153{
Eric Dumazete8051682010-11-15 06:38:10 +0000154 struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900155 struct br_ip ip;
156
Herbert Xu7f285fa2010-07-05 14:50:08 +0000157 if (br->multicast_disabled)
Herbert Xueb1d1642010-02-27 19:41:45 +0000158 return NULL;
159
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900160 if (BR_INPUT_SKB_CB(skb)->igmp)
161 return NULL;
162
163 ip.proto = skb->protocol;
Cong Wangfbca58a2013-03-07 03:05:33 +0000164 ip.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900165
Herbert Xueb1d1642010-02-27 19:41:45 +0000166 switch (skb->protocol) {
167 case htons(ETH_P_IP):
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900168 ip.u.ip4 = ip_hdr(skb)->daddr;
169 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000170#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900171 case htons(ETH_P_IPV6):
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000172 ip.u.ip6 = ipv6_hdr(skb)->daddr;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900173 break;
174#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900175 default:
176 return NULL;
Herbert Xueb1d1642010-02-27 19:41:45 +0000177 }
178
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900179 return br_mdb_ip_get(mdb, &ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000180}
181
182static void br_mdb_free(struct rcu_head *head)
183{
184 struct net_bridge_mdb_htable *mdb =
185 container_of(head, struct net_bridge_mdb_htable, rcu);
186 struct net_bridge_mdb_htable *old = mdb->old;
187
188 mdb->old = NULL;
189 kfree(old->mhash);
190 kfree(old);
191}
192
193static int br_mdb_copy(struct net_bridge_mdb_htable *new,
194 struct net_bridge_mdb_htable *old,
195 int elasticity)
196{
197 struct net_bridge_mdb_entry *mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000198 int maxlen;
199 int len;
200 int i;
201
202 for (i = 0; i < old->max; i++)
Sasha Levinb67bfe02013-02-27 17:06:00 -0800203 hlist_for_each_entry(mp, &old->mhash[i], hlist[old->ver])
Herbert Xueb1d1642010-02-27 19:41:45 +0000204 hlist_add_head(&mp->hlist[new->ver],
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900205 &new->mhash[br_ip_hash(new, &mp->addr)]);
Herbert Xueb1d1642010-02-27 19:41:45 +0000206
207 if (!elasticity)
208 return 0;
209
210 maxlen = 0;
211 for (i = 0; i < new->max; i++) {
212 len = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800213 hlist_for_each_entry(mp, &new->mhash[i], hlist[new->ver])
Herbert Xueb1d1642010-02-27 19:41:45 +0000214 len++;
215 if (len > maxlen)
216 maxlen = len;
217 }
218
219 return maxlen > elasticity ? -EINVAL : 0;
220}
221
Cong Wangcfd56752012-12-11 22:23:08 +0000222void br_multicast_free_pg(struct rcu_head *head)
Herbert Xueb1d1642010-02-27 19:41:45 +0000223{
224 struct net_bridge_port_group *p =
225 container_of(head, struct net_bridge_port_group, rcu);
226
227 kfree(p);
228}
229
230static void br_multicast_free_group(struct rcu_head *head)
231{
232 struct net_bridge_mdb_entry *mp =
233 container_of(head, struct net_bridge_mdb_entry, rcu);
234
235 kfree(mp);
236}
237
238static void br_multicast_group_expired(unsigned long data)
239{
240 struct net_bridge_mdb_entry *mp = (void *)data;
241 struct net_bridge *br = mp->br;
242 struct net_bridge_mdb_htable *mdb;
243
244 spin_lock(&br->multicast_lock);
245 if (!netif_running(br->dev) || timer_pending(&mp->timer))
246 goto out;
247
Herbert Xu8a870172011-02-12 01:05:42 -0800248 mp->mglist = false;
Herbert Xueb1d1642010-02-27 19:41:45 +0000249
250 if (mp->ports)
251 goto out;
252
Eric Dumazete8051682010-11-15 06:38:10 +0000253 mdb = mlock_dereference(br->mdb, br);
254
Herbert Xueb1d1642010-02-27 19:41:45 +0000255 hlist_del_rcu(&mp->hlist[mdb->ver]);
256 mdb->size--;
257
Herbert Xueb1d1642010-02-27 19:41:45 +0000258 call_rcu_bh(&mp->rcu, br_multicast_free_group);
259
260out:
261 spin_unlock(&br->multicast_lock);
262}
263
264static void br_multicast_del_pg(struct net_bridge *br,
265 struct net_bridge_port_group *pg)
266{
Eric Dumazete8051682010-11-15 06:38:10 +0000267 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000268 struct net_bridge_mdb_entry *mp;
269 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +0000270 struct net_bridge_port_group __rcu **pp;
271
272 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +0000273
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900274 mp = br_mdb_ip_get(mdb, &pg->addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000275 if (WARN_ON(!mp))
276 return;
277
Eric Dumazete8051682010-11-15 06:38:10 +0000278 for (pp = &mp->ports;
279 (p = mlock_dereference(*pp, br)) != NULL;
280 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000281 if (p != pg)
282 continue;
283
stephen hemminger83f6a742010-04-27 15:01:06 +0000284 rcu_assign_pointer(*pp, p->next);
Herbert Xueb1d1642010-02-27 19:41:45 +0000285 hlist_del_init(&p->mglist);
286 del_timer(&p->timer);
Elad Raz45ebcce2016-04-21 12:52:45 +0200287 br_mdb_notify(br->dev, p->port, &pg->addr, RTM_DELMDB,
288 p->flags);
Herbert Xueb1d1642010-02-27 19:41:45 +0000289 call_rcu_bh(&p->rcu, br_multicast_free_pg);
290
Linus Lüssing454594f2013-10-20 00:58:57 +0200291 if (!mp->ports && !mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +0000292 netif_running(br->dev))
293 mod_timer(&mp->timer, jiffies);
294
295 return;
296 }
297
298 WARN_ON(1);
299}
300
301static void br_multicast_port_group_expired(unsigned long data)
302{
303 struct net_bridge_port_group *pg = (void *)data;
304 struct net_bridge *br = pg->port->br;
305
306 spin_lock(&br->multicast_lock);
307 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
Elad Raz9d06b6d2016-02-03 09:57:05 +0100308 hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT)
Herbert Xueb1d1642010-02-27 19:41:45 +0000309 goto out;
310
311 br_multicast_del_pg(br, pg);
312
313out:
314 spin_unlock(&br->multicast_lock);
315}
316
Eric Dumazete8051682010-11-15 06:38:10 +0000317static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max,
Herbert Xueb1d1642010-02-27 19:41:45 +0000318 int elasticity)
319{
Eric Dumazete8051682010-11-15 06:38:10 +0000320 struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000321 struct net_bridge_mdb_htable *mdb;
322 int err;
323
324 mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
325 if (!mdb)
326 return -ENOMEM;
327
328 mdb->max = max;
329 mdb->old = old;
330
331 mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
332 if (!mdb->mhash) {
333 kfree(mdb);
334 return -ENOMEM;
335 }
336
337 mdb->size = old ? old->size : 0;
338 mdb->ver = old ? old->ver ^ 1 : 0;
339
340 if (!old || elasticity)
341 get_random_bytes(&mdb->secret, sizeof(mdb->secret));
342 else
343 mdb->secret = old->secret;
344
345 if (!old)
346 goto out;
347
348 err = br_mdb_copy(mdb, old, elasticity);
349 if (err) {
350 kfree(mdb->mhash);
351 kfree(mdb);
352 return err;
353 }
354
Cong Wang2ce297f2012-12-10 02:15:35 +0000355 br_mdb_rehash_seq++;
Herbert Xueb1d1642010-02-27 19:41:45 +0000356 call_rcu_bh(&mdb->rcu, br_mdb_free);
357
358out:
359 rcu_assign_pointer(*mdbp, mdb);
360
361 return 0;
362}
363
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900364static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200365 __be32 group,
366 u8 *igmp_type)
Herbert Xueb1d1642010-02-27 19:41:45 +0000367{
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100368 struct igmpv3_query *ihv3;
369 size_t igmp_hdr_size;
Herbert Xueb1d1642010-02-27 19:41:45 +0000370 struct sk_buff *skb;
371 struct igmphdr *ih;
372 struct ethhdr *eth;
373 struct iphdr *iph;
374
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100375 igmp_hdr_size = sizeof(*ih);
376 if (br->multicast_igmp_version == 3)
377 igmp_hdr_size = sizeof(*ihv3);
Herbert Xueb1d1642010-02-27 19:41:45 +0000378 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100379 igmp_hdr_size + 4);
Herbert Xueb1d1642010-02-27 19:41:45 +0000380 if (!skb)
381 goto out;
382
383 skb->protocol = htons(ETH_P_IP);
384
385 skb_reset_mac_header(skb);
386 eth = eth_hdr(skb);
387
Joe Perchese5a727f2014-02-23 00:05:25 -0800388 ether_addr_copy(eth->h_source, br->dev->dev_addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000389 eth->h_dest[0] = 1;
390 eth->h_dest[1] = 0;
391 eth->h_dest[2] = 0x5e;
392 eth->h_dest[3] = 0;
393 eth->h_dest[4] = 0;
394 eth->h_dest[5] = 1;
395 eth->h_proto = htons(ETH_P_IP);
396 skb_put(skb, sizeof(*eth));
397
398 skb_set_network_header(skb, skb->len);
399 iph = ip_hdr(skb);
400
401 iph->version = 4;
402 iph->ihl = 6;
403 iph->tos = 0xc0;
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100404 iph->tot_len = htons(sizeof(*iph) + igmp_hdr_size + 4);
Herbert Xueb1d1642010-02-27 19:41:45 +0000405 iph->id = 0;
406 iph->frag_off = htons(IP_DF);
407 iph->ttl = 1;
408 iph->protocol = IPPROTO_IGMP;
Cong Wang1c8ad5b2013-05-21 21:52:54 +0000409 iph->saddr = br->multicast_query_use_ifaddr ?
410 inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000411 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
412 ((u8 *)&iph[1])[0] = IPOPT_RA;
413 ((u8 *)&iph[1])[1] = 4;
414 ((u8 *)&iph[1])[2] = 0;
415 ((u8 *)&iph[1])[3] = 0;
416 ip_send_check(iph);
417 skb_put(skb, 24);
418
419 skb_set_transport_header(skb, skb->len);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200420 *igmp_type = IGMP_HOST_MEMBERSHIP_QUERY;
Herbert Xueb1d1642010-02-27 19:41:45 +0000421
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100422 switch (br->multicast_igmp_version) {
423 case 2:
424 ih = igmp_hdr(skb);
425 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
426 ih->code = (group ? br->multicast_last_member_interval :
427 br->multicast_query_response_interval) /
428 (HZ / IGMP_TIMER_SCALE);
429 ih->group = group;
430 ih->csum = 0;
431 ih->csum = ip_compute_csum((void *)ih, sizeof(*ih));
432 break;
433 case 3:
434 ihv3 = igmpv3_query_hdr(skb);
435 ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY;
436 ihv3->code = (group ? br->multicast_last_member_interval :
437 br->multicast_query_response_interval) /
438 (HZ / IGMP_TIMER_SCALE);
439 ihv3->group = group;
440 ihv3->qqic = br->multicast_query_interval / HZ;
441 ihv3->nsrcs = 0;
442 ihv3->resv = 0;
443 ihv3->suppress = 0;
444 ihv3->qrv = 2;
445 ihv3->csum = 0;
446 ihv3->csum = ip_compute_csum((void *)ihv3, sizeof(*ihv3));
447 break;
448 }
449
450 skb_put(skb, igmp_hdr_size);
Herbert Xueb1d1642010-02-27 19:41:45 +0000451 __skb_pull(skb, sizeof(*eth));
452
453out:
454 return skb;
455}
456
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000457#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900458static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200459 const struct in6_addr *grp,
460 u8 *igmp_type)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900461{
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100462 struct mld2_query *mld2q;
463 unsigned long interval;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900464 struct ipv6hdr *ip6h;
465 struct mld_msg *mldq;
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100466 size_t mld_hdr_size;
467 struct sk_buff *skb;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900468 struct ethhdr *eth;
469 u8 *hopopt;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900470
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100471 mld_hdr_size = sizeof(*mldq);
472 if (br->multicast_mld_version == 2)
473 mld_hdr_size = sizeof(*mld2q);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900474 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100475 8 + mld_hdr_size);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900476 if (!skb)
477 goto out;
478
479 skb->protocol = htons(ETH_P_IPV6);
480
481 /* Ethernet header */
482 skb_reset_mac_header(skb);
483 eth = eth_hdr(skb);
484
Joe Perchese5a727f2014-02-23 00:05:25 -0800485 ether_addr_copy(eth->h_source, br->dev->dev_addr);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900486 eth->h_proto = htons(ETH_P_IPV6);
487 skb_put(skb, sizeof(*eth));
488
489 /* IPv6 header + HbH option */
490 skb_set_network_header(skb, skb->len);
491 ip6h = ipv6_hdr(skb);
492
493 *(__force __be32 *)ip6h = htonl(0x60000000);
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100494 ip6h->payload_len = htons(8 + mld_hdr_size);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900495 ip6h->nexthdr = IPPROTO_HOPOPTS;
496 ip6h->hop_limit = 1;
Linus Lüssinga7bff752011-03-22 11:40:32 +0000497 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
Ulrich Weberd1d81d42012-03-05 04:52:44 +0000498 if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
499 &ip6h->saddr)) {
500 kfree_skb(skb);
daniel0888d5f2016-06-24 12:35:18 +0200501 br->has_ipv6_addr = 0;
Ulrich Weberd1d81d42012-03-05 04:52:44 +0000502 return NULL;
503 }
daniel0888d5f2016-06-24 12:35:18 +0200504
505 br->has_ipv6_addr = 1;
Linus Lüssing36cff5a2011-02-17 08:17:51 +0000506 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900507
508 hopopt = (u8 *)(ip6h + 1);
509 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
510 hopopt[1] = 0; /* length of HbH */
511 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
512 hopopt[3] = 2; /* Length of RA Option */
513 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
514 hopopt[5] = 0;
Eldad Zack1de5a712012-05-17 06:00:25 +0000515 hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */
516 hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900517
518 skb_put(skb, sizeof(*ip6h) + 8);
519
520 /* ICMPv6 */
521 skb_set_transport_header(skb, skb->len);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200522 interval = ipv6_addr_any(grp) ?
Linus Lüssing32de8682013-06-16 23:20:34 +0200523 br->multicast_query_response_interval :
524 br->multicast_last_member_interval;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200525 *igmp_type = ICMPV6_MGM_QUERY;
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100526 switch (br->multicast_mld_version) {
527 case 1:
528 mldq = (struct mld_msg *)icmp6_hdr(skb);
529 mldq->mld_type = ICMPV6_MGM_QUERY;
530 mldq->mld_code = 0;
531 mldq->mld_cksum = 0;
532 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
533 mldq->mld_reserved = 0;
534 mldq->mld_mca = *grp;
535 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
536 sizeof(*mldq), IPPROTO_ICMPV6,
537 csum_partial(mldq,
538 sizeof(*mldq),
539 0));
540 break;
541 case 2:
542 mld2q = (struct mld2_query *)icmp6_hdr(skb);
543 mld2q->mld2q_mrc = ntohs((u16)jiffies_to_msecs(interval));
544 mld2q->mld2q_type = ICMPV6_MGM_QUERY;
545 mld2q->mld2q_code = 0;
546 mld2q->mld2q_cksum = 0;
547 mld2q->mld2q_resv1 = 0;
548 mld2q->mld2q_resv2 = 0;
549 mld2q->mld2q_suppress = 0;
550 mld2q->mld2q_qrv = 2;
551 mld2q->mld2q_nsrcs = 0;
552 mld2q->mld2q_qqic = br->multicast_query_interval / HZ;
553 mld2q->mld2q_mca = *grp;
554 mld2q->mld2q_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
555 sizeof(*mld2q),
556 IPPROTO_ICMPV6,
557 csum_partial(mld2q,
558 sizeof(*mld2q),
559 0));
560 break;
561 }
562 skb_put(skb, mld_hdr_size);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900563
564 __skb_pull(skb, sizeof(*eth));
565
566out:
567 return skb;
568}
569#endif
570
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900571static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200572 struct br_ip *addr,
573 u8 *igmp_type)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900574{
575 switch (addr->proto) {
576 case htons(ETH_P_IP):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200577 return br_ip4_multicast_alloc_query(br, addr->u.ip4, igmp_type);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000578#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900579 case htons(ETH_P_IPV6):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200580 return br_ip6_multicast_alloc_query(br, &addr->u.ip6,
581 igmp_type);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900582#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900583 }
584 return NULL;
585}
586
Herbert Xueb1d1642010-02-27 19:41:45 +0000587static struct net_bridge_mdb_entry *br_multicast_get_group(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900588 struct net_bridge *br, struct net_bridge_port *port,
589 struct br_ip *group, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +0000590{
Eric Dumazete8051682010-11-15 06:38:10 +0000591 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000592 struct net_bridge_mdb_entry *mp;
Eric Dumazet95c96172012-04-15 05:58:06 +0000593 unsigned int count = 0;
594 unsigned int max;
Herbert Xueb1d1642010-02-27 19:41:45 +0000595 int elasticity;
596 int err;
597
Eric Dumazete8051682010-11-15 06:38:10 +0000598 mdb = rcu_dereference_protected(br->mdb, 1);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800599 hlist_for_each_entry(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000600 count++;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900601 if (unlikely(br_ip_equal(group, &mp->addr)))
Herbert Xueb1d1642010-02-27 19:41:45 +0000602 return mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000603 }
604
605 elasticity = 0;
606 max = mdb->max;
607
608 if (unlikely(count > br->hash_elasticity && count)) {
609 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000610 br_info(br, "Multicast hash table "
611 "chain limit reached: %s\n",
612 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000613
614 elasticity = br->hash_elasticity;
615 }
616
617 if (mdb->size >= max) {
618 max *= 2;
Thomas Graf036be6d2012-07-10 22:29:19 +0000619 if (unlikely(max > br->hash_max)) {
620 br_warn(br, "Multicast hash table maximum of %d "
621 "reached, disabling snooping: %s\n",
622 br->hash_max,
623 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000624 err = -E2BIG;
625disable:
626 br->multicast_disabled = 1;
627 goto err;
628 }
629 }
630
631 if (max > mdb->max || elasticity) {
632 if (mdb->old) {
633 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000634 br_info(br, "Multicast hash table "
635 "on fire: %s\n",
636 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000637 err = -EEXIST;
638 goto err;
639 }
640
641 err = br_mdb_rehash(&br->mdb, max, elasticity);
642 if (err) {
stephen hemminger28a16c92010-05-10 09:31:09 +0000643 br_warn(br, "Cannot rehash multicast "
644 "hash table, disabling snooping: %s, %d, %d\n",
645 port ? port->dev->name : br->dev->name,
646 mdb->size, err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000647 goto disable;
648 }
649
650 err = -EAGAIN;
651 goto err;
652 }
653
654 return NULL;
655
656err:
657 mp = ERR_PTR(err);
658 return mp;
659}
660
Cong Wangcfd56752012-12-11 22:23:08 +0000661struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100662 struct net_bridge_port *p,
663 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000664{
Eric Dumazete8051682010-11-15 06:38:10 +0000665 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000666 struct net_bridge_mdb_entry *mp;
667 int hash;
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000668 int err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000669
Eric Dumazete8051682010-11-15 06:38:10 +0000670 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000671 if (!mdb) {
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000672 err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0);
673 if (err)
674 return ERR_PTR(err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000675 goto rehash;
676 }
677
678 hash = br_ip_hash(mdb, group);
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100679 mp = br_multicast_get_group(br, p, group, hash);
Herbert Xueb1d1642010-02-27 19:41:45 +0000680 switch (PTR_ERR(mp)) {
681 case 0:
682 break;
683
684 case -EAGAIN:
685rehash:
Eric Dumazete8051682010-11-15 06:38:10 +0000686 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000687 hash = br_ip_hash(mdb, group);
688 break;
689
690 default:
691 goto out;
692 }
693
694 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
695 if (unlikely(!mp))
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000696 return ERR_PTR(-ENOMEM);
Herbert Xueb1d1642010-02-27 19:41:45 +0000697
698 mp->br = br;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900699 mp->addr = *group;
Eric Dumazet1faabf22013-07-19 20:07:16 -0700700 setup_timer(&mp->timer, br_multicast_group_expired,
701 (unsigned long)mp);
702
Herbert Xueb1d1642010-02-27 19:41:45 +0000703 hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
704 mdb->size++;
705
706out:
707 return mp;
708}
709
Cong Wangcfd56752012-12-11 22:23:08 +0000710struct net_bridge_port_group *br_multicast_new_port_group(
711 struct net_bridge_port *port,
712 struct br_ip *group,
Amerigo Wangccb1c312012-12-14 22:09:51 +0000713 struct net_bridge_port_group __rcu *next,
Elad Raz9d06b6d2016-02-03 09:57:05 +0100714 unsigned char flags)
Cong Wangcfd56752012-12-11 22:23:08 +0000715{
716 struct net_bridge_port_group *p;
717
718 p = kzalloc(sizeof(*p), GFP_ATOMIC);
719 if (unlikely(!p))
720 return NULL;
721
722 p->addr = *group;
723 p->port = port;
Elad Raz9d06b6d2016-02-03 09:57:05 +0100724 p->flags = flags;
stephen hemmingereca2a432012-12-13 06:51:28 +0000725 rcu_assign_pointer(p->next, next);
Cong Wangcfd56752012-12-11 22:23:08 +0000726 hlist_add_head(&p->mglist, &port->mglist);
727 setup_timer(&p->timer, br_multicast_port_group_expired,
728 (unsigned long)p);
729 return p;
730}
731
Herbert Xueb1d1642010-02-27 19:41:45 +0000732static int br_multicast_add_group(struct net_bridge *br,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900733 struct net_bridge_port *port,
734 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000735{
Eric Dumazete8051682010-11-15 06:38:10 +0000736 struct net_bridge_port_group __rcu **pp;
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100737 struct net_bridge_port_group *p;
738 struct net_bridge_mdb_entry *mp;
Linus Lüssing454594f2013-10-20 00:58:57 +0200739 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +0000740 int err;
741
Herbert Xueb1d1642010-02-27 19:41:45 +0000742 spin_lock(&br->multicast_lock);
743 if (!netif_running(br->dev) ||
744 (port && port->state == BR_STATE_DISABLED))
745 goto out;
746
747 mp = br_multicast_new_group(br, port, group);
748 err = PTR_ERR(mp);
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000749 if (IS_ERR(mp))
Herbert Xueb1d1642010-02-27 19:41:45 +0000750 goto err;
751
752 if (!port) {
Herbert Xu8a870172011-02-12 01:05:42 -0800753 mp->mglist = true;
Linus Lüssing454594f2013-10-20 00:58:57 +0200754 mod_timer(&mp->timer, now + br->multicast_membership_interval);
Herbert Xueb1d1642010-02-27 19:41:45 +0000755 goto out;
756 }
757
Eric Dumazete8051682010-11-15 06:38:10 +0000758 for (pp = &mp->ports;
759 (p = mlock_dereference(*pp, br)) != NULL;
760 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000761 if (p->port == port)
Linus Lüssing454594f2013-10-20 00:58:57 +0200762 goto found;
Herbert Xueb1d1642010-02-27 19:41:45 +0000763 if ((unsigned long)p->port < (unsigned long)port)
764 break;
765 }
766
Elad Raz9d06b6d2016-02-03 09:57:05 +0100767 p = br_multicast_new_port_group(port, group, *pp, 0);
Herbert Xueb1d1642010-02-27 19:41:45 +0000768 if (unlikely(!p))
769 goto err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000770 rcu_assign_pointer(*pp, p);
Elad Raz45ebcce2016-04-21 12:52:45 +0200771 br_mdb_notify(br->dev, port, group, RTM_NEWMDB, 0);
Herbert Xueb1d1642010-02-27 19:41:45 +0000772
Linus Lüssing454594f2013-10-20 00:58:57 +0200773found:
774 mod_timer(&p->timer, now + br->multicast_membership_interval);
Herbert Xueb1d1642010-02-27 19:41:45 +0000775out:
776 err = 0;
777
778err:
779 spin_unlock(&br->multicast_lock);
780 return err;
781}
782
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900783static int br_ip4_multicast_add_group(struct net_bridge *br,
784 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000785 __be32 group,
786 __u16 vid)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900787{
788 struct br_ip br_group;
789
790 if (ipv4_is_local_multicast(group))
791 return 0;
792
793 br_group.u.ip4 = group;
794 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000795 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900796
797 return br_multicast_add_group(br, port, &br_group);
798}
799
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000800#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900801static int br_ip6_multicast_add_group(struct net_bridge *br,
802 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000803 const struct in6_addr *group,
804 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900805{
806 struct br_ip br_group;
807
Linus Lüssing3c3769e2013-09-04 02:13:39 +0200808 if (ipv6_addr_is_ll_all_nodes(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900809 return 0;
810
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000811 br_group.u.ip6 = *group;
Linus Lüssing9cc6e0c2011-02-15 13:19:17 +0000812 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000813 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900814
815 return br_multicast_add_group(br, port, &br_group);
816}
817#endif
818
Herbert Xueb1d1642010-02-27 19:41:45 +0000819static void br_multicast_router_expired(unsigned long data)
820{
821 struct net_bridge_port *port = (void *)data;
822 struct net_bridge *br = port->br;
823
824 spin_lock(&br->multicast_lock);
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +0100825 if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
826 port->multicast_router == MDB_RTR_TYPE_PERM ||
Herbert Xueb1d1642010-02-27 19:41:45 +0000827 timer_pending(&port->multicast_router_timer) ||
828 hlist_unhashed(&port->rlist))
829 goto out;
830
831 hlist_del_init_rcu(&port->rlist);
Satish Ashok949f1e32015-07-23 05:00:53 -0700832 br_rtr_notify(br->dev, port, RTM_DELMDB);
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +0100833 /* Don't allow timer refresh if the router expired */
834 if (port->multicast_router == MDB_RTR_TYPE_TEMP)
835 port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Herbert Xueb1d1642010-02-27 19:41:45 +0000836
837out:
838 spin_unlock(&br->multicast_lock);
839}
840
841static void br_multicast_local_router_expired(unsigned long data)
842{
843}
844
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200845static void br_multicast_querier_expired(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +0200846 struct bridge_mcast_own_query *query)
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000847{
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000848 spin_lock(&br->multicast_lock);
849 if (!netif_running(br->dev) || br->multicast_disabled)
850 goto out;
851
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200852 br_multicast_start_querier(br, query);
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000853
854out:
855 spin_unlock(&br->multicast_lock);
856}
857
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200858static void br_ip4_multicast_querier_expired(unsigned long data)
859{
860 struct net_bridge *br = (void *)data;
861
Linus Lüssing90010b32014-06-07 18:26:26 +0200862 br_multicast_querier_expired(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200863}
864
865#if IS_ENABLED(CONFIG_IPV6)
866static void br_ip6_multicast_querier_expired(unsigned long data)
867{
868 struct net_bridge *br = (void *)data;
869
Linus Lüssing90010b32014-06-07 18:26:26 +0200870 br_multicast_querier_expired(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200871}
872#endif
873
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200874static void br_multicast_select_own_querier(struct net_bridge *br,
875 struct br_ip *ip,
876 struct sk_buff *skb)
877{
878 if (ip->proto == htons(ETH_P_IP))
879 br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr;
880#if IS_ENABLED(CONFIG_IPV6)
881 else
882 br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr;
883#endif
884}
885
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900886static void __br_multicast_send_query(struct net_bridge *br,
887 struct net_bridge_port *port,
888 struct br_ip *ip)
Herbert Xueb1d1642010-02-27 19:41:45 +0000889{
Herbert Xueb1d1642010-02-27 19:41:45 +0000890 struct sk_buff *skb;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200891 u8 igmp_type;
Herbert Xueb1d1642010-02-27 19:41:45 +0000892
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200893 skb = br_multicast_alloc_query(br, ip, &igmp_type);
Herbert Xueb1d1642010-02-27 19:41:45 +0000894 if (!skb)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900895 return;
Herbert Xueb1d1642010-02-27 19:41:45 +0000896
897 if (port) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000898 skb->dev = port->dev;
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -0700899 br_multicast_count(br, port, skb, igmp_type,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200900 BR_MCAST_DIR_TX);
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500901 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
902 dev_net(port->dev), NULL, skb, NULL, skb->dev,
Linus Lüssingf0b4eec2014-11-17 12:20:28 +0100903 br_dev_queue_push_xmit);
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200904 } else {
905 br_multicast_select_own_querier(br, ip, skb);
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -0700906 br_multicast_count(br, port, skb, igmp_type,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200907 BR_MCAST_DIR_RX);
Herbert Xueb1d1642010-02-27 19:41:45 +0000908 netif_rx(skb);
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200909 }
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900910}
Herbert Xueb1d1642010-02-27 19:41:45 +0000911
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900912static void br_multicast_send_query(struct net_bridge *br,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200913 struct net_bridge_port *port,
Linus Lüssing90010b32014-06-07 18:26:26 +0200914 struct bridge_mcast_own_query *own_query)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900915{
Linus Lüssing90010b32014-06-07 18:26:26 +0200916 struct bridge_mcast_other_query *other_query = NULL;
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100917 struct br_ip br_group;
918 unsigned long time;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900919
920 if (!netif_running(br->dev) || br->multicast_disabled ||
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200921 !br->multicast_querier)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900922 return;
923
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900924 memset(&br_group.u, 0, sizeof(br_group.u));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900925
Linus Lüssing90010b32014-06-07 18:26:26 +0200926 if (port ? (own_query == &port->ip4_own_query) :
927 (own_query == &br->ip4_own_query)) {
928 other_query = &br->ip4_other_query;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200929 br_group.proto = htons(ETH_P_IP);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000930#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200931 } else {
Linus Lüssing90010b32014-06-07 18:26:26 +0200932 other_query = &br->ip6_other_query;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200933 br_group.proto = htons(ETH_P_IPV6);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900934#endif
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200935 }
936
Linus Lüssing90010b32014-06-07 18:26:26 +0200937 if (!other_query || timer_pending(&other_query->timer))
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200938 return;
939
940 __br_multicast_send_query(br, port, &br_group);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900941
Herbert Xueb1d1642010-02-27 19:41:45 +0000942 time = jiffies;
Linus Lüssing90010b32014-06-07 18:26:26 +0200943 time += own_query->startup_sent < br->multicast_startup_query_count ?
Herbert Xueb1d1642010-02-27 19:41:45 +0000944 br->multicast_startup_query_interval :
945 br->multicast_query_interval;
Linus Lüssing90010b32014-06-07 18:26:26 +0200946 mod_timer(&own_query->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +0000947}
948
Linus Lüssing90010b32014-06-07 18:26:26 +0200949static void
950br_multicast_port_query_expired(struct net_bridge_port *port,
951 struct bridge_mcast_own_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +0000952{
Herbert Xueb1d1642010-02-27 19:41:45 +0000953 struct net_bridge *br = port->br;
954
955 spin_lock(&br->multicast_lock);
Dan Carpenter02a780c2010-03-06 01:14:09 +0000956 if (port->state == BR_STATE_DISABLED ||
957 port->state == BR_STATE_BLOCKING)
Herbert Xueb1d1642010-02-27 19:41:45 +0000958 goto out;
959
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200960 if (query->startup_sent < br->multicast_startup_query_count)
961 query->startup_sent++;
Herbert Xueb1d1642010-02-27 19:41:45 +0000962
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200963 br_multicast_send_query(port->br, port, query);
Herbert Xueb1d1642010-02-27 19:41:45 +0000964
965out:
966 spin_unlock(&br->multicast_lock);
967}
968
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200969static void br_ip4_multicast_port_query_expired(unsigned long data)
970{
971 struct net_bridge_port *port = (void *)data;
972
Linus Lüssing90010b32014-06-07 18:26:26 +0200973 br_multicast_port_query_expired(port, &port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200974}
975
976#if IS_ENABLED(CONFIG_IPV6)
977static void br_ip6_multicast_port_query_expired(unsigned long data)
978{
979 struct net_bridge_port *port = (void *)data;
980
Linus Lüssing90010b32014-06-07 18:26:26 +0200981 br_multicast_port_query_expired(port, &port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200982}
983#endif
984
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200985int br_multicast_add_port(struct net_bridge_port *port)
Herbert Xueb1d1642010-02-27 19:41:45 +0000986{
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +0100987 port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Herbert Xueb1d1642010-02-27 19:41:45 +0000988
989 setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
990 (unsigned long)port);
Linus Lüssing90010b32014-06-07 18:26:26 +0200991 setup_timer(&port->ip4_own_query.timer,
992 br_ip4_multicast_port_query_expired, (unsigned long)port);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200993#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +0200994 setup_timer(&port->ip6_own_query.timer,
995 br_ip6_multicast_port_query_expired, (unsigned long)port);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200996#endif
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200997 port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
998 if (!port->mcast_stats)
999 return -ENOMEM;
1000
1001 return 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001002}
1003
1004void br_multicast_del_port(struct net_bridge_port *port)
1005{
Satish Ashoke10177a2015-07-15 07:16:51 -07001006 struct net_bridge *br = port->br;
1007 struct net_bridge_port_group *pg;
1008 struct hlist_node *n;
1009
1010 /* Take care of the remaining groups, only perm ones should be left */
1011 spin_lock_bh(&br->multicast_lock);
1012 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
1013 br_multicast_del_pg(br, pg);
1014 spin_unlock_bh(&br->multicast_lock);
Herbert Xueb1d1642010-02-27 19:41:45 +00001015 del_timer_sync(&port->multicast_router_timer);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001016 free_percpu(port->mcast_stats);
Herbert Xueb1d1642010-02-27 19:41:45 +00001017}
1018
Linus Lüssing90010b32014-06-07 18:26:26 +02001019static void br_multicast_enable(struct bridge_mcast_own_query *query)
Herbert Xu561f1102010-02-27 19:41:50 +00001020{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001021 query->startup_sent = 0;
Herbert Xu561f1102010-02-27 19:41:50 +00001022
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001023 if (try_to_del_timer_sync(&query->timer) >= 0 ||
1024 del_timer(&query->timer))
1025 mod_timer(&query->timer, jiffies);
Herbert Xu561f1102010-02-27 19:41:50 +00001026}
1027
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02001028static void __br_multicast_enable_port(struct net_bridge_port *port)
Herbert Xueb1d1642010-02-27 19:41:45 +00001029{
1030 struct net_bridge *br = port->br;
1031
Herbert Xueb1d1642010-02-27 19:41:45 +00001032 if (br->multicast_disabled || !netif_running(br->dev))
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02001033 return;
Herbert Xueb1d1642010-02-27 19:41:45 +00001034
Linus Lüssing90010b32014-06-07 18:26:26 +02001035 br_multicast_enable(&port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001036#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001037 br_multicast_enable(&port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001038#endif
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001039 if (port->multicast_router == MDB_RTR_TYPE_PERM &&
1040 hlist_unhashed(&port->rlist))
Satish Ashok754bc542015-06-19 01:22:57 -07001041 br_multicast_add_router(br, port);
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02001042}
Herbert Xueb1d1642010-02-27 19:41:45 +00001043
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02001044void br_multicast_enable_port(struct net_bridge_port *port)
1045{
1046 struct net_bridge *br = port->br;
1047
1048 spin_lock(&br->multicast_lock);
1049 __br_multicast_enable_port(port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001050 spin_unlock(&br->multicast_lock);
1051}
1052
1053void br_multicast_disable_port(struct net_bridge_port *port)
1054{
1055 struct net_bridge *br = port->br;
1056 struct net_bridge_port_group *pg;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001057 struct hlist_node *n;
Herbert Xueb1d1642010-02-27 19:41:45 +00001058
1059 spin_lock(&br->multicast_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001060 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
Elad Raz9d06b6d2016-02-03 09:57:05 +01001061 if (!(pg->flags & MDB_PG_FLAGS_PERMANENT))
Satish Ashoke10177a2015-07-15 07:16:51 -07001062 br_multicast_del_pg(br, pg);
Herbert Xueb1d1642010-02-27 19:41:45 +00001063
Satish Ashok949f1e32015-07-23 05:00:53 -07001064 if (!hlist_unhashed(&port->rlist)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001065 hlist_del_init_rcu(&port->rlist);
Satish Ashok949f1e32015-07-23 05:00:53 -07001066 br_rtr_notify(br->dev, port, RTM_DELMDB);
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001067 /* Don't allow timer refresh if disabling */
1068 if (port->multicast_router == MDB_RTR_TYPE_TEMP)
1069 port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Satish Ashok949f1e32015-07-23 05:00:53 -07001070 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001071 del_timer(&port->multicast_router_timer);
Linus Lüssing90010b32014-06-07 18:26:26 +02001072 del_timer(&port->ip4_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001073#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001074 del_timer(&port->ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001075#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001076 spin_unlock(&br->multicast_lock);
1077}
1078
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001079static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
1080 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001081 struct sk_buff *skb,
1082 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001083{
1084 struct igmpv3_report *ih;
1085 struct igmpv3_grec *grec;
1086 int i;
1087 int len;
1088 int num;
1089 int type;
1090 int err = 0;
1091 __be32 group;
1092
Herbert Xueb1d1642010-02-27 19:41:45 +00001093 ih = igmpv3_report_hdr(skb);
1094 num = ntohs(ih->ngrec);
Linus Lüssingc2d4fbd2015-09-11 18:39:48 +02001095 len = skb_transport_offset(skb) + sizeof(*ih);
Herbert Xueb1d1642010-02-27 19:41:45 +00001096
1097 for (i = 0; i < num; i++) {
1098 len += sizeof(*grec);
1099 if (!pskb_may_pull(skb, len))
1100 return -EINVAL;
1101
Herbert Xufd218cf2010-04-07 21:20:47 -07001102 grec = (void *)(skb->data + len - sizeof(*grec));
Herbert Xueb1d1642010-02-27 19:41:45 +00001103 group = grec->grec_mca;
1104 type = grec->grec_type;
1105
Eric Dumazet8eabf952010-04-20 03:20:05 +00001106 len += ntohs(grec->grec_nsrcs) * 4;
Herbert Xueb1d1642010-02-27 19:41:45 +00001107 if (!pskb_may_pull(skb, len))
1108 return -EINVAL;
1109
1110 /* We treat this as an IGMPv2 report for now. */
1111 switch (type) {
1112 case IGMPV3_MODE_IS_INCLUDE:
1113 case IGMPV3_MODE_IS_EXCLUDE:
1114 case IGMPV3_CHANGE_TO_INCLUDE:
1115 case IGMPV3_CHANGE_TO_EXCLUDE:
1116 case IGMPV3_ALLOW_NEW_SOURCES:
1117 case IGMPV3_BLOCK_OLD_SOURCES:
1118 break;
1119
1120 default:
1121 continue;
1122 }
1123
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001124 if ((type == IGMPV3_CHANGE_TO_INCLUDE ||
1125 type == IGMPV3_MODE_IS_INCLUDE) &&
1126 ntohs(grec->grec_nsrcs) == 0) {
1127 br_ip4_multicast_leave_group(br, port, group, vid);
1128 } else {
1129 err = br_ip4_multicast_add_group(br, port, group, vid);
1130 if (err)
1131 break;
1132 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001133 }
1134
1135 return err;
1136}
1137
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001138#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001139static int br_ip6_multicast_mld2_report(struct net_bridge *br,
1140 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001141 struct sk_buff *skb,
1142 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001143{
1144 struct icmp6hdr *icmp6h;
1145 struct mld2_grec *grec;
1146 int i;
1147 int len;
1148 int num;
1149 int err = 0;
1150
1151 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
1152 return -EINVAL;
1153
1154 icmp6h = icmp6_hdr(skb);
1155 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
Linus Lüssingc2d4fbd2015-09-11 18:39:48 +02001156 len = skb_transport_offset(skb) + sizeof(*icmp6h);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001157
1158 for (i = 0; i < num; i++) {
1159 __be16 *nsrcs, _nsrcs;
1160
1161 nsrcs = skb_header_pointer(skb,
1162 len + offsetof(struct mld2_grec,
Linus Lüssing649e9842011-02-15 13:19:18 +00001163 grec_nsrcs),
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001164 sizeof(_nsrcs), &_nsrcs);
1165 if (!nsrcs)
1166 return -EINVAL;
1167
1168 if (!pskb_may_pull(skb,
1169 len + sizeof(*grec) +
Linus Lüssingd41db9f2011-02-15 13:19:19 +00001170 sizeof(struct in6_addr) * ntohs(*nsrcs)))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001171 return -EINVAL;
1172
1173 grec = (struct mld2_grec *)(skb->data + len);
Linus Lüssingd41db9f2011-02-15 13:19:19 +00001174 len += sizeof(*grec) +
1175 sizeof(struct in6_addr) * ntohs(*nsrcs);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001176
1177 /* We treat these as MLDv1 reports for now. */
1178 switch (grec->grec_type) {
1179 case MLD2_MODE_IS_INCLUDE:
1180 case MLD2_MODE_IS_EXCLUDE:
1181 case MLD2_CHANGE_TO_INCLUDE:
1182 case MLD2_CHANGE_TO_EXCLUDE:
1183 case MLD2_ALLOW_NEW_SOURCES:
1184 case MLD2_BLOCK_OLD_SOURCES:
1185 break;
1186
1187 default:
1188 continue;
1189 }
1190
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001191 if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE ||
1192 grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
1193 ntohs(*nsrcs) == 0) {
1194 br_ip6_multicast_leave_group(br, port, &grec->grec_mca,
1195 vid);
1196 } else {
1197 err = br_ip6_multicast_add_group(br, port,
1198 &grec->grec_mca, vid);
Davide Caratti92642512016-08-31 14:16:44 +02001199 if (err)
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001200 break;
1201 }
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001202 }
1203
1204 return err;
1205}
1206#endif
1207
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001208static bool br_ip4_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001209 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001210 __be32 saddr)
1211{
1212 if (!timer_pending(&br->ip4_own_query.timer) &&
1213 !timer_pending(&br->ip4_other_query.timer))
1214 goto update;
1215
1216 if (!br->ip4_querier.addr.u.ip4)
1217 goto update;
1218
1219 if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4))
1220 goto update;
1221
1222 return false;
1223
1224update:
1225 br->ip4_querier.addr.u.ip4 = saddr;
1226
Linus Lüssing2cd41432014-06-07 18:26:29 +02001227 /* update protected by general multicast_lock by caller */
1228 rcu_assign_pointer(br->ip4_querier.port, port);
1229
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001230 return true;
1231}
1232
1233#if IS_ENABLED(CONFIG_IPV6)
1234static bool br_ip6_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001235 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001236 struct in6_addr *saddr)
1237{
1238 if (!timer_pending(&br->ip6_own_query.timer) &&
1239 !timer_pending(&br->ip6_other_query.timer))
1240 goto update;
1241
1242 if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0)
1243 goto update;
1244
1245 return false;
1246
1247update:
1248 br->ip6_querier.addr.u.ip6 = *saddr;
1249
Linus Lüssing2cd41432014-06-07 18:26:29 +02001250 /* update protected by general multicast_lock by caller */
1251 rcu_assign_pointer(br->ip6_querier.port, port);
1252
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001253 return true;
1254}
1255#endif
1256
1257static bool br_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001258 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001259 struct br_ip *saddr)
1260{
1261 switch (saddr->proto) {
1262 case htons(ETH_P_IP):
Linus Lüssing2cd41432014-06-07 18:26:29 +02001263 return br_ip4_multicast_select_querier(br, port, saddr->u.ip4);
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001264#if IS_ENABLED(CONFIG_IPV6)
1265 case htons(ETH_P_IPV6):
Linus Lüssing2cd41432014-06-07 18:26:29 +02001266 return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6);
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001267#endif
1268 }
1269
1270 return false;
1271}
1272
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001273static void
Linus Lüssing90010b32014-06-07 18:26:26 +02001274br_multicast_update_query_timer(struct net_bridge *br,
1275 struct bridge_mcast_other_query *query,
1276 unsigned long max_delay)
Linus Lüssingb00589a2013-08-01 01:06:20 +02001277{
Linus Lüssing90010b32014-06-07 18:26:26 +02001278 if (!timer_pending(&query->timer))
1279 query->delay_time = jiffies + max_delay;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001280
Linus Lüssing90010b32014-06-07 18:26:26 +02001281 mod_timer(&query->timer, jiffies + br->multicast_querier_interval);
Linus Lüssingb00589a2013-08-01 01:06:20 +02001282}
1283
stephen hemminger7e80c122010-04-27 15:01:04 +00001284/*
Cong Wang7c776022013-06-21 15:37:25 +08001285 * Add port to router_list
stephen hemminger7e80c122010-04-27 15:01:04 +00001286 * list is maintained ordered by pointer value
1287 * and locked by br->multicast_lock and RCU
1288 */
Herbert Xu0909e112010-02-27 19:41:49 +00001289static void br_multicast_add_router(struct net_bridge *br,
1290 struct net_bridge_port *port)
1291{
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001292 struct net_bridge_port *p;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001293 struct hlist_node *slot = NULL;
Herbert Xu0909e112010-02-27 19:41:49 +00001294
Nikolay Aleksandrov1a040ea2015-06-09 10:23:57 -07001295 if (!hlist_unhashed(&port->rlist))
1296 return;
1297
Sasha Levinb67bfe02013-02-27 17:06:00 -08001298 hlist_for_each_entry(p, &br->router_list, rlist) {
stephen hemminger7e80c122010-04-27 15:01:04 +00001299 if ((unsigned long) port >= (unsigned long) p)
1300 break;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001301 slot = &p->rlist;
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001302 }
Herbert Xu0909e112010-02-27 19:41:49 +00001303
stephen hemminger7e80c122010-04-27 15:01:04 +00001304 if (slot)
Ken Helias1d023282014-08-06 16:09:16 -07001305 hlist_add_behind_rcu(&port->rlist, slot);
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001306 else
1307 hlist_add_head_rcu(&port->rlist, &br->router_list);
Satish Ashok949f1e32015-07-23 05:00:53 -07001308 br_rtr_notify(br->dev, port, RTM_NEWMDB);
Herbert Xu0909e112010-02-27 19:41:49 +00001309}
1310
Herbert Xueb1d1642010-02-27 19:41:45 +00001311static void br_multicast_mark_router(struct net_bridge *br,
1312 struct net_bridge_port *port)
1313{
1314 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +00001315
1316 if (!port) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001317 if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY)
Herbert Xueb1d1642010-02-27 19:41:45 +00001318 mod_timer(&br->multicast_router_timer,
1319 now + br->multicast_querier_interval);
1320 return;
1321 }
1322
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001323 if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
1324 port->multicast_router == MDB_RTR_TYPE_PERM)
Herbert Xueb1d1642010-02-27 19:41:45 +00001325 return;
1326
Herbert Xu0909e112010-02-27 19:41:49 +00001327 br_multicast_add_router(br, port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001328
Herbert Xueb1d1642010-02-27 19:41:45 +00001329 mod_timer(&port->multicast_router_timer,
1330 now + br->multicast_querier_interval);
1331}
1332
1333static void br_multicast_query_received(struct net_bridge *br,
1334 struct net_bridge_port *port,
Linus Lüssing90010b32014-06-07 18:26:26 +02001335 struct bridge_mcast_other_query *query,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001336 struct br_ip *saddr,
Linus Lüssingb00589a2013-08-01 01:06:20 +02001337 unsigned long max_delay)
Herbert Xueb1d1642010-02-27 19:41:45 +00001338{
Linus Lüssing2cd41432014-06-07 18:26:29 +02001339 if (!br_multicast_select_querier(br, port, saddr))
Herbert Xueb1d1642010-02-27 19:41:45 +00001340 return;
1341
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001342 br_multicast_update_query_timer(br, query, max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001343 br_multicast_mark_router(br, port);
1344}
1345
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001346static int br_ip4_multicast_query(struct net_bridge *br,
1347 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001348 struct sk_buff *skb,
1349 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001350{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001351 const struct iphdr *iph = ip_hdr(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001352 struct igmphdr *ih = igmp_hdr(skb);
1353 struct net_bridge_mdb_entry *mp;
1354 struct igmpv3_query *ih3;
1355 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +00001356 struct net_bridge_port_group __rcu **pp;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001357 struct br_ip saddr;
Herbert Xueb1d1642010-02-27 19:41:45 +00001358 unsigned long max_delay;
1359 unsigned long now = jiffies;
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001360 unsigned int offset = skb_transport_offset(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001361 __be32 group;
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001362 int err = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001363
1364 spin_lock(&br->multicast_lock);
1365 if (!netif_running(br->dev) ||
1366 (port && port->state == BR_STATE_DISABLED))
1367 goto out;
1368
Herbert Xueb1d1642010-02-27 19:41:45 +00001369 group = ih->group;
1370
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001371 if (skb->len == offset + sizeof(*ih)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001372 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1373
1374 if (!max_delay) {
1375 max_delay = 10 * HZ;
1376 group = 0;
1377 }
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001378 } else if (skb->len >= offset + sizeof(*ih3)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001379 ih3 = igmpv3_query_hdr(skb);
1380 if (ih3->nsrcs)
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001381 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001382
YOSHIFUJI Hideaki / 吉藤英明0ba8c9e2010-03-15 19:27:00 +00001383 max_delay = ih3->code ?
1384 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001385 } else {
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001386 goto out;
1387 }
1388
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001389 if (!group) {
1390 saddr.proto = htons(ETH_P_IP);
1391 saddr.u.ip4 = iph->saddr;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001392
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001393 br_multicast_query_received(br, port, &br->ip4_other_query,
1394 &saddr, max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001395 goto out;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001396 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001397
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001398 mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001399 if (!mp)
1400 goto out;
1401
1402 max_delay *= br->multicast_last_member_count;
1403
Herbert Xu8a870172011-02-12 01:05:42 -08001404 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001405 (timer_pending(&mp->timer) ?
1406 time_after(mp->timer.expires, now + max_delay) :
1407 try_to_del_timer_sync(&mp->timer) >= 0))
1408 mod_timer(&mp->timer, now + max_delay);
1409
Eric Dumazete8051682010-11-15 06:38:10 +00001410 for (pp = &mp->ports;
1411 (p = mlock_dereference(*pp, br)) != NULL;
1412 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001413 if (timer_pending(&p->timer) ?
1414 time_after(p->timer.expires, now + max_delay) :
1415 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001416 mod_timer(&p->timer, now + max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001417 }
1418
1419out:
1420 spin_unlock(&br->multicast_lock);
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001421 return err;
Herbert Xueb1d1642010-02-27 19:41:45 +00001422}
1423
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001424#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001425static int br_ip6_multicast_query(struct net_bridge *br,
1426 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001427 struct sk_buff *skb,
1428 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001429{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001430 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
stephen hemmingereca2a432012-12-13 06:51:28 +00001431 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001432 struct net_bridge_mdb_entry *mp;
1433 struct mld2_query *mld2q;
Eric Dumazete8051682010-11-15 06:38:10 +00001434 struct net_bridge_port_group *p;
1435 struct net_bridge_port_group __rcu **pp;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001436 struct br_ip saddr;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001437 unsigned long max_delay;
1438 unsigned long now = jiffies;
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001439 unsigned int offset = skb_transport_offset(skb);
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001440 const struct in6_addr *group = NULL;
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001441 bool is_general_query;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001442 int err = 0;
1443
1444 spin_lock(&br->multicast_lock);
1445 if (!netif_running(br->dev) ||
1446 (port && port->state == BR_STATE_DISABLED))
1447 goto out;
1448
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001449 if (skb->len == offset + sizeof(*mld)) {
1450 if (!pskb_may_pull(skb, offset + sizeof(*mld))) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001451 err = -EINVAL;
1452 goto out;
1453 }
1454 mld = (struct mld_msg *) icmp6_hdr(skb);
Li RongQing47152132012-07-09 23:56:12 +00001455 max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001456 if (max_delay)
1457 group = &mld->mld_mca;
Linus Lüssing248ba8e2013-08-06 00:32:05 +02001458 } else {
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001459 if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001460 err = -EINVAL;
1461 goto out;
1462 }
1463 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1464 if (!mld2q->mld2q_nsrcs)
1465 group = &mld2q->mld2q_mca;
Daniel Borkmanne3f5b172013-09-04 00:19:39 +02001466
1467 max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001468 }
1469
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001470 is_general_query = group && ipv6_addr_any(group);
1471
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001472 if (is_general_query) {
1473 saddr.proto = htons(ETH_P_IPV6);
1474 saddr.u.ip6 = ip6h->saddr;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001475
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001476 br_multicast_query_received(br, port, &br->ip6_other_query,
1477 &saddr, max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001478 goto out;
Linus Lüssing6c03ee82014-06-12 01:41:23 +02001479 } else if (!group) {
1480 goto out;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001481 }
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001482
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001483 mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001484 if (!mp)
1485 goto out;
1486
1487 max_delay *= br->multicast_last_member_count;
Herbert Xu8a870172011-02-12 01:05:42 -08001488 if (mp->mglist &&
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001489 (timer_pending(&mp->timer) ?
1490 time_after(mp->timer.expires, now + max_delay) :
1491 try_to_del_timer_sync(&mp->timer) >= 0))
1492 mod_timer(&mp->timer, now + max_delay);
1493
Eric Dumazete8051682010-11-15 06:38:10 +00001494 for (pp = &mp->ports;
1495 (p = mlock_dereference(*pp, br)) != NULL;
1496 pp = &p->next) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001497 if (timer_pending(&p->timer) ?
1498 time_after(p->timer.expires, now + max_delay) :
1499 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001500 mod_timer(&p->timer, now + max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001501 }
1502
1503out:
1504 spin_unlock(&br->multicast_lock);
1505 return err;
1506}
1507#endif
1508
Linus Lüssing90010b32014-06-07 18:26:26 +02001509static void
1510br_multicast_leave_group(struct net_bridge *br,
1511 struct net_bridge_port *port,
1512 struct br_ip *group,
1513 struct bridge_mcast_other_query *other_query,
1514 struct bridge_mcast_own_query *own_query)
Herbert Xueb1d1642010-02-27 19:41:45 +00001515{
1516 struct net_bridge_mdb_htable *mdb;
1517 struct net_bridge_mdb_entry *mp;
1518 struct net_bridge_port_group *p;
1519 unsigned long now;
1520 unsigned long time;
1521
Herbert Xueb1d1642010-02-27 19:41:45 +00001522 spin_lock(&br->multicast_lock);
1523 if (!netif_running(br->dev) ||
Satish Ashok544586f2015-07-28 03:28:27 -07001524 (port && port->state == BR_STATE_DISABLED))
Herbert Xueb1d1642010-02-27 19:41:45 +00001525 goto out;
1526
Eric Dumazete8051682010-11-15 06:38:10 +00001527 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001528 mp = br_mdb_ip_get(mdb, group);
1529 if (!mp)
1530 goto out;
1531
Satish Ashok544586f2015-07-28 03:28:27 -07001532 if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
1533 struct net_bridge_port_group __rcu **pp;
1534
1535 for (pp = &mp->ports;
1536 (p = mlock_dereference(*pp, br)) != NULL;
1537 pp = &p->next) {
1538 if (p->port != port)
1539 continue;
1540
1541 rcu_assign_pointer(*pp, p->next);
1542 hlist_del_init(&p->mglist);
1543 del_timer(&p->timer);
1544 call_rcu_bh(&p->rcu, br_multicast_free_pg);
Elad Raz45ebcce2016-04-21 12:52:45 +02001545 br_mdb_notify(br->dev, port, group, RTM_DELMDB,
1546 p->flags);
Satish Ashok544586f2015-07-28 03:28:27 -07001547
1548 if (!mp->ports && !mp->mglist &&
1549 netif_running(br->dev))
1550 mod_timer(&mp->timer, jiffies);
1551 }
1552 goto out;
1553 }
1554
1555 if (timer_pending(&other_query->timer))
1556 goto out;
1557
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001558 if (br->multicast_querier) {
Cong Wang6b7df112013-05-21 21:52:56 +00001559 __br_multicast_send_query(br, port, &mp->addr);
1560
1561 time = jiffies + br->multicast_last_member_count *
1562 br->multicast_last_member_interval;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001563
Linus Lüssing90010b32014-06-07 18:26:26 +02001564 mod_timer(&own_query->timer, time);
Cong Wang6b7df112013-05-21 21:52:56 +00001565
1566 for (p = mlock_dereference(mp->ports, br);
1567 p != NULL;
1568 p = mlock_dereference(p->next, br)) {
1569 if (p->port != port)
1570 continue;
1571
1572 if (!hlist_unhashed(&p->mglist) &&
1573 (timer_pending(&p->timer) ?
1574 time_after(p->timer.expires, time) :
1575 try_to_del_timer_sync(&p->timer) >= 0)) {
1576 mod_timer(&p->timer, time);
1577 }
1578
1579 break;
1580 }
1581 }
1582
Herbert Xueb1d1642010-02-27 19:41:45 +00001583 now = jiffies;
1584 time = now + br->multicast_last_member_count *
1585 br->multicast_last_member_interval;
1586
1587 if (!port) {
Linus Lüssing454594f2013-10-20 00:58:57 +02001588 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001589 (timer_pending(&mp->timer) ?
1590 time_after(mp->timer.expires, time) :
1591 try_to_del_timer_sync(&mp->timer) >= 0)) {
1592 mod_timer(&mp->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +00001593 }
Linus Lüssing454594f2013-10-20 00:58:57 +02001594
1595 goto out;
1596 }
1597
1598 for (p = mlock_dereference(mp->ports, br);
1599 p != NULL;
1600 p = mlock_dereference(p->next, br)) {
1601 if (p->port != port)
1602 continue;
1603
1604 if (!hlist_unhashed(&p->mglist) &&
1605 (timer_pending(&p->timer) ?
1606 time_after(p->timer.expires, time) :
1607 try_to_del_timer_sync(&p->timer) >= 0)) {
1608 mod_timer(&p->timer, time);
1609 }
1610
1611 break;
Herbert Xueb1d1642010-02-27 19:41:45 +00001612 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001613out:
1614 spin_unlock(&br->multicast_lock);
1615}
1616
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001617static void br_ip4_multicast_leave_group(struct net_bridge *br,
1618 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001619 __be32 group,
1620 __u16 vid)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001621{
1622 struct br_ip br_group;
Linus Lüssing90010b32014-06-07 18:26:26 +02001623 struct bridge_mcast_own_query *own_query;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001624
1625 if (ipv4_is_local_multicast(group))
1626 return;
1627
Linus Lüssing90010b32014-06-07 18:26:26 +02001628 own_query = port ? &port->ip4_own_query : &br->ip4_own_query;
1629
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001630 br_group.u.ip4 = group;
1631 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001632 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001633
Linus Lüssing90010b32014-06-07 18:26:26 +02001634 br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
1635 own_query);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001636}
1637
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001638#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001639static void br_ip6_multicast_leave_group(struct net_bridge *br,
1640 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001641 const struct in6_addr *group,
1642 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001643{
1644 struct br_ip br_group;
Linus Lüssing90010b32014-06-07 18:26:26 +02001645 struct bridge_mcast_own_query *own_query;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001646
Linus Lüssing3c3769e2013-09-04 02:13:39 +02001647 if (ipv6_addr_is_ll_all_nodes(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001648 return;
1649
Linus Lüssing90010b32014-06-07 18:26:26 +02001650 own_query = port ? &port->ip6_own_query : &br->ip6_own_query;
1651
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001652 br_group.u.ip6 = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001653 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001654 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001655
Linus Lüssing90010b32014-06-07 18:26:26 +02001656 br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
1657 own_query);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001658}
1659#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001660
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001661static void br_multicast_err_count(const struct net_bridge *br,
1662 const struct net_bridge_port *p,
1663 __be16 proto)
1664{
1665 struct bridge_mcast_stats __percpu *stats;
1666 struct bridge_mcast_stats *pstats;
1667
1668 if (!br->multicast_stats_enabled)
1669 return;
1670
1671 if (p)
1672 stats = p->mcast_stats;
1673 else
1674 stats = br->mcast_stats;
1675 if (WARN_ON(!stats))
1676 return;
1677
1678 pstats = this_cpu_ptr(stats);
1679
1680 u64_stats_update_begin(&pstats->syncp);
1681 switch (proto) {
1682 case htons(ETH_P_IP):
1683 pstats->mstats.igmp_parse_errors++;
1684 break;
1685#if IS_ENABLED(CONFIG_IPV6)
1686 case htons(ETH_P_IPV6):
1687 pstats->mstats.mld_parse_errors++;
1688 break;
1689#endif
1690 }
1691 u64_stats_update_end(&pstats->syncp);
1692}
1693
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001694static void br_multicast_pim(struct net_bridge *br,
1695 struct net_bridge_port *port,
1696 const struct sk_buff *skb)
1697{
1698 unsigned int offset = skb_transport_offset(skb);
1699 struct pimhdr *pimhdr, _pimhdr;
1700
1701 pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr);
1702 if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION ||
1703 pim_hdr_type(pimhdr) != PIM_TYPE_HELLO)
1704 return;
1705
1706 br_multicast_mark_router(br, port);
1707}
1708
Herbert Xueb1d1642010-02-27 19:41:45 +00001709static int br_multicast_ipv4_rcv(struct net_bridge *br,
1710 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001711 struct sk_buff *skb,
1712 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001713{
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001714 struct sk_buff *skb_trimmed = NULL;
Herbert Xueb1d1642010-02-27 19:41:45 +00001715 struct igmphdr *ih;
Herbert Xueb1d1642010-02-27 19:41:45 +00001716 int err;
1717
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001718 err = ip_mc_check_igmp(skb, &skb_trimmed);
Herbert Xueb1d1642010-02-27 19:41:45 +00001719
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001720 if (err == -ENOMSG) {
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001721 if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) {
Herbert Xubd4265f2011-06-23 02:39:12 +00001722 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001723 } else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) {
1724 if (ip_hdr(skb)->protocol == IPPROTO_PIM)
1725 br_multicast_pim(br, port, skb);
1726 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001727 return 0;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001728 } else if (err < 0) {
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001729 br_multicast_err_count(br, port, skb->protocol);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001730 return err;
Herbert Xubd4265f2011-06-23 02:39:12 +00001731 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001732
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001733 ih = igmp_hdr(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001734 BR_INPUT_SKB_CB(skb)->igmp = ih->type;
Herbert Xueb1d1642010-02-27 19:41:45 +00001735
1736 switch (ih->type) {
1737 case IGMP_HOST_MEMBERSHIP_REPORT:
1738 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Fernando Luis Vázquez Cao62b2bcb2011-06-13 15:04:43 +00001739 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001740 err = br_ip4_multicast_add_group(br, port, ih->group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001741 break;
1742 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001743 err = br_ip4_multicast_igmp3_report(br, port, skb_trimmed, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001744 break;
1745 case IGMP_HOST_MEMBERSHIP_QUERY:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001746 err = br_ip4_multicast_query(br, port, skb_trimmed, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001747 break;
1748 case IGMP_HOST_LEAVE_MESSAGE:
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001749 br_ip4_multicast_leave_group(br, port, ih->group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001750 break;
1751 }
1752
Linus Lüssinga5169932015-08-13 05:54:07 +02001753 if (skb_trimmed && skb_trimmed != skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001754 kfree_skb(skb_trimmed);
1755
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07001756 br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001757 BR_MCAST_DIR_RX);
1758
Herbert Xueb1d1642010-02-27 19:41:45 +00001759 return err;
1760}
1761
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001762#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001763static int br_multicast_ipv6_rcv(struct net_bridge *br,
1764 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001765 struct sk_buff *skb,
1766 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001767{
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001768 struct sk_buff *skb_trimmed = NULL;
1769 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001770 int err;
1771
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001772 err = ipv6_mc_check_mld(skb, &skb_trimmed);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001773
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001774 if (err == -ENOMSG) {
1775 if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
1776 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Linus Lüssing8fad9c32013-09-04 02:13:38 +02001777 return 0;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001778 } else if (err < 0) {
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001779 br_multicast_err_count(br, port, skb->protocol);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001780 return err;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001781 }
1782
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001783 mld = (struct mld_msg *)skb_transport_header(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001784 BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001785
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001786 switch (mld->mld_type) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001787 case ICMPV6_MGM_REPORT:
Fernando Luis Vázquez Caofc2af6c2011-06-13 15:06:58 +00001788 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001789 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001790 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001791 case ICMPV6_MLD2_REPORT:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001792 err = br_ip6_multicast_mld2_report(br, port, skb_trimmed, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001793 break;
1794 case ICMPV6_MGM_QUERY:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001795 err = br_ip6_multicast_query(br, port, skb_trimmed, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001796 break;
1797 case ICMPV6_MGM_REDUCTION:
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001798 br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001799 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001800 }
1801
Linus Lüssinga5169932015-08-13 05:54:07 +02001802 if (skb_trimmed && skb_trimmed != skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001803 kfree_skb(skb_trimmed);
1804
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07001805 br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001806 BR_MCAST_DIR_RX);
1807
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001808 return err;
1809}
1810#endif
1811
Herbert Xueb1d1642010-02-27 19:41:45 +00001812int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001813 struct sk_buff *skb, u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001814{
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001815 int ret = 0;
1816
YOSHIFUJI Hideaki / 吉藤英明1fafc7a2010-04-25 08:06:40 +00001817 BR_INPUT_SKB_CB(skb)->igmp = 0;
1818 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1819
Herbert Xueb1d1642010-02-27 19:41:45 +00001820 if (br->multicast_disabled)
1821 return 0;
1822
1823 switch (skb->protocol) {
1824 case htons(ETH_P_IP):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001825 ret = br_multicast_ipv4_rcv(br, port, skb, vid);
1826 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001827#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001828 case htons(ETH_P_IPV6):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001829 ret = br_multicast_ipv6_rcv(br, port, skb, vid);
1830 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001831#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001832 }
1833
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001834 return ret;
Herbert Xueb1d1642010-02-27 19:41:45 +00001835}
1836
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001837static void br_multicast_query_expired(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001838 struct bridge_mcast_own_query *query,
1839 struct bridge_mcast_querier *querier)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001840{
1841 spin_lock(&br->multicast_lock);
1842 if (query->startup_sent < br->multicast_startup_query_count)
1843 query->startup_sent++;
1844
Eric Dumazet71d9f612015-05-28 04:42:54 -07001845 RCU_INIT_POINTER(querier->port, NULL);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001846 br_multicast_send_query(br, NULL, query);
1847 spin_unlock(&br->multicast_lock);
1848}
1849
1850static void br_ip4_multicast_query_expired(unsigned long data)
Herbert Xueb1d1642010-02-27 19:41:45 +00001851{
1852 struct net_bridge *br = (void *)data;
1853
Linus Lüssing2cd41432014-06-07 18:26:29 +02001854 br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier);
Herbert Xueb1d1642010-02-27 19:41:45 +00001855}
1856
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001857#if IS_ENABLED(CONFIG_IPV6)
1858static void br_ip6_multicast_query_expired(unsigned long data)
1859{
1860 struct net_bridge *br = (void *)data;
1861
Linus Lüssing2cd41432014-06-07 18:26:29 +02001862 br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001863}
1864#endif
1865
Herbert Xueb1d1642010-02-27 19:41:45 +00001866void br_multicast_init(struct net_bridge *br)
1867{
1868 br->hash_elasticity = 4;
1869 br->hash_max = 512;
1870
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001871 br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Herbert Xuc5c23262012-04-13 02:37:42 +00001872 br->multicast_querier = 0;
Cong Wang1c8ad5b2013-05-21 21:52:54 +00001873 br->multicast_query_use_ifaddr = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001874 br->multicast_last_member_count = 2;
1875 br->multicast_startup_query_count = 2;
1876
1877 br->multicast_last_member_interval = HZ;
1878 br->multicast_query_response_interval = 10 * HZ;
1879 br->multicast_startup_query_interval = 125 * HZ / 4;
1880 br->multicast_query_interval = 125 * HZ;
1881 br->multicast_querier_interval = 255 * HZ;
1882 br->multicast_membership_interval = 260 * HZ;
1883
Linus Lüssing90010b32014-06-07 18:26:26 +02001884 br->ip4_other_query.delay_time = 0;
Linus Lüssing2cd41432014-06-07 18:26:29 +02001885 br->ip4_querier.port = NULL;
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001886 br->multicast_igmp_version = 2;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001887#if IS_ENABLED(CONFIG_IPV6)
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001888 br->multicast_mld_version = 1;
Linus Lüssing90010b32014-06-07 18:26:26 +02001889 br->ip6_other_query.delay_time = 0;
Linus Lüssing2cd41432014-06-07 18:26:29 +02001890 br->ip6_querier.port = NULL;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001891#endif
daniel0888d5f2016-06-24 12:35:18 +02001892 br->has_ipv6_addr = 1;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001893
Herbert Xueb1d1642010-02-27 19:41:45 +00001894 spin_lock_init(&br->multicast_lock);
1895 setup_timer(&br->multicast_router_timer,
1896 br_multicast_local_router_expired, 0);
Linus Lüssing90010b32014-06-07 18:26:26 +02001897 setup_timer(&br->ip4_other_query.timer,
1898 br_ip4_multicast_querier_expired, (unsigned long)br);
1899 setup_timer(&br->ip4_own_query.timer, br_ip4_multicast_query_expired,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001900 (unsigned long)br);
1901#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001902 setup_timer(&br->ip6_other_query.timer,
1903 br_ip6_multicast_querier_expired, (unsigned long)br);
1904 setup_timer(&br->ip6_own_query.timer, br_ip6_multicast_query_expired,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001905 (unsigned long)br);
1906#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001907}
1908
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001909static void __br_multicast_open(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +02001910 struct bridge_mcast_own_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +00001911{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001912 query->startup_sent = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001913
1914 if (br->multicast_disabled)
1915 return;
1916
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001917 mod_timer(&query->timer, jiffies);
1918}
1919
1920void br_multicast_open(struct net_bridge *br)
1921{
Linus Lüssing90010b32014-06-07 18:26:26 +02001922 __br_multicast_open(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001923#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001924 __br_multicast_open(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001925#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001926}
1927
1928void br_multicast_stop(struct net_bridge *br)
1929{
Herbert Xueb1d1642010-02-27 19:41:45 +00001930 del_timer_sync(&br->multicast_router_timer);
Linus Lüssing90010b32014-06-07 18:26:26 +02001931 del_timer_sync(&br->ip4_other_query.timer);
1932 del_timer_sync(&br->ip4_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001933#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001934 del_timer_sync(&br->ip6_other_query.timer);
1935 del_timer_sync(&br->ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001936#endif
Satish Ashoke10177a2015-07-15 07:16:51 -07001937}
1938
1939void br_multicast_dev_del(struct net_bridge *br)
1940{
1941 struct net_bridge_mdb_htable *mdb;
1942 struct net_bridge_mdb_entry *mp;
1943 struct hlist_node *n;
1944 u32 ver;
1945 int i;
Herbert Xueb1d1642010-02-27 19:41:45 +00001946
1947 spin_lock_bh(&br->multicast_lock);
Eric Dumazete8051682010-11-15 06:38:10 +00001948 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001949 if (!mdb)
1950 goto out;
1951
1952 br->mdb = NULL;
1953
1954 ver = mdb->ver;
1955 for (i = 0; i < mdb->max; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001956 hlist_for_each_entry_safe(mp, n, &mdb->mhash[i],
Herbert Xueb1d1642010-02-27 19:41:45 +00001957 hlist[ver]) {
1958 del_timer(&mp->timer);
Herbert Xueb1d1642010-02-27 19:41:45 +00001959 call_rcu_bh(&mp->rcu, br_multicast_free_group);
1960 }
1961 }
1962
1963 if (mdb->old) {
1964 spin_unlock_bh(&br->multicast_lock);
Herbert Xu10cc2b52010-03-05 21:03:35 +00001965 rcu_barrier_bh();
Herbert Xueb1d1642010-02-27 19:41:45 +00001966 spin_lock_bh(&br->multicast_lock);
1967 WARN_ON(mdb->old);
1968 }
1969
1970 mdb->old = mdb;
1971 call_rcu_bh(&mdb->rcu, br_mdb_free);
1972
1973out:
1974 spin_unlock_bh(&br->multicast_lock);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001975
1976 free_percpu(br->mcast_stats);
Herbert Xueb1d1642010-02-27 19:41:45 +00001977}
Herbert Xu0909e112010-02-27 19:41:49 +00001978
1979int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1980{
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02001981 int err = -EINVAL;
Herbert Xu0909e112010-02-27 19:41:49 +00001982
1983 spin_lock_bh(&br->multicast_lock);
Herbert Xu0909e112010-02-27 19:41:49 +00001984
1985 switch (val) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001986 case MDB_RTR_TYPE_DISABLED:
1987 case MDB_RTR_TYPE_PERM:
Herbert Xu0909e112010-02-27 19:41:49 +00001988 del_timer(&br->multicast_router_timer);
1989 /* fall through */
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001990 case MDB_RTR_TYPE_TEMP_QUERY:
Herbert Xu0909e112010-02-27 19:41:49 +00001991 br->multicast_router = val;
1992 err = 0;
1993 break;
Herbert Xu0909e112010-02-27 19:41:49 +00001994 }
1995
Herbert Xu0909e112010-02-27 19:41:49 +00001996 spin_unlock_bh(&br->multicast_lock);
1997
1998 return err;
1999}
2000
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002001static void __del_port_router(struct net_bridge_port *p)
2002{
2003 if (hlist_unhashed(&p->rlist))
2004 return;
2005 hlist_del_init_rcu(&p->rlist);
2006 br_rtr_notify(p->br->dev, p, RTM_DELMDB);
2007}
2008
Herbert Xu0909e112010-02-27 19:41:49 +00002009int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
2010{
2011 struct net_bridge *br = p->br;
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01002012 unsigned long now = jiffies;
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02002013 int err = -EINVAL;
Herbert Xu0909e112010-02-27 19:41:49 +00002014
2015 spin_lock(&br->multicast_lock);
Nikolay Aleksandrov4950cfd2016-02-26 21:20:02 +01002016 if (p->multicast_router == val) {
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01002017 /* Refresh the temp router port timer */
2018 if (p->multicast_router == MDB_RTR_TYPE_TEMP)
2019 mod_timer(&p->multicast_router_timer,
2020 now + br->multicast_querier_interval);
Nikolay Aleksandrov4950cfd2016-02-26 21:20:02 +01002021 err = 0;
2022 goto unlock;
2023 }
Herbert Xu0909e112010-02-27 19:41:49 +00002024 switch (val) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002025 case MDB_RTR_TYPE_DISABLED:
2026 p->multicast_router = MDB_RTR_TYPE_DISABLED;
2027 __del_port_router(p);
Herbert Xu0909e112010-02-27 19:41:49 +00002028 del_timer(&p->multicast_router_timer);
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002029 break;
2030 case MDB_RTR_TYPE_TEMP_QUERY:
2031 p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
2032 __del_port_router(p);
2033 break;
2034 case MDB_RTR_TYPE_PERM:
2035 p->multicast_router = MDB_RTR_TYPE_PERM;
2036 del_timer(&p->multicast_router_timer);
Herbert Xu0909e112010-02-27 19:41:49 +00002037 br_multicast_add_router(br, p);
2038 break;
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01002039 case MDB_RTR_TYPE_TEMP:
2040 p->multicast_router = MDB_RTR_TYPE_TEMP;
2041 br_multicast_mark_router(br, p);
2042 break;
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002043 default:
2044 goto unlock;
Herbert Xu0909e112010-02-27 19:41:49 +00002045 }
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002046 err = 0;
2047unlock:
Herbert Xu0909e112010-02-27 19:41:49 +00002048 spin_unlock(&br->multicast_lock);
2049
2050 return err;
2051}
Herbert Xu561f1102010-02-27 19:41:50 +00002052
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002053static void br_multicast_start_querier(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +02002054 struct bridge_mcast_own_query *query)
Herbert Xu561f1102010-02-27 19:41:50 +00002055{
2056 struct net_bridge_port *port;
Herbert Xu74857212012-04-13 02:37:42 +00002057
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002058 __br_multicast_open(br, query);
Herbert Xu74857212012-04-13 02:37:42 +00002059
2060 list_for_each_entry(port, &br->port_list, list) {
2061 if (port->state == BR_STATE_DISABLED ||
2062 port->state == BR_STATE_BLOCKING)
2063 continue;
2064
Linus Lüssing90010b32014-06-07 18:26:26 +02002065 if (query == &br->ip4_own_query)
2066 br_multicast_enable(&port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002067#if IS_ENABLED(CONFIG_IPV6)
2068 else
Linus Lüssing90010b32014-06-07 18:26:26 +02002069 br_multicast_enable(&port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002070#endif
Herbert Xu74857212012-04-13 02:37:42 +00002071 }
2072}
2073
2074int br_multicast_toggle(struct net_bridge *br, unsigned long val)
2075{
Eric Dumazete8051682010-11-15 06:38:10 +00002076 struct net_bridge_mdb_htable *mdb;
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02002077 struct net_bridge_port *port;
2078 int err = 0;
Herbert Xu561f1102010-02-27 19:41:50 +00002079
Andrey Vaginef5e0d82011-11-10 05:48:03 +00002080 spin_lock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00002081 if (br->multicast_disabled == !val)
2082 goto unlock;
2083
2084 br->multicast_disabled = !val;
2085 if (br->multicast_disabled)
2086 goto unlock;
2087
Herbert Xu3a7fda02010-07-29 00:45:30 +00002088 if (!netif_running(br->dev))
2089 goto unlock;
2090
Eric Dumazete8051682010-11-15 06:38:10 +00002091 mdb = mlock_dereference(br->mdb, br);
2092 if (mdb) {
2093 if (mdb->old) {
Herbert Xu561f1102010-02-27 19:41:50 +00002094 err = -EEXIST;
2095rollback:
2096 br->multicast_disabled = !!val;
2097 goto unlock;
2098 }
2099
Eric Dumazete8051682010-11-15 06:38:10 +00002100 err = br_mdb_rehash(&br->mdb, mdb->max,
Herbert Xu561f1102010-02-27 19:41:50 +00002101 br->hash_elasticity);
2102 if (err)
2103 goto rollback;
2104 }
2105
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02002106 br_multicast_open(br);
2107 list_for_each_entry(port, &br->port_list, list)
2108 __br_multicast_enable_port(port);
Herbert Xu561f1102010-02-27 19:41:50 +00002109
2110unlock:
Andrey Vaginef5e0d82011-11-10 05:48:03 +00002111 spin_unlock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00002112
2113 return err;
2114}
Herbert Xub1951672010-02-27 19:41:51 +00002115
Herbert Xuc5c23262012-04-13 02:37:42 +00002116int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
2117{
Linus Lüssingb00589a2013-08-01 01:06:20 +02002118 unsigned long max_delay;
2119
Herbert Xuc5c23262012-04-13 02:37:42 +00002120 val = !!val;
2121
2122 spin_lock_bh(&br->multicast_lock);
2123 if (br->multicast_querier == val)
2124 goto unlock;
2125
2126 br->multicast_querier = val;
Linus Lüssingb00589a2013-08-01 01:06:20 +02002127 if (!val)
2128 goto unlock;
2129
2130 max_delay = br->multicast_query_response_interval;
Linus Lüssingb00589a2013-08-01 01:06:20 +02002131
Linus Lüssing90010b32014-06-07 18:26:26 +02002132 if (!timer_pending(&br->ip4_other_query.timer))
2133 br->ip4_other_query.delay_time = jiffies + max_delay;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002134
Linus Lüssing90010b32014-06-07 18:26:26 +02002135 br_multicast_start_querier(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002136
2137#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02002138 if (!timer_pending(&br->ip6_other_query.timer))
2139 br->ip6_other_query.delay_time = jiffies + max_delay;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002140
Linus Lüssing90010b32014-06-07 18:26:26 +02002141 br_multicast_start_querier(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002142#endif
Herbert Xuc5c23262012-04-13 02:37:42 +00002143
2144unlock:
2145 spin_unlock_bh(&br->multicast_lock);
2146
2147 return 0;
2148}
2149
Herbert Xub1951672010-02-27 19:41:51 +00002150int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
2151{
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02002152 int err = -EINVAL;
Herbert Xub1951672010-02-27 19:41:51 +00002153 u32 old;
Eric Dumazete8051682010-11-15 06:38:10 +00002154 struct net_bridge_mdb_htable *mdb;
Herbert Xub1951672010-02-27 19:41:51 +00002155
Curt Brunefe0d6922014-01-06 11:00:32 -08002156 spin_lock_bh(&br->multicast_lock);
Herbert Xub1951672010-02-27 19:41:51 +00002157 if (!is_power_of_2(val))
2158 goto unlock;
Eric Dumazete8051682010-11-15 06:38:10 +00002159
2160 mdb = mlock_dereference(br->mdb, br);
2161 if (mdb && val < mdb->size)
Herbert Xub1951672010-02-27 19:41:51 +00002162 goto unlock;
2163
2164 err = 0;
2165
2166 old = br->hash_max;
2167 br->hash_max = val;
2168
Eric Dumazete8051682010-11-15 06:38:10 +00002169 if (mdb) {
2170 if (mdb->old) {
Herbert Xub1951672010-02-27 19:41:51 +00002171 err = -EEXIST;
2172rollback:
2173 br->hash_max = old;
2174 goto unlock;
2175 }
2176
2177 err = br_mdb_rehash(&br->mdb, br->hash_max,
2178 br->hash_elasticity);
2179 if (err)
2180 goto rollback;
2181 }
2182
2183unlock:
Curt Brunefe0d6922014-01-06 11:00:32 -08002184 spin_unlock_bh(&br->multicast_lock);
Herbert Xub1951672010-02-27 19:41:51 +00002185
2186 return err;
2187}
Linus Lüssing07f8ac42014-06-07 18:26:28 +02002188
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01002189int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val)
2190{
2191 /* Currently we support only version 2 and 3 */
2192 switch (val) {
2193 case 2:
2194 case 3:
2195 break;
2196 default:
2197 return -EINVAL;
2198 }
2199
2200 spin_lock_bh(&br->multicast_lock);
2201 br->multicast_igmp_version = val;
2202 spin_unlock_bh(&br->multicast_lock);
2203
2204 return 0;
2205}
2206
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01002207#if IS_ENABLED(CONFIG_IPV6)
2208int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val)
2209{
2210 /* Currently we support version 1 and 2 */
2211 switch (val) {
2212 case 1:
2213 case 2:
2214 break;
2215 default:
2216 return -EINVAL;
2217 }
2218
2219 spin_lock_bh(&br->multicast_lock);
2220 br->multicast_mld_version = val;
2221 spin_unlock_bh(&br->multicast_lock);
2222
2223 return 0;
2224}
2225#endif
2226
Linus Lüssing07f8ac42014-06-07 18:26:28 +02002227/**
2228 * br_multicast_list_adjacent - Returns snooped multicast addresses
2229 * @dev: The bridge port adjacent to which to retrieve addresses
2230 * @br_ip_list: The list to store found, snooped multicast IP addresses in
2231 *
2232 * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
2233 * snooping feature on all bridge ports of dev's bridge device, excluding
2234 * the addresses from dev itself.
2235 *
2236 * Returns the number of items added to br_ip_list.
2237 *
2238 * Notes:
2239 * - br_ip_list needs to be initialized by caller
2240 * - br_ip_list might contain duplicates in the end
2241 * (needs to be taken care of by caller)
2242 * - br_ip_list needs to be freed by caller
2243 */
2244int br_multicast_list_adjacent(struct net_device *dev,
2245 struct list_head *br_ip_list)
2246{
2247 struct net_bridge *br;
2248 struct net_bridge_port *port;
2249 struct net_bridge_port_group *group;
2250 struct br_ip_list *entry;
2251 int count = 0;
2252
2253 rcu_read_lock();
2254 if (!br_ip_list || !br_port_exists(dev))
2255 goto unlock;
2256
2257 port = br_port_get_rcu(dev);
2258 if (!port || !port->br)
2259 goto unlock;
2260
2261 br = port->br;
2262
2263 list_for_each_entry_rcu(port, &br->port_list, list) {
2264 if (!port->dev || port->dev == dev)
2265 continue;
2266
2267 hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
2268 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
2269 if (!entry)
2270 goto unlock;
2271
2272 entry->addr = group->addr;
2273 list_add(&entry->list, br_ip_list);
2274 count++;
2275 }
2276 }
2277
2278unlock:
2279 rcu_read_unlock();
2280 return count;
2281}
2282EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
Linus Lüssing2cd41432014-06-07 18:26:29 +02002283
2284/**
Linus Lüssingc34963e2014-07-07 05:41:17 +02002285 * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2286 * @dev: The bridge port providing the bridge on which to check for a querier
2287 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2288 *
2289 * Checks whether the given interface has a bridge on top and if so returns
2290 * true if a valid querier exists anywhere on the bridged link layer.
2291 * Otherwise returns false.
2292 */
2293bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
2294{
2295 struct net_bridge *br;
2296 struct net_bridge_port *port;
2297 struct ethhdr eth;
2298 bool ret = false;
2299
2300 rcu_read_lock();
2301 if (!br_port_exists(dev))
2302 goto unlock;
2303
2304 port = br_port_get_rcu(dev);
2305 if (!port || !port->br)
2306 goto unlock;
2307
2308 br = port->br;
2309
2310 memset(&eth, 0, sizeof(eth));
2311 eth.h_proto = htons(proto);
2312
2313 ret = br_multicast_querier_exists(br, &eth);
2314
2315unlock:
2316 rcu_read_unlock();
2317 return ret;
2318}
2319EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
2320
2321/**
Linus Lüssing2cd41432014-06-07 18:26:29 +02002322 * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
2323 * @dev: The bridge port adjacent to which to check for a querier
2324 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2325 *
2326 * Checks whether the given interface has a bridge on top and if so returns
2327 * true if a selected querier is behind one of the other ports of this
2328 * bridge. Otherwise returns false.
2329 */
2330bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
2331{
2332 struct net_bridge *br;
2333 struct net_bridge_port *port;
2334 bool ret = false;
2335
2336 rcu_read_lock();
2337 if (!br_port_exists(dev))
2338 goto unlock;
2339
2340 port = br_port_get_rcu(dev);
2341 if (!port || !port->br)
2342 goto unlock;
2343
2344 br = port->br;
2345
2346 switch (proto) {
2347 case ETH_P_IP:
2348 if (!timer_pending(&br->ip4_other_query.timer) ||
2349 rcu_dereference(br->ip4_querier.port) == port)
2350 goto unlock;
2351 break;
Linus Lüssing3993c4e2014-06-12 01:41:24 +02002352#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing2cd41432014-06-07 18:26:29 +02002353 case ETH_P_IPV6:
2354 if (!timer_pending(&br->ip6_other_query.timer) ||
2355 rcu_dereference(br->ip6_querier.port) == port)
2356 goto unlock;
2357 break;
Linus Lüssing3993c4e2014-06-12 01:41:24 +02002358#endif
Linus Lüssing2cd41432014-06-07 18:26:29 +02002359 default:
2360 goto unlock;
2361 }
2362
2363 ret = true;
2364unlock:
2365 rcu_read_unlock();
2366 return ret;
2367}
2368EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002369
2370static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002371 const struct sk_buff *skb, u8 type, u8 dir)
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002372{
2373 struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002374 __be16 proto = skb->protocol;
2375 unsigned int t_len;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002376
2377 u64_stats_update_begin(&pstats->syncp);
2378 switch (proto) {
2379 case htons(ETH_P_IP):
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002380 t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002381 switch (type) {
2382 case IGMP_HOST_MEMBERSHIP_REPORT:
2383 pstats->mstats.igmp_v1reports[dir]++;
2384 break;
2385 case IGMPV2_HOST_MEMBERSHIP_REPORT:
2386 pstats->mstats.igmp_v2reports[dir]++;
2387 break;
2388 case IGMPV3_HOST_MEMBERSHIP_REPORT:
2389 pstats->mstats.igmp_v3reports[dir]++;
2390 break;
2391 case IGMP_HOST_MEMBERSHIP_QUERY:
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002392 if (t_len != sizeof(struct igmphdr)) {
2393 pstats->mstats.igmp_v3queries[dir]++;
2394 } else {
2395 unsigned int offset = skb_transport_offset(skb);
2396 struct igmphdr *ih, _ihdr;
2397
2398 ih = skb_header_pointer(skb, offset,
2399 sizeof(_ihdr), &_ihdr);
2400 if (!ih)
2401 break;
2402 if (!ih->code)
2403 pstats->mstats.igmp_v1queries[dir]++;
2404 else
2405 pstats->mstats.igmp_v2queries[dir]++;
2406 }
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002407 break;
2408 case IGMP_HOST_LEAVE_MESSAGE:
2409 pstats->mstats.igmp_leaves[dir]++;
2410 break;
2411 }
2412 break;
2413#if IS_ENABLED(CONFIG_IPV6)
2414 case htons(ETH_P_IPV6):
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002415 t_len = ntohs(ipv6_hdr(skb)->payload_len) +
2416 sizeof(struct ipv6hdr);
2417 t_len -= skb_network_header_len(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002418 switch (type) {
2419 case ICMPV6_MGM_REPORT:
2420 pstats->mstats.mld_v1reports[dir]++;
2421 break;
2422 case ICMPV6_MLD2_REPORT:
2423 pstats->mstats.mld_v2reports[dir]++;
2424 break;
2425 case ICMPV6_MGM_QUERY:
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002426 if (t_len != sizeof(struct mld_msg))
2427 pstats->mstats.mld_v2queries[dir]++;
2428 else
2429 pstats->mstats.mld_v1queries[dir]++;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002430 break;
2431 case ICMPV6_MGM_REDUCTION:
2432 pstats->mstats.mld_leaves[dir]++;
2433 break;
2434 }
2435 break;
2436#endif /* CONFIG_IPV6 */
2437 }
2438 u64_stats_update_end(&pstats->syncp);
2439}
2440
2441void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002442 const struct sk_buff *skb, u8 type, u8 dir)
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002443{
2444 struct bridge_mcast_stats __percpu *stats;
2445
2446 /* if multicast_disabled is true then igmp type can't be set */
2447 if (!type || !br->multicast_stats_enabled)
2448 return;
2449
2450 if (p)
2451 stats = p->mcast_stats;
2452 else
2453 stats = br->mcast_stats;
2454 if (WARN_ON(!stats))
2455 return;
2456
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002457 br_mcast_stats_add(stats, skb, type, dir);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002458}
2459
2460int br_multicast_init_stats(struct net_bridge *br)
2461{
2462 br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
2463 if (!br->mcast_stats)
2464 return -ENOMEM;
2465
2466 return 0;
2467}
2468
2469static void mcast_stats_add_dir(u64 *dst, u64 *src)
2470{
2471 dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
2472 dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
2473}
2474
2475void br_multicast_get_stats(const struct net_bridge *br,
2476 const struct net_bridge_port *p,
2477 struct br_mcast_stats *dest)
2478{
2479 struct bridge_mcast_stats __percpu *stats;
2480 struct br_mcast_stats tdst;
2481 int i;
2482
2483 memset(dest, 0, sizeof(*dest));
2484 if (p)
2485 stats = p->mcast_stats;
2486 else
2487 stats = br->mcast_stats;
2488 if (WARN_ON(!stats))
2489 return;
2490
2491 memset(&tdst, 0, sizeof(tdst));
2492 for_each_possible_cpu(i) {
2493 struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
2494 struct br_mcast_stats temp;
2495 unsigned int start;
2496
2497 do {
2498 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
2499 memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
2500 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
2501
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002502 mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
2503 mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
2504 mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002505 mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
2506 mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
2507 mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
2508 mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
2509 tdst.igmp_parse_errors += temp.igmp_parse_errors;
2510
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002511 mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
2512 mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002513 mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
2514 mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
2515 mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
2516 tdst.mld_parse_errors += temp.mld_parse_errors;
2517 }
2518 memcpy(dest, &tdst, sizeof(*dest));
2519}