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 9 static void __vlan_add_pvid(struct net_port_vlans *v, u16 vid) 10 { 11 if (v->pvid == vid) 12 return; 13 14 smp_wmb(); 15 v->pvid = vid; 16 } 17 18 static void __vlan_delete_pvid(struct net_port_vlans *v, u16 vid) 19 { 20 if (v->pvid != vid) 21 return; 22 23 smp_wmb(); 24 v->pvid = 0; 25 } 26 27 static void __vlan_add_flags(struct net_port_vlans *v, u16 vid, u16 flags) 28 { 29 if (flags & BRIDGE_VLAN_INFO_PVID) 30 __vlan_add_pvid(v, vid); 31 else 32 __vlan_delete_pvid(v, vid); 33 34 if (flags & BRIDGE_VLAN_INFO_UNTAGGED) 35 set_bit(vid, v->untagged_bitmap); 36 else 37 clear_bit(vid, v->untagged_bitmap); 38 } 39 40 static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br, 41 u16 vid, u16 flags) 42 { 43 const struct net_device_ops *ops = dev->netdev_ops; 44 int err; 45 46 /* If driver uses VLAN ndo ops, use 8021q to install vid 47 * on device, otherwise try switchdev ops to install vid. 48 */ 49 50 if (ops->ndo_vlan_rx_add_vid) { 51 err = vlan_vid_add(dev, br->vlan_proto, vid); 52 } else { 53 struct switchdev_obj vlan_obj = { 54 .id = SWITCHDEV_OBJ_PORT_VLAN, 55 .u.vlan = { 56 .flags = flags, 57 .vid_begin = vid, 58 .vid_end = vid, 59 }, 60 }; 61 62 err = switchdev_port_obj_add(dev, &vlan_obj); 63 if (err == -EOPNOTSUPP) 64 err = 0; 65 } 66 67 return err; 68 } 69 70 static int __vlan_add(struct net_port_vlans *v, u16 vid, u16 flags) 71 { 72 struct net_bridge_port *p = NULL; 73 struct net_bridge *br; 74 struct net_device *dev; 75 int err; 76 77 if (test_bit(vid, v->vlan_bitmap)) { 78 __vlan_add_flags(v, vid, flags); 79 return 0; 80 } 81 82 if (v->port_idx) { 83 p = v->parent.port; 84 br = p->br; 85 dev = p->dev; 86 } else { 87 br = v->parent.br; 88 dev = br->dev; 89 } 90 91 if (p) { 92 /* Add VLAN to the device filter if it is supported. 93 * This ensures tagged traffic enters the bridge when 94 * promiscuous mode is disabled by br_manage_promisc(). 95 */ 96 err = __vlan_vid_add(dev, br, vid, flags); 97 if (err) 98 return err; 99 } 100 101 err = br_fdb_insert(br, p, dev->dev_addr, vid); 102 if (err) { 103 br_err(br, "failed insert local address into bridge " 104 "forwarding table\n"); 105 goto out_filt; 106 } 107 108 set_bit(vid, v->vlan_bitmap); 109 v->num_vlans++; 110 __vlan_add_flags(v, vid, flags); 111 112 return 0; 113 114 out_filt: 115 if (p) 116 vlan_vid_del(dev, br->vlan_proto, vid); 117 return err; 118 } 119 120 static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br, 121 u16 vid) 122 { 123 const struct net_device_ops *ops = dev->netdev_ops; 124 int err = 0; 125 126 /* If driver uses VLAN ndo ops, use 8021q to delete vid 127 * on device, otherwise try switchdev ops to delete vid. 128 */ 129 130 if (ops->ndo_vlan_rx_kill_vid) { 131 vlan_vid_del(dev, br->vlan_proto, vid); 132 } else { 133 struct switchdev_obj vlan_obj = { 134 .id = SWITCHDEV_OBJ_PORT_VLAN, 135 .u.vlan = { 136 .vid_begin = vid, 137 .vid_end = vid, 138 }, 139 }; 140 141 err = switchdev_port_obj_del(dev, &vlan_obj); 142 if (err == -EOPNOTSUPP) 143 err = 0; 144 } 145 146 return err; 147 } 148 149 static int __vlan_del(struct net_port_vlans *v, u16 vid) 150 { 151 if (!test_bit(vid, v->vlan_bitmap)) 152 return -EINVAL; 153 154 __vlan_delete_pvid(v, vid); 155 clear_bit(vid, v->untagged_bitmap); 156 157 if (v->port_idx) { 158 struct net_bridge_port *p = v->parent.port; 159 int err; 160 161 err = __vlan_vid_del(p->dev, p->br, vid); 162 if (err) 163 return err; 164 } 165 166 clear_bit(vid, v->vlan_bitmap); 167 v->num_vlans--; 168 if (bitmap_empty(v->vlan_bitmap, VLAN_N_VID)) { 169 if (v->port_idx) 170 RCU_INIT_POINTER(v->parent.port->vlan_info, NULL); 171 else 172 RCU_INIT_POINTER(v->parent.br->vlan_info, NULL); 173 kfree_rcu(v, rcu); 174 } 175 return 0; 176 } 177 178 static void __vlan_flush(struct net_port_vlans *v) 179 { 180 smp_wmb(); 181 v->pvid = 0; 182 bitmap_zero(v->vlan_bitmap, VLAN_N_VID); 183 if (v->port_idx) 184 RCU_INIT_POINTER(v->parent.port->vlan_info, NULL); 185 else 186 RCU_INIT_POINTER(v->parent.br->vlan_info, NULL); 187 kfree_rcu(v, rcu); 188 } 189 190 struct sk_buff *br_handle_vlan(struct net_bridge *br, 191 const struct net_port_vlans *pv, 192 struct sk_buff *skb) 193 { 194 u16 vid; 195 196 /* If this packet was not filtered at input, let it pass */ 197 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered) 198 goto out; 199 200 /* Vlan filter table must be configured at this point. The 201 * only exception is the bridge is set in promisc mode and the 202 * packet is destined for the bridge device. In this case 203 * pass the packet as is. 204 */ 205 if (!pv) { 206 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) { 207 goto out; 208 } else { 209 kfree_skb(skb); 210 return NULL; 211 } 212 } 213 214 /* At this point, we know that the frame was filtered and contains 215 * a valid vlan id. If the vlan id is set in the untagged bitmap, 216 * send untagged; otherwise, send tagged. 217 */ 218 br_vlan_get_tag(skb, &vid); 219 if (test_bit(vid, pv->untagged_bitmap)) 220 skb->vlan_tci = 0; 221 222 out: 223 return skb; 224 } 225 226 /* Called under RCU */ 227 bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v, 228 struct sk_buff *skb, u16 *vid) 229 { 230 bool tagged; 231 __be16 proto; 232 233 /* If VLAN filtering is disabled on the bridge, all packets are 234 * permitted. 235 */ 236 if (!br->vlan_enabled) { 237 BR_INPUT_SKB_CB(skb)->vlan_filtered = false; 238 return true; 239 } 240 241 /* If there are no vlan in the permitted list, all packets are 242 * rejected. 243 */ 244 if (!v) 245 goto drop; 246 247 BR_INPUT_SKB_CB(skb)->vlan_filtered = true; 248 proto = br->vlan_proto; 249 250 /* If vlan tx offload is disabled on bridge device and frame was 251 * sent from vlan device on the bridge device, it does not have 252 * HW accelerated vlan tag. 253 */ 254 if (unlikely(!skb_vlan_tag_present(skb) && 255 skb->protocol == proto)) { 256 skb = skb_vlan_untag(skb); 257 if (unlikely(!skb)) 258 return false; 259 } 260 261 if (!br_vlan_get_tag(skb, vid)) { 262 /* Tagged frame */ 263 if (skb->vlan_proto != proto) { 264 /* Protocol-mismatch, empty out vlan_tci for new tag */ 265 skb_push(skb, ETH_HLEN); 266 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto, 267 skb_vlan_tag_get(skb)); 268 if (unlikely(!skb)) 269 return false; 270 271 skb_pull(skb, ETH_HLEN); 272 skb_reset_mac_len(skb); 273 *vid = 0; 274 tagged = false; 275 } else { 276 tagged = true; 277 } 278 } else { 279 /* Untagged frame */ 280 tagged = false; 281 } 282 283 if (!*vid) { 284 u16 pvid = br_get_pvid(v); 285 286 /* Frame had a tag with VID 0 or did not have a tag. 287 * See if pvid is set on this port. That tells us which 288 * vlan untagged or priority-tagged traffic belongs to. 289 */ 290 if (!pvid) 291 goto drop; 292 293 /* PVID is set on this port. Any untagged or priority-tagged 294 * ingress frame is considered to belong to this vlan. 295 */ 296 *vid = pvid; 297 if (likely(!tagged)) 298 /* Untagged Frame. */ 299 __vlan_hwaccel_put_tag(skb, proto, pvid); 300 else 301 /* Priority-tagged Frame. 302 * At this point, We know that skb->vlan_tci had 303 * VLAN_TAG_PRESENT bit and its VID field was 0x000. 304 * We update only VID field and preserve PCP field. 305 */ 306 skb->vlan_tci |= pvid; 307 308 return true; 309 } 310 311 /* Frame had a valid vlan tag. See if vlan is allowed */ 312 if (test_bit(*vid, v->vlan_bitmap)) 313 return true; 314 drop: 315 kfree_skb(skb); 316 return false; 317 } 318 319 /* Called under RCU. */ 320 bool br_allowed_egress(struct net_bridge *br, 321 const struct net_port_vlans *v, 322 const struct sk_buff *skb) 323 { 324 u16 vid; 325 326 /* If this packet was not filtered at input, let it pass */ 327 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered) 328 return true; 329 330 if (!v) 331 return false; 332 333 br_vlan_get_tag(skb, &vid); 334 if (test_bit(vid, v->vlan_bitmap)) 335 return true; 336 337 return false; 338 } 339 340 /* Called under RCU */ 341 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid) 342 { 343 struct net_bridge *br = p->br; 344 struct net_port_vlans *v; 345 346 /* If filtering was disabled at input, let it pass. */ 347 if (!br->vlan_enabled) 348 return true; 349 350 v = rcu_dereference(p->vlan_info); 351 if (!v) 352 return false; 353 354 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto) 355 *vid = 0; 356 357 if (!*vid) { 358 *vid = br_get_pvid(v); 359 if (!*vid) 360 return false; 361 362 return true; 363 } 364 365 if (test_bit(*vid, v->vlan_bitmap)) 366 return true; 367 368 return false; 369 } 370 371 /* Must be protected by RTNL. 372 * Must be called with vid in range from 1 to 4094 inclusive. 373 */ 374 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags) 375 { 376 struct net_port_vlans *pv = NULL; 377 int err; 378 379 ASSERT_RTNL(); 380 381 pv = rtnl_dereference(br->vlan_info); 382 if (pv) 383 return __vlan_add(pv, vid, flags); 384 385 /* Create port vlan infomration 386 */ 387 pv = kzalloc(sizeof(*pv), GFP_KERNEL); 388 if (!pv) 389 return -ENOMEM; 390 391 pv->parent.br = br; 392 err = __vlan_add(pv, vid, flags); 393 if (err) 394 goto out; 395 396 rcu_assign_pointer(br->vlan_info, pv); 397 return 0; 398 out: 399 kfree(pv); 400 return err; 401 } 402 403 /* Must be protected by RTNL. 404 * Must be called with vid in range from 1 to 4094 inclusive. 405 */ 406 int br_vlan_delete(struct net_bridge *br, u16 vid) 407 { 408 struct net_port_vlans *pv; 409 410 ASSERT_RTNL(); 411 412 pv = rtnl_dereference(br->vlan_info); 413 if (!pv) 414 return -EINVAL; 415 416 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid); 417 418 __vlan_del(pv, vid); 419 return 0; 420 } 421 422 void br_vlan_flush(struct net_bridge *br) 423 { 424 struct net_port_vlans *pv; 425 426 ASSERT_RTNL(); 427 pv = rtnl_dereference(br->vlan_info); 428 if (!pv) 429 return; 430 431 __vlan_flush(pv); 432 } 433 434 bool br_vlan_find(struct net_bridge *br, u16 vid) 435 { 436 struct net_port_vlans *pv; 437 bool found = false; 438 439 rcu_read_lock(); 440 pv = rcu_dereference(br->vlan_info); 441 442 if (!pv) 443 goto out; 444 445 if (test_bit(vid, pv->vlan_bitmap)) 446 found = true; 447 448 out: 449 rcu_read_unlock(); 450 return found; 451 } 452 453 /* Must be protected by RTNL. */ 454 static void recalculate_group_addr(struct net_bridge *br) 455 { 456 if (br->group_addr_set) 457 return; 458 459 spin_lock_bh(&br->lock); 460 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) { 461 /* Bridge Group Address */ 462 br->group_addr[5] = 0x00; 463 } else { /* vlan_enabled && ETH_P_8021AD */ 464 /* Provider Bridge Group Address */ 465 br->group_addr[5] = 0x08; 466 } 467 spin_unlock_bh(&br->lock); 468 } 469 470 /* Must be protected by RTNL. */ 471 void br_recalculate_fwd_mask(struct net_bridge *br) 472 { 473 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) 474 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT; 475 else /* vlan_enabled && ETH_P_8021AD */ 476 br->group_fwd_mask_required = BR_GROUPFWD_8021AD & 477 ~(1u << br->group_addr[5]); 478 } 479 480 int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val) 481 { 482 if (br->vlan_enabled == val) 483 return 0; 484 485 br->vlan_enabled = val; 486 br_manage_promisc(br); 487 recalculate_group_addr(br); 488 br_recalculate_fwd_mask(br); 489 490 return 0; 491 } 492 493 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val) 494 { 495 if (!rtnl_trylock()) 496 return restart_syscall(); 497 498 __br_vlan_filter_toggle(br, val); 499 rtnl_unlock(); 500 501 return 0; 502 } 503 504 int __br_vlan_set_proto(struct net_bridge *br, __be16 proto) 505 { 506 int err = 0; 507 struct net_bridge_port *p; 508 struct net_port_vlans *pv; 509 __be16 oldproto; 510 u16 vid, errvid; 511 512 if (br->vlan_proto == proto) 513 return 0; 514 515 /* Add VLANs for the new proto to the device filter. */ 516 list_for_each_entry(p, &br->port_list, list) { 517 pv = rtnl_dereference(p->vlan_info); 518 if (!pv) 519 continue; 520 521 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) { 522 err = vlan_vid_add(p->dev, proto, vid); 523 if (err) 524 goto err_filt; 525 } 526 } 527 528 oldproto = br->vlan_proto; 529 br->vlan_proto = proto; 530 531 recalculate_group_addr(br); 532 br_recalculate_fwd_mask(br); 533 534 /* Delete VLANs for the old proto from the device filter. */ 535 list_for_each_entry(p, &br->port_list, list) { 536 pv = rtnl_dereference(p->vlan_info); 537 if (!pv) 538 continue; 539 540 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) 541 vlan_vid_del(p->dev, oldproto, vid); 542 } 543 544 return 0; 545 546 err_filt: 547 errvid = vid; 548 for_each_set_bit(vid, pv->vlan_bitmap, errvid) 549 vlan_vid_del(p->dev, proto, vid); 550 551 list_for_each_entry_continue_reverse(p, &br->port_list, list) { 552 pv = rtnl_dereference(p->vlan_info); 553 if (!pv) 554 continue; 555 556 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) 557 vlan_vid_del(p->dev, proto, vid); 558 } 559 560 return err; 561 } 562 563 int br_vlan_set_proto(struct net_bridge *br, unsigned long val) 564 { 565 int err; 566 567 if (val != ETH_P_8021Q && val != ETH_P_8021AD) 568 return -EPROTONOSUPPORT; 569 570 if (!rtnl_trylock()) 571 return restart_syscall(); 572 573 err = __br_vlan_set_proto(br, htons(val)); 574 rtnl_unlock(); 575 576 return err; 577 } 578 579 static bool vlan_default_pvid(struct net_port_vlans *pv, u16 vid) 580 { 581 return pv && vid == pv->pvid && test_bit(vid, pv->untagged_bitmap); 582 } 583 584 static void br_vlan_disable_default_pvid(struct net_bridge *br) 585 { 586 struct net_bridge_port *p; 587 u16 pvid = br->default_pvid; 588 589 /* Disable default_pvid on all ports where it is still 590 * configured. 591 */ 592 if (vlan_default_pvid(br_get_vlan_info(br), pvid)) 593 br_vlan_delete(br, pvid); 594 595 list_for_each_entry(p, &br->port_list, list) { 596 if (vlan_default_pvid(nbp_get_vlan_info(p), pvid)) 597 nbp_vlan_delete(p, pvid); 598 } 599 600 br->default_pvid = 0; 601 } 602 603 static int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid) 604 { 605 struct net_bridge_port *p; 606 u16 old_pvid; 607 int err = 0; 608 unsigned long *changed; 609 610 changed = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long), 611 GFP_KERNEL); 612 if (!changed) 613 return -ENOMEM; 614 615 old_pvid = br->default_pvid; 616 617 /* Update default_pvid config only if we do not conflict with 618 * user configuration. 619 */ 620 if ((!old_pvid || vlan_default_pvid(br_get_vlan_info(br), old_pvid)) && 621 !br_vlan_find(br, pvid)) { 622 err = br_vlan_add(br, pvid, 623 BRIDGE_VLAN_INFO_PVID | 624 BRIDGE_VLAN_INFO_UNTAGGED); 625 if (err) 626 goto out; 627 br_vlan_delete(br, old_pvid); 628 set_bit(0, changed); 629 } 630 631 list_for_each_entry(p, &br->port_list, list) { 632 /* Update default_pvid config only if we do not conflict with 633 * user configuration. 634 */ 635 if ((old_pvid && 636 !vlan_default_pvid(nbp_get_vlan_info(p), old_pvid)) || 637 nbp_vlan_find(p, pvid)) 638 continue; 639 640 err = nbp_vlan_add(p, pvid, 641 BRIDGE_VLAN_INFO_PVID | 642 BRIDGE_VLAN_INFO_UNTAGGED); 643 if (err) 644 goto err_port; 645 nbp_vlan_delete(p, old_pvid); 646 set_bit(p->port_no, changed); 647 } 648 649 br->default_pvid = pvid; 650 651 out: 652 kfree(changed); 653 return err; 654 655 err_port: 656 list_for_each_entry_continue_reverse(p, &br->port_list, list) { 657 if (!test_bit(p->port_no, changed)) 658 continue; 659 660 if (old_pvid) 661 nbp_vlan_add(p, old_pvid, 662 BRIDGE_VLAN_INFO_PVID | 663 BRIDGE_VLAN_INFO_UNTAGGED); 664 nbp_vlan_delete(p, pvid); 665 } 666 667 if (test_bit(0, changed)) { 668 if (old_pvid) 669 br_vlan_add(br, old_pvid, 670 BRIDGE_VLAN_INFO_PVID | 671 BRIDGE_VLAN_INFO_UNTAGGED); 672 br_vlan_delete(br, pvid); 673 } 674 goto out; 675 } 676 677 int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val) 678 { 679 u16 pvid = val; 680 int err = 0; 681 682 if (val >= VLAN_VID_MASK) 683 return -EINVAL; 684 685 if (!rtnl_trylock()) 686 return restart_syscall(); 687 688 if (pvid == br->default_pvid) 689 goto unlock; 690 691 /* Only allow default pvid change when filtering is disabled */ 692 if (br->vlan_enabled) { 693 pr_info_once("Please disable vlan filtering to change default_pvid\n"); 694 err = -EPERM; 695 goto unlock; 696 } 697 698 if (!pvid) 699 br_vlan_disable_default_pvid(br); 700 else 701 err = __br_vlan_set_default_pvid(br, pvid); 702 703 unlock: 704 rtnl_unlock(); 705 return err; 706 } 707 708 int br_vlan_init(struct net_bridge *br) 709 { 710 br->vlan_proto = htons(ETH_P_8021Q); 711 br->default_pvid = 1; 712 return br_vlan_add(br, 1, 713 BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED); 714 } 715 716 /* Must be protected by RTNL. 717 * Must be called with vid in range from 1 to 4094 inclusive. 718 */ 719 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags) 720 { 721 struct net_port_vlans *pv = NULL; 722 int err; 723 724 ASSERT_RTNL(); 725 726 pv = rtnl_dereference(port->vlan_info); 727 if (pv) 728 return __vlan_add(pv, vid, flags); 729 730 /* Create port vlan infomration 731 */ 732 pv = kzalloc(sizeof(*pv), GFP_KERNEL); 733 if (!pv) { 734 err = -ENOMEM; 735 goto clean_up; 736 } 737 738 pv->port_idx = port->port_no; 739 pv->parent.port = port; 740 err = __vlan_add(pv, vid, flags); 741 if (err) 742 goto clean_up; 743 744 rcu_assign_pointer(port->vlan_info, pv); 745 return 0; 746 747 clean_up: 748 kfree(pv); 749 return err; 750 } 751 752 /* Must be protected by RTNL. 753 * Must be called with vid in range from 1 to 4094 inclusive. 754 */ 755 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid) 756 { 757 struct net_port_vlans *pv; 758 759 ASSERT_RTNL(); 760 761 pv = rtnl_dereference(port->vlan_info); 762 if (!pv) 763 return -EINVAL; 764 765 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid); 766 br_fdb_delete_by_port(port->br, port, vid, 0); 767 768 return __vlan_del(pv, vid); 769 } 770 771 void nbp_vlan_flush(struct net_bridge_port *port) 772 { 773 struct net_port_vlans *pv; 774 u16 vid; 775 776 ASSERT_RTNL(); 777 778 pv = rtnl_dereference(port->vlan_info); 779 if (!pv) 780 return; 781 782 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) 783 vlan_vid_del(port->dev, port->br->vlan_proto, vid); 784 785 __vlan_flush(pv); 786 } 787 788 bool nbp_vlan_find(struct net_bridge_port *port, u16 vid) 789 { 790 struct net_port_vlans *pv; 791 bool found = false; 792 793 rcu_read_lock(); 794 pv = rcu_dereference(port->vlan_info); 795 796 if (!pv) 797 goto out; 798 799 if (test_bit(vid, pv->vlan_bitmap)) 800 found = true; 801 802 out: 803 rcu_read_unlock(); 804 return found; 805 } 806 807 int nbp_vlan_init(struct net_bridge_port *p) 808 { 809 return p->br->default_pvid ? 810 nbp_vlan_add(p, p->br->default_pvid, 811 BRIDGE_VLAN_INFO_PVID | 812 BRIDGE_VLAN_INFO_UNTAGGED) : 813 0; 814 } 815