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 void vlan_group_free(struct vlan_group *grp)
58 for (i = 0; i < VLAN_GROUP_ARRAY_SPLIT_PARTS; i++)
59 kfree(grp->vlan_devices_arrays[i]);
63 static struct vlan_group *vlan_group_alloc(struct net_device *real_dev)
65 struct vlan_group *grp;
67 grp = kzalloc(sizeof(struct vlan_group), GFP_KERNEL);
71 grp->real_dev = real_dev;
75 static int vlan_group_prealloc_vid(struct vlan_group *vg, u16 vlan_id)
77 struct net_device **array;
82 array = vg->vlan_devices_arrays[vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
86 size = sizeof(struct net_device *) * VLAN_GROUP_ARRAY_PART_LEN;
87 array = kzalloc(size, GFP_KERNEL);
91 vg->vlan_devices_arrays[vlan_id / VLAN_GROUP_ARRAY_PART_LEN] = array;
95 static void vlan_rcu_free(struct rcu_head *rcu)
97 vlan_group_free(container_of(rcu, struct vlan_group, rcu));
100 void unregister_vlan_dev(struct net_device *dev, struct list_head *head)
102 struct vlan_dev_info *vlan = vlan_dev_info(dev);
103 struct net_device *real_dev = vlan->real_dev;
104 const struct net_device_ops *ops = real_dev->netdev_ops;
105 struct vlan_group *grp;
106 u16 vlan_id = vlan->vlan_id;
110 grp = rtnl_dereference(real_dev->vlgrp);
115 if (vlan->flags & VLAN_FLAG_GVRP)
116 vlan_gvrp_request_leave(dev);
118 vlan_group_set_device(grp, vlan_id, NULL);
119 /* Because unregister_netdevice_queue() makes sure at least one rcu
120 * grace period is respected before device freeing,
121 * we dont need to call synchronize_net() here.
123 unregister_netdevice_queue(dev, head);
125 /* If the group is now empty, kill off the group. */
126 if (grp->nr_vlans == 0) {
127 vlan_gvrp_uninit_applicant(real_dev);
129 RCU_INIT_POINTER(real_dev->vlgrp, NULL);
131 /* Free the group, after all cpu's are done. */
132 call_rcu(&grp->rcu, vlan_rcu_free);
135 if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
136 ops->ndo_vlan_rx_kill_vid(real_dev, vlan_id);
138 /* Get rid of the vlan's reference to real_dev */
142 int vlan_check_real_dev(struct net_device *real_dev, u16 vlan_id)
144 const char *name = real_dev->name;
145 const struct net_device_ops *ops = real_dev->netdev_ops;
147 if (real_dev->features & NETIF_F_VLAN_CHALLENGED) {
148 pr_info("VLANs not supported on %s\n", name);
152 if ((real_dev->features & NETIF_F_HW_VLAN_FILTER) &&
153 (!ops->ndo_vlan_rx_add_vid || !ops->ndo_vlan_rx_kill_vid)) {
154 pr_info("Device %s has buggy VLAN hw accel\n", name);
158 if (vlan_find_dev(real_dev, vlan_id) != NULL)
164 int register_vlan_dev(struct net_device *dev)
166 struct vlan_dev_info *vlan = vlan_dev_info(dev);
167 struct net_device *real_dev = vlan->real_dev;
168 const struct net_device_ops *ops = real_dev->netdev_ops;
169 u16 vlan_id = vlan->vlan_id;
170 struct vlan_group *grp, *ngrp = NULL;
173 grp = rtnl_dereference(real_dev->vlgrp);
175 ngrp = grp = vlan_group_alloc(real_dev);
178 err = vlan_gvrp_init_applicant(real_dev);
183 err = vlan_group_prealloc_vid(grp, vlan_id);
185 goto out_uninit_applicant;
187 err = register_netdevice(dev);
189 goto out_uninit_applicant;
191 /* Account for reference in struct vlan_dev_info */
194 netif_stacked_transfer_operstate(real_dev, dev);
195 linkwatch_fire_event(dev); /* _MUST_ call rfc2863_policy() */
197 /* So, got the sucker initialized, now lets place
198 * it into our local structure.
200 vlan_group_set_device(grp, vlan_id, dev);
204 rcu_assign_pointer(real_dev->vlgrp, ngrp);
206 if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
207 ops->ndo_vlan_rx_add_vid(real_dev, vlan_id);
211 out_uninit_applicant:
213 vlan_gvrp_uninit_applicant(real_dev);
216 /* Free the group, after all cpu's are done. */
217 call_rcu(&ngrp->rcu, vlan_rcu_free);
222 /* Attach a VLAN device to a mac address (ie Ethernet Card).
223 * Returns 0 if the device was created or a negative error code otherwise.
225 static int register_vlan_device(struct net_device *real_dev, u16 vlan_id)
227 struct net_device *new_dev;
228 struct net *net = dev_net(real_dev);
229 struct vlan_net *vn = net_generic(net, vlan_net_id);
233 if (vlan_id >= VLAN_VID_MASK)
236 err = vlan_check_real_dev(real_dev, vlan_id);
240 /* Gotta set up the fields for the device. */
241 switch (vn->name_type) {
242 case VLAN_NAME_TYPE_RAW_PLUS_VID:
243 /* name will look like: eth1.0005 */
244 snprintf(name, IFNAMSIZ, "%s.%.4i", real_dev->name, vlan_id);
246 case VLAN_NAME_TYPE_PLUS_VID_NO_PAD:
247 /* Put our vlan.VID in the name.
248 * Name will look like: vlan5
250 snprintf(name, IFNAMSIZ, "vlan%i", vlan_id);
252 case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD:
253 /* Put our vlan.VID in the name.
254 * Name will look like: eth0.5
256 snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, vlan_id);
258 case VLAN_NAME_TYPE_PLUS_VID:
259 /* Put our vlan.VID in the name.
260 * Name will look like: vlan0005
263 snprintf(name, IFNAMSIZ, "vlan%.4i", vlan_id);
266 new_dev = alloc_netdev(sizeof(struct vlan_dev_info), name, vlan_setup);
271 dev_net_set(new_dev, net);
272 /* need 4 bytes for extra VLAN header info,
273 * hope the underlying device can handle it.
275 new_dev->mtu = real_dev->mtu;
277 vlan_dev_info(new_dev)->vlan_id = vlan_id;
278 vlan_dev_info(new_dev)->real_dev = real_dev;
279 vlan_dev_info(new_dev)->dent = NULL;
280 vlan_dev_info(new_dev)->flags = VLAN_FLAG_REORDER_HDR;
282 new_dev->rtnl_link_ops = &vlan_link_ops;
283 err = register_vlan_dev(new_dev);
285 goto out_free_newdev;
290 free_netdev(new_dev);
294 static void vlan_sync_address(struct net_device *dev,
295 struct net_device *vlandev)
297 struct vlan_dev_info *vlan = vlan_dev_info(vlandev);
299 /* May be called without an actual change */
300 if (!compare_ether_addr(vlan->real_dev_addr, dev->dev_addr))
303 /* vlan address was different from the old address and is equal to
305 if (compare_ether_addr(vlandev->dev_addr, vlan->real_dev_addr) &&
306 !compare_ether_addr(vlandev->dev_addr, dev->dev_addr))
307 dev_uc_del(dev, vlandev->dev_addr);
309 /* vlan address was equal to the old address and is different from
311 if (!compare_ether_addr(vlandev->dev_addr, vlan->real_dev_addr) &&
312 compare_ether_addr(vlandev->dev_addr, dev->dev_addr))
313 dev_uc_add(dev, vlandev->dev_addr);
315 memcpy(vlan->real_dev_addr, dev->dev_addr, ETH_ALEN);
318 static void vlan_transfer_features(struct net_device *dev,
319 struct net_device *vlandev)
321 vlandev->gso_max_size = dev->gso_max_size;
323 if (dev->features & NETIF_F_HW_VLAN_TX)
324 vlandev->hard_header_len = dev->hard_header_len;
326 vlandev->hard_header_len = dev->hard_header_len + VLAN_HLEN;
328 #if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
329 vlandev->fcoe_ddp_xid = dev->fcoe_ddp_xid;
332 netdev_update_features(vlandev);
335 static void __vlan_device_event(struct net_device *dev, unsigned long event)
338 case NETDEV_CHANGENAME:
339 vlan_proc_rem_dev(dev);
340 if (vlan_proc_add_dev(dev) < 0)
341 pr_warn("failed to change proc name for %s\n",
344 case NETDEV_REGISTER:
345 if (vlan_proc_add_dev(dev) < 0)
346 pr_warn("failed to add proc entry for %s\n", dev->name);
348 case NETDEV_UNREGISTER:
349 vlan_proc_rem_dev(dev);
354 static int vlan_device_event(struct notifier_block *unused, unsigned long event,
357 struct net_device *dev = ptr;
358 struct vlan_group *grp;
360 struct net_device *vlandev;
361 struct vlan_dev_info *vlan;
364 if (is_vlan_dev(dev))
365 __vlan_device_event(dev, event);
367 if ((event == NETDEV_UP) &&
368 (dev->features & NETIF_F_HW_VLAN_FILTER) &&
369 dev->netdev_ops->ndo_vlan_rx_add_vid) {
370 pr_info("adding VLAN 0 to HW filter on device %s\n",
372 dev->netdev_ops->ndo_vlan_rx_add_vid(dev, 0);
375 grp = rtnl_dereference(dev->vlgrp);
379 /* It is OK that we do not hold the group lock right now,
380 * as we run under the RTNL lock.
385 /* Propagate real device state to vlan devices */
386 for (i = 0; i < VLAN_N_VID; i++) {
387 vlandev = vlan_group_get_device(grp, i);
391 netif_stacked_transfer_operstate(dev, vlandev);
395 case NETDEV_CHANGEADDR:
396 /* Adjust unicast filters on underlying device */
397 for (i = 0; i < VLAN_N_VID; i++) {
398 vlandev = vlan_group_get_device(grp, i);
402 flgs = vlandev->flags;
403 if (!(flgs & IFF_UP))
406 vlan_sync_address(dev, vlandev);
410 case NETDEV_CHANGEMTU:
411 for (i = 0; i < VLAN_N_VID; i++) {
412 vlandev = vlan_group_get_device(grp, i);
416 if (vlandev->mtu <= dev->mtu)
419 dev_set_mtu(vlandev, dev->mtu);
423 case NETDEV_FEAT_CHANGE:
424 /* Propagate device features to underlying device */
425 for (i = 0; i < VLAN_N_VID; i++) {
426 vlandev = vlan_group_get_device(grp, i);
430 vlan_transfer_features(dev, vlandev);
436 /* Put all VLANs for this dev in the down state too. */
437 for (i = 0; i < VLAN_N_VID; i++) {
438 vlandev = vlan_group_get_device(grp, i);
442 flgs = vlandev->flags;
443 if (!(flgs & IFF_UP))
446 vlan = vlan_dev_info(vlandev);
447 if (!(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
448 dev_change_flags(vlandev, flgs & ~IFF_UP);
449 netif_stacked_transfer_operstate(dev, vlandev);
454 /* Put all VLANs for this dev in the up state too. */
455 for (i = 0; i < VLAN_N_VID; i++) {
456 vlandev = vlan_group_get_device(grp, i);
460 flgs = vlandev->flags;
464 vlan = vlan_dev_info(vlandev);
465 if (!(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
466 dev_change_flags(vlandev, flgs | IFF_UP);
467 netif_stacked_transfer_operstate(dev, vlandev);
471 case NETDEV_UNREGISTER:
472 /* twiddle thumbs on netns device moves */
473 if (dev->reg_state != NETREG_UNREGISTERING)
476 for (i = 0; i < VLAN_N_VID; i++) {
477 vlandev = vlan_group_get_device(grp, i);
481 /* unregistration of last vlan destroys group, abort
483 if (grp->nr_vlans == 1)
486 unregister_vlan_dev(vlandev, &list);
488 unregister_netdevice_many(&list);
491 case NETDEV_PRE_TYPE_CHANGE:
492 /* Forbid underlaying device to change its type. */
495 case NETDEV_NOTIFY_PEERS:
496 case NETDEV_BONDING_FAILOVER:
497 /* Propagate to vlan devices */
498 for (i = 0; i < VLAN_N_VID; i++) {
499 vlandev = vlan_group_get_device(grp, i);
503 call_netdevice_notifiers(event, vlandev);
512 static struct notifier_block vlan_notifier_block __read_mostly = {
513 .notifier_call = vlan_device_event,
517 * VLAN IOCTL handler.
518 * o execute requested action or pass command to the device driver
519 * arg is really a struct vlan_ioctl_args __user *.
521 static int vlan_ioctl_handler(struct net *net, void __user *arg)
524 struct vlan_ioctl_args args;
525 struct net_device *dev = NULL;
527 if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args)))
530 /* Null terminate this sucker, just in case. */
531 args.device1[23] = 0;
532 args.u.device2[23] = 0;
537 case SET_VLAN_INGRESS_PRIORITY_CMD:
538 case SET_VLAN_EGRESS_PRIORITY_CMD:
539 case SET_VLAN_FLAG_CMD:
542 case GET_VLAN_REALDEV_NAME_CMD:
543 case GET_VLAN_VID_CMD:
545 dev = __dev_get_by_name(net, args.device1);
550 if (args.cmd != ADD_VLAN_CMD && !is_vlan_dev(dev))
555 case SET_VLAN_INGRESS_PRIORITY_CMD:
557 if (!capable(CAP_NET_ADMIN))
559 vlan_dev_set_ingress_priority(dev,
565 case SET_VLAN_EGRESS_PRIORITY_CMD:
567 if (!capable(CAP_NET_ADMIN))
569 err = vlan_dev_set_egress_priority(dev,
574 case SET_VLAN_FLAG_CMD:
576 if (!capable(CAP_NET_ADMIN))
578 err = vlan_dev_change_flags(dev,
579 args.vlan_qos ? args.u.flag : 0,
583 case SET_VLAN_NAME_TYPE_CMD:
585 if (!capable(CAP_NET_ADMIN))
587 if ((args.u.name_type >= 0) &&
588 (args.u.name_type < VLAN_NAME_TYPE_HIGHEST)) {
591 vn = net_generic(net, vlan_net_id);
592 vn->name_type = args.u.name_type;
601 if (!capable(CAP_NET_ADMIN))
603 err = register_vlan_device(dev, args.u.VID);
608 if (!capable(CAP_NET_ADMIN))
610 unregister_vlan_dev(dev, NULL);
614 case GET_VLAN_REALDEV_NAME_CMD:
616 vlan_dev_get_realdev_name(dev, args.u.device2);
617 if (copy_to_user(arg, &args,
618 sizeof(struct vlan_ioctl_args)))
622 case GET_VLAN_VID_CMD:
624 args.u.VID = vlan_dev_vlan_id(dev);
625 if (copy_to_user(arg, &args,
626 sizeof(struct vlan_ioctl_args)))
639 static int __net_init vlan_init_net(struct net *net)
641 struct vlan_net *vn = net_generic(net, vlan_net_id);
644 vn->name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;
646 err = vlan_proc_init(net);
651 static void __net_exit vlan_exit_net(struct net *net)
653 vlan_proc_cleanup(net);
656 static struct pernet_operations vlan_net_ops = {
657 .init = vlan_init_net,
658 .exit = vlan_exit_net,
660 .size = sizeof(struct vlan_net),
663 static int __init vlan_proto_init(void)
667 pr_info("%s v%s\n", vlan_fullname, vlan_version);
669 err = register_pernet_subsys(&vlan_net_ops);
673 err = register_netdevice_notifier(&vlan_notifier_block);
677 err = vlan_gvrp_init();
681 err = vlan_netlink_init();
685 vlan_ioctl_set(vlan_ioctl_handler);
691 unregister_netdevice_notifier(&vlan_notifier_block);
693 unregister_pernet_subsys(&vlan_net_ops);
698 static void __exit vlan_cleanup_module(void)
700 vlan_ioctl_set(NULL);
703 unregister_netdevice_notifier(&vlan_notifier_block);
705 unregister_pernet_subsys(&vlan_net_ops);
706 rcu_barrier(); /* Wait for completion of call_rcu()'s */
711 module_init(vlan_proto_init);
712 module_exit(vlan_cleanup_module);
714 MODULE_LICENSE("GPL");
715 MODULE_VERSION(DRV_VERSION);