]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/8021q/vlan.c
[ETHER]: Bring back MAC_FMT
[mirror_ubuntu-zesty-kernel.git] / net / 8021q / vlan.c
CommitLineData
1da177e4
LT
1/*
2 * INET 802.1Q VLAN
3 * Ethernet-type device handling.
4 *
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
122952fc 8 *
1da177e4
LT
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>
14 *
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.
19 */
20
21#include <asm/uaccess.h> /* for copy_from_user */
4fc268d2 22#include <linux/capability.h>
1da177e4
LT
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>
e9dc8653 34#include <net/net_namespace.h>
1da177e4
LT
35
36#include <linux/if_vlan.h>
37#include "vlan.h"
38#include "vlanproc.h"
39
40#define DRV_VERSION "1.8"
41
42/* Global VLAN variables */
43
44/* Our listing of VLAN group(s) */
45static 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)
47
48static char vlan_fullname[] = "802.1Q VLAN Support";
49static char vlan_version[] = DRV_VERSION;
50static char vlan_copyright[] = "Ben Greear <greearb@candelatech.com>";
51static char vlan_buggyright[] = "David S. Miller <davem@redhat.com>";
52
53static int vlan_device_event(struct notifier_block *, unsigned long, void *);
881d966b 54static int vlan_ioctl_handler(struct net *net, void __user *);
1da177e4
LT
55static int unregister_vlan_dev(struct net_device *, unsigned short );
56
57static struct notifier_block vlan_notifier_block = {
58 .notifier_call = vlan_device_event,
59};
60
61/* These may be changed at run-time through IOCTLs */
62
63/* Determines interface naming scheme. */
64unsigned short vlan_name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;
65
66static struct packet_type vlan_packet_type = {
67 .type = __constant_htons(ETH_P_8021Q),
68 .func = vlan_skb_recv, /* VLAN receive method */
69};
70
1da177e4
LT
71/* End of global variables definitions. */
72
73/*
74 * Function vlan_proto_init (pro)
75 *
122952fc 76 * Initialize VLAN protocol layer,
1da177e4
LT
77 *
78 */
79static int __init vlan_proto_init(void)
80{
81 int err;
82
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);
87
88 /* proc file system initialization */
89 err = vlan_proc_init();
90 if (err < 0) {
122952fc 91 printk(KERN_ERR
b7a4a836
PM
92 "%s: can't create entry in proc filesystem!\n",
93 __FUNCTION__);
1da177e4
LT
94 return err;
95 }
96
97 dev_add_pack(&vlan_packet_type);
98
99 /* Register us to receive netdevice events */
100 err = register_netdevice_notifier(&vlan_notifier_block);
07b5b17e
PM
101 if (err < 0)
102 goto err1;
1da177e4 103
07b5b17e
PM
104 err = vlan_netlink_init();
105 if (err < 0)
106 goto err2;
1da177e4 107
07b5b17e 108 vlan_ioctl_set(vlan_ioctl_handler);
1da177e4 109 return 0;
07b5b17e
PM
110
111err2:
112 unregister_netdevice_notifier(&vlan_notifier_block);
113err1:
114 vlan_proc_cleanup();
115 dev_remove_pack(&vlan_packet_type);
116 return err;
1da177e4
LT
117}
118
1da177e4
LT
119/*
120 * Module 'remove' entry point.
121 * o delete /proc/net/router directory and static entries.
122952fc 122 */
1da177e4
LT
123static void __exit vlan_cleanup_module(void)
124{
125 int i;
126
127 vlan_ioctl_set(NULL);
3f03e387 128 vlan_netlink_fini();
1da177e4
LT
129
130 /* Un-register us from receiving netdevice events */
131 unregister_netdevice_notifier(&vlan_notifier_block);
132
133 dev_remove_pack(&vlan_packet_type);
1da177e4
LT
134
135 /* This table must be empty if there are no module
136 * references left.
137 */
138 for (i = 0; i < VLAN_GRP_HASH_SIZE; i++) {
139 BUG_ON(!hlist_empty(&vlan_group_hash[i]));
140 }
141 vlan_proc_cleanup();
142
143 synchronize_net();
144}
145
146module_init(vlan_proto_init);
147module_exit(vlan_cleanup_module);
148
149/* Must be invoked with RCU read lock (no preempt) */
150static struct vlan_group *__vlan_find_group(int real_dev_ifindex)
151{
152 struct vlan_group *grp;
153 struct hlist_node *n;
154 int hash = vlan_grp_hashfn(real_dev_ifindex);
155
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;
159 }
160
161 return NULL;
162}
163
164/* Find the protocol handler. Assumes VID < VLAN_VID_MASK.
165 *
166 * Must be invoked with RCU read lock (no preempt)
167 */
168struct net_device *__find_vlan_dev(struct net_device *real_dev,
169 unsigned short VID)
170{
171 struct vlan_group *grp = __vlan_find_group(real_dev->ifindex);
172
173 if (grp)
5c15bdec 174 return vlan_group_get_device(grp, VID);
1da177e4
LT
175
176 return NULL;
177}
178
5c15bdec
DA
179static void vlan_group_free(struct vlan_group *grp)
180{
181 int i;
182
183 for (i=0; i < VLAN_GROUP_ARRAY_SPLIT_PARTS; i++)
184 kfree(grp->vlan_devices_arrays[i]);
185 kfree(grp);
186}
187
42429aae
PM
188static struct vlan_group *vlan_group_alloc(int ifindex)
189{
190 struct vlan_group *grp;
191 unsigned int size;
192 unsigned int i;
193
194 grp = kzalloc(sizeof(struct vlan_group), GFP_KERNEL);
195 if (!grp)
196 return NULL;
197
198 size = sizeof(struct net_device *) * VLAN_GROUP_ARRAY_PART_LEN;
199
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;
204 }
205
206 grp->real_dev_ifindex = ifindex;
207 hlist_add_head_rcu(&grp->hlist,
208 &vlan_group_hash[vlan_grp_hashfn(ifindex)]);
209 return grp;
210
211err:
212 vlan_group_free(grp);
213 return NULL;
214}
215
1da177e4
LT
216static void vlan_rcu_free(struct rcu_head *rcu)
217{
5c15bdec 218 vlan_group_free(container_of(rcu, struct vlan_group, rcu));
1da177e4
LT
219}
220
221
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.
225 *
226 * The RTNL lock must be held.
227 */
228static int unregister_vlan_dev(struct net_device *real_dev,
229 unsigned short vlan_id)
230{
231 struct net_device *dev = NULL;
232 int real_dev_ifindex = real_dev->ifindex;
233 struct vlan_group *grp;
234 int i, ret;
235
236#ifdef VLAN_DEBUG
237 printk(VLAN_DBG "%s: VID: %i\n", __FUNCTION__, vlan_id);
238#endif
239
240 /* sanity check */
241 if (vlan_id >= VLAN_VID_MASK)
242 return -EINVAL;
243
244 ASSERT_RTNL();
245 grp = __vlan_find_group(real_dev_ifindex);
246
247 ret = 0;
248
249 if (grp) {
5c15bdec 250 dev = vlan_group_get_device(grp, vlan_id);
1da177e4
LT
251 if (dev) {
252 /* Remove proc entry */
253 vlan_proc_rem_dev(dev);
254
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.
258 */
d2d1acdb 259 if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
1da177e4 260 real_dev->vlan_rx_kill_vid(real_dev, vlan_id);
1da177e4 261
5c15bdec 262 vlan_group_set_device(grp, vlan_id, NULL);
1da177e4
LT
263 synchronize_net();
264
265
266 /* Caller unregisters (and if necessary, puts)
267 * VLAN device, but we get rid of the reference to
268 * real_dev here.
269 */
270 dev_put(real_dev);
271
272 /* If the group is now empty, kill off the
273 * group.
274 */
275 for (i = 0; i < VLAN_VID_MASK; i++)
5c15bdec 276 if (vlan_group_get_device(grp, i))
1da177e4
LT
277 break;
278
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);
282
283 hlist_del_rcu(&grp->hlist);
284
285 /* Free the group, after all cpu's are done. */
286 call_rcu(&grp->rcu, vlan_rcu_free);
287
288 grp = NULL;
289 ret = 1;
290 }
291 }
292 }
293
122952fc 294 return ret;
1da177e4
LT
295}
296
07b5b17e 297int unregister_vlan_device(struct net_device *dev)
1da177e4 298{
1da177e4
LT
299 int ret;
300
c17d8874
PM
301 ret = unregister_vlan_dev(VLAN_DEV_INFO(dev)->real_dev,
302 VLAN_DEV_INFO(dev)->vlan_id);
303 unregister_netdevice(dev);
1da177e4 304
c17d8874
PM
305 if (ret == 1)
306 ret = 0;
1da177e4
LT
307 return ret;
308}
309
ddd7bf9f
SR
310static void vlan_transfer_operstate(const struct net_device *dev, struct net_device *vlandev)
311{
312 /* Have to respect userspace enforced dormant state
313 * of real device, also must allow supplicant running
314 * on VLAN device
315 */
316 if (dev->operstate == IF_OPER_DORMANT)
317 netif_dormant_on(vlandev);
318 else
319 netif_dormant_off(vlandev);
320
321 if (netif_carrier_ok(dev)) {
322 if (!netif_carrier_ok(vlandev))
323 netif_carrier_on(vlandev);
324 } else {
325 if (netif_carrier_ok(vlandev))
326 netif_carrier_off(vlandev);
327 }
328}
329
07b5b17e 330int vlan_check_real_dev(struct net_device *real_dev, unsigned short vlan_id)
1da177e4 331{
1da177e4
LT
332 if (real_dev->features & NETIF_F_VLAN_CHALLENGED) {
333 printk(VLAN_DBG "%s: VLANs not supported on %s.\n",
334 __FUNCTION__, real_dev->name);
c1d3ee99 335 return -EOPNOTSUPP;
1da177e4
LT
336 }
337
338 if ((real_dev->features & NETIF_F_HW_VLAN_RX) &&
d2d1acdb 339 !real_dev->vlan_rx_register) {
1da177e4
LT
340 printk(VLAN_DBG "%s: Device %s has buggy VLAN hw accel.\n",
341 __FUNCTION__, real_dev->name);
c1d3ee99 342 return -EOPNOTSUPP;
1da177e4
LT
343 }
344
345 if ((real_dev->features & NETIF_F_HW_VLAN_FILTER) &&
d2d1acdb 346 (!real_dev->vlan_rx_add_vid || !real_dev->vlan_rx_kill_vid)) {
1da177e4
LT
347 printk(VLAN_DBG "%s: Device %s has buggy VLAN hw accel.\n",
348 __FUNCTION__, real_dev->name);
c1d3ee99 349 return -EOPNOTSUPP;
1da177e4
LT
350 }
351
1da177e4
LT
352 /* The real device must be up and operating in order to
353 * assosciate a VLAN device with it.
354 */
355 if (!(real_dev->flags & IFF_UP))
c1d3ee99 356 return -ENETDOWN;
1da177e4 357
c1d3ee99 358 if (__find_vlan_dev(real_dev, vlan_id) != NULL) {
1da177e4
LT
359 /* was already registered. */
360 printk(VLAN_DBG "%s: ALREADY had VLAN registered\n", __FUNCTION__);
c1d3ee99 361 return -EEXIST;
1da177e4
LT
362 }
363
c1d3ee99
PM
364 return 0;
365}
366
07b5b17e 367int register_vlan_dev(struct net_device *dev)
e89fe42c
PM
368{
369 struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev);
370 struct net_device *real_dev = vlan->real_dev;
371 unsigned short vlan_id = vlan->vlan_id;
372 struct vlan_group *grp, *ngrp = NULL;
373 int err;
374
375 grp = __vlan_find_group(real_dev->ifindex);
376 if (!grp) {
377 ngrp = grp = vlan_group_alloc(real_dev->ifindex);
378 if (!grp)
379 return -ENOBUFS;
380 }
381
382 err = register_netdevice(dev);
383 if (err < 0)
384 goto out_free_group;
385
386 /* Account for reference in struct vlan_dev_info */
387 dev_hold(real_dev);
388
389 vlan_transfer_operstate(real_dev, dev);
390 linkwatch_fire_event(dev); /* _MUST_ call rfc2863_policy() */
391
392 /* So, got the sucker initialized, now lets place
393 * it into our local structure.
394 */
395 vlan_group_set_device(grp, vlan_id, dev);
396 if (ngrp && real_dev->features & NETIF_F_HW_VLAN_RX)
397 real_dev->vlan_rx_register(real_dev, ngrp);
398 if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
399 real_dev->vlan_rx_add_vid(real_dev, vlan_id);
400
401 if (vlan_proc_add_dev(dev) < 0)
402 printk(KERN_WARNING "VLAN: failed to add proc entry for %s\n",
403 dev->name);
404 return 0;
405
406out_free_group:
407 if (ngrp)
408 vlan_group_free(ngrp);
409 return err;
410}
411
c1d3ee99 412/* Attach a VLAN device to a mac address (ie Ethernet Card).
2ae0bf69 413 * Returns 0 if the device was created or a negative error code otherwise.
c1d3ee99 414 */
2ae0bf69
PM
415static int register_vlan_device(struct net_device *real_dev,
416 unsigned short VLAN_ID)
c1d3ee99 417{
c1d3ee99
PM
418 struct net_device *new_dev;
419 char name[IFNAMSIZ];
2ae0bf69 420 int err;
c1d3ee99
PM
421
422#ifdef VLAN_DEBUG
423 printk(VLAN_DBG "%s: if_name -:%s:- vid: %i\n",
424 __FUNCTION__, eth_IF_name, VLAN_ID);
425#endif
426
427 if (VLAN_ID >= VLAN_VID_MASK)
2ae0bf69 428 return -ERANGE;
c1d3ee99 429
2ae0bf69
PM
430 err = vlan_check_real_dev(real_dev, VLAN_ID);
431 if (err < 0)
432 return err;
c1d3ee99 433
1da177e4
LT
434 /* Gotta set up the fields for the device. */
435#ifdef VLAN_DEBUG
436 printk(VLAN_DBG "About to allocate name, vlan_name_type: %i\n",
437 vlan_name_type);
438#endif
439 switch (vlan_name_type) {
440 case VLAN_NAME_TYPE_RAW_PLUS_VID:
441 /* name will look like: eth1.0005 */
442 snprintf(name, IFNAMSIZ, "%s.%.4i", real_dev->name, VLAN_ID);
443 break;
444 case VLAN_NAME_TYPE_PLUS_VID_NO_PAD:
445 /* Put our vlan.VID in the name.
446 * Name will look like: vlan5
447 */
448 snprintf(name, IFNAMSIZ, "vlan%i", VLAN_ID);
449 break;
450 case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD:
451 /* Put our vlan.VID in the name.
452 * Name will look like: eth0.5
453 */
454 snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, VLAN_ID);
455 break;
456 case VLAN_NAME_TYPE_PLUS_VID:
457 /* Put our vlan.VID in the name.
458 * Name will look like: vlan0005
459 */
460 default:
461 snprintf(name, IFNAMSIZ, "vlan%.4i", VLAN_ID);
3ff50b79 462 }
122952fc 463
1da177e4
LT
464 new_dev = alloc_netdev(sizeof(struct vlan_dev_info), name,
465 vlan_setup);
5dd8d1e9 466
1da177e4 467 if (new_dev == NULL)
2ae0bf69 468 return -ENOBUFS;
1da177e4 469
1da177e4
LT
470 /* need 4 bytes for extra VLAN header info,
471 * hope the underlying device can handle it.
472 */
473 new_dev->mtu = real_dev->mtu;
474
2f4284a4
PM
475#ifdef VLAN_DEBUG
476 printk(VLAN_DBG "Allocated new name -:%s:-\n", new_dev->name);
1da177e4
LT
477 VLAN_MEM_DBG("new_dev->priv malloc, addr: %p size: %i\n",
478 new_dev->priv,
479 sizeof(struct vlan_dev_info));
2f4284a4 480#endif
1da177e4
LT
481
482 VLAN_DEV_INFO(new_dev)->vlan_id = VLAN_ID; /* 1 through VLAN_VID_MASK */
483 VLAN_DEV_INFO(new_dev)->real_dev = real_dev;
484 VLAN_DEV_INFO(new_dev)->dent = NULL;
a4bf3af4 485 VLAN_DEV_INFO(new_dev)->flags = VLAN_FLAG_REORDER_HDR;
1da177e4 486
07b5b17e 487 new_dev->rtnl_link_ops = &vlan_link_ops;
2ae0bf69
PM
488 err = register_vlan_dev(new_dev);
489 if (err < 0)
e89fe42c 490 goto out_free_newdev;
1da177e4 491
1da177e4
LT
492#ifdef VLAN_DEBUG
493 printk(VLAN_DBG "Allocated new device successfully, returning.\n");
494#endif
2ae0bf69 495 return 0;
1da177e4 496
1da177e4
LT
497out_free_newdev:
498 free_netdev(new_dev);
2ae0bf69 499 return err;
1da177e4
LT
500}
501
8c979c26
PM
502static void vlan_sync_address(struct net_device *dev,
503 struct net_device *vlandev)
504{
505 struct vlan_dev_info *vlan = VLAN_DEV_INFO(vlandev);
506
507 /* May be called without an actual change */
508 if (!compare_ether_addr(vlan->real_dev_addr, dev->dev_addr))
509 return;
510
511 /* vlan address was different from the old address and is equal to
512 * the new address */
513 if (compare_ether_addr(vlandev->dev_addr, vlan->real_dev_addr) &&
514 !compare_ether_addr(vlandev->dev_addr, dev->dev_addr))
515 dev_unicast_delete(dev, vlandev->dev_addr, ETH_ALEN);
516
517 /* vlan address was equal to the old address and is different from
518 * the new address */
519 if (!compare_ether_addr(vlandev->dev_addr, vlan->real_dev_addr) &&
520 compare_ether_addr(vlandev->dev_addr, dev->dev_addr))
521 dev_unicast_add(dev, vlandev->dev_addr, ETH_ALEN);
522
523 memcpy(vlan->real_dev_addr, dev->dev_addr, ETH_ALEN);
524}
525
1da177e4
LT
526static int vlan_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
527{
528 struct net_device *dev = ptr;
529 struct vlan_group *grp = __vlan_find_group(dev->ifindex);
530 int i, flgs;
531 struct net_device *vlandev;
532
e9dc8653
EB
533 if (dev->nd_net != &init_net)
534 return NOTIFY_DONE;
535
1da177e4
LT
536 if (!grp)
537 goto out;
538
539 /* It is OK that we do not hold the group lock right now,
540 * as we run under the RTNL lock.
541 */
542
543 switch (event) {
544 case NETDEV_CHANGE:
545 /* Propagate real device state to vlan devices */
1da177e4 546 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
5c15bdec 547 vlandev = vlan_group_get_device(grp, i);
1da177e4
LT
548 if (!vlandev)
549 continue;
550
ddd7bf9f 551 vlan_transfer_operstate(dev, vlandev);
1da177e4
LT
552 }
553 break;
554
8c979c26
PM
555 case NETDEV_CHANGEADDR:
556 /* Adjust unicast filters on underlying device */
557 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
558 vlandev = vlan_group_get_device(grp, i);
559 if (!vlandev)
560 continue;
561
d932e04a
PM
562 flgs = vlandev->flags;
563 if (!(flgs & IFF_UP))
564 continue;
565
8c979c26
PM
566 vlan_sync_address(dev, vlandev);
567 }
568 break;
569
1da177e4
LT
570 case NETDEV_DOWN:
571 /* Put all VLANs for this dev in the down state too. */
572 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
5c15bdec 573 vlandev = vlan_group_get_device(grp, i);
1da177e4
LT
574 if (!vlandev)
575 continue;
576
577 flgs = vlandev->flags;
578 if (!(flgs & IFF_UP))
579 continue;
580
581 dev_change_flags(vlandev, flgs & ~IFF_UP);
582 }
583 break;
584
585 case NETDEV_UP:
586 /* Put all VLANs for this dev in the up state too. */
587 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
5c15bdec 588 vlandev = vlan_group_get_device(grp, i);
1da177e4
LT
589 if (!vlandev)
590 continue;
122952fc 591
1da177e4
LT
592 flgs = vlandev->flags;
593 if (flgs & IFF_UP)
594 continue;
595
596 dev_change_flags(vlandev, flgs | IFF_UP);
597 }
598 break;
122952fc 599
1da177e4
LT
600 case NETDEV_UNREGISTER:
601 /* Delete all VLANs for this dev. */
602 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
603 int ret;
604
5c15bdec 605 vlandev = vlan_group_get_device(grp, i);
1da177e4
LT
606 if (!vlandev)
607 continue;
608
609 ret = unregister_vlan_dev(dev,
610 VLAN_DEV_INFO(vlandev)->vlan_id);
611
612 unregister_netdevice(vlandev);
613
614 /* Group was destroyed? */
615 if (ret == 1)
616 break;
617 }
618 break;
3ff50b79 619 }
1da177e4
LT
620
621out:
622 return NOTIFY_DONE;
623}
624
625/*
626 * VLAN IOCTL handler.
627 * o execute requested action or pass command to the device driver
628 * arg is really a struct vlan_ioctl_args __user *.
629 */
881d966b 630static int vlan_ioctl_handler(struct net *net, void __user *arg)
1da177e4 631{
c17d8874 632 int err;
1da177e4
LT
633 unsigned short vid = 0;
634 struct vlan_ioctl_args args;
c17d8874 635 struct net_device *dev = NULL;
1da177e4
LT
636
637 if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args)))
638 return -EFAULT;
639
640 /* Null terminate this sucker, just in case. */
641 args.device1[23] = 0;
642 args.u.device2[23] = 0;
643
644#ifdef VLAN_DEBUG
645 printk(VLAN_DBG "%s: args.cmd: %x\n", __FUNCTION__, args.cmd);
646#endif
647
c17d8874
PM
648 rtnl_lock();
649
1da177e4
LT
650 switch (args.cmd) {
651 case SET_VLAN_INGRESS_PRIORITY_CMD:
c17d8874
PM
652 case SET_VLAN_EGRESS_PRIORITY_CMD:
653 case SET_VLAN_FLAG_CMD:
654 case ADD_VLAN_CMD:
655 case DEL_VLAN_CMD:
656 case GET_VLAN_REALDEV_NAME_CMD:
657 case GET_VLAN_VID_CMD:
658 err = -ENODEV;
881d966b 659 dev = __dev_get_by_name(&init_net, args.device1);
c17d8874
PM
660 if (!dev)
661 goto out;
662
663 err = -EINVAL;
664 if (args.cmd != ADD_VLAN_CMD &&
665 !(dev->priv_flags & IFF_802_1Q_VLAN))
666 goto out;
667 }
668
669 switch (args.cmd) {
670 case SET_VLAN_INGRESS_PRIORITY_CMD:
671 err = -EPERM;
1da177e4 672 if (!capable(CAP_NET_ADMIN))
c17d8874
PM
673 break;
674 vlan_dev_set_ingress_priority(dev,
675 args.u.skb_priority,
676 args.vlan_qos);
fffe470a 677 err = 0;
1da177e4
LT
678 break;
679
680 case SET_VLAN_EGRESS_PRIORITY_CMD:
c17d8874 681 err = -EPERM;
1da177e4 682 if (!capable(CAP_NET_ADMIN))
c17d8874
PM
683 break;
684 err = vlan_dev_set_egress_priority(dev,
1da177e4
LT
685 args.u.skb_priority,
686 args.vlan_qos);
687 break;
688
689 case SET_VLAN_FLAG_CMD:
c17d8874 690 err = -EPERM;
1da177e4 691 if (!capable(CAP_NET_ADMIN))
c17d8874
PM
692 break;
693 err = vlan_dev_set_vlan_flag(dev,
1da177e4
LT
694 args.u.flag,
695 args.vlan_qos);
696 break;
697
698 case SET_VLAN_NAME_TYPE_CMD:
c17d8874 699 err = -EPERM;
1da177e4 700 if (!capable(CAP_NET_ADMIN))
e35de026 701 break;
c17d8874
PM
702 if ((args.u.name_type >= 0) &&
703 (args.u.name_type < VLAN_NAME_TYPE_HIGHEST)) {
1da177e4
LT
704 vlan_name_type = args.u.name_type;
705 err = 0;
706 } else {
707 err = -EINVAL;
708 }
709 break;
710
711 case ADD_VLAN_CMD:
c17d8874 712 err = -EPERM;
1da177e4 713 if (!capable(CAP_NET_ADMIN))
c17d8874 714 break;
2ae0bf69 715 err = register_vlan_device(dev, args.u.VID);
1da177e4
LT
716 break;
717
718 case DEL_VLAN_CMD:
c17d8874 719 err = -EPERM;
1da177e4 720 if (!capable(CAP_NET_ADMIN))
c17d8874
PM
721 break;
722 err = unregister_vlan_device(dev);
1da177e4
LT
723 break;
724
725 case GET_VLAN_INGRESS_PRIORITY_CMD:
726 /* TODO: Implement
727 err = vlan_dev_get_ingress_priority(args);
728 if (copy_to_user((void*)arg, &args,
122952fc
YH
729 sizeof(struct vlan_ioctl_args))) {
730 err = -EFAULT;
1da177e4
LT
731 }
732 */
733 err = -EINVAL;
734 break;
735 case GET_VLAN_EGRESS_PRIORITY_CMD:
736 /* TODO: Implement
737 err = vlan_dev_get_egress_priority(args.device1, &(args.args);
738 if (copy_to_user((void*)arg, &args,
122952fc
YH
739 sizeof(struct vlan_ioctl_args))) {
740 err = -EFAULT;
1da177e4
LT
741 }
742 */
743 err = -EINVAL;
744 break;
745 case GET_VLAN_REALDEV_NAME_CMD:
3f5f4346 746 err = 0;
c17d8874 747 vlan_dev_get_realdev_name(dev, args.u.device2);
1da177e4
LT
748 if (copy_to_user(arg, &args,
749 sizeof(struct vlan_ioctl_args))) {
750 err = -EFAULT;
751 }
752 break;
753
754 case GET_VLAN_VID_CMD:
3f5f4346 755 err = 0;
c17d8874 756 vlan_dev_get_vid(dev, &vid);
1da177e4
LT
757 args.u.VID = vid;
758 if (copy_to_user(arg, &args,
759 sizeof(struct vlan_ioctl_args))) {
122952fc 760 err = -EFAULT;
1da177e4
LT
761 }
762 break;
763
764 default:
765 /* pass on to underlying device instead?? */
766 printk(VLAN_DBG "%s: Unknown VLAN CMD: %x \n",
767 __FUNCTION__, args.cmd);
c17d8874
PM
768 err = -EINVAL;
769 break;
3ff50b79 770 }
7eb1b3d3 771out:
c17d8874 772 rtnl_unlock();
1da177e4
LT
773 return err;
774}
775
776MODULE_LICENSE("GPL");
777MODULE_VERSION(DRV_VERSION);