vlan.c 16.1 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

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

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

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

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

	if ((real_dev->features & NETIF_F_HW_VLAN_FILTER) &&
126
	    (!ops->ndo_vlan_rx_add_vid || !ops->ndo_vlan_rx_kill_vid)) {
J
Joe Perches 已提交
127
		pr_info("Device %s has buggy VLAN hw accel\n", name);
P
Patrick McHardy 已提交
128
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
129 130
	}

131
	if (vlan_find_dev(real_dev, vlan_id) != NULL)
P
Patrick McHardy 已提交
132
		return -EEXIST;
L
Linus Torvalds 已提交
133

P
Patrick McHardy 已提交
134 135 136
	return 0;
}

P
Patrick McHardy 已提交
137
int register_vlan_dev(struct net_device *dev)
138
{
139
	struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
140
	struct net_device *real_dev = vlan->real_dev;
141
	u16 vlan_id = vlan->vlan_id;
142 143
	struct vlan_info *vlan_info;
	struct vlan_group *grp;
144 145
	int err;

146 147 148 149 150 151 152 153 154 155
	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 已提交
156 157
		err = vlan_gvrp_init_applicant(real_dev);
		if (err < 0)
158
			goto out_vid_del;
159 160
	}

161 162
	err = vlan_group_prealloc_vid(grp, vlan_id);
	if (err < 0)
P
Patrick McHardy 已提交
163
		goto out_uninit_applicant;
164

165 166
	err = register_netdevice(dev);
	if (err < 0)
P
Patrick McHardy 已提交
167
		goto out_uninit_applicant;
168

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

172
	netif_stacked_transfer_operstate(real_dev, dev);
173 174 175 176 177 178
	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);
179
	grp->nr_vlan_devs++;
180 181 182

	return 0;

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

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

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

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

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

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

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

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

247 248 249 250
	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 已提交
251

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

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

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

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

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

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

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

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

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

	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 已提交
298
#if IS_ENABLED(CONFIG_FCOE)
299 300
	vlandev->fcoe_ddp_xid = dev->fcoe_ddp_xid;
#endif
301 302

	netdev_update_features(vlandev);
303 304
}

305 306 307 308 309 310
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 已提交
311 312
			pr_warn("failed to change proc name for %s\n",
				dev->name);
313
		break;
314 315
	case NETDEV_REGISTER:
		if (vlan_proc_add_dev(dev) < 0)
J
Joe Perches 已提交
316
			pr_warn("failed to add proc entry for %s\n", dev->name);
317 318 319 320
		break;
	case NETDEV_UNREGISTER:
		vlan_proc_rem_dev(dev);
		break;
321 322 323
	}
}

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

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

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

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

	/* 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 */
357
		for (i = 0; i < VLAN_N_VID; i++) {
D
Dan Aloni 已提交
358
			vlandev = vlan_group_get_device(grp, i);
L
Linus Torvalds 已提交
359 360 361
			if (!vlandev)
				continue;

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

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

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

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

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

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

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

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

			vlan_transfer_features(dev, vlandev);
		}

		break;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

out:
	return NOTIFY_DONE;
}

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

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

	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;

510 511
	rtnl_lock();

L
Linus Torvalds 已提交
512 513
	switch (args.cmd) {
	case SET_VLAN_INGRESS_PRIORITY_CMD:
514 515 516 517 518 519 520
	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;
521
		dev = __dev_get_by_name(net, args.device1);
522 523 524 525
		if (!dev)
			goto out;

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

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

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

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

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

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

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

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

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

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

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

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

620 621
	vn->name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;

622 623
	err = vlan_proc_init(net);

624 625 626
	return err;
}

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

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

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

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

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

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

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

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

661 662 663
	vlan_ioctl_set(vlan_ioctl_handler);
	return 0;

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

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

	unregister_netdevice_notifier(&vlan_notifier_block);

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

	vlan_gvrp_uninit();
685 686 687 688 689
}

module_init(vlan_proto_init);
module_exit(vlan_cleanup_module);

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