1 #include <linux/kernel.h> 2 #include <linux/netdevice.h> 3 #include <linux/rtnetlink.h> 4 #include <linux/slab.h> 5 #include <net/switchdev.h> 6 7 #include "br_private.h" 8 #include "br_private_tunnel.h" 9 10 static inline int br_vlan_cmp(struct rhashtable_compare_arg *arg, 11 const void *ptr) 12 { 13 const struct net_bridge_vlan *vle = ptr; 14 u16 vid = *(u16 *)arg->key; 15 16 return vle->vid != vid; 17 } 18 19 static const struct rhashtable_params br_vlan_rht_params = { 20 .head_offset = offsetof(struct net_bridge_vlan, vnode), 21 .key_offset = offsetof(struct net_bridge_vlan, vid), 22 .key_len = sizeof(u16), 23 .nelem_hint = 3, 24 .locks_mul = 1, 25 .max_size = VLAN_N_VID, 26 .obj_cmpfn = br_vlan_cmp, 27 .automatic_shrinking = true, 28 }; 29 30 static struct net_bridge_vlan *br_vlan_lookup(struct rhashtable *tbl, u16 vid) 31 { 32 return rhashtable_lookup_fast(tbl, &vid, br_vlan_rht_params); 33 } 34 35 static bool __vlan_add_pvid(struct net_bridge_vlan_group *vg, u16 vid) 36 { 37 if (vg->pvid == vid) 38 return false; 39 40 smp_wmb(); 41 vg->pvid = vid; 42 43 return true; 44 } 45 46 static bool __vlan_delete_pvid(struct net_bridge_vlan_group *vg, u16 vid) 47 { 48 if (vg->pvid != vid) 49 return false; 50 51 smp_wmb(); 52 vg->pvid = 0; 53 54 return true; 55 } 56 57 /* return true if anything changed, false otherwise */ 58 static bool __vlan_add_flags(struct net_bridge_vlan *v, u16 flags) 59 { 60 struct net_bridge_vlan_group *vg; 61 u16 old_flags = v->flags; 62 bool ret; 63 64 if (br_vlan_is_master(v)) 65 vg = br_vlan_group(v->br); 66 else 67 vg = nbp_vlan_group(v->port); 68 69 if (flags & BRIDGE_VLAN_INFO_PVID) 70 ret = __vlan_add_pvid(vg, v->vid); 71 else 72 ret = __vlan_delete_pvid(vg, v->vid); 73 74 if (flags & BRIDGE_VLAN_INFO_UNTAGGED) 75 v->flags |= BRIDGE_VLAN_INFO_UNTAGGED; 76 else 77 v->flags &= ~BRIDGE_VLAN_INFO_UNTAGGED; 78 79 return ret || !!(old_flags ^ v->flags); 80 } 81 82 static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br, 83 u16 vid, u16 flags) 84 { 85 int err; 86 87 /* Try switchdev op first. In case it is not supported, fallback to 88 * 8021q add. 89 */ 90 err = br_switchdev_port_vlan_add(dev, vid, flags); 91 if (err == -EOPNOTSUPP) 92 return vlan_vid_add(dev, br->vlan_proto, vid); 93 return err; 94 } 95 96 static void __vlan_add_list(struct net_bridge_vlan *v) 97 { 98 struct net_bridge_vlan_group *vg; 99 struct list_head *headp, *hpos; 100 struct net_bridge_vlan *vent; 101 102 if (br_vlan_is_master(v)) 103 vg = br_vlan_group(v->br); 104 else 105 vg = nbp_vlan_group(v->port); 106 107 headp = &vg->vlan_list; 108 list_for_each_prev(hpos, headp) { 109 vent = list_entry(hpos, struct net_bridge_vlan, vlist); 110 if (v->vid < vent->vid) 111 continue; 112 else 113 break; 114 } 115 list_add_rcu(&v->vlist, hpos); 116 } 117 118 static void __vlan_del_list(struct net_bridge_vlan *v) 119 { 120 list_del_rcu(&v->vlist); 121 } 122 123 static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br, 124 u16 vid) 125 { 126 int err; 127 128 /* Try switchdev op first. In case it is not supported, fallback to 129 * 8021q del. 130 */ 131 err = br_switchdev_port_vlan_del(dev, vid); 132 if (err == -EOPNOTSUPP) { 133 vlan_vid_del(dev, br->vlan_proto, vid); 134 return 0; 135 } 136 return err; 137 } 138 139 /* Returns a master vlan, if it didn't exist it gets created. In all cases a 140 * a reference is taken to the master vlan before returning. 141 */ 142 static struct net_bridge_vlan *br_vlan_get_master(struct net_bridge *br, u16 vid) 143 { 144 struct net_bridge_vlan_group *vg; 145 struct net_bridge_vlan *masterv; 146 147 vg = br_vlan_group(br); 148 masterv = br_vlan_find(vg, vid); 149 if (!masterv) { 150 bool changed; 151 152 /* missing global ctx, create it now */ 153 if (br_vlan_add(br, vid, 0, &changed)) 154 return NULL; 155 masterv = br_vlan_find(vg, vid); 156 if (WARN_ON(!masterv)) 157 return NULL; 158 refcount_set(&masterv->refcnt, 1); 159 return masterv; 160 } 161 refcount_inc(&masterv->refcnt); 162 163 return masterv; 164 } 165 166 static void br_master_vlan_rcu_free(struct rcu_head *rcu) 167 { 168 struct net_bridge_vlan *v; 169 170 v = container_of(rcu, struct net_bridge_vlan, rcu); 171 WARN_ON(!br_vlan_is_master(v)); 172 free_percpu(v->stats); 173 v->stats = NULL; 174 kfree(v); 175 } 176 177 static void br_vlan_put_master(struct net_bridge_vlan *masterv) 178 { 179 struct net_bridge_vlan_group *vg; 180 181 if (!br_vlan_is_master(masterv)) 182 return; 183 184 vg = br_vlan_group(masterv->br); 185 if (refcount_dec_and_test(&masterv->refcnt)) { 186 rhashtable_remove_fast(&vg->vlan_hash, 187 &masterv->vnode, br_vlan_rht_params); 188 __vlan_del_list(masterv); 189 call_rcu(&masterv->rcu, br_master_vlan_rcu_free); 190 } 191 } 192 193 /* This is the shared VLAN add function which works for both ports and bridge 194 * devices. There are four possible calls to this function in terms of the 195 * vlan entry type: 196 * 1. vlan is being added on a port (no master flags, global entry exists) 197 * 2. vlan is being added on a bridge (both master and brentry flags) 198 * 3. vlan is being added on a port, but a global entry didn't exist which 199 * is being created right now (master flag set, brentry flag unset), the 200 * global entry is used for global per-vlan features, but not for filtering 201 * 4. same as 3 but with both master and brentry flags set so the entry 202 * will be used for filtering in both the port and the bridge 203 */ 204 static int __vlan_add(struct net_bridge_vlan *v, u16 flags) 205 { 206 struct net_bridge_vlan *masterv = NULL; 207 struct net_bridge_port *p = NULL; 208 struct net_bridge_vlan_group *vg; 209 struct net_device *dev; 210 struct net_bridge *br; 211 int err; 212 213 if (br_vlan_is_master(v)) { 214 br = v->br; 215 dev = br->dev; 216 vg = br_vlan_group(br); 217 } else { 218 p = v->port; 219 br = p->br; 220 dev = p->dev; 221 vg = nbp_vlan_group(p); 222 } 223 224 if (p) { 225 /* Add VLAN to the device filter if it is supported. 226 * This ensures tagged traffic enters the bridge when 227 * promiscuous mode is disabled by br_manage_promisc(). 228 */ 229 err = __vlan_vid_add(dev, br, v->vid, flags); 230 if (err) 231 goto out; 232 233 /* need to work on the master vlan too */ 234 if (flags & BRIDGE_VLAN_INFO_MASTER) { 235 bool changed; 236 237 err = br_vlan_add(br, v->vid, 238 flags | BRIDGE_VLAN_INFO_BRENTRY, 239 &changed); 240 if (err) 241 goto out_filt; 242 } 243 244 masterv = br_vlan_get_master(br, v->vid); 245 if (!masterv) 246 goto out_filt; 247 v->brvlan = masterv; 248 v->stats = masterv->stats; 249 } else { 250 err = br_switchdev_port_vlan_add(dev, v->vid, flags); 251 if (err && err != -EOPNOTSUPP) 252 goto out; 253 } 254 255 /* Add the dev mac and count the vlan only if it's usable */ 256 if (br_vlan_should_use(v)) { 257 err = br_fdb_insert(br, p, dev->dev_addr, v->vid); 258 if (err) { 259 br_err(br, "failed insert local address into bridge forwarding table\n"); 260 goto out_filt; 261 } 262 vg->num_vlans++; 263 } 264 265 err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode, 266 br_vlan_rht_params); 267 if (err) 268 goto out_fdb_insert; 269 270 __vlan_add_list(v); 271 __vlan_add_flags(v, flags); 272 out: 273 return err; 274 275 out_fdb_insert: 276 if (br_vlan_should_use(v)) { 277 br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid); 278 vg->num_vlans--; 279 } 280 281 out_filt: 282 if (p) { 283 __vlan_vid_del(dev, br, v->vid); 284 if (masterv) { 285 br_vlan_put_master(masterv); 286 v->brvlan = NULL; 287 } 288 } else { 289 br_switchdev_port_vlan_del(dev, v->vid); 290 } 291 292 goto out; 293 } 294 295 static int __vlan_del(struct net_bridge_vlan *v) 296 { 297 struct net_bridge_vlan *masterv = v; 298 struct net_bridge_vlan_group *vg; 299 struct net_bridge_port *p = NULL; 300 int err = 0; 301 302 if (br_vlan_is_master(v)) { 303 vg = br_vlan_group(v->br); 304 } else { 305 p = v->port; 306 vg = nbp_vlan_group(v->port); 307 masterv = v->brvlan; 308 } 309 310 __vlan_delete_pvid(vg, v->vid); 311 if (p) { 312 err = __vlan_vid_del(p->dev, p->br, v->vid); 313 if (err) 314 goto out; 315 } else { 316 err = br_switchdev_port_vlan_del(v->br->dev, v->vid); 317 if (err && err != -EOPNOTSUPP) 318 goto out; 319 err = 0; 320 } 321 322 if (br_vlan_should_use(v)) { 323 v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY; 324 vg->num_vlans--; 325 } 326 327 if (masterv != v) { 328 vlan_tunnel_info_del(vg, v); 329 rhashtable_remove_fast(&vg->vlan_hash, &v->vnode, 330 br_vlan_rht_params); 331 __vlan_del_list(v); 332 kfree_rcu(v, rcu); 333 } 334 335 br_vlan_put_master(masterv); 336 out: 337 return err; 338 } 339 340 static void __vlan_group_free(struct net_bridge_vlan_group *vg) 341 { 342 WARN_ON(!list_empty(&vg->vlan_list)); 343 rhashtable_destroy(&vg->vlan_hash); 344 vlan_tunnel_deinit(vg); 345 kfree(vg); 346 } 347 348 static void __vlan_flush(struct net_bridge_vlan_group *vg) 349 { 350 struct net_bridge_vlan *vlan, *tmp; 351 352 __vlan_delete_pvid(vg, vg->pvid); 353 list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist) 354 __vlan_del(vlan); 355 } 356 357 struct sk_buff *br_handle_vlan(struct net_bridge *br, 358 const struct net_bridge_port *p, 359 struct net_bridge_vlan_group *vg, 360 struct sk_buff *skb) 361 { 362 struct br_vlan_stats *stats; 363 struct net_bridge_vlan *v; 364 u16 vid; 365 366 /* If this packet was not filtered at input, let it pass */ 367 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered) 368 goto out; 369 370 /* At this point, we know that the frame was filtered and contains 371 * a valid vlan id. If the vlan id has untagged flag set, 372 * send untagged; otherwise, send tagged. 373 */ 374 br_vlan_get_tag(skb, &vid); 375 v = br_vlan_find(vg, vid); 376 /* Vlan entry must be configured at this point. The 377 * only exception is the bridge is set in promisc mode and the 378 * packet is destined for the bridge device. In this case 379 * pass the packet as is. 380 */ 381 if (!v || !br_vlan_should_use(v)) { 382 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) { 383 goto out; 384 } else { 385 kfree_skb(skb); 386 return NULL; 387 } 388 } 389 if (br->vlan_stats_enabled) { 390 stats = this_cpu_ptr(v->stats); 391 u64_stats_update_begin(&stats->syncp); 392 stats->tx_bytes += skb->len; 393 stats->tx_packets++; 394 u64_stats_update_end(&stats->syncp); 395 } 396 397 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED) 398 skb->vlan_tci = 0; 399 400 if (p && (p->flags & BR_VLAN_TUNNEL) && 401 br_handle_egress_vlan_tunnel(skb, v)) { 402 kfree_skb(skb); 403 return NULL; 404 } 405 out: 406 return skb; 407 } 408 409 /* Called under RCU */ 410 static bool __allowed_ingress(const struct net_bridge *br, 411 struct net_bridge_vlan_group *vg, 412 struct sk_buff *skb, u16 *vid) 413 { 414 struct br_vlan_stats *stats; 415 struct net_bridge_vlan *v; 416 bool tagged; 417 418 BR_INPUT_SKB_CB(skb)->vlan_filtered = true; 419 /* If vlan tx offload is disabled on bridge device and frame was 420 * sent from vlan device on the bridge device, it does not have 421 * HW accelerated vlan tag. 422 */ 423 if (unlikely(!skb_vlan_tag_present(skb) && 424 skb->protocol == br->vlan_proto)) { 425 skb = skb_vlan_untag(skb); 426 if (unlikely(!skb)) 427 return false; 428 } 429 430 if (!br_vlan_get_tag(skb, vid)) { 431 /* Tagged frame */ 432 if (skb->vlan_proto != br->vlan_proto) { 433 /* Protocol-mismatch, empty out vlan_tci for new tag */ 434 skb_push(skb, ETH_HLEN); 435 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto, 436 skb_vlan_tag_get(skb)); 437 if (unlikely(!skb)) 438 return false; 439 440 skb_pull(skb, ETH_HLEN); 441 skb_reset_mac_len(skb); 442 *vid = 0; 443 tagged = false; 444 } else { 445 tagged = true; 446 } 447 } else { 448 /* Untagged frame */ 449 tagged = false; 450 } 451 452 if (!*vid) { 453 u16 pvid = br_get_pvid(vg); 454 455 /* Frame had a tag with VID 0 or did not have a tag. 456 * See if pvid is set on this port. That tells us which 457 * vlan untagged or priority-tagged traffic belongs to. 458 */ 459 if (!pvid) 460 goto drop; 461 462 /* PVID is set on this port. Any untagged or priority-tagged 463 * ingress frame is considered to belong to this vlan. 464 */ 465 *vid = pvid; 466 if (likely(!tagged)) 467 /* Untagged Frame. */ 468 __vlan_hwaccel_put_tag(skb, br->vlan_proto, pvid); 469 else 470 /* Priority-tagged Frame. 471 * At this point, We know that skb->vlan_tci had 472 * VLAN_TAG_PRESENT bit and its VID field was 0x000. 473 * We update only VID field and preserve PCP field. 474 */ 475 skb->vlan_tci |= pvid; 476 477 /* if stats are disabled we can avoid the lookup */ 478 if (!br->vlan_stats_enabled) 479 return true; 480 } 481 v = br_vlan_find(vg, *vid); 482 if (!v || !br_vlan_should_use(v)) 483 goto drop; 484 485 if (br->vlan_stats_enabled) { 486 stats = this_cpu_ptr(v->stats); 487 u64_stats_update_begin(&stats->syncp); 488 stats->rx_bytes += skb->len; 489 stats->rx_packets++; 490 u64_stats_update_end(&stats->syncp); 491 } 492 493 return true; 494 495 drop: 496 kfree_skb(skb); 497 return false; 498 } 499 500 bool br_allowed_ingress(const struct net_bridge *br, 501 struct net_bridge_vlan_group *vg, struct sk_buff *skb, 502 u16 *vid) 503 { 504 /* If VLAN filtering is disabled on the bridge, all packets are 505 * permitted. 506 */ 507 if (!br->vlan_enabled) { 508 BR_INPUT_SKB_CB(skb)->vlan_filtered = false; 509 return true; 510 } 511 512 return __allowed_ingress(br, vg, skb, vid); 513 } 514 515 /* Called under RCU. */ 516 bool br_allowed_egress(struct net_bridge_vlan_group *vg, 517 const struct sk_buff *skb) 518 { 519 const struct net_bridge_vlan *v; 520 u16 vid; 521 522 /* If this packet was not filtered at input, let it pass */ 523 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered) 524 return true; 525 526 br_vlan_get_tag(skb, &vid); 527 v = br_vlan_find(vg, vid); 528 if (v && br_vlan_should_use(v)) 529 return true; 530 531 return false; 532 } 533 534 /* Called under RCU */ 535 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid) 536 { 537 struct net_bridge_vlan_group *vg; 538 struct net_bridge *br = p->br; 539 540 /* If filtering was disabled at input, let it pass. */ 541 if (!br->vlan_enabled) 542 return true; 543 544 vg = nbp_vlan_group_rcu(p); 545 if (!vg || !vg->num_vlans) 546 return false; 547 548 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto) 549 *vid = 0; 550 551 if (!*vid) { 552 *vid = br_get_pvid(vg); 553 if (!*vid) 554 return false; 555 556 return true; 557 } 558 559 if (br_vlan_find(vg, *vid)) 560 return true; 561 562 return false; 563 } 564 565 static int br_vlan_add_existing(struct net_bridge *br, 566 struct net_bridge_vlan_group *vg, 567 struct net_bridge_vlan *vlan, 568 u16 flags, bool *changed) 569 { 570 int err; 571 572 err = br_switchdev_port_vlan_add(br->dev, vlan->vid, flags); 573 if (err && err != -EOPNOTSUPP) 574 return err; 575 576 if (!br_vlan_is_brentry(vlan)) { 577 /* Trying to change flags of non-existent bridge vlan */ 578 if (!(flags & BRIDGE_VLAN_INFO_BRENTRY)) { 579 err = -EINVAL; 580 goto err_flags; 581 } 582 /* It was only kept for port vlans, now make it real */ 583 err = br_fdb_insert(br, NULL, br->dev->dev_addr, 584 vlan->vid); 585 if (err) { 586 br_err(br, "failed to insert local address into bridge forwarding table\n"); 587 goto err_fdb_insert; 588 } 589 590 refcount_inc(&vlan->refcnt); 591 vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY; 592 vg->num_vlans++; 593 *changed = true; 594 } 595 596 if (__vlan_add_flags(vlan, flags)) 597 *changed = true; 598 599 return 0; 600 601 err_fdb_insert: 602 err_flags: 603 br_switchdev_port_vlan_del(br->dev, vlan->vid); 604 return err; 605 } 606 607 /* Must be protected by RTNL. 608 * Must be called with vid in range from 1 to 4094 inclusive. 609 * changed must be true only if the vlan was created or updated 610 */ 611 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags, bool *changed) 612 { 613 struct net_bridge_vlan_group *vg; 614 struct net_bridge_vlan *vlan; 615 int ret; 616 617 ASSERT_RTNL(); 618 619 *changed = false; 620 vg = br_vlan_group(br); 621 vlan = br_vlan_find(vg, vid); 622 if (vlan) 623 return br_vlan_add_existing(br, vg, vlan, flags, changed); 624 625 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL); 626 if (!vlan) 627 return -ENOMEM; 628 629 vlan->stats = netdev_alloc_pcpu_stats(struct br_vlan_stats); 630 if (!vlan->stats) { 631 kfree(vlan); 632 return -ENOMEM; 633 } 634 vlan->vid = vid; 635 vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER; 636 vlan->flags &= ~BRIDGE_VLAN_INFO_PVID; 637 vlan->br = br; 638 if (flags & BRIDGE_VLAN_INFO_BRENTRY) 639 refcount_set(&vlan->refcnt, 1); 640 ret = __vlan_add(vlan, flags); 641 if (ret) { 642 free_percpu(vlan->stats); 643 kfree(vlan); 644 } else { 645 *changed = true; 646 } 647 648 return ret; 649 } 650 651 /* Must be protected by RTNL. 652 * Must be called with vid in range from 1 to 4094 inclusive. 653 */ 654 int br_vlan_delete(struct net_bridge *br, u16 vid) 655 { 656 struct net_bridge_vlan_group *vg; 657 struct net_bridge_vlan *v; 658 659 ASSERT_RTNL(); 660 661 vg = br_vlan_group(br); 662 v = br_vlan_find(vg, vid); 663 if (!v || !br_vlan_is_brentry(v)) 664 return -ENOENT; 665 666 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid); 667 br_fdb_delete_by_port(br, NULL, vid, 0); 668 669 vlan_tunnel_info_del(vg, v); 670 671 return __vlan_del(v); 672 } 673 674 void br_vlan_flush(struct net_bridge *br) 675 { 676 struct net_bridge_vlan_group *vg; 677 678 ASSERT_RTNL(); 679 680 vg = br_vlan_group(br); 681 __vlan_flush(vg); 682 RCU_INIT_POINTER(br->vlgrp, NULL); 683 synchronize_rcu(); 684 __vlan_group_free(vg); 685 } 686 687 struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid) 688 { 689 if (!vg) 690 return NULL; 691 692 return br_vlan_lookup(&vg->vlan_hash, vid); 693 } 694 695 /* Must be protected by RTNL. */ 696 static void recalculate_group_addr(struct net_bridge *br) 697 { 698 if (br->group_addr_set) 699 return; 700 701 spin_lock_bh(&br->lock); 702 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) { 703 /* Bridge Group Address */ 704 br->group_addr[5] = 0x00; 705 } else { /* vlan_enabled && ETH_P_8021AD */ 706 /* Provider Bridge Group Address */ 707 br->group_addr[5] = 0x08; 708 } 709 spin_unlock_bh(&br->lock); 710 } 711 712 /* Must be protected by RTNL. */ 713 void br_recalculate_fwd_mask(struct net_bridge *br) 714 { 715 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) 716 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT; 717 else /* vlan_enabled && ETH_P_8021AD */ 718 br->group_fwd_mask_required = BR_GROUPFWD_8021AD & 719 ~(1u << br->group_addr[5]); 720 } 721 722 int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val) 723 { 724 struct switchdev_attr attr = { 725 .orig_dev = br->dev, 726 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING, 727 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP, 728 .u.vlan_filtering = val, 729 }; 730 int err; 731 732 if (br->vlan_enabled == val) 733 return 0; 734 735 err = switchdev_port_attr_set(br->dev, &attr); 736 if (err && err != -EOPNOTSUPP) 737 return err; 738 739 br->vlan_enabled = val; 740 br_manage_promisc(br); 741 recalculate_group_addr(br); 742 br_recalculate_fwd_mask(br); 743 744 return 0; 745 } 746 747 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val) 748 { 749 return __br_vlan_filter_toggle(br, val); 750 } 751 752 bool br_vlan_enabled(const struct net_device *dev) 753 { 754 struct net_bridge *br = netdev_priv(dev); 755 756 return !!br->vlan_enabled; 757 } 758 EXPORT_SYMBOL_GPL(br_vlan_enabled); 759 760 int __br_vlan_set_proto(struct net_bridge *br, __be16 proto) 761 { 762 int err = 0; 763 struct net_bridge_port *p; 764 struct net_bridge_vlan *vlan; 765 struct net_bridge_vlan_group *vg; 766 __be16 oldproto; 767 768 if (br->vlan_proto == proto) 769 return 0; 770 771 /* Add VLANs for the new proto to the device filter. */ 772 list_for_each_entry(p, &br->port_list, list) { 773 vg = nbp_vlan_group(p); 774 list_for_each_entry(vlan, &vg->vlan_list, vlist) { 775 err = vlan_vid_add(p->dev, proto, vlan->vid); 776 if (err) 777 goto err_filt; 778 } 779 } 780 781 oldproto = br->vlan_proto; 782 br->vlan_proto = proto; 783 784 recalculate_group_addr(br); 785 br_recalculate_fwd_mask(br); 786 787 /* Delete VLANs for the old proto from the device filter. */ 788 list_for_each_entry(p, &br->port_list, list) { 789 vg = nbp_vlan_group(p); 790 list_for_each_entry(vlan, &vg->vlan_list, vlist) 791 vlan_vid_del(p->dev, oldproto, vlan->vid); 792 } 793 794 return 0; 795 796 err_filt: 797 list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist) 798 vlan_vid_del(p->dev, proto, vlan->vid); 799 800 list_for_each_entry_continue_reverse(p, &br->port_list, list) { 801 vg = nbp_vlan_group(p); 802 list_for_each_entry(vlan, &vg->vlan_list, vlist) 803 vlan_vid_del(p->dev, proto, vlan->vid); 804 } 805 806 return err; 807 } 808 809 int br_vlan_set_proto(struct net_bridge *br, unsigned long val) 810 { 811 if (val != ETH_P_8021Q && val != ETH_P_8021AD) 812 return -EPROTONOSUPPORT; 813 814 return __br_vlan_set_proto(br, htons(val)); 815 } 816 817 int br_vlan_set_stats(struct net_bridge *br, unsigned long val) 818 { 819 switch (val) { 820 case 0: 821 case 1: 822 br->vlan_stats_enabled = val; 823 break; 824 default: 825 return -EINVAL; 826 } 827 828 return 0; 829 } 830 831 static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid) 832 { 833 struct net_bridge_vlan *v; 834 835 if (vid != vg->pvid) 836 return false; 837 838 v = br_vlan_lookup(&vg->vlan_hash, vid); 839 if (v && br_vlan_should_use(v) && 840 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)) 841 return true; 842 843 return false; 844 } 845 846 static void br_vlan_disable_default_pvid(struct net_bridge *br) 847 { 848 struct net_bridge_port *p; 849 u16 pvid = br->default_pvid; 850 851 /* Disable default_pvid on all ports where it is still 852 * configured. 853 */ 854 if (vlan_default_pvid(br_vlan_group(br), pvid)) 855 br_vlan_delete(br, pvid); 856 857 list_for_each_entry(p, &br->port_list, list) { 858 if (vlan_default_pvid(nbp_vlan_group(p), pvid)) 859 nbp_vlan_delete(p, pvid); 860 } 861 862 br->default_pvid = 0; 863 } 864 865 int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid) 866 { 867 const struct net_bridge_vlan *pvent; 868 struct net_bridge_vlan_group *vg; 869 struct net_bridge_port *p; 870 unsigned long *changed; 871 bool vlchange; 872 u16 old_pvid; 873 int err = 0; 874 875 if (!pvid) { 876 br_vlan_disable_default_pvid(br); 877 return 0; 878 } 879 880 changed = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long), 881 GFP_KERNEL); 882 if (!changed) 883 return -ENOMEM; 884 885 old_pvid = br->default_pvid; 886 887 /* Update default_pvid config only if we do not conflict with 888 * user configuration. 889 */ 890 vg = br_vlan_group(br); 891 pvent = br_vlan_find(vg, pvid); 892 if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) && 893 (!pvent || !br_vlan_should_use(pvent))) { 894 err = br_vlan_add(br, pvid, 895 BRIDGE_VLAN_INFO_PVID | 896 BRIDGE_VLAN_INFO_UNTAGGED | 897 BRIDGE_VLAN_INFO_BRENTRY, 898 &vlchange); 899 if (err) 900 goto out; 901 br_vlan_delete(br, old_pvid); 902 set_bit(0, changed); 903 } 904 905 list_for_each_entry(p, &br->port_list, list) { 906 /* Update default_pvid config only if we do not conflict with 907 * user configuration. 908 */ 909 vg = nbp_vlan_group(p); 910 if ((old_pvid && 911 !vlan_default_pvid(vg, old_pvid)) || 912 br_vlan_find(vg, pvid)) 913 continue; 914 915 err = nbp_vlan_add(p, pvid, 916 BRIDGE_VLAN_INFO_PVID | 917 BRIDGE_VLAN_INFO_UNTAGGED, 918 &vlchange); 919 if (err) 920 goto err_port; 921 nbp_vlan_delete(p, old_pvid); 922 set_bit(p->port_no, changed); 923 } 924 925 br->default_pvid = pvid; 926 927 out: 928 kfree(changed); 929 return err; 930 931 err_port: 932 list_for_each_entry_continue_reverse(p, &br->port_list, list) { 933 if (!test_bit(p->port_no, changed)) 934 continue; 935 936 if (old_pvid) 937 nbp_vlan_add(p, old_pvid, 938 BRIDGE_VLAN_INFO_PVID | 939 BRIDGE_VLAN_INFO_UNTAGGED, 940 &vlchange); 941 nbp_vlan_delete(p, pvid); 942 } 943 944 if (test_bit(0, changed)) { 945 if (old_pvid) 946 br_vlan_add(br, old_pvid, 947 BRIDGE_VLAN_INFO_PVID | 948 BRIDGE_VLAN_INFO_UNTAGGED | 949 BRIDGE_VLAN_INFO_BRENTRY, 950 &vlchange); 951 br_vlan_delete(br, pvid); 952 } 953 goto out; 954 } 955 956 int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val) 957 { 958 u16 pvid = val; 959 int err = 0; 960 961 if (val >= VLAN_VID_MASK) 962 return -EINVAL; 963 964 if (pvid == br->default_pvid) 965 goto out; 966 967 /* Only allow default pvid change when filtering is disabled */ 968 if (br->vlan_enabled) { 969 pr_info_once("Please disable vlan filtering to change default_pvid\n"); 970 err = -EPERM; 971 goto out; 972 } 973 err = __br_vlan_set_default_pvid(br, pvid); 974 out: 975 return err; 976 } 977 978 int br_vlan_init(struct net_bridge *br) 979 { 980 struct net_bridge_vlan_group *vg; 981 int ret = -ENOMEM; 982 bool changed; 983 984 vg = kzalloc(sizeof(*vg), GFP_KERNEL); 985 if (!vg) 986 goto out; 987 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params); 988 if (ret) 989 goto err_rhtbl; 990 ret = vlan_tunnel_init(vg); 991 if (ret) 992 goto err_tunnel_init; 993 INIT_LIST_HEAD(&vg->vlan_list); 994 br->vlan_proto = htons(ETH_P_8021Q); 995 br->default_pvid = 1; 996 rcu_assign_pointer(br->vlgrp, vg); 997 ret = br_vlan_add(br, 1, 998 BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED | 999 BRIDGE_VLAN_INFO_BRENTRY, &changed); 1000 if (ret) 1001 goto err_vlan_add; 1002 1003 out: 1004 return ret; 1005 1006 err_vlan_add: 1007 vlan_tunnel_deinit(vg); 1008 err_tunnel_init: 1009 rhashtable_destroy(&vg->vlan_hash); 1010 err_rhtbl: 1011 kfree(vg); 1012 1013 goto out; 1014 } 1015 1016 int nbp_vlan_init(struct net_bridge_port *p) 1017 { 1018 struct switchdev_attr attr = { 1019 .orig_dev = p->br->dev, 1020 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING, 1021 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP, 1022 .u.vlan_filtering = p->br->vlan_enabled, 1023 }; 1024 struct net_bridge_vlan_group *vg; 1025 int ret = -ENOMEM; 1026 1027 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL); 1028 if (!vg) 1029 goto out; 1030 1031 ret = switchdev_port_attr_set(p->dev, &attr); 1032 if (ret && ret != -EOPNOTSUPP) 1033 goto err_vlan_enabled; 1034 1035 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params); 1036 if (ret) 1037 goto err_rhtbl; 1038 ret = vlan_tunnel_init(vg); 1039 if (ret) 1040 goto err_tunnel_init; 1041 INIT_LIST_HEAD(&vg->vlan_list); 1042 rcu_assign_pointer(p->vlgrp, vg); 1043 if (p->br->default_pvid) { 1044 bool changed; 1045 1046 ret = nbp_vlan_add(p, p->br->default_pvid, 1047 BRIDGE_VLAN_INFO_PVID | 1048 BRIDGE_VLAN_INFO_UNTAGGED, 1049 &changed); 1050 if (ret) 1051 goto err_vlan_add; 1052 } 1053 out: 1054 return ret; 1055 1056 err_vlan_add: 1057 RCU_INIT_POINTER(p->vlgrp, NULL); 1058 synchronize_rcu(); 1059 vlan_tunnel_deinit(vg); 1060 err_tunnel_init: 1061 rhashtable_destroy(&vg->vlan_hash); 1062 err_rhtbl: 1063 err_vlan_enabled: 1064 kfree(vg); 1065 1066 goto out; 1067 } 1068 1069 /* Must be protected by RTNL. 1070 * Must be called with vid in range from 1 to 4094 inclusive. 1071 * changed must be true only if the vlan was created or updated 1072 */ 1073 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags, 1074 bool *changed) 1075 { 1076 struct net_bridge_vlan *vlan; 1077 int ret; 1078 1079 ASSERT_RTNL(); 1080 1081 *changed = false; 1082 vlan = br_vlan_find(nbp_vlan_group(port), vid); 1083 if (vlan) { 1084 /* Pass the flags to the hardware bridge */ 1085 ret = br_switchdev_port_vlan_add(port->dev, vid, flags); 1086 if (ret && ret != -EOPNOTSUPP) 1087 return ret; 1088 *changed = __vlan_add_flags(vlan, flags); 1089 1090 return 0; 1091 } 1092 1093 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL); 1094 if (!vlan) 1095 return -ENOMEM; 1096 1097 vlan->vid = vid; 1098 vlan->port = port; 1099 ret = __vlan_add(vlan, flags); 1100 if (ret) 1101 kfree(vlan); 1102 else 1103 *changed = true; 1104 1105 return ret; 1106 } 1107 1108 /* Must be protected by RTNL. 1109 * Must be called with vid in range from 1 to 4094 inclusive. 1110 */ 1111 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid) 1112 { 1113 struct net_bridge_vlan *v; 1114 1115 ASSERT_RTNL(); 1116 1117 v = br_vlan_find(nbp_vlan_group(port), vid); 1118 if (!v) 1119 return -ENOENT; 1120 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid); 1121 br_fdb_delete_by_port(port->br, port, vid, 0); 1122 1123 return __vlan_del(v); 1124 } 1125 1126 void nbp_vlan_flush(struct net_bridge_port *port) 1127 { 1128 struct net_bridge_vlan_group *vg; 1129 1130 ASSERT_RTNL(); 1131 1132 vg = nbp_vlan_group(port); 1133 __vlan_flush(vg); 1134 RCU_INIT_POINTER(port->vlgrp, NULL); 1135 synchronize_rcu(); 1136 __vlan_group_free(vg); 1137 } 1138 1139 void br_vlan_get_stats(const struct net_bridge_vlan *v, 1140 struct br_vlan_stats *stats) 1141 { 1142 int i; 1143 1144 memset(stats, 0, sizeof(*stats)); 1145 for_each_possible_cpu(i) { 1146 u64 rxpackets, rxbytes, txpackets, txbytes; 1147 struct br_vlan_stats *cpu_stats; 1148 unsigned int start; 1149 1150 cpu_stats = per_cpu_ptr(v->stats, i); 1151 do { 1152 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp); 1153 rxpackets = cpu_stats->rx_packets; 1154 rxbytes = cpu_stats->rx_bytes; 1155 txbytes = cpu_stats->tx_bytes; 1156 txpackets = cpu_stats->tx_packets; 1157 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start)); 1158 1159 stats->rx_packets += rxpackets; 1160 stats->rx_bytes += rxbytes; 1161 stats->tx_bytes += txbytes; 1162 stats->tx_packets += txpackets; 1163 } 1164 } 1165 1166 int br_vlan_get_pvid(const struct net_device *dev, u16 *p_pvid) 1167 { 1168 struct net_bridge_vlan_group *vg; 1169 1170 ASSERT_RTNL(); 1171 if (netif_is_bridge_master(dev)) 1172 vg = br_vlan_group(netdev_priv(dev)); 1173 else 1174 return -EINVAL; 1175 1176 *p_pvid = br_get_pvid(vg); 1177 return 0; 1178 } 1179 EXPORT_SYMBOL_GPL(br_vlan_get_pvid); 1180 1181 int br_vlan_get_info(const struct net_device *dev, u16 vid, 1182 struct bridge_vlan_info *p_vinfo) 1183 { 1184 struct net_bridge_vlan_group *vg; 1185 struct net_bridge_vlan *v; 1186 struct net_bridge_port *p; 1187 1188 ASSERT_RTNL(); 1189 p = br_port_get_check_rtnl(dev); 1190 if (p) 1191 vg = nbp_vlan_group(p); 1192 else if (netif_is_bridge_master(dev)) 1193 vg = br_vlan_group(netdev_priv(dev)); 1194 else 1195 return -EINVAL; 1196 1197 v = br_vlan_find(vg, vid); 1198 if (!v) 1199 return -ENOENT; 1200 1201 p_vinfo->vid = vid; 1202 p_vinfo->flags = v->flags; 1203 return 0; 1204 } 1205 EXPORT_SYMBOL_GPL(br_vlan_get_info); 1206