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