1 /*
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/export.h>
15 #include <linux/if_ether.h>
16 #include <linux/igmp.h>
17 #include <linux/jhash.h>
18 #include <linux/kernel.h>
19 #include <linux/log2.h>
20 #include <linux/netdevice.h>
21 #include <linux/netfilter_bridge.h>
22 #include <linux/random.h>
23 #include <linux/rculist.h>
24 #include <linux/skbuff.h>
25 #include <linux/slab.h>
26 #include <linux/timer.h>
27 #include <linux/inetdevice.h>
28 #include <linux/mroute.h>
29 #include <net/ip.h>
30 #include <net/switchdev.h>
31 #if IS_ENABLED(CONFIG_IPV6)
32 #include <net/ipv6.h>
33 #include <net/mld.h>
34 #include <net/ip6_checksum.h>
35 #include <net/addrconf.h>
36 #endif
37
38 #include "br_private.h"
39
40 static void br_multicast_start_querier(struct net_bridge *br,
41 struct bridge_mcast_own_query *query);
42 static void br_multicast_add_router(struct net_bridge *br,
43 struct net_bridge_port *port);
44 static void br_ip4_multicast_leave_group(struct net_bridge *br,
45 struct net_bridge_port *port,
46 __be32 group,
47 __u16 vid,
48 const unsigned char *src);
49
50 static void __del_port_router(struct net_bridge_port *p);
51 #if IS_ENABLED(CONFIG_IPV6)
52 static void br_ip6_multicast_leave_group(struct net_bridge *br,
53 struct net_bridge_port *port,
54 const struct in6_addr *group,
55 __u16 vid, const unsigned char *src);
56 #endif
57 unsigned int br_mdb_rehash_seq;
58
br_ip_equal(const struct br_ip * a,const struct br_ip * b)59 static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b)
60 {
61 if (a->proto != b->proto)
62 return 0;
63 if (a->vid != b->vid)
64 return 0;
65 switch (a->proto) {
66 case htons(ETH_P_IP):
67 return a->u.ip4 == b->u.ip4;
68 #if IS_ENABLED(CONFIG_IPV6)
69 case htons(ETH_P_IPV6):
70 return ipv6_addr_equal(&a->u.ip6, &b->u.ip6);
71 #endif
72 }
73 return 0;
74 }
75
__br_ip4_hash(struct net_bridge_mdb_htable * mdb,__be32 ip,__u16 vid)76 static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip,
77 __u16 vid)
78 {
79 return jhash_2words((__force u32)ip, vid, mdb->secret) & (mdb->max - 1);
80 }
81
82 #if IS_ENABLED(CONFIG_IPV6)
__br_ip6_hash(struct net_bridge_mdb_htable * mdb,const struct in6_addr * ip,__u16 vid)83 static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb,
84 const struct in6_addr *ip,
85 __u16 vid)
86 {
87 return jhash_2words(ipv6_addr_hash(ip), vid,
88 mdb->secret) & (mdb->max - 1);
89 }
90 #endif
91
br_ip_hash(struct net_bridge_mdb_htable * mdb,struct br_ip * ip)92 static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb,
93 struct br_ip *ip)
94 {
95 switch (ip->proto) {
96 case htons(ETH_P_IP):
97 return __br_ip4_hash(mdb, ip->u.ip4, ip->vid);
98 #if IS_ENABLED(CONFIG_IPV6)
99 case htons(ETH_P_IPV6):
100 return __br_ip6_hash(mdb, &ip->u.ip6, ip->vid);
101 #endif
102 }
103 return 0;
104 }
105
__br_mdb_ip_get(struct net_bridge_mdb_htable * mdb,struct br_ip * dst,int hash)106 static struct net_bridge_mdb_entry *__br_mdb_ip_get(
107 struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash)
108 {
109 struct net_bridge_mdb_entry *mp;
110
111 hlist_for_each_entry_rcu(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
112 if (br_ip_equal(&mp->addr, dst))
113 return mp;
114 }
115
116 return NULL;
117 }
118
br_mdb_ip_get(struct net_bridge_mdb_htable * mdb,struct br_ip * dst)119 struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge_mdb_htable *mdb,
120 struct br_ip *dst)
121 {
122 if (!mdb)
123 return NULL;
124
125 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst));
126 }
127
br_mdb_ip4_get(struct net_bridge_mdb_htable * mdb,__be32 dst,__u16 vid)128 static struct net_bridge_mdb_entry *br_mdb_ip4_get(
129 struct net_bridge_mdb_htable *mdb, __be32 dst, __u16 vid)
130 {
131 struct br_ip br_dst;
132
133 br_dst.u.ip4 = dst;
134 br_dst.proto = htons(ETH_P_IP);
135 br_dst.vid = vid;
136
137 return br_mdb_ip_get(mdb, &br_dst);
138 }
139
140 #if IS_ENABLED(CONFIG_IPV6)
br_mdb_ip6_get(struct net_bridge_mdb_htable * mdb,const struct in6_addr * dst,__u16 vid)141 static struct net_bridge_mdb_entry *br_mdb_ip6_get(
142 struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst,
143 __u16 vid)
144 {
145 struct br_ip br_dst;
146
147 br_dst.u.ip6 = *dst;
148 br_dst.proto = htons(ETH_P_IPV6);
149 br_dst.vid = vid;
150
151 return br_mdb_ip_get(mdb, &br_dst);
152 }
153 #endif
154
br_mdb_get(struct net_bridge * br,struct sk_buff * skb,u16 vid)155 struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
156 struct sk_buff *skb, u16 vid)
157 {
158 struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb);
159 struct br_ip ip;
160
161 if (br->multicast_disabled)
162 return NULL;
163
164 if (BR_INPUT_SKB_CB(skb)->igmp)
165 return NULL;
166
167 ip.proto = skb->protocol;
168 ip.vid = vid;
169
170 switch (skb->protocol) {
171 case htons(ETH_P_IP):
172 ip.u.ip4 = ip_hdr(skb)->daddr;
173 break;
174 #if IS_ENABLED(CONFIG_IPV6)
175 case htons(ETH_P_IPV6):
176 ip.u.ip6 = ipv6_hdr(skb)->daddr;
177 break;
178 #endif
179 default:
180 return NULL;
181 }
182
183 return br_mdb_ip_get(mdb, &ip);
184 }
185
br_mdb_free(struct rcu_head * head)186 static void br_mdb_free(struct rcu_head *head)
187 {
188 struct net_bridge_mdb_htable *mdb =
189 container_of(head, struct net_bridge_mdb_htable, rcu);
190 struct net_bridge_mdb_htable *old = mdb->old;
191
192 mdb->old = NULL;
193 kfree(old->mhash);
194 kfree(old);
195 }
196
br_mdb_copy(struct net_bridge_mdb_htable * new,struct net_bridge_mdb_htable * old,int elasticity)197 static int br_mdb_copy(struct net_bridge_mdb_htable *new,
198 struct net_bridge_mdb_htable *old,
199 int elasticity)
200 {
201 struct net_bridge_mdb_entry *mp;
202 int maxlen;
203 int len;
204 int i;
205
206 for (i = 0; i < old->max; i++)
207 hlist_for_each_entry(mp, &old->mhash[i], hlist[old->ver])
208 hlist_add_head(&mp->hlist[new->ver],
209 &new->mhash[br_ip_hash(new, &mp->addr)]);
210
211 if (!elasticity)
212 return 0;
213
214 maxlen = 0;
215 for (i = 0; i < new->max; i++) {
216 len = 0;
217 hlist_for_each_entry(mp, &new->mhash[i], hlist[new->ver])
218 len++;
219 if (len > maxlen)
220 maxlen = len;
221 }
222
223 return maxlen > elasticity ? -EINVAL : 0;
224 }
225
br_multicast_free_pg(struct rcu_head * head)226 void br_multicast_free_pg(struct rcu_head *head)
227 {
228 struct net_bridge_port_group *p =
229 container_of(head, struct net_bridge_port_group, rcu);
230
231 kfree(p);
232 }
233
br_multicast_free_group(struct rcu_head * head)234 static void br_multicast_free_group(struct rcu_head *head)
235 {
236 struct net_bridge_mdb_entry *mp =
237 container_of(head, struct net_bridge_mdb_entry, rcu);
238
239 kfree(mp);
240 }
241
br_multicast_group_expired(struct timer_list * t)242 static void br_multicast_group_expired(struct timer_list *t)
243 {
244 struct net_bridge_mdb_entry *mp = from_timer(mp, t, timer);
245 struct net_bridge *br = mp->br;
246 struct net_bridge_mdb_htable *mdb;
247
248 spin_lock(&br->multicast_lock);
249 if (!netif_running(br->dev) || timer_pending(&mp->timer))
250 goto out;
251
252 mp->host_joined = false;
253 br_mdb_notify(br->dev, NULL, &mp->addr, RTM_DELMDB, 0);
254
255 if (mp->ports)
256 goto out;
257
258 mdb = mlock_dereference(br->mdb, br);
259
260 hlist_del_rcu(&mp->hlist[mdb->ver]);
261 mdb->size--;
262
263 call_rcu_bh(&mp->rcu, br_multicast_free_group);
264
265 out:
266 spin_unlock(&br->multicast_lock);
267 }
268
br_multicast_del_pg(struct net_bridge * br,struct net_bridge_port_group * pg)269 static void br_multicast_del_pg(struct net_bridge *br,
270 struct net_bridge_port_group *pg)
271 {
272 struct net_bridge_mdb_htable *mdb;
273 struct net_bridge_mdb_entry *mp;
274 struct net_bridge_port_group *p;
275 struct net_bridge_port_group __rcu **pp;
276
277 mdb = mlock_dereference(br->mdb, br);
278
279 mp = br_mdb_ip_get(mdb, &pg->addr);
280 if (WARN_ON(!mp))
281 return;
282
283 for (pp = &mp->ports;
284 (p = mlock_dereference(*pp, br)) != NULL;
285 pp = &p->next) {
286 if (p != pg)
287 continue;
288
289 rcu_assign_pointer(*pp, p->next);
290 hlist_del_init(&p->mglist);
291 del_timer(&p->timer);
292 br_mdb_notify(br->dev, p->port, &pg->addr, RTM_DELMDB,
293 p->flags);
294 call_rcu_bh(&p->rcu, br_multicast_free_pg);
295
296 if (!mp->ports && !mp->host_joined &&
297 netif_running(br->dev))
298 mod_timer(&mp->timer, jiffies);
299
300 return;
301 }
302
303 WARN_ON(1);
304 }
305
br_multicast_port_group_expired(struct timer_list * t)306 static void br_multicast_port_group_expired(struct timer_list *t)
307 {
308 struct net_bridge_port_group *pg = from_timer(pg, t, timer);
309 struct net_bridge *br = pg->port->br;
310
311 spin_lock(&br->multicast_lock);
312 if (!netif_running(br->dev) || timer_pending(&pg->timer) ||
313 hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT)
314 goto out;
315
316 br_multicast_del_pg(br, pg);
317
318 out:
319 spin_unlock(&br->multicast_lock);
320 }
321
br_mdb_rehash(struct net_bridge_mdb_htable __rcu ** mdbp,int max,int elasticity)322 static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max,
323 int elasticity)
324 {
325 struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1);
326 struct net_bridge_mdb_htable *mdb;
327 int err;
328
329 mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC);
330 if (!mdb)
331 return -ENOMEM;
332
333 mdb->max = max;
334 mdb->old = old;
335
336 mdb->mhash = kcalloc(max, sizeof(*mdb->mhash), GFP_ATOMIC);
337 if (!mdb->mhash) {
338 kfree(mdb);
339 return -ENOMEM;
340 }
341
342 mdb->size = old ? old->size : 0;
343 mdb->ver = old ? old->ver ^ 1 : 0;
344
345 if (!old || elasticity)
346 get_random_bytes(&mdb->secret, sizeof(mdb->secret));
347 else
348 mdb->secret = old->secret;
349
350 if (!old)
351 goto out;
352
353 err = br_mdb_copy(mdb, old, elasticity);
354 if (err) {
355 kfree(mdb->mhash);
356 kfree(mdb);
357 return err;
358 }
359
360 br_mdb_rehash_seq++;
361 call_rcu_bh(&mdb->rcu, br_mdb_free);
362
363 out:
364 rcu_assign_pointer(*mdbp, mdb);
365
366 return 0;
367 }
368
br_ip4_multicast_alloc_query(struct net_bridge * br,__be32 group,u8 * igmp_type)369 static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
370 __be32 group,
371 u8 *igmp_type)
372 {
373 struct igmpv3_query *ihv3;
374 size_t igmp_hdr_size;
375 struct sk_buff *skb;
376 struct igmphdr *ih;
377 struct ethhdr *eth;
378 struct iphdr *iph;
379
380 igmp_hdr_size = sizeof(*ih);
381 if (br->multicast_igmp_version == 3)
382 igmp_hdr_size = sizeof(*ihv3);
383 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) +
384 igmp_hdr_size + 4);
385 if (!skb)
386 goto out;
387
388 skb->protocol = htons(ETH_P_IP);
389
390 skb_reset_mac_header(skb);
391 eth = eth_hdr(skb);
392
393 ether_addr_copy(eth->h_source, br->dev->dev_addr);
394 eth->h_dest[0] = 1;
395 eth->h_dest[1] = 0;
396 eth->h_dest[2] = 0x5e;
397 eth->h_dest[3] = 0;
398 eth->h_dest[4] = 0;
399 eth->h_dest[5] = 1;
400 eth->h_proto = htons(ETH_P_IP);
401 skb_put(skb, sizeof(*eth));
402
403 skb_set_network_header(skb, skb->len);
404 iph = ip_hdr(skb);
405
406 iph->version = 4;
407 iph->ihl = 6;
408 iph->tos = 0xc0;
409 iph->tot_len = htons(sizeof(*iph) + igmp_hdr_size + 4);
410 iph->id = 0;
411 iph->frag_off = htons(IP_DF);
412 iph->ttl = 1;
413 iph->protocol = IPPROTO_IGMP;
414 iph->saddr = br->multicast_query_use_ifaddr ?
415 inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0;
416 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
417 ((u8 *)&iph[1])[0] = IPOPT_RA;
418 ((u8 *)&iph[1])[1] = 4;
419 ((u8 *)&iph[1])[2] = 0;
420 ((u8 *)&iph[1])[3] = 0;
421 ip_send_check(iph);
422 skb_put(skb, 24);
423
424 skb_set_transport_header(skb, skb->len);
425 *igmp_type = IGMP_HOST_MEMBERSHIP_QUERY;
426
427 switch (br->multicast_igmp_version) {
428 case 2:
429 ih = igmp_hdr(skb);
430 ih->type = IGMP_HOST_MEMBERSHIP_QUERY;
431 ih->code = (group ? br->multicast_last_member_interval :
432 br->multicast_query_response_interval) /
433 (HZ / IGMP_TIMER_SCALE);
434 ih->group = group;
435 ih->csum = 0;
436 ih->csum = ip_compute_csum((void *)ih, sizeof(*ih));
437 break;
438 case 3:
439 ihv3 = igmpv3_query_hdr(skb);
440 ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY;
441 ihv3->code = (group ? br->multicast_last_member_interval :
442 br->multicast_query_response_interval) /
443 (HZ / IGMP_TIMER_SCALE);
444 ihv3->group = group;
445 ihv3->qqic = br->multicast_query_interval / HZ;
446 ihv3->nsrcs = 0;
447 ihv3->resv = 0;
448 ihv3->suppress = 0;
449 ihv3->qrv = 2;
450 ihv3->csum = 0;
451 ihv3->csum = ip_compute_csum((void *)ihv3, sizeof(*ihv3));
452 break;
453 }
454
455 skb_put(skb, igmp_hdr_size);
456 __skb_pull(skb, sizeof(*eth));
457
458 out:
459 return skb;
460 }
461
462 #if IS_ENABLED(CONFIG_IPV6)
br_ip6_multicast_alloc_query(struct net_bridge * br,const struct in6_addr * grp,u8 * igmp_type)463 static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
464 const struct in6_addr *grp,
465 u8 *igmp_type)
466 {
467 struct mld2_query *mld2q;
468 unsigned long interval;
469 struct ipv6hdr *ip6h;
470 struct mld_msg *mldq;
471 size_t mld_hdr_size;
472 struct sk_buff *skb;
473 struct ethhdr *eth;
474 u8 *hopopt;
475
476 mld_hdr_size = sizeof(*mldq);
477 if (br->multicast_mld_version == 2)
478 mld_hdr_size = sizeof(*mld2q);
479 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) +
480 8 + mld_hdr_size);
481 if (!skb)
482 goto out;
483
484 skb->protocol = htons(ETH_P_IPV6);
485
486 /* Ethernet header */
487 skb_reset_mac_header(skb);
488 eth = eth_hdr(skb);
489
490 ether_addr_copy(eth->h_source, br->dev->dev_addr);
491 eth->h_proto = htons(ETH_P_IPV6);
492 skb_put(skb, sizeof(*eth));
493
494 /* IPv6 header + HbH option */
495 skb_set_network_header(skb, skb->len);
496 ip6h = ipv6_hdr(skb);
497
498 *(__force __be32 *)ip6h = htonl(0x60000000);
499 ip6h->payload_len = htons(8 + mld_hdr_size);
500 ip6h->nexthdr = IPPROTO_HOPOPTS;
501 ip6h->hop_limit = 1;
502 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1));
503 if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0,
504 &ip6h->saddr)) {
505 kfree_skb(skb);
506 br->has_ipv6_addr = 0;
507 return NULL;
508 }
509
510 br->has_ipv6_addr = 1;
511 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
512
513 hopopt = (u8 *)(ip6h + 1);
514 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */
515 hopopt[1] = 0; /* length of HbH */
516 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */
517 hopopt[3] = 2; /* Length of RA Option */
518 hopopt[4] = 0; /* Type = 0x0000 (MLD) */
519 hopopt[5] = 0;
520 hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */
521 hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */
522
523 skb_put(skb, sizeof(*ip6h) + 8);
524
525 /* ICMPv6 */
526 skb_set_transport_header(skb, skb->len);
527 interval = ipv6_addr_any(grp) ?
528 br->multicast_query_response_interval :
529 br->multicast_last_member_interval;
530 *igmp_type = ICMPV6_MGM_QUERY;
531 switch (br->multicast_mld_version) {
532 case 1:
533 mldq = (struct mld_msg *)icmp6_hdr(skb);
534 mldq->mld_type = ICMPV6_MGM_QUERY;
535 mldq->mld_code = 0;
536 mldq->mld_cksum = 0;
537 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval));
538 mldq->mld_reserved = 0;
539 mldq->mld_mca = *grp;
540 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
541 sizeof(*mldq), IPPROTO_ICMPV6,
542 csum_partial(mldq,
543 sizeof(*mldq),
544 0));
545 break;
546 case 2:
547 mld2q = (struct mld2_query *)icmp6_hdr(skb);
548 mld2q->mld2q_mrc = htons((u16)jiffies_to_msecs(interval));
549 mld2q->mld2q_type = ICMPV6_MGM_QUERY;
550 mld2q->mld2q_code = 0;
551 mld2q->mld2q_cksum = 0;
552 mld2q->mld2q_resv1 = 0;
553 mld2q->mld2q_resv2 = 0;
554 mld2q->mld2q_suppress = 0;
555 mld2q->mld2q_qrv = 2;
556 mld2q->mld2q_nsrcs = 0;
557 mld2q->mld2q_qqic = br->multicast_query_interval / HZ;
558 mld2q->mld2q_mca = *grp;
559 mld2q->mld2q_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
560 sizeof(*mld2q),
561 IPPROTO_ICMPV6,
562 csum_partial(mld2q,
563 sizeof(*mld2q),
564 0));
565 break;
566 }
567 skb_put(skb, mld_hdr_size);
568
569 __skb_pull(skb, sizeof(*eth));
570
571 out:
572 return skb;
573 }
574 #endif
575
br_multicast_alloc_query(struct net_bridge * br,struct br_ip * addr,u8 * igmp_type)576 static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br,
577 struct br_ip *addr,
578 u8 *igmp_type)
579 {
580 switch (addr->proto) {
581 case htons(ETH_P_IP):
582 return br_ip4_multicast_alloc_query(br, addr->u.ip4, igmp_type);
583 #if IS_ENABLED(CONFIG_IPV6)
584 case htons(ETH_P_IPV6):
585 return br_ip6_multicast_alloc_query(br, &addr->u.ip6,
586 igmp_type);
587 #endif
588 }
589 return NULL;
590 }
591
br_multicast_get_group(struct net_bridge * br,struct net_bridge_port * port,struct br_ip * group,int hash)592 static struct net_bridge_mdb_entry *br_multicast_get_group(
593 struct net_bridge *br, struct net_bridge_port *port,
594 struct br_ip *group, int hash)
595 {
596 struct net_bridge_mdb_htable *mdb;
597 struct net_bridge_mdb_entry *mp;
598 unsigned int count = 0;
599 unsigned int max;
600 int elasticity;
601 int err;
602
603 mdb = rcu_dereference_protected(br->mdb, 1);
604 hlist_for_each_entry(mp, &mdb->mhash[hash], hlist[mdb->ver]) {
605 count++;
606 if (unlikely(br_ip_equal(group, &mp->addr)))
607 return mp;
608 }
609
610 elasticity = 0;
611 max = mdb->max;
612
613 if (unlikely(count > br->hash_elasticity && count)) {
614 if (net_ratelimit())
615 br_info(br, "Multicast hash table "
616 "chain limit reached: %s\n",
617 port ? port->dev->name : br->dev->name);
618
619 elasticity = br->hash_elasticity;
620 }
621
622 if (mdb->size >= max) {
623 max *= 2;
624 if (unlikely(max > br->hash_max)) {
625 br_warn(br, "Multicast hash table maximum of %d "
626 "reached, disabling snooping: %s\n",
627 br->hash_max,
628 port ? port->dev->name : br->dev->name);
629 err = -E2BIG;
630 disable:
631 br->multicast_disabled = 1;
632 goto err;
633 }
634 }
635
636 if (max > mdb->max || elasticity) {
637 if (mdb->old) {
638 if (net_ratelimit())
639 br_info(br, "Multicast hash table "
640 "on fire: %s\n",
641 port ? port->dev->name : br->dev->name);
642 err = -EEXIST;
643 goto err;
644 }
645
646 err = br_mdb_rehash(&br->mdb, max, elasticity);
647 if (err) {
648 br_warn(br, "Cannot rehash multicast "
649 "hash table, disabling snooping: %s, %d, %d\n",
650 port ? port->dev->name : br->dev->name,
651 mdb->size, err);
652 goto disable;
653 }
654
655 err = -EAGAIN;
656 goto err;
657 }
658
659 return NULL;
660
661 err:
662 mp = ERR_PTR(err);
663 return mp;
664 }
665
br_multicast_new_group(struct net_bridge * br,struct net_bridge_port * p,struct br_ip * group)666 struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br,
667 struct net_bridge_port *p,
668 struct br_ip *group)
669 {
670 struct net_bridge_mdb_htable *mdb;
671 struct net_bridge_mdb_entry *mp;
672 int hash;
673 int err;
674
675 mdb = rcu_dereference_protected(br->mdb, 1);
676 if (!mdb) {
677 err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0);
678 if (err)
679 return ERR_PTR(err);
680 goto rehash;
681 }
682
683 hash = br_ip_hash(mdb, group);
684 mp = br_multicast_get_group(br, p, group, hash);
685 switch (PTR_ERR(mp)) {
686 case 0:
687 break;
688
689 case -EAGAIN:
690 rehash:
691 mdb = rcu_dereference_protected(br->mdb, 1);
692 hash = br_ip_hash(mdb, group);
693 break;
694
695 default:
696 goto out;
697 }
698
699 mp = kzalloc(sizeof(*mp), GFP_ATOMIC);
700 if (unlikely(!mp))
701 return ERR_PTR(-ENOMEM);
702
703 mp->br = br;
704 mp->addr = *group;
705 timer_setup(&mp->timer, br_multicast_group_expired, 0);
706
707 hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]);
708 mdb->size++;
709
710 out:
711 return mp;
712 }
713
br_multicast_new_port_group(struct net_bridge_port * port,struct br_ip * group,struct net_bridge_port_group __rcu * next,unsigned char flags,const unsigned char * src)714 struct net_bridge_port_group *br_multicast_new_port_group(
715 struct net_bridge_port *port,
716 struct br_ip *group,
717 struct net_bridge_port_group __rcu *next,
718 unsigned char flags,
719 const unsigned char *src)
720 {
721 struct net_bridge_port_group *p;
722
723 p = kzalloc(sizeof(*p), GFP_ATOMIC);
724 if (unlikely(!p))
725 return NULL;
726
727 p->addr = *group;
728 p->port = port;
729 p->flags = flags;
730 rcu_assign_pointer(p->next, next);
731 hlist_add_head(&p->mglist, &port->mglist);
732 timer_setup(&p->timer, br_multicast_port_group_expired, 0);
733
734 if (src)
735 memcpy(p->eth_addr, src, ETH_ALEN);
736 else
737 memset(p->eth_addr, 0xff, ETH_ALEN);
738
739 return p;
740 }
741
br_port_group_equal(struct net_bridge_port_group * p,struct net_bridge_port * port,const unsigned char * src)742 static bool br_port_group_equal(struct net_bridge_port_group *p,
743 struct net_bridge_port *port,
744 const unsigned char *src)
745 {
746 if (p->port != port)
747 return false;
748
749 if (!(port->flags & BR_MULTICAST_TO_UNICAST))
750 return true;
751
752 return ether_addr_equal(src, p->eth_addr);
753 }
754
br_multicast_add_group(struct net_bridge * br,struct net_bridge_port * port,struct br_ip * group,const unsigned char * src)755 static int br_multicast_add_group(struct net_bridge *br,
756 struct net_bridge_port *port,
757 struct br_ip *group,
758 const unsigned char *src)
759 {
760 struct net_bridge_port_group __rcu **pp;
761 struct net_bridge_port_group *p;
762 struct net_bridge_mdb_entry *mp;
763 unsigned long now = jiffies;
764 int err;
765
766 spin_lock(&br->multicast_lock);
767 if (!netif_running(br->dev) ||
768 (port && port->state == BR_STATE_DISABLED))
769 goto out;
770
771 mp = br_multicast_new_group(br, port, group);
772 err = PTR_ERR(mp);
773 if (IS_ERR(mp))
774 goto err;
775
776 if (!port) {
777 if (!mp->host_joined) {
778 mp->host_joined = true;
779 br_mdb_notify(br->dev, NULL, &mp->addr, RTM_NEWMDB, 0);
780 }
781 mod_timer(&mp->timer, now + br->multicast_membership_interval);
782 goto out;
783 }
784
785 for (pp = &mp->ports;
786 (p = mlock_dereference(*pp, br)) != NULL;
787 pp = &p->next) {
788 if (br_port_group_equal(p, port, src))
789 goto found;
790 if ((unsigned long)p->port < (unsigned long)port)
791 break;
792 }
793
794 p = br_multicast_new_port_group(port, group, *pp, 0, src);
795 if (unlikely(!p))
796 goto err;
797 rcu_assign_pointer(*pp, p);
798 br_mdb_notify(br->dev, port, group, RTM_NEWMDB, 0);
799
800 found:
801 mod_timer(&p->timer, now + br->multicast_membership_interval);
802 out:
803 err = 0;
804
805 err:
806 spin_unlock(&br->multicast_lock);
807 return err;
808 }
809
br_ip4_multicast_add_group(struct net_bridge * br,struct net_bridge_port * port,__be32 group,__u16 vid,const unsigned char * src)810 static int br_ip4_multicast_add_group(struct net_bridge *br,
811 struct net_bridge_port *port,
812 __be32 group,
813 __u16 vid,
814 const unsigned char *src)
815 {
816 struct br_ip br_group;
817
818 if (ipv4_is_local_multicast(group))
819 return 0;
820
821 br_group.u.ip4 = group;
822 br_group.proto = htons(ETH_P_IP);
823 br_group.vid = vid;
824
825 return br_multicast_add_group(br, port, &br_group, src);
826 }
827
828 #if IS_ENABLED(CONFIG_IPV6)
br_ip6_multicast_add_group(struct net_bridge * br,struct net_bridge_port * port,const struct in6_addr * group,__u16 vid,const unsigned char * src)829 static int br_ip6_multicast_add_group(struct net_bridge *br,
830 struct net_bridge_port *port,
831 const struct in6_addr *group,
832 __u16 vid,
833 const unsigned char *src)
834 {
835 struct br_ip br_group;
836
837 if (ipv6_addr_is_ll_all_nodes(group))
838 return 0;
839
840 br_group.u.ip6 = *group;
841 br_group.proto = htons(ETH_P_IPV6);
842 br_group.vid = vid;
843
844 return br_multicast_add_group(br, port, &br_group, src);
845 }
846 #endif
847
br_multicast_router_expired(struct timer_list * t)848 static void br_multicast_router_expired(struct timer_list *t)
849 {
850 struct net_bridge_port *port =
851 from_timer(port, t, multicast_router_timer);
852 struct net_bridge *br = port->br;
853
854 spin_lock(&br->multicast_lock);
855 if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
856 port->multicast_router == MDB_RTR_TYPE_PERM ||
857 timer_pending(&port->multicast_router_timer))
858 goto out;
859
860 __del_port_router(port);
861 out:
862 spin_unlock(&br->multicast_lock);
863 }
864
br_mc_router_state_change(struct net_bridge * p,bool is_mc_router)865 static void br_mc_router_state_change(struct net_bridge *p,
866 bool is_mc_router)
867 {
868 struct switchdev_attr attr = {
869 .orig_dev = p->dev,
870 .id = SWITCHDEV_ATTR_ID_BRIDGE_MROUTER,
871 .flags = SWITCHDEV_F_DEFER,
872 .u.mrouter = is_mc_router,
873 };
874
875 switchdev_port_attr_set(p->dev, &attr);
876 }
877
br_multicast_local_router_expired(struct timer_list * t)878 static void br_multicast_local_router_expired(struct timer_list *t)
879 {
880 struct net_bridge *br = from_timer(br, t, multicast_router_timer);
881
882 spin_lock(&br->multicast_lock);
883 if (br->multicast_router == MDB_RTR_TYPE_DISABLED ||
884 br->multicast_router == MDB_RTR_TYPE_PERM ||
885 timer_pending(&br->multicast_router_timer))
886 goto out;
887
888 br_mc_router_state_change(br, false);
889 out:
890 spin_unlock(&br->multicast_lock);
891 }
892
br_multicast_querier_expired(struct net_bridge * br,struct bridge_mcast_own_query * query)893 static void br_multicast_querier_expired(struct net_bridge *br,
894 struct bridge_mcast_own_query *query)
895 {
896 spin_lock(&br->multicast_lock);
897 if (!netif_running(br->dev) || br->multicast_disabled)
898 goto out;
899
900 br_multicast_start_querier(br, query);
901
902 out:
903 spin_unlock(&br->multicast_lock);
904 }
905
br_ip4_multicast_querier_expired(struct timer_list * t)906 static void br_ip4_multicast_querier_expired(struct timer_list *t)
907 {
908 struct net_bridge *br = from_timer(br, t, ip4_other_query.timer);
909
910 br_multicast_querier_expired(br, &br->ip4_own_query);
911 }
912
913 #if IS_ENABLED(CONFIG_IPV6)
br_ip6_multicast_querier_expired(struct timer_list * t)914 static void br_ip6_multicast_querier_expired(struct timer_list *t)
915 {
916 struct net_bridge *br = from_timer(br, t, ip6_other_query.timer);
917
918 br_multicast_querier_expired(br, &br->ip6_own_query);
919 }
920 #endif
921
br_multicast_select_own_querier(struct net_bridge * br,struct br_ip * ip,struct sk_buff * skb)922 static void br_multicast_select_own_querier(struct net_bridge *br,
923 struct br_ip *ip,
924 struct sk_buff *skb)
925 {
926 if (ip->proto == htons(ETH_P_IP))
927 br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr;
928 #if IS_ENABLED(CONFIG_IPV6)
929 else
930 br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr;
931 #endif
932 }
933
__br_multicast_send_query(struct net_bridge * br,struct net_bridge_port * port,struct br_ip * ip)934 static void __br_multicast_send_query(struct net_bridge *br,
935 struct net_bridge_port *port,
936 struct br_ip *ip)
937 {
938 struct sk_buff *skb;
939 u8 igmp_type;
940
941 skb = br_multicast_alloc_query(br, ip, &igmp_type);
942 if (!skb)
943 return;
944
945 if (port) {
946 skb->dev = port->dev;
947 br_multicast_count(br, port, skb, igmp_type,
948 BR_MCAST_DIR_TX);
949 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
950 dev_net(port->dev), NULL, skb, NULL, skb->dev,
951 br_dev_queue_push_xmit);
952 } else {
953 br_multicast_select_own_querier(br, ip, skb);
954 br_multicast_count(br, port, skb, igmp_type,
955 BR_MCAST_DIR_RX);
956 netif_rx(skb);
957 }
958 }
959
br_multicast_send_query(struct net_bridge * br,struct net_bridge_port * port,struct bridge_mcast_own_query * own_query)960 static void br_multicast_send_query(struct net_bridge *br,
961 struct net_bridge_port *port,
962 struct bridge_mcast_own_query *own_query)
963 {
964 struct bridge_mcast_other_query *other_query = NULL;
965 struct br_ip br_group;
966 unsigned long time;
967
968 if (!netif_running(br->dev) || br->multicast_disabled ||
969 !br->multicast_querier)
970 return;
971
972 memset(&br_group.u, 0, sizeof(br_group.u));
973
974 if (port ? (own_query == &port->ip4_own_query) :
975 (own_query == &br->ip4_own_query)) {
976 other_query = &br->ip4_other_query;
977 br_group.proto = htons(ETH_P_IP);
978 #if IS_ENABLED(CONFIG_IPV6)
979 } else {
980 other_query = &br->ip6_other_query;
981 br_group.proto = htons(ETH_P_IPV6);
982 #endif
983 }
984
985 if (!other_query || timer_pending(&other_query->timer))
986 return;
987
988 __br_multicast_send_query(br, port, &br_group);
989
990 time = jiffies;
991 time += own_query->startup_sent < br->multicast_startup_query_count ?
992 br->multicast_startup_query_interval :
993 br->multicast_query_interval;
994 mod_timer(&own_query->timer, time);
995 }
996
997 static void
br_multicast_port_query_expired(struct net_bridge_port * port,struct bridge_mcast_own_query * query)998 br_multicast_port_query_expired(struct net_bridge_port *port,
999 struct bridge_mcast_own_query *query)
1000 {
1001 struct net_bridge *br = port->br;
1002
1003 spin_lock(&br->multicast_lock);
1004 if (port->state == BR_STATE_DISABLED ||
1005 port->state == BR_STATE_BLOCKING)
1006 goto out;
1007
1008 if (query->startup_sent < br->multicast_startup_query_count)
1009 query->startup_sent++;
1010
1011 br_multicast_send_query(port->br, port, query);
1012
1013 out:
1014 spin_unlock(&br->multicast_lock);
1015 }
1016
br_ip4_multicast_port_query_expired(struct timer_list * t)1017 static void br_ip4_multicast_port_query_expired(struct timer_list *t)
1018 {
1019 struct net_bridge_port *port = from_timer(port, t, ip4_own_query.timer);
1020
1021 br_multicast_port_query_expired(port, &port->ip4_own_query);
1022 }
1023
1024 #if IS_ENABLED(CONFIG_IPV6)
br_ip6_multicast_port_query_expired(struct timer_list * t)1025 static void br_ip6_multicast_port_query_expired(struct timer_list *t)
1026 {
1027 struct net_bridge_port *port = from_timer(port, t, ip6_own_query.timer);
1028
1029 br_multicast_port_query_expired(port, &port->ip6_own_query);
1030 }
1031 #endif
1032
br_mc_disabled_update(struct net_device * dev,bool value)1033 static void br_mc_disabled_update(struct net_device *dev, bool value)
1034 {
1035 struct switchdev_attr attr = {
1036 .orig_dev = dev,
1037 .id = SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED,
1038 .flags = SWITCHDEV_F_DEFER,
1039 .u.mc_disabled = value,
1040 };
1041
1042 switchdev_port_attr_set(dev, &attr);
1043 }
1044
br_multicast_add_port(struct net_bridge_port * port)1045 int br_multicast_add_port(struct net_bridge_port *port)
1046 {
1047 port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
1048
1049 timer_setup(&port->multicast_router_timer,
1050 br_multicast_router_expired, 0);
1051 timer_setup(&port->ip4_own_query.timer,
1052 br_ip4_multicast_port_query_expired, 0);
1053 #if IS_ENABLED(CONFIG_IPV6)
1054 timer_setup(&port->ip6_own_query.timer,
1055 br_ip6_multicast_port_query_expired, 0);
1056 #endif
1057 br_mc_disabled_update(port->dev, port->br->multicast_disabled);
1058
1059 port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
1060 if (!port->mcast_stats)
1061 return -ENOMEM;
1062
1063 return 0;
1064 }
1065
br_multicast_del_port(struct net_bridge_port * port)1066 void br_multicast_del_port(struct net_bridge_port *port)
1067 {
1068 struct net_bridge *br = port->br;
1069 struct net_bridge_port_group *pg;
1070 struct hlist_node *n;
1071
1072 /* Take care of the remaining groups, only perm ones should be left */
1073 spin_lock_bh(&br->multicast_lock);
1074 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
1075 br_multicast_del_pg(br, pg);
1076 spin_unlock_bh(&br->multicast_lock);
1077 del_timer_sync(&port->multicast_router_timer);
1078 free_percpu(port->mcast_stats);
1079 }
1080
br_multicast_enable(struct bridge_mcast_own_query * query)1081 static void br_multicast_enable(struct bridge_mcast_own_query *query)
1082 {
1083 query->startup_sent = 0;
1084
1085 if (try_to_del_timer_sync(&query->timer) >= 0 ||
1086 del_timer(&query->timer))
1087 mod_timer(&query->timer, jiffies);
1088 }
1089
__br_multicast_enable_port(struct net_bridge_port * port)1090 static void __br_multicast_enable_port(struct net_bridge_port *port)
1091 {
1092 struct net_bridge *br = port->br;
1093
1094 if (br->multicast_disabled || !netif_running(br->dev))
1095 return;
1096
1097 br_multicast_enable(&port->ip4_own_query);
1098 #if IS_ENABLED(CONFIG_IPV6)
1099 br_multicast_enable(&port->ip6_own_query);
1100 #endif
1101 if (port->multicast_router == MDB_RTR_TYPE_PERM &&
1102 hlist_unhashed(&port->rlist))
1103 br_multicast_add_router(br, port);
1104 }
1105
br_multicast_enable_port(struct net_bridge_port * port)1106 void br_multicast_enable_port(struct net_bridge_port *port)
1107 {
1108 struct net_bridge *br = port->br;
1109
1110 spin_lock(&br->multicast_lock);
1111 __br_multicast_enable_port(port);
1112 spin_unlock(&br->multicast_lock);
1113 }
1114
br_multicast_disable_port(struct net_bridge_port * port)1115 void br_multicast_disable_port(struct net_bridge_port *port)
1116 {
1117 struct net_bridge *br = port->br;
1118 struct net_bridge_port_group *pg;
1119 struct hlist_node *n;
1120
1121 spin_lock(&br->multicast_lock);
1122 hlist_for_each_entry_safe(pg, n, &port->mglist, mglist)
1123 if (!(pg->flags & MDB_PG_FLAGS_PERMANENT))
1124 br_multicast_del_pg(br, pg);
1125
1126 __del_port_router(port);
1127
1128 del_timer(&port->multicast_router_timer);
1129 del_timer(&port->ip4_own_query.timer);
1130 #if IS_ENABLED(CONFIG_IPV6)
1131 del_timer(&port->ip6_own_query.timer);
1132 #endif
1133 spin_unlock(&br->multicast_lock);
1134 }
1135
br_ip4_multicast_igmp3_report(struct net_bridge * br,struct net_bridge_port * port,struct sk_buff * skb,u16 vid)1136 static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
1137 struct net_bridge_port *port,
1138 struct sk_buff *skb,
1139 u16 vid)
1140 {
1141 const unsigned char *src;
1142 struct igmpv3_report *ih;
1143 struct igmpv3_grec *grec;
1144 int i;
1145 int len;
1146 int num;
1147 int type;
1148 int err = 0;
1149 __be32 group;
1150
1151 ih = igmpv3_report_hdr(skb);
1152 num = ntohs(ih->ngrec);
1153 len = skb_transport_offset(skb) + sizeof(*ih);
1154
1155 for (i = 0; i < num; i++) {
1156 len += sizeof(*grec);
1157 if (!pskb_may_pull(skb, len))
1158 return -EINVAL;
1159
1160 grec = (void *)(skb->data + len - sizeof(*grec));
1161 group = grec->grec_mca;
1162 type = grec->grec_type;
1163
1164 len += ntohs(grec->grec_nsrcs) * 4;
1165 if (!pskb_may_pull(skb, len))
1166 return -EINVAL;
1167
1168 /* We treat this as an IGMPv2 report for now. */
1169 switch (type) {
1170 case IGMPV3_MODE_IS_INCLUDE:
1171 case IGMPV3_MODE_IS_EXCLUDE:
1172 case IGMPV3_CHANGE_TO_INCLUDE:
1173 case IGMPV3_CHANGE_TO_EXCLUDE:
1174 case IGMPV3_ALLOW_NEW_SOURCES:
1175 case IGMPV3_BLOCK_OLD_SOURCES:
1176 break;
1177
1178 default:
1179 continue;
1180 }
1181
1182 src = eth_hdr(skb)->h_source;
1183 if ((type == IGMPV3_CHANGE_TO_INCLUDE ||
1184 type == IGMPV3_MODE_IS_INCLUDE) &&
1185 ntohs(grec->grec_nsrcs) == 0) {
1186 br_ip4_multicast_leave_group(br, port, group, vid, src);
1187 } else {
1188 err = br_ip4_multicast_add_group(br, port, group, vid,
1189 src);
1190 if (err)
1191 break;
1192 }
1193 }
1194
1195 return err;
1196 }
1197
1198 #if IS_ENABLED(CONFIG_IPV6)
br_ip6_multicast_mld2_report(struct net_bridge * br,struct net_bridge_port * port,struct sk_buff * skb,u16 vid)1199 static int br_ip6_multicast_mld2_report(struct net_bridge *br,
1200 struct net_bridge_port *port,
1201 struct sk_buff *skb,
1202 u16 vid)
1203 {
1204 const unsigned char *src;
1205 struct icmp6hdr *icmp6h;
1206 struct mld2_grec *grec;
1207 int i;
1208 int len;
1209 int num;
1210 int err = 0;
1211
1212 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
1213 return -EINVAL;
1214
1215 icmp6h = icmp6_hdr(skb);
1216 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
1217 len = skb_transport_offset(skb) + sizeof(*icmp6h);
1218
1219 for (i = 0; i < num; i++) {
1220 __be16 *nsrcs, _nsrcs;
1221
1222 nsrcs = skb_header_pointer(skb,
1223 len + offsetof(struct mld2_grec,
1224 grec_nsrcs),
1225 sizeof(_nsrcs), &_nsrcs);
1226 if (!nsrcs)
1227 return -EINVAL;
1228
1229 if (!pskb_may_pull(skb,
1230 len + sizeof(*grec) +
1231 sizeof(struct in6_addr) * ntohs(*nsrcs)))
1232 return -EINVAL;
1233
1234 grec = (struct mld2_grec *)(skb->data + len);
1235 len += sizeof(*grec) +
1236 sizeof(struct in6_addr) * ntohs(*nsrcs);
1237
1238 /* We treat these as MLDv1 reports for now. */
1239 switch (grec->grec_type) {
1240 case MLD2_MODE_IS_INCLUDE:
1241 case MLD2_MODE_IS_EXCLUDE:
1242 case MLD2_CHANGE_TO_INCLUDE:
1243 case MLD2_CHANGE_TO_EXCLUDE:
1244 case MLD2_ALLOW_NEW_SOURCES:
1245 case MLD2_BLOCK_OLD_SOURCES:
1246 break;
1247
1248 default:
1249 continue;
1250 }
1251
1252 src = eth_hdr(skb)->h_source;
1253 if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE ||
1254 grec->grec_type == MLD2_MODE_IS_INCLUDE) &&
1255 ntohs(*nsrcs) == 0) {
1256 br_ip6_multicast_leave_group(br, port, &grec->grec_mca,
1257 vid, src);
1258 } else {
1259 err = br_ip6_multicast_add_group(br, port,
1260 &grec->grec_mca, vid,
1261 src);
1262 if (err)
1263 break;
1264 }
1265 }
1266
1267 return err;
1268 }
1269 #endif
1270
br_ip4_multicast_select_querier(struct net_bridge * br,struct net_bridge_port * port,__be32 saddr)1271 static bool br_ip4_multicast_select_querier(struct net_bridge *br,
1272 struct net_bridge_port *port,
1273 __be32 saddr)
1274 {
1275 if (!timer_pending(&br->ip4_own_query.timer) &&
1276 !timer_pending(&br->ip4_other_query.timer))
1277 goto update;
1278
1279 if (!br->ip4_querier.addr.u.ip4)
1280 goto update;
1281
1282 if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4))
1283 goto update;
1284
1285 return false;
1286
1287 update:
1288 br->ip4_querier.addr.u.ip4 = saddr;
1289
1290 /* update protected by general multicast_lock by caller */
1291 rcu_assign_pointer(br->ip4_querier.port, port);
1292
1293 return true;
1294 }
1295
1296 #if IS_ENABLED(CONFIG_IPV6)
br_ip6_multicast_select_querier(struct net_bridge * br,struct net_bridge_port * port,struct in6_addr * saddr)1297 static bool br_ip6_multicast_select_querier(struct net_bridge *br,
1298 struct net_bridge_port *port,
1299 struct in6_addr *saddr)
1300 {
1301 if (!timer_pending(&br->ip6_own_query.timer) &&
1302 !timer_pending(&br->ip6_other_query.timer))
1303 goto update;
1304
1305 if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0)
1306 goto update;
1307
1308 return false;
1309
1310 update:
1311 br->ip6_querier.addr.u.ip6 = *saddr;
1312
1313 /* update protected by general multicast_lock by caller */
1314 rcu_assign_pointer(br->ip6_querier.port, port);
1315
1316 return true;
1317 }
1318 #endif
1319
br_multicast_select_querier(struct net_bridge * br,struct net_bridge_port * port,struct br_ip * saddr)1320 static bool br_multicast_select_querier(struct net_bridge *br,
1321 struct net_bridge_port *port,
1322 struct br_ip *saddr)
1323 {
1324 switch (saddr->proto) {
1325 case htons(ETH_P_IP):
1326 return br_ip4_multicast_select_querier(br, port, saddr->u.ip4);
1327 #if IS_ENABLED(CONFIG_IPV6)
1328 case htons(ETH_P_IPV6):
1329 return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6);
1330 #endif
1331 }
1332
1333 return false;
1334 }
1335
1336 static void
br_multicast_update_query_timer(struct net_bridge * br,struct bridge_mcast_other_query * query,unsigned long max_delay)1337 br_multicast_update_query_timer(struct net_bridge *br,
1338 struct bridge_mcast_other_query *query,
1339 unsigned long max_delay)
1340 {
1341 if (!timer_pending(&query->timer))
1342 query->delay_time = jiffies + max_delay;
1343
1344 mod_timer(&query->timer, jiffies + br->multicast_querier_interval);
1345 }
1346
br_port_mc_router_state_change(struct net_bridge_port * p,bool is_mc_router)1347 static void br_port_mc_router_state_change(struct net_bridge_port *p,
1348 bool is_mc_router)
1349 {
1350 struct switchdev_attr attr = {
1351 .orig_dev = p->dev,
1352 .id = SWITCHDEV_ATTR_ID_PORT_MROUTER,
1353 .flags = SWITCHDEV_F_DEFER,
1354 .u.mrouter = is_mc_router,
1355 };
1356
1357 switchdev_port_attr_set(p->dev, &attr);
1358 }
1359
1360 /*
1361 * Add port to router_list
1362 * list is maintained ordered by pointer value
1363 * and locked by br->multicast_lock and RCU
1364 */
br_multicast_add_router(struct net_bridge * br,struct net_bridge_port * port)1365 static void br_multicast_add_router(struct net_bridge *br,
1366 struct net_bridge_port *port)
1367 {
1368 struct net_bridge_port *p;
1369 struct hlist_node *slot = NULL;
1370
1371 if (!hlist_unhashed(&port->rlist))
1372 return;
1373
1374 hlist_for_each_entry(p, &br->router_list, rlist) {
1375 if ((unsigned long) port >= (unsigned long) p)
1376 break;
1377 slot = &p->rlist;
1378 }
1379
1380 if (slot)
1381 hlist_add_behind_rcu(&port->rlist, slot);
1382 else
1383 hlist_add_head_rcu(&port->rlist, &br->router_list);
1384 br_rtr_notify(br->dev, port, RTM_NEWMDB);
1385 br_port_mc_router_state_change(port, true);
1386 }
1387
br_multicast_mark_router(struct net_bridge * br,struct net_bridge_port * port)1388 static void br_multicast_mark_router(struct net_bridge *br,
1389 struct net_bridge_port *port)
1390 {
1391 unsigned long now = jiffies;
1392
1393 if (!port) {
1394 if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) {
1395 if (!timer_pending(&br->multicast_router_timer))
1396 br_mc_router_state_change(br, true);
1397 mod_timer(&br->multicast_router_timer,
1398 now + br->multicast_querier_interval);
1399 }
1400 return;
1401 }
1402
1403 if (port->multicast_router == MDB_RTR_TYPE_DISABLED ||
1404 port->multicast_router == MDB_RTR_TYPE_PERM)
1405 return;
1406
1407 br_multicast_add_router(br, port);
1408
1409 mod_timer(&port->multicast_router_timer,
1410 now + br->multicast_querier_interval);
1411 }
1412
br_multicast_query_received(struct net_bridge * br,struct net_bridge_port * port,struct bridge_mcast_other_query * query,struct br_ip * saddr,unsigned long max_delay)1413 static void br_multicast_query_received(struct net_bridge *br,
1414 struct net_bridge_port *port,
1415 struct bridge_mcast_other_query *query,
1416 struct br_ip *saddr,
1417 unsigned long max_delay)
1418 {
1419 if (!br_multicast_select_querier(br, port, saddr))
1420 return;
1421
1422 br_multicast_update_query_timer(br, query, max_delay);
1423 br_multicast_mark_router(br, port);
1424 }
1425
br_ip4_multicast_query(struct net_bridge * br,struct net_bridge_port * port,struct sk_buff * skb,u16 vid)1426 static void br_ip4_multicast_query(struct net_bridge *br,
1427 struct net_bridge_port *port,
1428 struct sk_buff *skb,
1429 u16 vid)
1430 {
1431 const struct iphdr *iph = ip_hdr(skb);
1432 struct igmphdr *ih = igmp_hdr(skb);
1433 struct net_bridge_mdb_entry *mp;
1434 struct igmpv3_query *ih3;
1435 struct net_bridge_port_group *p;
1436 struct net_bridge_port_group __rcu **pp;
1437 struct br_ip saddr;
1438 unsigned long max_delay;
1439 unsigned long now = jiffies;
1440 unsigned int offset = skb_transport_offset(skb);
1441 __be32 group;
1442
1443 spin_lock(&br->multicast_lock);
1444 if (!netif_running(br->dev) ||
1445 (port && port->state == BR_STATE_DISABLED))
1446 goto out;
1447
1448 group = ih->group;
1449
1450 if (skb->len == offset + sizeof(*ih)) {
1451 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE);
1452
1453 if (!max_delay) {
1454 max_delay = 10 * HZ;
1455 group = 0;
1456 }
1457 } else if (skb->len >= offset + sizeof(*ih3)) {
1458 ih3 = igmpv3_query_hdr(skb);
1459 if (ih3->nsrcs)
1460 goto out;
1461
1462 max_delay = ih3->code ?
1463 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1;
1464 } else {
1465 goto out;
1466 }
1467
1468 if (!group) {
1469 saddr.proto = htons(ETH_P_IP);
1470 saddr.u.ip4 = iph->saddr;
1471
1472 br_multicast_query_received(br, port, &br->ip4_other_query,
1473 &saddr, max_delay);
1474 goto out;
1475 }
1476
1477 mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid);
1478 if (!mp)
1479 goto out;
1480
1481 max_delay *= br->multicast_last_member_count;
1482
1483 if (mp->host_joined &&
1484 (timer_pending(&mp->timer) ?
1485 time_after(mp->timer.expires, now + max_delay) :
1486 try_to_del_timer_sync(&mp->timer) >= 0))
1487 mod_timer(&mp->timer, now + max_delay);
1488
1489 for (pp = &mp->ports;
1490 (p = mlock_dereference(*pp, br)) != NULL;
1491 pp = &p->next) {
1492 if (timer_pending(&p->timer) ?
1493 time_after(p->timer.expires, now + max_delay) :
1494 try_to_del_timer_sync(&p->timer) >= 0)
1495 mod_timer(&p->timer, now + max_delay);
1496 }
1497
1498 out:
1499 spin_unlock(&br->multicast_lock);
1500 }
1501
1502 #if IS_ENABLED(CONFIG_IPV6)
br_ip6_multicast_query(struct net_bridge * br,struct net_bridge_port * port,struct sk_buff * skb,u16 vid)1503 static int br_ip6_multicast_query(struct net_bridge *br,
1504 struct net_bridge_port *port,
1505 struct sk_buff *skb,
1506 u16 vid)
1507 {
1508 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
1509 struct mld_msg *mld;
1510 struct net_bridge_mdb_entry *mp;
1511 struct mld2_query *mld2q;
1512 struct net_bridge_port_group *p;
1513 struct net_bridge_port_group __rcu **pp;
1514 struct br_ip saddr;
1515 unsigned long max_delay;
1516 unsigned long now = jiffies;
1517 unsigned int offset = skb_transport_offset(skb);
1518 const struct in6_addr *group = NULL;
1519 bool is_general_query;
1520 int err = 0;
1521
1522 spin_lock(&br->multicast_lock);
1523 if (!netif_running(br->dev) ||
1524 (port && port->state == BR_STATE_DISABLED))
1525 goto out;
1526
1527 if (skb->len == offset + sizeof(*mld)) {
1528 if (!pskb_may_pull(skb, offset + sizeof(*mld))) {
1529 err = -EINVAL;
1530 goto out;
1531 }
1532 mld = (struct mld_msg *) icmp6_hdr(skb);
1533 max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
1534 if (max_delay)
1535 group = &mld->mld_mca;
1536 } else {
1537 if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) {
1538 err = -EINVAL;
1539 goto out;
1540 }
1541 mld2q = (struct mld2_query *)icmp6_hdr(skb);
1542 if (!mld2q->mld2q_nsrcs)
1543 group = &mld2q->mld2q_mca;
1544
1545 max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL);
1546 }
1547
1548 is_general_query = group && ipv6_addr_any(group);
1549
1550 if (is_general_query) {
1551 saddr.proto = htons(ETH_P_IPV6);
1552 saddr.u.ip6 = ip6h->saddr;
1553
1554 br_multicast_query_received(br, port, &br->ip6_other_query,
1555 &saddr, max_delay);
1556 goto out;
1557 } else if (!group) {
1558 goto out;
1559 }
1560
1561 mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid);
1562 if (!mp)
1563 goto out;
1564
1565 max_delay *= br->multicast_last_member_count;
1566 if (mp->host_joined &&
1567 (timer_pending(&mp->timer) ?
1568 time_after(mp->timer.expires, now + max_delay) :
1569 try_to_del_timer_sync(&mp->timer) >= 0))
1570 mod_timer(&mp->timer, now + max_delay);
1571
1572 for (pp = &mp->ports;
1573 (p = mlock_dereference(*pp, br)) != NULL;
1574 pp = &p->next) {
1575 if (timer_pending(&p->timer) ?
1576 time_after(p->timer.expires, now + max_delay) :
1577 try_to_del_timer_sync(&p->timer) >= 0)
1578 mod_timer(&p->timer, now + max_delay);
1579 }
1580
1581 out:
1582 spin_unlock(&br->multicast_lock);
1583 return err;
1584 }
1585 #endif
1586
1587 static void
br_multicast_leave_group(struct net_bridge * br,struct net_bridge_port * port,struct br_ip * group,struct bridge_mcast_other_query * other_query,struct bridge_mcast_own_query * own_query,const unsigned char * src)1588 br_multicast_leave_group(struct net_bridge *br,
1589 struct net_bridge_port *port,
1590 struct br_ip *group,
1591 struct bridge_mcast_other_query *other_query,
1592 struct bridge_mcast_own_query *own_query,
1593 const unsigned char *src)
1594 {
1595 struct net_bridge_mdb_htable *mdb;
1596 struct net_bridge_mdb_entry *mp;
1597 struct net_bridge_port_group *p;
1598 unsigned long now;
1599 unsigned long time;
1600
1601 spin_lock(&br->multicast_lock);
1602 if (!netif_running(br->dev) ||
1603 (port && port->state == BR_STATE_DISABLED))
1604 goto out;
1605
1606 mdb = mlock_dereference(br->mdb, br);
1607 mp = br_mdb_ip_get(mdb, group);
1608 if (!mp)
1609 goto out;
1610
1611 if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) {
1612 struct net_bridge_port_group __rcu **pp;
1613
1614 for (pp = &mp->ports;
1615 (p = mlock_dereference(*pp, br)) != NULL;
1616 pp = &p->next) {
1617 if (!br_port_group_equal(p, port, src))
1618 continue;
1619
1620 rcu_assign_pointer(*pp, p->next);
1621 hlist_del_init(&p->mglist);
1622 del_timer(&p->timer);
1623 call_rcu_bh(&p->rcu, br_multicast_free_pg);
1624 br_mdb_notify(br->dev, port, group, RTM_DELMDB,
1625 p->flags);
1626
1627 if (!mp->ports && !mp->host_joined &&
1628 netif_running(br->dev))
1629 mod_timer(&mp->timer, jiffies);
1630 }
1631 goto out;
1632 }
1633
1634 if (timer_pending(&other_query->timer))
1635 goto out;
1636
1637 if (br->multicast_querier) {
1638 __br_multicast_send_query(br, port, &mp->addr);
1639
1640 time = jiffies + br->multicast_last_member_count *
1641 br->multicast_last_member_interval;
1642
1643 mod_timer(&own_query->timer, time);
1644
1645 for (p = mlock_dereference(mp->ports, br);
1646 p != NULL;
1647 p = mlock_dereference(p->next, br)) {
1648 if (!br_port_group_equal(p, port, src))
1649 continue;
1650
1651 if (!hlist_unhashed(&p->mglist) &&
1652 (timer_pending(&p->timer) ?
1653 time_after(p->timer.expires, time) :
1654 try_to_del_timer_sync(&p->timer) >= 0)) {
1655 mod_timer(&p->timer, time);
1656 }
1657
1658 break;
1659 }
1660 }
1661
1662 now = jiffies;
1663 time = now + br->multicast_last_member_count *
1664 br->multicast_last_member_interval;
1665
1666 if (!port) {
1667 if (mp->host_joined &&
1668 (timer_pending(&mp->timer) ?
1669 time_after(mp->timer.expires, time) :
1670 try_to_del_timer_sync(&mp->timer) >= 0)) {
1671 mod_timer(&mp->timer, time);
1672 }
1673
1674 goto out;
1675 }
1676
1677 for (p = mlock_dereference(mp->ports, br);
1678 p != NULL;
1679 p = mlock_dereference(p->next, br)) {
1680 if (p->port != port)
1681 continue;
1682
1683 if (!hlist_unhashed(&p->mglist) &&
1684 (timer_pending(&p->timer) ?
1685 time_after(p->timer.expires, time) :
1686 try_to_del_timer_sync(&p->timer) >= 0)) {
1687 mod_timer(&p->timer, time);
1688 }
1689
1690 break;
1691 }
1692 out:
1693 spin_unlock(&br->multicast_lock);
1694 }
1695
br_ip4_multicast_leave_group(struct net_bridge * br,struct net_bridge_port * port,__be32 group,__u16 vid,const unsigned char * src)1696 static void br_ip4_multicast_leave_group(struct net_bridge *br,
1697 struct net_bridge_port *port,
1698 __be32 group,
1699 __u16 vid,
1700 const unsigned char *src)
1701 {
1702 struct br_ip br_group;
1703 struct bridge_mcast_own_query *own_query;
1704
1705 if (ipv4_is_local_multicast(group))
1706 return;
1707
1708 own_query = port ? &port->ip4_own_query : &br->ip4_own_query;
1709
1710 br_group.u.ip4 = group;
1711 br_group.proto = htons(ETH_P_IP);
1712 br_group.vid = vid;
1713
1714 br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query,
1715 own_query, src);
1716 }
1717
1718 #if IS_ENABLED(CONFIG_IPV6)
br_ip6_multicast_leave_group(struct net_bridge * br,struct net_bridge_port * port,const struct in6_addr * group,__u16 vid,const unsigned char * src)1719 static void br_ip6_multicast_leave_group(struct net_bridge *br,
1720 struct net_bridge_port *port,
1721 const struct in6_addr *group,
1722 __u16 vid,
1723 const unsigned char *src)
1724 {
1725 struct br_ip br_group;
1726 struct bridge_mcast_own_query *own_query;
1727
1728 if (ipv6_addr_is_ll_all_nodes(group))
1729 return;
1730
1731 own_query = port ? &port->ip6_own_query : &br->ip6_own_query;
1732
1733 br_group.u.ip6 = *group;
1734 br_group.proto = htons(ETH_P_IPV6);
1735 br_group.vid = vid;
1736
1737 br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query,
1738 own_query, src);
1739 }
1740 #endif
1741
br_multicast_err_count(const struct net_bridge * br,const struct net_bridge_port * p,__be16 proto)1742 static void br_multicast_err_count(const struct net_bridge *br,
1743 const struct net_bridge_port *p,
1744 __be16 proto)
1745 {
1746 struct bridge_mcast_stats __percpu *stats;
1747 struct bridge_mcast_stats *pstats;
1748
1749 if (!br->multicast_stats_enabled)
1750 return;
1751
1752 if (p)
1753 stats = p->mcast_stats;
1754 else
1755 stats = br->mcast_stats;
1756 if (WARN_ON(!stats))
1757 return;
1758
1759 pstats = this_cpu_ptr(stats);
1760
1761 u64_stats_update_begin(&pstats->syncp);
1762 switch (proto) {
1763 case htons(ETH_P_IP):
1764 pstats->mstats.igmp_parse_errors++;
1765 break;
1766 #if IS_ENABLED(CONFIG_IPV6)
1767 case htons(ETH_P_IPV6):
1768 pstats->mstats.mld_parse_errors++;
1769 break;
1770 #endif
1771 }
1772 u64_stats_update_end(&pstats->syncp);
1773 }
1774
br_multicast_pim(struct net_bridge * br,struct net_bridge_port * port,const struct sk_buff * skb)1775 static void br_multicast_pim(struct net_bridge *br,
1776 struct net_bridge_port *port,
1777 const struct sk_buff *skb)
1778 {
1779 unsigned int offset = skb_transport_offset(skb);
1780 struct pimhdr *pimhdr, _pimhdr;
1781
1782 pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr);
1783 if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION ||
1784 pim_hdr_type(pimhdr) != PIM_TYPE_HELLO)
1785 return;
1786
1787 br_multicast_mark_router(br, port);
1788 }
1789
br_multicast_ipv4_rcv(struct net_bridge * br,struct net_bridge_port * port,struct sk_buff * skb,u16 vid)1790 static int br_multicast_ipv4_rcv(struct net_bridge *br,
1791 struct net_bridge_port *port,
1792 struct sk_buff *skb,
1793 u16 vid)
1794 {
1795 struct sk_buff *skb_trimmed = NULL;
1796 const unsigned char *src;
1797 struct igmphdr *ih;
1798 int err;
1799
1800 err = ip_mc_check_igmp(skb, &skb_trimmed);
1801
1802 if (err == -ENOMSG) {
1803 if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) {
1804 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1805 } else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) {
1806 if (ip_hdr(skb)->protocol == IPPROTO_PIM)
1807 br_multicast_pim(br, port, skb);
1808 }
1809 return 0;
1810 } else if (err < 0) {
1811 br_multicast_err_count(br, port, skb->protocol);
1812 return err;
1813 }
1814
1815 ih = igmp_hdr(skb);
1816 src = eth_hdr(skb)->h_source;
1817 BR_INPUT_SKB_CB(skb)->igmp = ih->type;
1818
1819 switch (ih->type) {
1820 case IGMP_HOST_MEMBERSHIP_REPORT:
1821 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1822 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1823 err = br_ip4_multicast_add_group(br, port, ih->group, vid, src);
1824 break;
1825 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1826 err = br_ip4_multicast_igmp3_report(br, port, skb_trimmed, vid);
1827 break;
1828 case IGMP_HOST_MEMBERSHIP_QUERY:
1829 br_ip4_multicast_query(br, port, skb_trimmed, vid);
1830 break;
1831 case IGMP_HOST_LEAVE_MESSAGE:
1832 br_ip4_multicast_leave_group(br, port, ih->group, vid, src);
1833 break;
1834 }
1835
1836 if (skb_trimmed && skb_trimmed != skb)
1837 kfree_skb(skb_trimmed);
1838
1839 br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
1840 BR_MCAST_DIR_RX);
1841
1842 return err;
1843 }
1844
1845 #if IS_ENABLED(CONFIG_IPV6)
br_multicast_ipv6_rcv(struct net_bridge * br,struct net_bridge_port * port,struct sk_buff * skb,u16 vid)1846 static int br_multicast_ipv6_rcv(struct net_bridge *br,
1847 struct net_bridge_port *port,
1848 struct sk_buff *skb,
1849 u16 vid)
1850 {
1851 struct sk_buff *skb_trimmed = NULL;
1852 const unsigned char *src;
1853 struct mld_msg *mld;
1854 int err;
1855
1856 err = ipv6_mc_check_mld(skb, &skb_trimmed);
1857
1858 if (err == -ENOMSG) {
1859 if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
1860 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1861 return 0;
1862 } else if (err < 0) {
1863 br_multicast_err_count(br, port, skb->protocol);
1864 return err;
1865 }
1866
1867 mld = (struct mld_msg *)skb_transport_header(skb);
1868 BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type;
1869
1870 switch (mld->mld_type) {
1871 case ICMPV6_MGM_REPORT:
1872 src = eth_hdr(skb)->h_source;
1873 BR_INPUT_SKB_CB(skb)->mrouters_only = 1;
1874 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid,
1875 src);
1876 break;
1877 case ICMPV6_MLD2_REPORT:
1878 err = br_ip6_multicast_mld2_report(br, port, skb_trimmed, vid);
1879 break;
1880 case ICMPV6_MGM_QUERY:
1881 err = br_ip6_multicast_query(br, port, skb_trimmed, vid);
1882 break;
1883 case ICMPV6_MGM_REDUCTION:
1884 src = eth_hdr(skb)->h_source;
1885 br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src);
1886 break;
1887 }
1888
1889 if (skb_trimmed && skb_trimmed != skb)
1890 kfree_skb(skb_trimmed);
1891
1892 br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp,
1893 BR_MCAST_DIR_RX);
1894
1895 return err;
1896 }
1897 #endif
1898
br_multicast_rcv(struct net_bridge * br,struct net_bridge_port * port,struct sk_buff * skb,u16 vid)1899 int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1900 struct sk_buff *skb, u16 vid)
1901 {
1902 int ret = 0;
1903
1904 BR_INPUT_SKB_CB(skb)->igmp = 0;
1905 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
1906
1907 if (br->multicast_disabled)
1908 return 0;
1909
1910 switch (skb->protocol) {
1911 case htons(ETH_P_IP):
1912 ret = br_multicast_ipv4_rcv(br, port, skb, vid);
1913 break;
1914 #if IS_ENABLED(CONFIG_IPV6)
1915 case htons(ETH_P_IPV6):
1916 ret = br_multicast_ipv6_rcv(br, port, skb, vid);
1917 break;
1918 #endif
1919 }
1920
1921 return ret;
1922 }
1923
br_multicast_query_expired(struct net_bridge * br,struct bridge_mcast_own_query * query,struct bridge_mcast_querier * querier)1924 static void br_multicast_query_expired(struct net_bridge *br,
1925 struct bridge_mcast_own_query *query,
1926 struct bridge_mcast_querier *querier)
1927 {
1928 spin_lock(&br->multicast_lock);
1929 if (query->startup_sent < br->multicast_startup_query_count)
1930 query->startup_sent++;
1931
1932 RCU_INIT_POINTER(querier->port, NULL);
1933 br_multicast_send_query(br, NULL, query);
1934 spin_unlock(&br->multicast_lock);
1935 }
1936
br_ip4_multicast_query_expired(struct timer_list * t)1937 static void br_ip4_multicast_query_expired(struct timer_list *t)
1938 {
1939 struct net_bridge *br = from_timer(br, t, ip4_own_query.timer);
1940
1941 br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier);
1942 }
1943
1944 #if IS_ENABLED(CONFIG_IPV6)
br_ip6_multicast_query_expired(struct timer_list * t)1945 static void br_ip6_multicast_query_expired(struct timer_list *t)
1946 {
1947 struct net_bridge *br = from_timer(br, t, ip6_own_query.timer);
1948
1949 br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier);
1950 }
1951 #endif
1952
br_multicast_init(struct net_bridge * br)1953 void br_multicast_init(struct net_bridge *br)
1954 {
1955 br->hash_elasticity = 4;
1956 br->hash_max = 512;
1957
1958 br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
1959 br->multicast_querier = 0;
1960 br->multicast_query_use_ifaddr = 0;
1961 br->multicast_last_member_count = 2;
1962 br->multicast_startup_query_count = 2;
1963
1964 br->multicast_last_member_interval = HZ;
1965 br->multicast_query_response_interval = 10 * HZ;
1966 br->multicast_startup_query_interval = 125 * HZ / 4;
1967 br->multicast_query_interval = 125 * HZ;
1968 br->multicast_querier_interval = 255 * HZ;
1969 br->multicast_membership_interval = 260 * HZ;
1970
1971 br->ip4_other_query.delay_time = 0;
1972 br->ip4_querier.port = NULL;
1973 br->multicast_igmp_version = 2;
1974 #if IS_ENABLED(CONFIG_IPV6)
1975 br->multicast_mld_version = 1;
1976 br->ip6_other_query.delay_time = 0;
1977 br->ip6_querier.port = NULL;
1978 #endif
1979 br->has_ipv6_addr = 1;
1980
1981 spin_lock_init(&br->multicast_lock);
1982 timer_setup(&br->multicast_router_timer,
1983 br_multicast_local_router_expired, 0);
1984 timer_setup(&br->ip4_other_query.timer,
1985 br_ip4_multicast_querier_expired, 0);
1986 timer_setup(&br->ip4_own_query.timer,
1987 br_ip4_multicast_query_expired, 0);
1988 #if IS_ENABLED(CONFIG_IPV6)
1989 timer_setup(&br->ip6_other_query.timer,
1990 br_ip6_multicast_querier_expired, 0);
1991 timer_setup(&br->ip6_own_query.timer,
1992 br_ip6_multicast_query_expired, 0);
1993 #endif
1994 }
1995
__br_multicast_open(struct net_bridge * br,struct bridge_mcast_own_query * query)1996 static void __br_multicast_open(struct net_bridge *br,
1997 struct bridge_mcast_own_query *query)
1998 {
1999 query->startup_sent = 0;
2000
2001 if (br->multicast_disabled)
2002 return;
2003
2004 mod_timer(&query->timer, jiffies);
2005 }
2006
br_multicast_open(struct net_bridge * br)2007 void br_multicast_open(struct net_bridge *br)
2008 {
2009 __br_multicast_open(br, &br->ip4_own_query);
2010 #if IS_ENABLED(CONFIG_IPV6)
2011 __br_multicast_open(br, &br->ip6_own_query);
2012 #endif
2013 }
2014
br_multicast_stop(struct net_bridge * br)2015 void br_multicast_stop(struct net_bridge *br)
2016 {
2017 del_timer_sync(&br->multicast_router_timer);
2018 del_timer_sync(&br->ip4_other_query.timer);
2019 del_timer_sync(&br->ip4_own_query.timer);
2020 #if IS_ENABLED(CONFIG_IPV6)
2021 del_timer_sync(&br->ip6_other_query.timer);
2022 del_timer_sync(&br->ip6_own_query.timer);
2023 #endif
2024 }
2025
br_multicast_dev_del(struct net_bridge * br)2026 void br_multicast_dev_del(struct net_bridge *br)
2027 {
2028 struct net_bridge_mdb_htable *mdb;
2029 struct net_bridge_mdb_entry *mp;
2030 struct hlist_node *n;
2031 u32 ver;
2032 int i;
2033
2034 spin_lock_bh(&br->multicast_lock);
2035 mdb = mlock_dereference(br->mdb, br);
2036 if (!mdb)
2037 goto out;
2038
2039 br->mdb = NULL;
2040
2041 ver = mdb->ver;
2042 for (i = 0; i < mdb->max; i++) {
2043 hlist_for_each_entry_safe(mp, n, &mdb->mhash[i],
2044 hlist[ver]) {
2045 del_timer(&mp->timer);
2046 call_rcu_bh(&mp->rcu, br_multicast_free_group);
2047 }
2048 }
2049
2050 if (mdb->old) {
2051 spin_unlock_bh(&br->multicast_lock);
2052 rcu_barrier_bh();
2053 spin_lock_bh(&br->multicast_lock);
2054 WARN_ON(mdb->old);
2055 }
2056
2057 mdb->old = mdb;
2058 call_rcu_bh(&mdb->rcu, br_mdb_free);
2059
2060 out:
2061 spin_unlock_bh(&br->multicast_lock);
2062 }
2063
br_multicast_set_router(struct net_bridge * br,unsigned long val)2064 int br_multicast_set_router(struct net_bridge *br, unsigned long val)
2065 {
2066 int err = -EINVAL;
2067
2068 spin_lock_bh(&br->multicast_lock);
2069
2070 switch (val) {
2071 case MDB_RTR_TYPE_DISABLED:
2072 case MDB_RTR_TYPE_PERM:
2073 br_mc_router_state_change(br, val == MDB_RTR_TYPE_PERM);
2074 del_timer(&br->multicast_router_timer);
2075 br->multicast_router = val;
2076 err = 0;
2077 break;
2078 case MDB_RTR_TYPE_TEMP_QUERY:
2079 if (br->multicast_router != MDB_RTR_TYPE_TEMP_QUERY)
2080 br_mc_router_state_change(br, false);
2081 br->multicast_router = val;
2082 err = 0;
2083 break;
2084 }
2085
2086 spin_unlock_bh(&br->multicast_lock);
2087
2088 return err;
2089 }
2090
__del_port_router(struct net_bridge_port * p)2091 static void __del_port_router(struct net_bridge_port *p)
2092 {
2093 if (hlist_unhashed(&p->rlist))
2094 return;
2095 hlist_del_init_rcu(&p->rlist);
2096 br_rtr_notify(p->br->dev, p, RTM_DELMDB);
2097 br_port_mc_router_state_change(p, false);
2098
2099 /* don't allow timer refresh */
2100 if (p->multicast_router == MDB_RTR_TYPE_TEMP)
2101 p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
2102 }
2103
br_multicast_set_port_router(struct net_bridge_port * p,unsigned long val)2104 int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val)
2105 {
2106 struct net_bridge *br = p->br;
2107 unsigned long now = jiffies;
2108 int err = -EINVAL;
2109
2110 spin_lock(&br->multicast_lock);
2111 if (p->multicast_router == val) {
2112 /* Refresh the temp router port timer */
2113 if (p->multicast_router == MDB_RTR_TYPE_TEMP)
2114 mod_timer(&p->multicast_router_timer,
2115 now + br->multicast_querier_interval);
2116 err = 0;
2117 goto unlock;
2118 }
2119 switch (val) {
2120 case MDB_RTR_TYPE_DISABLED:
2121 p->multicast_router = MDB_RTR_TYPE_DISABLED;
2122 __del_port_router(p);
2123 del_timer(&p->multicast_router_timer);
2124 break;
2125 case MDB_RTR_TYPE_TEMP_QUERY:
2126 p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY;
2127 __del_port_router(p);
2128 break;
2129 case MDB_RTR_TYPE_PERM:
2130 p->multicast_router = MDB_RTR_TYPE_PERM;
2131 del_timer(&p->multicast_router_timer);
2132 br_multicast_add_router(br, p);
2133 break;
2134 case MDB_RTR_TYPE_TEMP:
2135 p->multicast_router = MDB_RTR_TYPE_TEMP;
2136 br_multicast_mark_router(br, p);
2137 break;
2138 default:
2139 goto unlock;
2140 }
2141 err = 0;
2142 unlock:
2143 spin_unlock(&br->multicast_lock);
2144
2145 return err;
2146 }
2147
br_multicast_start_querier(struct net_bridge * br,struct bridge_mcast_own_query * query)2148 static void br_multicast_start_querier(struct net_bridge *br,
2149 struct bridge_mcast_own_query *query)
2150 {
2151 struct net_bridge_port *port;
2152
2153 __br_multicast_open(br, query);
2154
2155 list_for_each_entry(port, &br->port_list, list) {
2156 if (port->state == BR_STATE_DISABLED ||
2157 port->state == BR_STATE_BLOCKING)
2158 continue;
2159
2160 if (query == &br->ip4_own_query)
2161 br_multicast_enable(&port->ip4_own_query);
2162 #if IS_ENABLED(CONFIG_IPV6)
2163 else
2164 br_multicast_enable(&port->ip6_own_query);
2165 #endif
2166 }
2167 }
2168
br_multicast_toggle(struct net_bridge * br,unsigned long val)2169 int br_multicast_toggle(struct net_bridge *br, unsigned long val)
2170 {
2171 struct net_bridge_mdb_htable *mdb;
2172 struct net_bridge_port *port;
2173 int err = 0;
2174
2175 spin_lock_bh(&br->multicast_lock);
2176 if (br->multicast_disabled == !val)
2177 goto unlock;
2178
2179 br_mc_disabled_update(br->dev, !val);
2180 br->multicast_disabled = !val;
2181 if (br->multicast_disabled)
2182 goto unlock;
2183
2184 if (!netif_running(br->dev))
2185 goto unlock;
2186
2187 mdb = mlock_dereference(br->mdb, br);
2188 if (mdb) {
2189 if (mdb->old) {
2190 err = -EEXIST;
2191 rollback:
2192 br->multicast_disabled = !!val;
2193 goto unlock;
2194 }
2195
2196 err = br_mdb_rehash(&br->mdb, mdb->max,
2197 br->hash_elasticity);
2198 if (err)
2199 goto rollback;
2200 }
2201
2202 br_multicast_open(br);
2203 list_for_each_entry(port, &br->port_list, list)
2204 __br_multicast_enable_port(port);
2205
2206 unlock:
2207 spin_unlock_bh(&br->multicast_lock);
2208
2209 return err;
2210 }
2211
br_multicast_enabled(const struct net_device * dev)2212 bool br_multicast_enabled(const struct net_device *dev)
2213 {
2214 struct net_bridge *br = netdev_priv(dev);
2215
2216 return !br->multicast_disabled;
2217 }
2218 EXPORT_SYMBOL_GPL(br_multicast_enabled);
2219
br_multicast_router(const struct net_device * dev)2220 bool br_multicast_router(const struct net_device *dev)
2221 {
2222 struct net_bridge *br = netdev_priv(dev);
2223 bool is_router;
2224
2225 spin_lock_bh(&br->multicast_lock);
2226 is_router = br_multicast_is_router(br);
2227 spin_unlock_bh(&br->multicast_lock);
2228 return is_router;
2229 }
2230 EXPORT_SYMBOL_GPL(br_multicast_router);
2231
br_multicast_set_querier(struct net_bridge * br,unsigned long val)2232 int br_multicast_set_querier(struct net_bridge *br, unsigned long val)
2233 {
2234 unsigned long max_delay;
2235
2236 val = !!val;
2237
2238 spin_lock_bh(&br->multicast_lock);
2239 if (br->multicast_querier == val)
2240 goto unlock;
2241
2242 br->multicast_querier = val;
2243 if (!val)
2244 goto unlock;
2245
2246 max_delay = br->multicast_query_response_interval;
2247
2248 if (!timer_pending(&br->ip4_other_query.timer))
2249 br->ip4_other_query.delay_time = jiffies + max_delay;
2250
2251 br_multicast_start_querier(br, &br->ip4_own_query);
2252
2253 #if IS_ENABLED(CONFIG_IPV6)
2254 if (!timer_pending(&br->ip6_other_query.timer))
2255 br->ip6_other_query.delay_time = jiffies + max_delay;
2256
2257 br_multicast_start_querier(br, &br->ip6_own_query);
2258 #endif
2259
2260 unlock:
2261 spin_unlock_bh(&br->multicast_lock);
2262
2263 return 0;
2264 }
2265
br_multicast_set_hash_max(struct net_bridge * br,unsigned long val)2266 int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
2267 {
2268 int err = -EINVAL;
2269 u32 old;
2270 struct net_bridge_mdb_htable *mdb;
2271
2272 spin_lock_bh(&br->multicast_lock);
2273 if (!is_power_of_2(val))
2274 goto unlock;
2275
2276 mdb = mlock_dereference(br->mdb, br);
2277 if (mdb && val < mdb->size)
2278 goto unlock;
2279
2280 err = 0;
2281
2282 old = br->hash_max;
2283 br->hash_max = val;
2284
2285 if (mdb) {
2286 if (mdb->old) {
2287 err = -EEXIST;
2288 rollback:
2289 br->hash_max = old;
2290 goto unlock;
2291 }
2292
2293 err = br_mdb_rehash(&br->mdb, br->hash_max,
2294 br->hash_elasticity);
2295 if (err)
2296 goto rollback;
2297 }
2298
2299 unlock:
2300 spin_unlock_bh(&br->multicast_lock);
2301
2302 return err;
2303 }
2304
br_multicast_set_igmp_version(struct net_bridge * br,unsigned long val)2305 int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val)
2306 {
2307 /* Currently we support only version 2 and 3 */
2308 switch (val) {
2309 case 2:
2310 case 3:
2311 break;
2312 default:
2313 return -EINVAL;
2314 }
2315
2316 spin_lock_bh(&br->multicast_lock);
2317 br->multicast_igmp_version = val;
2318 spin_unlock_bh(&br->multicast_lock);
2319
2320 return 0;
2321 }
2322
2323 #if IS_ENABLED(CONFIG_IPV6)
br_multicast_set_mld_version(struct net_bridge * br,unsigned long val)2324 int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val)
2325 {
2326 /* Currently we support version 1 and 2 */
2327 switch (val) {
2328 case 1:
2329 case 2:
2330 break;
2331 default:
2332 return -EINVAL;
2333 }
2334
2335 spin_lock_bh(&br->multicast_lock);
2336 br->multicast_mld_version = val;
2337 spin_unlock_bh(&br->multicast_lock);
2338
2339 return 0;
2340 }
2341 #endif
2342
2343 /**
2344 * br_multicast_list_adjacent - Returns snooped multicast addresses
2345 * @dev: The bridge port adjacent to which to retrieve addresses
2346 * @br_ip_list: The list to store found, snooped multicast IP addresses in
2347 *
2348 * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast
2349 * snooping feature on all bridge ports of dev's bridge device, excluding
2350 * the addresses from dev itself.
2351 *
2352 * Returns the number of items added to br_ip_list.
2353 *
2354 * Notes:
2355 * - br_ip_list needs to be initialized by caller
2356 * - br_ip_list might contain duplicates in the end
2357 * (needs to be taken care of by caller)
2358 * - br_ip_list needs to be freed by caller
2359 */
br_multicast_list_adjacent(struct net_device * dev,struct list_head * br_ip_list)2360 int br_multicast_list_adjacent(struct net_device *dev,
2361 struct list_head *br_ip_list)
2362 {
2363 struct net_bridge *br;
2364 struct net_bridge_port *port;
2365 struct net_bridge_port_group *group;
2366 struct br_ip_list *entry;
2367 int count = 0;
2368
2369 rcu_read_lock();
2370 if (!br_ip_list || !br_port_exists(dev))
2371 goto unlock;
2372
2373 port = br_port_get_rcu(dev);
2374 if (!port || !port->br)
2375 goto unlock;
2376
2377 br = port->br;
2378
2379 list_for_each_entry_rcu(port, &br->port_list, list) {
2380 if (!port->dev || port->dev == dev)
2381 continue;
2382
2383 hlist_for_each_entry_rcu(group, &port->mglist, mglist) {
2384 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
2385 if (!entry)
2386 goto unlock;
2387
2388 entry->addr = group->addr;
2389 list_add(&entry->list, br_ip_list);
2390 count++;
2391 }
2392 }
2393
2394 unlock:
2395 rcu_read_unlock();
2396 return count;
2397 }
2398 EXPORT_SYMBOL_GPL(br_multicast_list_adjacent);
2399
2400 /**
2401 * br_multicast_has_querier_anywhere - Checks for a querier on a bridge
2402 * @dev: The bridge port providing the bridge on which to check for a querier
2403 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2404 *
2405 * Checks whether the given interface has a bridge on top and if so returns
2406 * true if a valid querier exists anywhere on the bridged link layer.
2407 * Otherwise returns false.
2408 */
br_multicast_has_querier_anywhere(struct net_device * dev,int proto)2409 bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto)
2410 {
2411 struct net_bridge *br;
2412 struct net_bridge_port *port;
2413 struct ethhdr eth;
2414 bool ret = false;
2415
2416 rcu_read_lock();
2417 if (!br_port_exists(dev))
2418 goto unlock;
2419
2420 port = br_port_get_rcu(dev);
2421 if (!port || !port->br)
2422 goto unlock;
2423
2424 br = port->br;
2425
2426 memset(ð, 0, sizeof(eth));
2427 eth.h_proto = htons(proto);
2428
2429 ret = br_multicast_querier_exists(br, ð);
2430
2431 unlock:
2432 rcu_read_unlock();
2433 return ret;
2434 }
2435 EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere);
2436
2437 /**
2438 * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port
2439 * @dev: The bridge port adjacent to which to check for a querier
2440 * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6
2441 *
2442 * Checks whether the given interface has a bridge on top and if so returns
2443 * true if a selected querier is behind one of the other ports of this
2444 * bridge. Otherwise returns false.
2445 */
br_multicast_has_querier_adjacent(struct net_device * dev,int proto)2446 bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto)
2447 {
2448 struct net_bridge *br;
2449 struct net_bridge_port *port;
2450 bool ret = false;
2451
2452 rcu_read_lock();
2453 if (!br_port_exists(dev))
2454 goto unlock;
2455
2456 port = br_port_get_rcu(dev);
2457 if (!port || !port->br)
2458 goto unlock;
2459
2460 br = port->br;
2461
2462 switch (proto) {
2463 case ETH_P_IP:
2464 if (!timer_pending(&br->ip4_other_query.timer) ||
2465 rcu_dereference(br->ip4_querier.port) == port)
2466 goto unlock;
2467 break;
2468 #if IS_ENABLED(CONFIG_IPV6)
2469 case ETH_P_IPV6:
2470 if (!timer_pending(&br->ip6_other_query.timer) ||
2471 rcu_dereference(br->ip6_querier.port) == port)
2472 goto unlock;
2473 break;
2474 #endif
2475 default:
2476 goto unlock;
2477 }
2478
2479 ret = true;
2480 unlock:
2481 rcu_read_unlock();
2482 return ret;
2483 }
2484 EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent);
2485
br_mcast_stats_add(struct bridge_mcast_stats __percpu * stats,const struct sk_buff * skb,u8 type,u8 dir)2486 static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats,
2487 const struct sk_buff *skb, u8 type, u8 dir)
2488 {
2489 struct bridge_mcast_stats *pstats = this_cpu_ptr(stats);
2490 __be16 proto = skb->protocol;
2491 unsigned int t_len;
2492
2493 u64_stats_update_begin(&pstats->syncp);
2494 switch (proto) {
2495 case htons(ETH_P_IP):
2496 t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
2497 switch (type) {
2498 case IGMP_HOST_MEMBERSHIP_REPORT:
2499 pstats->mstats.igmp_v1reports[dir]++;
2500 break;
2501 case IGMPV2_HOST_MEMBERSHIP_REPORT:
2502 pstats->mstats.igmp_v2reports[dir]++;
2503 break;
2504 case IGMPV3_HOST_MEMBERSHIP_REPORT:
2505 pstats->mstats.igmp_v3reports[dir]++;
2506 break;
2507 case IGMP_HOST_MEMBERSHIP_QUERY:
2508 if (t_len != sizeof(struct igmphdr)) {
2509 pstats->mstats.igmp_v3queries[dir]++;
2510 } else {
2511 unsigned int offset = skb_transport_offset(skb);
2512 struct igmphdr *ih, _ihdr;
2513
2514 ih = skb_header_pointer(skb, offset,
2515 sizeof(_ihdr), &_ihdr);
2516 if (!ih)
2517 break;
2518 if (!ih->code)
2519 pstats->mstats.igmp_v1queries[dir]++;
2520 else
2521 pstats->mstats.igmp_v2queries[dir]++;
2522 }
2523 break;
2524 case IGMP_HOST_LEAVE_MESSAGE:
2525 pstats->mstats.igmp_leaves[dir]++;
2526 break;
2527 }
2528 break;
2529 #if IS_ENABLED(CONFIG_IPV6)
2530 case htons(ETH_P_IPV6):
2531 t_len = ntohs(ipv6_hdr(skb)->payload_len) +
2532 sizeof(struct ipv6hdr);
2533 t_len -= skb_network_header_len(skb);
2534 switch (type) {
2535 case ICMPV6_MGM_REPORT:
2536 pstats->mstats.mld_v1reports[dir]++;
2537 break;
2538 case ICMPV6_MLD2_REPORT:
2539 pstats->mstats.mld_v2reports[dir]++;
2540 break;
2541 case ICMPV6_MGM_QUERY:
2542 if (t_len != sizeof(struct mld_msg))
2543 pstats->mstats.mld_v2queries[dir]++;
2544 else
2545 pstats->mstats.mld_v1queries[dir]++;
2546 break;
2547 case ICMPV6_MGM_REDUCTION:
2548 pstats->mstats.mld_leaves[dir]++;
2549 break;
2550 }
2551 break;
2552 #endif /* CONFIG_IPV6 */
2553 }
2554 u64_stats_update_end(&pstats->syncp);
2555 }
2556
br_multicast_count(struct net_bridge * br,const struct net_bridge_port * p,const struct sk_buff * skb,u8 type,u8 dir)2557 void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
2558 const struct sk_buff *skb, u8 type, u8 dir)
2559 {
2560 struct bridge_mcast_stats __percpu *stats;
2561
2562 /* if multicast_disabled is true then igmp type can't be set */
2563 if (!type || !br->multicast_stats_enabled)
2564 return;
2565
2566 if (p)
2567 stats = p->mcast_stats;
2568 else
2569 stats = br->mcast_stats;
2570 if (WARN_ON(!stats))
2571 return;
2572
2573 br_mcast_stats_add(stats, skb, type, dir);
2574 }
2575
br_multicast_init_stats(struct net_bridge * br)2576 int br_multicast_init_stats(struct net_bridge *br)
2577 {
2578 br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats);
2579 if (!br->mcast_stats)
2580 return -ENOMEM;
2581
2582 return 0;
2583 }
2584
br_multicast_uninit_stats(struct net_bridge * br)2585 void br_multicast_uninit_stats(struct net_bridge *br)
2586 {
2587 free_percpu(br->mcast_stats);
2588 }
2589
mcast_stats_add_dir(u64 * dst,u64 * src)2590 static void mcast_stats_add_dir(u64 *dst, u64 *src)
2591 {
2592 dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX];
2593 dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX];
2594 }
2595
br_multicast_get_stats(const struct net_bridge * br,const struct net_bridge_port * p,struct br_mcast_stats * dest)2596 void br_multicast_get_stats(const struct net_bridge *br,
2597 const struct net_bridge_port *p,
2598 struct br_mcast_stats *dest)
2599 {
2600 struct bridge_mcast_stats __percpu *stats;
2601 struct br_mcast_stats tdst;
2602 int i;
2603
2604 memset(dest, 0, sizeof(*dest));
2605 if (p)
2606 stats = p->mcast_stats;
2607 else
2608 stats = br->mcast_stats;
2609 if (WARN_ON(!stats))
2610 return;
2611
2612 memset(&tdst, 0, sizeof(tdst));
2613 for_each_possible_cpu(i) {
2614 struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i);
2615 struct br_mcast_stats temp;
2616 unsigned int start;
2617
2618 do {
2619 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
2620 memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
2621 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
2622
2623 mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
2624 mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries);
2625 mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries);
2626 mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves);
2627 mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports);
2628 mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports);
2629 mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports);
2630 tdst.igmp_parse_errors += temp.igmp_parse_errors;
2631
2632 mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries);
2633 mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries);
2634 mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves);
2635 mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports);
2636 mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports);
2637 tdst.mld_parse_errors += temp.mld_parse_errors;
2638 }
2639 memcpy(dest, &tdst, sizeof(*dest));
2640 }
2641