1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * INET 802.1Q VLAN 4 * Ethernet-type device handling. 5 * 6 * Authors: Ben Greear <greearb@candelatech.com> 7 * Please send support related email to: netdev@vger.kernel.org 8 * VLAN Home Page: http://www.candelatech.com/~greear/vlan.html 9 * 10 * Fixes: 11 * Fix for packet capture - Nick Eggleston <nick@dccinc.com>; 12 * Add HW acceleration hooks - David S. Miller <davem@redhat.com>; 13 * Correct all the locking - David S. Miller <davem@redhat.com>; 14 * Use hash table for VLAN groups - David S. Miller <davem@redhat.com> 15 */ 16 17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 18 19 #include <linux/capability.h> 20 #include <linux/module.h> 21 #include <linux/netdevice.h> 22 #include <linux/skbuff.h> 23 #include <linux/slab.h> 24 #include <linux/init.h> 25 #include <linux/rculist.h> 26 #include <net/arp.h> 27 #include <linux/rtnetlink.h> 28 #include <linux/notifier.h> 29 #include <net/rtnetlink.h> 30 #include <net/net_namespace.h> 31 #include <net/netns/generic.h> 32 #include <linux/uaccess.h> 33 34 #include <linux/if_vlan.h> 35 #include "vlan.h" 36 #include "vlanproc.h" 37 38 #define DRV_VERSION "1.8" 39 40 /* Global VLAN variables */ 41 42 unsigned int vlan_net_id __read_mostly; 43 44 const char vlan_fullname[] = "802.1Q VLAN Support"; 45 const char vlan_version[] = DRV_VERSION; 46 47 /* End of global variables definitions. */ 48 49 static int vlan_group_prealloc_vid(struct vlan_group *vg, 50 __be16 vlan_proto, u16 vlan_id) 51 { 52 struct net_device **array; 53 unsigned int vidx; 54 unsigned int size; 55 int pidx; 56 57 ASSERT_RTNL(); 58 59 pidx = vlan_proto_idx(vlan_proto); 60 if (pidx < 0) 61 return -EINVAL; 62 63 vidx = vlan_id / VLAN_GROUP_ARRAY_PART_LEN; 64 array = vg->vlan_devices_arrays[pidx][vidx]; 65 if (array != NULL) 66 return 0; 67 68 size = sizeof(struct net_device *) * VLAN_GROUP_ARRAY_PART_LEN; 69 array = kzalloc(size, GFP_KERNEL_ACCOUNT); 70 if (array == NULL) 71 return -ENOBUFS; 72 73 /* paired with smp_rmb() in __vlan_group_get_device() */ 74 smp_wmb(); 75 76 vg->vlan_devices_arrays[pidx][vidx] = array; 77 return 0; 78 } 79 80 void vlan_stacked_transfer_operstate(const struct net_device *rootdev, 81 struct net_device *dev, 82 struct vlan_dev_priv *vlan) 83 { 84 if (!(vlan->flags & VLAN_FLAG_BRIDGE_BINDING)) 85 netif_stacked_transfer_operstate(rootdev, dev); 86 } 87 88 void unregister_vlan_dev(struct net_device *dev, struct list_head *head) 89 { 90 struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 91 struct net_device *real_dev = vlan->real_dev; 92 struct vlan_info *vlan_info; 93 struct vlan_group *grp; 94 u16 vlan_id = vlan->vlan_id; 95 96 ASSERT_RTNL(); 97 98 vlan_info = rtnl_dereference(real_dev->vlan_info); 99 BUG_ON(!vlan_info); 100 101 grp = &vlan_info->grp; 102 103 grp->nr_vlan_devs--; 104 105 if (vlan->flags & VLAN_FLAG_MVRP) 106 vlan_mvrp_request_leave(dev); 107 if (vlan->flags & VLAN_FLAG_GVRP) 108 vlan_gvrp_request_leave(dev); 109 110 vlan_group_set_device(grp, vlan->vlan_proto, vlan_id, NULL); 111 112 netdev_upper_dev_unlink(real_dev, dev); 113 /* Because unregister_netdevice_queue() makes sure at least one rcu 114 * grace period is respected before device freeing, 115 * we dont need to call synchronize_net() here. 116 */ 117 unregister_netdevice_queue(dev, head); 118 119 if (grp->nr_vlan_devs == 0) { 120 vlan_mvrp_uninit_applicant(real_dev); 121 vlan_gvrp_uninit_applicant(real_dev); 122 } 123 124 vlan_vid_del(real_dev, vlan->vlan_proto, vlan_id); 125 } 126 127 int vlan_check_real_dev(struct net_device *real_dev, 128 __be16 protocol, u16 vlan_id, 129 struct netlink_ext_ack *extack) 130 { 131 const char *name = real_dev->name; 132 133 if (real_dev->features & NETIF_F_VLAN_CHALLENGED || 134 real_dev->type != ARPHRD_ETHER) { 135 pr_info("VLANs not supported on %s\n", name); 136 NL_SET_ERR_MSG_MOD(extack, "VLANs not supported on device"); 137 return -EOPNOTSUPP; 138 } 139 140 if (vlan_find_dev(real_dev, protocol, vlan_id) != NULL) { 141 NL_SET_ERR_MSG_MOD(extack, "VLAN device already exists"); 142 return -EEXIST; 143 } 144 145 return 0; 146 } 147 148 int register_vlan_dev(struct net_device *dev, struct netlink_ext_ack *extack) 149 { 150 struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 151 struct net_device *real_dev = vlan->real_dev; 152 u16 vlan_id = vlan->vlan_id; 153 struct vlan_info *vlan_info; 154 struct vlan_group *grp; 155 int err; 156 157 err = vlan_vid_add(real_dev, vlan->vlan_proto, vlan_id); 158 if (err) 159 return err; 160 161 vlan_info = rtnl_dereference(real_dev->vlan_info); 162 /* vlan_info should be there now. vlan_vid_add took care of it */ 163 BUG_ON(!vlan_info); 164 165 grp = &vlan_info->grp; 166 if (grp->nr_vlan_devs == 0) { 167 err = vlan_gvrp_init_applicant(real_dev); 168 if (err < 0) 169 goto out_vid_del; 170 err = vlan_mvrp_init_applicant(real_dev); 171 if (err < 0) 172 goto out_uninit_gvrp; 173 } 174 175 err = vlan_group_prealloc_vid(grp, vlan->vlan_proto, vlan_id); 176 if (err < 0) 177 goto out_uninit_mvrp; 178 179 err = register_netdevice(dev); 180 if (err < 0) 181 goto out_uninit_mvrp; 182 183 err = netdev_upper_dev_link(real_dev, dev, extack); 184 if (err) 185 goto out_unregister_netdev; 186 187 vlan_stacked_transfer_operstate(real_dev, dev, vlan); 188 linkwatch_fire_event(dev); /* _MUST_ call rfc2863_policy() */ 189 190 /* So, got the sucker initialized, now lets place 191 * it into our local structure. 192 */ 193 vlan_group_set_device(grp, vlan->vlan_proto, vlan_id, dev); 194 grp->nr_vlan_devs++; 195 196 netdev_update_features(dev); 197 198 return 0; 199 200 out_unregister_netdev: 201 unregister_netdevice(dev); 202 out_uninit_mvrp: 203 if (grp->nr_vlan_devs == 0) 204 vlan_mvrp_uninit_applicant(real_dev); 205 out_uninit_gvrp: 206 if (grp->nr_vlan_devs == 0) 207 vlan_gvrp_uninit_applicant(real_dev); 208 out_vid_del: 209 vlan_vid_del(real_dev, vlan->vlan_proto, vlan_id); 210 return err; 211 } 212 213 /* Attach a VLAN device to a mac address (ie Ethernet Card). 214 * Returns 0 if the device was created or a negative error code otherwise. 215 */ 216 static int register_vlan_device(struct net_device *real_dev, u16 vlan_id) 217 { 218 struct net_device *new_dev; 219 struct vlan_dev_priv *vlan; 220 struct net *net = dev_net(real_dev); 221 struct vlan_net *vn = net_generic(net, vlan_net_id); 222 char name[IFNAMSIZ]; 223 int err; 224 225 if (vlan_id >= VLAN_VID_MASK) 226 return -ERANGE; 227 228 err = vlan_check_real_dev(real_dev, htons(ETH_P_8021Q), vlan_id, 229 NULL); 230 if (err < 0) 231 return err; 232 233 /* Gotta set up the fields for the device. */ 234 switch (vn->name_type) { 235 case VLAN_NAME_TYPE_RAW_PLUS_VID: 236 /* name will look like: eth1.0005 */ 237 snprintf(name, IFNAMSIZ, "%s.%.4i", real_dev->name, vlan_id); 238 break; 239 case VLAN_NAME_TYPE_PLUS_VID_NO_PAD: 240 /* Put our vlan.VID in the name. 241 * Name will look like: vlan5 242 */ 243 snprintf(name, IFNAMSIZ, "vlan%i", vlan_id); 244 break; 245 case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD: 246 /* Put our vlan.VID in the name. 247 * Name will look like: eth0.5 248 */ 249 snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, vlan_id); 250 break; 251 case VLAN_NAME_TYPE_PLUS_VID: 252 /* Put our vlan.VID in the name. 253 * Name will look like: vlan0005 254 */ 255 default: 256 snprintf(name, IFNAMSIZ, "vlan%.4i", vlan_id); 257 } 258 259 new_dev = alloc_netdev(sizeof(struct vlan_dev_priv), name, 260 NET_NAME_UNKNOWN, vlan_setup); 261 262 if (new_dev == NULL) 263 return -ENOBUFS; 264 265 dev_net_set(new_dev, net); 266 /* need 4 bytes for extra VLAN header info, 267 * hope the underlying device can handle it. 268 */ 269 new_dev->mtu = real_dev->mtu; 270 271 vlan = vlan_dev_priv(new_dev); 272 vlan->vlan_proto = htons(ETH_P_8021Q); 273 vlan->vlan_id = vlan_id; 274 vlan->real_dev = real_dev; 275 vlan->dent = NULL; 276 vlan->flags = VLAN_FLAG_REORDER_HDR; 277 278 new_dev->rtnl_link_ops = &vlan_link_ops; 279 err = register_vlan_dev(new_dev, NULL); 280 if (err < 0) 281 goto out_free_newdev; 282 283 return 0; 284 285 out_free_newdev: 286 free_netdev(new_dev); 287 return err; 288 } 289 290 static void vlan_sync_address(struct net_device *dev, 291 struct net_device *vlandev) 292 { 293 struct vlan_dev_priv *vlan = vlan_dev_priv(vlandev); 294 295 /* May be called without an actual change */ 296 if (ether_addr_equal(vlan->real_dev_addr, dev->dev_addr)) 297 return; 298 299 /* vlan continues to inherit address of lower device */ 300 if (vlan_dev_inherit_address(vlandev, dev)) 301 goto out; 302 303 /* vlan address was different from the old address and is equal to 304 * the new address */ 305 if (!ether_addr_equal(vlandev->dev_addr, vlan->real_dev_addr) && 306 ether_addr_equal(vlandev->dev_addr, dev->dev_addr)) 307 dev_uc_del(dev, vlandev->dev_addr); 308 309 /* vlan address was equal to the old address and is different from 310 * the new address */ 311 if (ether_addr_equal(vlandev->dev_addr, vlan->real_dev_addr) && 312 !ether_addr_equal(vlandev->dev_addr, dev->dev_addr)) 313 dev_uc_add(dev, vlandev->dev_addr); 314 315 out: 316 ether_addr_copy(vlan->real_dev_addr, dev->dev_addr); 317 } 318 319 static int __vlan_device_event(struct net_device *dev, unsigned long event) 320 { 321 int err = 0; 322 323 switch (event) { 324 case NETDEV_CHANGENAME: 325 vlan_proc_rem_dev(dev); 326 err = vlan_proc_add_dev(dev); 327 break; 328 case NETDEV_REGISTER: 329 err = vlan_proc_add_dev(dev); 330 break; 331 case NETDEV_UNREGISTER: 332 vlan_proc_rem_dev(dev); 333 break; 334 } 335 336 return err; 337 } 338 339 static void vlan_vid0_add(struct net_device *dev) 340 { 341 struct vlan_info *vlan_info; 342 int err; 343 344 if (!(dev->features & NETIF_F_HW_VLAN_CTAG_FILTER)) 345 return; 346 347 pr_info("adding VLAN 0 to HW filter on device %s\n", dev->name); 348 349 err = vlan_vid_add(dev, htons(ETH_P_8021Q), 0); 350 if (err) 351 return; 352 353 vlan_info = rtnl_dereference(dev->vlan_info); 354 vlan_info->auto_vid0 = true; 355 } 356 357 static void vlan_vid0_del(struct net_device *dev) 358 { 359 struct vlan_info *vlan_info = rtnl_dereference(dev->vlan_info); 360 361 if (!vlan_info || !vlan_info->auto_vid0) 362 return; 363 364 vlan_info->auto_vid0 = false; 365 vlan_vid_del(dev, htons(ETH_P_8021Q), 0); 366 } 367 368 static int vlan_device_event(struct notifier_block *unused, unsigned long event, 369 void *ptr) 370 { 371 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 372 struct vlan_group *grp; 373 struct vlan_info *vlan_info; 374 int i, flgs; 375 struct net_device *vlandev; 376 bool last = false; 377 LIST_HEAD(list); 378 int err; 379 380 if (is_vlan_dev(dev)) { 381 int err = __vlan_device_event(dev, event); 382 383 if (err) 384 return notifier_from_errno(err); 385 } 386 387 if (event == NETDEV_UP) 388 vlan_vid0_add(dev); 389 else if (event == NETDEV_DOWN) 390 vlan_vid0_del(dev); 391 392 vlan_info = rtnl_dereference(dev->vlan_info); 393 if (!vlan_info) 394 goto out; 395 grp = &vlan_info->grp; 396 397 /* It is OK that we do not hold the group lock right now, 398 * as we run under the RTNL lock. 399 */ 400 401 switch (event) { 402 case NETDEV_CHANGE: 403 /* Propagate real device state to vlan devices */ 404 vlan_group_for_each_dev(grp, i, vlandev) 405 vlan_stacked_transfer_operstate(dev, vlandev, 406 vlan_dev_priv(vlandev)); 407 break; 408 409 case NETDEV_CHANGEADDR: 410 /* Adjust unicast filters on underlying device */ 411 vlan_group_for_each_dev(grp, i, vlandev) { 412 flgs = vlandev->flags; 413 if (!(flgs & IFF_UP)) 414 continue; 415 416 vlan_sync_address(dev, vlandev); 417 } 418 break; 419 420 case NETDEV_CHANGEMTU: 421 vlan_group_for_each_dev(grp, i, vlandev) { 422 if (vlandev->mtu <= dev->mtu) 423 continue; 424 425 netdev_work_sched(vlandev, VLAN_WORK_MTU); 426 } 427 break; 428 429 case NETDEV_FEAT_CHANGE: 430 vlan_group_for_each_dev(grp, i, vlandev) 431 netdev_work_sched(vlandev, VLAN_WORK_FEATURES); 432 break; 433 434 case NETDEV_DOWN: 435 case NETDEV_UP: 436 vlan_group_for_each_dev(grp, i, vlandev) 437 netdev_work_sched(vlandev, VLAN_WORK_LINK_STATE); 438 break; 439 440 case NETDEV_UNREGISTER: 441 /* twiddle thumbs on netns device moves */ 442 if (dev->reg_state != NETREG_UNREGISTERING) 443 break; 444 445 vlan_group_for_each_dev(grp, i, vlandev) { 446 /* removal of last vid destroys vlan_info, abort 447 * afterwards */ 448 if (vlan_info->nr_vids == 1) 449 last = true; 450 451 unregister_vlan_dev(vlandev, &list); 452 if (last) 453 break; 454 } 455 unregister_netdevice_many(&list); 456 break; 457 458 case NETDEV_PRE_TYPE_CHANGE: 459 /* Forbid underlaying device to change its type. */ 460 if (vlan_uses_dev(dev)) 461 return NOTIFY_BAD; 462 break; 463 464 case NETDEV_NOTIFY_PEERS: 465 case NETDEV_BONDING_FAILOVER: 466 case NETDEV_RESEND_IGMP: 467 /* Propagate to vlan devices */ 468 vlan_group_for_each_dev(grp, i, vlandev) 469 call_netdevice_notifiers(event, vlandev); 470 break; 471 472 case NETDEV_CVLAN_FILTER_PUSH_INFO: 473 err = vlan_filter_push_vids(vlan_info, htons(ETH_P_8021Q)); 474 if (err) 475 return notifier_from_errno(err); 476 break; 477 478 case NETDEV_CVLAN_FILTER_DROP_INFO: 479 vlan_filter_drop_vids(vlan_info, htons(ETH_P_8021Q)); 480 break; 481 482 case NETDEV_SVLAN_FILTER_PUSH_INFO: 483 err = vlan_filter_push_vids(vlan_info, htons(ETH_P_8021AD)); 484 if (err) 485 return notifier_from_errno(err); 486 break; 487 488 case NETDEV_SVLAN_FILTER_DROP_INFO: 489 vlan_filter_drop_vids(vlan_info, htons(ETH_P_8021AD)); 490 break; 491 } 492 493 out: 494 return NOTIFY_DONE; 495 } 496 497 static struct notifier_block vlan_notifier_block __read_mostly = { 498 .notifier_call = vlan_device_event, 499 }; 500 501 /* 502 * VLAN IOCTL handler. 503 * o execute requested action or pass command to the device driver 504 * arg is really a struct vlan_ioctl_args __user *. 505 */ 506 static int vlan_ioctl_handler(struct net *net, void __user *arg) 507 { 508 int err; 509 struct vlan_ioctl_args args; 510 struct net_device *dev = NULL; 511 512 if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args))) 513 return -EFAULT; 514 515 /* Null terminate this sucker, just in case. */ 516 args.device1[sizeof(args.device1) - 1] = 0; 517 args.u.device2[sizeof(args.u.device2) - 1] = 0; 518 519 rtnl_lock(); 520 521 switch (args.cmd) { 522 case SET_VLAN_INGRESS_PRIORITY_CMD: 523 case SET_VLAN_EGRESS_PRIORITY_CMD: 524 case SET_VLAN_FLAG_CMD: 525 case ADD_VLAN_CMD: 526 case DEL_VLAN_CMD: 527 case GET_VLAN_REALDEV_NAME_CMD: 528 case GET_VLAN_VID_CMD: 529 err = -ENODEV; 530 dev = __dev_get_by_name(net, args.device1); 531 if (!dev) 532 goto out; 533 534 err = -EINVAL; 535 if (args.cmd != ADD_VLAN_CMD && !is_vlan_dev(dev)) 536 goto out; 537 } 538 539 switch (args.cmd) { 540 case SET_VLAN_INGRESS_PRIORITY_CMD: 541 err = -EPERM; 542 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 543 break; 544 vlan_dev_set_ingress_priority(dev, 545 args.u.skb_priority, 546 args.vlan_qos); 547 err = 0; 548 break; 549 550 case SET_VLAN_EGRESS_PRIORITY_CMD: 551 err = -EPERM; 552 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 553 break; 554 err = vlan_dev_set_egress_priority(dev, 555 args.u.skb_priority, 556 args.vlan_qos); 557 break; 558 559 case SET_VLAN_FLAG_CMD: 560 err = -EPERM; 561 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 562 break; 563 err = vlan_dev_change_flags(dev, 564 args.vlan_qos ? args.u.flag : 0, 565 args.u.flag); 566 break; 567 568 case SET_VLAN_NAME_TYPE_CMD: 569 err = -EPERM; 570 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 571 break; 572 if (args.u.name_type < VLAN_NAME_TYPE_HIGHEST) { 573 struct vlan_net *vn; 574 575 vn = net_generic(net, vlan_net_id); 576 vn->name_type = args.u.name_type; 577 err = 0; 578 } else { 579 err = -EINVAL; 580 } 581 break; 582 583 case ADD_VLAN_CMD: 584 err = -EPERM; 585 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 586 break; 587 err = register_vlan_device(dev, args.u.VID); 588 break; 589 590 case DEL_VLAN_CMD: 591 err = -EPERM; 592 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 593 break; 594 unregister_vlan_dev(dev, NULL); 595 err = 0; 596 break; 597 598 case GET_VLAN_REALDEV_NAME_CMD: 599 err = 0; 600 vlan_dev_get_realdev_name(dev, args.u.device2, 601 sizeof(args.u.device2)); 602 if (copy_to_user(arg, &args, 603 sizeof(struct vlan_ioctl_args))) 604 err = -EFAULT; 605 break; 606 607 case GET_VLAN_VID_CMD: 608 err = 0; 609 args.u.VID = vlan_dev_vlan_id(dev); 610 if (copy_to_user(arg, &args, 611 sizeof(struct vlan_ioctl_args))) 612 err = -EFAULT; 613 break; 614 615 default: 616 err = -EOPNOTSUPP; 617 break; 618 } 619 out: 620 rtnl_unlock(); 621 return err; 622 } 623 624 static int __net_init vlan_init_net(struct net *net) 625 { 626 struct vlan_net *vn = net_generic(net, vlan_net_id); 627 int err; 628 629 vn->name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD; 630 631 err = vlan_proc_init(net); 632 633 return err; 634 } 635 636 static void __net_exit vlan_exit_net(struct net *net) 637 { 638 vlan_proc_cleanup(net); 639 } 640 641 static struct pernet_operations vlan_net_ops = { 642 .init = vlan_init_net, 643 .exit = vlan_exit_net, 644 .id = &vlan_net_id, 645 .size = sizeof(struct vlan_net), 646 }; 647 648 static int __init vlan_proto_init(void) 649 { 650 int err; 651 652 pr_info("%s v%s\n", vlan_fullname, vlan_version); 653 654 err = register_pernet_subsys(&vlan_net_ops); 655 if (err < 0) 656 goto err0; 657 658 err = register_netdevice_notifier(&vlan_notifier_block); 659 if (err < 0) 660 goto err2; 661 662 err = vlan_gvrp_init(); 663 if (err < 0) 664 goto err3; 665 666 err = vlan_mvrp_init(); 667 if (err < 0) 668 goto err4; 669 670 err = vlan_netlink_init(); 671 if (err < 0) 672 goto err5; 673 674 vlan_ioctl_set(vlan_ioctl_handler); 675 return 0; 676 677 err5: 678 vlan_mvrp_uninit(); 679 err4: 680 vlan_gvrp_uninit(); 681 err3: 682 unregister_netdevice_notifier(&vlan_notifier_block); 683 err2: 684 unregister_pernet_subsys(&vlan_net_ops); 685 err0: 686 return err; 687 } 688 689 static void __exit vlan_cleanup_module(void) 690 { 691 vlan_ioctl_set(NULL); 692 693 vlan_netlink_fini(); 694 695 unregister_netdevice_notifier(&vlan_notifier_block); 696 697 unregister_pernet_subsys(&vlan_net_ops); 698 rcu_barrier(); /* Wait for completion of call_rcu()'s */ 699 700 vlan_mvrp_uninit(); 701 vlan_gvrp_uninit(); 702 } 703 704 module_init(vlan_proto_init); 705 module_exit(vlan_cleanup_module); 706 707 MODULE_DESCRIPTION("802.1Q/802.1ad VLAN Protocol"); 708 MODULE_LICENSE("GPL"); 709 MODULE_VERSION(DRV_VERSION); 710 MODULE_IMPORT_NS("NETDEV_INTERNAL"); 711