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
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>
28 #include <linux/init.h>
29 #include <net/p8022.h>
31 #include <linux/rtnetlink.h>
32 #include <linux/brlock.h>
33 #include <linux/notifier.h>
35 #include <linux/if_vlan.h>
39 /* Global VLAN variables */
41 /* Our listing of VLAN group(s) */
42 struct vlan_group *vlan_group_hash[VLAN_GRP_HASH_SIZE];
43 spinlock_t vlan_group_lock = SPIN_LOCK_UNLOCKED;
44 #define vlan_grp_hashfn(IDX) ((((IDX) >> VLAN_GRP_HASH_SHIFT) ^ (IDX)) & VLAN_GRP_HASH_MASK)
46 static char vlan_fullname[] = "802.1Q VLAN Support";
47 static unsigned int vlan_version = 1;
48 static unsigned int vlan_release = 8;
49 static char vlan_copyright[] = "Ben Greear <greearb@candelatech.com>";
50 static char vlan_buggyright[] = "David S. Miller <davem@redhat.com>";
52 static int vlan_device_event(struct notifier_block *, unsigned long, void *);
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),
69 func: vlan_skb_recv, /* VLAN receive method */
70 data: (void *)(-1), /* Set here '(void *)1' when this code can SHARE SKBs */
74 /* End of global variables definitions. */
77 * Function vlan_proto_init (pro)
79 * Initialize VLAN protocol layer,
82 static int __init vlan_proto_init(void)
86 printk(VLAN_INF "%s v%u.%u %s\n",
87 vlan_fullname, vlan_version, vlan_release, vlan_copyright);
88 printk(VLAN_INF "All bugs added by %s\n",
91 /* proc file system initialization */
92 err = vlan_proc_init();
95 "%s %s: can't create entry in proc filesystem!\n",
96 __FUNCTION__, VLAN_NAME);
100 dev_add_pack(&vlan_packet_type);
102 /* Register us to receive netdevice events */
103 register_netdevice_notifier(&vlan_notifier_block);
105 vlan_ioctl_hook = vlan_ioctl_handler;
111 * Module 'remove' entry point.
112 * o delete /proc/net/router directory and static entries.
114 static void __exit vlan_cleanup_module(void)
118 /* This table must be empty if there are no module
121 for (i = 0; i < VLAN_GRP_HASH_SIZE; i++) {
122 if (vlan_group_hash[i] != NULL)
126 /* Un-register us from receiving netdevice events */
127 unregister_netdevice_notifier(&vlan_notifier_block);
129 dev_remove_pack(&vlan_packet_type);
131 vlan_ioctl_hook = NULL;
134 module_init(vlan_proto_init);
135 module_exit(vlan_cleanup_module);
137 /* Must be invoked with vlan_group_lock held. */
138 static struct vlan_group *__vlan_find_group(int real_dev_ifindex)
140 struct vlan_group *grp;
142 for (grp = vlan_group_hash[vlan_grp_hashfn(real_dev_ifindex)];
145 if (grp->real_dev_ifindex == real_dev_ifindex)
152 /* Must hold vlan_group_lock. */
153 static void __grp_hash(struct vlan_group *grp)
155 struct vlan_group **head;
157 head = &vlan_group_hash[vlan_grp_hashfn(grp->real_dev_ifindex)];
162 /* Must hold vlan_group_lock. */
163 static void __grp_unhash(struct vlan_group *grp)
165 struct vlan_group *next, **pprev;
167 pprev = &vlan_group_hash[vlan_grp_hashfn(grp->real_dev_ifindex)];
169 while (next != grp) {
176 /* Find the protocol handler. Assumes VID < VLAN_VID_MASK.
178 * Must be invoked with vlan_group_lock held.
180 struct net_device *__find_vlan_dev(struct net_device *real_dev,
183 struct vlan_group *grp = __vlan_find_group(real_dev->ifindex);
186 return grp->vlan_devices[VID];
191 /* This returns 0 if everything went fine.
192 * It will return 1 if the group was killed as a result.
193 * A negative return indicates failure.
195 * The RTNL lock must be held.
197 static int unregister_vlan_dev(struct net_device *real_dev,
198 unsigned short vlan_id)
200 struct net_device *dev = NULL;
201 int real_dev_ifindex = real_dev->ifindex;
202 struct vlan_group *grp;
206 printk(VLAN_DBG "%s: VID: %i\n", __FUNCTION__, vlan_id);
210 if (vlan_id >= VLAN_VID_MASK)
213 spin_lock_bh(&vlan_group_lock);
214 grp = __vlan_find_group(real_dev_ifindex);
215 spin_unlock_bh(&vlan_group_lock);
220 dev = grp->vlan_devices[vlan_id];
222 /* Remove proc entry */
223 vlan_proc_rem_dev(dev);
225 /* Take it out of our own structures, but be sure to
226 * interlock with HW accelerating devices or SW vlan
227 * input packet processing.
229 if (real_dev->features &
230 (NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER)) {
231 real_dev->vlan_rx_kill_vid(real_dev, vlan_id);
234 br_write_lock(BR_NETPROTO_LOCK);
235 grp->vlan_devices[vlan_id] = NULL;
236 br_write_unlock(BR_NETPROTO_LOCK);
239 /* Caller unregisters (and if necessary, puts)
240 * VLAN device, but we get rid of the reference to
245 /* If the group is now empty, kill off the
248 for (i = 0; i < VLAN_VID_MASK; i++)
249 if (grp->vlan_devices[i])
252 if (i == VLAN_VID_MASK) {
253 if (real_dev->features & NETIF_F_HW_VLAN_RX)
254 real_dev->vlan_rx_register(real_dev, NULL);
256 spin_lock_bh(&vlan_group_lock);
258 spin_unlock_bh(&vlan_group_lock);
260 /* Free the group, after we have removed it
276 static int unregister_vlan_device(const char *vlan_IF_name)
278 struct net_device *dev = NULL;
282 dev = dev_get_by_name(vlan_IF_name);
285 if (dev->priv_flags & IFF_802_1Q_VLAN) {
288 ret = unregister_vlan_dev(VLAN_DEV_INFO(dev)->real_dev,
289 VLAN_DEV_INFO(dev)->vlan_id);
292 unregister_netdevice(dev);
300 "%s: ERROR: Tried to remove a non-vlan device "
301 "with VLAN code, name: %s priv_flags: %hX\n",
302 __FUNCTION__, dev->name, dev->priv_flags);
308 printk(VLAN_DBG "%s: WARNING: Could not find dev.\n", __FUNCTION__);
316 /* Attach a VLAN device to a mac address (ie Ethernet Card).
317 * Returns the device that was created, or NULL if there was
318 * an error of some kind.
320 static struct net_device *register_vlan_device(const char *eth_IF_name,
321 unsigned short VLAN_ID)
323 struct vlan_group *grp;
324 struct net_device *new_dev;
325 struct net_device *real_dev; /* the ethernet device */
330 printk(VLAN_DBG "%s: if_name -:%s:- vid: %i\n",
331 __FUNCTION__, eth_IF_name, VLAN_ID);
334 if (VLAN_ID >= VLAN_VID_MASK)
337 /* find the device relating to eth_IF_name. */
338 real_dev = dev_get_by_name(eth_IF_name);
342 if (real_dev->features & NETIF_F_VLAN_CHALLENGED) {
343 printk(VLAN_DBG "%s: VLANs not supported on %s.\n",
344 __FUNCTION__, real_dev->name);
348 if ((real_dev->features & NETIF_F_HW_VLAN_RX) &&
349 (real_dev->vlan_rx_register == NULL ||
350 real_dev->vlan_rx_kill_vid == NULL)) {
351 printk(VLAN_DBG "%s: Device %s has buggy VLAN hw accel.\n",
352 __FUNCTION__, real_dev->name);
356 if ((real_dev->features & NETIF_F_HW_VLAN_FILTER) &&
357 (real_dev->vlan_rx_add_vid == NULL ||
358 real_dev->vlan_rx_kill_vid == NULL)) {
359 printk(VLAN_DBG "%s: Device %s has buggy VLAN hw accel.\n",
360 __FUNCTION__, real_dev->name);
364 /* From this point on, all the data structures must remain
369 /* The real device must be up and operating in order to
370 * assosciate a VLAN device with it.
372 if (!(real_dev->flags & IFF_UP))
375 spin_lock_bh(&vlan_group_lock);
376 r = (__find_vlan_dev(real_dev, VLAN_ID) != NULL);
377 spin_unlock_bh(&vlan_group_lock);
380 /* was already registered. */
381 printk(VLAN_DBG "%s: ALREADY had VLAN registered\n", __FUNCTION__);
385 malloc_size = (sizeof(struct net_device));
386 new_dev = (struct net_device *) kmalloc(malloc_size, GFP_KERNEL);
387 VLAN_MEM_DBG("net_device malloc, addr: %p size: %i\n",
388 new_dev, malloc_size);
393 memset(new_dev, 0, malloc_size);
395 /* Set us up to have no queue, as the underlying Hardware device
396 * can do all the queueing we could want.
398 new_dev->tx_queue_len = 0;
400 /* Gotta set up the fields for the device. */
402 printk(VLAN_DBG "About to allocate name, vlan_name_type: %i\n",
405 switch (vlan_name_type) {
406 case VLAN_NAME_TYPE_RAW_PLUS_VID:
407 /* name will look like: eth1.0005 */
408 sprintf(new_dev->name, "%s.%.4i", real_dev->name, VLAN_ID);
410 case VLAN_NAME_TYPE_PLUS_VID_NO_PAD:
411 /* Put our vlan.VID in the name.
412 * Name will look like: vlan5
414 sprintf(new_dev->name, "vlan%i", VLAN_ID);
416 case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD:
417 /* Put our vlan.VID in the name.
418 * Name will look like: eth0.5
420 sprintf(new_dev->name, "%s.%i", real_dev->name, VLAN_ID);
422 case VLAN_NAME_TYPE_PLUS_VID:
423 /* Put our vlan.VID in the name.
424 * Name will look like: vlan0005
427 sprintf(new_dev->name, "vlan%.4i", VLAN_ID);
431 printk(VLAN_DBG "Allocated new name -:%s:-\n", new_dev->name);
433 /* set up method calls */
434 new_dev->init = vlan_dev_init;
435 new_dev->destructor = vlan_dev_destruct;
436 new_dev->features |= NETIF_F_DYNALLOC ;
438 /* new_dev->ifindex = 0; it will be set when added to
440 * iflink is set as well.
442 new_dev->get_stats = vlan_dev_get_stats;
444 /* IFF_BROADCAST|IFF_MULTICAST; ??? */
445 new_dev->flags = real_dev->flags;
446 new_dev->flags &= ~IFF_UP;
448 /* Make this thing known as a VLAN device */
449 new_dev->priv_flags |= IFF_802_1Q_VLAN;
451 /* need 4 bytes for extra VLAN header info,
452 * hope the underlying device can handle it.
454 new_dev->mtu = real_dev->mtu;
455 new_dev->change_mtu = vlan_dev_change_mtu;
457 /* TODO: maybe just assign it to be ETHERNET? */
458 new_dev->type = real_dev->type;
460 new_dev->hard_header_len = real_dev->hard_header_len;
461 if (!(real_dev->features & NETIF_F_HW_VLAN_TX)) {
462 /* Regular ethernet + 4 bytes (18 total). */
463 new_dev->hard_header_len += VLAN_HLEN;
466 new_dev->priv = kmalloc(sizeof(struct vlan_dev_info),
468 VLAN_MEM_DBG("new_dev->priv malloc, addr: %p size: %i\n",
470 sizeof(struct vlan_dev_info));
472 if (new_dev->priv == NULL)
473 goto out_free_newdev;
475 memset(new_dev->priv, 0, sizeof(struct vlan_dev_info));
477 memcpy(new_dev->broadcast, real_dev->broadcast, real_dev->addr_len);
478 memcpy(new_dev->dev_addr, real_dev->dev_addr, real_dev->addr_len);
479 new_dev->addr_len = real_dev->addr_len;
481 new_dev->open = vlan_dev_open;
482 new_dev->stop = vlan_dev_stop;
484 if (real_dev->features & NETIF_F_HW_VLAN_TX) {
485 new_dev->hard_header = real_dev->hard_header;
486 new_dev->hard_start_xmit = vlan_dev_hwaccel_hard_start_xmit;
487 new_dev->rebuild_header = real_dev->rebuild_header;
489 new_dev->hard_header = vlan_dev_hard_header;
490 new_dev->hard_start_xmit = vlan_dev_hard_start_xmit;
491 new_dev->rebuild_header = vlan_dev_rebuild_header;
493 new_dev->hard_header_parse = real_dev->hard_header_parse;
494 new_dev->set_mac_address = vlan_dev_set_mac_address;
495 new_dev->set_multicast_list = vlan_dev_set_multicast_list;
497 VLAN_DEV_INFO(new_dev)->vlan_id = VLAN_ID; /* 1 through VLAN_VID_MASK */
498 VLAN_DEV_INFO(new_dev)->real_dev = real_dev;
499 VLAN_DEV_INFO(new_dev)->dent = NULL;
500 VLAN_DEV_INFO(new_dev)->flags = vlan_default_dev_flags;
503 printk(VLAN_DBG "About to go find the group for idx: %i\n",
507 /* So, got the sucker initialized, now lets place
508 * it into our local structure.
510 spin_lock_bh(&vlan_group_lock);
511 grp = __vlan_find_group(real_dev->ifindex);
512 spin_unlock_bh(&vlan_group_lock);
514 /* Note, we are running under the RTNL semaphore
515 * so it cannot "appear" on us.
517 if (!grp) { /* need to add a new group */
518 grp = kmalloc(sizeof(struct vlan_group), GFP_KERNEL);
520 goto out_free_newdev_priv;
522 /* printk(KERN_ALERT "VLAN REGISTER: Allocated new group.\n"); */
523 memset(grp, 0, sizeof(struct vlan_group));
524 grp->real_dev_ifindex = real_dev->ifindex;
526 spin_lock_bh(&vlan_group_lock);
528 spin_unlock_bh(&vlan_group_lock);
530 if (real_dev->features & NETIF_F_HW_VLAN_RX)
531 real_dev->vlan_rx_register(real_dev, grp);
534 grp->vlan_devices[VLAN_ID] = new_dev;
536 vlan_proc_add_dev(new_dev); /* create it's proc entry */
538 if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
539 real_dev->vlan_rx_add_vid(real_dev, VLAN_ID);
541 register_netdevice(new_dev);
545 /* NOTE: We have a reference to the real device,
546 * so hold on to the reference.
548 MOD_INC_USE_COUNT; /* Add was a success!! */
550 printk(VLAN_DBG "Allocated new device successfully, returning.\n");
554 out_free_newdev_priv:
555 kfree(new_dev->priv);
570 static int vlan_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
572 struct net_device *dev = (struct net_device *)(ptr);
573 struct vlan_group *grp = NULL;
575 struct net_device *vlandev = NULL;
577 spin_lock_bh(&vlan_group_lock);
578 grp = __vlan_find_group(dev->ifindex);
579 spin_unlock_bh(&vlan_group_lock);
584 /* It is OK that we do not hold the group lock right now,
585 * as we run under the RTNL lock.
589 case NETDEV_CHANGEADDR:
590 case NETDEV_GOING_DOWN:
595 /* Put all VLANs for this dev in the down state too. */
596 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
597 vlandev = grp->vlan_devices[i];
601 flgs = vlandev->flags;
602 if (!(flgs & IFF_UP))
605 dev_change_flags(vlandev, flgs & ~IFF_UP);
610 /* Put all VLANs for this dev in the up state too. */
611 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
612 vlandev = grp->vlan_devices[i];
616 flgs = vlandev->flags;
620 dev_change_flags(vlandev, flgs | IFF_UP);
624 case NETDEV_UNREGISTER:
625 /* Delete all VLANs for this dev. */
626 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
629 vlandev = grp->vlan_devices[i];
633 ret = unregister_vlan_dev(dev,
634 VLAN_DEV_INFO(vlandev)->vlan_id);
637 unregister_netdevice(vlandev);
639 /* Group was destroyed? */
651 * VLAN IOCTL handler.
652 * o execute requested action or pass command to the device driver
653 * arg is really a void* to a vlan_ioctl_args structure.
655 int vlan_ioctl_handler(unsigned long arg)
658 struct vlan_ioctl_args args;
660 /* everything here needs root permissions, except aguably the
661 * hack ioctls for sending packets. However, I know _I_ don't
662 * want users running that on my network! --BLG
664 if (!capable(CAP_NET_ADMIN))
667 if (copy_from_user(&args, (void*)arg,
668 sizeof(struct vlan_ioctl_args)))
671 /* Null terminate this sucker, just in case. */
672 args.device1[23] = 0;
673 args.u.device2[23] = 0;
676 printk(VLAN_DBG "%s: args.cmd: %x\n", __FUNCTION__, args.cmd);
680 case SET_VLAN_INGRESS_PRIORITY_CMD:
681 err = vlan_dev_set_ingress_priority(args.device1,
686 case SET_VLAN_EGRESS_PRIORITY_CMD:
687 err = vlan_dev_set_egress_priority(args.device1,
692 case SET_VLAN_FLAG_CMD:
693 err = vlan_dev_set_vlan_flag(args.device1,
698 case SET_VLAN_NAME_TYPE_CMD:
699 if ((args.u.name_type >= 0) &&
700 (args.u.name_type < VLAN_NAME_TYPE_HIGHEST)) {
701 vlan_name_type = args.u.name_type;
708 /* TODO: Figure out how to pass info back...
709 case GET_VLAN_INGRESS_PRIORITY_IOCTL:
710 err = vlan_dev_get_ingress_priority(args);
713 case GET_VLAN_EGRESS_PRIORITY_IOCTL:
714 err = vlan_dev_get_egress_priority(args);
719 /* we have been given the name of the Ethernet Device we want to
720 * talk to: args.dev1 We also have the
721 * VLAN ID: args.u.VID
723 if (register_vlan_device(args.device1, args.u.VID)) {
731 /* Here, the args.dev1 is the actual VLAN we want
734 err = unregister_vlan_device(args.device1);
738 /* pass on to underlying device instead?? */
739 printk(VLAN_DBG "%s: Unknown VLAN CMD: %x \n",
740 __FUNCTION__, args.cmd);
747 MODULE_LICENSE("GPL");