[VLAN]: Use dev->stats
[linux-2.6.git] / net / 8021q / vlan.c
blob54f2346964112e5535af96f635e7e4868fb9b1f2
1 /*
2 * INET 802.1Q VLAN
3 * Ethernet-type device handling.
5 * Authors: Ben Greear <greearb@candelatech.com>
6 * Please send support related email to: vlan@scry.wanfear.com
7 * VLAN Home Page: http://www.candelatech.com/~greear/vlan.html
9 * Fixes:
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 #include <asm/uaccess.h> /* for copy_from_user */
22 #include <linux/capability.h>
23 #include <linux/module.h>
24 #include <linux/netdevice.h>
25 #include <linux/skbuff.h>
26 #include <net/datalink.h>
27 #include <linux/mm.h>
28 #include <linux/in.h>
29 #include <linux/init.h>
30 #include <net/p8022.h>
31 #include <net/arp.h>
32 #include <linux/rtnetlink.h>
33 #include <linux/notifier.h>
34 #include <net/net_namespace.h>
36 #include <linux/if_vlan.h>
37 #include "vlan.h"
38 #include "vlanproc.h"
40 #define DRV_VERSION "1.8"
42 /* Global VLAN variables */
44 /* Our listing of VLAN group(s) */
45 static struct hlist_head vlan_group_hash[VLAN_GRP_HASH_SIZE];
46 #define vlan_grp_hashfn(IDX) ((((IDX) >> VLAN_GRP_HASH_SHIFT) ^ (IDX)) & VLAN_GRP_HASH_MASK)
48 static char vlan_fullname[] = "802.1Q VLAN Support";
49 static char vlan_version[] = DRV_VERSION;
50 static char vlan_copyright[] = "Ben Greear <greearb@candelatech.com>";
51 static char vlan_buggyright[] = "David S. Miller <davem@redhat.com>";
53 static int vlan_device_event(struct notifier_block *, unsigned long, void *);
54 static int vlan_ioctl_handler(struct net *net, void __user *);
55 static int unregister_vlan_dev(struct net_device *, unsigned short );
57 static struct notifier_block vlan_notifier_block = {
58 .notifier_call = vlan_device_event,
61 /* These may be changed at run-time through IOCTLs */
63 /* Determines interface naming scheme. */
64 unsigned short vlan_name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;
66 static struct packet_type vlan_packet_type = {
67 .type = __constant_htons(ETH_P_8021Q),
68 .func = vlan_skb_recv, /* VLAN receive method */
71 /* End of global variables definitions. */
74 * Function vlan_proto_init (pro)
76 * Initialize VLAN protocol layer,
79 static int __init vlan_proto_init(void)
81 int err;
83 printk(VLAN_INF "%s v%s %s\n",
84 vlan_fullname, vlan_version, vlan_copyright);
85 printk(VLAN_INF "All bugs added by %s\n",
86 vlan_buggyright);
88 /* proc file system initialization */
89 err = vlan_proc_init();
90 if (err < 0) {
91 printk(KERN_ERR
92 "%s: can't create entry in proc filesystem!\n",
93 __FUNCTION__);
94 return err;
97 dev_add_pack(&vlan_packet_type);
99 /* Register us to receive netdevice events */
100 err = register_netdevice_notifier(&vlan_notifier_block);
101 if (err < 0)
102 goto err1;
104 err = vlan_netlink_init();
105 if (err < 0)
106 goto err2;
108 vlan_ioctl_set(vlan_ioctl_handler);
109 return 0;
111 err2:
112 unregister_netdevice_notifier(&vlan_notifier_block);
113 err1:
114 vlan_proc_cleanup();
115 dev_remove_pack(&vlan_packet_type);
116 return err;
120 * Module 'remove' entry point.
121 * o delete /proc/net/router directory and static entries.
123 static void __exit vlan_cleanup_module(void)
125 int i;
127 vlan_ioctl_set(NULL);
128 vlan_netlink_fini();
130 /* Un-register us from receiving netdevice events */
131 unregister_netdevice_notifier(&vlan_notifier_block);
133 dev_remove_pack(&vlan_packet_type);
135 /* This table must be empty if there are no module
136 * references left.
138 for (i = 0; i < VLAN_GRP_HASH_SIZE; i++) {
139 BUG_ON(!hlist_empty(&vlan_group_hash[i]));
141 vlan_proc_cleanup();
143 synchronize_net();
146 module_init(vlan_proto_init);
147 module_exit(vlan_cleanup_module);
149 /* Must be invoked with RCU read lock (no preempt) */
150 static struct vlan_group *__vlan_find_group(int real_dev_ifindex)
152 struct vlan_group *grp;
153 struct hlist_node *n;
154 int hash = vlan_grp_hashfn(real_dev_ifindex);
156 hlist_for_each_entry_rcu(grp, n, &vlan_group_hash[hash], hlist) {
157 if (grp->real_dev_ifindex == real_dev_ifindex)
158 return grp;
161 return NULL;
164 /* Find the protocol handler. Assumes VID < VLAN_VID_MASK.
166 * Must be invoked with RCU read lock (no preempt)
168 struct net_device *__find_vlan_dev(struct net_device *real_dev,
169 unsigned short VID)
171 struct vlan_group *grp = __vlan_find_group(real_dev->ifindex);
173 if (grp)
174 return vlan_group_get_device(grp, VID);
176 return NULL;
179 static void vlan_group_free(struct vlan_group *grp)
181 int i;
183 for (i=0; i < VLAN_GROUP_ARRAY_SPLIT_PARTS; i++)
184 kfree(grp->vlan_devices_arrays[i]);
185 kfree(grp);
188 static struct vlan_group *vlan_group_alloc(int ifindex)
190 struct vlan_group *grp;
191 unsigned int size;
192 unsigned int i;
194 grp = kzalloc(sizeof(struct vlan_group), GFP_KERNEL);
195 if (!grp)
196 return NULL;
198 size = sizeof(struct net_device *) * VLAN_GROUP_ARRAY_PART_LEN;
200 for (i = 0; i < VLAN_GROUP_ARRAY_SPLIT_PARTS; i++) {
201 grp->vlan_devices_arrays[i] = kzalloc(size, GFP_KERNEL);
202 if (!grp->vlan_devices_arrays[i])
203 goto err;
206 grp->real_dev_ifindex = ifindex;
207 hlist_add_head_rcu(&grp->hlist,
208 &vlan_group_hash[vlan_grp_hashfn(ifindex)]);
209 return grp;
211 err:
212 vlan_group_free(grp);
213 return NULL;
216 static void vlan_rcu_free(struct rcu_head *rcu)
218 vlan_group_free(container_of(rcu, struct vlan_group, rcu));
222 /* This returns 0 if everything went fine.
223 * It will return 1 if the group was killed as a result.
224 * A negative return indicates failure.
226 * The RTNL lock must be held.
228 static int unregister_vlan_dev(struct net_device *real_dev,
229 unsigned short vlan_id)
231 struct net_device *dev = NULL;
232 int real_dev_ifindex = real_dev->ifindex;
233 struct vlan_group *grp;
234 int i, ret;
236 #ifdef VLAN_DEBUG
237 printk(VLAN_DBG "%s: VID: %i\n", __FUNCTION__, vlan_id);
238 #endif
240 /* sanity check */
241 if (vlan_id >= VLAN_VID_MASK)
242 return -EINVAL;
244 ASSERT_RTNL();
245 grp = __vlan_find_group(real_dev_ifindex);
247 ret = 0;
249 if (grp) {
250 dev = vlan_group_get_device(grp, vlan_id);
251 if (dev) {
252 /* Remove proc entry */
253 vlan_proc_rem_dev(dev);
255 /* Take it out of our own structures, but be sure to
256 * interlock with HW accelerating devices or SW vlan
257 * input packet processing.
259 if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
260 real_dev->vlan_rx_kill_vid(real_dev, vlan_id);
262 vlan_group_set_device(grp, vlan_id, NULL);
263 synchronize_net();
266 /* Caller unregisters (and if necessary, puts)
267 * VLAN device, but we get rid of the reference to
268 * real_dev here.
270 dev_put(real_dev);
272 /* If the group is now empty, kill off the
273 * group.
275 for (i = 0; i < VLAN_VID_MASK; i++)
276 if (vlan_group_get_device(grp, i))
277 break;
279 if (i == VLAN_VID_MASK) {
280 if (real_dev->features & NETIF_F_HW_VLAN_RX)
281 real_dev->vlan_rx_register(real_dev, NULL);
283 hlist_del_rcu(&grp->hlist);
285 /* Free the group, after all cpu's are done. */
286 call_rcu(&grp->rcu, vlan_rcu_free);
288 grp = NULL;
289 ret = 1;
294 return ret;
297 int unregister_vlan_device(struct net_device *dev)
299 int ret;
301 ret = unregister_vlan_dev(VLAN_DEV_INFO(dev)->real_dev,
302 VLAN_DEV_INFO(dev)->vlan_id);
303 unregister_netdevice(dev);
305 if (ret == 1)
306 ret = 0;
307 return ret;
311 * vlan network devices have devices nesting below it, and are a special
312 * "super class" of normal network devices; split their locks off into a
313 * separate class since they always nest.
315 static struct lock_class_key vlan_netdev_xmit_lock_key;
317 static const struct header_ops vlan_header_ops = {
318 .create = vlan_dev_hard_header,
319 .rebuild = vlan_dev_rebuild_header,
320 .parse = eth_header_parse,
323 static int vlan_dev_init(struct net_device *dev)
325 struct net_device *real_dev = VLAN_DEV_INFO(dev)->real_dev;
326 int subclass = 0;
328 /* IFF_BROADCAST|IFF_MULTICAST; ??? */
329 dev->flags = real_dev->flags & ~IFF_UP;
330 dev->iflink = real_dev->ifindex;
331 dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
332 (1<<__LINK_STATE_DORMANT))) |
333 (1<<__LINK_STATE_PRESENT);
335 /* ipv6 shared card related stuff */
336 dev->dev_id = real_dev->dev_id;
338 if (is_zero_ether_addr(dev->dev_addr))
339 memcpy(dev->dev_addr, real_dev->dev_addr, dev->addr_len);
340 if (is_zero_ether_addr(dev->broadcast))
341 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
343 if (real_dev->features & NETIF_F_HW_VLAN_TX) {
344 dev->header_ops = real_dev->header_ops;
345 dev->hard_header_len = real_dev->hard_header_len;
346 dev->hard_start_xmit = vlan_dev_hwaccel_hard_start_xmit;
347 } else {
348 dev->header_ops = &vlan_header_ops;
349 dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
350 dev->hard_start_xmit = vlan_dev_hard_start_xmit;
353 if (real_dev->priv_flags & IFF_802_1Q_VLAN)
354 subclass = 1;
356 lockdep_set_class_and_subclass(&dev->_xmit_lock,
357 &vlan_netdev_xmit_lock_key, subclass);
358 return 0;
361 void vlan_setup(struct net_device *new_dev)
363 ether_setup(new_dev);
365 /* new_dev->ifindex = 0; it will be set when added to
366 * the global list.
367 * iflink is set as well.
369 /* Make this thing known as a VLAN device */
370 new_dev->priv_flags |= IFF_802_1Q_VLAN;
372 /* Set us up to have no queue, as the underlying Hardware device
373 * can do all the queueing we could want.
375 new_dev->tx_queue_len = 0;
377 /* set up method calls */
378 new_dev->change_mtu = vlan_dev_change_mtu;
379 new_dev->init = vlan_dev_init;
380 new_dev->open = vlan_dev_open;
381 new_dev->stop = vlan_dev_stop;
382 new_dev->set_mac_address = vlan_set_mac_address;
383 new_dev->set_multicast_list = vlan_dev_set_multicast_list;
384 new_dev->change_rx_flags = vlan_change_rx_flags;
385 new_dev->destructor = free_netdev;
386 new_dev->do_ioctl = vlan_dev_ioctl;
388 memset(new_dev->broadcast, 0, ETH_ALEN);
391 static void vlan_transfer_operstate(const struct net_device *dev, struct net_device *vlandev)
393 /* Have to respect userspace enforced dormant state
394 * of real device, also must allow supplicant running
395 * on VLAN device
397 if (dev->operstate == IF_OPER_DORMANT)
398 netif_dormant_on(vlandev);
399 else
400 netif_dormant_off(vlandev);
402 if (netif_carrier_ok(dev)) {
403 if (!netif_carrier_ok(vlandev))
404 netif_carrier_on(vlandev);
405 } else {
406 if (netif_carrier_ok(vlandev))
407 netif_carrier_off(vlandev);
411 int vlan_check_real_dev(struct net_device *real_dev, unsigned short vlan_id)
413 if (real_dev->features & NETIF_F_VLAN_CHALLENGED) {
414 printk(VLAN_DBG "%s: VLANs not supported on %s.\n",
415 __FUNCTION__, real_dev->name);
416 return -EOPNOTSUPP;
419 if ((real_dev->features & NETIF_F_HW_VLAN_RX) &&
420 !real_dev->vlan_rx_register) {
421 printk(VLAN_DBG "%s: Device %s has buggy VLAN hw accel.\n",
422 __FUNCTION__, real_dev->name);
423 return -EOPNOTSUPP;
426 if ((real_dev->features & NETIF_F_HW_VLAN_FILTER) &&
427 (!real_dev->vlan_rx_add_vid || !real_dev->vlan_rx_kill_vid)) {
428 printk(VLAN_DBG "%s: Device %s has buggy VLAN hw accel.\n",
429 __FUNCTION__, real_dev->name);
430 return -EOPNOTSUPP;
433 /* The real device must be up and operating in order to
434 * assosciate a VLAN device with it.
436 if (!(real_dev->flags & IFF_UP))
437 return -ENETDOWN;
439 if (__find_vlan_dev(real_dev, vlan_id) != NULL) {
440 /* was already registered. */
441 printk(VLAN_DBG "%s: ALREADY had VLAN registered\n", __FUNCTION__);
442 return -EEXIST;
445 return 0;
448 int register_vlan_dev(struct net_device *dev)
450 struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev);
451 struct net_device *real_dev = vlan->real_dev;
452 unsigned short vlan_id = vlan->vlan_id;
453 struct vlan_group *grp, *ngrp = NULL;
454 int err;
456 grp = __vlan_find_group(real_dev->ifindex);
457 if (!grp) {
458 ngrp = grp = vlan_group_alloc(real_dev->ifindex);
459 if (!grp)
460 return -ENOBUFS;
463 err = register_netdevice(dev);
464 if (err < 0)
465 goto out_free_group;
467 /* Account for reference in struct vlan_dev_info */
468 dev_hold(real_dev);
470 vlan_transfer_operstate(real_dev, dev);
471 linkwatch_fire_event(dev); /* _MUST_ call rfc2863_policy() */
473 /* So, got the sucker initialized, now lets place
474 * it into our local structure.
476 vlan_group_set_device(grp, vlan_id, dev);
477 if (ngrp && real_dev->features & NETIF_F_HW_VLAN_RX)
478 real_dev->vlan_rx_register(real_dev, ngrp);
479 if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
480 real_dev->vlan_rx_add_vid(real_dev, vlan_id);
482 if (vlan_proc_add_dev(dev) < 0)
483 printk(KERN_WARNING "VLAN: failed to add proc entry for %s\n",
484 dev->name);
485 return 0;
487 out_free_group:
488 if (ngrp)
489 vlan_group_free(ngrp);
490 return err;
493 /* Attach a VLAN device to a mac address (ie Ethernet Card).
494 * Returns 0 if the device was created or a negative error code otherwise.
496 static int register_vlan_device(struct net_device *real_dev,
497 unsigned short VLAN_ID)
499 struct net_device *new_dev;
500 char name[IFNAMSIZ];
501 int err;
503 #ifdef VLAN_DEBUG
504 printk(VLAN_DBG "%s: if_name -:%s:- vid: %i\n",
505 __FUNCTION__, eth_IF_name, VLAN_ID);
506 #endif
508 if (VLAN_ID >= VLAN_VID_MASK)
509 return -ERANGE;
511 err = vlan_check_real_dev(real_dev, VLAN_ID);
512 if (err < 0)
513 return err;
515 /* Gotta set up the fields for the device. */
516 #ifdef VLAN_DEBUG
517 printk(VLAN_DBG "About to allocate name, vlan_name_type: %i\n",
518 vlan_name_type);
519 #endif
520 switch (vlan_name_type) {
521 case VLAN_NAME_TYPE_RAW_PLUS_VID:
522 /* name will look like: eth1.0005 */
523 snprintf(name, IFNAMSIZ, "%s.%.4i", real_dev->name, VLAN_ID);
524 break;
525 case VLAN_NAME_TYPE_PLUS_VID_NO_PAD:
526 /* Put our vlan.VID in the name.
527 * Name will look like: vlan5
529 snprintf(name, IFNAMSIZ, "vlan%i", VLAN_ID);
530 break;
531 case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD:
532 /* Put our vlan.VID in the name.
533 * Name will look like: eth0.5
535 snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, VLAN_ID);
536 break;
537 case VLAN_NAME_TYPE_PLUS_VID:
538 /* Put our vlan.VID in the name.
539 * Name will look like: vlan0005
541 default:
542 snprintf(name, IFNAMSIZ, "vlan%.4i", VLAN_ID);
545 new_dev = alloc_netdev(sizeof(struct vlan_dev_info), name,
546 vlan_setup);
548 if (new_dev == NULL)
549 return -ENOBUFS;
551 /* need 4 bytes for extra VLAN header info,
552 * hope the underlying device can handle it.
554 new_dev->mtu = real_dev->mtu;
556 #ifdef VLAN_DEBUG
557 printk(VLAN_DBG "Allocated new name -:%s:-\n", new_dev->name);
558 VLAN_MEM_DBG("new_dev->priv malloc, addr: %p size: %i\n",
559 new_dev->priv,
560 sizeof(struct vlan_dev_info));
561 #endif
563 VLAN_DEV_INFO(new_dev)->vlan_id = VLAN_ID; /* 1 through VLAN_VID_MASK */
564 VLAN_DEV_INFO(new_dev)->real_dev = real_dev;
565 VLAN_DEV_INFO(new_dev)->dent = NULL;
566 VLAN_DEV_INFO(new_dev)->flags = VLAN_FLAG_REORDER_HDR;
568 new_dev->rtnl_link_ops = &vlan_link_ops;
569 err = register_vlan_dev(new_dev);
570 if (err < 0)
571 goto out_free_newdev;
573 #ifdef VLAN_DEBUG
574 printk(VLAN_DBG "Allocated new device successfully, returning.\n");
575 #endif
576 return 0;
578 out_free_newdev:
579 free_netdev(new_dev);
580 return err;
583 static void vlan_sync_address(struct net_device *dev,
584 struct net_device *vlandev)
586 struct vlan_dev_info *vlan = VLAN_DEV_INFO(vlandev);
588 /* May be called without an actual change */
589 if (!compare_ether_addr(vlan->real_dev_addr, dev->dev_addr))
590 return;
592 /* vlan address was different from the old address and is equal to
593 * the new address */
594 if (compare_ether_addr(vlandev->dev_addr, vlan->real_dev_addr) &&
595 !compare_ether_addr(vlandev->dev_addr, dev->dev_addr))
596 dev_unicast_delete(dev, vlandev->dev_addr, ETH_ALEN);
598 /* vlan address was equal to the old address and is different from
599 * the new address */
600 if (!compare_ether_addr(vlandev->dev_addr, vlan->real_dev_addr) &&
601 compare_ether_addr(vlandev->dev_addr, dev->dev_addr))
602 dev_unicast_add(dev, vlandev->dev_addr, ETH_ALEN);
604 memcpy(vlan->real_dev_addr, dev->dev_addr, ETH_ALEN);
607 static int vlan_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
609 struct net_device *dev = ptr;
610 struct vlan_group *grp = __vlan_find_group(dev->ifindex);
611 int i, flgs;
612 struct net_device *vlandev;
614 if (dev->nd_net != &init_net)
615 return NOTIFY_DONE;
617 if (!grp)
618 goto out;
620 /* It is OK that we do not hold the group lock right now,
621 * as we run under the RTNL lock.
624 switch (event) {
625 case NETDEV_CHANGE:
626 /* Propagate real device state to vlan devices */
627 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
628 vlandev = vlan_group_get_device(grp, i);
629 if (!vlandev)
630 continue;
632 vlan_transfer_operstate(dev, vlandev);
634 break;
636 case NETDEV_CHANGEADDR:
637 /* Adjust unicast filters on underlying device */
638 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
639 vlandev = vlan_group_get_device(grp, i);
640 if (!vlandev)
641 continue;
643 flgs = vlandev->flags;
644 if (!(flgs & IFF_UP))
645 continue;
647 vlan_sync_address(dev, vlandev);
649 break;
651 case NETDEV_DOWN:
652 /* Put all VLANs for this dev in the down state too. */
653 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
654 vlandev = vlan_group_get_device(grp, i);
655 if (!vlandev)
656 continue;
658 flgs = vlandev->flags;
659 if (!(flgs & IFF_UP))
660 continue;
662 dev_change_flags(vlandev, flgs & ~IFF_UP);
664 break;
666 case NETDEV_UP:
667 /* Put all VLANs for this dev in the up state too. */
668 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
669 vlandev = vlan_group_get_device(grp, i);
670 if (!vlandev)
671 continue;
673 flgs = vlandev->flags;
674 if (flgs & IFF_UP)
675 continue;
677 dev_change_flags(vlandev, flgs | IFF_UP);
679 break;
681 case NETDEV_UNREGISTER:
682 /* Delete all VLANs for this dev. */
683 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
684 int ret;
686 vlandev = vlan_group_get_device(grp, i);
687 if (!vlandev)
688 continue;
690 ret = unregister_vlan_dev(dev,
691 VLAN_DEV_INFO(vlandev)->vlan_id);
693 unregister_netdevice(vlandev);
695 /* Group was destroyed? */
696 if (ret == 1)
697 break;
699 break;
702 out:
703 return NOTIFY_DONE;
707 * VLAN IOCTL handler.
708 * o execute requested action or pass command to the device driver
709 * arg is really a struct vlan_ioctl_args __user *.
711 static int vlan_ioctl_handler(struct net *net, void __user *arg)
713 int err;
714 unsigned short vid = 0;
715 struct vlan_ioctl_args args;
716 struct net_device *dev = NULL;
718 if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args)))
719 return -EFAULT;
721 /* Null terminate this sucker, just in case. */
722 args.device1[23] = 0;
723 args.u.device2[23] = 0;
725 #ifdef VLAN_DEBUG
726 printk(VLAN_DBG "%s: args.cmd: %x\n", __FUNCTION__, args.cmd);
727 #endif
729 rtnl_lock();
731 switch (args.cmd) {
732 case SET_VLAN_INGRESS_PRIORITY_CMD:
733 case SET_VLAN_EGRESS_PRIORITY_CMD:
734 case SET_VLAN_FLAG_CMD:
735 case ADD_VLAN_CMD:
736 case DEL_VLAN_CMD:
737 case GET_VLAN_REALDEV_NAME_CMD:
738 case GET_VLAN_VID_CMD:
739 err = -ENODEV;
740 dev = __dev_get_by_name(&init_net, args.device1);
741 if (!dev)
742 goto out;
744 err = -EINVAL;
745 if (args.cmd != ADD_VLAN_CMD &&
746 !(dev->priv_flags & IFF_802_1Q_VLAN))
747 goto out;
750 switch (args.cmd) {
751 case SET_VLAN_INGRESS_PRIORITY_CMD:
752 err = -EPERM;
753 if (!capable(CAP_NET_ADMIN))
754 break;
755 vlan_dev_set_ingress_priority(dev,
756 args.u.skb_priority,
757 args.vlan_qos);
758 err = 0;
759 break;
761 case SET_VLAN_EGRESS_PRIORITY_CMD:
762 err = -EPERM;
763 if (!capable(CAP_NET_ADMIN))
764 break;
765 err = vlan_dev_set_egress_priority(dev,
766 args.u.skb_priority,
767 args.vlan_qos);
768 break;
770 case SET_VLAN_FLAG_CMD:
771 err = -EPERM;
772 if (!capable(CAP_NET_ADMIN))
773 break;
774 err = vlan_dev_set_vlan_flag(dev,
775 args.u.flag,
776 args.vlan_qos);
777 break;
779 case SET_VLAN_NAME_TYPE_CMD:
780 err = -EPERM;
781 if (!capable(CAP_NET_ADMIN))
782 break;
783 if ((args.u.name_type >= 0) &&
784 (args.u.name_type < VLAN_NAME_TYPE_HIGHEST)) {
785 vlan_name_type = args.u.name_type;
786 err = 0;
787 } else {
788 err = -EINVAL;
790 break;
792 case ADD_VLAN_CMD:
793 err = -EPERM;
794 if (!capable(CAP_NET_ADMIN))
795 break;
796 err = register_vlan_device(dev, args.u.VID);
797 break;
799 case DEL_VLAN_CMD:
800 err = -EPERM;
801 if (!capable(CAP_NET_ADMIN))
802 break;
803 err = unregister_vlan_device(dev);
804 break;
806 case GET_VLAN_INGRESS_PRIORITY_CMD:
807 /* TODO: Implement
808 err = vlan_dev_get_ingress_priority(args);
809 if (copy_to_user((void*)arg, &args,
810 sizeof(struct vlan_ioctl_args))) {
811 err = -EFAULT;
814 err = -EINVAL;
815 break;
816 case GET_VLAN_EGRESS_PRIORITY_CMD:
817 /* TODO: Implement
818 err = vlan_dev_get_egress_priority(args.device1, &(args.args);
819 if (copy_to_user((void*)arg, &args,
820 sizeof(struct vlan_ioctl_args))) {
821 err = -EFAULT;
824 err = -EINVAL;
825 break;
826 case GET_VLAN_REALDEV_NAME_CMD:
827 err = 0;
828 vlan_dev_get_realdev_name(dev, args.u.device2);
829 if (copy_to_user(arg, &args,
830 sizeof(struct vlan_ioctl_args))) {
831 err = -EFAULT;
833 break;
835 case GET_VLAN_VID_CMD:
836 err = 0;
837 vlan_dev_get_vid(dev, &vid);
838 args.u.VID = vid;
839 if (copy_to_user(arg, &args,
840 sizeof(struct vlan_ioctl_args))) {
841 err = -EFAULT;
843 break;
845 default:
846 /* pass on to underlying device instead?? */
847 printk(VLAN_DBG "%s: Unknown VLAN CMD: %x \n",
848 __FUNCTION__, args.cmd);
849 err = -EINVAL;
850 break;
852 out:
853 rtnl_unlock();
854 return err;
857 MODULE_LICENSE("GPL");
858 MODULE_VERSION(DRV_VERSION);