blob: 702a1ae9220b79bd9e60cc25732201fe8750dcd2 [file] [log] [blame]
Herbert Xueb1d1642010-02-27 19:41:45 +00001/*
2 * Bridge multicast support.
3 *
4 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 */
12
13#include <linux/err.h>
14#include <linux/if_ether.h>
15#include <linux/igmp.h>
16#include <linux/jhash.h>
17#include <linux/kernel.h>
Herbert Xub1951672010-02-27 19:41:51 +000018#include <linux/log2.h>
Herbert Xueb1d1642010-02-27 19:41:45 +000019#include <linux/netdevice.h>
20#include <linux/netfilter_bridge.h>
21#include <linux/random.h>
22#include <linux/rculist.h>
23#include <linux/skbuff.h>
24#include <linux/slab.h>
25#include <linux/timer.h>
26#include <net/ip.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000027#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090028#include <net/ipv6.h>
29#include <net/mld.h>
30#include <net/addrconf.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
Eric Dumazete8051682010-11-15 06:38:10 +000036#define mlock_dereference(X, br) \
37 rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock))
38
Eric Dumazetdfd56b82011-12-10 09:48:31 +000039#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssinge4de9f92011-02-15 13:19:21 +000040static inline int ipv6_is_transient_multicast(const struct in6_addr *addr)
Herbert Xueb1d1642010-02-27 19:41:45 +000041{
Linus Lüssinge4de9f92011-02-15 13:19:21 +000042 if (ipv6_addr_is_multicast(addr) && IPV6_ADDR_MC_FLAG_TRANSIENT(addr))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090043 return 1;
44 return 0;
45}
46#endif
47
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090048static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
49{
50 if (a->proto != b->proto)
51 return 0;
52 switch (a->proto) {
53 case htons(ETH_P_IP):
54 return a->u.ip4 == b->u.ip4;
Eric Dumazetdfd56b82011-12-10 09:48:31 +000055#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090056 case htons(ETH_P_IPV6):
57 return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
58#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090059 }
60 return 0;
61}
62
63static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip)
Herbert Xueb1d1642010-02-27 19:41:45 +000064{
Eric Dumazet0eae88f2010-04-20 19:06:52 -070065 return jhash_1word(mdb->secret, (__force u32)ip) & (mdb->max - 1);
Herbert Xueb1d1642010-02-27 19:41:45 +000066}
67
Eric Dumazetdfd56b82011-12-10 09:48:31 +000068#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090069static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb,
70 const struct in6_addr *ip)
71{
72 return jhash2((__force u32 *)ip->s6_addr32, 4, mdb->secret) & (mdb->max - 1);
73}
74#endif
75
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090076static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb,
77 struct br_ip *ip)
78{
79 switch (ip->proto) {
80 case htons(ETH_P_IP):
81 return __br_ip4_hash(mdb, ip->u.ip4);
Eric Dumazetdfd56b82011-12-10 09:48:31 +000082#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +090083 case htons(ETH_P_IPV6):
84 return __br_ip6_hash(mdb, &ip->u.ip6);
85#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090086 }
87 return 0;
Herbert Xueb1d1642010-02-27 19:41:45 +000088}
89
90static struct net_bridge_mdb_entry *__br_mdb_ip_get(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090091 struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +000092{
93 struct net_bridge_mdb_entry *mp;
94 struct hlist_node *p;
95
Herbert Xu49f5fcf2010-03-05 21:07:39 +000096 hlist_for_each_entry_rcu(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) {
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +090097 if (br_ip_equal(&mp->addr, dst))
Herbert Xueb1d1642010-02-27 19:41:45 +000098 return mp;
99 }
100
101 return NULL;
102}
103
Herbert Xu7f285fa2010-07-05 14:50:08 +0000104static struct net_bridge_mdb_entry *br_mdb_ip_get(
105 struct net_bridge_mdb_htable *mdb, struct br_ip *dst)
106{
107 if (!mdb)
108 return NULL;
109
110 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
111}
112
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900113static struct net_bridge_mdb_entry *br_mdb_ip4_get(
Herbert Xueb1d1642010-02-27 19:41:45 +0000114 struct net_bridge_mdb_htable *mdb, __be32 dst)
115{
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900116 struct br_ip br_dst;
Herbert Xu0821ec52010-03-15 20:38:25 -0700117
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900118 br_dst.u.ip4 = dst;
119 br_dst.proto = htons(ETH_P_IP);
120
Herbert Xu7f285fa2010-07-05 14:50:08 +0000121 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900122}
123
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000124#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900125static struct net_bridge_mdb_entry *br_mdb_ip6_get(
126 struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst)
127{
128 struct br_ip br_dst;
129
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000130 br_dst.u.ip6 = *dst;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900131 br_dst.proto = htons(ETH_P_IPV6);
132
Herbert Xu7f285fa2010-07-05 14:50:08 +0000133 return br_mdb_ip_get(mdb, &br_dst);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900134}
135#endif
136
Herbert Xueb1d1642010-02-27 19:41:45 +0000137struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
138 struct sk_buff *skb)
139{
Eric Dumazete8051682010-11-15 06:38:10 +0000140 struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900141 struct br_ip ip;
142
Herbert Xu7f285fa2010-07-05 14:50:08 +0000143 if (br->multicast_disabled)
Herbert Xueb1d1642010-02-27 19:41:45 +0000144 return NULL;
145
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900146 if (BR_INPUT_SKB_CB(skb)->igmp)
147 return NULL;
148
149 ip.proto = skb->protocol;
150
Herbert Xueb1d1642010-02-27 19:41:45 +0000151 switch (skb->protocol) {
152 case htons(ETH_P_IP):
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900153 ip.u.ip4 = ip_hdr(skb)->daddr;
154 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000155#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900156 case htons(ETH_P_IPV6):
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000157 ip.u.ip6 = ipv6_hdr(skb)->daddr;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900158 break;
159#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900160 default:
161 return NULL;
Herbert Xueb1d1642010-02-27 19:41:45 +0000162 }
163
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900164 return br_mdb_ip_get(mdb, &ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000165}
166
167static void br_mdb_free(struct rcu_head *head)
168{
169 struct net_bridge_mdb_htable *mdb =
170 container_of(head, struct net_bridge_mdb_htable, rcu);
171 struct net_bridge_mdb_htable *old = mdb->old;
172
173 mdb->old = NULL;
174 kfree(old->mhash);
175 kfree(old);
176}
177
178static int br_mdb_copy(struct net_bridge_mdb_htable *new,
179 struct net_bridge_mdb_htable *old,
180 int elasticity)
181{
182 struct net_bridge_mdb_entry *mp;
183 struct hlist_node *p;
184 int maxlen;
185 int len;
186 int i;
187
188 for (i = 0; i < old->max; i++)
189 hlist_for_each_entry(mp, p, &old->mhash[i], hlist[old->ver])
190 hlist_add_head(&mp->hlist[new->ver],
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900191 &new->mhash[br_ip_hash(new, &mp->addr)]);
Herbert Xueb1d1642010-02-27 19:41:45 +0000192
193 if (!elasticity)
194 return 0;
195
196 maxlen = 0;
197 for (i = 0; i < new->max; i++) {
198 len = 0;
199 hlist_for_each_entry(mp, p, &new->mhash[i], hlist[new->ver])
200 len++;
201 if (len > maxlen)
202 maxlen = len;
203 }
204
205 return maxlen > elasticity ? -EINVAL : 0;
206}
207
208static void br_multicast_free_pg(struct rcu_head *head)
209{
210 struct net_bridge_port_group *p =
211 container_of(head, struct net_bridge_port_group, rcu);
212
213 kfree(p);
214}
215
216static void br_multicast_free_group(struct rcu_head *head)
217{
218 struct net_bridge_mdb_entry *mp =
219 container_of(head, struct net_bridge_mdb_entry, rcu);
220
221 kfree(mp);
222}
223
224static void br_multicast_group_expired(unsigned long data)
225{
226 struct net_bridge_mdb_entry *mp = (void *)data;
227 struct net_bridge *br = mp->br;
228 struct net_bridge_mdb_htable *mdb;
229
230 spin_lock(&br->multicast_lock);
231 if (!netif_running(br->dev) || timer_pending(&mp->timer))
232 goto out;
233
Herbert Xu8a870172011-02-12 01:05:42 -0800234 mp->mglist = false;
Herbert Xueb1d1642010-02-27 19:41:45 +0000235
236 if (mp->ports)
237 goto out;
238
Eric Dumazete8051682010-11-15 06:38:10 +0000239 mdb = mlock_dereference(br->mdb, br);
240
Herbert Xueb1d1642010-02-27 19:41:45 +0000241 hlist_del_rcu(&mp->hlist[mdb->ver]);
242 mdb->size--;
243
244 del_timer(&mp->query_timer);
245 call_rcu_bh(&mp->rcu, br_multicast_free_group);
246
247out:
248 spin_unlock(&br->multicast_lock);
249}
250
251static void br_multicast_del_pg(struct net_bridge *br,
252 struct net_bridge_port_group *pg)
253{
Eric Dumazete8051682010-11-15 06:38:10 +0000254 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000255 struct net_bridge_mdb_entry *mp;
256 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +0000257 struct net_bridge_port_group __rcu **pp;
258
259 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +0000260
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900261 mp = br_mdb_ip_get(mdb, &pg->addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000262 if (WARN_ON(!mp))
263 return;
264
Eric Dumazete8051682010-11-15 06:38:10 +0000265 for (pp = &mp->ports;
266 (p = mlock_dereference(*pp, br)) != NULL;
267 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000268 if (p != pg)
269 continue;
270
stephen hemminger83f6a742010-04-27 15:01:06 +0000271 rcu_assign_pointer(*pp, p->next);
Herbert Xueb1d1642010-02-27 19:41:45 +0000272 hlist_del_init(&p->mglist);
273 del_timer(&p->timer);
274 del_timer(&p->query_timer);
275 call_rcu_bh(&p->rcu, br_multicast_free_pg);
276
Herbert Xu8a870172011-02-12 01:05:42 -0800277 if (!mp->ports && !mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +0000278 netif_running(br->dev))
279 mod_timer(&mp->timer, jiffies);
280
281 return;
282 }
283
284 WARN_ON(1);
285}
286
287static void br_multicast_port_group_expired(unsigned long data)
288{
289 struct net_bridge_port_group *pg = (void *)data;
290 struct net_bridge *br = pg->port->br;
291
292 spin_lock(&br->multicast_lock);
293 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
294 hlist_unhashed(&pg->mglist))
295 goto out;
296
297 br_multicast_del_pg(br, pg);
298
299out:
300 spin_unlock(&br->multicast_lock);
301}
302
Eric Dumazete8051682010-11-15 06:38:10 +0000303static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max,
Herbert Xueb1d1642010-02-27 19:41:45 +0000304 int elasticity)
305{
Eric Dumazete8051682010-11-15 06:38:10 +0000306 struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000307 struct net_bridge_mdb_htable *mdb;
308 int err;
309
310 mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
311 if (!mdb)
312 return -ENOMEM;
313
314 mdb->max = max;
315 mdb->old = old;
316
317 mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC);
318 if (!mdb->mhash) {
319 kfree(mdb);
320 return -ENOMEM;
321 }
322
323 mdb->size = old ? old->size : 0;
324 mdb->ver = old ? old->ver ^ 1 : 0;
325
326 if (!old || elasticity)
327 get_random_bytes(&mdb->secret, sizeof(mdb->secret));
328 else
329 mdb->secret = old->secret;
330
331 if (!old)
332 goto out;
333
334 err = br_mdb_copy(mdb, old, elasticity);
335 if (err) {
336 kfree(mdb->mhash);
337 kfree(mdb);
338 return err;
339 }
340
341 call_rcu_bh(&mdb->rcu, br_mdb_free);
342
343out:
344 rcu_assign_pointer(*mdbp, mdb);
345
346 return 0;
347}
348
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900349static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
350 __be32 group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000351{
352 struct sk_buff *skb;
353 struct igmphdr *ih;
354 struct ethhdr *eth;
355 struct iphdr *iph;
356
357 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
358 sizeof(*ih) + 4);
359 if (!skb)
360 goto out;
361
362 skb->protocol = htons(ETH_P_IP);
363
364 skb_reset_mac_header(skb);
365 eth = eth_hdr(skb);
366
367 memcpy(eth->h_source, br->dev->dev_addr, 6);
368 eth->h_dest[0] = 1;
369 eth->h_dest[1] = 0;
370 eth->h_dest[2] = 0x5e;
371 eth->h_dest[3] = 0;
372 eth->h_dest[4] = 0;
373 eth->h_dest[5] = 1;
374 eth->h_proto = htons(ETH_P_IP);
375 skb_put(skb, sizeof(*eth));
376
377 skb_set_network_header(skb, skb->len);
378 iph = ip_hdr(skb);
379
380 iph->version = 4;
381 iph->ihl = 6;
382 iph->tos = 0xc0;
383 iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4);
384 iph->id = 0;
385 iph->frag_off = htons(IP_DF);
386 iph->ttl = 1;
387 iph->protocol = IPPROTO_IGMP;
388 iph->saddr = 0;
389 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
390 ((u8 *)&iph[1])[0] = IPOPT_RA;
391 ((u8 *)&iph[1])[1] = 4;
392 ((u8 *)&iph[1])[2] = 0;
393 ((u8 *)&iph[1])[3] = 0;
394 ip_send_check(iph);
395 skb_put(skb, 24);
396
397 skb_set_transport_header(skb, skb->len);
398 ih = igmp_hdr(skb);
399 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
400 ih->code = (group ? br->multicast_last_member_interval :
401 br->multicast_query_response_interval) /
402 (HZ / IGMP_TIMER_SCALE);
403 ih->group = group;
404 ih->csum = 0;
405 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
406 skb_put(skb, sizeof(*ih));
407
408 __skb_pull(skb, sizeof(*eth));
409
410out:
411 return skb;
412}
413
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000414#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900415static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000416 const struct in6_addr *group)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900417{
418 struct sk_buff *skb;
419 struct ipv6hdr *ip6h;
420 struct mld_msg *mldq;
421 struct ethhdr *eth;
422 u8 *hopopt;
423 unsigned long interval;
424
425 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
426 8 + sizeof(*mldq));
427 if (!skb)
428 goto out;
429
430 skb->protocol = htons(ETH_P_IPV6);
431
432 /* Ethernet header */
433 skb_reset_mac_header(skb);
434 eth = eth_hdr(skb);
435
436 memcpy(eth->h_source, br->dev->dev_addr, 6);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900437 eth->h_proto = htons(ETH_P_IPV6);
438 skb_put(skb, sizeof(*eth));
439
440 /* IPv6 header + HbH option */
441 skb_set_network_header(skb, skb->len);
442 ip6h = ipv6_hdr(skb);
443
444 *(__force __be32 *)ip6h = htonl(0x60000000);
David Stevens76d66152010-12-14 08:42:16 +0000445 ip6h->payload_len = htons(8 + sizeof(*mldq));
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900446 ip6h->nexthdr = IPPROTO_HOPOPTS;
447 ip6h->hop_limit = 1;
Linus Lüssinga7bff752011-03-22 11:40:32 +0000448 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
Ulrich Weberd1d81d42012-03-05 04:52:44 +0000449 if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
450 &ip6h->saddr)) {
451 kfree_skb(skb);
452 return NULL;
453 }
Linus Lüssing36cff5a2011-02-17 08:17:51 +0000454 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900455
456 hopopt = (u8 *)(ip6h + 1);
457 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
458 hopopt[1] = 0; /* length of HbH */
459 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
460 hopopt[3] = 2; /* Length of RA Option */
461 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
462 hopopt[5] = 0;
463 hopopt[6] = IPV6_TLV_PAD0; /* Pad0 */
464 hopopt[7] = IPV6_TLV_PAD0; /* Pad0 */
465
466 skb_put(skb, sizeof(*ip6h) + 8);
467
468 /* ICMPv6 */
469 skb_set_transport_header(skb, skb->len);
470 mldq = (struct mld_msg *) icmp6_hdr(skb);
471
472 interval = ipv6_addr_any(group) ? br->multicast_last_member_interval :
473 br->multicast_query_response_interval;
474
475 mldq->mld_type = ICMPV6_MGM_QUERY;
476 mldq->mld_code = 0;
477 mldq->mld_cksum = 0;
478 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
479 mldq->mld_reserved = 0;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000480 mldq->mld_mca = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900481
482 /* checksum */
483 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
484 sizeof(*mldq), IPPROTO_ICMPV6,
485 csum_partial(mldq,
486 sizeof(*mldq), 0));
487 skb_put(skb, sizeof(*mldq));
488
489 __skb_pull(skb, sizeof(*eth));
490
491out:
492 return skb;
493}
494#endif
495
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900496static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
497 struct br_ip *addr)
498{
499 switch (addr->proto) {
500 case htons(ETH_P_IP):
501 return br_ip4_multicast_alloc_query(br, addr->u.ip4);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000502#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900503 case htons(ETH_P_IPV6):
504 return br_ip6_multicast_alloc_query(br, &addr->u.ip6);
505#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900506 }
507 return NULL;
508}
509
Herbert Xueb1d1642010-02-27 19:41:45 +0000510static void br_multicast_send_group_query(struct net_bridge_mdb_entry *mp)
511{
512 struct net_bridge *br = mp->br;
513 struct sk_buff *skb;
514
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900515 skb = br_multicast_alloc_query(br, &mp->addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000516 if (!skb)
517 goto timer;
518
519 netif_rx(skb);
520
521timer:
522 if (++mp->queries_sent < br->multicast_last_member_count)
523 mod_timer(&mp->query_timer,
524 jiffies + br->multicast_last_member_interval);
525}
526
527static void br_multicast_group_query_expired(unsigned long data)
528{
529 struct net_bridge_mdb_entry *mp = (void *)data;
530 struct net_bridge *br = mp->br;
531
532 spin_lock(&br->multicast_lock);
Herbert Xu8a870172011-02-12 01:05:42 -0800533 if (!netif_running(br->dev) || !mp->mglist ||
Herbert Xueb1d1642010-02-27 19:41:45 +0000534 mp->queries_sent >= br->multicast_last_member_count)
535 goto out;
536
537 br_multicast_send_group_query(mp);
538
539out:
540 spin_unlock(&br->multicast_lock);
541}
542
543static void br_multicast_send_port_group_query(struct net_bridge_port_group *pg)
544{
545 struct net_bridge_port *port = pg->port;
546 struct net_bridge *br = port->br;
547 struct sk_buff *skb;
548
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900549 skb = br_multicast_alloc_query(br, &pg->addr);
Herbert Xueb1d1642010-02-27 19:41:45 +0000550 if (!skb)
551 goto timer;
552
553 br_deliver(port, skb);
554
555timer:
556 if (++pg->queries_sent < br->multicast_last_member_count)
557 mod_timer(&pg->query_timer,
558 jiffies + br->multicast_last_member_interval);
559}
560
561static void br_multicast_port_group_query_expired(unsigned long data)
562{
563 struct net_bridge_port_group *pg = (void *)data;
564 struct net_bridge_port *port = pg->port;
565 struct net_bridge *br = port->br;
566
567 spin_lock(&br->multicast_lock);
568 if (!netif_running(br->dev) || hlist_unhashed(&pg->mglist) ||
569 pg->queries_sent >= br->multicast_last_member_count)
570 goto out;
571
572 br_multicast_send_port_group_query(pg);
573
574out:
575 spin_unlock(&br->multicast_lock);
576}
577
578static struct net_bridge_mdb_entry *br_multicast_get_group(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900579 struct net_bridge *br, struct net_bridge_port *port,
580 struct br_ip *group, int hash)
Herbert Xueb1d1642010-02-27 19:41:45 +0000581{
Eric Dumazete8051682010-11-15 06:38:10 +0000582 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000583 struct net_bridge_mdb_entry *mp;
584 struct hlist_node *p;
585 unsigned count = 0;
586 unsigned max;
587 int elasticity;
588 int err;
589
Eric Dumazete8051682010-11-15 06:38:10 +0000590 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000591 hlist_for_each_entry(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) {
592 count++;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900593 if (unlikely(br_ip_equal(group, &mp->addr)))
Herbert Xueb1d1642010-02-27 19:41:45 +0000594 return mp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000595 }
596
597 elasticity = 0;
598 max = mdb->max;
599
600 if (unlikely(count > br->hash_elasticity && count)) {
601 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000602 br_info(br, "Multicast hash table "
603 "chain limit reached: %s\n",
604 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000605
606 elasticity = br->hash_elasticity;
607 }
608
609 if (mdb->size >= max) {
610 max *= 2;
611 if (unlikely(max >= br->hash_max)) {
stephen hemminger28a16c92010-05-10 09:31:09 +0000612 br_warn(br, "Multicast hash table maximum "
613 "reached, disabling snooping: %s, %d\n",
614 port ? port->dev->name : br->dev->name, max);
Herbert Xueb1d1642010-02-27 19:41:45 +0000615 err = -E2BIG;
616disable:
617 br->multicast_disabled = 1;
618 goto err;
619 }
620 }
621
622 if (max > mdb->max || elasticity) {
623 if (mdb->old) {
624 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000625 br_info(br, "Multicast hash table "
626 "on fire: %s\n",
627 port ? port->dev->name : br->dev->name);
Herbert Xueb1d1642010-02-27 19:41:45 +0000628 err = -EEXIST;
629 goto err;
630 }
631
632 err = br_mdb_rehash(&br->mdb, max, elasticity);
633 if (err) {
stephen hemminger28a16c92010-05-10 09:31:09 +0000634 br_warn(br, "Cannot rehash multicast "
635 "hash table, disabling snooping: %s, %d, %d\n",
636 port ? port->dev->name : br->dev->name,
637 mdb->size, err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000638 goto disable;
639 }
640
641 err = -EAGAIN;
642 goto err;
643 }
644
645 return NULL;
646
647err:
648 mp = ERR_PTR(err);
649 return mp;
650}
651
652static struct net_bridge_mdb_entry *br_multicast_new_group(
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900653 struct net_bridge *br, struct net_bridge_port *port,
654 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000655{
Eric Dumazete8051682010-11-15 06:38:10 +0000656 struct net_bridge_mdb_htable *mdb;
Herbert Xueb1d1642010-02-27 19:41:45 +0000657 struct net_bridge_mdb_entry *mp;
658 int hash;
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000659 int err;
Herbert Xueb1d1642010-02-27 19:41:45 +0000660
Eric Dumazete8051682010-11-15 06:38:10 +0000661 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000662 if (!mdb) {
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000663 err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0);
664 if (err)
665 return ERR_PTR(err);
Herbert Xueb1d1642010-02-27 19:41:45 +0000666 goto rehash;
667 }
668
669 hash = br_ip_hash(mdb, group);
670 mp = br_multicast_get_group(br, port, group, hash);
671 switch (PTR_ERR(mp)) {
672 case 0:
673 break;
674
675 case -EAGAIN:
676rehash:
Eric Dumazete8051682010-11-15 06:38:10 +0000677 mdb = rcu_dereference_protected(br->mdb, 1);
Herbert Xueb1d1642010-02-27 19:41:45 +0000678 hash = br_ip_hash(mdb, group);
679 break;
680
681 default:
682 goto out;
683 }
684
685 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
686 if (unlikely(!mp))
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000687 return ERR_PTR(-ENOMEM);
Herbert Xueb1d1642010-02-27 19:41:45 +0000688
689 mp->br = br;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900690 mp->addr = *group;
Herbert Xueb1d1642010-02-27 19:41:45 +0000691 setup_timer(&mp->timer, br_multicast_group_expired,
692 (unsigned long)mp);
693 setup_timer(&mp->query_timer, br_multicast_group_query_expired,
694 (unsigned long)mp);
695
696 hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
697 mdb->size++;
698
699out:
700 return mp;
701}
702
703static int br_multicast_add_group(struct net_bridge *br,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900704 struct net_bridge_port *port,
705 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +0000706{
707 struct net_bridge_mdb_entry *mp;
708 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +0000709 struct net_bridge_port_group __rcu **pp;
Herbert Xueb1d1642010-02-27 19:41:45 +0000710 unsigned long now = jiffies;
711 int err;
712
Herbert Xueb1d1642010-02-27 19:41:45 +0000713 spin_lock(&br->multicast_lock);
714 if (!netif_running(br->dev) ||
715 (port && port->state == BR_STATE_DISABLED))
716 goto out;
717
718 mp = br_multicast_new_group(br, port, group);
719 err = PTR_ERR(mp);
Tobias Klauser4c0833b2010-12-10 03:18:04 +0000720 if (IS_ERR(mp))
Herbert Xueb1d1642010-02-27 19:41:45 +0000721 goto err;
722
723 if (!port) {
Herbert Xu8a870172011-02-12 01:05:42 -0800724 mp->mglist = true;
Herbert Xueb1d1642010-02-27 19:41:45 +0000725 mod_timer(&mp->timer, now + br->multicast_membership_interval);
726 goto out;
727 }
728
Eric Dumazete8051682010-11-15 06:38:10 +0000729 for (pp = &mp->ports;
730 (p = mlock_dereference(*pp, br)) != NULL;
731 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +0000732 if (p->port == port)
733 goto found;
734 if ((unsigned long)p->port < (unsigned long)port)
735 break;
736 }
737
738 p = kzalloc(sizeof(*p), GFP_ATOMIC);
739 err = -ENOMEM;
740 if (unlikely(!p))
741 goto err;
742
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900743 p->addr = *group;
Herbert Xueb1d1642010-02-27 19:41:45 +0000744 p->port = port;
745 p->next = *pp;
746 hlist_add_head(&p->mglist, &port->mglist);
747 setup_timer(&p->timer, br_multicast_port_group_expired,
748 (unsigned long)p);
749 setup_timer(&p->query_timer, br_multicast_port_group_query_expired,
750 (unsigned long)p);
751
752 rcu_assign_pointer(*pp, p);
753
754found:
755 mod_timer(&p->timer, now + br->multicast_membership_interval);
756out:
757 err = 0;
758
759err:
760 spin_unlock(&br->multicast_lock);
761 return err;
762}
763
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900764static int br_ip4_multicast_add_group(struct net_bridge *br,
765 struct net_bridge_port *port,
766 __be32 group)
767{
768 struct br_ip br_group;
769
770 if (ipv4_is_local_multicast(group))
771 return 0;
772
773 br_group.u.ip4 = group;
774 br_group.proto = htons(ETH_P_IP);
775
776 return br_multicast_add_group(br, port, &br_group);
777}
778
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000779#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900780static int br_ip6_multicast_add_group(struct net_bridge *br,
781 struct net_bridge_port *port,
782 const struct in6_addr *group)
783{
784 struct br_ip br_group;
785
Linus Lüssinge4de9f92011-02-15 13:19:21 +0000786 if (!ipv6_is_transient_multicast(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900787 return 0;
788
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000789 br_group.u.ip6 = *group;
Linus Lüssing9cc6e0c2011-02-15 13:19:17 +0000790 br_group.proto = htons(ETH_P_IPV6);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900791
792 return br_multicast_add_group(br, port, &br_group);
793}
794#endif
795
Herbert Xueb1d1642010-02-27 19:41:45 +0000796static void br_multicast_router_expired(unsigned long data)
797{
798 struct net_bridge_port *port = (void *)data;
799 struct net_bridge *br = port->br;
800
801 spin_lock(&br->multicast_lock);
802 if (port->multicast_router != 1 ||
803 timer_pending(&port->multicast_router_timer) ||
804 hlist_unhashed(&port->rlist))
805 goto out;
806
807 hlist_del_init_rcu(&port->rlist);
808
809out:
810 spin_unlock(&br->multicast_lock);
811}
812
813static void br_multicast_local_router_expired(unsigned long data)
814{
815}
816
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900817static void __br_multicast_send_query(struct net_bridge *br,
818 struct net_bridge_port *port,
819 struct br_ip *ip)
Herbert Xueb1d1642010-02-27 19:41:45 +0000820{
Herbert Xueb1d1642010-02-27 19:41:45 +0000821 struct sk_buff *skb;
822
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900823 skb = br_multicast_alloc_query(br, ip);
Herbert Xueb1d1642010-02-27 19:41:45 +0000824 if (!skb)
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900825 return;
Herbert Xueb1d1642010-02-27 19:41:45 +0000826
827 if (port) {
828 __skb_push(skb, sizeof(struct ethhdr));
829 skb->dev = port->dev;
Jan Engelhardt713aefa2010-03-23 04:07:21 +0100830 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
Herbert Xueb1d1642010-02-27 19:41:45 +0000831 dev_queue_xmit);
832 } else
833 netif_rx(skb);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900834}
Herbert Xueb1d1642010-02-27 19:41:45 +0000835
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900836static void br_multicast_send_query(struct net_bridge *br,
837 struct net_bridge_port *port, u32 sent)
838{
839 unsigned long time;
840 struct br_ip br_group;
841
842 if (!netif_running(br->dev) || br->multicast_disabled ||
843 timer_pending(&br->multicast_querier_timer))
844 return;
845
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900846 memset(&br_group.u, 0, sizeof(br_group.u));
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900847
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900848 br_group.proto = htons(ETH_P_IP);
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900849 __br_multicast_send_query(br, port, &br_group);
850
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000851#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900852 br_group.proto = htons(ETH_P_IPV6);
853 __br_multicast_send_query(br, port, &br_group);
854#endif
855
Herbert Xueb1d1642010-02-27 19:41:45 +0000856 time = jiffies;
857 time += sent < br->multicast_startup_query_count ?
858 br->multicast_startup_query_interval :
859 br->multicast_query_interval;
860 mod_timer(port ? &port->multicast_query_timer :
861 &br->multicast_query_timer, time);
862}
863
864static void br_multicast_port_query_expired(unsigned long data)
865{
866 struct net_bridge_port *port = (void *)data;
867 struct net_bridge *br = port->br;
868
869 spin_lock(&br->multicast_lock);
Dan Carpenter02a780c2010-03-06 01:14:09 +0000870 if (port->state == BR_STATE_DISABLED ||
871 port->state == BR_STATE_BLOCKING)
Herbert Xueb1d1642010-02-27 19:41:45 +0000872 goto out;
873
874 if (port->multicast_startup_queries_sent <
875 br->multicast_startup_query_count)
876 port->multicast_startup_queries_sent++;
877
878 br_multicast_send_query(port->br, port,
879 port->multicast_startup_queries_sent);
880
881out:
882 spin_unlock(&br->multicast_lock);
883}
884
885void br_multicast_add_port(struct net_bridge_port *port)
886{
887 port->multicast_router = 1;
888
889 setup_timer(&port->multicast_router_timer, br_multicast_router_expired,
890 (unsigned long)port);
891 setup_timer(&port->multicast_query_timer,
892 br_multicast_port_query_expired, (unsigned long)port);
893}
894
895void br_multicast_del_port(struct net_bridge_port *port)
896{
897 del_timer_sync(&port->multicast_router_timer);
898}
899
Herbert Xu561f1102010-02-27 19:41:50 +0000900static void __br_multicast_enable_port(struct net_bridge_port *port)
901{
902 port->multicast_startup_queries_sent = 0;
903
904 if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 ||
905 del_timer(&port->multicast_query_timer))
906 mod_timer(&port->multicast_query_timer, jiffies);
907}
908
Herbert Xueb1d1642010-02-27 19:41:45 +0000909void br_multicast_enable_port(struct net_bridge_port *port)
910{
911 struct net_bridge *br = port->br;
912
913 spin_lock(&br->multicast_lock);
914 if (br->multicast_disabled || !netif_running(br->dev))
915 goto out;
916
Herbert Xu561f1102010-02-27 19:41:50 +0000917 __br_multicast_enable_port(port);
Herbert Xueb1d1642010-02-27 19:41:45 +0000918
919out:
920 spin_unlock(&br->multicast_lock);
921}
922
923void br_multicast_disable_port(struct net_bridge_port *port)
924{
925 struct net_bridge *br = port->br;
926 struct net_bridge_port_group *pg;
927 struct hlist_node *p, *n;
928
929 spin_lock(&br->multicast_lock);
930 hlist_for_each_entry_safe(pg, p, n, &port->mglist, mglist)
931 br_multicast_del_pg(br, pg);
932
933 if (!hlist_unhashed(&port->rlist))
934 hlist_del_init_rcu(&port->rlist);
935 del_timer(&port->multicast_router_timer);
936 del_timer(&port->multicast_query_timer);
937 spin_unlock(&br->multicast_lock);
938}
939
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900940static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
941 struct net_bridge_port *port,
942 struct sk_buff *skb)
Herbert Xueb1d1642010-02-27 19:41:45 +0000943{
944 struct igmpv3_report *ih;
945 struct igmpv3_grec *grec;
946 int i;
947 int len;
948 int num;
949 int type;
950 int err = 0;
951 __be32 group;
952
953 if (!pskb_may_pull(skb, sizeof(*ih)))
954 return -EINVAL;
955
956 ih = igmpv3_report_hdr(skb);
957 num = ntohs(ih->ngrec);
958 len = sizeof(*ih);
959
960 for (i = 0; i < num; i++) {
961 len += sizeof(*grec);
962 if (!pskb_may_pull(skb, len))
963 return -EINVAL;
964
Herbert Xufd218cf2010-04-07 21:20:47 -0700965 grec = (void *)(skb->data + len - sizeof(*grec));
Herbert Xueb1d1642010-02-27 19:41:45 +0000966 group = grec->grec_mca;
967 type = grec->grec_type;
968
Eric Dumazet8eabf952010-04-20 03:20:05 +0000969 len += ntohs(grec->grec_nsrcs) * 4;
Herbert Xueb1d1642010-02-27 19:41:45 +0000970 if (!pskb_may_pull(skb, len))
971 return -EINVAL;
972
973 /* We treat this as an IGMPv2 report for now. */
974 switch (type) {
975 case IGMPV3_MODE_IS_INCLUDE:
976 case IGMPV3_MODE_IS_EXCLUDE:
977 case IGMPV3_CHANGE_TO_INCLUDE:
978 case IGMPV3_CHANGE_TO_EXCLUDE:
979 case IGMPV3_ALLOW_NEW_SOURCES:
980 case IGMPV3_BLOCK_OLD_SOURCES:
981 break;
982
983 default:
984 continue;
985 }
986
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +0900987 err = br_ip4_multicast_add_group(br, port, group);
Herbert Xueb1d1642010-02-27 19:41:45 +0000988 if (err)
989 break;
990 }
991
992 return err;
993}
994
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000995#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +0900996static int br_ip6_multicast_mld2_report(struct net_bridge *br,
997 struct net_bridge_port *port,
998 struct sk_buff *skb)
999{
1000 struct icmp6hdr *icmp6h;
1001 struct mld2_grec *grec;
1002 int i;
1003 int len;
1004 int num;
1005 int err = 0;
1006
1007 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
1008 return -EINVAL;
1009
1010 icmp6h = icmp6_hdr(skb);
1011 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
1012 len = sizeof(*icmp6h);
1013
1014 for (i = 0; i < num; i++) {
1015 __be16 *nsrcs, _nsrcs;
1016
1017 nsrcs = skb_header_pointer(skb,
1018 len + offsetof(struct mld2_grec,
Linus Lüssing649e9842011-02-15 13:19:18 +00001019 grec_nsrcs),
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001020 sizeof(_nsrcs), &_nsrcs);
1021 if (!nsrcs)
1022 return -EINVAL;
1023
1024 if (!pskb_may_pull(skb,
1025 len + sizeof(*grec) +
Linus Lüssingd41db9f2011-02-15 13:19:19 +00001026 sizeof(struct in6_addr) * ntohs(*nsrcs)))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001027 return -EINVAL;
1028
1029 grec = (struct mld2_grec *)(skb->data + len);
Linus Lüssingd41db9f2011-02-15 13:19:19 +00001030 len += sizeof(*grec) +
1031 sizeof(struct in6_addr) * ntohs(*nsrcs);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001032
1033 /* We treat these as MLDv1 reports for now. */
1034 switch (grec->grec_type) {
1035 case MLD2_MODE_IS_INCLUDE:
1036 case MLD2_MODE_IS_EXCLUDE:
1037 case MLD2_CHANGE_TO_INCLUDE:
1038 case MLD2_CHANGE_TO_EXCLUDE:
1039 case MLD2_ALLOW_NEW_SOURCES:
1040 case MLD2_BLOCK_OLD_SOURCES:
1041 break;
1042
1043 default:
1044 continue;
1045 }
1046
1047 err = br_ip6_multicast_add_group(br, port, &grec->grec_mca);
1048 if (!err)
1049 break;
1050 }
1051
1052 return err;
1053}
1054#endif
1055
stephen hemminger7e80c122010-04-27 15:01:04 +00001056/*
1057 * Add port to rotuer_list
1058 * list is maintained ordered by pointer value
1059 * and locked by br->multicast_lock and RCU
1060 */
Herbert Xu0909e112010-02-27 19:41:49 +00001061static void br_multicast_add_router(struct net_bridge *br,
1062 struct net_bridge_port *port)
1063{
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001064 struct net_bridge_port *p;
stephen hemminger7e80c122010-04-27 15:01:04 +00001065 struct hlist_node *n, *slot = NULL;
Herbert Xu0909e112010-02-27 19:41:49 +00001066
David S. Miller709b9322010-04-27 16:49:58 -07001067 hlist_for_each_entry(p, n, &br->router_list, rlist) {
stephen hemminger7e80c122010-04-27 15:01:04 +00001068 if ((unsigned long) port >= (unsigned long) p)
1069 break;
1070 slot = n;
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001071 }
Herbert Xu0909e112010-02-27 19:41:49 +00001072
stephen hemminger7e80c122010-04-27 15:01:04 +00001073 if (slot)
1074 hlist_add_after_rcu(slot, &port->rlist);
stephen hemmingerdcdca2c2010-04-27 07:13:11 +00001075 else
1076 hlist_add_head_rcu(&port->rlist, &br->router_list);
Herbert Xu0909e112010-02-27 19:41:49 +00001077}
1078
Herbert Xueb1d1642010-02-27 19:41:45 +00001079static void br_multicast_mark_router(struct net_bridge *br,
1080 struct net_bridge_port *port)
1081{
1082 unsigned long now = jiffies;
Herbert Xueb1d1642010-02-27 19:41:45 +00001083
1084 if (!port) {
1085 if (br->multicast_router == 1)
1086 mod_timer(&br->multicast_router_timer,
1087 now + br->multicast_querier_interval);
1088 return;
1089 }
1090
1091 if (port->multicast_router != 1)
1092 return;
1093
1094 if (!hlist_unhashed(&port->rlist))
1095 goto timer;
1096
Herbert Xu0909e112010-02-27 19:41:49 +00001097 br_multicast_add_router(br, port);
Herbert Xueb1d1642010-02-27 19:41:45 +00001098
1099timer:
1100 mod_timer(&port->multicast_router_timer,
1101 now + br->multicast_querier_interval);
1102}
1103
1104static void br_multicast_query_received(struct net_bridge *br,
1105 struct net_bridge_port *port,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001106 int saddr)
Herbert Xueb1d1642010-02-27 19:41:45 +00001107{
1108 if (saddr)
1109 mod_timer(&br->multicast_querier_timer,
1110 jiffies + br->multicast_querier_interval);
1111 else if (timer_pending(&br->multicast_querier_timer))
1112 return;
1113
1114 br_multicast_mark_router(br, port);
1115}
1116
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001117static int br_ip4_multicast_query(struct net_bridge *br,
1118 struct net_bridge_port *port,
1119 struct sk_buff *skb)
Herbert Xueb1d1642010-02-27 19:41:45 +00001120{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001121 const struct iphdr *iph = ip_hdr(skb);
Herbert Xueb1d1642010-02-27 19:41:45 +00001122 struct igmphdr *ih = igmp_hdr(skb);
1123 struct net_bridge_mdb_entry *mp;
1124 struct igmpv3_query *ih3;
1125 struct net_bridge_port_group *p;
Eric Dumazete8051682010-11-15 06:38:10 +00001126 struct net_bridge_port_group __rcu **pp;
Herbert Xueb1d1642010-02-27 19:41:45 +00001127 unsigned long max_delay;
1128 unsigned long now = jiffies;
1129 __be32 group;
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001130 int err = 0;
Herbert Xueb1d1642010-02-27 19:41:45 +00001131
1132 spin_lock(&br->multicast_lock);
1133 if (!netif_running(br->dev) ||
1134 (port && port->state == BR_STATE_DISABLED))
1135 goto out;
1136
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001137 br_multicast_query_received(br, port, !!iph->saddr);
Herbert Xueb1d1642010-02-27 19:41:45 +00001138
1139 group = ih->group;
1140
1141 if (skb->len == sizeof(*ih)) {
1142 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1143
1144 if (!max_delay) {
1145 max_delay = 10 * HZ;
1146 group = 0;
1147 }
1148 } else {
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001149 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) {
1150 err = -EINVAL;
1151 goto out;
1152 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001153
1154 ih3 = igmpv3_query_hdr(skb);
1155 if (ih3->nsrcs)
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001156 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001157
YOSHIFUJI Hideaki / 吉藤英明0ba8c9e2010-03-15 19:27:00 +00001158 max_delay = ih3->code ?
1159 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
Herbert Xueb1d1642010-02-27 19:41:45 +00001160 }
1161
1162 if (!group)
1163 goto out;
1164
Eric Dumazete8051682010-11-15 06:38:10 +00001165 mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group);
Herbert Xueb1d1642010-02-27 19:41:45 +00001166 if (!mp)
1167 goto out;
1168
1169 max_delay *= br->multicast_last_member_count;
1170
Herbert Xu8a870172011-02-12 01:05:42 -08001171 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001172 (timer_pending(&mp->timer) ?
1173 time_after(mp->timer.expires, now + max_delay) :
1174 try_to_del_timer_sync(&mp->timer) >= 0))
1175 mod_timer(&mp->timer, now + max_delay);
1176
Eric Dumazete8051682010-11-15 06:38:10 +00001177 for (pp = &mp->ports;
1178 (p = mlock_dereference(*pp, br)) != NULL;
1179 pp = &p->next) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001180 if (timer_pending(&p->timer) ?
1181 time_after(p->timer.expires, now + max_delay) :
1182 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001183 mod_timer(&p->timer, now + max_delay);
Herbert Xueb1d1642010-02-27 19:41:45 +00001184 }
1185
1186out:
1187 spin_unlock(&br->multicast_lock);
YOSHIFUJI Hideakibec68ff2010-03-13 12:27:21 -08001188 return err;
Herbert Xueb1d1642010-02-27 19:41:45 +00001189}
1190
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001191#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001192static int br_ip6_multicast_query(struct net_bridge *br,
1193 struct net_bridge_port *port,
1194 struct sk_buff *skb)
1195{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001196 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001197 struct mld_msg *mld = (struct mld_msg *) icmp6_hdr(skb);
1198 struct net_bridge_mdb_entry *mp;
1199 struct mld2_query *mld2q;
Eric Dumazete8051682010-11-15 06:38:10 +00001200 struct net_bridge_port_group *p;
1201 struct net_bridge_port_group __rcu **pp;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001202 unsigned long max_delay;
1203 unsigned long now = jiffies;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001204 const struct in6_addr *group = NULL;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001205 int err = 0;
1206
1207 spin_lock(&br->multicast_lock);
1208 if (!netif_running(br->dev) ||
1209 (port && port->state == BR_STATE_DISABLED))
1210 goto out;
1211
1212 br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->saddr));
1213
1214 if (skb->len == sizeof(*mld)) {
1215 if (!pskb_may_pull(skb, sizeof(*mld))) {
1216 err = -EINVAL;
1217 goto out;
1218 }
1219 mld = (struct mld_msg *) icmp6_hdr(skb);
1220 max_delay = msecs_to_jiffies(htons(mld->mld_maxdelay));
1221 if (max_delay)
1222 group = &mld->mld_mca;
1223 } else if (skb->len >= sizeof(*mld2q)) {
1224 if (!pskb_may_pull(skb, sizeof(*mld2q))) {
1225 err = -EINVAL;
1226 goto out;
1227 }
1228 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1229 if (!mld2q->mld2q_nsrcs)
1230 group = &mld2q->mld2q_mca;
1231 max_delay = mld2q->mld2q_mrc ? MLDV2_MRC(mld2q->mld2q_mrc) : 1;
1232 }
1233
1234 if (!group)
1235 goto out;
1236
Eric Dumazete8051682010-11-15 06:38:10 +00001237 mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001238 if (!mp)
1239 goto out;
1240
1241 max_delay *= br->multicast_last_member_count;
Herbert Xu8a870172011-02-12 01:05:42 -08001242 if (mp->mglist &&
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001243 (timer_pending(&mp->timer) ?
1244 time_after(mp->timer.expires, now + max_delay) :
1245 try_to_del_timer_sync(&mp->timer) >= 0))
1246 mod_timer(&mp->timer, now + max_delay);
1247
Eric Dumazete8051682010-11-15 06:38:10 +00001248 for (pp = &mp->ports;
1249 (p = mlock_dereference(*pp, br)) != NULL;
1250 pp = &p->next) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001251 if (timer_pending(&p->timer) ?
1252 time_after(p->timer.expires, now + max_delay) :
1253 try_to_del_timer_sync(&p->timer) >= 0)
Herbert Xu24f9cdc2011-02-11 12:42:07 +00001254 mod_timer(&p->timer, now + max_delay);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001255 }
1256
1257out:
1258 spin_unlock(&br->multicast_lock);
1259 return err;
1260}
1261#endif
1262
Herbert Xueb1d1642010-02-27 19:41:45 +00001263static void br_multicast_leave_group(struct net_bridge *br,
1264 struct net_bridge_port *port,
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001265 struct br_ip *group)
Herbert Xueb1d1642010-02-27 19:41:45 +00001266{
1267 struct net_bridge_mdb_htable *mdb;
1268 struct net_bridge_mdb_entry *mp;
1269 struct net_bridge_port_group *p;
1270 unsigned long now;
1271 unsigned long time;
1272
Herbert Xueb1d1642010-02-27 19:41:45 +00001273 spin_lock(&br->multicast_lock);
1274 if (!netif_running(br->dev) ||
1275 (port && port->state == BR_STATE_DISABLED) ||
1276 timer_pending(&br->multicast_querier_timer))
1277 goto out;
1278
Eric Dumazete8051682010-11-15 06:38:10 +00001279 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001280 mp = br_mdb_ip_get(mdb, group);
1281 if (!mp)
1282 goto out;
1283
1284 now = jiffies;
1285 time = now + br->multicast_last_member_count *
1286 br->multicast_last_member_interval;
1287
1288 if (!port) {
Herbert Xu8a870172011-02-12 01:05:42 -08001289 if (mp->mglist &&
Herbert Xueb1d1642010-02-27 19:41:45 +00001290 (timer_pending(&mp->timer) ?
1291 time_after(mp->timer.expires, time) :
1292 try_to_del_timer_sync(&mp->timer) >= 0)) {
1293 mod_timer(&mp->timer, time);
1294
1295 mp->queries_sent = 0;
1296 mod_timer(&mp->query_timer, now);
1297 }
1298
1299 goto out;
1300 }
1301
Eric Dumazete8051682010-11-15 06:38:10 +00001302 for (p = mlock_dereference(mp->ports, br);
1303 p != NULL;
1304 p = mlock_dereference(p->next, br)) {
Herbert Xueb1d1642010-02-27 19:41:45 +00001305 if (p->port != port)
1306 continue;
1307
1308 if (!hlist_unhashed(&p->mglist) &&
1309 (timer_pending(&p->timer) ?
1310 time_after(p->timer.expires, time) :
1311 try_to_del_timer_sync(&p->timer) >= 0)) {
1312 mod_timer(&p->timer, time);
1313
1314 p->queries_sent = 0;
1315 mod_timer(&p->query_timer, now);
1316 }
1317
1318 break;
1319 }
1320
1321out:
1322 spin_unlock(&br->multicast_lock);
1323}
1324
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001325static void br_ip4_multicast_leave_group(struct net_bridge *br,
1326 struct net_bridge_port *port,
1327 __be32 group)
1328{
1329 struct br_ip br_group;
1330
1331 if (ipv4_is_local_multicast(group))
1332 return;
1333
1334 br_group.u.ip4 = group;
1335 br_group.proto = htons(ETH_P_IP);
1336
1337 br_multicast_leave_group(br, port, &br_group);
1338}
1339
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001340#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001341static void br_ip6_multicast_leave_group(struct net_bridge *br,
1342 struct net_bridge_port *port,
1343 const struct in6_addr *group)
1344{
1345 struct br_ip br_group;
1346
Linus Lüssinge4de9f92011-02-15 13:19:21 +00001347 if (!ipv6_is_transient_multicast(group))
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001348 return;
1349
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001350 br_group.u.ip6 = *group;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001351 br_group.proto = htons(ETH_P_IPV6);
1352
1353 br_multicast_leave_group(br, port, &br_group);
1354}
1355#endif
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001356
Herbert Xueb1d1642010-02-27 19:41:45 +00001357static int br_multicast_ipv4_rcv(struct net_bridge *br,
1358 struct net_bridge_port *port,
1359 struct sk_buff *skb)
1360{
1361 struct sk_buff *skb2 = skb;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001362 const struct iphdr *iph;
Herbert Xueb1d1642010-02-27 19:41:45 +00001363 struct igmphdr *ih;
1364 unsigned len;
1365 unsigned offset;
1366 int err;
1367
Herbert Xueb1d1642010-02-27 19:41:45 +00001368 /* We treat OOM as packet loss for now. */
1369 if (!pskb_may_pull(skb, sizeof(*iph)))
1370 return -EINVAL;
1371
1372 iph = ip_hdr(skb);
1373
1374 if (iph->ihl < 5 || iph->version != 4)
1375 return -EINVAL;
1376
1377 if (!pskb_may_pull(skb, ip_hdrlen(skb)))
1378 return -EINVAL;
1379
1380 iph = ip_hdr(skb);
1381
1382 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1383 return -EINVAL;
1384
Herbert Xubd4265f2011-06-23 02:39:12 +00001385 if (iph->protocol != IPPROTO_IGMP) {
1386 if ((iph->daddr & IGMP_LOCAL_GROUP_MASK) != IGMP_LOCAL_GROUP)
1387 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
Herbert Xueb1d1642010-02-27 19:41:45 +00001388 return 0;
Herbert Xubd4265f2011-06-23 02:39:12 +00001389 }
Herbert Xueb1d1642010-02-27 19:41:45 +00001390
1391 len = ntohs(iph->tot_len);
1392 if (skb->len < len || len < ip_hdrlen(skb))
1393 return -EINVAL;
1394
1395 if (skb->len > len) {
1396 skb2 = skb_clone(skb, GFP_ATOMIC);
1397 if (!skb2)
1398 return -ENOMEM;
1399
1400 err = pskb_trim_rcsum(skb2, len);
1401 if (err)
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001402 goto err_out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001403 }
1404
1405 len -= ip_hdrlen(skb2);
1406 offset = skb_network_offset(skb2) + ip_hdrlen(skb2);
1407 __skb_pull(skb2, offset);
1408 skb_reset_transport_header(skb2);
1409
1410 err = -EINVAL;
1411 if (!pskb_may_pull(skb2, sizeof(*ih)))
1412 goto out;
1413
Herbert Xueb1d1642010-02-27 19:41:45 +00001414 switch (skb2->ip_summed) {
1415 case CHECKSUM_COMPLETE:
1416 if (!csum_fold(skb2->csum))
1417 break;
1418 /* fall through */
1419 case CHECKSUM_NONE:
1420 skb2->csum = 0;
1421 if (skb_checksum_complete(skb2))
YOSHIFUJI Hideaki / 吉藤英明84408532010-03-15 19:26:56 +00001422 goto out;
Herbert Xueb1d1642010-02-27 19:41:45 +00001423 }
1424
1425 err = 0;
1426
1427 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;
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001434 err = br_ip4_multicast_add_group(br, port, ih->group);
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:
YOSHIFUJI Hideaki8ef2a9a2010-04-18 12:42:07 +09001443 br_ip4_multicast_leave_group(br, port, ih->group);
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;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001465 unsigned len;
Kulikov Vasiliybb7a0bd2010-07-15 08:47:33 +00001466 int offset;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001467 int err;
1468
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
1551 BR_INPUT_SKB_CB(skb)->igmp = 1;
1552
Eric Dumazet22df1332011-08-23 19:57:05 +00001553 switch (icmp6_type) {
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001554 case ICMPV6_MGM_REPORT:
1555 {
Tomas Winkler9d890812011-01-03 11:26:08 -08001556 struct mld_msg *mld;
1557 if (!pskb_may_pull(skb2, sizeof(*mld))) {
1558 err = -EINVAL;
1559 goto out;
1560 }
1561 mld = (struct mld_msg *)skb_transport_header(skb2);
Fernando Luis Vázquez Caofc2af6c2011-06-13 15:06:58 +00001562 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001563 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca);
1564 break;
1565 }
1566 case ICMPV6_MLD2_REPORT:
1567 err = br_ip6_multicast_mld2_report(br, port, skb2);
1568 break;
1569 case ICMPV6_MGM_QUERY:
1570 err = br_ip6_multicast_query(br, port, skb2);
1571 break;
1572 case ICMPV6_MGM_REDUCTION:
1573 {
Tomas Winkler9d890812011-01-03 11:26:08 -08001574 struct mld_msg *mld;
1575 if (!pskb_may_pull(skb2, sizeof(*mld))) {
1576 err = -EINVAL;
1577 goto out;
1578 }
1579 mld = (struct mld_msg *)skb_transport_header(skb2);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001580 br_ip6_multicast_leave_group(br, port, &mld->mld_mca);
1581 }
1582 }
1583
1584out:
Tomas Winkler9d890812011-01-03 11:26:08 -08001585 kfree_skb(skb2);
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001586 return err;
1587}
1588#endif
1589
Herbert Xueb1d1642010-02-27 19:41:45 +00001590int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1591 struct sk_buff *skb)
1592{
YOSHIFUJI Hideaki / 吉藤英明1fafc7a2010-04-25 08:06:40 +00001593 BR_INPUT_SKB_CB(skb)->igmp = 0;
1594 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1595
Herbert Xueb1d1642010-02-27 19:41:45 +00001596 if (br->multicast_disabled)
1597 return 0;
1598
1599 switch (skb->protocol) {
1600 case htons(ETH_P_IP):
1601 return br_multicast_ipv4_rcv(br, port, skb);
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001602#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki08b202b2010-04-23 01:54:22 +09001603 case htons(ETH_P_IPV6):
1604 return br_multicast_ipv6_rcv(br, port, skb);
1605#endif
Herbert Xueb1d1642010-02-27 19:41:45 +00001606 }
1607
1608 return 0;
1609}
1610
1611static void br_multicast_query_expired(unsigned long data)
1612{
1613 struct net_bridge *br = (void *)data;
1614
1615 spin_lock(&br->multicast_lock);
1616 if (br->multicast_startup_queries_sent <
1617 br->multicast_startup_query_count)
1618 br->multicast_startup_queries_sent++;
1619
1620 br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent);
1621
1622 spin_unlock(&br->multicast_lock);
1623}
1624
1625void br_multicast_init(struct net_bridge *br)
1626{
1627 br->hash_elasticity = 4;
1628 br->hash_max = 512;
1629
1630 br->multicast_router = 1;
1631 br->multicast_last_member_count = 2;
1632 br->multicast_startup_query_count = 2;
1633
1634 br->multicast_last_member_interval = HZ;
1635 br->multicast_query_response_interval = 10 * HZ;
1636 br->multicast_startup_query_interval = 125 * HZ / 4;
1637 br->multicast_query_interval = 125 * HZ;
1638 br->multicast_querier_interval = 255 * HZ;
1639 br->multicast_membership_interval = 260 * HZ;
1640
1641 spin_lock_init(&br->multicast_lock);
1642 setup_timer(&br->multicast_router_timer,
1643 br_multicast_local_router_expired, 0);
1644 setup_timer(&br->multicast_querier_timer,
1645 br_multicast_local_router_expired, 0);
1646 setup_timer(&br->multicast_query_timer, br_multicast_query_expired,
1647 (unsigned long)br);
1648}
1649
1650void br_multicast_open(struct net_bridge *br)
1651{
1652 br->multicast_startup_queries_sent = 0;
1653
1654 if (br->multicast_disabled)
1655 return;
1656
1657 mod_timer(&br->multicast_query_timer, jiffies);
1658}
1659
1660void br_multicast_stop(struct net_bridge *br)
1661{
1662 struct net_bridge_mdb_htable *mdb;
1663 struct net_bridge_mdb_entry *mp;
1664 struct hlist_node *p, *n;
1665 u32 ver;
1666 int i;
1667
1668 del_timer_sync(&br->multicast_router_timer);
1669 del_timer_sync(&br->multicast_querier_timer);
1670 del_timer_sync(&br->multicast_query_timer);
1671
1672 spin_lock_bh(&br->multicast_lock);
Eric Dumazete8051682010-11-15 06:38:10 +00001673 mdb = mlock_dereference(br->mdb, br);
Herbert Xueb1d1642010-02-27 19:41:45 +00001674 if (!mdb)
1675 goto out;
1676
1677 br->mdb = NULL;
1678
1679 ver = mdb->ver;
1680 for (i = 0; i < mdb->max; i++) {
1681 hlist_for_each_entry_safe(mp, p, n, &mdb->mhash[i],
1682 hlist[ver]) {
1683 del_timer(&mp->timer);
1684 del_timer(&mp->query_timer);
1685 call_rcu_bh(&mp->rcu, br_multicast_free_group);
1686 }
1687 }
1688
1689 if (mdb->old) {
1690 spin_unlock_bh(&br->multicast_lock);
Herbert Xu10cc2b52010-03-05 21:03:35 +00001691 rcu_barrier_bh();
Herbert Xueb1d1642010-02-27 19:41:45 +00001692 spin_lock_bh(&br->multicast_lock);
1693 WARN_ON(mdb->old);
1694 }
1695
1696 mdb->old = mdb;
1697 call_rcu_bh(&mdb->rcu, br_mdb_free);
1698
1699out:
1700 spin_unlock_bh(&br->multicast_lock);
1701}
Herbert Xu0909e112010-02-27 19:41:49 +00001702
1703int br_multicast_set_router(struct net_bridge *br, unsigned long val)
1704{
1705 int err = -ENOENT;
1706
1707 spin_lock_bh(&br->multicast_lock);
1708 if (!netif_running(br->dev))
1709 goto unlock;
1710
1711 switch (val) {
1712 case 0:
1713 case 2:
1714 del_timer(&br->multicast_router_timer);
1715 /* fall through */
1716 case 1:
1717 br->multicast_router = val;
1718 err = 0;
1719 break;
1720
1721 default:
1722 err = -EINVAL;
1723 break;
1724 }
1725
1726unlock:
1727 spin_unlock_bh(&br->multicast_lock);
1728
1729 return err;
1730}
1731
1732int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
1733{
1734 struct net_bridge *br = p->br;
1735 int err = -ENOENT;
1736
1737 spin_lock(&br->multicast_lock);
1738 if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED)
1739 goto unlock;
1740
1741 switch (val) {
1742 case 0:
1743 case 1:
1744 case 2:
1745 p->multicast_router = val;
1746 err = 0;
1747
1748 if (val < 2 && !hlist_unhashed(&p->rlist))
1749 hlist_del_init_rcu(&p->rlist);
1750
1751 if (val == 1)
1752 break;
1753
1754 del_timer(&p->multicast_router_timer);
1755
1756 if (val == 0)
1757 break;
1758
1759 br_multicast_add_router(br, p);
1760 break;
1761
1762 default:
1763 err = -EINVAL;
1764 break;
1765 }
1766
1767unlock:
1768 spin_unlock(&br->multicast_lock);
1769
1770 return err;
1771}
Herbert Xu561f1102010-02-27 19:41:50 +00001772
1773int br_multicast_toggle(struct net_bridge *br, unsigned long val)
1774{
1775 struct net_bridge_port *port;
Herbert Xu3a7fda02010-07-29 00:45:30 +00001776 int err = 0;
Eric Dumazete8051682010-11-15 06:38:10 +00001777 struct net_bridge_mdb_htable *mdb;
Herbert Xu561f1102010-02-27 19:41:50 +00001778
Andrey Vaginef5e0d82011-11-10 05:48:03 +00001779 spin_lock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00001780 if (br->multicast_disabled == !val)
1781 goto unlock;
1782
1783 br->multicast_disabled = !val;
1784 if (br->multicast_disabled)
1785 goto unlock;
1786
Herbert Xu3a7fda02010-07-29 00:45:30 +00001787 if (!netif_running(br->dev))
1788 goto unlock;
1789
Eric Dumazete8051682010-11-15 06:38:10 +00001790 mdb = mlock_dereference(br->mdb, br);
1791 if (mdb) {
1792 if (mdb->old) {
Herbert Xu561f1102010-02-27 19:41:50 +00001793 err = -EEXIST;
1794rollback:
1795 br->multicast_disabled = !!val;
1796 goto unlock;
1797 }
1798
Eric Dumazete8051682010-11-15 06:38:10 +00001799 err = br_mdb_rehash(&br->mdb, mdb->max,
Herbert Xu561f1102010-02-27 19:41:50 +00001800 br->hash_elasticity);
1801 if (err)
1802 goto rollback;
1803 }
1804
1805 br_multicast_open(br);
1806 list_for_each_entry(port, &br->port_list, list) {
1807 if (port->state == BR_STATE_DISABLED ||
1808 port->state == BR_STATE_BLOCKING)
1809 continue;
1810
1811 __br_multicast_enable_port(port);
1812 }
1813
1814unlock:
Andrey Vaginef5e0d82011-11-10 05:48:03 +00001815 spin_unlock_bh(&br->multicast_lock);
Herbert Xu561f1102010-02-27 19:41:50 +00001816
1817 return err;
1818}
Herbert Xub1951672010-02-27 19:41:51 +00001819
1820int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
1821{
1822 int err = -ENOENT;
1823 u32 old;
Eric Dumazete8051682010-11-15 06:38:10 +00001824 struct net_bridge_mdb_htable *mdb;
Herbert Xub1951672010-02-27 19:41:51 +00001825
1826 spin_lock(&br->multicast_lock);
1827 if (!netif_running(br->dev))
1828 goto unlock;
1829
1830 err = -EINVAL;
1831 if (!is_power_of_2(val))
1832 goto unlock;
Eric Dumazete8051682010-11-15 06:38:10 +00001833
1834 mdb = mlock_dereference(br->mdb, br);
1835 if (mdb && val < mdb->size)
Herbert Xub1951672010-02-27 19:41:51 +00001836 goto unlock;
1837
1838 err = 0;
1839
1840 old = br->hash_max;
1841 br->hash_max = val;
1842
Eric Dumazete8051682010-11-15 06:38:10 +00001843 if (mdb) {
1844 if (mdb->old) {
Herbert Xub1951672010-02-27 19:41:51 +00001845 err = -EEXIST;
1846rollback:
1847 br->hash_max = old;
1848 goto unlock;
1849 }
1850
1851 err = br_mdb_rehash(&br->mdb, br->hash_max,
1852 br->hash_elasticity);
1853 if (err)
1854 goto rollback;
1855 }
1856
1857unlock:
1858 spin_unlock(&br->multicast_lock);
1859
1860 return err;
1861}