xref: /openbmc/linux/net/8021q/vlan.c (revision 3ff50b7997fe06cd5d276b229967bb52d6b3b6c1)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		802.1Q VLAN
31da177e4SLinus Torvalds  *		Ethernet-type device handling.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Authors:	Ben Greear <greearb@candelatech.com>
61da177e4SLinus Torvalds  *              Please send support related email to: vlan@scry.wanfear.com
71da177e4SLinus Torvalds  *              VLAN Home Page: http://www.candelatech.com/~greear/vlan.html
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Fixes:
101da177e4SLinus Torvalds  *              Fix for packet capture - Nick Eggleston <nick@dccinc.com>;
111da177e4SLinus Torvalds  *		Add HW acceleration hooks - David S. Miller <davem@redhat.com>;
121da177e4SLinus Torvalds  *		Correct all the locking - David S. Miller <davem@redhat.com>;
131da177e4SLinus Torvalds  *		Use hash table for VLAN groups - David S. Miller <davem@redhat.com>
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
161da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
171da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
181da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
191da177e4SLinus Torvalds  */
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #include <asm/uaccess.h> /* for copy_from_user */
224fc268d2SRandy Dunlap #include <linux/capability.h>
231da177e4SLinus Torvalds #include <linux/module.h>
241da177e4SLinus Torvalds #include <linux/netdevice.h>
251da177e4SLinus Torvalds #include <linux/skbuff.h>
261da177e4SLinus Torvalds #include <net/datalink.h>
271da177e4SLinus Torvalds #include <linux/mm.h>
281da177e4SLinus Torvalds #include <linux/in.h>
291da177e4SLinus Torvalds #include <linux/init.h>
301da177e4SLinus Torvalds #include <net/p8022.h>
311da177e4SLinus Torvalds #include <net/arp.h>
321da177e4SLinus Torvalds #include <linux/rtnetlink.h>
331da177e4SLinus Torvalds #include <linux/notifier.h>
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds #include <linux/if_vlan.h>
361da177e4SLinus Torvalds #include "vlan.h"
371da177e4SLinus Torvalds #include "vlanproc.h"
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #define DRV_VERSION "1.8"
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds /* Global VLAN variables */
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds /* Our listing of VLAN group(s) */
441da177e4SLinus Torvalds static struct hlist_head vlan_group_hash[VLAN_GRP_HASH_SIZE];
451da177e4SLinus Torvalds #define vlan_grp_hashfn(IDX)	((((IDX) >> VLAN_GRP_HASH_SHIFT) ^ (IDX)) & VLAN_GRP_HASH_MASK)
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds static char vlan_fullname[] = "802.1Q VLAN Support";
481da177e4SLinus Torvalds static char vlan_version[] = DRV_VERSION;
491da177e4SLinus Torvalds static char vlan_copyright[] = "Ben Greear <greearb@candelatech.com>";
501da177e4SLinus Torvalds static char vlan_buggyright[] = "David S. Miller <davem@redhat.com>";
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds static int vlan_device_event(struct notifier_block *, unsigned long, void *);
531da177e4SLinus Torvalds static int vlan_ioctl_handler(void __user *);
541da177e4SLinus Torvalds static int unregister_vlan_dev(struct net_device *, unsigned short );
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds static struct notifier_block vlan_notifier_block = {
571da177e4SLinus Torvalds 	.notifier_call = vlan_device_event,
581da177e4SLinus Torvalds };
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds /* These may be changed at run-time through IOCTLs */
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds /* Determines interface naming scheme. */
631da177e4SLinus Torvalds unsigned short vlan_name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds static struct packet_type vlan_packet_type = {
661da177e4SLinus Torvalds 	.type = __constant_htons(ETH_P_8021Q),
671da177e4SLinus Torvalds 	.func = vlan_skb_recv, /* VLAN receive method */
681da177e4SLinus Torvalds };
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds /* End of global variables definitions. */
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds /*
731da177e4SLinus Torvalds  * Function vlan_proto_init (pro)
741da177e4SLinus Torvalds  *
751da177e4SLinus Torvalds  *    Initialize VLAN protocol layer,
761da177e4SLinus Torvalds  *
771da177e4SLinus Torvalds  */
781da177e4SLinus Torvalds static int __init vlan_proto_init(void)
791da177e4SLinus Torvalds {
801da177e4SLinus Torvalds 	int err;
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 	printk(VLAN_INF "%s v%s %s\n",
831da177e4SLinus Torvalds 	       vlan_fullname, vlan_version, vlan_copyright);
841da177e4SLinus Torvalds 	printk(VLAN_INF "All bugs added by %s\n",
851da177e4SLinus Torvalds 	       vlan_buggyright);
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	/* proc file system initialization */
881da177e4SLinus Torvalds 	err = vlan_proc_init();
891da177e4SLinus Torvalds 	if (err < 0) {
901da177e4SLinus Torvalds 		printk(KERN_ERR
911da177e4SLinus Torvalds 		       "%s %s: can't create entry in proc filesystem!\n",
921da177e4SLinus Torvalds 		       __FUNCTION__, VLAN_NAME);
931da177e4SLinus Torvalds 		return err;
941da177e4SLinus Torvalds 	}
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	dev_add_pack(&vlan_packet_type);
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	/* Register us to receive netdevice events */
991da177e4SLinus Torvalds 	err = register_netdevice_notifier(&vlan_notifier_block);
1001da177e4SLinus Torvalds 	if (err < 0) {
1011da177e4SLinus Torvalds 		dev_remove_pack(&vlan_packet_type);
1021da177e4SLinus Torvalds 		vlan_proc_cleanup();
1031da177e4SLinus Torvalds 		return err;
1041da177e4SLinus Torvalds 	}
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 	vlan_ioctl_set(vlan_ioctl_handler);
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 	return 0;
1091da177e4SLinus Torvalds }
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds /* Cleanup all vlan devices
1121da177e4SLinus Torvalds  * Note: devices that have been registered that but not
1131da177e4SLinus Torvalds  * brought up will exist but have no module ref count.
1141da177e4SLinus Torvalds  */
1151da177e4SLinus Torvalds static void __exit vlan_cleanup_devices(void)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	struct net_device *dev, *nxt;
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds 	rtnl_lock();
1201da177e4SLinus Torvalds 	for (dev = dev_base; dev; dev = nxt) {
1211da177e4SLinus Torvalds 		nxt = dev->next;
1221da177e4SLinus Torvalds 		if (dev->priv_flags & IFF_802_1Q_VLAN) {
1231da177e4SLinus Torvalds 			unregister_vlan_dev(VLAN_DEV_INFO(dev)->real_dev,
1241da177e4SLinus Torvalds 					    VLAN_DEV_INFO(dev)->vlan_id);
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 			unregister_netdevice(dev);
1271da177e4SLinus Torvalds 		}
1281da177e4SLinus Torvalds 	}
1291da177e4SLinus Torvalds 	rtnl_unlock();
1301da177e4SLinus Torvalds }
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds /*
1331da177e4SLinus Torvalds  *     Module 'remove' entry point.
1341da177e4SLinus Torvalds  *     o delete /proc/net/router directory and static entries.
1351da177e4SLinus Torvalds  */
1361da177e4SLinus Torvalds static void __exit vlan_cleanup_module(void)
1371da177e4SLinus Torvalds {
1381da177e4SLinus Torvalds 	int i;
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds 	vlan_ioctl_set(NULL);
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	/* Un-register us from receiving netdevice events */
1431da177e4SLinus Torvalds 	unregister_netdevice_notifier(&vlan_notifier_block);
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	dev_remove_pack(&vlan_packet_type);
1461da177e4SLinus Torvalds 	vlan_cleanup_devices();
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 	/* This table must be empty if there are no module
1491da177e4SLinus Torvalds 	 * references left.
1501da177e4SLinus Torvalds 	 */
1511da177e4SLinus Torvalds 	for (i = 0; i < VLAN_GRP_HASH_SIZE; i++) {
1521da177e4SLinus Torvalds 		BUG_ON(!hlist_empty(&vlan_group_hash[i]));
1531da177e4SLinus Torvalds 	}
1541da177e4SLinus Torvalds 	vlan_proc_cleanup();
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	synchronize_net();
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds module_init(vlan_proto_init);
1601da177e4SLinus Torvalds module_exit(vlan_cleanup_module);
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds /* Must be invoked with RCU read lock (no preempt) */
1631da177e4SLinus Torvalds static struct vlan_group *__vlan_find_group(int real_dev_ifindex)
1641da177e4SLinus Torvalds {
1651da177e4SLinus Torvalds 	struct vlan_group *grp;
1661da177e4SLinus Torvalds 	struct hlist_node *n;
1671da177e4SLinus Torvalds 	int hash = vlan_grp_hashfn(real_dev_ifindex);
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	hlist_for_each_entry_rcu(grp, n, &vlan_group_hash[hash], hlist) {
1701da177e4SLinus Torvalds 		if (grp->real_dev_ifindex == real_dev_ifindex)
1711da177e4SLinus Torvalds 			return grp;
1721da177e4SLinus Torvalds 	}
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	return NULL;
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds /*  Find the protocol handler.  Assumes VID < VLAN_VID_MASK.
1781da177e4SLinus Torvalds  *
1791da177e4SLinus Torvalds  * Must be invoked with RCU read lock (no preempt)
1801da177e4SLinus Torvalds  */
1811da177e4SLinus Torvalds struct net_device *__find_vlan_dev(struct net_device *real_dev,
1821da177e4SLinus Torvalds 				   unsigned short VID)
1831da177e4SLinus Torvalds {
1841da177e4SLinus Torvalds 	struct vlan_group *grp = __vlan_find_group(real_dev->ifindex);
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds 	if (grp)
1875c15bdecSDan Aloni 		return vlan_group_get_device(grp, VID);
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	return NULL;
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
1925c15bdecSDan Aloni static void vlan_group_free(struct vlan_group *grp)
1935c15bdecSDan Aloni {
1945c15bdecSDan Aloni 	int i;
1955c15bdecSDan Aloni 
1965c15bdecSDan Aloni 	for (i=0; i < VLAN_GROUP_ARRAY_SPLIT_PARTS; i++)
1975c15bdecSDan Aloni 		kfree(grp->vlan_devices_arrays[i]);
1985c15bdecSDan Aloni 	kfree(grp);
1995c15bdecSDan Aloni }
2005c15bdecSDan Aloni 
2011da177e4SLinus Torvalds static void vlan_rcu_free(struct rcu_head *rcu)
2021da177e4SLinus Torvalds {
2035c15bdecSDan Aloni 	vlan_group_free(container_of(rcu, struct vlan_group, rcu));
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds /* This returns 0 if everything went fine.
2081da177e4SLinus Torvalds  * It will return 1 if the group was killed as a result.
2091da177e4SLinus Torvalds  * A negative return indicates failure.
2101da177e4SLinus Torvalds  *
2111da177e4SLinus Torvalds  * The RTNL lock must be held.
2121da177e4SLinus Torvalds  */
2131da177e4SLinus Torvalds static int unregister_vlan_dev(struct net_device *real_dev,
2141da177e4SLinus Torvalds 			       unsigned short vlan_id)
2151da177e4SLinus Torvalds {
2161da177e4SLinus Torvalds 	struct net_device *dev = NULL;
2171da177e4SLinus Torvalds 	int real_dev_ifindex = real_dev->ifindex;
2181da177e4SLinus Torvalds 	struct vlan_group *grp;
2191da177e4SLinus Torvalds 	int i, ret;
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds #ifdef VLAN_DEBUG
2221da177e4SLinus Torvalds 	printk(VLAN_DBG "%s: VID: %i\n", __FUNCTION__, vlan_id);
2231da177e4SLinus Torvalds #endif
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	/* sanity check */
2261da177e4SLinus Torvalds 	if (vlan_id >= VLAN_VID_MASK)
2271da177e4SLinus Torvalds 		return -EINVAL;
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 	ASSERT_RTNL();
2301da177e4SLinus Torvalds 	grp = __vlan_find_group(real_dev_ifindex);
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	ret = 0;
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	if (grp) {
2355c15bdecSDan Aloni 		dev = vlan_group_get_device(grp, vlan_id);
2361da177e4SLinus Torvalds 		if (dev) {
2371da177e4SLinus Torvalds 			/* Remove proc entry */
2381da177e4SLinus Torvalds 			vlan_proc_rem_dev(dev);
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 			/* Take it out of our own structures, but be sure to
2411da177e4SLinus Torvalds 			 * interlock with HW accelerating devices or SW vlan
2421da177e4SLinus Torvalds 			 * input packet processing.
2431da177e4SLinus Torvalds 			 */
2441da177e4SLinus Torvalds 			if (real_dev->features &
2451da177e4SLinus Torvalds 			    (NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER)) {
2461da177e4SLinus Torvalds 				real_dev->vlan_rx_kill_vid(real_dev, vlan_id);
2471da177e4SLinus Torvalds 			}
2481da177e4SLinus Torvalds 
2495c15bdecSDan Aloni 			vlan_group_set_device(grp, vlan_id, NULL);
2501da177e4SLinus Torvalds 			synchronize_net();
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 			/* Caller unregisters (and if necessary, puts)
2541da177e4SLinus Torvalds 			 * VLAN device, but we get rid of the reference to
2551da177e4SLinus Torvalds 			 * real_dev here.
2561da177e4SLinus Torvalds 			 */
2571da177e4SLinus Torvalds 			dev_put(real_dev);
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 			/* If the group is now empty, kill off the
2601da177e4SLinus Torvalds 			 * group.
2611da177e4SLinus Torvalds 			 */
2621da177e4SLinus Torvalds 			for (i = 0; i < VLAN_VID_MASK; i++)
2635c15bdecSDan Aloni 				if (vlan_group_get_device(grp, i))
2641da177e4SLinus Torvalds 					break;
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 			if (i == VLAN_VID_MASK) {
2671da177e4SLinus Torvalds 				if (real_dev->features & NETIF_F_HW_VLAN_RX)
2681da177e4SLinus Torvalds 					real_dev->vlan_rx_register(real_dev, NULL);
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 				hlist_del_rcu(&grp->hlist);
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 				/* Free the group, after all cpu's are done. */
2731da177e4SLinus Torvalds 				call_rcu(&grp->rcu, vlan_rcu_free);
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 				grp = NULL;
2761da177e4SLinus Torvalds 				ret = 1;
2771da177e4SLinus Torvalds 			}
2781da177e4SLinus Torvalds 		}
2791da177e4SLinus Torvalds 	}
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	return ret;
2821da177e4SLinus Torvalds }
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds static int unregister_vlan_device(const char *vlan_IF_name)
2851da177e4SLinus Torvalds {
2861da177e4SLinus Torvalds 	struct net_device *dev = NULL;
2871da177e4SLinus Torvalds 	int ret;
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	dev = dev_get_by_name(vlan_IF_name);
2911da177e4SLinus Torvalds 	ret = -EINVAL;
2921da177e4SLinus Torvalds 	if (dev) {
2931da177e4SLinus Torvalds 		if (dev->priv_flags & IFF_802_1Q_VLAN) {
2941da177e4SLinus Torvalds 			rtnl_lock();
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 			ret = unregister_vlan_dev(VLAN_DEV_INFO(dev)->real_dev,
2971da177e4SLinus Torvalds 						  VLAN_DEV_INFO(dev)->vlan_id);
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 			dev_put(dev);
3001da177e4SLinus Torvalds 			unregister_netdevice(dev);
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 			rtnl_unlock();
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 			if (ret == 1)
3051da177e4SLinus Torvalds 				ret = 0;
3061da177e4SLinus Torvalds 		} else {
3071da177e4SLinus Torvalds 			printk(VLAN_ERR
3081da177e4SLinus Torvalds 			       "%s: ERROR:	Tried to remove a non-vlan device "
3091da177e4SLinus Torvalds 			       "with VLAN code, name: %s  priv_flags: %hX\n",
3101da177e4SLinus Torvalds 			       __FUNCTION__, dev->name, dev->priv_flags);
3111da177e4SLinus Torvalds 			dev_put(dev);
3121da177e4SLinus Torvalds 			ret = -EPERM;
3131da177e4SLinus Torvalds 		}
3141da177e4SLinus Torvalds 	} else {
3151da177e4SLinus Torvalds #ifdef VLAN_DEBUG
3161da177e4SLinus Torvalds 		printk(VLAN_DBG "%s: WARNING: Could not find dev.\n", __FUNCTION__);
3171da177e4SLinus Torvalds #endif
3181da177e4SLinus Torvalds 		ret = -EINVAL;
3191da177e4SLinus Torvalds 	}
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	return ret;
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds static void vlan_setup(struct net_device *new_dev)
3251da177e4SLinus Torvalds {
3261da177e4SLinus Torvalds 	SET_MODULE_OWNER(new_dev);
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	/* new_dev->ifindex = 0;  it will be set when added to
3291da177e4SLinus Torvalds 	 * the global list.
3301da177e4SLinus Torvalds 	 * iflink is set as well.
3311da177e4SLinus Torvalds 	 */
3321da177e4SLinus Torvalds 	new_dev->get_stats = vlan_dev_get_stats;
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	/* Make this thing known as a VLAN device */
3351da177e4SLinus Torvalds 	new_dev->priv_flags |= IFF_802_1Q_VLAN;
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	/* Set us up to have no queue, as the underlying Hardware device
3381da177e4SLinus Torvalds 	 * can do all the queueing we could want.
3391da177e4SLinus Torvalds 	 */
3401da177e4SLinus Torvalds 	new_dev->tx_queue_len = 0;
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	/* set up method calls */
3431da177e4SLinus Torvalds 	new_dev->change_mtu = vlan_dev_change_mtu;
3441da177e4SLinus Torvalds 	new_dev->open = vlan_dev_open;
3451da177e4SLinus Torvalds 	new_dev->stop = vlan_dev_stop;
3461da177e4SLinus Torvalds 	new_dev->set_mac_address = vlan_dev_set_mac_address;
3471da177e4SLinus Torvalds 	new_dev->set_multicast_list = vlan_dev_set_multicast_list;
3481da177e4SLinus Torvalds 	new_dev->destructor = free_netdev;
3491da177e4SLinus Torvalds 	new_dev->do_ioctl = vlan_dev_ioctl;
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
352ddd7bf9fSStefan Rompf static void vlan_transfer_operstate(const struct net_device *dev, struct net_device *vlandev)
353ddd7bf9fSStefan Rompf {
354ddd7bf9fSStefan Rompf 	/* Have to respect userspace enforced dormant state
355ddd7bf9fSStefan Rompf 	 * of real device, also must allow supplicant running
356ddd7bf9fSStefan Rompf 	 * on VLAN device
357ddd7bf9fSStefan Rompf 	 */
358ddd7bf9fSStefan Rompf 	if (dev->operstate == IF_OPER_DORMANT)
359ddd7bf9fSStefan Rompf 		netif_dormant_on(vlandev);
360ddd7bf9fSStefan Rompf 	else
361ddd7bf9fSStefan Rompf 		netif_dormant_off(vlandev);
362ddd7bf9fSStefan Rompf 
363ddd7bf9fSStefan Rompf 	if (netif_carrier_ok(dev)) {
364ddd7bf9fSStefan Rompf 		if (!netif_carrier_ok(vlandev))
365ddd7bf9fSStefan Rompf 			netif_carrier_on(vlandev);
366ddd7bf9fSStefan Rompf 	} else {
367ddd7bf9fSStefan Rompf 		if (netif_carrier_ok(vlandev))
368ddd7bf9fSStefan Rompf 			netif_carrier_off(vlandev);
369ddd7bf9fSStefan Rompf 	}
370ddd7bf9fSStefan Rompf }
371ddd7bf9fSStefan Rompf 
3725dd8d1e9SArjan van de Ven /*
3735dd8d1e9SArjan van de Ven  * vlan network devices have devices nesting below it, and are a special
3745dd8d1e9SArjan van de Ven  * "super class" of normal network devices; split their locks off into a
3755dd8d1e9SArjan van de Ven  * separate class since they always nest.
3765dd8d1e9SArjan van de Ven  */
3775dd8d1e9SArjan van de Ven static struct lock_class_key vlan_netdev_xmit_lock_key;
3785dd8d1e9SArjan van de Ven 
3795dd8d1e9SArjan van de Ven 
3801da177e4SLinus Torvalds /*  Attach a VLAN device to a mac address (ie Ethernet Card).
3811da177e4SLinus Torvalds  *  Returns the device that was created, or NULL if there was
3821da177e4SLinus Torvalds  *  an error of some kind.
3831da177e4SLinus Torvalds  */
3841da177e4SLinus Torvalds static struct net_device *register_vlan_device(const char *eth_IF_name,
3851da177e4SLinus Torvalds 					       unsigned short VLAN_ID)
3861da177e4SLinus Torvalds {
3871da177e4SLinus Torvalds 	struct vlan_group *grp;
3881da177e4SLinus Torvalds 	struct net_device *new_dev;
3891da177e4SLinus Torvalds 	struct net_device *real_dev; /* the ethernet device */
3901da177e4SLinus Torvalds 	char name[IFNAMSIZ];
3915c15bdecSDan Aloni 	int i;
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds #ifdef VLAN_DEBUG
3941da177e4SLinus Torvalds 	printk(VLAN_DBG "%s: if_name -:%s:-	vid: %i\n",
3951da177e4SLinus Torvalds 		__FUNCTION__, eth_IF_name, VLAN_ID);
3961da177e4SLinus Torvalds #endif
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	if (VLAN_ID >= VLAN_VID_MASK)
3991da177e4SLinus Torvalds 		goto out_ret_null;
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	/* find the device relating to eth_IF_name. */
4021da177e4SLinus Torvalds 	real_dev = dev_get_by_name(eth_IF_name);
4031da177e4SLinus Torvalds 	if (!real_dev)
4041da177e4SLinus Torvalds 		goto out_ret_null;
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	if (real_dev->features & NETIF_F_VLAN_CHALLENGED) {
4071da177e4SLinus Torvalds 		printk(VLAN_DBG "%s: VLANs not supported on %s.\n",
4081da177e4SLinus Torvalds 			__FUNCTION__, real_dev->name);
4091da177e4SLinus Torvalds 		goto out_put_dev;
4101da177e4SLinus Torvalds 	}
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	if ((real_dev->features & NETIF_F_HW_VLAN_RX) &&
4131da177e4SLinus Torvalds 	    (real_dev->vlan_rx_register == NULL ||
4141da177e4SLinus Torvalds 	     real_dev->vlan_rx_kill_vid == NULL)) {
4151da177e4SLinus Torvalds 		printk(VLAN_DBG "%s: Device %s has buggy VLAN hw accel.\n",
4161da177e4SLinus Torvalds 			__FUNCTION__, real_dev->name);
4171da177e4SLinus Torvalds 		goto out_put_dev;
4181da177e4SLinus Torvalds 	}
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	if ((real_dev->features & NETIF_F_HW_VLAN_FILTER) &&
4211da177e4SLinus Torvalds 	    (real_dev->vlan_rx_add_vid == NULL ||
4221da177e4SLinus Torvalds 	     real_dev->vlan_rx_kill_vid == NULL)) {
4231da177e4SLinus Torvalds 		printk(VLAN_DBG "%s: Device %s has buggy VLAN hw accel.\n",
4241da177e4SLinus Torvalds 			__FUNCTION__, real_dev->name);
4251da177e4SLinus Torvalds 		goto out_put_dev;
4261da177e4SLinus Torvalds 	}
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 	/* From this point on, all the data structures must remain
4291da177e4SLinus Torvalds 	 * consistent.
4301da177e4SLinus Torvalds 	 */
4311da177e4SLinus Torvalds 	rtnl_lock();
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	/* The real device must be up and operating in order to
4341da177e4SLinus Torvalds 	 * assosciate a VLAN device with it.
4351da177e4SLinus Torvalds 	 */
4361da177e4SLinus Torvalds 	if (!(real_dev->flags & IFF_UP))
4371da177e4SLinus Torvalds 		goto out_unlock;
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	if (__find_vlan_dev(real_dev, VLAN_ID) != NULL) {
4401da177e4SLinus Torvalds 		/* was already registered. */
4411da177e4SLinus Torvalds 		printk(VLAN_DBG "%s: ALREADY had VLAN registered\n", __FUNCTION__);
4421da177e4SLinus Torvalds 		goto out_unlock;
4431da177e4SLinus Torvalds 	}
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	/* Gotta set up the fields for the device. */
4461da177e4SLinus Torvalds #ifdef VLAN_DEBUG
4471da177e4SLinus Torvalds 	printk(VLAN_DBG "About to allocate name, vlan_name_type: %i\n",
4481da177e4SLinus Torvalds 	       vlan_name_type);
4491da177e4SLinus Torvalds #endif
4501da177e4SLinus Torvalds 	switch (vlan_name_type) {
4511da177e4SLinus Torvalds 	case VLAN_NAME_TYPE_RAW_PLUS_VID:
4521da177e4SLinus Torvalds 		/* name will look like:	 eth1.0005 */
4531da177e4SLinus Torvalds 		snprintf(name, IFNAMSIZ, "%s.%.4i", real_dev->name, VLAN_ID);
4541da177e4SLinus Torvalds 		break;
4551da177e4SLinus Torvalds 	case VLAN_NAME_TYPE_PLUS_VID_NO_PAD:
4561da177e4SLinus Torvalds 		/* Put our vlan.VID in the name.
4571da177e4SLinus Torvalds 		 * Name will look like:	 vlan5
4581da177e4SLinus Torvalds 		 */
4591da177e4SLinus Torvalds 		snprintf(name, IFNAMSIZ, "vlan%i", VLAN_ID);
4601da177e4SLinus Torvalds 		break;
4611da177e4SLinus Torvalds 	case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD:
4621da177e4SLinus Torvalds 		/* Put our vlan.VID in the name.
4631da177e4SLinus Torvalds 		 * Name will look like:	 eth0.5
4641da177e4SLinus Torvalds 		 */
4651da177e4SLinus Torvalds 		snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, VLAN_ID);
4661da177e4SLinus Torvalds 		break;
4671da177e4SLinus Torvalds 	case VLAN_NAME_TYPE_PLUS_VID:
4681da177e4SLinus Torvalds 		/* Put our vlan.VID in the name.
4691da177e4SLinus Torvalds 		 * Name will look like:	 vlan0005
4701da177e4SLinus Torvalds 		 */
4711da177e4SLinus Torvalds 	default:
4721da177e4SLinus Torvalds 		snprintf(name, IFNAMSIZ, "vlan%.4i", VLAN_ID);
473*3ff50b79SStephen Hemminger 	}
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	new_dev = alloc_netdev(sizeof(struct vlan_dev_info), name,
4761da177e4SLinus Torvalds 			       vlan_setup);
4775dd8d1e9SArjan van de Ven 
4781da177e4SLinus Torvalds 	if (new_dev == NULL)
4791da177e4SLinus Torvalds 		goto out_unlock;
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds #ifdef VLAN_DEBUG
4821da177e4SLinus Torvalds 	printk(VLAN_DBG "Allocated new name -:%s:-\n", new_dev->name);
4831da177e4SLinus Torvalds #endif
4841da177e4SLinus Torvalds 	/* IFF_BROADCAST|IFF_MULTICAST; ??? */
4851da177e4SLinus Torvalds 	new_dev->flags = real_dev->flags;
4861da177e4SLinus Torvalds 	new_dev->flags &= ~IFF_UP;
4871da177e4SLinus Torvalds 
4886c753c3dSStefan Rompf 	new_dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
4896c753c3dSStefan Rompf 					     (1<<__LINK_STATE_DORMANT))) |
4906c753c3dSStefan Rompf 			 (1<<__LINK_STATE_PRESENT);
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	/* need 4 bytes for extra VLAN header info,
4931da177e4SLinus Torvalds 	 * hope the underlying device can handle it.
4941da177e4SLinus Torvalds 	 */
4951da177e4SLinus Torvalds 	new_dev->mtu = real_dev->mtu;
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	/* TODO: maybe just assign it to be ETHERNET? */
4981da177e4SLinus Torvalds 	new_dev->type = real_dev->type;
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds 	new_dev->hard_header_len = real_dev->hard_header_len;
5011da177e4SLinus Torvalds 	if (!(real_dev->features & NETIF_F_HW_VLAN_TX)) {
5021da177e4SLinus Torvalds 		/* Regular ethernet + 4 bytes (18 total). */
5031da177e4SLinus Torvalds 		new_dev->hard_header_len += VLAN_HLEN;
5041da177e4SLinus Torvalds 	}
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 	VLAN_MEM_DBG("new_dev->priv malloc, addr: %p  size: %i\n",
5071da177e4SLinus Torvalds 		     new_dev->priv,
5081da177e4SLinus Torvalds 		     sizeof(struct vlan_dev_info));
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	memcpy(new_dev->broadcast, real_dev->broadcast, real_dev->addr_len);
5111da177e4SLinus Torvalds 	memcpy(new_dev->dev_addr, real_dev->dev_addr, real_dev->addr_len);
5121da177e4SLinus Torvalds 	new_dev->addr_len = real_dev->addr_len;
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 	if (real_dev->features & NETIF_F_HW_VLAN_TX) {
5151da177e4SLinus Torvalds 		new_dev->hard_header = real_dev->hard_header;
5161da177e4SLinus Torvalds 		new_dev->hard_start_xmit = vlan_dev_hwaccel_hard_start_xmit;
5171da177e4SLinus Torvalds 		new_dev->rebuild_header = real_dev->rebuild_header;
5181da177e4SLinus Torvalds 	} else {
5191da177e4SLinus Torvalds 		new_dev->hard_header = vlan_dev_hard_header;
5201da177e4SLinus Torvalds 		new_dev->hard_start_xmit = vlan_dev_hard_start_xmit;
5211da177e4SLinus Torvalds 		new_dev->rebuild_header = vlan_dev_rebuild_header;
5221da177e4SLinus Torvalds 	}
5231da177e4SLinus Torvalds 	new_dev->hard_header_parse = real_dev->hard_header_parse;
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 	VLAN_DEV_INFO(new_dev)->vlan_id = VLAN_ID; /* 1 through VLAN_VID_MASK */
5261da177e4SLinus Torvalds 	VLAN_DEV_INFO(new_dev)->real_dev = real_dev;
5271da177e4SLinus Torvalds 	VLAN_DEV_INFO(new_dev)->dent = NULL;
5281da177e4SLinus Torvalds 	VLAN_DEV_INFO(new_dev)->flags = 1;
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds #ifdef VLAN_DEBUG
5311da177e4SLinus Torvalds 	printk(VLAN_DBG "About to go find the group for idx: %i\n",
5321da177e4SLinus Torvalds 	       real_dev->ifindex);
5331da177e4SLinus Torvalds #endif
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds 	if (register_netdevice(new_dev))
5361da177e4SLinus Torvalds 		goto out_free_newdev;
5371da177e4SLinus Torvalds 
5385dd8d1e9SArjan van de Ven 	lockdep_set_class(&new_dev->_xmit_lock, &vlan_netdev_xmit_lock_key);
5395dd8d1e9SArjan van de Ven 
540ddd7bf9fSStefan Rompf 	new_dev->iflink = real_dev->ifindex;
541ddd7bf9fSStefan Rompf 	vlan_transfer_operstate(real_dev, new_dev);
542ddd7bf9fSStefan Rompf 	linkwatch_fire_event(new_dev); /* _MUST_ call rfc2863_policy() */
543ddd7bf9fSStefan Rompf 
5441da177e4SLinus Torvalds 	/* So, got the sucker initialized, now lets place
5451da177e4SLinus Torvalds 	 * it into our local structure.
5461da177e4SLinus Torvalds 	 */
5471da177e4SLinus Torvalds 	grp = __vlan_find_group(real_dev->ifindex);
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds 	/* Note, we are running under the RTNL semaphore
5501da177e4SLinus Torvalds 	 * so it cannot "appear" on us.
5511da177e4SLinus Torvalds 	 */
5521da177e4SLinus Torvalds 	if (!grp) { /* need to add a new group */
5530da974f4SPanagiotis Issaris 		grp = kzalloc(sizeof(struct vlan_group), GFP_KERNEL);
5541da177e4SLinus Torvalds 		if (!grp)
5551da177e4SLinus Torvalds 			goto out_free_unregister;
5561da177e4SLinus Torvalds 
5575c15bdecSDan Aloni 		for (i=0; i < VLAN_GROUP_ARRAY_SPLIT_PARTS; i++) {
5585c15bdecSDan Aloni 			grp->vlan_devices_arrays[i] = kzalloc(
5595c15bdecSDan Aloni 				sizeof(struct net_device *)*VLAN_GROUP_ARRAY_PART_LEN,
5605c15bdecSDan Aloni 				GFP_KERNEL);
5615c15bdecSDan Aloni 
5625c15bdecSDan Aloni 			if (!grp->vlan_devices_arrays[i])
5635c15bdecSDan Aloni 				goto out_free_arrays;
5645c15bdecSDan Aloni 		}
5655c15bdecSDan Aloni 
5661da177e4SLinus Torvalds 		/* printk(KERN_ALERT "VLAN REGISTER:  Allocated new group.\n"); */
5671da177e4SLinus Torvalds 		grp->real_dev_ifindex = real_dev->ifindex;
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 		hlist_add_head_rcu(&grp->hlist,
5701da177e4SLinus Torvalds 				   &vlan_group_hash[vlan_grp_hashfn(real_dev->ifindex)]);
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 		if (real_dev->features & NETIF_F_HW_VLAN_RX)
5731da177e4SLinus Torvalds 			real_dev->vlan_rx_register(real_dev, grp);
5741da177e4SLinus Torvalds 	}
5751da177e4SLinus Torvalds 
5765c15bdecSDan Aloni 	vlan_group_set_device(grp, VLAN_ID, new_dev);
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	if (vlan_proc_add_dev(new_dev)<0)/* create it's proc entry */
5791da177e4SLinus Torvalds 		printk(KERN_WARNING "VLAN: failed to add proc entry for %s\n",
5801da177e4SLinus Torvalds 							 new_dev->name);
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
5831da177e4SLinus Torvalds 		real_dev->vlan_rx_add_vid(real_dev, VLAN_ID);
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	rtnl_unlock();
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds #ifdef VLAN_DEBUG
5891da177e4SLinus Torvalds 	printk(VLAN_DBG "Allocated new device successfully, returning.\n");
5901da177e4SLinus Torvalds #endif
5911da177e4SLinus Torvalds 	return new_dev;
5921da177e4SLinus Torvalds 
5935c15bdecSDan Aloni out_free_arrays:
5945c15bdecSDan Aloni 	vlan_group_free(grp);
5955c15bdecSDan Aloni 
5961da177e4SLinus Torvalds out_free_unregister:
5971da177e4SLinus Torvalds 	unregister_netdev(new_dev);
5981da177e4SLinus Torvalds 	goto out_unlock;
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds out_free_newdev:
6011da177e4SLinus Torvalds 	free_netdev(new_dev);
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds out_unlock:
6041da177e4SLinus Torvalds 	rtnl_unlock();
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds out_put_dev:
6071da177e4SLinus Torvalds 	dev_put(real_dev);
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds out_ret_null:
6101da177e4SLinus Torvalds 	return NULL;
6111da177e4SLinus Torvalds }
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds static int vlan_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
6141da177e4SLinus Torvalds {
6151da177e4SLinus Torvalds 	struct net_device *dev = ptr;
6161da177e4SLinus Torvalds 	struct vlan_group *grp = __vlan_find_group(dev->ifindex);
6171da177e4SLinus Torvalds 	int i, flgs;
6181da177e4SLinus Torvalds 	struct net_device *vlandev;
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds 	if (!grp)
6211da177e4SLinus Torvalds 		goto out;
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	/* It is OK that we do not hold the group lock right now,
6241da177e4SLinus Torvalds 	 * as we run under the RTNL lock.
6251da177e4SLinus Torvalds 	 */
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 	switch (event) {
6281da177e4SLinus Torvalds 	case NETDEV_CHANGE:
6291da177e4SLinus Torvalds 		/* Propagate real device state to vlan devices */
6301da177e4SLinus Torvalds 		for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
6315c15bdecSDan Aloni 			vlandev = vlan_group_get_device(grp, i);
6321da177e4SLinus Torvalds 			if (!vlandev)
6331da177e4SLinus Torvalds 				continue;
6341da177e4SLinus Torvalds 
635ddd7bf9fSStefan Rompf 			vlan_transfer_operstate(dev, vlandev);
6361da177e4SLinus Torvalds 		}
6371da177e4SLinus Torvalds 		break;
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	case NETDEV_DOWN:
6401da177e4SLinus Torvalds 		/* Put all VLANs for this dev in the down state too.  */
6411da177e4SLinus Torvalds 		for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
6425c15bdecSDan Aloni 			vlandev = vlan_group_get_device(grp, i);
6431da177e4SLinus Torvalds 			if (!vlandev)
6441da177e4SLinus Torvalds 				continue;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 			flgs = vlandev->flags;
6471da177e4SLinus Torvalds 			if (!(flgs & IFF_UP))
6481da177e4SLinus Torvalds 				continue;
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 			dev_change_flags(vlandev, flgs & ~IFF_UP);
6511da177e4SLinus Torvalds 		}
6521da177e4SLinus Torvalds 		break;
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	case NETDEV_UP:
6551da177e4SLinus Torvalds 		/* Put all VLANs for this dev in the up state too.  */
6561da177e4SLinus Torvalds 		for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
6575c15bdecSDan Aloni 			vlandev = vlan_group_get_device(grp, i);
6581da177e4SLinus Torvalds 			if (!vlandev)
6591da177e4SLinus Torvalds 				continue;
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 			flgs = vlandev->flags;
6621da177e4SLinus Torvalds 			if (flgs & IFF_UP)
6631da177e4SLinus Torvalds 				continue;
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds 			dev_change_flags(vlandev, flgs | IFF_UP);
6661da177e4SLinus Torvalds 		}
6671da177e4SLinus Torvalds 		break;
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
6701da177e4SLinus Torvalds 		/* Delete all VLANs for this dev. */
6711da177e4SLinus Torvalds 		for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
6721da177e4SLinus Torvalds 			int ret;
6731da177e4SLinus Torvalds 
6745c15bdecSDan Aloni 			vlandev = vlan_group_get_device(grp, i);
6751da177e4SLinus Torvalds 			if (!vlandev)
6761da177e4SLinus Torvalds 				continue;
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 			ret = unregister_vlan_dev(dev,
6791da177e4SLinus Torvalds 						  VLAN_DEV_INFO(vlandev)->vlan_id);
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 			unregister_netdevice(vlandev);
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 			/* Group was destroyed? */
6841da177e4SLinus Torvalds 			if (ret == 1)
6851da177e4SLinus Torvalds 				break;
6861da177e4SLinus Torvalds 		}
6871da177e4SLinus Torvalds 		break;
688*3ff50b79SStephen Hemminger 	}
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds out:
6911da177e4SLinus Torvalds 	return NOTIFY_DONE;
6921da177e4SLinus Torvalds }
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds /*
6951da177e4SLinus Torvalds  *	VLAN IOCTL handler.
6961da177e4SLinus Torvalds  *	o execute requested action or pass command to the device driver
6971da177e4SLinus Torvalds  *   arg is really a struct vlan_ioctl_args __user *.
6981da177e4SLinus Torvalds  */
6991da177e4SLinus Torvalds static int vlan_ioctl_handler(void __user *arg)
7001da177e4SLinus Torvalds {
7011da177e4SLinus Torvalds 	int err = 0;
7021da177e4SLinus Torvalds 	unsigned short vid = 0;
7031da177e4SLinus Torvalds 	struct vlan_ioctl_args args;
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 	if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args)))
7061da177e4SLinus Torvalds 		return -EFAULT;
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds 	/* Null terminate this sucker, just in case. */
7091da177e4SLinus Torvalds 	args.device1[23] = 0;
7101da177e4SLinus Torvalds 	args.u.device2[23] = 0;
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds #ifdef VLAN_DEBUG
7131da177e4SLinus Torvalds 	printk(VLAN_DBG "%s: args.cmd: %x\n", __FUNCTION__, args.cmd);
7141da177e4SLinus Torvalds #endif
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	switch (args.cmd) {
7171da177e4SLinus Torvalds 	case SET_VLAN_INGRESS_PRIORITY_CMD:
7181da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
7191da177e4SLinus Torvalds 			return -EPERM;
7201da177e4SLinus Torvalds 		err = vlan_dev_set_ingress_priority(args.device1,
7211da177e4SLinus Torvalds 						    args.u.skb_priority,
7221da177e4SLinus Torvalds 						    args.vlan_qos);
7231da177e4SLinus Torvalds 		break;
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 	case SET_VLAN_EGRESS_PRIORITY_CMD:
7261da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
7271da177e4SLinus Torvalds 			return -EPERM;
7281da177e4SLinus Torvalds 		err = vlan_dev_set_egress_priority(args.device1,
7291da177e4SLinus Torvalds 						   args.u.skb_priority,
7301da177e4SLinus Torvalds 						   args.vlan_qos);
7311da177e4SLinus Torvalds 		break;
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds 	case SET_VLAN_FLAG_CMD:
7341da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
7351da177e4SLinus Torvalds 			return -EPERM;
7361da177e4SLinus Torvalds 		err = vlan_dev_set_vlan_flag(args.device1,
7371da177e4SLinus Torvalds 					     args.u.flag,
7381da177e4SLinus Torvalds 					     args.vlan_qos);
7391da177e4SLinus Torvalds 		break;
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	case SET_VLAN_NAME_TYPE_CMD:
7421da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
7431da177e4SLinus Torvalds 			return -EPERM;
7441da177e4SLinus Torvalds 		if ((args.u.name_type >= 0) &&
7451da177e4SLinus Torvalds 		    (args.u.name_type < VLAN_NAME_TYPE_HIGHEST)) {
7461da177e4SLinus Torvalds 			vlan_name_type = args.u.name_type;
7471da177e4SLinus Torvalds 			err = 0;
7481da177e4SLinus Torvalds 		} else {
7491da177e4SLinus Torvalds 			err = -EINVAL;
7501da177e4SLinus Torvalds 		}
7511da177e4SLinus Torvalds 		break;
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 	case ADD_VLAN_CMD:
7541da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
7551da177e4SLinus Torvalds 			return -EPERM;
7561da177e4SLinus Torvalds 		/* we have been given the name of the Ethernet Device we want to
7571da177e4SLinus Torvalds 		 * talk to:  args.dev1	 We also have the
7581da177e4SLinus Torvalds 		 * VLAN ID:  args.u.VID
7591da177e4SLinus Torvalds 		 */
7601da177e4SLinus Torvalds 		if (register_vlan_device(args.device1, args.u.VID)) {
7611da177e4SLinus Torvalds 			err = 0;
7621da177e4SLinus Torvalds 		} else {
7631da177e4SLinus Torvalds 			err = -EINVAL;
7641da177e4SLinus Torvalds 		}
7651da177e4SLinus Torvalds 		break;
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 	case DEL_VLAN_CMD:
7681da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
7691da177e4SLinus Torvalds 			return -EPERM;
7701da177e4SLinus Torvalds 		/* Here, the args.dev1 is the actual VLAN we want
7711da177e4SLinus Torvalds 		 * to get rid of.
7721da177e4SLinus Torvalds 		 */
7731da177e4SLinus Torvalds 		err = unregister_vlan_device(args.device1);
7741da177e4SLinus Torvalds 		break;
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	case GET_VLAN_INGRESS_PRIORITY_CMD:
7771da177e4SLinus Torvalds 		/* TODO:  Implement
7781da177e4SLinus Torvalds 		   err = vlan_dev_get_ingress_priority(args);
7791da177e4SLinus Torvalds 		   if (copy_to_user((void*)arg, &args,
7801da177e4SLinus Torvalds 			sizeof(struct vlan_ioctl_args))) {
7811da177e4SLinus Torvalds 			err = -EFAULT;
7821da177e4SLinus Torvalds 		   }
7831da177e4SLinus Torvalds 		*/
7841da177e4SLinus Torvalds 		err = -EINVAL;
7851da177e4SLinus Torvalds 		break;
7861da177e4SLinus Torvalds 	case GET_VLAN_EGRESS_PRIORITY_CMD:
7871da177e4SLinus Torvalds 		/* TODO:  Implement
7881da177e4SLinus Torvalds 		   err = vlan_dev_get_egress_priority(args.device1, &(args.args);
7891da177e4SLinus Torvalds 		   if (copy_to_user((void*)arg, &args,
7901da177e4SLinus Torvalds 			sizeof(struct vlan_ioctl_args))) {
7911da177e4SLinus Torvalds 			err = -EFAULT;
7921da177e4SLinus Torvalds 		   }
7931da177e4SLinus Torvalds 		*/
7941da177e4SLinus Torvalds 		err = -EINVAL;
7951da177e4SLinus Torvalds 		break;
7961da177e4SLinus Torvalds 	case GET_VLAN_REALDEV_NAME_CMD:
7971da177e4SLinus Torvalds 		err = vlan_dev_get_realdev_name(args.device1, args.u.device2);
7987eb1b3d3SMika Kukkonen 		if (err)
7997eb1b3d3SMika Kukkonen 			goto out;
8001da177e4SLinus Torvalds 		if (copy_to_user(arg, &args,
8011da177e4SLinus Torvalds 				 sizeof(struct vlan_ioctl_args))) {
8021da177e4SLinus Torvalds 			err = -EFAULT;
8031da177e4SLinus Torvalds 		}
8041da177e4SLinus Torvalds 		break;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 	case GET_VLAN_VID_CMD:
8071da177e4SLinus Torvalds 		err = vlan_dev_get_vid(args.device1, &vid);
8087eb1b3d3SMika Kukkonen 		if (err)
8097eb1b3d3SMika Kukkonen 			goto out;
8101da177e4SLinus Torvalds 		args.u.VID = vid;
8111da177e4SLinus Torvalds 		if (copy_to_user(arg, &args,
8121da177e4SLinus Torvalds 				 sizeof(struct vlan_ioctl_args))) {
8131da177e4SLinus Torvalds 		      err = -EFAULT;
8141da177e4SLinus Torvalds 		}
8151da177e4SLinus Torvalds 		break;
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	default:
8181da177e4SLinus Torvalds 		/* pass on to underlying device instead?? */
8191da177e4SLinus Torvalds 		printk(VLAN_DBG "%s: Unknown VLAN CMD: %x \n",
8201da177e4SLinus Torvalds 			__FUNCTION__, args.cmd);
8211da177e4SLinus Torvalds 		return -EINVAL;
822*3ff50b79SStephen Hemminger 	}
8237eb1b3d3SMika Kukkonen out:
8241da177e4SLinus Torvalds 	return err;
8251da177e4SLinus Torvalds }
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds MODULE_LICENSE("GPL");
8281da177e4SLinus Torvalds MODULE_VERSION(DRV_VERSION);
829