blob: 923fbeaf7afdbb957afb732fc226c4fb669e6a2e [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 Stevens76d66152010-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
468 interval = ipv6_addr_any(group) ? br->multicast_last_member_interval :
469 br->multicast_query_response_interval;
470
471 mldq->mld_type = ICMPV6_MGM_QUERY;
472 mldq->mld_code = 0;
473 mldq->mld_cksum = 0;
474 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
475 mldq->mld_reserved = 0;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000476 mldq->mld_mca = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900477
478 /* checksum */
479 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
480 sizeof(*mldq), IPPROTO_ICMPV6,
481 csum_partial(mldq,
482 sizeof(*mldq), 0));
483 skb_put(skb, sizeof(*mldq));
484
485 __skb_pull(skb, sizeof(*eth));
486
487out:
488 return skb;
489}
490#endif
491
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900492static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
493 struct br_ip *addr)
494{
495 switch (addr->proto) {
496 case htons(ETH_P_IP):
497 return br_ip4_multicast_alloc_query(br, addr->u.ip4);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000498#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900499 case htons(ETH_P_IPV6):
500 return br_ip6_multicast_alloc_query(br, &addr->u.ip6);
501#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900502 }
503 return NULL;
504}
505
Herbert Xueb1d1642010-02-27 19:41:45 +0000506static struct net_bridge_mdb_entry *br_multicast_get_group(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900507 struct net_bridge *br, struct net_bridge_port *port,
508 struct br_ip *group, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +0000509{
Eric Dumazete8051682010-11-15 06:38:10 +0000510 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000511 struct net_bridge_mdb_entry *mp;
Eric Dumazet95c96172012-04-15 05:58:06 +0000512 unsigned int count = 0;
513 unsigned int max;
Herbert Xueb1d1642010-02-27 19:41:45 +0000514 int elasticity;
515 int err;
516
Eric Dumazete8051682010-11-15 06:38:10 +0000517 mdb = rcu_dereference_protected(br->mdb, 1);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800518 hlist_for_each_entry(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000519 count++;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900520 if (unlikely(br_ip_equal(group, &mp->addr)))
Herbert Xueb1d1642010-02-27 19:41:45 +0000521 return mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000522 }
523
524 elasticity = 0;
525 max = mdb->max;
526
527 if (unlikely(count > br->hash_elasticity && count)) {
528 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000529 br_info(br, "Multicast hash table "
530 "chain limit reached: %s\n",
531 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000532
533 elasticity = br->hash_elasticity;
534 }
535
536 if (mdb->size >= max) {
537 max *= 2;
Thomas Graf036be6d2012-07-10 22:29:19 +0000538 if (unlikely(max > br->hash_max)) {
539 br_warn(br, "Multicast hash table maximum of %d "
540 "reached, disabling snooping: %s\n",
541 br->hash_max,
542 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000543 err = -E2BIG;
544disable:
545 br->multicast_disabled = 1;
546 goto err;
547 }
548 }
549
550 if (max > mdb->max || elasticity) {
551 if (mdb->old) {
552 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000553 br_info(br, "Multicast hash table "
554 "on fire: %s\n",
555 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000556 err = -EEXIST;
557 goto err;
558 }
559
560 err = br_mdb_rehash(&br->mdb, max, elasticity);
561 if (err) {
stephen hemminger28a16c92010-05-10 09:31:09 +0000562 br_warn(br, "Cannot rehash multicast "
563 "hash table, disabling snooping: %s, %d, %d\n",
564 port ? port->dev->name : br->dev->name,
565 mdb->size, err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000566 goto disable;
567 }
568
569 err = -EAGAIN;
570 goto err;
571 }
572
573 return NULL;
574
575err:
576 mp = ERR_PTR(err);
577 return mp;
578}
579
Cong Wangcfd56752012-12-11 22:23:08 +0000580struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
581 struct net_bridge_port *port, struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000582{
Eric Dumazete8051682010-11-15 06:38:10 +0000583 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000584 struct net_bridge_mdb_entry *mp;
585 int hash;
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000586 int err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000587
Eric Dumazete8051682010-11-15 06:38:10 +0000588 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000589 if (!mdb) {
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000590 err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0);
591 if (err)
592 return ERR_PTR(err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000593 goto rehash;
594 }
595
596 hash = br_ip_hash(mdb, group);
597 mp = br_multicast_get_group(br, port, group, hash);
598 switch (PTR_ERR(mp)) {
599 case 0:
600 break;
601
602 case -EAGAIN:
603rehash:
Eric Dumazete8051682010-11-15 06:38:10 +0000604 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000605 hash = br_ip_hash(mdb, group);
606 break;
607
608 default:
609 goto out;
610 }
611
612 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
613 if (unlikely(!mp))
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000614 return ERR_PTR(-ENOMEM);
Herbert Xueb1d1642010-02-27 19:41:45 +0000615
616 mp->br = br;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900617 mp->addr = *group;
Herbert Xueb1d1642010-02-27 19:41:45 +0000618 setup_timer(&mp->timer, br_multicast_group_expired,
619 (unsigned long)mp);
Herbert Xueb1d1642010-02-27 19:41:45 +0000620
621 hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
622 mdb->size++;
623
624out:
625 return mp;
626}
627
Cong Wangcfd56752012-12-11 22:23:08 +0000628struct net_bridge_port_group *br_multicast_new_port_group(
629 struct net_bridge_port *port,
630 struct br_ip *group,
Amerigo Wangccb1c312012-12-14 22:09:51 +0000631 struct net_bridge_port_group __rcu *next,
632 unsigned char state)
Cong Wangcfd56752012-12-11 22:23:08 +0000633{
634 struct net_bridge_port_group *p;
635
636 p = kzalloc(sizeof(*p), GFP_ATOMIC);
637 if (unlikely(!p))
638 return NULL;
639
640 p->addr = *group;
641 p->port = port;
Amerigo Wangccb1c312012-12-14 22:09:51 +0000642 p->state = state;
stephen hemmingereca2a432012-12-13 06:51:28 +0000643 rcu_assign_pointer(p->next, next);
Cong Wangcfd56752012-12-11 22:23:08 +0000644 hlist_add_head(&p->mglist, &port->mglist);
645 setup_timer(&p->timer, br_multicast_port_group_expired,
646 (unsigned long)p);
647 return p;
648}
649
Herbert Xueb1d1642010-02-27 19:41:45 +0000650static int br_multicast_add_group(struct net_bridge *br,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900651 struct net_bridge_port *port,
652 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000653{
654 struct net_bridge_mdb_entry *mp;
655 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +0000656 struct net_bridge_port_group __rcu **pp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000657 unsigned long now = jiffies;
658 int err;
659
Herbert Xueb1d1642010-02-27 19:41:45 +0000660 spin_lock(&br->multicast_lock);
661 if (!netif_running(br->dev) ||
662 (port && port->state == BR_STATE_DISABLED))
663 goto out;
664
665 mp = br_multicast_new_group(br, port, group);
666 err = PTR_ERR(mp);
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000667 if (IS_ERR(mp))
Herbert Xueb1d1642010-02-27 19:41:45 +0000668 goto err;
669
670 if (!port) {
Herbert Xu8a870172011-02-12 01:05:42 -0800671 mp->mglist = true;
Herbert Xueb1d1642010-02-27 19:41:45 +0000672 mod_timer(&mp->timer, now + br->multicast_membership_interval);
673 goto out;
674 }
675
Eric Dumazete8051682010-11-15 06:38:10 +0000676 for (pp = &mp->ports;
677 (p = mlock_dereference(*pp, br)) != NULL;
678 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000679 if (p->port == port)
680 goto found;
681 if ((unsigned long)p->port < (unsigned long)port)
682 break;
683 }
684
Amerigo Wangccb1c312012-12-14 22:09:51 +0000685 p = br_multicast_new_port_group(port, group, *pp, MDB_TEMPORARY);
Herbert Xueb1d1642010-02-27 19:41:45 +0000686 if (unlikely(!p))
687 goto err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000688 rcu_assign_pointer(*pp, p);
Cong Wang37a393b2012-12-11 22:23:07 +0000689 br_mdb_notify(br->dev, port, group, RTM_NEWMDB);
Herbert Xueb1d1642010-02-27 19:41:45 +0000690
691found:
692 mod_timer(&p->timer, now + br->multicast_membership_interval);
693out:
694 err = 0;
695
696err:
697 spin_unlock(&br->multicast_lock);
698 return err;
699}
700
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900701static int br_ip4_multicast_add_group(struct net_bridge *br,
702 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000703 __be32 group,
704 __u16 vid)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900705{
706 struct br_ip br_group;
707
708 if (ipv4_is_local_multicast(group))
709 return 0;
710
711 br_group.u.ip4 = group;
712 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000713 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900714
715 return br_multicast_add_group(br, port, &br_group);
716}
717
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000718#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900719static int br_ip6_multicast_add_group(struct net_bridge *br,
720 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000721 const struct in6_addr *group,
722 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900723{
724 struct br_ip br_group;
725
Linus Lüssinge4de9f92011-02-15 13:19:21 +0000726 if (!ipv6_is_transient_multicast(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900727 return 0;
728
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000729 br_group.u.ip6 = *group;
Linus Lüssing9cc6e0c2011-02-15 13:19:17 +0000730 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000731 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900732
733 return br_multicast_add_group(br, port, &br_group);
734}
735#endif
736
Herbert Xueb1d1642010-02-27 19:41:45 +0000737static void br_multicast_router_expired(unsigned long data)
738{
739 struct net_bridge_port *port = (void *)data;
740 struct net_bridge *br = port->br;
741
742 spin_lock(&br->multicast_lock);
743 if (port->multicast_router != 1 ||
744 timer_pending(&port->multicast_router_timer) ||
745 hlist_unhashed(&port->rlist))
746 goto out;
747
748 hlist_del_init_rcu(&port->rlist);
749
750out:
751 spin_unlock(&br->multicast_lock);
752}
753
754static void br_multicast_local_router_expired(unsigned long data)
755{
756}
757
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000758static void br_multicast_querier_expired(unsigned long data)
759{
Herbert Xubb63f1f2012-04-30 00:22:56 +0000760 struct net_bridge *br = (void *)data;
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000761
762 spin_lock(&br->multicast_lock);
763 if (!netif_running(br->dev) || br->multicast_disabled)
764 goto out;
765
766 br_multicast_start_querier(br);
767
768out:
769 spin_unlock(&br->multicast_lock);
770}
771
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900772static void __br_multicast_send_query(struct net_bridge *br,
773 struct net_bridge_port *port,
774 struct br_ip *ip)
Herbert Xueb1d1642010-02-27 19:41:45 +0000775{
Herbert Xueb1d1642010-02-27 19:41:45 +0000776 struct sk_buff *skb;
777
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900778 skb = br_multicast_alloc_query(br, ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000779 if (!skb)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900780 return;
Herbert Xueb1d1642010-02-27 19:41:45 +0000781
782 if (port) {
783 __skb_push(skb, sizeof(struct ethhdr));
784 skb->dev = port->dev;
Jan Engelhardt713aefa2010-03-23 04:07:21 +0100785 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
Herbert Xueb1d1642010-02-27 19:41:45 +0000786 dev_queue_xmit);
787 } else
788 netif_rx(skb);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900789}
Herbert Xueb1d1642010-02-27 19:41:45 +0000790
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900791static void br_multicast_send_query(struct net_bridge *br,
792 struct net_bridge_port *port, u32 sent)
793{
794 unsigned long time;
795 struct br_ip br_group;
796
797 if (!netif_running(br->dev) || br->multicast_disabled ||
Herbert Xuc5c23262012-04-13 02:37:42 +0000798 !br->multicast_querier ||
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900799 timer_pending(&br->multicast_querier_timer))
800 return;
801
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900802 memset(&br_group.u, 0, sizeof(br_group.u));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900803
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900804 br_group.proto = htons(ETH_P_IP);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900805 __br_multicast_send_query(br, port, &br_group);
806
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000807#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900808 br_group.proto = htons(ETH_P_IPV6);
809 __br_multicast_send_query(br, port, &br_group);
810#endif
811
Herbert Xueb1d1642010-02-27 19:41:45 +0000812 time = jiffies;
813 time += sent < br->multicast_startup_query_count ?
814 br->multicast_startup_query_interval :
815 br->multicast_query_interval;
816 mod_timer(port ? &port->multicast_query_timer :
817 &br->multicast_query_timer, time);
818}
819
820static void br_multicast_port_query_expired(unsigned long data)
821{
822 struct net_bridge_port *port = (void *)data;
823 struct net_bridge *br = port->br;
824
825 spin_lock(&br->multicast_lock);
Dan Carpenter02a780c2010-03-06 01:14:09 +0000826 if (port->state == BR_STATE_DISABLED ||
827 port->state == BR_STATE_BLOCKING)
Herbert Xueb1d1642010-02-27 19:41:45 +0000828 goto out;
829
830 if (port->multicast_startup_queries_sent <
831 br->multicast_startup_query_count)
832 port->multicast_startup_queries_sent++;
833
834 br_multicast_send_query(port->br, port,
835 port->multicast_startup_queries_sent);
836
837out:
838 spin_unlock(&br->multicast_lock);
839}
840
841void br_multicast_add_port(struct net_bridge_port *port)
842{
843 port->multicast_router = 1;
844
845 setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
846 (unsigned long)port);
847 setup_timer(&port->multicast_query_timer,
848 br_multicast_port_query_expired, (unsigned long)port);
849}
850
851void br_multicast_del_port(struct net_bridge_port *port)
852{
853 del_timer_sync(&port->multicast_router_timer);
854}
855
Herbert Xu561f1102010-02-27 19:41:50 +0000856static void __br_multicast_enable_port(struct net_bridge_port *port)
857{
858 port->multicast_startup_queries_sent = 0;
859
860 if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 ||
861 del_timer(&port->multicast_query_timer))
862 mod_timer(&port->multicast_query_timer, jiffies);
863}
864
Herbert Xueb1d1642010-02-27 19:41:45 +0000865void br_multicast_enable_port(struct net_bridge_port *port)
866{
867 struct net_bridge *br = port->br;
868
869 spin_lock(&br->multicast_lock);
870 if (br->multicast_disabled || !netif_running(br->dev))
871 goto out;
872
Herbert Xu561f1102010-02-27 19:41:50 +0000873 __br_multicast_enable_port(port);
Herbert Xueb1d1642010-02-27 19:41:45 +0000874
875out:
876 spin_unlock(&br->multicast_lock);
877}
878
879void br_multicast_disable_port(struct net_bridge_port *port)
880{
881 struct net_bridge *br = port->br;
882 struct net_bridge_port_group *pg;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800883 struct hlist_node *n;
Herbert Xueb1d1642010-02-27 19:41:45 +0000884
885 spin_lock(&br->multicast_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800886 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
Herbert Xueb1d1642010-02-27 19:41:45 +0000887 br_multicast_del_pg(br, pg);
888
889 if (!hlist_unhashed(&port->rlist))
890 hlist_del_init_rcu(&port->rlist);
891 del_timer(&port->multicast_router_timer);
892 del_timer(&port->multicast_query_timer);
893 spin_unlock(&br->multicast_lock);
894}
895
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900896static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
897 struct net_bridge_port *port,
898 struct sk_buff *skb)
Herbert Xueb1d1642010-02-27 19:41:45 +0000899{
900 struct igmpv3_report *ih;
901 struct igmpv3_grec *grec;
902 int i;
903 int len;
904 int num;
905 int type;
906 int err = 0;
907 __be32 group;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000908 u16 vid = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000909
910 if (!pskb_may_pull(skb, sizeof(*ih)))
911 return -EINVAL;
912
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000913 br_vlan_get_tag(skb, &vid);
Herbert Xueb1d1642010-02-27 19:41:45 +0000914 ih = igmpv3_report_hdr(skb);
915 num = ntohs(ih->ngrec);
916 len = sizeof(*ih);
917
918 for (i = 0; i < num; i++) {
919 len += sizeof(*grec);
920 if (!pskb_may_pull(skb, len))
921 return -EINVAL;
922
Herbert Xufd218cf2010-04-07 21:20:47 -0700923 grec = (void *)(skb->data + len - sizeof(*grec));
Herbert Xueb1d1642010-02-27 19:41:45 +0000924 group = grec->grec_mca;
925 type = grec->grec_type;
926
Eric Dumazet8eabf952010-04-20 03:20:05 +0000927 len += ntohs(grec->grec_nsrcs) * 4;
Herbert Xueb1d1642010-02-27 19:41:45 +0000928 if (!pskb_may_pull(skb, len))
929 return -EINVAL;
930
931 /* We treat this as an IGMPv2 report for now. */
932 switch (type) {
933 case IGMPV3_MODE_IS_INCLUDE:
934 case IGMPV3_MODE_IS_EXCLUDE:
935 case IGMPV3_CHANGE_TO_INCLUDE:
936 case IGMPV3_CHANGE_TO_EXCLUDE:
937 case IGMPV3_ALLOW_NEW_SOURCES:
938 case IGMPV3_BLOCK_OLD_SOURCES:
939 break;
940
941 default:
942 continue;
943 }
944
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000945 err = br_ip4_multicast_add_group(br, port, group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +0000946 if (err)
947 break;
948 }
949
950 return err;
951}
952
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000953#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900954static int br_ip6_multicast_mld2_report(struct net_bridge *br,
955 struct net_bridge_port *port,
956 struct sk_buff *skb)
957{
958 struct icmp6hdr *icmp6h;
959 struct mld2_grec *grec;
960 int i;
961 int len;
962 int num;
963 int err = 0;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000964 u16 vid = 0;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900965
966 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
967 return -EINVAL;
968
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000969 br_vlan_get_tag(skb, &vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900970 icmp6h = icmp6_hdr(skb);
971 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
972 len = sizeof(*icmp6h);
973
974 for (i = 0; i < num; i++) {
975 __be16 *nsrcs, _nsrcs;
976
977 nsrcs = skb_header_pointer(skb,
978 len + offsetof(struct mld2_grec,
Linus Lüssing649e9842011-02-15 13:19:18 +0000979 grec_nsrcs),
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900980 sizeof(_nsrcs), &_nsrcs);
981 if (!nsrcs)
982 return -EINVAL;
983
984 if (!pskb_may_pull(skb,
985 len + sizeof(*grec) +
Linus Lüssingd41db9f2011-02-15 13:19:19 +0000986 sizeof(struct in6_addr) * ntohs(*nsrcs)))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900987 return -EINVAL;
988
989 grec = (struct mld2_grec *)(skb->data + len);
Linus Lüssingd41db9f2011-02-15 13:19:19 +0000990 len += sizeof(*grec) +
991 sizeof(struct in6_addr) * ntohs(*nsrcs);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900992
993 /* We treat these as MLDv1 reports for now. */
994 switch (grec->grec_type) {
995 case MLD2_MODE_IS_INCLUDE:
996 case MLD2_MODE_IS_EXCLUDE:
997 case MLD2_CHANGE_TO_INCLUDE:
998 case MLD2_CHANGE_TO_EXCLUDE:
999 case MLD2_ALLOW_NEW_SOURCES:
1000 case MLD2_BLOCK_OLD_SOURCES:
1001 break;
1002
1003 default:
1004 continue;
1005 }
1006
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001007 err = br_ip6_multicast_add_group(br, port, &grec->grec_mca,
1008 vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001009 if (!err)
1010 break;
1011 }
1012
1013 return err;
1014}
1015#endif
1016
stephen hemminger7e80c122010-04-27 15:01:04 +00001017/*
1018 * Add port to rotuer_list
1019 * list is maintained ordered by pointer value
1020 * and locked by br->multicast_lock and RCU
1021 */
Herbert Xu0909e112010-02-27 19:41:49 +00001022static void br_multicast_add_router(struct net_bridge *br,
1023 struct net_bridge_port *port)
1024{
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001025 struct net_bridge_port *p;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001026 struct hlist_node *slot = NULL;
Herbert Xu0909e112010-02-27 19:41:49 +00001027
Sasha Levinb67bfe02013-02-27 17:06:00 -08001028 hlist_for_each_entry(p, &br->router_list, rlist) {
stephen hemminger7e80c122010-04-27 15:01:04 +00001029 if ((unsigned long) port >= (unsigned long) p)
1030 break;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001031 slot = &p->rlist;
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001032 }
Herbert Xu0909e112010-02-27 19:41:49 +00001033
stephen hemminger7e80c122010-04-27 15:01:04 +00001034 if (slot)
1035 hlist_add_after_rcu(slot, &port->rlist);
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001036 else
1037 hlist_add_head_rcu(&port->rlist, &br->router_list);
Herbert Xu0909e112010-02-27 19:41:49 +00001038}
1039
Herbert Xueb1d1642010-02-27 19:41:45 +00001040static void br_multicast_mark_router(struct net_bridge *br,
1041 struct net_bridge_port *port)
1042{
1043 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +00001044
1045 if (!port) {
1046 if (br->multicast_router == 1)
1047 mod_timer(&br->multicast_router_timer,
1048 now + br->multicast_querier_interval);
1049 return;
1050 }
1051
1052 if (port->multicast_router != 1)
1053 return;
1054
1055 if (!hlist_unhashed(&port->rlist))
1056 goto timer;
1057
Herbert Xu0909e112010-02-27 19:41:49 +00001058 br_multicast_add_router(br, port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001059
1060timer:
1061 mod_timer(&port->multicast_router_timer,
1062 now + br->multicast_querier_interval);
1063}
1064
1065static void br_multicast_query_received(struct net_bridge *br,
1066 struct net_bridge_port *port,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001067 int saddr)
Herbert Xueb1d1642010-02-27 19:41:45 +00001068{
1069 if (saddr)
1070 mod_timer(&br->multicast_querier_timer,
1071 jiffies + br->multicast_querier_interval);
1072 else if (timer_pending(&br->multicast_querier_timer))
1073 return;
1074
1075 br_multicast_mark_router(br, port);
1076}
1077
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001078static int br_ip4_multicast_query(struct net_bridge *br,
1079 struct net_bridge_port *port,
1080 struct sk_buff *skb)
Herbert Xueb1d1642010-02-27 19:41:45 +00001081{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001082 const struct iphdr *iph = ip_hdr(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001083 struct igmphdr *ih = igmp_hdr(skb);
1084 struct net_bridge_mdb_entry *mp;
1085 struct igmpv3_query *ih3;
1086 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +00001087 struct net_bridge_port_group __rcu **pp;
Herbert Xueb1d1642010-02-27 19:41:45 +00001088 unsigned long max_delay;
1089 unsigned long now = jiffies;
1090 __be32 group;
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001091 int err = 0;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001092 u16 vid = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001093
1094 spin_lock(&br->multicast_lock);
1095 if (!netif_running(br->dev) ||
1096 (port && port->state == BR_STATE_DISABLED))
1097 goto out;
1098
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001099 br_multicast_query_received(br, port, !!iph->saddr);
Herbert Xueb1d1642010-02-27 19:41:45 +00001100
1101 group = ih->group;
1102
1103 if (skb->len == sizeof(*ih)) {
1104 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1105
1106 if (!max_delay) {
1107 max_delay = 10 * HZ;
1108 group = 0;
1109 }
1110 } else {
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001111 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) {
1112 err = -EINVAL;
1113 goto out;
1114 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001115
1116 ih3 = igmpv3_query_hdr(skb);
1117 if (ih3->nsrcs)
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001118 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001119
YOSHIFUJI Hideaki / 吉藤英明0ba8c9e2010-03-15 19:27:00 +00001120 max_delay = ih3->code ?
1121 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
Herbert Xueb1d1642010-02-27 19:41:45 +00001122 }
1123
1124 if (!group)
1125 goto out;
1126
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001127 br_vlan_get_tag(skb, &vid);
1128 mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001129 if (!mp)
1130 goto out;
1131
1132 max_delay *= br->multicast_last_member_count;
1133
Herbert Xu8a870172011-02-12 01:05:42 -08001134 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001135 (timer_pending(&mp->timer) ?
1136 time_after(mp->timer.expires, now + max_delay) :
1137 try_to_del_timer_sync(&mp->timer) >= 0))
1138 mod_timer(&mp->timer, now + max_delay);
1139
Eric Dumazete8051682010-11-15 06:38:10 +00001140 for (pp = &mp->ports;
1141 (p = mlock_dereference(*pp, br)) != NULL;
1142 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001143 if (timer_pending(&p->timer) ?
1144 time_after(p->timer.expires, now + max_delay) :
1145 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001146 mod_timer(&p->timer, now + max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001147 }
1148
1149out:
1150 spin_unlock(&br->multicast_lock);
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001151 return err;
Herbert Xueb1d1642010-02-27 19:41:45 +00001152}
1153
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001154#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001155static int br_ip6_multicast_query(struct net_bridge *br,
1156 struct net_bridge_port *port,
1157 struct sk_buff *skb)
1158{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001159 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
stephen hemmingereca2a432012-12-13 06:51:28 +00001160 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001161 struct net_bridge_mdb_entry *mp;
1162 struct mld2_query *mld2q;
Eric Dumazete8051682010-11-15 06:38:10 +00001163 struct net_bridge_port_group *p;
1164 struct net_bridge_port_group __rcu **pp;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001165 unsigned long max_delay;
1166 unsigned long now = jiffies;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001167 const struct in6_addr *group = NULL;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001168 int err = 0;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001169 u16 vid = 0;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001170
1171 spin_lock(&br->multicast_lock);
1172 if (!netif_running(br->dev) ||
1173 (port && port->state == BR_STATE_DISABLED))
1174 goto out;
1175
1176 br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->saddr));
1177
1178 if (skb->len == sizeof(*mld)) {
1179 if (!pskb_may_pull(skb, sizeof(*mld))) {
1180 err = -EINVAL;
1181 goto out;
1182 }
1183 mld = (struct mld_msg *) icmp6_hdr(skb);
Li RongQing47152132012-07-09 23:56:12 +00001184 max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001185 if (max_delay)
1186 group = &mld->mld_mca;
1187 } else if (skb->len >= sizeof(*mld2q)) {
1188 if (!pskb_may_pull(skb, sizeof(*mld2q))) {
1189 err = -EINVAL;
1190 goto out;
1191 }
1192 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1193 if (!mld2q->mld2q_nsrcs)
1194 group = &mld2q->mld2q_mca;
Ang Way Chuang8fa45a72012-12-13 23:08:39 +00001195 max_delay = mld2q->mld2q_mrc ? MLDV2_MRC(ntohs(mld2q->mld2q_mrc)) : 1;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001196 }
1197
1198 if (!group)
1199 goto out;
1200
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001201 br_vlan_get_tag(skb, &vid);
1202 mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001203 if (!mp)
1204 goto out;
1205
1206 max_delay *= br->multicast_last_member_count;
Herbert Xu8a870172011-02-12 01:05:42 -08001207 if (mp->mglist &&
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001208 (timer_pending(&mp->timer) ?
1209 time_after(mp->timer.expires, now + max_delay) :
1210 try_to_del_timer_sync(&mp->timer) >= 0))
1211 mod_timer(&mp->timer, now + max_delay);
1212
Eric Dumazete8051682010-11-15 06:38:10 +00001213 for (pp = &mp->ports;
1214 (p = mlock_dereference(*pp, br)) != NULL;
1215 pp = &p->next) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001216 if (timer_pending(&p->timer) ?
1217 time_after(p->timer.expires, now + max_delay) :
1218 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001219 mod_timer(&p->timer, now + max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001220 }
1221
1222out:
1223 spin_unlock(&br->multicast_lock);
1224 return err;
1225}
1226#endif
1227
Herbert Xueb1d1642010-02-27 19:41:45 +00001228static void br_multicast_leave_group(struct net_bridge *br,
1229 struct net_bridge_port *port,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001230 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +00001231{
1232 struct net_bridge_mdb_htable *mdb;
1233 struct net_bridge_mdb_entry *mp;
1234 struct net_bridge_port_group *p;
1235 unsigned long now;
1236 unsigned long time;
1237
Herbert Xueb1d1642010-02-27 19:41:45 +00001238 spin_lock(&br->multicast_lock);
1239 if (!netif_running(br->dev) ||
1240 (port && port->state == BR_STATE_DISABLED) ||
1241 timer_pending(&br->multicast_querier_timer))
1242 goto out;
1243
Eric Dumazete8051682010-11-15 06:38:10 +00001244 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001245 mp = br_mdb_ip_get(mdb, group);
1246 if (!mp)
1247 goto out;
1248
David S. Millerc2d3bab2012-12-05 16:24:45 -05001249 if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
Amerigo Wang50426b52012-12-03 23:56:40 +00001250 struct net_bridge_port_group __rcu **pp;
1251
1252 for (pp = &mp->ports;
1253 (p = mlock_dereference(*pp, br)) != NULL;
1254 pp = &p->next) {
1255 if (p->port != port)
1256 continue;
1257
1258 rcu_assign_pointer(*pp, p->next);
1259 hlist_del_init(&p->mglist);
1260 del_timer(&p->timer);
1261 call_rcu_bh(&p->rcu, br_multicast_free_pg);
Cong Wang37a393b2012-12-11 22:23:07 +00001262 br_mdb_notify(br->dev, port, group, RTM_DELMDB);
Amerigo Wang50426b52012-12-03 23:56:40 +00001263
1264 if (!mp->ports && !mp->mglist &&
1265 netif_running(br->dev))
1266 mod_timer(&mp->timer, jiffies);
1267 }
1268 goto out;
1269 }
1270
Herbert Xueb1d1642010-02-27 19:41:45 +00001271 now = jiffies;
1272 time = now + br->multicast_last_member_count *
1273 br->multicast_last_member_interval;
1274
1275 if (!port) {
Herbert Xu8a870172011-02-12 01:05:42 -08001276 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001277 (timer_pending(&mp->timer) ?
1278 time_after(mp->timer.expires, time) :
1279 try_to_del_timer_sync(&mp->timer) >= 0)) {
1280 mod_timer(&mp->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +00001281 }
1282
1283 goto out;
1284 }
1285
Eric Dumazete8051682010-11-15 06:38:10 +00001286 for (p = mlock_dereference(mp->ports, br);
1287 p != NULL;
1288 p = mlock_dereference(p->next, br)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001289 if (p->port != port)
1290 continue;
1291
1292 if (!hlist_unhashed(&p->mglist) &&
1293 (timer_pending(&p->timer) ?
1294 time_after(p->timer.expires, time) :
1295 try_to_del_timer_sync(&p->timer) >= 0)) {
1296 mod_timer(&p->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +00001297 }
1298
1299 break;
1300 }
1301
1302out:
1303 spin_unlock(&br->multicast_lock);
1304}
1305
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001306static void br_ip4_multicast_leave_group(struct net_bridge *br,
1307 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001308 __be32 group,
1309 __u16 vid)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001310{
1311 struct br_ip br_group;
1312
1313 if (ipv4_is_local_multicast(group))
1314 return;
1315
1316 br_group.u.ip4 = group;
1317 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001318 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001319
1320 br_multicast_leave_group(br, port, &br_group);
1321}
1322
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001323#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001324static void br_ip6_multicast_leave_group(struct net_bridge *br,
1325 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001326 const struct in6_addr *group,
1327 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001328{
1329 struct br_ip br_group;
1330
Linus Lüssinge4de9f92011-02-15 13:19:21 +00001331 if (!ipv6_is_transient_multicast(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001332 return;
1333
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001334 br_group.u.ip6 = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001335 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001336 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001337
1338 br_multicast_leave_group(br, port, &br_group);
1339}
1340#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001341
Herbert Xueb1d1642010-02-27 19:41:45 +00001342static int br_multicast_ipv4_rcv(struct net_bridge *br,
1343 struct net_bridge_port *port,
1344 struct sk_buff *skb)
1345{
1346 struct sk_buff *skb2 = skb;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001347 const struct iphdr *iph;
Herbert Xueb1d1642010-02-27 19:41:45 +00001348 struct igmphdr *ih;
Eric Dumazet95c96172012-04-15 05:58:06 +00001349 unsigned int len;
1350 unsigned int offset;
Herbert Xueb1d1642010-02-27 19:41:45 +00001351 int err;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001352 u16 vid = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001353
Herbert Xueb1d1642010-02-27 19:41:45 +00001354 /* We treat OOM as packet loss for now. */
1355 if (!pskb_may_pull(skb, sizeof(*iph)))
1356 return -EINVAL;
1357
1358 iph = ip_hdr(skb);
1359
1360 if (iph->ihl < 5 || iph->version != 4)
1361 return -EINVAL;
1362
1363 if (!pskb_may_pull(skb, ip_hdrlen(skb)))
1364 return -EINVAL;
1365
1366 iph = ip_hdr(skb);
1367
1368 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1369 return -EINVAL;
1370
Herbert Xubd4265f2011-06-23 02:39:12 +00001371 if (iph->protocol != IPPROTO_IGMP) {
1372 if ((iph->daddr & IGMP_LOCAL_GROUP_MASK) != IGMP_LOCAL_GROUP)
1373 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Herbert Xueb1d1642010-02-27 19:41:45 +00001374 return 0;
Herbert Xubd4265f2011-06-23 02:39:12 +00001375 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001376
1377 len = ntohs(iph->tot_len);
1378 if (skb->len < len || len < ip_hdrlen(skb))
1379 return -EINVAL;
1380
1381 if (skb->len > len) {
1382 skb2 = skb_clone(skb, GFP_ATOMIC);
1383 if (!skb2)
1384 return -ENOMEM;
1385
1386 err = pskb_trim_rcsum(skb2, len);
1387 if (err)
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001388 goto err_out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001389 }
1390
1391 len -= ip_hdrlen(skb2);
1392 offset = skb_network_offset(skb2) + ip_hdrlen(skb2);
1393 __skb_pull(skb2, offset);
1394 skb_reset_transport_header(skb2);
1395
1396 err = -EINVAL;
1397 if (!pskb_may_pull(skb2, sizeof(*ih)))
1398 goto out;
1399
Herbert Xueb1d1642010-02-27 19:41:45 +00001400 switch (skb2->ip_summed) {
1401 case CHECKSUM_COMPLETE:
1402 if (!csum_fold(skb2->csum))
1403 break;
1404 /* fall through */
1405 case CHECKSUM_NONE:
1406 skb2->csum = 0;
1407 if (skb_checksum_complete(skb2))
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001408 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001409 }
1410
1411 err = 0;
1412
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001413 br_vlan_get_tag(skb2, &vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001414 BR_INPUT_SKB_CB(skb)->igmp = 1;
1415 ih = igmp_hdr(skb2);
1416
1417 switch (ih->type) {
1418 case IGMP_HOST_MEMBERSHIP_REPORT:
1419 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Fernando Luis Vázquez Cao62b2bcb2011-06-13 15:04:43 +00001420 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001421 err = br_ip4_multicast_add_group(br, port, ih->group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001422 break;
1423 case IGMPV3_HOST_MEMBERSHIP_REPORT:
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001424 err = br_ip4_multicast_igmp3_report(br, port, skb2);
Herbert Xueb1d1642010-02-27 19:41:45 +00001425 break;
1426 case IGMP_HOST_MEMBERSHIP_QUERY:
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001427 err = br_ip4_multicast_query(br, port, skb2);
Herbert Xueb1d1642010-02-27 19:41:45 +00001428 break;
1429 case IGMP_HOST_LEAVE_MESSAGE:
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001430 br_ip4_multicast_leave_group(br, port, ih->group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001431 break;
1432 }
1433
1434out:
1435 __skb_push(skb2, offset);
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001436err_out:
Herbert Xueb1d1642010-02-27 19:41:45 +00001437 if (skb2 != skb)
1438 kfree_skb(skb2);
1439 return err;
1440}
1441
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001442#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001443static int br_multicast_ipv6_rcv(struct net_bridge *br,
1444 struct net_bridge_port *port,
1445 struct sk_buff *skb)
1446{
Tomas Winkler9d890812011-01-03 11:26:08 -08001447 struct sk_buff *skb2;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001448 const struct ipv6hdr *ip6h;
Eric Dumazet22df1332011-08-23 19:57:05 +00001449 u8 icmp6_type;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001450 u8 nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -08001451 __be16 frag_off;
Eric Dumazet95c96172012-04-15 05:58:06 +00001452 unsigned int len;
Kulikov Vasiliybb7a0bd2010-07-15 08:47:33 +00001453 int offset;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001454 int err;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001455 u16 vid = 0;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001456
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001457 if (!pskb_may_pull(skb, sizeof(*ip6h)))
1458 return -EINVAL;
1459
1460 ip6h = ipv6_hdr(skb);
1461
1462 /*
1463 * We're interested in MLD messages only.
1464 * - Version is 6
1465 * - MLD has always Router Alert hop-by-hop option
1466 * - But we do not support jumbrograms.
1467 */
1468 if (ip6h->version != 6 ||
1469 ip6h->nexthdr != IPPROTO_HOPOPTS ||
1470 ip6h->payload_len == 0)
1471 return 0;
1472
Linus Lüssingff9a57a2011-03-26 20:27:24 +00001473 len = ntohs(ip6h->payload_len) + sizeof(*ip6h);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001474 if (skb->len < len)
1475 return -EINVAL;
1476
1477 nexthdr = ip6h->nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -08001478 offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr, &frag_off);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001479
1480 if (offset < 0 || nexthdr != IPPROTO_ICMPV6)
1481 return 0;
1482
1483 /* Okay, we found ICMPv6 header */
1484 skb2 = skb_clone(skb, GFP_ATOMIC);
1485 if (!skb2)
1486 return -ENOMEM;
1487
Tomas Winkler9d890812011-01-03 11:26:08 -08001488 err = -EINVAL;
1489 if (!pskb_may_pull(skb2, offset + sizeof(struct icmp6hdr)))
1490 goto out;
1491
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001492 len -= offset - skb_network_offset(skb2);
1493
1494 __skb_pull(skb2, offset);
1495 skb_reset_transport_header(skb2);
stephen hemmingerfa2da8c2011-11-15 08:09:14 +00001496 skb_postpull_rcsum(skb2, skb_network_header(skb2),
1497 skb_network_header_len(skb2));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001498
Eric Dumazet22df1332011-08-23 19:57:05 +00001499 icmp6_type = icmp6_hdr(skb2)->icmp6_type;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001500
Eric Dumazet22df1332011-08-23 19:57:05 +00001501 switch (icmp6_type) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001502 case ICMPV6_MGM_QUERY:
1503 case ICMPV6_MGM_REPORT:
1504 case ICMPV6_MGM_REDUCTION:
1505 case ICMPV6_MLD2_REPORT:
1506 break;
1507 default:
1508 err = 0;
1509 goto out;
1510 }
1511
1512 /* Okay, we found MLD message. Check further. */
1513 if (skb2->len > len) {
1514 err = pskb_trim_rcsum(skb2, len);
1515 if (err)
1516 goto out;
Yan, Zheng4b275d72011-08-23 22:54:33 +00001517 err = -EINVAL;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001518 }
1519
Yan, Zheng4b275d72011-08-23 22:54:33 +00001520 ip6h = ipv6_hdr(skb2);
1521
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001522 switch (skb2->ip_summed) {
1523 case CHECKSUM_COMPLETE:
Yan, Zheng4b275d72011-08-23 22:54:33 +00001524 if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, skb2->len,
1525 IPPROTO_ICMPV6, skb2->csum))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001526 break;
1527 /*FALLTHROUGH*/
1528 case CHECKSUM_NONE:
Yan, Zheng4b275d72011-08-23 22:54:33 +00001529 skb2->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr,
1530 &ip6h->daddr,
1531 skb2->len,
1532 IPPROTO_ICMPV6, 0));
1533 if (__skb_checksum_complete(skb2))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001534 goto out;
1535 }
1536
1537 err = 0;
1538
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001539 br_vlan_get_tag(skb, &vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001540 BR_INPUT_SKB_CB(skb)->igmp = 1;
1541
Eric Dumazet22df1332011-08-23 19:57:05 +00001542 switch (icmp6_type) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001543 case ICMPV6_MGM_REPORT:
1544 {
Tomas Winkler9d890812011-01-03 11:26:08 -08001545 struct mld_msg *mld;
1546 if (!pskb_may_pull(skb2, sizeof(*mld))) {
1547 err = -EINVAL;
1548 goto out;
1549 }
1550 mld = (struct mld_msg *)skb_transport_header(skb2);
Fernando Luis Vázquez Caofc2af6c2011-06-13 15:06:58 +00001551 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001552 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001553 break;
1554 }
1555 case ICMPV6_MLD2_REPORT:
1556 err = br_ip6_multicast_mld2_report(br, port, skb2);
1557 break;
1558 case ICMPV6_MGM_QUERY:
1559 err = br_ip6_multicast_query(br, port, skb2);
1560 break;
1561 case ICMPV6_MGM_REDUCTION:
1562 {
Tomas Winkler9d890812011-01-03 11:26:08 -08001563 struct mld_msg *mld;
1564 if (!pskb_may_pull(skb2, sizeof(*mld))) {
1565 err = -EINVAL;
1566 goto out;
1567 }
1568 mld = (struct mld_msg *)skb_transport_header(skb2);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001569 br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001570 }
1571 }
1572
1573out:
Tomas Winkler9d890812011-01-03 11:26:08 -08001574 kfree_skb(skb2);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001575 return err;
1576}
1577#endif
1578
Herbert Xueb1d1642010-02-27 19:41:45 +00001579int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1580 struct sk_buff *skb)
1581{
YOSHIFUJI Hideaki / 吉藤英明1fafc7a2010-04-25 08:06:40 +00001582 BR_INPUT_SKB_CB(skb)->igmp = 0;
1583 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1584
Herbert Xueb1d1642010-02-27 19:41:45 +00001585 if (br->multicast_disabled)
1586 return 0;
1587
1588 switch (skb->protocol) {
1589 case htons(ETH_P_IP):
1590 return br_multicast_ipv4_rcv(br, port, skb);
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001591#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001592 case htons(ETH_P_IPV6):
1593 return br_multicast_ipv6_rcv(br, port, skb);
1594#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001595 }
1596
1597 return 0;
1598}
1599
1600static void br_multicast_query_expired(unsigned long data)
1601{
1602 struct net_bridge *br = (void *)data;
1603
1604 spin_lock(&br->multicast_lock);
1605 if (br->multicast_startup_queries_sent <
1606 br->multicast_startup_query_count)
1607 br->multicast_startup_queries_sent++;
1608
1609 br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent);
1610
1611 spin_unlock(&br->multicast_lock);
1612}
1613
1614void br_multicast_init(struct net_bridge *br)
1615{
1616 br->hash_elasticity = 4;
1617 br->hash_max = 512;
1618
1619 br->multicast_router = 1;
Herbert Xuc5c23262012-04-13 02:37:42 +00001620 br->multicast_querier = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001621 br->multicast_last_member_count = 2;
1622 br->multicast_startup_query_count = 2;
1623
1624 br->multicast_last_member_interval = HZ;
1625 br->multicast_query_response_interval = 10 * HZ;
1626 br->multicast_startup_query_interval = 125 * HZ / 4;
1627 br->multicast_query_interval = 125 * HZ;
1628 br->multicast_querier_interval = 255 * HZ;
1629 br->multicast_membership_interval = 260 * HZ;
1630
1631 spin_lock_init(&br->multicast_lock);
1632 setup_timer(&br->multicast_router_timer,
1633 br_multicast_local_router_expired, 0);
1634 setup_timer(&br->multicast_querier_timer,
Herbert Xubb63f1f2012-04-30 00:22:56 +00001635 br_multicast_querier_expired, (unsigned long)br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001636 setup_timer(&br->multicast_query_timer, br_multicast_query_expired,
1637 (unsigned long)br);
1638}
1639
1640void br_multicast_open(struct net_bridge *br)
1641{
1642 br->multicast_startup_queries_sent = 0;
1643
1644 if (br->multicast_disabled)
1645 return;
1646
1647 mod_timer(&br->multicast_query_timer, jiffies);
1648}
1649
1650void br_multicast_stop(struct net_bridge *br)
1651{
1652 struct net_bridge_mdb_htable *mdb;
1653 struct net_bridge_mdb_entry *mp;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001654 struct hlist_node *n;
Herbert Xueb1d1642010-02-27 19:41:45 +00001655 u32 ver;
1656 int i;
1657
1658 del_timer_sync(&br->multicast_router_timer);
1659 del_timer_sync(&br->multicast_querier_timer);
1660 del_timer_sync(&br->multicast_query_timer);
1661
1662 spin_lock_bh(&br->multicast_lock);
Eric Dumazete8051682010-11-15 06:38:10 +00001663 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001664 if (!mdb)
1665 goto out;
1666
1667 br->mdb = NULL;
1668
1669 ver = mdb->ver;
1670 for (i = 0; i < mdb->max; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001671 hlist_for_each_entry_safe(mp, n, &mdb->mhash[i],
Herbert Xueb1d1642010-02-27 19:41:45 +00001672 hlist[ver]) {
1673 del_timer(&mp->timer);
Herbert Xueb1d1642010-02-27 19:41:45 +00001674 call_rcu_bh(&mp->rcu, br_multicast_free_group);
1675 }
1676 }
1677
1678 if (mdb->old) {
1679 spin_unlock_bh(&br->multicast_lock);
Herbert Xu10cc2b52010-03-05 21:03:35 +00001680 rcu_barrier_bh();
Herbert Xueb1d1642010-02-27 19:41:45 +00001681 spin_lock_bh(&br->multicast_lock);
1682 WARN_ON(mdb->old);
1683 }
1684
1685 mdb->old = mdb;
1686 call_rcu_bh(&mdb->rcu, br_mdb_free);
1687
1688out:
1689 spin_unlock_bh(&br->multicast_lock);
1690}
Herbert Xu0909e112010-02-27 19:41:49 +00001691
1692int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1693{
1694 int err = -ENOENT;
1695
1696 spin_lock_bh(&br->multicast_lock);
1697 if (!netif_running(br->dev))
1698 goto unlock;
1699
1700 switch (val) {
1701 case 0:
1702 case 2:
1703 del_timer(&br->multicast_router_timer);
1704 /* fall through */
1705 case 1:
1706 br->multicast_router = val;
1707 err = 0;
1708 break;
1709
1710 default:
1711 err = -EINVAL;
1712 break;
1713 }
1714
1715unlock:
1716 spin_unlock_bh(&br->multicast_lock);
1717
1718 return err;
1719}
1720
1721int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1722{
1723 struct net_bridge *br = p->br;
1724 int err = -ENOENT;
1725
1726 spin_lock(&br->multicast_lock);
1727 if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED)
1728 goto unlock;
1729
1730 switch (val) {
1731 case 0:
1732 case 1:
1733 case 2:
1734 p->multicast_router = val;
1735 err = 0;
1736
1737 if (val < 2 && !hlist_unhashed(&p->rlist))
1738 hlist_del_init_rcu(&p->rlist);
1739
1740 if (val == 1)
1741 break;
1742
1743 del_timer(&p->multicast_router_timer);
1744
1745 if (val == 0)
1746 break;
1747
1748 br_multicast_add_router(br, p);
1749 break;
1750
1751 default:
1752 err = -EINVAL;
1753 break;
1754 }
1755
1756unlock:
1757 spin_unlock(&br->multicast_lock);
1758
1759 return err;
1760}
Herbert Xu561f1102010-02-27 19:41:50 +00001761
Herbert Xu74857212012-04-13 02:37:42 +00001762static void br_multicast_start_querier(struct net_bridge *br)
Herbert Xu561f1102010-02-27 19:41:50 +00001763{
1764 struct net_bridge_port *port;
Herbert Xu74857212012-04-13 02:37:42 +00001765
1766 br_multicast_open(br);
1767
1768 list_for_each_entry(port, &br->port_list, list) {
1769 if (port->state == BR_STATE_DISABLED ||
1770 port->state == BR_STATE_BLOCKING)
1771 continue;
1772
1773 __br_multicast_enable_port(port);
1774 }
1775}
1776
1777int br_multicast_toggle(struct net_bridge *br, unsigned long val)
1778{
Herbert Xu3a7fda02010-07-29 00:45:30 +00001779 int err = 0;
Eric Dumazete8051682010-11-15 06:38:10 +00001780 struct net_bridge_mdb_htable *mdb;
Herbert Xu561f1102010-02-27 19:41:50 +00001781
Andrey Vaginef5e0d82011-11-10 05:48:03 +00001782 spin_lock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00001783 if (br->multicast_disabled == !val)
1784 goto unlock;
1785
1786 br->multicast_disabled = !val;
1787 if (br->multicast_disabled)
1788 goto unlock;
1789
Herbert Xu3a7fda02010-07-29 00:45:30 +00001790 if (!netif_running(br->dev))
1791 goto unlock;
1792
Eric Dumazete8051682010-11-15 06:38:10 +00001793 mdb = mlock_dereference(br->mdb, br);
1794 if (mdb) {
1795 if (mdb->old) {
Herbert Xu561f1102010-02-27 19:41:50 +00001796 err = -EEXIST;
1797rollback:
1798 br->multicast_disabled = !!val;
1799 goto unlock;
1800 }
1801
Eric Dumazete8051682010-11-15 06:38:10 +00001802 err = br_mdb_rehash(&br->mdb, mdb->max,
Herbert Xu561f1102010-02-27 19:41:50 +00001803 br->hash_elasticity);
1804 if (err)
1805 goto rollback;
1806 }
1807
Herbert Xu74857212012-04-13 02:37:42 +00001808 br_multicast_start_querier(br);
Herbert Xu561f1102010-02-27 19:41:50 +00001809
1810unlock:
Andrey Vaginef5e0d82011-11-10 05:48:03 +00001811 spin_unlock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00001812
1813 return err;
1814}
Herbert Xub1951672010-02-27 19:41:51 +00001815
Herbert Xuc5c23262012-04-13 02:37:42 +00001816int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
1817{
1818 val = !!val;
1819
1820 spin_lock_bh(&br->multicast_lock);
1821 if (br->multicast_querier == val)
1822 goto unlock;
1823
1824 br->multicast_querier = val;
1825 if (val)
1826 br_multicast_start_querier(br);
1827
1828unlock:
1829 spin_unlock_bh(&br->multicast_lock);
1830
1831 return 0;
1832}
1833
Herbert Xub1951672010-02-27 19:41:51 +00001834int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
1835{
1836 int err = -ENOENT;
1837 u32 old;
Eric Dumazete8051682010-11-15 06:38:10 +00001838 struct net_bridge_mdb_htable *mdb;
Herbert Xub1951672010-02-27 19:41:51 +00001839
1840 spin_lock(&br->multicast_lock);
1841 if (!netif_running(br->dev))
1842 goto unlock;
1843
1844 err = -EINVAL;
1845 if (!is_power_of_2(val))
1846 goto unlock;
Eric Dumazete8051682010-11-15 06:38:10 +00001847
1848 mdb = mlock_dereference(br->mdb, br);
1849 if (mdb && val < mdb->size)
Herbert Xub1951672010-02-27 19:41:51 +00001850 goto unlock;
1851
1852 err = 0;
1853
1854 old = br->hash_max;
1855 br->hash_max = val;
1856
Eric Dumazete8051682010-11-15 06:38:10 +00001857 if (mdb) {
1858 if (mdb->old) {
Herbert Xub1951672010-02-27 19:41:51 +00001859 err = -EEXIST;
1860rollback:
1861 br->hash_max = old;
1862 goto unlock;
1863 }
1864
1865 err = br_mdb_rehash(&br->mdb, br->hash_max,
1866 br->hash_elasticity);
1867 if (err)
1868 goto rollback;
1869 }
1870
1871unlock:
1872 spin_unlock(&br->multicast_lock);
1873
1874 return err;
1875}