[NET]: Nuke SET_MODULE_OWNER macro.
[deliverable/linux.git] / 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 #include <net/net_namespace.h>
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) */
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)
47
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>";
52
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 );
56
57 static 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. */
64 unsigned short vlan_name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;
65
66 static struct packet_type vlan_packet_type = {
67 .type = __constant_htons(ETH_P_8021Q),
68 .func = vlan_skb_recv, /* VLAN receive method */
69 };
70
71 /* End of global variables definitions. */
72
73 /*
74 * Function vlan_proto_init (pro)
75 *
76 * Initialize VLAN protocol layer,
77 *
78 */
79 static 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) {
91 printk(KERN_ERR
92 "%s %s: can't create entry in proc filesystem!\n",
93 __FUNCTION__, VLAN_NAME);
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);
101 if (err < 0)
102 goto err1;
103
104 err = vlan_netlink_init();
105 if (err < 0)
106 goto err2;
107
108 vlan_ioctl_set(vlan_ioctl_handler);
109 return 0;
110
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;
117 }
118
119 /*
120 * Module 'remove' entry point.
121 * o delete /proc/net/router directory and static entries.
122 */
123 static void __exit vlan_cleanup_module(void)
124 {
125 int i;
126
127 vlan_netlink_fini();
128 vlan_ioctl_set(NULL);
129
130 /* Un-register us from receiving netdevice events */
131 unregister_netdevice_notifier(&vlan_notifier_block);
132
133 dev_remove_pack(&vlan_packet_type);
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
146 module_init(vlan_proto_init);
147 module_exit(vlan_cleanup_module);
148
149 /* Must be invoked with RCU read lock (no preempt) */
150 static 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 */
168 struct 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)
174 return vlan_group_get_device(grp, VID);
175
176 return NULL;
177 }
178
179 static 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
188 static 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
211 err:
212 vlan_group_free(grp);
213 return NULL;
214 }
215
216 static void vlan_rcu_free(struct rcu_head *rcu)
217 {
218 vlan_group_free(container_of(rcu, struct vlan_group, rcu));
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 */
228 static 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) {
250 dev = vlan_group_get_device(grp, vlan_id);
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 */
259 if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
260 real_dev->vlan_rx_kill_vid(real_dev, vlan_id);
261
262 vlan_group_set_device(grp, vlan_id, NULL);
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++)
276 if (vlan_group_get_device(grp, i))
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
294 return ret;
295 }
296
297 int unregister_vlan_device(struct net_device *dev)
298 {
299 int ret;
300
301 ret = unregister_vlan_dev(VLAN_DEV_INFO(dev)->real_dev,
302 VLAN_DEV_INFO(dev)->vlan_id);
303 unregister_netdevice(dev);
304
305 if (ret == 1)
306 ret = 0;
307 return ret;
308 }
309
310 /*
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.
314 */
315 static struct lock_class_key vlan_netdev_xmit_lock_key;
316
317 static int vlan_dev_init(struct net_device *dev)
318 {
319 struct net_device *real_dev = VLAN_DEV_INFO(dev)->real_dev;
320
321 /* IFF_BROADCAST|IFF_MULTICAST; ??? */
322 dev->flags = real_dev->flags & ~IFF_UP;
323 dev->iflink = real_dev->ifindex;
324 dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
325 (1<<__LINK_STATE_DORMANT))) |
326 (1<<__LINK_STATE_PRESENT);
327
328 if (is_zero_ether_addr(dev->dev_addr))
329 memcpy(dev->dev_addr, real_dev->dev_addr, dev->addr_len);
330 if (is_zero_ether_addr(dev->broadcast))
331 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
332
333 if (real_dev->features & NETIF_F_HW_VLAN_TX) {
334 dev->hard_header = real_dev->hard_header;
335 dev->hard_header_len = real_dev->hard_header_len;
336 dev->hard_start_xmit = vlan_dev_hwaccel_hard_start_xmit;
337 dev->rebuild_header = real_dev->rebuild_header;
338 } else {
339 dev->hard_header = vlan_dev_hard_header;
340 dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
341 dev->hard_start_xmit = vlan_dev_hard_start_xmit;
342 dev->rebuild_header = vlan_dev_rebuild_header;
343 }
344 dev->hard_header_parse = real_dev->hard_header_parse;
345 dev->hard_header_cache = NULL;
346
347 lockdep_set_class(&dev->_xmit_lock, &vlan_netdev_xmit_lock_key);
348 return 0;
349 }
350
351 void vlan_setup(struct net_device *new_dev)
352 {
353 ether_setup(new_dev);
354
355 /* new_dev->ifindex = 0; it will be set when added to
356 * the global list.
357 * iflink is set as well.
358 */
359 new_dev->get_stats = vlan_dev_get_stats;
360
361 /* Make this thing known as a VLAN device */
362 new_dev->priv_flags |= IFF_802_1Q_VLAN;
363
364 /* Set us up to have no queue, as the underlying Hardware device
365 * can do all the queueing we could want.
366 */
367 new_dev->tx_queue_len = 0;
368
369 /* set up method calls */
370 new_dev->change_mtu = vlan_dev_change_mtu;
371 new_dev->init = vlan_dev_init;
372 new_dev->open = vlan_dev_open;
373 new_dev->stop = vlan_dev_stop;
374 new_dev->set_multicast_list = vlan_dev_set_multicast_list;
375 new_dev->change_rx_flags = vlan_change_rx_flags;
376 new_dev->destructor = free_netdev;
377 new_dev->do_ioctl = vlan_dev_ioctl;
378
379 memset(new_dev->broadcast, 0, ETH_ALEN);
380 }
381
382 static void vlan_transfer_operstate(const struct net_device *dev, struct net_device *vlandev)
383 {
384 /* Have to respect userspace enforced dormant state
385 * of real device, also must allow supplicant running
386 * on VLAN device
387 */
388 if (dev->operstate == IF_OPER_DORMANT)
389 netif_dormant_on(vlandev);
390 else
391 netif_dormant_off(vlandev);
392
393 if (netif_carrier_ok(dev)) {
394 if (!netif_carrier_ok(vlandev))
395 netif_carrier_on(vlandev);
396 } else {
397 if (netif_carrier_ok(vlandev))
398 netif_carrier_off(vlandev);
399 }
400 }
401
402 int vlan_check_real_dev(struct net_device *real_dev, unsigned short vlan_id)
403 {
404 if (real_dev->features & NETIF_F_VLAN_CHALLENGED) {
405 printk(VLAN_DBG "%s: VLANs not supported on %s.\n",
406 __FUNCTION__, real_dev->name);
407 return -EOPNOTSUPP;
408 }
409
410 if ((real_dev->features & NETIF_F_HW_VLAN_RX) &&
411 !real_dev->vlan_rx_register) {
412 printk(VLAN_DBG "%s: Device %s has buggy VLAN hw accel.\n",
413 __FUNCTION__, real_dev->name);
414 return -EOPNOTSUPP;
415 }
416
417 if ((real_dev->features & NETIF_F_HW_VLAN_FILTER) &&
418 (!real_dev->vlan_rx_add_vid || !real_dev->vlan_rx_kill_vid)) {
419 printk(VLAN_DBG "%s: Device %s has buggy VLAN hw accel.\n",
420 __FUNCTION__, real_dev->name);
421 return -EOPNOTSUPP;
422 }
423
424 /* The real device must be up and operating in order to
425 * assosciate a VLAN device with it.
426 */
427 if (!(real_dev->flags & IFF_UP))
428 return -ENETDOWN;
429
430 if (__find_vlan_dev(real_dev, vlan_id) != NULL) {
431 /* was already registered. */
432 printk(VLAN_DBG "%s: ALREADY had VLAN registered\n", __FUNCTION__);
433 return -EEXIST;
434 }
435
436 return 0;
437 }
438
439 int register_vlan_dev(struct net_device *dev)
440 {
441 struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev);
442 struct net_device *real_dev = vlan->real_dev;
443 unsigned short vlan_id = vlan->vlan_id;
444 struct vlan_group *grp, *ngrp = NULL;
445 int err;
446
447 grp = __vlan_find_group(real_dev->ifindex);
448 if (!grp) {
449 ngrp = grp = vlan_group_alloc(real_dev->ifindex);
450 if (!grp)
451 return -ENOBUFS;
452 }
453
454 err = register_netdevice(dev);
455 if (err < 0)
456 goto out_free_group;
457
458 /* Account for reference in struct vlan_dev_info */
459 dev_hold(real_dev);
460
461 vlan_transfer_operstate(real_dev, dev);
462 linkwatch_fire_event(dev); /* _MUST_ call rfc2863_policy() */
463
464 /* So, got the sucker initialized, now lets place
465 * it into our local structure.
466 */
467 vlan_group_set_device(grp, vlan_id, dev);
468 if (ngrp && real_dev->features & NETIF_F_HW_VLAN_RX)
469 real_dev->vlan_rx_register(real_dev, ngrp);
470 if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
471 real_dev->vlan_rx_add_vid(real_dev, vlan_id);
472
473 if (vlan_proc_add_dev(dev) < 0)
474 printk(KERN_WARNING "VLAN: failed to add proc entry for %s\n",
475 dev->name);
476 return 0;
477
478 out_free_group:
479 if (ngrp)
480 vlan_group_free(ngrp);
481 return err;
482 }
483
484 /* Attach a VLAN device to a mac address (ie Ethernet Card).
485 * Returns 0 if the device was created or a negative error code otherwise.
486 */
487 static int register_vlan_device(struct net_device *real_dev,
488 unsigned short VLAN_ID)
489 {
490 struct net_device *new_dev;
491 char name[IFNAMSIZ];
492 int err;
493
494 #ifdef VLAN_DEBUG
495 printk(VLAN_DBG "%s: if_name -:%s:- vid: %i\n",
496 __FUNCTION__, eth_IF_name, VLAN_ID);
497 #endif
498
499 if (VLAN_ID >= VLAN_VID_MASK)
500 return -ERANGE;
501
502 err = vlan_check_real_dev(real_dev, VLAN_ID);
503 if (err < 0)
504 return err;
505
506 /* Gotta set up the fields for the device. */
507 #ifdef VLAN_DEBUG
508 printk(VLAN_DBG "About to allocate name, vlan_name_type: %i\n",
509 vlan_name_type);
510 #endif
511 switch (vlan_name_type) {
512 case VLAN_NAME_TYPE_RAW_PLUS_VID:
513 /* name will look like: eth1.0005 */
514 snprintf(name, IFNAMSIZ, "%s.%.4i", real_dev->name, VLAN_ID);
515 break;
516 case VLAN_NAME_TYPE_PLUS_VID_NO_PAD:
517 /* Put our vlan.VID in the name.
518 * Name will look like: vlan5
519 */
520 snprintf(name, IFNAMSIZ, "vlan%i", VLAN_ID);
521 break;
522 case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD:
523 /* Put our vlan.VID in the name.
524 * Name will look like: eth0.5
525 */
526 snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, VLAN_ID);
527 break;
528 case VLAN_NAME_TYPE_PLUS_VID:
529 /* Put our vlan.VID in the name.
530 * Name will look like: vlan0005
531 */
532 default:
533 snprintf(name, IFNAMSIZ, "vlan%.4i", VLAN_ID);
534 }
535
536 new_dev = alloc_netdev(sizeof(struct vlan_dev_info), name,
537 vlan_setup);
538
539 if (new_dev == NULL)
540 return -ENOBUFS;
541
542 /* need 4 bytes for extra VLAN header info,
543 * hope the underlying device can handle it.
544 */
545 new_dev->mtu = real_dev->mtu;
546
547 #ifdef VLAN_DEBUG
548 printk(VLAN_DBG "Allocated new name -:%s:-\n", new_dev->name);
549 VLAN_MEM_DBG("new_dev->priv malloc, addr: %p size: %i\n",
550 new_dev->priv,
551 sizeof(struct vlan_dev_info));
552 #endif
553
554 VLAN_DEV_INFO(new_dev)->vlan_id = VLAN_ID; /* 1 through VLAN_VID_MASK */
555 VLAN_DEV_INFO(new_dev)->real_dev = real_dev;
556 VLAN_DEV_INFO(new_dev)->dent = NULL;
557 VLAN_DEV_INFO(new_dev)->flags = VLAN_FLAG_REORDER_HDR;
558
559 new_dev->rtnl_link_ops = &vlan_link_ops;
560 err = register_vlan_dev(new_dev);
561 if (err < 0)
562 goto out_free_newdev;
563
564 #ifdef VLAN_DEBUG
565 printk(VLAN_DBG "Allocated new device successfully, returning.\n");
566 #endif
567 return 0;
568
569 out_free_newdev:
570 free_netdev(new_dev);
571 return err;
572 }
573
574 static void vlan_sync_address(struct net_device *dev,
575 struct net_device *vlandev)
576 {
577 struct vlan_dev_info *vlan = VLAN_DEV_INFO(vlandev);
578
579 /* May be called without an actual change */
580 if (!compare_ether_addr(vlan->real_dev_addr, dev->dev_addr))
581 return;
582
583 /* vlan address was different from the old address and is equal to
584 * the new address */
585 if (compare_ether_addr(vlandev->dev_addr, vlan->real_dev_addr) &&
586 !compare_ether_addr(vlandev->dev_addr, dev->dev_addr))
587 dev_unicast_delete(dev, vlandev->dev_addr, ETH_ALEN);
588
589 /* vlan address was equal to the old address and is different from
590 * the new address */
591 if (!compare_ether_addr(vlandev->dev_addr, vlan->real_dev_addr) &&
592 compare_ether_addr(vlandev->dev_addr, dev->dev_addr))
593 dev_unicast_add(dev, vlandev->dev_addr, ETH_ALEN);
594
595 memcpy(vlan->real_dev_addr, dev->dev_addr, ETH_ALEN);
596 }
597
598 static int vlan_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
599 {
600 struct net_device *dev = ptr;
601 struct vlan_group *grp = __vlan_find_group(dev->ifindex);
602 int i, flgs;
603 struct net_device *vlandev;
604
605 if (dev->nd_net != &init_net)
606 return NOTIFY_DONE;
607
608 if (!grp)
609 goto out;
610
611 /* It is OK that we do not hold the group lock right now,
612 * as we run under the RTNL lock.
613 */
614
615 switch (event) {
616 case NETDEV_CHANGE:
617 /* Propagate real device state to vlan devices */
618 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
619 vlandev = vlan_group_get_device(grp, i);
620 if (!vlandev)
621 continue;
622
623 vlan_transfer_operstate(dev, vlandev);
624 }
625 break;
626
627 case NETDEV_CHANGEADDR:
628 /* Adjust unicast filters on underlying device */
629 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
630 vlandev = vlan_group_get_device(grp, i);
631 if (!vlandev)
632 continue;
633
634 vlan_sync_address(dev, vlandev);
635 }
636 break;
637
638 case NETDEV_DOWN:
639 /* Put all VLANs for this dev in the down state too. */
640 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
641 vlandev = vlan_group_get_device(grp, i);
642 if (!vlandev)
643 continue;
644
645 flgs = vlandev->flags;
646 if (!(flgs & IFF_UP))
647 continue;
648
649 dev_change_flags(vlandev, flgs & ~IFF_UP);
650 }
651 break;
652
653 case NETDEV_UP:
654 /* Put all VLANs for this dev in the up state too. */
655 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
656 vlandev = vlan_group_get_device(grp, i);
657 if (!vlandev)
658 continue;
659
660 flgs = vlandev->flags;
661 if (flgs & IFF_UP)
662 continue;
663
664 dev_change_flags(vlandev, flgs | IFF_UP);
665 }
666 break;
667
668 case NETDEV_UNREGISTER:
669 /* Delete all VLANs for this dev. */
670 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
671 int ret;
672
673 vlandev = vlan_group_get_device(grp, i);
674 if (!vlandev)
675 continue;
676
677 ret = unregister_vlan_dev(dev,
678 VLAN_DEV_INFO(vlandev)->vlan_id);
679
680 unregister_netdevice(vlandev);
681
682 /* Group was destroyed? */
683 if (ret == 1)
684 break;
685 }
686 break;
687 }
688
689 out:
690 return NOTIFY_DONE;
691 }
692
693 /*
694 * VLAN IOCTL handler.
695 * o execute requested action or pass command to the device driver
696 * arg is really a struct vlan_ioctl_args __user *.
697 */
698 static int vlan_ioctl_handler(struct net *net, void __user *arg)
699 {
700 int err;
701 unsigned short vid = 0;
702 struct vlan_ioctl_args args;
703 struct net_device *dev = NULL;
704
705 if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args)))
706 return -EFAULT;
707
708 /* Null terminate this sucker, just in case. */
709 args.device1[23] = 0;
710 args.u.device2[23] = 0;
711
712 #ifdef VLAN_DEBUG
713 printk(VLAN_DBG "%s: args.cmd: %x\n", __FUNCTION__, args.cmd);
714 #endif
715
716 rtnl_lock();
717
718 switch (args.cmd) {
719 case SET_VLAN_INGRESS_PRIORITY_CMD:
720 case SET_VLAN_EGRESS_PRIORITY_CMD:
721 case SET_VLAN_FLAG_CMD:
722 case ADD_VLAN_CMD:
723 case DEL_VLAN_CMD:
724 case GET_VLAN_REALDEV_NAME_CMD:
725 case GET_VLAN_VID_CMD:
726 err = -ENODEV;
727 dev = __dev_get_by_name(&init_net, args.device1);
728 if (!dev)
729 goto out;
730
731 err = -EINVAL;
732 if (args.cmd != ADD_VLAN_CMD &&
733 !(dev->priv_flags & IFF_802_1Q_VLAN))
734 goto out;
735 }
736
737 switch (args.cmd) {
738 case SET_VLAN_INGRESS_PRIORITY_CMD:
739 err = -EPERM;
740 if (!capable(CAP_NET_ADMIN))
741 break;
742 vlan_dev_set_ingress_priority(dev,
743 args.u.skb_priority,
744 args.vlan_qos);
745 break;
746
747 case SET_VLAN_EGRESS_PRIORITY_CMD:
748 err = -EPERM;
749 if (!capable(CAP_NET_ADMIN))
750 break;
751 err = vlan_dev_set_egress_priority(dev,
752 args.u.skb_priority,
753 args.vlan_qos);
754 break;
755
756 case SET_VLAN_FLAG_CMD:
757 err = -EPERM;
758 if (!capable(CAP_NET_ADMIN))
759 break;
760 err = vlan_dev_set_vlan_flag(dev,
761 args.u.flag,
762 args.vlan_qos);
763 break;
764
765 case SET_VLAN_NAME_TYPE_CMD:
766 err = -EPERM;
767 if (!capable(CAP_NET_ADMIN))
768 return -EPERM;
769 if ((args.u.name_type >= 0) &&
770 (args.u.name_type < VLAN_NAME_TYPE_HIGHEST)) {
771 vlan_name_type = args.u.name_type;
772 err = 0;
773 } else {
774 err = -EINVAL;
775 }
776 break;
777
778 case ADD_VLAN_CMD:
779 err = -EPERM;
780 if (!capable(CAP_NET_ADMIN))
781 break;
782 err = register_vlan_device(dev, args.u.VID);
783 break;
784
785 case DEL_VLAN_CMD:
786 err = -EPERM;
787 if (!capable(CAP_NET_ADMIN))
788 break;
789 err = unregister_vlan_device(dev);
790 break;
791
792 case GET_VLAN_INGRESS_PRIORITY_CMD:
793 /* TODO: Implement
794 err = vlan_dev_get_ingress_priority(args);
795 if (copy_to_user((void*)arg, &args,
796 sizeof(struct vlan_ioctl_args))) {
797 err = -EFAULT;
798 }
799 */
800 err = -EINVAL;
801 break;
802 case GET_VLAN_EGRESS_PRIORITY_CMD:
803 /* TODO: Implement
804 err = vlan_dev_get_egress_priority(args.device1, &(args.args);
805 if (copy_to_user((void*)arg, &args,
806 sizeof(struct vlan_ioctl_args))) {
807 err = -EFAULT;
808 }
809 */
810 err = -EINVAL;
811 break;
812 case GET_VLAN_REALDEV_NAME_CMD:
813 err = 0;
814 vlan_dev_get_realdev_name(dev, args.u.device2);
815 if (copy_to_user(arg, &args,
816 sizeof(struct vlan_ioctl_args))) {
817 err = -EFAULT;
818 }
819 break;
820
821 case GET_VLAN_VID_CMD:
822 err = 0;
823 vlan_dev_get_vid(dev, &vid);
824 args.u.VID = vid;
825 if (copy_to_user(arg, &args,
826 sizeof(struct vlan_ioctl_args))) {
827 err = -EFAULT;
828 }
829 break;
830
831 default:
832 /* pass on to underlying device instead?? */
833 printk(VLAN_DBG "%s: Unknown VLAN CMD: %x \n",
834 __FUNCTION__, args.cmd);
835 err = -EINVAL;
836 break;
837 }
838 out:
839 rtnl_unlock();
840 return err;
841 }
842
843 MODULE_LICENSE("GPL");
844 MODULE_VERSION(DRV_VERSION);
This page took 0.070953 seconds and 6 git commands to generate.