3 * Ethernet-type device handling.
5 * Authors: Ben Greear <greearb@candelatech.com>
6 * Please send support related email to: netdev@vger.kernel.org
7 * VLAN Home Page: http://www.candelatech.com/~greear/vlan.html
10 * Fix for packet capture - Nick Eggleston <nick@dccinc.com>;
11 * Add HW acceleration hooks - David S. Miller <davem@redhat.com>;
12 * Correct all the locking - David S. Miller <davem@redhat.com>;
13 * Use hash table for VLAN groups - David S. Miller <davem@redhat.com>
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/capability.h>
24 #include <linux/module.h>
25 #include <linux/netdevice.h>
26 #include <linux/skbuff.h>
27 #include <linux/slab.h>
28 #include <linux/init.h>
29 #include <linux/rculist.h>
30 #include <net/p8022.h>
32 #include <linux/rtnetlink.h>
33 #include <linux/notifier.h>
34 #include <net/rtnetlink.h>
35 #include <net/net_namespace.h>
36 #include <net/netns/generic.h>
37 #include <asm/uaccess.h>
39 #include <linux/if_vlan.h>
43 #define DRV_VERSION "1.8"
45 /* Global VLAN variables */
47 int vlan_net_id __read_mostly;
49 const char vlan_fullname[] = "802.1Q VLAN Support";
50 const char vlan_version[] = DRV_VERSION;
52 /* End of global variables definitions. */
54 static int vlan_group_prealloc_vid(struct vlan_group *vg, u16 vlan_id)
56 struct net_device **array;
61 array = vg->vlan_devices_arrays[vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
65 size = sizeof(struct net_device *) * VLAN_GROUP_ARRAY_PART_LEN;
66 array = kzalloc(size, GFP_KERNEL);
70 vg->vlan_devices_arrays[vlan_id / VLAN_GROUP_ARRAY_PART_LEN] = array;
74 void unregister_vlan_dev(struct net_device *dev, struct list_head *head)
76 struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
77 struct net_device *real_dev = vlan->real_dev;
78 struct vlan_info *vlan_info;
79 struct vlan_group *grp;
80 u16 vlan_id = vlan->vlan_id;
84 vlan_info = rtnl_dereference(real_dev->vlan_info);
87 grp = &vlan_info->grp;
89 /* Take it out of our own structures, but be sure to interlock with
90 * HW accelerating devices or SW vlan input packet processing if
91 * VLAN is not 0 (leave it there for 802.1p).
94 vlan_vid_del(real_dev, vlan_id);
98 if (vlan->flags & VLAN_FLAG_GVRP)
99 vlan_gvrp_request_leave(dev);
101 vlan_group_set_device(grp, vlan_id, NULL);
102 /* Because unregister_netdevice_queue() makes sure at least one rcu
103 * grace period is respected before device freeing,
104 * we dont need to call synchronize_net() here.
106 unregister_netdevice_queue(dev, head);
108 netdev_upper_dev_unlink(real_dev, dev);
110 if (grp->nr_vlan_devs == 0)
111 vlan_gvrp_uninit_applicant(real_dev);
113 /* Get rid of the vlan's reference to real_dev */
117 int vlan_check_real_dev(struct net_device *real_dev, u16 vlan_id)
119 const char *name = real_dev->name;
120 const struct net_device_ops *ops = real_dev->netdev_ops;
122 if (real_dev->features & NETIF_F_VLAN_CHALLENGED) {
123 pr_info("VLANs not supported on %s\n", name);
127 if ((real_dev->features & NETIF_F_HW_VLAN_FILTER) &&
128 (!ops->ndo_vlan_rx_add_vid || !ops->ndo_vlan_rx_kill_vid)) {
129 pr_info("Device %s has buggy VLAN hw accel\n", name);
133 if (vlan_find_dev(real_dev, vlan_id) != NULL)
139 int register_vlan_dev(struct net_device *dev)
141 struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
142 struct net_device *real_dev = vlan->real_dev;
143 u16 vlan_id = vlan->vlan_id;
144 struct vlan_info *vlan_info;
145 struct vlan_group *grp;
148 err = vlan_vid_add(real_dev, vlan_id);
152 vlan_info = rtnl_dereference(real_dev->vlan_info);
153 /* vlan_info should be there now. vlan_vid_add took care of it */
156 grp = &vlan_info->grp;
157 if (grp->nr_vlan_devs == 0) {
158 err = vlan_gvrp_init_applicant(real_dev);
163 err = vlan_group_prealloc_vid(grp, vlan_id);
165 goto out_uninit_applicant;
167 err = netdev_upper_dev_link(real_dev, dev);
169 goto out_uninit_applicant;
171 err = register_netdevice(dev);
173 goto out_upper_dev_unlink;
175 /* Account for reference in struct vlan_dev_priv */
178 netif_stacked_transfer_operstate(real_dev, dev);
179 linkwatch_fire_event(dev); /* _MUST_ call rfc2863_policy() */
181 /* So, got the sucker initialized, now lets place
182 * it into our local structure.
184 vlan_group_set_device(grp, vlan_id, dev);
189 out_upper_dev_unlink:
190 netdev_upper_dev_unlink(real_dev, dev);
191 out_uninit_applicant:
192 if (grp->nr_vlan_devs == 0)
193 vlan_gvrp_uninit_applicant(real_dev);
195 vlan_vid_del(real_dev, vlan_id);
199 /* Attach a VLAN device to a mac address (ie Ethernet Card).
200 * Returns 0 if the device was created or a negative error code otherwise.
202 static int register_vlan_device(struct net_device *real_dev, u16 vlan_id)
204 struct net_device *new_dev;
205 struct net *net = dev_net(real_dev);
206 struct vlan_net *vn = net_generic(net, vlan_net_id);
210 if (vlan_id >= VLAN_VID_MASK)
213 err = vlan_check_real_dev(real_dev, vlan_id);
217 /* Gotta set up the fields for the device. */
218 switch (vn->name_type) {
219 case VLAN_NAME_TYPE_RAW_PLUS_VID:
220 /* name will look like: eth1.0005 */
221 snprintf(name, IFNAMSIZ, "%s.%.4i", real_dev->name, vlan_id);
223 case VLAN_NAME_TYPE_PLUS_VID_NO_PAD:
224 /* Put our vlan.VID in the name.
225 * Name will look like: vlan5
227 snprintf(name, IFNAMSIZ, "vlan%i", vlan_id);
229 case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD:
230 /* Put our vlan.VID in the name.
231 * Name will look like: eth0.5
233 snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, vlan_id);
235 case VLAN_NAME_TYPE_PLUS_VID:
236 /* Put our vlan.VID in the name.
237 * Name will look like: vlan0005
240 snprintf(name, IFNAMSIZ, "vlan%.4i", vlan_id);
243 new_dev = alloc_netdev(sizeof(struct vlan_dev_priv), name, vlan_setup);
248 dev_net_set(new_dev, net);
249 /* need 4 bytes for extra VLAN header info,
250 * hope the underlying device can handle it.
252 new_dev->mtu = real_dev->mtu;
253 new_dev->priv_flags |= (real_dev->priv_flags & IFF_UNICAST_FLT);
255 vlan_dev_priv(new_dev)->vlan_id = vlan_id;
256 vlan_dev_priv(new_dev)->real_dev = real_dev;
257 vlan_dev_priv(new_dev)->dent = NULL;
258 vlan_dev_priv(new_dev)->flags = VLAN_FLAG_REORDER_HDR;
260 new_dev->rtnl_link_ops = &vlan_link_ops;
261 err = register_vlan_dev(new_dev);
263 goto out_free_newdev;
268 free_netdev(new_dev);
272 static void vlan_sync_address(struct net_device *dev,
273 struct net_device *vlandev)
275 struct vlan_dev_priv *vlan = vlan_dev_priv(vlandev);
277 /* May be called without an actual change */
278 if (ether_addr_equal(vlan->real_dev_addr, dev->dev_addr))
281 /* vlan address was different from the old address and is equal to
283 if (!ether_addr_equal(vlandev->dev_addr, vlan->real_dev_addr) &&
284 ether_addr_equal(vlandev->dev_addr, dev->dev_addr))
285 dev_uc_del(dev, vlandev->dev_addr);
287 /* vlan address was equal to the old address and is different from
289 if (ether_addr_equal(vlandev->dev_addr, vlan->real_dev_addr) &&
290 !ether_addr_equal(vlandev->dev_addr, dev->dev_addr))
291 dev_uc_add(dev, vlandev->dev_addr);
293 memcpy(vlan->real_dev_addr, dev->dev_addr, ETH_ALEN);
296 static void vlan_transfer_features(struct net_device *dev,
297 struct net_device *vlandev)
299 vlandev->gso_max_size = dev->gso_max_size;
301 if (dev->features & NETIF_F_HW_VLAN_TX)
302 vlandev->hard_header_len = dev->hard_header_len;
304 vlandev->hard_header_len = dev->hard_header_len + VLAN_HLEN;
306 #if IS_ENABLED(CONFIG_FCOE)
307 vlandev->fcoe_ddp_xid = dev->fcoe_ddp_xid;
310 netdev_update_features(vlandev);
313 static void __vlan_device_event(struct net_device *dev, unsigned long event)
316 case NETDEV_CHANGENAME:
317 vlan_proc_rem_dev(dev);
318 if (vlan_proc_add_dev(dev) < 0)
319 pr_warn("failed to change proc name for %s\n",
322 case NETDEV_REGISTER:
323 if (vlan_proc_add_dev(dev) < 0)
324 pr_warn("failed to add proc entry for %s\n", dev->name);
326 case NETDEV_UNREGISTER:
327 vlan_proc_rem_dev(dev);
332 static int vlan_device_event(struct notifier_block *unused, unsigned long event,
335 struct net_device *dev = ptr;
336 struct vlan_group *grp;
337 struct vlan_info *vlan_info;
339 struct net_device *vlandev;
340 struct vlan_dev_priv *vlan;
343 if (is_vlan_dev(dev))
344 __vlan_device_event(dev, event);
346 if ((event == NETDEV_UP) &&
347 (dev->features & NETIF_F_HW_VLAN_FILTER)) {
348 pr_info("adding VLAN 0 to HW filter on device %s\n",
350 vlan_vid_add(dev, 0);
353 vlan_info = rtnl_dereference(dev->vlan_info);
356 grp = &vlan_info->grp;
358 /* It is OK that we do not hold the group lock right now,
359 * as we run under the RTNL lock.
364 /* Propagate real device state to vlan devices */
365 for (i = 0; i < VLAN_N_VID; i++) {
366 vlandev = vlan_group_get_device(grp, i);
370 netif_stacked_transfer_operstate(dev, vlandev);
374 case NETDEV_CHANGEADDR:
375 /* Adjust unicast filters on underlying device */
376 for (i = 0; i < VLAN_N_VID; i++) {
377 vlandev = vlan_group_get_device(grp, i);
381 flgs = vlandev->flags;
382 if (!(flgs & IFF_UP))
385 vlan_sync_address(dev, vlandev);
389 case NETDEV_CHANGEMTU:
390 for (i = 0; i < VLAN_N_VID; i++) {
391 vlandev = vlan_group_get_device(grp, i);
395 if (vlandev->mtu <= dev->mtu)
398 dev_set_mtu(vlandev, dev->mtu);
402 case NETDEV_FEAT_CHANGE:
403 /* Propagate device features to underlying device */
404 for (i = 0; i < VLAN_N_VID; i++) {
405 vlandev = vlan_group_get_device(grp, i);
409 vlan_transfer_features(dev, vlandev);
415 if (dev->features & NETIF_F_HW_VLAN_FILTER)
416 vlan_vid_del(dev, 0);
418 /* Put all VLANs for this dev in the down state too. */
419 for (i = 0; i < VLAN_N_VID; i++) {
420 vlandev = vlan_group_get_device(grp, i);
424 flgs = vlandev->flags;
425 if (!(flgs & IFF_UP))
428 vlan = vlan_dev_priv(vlandev);
429 if (!(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
430 dev_change_flags(vlandev, flgs & ~IFF_UP);
431 netif_stacked_transfer_operstate(dev, vlandev);
436 /* Put all VLANs for this dev in the up state too. */
437 for (i = 0; i < VLAN_N_VID; i++) {
438 vlandev = vlan_group_get_device(grp, i);
442 flgs = vlandev->flags;
446 vlan = vlan_dev_priv(vlandev);
447 if (!(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
448 dev_change_flags(vlandev, flgs | IFF_UP);
449 netif_stacked_transfer_operstate(dev, vlandev);
453 case NETDEV_UNREGISTER:
454 /* twiddle thumbs on netns device moves */
455 if (dev->reg_state != NETREG_UNREGISTERING)
458 for (i = 0; i < VLAN_N_VID; i++) {
459 vlandev = vlan_group_get_device(grp, i);
463 /* removal of last vid destroys vlan_info, abort
465 if (vlan_info->nr_vids == 1)
468 unregister_vlan_dev(vlandev, &list);
470 unregister_netdevice_many(&list);
473 case NETDEV_PRE_TYPE_CHANGE:
474 /* Forbid underlaying device to change its type. */
475 if (vlan_uses_dev(dev))
479 case NETDEV_NOTIFY_PEERS:
480 case NETDEV_BONDING_FAILOVER:
481 /* Propagate to vlan devices */
482 for (i = 0; i < VLAN_N_VID; i++) {
483 vlandev = vlan_group_get_device(grp, i);
487 call_netdevice_notifiers(event, vlandev);
496 static struct notifier_block vlan_notifier_block __read_mostly = {
497 .notifier_call = vlan_device_event,
501 * VLAN IOCTL handler.
502 * o execute requested action or pass command to the device driver
503 * arg is really a struct vlan_ioctl_args __user *.
505 static int vlan_ioctl_handler(struct net *net, void __user *arg)
508 struct vlan_ioctl_args args;
509 struct net_device *dev = NULL;
511 if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args)))
514 /* Null terminate this sucker, just in case. */
515 args.device1[23] = 0;
516 args.u.device2[23] = 0;
521 case SET_VLAN_INGRESS_PRIORITY_CMD:
522 case SET_VLAN_EGRESS_PRIORITY_CMD:
523 case SET_VLAN_FLAG_CMD:
526 case GET_VLAN_REALDEV_NAME_CMD:
527 case GET_VLAN_VID_CMD:
529 dev = __dev_get_by_name(net, args.device1);
534 if (args.cmd != ADD_VLAN_CMD && !is_vlan_dev(dev))
539 case SET_VLAN_INGRESS_PRIORITY_CMD:
541 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
543 vlan_dev_set_ingress_priority(dev,
549 case SET_VLAN_EGRESS_PRIORITY_CMD:
551 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
553 err = vlan_dev_set_egress_priority(dev,
558 case SET_VLAN_FLAG_CMD:
560 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
562 err = vlan_dev_change_flags(dev,
563 args.vlan_qos ? args.u.flag : 0,
567 case SET_VLAN_NAME_TYPE_CMD:
569 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
571 if ((args.u.name_type >= 0) &&
572 (args.u.name_type < VLAN_NAME_TYPE_HIGHEST)) {
575 vn = net_generic(net, vlan_net_id);
576 vn->name_type = args.u.name_type;
585 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
587 err = register_vlan_device(dev, args.u.VID);
592 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
594 unregister_vlan_dev(dev, NULL);
598 case GET_VLAN_REALDEV_NAME_CMD:
600 vlan_dev_get_realdev_name(dev, args.u.device2);
601 if (copy_to_user(arg, &args,
602 sizeof(struct vlan_ioctl_args)))
606 case GET_VLAN_VID_CMD:
608 args.u.VID = vlan_dev_vlan_id(dev);
609 if (copy_to_user(arg, &args,
610 sizeof(struct vlan_ioctl_args)))
623 static int __net_init vlan_init_net(struct net *net)
625 struct vlan_net *vn = net_generic(net, vlan_net_id);
628 vn->name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;
630 err = vlan_proc_init(net);
635 static void __net_exit vlan_exit_net(struct net *net)
637 vlan_proc_cleanup(net);
640 static struct pernet_operations vlan_net_ops = {
641 .init = vlan_init_net,
642 .exit = vlan_exit_net,
644 .size = sizeof(struct vlan_net),
647 static int __init vlan_proto_init(void)
651 pr_info("%s v%s\n", vlan_fullname, vlan_version);
653 err = register_pernet_subsys(&vlan_net_ops);
657 err = register_netdevice_notifier(&vlan_notifier_block);
661 err = vlan_gvrp_init();
665 err = vlan_netlink_init();
669 vlan_ioctl_set(vlan_ioctl_handler);
675 unregister_netdevice_notifier(&vlan_notifier_block);
677 unregister_pernet_subsys(&vlan_net_ops);
682 static void __exit vlan_cleanup_module(void)
684 vlan_ioctl_set(NULL);
687 unregister_netdevice_notifier(&vlan_notifier_block);
689 unregister_pernet_subsys(&vlan_net_ops);
690 rcu_barrier(); /* Wait for completion of call_rcu()'s */
695 module_init(vlan_proto_init);
696 module_exit(vlan_cleanup_module);
698 MODULE_LICENSE("GPL");
699 MODULE_VERSION(DRV_VERSION);