blob: a4c15df2b7920301f7d12918dc21a1d6f2474352 [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);
Satish Ashok754bc542015-06-19 01:22:57 -070040static void br_multicast_add_router(struct net_bridge *br,
41 struct net_bridge_port *port);
Satish Ashokbc8c20a2015-07-13 05:28:37 -070042static void br_ip4_multicast_leave_group(struct net_bridge *br,
43 struct net_bridge_port *port,
44 __be32 group,
45 __u16 vid);
46#if IS_ENABLED(CONFIG_IPV6)
47static void br_ip6_multicast_leave_group(struct net_bridge *br,
48 struct net_bridge_port *port,
49 const struct in6_addr *group,
50 __u16 vid);
51#endif
Cong Wang2ce297f2012-12-10 02:15:35 +000052unsigned int br_mdb_rehash_seq;
Herbert Xuc83b8fa2012-04-13 02:37:42 +000053
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090054static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
55{
56 if (a->proto != b->proto)
57 return 0;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000058 if (a->vid != b->vid)
59 return 0;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090060 switch (a->proto) {
61 case htons(ETH_P_IP):
62 return a->u.ip4 == b->u.ip4;
Eric Dumazetdfd56b82011-12-10 09:48:31 +000063#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090064 case htons(ETH_P_IPV6):
65 return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
66#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090067 }
68 return 0;
69}
70
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000071static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip,
72 __u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +000073{
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000074 return jhash_2words((__force u32)ip, vid, mdb->secret) & (mdb->max - 1);
Herbert Xueb1d1642010-02-27 19:41:45 +000075}
76
Eric Dumazetdfd56b82011-12-10 09:48:31 +000077#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090078static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000079 const struct in6_addr *ip,
80 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090081{
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000082 return jhash_2words(ipv6_addr_hash(ip), vid,
83 mdb->secret) & (mdb->max - 1);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090084}
85#endif
86
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090087static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb,
88 struct br_ip *ip)
89{
90 switch (ip->proto) {
91 case htons(ETH_P_IP):
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000092 return __br_ip4_hash(mdb, ip->u.ip4, ip->vid);
Eric Dumazetdfd56b82011-12-10 09:48:31 +000093#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090094 case htons(ETH_P_IPV6):
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000095 return __br_ip6_hash(mdb, &ip->u.ip6, ip->vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090096#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090097 }
98 return 0;
Herbert Xueb1d1642010-02-27 19:41:45 +000099}
100
101static struct net_bridge_mdb_entry *__br_mdb_ip_get(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900102 struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +0000103{
104 struct net_bridge_mdb_entry *mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000105
Sasha Levinb67bfe02013-02-27 17:06:00 -0800106 hlist_for_each_entry_rcu(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900107 if (br_ip_equal(&mp->addr, dst))
Herbert Xueb1d1642010-02-27 19:41:45 +0000108 return mp;
109 }
110
111 return NULL;
112}
113
Cong Wangcfd56752012-12-11 22:23:08 +0000114struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge_mdb_htable *mdb,
115 struct br_ip *dst)
Herbert Xu7f285fa2010-07-05 14:50:08 +0000116{
117 if (!mdb)
118 return NULL;
119
120 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
121}
122
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900123static struct net_bridge_mdb_entry *br_mdb_ip4_get(
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000124 struct net_bridge_mdb_htable *mdb, __be32 dst, __u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000125{
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900126 struct br_ip br_dst;
Herbert Xu0821ec52010-03-15 20:38:25 -0700127
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900128 br_dst.u.ip4 = dst;
129 br_dst.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000130 br_dst.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900131
Herbert Xu7f285fa2010-07-05 14:50:08 +0000132 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900133}
134
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000135#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900136static struct net_bridge_mdb_entry *br_mdb_ip6_get(
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000137 struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst,
138 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900139{
140 struct br_ip br_dst;
141
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000142 br_dst.u.ip6 = *dst;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900143 br_dst.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000144 br_dst.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900145
Herbert Xu7f285fa2010-07-05 14:50:08 +0000146 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900147}
148#endif
149
Herbert Xueb1d1642010-02-27 19:41:45 +0000150struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
Cong Wangfbca58a2013-03-07 03:05:33 +0000151 struct sk_buff *skb, u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000152{
Eric Dumazete8051682010-11-15 06:38:10 +0000153 struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900154 struct br_ip ip;
155
Herbert Xu7f285fa2010-07-05 14:50:08 +0000156 if (br->multicast_disabled)
Herbert Xueb1d1642010-02-27 19:41:45 +0000157 return NULL;
158
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900159 if (BR_INPUT_SKB_CB(skb)->igmp)
160 return NULL;
161
162 ip.proto = skb->protocol;
Cong Wangfbca58a2013-03-07 03:05:33 +0000163 ip.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900164
Herbert Xueb1d1642010-02-27 19:41:45 +0000165 switch (skb->protocol) {
166 case htons(ETH_P_IP):
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900167 ip.u.ip4 = ip_hdr(skb)->daddr;
168 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000169#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900170 case htons(ETH_P_IPV6):
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000171 ip.u.ip6 = ipv6_hdr(skb)->daddr;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900172 break;
173#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900174 default:
175 return NULL;
Herbert Xueb1d1642010-02-27 19:41:45 +0000176 }
177
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900178 return br_mdb_ip_get(mdb, &ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000179}
180
181static void br_mdb_free(struct rcu_head *head)
182{
183 struct net_bridge_mdb_htable *mdb =
184 container_of(head, struct net_bridge_mdb_htable, rcu);
185 struct net_bridge_mdb_htable *old = mdb->old;
186
187 mdb->old = NULL;
188 kfree(old->mhash);
189 kfree(old);
190}
191
192static int br_mdb_copy(struct net_bridge_mdb_htable *new,
193 struct net_bridge_mdb_htable *old,
194 int elasticity)
195{
196 struct net_bridge_mdb_entry *mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000197 int maxlen;
198 int len;
199 int i;
200
201 for (i = 0; i < old->max; i++)
Sasha Levinb67bfe02013-02-27 17:06:00 -0800202 hlist_for_each_entry(mp, &old->mhash[i], hlist[old->ver])
Herbert Xueb1d1642010-02-27 19:41:45 +0000203 hlist_add_head(&mp->hlist[new->ver],
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900204 &new->mhash[br_ip_hash(new, &mp->addr)]);
Herbert Xueb1d1642010-02-27 19:41:45 +0000205
206 if (!elasticity)
207 return 0;
208
209 maxlen = 0;
210 for (i = 0; i < new->max; i++) {
211 len = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800212 hlist_for_each_entry(mp, &new->mhash[i], hlist[new->ver])
Herbert Xueb1d1642010-02-27 19:41:45 +0000213 len++;
214 if (len > maxlen)
215 maxlen = len;
216 }
217
218 return maxlen > elasticity ? -EINVAL : 0;
219}
220
Cong Wangcfd56752012-12-11 22:23:08 +0000221void br_multicast_free_pg(struct rcu_head *head)
Herbert Xueb1d1642010-02-27 19:41:45 +0000222{
223 struct net_bridge_port_group *p =
224 container_of(head, struct net_bridge_port_group, rcu);
225
226 kfree(p);
227}
228
229static void br_multicast_free_group(struct rcu_head *head)
230{
231 struct net_bridge_mdb_entry *mp =
232 container_of(head, struct net_bridge_mdb_entry, rcu);
233
234 kfree(mp);
235}
236
237static void br_multicast_group_expired(unsigned long data)
238{
239 struct net_bridge_mdb_entry *mp = (void *)data;
240 struct net_bridge *br = mp->br;
241 struct net_bridge_mdb_htable *mdb;
242
243 spin_lock(&br->multicast_lock);
244 if (!netif_running(br->dev) || timer_pending(&mp->timer))
245 goto out;
246
Herbert Xu8a870172011-02-12 01:05:42 -0800247 mp->mglist = false;
Herbert Xueb1d1642010-02-27 19:41:45 +0000248
249 if (mp->ports)
250 goto out;
251
Eric Dumazete8051682010-11-15 06:38:10 +0000252 mdb = mlock_dereference(br->mdb, br);
253
Herbert Xueb1d1642010-02-27 19:41:45 +0000254 hlist_del_rcu(&mp->hlist[mdb->ver]);
255 mdb->size--;
256
Herbert Xueb1d1642010-02-27 19:41:45 +0000257 call_rcu_bh(&mp->rcu, br_multicast_free_group);
258
259out:
260 spin_unlock(&br->multicast_lock);
261}
262
263static void br_multicast_del_pg(struct net_bridge *br,
264 struct net_bridge_port_group *pg)
265{
Eric Dumazete8051682010-11-15 06:38:10 +0000266 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000267 struct net_bridge_mdb_entry *mp;
268 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +0000269 struct net_bridge_port_group __rcu **pp;
270
271 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +0000272
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900273 mp = br_mdb_ip_get(mdb, &pg->addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000274 if (WARN_ON(!mp))
275 return;
276
Eric Dumazete8051682010-11-15 06:38:10 +0000277 for (pp = &mp->ports;
278 (p = mlock_dereference(*pp, br)) != NULL;
279 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000280 if (p != pg)
281 continue;
282
stephen hemminger83f6a742010-04-27 15:01:06 +0000283 rcu_assign_pointer(*pp, p->next);
Herbert Xueb1d1642010-02-27 19:41:45 +0000284 hlist_del_init(&p->mglist);
285 del_timer(&p->timer);
Elad Raz9e8430f2016-02-03 09:57:06 +0100286 br_mdb_notify(br->dev, p, RTM_DELMDB);
Herbert Xueb1d1642010-02-27 19:41:45 +0000287 call_rcu_bh(&p->rcu, br_multicast_free_pg);
288
Linus Lüssing454594f2013-10-20 00:58:57 +0200289 if (!mp->ports && !mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +0000290 netif_running(br->dev))
291 mod_timer(&mp->timer, jiffies);
292
293 return;
294 }
295
296 WARN_ON(1);
297}
298
299static void br_multicast_port_group_expired(unsigned long data)
300{
301 struct net_bridge_port_group *pg = (void *)data;
302 struct net_bridge *br = pg->port->br;
303
304 spin_lock(&br->multicast_lock);
305 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
Elad Raz9d06b6d2016-02-03 09:57:05 +0100306 hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT)
Herbert Xueb1d1642010-02-27 19:41:45 +0000307 goto out;
308
309 br_multicast_del_pg(br, pg);
310
311out:
312 spin_unlock(&br->multicast_lock);
313}
314
Eric Dumazete8051682010-11-15 06:38:10 +0000315static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max,
Herbert Xueb1d1642010-02-27 19:41:45 +0000316 int elasticity)
317{
Eric Dumazete8051682010-11-15 06:38:10 +0000318 struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000319 struct net_bridge_mdb_htable *mdb;
320 int err;
321
322 mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
323 if (!mdb)
324 return -ENOMEM;
325
326 mdb->max = max;
327 mdb->old = old;
328
329 mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
330 if (!mdb->mhash) {
331 kfree(mdb);
332 return -ENOMEM;
333 }
334
335 mdb->size = old ? old->size : 0;
336 mdb->ver = old ? old->ver ^ 1 : 0;
337
338 if (!old || elasticity)
339 get_random_bytes(&mdb->secret, sizeof(mdb->secret));
340 else
341 mdb->secret = old->secret;
342
343 if (!old)
344 goto out;
345
346 err = br_mdb_copy(mdb, old, elasticity);
347 if (err) {
348 kfree(mdb->mhash);
349 kfree(mdb);
350 return err;
351 }
352
Cong Wang2ce297f2012-12-10 02:15:35 +0000353 br_mdb_rehash_seq++;
Herbert Xueb1d1642010-02-27 19:41:45 +0000354 call_rcu_bh(&mdb->rcu, br_mdb_free);
355
356out:
357 rcu_assign_pointer(*mdbp, mdb);
358
359 return 0;
360}
361
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900362static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
363 __be32 group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000364{
365 struct sk_buff *skb;
366 struct igmphdr *ih;
367 struct ethhdr *eth;
368 struct iphdr *iph;
369
370 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
371 sizeof(*ih) + 4);
372 if (!skb)
373 goto out;
374
375 skb->protocol = htons(ETH_P_IP);
376
377 skb_reset_mac_header(skb);
378 eth = eth_hdr(skb);
379
Joe Perchese5a727f2014-02-23 00:05:25 -0800380 ether_addr_copy(eth->h_source, br->dev->dev_addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000381 eth->h_dest[0] = 1;
382 eth->h_dest[1] = 0;
383 eth->h_dest[2] = 0x5e;
384 eth->h_dest[3] = 0;
385 eth->h_dest[4] = 0;
386 eth->h_dest[5] = 1;
387 eth->h_proto = htons(ETH_P_IP);
388 skb_put(skb, sizeof(*eth));
389
390 skb_set_network_header(skb, skb->len);
391 iph = ip_hdr(skb);
392
393 iph->version = 4;
394 iph->ihl = 6;
395 iph->tos = 0xc0;
396 iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4);
397 iph->id = 0;
398 iph->frag_off = htons(IP_DF);
399 iph->ttl = 1;
400 iph->protocol = IPPROTO_IGMP;
Cong Wang1c8ad5b2013-05-21 21:52:54 +0000401 iph->saddr = br->multicast_query_use_ifaddr ?
402 inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000403 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
404 ((u8 *)&iph[1])[0] = IPOPT_RA;
405 ((u8 *)&iph[1])[1] = 4;
406 ((u8 *)&iph[1])[2] = 0;
407 ((u8 *)&iph[1])[3] = 0;
408 ip_send_check(iph);
409 skb_put(skb, 24);
410
411 skb_set_transport_header(skb, skb->len);
412 ih = igmp_hdr(skb);
413 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
414 ih->code = (group ? br->multicast_last_member_interval :
415 br->multicast_query_response_interval) /
416 (HZ / IGMP_TIMER_SCALE);
417 ih->group = group;
418 ih->csum = 0;
419 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
420 skb_put(skb, sizeof(*ih));
421
422 __skb_pull(skb, sizeof(*eth));
423
424out:
425 return skb;
426}
427
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000428#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900429static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000430 const struct in6_addr *group)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900431{
432 struct sk_buff *skb;
433 struct ipv6hdr *ip6h;
434 struct mld_msg *mldq;
435 struct ethhdr *eth;
436 u8 *hopopt;
437 unsigned long interval;
438
439 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
440 8 + sizeof(*mldq));
441 if (!skb)
442 goto out;
443
444 skb->protocol = htons(ETH_P_IPV6);
445
446 /* Ethernet header */
447 skb_reset_mac_header(skb);
448 eth = eth_hdr(skb);
449
Joe Perchese5a727f2014-02-23 00:05:25 -0800450 ether_addr_copy(eth->h_source, br->dev->dev_addr);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900451 eth->h_proto = htons(ETH_P_IPV6);
452 skb_put(skb, sizeof(*eth));
453
454 /* IPv6 header + HbH option */
455 skb_set_network_header(skb, skb->len);
456 ip6h = ipv6_hdr(skb);
457
458 *(__force __be32 *)ip6h = htonl(0x60000000);
David Stevens76d66152010-12-14 08:42:16 +0000459 ip6h->payload_len = htons(8 + sizeof(*mldq));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900460 ip6h->nexthdr = IPPROTO_HOPOPTS;
461 ip6h->hop_limit = 1;
Linus Lüssinga7bff752011-03-22 11:40:32 +0000462 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
Ulrich Weberd1d81d42012-03-05 04:52:44 +0000463 if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
464 &ip6h->saddr)) {
465 kfree_skb(skb);
466 return NULL;
467 }
Linus Lüssing36cff5a2011-02-17 08:17:51 +0000468 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900469
470 hopopt = (u8 *)(ip6h + 1);
471 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
472 hopopt[1] = 0; /* length of HbH */
473 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
474 hopopt[3] = 2; /* Length of RA Option */
475 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
476 hopopt[5] = 0;
Eldad Zack1de5a712012-05-17 06:00:25 +0000477 hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */
478 hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900479
480 skb_put(skb, sizeof(*ip6h) + 8);
481
482 /* ICMPv6 */
483 skb_set_transport_header(skb, skb->len);
484 mldq = (struct mld_msg *) icmp6_hdr(skb);
485
Linus Lüssing32de8682013-06-16 23:20:34 +0200486 interval = ipv6_addr_any(group) ?
487 br->multicast_query_response_interval :
488 br->multicast_last_member_interval;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900489
490 mldq->mld_type = ICMPV6_MGM_QUERY;
491 mldq->mld_code = 0;
492 mldq->mld_cksum = 0;
493 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
494 mldq->mld_reserved = 0;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000495 mldq->mld_mca = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900496
497 /* checksum */
498 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
499 sizeof(*mldq), IPPROTO_ICMPV6,
500 csum_partial(mldq,
501 sizeof(*mldq), 0));
502 skb_put(skb, sizeof(*mldq));
503
504 __skb_pull(skb, sizeof(*eth));
505
506out:
507 return skb;
508}
509#endif
510
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900511static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
512 struct br_ip *addr)
513{
514 switch (addr->proto) {
515 case htons(ETH_P_IP):
516 return br_ip4_multicast_alloc_query(br, addr->u.ip4);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000517#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900518 case htons(ETH_P_IPV6):
519 return br_ip6_multicast_alloc_query(br, &addr->u.ip6);
520#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900521 }
522 return NULL;
523}
524
Herbert Xueb1d1642010-02-27 19:41:45 +0000525static struct net_bridge_mdb_entry *br_multicast_get_group(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900526 struct net_bridge *br, struct net_bridge_port *port,
527 struct br_ip *group, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +0000528{
Eric Dumazete8051682010-11-15 06:38:10 +0000529 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000530 struct net_bridge_mdb_entry *mp;
Eric Dumazet95c96172012-04-15 05:58:06 +0000531 unsigned int count = 0;
532 unsigned int max;
Herbert Xueb1d1642010-02-27 19:41:45 +0000533 int elasticity;
534 int err;
535
Eric Dumazete8051682010-11-15 06:38:10 +0000536 mdb = rcu_dereference_protected(br->mdb, 1);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800537 hlist_for_each_entry(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000538 count++;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900539 if (unlikely(br_ip_equal(group, &mp->addr)))
Herbert Xueb1d1642010-02-27 19:41:45 +0000540 return mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000541 }
542
543 elasticity = 0;
544 max = mdb->max;
545
546 if (unlikely(count > br->hash_elasticity && count)) {
547 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000548 br_info(br, "Multicast hash table "
549 "chain limit reached: %s\n",
550 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000551
552 elasticity = br->hash_elasticity;
553 }
554
555 if (mdb->size >= max) {
556 max *= 2;
Thomas Graf036be6d2012-07-10 22:29:19 +0000557 if (unlikely(max > br->hash_max)) {
558 br_warn(br, "Multicast hash table maximum of %d "
559 "reached, disabling snooping: %s\n",
560 br->hash_max,
561 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000562 err = -E2BIG;
563disable:
564 br->multicast_disabled = 1;
565 goto err;
566 }
567 }
568
569 if (max > mdb->max || elasticity) {
570 if (mdb->old) {
571 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000572 br_info(br, "Multicast hash table "
573 "on fire: %s\n",
574 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000575 err = -EEXIST;
576 goto err;
577 }
578
579 err = br_mdb_rehash(&br->mdb, max, elasticity);
580 if (err) {
stephen hemminger28a16c92010-05-10 09:31:09 +0000581 br_warn(br, "Cannot rehash multicast "
582 "hash table, disabling snooping: %s, %d, %d\n",
583 port ? port->dev->name : br->dev->name,
584 mdb->size, err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000585 goto disable;
586 }
587
588 err = -EAGAIN;
589 goto err;
590 }
591
592 return NULL;
593
594err:
595 mp = ERR_PTR(err);
596 return mp;
597}
598
Cong Wangcfd56752012-12-11 22:23:08 +0000599struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
600 struct net_bridge_port *port, struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000601{
Eric Dumazete8051682010-11-15 06:38:10 +0000602 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000603 struct net_bridge_mdb_entry *mp;
604 int hash;
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000605 int err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000606
Eric Dumazete8051682010-11-15 06:38:10 +0000607 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000608 if (!mdb) {
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000609 err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0);
610 if (err)
611 return ERR_PTR(err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000612 goto rehash;
613 }
614
615 hash = br_ip_hash(mdb, group);
616 mp = br_multicast_get_group(br, port, group, hash);
617 switch (PTR_ERR(mp)) {
618 case 0:
619 break;
620
621 case -EAGAIN:
622rehash:
Eric Dumazete8051682010-11-15 06:38:10 +0000623 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000624 hash = br_ip_hash(mdb, group);
625 break;
626
627 default:
628 goto out;
629 }
630
631 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
632 if (unlikely(!mp))
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000633 return ERR_PTR(-ENOMEM);
Herbert Xueb1d1642010-02-27 19:41:45 +0000634
635 mp->br = br;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900636 mp->addr = *group;
Eric Dumazet1faabf22013-07-19 20:07:16 -0700637 setup_timer(&mp->timer, br_multicast_group_expired,
638 (unsigned long)mp);
639
Herbert Xueb1d1642010-02-27 19:41:45 +0000640 hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
641 mdb->size++;
642
643out:
644 return mp;
645}
646
Cong Wangcfd56752012-12-11 22:23:08 +0000647struct net_bridge_port_group *br_multicast_new_port_group(
648 struct net_bridge_port *port,
649 struct br_ip *group,
Amerigo Wangccb1c312012-12-14 22:09:51 +0000650 struct net_bridge_port_group __rcu *next,
Elad Raz9d06b6d2016-02-03 09:57:05 +0100651 unsigned char flags)
Cong Wangcfd56752012-12-11 22:23:08 +0000652{
653 struct net_bridge_port_group *p;
654
655 p = kzalloc(sizeof(*p), GFP_ATOMIC);
656 if (unlikely(!p))
657 return NULL;
658
659 p->addr = *group;
660 p->port = port;
Elad Raz9d06b6d2016-02-03 09:57:05 +0100661 p->flags = flags;
stephen hemmingereca2a432012-12-13 06:51:28 +0000662 rcu_assign_pointer(p->next, next);
Cong Wangcfd56752012-12-11 22:23:08 +0000663 hlist_add_head(&p->mglist, &port->mglist);
664 setup_timer(&p->timer, br_multicast_port_group_expired,
665 (unsigned long)p);
666 return p;
667}
668
Herbert Xueb1d1642010-02-27 19:41:45 +0000669static int br_multicast_add_group(struct net_bridge *br,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900670 struct net_bridge_port *port,
671 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000672{
673 struct net_bridge_mdb_entry *mp;
674 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +0000675 struct net_bridge_port_group __rcu **pp;
Linus Lüssing454594f2013-10-20 00:58:57 +0200676 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +0000677 int err;
678
Herbert Xueb1d1642010-02-27 19:41:45 +0000679 spin_lock(&br->multicast_lock);
680 if (!netif_running(br->dev) ||
681 (port && port->state == BR_STATE_DISABLED))
682 goto out;
683
684 mp = br_multicast_new_group(br, port, group);
685 err = PTR_ERR(mp);
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000686 if (IS_ERR(mp))
Herbert Xueb1d1642010-02-27 19:41:45 +0000687 goto err;
688
689 if (!port) {
Herbert Xu8a870172011-02-12 01:05:42 -0800690 mp->mglist = true;
Linus Lüssing454594f2013-10-20 00:58:57 +0200691 mod_timer(&mp->timer, now + br->multicast_membership_interval);
Herbert Xueb1d1642010-02-27 19:41:45 +0000692 goto out;
693 }
694
Eric Dumazete8051682010-11-15 06:38:10 +0000695 for (pp = &mp->ports;
696 (p = mlock_dereference(*pp, br)) != NULL;
697 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000698 if (p->port == port)
Linus Lüssing454594f2013-10-20 00:58:57 +0200699 goto found;
Herbert Xueb1d1642010-02-27 19:41:45 +0000700 if ((unsigned long)p->port < (unsigned long)port)
701 break;
702 }
703
Elad Raz9d06b6d2016-02-03 09:57:05 +0100704 p = br_multicast_new_port_group(port, group, *pp, 0);
Herbert Xueb1d1642010-02-27 19:41:45 +0000705 if (unlikely(!p))
706 goto err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000707 rcu_assign_pointer(*pp, p);
Elad Raz9e8430f2016-02-03 09:57:06 +0100708 br_mdb_notify(br->dev, p, RTM_NEWMDB);
Herbert Xueb1d1642010-02-27 19:41:45 +0000709
Linus Lüssing454594f2013-10-20 00:58:57 +0200710found:
711 mod_timer(&p->timer, now + br->multicast_membership_interval);
Herbert Xueb1d1642010-02-27 19:41:45 +0000712out:
713 err = 0;
714
715err:
716 spin_unlock(&br->multicast_lock);
717 return err;
718}
719
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900720static int br_ip4_multicast_add_group(struct net_bridge *br,
721 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000722 __be32 group,
723 __u16 vid)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900724{
725 struct br_ip br_group;
726
727 if (ipv4_is_local_multicast(group))
728 return 0;
729
730 br_group.u.ip4 = group;
731 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000732 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900733
734 return br_multicast_add_group(br, port, &br_group);
735}
736
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000737#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900738static int br_ip6_multicast_add_group(struct net_bridge *br,
739 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000740 const struct in6_addr *group,
741 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900742{
743 struct br_ip br_group;
744
Linus Lüssing3c3769e2013-09-04 02:13:39 +0200745 if (ipv6_addr_is_ll_all_nodes(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900746 return 0;
747
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000748 br_group.u.ip6 = *group;
Linus Lüssing9cc6e0c2011-02-15 13:19:17 +0000749 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000750 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900751
752 return br_multicast_add_group(br, port, &br_group);
753}
754#endif
755
Herbert Xueb1d1642010-02-27 19:41:45 +0000756static void br_multicast_router_expired(unsigned long data)
757{
758 struct net_bridge_port *port = (void *)data;
759 struct net_bridge *br = port->br;
760
761 spin_lock(&br->multicast_lock);
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +0100762 if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
763 port->multicast_router == MDB_RTR_TYPE_PERM ||
Herbert Xueb1d1642010-02-27 19:41:45 +0000764 timer_pending(&port->multicast_router_timer) ||
765 hlist_unhashed(&port->rlist))
766 goto out;
767
768 hlist_del_init_rcu(&port->rlist);
Satish Ashok949f1e32015-07-23 05:00:53 -0700769 br_rtr_notify(br->dev, port, RTM_DELMDB);
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +0100770 /* Don't allow timer refresh if the router expired */
771 if (port->multicast_router == MDB_RTR_TYPE_TEMP)
772 port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Herbert Xueb1d1642010-02-27 19:41:45 +0000773
774out:
775 spin_unlock(&br->multicast_lock);
776}
777
778static void br_multicast_local_router_expired(unsigned long data)
779{
780}
781
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200782static void br_multicast_querier_expired(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +0200783 struct bridge_mcast_own_query *query)
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000784{
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000785 spin_lock(&br->multicast_lock);
786 if (!netif_running(br->dev) || br->multicast_disabled)
787 goto out;
788
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200789 br_multicast_start_querier(br, query);
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000790
791out:
792 spin_unlock(&br->multicast_lock);
793}
794
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200795static void br_ip4_multicast_querier_expired(unsigned long data)
796{
797 struct net_bridge *br = (void *)data;
798
Linus Lüssing90010b32014-06-07 18:26:26 +0200799 br_multicast_querier_expired(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200800}
801
802#if IS_ENABLED(CONFIG_IPV6)
803static void br_ip6_multicast_querier_expired(unsigned long data)
804{
805 struct net_bridge *br = (void *)data;
806
Linus Lüssing90010b32014-06-07 18:26:26 +0200807 br_multicast_querier_expired(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200808}
809#endif
810
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200811static void br_multicast_select_own_querier(struct net_bridge *br,
812 struct br_ip *ip,
813 struct sk_buff *skb)
814{
815 if (ip->proto == htons(ETH_P_IP))
816 br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr;
817#if IS_ENABLED(CONFIG_IPV6)
818 else
819 br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr;
820#endif
821}
822
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900823static void __br_multicast_send_query(struct net_bridge *br,
824 struct net_bridge_port *port,
825 struct br_ip *ip)
Herbert Xueb1d1642010-02-27 19:41:45 +0000826{
Herbert Xueb1d1642010-02-27 19:41:45 +0000827 struct sk_buff *skb;
828
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900829 skb = br_multicast_alloc_query(br, ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000830 if (!skb)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900831 return;
Herbert Xueb1d1642010-02-27 19:41:45 +0000832
833 if (port) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000834 skb->dev = port->dev;
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500835 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
836 dev_net(port->dev), NULL, skb, NULL, skb->dev,
Linus Lüssingf0b4eec2014-11-17 12:20:28 +0100837 br_dev_queue_push_xmit);
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200838 } else {
839 br_multicast_select_own_querier(br, ip, skb);
Herbert Xueb1d1642010-02-27 19:41:45 +0000840 netif_rx(skb);
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200841 }
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900842}
Herbert Xueb1d1642010-02-27 19:41:45 +0000843
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900844static void br_multicast_send_query(struct net_bridge *br,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200845 struct net_bridge_port *port,
Linus Lüssing90010b32014-06-07 18:26:26 +0200846 struct bridge_mcast_own_query *own_query)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900847{
848 unsigned long time;
849 struct br_ip br_group;
Linus Lüssing90010b32014-06-07 18:26:26 +0200850 struct bridge_mcast_other_query *other_query = NULL;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900851
852 if (!netif_running(br->dev) || br->multicast_disabled ||
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200853 !br->multicast_querier)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900854 return;
855
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900856 memset(&br_group.u, 0, sizeof(br_group.u));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900857
Linus Lüssing90010b32014-06-07 18:26:26 +0200858 if (port ? (own_query == &port->ip4_own_query) :
859 (own_query == &br->ip4_own_query)) {
860 other_query = &br->ip4_other_query;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200861 br_group.proto = htons(ETH_P_IP);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000862#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200863 } else {
Linus Lüssing90010b32014-06-07 18:26:26 +0200864 other_query = &br->ip6_other_query;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200865 br_group.proto = htons(ETH_P_IPV6);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900866#endif
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200867 }
868
Linus Lüssing90010b32014-06-07 18:26:26 +0200869 if (!other_query || timer_pending(&other_query->timer))
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200870 return;
871
872 __br_multicast_send_query(br, port, &br_group);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900873
Herbert Xueb1d1642010-02-27 19:41:45 +0000874 time = jiffies;
Linus Lüssing90010b32014-06-07 18:26:26 +0200875 time += own_query->startup_sent < br->multicast_startup_query_count ?
Herbert Xueb1d1642010-02-27 19:41:45 +0000876 br->multicast_startup_query_interval :
877 br->multicast_query_interval;
Linus Lüssing90010b32014-06-07 18:26:26 +0200878 mod_timer(&own_query->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +0000879}
880
Linus Lüssing90010b32014-06-07 18:26:26 +0200881static void
882br_multicast_port_query_expired(struct net_bridge_port *port,
883 struct bridge_mcast_own_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +0000884{
Herbert Xueb1d1642010-02-27 19:41:45 +0000885 struct net_bridge *br = port->br;
886
887 spin_lock(&br->multicast_lock);
Dan Carpenter02a780c2010-03-06 01:14:09 +0000888 if (port->state == BR_STATE_DISABLED ||
889 port->state == BR_STATE_BLOCKING)
Herbert Xueb1d1642010-02-27 19:41:45 +0000890 goto out;
891
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200892 if (query->startup_sent < br->multicast_startup_query_count)
893 query->startup_sent++;
Herbert Xueb1d1642010-02-27 19:41:45 +0000894
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200895 br_multicast_send_query(port->br, port, query);
Herbert Xueb1d1642010-02-27 19:41:45 +0000896
897out:
898 spin_unlock(&br->multicast_lock);
899}
900
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200901static void br_ip4_multicast_port_query_expired(unsigned long data)
902{
903 struct net_bridge_port *port = (void *)data;
904
Linus Lüssing90010b32014-06-07 18:26:26 +0200905 br_multicast_port_query_expired(port, &port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200906}
907
908#if IS_ENABLED(CONFIG_IPV6)
909static void br_ip6_multicast_port_query_expired(unsigned long data)
910{
911 struct net_bridge_port *port = (void *)data;
912
Linus Lüssing90010b32014-06-07 18:26:26 +0200913 br_multicast_port_query_expired(port, &port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200914}
915#endif
916
Herbert Xueb1d1642010-02-27 19:41:45 +0000917void br_multicast_add_port(struct net_bridge_port *port)
918{
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +0100919 port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Herbert Xueb1d1642010-02-27 19:41:45 +0000920
921 setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
922 (unsigned long)port);
Linus Lüssing90010b32014-06-07 18:26:26 +0200923 setup_timer(&port->ip4_own_query.timer,
924 br_ip4_multicast_port_query_expired, (unsigned long)port);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200925#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +0200926 setup_timer(&port->ip6_own_query.timer,
927 br_ip6_multicast_port_query_expired, (unsigned long)port);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200928#endif
Herbert Xueb1d1642010-02-27 19:41:45 +0000929}
930
931void br_multicast_del_port(struct net_bridge_port *port)
932{
Satish Ashoke10177a2015-07-15 07:16:51 -0700933 struct net_bridge *br = port->br;
934 struct net_bridge_port_group *pg;
935 struct hlist_node *n;
936
937 /* Take care of the remaining groups, only perm ones should be left */
938 spin_lock_bh(&br->multicast_lock);
939 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
940 br_multicast_del_pg(br, pg);
941 spin_unlock_bh(&br->multicast_lock);
Herbert Xueb1d1642010-02-27 19:41:45 +0000942 del_timer_sync(&port->multicast_router_timer);
943}
944
Linus Lüssing90010b32014-06-07 18:26:26 +0200945static void br_multicast_enable(struct bridge_mcast_own_query *query)
Herbert Xu561f1102010-02-27 19:41:50 +0000946{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200947 query->startup_sent = 0;
Herbert Xu561f1102010-02-27 19:41:50 +0000948
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200949 if (try_to_del_timer_sync(&query->timer) >= 0 ||
950 del_timer(&query->timer))
951 mod_timer(&query->timer, jiffies);
Herbert Xu561f1102010-02-27 19:41:50 +0000952}
953
Herbert Xueb1d1642010-02-27 19:41:45 +0000954void br_multicast_enable_port(struct net_bridge_port *port)
955{
956 struct net_bridge *br = port->br;
957
958 spin_lock(&br->multicast_lock);
959 if (br->multicast_disabled || !netif_running(br->dev))
960 goto out;
961
Linus Lüssing90010b32014-06-07 18:26:26 +0200962 br_multicast_enable(&port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200963#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +0200964 br_multicast_enable(&port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200965#endif
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +0100966 if (port->multicast_router == MDB_RTR_TYPE_PERM &&
967 hlist_unhashed(&port->rlist))
Satish Ashok754bc542015-06-19 01:22:57 -0700968 br_multicast_add_router(br, port);
Herbert Xueb1d1642010-02-27 19:41:45 +0000969
970out:
971 spin_unlock(&br->multicast_lock);
972}
973
974void br_multicast_disable_port(struct net_bridge_port *port)
975{
976 struct net_bridge *br = port->br;
977 struct net_bridge_port_group *pg;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800978 struct hlist_node *n;
Herbert Xueb1d1642010-02-27 19:41:45 +0000979
980 spin_lock(&br->multicast_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800981 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
Elad Raz9d06b6d2016-02-03 09:57:05 +0100982 if (!(pg->flags & MDB_PG_FLAGS_PERMANENT))
Satish Ashoke10177a2015-07-15 07:16:51 -0700983 br_multicast_del_pg(br, pg);
Herbert Xueb1d1642010-02-27 19:41:45 +0000984
Satish Ashok949f1e32015-07-23 05:00:53 -0700985 if (!hlist_unhashed(&port->rlist)) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000986 hlist_del_init_rcu(&port->rlist);
Satish Ashok949f1e32015-07-23 05:00:53 -0700987 br_rtr_notify(br->dev, port, RTM_DELMDB);
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +0100988 /* Don't allow timer refresh if disabling */
989 if (port->multicast_router == MDB_RTR_TYPE_TEMP)
990 port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Satish Ashok949f1e32015-07-23 05:00:53 -0700991 }
Herbert Xueb1d1642010-02-27 19:41:45 +0000992 del_timer(&port->multicast_router_timer);
Linus Lüssing90010b32014-06-07 18:26:26 +0200993 del_timer(&port->ip4_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200994#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +0200995 del_timer(&port->ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200996#endif
Herbert Xueb1d1642010-02-27 19:41:45 +0000997 spin_unlock(&br->multicast_lock);
998}
999
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001000static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
1001 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001002 struct sk_buff *skb,
1003 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001004{
1005 struct igmpv3_report *ih;
1006 struct igmpv3_grec *grec;
1007 int i;
1008 int len;
1009 int num;
1010 int type;
1011 int err = 0;
1012 __be32 group;
1013
Herbert Xueb1d1642010-02-27 19:41:45 +00001014 ih = igmpv3_report_hdr(skb);
1015 num = ntohs(ih->ngrec);
Linus Lüssingc2d4fbd2015-09-11 18:39:48 +02001016 len = skb_transport_offset(skb) + sizeof(*ih);
Herbert Xueb1d1642010-02-27 19:41:45 +00001017
1018 for (i = 0; i < num; i++) {
1019 len += sizeof(*grec);
1020 if (!pskb_may_pull(skb, len))
1021 return -EINVAL;
1022
Herbert Xufd218cf2010-04-07 21:20:47 -07001023 grec = (void *)(skb->data + len - sizeof(*grec));
Herbert Xueb1d1642010-02-27 19:41:45 +00001024 group = grec->grec_mca;
1025 type = grec->grec_type;
1026
Eric Dumazet8eabf952010-04-20 03:20:05 +00001027 len += ntohs(grec->grec_nsrcs) * 4;
Herbert Xueb1d1642010-02-27 19:41:45 +00001028 if (!pskb_may_pull(skb, len))
1029 return -EINVAL;
1030
1031 /* We treat this as an IGMPv2 report for now. */
1032 switch (type) {
1033 case IGMPV3_MODE_IS_INCLUDE:
1034 case IGMPV3_MODE_IS_EXCLUDE:
1035 case IGMPV3_CHANGE_TO_INCLUDE:
1036 case IGMPV3_CHANGE_TO_EXCLUDE:
1037 case IGMPV3_ALLOW_NEW_SOURCES:
1038 case IGMPV3_BLOCK_OLD_SOURCES:
1039 break;
1040
1041 default:
1042 continue;
1043 }
1044
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001045 if ((type == IGMPV3_CHANGE_TO_INCLUDE ||
1046 type == IGMPV3_MODE_IS_INCLUDE) &&
1047 ntohs(grec->grec_nsrcs) == 0) {
1048 br_ip4_multicast_leave_group(br, port, group, vid);
1049 } else {
1050 err = br_ip4_multicast_add_group(br, port, group, vid);
1051 if (err)
1052 break;
1053 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001054 }
1055
1056 return err;
1057}
1058
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001059#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001060static int br_ip6_multicast_mld2_report(struct net_bridge *br,
1061 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001062 struct sk_buff *skb,
1063 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001064{
1065 struct icmp6hdr *icmp6h;
1066 struct mld2_grec *grec;
1067 int i;
1068 int len;
1069 int num;
1070 int err = 0;
1071
1072 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
1073 return -EINVAL;
1074
1075 icmp6h = icmp6_hdr(skb);
1076 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
Linus Lüssingc2d4fbd2015-09-11 18:39:48 +02001077 len = skb_transport_offset(skb) + sizeof(*icmp6h);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001078
1079 for (i = 0; i < num; i++) {
1080 __be16 *nsrcs, _nsrcs;
1081
1082 nsrcs = skb_header_pointer(skb,
1083 len + offsetof(struct mld2_grec,
Linus Lüssing649e9842011-02-15 13:19:18 +00001084 grec_nsrcs),
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001085 sizeof(_nsrcs), &_nsrcs);
1086 if (!nsrcs)
1087 return -EINVAL;
1088
1089 if (!pskb_may_pull(skb,
1090 len + sizeof(*grec) +
Linus Lüssingd41db9f2011-02-15 13:19:19 +00001091 sizeof(struct in6_addr) * ntohs(*nsrcs)))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001092 return -EINVAL;
1093
1094 grec = (struct mld2_grec *)(skb->data + len);
Linus Lüssingd41db9f2011-02-15 13:19:19 +00001095 len += sizeof(*grec) +
1096 sizeof(struct in6_addr) * ntohs(*nsrcs);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001097
1098 /* We treat these as MLDv1 reports for now. */
1099 switch (grec->grec_type) {
1100 case MLD2_MODE_IS_INCLUDE:
1101 case MLD2_MODE_IS_EXCLUDE:
1102 case MLD2_CHANGE_TO_INCLUDE:
1103 case MLD2_CHANGE_TO_EXCLUDE:
1104 case MLD2_ALLOW_NEW_SOURCES:
1105 case MLD2_BLOCK_OLD_SOURCES:
1106 break;
1107
1108 default:
1109 continue;
1110 }
1111
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001112 if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE ||
1113 grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
1114 ntohs(*nsrcs) == 0) {
1115 br_ip6_multicast_leave_group(br, port, &grec->grec_mca,
1116 vid);
1117 } else {
1118 err = br_ip6_multicast_add_group(br, port,
1119 &grec->grec_mca, vid);
1120 if (!err)
1121 break;
1122 }
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001123 }
1124
1125 return err;
1126}
1127#endif
1128
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001129static bool br_ip4_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 __be32 saddr)
1132{
1133 if (!timer_pending(&br->ip4_own_query.timer) &&
1134 !timer_pending(&br->ip4_other_query.timer))
1135 goto update;
1136
1137 if (!br->ip4_querier.addr.u.ip4)
1138 goto update;
1139
1140 if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4))
1141 goto update;
1142
1143 return false;
1144
1145update:
1146 br->ip4_querier.addr.u.ip4 = saddr;
1147
Linus Lüssing2cd41432014-06-07 18:26:29 +02001148 /* update protected by general multicast_lock by caller */
1149 rcu_assign_pointer(br->ip4_querier.port, port);
1150
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001151 return true;
1152}
1153
1154#if IS_ENABLED(CONFIG_IPV6)
1155static bool br_ip6_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001156 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001157 struct in6_addr *saddr)
1158{
1159 if (!timer_pending(&br->ip6_own_query.timer) &&
1160 !timer_pending(&br->ip6_other_query.timer))
1161 goto update;
1162
1163 if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0)
1164 goto update;
1165
1166 return false;
1167
1168update:
1169 br->ip6_querier.addr.u.ip6 = *saddr;
1170
Linus Lüssing2cd41432014-06-07 18:26:29 +02001171 /* update protected by general multicast_lock by caller */
1172 rcu_assign_pointer(br->ip6_querier.port, port);
1173
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001174 return true;
1175}
1176#endif
1177
1178static bool br_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001179 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001180 struct br_ip *saddr)
1181{
1182 switch (saddr->proto) {
1183 case htons(ETH_P_IP):
Linus Lüssing2cd41432014-06-07 18:26:29 +02001184 return br_ip4_multicast_select_querier(br, port, saddr->u.ip4);
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001185#if IS_ENABLED(CONFIG_IPV6)
1186 case htons(ETH_P_IPV6):
Linus Lüssing2cd41432014-06-07 18:26:29 +02001187 return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6);
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001188#endif
1189 }
1190
1191 return false;
1192}
1193
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001194static void
Linus Lüssing90010b32014-06-07 18:26:26 +02001195br_multicast_update_query_timer(struct net_bridge *br,
1196 struct bridge_mcast_other_query *query,
1197 unsigned long max_delay)
Linus Lüssingb00589a2013-08-01 01:06:20 +02001198{
Linus Lüssing90010b32014-06-07 18:26:26 +02001199 if (!timer_pending(&query->timer))
1200 query->delay_time = jiffies + max_delay;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001201
Linus Lüssing90010b32014-06-07 18:26:26 +02001202 mod_timer(&query->timer, jiffies + br->multicast_querier_interval);
Linus Lüssingb00589a2013-08-01 01:06:20 +02001203}
1204
stephen hemminger7e80c122010-04-27 15:01:04 +00001205/*
Cong Wang7c776022013-06-21 15:37:25 +08001206 * Add port to router_list
stephen hemminger7e80c122010-04-27 15:01:04 +00001207 * list is maintained ordered by pointer value
1208 * and locked by br->multicast_lock and RCU
1209 */
Herbert Xu0909e112010-02-27 19:41:49 +00001210static void br_multicast_add_router(struct net_bridge *br,
1211 struct net_bridge_port *port)
1212{
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001213 struct net_bridge_port *p;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001214 struct hlist_node *slot = NULL;
Herbert Xu0909e112010-02-27 19:41:49 +00001215
Nikolay Aleksandrov1a040ea2015-06-09 10:23:57 -07001216 if (!hlist_unhashed(&port->rlist))
1217 return;
1218
Sasha Levinb67bfe02013-02-27 17:06:00 -08001219 hlist_for_each_entry(p, &br->router_list, rlist) {
stephen hemminger7e80c122010-04-27 15:01:04 +00001220 if ((unsigned long) port >= (unsigned long) p)
1221 break;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001222 slot = &p->rlist;
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001223 }
Herbert Xu0909e112010-02-27 19:41:49 +00001224
stephen hemminger7e80c122010-04-27 15:01:04 +00001225 if (slot)
Ken Helias1d023282014-08-06 16:09:16 -07001226 hlist_add_behind_rcu(&port->rlist, slot);
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001227 else
1228 hlist_add_head_rcu(&port->rlist, &br->router_list);
Satish Ashok949f1e32015-07-23 05:00:53 -07001229 br_rtr_notify(br->dev, port, RTM_NEWMDB);
Herbert Xu0909e112010-02-27 19:41:49 +00001230}
1231
Herbert Xueb1d1642010-02-27 19:41:45 +00001232static void br_multicast_mark_router(struct net_bridge *br,
1233 struct net_bridge_port *port)
1234{
1235 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +00001236
1237 if (!port) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001238 if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY)
Herbert Xueb1d1642010-02-27 19:41:45 +00001239 mod_timer(&br->multicast_router_timer,
1240 now + br->multicast_querier_interval);
1241 return;
1242 }
1243
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001244 if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
1245 port->multicast_router == MDB_RTR_TYPE_PERM)
Herbert Xueb1d1642010-02-27 19:41:45 +00001246 return;
1247
Herbert Xu0909e112010-02-27 19:41:49 +00001248 br_multicast_add_router(br, port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001249
Herbert Xueb1d1642010-02-27 19:41:45 +00001250 mod_timer(&port->multicast_router_timer,
1251 now + br->multicast_querier_interval);
1252}
1253
1254static void br_multicast_query_received(struct net_bridge *br,
1255 struct net_bridge_port *port,
Linus Lüssing90010b32014-06-07 18:26:26 +02001256 struct bridge_mcast_other_query *query,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001257 struct br_ip *saddr,
Linus Lüssingb00589a2013-08-01 01:06:20 +02001258 unsigned long max_delay)
Herbert Xueb1d1642010-02-27 19:41:45 +00001259{
Linus Lüssing2cd41432014-06-07 18:26:29 +02001260 if (!br_multicast_select_querier(br, port, saddr))
Herbert Xueb1d1642010-02-27 19:41:45 +00001261 return;
1262
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001263 br_multicast_update_query_timer(br, query, max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001264 br_multicast_mark_router(br, port);
1265}
1266
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001267static int br_ip4_multicast_query(struct net_bridge *br,
1268 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001269 struct sk_buff *skb,
1270 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001271{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001272 const struct iphdr *iph = ip_hdr(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001273 struct igmphdr *ih = igmp_hdr(skb);
1274 struct net_bridge_mdb_entry *mp;
1275 struct igmpv3_query *ih3;
1276 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +00001277 struct net_bridge_port_group __rcu **pp;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001278 struct br_ip saddr;
Herbert Xueb1d1642010-02-27 19:41:45 +00001279 unsigned long max_delay;
1280 unsigned long now = jiffies;
1281 __be32 group;
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001282 int err = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001283
1284 spin_lock(&br->multicast_lock);
1285 if (!netif_running(br->dev) ||
1286 (port && port->state == BR_STATE_DISABLED))
1287 goto out;
1288
Herbert Xueb1d1642010-02-27 19:41:45 +00001289 group = ih->group;
1290
1291 if (skb->len == sizeof(*ih)) {
1292 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1293
1294 if (!max_delay) {
1295 max_delay = 10 * HZ;
1296 group = 0;
1297 }
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001298 } else if (skb->len >= sizeof(*ih3)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001299 ih3 = igmpv3_query_hdr(skb);
1300 if (ih3->nsrcs)
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001301 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001302
YOSHIFUJI Hideaki / 吉藤英明0ba8c9e2010-03-15 19:27:00 +00001303 max_delay = ih3->code ?
1304 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001305 } else {
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001306 goto out;
1307 }
1308
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001309 if (!group) {
1310 saddr.proto = htons(ETH_P_IP);
1311 saddr.u.ip4 = iph->saddr;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001312
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001313 br_multicast_query_received(br, port, &br->ip4_other_query,
1314 &saddr, max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001315 goto out;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001316 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001317
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001318 mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001319 if (!mp)
1320 goto out;
1321
1322 max_delay *= br->multicast_last_member_count;
1323
Herbert Xu8a870172011-02-12 01:05:42 -08001324 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001325 (timer_pending(&mp->timer) ?
1326 time_after(mp->timer.expires, now + max_delay) :
1327 try_to_del_timer_sync(&mp->timer) >= 0))
1328 mod_timer(&mp->timer, now + max_delay);
1329
Eric Dumazete8051682010-11-15 06:38:10 +00001330 for (pp = &mp->ports;
1331 (p = mlock_dereference(*pp, br)) != NULL;
1332 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001333 if (timer_pending(&p->timer) ?
1334 time_after(p->timer.expires, now + max_delay) :
1335 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001336 mod_timer(&p->timer, now + max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001337 }
1338
1339out:
1340 spin_unlock(&br->multicast_lock);
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001341 return err;
Herbert Xueb1d1642010-02-27 19:41:45 +00001342}
1343
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001344#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001345static int br_ip6_multicast_query(struct net_bridge *br,
1346 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001347 struct sk_buff *skb,
1348 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001349{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001350 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
stephen hemmingereca2a432012-12-13 06:51:28 +00001351 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001352 struct net_bridge_mdb_entry *mp;
1353 struct mld2_query *mld2q;
Eric Dumazete8051682010-11-15 06:38:10 +00001354 struct net_bridge_port_group *p;
1355 struct net_bridge_port_group __rcu **pp;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001356 struct br_ip saddr;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001357 unsigned long max_delay;
1358 unsigned long now = jiffies;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001359 const struct in6_addr *group = NULL;
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001360 bool is_general_query;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001361 int err = 0;
1362
1363 spin_lock(&br->multicast_lock);
1364 if (!netif_running(br->dev) ||
1365 (port && port->state == BR_STATE_DISABLED))
1366 goto out;
1367
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001368 if (skb->len == sizeof(*mld)) {
1369 if (!pskb_may_pull(skb, sizeof(*mld))) {
1370 err = -EINVAL;
1371 goto out;
1372 }
1373 mld = (struct mld_msg *) icmp6_hdr(skb);
Li RongQing47152132012-07-09 23:56:12 +00001374 max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001375 if (max_delay)
1376 group = &mld->mld_mca;
Linus Lüssing248ba8e2013-08-06 00:32:05 +02001377 } else {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001378 if (!pskb_may_pull(skb, sizeof(*mld2q))) {
1379 err = -EINVAL;
1380 goto out;
1381 }
1382 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1383 if (!mld2q->mld2q_nsrcs)
1384 group = &mld2q->mld2q_mca;
Daniel Borkmanne3f5b172013-09-04 00:19:39 +02001385
1386 max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001387 }
1388
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001389 is_general_query = group && ipv6_addr_any(group);
1390
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001391 if (is_general_query) {
1392 saddr.proto = htons(ETH_P_IPV6);
1393 saddr.u.ip6 = ip6h->saddr;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001394
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001395 br_multicast_query_received(br, port, &br->ip6_other_query,
1396 &saddr, max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001397 goto out;
Linus Lüssing6c03ee82014-06-12 01:41:23 +02001398 } else if (!group) {
1399 goto out;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001400 }
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001401
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001402 mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001403 if (!mp)
1404 goto out;
1405
1406 max_delay *= br->multicast_last_member_count;
Herbert Xu8a870172011-02-12 01:05:42 -08001407 if (mp->mglist &&
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001408 (timer_pending(&mp->timer) ?
1409 time_after(mp->timer.expires, now + max_delay) :
1410 try_to_del_timer_sync(&mp->timer) >= 0))
1411 mod_timer(&mp->timer, now + max_delay);
1412
Eric Dumazete8051682010-11-15 06:38:10 +00001413 for (pp = &mp->ports;
1414 (p = mlock_dereference(*pp, br)) != NULL;
1415 pp = &p->next) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001416 if (timer_pending(&p->timer) ?
1417 time_after(p->timer.expires, now + max_delay) :
1418 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001419 mod_timer(&p->timer, now + max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001420 }
1421
1422out:
1423 spin_unlock(&br->multicast_lock);
1424 return err;
1425}
1426#endif
1427
Linus Lüssing90010b32014-06-07 18:26:26 +02001428static void
1429br_multicast_leave_group(struct net_bridge *br,
1430 struct net_bridge_port *port,
1431 struct br_ip *group,
1432 struct bridge_mcast_other_query *other_query,
1433 struct bridge_mcast_own_query *own_query)
Herbert Xueb1d1642010-02-27 19:41:45 +00001434{
1435 struct net_bridge_mdb_htable *mdb;
1436 struct net_bridge_mdb_entry *mp;
1437 struct net_bridge_port_group *p;
1438 unsigned long now;
1439 unsigned long time;
1440
Herbert Xueb1d1642010-02-27 19:41:45 +00001441 spin_lock(&br->multicast_lock);
1442 if (!netif_running(br->dev) ||
Satish Ashok544586f2015-07-28 03:28:27 -07001443 (port && port->state == BR_STATE_DISABLED))
Herbert Xueb1d1642010-02-27 19:41:45 +00001444 goto out;
1445
Eric Dumazete8051682010-11-15 06:38:10 +00001446 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001447 mp = br_mdb_ip_get(mdb, group);
1448 if (!mp)
1449 goto out;
1450
Satish Ashok544586f2015-07-28 03:28:27 -07001451 if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
1452 struct net_bridge_port_group __rcu **pp;
1453
1454 for (pp = &mp->ports;
1455 (p = mlock_dereference(*pp, br)) != NULL;
1456 pp = &p->next) {
1457 if (p->port != port)
1458 continue;
1459
1460 rcu_assign_pointer(*pp, p->next);
1461 hlist_del_init(&p->mglist);
1462 del_timer(&p->timer);
1463 call_rcu_bh(&p->rcu, br_multicast_free_pg);
Elad Raz9e8430f2016-02-03 09:57:06 +01001464 br_mdb_notify(br->dev, p, RTM_DELMDB);
Satish Ashok544586f2015-07-28 03:28:27 -07001465
1466 if (!mp->ports && !mp->mglist &&
1467 netif_running(br->dev))
1468 mod_timer(&mp->timer, jiffies);
1469 }
1470 goto out;
1471 }
1472
1473 if (timer_pending(&other_query->timer))
1474 goto out;
1475
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001476 if (br->multicast_querier) {
Cong Wang6b7df112013-05-21 21:52:56 +00001477 __br_multicast_send_query(br, port, &mp->addr);
1478
1479 time = jiffies + br->multicast_last_member_count *
1480 br->multicast_last_member_interval;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001481
Linus Lüssing90010b32014-06-07 18:26:26 +02001482 mod_timer(&own_query->timer, time);
Cong Wang6b7df112013-05-21 21:52:56 +00001483
1484 for (p = mlock_dereference(mp->ports, br);
1485 p != NULL;
1486 p = mlock_dereference(p->next, br)) {
1487 if (p->port != port)
1488 continue;
1489
1490 if (!hlist_unhashed(&p->mglist) &&
1491 (timer_pending(&p->timer) ?
1492 time_after(p->timer.expires, time) :
1493 try_to_del_timer_sync(&p->timer) >= 0)) {
1494 mod_timer(&p->timer, time);
1495 }
1496
1497 break;
1498 }
1499 }
1500
Herbert Xueb1d1642010-02-27 19:41:45 +00001501 now = jiffies;
1502 time = now + br->multicast_last_member_count *
1503 br->multicast_last_member_interval;
1504
1505 if (!port) {
Linus Lüssing454594f2013-10-20 00:58:57 +02001506 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001507 (timer_pending(&mp->timer) ?
1508 time_after(mp->timer.expires, time) :
1509 try_to_del_timer_sync(&mp->timer) >= 0)) {
1510 mod_timer(&mp->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +00001511 }
Linus Lüssing454594f2013-10-20 00:58:57 +02001512
1513 goto out;
1514 }
1515
1516 for (p = mlock_dereference(mp->ports, br);
1517 p != NULL;
1518 p = mlock_dereference(p->next, br)) {
1519 if (p->port != port)
1520 continue;
1521
1522 if (!hlist_unhashed(&p->mglist) &&
1523 (timer_pending(&p->timer) ?
1524 time_after(p->timer.expires, time) :
1525 try_to_del_timer_sync(&p->timer) >= 0)) {
1526 mod_timer(&p->timer, time);
1527 }
1528
1529 break;
Herbert Xueb1d1642010-02-27 19:41:45 +00001530 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001531out:
1532 spin_unlock(&br->multicast_lock);
1533}
1534
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001535static void br_ip4_multicast_leave_group(struct net_bridge *br,
1536 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001537 __be32 group,
1538 __u16 vid)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001539{
1540 struct br_ip br_group;
Linus Lüssing90010b32014-06-07 18:26:26 +02001541 struct bridge_mcast_own_query *own_query;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001542
1543 if (ipv4_is_local_multicast(group))
1544 return;
1545
Linus Lüssing90010b32014-06-07 18:26:26 +02001546 own_query = port ? &port->ip4_own_query : &br->ip4_own_query;
1547
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001548 br_group.u.ip4 = group;
1549 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001550 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001551
Linus Lüssing90010b32014-06-07 18:26:26 +02001552 br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
1553 own_query);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001554}
1555
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001556#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001557static void br_ip6_multicast_leave_group(struct net_bridge *br,
1558 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001559 const struct in6_addr *group,
1560 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001561{
1562 struct br_ip br_group;
Linus Lüssing90010b32014-06-07 18:26:26 +02001563 struct bridge_mcast_own_query *own_query;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001564
Linus Lüssing3c3769e2013-09-04 02:13:39 +02001565 if (ipv6_addr_is_ll_all_nodes(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001566 return;
1567
Linus Lüssing90010b32014-06-07 18:26:26 +02001568 own_query = port ? &port->ip6_own_query : &br->ip6_own_query;
1569
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001570 br_group.u.ip6 = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001571 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001572 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001573
Linus Lüssing90010b32014-06-07 18:26:26 +02001574 br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
1575 own_query);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001576}
1577#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001578
Herbert Xueb1d1642010-02-27 19:41:45 +00001579static int br_multicast_ipv4_rcv(struct net_bridge *br,
1580 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001581 struct sk_buff *skb,
1582 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001583{
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001584 struct sk_buff *skb_trimmed = NULL;
Herbert Xueb1d1642010-02-27 19:41:45 +00001585 struct igmphdr *ih;
Herbert Xueb1d1642010-02-27 19:41:45 +00001586 int err;
1587
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001588 err = ip_mc_check_igmp(skb, &skb_trimmed);
Herbert Xueb1d1642010-02-27 19:41:45 +00001589
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001590 if (err == -ENOMSG) {
1591 if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr))
Herbert Xubd4265f2011-06-23 02:39:12 +00001592 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Herbert Xueb1d1642010-02-27 19:41:45 +00001593 return 0;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001594 } else if (err < 0) {
1595 return err;
Herbert Xubd4265f2011-06-23 02:39:12 +00001596 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001597
Herbert Xueb1d1642010-02-27 19:41:45 +00001598 BR_INPUT_SKB_CB(skb)->igmp = 1;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001599 ih = igmp_hdr(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001600
1601 switch (ih->type) {
1602 case IGMP_HOST_MEMBERSHIP_REPORT:
1603 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Fernando Luis Vázquez Cao62b2bcb2011-06-13 15:04:43 +00001604 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001605 err = br_ip4_multicast_add_group(br, port, ih->group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001606 break;
1607 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001608 err = br_ip4_multicast_igmp3_report(br, port, skb_trimmed, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001609 break;
1610 case IGMP_HOST_MEMBERSHIP_QUERY:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001611 err = br_ip4_multicast_query(br, port, skb_trimmed, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001612 break;
1613 case IGMP_HOST_LEAVE_MESSAGE:
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001614 br_ip4_multicast_leave_group(br, port, ih->group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001615 break;
1616 }
1617
Linus Lüssinga5169932015-08-13 05:54:07 +02001618 if (skb_trimmed && skb_trimmed != skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001619 kfree_skb(skb_trimmed);
1620
Herbert Xueb1d1642010-02-27 19:41:45 +00001621 return err;
1622}
1623
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001624#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001625static int br_multicast_ipv6_rcv(struct net_bridge *br,
1626 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001627 struct sk_buff *skb,
1628 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001629{
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001630 struct sk_buff *skb_trimmed = NULL;
1631 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001632 int err;
1633
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001634 err = ipv6_mc_check_mld(skb, &skb_trimmed);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001635
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001636 if (err == -ENOMSG) {
1637 if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
1638 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Linus Lüssing8fad9c392013-09-04 02:13:38 +02001639 return 0;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001640 } else if (err < 0) {
1641 return err;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001642 }
1643
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001644 BR_INPUT_SKB_CB(skb)->igmp = 1;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001645 mld = (struct mld_msg *)skb_transport_header(skb);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001646
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001647 switch (mld->mld_type) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001648 case ICMPV6_MGM_REPORT:
Fernando Luis Vázquez Caofc2af6c2011-06-13 15:06:58 +00001649 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001650 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001651 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001652 case ICMPV6_MLD2_REPORT:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001653 err = br_ip6_multicast_mld2_report(br, port, skb_trimmed, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001654 break;
1655 case ICMPV6_MGM_QUERY:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001656 err = br_ip6_multicast_query(br, port, skb_trimmed, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001657 break;
1658 case ICMPV6_MGM_REDUCTION:
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001659 br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001660 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001661 }
1662
Linus Lüssinga5169932015-08-13 05:54:07 +02001663 if (skb_trimmed && skb_trimmed != skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001664 kfree_skb(skb_trimmed);
1665
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001666 return err;
1667}
1668#endif
1669
Herbert Xueb1d1642010-02-27 19:41:45 +00001670int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001671 struct sk_buff *skb, u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001672{
YOSHIFUJI Hideaki / 吉藤英明1fafc7a2010-04-25 08:06:40 +00001673 BR_INPUT_SKB_CB(skb)->igmp = 0;
1674 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1675
Herbert Xueb1d1642010-02-27 19:41:45 +00001676 if (br->multicast_disabled)
1677 return 0;
1678
1679 switch (skb->protocol) {
1680 case htons(ETH_P_IP):
Vlad Yasevich06499092013-10-28 15:45:07 -04001681 return br_multicast_ipv4_rcv(br, port, skb, vid);
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001682#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001683 case htons(ETH_P_IPV6):
Vlad Yasevich06499092013-10-28 15:45:07 -04001684 return br_multicast_ipv6_rcv(br, port, skb, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001685#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001686 }
1687
1688 return 0;
1689}
1690
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001691static void br_multicast_query_expired(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001692 struct bridge_mcast_own_query *query,
1693 struct bridge_mcast_querier *querier)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001694{
1695 spin_lock(&br->multicast_lock);
1696 if (query->startup_sent < br->multicast_startup_query_count)
1697 query->startup_sent++;
1698
Eric Dumazet71d9f612015-05-28 04:42:54 -07001699 RCU_INIT_POINTER(querier->port, NULL);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001700 br_multicast_send_query(br, NULL, query);
1701 spin_unlock(&br->multicast_lock);
1702}
1703
1704static void br_ip4_multicast_query_expired(unsigned long data)
Herbert Xueb1d1642010-02-27 19:41:45 +00001705{
1706 struct net_bridge *br = (void *)data;
1707
Linus Lüssing2cd41432014-06-07 18:26:29 +02001708 br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier);
Herbert Xueb1d1642010-02-27 19:41:45 +00001709}
1710
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001711#if IS_ENABLED(CONFIG_IPV6)
1712static void br_ip6_multicast_query_expired(unsigned long data)
1713{
1714 struct net_bridge *br = (void *)data;
1715
Linus Lüssing2cd41432014-06-07 18:26:29 +02001716 br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001717}
1718#endif
1719
Herbert Xueb1d1642010-02-27 19:41:45 +00001720void br_multicast_init(struct net_bridge *br)
1721{
1722 br->hash_elasticity = 4;
1723 br->hash_max = 512;
1724
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001725 br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Herbert Xuc5c23262012-04-13 02:37:42 +00001726 br->multicast_querier = 0;
Cong Wang1c8ad5b2013-05-21 21:52:54 +00001727 br->multicast_query_use_ifaddr = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001728 br->multicast_last_member_count = 2;
1729 br->multicast_startup_query_count = 2;
1730
1731 br->multicast_last_member_interval = HZ;
1732 br->multicast_query_response_interval = 10 * HZ;
1733 br->multicast_startup_query_interval = 125 * HZ / 4;
1734 br->multicast_query_interval = 125 * HZ;
1735 br->multicast_querier_interval = 255 * HZ;
1736 br->multicast_membership_interval = 260 * HZ;
1737
Linus Lüssing90010b32014-06-07 18:26:26 +02001738 br->ip4_other_query.delay_time = 0;
Linus Lüssing2cd41432014-06-07 18:26:29 +02001739 br->ip4_querier.port = NULL;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001740#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001741 br->ip6_other_query.delay_time = 0;
Linus Lüssing2cd41432014-06-07 18:26:29 +02001742 br->ip6_querier.port = NULL;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001743#endif
Linus Lüssingb00589a2013-08-01 01:06:20 +02001744
Herbert Xueb1d1642010-02-27 19:41:45 +00001745 spin_lock_init(&br->multicast_lock);
1746 setup_timer(&br->multicast_router_timer,
1747 br_multicast_local_router_expired, 0);
Linus Lüssing90010b32014-06-07 18:26:26 +02001748 setup_timer(&br->ip4_other_query.timer,
1749 br_ip4_multicast_querier_expired, (unsigned long)br);
1750 setup_timer(&br->ip4_own_query.timer, br_ip4_multicast_query_expired,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001751 (unsigned long)br);
1752#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001753 setup_timer(&br->ip6_other_query.timer,
1754 br_ip6_multicast_querier_expired, (unsigned long)br);
1755 setup_timer(&br->ip6_own_query.timer, br_ip6_multicast_query_expired,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001756 (unsigned long)br);
1757#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001758}
1759
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001760static void __br_multicast_open(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +02001761 struct bridge_mcast_own_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +00001762{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001763 query->startup_sent = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001764
1765 if (br->multicast_disabled)
1766 return;
1767
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001768 mod_timer(&query->timer, jiffies);
1769}
1770
1771void br_multicast_open(struct net_bridge *br)
1772{
Linus Lüssing90010b32014-06-07 18:26:26 +02001773 __br_multicast_open(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001774#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001775 __br_multicast_open(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001776#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001777}
1778
1779void br_multicast_stop(struct net_bridge *br)
1780{
Herbert Xueb1d1642010-02-27 19:41:45 +00001781 del_timer_sync(&br->multicast_router_timer);
Linus Lüssing90010b32014-06-07 18:26:26 +02001782 del_timer_sync(&br->ip4_other_query.timer);
1783 del_timer_sync(&br->ip4_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001784#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001785 del_timer_sync(&br->ip6_other_query.timer);
1786 del_timer_sync(&br->ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001787#endif
Satish Ashoke10177a2015-07-15 07:16:51 -07001788}
1789
1790void br_multicast_dev_del(struct net_bridge *br)
1791{
1792 struct net_bridge_mdb_htable *mdb;
1793 struct net_bridge_mdb_entry *mp;
1794 struct hlist_node *n;
1795 u32 ver;
1796 int i;
Herbert Xueb1d1642010-02-27 19:41:45 +00001797
1798 spin_lock_bh(&br->multicast_lock);
Eric Dumazete8051682010-11-15 06:38:10 +00001799 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001800 if (!mdb)
1801 goto out;
1802
1803 br->mdb = NULL;
1804
1805 ver = mdb->ver;
1806 for (i = 0; i < mdb->max; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001807 hlist_for_each_entry_safe(mp, n, &mdb->mhash[i],
Herbert Xueb1d1642010-02-27 19:41:45 +00001808 hlist[ver]) {
1809 del_timer(&mp->timer);
Herbert Xueb1d1642010-02-27 19:41:45 +00001810 call_rcu_bh(&mp->rcu, br_multicast_free_group);
1811 }
1812 }
1813
1814 if (mdb->old) {
1815 spin_unlock_bh(&br->multicast_lock);
Herbert Xu10cc2b52010-03-05 21:03:35 +00001816 rcu_barrier_bh();
Herbert Xueb1d1642010-02-27 19:41:45 +00001817 spin_lock_bh(&br->multicast_lock);
1818 WARN_ON(mdb->old);
1819 }
1820
1821 mdb->old = mdb;
1822 call_rcu_bh(&mdb->rcu, br_mdb_free);
1823
1824out:
1825 spin_unlock_bh(&br->multicast_lock);
1826}
Herbert Xu0909e112010-02-27 19:41:49 +00001827
1828int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1829{
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02001830 int err = -EINVAL;
Herbert Xu0909e112010-02-27 19:41:49 +00001831
1832 spin_lock_bh(&br->multicast_lock);
Herbert Xu0909e112010-02-27 19:41:49 +00001833
1834 switch (val) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001835 case MDB_RTR_TYPE_DISABLED:
1836 case MDB_RTR_TYPE_PERM:
Herbert Xu0909e112010-02-27 19:41:49 +00001837 del_timer(&br->multicast_router_timer);
1838 /* fall through */
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001839 case MDB_RTR_TYPE_TEMP_QUERY:
Herbert Xu0909e112010-02-27 19:41:49 +00001840 br->multicast_router = val;
1841 err = 0;
1842 break;
Herbert Xu0909e112010-02-27 19:41:49 +00001843 }
1844
Herbert Xu0909e112010-02-27 19:41:49 +00001845 spin_unlock_bh(&br->multicast_lock);
1846
1847 return err;
1848}
1849
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001850static void __del_port_router(struct net_bridge_port *p)
1851{
1852 if (hlist_unhashed(&p->rlist))
1853 return;
1854 hlist_del_init_rcu(&p->rlist);
1855 br_rtr_notify(p->br->dev, p, RTM_DELMDB);
1856}
1857
Herbert Xu0909e112010-02-27 19:41:49 +00001858int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1859{
1860 struct net_bridge *br = p->br;
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001861 unsigned long now = jiffies;
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02001862 int err = -EINVAL;
Herbert Xu0909e112010-02-27 19:41:49 +00001863
1864 spin_lock(&br->multicast_lock);
Nikolay Aleksandrov4950cfd2016-02-26 21:20:02 +01001865 if (p->multicast_router == val) {
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001866 /* Refresh the temp router port timer */
1867 if (p->multicast_router == MDB_RTR_TYPE_TEMP)
1868 mod_timer(&p->multicast_router_timer,
1869 now + br->multicast_querier_interval);
Nikolay Aleksandrov4950cfd2016-02-26 21:20:02 +01001870 err = 0;
1871 goto unlock;
1872 }
Herbert Xu0909e112010-02-27 19:41:49 +00001873 switch (val) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001874 case MDB_RTR_TYPE_DISABLED:
1875 p->multicast_router = MDB_RTR_TYPE_DISABLED;
1876 __del_port_router(p);
Herbert Xu0909e112010-02-27 19:41:49 +00001877 del_timer(&p->multicast_router_timer);
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001878 break;
1879 case MDB_RTR_TYPE_TEMP_QUERY:
1880 p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
1881 __del_port_router(p);
1882 break;
1883 case MDB_RTR_TYPE_PERM:
1884 p->multicast_router = MDB_RTR_TYPE_PERM;
1885 del_timer(&p->multicast_router_timer);
Herbert Xu0909e112010-02-27 19:41:49 +00001886 br_multicast_add_router(br, p);
1887 break;
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001888 case MDB_RTR_TYPE_TEMP:
1889 p->multicast_router = MDB_RTR_TYPE_TEMP;
1890 br_multicast_mark_router(br, p);
1891 break;
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001892 default:
1893 goto unlock;
Herbert Xu0909e112010-02-27 19:41:49 +00001894 }
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001895 err = 0;
1896unlock:
Herbert Xu0909e112010-02-27 19:41:49 +00001897 spin_unlock(&br->multicast_lock);
1898
1899 return err;
1900}
Herbert Xu561f1102010-02-27 19:41:50 +00001901
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001902static void br_multicast_start_querier(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +02001903 struct bridge_mcast_own_query *query)
Herbert Xu561f1102010-02-27 19:41:50 +00001904{
1905 struct net_bridge_port *port;
Herbert Xu74857212012-04-13 02:37:42 +00001906
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001907 __br_multicast_open(br, query);
Herbert Xu74857212012-04-13 02:37:42 +00001908
1909 list_for_each_entry(port, &br->port_list, list) {
1910 if (port->state == BR_STATE_DISABLED ||
1911 port->state == BR_STATE_BLOCKING)
1912 continue;
1913
Linus Lüssing90010b32014-06-07 18:26:26 +02001914 if (query == &br->ip4_own_query)
1915 br_multicast_enable(&port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001916#if IS_ENABLED(CONFIG_IPV6)
1917 else
Linus Lüssing90010b32014-06-07 18:26:26 +02001918 br_multicast_enable(&port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001919#endif
Herbert Xu74857212012-04-13 02:37:42 +00001920 }
1921}
1922
1923int br_multicast_toggle(struct net_bridge *br, unsigned long val)
1924{
Herbert Xu3a7fda02010-07-29 00:45:30 +00001925 int err = 0;
Eric Dumazete8051682010-11-15 06:38:10 +00001926 struct net_bridge_mdb_htable *mdb;
Herbert Xu561f1102010-02-27 19:41:50 +00001927
Andrey Vaginef5e0d82011-11-10 05:48:03 +00001928 spin_lock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00001929 if (br->multicast_disabled == !val)
1930 goto unlock;
1931
1932 br->multicast_disabled = !val;
1933 if (br->multicast_disabled)
1934 goto unlock;
1935
Herbert Xu3a7fda02010-07-29 00:45:30 +00001936 if (!netif_running(br->dev))
1937 goto unlock;
1938
Eric Dumazete8051682010-11-15 06:38:10 +00001939 mdb = mlock_dereference(br->mdb, br);
1940 if (mdb) {
1941 if (mdb->old) {
Herbert Xu561f1102010-02-27 19:41:50 +00001942 err = -EEXIST;
1943rollback:
1944 br->multicast_disabled = !!val;
1945 goto unlock;
1946 }
1947
Eric Dumazete8051682010-11-15 06:38:10 +00001948 err = br_mdb_rehash(&br->mdb, mdb->max,
Herbert Xu561f1102010-02-27 19:41:50 +00001949 br->hash_elasticity);
1950 if (err)
1951 goto rollback;
1952 }
1953
Linus Lüssing90010b32014-06-07 18:26:26 +02001954 br_multicast_start_querier(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001955#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001956 br_multicast_start_querier(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001957#endif
Herbert Xu561f1102010-02-27 19:41:50 +00001958
1959unlock:
Andrey Vaginef5e0d82011-11-10 05:48:03 +00001960 spin_unlock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00001961
1962 return err;
1963}
Herbert Xub1951672010-02-27 19:41:51 +00001964
Herbert Xuc5c23262012-04-13 02:37:42 +00001965int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
1966{
Linus Lüssingb00589a2013-08-01 01:06:20 +02001967 unsigned long max_delay;
1968
Herbert Xuc5c23262012-04-13 02:37:42 +00001969 val = !!val;
1970
1971 spin_lock_bh(&br->multicast_lock);
1972 if (br->multicast_querier == val)
1973 goto unlock;
1974
1975 br->multicast_querier = val;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001976 if (!val)
1977 goto unlock;
1978
1979 max_delay = br->multicast_query_response_interval;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001980
Linus Lüssing90010b32014-06-07 18:26:26 +02001981 if (!timer_pending(&br->ip4_other_query.timer))
1982 br->ip4_other_query.delay_time = jiffies + max_delay;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001983
Linus Lüssing90010b32014-06-07 18:26:26 +02001984 br_multicast_start_querier(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001985
1986#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001987 if (!timer_pending(&br->ip6_other_query.timer))
1988 br->ip6_other_query.delay_time = jiffies + max_delay;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001989
Linus Lüssing90010b32014-06-07 18:26:26 +02001990 br_multicast_start_querier(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001991#endif
Herbert Xuc5c23262012-04-13 02:37:42 +00001992
1993unlock:
1994 spin_unlock_bh(&br->multicast_lock);
1995
1996 return 0;
1997}
1998
Herbert Xub1951672010-02-27 19:41:51 +00001999int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
2000{
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02002001 int err = -EINVAL;
Herbert Xub1951672010-02-27 19:41:51 +00002002 u32 old;
Eric Dumazete8051682010-11-15 06:38:10 +00002003 struct net_bridge_mdb_htable *mdb;
Herbert Xub1951672010-02-27 19:41:51 +00002004
Curt Brunefe0d6922014-01-06 11:00:32 -08002005 spin_lock_bh(&br->multicast_lock);
Herbert Xub1951672010-02-27 19:41:51 +00002006 if (!is_power_of_2(val))
2007 goto unlock;
Eric Dumazete8051682010-11-15 06:38:10 +00002008
2009 mdb = mlock_dereference(br->mdb, br);
2010 if (mdb && val < mdb->size)
Herbert Xub1951672010-02-27 19:41:51 +00002011 goto unlock;
2012
2013 err = 0;
2014
2015 old = br->hash_max;
2016 br->hash_max = val;
2017
Eric Dumazete8051682010-11-15 06:38:10 +00002018 if (mdb) {
2019 if (mdb->old) {
Herbert Xub1951672010-02-27 19:41:51 +00002020 err = -EEXIST;
2021rollback:
2022 br->hash_max = old;
2023 goto unlock;
2024 }
2025
2026 err = br_mdb_rehash(&br->mdb, br->hash_max,
2027 br->hash_elasticity);
2028 if (err)
2029 goto rollback;
2030 }
2031
2032unlock:
Curt Brunefe0d6922014-01-06 11:00:32 -08002033 spin_unlock_bh(&br->multicast_lock);
Herbert Xub1951672010-02-27 19:41:51 +00002034
2035 return err;
2036}
Linus Lüssing07f8ac42014-06-07 18:26:28 +02002037
2038/**
2039 * br_multicast_list_adjacent - Returns snooped multicast addresses
2040 * @dev: The bridge port adjacent to which to retrieve addresses
2041 * @br_ip_list: The list to store found, snooped multicast IP addresses in
2042 *
2043 * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
2044 * snooping feature on all bridge ports of dev's bridge device, excluding
2045 * the addresses from dev itself.
2046 *
2047 * Returns the number of items added to br_ip_list.
2048 *
2049 * Notes:
2050 * - br_ip_list needs to be initialized by caller
2051 * - br_ip_list might contain duplicates in the end
2052 * (needs to be taken care of by caller)
2053 * - br_ip_list needs to be freed by caller
2054 */
2055int br_multicast_list_adjacent(struct net_device *dev,
2056 struct list_head *br_ip_list)
2057{
2058 struct net_bridge *br;
2059 struct net_bridge_port *port;
2060 struct net_bridge_port_group *group;
2061 struct br_ip_list *entry;
2062 int count = 0;
2063
2064 rcu_read_lock();
2065 if (!br_ip_list || !br_port_exists(dev))
2066 goto unlock;
2067
2068 port = br_port_get_rcu(dev);
2069 if (!port || !port->br)
2070 goto unlock;
2071
2072 br = port->br;
2073
2074 list_for_each_entry_rcu(port, &br->port_list, list) {
2075 if (!port->dev || port->dev == dev)
2076 continue;
2077
2078 hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
2079 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
2080 if (!entry)
2081 goto unlock;
2082
2083 entry->addr = group->addr;
2084 list_add(&entry->list, br_ip_list);
2085 count++;
2086 }
2087 }
2088
2089unlock:
2090 rcu_read_unlock();
2091 return count;
2092}
2093EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
Linus Lüssing2cd41432014-06-07 18:26:29 +02002094
2095/**
Linus Lüssingc34963e2014-07-07 05:41:17 +02002096 * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2097 * @dev: The bridge port providing the bridge on which to check for a querier
2098 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2099 *
2100 * Checks whether the given interface has a bridge on top and if so returns
2101 * true if a valid querier exists anywhere on the bridged link layer.
2102 * Otherwise returns false.
2103 */
2104bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
2105{
2106 struct net_bridge *br;
2107 struct net_bridge_port *port;
2108 struct ethhdr eth;
2109 bool ret = false;
2110
2111 rcu_read_lock();
2112 if (!br_port_exists(dev))
2113 goto unlock;
2114
2115 port = br_port_get_rcu(dev);
2116 if (!port || !port->br)
2117 goto unlock;
2118
2119 br = port->br;
2120
2121 memset(&eth, 0, sizeof(eth));
2122 eth.h_proto = htons(proto);
2123
2124 ret = br_multicast_querier_exists(br, &eth);
2125
2126unlock:
2127 rcu_read_unlock();
2128 return ret;
2129}
2130EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
2131
2132/**
Linus Lüssing2cd41432014-06-07 18:26:29 +02002133 * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
2134 * @dev: The bridge port adjacent to which to check for a querier
2135 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2136 *
2137 * Checks whether the given interface has a bridge on top and if so returns
2138 * true if a selected querier is behind one of the other ports of this
2139 * bridge. Otherwise returns false.
2140 */
2141bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
2142{
2143 struct net_bridge *br;
2144 struct net_bridge_port *port;
2145 bool ret = false;
2146
2147 rcu_read_lock();
2148 if (!br_port_exists(dev))
2149 goto unlock;
2150
2151 port = br_port_get_rcu(dev);
2152 if (!port || !port->br)
2153 goto unlock;
2154
2155 br = port->br;
2156
2157 switch (proto) {
2158 case ETH_P_IP:
2159 if (!timer_pending(&br->ip4_other_query.timer) ||
2160 rcu_dereference(br->ip4_querier.port) == port)
2161 goto unlock;
2162 break;
Linus Lüssing3993c4e2014-06-12 01:41:24 +02002163#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing2cd41432014-06-07 18:26:29 +02002164 case ETH_P_IPV6:
2165 if (!timer_pending(&br->ip6_other_query.timer) ||
2166 rcu_dereference(br->ip6_querier.port) == port)
2167 goto unlock;
2168 break;
Linus Lüssing3993c4e2014-06-12 01:41:24 +02002169#endif
Linus Lüssing2cd41432014-06-07 18:26:29 +02002170 default:
2171 goto unlock;
2172 }
2173
2174 ret = true;
2175unlock:
2176 rcu_read_unlock();
2177 return ret;
2178}
2179EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);