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_priv *vlan = vlan_dev_priv(dev);
103 struct net_device *real_dev = vlan->real_dev;
104 struct vlan_group *grp;
105 u16 vlan_id = vlan->vlan_id;
109 grp = rtnl_dereference(real_dev->vlgrp);
112 /* Take it out of our own structures, but be sure to interlock with
113 * HW accelerating devices or SW vlan input packet processing if
114 * VLAN is not 0 (leave it there for 802.1p).
117 vlan_vid_del(real_dev, vlan_id);
121 if (vlan->flags & VLAN_FLAG_GVRP)
122 vlan_gvrp_request_leave(dev);
124 vlan_group_set_device(grp, vlan_id, NULL);
125 /* Because unregister_netdevice_queue() makes sure at least one rcu
126 * grace period is respected before device freeing,
127 * we dont need to call synchronize_net() here.
129 unregister_netdevice_queue(dev, head);
131 /* If the group is now empty, kill off the group. */
132 if (grp->nr_vlans == 0) {
133 vlan_gvrp_uninit_applicant(real_dev);
135 RCU_INIT_POINTER(real_dev->vlgrp, NULL);
137 /* Free the group, after all cpu's are done. */
138 call_rcu(&grp->rcu, vlan_rcu_free);
141 /* Get rid of the vlan's reference to real_dev */
145 int vlan_check_real_dev(struct net_device *real_dev, u16 vlan_id)
147 const char *name = real_dev->name;
148 const struct net_device_ops *ops = real_dev->netdev_ops;
150 if (real_dev->features & NETIF_F_VLAN_CHALLENGED) {
151 pr_info("VLANs not supported on %s\n", name);
155 if ((real_dev->features & NETIF_F_HW_VLAN_FILTER) &&
156 (!ops->ndo_vlan_rx_add_vid || !ops->ndo_vlan_rx_kill_vid)) {
157 pr_info("Device %s has buggy VLAN hw accel\n", name);
161 if (vlan_find_dev(real_dev, vlan_id) != NULL)
167 int register_vlan_dev(struct net_device *dev)
169 struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
170 struct net_device *real_dev = vlan->real_dev;
171 u16 vlan_id = vlan->vlan_id;
172 struct vlan_group *grp, *ngrp = NULL;
175 grp = rtnl_dereference(real_dev->vlgrp);
177 ngrp = grp = vlan_group_alloc(real_dev);
180 err = vlan_gvrp_init_applicant(real_dev);
185 err = vlan_group_prealloc_vid(grp, vlan_id);
187 goto out_uninit_applicant;
189 err = register_netdevice(dev);
191 goto out_uninit_applicant;
193 /* Account for reference in struct vlan_dev_priv */
196 netif_stacked_transfer_operstate(real_dev, dev);
197 linkwatch_fire_event(dev); /* _MUST_ call rfc2863_policy() */
199 /* So, got the sucker initialized, now lets place
200 * it into our local structure.
202 vlan_group_set_device(grp, vlan_id, dev);
206 rcu_assign_pointer(real_dev->vlgrp, ngrp);
208 vlan_vid_add(real_dev, vlan_id);
212 out_uninit_applicant:
214 vlan_gvrp_uninit_applicant(real_dev);
217 /* Free the group, after all cpu's are done. */
218 call_rcu(&ngrp->rcu, vlan_rcu_free);
223 /* Attach a VLAN device to a mac address (ie Ethernet Card).
224 * Returns 0 if the device was created or a negative error code otherwise.
226 static int register_vlan_device(struct net_device *real_dev, u16 vlan_id)
228 struct net_device *new_dev;
229 struct net *net = dev_net(real_dev);
230 struct vlan_net *vn = net_generic(net, vlan_net_id);
234 if (vlan_id >= VLAN_VID_MASK)
237 err = vlan_check_real_dev(real_dev, vlan_id);
241 /* Gotta set up the fields for the device. */
242 switch (vn->name_type) {
243 case VLAN_NAME_TYPE_RAW_PLUS_VID:
244 /* name will look like: eth1.0005 */
245 snprintf(name, IFNAMSIZ, "%s.%.4i", real_dev->name, vlan_id);
247 case VLAN_NAME_TYPE_PLUS_VID_NO_PAD:
248 /* Put our vlan.VID in the name.
249 * Name will look like: vlan5
251 snprintf(name, IFNAMSIZ, "vlan%i", vlan_id);
253 case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD:
254 /* Put our vlan.VID in the name.
255 * Name will look like: eth0.5
257 snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, vlan_id);
259 case VLAN_NAME_TYPE_PLUS_VID:
260 /* Put our vlan.VID in the name.
261 * Name will look like: vlan0005
264 snprintf(name, IFNAMSIZ, "vlan%.4i", vlan_id);
267 new_dev = alloc_netdev(sizeof(struct vlan_dev_priv), name, vlan_setup);
272 dev_net_set(new_dev, net);
273 /* need 4 bytes for extra VLAN header info,
274 * hope the underlying device can handle it.
276 new_dev->mtu = real_dev->mtu;
278 vlan_dev_priv(new_dev)->vlan_id = vlan_id;
279 vlan_dev_priv(new_dev)->real_dev = real_dev;
280 vlan_dev_priv(new_dev)->dent = NULL;
281 vlan_dev_priv(new_dev)->flags = VLAN_FLAG_REORDER_HDR;
283 new_dev->rtnl_link_ops = &vlan_link_ops;
284 err = register_vlan_dev(new_dev);
286 goto out_free_newdev;
291 free_netdev(new_dev);
295 static void vlan_sync_address(struct net_device *dev,
296 struct net_device *vlandev)
298 struct vlan_dev_priv *vlan = vlan_dev_priv(vlandev);
300 /* May be called without an actual change */
301 if (!compare_ether_addr(vlan->real_dev_addr, dev->dev_addr))
304 /* vlan address was different from the old address and is equal to
306 if (compare_ether_addr(vlandev->dev_addr, vlan->real_dev_addr) &&
307 !compare_ether_addr(vlandev->dev_addr, dev->dev_addr))
308 dev_uc_del(dev, vlandev->dev_addr);
310 /* vlan address was equal to the old address and is different from
312 if (!compare_ether_addr(vlandev->dev_addr, vlan->real_dev_addr) &&
313 compare_ether_addr(vlandev->dev_addr, dev->dev_addr))
314 dev_uc_add(dev, vlandev->dev_addr);
316 memcpy(vlan->real_dev_addr, dev->dev_addr, ETH_ALEN);
319 static void vlan_transfer_features(struct net_device *dev,
320 struct net_device *vlandev)
322 vlandev->gso_max_size = dev->gso_max_size;
324 if (dev->features & NETIF_F_HW_VLAN_TX)
325 vlandev->hard_header_len = dev->hard_header_len;
327 vlandev->hard_header_len = dev->hard_header_len + VLAN_HLEN;
329 #if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
330 vlandev->fcoe_ddp_xid = dev->fcoe_ddp_xid;
333 netdev_update_features(vlandev);
336 static void __vlan_device_event(struct net_device *dev, unsigned long event)
339 case NETDEV_CHANGENAME:
340 vlan_proc_rem_dev(dev);
341 if (vlan_proc_add_dev(dev) < 0)
342 pr_warn("failed to change proc name for %s\n",
345 case NETDEV_REGISTER:
346 if (vlan_proc_add_dev(dev) < 0)
347 pr_warn("failed to add proc entry for %s\n", dev->name);
349 case NETDEV_UNREGISTER:
350 vlan_proc_rem_dev(dev);
355 static int vlan_device_event(struct notifier_block *unused, unsigned long event,
358 struct net_device *dev = ptr;
359 struct vlan_group *grp;
361 struct net_device *vlandev;
362 struct vlan_dev_priv *vlan;
365 if (is_vlan_dev(dev))
366 __vlan_device_event(dev, event);
368 if ((event == NETDEV_UP) &&
369 (dev->features & NETIF_F_HW_VLAN_FILTER)) {
370 pr_info("adding VLAN 0 to HW filter on device %s\n",
372 vlan_vid_add(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_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);
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_priv(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);