blob: d6448e35e02712682953d3d617aeac3b5f40737e [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>
26#include <net/ip.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000027#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090028#include <net/ipv6.h>
29#include <net/mld.h>
David S. Millerd4c4f072010-04-27 10:16:54 -070030#include <net/ip6_checksum.h>
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090031#endif
Herbert Xueb1d1642010-02-27 19:41:45 +000032
33#include "br_private.h"
34
Herbert Xuc83b8fa2012-04-13 02:37:42 +000035static void br_multicast_start_querier(struct net_bridge *br);
Cong Wang2ce297f2012-12-10 02:15:35 +000036unsigned int br_mdb_rehash_seq;
Herbert Xuc83b8fa2012-04-13 02:37:42 +000037
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090038static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
39{
40 if (a->proto != b->proto)
41 return 0;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000042 if (a->vid != b->vid)
43 return 0;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090044 switch (a->proto) {
45 case htons(ETH_P_IP):
46 return a->u.ip4 == b->u.ip4;
Eric Dumazetdfd56b82011-12-10 09:48:31 +000047#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090048 case htons(ETH_P_IPV6):
49 return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
50#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090051 }
52 return 0;
53}
54
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000055static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip,
56 __u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +000057{
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000058 return jhash_2words((__force u32)ip, vid, mdb->secret) & (mdb->max - 1);
Herbert Xueb1d1642010-02-27 19:41:45 +000059}
60
Eric Dumazetdfd56b82011-12-10 09:48:31 +000061#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090062static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000063 const struct in6_addr *ip,
64 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090065{
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000066 return jhash_2words(ipv6_addr_hash(ip), vid,
67 mdb->secret) & (mdb->max - 1);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090068}
69#endif
70
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090071static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb,
72 struct br_ip *ip)
73{
74 switch (ip->proto) {
75 case htons(ETH_P_IP):
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000076 return __br_ip4_hash(mdb, ip->u.ip4, ip->vid);
Eric Dumazetdfd56b82011-12-10 09:48:31 +000077#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090078 case htons(ETH_P_IPV6):
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000079 return __br_ip6_hash(mdb, &ip->u.ip6, ip->vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090080#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090081 }
82 return 0;
Herbert Xueb1d1642010-02-27 19:41:45 +000083}
84
85static struct net_bridge_mdb_entry *__br_mdb_ip_get(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090086 struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +000087{
88 struct net_bridge_mdb_entry *mp;
Herbert Xueb1d1642010-02-27 19:41:45 +000089
Sasha Levinb67bfe02013-02-27 17:06:00 -080090 hlist_for_each_entry_rcu(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090091 if (br_ip_equal(&mp->addr, dst))
Herbert Xueb1d1642010-02-27 19:41:45 +000092 return mp;
93 }
94
95 return NULL;
96}
97
Cong Wangcfd56752012-12-11 22:23:08 +000098struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge_mdb_htable *mdb,
99 struct br_ip *dst)
Herbert Xu7f285fa2010-07-05 14:50:08 +0000100{
101 if (!mdb)
102 return NULL;
103
104 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
105}
106
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900107static struct net_bridge_mdb_entry *br_mdb_ip4_get(
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000108 struct net_bridge_mdb_htable *mdb, __be32 dst, __u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000109{
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900110 struct br_ip br_dst;
Herbert Xu0821ec52010-03-15 20:38:25 -0700111
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900112 br_dst.u.ip4 = dst;
113 br_dst.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000114 br_dst.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900115
Herbert Xu7f285fa2010-07-05 14:50:08 +0000116 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900117}
118
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000119#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900120static struct net_bridge_mdb_entry *br_mdb_ip6_get(
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000121 struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst,
122 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900123{
124 struct br_ip br_dst;
125
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000126 br_dst.u.ip6 = *dst;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900127 br_dst.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000128 br_dst.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900129
Herbert Xu7f285fa2010-07-05 14:50:08 +0000130 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900131}
132#endif
133
Herbert Xueb1d1642010-02-27 19:41:45 +0000134struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
Cong Wangfbca58a2013-03-07 03:05:33 +0000135 struct sk_buff *skb, u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000136{
Eric Dumazete8051682010-11-15 06:38:10 +0000137 struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900138 struct br_ip ip;
139
Herbert Xu7f285fa2010-07-05 14:50:08 +0000140 if (br->multicast_disabled)
Herbert Xueb1d1642010-02-27 19:41:45 +0000141 return NULL;
142
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900143 if (BR_INPUT_SKB_CB(skb)->igmp)
144 return NULL;
145
146 ip.proto = skb->protocol;
Cong Wangfbca58a2013-03-07 03:05:33 +0000147 ip.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900148
Herbert Xueb1d1642010-02-27 19:41:45 +0000149 switch (skb->protocol) {
150 case htons(ETH_P_IP):
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900151 ip.u.ip4 = ip_hdr(skb)->daddr;
152 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000153#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900154 case htons(ETH_P_IPV6):
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000155 ip.u.ip6 = ipv6_hdr(skb)->daddr;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900156 break;
157#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900158 default:
159 return NULL;
Herbert Xueb1d1642010-02-27 19:41:45 +0000160 }
161
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900162 return br_mdb_ip_get(mdb, &ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000163}
164
165static void br_mdb_free(struct rcu_head *head)
166{
167 struct net_bridge_mdb_htable *mdb =
168 container_of(head, struct net_bridge_mdb_htable, rcu);
169 struct net_bridge_mdb_htable *old = mdb->old;
170
171 mdb->old = NULL;
172 kfree(old->mhash);
173 kfree(old);
174}
175
176static int br_mdb_copy(struct net_bridge_mdb_htable *new,
177 struct net_bridge_mdb_htable *old,
178 int elasticity)
179{
180 struct net_bridge_mdb_entry *mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000181 int maxlen;
182 int len;
183 int i;
184
185 for (i = 0; i < old->max; i++)
Sasha Levinb67bfe02013-02-27 17:06:00 -0800186 hlist_for_each_entry(mp, &old->mhash[i], hlist[old->ver])
Herbert Xueb1d1642010-02-27 19:41:45 +0000187 hlist_add_head(&mp->hlist[new->ver],
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900188 &new->mhash[br_ip_hash(new, &mp->addr)]);
Herbert Xueb1d1642010-02-27 19:41:45 +0000189
190 if (!elasticity)
191 return 0;
192
193 maxlen = 0;
194 for (i = 0; i < new->max; i++) {
195 len = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800196 hlist_for_each_entry(mp, &new->mhash[i], hlist[new->ver])
Herbert Xueb1d1642010-02-27 19:41:45 +0000197 len++;
198 if (len > maxlen)
199 maxlen = len;
200 }
201
202 return maxlen > elasticity ? -EINVAL : 0;
203}
204
Cong Wangcfd56752012-12-11 22:23:08 +0000205void br_multicast_free_pg(struct rcu_head *head)
Herbert Xueb1d1642010-02-27 19:41:45 +0000206{
207 struct net_bridge_port_group *p =
208 container_of(head, struct net_bridge_port_group, rcu);
209
210 kfree(p);
211}
212
213static void br_multicast_free_group(struct rcu_head *head)
214{
215 struct net_bridge_mdb_entry *mp =
216 container_of(head, struct net_bridge_mdb_entry, rcu);
217
218 kfree(mp);
219}
220
221static void br_multicast_group_expired(unsigned long data)
222{
223 struct net_bridge_mdb_entry *mp = (void *)data;
224 struct net_bridge *br = mp->br;
225 struct net_bridge_mdb_htable *mdb;
226
227 spin_lock(&br->multicast_lock);
228 if (!netif_running(br->dev) || timer_pending(&mp->timer))
229 goto out;
230
Herbert Xu8a870172011-02-12 01:05:42 -0800231 mp->mglist = false;
Herbert Xueb1d1642010-02-27 19:41:45 +0000232
233 if (mp->ports)
234 goto out;
235
Eric Dumazete8051682010-11-15 06:38:10 +0000236 mdb = mlock_dereference(br->mdb, br);
237
Herbert Xueb1d1642010-02-27 19:41:45 +0000238 hlist_del_rcu(&mp->hlist[mdb->ver]);
239 mdb->size--;
240
Herbert Xueb1d1642010-02-27 19:41:45 +0000241 call_rcu_bh(&mp->rcu, br_multicast_free_group);
242
243out:
244 spin_unlock(&br->multicast_lock);
245}
246
247static void br_multicast_del_pg(struct net_bridge *br,
248 struct net_bridge_port_group *pg)
249{
Eric Dumazete8051682010-11-15 06:38:10 +0000250 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000251 struct net_bridge_mdb_entry *mp;
252 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +0000253 struct net_bridge_port_group __rcu **pp;
254
255 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +0000256
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900257 mp = br_mdb_ip_get(mdb, &pg->addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000258 if (WARN_ON(!mp))
259 return;
260
Eric Dumazete8051682010-11-15 06:38:10 +0000261 for (pp = &mp->ports;
262 (p = mlock_dereference(*pp, br)) != NULL;
263 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000264 if (p != pg)
265 continue;
266
stephen hemminger83f6a742010-04-27 15:01:06 +0000267 rcu_assign_pointer(*pp, p->next);
Herbert Xueb1d1642010-02-27 19:41:45 +0000268 hlist_del_init(&p->mglist);
269 del_timer(&p->timer);
Herbert Xueb1d1642010-02-27 19:41:45 +0000270 call_rcu_bh(&p->rcu, br_multicast_free_pg);
271
Herbert Xu8a870172011-02-12 01:05:42 -0800272 if (!mp->ports && !mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +0000273 netif_running(br->dev))
274 mod_timer(&mp->timer, jiffies);
275
276 return;
277 }
278
279 WARN_ON(1);
280}
281
282static void br_multicast_port_group_expired(unsigned long data)
283{
284 struct net_bridge_port_group *pg = (void *)data;
285 struct net_bridge *br = pg->port->br;
286
287 spin_lock(&br->multicast_lock);
288 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
Amerigo Wangccb1c312012-12-14 22:09:51 +0000289 hlist_unhashed(&pg->mglist) || pg->state & MDB_PERMANENT)
Herbert Xueb1d1642010-02-27 19:41:45 +0000290 goto out;
291
292 br_multicast_del_pg(br, pg);
293
294out:
295 spin_unlock(&br->multicast_lock);
296}
297
Eric Dumazete8051682010-11-15 06:38:10 +0000298static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max,
Herbert Xueb1d1642010-02-27 19:41:45 +0000299 int elasticity)
300{
Eric Dumazete8051682010-11-15 06:38:10 +0000301 struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000302 struct net_bridge_mdb_htable *mdb;
303 int err;
304
305 mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
306 if (!mdb)
307 return -ENOMEM;
308
309 mdb->max = max;
310 mdb->old = old;
311
312 mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
313 if (!mdb->mhash) {
314 kfree(mdb);
315 return -ENOMEM;
316 }
317
318 mdb->size = old ? old->size : 0;
319 mdb->ver = old ? old->ver ^ 1 : 0;
320
321 if (!old || elasticity)
322 get_random_bytes(&mdb->secret, sizeof(mdb->secret));
323 else
324 mdb->secret = old->secret;
325
326 if (!old)
327 goto out;
328
329 err = br_mdb_copy(mdb, old, elasticity);
330 if (err) {
331 kfree(mdb->mhash);
332 kfree(mdb);
333 return err;
334 }
335
Cong Wang2ce297f2012-12-10 02:15:35 +0000336 br_mdb_rehash_seq++;
Herbert Xueb1d1642010-02-27 19:41:45 +0000337 call_rcu_bh(&mdb->rcu, br_mdb_free);
338
339out:
340 rcu_assign_pointer(*mdbp, mdb);
341
342 return 0;
343}
344
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900345static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
346 __be32 group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000347{
348 struct sk_buff *skb;
349 struct igmphdr *ih;
350 struct ethhdr *eth;
351 struct iphdr *iph;
352
353 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
354 sizeof(*ih) + 4);
355 if (!skb)
356 goto out;
357
358 skb->protocol = htons(ETH_P_IP);
359
360 skb_reset_mac_header(skb);
361 eth = eth_hdr(skb);
362
363 memcpy(eth->h_source, br->dev->dev_addr, 6);
364 eth->h_dest[0] = 1;
365 eth->h_dest[1] = 0;
366 eth->h_dest[2] = 0x5e;
367 eth->h_dest[3] = 0;
368 eth->h_dest[4] = 0;
369 eth->h_dest[5] = 1;
370 eth->h_proto = htons(ETH_P_IP);
371 skb_put(skb, sizeof(*eth));
372
373 skb_set_network_header(skb, skb->len);
374 iph = ip_hdr(skb);
375
376 iph->version = 4;
377 iph->ihl = 6;
378 iph->tos = 0xc0;
379 iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4);
380 iph->id = 0;
381 iph->frag_off = htons(IP_DF);
382 iph->ttl = 1;
383 iph->protocol = IPPROTO_IGMP;
384 iph->saddr = 0;
385 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
386 ((u8 *)&iph[1])[0] = IPOPT_RA;
387 ((u8 *)&iph[1])[1] = 4;
388 ((u8 *)&iph[1])[2] = 0;
389 ((u8 *)&iph[1])[3] = 0;
390 ip_send_check(iph);
391 skb_put(skb, 24);
392
393 skb_set_transport_header(skb, skb->len);
394 ih = igmp_hdr(skb);
395 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
396 ih->code = (group ? br->multicast_last_member_interval :
397 br->multicast_query_response_interval) /
398 (HZ / IGMP_TIMER_SCALE);
399 ih->group = group;
400 ih->csum = 0;
401 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
402 skb_put(skb, sizeof(*ih));
403
404 __skb_pull(skb, sizeof(*eth));
405
406out:
407 return skb;
408}
409
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000410#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900411static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000412 const struct in6_addr *group)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900413{
414 struct sk_buff *skb;
415 struct ipv6hdr *ip6h;
416 struct mld_msg *mldq;
417 struct ethhdr *eth;
418 u8 *hopopt;
419 unsigned long interval;
420
421 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
422 8 + sizeof(*mldq));
423 if (!skb)
424 goto out;
425
426 skb->protocol = htons(ETH_P_IPV6);
427
428 /* Ethernet header */
429 skb_reset_mac_header(skb);
430 eth = eth_hdr(skb);
431
432 memcpy(eth->h_source, br->dev->dev_addr, 6);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900433 eth->h_proto = htons(ETH_P_IPV6);
434 skb_put(skb, sizeof(*eth));
435
436 /* IPv6 header + HbH option */
437 skb_set_network_header(skb, skb->len);
438 ip6h = ipv6_hdr(skb);
439
440 *(__force __be32 *)ip6h = htonl(0x60000000);
David Stevens76d6615862010-12-14 08:42:16 +0000441 ip6h->payload_len = htons(8 + sizeof(*mldq));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900442 ip6h->nexthdr = IPPROTO_HOPOPTS;
443 ip6h->hop_limit = 1;
Linus Lüssinga7bff752011-03-22 11:40:32 +0000444 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
Ulrich Weberd1d81d42012-03-05 04:52:44 +0000445 if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
446 &ip6h->saddr)) {
447 kfree_skb(skb);
448 return NULL;
449 }
Linus Lüssing36cff5a2011-02-17 08:17:51 +0000450 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900451
452 hopopt = (u8 *)(ip6h + 1);
453 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
454 hopopt[1] = 0; /* length of HbH */
455 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
456 hopopt[3] = 2; /* Length of RA Option */
457 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
458 hopopt[5] = 0;
Eldad Zack1de5a712012-05-17 06:00:25 +0000459 hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */
460 hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900461
462 skb_put(skb, sizeof(*ip6h) + 8);
463
464 /* ICMPv6 */
465 skb_set_transport_header(skb, skb->len);
466 mldq = (struct mld_msg *) icmp6_hdr(skb);
467
Linus Lüssing32de8682013-06-16 23:20:34 +0200468 interval = ipv6_addr_any(group) ?
469 br->multicast_query_response_interval :
470 br->multicast_last_member_interval;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900471
472 mldq->mld_type = ICMPV6_MGM_QUERY;
473 mldq->mld_code = 0;
474 mldq->mld_cksum = 0;
475 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
476 mldq->mld_reserved = 0;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000477 mldq->mld_mca = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900478
479 /* checksum */
480 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
481 sizeof(*mldq), IPPROTO_ICMPV6,
482 csum_partial(mldq,
483 sizeof(*mldq), 0));
484 skb_put(skb, sizeof(*mldq));
485
486 __skb_pull(skb, sizeof(*eth));
487
488out:
489 return skb;
490}
491#endif
492
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900493static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
494 struct br_ip *addr)
495{
496 switch (addr->proto) {
497 case htons(ETH_P_IP):
498 return br_ip4_multicast_alloc_query(br, addr->u.ip4);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000499#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900500 case htons(ETH_P_IPV6):
501 return br_ip6_multicast_alloc_query(br, &addr->u.ip6);
502#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900503 }
504 return NULL;
505}
506
Herbert Xueb1d1642010-02-27 19:41:45 +0000507static struct net_bridge_mdb_entry *br_multicast_get_group(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900508 struct net_bridge *br, struct net_bridge_port *port,
509 struct br_ip *group, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +0000510{
Eric Dumazete8051682010-11-15 06:38:10 +0000511 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000512 struct net_bridge_mdb_entry *mp;
Eric Dumazet95c96172012-04-15 05:58:06 +0000513 unsigned int count = 0;
514 unsigned int max;
Herbert Xueb1d1642010-02-27 19:41:45 +0000515 int elasticity;
516 int err;
517
Eric Dumazete8051682010-11-15 06:38:10 +0000518 mdb = rcu_dereference_protected(br->mdb, 1);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800519 hlist_for_each_entry(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000520 count++;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900521 if (unlikely(br_ip_equal(group, &mp->addr)))
Herbert Xueb1d1642010-02-27 19:41:45 +0000522 return mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000523 }
524
525 elasticity = 0;
526 max = mdb->max;
527
528 if (unlikely(count > br->hash_elasticity && count)) {
529 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000530 br_info(br, "Multicast hash table "
531 "chain limit reached: %s\n",
532 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000533
534 elasticity = br->hash_elasticity;
535 }
536
537 if (mdb->size >= max) {
538 max *= 2;
Thomas Graf036be6d2012-07-10 22:29:19 +0000539 if (unlikely(max > br->hash_max)) {
540 br_warn(br, "Multicast hash table maximum of %d "
541 "reached, disabling snooping: %s\n",
542 br->hash_max,
543 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000544 err = -E2BIG;
545disable:
546 br->multicast_disabled = 1;
547 goto err;
548 }
549 }
550
551 if (max > mdb->max || elasticity) {
552 if (mdb->old) {
553 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000554 br_info(br, "Multicast hash table "
555 "on fire: %s\n",
556 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000557 err = -EEXIST;
558 goto err;
559 }
560
561 err = br_mdb_rehash(&br->mdb, max, elasticity);
562 if (err) {
stephen hemminger28a16c92010-05-10 09:31:09 +0000563 br_warn(br, "Cannot rehash multicast "
564 "hash table, disabling snooping: %s, %d, %d\n",
565 port ? port->dev->name : br->dev->name,
566 mdb->size, err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000567 goto disable;
568 }
569
570 err = -EAGAIN;
571 goto err;
572 }
573
574 return NULL;
575
576err:
577 mp = ERR_PTR(err);
578 return mp;
579}
580
Cong Wangcfd56752012-12-11 22:23:08 +0000581struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
582 struct net_bridge_port *port, struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000583{
Eric Dumazete8051682010-11-15 06:38:10 +0000584 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000585 struct net_bridge_mdb_entry *mp;
586 int hash;
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000587 int err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000588
Eric Dumazete8051682010-11-15 06:38:10 +0000589 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000590 if (!mdb) {
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000591 err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0);
592 if (err)
593 return ERR_PTR(err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000594 goto rehash;
595 }
596
597 hash = br_ip_hash(mdb, group);
598 mp = br_multicast_get_group(br, port, group, hash);
599 switch (PTR_ERR(mp)) {
600 case 0:
601 break;
602
603 case -EAGAIN:
604rehash:
Eric Dumazete8051682010-11-15 06:38:10 +0000605 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000606 hash = br_ip_hash(mdb, group);
607 break;
608
609 default:
610 goto out;
611 }
612
613 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
614 if (unlikely(!mp))
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000615 return ERR_PTR(-ENOMEM);
Herbert Xueb1d1642010-02-27 19:41:45 +0000616
617 mp->br = br;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900618 mp->addr = *group;
Herbert Xueb1d1642010-02-27 19:41:45 +0000619 setup_timer(&mp->timer, br_multicast_group_expired,
620 (unsigned long)mp);
Herbert Xueb1d1642010-02-27 19:41:45 +0000621
622 hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
623 mdb->size++;
624
625out:
626 return mp;
627}
628
Cong Wangcfd56752012-12-11 22:23:08 +0000629struct net_bridge_port_group *br_multicast_new_port_group(
630 struct net_bridge_port *port,
631 struct br_ip *group,
Amerigo Wangccb1c312012-12-14 22:09:51 +0000632 struct net_bridge_port_group __rcu *next,
633 unsigned char state)
Cong Wangcfd56752012-12-11 22:23:08 +0000634{
635 struct net_bridge_port_group *p;
636
637 p = kzalloc(sizeof(*p), GFP_ATOMIC);
638 if (unlikely(!p))
639 return NULL;
640
641 p->addr = *group;
642 p->port = port;
Amerigo Wangccb1c312012-12-14 22:09:51 +0000643 p->state = state;
stephen hemmingereca2a432012-12-13 06:51:28 +0000644 rcu_assign_pointer(p->next, next);
Cong Wangcfd56752012-12-11 22:23:08 +0000645 hlist_add_head(&p->mglist, &port->mglist);
646 setup_timer(&p->timer, br_multicast_port_group_expired,
647 (unsigned long)p);
648 return p;
649}
650
Herbert Xueb1d1642010-02-27 19:41:45 +0000651static int br_multicast_add_group(struct net_bridge *br,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900652 struct net_bridge_port *port,
653 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000654{
655 struct net_bridge_mdb_entry *mp;
656 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +0000657 struct net_bridge_port_group __rcu **pp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000658 unsigned long now = jiffies;
659 int err;
660
Herbert Xueb1d1642010-02-27 19:41:45 +0000661 spin_lock(&br->multicast_lock);
662 if (!netif_running(br->dev) ||
663 (port && port->state == BR_STATE_DISABLED))
664 goto out;
665
666 mp = br_multicast_new_group(br, port, group);
667 err = PTR_ERR(mp);
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000668 if (IS_ERR(mp))
Herbert Xueb1d1642010-02-27 19:41:45 +0000669 goto err;
670
671 if (!port) {
Herbert Xu8a870172011-02-12 01:05:42 -0800672 mp->mglist = true;
Herbert Xueb1d1642010-02-27 19:41:45 +0000673 mod_timer(&mp->timer, now + br->multicast_membership_interval);
674 goto out;
675 }
676
Eric Dumazete8051682010-11-15 06:38:10 +0000677 for (pp = &mp->ports;
678 (p = mlock_dereference(*pp, br)) != NULL;
679 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000680 if (p->port == port)
681 goto found;
682 if ((unsigned long)p->port < (unsigned long)port)
683 break;
684 }
685
Amerigo Wangccb1c312012-12-14 22:09:51 +0000686 p = br_multicast_new_port_group(port, group, *pp, MDB_TEMPORARY);
Herbert Xueb1d1642010-02-27 19:41:45 +0000687 if (unlikely(!p))
688 goto err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000689 rcu_assign_pointer(*pp, p);
Cong Wang37a393b2012-12-11 22:23:07 +0000690 br_mdb_notify(br->dev, port, group, RTM_NEWMDB);
Herbert Xueb1d1642010-02-27 19:41:45 +0000691
692found:
693 mod_timer(&p->timer, now + br->multicast_membership_interval);
694out:
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
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000759static void br_multicast_querier_expired(unsigned long data)
760{
Herbert Xubb63f1f2012-04-30 00:22:56 +0000761 struct net_bridge *br = (void *)data;
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000762
763 spin_lock(&br->multicast_lock);
764 if (!netif_running(br->dev) || br->multicast_disabled)
765 goto out;
766
767 br_multicast_start_querier(br);
768
769out:
770 spin_unlock(&br->multicast_lock);
771}
772
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900773static void __br_multicast_send_query(struct net_bridge *br,
774 struct net_bridge_port *port,
775 struct br_ip *ip)
Herbert Xueb1d1642010-02-27 19:41:45 +0000776{
Herbert Xueb1d1642010-02-27 19:41:45 +0000777 struct sk_buff *skb;
778
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900779 skb = br_multicast_alloc_query(br, ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000780 if (!skb)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900781 return;
Herbert Xueb1d1642010-02-27 19:41:45 +0000782
783 if (port) {
784 __skb_push(skb, sizeof(struct ethhdr));
785 skb->dev = port->dev;
Jan Engelhardt713aefa2010-03-23 04:07:21 +0100786 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
Herbert Xueb1d1642010-02-27 19:41:45 +0000787 dev_queue_xmit);
788 } else
789 netif_rx(skb);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900790}
Herbert Xueb1d1642010-02-27 19:41:45 +0000791
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900792static void br_multicast_send_query(struct net_bridge *br,
793 struct net_bridge_port *port, u32 sent)
794{
795 unsigned long time;
796 struct br_ip br_group;
797
798 if (!netif_running(br->dev) || br->multicast_disabled ||
Herbert Xuc5c23262012-04-13 02:37:42 +0000799 !br->multicast_querier ||
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900800 timer_pending(&br->multicast_querier_timer))
801 return;
802
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900803 memset(&br_group.u, 0, sizeof(br_group.u));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900804
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900805 br_group.proto = htons(ETH_P_IP);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900806 __br_multicast_send_query(br, port, &br_group);
807
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000808#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900809 br_group.proto = htons(ETH_P_IPV6);
810 __br_multicast_send_query(br, port, &br_group);
811#endif
812
Herbert Xueb1d1642010-02-27 19:41:45 +0000813 time = jiffies;
814 time += sent < br->multicast_startup_query_count ?
815 br->multicast_startup_query_interval :
816 br->multicast_query_interval;
817 mod_timer(port ? &port->multicast_query_timer :
818 &br->multicast_query_timer, time);
819}
820
821static void br_multicast_port_query_expired(unsigned long data)
822{
823 struct net_bridge_port *port = (void *)data;
824 struct net_bridge *br = port->br;
825
826 spin_lock(&br->multicast_lock);
Dan Carpenter02a780c2010-03-06 01:14:09 +0000827 if (port->state == BR_STATE_DISABLED ||
828 port->state == BR_STATE_BLOCKING)
Herbert Xueb1d1642010-02-27 19:41:45 +0000829 goto out;
830
831 if (port->multicast_startup_queries_sent <
832 br->multicast_startup_query_count)
833 port->multicast_startup_queries_sent++;
834
835 br_multicast_send_query(port->br, port,
836 port->multicast_startup_queries_sent);
837
838out:
839 spin_unlock(&br->multicast_lock);
840}
841
842void br_multicast_add_port(struct net_bridge_port *port)
843{
844 port->multicast_router = 1;
845
846 setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
847 (unsigned long)port);
848 setup_timer(&port->multicast_query_timer,
849 br_multicast_port_query_expired, (unsigned long)port);
850}
851
852void br_multicast_del_port(struct net_bridge_port *port)
853{
854 del_timer_sync(&port->multicast_router_timer);
855}
856
Herbert Xu561f1102010-02-27 19:41:50 +0000857static void __br_multicast_enable_port(struct net_bridge_port *port)
858{
859 port->multicast_startup_queries_sent = 0;
860
861 if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 ||
862 del_timer(&port->multicast_query_timer))
863 mod_timer(&port->multicast_query_timer, jiffies);
864}
865
Herbert Xueb1d1642010-02-27 19:41:45 +0000866void br_multicast_enable_port(struct net_bridge_port *port)
867{
868 struct net_bridge *br = port->br;
869
870 spin_lock(&br->multicast_lock);
871 if (br->multicast_disabled || !netif_running(br->dev))
872 goto out;
873
Herbert Xu561f1102010-02-27 19:41:50 +0000874 __br_multicast_enable_port(port);
Herbert Xueb1d1642010-02-27 19:41:45 +0000875
876out:
877 spin_unlock(&br->multicast_lock);
878}
879
880void br_multicast_disable_port(struct net_bridge_port *port)
881{
882 struct net_bridge *br = port->br;
883 struct net_bridge_port_group *pg;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800884 struct hlist_node *n;
Herbert Xueb1d1642010-02-27 19:41:45 +0000885
886 spin_lock(&br->multicast_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800887 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
Herbert Xueb1d1642010-02-27 19:41:45 +0000888 br_multicast_del_pg(br, pg);
889
890 if (!hlist_unhashed(&port->rlist))
891 hlist_del_init_rcu(&port->rlist);
892 del_timer(&port->multicast_router_timer);
893 del_timer(&port->multicast_query_timer);
894 spin_unlock(&br->multicast_lock);
895}
896
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900897static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
898 struct net_bridge_port *port,
899 struct sk_buff *skb)
Herbert Xueb1d1642010-02-27 19:41:45 +0000900{
901 struct igmpv3_report *ih;
902 struct igmpv3_grec *grec;
903 int i;
904 int len;
905 int num;
906 int type;
907 int err = 0;
908 __be32 group;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000909 u16 vid = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000910
911 if (!pskb_may_pull(skb, sizeof(*ih)))
912 return -EINVAL;
913
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000914 br_vlan_get_tag(skb, &vid);
Herbert Xueb1d1642010-02-27 19:41:45 +0000915 ih = igmpv3_report_hdr(skb);
916 num = ntohs(ih->ngrec);
917 len = sizeof(*ih);
918
919 for (i = 0; i < num; i++) {
920 len += sizeof(*grec);
921 if (!pskb_may_pull(skb, len))
922 return -EINVAL;
923
Herbert Xufd218cf2010-04-07 21:20:47 -0700924 grec = (void *)(skb->data + len - sizeof(*grec));
Herbert Xueb1d1642010-02-27 19:41:45 +0000925 group = grec->grec_mca;
926 type = grec->grec_type;
927
Eric Dumazet8eabf952010-04-20 03:20:05 +0000928 len += ntohs(grec->grec_nsrcs) * 4;
Herbert Xueb1d1642010-02-27 19:41:45 +0000929 if (!pskb_may_pull(skb, len))
930 return -EINVAL;
931
932 /* We treat this as an IGMPv2 report for now. */
933 switch (type) {
934 case IGMPV3_MODE_IS_INCLUDE:
935 case IGMPV3_MODE_IS_EXCLUDE:
936 case IGMPV3_CHANGE_TO_INCLUDE:
937 case IGMPV3_CHANGE_TO_EXCLUDE:
938 case IGMPV3_ALLOW_NEW_SOURCES:
939 case IGMPV3_BLOCK_OLD_SOURCES:
940 break;
941
942 default:
943 continue;
944 }
945
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000946 err = br_ip4_multicast_add_group(br, port, group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +0000947 if (err)
948 break;
949 }
950
951 return err;
952}
953
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000954#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900955static int br_ip6_multicast_mld2_report(struct net_bridge *br,
956 struct net_bridge_port *port,
957 struct sk_buff *skb)
958{
959 struct icmp6hdr *icmp6h;
960 struct mld2_grec *grec;
961 int i;
962 int len;
963 int num;
964 int err = 0;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000965 u16 vid = 0;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900966
967 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
968 return -EINVAL;
969
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000970 br_vlan_get_tag(skb, &vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900971 icmp6h = icmp6_hdr(skb);
972 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
973 len = sizeof(*icmp6h);
974
975 for (i = 0; i < num; i++) {
976 __be16 *nsrcs, _nsrcs;
977
978 nsrcs = skb_header_pointer(skb,
979 len + offsetof(struct mld2_grec,
Linus Lüssing649e9842011-02-15 13:19:18 +0000980 grec_nsrcs),
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900981 sizeof(_nsrcs), &_nsrcs);
982 if (!nsrcs)
983 return -EINVAL;
984
985 if (!pskb_may_pull(skb,
986 len + sizeof(*grec) +
Linus Lüssingd41db9f2011-02-15 13:19:19 +0000987 sizeof(struct in6_addr) * ntohs(*nsrcs)))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900988 return -EINVAL;
989
990 grec = (struct mld2_grec *)(skb->data + len);
Linus Lüssingd41db9f2011-02-15 13:19:19 +0000991 len += sizeof(*grec) +
992 sizeof(struct in6_addr) * ntohs(*nsrcs);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900993
994 /* We treat these as MLDv1 reports for now. */
995 switch (grec->grec_type) {
996 case MLD2_MODE_IS_INCLUDE:
997 case MLD2_MODE_IS_EXCLUDE:
998 case MLD2_CHANGE_TO_INCLUDE:
999 case MLD2_CHANGE_TO_EXCLUDE:
1000 case MLD2_ALLOW_NEW_SOURCES:
1001 case MLD2_BLOCK_OLD_SOURCES:
1002 break;
1003
1004 default:
1005 continue;
1006 }
1007
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001008 err = br_ip6_multicast_add_group(br, port, &grec->grec_mca,
1009 vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001010 if (!err)
1011 break;
1012 }
1013
1014 return err;
1015}
1016#endif
1017
stephen hemminger7e80c122010-04-27 15:01:04 +00001018/*
1019 * Add port to rotuer_list
1020 * list is maintained ordered by pointer value
1021 * and locked by br->multicast_lock and RCU
1022 */
Herbert Xu0909e112010-02-27 19:41:49 +00001023static void br_multicast_add_router(struct net_bridge *br,
1024 struct net_bridge_port *port)
1025{
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001026 struct net_bridge_port *p;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001027 struct hlist_node *slot = NULL;
Herbert Xu0909e112010-02-27 19:41:49 +00001028
Sasha Levinb67bfe02013-02-27 17:06:00 -08001029 hlist_for_each_entry(p, &br->router_list, rlist) {
stephen hemminger7e80c122010-04-27 15:01:04 +00001030 if ((unsigned long) port >= (unsigned long) p)
1031 break;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001032 slot = &p->rlist;
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001033 }
Herbert Xu0909e112010-02-27 19:41:49 +00001034
stephen hemminger7e80c122010-04-27 15:01:04 +00001035 if (slot)
1036 hlist_add_after_rcu(slot, &port->rlist);
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001037 else
1038 hlist_add_head_rcu(&port->rlist, &br->router_list);
Herbert Xu0909e112010-02-27 19:41:49 +00001039}
1040
Herbert Xueb1d1642010-02-27 19:41:45 +00001041static void br_multicast_mark_router(struct net_bridge *br,
1042 struct net_bridge_port *port)
1043{
1044 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +00001045
1046 if (!port) {
1047 if (br->multicast_router == 1)
1048 mod_timer(&br->multicast_router_timer,
1049 now + br->multicast_querier_interval);
1050 return;
1051 }
1052
1053 if (port->multicast_router != 1)
1054 return;
1055
1056 if (!hlist_unhashed(&port->rlist))
1057 goto timer;
1058
Herbert Xu0909e112010-02-27 19:41:49 +00001059 br_multicast_add_router(br, port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001060
1061timer:
1062 mod_timer(&port->multicast_router_timer,
1063 now + br->multicast_querier_interval);
1064}
1065
1066static void br_multicast_query_received(struct net_bridge *br,
1067 struct net_bridge_port *port,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001068 int saddr)
Herbert Xueb1d1642010-02-27 19:41:45 +00001069{
1070 if (saddr)
1071 mod_timer(&br->multicast_querier_timer,
1072 jiffies + br->multicast_querier_interval);
1073 else if (timer_pending(&br->multicast_querier_timer))
1074 return;
1075
1076 br_multicast_mark_router(br, port);
1077}
1078
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001079static int br_ip4_multicast_query(struct net_bridge *br,
1080 struct net_bridge_port *port,
1081 struct sk_buff *skb)
Herbert Xueb1d1642010-02-27 19:41:45 +00001082{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001083 const struct iphdr *iph = ip_hdr(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001084 struct igmphdr *ih = igmp_hdr(skb);
1085 struct net_bridge_mdb_entry *mp;
1086 struct igmpv3_query *ih3;
1087 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +00001088 struct net_bridge_port_group __rcu **pp;
Herbert Xueb1d1642010-02-27 19:41:45 +00001089 unsigned long max_delay;
1090 unsigned long now = jiffies;
1091 __be32 group;
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001092 int err = 0;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001093 u16 vid = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001094
1095 spin_lock(&br->multicast_lock);
1096 if (!netif_running(br->dev) ||
1097 (port && port->state == BR_STATE_DISABLED))
1098 goto out;
1099
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001100 br_multicast_query_received(br, port, !!iph->saddr);
Herbert Xueb1d1642010-02-27 19:41:45 +00001101
1102 group = ih->group;
1103
1104 if (skb->len == sizeof(*ih)) {
1105 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1106
1107 if (!max_delay) {
1108 max_delay = 10 * HZ;
1109 group = 0;
1110 }
1111 } else {
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001112 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) {
1113 err = -EINVAL;
1114 goto out;
1115 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001116
1117 ih3 = igmpv3_query_hdr(skb);
1118 if (ih3->nsrcs)
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001119 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001120
YOSHIFUJI Hideaki / 吉藤英明0ba8c9e2010-03-15 19:27:00 +00001121 max_delay = ih3->code ?
1122 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
Herbert Xueb1d1642010-02-27 19:41:45 +00001123 }
1124
1125 if (!group)
1126 goto out;
1127
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001128 br_vlan_get_tag(skb, &vid);
1129 mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001130 if (!mp)
1131 goto out;
1132
1133 max_delay *= br->multicast_last_member_count;
1134
Herbert Xu8a870172011-02-12 01:05:42 -08001135 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001136 (timer_pending(&mp->timer) ?
1137 time_after(mp->timer.expires, now + max_delay) :
1138 try_to_del_timer_sync(&mp->timer) >= 0))
1139 mod_timer(&mp->timer, now + max_delay);
1140
Eric Dumazete8051682010-11-15 06:38:10 +00001141 for (pp = &mp->ports;
1142 (p = mlock_dereference(*pp, br)) != NULL;
1143 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001144 if (timer_pending(&p->timer) ?
1145 time_after(p->timer.expires, now + max_delay) :
1146 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001147 mod_timer(&p->timer, now + max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001148 }
1149
1150out:
1151 spin_unlock(&br->multicast_lock);
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001152 return err;
Herbert Xueb1d1642010-02-27 19:41:45 +00001153}
1154
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001155#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001156static int br_ip6_multicast_query(struct net_bridge *br,
1157 struct net_bridge_port *port,
1158 struct sk_buff *skb)
1159{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001160 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
stephen hemmingereca2a432012-12-13 06:51:28 +00001161 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001162 struct net_bridge_mdb_entry *mp;
1163 struct mld2_query *mld2q;
Eric Dumazete8051682010-11-15 06:38:10 +00001164 struct net_bridge_port_group *p;
1165 struct net_bridge_port_group __rcu **pp;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001166 unsigned long max_delay;
1167 unsigned long now = jiffies;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001168 const struct in6_addr *group = NULL;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001169 int err = 0;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001170 u16 vid = 0;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001171
1172 spin_lock(&br->multicast_lock);
1173 if (!netif_running(br->dev) ||
1174 (port && port->state == BR_STATE_DISABLED))
1175 goto out;
1176
1177 br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->saddr));
1178
1179 if (skb->len == sizeof(*mld)) {
1180 if (!pskb_may_pull(skb, sizeof(*mld))) {
1181 err = -EINVAL;
1182 goto out;
1183 }
1184 mld = (struct mld_msg *) icmp6_hdr(skb);
Li RongQing47152132012-07-09 23:56:12 +00001185 max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001186 if (max_delay)
1187 group = &mld->mld_mca;
1188 } else if (skb->len >= sizeof(*mld2q)) {
1189 if (!pskb_may_pull(skb, sizeof(*mld2q))) {
1190 err = -EINVAL;
1191 goto out;
1192 }
1193 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1194 if (!mld2q->mld2q_nsrcs)
1195 group = &mld2q->mld2q_mca;
Ang Way Chuang8fa45a72012-12-13 23:08:39 +00001196 max_delay = mld2q->mld2q_mrc ? MLDV2_MRC(ntohs(mld2q->mld2q_mrc)) : 1;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001197 }
1198
1199 if (!group)
1200 goto out;
1201
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001202 br_vlan_get_tag(skb, &vid);
1203 mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001204 if (!mp)
1205 goto out;
1206
1207 max_delay *= br->multicast_last_member_count;
Herbert Xu8a870172011-02-12 01:05:42 -08001208 if (mp->mglist &&
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001209 (timer_pending(&mp->timer) ?
1210 time_after(mp->timer.expires, now + max_delay) :
1211 try_to_del_timer_sync(&mp->timer) >= 0))
1212 mod_timer(&mp->timer, now + max_delay);
1213
Eric Dumazete8051682010-11-15 06:38:10 +00001214 for (pp = &mp->ports;
1215 (p = mlock_dereference(*pp, br)) != NULL;
1216 pp = &p->next) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001217 if (timer_pending(&p->timer) ?
1218 time_after(p->timer.expires, now + max_delay) :
1219 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001220 mod_timer(&p->timer, now + max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001221 }
1222
1223out:
1224 spin_unlock(&br->multicast_lock);
1225 return err;
1226}
1227#endif
1228
Herbert Xueb1d1642010-02-27 19:41:45 +00001229static void br_multicast_leave_group(struct net_bridge *br,
1230 struct net_bridge_port *port,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001231 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +00001232{
1233 struct net_bridge_mdb_htable *mdb;
1234 struct net_bridge_mdb_entry *mp;
1235 struct net_bridge_port_group *p;
1236 unsigned long now;
1237 unsigned long time;
1238
Herbert Xueb1d1642010-02-27 19:41:45 +00001239 spin_lock(&br->multicast_lock);
1240 if (!netif_running(br->dev) ||
1241 (port && port->state == BR_STATE_DISABLED) ||
1242 timer_pending(&br->multicast_querier_timer))
1243 goto out;
1244
Eric Dumazete8051682010-11-15 06:38:10 +00001245 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001246 mp = br_mdb_ip_get(mdb, group);
1247 if (!mp)
1248 goto out;
1249
David S. Millerc2d3bab2012-12-05 16:24:45 -05001250 if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
Amerigo Wang50426b52012-12-03 23:56:40 +00001251 struct net_bridge_port_group __rcu **pp;
1252
1253 for (pp = &mp->ports;
1254 (p = mlock_dereference(*pp, br)) != NULL;
1255 pp = &p->next) {
1256 if (p->port != port)
1257 continue;
1258
1259 rcu_assign_pointer(*pp, p->next);
1260 hlist_del_init(&p->mglist);
1261 del_timer(&p->timer);
1262 call_rcu_bh(&p->rcu, br_multicast_free_pg);
Cong Wang37a393b2012-12-11 22:23:07 +00001263 br_mdb_notify(br->dev, port, group, RTM_DELMDB);
Amerigo Wang50426b52012-12-03 23:56:40 +00001264
1265 if (!mp->ports && !mp->mglist &&
1266 netif_running(br->dev))
1267 mod_timer(&mp->timer, jiffies);
1268 }
1269 goto out;
1270 }
1271
Herbert Xueb1d1642010-02-27 19:41:45 +00001272 now = jiffies;
1273 time = now + br->multicast_last_member_count *
1274 br->multicast_last_member_interval;
1275
1276 if (!port) {
Herbert Xu8a870172011-02-12 01:05:42 -08001277 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001278 (timer_pending(&mp->timer) ?
1279 time_after(mp->timer.expires, time) :
1280 try_to_del_timer_sync(&mp->timer) >= 0)) {
1281 mod_timer(&mp->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +00001282 }
1283
1284 goto out;
1285 }
1286
Eric Dumazete8051682010-11-15 06:38:10 +00001287 for (p = mlock_dereference(mp->ports, br);
1288 p != NULL;
1289 p = mlock_dereference(p->next, br)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001290 if (p->port != port)
1291 continue;
1292
1293 if (!hlist_unhashed(&p->mglist) &&
1294 (timer_pending(&p->timer) ?
1295 time_after(p->timer.expires, time) :
1296 try_to_del_timer_sync(&p->timer) >= 0)) {
1297 mod_timer(&p->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +00001298 }
1299
1300 break;
1301 }
1302
1303out:
1304 spin_unlock(&br->multicast_lock);
1305}
1306
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001307static void br_ip4_multicast_leave_group(struct net_bridge *br,
1308 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001309 __be32 group,
1310 __u16 vid)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001311{
1312 struct br_ip br_group;
1313
1314 if (ipv4_is_local_multicast(group))
1315 return;
1316
1317 br_group.u.ip4 = group;
1318 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001319 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001320
1321 br_multicast_leave_group(br, port, &br_group);
1322}
1323
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001324#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001325static void br_ip6_multicast_leave_group(struct net_bridge *br,
1326 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001327 const struct in6_addr *group,
1328 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001329{
1330 struct br_ip br_group;
1331
Linus Lüssinge4de9f92011-02-15 13:19:21 +00001332 if (!ipv6_is_transient_multicast(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001333 return;
1334
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001335 br_group.u.ip6 = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001336 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001337 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001338
1339 br_multicast_leave_group(br, port, &br_group);
1340}
1341#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001342
Herbert Xueb1d1642010-02-27 19:41:45 +00001343static int br_multicast_ipv4_rcv(struct net_bridge *br,
1344 struct net_bridge_port *port,
1345 struct sk_buff *skb)
1346{
1347 struct sk_buff *skb2 = skb;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001348 const struct iphdr *iph;
Herbert Xueb1d1642010-02-27 19:41:45 +00001349 struct igmphdr *ih;
Eric Dumazet95c96172012-04-15 05:58:06 +00001350 unsigned int len;
1351 unsigned int offset;
Herbert Xueb1d1642010-02-27 19:41:45 +00001352 int err;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001353 u16 vid = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001354
Herbert Xueb1d1642010-02-27 19:41:45 +00001355 /* We treat OOM as packet loss for now. */
1356 if (!pskb_may_pull(skb, sizeof(*iph)))
1357 return -EINVAL;
1358
1359 iph = ip_hdr(skb);
1360
1361 if (iph->ihl < 5 || iph->version != 4)
1362 return -EINVAL;
1363
1364 if (!pskb_may_pull(skb, ip_hdrlen(skb)))
1365 return -EINVAL;
1366
1367 iph = ip_hdr(skb);
1368
1369 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1370 return -EINVAL;
1371
Herbert Xubd4265f2011-06-23 02:39:12 +00001372 if (iph->protocol != IPPROTO_IGMP) {
Amerigo Wangbf5e4dd2013-03-07 02:32:26 +00001373 if (!ipv4_is_local_multicast(iph->daddr))
Herbert Xubd4265f2011-06-23 02:39:12 +00001374 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Herbert Xueb1d1642010-02-27 19:41:45 +00001375 return 0;
Herbert Xubd4265f2011-06-23 02:39:12 +00001376 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001377
1378 len = ntohs(iph->tot_len);
1379 if (skb->len < len || len < ip_hdrlen(skb))
1380 return -EINVAL;
1381
1382 if (skb->len > len) {
1383 skb2 = skb_clone(skb, GFP_ATOMIC);
1384 if (!skb2)
1385 return -ENOMEM;
1386
1387 err = pskb_trim_rcsum(skb2, len);
1388 if (err)
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001389 goto err_out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001390 }
1391
1392 len -= ip_hdrlen(skb2);
1393 offset = skb_network_offset(skb2) + ip_hdrlen(skb2);
1394 __skb_pull(skb2, offset);
1395 skb_reset_transport_header(skb2);
1396
1397 err = -EINVAL;
1398 if (!pskb_may_pull(skb2, sizeof(*ih)))
1399 goto out;
1400
Herbert Xueb1d1642010-02-27 19:41:45 +00001401 switch (skb2->ip_summed) {
1402 case CHECKSUM_COMPLETE:
1403 if (!csum_fold(skb2->csum))
1404 break;
1405 /* fall through */
1406 case CHECKSUM_NONE:
1407 skb2->csum = 0;
1408 if (skb_checksum_complete(skb2))
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001409 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001410 }
1411
1412 err = 0;
1413
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001414 br_vlan_get_tag(skb2, &vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001415 BR_INPUT_SKB_CB(skb)->igmp = 1;
1416 ih = igmp_hdr(skb2);
1417
1418 switch (ih->type) {
1419 case IGMP_HOST_MEMBERSHIP_REPORT:
1420 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Fernando Luis Vázquez Cao62b2bcb2011-06-13 15:04:43 +00001421 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001422 err = br_ip4_multicast_add_group(br, port, ih->group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001423 break;
1424 case IGMPV3_HOST_MEMBERSHIP_REPORT:
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001425 err = br_ip4_multicast_igmp3_report(br, port, skb2);
Herbert Xueb1d1642010-02-27 19:41:45 +00001426 break;
1427 case IGMP_HOST_MEMBERSHIP_QUERY:
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001428 err = br_ip4_multicast_query(br, port, skb2);
Herbert Xueb1d1642010-02-27 19:41:45 +00001429 break;
1430 case IGMP_HOST_LEAVE_MESSAGE:
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001431 br_ip4_multicast_leave_group(br, port, ih->group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001432 break;
1433 }
1434
1435out:
1436 __skb_push(skb2, offset);
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001437err_out:
Herbert Xueb1d1642010-02-27 19:41:45 +00001438 if (skb2 != skb)
1439 kfree_skb(skb2);
1440 return err;
1441}
1442
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001443#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001444static int br_multicast_ipv6_rcv(struct net_bridge *br,
1445 struct net_bridge_port *port,
1446 struct sk_buff *skb)
1447{
Tomas Winkler9d890812011-01-03 11:26:08 -08001448 struct sk_buff *skb2;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001449 const struct ipv6hdr *ip6h;
Eric Dumazet22df1332011-08-23 19:57:05 +00001450 u8 icmp6_type;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001451 u8 nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -08001452 __be16 frag_off;
Eric Dumazet95c96172012-04-15 05:58:06 +00001453 unsigned int len;
Kulikov Vasiliybb7a0bd2010-07-15 08:47:33 +00001454 int offset;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001455 int err;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001456 u16 vid = 0;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001457
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001458 if (!pskb_may_pull(skb, sizeof(*ip6h)))
1459 return -EINVAL;
1460
1461 ip6h = ipv6_hdr(skb);
1462
1463 /*
1464 * We're interested in MLD messages only.
1465 * - Version is 6
1466 * - MLD has always Router Alert hop-by-hop option
1467 * - But we do not support jumbrograms.
1468 */
1469 if (ip6h->version != 6 ||
1470 ip6h->nexthdr != IPPROTO_HOPOPTS ||
1471 ip6h->payload_len == 0)
1472 return 0;
1473
Linus Lüssingff9a57a2011-03-26 20:27:24 +00001474 len = ntohs(ip6h->payload_len) + sizeof(*ip6h);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001475 if (skb->len < len)
1476 return -EINVAL;
1477
1478 nexthdr = ip6h->nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -08001479 offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr, &frag_off);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001480
1481 if (offset < 0 || nexthdr != IPPROTO_ICMPV6)
1482 return 0;
1483
1484 /* Okay, we found ICMPv6 header */
1485 skb2 = skb_clone(skb, GFP_ATOMIC);
1486 if (!skb2)
1487 return -ENOMEM;
1488
Tomas Winkler9d890812011-01-03 11:26:08 -08001489 err = -EINVAL;
1490 if (!pskb_may_pull(skb2, offset + sizeof(struct icmp6hdr)))
1491 goto out;
1492
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001493 len -= offset - skb_network_offset(skb2);
1494
1495 __skb_pull(skb2, offset);
1496 skb_reset_transport_header(skb2);
stephen hemmingerfa2da8c2011-11-15 08:09:14 +00001497 skb_postpull_rcsum(skb2, skb_network_header(skb2),
1498 skb_network_header_len(skb2));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001499
Eric Dumazet22df1332011-08-23 19:57:05 +00001500 icmp6_type = icmp6_hdr(skb2)->icmp6_type;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001501
Eric Dumazet22df1332011-08-23 19:57:05 +00001502 switch (icmp6_type) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001503 case ICMPV6_MGM_QUERY:
1504 case ICMPV6_MGM_REPORT:
1505 case ICMPV6_MGM_REDUCTION:
1506 case ICMPV6_MLD2_REPORT:
1507 break;
1508 default:
1509 err = 0;
1510 goto out;
1511 }
1512
1513 /* Okay, we found MLD message. Check further. */
1514 if (skb2->len > len) {
1515 err = pskb_trim_rcsum(skb2, len);
1516 if (err)
1517 goto out;
Yan, Zheng4b275d72011-08-23 22:54:33 +00001518 err = -EINVAL;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001519 }
1520
Yan, Zheng4b275d72011-08-23 22:54:33 +00001521 ip6h = ipv6_hdr(skb2);
1522
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001523 switch (skb2->ip_summed) {
1524 case CHECKSUM_COMPLETE:
Yan, Zheng4b275d72011-08-23 22:54:33 +00001525 if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, skb2->len,
1526 IPPROTO_ICMPV6, skb2->csum))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001527 break;
1528 /*FALLTHROUGH*/
1529 case CHECKSUM_NONE:
Yan, Zheng4b275d72011-08-23 22:54:33 +00001530 skb2->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr,
1531 &ip6h->daddr,
1532 skb2->len,
1533 IPPROTO_ICMPV6, 0));
1534 if (__skb_checksum_complete(skb2))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001535 goto out;
1536 }
1537
1538 err = 0;
1539
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001540 br_vlan_get_tag(skb, &vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001541 BR_INPUT_SKB_CB(skb)->igmp = 1;
1542
Eric Dumazet22df1332011-08-23 19:57:05 +00001543 switch (icmp6_type) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001544 case ICMPV6_MGM_REPORT:
1545 {
Tomas Winkler9d890812011-01-03 11:26:08 -08001546 struct mld_msg *mld;
1547 if (!pskb_may_pull(skb2, sizeof(*mld))) {
1548 err = -EINVAL;
1549 goto out;
1550 }
1551 mld = (struct mld_msg *)skb_transport_header(skb2);
Fernando Luis Vázquez Caofc2af6c2011-06-13 15:06:58 +00001552 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001553 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001554 break;
1555 }
1556 case ICMPV6_MLD2_REPORT:
1557 err = br_ip6_multicast_mld2_report(br, port, skb2);
1558 break;
1559 case ICMPV6_MGM_QUERY:
1560 err = br_ip6_multicast_query(br, port, skb2);
1561 break;
1562 case ICMPV6_MGM_REDUCTION:
1563 {
Tomas Winkler9d890812011-01-03 11:26:08 -08001564 struct mld_msg *mld;
1565 if (!pskb_may_pull(skb2, sizeof(*mld))) {
1566 err = -EINVAL;
1567 goto out;
1568 }
1569 mld = (struct mld_msg *)skb_transport_header(skb2);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001570 br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001571 }
1572 }
1573
1574out:
Tomas Winkler9d890812011-01-03 11:26:08 -08001575 kfree_skb(skb2);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001576 return err;
1577}
1578#endif
1579
Herbert Xueb1d1642010-02-27 19:41:45 +00001580int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1581 struct sk_buff *skb)
1582{
YOSHIFUJI Hideaki / 吉藤英明1fafc7a2010-04-25 08:06:40 +00001583 BR_INPUT_SKB_CB(skb)->igmp = 0;
1584 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1585
Herbert Xueb1d1642010-02-27 19:41:45 +00001586 if (br->multicast_disabled)
1587 return 0;
1588
1589 switch (skb->protocol) {
1590 case htons(ETH_P_IP):
1591 return br_multicast_ipv4_rcv(br, port, skb);
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001592#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001593 case htons(ETH_P_IPV6):
1594 return br_multicast_ipv6_rcv(br, port, skb);
1595#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001596 }
1597
1598 return 0;
1599}
1600
1601static void br_multicast_query_expired(unsigned long data)
1602{
1603 struct net_bridge *br = (void *)data;
1604
1605 spin_lock(&br->multicast_lock);
1606 if (br->multicast_startup_queries_sent <
1607 br->multicast_startup_query_count)
1608 br->multicast_startup_queries_sent++;
1609
1610 br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent);
1611
1612 spin_unlock(&br->multicast_lock);
1613}
1614
1615void br_multicast_init(struct net_bridge *br)
1616{
1617 br->hash_elasticity = 4;
1618 br->hash_max = 512;
1619
1620 br->multicast_router = 1;
Herbert Xuc5c23262012-04-13 02:37:42 +00001621 br->multicast_querier = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001622 br->multicast_last_member_count = 2;
1623 br->multicast_startup_query_count = 2;
1624
1625 br->multicast_last_member_interval = HZ;
1626 br->multicast_query_response_interval = 10 * HZ;
1627 br->multicast_startup_query_interval = 125 * HZ / 4;
1628 br->multicast_query_interval = 125 * HZ;
1629 br->multicast_querier_interval = 255 * HZ;
1630 br->multicast_membership_interval = 260 * HZ;
1631
1632 spin_lock_init(&br->multicast_lock);
1633 setup_timer(&br->multicast_router_timer,
1634 br_multicast_local_router_expired, 0);
1635 setup_timer(&br->multicast_querier_timer,
Herbert Xubb63f1f2012-04-30 00:22:56 +00001636 br_multicast_querier_expired, (unsigned long)br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001637 setup_timer(&br->multicast_query_timer, br_multicast_query_expired,
1638 (unsigned long)br);
1639}
1640
1641void br_multicast_open(struct net_bridge *br)
1642{
1643 br->multicast_startup_queries_sent = 0;
1644
1645 if (br->multicast_disabled)
1646 return;
1647
1648 mod_timer(&br->multicast_query_timer, jiffies);
1649}
1650
1651void br_multicast_stop(struct net_bridge *br)
1652{
1653 struct net_bridge_mdb_htable *mdb;
1654 struct net_bridge_mdb_entry *mp;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001655 struct hlist_node *n;
Herbert Xueb1d1642010-02-27 19:41:45 +00001656 u32 ver;
1657 int i;
1658
1659 del_timer_sync(&br->multicast_router_timer);
1660 del_timer_sync(&br->multicast_querier_timer);
1661 del_timer_sync(&br->multicast_query_timer);
1662
1663 spin_lock_bh(&br->multicast_lock);
Eric Dumazete8051682010-11-15 06:38:10 +00001664 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001665 if (!mdb)
1666 goto out;
1667
1668 br->mdb = NULL;
1669
1670 ver = mdb->ver;
1671 for (i = 0; i < mdb->max; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001672 hlist_for_each_entry_safe(mp, n, &mdb->mhash[i],
Herbert Xueb1d1642010-02-27 19:41:45 +00001673 hlist[ver]) {
1674 del_timer(&mp->timer);
Herbert Xueb1d1642010-02-27 19:41:45 +00001675 call_rcu_bh(&mp->rcu, br_multicast_free_group);
1676 }
1677 }
1678
1679 if (mdb->old) {
1680 spin_unlock_bh(&br->multicast_lock);
Herbert Xu10cc2b52010-03-05 21:03:35 +00001681 rcu_barrier_bh();
Herbert Xueb1d1642010-02-27 19:41:45 +00001682 spin_lock_bh(&br->multicast_lock);
1683 WARN_ON(mdb->old);
1684 }
1685
1686 mdb->old = mdb;
1687 call_rcu_bh(&mdb->rcu, br_mdb_free);
1688
1689out:
1690 spin_unlock_bh(&br->multicast_lock);
1691}
Herbert Xu0909e112010-02-27 19:41:49 +00001692
1693int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1694{
1695 int err = -ENOENT;
1696
1697 spin_lock_bh(&br->multicast_lock);
1698 if (!netif_running(br->dev))
1699 goto unlock;
1700
1701 switch (val) {
1702 case 0:
1703 case 2:
1704 del_timer(&br->multicast_router_timer);
1705 /* fall through */
1706 case 1:
1707 br->multicast_router = val;
1708 err = 0;
1709 break;
1710
1711 default:
1712 err = -EINVAL;
1713 break;
1714 }
1715
1716unlock:
1717 spin_unlock_bh(&br->multicast_lock);
1718
1719 return err;
1720}
1721
1722int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1723{
1724 struct net_bridge *br = p->br;
1725 int err = -ENOENT;
1726
1727 spin_lock(&br->multicast_lock);
1728 if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED)
1729 goto unlock;
1730
1731 switch (val) {
1732 case 0:
1733 case 1:
1734 case 2:
1735 p->multicast_router = val;
1736 err = 0;
1737
1738 if (val < 2 && !hlist_unhashed(&p->rlist))
1739 hlist_del_init_rcu(&p->rlist);
1740
1741 if (val == 1)
1742 break;
1743
1744 del_timer(&p->multicast_router_timer);
1745
1746 if (val == 0)
1747 break;
1748
1749 br_multicast_add_router(br, p);
1750 break;
1751
1752 default:
1753 err = -EINVAL;
1754 break;
1755 }
1756
1757unlock:
1758 spin_unlock(&br->multicast_lock);
1759
1760 return err;
1761}
Herbert Xu561f1102010-02-27 19:41:50 +00001762
Herbert Xu74857212012-04-13 02:37:42 +00001763static void br_multicast_start_querier(struct net_bridge *br)
Herbert Xu561f1102010-02-27 19:41:50 +00001764{
1765 struct net_bridge_port *port;
Herbert Xu74857212012-04-13 02:37:42 +00001766
1767 br_multicast_open(br);
1768
1769 list_for_each_entry(port, &br->port_list, list) {
1770 if (port->state == BR_STATE_DISABLED ||
1771 port->state == BR_STATE_BLOCKING)
1772 continue;
1773
1774 __br_multicast_enable_port(port);
1775 }
1776}
1777
1778int br_multicast_toggle(struct net_bridge *br, unsigned long val)
1779{
Herbert Xu3a7fda02010-07-29 00:45:30 +00001780 int err = 0;
Eric Dumazete8051682010-11-15 06:38:10 +00001781 struct net_bridge_mdb_htable *mdb;
Herbert Xu561f1102010-02-27 19:41:50 +00001782
Andrey Vaginef5e0d82011-11-10 05:48:03 +00001783 spin_lock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00001784 if (br->multicast_disabled == !val)
1785 goto unlock;
1786
1787 br->multicast_disabled = !val;
1788 if (br->multicast_disabled)
1789 goto unlock;
1790
Herbert Xu3a7fda02010-07-29 00:45:30 +00001791 if (!netif_running(br->dev))
1792 goto unlock;
1793
Eric Dumazete8051682010-11-15 06:38:10 +00001794 mdb = mlock_dereference(br->mdb, br);
1795 if (mdb) {
1796 if (mdb->old) {
Herbert Xu561f1102010-02-27 19:41:50 +00001797 err = -EEXIST;
1798rollback:
1799 br->multicast_disabled = !!val;
1800 goto unlock;
1801 }
1802
Eric Dumazete8051682010-11-15 06:38:10 +00001803 err = br_mdb_rehash(&br->mdb, mdb->max,
Herbert Xu561f1102010-02-27 19:41:50 +00001804 br->hash_elasticity);
1805 if (err)
1806 goto rollback;
1807 }
1808
Herbert Xu74857212012-04-13 02:37:42 +00001809 br_multicast_start_querier(br);
Herbert Xu561f1102010-02-27 19:41:50 +00001810
1811unlock:
Andrey Vaginef5e0d82011-11-10 05:48:03 +00001812 spin_unlock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00001813
1814 return err;
1815}
Herbert Xub1951672010-02-27 19:41:51 +00001816
Herbert Xuc5c23262012-04-13 02:37:42 +00001817int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
1818{
1819 val = !!val;
1820
1821 spin_lock_bh(&br->multicast_lock);
1822 if (br->multicast_querier == val)
1823 goto unlock;
1824
1825 br->multicast_querier = val;
1826 if (val)
1827 br_multicast_start_querier(br);
1828
1829unlock:
1830 spin_unlock_bh(&br->multicast_lock);
1831
1832 return 0;
1833}
1834
Herbert Xub1951672010-02-27 19:41:51 +00001835int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
1836{
1837 int err = -ENOENT;
1838 u32 old;
Eric Dumazete8051682010-11-15 06:38:10 +00001839 struct net_bridge_mdb_htable *mdb;
Herbert Xub1951672010-02-27 19:41:51 +00001840
1841 spin_lock(&br->multicast_lock);
1842 if (!netif_running(br->dev))
1843 goto unlock;
1844
1845 err = -EINVAL;
1846 if (!is_power_of_2(val))
1847 goto unlock;
Eric Dumazete8051682010-11-15 06:38:10 +00001848
1849 mdb = mlock_dereference(br->mdb, br);
1850 if (mdb && val < mdb->size)
Herbert Xub1951672010-02-27 19:41:51 +00001851 goto unlock;
1852
1853 err = 0;
1854
1855 old = br->hash_max;
1856 br->hash_max = val;
1857
Eric Dumazete8051682010-11-15 06:38:10 +00001858 if (mdb) {
1859 if (mdb->old) {
Herbert Xub1951672010-02-27 19:41:51 +00001860 err = -EEXIST;
1861rollback:
1862 br->hash_max = old;
1863 goto unlock;
1864 }
1865
1866 err = br_mdb_rehash(&br->mdb, br->hash_max,
1867 br->hash_elasticity);
1868 if (err)
1869 goto rollback;
1870 }
1871
1872unlock:
1873 spin_unlock(&br->multicast_lock);
1874
1875 return err;
1876}