blob: 073d54afa0568396b9a4f929de54553cffc7016e [file] [log] [blame]
Herbert Xueb1d1642010-02-27 19:41:45 +00001/*
2 * Bridge multicast support.
3 *
4 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 */
12
13#include <linux/err.h>
Linus Lüssing07f8ac42014-06-07 18:26:28 +020014#include <linux/export.h>
Herbert Xueb1d1642010-02-27 19:41:45 +000015#include <linux/if_ether.h>
16#include <linux/igmp.h>
17#include <linux/jhash.h>
18#include <linux/kernel.h>
Herbert Xub1951672010-02-27 19:41:51 +000019#include <linux/log2.h>
Herbert Xueb1d1642010-02-27 19:41:45 +000020#include <linux/netdevice.h>
21#include <linux/netfilter_bridge.h>
22#include <linux/random.h>
23#include <linux/rculist.h>
24#include <linux/skbuff.h>
25#include <linux/slab.h>
26#include <linux/timer.h>
Cong Wang1c8ad5b2013-05-21 21:52:54 +000027#include <linux/inetdevice.h>
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +010028#include <linux/mroute.h>
Herbert Xueb1d1642010-02-27 19:41:45 +000029#include <net/ip.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000030#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090031#include <net/ipv6.h>
32#include <net/mld.h>
David S. Millerd4c4f072010-04-27 10:16:54 -070033#include <net/ip6_checksum.h>
Linus Lüssing3c3769e2013-09-04 02:13:39 +020034#include <net/addrconf.h>
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090035#endif
Herbert Xueb1d1642010-02-27 19:41:45 +000036
37#include "br_private.h"
38
Linus Lüssingcc0fdd82013-08-30 17:28:17 +020039static void br_multicast_start_querier(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +020040 struct bridge_mcast_own_query *query);
Satish Ashok754bc542015-06-19 01:22:57 -070041static void br_multicast_add_router(struct net_bridge *br,
42 struct net_bridge_port *port);
Satish Ashokbc8c20a2015-07-13 05:28:37 -070043static void br_ip4_multicast_leave_group(struct net_bridge *br,
44 struct net_bridge_port *port,
45 __be32 group,
46 __u16 vid);
47#if IS_ENABLED(CONFIG_IPV6)
48static void br_ip6_multicast_leave_group(struct net_bridge *br,
49 struct net_bridge_port *port,
50 const struct in6_addr *group,
51 __u16 vid);
52#endif
Cong Wang2ce297f2012-12-10 02:15:35 +000053unsigned int br_mdb_rehash_seq;
Herbert Xuc83b8fa2012-04-13 02:37:42 +000054
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090055static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
56{
57 if (a->proto != b->proto)
58 return 0;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000059 if (a->vid != b->vid)
60 return 0;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090061 switch (a->proto) {
62 case htons(ETH_P_IP):
63 return a->u.ip4 == b->u.ip4;
Eric Dumazetdfd56b82011-12-10 09:48:31 +000064#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090065 case htons(ETH_P_IPV6):
66 return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
67#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090068 }
69 return 0;
70}
71
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000072static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip,
73 __u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +000074{
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000075 return jhash_2words((__force u32)ip, vid, mdb->secret) & (mdb->max - 1);
Herbert Xueb1d1642010-02-27 19:41:45 +000076}
77
Eric Dumazetdfd56b82011-12-10 09:48:31 +000078#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090079static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000080 const struct in6_addr *ip,
81 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090082{
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000083 return jhash_2words(ipv6_addr_hash(ip), vid,
84 mdb->secret) & (mdb->max - 1);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090085}
86#endif
87
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090088static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb,
89 struct br_ip *ip)
90{
91 switch (ip->proto) {
92 case htons(ETH_P_IP):
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000093 return __br_ip4_hash(mdb, ip->u.ip4, ip->vid);
Eric Dumazetdfd56b82011-12-10 09:48:31 +000094#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090095 case htons(ETH_P_IPV6):
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000096 return __br_ip6_hash(mdb, &ip->u.ip6, ip->vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090097#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090098 }
99 return 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000100}
101
102static struct net_bridge_mdb_entry *__br_mdb_ip_get(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900103 struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +0000104{
105 struct net_bridge_mdb_entry *mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000106
Sasha Levinb67bfe02013-02-27 17:06:00 -0800107 hlist_for_each_entry_rcu(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900108 if (br_ip_equal(&mp->addr, dst))
Herbert Xueb1d1642010-02-27 19:41:45 +0000109 return mp;
110 }
111
112 return NULL;
113}
114
Cong Wangcfd56752012-12-11 22:23:08 +0000115struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge_mdb_htable *mdb,
116 struct br_ip *dst)
Herbert Xu7f285fa2010-07-05 14:50:08 +0000117{
118 if (!mdb)
119 return NULL;
120
121 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
122}
123
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900124static struct net_bridge_mdb_entry *br_mdb_ip4_get(
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000125 struct net_bridge_mdb_htable *mdb, __be32 dst, __u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000126{
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900127 struct br_ip br_dst;
Herbert Xu0821ec52010-03-15 20:38:25 -0700128
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900129 br_dst.u.ip4 = dst;
130 br_dst.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000131 br_dst.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900132
Herbert Xu7f285fa2010-07-05 14:50:08 +0000133 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900134}
135
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000136#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900137static struct net_bridge_mdb_entry *br_mdb_ip6_get(
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000138 struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst,
139 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900140{
141 struct br_ip br_dst;
142
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000143 br_dst.u.ip6 = *dst;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900144 br_dst.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000145 br_dst.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900146
Herbert Xu7f285fa2010-07-05 14:50:08 +0000147 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900148}
149#endif
150
Herbert Xueb1d1642010-02-27 19:41:45 +0000151struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
Cong Wangfbca58a2013-03-07 03:05:33 +0000152 struct sk_buff *skb, u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000153{
Eric Dumazete8051682010-11-15 06:38:10 +0000154 struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900155 struct br_ip ip;
156
Herbert Xu7f285fa2010-07-05 14:50:08 +0000157 if (br->multicast_disabled)
Herbert Xueb1d1642010-02-27 19:41:45 +0000158 return NULL;
159
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900160 if (BR_INPUT_SKB_CB(skb)->igmp)
161 return NULL;
162
163 ip.proto = skb->protocol;
Cong Wangfbca58a2013-03-07 03:05:33 +0000164 ip.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900165
Herbert Xueb1d1642010-02-27 19:41:45 +0000166 switch (skb->protocol) {
167 case htons(ETH_P_IP):
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900168 ip.u.ip4 = ip_hdr(skb)->daddr;
169 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000170#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900171 case htons(ETH_P_IPV6):
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000172 ip.u.ip6 = ipv6_hdr(skb)->daddr;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900173 break;
174#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900175 default:
176 return NULL;
Herbert Xueb1d1642010-02-27 19:41:45 +0000177 }
178
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900179 return br_mdb_ip_get(mdb, &ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000180}
181
182static void br_mdb_free(struct rcu_head *head)
183{
184 struct net_bridge_mdb_htable *mdb =
185 container_of(head, struct net_bridge_mdb_htable, rcu);
186 struct net_bridge_mdb_htable *old = mdb->old;
187
188 mdb->old = NULL;
189 kfree(old->mhash);
190 kfree(old);
191}
192
193static int br_mdb_copy(struct net_bridge_mdb_htable *new,
194 struct net_bridge_mdb_htable *old,
195 int elasticity)
196{
197 struct net_bridge_mdb_entry *mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000198 int maxlen;
199 int len;
200 int i;
201
202 for (i = 0; i < old->max; i++)
Sasha Levinb67bfe02013-02-27 17:06:00 -0800203 hlist_for_each_entry(mp, &old->mhash[i], hlist[old->ver])
Herbert Xueb1d1642010-02-27 19:41:45 +0000204 hlist_add_head(&mp->hlist[new->ver],
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900205 &new->mhash[br_ip_hash(new, &mp->addr)]);
Herbert Xueb1d1642010-02-27 19:41:45 +0000206
207 if (!elasticity)
208 return 0;
209
210 maxlen = 0;
211 for (i = 0; i < new->max; i++) {
212 len = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800213 hlist_for_each_entry(mp, &new->mhash[i], hlist[new->ver])
Herbert Xueb1d1642010-02-27 19:41:45 +0000214 len++;
215 if (len > maxlen)
216 maxlen = len;
217 }
218
219 return maxlen > elasticity ? -EINVAL : 0;
220}
221
Cong Wangcfd56752012-12-11 22:23:08 +0000222void br_multicast_free_pg(struct rcu_head *head)
Herbert Xueb1d1642010-02-27 19:41:45 +0000223{
224 struct net_bridge_port_group *p =
225 container_of(head, struct net_bridge_port_group, rcu);
226
227 kfree(p);
228}
229
230static void br_multicast_free_group(struct rcu_head *head)
231{
232 struct net_bridge_mdb_entry *mp =
233 container_of(head, struct net_bridge_mdb_entry, rcu);
234
235 kfree(mp);
236}
237
238static void br_multicast_group_expired(unsigned long data)
239{
240 struct net_bridge_mdb_entry *mp = (void *)data;
241 struct net_bridge *br = mp->br;
242 struct net_bridge_mdb_htable *mdb;
243
244 spin_lock(&br->multicast_lock);
245 if (!netif_running(br->dev) || timer_pending(&mp->timer))
246 goto out;
247
Herbert Xu8a870172011-02-12 01:05:42 -0800248 mp->mglist = false;
Herbert Xueb1d1642010-02-27 19:41:45 +0000249
250 if (mp->ports)
251 goto out;
252
Eric Dumazete8051682010-11-15 06:38:10 +0000253 mdb = mlock_dereference(br->mdb, br);
254
Herbert Xueb1d1642010-02-27 19:41:45 +0000255 hlist_del_rcu(&mp->hlist[mdb->ver]);
256 mdb->size--;
257
Herbert Xueb1d1642010-02-27 19:41:45 +0000258 call_rcu_bh(&mp->rcu, br_multicast_free_group);
259
260out:
261 spin_unlock(&br->multicast_lock);
262}
263
264static void br_multicast_del_pg(struct net_bridge *br,
265 struct net_bridge_port_group *pg)
266{
Eric Dumazete8051682010-11-15 06:38:10 +0000267 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000268 struct net_bridge_mdb_entry *mp;
269 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +0000270 struct net_bridge_port_group __rcu **pp;
271
272 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +0000273
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900274 mp = br_mdb_ip_get(mdb, &pg->addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000275 if (WARN_ON(!mp))
276 return;
277
Eric Dumazete8051682010-11-15 06:38:10 +0000278 for (pp = &mp->ports;
279 (p = mlock_dereference(*pp, br)) != NULL;
280 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000281 if (p != pg)
282 continue;
283
stephen hemminger83f6a742010-04-27 15:01:06 +0000284 rcu_assign_pointer(*pp, p->next);
Herbert Xueb1d1642010-02-27 19:41:45 +0000285 hlist_del_init(&p->mglist);
286 del_timer(&p->timer);
Elad Raz45ebcce2016-04-21 12:52:45 +0200287 br_mdb_notify(br->dev, p->port, &pg->addr, RTM_DELMDB,
288 p->flags);
Herbert Xueb1d1642010-02-27 19:41:45 +0000289 call_rcu_bh(&p->rcu, br_multicast_free_pg);
290
Linus Lüssing454594f2013-10-20 00:58:57 +0200291 if (!mp->ports && !mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +0000292 netif_running(br->dev))
293 mod_timer(&mp->timer, jiffies);
294
295 return;
296 }
297
298 WARN_ON(1);
299}
300
301static void br_multicast_port_group_expired(unsigned long data)
302{
303 struct net_bridge_port_group *pg = (void *)data;
304 struct net_bridge *br = pg->port->br;
305
306 spin_lock(&br->multicast_lock);
307 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
Elad Raz9d06b6d2016-02-03 09:57:05 +0100308 hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT)
Herbert Xueb1d1642010-02-27 19:41:45 +0000309 goto out;
310
311 br_multicast_del_pg(br, pg);
312
313out:
314 spin_unlock(&br->multicast_lock);
315}
316
Eric Dumazete8051682010-11-15 06:38:10 +0000317static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max,
Herbert Xueb1d1642010-02-27 19:41:45 +0000318 int elasticity)
319{
Eric Dumazete8051682010-11-15 06:38:10 +0000320 struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000321 struct net_bridge_mdb_htable *mdb;
322 int err;
323
324 mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
325 if (!mdb)
326 return -ENOMEM;
327
328 mdb->max = max;
329 mdb->old = old;
330
331 mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
332 if (!mdb->mhash) {
333 kfree(mdb);
334 return -ENOMEM;
335 }
336
337 mdb->size = old ? old->size : 0;
338 mdb->ver = old ? old->ver ^ 1 : 0;
339
340 if (!old || elasticity)
341 get_random_bytes(&mdb->secret, sizeof(mdb->secret));
342 else
343 mdb->secret = old->secret;
344
345 if (!old)
346 goto out;
347
348 err = br_mdb_copy(mdb, old, elasticity);
349 if (err) {
350 kfree(mdb->mhash);
351 kfree(mdb);
352 return err;
353 }
354
Cong Wang2ce297f2012-12-10 02:15:35 +0000355 br_mdb_rehash_seq++;
Herbert Xueb1d1642010-02-27 19:41:45 +0000356 call_rcu_bh(&mdb->rcu, br_mdb_free);
357
358out:
359 rcu_assign_pointer(*mdbp, mdb);
360
361 return 0;
362}
363
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900364static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200365 __be32 group,
366 u8 *igmp_type)
Herbert Xueb1d1642010-02-27 19:41:45 +0000367{
368 struct sk_buff *skb;
369 struct igmphdr *ih;
370 struct ethhdr *eth;
371 struct iphdr *iph;
372
373 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
374 sizeof(*ih) + 4);
375 if (!skb)
376 goto out;
377
378 skb->protocol = htons(ETH_P_IP);
379
380 skb_reset_mac_header(skb);
381 eth = eth_hdr(skb);
382
Joe Perchese5a727f2014-02-23 00:05:25 -0800383 ether_addr_copy(eth->h_source, br->dev->dev_addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000384 eth->h_dest[0] = 1;
385 eth->h_dest[1] = 0;
386 eth->h_dest[2] = 0x5e;
387 eth->h_dest[3] = 0;
388 eth->h_dest[4] = 0;
389 eth->h_dest[5] = 1;
390 eth->h_proto = htons(ETH_P_IP);
391 skb_put(skb, sizeof(*eth));
392
393 skb_set_network_header(skb, skb->len);
394 iph = ip_hdr(skb);
395
396 iph->version = 4;
397 iph->ihl = 6;
398 iph->tos = 0xc0;
399 iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4);
400 iph->id = 0;
401 iph->frag_off = htons(IP_DF);
402 iph->ttl = 1;
403 iph->protocol = IPPROTO_IGMP;
Cong Wang1c8ad5b2013-05-21 21:52:54 +0000404 iph->saddr = br->multicast_query_use_ifaddr ?
405 inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000406 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
407 ((u8 *)&iph[1])[0] = IPOPT_RA;
408 ((u8 *)&iph[1])[1] = 4;
409 ((u8 *)&iph[1])[2] = 0;
410 ((u8 *)&iph[1])[3] = 0;
411 ip_send_check(iph);
412 skb_put(skb, 24);
413
414 skb_set_transport_header(skb, skb->len);
415 ih = igmp_hdr(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200416 *igmp_type = IGMP_HOST_MEMBERSHIP_QUERY;
Herbert Xueb1d1642010-02-27 19:41:45 +0000417 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
418 ih->code = (group ? br->multicast_last_member_interval :
419 br->multicast_query_response_interval) /
420 (HZ / IGMP_TIMER_SCALE);
421 ih->group = group;
422 ih->csum = 0;
423 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
424 skb_put(skb, sizeof(*ih));
425
426 __skb_pull(skb, sizeof(*eth));
427
428out:
429 return skb;
430}
431
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000432#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900433static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200434 const struct in6_addr *grp,
435 u8 *igmp_type)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900436{
437 struct sk_buff *skb;
438 struct ipv6hdr *ip6h;
439 struct mld_msg *mldq;
440 struct ethhdr *eth;
441 u8 *hopopt;
442 unsigned long interval;
443
444 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
445 8 + sizeof(*mldq));
446 if (!skb)
447 goto out;
448
449 skb->protocol = htons(ETH_P_IPV6);
450
451 /* Ethernet header */
452 skb_reset_mac_header(skb);
453 eth = eth_hdr(skb);
454
Joe Perchese5a727f2014-02-23 00:05:25 -0800455 ether_addr_copy(eth->h_source, br->dev->dev_addr);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900456 eth->h_proto = htons(ETH_P_IPV6);
457 skb_put(skb, sizeof(*eth));
458
459 /* IPv6 header + HbH option */
460 skb_set_network_header(skb, skb->len);
461 ip6h = ipv6_hdr(skb);
462
463 *(__force __be32 *)ip6h = htonl(0x60000000);
David Stevens76d6615862010-12-14 08:42:16 +0000464 ip6h->payload_len = htons(8 + sizeof(*mldq));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900465 ip6h->nexthdr = IPPROTO_HOPOPTS;
466 ip6h->hop_limit = 1;
Linus Lüssinga7bff752011-03-22 11:40:32 +0000467 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
Ulrich Weberd1d81d42012-03-05 04:52:44 +0000468 if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
469 &ip6h->saddr)) {
470 kfree_skb(skb);
daniel0888d5f2016-06-24 12:35:18 +0200471 br->has_ipv6_addr = 0;
Ulrich Weberd1d81d42012-03-05 04:52:44 +0000472 return NULL;
473 }
daniel0888d5f2016-06-24 12:35:18 +0200474
475 br->has_ipv6_addr = 1;
Linus Lüssing36cff5a2011-02-17 08:17:51 +0000476 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900477
478 hopopt = (u8 *)(ip6h + 1);
479 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
480 hopopt[1] = 0; /* length of HbH */
481 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
482 hopopt[3] = 2; /* Length of RA Option */
483 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
484 hopopt[5] = 0;
Eldad Zack1de5a712012-05-17 06:00:25 +0000485 hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */
486 hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900487
488 skb_put(skb, sizeof(*ip6h) + 8);
489
490 /* ICMPv6 */
491 skb_set_transport_header(skb, skb->len);
492 mldq = (struct mld_msg *) icmp6_hdr(skb);
493
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200494 interval = ipv6_addr_any(grp) ?
Linus Lüssing32de8682013-06-16 23:20:34 +0200495 br->multicast_query_response_interval :
496 br->multicast_last_member_interval;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900497
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200498 *igmp_type = ICMPV6_MGM_QUERY;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900499 mldq->mld_type = ICMPV6_MGM_QUERY;
500 mldq->mld_code = 0;
501 mldq->mld_cksum = 0;
502 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
503 mldq->mld_reserved = 0;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200504 mldq->mld_mca = *grp;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900505
506 /* checksum */
507 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
508 sizeof(*mldq), IPPROTO_ICMPV6,
509 csum_partial(mldq,
510 sizeof(*mldq), 0));
511 skb_put(skb, sizeof(*mldq));
512
513 __skb_pull(skb, sizeof(*eth));
514
515out:
516 return skb;
517}
518#endif
519
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900520static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200521 struct br_ip *addr,
522 u8 *igmp_type)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900523{
524 switch (addr->proto) {
525 case htons(ETH_P_IP):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200526 return br_ip4_multicast_alloc_query(br, addr->u.ip4, igmp_type);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000527#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900528 case htons(ETH_P_IPV6):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200529 return br_ip6_multicast_alloc_query(br, &addr->u.ip6,
530 igmp_type);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900531#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900532 }
533 return NULL;
534}
535
Herbert Xueb1d1642010-02-27 19:41:45 +0000536static struct net_bridge_mdb_entry *br_multicast_get_group(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900537 struct net_bridge *br, struct net_bridge_port *port,
538 struct br_ip *group, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +0000539{
Eric Dumazete8051682010-11-15 06:38:10 +0000540 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000541 struct net_bridge_mdb_entry *mp;
Eric Dumazet95c96172012-04-15 05:58:06 +0000542 unsigned int count = 0;
543 unsigned int max;
Herbert Xueb1d1642010-02-27 19:41:45 +0000544 int elasticity;
545 int err;
546
Eric Dumazete8051682010-11-15 06:38:10 +0000547 mdb = rcu_dereference_protected(br->mdb, 1);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800548 hlist_for_each_entry(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000549 count++;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900550 if (unlikely(br_ip_equal(group, &mp->addr)))
Herbert Xueb1d1642010-02-27 19:41:45 +0000551 return mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000552 }
553
554 elasticity = 0;
555 max = mdb->max;
556
557 if (unlikely(count > br->hash_elasticity && count)) {
558 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000559 br_info(br, "Multicast hash table "
560 "chain limit reached: %s\n",
561 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000562
563 elasticity = br->hash_elasticity;
564 }
565
566 if (mdb->size >= max) {
567 max *= 2;
Thomas Graf036be6db2012-07-10 22:29:19 +0000568 if (unlikely(max > br->hash_max)) {
569 br_warn(br, "Multicast hash table maximum of %d "
570 "reached, disabling snooping: %s\n",
571 br->hash_max,
572 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000573 err = -E2BIG;
574disable:
575 br->multicast_disabled = 1;
576 goto err;
577 }
578 }
579
580 if (max > mdb->max || elasticity) {
581 if (mdb->old) {
582 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000583 br_info(br, "Multicast hash table "
584 "on fire: %s\n",
585 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000586 err = -EEXIST;
587 goto err;
588 }
589
590 err = br_mdb_rehash(&br->mdb, max, elasticity);
591 if (err) {
stephen hemminger28a16c92010-05-10 09:31:09 +0000592 br_warn(br, "Cannot rehash multicast "
593 "hash table, disabling snooping: %s, %d, %d\n",
594 port ? port->dev->name : br->dev->name,
595 mdb->size, err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000596 goto disable;
597 }
598
599 err = -EAGAIN;
600 goto err;
601 }
602
603 return NULL;
604
605err:
606 mp = ERR_PTR(err);
607 return mp;
608}
609
Cong Wangcfd56752012-12-11 22:23:08 +0000610struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
611 struct net_bridge_port *port, struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000612{
Eric Dumazete8051682010-11-15 06:38:10 +0000613 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000614 struct net_bridge_mdb_entry *mp;
615 int hash;
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000616 int err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000617
Eric Dumazete8051682010-11-15 06:38:10 +0000618 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000619 if (!mdb) {
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000620 err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0);
621 if (err)
622 return ERR_PTR(err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000623 goto rehash;
624 }
625
626 hash = br_ip_hash(mdb, group);
627 mp = br_multicast_get_group(br, port, group, hash);
628 switch (PTR_ERR(mp)) {
629 case 0:
630 break;
631
632 case -EAGAIN:
633rehash:
Eric Dumazete8051682010-11-15 06:38:10 +0000634 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000635 hash = br_ip_hash(mdb, group);
636 break;
637
638 default:
639 goto out;
640 }
641
642 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
643 if (unlikely(!mp))
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000644 return ERR_PTR(-ENOMEM);
Herbert Xueb1d1642010-02-27 19:41:45 +0000645
646 mp->br = br;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900647 mp->addr = *group;
Eric Dumazet1faabf22013-07-19 20:07:16 -0700648 setup_timer(&mp->timer, br_multicast_group_expired,
649 (unsigned long)mp);
650
Herbert Xueb1d1642010-02-27 19:41:45 +0000651 hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
652 mdb->size++;
653
654out:
655 return mp;
656}
657
Cong Wangcfd56752012-12-11 22:23:08 +0000658struct net_bridge_port_group *br_multicast_new_port_group(
659 struct net_bridge_port *port,
660 struct br_ip *group,
Amerigo Wangccb1c312012-12-14 22:09:51 +0000661 struct net_bridge_port_group __rcu *next,
Elad Raz9d06b6d2016-02-03 09:57:05 +0100662 unsigned char flags)
Cong Wangcfd56752012-12-11 22:23:08 +0000663{
664 struct net_bridge_port_group *p;
665
666 p = kzalloc(sizeof(*p), GFP_ATOMIC);
667 if (unlikely(!p))
668 return NULL;
669
670 p->addr = *group;
671 p->port = port;
Elad Raz9d06b6d2016-02-03 09:57:05 +0100672 p->flags = flags;
stephen hemmingereca2a432012-12-13 06:51:28 +0000673 rcu_assign_pointer(p->next, next);
Cong Wangcfd56752012-12-11 22:23:08 +0000674 hlist_add_head(&p->mglist, &port->mglist);
675 setup_timer(&p->timer, br_multicast_port_group_expired,
676 (unsigned long)p);
677 return p;
678}
679
Herbert Xueb1d1642010-02-27 19:41:45 +0000680static int br_multicast_add_group(struct net_bridge *br,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900681 struct net_bridge_port *port,
682 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000683{
684 struct net_bridge_mdb_entry *mp;
685 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +0000686 struct net_bridge_port_group __rcu **pp;
Linus Lüssing454594f2013-10-20 00:58:57 +0200687 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +0000688 int err;
689
Herbert Xueb1d1642010-02-27 19:41:45 +0000690 spin_lock(&br->multicast_lock);
691 if (!netif_running(br->dev) ||
692 (port && port->state == BR_STATE_DISABLED))
693 goto out;
694
695 mp = br_multicast_new_group(br, port, group);
696 err = PTR_ERR(mp);
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000697 if (IS_ERR(mp))
Herbert Xueb1d1642010-02-27 19:41:45 +0000698 goto err;
699
700 if (!port) {
Herbert Xu8a870172011-02-12 01:05:42 -0800701 mp->mglist = true;
Linus Lüssing454594f2013-10-20 00:58:57 +0200702 mod_timer(&mp->timer, now + br->multicast_membership_interval);
Herbert Xueb1d1642010-02-27 19:41:45 +0000703 goto out;
704 }
705
Eric Dumazete8051682010-11-15 06:38:10 +0000706 for (pp = &mp->ports;
707 (p = mlock_dereference(*pp, br)) != NULL;
708 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000709 if (p->port == port)
Linus Lüssing454594f2013-10-20 00:58:57 +0200710 goto found;
Herbert Xueb1d1642010-02-27 19:41:45 +0000711 if ((unsigned long)p->port < (unsigned long)port)
712 break;
713 }
714
Elad Raz9d06b6d2016-02-03 09:57:05 +0100715 p = br_multicast_new_port_group(port, group, *pp, 0);
Herbert Xueb1d1642010-02-27 19:41:45 +0000716 if (unlikely(!p))
717 goto err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000718 rcu_assign_pointer(*pp, p);
Elad Raz45ebcce2016-04-21 12:52:45 +0200719 br_mdb_notify(br->dev, port, group, RTM_NEWMDB, 0);
Herbert Xueb1d1642010-02-27 19:41:45 +0000720
Linus Lüssing454594f2013-10-20 00:58:57 +0200721found:
722 mod_timer(&p->timer, now + br->multicast_membership_interval);
Herbert Xueb1d1642010-02-27 19:41:45 +0000723out:
724 err = 0;
725
726err:
727 spin_unlock(&br->multicast_lock);
728 return err;
729}
730
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900731static int br_ip4_multicast_add_group(struct net_bridge *br,
732 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000733 __be32 group,
734 __u16 vid)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900735{
736 struct br_ip br_group;
737
738 if (ipv4_is_local_multicast(group))
739 return 0;
740
741 br_group.u.ip4 = group;
742 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000743 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900744
745 return br_multicast_add_group(br, port, &br_group);
746}
747
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000748#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900749static int br_ip6_multicast_add_group(struct net_bridge *br,
750 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000751 const struct in6_addr *group,
752 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900753{
754 struct br_ip br_group;
755
Linus Lüssing3c3769e2013-09-04 02:13:39 +0200756 if (ipv6_addr_is_ll_all_nodes(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900757 return 0;
758
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000759 br_group.u.ip6 = *group;
Linus Lüssing9cc6e0c2011-02-15 13:19:17 +0000760 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000761 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900762
763 return br_multicast_add_group(br, port, &br_group);
764}
765#endif
766
Herbert Xueb1d1642010-02-27 19:41:45 +0000767static void br_multicast_router_expired(unsigned long data)
768{
769 struct net_bridge_port *port = (void *)data;
770 struct net_bridge *br = port->br;
771
772 spin_lock(&br->multicast_lock);
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +0100773 if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
774 port->multicast_router == MDB_RTR_TYPE_PERM ||
Herbert Xueb1d1642010-02-27 19:41:45 +0000775 timer_pending(&port->multicast_router_timer) ||
776 hlist_unhashed(&port->rlist))
777 goto out;
778
779 hlist_del_init_rcu(&port->rlist);
Satish Ashok949f1e32015-07-23 05:00:53 -0700780 br_rtr_notify(br->dev, port, RTM_DELMDB);
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +0100781 /* Don't allow timer refresh if the router expired */
782 if (port->multicast_router == MDB_RTR_TYPE_TEMP)
783 port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Herbert Xueb1d1642010-02-27 19:41:45 +0000784
785out:
786 spin_unlock(&br->multicast_lock);
787}
788
789static void br_multicast_local_router_expired(unsigned long data)
790{
791}
792
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200793static void br_multicast_querier_expired(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +0200794 struct bridge_mcast_own_query *query)
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000795{
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000796 spin_lock(&br->multicast_lock);
797 if (!netif_running(br->dev) || br->multicast_disabled)
798 goto out;
799
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200800 br_multicast_start_querier(br, query);
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000801
802out:
803 spin_unlock(&br->multicast_lock);
804}
805
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200806static void br_ip4_multicast_querier_expired(unsigned long data)
807{
808 struct net_bridge *br = (void *)data;
809
Linus Lüssing90010b32014-06-07 18:26:26 +0200810 br_multicast_querier_expired(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200811}
812
813#if IS_ENABLED(CONFIG_IPV6)
814static void br_ip6_multicast_querier_expired(unsigned long data)
815{
816 struct net_bridge *br = (void *)data;
817
Linus Lüssing90010b32014-06-07 18:26:26 +0200818 br_multicast_querier_expired(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200819}
820#endif
821
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200822static void br_multicast_select_own_querier(struct net_bridge *br,
823 struct br_ip *ip,
824 struct sk_buff *skb)
825{
826 if (ip->proto == htons(ETH_P_IP))
827 br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr;
828#if IS_ENABLED(CONFIG_IPV6)
829 else
830 br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr;
831#endif
832}
833
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900834static void __br_multicast_send_query(struct net_bridge *br,
835 struct net_bridge_port *port,
836 struct br_ip *ip)
Herbert Xueb1d1642010-02-27 19:41:45 +0000837{
Herbert Xueb1d1642010-02-27 19:41:45 +0000838 struct sk_buff *skb;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200839 u8 igmp_type;
Herbert Xueb1d1642010-02-27 19:41:45 +0000840
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200841 skb = br_multicast_alloc_query(br, ip, &igmp_type);
Herbert Xueb1d1642010-02-27 19:41:45 +0000842 if (!skb)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900843 return;
Herbert Xueb1d1642010-02-27 19:41:45 +0000844
845 if (port) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000846 skb->dev = port->dev;
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -0700847 br_multicast_count(br, port, skb, igmp_type,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200848 BR_MCAST_DIR_TX);
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500849 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
850 dev_net(port->dev), NULL, skb, NULL, skb->dev,
Linus Lüssingf0b4eec2014-11-17 12:20:28 +0100851 br_dev_queue_push_xmit);
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200852 } else {
853 br_multicast_select_own_querier(br, ip, skb);
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -0700854 br_multicast_count(br, port, skb, igmp_type,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200855 BR_MCAST_DIR_RX);
Herbert Xueb1d1642010-02-27 19:41:45 +0000856 netif_rx(skb);
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200857 }
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900858}
Herbert Xueb1d1642010-02-27 19:41:45 +0000859
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900860static void br_multicast_send_query(struct net_bridge *br,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200861 struct net_bridge_port *port,
Linus Lüssing90010b32014-06-07 18:26:26 +0200862 struct bridge_mcast_own_query *own_query)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900863{
864 unsigned long time;
865 struct br_ip br_group;
Linus Lüssing90010b32014-06-07 18:26:26 +0200866 struct bridge_mcast_other_query *other_query = NULL;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900867
868 if (!netif_running(br->dev) || br->multicast_disabled ||
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200869 !br->multicast_querier)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900870 return;
871
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900872 memset(&br_group.u, 0, sizeof(br_group.u));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900873
Linus Lüssing90010b32014-06-07 18:26:26 +0200874 if (port ? (own_query == &port->ip4_own_query) :
875 (own_query == &br->ip4_own_query)) {
876 other_query = &br->ip4_other_query;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200877 br_group.proto = htons(ETH_P_IP);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000878#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200879 } else {
Linus Lüssing90010b32014-06-07 18:26:26 +0200880 other_query = &br->ip6_other_query;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200881 br_group.proto = htons(ETH_P_IPV6);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900882#endif
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200883 }
884
Linus Lüssing90010b32014-06-07 18:26:26 +0200885 if (!other_query || timer_pending(&other_query->timer))
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200886 return;
887
888 __br_multicast_send_query(br, port, &br_group);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900889
Herbert Xueb1d1642010-02-27 19:41:45 +0000890 time = jiffies;
Linus Lüssing90010b32014-06-07 18:26:26 +0200891 time += own_query->startup_sent < br->multicast_startup_query_count ?
Herbert Xueb1d1642010-02-27 19:41:45 +0000892 br->multicast_startup_query_interval :
893 br->multicast_query_interval;
Linus Lüssing90010b32014-06-07 18:26:26 +0200894 mod_timer(&own_query->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +0000895}
896
Linus Lüssing90010b32014-06-07 18:26:26 +0200897static void
898br_multicast_port_query_expired(struct net_bridge_port *port,
899 struct bridge_mcast_own_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +0000900{
Herbert Xueb1d1642010-02-27 19:41:45 +0000901 struct net_bridge *br = port->br;
902
903 spin_lock(&br->multicast_lock);
Dan Carpenter02a780c2010-03-06 01:14:09 +0000904 if (port->state == BR_STATE_DISABLED ||
905 port->state == BR_STATE_BLOCKING)
Herbert Xueb1d1642010-02-27 19:41:45 +0000906 goto out;
907
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200908 if (query->startup_sent < br->multicast_startup_query_count)
909 query->startup_sent++;
Herbert Xueb1d1642010-02-27 19:41:45 +0000910
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200911 br_multicast_send_query(port->br, port, query);
Herbert Xueb1d1642010-02-27 19:41:45 +0000912
913out:
914 spin_unlock(&br->multicast_lock);
915}
916
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200917static void br_ip4_multicast_port_query_expired(unsigned long data)
918{
919 struct net_bridge_port *port = (void *)data;
920
Linus Lüssing90010b32014-06-07 18:26:26 +0200921 br_multicast_port_query_expired(port, &port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200922}
923
924#if IS_ENABLED(CONFIG_IPV6)
925static void br_ip6_multicast_port_query_expired(unsigned long data)
926{
927 struct net_bridge_port *port = (void *)data;
928
Linus Lüssing90010b32014-06-07 18:26:26 +0200929 br_multicast_port_query_expired(port, &port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200930}
931#endif
932
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200933int br_multicast_add_port(struct net_bridge_port *port)
Herbert Xueb1d1642010-02-27 19:41:45 +0000934{
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +0100935 port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Herbert Xueb1d1642010-02-27 19:41:45 +0000936
937 setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
938 (unsigned long)port);
Linus Lüssing90010b32014-06-07 18:26:26 +0200939 setup_timer(&port->ip4_own_query.timer,
940 br_ip4_multicast_port_query_expired, (unsigned long)port);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200941#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +0200942 setup_timer(&port->ip6_own_query.timer,
943 br_ip6_multicast_port_query_expired, (unsigned long)port);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200944#endif
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200945 port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
946 if (!port->mcast_stats)
947 return -ENOMEM;
948
949 return 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000950}
951
952void br_multicast_del_port(struct net_bridge_port *port)
953{
Satish Ashoke10177a2015-07-15 07:16:51 -0700954 struct net_bridge *br = port->br;
955 struct net_bridge_port_group *pg;
956 struct hlist_node *n;
957
958 /* Take care of the remaining groups, only perm ones should be left */
959 spin_lock_bh(&br->multicast_lock);
960 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
961 br_multicast_del_pg(br, pg);
962 spin_unlock_bh(&br->multicast_lock);
Herbert Xueb1d1642010-02-27 19:41:45 +0000963 del_timer_sync(&port->multicast_router_timer);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200964 free_percpu(port->mcast_stats);
Herbert Xueb1d1642010-02-27 19:41:45 +0000965}
966
Linus Lüssing90010b32014-06-07 18:26:26 +0200967static void br_multicast_enable(struct bridge_mcast_own_query *query)
Herbert Xu561f1102010-02-27 19:41:50 +0000968{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200969 query->startup_sent = 0;
Herbert Xu561f1102010-02-27 19:41:50 +0000970
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200971 if (try_to_del_timer_sync(&query->timer) >= 0 ||
972 del_timer(&query->timer))
973 mod_timer(&query->timer, jiffies);
Herbert Xu561f1102010-02-27 19:41:50 +0000974}
975
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +0200976static void __br_multicast_enable_port(struct net_bridge_port *port)
Herbert Xueb1d1642010-02-27 19:41:45 +0000977{
978 struct net_bridge *br = port->br;
979
Herbert Xueb1d1642010-02-27 19:41:45 +0000980 if (br->multicast_disabled || !netif_running(br->dev))
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +0200981 return;
Herbert Xueb1d1642010-02-27 19:41:45 +0000982
Linus Lüssing90010b32014-06-07 18:26:26 +0200983 br_multicast_enable(&port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200984#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +0200985 br_multicast_enable(&port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200986#endif
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +0100987 if (port->multicast_router == MDB_RTR_TYPE_PERM &&
988 hlist_unhashed(&port->rlist))
Satish Ashok754bc542015-06-19 01:22:57 -0700989 br_multicast_add_router(br, port);
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +0200990}
Herbert Xueb1d1642010-02-27 19:41:45 +0000991
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +0200992void br_multicast_enable_port(struct net_bridge_port *port)
993{
994 struct net_bridge *br = port->br;
995
996 spin_lock(&br->multicast_lock);
997 __br_multicast_enable_port(port);
Herbert Xueb1d1642010-02-27 19:41:45 +0000998 spin_unlock(&br->multicast_lock);
999}
1000
1001void br_multicast_disable_port(struct net_bridge_port *port)
1002{
1003 struct net_bridge *br = port->br;
1004 struct net_bridge_port_group *pg;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001005 struct hlist_node *n;
Herbert Xueb1d1642010-02-27 19:41:45 +00001006
1007 spin_lock(&br->multicast_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001008 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
Elad Raz9d06b6d2016-02-03 09:57:05 +01001009 if (!(pg->flags & MDB_PG_FLAGS_PERMANENT))
Satish Ashoke10177a2015-07-15 07:16:51 -07001010 br_multicast_del_pg(br, pg);
Herbert Xueb1d1642010-02-27 19:41:45 +00001011
Satish Ashok949f1e32015-07-23 05:00:53 -07001012 if (!hlist_unhashed(&port->rlist)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001013 hlist_del_init_rcu(&port->rlist);
Satish Ashok949f1e32015-07-23 05:00:53 -07001014 br_rtr_notify(br->dev, port, RTM_DELMDB);
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001015 /* Don't allow timer refresh if disabling */
1016 if (port->multicast_router == MDB_RTR_TYPE_TEMP)
1017 port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Satish Ashok949f1e32015-07-23 05:00:53 -07001018 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001019 del_timer(&port->multicast_router_timer);
Linus Lüssing90010b32014-06-07 18:26:26 +02001020 del_timer(&port->ip4_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001021#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001022 del_timer(&port->ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001023#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001024 spin_unlock(&br->multicast_lock);
1025}
1026
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001027static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
1028 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001029 struct sk_buff *skb,
1030 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001031{
1032 struct igmpv3_report *ih;
1033 struct igmpv3_grec *grec;
1034 int i;
1035 int len;
1036 int num;
1037 int type;
1038 int err = 0;
1039 __be32 group;
1040
Herbert Xueb1d1642010-02-27 19:41:45 +00001041 ih = igmpv3_report_hdr(skb);
1042 num = ntohs(ih->ngrec);
Linus Lüssingc2d4fbd2015-09-11 18:39:48 +02001043 len = skb_transport_offset(skb) + sizeof(*ih);
Herbert Xueb1d1642010-02-27 19:41:45 +00001044
1045 for (i = 0; i < num; i++) {
1046 len += sizeof(*grec);
1047 if (!pskb_may_pull(skb, len))
1048 return -EINVAL;
1049
Herbert Xufd218cf2010-04-07 21:20:47 -07001050 grec = (void *)(skb->data + len - sizeof(*grec));
Herbert Xueb1d1642010-02-27 19:41:45 +00001051 group = grec->grec_mca;
1052 type = grec->grec_type;
1053
Eric Dumazet8eabf952010-04-20 03:20:05 +00001054 len += ntohs(grec->grec_nsrcs) * 4;
Herbert Xueb1d1642010-02-27 19:41:45 +00001055 if (!pskb_may_pull(skb, len))
1056 return -EINVAL;
1057
1058 /* We treat this as an IGMPv2 report for now. */
1059 switch (type) {
1060 case IGMPV3_MODE_IS_INCLUDE:
1061 case IGMPV3_MODE_IS_EXCLUDE:
1062 case IGMPV3_CHANGE_TO_INCLUDE:
1063 case IGMPV3_CHANGE_TO_EXCLUDE:
1064 case IGMPV3_ALLOW_NEW_SOURCES:
1065 case IGMPV3_BLOCK_OLD_SOURCES:
1066 break;
1067
1068 default:
1069 continue;
1070 }
1071
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001072 if ((type == IGMPV3_CHANGE_TO_INCLUDE ||
1073 type == IGMPV3_MODE_IS_INCLUDE) &&
1074 ntohs(grec->grec_nsrcs) == 0) {
1075 br_ip4_multicast_leave_group(br, port, group, vid);
1076 } else {
1077 err = br_ip4_multicast_add_group(br, port, group, vid);
1078 if (err)
1079 break;
1080 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001081 }
1082
1083 return err;
1084}
1085
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001086#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001087static int br_ip6_multicast_mld2_report(struct net_bridge *br,
1088 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001089 struct sk_buff *skb,
1090 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001091{
1092 struct icmp6hdr *icmp6h;
1093 struct mld2_grec *grec;
1094 int i;
1095 int len;
1096 int num;
1097 int err = 0;
1098
1099 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
1100 return -EINVAL;
1101
1102 icmp6h = icmp6_hdr(skb);
1103 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
Linus Lüssingc2d4fbd2015-09-11 18:39:48 +02001104 len = skb_transport_offset(skb) + sizeof(*icmp6h);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001105
1106 for (i = 0; i < num; i++) {
1107 __be16 *nsrcs, _nsrcs;
1108
1109 nsrcs = skb_header_pointer(skb,
1110 len + offsetof(struct mld2_grec,
Linus Lüssing649e9842011-02-15 13:19:18 +00001111 grec_nsrcs),
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001112 sizeof(_nsrcs), &_nsrcs);
1113 if (!nsrcs)
1114 return -EINVAL;
1115
1116 if (!pskb_may_pull(skb,
1117 len + sizeof(*grec) +
Linus Lüssingd41db9f2011-02-15 13:19:19 +00001118 sizeof(struct in6_addr) * ntohs(*nsrcs)))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001119 return -EINVAL;
1120
1121 grec = (struct mld2_grec *)(skb->data + len);
Linus Lüssingd41db9f2011-02-15 13:19:19 +00001122 len += sizeof(*grec) +
1123 sizeof(struct in6_addr) * ntohs(*nsrcs);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001124
1125 /* We treat these as MLDv1 reports for now. */
1126 switch (grec->grec_type) {
1127 case MLD2_MODE_IS_INCLUDE:
1128 case MLD2_MODE_IS_EXCLUDE:
1129 case MLD2_CHANGE_TO_INCLUDE:
1130 case MLD2_CHANGE_TO_EXCLUDE:
1131 case MLD2_ALLOW_NEW_SOURCES:
1132 case MLD2_BLOCK_OLD_SOURCES:
1133 break;
1134
1135 default:
1136 continue;
1137 }
1138
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001139 if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE ||
1140 grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
1141 ntohs(*nsrcs) == 0) {
1142 br_ip6_multicast_leave_group(br, port, &grec->grec_mca,
1143 vid);
1144 } else {
1145 err = br_ip6_multicast_add_group(br, port,
1146 &grec->grec_mca, vid);
Davide Caratti92642512016-08-31 14:16:44 +02001147 if (err)
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001148 break;
1149 }
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001150 }
1151
1152 return err;
1153}
1154#endif
1155
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001156static bool br_ip4_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001157 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001158 __be32 saddr)
1159{
1160 if (!timer_pending(&br->ip4_own_query.timer) &&
1161 !timer_pending(&br->ip4_other_query.timer))
1162 goto update;
1163
1164 if (!br->ip4_querier.addr.u.ip4)
1165 goto update;
1166
1167 if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4))
1168 goto update;
1169
1170 return false;
1171
1172update:
1173 br->ip4_querier.addr.u.ip4 = saddr;
1174
Linus Lüssing2cd41432014-06-07 18:26:29 +02001175 /* update protected by general multicast_lock by caller */
1176 rcu_assign_pointer(br->ip4_querier.port, port);
1177
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001178 return true;
1179}
1180
1181#if IS_ENABLED(CONFIG_IPV6)
1182static bool br_ip6_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001183 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001184 struct in6_addr *saddr)
1185{
1186 if (!timer_pending(&br->ip6_own_query.timer) &&
1187 !timer_pending(&br->ip6_other_query.timer))
1188 goto update;
1189
1190 if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0)
1191 goto update;
1192
1193 return false;
1194
1195update:
1196 br->ip6_querier.addr.u.ip6 = *saddr;
1197
Linus Lüssing2cd41432014-06-07 18:26:29 +02001198 /* update protected by general multicast_lock by caller */
1199 rcu_assign_pointer(br->ip6_querier.port, port);
1200
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001201 return true;
1202}
1203#endif
1204
1205static bool br_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001206 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001207 struct br_ip *saddr)
1208{
1209 switch (saddr->proto) {
1210 case htons(ETH_P_IP):
Linus Lüssing2cd41432014-06-07 18:26:29 +02001211 return br_ip4_multicast_select_querier(br, port, saddr->u.ip4);
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001212#if IS_ENABLED(CONFIG_IPV6)
1213 case htons(ETH_P_IPV6):
Linus Lüssing2cd41432014-06-07 18:26:29 +02001214 return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6);
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001215#endif
1216 }
1217
1218 return false;
1219}
1220
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001221static void
Linus Lüssing90010b32014-06-07 18:26:26 +02001222br_multicast_update_query_timer(struct net_bridge *br,
1223 struct bridge_mcast_other_query *query,
1224 unsigned long max_delay)
Linus Lüssingb00589a2013-08-01 01:06:20 +02001225{
Linus Lüssing90010b32014-06-07 18:26:26 +02001226 if (!timer_pending(&query->timer))
1227 query->delay_time = jiffies + max_delay;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001228
Linus Lüssing90010b32014-06-07 18:26:26 +02001229 mod_timer(&query->timer, jiffies + br->multicast_querier_interval);
Linus Lüssingb00589a2013-08-01 01:06:20 +02001230}
1231
stephen hemminger7e80c122010-04-27 15:01:04 +00001232/*
Cong Wang7c776022013-06-21 15:37:25 +08001233 * Add port to router_list
stephen hemminger7e80c122010-04-27 15:01:04 +00001234 * list is maintained ordered by pointer value
1235 * and locked by br->multicast_lock and RCU
1236 */
Herbert Xu0909e112010-02-27 19:41:49 +00001237static void br_multicast_add_router(struct net_bridge *br,
1238 struct net_bridge_port *port)
1239{
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001240 struct net_bridge_port *p;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001241 struct hlist_node *slot = NULL;
Herbert Xu0909e112010-02-27 19:41:49 +00001242
Nikolay Aleksandrov1a040ea2015-06-09 10:23:57 -07001243 if (!hlist_unhashed(&port->rlist))
1244 return;
1245
Sasha Levinb67bfe02013-02-27 17:06:00 -08001246 hlist_for_each_entry(p, &br->router_list, rlist) {
stephen hemminger7e80c122010-04-27 15:01:04 +00001247 if ((unsigned long) port >= (unsigned long) p)
1248 break;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001249 slot = &p->rlist;
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001250 }
Herbert Xu0909e112010-02-27 19:41:49 +00001251
stephen hemminger7e80c122010-04-27 15:01:04 +00001252 if (slot)
Ken Helias1d023282014-08-06 16:09:16 -07001253 hlist_add_behind_rcu(&port->rlist, slot);
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001254 else
1255 hlist_add_head_rcu(&port->rlist, &br->router_list);
Satish Ashok949f1e32015-07-23 05:00:53 -07001256 br_rtr_notify(br->dev, port, RTM_NEWMDB);
Herbert Xu0909e112010-02-27 19:41:49 +00001257}
1258
Herbert Xueb1d1642010-02-27 19:41:45 +00001259static void br_multicast_mark_router(struct net_bridge *br,
1260 struct net_bridge_port *port)
1261{
1262 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +00001263
1264 if (!port) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001265 if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY)
Herbert Xueb1d1642010-02-27 19:41:45 +00001266 mod_timer(&br->multicast_router_timer,
1267 now + br->multicast_querier_interval);
1268 return;
1269 }
1270
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001271 if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
1272 port->multicast_router == MDB_RTR_TYPE_PERM)
Herbert Xueb1d1642010-02-27 19:41:45 +00001273 return;
1274
Herbert Xu0909e112010-02-27 19:41:49 +00001275 br_multicast_add_router(br, port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001276
Herbert Xueb1d1642010-02-27 19:41:45 +00001277 mod_timer(&port->multicast_router_timer,
1278 now + br->multicast_querier_interval);
1279}
1280
1281static void br_multicast_query_received(struct net_bridge *br,
1282 struct net_bridge_port *port,
Linus Lüssing90010b32014-06-07 18:26:26 +02001283 struct bridge_mcast_other_query *query,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001284 struct br_ip *saddr,
Linus Lüssingb00589a2013-08-01 01:06:20 +02001285 unsigned long max_delay)
Herbert Xueb1d1642010-02-27 19:41:45 +00001286{
Linus Lüssing2cd41432014-06-07 18:26:29 +02001287 if (!br_multicast_select_querier(br, port, saddr))
Herbert Xueb1d1642010-02-27 19:41:45 +00001288 return;
1289
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001290 br_multicast_update_query_timer(br, query, max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001291 br_multicast_mark_router(br, port);
1292}
1293
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001294static int br_ip4_multicast_query(struct net_bridge *br,
1295 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001296 struct sk_buff *skb,
1297 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001298{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001299 const struct iphdr *iph = ip_hdr(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001300 struct igmphdr *ih = igmp_hdr(skb);
1301 struct net_bridge_mdb_entry *mp;
1302 struct igmpv3_query *ih3;
1303 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +00001304 struct net_bridge_port_group __rcu **pp;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001305 struct br_ip saddr;
Herbert Xueb1d1642010-02-27 19:41:45 +00001306 unsigned long max_delay;
1307 unsigned long now = jiffies;
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001308 unsigned int offset = skb_transport_offset(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001309 __be32 group;
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001310 int err = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001311
1312 spin_lock(&br->multicast_lock);
1313 if (!netif_running(br->dev) ||
1314 (port && port->state == BR_STATE_DISABLED))
1315 goto out;
1316
Herbert Xueb1d1642010-02-27 19:41:45 +00001317 group = ih->group;
1318
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001319 if (skb->len == offset + sizeof(*ih)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001320 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1321
1322 if (!max_delay) {
1323 max_delay = 10 * HZ;
1324 group = 0;
1325 }
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001326 } else if (skb->len >= offset + sizeof(*ih3)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001327 ih3 = igmpv3_query_hdr(skb);
1328 if (ih3->nsrcs)
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001329 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001330
YOSHIFUJI Hideaki / 吉藤英明0ba8c9e2010-03-15 19:27:00 +00001331 max_delay = ih3->code ?
1332 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001333 } else {
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001334 goto out;
1335 }
1336
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001337 if (!group) {
1338 saddr.proto = htons(ETH_P_IP);
1339 saddr.u.ip4 = iph->saddr;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001340
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001341 br_multicast_query_received(br, port, &br->ip4_other_query,
1342 &saddr, max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001343 goto out;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001344 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001345
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001346 mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001347 if (!mp)
1348 goto out;
1349
1350 max_delay *= br->multicast_last_member_count;
1351
Herbert Xu8a870172011-02-12 01:05:42 -08001352 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001353 (timer_pending(&mp->timer) ?
1354 time_after(mp->timer.expires, now + max_delay) :
1355 try_to_del_timer_sync(&mp->timer) >= 0))
1356 mod_timer(&mp->timer, now + max_delay);
1357
Eric Dumazete8051682010-11-15 06:38:10 +00001358 for (pp = &mp->ports;
1359 (p = mlock_dereference(*pp, br)) != NULL;
1360 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001361 if (timer_pending(&p->timer) ?
1362 time_after(p->timer.expires, now + max_delay) :
1363 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001364 mod_timer(&p->timer, now + max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001365 }
1366
1367out:
1368 spin_unlock(&br->multicast_lock);
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001369 return err;
Herbert Xueb1d1642010-02-27 19:41:45 +00001370}
1371
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001372#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001373static int br_ip6_multicast_query(struct net_bridge *br,
1374 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001375 struct sk_buff *skb,
1376 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001377{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001378 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
stephen hemmingereca2a432012-12-13 06:51:28 +00001379 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001380 struct net_bridge_mdb_entry *mp;
1381 struct mld2_query *mld2q;
Eric Dumazete8051682010-11-15 06:38:10 +00001382 struct net_bridge_port_group *p;
1383 struct net_bridge_port_group __rcu **pp;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001384 struct br_ip saddr;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001385 unsigned long max_delay;
1386 unsigned long now = jiffies;
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001387 unsigned int offset = skb_transport_offset(skb);
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001388 const struct in6_addr *group = NULL;
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001389 bool is_general_query;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001390 int err = 0;
1391
1392 spin_lock(&br->multicast_lock);
1393 if (!netif_running(br->dev) ||
1394 (port && port->state == BR_STATE_DISABLED))
1395 goto out;
1396
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001397 if (skb->len == offset + sizeof(*mld)) {
1398 if (!pskb_may_pull(skb, offset + sizeof(*mld))) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001399 err = -EINVAL;
1400 goto out;
1401 }
1402 mld = (struct mld_msg *) icmp6_hdr(skb);
Li RongQing47152132012-07-09 23:56:12 +00001403 max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001404 if (max_delay)
1405 group = &mld->mld_mca;
Linus Lüssing248ba8e2013-08-06 00:32:05 +02001406 } else {
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001407 if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001408 err = -EINVAL;
1409 goto out;
1410 }
1411 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1412 if (!mld2q->mld2q_nsrcs)
1413 group = &mld2q->mld2q_mca;
Daniel Borkmanne3f5b172013-09-04 00:19:39 +02001414
1415 max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001416 }
1417
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001418 is_general_query = group && ipv6_addr_any(group);
1419
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001420 if (is_general_query) {
1421 saddr.proto = htons(ETH_P_IPV6);
1422 saddr.u.ip6 = ip6h->saddr;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001423
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001424 br_multicast_query_received(br, port, &br->ip6_other_query,
1425 &saddr, max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001426 goto out;
Linus Lüssing6c03ee82014-06-12 01:41:23 +02001427 } else if (!group) {
1428 goto out;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001429 }
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001430
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001431 mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001432 if (!mp)
1433 goto out;
1434
1435 max_delay *= br->multicast_last_member_count;
Herbert Xu8a870172011-02-12 01:05:42 -08001436 if (mp->mglist &&
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001437 (timer_pending(&mp->timer) ?
1438 time_after(mp->timer.expires, now + max_delay) :
1439 try_to_del_timer_sync(&mp->timer) >= 0))
1440 mod_timer(&mp->timer, now + max_delay);
1441
Eric Dumazete8051682010-11-15 06:38:10 +00001442 for (pp = &mp->ports;
1443 (p = mlock_dereference(*pp, br)) != NULL;
1444 pp = &p->next) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001445 if (timer_pending(&p->timer) ?
1446 time_after(p->timer.expires, now + max_delay) :
1447 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001448 mod_timer(&p->timer, now + max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001449 }
1450
1451out:
1452 spin_unlock(&br->multicast_lock);
1453 return err;
1454}
1455#endif
1456
Linus Lüssing90010b32014-06-07 18:26:26 +02001457static void
1458br_multicast_leave_group(struct net_bridge *br,
1459 struct net_bridge_port *port,
1460 struct br_ip *group,
1461 struct bridge_mcast_other_query *other_query,
1462 struct bridge_mcast_own_query *own_query)
Herbert Xueb1d1642010-02-27 19:41:45 +00001463{
1464 struct net_bridge_mdb_htable *mdb;
1465 struct net_bridge_mdb_entry *mp;
1466 struct net_bridge_port_group *p;
1467 unsigned long now;
1468 unsigned long time;
1469
Herbert Xueb1d1642010-02-27 19:41:45 +00001470 spin_lock(&br->multicast_lock);
1471 if (!netif_running(br->dev) ||
Satish Ashok544586f2015-07-28 03:28:27 -07001472 (port && port->state == BR_STATE_DISABLED))
Herbert Xueb1d1642010-02-27 19:41:45 +00001473 goto out;
1474
Eric Dumazete8051682010-11-15 06:38:10 +00001475 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001476 mp = br_mdb_ip_get(mdb, group);
1477 if (!mp)
1478 goto out;
1479
Satish Ashok544586f2015-07-28 03:28:27 -07001480 if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
1481 struct net_bridge_port_group __rcu **pp;
1482
1483 for (pp = &mp->ports;
1484 (p = mlock_dereference(*pp, br)) != NULL;
1485 pp = &p->next) {
1486 if (p->port != port)
1487 continue;
1488
1489 rcu_assign_pointer(*pp, p->next);
1490 hlist_del_init(&p->mglist);
1491 del_timer(&p->timer);
1492 call_rcu_bh(&p->rcu, br_multicast_free_pg);
Elad Raz45ebcce2016-04-21 12:52:45 +02001493 br_mdb_notify(br->dev, port, group, RTM_DELMDB,
1494 p->flags);
Satish Ashok544586f2015-07-28 03:28:27 -07001495
1496 if (!mp->ports && !mp->mglist &&
1497 netif_running(br->dev))
1498 mod_timer(&mp->timer, jiffies);
1499 }
1500 goto out;
1501 }
1502
1503 if (timer_pending(&other_query->timer))
1504 goto out;
1505
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001506 if (br->multicast_querier) {
Cong Wang6b7df112013-05-21 21:52:56 +00001507 __br_multicast_send_query(br, port, &mp->addr);
1508
1509 time = jiffies + br->multicast_last_member_count *
1510 br->multicast_last_member_interval;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001511
Linus Lüssing90010b32014-06-07 18:26:26 +02001512 mod_timer(&own_query->timer, time);
Cong Wang6b7df112013-05-21 21:52:56 +00001513
1514 for (p = mlock_dereference(mp->ports, br);
1515 p != NULL;
1516 p = mlock_dereference(p->next, br)) {
1517 if (p->port != port)
1518 continue;
1519
1520 if (!hlist_unhashed(&p->mglist) &&
1521 (timer_pending(&p->timer) ?
1522 time_after(p->timer.expires, time) :
1523 try_to_del_timer_sync(&p->timer) >= 0)) {
1524 mod_timer(&p->timer, time);
1525 }
1526
1527 break;
1528 }
1529 }
1530
Herbert Xueb1d1642010-02-27 19:41:45 +00001531 now = jiffies;
1532 time = now + br->multicast_last_member_count *
1533 br->multicast_last_member_interval;
1534
1535 if (!port) {
Linus Lüssing454594f2013-10-20 00:58:57 +02001536 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001537 (timer_pending(&mp->timer) ?
1538 time_after(mp->timer.expires, time) :
1539 try_to_del_timer_sync(&mp->timer) >= 0)) {
1540 mod_timer(&mp->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +00001541 }
Linus Lüssing454594f2013-10-20 00:58:57 +02001542
1543 goto out;
1544 }
1545
1546 for (p = mlock_dereference(mp->ports, br);
1547 p != NULL;
1548 p = mlock_dereference(p->next, br)) {
1549 if (p->port != port)
1550 continue;
1551
1552 if (!hlist_unhashed(&p->mglist) &&
1553 (timer_pending(&p->timer) ?
1554 time_after(p->timer.expires, time) :
1555 try_to_del_timer_sync(&p->timer) >= 0)) {
1556 mod_timer(&p->timer, time);
1557 }
1558
1559 break;
Herbert Xueb1d1642010-02-27 19:41:45 +00001560 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001561out:
1562 spin_unlock(&br->multicast_lock);
1563}
1564
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001565static void br_ip4_multicast_leave_group(struct net_bridge *br,
1566 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001567 __be32 group,
1568 __u16 vid)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001569{
1570 struct br_ip br_group;
Linus Lüssing90010b32014-06-07 18:26:26 +02001571 struct bridge_mcast_own_query *own_query;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001572
1573 if (ipv4_is_local_multicast(group))
1574 return;
1575
Linus Lüssing90010b32014-06-07 18:26:26 +02001576 own_query = port ? &port->ip4_own_query : &br->ip4_own_query;
1577
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001578 br_group.u.ip4 = group;
1579 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001580 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001581
Linus Lüssing90010b32014-06-07 18:26:26 +02001582 br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
1583 own_query);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001584}
1585
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001586#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001587static void br_ip6_multicast_leave_group(struct net_bridge *br,
1588 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001589 const struct in6_addr *group,
1590 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001591{
1592 struct br_ip br_group;
Linus Lüssing90010b32014-06-07 18:26:26 +02001593 struct bridge_mcast_own_query *own_query;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001594
Linus Lüssing3c3769e2013-09-04 02:13:39 +02001595 if (ipv6_addr_is_ll_all_nodes(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001596 return;
1597
Linus Lüssing90010b32014-06-07 18:26:26 +02001598 own_query = port ? &port->ip6_own_query : &br->ip6_own_query;
1599
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001600 br_group.u.ip6 = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001601 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001602 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001603
Linus Lüssing90010b32014-06-07 18:26:26 +02001604 br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
1605 own_query);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001606}
1607#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001608
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001609static void br_multicast_err_count(const struct net_bridge *br,
1610 const struct net_bridge_port *p,
1611 __be16 proto)
1612{
1613 struct bridge_mcast_stats __percpu *stats;
1614 struct bridge_mcast_stats *pstats;
1615
1616 if (!br->multicast_stats_enabled)
1617 return;
1618
1619 if (p)
1620 stats = p->mcast_stats;
1621 else
1622 stats = br->mcast_stats;
1623 if (WARN_ON(!stats))
1624 return;
1625
1626 pstats = this_cpu_ptr(stats);
1627
1628 u64_stats_update_begin(&pstats->syncp);
1629 switch (proto) {
1630 case htons(ETH_P_IP):
1631 pstats->mstats.igmp_parse_errors++;
1632 break;
1633#if IS_ENABLED(CONFIG_IPV6)
1634 case htons(ETH_P_IPV6):
1635 pstats->mstats.mld_parse_errors++;
1636 break;
1637#endif
1638 }
1639 u64_stats_update_end(&pstats->syncp);
1640}
1641
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001642static void br_multicast_pim(struct net_bridge *br,
1643 struct net_bridge_port *port,
1644 const struct sk_buff *skb)
1645{
1646 unsigned int offset = skb_transport_offset(skb);
1647 struct pimhdr *pimhdr, _pimhdr;
1648
1649 pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr);
1650 if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION ||
1651 pim_hdr_type(pimhdr) != PIM_TYPE_HELLO)
1652 return;
1653
1654 br_multicast_mark_router(br, port);
1655}
1656
Herbert Xueb1d1642010-02-27 19:41:45 +00001657static int br_multicast_ipv4_rcv(struct net_bridge *br,
1658 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001659 struct sk_buff *skb,
1660 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001661{
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001662 struct sk_buff *skb_trimmed = NULL;
Herbert Xueb1d1642010-02-27 19:41:45 +00001663 struct igmphdr *ih;
Herbert Xueb1d1642010-02-27 19:41:45 +00001664 int err;
1665
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001666 err = ip_mc_check_igmp(skb, &skb_trimmed);
Herbert Xueb1d1642010-02-27 19:41:45 +00001667
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001668 if (err == -ENOMSG) {
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001669 if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) {
Herbert Xubd4265f2011-06-23 02:39:12 +00001670 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Nikolay Aleksandrov91b02d32016-10-31 13:21:05 +01001671 } else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) {
1672 if (ip_hdr(skb)->protocol == IPPROTO_PIM)
1673 br_multicast_pim(br, port, skb);
1674 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001675 return 0;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001676 } else if (err < 0) {
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001677 br_multicast_err_count(br, port, skb->protocol);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001678 return err;
Herbert Xubd4265f2011-06-23 02:39:12 +00001679 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001680
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001681 ih = igmp_hdr(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001682 BR_INPUT_SKB_CB(skb)->igmp = ih->type;
Herbert Xueb1d1642010-02-27 19:41:45 +00001683
1684 switch (ih->type) {
1685 case IGMP_HOST_MEMBERSHIP_REPORT:
1686 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Fernando Luis Vázquez Cao62b2bcb2011-06-13 15:04:43 +00001687 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001688 err = br_ip4_multicast_add_group(br, port, ih->group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001689 break;
1690 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001691 err = br_ip4_multicast_igmp3_report(br, port, skb_trimmed, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001692 break;
1693 case IGMP_HOST_MEMBERSHIP_QUERY:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001694 err = br_ip4_multicast_query(br, port, skb_trimmed, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001695 break;
1696 case IGMP_HOST_LEAVE_MESSAGE:
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001697 br_ip4_multicast_leave_group(br, port, ih->group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001698 break;
1699 }
1700
Linus Lüssinga5169932015-08-13 05:54:07 +02001701 if (skb_trimmed && skb_trimmed != skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001702 kfree_skb(skb_trimmed);
1703
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07001704 br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001705 BR_MCAST_DIR_RX);
1706
Herbert Xueb1d1642010-02-27 19:41:45 +00001707 return err;
1708}
1709
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001710#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001711static int br_multicast_ipv6_rcv(struct net_bridge *br,
1712 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001713 struct sk_buff *skb,
1714 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001715{
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001716 struct sk_buff *skb_trimmed = NULL;
1717 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001718 int err;
1719
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001720 err = ipv6_mc_check_mld(skb, &skb_trimmed);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001721
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001722 if (err == -ENOMSG) {
1723 if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
1724 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Linus Lüssing8fad9c32013-09-04 02:13:38 +02001725 return 0;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001726 } else if (err < 0) {
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001727 br_multicast_err_count(br, port, skb->protocol);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001728 return err;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001729 }
1730
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001731 mld = (struct mld_msg *)skb_transport_header(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001732 BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001733
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001734 switch (mld->mld_type) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001735 case ICMPV6_MGM_REPORT:
Fernando Luis Vázquez Caofc2af6c2011-06-13 15:06:58 +00001736 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001737 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001738 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001739 case ICMPV6_MLD2_REPORT:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001740 err = br_ip6_multicast_mld2_report(br, port, skb_trimmed, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001741 break;
1742 case ICMPV6_MGM_QUERY:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001743 err = br_ip6_multicast_query(br, port, skb_trimmed, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001744 break;
1745 case ICMPV6_MGM_REDUCTION:
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001746 br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001747 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001748 }
1749
Linus Lüssinga5169932015-08-13 05:54:07 +02001750 if (skb_trimmed && skb_trimmed != skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001751 kfree_skb(skb_trimmed);
1752
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07001753 br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001754 BR_MCAST_DIR_RX);
1755
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001756 return err;
1757}
1758#endif
1759
Herbert Xueb1d1642010-02-27 19:41:45 +00001760int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001761 struct sk_buff *skb, u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001762{
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001763 int ret = 0;
1764
YOSHIFUJI Hideaki / 吉藤英明1fafc7a2010-04-25 08:06:40 +00001765 BR_INPUT_SKB_CB(skb)->igmp = 0;
1766 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1767
Herbert Xueb1d1642010-02-27 19:41:45 +00001768 if (br->multicast_disabled)
1769 return 0;
1770
1771 switch (skb->protocol) {
1772 case htons(ETH_P_IP):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001773 ret = br_multicast_ipv4_rcv(br, port, skb, vid);
1774 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001775#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001776 case htons(ETH_P_IPV6):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001777 ret = br_multicast_ipv6_rcv(br, port, skb, vid);
1778 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001779#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001780 }
1781
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001782 return ret;
Herbert Xueb1d1642010-02-27 19:41:45 +00001783}
1784
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001785static void br_multicast_query_expired(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001786 struct bridge_mcast_own_query *query,
1787 struct bridge_mcast_querier *querier)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001788{
1789 spin_lock(&br->multicast_lock);
1790 if (query->startup_sent < br->multicast_startup_query_count)
1791 query->startup_sent++;
1792
Eric Dumazet71d9f612015-05-28 04:42:54 -07001793 RCU_INIT_POINTER(querier->port, NULL);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001794 br_multicast_send_query(br, NULL, query);
1795 spin_unlock(&br->multicast_lock);
1796}
1797
1798static void br_ip4_multicast_query_expired(unsigned long data)
Herbert Xueb1d1642010-02-27 19:41:45 +00001799{
1800 struct net_bridge *br = (void *)data;
1801
Linus Lüssing2cd41432014-06-07 18:26:29 +02001802 br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier);
Herbert Xueb1d1642010-02-27 19:41:45 +00001803}
1804
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001805#if IS_ENABLED(CONFIG_IPV6)
1806static void br_ip6_multicast_query_expired(unsigned long data)
1807{
1808 struct net_bridge *br = (void *)data;
1809
Linus Lüssing2cd41432014-06-07 18:26:29 +02001810 br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001811}
1812#endif
1813
Herbert Xueb1d1642010-02-27 19:41:45 +00001814void br_multicast_init(struct net_bridge *br)
1815{
1816 br->hash_elasticity = 4;
1817 br->hash_max = 512;
1818
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001819 br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Herbert Xuc5c23262012-04-13 02:37:42 +00001820 br->multicast_querier = 0;
Cong Wang1c8ad5b2013-05-21 21:52:54 +00001821 br->multicast_query_use_ifaddr = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001822 br->multicast_last_member_count = 2;
1823 br->multicast_startup_query_count = 2;
1824
1825 br->multicast_last_member_interval = HZ;
1826 br->multicast_query_response_interval = 10 * HZ;
1827 br->multicast_startup_query_interval = 125 * HZ / 4;
1828 br->multicast_query_interval = 125 * HZ;
1829 br->multicast_querier_interval = 255 * HZ;
1830 br->multicast_membership_interval = 260 * HZ;
1831
Linus Lüssing90010b32014-06-07 18:26:26 +02001832 br->ip4_other_query.delay_time = 0;
Linus Lüssing2cd41432014-06-07 18:26:29 +02001833 br->ip4_querier.port = NULL;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001834#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001835 br->ip6_other_query.delay_time = 0;
Linus Lüssing2cd41432014-06-07 18:26:29 +02001836 br->ip6_querier.port = NULL;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001837#endif
daniel0888d5f2016-06-24 12:35:18 +02001838 br->has_ipv6_addr = 1;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001839
Herbert Xueb1d1642010-02-27 19:41:45 +00001840 spin_lock_init(&br->multicast_lock);
1841 setup_timer(&br->multicast_router_timer,
1842 br_multicast_local_router_expired, 0);
Linus Lüssing90010b32014-06-07 18:26:26 +02001843 setup_timer(&br->ip4_other_query.timer,
1844 br_ip4_multicast_querier_expired, (unsigned long)br);
1845 setup_timer(&br->ip4_own_query.timer, br_ip4_multicast_query_expired,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001846 (unsigned long)br);
1847#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001848 setup_timer(&br->ip6_other_query.timer,
1849 br_ip6_multicast_querier_expired, (unsigned long)br);
1850 setup_timer(&br->ip6_own_query.timer, br_ip6_multicast_query_expired,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001851 (unsigned long)br);
1852#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001853}
1854
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001855static void __br_multicast_open(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +02001856 struct bridge_mcast_own_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +00001857{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001858 query->startup_sent = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001859
1860 if (br->multicast_disabled)
1861 return;
1862
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001863 mod_timer(&query->timer, jiffies);
1864}
1865
1866void br_multicast_open(struct net_bridge *br)
1867{
Linus Lüssing90010b32014-06-07 18:26:26 +02001868 __br_multicast_open(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001869#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001870 __br_multicast_open(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001871#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001872}
1873
1874void br_multicast_stop(struct net_bridge *br)
1875{
Herbert Xueb1d1642010-02-27 19:41:45 +00001876 del_timer_sync(&br->multicast_router_timer);
Linus Lüssing90010b32014-06-07 18:26:26 +02001877 del_timer_sync(&br->ip4_other_query.timer);
1878 del_timer_sync(&br->ip4_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001879#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001880 del_timer_sync(&br->ip6_other_query.timer);
1881 del_timer_sync(&br->ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001882#endif
Satish Ashoke10177a2015-07-15 07:16:51 -07001883}
1884
1885void br_multicast_dev_del(struct net_bridge *br)
1886{
1887 struct net_bridge_mdb_htable *mdb;
1888 struct net_bridge_mdb_entry *mp;
1889 struct hlist_node *n;
1890 u32 ver;
1891 int i;
Herbert Xueb1d1642010-02-27 19:41:45 +00001892
1893 spin_lock_bh(&br->multicast_lock);
Eric Dumazete8051682010-11-15 06:38:10 +00001894 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001895 if (!mdb)
1896 goto out;
1897
1898 br->mdb = NULL;
1899
1900 ver = mdb->ver;
1901 for (i = 0; i < mdb->max; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001902 hlist_for_each_entry_safe(mp, n, &mdb->mhash[i],
Herbert Xueb1d1642010-02-27 19:41:45 +00001903 hlist[ver]) {
1904 del_timer(&mp->timer);
Herbert Xueb1d1642010-02-27 19:41:45 +00001905 call_rcu_bh(&mp->rcu, br_multicast_free_group);
1906 }
1907 }
1908
1909 if (mdb->old) {
1910 spin_unlock_bh(&br->multicast_lock);
Herbert Xu10cc2b52010-03-05 21:03:35 +00001911 rcu_barrier_bh();
Herbert Xueb1d1642010-02-27 19:41:45 +00001912 spin_lock_bh(&br->multicast_lock);
1913 WARN_ON(mdb->old);
1914 }
1915
1916 mdb->old = mdb;
1917 call_rcu_bh(&mdb->rcu, br_mdb_free);
1918
1919out:
1920 spin_unlock_bh(&br->multicast_lock);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001921
1922 free_percpu(br->mcast_stats);
Herbert Xueb1d1642010-02-27 19:41:45 +00001923}
Herbert Xu0909e112010-02-27 19:41:49 +00001924
1925int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1926{
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02001927 int err = -EINVAL;
Herbert Xu0909e112010-02-27 19:41:49 +00001928
1929 spin_lock_bh(&br->multicast_lock);
Herbert Xu0909e112010-02-27 19:41:49 +00001930
1931 switch (val) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001932 case MDB_RTR_TYPE_DISABLED:
1933 case MDB_RTR_TYPE_PERM:
Herbert Xu0909e112010-02-27 19:41:49 +00001934 del_timer(&br->multicast_router_timer);
1935 /* fall through */
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001936 case MDB_RTR_TYPE_TEMP_QUERY:
Herbert Xu0909e112010-02-27 19:41:49 +00001937 br->multicast_router = val;
1938 err = 0;
1939 break;
Herbert Xu0909e112010-02-27 19:41:49 +00001940 }
1941
Herbert Xu0909e112010-02-27 19:41:49 +00001942 spin_unlock_bh(&br->multicast_lock);
1943
1944 return err;
1945}
1946
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001947static void __del_port_router(struct net_bridge_port *p)
1948{
1949 if (hlist_unhashed(&p->rlist))
1950 return;
1951 hlist_del_init_rcu(&p->rlist);
1952 br_rtr_notify(p->br->dev, p, RTM_DELMDB);
1953}
1954
Herbert Xu0909e112010-02-27 19:41:49 +00001955int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1956{
1957 struct net_bridge *br = p->br;
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001958 unsigned long now = jiffies;
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02001959 int err = -EINVAL;
Herbert Xu0909e112010-02-27 19:41:49 +00001960
1961 spin_lock(&br->multicast_lock);
Nikolay Aleksandrov4950cfd2016-02-26 21:20:02 +01001962 if (p->multicast_router == val) {
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001963 /* Refresh the temp router port timer */
1964 if (p->multicast_router == MDB_RTR_TYPE_TEMP)
1965 mod_timer(&p->multicast_router_timer,
1966 now + br->multicast_querier_interval);
Nikolay Aleksandrov4950cfd2016-02-26 21:20:02 +01001967 err = 0;
1968 goto unlock;
1969 }
Herbert Xu0909e112010-02-27 19:41:49 +00001970 switch (val) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001971 case MDB_RTR_TYPE_DISABLED:
1972 p->multicast_router = MDB_RTR_TYPE_DISABLED;
1973 __del_port_router(p);
Herbert Xu0909e112010-02-27 19:41:49 +00001974 del_timer(&p->multicast_router_timer);
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001975 break;
1976 case MDB_RTR_TYPE_TEMP_QUERY:
1977 p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
1978 __del_port_router(p);
1979 break;
1980 case MDB_RTR_TYPE_PERM:
1981 p->multicast_router = MDB_RTR_TYPE_PERM;
1982 del_timer(&p->multicast_router_timer);
Herbert Xu0909e112010-02-27 19:41:49 +00001983 br_multicast_add_router(br, p);
1984 break;
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001985 case MDB_RTR_TYPE_TEMP:
1986 p->multicast_router = MDB_RTR_TYPE_TEMP;
1987 br_multicast_mark_router(br, p);
1988 break;
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001989 default:
1990 goto unlock;
Herbert Xu0909e112010-02-27 19:41:49 +00001991 }
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001992 err = 0;
1993unlock:
Herbert Xu0909e112010-02-27 19:41:49 +00001994 spin_unlock(&br->multicast_lock);
1995
1996 return err;
1997}
Herbert Xu561f1102010-02-27 19:41:50 +00001998
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001999static void br_multicast_start_querier(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +02002000 struct bridge_mcast_own_query *query)
Herbert Xu561f1102010-02-27 19:41:50 +00002001{
2002 struct net_bridge_port *port;
Herbert Xu74857212012-04-13 02:37:42 +00002003
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002004 __br_multicast_open(br, query);
Herbert Xu74857212012-04-13 02:37:42 +00002005
2006 list_for_each_entry(port, &br->port_list, list) {
2007 if (port->state == BR_STATE_DISABLED ||
2008 port->state == BR_STATE_BLOCKING)
2009 continue;
2010
Linus Lüssing90010b32014-06-07 18:26:26 +02002011 if (query == &br->ip4_own_query)
2012 br_multicast_enable(&port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002013#if IS_ENABLED(CONFIG_IPV6)
2014 else
Linus Lüssing90010b32014-06-07 18:26:26 +02002015 br_multicast_enable(&port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002016#endif
Herbert Xu74857212012-04-13 02:37:42 +00002017 }
2018}
2019
2020int br_multicast_toggle(struct net_bridge *br, unsigned long val)
2021{
Eric Dumazete8051682010-11-15 06:38:10 +00002022 struct net_bridge_mdb_htable *mdb;
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02002023 struct net_bridge_port *port;
2024 int err = 0;
Herbert Xu561f1102010-02-27 19:41:50 +00002025
Andrey Vaginef5e0d82011-11-10 05:48:03 +00002026 spin_lock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00002027 if (br->multicast_disabled == !val)
2028 goto unlock;
2029
2030 br->multicast_disabled = !val;
2031 if (br->multicast_disabled)
2032 goto unlock;
2033
Herbert Xu3a7fda02010-07-29 00:45:30 +00002034 if (!netif_running(br->dev))
2035 goto unlock;
2036
Eric Dumazete8051682010-11-15 06:38:10 +00002037 mdb = mlock_dereference(br->mdb, br);
2038 if (mdb) {
2039 if (mdb->old) {
Herbert Xu561f1102010-02-27 19:41:50 +00002040 err = -EEXIST;
2041rollback:
2042 br->multicast_disabled = !!val;
2043 goto unlock;
2044 }
2045
Eric Dumazete8051682010-11-15 06:38:10 +00002046 err = br_mdb_rehash(&br->mdb, mdb->max,
Herbert Xu561f1102010-02-27 19:41:50 +00002047 br->hash_elasticity);
2048 if (err)
2049 goto rollback;
2050 }
2051
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02002052 br_multicast_open(br);
2053 list_for_each_entry(port, &br->port_list, list)
2054 __br_multicast_enable_port(port);
Herbert Xu561f1102010-02-27 19:41:50 +00002055
2056unlock:
Andrey Vaginef5e0d82011-11-10 05:48:03 +00002057 spin_unlock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00002058
2059 return err;
2060}
Herbert Xub1951672010-02-27 19:41:51 +00002061
Herbert Xuc5c23262012-04-13 02:37:42 +00002062int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
2063{
Linus Lüssingb00589a2013-08-01 01:06:20 +02002064 unsigned long max_delay;
2065
Herbert Xuc5c23262012-04-13 02:37:42 +00002066 val = !!val;
2067
2068 spin_lock_bh(&br->multicast_lock);
2069 if (br->multicast_querier == val)
2070 goto unlock;
2071
2072 br->multicast_querier = val;
Linus Lüssingb00589a2013-08-01 01:06:20 +02002073 if (!val)
2074 goto unlock;
2075
2076 max_delay = br->multicast_query_response_interval;
Linus Lüssingb00589a2013-08-01 01:06:20 +02002077
Linus Lüssing90010b32014-06-07 18:26:26 +02002078 if (!timer_pending(&br->ip4_other_query.timer))
2079 br->ip4_other_query.delay_time = jiffies + max_delay;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002080
Linus Lüssing90010b32014-06-07 18:26:26 +02002081 br_multicast_start_querier(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002082
2083#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02002084 if (!timer_pending(&br->ip6_other_query.timer))
2085 br->ip6_other_query.delay_time = jiffies + max_delay;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002086
Linus Lüssing90010b32014-06-07 18:26:26 +02002087 br_multicast_start_querier(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002088#endif
Herbert Xuc5c23262012-04-13 02:37:42 +00002089
2090unlock:
2091 spin_unlock_bh(&br->multicast_lock);
2092
2093 return 0;
2094}
2095
Herbert Xub1951672010-02-27 19:41:51 +00002096int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
2097{
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02002098 int err = -EINVAL;
Herbert Xub1951672010-02-27 19:41:51 +00002099 u32 old;
Eric Dumazete8051682010-11-15 06:38:10 +00002100 struct net_bridge_mdb_htable *mdb;
Herbert Xub1951672010-02-27 19:41:51 +00002101
Curt Brunefe0d6922014-01-06 11:00:32 -08002102 spin_lock_bh(&br->multicast_lock);
Herbert Xub1951672010-02-27 19:41:51 +00002103 if (!is_power_of_2(val))
2104 goto unlock;
Eric Dumazete8051682010-11-15 06:38:10 +00002105
2106 mdb = mlock_dereference(br->mdb, br);
2107 if (mdb && val < mdb->size)
Herbert Xub1951672010-02-27 19:41:51 +00002108 goto unlock;
2109
2110 err = 0;
2111
2112 old = br->hash_max;
2113 br->hash_max = val;
2114
Eric Dumazete8051682010-11-15 06:38:10 +00002115 if (mdb) {
2116 if (mdb->old) {
Herbert Xub1951672010-02-27 19:41:51 +00002117 err = -EEXIST;
2118rollback:
2119 br->hash_max = old;
2120 goto unlock;
2121 }
2122
2123 err = br_mdb_rehash(&br->mdb, br->hash_max,
2124 br->hash_elasticity);
2125 if (err)
2126 goto rollback;
2127 }
2128
2129unlock:
Curt Brunefe0d6922014-01-06 11:00:32 -08002130 spin_unlock_bh(&br->multicast_lock);
Herbert Xub1951672010-02-27 19:41:51 +00002131
2132 return err;
2133}
Linus Lüssing07f8ac42014-06-07 18:26:28 +02002134
2135/**
2136 * br_multicast_list_adjacent - Returns snooped multicast addresses
2137 * @dev: The bridge port adjacent to which to retrieve addresses
2138 * @br_ip_list: The list to store found, snooped multicast IP addresses in
2139 *
2140 * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
2141 * snooping feature on all bridge ports of dev's bridge device, excluding
2142 * the addresses from dev itself.
2143 *
2144 * Returns the number of items added to br_ip_list.
2145 *
2146 * Notes:
2147 * - br_ip_list needs to be initialized by caller
2148 * - br_ip_list might contain duplicates in the end
2149 * (needs to be taken care of by caller)
2150 * - br_ip_list needs to be freed by caller
2151 */
2152int br_multicast_list_adjacent(struct net_device *dev,
2153 struct list_head *br_ip_list)
2154{
2155 struct net_bridge *br;
2156 struct net_bridge_port *port;
2157 struct net_bridge_port_group *group;
2158 struct br_ip_list *entry;
2159 int count = 0;
2160
2161 rcu_read_lock();
2162 if (!br_ip_list || !br_port_exists(dev))
2163 goto unlock;
2164
2165 port = br_port_get_rcu(dev);
2166 if (!port || !port->br)
2167 goto unlock;
2168
2169 br = port->br;
2170
2171 list_for_each_entry_rcu(port, &br->port_list, list) {
2172 if (!port->dev || port->dev == dev)
2173 continue;
2174
2175 hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
2176 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
2177 if (!entry)
2178 goto unlock;
2179
2180 entry->addr = group->addr;
2181 list_add(&entry->list, br_ip_list);
2182 count++;
2183 }
2184 }
2185
2186unlock:
2187 rcu_read_unlock();
2188 return count;
2189}
2190EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
Linus Lüssing2cd41432014-06-07 18:26:29 +02002191
2192/**
Linus Lüssingc34963e2014-07-07 05:41:17 +02002193 * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2194 * @dev: The bridge port providing the bridge on which to check for a querier
2195 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2196 *
2197 * Checks whether the given interface has a bridge on top and if so returns
2198 * true if a valid querier exists anywhere on the bridged link layer.
2199 * Otherwise returns false.
2200 */
2201bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
2202{
2203 struct net_bridge *br;
2204 struct net_bridge_port *port;
2205 struct ethhdr eth;
2206 bool ret = false;
2207
2208 rcu_read_lock();
2209 if (!br_port_exists(dev))
2210 goto unlock;
2211
2212 port = br_port_get_rcu(dev);
2213 if (!port || !port->br)
2214 goto unlock;
2215
2216 br = port->br;
2217
2218 memset(&eth, 0, sizeof(eth));
2219 eth.h_proto = htons(proto);
2220
2221 ret = br_multicast_querier_exists(br, &eth);
2222
2223unlock:
2224 rcu_read_unlock();
2225 return ret;
2226}
2227EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
2228
2229/**
Linus Lüssing2cd41432014-06-07 18:26:29 +02002230 * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
2231 * @dev: The bridge port adjacent to which to check for a querier
2232 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2233 *
2234 * Checks whether the given interface has a bridge on top and if so returns
2235 * true if a selected querier is behind one of the other ports of this
2236 * bridge. Otherwise returns false.
2237 */
2238bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
2239{
2240 struct net_bridge *br;
2241 struct net_bridge_port *port;
2242 bool ret = false;
2243
2244 rcu_read_lock();
2245 if (!br_port_exists(dev))
2246 goto unlock;
2247
2248 port = br_port_get_rcu(dev);
2249 if (!port || !port->br)
2250 goto unlock;
2251
2252 br = port->br;
2253
2254 switch (proto) {
2255 case ETH_P_IP:
2256 if (!timer_pending(&br->ip4_other_query.timer) ||
2257 rcu_dereference(br->ip4_querier.port) == port)
2258 goto unlock;
2259 break;
Linus Lüssing3993c4e2014-06-12 01:41:24 +02002260#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing2cd41432014-06-07 18:26:29 +02002261 case ETH_P_IPV6:
2262 if (!timer_pending(&br->ip6_other_query.timer) ||
2263 rcu_dereference(br->ip6_querier.port) == port)
2264 goto unlock;
2265 break;
Linus Lüssing3993c4e2014-06-12 01:41:24 +02002266#endif
Linus Lüssing2cd41432014-06-07 18:26:29 +02002267 default:
2268 goto unlock;
2269 }
2270
2271 ret = true;
2272unlock:
2273 rcu_read_unlock();
2274 return ret;
2275}
2276EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002277
2278static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002279 const struct sk_buff *skb, u8 type, u8 dir)
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002280{
2281 struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002282 __be16 proto = skb->protocol;
2283 unsigned int t_len;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002284
2285 u64_stats_update_begin(&pstats->syncp);
2286 switch (proto) {
2287 case htons(ETH_P_IP):
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002288 t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002289 switch (type) {
2290 case IGMP_HOST_MEMBERSHIP_REPORT:
2291 pstats->mstats.igmp_v1reports[dir]++;
2292 break;
2293 case IGMPV2_HOST_MEMBERSHIP_REPORT:
2294 pstats->mstats.igmp_v2reports[dir]++;
2295 break;
2296 case IGMPV3_HOST_MEMBERSHIP_REPORT:
2297 pstats->mstats.igmp_v3reports[dir]++;
2298 break;
2299 case IGMP_HOST_MEMBERSHIP_QUERY:
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002300 if (t_len != sizeof(struct igmphdr)) {
2301 pstats->mstats.igmp_v3queries[dir]++;
2302 } else {
2303 unsigned int offset = skb_transport_offset(skb);
2304 struct igmphdr *ih, _ihdr;
2305
2306 ih = skb_header_pointer(skb, offset,
2307 sizeof(_ihdr), &_ihdr);
2308 if (!ih)
2309 break;
2310 if (!ih->code)
2311 pstats->mstats.igmp_v1queries[dir]++;
2312 else
2313 pstats->mstats.igmp_v2queries[dir]++;
2314 }
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002315 break;
2316 case IGMP_HOST_LEAVE_MESSAGE:
2317 pstats->mstats.igmp_leaves[dir]++;
2318 break;
2319 }
2320 break;
2321#if IS_ENABLED(CONFIG_IPV6)
2322 case htons(ETH_P_IPV6):
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002323 t_len = ntohs(ipv6_hdr(skb)->payload_len) +
2324 sizeof(struct ipv6hdr);
2325 t_len -= skb_network_header_len(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002326 switch (type) {
2327 case ICMPV6_MGM_REPORT:
2328 pstats->mstats.mld_v1reports[dir]++;
2329 break;
2330 case ICMPV6_MLD2_REPORT:
2331 pstats->mstats.mld_v2reports[dir]++;
2332 break;
2333 case ICMPV6_MGM_QUERY:
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002334 if (t_len != sizeof(struct mld_msg))
2335 pstats->mstats.mld_v2queries[dir]++;
2336 else
2337 pstats->mstats.mld_v1queries[dir]++;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002338 break;
2339 case ICMPV6_MGM_REDUCTION:
2340 pstats->mstats.mld_leaves[dir]++;
2341 break;
2342 }
2343 break;
2344#endif /* CONFIG_IPV6 */
2345 }
2346 u64_stats_update_end(&pstats->syncp);
2347}
2348
2349void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002350 const struct sk_buff *skb, u8 type, u8 dir)
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002351{
2352 struct bridge_mcast_stats __percpu *stats;
2353
2354 /* if multicast_disabled is true then igmp type can't be set */
2355 if (!type || !br->multicast_stats_enabled)
2356 return;
2357
2358 if (p)
2359 stats = p->mcast_stats;
2360 else
2361 stats = br->mcast_stats;
2362 if (WARN_ON(!stats))
2363 return;
2364
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002365 br_mcast_stats_add(stats, skb, type, dir);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002366}
2367
2368int br_multicast_init_stats(struct net_bridge *br)
2369{
2370 br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
2371 if (!br->mcast_stats)
2372 return -ENOMEM;
2373
2374 return 0;
2375}
2376
2377static void mcast_stats_add_dir(u64 *dst, u64 *src)
2378{
2379 dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
2380 dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
2381}
2382
2383void br_multicast_get_stats(const struct net_bridge *br,
2384 const struct net_bridge_port *p,
2385 struct br_mcast_stats *dest)
2386{
2387 struct bridge_mcast_stats __percpu *stats;
2388 struct br_mcast_stats tdst;
2389 int i;
2390
2391 memset(dest, 0, sizeof(*dest));
2392 if (p)
2393 stats = p->mcast_stats;
2394 else
2395 stats = br->mcast_stats;
2396 if (WARN_ON(!stats))
2397 return;
2398
2399 memset(&tdst, 0, sizeof(tdst));
2400 for_each_possible_cpu(i) {
2401 struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
2402 struct br_mcast_stats temp;
2403 unsigned int start;
2404
2405 do {
2406 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
2407 memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
2408 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
2409
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002410 mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
2411 mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
2412 mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002413 mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
2414 mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
2415 mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
2416 mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
2417 tdst.igmp_parse_errors += temp.igmp_parse_errors;
2418
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002419 mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
2420 mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002421 mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
2422 mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
2423 mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
2424 tdst.mld_parse_errors += temp.mld_parse_errors;
2425 }
2426 memcpy(dest, &tdst, sizeof(*dest));
2427}