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