]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/8021q/vlan.c
vlan: Optimize multiple unregistration
[mirror_ubuntu-bionic-kernel.git] / net / 8021q / vlan.c
CommitLineData
1da177e4
LT
1/*
2 * INET 802.1Q VLAN
3 * Ethernet-type device handling.
4 *
5 * Authors: Ben Greear <greearb@candelatech.com>
ad712087 6 * Please send support related email to: netdev@vger.kernel.org
1da177e4 7 * VLAN Home Page: http://www.candelatech.com/~greear/vlan.html
122952fc 8 *
1da177e4
LT
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
4fc268d2 21#include <linux/capability.h>
1da177e4
LT
22#include <linux/module.h>
23#include <linux/netdevice.h>
24#include <linux/skbuff.h>
1da177e4 25#include <linux/init.h>
82524746 26#include <linux/rculist.h>
1da177e4
LT
27#include <net/p8022.h>
28#include <net/arp.h>
29#include <linux/rtnetlink.h>
30#include <linux/notifier.h>
61362766 31#include <net/rtnetlink.h>
e9dc8653 32#include <net/net_namespace.h>
d9ed0f0e 33#include <net/netns/generic.h>
61362766 34#include <asm/uaccess.h>
1da177e4
LT
35
36#include <linux/if_vlan.h>
37#include "vlan.h"
38#include "vlanproc.h"
39
40#define DRV_VERSION "1.8"
41
42/* Global VLAN variables */
43
d9ed0f0e
PE
44int vlan_net_id;
45
1da177e4
LT
46/* Our listing of VLAN group(s) */
47static struct hlist_head vlan_group_hash[VLAN_GRP_HASH_SIZE];
1da177e4 48
b3020061
SH
49const char vlan_fullname[] = "802.1Q VLAN Support";
50const char vlan_version[] = DRV_VERSION;
51static const char vlan_copyright[] = "Ben Greear <greearb@candelatech.com>";
52static const char vlan_buggyright[] = "David S. Miller <davem@redhat.com>";
1da177e4 53
7546dd97 54static struct packet_type vlan_packet_type __read_mostly = {
09640e63 55 .type = cpu_to_be16(ETH_P_8021Q),
1da177e4
LT
56 .func = vlan_skb_recv, /* VLAN receive method */
57};
58
1da177e4
LT
59/* End of global variables definitions. */
60
2029cc2c
PM
61static inline unsigned int vlan_grp_hashfn(unsigned int idx)
62{
63 return ((idx >> VLAN_GRP_HASH_SHIFT) ^ idx) & VLAN_GRP_HASH_MASK;
64}
65
1da177e4 66/* Must be invoked with RCU read lock (no preempt) */
a9fde260 67static struct vlan_group *__vlan_find_group(struct net_device *real_dev)
1da177e4
LT
68{
69 struct vlan_group *grp;
70 struct hlist_node *n;
a9fde260 71 int hash = vlan_grp_hashfn(real_dev->ifindex);
1da177e4
LT
72
73 hlist_for_each_entry_rcu(grp, n, &vlan_group_hash[hash], hlist) {
a9fde260 74 if (grp->real_dev == real_dev)
1da177e4
LT
75 return grp;
76 }
77
78 return NULL;
79}
80
81/* Find the protocol handler. Assumes VID < VLAN_VID_MASK.
82 *
83 * Must be invoked with RCU read lock (no preempt)
84 */
9bb8582e 85struct net_device *__find_vlan_dev(struct net_device *real_dev, u16 vlan_id)
1da177e4 86{
a9fde260 87 struct vlan_group *grp = __vlan_find_group(real_dev);
1da177e4
LT
88
89 if (grp)
9bb8582e 90 return vlan_group_get_device(grp, vlan_id);
1da177e4
LT
91
92 return NULL;
93}
94
5c15bdec
DA
95static void vlan_group_free(struct vlan_group *grp)
96{
97 int i;
98
2029cc2c 99 for (i = 0; i < VLAN_GROUP_ARRAY_SPLIT_PARTS; i++)
5c15bdec
DA
100 kfree(grp->vlan_devices_arrays[i]);
101 kfree(grp);
102}
103
a9fde260 104static struct vlan_group *vlan_group_alloc(struct net_device *real_dev)
42429aae
PM
105{
106 struct vlan_group *grp;
42429aae
PM
107
108 grp = kzalloc(sizeof(struct vlan_group), GFP_KERNEL);
109 if (!grp)
110 return NULL;
111
a9fde260 112 grp->real_dev = real_dev;
42429aae 113 hlist_add_head_rcu(&grp->hlist,
a9fde260 114 &vlan_group_hash[vlan_grp_hashfn(real_dev->ifindex)]);
42429aae 115 return grp;
67727184 116}
42429aae 117
9bb8582e 118static int vlan_group_prealloc_vid(struct vlan_group *vg, u16 vlan_id)
67727184
PE
119{
120 struct net_device **array;
121 unsigned int size;
122
123 ASSERT_RTNL();
124
9bb8582e 125 array = vg->vlan_devices_arrays[vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
67727184
PE
126 if (array != NULL)
127 return 0;
128
129 size = sizeof(struct net_device *) * VLAN_GROUP_ARRAY_PART_LEN;
130 array = kzalloc(size, GFP_KERNEL);
131 if (array == NULL)
132 return -ENOBUFS;
133
9bb8582e 134 vg->vlan_devices_arrays[vlan_id / VLAN_GROUP_ARRAY_PART_LEN] = array;
67727184 135 return 0;
42429aae
PM
136}
137
1da177e4
LT
138static void vlan_rcu_free(struct rcu_head *rcu)
139{
5c15bdec 140 vlan_group_free(container_of(rcu, struct vlan_group, rcu));
1da177e4
LT
141}
142
23289a37 143void unregister_vlan_dev(struct net_device *dev, struct list_head *head)
1da177e4 144{
9dfebcc6 145 struct vlan_dev_info *vlan = vlan_dev_info(dev);
af301517 146 struct net_device *real_dev = vlan->real_dev;
656299f7 147 const struct net_device_ops *ops = real_dev->netdev_ops;
1da177e4 148 struct vlan_group *grp;
9bb8582e 149 u16 vlan_id = vlan->vlan_id;
1da177e4
LT
150
151 ASSERT_RTNL();
acc5efbc 152
a9fde260 153 grp = __vlan_find_group(real_dev);
af301517 154 BUG_ON(!grp);
acc5efbc 155
acc5efbc
PM
156 /* Take it out of our own structures, but be sure to interlock with
157 * HW accelerating devices or SW vlan input packet processing.
158 */
159 if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
656299f7 160 ops->ndo_vlan_rx_kill_vid(real_dev, vlan_id);
1da177e4 161
af301517 162 grp->nr_vlans--;
acc5efbc 163
63c8099d
ED
164 if (!grp->killall) {
165 vlan_group_set_device(grp, vlan_id, NULL);
166 synchronize_net();
167 }
23289a37 168 unregister_netdevice_queue(dev, head);
ce305002 169
acc5efbc 170 /* If the group is now empty, kill off the group. */
af301517 171 if (grp->nr_vlans == 0) {
70c03b49
PM
172 vlan_gvrp_uninit_applicant(real_dev);
173
acc5efbc 174 if (real_dev->features & NETIF_F_HW_VLAN_RX)
656299f7 175 ops->ndo_vlan_rx_register(real_dev, NULL);
acc5efbc
PM
176
177 hlist_del_rcu(&grp->hlist);
1da177e4 178
acc5efbc
PM
179 /* Free the group, after all cpu's are done. */
180 call_rcu(&grp->rcu, vlan_rcu_free);
1da177e4
LT
181 }
182
af301517
PM
183 /* Get rid of the vlan's reference to real_dev */
184 dev_put(real_dev);
1da177e4
LT
185}
186
63c8099d
ED
187void unregister_vlan_dev_alls(struct vlan_group *grp)
188{
189 LIST_HEAD(list);
190 int i;
191 struct net_device *vlandev;
192 struct vlan_group save;
193
194 memcpy(&save, grp, sizeof(save));
195 memset(&grp->vlan_devices_arrays, 0, sizeof(grp->vlan_devices_arrays));
196 grp->killall = 1;
197
198 synchronize_net();
199
200 /* Delete all VLANs for this dev. */
201 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
202 vlandev = vlan_group_get_device(&save, i);
203 if (!vlandev)
204 continue;
205
206 unregister_vlan_dev(vlandev, &list);
207 if (grp->nr_vlans == 0)
208 break;
209 }
210 unregister_netdevice_many(&list);
211 for (i = 0; i < VLAN_GROUP_ARRAY_SPLIT_PARTS; i++)
212 kfree(save.vlan_devices_arrays[i]);
213}
214
2029cc2c
PM
215static void vlan_transfer_operstate(const struct net_device *dev,
216 struct net_device *vlandev)
ddd7bf9f
SR
217{
218 /* Have to respect userspace enforced dormant state
219 * of real device, also must allow supplicant running
220 * on VLAN device
221 */
222 if (dev->operstate == IF_OPER_DORMANT)
223 netif_dormant_on(vlandev);
224 else
225 netif_dormant_off(vlandev);
226
227 if (netif_carrier_ok(dev)) {
228 if (!netif_carrier_ok(vlandev))
229 netif_carrier_on(vlandev);
230 } else {
231 if (netif_carrier_ok(vlandev))
232 netif_carrier_off(vlandev);
233 }
234}
235
9bb8582e 236int vlan_check_real_dev(struct net_device *real_dev, u16 vlan_id)
1da177e4 237{
656299f7
SH
238 const char *name = real_dev->name;
239 const struct net_device_ops *ops = real_dev->netdev_ops;
40f98e1a 240
1da177e4 241 if (real_dev->features & NETIF_F_VLAN_CHALLENGED) {
40f98e1a 242 pr_info("8021q: VLANs not supported on %s\n", name);
c1d3ee99 243 return -EOPNOTSUPP;
1da177e4
LT
244 }
245
656299f7 246 if ((real_dev->features & NETIF_F_HW_VLAN_RX) && !ops->ndo_vlan_rx_register) {
40f98e1a 247 pr_info("8021q: device %s has buggy VLAN hw accel\n", name);
c1d3ee99 248 return -EOPNOTSUPP;
1da177e4
LT
249 }
250
251 if ((real_dev->features & NETIF_F_HW_VLAN_FILTER) &&
656299f7 252 (!ops->ndo_vlan_rx_add_vid || !ops->ndo_vlan_rx_kill_vid)) {
40f98e1a 253 pr_info("8021q: Device %s has buggy VLAN hw accel\n", name);
c1d3ee99 254 return -EOPNOTSUPP;
1da177e4
LT
255 }
256
40f98e1a 257 if (__find_vlan_dev(real_dev, vlan_id) != NULL)
c1d3ee99 258 return -EEXIST;
1da177e4 259
c1d3ee99
PM
260 return 0;
261}
262
07b5b17e 263int register_vlan_dev(struct net_device *dev)
e89fe42c 264{
9dfebcc6 265 struct vlan_dev_info *vlan = vlan_dev_info(dev);
e89fe42c 266 struct net_device *real_dev = vlan->real_dev;
656299f7 267 const struct net_device_ops *ops = real_dev->netdev_ops;
9bb8582e 268 u16 vlan_id = vlan->vlan_id;
e89fe42c
PM
269 struct vlan_group *grp, *ngrp = NULL;
270 int err;
271
a9fde260 272 grp = __vlan_find_group(real_dev);
e89fe42c 273 if (!grp) {
a9fde260 274 ngrp = grp = vlan_group_alloc(real_dev);
e89fe42c
PM
275 if (!grp)
276 return -ENOBUFS;
70c03b49
PM
277 err = vlan_gvrp_init_applicant(real_dev);
278 if (err < 0)
279 goto out_free_group;
e89fe42c
PM
280 }
281
67727184
PE
282 err = vlan_group_prealloc_vid(grp, vlan_id);
283 if (err < 0)
70c03b49 284 goto out_uninit_applicant;
67727184 285
e89fe42c
PM
286 err = register_netdevice(dev);
287 if (err < 0)
70c03b49 288 goto out_uninit_applicant;
e89fe42c
PM
289
290 /* Account for reference in struct vlan_dev_info */
291 dev_hold(real_dev);
292
293 vlan_transfer_operstate(real_dev, dev);
294 linkwatch_fire_event(dev); /* _MUST_ call rfc2863_policy() */
295
296 /* So, got the sucker initialized, now lets place
297 * it into our local structure.
298 */
299 vlan_group_set_device(grp, vlan_id, dev);
af301517
PM
300 grp->nr_vlans++;
301
e89fe42c 302 if (ngrp && real_dev->features & NETIF_F_HW_VLAN_RX)
656299f7 303 ops->ndo_vlan_rx_register(real_dev, ngrp);
e89fe42c 304 if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
656299f7 305 ops->ndo_vlan_rx_add_vid(real_dev, vlan_id);
e89fe42c 306
e89fe42c
PM
307 return 0;
308
70c03b49
PM
309out_uninit_applicant:
310 if (ngrp)
311 vlan_gvrp_uninit_applicant(real_dev);
e89fe42c
PM
312out_free_group:
313 if (ngrp)
314 vlan_group_free(ngrp);
315 return err;
316}
317
c1d3ee99 318/* Attach a VLAN device to a mac address (ie Ethernet Card).
2ae0bf69 319 * Returns 0 if the device was created or a negative error code otherwise.
c1d3ee99 320 */
9bb8582e 321static int register_vlan_device(struct net_device *real_dev, u16 vlan_id)
c1d3ee99 322{
c1d3ee99 323 struct net_device *new_dev;
7a17a2f7
PE
324 struct net *net = dev_net(real_dev);
325 struct vlan_net *vn = net_generic(net, vlan_net_id);
c1d3ee99 326 char name[IFNAMSIZ];
2ae0bf69 327 int err;
c1d3ee99 328
9bb8582e 329 if (vlan_id >= VLAN_VID_MASK)
2ae0bf69 330 return -ERANGE;
c1d3ee99 331
9bb8582e 332 err = vlan_check_real_dev(real_dev, vlan_id);
2ae0bf69
PM
333 if (err < 0)
334 return err;
c1d3ee99 335
1da177e4 336 /* Gotta set up the fields for the device. */
7a17a2f7 337 switch (vn->name_type) {
1da177e4
LT
338 case VLAN_NAME_TYPE_RAW_PLUS_VID:
339 /* name will look like: eth1.0005 */
9bb8582e 340 snprintf(name, IFNAMSIZ, "%s.%.4i", real_dev->name, vlan_id);
1da177e4
LT
341 break;
342 case VLAN_NAME_TYPE_PLUS_VID_NO_PAD:
343 /* Put our vlan.VID in the name.
344 * Name will look like: vlan5
345 */
9bb8582e 346 snprintf(name, IFNAMSIZ, "vlan%i", vlan_id);
1da177e4
LT
347 break;
348 case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD:
349 /* Put our vlan.VID in the name.
350 * Name will look like: eth0.5
351 */
9bb8582e 352 snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, vlan_id);
1da177e4
LT
353 break;
354 case VLAN_NAME_TYPE_PLUS_VID:
355 /* Put our vlan.VID in the name.
356 * Name will look like: vlan0005
357 */
358 default:
9bb8582e 359 snprintf(name, IFNAMSIZ, "vlan%.4i", vlan_id);
3ff50b79 360 }
122952fc 361
2e59af3d
ED
362 new_dev = alloc_netdev_mq(sizeof(struct vlan_dev_info), name,
363 vlan_setup, real_dev->num_tx_queues);
5dd8d1e9 364
1da177e4 365 if (new_dev == NULL)
2ae0bf69 366 return -ENOBUFS;
1da177e4 367
2e59af3d 368 new_dev->real_num_tx_queues = real_dev->real_num_tx_queues;
65d292a2 369 dev_net_set(new_dev, net);
1da177e4
LT
370 /* need 4 bytes for extra VLAN header info,
371 * hope the underlying device can handle it.
372 */
373 new_dev->mtu = real_dev->mtu;
374
9bb8582e 375 vlan_dev_info(new_dev)->vlan_id = vlan_id;
9dfebcc6
PM
376 vlan_dev_info(new_dev)->real_dev = real_dev;
377 vlan_dev_info(new_dev)->dent = NULL;
378 vlan_dev_info(new_dev)->flags = VLAN_FLAG_REORDER_HDR;
1da177e4 379
07b5b17e 380 new_dev->rtnl_link_ops = &vlan_link_ops;
2ae0bf69
PM
381 err = register_vlan_dev(new_dev);
382 if (err < 0)
e89fe42c 383 goto out_free_newdev;
1da177e4 384
2ae0bf69 385 return 0;
1da177e4 386
1da177e4
LT
387out_free_newdev:
388 free_netdev(new_dev);
2ae0bf69 389 return err;
1da177e4
LT
390}
391
8c979c26
PM
392static void vlan_sync_address(struct net_device *dev,
393 struct net_device *vlandev)
394{
9dfebcc6 395 struct vlan_dev_info *vlan = vlan_dev_info(vlandev);
8c979c26
PM
396
397 /* May be called without an actual change */
398 if (!compare_ether_addr(vlan->real_dev_addr, dev->dev_addr))
399 return;
400
401 /* vlan address was different from the old address and is equal to
402 * the new address */
403 if (compare_ether_addr(vlandev->dev_addr, vlan->real_dev_addr) &&
404 !compare_ether_addr(vlandev->dev_addr, dev->dev_addr))
ccffad25 405 dev_unicast_delete(dev, vlandev->dev_addr);
8c979c26
PM
406
407 /* vlan address was equal to the old address and is different from
408 * the new address */
409 if (!compare_ether_addr(vlandev->dev_addr, vlan->real_dev_addr) &&
410 compare_ether_addr(vlandev->dev_addr, dev->dev_addr))
ccffad25 411 dev_unicast_add(dev, vlandev->dev_addr);
8c979c26
PM
412
413 memcpy(vlan->real_dev_addr, dev->dev_addr, ETH_ALEN);
414}
415
5fb13570
PM
416static void vlan_transfer_features(struct net_device *dev,
417 struct net_device *vlandev)
418{
419 unsigned long old_features = vlandev->features;
420
289c79a4
PM
421 vlandev->features &= ~dev->vlan_features;
422 vlandev->features |= dev->features & dev->vlan_features;
1ae4be22 423 vlandev->gso_max_size = dev->gso_max_size;
b85daa53
VD
424#if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
425 vlandev->fcoe_ddp_xid = dev->fcoe_ddp_xid;
426#endif
5fb13570
PM
427
428 if (old_features != vlandev->features)
429 netdev_features_change(vlandev);
430}
431
802fb176
PE
432static void __vlan_device_event(struct net_device *dev, unsigned long event)
433{
434 switch (event) {
435 case NETDEV_CHANGENAME:
436 vlan_proc_rem_dev(dev);
437 if (vlan_proc_add_dev(dev) < 0)
438 pr_warning("8021q: failed to change proc name for %s\n",
439 dev->name);
440 break;
30688a9a
PE
441 case NETDEV_REGISTER:
442 if (vlan_proc_add_dev(dev) < 0)
443 pr_warning("8021q: failed to add proc entry for %s\n",
444 dev->name);
445 break;
446 case NETDEV_UNREGISTER:
447 vlan_proc_rem_dev(dev);
448 break;
802fb176
PE
449 }
450}
451
2029cc2c
PM
452static int vlan_device_event(struct notifier_block *unused, unsigned long event,
453 void *ptr)
1da177e4
LT
454{
455 struct net_device *dev = ptr;
802fb176 456 struct vlan_group *grp;
1da177e4
LT
457 int i, flgs;
458 struct net_device *vlandev;
459
81d85346 460 if (is_vlan_dev(dev))
802fb176 461 __vlan_device_event(dev, event);
802fb176 462
a9fde260 463 grp = __vlan_find_group(dev);
1da177e4
LT
464 if (!grp)
465 goto out;
466
467 /* It is OK that we do not hold the group lock right now,
468 * as we run under the RTNL lock.
469 */
470
471 switch (event) {
472 case NETDEV_CHANGE:
473 /* Propagate real device state to vlan devices */
1da177e4 474 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
5c15bdec 475 vlandev = vlan_group_get_device(grp, i);
1da177e4
LT
476 if (!vlandev)
477 continue;
478
ddd7bf9f 479 vlan_transfer_operstate(dev, vlandev);
1da177e4
LT
480 }
481 break;
482
8c979c26
PM
483 case NETDEV_CHANGEADDR:
484 /* Adjust unicast filters on underlying device */
485 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
486 vlandev = vlan_group_get_device(grp, i);
487 if (!vlandev)
488 continue;
489
d932e04a
PM
490 flgs = vlandev->flags;
491 if (!(flgs & IFF_UP))
492 continue;
493
8c979c26
PM
494 vlan_sync_address(dev, vlandev);
495 }
2e477c9b
HX
496 break;
497
498 case NETDEV_CHANGEMTU:
499 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
500 vlandev = vlan_group_get_device(grp, i);
501 if (!vlandev)
502 continue;
503
504 if (vlandev->mtu <= dev->mtu)
505 continue;
506
507 dev_set_mtu(vlandev, dev->mtu);
508 }
8c979c26
PM
509 break;
510
5fb13570
PM
511 case NETDEV_FEAT_CHANGE:
512 /* Propagate device features to underlying device */
513 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
514 vlandev = vlan_group_get_device(grp, i);
515 if (!vlandev)
516 continue;
517
518 vlan_transfer_features(dev, vlandev);
519 }
520
521 break;
522
1da177e4
LT
523 case NETDEV_DOWN:
524 /* Put all VLANs for this dev in the down state too. */
525 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
5c15bdec 526 vlandev = vlan_group_get_device(grp, i);
1da177e4
LT
527 if (!vlandev)
528 continue;
529
530 flgs = vlandev->flags;
531 if (!(flgs & IFF_UP))
532 continue;
533
534 dev_change_flags(vlandev, flgs & ~IFF_UP);
adc667e8 535 vlan_transfer_operstate(dev, vlandev);
1da177e4
LT
536 }
537 break;
538
539 case NETDEV_UP:
540 /* Put all VLANs for this dev in the up state too. */
541 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
5c15bdec 542 vlandev = vlan_group_get_device(grp, i);
1da177e4
LT
543 if (!vlandev)
544 continue;
122952fc 545
1da177e4
LT
546 flgs = vlandev->flags;
547 if (flgs & IFF_UP)
548 continue;
549
550 dev_change_flags(vlandev, flgs | IFF_UP);
adc667e8 551 vlan_transfer_operstate(dev, vlandev);
1da177e4
LT
552 }
553 break;
122952fc 554
1da177e4 555 case NETDEV_UNREGISTER:
63c8099d 556 unregister_vlan_dev_alls(grp);
1da177e4 557 break;
3ff50b79 558 }
1da177e4
LT
559
560out:
561 return NOTIFY_DONE;
562}
563
69ab4b7d
PM
564static struct notifier_block vlan_notifier_block __read_mostly = {
565 .notifier_call = vlan_device_event,
566};
567
1da177e4
LT
568/*
569 * VLAN IOCTL handler.
570 * o execute requested action or pass command to the device driver
571 * arg is really a struct vlan_ioctl_args __user *.
572 */
881d966b 573static int vlan_ioctl_handler(struct net *net, void __user *arg)
1da177e4 574{
c17d8874 575 int err;
1da177e4 576 struct vlan_ioctl_args args;
c17d8874 577 struct net_device *dev = NULL;
1da177e4
LT
578
579 if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args)))
580 return -EFAULT;
581
582 /* Null terminate this sucker, just in case. */
583 args.device1[23] = 0;
584 args.u.device2[23] = 0;
585
c17d8874
PM
586 rtnl_lock();
587
1da177e4
LT
588 switch (args.cmd) {
589 case SET_VLAN_INGRESS_PRIORITY_CMD:
c17d8874
PM
590 case SET_VLAN_EGRESS_PRIORITY_CMD:
591 case SET_VLAN_FLAG_CMD:
592 case ADD_VLAN_CMD:
593 case DEL_VLAN_CMD:
594 case GET_VLAN_REALDEV_NAME_CMD:
595 case GET_VLAN_VID_CMD:
596 err = -ENODEV;
65d292a2 597 dev = __dev_get_by_name(net, args.device1);
c17d8874
PM
598 if (!dev)
599 goto out;
600
601 err = -EINVAL;
26a25239 602 if (args.cmd != ADD_VLAN_CMD && !is_vlan_dev(dev))
c17d8874
PM
603 goto out;
604 }
605
606 switch (args.cmd) {
607 case SET_VLAN_INGRESS_PRIORITY_CMD:
608 err = -EPERM;
1da177e4 609 if (!capable(CAP_NET_ADMIN))
c17d8874
PM
610 break;
611 vlan_dev_set_ingress_priority(dev,
612 args.u.skb_priority,
613 args.vlan_qos);
fffe470a 614 err = 0;
1da177e4
LT
615 break;
616
617 case SET_VLAN_EGRESS_PRIORITY_CMD:
c17d8874 618 err = -EPERM;
1da177e4 619 if (!capable(CAP_NET_ADMIN))
c17d8874
PM
620 break;
621 err = vlan_dev_set_egress_priority(dev,
1da177e4
LT
622 args.u.skb_priority,
623 args.vlan_qos);
624 break;
625
626 case SET_VLAN_FLAG_CMD:
c17d8874 627 err = -EPERM;
1da177e4 628 if (!capable(CAP_NET_ADMIN))
c17d8874 629 break;
b3ce0325
PM
630 err = vlan_dev_change_flags(dev,
631 args.vlan_qos ? args.u.flag : 0,
632 args.u.flag);
1da177e4
LT
633 break;
634
635 case SET_VLAN_NAME_TYPE_CMD:
c17d8874 636 err = -EPERM;
1da177e4 637 if (!capable(CAP_NET_ADMIN))
e35de026 638 break;
c17d8874
PM
639 if ((args.u.name_type >= 0) &&
640 (args.u.name_type < VLAN_NAME_TYPE_HIGHEST)) {
7a17a2f7
PE
641 struct vlan_net *vn;
642
643 vn = net_generic(net, vlan_net_id);
644 vn->name_type = args.u.name_type;
1da177e4
LT
645 err = 0;
646 } else {
647 err = -EINVAL;
648 }
649 break;
650
651 case ADD_VLAN_CMD:
c17d8874 652 err = -EPERM;
1da177e4 653 if (!capable(CAP_NET_ADMIN))
c17d8874 654 break;
2ae0bf69 655 err = register_vlan_device(dev, args.u.VID);
1da177e4
LT
656 break;
657
658 case DEL_VLAN_CMD:
c17d8874 659 err = -EPERM;
1da177e4 660 if (!capable(CAP_NET_ADMIN))
c17d8874 661 break;
23289a37 662 unregister_vlan_dev(dev, NULL);
af301517 663 err = 0;
1da177e4
LT
664 break;
665
1da177e4 666 case GET_VLAN_REALDEV_NAME_CMD:
3f5f4346 667 err = 0;
c17d8874 668 vlan_dev_get_realdev_name(dev, args.u.device2);
1da177e4 669 if (copy_to_user(arg, &args,
2029cc2c 670 sizeof(struct vlan_ioctl_args)))
1da177e4 671 err = -EFAULT;
1da177e4
LT
672 break;
673
674 case GET_VLAN_VID_CMD:
3f5f4346 675 err = 0;
22d1ba74 676 args.u.VID = vlan_dev_vlan_id(dev);
1da177e4 677 if (copy_to_user(arg, &args,
2029cc2c 678 sizeof(struct vlan_ioctl_args)))
122952fc 679 err = -EFAULT;
1da177e4
LT
680 break;
681
682 default:
198a291c 683 err = -EOPNOTSUPP;
c17d8874 684 break;
3ff50b79 685 }
7eb1b3d3 686out:
c17d8874 687 rtnl_unlock();
1da177e4
LT
688 return err;
689}
690
d9ed0f0e
PE
691static int vlan_init_net(struct net *net)
692{
693 int err;
694 struct vlan_net *vn;
695
696 err = -ENOMEM;
697 vn = kzalloc(sizeof(struct vlan_net), GFP_KERNEL);
698 if (vn == NULL)
699 goto err_alloc;
700
701 err = net_assign_generic(net, vlan_net_id, vn);
702 if (err < 0)
703 goto err_assign;
704
7a17a2f7
PE
705 vn->name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;
706
cd1c7014
PE
707 err = vlan_proc_init(net);
708 if (err < 0)
709 goto err_proc;
710
d9ed0f0e
PE
711 return 0;
712
cd1c7014
PE
713err_proc:
714 /* nothing */
d9ed0f0e
PE
715err_assign:
716 kfree(vn);
717err_alloc:
718 return err;
719}
720
721static void vlan_exit_net(struct net *net)
722{
723 struct vlan_net *vn;
724
725 vn = net_generic(net, vlan_net_id);
65d292a2 726 rtnl_kill_links(net, &vlan_link_ops);
cd1c7014 727 vlan_proc_cleanup(net);
d9ed0f0e
PE
728 kfree(vn);
729}
730
731static struct pernet_operations vlan_net_ops = {
732 .init = vlan_init_net,
733 .exit = vlan_exit_net,
734};
735
69ab4b7d
PM
736static int __init vlan_proto_init(void)
737{
738 int err;
739
740 pr_info("%s v%s %s\n", vlan_fullname, vlan_version, vlan_copyright);
741 pr_info("All bugs added by %s\n", vlan_buggyright);
742
d9ed0f0e
PE
743 err = register_pernet_gen_device(&vlan_net_id, &vlan_net_ops);
744 if (err < 0)
745 goto err0;
746
69ab4b7d
PM
747 err = register_netdevice_notifier(&vlan_notifier_block);
748 if (err < 0)
749 goto err2;
750
70c03b49 751 err = vlan_gvrp_init();
69ab4b7d
PM
752 if (err < 0)
753 goto err3;
754
70c03b49
PM
755 err = vlan_netlink_init();
756 if (err < 0)
757 goto err4;
758
69ab4b7d
PM
759 dev_add_pack(&vlan_packet_type);
760 vlan_ioctl_set(vlan_ioctl_handler);
761 return 0;
762
70c03b49
PM
763err4:
764 vlan_gvrp_uninit();
69ab4b7d
PM
765err3:
766 unregister_netdevice_notifier(&vlan_notifier_block);
767err2:
d9ed0f0e
PE
768 unregister_pernet_gen_device(vlan_net_id, &vlan_net_ops);
769err0:
69ab4b7d
PM
770 return err;
771}
772
773static void __exit vlan_cleanup_module(void)
774{
775 unsigned int i;
776
777 vlan_ioctl_set(NULL);
778 vlan_netlink_fini();
779
780 unregister_netdevice_notifier(&vlan_notifier_block);
781
782 dev_remove_pack(&vlan_packet_type);
783
784 /* This table must be empty if there are no module references left. */
785 for (i = 0; i < VLAN_GRP_HASH_SIZE; i++)
786 BUG_ON(!hlist_empty(&vlan_group_hash[i]));
787
d9ed0f0e 788 unregister_pernet_gen_device(vlan_net_id, &vlan_net_ops);
6e327c11 789 rcu_barrier(); /* Wait for completion of call_rcu()'s */
70c03b49
PM
790
791 vlan_gvrp_uninit();
69ab4b7d
PM
792}
793
794module_init(vlan_proto_init);
795module_exit(vlan_cleanup_module);
796
1da177e4
LT
797MODULE_LICENSE("GPL");
798MODULE_VERSION(DRV_VERSION);