blob: a9ced0a6f4c083061cdd27f8b3f0a41b47756238 [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>
Eric W. Biedermane9dc8652007-09-12 13:02:17 +020034#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
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
44/* Our listing of VLAN group(s) */
45static struct hlist_head vlan_group_hash[VLAN_GRP_HASH_SIZE];
46#define vlan_grp_hashfn(IDX) ((((IDX) >> VLAN_GRP_HASH_SHIFT) ^ (IDX)) & VLAN_GRP_HASH_MASK)
47
48static char vlan_fullname[] = "802.1Q VLAN Support";
49static char vlan_version[] = DRV_VERSION;
50static char vlan_copyright[] = "Ben Greear <greearb@candelatech.com>";
51static char vlan_buggyright[] = "David S. Miller <davem@redhat.com>";
52
53static int vlan_device_event(struct notifier_block *, unsigned long, void *);
Eric W. Biederman881d9662007-09-17 11:56:21 -070054static int vlan_ioctl_handler(struct net *net, void __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static int unregister_vlan_dev(struct net_device *, unsigned short );
56
57static struct notifier_block vlan_notifier_block = {
58 .notifier_call = vlan_device_event,
59};
60
61/* These may be changed at run-time through IOCTLs */
62
63/* Determines interface naming scheme. */
64unsigned short vlan_name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;
65
66static struct packet_type vlan_packet_type = {
67 .type = __constant_htons(ETH_P_8021Q),
68 .func = vlan_skb_recv, /* VLAN receive method */
69};
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/* End of global variables definitions. */
72
73/*
74 * Function vlan_proto_init (pro)
75 *
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +090076 * Initialize VLAN protocol layer,
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 *
78 */
79static int __init vlan_proto_init(void)
80{
81 int err;
82
83 printk(VLAN_INF "%s v%s %s\n",
84 vlan_fullname, vlan_version, vlan_copyright);
85 printk(VLAN_INF "All bugs added by %s\n",
86 vlan_buggyright);
87
88 /* proc file system initialization */
89 err = vlan_proc_init();
90 if (err < 0) {
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +090091 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 "%s %s: can't create entry in proc filesystem!\n",
93 __FUNCTION__, VLAN_NAME);
94 return err;
95 }
96
97 dev_add_pack(&vlan_packet_type);
98
99 /* Register us to receive netdevice events */
100 err = register_netdevice_notifier(&vlan_notifier_block);
Patrick McHardy07b5b172007-06-13 12:07:54 -0700101 if (err < 0)
102 goto err1;
103
104 err = vlan_netlink_init();
105 if (err < 0)
106 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 vlan_ioctl_set(vlan_ioctl_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return 0;
Patrick McHardy07b5b172007-06-13 12:07:54 -0700110
111err2:
112 unregister_netdevice_notifier(&vlan_notifier_block);
113err1:
114 vlan_proc_cleanup();
115 dev_remove_pack(&vlan_packet_type);
116 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/*
120 * Module 'remove' entry point.
121 * o delete /proc/net/router directory and static entries.
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900122 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static void __exit vlan_cleanup_module(void)
124{
125 int i;
126
Patrick McHardy07b5b172007-06-13 12:07:54 -0700127 vlan_netlink_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 vlan_ioctl_set(NULL);
129
130 /* Un-register us from receiving netdevice events */
131 unregister_netdevice_notifier(&vlan_notifier_block);
132
133 dev_remove_pack(&vlan_packet_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 /* This table must be empty if there are no module
136 * references left.
137 */
138 for (i = 0; i < VLAN_GRP_HASH_SIZE; i++) {
139 BUG_ON(!hlist_empty(&vlan_group_hash[i]));
140 }
141 vlan_proc_cleanup();
142
143 synchronize_net();
144}
145
146module_init(vlan_proto_init);
147module_exit(vlan_cleanup_module);
148
149/* Must be invoked with RCU read lock (no preempt) */
150static struct vlan_group *__vlan_find_group(int real_dev_ifindex)
151{
152 struct vlan_group *grp;
153 struct hlist_node *n;
154 int hash = vlan_grp_hashfn(real_dev_ifindex);
155
156 hlist_for_each_entry_rcu(grp, n, &vlan_group_hash[hash], hlist) {
157 if (grp->real_dev_ifindex == real_dev_ifindex)
158 return grp;
159 }
160
161 return NULL;
162}
163
164/* Find the protocol handler. Assumes VID < VLAN_VID_MASK.
165 *
166 * Must be invoked with RCU read lock (no preempt)
167 */
168struct net_device *__find_vlan_dev(struct net_device *real_dev,
169 unsigned short VID)
170{
171 struct vlan_group *grp = __vlan_find_group(real_dev->ifindex);
172
173 if (grp)
Dan Aloni5c15bde2007-03-02 20:44:51 -0800174 return vlan_group_get_device(grp, VID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 return NULL;
177}
178
Dan Aloni5c15bde2007-03-02 20:44:51 -0800179static void vlan_group_free(struct vlan_group *grp)
180{
181 int i;
182
183 for (i=0; i < VLAN_GROUP_ARRAY_SPLIT_PARTS; i++)
184 kfree(grp->vlan_devices_arrays[i]);
185 kfree(grp);
186}
187
Patrick McHardy42429aa2007-06-13 12:05:59 -0700188static struct vlan_group *vlan_group_alloc(int ifindex)
189{
190 struct vlan_group *grp;
191 unsigned int size;
192 unsigned int i;
193
194 grp = kzalloc(sizeof(struct vlan_group), GFP_KERNEL);
195 if (!grp)
196 return NULL;
197
198 size = sizeof(struct net_device *) * VLAN_GROUP_ARRAY_PART_LEN;
199
200 for (i = 0; i < VLAN_GROUP_ARRAY_SPLIT_PARTS; i++) {
201 grp->vlan_devices_arrays[i] = kzalloc(size, GFP_KERNEL);
202 if (!grp->vlan_devices_arrays[i])
203 goto err;
204 }
205
206 grp->real_dev_ifindex = ifindex;
207 hlist_add_head_rcu(&grp->hlist,
208 &vlan_group_hash[vlan_grp_hashfn(ifindex)]);
209 return grp;
210
211err:
212 vlan_group_free(grp);
213 return NULL;
214}
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216static void vlan_rcu_free(struct rcu_head *rcu)
217{
Dan Aloni5c15bde2007-03-02 20:44:51 -0800218 vlan_group_free(container_of(rcu, struct vlan_group, rcu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221
222/* This returns 0 if everything went fine.
223 * It will return 1 if the group was killed as a result.
224 * A negative return indicates failure.
225 *
226 * The RTNL lock must be held.
227 */
228static int unregister_vlan_dev(struct net_device *real_dev,
229 unsigned short vlan_id)
230{
231 struct net_device *dev = NULL;
232 int real_dev_ifindex = real_dev->ifindex;
233 struct vlan_group *grp;
234 int i, ret;
235
236#ifdef VLAN_DEBUG
237 printk(VLAN_DBG "%s: VID: %i\n", __FUNCTION__, vlan_id);
238#endif
239
240 /* sanity check */
241 if (vlan_id >= VLAN_VID_MASK)
242 return -EINVAL;
243
244 ASSERT_RTNL();
245 grp = __vlan_find_group(real_dev_ifindex);
246
247 ret = 0;
248
249 if (grp) {
Dan Aloni5c15bde2007-03-02 20:44:51 -0800250 dev = vlan_group_get_device(grp, vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (dev) {
252 /* Remove proc entry */
253 vlan_proc_rem_dev(dev);
254
255 /* Take it out of our own structures, but be sure to
256 * interlock with HW accelerating devices or SW vlan
257 * input packet processing.
258 */
Stephen Hemmingerd2d1acd2007-06-01 09:43:57 -0700259 if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 real_dev->vlan_rx_kill_vid(real_dev, vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Dan Aloni5c15bde2007-03-02 20:44:51 -0800262 vlan_group_set_device(grp, vlan_id, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 synchronize_net();
264
265
266 /* Caller unregisters (and if necessary, puts)
267 * VLAN device, but we get rid of the reference to
268 * real_dev here.
269 */
270 dev_put(real_dev);
271
272 /* If the group is now empty, kill off the
273 * group.
274 */
275 for (i = 0; i < VLAN_VID_MASK; i++)
Dan Aloni5c15bde2007-03-02 20:44:51 -0800276 if (vlan_group_get_device(grp, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 break;
278
279 if (i == VLAN_VID_MASK) {
280 if (real_dev->features & NETIF_F_HW_VLAN_RX)
281 real_dev->vlan_rx_register(real_dev, NULL);
282
283 hlist_del_rcu(&grp->hlist);
284
285 /* Free the group, after all cpu's are done. */
286 call_rcu(&grp->rcu, vlan_rcu_free);
287
288 grp = NULL;
289 ret = 1;
290 }
291 }
292 }
293
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900294 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Patrick McHardy07b5b172007-06-13 12:07:54 -0700297int unregister_vlan_device(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 int ret;
300
Patrick McHardyc17d8872007-06-13 12:05:22 -0700301 ret = unregister_vlan_dev(VLAN_DEV_INFO(dev)->real_dev,
302 VLAN_DEV_INFO(dev)->vlan_id);
303 unregister_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Patrick McHardyc17d8872007-06-13 12:05:22 -0700305 if (ret == 1)
306 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return ret;
308}
309
Patrick McHardy2f4284a2007-06-13 12:05:41 -0700310/*
311 * vlan network devices have devices nesting below it, and are a special
312 * "super class" of normal network devices; split their locks off into a
313 * separate class since they always nest.
314 */
315static struct lock_class_key vlan_netdev_xmit_lock_key;
316
317static int vlan_dev_init(struct net_device *dev)
318{
319 struct net_device *real_dev = VLAN_DEV_INFO(dev)->real_dev;
320
321 /* IFF_BROADCAST|IFF_MULTICAST; ??? */
322 dev->flags = real_dev->flags & ~IFF_UP;
323 dev->iflink = real_dev->ifindex;
324 dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
325 (1<<__LINK_STATE_DORMANT))) |
326 (1<<__LINK_STATE_PRESENT);
327
Patrick McHardy0e068772007-07-11 19:42:31 -0700328 if (is_zero_ether_addr(dev->dev_addr))
329 memcpy(dev->dev_addr, real_dev->dev_addr, dev->addr_len);
330 if (is_zero_ether_addr(dev->broadcast))
331 memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
Patrick McHardy2f4284a2007-06-13 12:05:41 -0700332
333 if (real_dev->features & NETIF_F_HW_VLAN_TX) {
334 dev->hard_header = real_dev->hard_header;
335 dev->hard_header_len = real_dev->hard_header_len;
336 dev->hard_start_xmit = vlan_dev_hwaccel_hard_start_xmit;
337 dev->rebuild_header = real_dev->rebuild_header;
338 } else {
339 dev->hard_header = vlan_dev_hard_header;
340 dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
341 dev->hard_start_xmit = vlan_dev_hard_start_xmit;
342 dev->rebuild_header = vlan_dev_rebuild_header;
343 }
344 dev->hard_header_parse = real_dev->hard_header_parse;
Patrick McHardy8c979c22007-07-11 19:45:24 -0700345 dev->hard_header_cache = NULL;
Patrick McHardy2f4284a2007-06-13 12:05:41 -0700346
347 lockdep_set_class(&dev->_xmit_lock, &vlan_netdev_xmit_lock_key);
348 return 0;
349}
350
Patrick McHardy07b5b172007-06-13 12:07:54 -0700351void vlan_setup(struct net_device *new_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 SET_MODULE_OWNER(new_dev);
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900354
Patrick McHardy8c979c22007-07-11 19:45:24 -0700355 ether_setup(new_dev);
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 /* new_dev->ifindex = 0; it will be set when added to
358 * the global list.
359 * iflink is set as well.
360 */
361 new_dev->get_stats = vlan_dev_get_stats;
362
363 /* Make this thing known as a VLAN device */
364 new_dev->priv_flags |= IFF_802_1Q_VLAN;
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /* Set us up to have no queue, as the underlying Hardware device
367 * can do all the queueing we could want.
368 */
369 new_dev->tx_queue_len = 0;
370
371 /* set up method calls */
372 new_dev->change_mtu = vlan_dev_change_mtu;
Patrick McHardy2f4284a2007-06-13 12:05:41 -0700373 new_dev->init = vlan_dev_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 new_dev->open = vlan_dev_open;
375 new_dev->stop = vlan_dev_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 new_dev->set_multicast_list = vlan_dev_set_multicast_list;
Patrick McHardy6c78dcb2007-07-14 18:52:56 -0700377 new_dev->change_rx_flags = vlan_change_rx_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 new_dev->destructor = free_netdev;
379 new_dev->do_ioctl = vlan_dev_ioctl;
Patrick McHardy0e068772007-07-11 19:42:31 -0700380
Patrick McHardya7ecfc82007-07-14 18:56:30 -0700381 memset(new_dev->broadcast, 0, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
Stefan Rompfddd7bf92006-03-20 17:11:41 -0800384static void vlan_transfer_operstate(const struct net_device *dev, struct net_device *vlandev)
385{
386 /* Have to respect userspace enforced dormant state
387 * of real device, also must allow supplicant running
388 * on VLAN device
389 */
390 if (dev->operstate == IF_OPER_DORMANT)
391 netif_dormant_on(vlandev);
392 else
393 netif_dormant_off(vlandev);
394
395 if (netif_carrier_ok(dev)) {
396 if (!netif_carrier_ok(vlandev))
397 netif_carrier_on(vlandev);
398 } else {
399 if (netif_carrier_ok(vlandev))
400 netif_carrier_off(vlandev);
401 }
402}
403
Patrick McHardy07b5b172007-06-13 12:07:54 -0700404int vlan_check_real_dev(struct net_device *real_dev, unsigned short vlan_id)
Patrick McHardyc1d3ee92007-06-13 12:06:14 -0700405{
406 if (real_dev->features & NETIF_F_VLAN_CHALLENGED) {
407 printk(VLAN_DBG "%s: VLANs not supported on %s.\n",
408 __FUNCTION__, real_dev->name);
409 return -EOPNOTSUPP;
410 }
411
412 if ((real_dev->features & NETIF_F_HW_VLAN_RX) &&
413 !real_dev->vlan_rx_register) {
414 printk(VLAN_DBG "%s: Device %s has buggy VLAN hw accel.\n",
415 __FUNCTION__, real_dev->name);
416 return -EOPNOTSUPP;
417 }
418
419 if ((real_dev->features & NETIF_F_HW_VLAN_FILTER) &&
420 (!real_dev->vlan_rx_add_vid || !real_dev->vlan_rx_kill_vid)) {
421 printk(VLAN_DBG "%s: Device %s has buggy VLAN hw accel.\n",
422 __FUNCTION__, real_dev->name);
423 return -EOPNOTSUPP;
424 }
425
426 /* The real device must be up and operating in order to
427 * assosciate a VLAN device with it.
428 */
429 if (!(real_dev->flags & IFF_UP))
430 return -ENETDOWN;
431
432 if (__find_vlan_dev(real_dev, vlan_id) != NULL) {
433 /* was already registered. */
434 printk(VLAN_DBG "%s: ALREADY had VLAN registered\n", __FUNCTION__);
435 return -EEXIST;
436 }
437
438 return 0;
439}
440
Patrick McHardy07b5b172007-06-13 12:07:54 -0700441int register_vlan_dev(struct net_device *dev)
Patrick McHardye89fe422007-06-13 12:06:29 -0700442{
443 struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev);
444 struct net_device *real_dev = vlan->real_dev;
445 unsigned short vlan_id = vlan->vlan_id;
446 struct vlan_group *grp, *ngrp = NULL;
447 int err;
448
449 grp = __vlan_find_group(real_dev->ifindex);
450 if (!grp) {
451 ngrp = grp = vlan_group_alloc(real_dev->ifindex);
452 if (!grp)
453 return -ENOBUFS;
454 }
455
456 err = register_netdevice(dev);
457 if (err < 0)
458 goto out_free_group;
459
460 /* Account for reference in struct vlan_dev_info */
461 dev_hold(real_dev);
462
463 vlan_transfer_operstate(real_dev, dev);
464 linkwatch_fire_event(dev); /* _MUST_ call rfc2863_policy() */
465
466 /* So, got the sucker initialized, now lets place
467 * it into our local structure.
468 */
469 vlan_group_set_device(grp, vlan_id, dev);
470 if (ngrp && real_dev->features & NETIF_F_HW_VLAN_RX)
471 real_dev->vlan_rx_register(real_dev, ngrp);
472 if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
473 real_dev->vlan_rx_add_vid(real_dev, vlan_id);
474
475 if (vlan_proc_add_dev(dev) < 0)
476 printk(KERN_WARNING "VLAN: failed to add proc entry for %s\n",
477 dev->name);
478 return 0;
479
480out_free_group:
481 if (ngrp)
482 vlan_group_free(ngrp);
483 return err;
484}
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486/* Attach a VLAN device to a mac address (ie Ethernet Card).
Patrick McHardy2ae0bf62007-06-13 12:06:43 -0700487 * Returns 0 if the device was created or a negative error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 */
Patrick McHardy2ae0bf62007-06-13 12:06:43 -0700489static int register_vlan_device(struct net_device *real_dev,
490 unsigned short VLAN_ID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 struct net_device *new_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 char name[IFNAMSIZ];
Patrick McHardy2ae0bf62007-06-13 12:06:43 -0700494 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496#ifdef VLAN_DEBUG
497 printk(VLAN_DBG "%s: if_name -:%s:- vid: %i\n",
498 __FUNCTION__, eth_IF_name, VLAN_ID);
499#endif
500
501 if (VLAN_ID >= VLAN_VID_MASK)
Patrick McHardy2ae0bf62007-06-13 12:06:43 -0700502 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Patrick McHardy2ae0bf62007-06-13 12:06:43 -0700504 err = vlan_check_real_dev(real_dev, VLAN_ID);
505 if (err < 0)
506 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 /* Gotta set up the fields for the device. */
509#ifdef VLAN_DEBUG
510 printk(VLAN_DBG "About to allocate name, vlan_name_type: %i\n",
511 vlan_name_type);
512#endif
513 switch (vlan_name_type) {
514 case VLAN_NAME_TYPE_RAW_PLUS_VID:
515 /* name will look like: eth1.0005 */
516 snprintf(name, IFNAMSIZ, "%s.%.4i", real_dev->name, VLAN_ID);
517 break;
518 case VLAN_NAME_TYPE_PLUS_VID_NO_PAD:
519 /* Put our vlan.VID in the name.
520 * Name will look like: vlan5
521 */
522 snprintf(name, IFNAMSIZ, "vlan%i", VLAN_ID);
523 break;
524 case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD:
525 /* Put our vlan.VID in the name.
526 * Name will look like: eth0.5
527 */
528 snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, VLAN_ID);
529 break;
530 case VLAN_NAME_TYPE_PLUS_VID:
531 /* Put our vlan.VID in the name.
532 * Name will look like: vlan0005
533 */
534 default:
535 snprintf(name, IFNAMSIZ, "vlan%.4i", VLAN_ID);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700536 }
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 new_dev = alloc_netdev(sizeof(struct vlan_dev_info), name,
539 vlan_setup);
Arjan van de Ven5dd8d1e2006-07-03 00:25:33 -0700540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (new_dev == NULL)
Patrick McHardy2ae0bf62007-06-13 12:06:43 -0700542 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /* need 4 bytes for extra VLAN header info,
545 * hope the underlying device can handle it.
546 */
547 new_dev->mtu = real_dev->mtu;
548
Patrick McHardy2f4284a2007-06-13 12:05:41 -0700549#ifdef VLAN_DEBUG
550 printk(VLAN_DBG "Allocated new name -:%s:-\n", new_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 VLAN_MEM_DBG("new_dev->priv malloc, addr: %p size: %i\n",
552 new_dev->priv,
553 sizeof(struct vlan_dev_info));
Patrick McHardy2f4284a2007-06-13 12:05:41 -0700554#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 VLAN_DEV_INFO(new_dev)->vlan_id = VLAN_ID; /* 1 through VLAN_VID_MASK */
557 VLAN_DEV_INFO(new_dev)->real_dev = real_dev;
558 VLAN_DEV_INFO(new_dev)->dent = NULL;
Patrick McHardya4bf3af42007-06-13 12:07:37 -0700559 VLAN_DEV_INFO(new_dev)->flags = VLAN_FLAG_REORDER_HDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Patrick McHardy07b5b172007-06-13 12:07:54 -0700561 new_dev->rtnl_link_ops = &vlan_link_ops;
Patrick McHardy2ae0bf62007-06-13 12:06:43 -0700562 err = register_vlan_dev(new_dev);
563 if (err < 0)
Patrick McHardye89fe422007-06-13 12:06:29 -0700564 goto out_free_newdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566#ifdef VLAN_DEBUG
567 printk(VLAN_DBG "Allocated new device successfully, returning.\n");
568#endif
Patrick McHardy2ae0bf62007-06-13 12:06:43 -0700569 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571out_free_newdev:
572 free_netdev(new_dev);
Patrick McHardy2ae0bf62007-06-13 12:06:43 -0700573 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Patrick McHardy8c979c22007-07-11 19:45:24 -0700576static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600static 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
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200607 if (dev->nd_net != &init_net)
608 return NOTIFY_DONE;
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (!grp)
611 goto out;
612
613 /* It is OK that we do not hold the group lock right now,
614 * as we run under the RTNL lock.
615 */
616
617 switch (event) {
618 case NETDEV_CHANGE:
619 /* Propagate real device state to vlan devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
Dan Aloni5c15bde2007-03-02 20:44:51 -0800621 vlandev = vlan_group_get_device(grp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (!vlandev)
623 continue;
624
Stefan Rompfddd7bf92006-03-20 17:11:41 -0800625 vlan_transfer_operstate(dev, vlandev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627 break;
628
Patrick McHardy8c979c22007-07-11 19:45:24 -0700629 case NETDEV_CHANGEADDR:
630 /* Adjust unicast filters on underlying device */
631 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
632 vlandev = vlan_group_get_device(grp, i);
633 if (!vlandev)
634 continue;
635
636 vlan_sync_address(dev, vlandev);
637 }
638 break;
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 case NETDEV_DOWN:
641 /* Put all VLANs for this dev in the down state too. */
642 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
Dan Aloni5c15bde2007-03-02 20:44:51 -0800643 vlandev = vlan_group_get_device(grp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (!vlandev)
645 continue;
646
647 flgs = vlandev->flags;
648 if (!(flgs & IFF_UP))
649 continue;
650
651 dev_change_flags(vlandev, flgs & ~IFF_UP);
652 }
653 break;
654
655 case NETDEV_UP:
656 /* Put all VLANs for this dev in the up state too. */
657 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
Dan Aloni5c15bde2007-03-02 20:44:51 -0800658 vlandev = vlan_group_get_device(grp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (!vlandev)
660 continue;
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 flgs = vlandev->flags;
663 if (flgs & IFF_UP)
664 continue;
665
666 dev_change_flags(vlandev, flgs | IFF_UP);
667 }
668 break;
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 case NETDEV_UNREGISTER:
671 /* Delete all VLANs for this dev. */
672 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
673 int ret;
674
Dan Aloni5c15bde2007-03-02 20:44:51 -0800675 vlandev = vlan_group_get_device(grp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (!vlandev)
677 continue;
678
679 ret = unregister_vlan_dev(dev,
680 VLAN_DEV_INFO(vlandev)->vlan_id);
681
682 unregister_netdevice(vlandev);
683
684 /* Group was destroyed? */
685 if (ret == 1)
686 break;
687 }
688 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691out:
692 return NOTIFY_DONE;
693}
694
695/*
696 * VLAN IOCTL handler.
697 * o execute requested action or pass command to the device driver
698 * arg is really a struct vlan_ioctl_args __user *.
699 */
Eric W. Biederman881d9662007-09-17 11:56:21 -0700700static int vlan_ioctl_handler(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Patrick McHardyc17d8872007-06-13 12:05:22 -0700702 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 unsigned short vid = 0;
704 struct vlan_ioctl_args args;
Patrick McHardyc17d8872007-06-13 12:05:22 -0700705 struct net_device *dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args)))
708 return -EFAULT;
709
710 /* Null terminate this sucker, just in case. */
711 args.device1[23] = 0;
712 args.u.device2[23] = 0;
713
714#ifdef VLAN_DEBUG
715 printk(VLAN_DBG "%s: args.cmd: %x\n", __FUNCTION__, args.cmd);
716#endif
717
Patrick McHardyc17d8872007-06-13 12:05:22 -0700718 rtnl_lock();
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 switch (args.cmd) {
721 case SET_VLAN_INGRESS_PRIORITY_CMD:
Patrick McHardyc17d8872007-06-13 12:05:22 -0700722 case SET_VLAN_EGRESS_PRIORITY_CMD:
723 case SET_VLAN_FLAG_CMD:
724 case ADD_VLAN_CMD:
725 case DEL_VLAN_CMD:
726 case GET_VLAN_REALDEV_NAME_CMD:
727 case GET_VLAN_VID_CMD:
728 err = -ENODEV;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700729 dev = __dev_get_by_name(&init_net, args.device1);
Patrick McHardyc17d8872007-06-13 12:05:22 -0700730 if (!dev)
731 goto out;
732
733 err = -EINVAL;
734 if (args.cmd != ADD_VLAN_CMD &&
735 !(dev->priv_flags & IFF_802_1Q_VLAN))
736 goto out;
737 }
738
739 switch (args.cmd) {
740 case SET_VLAN_INGRESS_PRIORITY_CMD:
741 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (!capable(CAP_NET_ADMIN))
Patrick McHardyc17d8872007-06-13 12:05:22 -0700743 break;
744 vlan_dev_set_ingress_priority(dev,
745 args.u.skb_priority,
746 args.vlan_qos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 break;
748
749 case SET_VLAN_EGRESS_PRIORITY_CMD:
Patrick McHardyc17d8872007-06-13 12:05:22 -0700750 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (!capable(CAP_NET_ADMIN))
Patrick McHardyc17d8872007-06-13 12:05:22 -0700752 break;
753 err = vlan_dev_set_egress_priority(dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 args.u.skb_priority,
755 args.vlan_qos);
756 break;
757
758 case SET_VLAN_FLAG_CMD:
Patrick McHardyc17d8872007-06-13 12:05:22 -0700759 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (!capable(CAP_NET_ADMIN))
Patrick McHardyc17d8872007-06-13 12:05:22 -0700761 break;
762 err = vlan_dev_set_vlan_flag(dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 args.u.flag,
764 args.vlan_qos);
765 break;
766
767 case SET_VLAN_NAME_TYPE_CMD:
Patrick McHardyc17d8872007-06-13 12:05:22 -0700768 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (!capable(CAP_NET_ADMIN))
770 return -EPERM;
Patrick McHardyc17d8872007-06-13 12:05:22 -0700771 if ((args.u.name_type >= 0) &&
772 (args.u.name_type < VLAN_NAME_TYPE_HIGHEST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 vlan_name_type = args.u.name_type;
774 err = 0;
775 } else {
776 err = -EINVAL;
777 }
778 break;
779
780 case ADD_VLAN_CMD:
Patrick McHardyc17d8872007-06-13 12:05:22 -0700781 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (!capable(CAP_NET_ADMIN))
Patrick McHardyc17d8872007-06-13 12:05:22 -0700783 break;
Patrick McHardy2ae0bf62007-06-13 12:06:43 -0700784 err = register_vlan_device(dev, args.u.VID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 break;
786
787 case DEL_VLAN_CMD:
Patrick McHardyc17d8872007-06-13 12:05:22 -0700788 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (!capable(CAP_NET_ADMIN))
Patrick McHardyc17d8872007-06-13 12:05:22 -0700790 break;
791 err = unregister_vlan_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 break;
793
794 case GET_VLAN_INGRESS_PRIORITY_CMD:
795 /* TODO: Implement
796 err = vlan_dev_get_ingress_priority(args);
797 if (copy_to_user((void*)arg, &args,
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900798 sizeof(struct vlan_ioctl_args))) {
799 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801 */
802 err = -EINVAL;
803 break;
804 case GET_VLAN_EGRESS_PRIORITY_CMD:
805 /* TODO: Implement
806 err = vlan_dev_get_egress_priority(args.device1, &(args.args);
807 if (copy_to_user((void*)arg, &args,
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900808 sizeof(struct vlan_ioctl_args))) {
809 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811 */
812 err = -EINVAL;
813 break;
814 case GET_VLAN_REALDEV_NAME_CMD:
Andrew Morton3f5f4342007-07-24 15:37:11 -0700815 err = 0;
Patrick McHardyc17d8872007-06-13 12:05:22 -0700816 vlan_dev_get_realdev_name(dev, args.u.device2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (copy_to_user(arg, &args,
818 sizeof(struct vlan_ioctl_args))) {
819 err = -EFAULT;
820 }
821 break;
822
823 case GET_VLAN_VID_CMD:
Andrew Morton3f5f4342007-07-24 15:37:11 -0700824 err = 0;
Patrick McHardyc17d8872007-06-13 12:05:22 -0700825 vlan_dev_get_vid(dev, &vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 args.u.VID = vid;
827 if (copy_to_user(arg, &args,
828 sizeof(struct vlan_ioctl_args))) {
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900829 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831 break;
832
833 default:
834 /* pass on to underlying device instead?? */
835 printk(VLAN_DBG "%s: Unknown VLAN CMD: %x \n",
836 __FUNCTION__, args.cmd);
Patrick McHardyc17d8872007-06-13 12:05:22 -0700837 err = -EINVAL;
838 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700839 }
Mika Kukkonen7eb1b3d2005-12-21 18:39:49 -0800840out:
Patrick McHardyc17d8872007-06-13 12:05:22 -0700841 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return err;
843}
844
845MODULE_LICENSE("GPL");
846MODULE_VERSION(DRV_VERSION);