blob: 9d1d0e66c357b22f8354b0634ca10270d72d5e1b [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>
14#include <linux/if_ether.h>
15#include <linux/igmp.h>
16#include <linux/jhash.h>
17#include <linux/kernel.h>
Herbert Xub1951672010-02-27 19:41:51 +000018#include <linux/log2.h>
Herbert Xueb1d1642010-02-27 19:41:45 +000019#include <linux/netdevice.h>
20#include <linux/netfilter_bridge.h>
21#include <linux/random.h>
22#include <linux/rculist.h>
23#include <linux/skbuff.h>
24#include <linux/slab.h>
25#include <linux/timer.h>
Cong Wang1c8ad5b2013-05-21 21:52:54 +000026#include <linux/inetdevice.h>
Herbert Xueb1d1642010-02-27 19:41:45 +000027#include <net/ip.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000028#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090029#include <net/ipv6.h>
30#include <net/mld.h>
David S. Millerd4c4f072010-04-27 10:16:54 -070031#include <net/ip6_checksum.h>
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090032#endif
Herbert Xueb1d1642010-02-27 19:41:45 +000033
34#include "br_private.h"
35
Linus Lüssingcc0fdd82013-08-30 17:28:17 +020036static void br_multicast_start_querier(struct net_bridge *br,
37 struct bridge_mcast_query *query);
Cong Wang2ce297f2012-12-10 02:15:35 +000038unsigned int br_mdb_rehash_seq;
Herbert Xuc83b8fa2012-04-13 02:37:42 +000039
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090040static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
41{
42 if (a->proto != b->proto)
43 return 0;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000044 if (a->vid != b->vid)
45 return 0;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090046 switch (a->proto) {
47 case htons(ETH_P_IP):
48 return a->u.ip4 == b->u.ip4;
Eric Dumazetdfd56b82011-12-10 09:48:31 +000049#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090050 case htons(ETH_P_IPV6):
51 return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
52#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090053 }
54 return 0;
55}
56
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000057static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip,
58 __u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +000059{
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000060 return jhash_2words((__force u32)ip, vid, mdb->secret) & (mdb->max - 1);
Herbert Xueb1d1642010-02-27 19:41:45 +000061}
62
Eric Dumazetdfd56b82011-12-10 09:48:31 +000063#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090064static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000065 const struct in6_addr *ip,
66 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090067{
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000068 return jhash_2words(ipv6_addr_hash(ip), vid,
69 mdb->secret) & (mdb->max - 1);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090070}
71#endif
72
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090073static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb,
74 struct br_ip *ip)
75{
76 switch (ip->proto) {
77 case htons(ETH_P_IP):
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000078 return __br_ip4_hash(mdb, ip->u.ip4, ip->vid);
Eric Dumazetdfd56b82011-12-10 09:48:31 +000079#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090080 case htons(ETH_P_IPV6):
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000081 return __br_ip6_hash(mdb, &ip->u.ip6, ip->vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090082#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090083 }
84 return 0;
Herbert Xueb1d1642010-02-27 19:41:45 +000085}
86
87static struct net_bridge_mdb_entry *__br_mdb_ip_get(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090088 struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +000089{
90 struct net_bridge_mdb_entry *mp;
Herbert Xueb1d1642010-02-27 19:41:45 +000091
Sasha Levinb67bfe02013-02-27 17:06:00 -080092 hlist_for_each_entry_rcu(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090093 if (br_ip_equal(&mp->addr, dst))
Herbert Xueb1d1642010-02-27 19:41:45 +000094 return mp;
95 }
96
97 return NULL;
98}
99
Cong Wangcfd56752012-12-11 22:23:08 +0000100struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge_mdb_htable *mdb,
101 struct br_ip *dst)
Herbert Xu7f285fa2010-07-05 14:50:08 +0000102{
103 if (!mdb)
104 return NULL;
105
106 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
107}
108
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900109static struct net_bridge_mdb_entry *br_mdb_ip4_get(
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000110 struct net_bridge_mdb_htable *mdb, __be32 dst, __u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000111{
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900112 struct br_ip br_dst;
Herbert Xu0821ec52010-03-15 20:38:25 -0700113
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900114 br_dst.u.ip4 = dst;
115 br_dst.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000116 br_dst.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900117
Herbert Xu7f285fa2010-07-05 14:50:08 +0000118 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900119}
120
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000121#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900122static struct net_bridge_mdb_entry *br_mdb_ip6_get(
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000123 struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst,
124 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900125{
126 struct br_ip br_dst;
127
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000128 br_dst.u.ip6 = *dst;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900129 br_dst.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000130 br_dst.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900131
Herbert Xu7f285fa2010-07-05 14:50:08 +0000132 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900133}
134#endif
135
Herbert Xueb1d1642010-02-27 19:41:45 +0000136struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
Cong Wangfbca58a2013-03-07 03:05:33 +0000137 struct sk_buff *skb, u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000138{
Eric Dumazete8051682010-11-15 06:38:10 +0000139 struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900140 struct br_ip ip;
141
Herbert Xu7f285fa2010-07-05 14:50:08 +0000142 if (br->multicast_disabled)
Herbert Xueb1d1642010-02-27 19:41:45 +0000143 return NULL;
144
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900145 if (BR_INPUT_SKB_CB(skb)->igmp)
146 return NULL;
147
148 ip.proto = skb->protocol;
Cong Wangfbca58a2013-03-07 03:05:33 +0000149 ip.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900150
Herbert Xueb1d1642010-02-27 19:41:45 +0000151 switch (skb->protocol) {
152 case htons(ETH_P_IP):
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900153 ip.u.ip4 = ip_hdr(skb)->daddr;
154 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000155#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900156 case htons(ETH_P_IPV6):
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000157 ip.u.ip6 = ipv6_hdr(skb)->daddr;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900158 break;
159#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900160 default:
161 return NULL;
Herbert Xueb1d1642010-02-27 19:41:45 +0000162 }
163
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900164 return br_mdb_ip_get(mdb, &ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000165}
166
167static void br_mdb_free(struct rcu_head *head)
168{
169 struct net_bridge_mdb_htable *mdb =
170 container_of(head, struct net_bridge_mdb_htable, rcu);
171 struct net_bridge_mdb_htable *old = mdb->old;
172
173 mdb->old = NULL;
174 kfree(old->mhash);
175 kfree(old);
176}
177
178static int br_mdb_copy(struct net_bridge_mdb_htable *new,
179 struct net_bridge_mdb_htable *old,
180 int elasticity)
181{
182 struct net_bridge_mdb_entry *mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000183 int maxlen;
184 int len;
185 int i;
186
187 for (i = 0; i < old->max; i++)
Sasha Levinb67bfe02013-02-27 17:06:00 -0800188 hlist_for_each_entry(mp, &old->mhash[i], hlist[old->ver])
Herbert Xueb1d1642010-02-27 19:41:45 +0000189 hlist_add_head(&mp->hlist[new->ver],
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900190 &new->mhash[br_ip_hash(new, &mp->addr)]);
Herbert Xueb1d1642010-02-27 19:41:45 +0000191
192 if (!elasticity)
193 return 0;
194
195 maxlen = 0;
196 for (i = 0; i < new->max; i++) {
197 len = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800198 hlist_for_each_entry(mp, &new->mhash[i], hlist[new->ver])
Herbert Xueb1d1642010-02-27 19:41:45 +0000199 len++;
200 if (len > maxlen)
201 maxlen = len;
202 }
203
204 return maxlen > elasticity ? -EINVAL : 0;
205}
206
Cong Wangcfd56752012-12-11 22:23:08 +0000207void br_multicast_free_pg(struct rcu_head *head)
Herbert Xueb1d1642010-02-27 19:41:45 +0000208{
209 struct net_bridge_port_group *p =
210 container_of(head, struct net_bridge_port_group, rcu);
211
212 kfree(p);
213}
214
215static void br_multicast_free_group(struct rcu_head *head)
216{
217 struct net_bridge_mdb_entry *mp =
218 container_of(head, struct net_bridge_mdb_entry, rcu);
219
220 kfree(mp);
221}
222
223static void br_multicast_group_expired(unsigned long data)
224{
225 struct net_bridge_mdb_entry *mp = (void *)data;
226 struct net_bridge *br = mp->br;
227 struct net_bridge_mdb_htable *mdb;
228
229 spin_lock(&br->multicast_lock);
230 if (!netif_running(br->dev) || timer_pending(&mp->timer))
231 goto out;
232
Herbert Xu8a870172011-02-12 01:05:42 -0800233 mp->mglist = false;
Herbert Xueb1d1642010-02-27 19:41:45 +0000234
235 if (mp->ports)
236 goto out;
237
Eric Dumazete8051682010-11-15 06:38:10 +0000238 mdb = mlock_dereference(br->mdb, br);
239
Herbert Xueb1d1642010-02-27 19:41:45 +0000240 hlist_del_rcu(&mp->hlist[mdb->ver]);
241 mdb->size--;
242
Herbert Xueb1d1642010-02-27 19:41:45 +0000243 call_rcu_bh(&mp->rcu, br_multicast_free_group);
244
245out:
246 spin_unlock(&br->multicast_lock);
247}
248
249static void br_multicast_del_pg(struct net_bridge *br,
250 struct net_bridge_port_group *pg)
251{
Eric Dumazete8051682010-11-15 06:38:10 +0000252 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000253 struct net_bridge_mdb_entry *mp;
254 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +0000255 struct net_bridge_port_group __rcu **pp;
256
257 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +0000258
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900259 mp = br_mdb_ip_get(mdb, &pg->addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000260 if (WARN_ON(!mp))
261 return;
262
Eric Dumazete8051682010-11-15 06:38:10 +0000263 for (pp = &mp->ports;
264 (p = mlock_dereference(*pp, br)) != NULL;
265 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000266 if (p != pg)
267 continue;
268
stephen hemminger83f6a742010-04-27 15:01:06 +0000269 rcu_assign_pointer(*pp, p->next);
Herbert Xueb1d1642010-02-27 19:41:45 +0000270 hlist_del_init(&p->mglist);
271 del_timer(&p->timer);
Herbert Xueb1d1642010-02-27 19:41:45 +0000272 call_rcu_bh(&p->rcu, br_multicast_free_pg);
273
Cong Wangc7e8e8a2013-07-05 19:36:17 +0800274 if (!mp->ports && !mp->mglist && mp->timer_armed &&
Herbert Xueb1d1642010-02-27 19:41:45 +0000275 netif_running(br->dev))
276 mod_timer(&mp->timer, jiffies);
277
278 return;
279 }
280
281 WARN_ON(1);
282}
283
284static void br_multicast_port_group_expired(unsigned long data)
285{
286 struct net_bridge_port_group *pg = (void *)data;
287 struct net_bridge *br = pg->port->br;
288
289 spin_lock(&br->multicast_lock);
290 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
Amerigo Wangccb1c312012-12-14 22:09:51 +0000291 hlist_unhashed(&pg->mglist) || pg->state & MDB_PERMANENT)
Herbert Xueb1d1642010-02-27 19:41:45 +0000292 goto out;
293
294 br_multicast_del_pg(br, pg);
295
296out:
297 spin_unlock(&br->multicast_lock);
298}
299
Eric Dumazete8051682010-11-15 06:38:10 +0000300static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max,
Herbert Xueb1d1642010-02-27 19:41:45 +0000301 int elasticity)
302{
Eric Dumazete8051682010-11-15 06:38:10 +0000303 struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000304 struct net_bridge_mdb_htable *mdb;
305 int err;
306
307 mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
308 if (!mdb)
309 return -ENOMEM;
310
311 mdb->max = max;
312 mdb->old = old;
313
314 mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
315 if (!mdb->mhash) {
316 kfree(mdb);
317 return -ENOMEM;
318 }
319
320 mdb->size = old ? old->size : 0;
321 mdb->ver = old ? old->ver ^ 1 : 0;
322
323 if (!old || elasticity)
324 get_random_bytes(&mdb->secret, sizeof(mdb->secret));
325 else
326 mdb->secret = old->secret;
327
328 if (!old)
329 goto out;
330
331 err = br_mdb_copy(mdb, old, elasticity);
332 if (err) {
333 kfree(mdb->mhash);
334 kfree(mdb);
335 return err;
336 }
337
Cong Wang2ce297f2012-12-10 02:15:35 +0000338 br_mdb_rehash_seq++;
Herbert Xueb1d1642010-02-27 19:41:45 +0000339 call_rcu_bh(&mdb->rcu, br_mdb_free);
340
341out:
342 rcu_assign_pointer(*mdbp, mdb);
343
344 return 0;
345}
346
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900347static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
348 __be32 group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000349{
350 struct sk_buff *skb;
351 struct igmphdr *ih;
352 struct ethhdr *eth;
353 struct iphdr *iph;
354
355 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
356 sizeof(*ih) + 4);
357 if (!skb)
358 goto out;
359
360 skb->protocol = htons(ETH_P_IP);
361
362 skb_reset_mac_header(skb);
363 eth = eth_hdr(skb);
364
365 memcpy(eth->h_source, br->dev->dev_addr, 6);
366 eth->h_dest[0] = 1;
367 eth->h_dest[1] = 0;
368 eth->h_dest[2] = 0x5e;
369 eth->h_dest[3] = 0;
370 eth->h_dest[4] = 0;
371 eth->h_dest[5] = 1;
372 eth->h_proto = htons(ETH_P_IP);
373 skb_put(skb, sizeof(*eth));
374
375 skb_set_network_header(skb, skb->len);
376 iph = ip_hdr(skb);
377
378 iph->version = 4;
379 iph->ihl = 6;
380 iph->tos = 0xc0;
381 iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4);
382 iph->id = 0;
383 iph->frag_off = htons(IP_DF);
384 iph->ttl = 1;
385 iph->protocol = IPPROTO_IGMP;
Cong Wang1c8ad5b2013-05-21 21:52:54 +0000386 iph->saddr = br->multicast_query_use_ifaddr ?
387 inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000388 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
389 ((u8 *)&iph[1])[0] = IPOPT_RA;
390 ((u8 *)&iph[1])[1] = 4;
391 ((u8 *)&iph[1])[2] = 0;
392 ((u8 *)&iph[1])[3] = 0;
393 ip_send_check(iph);
394 skb_put(skb, 24);
395
396 skb_set_transport_header(skb, skb->len);
397 ih = igmp_hdr(skb);
398 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
399 ih->code = (group ? br->multicast_last_member_interval :
400 br->multicast_query_response_interval) /
401 (HZ / IGMP_TIMER_SCALE);
402 ih->group = group;
403 ih->csum = 0;
404 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
405 skb_put(skb, sizeof(*ih));
406
407 __skb_pull(skb, sizeof(*eth));
408
409out:
410 return skb;
411}
412
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000413#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900414static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000415 const struct in6_addr *group)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900416{
417 struct sk_buff *skb;
418 struct ipv6hdr *ip6h;
419 struct mld_msg *mldq;
420 struct ethhdr *eth;
421 u8 *hopopt;
422 unsigned long interval;
423
424 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
425 8 + sizeof(*mldq));
426 if (!skb)
427 goto out;
428
429 skb->protocol = htons(ETH_P_IPV6);
430
431 /* Ethernet header */
432 skb_reset_mac_header(skb);
433 eth = eth_hdr(skb);
434
435 memcpy(eth->h_source, br->dev->dev_addr, 6);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900436 eth->h_proto = htons(ETH_P_IPV6);
437 skb_put(skb, sizeof(*eth));
438
439 /* IPv6 header + HbH option */
440 skb_set_network_header(skb, skb->len);
441 ip6h = ipv6_hdr(skb);
442
443 *(__force __be32 *)ip6h = htonl(0x60000000);
David Stevens76d66152010-12-14 08:42:16 +0000444 ip6h->payload_len = htons(8 + sizeof(*mldq));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900445 ip6h->nexthdr = IPPROTO_HOPOPTS;
446 ip6h->hop_limit = 1;
Linus Lüssinga7bff752011-03-22 11:40:32 +0000447 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
Ulrich Weberd1d81d42012-03-05 04:52:44 +0000448 if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
449 &ip6h->saddr)) {
450 kfree_skb(skb);
451 return NULL;
452 }
Linus Lüssing36cff5a2011-02-17 08:17:51 +0000453 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900454
455 hopopt = (u8 *)(ip6h + 1);
456 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
457 hopopt[1] = 0; /* length of HbH */
458 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
459 hopopt[3] = 2; /* Length of RA Option */
460 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
461 hopopt[5] = 0;
Eldad Zack1de5a712012-05-17 06:00:25 +0000462 hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */
463 hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900464
465 skb_put(skb, sizeof(*ip6h) + 8);
466
467 /* ICMPv6 */
468 skb_set_transport_header(skb, skb->len);
469 mldq = (struct mld_msg *) icmp6_hdr(skb);
470
Linus Lüssing32de8682013-06-16 23:20:34 +0200471 interval = ipv6_addr_any(group) ?
472 br->multicast_query_response_interval :
473 br->multicast_last_member_interval;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900474
475 mldq->mld_type = ICMPV6_MGM_QUERY;
476 mldq->mld_code = 0;
477 mldq->mld_cksum = 0;
478 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
479 mldq->mld_reserved = 0;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000480 mldq->mld_mca = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900481
482 /* checksum */
483 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
484 sizeof(*mldq), IPPROTO_ICMPV6,
485 csum_partial(mldq,
486 sizeof(*mldq), 0));
487 skb_put(skb, sizeof(*mldq));
488
489 __skb_pull(skb, sizeof(*eth));
490
491out:
492 return skb;
493}
494#endif
495
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900496static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
497 struct br_ip *addr)
498{
499 switch (addr->proto) {
500 case htons(ETH_P_IP):
501 return br_ip4_multicast_alloc_query(br, addr->u.ip4);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000502#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900503 case htons(ETH_P_IPV6):
504 return br_ip6_multicast_alloc_query(br, &addr->u.ip6);
505#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900506 }
507 return NULL;
508}
509
Herbert Xueb1d1642010-02-27 19:41:45 +0000510static struct net_bridge_mdb_entry *br_multicast_get_group(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900511 struct net_bridge *br, struct net_bridge_port *port,
512 struct br_ip *group, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +0000513{
Eric Dumazete8051682010-11-15 06:38:10 +0000514 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000515 struct net_bridge_mdb_entry *mp;
Eric Dumazet95c96172012-04-15 05:58:06 +0000516 unsigned int count = 0;
517 unsigned int max;
Herbert Xueb1d1642010-02-27 19:41:45 +0000518 int elasticity;
519 int err;
520
Eric Dumazete8051682010-11-15 06:38:10 +0000521 mdb = rcu_dereference_protected(br->mdb, 1);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800522 hlist_for_each_entry(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000523 count++;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900524 if (unlikely(br_ip_equal(group, &mp->addr)))
Herbert Xueb1d1642010-02-27 19:41:45 +0000525 return mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000526 }
527
528 elasticity = 0;
529 max = mdb->max;
530
531 if (unlikely(count > br->hash_elasticity && count)) {
532 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000533 br_info(br, "Multicast hash table "
534 "chain limit reached: %s\n",
535 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000536
537 elasticity = br->hash_elasticity;
538 }
539
540 if (mdb->size >= max) {
541 max *= 2;
Thomas Graf036be6d2012-07-10 22:29:19 +0000542 if (unlikely(max > br->hash_max)) {
543 br_warn(br, "Multicast hash table maximum of %d "
544 "reached, disabling snooping: %s\n",
545 br->hash_max,
546 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000547 err = -E2BIG;
548disable:
549 br->multicast_disabled = 1;
550 goto err;
551 }
552 }
553
554 if (max > mdb->max || elasticity) {
555 if (mdb->old) {
556 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000557 br_info(br, "Multicast hash table "
558 "on fire: %s\n",
559 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000560 err = -EEXIST;
561 goto err;
562 }
563
564 err = br_mdb_rehash(&br->mdb, max, elasticity);
565 if (err) {
stephen hemminger28a16c92010-05-10 09:31:09 +0000566 br_warn(br, "Cannot rehash multicast "
567 "hash table, disabling snooping: %s, %d, %d\n",
568 port ? port->dev->name : br->dev->name,
569 mdb->size, err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000570 goto disable;
571 }
572
573 err = -EAGAIN;
574 goto err;
575 }
576
577 return NULL;
578
579err:
580 mp = ERR_PTR(err);
581 return mp;
582}
583
Cong Wangcfd56752012-12-11 22:23:08 +0000584struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
585 struct net_bridge_port *port, struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000586{
Eric Dumazete8051682010-11-15 06:38:10 +0000587 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000588 struct net_bridge_mdb_entry *mp;
589 int hash;
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000590 int err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000591
Eric Dumazete8051682010-11-15 06:38:10 +0000592 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000593 if (!mdb) {
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000594 err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0);
595 if (err)
596 return ERR_PTR(err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000597 goto rehash;
598 }
599
600 hash = br_ip_hash(mdb, group);
601 mp = br_multicast_get_group(br, port, group, hash);
602 switch (PTR_ERR(mp)) {
603 case 0:
604 break;
605
606 case -EAGAIN:
607rehash:
Eric Dumazete8051682010-11-15 06:38:10 +0000608 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000609 hash = br_ip_hash(mdb, group);
610 break;
611
612 default:
613 goto out;
614 }
615
616 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
617 if (unlikely(!mp))
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000618 return ERR_PTR(-ENOMEM);
Herbert Xueb1d1642010-02-27 19:41:45 +0000619
620 mp->br = br;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900621 mp->addr = *group;
Herbert Xueb1d1642010-02-27 19:41:45 +0000622
Eric Dumazet1faabf22013-07-19 20:07:16 -0700623 setup_timer(&mp->timer, br_multicast_group_expired,
624 (unsigned long)mp);
625
Herbert Xueb1d1642010-02-27 19:41:45 +0000626 hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
627 mdb->size++;
628
629out:
630 return mp;
631}
632
Cong Wangcfd56752012-12-11 22:23:08 +0000633struct net_bridge_port_group *br_multicast_new_port_group(
634 struct net_bridge_port *port,
635 struct br_ip *group,
Amerigo Wangccb1c312012-12-14 22:09:51 +0000636 struct net_bridge_port_group __rcu *next,
637 unsigned char state)
Cong Wangcfd56752012-12-11 22:23:08 +0000638{
639 struct net_bridge_port_group *p;
640
641 p = kzalloc(sizeof(*p), GFP_ATOMIC);
642 if (unlikely(!p))
643 return NULL;
644
645 p->addr = *group;
646 p->port = port;
Amerigo Wangccb1c312012-12-14 22:09:51 +0000647 p->state = state;
stephen hemmingereca2a432012-12-13 06:51:28 +0000648 rcu_assign_pointer(p->next, next);
Cong Wangcfd56752012-12-11 22:23:08 +0000649 hlist_add_head(&p->mglist, &port->mglist);
650 setup_timer(&p->timer, br_multicast_port_group_expired,
651 (unsigned long)p);
652 return p;
653}
654
Herbert Xueb1d1642010-02-27 19:41:45 +0000655static int br_multicast_add_group(struct net_bridge *br,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900656 struct net_bridge_port *port,
657 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000658{
659 struct net_bridge_mdb_entry *mp;
660 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +0000661 struct net_bridge_port_group __rcu **pp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000662 int err;
663
Herbert Xueb1d1642010-02-27 19:41:45 +0000664 spin_lock(&br->multicast_lock);
665 if (!netif_running(br->dev) ||
666 (port && port->state == BR_STATE_DISABLED))
667 goto out;
668
669 mp = br_multicast_new_group(br, port, group);
670 err = PTR_ERR(mp);
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000671 if (IS_ERR(mp))
Herbert Xueb1d1642010-02-27 19:41:45 +0000672 goto err;
673
674 if (!port) {
Herbert Xu8a870172011-02-12 01:05:42 -0800675 mp->mglist = true;
Herbert Xueb1d1642010-02-27 19:41:45 +0000676 goto out;
677 }
678
Eric Dumazete8051682010-11-15 06:38:10 +0000679 for (pp = &mp->ports;
680 (p = mlock_dereference(*pp, br)) != NULL;
681 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000682 if (p->port == port)
Cong Wang9f00b2e2013-05-21 21:52:55 +0000683 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +0000684 if ((unsigned long)p->port < (unsigned long)port)
685 break;
686 }
687
Amerigo Wangccb1c312012-12-14 22:09:51 +0000688 p = br_multicast_new_port_group(port, group, *pp, MDB_TEMPORARY);
Herbert Xueb1d1642010-02-27 19:41:45 +0000689 if (unlikely(!p))
690 goto err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000691 rcu_assign_pointer(*pp, p);
Cong Wang37a393b2012-12-11 22:23:07 +0000692 br_mdb_notify(br->dev, port, group, RTM_NEWMDB);
Herbert Xueb1d1642010-02-27 19:41:45 +0000693
Herbert Xueb1d1642010-02-27 19:41:45 +0000694out:
695 err = 0;
696
697err:
698 spin_unlock(&br->multicast_lock);
699 return err;
700}
701
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900702static int br_ip4_multicast_add_group(struct net_bridge *br,
703 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000704 __be32 group,
705 __u16 vid)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900706{
707 struct br_ip br_group;
708
709 if (ipv4_is_local_multicast(group))
710 return 0;
711
712 br_group.u.ip4 = group;
713 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000714 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900715
716 return br_multicast_add_group(br, port, &br_group);
717}
718
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000719#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900720static int br_ip6_multicast_add_group(struct net_bridge *br,
721 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000722 const struct in6_addr *group,
723 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900724{
725 struct br_ip br_group;
726
Linus Lüssinge4de9f92011-02-15 13:19:21 +0000727 if (!ipv6_is_transient_multicast(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900728 return 0;
729
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000730 br_group.u.ip6 = *group;
Linus Lüssing9cc6e0c2011-02-15 13:19:17 +0000731 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000732 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900733
734 return br_multicast_add_group(br, port, &br_group);
735}
736#endif
737
Herbert Xueb1d1642010-02-27 19:41:45 +0000738static void br_multicast_router_expired(unsigned long data)
739{
740 struct net_bridge_port *port = (void *)data;
741 struct net_bridge *br = port->br;
742
743 spin_lock(&br->multicast_lock);
744 if (port->multicast_router != 1 ||
745 timer_pending(&port->multicast_router_timer) ||
746 hlist_unhashed(&port->rlist))
747 goto out;
748
749 hlist_del_init_rcu(&port->rlist);
750
751out:
752 spin_unlock(&br->multicast_lock);
753}
754
755static void br_multicast_local_router_expired(unsigned long data)
756{
757}
758
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200759static void br_multicast_querier_expired(struct net_bridge *br,
760 struct bridge_mcast_query *query)
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000761{
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000762 spin_lock(&br->multicast_lock);
763 if (!netif_running(br->dev) || br->multicast_disabled)
764 goto out;
765
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200766 br_multicast_start_querier(br, query);
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000767
768out:
769 spin_unlock(&br->multicast_lock);
770}
771
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200772static void br_ip4_multicast_querier_expired(unsigned long data)
773{
774 struct net_bridge *br = (void *)data;
775
776 br_multicast_querier_expired(br, &br->ip4_query);
777}
778
779#if IS_ENABLED(CONFIG_IPV6)
780static void br_ip6_multicast_querier_expired(unsigned long data)
781{
782 struct net_bridge *br = (void *)data;
783
784 br_multicast_querier_expired(br, &br->ip6_query);
785}
786#endif
787
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900788static void __br_multicast_send_query(struct net_bridge *br,
789 struct net_bridge_port *port,
790 struct br_ip *ip)
Herbert Xueb1d1642010-02-27 19:41:45 +0000791{
Herbert Xueb1d1642010-02-27 19:41:45 +0000792 struct sk_buff *skb;
793
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900794 skb = br_multicast_alloc_query(br, ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000795 if (!skb)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900796 return;
Herbert Xueb1d1642010-02-27 19:41:45 +0000797
798 if (port) {
799 __skb_push(skb, sizeof(struct ethhdr));
800 skb->dev = port->dev;
Jan Engelhardt713aefa2010-03-23 04:07:21 +0100801 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
Herbert Xueb1d1642010-02-27 19:41:45 +0000802 dev_queue_xmit);
803 } else
804 netif_rx(skb);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900805}
Herbert Xueb1d1642010-02-27 19:41:45 +0000806
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900807static void br_multicast_send_query(struct net_bridge *br,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200808 struct net_bridge_port *port,
809 struct bridge_mcast_query *query)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900810{
811 unsigned long time;
812 struct br_ip br_group;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200813 struct bridge_mcast_querier *querier = NULL;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900814
815 if (!netif_running(br->dev) || br->multicast_disabled ||
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200816 !br->multicast_querier)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900817 return;
818
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900819 memset(&br_group.u, 0, sizeof(br_group.u));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900820
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200821 if (port ? (query == &port->ip4_query) :
822 (query == &br->ip4_query)) {
823 querier = &br->ip4_querier;
824 br_group.proto = htons(ETH_P_IP);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000825#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200826 } else {
827 querier = &br->ip6_querier;
828 br_group.proto = htons(ETH_P_IPV6);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900829#endif
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200830 }
831
832 if (!querier || timer_pending(&querier->timer))
833 return;
834
835 __br_multicast_send_query(br, port, &br_group);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900836
Herbert Xueb1d1642010-02-27 19:41:45 +0000837 time = jiffies;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200838 time += query->startup_sent < br->multicast_startup_query_count ?
Herbert Xueb1d1642010-02-27 19:41:45 +0000839 br->multicast_startup_query_interval :
840 br->multicast_query_interval;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200841 mod_timer(&query->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +0000842}
843
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200844static void br_multicast_port_query_expired(struct net_bridge_port *port,
845 struct bridge_mcast_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +0000846{
Herbert Xueb1d1642010-02-27 19:41:45 +0000847 struct net_bridge *br = port->br;
848
849 spin_lock(&br->multicast_lock);
Dan Carpenter02a780c2010-03-06 01:14:09 +0000850 if (port->state == BR_STATE_DISABLED ||
851 port->state == BR_STATE_BLOCKING)
Herbert Xueb1d1642010-02-27 19:41:45 +0000852 goto out;
853
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200854 if (query->startup_sent < br->multicast_startup_query_count)
855 query->startup_sent++;
Herbert Xueb1d1642010-02-27 19:41:45 +0000856
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200857 br_multicast_send_query(port->br, port, query);
Herbert Xueb1d1642010-02-27 19:41:45 +0000858
859out:
860 spin_unlock(&br->multicast_lock);
861}
862
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200863static void br_ip4_multicast_port_query_expired(unsigned long data)
864{
865 struct net_bridge_port *port = (void *)data;
866
867 br_multicast_port_query_expired(port, &port->ip4_query);
868}
869
870#if IS_ENABLED(CONFIG_IPV6)
871static void br_ip6_multicast_port_query_expired(unsigned long data)
872{
873 struct net_bridge_port *port = (void *)data;
874
875 br_multicast_port_query_expired(port, &port->ip6_query);
876}
877#endif
878
Herbert Xueb1d1642010-02-27 19:41:45 +0000879void br_multicast_add_port(struct net_bridge_port *port)
880{
881 port->multicast_router = 1;
882
883 setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
884 (unsigned long)port);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200885 setup_timer(&port->ip4_query.timer, br_ip4_multicast_port_query_expired,
886 (unsigned long)port);
887#if IS_ENABLED(CONFIG_IPV6)
888 setup_timer(&port->ip6_query.timer, br_ip6_multicast_port_query_expired,
889 (unsigned long)port);
890#endif
Herbert Xueb1d1642010-02-27 19:41:45 +0000891}
892
893void br_multicast_del_port(struct net_bridge_port *port)
894{
895 del_timer_sync(&port->multicast_router_timer);
896}
897
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200898static void br_multicast_enable(struct bridge_mcast_query *query)
Herbert Xu561f1102010-02-27 19:41:50 +0000899{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200900 query->startup_sent = 0;
Herbert Xu561f1102010-02-27 19:41:50 +0000901
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200902 if (try_to_del_timer_sync(&query->timer) >= 0 ||
903 del_timer(&query->timer))
904 mod_timer(&query->timer, jiffies);
Herbert Xu561f1102010-02-27 19:41:50 +0000905}
906
Herbert Xueb1d1642010-02-27 19:41:45 +0000907void br_multicast_enable_port(struct net_bridge_port *port)
908{
909 struct net_bridge *br = port->br;
910
911 spin_lock(&br->multicast_lock);
912 if (br->multicast_disabled || !netif_running(br->dev))
913 goto out;
914
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200915 br_multicast_enable(&port->ip4_query);
916#if IS_ENABLED(CONFIG_IPV6)
917 br_multicast_enable(&port->ip6_query);
918#endif
Herbert Xueb1d1642010-02-27 19:41:45 +0000919
920out:
921 spin_unlock(&br->multicast_lock);
922}
923
924void br_multicast_disable_port(struct net_bridge_port *port)
925{
926 struct net_bridge *br = port->br;
927 struct net_bridge_port_group *pg;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800928 struct hlist_node *n;
Herbert Xueb1d1642010-02-27 19:41:45 +0000929
930 spin_lock(&br->multicast_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800931 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
Herbert Xueb1d1642010-02-27 19:41:45 +0000932 br_multicast_del_pg(br, pg);
933
934 if (!hlist_unhashed(&port->rlist))
935 hlist_del_init_rcu(&port->rlist);
936 del_timer(&port->multicast_router_timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200937 del_timer(&port->ip4_query.timer);
938#if IS_ENABLED(CONFIG_IPV6)
939 del_timer(&port->ip6_query.timer);
940#endif
Herbert Xueb1d1642010-02-27 19:41:45 +0000941 spin_unlock(&br->multicast_lock);
942}
943
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900944static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
945 struct net_bridge_port *port,
946 struct sk_buff *skb)
Herbert Xueb1d1642010-02-27 19:41:45 +0000947{
948 struct igmpv3_report *ih;
949 struct igmpv3_grec *grec;
950 int i;
951 int len;
952 int num;
953 int type;
954 int err = 0;
955 __be32 group;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000956 u16 vid = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000957
958 if (!pskb_may_pull(skb, sizeof(*ih)))
959 return -EINVAL;
960
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000961 br_vlan_get_tag(skb, &vid);
Herbert Xueb1d1642010-02-27 19:41:45 +0000962 ih = igmpv3_report_hdr(skb);
963 num = ntohs(ih->ngrec);
964 len = sizeof(*ih);
965
966 for (i = 0; i < num; i++) {
967 len += sizeof(*grec);
968 if (!pskb_may_pull(skb, len))
969 return -EINVAL;
970
Herbert Xufd218cf2010-04-07 21:20:47 -0700971 grec = (void *)(skb->data + len - sizeof(*grec));
Herbert Xueb1d1642010-02-27 19:41:45 +0000972 group = grec->grec_mca;
973 type = grec->grec_type;
974
Eric Dumazet8eabf952010-04-20 03:20:05 +0000975 len += ntohs(grec->grec_nsrcs) * 4;
Herbert Xueb1d1642010-02-27 19:41:45 +0000976 if (!pskb_may_pull(skb, len))
977 return -EINVAL;
978
979 /* We treat this as an IGMPv2 report for now. */
980 switch (type) {
981 case IGMPV3_MODE_IS_INCLUDE:
982 case IGMPV3_MODE_IS_EXCLUDE:
983 case IGMPV3_CHANGE_TO_INCLUDE:
984 case IGMPV3_CHANGE_TO_EXCLUDE:
985 case IGMPV3_ALLOW_NEW_SOURCES:
986 case IGMPV3_BLOCK_OLD_SOURCES:
987 break;
988
989 default:
990 continue;
991 }
992
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000993 err = br_ip4_multicast_add_group(br, port, group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +0000994 if (err)
995 break;
996 }
997
998 return err;
999}
1000
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001001#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001002static int br_ip6_multicast_mld2_report(struct net_bridge *br,
1003 struct net_bridge_port *port,
1004 struct sk_buff *skb)
1005{
1006 struct icmp6hdr *icmp6h;
1007 struct mld2_grec *grec;
1008 int i;
1009 int len;
1010 int num;
1011 int err = 0;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001012 u16 vid = 0;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001013
1014 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
1015 return -EINVAL;
1016
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001017 br_vlan_get_tag(skb, &vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001018 icmp6h = icmp6_hdr(skb);
1019 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
1020 len = sizeof(*icmp6h);
1021
1022 for (i = 0; i < num; i++) {
1023 __be16 *nsrcs, _nsrcs;
1024
1025 nsrcs = skb_header_pointer(skb,
1026 len + offsetof(struct mld2_grec,
Linus Lüssing649e9842011-02-15 13:19:18 +00001027 grec_nsrcs),
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001028 sizeof(_nsrcs), &_nsrcs);
1029 if (!nsrcs)
1030 return -EINVAL;
1031
1032 if (!pskb_may_pull(skb,
1033 len + sizeof(*grec) +
Linus Lüssingd41db9f2011-02-15 13:19:19 +00001034 sizeof(struct in6_addr) * ntohs(*nsrcs)))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001035 return -EINVAL;
1036
1037 grec = (struct mld2_grec *)(skb->data + len);
Linus Lüssingd41db9f2011-02-15 13:19:19 +00001038 len += sizeof(*grec) +
1039 sizeof(struct in6_addr) * ntohs(*nsrcs);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001040
1041 /* We treat these as MLDv1 reports for now. */
1042 switch (grec->grec_type) {
1043 case MLD2_MODE_IS_INCLUDE:
1044 case MLD2_MODE_IS_EXCLUDE:
1045 case MLD2_CHANGE_TO_INCLUDE:
1046 case MLD2_CHANGE_TO_EXCLUDE:
1047 case MLD2_ALLOW_NEW_SOURCES:
1048 case MLD2_BLOCK_OLD_SOURCES:
1049 break;
1050
1051 default:
1052 continue;
1053 }
1054
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001055 err = br_ip6_multicast_add_group(br, port, &grec->grec_mca,
1056 vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001057 if (!err)
1058 break;
1059 }
1060
1061 return err;
1062}
1063#endif
1064
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001065static void
1066br_multicast_update_querier_timer(struct net_bridge *br,
1067 struct bridge_mcast_querier *querier,
1068 unsigned long max_delay)
Linus Lüssingb00589a2013-08-01 01:06:20 +02001069{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001070 if (!timer_pending(&querier->timer))
1071 querier->delay_time = jiffies + max_delay;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001072
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001073 mod_timer(&querier->timer, jiffies + br->multicast_querier_interval);
Linus Lüssingb00589a2013-08-01 01:06:20 +02001074}
1075
stephen hemminger7e80c122010-04-27 15:01:04 +00001076/*
Cong Wang7c776022013-06-21 15:37:25 +08001077 * Add port to router_list
stephen hemminger7e80c122010-04-27 15:01:04 +00001078 * list is maintained ordered by pointer value
1079 * and locked by br->multicast_lock and RCU
1080 */
Herbert Xu0909e112010-02-27 19:41:49 +00001081static void br_multicast_add_router(struct net_bridge *br,
1082 struct net_bridge_port *port)
1083{
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001084 struct net_bridge_port *p;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001085 struct hlist_node *slot = NULL;
Herbert Xu0909e112010-02-27 19:41:49 +00001086
Sasha Levinb67bfe02013-02-27 17:06:00 -08001087 hlist_for_each_entry(p, &br->router_list, rlist) {
stephen hemminger7e80c122010-04-27 15:01:04 +00001088 if ((unsigned long) port >= (unsigned long) p)
1089 break;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001090 slot = &p->rlist;
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001091 }
Herbert Xu0909e112010-02-27 19:41:49 +00001092
stephen hemminger7e80c122010-04-27 15:01:04 +00001093 if (slot)
1094 hlist_add_after_rcu(slot, &port->rlist);
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001095 else
1096 hlist_add_head_rcu(&port->rlist, &br->router_list);
Herbert Xu0909e112010-02-27 19:41:49 +00001097}
1098
Herbert Xueb1d1642010-02-27 19:41:45 +00001099static void br_multicast_mark_router(struct net_bridge *br,
1100 struct net_bridge_port *port)
1101{
1102 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +00001103
1104 if (!port) {
1105 if (br->multicast_router == 1)
1106 mod_timer(&br->multicast_router_timer,
1107 now + br->multicast_querier_interval);
1108 return;
1109 }
1110
1111 if (port->multicast_router != 1)
1112 return;
1113
1114 if (!hlist_unhashed(&port->rlist))
1115 goto timer;
1116
Herbert Xu0909e112010-02-27 19:41:49 +00001117 br_multicast_add_router(br, port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001118
1119timer:
1120 mod_timer(&port->multicast_router_timer,
1121 now + br->multicast_querier_interval);
1122}
1123
1124static void br_multicast_query_received(struct net_bridge *br,
1125 struct net_bridge_port *port,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001126 struct bridge_mcast_querier *querier,
Linus Lüssingb00589a2013-08-01 01:06:20 +02001127 int saddr,
1128 unsigned long max_delay)
Herbert Xueb1d1642010-02-27 19:41:45 +00001129{
1130 if (saddr)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001131 br_multicast_update_querier_timer(br, querier, max_delay);
1132 else if (timer_pending(&querier->timer))
Herbert Xueb1d1642010-02-27 19:41:45 +00001133 return;
1134
1135 br_multicast_mark_router(br, port);
1136}
1137
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001138static int br_ip4_multicast_query(struct net_bridge *br,
1139 struct net_bridge_port *port,
1140 struct sk_buff *skb)
Herbert Xueb1d1642010-02-27 19:41:45 +00001141{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001142 const struct iphdr *iph = ip_hdr(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001143 struct igmphdr *ih = igmp_hdr(skb);
1144 struct net_bridge_mdb_entry *mp;
1145 struct igmpv3_query *ih3;
1146 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +00001147 struct net_bridge_port_group __rcu **pp;
Herbert Xueb1d1642010-02-27 19:41:45 +00001148 unsigned long max_delay;
1149 unsigned long now = jiffies;
1150 __be32 group;
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001151 int err = 0;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001152 u16 vid = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001153
1154 spin_lock(&br->multicast_lock);
1155 if (!netif_running(br->dev) ||
1156 (port && port->state == BR_STATE_DISABLED))
1157 goto out;
1158
Herbert Xueb1d1642010-02-27 19:41:45 +00001159 group = ih->group;
1160
1161 if (skb->len == sizeof(*ih)) {
1162 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1163
1164 if (!max_delay) {
1165 max_delay = 10 * HZ;
1166 group = 0;
1167 }
1168 } else {
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001169 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) {
1170 err = -EINVAL;
1171 goto out;
1172 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001173
1174 ih3 = igmpv3_query_hdr(skb);
1175 if (ih3->nsrcs)
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001176 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001177
YOSHIFUJI Hideaki / 吉藤英明0ba8c9e2010-03-15 19:27:00 +00001178 max_delay = ih3->code ?
1179 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
Herbert Xueb1d1642010-02-27 19:41:45 +00001180 }
1181
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001182 br_multicast_query_received(br, port, &br->ip4_querier, !!iph->saddr,
1183 max_delay);
Linus Lüssingb00589a2013-08-01 01:06:20 +02001184
Herbert Xueb1d1642010-02-27 19:41:45 +00001185 if (!group)
1186 goto out;
1187
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001188 br_vlan_get_tag(skb, &vid);
1189 mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001190 if (!mp)
1191 goto out;
1192
Cong Wang9f00b2e2013-05-21 21:52:55 +00001193 mod_timer(&mp->timer, now + br->multicast_membership_interval);
1194 mp->timer_armed = true;
1195
Herbert Xueb1d1642010-02-27 19:41:45 +00001196 max_delay *= br->multicast_last_member_count;
1197
Herbert Xu8a870172011-02-12 01:05:42 -08001198 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001199 (timer_pending(&mp->timer) ?
1200 time_after(mp->timer.expires, now + max_delay) :
1201 try_to_del_timer_sync(&mp->timer) >= 0))
1202 mod_timer(&mp->timer, now + max_delay);
1203
Eric Dumazete8051682010-11-15 06:38:10 +00001204 for (pp = &mp->ports;
1205 (p = mlock_dereference(*pp, br)) != NULL;
1206 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001207 if (timer_pending(&p->timer) ?
1208 time_after(p->timer.expires, now + max_delay) :
1209 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001210 mod_timer(&p->timer, now + max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001211 }
1212
1213out:
1214 spin_unlock(&br->multicast_lock);
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001215 return err;
Herbert Xueb1d1642010-02-27 19:41:45 +00001216}
1217
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001218#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001219static int br_ip6_multicast_query(struct net_bridge *br,
1220 struct net_bridge_port *port,
1221 struct sk_buff *skb)
1222{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001223 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
stephen hemmingereca2a432012-12-13 06:51:28 +00001224 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001225 struct net_bridge_mdb_entry *mp;
1226 struct mld2_query *mld2q;
Eric Dumazete8051682010-11-15 06:38:10 +00001227 struct net_bridge_port_group *p;
1228 struct net_bridge_port_group __rcu **pp;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001229 unsigned long max_delay;
1230 unsigned long now = jiffies;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001231 const struct in6_addr *group = NULL;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001232 int err = 0;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001233 u16 vid = 0;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001234
1235 spin_lock(&br->multicast_lock);
1236 if (!netif_running(br->dev) ||
1237 (port && port->state == BR_STATE_DISABLED))
1238 goto out;
1239
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001240 if (skb->len == sizeof(*mld)) {
1241 if (!pskb_may_pull(skb, sizeof(*mld))) {
1242 err = -EINVAL;
1243 goto out;
1244 }
1245 mld = (struct mld_msg *) icmp6_hdr(skb);
Li RongQing47152132012-07-09 23:56:12 +00001246 max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001247 if (max_delay)
1248 group = &mld->mld_mca;
Linus Lüssing248ba8e2013-08-06 00:32:05 +02001249 } else {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001250 if (!pskb_may_pull(skb, sizeof(*mld2q))) {
1251 err = -EINVAL;
1252 goto out;
1253 }
1254 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1255 if (!mld2q->mld2q_nsrcs)
1256 group = &mld2q->mld2q_mca;
Ang Way Chuang8fa45a72012-12-13 23:08:39 +00001257 max_delay = mld2q->mld2q_mrc ? MLDV2_MRC(ntohs(mld2q->mld2q_mrc)) : 1;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001258 }
1259
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001260 br_multicast_query_received(br, port, &br->ip6_querier,
1261 !ipv6_addr_any(&ip6h->saddr), max_delay);
Linus Lüssingb00589a2013-08-01 01:06:20 +02001262
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001263 if (!group)
1264 goto out;
1265
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001266 br_vlan_get_tag(skb, &vid);
1267 mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001268 if (!mp)
1269 goto out;
1270
Cong Wang9f00b2e2013-05-21 21:52:55 +00001271 mod_timer(&mp->timer, now + br->multicast_membership_interval);
1272 mp->timer_armed = true;
1273
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001274 max_delay *= br->multicast_last_member_count;
Herbert Xu8a870172011-02-12 01:05:42 -08001275 if (mp->mglist &&
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001276 (timer_pending(&mp->timer) ?
1277 time_after(mp->timer.expires, now + max_delay) :
1278 try_to_del_timer_sync(&mp->timer) >= 0))
1279 mod_timer(&mp->timer, now + max_delay);
1280
Eric Dumazete8051682010-11-15 06:38:10 +00001281 for (pp = &mp->ports;
1282 (p = mlock_dereference(*pp, br)) != NULL;
1283 pp = &p->next) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001284 if (timer_pending(&p->timer) ?
1285 time_after(p->timer.expires, now + max_delay) :
1286 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001287 mod_timer(&p->timer, now + max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001288 }
1289
1290out:
1291 spin_unlock(&br->multicast_lock);
1292 return err;
1293}
1294#endif
1295
Herbert Xueb1d1642010-02-27 19:41:45 +00001296static void br_multicast_leave_group(struct net_bridge *br,
1297 struct net_bridge_port *port,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001298 struct br_ip *group,
1299 struct bridge_mcast_querier *querier,
1300 struct bridge_mcast_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +00001301{
1302 struct net_bridge_mdb_htable *mdb;
1303 struct net_bridge_mdb_entry *mp;
1304 struct net_bridge_port_group *p;
1305 unsigned long now;
1306 unsigned long time;
1307
Herbert Xueb1d1642010-02-27 19:41:45 +00001308 spin_lock(&br->multicast_lock);
1309 if (!netif_running(br->dev) ||
1310 (port && port->state == BR_STATE_DISABLED) ||
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001311 timer_pending(&querier->timer))
Herbert Xueb1d1642010-02-27 19:41:45 +00001312 goto out;
1313
Eric Dumazete8051682010-11-15 06:38:10 +00001314 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001315 mp = br_mdb_ip_get(mdb, group);
1316 if (!mp)
1317 goto out;
1318
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001319 if (br->multicast_querier) {
Cong Wang6b7df112013-05-21 21:52:56 +00001320 __br_multicast_send_query(br, port, &mp->addr);
1321
1322 time = jiffies + br->multicast_last_member_count *
1323 br->multicast_last_member_interval;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001324
1325 mod_timer(&query->timer, time);
Cong Wang6b7df112013-05-21 21:52:56 +00001326
1327 for (p = mlock_dereference(mp->ports, br);
1328 p != NULL;
1329 p = mlock_dereference(p->next, br)) {
1330 if (p->port != port)
1331 continue;
1332
1333 if (!hlist_unhashed(&p->mglist) &&
1334 (timer_pending(&p->timer) ?
1335 time_after(p->timer.expires, time) :
1336 try_to_del_timer_sync(&p->timer) >= 0)) {
1337 mod_timer(&p->timer, time);
1338 }
1339
1340 break;
1341 }
1342 }
1343
David S. Millerc2d3bab2012-12-05 16:24:45 -05001344 if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
Amerigo Wang50426b52012-12-03 23:56:40 +00001345 struct net_bridge_port_group __rcu **pp;
1346
1347 for (pp = &mp->ports;
1348 (p = mlock_dereference(*pp, br)) != NULL;
1349 pp = &p->next) {
1350 if (p->port != port)
1351 continue;
1352
1353 rcu_assign_pointer(*pp, p->next);
1354 hlist_del_init(&p->mglist);
1355 del_timer(&p->timer);
1356 call_rcu_bh(&p->rcu, br_multicast_free_pg);
Cong Wang37a393b2012-12-11 22:23:07 +00001357 br_mdb_notify(br->dev, port, group, RTM_DELMDB);
Amerigo Wang50426b52012-12-03 23:56:40 +00001358
Cong Wang9f00b2e2013-05-21 21:52:55 +00001359 if (!mp->ports && !mp->mglist && mp->timer_armed &&
Amerigo Wang50426b52012-12-03 23:56:40 +00001360 netif_running(br->dev))
1361 mod_timer(&mp->timer, jiffies);
1362 }
1363 goto out;
1364 }
1365
Herbert Xueb1d1642010-02-27 19:41:45 +00001366 now = jiffies;
1367 time = now + br->multicast_last_member_count *
1368 br->multicast_last_member_interval;
1369
1370 if (!port) {
Cong Wang9f00b2e2013-05-21 21:52:55 +00001371 if (mp->mglist && mp->timer_armed &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001372 (timer_pending(&mp->timer) ?
1373 time_after(mp->timer.expires, time) :
1374 try_to_del_timer_sync(&mp->timer) >= 0)) {
1375 mod_timer(&mp->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +00001376 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001377 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001378out:
1379 spin_unlock(&br->multicast_lock);
1380}
1381
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001382static void br_ip4_multicast_leave_group(struct net_bridge *br,
1383 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001384 __be32 group,
1385 __u16 vid)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001386{
1387 struct br_ip br_group;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001388 struct bridge_mcast_query *query = port ? &port->ip4_query :
1389 &br->ip4_query;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001390
1391 if (ipv4_is_local_multicast(group))
1392 return;
1393
1394 br_group.u.ip4 = group;
1395 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001396 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001397
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001398 br_multicast_leave_group(br, port, &br_group, &br->ip4_querier, query);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001399}
1400
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001401#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001402static void br_ip6_multicast_leave_group(struct net_bridge *br,
1403 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001404 const struct in6_addr *group,
1405 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001406{
1407 struct br_ip br_group;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001408 struct bridge_mcast_query *query = port ? &port->ip6_query :
1409 &br->ip6_query;
1410
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001411
Linus Lüssinge4de9f92011-02-15 13:19:21 +00001412 if (!ipv6_is_transient_multicast(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001413 return;
1414
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001415 br_group.u.ip6 = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001416 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001417 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001418
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001419 br_multicast_leave_group(br, port, &br_group, &br->ip6_querier, query);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001420}
1421#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001422
Herbert Xueb1d1642010-02-27 19:41:45 +00001423static int br_multicast_ipv4_rcv(struct net_bridge *br,
1424 struct net_bridge_port *port,
1425 struct sk_buff *skb)
1426{
1427 struct sk_buff *skb2 = skb;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001428 const struct iphdr *iph;
Herbert Xueb1d1642010-02-27 19:41:45 +00001429 struct igmphdr *ih;
Eric Dumazet95c96172012-04-15 05:58:06 +00001430 unsigned int len;
1431 unsigned int offset;
Herbert Xueb1d1642010-02-27 19:41:45 +00001432 int err;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001433 u16 vid = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001434
Herbert Xueb1d1642010-02-27 19:41:45 +00001435 /* We treat OOM as packet loss for now. */
1436 if (!pskb_may_pull(skb, sizeof(*iph)))
1437 return -EINVAL;
1438
1439 iph = ip_hdr(skb);
1440
1441 if (iph->ihl < 5 || iph->version != 4)
1442 return -EINVAL;
1443
1444 if (!pskb_may_pull(skb, ip_hdrlen(skb)))
1445 return -EINVAL;
1446
1447 iph = ip_hdr(skb);
1448
1449 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1450 return -EINVAL;
1451
Herbert Xubd4265f2011-06-23 02:39:12 +00001452 if (iph->protocol != IPPROTO_IGMP) {
Amerigo Wangbf5e4dd2013-03-07 02:32:26 +00001453 if (!ipv4_is_local_multicast(iph->daddr))
Herbert Xubd4265f2011-06-23 02:39:12 +00001454 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Herbert Xueb1d1642010-02-27 19:41:45 +00001455 return 0;
Herbert Xubd4265f2011-06-23 02:39:12 +00001456 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001457
1458 len = ntohs(iph->tot_len);
1459 if (skb->len < len || len < ip_hdrlen(skb))
1460 return -EINVAL;
1461
1462 if (skb->len > len) {
1463 skb2 = skb_clone(skb, GFP_ATOMIC);
1464 if (!skb2)
1465 return -ENOMEM;
1466
1467 err = pskb_trim_rcsum(skb2, len);
1468 if (err)
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001469 goto err_out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001470 }
1471
1472 len -= ip_hdrlen(skb2);
1473 offset = skb_network_offset(skb2) + ip_hdrlen(skb2);
1474 __skb_pull(skb2, offset);
1475 skb_reset_transport_header(skb2);
1476
1477 err = -EINVAL;
1478 if (!pskb_may_pull(skb2, sizeof(*ih)))
1479 goto out;
1480
Herbert Xueb1d1642010-02-27 19:41:45 +00001481 switch (skb2->ip_summed) {
1482 case CHECKSUM_COMPLETE:
1483 if (!csum_fold(skb2->csum))
1484 break;
1485 /* fall through */
1486 case CHECKSUM_NONE:
1487 skb2->csum = 0;
1488 if (skb_checksum_complete(skb2))
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001489 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001490 }
1491
1492 err = 0;
1493
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001494 br_vlan_get_tag(skb2, &vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001495 BR_INPUT_SKB_CB(skb)->igmp = 1;
1496 ih = igmp_hdr(skb2);
1497
1498 switch (ih->type) {
1499 case IGMP_HOST_MEMBERSHIP_REPORT:
1500 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Fernando Luis Vázquez Cao62b2bcb2011-06-13 15:04:43 +00001501 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001502 err = br_ip4_multicast_add_group(br, port, ih->group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001503 break;
1504 case IGMPV3_HOST_MEMBERSHIP_REPORT:
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001505 err = br_ip4_multicast_igmp3_report(br, port, skb2);
Herbert Xueb1d1642010-02-27 19:41:45 +00001506 break;
1507 case IGMP_HOST_MEMBERSHIP_QUERY:
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001508 err = br_ip4_multicast_query(br, port, skb2);
Herbert Xueb1d1642010-02-27 19:41:45 +00001509 break;
1510 case IGMP_HOST_LEAVE_MESSAGE:
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001511 br_ip4_multicast_leave_group(br, port, ih->group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001512 break;
1513 }
1514
1515out:
1516 __skb_push(skb2, offset);
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001517err_out:
Herbert Xueb1d1642010-02-27 19:41:45 +00001518 if (skb2 != skb)
1519 kfree_skb(skb2);
1520 return err;
1521}
1522
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001523#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001524static int br_multicast_ipv6_rcv(struct net_bridge *br,
1525 struct net_bridge_port *port,
1526 struct sk_buff *skb)
1527{
Tomas Winkler9d890812011-01-03 11:26:08 -08001528 struct sk_buff *skb2;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001529 const struct ipv6hdr *ip6h;
Eric Dumazet22df1332011-08-23 19:57:05 +00001530 u8 icmp6_type;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001531 u8 nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -08001532 __be16 frag_off;
Eric Dumazet95c96172012-04-15 05:58:06 +00001533 unsigned int len;
Kulikov Vasiliybb7a0bd2010-07-15 08:47:33 +00001534 int offset;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001535 int err;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001536 u16 vid = 0;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001537
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001538 if (!pskb_may_pull(skb, sizeof(*ip6h)))
1539 return -EINVAL;
1540
1541 ip6h = ipv6_hdr(skb);
1542
1543 /*
1544 * We're interested in MLD messages only.
1545 * - Version is 6
1546 * - MLD has always Router Alert hop-by-hop option
1547 * - But we do not support jumbrograms.
1548 */
1549 if (ip6h->version != 6 ||
1550 ip6h->nexthdr != IPPROTO_HOPOPTS ||
1551 ip6h->payload_len == 0)
1552 return 0;
1553
Linus Lüssingff9a57a2011-03-26 20:27:24 +00001554 len = ntohs(ip6h->payload_len) + sizeof(*ip6h);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001555 if (skb->len < len)
1556 return -EINVAL;
1557
1558 nexthdr = ip6h->nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -08001559 offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr, &frag_off);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001560
1561 if (offset < 0 || nexthdr != IPPROTO_ICMPV6)
1562 return 0;
1563
1564 /* Okay, we found ICMPv6 header */
1565 skb2 = skb_clone(skb, GFP_ATOMIC);
1566 if (!skb2)
1567 return -ENOMEM;
1568
Tomas Winkler9d890812011-01-03 11:26:08 -08001569 err = -EINVAL;
1570 if (!pskb_may_pull(skb2, offset + sizeof(struct icmp6hdr)))
1571 goto out;
1572
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001573 len -= offset - skb_network_offset(skb2);
1574
1575 __skb_pull(skb2, offset);
1576 skb_reset_transport_header(skb2);
stephen hemmingerfa2da8c2011-11-15 08:09:14 +00001577 skb_postpull_rcsum(skb2, skb_network_header(skb2),
1578 skb_network_header_len(skb2));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001579
Eric Dumazet22df1332011-08-23 19:57:05 +00001580 icmp6_type = icmp6_hdr(skb2)->icmp6_type;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001581
Eric Dumazet22df1332011-08-23 19:57:05 +00001582 switch (icmp6_type) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001583 case ICMPV6_MGM_QUERY:
1584 case ICMPV6_MGM_REPORT:
1585 case ICMPV6_MGM_REDUCTION:
1586 case ICMPV6_MLD2_REPORT:
1587 break;
1588 default:
1589 err = 0;
1590 goto out;
1591 }
1592
1593 /* Okay, we found MLD message. Check further. */
1594 if (skb2->len > len) {
1595 err = pskb_trim_rcsum(skb2, len);
1596 if (err)
1597 goto out;
Yan, Zheng4b275d72011-08-23 22:54:33 +00001598 err = -EINVAL;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001599 }
1600
Yan, Zheng4b275d72011-08-23 22:54:33 +00001601 ip6h = ipv6_hdr(skb2);
1602
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001603 switch (skb2->ip_summed) {
1604 case CHECKSUM_COMPLETE:
Yan, Zheng4b275d72011-08-23 22:54:33 +00001605 if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, skb2->len,
1606 IPPROTO_ICMPV6, skb2->csum))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001607 break;
1608 /*FALLTHROUGH*/
1609 case CHECKSUM_NONE:
Yan, Zheng4b275d72011-08-23 22:54:33 +00001610 skb2->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr,
1611 &ip6h->daddr,
1612 skb2->len,
1613 IPPROTO_ICMPV6, 0));
1614 if (__skb_checksum_complete(skb2))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001615 goto out;
1616 }
1617
1618 err = 0;
1619
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001620 br_vlan_get_tag(skb, &vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001621 BR_INPUT_SKB_CB(skb)->igmp = 1;
1622
Eric Dumazet22df1332011-08-23 19:57:05 +00001623 switch (icmp6_type) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001624 case ICMPV6_MGM_REPORT:
1625 {
Tomas Winkler9d890812011-01-03 11:26:08 -08001626 struct mld_msg *mld;
1627 if (!pskb_may_pull(skb2, sizeof(*mld))) {
1628 err = -EINVAL;
1629 goto out;
1630 }
1631 mld = (struct mld_msg *)skb_transport_header(skb2);
Fernando Luis Vázquez Caofc2af6c2011-06-13 15:06:58 +00001632 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001633 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001634 break;
1635 }
1636 case ICMPV6_MLD2_REPORT:
1637 err = br_ip6_multicast_mld2_report(br, port, skb2);
1638 break;
1639 case ICMPV6_MGM_QUERY:
1640 err = br_ip6_multicast_query(br, port, skb2);
1641 break;
1642 case ICMPV6_MGM_REDUCTION:
1643 {
Tomas Winkler9d890812011-01-03 11:26:08 -08001644 struct mld_msg *mld;
1645 if (!pskb_may_pull(skb2, sizeof(*mld))) {
1646 err = -EINVAL;
1647 goto out;
1648 }
1649 mld = (struct mld_msg *)skb_transport_header(skb2);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001650 br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001651 }
1652 }
1653
1654out:
Tomas Winkler9d890812011-01-03 11:26:08 -08001655 kfree_skb(skb2);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001656 return err;
1657}
1658#endif
1659
Herbert Xueb1d1642010-02-27 19:41:45 +00001660int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1661 struct sk_buff *skb)
1662{
YOSHIFUJI Hideaki / 吉藤英明1fafc7a2010-04-25 08:06:40 +00001663 BR_INPUT_SKB_CB(skb)->igmp = 0;
1664 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1665
Herbert Xueb1d1642010-02-27 19:41:45 +00001666 if (br->multicast_disabled)
1667 return 0;
1668
1669 switch (skb->protocol) {
1670 case htons(ETH_P_IP):
1671 return br_multicast_ipv4_rcv(br, port, skb);
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001672#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001673 case htons(ETH_P_IPV6):
1674 return br_multicast_ipv6_rcv(br, port, skb);
1675#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001676 }
1677
1678 return 0;
1679}
1680
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001681static void br_multicast_query_expired(struct net_bridge *br,
1682 struct bridge_mcast_query *query)
1683{
1684 spin_lock(&br->multicast_lock);
1685 if (query->startup_sent < br->multicast_startup_query_count)
1686 query->startup_sent++;
1687
1688 br_multicast_send_query(br, NULL, query);
1689 spin_unlock(&br->multicast_lock);
1690}
1691
1692static void br_ip4_multicast_query_expired(unsigned long data)
Herbert Xueb1d1642010-02-27 19:41:45 +00001693{
1694 struct net_bridge *br = (void *)data;
1695
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001696 br_multicast_query_expired(br, &br->ip4_query);
Herbert Xueb1d1642010-02-27 19:41:45 +00001697}
1698
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001699#if IS_ENABLED(CONFIG_IPV6)
1700static void br_ip6_multicast_query_expired(unsigned long data)
1701{
1702 struct net_bridge *br = (void *)data;
1703
1704 br_multicast_query_expired(br, &br->ip6_query);
1705}
1706#endif
1707
Herbert Xueb1d1642010-02-27 19:41:45 +00001708void br_multicast_init(struct net_bridge *br)
1709{
1710 br->hash_elasticity = 4;
1711 br->hash_max = 512;
1712
1713 br->multicast_router = 1;
Herbert Xuc5c23262012-04-13 02:37:42 +00001714 br->multicast_querier = 0;
Cong Wang1c8ad5b2013-05-21 21:52:54 +00001715 br->multicast_query_use_ifaddr = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001716 br->multicast_last_member_count = 2;
1717 br->multicast_startup_query_count = 2;
1718
1719 br->multicast_last_member_interval = HZ;
1720 br->multicast_query_response_interval = 10 * HZ;
1721 br->multicast_startup_query_interval = 125 * HZ / 4;
1722 br->multicast_query_interval = 125 * HZ;
1723 br->multicast_querier_interval = 255 * HZ;
1724 br->multicast_membership_interval = 260 * HZ;
1725
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001726 br->ip4_querier.delay_time = 0;
1727#if IS_ENABLED(CONFIG_IPV6)
1728 br->ip6_querier.delay_time = 0;
1729#endif
Linus Lüssingb00589a2013-08-01 01:06:20 +02001730
Herbert Xueb1d1642010-02-27 19:41:45 +00001731 spin_lock_init(&br->multicast_lock);
1732 setup_timer(&br->multicast_router_timer,
1733 br_multicast_local_router_expired, 0);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001734 setup_timer(&br->ip4_querier.timer, br_ip4_multicast_querier_expired,
Herbert Xueb1d1642010-02-27 19:41:45 +00001735 (unsigned long)br);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001736 setup_timer(&br->ip4_query.timer, br_ip4_multicast_query_expired,
1737 (unsigned long)br);
1738#if IS_ENABLED(CONFIG_IPV6)
1739 setup_timer(&br->ip6_querier.timer, br_ip6_multicast_querier_expired,
1740 (unsigned long)br);
1741 setup_timer(&br->ip6_query.timer, br_ip6_multicast_query_expired,
1742 (unsigned long)br);
1743#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001744}
1745
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001746static void __br_multicast_open(struct net_bridge *br,
1747 struct bridge_mcast_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +00001748{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001749 query->startup_sent = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001750
1751 if (br->multicast_disabled)
1752 return;
1753
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001754 mod_timer(&query->timer, jiffies);
1755}
1756
1757void br_multicast_open(struct net_bridge *br)
1758{
1759 __br_multicast_open(br, &br->ip4_query);
1760#if IS_ENABLED(CONFIG_IPV6)
1761 __br_multicast_open(br, &br->ip6_query);
1762#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001763}
1764
1765void br_multicast_stop(struct net_bridge *br)
1766{
1767 struct net_bridge_mdb_htable *mdb;
1768 struct net_bridge_mdb_entry *mp;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001769 struct hlist_node *n;
Herbert Xueb1d1642010-02-27 19:41:45 +00001770 u32 ver;
1771 int i;
1772
1773 del_timer_sync(&br->multicast_router_timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001774 del_timer_sync(&br->ip4_querier.timer);
1775 del_timer_sync(&br->ip4_query.timer);
1776#if IS_ENABLED(CONFIG_IPV6)
1777 del_timer_sync(&br->ip6_querier.timer);
1778 del_timer_sync(&br->ip6_query.timer);
1779#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001780
1781 spin_lock_bh(&br->multicast_lock);
Eric Dumazete8051682010-11-15 06:38:10 +00001782 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001783 if (!mdb)
1784 goto out;
1785
1786 br->mdb = NULL;
1787
1788 ver = mdb->ver;
1789 for (i = 0; i < mdb->max; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001790 hlist_for_each_entry_safe(mp, n, &mdb->mhash[i],
Herbert Xueb1d1642010-02-27 19:41:45 +00001791 hlist[ver]) {
1792 del_timer(&mp->timer);
Cong Wang9f00b2e2013-05-21 21:52:55 +00001793 mp->timer_armed = false;
Herbert Xueb1d1642010-02-27 19:41:45 +00001794 call_rcu_bh(&mp->rcu, br_multicast_free_group);
1795 }
1796 }
1797
1798 if (mdb->old) {
1799 spin_unlock_bh(&br->multicast_lock);
Herbert Xu10cc2b52010-03-05 21:03:35 +00001800 rcu_barrier_bh();
Herbert Xueb1d1642010-02-27 19:41:45 +00001801 spin_lock_bh(&br->multicast_lock);
1802 WARN_ON(mdb->old);
1803 }
1804
1805 mdb->old = mdb;
1806 call_rcu_bh(&mdb->rcu, br_mdb_free);
1807
1808out:
1809 spin_unlock_bh(&br->multicast_lock);
1810}
Herbert Xu0909e112010-02-27 19:41:49 +00001811
1812int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1813{
1814 int err = -ENOENT;
1815
1816 spin_lock_bh(&br->multicast_lock);
1817 if (!netif_running(br->dev))
1818 goto unlock;
1819
1820 switch (val) {
1821 case 0:
1822 case 2:
1823 del_timer(&br->multicast_router_timer);
1824 /* fall through */
1825 case 1:
1826 br->multicast_router = val;
1827 err = 0;
1828 break;
1829
1830 default:
1831 err = -EINVAL;
1832 break;
1833 }
1834
1835unlock:
1836 spin_unlock_bh(&br->multicast_lock);
1837
1838 return err;
1839}
1840
1841int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1842{
1843 struct net_bridge *br = p->br;
1844 int err = -ENOENT;
1845
1846 spin_lock(&br->multicast_lock);
1847 if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED)
1848 goto unlock;
1849
1850 switch (val) {
1851 case 0:
1852 case 1:
1853 case 2:
1854 p->multicast_router = val;
1855 err = 0;
1856
1857 if (val < 2 && !hlist_unhashed(&p->rlist))
1858 hlist_del_init_rcu(&p->rlist);
1859
1860 if (val == 1)
1861 break;
1862
1863 del_timer(&p->multicast_router_timer);
1864
1865 if (val == 0)
1866 break;
1867
1868 br_multicast_add_router(br, p);
1869 break;
1870
1871 default:
1872 err = -EINVAL;
1873 break;
1874 }
1875
1876unlock:
1877 spin_unlock(&br->multicast_lock);
1878
1879 return err;
1880}
Herbert Xu561f1102010-02-27 19:41:50 +00001881
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001882static void br_multicast_start_querier(struct net_bridge *br,
1883 struct bridge_mcast_query *query)
Herbert Xu561f1102010-02-27 19:41:50 +00001884{
1885 struct net_bridge_port *port;
Herbert Xu74857212012-04-13 02:37:42 +00001886
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001887 __br_multicast_open(br, query);
Herbert Xu74857212012-04-13 02:37:42 +00001888
1889 list_for_each_entry(port, &br->port_list, list) {
1890 if (port->state == BR_STATE_DISABLED ||
1891 port->state == BR_STATE_BLOCKING)
1892 continue;
1893
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001894 if (query == &br->ip4_query)
1895 br_multicast_enable(&port->ip4_query);
1896#if IS_ENABLED(CONFIG_IPV6)
1897 else
1898 br_multicast_enable(&port->ip6_query);
1899#endif
Herbert Xu74857212012-04-13 02:37:42 +00001900 }
1901}
1902
1903int br_multicast_toggle(struct net_bridge *br, unsigned long val)
1904{
Herbert Xu3a7fda02010-07-29 00:45:30 +00001905 int err = 0;
Eric Dumazete8051682010-11-15 06:38:10 +00001906 struct net_bridge_mdb_htable *mdb;
Herbert Xu561f1102010-02-27 19:41:50 +00001907
Andrey Vaginef5e0d82011-11-10 05:48:03 +00001908 spin_lock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00001909 if (br->multicast_disabled == !val)
1910 goto unlock;
1911
1912 br->multicast_disabled = !val;
1913 if (br->multicast_disabled)
1914 goto unlock;
1915
Herbert Xu3a7fda02010-07-29 00:45:30 +00001916 if (!netif_running(br->dev))
1917 goto unlock;
1918
Eric Dumazete8051682010-11-15 06:38:10 +00001919 mdb = mlock_dereference(br->mdb, br);
1920 if (mdb) {
1921 if (mdb->old) {
Herbert Xu561f1102010-02-27 19:41:50 +00001922 err = -EEXIST;
1923rollback:
1924 br->multicast_disabled = !!val;
1925 goto unlock;
1926 }
1927
Eric Dumazete8051682010-11-15 06:38:10 +00001928 err = br_mdb_rehash(&br->mdb, mdb->max,
Herbert Xu561f1102010-02-27 19:41:50 +00001929 br->hash_elasticity);
1930 if (err)
1931 goto rollback;
1932 }
1933
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001934 br_multicast_start_querier(br, &br->ip4_query);
1935#if IS_ENABLED(CONFIG_IPV6)
1936 br_multicast_start_querier(br, &br->ip6_query);
1937#endif
Herbert Xu561f1102010-02-27 19:41:50 +00001938
1939unlock:
Andrey Vaginef5e0d82011-11-10 05:48:03 +00001940 spin_unlock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00001941
1942 return err;
1943}
Herbert Xub1951672010-02-27 19:41:51 +00001944
Herbert Xuc5c23262012-04-13 02:37:42 +00001945int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
1946{
Linus Lüssingb00589a2013-08-01 01:06:20 +02001947 unsigned long max_delay;
1948
Herbert Xuc5c23262012-04-13 02:37:42 +00001949 val = !!val;
1950
1951 spin_lock_bh(&br->multicast_lock);
1952 if (br->multicast_querier == val)
1953 goto unlock;
1954
1955 br->multicast_querier = val;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001956 if (!val)
1957 goto unlock;
1958
1959 max_delay = br->multicast_query_response_interval;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001960
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001961 if (!timer_pending(&br->ip4_querier.timer))
1962 br->ip4_querier.delay_time = jiffies + max_delay;
1963
1964 br_multicast_start_querier(br, &br->ip4_query);
1965
1966#if IS_ENABLED(CONFIG_IPV6)
1967 if (!timer_pending(&br->ip6_querier.timer))
1968 br->ip6_querier.delay_time = jiffies + max_delay;
1969
1970 br_multicast_start_querier(br, &br->ip6_query);
1971#endif
Herbert Xuc5c23262012-04-13 02:37:42 +00001972
1973unlock:
1974 spin_unlock_bh(&br->multicast_lock);
1975
1976 return 0;
1977}
1978
Herbert Xub1951672010-02-27 19:41:51 +00001979int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
1980{
1981 int err = -ENOENT;
1982 u32 old;
Eric Dumazete8051682010-11-15 06:38:10 +00001983 struct net_bridge_mdb_htable *mdb;
Herbert Xub1951672010-02-27 19:41:51 +00001984
1985 spin_lock(&br->multicast_lock);
1986 if (!netif_running(br->dev))
1987 goto unlock;
1988
1989 err = -EINVAL;
1990 if (!is_power_of_2(val))
1991 goto unlock;
Eric Dumazete8051682010-11-15 06:38:10 +00001992
1993 mdb = mlock_dereference(br->mdb, br);
1994 if (mdb && val < mdb->size)
Herbert Xub1951672010-02-27 19:41:51 +00001995 goto unlock;
1996
1997 err = 0;
1998
1999 old = br->hash_max;
2000 br->hash_max = val;
2001
Eric Dumazete8051682010-11-15 06:38:10 +00002002 if (mdb) {
2003 if (mdb->old) {
Herbert Xub1951672010-02-27 19:41:51 +00002004 err = -EEXIST;
2005rollback:
2006 br->hash_max = old;
2007 goto unlock;
2008 }
2009
2010 err = br_mdb_rehash(&br->mdb, br->hash_max,
2011 br->hash_elasticity);
2012 if (err)
2013 goto rollback;
2014 }
2015
2016unlock:
2017 spin_unlock(&br->multicast_lock);
2018
2019 return err;
2020}