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/if_ether.h> 15 #include <linux/igmp.h> 16 #include <linux/jhash.h> 17 #include <linux/kernel.h> 18 #include <linux/log2.h> 19 #include <linux/netdevice.h> 20 #include <linux/netfilter_bridge.h> 21 #include <linux/random.h> 22 #include <linux/rculist.h> 23 #include <linux/skbuff.h> 24 #include <linux/slab.h> 25 #include <linux/timer.h> 26 #include <net/ip.h> 27 #if IS_ENABLED(CONFIG_IPV6) 28 #include <net/ipv6.h> 29 #include <net/mld.h> 30 #include <net/ip6_checksum.h> 31 #endif 32 33 #include "br_private.h" 34 35 static void br_multicast_start_querier(struct net_bridge *br); 36 unsigned int br_mdb_rehash_seq; 37 38 static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b) 39 { 40 if (a->proto != b->proto) 41 return 0; 42 switch (a->proto) { 43 case htons(ETH_P_IP): 44 return a->u.ip4 == b->u.ip4; 45 #if IS_ENABLED(CONFIG_IPV6) 46 case htons(ETH_P_IPV6): 47 return ipv6_addr_equal(&a->u.ip6, &b->u.ip6); 48 #endif 49 } 50 return 0; 51 } 52 53 static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip) 54 { 55 return jhash_1word(mdb->secret, (__force u32)ip) & (mdb->max - 1); 56 } 57 58 #if IS_ENABLED(CONFIG_IPV6) 59 static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb, 60 const struct in6_addr *ip) 61 { 62 return jhash2((__force u32 *)ip->s6_addr32, 4, mdb->secret) & (mdb->max - 1); 63 } 64 #endif 65 66 static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb, 67 struct br_ip *ip) 68 { 69 switch (ip->proto) { 70 case htons(ETH_P_IP): 71 return __br_ip4_hash(mdb, ip->u.ip4); 72 #if IS_ENABLED(CONFIG_IPV6) 73 case htons(ETH_P_IPV6): 74 return __br_ip6_hash(mdb, &ip->u.ip6); 75 #endif 76 } 77 return 0; 78 } 79 80 static struct net_bridge_mdb_entry *__br_mdb_ip_get( 81 struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash) 82 { 83 struct net_bridge_mdb_entry *mp; 84 struct hlist_node *p; 85 86 hlist_for_each_entry_rcu(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) { 87 if (br_ip_equal(&mp->addr, dst)) 88 return mp; 89 } 90 91 return NULL; 92 } 93 94 struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge_mdb_htable *mdb, 95 struct br_ip *dst) 96 { 97 if (!mdb) 98 return NULL; 99 100 return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst)); 101 } 102 103 static struct net_bridge_mdb_entry *br_mdb_ip4_get( 104 struct net_bridge_mdb_htable *mdb, __be32 dst) 105 { 106 struct br_ip br_dst; 107 108 br_dst.u.ip4 = dst; 109 br_dst.proto = htons(ETH_P_IP); 110 111 return br_mdb_ip_get(mdb, &br_dst); 112 } 113 114 #if IS_ENABLED(CONFIG_IPV6) 115 static struct net_bridge_mdb_entry *br_mdb_ip6_get( 116 struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst) 117 { 118 struct br_ip br_dst; 119 120 br_dst.u.ip6 = *dst; 121 br_dst.proto = htons(ETH_P_IPV6); 122 123 return br_mdb_ip_get(mdb, &br_dst); 124 } 125 #endif 126 127 struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br, 128 struct sk_buff *skb) 129 { 130 struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb); 131 struct br_ip ip; 132 133 if (br->multicast_disabled) 134 return NULL; 135 136 if (BR_INPUT_SKB_CB(skb)->igmp) 137 return NULL; 138 139 ip.proto = skb->protocol; 140 141 switch (skb->protocol) { 142 case htons(ETH_P_IP): 143 ip.u.ip4 = ip_hdr(skb)->daddr; 144 break; 145 #if IS_ENABLED(CONFIG_IPV6) 146 case htons(ETH_P_IPV6): 147 ip.u.ip6 = ipv6_hdr(skb)->daddr; 148 break; 149 #endif 150 default: 151 return NULL; 152 } 153 154 return br_mdb_ip_get(mdb, &ip); 155 } 156 157 static void br_mdb_free(struct rcu_head *head) 158 { 159 struct net_bridge_mdb_htable *mdb = 160 container_of(head, struct net_bridge_mdb_htable, rcu); 161 struct net_bridge_mdb_htable *old = mdb->old; 162 163 mdb->old = NULL; 164 kfree(old->mhash); 165 kfree(old); 166 } 167 168 static int br_mdb_copy(struct net_bridge_mdb_htable *new, 169 struct net_bridge_mdb_htable *old, 170 int elasticity) 171 { 172 struct net_bridge_mdb_entry *mp; 173 struct hlist_node *p; 174 int maxlen; 175 int len; 176 int i; 177 178 for (i = 0; i < old->max; i++) 179 hlist_for_each_entry(mp, p, &old->mhash[i], hlist[old->ver]) 180 hlist_add_head(&mp->hlist[new->ver], 181 &new->mhash[br_ip_hash(new, &mp->addr)]); 182 183 if (!elasticity) 184 return 0; 185 186 maxlen = 0; 187 for (i = 0; i < new->max; i++) { 188 len = 0; 189 hlist_for_each_entry(mp, p, &new->mhash[i], hlist[new->ver]) 190 len++; 191 if (len > maxlen) 192 maxlen = len; 193 } 194 195 return maxlen > elasticity ? -EINVAL : 0; 196 } 197 198 void br_multicast_free_pg(struct rcu_head *head) 199 { 200 struct net_bridge_port_group *p = 201 container_of(head, struct net_bridge_port_group, rcu); 202 203 kfree(p); 204 } 205 206 static void br_multicast_free_group(struct rcu_head *head) 207 { 208 struct net_bridge_mdb_entry *mp = 209 container_of(head, struct net_bridge_mdb_entry, rcu); 210 211 kfree(mp); 212 } 213 214 static void br_multicast_group_expired(unsigned long data) 215 { 216 struct net_bridge_mdb_entry *mp = (void *)data; 217 struct net_bridge *br = mp->br; 218 struct net_bridge_mdb_htable *mdb; 219 220 spin_lock(&br->multicast_lock); 221 if (!netif_running(br->dev) || timer_pending(&mp->timer)) 222 goto out; 223 224 mp->mglist = false; 225 226 if (mp->ports) 227 goto out; 228 229 mdb = mlock_dereference(br->mdb, br); 230 231 hlist_del_rcu(&mp->hlist[mdb->ver]); 232 mdb->size--; 233 234 call_rcu_bh(&mp->rcu, br_multicast_free_group); 235 236 out: 237 spin_unlock(&br->multicast_lock); 238 } 239 240 static void br_multicast_del_pg(struct net_bridge *br, 241 struct net_bridge_port_group *pg) 242 { 243 struct net_bridge_mdb_htable *mdb; 244 struct net_bridge_mdb_entry *mp; 245 struct net_bridge_port_group *p; 246 struct net_bridge_port_group __rcu **pp; 247 248 mdb = mlock_dereference(br->mdb, br); 249 250 mp = br_mdb_ip_get(mdb, &pg->addr); 251 if (WARN_ON(!mp)) 252 return; 253 254 for (pp = &mp->ports; 255 (p = mlock_dereference(*pp, br)) != NULL; 256 pp = &p->next) { 257 if (p != pg) 258 continue; 259 260 rcu_assign_pointer(*pp, p->next); 261 hlist_del_init(&p->mglist); 262 del_timer(&p->timer); 263 call_rcu_bh(&p->rcu, br_multicast_free_pg); 264 265 if (!mp->ports && !mp->mglist && 266 netif_running(br->dev)) 267 mod_timer(&mp->timer, jiffies); 268 269 return; 270 } 271 272 WARN_ON(1); 273 } 274 275 static void br_multicast_port_group_expired(unsigned long data) 276 { 277 struct net_bridge_port_group *pg = (void *)data; 278 struct net_bridge *br = pg->port->br; 279 280 spin_lock(&br->multicast_lock); 281 if (!netif_running(br->dev) || timer_pending(&pg->timer) || 282 hlist_unhashed(&pg->mglist) || pg->state & MDB_PERMANENT) 283 goto out; 284 285 br_multicast_del_pg(br, pg); 286 287 out: 288 spin_unlock(&br->multicast_lock); 289 } 290 291 static int br_mdb_rehash(struct net_bridge_mdb_htable __rcu **mdbp, int max, 292 int elasticity) 293 { 294 struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1); 295 struct net_bridge_mdb_htable *mdb; 296 int err; 297 298 mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC); 299 if (!mdb) 300 return -ENOMEM; 301 302 mdb->max = max; 303 mdb->old = old; 304 305 mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC); 306 if (!mdb->mhash) { 307 kfree(mdb); 308 return -ENOMEM; 309 } 310 311 mdb->size = old ? old->size : 0; 312 mdb->ver = old ? old->ver ^ 1 : 0; 313 314 if (!old || elasticity) 315 get_random_bytes(&mdb->secret, sizeof(mdb->secret)); 316 else 317 mdb->secret = old->secret; 318 319 if (!old) 320 goto out; 321 322 err = br_mdb_copy(mdb, old, elasticity); 323 if (err) { 324 kfree(mdb->mhash); 325 kfree(mdb); 326 return err; 327 } 328 329 br_mdb_rehash_seq++; 330 call_rcu_bh(&mdb->rcu, br_mdb_free); 331 332 out: 333 rcu_assign_pointer(*mdbp, mdb); 334 335 return 0; 336 } 337 338 static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br, 339 __be32 group) 340 { 341 struct sk_buff *skb; 342 struct igmphdr *ih; 343 struct ethhdr *eth; 344 struct iphdr *iph; 345 346 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) + 347 sizeof(*ih) + 4); 348 if (!skb) 349 goto out; 350 351 skb->protocol = htons(ETH_P_IP); 352 353 skb_reset_mac_header(skb); 354 eth = eth_hdr(skb); 355 356 memcpy(eth->h_source, br->dev->dev_addr, 6); 357 eth->h_dest[0] = 1; 358 eth->h_dest[1] = 0; 359 eth->h_dest[2] = 0x5e; 360 eth->h_dest[3] = 0; 361 eth->h_dest[4] = 0; 362 eth->h_dest[5] = 1; 363 eth->h_proto = htons(ETH_P_IP); 364 skb_put(skb, sizeof(*eth)); 365 366 skb_set_network_header(skb, skb->len); 367 iph = ip_hdr(skb); 368 369 iph->version = 4; 370 iph->ihl = 6; 371 iph->tos = 0xc0; 372 iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4); 373 iph->id = 0; 374 iph->frag_off = htons(IP_DF); 375 iph->ttl = 1; 376 iph->protocol = IPPROTO_IGMP; 377 iph->saddr = 0; 378 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP); 379 ((u8 *)&iph[1])[0] = IPOPT_RA; 380 ((u8 *)&iph[1])[1] = 4; 381 ((u8 *)&iph[1])[2] = 0; 382 ((u8 *)&iph[1])[3] = 0; 383 ip_send_check(iph); 384 skb_put(skb, 24); 385 386 skb_set_transport_header(skb, skb->len); 387 ih = igmp_hdr(skb); 388 ih->type = IGMP_HOST_MEMBERSHIP_QUERY; 389 ih->code = (group ? br->multicast_last_member_interval : 390 br->multicast_query_response_interval) / 391 (HZ / IGMP_TIMER_SCALE); 392 ih->group = group; 393 ih->csum = 0; 394 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr)); 395 skb_put(skb, sizeof(*ih)); 396 397 __skb_pull(skb, sizeof(*eth)); 398 399 out: 400 return skb; 401 } 402 403 #if IS_ENABLED(CONFIG_IPV6) 404 static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br, 405 const struct in6_addr *group) 406 { 407 struct sk_buff *skb; 408 struct ipv6hdr *ip6h; 409 struct mld_msg *mldq; 410 struct ethhdr *eth; 411 u8 *hopopt; 412 unsigned long interval; 413 414 skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) + 415 8 + sizeof(*mldq)); 416 if (!skb) 417 goto out; 418 419 skb->protocol = htons(ETH_P_IPV6); 420 421 /* Ethernet header */ 422 skb_reset_mac_header(skb); 423 eth = eth_hdr(skb); 424 425 memcpy(eth->h_source, br->dev->dev_addr, 6); 426 eth->h_proto = htons(ETH_P_IPV6); 427 skb_put(skb, sizeof(*eth)); 428 429 /* IPv6 header + HbH option */ 430 skb_set_network_header(skb, skb->len); 431 ip6h = ipv6_hdr(skb); 432 433 *(__force __be32 *)ip6h = htonl(0x60000000); 434 ip6h->payload_len = htons(8 + sizeof(*mldq)); 435 ip6h->nexthdr = IPPROTO_HOPOPTS; 436 ip6h->hop_limit = 1; 437 ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1)); 438 if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0, 439 &ip6h->saddr)) { 440 kfree_skb(skb); 441 return NULL; 442 } 443 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest); 444 445 hopopt = (u8 *)(ip6h + 1); 446 hopopt[0] = IPPROTO_ICMPV6; /* next hdr */ 447 hopopt[1] = 0; /* length of HbH */ 448 hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */ 449 hopopt[3] = 2; /* Length of RA Option */ 450 hopopt[4] = 0; /* Type = 0x0000 (MLD) */ 451 hopopt[5] = 0; 452 hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */ 453 hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */ 454 455 skb_put(skb, sizeof(*ip6h) + 8); 456 457 /* ICMPv6 */ 458 skb_set_transport_header(skb, skb->len); 459 mldq = (struct mld_msg *) icmp6_hdr(skb); 460 461 interval = ipv6_addr_any(group) ? br->multicast_last_member_interval : 462 br->multicast_query_response_interval; 463 464 mldq->mld_type = ICMPV6_MGM_QUERY; 465 mldq->mld_code = 0; 466 mldq->mld_cksum = 0; 467 mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval)); 468 mldq->mld_reserved = 0; 469 mldq->mld_mca = *group; 470 471 /* checksum */ 472 mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, 473 sizeof(*mldq), IPPROTO_ICMPV6, 474 csum_partial(mldq, 475 sizeof(*mldq), 0)); 476 skb_put(skb, sizeof(*mldq)); 477 478 __skb_pull(skb, sizeof(*eth)); 479 480 out: 481 return skb; 482 } 483 #endif 484 485 static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br, 486 struct br_ip *addr) 487 { 488 switch (addr->proto) { 489 case htons(ETH_P_IP): 490 return br_ip4_multicast_alloc_query(br, addr->u.ip4); 491 #if IS_ENABLED(CONFIG_IPV6) 492 case htons(ETH_P_IPV6): 493 return br_ip6_multicast_alloc_query(br, &addr->u.ip6); 494 #endif 495 } 496 return NULL; 497 } 498 499 static struct net_bridge_mdb_entry *br_multicast_get_group( 500 struct net_bridge *br, struct net_bridge_port *port, 501 struct br_ip *group, int hash) 502 { 503 struct net_bridge_mdb_htable *mdb; 504 struct net_bridge_mdb_entry *mp; 505 struct hlist_node *p; 506 unsigned int count = 0; 507 unsigned int max; 508 int elasticity; 509 int err; 510 511 mdb = rcu_dereference_protected(br->mdb, 1); 512 hlist_for_each_entry(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) { 513 count++; 514 if (unlikely(br_ip_equal(group, &mp->addr))) 515 return mp; 516 } 517 518 elasticity = 0; 519 max = mdb->max; 520 521 if (unlikely(count > br->hash_elasticity && count)) { 522 if (net_ratelimit()) 523 br_info(br, "Multicast hash table " 524 "chain limit reached: %s\n", 525 port ? port->dev->name : br->dev->name); 526 527 elasticity = br->hash_elasticity; 528 } 529 530 if (mdb->size >= max) { 531 max *= 2; 532 if (unlikely(max > br->hash_max)) { 533 br_warn(br, "Multicast hash table maximum of %d " 534 "reached, disabling snooping: %s\n", 535 br->hash_max, 536 port ? port->dev->name : br->dev->name); 537 err = -E2BIG; 538 disable: 539 br->multicast_disabled = 1; 540 goto err; 541 } 542 } 543 544 if (max > mdb->max || elasticity) { 545 if (mdb->old) { 546 if (net_ratelimit()) 547 br_info(br, "Multicast hash table " 548 "on fire: %s\n", 549 port ? port->dev->name : br->dev->name); 550 err = -EEXIST; 551 goto err; 552 } 553 554 err = br_mdb_rehash(&br->mdb, max, elasticity); 555 if (err) { 556 br_warn(br, "Cannot rehash multicast " 557 "hash table, disabling snooping: %s, %d, %d\n", 558 port ? port->dev->name : br->dev->name, 559 mdb->size, err); 560 goto disable; 561 } 562 563 err = -EAGAIN; 564 goto err; 565 } 566 567 return NULL; 568 569 err: 570 mp = ERR_PTR(err); 571 return mp; 572 } 573 574 struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br, 575 struct net_bridge_port *port, struct br_ip *group) 576 { 577 struct net_bridge_mdb_htable *mdb; 578 struct net_bridge_mdb_entry *mp; 579 int hash; 580 int err; 581 582 mdb = rcu_dereference_protected(br->mdb, 1); 583 if (!mdb) { 584 err = br_mdb_rehash(&br->mdb, BR_HASH_SIZE, 0); 585 if (err) 586 return ERR_PTR(err); 587 goto rehash; 588 } 589 590 hash = br_ip_hash(mdb, group); 591 mp = br_multicast_get_group(br, port, group, hash); 592 switch (PTR_ERR(mp)) { 593 case 0: 594 break; 595 596 case -EAGAIN: 597 rehash: 598 mdb = rcu_dereference_protected(br->mdb, 1); 599 hash = br_ip_hash(mdb, group); 600 break; 601 602 default: 603 goto out; 604 } 605 606 mp = kzalloc(sizeof(*mp), GFP_ATOMIC); 607 if (unlikely(!mp)) 608 return ERR_PTR(-ENOMEM); 609 610 mp->br = br; 611 mp->addr = *group; 612 setup_timer(&mp->timer, br_multicast_group_expired, 613 (unsigned long)mp); 614 615 hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]); 616 mdb->size++; 617 618 out: 619 return mp; 620 } 621 622 struct net_bridge_port_group *br_multicast_new_port_group( 623 struct net_bridge_port *port, 624 struct br_ip *group, 625 struct net_bridge_port_group __rcu *next, 626 unsigned char state) 627 { 628 struct net_bridge_port_group *p; 629 630 p = kzalloc(sizeof(*p), GFP_ATOMIC); 631 if (unlikely(!p)) 632 return NULL; 633 634 p->addr = *group; 635 p->port = port; 636 p->state = state; 637 rcu_assign_pointer(p->next, next); 638 hlist_add_head(&p->mglist, &port->mglist); 639 setup_timer(&p->timer, br_multicast_port_group_expired, 640 (unsigned long)p); 641 return p; 642 } 643 644 static int br_multicast_add_group(struct net_bridge *br, 645 struct net_bridge_port *port, 646 struct br_ip *group) 647 { 648 struct net_bridge_mdb_entry *mp; 649 struct net_bridge_port_group *p; 650 struct net_bridge_port_group __rcu **pp; 651 unsigned long now = jiffies; 652 int err; 653 654 spin_lock(&br->multicast_lock); 655 if (!netif_running(br->dev) || 656 (port && port->state == BR_STATE_DISABLED)) 657 goto out; 658 659 mp = br_multicast_new_group(br, port, group); 660 err = PTR_ERR(mp); 661 if (IS_ERR(mp)) 662 goto err; 663 664 if (!port) { 665 mp->mglist = true; 666 mod_timer(&mp->timer, now + br->multicast_membership_interval); 667 goto out; 668 } 669 670 for (pp = &mp->ports; 671 (p = mlock_dereference(*pp, br)) != NULL; 672 pp = &p->next) { 673 if (p->port == port) 674 goto found; 675 if ((unsigned long)p->port < (unsigned long)port) 676 break; 677 } 678 679 p = br_multicast_new_port_group(port, group, *pp, MDB_TEMPORARY); 680 if (unlikely(!p)) 681 goto err; 682 rcu_assign_pointer(*pp, p); 683 br_mdb_notify(br->dev, port, group, RTM_NEWMDB); 684 685 found: 686 mod_timer(&p->timer, now + br->multicast_membership_interval); 687 out: 688 err = 0; 689 690 err: 691 spin_unlock(&br->multicast_lock); 692 return err; 693 } 694 695 static int br_ip4_multicast_add_group(struct net_bridge *br, 696 struct net_bridge_port *port, 697 __be32 group) 698 { 699 struct br_ip br_group; 700 701 if (ipv4_is_local_multicast(group)) 702 return 0; 703 704 br_group.u.ip4 = group; 705 br_group.proto = htons(ETH_P_IP); 706 707 return br_multicast_add_group(br, port, &br_group); 708 } 709 710 #if IS_ENABLED(CONFIG_IPV6) 711 static int br_ip6_multicast_add_group(struct net_bridge *br, 712 struct net_bridge_port *port, 713 const struct in6_addr *group) 714 { 715 struct br_ip br_group; 716 717 if (!ipv6_is_transient_multicast(group)) 718 return 0; 719 720 br_group.u.ip6 = *group; 721 br_group.proto = htons(ETH_P_IPV6); 722 723 return br_multicast_add_group(br, port, &br_group); 724 } 725 #endif 726 727 static void br_multicast_router_expired(unsigned long data) 728 { 729 struct net_bridge_port *port = (void *)data; 730 struct net_bridge *br = port->br; 731 732 spin_lock(&br->multicast_lock); 733 if (port->multicast_router != 1 || 734 timer_pending(&port->multicast_router_timer) || 735 hlist_unhashed(&port->rlist)) 736 goto out; 737 738 hlist_del_init_rcu(&port->rlist); 739 740 out: 741 spin_unlock(&br->multicast_lock); 742 } 743 744 static void br_multicast_local_router_expired(unsigned long data) 745 { 746 } 747 748 static void br_multicast_querier_expired(unsigned long data) 749 { 750 struct net_bridge *br = (void *)data; 751 752 spin_lock(&br->multicast_lock); 753 if (!netif_running(br->dev) || br->multicast_disabled) 754 goto out; 755 756 br_multicast_start_querier(br); 757 758 out: 759 spin_unlock(&br->multicast_lock); 760 } 761 762 static void __br_multicast_send_query(struct net_bridge *br, 763 struct net_bridge_port *port, 764 struct br_ip *ip) 765 { 766 struct sk_buff *skb; 767 768 skb = br_multicast_alloc_query(br, ip); 769 if (!skb) 770 return; 771 772 if (port) { 773 __skb_push(skb, sizeof(struct ethhdr)); 774 skb->dev = port->dev; 775 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev, 776 dev_queue_xmit); 777 } else 778 netif_rx(skb); 779 } 780 781 static void br_multicast_send_query(struct net_bridge *br, 782 struct net_bridge_port *port, u32 sent) 783 { 784 unsigned long time; 785 struct br_ip br_group; 786 787 if (!netif_running(br->dev) || br->multicast_disabled || 788 !br->multicast_querier || 789 timer_pending(&br->multicast_querier_timer)) 790 return; 791 792 memset(&br_group.u, 0, sizeof(br_group.u)); 793 794 br_group.proto = htons(ETH_P_IP); 795 __br_multicast_send_query(br, port, &br_group); 796 797 #if IS_ENABLED(CONFIG_IPV6) 798 br_group.proto = htons(ETH_P_IPV6); 799 __br_multicast_send_query(br, port, &br_group); 800 #endif 801 802 time = jiffies; 803 time += sent < br->multicast_startup_query_count ? 804 br->multicast_startup_query_interval : 805 br->multicast_query_interval; 806 mod_timer(port ? &port->multicast_query_timer : 807 &br->multicast_query_timer, time); 808 } 809 810 static void br_multicast_port_query_expired(unsigned long data) 811 { 812 struct net_bridge_port *port = (void *)data; 813 struct net_bridge *br = port->br; 814 815 spin_lock(&br->multicast_lock); 816 if (port->state == BR_STATE_DISABLED || 817 port->state == BR_STATE_BLOCKING) 818 goto out; 819 820 if (port->multicast_startup_queries_sent < 821 br->multicast_startup_query_count) 822 port->multicast_startup_queries_sent++; 823 824 br_multicast_send_query(port->br, port, 825 port->multicast_startup_queries_sent); 826 827 out: 828 spin_unlock(&br->multicast_lock); 829 } 830 831 void br_multicast_add_port(struct net_bridge_port *port) 832 { 833 port->multicast_router = 1; 834 835 setup_timer(&port->multicast_router_timer, br_multicast_router_expired, 836 (unsigned long)port); 837 setup_timer(&port->multicast_query_timer, 838 br_multicast_port_query_expired, (unsigned long)port); 839 } 840 841 void br_multicast_del_port(struct net_bridge_port *port) 842 { 843 del_timer_sync(&port->multicast_router_timer); 844 } 845 846 static void __br_multicast_enable_port(struct net_bridge_port *port) 847 { 848 port->multicast_startup_queries_sent = 0; 849 850 if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 || 851 del_timer(&port->multicast_query_timer)) 852 mod_timer(&port->multicast_query_timer, jiffies); 853 } 854 855 void br_multicast_enable_port(struct net_bridge_port *port) 856 { 857 struct net_bridge *br = port->br; 858 859 spin_lock(&br->multicast_lock); 860 if (br->multicast_disabled || !netif_running(br->dev)) 861 goto out; 862 863 __br_multicast_enable_port(port); 864 865 out: 866 spin_unlock(&br->multicast_lock); 867 } 868 869 void br_multicast_disable_port(struct net_bridge_port *port) 870 { 871 struct net_bridge *br = port->br; 872 struct net_bridge_port_group *pg; 873 struct hlist_node *p, *n; 874 875 spin_lock(&br->multicast_lock); 876 hlist_for_each_entry_safe(pg, p, n, &port->mglist, mglist) 877 br_multicast_del_pg(br, pg); 878 879 if (!hlist_unhashed(&port->rlist)) 880 hlist_del_init_rcu(&port->rlist); 881 del_timer(&port->multicast_router_timer); 882 del_timer(&port->multicast_query_timer); 883 spin_unlock(&br->multicast_lock); 884 } 885 886 static int br_ip4_multicast_igmp3_report(struct net_bridge *br, 887 struct net_bridge_port *port, 888 struct sk_buff *skb) 889 { 890 struct igmpv3_report *ih; 891 struct igmpv3_grec *grec; 892 int i; 893 int len; 894 int num; 895 int type; 896 int err = 0; 897 __be32 group; 898 899 if (!pskb_may_pull(skb, sizeof(*ih))) 900 return -EINVAL; 901 902 ih = igmpv3_report_hdr(skb); 903 num = ntohs(ih->ngrec); 904 len = sizeof(*ih); 905 906 for (i = 0; i < num; i++) { 907 len += sizeof(*grec); 908 if (!pskb_may_pull(skb, len)) 909 return -EINVAL; 910 911 grec = (void *)(skb->data + len - sizeof(*grec)); 912 group = grec->grec_mca; 913 type = grec->grec_type; 914 915 len += ntohs(grec->grec_nsrcs) * 4; 916 if (!pskb_may_pull(skb, len)) 917 return -EINVAL; 918 919 /* We treat this as an IGMPv2 report for now. */ 920 switch (type) { 921 case IGMPV3_MODE_IS_INCLUDE: 922 case IGMPV3_MODE_IS_EXCLUDE: 923 case IGMPV3_CHANGE_TO_INCLUDE: 924 case IGMPV3_CHANGE_TO_EXCLUDE: 925 case IGMPV3_ALLOW_NEW_SOURCES: 926 case IGMPV3_BLOCK_OLD_SOURCES: 927 break; 928 929 default: 930 continue; 931 } 932 933 err = br_ip4_multicast_add_group(br, port, group); 934 if (err) 935 break; 936 } 937 938 return err; 939 } 940 941 #if IS_ENABLED(CONFIG_IPV6) 942 static int br_ip6_multicast_mld2_report(struct net_bridge *br, 943 struct net_bridge_port *port, 944 struct sk_buff *skb) 945 { 946 struct icmp6hdr *icmp6h; 947 struct mld2_grec *grec; 948 int i; 949 int len; 950 int num; 951 int err = 0; 952 953 if (!pskb_may_pull(skb, sizeof(*icmp6h))) 954 return -EINVAL; 955 956 icmp6h = icmp6_hdr(skb); 957 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]); 958 len = sizeof(*icmp6h); 959 960 for (i = 0; i < num; i++) { 961 __be16 *nsrcs, _nsrcs; 962 963 nsrcs = skb_header_pointer(skb, 964 len + offsetof(struct mld2_grec, 965 grec_nsrcs), 966 sizeof(_nsrcs), &_nsrcs); 967 if (!nsrcs) 968 return -EINVAL; 969 970 if (!pskb_may_pull(skb, 971 len + sizeof(*grec) + 972 sizeof(struct in6_addr) * ntohs(*nsrcs))) 973 return -EINVAL; 974 975 grec = (struct mld2_grec *)(skb->data + len); 976 len += sizeof(*grec) + 977 sizeof(struct in6_addr) * ntohs(*nsrcs); 978 979 /* We treat these as MLDv1 reports for now. */ 980 switch (grec->grec_type) { 981 case MLD2_MODE_IS_INCLUDE: 982 case MLD2_MODE_IS_EXCLUDE: 983 case MLD2_CHANGE_TO_INCLUDE: 984 case MLD2_CHANGE_TO_EXCLUDE: 985 case MLD2_ALLOW_NEW_SOURCES: 986 case MLD2_BLOCK_OLD_SOURCES: 987 break; 988 989 default: 990 continue; 991 } 992 993 err = br_ip6_multicast_add_group(br, port, &grec->grec_mca); 994 if (!err) 995 break; 996 } 997 998 return err; 999 } 1000 #endif 1001 1002 /* 1003 * Add port to rotuer_list 1004 * list is maintained ordered by pointer value 1005 * and locked by br->multicast_lock and RCU 1006 */ 1007 static void br_multicast_add_router(struct net_bridge *br, 1008 struct net_bridge_port *port) 1009 { 1010 struct net_bridge_port *p; 1011 struct hlist_node *n, *slot = NULL; 1012 1013 hlist_for_each_entry(p, n, &br->router_list, rlist) { 1014 if ((unsigned long) port >= (unsigned long) p) 1015 break; 1016 slot = n; 1017 } 1018 1019 if (slot) 1020 hlist_add_after_rcu(slot, &port->rlist); 1021 else 1022 hlist_add_head_rcu(&port->rlist, &br->router_list); 1023 } 1024 1025 static void br_multicast_mark_router(struct net_bridge *br, 1026 struct net_bridge_port *port) 1027 { 1028 unsigned long now = jiffies; 1029 1030 if (!port) { 1031 if (br->multicast_router == 1) 1032 mod_timer(&br->multicast_router_timer, 1033 now + br->multicast_querier_interval); 1034 return; 1035 } 1036 1037 if (port->multicast_router != 1) 1038 return; 1039 1040 if (!hlist_unhashed(&port->rlist)) 1041 goto timer; 1042 1043 br_multicast_add_router(br, port); 1044 1045 timer: 1046 mod_timer(&port->multicast_router_timer, 1047 now + br->multicast_querier_interval); 1048 } 1049 1050 static void br_multicast_query_received(struct net_bridge *br, 1051 struct net_bridge_port *port, 1052 int saddr) 1053 { 1054 if (saddr) 1055 mod_timer(&br->multicast_querier_timer, 1056 jiffies + br->multicast_querier_interval); 1057 else if (timer_pending(&br->multicast_querier_timer)) 1058 return; 1059 1060 br_multicast_mark_router(br, port); 1061 } 1062 1063 static int br_ip4_multicast_query(struct net_bridge *br, 1064 struct net_bridge_port *port, 1065 struct sk_buff *skb) 1066 { 1067 const struct iphdr *iph = ip_hdr(skb); 1068 struct igmphdr *ih = igmp_hdr(skb); 1069 struct net_bridge_mdb_entry *mp; 1070 struct igmpv3_query *ih3; 1071 struct net_bridge_port_group *p; 1072 struct net_bridge_port_group __rcu **pp; 1073 unsigned long max_delay; 1074 unsigned long now = jiffies; 1075 __be32 group; 1076 int err = 0; 1077 1078 spin_lock(&br->multicast_lock); 1079 if (!netif_running(br->dev) || 1080 (port && port->state == BR_STATE_DISABLED)) 1081 goto out; 1082 1083 br_multicast_query_received(br, port, !!iph->saddr); 1084 1085 group = ih->group; 1086 1087 if (skb->len == sizeof(*ih)) { 1088 max_delay = ih->code * (HZ / IGMP_TIMER_SCALE); 1089 1090 if (!max_delay) { 1091 max_delay = 10 * HZ; 1092 group = 0; 1093 } 1094 } else { 1095 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) { 1096 err = -EINVAL; 1097 goto out; 1098 } 1099 1100 ih3 = igmpv3_query_hdr(skb); 1101 if (ih3->nsrcs) 1102 goto out; 1103 1104 max_delay = ih3->code ? 1105 IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1; 1106 } 1107 1108 if (!group) 1109 goto out; 1110 1111 mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group); 1112 if (!mp) 1113 goto out; 1114 1115 max_delay *= br->multicast_last_member_count; 1116 1117 if (mp->mglist && 1118 (timer_pending(&mp->timer) ? 1119 time_after(mp->timer.expires, now + max_delay) : 1120 try_to_del_timer_sync(&mp->timer) >= 0)) 1121 mod_timer(&mp->timer, now + max_delay); 1122 1123 for (pp = &mp->ports; 1124 (p = mlock_dereference(*pp, br)) != NULL; 1125 pp = &p->next) { 1126 if (timer_pending(&p->timer) ? 1127 time_after(p->timer.expires, now + max_delay) : 1128 try_to_del_timer_sync(&p->timer) >= 0) 1129 mod_timer(&p->timer, now + max_delay); 1130 } 1131 1132 out: 1133 spin_unlock(&br->multicast_lock); 1134 return err; 1135 } 1136 1137 #if IS_ENABLED(CONFIG_IPV6) 1138 static int br_ip6_multicast_query(struct net_bridge *br, 1139 struct net_bridge_port *port, 1140 struct sk_buff *skb) 1141 { 1142 const struct ipv6hdr *ip6h = ipv6_hdr(skb); 1143 struct mld_msg *mld; 1144 struct net_bridge_mdb_entry *mp; 1145 struct mld2_query *mld2q; 1146 struct net_bridge_port_group *p; 1147 struct net_bridge_port_group __rcu **pp; 1148 unsigned long max_delay; 1149 unsigned long now = jiffies; 1150 const struct in6_addr *group = NULL; 1151 int err = 0; 1152 1153 spin_lock(&br->multicast_lock); 1154 if (!netif_running(br->dev) || 1155 (port && port->state == BR_STATE_DISABLED)) 1156 goto out; 1157 1158 br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->saddr)); 1159 1160 if (skb->len == sizeof(*mld)) { 1161 if (!pskb_may_pull(skb, sizeof(*mld))) { 1162 err = -EINVAL; 1163 goto out; 1164 } 1165 mld = (struct mld_msg *) icmp6_hdr(skb); 1166 max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay)); 1167 if (max_delay) 1168 group = &mld->mld_mca; 1169 } else if (skb->len >= sizeof(*mld2q)) { 1170 if (!pskb_may_pull(skb, sizeof(*mld2q))) { 1171 err = -EINVAL; 1172 goto out; 1173 } 1174 mld2q = (struct mld2_query *)icmp6_hdr(skb); 1175 if (!mld2q->mld2q_nsrcs) 1176 group = &mld2q->mld2q_mca; 1177 max_delay = mld2q->mld2q_mrc ? MLDV2_MRC(ntohs(mld2q->mld2q_mrc)) : 1; 1178 } 1179 1180 if (!group) 1181 goto out; 1182 1183 mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group); 1184 if (!mp) 1185 goto out; 1186 1187 max_delay *= br->multicast_last_member_count; 1188 if (mp->mglist && 1189 (timer_pending(&mp->timer) ? 1190 time_after(mp->timer.expires, now + max_delay) : 1191 try_to_del_timer_sync(&mp->timer) >= 0)) 1192 mod_timer(&mp->timer, now + max_delay); 1193 1194 for (pp = &mp->ports; 1195 (p = mlock_dereference(*pp, br)) != NULL; 1196 pp = &p->next) { 1197 if (timer_pending(&p->timer) ? 1198 time_after(p->timer.expires, now + max_delay) : 1199 try_to_del_timer_sync(&p->timer) >= 0) 1200 mod_timer(&p->timer, now + max_delay); 1201 } 1202 1203 out: 1204 spin_unlock(&br->multicast_lock); 1205 return err; 1206 } 1207 #endif 1208 1209 static void br_multicast_leave_group(struct net_bridge *br, 1210 struct net_bridge_port *port, 1211 struct br_ip *group) 1212 { 1213 struct net_bridge_mdb_htable *mdb; 1214 struct net_bridge_mdb_entry *mp; 1215 struct net_bridge_port_group *p; 1216 unsigned long now; 1217 unsigned long time; 1218 1219 spin_lock(&br->multicast_lock); 1220 if (!netif_running(br->dev) || 1221 (port && port->state == BR_STATE_DISABLED) || 1222 timer_pending(&br->multicast_querier_timer)) 1223 goto out; 1224 1225 mdb = mlock_dereference(br->mdb, br); 1226 mp = br_mdb_ip_get(mdb, group); 1227 if (!mp) 1228 goto out; 1229 1230 if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) { 1231 struct net_bridge_port_group __rcu **pp; 1232 1233 for (pp = &mp->ports; 1234 (p = mlock_dereference(*pp, br)) != NULL; 1235 pp = &p->next) { 1236 if (p->port != port) 1237 continue; 1238 1239 rcu_assign_pointer(*pp, p->next); 1240 hlist_del_init(&p->mglist); 1241 del_timer(&p->timer); 1242 call_rcu_bh(&p->rcu, br_multicast_free_pg); 1243 br_mdb_notify(br->dev, port, group, RTM_DELMDB); 1244 1245 if (!mp->ports && !mp->mglist && 1246 netif_running(br->dev)) 1247 mod_timer(&mp->timer, jiffies); 1248 } 1249 goto out; 1250 } 1251 1252 now = jiffies; 1253 time = now + br->multicast_last_member_count * 1254 br->multicast_last_member_interval; 1255 1256 if (!port) { 1257 if (mp->mglist && 1258 (timer_pending(&mp->timer) ? 1259 time_after(mp->timer.expires, time) : 1260 try_to_del_timer_sync(&mp->timer) >= 0)) { 1261 mod_timer(&mp->timer, time); 1262 } 1263 1264 goto out; 1265 } 1266 1267 for (p = mlock_dereference(mp->ports, br); 1268 p != NULL; 1269 p = mlock_dereference(p->next, br)) { 1270 if (p->port != port) 1271 continue; 1272 1273 if (!hlist_unhashed(&p->mglist) && 1274 (timer_pending(&p->timer) ? 1275 time_after(p->timer.expires, time) : 1276 try_to_del_timer_sync(&p->timer) >= 0)) { 1277 mod_timer(&p->timer, time); 1278 } 1279 1280 break; 1281 } 1282 1283 out: 1284 spin_unlock(&br->multicast_lock); 1285 } 1286 1287 static void br_ip4_multicast_leave_group(struct net_bridge *br, 1288 struct net_bridge_port *port, 1289 __be32 group) 1290 { 1291 struct br_ip br_group; 1292 1293 if (ipv4_is_local_multicast(group)) 1294 return; 1295 1296 br_group.u.ip4 = group; 1297 br_group.proto = htons(ETH_P_IP); 1298 1299 br_multicast_leave_group(br, port, &br_group); 1300 } 1301 1302 #if IS_ENABLED(CONFIG_IPV6) 1303 static void br_ip6_multicast_leave_group(struct net_bridge *br, 1304 struct net_bridge_port *port, 1305 const struct in6_addr *group) 1306 { 1307 struct br_ip br_group; 1308 1309 if (!ipv6_is_transient_multicast(group)) 1310 return; 1311 1312 br_group.u.ip6 = *group; 1313 br_group.proto = htons(ETH_P_IPV6); 1314 1315 br_multicast_leave_group(br, port, &br_group); 1316 } 1317 #endif 1318 1319 static int br_multicast_ipv4_rcv(struct net_bridge *br, 1320 struct net_bridge_port *port, 1321 struct sk_buff *skb) 1322 { 1323 struct sk_buff *skb2 = skb; 1324 const struct iphdr *iph; 1325 struct igmphdr *ih; 1326 unsigned int len; 1327 unsigned int offset; 1328 int err; 1329 1330 /* We treat OOM as packet loss for now. */ 1331 if (!pskb_may_pull(skb, sizeof(*iph))) 1332 return -EINVAL; 1333 1334 iph = ip_hdr(skb); 1335 1336 if (iph->ihl < 5 || iph->version != 4) 1337 return -EINVAL; 1338 1339 if (!pskb_may_pull(skb, ip_hdrlen(skb))) 1340 return -EINVAL; 1341 1342 iph = ip_hdr(skb); 1343 1344 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl))) 1345 return -EINVAL; 1346 1347 if (iph->protocol != IPPROTO_IGMP) { 1348 if ((iph->daddr & IGMP_LOCAL_GROUP_MASK) != IGMP_LOCAL_GROUP) 1349 BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 1350 return 0; 1351 } 1352 1353 len = ntohs(iph->tot_len); 1354 if (skb->len < len || len < ip_hdrlen(skb)) 1355 return -EINVAL; 1356 1357 if (skb->len > len) { 1358 skb2 = skb_clone(skb, GFP_ATOMIC); 1359 if (!skb2) 1360 return -ENOMEM; 1361 1362 err = pskb_trim_rcsum(skb2, len); 1363 if (err) 1364 goto err_out; 1365 } 1366 1367 len -= ip_hdrlen(skb2); 1368 offset = skb_network_offset(skb2) + ip_hdrlen(skb2); 1369 __skb_pull(skb2, offset); 1370 skb_reset_transport_header(skb2); 1371 1372 err = -EINVAL; 1373 if (!pskb_may_pull(skb2, sizeof(*ih))) 1374 goto out; 1375 1376 switch (skb2->ip_summed) { 1377 case CHECKSUM_COMPLETE: 1378 if (!csum_fold(skb2->csum)) 1379 break; 1380 /* fall through */ 1381 case CHECKSUM_NONE: 1382 skb2->csum = 0; 1383 if (skb_checksum_complete(skb2)) 1384 goto out; 1385 } 1386 1387 err = 0; 1388 1389 BR_INPUT_SKB_CB(skb)->igmp = 1; 1390 ih = igmp_hdr(skb2); 1391 1392 switch (ih->type) { 1393 case IGMP_HOST_MEMBERSHIP_REPORT: 1394 case IGMPV2_HOST_MEMBERSHIP_REPORT: 1395 BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 1396 err = br_ip4_multicast_add_group(br, port, ih->group); 1397 break; 1398 case IGMPV3_HOST_MEMBERSHIP_REPORT: 1399 err = br_ip4_multicast_igmp3_report(br, port, skb2); 1400 break; 1401 case IGMP_HOST_MEMBERSHIP_QUERY: 1402 err = br_ip4_multicast_query(br, port, skb2); 1403 break; 1404 case IGMP_HOST_LEAVE_MESSAGE: 1405 br_ip4_multicast_leave_group(br, port, ih->group); 1406 break; 1407 } 1408 1409 out: 1410 __skb_push(skb2, offset); 1411 err_out: 1412 if (skb2 != skb) 1413 kfree_skb(skb2); 1414 return err; 1415 } 1416 1417 #if IS_ENABLED(CONFIG_IPV6) 1418 static int br_multicast_ipv6_rcv(struct net_bridge *br, 1419 struct net_bridge_port *port, 1420 struct sk_buff *skb) 1421 { 1422 struct sk_buff *skb2; 1423 const struct ipv6hdr *ip6h; 1424 u8 icmp6_type; 1425 u8 nexthdr; 1426 __be16 frag_off; 1427 unsigned int len; 1428 int offset; 1429 int err; 1430 1431 if (!pskb_may_pull(skb, sizeof(*ip6h))) 1432 return -EINVAL; 1433 1434 ip6h = ipv6_hdr(skb); 1435 1436 /* 1437 * We're interested in MLD messages only. 1438 * - Version is 6 1439 * - MLD has always Router Alert hop-by-hop option 1440 * - But we do not support jumbrograms. 1441 */ 1442 if (ip6h->version != 6 || 1443 ip6h->nexthdr != IPPROTO_HOPOPTS || 1444 ip6h->payload_len == 0) 1445 return 0; 1446 1447 len = ntohs(ip6h->payload_len) + sizeof(*ip6h); 1448 if (skb->len < len) 1449 return -EINVAL; 1450 1451 nexthdr = ip6h->nexthdr; 1452 offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr, &frag_off); 1453 1454 if (offset < 0 || nexthdr != IPPROTO_ICMPV6) 1455 return 0; 1456 1457 /* Okay, we found ICMPv6 header */ 1458 skb2 = skb_clone(skb, GFP_ATOMIC); 1459 if (!skb2) 1460 return -ENOMEM; 1461 1462 err = -EINVAL; 1463 if (!pskb_may_pull(skb2, offset + sizeof(struct icmp6hdr))) 1464 goto out; 1465 1466 len -= offset - skb_network_offset(skb2); 1467 1468 __skb_pull(skb2, offset); 1469 skb_reset_transport_header(skb2); 1470 skb_postpull_rcsum(skb2, skb_network_header(skb2), 1471 skb_network_header_len(skb2)); 1472 1473 icmp6_type = icmp6_hdr(skb2)->icmp6_type; 1474 1475 switch (icmp6_type) { 1476 case ICMPV6_MGM_QUERY: 1477 case ICMPV6_MGM_REPORT: 1478 case ICMPV6_MGM_REDUCTION: 1479 case ICMPV6_MLD2_REPORT: 1480 break; 1481 default: 1482 err = 0; 1483 goto out; 1484 } 1485 1486 /* Okay, we found MLD message. Check further. */ 1487 if (skb2->len > len) { 1488 err = pskb_trim_rcsum(skb2, len); 1489 if (err) 1490 goto out; 1491 err = -EINVAL; 1492 } 1493 1494 ip6h = ipv6_hdr(skb2); 1495 1496 switch (skb2->ip_summed) { 1497 case CHECKSUM_COMPLETE: 1498 if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, skb2->len, 1499 IPPROTO_ICMPV6, skb2->csum)) 1500 break; 1501 /*FALLTHROUGH*/ 1502 case CHECKSUM_NONE: 1503 skb2->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr, 1504 &ip6h->daddr, 1505 skb2->len, 1506 IPPROTO_ICMPV6, 0)); 1507 if (__skb_checksum_complete(skb2)) 1508 goto out; 1509 } 1510 1511 err = 0; 1512 1513 BR_INPUT_SKB_CB(skb)->igmp = 1; 1514 1515 switch (icmp6_type) { 1516 case ICMPV6_MGM_REPORT: 1517 { 1518 struct mld_msg *mld; 1519 if (!pskb_may_pull(skb2, sizeof(*mld))) { 1520 err = -EINVAL; 1521 goto out; 1522 } 1523 mld = (struct mld_msg *)skb_transport_header(skb2); 1524 BR_INPUT_SKB_CB(skb)->mrouters_only = 1; 1525 err = br_ip6_multicast_add_group(br, port, &mld->mld_mca); 1526 break; 1527 } 1528 case ICMPV6_MLD2_REPORT: 1529 err = br_ip6_multicast_mld2_report(br, port, skb2); 1530 break; 1531 case ICMPV6_MGM_QUERY: 1532 err = br_ip6_multicast_query(br, port, skb2); 1533 break; 1534 case ICMPV6_MGM_REDUCTION: 1535 { 1536 struct mld_msg *mld; 1537 if (!pskb_may_pull(skb2, sizeof(*mld))) { 1538 err = -EINVAL; 1539 goto out; 1540 } 1541 mld = (struct mld_msg *)skb_transport_header(skb2); 1542 br_ip6_multicast_leave_group(br, port, &mld->mld_mca); 1543 } 1544 } 1545 1546 out: 1547 kfree_skb(skb2); 1548 return err; 1549 } 1550 #endif 1551 1552 int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port, 1553 struct sk_buff *skb) 1554 { 1555 BR_INPUT_SKB_CB(skb)->igmp = 0; 1556 BR_INPUT_SKB_CB(skb)->mrouters_only = 0; 1557 1558 if (br->multicast_disabled) 1559 return 0; 1560 1561 switch (skb->protocol) { 1562 case htons(ETH_P_IP): 1563 return br_multicast_ipv4_rcv(br, port, skb); 1564 #if IS_ENABLED(CONFIG_IPV6) 1565 case htons(ETH_P_IPV6): 1566 return br_multicast_ipv6_rcv(br, port, skb); 1567 #endif 1568 } 1569 1570 return 0; 1571 } 1572 1573 static void br_multicast_query_expired(unsigned long data) 1574 { 1575 struct net_bridge *br = (void *)data; 1576 1577 spin_lock(&br->multicast_lock); 1578 if (br->multicast_startup_queries_sent < 1579 br->multicast_startup_query_count) 1580 br->multicast_startup_queries_sent++; 1581 1582 br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent); 1583 1584 spin_unlock(&br->multicast_lock); 1585 } 1586 1587 void br_multicast_init(struct net_bridge *br) 1588 { 1589 br->hash_elasticity = 4; 1590 br->hash_max = 512; 1591 1592 br->multicast_router = 1; 1593 br->multicast_querier = 0; 1594 br->multicast_last_member_count = 2; 1595 br->multicast_startup_query_count = 2; 1596 1597 br->multicast_last_member_interval = HZ; 1598 br->multicast_query_response_interval = 10 * HZ; 1599 br->multicast_startup_query_interval = 125 * HZ / 4; 1600 br->multicast_query_interval = 125 * HZ; 1601 br->multicast_querier_interval = 255 * HZ; 1602 br->multicast_membership_interval = 260 * HZ; 1603 1604 spin_lock_init(&br->multicast_lock); 1605 setup_timer(&br->multicast_router_timer, 1606 br_multicast_local_router_expired, 0); 1607 setup_timer(&br->multicast_querier_timer, 1608 br_multicast_querier_expired, (unsigned long)br); 1609 setup_timer(&br->multicast_query_timer, br_multicast_query_expired, 1610 (unsigned long)br); 1611 } 1612 1613 void br_multicast_open(struct net_bridge *br) 1614 { 1615 br->multicast_startup_queries_sent = 0; 1616 1617 if (br->multicast_disabled) 1618 return; 1619 1620 mod_timer(&br->multicast_query_timer, jiffies); 1621 } 1622 1623 void br_multicast_stop(struct net_bridge *br) 1624 { 1625 struct net_bridge_mdb_htable *mdb; 1626 struct net_bridge_mdb_entry *mp; 1627 struct hlist_node *p, *n; 1628 u32 ver; 1629 int i; 1630 1631 del_timer_sync(&br->multicast_router_timer); 1632 del_timer_sync(&br->multicast_querier_timer); 1633 del_timer_sync(&br->multicast_query_timer); 1634 1635 spin_lock_bh(&br->multicast_lock); 1636 mdb = mlock_dereference(br->mdb, br); 1637 if (!mdb) 1638 goto out; 1639 1640 br->mdb = NULL; 1641 1642 ver = mdb->ver; 1643 for (i = 0; i < mdb->max; i++) { 1644 hlist_for_each_entry_safe(mp, p, n, &mdb->mhash[i], 1645 hlist[ver]) { 1646 del_timer(&mp->timer); 1647 call_rcu_bh(&mp->rcu, br_multicast_free_group); 1648 } 1649 } 1650 1651 if (mdb->old) { 1652 spin_unlock_bh(&br->multicast_lock); 1653 rcu_barrier_bh(); 1654 spin_lock_bh(&br->multicast_lock); 1655 WARN_ON(mdb->old); 1656 } 1657 1658 mdb->old = mdb; 1659 call_rcu_bh(&mdb->rcu, br_mdb_free); 1660 1661 out: 1662 spin_unlock_bh(&br->multicast_lock); 1663 } 1664 1665 int br_multicast_set_router(struct net_bridge *br, unsigned long val) 1666 { 1667 int err = -ENOENT; 1668 1669 spin_lock_bh(&br->multicast_lock); 1670 if (!netif_running(br->dev)) 1671 goto unlock; 1672 1673 switch (val) { 1674 case 0: 1675 case 2: 1676 del_timer(&br->multicast_router_timer); 1677 /* fall through */ 1678 case 1: 1679 br->multicast_router = val; 1680 err = 0; 1681 break; 1682 1683 default: 1684 err = -EINVAL; 1685 break; 1686 } 1687 1688 unlock: 1689 spin_unlock_bh(&br->multicast_lock); 1690 1691 return err; 1692 } 1693 1694 int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val) 1695 { 1696 struct net_bridge *br = p->br; 1697 int err = -ENOENT; 1698 1699 spin_lock(&br->multicast_lock); 1700 if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED) 1701 goto unlock; 1702 1703 switch (val) { 1704 case 0: 1705 case 1: 1706 case 2: 1707 p->multicast_router = val; 1708 err = 0; 1709 1710 if (val < 2 && !hlist_unhashed(&p->rlist)) 1711 hlist_del_init_rcu(&p->rlist); 1712 1713 if (val == 1) 1714 break; 1715 1716 del_timer(&p->multicast_router_timer); 1717 1718 if (val == 0) 1719 break; 1720 1721 br_multicast_add_router(br, p); 1722 break; 1723 1724 default: 1725 err = -EINVAL; 1726 break; 1727 } 1728 1729 unlock: 1730 spin_unlock(&br->multicast_lock); 1731 1732 return err; 1733 } 1734 1735 static void br_multicast_start_querier(struct net_bridge *br) 1736 { 1737 struct net_bridge_port *port; 1738 1739 br_multicast_open(br); 1740 1741 list_for_each_entry(port, &br->port_list, list) { 1742 if (port->state == BR_STATE_DISABLED || 1743 port->state == BR_STATE_BLOCKING) 1744 continue; 1745 1746 __br_multicast_enable_port(port); 1747 } 1748 } 1749 1750 int br_multicast_toggle(struct net_bridge *br, unsigned long val) 1751 { 1752 int err = 0; 1753 struct net_bridge_mdb_htable *mdb; 1754 1755 spin_lock_bh(&br->multicast_lock); 1756 if (br->multicast_disabled == !val) 1757 goto unlock; 1758 1759 br->multicast_disabled = !val; 1760 if (br->multicast_disabled) 1761 goto unlock; 1762 1763 if (!netif_running(br->dev)) 1764 goto unlock; 1765 1766 mdb = mlock_dereference(br->mdb, br); 1767 if (mdb) { 1768 if (mdb->old) { 1769 err = -EEXIST; 1770 rollback: 1771 br->multicast_disabled = !!val; 1772 goto unlock; 1773 } 1774 1775 err = br_mdb_rehash(&br->mdb, mdb->max, 1776 br->hash_elasticity); 1777 if (err) 1778 goto rollback; 1779 } 1780 1781 br_multicast_start_querier(br); 1782 1783 unlock: 1784 spin_unlock_bh(&br->multicast_lock); 1785 1786 return err; 1787 } 1788 1789 int br_multicast_set_querier(struct net_bridge *br, unsigned long val) 1790 { 1791 val = !!val; 1792 1793 spin_lock_bh(&br->multicast_lock); 1794 if (br->multicast_querier == val) 1795 goto unlock; 1796 1797 br->multicast_querier = val; 1798 if (val) 1799 br_multicast_start_querier(br); 1800 1801 unlock: 1802 spin_unlock_bh(&br->multicast_lock); 1803 1804 return 0; 1805 } 1806 1807 int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val) 1808 { 1809 int err = -ENOENT; 1810 u32 old; 1811 struct net_bridge_mdb_htable *mdb; 1812 1813 spin_lock(&br->multicast_lock); 1814 if (!netif_running(br->dev)) 1815 goto unlock; 1816 1817 err = -EINVAL; 1818 if (!is_power_of_2(val)) 1819 goto unlock; 1820 1821 mdb = mlock_dereference(br->mdb, br); 1822 if (mdb && val < mdb->size) 1823 goto unlock; 1824 1825 err = 0; 1826 1827 old = br->hash_max; 1828 br->hash_max = val; 1829 1830 if (mdb) { 1831 if (mdb->old) { 1832 err = -EEXIST; 1833 rollback: 1834 br->hash_max = old; 1835 goto unlock; 1836 } 1837 1838 err = br_mdb_rehash(&br->mdb, br->hash_max, 1839 br->hash_elasticity); 1840 if (err) 1841 goto rollback; 1842 } 1843 1844 unlock: 1845 spin_unlock(&br->multicast_lock); 1846 1847 return err; 1848 } 1849