blob: 8c0e896936ffbbe760a8fcfbdaa58bfc97fa3ba4 [file] [log] [blame]
Herbert Xueb1d1642010-02-27 19:41:45 +00001/*
2 * Bridge multicast support.
3 *
4 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 */
12
13#include <linux/err.h>
Linus Lüssing07f8ac42014-06-07 18:26:28 +020014#include <linux/export.h>
Herbert Xueb1d1642010-02-27 19:41:45 +000015#include <linux/if_ether.h>
16#include <linux/igmp.h>
17#include <linux/jhash.h>
18#include <linux/kernel.h>
Herbert Xub1951672010-02-27 19:41:51 +000019#include <linux/log2.h>
Herbert Xueb1d1642010-02-27 19:41:45 +000020#include <linux/netdevice.h>
21#include <linux/netfilter_bridge.h>
22#include <linux/random.h>
23#include <linux/rculist.h>
24#include <linux/skbuff.h>
25#include <linux/slab.h>
26#include <linux/timer.h>
Cong Wang1c8ad5b2013-05-21 21:52:54 +000027#include <linux/inetdevice.h>
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +010028#include <linux/mroute.h>
Herbert Xueb1d1642010-02-27 19:41:45 +000029#include <net/ip.h>
Nogah Frankel147c1e92017-02-09 14:54:40 +010030#include <net/switchdev.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000031#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090032#include <net/ipv6.h>
33#include <net/mld.h>
David S. Millerd4c4f072010-04-27 10:16:54 -070034#include <net/ip6_checksum.h>
Linus Lüssing3c3769e2013-09-04 02:13:39 +020035#include <net/addrconf.h>
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090036#endif
Herbert Xueb1d1642010-02-27 19:41:45 +000037
38#include "br_private.h"
39
Linus Lüssingcc0fdd82013-08-30 17:28:17 +020040static void br_multicast_start_querier(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +020041 struct bridge_mcast_own_query *query);
Satish Ashok754bc542015-06-19 01:22:57 -070042static void br_multicast_add_router(struct net_bridge *br,
43 struct net_bridge_port *port);
Satish Ashokbc8c20a2015-07-13 05:28:37 -070044static void br_ip4_multicast_leave_group(struct net_bridge *br,
45 struct net_bridge_port *port,
46 __be32 group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +010047 __u16 vid,
48 const unsigned char *src);
49
Satish Ashokbc8c20a2015-07-13 05:28:37 -070050#if IS_ENABLED(CONFIG_IPV6)
51static void br_ip6_multicast_leave_group(struct net_bridge *br,
52 struct net_bridge_port *port,
53 const struct in6_addr *group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +010054 __u16 vid, const unsigned char *src);
Satish Ashokbc8c20a2015-07-13 05:28:37 -070055#endif
Cong Wang2ce297f2012-12-10 02:15:35 +000056unsigned int br_mdb_rehash_seq;
Herbert Xuc83b8fa2012-04-13 02:37:42 +000057
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090058static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
59{
60 if (a->proto != b->proto)
61 return 0;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000062 if (a->vid != b->vid)
63 return 0;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090064 switch (a->proto) {
65 case htons(ETH_P_IP):
66 return a->u.ip4 == b->u.ip4;
Eric Dumazetdfd56b82011-12-10 09:48:31 +000067#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090068 case htons(ETH_P_IPV6):
69 return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
70#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090071 }
72 return 0;
73}
74
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000075static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip,
76 __u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +000077{
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000078 return jhash_2words((__force u32)ip, vid, mdb->secret) & (mdb->max - 1);
Herbert Xueb1d1642010-02-27 19:41:45 +000079}
80
Eric Dumazetdfd56b82011-12-10 09:48:31 +000081#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090082static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000083 const struct in6_addr *ip,
84 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090085{
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000086 return jhash_2words(ipv6_addr_hash(ip), vid,
87 mdb->secret) & (mdb->max - 1);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090088}
89#endif
90
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090091static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb,
92 struct br_ip *ip)
93{
94 switch (ip->proto) {
95 case htons(ETH_P_IP):
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000096 return __br_ip4_hash(mdb, ip->u.ip4, ip->vid);
Eric Dumazetdfd56b82011-12-10 09:48:31 +000097#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090098 case htons(ETH_P_IPV6):
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000099 return __br_ip6_hash(mdb, &ip->u.ip6, ip->vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900100#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900101 }
102 return 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000103}
104
105static struct net_bridge_mdb_entry *__br_mdb_ip_get(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900106 struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +0000107{
108 struct net_bridge_mdb_entry *mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000109
Sasha Levinb67bfe02013-02-27 17:06:00 -0800110 hlist_for_each_entry_rcu(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900111 if (br_ip_equal(&mp->addr, dst))
Herbert Xueb1d1642010-02-27 19:41:45 +0000112 return mp;
113 }
114
115 return NULL;
116}
117
Cong Wangcfd56752012-12-11 22:23:08 +0000118struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge_mdb_htable *mdb,
119 struct br_ip *dst)
Herbert Xu7f285fa2010-07-05 14:50:08 +0000120{
121 if (!mdb)
122 return NULL;
123
124 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
125}
126
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900127static struct net_bridge_mdb_entry *br_mdb_ip4_get(
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000128 struct net_bridge_mdb_htable *mdb, __be32 dst, __u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000129{
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900130 struct br_ip br_dst;
Herbert Xu0821ec52010-03-15 20:38:25 -0700131
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900132 br_dst.u.ip4 = dst;
133 br_dst.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000134 br_dst.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900135
Herbert Xu7f285fa2010-07-05 14:50:08 +0000136 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900137}
138
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000139#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900140static struct net_bridge_mdb_entry *br_mdb_ip6_get(
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000141 struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst,
142 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900143{
144 struct br_ip br_dst;
145
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000146 br_dst.u.ip6 = *dst;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900147 br_dst.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000148 br_dst.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900149
Herbert Xu7f285fa2010-07-05 14:50:08 +0000150 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900151}
152#endif
153
Herbert Xueb1d1642010-02-27 19:41:45 +0000154struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
Cong Wangfbca58a2013-03-07 03:05:33 +0000155 struct sk_buff *skb, u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000156{
Eric Dumazete8051682010-11-15 06:38:10 +0000157 struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900158 struct br_ip ip;
159
Herbert Xu7f285fa2010-07-05 14:50:08 +0000160 if (br->multicast_disabled)
Herbert Xueb1d1642010-02-27 19:41:45 +0000161 return NULL;
162
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900163 if (BR_INPUT_SKB_CB(skb)->igmp)
164 return NULL;
165
166 ip.proto = skb->protocol;
Cong Wangfbca58a2013-03-07 03:05:33 +0000167 ip.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900168
Herbert Xueb1d1642010-02-27 19:41:45 +0000169 switch (skb->protocol) {
170 case htons(ETH_P_IP):
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900171 ip.u.ip4 = ip_hdr(skb)->daddr;
172 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000173#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900174 case htons(ETH_P_IPV6):
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000175 ip.u.ip6 = ipv6_hdr(skb)->daddr;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900176 break;
177#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900178 default:
179 return NULL;
Herbert Xueb1d1642010-02-27 19:41:45 +0000180 }
181
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900182 return br_mdb_ip_get(mdb, &ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000183}
184
185static void br_mdb_free(struct rcu_head *head)
186{
187 struct net_bridge_mdb_htable *mdb =
188 container_of(head, struct net_bridge_mdb_htable, rcu);
189 struct net_bridge_mdb_htable *old = mdb->old;
190
191 mdb->old = NULL;
192 kfree(old->mhash);
193 kfree(old);
194}
195
196static int br_mdb_copy(struct net_bridge_mdb_htable *new,
197 struct net_bridge_mdb_htable *old,
198 int elasticity)
199{
200 struct net_bridge_mdb_entry *mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000201 int maxlen;
202 int len;
203 int i;
204
205 for (i = 0; i < old->max; i++)
Sasha Levinb67bfe02013-02-27 17:06:00 -0800206 hlist_for_each_entry(mp, &old->mhash[i], hlist[old->ver])
Herbert Xueb1d1642010-02-27 19:41:45 +0000207 hlist_add_head(&mp->hlist[new->ver],
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900208 &new->mhash[br_ip_hash(new, &mp->addr)]);
Herbert Xueb1d1642010-02-27 19:41:45 +0000209
210 if (!elasticity)
211 return 0;
212
213 maxlen = 0;
214 for (i = 0; i < new->max; i++) {
215 len = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800216 hlist_for_each_entry(mp, &new->mhash[i], hlist[new->ver])
Herbert Xueb1d1642010-02-27 19:41:45 +0000217 len++;
218 if (len > maxlen)
219 maxlen = len;
220 }
221
222 return maxlen > elasticity ? -EINVAL : 0;
223}
224
Cong Wangcfd56752012-12-11 22:23:08 +0000225void br_multicast_free_pg(struct rcu_head *head)
Herbert Xueb1d1642010-02-27 19:41:45 +0000226{
227 struct net_bridge_port_group *p =
228 container_of(head, struct net_bridge_port_group, rcu);
229
230 kfree(p);
231}
232
233static void br_multicast_free_group(struct rcu_head *head)
234{
235 struct net_bridge_mdb_entry *mp =
236 container_of(head, struct net_bridge_mdb_entry, rcu);
237
238 kfree(mp);
239}
240
241static void br_multicast_group_expired(unsigned long data)
242{
243 struct net_bridge_mdb_entry *mp = (void *)data;
244 struct net_bridge *br = mp->br;
245 struct net_bridge_mdb_htable *mdb;
246
247 spin_lock(&br->multicast_lock);
248 if (!netif_running(br->dev) || timer_pending(&mp->timer))
249 goto out;
250
Herbert Xu8a870172011-02-12 01:05:42 -0800251 mp->mglist = false;
Herbert Xueb1d1642010-02-27 19:41:45 +0000252
253 if (mp->ports)
254 goto out;
255
Eric Dumazete8051682010-11-15 06:38:10 +0000256 mdb = mlock_dereference(br->mdb, br);
257
Herbert Xueb1d1642010-02-27 19:41:45 +0000258 hlist_del_rcu(&mp->hlist[mdb->ver]);
259 mdb->size--;
260
Herbert Xueb1d1642010-02-27 19:41:45 +0000261 call_rcu_bh(&mp->rcu, br_multicast_free_group);
262
263out:
264 spin_unlock(&br->multicast_lock);
265}
266
267static void br_multicast_del_pg(struct net_bridge *br,
268 struct net_bridge_port_group *pg)
269{
Eric Dumazete8051682010-11-15 06:38:10 +0000270 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000271 struct net_bridge_mdb_entry *mp;
272 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +0000273 struct net_bridge_port_group __rcu **pp;
274
275 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +0000276
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900277 mp = br_mdb_ip_get(mdb, &pg->addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000278 if (WARN_ON(!mp))
279 return;
280
Eric Dumazete8051682010-11-15 06:38:10 +0000281 for (pp = &mp->ports;
282 (p = mlock_dereference(*pp, br)) != NULL;
283 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000284 if (p != pg)
285 continue;
286
stephen hemminger83f6a742010-04-27 15:01:06 +0000287 rcu_assign_pointer(*pp, p->next);
Herbert Xueb1d1642010-02-27 19:41:45 +0000288 hlist_del_init(&p->mglist);
289 del_timer(&p->timer);
Elad Raz45ebcce2016-04-21 12:52:45 +0200290 br_mdb_notify(br->dev, p->port, &pg->addr, RTM_DELMDB,
291 p->flags);
Herbert Xueb1d1642010-02-27 19:41:45 +0000292 call_rcu_bh(&p->rcu, br_multicast_free_pg);
293
Linus Lüssing454594f2013-10-20 00:58:57 +0200294 if (!mp->ports && !mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +0000295 netif_running(br->dev))
296 mod_timer(&mp->timer, jiffies);
297
298 return;
299 }
300
301 WARN_ON(1);
302}
303
304static void br_multicast_port_group_expired(unsigned long data)
305{
306 struct net_bridge_port_group *pg = (void *)data;
307 struct net_bridge *br = pg->port->br;
308
309 spin_lock(&br->multicast_lock);
310 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
Elad Raz9d06b6d2016-02-03 09:57:05 +0100311 hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT)
Herbert Xueb1d1642010-02-27 19:41:45 +0000312 goto out;
313
314 br_multicast_del_pg(br, pg);
315
316out:
317 spin_unlock(&br->multicast_lock);
318}
319
Eric Dumazete8051682010-11-15 06:38:10 +0000320static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max,
Herbert Xueb1d1642010-02-27 19:41:45 +0000321 int elasticity)
322{
Eric Dumazete8051682010-11-15 06:38:10 +0000323 struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000324 struct net_bridge_mdb_htable *mdb;
325 int err;
326
327 mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
328 if (!mdb)
329 return -ENOMEM;
330
331 mdb->max = max;
332 mdb->old = old;
333
334 mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
335 if (!mdb->mhash) {
336 kfree(mdb);
337 return -ENOMEM;
338 }
339
340 mdb->size = old ? old->size : 0;
341 mdb->ver = old ? old->ver ^ 1 : 0;
342
343 if (!old || elasticity)
344 get_random_bytes(&mdb->secret, sizeof(mdb->secret));
345 else
346 mdb->secret = old->secret;
347
348 if (!old)
349 goto out;
350
351 err = br_mdb_copy(mdb, old, elasticity);
352 if (err) {
353 kfree(mdb->mhash);
354 kfree(mdb);
355 return err;
356 }
357
Cong Wang2ce297f2012-12-10 02:15:35 +0000358 br_mdb_rehash_seq++;
Herbert Xueb1d1642010-02-27 19:41:45 +0000359 call_rcu_bh(&mdb->rcu, br_mdb_free);
360
361out:
362 rcu_assign_pointer(*mdbp, mdb);
363
364 return 0;
365}
366
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900367static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200368 __be32 group,
369 u8 *igmp_type)
Herbert Xueb1d1642010-02-27 19:41:45 +0000370{
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100371 struct igmpv3_query *ihv3;
372 size_t igmp_hdr_size;
Herbert Xueb1d1642010-02-27 19:41:45 +0000373 struct sk_buff *skb;
374 struct igmphdr *ih;
375 struct ethhdr *eth;
376 struct iphdr *iph;
377
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100378 igmp_hdr_size = sizeof(*ih);
379 if (br->multicast_igmp_version == 3)
380 igmp_hdr_size = sizeof(*ihv3);
Herbert Xueb1d1642010-02-27 19:41:45 +0000381 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100382 igmp_hdr_size + 4);
Herbert Xueb1d1642010-02-27 19:41:45 +0000383 if (!skb)
384 goto out;
385
386 skb->protocol = htons(ETH_P_IP);
387
388 skb_reset_mac_header(skb);
389 eth = eth_hdr(skb);
390
Joe Perchese5a727f2014-02-23 00:05:25 -0800391 ether_addr_copy(eth->h_source, br->dev->dev_addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000392 eth->h_dest[0] = 1;
393 eth->h_dest[1] = 0;
394 eth->h_dest[2] = 0x5e;
395 eth->h_dest[3] = 0;
396 eth->h_dest[4] = 0;
397 eth->h_dest[5] = 1;
398 eth->h_proto = htons(ETH_P_IP);
399 skb_put(skb, sizeof(*eth));
400
401 skb_set_network_header(skb, skb->len);
402 iph = ip_hdr(skb);
403
404 iph->version = 4;
405 iph->ihl = 6;
406 iph->tos = 0xc0;
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100407 iph->tot_len = htons(sizeof(*iph) + igmp_hdr_size + 4);
Herbert Xueb1d1642010-02-27 19:41:45 +0000408 iph->id = 0;
409 iph->frag_off = htons(IP_DF);
410 iph->ttl = 1;
411 iph->protocol = IPPROTO_IGMP;
Cong Wang1c8ad5b2013-05-21 21:52:54 +0000412 iph->saddr = br->multicast_query_use_ifaddr ?
413 inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000414 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
415 ((u8 *)&iph[1])[0] = IPOPT_RA;
416 ((u8 *)&iph[1])[1] = 4;
417 ((u8 *)&iph[1])[2] = 0;
418 ((u8 *)&iph[1])[3] = 0;
419 ip_send_check(iph);
420 skb_put(skb, 24);
421
422 skb_set_transport_header(skb, skb->len);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200423 *igmp_type = IGMP_HOST_MEMBERSHIP_QUERY;
Herbert Xueb1d1642010-02-27 19:41:45 +0000424
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100425 switch (br->multicast_igmp_version) {
426 case 2:
427 ih = igmp_hdr(skb);
428 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
429 ih->code = (group ? br->multicast_last_member_interval :
430 br->multicast_query_response_interval) /
431 (HZ / IGMP_TIMER_SCALE);
432 ih->group = group;
433 ih->csum = 0;
434 ih->csum = ip_compute_csum((void *)ih, sizeof(*ih));
435 break;
436 case 3:
437 ihv3 = igmpv3_query_hdr(skb);
438 ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY;
439 ihv3->code = (group ? br->multicast_last_member_interval :
440 br->multicast_query_response_interval) /
441 (HZ / IGMP_TIMER_SCALE);
442 ihv3->group = group;
443 ihv3->qqic = br->multicast_query_interval / HZ;
444 ihv3->nsrcs = 0;
445 ihv3->resv = 0;
446 ihv3->suppress = 0;
447 ihv3->qrv = 2;
448 ihv3->csum = 0;
449 ihv3->csum = ip_compute_csum((void *)ihv3, sizeof(*ihv3));
450 break;
451 }
452
453 skb_put(skb, igmp_hdr_size);
Herbert Xueb1d1642010-02-27 19:41:45 +0000454 __skb_pull(skb, sizeof(*eth));
455
456out:
457 return skb;
458}
459
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000460#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900461static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200462 const struct in6_addr *grp,
463 u8 *igmp_type)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900464{
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100465 struct mld2_query *mld2q;
466 unsigned long interval;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900467 struct ipv6hdr *ip6h;
468 struct mld_msg *mldq;
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100469 size_t mld_hdr_size;
470 struct sk_buff *skb;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900471 struct ethhdr *eth;
472 u8 *hopopt;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900473
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100474 mld_hdr_size = sizeof(*mldq);
475 if (br->multicast_mld_version == 2)
476 mld_hdr_size = sizeof(*mld2q);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900477 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100478 8 + mld_hdr_size);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900479 if (!skb)
480 goto out;
481
482 skb->protocol = htons(ETH_P_IPV6);
483
484 /* Ethernet header */
485 skb_reset_mac_header(skb);
486 eth = eth_hdr(skb);
487
Joe Perchese5a727f2014-02-23 00:05:25 -0800488 ether_addr_copy(eth->h_source, br->dev->dev_addr);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900489 eth->h_proto = htons(ETH_P_IPV6);
490 skb_put(skb, sizeof(*eth));
491
492 /* IPv6 header + HbH option */
493 skb_set_network_header(skb, skb->len);
494 ip6h = ipv6_hdr(skb);
495
496 *(__force __be32 *)ip6h = htonl(0x60000000);
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100497 ip6h->payload_len = htons(8 + mld_hdr_size);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900498 ip6h->nexthdr = IPPROTO_HOPOPTS;
499 ip6h->hop_limit = 1;
Linus Lüssinga7bff752011-03-22 11:40:32 +0000500 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
Ulrich Weberd1d81d42012-03-05 04:52:44 +0000501 if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
502 &ip6h->saddr)) {
503 kfree_skb(skb);
daniel0888d5f2016-06-24 12:35:18 +0200504 br->has_ipv6_addr = 0;
Ulrich Weberd1d81d42012-03-05 04:52:44 +0000505 return NULL;
506 }
daniel0888d5f2016-06-24 12:35:18 +0200507
508 br->has_ipv6_addr = 1;
Linus Lüssing36cff5a2011-02-17 08:17:51 +0000509 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900510
511 hopopt = (u8 *)(ip6h + 1);
512 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
513 hopopt[1] = 0; /* length of HbH */
514 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
515 hopopt[3] = 2; /* Length of RA Option */
516 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
517 hopopt[5] = 0;
Eldad Zack1de5a712012-05-17 06:00:25 +0000518 hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */
519 hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900520
521 skb_put(skb, sizeof(*ip6h) + 8);
522
523 /* ICMPv6 */
524 skb_set_transport_header(skb, skb->len);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200525 interval = ipv6_addr_any(grp) ?
Linus Lüssing32de8682013-06-16 23:20:34 +0200526 br->multicast_query_response_interval :
527 br->multicast_last_member_interval;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200528 *igmp_type = ICMPV6_MGM_QUERY;
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100529 switch (br->multicast_mld_version) {
530 case 1:
531 mldq = (struct mld_msg *)icmp6_hdr(skb);
532 mldq->mld_type = ICMPV6_MGM_QUERY;
533 mldq->mld_code = 0;
534 mldq->mld_cksum = 0;
535 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
536 mldq->mld_reserved = 0;
537 mldq->mld_mca = *grp;
538 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
539 sizeof(*mldq), IPPROTO_ICMPV6,
540 csum_partial(mldq,
541 sizeof(*mldq),
542 0));
543 break;
544 case 2:
545 mld2q = (struct mld2_query *)icmp6_hdr(skb);
Lance Richardson53631a52017-01-16 18:11:35 -0500546 mld2q->mld2q_mrc = htons((u16)jiffies_to_msecs(interval));
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100547 mld2q->mld2q_type = ICMPV6_MGM_QUERY;
548 mld2q->mld2q_code = 0;
549 mld2q->mld2q_cksum = 0;
550 mld2q->mld2q_resv1 = 0;
551 mld2q->mld2q_resv2 = 0;
552 mld2q->mld2q_suppress = 0;
553 mld2q->mld2q_qrv = 2;
554 mld2q->mld2q_nsrcs = 0;
555 mld2q->mld2q_qqic = br->multicast_query_interval / HZ;
556 mld2q->mld2q_mca = *grp;
557 mld2q->mld2q_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
558 sizeof(*mld2q),
559 IPPROTO_ICMPV6,
560 csum_partial(mld2q,
561 sizeof(*mld2q),
562 0));
563 break;
564 }
565 skb_put(skb, mld_hdr_size);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900566
567 __skb_pull(skb, sizeof(*eth));
568
569out:
570 return skb;
571}
572#endif
573
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900574static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200575 struct br_ip *addr,
576 u8 *igmp_type)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900577{
578 switch (addr->proto) {
579 case htons(ETH_P_IP):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200580 return br_ip4_multicast_alloc_query(br, addr->u.ip4, igmp_type);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000581#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900582 case htons(ETH_P_IPV6):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200583 return br_ip6_multicast_alloc_query(br, &addr->u.ip6,
584 igmp_type);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900585#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900586 }
587 return NULL;
588}
589
Herbert Xueb1d1642010-02-27 19:41:45 +0000590static struct net_bridge_mdb_entry *br_multicast_get_group(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900591 struct net_bridge *br, struct net_bridge_port *port,
592 struct br_ip *group, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +0000593{
Eric Dumazete8051682010-11-15 06:38:10 +0000594 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000595 struct net_bridge_mdb_entry *mp;
Eric Dumazet95c96172012-04-15 05:58:06 +0000596 unsigned int count = 0;
597 unsigned int max;
Herbert Xueb1d1642010-02-27 19:41:45 +0000598 int elasticity;
599 int err;
600
Eric Dumazete8051682010-11-15 06:38:10 +0000601 mdb = rcu_dereference_protected(br->mdb, 1);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800602 hlist_for_each_entry(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000603 count++;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900604 if (unlikely(br_ip_equal(group, &mp->addr)))
Herbert Xueb1d1642010-02-27 19:41:45 +0000605 return mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000606 }
607
608 elasticity = 0;
609 max = mdb->max;
610
611 if (unlikely(count > br->hash_elasticity && count)) {
612 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000613 br_info(br, "Multicast hash table "
614 "chain limit reached: %s\n",
615 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000616
617 elasticity = br->hash_elasticity;
618 }
619
620 if (mdb->size >= max) {
621 max *= 2;
Thomas Graf036be6d2012-07-10 22:29:19 +0000622 if (unlikely(max > br->hash_max)) {
623 br_warn(br, "Multicast hash table maximum of %d "
624 "reached, disabling snooping: %s\n",
625 br->hash_max,
626 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000627 err = -E2BIG;
628disable:
629 br->multicast_disabled = 1;
630 goto err;
631 }
632 }
633
634 if (max > mdb->max || elasticity) {
635 if (mdb->old) {
636 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000637 br_info(br, "Multicast hash table "
638 "on fire: %s\n",
639 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000640 err = -EEXIST;
641 goto err;
642 }
643
644 err = br_mdb_rehash(&br->mdb, max, elasticity);
645 if (err) {
stephen hemminger28a16c92010-05-10 09:31:09 +0000646 br_warn(br, "Cannot rehash multicast "
647 "hash table, disabling snooping: %s, %d, %d\n",
648 port ? port->dev->name : br->dev->name,
649 mdb->size, err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000650 goto disable;
651 }
652
653 err = -EAGAIN;
654 goto err;
655 }
656
657 return NULL;
658
659err:
660 mp = ERR_PTR(err);
661 return mp;
662}
663
Cong Wangcfd56752012-12-11 22:23:08 +0000664struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100665 struct net_bridge_port *p,
666 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000667{
Eric Dumazete8051682010-11-15 06:38:10 +0000668 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000669 struct net_bridge_mdb_entry *mp;
670 int hash;
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000671 int err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000672
Eric Dumazete8051682010-11-15 06:38:10 +0000673 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000674 if (!mdb) {
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000675 err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0);
676 if (err)
677 return ERR_PTR(err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000678 goto rehash;
679 }
680
681 hash = br_ip_hash(mdb, group);
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100682 mp = br_multicast_get_group(br, p, group, hash);
Herbert Xueb1d1642010-02-27 19:41:45 +0000683 switch (PTR_ERR(mp)) {
684 case 0:
685 break;
686
687 case -EAGAIN:
688rehash:
Eric Dumazete8051682010-11-15 06:38:10 +0000689 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000690 hash = br_ip_hash(mdb, group);
691 break;
692
693 default:
694 goto out;
695 }
696
697 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
698 if (unlikely(!mp))
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000699 return ERR_PTR(-ENOMEM);
Herbert Xueb1d1642010-02-27 19:41:45 +0000700
701 mp->br = br;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900702 mp->addr = *group;
Eric Dumazet1faabf22013-07-19 20:07:16 -0700703 setup_timer(&mp->timer, br_multicast_group_expired,
704 (unsigned long)mp);
705
Herbert Xueb1d1642010-02-27 19:41:45 +0000706 hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
707 mdb->size++;
708
709out:
710 return mp;
711}
712
Cong Wangcfd56752012-12-11 22:23:08 +0000713struct net_bridge_port_group *br_multicast_new_port_group(
714 struct net_bridge_port *port,
715 struct br_ip *group,
Amerigo Wangccb1c312012-12-14 22:09:51 +0000716 struct net_bridge_port_group __rcu *next,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100717 unsigned char flags,
718 const unsigned char *src)
Cong Wangcfd56752012-12-11 22:23:08 +0000719{
720 struct net_bridge_port_group *p;
721
722 p = kzalloc(sizeof(*p), GFP_ATOMIC);
723 if (unlikely(!p))
724 return NULL;
725
726 p->addr = *group;
727 p->port = port;
Elad Raz9d06b6d2016-02-03 09:57:05 +0100728 p->flags = flags;
stephen hemmingereca2a432012-12-13 06:51:28 +0000729 rcu_assign_pointer(p->next, next);
Cong Wangcfd56752012-12-11 22:23:08 +0000730 hlist_add_head(&p->mglist, &port->mglist);
731 setup_timer(&p->timer, br_multicast_port_group_expired,
732 (unsigned long)p);
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100733
734 if (src)
735 memcpy(p->eth_addr, src, ETH_ALEN);
736 else
737 memset(p->eth_addr, 0xff, ETH_ALEN);
738
Cong Wangcfd56752012-12-11 22:23:08 +0000739 return p;
740}
741
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100742static bool br_port_group_equal(struct net_bridge_port_group *p,
743 struct net_bridge_port *port,
744 const unsigned char *src)
745{
746 if (p->port != port)
747 return false;
748
749 if (!(port->flags & BR_MULTICAST_TO_UNICAST))
750 return true;
751
752 return ether_addr_equal(src, p->eth_addr);
753}
754
Herbert Xueb1d1642010-02-27 19:41:45 +0000755static int br_multicast_add_group(struct net_bridge *br,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900756 struct net_bridge_port *port,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100757 struct br_ip *group,
758 const unsigned char *src)
Herbert Xueb1d1642010-02-27 19:41:45 +0000759{
Eric Dumazete8051682010-11-15 06:38:10 +0000760 struct net_bridge_port_group __rcu **pp;
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100761 struct net_bridge_port_group *p;
762 struct net_bridge_mdb_entry *mp;
Linus Lüssing454594f2013-10-20 00:58:57 +0200763 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +0000764 int err;
765
Herbert Xueb1d1642010-02-27 19:41:45 +0000766 spin_lock(&br->multicast_lock);
767 if (!netif_running(br->dev) ||
768 (port && port->state == BR_STATE_DISABLED))
769 goto out;
770
771 mp = br_multicast_new_group(br, port, group);
772 err = PTR_ERR(mp);
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000773 if (IS_ERR(mp))
Herbert Xueb1d1642010-02-27 19:41:45 +0000774 goto err;
775
776 if (!port) {
Herbert Xu8a870172011-02-12 01:05:42 -0800777 mp->mglist = true;
Linus Lüssing454594f2013-10-20 00:58:57 +0200778 mod_timer(&mp->timer, now + br->multicast_membership_interval);
Herbert Xueb1d1642010-02-27 19:41:45 +0000779 goto out;
780 }
781
Eric Dumazete8051682010-11-15 06:38:10 +0000782 for (pp = &mp->ports;
783 (p = mlock_dereference(*pp, br)) != NULL;
784 pp = &p->next) {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100785 if (br_port_group_equal(p, port, src))
Linus Lüssing454594f2013-10-20 00:58:57 +0200786 goto found;
Herbert Xueb1d1642010-02-27 19:41:45 +0000787 if ((unsigned long)p->port < (unsigned long)port)
788 break;
789 }
790
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100791 p = br_multicast_new_port_group(port, group, *pp, 0, src);
Herbert Xueb1d1642010-02-27 19:41:45 +0000792 if (unlikely(!p))
793 goto err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000794 rcu_assign_pointer(*pp, p);
Elad Raz45ebcce2016-04-21 12:52:45 +0200795 br_mdb_notify(br->dev, port, group, RTM_NEWMDB, 0);
Herbert Xueb1d1642010-02-27 19:41:45 +0000796
Linus Lüssing454594f2013-10-20 00:58:57 +0200797found:
798 mod_timer(&p->timer, now + br->multicast_membership_interval);
Herbert Xueb1d1642010-02-27 19:41:45 +0000799out:
800 err = 0;
801
802err:
803 spin_unlock(&br->multicast_lock);
804 return err;
805}
806
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900807static int br_ip4_multicast_add_group(struct net_bridge *br,
808 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000809 __be32 group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100810 __u16 vid,
811 const unsigned char *src)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900812{
813 struct br_ip br_group;
814
815 if (ipv4_is_local_multicast(group))
816 return 0;
817
818 br_group.u.ip4 = group;
819 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000820 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900821
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100822 return br_multicast_add_group(br, port, &br_group, src);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900823}
824
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000825#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900826static int br_ip6_multicast_add_group(struct net_bridge *br,
827 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000828 const struct in6_addr *group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100829 __u16 vid,
830 const unsigned char *src)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900831{
832 struct br_ip br_group;
833
Linus Lüssing3c3769e2013-09-04 02:13:39 +0200834 if (ipv6_addr_is_ll_all_nodes(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900835 return 0;
836
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000837 br_group.u.ip6 = *group;
Linus Lüssing9cc6e0c2011-02-15 13:19:17 +0000838 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000839 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900840
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100841 return br_multicast_add_group(br, port, &br_group, src);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900842}
843#endif
844
Herbert Xueb1d1642010-02-27 19:41:45 +0000845static void br_multicast_router_expired(unsigned long data)
846{
847 struct net_bridge_port *port = (void *)data;
848 struct net_bridge *br = port->br;
849
850 spin_lock(&br->multicast_lock);
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +0100851 if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
852 port->multicast_router == MDB_RTR_TYPE_PERM ||
Herbert Xueb1d1642010-02-27 19:41:45 +0000853 timer_pending(&port->multicast_router_timer) ||
854 hlist_unhashed(&port->rlist))
855 goto out;
856
857 hlist_del_init_rcu(&port->rlist);
Satish Ashok949f1e32015-07-23 05:00:53 -0700858 br_rtr_notify(br->dev, port, RTM_DELMDB);
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +0100859 /* Don't allow timer refresh if the router expired */
860 if (port->multicast_router == MDB_RTR_TYPE_TEMP)
861 port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Herbert Xueb1d1642010-02-27 19:41:45 +0000862
863out:
864 spin_unlock(&br->multicast_lock);
865}
866
867static void br_multicast_local_router_expired(unsigned long data)
868{
869}
870
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200871static void br_multicast_querier_expired(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +0200872 struct bridge_mcast_own_query *query)
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000873{
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000874 spin_lock(&br->multicast_lock);
875 if (!netif_running(br->dev) || br->multicast_disabled)
876 goto out;
877
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200878 br_multicast_start_querier(br, query);
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000879
880out:
881 spin_unlock(&br->multicast_lock);
882}
883
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200884static void br_ip4_multicast_querier_expired(unsigned long data)
885{
886 struct net_bridge *br = (void *)data;
887
Linus Lüssing90010b32014-06-07 18:26:26 +0200888 br_multicast_querier_expired(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200889}
890
891#if IS_ENABLED(CONFIG_IPV6)
892static void br_ip6_multicast_querier_expired(unsigned long data)
893{
894 struct net_bridge *br = (void *)data;
895
Linus Lüssing90010b32014-06-07 18:26:26 +0200896 br_multicast_querier_expired(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200897}
898#endif
899
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200900static void br_multicast_select_own_querier(struct net_bridge *br,
901 struct br_ip *ip,
902 struct sk_buff *skb)
903{
904 if (ip->proto == htons(ETH_P_IP))
905 br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr;
906#if IS_ENABLED(CONFIG_IPV6)
907 else
908 br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr;
909#endif
910}
911
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900912static void __br_multicast_send_query(struct net_bridge *br,
913 struct net_bridge_port *port,
914 struct br_ip *ip)
Herbert Xueb1d1642010-02-27 19:41:45 +0000915{
Herbert Xueb1d1642010-02-27 19:41:45 +0000916 struct sk_buff *skb;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200917 u8 igmp_type;
Herbert Xueb1d1642010-02-27 19:41:45 +0000918
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200919 skb = br_multicast_alloc_query(br, ip, &igmp_type);
Herbert Xueb1d1642010-02-27 19:41:45 +0000920 if (!skb)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900921 return;
Herbert Xueb1d1642010-02-27 19:41:45 +0000922
923 if (port) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000924 skb->dev = port->dev;
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -0700925 br_multicast_count(br, port, skb, igmp_type,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200926 BR_MCAST_DIR_TX);
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500927 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
928 dev_net(port->dev), NULL, skb, NULL, skb->dev,
Linus Lüssingf0b4eec2014-11-17 12:20:28 +0100929 br_dev_queue_push_xmit);
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200930 } else {
931 br_multicast_select_own_querier(br, ip, skb);
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -0700932 br_multicast_count(br, port, skb, igmp_type,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200933 BR_MCAST_DIR_RX);
Herbert Xueb1d1642010-02-27 19:41:45 +0000934 netif_rx(skb);
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200935 }
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900936}
Herbert Xueb1d1642010-02-27 19:41:45 +0000937
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900938static void br_multicast_send_query(struct net_bridge *br,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200939 struct net_bridge_port *port,
Linus Lüssing90010b32014-06-07 18:26:26 +0200940 struct bridge_mcast_own_query *own_query)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900941{
Linus Lüssing90010b32014-06-07 18:26:26 +0200942 struct bridge_mcast_other_query *other_query = NULL;
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100943 struct br_ip br_group;
944 unsigned long time;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900945
946 if (!netif_running(br->dev) || br->multicast_disabled ||
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200947 !br->multicast_querier)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900948 return;
949
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900950 memset(&br_group.u, 0, sizeof(br_group.u));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900951
Linus Lüssing90010b32014-06-07 18:26:26 +0200952 if (port ? (own_query == &port->ip4_own_query) :
953 (own_query == &br->ip4_own_query)) {
954 other_query = &br->ip4_other_query;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200955 br_group.proto = htons(ETH_P_IP);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000956#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200957 } else {
Linus Lüssing90010b32014-06-07 18:26:26 +0200958 other_query = &br->ip6_other_query;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200959 br_group.proto = htons(ETH_P_IPV6);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900960#endif
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200961 }
962
Linus Lüssing90010b32014-06-07 18:26:26 +0200963 if (!other_query || timer_pending(&other_query->timer))
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200964 return;
965
966 __br_multicast_send_query(br, port, &br_group);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900967
Herbert Xueb1d1642010-02-27 19:41:45 +0000968 time = jiffies;
Linus Lüssing90010b32014-06-07 18:26:26 +0200969 time += own_query->startup_sent < br->multicast_startup_query_count ?
Herbert Xueb1d1642010-02-27 19:41:45 +0000970 br->multicast_startup_query_interval :
971 br->multicast_query_interval;
Linus Lüssing90010b32014-06-07 18:26:26 +0200972 mod_timer(&own_query->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +0000973}
974
Linus Lüssing90010b32014-06-07 18:26:26 +0200975static void
976br_multicast_port_query_expired(struct net_bridge_port *port,
977 struct bridge_mcast_own_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +0000978{
Herbert Xueb1d1642010-02-27 19:41:45 +0000979 struct net_bridge *br = port->br;
980
981 spin_lock(&br->multicast_lock);
Dan Carpenter02a780c2010-03-06 01:14:09 +0000982 if (port->state == BR_STATE_DISABLED ||
983 port->state == BR_STATE_BLOCKING)
Herbert Xueb1d1642010-02-27 19:41:45 +0000984 goto out;
985
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200986 if (query->startup_sent < br->multicast_startup_query_count)
987 query->startup_sent++;
Herbert Xueb1d1642010-02-27 19:41:45 +0000988
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200989 br_multicast_send_query(port->br, port, query);
Herbert Xueb1d1642010-02-27 19:41:45 +0000990
991out:
992 spin_unlock(&br->multicast_lock);
993}
994
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200995static void br_ip4_multicast_port_query_expired(unsigned long data)
996{
997 struct net_bridge_port *port = (void *)data;
998
Linus Lüssing90010b32014-06-07 18:26:26 +0200999 br_multicast_port_query_expired(port, &port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001000}
1001
1002#if IS_ENABLED(CONFIG_IPV6)
1003static void br_ip6_multicast_port_query_expired(unsigned long data)
1004{
1005 struct net_bridge_port *port = (void *)data;
1006
Linus Lüssing90010b32014-06-07 18:26:26 +02001007 br_multicast_port_query_expired(port, &port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001008}
1009#endif
1010
Nogah Frankel147c1e92017-02-09 14:54:40 +01001011static void br_mc_disabled_update(struct net_device *dev, bool value)
1012{
1013 struct switchdev_attr attr = {
1014 .orig_dev = dev,
1015 .id = SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED,
1016 .flags = SWITCHDEV_F_DEFER,
1017 .u.mc_disabled = value,
1018 };
1019
1020 switchdev_port_attr_set(dev, &attr);
1021}
1022
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001023int br_multicast_add_port(struct net_bridge_port *port)
Herbert Xueb1d1642010-02-27 19:41:45 +00001024{
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001025 port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Herbert Xueb1d1642010-02-27 19:41:45 +00001026
1027 setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
1028 (unsigned long)port);
Linus Lüssing90010b32014-06-07 18:26:26 +02001029 setup_timer(&port->ip4_own_query.timer,
1030 br_ip4_multicast_port_query_expired, (unsigned long)port);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001031#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001032 setup_timer(&port->ip6_own_query.timer,
1033 br_ip6_multicast_port_query_expired, (unsigned long)port);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001034#endif
Nogah Frankel147c1e92017-02-09 14:54:40 +01001035 br_mc_disabled_update(port->dev, port->br->multicast_disabled);
1036
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001037 port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
1038 if (!port->mcast_stats)
1039 return -ENOMEM;
1040
1041 return 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001042}
1043
1044void br_multicast_del_port(struct net_bridge_port *port)
1045{
Satish Ashoke10177a2015-07-15 07:16:51 -07001046 struct net_bridge *br = port->br;
1047 struct net_bridge_port_group *pg;
1048 struct hlist_node *n;
1049
1050 /* Take care of the remaining groups, only perm ones should be left */
1051 spin_lock_bh(&br->multicast_lock);
1052 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
1053 br_multicast_del_pg(br, pg);
1054 spin_unlock_bh(&br->multicast_lock);
Herbert Xueb1d1642010-02-27 19:41:45 +00001055 del_timer_sync(&port->multicast_router_timer);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001056 free_percpu(port->mcast_stats);
Herbert Xueb1d1642010-02-27 19:41:45 +00001057}
1058
Linus Lüssing90010b32014-06-07 18:26:26 +02001059static void br_multicast_enable(struct bridge_mcast_own_query *query)
Herbert Xu561f1102010-02-27 19:41:50 +00001060{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001061 query->startup_sent = 0;
Herbert Xu561f1102010-02-27 19:41:50 +00001062
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001063 if (try_to_del_timer_sync(&query->timer) >= 0 ||
1064 del_timer(&query->timer))
1065 mod_timer(&query->timer, jiffies);
Herbert Xu561f1102010-02-27 19:41:50 +00001066}
1067
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02001068static void __br_multicast_enable_port(struct net_bridge_port *port)
Herbert Xueb1d1642010-02-27 19:41:45 +00001069{
1070 struct net_bridge *br = port->br;
1071
Herbert Xueb1d1642010-02-27 19:41:45 +00001072 if (br->multicast_disabled || !netif_running(br->dev))
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02001073 return;
Herbert Xueb1d1642010-02-27 19:41:45 +00001074
Linus Lüssing90010b32014-06-07 18:26:26 +02001075 br_multicast_enable(&port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001076#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001077 br_multicast_enable(&port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001078#endif
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001079 if (port->multicast_router == MDB_RTR_TYPE_PERM &&
1080 hlist_unhashed(&port->rlist))
Satish Ashok754bc542015-06-19 01:22:57 -07001081 br_multicast_add_router(br, port);
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02001082}
Herbert Xueb1d1642010-02-27 19:41:45 +00001083
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02001084void br_multicast_enable_port(struct net_bridge_port *port)
1085{
1086 struct net_bridge *br = port->br;
1087
1088 spin_lock(&br->multicast_lock);
1089 __br_multicast_enable_port(port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001090 spin_unlock(&br->multicast_lock);
1091}
1092
1093void br_multicast_disable_port(struct net_bridge_port *port)
1094{
1095 struct net_bridge *br = port->br;
1096 struct net_bridge_port_group *pg;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001097 struct hlist_node *n;
Herbert Xueb1d1642010-02-27 19:41:45 +00001098
1099 spin_lock(&br->multicast_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001100 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
Elad Raz9d06b6d2016-02-03 09:57:05 +01001101 if (!(pg->flags & MDB_PG_FLAGS_PERMANENT))
Satish Ashoke10177a2015-07-15 07:16:51 -07001102 br_multicast_del_pg(br, pg);
Herbert Xueb1d1642010-02-27 19:41:45 +00001103
Satish Ashok949f1e32015-07-23 05:00:53 -07001104 if (!hlist_unhashed(&port->rlist)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001105 hlist_del_init_rcu(&port->rlist);
Satish Ashok949f1e32015-07-23 05:00:53 -07001106 br_rtr_notify(br->dev, port, RTM_DELMDB);
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001107 /* Don't allow timer refresh if disabling */
1108 if (port->multicast_router == MDB_RTR_TYPE_TEMP)
1109 port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Satish Ashok949f1e32015-07-23 05:00:53 -07001110 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001111 del_timer(&port->multicast_router_timer);
Linus Lüssing90010b32014-06-07 18:26:26 +02001112 del_timer(&port->ip4_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001113#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001114 del_timer(&port->ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001115#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001116 spin_unlock(&br->multicast_lock);
1117}
1118
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001119static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
1120 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001121 struct sk_buff *skb,
1122 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001123{
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001124 const unsigned char *src;
Herbert Xueb1d1642010-02-27 19:41:45 +00001125 struct igmpv3_report *ih;
1126 struct igmpv3_grec *grec;
1127 int i;
1128 int len;
1129 int num;
1130 int type;
1131 int err = 0;
1132 __be32 group;
1133
Herbert Xueb1d1642010-02-27 19:41:45 +00001134 ih = igmpv3_report_hdr(skb);
1135 num = ntohs(ih->ngrec);
Linus Lüssingc2d4fbd2015-09-11 18:39:48 +02001136 len = skb_transport_offset(skb) + sizeof(*ih);
Herbert Xueb1d1642010-02-27 19:41:45 +00001137
1138 for (i = 0; i < num; i++) {
1139 len += sizeof(*grec);
1140 if (!pskb_may_pull(skb, len))
1141 return -EINVAL;
1142
Herbert Xufd218cf2010-04-07 21:20:47 -07001143 grec = (void *)(skb->data + len - sizeof(*grec));
Herbert Xueb1d1642010-02-27 19:41:45 +00001144 group = grec->grec_mca;
1145 type = grec->grec_type;
1146
Eric Dumazet8eabf952010-04-20 03:20:05 +00001147 len += ntohs(grec->grec_nsrcs) * 4;
Herbert Xueb1d1642010-02-27 19:41:45 +00001148 if (!pskb_may_pull(skb, len))
1149 return -EINVAL;
1150
1151 /* We treat this as an IGMPv2 report for now. */
1152 switch (type) {
1153 case IGMPV3_MODE_IS_INCLUDE:
1154 case IGMPV3_MODE_IS_EXCLUDE:
1155 case IGMPV3_CHANGE_TO_INCLUDE:
1156 case IGMPV3_CHANGE_TO_EXCLUDE:
1157 case IGMPV3_ALLOW_NEW_SOURCES:
1158 case IGMPV3_BLOCK_OLD_SOURCES:
1159 break;
1160
1161 default:
1162 continue;
1163 }
1164
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001165 src = eth_hdr(skb)->h_source;
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001166 if ((type == IGMPV3_CHANGE_TO_INCLUDE ||
1167 type == IGMPV3_MODE_IS_INCLUDE) &&
1168 ntohs(grec->grec_nsrcs) == 0) {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001169 br_ip4_multicast_leave_group(br, port, group, vid, src);
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001170 } else {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001171 err = br_ip4_multicast_add_group(br, port, group, vid,
1172 src);
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001173 if (err)
1174 break;
1175 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001176 }
1177
1178 return err;
1179}
1180
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001181#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001182static int br_ip6_multicast_mld2_report(struct net_bridge *br,
1183 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001184 struct sk_buff *skb,
1185 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001186{
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001187 const unsigned char *src;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001188 struct icmp6hdr *icmp6h;
1189 struct mld2_grec *grec;
1190 int i;
1191 int len;
1192 int num;
1193 int err = 0;
1194
1195 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
1196 return -EINVAL;
1197
1198 icmp6h = icmp6_hdr(skb);
1199 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
Linus Lüssingc2d4fbd2015-09-11 18:39:48 +02001200 len = skb_transport_offset(skb) + sizeof(*icmp6h);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001201
1202 for (i = 0; i < num; i++) {
1203 __be16 *nsrcs, _nsrcs;
1204
1205 nsrcs = skb_header_pointer(skb,
1206 len + offsetof(struct mld2_grec,
Linus Lüssing649e9842011-02-15 13:19:18 +00001207 grec_nsrcs),
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001208 sizeof(_nsrcs), &_nsrcs);
1209 if (!nsrcs)
1210 return -EINVAL;
1211
1212 if (!pskb_may_pull(skb,
1213 len + sizeof(*grec) +
Linus Lüssingd41db9f2011-02-15 13:19:19 +00001214 sizeof(struct in6_addr) * ntohs(*nsrcs)))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001215 return -EINVAL;
1216
1217 grec = (struct mld2_grec *)(skb->data + len);
Linus Lüssingd41db9f2011-02-15 13:19:19 +00001218 len += sizeof(*grec) +
1219 sizeof(struct in6_addr) * ntohs(*nsrcs);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001220
1221 /* We treat these as MLDv1 reports for now. */
1222 switch (grec->grec_type) {
1223 case MLD2_MODE_IS_INCLUDE:
1224 case MLD2_MODE_IS_EXCLUDE:
1225 case MLD2_CHANGE_TO_INCLUDE:
1226 case MLD2_CHANGE_TO_EXCLUDE:
1227 case MLD2_ALLOW_NEW_SOURCES:
1228 case MLD2_BLOCK_OLD_SOURCES:
1229 break;
1230
1231 default:
1232 continue;
1233 }
1234
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001235 src = eth_hdr(skb)->h_source;
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001236 if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE ||
1237 grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
1238 ntohs(*nsrcs) == 0) {
1239 br_ip6_multicast_leave_group(br, port, &grec->grec_mca,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001240 vid, src);
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001241 } else {
1242 err = br_ip6_multicast_add_group(br, port,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001243 &grec->grec_mca, vid,
1244 src);
Davide Caratti92642512016-08-31 14:16:44 +02001245 if (err)
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001246 break;
1247 }
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001248 }
1249
1250 return err;
1251}
1252#endif
1253
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001254static bool br_ip4_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001255 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001256 __be32 saddr)
1257{
1258 if (!timer_pending(&br->ip4_own_query.timer) &&
1259 !timer_pending(&br->ip4_other_query.timer))
1260 goto update;
1261
1262 if (!br->ip4_querier.addr.u.ip4)
1263 goto update;
1264
1265 if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4))
1266 goto update;
1267
1268 return false;
1269
1270update:
1271 br->ip4_querier.addr.u.ip4 = saddr;
1272
Linus Lüssing2cd41432014-06-07 18:26:29 +02001273 /* update protected by general multicast_lock by caller */
1274 rcu_assign_pointer(br->ip4_querier.port, port);
1275
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001276 return true;
1277}
1278
1279#if IS_ENABLED(CONFIG_IPV6)
1280static bool br_ip6_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001281 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001282 struct in6_addr *saddr)
1283{
1284 if (!timer_pending(&br->ip6_own_query.timer) &&
1285 !timer_pending(&br->ip6_other_query.timer))
1286 goto update;
1287
1288 if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0)
1289 goto update;
1290
1291 return false;
1292
1293update:
1294 br->ip6_querier.addr.u.ip6 = *saddr;
1295
Linus Lüssing2cd41432014-06-07 18:26:29 +02001296 /* update protected by general multicast_lock by caller */
1297 rcu_assign_pointer(br->ip6_querier.port, port);
1298
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001299 return true;
1300}
1301#endif
1302
1303static bool br_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001304 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001305 struct br_ip *saddr)
1306{
1307 switch (saddr->proto) {
1308 case htons(ETH_P_IP):
Linus Lüssing2cd41432014-06-07 18:26:29 +02001309 return br_ip4_multicast_select_querier(br, port, saddr->u.ip4);
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001310#if IS_ENABLED(CONFIG_IPV6)
1311 case htons(ETH_P_IPV6):
Linus Lüssing2cd41432014-06-07 18:26:29 +02001312 return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6);
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001313#endif
1314 }
1315
1316 return false;
1317}
1318
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001319static void
Linus Lüssing90010b32014-06-07 18:26:26 +02001320br_multicast_update_query_timer(struct net_bridge *br,
1321 struct bridge_mcast_other_query *query,
1322 unsigned long max_delay)
Linus Lüssingb00589a2013-08-01 01:06:20 +02001323{
Linus Lüssing90010b32014-06-07 18:26:26 +02001324 if (!timer_pending(&query->timer))
1325 query->delay_time = jiffies + max_delay;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001326
Linus Lüssing90010b32014-06-07 18:26:26 +02001327 mod_timer(&query->timer, jiffies + br->multicast_querier_interval);
Linus Lüssingb00589a2013-08-01 01:06:20 +02001328}
1329
stephen hemminger7e80c122010-04-27 15:01:04 +00001330/*
Cong Wang7c776022013-06-21 15:37:25 +08001331 * Add port to router_list
stephen hemminger7e80c122010-04-27 15:01:04 +00001332 * list is maintained ordered by pointer value
1333 * and locked by br->multicast_lock and RCU
1334 */
Herbert Xu0909e112010-02-27 19:41:49 +00001335static void br_multicast_add_router(struct net_bridge *br,
1336 struct net_bridge_port *port)
1337{
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001338 struct net_bridge_port *p;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001339 struct hlist_node *slot = NULL;
Herbert Xu0909e112010-02-27 19:41:49 +00001340
Nikolay Aleksandrov1a040ea2015-06-09 10:23:57 -07001341 if (!hlist_unhashed(&port->rlist))
1342 return;
1343
Sasha Levinb67bfe02013-02-27 17:06:00 -08001344 hlist_for_each_entry(p, &br->router_list, rlist) {
stephen hemminger7e80c122010-04-27 15:01:04 +00001345 if ((unsigned long) port >= (unsigned long) p)
1346 break;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001347 slot = &p->rlist;
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001348 }
Herbert Xu0909e112010-02-27 19:41:49 +00001349
stephen hemminger7e80c122010-04-27 15:01:04 +00001350 if (slot)
Ken Helias1d023282014-08-06 16:09:16 -07001351 hlist_add_behind_rcu(&port->rlist, slot);
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001352 else
1353 hlist_add_head_rcu(&port->rlist, &br->router_list);
Satish Ashok949f1e32015-07-23 05:00:53 -07001354 br_rtr_notify(br->dev, port, RTM_NEWMDB);
Herbert Xu0909e112010-02-27 19:41:49 +00001355}
1356
Herbert Xueb1d1642010-02-27 19:41:45 +00001357static void br_multicast_mark_router(struct net_bridge *br,
1358 struct net_bridge_port *port)
1359{
1360 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +00001361
1362 if (!port) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001363 if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY)
Herbert Xueb1d1642010-02-27 19:41:45 +00001364 mod_timer(&br->multicast_router_timer,
1365 now + br->multicast_querier_interval);
1366 return;
1367 }
1368
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001369 if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
1370 port->multicast_router == MDB_RTR_TYPE_PERM)
Herbert Xueb1d1642010-02-27 19:41:45 +00001371 return;
1372
Herbert Xu0909e112010-02-27 19:41:49 +00001373 br_multicast_add_router(br, port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001374
Herbert Xueb1d1642010-02-27 19:41:45 +00001375 mod_timer(&port->multicast_router_timer,
1376 now + br->multicast_querier_interval);
1377}
1378
1379static void br_multicast_query_received(struct net_bridge *br,
1380 struct net_bridge_port *port,
Linus Lüssing90010b32014-06-07 18:26:26 +02001381 struct bridge_mcast_other_query *query,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001382 struct br_ip *saddr,
Linus Lüssingb00589a2013-08-01 01:06:20 +02001383 unsigned long max_delay)
Herbert Xueb1d1642010-02-27 19:41:45 +00001384{
Linus Lüssing2cd41432014-06-07 18:26:29 +02001385 if (!br_multicast_select_querier(br, port, saddr))
Herbert Xueb1d1642010-02-27 19:41:45 +00001386 return;
1387
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001388 br_multicast_update_query_timer(br, query, max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001389 br_multicast_mark_router(br, port);
1390}
1391
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001392static int br_ip4_multicast_query(struct net_bridge *br,
1393 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001394 struct sk_buff *skb,
1395 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001396{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001397 const struct iphdr *iph = ip_hdr(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001398 struct igmphdr *ih = igmp_hdr(skb);
1399 struct net_bridge_mdb_entry *mp;
1400 struct igmpv3_query *ih3;
1401 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +00001402 struct net_bridge_port_group __rcu **pp;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001403 struct br_ip saddr;
Herbert Xueb1d1642010-02-27 19:41:45 +00001404 unsigned long max_delay;
1405 unsigned long now = jiffies;
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001406 unsigned int offset = skb_transport_offset(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001407 __be32 group;
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001408 int err = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001409
1410 spin_lock(&br->multicast_lock);
1411 if (!netif_running(br->dev) ||
1412 (port && port->state == BR_STATE_DISABLED))
1413 goto out;
1414
Herbert Xueb1d1642010-02-27 19:41:45 +00001415 group = ih->group;
1416
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001417 if (skb->len == offset + sizeof(*ih)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001418 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1419
1420 if (!max_delay) {
1421 max_delay = 10 * HZ;
1422 group = 0;
1423 }
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001424 } else if (skb->len >= offset + sizeof(*ih3)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001425 ih3 = igmpv3_query_hdr(skb);
1426 if (ih3->nsrcs)
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001427 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001428
YOSHIFUJI Hideaki / 吉藤英明0ba8c9e2010-03-15 19:27:00 +00001429 max_delay = ih3->code ?
1430 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001431 } else {
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001432 goto out;
1433 }
1434
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001435 if (!group) {
1436 saddr.proto = htons(ETH_P_IP);
1437 saddr.u.ip4 = iph->saddr;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001438
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001439 br_multicast_query_received(br, port, &br->ip4_other_query,
1440 &saddr, max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001441 goto out;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001442 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001443
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001444 mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001445 if (!mp)
1446 goto out;
1447
1448 max_delay *= br->multicast_last_member_count;
1449
Herbert Xu8a870172011-02-12 01:05:42 -08001450 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001451 (timer_pending(&mp->timer) ?
1452 time_after(mp->timer.expires, now + max_delay) :
1453 try_to_del_timer_sync(&mp->timer) >= 0))
1454 mod_timer(&mp->timer, now + max_delay);
1455
Eric Dumazete8051682010-11-15 06:38:10 +00001456 for (pp = &mp->ports;
1457 (p = mlock_dereference(*pp, br)) != NULL;
1458 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001459 if (timer_pending(&p->timer) ?
1460 time_after(p->timer.expires, now + max_delay) :
1461 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001462 mod_timer(&p->timer, now + max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001463 }
1464
1465out:
1466 spin_unlock(&br->multicast_lock);
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001467 return err;
Herbert Xueb1d1642010-02-27 19:41:45 +00001468}
1469
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001470#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001471static int br_ip6_multicast_query(struct net_bridge *br,
1472 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001473 struct sk_buff *skb,
1474 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001475{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001476 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
stephen hemmingereca2a432012-12-13 06:51:28 +00001477 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001478 struct net_bridge_mdb_entry *mp;
1479 struct mld2_query *mld2q;
Eric Dumazete8051682010-11-15 06:38:10 +00001480 struct net_bridge_port_group *p;
1481 struct net_bridge_port_group __rcu **pp;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001482 struct br_ip saddr;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001483 unsigned long max_delay;
1484 unsigned long now = jiffies;
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001485 unsigned int offset = skb_transport_offset(skb);
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001486 const struct in6_addr *group = NULL;
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001487 bool is_general_query;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001488 int err = 0;
1489
1490 spin_lock(&br->multicast_lock);
1491 if (!netif_running(br->dev) ||
1492 (port && port->state == BR_STATE_DISABLED))
1493 goto out;
1494
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001495 if (skb->len == offset + sizeof(*mld)) {
1496 if (!pskb_may_pull(skb, offset + sizeof(*mld))) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001497 err = -EINVAL;
1498 goto out;
1499 }
1500 mld = (struct mld_msg *) icmp6_hdr(skb);
Li RongQing47152132012-07-09 23:56:12 +00001501 max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001502 if (max_delay)
1503 group = &mld->mld_mca;
Linus Lüssing248ba8e2013-08-06 00:32:05 +02001504 } else {
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001505 if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001506 err = -EINVAL;
1507 goto out;
1508 }
1509 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1510 if (!mld2q->mld2q_nsrcs)
1511 group = &mld2q->mld2q_mca;
Daniel Borkmanne3f5b172013-09-04 00:19:39 +02001512
1513 max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001514 }
1515
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001516 is_general_query = group && ipv6_addr_any(group);
1517
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001518 if (is_general_query) {
1519 saddr.proto = htons(ETH_P_IPV6);
1520 saddr.u.ip6 = ip6h->saddr;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001521
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001522 br_multicast_query_received(br, port, &br->ip6_other_query,
1523 &saddr, max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001524 goto out;
Linus Lüssing6c03ee82014-06-12 01:41:23 +02001525 } else if (!group) {
1526 goto out;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001527 }
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001528
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001529 mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001530 if (!mp)
1531 goto out;
1532
1533 max_delay *= br->multicast_last_member_count;
Herbert Xu8a870172011-02-12 01:05:42 -08001534 if (mp->mglist &&
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001535 (timer_pending(&mp->timer) ?
1536 time_after(mp->timer.expires, now + max_delay) :
1537 try_to_del_timer_sync(&mp->timer) >= 0))
1538 mod_timer(&mp->timer, now + max_delay);
1539
Eric Dumazete8051682010-11-15 06:38:10 +00001540 for (pp = &mp->ports;
1541 (p = mlock_dereference(*pp, br)) != NULL;
1542 pp = &p->next) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001543 if (timer_pending(&p->timer) ?
1544 time_after(p->timer.expires, now + max_delay) :
1545 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001546 mod_timer(&p->timer, now + max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001547 }
1548
1549out:
1550 spin_unlock(&br->multicast_lock);
1551 return err;
1552}
1553#endif
1554
Linus Lüssing90010b32014-06-07 18:26:26 +02001555static void
1556br_multicast_leave_group(struct net_bridge *br,
1557 struct net_bridge_port *port,
1558 struct br_ip *group,
1559 struct bridge_mcast_other_query *other_query,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001560 struct bridge_mcast_own_query *own_query,
1561 const unsigned char *src)
Herbert Xueb1d1642010-02-27 19:41:45 +00001562{
1563 struct net_bridge_mdb_htable *mdb;
1564 struct net_bridge_mdb_entry *mp;
1565 struct net_bridge_port_group *p;
1566 unsigned long now;
1567 unsigned long time;
1568
Herbert Xueb1d1642010-02-27 19:41:45 +00001569 spin_lock(&br->multicast_lock);
1570 if (!netif_running(br->dev) ||
Satish Ashok544586f2015-07-28 03:28:27 -07001571 (port && port->state == BR_STATE_DISABLED))
Herbert Xueb1d1642010-02-27 19:41:45 +00001572 goto out;
1573
Eric Dumazete8051682010-11-15 06:38:10 +00001574 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001575 mp = br_mdb_ip_get(mdb, group);
1576 if (!mp)
1577 goto out;
1578
Satish Ashok544586f2015-07-28 03:28:27 -07001579 if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
1580 struct net_bridge_port_group __rcu **pp;
1581
1582 for (pp = &mp->ports;
1583 (p = mlock_dereference(*pp, br)) != NULL;
1584 pp = &p->next) {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001585 if (!br_port_group_equal(p, port, src))
Satish Ashok544586f2015-07-28 03:28:27 -07001586 continue;
1587
1588 rcu_assign_pointer(*pp, p->next);
1589 hlist_del_init(&p->mglist);
1590 del_timer(&p->timer);
1591 call_rcu_bh(&p->rcu, br_multicast_free_pg);
Elad Raz45ebcce2016-04-21 12:52:45 +02001592 br_mdb_notify(br->dev, port, group, RTM_DELMDB,
1593 p->flags);
Satish Ashok544586f2015-07-28 03:28:27 -07001594
1595 if (!mp->ports && !mp->mglist &&
1596 netif_running(br->dev))
1597 mod_timer(&mp->timer, jiffies);
1598 }
1599 goto out;
1600 }
1601
1602 if (timer_pending(&other_query->timer))
1603 goto out;
1604
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001605 if (br->multicast_querier) {
Cong Wang6b7df112013-05-21 21:52:56 +00001606 __br_multicast_send_query(br, port, &mp->addr);
1607
1608 time = jiffies + br->multicast_last_member_count *
1609 br->multicast_last_member_interval;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001610
Linus Lüssing90010b32014-06-07 18:26:26 +02001611 mod_timer(&own_query->timer, time);
Cong Wang6b7df112013-05-21 21:52:56 +00001612
1613 for (p = mlock_dereference(mp->ports, br);
1614 p != NULL;
1615 p = mlock_dereference(p->next, br)) {
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001616 if (!br_port_group_equal(p, port, src))
Cong Wang6b7df112013-05-21 21:52:56 +00001617 continue;
1618
1619 if (!hlist_unhashed(&p->mglist) &&
1620 (timer_pending(&p->timer) ?
1621 time_after(p->timer.expires, time) :
1622 try_to_del_timer_sync(&p->timer) >= 0)) {
1623 mod_timer(&p->timer, time);
1624 }
1625
1626 break;
1627 }
1628 }
1629
Herbert Xueb1d1642010-02-27 19:41:45 +00001630 now = jiffies;
1631 time = now + br->multicast_last_member_count *
1632 br->multicast_last_member_interval;
1633
1634 if (!port) {
Linus Lüssing454594f2013-10-20 00:58:57 +02001635 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001636 (timer_pending(&mp->timer) ?
1637 time_after(mp->timer.expires, time) :
1638 try_to_del_timer_sync(&mp->timer) >= 0)) {
1639 mod_timer(&mp->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +00001640 }
Linus Lüssing454594f2013-10-20 00:58:57 +02001641
1642 goto out;
1643 }
1644
1645 for (p = mlock_dereference(mp->ports, br);
1646 p != NULL;
1647 p = mlock_dereference(p->next, br)) {
1648 if (p->port != port)
1649 continue;
1650
1651 if (!hlist_unhashed(&p->mglist) &&
1652 (timer_pending(&p->timer) ?
1653 time_after(p->timer.expires, time) :
1654 try_to_del_timer_sync(&p->timer) >= 0)) {
1655 mod_timer(&p->timer, time);
1656 }
1657
1658 break;
Herbert Xueb1d1642010-02-27 19:41:45 +00001659 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001660out:
1661 spin_unlock(&br->multicast_lock);
1662}
1663
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001664static void br_ip4_multicast_leave_group(struct net_bridge *br,
1665 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001666 __be32 group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001667 __u16 vid,
1668 const unsigned char *src)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001669{
1670 struct br_ip br_group;
Linus Lüssing90010b32014-06-07 18:26:26 +02001671 struct bridge_mcast_own_query *own_query;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001672
1673 if (ipv4_is_local_multicast(group))
1674 return;
1675
Linus Lüssing90010b32014-06-07 18:26:26 +02001676 own_query = port ? &port->ip4_own_query : &br->ip4_own_query;
1677
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001678 br_group.u.ip4 = group;
1679 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001680 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001681
Linus Lüssing90010b32014-06-07 18:26:26 +02001682 br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001683 own_query, src);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001684}
1685
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001686#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001687static void br_ip6_multicast_leave_group(struct net_bridge *br,
1688 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001689 const struct in6_addr *group,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001690 __u16 vid,
1691 const unsigned char *src)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001692{
1693 struct br_ip br_group;
Linus Lüssing90010b32014-06-07 18:26:26 +02001694 struct bridge_mcast_own_query *own_query;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001695
Linus Lüssing3c3769e2013-09-04 02:13:39 +02001696 if (ipv6_addr_is_ll_all_nodes(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001697 return;
1698
Linus Lüssing90010b32014-06-07 18:26:26 +02001699 own_query = port ? &port->ip6_own_query : &br->ip6_own_query;
1700
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001701 br_group.u.ip6 = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001702 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001703 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001704
Linus Lüssing90010b32014-06-07 18:26:26 +02001705 br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001706 own_query, src);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001707}
1708#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001709
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001710static void br_multicast_err_count(const struct net_bridge *br,
1711 const struct net_bridge_port *p,
1712 __be16 proto)
1713{
1714 struct bridge_mcast_stats __percpu *stats;
1715 struct bridge_mcast_stats *pstats;
1716
1717 if (!br->multicast_stats_enabled)
1718 return;
1719
1720 if (p)
1721 stats = p->mcast_stats;
1722 else
1723 stats = br->mcast_stats;
1724 if (WARN_ON(!stats))
1725 return;
1726
1727 pstats = this_cpu_ptr(stats);
1728
1729 u64_stats_update_begin(&pstats->syncp);
1730 switch (proto) {
1731 case htons(ETH_P_IP):
1732 pstats->mstats.igmp_parse_errors++;
1733 break;
1734#if IS_ENABLED(CONFIG_IPV6)
1735 case htons(ETH_P_IPV6):
1736 pstats->mstats.mld_parse_errors++;
1737 break;
1738#endif
1739 }
1740 u64_stats_update_end(&pstats->syncp);
1741}
1742
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001743static void br_multicast_pim(struct net_bridge *br,
1744 struct net_bridge_port *port,
1745 const struct sk_buff *skb)
1746{
1747 unsigned int offset = skb_transport_offset(skb);
1748 struct pimhdr *pimhdr, _pimhdr;
1749
1750 pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr);
1751 if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION ||
1752 pim_hdr_type(pimhdr) != PIM_TYPE_HELLO)
1753 return;
1754
1755 br_multicast_mark_router(br, port);
1756}
1757
Herbert Xueb1d1642010-02-27 19:41:45 +00001758static int br_multicast_ipv4_rcv(struct net_bridge *br,
1759 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001760 struct sk_buff *skb,
1761 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001762{
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001763 struct sk_buff *skb_trimmed = NULL;
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001764 const unsigned char *src;
Herbert Xueb1d1642010-02-27 19:41:45 +00001765 struct igmphdr *ih;
Herbert Xueb1d1642010-02-27 19:41:45 +00001766 int err;
1767
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001768 err = ip_mc_check_igmp(skb, &skb_trimmed);
Herbert Xueb1d1642010-02-27 19:41:45 +00001769
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001770 if (err == -ENOMSG) {
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001771 if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) {
Herbert Xubd4265f2011-06-23 02:39:12 +00001772 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001773 } else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) {
1774 if (ip_hdr(skb)->protocol == IPPROTO_PIM)
1775 br_multicast_pim(br, port, skb);
1776 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001777 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;
Herbert Xubd4265f2011-06-23 02:39:12 +00001781 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001782
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001783 ih = igmp_hdr(skb);
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001784 src = eth_hdr(skb)->h_source;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001785 BR_INPUT_SKB_CB(skb)->igmp = ih->type;
Herbert Xueb1d1642010-02-27 19:41:45 +00001786
1787 switch (ih->type) {
1788 case IGMP_HOST_MEMBERSHIP_REPORT:
1789 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Fernando Luis Vázquez Cao62b2bcb2011-06-13 15:04:43 +00001790 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001791 err = br_ip4_multicast_add_group(br, port, ih->group, vid, src);
Herbert Xueb1d1642010-02-27 19:41:45 +00001792 break;
1793 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001794 err = br_ip4_multicast_igmp3_report(br, port, skb_trimmed, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001795 break;
1796 case IGMP_HOST_MEMBERSHIP_QUERY:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001797 err = br_ip4_multicast_query(br, port, skb_trimmed, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001798 break;
1799 case IGMP_HOST_LEAVE_MESSAGE:
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001800 br_ip4_multicast_leave_group(br, port, ih->group, vid, src);
Herbert Xueb1d1642010-02-27 19:41:45 +00001801 break;
1802 }
1803
Linus Lüssinga5169932015-08-13 05:54:07 +02001804 if (skb_trimmed && skb_trimmed != skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001805 kfree_skb(skb_trimmed);
1806
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07001807 br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001808 BR_MCAST_DIR_RX);
1809
Herbert Xueb1d1642010-02-27 19:41:45 +00001810 return err;
1811}
1812
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001813#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001814static int br_multicast_ipv6_rcv(struct net_bridge *br,
1815 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001816 struct sk_buff *skb,
1817 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001818{
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001819 struct sk_buff *skb_trimmed = NULL;
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001820 const unsigned char *src;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001821 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001822 int err;
1823
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001824 err = ipv6_mc_check_mld(skb, &skb_trimmed);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001825
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001826 if (err == -ENOMSG) {
1827 if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
1828 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Linus Lüssing8fad9c32013-09-04 02:13:38 +02001829 return 0;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001830 } else if (err < 0) {
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001831 br_multicast_err_count(br, port, skb->protocol);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001832 return err;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001833 }
1834
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001835 mld = (struct mld_msg *)skb_transport_header(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001836 BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001837
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001838 switch (mld->mld_type) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001839 case ICMPV6_MGM_REPORT:
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001840 src = eth_hdr(skb)->h_source;
Fernando Luis Vázquez Caofc2af6c2011-06-13 15:06:58 +00001841 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001842 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid,
1843 src);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001844 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001845 case ICMPV6_MLD2_REPORT:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001846 err = br_ip6_multicast_mld2_report(br, port, skb_trimmed, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001847 break;
1848 case ICMPV6_MGM_QUERY:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001849 err = br_ip6_multicast_query(br, port, skb_trimmed, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001850 break;
1851 case ICMPV6_MGM_REDUCTION:
Felix Fietkau6db6f0e2017-01-21 21:01:32 +01001852 src = eth_hdr(skb)->h_source;
1853 br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001854 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001855 }
1856
Linus Lüssinga5169932015-08-13 05:54:07 +02001857 if (skb_trimmed && skb_trimmed != skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001858 kfree_skb(skb_trimmed);
1859
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07001860 br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001861 BR_MCAST_DIR_RX);
1862
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001863 return err;
1864}
1865#endif
1866
Herbert Xueb1d1642010-02-27 19:41:45 +00001867int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001868 struct sk_buff *skb, u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001869{
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001870 int ret = 0;
1871
YOSHIFUJI Hideaki / 吉藤英明1fafc7a2010-04-25 08:06:40 +00001872 BR_INPUT_SKB_CB(skb)->igmp = 0;
1873 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1874
Herbert Xueb1d1642010-02-27 19:41:45 +00001875 if (br->multicast_disabled)
1876 return 0;
1877
1878 switch (skb->protocol) {
1879 case htons(ETH_P_IP):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001880 ret = br_multicast_ipv4_rcv(br, port, skb, vid);
1881 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001882#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001883 case htons(ETH_P_IPV6):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001884 ret = br_multicast_ipv6_rcv(br, port, skb, vid);
1885 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001886#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001887 }
1888
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001889 return ret;
Herbert Xueb1d1642010-02-27 19:41:45 +00001890}
1891
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001892static void br_multicast_query_expired(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001893 struct bridge_mcast_own_query *query,
1894 struct bridge_mcast_querier *querier)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001895{
1896 spin_lock(&br->multicast_lock);
1897 if (query->startup_sent < br->multicast_startup_query_count)
1898 query->startup_sent++;
1899
Eric Dumazet71d9f612015-05-28 04:42:54 -07001900 RCU_INIT_POINTER(querier->port, NULL);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001901 br_multicast_send_query(br, NULL, query);
1902 spin_unlock(&br->multicast_lock);
1903}
1904
1905static void br_ip4_multicast_query_expired(unsigned long data)
Herbert Xueb1d1642010-02-27 19:41:45 +00001906{
1907 struct net_bridge *br = (void *)data;
1908
Linus Lüssing2cd41432014-06-07 18:26:29 +02001909 br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier);
Herbert Xueb1d1642010-02-27 19:41:45 +00001910}
1911
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001912#if IS_ENABLED(CONFIG_IPV6)
1913static void br_ip6_multicast_query_expired(unsigned long data)
1914{
1915 struct net_bridge *br = (void *)data;
1916
Linus Lüssing2cd41432014-06-07 18:26:29 +02001917 br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001918}
1919#endif
1920
Herbert Xueb1d1642010-02-27 19:41:45 +00001921void br_multicast_init(struct net_bridge *br)
1922{
1923 br->hash_elasticity = 4;
1924 br->hash_max = 512;
1925
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001926 br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Herbert Xuc5c23262012-04-13 02:37:42 +00001927 br->multicast_querier = 0;
Cong Wang1c8ad5b2013-05-21 21:52:54 +00001928 br->multicast_query_use_ifaddr = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001929 br->multicast_last_member_count = 2;
1930 br->multicast_startup_query_count = 2;
1931
1932 br->multicast_last_member_interval = HZ;
1933 br->multicast_query_response_interval = 10 * HZ;
1934 br->multicast_startup_query_interval = 125 * HZ / 4;
1935 br->multicast_query_interval = 125 * HZ;
1936 br->multicast_querier_interval = 255 * HZ;
1937 br->multicast_membership_interval = 260 * HZ;
1938
Linus Lüssing90010b32014-06-07 18:26:26 +02001939 br->ip4_other_query.delay_time = 0;
Linus Lüssing2cd41432014-06-07 18:26:29 +02001940 br->ip4_querier.port = NULL;
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001941 br->multicast_igmp_version = 2;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001942#if IS_ENABLED(CONFIG_IPV6)
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001943 br->multicast_mld_version = 1;
Linus Lüssing90010b32014-06-07 18:26:26 +02001944 br->ip6_other_query.delay_time = 0;
Linus Lüssing2cd41432014-06-07 18:26:29 +02001945 br->ip6_querier.port = NULL;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001946#endif
daniel0888d5f2016-06-24 12:35:18 +02001947 br->has_ipv6_addr = 1;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001948
Herbert Xueb1d1642010-02-27 19:41:45 +00001949 spin_lock_init(&br->multicast_lock);
1950 setup_timer(&br->multicast_router_timer,
1951 br_multicast_local_router_expired, 0);
Linus Lüssing90010b32014-06-07 18:26:26 +02001952 setup_timer(&br->ip4_other_query.timer,
1953 br_ip4_multicast_querier_expired, (unsigned long)br);
1954 setup_timer(&br->ip4_own_query.timer, br_ip4_multicast_query_expired,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001955 (unsigned long)br);
1956#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001957 setup_timer(&br->ip6_other_query.timer,
1958 br_ip6_multicast_querier_expired, (unsigned long)br);
1959 setup_timer(&br->ip6_own_query.timer, br_ip6_multicast_query_expired,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001960 (unsigned long)br);
1961#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001962}
1963
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001964static void __br_multicast_open(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +02001965 struct bridge_mcast_own_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +00001966{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001967 query->startup_sent = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001968
1969 if (br->multicast_disabled)
1970 return;
1971
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001972 mod_timer(&query->timer, jiffies);
1973}
1974
1975void br_multicast_open(struct net_bridge *br)
1976{
Linus Lüssing90010b32014-06-07 18:26:26 +02001977 __br_multicast_open(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001978#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001979 __br_multicast_open(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001980#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001981}
1982
1983void br_multicast_stop(struct net_bridge *br)
1984{
Herbert Xueb1d1642010-02-27 19:41:45 +00001985 del_timer_sync(&br->multicast_router_timer);
Linus Lüssing90010b32014-06-07 18:26:26 +02001986 del_timer_sync(&br->ip4_other_query.timer);
1987 del_timer_sync(&br->ip4_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001988#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001989 del_timer_sync(&br->ip6_other_query.timer);
1990 del_timer_sync(&br->ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001991#endif
Satish Ashoke10177a2015-07-15 07:16:51 -07001992}
1993
1994void br_multicast_dev_del(struct net_bridge *br)
1995{
1996 struct net_bridge_mdb_htable *mdb;
1997 struct net_bridge_mdb_entry *mp;
1998 struct hlist_node *n;
1999 u32 ver;
2000 int i;
Herbert Xueb1d1642010-02-27 19:41:45 +00002001
2002 spin_lock_bh(&br->multicast_lock);
Eric Dumazete8051682010-11-15 06:38:10 +00002003 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00002004 if (!mdb)
2005 goto out;
2006
2007 br->mdb = NULL;
2008
2009 ver = mdb->ver;
2010 for (i = 0; i < mdb->max; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08002011 hlist_for_each_entry_safe(mp, n, &mdb->mhash[i],
Herbert Xueb1d1642010-02-27 19:41:45 +00002012 hlist[ver]) {
2013 del_timer(&mp->timer);
Herbert Xueb1d1642010-02-27 19:41:45 +00002014 call_rcu_bh(&mp->rcu, br_multicast_free_group);
2015 }
2016 }
2017
2018 if (mdb->old) {
2019 spin_unlock_bh(&br->multicast_lock);
Herbert Xu10cc2b52010-03-05 21:03:35 +00002020 rcu_barrier_bh();
Herbert Xueb1d1642010-02-27 19:41:45 +00002021 spin_lock_bh(&br->multicast_lock);
2022 WARN_ON(mdb->old);
2023 }
2024
2025 mdb->old = mdb;
2026 call_rcu_bh(&mdb->rcu, br_mdb_free);
2027
2028out:
2029 spin_unlock_bh(&br->multicast_lock);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002030
2031 free_percpu(br->mcast_stats);
Herbert Xueb1d1642010-02-27 19:41:45 +00002032}
Herbert Xu0909e112010-02-27 19:41:49 +00002033
2034int br_multicast_set_router(struct net_bridge *br, unsigned long val)
2035{
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02002036 int err = -EINVAL;
Herbert Xu0909e112010-02-27 19:41:49 +00002037
2038 spin_lock_bh(&br->multicast_lock);
Herbert Xu0909e112010-02-27 19:41:49 +00002039
2040 switch (val) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002041 case MDB_RTR_TYPE_DISABLED:
2042 case MDB_RTR_TYPE_PERM:
Herbert Xu0909e112010-02-27 19:41:49 +00002043 del_timer(&br->multicast_router_timer);
2044 /* fall through */
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002045 case MDB_RTR_TYPE_TEMP_QUERY:
Herbert Xu0909e112010-02-27 19:41:49 +00002046 br->multicast_router = val;
2047 err = 0;
2048 break;
Herbert Xu0909e112010-02-27 19:41:49 +00002049 }
2050
Herbert Xu0909e112010-02-27 19:41:49 +00002051 spin_unlock_bh(&br->multicast_lock);
2052
2053 return err;
2054}
2055
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002056static void __del_port_router(struct net_bridge_port *p)
2057{
2058 if (hlist_unhashed(&p->rlist))
2059 return;
2060 hlist_del_init_rcu(&p->rlist);
2061 br_rtr_notify(p->br->dev, p, RTM_DELMDB);
2062}
2063
Herbert Xu0909e112010-02-27 19:41:49 +00002064int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
2065{
2066 struct net_bridge *br = p->br;
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01002067 unsigned long now = jiffies;
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02002068 int err = -EINVAL;
Herbert Xu0909e112010-02-27 19:41:49 +00002069
2070 spin_lock(&br->multicast_lock);
Nikolay Aleksandrov4950cfd2016-02-26 21:20:02 +01002071 if (p->multicast_router == val) {
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01002072 /* Refresh the temp router port timer */
2073 if (p->multicast_router == MDB_RTR_TYPE_TEMP)
2074 mod_timer(&p->multicast_router_timer,
2075 now + br->multicast_querier_interval);
Nikolay Aleksandrov4950cfd2016-02-26 21:20:02 +01002076 err = 0;
2077 goto unlock;
2078 }
Herbert Xu0909e112010-02-27 19:41:49 +00002079 switch (val) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002080 case MDB_RTR_TYPE_DISABLED:
2081 p->multicast_router = MDB_RTR_TYPE_DISABLED;
2082 __del_port_router(p);
Herbert Xu0909e112010-02-27 19:41:49 +00002083 del_timer(&p->multicast_router_timer);
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002084 break;
2085 case MDB_RTR_TYPE_TEMP_QUERY:
2086 p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
2087 __del_port_router(p);
2088 break;
2089 case MDB_RTR_TYPE_PERM:
2090 p->multicast_router = MDB_RTR_TYPE_PERM;
2091 del_timer(&p->multicast_router_timer);
Herbert Xu0909e112010-02-27 19:41:49 +00002092 br_multicast_add_router(br, p);
2093 break;
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01002094 case MDB_RTR_TYPE_TEMP:
2095 p->multicast_router = MDB_RTR_TYPE_TEMP;
2096 br_multicast_mark_router(br, p);
2097 break;
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002098 default:
2099 goto unlock;
Herbert Xu0909e112010-02-27 19:41:49 +00002100 }
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01002101 err = 0;
2102unlock:
Herbert Xu0909e112010-02-27 19:41:49 +00002103 spin_unlock(&br->multicast_lock);
2104
2105 return err;
2106}
Herbert Xu561f1102010-02-27 19:41:50 +00002107
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002108static void br_multicast_start_querier(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +02002109 struct bridge_mcast_own_query *query)
Herbert Xu561f1102010-02-27 19:41:50 +00002110{
2111 struct net_bridge_port *port;
Herbert Xu74857212012-04-13 02:37:42 +00002112
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002113 __br_multicast_open(br, query);
Herbert Xu74857212012-04-13 02:37:42 +00002114
2115 list_for_each_entry(port, &br->port_list, list) {
2116 if (port->state == BR_STATE_DISABLED ||
2117 port->state == BR_STATE_BLOCKING)
2118 continue;
2119
Linus Lüssing90010b32014-06-07 18:26:26 +02002120 if (query == &br->ip4_own_query)
2121 br_multicast_enable(&port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002122#if IS_ENABLED(CONFIG_IPV6)
2123 else
Linus Lüssing90010b32014-06-07 18:26:26 +02002124 br_multicast_enable(&port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002125#endif
Herbert Xu74857212012-04-13 02:37:42 +00002126 }
2127}
2128
2129int br_multicast_toggle(struct net_bridge *br, unsigned long val)
2130{
Eric Dumazete8051682010-11-15 06:38:10 +00002131 struct net_bridge_mdb_htable *mdb;
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02002132 struct net_bridge_port *port;
2133 int err = 0;
Herbert Xu561f1102010-02-27 19:41:50 +00002134
Andrey Vaginef5e0d82011-11-10 05:48:03 +00002135 spin_lock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00002136 if (br->multicast_disabled == !val)
2137 goto unlock;
2138
Nogah Frankel147c1e92017-02-09 14:54:40 +01002139 br_mc_disabled_update(br->dev, !val);
Herbert Xu561f1102010-02-27 19:41:50 +00002140 br->multicast_disabled = !val;
2141 if (br->multicast_disabled)
2142 goto unlock;
2143
Herbert Xu3a7fda02010-07-29 00:45:30 +00002144 if (!netif_running(br->dev))
2145 goto unlock;
2146
Eric Dumazete8051682010-11-15 06:38:10 +00002147 mdb = mlock_dereference(br->mdb, br);
2148 if (mdb) {
2149 if (mdb->old) {
Herbert Xu561f1102010-02-27 19:41:50 +00002150 err = -EEXIST;
2151rollback:
2152 br->multicast_disabled = !!val;
2153 goto unlock;
2154 }
2155
Eric Dumazete8051682010-11-15 06:38:10 +00002156 err = br_mdb_rehash(&br->mdb, mdb->max,
Herbert Xu561f1102010-02-27 19:41:50 +00002157 br->hash_elasticity);
2158 if (err)
2159 goto rollback;
2160 }
2161
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02002162 br_multicast_open(br);
2163 list_for_each_entry(port, &br->port_list, list)
2164 __br_multicast_enable_port(port);
Herbert Xu561f1102010-02-27 19:41:50 +00002165
2166unlock:
Andrey Vaginef5e0d82011-11-10 05:48:03 +00002167 spin_unlock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00002168
2169 return err;
2170}
Herbert Xub1951672010-02-27 19:41:51 +00002171
Herbert Xuc5c23262012-04-13 02:37:42 +00002172int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
2173{
Linus Lüssingb00589a2013-08-01 01:06:20 +02002174 unsigned long max_delay;
2175
Herbert Xuc5c23262012-04-13 02:37:42 +00002176 val = !!val;
2177
2178 spin_lock_bh(&br->multicast_lock);
2179 if (br->multicast_querier == val)
2180 goto unlock;
2181
2182 br->multicast_querier = val;
Linus Lüssingb00589a2013-08-01 01:06:20 +02002183 if (!val)
2184 goto unlock;
2185
2186 max_delay = br->multicast_query_response_interval;
Linus Lüssingb00589a2013-08-01 01:06:20 +02002187
Linus Lüssing90010b32014-06-07 18:26:26 +02002188 if (!timer_pending(&br->ip4_other_query.timer))
2189 br->ip4_other_query.delay_time = jiffies + max_delay;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002190
Linus Lüssing90010b32014-06-07 18:26:26 +02002191 br_multicast_start_querier(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002192
2193#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02002194 if (!timer_pending(&br->ip6_other_query.timer))
2195 br->ip6_other_query.delay_time = jiffies + max_delay;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002196
Linus Lüssing90010b32014-06-07 18:26:26 +02002197 br_multicast_start_querier(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002198#endif
Herbert Xuc5c23262012-04-13 02:37:42 +00002199
2200unlock:
2201 spin_unlock_bh(&br->multicast_lock);
2202
2203 return 0;
2204}
2205
Herbert Xub1951672010-02-27 19:41:51 +00002206int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
2207{
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02002208 int err = -EINVAL;
Herbert Xub1951672010-02-27 19:41:51 +00002209 u32 old;
Eric Dumazete8051682010-11-15 06:38:10 +00002210 struct net_bridge_mdb_htable *mdb;
Herbert Xub1951672010-02-27 19:41:51 +00002211
Curt Brunefe0d6922014-01-06 11:00:32 -08002212 spin_lock_bh(&br->multicast_lock);
Herbert Xub1951672010-02-27 19:41:51 +00002213 if (!is_power_of_2(val))
2214 goto unlock;
Eric Dumazete8051682010-11-15 06:38:10 +00002215
2216 mdb = mlock_dereference(br->mdb, br);
2217 if (mdb && val < mdb->size)
Herbert Xub1951672010-02-27 19:41:51 +00002218 goto unlock;
2219
2220 err = 0;
2221
2222 old = br->hash_max;
2223 br->hash_max = val;
2224
Eric Dumazete8051682010-11-15 06:38:10 +00002225 if (mdb) {
2226 if (mdb->old) {
Herbert Xub1951672010-02-27 19:41:51 +00002227 err = -EEXIST;
2228rollback:
2229 br->hash_max = old;
2230 goto unlock;
2231 }
2232
2233 err = br_mdb_rehash(&br->mdb, br->hash_max,
2234 br->hash_elasticity);
2235 if (err)
2236 goto rollback;
2237 }
2238
2239unlock:
Curt Brunefe0d6922014-01-06 11:00:32 -08002240 spin_unlock_bh(&br->multicast_lock);
Herbert Xub1951672010-02-27 19:41:51 +00002241
2242 return err;
2243}
Linus Lüssing07f8ac42014-06-07 18:26:28 +02002244
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01002245int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val)
2246{
2247 /* Currently we support only version 2 and 3 */
2248 switch (val) {
2249 case 2:
2250 case 3:
2251 break;
2252 default:
2253 return -EINVAL;
2254 }
2255
2256 spin_lock_bh(&br->multicast_lock);
2257 br->multicast_igmp_version = val;
2258 spin_unlock_bh(&br->multicast_lock);
2259
2260 return 0;
2261}
2262
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01002263#if IS_ENABLED(CONFIG_IPV6)
2264int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val)
2265{
2266 /* Currently we support version 1 and 2 */
2267 switch (val) {
2268 case 1:
2269 case 2:
2270 break;
2271 default:
2272 return -EINVAL;
2273 }
2274
2275 spin_lock_bh(&br->multicast_lock);
2276 br->multicast_mld_version = val;
2277 spin_unlock_bh(&br->multicast_lock);
2278
2279 return 0;
2280}
2281#endif
2282
Linus Lüssing07f8ac42014-06-07 18:26:28 +02002283/**
2284 * br_multicast_list_adjacent - Returns snooped multicast addresses
2285 * @dev: The bridge port adjacent to which to retrieve addresses
2286 * @br_ip_list: The list to store found, snooped multicast IP addresses in
2287 *
2288 * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
2289 * snooping feature on all bridge ports of dev's bridge device, excluding
2290 * the addresses from dev itself.
2291 *
2292 * Returns the number of items added to br_ip_list.
2293 *
2294 * Notes:
2295 * - br_ip_list needs to be initialized by caller
2296 * - br_ip_list might contain duplicates in the end
2297 * (needs to be taken care of by caller)
2298 * - br_ip_list needs to be freed by caller
2299 */
2300int br_multicast_list_adjacent(struct net_device *dev,
2301 struct list_head *br_ip_list)
2302{
2303 struct net_bridge *br;
2304 struct net_bridge_port *port;
2305 struct net_bridge_port_group *group;
2306 struct br_ip_list *entry;
2307 int count = 0;
2308
2309 rcu_read_lock();
2310 if (!br_ip_list || !br_port_exists(dev))
2311 goto unlock;
2312
2313 port = br_port_get_rcu(dev);
2314 if (!port || !port->br)
2315 goto unlock;
2316
2317 br = port->br;
2318
2319 list_for_each_entry_rcu(port, &br->port_list, list) {
2320 if (!port->dev || port->dev == dev)
2321 continue;
2322
2323 hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
2324 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
2325 if (!entry)
2326 goto unlock;
2327
2328 entry->addr = group->addr;
2329 list_add(&entry->list, br_ip_list);
2330 count++;
2331 }
2332 }
2333
2334unlock:
2335 rcu_read_unlock();
2336 return count;
2337}
2338EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
Linus Lüssing2cd41432014-06-07 18:26:29 +02002339
2340/**
Linus Lüssingc34963e2014-07-07 05:41:17 +02002341 * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2342 * @dev: The bridge port providing the bridge on which to check for a querier
2343 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2344 *
2345 * Checks whether the given interface has a bridge on top and if so returns
2346 * true if a valid querier exists anywhere on the bridged link layer.
2347 * Otherwise returns false.
2348 */
2349bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
2350{
2351 struct net_bridge *br;
2352 struct net_bridge_port *port;
2353 struct ethhdr eth;
2354 bool ret = false;
2355
2356 rcu_read_lock();
2357 if (!br_port_exists(dev))
2358 goto unlock;
2359
2360 port = br_port_get_rcu(dev);
2361 if (!port || !port->br)
2362 goto unlock;
2363
2364 br = port->br;
2365
2366 memset(&eth, 0, sizeof(eth));
2367 eth.h_proto = htons(proto);
2368
2369 ret = br_multicast_querier_exists(br, &eth);
2370
2371unlock:
2372 rcu_read_unlock();
2373 return ret;
2374}
2375EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
2376
2377/**
Linus Lüssing2cd41432014-06-07 18:26:29 +02002378 * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
2379 * @dev: The bridge port adjacent to which to check for a querier
2380 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2381 *
2382 * Checks whether the given interface has a bridge on top and if so returns
2383 * true if a selected querier is behind one of the other ports of this
2384 * bridge. Otherwise returns false.
2385 */
2386bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
2387{
2388 struct net_bridge *br;
2389 struct net_bridge_port *port;
2390 bool ret = false;
2391
2392 rcu_read_lock();
2393 if (!br_port_exists(dev))
2394 goto unlock;
2395
2396 port = br_port_get_rcu(dev);
2397 if (!port || !port->br)
2398 goto unlock;
2399
2400 br = port->br;
2401
2402 switch (proto) {
2403 case ETH_P_IP:
2404 if (!timer_pending(&br->ip4_other_query.timer) ||
2405 rcu_dereference(br->ip4_querier.port) == port)
2406 goto unlock;
2407 break;
Linus Lüssing3993c4e2014-06-12 01:41:24 +02002408#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing2cd41432014-06-07 18:26:29 +02002409 case ETH_P_IPV6:
2410 if (!timer_pending(&br->ip6_other_query.timer) ||
2411 rcu_dereference(br->ip6_querier.port) == port)
2412 goto unlock;
2413 break;
Linus Lüssing3993c4e2014-06-12 01:41:24 +02002414#endif
Linus Lüssing2cd41432014-06-07 18:26:29 +02002415 default:
2416 goto unlock;
2417 }
2418
2419 ret = true;
2420unlock:
2421 rcu_read_unlock();
2422 return ret;
2423}
2424EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002425
2426static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002427 const struct sk_buff *skb, u8 type, u8 dir)
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002428{
2429 struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002430 __be16 proto = skb->protocol;
2431 unsigned int t_len;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002432
2433 u64_stats_update_begin(&pstats->syncp);
2434 switch (proto) {
2435 case htons(ETH_P_IP):
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002436 t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002437 switch (type) {
2438 case IGMP_HOST_MEMBERSHIP_REPORT:
2439 pstats->mstats.igmp_v1reports[dir]++;
2440 break;
2441 case IGMPV2_HOST_MEMBERSHIP_REPORT:
2442 pstats->mstats.igmp_v2reports[dir]++;
2443 break;
2444 case IGMPV3_HOST_MEMBERSHIP_REPORT:
2445 pstats->mstats.igmp_v3reports[dir]++;
2446 break;
2447 case IGMP_HOST_MEMBERSHIP_QUERY:
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002448 if (t_len != sizeof(struct igmphdr)) {
2449 pstats->mstats.igmp_v3queries[dir]++;
2450 } else {
2451 unsigned int offset = skb_transport_offset(skb);
2452 struct igmphdr *ih, _ihdr;
2453
2454 ih = skb_header_pointer(skb, offset,
2455 sizeof(_ihdr), &_ihdr);
2456 if (!ih)
2457 break;
2458 if (!ih->code)
2459 pstats->mstats.igmp_v1queries[dir]++;
2460 else
2461 pstats->mstats.igmp_v2queries[dir]++;
2462 }
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002463 break;
2464 case IGMP_HOST_LEAVE_MESSAGE:
2465 pstats->mstats.igmp_leaves[dir]++;
2466 break;
2467 }
2468 break;
2469#if IS_ENABLED(CONFIG_IPV6)
2470 case htons(ETH_P_IPV6):
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002471 t_len = ntohs(ipv6_hdr(skb)->payload_len) +
2472 sizeof(struct ipv6hdr);
2473 t_len -= skb_network_header_len(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002474 switch (type) {
2475 case ICMPV6_MGM_REPORT:
2476 pstats->mstats.mld_v1reports[dir]++;
2477 break;
2478 case ICMPV6_MLD2_REPORT:
2479 pstats->mstats.mld_v2reports[dir]++;
2480 break;
2481 case ICMPV6_MGM_QUERY:
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002482 if (t_len != sizeof(struct mld_msg))
2483 pstats->mstats.mld_v2queries[dir]++;
2484 else
2485 pstats->mstats.mld_v1queries[dir]++;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002486 break;
2487 case ICMPV6_MGM_REDUCTION:
2488 pstats->mstats.mld_leaves[dir]++;
2489 break;
2490 }
2491 break;
2492#endif /* CONFIG_IPV6 */
2493 }
2494 u64_stats_update_end(&pstats->syncp);
2495}
2496
2497void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002498 const struct sk_buff *skb, u8 type, u8 dir)
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002499{
2500 struct bridge_mcast_stats __percpu *stats;
2501
2502 /* if multicast_disabled is true then igmp type can't be set */
2503 if (!type || !br->multicast_stats_enabled)
2504 return;
2505
2506 if (p)
2507 stats = p->mcast_stats;
2508 else
2509 stats = br->mcast_stats;
2510 if (WARN_ON(!stats))
2511 return;
2512
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002513 br_mcast_stats_add(stats, skb, type, dir);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002514}
2515
2516int br_multicast_init_stats(struct net_bridge *br)
2517{
2518 br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
2519 if (!br->mcast_stats)
2520 return -ENOMEM;
2521
2522 return 0;
2523}
2524
2525static void mcast_stats_add_dir(u64 *dst, u64 *src)
2526{
2527 dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
2528 dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
2529}
2530
2531void br_multicast_get_stats(const struct net_bridge *br,
2532 const struct net_bridge_port *p,
2533 struct br_mcast_stats *dest)
2534{
2535 struct bridge_mcast_stats __percpu *stats;
2536 struct br_mcast_stats tdst;
2537 int i;
2538
2539 memset(dest, 0, sizeof(*dest));
2540 if (p)
2541 stats = p->mcast_stats;
2542 else
2543 stats = br->mcast_stats;
2544 if (WARN_ON(!stats))
2545 return;
2546
2547 memset(&tdst, 0, sizeof(tdst));
2548 for_each_possible_cpu(i) {
2549 struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
2550 struct br_mcast_stats temp;
2551 unsigned int start;
2552
2553 do {
2554 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
2555 memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
2556 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
2557
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002558 mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
2559 mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
2560 mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002561 mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
2562 mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
2563 mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
2564 mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
2565 tdst.igmp_parse_errors += temp.igmp_parse_errors;
2566
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002567 mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
2568 mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002569 mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
2570 mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
2571 mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
2572 tdst.mld_parse_errors += temp.mld_parse_errors;
2573 }
2574 memcpy(dest, &tdst, sizeof(*dest));
2575}