blob: 31952a103949d440ddf775885868b68b43c104ce [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 Stevens76d66152010-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
Linus Lüssing32de8682013-06-16 23:20:34 +0200470 interval = ipv6_addr_any(group) ?
471 br->multicast_query_response_interval :
472 br->multicast_last_member_interval;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900473
474 mldq->mld_type = ICMPV6_MGM_QUERY;
475 mldq->mld_code = 0;
476 mldq->mld_cksum = 0;
477 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
478 mldq->mld_reserved = 0;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000479 mldq->mld_mca = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900480
481 /* checksum */
482 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
483 sizeof(*mldq), IPPROTO_ICMPV6,
484 csum_partial(mldq,
485 sizeof(*mldq), 0));
486 skb_put(skb, sizeof(*mldq));
487
488 __skb_pull(skb, sizeof(*eth));
489
490out:
491 return skb;
492}
493#endif
494
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900495static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
496 struct br_ip *addr)
497{
498 switch (addr->proto) {
499 case htons(ETH_P_IP):
500 return br_ip4_multicast_alloc_query(br, addr->u.ip4);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000501#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900502 case htons(ETH_P_IPV6):
503 return br_ip6_multicast_alloc_query(br, &addr->u.ip6);
504#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900505 }
506 return NULL;
507}
508
Herbert Xueb1d1642010-02-27 19:41:45 +0000509static struct net_bridge_mdb_entry *br_multicast_get_group(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900510 struct net_bridge *br, struct net_bridge_port *port,
511 struct br_ip *group, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +0000512{
Eric Dumazete8051682010-11-15 06:38:10 +0000513 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000514 struct net_bridge_mdb_entry *mp;
Eric Dumazet95c96172012-04-15 05:58:06 +0000515 unsigned int count = 0;
516 unsigned int max;
Herbert Xueb1d1642010-02-27 19:41:45 +0000517 int elasticity;
518 int err;
519
Eric Dumazete8051682010-11-15 06:38:10 +0000520 mdb = rcu_dereference_protected(br->mdb, 1);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800521 hlist_for_each_entry(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000522 count++;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900523 if (unlikely(br_ip_equal(group, &mp->addr)))
Herbert Xueb1d1642010-02-27 19:41:45 +0000524 return mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000525 }
526
527 elasticity = 0;
528 max = mdb->max;
529
530 if (unlikely(count > br->hash_elasticity && count)) {
531 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000532 br_info(br, "Multicast hash table "
533 "chain limit reached: %s\n",
534 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000535
536 elasticity = br->hash_elasticity;
537 }
538
539 if (mdb->size >= max) {
540 max *= 2;
Thomas Graf036be6d2012-07-10 22:29:19 +0000541 if (unlikely(max > br->hash_max)) {
542 br_warn(br, "Multicast hash table maximum of %d "
543 "reached, disabling snooping: %s\n",
544 br->hash_max,
545 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000546 err = -E2BIG;
547disable:
548 br->multicast_disabled = 1;
549 goto err;
550 }
551 }
552
553 if (max > mdb->max || elasticity) {
554 if (mdb->old) {
555 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000556 br_info(br, "Multicast hash table "
557 "on fire: %s\n",
558 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000559 err = -EEXIST;
560 goto err;
561 }
562
563 err = br_mdb_rehash(&br->mdb, max, elasticity);
564 if (err) {
stephen hemminger28a16c92010-05-10 09:31:09 +0000565 br_warn(br, "Cannot rehash multicast "
566 "hash table, disabling snooping: %s, %d, %d\n",
567 port ? port->dev->name : br->dev->name,
568 mdb->size, err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000569 goto disable;
570 }
571
572 err = -EAGAIN;
573 goto err;
574 }
575
576 return NULL;
577
578err:
579 mp = ERR_PTR(err);
580 return mp;
581}
582
Cong Wangcfd56752012-12-11 22:23:08 +0000583struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
584 struct net_bridge_port *port, struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000585{
Eric Dumazete8051682010-11-15 06:38:10 +0000586 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000587 struct net_bridge_mdb_entry *mp;
588 int hash;
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000589 int err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000590
Eric Dumazete8051682010-11-15 06:38:10 +0000591 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000592 if (!mdb) {
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000593 err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0);
594 if (err)
595 return ERR_PTR(err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000596 goto rehash;
597 }
598
599 hash = br_ip_hash(mdb, group);
600 mp = br_multicast_get_group(br, port, group, hash);
601 switch (PTR_ERR(mp)) {
602 case 0:
603 break;
604
605 case -EAGAIN:
606rehash:
Eric Dumazete8051682010-11-15 06:38:10 +0000607 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000608 hash = br_ip_hash(mdb, group);
609 break;
610
611 default:
612 goto out;
613 }
614
615 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
616 if (unlikely(!mp))
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000617 return ERR_PTR(-ENOMEM);
Herbert Xueb1d1642010-02-27 19:41:45 +0000618
619 mp->br = br;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900620 mp->addr = *group;
Herbert Xueb1d1642010-02-27 19:41:45 +0000621
622 hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
623 mdb->size++;
624
625out:
626 return mp;
627}
628
Cong Wangcfd56752012-12-11 22:23:08 +0000629struct net_bridge_port_group *br_multicast_new_port_group(
630 struct net_bridge_port *port,
631 struct br_ip *group,
Amerigo Wangccb1c312012-12-14 22:09:51 +0000632 struct net_bridge_port_group __rcu *next,
633 unsigned char state)
Cong Wangcfd56752012-12-11 22:23:08 +0000634{
635 struct net_bridge_port_group *p;
636
637 p = kzalloc(sizeof(*p), GFP_ATOMIC);
638 if (unlikely(!p))
639 return NULL;
640
641 p->addr = *group;
642 p->port = port;
Amerigo Wangccb1c312012-12-14 22:09:51 +0000643 p->state = state;
stephen hemmingereca2a432012-12-13 06:51:28 +0000644 rcu_assign_pointer(p->next, next);
Cong Wangcfd56752012-12-11 22:23:08 +0000645 hlist_add_head(&p->mglist, &port->mglist);
646 setup_timer(&p->timer, br_multicast_port_group_expired,
647 (unsigned long)p);
648 return p;
649}
650
Herbert Xueb1d1642010-02-27 19:41:45 +0000651static int br_multicast_add_group(struct net_bridge *br,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900652 struct net_bridge_port *port,
653 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000654{
655 struct net_bridge_mdb_entry *mp;
656 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +0000657 struct net_bridge_port_group __rcu **pp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000658 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 goto out;
673 }
674
Eric Dumazete8051682010-11-15 06:38:10 +0000675 for (pp = &mp->ports;
676 (p = mlock_dereference(*pp, br)) != NULL;
677 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000678 if (p->port == port)
Cong Wang9f00b2e2013-05-21 21:52:55 +0000679 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +0000680 if ((unsigned long)p->port < (unsigned long)port)
681 break;
682 }
683
Amerigo Wangccb1c312012-12-14 22:09:51 +0000684 p = br_multicast_new_port_group(port, group, *pp, MDB_TEMPORARY);
Herbert Xueb1d1642010-02-27 19:41:45 +0000685 if (unlikely(!p))
686 goto err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000687 rcu_assign_pointer(*pp, p);
Cong Wang37a393b2012-12-11 22:23:07 +0000688 br_mdb_notify(br->dev, port, group, RTM_NEWMDB);
Herbert Xueb1d1642010-02-27 19:41:45 +0000689
Herbert Xueb1d1642010-02-27 19:41:45 +0000690out:
691 err = 0;
692
693err:
694 spin_unlock(&br->multicast_lock);
695 return err;
696}
697
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900698static int br_ip4_multicast_add_group(struct net_bridge *br,
699 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000700 __be32 group,
701 __u16 vid)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900702{
703 struct br_ip br_group;
704
705 if (ipv4_is_local_multicast(group))
706 return 0;
707
708 br_group.u.ip4 = group;
709 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000710 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900711
712 return br_multicast_add_group(br, port, &br_group);
713}
714
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000715#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900716static int br_ip6_multicast_add_group(struct net_bridge *br,
717 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000718 const struct in6_addr *group,
719 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900720{
721 struct br_ip br_group;
722
Linus Lüssinge4de9f92011-02-15 13:19:21 +0000723 if (!ipv6_is_transient_multicast(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900724 return 0;
725
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000726 br_group.u.ip6 = *group;
Linus Lüssing9cc6e0c2011-02-15 13:19:17 +0000727 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000728 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900729
730 return br_multicast_add_group(br, port, &br_group);
731}
732#endif
733
Herbert Xueb1d1642010-02-27 19:41:45 +0000734static void br_multicast_router_expired(unsigned long data)
735{
736 struct net_bridge_port *port = (void *)data;
737 struct net_bridge *br = port->br;
738
739 spin_lock(&br->multicast_lock);
740 if (port->multicast_router != 1 ||
741 timer_pending(&port->multicast_router_timer) ||
742 hlist_unhashed(&port->rlist))
743 goto out;
744
745 hlist_del_init_rcu(&port->rlist);
746
747out:
748 spin_unlock(&br->multicast_lock);
749}
750
751static void br_multicast_local_router_expired(unsigned long data)
752{
753}
754
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000755static void br_multicast_querier_expired(unsigned long data)
756{
Herbert Xubb63f1f2012-04-30 00:22:56 +0000757 struct net_bridge *br = (void *)data;
Herbert Xuc83b8fa2012-04-13 02:37:42 +0000758
759 spin_lock(&br->multicast_lock);
760 if (!netif_running(br->dev) || br->multicast_disabled)
761 goto out;
762
763 br_multicast_start_querier(br);
764
765out:
766 spin_unlock(&br->multicast_lock);
767}
768
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900769static void __br_multicast_send_query(struct net_bridge *br,
770 struct net_bridge_port *port,
771 struct br_ip *ip)
Herbert Xueb1d1642010-02-27 19:41:45 +0000772{
Herbert Xueb1d1642010-02-27 19:41:45 +0000773 struct sk_buff *skb;
774
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900775 skb = br_multicast_alloc_query(br, ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000776 if (!skb)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900777 return;
Herbert Xueb1d1642010-02-27 19:41:45 +0000778
779 if (port) {
780 __skb_push(skb, sizeof(struct ethhdr));
781 skb->dev = port->dev;
Jan Engelhardt713aefa2010-03-23 04:07:21 +0100782 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
Herbert Xueb1d1642010-02-27 19:41:45 +0000783 dev_queue_xmit);
784 } else
785 netif_rx(skb);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900786}
Herbert Xueb1d1642010-02-27 19:41:45 +0000787
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900788static void br_multicast_send_query(struct net_bridge *br,
789 struct net_bridge_port *port, u32 sent)
790{
791 unsigned long time;
792 struct br_ip br_group;
793
794 if (!netif_running(br->dev) || br->multicast_disabled ||
Herbert Xuc5c23262012-04-13 02:37:42 +0000795 !br->multicast_querier ||
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900796 timer_pending(&br->multicast_querier_timer))
797 return;
798
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900799 memset(&br_group.u, 0, sizeof(br_group.u));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900800
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900801 br_group.proto = htons(ETH_P_IP);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900802 __br_multicast_send_query(br, port, &br_group);
803
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000804#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900805 br_group.proto = htons(ETH_P_IPV6);
806 __br_multicast_send_query(br, port, &br_group);
807#endif
808
Herbert Xueb1d1642010-02-27 19:41:45 +0000809 time = jiffies;
810 time += sent < br->multicast_startup_query_count ?
811 br->multicast_startup_query_interval :
812 br->multicast_query_interval;
813 mod_timer(port ? &port->multicast_query_timer :
814 &br->multicast_query_timer, time);
815}
816
817static void br_multicast_port_query_expired(unsigned long data)
818{
819 struct net_bridge_port *port = (void *)data;
820 struct net_bridge *br = port->br;
821
822 spin_lock(&br->multicast_lock);
Dan Carpenter02a780c2010-03-06 01:14:09 +0000823 if (port->state == BR_STATE_DISABLED ||
824 port->state == BR_STATE_BLOCKING)
Herbert Xueb1d1642010-02-27 19:41:45 +0000825 goto out;
826
827 if (port->multicast_startup_queries_sent <
828 br->multicast_startup_query_count)
829 port->multicast_startup_queries_sent++;
830
831 br_multicast_send_query(port->br, port,
832 port->multicast_startup_queries_sent);
833
834out:
835 spin_unlock(&br->multicast_lock);
836}
837
838void br_multicast_add_port(struct net_bridge_port *port)
839{
840 port->multicast_router = 1;
841
842 setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
843 (unsigned long)port);
844 setup_timer(&port->multicast_query_timer,
845 br_multicast_port_query_expired, (unsigned long)port);
846}
847
848void br_multicast_del_port(struct net_bridge_port *port)
849{
850 del_timer_sync(&port->multicast_router_timer);
851}
852
Herbert Xu561f1102010-02-27 19:41:50 +0000853static void __br_multicast_enable_port(struct net_bridge_port *port)
854{
855 port->multicast_startup_queries_sent = 0;
856
857 if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 ||
858 del_timer(&port->multicast_query_timer))
859 mod_timer(&port->multicast_query_timer, jiffies);
860}
861
Herbert Xueb1d1642010-02-27 19:41:45 +0000862void br_multicast_enable_port(struct net_bridge_port *port)
863{
864 struct net_bridge *br = port->br;
865
866 spin_lock(&br->multicast_lock);
867 if (br->multicast_disabled || !netif_running(br->dev))
868 goto out;
869
Herbert Xu561f1102010-02-27 19:41:50 +0000870 __br_multicast_enable_port(port);
Herbert Xueb1d1642010-02-27 19:41:45 +0000871
872out:
873 spin_unlock(&br->multicast_lock);
874}
875
876void br_multicast_disable_port(struct net_bridge_port *port)
877{
878 struct net_bridge *br = port->br;
879 struct net_bridge_port_group *pg;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800880 struct hlist_node *n;
Herbert Xueb1d1642010-02-27 19:41:45 +0000881
882 spin_lock(&br->multicast_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800883 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
Herbert Xueb1d1642010-02-27 19:41:45 +0000884 br_multicast_del_pg(br, pg);
885
886 if (!hlist_unhashed(&port->rlist))
887 hlist_del_init_rcu(&port->rlist);
888 del_timer(&port->multicast_router_timer);
889 del_timer(&port->multicast_query_timer);
890 spin_unlock(&br->multicast_lock);
891}
892
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900893static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
894 struct net_bridge_port *port,
895 struct sk_buff *skb)
Herbert Xueb1d1642010-02-27 19:41:45 +0000896{
897 struct igmpv3_report *ih;
898 struct igmpv3_grec *grec;
899 int i;
900 int len;
901 int num;
902 int type;
903 int err = 0;
904 __be32 group;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000905 u16 vid = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +0000906
907 if (!pskb_may_pull(skb, sizeof(*ih)))
908 return -EINVAL;
909
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000910 br_vlan_get_tag(skb, &vid);
Herbert Xueb1d1642010-02-27 19:41:45 +0000911 ih = igmpv3_report_hdr(skb);
912 num = ntohs(ih->ngrec);
913 len = sizeof(*ih);
914
915 for (i = 0; i < num; i++) {
916 len += sizeof(*grec);
917 if (!pskb_may_pull(skb, len))
918 return -EINVAL;
919
Herbert Xufd218cf2010-04-07 21:20:47 -0700920 grec = (void *)(skb->data + len - sizeof(*grec));
Herbert Xueb1d1642010-02-27 19:41:45 +0000921 group = grec->grec_mca;
922 type = grec->grec_type;
923
Eric Dumazet8eabf952010-04-20 03:20:05 +0000924 len += ntohs(grec->grec_nsrcs) * 4;
Herbert Xueb1d1642010-02-27 19:41:45 +0000925 if (!pskb_may_pull(skb, len))
926 return -EINVAL;
927
928 /* We treat this as an IGMPv2 report for now. */
929 switch (type) {
930 case IGMPV3_MODE_IS_INCLUDE:
931 case IGMPV3_MODE_IS_EXCLUDE:
932 case IGMPV3_CHANGE_TO_INCLUDE:
933 case IGMPV3_CHANGE_TO_EXCLUDE:
934 case IGMPV3_ALLOW_NEW_SOURCES:
935 case IGMPV3_BLOCK_OLD_SOURCES:
936 break;
937
938 default:
939 continue;
940 }
941
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000942 err = br_ip4_multicast_add_group(br, port, group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +0000943 if (err)
944 break;
945 }
946
947 return err;
948}
949
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000950#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900951static int br_ip6_multicast_mld2_report(struct net_bridge *br,
952 struct net_bridge_port *port,
953 struct sk_buff *skb)
954{
955 struct icmp6hdr *icmp6h;
956 struct mld2_grec *grec;
957 int i;
958 int len;
959 int num;
960 int err = 0;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000961 u16 vid = 0;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900962
963 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
964 return -EINVAL;
965
Vlad Yasevichb0e9a302013-02-13 12:00:17 +0000966 br_vlan_get_tag(skb, &vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900967 icmp6h = icmp6_hdr(skb);
968 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
969 len = sizeof(*icmp6h);
970
971 for (i = 0; i < num; i++) {
972 __be16 *nsrcs, _nsrcs;
973
974 nsrcs = skb_header_pointer(skb,
975 len + offsetof(struct mld2_grec,
Linus Lüssing649e9842011-02-15 13:19:18 +0000976 grec_nsrcs),
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900977 sizeof(_nsrcs), &_nsrcs);
978 if (!nsrcs)
979 return -EINVAL;
980
981 if (!pskb_may_pull(skb,
982 len + sizeof(*grec) +
Linus Lüssingd41db9f2011-02-15 13:19:19 +0000983 sizeof(struct in6_addr) * ntohs(*nsrcs)))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900984 return -EINVAL;
985
986 grec = (struct mld2_grec *)(skb->data + len);
Linus Lüssingd41db9f2011-02-15 13:19:19 +0000987 len += sizeof(*grec) +
988 sizeof(struct in6_addr) * ntohs(*nsrcs);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900989
990 /* We treat these as MLDv1 reports for now. */
991 switch (grec->grec_type) {
992 case MLD2_MODE_IS_INCLUDE:
993 case MLD2_MODE_IS_EXCLUDE:
994 case MLD2_CHANGE_TO_INCLUDE:
995 case MLD2_CHANGE_TO_EXCLUDE:
996 case MLD2_ALLOW_NEW_SOURCES:
997 case MLD2_BLOCK_OLD_SOURCES:
998 break;
999
1000 default:
1001 continue;
1002 }
1003
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001004 err = br_ip6_multicast_add_group(br, port, &grec->grec_mca,
1005 vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001006 if (!err)
1007 break;
1008 }
1009
1010 return err;
1011}
1012#endif
1013
stephen hemminger7e80c122010-04-27 15:01:04 +00001014/*
1015 * Add port to rotuer_list
1016 * list is maintained ordered by pointer value
1017 * and locked by br->multicast_lock and RCU
1018 */
Herbert Xu0909e112010-02-27 19:41:49 +00001019static void br_multicast_add_router(struct net_bridge *br,
1020 struct net_bridge_port *port)
1021{
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001022 struct net_bridge_port *p;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001023 struct hlist_node *slot = NULL;
Herbert Xu0909e112010-02-27 19:41:49 +00001024
Sasha Levinb67bfe02013-02-27 17:06:00 -08001025 hlist_for_each_entry(p, &br->router_list, rlist) {
stephen hemminger7e80c122010-04-27 15:01:04 +00001026 if ((unsigned long) port >= (unsigned long) p)
1027 break;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001028 slot = &p->rlist;
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001029 }
Herbert Xu0909e112010-02-27 19:41:49 +00001030
stephen hemminger7e80c122010-04-27 15:01:04 +00001031 if (slot)
1032 hlist_add_after_rcu(slot, &port->rlist);
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001033 else
1034 hlist_add_head_rcu(&port->rlist, &br->router_list);
Herbert Xu0909e112010-02-27 19:41:49 +00001035}
1036
Herbert Xueb1d1642010-02-27 19:41:45 +00001037static void br_multicast_mark_router(struct net_bridge *br,
1038 struct net_bridge_port *port)
1039{
1040 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +00001041
1042 if (!port) {
1043 if (br->multicast_router == 1)
1044 mod_timer(&br->multicast_router_timer,
1045 now + br->multicast_querier_interval);
1046 return;
1047 }
1048
1049 if (port->multicast_router != 1)
1050 return;
1051
1052 if (!hlist_unhashed(&port->rlist))
1053 goto timer;
1054
Herbert Xu0909e112010-02-27 19:41:49 +00001055 br_multicast_add_router(br, port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001056
1057timer:
1058 mod_timer(&port->multicast_router_timer,
1059 now + br->multicast_querier_interval);
1060}
1061
1062static void br_multicast_query_received(struct net_bridge *br,
1063 struct net_bridge_port *port,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001064 int saddr)
Herbert Xueb1d1642010-02-27 19:41:45 +00001065{
1066 if (saddr)
1067 mod_timer(&br->multicast_querier_timer,
1068 jiffies + br->multicast_querier_interval);
1069 else if (timer_pending(&br->multicast_querier_timer))
1070 return;
1071
1072 br_multicast_mark_router(br, port);
1073}
1074
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001075static int br_ip4_multicast_query(struct net_bridge *br,
1076 struct net_bridge_port *port,
1077 struct sk_buff *skb)
Herbert Xueb1d1642010-02-27 19:41:45 +00001078{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001079 const struct iphdr *iph = ip_hdr(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001080 struct igmphdr *ih = igmp_hdr(skb);
1081 struct net_bridge_mdb_entry *mp;
1082 struct igmpv3_query *ih3;
1083 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +00001084 struct net_bridge_port_group __rcu **pp;
Herbert Xueb1d1642010-02-27 19:41:45 +00001085 unsigned long max_delay;
1086 unsigned long now = jiffies;
1087 __be32 group;
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001088 int err = 0;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001089 u16 vid = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001090
1091 spin_lock(&br->multicast_lock);
1092 if (!netif_running(br->dev) ||
1093 (port && port->state == BR_STATE_DISABLED))
1094 goto out;
1095
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001096 br_multicast_query_received(br, port, !!iph->saddr);
Herbert Xueb1d1642010-02-27 19:41:45 +00001097
1098 group = ih->group;
1099
1100 if (skb->len == sizeof(*ih)) {
1101 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1102
1103 if (!max_delay) {
1104 max_delay = 10 * HZ;
1105 group = 0;
1106 }
1107 } else {
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001108 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) {
1109 err = -EINVAL;
1110 goto out;
1111 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001112
1113 ih3 = igmpv3_query_hdr(skb);
1114 if (ih3->nsrcs)
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001115 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001116
YOSHIFUJI Hideaki / 吉藤英明0ba8c9e2010-03-15 19:27:00 +00001117 max_delay = ih3->code ?
1118 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
Herbert Xueb1d1642010-02-27 19:41:45 +00001119 }
1120
1121 if (!group)
1122 goto out;
1123
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001124 br_vlan_get_tag(skb, &vid);
1125 mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001126 if (!mp)
1127 goto out;
1128
Cong Wang9f00b2e2013-05-21 21:52:55 +00001129 setup_timer(&mp->timer, br_multicast_group_expired, (unsigned long)mp);
1130 mod_timer(&mp->timer, now + br->multicast_membership_interval);
1131 mp->timer_armed = true;
1132
Herbert Xueb1d1642010-02-27 19:41:45 +00001133 max_delay *= br->multicast_last_member_count;
1134
Herbert Xu8a870172011-02-12 01:05:42 -08001135 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001136 (timer_pending(&mp->timer) ?
1137 time_after(mp->timer.expires, now + max_delay) :
1138 try_to_del_timer_sync(&mp->timer) >= 0))
1139 mod_timer(&mp->timer, now + max_delay);
1140
Eric Dumazete8051682010-11-15 06:38:10 +00001141 for (pp = &mp->ports;
1142 (p = mlock_dereference(*pp, br)) != NULL;
1143 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001144 if (timer_pending(&p->timer) ?
1145 time_after(p->timer.expires, now + max_delay) :
1146 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001147 mod_timer(&p->timer, now + max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001148 }
1149
1150out:
1151 spin_unlock(&br->multicast_lock);
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001152 return err;
Herbert Xueb1d1642010-02-27 19:41:45 +00001153}
1154
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001155#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001156static int br_ip6_multicast_query(struct net_bridge *br,
1157 struct net_bridge_port *port,
1158 struct sk_buff *skb)
1159{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001160 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
stephen hemmingereca2a432012-12-13 06:51:28 +00001161 struct mld_msg *mld;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001162 struct net_bridge_mdb_entry *mp;
1163 struct mld2_query *mld2q;
Eric Dumazete8051682010-11-15 06:38:10 +00001164 struct net_bridge_port_group *p;
1165 struct net_bridge_port_group __rcu **pp;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001166 unsigned long max_delay;
1167 unsigned long now = jiffies;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001168 const struct in6_addr *group = NULL;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001169 int err = 0;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001170 u16 vid = 0;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001171
1172 spin_lock(&br->multicast_lock);
1173 if (!netif_running(br->dev) ||
1174 (port && port->state == BR_STATE_DISABLED))
1175 goto out;
1176
1177 br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->saddr));
1178
1179 if (skb->len == sizeof(*mld)) {
1180 if (!pskb_may_pull(skb, sizeof(*mld))) {
1181 err = -EINVAL;
1182 goto out;
1183 }
1184 mld = (struct mld_msg *) icmp6_hdr(skb);
Li RongQing47152132012-07-09 23:56:12 +00001185 max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001186 if (max_delay)
1187 group = &mld->mld_mca;
1188 } else if (skb->len >= sizeof(*mld2q)) {
1189 if (!pskb_may_pull(skb, sizeof(*mld2q))) {
1190 err = -EINVAL;
1191 goto out;
1192 }
1193 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1194 if (!mld2q->mld2q_nsrcs)
1195 group = &mld2q->mld2q_mca;
Ang Way Chuang8fa45a72012-12-13 23:08:39 +00001196 max_delay = mld2q->mld2q_mrc ? MLDV2_MRC(ntohs(mld2q->mld2q_mrc)) : 1;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001197 }
1198
1199 if (!group)
1200 goto out;
1201
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001202 br_vlan_get_tag(skb, &vid);
1203 mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001204 if (!mp)
1205 goto out;
1206
Cong Wang9f00b2e2013-05-21 21:52:55 +00001207 setup_timer(&mp->timer, br_multicast_group_expired, (unsigned long)mp);
1208 mod_timer(&mp->timer, now + br->multicast_membership_interval);
1209 mp->timer_armed = true;
1210
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001211 max_delay *= br->multicast_last_member_count;
Herbert Xu8a870172011-02-12 01:05:42 -08001212 if (mp->mglist &&
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001213 (timer_pending(&mp->timer) ?
1214 time_after(mp->timer.expires, now + max_delay) :
1215 try_to_del_timer_sync(&mp->timer) >= 0))
1216 mod_timer(&mp->timer, now + max_delay);
1217
Eric Dumazete8051682010-11-15 06:38:10 +00001218 for (pp = &mp->ports;
1219 (p = mlock_dereference(*pp, br)) != NULL;
1220 pp = &p->next) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001221 if (timer_pending(&p->timer) ?
1222 time_after(p->timer.expires, now + max_delay) :
1223 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001224 mod_timer(&p->timer, now + max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001225 }
1226
1227out:
1228 spin_unlock(&br->multicast_lock);
1229 return err;
1230}
1231#endif
1232
Herbert Xueb1d1642010-02-27 19:41:45 +00001233static void br_multicast_leave_group(struct net_bridge *br,
1234 struct net_bridge_port *port,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001235 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +00001236{
1237 struct net_bridge_mdb_htable *mdb;
1238 struct net_bridge_mdb_entry *mp;
1239 struct net_bridge_port_group *p;
1240 unsigned long now;
1241 unsigned long time;
1242
Herbert Xueb1d1642010-02-27 19:41:45 +00001243 spin_lock(&br->multicast_lock);
1244 if (!netif_running(br->dev) ||
1245 (port && port->state == BR_STATE_DISABLED) ||
1246 timer_pending(&br->multicast_querier_timer))
1247 goto out;
1248
Eric Dumazete8051682010-11-15 06:38:10 +00001249 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001250 mp = br_mdb_ip_get(mdb, group);
1251 if (!mp)
1252 goto out;
1253
Cong Wang6b7df112013-05-21 21:52:56 +00001254 if (br->multicast_querier &&
1255 !timer_pending(&br->multicast_querier_timer)) {
1256 __br_multicast_send_query(br, port, &mp->addr);
1257
1258 time = jiffies + br->multicast_last_member_count *
1259 br->multicast_last_member_interval;
1260 mod_timer(port ? &port->multicast_query_timer :
1261 &br->multicast_query_timer, time);
1262
1263 for (p = mlock_dereference(mp->ports, br);
1264 p != NULL;
1265 p = mlock_dereference(p->next, br)) {
1266 if (p->port != port)
1267 continue;
1268
1269 if (!hlist_unhashed(&p->mglist) &&
1270 (timer_pending(&p->timer) ?
1271 time_after(p->timer.expires, time) :
1272 try_to_del_timer_sync(&p->timer) >= 0)) {
1273 mod_timer(&p->timer, time);
1274 }
1275
1276 break;
1277 }
1278 }
1279
David S. Millerc2d3bab2012-12-05 16:24:45 -05001280 if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
Amerigo Wang50426b52012-12-03 23:56:40 +00001281 struct net_bridge_port_group __rcu **pp;
1282
1283 for (pp = &mp->ports;
1284 (p = mlock_dereference(*pp, br)) != NULL;
1285 pp = &p->next) {
1286 if (p->port != port)
1287 continue;
1288
1289 rcu_assign_pointer(*pp, p->next);
1290 hlist_del_init(&p->mglist);
1291 del_timer(&p->timer);
1292 call_rcu_bh(&p->rcu, br_multicast_free_pg);
Cong Wang37a393b2012-12-11 22:23:07 +00001293 br_mdb_notify(br->dev, port, group, RTM_DELMDB);
Amerigo Wang50426b52012-12-03 23:56:40 +00001294
Cong Wang9f00b2e2013-05-21 21:52:55 +00001295 if (!mp->ports && !mp->mglist && mp->timer_armed &&
Amerigo Wang50426b52012-12-03 23:56:40 +00001296 netif_running(br->dev))
1297 mod_timer(&mp->timer, jiffies);
1298 }
1299 goto out;
1300 }
1301
Herbert Xueb1d1642010-02-27 19:41:45 +00001302 now = jiffies;
1303 time = now + br->multicast_last_member_count *
1304 br->multicast_last_member_interval;
1305
1306 if (!port) {
Cong Wang9f00b2e2013-05-21 21:52:55 +00001307 if (mp->mglist && mp->timer_armed &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001308 (timer_pending(&mp->timer) ?
1309 time_after(mp->timer.expires, time) :
1310 try_to_del_timer_sync(&mp->timer) >= 0)) {
1311 mod_timer(&mp->timer, time);
Herbert Xueb1d1642010-02-27 19:41:45 +00001312 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001313 }
1314
1315out:
1316 spin_unlock(&br->multicast_lock);
1317}
1318
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001319static void br_ip4_multicast_leave_group(struct net_bridge *br,
1320 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001321 __be32 group,
1322 __u16 vid)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001323{
1324 struct br_ip br_group;
1325
1326 if (ipv4_is_local_multicast(group))
1327 return;
1328
1329 br_group.u.ip4 = group;
1330 br_group.proto = htons(ETH_P_IP);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001331 br_group.vid = vid;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001332
1333 br_multicast_leave_group(br, port, &br_group);
1334}
1335
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001336#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001337static void br_ip6_multicast_leave_group(struct net_bridge *br,
1338 struct net_bridge_port *port,
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001339 const struct in6_addr *group,
1340 __u16 vid)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001341{
1342 struct br_ip br_group;
1343
Linus Lüssinge4de9f92011-02-15 13:19:21 +00001344 if (!ipv6_is_transient_multicast(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001345 return;
1346
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001347 br_group.u.ip6 = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001348 br_group.proto = htons(ETH_P_IPV6);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001349 br_group.vid = vid;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001350
1351 br_multicast_leave_group(br, port, &br_group);
1352}
1353#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001354
Herbert Xueb1d1642010-02-27 19:41:45 +00001355static int br_multicast_ipv4_rcv(struct net_bridge *br,
1356 struct net_bridge_port *port,
1357 struct sk_buff *skb)
1358{
1359 struct sk_buff *skb2 = skb;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001360 const struct iphdr *iph;
Herbert Xueb1d1642010-02-27 19:41:45 +00001361 struct igmphdr *ih;
Eric Dumazet95c96172012-04-15 05:58:06 +00001362 unsigned int len;
1363 unsigned int offset;
Herbert Xueb1d1642010-02-27 19:41:45 +00001364 int err;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001365 u16 vid = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001366
Herbert Xueb1d1642010-02-27 19:41:45 +00001367 /* We treat OOM as packet loss for now. */
1368 if (!pskb_may_pull(skb, sizeof(*iph)))
1369 return -EINVAL;
1370
1371 iph = ip_hdr(skb);
1372
1373 if (iph->ihl < 5 || iph->version != 4)
1374 return -EINVAL;
1375
1376 if (!pskb_may_pull(skb, ip_hdrlen(skb)))
1377 return -EINVAL;
1378
1379 iph = ip_hdr(skb);
1380
1381 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1382 return -EINVAL;
1383
Herbert Xubd4265f2011-06-23 02:39:12 +00001384 if (iph->protocol != IPPROTO_IGMP) {
Amerigo Wangbf5e4dd2013-03-07 02:32:26 +00001385 if (!ipv4_is_local_multicast(iph->daddr))
Herbert Xubd4265f2011-06-23 02:39:12 +00001386 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Herbert Xueb1d1642010-02-27 19:41:45 +00001387 return 0;
Herbert Xubd4265f2011-06-23 02:39:12 +00001388 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001389
1390 len = ntohs(iph->tot_len);
1391 if (skb->len < len || len < ip_hdrlen(skb))
1392 return -EINVAL;
1393
1394 if (skb->len > len) {
1395 skb2 = skb_clone(skb, GFP_ATOMIC);
1396 if (!skb2)
1397 return -ENOMEM;
1398
1399 err = pskb_trim_rcsum(skb2, len);
1400 if (err)
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001401 goto err_out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001402 }
1403
1404 len -= ip_hdrlen(skb2);
1405 offset = skb_network_offset(skb2) + ip_hdrlen(skb2);
1406 __skb_pull(skb2, offset);
1407 skb_reset_transport_header(skb2);
1408
1409 err = -EINVAL;
1410 if (!pskb_may_pull(skb2, sizeof(*ih)))
1411 goto out;
1412
Herbert Xueb1d1642010-02-27 19:41:45 +00001413 switch (skb2->ip_summed) {
1414 case CHECKSUM_COMPLETE:
1415 if (!csum_fold(skb2->csum))
1416 break;
1417 /* fall through */
1418 case CHECKSUM_NONE:
1419 skb2->csum = 0;
1420 if (skb_checksum_complete(skb2))
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001421 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001422 }
1423
1424 err = 0;
1425
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001426 br_vlan_get_tag(skb2, &vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001427 BR_INPUT_SKB_CB(skb)->igmp = 1;
1428 ih = igmp_hdr(skb2);
1429
1430 switch (ih->type) {
1431 case IGMP_HOST_MEMBERSHIP_REPORT:
1432 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Fernando Luis Vázquez Cao62b2bcb2011-06-13 15:04:43 +00001433 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001434 err = br_ip4_multicast_add_group(br, port, ih->group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001435 break;
1436 case IGMPV3_HOST_MEMBERSHIP_REPORT:
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001437 err = br_ip4_multicast_igmp3_report(br, port, skb2);
Herbert Xueb1d1642010-02-27 19:41:45 +00001438 break;
1439 case IGMP_HOST_MEMBERSHIP_QUERY:
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001440 err = br_ip4_multicast_query(br, port, skb2);
Herbert Xueb1d1642010-02-27 19:41:45 +00001441 break;
1442 case IGMP_HOST_LEAVE_MESSAGE:
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001443 br_ip4_multicast_leave_group(br, port, ih->group, vid);
Herbert Xueb1d1642010-02-27 19:41:45 +00001444 break;
1445 }
1446
1447out:
1448 __skb_push(skb2, offset);
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001449err_out:
Herbert Xueb1d1642010-02-27 19:41:45 +00001450 if (skb2 != skb)
1451 kfree_skb(skb2);
1452 return err;
1453}
1454
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001455#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001456static int br_multicast_ipv6_rcv(struct net_bridge *br,
1457 struct net_bridge_port *port,
1458 struct sk_buff *skb)
1459{
Tomas Winkler9d890812011-01-03 11:26:08 -08001460 struct sk_buff *skb2;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001461 const struct ipv6hdr *ip6h;
Eric Dumazet22df1332011-08-23 19:57:05 +00001462 u8 icmp6_type;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001463 u8 nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -08001464 __be16 frag_off;
Eric Dumazet95c96172012-04-15 05:58:06 +00001465 unsigned int len;
Kulikov Vasiliybb7a0bd2010-07-15 08:47:33 +00001466 int offset;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001467 int err;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001468 u16 vid = 0;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001469
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001470 if (!pskb_may_pull(skb, sizeof(*ip6h)))
1471 return -EINVAL;
1472
1473 ip6h = ipv6_hdr(skb);
1474
1475 /*
1476 * We're interested in MLD messages only.
1477 * - Version is 6
1478 * - MLD has always Router Alert hop-by-hop option
1479 * - But we do not support jumbrograms.
1480 */
1481 if (ip6h->version != 6 ||
1482 ip6h->nexthdr != IPPROTO_HOPOPTS ||
1483 ip6h->payload_len == 0)
1484 return 0;
1485
Linus Lüssingff9a57a2011-03-26 20:27:24 +00001486 len = ntohs(ip6h->payload_len) + sizeof(*ip6h);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001487 if (skb->len < len)
1488 return -EINVAL;
1489
1490 nexthdr = ip6h->nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -08001491 offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr, &frag_off);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001492
1493 if (offset < 0 || nexthdr != IPPROTO_ICMPV6)
1494 return 0;
1495
1496 /* Okay, we found ICMPv6 header */
1497 skb2 = skb_clone(skb, GFP_ATOMIC);
1498 if (!skb2)
1499 return -ENOMEM;
1500
Tomas Winkler9d890812011-01-03 11:26:08 -08001501 err = -EINVAL;
1502 if (!pskb_may_pull(skb2, offset + sizeof(struct icmp6hdr)))
1503 goto out;
1504
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001505 len -= offset - skb_network_offset(skb2);
1506
1507 __skb_pull(skb2, offset);
1508 skb_reset_transport_header(skb2);
stephen hemmingerfa2da8c2011-11-15 08:09:14 +00001509 skb_postpull_rcsum(skb2, skb_network_header(skb2),
1510 skb_network_header_len(skb2));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001511
Eric Dumazet22df1332011-08-23 19:57:05 +00001512 icmp6_type = icmp6_hdr(skb2)->icmp6_type;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001513
Eric Dumazet22df1332011-08-23 19:57:05 +00001514 switch (icmp6_type) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001515 case ICMPV6_MGM_QUERY:
1516 case ICMPV6_MGM_REPORT:
1517 case ICMPV6_MGM_REDUCTION:
1518 case ICMPV6_MLD2_REPORT:
1519 break;
1520 default:
1521 err = 0;
1522 goto out;
1523 }
1524
1525 /* Okay, we found MLD message. Check further. */
1526 if (skb2->len > len) {
1527 err = pskb_trim_rcsum(skb2, len);
1528 if (err)
1529 goto out;
Yan, Zheng4b275d72011-08-23 22:54:33 +00001530 err = -EINVAL;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001531 }
1532
Yan, Zheng4b275d72011-08-23 22:54:33 +00001533 ip6h = ipv6_hdr(skb2);
1534
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001535 switch (skb2->ip_summed) {
1536 case CHECKSUM_COMPLETE:
Yan, Zheng4b275d72011-08-23 22:54:33 +00001537 if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, skb2->len,
1538 IPPROTO_ICMPV6, skb2->csum))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001539 break;
1540 /*FALLTHROUGH*/
1541 case CHECKSUM_NONE:
Yan, Zheng4b275d72011-08-23 22:54:33 +00001542 skb2->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr,
1543 &ip6h->daddr,
1544 skb2->len,
1545 IPPROTO_ICMPV6, 0));
1546 if (__skb_checksum_complete(skb2))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001547 goto out;
1548 }
1549
1550 err = 0;
1551
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001552 br_vlan_get_tag(skb, &vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001553 BR_INPUT_SKB_CB(skb)->igmp = 1;
1554
Eric Dumazet22df1332011-08-23 19:57:05 +00001555 switch (icmp6_type) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001556 case ICMPV6_MGM_REPORT:
1557 {
Tomas Winkler9d890812011-01-03 11:26:08 -08001558 struct mld_msg *mld;
1559 if (!pskb_may_pull(skb2, sizeof(*mld))) {
1560 err = -EINVAL;
1561 goto out;
1562 }
1563 mld = (struct mld_msg *)skb_transport_header(skb2);
Fernando Luis Vázquez Caofc2af6c2011-06-13 15:06:58 +00001564 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001565 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001566 break;
1567 }
1568 case ICMPV6_MLD2_REPORT:
1569 err = br_ip6_multicast_mld2_report(br, port, skb2);
1570 break;
1571 case ICMPV6_MGM_QUERY:
1572 err = br_ip6_multicast_query(br, port, skb2);
1573 break;
1574 case ICMPV6_MGM_REDUCTION:
1575 {
Tomas Winkler9d890812011-01-03 11:26:08 -08001576 struct mld_msg *mld;
1577 if (!pskb_may_pull(skb2, sizeof(*mld))) {
1578 err = -EINVAL;
1579 goto out;
1580 }
1581 mld = (struct mld_msg *)skb_transport_header(skb2);
Vlad Yasevichb0e9a302013-02-13 12:00:17 +00001582 br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001583 }
1584 }
1585
1586out:
Tomas Winkler9d890812011-01-03 11:26:08 -08001587 kfree_skb(skb2);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001588 return err;
1589}
1590#endif
1591
Herbert Xueb1d1642010-02-27 19:41:45 +00001592int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1593 struct sk_buff *skb)
1594{
YOSHIFUJI Hideaki / 吉藤英明1fafc7a2010-04-25 08:06:40 +00001595 BR_INPUT_SKB_CB(skb)->igmp = 0;
1596 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1597
Herbert Xueb1d1642010-02-27 19:41:45 +00001598 if (br->multicast_disabled)
1599 return 0;
1600
1601 switch (skb->protocol) {
1602 case htons(ETH_P_IP):
1603 return br_multicast_ipv4_rcv(br, port, skb);
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001604#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001605 case htons(ETH_P_IPV6):
1606 return br_multicast_ipv6_rcv(br, port, skb);
1607#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001608 }
1609
1610 return 0;
1611}
1612
1613static void br_multicast_query_expired(unsigned long data)
1614{
1615 struct net_bridge *br = (void *)data;
1616
1617 spin_lock(&br->multicast_lock);
1618 if (br->multicast_startup_queries_sent <
1619 br->multicast_startup_query_count)
1620 br->multicast_startup_queries_sent++;
1621
1622 br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent);
1623
1624 spin_unlock(&br->multicast_lock);
1625}
1626
1627void br_multicast_init(struct net_bridge *br)
1628{
1629 br->hash_elasticity = 4;
1630 br->hash_max = 512;
1631
1632 br->multicast_router = 1;
Herbert Xuc5c23262012-04-13 02:37:42 +00001633 br->multicast_querier = 0;
Cong Wang1c8ad5b2013-05-21 21:52:54 +00001634 br->multicast_query_use_ifaddr = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001635 br->multicast_last_member_count = 2;
1636 br->multicast_startup_query_count = 2;
1637
1638 br->multicast_last_member_interval = HZ;
1639 br->multicast_query_response_interval = 10 * HZ;
1640 br->multicast_startup_query_interval = 125 * HZ / 4;
1641 br->multicast_query_interval = 125 * HZ;
1642 br->multicast_querier_interval = 255 * HZ;
1643 br->multicast_membership_interval = 260 * HZ;
1644
1645 spin_lock_init(&br->multicast_lock);
1646 setup_timer(&br->multicast_router_timer,
1647 br_multicast_local_router_expired, 0);
1648 setup_timer(&br->multicast_querier_timer,
Herbert Xubb63f1f2012-04-30 00:22:56 +00001649 br_multicast_querier_expired, (unsigned long)br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001650 setup_timer(&br->multicast_query_timer, br_multicast_query_expired,
1651 (unsigned long)br);
1652}
1653
1654void br_multicast_open(struct net_bridge *br)
1655{
1656 br->multicast_startup_queries_sent = 0;
1657
1658 if (br->multicast_disabled)
1659 return;
1660
1661 mod_timer(&br->multicast_query_timer, jiffies);
1662}
1663
1664void br_multicast_stop(struct net_bridge *br)
1665{
1666 struct net_bridge_mdb_htable *mdb;
1667 struct net_bridge_mdb_entry *mp;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001668 struct hlist_node *n;
Herbert Xueb1d1642010-02-27 19:41:45 +00001669 u32 ver;
1670 int i;
1671
1672 del_timer_sync(&br->multicast_router_timer);
1673 del_timer_sync(&br->multicast_querier_timer);
1674 del_timer_sync(&br->multicast_query_timer);
1675
1676 spin_lock_bh(&br->multicast_lock);
Eric Dumazete8051682010-11-15 06:38:10 +00001677 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001678 if (!mdb)
1679 goto out;
1680
1681 br->mdb = NULL;
1682
1683 ver = mdb->ver;
1684 for (i = 0; i < mdb->max; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001685 hlist_for_each_entry_safe(mp, n, &mdb->mhash[i],
Herbert Xueb1d1642010-02-27 19:41:45 +00001686 hlist[ver]) {
1687 del_timer(&mp->timer);
Cong Wang9f00b2e2013-05-21 21:52:55 +00001688 mp->timer_armed = false;
Herbert Xueb1d1642010-02-27 19:41:45 +00001689 call_rcu_bh(&mp->rcu, br_multicast_free_group);
1690 }
1691 }
1692
1693 if (mdb->old) {
1694 spin_unlock_bh(&br->multicast_lock);
Herbert Xu10cc2b52010-03-05 21:03:35 +00001695 rcu_barrier_bh();
Herbert Xueb1d1642010-02-27 19:41:45 +00001696 spin_lock_bh(&br->multicast_lock);
1697 WARN_ON(mdb->old);
1698 }
1699
1700 mdb->old = mdb;
1701 call_rcu_bh(&mdb->rcu, br_mdb_free);
1702
1703out:
1704 spin_unlock_bh(&br->multicast_lock);
1705}
Herbert Xu0909e112010-02-27 19:41:49 +00001706
1707int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1708{
1709 int err = -ENOENT;
1710
1711 spin_lock_bh(&br->multicast_lock);
1712 if (!netif_running(br->dev))
1713 goto unlock;
1714
1715 switch (val) {
1716 case 0:
1717 case 2:
1718 del_timer(&br->multicast_router_timer);
1719 /* fall through */
1720 case 1:
1721 br->multicast_router = val;
1722 err = 0;
1723 break;
1724
1725 default:
1726 err = -EINVAL;
1727 break;
1728 }
1729
1730unlock:
1731 spin_unlock_bh(&br->multicast_lock);
1732
1733 return err;
1734}
1735
1736int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1737{
1738 struct net_bridge *br = p->br;
1739 int err = -ENOENT;
1740
1741 spin_lock(&br->multicast_lock);
1742 if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED)
1743 goto unlock;
1744
1745 switch (val) {
1746 case 0:
1747 case 1:
1748 case 2:
1749 p->multicast_router = val;
1750 err = 0;
1751
1752 if (val < 2 && !hlist_unhashed(&p->rlist))
1753 hlist_del_init_rcu(&p->rlist);
1754
1755 if (val == 1)
1756 break;
1757
1758 del_timer(&p->multicast_router_timer);
1759
1760 if (val == 0)
1761 break;
1762
1763 br_multicast_add_router(br, p);
1764 break;
1765
1766 default:
1767 err = -EINVAL;
1768 break;
1769 }
1770
1771unlock:
1772 spin_unlock(&br->multicast_lock);
1773
1774 return err;
1775}
Herbert Xu561f1102010-02-27 19:41:50 +00001776
Herbert Xu74857212012-04-13 02:37:42 +00001777static void br_multicast_start_querier(struct net_bridge *br)
Herbert Xu561f1102010-02-27 19:41:50 +00001778{
1779 struct net_bridge_port *port;
Herbert Xu74857212012-04-13 02:37:42 +00001780
1781 br_multicast_open(br);
1782
1783 list_for_each_entry(port, &br->port_list, list) {
1784 if (port->state == BR_STATE_DISABLED ||
1785 port->state == BR_STATE_BLOCKING)
1786 continue;
1787
1788 __br_multicast_enable_port(port);
1789 }
1790}
1791
1792int br_multicast_toggle(struct net_bridge *br, unsigned long val)
1793{
Herbert Xu3a7fda02010-07-29 00:45:30 +00001794 int err = 0;
Eric Dumazete8051682010-11-15 06:38:10 +00001795 struct net_bridge_mdb_htable *mdb;
Herbert Xu561f1102010-02-27 19:41:50 +00001796
Andrey Vaginef5e0d82011-11-10 05:48:03 +00001797 spin_lock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00001798 if (br->multicast_disabled == !val)
1799 goto unlock;
1800
1801 br->multicast_disabled = !val;
1802 if (br->multicast_disabled)
1803 goto unlock;
1804
Herbert Xu3a7fda02010-07-29 00:45:30 +00001805 if (!netif_running(br->dev))
1806 goto unlock;
1807
Eric Dumazete8051682010-11-15 06:38:10 +00001808 mdb = mlock_dereference(br->mdb, br);
1809 if (mdb) {
1810 if (mdb->old) {
Herbert Xu561f1102010-02-27 19:41:50 +00001811 err = -EEXIST;
1812rollback:
1813 br->multicast_disabled = !!val;
1814 goto unlock;
1815 }
1816
Eric Dumazete8051682010-11-15 06:38:10 +00001817 err = br_mdb_rehash(&br->mdb, mdb->max,
Herbert Xu561f1102010-02-27 19:41:50 +00001818 br->hash_elasticity);
1819 if (err)
1820 goto rollback;
1821 }
1822
Herbert Xu74857212012-04-13 02:37:42 +00001823 br_multicast_start_querier(br);
Herbert Xu561f1102010-02-27 19:41:50 +00001824
1825unlock:
Andrey Vaginef5e0d82011-11-10 05:48:03 +00001826 spin_unlock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00001827
1828 return err;
1829}
Herbert Xub1951672010-02-27 19:41:51 +00001830
Herbert Xuc5c23262012-04-13 02:37:42 +00001831int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
1832{
1833 val = !!val;
1834
1835 spin_lock_bh(&br->multicast_lock);
1836 if (br->multicast_querier == val)
1837 goto unlock;
1838
1839 br->multicast_querier = val;
1840 if (val)
1841 br_multicast_start_querier(br);
1842
1843unlock:
1844 spin_unlock_bh(&br->multicast_lock);
1845
1846 return 0;
1847}
1848
Herbert Xub1951672010-02-27 19:41:51 +00001849int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
1850{
1851 int err = -ENOENT;
1852 u32 old;
Eric Dumazete8051682010-11-15 06:38:10 +00001853 struct net_bridge_mdb_htable *mdb;
Herbert Xub1951672010-02-27 19:41:51 +00001854
1855 spin_lock(&br->multicast_lock);
1856 if (!netif_running(br->dev))
1857 goto unlock;
1858
1859 err = -EINVAL;
1860 if (!is_power_of_2(val))
1861 goto unlock;
Eric Dumazete8051682010-11-15 06:38:10 +00001862
1863 mdb = mlock_dereference(br->mdb, br);
1864 if (mdb && val < mdb->size)
Herbert Xub1951672010-02-27 19:41:51 +00001865 goto unlock;
1866
1867 err = 0;
1868
1869 old = br->hash_max;
1870 br->hash_max = val;
1871
Eric Dumazete8051682010-11-15 06:38:10 +00001872 if (mdb) {
1873 if (mdb->old) {
Herbert Xub1951672010-02-27 19:41:51 +00001874 err = -EEXIST;
1875rollback:
1876 br->hash_max = old;
1877 goto unlock;
1878 }
1879
1880 err = br_mdb_rehash(&br->mdb, br->hash_max,
1881 br->hash_elasticity);
1882 if (err)
1883 goto rollback;
1884 }
1885
1886unlock:
1887 spin_unlock(&br->multicast_lock);
1888
1889 return err;
1890}