blob: 7c78b8df1d815c775f2d64814ca4bb6679b24b47 [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>
Herbert Xueb1d1642010-02-27 19:41:45 +000028#include <net/ip.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000029#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090030#include <net/ipv6.h>
31#include <net/mld.h>
David S. Millerd4c4f072010-04-27 10:16:54 -070032#include <net/ip6_checksum.h>
Linus Lüssing3c3769e2013-09-04 02:13:39 +020033#include <net/addrconf.h>
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090034#endif
Herbert Xueb1d1642010-02-27 19:41:45 +000035
36#include "br_private.h"
37
Linus Lüssingcc0fdd82013-08-30 17:28:17 +020038static void br_multicast_start_querier(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +020039 struct bridge_mcast_own_query *query);
Cong Wang2ce297f2012-12-10 02:15:35 +000040unsigned int br_mdb_rehash_seq;
Herbert Xuc83b8fa2012-04-13 02:37:42 +000041
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090042static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
43{
44 if (a->proto != b->proto)
45 return 0;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000046 if (a->vid != b->vid)
47 return 0;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090048 switch (a->proto) {
49 case htons(ETH_P_IP):
50 return a->u.ip4 == b->u.ip4;
Eric Dumazetdfd56b82011-12-10 09:48:31 +000051#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090052 case htons(ETH_P_IPV6):
53 return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
54#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090055 }
56 return 0;
57}
58
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000059static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip,
60 __u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +000061{
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000062 return jhash_2words((__force u32)ip, vid, mdb->secret) & (mdb->max - 1);
Herbert Xueb1d1642010-02-27 19:41:45 +000063}
64
Eric Dumazetdfd56b82011-12-10 09:48:31 +000065#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090066static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000067 const struct in6_addr *ip,
68 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090069{
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000070 return jhash_2words(ipv6_addr_hash(ip), vid,
71 mdb->secret) & (mdb->max - 1);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090072}
73#endif
74
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090075static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb,
76 struct br_ip *ip)
77{
78 switch (ip->proto) {
79 case htons(ETH_P_IP):
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000080 return __br_ip4_hash(mdb, ip->u.ip4, ip->vid);
Eric Dumazetdfd56b82011-12-10 09:48:31 +000081#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090082 case htons(ETH_P_IPV6):
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000083 return __br_ip6_hash(mdb, &ip->u.ip6, ip->vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090084#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090085 }
86 return 0;
Herbert Xueb1d1642010-02-27 19:41:45 +000087}
88
89static struct net_bridge_mdb_entry *__br_mdb_ip_get(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090090 struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +000091{
92 struct net_bridge_mdb_entry *mp;
Herbert Xueb1d1642010-02-27 19:41:45 +000093
Sasha Levinb67bfe02013-02-27 17:06:00 -080094 hlist_for_each_entry_rcu(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090095 if (br_ip_equal(&mp->addr, dst))
Herbert Xueb1d1642010-02-27 19:41:45 +000096 return mp;
97 }
98
99 return NULL;
100}
101
Cong Wangcfd56752012-12-11 22:23:08 +0000102struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge_mdb_htable *mdb,
103 struct br_ip *dst)
Herbert Xu7f285fa2010-07-05 14:50:08 +0000104{
105 if (!mdb)
106 return NULL;
107
108 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
109}
110
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900111static struct net_bridge_mdb_entry *br_mdb_ip4_get(
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000112 struct net_bridge_mdb_htable *mdb, __be32 dst, __u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000113{
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900114 struct br_ip br_dst;
Herbert Xu0821ec52010-03-15 20:38:25 -0700115
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900116 br_dst.u.ip4 = dst;
117 br_dst.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000118 br_dst.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900119
Herbert Xu7f285fa2010-07-05 14:50:08 +0000120 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900121}
122
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000123#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900124static struct net_bridge_mdb_entry *br_mdb_ip6_get(
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000125 struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst,
126 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900127{
128 struct br_ip br_dst;
129
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000130 br_dst.u.ip6 = *dst;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900131 br_dst.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000132 br_dst.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900133
Herbert Xu7f285fa2010-07-05 14:50:08 +0000134 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900135}
136#endif
137
Herbert Xueb1d1642010-02-27 19:41:45 +0000138struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
Cong Wangfbca58a2013-03-07 03:05:33 +0000139 struct sk_buff *skb, u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000140{
Eric Dumazete8051682010-11-15 06:38:10 +0000141 struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900142 struct br_ip ip;
143
Herbert Xu7f285fa2010-07-05 14:50:08 +0000144 if (br->multicast_disabled)
Herbert Xueb1d1642010-02-27 19:41:45 +0000145 return NULL;
146
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900147 if (BR_INPUT_SKB_CB(skb)->igmp)
148 return NULL;
149
150 ip.proto = skb->protocol;
Cong Wangfbca58a2013-03-07 03:05:33 +0000151 ip.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900152
Herbert Xueb1d1642010-02-27 19:41:45 +0000153 switch (skb->protocol) {
154 case htons(ETH_P_IP):
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900155 ip.u.ip4 = ip_hdr(skb)->daddr;
156 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000157#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900158 case htons(ETH_P_IPV6):
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000159 ip.u.ip6 = ipv6_hdr(skb)->daddr;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900160 break;
161#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900162 default:
163 return NULL;
Herbert Xueb1d1642010-02-27 19:41:45 +0000164 }
165
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900166 return br_mdb_ip_get(mdb, &ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000167}
168
169static void br_mdb_free(struct rcu_head *head)
170{
171 struct net_bridge_mdb_htable *mdb =
172 container_of(head, struct net_bridge_mdb_htable, rcu);
173 struct net_bridge_mdb_htable *old = mdb->old;
174
175 mdb->old = NULL;
176 kfree(old->mhash);
177 kfree(old);
178}
179
180static int br_mdb_copy(struct net_bridge_mdb_htable *new,
181 struct net_bridge_mdb_htable *old,
182 int elasticity)
183{
184 struct net_bridge_mdb_entry *mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000185 int maxlen;
186 int len;
187 int i;
188
189 for (i = 0; i < old->max; i++)
Sasha Levinb67bfe02013-02-27 17:06:00 -0800190 hlist_for_each_entry(mp, &old->mhash[i], hlist[old->ver])
Herbert Xueb1d1642010-02-27 19:41:45 +0000191 hlist_add_head(&mp->hlist[new->ver],
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900192 &new->mhash[br_ip_hash(new, &mp->addr)]);
Herbert Xueb1d1642010-02-27 19:41:45 +0000193
194 if (!elasticity)
195 return 0;
196
197 maxlen = 0;
198 for (i = 0; i < new->max; i++) {
199 len = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800200 hlist_for_each_entry(mp, &new->mhash[i], hlist[new->ver])
Herbert Xueb1d1642010-02-27 19:41:45 +0000201 len++;
202 if (len > maxlen)
203 maxlen = len;
204 }
205
206 return maxlen > elasticity ? -EINVAL : 0;
207}
208
Cong Wangcfd56752012-12-11 22:23:08 +0000209void br_multicast_free_pg(struct rcu_head *head)
Herbert Xueb1d1642010-02-27 19:41:45 +0000210{
211 struct net_bridge_port_group *p =
212 container_of(head, struct net_bridge_port_group, rcu);
213
214 kfree(p);
215}
216
217static void br_multicast_free_group(struct rcu_head *head)
218{
219 struct net_bridge_mdb_entry *mp =
220 container_of(head, struct net_bridge_mdb_entry, rcu);
221
222 kfree(mp);
223}
224
225static void br_multicast_group_expired(unsigned long data)
226{
227 struct net_bridge_mdb_entry *mp = (void *)data;
228 struct net_bridge *br = mp->br;
229 struct net_bridge_mdb_htable *mdb;
230
231 spin_lock(&br->multicast_lock);
232 if (!netif_running(br->dev) || timer_pending(&mp->timer))
233 goto out;
234
Herbert Xu8a870172011-02-12 01:05:42 -0800235 mp->mglist = false;
Herbert Xueb1d1642010-02-27 19:41:45 +0000236
237 if (mp->ports)
238 goto out;
239
Eric Dumazete8051682010-11-15 06:38:10 +0000240 mdb = mlock_dereference(br->mdb, br);
241
Herbert Xueb1d1642010-02-27 19:41:45 +0000242 hlist_del_rcu(&mp->hlist[mdb->ver]);
243 mdb->size--;
244
Herbert Xueb1d1642010-02-27 19:41:45 +0000245 call_rcu_bh(&mp->rcu, br_multicast_free_group);
246
247out:
248 spin_unlock(&br->multicast_lock);
249}
250
251static void br_multicast_del_pg(struct net_bridge *br,
252 struct net_bridge_port_group *pg)
253{
Eric Dumazete8051682010-11-15 06:38:10 +0000254 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000255 struct net_bridge_mdb_entry *mp;
256 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +0000257 struct net_bridge_port_group __rcu **pp;
258
259 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +0000260
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900261 mp = br_mdb_ip_get(mdb, &pg->addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000262 if (WARN_ON(!mp))
263 return;
264
Eric Dumazete8051682010-11-15 06:38:10 +0000265 for (pp = &mp->ports;
266 (p = mlock_dereference(*pp, br)) != NULL;
267 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000268 if (p != pg)
269 continue;
270
stephen hemminger83f6a742010-04-27 15:01:06 +0000271 rcu_assign_pointer(*pp, p->next);
Herbert Xueb1d1642010-02-27 19:41:45 +0000272 hlist_del_init(&p->mglist);
273 del_timer(&p->timer);
Herbert Xueb1d1642010-02-27 19:41:45 +0000274 call_rcu_bh(&p->rcu, br_multicast_free_pg);
275
Linus Lüssing454594f2013-10-20 00:58:57 +0200276 if (!mp->ports && !mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +0000277 netif_running(br->dev))
278 mod_timer(&mp->timer, jiffies);
279
280 return;
281 }
282
283 WARN_ON(1);
284}
285
286static void br_multicast_port_group_expired(unsigned long data)
287{
288 struct net_bridge_port_group *pg = (void *)data;
289 struct net_bridge *br = pg->port->br;
290
291 spin_lock(&br->multicast_lock);
292 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
Amerigo Wangccb1c312012-12-14 22:09:51 +0000293 hlist_unhashed(&pg->mglist) || pg->state & MDB_PERMANENT)
Herbert Xueb1d1642010-02-27 19:41:45 +0000294 goto out;
295
296 br_multicast_del_pg(br, pg);
297
298out:
299 spin_unlock(&br->multicast_lock);
300}
301
Eric Dumazete8051682010-11-15 06:38:10 +0000302static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max,
Herbert Xueb1d1642010-02-27 19:41:45 +0000303 int elasticity)
304{
Eric Dumazete8051682010-11-15 06:38:10 +0000305 struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000306 struct net_bridge_mdb_htable *mdb;
307 int err;
308
309 mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
310 if (!mdb)
311 return -ENOMEM;
312
313 mdb->max = max;
314 mdb->old = old;
315
316 mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
317 if (!mdb->mhash) {
318 kfree(mdb);
319 return -ENOMEM;
320 }
321
322 mdb->size = old ? old->size : 0;
323 mdb->ver = old ? old->ver ^ 1 : 0;
324
325 if (!old || elasticity)
326 get_random_bytes(&mdb->secret, sizeof(mdb->secret));
327 else
328 mdb->secret = old->secret;
329
330 if (!old)
331 goto out;
332
333 err = br_mdb_copy(mdb, old, elasticity);
334 if (err) {
335 kfree(mdb->mhash);
336 kfree(mdb);
337 return err;
338 }
339
Cong Wang2ce297f2012-12-10 02:15:35 +0000340 br_mdb_rehash_seq++;
Herbert Xueb1d1642010-02-27 19:41:45 +0000341 call_rcu_bh(&mdb->rcu, br_mdb_free);
342
343out:
344 rcu_assign_pointer(*mdbp, mdb);
345
346 return 0;
347}
348
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900349static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
350 __be32 group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000351{
352 struct sk_buff *skb;
353 struct igmphdr *ih;
354 struct ethhdr *eth;
355 struct iphdr *iph;
356
357 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
358 sizeof(*ih) + 4);
359 if (!skb)
360 goto out;
361
362 skb->protocol = htons(ETH_P_IP);
363
364 skb_reset_mac_header(skb);
365 eth = eth_hdr(skb);
366
Joe Perchese5a727f2014-02-23 00:05:25 -0800367 ether_addr_copy(eth->h_source, br->dev->dev_addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000368 eth->h_dest[0] = 1;
369 eth->h_dest[1] = 0;
370 eth->h_dest[2] = 0x5e;
371 eth->h_dest[3] = 0;
372 eth->h_dest[4] = 0;
373 eth->h_dest[5] = 1;
374 eth->h_proto = htons(ETH_P_IP);
375 skb_put(skb, sizeof(*eth));
376
377 skb_set_network_header(skb, skb->len);
378 iph = ip_hdr(skb);
379
380 iph->version = 4;
381 iph->ihl = 6;
382 iph->tos = 0xc0;
383 iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4);
384 iph->id = 0;
385 iph->frag_off = htons(IP_DF);
386 iph->ttl = 1;
387 iph->protocol = IPPROTO_IGMP;
Cong Wang1c8ad5b2013-05-21 21:52:54 +0000388 iph->saddr = br->multicast_query_use_ifaddr ?
389 inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000390 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
391 ((u8 *)&iph[1])[0] = IPOPT_RA;
392 ((u8 *)&iph[1])[1] = 4;
393 ((u8 *)&iph[1])[2] = 0;
394 ((u8 *)&iph[1])[3] = 0;
395 ip_send_check(iph);
396 skb_put(skb, 24);
397
398 skb_set_transport_header(skb, skb->len);
399 ih = igmp_hdr(skb);
400 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
401 ih->code = (group ? br->multicast_last_member_interval :
402 br->multicast_query_response_interval) /
403 (HZ / IGMP_TIMER_SCALE);
404 ih->group = group;
405 ih->csum = 0;
406 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
407 skb_put(skb, sizeof(*ih));
408
409 __skb_pull(skb, sizeof(*eth));
410
411out:
412 return skb;
413}
414
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000415#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900416static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000417 const struct in6_addr *group)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900418{
419 struct sk_buff *skb;
420 struct ipv6hdr *ip6h;
421 struct mld_msg *mldq;
422 struct ethhdr *eth;
423 u8 *hopopt;
424 unsigned long interval;
425
426 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
427 8 + sizeof(*mldq));
428 if (!skb)
429 goto out;
430
431 skb->protocol = htons(ETH_P_IPV6);
432
433 /* Ethernet header */
434 skb_reset_mac_header(skb);
435 eth = eth_hdr(skb);
436
Joe Perchese5a727f2014-02-23 00:05:25 -0800437 ether_addr_copy(eth->h_source, br->dev->dev_addr);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900438 eth->h_proto = htons(ETH_P_IPV6);
439 skb_put(skb, sizeof(*eth));
440
441 /* IPv6 header + HbH option */
442 skb_set_network_header(skb, skb->len);
443 ip6h = ipv6_hdr(skb);
444
445 *(__force __be32 *)ip6h = htonl(0x60000000);
David Stevens76d6615862010-12-14 08:42:16 +0000446 ip6h->payload_len = htons(8 + sizeof(*mldq));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900447 ip6h->nexthdr = IPPROTO_HOPOPTS;
448 ip6h->hop_limit = 1;
Linus Lüssinga7bff752011-03-22 11:40:32 +0000449 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
Ulrich Weberd1d81d42012-03-05 04:52:44 +0000450 if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
451 &ip6h->saddr)) {
452 kfree_skb(skb);
453 return NULL;
454 }
Linus Lüssing36cff5a2011-02-17 08:17:51 +0000455 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900456
457 hopopt = (u8 *)(ip6h + 1);
458 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
459 hopopt[1] = 0; /* length of HbH */
460 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
461 hopopt[3] = 2; /* Length of RA Option */
462 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
463 hopopt[5] = 0;
Eldad Zack1de5a712012-05-17 06:00:25 +0000464 hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */
465 hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900466
467 skb_put(skb, sizeof(*ip6h) + 8);
468
469 /* ICMPv6 */
470 skb_set_transport_header(skb, skb->len);
471 mldq = (struct mld_msg *) icmp6_hdr(skb);
472
Linus Lüssing32de8682013-06-16 23:20:34 +0200473 interval = ipv6_addr_any(group) ?
474 br->multicast_query_response_interval :
475 br->multicast_last_member_interval;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900476
477 mldq->mld_type = ICMPV6_MGM_QUERY;
478 mldq->mld_code = 0;
479 mldq->mld_cksum = 0;
480 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
481 mldq->mld_reserved = 0;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000482 mldq->mld_mca = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900483
484 /* checksum */
485 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
486 sizeof(*mldq), IPPROTO_ICMPV6,
487 csum_partial(mldq,
488 sizeof(*mldq), 0));
489 skb_put(skb, sizeof(*mldq));
490
491 __skb_pull(skb, sizeof(*eth));
492
493out:
494 return skb;
495}
496#endif
497
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900498static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
499 struct br_ip *addr)
500{
501 switch (addr->proto) {
502 case htons(ETH_P_IP):
503 return br_ip4_multicast_alloc_query(br, addr->u.ip4);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000504#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900505 case htons(ETH_P_IPV6):
506 return br_ip6_multicast_alloc_query(br, &addr->u.ip6);
507#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900508 }
509 return NULL;
510}
511
Herbert Xueb1d1642010-02-27 19:41:45 +0000512static struct net_bridge_mdb_entry *br_multicast_get_group(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900513 struct net_bridge *br, struct net_bridge_port *port,
514 struct br_ip *group, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +0000515{
Eric Dumazete8051682010-11-15 06:38:10 +0000516 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000517 struct net_bridge_mdb_entry *mp;
Eric Dumazet95c96172012-04-15 05:58:06 +0000518 unsigned int count = 0;
519 unsigned int max;
Herbert Xueb1d1642010-02-27 19:41:45 +0000520 int elasticity;
521 int err;
522
Eric Dumazete8051682010-11-15 06:38:10 +0000523 mdb = rcu_dereference_protected(br->mdb, 1);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800524 hlist_for_each_entry(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000525 count++;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900526 if (unlikely(br_ip_equal(group, &mp->addr)))
Herbert Xueb1d1642010-02-27 19:41:45 +0000527 return mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000528 }
529
530 elasticity = 0;
531 max = mdb->max;
532
533 if (unlikely(count > br->hash_elasticity && count)) {
534 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000535 br_info(br, "Multicast hash table "
536 "chain limit reached: %s\n",
537 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000538
539 elasticity = br->hash_elasticity;
540 }
541
542 if (mdb->size >= max) {
543 max *= 2;
Thomas Graf036be6d2012-07-10 22:29:19 +0000544 if (unlikely(max > br->hash_max)) {
545 br_warn(br, "Multicast hash table maximum of %d "
546 "reached, disabling snooping: %s\n",
547 br->hash_max,
548 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000549 err = -E2BIG;
550disable:
551 br->multicast_disabled = 1;
552 goto err;
553 }
554 }
555
556 if (max > mdb->max || elasticity) {
557 if (mdb->old) {
558 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000559 br_info(br, "Multicast hash table "
560 "on fire: %s\n",
561 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000562 err = -EEXIST;
563 goto err;
564 }
565
566 err = br_mdb_rehash(&br->mdb, max, elasticity);
567 if (err) {
stephen hemminger28a16c92010-05-10 09:31:09 +0000568 br_warn(br, "Cannot rehash multicast "
569 "hash table, disabling snooping: %s, %d, %d\n",
570 port ? port->dev->name : br->dev->name,
571 mdb->size, err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000572 goto disable;
573 }
574
575 err = -EAGAIN;
576 goto err;
577 }
578
579 return NULL;
580
581err:
582 mp = ERR_PTR(err);
583 return mp;
584}
585
Cong Wangcfd56752012-12-11 22:23:08 +0000586struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
587 struct net_bridge_port *port, struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000588{
Eric Dumazete8051682010-11-15 06:38:10 +0000589 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000590 struct net_bridge_mdb_entry *mp;
591 int hash;
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000592 int err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000593
Eric Dumazete8051682010-11-15 06:38:10 +0000594 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000595 if (!mdb) {
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000596 err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0);
597 if (err)
598 return ERR_PTR(err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000599 goto rehash;
600 }
601
602 hash = br_ip_hash(mdb, group);
603 mp = br_multicast_get_group(br, port, group, hash);
604 switch (PTR_ERR(mp)) {
605 case 0:
606 break;
607
608 case -EAGAIN:
609rehash:
Eric Dumazete8051682010-11-15 06:38:10 +0000610 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000611 hash = br_ip_hash(mdb, group);
612 break;
613
614 default:
615 goto out;
616 }
617
618 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
619 if (unlikely(!mp))
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000620 return ERR_PTR(-ENOMEM);
Herbert Xueb1d1642010-02-27 19:41:45 +0000621
622 mp->br = br;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900623 mp->addr = *group;
Eric Dumazet1faabf22013-07-19 20:07:16 -0700624 setup_timer(&mp->timer, br_multicast_group_expired,
625 (unsigned long)mp);
626
Herbert Xueb1d1642010-02-27 19:41:45 +0000627 hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
628 mdb->size++;
629
630out:
631 return mp;
632}
633
Cong Wangcfd56752012-12-11 22:23:08 +0000634struct net_bridge_port_group *br_multicast_new_port_group(
635 struct net_bridge_port *port,
636 struct br_ip *group,
Amerigo Wangccb1c312012-12-14 22:09:51 +0000637 struct net_bridge_port_group __rcu *next,
638 unsigned char state)
Cong Wangcfd56752012-12-11 22:23:08 +0000639{
640 struct net_bridge_port_group *p;
641
642 p = kzalloc(sizeof(*p), GFP_ATOMIC);
643 if (unlikely(!p))
644 return NULL;
645
646 p->addr = *group;
647 p->port = port;
Amerigo Wangccb1c312012-12-14 22:09:51 +0000648 p->state = state;
stephen hemmingereca2a432012-12-13 06:51:28 +0000649 rcu_assign_pointer(p->next, next);
Cong Wangcfd56752012-12-11 22:23:08 +0000650 hlist_add_head(&p->mglist, &port->mglist);
651 setup_timer(&p->timer, br_multicast_port_group_expired,
652 (unsigned long)p);
653 return p;
654}
655
Herbert Xueb1d1642010-02-27 19:41:45 +0000656static int br_multicast_add_group(struct net_bridge *br,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900657 struct net_bridge_port *port,
658 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000659{
660 struct net_bridge_mdb_entry *mp;
661 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +0000662 struct net_bridge_port_group __rcu **pp;
Linus Lüssing454594f2013-10-20 00:58:57 +0200663 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +0000664 int err;
665
Herbert Xueb1d1642010-02-27 19:41:45 +0000666 spin_lock(&br->multicast_lock);
667 if (!netif_running(br->dev) ||
668 (port && port->state == BR_STATE_DISABLED))
669 goto out;
670
671 mp = br_multicast_new_group(br, port, group);
672 err = PTR_ERR(mp);
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000673 if (IS_ERR(mp))
Herbert Xueb1d1642010-02-27 19:41:45 +0000674 goto err;
675
676 if (!port) {
Herbert Xu8a870172011-02-12 01:05:42 -0800677 mp->mglist = true;
Linus Lüssing454594f2013-10-20 00:58:57 +0200678 mod_timer(&mp->timer, now + br->multicast_membership_interval);
Herbert Xueb1d1642010-02-27 19:41:45 +0000679 goto out;
680 }
681
Eric Dumazete8051682010-11-15 06:38:10 +0000682 for (pp = &mp->ports;
683 (p = mlock_dereference(*pp, br)) != NULL;
684 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000685 if (p->port == port)
Linus Lüssing454594f2013-10-20 00:58:57 +0200686 goto found;
Herbert Xueb1d1642010-02-27 19:41:45 +0000687 if ((unsigned long)p->port < (unsigned long)port)
688 break;
689 }
690
Amerigo Wangccb1c312012-12-14 22:09:51 +0000691 p = br_multicast_new_port_group(port, group, *pp, MDB_TEMPORARY);
Herbert Xueb1d1642010-02-27 19:41:45 +0000692 if (unlikely(!p))
693 goto err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000694 rcu_assign_pointer(*pp, p);
Cong Wang37a393b2012-12-11 22:23:07 +0000695 br_mdb_notify(br->dev, port, group, RTM_NEWMDB);
Herbert Xueb1d1642010-02-27 19:41:45 +0000696
Linus Lüssing454594f2013-10-20 00:58:57 +0200697found:
698 mod_timer(&p->timer, now + br->multicast_membership_interval);
Herbert Xueb1d1642010-02-27 19:41:45 +0000699out:
700 err = 0;
701
702err:
703 spin_unlock(&br->multicast_lock);
704 return err;
705}
706
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900707static int br_ip4_multicast_add_group(struct net_bridge *br,
708 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000709 __be32 group,
710 __u16 vid)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900711{
712 struct br_ip br_group;
713
714 if (ipv4_is_local_multicast(group))
715 return 0;
716
717 br_group.u.ip4 = group;
718 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000719 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900720
721 return br_multicast_add_group(br, port, &br_group);
722}
723
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000724#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900725static int br_ip6_multicast_add_group(struct net_bridge *br,
726 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000727 const struct in6_addr *group,
728 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900729{
730 struct br_ip br_group;
731
Linus Lüssing3c3769e2013-09-04 02:13:39 +0200732 if (ipv6_addr_is_ll_all_nodes(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900733 return 0;
734
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000735 br_group.u.ip6 = *group;
Linus Lüssing9cc6e0c2011-02-15 13:19:17 +0000736 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000737 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900738
739 return br_multicast_add_group(br, port, &br_group);
740}
741#endif
742
Herbert Xueb1d1642010-02-27 19:41:45 +0000743static void br_multicast_router_expired(unsigned long data)
744{
745 struct net_bridge_port *port = (void *)data;
746 struct net_bridge *br = port->br;
747
748 spin_lock(&br->multicast_lock);
749 if (port->multicast_router != 1 ||
750 timer_pending(&port->multicast_router_timer) ||
751 hlist_unhashed(&port->rlist))
752 goto out;
753
754 hlist_del_init_rcu(&port->rlist);
755
756out:
757 spin_unlock(&br->multicast_lock);
758}
759
760static void br_multicast_local_router_expired(unsigned long data)
761{
762}
763
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200764static void br_multicast_querier_expired(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +0200765 struct bridge_mcast_own_query *query)
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000766{
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000767 spin_lock(&br->multicast_lock);
768 if (!netif_running(br->dev) || br->multicast_disabled)
769 goto out;
770
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200771 br_multicast_start_querier(br, query);
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000772
773out:
774 spin_unlock(&br->multicast_lock);
775}
776
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200777static void br_ip4_multicast_querier_expired(unsigned long data)
778{
779 struct net_bridge *br = (void *)data;
780
Linus Lüssing90010b32014-06-07 18:26:26 +0200781 br_multicast_querier_expired(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200782}
783
784#if IS_ENABLED(CONFIG_IPV6)
785static void br_ip6_multicast_querier_expired(unsigned long data)
786{
787 struct net_bridge *br = (void *)data;
788
Linus Lüssing90010b32014-06-07 18:26:26 +0200789 br_multicast_querier_expired(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200790}
791#endif
792
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200793static void br_multicast_select_own_querier(struct net_bridge *br,
794 struct br_ip *ip,
795 struct sk_buff *skb)
796{
797 if (ip->proto == htons(ETH_P_IP))
798 br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr;
799#if IS_ENABLED(CONFIG_IPV6)
800 else
801 br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr;
802#endif
803}
804
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900805static void __br_multicast_send_query(struct net_bridge *br,
806 struct net_bridge_port *port,
807 struct br_ip *ip)
Herbert Xueb1d1642010-02-27 19:41:45 +0000808{
Herbert Xueb1d1642010-02-27 19:41:45 +0000809 struct sk_buff *skb;
810
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900811 skb = br_multicast_alloc_query(br, ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000812 if (!skb)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900813 return;
Herbert Xueb1d1642010-02-27 19:41:45 +0000814
815 if (port) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000816 skb->dev = port->dev;
David Miller7026b1d2015-04-05 22:19:04 -0400817 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, NULL, skb,
818 NULL, skb->dev,
Linus Lüssingf0b4eec2014-11-17 12:20:28 +0100819 br_dev_queue_push_xmit);
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200820 } else {
821 br_multicast_select_own_querier(br, ip, skb);
Herbert Xueb1d1642010-02-27 19:41:45 +0000822 netif_rx(skb);
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200823 }
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900824}
Herbert Xueb1d1642010-02-27 19:41:45 +0000825
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900826static void br_multicast_send_query(struct net_bridge *br,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200827 struct net_bridge_port *port,
Linus Lüssing90010b32014-06-07 18:26:26 +0200828 struct bridge_mcast_own_query *own_query)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900829{
830 unsigned long time;
831 struct br_ip br_group;
Linus Lüssing90010b32014-06-07 18:26:26 +0200832 struct bridge_mcast_other_query *other_query = NULL;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900833
834 if (!netif_running(br->dev) || br->multicast_disabled ||
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200835 !br->multicast_querier)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900836 return;
837
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900838 memset(&br_group.u, 0, sizeof(br_group.u));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900839
Linus Lüssing90010b32014-06-07 18:26:26 +0200840 if (port ? (own_query == &port->ip4_own_query) :
841 (own_query == &br->ip4_own_query)) {
842 other_query = &br->ip4_other_query;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200843 br_group.proto = htons(ETH_P_IP);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000844#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200845 } else {
Linus Lüssing90010b32014-06-07 18:26:26 +0200846 other_query = &br->ip6_other_query;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200847 br_group.proto = htons(ETH_P_IPV6);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900848#endif
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200849 }
850
Linus Lüssing90010b32014-06-07 18:26:26 +0200851 if (!other_query || timer_pending(&other_query->timer))
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200852 return;
853
854 __br_multicast_send_query(br, port, &br_group);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900855
Herbert Xueb1d1642010-02-27 19:41:45 +0000856 time = jiffies;
Linus Lüssing90010b32014-06-07 18:26:26 +0200857 time += own_query->startup_sent < br->multicast_startup_query_count ?
Herbert Xueb1d1642010-02-27 19:41:45 +0000858 br->multicast_startup_query_interval :
859 br->multicast_query_interval;
Linus Lüssing90010b32014-06-07 18:26:26 +0200860 mod_timer(&own_query->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +0000861}
862
Linus Lüssing90010b32014-06-07 18:26:26 +0200863static void
864br_multicast_port_query_expired(struct net_bridge_port *port,
865 struct bridge_mcast_own_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +0000866{
Herbert Xueb1d1642010-02-27 19:41:45 +0000867 struct net_bridge *br = port->br;
868
869 spin_lock(&br->multicast_lock);
Dan Carpenter02a780c2010-03-06 01:14:09 +0000870 if (port->state == BR_STATE_DISABLED ||
871 port->state == BR_STATE_BLOCKING)
Herbert Xueb1d1642010-02-27 19:41:45 +0000872 goto out;
873
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200874 if (query->startup_sent < br->multicast_startup_query_count)
875 query->startup_sent++;
Herbert Xueb1d1642010-02-27 19:41:45 +0000876
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200877 br_multicast_send_query(port->br, port, query);
Herbert Xueb1d1642010-02-27 19:41:45 +0000878
879out:
880 spin_unlock(&br->multicast_lock);
881}
882
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200883static void br_ip4_multicast_port_query_expired(unsigned long data)
884{
885 struct net_bridge_port *port = (void *)data;
886
Linus Lüssing90010b32014-06-07 18:26:26 +0200887 br_multicast_port_query_expired(port, &port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200888}
889
890#if IS_ENABLED(CONFIG_IPV6)
891static void br_ip6_multicast_port_query_expired(unsigned long data)
892{
893 struct net_bridge_port *port = (void *)data;
894
Linus Lüssing90010b32014-06-07 18:26:26 +0200895 br_multicast_port_query_expired(port, &port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200896}
897#endif
898
Herbert Xueb1d1642010-02-27 19:41:45 +0000899void br_multicast_add_port(struct net_bridge_port *port)
900{
901 port->multicast_router = 1;
902
903 setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
904 (unsigned long)port);
Linus Lüssing90010b32014-06-07 18:26:26 +0200905 setup_timer(&port->ip4_own_query.timer,
906 br_ip4_multicast_port_query_expired, (unsigned long)port);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200907#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +0200908 setup_timer(&port->ip6_own_query.timer,
909 br_ip6_multicast_port_query_expired, (unsigned long)port);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200910#endif
Herbert Xueb1d1642010-02-27 19:41:45 +0000911}
912
913void br_multicast_del_port(struct net_bridge_port *port)
914{
915 del_timer_sync(&port->multicast_router_timer);
916}
917
Linus Lüssing90010b32014-06-07 18:26:26 +0200918static void br_multicast_enable(struct bridge_mcast_own_query *query)
Herbert Xu561f1102010-02-27 19:41:50 +0000919{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200920 query->startup_sent = 0;
Herbert Xu561f1102010-02-27 19:41:50 +0000921
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200922 if (try_to_del_timer_sync(&query->timer) >= 0 ||
923 del_timer(&query->timer))
924 mod_timer(&query->timer, jiffies);
Herbert Xu561f1102010-02-27 19:41:50 +0000925}
926
Herbert Xueb1d1642010-02-27 19:41:45 +0000927void br_multicast_enable_port(struct net_bridge_port *port)
928{
929 struct net_bridge *br = port->br;
930
931 spin_lock(&br->multicast_lock);
932 if (br->multicast_disabled || !netif_running(br->dev))
933 goto out;
934
Linus Lüssing90010b32014-06-07 18:26:26 +0200935 br_multicast_enable(&port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200936#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +0200937 br_multicast_enable(&port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200938#endif
Herbert Xueb1d1642010-02-27 19:41:45 +0000939
940out:
941 spin_unlock(&br->multicast_lock);
942}
943
944void br_multicast_disable_port(struct net_bridge_port *port)
945{
946 struct net_bridge *br = port->br;
947 struct net_bridge_port_group *pg;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800948 struct hlist_node *n;
Herbert Xueb1d1642010-02-27 19:41:45 +0000949
950 spin_lock(&br->multicast_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800951 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
Herbert Xueb1d1642010-02-27 19:41:45 +0000952 br_multicast_del_pg(br, pg);
953
954 if (!hlist_unhashed(&port->rlist))
955 hlist_del_init_rcu(&port->rlist);
956 del_timer(&port->multicast_router_timer);
Linus Lüssing90010b32014-06-07 18:26:26 +0200957 del_timer(&port->ip4_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200958#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +0200959 del_timer(&port->ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200960#endif
Herbert Xueb1d1642010-02-27 19:41:45 +0000961 spin_unlock(&br->multicast_lock);
962}
963
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900964static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
965 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -0400966 struct sk_buff *skb,
967 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000968{
969 struct igmpv3_report *ih;
970 struct igmpv3_grec *grec;
971 int i;
972 int len;
973 int num;
974 int type;
975 int err = 0;
976 __be32 group;
977
Herbert Xueb1d1642010-02-27 19:41:45 +0000978 ih = igmpv3_report_hdr(skb);
979 num = ntohs(ih->ngrec);
980 len = sizeof(*ih);
981
982 for (i = 0; i < num; i++) {
983 len += sizeof(*grec);
984 if (!pskb_may_pull(skb, len))
985 return -EINVAL;
986
Herbert Xufd218cf2010-04-07 21:20:47 -0700987 grec = (void *)(skb->data + len - sizeof(*grec));
Herbert Xueb1d1642010-02-27 19:41:45 +0000988 group = grec->grec_mca;
989 type = grec->grec_type;
990
Eric Dumazet8eabf952010-04-20 03:20:05 +0000991 len += ntohs(grec->grec_nsrcs) * 4;
Herbert Xueb1d1642010-02-27 19:41:45 +0000992 if (!pskb_may_pull(skb, len))
993 return -EINVAL;
994
995 /* We treat this as an IGMPv2 report for now. */
996 switch (type) {
997 case IGMPV3_MODE_IS_INCLUDE:
998 case IGMPV3_MODE_IS_EXCLUDE:
999 case IGMPV3_CHANGE_TO_INCLUDE:
1000 case IGMPV3_CHANGE_TO_EXCLUDE:
1001 case IGMPV3_ALLOW_NEW_SOURCES:
1002 case IGMPV3_BLOCK_OLD_SOURCES:
1003 break;
1004
1005 default:
1006 continue;
1007 }
1008
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001009 err = br_ip4_multicast_add_group(br, port, group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001010 if (err)
1011 break;
1012 }
1013
1014 return err;
1015}
1016
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001017#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001018static int br_ip6_multicast_mld2_report(struct net_bridge *br,
1019 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001020 struct sk_buff *skb,
1021 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001022{
1023 struct icmp6hdr *icmp6h;
1024 struct mld2_grec *grec;
1025 int i;
1026 int len;
1027 int num;
1028 int err = 0;
1029
1030 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
1031 return -EINVAL;
1032
1033 icmp6h = icmp6_hdr(skb);
1034 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
1035 len = sizeof(*icmp6h);
1036
1037 for (i = 0; i < num; i++) {
1038 __be16 *nsrcs, _nsrcs;
1039
1040 nsrcs = skb_header_pointer(skb,
1041 len + offsetof(struct mld2_grec,
Linus Lüssing649e9842011-02-15 13:19:18 +00001042 grec_nsrcs),
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001043 sizeof(_nsrcs), &_nsrcs);
1044 if (!nsrcs)
1045 return -EINVAL;
1046
1047 if (!pskb_may_pull(skb,
1048 len + sizeof(*grec) +
Linus Lüssingd41db9f2011-02-15 13:19:19 +00001049 sizeof(struct in6_addr) * ntohs(*nsrcs)))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001050 return -EINVAL;
1051
1052 grec = (struct mld2_grec *)(skb->data + len);
Linus Lüssingd41db9f2011-02-15 13:19:19 +00001053 len += sizeof(*grec) +
1054 sizeof(struct in6_addr) * ntohs(*nsrcs);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001055
1056 /* We treat these as MLDv1 reports for now. */
1057 switch (grec->grec_type) {
1058 case MLD2_MODE_IS_INCLUDE:
1059 case MLD2_MODE_IS_EXCLUDE:
1060 case MLD2_CHANGE_TO_INCLUDE:
1061 case MLD2_CHANGE_TO_EXCLUDE:
1062 case MLD2_ALLOW_NEW_SOURCES:
1063 case MLD2_BLOCK_OLD_SOURCES:
1064 break;
1065
1066 default:
1067 continue;
1068 }
1069
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001070 err = br_ip6_multicast_add_group(br, port, &grec->grec_mca,
1071 vid);
Thadeu Lima de Souza Cascardo47cc84c2015-05-22 12:18:59 -03001072 if (err)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001073 break;
1074 }
1075
1076 return err;
1077}
1078#endif
1079
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001080static bool br_ip4_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001081 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001082 __be32 saddr)
1083{
1084 if (!timer_pending(&br->ip4_own_query.timer) &&
1085 !timer_pending(&br->ip4_other_query.timer))
1086 goto update;
1087
1088 if (!br->ip4_querier.addr.u.ip4)
1089 goto update;
1090
1091 if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4))
1092 goto update;
1093
1094 return false;
1095
1096update:
1097 br->ip4_querier.addr.u.ip4 = saddr;
1098
Linus Lüssing2cd41432014-06-07 18:26:29 +02001099 /* update protected by general multicast_lock by caller */
1100 rcu_assign_pointer(br->ip4_querier.port, port);
1101
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001102 return true;
1103}
1104
1105#if IS_ENABLED(CONFIG_IPV6)
1106static bool br_ip6_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001107 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001108 struct in6_addr *saddr)
1109{
1110 if (!timer_pending(&br->ip6_own_query.timer) &&
1111 !timer_pending(&br->ip6_other_query.timer))
1112 goto update;
1113
1114 if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0)
1115 goto update;
1116
1117 return false;
1118
1119update:
1120 br->ip6_querier.addr.u.ip6 = *saddr;
1121
Linus Lüssing2cd41432014-06-07 18:26:29 +02001122 /* update protected by general multicast_lock by caller */
1123 rcu_assign_pointer(br->ip6_querier.port, port);
1124
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001125 return true;
1126}
1127#endif
1128
1129static bool br_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001130 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001131 struct br_ip *saddr)
1132{
1133 switch (saddr->proto) {
1134 case htons(ETH_P_IP):
Linus Lüssing2cd41432014-06-07 18:26:29 +02001135 return br_ip4_multicast_select_querier(br, port, saddr->u.ip4);
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001136#if IS_ENABLED(CONFIG_IPV6)
1137 case htons(ETH_P_IPV6):
Linus Lüssing2cd41432014-06-07 18:26:29 +02001138 return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6);
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001139#endif
1140 }
1141
1142 return false;
1143}
1144
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001145static void
Linus Lüssing90010b32014-06-07 18:26:26 +02001146br_multicast_update_query_timer(struct net_bridge *br,
1147 struct bridge_mcast_other_query *query,
1148 unsigned long max_delay)
Linus Lüssingb00589a2013-08-01 01:06:20 +02001149{
Linus Lüssing90010b32014-06-07 18:26:26 +02001150 if (!timer_pending(&query->timer))
1151 query->delay_time = jiffies + max_delay;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001152
Linus Lüssing90010b32014-06-07 18:26:26 +02001153 mod_timer(&query->timer, jiffies + br->multicast_querier_interval);
Linus Lüssingb00589a2013-08-01 01:06:20 +02001154}
1155
stephen hemminger7e80c122010-04-27 15:01:04 +00001156/*
Cong Wang7c776022013-06-21 15:37:25 +08001157 * Add port to router_list
stephen hemminger7e80c122010-04-27 15:01:04 +00001158 * list is maintained ordered by pointer value
1159 * and locked by br->multicast_lock and RCU
1160 */
Herbert Xu0909e112010-02-27 19:41:49 +00001161static void br_multicast_add_router(struct net_bridge *br,
1162 struct net_bridge_port *port)
1163{
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001164 struct net_bridge_port *p;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001165 struct hlist_node *slot = NULL;
Herbert Xu0909e112010-02-27 19:41:49 +00001166
Sasha Levinb67bfe02013-02-27 17:06:00 -08001167 hlist_for_each_entry(p, &br->router_list, rlist) {
stephen hemminger7e80c122010-04-27 15:01:04 +00001168 if ((unsigned long) port >= (unsigned long) p)
1169 break;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001170 slot = &p->rlist;
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001171 }
Herbert Xu0909e112010-02-27 19:41:49 +00001172
stephen hemminger7e80c122010-04-27 15:01:04 +00001173 if (slot)
Ken Helias1d023282014-08-06 16:09:16 -07001174 hlist_add_behind_rcu(&port->rlist, slot);
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001175 else
1176 hlist_add_head_rcu(&port->rlist, &br->router_list);
Herbert Xu0909e112010-02-27 19:41:49 +00001177}
1178
Herbert Xueb1d1642010-02-27 19:41:45 +00001179static void br_multicast_mark_router(struct net_bridge *br,
1180 struct net_bridge_port *port)
1181{
1182 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +00001183
1184 if (!port) {
1185 if (br->multicast_router == 1)
1186 mod_timer(&br->multicast_router_timer,
1187 now + br->multicast_querier_interval);
1188 return;
1189 }
1190
1191 if (port->multicast_router != 1)
1192 return;
1193
1194 if (!hlist_unhashed(&port->rlist))
1195 goto timer;
1196
Herbert Xu0909e112010-02-27 19:41:49 +00001197 br_multicast_add_router(br, port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001198
1199timer:
1200 mod_timer(&port->multicast_router_timer,
1201 now + br->multicast_querier_interval);
1202}
1203
1204static void br_multicast_query_received(struct net_bridge *br,
1205 struct net_bridge_port *port,
Linus Lüssing90010b32014-06-07 18:26:26 +02001206 struct bridge_mcast_other_query *query,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001207 struct br_ip *saddr,
Linus Lüssingb00589a2013-08-01 01:06:20 +02001208 unsigned long max_delay)
Herbert Xueb1d1642010-02-27 19:41:45 +00001209{
Linus Lüssing2cd41432014-06-07 18:26:29 +02001210 if (!br_multicast_select_querier(br, port, saddr))
Herbert Xueb1d1642010-02-27 19:41:45 +00001211 return;
1212
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001213 br_multicast_update_query_timer(br, query, max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001214 br_multicast_mark_router(br, port);
1215}
1216
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001217static int br_ip4_multicast_query(struct net_bridge *br,
1218 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001219 struct sk_buff *skb,
1220 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001221{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001222 const struct iphdr *iph = ip_hdr(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001223 struct igmphdr *ih = igmp_hdr(skb);
1224 struct net_bridge_mdb_entry *mp;
1225 struct igmpv3_query *ih3;
1226 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +00001227 struct net_bridge_port_group __rcu **pp;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001228 struct br_ip saddr;
Herbert Xueb1d1642010-02-27 19:41:45 +00001229 unsigned long max_delay;
1230 unsigned long now = jiffies;
1231 __be32 group;
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001232 int err = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001233
1234 spin_lock(&br->multicast_lock);
1235 if (!netif_running(br->dev) ||
1236 (port && port->state == BR_STATE_DISABLED))
1237 goto out;
1238
Herbert Xueb1d1642010-02-27 19:41:45 +00001239 group = ih->group;
1240
1241 if (skb->len == sizeof(*ih)) {
1242 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1243
1244 if (!max_delay) {
1245 max_delay = 10 * HZ;
1246 group = 0;
1247 }
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001248 } else if (skb->len >= sizeof(*ih3)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001249 ih3 = igmpv3_query_hdr(skb);
1250 if (ih3->nsrcs)
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001251 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001252
YOSHIFUJI Hideaki / 吉藤英明0ba8c9e2010-03-15 19:27:00 +00001253 max_delay = ih3->code ?
1254 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001255 } else {
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001256 goto out;
1257 }
1258
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001259 if (!group) {
1260 saddr.proto = htons(ETH_P_IP);
1261 saddr.u.ip4 = iph->saddr;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001262
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001263 br_multicast_query_received(br, port, &br->ip4_other_query,
1264 &saddr, max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001265 goto out;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001266 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001267
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001268 mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001269 if (!mp)
1270 goto out;
1271
1272 max_delay *= br->multicast_last_member_count;
1273
Herbert Xu8a870172011-02-12 01:05:42 -08001274 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001275 (timer_pending(&mp->timer) ?
1276 time_after(mp->timer.expires, now + max_delay) :
1277 try_to_del_timer_sync(&mp->timer) >= 0))
1278 mod_timer(&mp->timer, now + max_delay);
1279
Eric Dumazete8051682010-11-15 06:38:10 +00001280 for (pp = &mp->ports;
1281 (p = mlock_dereference(*pp, br)) != NULL;
1282 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001283 if (timer_pending(&p->timer) ?
1284 time_after(p->timer.expires, now + max_delay) :
1285 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001286 mod_timer(&p->timer, now + max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001287 }
1288
1289out:
1290 spin_unlock(&br->multicast_lock);
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001291 return err;
Herbert Xueb1d1642010-02-27 19:41:45 +00001292}
1293
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001294#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001295static int br_ip6_multicast_query(struct net_bridge *br,
1296 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001297 struct sk_buff *skb,
1298 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001299{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001300 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
stephen hemmingereca2a432012-12-13 06:51:28 +00001301 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001302 struct net_bridge_mdb_entry *mp;
1303 struct mld2_query *mld2q;
Eric Dumazete8051682010-11-15 06:38:10 +00001304 struct net_bridge_port_group *p;
1305 struct net_bridge_port_group __rcu **pp;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001306 struct br_ip saddr;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001307 unsigned long max_delay;
1308 unsigned long now = jiffies;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001309 const struct in6_addr *group = NULL;
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001310 bool is_general_query;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001311 int err = 0;
1312
1313 spin_lock(&br->multicast_lock);
1314 if (!netif_running(br->dev) ||
1315 (port && port->state == BR_STATE_DISABLED))
1316 goto out;
1317
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001318 if (skb->len == sizeof(*mld)) {
1319 if (!pskb_may_pull(skb, sizeof(*mld))) {
1320 err = -EINVAL;
1321 goto out;
1322 }
1323 mld = (struct mld_msg *) icmp6_hdr(skb);
Li RongQing47152132012-07-09 23:56:12 +00001324 max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001325 if (max_delay)
1326 group = &mld->mld_mca;
Linus Lüssing248ba8e2013-08-06 00:32:05 +02001327 } else {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001328 if (!pskb_may_pull(skb, sizeof(*mld2q))) {
1329 err = -EINVAL;
1330 goto out;
1331 }
1332 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1333 if (!mld2q->mld2q_nsrcs)
1334 group = &mld2q->mld2q_mca;
Daniel Borkmanne3f5b172013-09-04 00:19:39 +02001335
1336 max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001337 }
1338
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001339 is_general_query = group && ipv6_addr_any(group);
1340
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001341 if (is_general_query) {
1342 saddr.proto = htons(ETH_P_IPV6);
1343 saddr.u.ip6 = ip6h->saddr;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001344
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001345 br_multicast_query_received(br, port, &br->ip6_other_query,
1346 &saddr, max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001347 goto out;
Linus Lüssing6c03ee82014-06-12 01:41:23 +02001348 } else if (!group) {
1349 goto out;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001350 }
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001351
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001352 mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001353 if (!mp)
1354 goto out;
1355
1356 max_delay *= br->multicast_last_member_count;
Herbert Xu8a870172011-02-12 01:05:42 -08001357 if (mp->mglist &&
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001358 (timer_pending(&mp->timer) ?
1359 time_after(mp->timer.expires, now + max_delay) :
1360 try_to_del_timer_sync(&mp->timer) >= 0))
1361 mod_timer(&mp->timer, now + max_delay);
1362
Eric Dumazete8051682010-11-15 06:38:10 +00001363 for (pp = &mp->ports;
1364 (p = mlock_dereference(*pp, br)) != NULL;
1365 pp = &p->next) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001366 if (timer_pending(&p->timer) ?
1367 time_after(p->timer.expires, now + max_delay) :
1368 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001369 mod_timer(&p->timer, now + max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001370 }
1371
1372out:
1373 spin_unlock(&br->multicast_lock);
1374 return err;
1375}
1376#endif
1377
Linus Lüssing90010b32014-06-07 18:26:26 +02001378static void
1379br_multicast_leave_group(struct net_bridge *br,
1380 struct net_bridge_port *port,
1381 struct br_ip *group,
1382 struct bridge_mcast_other_query *other_query,
1383 struct bridge_mcast_own_query *own_query)
Herbert Xueb1d1642010-02-27 19:41:45 +00001384{
1385 struct net_bridge_mdb_htable *mdb;
1386 struct net_bridge_mdb_entry *mp;
1387 struct net_bridge_port_group *p;
1388 unsigned long now;
1389 unsigned long time;
1390
Herbert Xueb1d1642010-02-27 19:41:45 +00001391 spin_lock(&br->multicast_lock);
1392 if (!netif_running(br->dev) ||
1393 (port && port->state == BR_STATE_DISABLED) ||
Linus Lüssing90010b32014-06-07 18:26:26 +02001394 timer_pending(&other_query->timer))
Herbert Xueb1d1642010-02-27 19:41:45 +00001395 goto out;
1396
Eric Dumazete8051682010-11-15 06:38:10 +00001397 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001398 mp = br_mdb_ip_get(mdb, group);
1399 if (!mp)
1400 goto out;
1401
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001402 if (br->multicast_querier) {
Cong Wang6b7df112013-05-21 21:52:56 +00001403 __br_multicast_send_query(br, port, &mp->addr);
1404
1405 time = jiffies + br->multicast_last_member_count *
1406 br->multicast_last_member_interval;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001407
Linus Lüssing90010b32014-06-07 18:26:26 +02001408 mod_timer(&own_query->timer, time);
Cong Wang6b7df112013-05-21 21:52:56 +00001409
1410 for (p = mlock_dereference(mp->ports, br);
1411 p != NULL;
1412 p = mlock_dereference(p->next, br)) {
1413 if (p->port != port)
1414 continue;
1415
1416 if (!hlist_unhashed(&p->mglist) &&
1417 (timer_pending(&p->timer) ?
1418 time_after(p->timer.expires, time) :
1419 try_to_del_timer_sync(&p->timer) >= 0)) {
1420 mod_timer(&p->timer, time);
1421 }
1422
1423 break;
1424 }
1425 }
1426
David S. Millerc2d3bab2012-12-05 16:24:45 -05001427 if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
Amerigo Wang50426b52012-12-03 23:56:40 +00001428 struct net_bridge_port_group __rcu **pp;
1429
1430 for (pp = &mp->ports;
1431 (p = mlock_dereference(*pp, br)) != NULL;
1432 pp = &p->next) {
1433 if (p->port != port)
1434 continue;
1435
1436 rcu_assign_pointer(*pp, p->next);
1437 hlist_del_init(&p->mglist);
1438 del_timer(&p->timer);
1439 call_rcu_bh(&p->rcu, br_multicast_free_pg);
Cong Wang37a393b2012-12-11 22:23:07 +00001440 br_mdb_notify(br->dev, port, group, RTM_DELMDB);
Amerigo Wang50426b52012-12-03 23:56:40 +00001441
Linus Lüssing454594f2013-10-20 00:58:57 +02001442 if (!mp->ports && !mp->mglist &&
Amerigo Wang50426b52012-12-03 23:56:40 +00001443 netif_running(br->dev))
1444 mod_timer(&mp->timer, jiffies);
1445 }
1446 goto out;
1447 }
1448
Herbert Xueb1d1642010-02-27 19:41:45 +00001449 now = jiffies;
1450 time = now + br->multicast_last_member_count *
1451 br->multicast_last_member_interval;
1452
1453 if (!port) {
Linus Lüssing454594f2013-10-20 00:58:57 +02001454 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001455 (timer_pending(&mp->timer) ?
1456 time_after(mp->timer.expires, time) :
1457 try_to_del_timer_sync(&mp->timer) >= 0)) {
1458 mod_timer(&mp->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +00001459 }
Linus Lüssing454594f2013-10-20 00:58:57 +02001460
1461 goto out;
1462 }
1463
1464 for (p = mlock_dereference(mp->ports, br);
1465 p != NULL;
1466 p = mlock_dereference(p->next, br)) {
1467 if (p->port != port)
1468 continue;
1469
1470 if (!hlist_unhashed(&p->mglist) &&
1471 (timer_pending(&p->timer) ?
1472 time_after(p->timer.expires, time) :
1473 try_to_del_timer_sync(&p->timer) >= 0)) {
1474 mod_timer(&p->timer, time);
1475 }
1476
1477 break;
Herbert Xueb1d1642010-02-27 19:41:45 +00001478 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001479out:
1480 spin_unlock(&br->multicast_lock);
1481}
1482
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001483static void br_ip4_multicast_leave_group(struct net_bridge *br,
1484 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001485 __be32 group,
1486 __u16 vid)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001487{
1488 struct br_ip br_group;
Linus Lüssing90010b32014-06-07 18:26:26 +02001489 struct bridge_mcast_own_query *own_query;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001490
1491 if (ipv4_is_local_multicast(group))
1492 return;
1493
Linus Lüssing90010b32014-06-07 18:26:26 +02001494 own_query = port ? &port->ip4_own_query : &br->ip4_own_query;
1495
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001496 br_group.u.ip4 = group;
1497 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001498 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001499
Linus Lüssing90010b32014-06-07 18:26:26 +02001500 br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
1501 own_query);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001502}
1503
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001504#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001505static void br_ip6_multicast_leave_group(struct net_bridge *br,
1506 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001507 const struct in6_addr *group,
1508 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001509{
1510 struct br_ip br_group;
Linus Lüssing90010b32014-06-07 18:26:26 +02001511 struct bridge_mcast_own_query *own_query;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001512
Linus Lüssing3c3769e2013-09-04 02:13:39 +02001513 if (ipv6_addr_is_ll_all_nodes(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001514 return;
1515
Linus Lüssing90010b32014-06-07 18:26:26 +02001516 own_query = port ? &port->ip6_own_query : &br->ip6_own_query;
1517
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001518 br_group.u.ip6 = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001519 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001520 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001521
Linus Lüssing90010b32014-06-07 18:26:26 +02001522 br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
1523 own_query);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001524}
1525#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001526
Herbert Xueb1d1642010-02-27 19:41:45 +00001527static int br_multicast_ipv4_rcv(struct net_bridge *br,
1528 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001529 struct sk_buff *skb,
1530 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001531{
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001532 struct sk_buff *skb_trimmed = NULL;
Herbert Xueb1d1642010-02-27 19:41:45 +00001533 struct igmphdr *ih;
Herbert Xueb1d1642010-02-27 19:41:45 +00001534 int err;
1535
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001536 err = ip_mc_check_igmp(skb, &skb_trimmed);
Herbert Xueb1d1642010-02-27 19:41:45 +00001537
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001538 if (err == -ENOMSG) {
1539 if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr))
Herbert Xubd4265f2011-06-23 02:39:12 +00001540 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Herbert Xueb1d1642010-02-27 19:41:45 +00001541 return 0;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001542 } else if (err < 0) {
1543 return err;
Herbert Xubd4265f2011-06-23 02:39:12 +00001544 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001545
Herbert Xueb1d1642010-02-27 19:41:45 +00001546 BR_INPUT_SKB_CB(skb)->igmp = 1;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001547 ih = igmp_hdr(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001548
1549 switch (ih->type) {
1550 case IGMP_HOST_MEMBERSHIP_REPORT:
1551 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Fernando Luis Vázquez Cao62b2bcb2011-06-13 15:04:43 +00001552 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001553 err = br_ip4_multicast_add_group(br, port, ih->group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001554 break;
1555 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001556 err = br_ip4_multicast_igmp3_report(br, port, skb_trimmed, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001557 break;
1558 case IGMP_HOST_MEMBERSHIP_QUERY:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001559 err = br_ip4_multicast_query(br, port, skb_trimmed, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001560 break;
1561 case IGMP_HOST_LEAVE_MESSAGE:
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001562 br_ip4_multicast_leave_group(br, port, ih->group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001563 break;
1564 }
1565
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001566 if (skb_trimmed)
1567 kfree_skb(skb_trimmed);
1568
Herbert Xueb1d1642010-02-27 19:41:45 +00001569 return err;
1570}
1571
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001572#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001573static int br_multicast_ipv6_rcv(struct net_bridge *br,
1574 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001575 struct sk_buff *skb,
1576 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001577{
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001578 struct sk_buff *skb_trimmed = NULL;
1579 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001580 int err;
1581
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001582 err = ipv6_mc_check_mld(skb, &skb_trimmed);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001583
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001584 if (err == -ENOMSG) {
1585 if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
1586 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Linus Lüssing8fad9c32013-09-04 02:13:38 +02001587 return 0;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001588 } else if (err < 0) {
1589 return err;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001590 }
1591
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001592 BR_INPUT_SKB_CB(skb)->igmp = 1;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001593 mld = (struct mld_msg *)skb_transport_header(skb);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001594
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001595 switch (mld->mld_type) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001596 case ICMPV6_MGM_REPORT:
Fernando Luis Vázquez Caofc2af6c2011-06-13 15:06:58 +00001597 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001598 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001599 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001600 case ICMPV6_MLD2_REPORT:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001601 err = br_ip6_multicast_mld2_report(br, port, skb_trimmed, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001602 break;
1603 case ICMPV6_MGM_QUERY:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001604 err = br_ip6_multicast_query(br, port, skb_trimmed, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001605 break;
1606 case ICMPV6_MGM_REDUCTION:
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001607 br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001608 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001609 }
1610
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001611 if (skb_trimmed)
1612 kfree_skb(skb_trimmed);
1613
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001614 return err;
1615}
1616#endif
1617
Herbert Xueb1d1642010-02-27 19:41:45 +00001618int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001619 struct sk_buff *skb, u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001620{
YOSHIFUJI Hideaki / 吉藤英明1fafc7a2010-04-25 08:06:40 +00001621 BR_INPUT_SKB_CB(skb)->igmp = 0;
1622 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1623
Herbert Xueb1d1642010-02-27 19:41:45 +00001624 if (br->multicast_disabled)
1625 return 0;
1626
1627 switch (skb->protocol) {
1628 case htons(ETH_P_IP):
Vlad Yasevich06499092013-10-28 15:45:07 -04001629 return br_multicast_ipv4_rcv(br, port, skb, vid);
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001630#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001631 case htons(ETH_P_IPV6):
Vlad Yasevich06499092013-10-28 15:45:07 -04001632 return br_multicast_ipv6_rcv(br, port, skb, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001633#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001634 }
1635
1636 return 0;
1637}
1638
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001639static void br_multicast_query_expired(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001640 struct bridge_mcast_own_query *query,
1641 struct bridge_mcast_querier *querier)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001642{
1643 spin_lock(&br->multicast_lock);
1644 if (query->startup_sent < br->multicast_startup_query_count)
1645 query->startup_sent++;
1646
Andreea-Cristina Bernat09329972014-08-22 16:06:09 +03001647 RCU_INIT_POINTER(querier, NULL);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001648 br_multicast_send_query(br, NULL, query);
1649 spin_unlock(&br->multicast_lock);
1650}
1651
1652static void br_ip4_multicast_query_expired(unsigned long data)
Herbert Xueb1d1642010-02-27 19:41:45 +00001653{
1654 struct net_bridge *br = (void *)data;
1655
Linus Lüssing2cd41432014-06-07 18:26:29 +02001656 br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier);
Herbert Xueb1d1642010-02-27 19:41:45 +00001657}
1658
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001659#if IS_ENABLED(CONFIG_IPV6)
1660static void br_ip6_multicast_query_expired(unsigned long data)
1661{
1662 struct net_bridge *br = (void *)data;
1663
Linus Lüssing2cd41432014-06-07 18:26:29 +02001664 br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001665}
1666#endif
1667
Herbert Xueb1d1642010-02-27 19:41:45 +00001668void br_multicast_init(struct net_bridge *br)
1669{
1670 br->hash_elasticity = 4;
1671 br->hash_max = 512;
1672
1673 br->multicast_router = 1;
Herbert Xuc5c23262012-04-13 02:37:42 +00001674 br->multicast_querier = 0;
Cong Wang1c8ad5b2013-05-21 21:52:54 +00001675 br->multicast_query_use_ifaddr = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001676 br->multicast_last_member_count = 2;
1677 br->multicast_startup_query_count = 2;
1678
1679 br->multicast_last_member_interval = HZ;
1680 br->multicast_query_response_interval = 10 * HZ;
1681 br->multicast_startup_query_interval = 125 * HZ / 4;
1682 br->multicast_query_interval = 125 * HZ;
1683 br->multicast_querier_interval = 255 * HZ;
1684 br->multicast_membership_interval = 260 * HZ;
1685
Linus Lüssing90010b32014-06-07 18:26:26 +02001686 br->ip4_other_query.delay_time = 0;
Linus Lüssing2cd41432014-06-07 18:26:29 +02001687 br->ip4_querier.port = NULL;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001688#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001689 br->ip6_other_query.delay_time = 0;
Linus Lüssing2cd41432014-06-07 18:26:29 +02001690 br->ip6_querier.port = NULL;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001691#endif
Linus Lüssingb00589a2013-08-01 01:06:20 +02001692
Herbert Xueb1d1642010-02-27 19:41:45 +00001693 spin_lock_init(&br->multicast_lock);
1694 setup_timer(&br->multicast_router_timer,
1695 br_multicast_local_router_expired, 0);
Linus Lüssing90010b32014-06-07 18:26:26 +02001696 setup_timer(&br->ip4_other_query.timer,
1697 br_ip4_multicast_querier_expired, (unsigned long)br);
1698 setup_timer(&br->ip4_own_query.timer, br_ip4_multicast_query_expired,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001699 (unsigned long)br);
1700#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001701 setup_timer(&br->ip6_other_query.timer,
1702 br_ip6_multicast_querier_expired, (unsigned long)br);
1703 setup_timer(&br->ip6_own_query.timer, br_ip6_multicast_query_expired,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001704 (unsigned long)br);
1705#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001706}
1707
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001708static void __br_multicast_open(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +02001709 struct bridge_mcast_own_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +00001710{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001711 query->startup_sent = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001712
1713 if (br->multicast_disabled)
1714 return;
1715
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001716 mod_timer(&query->timer, jiffies);
1717}
1718
1719void br_multicast_open(struct net_bridge *br)
1720{
Linus Lüssing90010b32014-06-07 18:26:26 +02001721 __br_multicast_open(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001722#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001723 __br_multicast_open(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001724#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001725}
1726
1727void br_multicast_stop(struct net_bridge *br)
1728{
1729 struct net_bridge_mdb_htable *mdb;
1730 struct net_bridge_mdb_entry *mp;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001731 struct hlist_node *n;
Herbert Xueb1d1642010-02-27 19:41:45 +00001732 u32 ver;
1733 int i;
1734
1735 del_timer_sync(&br->multicast_router_timer);
Linus Lüssing90010b32014-06-07 18:26:26 +02001736 del_timer_sync(&br->ip4_other_query.timer);
1737 del_timer_sync(&br->ip4_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001738#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001739 del_timer_sync(&br->ip6_other_query.timer);
1740 del_timer_sync(&br->ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001741#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001742
1743 spin_lock_bh(&br->multicast_lock);
Eric Dumazete8051682010-11-15 06:38:10 +00001744 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001745 if (!mdb)
1746 goto out;
1747
1748 br->mdb = NULL;
1749
1750 ver = mdb->ver;
1751 for (i = 0; i < mdb->max; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001752 hlist_for_each_entry_safe(mp, n, &mdb->mhash[i],
Herbert Xueb1d1642010-02-27 19:41:45 +00001753 hlist[ver]) {
1754 del_timer(&mp->timer);
Herbert Xueb1d1642010-02-27 19:41:45 +00001755 call_rcu_bh(&mp->rcu, br_multicast_free_group);
1756 }
1757 }
1758
1759 if (mdb->old) {
1760 spin_unlock_bh(&br->multicast_lock);
Herbert Xu10cc2b52010-03-05 21:03:35 +00001761 rcu_barrier_bh();
Herbert Xueb1d1642010-02-27 19:41:45 +00001762 spin_lock_bh(&br->multicast_lock);
1763 WARN_ON(mdb->old);
1764 }
1765
1766 mdb->old = mdb;
1767 call_rcu_bh(&mdb->rcu, br_mdb_free);
1768
1769out:
1770 spin_unlock_bh(&br->multicast_lock);
1771}
Herbert Xu0909e112010-02-27 19:41:49 +00001772
1773int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1774{
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02001775 int err = -EINVAL;
Herbert Xu0909e112010-02-27 19:41:49 +00001776
1777 spin_lock_bh(&br->multicast_lock);
Herbert Xu0909e112010-02-27 19:41:49 +00001778
1779 switch (val) {
1780 case 0:
1781 case 2:
1782 del_timer(&br->multicast_router_timer);
1783 /* fall through */
1784 case 1:
1785 br->multicast_router = val;
1786 err = 0;
1787 break;
Herbert Xu0909e112010-02-27 19:41:49 +00001788 }
1789
Herbert Xu0909e112010-02-27 19:41:49 +00001790 spin_unlock_bh(&br->multicast_lock);
1791
1792 return err;
1793}
1794
1795int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1796{
1797 struct net_bridge *br = p->br;
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02001798 int err = -EINVAL;
Herbert Xu0909e112010-02-27 19:41:49 +00001799
1800 spin_lock(&br->multicast_lock);
Herbert Xu0909e112010-02-27 19:41:49 +00001801
1802 switch (val) {
1803 case 0:
1804 case 1:
1805 case 2:
1806 p->multicast_router = val;
1807 err = 0;
1808
1809 if (val < 2 && !hlist_unhashed(&p->rlist))
1810 hlist_del_init_rcu(&p->rlist);
1811
1812 if (val == 1)
1813 break;
1814
1815 del_timer(&p->multicast_router_timer);
1816
1817 if (val == 0)
1818 break;
1819
1820 br_multicast_add_router(br, p);
1821 break;
Herbert Xu0909e112010-02-27 19:41:49 +00001822 }
1823
Herbert Xu0909e112010-02-27 19:41:49 +00001824 spin_unlock(&br->multicast_lock);
1825
1826 return err;
1827}
Herbert Xu561f1102010-02-27 19:41:50 +00001828
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001829static void br_multicast_start_querier(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +02001830 struct bridge_mcast_own_query *query)
Herbert Xu561f1102010-02-27 19:41:50 +00001831{
1832 struct net_bridge_port *port;
Herbert Xu74857212012-04-13 02:37:42 +00001833
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001834 __br_multicast_open(br, query);
Herbert Xu74857212012-04-13 02:37:42 +00001835
1836 list_for_each_entry(port, &br->port_list, list) {
1837 if (port->state == BR_STATE_DISABLED ||
1838 port->state == BR_STATE_BLOCKING)
1839 continue;
1840
Linus Lüssing90010b32014-06-07 18:26:26 +02001841 if (query == &br->ip4_own_query)
1842 br_multicast_enable(&port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001843#if IS_ENABLED(CONFIG_IPV6)
1844 else
Linus Lüssing90010b32014-06-07 18:26:26 +02001845 br_multicast_enable(&port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001846#endif
Herbert Xu74857212012-04-13 02:37:42 +00001847 }
1848}
1849
1850int br_multicast_toggle(struct net_bridge *br, unsigned long val)
1851{
Herbert Xu3a7fda02010-07-29 00:45:30 +00001852 int err = 0;
Eric Dumazete8051682010-11-15 06:38:10 +00001853 struct net_bridge_mdb_htable *mdb;
Herbert Xu561f1102010-02-27 19:41:50 +00001854
Andrey Vaginef5e0d82011-11-10 05:48:03 +00001855 spin_lock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00001856 if (br->multicast_disabled == !val)
1857 goto unlock;
1858
1859 br->multicast_disabled = !val;
1860 if (br->multicast_disabled)
1861 goto unlock;
1862
Herbert Xu3a7fda02010-07-29 00:45:30 +00001863 if (!netif_running(br->dev))
1864 goto unlock;
1865
Eric Dumazete8051682010-11-15 06:38:10 +00001866 mdb = mlock_dereference(br->mdb, br);
1867 if (mdb) {
1868 if (mdb->old) {
Herbert Xu561f1102010-02-27 19:41:50 +00001869 err = -EEXIST;
1870rollback:
1871 br->multicast_disabled = !!val;
1872 goto unlock;
1873 }
1874
Eric Dumazete8051682010-11-15 06:38:10 +00001875 err = br_mdb_rehash(&br->mdb, mdb->max,
Herbert Xu561f1102010-02-27 19:41:50 +00001876 br->hash_elasticity);
1877 if (err)
1878 goto rollback;
1879 }
1880
Linus Lüssing90010b32014-06-07 18:26:26 +02001881 br_multicast_start_querier(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001882#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001883 br_multicast_start_querier(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001884#endif
Herbert Xu561f1102010-02-27 19:41:50 +00001885
1886unlock:
Andrey Vaginef5e0d82011-11-10 05:48:03 +00001887 spin_unlock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00001888
1889 return err;
1890}
Herbert Xub1951672010-02-27 19:41:51 +00001891
Herbert Xuc5c23262012-04-13 02:37:42 +00001892int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
1893{
Linus Lüssingb00589a2013-08-01 01:06:20 +02001894 unsigned long max_delay;
1895
Herbert Xuc5c23262012-04-13 02:37:42 +00001896 val = !!val;
1897
1898 spin_lock_bh(&br->multicast_lock);
1899 if (br->multicast_querier == val)
1900 goto unlock;
1901
1902 br->multicast_querier = val;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001903 if (!val)
1904 goto unlock;
1905
1906 max_delay = br->multicast_query_response_interval;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001907
Linus Lüssing90010b32014-06-07 18:26:26 +02001908 if (!timer_pending(&br->ip4_other_query.timer))
1909 br->ip4_other_query.delay_time = jiffies + max_delay;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001910
Linus Lüssing90010b32014-06-07 18:26:26 +02001911 br_multicast_start_querier(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001912
1913#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001914 if (!timer_pending(&br->ip6_other_query.timer))
1915 br->ip6_other_query.delay_time = jiffies + max_delay;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001916
Linus Lüssing90010b32014-06-07 18:26:26 +02001917 br_multicast_start_querier(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001918#endif
Herbert Xuc5c23262012-04-13 02:37:42 +00001919
1920unlock:
1921 spin_unlock_bh(&br->multicast_lock);
1922
1923 return 0;
1924}
1925
Herbert Xub1951672010-02-27 19:41:51 +00001926int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
1927{
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02001928 int err = -EINVAL;
Herbert Xub1951672010-02-27 19:41:51 +00001929 u32 old;
Eric Dumazete8051682010-11-15 06:38:10 +00001930 struct net_bridge_mdb_htable *mdb;
Herbert Xub1951672010-02-27 19:41:51 +00001931
Curt Brunefe0d6922014-01-06 11:00:32 -08001932 spin_lock_bh(&br->multicast_lock);
Herbert Xub1951672010-02-27 19:41:51 +00001933 if (!is_power_of_2(val))
1934 goto unlock;
Eric Dumazete8051682010-11-15 06:38:10 +00001935
1936 mdb = mlock_dereference(br->mdb, br);
1937 if (mdb && val < mdb->size)
Herbert Xub1951672010-02-27 19:41:51 +00001938 goto unlock;
1939
1940 err = 0;
1941
1942 old = br->hash_max;
1943 br->hash_max = val;
1944
Eric Dumazete8051682010-11-15 06:38:10 +00001945 if (mdb) {
1946 if (mdb->old) {
Herbert Xub1951672010-02-27 19:41:51 +00001947 err = -EEXIST;
1948rollback:
1949 br->hash_max = old;
1950 goto unlock;
1951 }
1952
1953 err = br_mdb_rehash(&br->mdb, br->hash_max,
1954 br->hash_elasticity);
1955 if (err)
1956 goto rollback;
1957 }
1958
1959unlock:
Curt Brunefe0d6922014-01-06 11:00:32 -08001960 spin_unlock_bh(&br->multicast_lock);
Herbert Xub1951672010-02-27 19:41:51 +00001961
1962 return err;
1963}
Linus Lüssing07f8ac42014-06-07 18:26:28 +02001964
1965/**
1966 * br_multicast_list_adjacent - Returns snooped multicast addresses
1967 * @dev: The bridge port adjacent to which to retrieve addresses
1968 * @br_ip_list: The list to store found, snooped multicast IP addresses in
1969 *
1970 * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
1971 * snooping feature on all bridge ports of dev's bridge device, excluding
1972 * the addresses from dev itself.
1973 *
1974 * Returns the number of items added to br_ip_list.
1975 *
1976 * Notes:
1977 * - br_ip_list needs to be initialized by caller
1978 * - br_ip_list might contain duplicates in the end
1979 * (needs to be taken care of by caller)
1980 * - br_ip_list needs to be freed by caller
1981 */
1982int br_multicast_list_adjacent(struct net_device *dev,
1983 struct list_head *br_ip_list)
1984{
1985 struct net_bridge *br;
1986 struct net_bridge_port *port;
1987 struct net_bridge_port_group *group;
1988 struct br_ip_list *entry;
1989 int count = 0;
1990
1991 rcu_read_lock();
1992 if (!br_ip_list || !br_port_exists(dev))
1993 goto unlock;
1994
1995 port = br_port_get_rcu(dev);
1996 if (!port || !port->br)
1997 goto unlock;
1998
1999 br = port->br;
2000
2001 list_for_each_entry_rcu(port, &br->port_list, list) {
2002 if (!port->dev || port->dev == dev)
2003 continue;
2004
2005 hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
2006 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
2007 if (!entry)
2008 goto unlock;
2009
2010 entry->addr = group->addr;
2011 list_add(&entry->list, br_ip_list);
2012 count++;
2013 }
2014 }
2015
2016unlock:
2017 rcu_read_unlock();
2018 return count;
2019}
2020EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
Linus Lüssing2cd41432014-06-07 18:26:29 +02002021
2022/**
Linus Lüssingc34963e2014-07-07 05:41:17 +02002023 * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2024 * @dev: The bridge port providing the bridge on which to check for a querier
2025 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2026 *
2027 * Checks whether the given interface has a bridge on top and if so returns
2028 * true if a valid querier exists anywhere on the bridged link layer.
2029 * Otherwise returns false.
2030 */
2031bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
2032{
2033 struct net_bridge *br;
2034 struct net_bridge_port *port;
2035 struct ethhdr eth;
2036 bool ret = false;
2037
2038 rcu_read_lock();
2039 if (!br_port_exists(dev))
2040 goto unlock;
2041
2042 port = br_port_get_rcu(dev);
2043 if (!port || !port->br)
2044 goto unlock;
2045
2046 br = port->br;
2047
2048 memset(&eth, 0, sizeof(eth));
2049 eth.h_proto = htons(proto);
2050
2051 ret = br_multicast_querier_exists(br, &eth);
2052
2053unlock:
2054 rcu_read_unlock();
2055 return ret;
2056}
2057EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
2058
2059/**
Linus Lüssing2cd41432014-06-07 18:26:29 +02002060 * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
2061 * @dev: The bridge port adjacent to which to check for a querier
2062 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2063 *
2064 * Checks whether the given interface has a bridge on top and if so returns
2065 * true if a selected querier is behind one of the other ports of this
2066 * bridge. Otherwise returns false.
2067 */
2068bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
2069{
2070 struct net_bridge *br;
2071 struct net_bridge_port *port;
2072 bool ret = false;
2073
2074 rcu_read_lock();
2075 if (!br_port_exists(dev))
2076 goto unlock;
2077
2078 port = br_port_get_rcu(dev);
2079 if (!port || !port->br)
2080 goto unlock;
2081
2082 br = port->br;
2083
2084 switch (proto) {
2085 case ETH_P_IP:
2086 if (!timer_pending(&br->ip4_other_query.timer) ||
2087 rcu_dereference(br->ip4_querier.port) == port)
2088 goto unlock;
2089 break;
Linus Lüssing3993c4e2014-06-12 01:41:24 +02002090#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing2cd41432014-06-07 18:26:29 +02002091 case ETH_P_IPV6:
2092 if (!timer_pending(&br->ip6_other_query.timer) ||
2093 rcu_dereference(br->ip6_querier.port) == port)
2094 goto unlock;
2095 break;
Linus Lüssing3993c4e2014-06-12 01:41:24 +02002096#endif
Linus Lüssing2cd41432014-06-07 18:26:29 +02002097 default:
2098 goto unlock;
2099 }
2100
2101 ret = true;
2102unlock:
2103 rcu_read_unlock();
2104 return ret;
2105}
2106EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);