initial commit with v2.6.9
[linux-2.6.9-moxart.git] / net / 8021q / vlan.c
blob3ee1dcd35e525b8c59f685259eee4b8ea4200e14
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
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>
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/module.h>
23 #include <linux/netdevice.h>
24 #include <linux/skbuff.h>
25 #include <net/datalink.h>
26 #include <linux/mm.h>
27 #include <linux/in.h>
28 #include <linux/init.h>
29 #include <net/p8022.h>
30 #include <net/arp.h>
31 #include <linux/rtnetlink.h>
32 #include <linux/notifier.h>
34 #include <linux/if_vlan.h>
35 #include "vlan.h"
36 #include "vlanproc.h"
38 /* Global VLAN variables */
40 /* Our listing of VLAN group(s) */
41 struct hlist_head vlan_group_hash[VLAN_GRP_HASH_SIZE];
42 #define vlan_grp_hashfn(IDX) ((((IDX) >> VLAN_GRP_HASH_SHIFT) ^ (IDX)) & VLAN_GRP_HASH_MASK)
44 static char vlan_fullname[] = "802.1Q VLAN Support";
45 static unsigned int vlan_version = 1;
46 static unsigned int vlan_release = 8;
47 static char vlan_copyright[] = "Ben Greear <greearb@candelatech.com>";
48 static char vlan_buggyright[] = "David S. Miller <davem@redhat.com>";
50 static int vlan_device_event(struct notifier_block *, unsigned long, void *);
51 static int vlan_ioctl_handler(void __user *);
52 static int unregister_vlan_dev(struct net_device *, unsigned short );
54 struct notifier_block vlan_notifier_block = {
55 .notifier_call = vlan_device_event,
58 /* These may be changed at run-time through IOCTLs */
60 /* Determines interface naming scheme. */
61 unsigned short vlan_name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;
63 /* DO reorder the header by default */
64 unsigned short vlan_default_dev_flags = 1;
66 static struct packet_type vlan_packet_type = {
67 .type = __constant_htons(ETH_P_8021Q),
68 .func = vlan_skb_recv, /* VLAN receive method */
71 /* Bits of netdev state that are propogated from real device to virtual */
72 #define VLAN_LINK_STATE_MASK \
73 ((1<<__LINK_STATE_PRESENT)|(1<<__LINK_STATE_NOCARRIER))
75 /* End of global variables definitions. */
78 * Function vlan_proto_init (pro)
80 * Initialize VLAN protocol layer,
83 static int __init vlan_proto_init(void)
85 int err;
87 printk(VLAN_INF "%s v%u.%u %s\n",
88 vlan_fullname, vlan_version, vlan_release, vlan_copyright);
89 printk(VLAN_INF "All bugs added by %s\n",
90 vlan_buggyright);
92 /* proc file system initialization */
93 err = vlan_proc_init();
94 if (err < 0) {
95 printk(KERN_ERR
96 "%s %s: can't create entry in proc filesystem!\n",
97 __FUNCTION__, VLAN_NAME);
98 return 1;
101 dev_add_pack(&vlan_packet_type);
103 /* Register us to receive netdevice events */
104 register_netdevice_notifier(&vlan_notifier_block);
106 vlan_ioctl_set(vlan_ioctl_handler);
108 return 0;
111 /* Cleanup all vlan devices
112 * Note: devices that have been registered that but not
113 * brought up will exist but have no module ref count.
115 static void __exit vlan_cleanup_devices(void)
117 struct net_device *dev, *nxt;
119 rtnl_lock();
120 for (dev = dev_base; dev; dev = nxt) {
121 nxt = dev->next;
122 if (dev->priv_flags & IFF_802_1Q_VLAN) {
123 unregister_vlan_dev(VLAN_DEV_INFO(dev)->real_dev,
124 VLAN_DEV_INFO(dev)->vlan_id);
126 unregister_netdevice(dev);
129 rtnl_unlock();
133 * Module 'remove' entry point.
134 * o delete /proc/net/router directory and static entries.
136 static void __exit vlan_cleanup_module(void)
138 int i;
140 vlan_ioctl_set(NULL);
142 /* Un-register us from receiving netdevice events */
143 unregister_netdevice_notifier(&vlan_notifier_block);
145 dev_remove_pack(&vlan_packet_type);
146 vlan_cleanup_devices();
148 /* This table must be empty if there are no module
149 * references left.
151 for (i = 0; i < VLAN_GRP_HASH_SIZE; i++) {
152 BUG_ON(!hlist_empty(&vlan_group_hash[i]));
154 vlan_proc_cleanup();
156 synchronize_net();
159 module_init(vlan_proto_init);
160 module_exit(vlan_cleanup_module);
162 /* Must be invoked with RCU read lock (no preempt) */
163 static struct vlan_group *__vlan_find_group(int real_dev_ifindex)
165 struct vlan_group *grp;
166 struct hlist_node *n;
167 int hash = vlan_grp_hashfn(real_dev_ifindex);
169 hlist_for_each_entry_rcu(grp, n, &vlan_group_hash[hash], hlist) {
170 if (grp->real_dev_ifindex == real_dev_ifindex)
171 return grp;
174 return NULL;
177 /* Find the protocol handler. Assumes VID < VLAN_VID_MASK.
179 * Must be invoked with RCU read lock (no preempt)
181 struct net_device *__find_vlan_dev(struct net_device *real_dev,
182 unsigned short VID)
184 struct vlan_group *grp = __vlan_find_group(real_dev->ifindex);
186 if (grp)
187 return grp->vlan_devices[VID];
189 return NULL;
192 static void vlan_rcu_free(struct rcu_head *rcu)
194 kfree(container_of(rcu, struct vlan_group, rcu));
198 /* This returns 0 if everything went fine.
199 * It will return 1 if the group was killed as a result.
200 * A negative return indicates failure.
202 * The RTNL lock must be held.
204 static int unregister_vlan_dev(struct net_device *real_dev,
205 unsigned short vlan_id)
207 struct net_device *dev = NULL;
208 int real_dev_ifindex = real_dev->ifindex;
209 struct vlan_group *grp;
210 int i, ret;
212 #ifdef VLAN_DEBUG
213 printk(VLAN_DBG "%s: VID: %i\n", __FUNCTION__, vlan_id);
214 #endif
216 /* sanity check */
217 if (vlan_id >= VLAN_VID_MASK)
218 return -EINVAL;
220 ASSERT_RTNL();
221 grp = __vlan_find_group(real_dev_ifindex);
223 ret = 0;
225 if (grp) {
226 dev = grp->vlan_devices[vlan_id];
227 if (dev) {
228 /* Remove proc entry */
229 vlan_proc_rem_dev(dev);
231 /* Take it out of our own structures, but be sure to
232 * interlock with HW accelerating devices or SW vlan
233 * input packet processing.
235 if (real_dev->features &
236 (NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER)) {
237 real_dev->vlan_rx_kill_vid(real_dev, vlan_id);
240 grp->vlan_devices[vlan_id] = NULL;
241 synchronize_net();
244 /* Caller unregisters (and if necessary, puts)
245 * VLAN device, but we get rid of the reference to
246 * real_dev here.
248 dev_put(real_dev);
250 /* If the group is now empty, kill off the
251 * group.
253 for (i = 0; i < VLAN_VID_MASK; i++)
254 if (grp->vlan_devices[i])
255 break;
257 if (i == VLAN_VID_MASK) {
258 if (real_dev->features & NETIF_F_HW_VLAN_RX)
259 real_dev->vlan_rx_register(real_dev, NULL);
261 hlist_del_rcu(&grp->hlist);
263 /* Free the group, after all cpu's are done. */
264 call_rcu(&grp->rcu, vlan_rcu_free);
266 grp = NULL;
267 ret = 1;
272 return ret;
275 static int unregister_vlan_device(const char *vlan_IF_name)
277 struct net_device *dev = NULL;
278 int ret;
281 dev = dev_get_by_name(vlan_IF_name);
282 ret = -EINVAL;
283 if (dev) {
284 if (dev->priv_flags & IFF_802_1Q_VLAN) {
285 rtnl_lock();
287 ret = unregister_vlan_dev(VLAN_DEV_INFO(dev)->real_dev,
288 VLAN_DEV_INFO(dev)->vlan_id);
290 dev_put(dev);
291 unregister_netdevice(dev);
293 rtnl_unlock();
295 if (ret == 1)
296 ret = 0;
297 } else {
298 printk(VLAN_ERR
299 "%s: ERROR: Tried to remove a non-vlan device "
300 "with VLAN code, name: %s priv_flags: %hX\n",
301 __FUNCTION__, dev->name, dev->priv_flags);
302 dev_put(dev);
303 ret = -EPERM;
305 } else {
306 #ifdef VLAN_DEBUG
307 printk(VLAN_DBG "%s: WARNING: Could not find dev.\n", __FUNCTION__);
308 #endif
309 ret = -EINVAL;
312 return ret;
315 static void vlan_setup(struct net_device *new_dev)
317 SET_MODULE_OWNER(new_dev);
319 /* new_dev->ifindex = 0; it will be set when added to
320 * the global list.
321 * iflink is set as well.
323 new_dev->get_stats = vlan_dev_get_stats;
325 /* Make this thing known as a VLAN device */
326 new_dev->priv_flags |= IFF_802_1Q_VLAN;
328 /* Set us up to have no queue, as the underlying Hardware device
329 * can do all the queueing we could want.
331 new_dev->tx_queue_len = 0;
333 /* set up method calls */
334 new_dev->change_mtu = vlan_dev_change_mtu;
335 new_dev->open = vlan_dev_open;
336 new_dev->stop = vlan_dev_stop;
337 new_dev->set_mac_address = vlan_dev_set_mac_address;
338 new_dev->set_multicast_list = vlan_dev_set_multicast_list;
339 new_dev->destructor = free_netdev;
340 new_dev->do_ioctl = vlan_dev_ioctl;
343 /* Attach a VLAN device to a mac address (ie Ethernet Card).
344 * Returns the device that was created, or NULL if there was
345 * an error of some kind.
347 static struct net_device *register_vlan_device(const char *eth_IF_name,
348 unsigned short VLAN_ID)
350 struct vlan_group *grp;
351 struct net_device *new_dev;
352 struct net_device *real_dev; /* the ethernet device */
353 char name[IFNAMSIZ];
355 #ifdef VLAN_DEBUG
356 printk(VLAN_DBG "%s: if_name -:%s:- vid: %i\n",
357 __FUNCTION__, eth_IF_name, VLAN_ID);
358 #endif
360 if (VLAN_ID >= VLAN_VID_MASK)
361 goto out_ret_null;
363 /* find the device relating to eth_IF_name. */
364 real_dev = dev_get_by_name(eth_IF_name);
365 if (!real_dev)
366 goto out_ret_null;
368 if (real_dev->features & NETIF_F_VLAN_CHALLENGED) {
369 printk(VLAN_DBG "%s: VLANs not supported on %s.\n",
370 __FUNCTION__, real_dev->name);
371 goto out_put_dev;
374 if ((real_dev->features & NETIF_F_HW_VLAN_RX) &&
375 (real_dev->vlan_rx_register == NULL ||
376 real_dev->vlan_rx_kill_vid == NULL)) {
377 printk(VLAN_DBG "%s: Device %s has buggy VLAN hw accel.\n",
378 __FUNCTION__, real_dev->name);
379 goto out_put_dev;
382 if ((real_dev->features & NETIF_F_HW_VLAN_FILTER) &&
383 (real_dev->vlan_rx_add_vid == NULL ||
384 real_dev->vlan_rx_kill_vid == NULL)) {
385 printk(VLAN_DBG "%s: Device %s has buggy VLAN hw accel.\n",
386 __FUNCTION__, real_dev->name);
387 goto out_put_dev;
390 /* From this point on, all the data structures must remain
391 * consistent.
393 rtnl_lock();
395 /* The real device must be up and operating in order to
396 * assosciate a VLAN device with it.
398 if (!(real_dev->flags & IFF_UP))
399 goto out_unlock;
401 if (__find_vlan_dev(real_dev, VLAN_ID) != NULL) {
402 /* was already registered. */
403 printk(VLAN_DBG "%s: ALREADY had VLAN registered\n", __FUNCTION__);
404 goto out_unlock;
407 /* Gotta set up the fields for the device. */
408 #ifdef VLAN_DEBUG
409 printk(VLAN_DBG "About to allocate name, vlan_name_type: %i\n",
410 vlan_name_type);
411 #endif
412 switch (vlan_name_type) {
413 case VLAN_NAME_TYPE_RAW_PLUS_VID:
414 /* name will look like: eth1.0005 */
415 snprintf(name, IFNAMSIZ, "%s.%.4i", real_dev->name, VLAN_ID);
416 break;
417 case VLAN_NAME_TYPE_PLUS_VID_NO_PAD:
418 /* Put our vlan.VID in the name.
419 * Name will look like: vlan5
421 snprintf(name, IFNAMSIZ, "vlan%i", VLAN_ID);
422 break;
423 case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD:
424 /* Put our vlan.VID in the name.
425 * Name will look like: eth0.5
427 snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, VLAN_ID);
428 break;
429 case VLAN_NAME_TYPE_PLUS_VID:
430 /* Put our vlan.VID in the name.
431 * Name will look like: vlan0005
433 default:
434 snprintf(name, IFNAMSIZ, "vlan%.4i", VLAN_ID);
437 new_dev = alloc_netdev(sizeof(struct vlan_dev_info), name,
438 vlan_setup);
439 if (new_dev == NULL)
440 goto out_unlock;
442 #ifdef VLAN_DEBUG
443 printk(VLAN_DBG "Allocated new name -:%s:-\n", new_dev->name);
444 #endif
445 /* IFF_BROADCAST|IFF_MULTICAST; ??? */
446 new_dev->flags = real_dev->flags;
447 new_dev->flags &= ~IFF_UP;
449 new_dev->state = real_dev->state & VLAN_LINK_STATE_MASK;
451 /* need 4 bytes for extra VLAN header info,
452 * hope the underlying device can handle it.
454 new_dev->mtu = real_dev->mtu;
456 /* TODO: maybe just assign it to be ETHERNET? */
457 new_dev->type = real_dev->type;
459 new_dev->hard_header_len = real_dev->hard_header_len;
460 if (!(real_dev->features & NETIF_F_HW_VLAN_TX)) {
461 /* Regular ethernet + 4 bytes (18 total). */
462 new_dev->hard_header_len += VLAN_HLEN;
465 VLAN_MEM_DBG("new_dev->priv malloc, addr: %p size: %i\n",
466 new_dev->priv,
467 sizeof(struct vlan_dev_info));
469 memcpy(new_dev->broadcast, real_dev->broadcast, real_dev->addr_len);
470 memcpy(new_dev->dev_addr, real_dev->dev_addr, real_dev->addr_len);
471 new_dev->addr_len = real_dev->addr_len;
473 if (real_dev->features & NETIF_F_HW_VLAN_TX) {
474 new_dev->hard_header = real_dev->hard_header;
475 new_dev->hard_start_xmit = vlan_dev_hwaccel_hard_start_xmit;
476 new_dev->rebuild_header = real_dev->rebuild_header;
477 } else {
478 new_dev->hard_header = vlan_dev_hard_header;
479 new_dev->hard_start_xmit = vlan_dev_hard_start_xmit;
480 new_dev->rebuild_header = vlan_dev_rebuild_header;
482 new_dev->hard_header_parse = real_dev->hard_header_parse;
484 VLAN_DEV_INFO(new_dev)->vlan_id = VLAN_ID; /* 1 through VLAN_VID_MASK */
485 VLAN_DEV_INFO(new_dev)->real_dev = real_dev;
486 VLAN_DEV_INFO(new_dev)->dent = NULL;
487 VLAN_DEV_INFO(new_dev)->flags = vlan_default_dev_flags;
489 #ifdef VLAN_DEBUG
490 printk(VLAN_DBG "About to go find the group for idx: %i\n",
491 real_dev->ifindex);
492 #endif
494 if (register_netdevice(new_dev))
495 goto out_free_newdev;
497 /* So, got the sucker initialized, now lets place
498 * it into our local structure.
500 grp = __vlan_find_group(real_dev->ifindex);
502 /* Note, we are running under the RTNL semaphore
503 * so it cannot "appear" on us.
505 if (!grp) { /* need to add a new group */
506 grp = kmalloc(sizeof(struct vlan_group), GFP_KERNEL);
507 if (!grp)
508 goto out_free_unregister;
510 /* printk(KERN_ALERT "VLAN REGISTER: Allocated new group.\n"); */
511 memset(grp, 0, sizeof(struct vlan_group));
512 grp->real_dev_ifindex = real_dev->ifindex;
514 hlist_add_head_rcu(&grp->hlist,
515 &vlan_group_hash[vlan_grp_hashfn(real_dev->ifindex)]);
517 if (real_dev->features & NETIF_F_HW_VLAN_RX)
518 real_dev->vlan_rx_register(real_dev, grp);
521 grp->vlan_devices[VLAN_ID] = new_dev;
523 if (vlan_proc_add_dev(new_dev)<0)/* create it's proc entry */
524 printk(KERN_WARNING "VLAN: failed to add proc entry for %s\n",
525 new_dev->name);
527 if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
528 real_dev->vlan_rx_add_vid(real_dev, VLAN_ID);
530 rtnl_unlock();
533 #ifdef VLAN_DEBUG
534 printk(VLAN_DBG "Allocated new device successfully, returning.\n");
535 #endif
536 return new_dev;
538 out_free_unregister:
539 unregister_netdev(new_dev);
540 goto out_unlock;
542 out_free_newdev:
543 free_netdev(new_dev);
545 out_unlock:
546 rtnl_unlock();
548 out_put_dev:
549 dev_put(real_dev);
551 out_ret_null:
552 return NULL;
555 static int vlan_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
557 struct net_device *dev = ptr;
558 struct vlan_group *grp = __vlan_find_group(dev->ifindex);
559 int i, flgs;
560 struct net_device *vlandev;
562 if (!grp)
563 goto out;
565 /* It is OK that we do not hold the group lock right now,
566 * as we run under the RTNL lock.
569 switch (event) {
570 case NETDEV_CHANGE:
571 /* Propogate real device state to vlan devices */
572 flgs = dev->state & VLAN_LINK_STATE_MASK;
573 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
574 vlandev = grp->vlan_devices[i];
575 if (!vlandev)
576 continue;
578 if ((vlandev->state & VLAN_LINK_STATE_MASK) != flgs) {
579 vlandev->state = (vlandev->state &~ VLAN_LINK_STATE_MASK)
580 | flgs;
581 netdev_state_change(vlandev);
584 break;
586 case NETDEV_DOWN:
587 /* Put all VLANs for this dev in the down state too. */
588 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
589 vlandev = grp->vlan_devices[i];
590 if (!vlandev)
591 continue;
593 flgs = vlandev->flags;
594 if (!(flgs & IFF_UP))
595 continue;
597 dev_change_flags(vlandev, flgs & ~IFF_UP);
599 break;
601 case NETDEV_UP:
602 /* Put all VLANs for this dev in the up state too. */
603 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
604 vlandev = grp->vlan_devices[i];
605 if (!vlandev)
606 continue;
608 flgs = vlandev->flags;
609 if (flgs & IFF_UP)
610 continue;
612 dev_change_flags(vlandev, flgs | IFF_UP);
614 break;
616 case NETDEV_UNREGISTER:
617 /* Delete all VLANs for this dev. */
618 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
619 int ret;
621 vlandev = grp->vlan_devices[i];
622 if (!vlandev)
623 continue;
625 ret = unregister_vlan_dev(dev,
626 VLAN_DEV_INFO(vlandev)->vlan_id);
628 unregister_netdevice(vlandev);
630 /* Group was destroyed? */
631 if (ret == 1)
632 break;
634 break;
637 out:
638 return NOTIFY_DONE;
642 * VLAN IOCTL handler.
643 * o execute requested action or pass command to the device driver
644 * arg is really a struct vlan_ioctl_args __user *.
646 static int vlan_ioctl_handler(void __user *arg)
648 int err = 0;
649 struct vlan_ioctl_args args;
651 /* everything here needs root permissions, except aguably the
652 * hack ioctls for sending packets. However, I know _I_ don't
653 * want users running that on my network! --BLG
655 if (!capable(CAP_NET_ADMIN))
656 return -EPERM;
658 if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args)))
659 return -EFAULT;
661 /* Null terminate this sucker, just in case. */
662 args.device1[23] = 0;
663 args.u.device2[23] = 0;
665 #ifdef VLAN_DEBUG
666 printk(VLAN_DBG "%s: args.cmd: %x\n", __FUNCTION__, args.cmd);
667 #endif
669 switch (args.cmd) {
670 case SET_VLAN_INGRESS_PRIORITY_CMD:
671 err = vlan_dev_set_ingress_priority(args.device1,
672 args.u.skb_priority,
673 args.vlan_qos);
674 break;
676 case SET_VLAN_EGRESS_PRIORITY_CMD:
677 err = vlan_dev_set_egress_priority(args.device1,
678 args.u.skb_priority,
679 args.vlan_qos);
680 break;
682 case SET_VLAN_FLAG_CMD:
683 err = vlan_dev_set_vlan_flag(args.device1,
684 args.u.flag,
685 args.vlan_qos);
686 break;
688 case SET_VLAN_NAME_TYPE_CMD:
689 if ((args.u.name_type >= 0) &&
690 (args.u.name_type < VLAN_NAME_TYPE_HIGHEST)) {
691 vlan_name_type = args.u.name_type;
692 err = 0;
693 } else {
694 err = -EINVAL;
696 break;
698 /* TODO: Figure out how to pass info back...
699 case GET_VLAN_INGRESS_PRIORITY_IOCTL:
700 err = vlan_dev_get_ingress_priority(args);
701 break;
703 case GET_VLAN_EGRESS_PRIORITY_IOCTL:
704 err = vlan_dev_get_egress_priority(args);
705 break;
708 case ADD_VLAN_CMD:
709 /* we have been given the name of the Ethernet Device we want to
710 * talk to: args.dev1 We also have the
711 * VLAN ID: args.u.VID
713 if (register_vlan_device(args.device1, args.u.VID)) {
714 err = 0;
715 } else {
716 err = -EINVAL;
718 break;
720 case DEL_VLAN_CMD:
721 /* Here, the args.dev1 is the actual VLAN we want
722 * to get rid of.
724 err = unregister_vlan_device(args.device1);
725 break;
727 default:
728 /* pass on to underlying device instead?? */
729 printk(VLAN_DBG "%s: Unknown VLAN CMD: %x \n",
730 __FUNCTION__, args.cmd);
731 return -EINVAL;
734 return err;
737 MODULE_LICENSE("GPL");