vlan.c 16.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5
/*
 * INET		802.1Q VLAN
 *		Ethernet-type device handling.
 *
 * Authors:	Ben Greear <greearb@candelatech.com>
P
Patrick McHardy 已提交
6
 *              Please send support related email to: netdev@vger.kernel.org
L
Linus Torvalds 已提交
7
 *              VLAN Home Page: http://www.candelatech.com/~greear/vlan.html
8
 *
L
Linus Torvalds 已提交
9 10 11 12 13 14 15 16 17 18 19 20
 * Fixes:
 *              Fix for packet capture - Nick Eggleston <nick@dccinc.com>;
 *		Add HW acceleration hooks - David S. Miller <davem@redhat.com>;
 *		Correct all the locking - David S. Miller <davem@redhat.com>;
 *		Use hash table for VLAN groups - David S. Miller <davem@redhat.com>
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 */

J
Joe Perches 已提交
21 22
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

23
#include <linux/capability.h>
L
Linus Torvalds 已提交
24 25 26
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/skbuff.h>
27
#include <linux/slab.h>
L
Linus Torvalds 已提交
28
#include <linux/init.h>
29
#include <linux/rculist.h>
L
Linus Torvalds 已提交
30 31 32 33
#include <net/p8022.h>
#include <net/arp.h>
#include <linux/rtnetlink.h>
#include <linux/notifier.h>
34
#include <net/rtnetlink.h>
35
#include <net/net_namespace.h>
36
#include <net/netns/generic.h>
37
#include <asm/uaccess.h>
L
Linus Torvalds 已提交
38 39 40 41 42 43 44 45 46

#include <linux/if_vlan.h>
#include "vlan.h"
#include "vlanproc.h"

#define DRV_VERSION "1.8"

/* Global VLAN variables */

47
int vlan_net_id __read_mostly;
48

49 50
const char vlan_fullname[] = "802.1Q VLAN Support";
const char vlan_version[] = DRV_VERSION;
L
Linus Torvalds 已提交
51 52 53

/* End of global variables definitions. */

54
static int vlan_group_prealloc_vid(struct vlan_group *vg, u16 vlan_id)
55 56 57 58 59 60
{
	struct net_device **array;
	unsigned int size;

	ASSERT_RTNL();

61
	array = vg->vlan_devices_arrays[vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
62 63 64 65 66 67 68 69
	if (array != NULL)
		return 0;

	size = sizeof(struct net_device *) * VLAN_GROUP_ARRAY_PART_LEN;
	array = kzalloc(size, GFP_KERNEL);
	if (array == NULL)
		return -ENOBUFS;

70
	vg->vlan_devices_arrays[vlan_id / VLAN_GROUP_ARRAY_PART_LEN] = array;
71
	return 0;
72 73
}

74
void unregister_vlan_dev(struct net_device *dev, struct list_head *head)
L
Linus Torvalds 已提交
75
{
76
	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
77
	struct net_device *real_dev = vlan->real_dev;
78
	struct vlan_info *vlan_info;
L
Linus Torvalds 已提交
79
	struct vlan_group *grp;
80
	u16 vlan_id = vlan->vlan_id;
L
Linus Torvalds 已提交
81 82

	ASSERT_RTNL();
83

84 85 86 87
	vlan_info = rtnl_dereference(real_dev->vlan_info);
	BUG_ON(!vlan_info);

	grp = &vlan_info->grp;
88 89

	/* Take it out of our own structures, but be sure to interlock with
90 91
	 * HW accelerating devices or SW vlan input packet processing if
	 * VLAN is not 0 (leave it there for 802.1p).
92
	 */
93 94
	if (vlan_id)
		vlan_vid_del(real_dev, vlan_id);
L
Linus Torvalds 已提交
95

96
	grp->nr_vlan_devs--;
97

E
Eric Dumazet 已提交
98 99 100
	if (vlan->flags & VLAN_FLAG_GVRP)
		vlan_gvrp_request_leave(dev);

101
	vlan_group_set_device(grp, vlan_id, NULL);
102 103 104 105
	/* Because unregister_netdevice_queue() makes sure at least one rcu
	 * grace period is respected before device freeing,
	 * we dont need to call synchronize_net() here.
	 */
106
	unregister_netdevice_queue(dev, head);
107

J
Jiri Pirko 已提交
108 109
	netdev_upper_dev_unlink(real_dev, dev);

110
	if (grp->nr_vlan_devs == 0)
P
Patrick McHardy 已提交
111 112
		vlan_gvrp_uninit_applicant(real_dev);

113 114
	/* Get rid of the vlan's reference to real_dev */
	dev_put(real_dev);
L
Linus Torvalds 已提交
115 116
}

117
int vlan_check_real_dev(struct net_device *real_dev, u16 vlan_id)
L
Linus Torvalds 已提交
118
{
119
	const char *name = real_dev->name;
120

L
Linus Torvalds 已提交
121
	if (real_dev->features & NETIF_F_VLAN_CHALLENGED) {
J
Joe Perches 已提交
122
		pr_info("VLANs not supported on %s\n", name);
P
Patrick McHardy 已提交
123
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
124 125
	}

126
	if (vlan_find_dev(real_dev, vlan_id) != NULL)
P
Patrick McHardy 已提交
127
		return -EEXIST;
L
Linus Torvalds 已提交
128

P
Patrick McHardy 已提交
129 130 131
	return 0;
}

P
Patrick McHardy 已提交
132
int register_vlan_dev(struct net_device *dev)
133
{
134
	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
135
	struct net_device *real_dev = vlan->real_dev;
136
	u16 vlan_id = vlan->vlan_id;
137 138
	struct vlan_info *vlan_info;
	struct vlan_group *grp;
139 140
	int err;

141 142 143 144 145 146 147 148 149 150
	err = vlan_vid_add(real_dev, vlan_id);
	if (err)
		return err;

	vlan_info = rtnl_dereference(real_dev->vlan_info);
	/* vlan_info should be there now. vlan_vid_add took care of it */
	BUG_ON(!vlan_info);

	grp = &vlan_info->grp;
	if (grp->nr_vlan_devs == 0) {
P
Patrick McHardy 已提交
151 152
		err = vlan_gvrp_init_applicant(real_dev);
		if (err < 0)
153
			goto out_vid_del;
154 155
	}

156 157
	err = vlan_group_prealloc_vid(grp, vlan_id);
	if (err < 0)
P
Patrick McHardy 已提交
158
		goto out_uninit_applicant;
159

J
Jiri Pirko 已提交
160 161 162 163
	err = netdev_upper_dev_link(real_dev, dev);
	if (err)
		goto out_uninit_applicant;

164 165
	err = register_netdevice(dev);
	if (err < 0)
J
Jiri Pirko 已提交
166
		goto out_upper_dev_unlink;
167

168
	/* Account for reference in struct vlan_dev_priv */
169 170
	dev_hold(real_dev);

171
	netif_stacked_transfer_operstate(real_dev, dev);
172 173 174 175 176 177
	linkwatch_fire_event(dev); /* _MUST_ call rfc2863_policy() */

	/* So, got the sucker initialized, now lets place
	 * it into our local structure.
	 */
	vlan_group_set_device(grp, vlan_id, dev);
178
	grp->nr_vlan_devs++;
179 180 181

	return 0;

J
Jiri Pirko 已提交
182 183
out_upper_dev_unlink:
	netdev_upper_dev_unlink(real_dev, dev);
P
Patrick McHardy 已提交
184
out_uninit_applicant:
185
	if (grp->nr_vlan_devs == 0)
P
Patrick McHardy 已提交
186
		vlan_gvrp_uninit_applicant(real_dev);
187 188
out_vid_del:
	vlan_vid_del(real_dev, vlan_id);
189 190 191
	return err;
}

P
Patrick McHardy 已提交
192
/*  Attach a VLAN device to a mac address (ie Ethernet Card).
193
 *  Returns 0 if the device was created or a negative error code otherwise.
P
Patrick McHardy 已提交
194
 */
195
static int register_vlan_device(struct net_device *real_dev, u16 vlan_id)
P
Patrick McHardy 已提交
196 197
{
	struct net_device *new_dev;
198 199
	struct net *net = dev_net(real_dev);
	struct vlan_net *vn = net_generic(net, vlan_net_id);
P
Patrick McHardy 已提交
200
	char name[IFNAMSIZ];
201
	int err;
P
Patrick McHardy 已提交
202

203
	if (vlan_id >= VLAN_VID_MASK)
204
		return -ERANGE;
P
Patrick McHardy 已提交
205

206
	err = vlan_check_real_dev(real_dev, vlan_id);
207 208
	if (err < 0)
		return err;
P
Patrick McHardy 已提交
209

L
Linus Torvalds 已提交
210
	/* Gotta set up the fields for the device. */
211
	switch (vn->name_type) {
L
Linus Torvalds 已提交
212 213
	case VLAN_NAME_TYPE_RAW_PLUS_VID:
		/* name will look like:	 eth1.0005 */
214
		snprintf(name, IFNAMSIZ, "%s.%.4i", real_dev->name, vlan_id);
L
Linus Torvalds 已提交
215 216 217 218 219
		break;
	case VLAN_NAME_TYPE_PLUS_VID_NO_PAD:
		/* Put our vlan.VID in the name.
		 * Name will look like:	 vlan5
		 */
220
		snprintf(name, IFNAMSIZ, "vlan%i", vlan_id);
L
Linus Torvalds 已提交
221 222 223 224 225
		break;
	case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD:
		/* Put our vlan.VID in the name.
		 * Name will look like:	 eth0.5
		 */
226
		snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, vlan_id);
L
Linus Torvalds 已提交
227 228 229 230 231 232
		break;
	case VLAN_NAME_TYPE_PLUS_VID:
		/* Put our vlan.VID in the name.
		 * Name will look like:	 vlan0005
		 */
	default:
233
		snprintf(name, IFNAMSIZ, "vlan%.4i", vlan_id);
234
	}
235

236
	new_dev = alloc_netdev(sizeof(struct vlan_dev_priv), name, vlan_setup);
237

L
Linus Torvalds 已提交
238
	if (new_dev == NULL)
239
		return -ENOBUFS;
L
Linus Torvalds 已提交
240

241
	dev_net_set(new_dev, net);
L
Linus Torvalds 已提交
242 243 244 245
	/* need 4 bytes for extra VLAN header info,
	 * hope the underlying device can handle it.
	 */
	new_dev->mtu = real_dev->mtu;
246
	new_dev->priv_flags |= (real_dev->priv_flags & IFF_UNICAST_FLT);
L
Linus Torvalds 已提交
247

248 249 250 251
	vlan_dev_priv(new_dev)->vlan_id = vlan_id;
	vlan_dev_priv(new_dev)->real_dev = real_dev;
	vlan_dev_priv(new_dev)->dent = NULL;
	vlan_dev_priv(new_dev)->flags = VLAN_FLAG_REORDER_HDR;
L
Linus Torvalds 已提交
252

P
Patrick McHardy 已提交
253
	new_dev->rtnl_link_ops = &vlan_link_ops;
254 255
	err = register_vlan_dev(new_dev);
	if (err < 0)
256
		goto out_free_newdev;
L
Linus Torvalds 已提交
257

258
	return 0;
L
Linus Torvalds 已提交
259 260 261

out_free_newdev:
	free_netdev(new_dev);
262
	return err;
L
Linus Torvalds 已提交
263 264
}

265 266 267
static void vlan_sync_address(struct net_device *dev,
			      struct net_device *vlandev)
{
268
	struct vlan_dev_priv *vlan = vlan_dev_priv(vlandev);
269 270

	/* May be called without an actual change */
271
	if (ether_addr_equal(vlan->real_dev_addr, dev->dev_addr))
272 273 274 275
		return;

	/* vlan address was different from the old address and is equal to
	 * the new address */
276 277
	if (!ether_addr_equal(vlandev->dev_addr, vlan->real_dev_addr) &&
	    ether_addr_equal(vlandev->dev_addr, dev->dev_addr))
278
		dev_uc_del(dev, vlandev->dev_addr);
279 280 281

	/* vlan address was equal to the old address and is different from
	 * the new address */
282 283
	if (ether_addr_equal(vlandev->dev_addr, vlan->real_dev_addr) &&
	    !ether_addr_equal(vlandev->dev_addr, dev->dev_addr))
284
		dev_uc_add(dev, vlandev->dev_addr);
285 286 287 288

	memcpy(vlan->real_dev_addr, dev->dev_addr, ETH_ALEN);
}

289 290 291
static void vlan_transfer_features(struct net_device *dev,
				   struct net_device *vlandev)
{
292
	vlandev->gso_max_size = dev->gso_max_size;
293 294 295 296 297 298

	if (dev->features & NETIF_F_HW_VLAN_TX)
		vlandev->hard_header_len = dev->hard_header_len;
	else
		vlandev->hard_header_len = dev->hard_header_len + VLAN_HLEN;

A
Amerigo Wang 已提交
299
#if IS_ENABLED(CONFIG_FCOE)
300 301
	vlandev->fcoe_ddp_xid = dev->fcoe_ddp_xid;
#endif
302 303

	netdev_update_features(vlandev);
304 305
}

306 307 308 309 310 311
static void __vlan_device_event(struct net_device *dev, unsigned long event)
{
	switch (event) {
	case NETDEV_CHANGENAME:
		vlan_proc_rem_dev(dev);
		if (vlan_proc_add_dev(dev) < 0)
J
Joe Perches 已提交
312 313
			pr_warn("failed to change proc name for %s\n",
				dev->name);
314
		break;
315 316
	case NETDEV_REGISTER:
		if (vlan_proc_add_dev(dev) < 0)
J
Joe Perches 已提交
317
			pr_warn("failed to add proc entry for %s\n", dev->name);
318 319 320 321
		break;
	case NETDEV_UNREGISTER:
		vlan_proc_rem_dev(dev);
		break;
322 323 324
	}
}

P
Patrick McHardy 已提交
325 326
static int vlan_device_event(struct notifier_block *unused, unsigned long event,
			     void *ptr)
L
Linus Torvalds 已提交
327 328
{
	struct net_device *dev = ptr;
329
	struct vlan_group *grp;
330
	struct vlan_info *vlan_info;
L
Linus Torvalds 已提交
331 332
	int i, flgs;
	struct net_device *vlandev;
333
	struct vlan_dev_priv *vlan;
334
	LIST_HEAD(list);
L
Linus Torvalds 已提交
335

336
	if (is_vlan_dev(dev))
337 338
		__vlan_device_event(dev, event);

339
	if ((event == NETDEV_UP) &&
340
	    (dev->features & NETIF_F_HW_VLAN_FILTER)) {
J
Joe Perches 已提交
341
		pr_info("adding VLAN 0 to HW filter on device %s\n",
342
			dev->name);
343
		vlan_vid_add(dev, 0);
344 345
	}

346 347
	vlan_info = rtnl_dereference(dev->vlan_info);
	if (!vlan_info)
L
Linus Torvalds 已提交
348
		goto out;
349
	grp = &vlan_info->grp;
L
Linus Torvalds 已提交
350 351 352 353 354 355 356 357

	/* It is OK that we do not hold the group lock right now,
	 * as we run under the RTNL lock.
	 */

	switch (event) {
	case NETDEV_CHANGE:
		/* Propagate real device state to vlan devices */
358
		for (i = 0; i < VLAN_N_VID; i++) {
D
Dan Aloni 已提交
359
			vlandev = vlan_group_get_device(grp, i);
L
Linus Torvalds 已提交
360 361 362
			if (!vlandev)
				continue;

363
			netif_stacked_transfer_operstate(dev, vlandev);
L
Linus Torvalds 已提交
364 365 366
		}
		break;

367 368
	case NETDEV_CHANGEADDR:
		/* Adjust unicast filters on underlying device */
369
		for (i = 0; i < VLAN_N_VID; i++) {
370 371 372 373
			vlandev = vlan_group_get_device(grp, i);
			if (!vlandev)
				continue;

374 375 376 377
			flgs = vlandev->flags;
			if (!(flgs & IFF_UP))
				continue;

378 379
			vlan_sync_address(dev, vlandev);
		}
380 381 382
		break;

	case NETDEV_CHANGEMTU:
383
		for (i = 0; i < VLAN_N_VID; i++) {
384 385 386 387 388 389 390 391 392
			vlandev = vlan_group_get_device(grp, i);
			if (!vlandev)
				continue;

			if (vlandev->mtu <= dev->mtu)
				continue;

			dev_set_mtu(vlandev, dev->mtu);
		}
393 394
		break;

395 396
	case NETDEV_FEAT_CHANGE:
		/* Propagate device features to underlying device */
397
		for (i = 0; i < VLAN_N_VID; i++) {
398 399 400 401 402 403 404 405 406
			vlandev = vlan_group_get_device(grp, i);
			if (!vlandev)
				continue;

			vlan_transfer_features(dev, vlandev);
		}

		break;

L
Linus Torvalds 已提交
407
	case NETDEV_DOWN:
408 409 410
		if (dev->features & NETIF_F_HW_VLAN_FILTER)
			vlan_vid_del(dev, 0);

L
Linus Torvalds 已提交
411
		/* Put all VLANs for this dev in the down state too.  */
412
		for (i = 0; i < VLAN_N_VID; i++) {
D
Dan Aloni 已提交
413
			vlandev = vlan_group_get_device(grp, i);
L
Linus Torvalds 已提交
414 415 416 417 418 419 420
			if (!vlandev)
				continue;

			flgs = vlandev->flags;
			if (!(flgs & IFF_UP))
				continue;

421
			vlan = vlan_dev_priv(vlandev);
422 423
			if (!(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
				dev_change_flags(vlandev, flgs & ~IFF_UP);
424
			netif_stacked_transfer_operstate(dev, vlandev);
L
Linus Torvalds 已提交
425 426 427 428 429
		}
		break;

	case NETDEV_UP:
		/* Put all VLANs for this dev in the up state too.  */
430
		for (i = 0; i < VLAN_N_VID; i++) {
D
Dan Aloni 已提交
431
			vlandev = vlan_group_get_device(grp, i);
L
Linus Torvalds 已提交
432 433
			if (!vlandev)
				continue;
434

L
Linus Torvalds 已提交
435 436 437 438
			flgs = vlandev->flags;
			if (flgs & IFF_UP)
				continue;

439
			vlan = vlan_dev_priv(vlandev);
440 441
			if (!(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
				dev_change_flags(vlandev, flgs | IFF_UP);
442
			netif_stacked_transfer_operstate(dev, vlandev);
L
Linus Torvalds 已提交
443 444
		}
		break;
445

L
Linus Torvalds 已提交
446
	case NETDEV_UNREGISTER:
447 448 449 450
		/* twiddle thumbs on netns device moves */
		if (dev->reg_state != NETREG_UNREGISTERING)
			break;

451
		for (i = 0; i < VLAN_N_VID; i++) {
452 453 454 455
			vlandev = vlan_group_get_device(grp, i);
			if (!vlandev)
				continue;

456
			/* removal of last vid destroys vlan_info, abort
457
			 * afterwards */
458
			if (vlan_info->nr_vids == 1)
459
				i = VLAN_N_VID;
460 461 462 463

			unregister_vlan_dev(vlandev, &list);
		}
		unregister_netdevice_many(&list);
L
Linus Torvalds 已提交
464
		break;
465 466 467

	case NETDEV_PRE_TYPE_CHANGE:
		/* Forbid underlaying device to change its type. */
468 469 470
		if (vlan_uses_dev(dev))
			return NOTIFY_BAD;
		break;
471 472

	case NETDEV_NOTIFY_PEERS:
473
	case NETDEV_BONDING_FAILOVER:
474 475 476 477 478 479
		/* Propagate to vlan devices */
		for (i = 0; i < VLAN_N_VID; i++) {
			vlandev = vlan_group_get_device(grp, i);
			if (!vlandev)
				continue;

480
			call_netdevice_notifiers(event, vlandev);
481 482
		}
		break;
483
	}
L
Linus Torvalds 已提交
484 485 486 487 488

out:
	return NOTIFY_DONE;
}

489 490 491 492
static struct notifier_block vlan_notifier_block __read_mostly = {
	.notifier_call = vlan_device_event,
};

L
Linus Torvalds 已提交
493 494 495 496 497
/*
 *	VLAN IOCTL handler.
 *	o execute requested action or pass command to the device driver
 *   arg is really a struct vlan_ioctl_args __user *.
 */
498
static int vlan_ioctl_handler(struct net *net, void __user *arg)
L
Linus Torvalds 已提交
499
{
500
	int err;
L
Linus Torvalds 已提交
501
	struct vlan_ioctl_args args;
502
	struct net_device *dev = NULL;
L
Linus Torvalds 已提交
503 504 505 506 507 508 509 510

	if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args)))
		return -EFAULT;

	/* Null terminate this sucker, just in case. */
	args.device1[23] = 0;
	args.u.device2[23] = 0;

511 512
	rtnl_lock();

L
Linus Torvalds 已提交
513 514
	switch (args.cmd) {
	case SET_VLAN_INGRESS_PRIORITY_CMD:
515 516 517 518 519 520 521
	case SET_VLAN_EGRESS_PRIORITY_CMD:
	case SET_VLAN_FLAG_CMD:
	case ADD_VLAN_CMD:
	case DEL_VLAN_CMD:
	case GET_VLAN_REALDEV_NAME_CMD:
	case GET_VLAN_VID_CMD:
		err = -ENODEV;
522
		dev = __dev_get_by_name(net, args.device1);
523 524 525 526
		if (!dev)
			goto out;

		err = -EINVAL;
J
Joonwoo Park 已提交
527
		if (args.cmd != ADD_VLAN_CMD && !is_vlan_dev(dev))
528 529 530 531 532 533
			goto out;
	}

	switch (args.cmd) {
	case SET_VLAN_INGRESS_PRIORITY_CMD:
		err = -EPERM;
534
		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
535 536 537 538
			break;
		vlan_dev_set_ingress_priority(dev,
					      args.u.skb_priority,
					      args.vlan_qos);
539
		err = 0;
L
Linus Torvalds 已提交
540 541 542
		break;

	case SET_VLAN_EGRESS_PRIORITY_CMD:
543
		err = -EPERM;
544
		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
545 546
			break;
		err = vlan_dev_set_egress_priority(dev,
L
Linus Torvalds 已提交
547 548 549 550 551
						   args.u.skb_priority,
						   args.vlan_qos);
		break;

	case SET_VLAN_FLAG_CMD:
552
		err = -EPERM;
553
		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
554
			break;
555 556 557
		err = vlan_dev_change_flags(dev,
					    args.vlan_qos ? args.u.flag : 0,
					    args.u.flag);
L
Linus Torvalds 已提交
558 559 560
		break;

	case SET_VLAN_NAME_TYPE_CMD:
561
		err = -EPERM;
562
		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
563
			break;
564 565
		if ((args.u.name_type >= 0) &&
		    (args.u.name_type < VLAN_NAME_TYPE_HIGHEST)) {
566 567 568 569
			struct vlan_net *vn;

			vn = net_generic(net, vlan_net_id);
			vn->name_type = args.u.name_type;
L
Linus Torvalds 已提交
570 571 572 573 574 575 576
			err = 0;
		} else {
			err = -EINVAL;
		}
		break;

	case ADD_VLAN_CMD:
577
		err = -EPERM;
578
		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
579
			break;
580
		err = register_vlan_device(dev, args.u.VID);
L
Linus Torvalds 已提交
581 582 583
		break;

	case DEL_VLAN_CMD:
584
		err = -EPERM;
585
		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
586
			break;
587
		unregister_vlan_dev(dev, NULL);
588
		err = 0;
L
Linus Torvalds 已提交
589 590 591
		break;

	case GET_VLAN_REALDEV_NAME_CMD:
592
		err = 0;
593
		vlan_dev_get_realdev_name(dev, args.u.device2);
L
Linus Torvalds 已提交
594
		if (copy_to_user(arg, &args,
P
Patrick McHardy 已提交
595
				 sizeof(struct vlan_ioctl_args)))
L
Linus Torvalds 已提交
596 597 598 599
			err = -EFAULT;
		break;

	case GET_VLAN_VID_CMD:
600
		err = 0;
601
		args.u.VID = vlan_dev_vlan_id(dev);
L
Linus Torvalds 已提交
602
		if (copy_to_user(arg, &args,
P
Patrick McHardy 已提交
603
				 sizeof(struct vlan_ioctl_args)))
604
		      err = -EFAULT;
L
Linus Torvalds 已提交
605 606 607
		break;

	default:
608
		err = -EOPNOTSUPP;
609
		break;
610
	}
611
out:
612
	rtnl_unlock();
L
Linus Torvalds 已提交
613 614 615
	return err;
}

616
static int __net_init vlan_init_net(struct net *net)
617
{
618
	struct vlan_net *vn = net_generic(net, vlan_net_id);
619 620
	int err;

621 622
	vn->name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;

623 624
	err = vlan_proc_init(net);

625 626 627
	return err;
}

628
static void __net_exit vlan_exit_net(struct net *net)
629
{
630
	vlan_proc_cleanup(net);
631 632 633 634 635
}

static struct pernet_operations vlan_net_ops = {
	.init = vlan_init_net,
	.exit = vlan_exit_net,
636 637
	.id   = &vlan_net_id,
	.size = sizeof(struct vlan_net),
638 639
};

640 641 642 643
static int __init vlan_proto_init(void)
{
	int err;

644
	pr_info("%s v%s\n", vlan_fullname, vlan_version);
645

646
	err = register_pernet_subsys(&vlan_net_ops);
647 648 649
	if (err < 0)
		goto err0;

650 651 652 653
	err = register_netdevice_notifier(&vlan_notifier_block);
	if (err < 0)
		goto err2;

P
Patrick McHardy 已提交
654
	err = vlan_gvrp_init();
655 656 657
	if (err < 0)
		goto err3;

P
Patrick McHardy 已提交
658 659 660 661
	err = vlan_netlink_init();
	if (err < 0)
		goto err4;

662 663 664
	vlan_ioctl_set(vlan_ioctl_handler);
	return 0;

P
Patrick McHardy 已提交
665 666
err4:
	vlan_gvrp_uninit();
667 668 669
err3:
	unregister_netdevice_notifier(&vlan_notifier_block);
err2:
670
	unregister_pernet_subsys(&vlan_net_ops);
671
err0:
672 673 674 675 676 677 678 679 680 681
	return err;
}

static void __exit vlan_cleanup_module(void)
{
	vlan_ioctl_set(NULL);
	vlan_netlink_fini();

	unregister_netdevice_notifier(&vlan_notifier_block);

682
	unregister_pernet_subsys(&vlan_net_ops);
683
	rcu_barrier(); /* Wait for completion of call_rcu()'s */
P
Patrick McHardy 已提交
684 685

	vlan_gvrp_uninit();
686 687 688 689 690
}

module_init(vlan_proto_init);
module_exit(vlan_cleanup_module);

L
Linus Torvalds 已提交
691 692
MODULE_LICENSE("GPL");
MODULE_VERSION(DRV_VERSION);