blob: b463ba47024df2cb1737064592073cceb5648578 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +09008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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 */
Randy Dunlap4fc268d2006-01-11 12:17:47 -080022#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/* End of global variables definitions. */
71
72/*
73 * Function vlan_proto_init (pro)
74 *
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +090075 * Initialize VLAN protocol layer,
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 *
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) {
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +090090 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 "%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);
Patrick McHardy07b5b172007-06-13 12:07:54 -0700100 if (err < 0)
101 goto err1;
102
103 err = vlan_netlink_init();
104 if (err < 0)
105 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 vlan_ioctl_set(vlan_ioctl_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return 0;
Patrick McHardy07b5b172007-06-13 12:07:54 -0700109
110err2:
111 unregister_netdevice_notifier(&vlan_notifier_block);
112err1:
113 vlan_proc_cleanup();
114 dev_remove_pack(&vlan_packet_type);
115 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900118/* Cleanup all vlan devices
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 * Note: devices that have been registered that but not
120 * brought up will exist but have no module ref count.
121 */
122static void __exit vlan_cleanup_devices(void)
123{
124 struct net_device *dev, *nxt;
125
126 rtnl_lock();
Pavel Emelianov7562f872007-05-03 15:13:45 -0700127 for_each_netdev_safe(dev, nxt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (dev->priv_flags & IFF_802_1Q_VLAN) {
129 unregister_vlan_dev(VLAN_DEV_INFO(dev)->real_dev,
130 VLAN_DEV_INFO(dev)->vlan_id);
131
132 unregister_netdevice(dev);
133 }
134 }
135 rtnl_unlock();
136}
137
138/*
139 * Module 'remove' entry point.
140 * o delete /proc/net/router directory and static entries.
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900141 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142static void __exit vlan_cleanup_module(void)
143{
144 int i;
145
Patrick McHardy07b5b172007-06-13 12:07:54 -0700146 vlan_netlink_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 vlan_ioctl_set(NULL);
148
149 /* Un-register us from receiving netdevice events */
150 unregister_netdevice_notifier(&vlan_notifier_block);
151
152 dev_remove_pack(&vlan_packet_type);
153 vlan_cleanup_devices();
154
155 /* This table must be empty if there are no module
156 * references left.
157 */
158 for (i = 0; i < VLAN_GRP_HASH_SIZE; i++) {
159 BUG_ON(!hlist_empty(&vlan_group_hash[i]));
160 }
161 vlan_proc_cleanup();
162
163 synchronize_net();
164}
165
166module_init(vlan_proto_init);
167module_exit(vlan_cleanup_module);
168
169/* Must be invoked with RCU read lock (no preempt) */
170static struct vlan_group *__vlan_find_group(int real_dev_ifindex)
171{
172 struct vlan_group *grp;
173 struct hlist_node *n;
174 int hash = vlan_grp_hashfn(real_dev_ifindex);
175
176 hlist_for_each_entry_rcu(grp, n, &vlan_group_hash[hash], hlist) {
177 if (grp->real_dev_ifindex == real_dev_ifindex)
178 return grp;
179 }
180
181 return NULL;
182}
183
184/* Find the protocol handler. Assumes VID < VLAN_VID_MASK.
185 *
186 * Must be invoked with RCU read lock (no preempt)
187 */
188struct net_device *__find_vlan_dev(struct net_device *real_dev,
189 unsigned short VID)
190{
191 struct vlan_group *grp = __vlan_find_group(real_dev->ifindex);
192
193 if (grp)
Dan Aloni5c15bde2007-03-02 20:44:51 -0800194 return vlan_group_get_device(grp, VID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 return NULL;
197}
198
Dan Aloni5c15bde2007-03-02 20:44:51 -0800199static void vlan_group_free(struct vlan_group *grp)
200{
201 int i;
202
203 for (i=0; i < VLAN_GROUP_ARRAY_SPLIT_PARTS; i++)
204 kfree(grp->vlan_devices_arrays[i]);
205 kfree(grp);
206}
207
Patrick McHardy42429aa2007-06-13 12:05:59 -0700208static struct vlan_group *vlan_group_alloc(int ifindex)
209{
210 struct vlan_group *grp;
211 unsigned int size;
212 unsigned int i;
213
214 grp = kzalloc(sizeof(struct vlan_group), GFP_KERNEL);
215 if (!grp)
216 return NULL;
217
218 size = sizeof(struct net_device *) * VLAN_GROUP_ARRAY_PART_LEN;
219
220 for (i = 0; i < VLAN_GROUP_ARRAY_SPLIT_PARTS; i++) {
221 grp->vlan_devices_arrays[i] = kzalloc(size, GFP_KERNEL);
222 if (!grp->vlan_devices_arrays[i])
223 goto err;
224 }
225
226 grp->real_dev_ifindex = ifindex;
227 hlist_add_head_rcu(&grp->hlist,
228 &vlan_group_hash[vlan_grp_hashfn(ifindex)]);
229 return grp;
230
231err:
232 vlan_group_free(grp);
233 return NULL;
234}
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236static void vlan_rcu_free(struct rcu_head *rcu)
237{
Dan Aloni5c15bde2007-03-02 20:44:51 -0800238 vlan_group_free(container_of(rcu, struct vlan_group, rcu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
241
242/* This returns 0 if everything went fine.
243 * It will return 1 if the group was killed as a result.
244 * A negative return indicates failure.
245 *
246 * The RTNL lock must be held.
247 */
248static int unregister_vlan_dev(struct net_device *real_dev,
249 unsigned short vlan_id)
250{
251 struct net_device *dev = NULL;
252 int real_dev_ifindex = real_dev->ifindex;
253 struct vlan_group *grp;
254 int i, ret;
255
256#ifdef VLAN_DEBUG
257 printk(VLAN_DBG "%s: VID: %i\n", __FUNCTION__, vlan_id);
258#endif
259
260 /* sanity check */
261 if (vlan_id >= VLAN_VID_MASK)
262 return -EINVAL;
263
264 ASSERT_RTNL();
265 grp = __vlan_find_group(real_dev_ifindex);
266
267 ret = 0;
268
269 if (grp) {
Dan Aloni5c15bde2007-03-02 20:44:51 -0800270 dev = vlan_group_get_device(grp, vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (dev) {
272 /* Remove proc entry */
273 vlan_proc_rem_dev(dev);
274
275 /* Take it out of our own structures, but be sure to
276 * interlock with HW accelerating devices or SW vlan
277 * input packet processing.
278 */
Stephen Hemmingerd2d1acd2007-06-01 09:43:57 -0700279 if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 real_dev->vlan_rx_kill_vid(real_dev, vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Dan Aloni5c15bde2007-03-02 20:44:51 -0800282 vlan_group_set_device(grp, vlan_id, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 synchronize_net();
284
285
286 /* Caller unregisters (and if necessary, puts)
287 * VLAN device, but we get rid of the reference to
288 * real_dev here.
289 */
290 dev_put(real_dev);
291
292 /* If the group is now empty, kill off the
293 * group.
294 */
295 for (i = 0; i < VLAN_VID_MASK; i++)
Dan Aloni5c15bde2007-03-02 20:44:51 -0800296 if (vlan_group_get_device(grp, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 break;
298
299 if (i == VLAN_VID_MASK) {
300 if (real_dev->features & NETIF_F_HW_VLAN_RX)
301 real_dev->vlan_rx_register(real_dev, NULL);
302
303 hlist_del_rcu(&grp->hlist);
304
305 /* Free the group, after all cpu's are done. */
306 call_rcu(&grp->rcu, vlan_rcu_free);
307
308 grp = NULL;
309 ret = 1;
310 }
311 }
312 }
313
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900314 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Patrick McHardy07b5b172007-06-13 12:07:54 -0700317int unregister_vlan_device(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 int ret;
320
Patrick McHardyc17d8872007-06-13 12:05:22 -0700321 ret = unregister_vlan_dev(VLAN_DEV_INFO(dev)->real_dev,
322 VLAN_DEV_INFO(dev)->vlan_id);
323 unregister_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Patrick McHardyc17d8872007-06-13 12:05:22 -0700325 if (ret == 1)
326 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return ret;
328}
329
Patrick McHardy2f4284a2007-06-13 12:05:41 -0700330/*
331 * vlan network devices have devices nesting below it, and are a special
332 * "super class" of normal network devices; split their locks off into a
333 * separate class since they always nest.
334 */
335static struct lock_class_key vlan_netdev_xmit_lock_key;
336
337static int vlan_dev_init(struct net_device *dev)
338{
339 struct net_device *real_dev = VLAN_DEV_INFO(dev)->real_dev;
340
341 /* IFF_BROADCAST|IFF_MULTICAST; ??? */
342 dev->flags = real_dev->flags & ~IFF_UP;
343 dev->iflink = real_dev->ifindex;
344 dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
345 (1<<__LINK_STATE_DORMANT))) |
346 (1<<__LINK_STATE_PRESENT);
347
Patrick McHardy2f4284a2007-06-13 12:05:41 -0700348 memcpy(dev->broadcast, real_dev->broadcast, real_dev->addr_len);
349 memcpy(dev->dev_addr, real_dev->dev_addr, real_dev->addr_len);
Patrick McHardy2f4284a2007-06-13 12:05:41 -0700350
351 if (real_dev->features & NETIF_F_HW_VLAN_TX) {
352 dev->hard_header = real_dev->hard_header;
353 dev->hard_header_len = real_dev->hard_header_len;
354 dev->hard_start_xmit = vlan_dev_hwaccel_hard_start_xmit;
355 dev->rebuild_header = real_dev->rebuild_header;
356 } else {
357 dev->hard_header = vlan_dev_hard_header;
358 dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
359 dev->hard_start_xmit = vlan_dev_hard_start_xmit;
360 dev->rebuild_header = vlan_dev_rebuild_header;
361 }
362 dev->hard_header_parse = real_dev->hard_header_parse;
Patrick McHardy8c979c22007-07-11 19:45:24 -0700363 dev->hard_header_cache = NULL;
Patrick McHardy2f4284a2007-06-13 12:05:41 -0700364
365 lockdep_set_class(&dev->_xmit_lock, &vlan_netdev_xmit_lock_key);
366 return 0;
367}
368
Patrick McHardy07b5b172007-06-13 12:07:54 -0700369void vlan_setup(struct net_device *new_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 SET_MODULE_OWNER(new_dev);
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900372
Patrick McHardy8c979c22007-07-11 19:45:24 -0700373 ether_setup(new_dev);
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /* new_dev->ifindex = 0; it will be set when added to
376 * the global list.
377 * iflink is set as well.
378 */
379 new_dev->get_stats = vlan_dev_get_stats;
380
381 /* Make this thing known as a VLAN device */
382 new_dev->priv_flags |= IFF_802_1Q_VLAN;
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 /* Set us up to have no queue, as the underlying Hardware device
385 * can do all the queueing we could want.
386 */
387 new_dev->tx_queue_len = 0;
388
389 /* set up method calls */
390 new_dev->change_mtu = vlan_dev_change_mtu;
Patrick McHardy2f4284a2007-06-13 12:05:41 -0700391 new_dev->init = vlan_dev_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 new_dev->open = vlan_dev_open;
393 new_dev->stop = vlan_dev_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 new_dev->set_multicast_list = vlan_dev_set_multicast_list;
395 new_dev->destructor = free_netdev;
396 new_dev->do_ioctl = vlan_dev_ioctl;
397}
398
Stefan Rompfddd7bf92006-03-20 17:11:41 -0800399static void vlan_transfer_operstate(const struct net_device *dev, struct net_device *vlandev)
400{
401 /* Have to respect userspace enforced dormant state
402 * of real device, also must allow supplicant running
403 * on VLAN device
404 */
405 if (dev->operstate == IF_OPER_DORMANT)
406 netif_dormant_on(vlandev);
407 else
408 netif_dormant_off(vlandev);
409
410 if (netif_carrier_ok(dev)) {
411 if (!netif_carrier_ok(vlandev))
412 netif_carrier_on(vlandev);
413 } else {
414 if (netif_carrier_ok(vlandev))
415 netif_carrier_off(vlandev);
416 }
417}
418
Patrick McHardy07b5b172007-06-13 12:07:54 -0700419int vlan_check_real_dev(struct net_device *real_dev, unsigned short vlan_id)
Patrick McHardyc1d3ee92007-06-13 12:06:14 -0700420{
421 if (real_dev->features & NETIF_F_VLAN_CHALLENGED) {
422 printk(VLAN_DBG "%s: VLANs not supported on %s.\n",
423 __FUNCTION__, real_dev->name);
424 return -EOPNOTSUPP;
425 }
426
427 if ((real_dev->features & NETIF_F_HW_VLAN_RX) &&
428 !real_dev->vlan_rx_register) {
429 printk(VLAN_DBG "%s: Device %s has buggy VLAN hw accel.\n",
430 __FUNCTION__, real_dev->name);
431 return -EOPNOTSUPP;
432 }
433
434 if ((real_dev->features & NETIF_F_HW_VLAN_FILTER) &&
435 (!real_dev->vlan_rx_add_vid || !real_dev->vlan_rx_kill_vid)) {
436 printk(VLAN_DBG "%s: Device %s has buggy VLAN hw accel.\n",
437 __FUNCTION__, real_dev->name);
438 return -EOPNOTSUPP;
439 }
440
441 /* The real device must be up and operating in order to
442 * assosciate a VLAN device with it.
443 */
444 if (!(real_dev->flags & IFF_UP))
445 return -ENETDOWN;
446
447 if (__find_vlan_dev(real_dev, vlan_id) != NULL) {
448 /* was already registered. */
449 printk(VLAN_DBG "%s: ALREADY had VLAN registered\n", __FUNCTION__);
450 return -EEXIST;
451 }
452
453 return 0;
454}
455
Patrick McHardy07b5b172007-06-13 12:07:54 -0700456int register_vlan_dev(struct net_device *dev)
Patrick McHardye89fe422007-06-13 12:06:29 -0700457{
458 struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev);
459 struct net_device *real_dev = vlan->real_dev;
460 unsigned short vlan_id = vlan->vlan_id;
461 struct vlan_group *grp, *ngrp = NULL;
462 int err;
463
464 grp = __vlan_find_group(real_dev->ifindex);
465 if (!grp) {
466 ngrp = grp = vlan_group_alloc(real_dev->ifindex);
467 if (!grp)
468 return -ENOBUFS;
469 }
470
471 err = register_netdevice(dev);
472 if (err < 0)
473 goto out_free_group;
474
475 /* Account for reference in struct vlan_dev_info */
476 dev_hold(real_dev);
477
478 vlan_transfer_operstate(real_dev, dev);
479 linkwatch_fire_event(dev); /* _MUST_ call rfc2863_policy() */
480
481 /* So, got the sucker initialized, now lets place
482 * it into our local structure.
483 */
484 vlan_group_set_device(grp, vlan_id, dev);
485 if (ngrp && real_dev->features & NETIF_F_HW_VLAN_RX)
486 real_dev->vlan_rx_register(real_dev, ngrp);
487 if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
488 real_dev->vlan_rx_add_vid(real_dev, vlan_id);
489
490 if (vlan_proc_add_dev(dev) < 0)
491 printk(KERN_WARNING "VLAN: failed to add proc entry for %s\n",
492 dev->name);
493 return 0;
494
495out_free_group:
496 if (ngrp)
497 vlan_group_free(ngrp);
498 return err;
499}
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501/* Attach a VLAN device to a mac address (ie Ethernet Card).
Patrick McHardy2ae0bf62007-06-13 12:06:43 -0700502 * Returns 0 if the device was created or a negative error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 */
Patrick McHardy2ae0bf62007-06-13 12:06:43 -0700504static int register_vlan_device(struct net_device *real_dev,
505 unsigned short VLAN_ID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 struct net_device *new_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 char name[IFNAMSIZ];
Patrick McHardy2ae0bf62007-06-13 12:06:43 -0700509 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511#ifdef VLAN_DEBUG
512 printk(VLAN_DBG "%s: if_name -:%s:- vid: %i\n",
513 __FUNCTION__, eth_IF_name, VLAN_ID);
514#endif
515
516 if (VLAN_ID >= VLAN_VID_MASK)
Patrick McHardy2ae0bf62007-06-13 12:06:43 -0700517 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Patrick McHardy2ae0bf62007-06-13 12:06:43 -0700519 err = vlan_check_real_dev(real_dev, VLAN_ID);
520 if (err < 0)
521 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 /* Gotta set up the fields for the device. */
524#ifdef VLAN_DEBUG
525 printk(VLAN_DBG "About to allocate name, vlan_name_type: %i\n",
526 vlan_name_type);
527#endif
528 switch (vlan_name_type) {
529 case VLAN_NAME_TYPE_RAW_PLUS_VID:
530 /* name will look like: eth1.0005 */
531 snprintf(name, IFNAMSIZ, "%s.%.4i", real_dev->name, VLAN_ID);
532 break;
533 case VLAN_NAME_TYPE_PLUS_VID_NO_PAD:
534 /* Put our vlan.VID in the name.
535 * Name will look like: vlan5
536 */
537 snprintf(name, IFNAMSIZ, "vlan%i", VLAN_ID);
538 break;
539 case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD:
540 /* Put our vlan.VID in the name.
541 * Name will look like: eth0.5
542 */
543 snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, VLAN_ID);
544 break;
545 case VLAN_NAME_TYPE_PLUS_VID:
546 /* Put our vlan.VID in the name.
547 * Name will look like: vlan0005
548 */
549 default:
550 snprintf(name, IFNAMSIZ, "vlan%.4i", VLAN_ID);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700551 }
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 new_dev = alloc_netdev(sizeof(struct vlan_dev_info), name,
554 vlan_setup);
Arjan van de Ven5dd8d1e2006-07-03 00:25:33 -0700555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (new_dev == NULL)
Patrick McHardy2ae0bf62007-06-13 12:06:43 -0700557 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 /* need 4 bytes for extra VLAN header info,
560 * hope the underlying device can handle it.
561 */
562 new_dev->mtu = real_dev->mtu;
563
Patrick McHardy2f4284a2007-06-13 12:05:41 -0700564#ifdef VLAN_DEBUG
565 printk(VLAN_DBG "Allocated new name -:%s:-\n", new_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 VLAN_MEM_DBG("new_dev->priv malloc, addr: %p size: %i\n",
567 new_dev->priv,
568 sizeof(struct vlan_dev_info));
Patrick McHardy2f4284a2007-06-13 12:05:41 -0700569#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 VLAN_DEV_INFO(new_dev)->vlan_id = VLAN_ID; /* 1 through VLAN_VID_MASK */
572 VLAN_DEV_INFO(new_dev)->real_dev = real_dev;
573 VLAN_DEV_INFO(new_dev)->dent = NULL;
Patrick McHardya4bf3af42007-06-13 12:07:37 -0700574 VLAN_DEV_INFO(new_dev)->flags = VLAN_FLAG_REORDER_HDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Patrick McHardy07b5b172007-06-13 12:07:54 -0700576 new_dev->rtnl_link_ops = &vlan_link_ops;
Patrick McHardy2ae0bf62007-06-13 12:06:43 -0700577 err = register_vlan_dev(new_dev);
578 if (err < 0)
Patrick McHardye89fe422007-06-13 12:06:29 -0700579 goto out_free_newdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Patrick McHardyc17d8872007-06-13 12:05:22 -0700581 /* Account for reference in struct vlan_dev_info */
582 dev_hold(real_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583#ifdef VLAN_DEBUG
584 printk(VLAN_DBG "Allocated new device successfully, returning.\n");
585#endif
Patrick McHardy2ae0bf62007-06-13 12:06:43 -0700586 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588out_free_newdev:
589 free_netdev(new_dev);
Patrick McHardy2ae0bf62007-06-13 12:06:43 -0700590 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
Patrick McHardy8c979c22007-07-11 19:45:24 -0700593static void vlan_sync_address(struct net_device *dev,
594 struct net_device *vlandev)
595{
596 struct vlan_dev_info *vlan = VLAN_DEV_INFO(vlandev);
597
598 /* May be called without an actual change */
599 if (!compare_ether_addr(vlan->real_dev_addr, dev->dev_addr))
600 return;
601
602 /* vlan address was different from the old address and is equal to
603 * the new address */
604 if (compare_ether_addr(vlandev->dev_addr, vlan->real_dev_addr) &&
605 !compare_ether_addr(vlandev->dev_addr, dev->dev_addr))
606 dev_unicast_delete(dev, vlandev->dev_addr, ETH_ALEN);
607
608 /* vlan address was equal to the old address and is different from
609 * the new address */
610 if (!compare_ether_addr(vlandev->dev_addr, vlan->real_dev_addr) &&
611 compare_ether_addr(vlandev->dev_addr, dev->dev_addr))
612 dev_unicast_add(dev, vlandev->dev_addr, ETH_ALEN);
613
614 memcpy(vlan->real_dev_addr, dev->dev_addr, ETH_ALEN);
615}
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617static int vlan_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
618{
619 struct net_device *dev = ptr;
620 struct vlan_group *grp = __vlan_find_group(dev->ifindex);
621 int i, flgs;
622 struct net_device *vlandev;
623
624 if (!grp)
625 goto out;
626
627 /* It is OK that we do not hold the group lock right now,
628 * as we run under the RTNL lock.
629 */
630
631 switch (event) {
632 case NETDEV_CHANGE:
633 /* Propagate real device state to vlan devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
Dan Aloni5c15bde2007-03-02 20:44:51 -0800635 vlandev = vlan_group_get_device(grp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (!vlandev)
637 continue;
638
Stefan Rompfddd7bf92006-03-20 17:11:41 -0800639 vlan_transfer_operstate(dev, vlandev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641 break;
642
Patrick McHardy8c979c22007-07-11 19:45:24 -0700643 case NETDEV_CHANGEADDR:
644 /* Adjust unicast filters on underlying device */
645 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
646 vlandev = vlan_group_get_device(grp, i);
647 if (!vlandev)
648 continue;
649
650 vlan_sync_address(dev, vlandev);
651 }
652 break;
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 case NETDEV_DOWN:
655 /* Put all VLANs for this dev in the down state too. */
656 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
Dan Aloni5c15bde2007-03-02 20:44:51 -0800657 vlandev = vlan_group_get_device(grp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (!vlandev)
659 continue;
660
661 flgs = vlandev->flags;
662 if (!(flgs & IFF_UP))
663 continue;
664
665 dev_change_flags(vlandev, flgs & ~IFF_UP);
666 }
667 break;
668
669 case NETDEV_UP:
670 /* Put all VLANs for this dev in the up state too. */
671 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
Dan Aloni5c15bde2007-03-02 20:44:51 -0800672 vlandev = vlan_group_get_device(grp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (!vlandev)
674 continue;
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 flgs = vlandev->flags;
677 if (flgs & IFF_UP)
678 continue;
679
680 dev_change_flags(vlandev, flgs | IFF_UP);
681 }
682 break;
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 case NETDEV_UNREGISTER:
685 /* Delete all VLANs for this dev. */
686 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
687 int ret;
688
Dan Aloni5c15bde2007-03-02 20:44:51 -0800689 vlandev = vlan_group_get_device(grp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (!vlandev)
691 continue;
692
693 ret = unregister_vlan_dev(dev,
694 VLAN_DEV_INFO(vlandev)->vlan_id);
695
696 unregister_netdevice(vlandev);
697
698 /* Group was destroyed? */
699 if (ret == 1)
700 break;
701 }
702 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705out:
706 return NOTIFY_DONE;
707}
708
709/*
710 * VLAN IOCTL handler.
711 * o execute requested action or pass command to the device driver
712 * arg is really a struct vlan_ioctl_args __user *.
713 */
714static int vlan_ioctl_handler(void __user *arg)
715{
Patrick McHardyc17d8872007-06-13 12:05:22 -0700716 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 unsigned short vid = 0;
718 struct vlan_ioctl_args args;
Patrick McHardyc17d8872007-06-13 12:05:22 -0700719 struct net_device *dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args)))
722 return -EFAULT;
723
724 /* Null terminate this sucker, just in case. */
725 args.device1[23] = 0;
726 args.u.device2[23] = 0;
727
728#ifdef VLAN_DEBUG
729 printk(VLAN_DBG "%s: args.cmd: %x\n", __FUNCTION__, args.cmd);
730#endif
731
Patrick McHardyc17d8872007-06-13 12:05:22 -0700732 rtnl_lock();
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 switch (args.cmd) {
735 case SET_VLAN_INGRESS_PRIORITY_CMD:
Patrick McHardyc17d8872007-06-13 12:05:22 -0700736 case SET_VLAN_EGRESS_PRIORITY_CMD:
737 case SET_VLAN_FLAG_CMD:
738 case ADD_VLAN_CMD:
739 case DEL_VLAN_CMD:
740 case GET_VLAN_REALDEV_NAME_CMD:
741 case GET_VLAN_VID_CMD:
742 err = -ENODEV;
743 dev = __dev_get_by_name(args.device1);
744 if (!dev)
745 goto out;
746
747 err = -EINVAL;
748 if (args.cmd != ADD_VLAN_CMD &&
749 !(dev->priv_flags & IFF_802_1Q_VLAN))
750 goto out;
751 }
752
753 switch (args.cmd) {
754 case SET_VLAN_INGRESS_PRIORITY_CMD:
755 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (!capable(CAP_NET_ADMIN))
Patrick McHardyc17d8872007-06-13 12:05:22 -0700757 break;
758 vlan_dev_set_ingress_priority(dev,
759 args.u.skb_priority,
760 args.vlan_qos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 break;
762
763 case SET_VLAN_EGRESS_PRIORITY_CMD:
Patrick McHardyc17d8872007-06-13 12:05:22 -0700764 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (!capable(CAP_NET_ADMIN))
Patrick McHardyc17d8872007-06-13 12:05:22 -0700766 break;
767 err = vlan_dev_set_egress_priority(dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 args.u.skb_priority,
769 args.vlan_qos);
770 break;
771
772 case SET_VLAN_FLAG_CMD:
Patrick McHardyc17d8872007-06-13 12:05:22 -0700773 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (!capable(CAP_NET_ADMIN))
Patrick McHardyc17d8872007-06-13 12:05:22 -0700775 break;
776 err = vlan_dev_set_vlan_flag(dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 args.u.flag,
778 args.vlan_qos);
779 break;
780
781 case SET_VLAN_NAME_TYPE_CMD:
Patrick McHardyc17d8872007-06-13 12:05:22 -0700782 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (!capable(CAP_NET_ADMIN))
784 return -EPERM;
Patrick McHardyc17d8872007-06-13 12:05:22 -0700785 if ((args.u.name_type >= 0) &&
786 (args.u.name_type < VLAN_NAME_TYPE_HIGHEST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 vlan_name_type = args.u.name_type;
788 err = 0;
789 } else {
790 err = -EINVAL;
791 }
792 break;
793
794 case ADD_VLAN_CMD:
Patrick McHardyc17d8872007-06-13 12:05:22 -0700795 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (!capable(CAP_NET_ADMIN))
Patrick McHardyc17d8872007-06-13 12:05:22 -0700797 break;
Patrick McHardy2ae0bf62007-06-13 12:06:43 -0700798 err = register_vlan_device(dev, args.u.VID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 break;
800
801 case DEL_VLAN_CMD:
Patrick McHardyc17d8872007-06-13 12:05:22 -0700802 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (!capable(CAP_NET_ADMIN))
Patrick McHardyc17d8872007-06-13 12:05:22 -0700804 break;
805 err = unregister_vlan_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 break;
807
808 case GET_VLAN_INGRESS_PRIORITY_CMD:
809 /* TODO: Implement
810 err = vlan_dev_get_ingress_priority(args);
811 if (copy_to_user((void*)arg, &args,
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900812 sizeof(struct vlan_ioctl_args))) {
813 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815 */
816 err = -EINVAL;
817 break;
818 case GET_VLAN_EGRESS_PRIORITY_CMD:
819 /* TODO: Implement
820 err = vlan_dev_get_egress_priority(args.device1, &(args.args);
821 if (copy_to_user((void*)arg, &args,
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900822 sizeof(struct vlan_ioctl_args))) {
823 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825 */
826 err = -EINVAL;
827 break;
828 case GET_VLAN_REALDEV_NAME_CMD:
Patrick McHardyc17d8872007-06-13 12:05:22 -0700829 vlan_dev_get_realdev_name(dev, args.u.device2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (copy_to_user(arg, &args,
831 sizeof(struct vlan_ioctl_args))) {
832 err = -EFAULT;
833 }
834 break;
835
836 case GET_VLAN_VID_CMD:
Patrick McHardyc17d8872007-06-13 12:05:22 -0700837 vlan_dev_get_vid(dev, &vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 args.u.VID = vid;
839 if (copy_to_user(arg, &args,
840 sizeof(struct vlan_ioctl_args))) {
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900841 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
843 break;
844
845 default:
846 /* pass on to underlying device instead?? */
847 printk(VLAN_DBG "%s: Unknown VLAN CMD: %x \n",
848 __FUNCTION__, args.cmd);
Patrick McHardyc17d8872007-06-13 12:05:22 -0700849 err = -EINVAL;
850 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700851 }
Mika Kukkonen7eb1b3d2005-12-21 18:39:49 -0800852out:
Patrick McHardyc17d8872007-06-13 12:05:22 -0700853 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return err;
855}
856
857MODULE_LICENSE("GPL");
858MODULE_VERSION(DRV_VERSION);