blob: 2136e45f5277764264c181b201664822edef4c17 [file] [log] [blame]
Herbert Xueb1d1642010-02-27 19:41:45 +00001/*
2 * Bridge multicast support.
3 *
4 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 */
12
13#include <linux/err.h>
Linus Lüssing07f8ac42014-06-07 18:26:28 +020014#include <linux/export.h>
Herbert Xueb1d1642010-02-27 19:41:45 +000015#include <linux/if_ether.h>
16#include <linux/igmp.h>
17#include <linux/jhash.h>
18#include <linux/kernel.h>
Herbert Xub1951672010-02-27 19:41:51 +000019#include <linux/log2.h>
Herbert Xueb1d1642010-02-27 19:41:45 +000020#include <linux/netdevice.h>
21#include <linux/netfilter_bridge.h>
22#include <linux/random.h>
23#include <linux/rculist.h>
24#include <linux/skbuff.h>
25#include <linux/slab.h>
26#include <linux/timer.h>
Cong Wang1c8ad5b2013-05-21 21:52:54 +000027#include <linux/inetdevice.h>
Herbert Xueb1d1642010-02-27 19:41:45 +000028#include <net/ip.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000029#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090030#include <net/ipv6.h>
31#include <net/mld.h>
David S. Millerd4c4f072010-04-27 10:16:54 -070032#include <net/ip6_checksum.h>
Linus Lüssing3c3769e2013-09-04 02:13:39 +020033#include <net/addrconf.h>
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090034#endif
Herbert Xueb1d1642010-02-27 19:41:45 +000035
36#include "br_private.h"
37
Linus Lüssingcc0fdd82013-08-30 17:28:17 +020038static void br_multicast_start_querier(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +020039 struct bridge_mcast_own_query *query);
Satish Ashok754bc542015-06-19 01:22:57 -070040static void br_multicast_add_router(struct net_bridge *br,
41 struct net_bridge_port *port);
Satish Ashokbc8c20a2015-07-13 05:28:37 -070042static void br_ip4_multicast_leave_group(struct net_bridge *br,
43 struct net_bridge_port *port,
44 __be32 group,
45 __u16 vid);
46#if IS_ENABLED(CONFIG_IPV6)
47static void br_ip6_multicast_leave_group(struct net_bridge *br,
48 struct net_bridge_port *port,
49 const struct in6_addr *group,
50 __u16 vid);
51#endif
Cong Wang2ce297f2012-12-10 02:15:35 +000052unsigned int br_mdb_rehash_seq;
Herbert Xuc83b8fa2012-04-13 02:37:42 +000053
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090054static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
55{
56 if (a->proto != b->proto)
57 return 0;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000058 if (a->vid != b->vid)
59 return 0;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090060 switch (a->proto) {
61 case htons(ETH_P_IP):
62 return a->u.ip4 == b->u.ip4;
Eric Dumazetdfd56b82011-12-10 09:48:31 +000063#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090064 case htons(ETH_P_IPV6):
65 return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
66#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090067 }
68 return 0;
69}
70
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000071static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip,
72 __u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +000073{
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000074 return jhash_2words((__force u32)ip, vid, mdb->secret) & (mdb->max - 1);
Herbert Xueb1d1642010-02-27 19:41:45 +000075}
76
Eric Dumazetdfd56b82011-12-10 09:48:31 +000077#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090078static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000079 const struct in6_addr *ip,
80 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090081{
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000082 return jhash_2words(ipv6_addr_hash(ip), vid,
83 mdb->secret) & (mdb->max - 1);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090084}
85#endif
86
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090087static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb,
88 struct br_ip *ip)
89{
90 switch (ip->proto) {
91 case htons(ETH_P_IP):
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000092 return __br_ip4_hash(mdb, ip->u.ip4, ip->vid);
Eric Dumazetdfd56b82011-12-10 09:48:31 +000093#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090094 case htons(ETH_P_IPV6):
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000095 return __br_ip6_hash(mdb, &ip->u.ip6, ip->vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090096#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090097 }
98 return 0;
Herbert Xueb1d1642010-02-27 19:41:45 +000099}
100
101static struct net_bridge_mdb_entry *__br_mdb_ip_get(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900102 struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +0000103{
104 struct net_bridge_mdb_entry *mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000105
Sasha Levinb67bfe02013-02-27 17:06:00 -0800106 hlist_for_each_entry_rcu(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900107 if (br_ip_equal(&mp->addr, dst))
Herbert Xueb1d1642010-02-27 19:41:45 +0000108 return mp;
109 }
110
111 return NULL;
112}
113
Cong Wangcfd56752012-12-11 22:23:08 +0000114struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge_mdb_htable *mdb,
115 struct br_ip *dst)
Herbert Xu7f285fa2010-07-05 14:50:08 +0000116{
117 if (!mdb)
118 return NULL;
119
120 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
121}
122
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900123static struct net_bridge_mdb_entry *br_mdb_ip4_get(
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000124 struct net_bridge_mdb_htable *mdb, __be32 dst, __u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000125{
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900126 struct br_ip br_dst;
Herbert Xu0821ec52010-03-15 20:38:25 -0700127
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900128 br_dst.u.ip4 = dst;
129 br_dst.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000130 br_dst.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900131
Herbert Xu7f285fa2010-07-05 14:50:08 +0000132 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900133}
134
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000135#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900136static struct net_bridge_mdb_entry *br_mdb_ip6_get(
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000137 struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst,
138 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900139{
140 struct br_ip br_dst;
141
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000142 br_dst.u.ip6 = *dst;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900143 br_dst.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000144 br_dst.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900145
Herbert Xu7f285fa2010-07-05 14:50:08 +0000146 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900147}
148#endif
149
Herbert Xueb1d1642010-02-27 19:41:45 +0000150struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
Cong Wangfbca58a2013-03-07 03:05:33 +0000151 struct sk_buff *skb, u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000152{
Eric Dumazete8051682010-11-15 06:38:10 +0000153 struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900154 struct br_ip ip;
155
Herbert Xu7f285fa2010-07-05 14:50:08 +0000156 if (br->multicast_disabled)
Herbert Xueb1d1642010-02-27 19:41:45 +0000157 return NULL;
158
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900159 if (BR_INPUT_SKB_CB(skb)->igmp)
160 return NULL;
161
162 ip.proto = skb->protocol;
Cong Wangfbca58a2013-03-07 03:05:33 +0000163 ip.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900164
Herbert Xueb1d1642010-02-27 19:41:45 +0000165 switch (skb->protocol) {
166 case htons(ETH_P_IP):
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900167 ip.u.ip4 = ip_hdr(skb)->daddr;
168 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000169#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900170 case htons(ETH_P_IPV6):
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000171 ip.u.ip6 = ipv6_hdr(skb)->daddr;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900172 break;
173#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900174 default:
175 return NULL;
Herbert Xueb1d1642010-02-27 19:41:45 +0000176 }
177
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900178 return br_mdb_ip_get(mdb, &ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000179}
180
181static void br_mdb_free(struct rcu_head *head)
182{
183 struct net_bridge_mdb_htable *mdb =
184 container_of(head, struct net_bridge_mdb_htable, rcu);
185 struct net_bridge_mdb_htable *old = mdb->old;
186
187 mdb->old = NULL;
188 kfree(old->mhash);
189 kfree(old);
190}
191
192static int br_mdb_copy(struct net_bridge_mdb_htable *new,
193 struct net_bridge_mdb_htable *old,
194 int elasticity)
195{
196 struct net_bridge_mdb_entry *mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000197 int maxlen;
198 int len;
199 int i;
200
201 for (i = 0; i < old->max; i++)
Sasha Levinb67bfe02013-02-27 17:06:00 -0800202 hlist_for_each_entry(mp, &old->mhash[i], hlist[old->ver])
Herbert Xueb1d1642010-02-27 19:41:45 +0000203 hlist_add_head(&mp->hlist[new->ver],
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900204 &new->mhash[br_ip_hash(new, &mp->addr)]);
Herbert Xueb1d1642010-02-27 19:41:45 +0000205
206 if (!elasticity)
207 return 0;
208
209 maxlen = 0;
210 for (i = 0; i < new->max; i++) {
211 len = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800212 hlist_for_each_entry(mp, &new->mhash[i], hlist[new->ver])
Herbert Xueb1d1642010-02-27 19:41:45 +0000213 len++;
214 if (len > maxlen)
215 maxlen = len;
216 }
217
218 return maxlen > elasticity ? -EINVAL : 0;
219}
220
Cong Wangcfd56752012-12-11 22:23:08 +0000221void br_multicast_free_pg(struct rcu_head *head)
Herbert Xueb1d1642010-02-27 19:41:45 +0000222{
223 struct net_bridge_port_group *p =
224 container_of(head, struct net_bridge_port_group, rcu);
225
226 kfree(p);
227}
228
229static void br_multicast_free_group(struct rcu_head *head)
230{
231 struct net_bridge_mdb_entry *mp =
232 container_of(head, struct net_bridge_mdb_entry, rcu);
233
234 kfree(mp);
235}
236
237static void br_multicast_group_expired(unsigned long data)
238{
239 struct net_bridge_mdb_entry *mp = (void *)data;
240 struct net_bridge *br = mp->br;
241 struct net_bridge_mdb_htable *mdb;
242
243 spin_lock(&br->multicast_lock);
244 if (!netif_running(br->dev) || timer_pending(&mp->timer))
245 goto out;
246
Herbert Xu8a870172011-02-12 01:05:42 -0800247 mp->mglist = false;
Herbert Xueb1d1642010-02-27 19:41:45 +0000248
249 if (mp->ports)
250 goto out;
251
Eric Dumazete8051682010-11-15 06:38:10 +0000252 mdb = mlock_dereference(br->mdb, br);
253
Herbert Xueb1d1642010-02-27 19:41:45 +0000254 hlist_del_rcu(&mp->hlist[mdb->ver]);
255 mdb->size--;
256
Herbert Xueb1d1642010-02-27 19:41:45 +0000257 call_rcu_bh(&mp->rcu, br_multicast_free_group);
258
259out:
260 spin_unlock(&br->multicast_lock);
261}
262
263static void br_multicast_del_pg(struct net_bridge *br,
264 struct net_bridge_port_group *pg)
265{
Eric Dumazete8051682010-11-15 06:38:10 +0000266 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000267 struct net_bridge_mdb_entry *mp;
268 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +0000269 struct net_bridge_port_group __rcu **pp;
270
271 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +0000272
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900273 mp = br_mdb_ip_get(mdb, &pg->addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000274 if (WARN_ON(!mp))
275 return;
276
Eric Dumazete8051682010-11-15 06:38:10 +0000277 for (pp = &mp->ports;
278 (p = mlock_dereference(*pp, br)) != NULL;
279 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000280 if (p != pg)
281 continue;
282
stephen hemminger83f6a742010-04-27 15:01:06 +0000283 rcu_assign_pointer(*pp, p->next);
Herbert Xueb1d1642010-02-27 19:41:45 +0000284 hlist_del_init(&p->mglist);
285 del_timer(&p->timer);
Elad Raz45ebcce2016-04-21 12:52:45 +0200286 br_mdb_notify(br->dev, p->port, &pg->addr, RTM_DELMDB,
287 p->flags);
Herbert Xueb1d1642010-02-27 19:41:45 +0000288 call_rcu_bh(&p->rcu, br_multicast_free_pg);
289
Linus Lüssing454594f2013-10-20 00:58:57 +0200290 if (!mp->ports && !mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +0000291 netif_running(br->dev))
292 mod_timer(&mp->timer, jiffies);
293
294 return;
295 }
296
297 WARN_ON(1);
298}
299
300static void br_multicast_port_group_expired(unsigned long data)
301{
302 struct net_bridge_port_group *pg = (void *)data;
303 struct net_bridge *br = pg->port->br;
304
305 spin_lock(&br->multicast_lock);
306 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
Elad Raz9d06b6d2016-02-03 09:57:05 +0100307 hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT)
Herbert Xueb1d1642010-02-27 19:41:45 +0000308 goto out;
309
310 br_multicast_del_pg(br, pg);
311
312out:
313 spin_unlock(&br->multicast_lock);
314}
315
Eric Dumazete8051682010-11-15 06:38:10 +0000316static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max,
Herbert Xueb1d1642010-02-27 19:41:45 +0000317 int elasticity)
318{
Eric Dumazete8051682010-11-15 06:38:10 +0000319 struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000320 struct net_bridge_mdb_htable *mdb;
321 int err;
322
323 mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
324 if (!mdb)
325 return -ENOMEM;
326
327 mdb->max = max;
328 mdb->old = old;
329
330 mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
331 if (!mdb->mhash) {
332 kfree(mdb);
333 return -ENOMEM;
334 }
335
336 mdb->size = old ? old->size : 0;
337 mdb->ver = old ? old->ver ^ 1 : 0;
338
339 if (!old || elasticity)
340 get_random_bytes(&mdb->secret, sizeof(mdb->secret));
341 else
342 mdb->secret = old->secret;
343
344 if (!old)
345 goto out;
346
347 err = br_mdb_copy(mdb, old, elasticity);
348 if (err) {
349 kfree(mdb->mhash);
350 kfree(mdb);
351 return err;
352 }
353
Cong Wang2ce297f2012-12-10 02:15:35 +0000354 br_mdb_rehash_seq++;
Herbert Xueb1d1642010-02-27 19:41:45 +0000355 call_rcu_bh(&mdb->rcu, br_mdb_free);
356
357out:
358 rcu_assign_pointer(*mdbp, mdb);
359
360 return 0;
361}
362
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900363static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200364 __be32 group,
365 u8 *igmp_type)
Herbert Xueb1d1642010-02-27 19:41:45 +0000366{
367 struct sk_buff *skb;
368 struct igmphdr *ih;
369 struct ethhdr *eth;
370 struct iphdr *iph;
371
372 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
373 sizeof(*ih) + 4);
374 if (!skb)
375 goto out;
376
377 skb->protocol = htons(ETH_P_IP);
378
379 skb_reset_mac_header(skb);
380 eth = eth_hdr(skb);
381
Joe Perchese5a727f2014-02-23 00:05:25 -0800382 ether_addr_copy(eth->h_source, br->dev->dev_addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000383 eth->h_dest[0] = 1;
384 eth->h_dest[1] = 0;
385 eth->h_dest[2] = 0x5e;
386 eth->h_dest[3] = 0;
387 eth->h_dest[4] = 0;
388 eth->h_dest[5] = 1;
389 eth->h_proto = htons(ETH_P_IP);
390 skb_put(skb, sizeof(*eth));
391
392 skb_set_network_header(skb, skb->len);
393 iph = ip_hdr(skb);
394
395 iph->version = 4;
396 iph->ihl = 6;
397 iph->tos = 0xc0;
398 iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4);
399 iph->id = 0;
400 iph->frag_off = htons(IP_DF);
401 iph->ttl = 1;
402 iph->protocol = IPPROTO_IGMP;
Cong Wang1c8ad5b2013-05-21 21:52:54 +0000403 iph->saddr = br->multicast_query_use_ifaddr ?
404 inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000405 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
406 ((u8 *)&iph[1])[0] = IPOPT_RA;
407 ((u8 *)&iph[1])[1] = 4;
408 ((u8 *)&iph[1])[2] = 0;
409 ((u8 *)&iph[1])[3] = 0;
410 ip_send_check(iph);
411 skb_put(skb, 24);
412
413 skb_set_transport_header(skb, skb->len);
414 ih = igmp_hdr(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200415 *igmp_type = IGMP_HOST_MEMBERSHIP_QUERY;
Herbert Xueb1d1642010-02-27 19:41:45 +0000416 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
417 ih->code = (group ? br->multicast_last_member_interval :
418 br->multicast_query_response_interval) /
419 (HZ / IGMP_TIMER_SCALE);
420 ih->group = group;
421 ih->csum = 0;
422 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
423 skb_put(skb, sizeof(*ih));
424
425 __skb_pull(skb, sizeof(*eth));
426
427out:
428 return skb;
429}
430
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000431#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900432static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200433 const struct in6_addr *grp,
434 u8 *igmp_type)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900435{
436 struct sk_buff *skb;
437 struct ipv6hdr *ip6h;
438 struct mld_msg *mldq;
439 struct ethhdr *eth;
440 u8 *hopopt;
441 unsigned long interval;
442
443 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
444 8 + sizeof(*mldq));
445 if (!skb)
446 goto out;
447
448 skb->protocol = htons(ETH_P_IPV6);
449
450 /* Ethernet header */
451 skb_reset_mac_header(skb);
452 eth = eth_hdr(skb);
453
Joe Perchese5a727f2014-02-23 00:05:25 -0800454 ether_addr_copy(eth->h_source, br->dev->dev_addr);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900455 eth->h_proto = htons(ETH_P_IPV6);
456 skb_put(skb, sizeof(*eth));
457
458 /* IPv6 header + HbH option */
459 skb_set_network_header(skb, skb->len);
460 ip6h = ipv6_hdr(skb);
461
462 *(__force __be32 *)ip6h = htonl(0x60000000);
David Stevens76d66152010-12-14 08:42:16 +0000463 ip6h->payload_len = htons(8 + sizeof(*mldq));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900464 ip6h->nexthdr = IPPROTO_HOPOPTS;
465 ip6h->hop_limit = 1;
Linus Lüssinga7bff752011-03-22 11:40:32 +0000466 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
Ulrich Weberd1d81d42012-03-05 04:52:44 +0000467 if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
468 &ip6h->saddr)) {
469 kfree_skb(skb);
daniel0888d5f2016-06-24 12:35:18 +0200470 br->has_ipv6_addr = 0;
Ulrich Weberd1d81d42012-03-05 04:52:44 +0000471 return NULL;
472 }
daniel0888d5f2016-06-24 12:35:18 +0200473
474 br->has_ipv6_addr = 1;
Linus Lüssing36cff5a2011-02-17 08:17:51 +0000475 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900476
477 hopopt = (u8 *)(ip6h + 1);
478 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
479 hopopt[1] = 0; /* length of HbH */
480 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
481 hopopt[3] = 2; /* Length of RA Option */
482 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
483 hopopt[5] = 0;
Eldad Zack1de5a712012-05-17 06:00:25 +0000484 hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */
485 hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900486
487 skb_put(skb, sizeof(*ip6h) + 8);
488
489 /* ICMPv6 */
490 skb_set_transport_header(skb, skb->len);
491 mldq = (struct mld_msg *) icmp6_hdr(skb);
492
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200493 interval = ipv6_addr_any(grp) ?
Linus Lüssing32de8682013-06-16 23:20:34 +0200494 br->multicast_query_response_interval :
495 br->multicast_last_member_interval;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900496
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200497 *igmp_type = ICMPV6_MGM_QUERY;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900498 mldq->mld_type = ICMPV6_MGM_QUERY;
499 mldq->mld_code = 0;
500 mldq->mld_cksum = 0;
501 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
502 mldq->mld_reserved = 0;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200503 mldq->mld_mca = *grp;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900504
505 /* checksum */
506 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
507 sizeof(*mldq), IPPROTO_ICMPV6,
508 csum_partial(mldq,
509 sizeof(*mldq), 0));
510 skb_put(skb, sizeof(*mldq));
511
512 __skb_pull(skb, sizeof(*eth));
513
514out:
515 return skb;
516}
517#endif
518
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900519static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200520 struct br_ip *addr,
521 u8 *igmp_type)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900522{
523 switch (addr->proto) {
524 case htons(ETH_P_IP):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200525 return br_ip4_multicast_alloc_query(br, addr->u.ip4, igmp_type);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000526#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900527 case htons(ETH_P_IPV6):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200528 return br_ip6_multicast_alloc_query(br, &addr->u.ip6,
529 igmp_type);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900530#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900531 }
532 return NULL;
533}
534
Herbert Xueb1d1642010-02-27 19:41:45 +0000535static struct net_bridge_mdb_entry *br_multicast_get_group(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900536 struct net_bridge *br, struct net_bridge_port *port,
537 struct br_ip *group, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +0000538{
Eric Dumazete8051682010-11-15 06:38:10 +0000539 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000540 struct net_bridge_mdb_entry *mp;
Eric Dumazet95c96172012-04-15 05:58:06 +0000541 unsigned int count = 0;
542 unsigned int max;
Herbert Xueb1d1642010-02-27 19:41:45 +0000543 int elasticity;
544 int err;
545
Eric Dumazete8051682010-11-15 06:38:10 +0000546 mdb = rcu_dereference_protected(br->mdb, 1);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800547 hlist_for_each_entry(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000548 count++;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900549 if (unlikely(br_ip_equal(group, &mp->addr)))
Herbert Xueb1d1642010-02-27 19:41:45 +0000550 return mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000551 }
552
553 elasticity = 0;
554 max = mdb->max;
555
556 if (unlikely(count > br->hash_elasticity && count)) {
557 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000558 br_info(br, "Multicast hash table "
559 "chain limit reached: %s\n",
560 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000561
562 elasticity = br->hash_elasticity;
563 }
564
565 if (mdb->size >= max) {
566 max *= 2;
Thomas Graf036be6d2012-07-10 22:29:19 +0000567 if (unlikely(max > br->hash_max)) {
568 br_warn(br, "Multicast hash table maximum of %d "
569 "reached, disabling snooping: %s\n",
570 br->hash_max,
571 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000572 err = -E2BIG;
573disable:
574 br->multicast_disabled = 1;
575 goto err;
576 }
577 }
578
579 if (max > mdb->max || elasticity) {
580 if (mdb->old) {
581 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000582 br_info(br, "Multicast hash table "
583 "on fire: %s\n",
584 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000585 err = -EEXIST;
586 goto err;
587 }
588
589 err = br_mdb_rehash(&br->mdb, max, elasticity);
590 if (err) {
stephen hemminger28a16c92010-05-10 09:31:09 +0000591 br_warn(br, "Cannot rehash multicast "
592 "hash table, disabling snooping: %s, %d, %d\n",
593 port ? port->dev->name : br->dev->name,
594 mdb->size, err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000595 goto disable;
596 }
597
598 err = -EAGAIN;
599 goto err;
600 }
601
602 return NULL;
603
604err:
605 mp = ERR_PTR(err);
606 return mp;
607}
608
Cong Wangcfd56752012-12-11 22:23:08 +0000609struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
610 struct net_bridge_port *port, struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000611{
Eric Dumazete8051682010-11-15 06:38:10 +0000612 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000613 struct net_bridge_mdb_entry *mp;
614 int hash;
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000615 int err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000616
Eric Dumazete8051682010-11-15 06:38:10 +0000617 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000618 if (!mdb) {
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000619 err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0);
620 if (err)
621 return ERR_PTR(err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000622 goto rehash;
623 }
624
625 hash = br_ip_hash(mdb, group);
626 mp = br_multicast_get_group(br, port, group, hash);
627 switch (PTR_ERR(mp)) {
628 case 0:
629 break;
630
631 case -EAGAIN:
632rehash:
Eric Dumazete8051682010-11-15 06:38:10 +0000633 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000634 hash = br_ip_hash(mdb, group);
635 break;
636
637 default:
638 goto out;
639 }
640
641 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
642 if (unlikely(!mp))
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000643 return ERR_PTR(-ENOMEM);
Herbert Xueb1d1642010-02-27 19:41:45 +0000644
645 mp->br = br;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900646 mp->addr = *group;
Eric Dumazet1faabf22013-07-19 20:07:16 -0700647 setup_timer(&mp->timer, br_multicast_group_expired,
648 (unsigned long)mp);
649
Herbert Xueb1d1642010-02-27 19:41:45 +0000650 hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
651 mdb->size++;
652
653out:
654 return mp;
655}
656
Cong Wangcfd56752012-12-11 22:23:08 +0000657struct net_bridge_port_group *br_multicast_new_port_group(
658 struct net_bridge_port *port,
659 struct br_ip *group,
Amerigo Wangccb1c312012-12-14 22:09:51 +0000660 struct net_bridge_port_group __rcu *next,
Elad Raz9d06b6d2016-02-03 09:57:05 +0100661 unsigned char flags)
Cong Wangcfd56752012-12-11 22:23:08 +0000662{
663 struct net_bridge_port_group *p;
664
665 p = kzalloc(sizeof(*p), GFP_ATOMIC);
666 if (unlikely(!p))
667 return NULL;
668
669 p->addr = *group;
670 p->port = port;
Elad Raz9d06b6d2016-02-03 09:57:05 +0100671 p->flags = flags;
stephen hemmingereca2a432012-12-13 06:51:28 +0000672 rcu_assign_pointer(p->next, next);
Cong Wangcfd56752012-12-11 22:23:08 +0000673 hlist_add_head(&p->mglist, &port->mglist);
674 setup_timer(&p->timer, br_multicast_port_group_expired,
675 (unsigned long)p);
676 return p;
677}
678
Herbert Xueb1d1642010-02-27 19:41:45 +0000679static int br_multicast_add_group(struct net_bridge *br,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900680 struct net_bridge_port *port,
681 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000682{
683 struct net_bridge_mdb_entry *mp;
684 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +0000685 struct net_bridge_port_group __rcu **pp;
Linus Lüssing454594f2013-10-20 00:58:57 +0200686 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +0000687 int err;
688
Herbert Xueb1d1642010-02-27 19:41:45 +0000689 spin_lock(&br->multicast_lock);
690 if (!netif_running(br->dev) ||
691 (port && port->state == BR_STATE_DISABLED))
692 goto out;
693
694 mp = br_multicast_new_group(br, port, group);
695 err = PTR_ERR(mp);
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000696 if (IS_ERR(mp))
Herbert Xueb1d1642010-02-27 19:41:45 +0000697 goto err;
698
699 if (!port) {
Herbert Xu8a870172011-02-12 01:05:42 -0800700 mp->mglist = true;
Linus Lüssing454594f2013-10-20 00:58:57 +0200701 mod_timer(&mp->timer, now + br->multicast_membership_interval);
Herbert Xueb1d1642010-02-27 19:41:45 +0000702 goto out;
703 }
704
Eric Dumazete8051682010-11-15 06:38:10 +0000705 for (pp = &mp->ports;
706 (p = mlock_dereference(*pp, br)) != NULL;
707 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000708 if (p->port == port)
Linus Lüssing454594f2013-10-20 00:58:57 +0200709 goto found;
Herbert Xueb1d1642010-02-27 19:41:45 +0000710 if ((unsigned long)p->port < (unsigned long)port)
711 break;
712 }
713
Elad Raz9d06b6d2016-02-03 09:57:05 +0100714 p = br_multicast_new_port_group(port, group, *pp, 0);
Herbert Xueb1d1642010-02-27 19:41:45 +0000715 if (unlikely(!p))
716 goto err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000717 rcu_assign_pointer(*pp, p);
Elad Raz45ebcce2016-04-21 12:52:45 +0200718 br_mdb_notify(br->dev, port, group, RTM_NEWMDB, 0);
Herbert Xueb1d1642010-02-27 19:41:45 +0000719
Linus Lüssing454594f2013-10-20 00:58:57 +0200720found:
721 mod_timer(&p->timer, now + br->multicast_membership_interval);
Herbert Xueb1d1642010-02-27 19:41:45 +0000722out:
723 err = 0;
724
725err:
726 spin_unlock(&br->multicast_lock);
727 return err;
728}
729
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900730static int br_ip4_multicast_add_group(struct net_bridge *br,
731 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000732 __be32 group,
733 __u16 vid)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900734{
735 struct br_ip br_group;
736
737 if (ipv4_is_local_multicast(group))
738 return 0;
739
740 br_group.u.ip4 = group;
741 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000742 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900743
744 return br_multicast_add_group(br, port, &br_group);
745}
746
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000747#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900748static int br_ip6_multicast_add_group(struct net_bridge *br,
749 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000750 const struct in6_addr *group,
751 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900752{
753 struct br_ip br_group;
754
Linus Lüssing3c3769e2013-09-04 02:13:39 +0200755 if (ipv6_addr_is_ll_all_nodes(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900756 return 0;
757
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000758 br_group.u.ip6 = *group;
Linus Lüssing9cc6e0c2011-02-15 13:19:17 +0000759 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000760 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900761
762 return br_multicast_add_group(br, port, &br_group);
763}
764#endif
765
Herbert Xueb1d1642010-02-27 19:41:45 +0000766static void br_multicast_router_expired(unsigned long data)
767{
768 struct net_bridge_port *port = (void *)data;
769 struct net_bridge *br = port->br;
770
771 spin_lock(&br->multicast_lock);
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +0100772 if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
773 port->multicast_router == MDB_RTR_TYPE_PERM ||
Herbert Xueb1d1642010-02-27 19:41:45 +0000774 timer_pending(&port->multicast_router_timer) ||
775 hlist_unhashed(&port->rlist))
776 goto out;
777
778 hlist_del_init_rcu(&port->rlist);
Satish Ashok949f1e32015-07-23 05:00:53 -0700779 br_rtr_notify(br->dev, port, RTM_DELMDB);
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +0100780 /* Don't allow timer refresh if the router expired */
781 if (port->multicast_router == MDB_RTR_TYPE_TEMP)
782 port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Herbert Xueb1d1642010-02-27 19:41:45 +0000783
784out:
785 spin_unlock(&br->multicast_lock);
786}
787
788static void br_multicast_local_router_expired(unsigned long data)
789{
790}
791
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200792static void br_multicast_querier_expired(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +0200793 struct bridge_mcast_own_query *query)
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000794{
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000795 spin_lock(&br->multicast_lock);
796 if (!netif_running(br->dev) || br->multicast_disabled)
797 goto out;
798
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200799 br_multicast_start_querier(br, query);
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000800
801out:
802 spin_unlock(&br->multicast_lock);
803}
804
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200805static void br_ip4_multicast_querier_expired(unsigned long data)
806{
807 struct net_bridge *br = (void *)data;
808
Linus Lüssing90010b32014-06-07 18:26:26 +0200809 br_multicast_querier_expired(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200810}
811
812#if IS_ENABLED(CONFIG_IPV6)
813static void br_ip6_multicast_querier_expired(unsigned long data)
814{
815 struct net_bridge *br = (void *)data;
816
Linus Lüssing90010b32014-06-07 18:26:26 +0200817 br_multicast_querier_expired(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200818}
819#endif
820
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200821static void br_multicast_select_own_querier(struct net_bridge *br,
822 struct br_ip *ip,
823 struct sk_buff *skb)
824{
825 if (ip->proto == htons(ETH_P_IP))
826 br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr;
827#if IS_ENABLED(CONFIG_IPV6)
828 else
829 br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr;
830#endif
831}
832
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900833static void __br_multicast_send_query(struct net_bridge *br,
834 struct net_bridge_port *port,
835 struct br_ip *ip)
Herbert Xueb1d1642010-02-27 19:41:45 +0000836{
Herbert Xueb1d1642010-02-27 19:41:45 +0000837 struct sk_buff *skb;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200838 u8 igmp_type;
Herbert Xueb1d1642010-02-27 19:41:45 +0000839
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200840 skb = br_multicast_alloc_query(br, ip, &igmp_type);
Herbert Xueb1d1642010-02-27 19:41:45 +0000841 if (!skb)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900842 return;
Herbert Xueb1d1642010-02-27 19:41:45 +0000843
844 if (port) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000845 skb->dev = port->dev;
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -0700846 br_multicast_count(br, port, skb, igmp_type,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200847 BR_MCAST_DIR_TX);
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500848 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
849 dev_net(port->dev), NULL, skb, NULL, skb->dev,
Linus Lüssingf0b4eec2014-11-17 12:20:28 +0100850 br_dev_queue_push_xmit);
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200851 } else {
852 br_multicast_select_own_querier(br, ip, skb);
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -0700853 br_multicast_count(br, port, skb, igmp_type,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200854 BR_MCAST_DIR_RX);
Herbert Xueb1d1642010-02-27 19:41:45 +0000855 netif_rx(skb);
Linus Lüssingdc4eb532014-06-07 18:26:27 +0200856 }
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900857}
Herbert Xueb1d1642010-02-27 19:41:45 +0000858
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900859static void br_multicast_send_query(struct net_bridge *br,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200860 struct net_bridge_port *port,
Linus Lüssing90010b32014-06-07 18:26:26 +0200861 struct bridge_mcast_own_query *own_query)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900862{
863 unsigned long time;
864 struct br_ip br_group;
Linus Lüssing90010b32014-06-07 18:26:26 +0200865 struct bridge_mcast_other_query *other_query = NULL;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900866
867 if (!netif_running(br->dev) || br->multicast_disabled ||
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200868 !br->multicast_querier)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900869 return;
870
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900871 memset(&br_group.u, 0, sizeof(br_group.u));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900872
Linus Lüssing90010b32014-06-07 18:26:26 +0200873 if (port ? (own_query == &port->ip4_own_query) :
874 (own_query == &br->ip4_own_query)) {
875 other_query = &br->ip4_other_query;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200876 br_group.proto = htons(ETH_P_IP);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000877#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200878 } else {
Linus Lüssing90010b32014-06-07 18:26:26 +0200879 other_query = &br->ip6_other_query;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200880 br_group.proto = htons(ETH_P_IPV6);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900881#endif
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200882 }
883
Linus Lüssing90010b32014-06-07 18:26:26 +0200884 if (!other_query || timer_pending(&other_query->timer))
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200885 return;
886
887 __br_multicast_send_query(br, port, &br_group);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900888
Herbert Xueb1d1642010-02-27 19:41:45 +0000889 time = jiffies;
Linus Lüssing90010b32014-06-07 18:26:26 +0200890 time += own_query->startup_sent < br->multicast_startup_query_count ?
Herbert Xueb1d1642010-02-27 19:41:45 +0000891 br->multicast_startup_query_interval :
892 br->multicast_query_interval;
Linus Lüssing90010b32014-06-07 18:26:26 +0200893 mod_timer(&own_query->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +0000894}
895
Linus Lüssing90010b32014-06-07 18:26:26 +0200896static void
897br_multicast_port_query_expired(struct net_bridge_port *port,
898 struct bridge_mcast_own_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +0000899{
Herbert Xueb1d1642010-02-27 19:41:45 +0000900 struct net_bridge *br = port->br;
901
902 spin_lock(&br->multicast_lock);
Dan Carpenter02a780c2010-03-06 01:14:09 +0000903 if (port->state == BR_STATE_DISABLED ||
904 port->state == BR_STATE_BLOCKING)
Herbert Xueb1d1642010-02-27 19:41:45 +0000905 goto out;
906
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200907 if (query->startup_sent < br->multicast_startup_query_count)
908 query->startup_sent++;
Herbert Xueb1d1642010-02-27 19:41:45 +0000909
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200910 br_multicast_send_query(port->br, port, query);
Herbert Xueb1d1642010-02-27 19:41:45 +0000911
912out:
913 spin_unlock(&br->multicast_lock);
914}
915
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200916static void br_ip4_multicast_port_query_expired(unsigned long data)
917{
918 struct net_bridge_port *port = (void *)data;
919
Linus Lüssing90010b32014-06-07 18:26:26 +0200920 br_multicast_port_query_expired(port, &port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200921}
922
923#if IS_ENABLED(CONFIG_IPV6)
924static void br_ip6_multicast_port_query_expired(unsigned long data)
925{
926 struct net_bridge_port *port = (void *)data;
927
Linus Lüssing90010b32014-06-07 18:26:26 +0200928 br_multicast_port_query_expired(port, &port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200929}
930#endif
931
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200932int br_multicast_add_port(struct net_bridge_port *port)
Herbert Xueb1d1642010-02-27 19:41:45 +0000933{
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +0100934 port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Herbert Xueb1d1642010-02-27 19:41:45 +0000935
936 setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
937 (unsigned long)port);
Linus Lüssing90010b32014-06-07 18:26:26 +0200938 setup_timer(&port->ip4_own_query.timer,
939 br_ip4_multicast_port_query_expired, (unsigned long)port);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200940#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +0200941 setup_timer(&port->ip6_own_query.timer,
942 br_ip6_multicast_port_query_expired, (unsigned long)port);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200943#endif
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200944 port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
945 if (!port->mcast_stats)
946 return -ENOMEM;
947
948 return 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000949}
950
951void br_multicast_del_port(struct net_bridge_port *port)
952{
Satish Ashoke10177a2015-07-15 07:16:51 -0700953 struct net_bridge *br = port->br;
954 struct net_bridge_port_group *pg;
955 struct hlist_node *n;
956
957 /* Take care of the remaining groups, only perm ones should be left */
958 spin_lock_bh(&br->multicast_lock);
959 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
960 br_multicast_del_pg(br, pg);
961 spin_unlock_bh(&br->multicast_lock);
Herbert Xueb1d1642010-02-27 19:41:45 +0000962 del_timer_sync(&port->multicast_router_timer);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200963 free_percpu(port->mcast_stats);
Herbert Xueb1d1642010-02-27 19:41:45 +0000964}
965
Linus Lüssing90010b32014-06-07 18:26:26 +0200966static void br_multicast_enable(struct bridge_mcast_own_query *query)
Herbert Xu561f1102010-02-27 19:41:50 +0000967{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200968 query->startup_sent = 0;
Herbert Xu561f1102010-02-27 19:41:50 +0000969
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200970 if (try_to_del_timer_sync(&query->timer) >= 0 ||
971 del_timer(&query->timer))
972 mod_timer(&query->timer, jiffies);
Herbert Xu561f1102010-02-27 19:41:50 +0000973}
974
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +0200975static void __br_multicast_enable_port(struct net_bridge_port *port)
Herbert Xueb1d1642010-02-27 19:41:45 +0000976{
977 struct net_bridge *br = port->br;
978
Herbert Xueb1d1642010-02-27 19:41:45 +0000979 if (br->multicast_disabled || !netif_running(br->dev))
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +0200980 return;
Herbert Xueb1d1642010-02-27 19:41:45 +0000981
Linus Lüssing90010b32014-06-07 18:26:26 +0200982 br_multicast_enable(&port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200983#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +0200984 br_multicast_enable(&port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200985#endif
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +0100986 if (port->multicast_router == MDB_RTR_TYPE_PERM &&
987 hlist_unhashed(&port->rlist))
Satish Ashok754bc542015-06-19 01:22:57 -0700988 br_multicast_add_router(br, port);
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +0200989}
Herbert Xueb1d1642010-02-27 19:41:45 +0000990
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +0200991void br_multicast_enable_port(struct net_bridge_port *port)
992{
993 struct net_bridge *br = port->br;
994
995 spin_lock(&br->multicast_lock);
996 __br_multicast_enable_port(port);
Herbert Xueb1d1642010-02-27 19:41:45 +0000997 spin_unlock(&br->multicast_lock);
998}
999
1000void br_multicast_disable_port(struct net_bridge_port *port)
1001{
1002 struct net_bridge *br = port->br;
1003 struct net_bridge_port_group *pg;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001004 struct hlist_node *n;
Herbert Xueb1d1642010-02-27 19:41:45 +00001005
1006 spin_lock(&br->multicast_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001007 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
Elad Raz9d06b6d2016-02-03 09:57:05 +01001008 if (!(pg->flags & MDB_PG_FLAGS_PERMANENT))
Satish Ashoke10177a2015-07-15 07:16:51 -07001009 br_multicast_del_pg(br, pg);
Herbert Xueb1d1642010-02-27 19:41:45 +00001010
Satish Ashok949f1e32015-07-23 05:00:53 -07001011 if (!hlist_unhashed(&port->rlist)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001012 hlist_del_init_rcu(&port->rlist);
Satish Ashok949f1e32015-07-23 05:00:53 -07001013 br_rtr_notify(br->dev, port, RTM_DELMDB);
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001014 /* Don't allow timer refresh if disabling */
1015 if (port->multicast_router == MDB_RTR_TYPE_TEMP)
1016 port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Satish Ashok949f1e32015-07-23 05:00:53 -07001017 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001018 del_timer(&port->multicast_router_timer);
Linus Lüssing90010b32014-06-07 18:26:26 +02001019 del_timer(&port->ip4_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001020#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001021 del_timer(&port->ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001022#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001023 spin_unlock(&br->multicast_lock);
1024}
1025
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001026static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
1027 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001028 struct sk_buff *skb,
1029 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001030{
1031 struct igmpv3_report *ih;
1032 struct igmpv3_grec *grec;
1033 int i;
1034 int len;
1035 int num;
1036 int type;
1037 int err = 0;
1038 __be32 group;
1039
Herbert Xueb1d1642010-02-27 19:41:45 +00001040 ih = igmpv3_report_hdr(skb);
1041 num = ntohs(ih->ngrec);
Linus Lüssingc2d4fbd2015-09-11 18:39:48 +02001042 len = skb_transport_offset(skb) + sizeof(*ih);
Herbert Xueb1d1642010-02-27 19:41:45 +00001043
1044 for (i = 0; i < num; i++) {
1045 len += sizeof(*grec);
1046 if (!pskb_may_pull(skb, len))
1047 return -EINVAL;
1048
Herbert Xufd218cf2010-04-07 21:20:47 -07001049 grec = (void *)(skb->data + len - sizeof(*grec));
Herbert Xueb1d1642010-02-27 19:41:45 +00001050 group = grec->grec_mca;
1051 type = grec->grec_type;
1052
Eric Dumazet8eabf952010-04-20 03:20:05 +00001053 len += ntohs(grec->grec_nsrcs) * 4;
Herbert Xueb1d1642010-02-27 19:41:45 +00001054 if (!pskb_may_pull(skb, len))
1055 return -EINVAL;
1056
1057 /* We treat this as an IGMPv2 report for now. */
1058 switch (type) {
1059 case IGMPV3_MODE_IS_INCLUDE:
1060 case IGMPV3_MODE_IS_EXCLUDE:
1061 case IGMPV3_CHANGE_TO_INCLUDE:
1062 case IGMPV3_CHANGE_TO_EXCLUDE:
1063 case IGMPV3_ALLOW_NEW_SOURCES:
1064 case IGMPV3_BLOCK_OLD_SOURCES:
1065 break;
1066
1067 default:
1068 continue;
1069 }
1070
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001071 if ((type == IGMPV3_CHANGE_TO_INCLUDE ||
1072 type == IGMPV3_MODE_IS_INCLUDE) &&
1073 ntohs(grec->grec_nsrcs) == 0) {
1074 br_ip4_multicast_leave_group(br, port, group, vid);
1075 } else {
1076 err = br_ip4_multicast_add_group(br, port, group, vid);
1077 if (err)
1078 break;
1079 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001080 }
1081
1082 return err;
1083}
1084
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001085#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001086static int br_ip6_multicast_mld2_report(struct net_bridge *br,
1087 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001088 struct sk_buff *skb,
1089 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001090{
1091 struct icmp6hdr *icmp6h;
1092 struct mld2_grec *grec;
1093 int i;
1094 int len;
1095 int num;
1096 int err = 0;
1097
1098 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
1099 return -EINVAL;
1100
1101 icmp6h = icmp6_hdr(skb);
1102 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
Linus Lüssingc2d4fbd2015-09-11 18:39:48 +02001103 len = skb_transport_offset(skb) + sizeof(*icmp6h);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001104
1105 for (i = 0; i < num; i++) {
1106 __be16 *nsrcs, _nsrcs;
1107
1108 nsrcs = skb_header_pointer(skb,
1109 len + offsetof(struct mld2_grec,
Linus Lüssing649e9842011-02-15 13:19:18 +00001110 grec_nsrcs),
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001111 sizeof(_nsrcs), &_nsrcs);
1112 if (!nsrcs)
1113 return -EINVAL;
1114
1115 if (!pskb_may_pull(skb,
1116 len + sizeof(*grec) +
Linus Lüssingd41db9f2011-02-15 13:19:19 +00001117 sizeof(struct in6_addr) * ntohs(*nsrcs)))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001118 return -EINVAL;
1119
1120 grec = (struct mld2_grec *)(skb->data + len);
Linus Lüssingd41db9f2011-02-15 13:19:19 +00001121 len += sizeof(*grec) +
1122 sizeof(struct in6_addr) * ntohs(*nsrcs);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001123
1124 /* We treat these as MLDv1 reports for now. */
1125 switch (grec->grec_type) {
1126 case MLD2_MODE_IS_INCLUDE:
1127 case MLD2_MODE_IS_EXCLUDE:
1128 case MLD2_CHANGE_TO_INCLUDE:
1129 case MLD2_CHANGE_TO_EXCLUDE:
1130 case MLD2_ALLOW_NEW_SOURCES:
1131 case MLD2_BLOCK_OLD_SOURCES:
1132 break;
1133
1134 default:
1135 continue;
1136 }
1137
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001138 if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE ||
1139 grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
1140 ntohs(*nsrcs) == 0) {
1141 br_ip6_multicast_leave_group(br, port, &grec->grec_mca,
1142 vid);
1143 } else {
1144 err = br_ip6_multicast_add_group(br, port,
1145 &grec->grec_mca, vid);
Davide Caratti92642512016-08-31 14:16:44 +02001146 if (err)
Satish Ashokbc8c20a2015-07-13 05:28:37 -07001147 break;
1148 }
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001149 }
1150
1151 return err;
1152}
1153#endif
1154
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001155static bool br_ip4_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001156 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001157 __be32 saddr)
1158{
1159 if (!timer_pending(&br->ip4_own_query.timer) &&
1160 !timer_pending(&br->ip4_other_query.timer))
1161 goto update;
1162
1163 if (!br->ip4_querier.addr.u.ip4)
1164 goto update;
1165
1166 if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4))
1167 goto update;
1168
1169 return false;
1170
1171update:
1172 br->ip4_querier.addr.u.ip4 = saddr;
1173
Linus Lüssing2cd41432014-06-07 18:26:29 +02001174 /* update protected by general multicast_lock by caller */
1175 rcu_assign_pointer(br->ip4_querier.port, port);
1176
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001177 return true;
1178}
1179
1180#if IS_ENABLED(CONFIG_IPV6)
1181static bool br_ip6_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001182 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001183 struct in6_addr *saddr)
1184{
1185 if (!timer_pending(&br->ip6_own_query.timer) &&
1186 !timer_pending(&br->ip6_other_query.timer))
1187 goto update;
1188
1189 if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0)
1190 goto update;
1191
1192 return false;
1193
1194update:
1195 br->ip6_querier.addr.u.ip6 = *saddr;
1196
Linus Lüssing2cd41432014-06-07 18:26:29 +02001197 /* update protected by general multicast_lock by caller */
1198 rcu_assign_pointer(br->ip6_querier.port, port);
1199
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001200 return true;
1201}
1202#endif
1203
1204static bool br_multicast_select_querier(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001205 struct net_bridge_port *port,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001206 struct br_ip *saddr)
1207{
1208 switch (saddr->proto) {
1209 case htons(ETH_P_IP):
Linus Lüssing2cd41432014-06-07 18:26:29 +02001210 return br_ip4_multicast_select_querier(br, port, saddr->u.ip4);
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001211#if IS_ENABLED(CONFIG_IPV6)
1212 case htons(ETH_P_IPV6):
Linus Lüssing2cd41432014-06-07 18:26:29 +02001213 return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6);
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001214#endif
1215 }
1216
1217 return false;
1218}
1219
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001220static void
Linus Lüssing90010b32014-06-07 18:26:26 +02001221br_multicast_update_query_timer(struct net_bridge *br,
1222 struct bridge_mcast_other_query *query,
1223 unsigned long max_delay)
Linus Lüssingb00589a2013-08-01 01:06:20 +02001224{
Linus Lüssing90010b32014-06-07 18:26:26 +02001225 if (!timer_pending(&query->timer))
1226 query->delay_time = jiffies + max_delay;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001227
Linus Lüssing90010b32014-06-07 18:26:26 +02001228 mod_timer(&query->timer, jiffies + br->multicast_querier_interval);
Linus Lüssingb00589a2013-08-01 01:06:20 +02001229}
1230
stephen hemminger7e80c122010-04-27 15:01:04 +00001231/*
Cong Wang7c776022013-06-21 15:37:25 +08001232 * Add port to router_list
stephen hemminger7e80c122010-04-27 15:01:04 +00001233 * list is maintained ordered by pointer value
1234 * and locked by br->multicast_lock and RCU
1235 */
Herbert Xu0909e112010-02-27 19:41:49 +00001236static void br_multicast_add_router(struct net_bridge *br,
1237 struct net_bridge_port *port)
1238{
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001239 struct net_bridge_port *p;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001240 struct hlist_node *slot = NULL;
Herbert Xu0909e112010-02-27 19:41:49 +00001241
Nikolay Aleksandrov1a040ea2015-06-09 10:23:57 -07001242 if (!hlist_unhashed(&port->rlist))
1243 return;
1244
Sasha Levinb67bfe02013-02-27 17:06:00 -08001245 hlist_for_each_entry(p, &br->router_list, rlist) {
stephen hemminger7e80c122010-04-27 15:01:04 +00001246 if ((unsigned long) port >= (unsigned long) p)
1247 break;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001248 slot = &p->rlist;
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001249 }
Herbert Xu0909e112010-02-27 19:41:49 +00001250
stephen hemminger7e80c122010-04-27 15:01:04 +00001251 if (slot)
Ken Helias1d023282014-08-06 16:09:16 -07001252 hlist_add_behind_rcu(&port->rlist, slot);
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001253 else
1254 hlist_add_head_rcu(&port->rlist, &br->router_list);
Satish Ashok949f1e32015-07-23 05:00:53 -07001255 br_rtr_notify(br->dev, port, RTM_NEWMDB);
Herbert Xu0909e112010-02-27 19:41:49 +00001256}
1257
Herbert Xueb1d1642010-02-27 19:41:45 +00001258static void br_multicast_mark_router(struct net_bridge *br,
1259 struct net_bridge_port *port)
1260{
1261 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +00001262
1263 if (!port) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001264 if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY)
Herbert Xueb1d1642010-02-27 19:41:45 +00001265 mod_timer(&br->multicast_router_timer,
1266 now + br->multicast_querier_interval);
1267 return;
1268 }
1269
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001270 if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
1271 port->multicast_router == MDB_RTR_TYPE_PERM)
Herbert Xueb1d1642010-02-27 19:41:45 +00001272 return;
1273
Herbert Xu0909e112010-02-27 19:41:49 +00001274 br_multicast_add_router(br, port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001275
Herbert Xueb1d1642010-02-27 19:41:45 +00001276 mod_timer(&port->multicast_router_timer,
1277 now + br->multicast_querier_interval);
1278}
1279
1280static void br_multicast_query_received(struct net_bridge *br,
1281 struct net_bridge_port *port,
Linus Lüssing90010b32014-06-07 18:26:26 +02001282 struct bridge_mcast_other_query *query,
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001283 struct br_ip *saddr,
Linus Lüssingb00589a2013-08-01 01:06:20 +02001284 unsigned long max_delay)
Herbert Xueb1d1642010-02-27 19:41:45 +00001285{
Linus Lüssing2cd41432014-06-07 18:26:29 +02001286 if (!br_multicast_select_querier(br, port, saddr))
Herbert Xueb1d1642010-02-27 19:41:45 +00001287 return;
1288
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001289 br_multicast_update_query_timer(br, query, max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001290 br_multicast_mark_router(br, port);
1291}
1292
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001293static int br_ip4_multicast_query(struct net_bridge *br,
1294 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001295 struct sk_buff *skb,
1296 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001297{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001298 const struct iphdr *iph = ip_hdr(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001299 struct igmphdr *ih = igmp_hdr(skb);
1300 struct net_bridge_mdb_entry *mp;
1301 struct igmpv3_query *ih3;
1302 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +00001303 struct net_bridge_port_group __rcu **pp;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001304 struct br_ip saddr;
Herbert Xueb1d1642010-02-27 19:41:45 +00001305 unsigned long max_delay;
1306 unsigned long now = jiffies;
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001307 unsigned int offset = skb_transport_offset(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001308 __be32 group;
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001309 int err = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001310
1311 spin_lock(&br->multicast_lock);
1312 if (!netif_running(br->dev) ||
1313 (port && port->state == BR_STATE_DISABLED))
1314 goto out;
1315
Herbert Xueb1d1642010-02-27 19:41:45 +00001316 group = ih->group;
1317
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001318 if (skb->len == offset + sizeof(*ih)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001319 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1320
1321 if (!max_delay) {
1322 max_delay = 10 * HZ;
1323 group = 0;
1324 }
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001325 } else if (skb->len >= offset + sizeof(*ih3)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001326 ih3 = igmpv3_query_hdr(skb);
1327 if (ih3->nsrcs)
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001328 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001329
YOSHIFUJI Hideaki / 吉藤英明0ba8c9e2010-03-15 19:27:00 +00001330 max_delay = ih3->code ?
1331 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001332 } else {
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001333 goto out;
1334 }
1335
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001336 if (!group) {
1337 saddr.proto = htons(ETH_P_IP);
1338 saddr.u.ip4 = iph->saddr;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001339
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001340 br_multicast_query_received(br, port, &br->ip4_other_query,
1341 &saddr, max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001342 goto out;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001343 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001344
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001345 mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001346 if (!mp)
1347 goto out;
1348
1349 max_delay *= br->multicast_last_member_count;
1350
Herbert Xu8a870172011-02-12 01:05:42 -08001351 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001352 (timer_pending(&mp->timer) ?
1353 time_after(mp->timer.expires, now + max_delay) :
1354 try_to_del_timer_sync(&mp->timer) >= 0))
1355 mod_timer(&mp->timer, now + max_delay);
1356
Eric Dumazete8051682010-11-15 06:38:10 +00001357 for (pp = &mp->ports;
1358 (p = mlock_dereference(*pp, br)) != NULL;
1359 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001360 if (timer_pending(&p->timer) ?
1361 time_after(p->timer.expires, now + max_delay) :
1362 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001363 mod_timer(&p->timer, now + max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001364 }
1365
1366out:
1367 spin_unlock(&br->multicast_lock);
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001368 return err;
Herbert Xueb1d1642010-02-27 19:41:45 +00001369}
1370
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001371#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001372static int br_ip6_multicast_query(struct net_bridge *br,
1373 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001374 struct sk_buff *skb,
1375 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001376{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001377 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
stephen hemmingereca2a432012-12-13 06:51:28 +00001378 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001379 struct net_bridge_mdb_entry *mp;
1380 struct mld2_query *mld2q;
Eric Dumazete8051682010-11-15 06:38:10 +00001381 struct net_bridge_port_group *p;
1382 struct net_bridge_port_group __rcu **pp;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001383 struct br_ip saddr;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001384 unsigned long max_delay;
1385 unsigned long now = jiffies;
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001386 unsigned int offset = skb_transport_offset(skb);
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001387 const struct in6_addr *group = NULL;
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001388 bool is_general_query;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001389 int err = 0;
1390
1391 spin_lock(&br->multicast_lock);
1392 if (!netif_running(br->dev) ||
1393 (port && port->state == BR_STATE_DISABLED))
1394 goto out;
1395
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001396 if (skb->len == offset + sizeof(*mld)) {
1397 if (!pskb_may_pull(skb, offset + sizeof(*mld))) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001398 err = -EINVAL;
1399 goto out;
1400 }
1401 mld = (struct mld_msg *) icmp6_hdr(skb);
Li RongQing47152132012-07-09 23:56:12 +00001402 max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001403 if (max_delay)
1404 group = &mld->mld_mca;
Linus Lüssing248ba8e2013-08-06 00:32:05 +02001405 } else {
Linus Lüssing856ce5d2016-05-04 17:25:02 +02001406 if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001407 err = -EINVAL;
1408 goto out;
1409 }
1410 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1411 if (!mld2q->mld2q_nsrcs)
1412 group = &mld2q->mld2q_mca;
Daniel Borkmanne3f5b172013-09-04 00:19:39 +02001413
1414 max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001415 }
1416
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001417 is_general_query = group && ipv6_addr_any(group);
1418
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001419 if (is_general_query) {
1420 saddr.proto = htons(ETH_P_IPV6);
1421 saddr.u.ip6 = ip6h->saddr;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001422
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001423 br_multicast_query_received(br, port, &br->ip6_other_query,
1424 &saddr, max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001425 goto out;
Linus Lüssing6c03ee82014-06-12 01:41:23 +02001426 } else if (!group) {
1427 goto out;
Linus Lüssingdc4eb532014-06-07 18:26:27 +02001428 }
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001429
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001430 mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001431 if (!mp)
1432 goto out;
1433
1434 max_delay *= br->multicast_last_member_count;
Herbert Xu8a870172011-02-12 01:05:42 -08001435 if (mp->mglist &&
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001436 (timer_pending(&mp->timer) ?
1437 time_after(mp->timer.expires, now + max_delay) :
1438 try_to_del_timer_sync(&mp->timer) >= 0))
1439 mod_timer(&mp->timer, now + max_delay);
1440
Eric Dumazete8051682010-11-15 06:38:10 +00001441 for (pp = &mp->ports;
1442 (p = mlock_dereference(*pp, br)) != NULL;
1443 pp = &p->next) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001444 if (timer_pending(&p->timer) ?
1445 time_after(p->timer.expires, now + max_delay) :
1446 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001447 mod_timer(&p->timer, now + max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001448 }
1449
1450out:
1451 spin_unlock(&br->multicast_lock);
1452 return err;
1453}
1454#endif
1455
Linus Lüssing90010b32014-06-07 18:26:26 +02001456static void
1457br_multicast_leave_group(struct net_bridge *br,
1458 struct net_bridge_port *port,
1459 struct br_ip *group,
1460 struct bridge_mcast_other_query *other_query,
1461 struct bridge_mcast_own_query *own_query)
Herbert Xueb1d1642010-02-27 19:41:45 +00001462{
1463 struct net_bridge_mdb_htable *mdb;
1464 struct net_bridge_mdb_entry *mp;
1465 struct net_bridge_port_group *p;
1466 unsigned long now;
1467 unsigned long time;
1468
Herbert Xueb1d1642010-02-27 19:41:45 +00001469 spin_lock(&br->multicast_lock);
1470 if (!netif_running(br->dev) ||
Satish Ashok544586f2015-07-28 03:28:27 -07001471 (port && port->state == BR_STATE_DISABLED))
Herbert Xueb1d1642010-02-27 19:41:45 +00001472 goto out;
1473
Eric Dumazete8051682010-11-15 06:38:10 +00001474 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001475 mp = br_mdb_ip_get(mdb, group);
1476 if (!mp)
1477 goto out;
1478
Satish Ashok544586f2015-07-28 03:28:27 -07001479 if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
1480 struct net_bridge_port_group __rcu **pp;
1481
1482 for (pp = &mp->ports;
1483 (p = mlock_dereference(*pp, br)) != NULL;
1484 pp = &p->next) {
1485 if (p->port != port)
1486 continue;
1487
1488 rcu_assign_pointer(*pp, p->next);
1489 hlist_del_init(&p->mglist);
1490 del_timer(&p->timer);
1491 call_rcu_bh(&p->rcu, br_multicast_free_pg);
Elad Raz45ebcce2016-04-21 12:52:45 +02001492 br_mdb_notify(br->dev, port, group, RTM_DELMDB,
1493 p->flags);
Satish Ashok544586f2015-07-28 03:28:27 -07001494
1495 if (!mp->ports && !mp->mglist &&
1496 netif_running(br->dev))
1497 mod_timer(&mp->timer, jiffies);
1498 }
1499 goto out;
1500 }
1501
1502 if (timer_pending(&other_query->timer))
1503 goto out;
1504
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001505 if (br->multicast_querier) {
Cong Wang6b7df112013-05-21 21:52:56 +00001506 __br_multicast_send_query(br, port, &mp->addr);
1507
1508 time = jiffies + br->multicast_last_member_count *
1509 br->multicast_last_member_interval;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001510
Linus Lüssing90010b32014-06-07 18:26:26 +02001511 mod_timer(&own_query->timer, time);
Cong Wang6b7df112013-05-21 21:52:56 +00001512
1513 for (p = mlock_dereference(mp->ports, br);
1514 p != NULL;
1515 p = mlock_dereference(p->next, br)) {
1516 if (p->port != port)
1517 continue;
1518
1519 if (!hlist_unhashed(&p->mglist) &&
1520 (timer_pending(&p->timer) ?
1521 time_after(p->timer.expires, time) :
1522 try_to_del_timer_sync(&p->timer) >= 0)) {
1523 mod_timer(&p->timer, time);
1524 }
1525
1526 break;
1527 }
1528 }
1529
Herbert Xueb1d1642010-02-27 19:41:45 +00001530 now = jiffies;
1531 time = now + br->multicast_last_member_count *
1532 br->multicast_last_member_interval;
1533
1534 if (!port) {
Linus Lüssing454594f2013-10-20 00:58:57 +02001535 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001536 (timer_pending(&mp->timer) ?
1537 time_after(mp->timer.expires, time) :
1538 try_to_del_timer_sync(&mp->timer) >= 0)) {
1539 mod_timer(&mp->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +00001540 }
Linus Lüssing454594f2013-10-20 00:58:57 +02001541
1542 goto out;
1543 }
1544
1545 for (p = mlock_dereference(mp->ports, br);
1546 p != NULL;
1547 p = mlock_dereference(p->next, br)) {
1548 if (p->port != port)
1549 continue;
1550
1551 if (!hlist_unhashed(&p->mglist) &&
1552 (timer_pending(&p->timer) ?
1553 time_after(p->timer.expires, time) :
1554 try_to_del_timer_sync(&p->timer) >= 0)) {
1555 mod_timer(&p->timer, time);
1556 }
1557
1558 break;
Herbert Xueb1d1642010-02-27 19:41:45 +00001559 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001560out:
1561 spin_unlock(&br->multicast_lock);
1562}
1563
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001564static void br_ip4_multicast_leave_group(struct net_bridge *br,
1565 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001566 __be32 group,
1567 __u16 vid)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001568{
1569 struct br_ip br_group;
Linus Lüssing90010b32014-06-07 18:26:26 +02001570 struct bridge_mcast_own_query *own_query;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001571
1572 if (ipv4_is_local_multicast(group))
1573 return;
1574
Linus Lüssing90010b32014-06-07 18:26:26 +02001575 own_query = port ? &port->ip4_own_query : &br->ip4_own_query;
1576
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001577 br_group.u.ip4 = group;
1578 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001579 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001580
Linus Lüssing90010b32014-06-07 18:26:26 +02001581 br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
1582 own_query);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001583}
1584
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001585#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001586static void br_ip6_multicast_leave_group(struct net_bridge *br,
1587 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001588 const struct in6_addr *group,
1589 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001590{
1591 struct br_ip br_group;
Linus Lüssing90010b32014-06-07 18:26:26 +02001592 struct bridge_mcast_own_query *own_query;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001593
Linus Lüssing3c3769e2013-09-04 02:13:39 +02001594 if (ipv6_addr_is_ll_all_nodes(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001595 return;
1596
Linus Lüssing90010b32014-06-07 18:26:26 +02001597 own_query = port ? &port->ip6_own_query : &br->ip6_own_query;
1598
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001599 br_group.u.ip6 = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001600 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001601 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001602
Linus Lüssing90010b32014-06-07 18:26:26 +02001603 br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
1604 own_query);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001605}
1606#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001607
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001608static void br_multicast_err_count(const struct net_bridge *br,
1609 const struct net_bridge_port *p,
1610 __be16 proto)
1611{
1612 struct bridge_mcast_stats __percpu *stats;
1613 struct bridge_mcast_stats *pstats;
1614
1615 if (!br->multicast_stats_enabled)
1616 return;
1617
1618 if (p)
1619 stats = p->mcast_stats;
1620 else
1621 stats = br->mcast_stats;
1622 if (WARN_ON(!stats))
1623 return;
1624
1625 pstats = this_cpu_ptr(stats);
1626
1627 u64_stats_update_begin(&pstats->syncp);
1628 switch (proto) {
1629 case htons(ETH_P_IP):
1630 pstats->mstats.igmp_parse_errors++;
1631 break;
1632#if IS_ENABLED(CONFIG_IPV6)
1633 case htons(ETH_P_IPV6):
1634 pstats->mstats.mld_parse_errors++;
1635 break;
1636#endif
1637 }
1638 u64_stats_update_end(&pstats->syncp);
1639}
1640
Herbert Xueb1d1642010-02-27 19:41:45 +00001641static int br_multicast_ipv4_rcv(struct net_bridge *br,
1642 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001643 struct sk_buff *skb,
1644 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001645{
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001646 struct sk_buff *skb_trimmed = NULL;
Herbert Xueb1d1642010-02-27 19:41:45 +00001647 struct igmphdr *ih;
Herbert Xueb1d1642010-02-27 19:41:45 +00001648 int err;
1649
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001650 err = ip_mc_check_igmp(skb, &skb_trimmed);
Herbert Xueb1d1642010-02-27 19:41:45 +00001651
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001652 if (err == -ENOMSG) {
1653 if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr))
Herbert Xubd4265f2011-06-23 02:39:12 +00001654 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Herbert Xueb1d1642010-02-27 19:41:45 +00001655 return 0;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001656 } else if (err < 0) {
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001657 br_multicast_err_count(br, port, skb->protocol);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001658 return err;
Herbert Xubd4265f2011-06-23 02:39:12 +00001659 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001660
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001661 ih = igmp_hdr(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001662 BR_INPUT_SKB_CB(skb)->igmp = ih->type;
Herbert Xueb1d1642010-02-27 19:41:45 +00001663
1664 switch (ih->type) {
1665 case IGMP_HOST_MEMBERSHIP_REPORT:
1666 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Fernando Luis Vázquez Cao62b2bcb2011-06-13 15:04:43 +00001667 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001668 err = br_ip4_multicast_add_group(br, port, ih->group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001669 break;
1670 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001671 err = br_ip4_multicast_igmp3_report(br, port, skb_trimmed, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001672 break;
1673 case IGMP_HOST_MEMBERSHIP_QUERY:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001674 err = br_ip4_multicast_query(br, port, skb_trimmed, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001675 break;
1676 case IGMP_HOST_LEAVE_MESSAGE:
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001677 br_ip4_multicast_leave_group(br, port, ih->group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001678 break;
1679 }
1680
Linus Lüssinga5169932015-08-13 05:54:07 +02001681 if (skb_trimmed && skb_trimmed != skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001682 kfree_skb(skb_trimmed);
1683
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07001684 br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001685 BR_MCAST_DIR_RX);
1686
Herbert Xueb1d1642010-02-27 19:41:45 +00001687 return err;
1688}
1689
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001690#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001691static int br_multicast_ipv6_rcv(struct net_bridge *br,
1692 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001693 struct sk_buff *skb,
1694 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001695{
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001696 struct sk_buff *skb_trimmed = NULL;
1697 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001698 int err;
1699
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001700 err = ipv6_mc_check_mld(skb, &skb_trimmed);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001701
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001702 if (err == -ENOMSG) {
1703 if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
1704 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Linus Lüssing8fad9c392013-09-04 02:13:38 +02001705 return 0;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001706 } else if (err < 0) {
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001707 br_multicast_err_count(br, port, skb->protocol);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001708 return err;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001709 }
1710
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001711 mld = (struct mld_msg *)skb_transport_header(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001712 BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001713
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001714 switch (mld->mld_type) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001715 case ICMPV6_MGM_REPORT:
Fernando Luis Vázquez Caofc2af6c2011-06-13 15:06:58 +00001716 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001717 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001718 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001719 case ICMPV6_MLD2_REPORT:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001720 err = br_ip6_multicast_mld2_report(br, port, skb_trimmed, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001721 break;
1722 case ICMPV6_MGM_QUERY:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001723 err = br_ip6_multicast_query(br, port, skb_trimmed, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001724 break;
1725 case ICMPV6_MGM_REDUCTION:
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001726 br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001727 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001728 }
1729
Linus Lüssinga5169932015-08-13 05:54:07 +02001730 if (skb_trimmed && skb_trimmed != skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001731 kfree_skb(skb_trimmed);
1732
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07001733 br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001734 BR_MCAST_DIR_RX);
1735
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001736 return err;
1737}
1738#endif
1739
Herbert Xueb1d1642010-02-27 19:41:45 +00001740int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001741 struct sk_buff *skb, u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001742{
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001743 int ret = 0;
1744
YOSHIFUJI Hideaki / 吉藤英明1fafc7a2010-04-25 08:06:40 +00001745 BR_INPUT_SKB_CB(skb)->igmp = 0;
1746 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1747
Herbert Xueb1d1642010-02-27 19:41:45 +00001748 if (br->multicast_disabled)
1749 return 0;
1750
1751 switch (skb->protocol) {
1752 case htons(ETH_P_IP):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001753 ret = br_multicast_ipv4_rcv(br, port, skb, vid);
1754 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001755#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001756 case htons(ETH_P_IPV6):
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001757 ret = br_multicast_ipv6_rcv(br, port, skb, vid);
1758 break;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001759#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001760 }
1761
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001762 return ret;
Herbert Xueb1d1642010-02-27 19:41:45 +00001763}
1764
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001765static void br_multicast_query_expired(struct net_bridge *br,
Linus Lüssing2cd41432014-06-07 18:26:29 +02001766 struct bridge_mcast_own_query *query,
1767 struct bridge_mcast_querier *querier)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001768{
1769 spin_lock(&br->multicast_lock);
1770 if (query->startup_sent < br->multicast_startup_query_count)
1771 query->startup_sent++;
1772
Eric Dumazet71d9f612015-05-28 04:42:54 -07001773 RCU_INIT_POINTER(querier->port, NULL);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001774 br_multicast_send_query(br, NULL, query);
1775 spin_unlock(&br->multicast_lock);
1776}
1777
1778static void br_ip4_multicast_query_expired(unsigned long data)
Herbert Xueb1d1642010-02-27 19:41:45 +00001779{
1780 struct net_bridge *br = (void *)data;
1781
Linus Lüssing2cd41432014-06-07 18:26:29 +02001782 br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier);
Herbert Xueb1d1642010-02-27 19:41:45 +00001783}
1784
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001785#if IS_ENABLED(CONFIG_IPV6)
1786static void br_ip6_multicast_query_expired(unsigned long data)
1787{
1788 struct net_bridge *br = (void *)data;
1789
Linus Lüssing2cd41432014-06-07 18:26:29 +02001790 br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001791}
1792#endif
1793
Herbert Xueb1d1642010-02-27 19:41:45 +00001794void br_multicast_init(struct net_bridge *br)
1795{
1796 br->hash_elasticity = 4;
1797 br->hash_max = 512;
1798
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001799 br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
Herbert Xuc5c23262012-04-13 02:37:42 +00001800 br->multicast_querier = 0;
Cong Wang1c8ad5b2013-05-21 21:52:54 +00001801 br->multicast_query_use_ifaddr = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001802 br->multicast_last_member_count = 2;
1803 br->multicast_startup_query_count = 2;
1804
1805 br->multicast_last_member_interval = HZ;
1806 br->multicast_query_response_interval = 10 * HZ;
1807 br->multicast_startup_query_interval = 125 * HZ / 4;
1808 br->multicast_query_interval = 125 * HZ;
1809 br->multicast_querier_interval = 255 * HZ;
1810 br->multicast_membership_interval = 260 * HZ;
1811
Linus Lüssing90010b32014-06-07 18:26:26 +02001812 br->ip4_other_query.delay_time = 0;
Linus Lüssing2cd41432014-06-07 18:26:29 +02001813 br->ip4_querier.port = NULL;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001814#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001815 br->ip6_other_query.delay_time = 0;
Linus Lüssing2cd41432014-06-07 18:26:29 +02001816 br->ip6_querier.port = NULL;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001817#endif
daniel0888d5f2016-06-24 12:35:18 +02001818 br->has_ipv6_addr = 1;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001819
Herbert Xueb1d1642010-02-27 19:41:45 +00001820 spin_lock_init(&br->multicast_lock);
1821 setup_timer(&br->multicast_router_timer,
1822 br_multicast_local_router_expired, 0);
Linus Lüssing90010b32014-06-07 18:26:26 +02001823 setup_timer(&br->ip4_other_query.timer,
1824 br_ip4_multicast_querier_expired, (unsigned long)br);
1825 setup_timer(&br->ip4_own_query.timer, br_ip4_multicast_query_expired,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001826 (unsigned long)br);
1827#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001828 setup_timer(&br->ip6_other_query.timer,
1829 br_ip6_multicast_querier_expired, (unsigned long)br);
1830 setup_timer(&br->ip6_own_query.timer, br_ip6_multicast_query_expired,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001831 (unsigned long)br);
1832#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001833}
1834
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001835static void __br_multicast_open(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +02001836 struct bridge_mcast_own_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +00001837{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001838 query->startup_sent = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001839
1840 if (br->multicast_disabled)
1841 return;
1842
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001843 mod_timer(&query->timer, jiffies);
1844}
1845
1846void br_multicast_open(struct net_bridge *br)
1847{
Linus Lüssing90010b32014-06-07 18:26:26 +02001848 __br_multicast_open(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001849#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001850 __br_multicast_open(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001851#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001852}
1853
1854void br_multicast_stop(struct net_bridge *br)
1855{
Herbert Xueb1d1642010-02-27 19:41:45 +00001856 del_timer_sync(&br->multicast_router_timer);
Linus Lüssing90010b32014-06-07 18:26:26 +02001857 del_timer_sync(&br->ip4_other_query.timer);
1858 del_timer_sync(&br->ip4_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001859#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02001860 del_timer_sync(&br->ip6_other_query.timer);
1861 del_timer_sync(&br->ip6_own_query.timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001862#endif
Satish Ashoke10177a2015-07-15 07:16:51 -07001863}
1864
1865void br_multicast_dev_del(struct net_bridge *br)
1866{
1867 struct net_bridge_mdb_htable *mdb;
1868 struct net_bridge_mdb_entry *mp;
1869 struct hlist_node *n;
1870 u32 ver;
1871 int i;
Herbert Xueb1d1642010-02-27 19:41:45 +00001872
1873 spin_lock_bh(&br->multicast_lock);
Eric Dumazete8051682010-11-15 06:38:10 +00001874 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001875 if (!mdb)
1876 goto out;
1877
1878 br->mdb = NULL;
1879
1880 ver = mdb->ver;
1881 for (i = 0; i < mdb->max; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001882 hlist_for_each_entry_safe(mp, n, &mdb->mhash[i],
Herbert Xueb1d1642010-02-27 19:41:45 +00001883 hlist[ver]) {
1884 del_timer(&mp->timer);
Herbert Xueb1d1642010-02-27 19:41:45 +00001885 call_rcu_bh(&mp->rcu, br_multicast_free_group);
1886 }
1887 }
1888
1889 if (mdb->old) {
1890 spin_unlock_bh(&br->multicast_lock);
Herbert Xu10cc2b52010-03-05 21:03:35 +00001891 rcu_barrier_bh();
Herbert Xueb1d1642010-02-27 19:41:45 +00001892 spin_lock_bh(&br->multicast_lock);
1893 WARN_ON(mdb->old);
1894 }
1895
1896 mdb->old = mdb;
1897 call_rcu_bh(&mdb->rcu, br_mdb_free);
1898
1899out:
1900 spin_unlock_bh(&br->multicast_lock);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001901
1902 free_percpu(br->mcast_stats);
Herbert Xueb1d1642010-02-27 19:41:45 +00001903}
Herbert Xu0909e112010-02-27 19:41:49 +00001904
1905int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1906{
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02001907 int err = -EINVAL;
Herbert Xu0909e112010-02-27 19:41:49 +00001908
1909 spin_lock_bh(&br->multicast_lock);
Herbert Xu0909e112010-02-27 19:41:49 +00001910
1911 switch (val) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001912 case MDB_RTR_TYPE_DISABLED:
1913 case MDB_RTR_TYPE_PERM:
Herbert Xu0909e112010-02-27 19:41:49 +00001914 del_timer(&br->multicast_router_timer);
1915 /* fall through */
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001916 case MDB_RTR_TYPE_TEMP_QUERY:
Herbert Xu0909e112010-02-27 19:41:49 +00001917 br->multicast_router = val;
1918 err = 0;
1919 break;
Herbert Xu0909e112010-02-27 19:41:49 +00001920 }
1921
Herbert Xu0909e112010-02-27 19:41:49 +00001922 spin_unlock_bh(&br->multicast_lock);
1923
1924 return err;
1925}
1926
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001927static void __del_port_router(struct net_bridge_port *p)
1928{
1929 if (hlist_unhashed(&p->rlist))
1930 return;
1931 hlist_del_init_rcu(&p->rlist);
1932 br_rtr_notify(p->br->dev, p, RTM_DELMDB);
1933}
1934
Herbert Xu0909e112010-02-27 19:41:49 +00001935int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1936{
1937 struct net_bridge *br = p->br;
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001938 unsigned long now = jiffies;
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02001939 int err = -EINVAL;
Herbert Xu0909e112010-02-27 19:41:49 +00001940
1941 spin_lock(&br->multicast_lock);
Nikolay Aleksandrov4950cfd2016-02-26 21:20:02 +01001942 if (p->multicast_router == val) {
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001943 /* Refresh the temp router port timer */
1944 if (p->multicast_router == MDB_RTR_TYPE_TEMP)
1945 mod_timer(&p->multicast_router_timer,
1946 now + br->multicast_querier_interval);
Nikolay Aleksandrov4950cfd2016-02-26 21:20:02 +01001947 err = 0;
1948 goto unlock;
1949 }
Herbert Xu0909e112010-02-27 19:41:49 +00001950 switch (val) {
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001951 case MDB_RTR_TYPE_DISABLED:
1952 p->multicast_router = MDB_RTR_TYPE_DISABLED;
1953 __del_port_router(p);
Herbert Xu0909e112010-02-27 19:41:49 +00001954 del_timer(&p->multicast_router_timer);
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001955 break;
1956 case MDB_RTR_TYPE_TEMP_QUERY:
1957 p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
1958 __del_port_router(p);
1959 break;
1960 case MDB_RTR_TYPE_PERM:
1961 p->multicast_router = MDB_RTR_TYPE_PERM;
1962 del_timer(&p->multicast_router_timer);
Herbert Xu0909e112010-02-27 19:41:49 +00001963 br_multicast_add_router(br, p);
1964 break;
Nikolay Aleksandrova55d8242016-02-26 21:20:03 +01001965 case MDB_RTR_TYPE_TEMP:
1966 p->multicast_router = MDB_RTR_TYPE_TEMP;
1967 br_multicast_mark_router(br, p);
1968 break;
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001969 default:
1970 goto unlock;
Herbert Xu0909e112010-02-27 19:41:49 +00001971 }
Nikolay Aleksandrov7f0aec72016-02-26 21:20:01 +01001972 err = 0;
1973unlock:
Herbert Xu0909e112010-02-27 19:41:49 +00001974 spin_unlock(&br->multicast_lock);
1975
1976 return err;
1977}
Herbert Xu561f1102010-02-27 19:41:50 +00001978
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001979static void br_multicast_start_querier(struct net_bridge *br,
Linus Lüssing90010b32014-06-07 18:26:26 +02001980 struct bridge_mcast_own_query *query)
Herbert Xu561f1102010-02-27 19:41:50 +00001981{
1982 struct net_bridge_port *port;
Herbert Xu74857212012-04-13 02:37:42 +00001983
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001984 __br_multicast_open(br, query);
Herbert Xu74857212012-04-13 02:37:42 +00001985
1986 list_for_each_entry(port, &br->port_list, list) {
1987 if (port->state == BR_STATE_DISABLED ||
1988 port->state == BR_STATE_BLOCKING)
1989 continue;
1990
Linus Lüssing90010b32014-06-07 18:26:26 +02001991 if (query == &br->ip4_own_query)
1992 br_multicast_enable(&port->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001993#if IS_ENABLED(CONFIG_IPV6)
1994 else
Linus Lüssing90010b32014-06-07 18:26:26 +02001995 br_multicast_enable(&port->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001996#endif
Herbert Xu74857212012-04-13 02:37:42 +00001997 }
1998}
1999
2000int br_multicast_toggle(struct net_bridge *br, unsigned long val)
2001{
Eric Dumazete8051682010-11-15 06:38:10 +00002002 struct net_bridge_mdb_htable *mdb;
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02002003 struct net_bridge_port *port;
2004 int err = 0;
Herbert Xu561f1102010-02-27 19:41:50 +00002005
Andrey Vaginef5e0d82011-11-10 05:48:03 +00002006 spin_lock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00002007 if (br->multicast_disabled == !val)
2008 goto unlock;
2009
2010 br->multicast_disabled = !val;
2011 if (br->multicast_disabled)
2012 goto unlock;
2013
Herbert Xu3a7fda02010-07-29 00:45:30 +00002014 if (!netif_running(br->dev))
2015 goto unlock;
2016
Eric Dumazete8051682010-11-15 06:38:10 +00002017 mdb = mlock_dereference(br->mdb, br);
2018 if (mdb) {
2019 if (mdb->old) {
Herbert Xu561f1102010-02-27 19:41:50 +00002020 err = -EEXIST;
2021rollback:
2022 br->multicast_disabled = !!val;
2023 goto unlock;
2024 }
2025
Eric Dumazete8051682010-11-15 06:38:10 +00002026 err = br_mdb_rehash(&br->mdb, mdb->max,
Herbert Xu561f1102010-02-27 19:41:50 +00002027 br->hash_elasticity);
2028 if (err)
2029 goto rollback;
2030 }
2031
Nikolay Aleksandrov7cb3f922016-10-18 18:09:48 +02002032 br_multicast_open(br);
2033 list_for_each_entry(port, &br->port_list, list)
2034 __br_multicast_enable_port(port);
Herbert Xu561f1102010-02-27 19:41:50 +00002035
2036unlock:
Andrey Vaginef5e0d82011-11-10 05:48:03 +00002037 spin_unlock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00002038
2039 return err;
2040}
Herbert Xub1951672010-02-27 19:41:51 +00002041
Herbert Xuc5c23262012-04-13 02:37:42 +00002042int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
2043{
Linus Lüssingb00589a2013-08-01 01:06:20 +02002044 unsigned long max_delay;
2045
Herbert Xuc5c23262012-04-13 02:37:42 +00002046 val = !!val;
2047
2048 spin_lock_bh(&br->multicast_lock);
2049 if (br->multicast_querier == val)
2050 goto unlock;
2051
2052 br->multicast_querier = val;
Linus Lüssingb00589a2013-08-01 01:06:20 +02002053 if (!val)
2054 goto unlock;
2055
2056 max_delay = br->multicast_query_response_interval;
Linus Lüssingb00589a2013-08-01 01:06:20 +02002057
Linus Lüssing90010b32014-06-07 18:26:26 +02002058 if (!timer_pending(&br->ip4_other_query.timer))
2059 br->ip4_other_query.delay_time = jiffies + max_delay;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002060
Linus Lüssing90010b32014-06-07 18:26:26 +02002061 br_multicast_start_querier(br, &br->ip4_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002062
2063#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing90010b32014-06-07 18:26:26 +02002064 if (!timer_pending(&br->ip6_other_query.timer))
2065 br->ip6_other_query.delay_time = jiffies + max_delay;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002066
Linus Lüssing90010b32014-06-07 18:26:26 +02002067 br_multicast_start_querier(br, &br->ip6_own_query);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002068#endif
Herbert Xuc5c23262012-04-13 02:37:42 +00002069
2070unlock:
2071 spin_unlock_bh(&br->multicast_lock);
2072
2073 return 0;
2074}
2075
Herbert Xub1951672010-02-27 19:41:51 +00002076int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
2077{
Linus Lüssing6ae4ae82015-05-23 03:12:34 +02002078 int err = -EINVAL;
Herbert Xub1951672010-02-27 19:41:51 +00002079 u32 old;
Eric Dumazete8051682010-11-15 06:38:10 +00002080 struct net_bridge_mdb_htable *mdb;
Herbert Xub1951672010-02-27 19:41:51 +00002081
Curt Brunefe0d6922014-01-06 11:00:32 -08002082 spin_lock_bh(&br->multicast_lock);
Herbert Xub1951672010-02-27 19:41:51 +00002083 if (!is_power_of_2(val))
2084 goto unlock;
Eric Dumazete8051682010-11-15 06:38:10 +00002085
2086 mdb = mlock_dereference(br->mdb, br);
2087 if (mdb && val < mdb->size)
Herbert Xub1951672010-02-27 19:41:51 +00002088 goto unlock;
2089
2090 err = 0;
2091
2092 old = br->hash_max;
2093 br->hash_max = val;
2094
Eric Dumazete8051682010-11-15 06:38:10 +00002095 if (mdb) {
2096 if (mdb->old) {
Herbert Xub1951672010-02-27 19:41:51 +00002097 err = -EEXIST;
2098rollback:
2099 br->hash_max = old;
2100 goto unlock;
2101 }
2102
2103 err = br_mdb_rehash(&br->mdb, br->hash_max,
2104 br->hash_elasticity);
2105 if (err)
2106 goto rollback;
2107 }
2108
2109unlock:
Curt Brunefe0d6922014-01-06 11:00:32 -08002110 spin_unlock_bh(&br->multicast_lock);
Herbert Xub1951672010-02-27 19:41:51 +00002111
2112 return err;
2113}
Linus Lüssing07f8ac42014-06-07 18:26:28 +02002114
2115/**
2116 * br_multicast_list_adjacent - Returns snooped multicast addresses
2117 * @dev: The bridge port adjacent to which to retrieve addresses
2118 * @br_ip_list: The list to store found, snooped multicast IP addresses in
2119 *
2120 * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
2121 * snooping feature on all bridge ports of dev's bridge device, excluding
2122 * the addresses from dev itself.
2123 *
2124 * Returns the number of items added to br_ip_list.
2125 *
2126 * Notes:
2127 * - br_ip_list needs to be initialized by caller
2128 * - br_ip_list might contain duplicates in the end
2129 * (needs to be taken care of by caller)
2130 * - br_ip_list needs to be freed by caller
2131 */
2132int br_multicast_list_adjacent(struct net_device *dev,
2133 struct list_head *br_ip_list)
2134{
2135 struct net_bridge *br;
2136 struct net_bridge_port *port;
2137 struct net_bridge_port_group *group;
2138 struct br_ip_list *entry;
2139 int count = 0;
2140
2141 rcu_read_lock();
2142 if (!br_ip_list || !br_port_exists(dev))
2143 goto unlock;
2144
2145 port = br_port_get_rcu(dev);
2146 if (!port || !port->br)
2147 goto unlock;
2148
2149 br = port->br;
2150
2151 list_for_each_entry_rcu(port, &br->port_list, list) {
2152 if (!port->dev || port->dev == dev)
2153 continue;
2154
2155 hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
2156 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
2157 if (!entry)
2158 goto unlock;
2159
2160 entry->addr = group->addr;
2161 list_add(&entry->list, br_ip_list);
2162 count++;
2163 }
2164 }
2165
2166unlock:
2167 rcu_read_unlock();
2168 return count;
2169}
2170EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
Linus Lüssing2cd41432014-06-07 18:26:29 +02002171
2172/**
Linus Lüssingc34963e2014-07-07 05:41:17 +02002173 * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2174 * @dev: The bridge port providing the bridge on which to check for a querier
2175 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2176 *
2177 * Checks whether the given interface has a bridge on top and if so returns
2178 * true if a valid querier exists anywhere on the bridged link layer.
2179 * Otherwise returns false.
2180 */
2181bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
2182{
2183 struct net_bridge *br;
2184 struct net_bridge_port *port;
2185 struct ethhdr eth;
2186 bool ret = false;
2187
2188 rcu_read_lock();
2189 if (!br_port_exists(dev))
2190 goto unlock;
2191
2192 port = br_port_get_rcu(dev);
2193 if (!port || !port->br)
2194 goto unlock;
2195
2196 br = port->br;
2197
2198 memset(&eth, 0, sizeof(eth));
2199 eth.h_proto = htons(proto);
2200
2201 ret = br_multicast_querier_exists(br, &eth);
2202
2203unlock:
2204 rcu_read_unlock();
2205 return ret;
2206}
2207EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
2208
2209/**
Linus Lüssing2cd41432014-06-07 18:26:29 +02002210 * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
2211 * @dev: The bridge port adjacent to which to check for a querier
2212 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2213 *
2214 * Checks whether the given interface has a bridge on top and if so returns
2215 * true if a selected querier is behind one of the other ports of this
2216 * bridge. Otherwise returns false.
2217 */
2218bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
2219{
2220 struct net_bridge *br;
2221 struct net_bridge_port *port;
2222 bool ret = false;
2223
2224 rcu_read_lock();
2225 if (!br_port_exists(dev))
2226 goto unlock;
2227
2228 port = br_port_get_rcu(dev);
2229 if (!port || !port->br)
2230 goto unlock;
2231
2232 br = port->br;
2233
2234 switch (proto) {
2235 case ETH_P_IP:
2236 if (!timer_pending(&br->ip4_other_query.timer) ||
2237 rcu_dereference(br->ip4_querier.port) == port)
2238 goto unlock;
2239 break;
Linus Lüssing3993c4e2014-06-12 01:41:24 +02002240#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing2cd41432014-06-07 18:26:29 +02002241 case ETH_P_IPV6:
2242 if (!timer_pending(&br->ip6_other_query.timer) ||
2243 rcu_dereference(br->ip6_querier.port) == port)
2244 goto unlock;
2245 break;
Linus Lüssing3993c4e2014-06-12 01:41:24 +02002246#endif
Linus Lüssing2cd41432014-06-07 18:26:29 +02002247 default:
2248 goto unlock;
2249 }
2250
2251 ret = true;
2252unlock:
2253 rcu_read_unlock();
2254 return ret;
2255}
2256EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002257
2258static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002259 const struct sk_buff *skb, u8 type, u8 dir)
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002260{
2261 struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002262 __be16 proto = skb->protocol;
2263 unsigned int t_len;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002264
2265 u64_stats_update_begin(&pstats->syncp);
2266 switch (proto) {
2267 case htons(ETH_P_IP):
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002268 t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002269 switch (type) {
2270 case IGMP_HOST_MEMBERSHIP_REPORT:
2271 pstats->mstats.igmp_v1reports[dir]++;
2272 break;
2273 case IGMPV2_HOST_MEMBERSHIP_REPORT:
2274 pstats->mstats.igmp_v2reports[dir]++;
2275 break;
2276 case IGMPV3_HOST_MEMBERSHIP_REPORT:
2277 pstats->mstats.igmp_v3reports[dir]++;
2278 break;
2279 case IGMP_HOST_MEMBERSHIP_QUERY:
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002280 if (t_len != sizeof(struct igmphdr)) {
2281 pstats->mstats.igmp_v3queries[dir]++;
2282 } else {
2283 unsigned int offset = skb_transport_offset(skb);
2284 struct igmphdr *ih, _ihdr;
2285
2286 ih = skb_header_pointer(skb, offset,
2287 sizeof(_ihdr), &_ihdr);
2288 if (!ih)
2289 break;
2290 if (!ih->code)
2291 pstats->mstats.igmp_v1queries[dir]++;
2292 else
2293 pstats->mstats.igmp_v2queries[dir]++;
2294 }
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002295 break;
2296 case IGMP_HOST_LEAVE_MESSAGE:
2297 pstats->mstats.igmp_leaves[dir]++;
2298 break;
2299 }
2300 break;
2301#if IS_ENABLED(CONFIG_IPV6)
2302 case htons(ETH_P_IPV6):
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002303 t_len = ntohs(ipv6_hdr(skb)->payload_len) +
2304 sizeof(struct ipv6hdr);
2305 t_len -= skb_network_header_len(skb);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002306 switch (type) {
2307 case ICMPV6_MGM_REPORT:
2308 pstats->mstats.mld_v1reports[dir]++;
2309 break;
2310 case ICMPV6_MLD2_REPORT:
2311 pstats->mstats.mld_v2reports[dir]++;
2312 break;
2313 case ICMPV6_MGM_QUERY:
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002314 if (t_len != sizeof(struct mld_msg))
2315 pstats->mstats.mld_v2queries[dir]++;
2316 else
2317 pstats->mstats.mld_v1queries[dir]++;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002318 break;
2319 case ICMPV6_MGM_REDUCTION:
2320 pstats->mstats.mld_leaves[dir]++;
2321 break;
2322 }
2323 break;
2324#endif /* CONFIG_IPV6 */
2325 }
2326 u64_stats_update_end(&pstats->syncp);
2327}
2328
2329void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002330 const struct sk_buff *skb, u8 type, u8 dir)
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002331{
2332 struct bridge_mcast_stats __percpu *stats;
2333
2334 /* if multicast_disabled is true then igmp type can't be set */
2335 if (!type || !br->multicast_stats_enabled)
2336 return;
2337
2338 if (p)
2339 stats = p->mcast_stats;
2340 else
2341 stats = br->mcast_stats;
2342 if (WARN_ON(!stats))
2343 return;
2344
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002345 br_mcast_stats_add(stats, skb, type, dir);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002346}
2347
2348int br_multicast_init_stats(struct net_bridge *br)
2349{
2350 br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
2351 if (!br->mcast_stats)
2352 return -ENOMEM;
2353
2354 return 0;
2355}
2356
2357static void mcast_stats_add_dir(u64 *dst, u64 *src)
2358{
2359 dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
2360 dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
2361}
2362
2363void br_multicast_get_stats(const struct net_bridge *br,
2364 const struct net_bridge_port *p,
2365 struct br_mcast_stats *dest)
2366{
2367 struct bridge_mcast_stats __percpu *stats;
2368 struct br_mcast_stats tdst;
2369 int i;
2370
2371 memset(dest, 0, sizeof(*dest));
2372 if (p)
2373 stats = p->mcast_stats;
2374 else
2375 stats = br->mcast_stats;
2376 if (WARN_ON(!stats))
2377 return;
2378
2379 memset(&tdst, 0, sizeof(tdst));
2380 for_each_possible_cpu(i) {
2381 struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
2382 struct br_mcast_stats temp;
2383 unsigned int start;
2384
2385 do {
2386 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
2387 memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
2388 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
2389
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002390 mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
2391 mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
2392 mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002393 mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
2394 mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
2395 mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
2396 mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
2397 tdst.igmp_parse_errors += temp.igmp_parse_errors;
2398
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -07002399 mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
2400 mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02002401 mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
2402 mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
2403 mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
2404 tdst.mld_parse_errors += temp.mld_parse_errors;
2405 }
2406 memcpy(dest, &tdst, sizeof(*dest));
2407}