blob: 37a467697967699e976cd481fe98ef25de5e36c2 [file] [log] [blame]
Herbert Xueb1d1642010-02-27 19:41:45 +00001/*
2 * Bridge multicast support.
3 *
4 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 */
12
13#include <linux/err.h>
14#include <linux/if_ether.h>
15#include <linux/igmp.h>
16#include <linux/jhash.h>
17#include <linux/kernel.h>
Herbert Xub1951672010-02-27 19:41:51 +000018#include <linux/log2.h>
Herbert Xueb1d1642010-02-27 19:41:45 +000019#include <linux/netdevice.h>
20#include <linux/netfilter_bridge.h>
21#include <linux/random.h>
22#include <linux/rculist.h>
23#include <linux/skbuff.h>
24#include <linux/slab.h>
25#include <linux/timer.h>
Cong Wang1c8ad5b2013-05-21 21:52:54 +000026#include <linux/inetdevice.h>
Herbert Xueb1d1642010-02-27 19:41:45 +000027#include <net/ip.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000028#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090029#include <net/ipv6.h>
30#include <net/mld.h>
David S. Millerd4c4f072010-04-27 10:16:54 -070031#include <net/ip6_checksum.h>
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090032#endif
Herbert Xueb1d1642010-02-27 19:41:45 +000033
34#include "br_private.h"
35
Herbert Xuc83b8fa2012-04-13 02:37:42 +000036static void br_multicast_start_querier(struct net_bridge *br);
Cong Wang2ce297f2012-12-10 02:15:35 +000037unsigned int br_mdb_rehash_seq;
Herbert Xuc83b8fa2012-04-13 02:37:42 +000038
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090039static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
40{
41 if (a->proto != b->proto)
42 return 0;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000043 if (a->vid != b->vid)
44 return 0;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090045 switch (a->proto) {
46 case htons(ETH_P_IP):
47 return a->u.ip4 == b->u.ip4;
Eric Dumazetdfd56b82011-12-10 09:48:31 +000048#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090049 case htons(ETH_P_IPV6):
50 return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
51#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090052 }
53 return 0;
54}
55
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000056static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip,
57 __u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +000058{
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000059 return jhash_2words((__force u32)ip, vid, mdb->secret) & (mdb->max - 1);
Herbert Xueb1d1642010-02-27 19:41:45 +000060}
61
Eric Dumazetdfd56b82011-12-10 09:48:31 +000062#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090063static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000064 const struct in6_addr *ip,
65 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090066{
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000067 return jhash_2words(ipv6_addr_hash(ip), vid,
68 mdb->secret) & (mdb->max - 1);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090069}
70#endif
71
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090072static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb,
73 struct br_ip *ip)
74{
75 switch (ip->proto) {
76 case htons(ETH_P_IP):
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000077 return __br_ip4_hash(mdb, ip->u.ip4, ip->vid);
Eric Dumazetdfd56b82011-12-10 09:48:31 +000078#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090079 case htons(ETH_P_IPV6):
Vlad Yasevichb0e9a302013-02-13 12:00:17 +000080 return __br_ip6_hash(mdb, &ip->u.ip6, ip->vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090081#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090082 }
83 return 0;
Herbert Xueb1d1642010-02-27 19:41:45 +000084}
85
86static struct net_bridge_mdb_entry *__br_mdb_ip_get(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090087 struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +000088{
89 struct net_bridge_mdb_entry *mp;
Herbert Xueb1d1642010-02-27 19:41:45 +000090
Sasha Levinb67bfe02013-02-27 17:06:00 -080091 hlist_for_each_entry_rcu(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090092 if (br_ip_equal(&mp->addr, dst))
Herbert Xueb1d1642010-02-27 19:41:45 +000093 return mp;
94 }
95
96 return NULL;
97}
98
Cong Wangcfd56752012-12-11 22:23:08 +000099struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge_mdb_htable *mdb,
100 struct br_ip *dst)
Herbert Xu7f285fa2010-07-05 14:50:08 +0000101{
102 if (!mdb)
103 return NULL;
104
105 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
106}
107
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900108static struct net_bridge_mdb_entry *br_mdb_ip4_get(
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000109 struct net_bridge_mdb_htable *mdb, __be32 dst, __u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000110{
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900111 struct br_ip br_dst;
Herbert Xu0821ec52010-03-15 20:38:25 -0700112
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900113 br_dst.u.ip4 = dst;
114 br_dst.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000115 br_dst.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900116
Herbert Xu7f285fa2010-07-05 14:50:08 +0000117 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900118}
119
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000120#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900121static struct net_bridge_mdb_entry *br_mdb_ip6_get(
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000122 struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst,
123 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900124{
125 struct br_ip br_dst;
126
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000127 br_dst.u.ip6 = *dst;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900128 br_dst.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000129 br_dst.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900130
Herbert Xu7f285fa2010-07-05 14:50:08 +0000131 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900132}
133#endif
134
Herbert Xueb1d1642010-02-27 19:41:45 +0000135struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
Cong Wangfbca58a2013-03-07 03:05:33 +0000136 struct sk_buff *skb, u16 vid)
Herbert Xueb1d1642010-02-27 19:41:45 +0000137{
Eric Dumazete8051682010-11-15 06:38:10 +0000138 struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900139 struct br_ip ip;
140
Herbert Xu7f285fa2010-07-05 14:50:08 +0000141 if (br->multicast_disabled)
Herbert Xueb1d1642010-02-27 19:41:45 +0000142 return NULL;
143
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900144 if (BR_INPUT_SKB_CB(skb)->igmp)
145 return NULL;
146
147 ip.proto = skb->protocol;
Cong Wangfbca58a2013-03-07 03:05:33 +0000148 ip.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900149
Herbert Xueb1d1642010-02-27 19:41:45 +0000150 switch (skb->protocol) {
151 case htons(ETH_P_IP):
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900152 ip.u.ip4 = ip_hdr(skb)->daddr;
153 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000154#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900155 case htons(ETH_P_IPV6):
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000156 ip.u.ip6 = ipv6_hdr(skb)->daddr;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900157 break;
158#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900159 default:
160 return NULL;
Herbert Xueb1d1642010-02-27 19:41:45 +0000161 }
162
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900163 return br_mdb_ip_get(mdb, &ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000164}
165
166static void br_mdb_free(struct rcu_head *head)
167{
168 struct net_bridge_mdb_htable *mdb =
169 container_of(head, struct net_bridge_mdb_htable, rcu);
170 struct net_bridge_mdb_htable *old = mdb->old;
171
172 mdb->old = NULL;
173 kfree(old->mhash);
174 kfree(old);
175}
176
177static int br_mdb_copy(struct net_bridge_mdb_htable *new,
178 struct net_bridge_mdb_htable *old,
179 int elasticity)
180{
181 struct net_bridge_mdb_entry *mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000182 int maxlen;
183 int len;
184 int i;
185
186 for (i = 0; i < old->max; i++)
Sasha Levinb67bfe02013-02-27 17:06:00 -0800187 hlist_for_each_entry(mp, &old->mhash[i], hlist[old->ver])
Herbert Xueb1d1642010-02-27 19:41:45 +0000188 hlist_add_head(&mp->hlist[new->ver],
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900189 &new->mhash[br_ip_hash(new, &mp->addr)]);
Herbert Xueb1d1642010-02-27 19:41:45 +0000190
191 if (!elasticity)
192 return 0;
193
194 maxlen = 0;
195 for (i = 0; i < new->max; i++) {
196 len = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800197 hlist_for_each_entry(mp, &new->mhash[i], hlist[new->ver])
Herbert Xueb1d1642010-02-27 19:41:45 +0000198 len++;
199 if (len > maxlen)
200 maxlen = len;
201 }
202
203 return maxlen > elasticity ? -EINVAL : 0;
204}
205
Cong Wangcfd56752012-12-11 22:23:08 +0000206void br_multicast_free_pg(struct rcu_head *head)
Herbert Xueb1d1642010-02-27 19:41:45 +0000207{
208 struct net_bridge_port_group *p =
209 container_of(head, struct net_bridge_port_group, rcu);
210
211 kfree(p);
212}
213
214static void br_multicast_free_group(struct rcu_head *head)
215{
216 struct net_bridge_mdb_entry *mp =
217 container_of(head, struct net_bridge_mdb_entry, rcu);
218
219 kfree(mp);
220}
221
222static void br_multicast_group_expired(unsigned long data)
223{
224 struct net_bridge_mdb_entry *mp = (void *)data;
225 struct net_bridge *br = mp->br;
226 struct net_bridge_mdb_htable *mdb;
227
228 spin_lock(&br->multicast_lock);
229 if (!netif_running(br->dev) || timer_pending(&mp->timer))
230 goto out;
231
Herbert Xu8a870172011-02-12 01:05:42 -0800232 mp->mglist = false;
Herbert Xueb1d1642010-02-27 19:41:45 +0000233
234 if (mp->ports)
235 goto out;
236
Eric Dumazete8051682010-11-15 06:38:10 +0000237 mdb = mlock_dereference(br->mdb, br);
238
Herbert Xueb1d1642010-02-27 19:41:45 +0000239 hlist_del_rcu(&mp->hlist[mdb->ver]);
240 mdb->size--;
241
Herbert Xueb1d1642010-02-27 19:41:45 +0000242 call_rcu_bh(&mp->rcu, br_multicast_free_group);
243
244out:
245 spin_unlock(&br->multicast_lock);
246}
247
248static void br_multicast_del_pg(struct net_bridge *br,
249 struct net_bridge_port_group *pg)
250{
Eric Dumazete8051682010-11-15 06:38:10 +0000251 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000252 struct net_bridge_mdb_entry *mp;
253 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +0000254 struct net_bridge_port_group __rcu **pp;
255
256 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +0000257
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900258 mp = br_mdb_ip_get(mdb, &pg->addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000259 if (WARN_ON(!mp))
260 return;
261
Eric Dumazete8051682010-11-15 06:38:10 +0000262 for (pp = &mp->ports;
263 (p = mlock_dereference(*pp, br)) != NULL;
264 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000265 if (p != pg)
266 continue;
267
stephen hemminger83f6a742010-04-27 15:01:06 +0000268 rcu_assign_pointer(*pp, p->next);
Herbert Xueb1d1642010-02-27 19:41:45 +0000269 hlist_del_init(&p->mglist);
270 del_timer(&p->timer);
Herbert Xueb1d1642010-02-27 19:41:45 +0000271 call_rcu_bh(&p->rcu, br_multicast_free_pg);
272
Herbert Xu8a870172011-02-12 01:05:42 -0800273 if (!mp->ports && !mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +0000274 netif_running(br->dev))
275 mod_timer(&mp->timer, jiffies);
276
277 return;
278 }
279
280 WARN_ON(1);
281}
282
283static void br_multicast_port_group_expired(unsigned long data)
284{
285 struct net_bridge_port_group *pg = (void *)data;
286 struct net_bridge *br = pg->port->br;
287
288 spin_lock(&br->multicast_lock);
289 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
Amerigo Wangccb1c312012-12-14 22:09:51 +0000290 hlist_unhashed(&pg->mglist) || pg->state & MDB_PERMANENT)
Herbert Xueb1d1642010-02-27 19:41:45 +0000291 goto out;
292
293 br_multicast_del_pg(br, pg);
294
295out:
296 spin_unlock(&br->multicast_lock);
297}
298
Eric Dumazete8051682010-11-15 06:38:10 +0000299static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max,
Herbert Xueb1d1642010-02-27 19:41:45 +0000300 int elasticity)
301{
Eric Dumazete8051682010-11-15 06:38:10 +0000302 struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000303 struct net_bridge_mdb_htable *mdb;
304 int err;
305
306 mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
307 if (!mdb)
308 return -ENOMEM;
309
310 mdb->max = max;
311 mdb->old = old;
312
313 mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
314 if (!mdb->mhash) {
315 kfree(mdb);
316 return -ENOMEM;
317 }
318
319 mdb->size = old ? old->size : 0;
320 mdb->ver = old ? old->ver ^ 1 : 0;
321
322 if (!old || elasticity)
323 get_random_bytes(&mdb->secret, sizeof(mdb->secret));
324 else
325 mdb->secret = old->secret;
326
327 if (!old)
328 goto out;
329
330 err = br_mdb_copy(mdb, old, elasticity);
331 if (err) {
332 kfree(mdb->mhash);
333 kfree(mdb);
334 return err;
335 }
336
Cong Wang2ce297f2012-12-10 02:15:35 +0000337 br_mdb_rehash_seq++;
Herbert Xueb1d1642010-02-27 19:41:45 +0000338 call_rcu_bh(&mdb->rcu, br_mdb_free);
339
340out:
341 rcu_assign_pointer(*mdbp, mdb);
342
343 return 0;
344}
345
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900346static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
347 __be32 group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000348{
349 struct sk_buff *skb;
350 struct igmphdr *ih;
351 struct ethhdr *eth;
352 struct iphdr *iph;
353
354 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
355 sizeof(*ih) + 4);
356 if (!skb)
357 goto out;
358
359 skb->protocol = htons(ETH_P_IP);
360
361 skb_reset_mac_header(skb);
362 eth = eth_hdr(skb);
363
364 memcpy(eth->h_source, br->dev->dev_addr, 6);
365 eth->h_dest[0] = 1;
366 eth->h_dest[1] = 0;
367 eth->h_dest[2] = 0x5e;
368 eth->h_dest[3] = 0;
369 eth->h_dest[4] = 0;
370 eth->h_dest[5] = 1;
371 eth->h_proto = htons(ETH_P_IP);
372 skb_put(skb, sizeof(*eth));
373
374 skb_set_network_header(skb, skb->len);
375 iph = ip_hdr(skb);
376
377 iph->version = 4;
378 iph->ihl = 6;
379 iph->tos = 0xc0;
380 iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4);
381 iph->id = 0;
382 iph->frag_off = htons(IP_DF);
383 iph->ttl = 1;
384 iph->protocol = IPPROTO_IGMP;
Cong Wang1c8ad5b2013-05-21 21:52:54 +0000385 iph->saddr = br->multicast_query_use_ifaddr ?
386 inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000387 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
388 ((u8 *)&iph[1])[0] = IPOPT_RA;
389 ((u8 *)&iph[1])[1] = 4;
390 ((u8 *)&iph[1])[2] = 0;
391 ((u8 *)&iph[1])[3] = 0;
392 ip_send_check(iph);
393 skb_put(skb, 24);
394
395 skb_set_transport_header(skb, skb->len);
396 ih = igmp_hdr(skb);
397 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
398 ih->code = (group ? br->multicast_last_member_interval :
399 br->multicast_query_response_interval) /
400 (HZ / IGMP_TIMER_SCALE);
401 ih->group = group;
402 ih->csum = 0;
403 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
404 skb_put(skb, sizeof(*ih));
405
406 __skb_pull(skb, sizeof(*eth));
407
408out:
409 return skb;
410}
411
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000412#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900413static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000414 const struct in6_addr *group)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900415{
416 struct sk_buff *skb;
417 struct ipv6hdr *ip6h;
418 struct mld_msg *mldq;
419 struct ethhdr *eth;
420 u8 *hopopt;
421 unsigned long interval;
422
423 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
424 8 + sizeof(*mldq));
425 if (!skb)
426 goto out;
427
428 skb->protocol = htons(ETH_P_IPV6);
429
430 /* Ethernet header */
431 skb_reset_mac_header(skb);
432 eth = eth_hdr(skb);
433
434 memcpy(eth->h_source, br->dev->dev_addr, 6);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900435 eth->h_proto = htons(ETH_P_IPV6);
436 skb_put(skb, sizeof(*eth));
437
438 /* IPv6 header + HbH option */
439 skb_set_network_header(skb, skb->len);
440 ip6h = ipv6_hdr(skb);
441
442 *(__force __be32 *)ip6h = htonl(0x60000000);
David Stevens76d6615862010-12-14 08:42:16 +0000443 ip6h->payload_len = htons(8 + sizeof(*mldq));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900444 ip6h->nexthdr = IPPROTO_HOPOPTS;
445 ip6h->hop_limit = 1;
Linus Lüssinga7bff752011-03-22 11:40:32 +0000446 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
Ulrich Weberd1d81d42012-03-05 04:52:44 +0000447 if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
448 &ip6h->saddr)) {
449 kfree_skb(skb);
450 return NULL;
451 }
Linus Lüssing36cff5a2011-02-17 08:17:51 +0000452 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900453
454 hopopt = (u8 *)(ip6h + 1);
455 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
456 hopopt[1] = 0; /* length of HbH */
457 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
458 hopopt[3] = 2; /* Length of RA Option */
459 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
460 hopopt[5] = 0;
Eldad Zack1de5a712012-05-17 06:00:25 +0000461 hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */
462 hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900463
464 skb_put(skb, sizeof(*ip6h) + 8);
465
466 /* ICMPv6 */
467 skb_set_transport_header(skb, skb->len);
468 mldq = (struct mld_msg *) icmp6_hdr(skb);
469
470 interval = ipv6_addr_any(group) ? br->multicast_last_member_interval :
471 br->multicast_query_response_interval;
472
473 mldq->mld_type = ICMPV6_MGM_QUERY;
474 mldq->mld_code = 0;
475 mldq->mld_cksum = 0;
476 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
477 mldq->mld_reserved = 0;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000478 mldq->mld_mca = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900479
480 /* checksum */
481 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
482 sizeof(*mldq), IPPROTO_ICMPV6,
483 csum_partial(mldq,
484 sizeof(*mldq), 0));
485 skb_put(skb, sizeof(*mldq));
486
487 __skb_pull(skb, sizeof(*eth));
488
489out:
490 return skb;
491}
492#endif
493
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900494static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
495 struct br_ip *addr)
496{
497 switch (addr->proto) {
498 case htons(ETH_P_IP):
499 return br_ip4_multicast_alloc_query(br, addr->u.ip4);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000500#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900501 case htons(ETH_P_IPV6):
502 return br_ip6_multicast_alloc_query(br, &addr->u.ip6);
503#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900504 }
505 return NULL;
506}
507
Herbert Xueb1d1642010-02-27 19:41:45 +0000508static struct net_bridge_mdb_entry *br_multicast_get_group(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900509 struct net_bridge *br, struct net_bridge_port *port,
510 struct br_ip *group, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +0000511{
Eric Dumazete8051682010-11-15 06:38:10 +0000512 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000513 struct net_bridge_mdb_entry *mp;
Eric Dumazet95c96172012-04-15 05:58:06 +0000514 unsigned int count = 0;
515 unsigned int max;
Herbert Xueb1d1642010-02-27 19:41:45 +0000516 int elasticity;
517 int err;
518
Eric Dumazete8051682010-11-15 06:38:10 +0000519 mdb = rcu_dereference_protected(br->mdb, 1);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800520 hlist_for_each_entry(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000521 count++;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900522 if (unlikely(br_ip_equal(group, &mp->addr)))
Herbert Xueb1d1642010-02-27 19:41:45 +0000523 return mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000524 }
525
526 elasticity = 0;
527 max = mdb->max;
528
529 if (unlikely(count > br->hash_elasticity && count)) {
530 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000531 br_info(br, "Multicast hash table "
532 "chain limit reached: %s\n",
533 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000534
535 elasticity = br->hash_elasticity;
536 }
537
538 if (mdb->size >= max) {
539 max *= 2;
Thomas Graf036be6d2012-07-10 22:29:19 +0000540 if (unlikely(max > br->hash_max)) {
541 br_warn(br, "Multicast hash table maximum of %d "
542 "reached, disabling snooping: %s\n",
543 br->hash_max,
544 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000545 err = -E2BIG;
546disable:
547 br->multicast_disabled = 1;
548 goto err;
549 }
550 }
551
552 if (max > mdb->max || elasticity) {
553 if (mdb->old) {
554 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000555 br_info(br, "Multicast hash table "
556 "on fire: %s\n",
557 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000558 err = -EEXIST;
559 goto err;
560 }
561
562 err = br_mdb_rehash(&br->mdb, max, elasticity);
563 if (err) {
stephen hemminger28a16c92010-05-10 09:31:09 +0000564 br_warn(br, "Cannot rehash multicast "
565 "hash table, disabling snooping: %s, %d, %d\n",
566 port ? port->dev->name : br->dev->name,
567 mdb->size, err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000568 goto disable;
569 }
570
571 err = -EAGAIN;
572 goto err;
573 }
574
575 return NULL;
576
577err:
578 mp = ERR_PTR(err);
579 return mp;
580}
581
Cong Wangcfd56752012-12-11 22:23:08 +0000582struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
583 struct net_bridge_port *port, struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000584{
Eric Dumazete8051682010-11-15 06:38:10 +0000585 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000586 struct net_bridge_mdb_entry *mp;
587 int hash;
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000588 int err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000589
Eric Dumazete8051682010-11-15 06:38:10 +0000590 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000591 if (!mdb) {
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000592 err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0);
593 if (err)
594 return ERR_PTR(err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000595 goto rehash;
596 }
597
598 hash = br_ip_hash(mdb, group);
599 mp = br_multicast_get_group(br, port, group, hash);
600 switch (PTR_ERR(mp)) {
601 case 0:
602 break;
603
604 case -EAGAIN:
605rehash:
Eric Dumazete8051682010-11-15 06:38:10 +0000606 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000607 hash = br_ip_hash(mdb, group);
608 break;
609
610 default:
611 goto out;
612 }
613
614 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
615 if (unlikely(!mp))
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000616 return ERR_PTR(-ENOMEM);
Herbert Xueb1d1642010-02-27 19:41:45 +0000617
618 mp->br = br;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900619 mp->addr = *group;
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 int err;
658
Herbert Xueb1d1642010-02-27 19:41:45 +0000659 spin_lock(&br->multicast_lock);
660 if (!netif_running(br->dev) ||
661 (port && port->state == BR_STATE_DISABLED))
662 goto out;
663
664 mp = br_multicast_new_group(br, port, group);
665 err = PTR_ERR(mp);
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000666 if (IS_ERR(mp))
Herbert Xueb1d1642010-02-27 19:41:45 +0000667 goto err;
668
669 if (!port) {
Herbert Xu8a870172011-02-12 01:05:42 -0800670 mp->mglist = true;
Herbert Xueb1d1642010-02-27 19:41:45 +0000671 goto out;
672 }
673
Eric Dumazete8051682010-11-15 06:38:10 +0000674 for (pp = &mp->ports;
675 (p = mlock_dereference(*pp, br)) != NULL;
676 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000677 if (p->port == port)
Cong Wang9f00b2e2013-05-21 21:52:55 +0000678 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +0000679 if ((unsigned long)p->port < (unsigned long)port)
680 break;
681 }
682
Amerigo Wangccb1c312012-12-14 22:09:51 +0000683 p = br_multicast_new_port_group(port, group, *pp, MDB_TEMPORARY);
Herbert Xueb1d1642010-02-27 19:41:45 +0000684 if (unlikely(!p))
685 goto err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000686 rcu_assign_pointer(*pp, p);
Cong Wang37a393b2012-12-11 22:23:07 +0000687 br_mdb_notify(br->dev, port, group, RTM_NEWMDB);
Herbert Xueb1d1642010-02-27 19:41:45 +0000688
Herbert Xueb1d1642010-02-27 19:41:45 +0000689out:
690 err = 0;
691
692err:
693 spin_unlock(&br->multicast_lock);
694 return err;
695}
696
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900697static int br_ip4_multicast_add_group(struct net_bridge *br,
698 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000699 __be32 group,
700 __u16 vid)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900701{
702 struct br_ip br_group;
703
704 if (ipv4_is_local_multicast(group))
705 return 0;
706
707 br_group.u.ip4 = group;
708 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000709 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900710
711 return br_multicast_add_group(br, port, &br_group);
712}
713
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000714#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900715static int br_ip6_multicast_add_group(struct net_bridge *br,
716 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000717 const struct in6_addr *group,
718 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900719{
720 struct br_ip br_group;
721
Linus Lüssinge4de9f92011-02-15 13:19:21 +0000722 if (!ipv6_is_transient_multicast(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900723 return 0;
724
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000725 br_group.u.ip6 = *group;
Linus Lüssing9cc6e0c2011-02-15 13:19:17 +0000726 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000727 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900728
729 return br_multicast_add_group(br, port, &br_group);
730}
731#endif
732
Herbert Xueb1d1642010-02-27 19:41:45 +0000733static void br_multicast_router_expired(unsigned long data)
734{
735 struct net_bridge_port *port = (void *)data;
736 struct net_bridge *br = port->br;
737
738 spin_lock(&br->multicast_lock);
739 if (port->multicast_router != 1 ||
740 timer_pending(&port->multicast_router_timer) ||
741 hlist_unhashed(&port->rlist))
742 goto out;
743
744 hlist_del_init_rcu(&port->rlist);
745
746out:
747 spin_unlock(&br->multicast_lock);
748}
749
750static void br_multicast_local_router_expired(unsigned long data)
751{
752}
753
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000754static void br_multicast_querier_expired(unsigned long data)
755{
Herbert Xubb63f1f2012-04-30 00:22:56 +0000756 struct net_bridge *br = (void *)data;
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000757
758 spin_lock(&br->multicast_lock);
759 if (!netif_running(br->dev) || br->multicast_disabled)
760 goto out;
761
762 br_multicast_start_querier(br);
763
764out:
765 spin_unlock(&br->multicast_lock);
766}
767
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900768static void __br_multicast_send_query(struct net_bridge *br,
769 struct net_bridge_port *port,
770 struct br_ip *ip)
Herbert Xueb1d1642010-02-27 19:41:45 +0000771{
Herbert Xueb1d1642010-02-27 19:41:45 +0000772 struct sk_buff *skb;
773
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900774 skb = br_multicast_alloc_query(br, ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000775 if (!skb)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900776 return;
Herbert Xueb1d1642010-02-27 19:41:45 +0000777
778 if (port) {
779 __skb_push(skb, sizeof(struct ethhdr));
780 skb->dev = port->dev;
Jan Engelhardt713aefa2010-03-23 04:07:21 +0100781 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
Herbert Xueb1d1642010-02-27 19:41:45 +0000782 dev_queue_xmit);
783 } else
784 netif_rx(skb);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900785}
Herbert Xueb1d1642010-02-27 19:41:45 +0000786
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900787static void br_multicast_send_query(struct net_bridge *br,
788 struct net_bridge_port *port, u32 sent)
789{
790 unsigned long time;
791 struct br_ip br_group;
792
793 if (!netif_running(br->dev) || br->multicast_disabled ||
Herbert Xuc5c23262012-04-13 02:37:42 +0000794 !br->multicast_querier ||
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900795 timer_pending(&br->multicast_querier_timer))
796 return;
797
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900798 memset(&br_group.u, 0, sizeof(br_group.u));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900799
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900800 br_group.proto = htons(ETH_P_IP);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900801 __br_multicast_send_query(br, port, &br_group);
802
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000803#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900804 br_group.proto = htons(ETH_P_IPV6);
805 __br_multicast_send_query(br, port, &br_group);
806#endif
807
Herbert Xueb1d1642010-02-27 19:41:45 +0000808 time = jiffies;
809 time += sent < br->multicast_startup_query_count ?
810 br->multicast_startup_query_interval :
811 br->multicast_query_interval;
812 mod_timer(port ? &port->multicast_query_timer :
813 &br->multicast_query_timer, time);
814}
815
816static void br_multicast_port_query_expired(unsigned long data)
817{
818 struct net_bridge_port *port = (void *)data;
819 struct net_bridge *br = port->br;
820
821 spin_lock(&br->multicast_lock);
Dan Carpenter02a780c2010-03-06 01:14:09 +0000822 if (port->state == BR_STATE_DISABLED ||
823 port->state == BR_STATE_BLOCKING)
Herbert Xueb1d1642010-02-27 19:41:45 +0000824 goto out;
825
826 if (port->multicast_startup_queries_sent <
827 br->multicast_startup_query_count)
828 port->multicast_startup_queries_sent++;
829
830 br_multicast_send_query(port->br, port,
831 port->multicast_startup_queries_sent);
832
833out:
834 spin_unlock(&br->multicast_lock);
835}
836
837void br_multicast_add_port(struct net_bridge_port *port)
838{
839 port->multicast_router = 1;
840
841 setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
842 (unsigned long)port);
843 setup_timer(&port->multicast_query_timer,
844 br_multicast_port_query_expired, (unsigned long)port);
845}
846
847void br_multicast_del_port(struct net_bridge_port *port)
848{
849 del_timer_sync(&port->multicast_router_timer);
850}
851
Herbert Xu561f1102010-02-27 19:41:50 +0000852static void __br_multicast_enable_port(struct net_bridge_port *port)
853{
854 port->multicast_startup_queries_sent = 0;
855
856 if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 ||
857 del_timer(&port->multicast_query_timer))
858 mod_timer(&port->multicast_query_timer, jiffies);
859}
860
Herbert Xueb1d1642010-02-27 19:41:45 +0000861void br_multicast_enable_port(struct net_bridge_port *port)
862{
863 struct net_bridge *br = port->br;
864
865 spin_lock(&br->multicast_lock);
866 if (br->multicast_disabled || !netif_running(br->dev))
867 goto out;
868
Herbert Xu561f1102010-02-27 19:41:50 +0000869 __br_multicast_enable_port(port);
Herbert Xueb1d1642010-02-27 19:41:45 +0000870
871out:
872 spin_unlock(&br->multicast_lock);
873}
874
875void br_multicast_disable_port(struct net_bridge_port *port)
876{
877 struct net_bridge *br = port->br;
878 struct net_bridge_port_group *pg;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800879 struct hlist_node *n;
Herbert Xueb1d1642010-02-27 19:41:45 +0000880
881 spin_lock(&br->multicast_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800882 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
Herbert Xueb1d1642010-02-27 19:41:45 +0000883 br_multicast_del_pg(br, pg);
884
885 if (!hlist_unhashed(&port->rlist))
886 hlist_del_init_rcu(&port->rlist);
887 del_timer(&port->multicast_router_timer);
888 del_timer(&port->multicast_query_timer);
889 spin_unlock(&br->multicast_lock);
890}
891
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900892static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
893 struct net_bridge_port *port,
894 struct sk_buff *skb)
Herbert Xueb1d1642010-02-27 19:41:45 +0000895{
896 struct igmpv3_report *ih;
897 struct igmpv3_grec *grec;
898 int i;
899 int len;
900 int num;
901 int type;
902 int err = 0;
903 __be32 group;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000904 u16 vid = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000905
906 if (!pskb_may_pull(skb, sizeof(*ih)))
907 return -EINVAL;
908
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000909 br_vlan_get_tag(skb, &vid);
Herbert Xueb1d1642010-02-27 19:41:45 +0000910 ih = igmpv3_report_hdr(skb);
911 num = ntohs(ih->ngrec);
912 len = sizeof(*ih);
913
914 for (i = 0; i < num; i++) {
915 len += sizeof(*grec);
916 if (!pskb_may_pull(skb, len))
917 return -EINVAL;
918
Herbert Xufd218cf2010-04-07 21:20:47 -0700919 grec = (void *)(skb->data + len - sizeof(*grec));
Herbert Xueb1d1642010-02-27 19:41:45 +0000920 group = grec->grec_mca;
921 type = grec->grec_type;
922
Eric Dumazet8eabf952010-04-20 03:20:05 +0000923 len += ntohs(grec->grec_nsrcs) * 4;
Herbert Xueb1d1642010-02-27 19:41:45 +0000924 if (!pskb_may_pull(skb, len))
925 return -EINVAL;
926
927 /* We treat this as an IGMPv2 report for now. */
928 switch (type) {
929 case IGMPV3_MODE_IS_INCLUDE:
930 case IGMPV3_MODE_IS_EXCLUDE:
931 case IGMPV3_CHANGE_TO_INCLUDE:
932 case IGMPV3_CHANGE_TO_EXCLUDE:
933 case IGMPV3_ALLOW_NEW_SOURCES:
934 case IGMPV3_BLOCK_OLD_SOURCES:
935 break;
936
937 default:
938 continue;
939 }
940
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000941 err = br_ip4_multicast_add_group(br, port, group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +0000942 if (err)
943 break;
944 }
945
946 return err;
947}
948
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000949#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900950static int br_ip6_multicast_mld2_report(struct net_bridge *br,
951 struct net_bridge_port *port,
952 struct sk_buff *skb)
953{
954 struct icmp6hdr *icmp6h;
955 struct mld2_grec *grec;
956 int i;
957 int len;
958 int num;
959 int err = 0;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000960 u16 vid = 0;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900961
962 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
963 return -EINVAL;
964
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000965 br_vlan_get_tag(skb, &vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900966 icmp6h = icmp6_hdr(skb);
967 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
968 len = sizeof(*icmp6h);
969
970 for (i = 0; i < num; i++) {
971 __be16 *nsrcs, _nsrcs;
972
973 nsrcs = skb_header_pointer(skb,
974 len + offsetof(struct mld2_grec,
Linus Lüssing649e9842011-02-15 13:19:18 +0000975 grec_nsrcs),
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900976 sizeof(_nsrcs), &_nsrcs);
977 if (!nsrcs)
978 return -EINVAL;
979
980 if (!pskb_may_pull(skb,
981 len + sizeof(*grec) +
Linus Lüssingd41db9f2011-02-15 13:19:19 +0000982 sizeof(struct in6_addr) * ntohs(*nsrcs)))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900983 return -EINVAL;
984
985 grec = (struct mld2_grec *)(skb->data + len);
Linus Lüssingd41db9f2011-02-15 13:19:19 +0000986 len += sizeof(*grec) +
987 sizeof(struct in6_addr) * ntohs(*nsrcs);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900988
989 /* We treat these as MLDv1 reports for now. */
990 switch (grec->grec_type) {
991 case MLD2_MODE_IS_INCLUDE:
992 case MLD2_MODE_IS_EXCLUDE:
993 case MLD2_CHANGE_TO_INCLUDE:
994 case MLD2_CHANGE_TO_EXCLUDE:
995 case MLD2_ALLOW_NEW_SOURCES:
996 case MLD2_BLOCK_OLD_SOURCES:
997 break;
998
999 default:
1000 continue;
1001 }
1002
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001003 err = br_ip6_multicast_add_group(br, port, &grec->grec_mca,
1004 vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001005 if (!err)
1006 break;
1007 }
1008
1009 return err;
1010}
1011#endif
1012
stephen hemminger7e80c122010-04-27 15:01:04 +00001013/*
1014 * Add port to rotuer_list
1015 * list is maintained ordered by pointer value
1016 * and locked by br->multicast_lock and RCU
1017 */
Herbert Xu0909e112010-02-27 19:41:49 +00001018static void br_multicast_add_router(struct net_bridge *br,
1019 struct net_bridge_port *port)
1020{
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001021 struct net_bridge_port *p;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001022 struct hlist_node *slot = NULL;
Herbert Xu0909e112010-02-27 19:41:49 +00001023
Sasha Levinb67bfe02013-02-27 17:06:00 -08001024 hlist_for_each_entry(p, &br->router_list, rlist) {
stephen hemminger7e80c122010-04-27 15:01:04 +00001025 if ((unsigned long) port >= (unsigned long) p)
1026 break;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001027 slot = &p->rlist;
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001028 }
Herbert Xu0909e112010-02-27 19:41:49 +00001029
stephen hemminger7e80c122010-04-27 15:01:04 +00001030 if (slot)
1031 hlist_add_after_rcu(slot, &port->rlist);
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001032 else
1033 hlist_add_head_rcu(&port->rlist, &br->router_list);
Herbert Xu0909e112010-02-27 19:41:49 +00001034}
1035
Herbert Xueb1d1642010-02-27 19:41:45 +00001036static void br_multicast_mark_router(struct net_bridge *br,
1037 struct net_bridge_port *port)
1038{
1039 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +00001040
1041 if (!port) {
1042 if (br->multicast_router == 1)
1043 mod_timer(&br->multicast_router_timer,
1044 now + br->multicast_querier_interval);
1045 return;
1046 }
1047
1048 if (port->multicast_router != 1)
1049 return;
1050
1051 if (!hlist_unhashed(&port->rlist))
1052 goto timer;
1053
Herbert Xu0909e112010-02-27 19:41:49 +00001054 br_multicast_add_router(br, port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001055
1056timer:
1057 mod_timer(&port->multicast_router_timer,
1058 now + br->multicast_querier_interval);
1059}
1060
1061static void br_multicast_query_received(struct net_bridge *br,
1062 struct net_bridge_port *port,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001063 int saddr)
Herbert Xueb1d1642010-02-27 19:41:45 +00001064{
1065 if (saddr)
1066 mod_timer(&br->multicast_querier_timer,
1067 jiffies + br->multicast_querier_interval);
1068 else if (timer_pending(&br->multicast_querier_timer))
1069 return;
1070
1071 br_multicast_mark_router(br, port);
1072}
1073
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001074static int br_ip4_multicast_query(struct net_bridge *br,
1075 struct net_bridge_port *port,
1076 struct sk_buff *skb)
Herbert Xueb1d1642010-02-27 19:41:45 +00001077{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001078 const struct iphdr *iph = ip_hdr(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001079 struct igmphdr *ih = igmp_hdr(skb);
1080 struct net_bridge_mdb_entry *mp;
1081 struct igmpv3_query *ih3;
1082 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +00001083 struct net_bridge_port_group __rcu **pp;
Herbert Xueb1d1642010-02-27 19:41:45 +00001084 unsigned long max_delay;
1085 unsigned long now = jiffies;
1086 __be32 group;
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001087 int err = 0;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001088 u16 vid = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001089
1090 spin_lock(&br->multicast_lock);
1091 if (!netif_running(br->dev) ||
1092 (port && port->state == BR_STATE_DISABLED))
1093 goto out;
1094
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001095 br_multicast_query_received(br, port, !!iph->saddr);
Herbert Xueb1d1642010-02-27 19:41:45 +00001096
1097 group = ih->group;
1098
1099 if (skb->len == sizeof(*ih)) {
1100 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1101
1102 if (!max_delay) {
1103 max_delay = 10 * HZ;
1104 group = 0;
1105 }
1106 } else {
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001107 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) {
1108 err = -EINVAL;
1109 goto out;
1110 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001111
1112 ih3 = igmpv3_query_hdr(skb);
1113 if (ih3->nsrcs)
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001114 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001115
YOSHIFUJI Hideaki / 吉藤英明0ba8c9e2010-03-15 19:27:00 +00001116 max_delay = ih3->code ?
1117 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
Herbert Xueb1d1642010-02-27 19:41:45 +00001118 }
1119
1120 if (!group)
1121 goto out;
1122
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001123 br_vlan_get_tag(skb, &vid);
1124 mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001125 if (!mp)
1126 goto out;
1127
Cong Wang9f00b2e2013-05-21 21:52:55 +00001128 setup_timer(&mp->timer, br_multicast_group_expired, (unsigned long)mp);
1129 mod_timer(&mp->timer, now + br->multicast_membership_interval);
1130 mp->timer_armed = true;
1131
Herbert Xueb1d1642010-02-27 19:41:45 +00001132 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
Cong Wang9f00b2e2013-05-21 21:52:55 +00001206 setup_timer(&mp->timer, br_multicast_group_expired, (unsigned long)mp);
1207 mod_timer(&mp->timer, now + br->multicast_membership_interval);
1208 mp->timer_armed = true;
1209
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001210 max_delay *= br->multicast_last_member_count;
Herbert Xu8a870172011-02-12 01:05:42 -08001211 if (mp->mglist &&
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001212 (timer_pending(&mp->timer) ?
1213 time_after(mp->timer.expires, now + max_delay) :
1214 try_to_del_timer_sync(&mp->timer) >= 0))
1215 mod_timer(&mp->timer, now + max_delay);
1216
Eric Dumazete8051682010-11-15 06:38:10 +00001217 for (pp = &mp->ports;
1218 (p = mlock_dereference(*pp, br)) != NULL;
1219 pp = &p->next) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001220 if (timer_pending(&p->timer) ?
1221 time_after(p->timer.expires, now + max_delay) :
1222 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001223 mod_timer(&p->timer, now + max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001224 }
1225
1226out:
1227 spin_unlock(&br->multicast_lock);
1228 return err;
1229}
1230#endif
1231
Herbert Xueb1d1642010-02-27 19:41:45 +00001232static void br_multicast_leave_group(struct net_bridge *br,
1233 struct net_bridge_port *port,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001234 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +00001235{
1236 struct net_bridge_mdb_htable *mdb;
1237 struct net_bridge_mdb_entry *mp;
1238 struct net_bridge_port_group *p;
1239 unsigned long now;
1240 unsigned long time;
1241
Herbert Xueb1d1642010-02-27 19:41:45 +00001242 spin_lock(&br->multicast_lock);
1243 if (!netif_running(br->dev) ||
1244 (port && port->state == BR_STATE_DISABLED) ||
1245 timer_pending(&br->multicast_querier_timer))
1246 goto out;
1247
Eric Dumazete8051682010-11-15 06:38:10 +00001248 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001249 mp = br_mdb_ip_get(mdb, group);
1250 if (!mp)
1251 goto out;
1252
Cong Wang6b7df112013-05-21 21:52:56 +00001253 if (br->multicast_querier &&
1254 !timer_pending(&br->multicast_querier_timer)) {
1255 __br_multicast_send_query(br, port, &mp->addr);
1256
1257 time = jiffies + br->multicast_last_member_count *
1258 br->multicast_last_member_interval;
1259 mod_timer(port ? &port->multicast_query_timer :
1260 &br->multicast_query_timer, time);
1261
1262 for (p = mlock_dereference(mp->ports, br);
1263 p != NULL;
1264 p = mlock_dereference(p->next, br)) {
1265 if (p->port != port)
1266 continue;
1267
1268 if (!hlist_unhashed(&p->mglist) &&
1269 (timer_pending(&p->timer) ?
1270 time_after(p->timer.expires, time) :
1271 try_to_del_timer_sync(&p->timer) >= 0)) {
1272 mod_timer(&p->timer, time);
1273 }
1274
1275 break;
1276 }
1277 }
1278
David S. Millerc2d3bab2012-12-05 16:24:45 -05001279 if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
Amerigo Wang50426b52012-12-03 23:56:40 +00001280 struct net_bridge_port_group __rcu **pp;
1281
1282 for (pp = &mp->ports;
1283 (p = mlock_dereference(*pp, br)) != NULL;
1284 pp = &p->next) {
1285 if (p->port != port)
1286 continue;
1287
1288 rcu_assign_pointer(*pp, p->next);
1289 hlist_del_init(&p->mglist);
1290 del_timer(&p->timer);
1291 call_rcu_bh(&p->rcu, br_multicast_free_pg);
Cong Wang37a393b2012-12-11 22:23:07 +00001292 br_mdb_notify(br->dev, port, group, RTM_DELMDB);
Amerigo Wang50426b52012-12-03 23:56:40 +00001293
Cong Wang9f00b2e2013-05-21 21:52:55 +00001294 if (!mp->ports && !mp->mglist && mp->timer_armed &&
Amerigo Wang50426b52012-12-03 23:56:40 +00001295 netif_running(br->dev))
1296 mod_timer(&mp->timer, jiffies);
1297 }
1298 goto out;
1299 }
1300
Herbert Xueb1d1642010-02-27 19:41:45 +00001301 now = jiffies;
1302 time = now + br->multicast_last_member_count *
1303 br->multicast_last_member_interval;
1304
1305 if (!port) {
Cong Wang9f00b2e2013-05-21 21:52:55 +00001306 if (mp->mglist && mp->timer_armed &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001307 (timer_pending(&mp->timer) ?
1308 time_after(mp->timer.expires, time) :
1309 try_to_del_timer_sync(&mp->timer) >= 0)) {
1310 mod_timer(&mp->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +00001311 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001312 }
1313
1314out:
1315 spin_unlock(&br->multicast_lock);
1316}
1317
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001318static void br_ip4_multicast_leave_group(struct net_bridge *br,
1319 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001320 __be32 group,
1321 __u16 vid)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001322{
1323 struct br_ip br_group;
1324
1325 if (ipv4_is_local_multicast(group))
1326 return;
1327
1328 br_group.u.ip4 = group;
1329 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001330 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001331
1332 br_multicast_leave_group(br, port, &br_group);
1333}
1334
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001335#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001336static void br_ip6_multicast_leave_group(struct net_bridge *br,
1337 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001338 const struct in6_addr *group,
1339 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001340{
1341 struct br_ip br_group;
1342
Linus Lüssinge4de9f92011-02-15 13:19:21 +00001343 if (!ipv6_is_transient_multicast(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001344 return;
1345
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001346 br_group.u.ip6 = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001347 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001348 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001349
1350 br_multicast_leave_group(br, port, &br_group);
1351}
1352#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001353
Herbert Xueb1d1642010-02-27 19:41:45 +00001354static int br_multicast_ipv4_rcv(struct net_bridge *br,
1355 struct net_bridge_port *port,
1356 struct sk_buff *skb)
1357{
1358 struct sk_buff *skb2 = skb;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001359 const struct iphdr *iph;
Herbert Xueb1d1642010-02-27 19:41:45 +00001360 struct igmphdr *ih;
Eric Dumazet95c96172012-04-15 05:58:06 +00001361 unsigned int len;
1362 unsigned int offset;
Herbert Xueb1d1642010-02-27 19:41:45 +00001363 int err;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001364 u16 vid = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001365
Herbert Xueb1d1642010-02-27 19:41:45 +00001366 /* We treat OOM as packet loss for now. */
1367 if (!pskb_may_pull(skb, sizeof(*iph)))
1368 return -EINVAL;
1369
1370 iph = ip_hdr(skb);
1371
1372 if (iph->ihl < 5 || iph->version != 4)
1373 return -EINVAL;
1374
1375 if (!pskb_may_pull(skb, ip_hdrlen(skb)))
1376 return -EINVAL;
1377
1378 iph = ip_hdr(skb);
1379
1380 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1381 return -EINVAL;
1382
Herbert Xubd4265f2011-06-23 02:39:12 +00001383 if (iph->protocol != IPPROTO_IGMP) {
Amerigo Wangbf5e4dd2013-03-07 02:32:26 +00001384 if (!ipv4_is_local_multicast(iph->daddr))
Herbert Xubd4265f2011-06-23 02:39:12 +00001385 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Herbert Xueb1d1642010-02-27 19:41:45 +00001386 return 0;
Herbert Xubd4265f2011-06-23 02:39:12 +00001387 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001388
1389 len = ntohs(iph->tot_len);
1390 if (skb->len < len || len < ip_hdrlen(skb))
1391 return -EINVAL;
1392
1393 if (skb->len > len) {
1394 skb2 = skb_clone(skb, GFP_ATOMIC);
1395 if (!skb2)
1396 return -ENOMEM;
1397
1398 err = pskb_trim_rcsum(skb2, len);
1399 if (err)
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001400 goto err_out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001401 }
1402
1403 len -= ip_hdrlen(skb2);
1404 offset = skb_network_offset(skb2) + ip_hdrlen(skb2);
1405 __skb_pull(skb2, offset);
1406 skb_reset_transport_header(skb2);
1407
1408 err = -EINVAL;
1409 if (!pskb_may_pull(skb2, sizeof(*ih)))
1410 goto out;
1411
Herbert Xueb1d1642010-02-27 19:41:45 +00001412 switch (skb2->ip_summed) {
1413 case CHECKSUM_COMPLETE:
1414 if (!csum_fold(skb2->csum))
1415 break;
1416 /* fall through */
1417 case CHECKSUM_NONE:
1418 skb2->csum = 0;
1419 if (skb_checksum_complete(skb2))
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001420 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001421 }
1422
1423 err = 0;
1424
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001425 br_vlan_get_tag(skb2, &vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001426 BR_INPUT_SKB_CB(skb)->igmp = 1;
1427 ih = igmp_hdr(skb2);
1428
1429 switch (ih->type) {
1430 case IGMP_HOST_MEMBERSHIP_REPORT:
1431 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Fernando Luis Vázquez Cao62b2bcb2011-06-13 15:04:43 +00001432 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001433 err = br_ip4_multicast_add_group(br, port, ih->group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001434 break;
1435 case IGMPV3_HOST_MEMBERSHIP_REPORT:
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001436 err = br_ip4_multicast_igmp3_report(br, port, skb2);
Herbert Xueb1d1642010-02-27 19:41:45 +00001437 break;
1438 case IGMP_HOST_MEMBERSHIP_QUERY:
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001439 err = br_ip4_multicast_query(br, port, skb2);
Herbert Xueb1d1642010-02-27 19:41:45 +00001440 break;
1441 case IGMP_HOST_LEAVE_MESSAGE:
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001442 br_ip4_multicast_leave_group(br, port, ih->group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001443 break;
1444 }
1445
1446out:
1447 __skb_push(skb2, offset);
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001448err_out:
Herbert Xueb1d1642010-02-27 19:41:45 +00001449 if (skb2 != skb)
1450 kfree_skb(skb2);
1451 return err;
1452}
1453
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001454#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001455static int br_multicast_ipv6_rcv(struct net_bridge *br,
1456 struct net_bridge_port *port,
1457 struct sk_buff *skb)
1458{
Tomas Winkler9d890812011-01-03 11:26:08 -08001459 struct sk_buff *skb2;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001460 const struct ipv6hdr *ip6h;
Eric Dumazet22df1332011-08-23 19:57:05 +00001461 u8 icmp6_type;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001462 u8 nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -08001463 __be16 frag_off;
Eric Dumazet95c96172012-04-15 05:58:06 +00001464 unsigned int len;
Kulikov Vasiliybb7a0bd2010-07-15 08:47:33 +00001465 int offset;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001466 int err;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001467 u16 vid = 0;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001468
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001469 if (!pskb_may_pull(skb, sizeof(*ip6h)))
1470 return -EINVAL;
1471
1472 ip6h = ipv6_hdr(skb);
1473
1474 /*
1475 * We're interested in MLD messages only.
1476 * - Version is 6
1477 * - MLD has always Router Alert hop-by-hop option
1478 * - But we do not support jumbrograms.
1479 */
1480 if (ip6h->version != 6 ||
1481 ip6h->nexthdr != IPPROTO_HOPOPTS ||
1482 ip6h->payload_len == 0)
1483 return 0;
1484
Linus Lüssingff9a57a2011-03-26 20:27:24 +00001485 len = ntohs(ip6h->payload_len) + sizeof(*ip6h);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001486 if (skb->len < len)
1487 return -EINVAL;
1488
1489 nexthdr = ip6h->nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -08001490 offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr, &frag_off);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001491
1492 if (offset < 0 || nexthdr != IPPROTO_ICMPV6)
1493 return 0;
1494
1495 /* Okay, we found ICMPv6 header */
1496 skb2 = skb_clone(skb, GFP_ATOMIC);
1497 if (!skb2)
1498 return -ENOMEM;
1499
Tomas Winkler9d890812011-01-03 11:26:08 -08001500 err = -EINVAL;
1501 if (!pskb_may_pull(skb2, offset + sizeof(struct icmp6hdr)))
1502 goto out;
1503
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001504 len -= offset - skb_network_offset(skb2);
1505
1506 __skb_pull(skb2, offset);
1507 skb_reset_transport_header(skb2);
stephen hemmingerfa2da8c2011-11-15 08:09:14 +00001508 skb_postpull_rcsum(skb2, skb_network_header(skb2),
1509 skb_network_header_len(skb2));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001510
Eric Dumazet22df1332011-08-23 19:57:05 +00001511 icmp6_type = icmp6_hdr(skb2)->icmp6_type;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001512
Eric Dumazet22df1332011-08-23 19:57:05 +00001513 switch (icmp6_type) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001514 case ICMPV6_MGM_QUERY:
1515 case ICMPV6_MGM_REPORT:
1516 case ICMPV6_MGM_REDUCTION:
1517 case ICMPV6_MLD2_REPORT:
1518 break;
1519 default:
1520 err = 0;
1521 goto out;
1522 }
1523
1524 /* Okay, we found MLD message. Check further. */
1525 if (skb2->len > len) {
1526 err = pskb_trim_rcsum(skb2, len);
1527 if (err)
1528 goto out;
Yan, Zheng4b275d72011-08-23 22:54:33 +00001529 err = -EINVAL;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001530 }
1531
Yan, Zheng4b275d72011-08-23 22:54:33 +00001532 ip6h = ipv6_hdr(skb2);
1533
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001534 switch (skb2->ip_summed) {
1535 case CHECKSUM_COMPLETE:
Yan, Zheng4b275d72011-08-23 22:54:33 +00001536 if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, skb2->len,
1537 IPPROTO_ICMPV6, skb2->csum))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001538 break;
1539 /*FALLTHROUGH*/
1540 case CHECKSUM_NONE:
Yan, Zheng4b275d72011-08-23 22:54:33 +00001541 skb2->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr,
1542 &ip6h->daddr,
1543 skb2->len,
1544 IPPROTO_ICMPV6, 0));
1545 if (__skb_checksum_complete(skb2))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001546 goto out;
1547 }
1548
1549 err = 0;
1550
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001551 br_vlan_get_tag(skb, &vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001552 BR_INPUT_SKB_CB(skb)->igmp = 1;
1553
Eric Dumazet22df1332011-08-23 19:57:05 +00001554 switch (icmp6_type) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001555 case ICMPV6_MGM_REPORT:
1556 {
Tomas Winkler9d890812011-01-03 11:26:08 -08001557 struct mld_msg *mld;
1558 if (!pskb_may_pull(skb2, sizeof(*mld))) {
1559 err = -EINVAL;
1560 goto out;
1561 }
1562 mld = (struct mld_msg *)skb_transport_header(skb2);
Fernando Luis Vázquez Caofc2af6c2011-06-13 15:06:58 +00001563 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001564 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001565 break;
1566 }
1567 case ICMPV6_MLD2_REPORT:
1568 err = br_ip6_multicast_mld2_report(br, port, skb2);
1569 break;
1570 case ICMPV6_MGM_QUERY:
1571 err = br_ip6_multicast_query(br, port, skb2);
1572 break;
1573 case ICMPV6_MGM_REDUCTION:
1574 {
Tomas Winkler9d890812011-01-03 11:26:08 -08001575 struct mld_msg *mld;
1576 if (!pskb_may_pull(skb2, sizeof(*mld))) {
1577 err = -EINVAL;
1578 goto out;
1579 }
1580 mld = (struct mld_msg *)skb_transport_header(skb2);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001581 br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001582 }
1583 }
1584
1585out:
Tomas Winkler9d890812011-01-03 11:26:08 -08001586 kfree_skb(skb2);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001587 return err;
1588}
1589#endif
1590
Herbert Xueb1d1642010-02-27 19:41:45 +00001591int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1592 struct sk_buff *skb)
1593{
YOSHIFUJI Hideaki / 吉藤英明1fafc7a2010-04-25 08:06:40 +00001594 BR_INPUT_SKB_CB(skb)->igmp = 0;
1595 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1596
Herbert Xueb1d1642010-02-27 19:41:45 +00001597 if (br->multicast_disabled)
1598 return 0;
1599
1600 switch (skb->protocol) {
1601 case htons(ETH_P_IP):
1602 return br_multicast_ipv4_rcv(br, port, skb);
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001603#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001604 case htons(ETH_P_IPV6):
1605 return br_multicast_ipv6_rcv(br, port, skb);
1606#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001607 }
1608
1609 return 0;
1610}
1611
1612static void br_multicast_query_expired(unsigned long data)
1613{
1614 struct net_bridge *br = (void *)data;
1615
1616 spin_lock(&br->multicast_lock);
1617 if (br->multicast_startup_queries_sent <
1618 br->multicast_startup_query_count)
1619 br->multicast_startup_queries_sent++;
1620
1621 br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent);
1622
1623 spin_unlock(&br->multicast_lock);
1624}
1625
1626void br_multicast_init(struct net_bridge *br)
1627{
1628 br->hash_elasticity = 4;
1629 br->hash_max = 512;
1630
1631 br->multicast_router = 1;
Herbert Xuc5c23262012-04-13 02:37:42 +00001632 br->multicast_querier = 0;
Cong Wang1c8ad5b2013-05-21 21:52:54 +00001633 br->multicast_query_use_ifaddr = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001634 br->multicast_last_member_count = 2;
1635 br->multicast_startup_query_count = 2;
1636
1637 br->multicast_last_member_interval = HZ;
1638 br->multicast_query_response_interval = 10 * HZ;
1639 br->multicast_startup_query_interval = 125 * HZ / 4;
1640 br->multicast_query_interval = 125 * HZ;
1641 br->multicast_querier_interval = 255 * HZ;
1642 br->multicast_membership_interval = 260 * HZ;
1643
1644 spin_lock_init(&br->multicast_lock);
1645 setup_timer(&br->multicast_router_timer,
1646 br_multicast_local_router_expired, 0);
1647 setup_timer(&br->multicast_querier_timer,
Herbert Xubb63f1f2012-04-30 00:22:56 +00001648 br_multicast_querier_expired, (unsigned long)br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001649 setup_timer(&br->multicast_query_timer, br_multicast_query_expired,
1650 (unsigned long)br);
1651}
1652
1653void br_multicast_open(struct net_bridge *br)
1654{
1655 br->multicast_startup_queries_sent = 0;
1656
1657 if (br->multicast_disabled)
1658 return;
1659
1660 mod_timer(&br->multicast_query_timer, jiffies);
1661}
1662
1663void br_multicast_stop(struct net_bridge *br)
1664{
1665 struct net_bridge_mdb_htable *mdb;
1666 struct net_bridge_mdb_entry *mp;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001667 struct hlist_node *n;
Herbert Xueb1d1642010-02-27 19:41:45 +00001668 u32 ver;
1669 int i;
1670
1671 del_timer_sync(&br->multicast_router_timer);
1672 del_timer_sync(&br->multicast_querier_timer);
1673 del_timer_sync(&br->multicast_query_timer);
1674
1675 spin_lock_bh(&br->multicast_lock);
Eric Dumazete8051682010-11-15 06:38:10 +00001676 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001677 if (!mdb)
1678 goto out;
1679
1680 br->mdb = NULL;
1681
1682 ver = mdb->ver;
1683 for (i = 0; i < mdb->max; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001684 hlist_for_each_entry_safe(mp, n, &mdb->mhash[i],
Herbert Xueb1d1642010-02-27 19:41:45 +00001685 hlist[ver]) {
1686 del_timer(&mp->timer);
Cong Wang9f00b2e2013-05-21 21:52:55 +00001687 mp->timer_armed = false;
Herbert Xueb1d1642010-02-27 19:41:45 +00001688 call_rcu_bh(&mp->rcu, br_multicast_free_group);
1689 }
1690 }
1691
1692 if (mdb->old) {
1693 spin_unlock_bh(&br->multicast_lock);
Herbert Xu10cc2b52010-03-05 21:03:35 +00001694 rcu_barrier_bh();
Herbert Xueb1d1642010-02-27 19:41:45 +00001695 spin_lock_bh(&br->multicast_lock);
1696 WARN_ON(mdb->old);
1697 }
1698
1699 mdb->old = mdb;
1700 call_rcu_bh(&mdb->rcu, br_mdb_free);
1701
1702out:
1703 spin_unlock_bh(&br->multicast_lock);
1704}
Herbert Xu0909e112010-02-27 19:41:49 +00001705
1706int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1707{
1708 int err = -ENOENT;
1709
1710 spin_lock_bh(&br->multicast_lock);
1711 if (!netif_running(br->dev))
1712 goto unlock;
1713
1714 switch (val) {
1715 case 0:
1716 case 2:
1717 del_timer(&br->multicast_router_timer);
1718 /* fall through */
1719 case 1:
1720 br->multicast_router = val;
1721 err = 0;
1722 break;
1723
1724 default:
1725 err = -EINVAL;
1726 break;
1727 }
1728
1729unlock:
1730 spin_unlock_bh(&br->multicast_lock);
1731
1732 return err;
1733}
1734
1735int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1736{
1737 struct net_bridge *br = p->br;
1738 int err = -ENOENT;
1739
1740 spin_lock(&br->multicast_lock);
1741 if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED)
1742 goto unlock;
1743
1744 switch (val) {
1745 case 0:
1746 case 1:
1747 case 2:
1748 p->multicast_router = val;
1749 err = 0;
1750
1751 if (val < 2 && !hlist_unhashed(&p->rlist))
1752 hlist_del_init_rcu(&p->rlist);
1753
1754 if (val == 1)
1755 break;
1756
1757 del_timer(&p->multicast_router_timer);
1758
1759 if (val == 0)
1760 break;
1761
1762 br_multicast_add_router(br, p);
1763 break;
1764
1765 default:
1766 err = -EINVAL;
1767 break;
1768 }
1769
1770unlock:
1771 spin_unlock(&br->multicast_lock);
1772
1773 return err;
1774}
Herbert Xu561f1102010-02-27 19:41:50 +00001775
Herbert Xu74857212012-04-13 02:37:42 +00001776static void br_multicast_start_querier(struct net_bridge *br)
Herbert Xu561f1102010-02-27 19:41:50 +00001777{
1778 struct net_bridge_port *port;
Herbert Xu74857212012-04-13 02:37:42 +00001779
1780 br_multicast_open(br);
1781
1782 list_for_each_entry(port, &br->port_list, list) {
1783 if (port->state == BR_STATE_DISABLED ||
1784 port->state == BR_STATE_BLOCKING)
1785 continue;
1786
1787 __br_multicast_enable_port(port);
1788 }
1789}
1790
1791int br_multicast_toggle(struct net_bridge *br, unsigned long val)
1792{
Herbert Xu3a7fda02010-07-29 00:45:30 +00001793 int err = 0;
Eric Dumazete8051682010-11-15 06:38:10 +00001794 struct net_bridge_mdb_htable *mdb;
Herbert Xu561f1102010-02-27 19:41:50 +00001795
Andrey Vaginef5e0d82011-11-10 05:48:03 +00001796 spin_lock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00001797 if (br->multicast_disabled == !val)
1798 goto unlock;
1799
1800 br->multicast_disabled = !val;
1801 if (br->multicast_disabled)
1802 goto unlock;
1803
Herbert Xu3a7fda02010-07-29 00:45:30 +00001804 if (!netif_running(br->dev))
1805 goto unlock;
1806
Eric Dumazete8051682010-11-15 06:38:10 +00001807 mdb = mlock_dereference(br->mdb, br);
1808 if (mdb) {
1809 if (mdb->old) {
Herbert Xu561f1102010-02-27 19:41:50 +00001810 err = -EEXIST;
1811rollback:
1812 br->multicast_disabled = !!val;
1813 goto unlock;
1814 }
1815
Eric Dumazete8051682010-11-15 06:38:10 +00001816 err = br_mdb_rehash(&br->mdb, mdb->max,
Herbert Xu561f1102010-02-27 19:41:50 +00001817 br->hash_elasticity);
1818 if (err)
1819 goto rollback;
1820 }
1821
Herbert Xu74857212012-04-13 02:37:42 +00001822 br_multicast_start_querier(br);
Herbert Xu561f1102010-02-27 19:41:50 +00001823
1824unlock:
Andrey Vaginef5e0d82011-11-10 05:48:03 +00001825 spin_unlock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00001826
1827 return err;
1828}
Herbert Xub1951672010-02-27 19:41:51 +00001829
Herbert Xuc5c23262012-04-13 02:37:42 +00001830int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
1831{
1832 val = !!val;
1833
1834 spin_lock_bh(&br->multicast_lock);
1835 if (br->multicast_querier == val)
1836 goto unlock;
1837
1838 br->multicast_querier = val;
1839 if (val)
1840 br_multicast_start_querier(br);
1841
1842unlock:
1843 spin_unlock_bh(&br->multicast_lock);
1844
1845 return 0;
1846}
1847
Herbert Xub1951672010-02-27 19:41:51 +00001848int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
1849{
1850 int err = -ENOENT;
1851 u32 old;
Eric Dumazete8051682010-11-15 06:38:10 +00001852 struct net_bridge_mdb_htable *mdb;
Herbert Xub1951672010-02-27 19:41:51 +00001853
1854 spin_lock(&br->multicast_lock);
1855 if (!netif_running(br->dev))
1856 goto unlock;
1857
1858 err = -EINVAL;
1859 if (!is_power_of_2(val))
1860 goto unlock;
Eric Dumazete8051682010-11-15 06:38:10 +00001861
1862 mdb = mlock_dereference(br->mdb, br);
1863 if (mdb && val < mdb->size)
Herbert Xub1951672010-02-27 19:41:51 +00001864 goto unlock;
1865
1866 err = 0;
1867
1868 old = br->hash_max;
1869 br->hash_max = val;
1870
Eric Dumazete8051682010-11-15 06:38:10 +00001871 if (mdb) {
1872 if (mdb->old) {
Herbert Xub1951672010-02-27 19:41:51 +00001873 err = -EEXIST;
1874rollback:
1875 br->hash_max = old;
1876 goto unlock;
1877 }
1878
1879 err = br_mdb_rehash(&br->mdb, br->hash_max,
1880 br->hash_elasticity);
1881 if (err)
1882 goto rollback;
1883 }
1884
1885unlock:
1886 spin_unlock(&br->multicast_lock);
1887
1888 return err;
1889}