vlan.c 16.9 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.
 */

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

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

#define DRV_VERSION "1.8"

/* Global VLAN variables */

45
int vlan_net_id __read_mostly;
46

47 48 49 50
const char vlan_fullname[] = "802.1Q VLAN Support";
const char vlan_version[] = DRV_VERSION;
static const char vlan_copyright[] = "Ben Greear <greearb@candelatech.com>";
static const char vlan_buggyright[] = "David S. Miller <davem@redhat.com>";
L
Linus Torvalds 已提交
51 52 53

/* End of global variables definitions. */

D
Dan Aloni 已提交
54 55 56 57
static void vlan_group_free(struct vlan_group *grp)
{
	int i;

P
Patrick McHardy 已提交
58
	for (i = 0; i < VLAN_GROUP_ARRAY_SPLIT_PARTS; i++)
D
Dan Aloni 已提交
59 60 61 62
		kfree(grp->vlan_devices_arrays[i]);
	kfree(grp);
}

63
static struct vlan_group *vlan_group_alloc(struct net_device *real_dev)
64 65 66 67 68 69 70
{
	struct vlan_group *grp;

	grp = kzalloc(sizeof(struct vlan_group), GFP_KERNEL);
	if (!grp)
		return NULL;

71
	grp->real_dev = real_dev;
72
	return grp;
73
}
74

75
static int vlan_group_prealloc_vid(struct vlan_group *vg, u16 vlan_id)
76 77 78 79 80 81
{
	struct net_device **array;
	unsigned int size;

	ASSERT_RTNL();

82
	array = vg->vlan_devices_arrays[vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
83 84 85 86 87 88 89 90
	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;

91
	vg->vlan_devices_arrays[vlan_id / VLAN_GROUP_ARRAY_PART_LEN] = array;
92
	return 0;
93 94
}

L
Linus Torvalds 已提交
95 96
static void vlan_rcu_free(struct rcu_head *rcu)
{
D
Dan Aloni 已提交
97
	vlan_group_free(container_of(rcu, struct vlan_group, rcu));
L
Linus Torvalds 已提交
98 99
}

100
void unregister_vlan_dev(struct net_device *dev, struct list_head *head)
L
Linus Torvalds 已提交
101
{
102
	struct vlan_dev_info *vlan = vlan_dev_info(dev);
103
	struct net_device *real_dev = vlan->real_dev;
104
	const struct net_device_ops *ops = real_dev->netdev_ops;
L
Linus Torvalds 已提交
105
	struct vlan_group *grp;
106
	u16 vlan_id = vlan->vlan_id;
L
Linus Torvalds 已提交
107 108

	ASSERT_RTNL();
109

E
Eric Dumazet 已提交
110
	grp = rtnl_dereference(real_dev->vlgrp);
111
	BUG_ON(!grp);
112 113

	/* Take it out of our own structures, but be sure to interlock with
114 115
	 * HW accelerating devices or SW vlan input packet processing if
	 * VLAN is not 0 (leave it there for 802.1p).
116
	 */
117
	if (vlan_id && (real_dev->features & NETIF_F_HW_VLAN_FILTER))
118
		ops->ndo_vlan_rx_kill_vid(real_dev, vlan_id);
L
Linus Torvalds 已提交
119

120
	grp->nr_vlans--;
121

122 123
	vlan_group_set_device(grp, vlan_id, NULL);
	if (!grp->killall)
124
		synchronize_net();
125

126
	unregister_netdevice_queue(dev, head);
127

128
	/* If the group is now empty, kill off the group. */
129
	if (grp->nr_vlans == 0) {
P
Patrick McHardy 已提交
130 131
		vlan_gvrp_uninit_applicant(real_dev);

132
		rcu_assign_pointer(real_dev->vlgrp, NULL);
133
		if (ops->ndo_vlan_rx_register)
134
			ops->ndo_vlan_rx_register(real_dev, NULL);
135 136 137

		/* Free the group, after all cpu's are done. */
		call_rcu(&grp->rcu, vlan_rcu_free);
L
Linus Torvalds 已提交
138 139
	}

140 141
	/* Get rid of the vlan's reference to real_dev */
	dev_put(real_dev);
L
Linus Torvalds 已提交
142 143
}

144
int vlan_check_real_dev(struct net_device *real_dev, u16 vlan_id)
L
Linus Torvalds 已提交
145
{
146 147
	const char *name = real_dev->name;
	const struct net_device_ops *ops = real_dev->netdev_ops;
148

L
Linus Torvalds 已提交
149
	if (real_dev->features & NETIF_F_VLAN_CHALLENGED) {
150
		pr_info("8021q: VLANs not supported on %s\n", name);
P
Patrick McHardy 已提交
151
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
152 153 154
	}

	if ((real_dev->features & NETIF_F_HW_VLAN_FILTER) &&
155
	    (!ops->ndo_vlan_rx_add_vid || !ops->ndo_vlan_rx_kill_vid)) {
156
		pr_info("8021q: Device %s has buggy VLAN hw accel\n", name);
P
Patrick McHardy 已提交
157
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
158 159
	}

160
	if (vlan_find_dev(real_dev, vlan_id) != NULL)
P
Patrick McHardy 已提交
161
		return -EEXIST;
L
Linus Torvalds 已提交
162

P
Patrick McHardy 已提交
163 164 165
	return 0;
}

P
Patrick McHardy 已提交
166
int register_vlan_dev(struct net_device *dev)
167
{
168
	struct vlan_dev_info *vlan = vlan_dev_info(dev);
169
	struct net_device *real_dev = vlan->real_dev;
170
	const struct net_device_ops *ops = real_dev->netdev_ops;
171
	u16 vlan_id = vlan->vlan_id;
172 173 174
	struct vlan_group *grp, *ngrp = NULL;
	int err;

E
Eric Dumazet 已提交
175
	grp = rtnl_dereference(real_dev->vlgrp);
176
	if (!grp) {
177
		ngrp = grp = vlan_group_alloc(real_dev);
178 179
		if (!grp)
			return -ENOBUFS;
P
Patrick McHardy 已提交
180 181 182
		err = vlan_gvrp_init_applicant(real_dev);
		if (err < 0)
			goto out_free_group;
183 184
	}

185 186
	err = vlan_group_prealloc_vid(grp, vlan_id);
	if (err < 0)
P
Patrick McHardy 已提交
187
		goto out_uninit_applicant;
188

189 190
	err = register_netdevice(dev);
	if (err < 0)
P
Patrick McHardy 已提交
191
		goto out_uninit_applicant;
192 193 194 195

	/* Account for reference in struct vlan_dev_info */
	dev_hold(real_dev);

196
	netif_stacked_transfer_operstate(real_dev, dev);
197 198 199 200 201 202
	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);
203 204
	grp->nr_vlans++;

205
	if (ngrp) {
206
		if (ops->ndo_vlan_rx_register)
207 208 209
			ops->ndo_vlan_rx_register(real_dev, ngrp);
		rcu_assign_pointer(real_dev->vlgrp, ngrp);
	}
210
	if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
211
		ops->ndo_vlan_rx_add_vid(real_dev, vlan_id);
212 213 214

	return 0;

P
Patrick McHardy 已提交
215 216 217
out_uninit_applicant:
	if (ngrp)
		vlan_gvrp_uninit_applicant(real_dev);
218
out_free_group:
219 220 221 222
	if (ngrp) {
		/* Free the group, after all cpu's are done. */
		call_rcu(&ngrp->rcu, vlan_rcu_free);
	}
223 224 225
	return err;
}

P
Patrick McHardy 已提交
226
/*  Attach a VLAN device to a mac address (ie Ethernet Card).
227
 *  Returns 0 if the device was created or a negative error code otherwise.
P
Patrick McHardy 已提交
228
 */
229
static int register_vlan_device(struct net_device *real_dev, u16 vlan_id)
P
Patrick McHardy 已提交
230 231
{
	struct net_device *new_dev;
232 233
	struct net *net = dev_net(real_dev);
	struct vlan_net *vn = net_generic(net, vlan_net_id);
P
Patrick McHardy 已提交
234
	char name[IFNAMSIZ];
235
	int err;
P
Patrick McHardy 已提交
236

237
	if (vlan_id >= VLAN_VID_MASK)
238
		return -ERANGE;
P
Patrick McHardy 已提交
239

240
	err = vlan_check_real_dev(real_dev, vlan_id);
241 242
	if (err < 0)
		return err;
P
Patrick McHardy 已提交
243

L
Linus Torvalds 已提交
244
	/* Gotta set up the fields for the device. */
245
	switch (vn->name_type) {
L
Linus Torvalds 已提交
246 247
	case VLAN_NAME_TYPE_RAW_PLUS_VID:
		/* name will look like:	 eth1.0005 */
248
		snprintf(name, IFNAMSIZ, "%s.%.4i", real_dev->name, vlan_id);
L
Linus Torvalds 已提交
249 250 251 252 253
		break;
	case VLAN_NAME_TYPE_PLUS_VID_NO_PAD:
		/* Put our vlan.VID in the name.
		 * Name will look like:	 vlan5
		 */
254
		snprintf(name, IFNAMSIZ, "vlan%i", vlan_id);
L
Linus Torvalds 已提交
255 256 257 258 259
		break;
	case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD:
		/* Put our vlan.VID in the name.
		 * Name will look like:	 eth0.5
		 */
260
		snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, vlan_id);
L
Linus Torvalds 已提交
261 262 263 264 265 266
		break;
	case VLAN_NAME_TYPE_PLUS_VID:
		/* Put our vlan.VID in the name.
		 * Name will look like:	 vlan0005
		 */
	default:
267
		snprintf(name, IFNAMSIZ, "vlan%.4i", vlan_id);
268
	}
269

E
Eric Dumazet 已提交
270
	new_dev = alloc_netdev(sizeof(struct vlan_dev_info), name, vlan_setup);
271

L
Linus Torvalds 已提交
272
	if (new_dev == NULL)
273
		return -ENOBUFS;
L
Linus Torvalds 已提交
274

275
	dev_net_set(new_dev, net);
L
Linus Torvalds 已提交
276 277 278 279 280
	/* need 4 bytes for extra VLAN header info,
	 * hope the underlying device can handle it.
	 */
	new_dev->mtu = real_dev->mtu;

281
	vlan_dev_info(new_dev)->vlan_id = vlan_id;
282 283 284
	vlan_dev_info(new_dev)->real_dev = real_dev;
	vlan_dev_info(new_dev)->dent = NULL;
	vlan_dev_info(new_dev)->flags = VLAN_FLAG_REORDER_HDR;
L
Linus Torvalds 已提交
285

P
Patrick McHardy 已提交
286
	new_dev->rtnl_link_ops = &vlan_link_ops;
287 288
	err = register_vlan_dev(new_dev);
	if (err < 0)
289
		goto out_free_newdev;
L
Linus Torvalds 已提交
290

291
	return 0;
L
Linus Torvalds 已提交
292 293 294

out_free_newdev:
	free_netdev(new_dev);
295
	return err;
L
Linus Torvalds 已提交
296 297
}

298 299 300
static void vlan_sync_address(struct net_device *dev,
			      struct net_device *vlandev)
{
301
	struct vlan_dev_info *vlan = vlan_dev_info(vlandev);
302 303 304 305 306 307 308 309 310

	/* May be called without an actual change */
	if (!compare_ether_addr(vlan->real_dev_addr, dev->dev_addr))
		return;

	/* vlan address was different from the old address and is equal to
	 * the new address */
	if (compare_ether_addr(vlandev->dev_addr, vlan->real_dev_addr) &&
	    !compare_ether_addr(vlandev->dev_addr, dev->dev_addr))
311
		dev_uc_del(dev, vlandev->dev_addr);
312 313 314 315 316

	/* vlan address was equal to the old address and is different from
	 * the new address */
	if (!compare_ether_addr(vlandev->dev_addr, vlan->real_dev_addr) &&
	    compare_ether_addr(vlandev->dev_addr, dev->dev_addr))
317
		dev_uc_add(dev, vlandev->dev_addr);
318 319 320 321

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

322 323 324
static void vlan_transfer_features(struct net_device *dev,
				   struct net_device *vlandev)
{
325
	vlandev->gso_max_size = dev->gso_max_size;
326 327 328 329 330 331

	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;

332 333 334
#if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
	vlandev->fcoe_ddp_xid = dev->fcoe_ddp_xid;
#endif
335 336

	netdev_update_features(vlandev);
337 338
}

339 340 341 342 343 344 345 346 347
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)
			pr_warning("8021q: failed to change proc name for %s\n",
					dev->name);
		break;
348 349 350 351 352 353 354 355
	case NETDEV_REGISTER:
		if (vlan_proc_add_dev(dev) < 0)
			pr_warning("8021q: failed to add proc entry for %s\n",
					dev->name);
		break;
	case NETDEV_UNREGISTER:
		vlan_proc_rem_dev(dev);
		break;
356 357 358
	}
}

P
Patrick McHardy 已提交
359 360
static int vlan_device_event(struct notifier_block *unused, unsigned long event,
			     void *ptr)
L
Linus Torvalds 已提交
361 362
{
	struct net_device *dev = ptr;
363
	struct vlan_group *grp;
L
Linus Torvalds 已提交
364 365
	int i, flgs;
	struct net_device *vlandev;
366
	struct vlan_dev_info *vlan;
367
	LIST_HEAD(list);
L
Linus Torvalds 已提交
368

369
	if (is_vlan_dev(dev))
370 371
		__vlan_device_event(dev, event);

372 373 374 375 376 377 378 379
	if ((event == NETDEV_UP) &&
	    (dev->features & NETIF_F_HW_VLAN_FILTER) &&
	    dev->netdev_ops->ndo_vlan_rx_add_vid) {
		pr_info("8021q: adding VLAN 0 to HW filter on device %s\n",
			dev->name);
		dev->netdev_ops->ndo_vlan_rx_add_vid(dev, 0);
	}

E
Eric Dumazet 已提交
380
	grp = rtnl_dereference(dev->vlgrp);
L
Linus Torvalds 已提交
381 382 383 384 385 386 387 388 389 390
	if (!grp)
		goto out;

	/* 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 */
391
		for (i = 0; i < VLAN_N_VID; i++) {
D
Dan Aloni 已提交
392
			vlandev = vlan_group_get_device(grp, i);
L
Linus Torvalds 已提交
393 394 395
			if (!vlandev)
				continue;

396
			netif_stacked_transfer_operstate(dev, vlandev);
L
Linus Torvalds 已提交
397 398 399
		}
		break;

400 401
	case NETDEV_CHANGEADDR:
		/* Adjust unicast filters on underlying device */
402
		for (i = 0; i < VLAN_N_VID; i++) {
403 404 405 406
			vlandev = vlan_group_get_device(grp, i);
			if (!vlandev)
				continue;

407 408 409 410
			flgs = vlandev->flags;
			if (!(flgs & IFF_UP))
				continue;

411 412
			vlan_sync_address(dev, vlandev);
		}
413 414 415
		break;

	case NETDEV_CHANGEMTU:
416
		for (i = 0; i < VLAN_N_VID; i++) {
417 418 419 420 421 422 423 424 425
			vlandev = vlan_group_get_device(grp, i);
			if (!vlandev)
				continue;

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

			dev_set_mtu(vlandev, dev->mtu);
		}
426 427
		break;

428 429
	case NETDEV_FEAT_CHANGE:
		/* Propagate device features to underlying device */
430
		for (i = 0; i < VLAN_N_VID; i++) {
431 432 433 434 435 436 437 438 439
			vlandev = vlan_group_get_device(grp, i);
			if (!vlandev)
				continue;

			vlan_transfer_features(dev, vlandev);
		}

		break;

L
Linus Torvalds 已提交
440 441
	case NETDEV_DOWN:
		/* Put all VLANs for this dev in the down state too.  */
442
		for (i = 0; i < VLAN_N_VID; i++) {
D
Dan Aloni 已提交
443
			vlandev = vlan_group_get_device(grp, i);
L
Linus Torvalds 已提交
444 445 446 447 448 449 450
			if (!vlandev)
				continue;

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

451 452 453
			vlan = vlan_dev_info(vlandev);
			if (!(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
				dev_change_flags(vlandev, flgs & ~IFF_UP);
454
			netif_stacked_transfer_operstate(dev, vlandev);
L
Linus Torvalds 已提交
455 456 457 458 459
		}
		break;

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

L
Linus Torvalds 已提交
465 466 467 468
			flgs = vlandev->flags;
			if (flgs & IFF_UP)
				continue;

469 470 471
			vlan = vlan_dev_info(vlandev);
			if (!(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
				dev_change_flags(vlandev, flgs | IFF_UP);
472
			netif_stacked_transfer_operstate(dev, vlandev);
L
Linus Torvalds 已提交
473 474
		}
		break;
475

L
Linus Torvalds 已提交
476
	case NETDEV_UNREGISTER:
477 478 479 480
		/* twiddle thumbs on netns device moves */
		if (dev->reg_state != NETREG_UNREGISTERING)
			break;

481 482 483
		/* Delete all VLANs for this dev. */
		grp->killall = 1;

484
		for (i = 0; i < VLAN_N_VID; i++) {
485 486 487 488 489 490 491
			vlandev = vlan_group_get_device(grp, i);
			if (!vlandev)
				continue;

			/* unregistration of last vlan destroys group, abort
			 * afterwards */
			if (grp->nr_vlans == 1)
492
				i = VLAN_N_VID;
493 494 495 496

			unregister_vlan_dev(vlandev, &list);
		}
		unregister_netdevice_many(&list);
L
Linus Torvalds 已提交
497
		break;
498 499 500 501

	case NETDEV_PRE_TYPE_CHANGE:
		/* Forbid underlaying device to change its type. */
		return NOTIFY_BAD;
502 503 504 505 506 507 508 509 510 511 512

	case NETDEV_NOTIFY_PEERS:
		/* Propagate to vlan devices */
		for (i = 0; i < VLAN_N_VID; i++) {
			vlandev = vlan_group_get_device(grp, i);
			if (!vlandev)
				continue;

			call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, vlandev);
		}
		break;
513
	}
L
Linus Torvalds 已提交
514 515 516 517 518

out:
	return NOTIFY_DONE;
}

519 520 521 522
static struct notifier_block vlan_notifier_block __read_mostly = {
	.notifier_call = vlan_device_event,
};

L
Linus Torvalds 已提交
523 524 525 526 527
/*
 *	VLAN IOCTL handler.
 *	o execute requested action or pass command to the device driver
 *   arg is really a struct vlan_ioctl_args __user *.
 */
528
static int vlan_ioctl_handler(struct net *net, void __user *arg)
L
Linus Torvalds 已提交
529
{
530
	int err;
L
Linus Torvalds 已提交
531
	struct vlan_ioctl_args args;
532
	struct net_device *dev = NULL;
L
Linus Torvalds 已提交
533 534 535 536 537 538 539 540

	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;

541 542
	rtnl_lock();

L
Linus Torvalds 已提交
543 544
	switch (args.cmd) {
	case SET_VLAN_INGRESS_PRIORITY_CMD:
545 546 547 548 549 550 551
	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;
552
		dev = __dev_get_by_name(net, args.device1);
553 554 555 556
		if (!dev)
			goto out;

		err = -EINVAL;
J
Joonwoo Park 已提交
557
		if (args.cmd != ADD_VLAN_CMD && !is_vlan_dev(dev))
558 559 560 561 562 563
			goto out;
	}

	switch (args.cmd) {
	case SET_VLAN_INGRESS_PRIORITY_CMD:
		err = -EPERM;
L
Linus Torvalds 已提交
564
		if (!capable(CAP_NET_ADMIN))
565 566 567 568
			break;
		vlan_dev_set_ingress_priority(dev,
					      args.u.skb_priority,
					      args.vlan_qos);
569
		err = 0;
L
Linus Torvalds 已提交
570 571 572
		break;

	case SET_VLAN_EGRESS_PRIORITY_CMD:
573
		err = -EPERM;
L
Linus Torvalds 已提交
574
		if (!capable(CAP_NET_ADMIN))
575 576
			break;
		err = vlan_dev_set_egress_priority(dev,
L
Linus Torvalds 已提交
577 578 579 580 581
						   args.u.skb_priority,
						   args.vlan_qos);
		break;

	case SET_VLAN_FLAG_CMD:
582
		err = -EPERM;
L
Linus Torvalds 已提交
583
		if (!capable(CAP_NET_ADMIN))
584
			break;
585 586 587
		err = vlan_dev_change_flags(dev,
					    args.vlan_qos ? args.u.flag : 0,
					    args.u.flag);
L
Linus Torvalds 已提交
588 589 590
		break;

	case SET_VLAN_NAME_TYPE_CMD:
591
		err = -EPERM;
L
Linus Torvalds 已提交
592
		if (!capable(CAP_NET_ADMIN))
593
			break;
594 595
		if ((args.u.name_type >= 0) &&
		    (args.u.name_type < VLAN_NAME_TYPE_HIGHEST)) {
596 597 598 599
			struct vlan_net *vn;

			vn = net_generic(net, vlan_net_id);
			vn->name_type = args.u.name_type;
L
Linus Torvalds 已提交
600 601 602 603 604 605 606
			err = 0;
		} else {
			err = -EINVAL;
		}
		break;

	case ADD_VLAN_CMD:
607
		err = -EPERM;
L
Linus Torvalds 已提交
608
		if (!capable(CAP_NET_ADMIN))
609
			break;
610
		err = register_vlan_device(dev, args.u.VID);
L
Linus Torvalds 已提交
611 612 613
		break;

	case DEL_VLAN_CMD:
614
		err = -EPERM;
L
Linus Torvalds 已提交
615
		if (!capable(CAP_NET_ADMIN))
616
			break;
617
		unregister_vlan_dev(dev, NULL);
618
		err = 0;
L
Linus Torvalds 已提交
619 620 621
		break;

	case GET_VLAN_REALDEV_NAME_CMD:
622
		err = 0;
623
		vlan_dev_get_realdev_name(dev, args.u.device2);
L
Linus Torvalds 已提交
624
		if (copy_to_user(arg, &args,
P
Patrick McHardy 已提交
625
				 sizeof(struct vlan_ioctl_args)))
L
Linus Torvalds 已提交
626 627 628 629
			err = -EFAULT;
		break;

	case GET_VLAN_VID_CMD:
630
		err = 0;
631
		args.u.VID = vlan_dev_vlan_id(dev);
L
Linus Torvalds 已提交
632
		if (copy_to_user(arg, &args,
P
Patrick McHardy 已提交
633
				 sizeof(struct vlan_ioctl_args)))
634
		      err = -EFAULT;
L
Linus Torvalds 已提交
635 636 637
		break;

	default:
638
		err = -EOPNOTSUPP;
639
		break;
640
	}
641
out:
642
	rtnl_unlock();
L
Linus Torvalds 已提交
643 644 645
	return err;
}

646
static int __net_init vlan_init_net(struct net *net)
647
{
648
	struct vlan_net *vn = net_generic(net, vlan_net_id);
649 650
	int err;

651 652
	vn->name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;

653 654
	err = vlan_proc_init(net);

655 656 657
	return err;
}

658
static void __net_exit vlan_exit_net(struct net *net)
659
{
660
	vlan_proc_cleanup(net);
661 662 663 664 665
}

static struct pernet_operations vlan_net_ops = {
	.init = vlan_init_net,
	.exit = vlan_exit_net,
666 667
	.id   = &vlan_net_id,
	.size = sizeof(struct vlan_net),
668 669
};

670 671 672 673 674 675 676
static int __init vlan_proto_init(void)
{
	int err;

	pr_info("%s v%s %s\n", vlan_fullname, vlan_version, vlan_copyright);
	pr_info("All bugs added by %s\n", vlan_buggyright);

677
	err = register_pernet_subsys(&vlan_net_ops);
678 679 680
	if (err < 0)
		goto err0;

681 682 683 684
	err = register_netdevice_notifier(&vlan_notifier_block);
	if (err < 0)
		goto err2;

P
Patrick McHardy 已提交
685
	err = vlan_gvrp_init();
686 687 688
	if (err < 0)
		goto err3;

P
Patrick McHardy 已提交
689 690 691 692
	err = vlan_netlink_init();
	if (err < 0)
		goto err4;

693 694 695
	vlan_ioctl_set(vlan_ioctl_handler);
	return 0;

P
Patrick McHardy 已提交
696 697
err4:
	vlan_gvrp_uninit();
698 699 700
err3:
	unregister_netdevice_notifier(&vlan_notifier_block);
err2:
701
	unregister_pernet_subsys(&vlan_net_ops);
702
err0:
703 704 705 706 707 708 709 710 711 712
	return err;
}

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

	unregister_netdevice_notifier(&vlan_notifier_block);

713
	unregister_pernet_subsys(&vlan_net_ops);
714
	rcu_barrier(); /* Wait for completion of call_rcu()'s */
P
Patrick McHardy 已提交
715 716

	vlan_gvrp_uninit();
717 718 719 720 721
}

module_init(vlan_proto_init);
module_exit(vlan_cleanup_module);

L
Linus Torvalds 已提交
722 723
MODULE_LICENSE("GPL");
MODULE_VERSION(DRV_VERSION);