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