blob: e56bae4f59ce8be052e5a6c2ad95e436b9a9b464 [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>
Linus Lüssing3c3769e2013-09-04 02:13:39 +020032#include <net/addrconf.h>
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090033#endif
Herbert Xueb1d1642010-02-27 19:41:45 +000034
35#include "br_private.h"
36
Linus Lüssingcc0fdd82013-08-30 17:28:17 +020037static void br_multicast_start_querier(struct net_bridge *br,
38 struct bridge_mcast_query *query);
Cong Wang2ce297f2012-12-10 02:15:35 +000039unsigned int br_mdb_rehash_seq;
Herbert Xuc83b8fa2012-04-13 02:37:42 +000040
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090041static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
42{
43 if (a->proto != b->proto)
44 return 0;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000045 if (a->vid != b->vid)
46 return 0;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090047 switch (a->proto) {
48 case htons(ETH_P_IP):
49 return a->u.ip4 == b->u.ip4;
Eric Dumazetdfd56b82011-12-10 09:48:31 +000050#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090051 case htons(ETH_P_IPV6):
52 return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
53#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090054 }
55 return 0;
56}
57
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000058static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip,
59 __u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +000060{
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000061 return jhash_2words((__force u32)ip, vid, mdb->secret) & (mdb->max - 1);
Herbert Xueb1d1642010-02-27 19:41:45 +000062}
63
Eric Dumazetdfd56b82011-12-10 09:48:31 +000064#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090065static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000066 const struct in6_addr *ip,
67 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090068{
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000069 return jhash_2words(ipv6_addr_hash(ip), vid,
70 mdb->secret) & (mdb->max - 1);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090071}
72#endif
73
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090074static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb,
75 struct br_ip *ip)
76{
77 switch (ip->proto) {
78 case htons(ETH_P_IP):
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000079 return __br_ip4_hash(mdb, ip->u.ip4, ip->vid);
Eric Dumazetdfd56b82011-12-10 09:48:31 +000080#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090081 case htons(ETH_P_IPV6):
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000082 return __br_ip6_hash(mdb, &ip->u.ip6, ip->vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090083#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090084 }
85 return 0;
Herbert Xueb1d1642010-02-27 19:41:45 +000086}
87
88static struct net_bridge_mdb_entry *__br_mdb_ip_get(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090089 struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +000090{
91 struct net_bridge_mdb_entry *mp;
Herbert Xueb1d1642010-02-27 19:41:45 +000092
Sasha Levinb67bfe02013-02-27 17:06:00 -080093 hlist_for_each_entry_rcu(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090094 if (br_ip_equal(&mp->addr, dst))
Herbert Xueb1d1642010-02-27 19:41:45 +000095 return mp;
96 }
97
98 return NULL;
99}
100
Cong Wangcfd56752012-12-11 22:23:08 +0000101struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge_mdb_htable *mdb,
102 struct br_ip *dst)
Herbert Xu7f285fa2010-07-05 14:50:08 +0000103{
104 if (!mdb)
105 return NULL;
106
107 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
108}
109
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900110static struct net_bridge_mdb_entry *br_mdb_ip4_get(
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000111 struct net_bridge_mdb_htable *mdb, __be32 dst, __u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000112{
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900113 struct br_ip br_dst;
Herbert Xu0821ec52010-03-15 20:38:25 -0700114
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900115 br_dst.u.ip4 = dst;
116 br_dst.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000117 br_dst.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900118
Herbert Xu7f285fa2010-07-05 14:50:08 +0000119 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900120}
121
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000122#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900123static struct net_bridge_mdb_entry *br_mdb_ip6_get(
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000124 struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst,
125 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900126{
127 struct br_ip br_dst;
128
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000129 br_dst.u.ip6 = *dst;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900130 br_dst.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000131 br_dst.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900132
Herbert Xu7f285fa2010-07-05 14:50:08 +0000133 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900134}
135#endif
136
Herbert Xueb1d1642010-02-27 19:41:45 +0000137struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
Cong Wangfbca58a2013-03-07 03:05:33 +0000138 struct sk_buff *skb, u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000139{
Eric Dumazete8051682010-11-15 06:38:10 +0000140 struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900141 struct br_ip ip;
142
Herbert Xu7f285fa2010-07-05 14:50:08 +0000143 if (br->multicast_disabled)
Herbert Xueb1d1642010-02-27 19:41:45 +0000144 return NULL;
145
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900146 if (BR_INPUT_SKB_CB(skb)->igmp)
147 return NULL;
148
149 ip.proto = skb->protocol;
Cong Wangfbca58a2013-03-07 03:05:33 +0000150 ip.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900151
Herbert Xueb1d1642010-02-27 19:41:45 +0000152 switch (skb->protocol) {
153 case htons(ETH_P_IP):
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900154 ip.u.ip4 = ip_hdr(skb)->daddr;
155 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000156#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900157 case htons(ETH_P_IPV6):
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000158 ip.u.ip6 = ipv6_hdr(skb)->daddr;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900159 break;
160#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900161 default:
162 return NULL;
Herbert Xueb1d1642010-02-27 19:41:45 +0000163 }
164
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900165 return br_mdb_ip_get(mdb, &ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000166}
167
168static void br_mdb_free(struct rcu_head *head)
169{
170 struct net_bridge_mdb_htable *mdb =
171 container_of(head, struct net_bridge_mdb_htable, rcu);
172 struct net_bridge_mdb_htable *old = mdb->old;
173
174 mdb->old = NULL;
175 kfree(old->mhash);
176 kfree(old);
177}
178
179static int br_mdb_copy(struct net_bridge_mdb_htable *new,
180 struct net_bridge_mdb_htable *old,
181 int elasticity)
182{
183 struct net_bridge_mdb_entry *mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000184 int maxlen;
185 int len;
186 int i;
187
188 for (i = 0; i < old->max; i++)
Sasha Levinb67bfe02013-02-27 17:06:00 -0800189 hlist_for_each_entry(mp, &old->mhash[i], hlist[old->ver])
Herbert Xueb1d1642010-02-27 19:41:45 +0000190 hlist_add_head(&mp->hlist[new->ver],
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900191 &new->mhash[br_ip_hash(new, &mp->addr)]);
Herbert Xueb1d1642010-02-27 19:41:45 +0000192
193 if (!elasticity)
194 return 0;
195
196 maxlen = 0;
197 for (i = 0; i < new->max; i++) {
198 len = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800199 hlist_for_each_entry(mp, &new->mhash[i], hlist[new->ver])
Herbert Xueb1d1642010-02-27 19:41:45 +0000200 len++;
201 if (len > maxlen)
202 maxlen = len;
203 }
204
205 return maxlen > elasticity ? -EINVAL : 0;
206}
207
Cong Wangcfd56752012-12-11 22:23:08 +0000208void br_multicast_free_pg(struct rcu_head *head)
Herbert Xueb1d1642010-02-27 19:41:45 +0000209{
210 struct net_bridge_port_group *p =
211 container_of(head, struct net_bridge_port_group, rcu);
212
213 kfree(p);
214}
215
216static void br_multicast_free_group(struct rcu_head *head)
217{
218 struct net_bridge_mdb_entry *mp =
219 container_of(head, struct net_bridge_mdb_entry, rcu);
220
221 kfree(mp);
222}
223
224static void br_multicast_group_expired(unsigned long data)
225{
226 struct net_bridge_mdb_entry *mp = (void *)data;
227 struct net_bridge *br = mp->br;
228 struct net_bridge_mdb_htable *mdb;
229
230 spin_lock(&br->multicast_lock);
231 if (!netif_running(br->dev) || timer_pending(&mp->timer))
232 goto out;
233
Herbert Xu8a870172011-02-12 01:05:42 -0800234 mp->mglist = false;
Herbert Xueb1d1642010-02-27 19:41:45 +0000235
236 if (mp->ports)
237 goto out;
238
Eric Dumazete8051682010-11-15 06:38:10 +0000239 mdb = mlock_dereference(br->mdb, br);
240
Herbert Xueb1d1642010-02-27 19:41:45 +0000241 hlist_del_rcu(&mp->hlist[mdb->ver]);
242 mdb->size--;
243
Herbert Xueb1d1642010-02-27 19:41:45 +0000244 call_rcu_bh(&mp->rcu, br_multicast_free_group);
245
246out:
247 spin_unlock(&br->multicast_lock);
248}
249
250static void br_multicast_del_pg(struct net_bridge *br,
251 struct net_bridge_port_group *pg)
252{
Eric Dumazete8051682010-11-15 06:38:10 +0000253 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000254 struct net_bridge_mdb_entry *mp;
255 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +0000256 struct net_bridge_port_group __rcu **pp;
257
258 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +0000259
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900260 mp = br_mdb_ip_get(mdb, &pg->addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000261 if (WARN_ON(!mp))
262 return;
263
Eric Dumazete8051682010-11-15 06:38:10 +0000264 for (pp = &mp->ports;
265 (p = mlock_dereference(*pp, br)) != NULL;
266 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000267 if (p != pg)
268 continue;
269
stephen hemminger83f6a742010-04-27 15:01:06 +0000270 rcu_assign_pointer(*pp, p->next);
Herbert Xueb1d1642010-02-27 19:41:45 +0000271 hlist_del_init(&p->mglist);
272 del_timer(&p->timer);
Herbert Xueb1d1642010-02-27 19:41:45 +0000273 call_rcu_bh(&p->rcu, br_multicast_free_pg);
274
Linus Lüssing454594f2013-10-20 00:58:57 +0200275 if (!mp->ports && !mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +0000276 netif_running(br->dev))
277 mod_timer(&mp->timer, jiffies);
278
279 return;
280 }
281
282 WARN_ON(1);
283}
284
285static void br_multicast_port_group_expired(unsigned long data)
286{
287 struct net_bridge_port_group *pg = (void *)data;
288 struct net_bridge *br = pg->port->br;
289
290 spin_lock(&br->multicast_lock);
291 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
Amerigo Wangccb1c312012-12-14 22:09:51 +0000292 hlist_unhashed(&pg->mglist) || pg->state & MDB_PERMANENT)
Herbert Xueb1d1642010-02-27 19:41:45 +0000293 goto out;
294
295 br_multicast_del_pg(br, pg);
296
297out:
298 spin_unlock(&br->multicast_lock);
299}
300
Eric Dumazete8051682010-11-15 06:38:10 +0000301static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max,
Herbert Xueb1d1642010-02-27 19:41:45 +0000302 int elasticity)
303{
Eric Dumazete8051682010-11-15 06:38:10 +0000304 struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000305 struct net_bridge_mdb_htable *mdb;
306 int err;
307
308 mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
309 if (!mdb)
310 return -ENOMEM;
311
312 mdb->max = max;
313 mdb->old = old;
314
315 mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
316 if (!mdb->mhash) {
317 kfree(mdb);
318 return -ENOMEM;
319 }
320
321 mdb->size = old ? old->size : 0;
322 mdb->ver = old ? old->ver ^ 1 : 0;
323
324 if (!old || elasticity)
325 get_random_bytes(&mdb->secret, sizeof(mdb->secret));
326 else
327 mdb->secret = old->secret;
328
329 if (!old)
330 goto out;
331
332 err = br_mdb_copy(mdb, old, elasticity);
333 if (err) {
334 kfree(mdb->mhash);
335 kfree(mdb);
336 return err;
337 }
338
Cong Wang2ce297f2012-12-10 02:15:35 +0000339 br_mdb_rehash_seq++;
Herbert Xueb1d1642010-02-27 19:41:45 +0000340 call_rcu_bh(&mdb->rcu, br_mdb_free);
341
342out:
343 rcu_assign_pointer(*mdbp, mdb);
344
345 return 0;
346}
347
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900348static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
349 __be32 group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000350{
351 struct sk_buff *skb;
352 struct igmphdr *ih;
353 struct ethhdr *eth;
354 struct iphdr *iph;
355
356 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
357 sizeof(*ih) + 4);
358 if (!skb)
359 goto out;
360
361 skb->protocol = htons(ETH_P_IP);
362
363 skb_reset_mac_header(skb);
364 eth = eth_hdr(skb);
365
Joe Perchesd458cdf2013-10-01 19:04:40 -0700366 memcpy(eth->h_source, br->dev->dev_addr, ETH_ALEN);
Herbert Xueb1d1642010-02-27 19:41:45 +0000367 eth->h_dest[0] = 1;
368 eth->h_dest[1] = 0;
369 eth->h_dest[2] = 0x5e;
370 eth->h_dest[3] = 0;
371 eth->h_dest[4] = 0;
372 eth->h_dest[5] = 1;
373 eth->h_proto = htons(ETH_P_IP);
374 skb_put(skb, sizeof(*eth));
375
376 skb_set_network_header(skb, skb->len);
377 iph = ip_hdr(skb);
378
379 iph->version = 4;
380 iph->ihl = 6;
381 iph->tos = 0xc0;
382 iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4);
383 iph->id = 0;
384 iph->frag_off = htons(IP_DF);
385 iph->ttl = 1;
386 iph->protocol = IPPROTO_IGMP;
Cong Wang1c8ad5b2013-05-21 21:52:54 +0000387 iph->saddr = br->multicast_query_use_ifaddr ?
388 inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000389 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
390 ((u8 *)&iph[1])[0] = IPOPT_RA;
391 ((u8 *)&iph[1])[1] = 4;
392 ((u8 *)&iph[1])[2] = 0;
393 ((u8 *)&iph[1])[3] = 0;
394 ip_send_check(iph);
395 skb_put(skb, 24);
396
397 skb_set_transport_header(skb, skb->len);
398 ih = igmp_hdr(skb);
399 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
400 ih->code = (group ? br->multicast_last_member_interval :
401 br->multicast_query_response_interval) /
402 (HZ / IGMP_TIMER_SCALE);
403 ih->group = group;
404 ih->csum = 0;
405 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
406 skb_put(skb, sizeof(*ih));
407
408 __skb_pull(skb, sizeof(*eth));
409
410out:
411 return skb;
412}
413
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000414#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900415static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000416 const struct in6_addr *group)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900417{
418 struct sk_buff *skb;
419 struct ipv6hdr *ip6h;
420 struct mld_msg *mldq;
421 struct ethhdr *eth;
422 u8 *hopopt;
423 unsigned long interval;
424
425 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
426 8 + sizeof(*mldq));
427 if (!skb)
428 goto out;
429
430 skb->protocol = htons(ETH_P_IPV6);
431
432 /* Ethernet header */
433 skb_reset_mac_header(skb);
434 eth = eth_hdr(skb);
435
Joe Perchesd458cdf2013-10-01 19:04:40 -0700436 memcpy(eth->h_source, br->dev->dev_addr, ETH_ALEN);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900437 eth->h_proto = htons(ETH_P_IPV6);
438 skb_put(skb, sizeof(*eth));
439
440 /* IPv6 header + HbH option */
441 skb_set_network_header(skb, skb->len);
442 ip6h = ipv6_hdr(skb);
443
444 *(__force __be32 *)ip6h = htonl(0x60000000);
David Stevens76d6615862010-12-14 08:42:16 +0000445 ip6h->payload_len = htons(8 + sizeof(*mldq));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900446 ip6h->nexthdr = IPPROTO_HOPOPTS;
447 ip6h->hop_limit = 1;
Linus Lüssinga7bff752011-03-22 11:40:32 +0000448 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
Ulrich Weberd1d81d42012-03-05 04:52:44 +0000449 if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
450 &ip6h->saddr)) {
451 kfree_skb(skb);
452 return NULL;
453 }
Linus Lüssing36cff5a2011-02-17 08:17:51 +0000454 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900455
456 hopopt = (u8 *)(ip6h + 1);
457 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
458 hopopt[1] = 0; /* length of HbH */
459 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
460 hopopt[3] = 2; /* Length of RA Option */
461 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
462 hopopt[5] = 0;
Eldad Zack1de5a712012-05-17 06:00:25 +0000463 hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */
464 hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900465
466 skb_put(skb, sizeof(*ip6h) + 8);
467
468 /* ICMPv6 */
469 skb_set_transport_header(skb, skb->len);
470 mldq = (struct mld_msg *) icmp6_hdr(skb);
471
Linus Lüssing32de8682013-06-16 23:20:34 +0200472 interval = ipv6_addr_any(group) ?
473 br->multicast_query_response_interval :
474 br->multicast_last_member_interval;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900475
476 mldq->mld_type = ICMPV6_MGM_QUERY;
477 mldq->mld_code = 0;
478 mldq->mld_cksum = 0;
479 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
480 mldq->mld_reserved = 0;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000481 mldq->mld_mca = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900482
483 /* checksum */
484 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
485 sizeof(*mldq), IPPROTO_ICMPV6,
486 csum_partial(mldq,
487 sizeof(*mldq), 0));
488 skb_put(skb, sizeof(*mldq));
489
490 __skb_pull(skb, sizeof(*eth));
491
492out:
493 return skb;
494}
495#endif
496
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900497static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
498 struct br_ip *addr)
499{
500 switch (addr->proto) {
501 case htons(ETH_P_IP):
502 return br_ip4_multicast_alloc_query(br, addr->u.ip4);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000503#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900504 case htons(ETH_P_IPV6):
505 return br_ip6_multicast_alloc_query(br, &addr->u.ip6);
506#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900507 }
508 return NULL;
509}
510
Herbert Xueb1d1642010-02-27 19:41:45 +0000511static struct net_bridge_mdb_entry *br_multicast_get_group(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900512 struct net_bridge *br, struct net_bridge_port *port,
513 struct br_ip *group, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +0000514{
Eric Dumazete8051682010-11-15 06:38:10 +0000515 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000516 struct net_bridge_mdb_entry *mp;
Eric Dumazet95c96172012-04-15 05:58:06 +0000517 unsigned int count = 0;
518 unsigned int max;
Herbert Xueb1d1642010-02-27 19:41:45 +0000519 int elasticity;
520 int err;
521
Eric Dumazete8051682010-11-15 06:38:10 +0000522 mdb = rcu_dereference_protected(br->mdb, 1);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800523 hlist_for_each_entry(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000524 count++;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900525 if (unlikely(br_ip_equal(group, &mp->addr)))
Herbert Xueb1d1642010-02-27 19:41:45 +0000526 return mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000527 }
528
529 elasticity = 0;
530 max = mdb->max;
531
532 if (unlikely(count > br->hash_elasticity && count)) {
533 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000534 br_info(br, "Multicast hash table "
535 "chain limit reached: %s\n",
536 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000537
538 elasticity = br->hash_elasticity;
539 }
540
541 if (mdb->size >= max) {
542 max *= 2;
Thomas Graf036be6d2012-07-10 22:29:19 +0000543 if (unlikely(max > br->hash_max)) {
544 br_warn(br, "Multicast hash table maximum of %d "
545 "reached, disabling snooping: %s\n",
546 br->hash_max,
547 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000548 err = -E2BIG;
549disable:
550 br->multicast_disabled = 1;
551 goto err;
552 }
553 }
554
555 if (max > mdb->max || elasticity) {
556 if (mdb->old) {
557 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000558 br_info(br, "Multicast hash table "
559 "on fire: %s\n",
560 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000561 err = -EEXIST;
562 goto err;
563 }
564
565 err = br_mdb_rehash(&br->mdb, max, elasticity);
566 if (err) {
stephen hemminger28a16c92010-05-10 09:31:09 +0000567 br_warn(br, "Cannot rehash multicast "
568 "hash table, disabling snooping: %s, %d, %d\n",
569 port ? port->dev->name : br->dev->name,
570 mdb->size, err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000571 goto disable;
572 }
573
574 err = -EAGAIN;
575 goto err;
576 }
577
578 return NULL;
579
580err:
581 mp = ERR_PTR(err);
582 return mp;
583}
584
Cong Wangcfd56752012-12-11 22:23:08 +0000585struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
586 struct net_bridge_port *port, struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000587{
Eric Dumazete8051682010-11-15 06:38:10 +0000588 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000589 struct net_bridge_mdb_entry *mp;
590 int hash;
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000591 int err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000592
Eric Dumazete8051682010-11-15 06:38:10 +0000593 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000594 if (!mdb) {
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000595 err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0);
596 if (err)
597 return ERR_PTR(err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000598 goto rehash;
599 }
600
601 hash = br_ip_hash(mdb, group);
602 mp = br_multicast_get_group(br, port, group, hash);
603 switch (PTR_ERR(mp)) {
604 case 0:
605 break;
606
607 case -EAGAIN:
608rehash:
Eric Dumazete8051682010-11-15 06:38:10 +0000609 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000610 hash = br_ip_hash(mdb, group);
611 break;
612
613 default:
614 goto out;
615 }
616
617 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
618 if (unlikely(!mp))
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000619 return ERR_PTR(-ENOMEM);
Herbert Xueb1d1642010-02-27 19:41:45 +0000620
621 mp->br = br;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900622 mp->addr = *group;
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;
Linus Lüssing454594f2013-10-20 00:58:57 +0200662 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +0000663 int err;
664
Herbert Xueb1d1642010-02-27 19:41:45 +0000665 spin_lock(&br->multicast_lock);
666 if (!netif_running(br->dev) ||
667 (port && port->state == BR_STATE_DISABLED))
668 goto out;
669
670 mp = br_multicast_new_group(br, port, group);
671 err = PTR_ERR(mp);
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000672 if (IS_ERR(mp))
Herbert Xueb1d1642010-02-27 19:41:45 +0000673 goto err;
674
675 if (!port) {
Herbert Xu8a870172011-02-12 01:05:42 -0800676 mp->mglist = true;
Linus Lüssing454594f2013-10-20 00:58:57 +0200677 mod_timer(&mp->timer, now + br->multicast_membership_interval);
Herbert Xueb1d1642010-02-27 19:41:45 +0000678 goto out;
679 }
680
Eric Dumazete8051682010-11-15 06:38:10 +0000681 for (pp = &mp->ports;
682 (p = mlock_dereference(*pp, br)) != NULL;
683 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000684 if (p->port == port)
Linus Lüssing454594f2013-10-20 00:58:57 +0200685 goto found;
Herbert Xueb1d1642010-02-27 19:41:45 +0000686 if ((unsigned long)p->port < (unsigned long)port)
687 break;
688 }
689
Amerigo Wangccb1c312012-12-14 22:09:51 +0000690 p = br_multicast_new_port_group(port, group, *pp, MDB_TEMPORARY);
Herbert Xueb1d1642010-02-27 19:41:45 +0000691 if (unlikely(!p))
692 goto err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000693 rcu_assign_pointer(*pp, p);
Cong Wang37a393b2012-12-11 22:23:07 +0000694 br_mdb_notify(br->dev, port, group, RTM_NEWMDB);
Herbert Xueb1d1642010-02-27 19:41:45 +0000695
Linus Lüssing454594f2013-10-20 00:58:57 +0200696found:
697 mod_timer(&p->timer, now + br->multicast_membership_interval);
Herbert Xueb1d1642010-02-27 19:41:45 +0000698out:
699 err = 0;
700
701err:
702 spin_unlock(&br->multicast_lock);
703 return err;
704}
705
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900706static int br_ip4_multicast_add_group(struct net_bridge *br,
707 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000708 __be32 group,
709 __u16 vid)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900710{
711 struct br_ip br_group;
712
713 if (ipv4_is_local_multicast(group))
714 return 0;
715
716 br_group.u.ip4 = group;
717 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000718 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900719
720 return br_multicast_add_group(br, port, &br_group);
721}
722
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000723#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900724static int br_ip6_multicast_add_group(struct net_bridge *br,
725 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000726 const struct in6_addr *group,
727 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900728{
729 struct br_ip br_group;
730
Linus Lüssing3c3769e2013-09-04 02:13:39 +0200731 if (ipv6_addr_is_ll_all_nodes(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900732 return 0;
733
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000734 br_group.u.ip6 = *group;
Linus Lüssing9cc6e0c2011-02-15 13:19:17 +0000735 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000736 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900737
738 return br_multicast_add_group(br, port, &br_group);
739}
740#endif
741
Herbert Xueb1d1642010-02-27 19:41:45 +0000742static void br_multicast_router_expired(unsigned long data)
743{
744 struct net_bridge_port *port = (void *)data;
745 struct net_bridge *br = port->br;
746
747 spin_lock(&br->multicast_lock);
748 if (port->multicast_router != 1 ||
749 timer_pending(&port->multicast_router_timer) ||
750 hlist_unhashed(&port->rlist))
751 goto out;
752
753 hlist_del_init_rcu(&port->rlist);
754
755out:
756 spin_unlock(&br->multicast_lock);
757}
758
759static void br_multicast_local_router_expired(unsigned long data)
760{
761}
762
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200763static void br_multicast_querier_expired(struct net_bridge *br,
764 struct bridge_mcast_query *query)
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000765{
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000766 spin_lock(&br->multicast_lock);
767 if (!netif_running(br->dev) || br->multicast_disabled)
768 goto out;
769
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200770 br_multicast_start_querier(br, query);
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000771
772out:
773 spin_unlock(&br->multicast_lock);
774}
775
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200776static void br_ip4_multicast_querier_expired(unsigned long data)
777{
778 struct net_bridge *br = (void *)data;
779
780 br_multicast_querier_expired(br, &br->ip4_query);
781}
782
783#if IS_ENABLED(CONFIG_IPV6)
784static void br_ip6_multicast_querier_expired(unsigned long data)
785{
786 struct net_bridge *br = (void *)data;
787
788 br_multicast_querier_expired(br, &br->ip6_query);
789}
790#endif
791
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900792static void __br_multicast_send_query(struct net_bridge *br,
793 struct net_bridge_port *port,
794 struct br_ip *ip)
Herbert Xueb1d1642010-02-27 19:41:45 +0000795{
Herbert Xueb1d1642010-02-27 19:41:45 +0000796 struct sk_buff *skb;
797
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900798 skb = br_multicast_alloc_query(br, ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000799 if (!skb)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900800 return;
Herbert Xueb1d1642010-02-27 19:41:45 +0000801
802 if (port) {
803 __skb_push(skb, sizeof(struct ethhdr));
804 skb->dev = port->dev;
Jan Engelhardt713aefa2010-03-23 04:07:21 +0100805 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
Herbert Xueb1d1642010-02-27 19:41:45 +0000806 dev_queue_xmit);
807 } else
808 netif_rx(skb);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900809}
Herbert Xueb1d1642010-02-27 19:41:45 +0000810
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900811static void br_multicast_send_query(struct net_bridge *br,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200812 struct net_bridge_port *port,
813 struct bridge_mcast_query *query)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900814{
815 unsigned long time;
816 struct br_ip br_group;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200817 struct bridge_mcast_querier *querier = NULL;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900818
819 if (!netif_running(br->dev) || br->multicast_disabled ||
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200820 !br->multicast_querier)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900821 return;
822
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900823 memset(&br_group.u, 0, sizeof(br_group.u));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900824
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200825 if (port ? (query == &port->ip4_query) :
826 (query == &br->ip4_query)) {
827 querier = &br->ip4_querier;
828 br_group.proto = htons(ETH_P_IP);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000829#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200830 } else {
831 querier = &br->ip6_querier;
832 br_group.proto = htons(ETH_P_IPV6);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900833#endif
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200834 }
835
836 if (!querier || timer_pending(&querier->timer))
837 return;
838
839 __br_multicast_send_query(br, port, &br_group);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900840
Herbert Xueb1d1642010-02-27 19:41:45 +0000841 time = jiffies;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200842 time += query->startup_sent < br->multicast_startup_query_count ?
Herbert Xueb1d1642010-02-27 19:41:45 +0000843 br->multicast_startup_query_interval :
844 br->multicast_query_interval;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200845 mod_timer(&query->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +0000846}
847
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200848static void br_multicast_port_query_expired(struct net_bridge_port *port,
849 struct bridge_mcast_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +0000850{
Herbert Xueb1d1642010-02-27 19:41:45 +0000851 struct net_bridge *br = port->br;
852
853 spin_lock(&br->multicast_lock);
Dan Carpenter02a780c2010-03-06 01:14:09 +0000854 if (port->state == BR_STATE_DISABLED ||
855 port->state == BR_STATE_BLOCKING)
Herbert Xueb1d1642010-02-27 19:41:45 +0000856 goto out;
857
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200858 if (query->startup_sent < br->multicast_startup_query_count)
859 query->startup_sent++;
Herbert Xueb1d1642010-02-27 19:41:45 +0000860
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200861 br_multicast_send_query(port->br, port, query);
Herbert Xueb1d1642010-02-27 19:41:45 +0000862
863out:
864 spin_unlock(&br->multicast_lock);
865}
866
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200867static void br_ip4_multicast_port_query_expired(unsigned long data)
868{
869 struct net_bridge_port *port = (void *)data;
870
871 br_multicast_port_query_expired(port, &port->ip4_query);
872}
873
874#if IS_ENABLED(CONFIG_IPV6)
875static void br_ip6_multicast_port_query_expired(unsigned long data)
876{
877 struct net_bridge_port *port = (void *)data;
878
879 br_multicast_port_query_expired(port, &port->ip6_query);
880}
881#endif
882
Herbert Xueb1d1642010-02-27 19:41:45 +0000883void br_multicast_add_port(struct net_bridge_port *port)
884{
885 port->multicast_router = 1;
886
887 setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
888 (unsigned long)port);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200889 setup_timer(&port->ip4_query.timer, br_ip4_multicast_port_query_expired,
890 (unsigned long)port);
891#if IS_ENABLED(CONFIG_IPV6)
892 setup_timer(&port->ip6_query.timer, br_ip6_multicast_port_query_expired,
893 (unsigned long)port);
894#endif
Herbert Xueb1d1642010-02-27 19:41:45 +0000895}
896
897void br_multicast_del_port(struct net_bridge_port *port)
898{
899 del_timer_sync(&port->multicast_router_timer);
900}
901
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200902static void br_multicast_enable(struct bridge_mcast_query *query)
Herbert Xu561f1102010-02-27 19:41:50 +0000903{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200904 query->startup_sent = 0;
Herbert Xu561f1102010-02-27 19:41:50 +0000905
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200906 if (try_to_del_timer_sync(&query->timer) >= 0 ||
907 del_timer(&query->timer))
908 mod_timer(&query->timer, jiffies);
Herbert Xu561f1102010-02-27 19:41:50 +0000909}
910
Herbert Xueb1d1642010-02-27 19:41:45 +0000911void br_multicast_enable_port(struct net_bridge_port *port)
912{
913 struct net_bridge *br = port->br;
914
915 spin_lock(&br->multicast_lock);
916 if (br->multicast_disabled || !netif_running(br->dev))
917 goto out;
918
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200919 br_multicast_enable(&port->ip4_query);
920#if IS_ENABLED(CONFIG_IPV6)
921 br_multicast_enable(&port->ip6_query);
922#endif
Herbert Xueb1d1642010-02-27 19:41:45 +0000923
924out:
925 spin_unlock(&br->multicast_lock);
926}
927
928void br_multicast_disable_port(struct net_bridge_port *port)
929{
930 struct net_bridge *br = port->br;
931 struct net_bridge_port_group *pg;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800932 struct hlist_node *n;
Herbert Xueb1d1642010-02-27 19:41:45 +0000933
934 spin_lock(&br->multicast_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800935 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
Herbert Xueb1d1642010-02-27 19:41:45 +0000936 br_multicast_del_pg(br, pg);
937
938 if (!hlist_unhashed(&port->rlist))
939 hlist_del_init_rcu(&port->rlist);
940 del_timer(&port->multicast_router_timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +0200941 del_timer(&port->ip4_query.timer);
942#if IS_ENABLED(CONFIG_IPV6)
943 del_timer(&port->ip6_query.timer);
944#endif
Herbert Xueb1d1642010-02-27 19:41:45 +0000945 spin_unlock(&br->multicast_lock);
946}
947
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900948static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
949 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -0400950 struct sk_buff *skb,
951 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000952{
953 struct igmpv3_report *ih;
954 struct igmpv3_grec *grec;
955 int i;
956 int len;
957 int num;
958 int type;
959 int err = 0;
960 __be32 group;
961
962 if (!pskb_may_pull(skb, sizeof(*ih)))
963 return -EINVAL;
964
965 ih = igmpv3_report_hdr(skb);
966 num = ntohs(ih->ngrec);
967 len = sizeof(*ih);
968
969 for (i = 0; i < num; i++) {
970 len += sizeof(*grec);
971 if (!pskb_may_pull(skb, len))
972 return -EINVAL;
973
Herbert Xufd218cf2010-04-07 21:20:47 -0700974 grec = (void *)(skb->data + len - sizeof(*grec));
Herbert Xueb1d1642010-02-27 19:41:45 +0000975 group = grec->grec_mca;
976 type = grec->grec_type;
977
Eric Dumazet8eabf952010-04-20 03:20:05 +0000978 len += ntohs(grec->grec_nsrcs) * 4;
Herbert Xueb1d1642010-02-27 19:41:45 +0000979 if (!pskb_may_pull(skb, len))
980 return -EINVAL;
981
982 /* We treat this as an IGMPv2 report for now. */
983 switch (type) {
984 case IGMPV3_MODE_IS_INCLUDE:
985 case IGMPV3_MODE_IS_EXCLUDE:
986 case IGMPV3_CHANGE_TO_INCLUDE:
987 case IGMPV3_CHANGE_TO_EXCLUDE:
988 case IGMPV3_ALLOW_NEW_SOURCES:
989 case IGMPV3_BLOCK_OLD_SOURCES:
990 break;
991
992 default:
993 continue;
994 }
995
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000996 err = br_ip4_multicast_add_group(br, port, group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +0000997 if (err)
998 break;
999 }
1000
1001 return err;
1002}
1003
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001004#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001005static int br_ip6_multicast_mld2_report(struct net_bridge *br,
1006 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001007 struct sk_buff *skb,
1008 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001009{
1010 struct icmp6hdr *icmp6h;
1011 struct mld2_grec *grec;
1012 int i;
1013 int len;
1014 int num;
1015 int err = 0;
1016
1017 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
1018 return -EINVAL;
1019
1020 icmp6h = icmp6_hdr(skb);
1021 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
1022 len = sizeof(*icmp6h);
1023
1024 for (i = 0; i < num; i++) {
1025 __be16 *nsrcs, _nsrcs;
1026
1027 nsrcs = skb_header_pointer(skb,
1028 len + offsetof(struct mld2_grec,
Linus Lüssing649e9842011-02-15 13:19:18 +00001029 grec_nsrcs),
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001030 sizeof(_nsrcs), &_nsrcs);
1031 if (!nsrcs)
1032 return -EINVAL;
1033
1034 if (!pskb_may_pull(skb,
1035 len + sizeof(*grec) +
Linus Lüssingd41db9f2011-02-15 13:19:19 +00001036 sizeof(struct in6_addr) * ntohs(*nsrcs)))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001037 return -EINVAL;
1038
1039 grec = (struct mld2_grec *)(skb->data + len);
Linus Lüssingd41db9f2011-02-15 13:19:19 +00001040 len += sizeof(*grec) +
1041 sizeof(struct in6_addr) * ntohs(*nsrcs);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001042
1043 /* We treat these as MLDv1 reports for now. */
1044 switch (grec->grec_type) {
1045 case MLD2_MODE_IS_INCLUDE:
1046 case MLD2_MODE_IS_EXCLUDE:
1047 case MLD2_CHANGE_TO_INCLUDE:
1048 case MLD2_CHANGE_TO_EXCLUDE:
1049 case MLD2_ALLOW_NEW_SOURCES:
1050 case MLD2_BLOCK_OLD_SOURCES:
1051 break;
1052
1053 default:
1054 continue;
1055 }
1056
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001057 err = br_ip6_multicast_add_group(br, port, &grec->grec_mca,
1058 vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001059 if (!err)
1060 break;
1061 }
1062
1063 return err;
1064}
1065#endif
1066
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001067static void
1068br_multicast_update_querier_timer(struct net_bridge *br,
1069 struct bridge_mcast_querier *querier,
1070 unsigned long max_delay)
Linus Lüssingb00589a2013-08-01 01:06:20 +02001071{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001072 if (!timer_pending(&querier->timer))
1073 querier->delay_time = jiffies + max_delay;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001074
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001075 mod_timer(&querier->timer, jiffies + br->multicast_querier_interval);
Linus Lüssingb00589a2013-08-01 01:06:20 +02001076}
1077
stephen hemminger7e80c122010-04-27 15:01:04 +00001078/*
Cong Wang7c776022013-06-21 15:37:25 +08001079 * Add port to router_list
stephen hemminger7e80c122010-04-27 15:01:04 +00001080 * list is maintained ordered by pointer value
1081 * and locked by br->multicast_lock and RCU
1082 */
Herbert Xu0909e112010-02-27 19:41:49 +00001083static void br_multicast_add_router(struct net_bridge *br,
1084 struct net_bridge_port *port)
1085{
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001086 struct net_bridge_port *p;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001087 struct hlist_node *slot = NULL;
Herbert Xu0909e112010-02-27 19:41:49 +00001088
Sasha Levinb67bfe02013-02-27 17:06:00 -08001089 hlist_for_each_entry(p, &br->router_list, rlist) {
stephen hemminger7e80c122010-04-27 15:01:04 +00001090 if ((unsigned long) port >= (unsigned long) p)
1091 break;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001092 slot = &p->rlist;
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001093 }
Herbert Xu0909e112010-02-27 19:41:49 +00001094
stephen hemminger7e80c122010-04-27 15:01:04 +00001095 if (slot)
1096 hlist_add_after_rcu(slot, &port->rlist);
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001097 else
1098 hlist_add_head_rcu(&port->rlist, &br->router_list);
Herbert Xu0909e112010-02-27 19:41:49 +00001099}
1100
Herbert Xueb1d1642010-02-27 19:41:45 +00001101static void br_multicast_mark_router(struct net_bridge *br,
1102 struct net_bridge_port *port)
1103{
1104 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +00001105
1106 if (!port) {
1107 if (br->multicast_router == 1)
1108 mod_timer(&br->multicast_router_timer,
1109 now + br->multicast_querier_interval);
1110 return;
1111 }
1112
1113 if (port->multicast_router != 1)
1114 return;
1115
1116 if (!hlist_unhashed(&port->rlist))
1117 goto timer;
1118
Herbert Xu0909e112010-02-27 19:41:49 +00001119 br_multicast_add_router(br, port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001120
1121timer:
1122 mod_timer(&port->multicast_router_timer,
1123 now + br->multicast_querier_interval);
1124}
1125
1126static void br_multicast_query_received(struct net_bridge *br,
1127 struct net_bridge_port *port,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001128 struct bridge_mcast_querier *querier,
Linus Lüssingb00589a2013-08-01 01:06:20 +02001129 int saddr,
1130 unsigned long max_delay)
Herbert Xueb1d1642010-02-27 19:41:45 +00001131{
1132 if (saddr)
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001133 br_multicast_update_querier_timer(br, querier, max_delay);
1134 else if (timer_pending(&querier->timer))
Herbert Xueb1d1642010-02-27 19:41:45 +00001135 return;
1136
1137 br_multicast_mark_router(br, port);
1138}
1139
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001140static int br_ip4_multicast_query(struct net_bridge *br,
1141 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001142 struct sk_buff *skb,
1143 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001144{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001145 const struct iphdr *iph = ip_hdr(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001146 struct igmphdr *ih = igmp_hdr(skb);
1147 struct net_bridge_mdb_entry *mp;
1148 struct igmpv3_query *ih3;
1149 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +00001150 struct net_bridge_port_group __rcu **pp;
Herbert Xueb1d1642010-02-27 19:41:45 +00001151 unsigned long max_delay;
1152 unsigned long now = jiffies;
1153 __be32 group;
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001154 int err = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001155
1156 spin_lock(&br->multicast_lock);
1157 if (!netif_running(br->dev) ||
1158 (port && port->state == BR_STATE_DISABLED))
1159 goto out;
1160
Herbert Xueb1d1642010-02-27 19:41:45 +00001161 group = ih->group;
1162
1163 if (skb->len == sizeof(*ih)) {
1164 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1165
1166 if (!max_delay) {
1167 max_delay = 10 * HZ;
1168 group = 0;
1169 }
1170 } else {
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001171 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) {
1172 err = -EINVAL;
1173 goto out;
1174 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001175
1176 ih3 = igmpv3_query_hdr(skb);
1177 if (ih3->nsrcs)
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001178 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001179
YOSHIFUJI Hideaki / 吉藤英明0ba8c9e2010-03-15 19:27:00 +00001180 max_delay = ih3->code ?
1181 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
Herbert Xueb1d1642010-02-27 19:41:45 +00001182 }
1183
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001184 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1185 * all-systems destination addresses (224.0.0.1) for general queries
1186 */
1187 if (!group && iph->daddr != htonl(INADDR_ALLHOSTS_GROUP)) {
1188 err = -EINVAL;
1189 goto out;
1190 }
1191
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001192 br_multicast_query_received(br, port, &br->ip4_querier, !!iph->saddr,
1193 max_delay);
Linus Lüssingb00589a2013-08-01 01:06:20 +02001194
Herbert Xueb1d1642010-02-27 19:41:45 +00001195 if (!group)
1196 goto out;
1197
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001198 mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001199 if (!mp)
1200 goto out;
1201
1202 max_delay *= br->multicast_last_member_count;
1203
Herbert Xu8a870172011-02-12 01:05:42 -08001204 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001205 (timer_pending(&mp->timer) ?
1206 time_after(mp->timer.expires, now + max_delay) :
1207 try_to_del_timer_sync(&mp->timer) >= 0))
1208 mod_timer(&mp->timer, now + max_delay);
1209
Eric Dumazete8051682010-11-15 06:38:10 +00001210 for (pp = &mp->ports;
1211 (p = mlock_dereference(*pp, br)) != NULL;
1212 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001213 if (timer_pending(&p->timer) ?
1214 time_after(p->timer.expires, now + max_delay) :
1215 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001216 mod_timer(&p->timer, now + max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001217 }
1218
1219out:
1220 spin_unlock(&br->multicast_lock);
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001221 return err;
Herbert Xueb1d1642010-02-27 19:41:45 +00001222}
1223
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001224#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001225static int br_ip6_multicast_query(struct net_bridge *br,
1226 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001227 struct sk_buff *skb,
1228 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001229{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001230 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
stephen hemmingereca2a432012-12-13 06:51:28 +00001231 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001232 struct net_bridge_mdb_entry *mp;
1233 struct mld2_query *mld2q;
Eric Dumazete8051682010-11-15 06:38:10 +00001234 struct net_bridge_port_group *p;
1235 struct net_bridge_port_group __rcu **pp;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001236 unsigned long max_delay;
1237 unsigned long now = jiffies;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001238 const struct in6_addr *group = NULL;
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001239 bool is_general_query;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001240 int err = 0;
1241
1242 spin_lock(&br->multicast_lock);
1243 if (!netif_running(br->dev) ||
1244 (port && port->state == BR_STATE_DISABLED))
1245 goto out;
1246
Linus Lüssing6565b9e2014-03-04 03:57:35 +01001247 /* RFC2710+RFC3810 (MLDv1+MLDv2) require link-local source addresses */
1248 if (!(ipv6_addr_type(&ip6h->saddr) & IPV6_ADDR_LINKLOCAL)) {
1249 err = -EINVAL;
1250 goto out;
1251 }
1252
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001253 if (skb->len == sizeof(*mld)) {
1254 if (!pskb_may_pull(skb, sizeof(*mld))) {
1255 err = -EINVAL;
1256 goto out;
1257 }
1258 mld = (struct mld_msg *) icmp6_hdr(skb);
Li RongQing47152132012-07-09 23:56:12 +00001259 max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001260 if (max_delay)
1261 group = &mld->mld_mca;
Linus Lüssing248ba8e2013-08-06 00:32:05 +02001262 } else {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001263 if (!pskb_may_pull(skb, sizeof(*mld2q))) {
1264 err = -EINVAL;
1265 goto out;
1266 }
1267 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1268 if (!mld2q->mld2q_nsrcs)
1269 group = &mld2q->mld2q_mca;
Daniel Borkmanne3f5b172013-09-04 00:19:39 +02001270
1271 max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001272 }
1273
Linus Lüssing9ed973c2014-03-10 22:25:24 +01001274 is_general_query = group && ipv6_addr_any(group);
1275
1276 /* RFC2710+RFC3810 (MLDv1+MLDv2) require the multicast link layer
1277 * all-nodes destination address (ff02::1) for general queries
1278 */
1279 if (is_general_query && !ipv6_addr_is_ll_all_nodes(&ip6h->daddr)) {
1280 err = -EINVAL;
1281 goto out;
1282 }
1283
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001284 br_multicast_query_received(br, port, &br->ip6_querier,
1285 !ipv6_addr_any(&ip6h->saddr), max_delay);
Linus Lüssingb00589a2013-08-01 01:06:20 +02001286
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001287 if (!group)
1288 goto out;
1289
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001290 mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001291 if (!mp)
1292 goto out;
1293
1294 max_delay *= br->multicast_last_member_count;
Herbert Xu8a870172011-02-12 01:05:42 -08001295 if (mp->mglist &&
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001296 (timer_pending(&mp->timer) ?
1297 time_after(mp->timer.expires, now + max_delay) :
1298 try_to_del_timer_sync(&mp->timer) >= 0))
1299 mod_timer(&mp->timer, now + max_delay);
1300
Eric Dumazete8051682010-11-15 06:38:10 +00001301 for (pp = &mp->ports;
1302 (p = mlock_dereference(*pp, br)) != NULL;
1303 pp = &p->next) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001304 if (timer_pending(&p->timer) ?
1305 time_after(p->timer.expires, now + max_delay) :
1306 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001307 mod_timer(&p->timer, now + max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001308 }
1309
1310out:
1311 spin_unlock(&br->multicast_lock);
1312 return err;
1313}
1314#endif
1315
Herbert Xueb1d1642010-02-27 19:41:45 +00001316static void br_multicast_leave_group(struct net_bridge *br,
1317 struct net_bridge_port *port,
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001318 struct br_ip *group,
1319 struct bridge_mcast_querier *querier,
1320 struct bridge_mcast_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +00001321{
1322 struct net_bridge_mdb_htable *mdb;
1323 struct net_bridge_mdb_entry *mp;
1324 struct net_bridge_port_group *p;
1325 unsigned long now;
1326 unsigned long time;
1327
Herbert Xueb1d1642010-02-27 19:41:45 +00001328 spin_lock(&br->multicast_lock);
1329 if (!netif_running(br->dev) ||
1330 (port && port->state == BR_STATE_DISABLED) ||
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001331 timer_pending(&querier->timer))
Herbert Xueb1d1642010-02-27 19:41:45 +00001332 goto out;
1333
Eric Dumazete8051682010-11-15 06:38:10 +00001334 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001335 mp = br_mdb_ip_get(mdb, group);
1336 if (!mp)
1337 goto out;
1338
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001339 if (br->multicast_querier) {
Cong Wang6b7df112013-05-21 21:52:56 +00001340 __br_multicast_send_query(br, port, &mp->addr);
1341
1342 time = jiffies + br->multicast_last_member_count *
1343 br->multicast_last_member_interval;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001344
1345 mod_timer(&query->timer, time);
Cong Wang6b7df112013-05-21 21:52:56 +00001346
1347 for (p = mlock_dereference(mp->ports, br);
1348 p != NULL;
1349 p = mlock_dereference(p->next, br)) {
1350 if (p->port != port)
1351 continue;
1352
1353 if (!hlist_unhashed(&p->mglist) &&
1354 (timer_pending(&p->timer) ?
1355 time_after(p->timer.expires, time) :
1356 try_to_del_timer_sync(&p->timer) >= 0)) {
1357 mod_timer(&p->timer, time);
1358 }
1359
1360 break;
1361 }
1362 }
1363
David S. Millerc2d3bab2012-12-05 16:24:45 -05001364 if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
Amerigo Wang50426b52012-12-03 23:56:40 +00001365 struct net_bridge_port_group __rcu **pp;
1366
1367 for (pp = &mp->ports;
1368 (p = mlock_dereference(*pp, br)) != NULL;
1369 pp = &p->next) {
1370 if (p->port != port)
1371 continue;
1372
1373 rcu_assign_pointer(*pp, p->next);
1374 hlist_del_init(&p->mglist);
1375 del_timer(&p->timer);
1376 call_rcu_bh(&p->rcu, br_multicast_free_pg);
Cong Wang37a393b2012-12-11 22:23:07 +00001377 br_mdb_notify(br->dev, port, group, RTM_DELMDB);
Amerigo Wang50426b52012-12-03 23:56:40 +00001378
Linus Lüssing454594f2013-10-20 00:58:57 +02001379 if (!mp->ports && !mp->mglist &&
Amerigo Wang50426b52012-12-03 23:56:40 +00001380 netif_running(br->dev))
1381 mod_timer(&mp->timer, jiffies);
1382 }
1383 goto out;
1384 }
1385
Herbert Xueb1d1642010-02-27 19:41:45 +00001386 now = jiffies;
1387 time = now + br->multicast_last_member_count *
1388 br->multicast_last_member_interval;
1389
1390 if (!port) {
Linus Lüssing454594f2013-10-20 00:58:57 +02001391 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001392 (timer_pending(&mp->timer) ?
1393 time_after(mp->timer.expires, time) :
1394 try_to_del_timer_sync(&mp->timer) >= 0)) {
1395 mod_timer(&mp->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +00001396 }
Linus Lüssing454594f2013-10-20 00:58:57 +02001397
1398 goto out;
1399 }
1400
1401 for (p = mlock_dereference(mp->ports, br);
1402 p != NULL;
1403 p = mlock_dereference(p->next, br)) {
1404 if (p->port != port)
1405 continue;
1406
1407 if (!hlist_unhashed(&p->mglist) &&
1408 (timer_pending(&p->timer) ?
1409 time_after(p->timer.expires, time) :
1410 try_to_del_timer_sync(&p->timer) >= 0)) {
1411 mod_timer(&p->timer, time);
1412 }
1413
1414 break;
Herbert Xueb1d1642010-02-27 19:41:45 +00001415 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001416out:
1417 spin_unlock(&br->multicast_lock);
1418}
1419
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001420static void br_ip4_multicast_leave_group(struct net_bridge *br,
1421 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001422 __be32 group,
1423 __u16 vid)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001424{
1425 struct br_ip br_group;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001426 struct bridge_mcast_query *query = port ? &port->ip4_query :
1427 &br->ip4_query;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001428
1429 if (ipv4_is_local_multicast(group))
1430 return;
1431
1432 br_group.u.ip4 = group;
1433 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001434 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001435
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001436 br_multicast_leave_group(br, port, &br_group, &br->ip4_querier, query);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001437}
1438
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001439#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001440static void br_ip6_multicast_leave_group(struct net_bridge *br,
1441 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001442 const struct in6_addr *group,
1443 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001444{
1445 struct br_ip br_group;
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001446 struct bridge_mcast_query *query = port ? &port->ip6_query :
1447 &br->ip6_query;
1448
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001449
Linus Lüssing3c3769e2013-09-04 02:13:39 +02001450 if (ipv6_addr_is_ll_all_nodes(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001451 return;
1452
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001453 br_group.u.ip6 = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001454 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001455 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001456
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001457 br_multicast_leave_group(br, port, &br_group, &br->ip6_querier, query);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001458}
1459#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001460
Herbert Xueb1d1642010-02-27 19:41:45 +00001461static int br_multicast_ipv4_rcv(struct net_bridge *br,
1462 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001463 struct sk_buff *skb,
1464 u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001465{
1466 struct sk_buff *skb2 = skb;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001467 const struct iphdr *iph;
Herbert Xueb1d1642010-02-27 19:41:45 +00001468 struct igmphdr *ih;
Eric Dumazet95c96172012-04-15 05:58:06 +00001469 unsigned int len;
1470 unsigned int offset;
Herbert Xueb1d1642010-02-27 19:41:45 +00001471 int err;
1472
Herbert Xueb1d1642010-02-27 19:41:45 +00001473 /* We treat OOM as packet loss for now. */
1474 if (!pskb_may_pull(skb, sizeof(*iph)))
1475 return -EINVAL;
1476
1477 iph = ip_hdr(skb);
1478
1479 if (iph->ihl < 5 || iph->version != 4)
1480 return -EINVAL;
1481
1482 if (!pskb_may_pull(skb, ip_hdrlen(skb)))
1483 return -EINVAL;
1484
1485 iph = ip_hdr(skb);
1486
1487 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1488 return -EINVAL;
1489
Herbert Xubd4265f2011-06-23 02:39:12 +00001490 if (iph->protocol != IPPROTO_IGMP) {
Amerigo Wangbf5e4dd2013-03-07 02:32:26 +00001491 if (!ipv4_is_local_multicast(iph->daddr))
Herbert Xubd4265f2011-06-23 02:39:12 +00001492 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Herbert Xueb1d1642010-02-27 19:41:45 +00001493 return 0;
Herbert Xubd4265f2011-06-23 02:39:12 +00001494 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001495
1496 len = ntohs(iph->tot_len);
1497 if (skb->len < len || len < ip_hdrlen(skb))
1498 return -EINVAL;
1499
1500 if (skb->len > len) {
1501 skb2 = skb_clone(skb, GFP_ATOMIC);
1502 if (!skb2)
1503 return -ENOMEM;
1504
1505 err = pskb_trim_rcsum(skb2, len);
1506 if (err)
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001507 goto err_out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001508 }
1509
1510 len -= ip_hdrlen(skb2);
1511 offset = skb_network_offset(skb2) + ip_hdrlen(skb2);
1512 __skb_pull(skb2, offset);
1513 skb_reset_transport_header(skb2);
1514
1515 err = -EINVAL;
1516 if (!pskb_may_pull(skb2, sizeof(*ih)))
1517 goto out;
1518
Herbert Xueb1d1642010-02-27 19:41:45 +00001519 switch (skb2->ip_summed) {
1520 case CHECKSUM_COMPLETE:
1521 if (!csum_fold(skb2->csum))
1522 break;
1523 /* fall through */
1524 case CHECKSUM_NONE:
1525 skb2->csum = 0;
1526 if (skb_checksum_complete(skb2))
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001527 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001528 }
1529
1530 err = 0;
1531
1532 BR_INPUT_SKB_CB(skb)->igmp = 1;
1533 ih = igmp_hdr(skb2);
1534
1535 switch (ih->type) {
1536 case IGMP_HOST_MEMBERSHIP_REPORT:
1537 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Fernando Luis Vázquez Cao62b2bcb2011-06-13 15:04:43 +00001538 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001539 err = br_ip4_multicast_add_group(br, port, ih->group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001540 break;
1541 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Vlad Yasevich06499092013-10-28 15:45:07 -04001542 err = br_ip4_multicast_igmp3_report(br, port, skb2, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001543 break;
1544 case IGMP_HOST_MEMBERSHIP_QUERY:
Vlad Yasevich06499092013-10-28 15:45:07 -04001545 err = br_ip4_multicast_query(br, port, skb2, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001546 break;
1547 case IGMP_HOST_LEAVE_MESSAGE:
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001548 br_ip4_multicast_leave_group(br, port, ih->group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001549 break;
1550 }
1551
1552out:
1553 __skb_push(skb2, offset);
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001554err_out:
Herbert Xueb1d1642010-02-27 19:41:45 +00001555 if (skb2 != skb)
1556 kfree_skb(skb2);
1557 return err;
1558}
1559
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001560#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001561static int br_multicast_ipv6_rcv(struct net_bridge *br,
1562 struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001563 struct sk_buff *skb,
1564 u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001565{
Tomas Winkler9d890812011-01-03 11:26:08 -08001566 struct sk_buff *skb2;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001567 const struct ipv6hdr *ip6h;
Eric Dumazet22df1332011-08-23 19:57:05 +00001568 u8 icmp6_type;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001569 u8 nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -08001570 __be16 frag_off;
Eric Dumazet95c96172012-04-15 05:58:06 +00001571 unsigned int len;
Kulikov Vasiliybb7a0bd2010-07-15 08:47:33 +00001572 int offset;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001573 int err;
1574
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001575 if (!pskb_may_pull(skb, sizeof(*ip6h)))
1576 return -EINVAL;
1577
1578 ip6h = ipv6_hdr(skb);
1579
1580 /*
1581 * We're interested in MLD messages only.
1582 * - Version is 6
1583 * - MLD has always Router Alert hop-by-hop option
1584 * - But we do not support jumbrograms.
1585 */
Linus Lüssing8fad9c32013-09-04 02:13:38 +02001586 if (ip6h->version != 6)
1587 return 0;
1588
1589 /* Prevent flooding this packet if there is no listener present */
Linus Lüssing3c3769e2013-09-04 02:13:39 +02001590 if (!ipv6_addr_is_ll_all_nodes(&ip6h->daddr))
Linus Lüssing8fad9c32013-09-04 02:13:38 +02001591 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1592
1593 if (ip6h->nexthdr != IPPROTO_HOPOPTS ||
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001594 ip6h->payload_len == 0)
1595 return 0;
1596
Linus Lüssingff9a57a2011-03-26 20:27:24 +00001597 len = ntohs(ip6h->payload_len) + sizeof(*ip6h);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001598 if (skb->len < len)
1599 return -EINVAL;
1600
1601 nexthdr = ip6h->nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -08001602 offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr, &frag_off);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001603
1604 if (offset < 0 || nexthdr != IPPROTO_ICMPV6)
1605 return 0;
1606
1607 /* Okay, we found ICMPv6 header */
1608 skb2 = skb_clone(skb, GFP_ATOMIC);
1609 if (!skb2)
1610 return -ENOMEM;
1611
Tomas Winkler9d890812011-01-03 11:26:08 -08001612 err = -EINVAL;
1613 if (!pskb_may_pull(skb2, offset + sizeof(struct icmp6hdr)))
1614 goto out;
1615
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001616 len -= offset - skb_network_offset(skb2);
1617
1618 __skb_pull(skb2, offset);
1619 skb_reset_transport_header(skb2);
stephen hemmingerfa2da8c2011-11-15 08:09:14 +00001620 skb_postpull_rcsum(skb2, skb_network_header(skb2),
1621 skb_network_header_len(skb2));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001622
Eric Dumazet22df1332011-08-23 19:57:05 +00001623 icmp6_type = icmp6_hdr(skb2)->icmp6_type;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001624
Eric Dumazet22df1332011-08-23 19:57:05 +00001625 switch (icmp6_type) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001626 case ICMPV6_MGM_QUERY:
1627 case ICMPV6_MGM_REPORT:
1628 case ICMPV6_MGM_REDUCTION:
1629 case ICMPV6_MLD2_REPORT:
1630 break;
1631 default:
1632 err = 0;
1633 goto out;
1634 }
1635
1636 /* Okay, we found MLD message. Check further. */
1637 if (skb2->len > len) {
1638 err = pskb_trim_rcsum(skb2, len);
1639 if (err)
1640 goto out;
Yan, Zheng4b275d72011-08-23 22:54:33 +00001641 err = -EINVAL;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001642 }
1643
Yan, Zheng4b275d72011-08-23 22:54:33 +00001644 ip6h = ipv6_hdr(skb2);
1645
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001646 switch (skb2->ip_summed) {
1647 case CHECKSUM_COMPLETE:
Yan, Zheng4b275d72011-08-23 22:54:33 +00001648 if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, skb2->len,
1649 IPPROTO_ICMPV6, skb2->csum))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001650 break;
1651 /*FALLTHROUGH*/
1652 case CHECKSUM_NONE:
Yan, Zheng4b275d72011-08-23 22:54:33 +00001653 skb2->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr,
1654 &ip6h->daddr,
1655 skb2->len,
1656 IPPROTO_ICMPV6, 0));
1657 if (__skb_checksum_complete(skb2))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001658 goto out;
1659 }
1660
1661 err = 0;
1662
1663 BR_INPUT_SKB_CB(skb)->igmp = 1;
1664
Eric Dumazet22df1332011-08-23 19:57:05 +00001665 switch (icmp6_type) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001666 case ICMPV6_MGM_REPORT:
1667 {
Tomas Winkler9d890812011-01-03 11:26:08 -08001668 struct mld_msg *mld;
1669 if (!pskb_may_pull(skb2, sizeof(*mld))) {
1670 err = -EINVAL;
1671 goto out;
1672 }
1673 mld = (struct mld_msg *)skb_transport_header(skb2);
Fernando Luis Vázquez Caofc2af6c2011-06-13 15:06:58 +00001674 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001675 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001676 break;
1677 }
1678 case ICMPV6_MLD2_REPORT:
Vlad Yasevich06499092013-10-28 15:45:07 -04001679 err = br_ip6_multicast_mld2_report(br, port, skb2, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001680 break;
1681 case ICMPV6_MGM_QUERY:
Vlad Yasevich06499092013-10-28 15:45:07 -04001682 err = br_ip6_multicast_query(br, port, skb2, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001683 break;
1684 case ICMPV6_MGM_REDUCTION:
1685 {
Tomas Winkler9d890812011-01-03 11:26:08 -08001686 struct mld_msg *mld;
1687 if (!pskb_may_pull(skb2, sizeof(*mld))) {
1688 err = -EINVAL;
1689 goto out;
1690 }
1691 mld = (struct mld_msg *)skb_transport_header(skb2);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001692 br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001693 }
1694 }
1695
1696out:
Tomas Winkler9d890812011-01-03 11:26:08 -08001697 kfree_skb(skb2);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001698 return err;
1699}
1700#endif
1701
Herbert Xueb1d1642010-02-27 19:41:45 +00001702int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
Vlad Yasevich06499092013-10-28 15:45:07 -04001703 struct sk_buff *skb, u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +00001704{
YOSHIFUJI Hideaki / 吉藤英明1fafc7a2010-04-25 08:06:40 +00001705 BR_INPUT_SKB_CB(skb)->igmp = 0;
1706 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1707
Herbert Xueb1d1642010-02-27 19:41:45 +00001708 if (br->multicast_disabled)
1709 return 0;
1710
1711 switch (skb->protocol) {
1712 case htons(ETH_P_IP):
Vlad Yasevich06499092013-10-28 15:45:07 -04001713 return br_multicast_ipv4_rcv(br, port, skb, vid);
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001714#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001715 case htons(ETH_P_IPV6):
Vlad Yasevich06499092013-10-28 15:45:07 -04001716 return br_multicast_ipv6_rcv(br, port, skb, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001717#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001718 }
1719
1720 return 0;
1721}
1722
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001723static void br_multicast_query_expired(struct net_bridge *br,
1724 struct bridge_mcast_query *query)
1725{
1726 spin_lock(&br->multicast_lock);
1727 if (query->startup_sent < br->multicast_startup_query_count)
1728 query->startup_sent++;
1729
1730 br_multicast_send_query(br, NULL, query);
1731 spin_unlock(&br->multicast_lock);
1732}
1733
1734static void br_ip4_multicast_query_expired(unsigned long data)
Herbert Xueb1d1642010-02-27 19:41:45 +00001735{
1736 struct net_bridge *br = (void *)data;
1737
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001738 br_multicast_query_expired(br, &br->ip4_query);
Herbert Xueb1d1642010-02-27 19:41:45 +00001739}
1740
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001741#if IS_ENABLED(CONFIG_IPV6)
1742static void br_ip6_multicast_query_expired(unsigned long data)
1743{
1744 struct net_bridge *br = (void *)data;
1745
1746 br_multicast_query_expired(br, &br->ip6_query);
1747}
1748#endif
1749
Herbert Xueb1d1642010-02-27 19:41:45 +00001750void br_multicast_init(struct net_bridge *br)
1751{
1752 br->hash_elasticity = 4;
1753 br->hash_max = 512;
1754
1755 br->multicast_router = 1;
Herbert Xuc5c23262012-04-13 02:37:42 +00001756 br->multicast_querier = 0;
Cong Wang1c8ad5b2013-05-21 21:52:54 +00001757 br->multicast_query_use_ifaddr = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001758 br->multicast_last_member_count = 2;
1759 br->multicast_startup_query_count = 2;
1760
1761 br->multicast_last_member_interval = HZ;
1762 br->multicast_query_response_interval = 10 * HZ;
1763 br->multicast_startup_query_interval = 125 * HZ / 4;
1764 br->multicast_query_interval = 125 * HZ;
1765 br->multicast_querier_interval = 255 * HZ;
1766 br->multicast_membership_interval = 260 * HZ;
1767
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001768 br->ip4_querier.delay_time = 0;
1769#if IS_ENABLED(CONFIG_IPV6)
1770 br->ip6_querier.delay_time = 0;
1771#endif
Linus Lüssingb00589a2013-08-01 01:06:20 +02001772
Herbert Xueb1d1642010-02-27 19:41:45 +00001773 spin_lock_init(&br->multicast_lock);
1774 setup_timer(&br->multicast_router_timer,
1775 br_multicast_local_router_expired, 0);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001776 setup_timer(&br->ip4_querier.timer, br_ip4_multicast_querier_expired,
Herbert Xueb1d1642010-02-27 19:41:45 +00001777 (unsigned long)br);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001778 setup_timer(&br->ip4_query.timer, br_ip4_multicast_query_expired,
1779 (unsigned long)br);
1780#if IS_ENABLED(CONFIG_IPV6)
1781 setup_timer(&br->ip6_querier.timer, br_ip6_multicast_querier_expired,
1782 (unsigned long)br);
1783 setup_timer(&br->ip6_query.timer, br_ip6_multicast_query_expired,
1784 (unsigned long)br);
1785#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001786}
1787
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001788static void __br_multicast_open(struct net_bridge *br,
1789 struct bridge_mcast_query *query)
Herbert Xueb1d1642010-02-27 19:41:45 +00001790{
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001791 query->startup_sent = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001792
1793 if (br->multicast_disabled)
1794 return;
1795
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001796 mod_timer(&query->timer, jiffies);
1797}
1798
1799void br_multicast_open(struct net_bridge *br)
1800{
1801 __br_multicast_open(br, &br->ip4_query);
1802#if IS_ENABLED(CONFIG_IPV6)
1803 __br_multicast_open(br, &br->ip6_query);
1804#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001805}
1806
1807void br_multicast_stop(struct net_bridge *br)
1808{
1809 struct net_bridge_mdb_htable *mdb;
1810 struct net_bridge_mdb_entry *mp;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001811 struct hlist_node *n;
Herbert Xueb1d1642010-02-27 19:41:45 +00001812 u32 ver;
1813 int i;
1814
1815 del_timer_sync(&br->multicast_router_timer);
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001816 del_timer_sync(&br->ip4_querier.timer);
1817 del_timer_sync(&br->ip4_query.timer);
1818#if IS_ENABLED(CONFIG_IPV6)
1819 del_timer_sync(&br->ip6_querier.timer);
1820 del_timer_sync(&br->ip6_query.timer);
1821#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001822
1823 spin_lock_bh(&br->multicast_lock);
Eric Dumazete8051682010-11-15 06:38:10 +00001824 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001825 if (!mdb)
1826 goto out;
1827
1828 br->mdb = NULL;
1829
1830 ver = mdb->ver;
1831 for (i = 0; i < mdb->max; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001832 hlist_for_each_entry_safe(mp, n, &mdb->mhash[i],
Herbert Xueb1d1642010-02-27 19:41:45 +00001833 hlist[ver]) {
1834 del_timer(&mp->timer);
Herbert Xueb1d1642010-02-27 19:41:45 +00001835 call_rcu_bh(&mp->rcu, br_multicast_free_group);
1836 }
1837 }
1838
1839 if (mdb->old) {
1840 spin_unlock_bh(&br->multicast_lock);
Herbert Xu10cc2b52010-03-05 21:03:35 +00001841 rcu_barrier_bh();
Herbert Xueb1d1642010-02-27 19:41:45 +00001842 spin_lock_bh(&br->multicast_lock);
1843 WARN_ON(mdb->old);
1844 }
1845
1846 mdb->old = mdb;
1847 call_rcu_bh(&mdb->rcu, br_mdb_free);
1848
1849out:
1850 spin_unlock_bh(&br->multicast_lock);
1851}
Herbert Xu0909e112010-02-27 19:41:49 +00001852
1853int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1854{
1855 int err = -ENOENT;
1856
1857 spin_lock_bh(&br->multicast_lock);
1858 if (!netif_running(br->dev))
1859 goto unlock;
1860
1861 switch (val) {
1862 case 0:
1863 case 2:
1864 del_timer(&br->multicast_router_timer);
1865 /* fall through */
1866 case 1:
1867 br->multicast_router = val;
1868 err = 0;
1869 break;
1870
1871 default:
1872 err = -EINVAL;
1873 break;
1874 }
1875
1876unlock:
1877 spin_unlock_bh(&br->multicast_lock);
1878
1879 return err;
1880}
1881
1882int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1883{
1884 struct net_bridge *br = p->br;
1885 int err = -ENOENT;
1886
1887 spin_lock(&br->multicast_lock);
1888 if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED)
1889 goto unlock;
1890
1891 switch (val) {
1892 case 0:
1893 case 1:
1894 case 2:
1895 p->multicast_router = val;
1896 err = 0;
1897
1898 if (val < 2 && !hlist_unhashed(&p->rlist))
1899 hlist_del_init_rcu(&p->rlist);
1900
1901 if (val == 1)
1902 break;
1903
1904 del_timer(&p->multicast_router_timer);
1905
1906 if (val == 0)
1907 break;
1908
1909 br_multicast_add_router(br, p);
1910 break;
1911
1912 default:
1913 err = -EINVAL;
1914 break;
1915 }
1916
1917unlock:
1918 spin_unlock(&br->multicast_lock);
1919
1920 return err;
1921}
Herbert Xu561f1102010-02-27 19:41:50 +00001922
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001923static void br_multicast_start_querier(struct net_bridge *br,
1924 struct bridge_mcast_query *query)
Herbert Xu561f1102010-02-27 19:41:50 +00001925{
1926 struct net_bridge_port *port;
Herbert Xu74857212012-04-13 02:37:42 +00001927
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001928 __br_multicast_open(br, query);
Herbert Xu74857212012-04-13 02:37:42 +00001929
1930 list_for_each_entry(port, &br->port_list, list) {
1931 if (port->state == BR_STATE_DISABLED ||
1932 port->state == BR_STATE_BLOCKING)
1933 continue;
1934
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001935 if (query == &br->ip4_query)
1936 br_multicast_enable(&port->ip4_query);
1937#if IS_ENABLED(CONFIG_IPV6)
1938 else
1939 br_multicast_enable(&port->ip6_query);
1940#endif
Herbert Xu74857212012-04-13 02:37:42 +00001941 }
1942}
1943
1944int br_multicast_toggle(struct net_bridge *br, unsigned long val)
1945{
Herbert Xu3a7fda02010-07-29 00:45:30 +00001946 int err = 0;
Eric Dumazete8051682010-11-15 06:38:10 +00001947 struct net_bridge_mdb_htable *mdb;
Herbert Xu561f1102010-02-27 19:41:50 +00001948
Andrey Vaginef5e0d82011-11-10 05:48:03 +00001949 spin_lock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00001950 if (br->multicast_disabled == !val)
1951 goto unlock;
1952
1953 br->multicast_disabled = !val;
1954 if (br->multicast_disabled)
1955 goto unlock;
1956
Herbert Xu3a7fda02010-07-29 00:45:30 +00001957 if (!netif_running(br->dev))
1958 goto unlock;
1959
Eric Dumazete8051682010-11-15 06:38:10 +00001960 mdb = mlock_dereference(br->mdb, br);
1961 if (mdb) {
1962 if (mdb->old) {
Herbert Xu561f1102010-02-27 19:41:50 +00001963 err = -EEXIST;
1964rollback:
1965 br->multicast_disabled = !!val;
1966 goto unlock;
1967 }
1968
Eric Dumazete8051682010-11-15 06:38:10 +00001969 err = br_mdb_rehash(&br->mdb, mdb->max,
Herbert Xu561f1102010-02-27 19:41:50 +00001970 br->hash_elasticity);
1971 if (err)
1972 goto rollback;
1973 }
1974
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02001975 br_multicast_start_querier(br, &br->ip4_query);
1976#if IS_ENABLED(CONFIG_IPV6)
1977 br_multicast_start_querier(br, &br->ip6_query);
1978#endif
Herbert Xu561f1102010-02-27 19:41:50 +00001979
1980unlock:
Andrey Vaginef5e0d82011-11-10 05:48:03 +00001981 spin_unlock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00001982
1983 return err;
1984}
Herbert Xub1951672010-02-27 19:41:51 +00001985
Herbert Xuc5c23262012-04-13 02:37:42 +00001986int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
1987{
Linus Lüssingb00589a2013-08-01 01:06:20 +02001988 unsigned long max_delay;
1989
Herbert Xuc5c23262012-04-13 02:37:42 +00001990 val = !!val;
1991
1992 spin_lock_bh(&br->multicast_lock);
1993 if (br->multicast_querier == val)
1994 goto unlock;
1995
1996 br->multicast_querier = val;
Linus Lüssingb00589a2013-08-01 01:06:20 +02001997 if (!val)
1998 goto unlock;
1999
2000 max_delay = br->multicast_query_response_interval;
Linus Lüssingb00589a2013-08-01 01:06:20 +02002001
Linus Lüssingcc0fdd82013-08-30 17:28:17 +02002002 if (!timer_pending(&br->ip4_querier.timer))
2003 br->ip4_querier.delay_time = jiffies + max_delay;
2004
2005 br_multicast_start_querier(br, &br->ip4_query);
2006
2007#if IS_ENABLED(CONFIG_IPV6)
2008 if (!timer_pending(&br->ip6_querier.timer))
2009 br->ip6_querier.delay_time = jiffies + max_delay;
2010
2011 br_multicast_start_querier(br, &br->ip6_query);
2012#endif
Herbert Xuc5c23262012-04-13 02:37:42 +00002013
2014unlock:
2015 spin_unlock_bh(&br->multicast_lock);
2016
2017 return 0;
2018}
2019
Herbert Xub1951672010-02-27 19:41:51 +00002020int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
2021{
2022 int err = -ENOENT;
2023 u32 old;
Eric Dumazete8051682010-11-15 06:38:10 +00002024 struct net_bridge_mdb_htable *mdb;
Herbert Xub1951672010-02-27 19:41:51 +00002025
Curt Brunefe0d6922014-01-06 11:00:32 -08002026 spin_lock_bh(&br->multicast_lock);
Herbert Xub1951672010-02-27 19:41:51 +00002027 if (!netif_running(br->dev))
2028 goto unlock;
2029
2030 err = -EINVAL;
2031 if (!is_power_of_2(val))
2032 goto unlock;
Eric Dumazete8051682010-11-15 06:38:10 +00002033
2034 mdb = mlock_dereference(br->mdb, br);
2035 if (mdb && val < mdb->size)
Herbert Xub1951672010-02-27 19:41:51 +00002036 goto unlock;
2037
2038 err = 0;
2039
2040 old = br->hash_max;
2041 br->hash_max = val;
2042
Eric Dumazete8051682010-11-15 06:38:10 +00002043 if (mdb) {
2044 if (mdb->old) {
Herbert Xub1951672010-02-27 19:41:51 +00002045 err = -EEXIST;
2046rollback:
2047 br->hash_max = old;
2048 goto unlock;
2049 }
2050
2051 err = br_mdb_rehash(&br->mdb, br->hash_max,
2052 br->hash_elasticity);
2053 if (err)
2054 goto rollback;
2055 }
2056
2057unlock:
Curt Brunefe0d6922014-01-06 11:00:32 -08002058 spin_unlock_bh(&br->multicast_lock);
Herbert Xub1951672010-02-27 19:41:51 +00002059
2060 return err;
2061}