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