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