blob: eb1c71ed7dfe928c80e39701138fd7a0f52160d1 [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);
100 if (err < 0) {
101 dev_remove_pack(&vlan_packet_type);
102 vlan_proc_cleanup();
103 return err;
104 }
105
106 vlan_ioctl_set(vlan_ioctl_handler);
107
108 return 0;
109}
110
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900111/* Cleanup all vlan devices
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * Note: devices that have been registered that but not
113 * brought up will exist but have no module ref count.
114 */
115static void __exit vlan_cleanup_devices(void)
116{
117 struct net_device *dev, *nxt;
118
119 rtnl_lock();
120 for (dev = dev_base; dev; dev = nxt) {
121 nxt = dev->next;
122 if (dev->priv_flags & IFF_802_1Q_VLAN) {
123 unregister_vlan_dev(VLAN_DEV_INFO(dev)->real_dev,
124 VLAN_DEV_INFO(dev)->vlan_id);
125
126 unregister_netdevice(dev);
127 }
128 }
129 rtnl_unlock();
130}
131
132/*
133 * Module 'remove' entry point.
134 * o delete /proc/net/router directory and static entries.
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900135 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static void __exit vlan_cleanup_module(void)
137{
138 int i;
139
140 vlan_ioctl_set(NULL);
141
142 /* Un-register us from receiving netdevice events */
143 unregister_netdevice_notifier(&vlan_notifier_block);
144
145 dev_remove_pack(&vlan_packet_type);
146 vlan_cleanup_devices();
147
148 /* This table must be empty if there are no module
149 * references left.
150 */
151 for (i = 0; i < VLAN_GRP_HASH_SIZE; i++) {
152 BUG_ON(!hlist_empty(&vlan_group_hash[i]));
153 }
154 vlan_proc_cleanup();
155
156 synchronize_net();
157}
158
159module_init(vlan_proto_init);
160module_exit(vlan_cleanup_module);
161
162/* Must be invoked with RCU read lock (no preempt) */
163static struct vlan_group *__vlan_find_group(int real_dev_ifindex)
164{
165 struct vlan_group *grp;
166 struct hlist_node *n;
167 int hash = vlan_grp_hashfn(real_dev_ifindex);
168
169 hlist_for_each_entry_rcu(grp, n, &vlan_group_hash[hash], hlist) {
170 if (grp->real_dev_ifindex == real_dev_ifindex)
171 return grp;
172 }
173
174 return NULL;
175}
176
177/* Find the protocol handler. Assumes VID < VLAN_VID_MASK.
178 *
179 * Must be invoked with RCU read lock (no preempt)
180 */
181struct net_device *__find_vlan_dev(struct net_device *real_dev,
182 unsigned short VID)
183{
184 struct vlan_group *grp = __vlan_find_group(real_dev->ifindex);
185
186 if (grp)
Dan Aloni5c15bde2007-03-02 20:44:51 -0800187 return vlan_group_get_device(grp, VID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 return NULL;
190}
191
Dan Aloni5c15bde2007-03-02 20:44:51 -0800192static void vlan_group_free(struct vlan_group *grp)
193{
194 int i;
195
196 for (i=0; i < VLAN_GROUP_ARRAY_SPLIT_PARTS; i++)
197 kfree(grp->vlan_devices_arrays[i]);
198 kfree(grp);
199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static void vlan_rcu_free(struct rcu_head *rcu)
202{
Dan Aloni5c15bde2007-03-02 20:44:51 -0800203 vlan_group_free(container_of(rcu, struct vlan_group, rcu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
206
207/* This returns 0 if everything went fine.
208 * It will return 1 if the group was killed as a result.
209 * A negative return indicates failure.
210 *
211 * The RTNL lock must be held.
212 */
213static int unregister_vlan_dev(struct net_device *real_dev,
214 unsigned short vlan_id)
215{
216 struct net_device *dev = NULL;
217 int real_dev_ifindex = real_dev->ifindex;
218 struct vlan_group *grp;
219 int i, ret;
220
221#ifdef VLAN_DEBUG
222 printk(VLAN_DBG "%s: VID: %i\n", __FUNCTION__, vlan_id);
223#endif
224
225 /* sanity check */
226 if (vlan_id >= VLAN_VID_MASK)
227 return -EINVAL;
228
229 ASSERT_RTNL();
230 grp = __vlan_find_group(real_dev_ifindex);
231
232 ret = 0;
233
234 if (grp) {
Dan Aloni5c15bde2007-03-02 20:44:51 -0800235 dev = vlan_group_get_device(grp, vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (dev) {
237 /* Remove proc entry */
238 vlan_proc_rem_dev(dev);
239
240 /* Take it out of our own structures, but be sure to
241 * interlock with HW accelerating devices or SW vlan
242 * input packet processing.
243 */
244 if (real_dev->features &
245 (NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER)) {
246 real_dev->vlan_rx_kill_vid(real_dev, vlan_id);
247 }
248
Dan Aloni5c15bde2007-03-02 20:44:51 -0800249 vlan_group_set_device(grp, vlan_id, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 synchronize_net();
251
252
253 /* Caller unregisters (and if necessary, puts)
254 * VLAN device, but we get rid of the reference to
255 * real_dev here.
256 */
257 dev_put(real_dev);
258
259 /* If the group is now empty, kill off the
260 * group.
261 */
262 for (i = 0; i < VLAN_VID_MASK; i++)
Dan Aloni5c15bde2007-03-02 20:44:51 -0800263 if (vlan_group_get_device(grp, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 break;
265
266 if (i == VLAN_VID_MASK) {
267 if (real_dev->features & NETIF_F_HW_VLAN_RX)
268 real_dev->vlan_rx_register(real_dev, NULL);
269
270 hlist_del_rcu(&grp->hlist);
271
272 /* Free the group, after all cpu's are done. */
273 call_rcu(&grp->rcu, vlan_rcu_free);
274
275 grp = NULL;
276 ret = 1;
277 }
278 }
279 }
280
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900281 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284static int unregister_vlan_device(const char *vlan_IF_name)
285{
286 struct net_device *dev = NULL;
287 int ret;
288
289
290 dev = dev_get_by_name(vlan_IF_name);
291 ret = -EINVAL;
292 if (dev) {
293 if (dev->priv_flags & IFF_802_1Q_VLAN) {
294 rtnl_lock();
295
296 ret = unregister_vlan_dev(VLAN_DEV_INFO(dev)->real_dev,
297 VLAN_DEV_INFO(dev)->vlan_id);
298
299 dev_put(dev);
300 unregister_netdevice(dev);
301
302 rtnl_unlock();
303
304 if (ret == 1)
305 ret = 0;
306 } else {
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900307 printk(VLAN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 "%s: ERROR: Tried to remove a non-vlan device "
309 "with VLAN code, name: %s priv_flags: %hX\n",
310 __FUNCTION__, dev->name, dev->priv_flags);
311 dev_put(dev);
312 ret = -EPERM;
313 }
314 } else {
315#ifdef VLAN_DEBUG
316 printk(VLAN_DBG "%s: WARNING: Could not find dev.\n", __FUNCTION__);
317#endif
318 ret = -EINVAL;
319 }
320
321 return ret;
322}
323
324static void vlan_setup(struct net_device *new_dev)
325{
326 SET_MODULE_OWNER(new_dev);
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 /* new_dev->ifindex = 0; it will be set when added to
329 * the global list.
330 * iflink is set as well.
331 */
332 new_dev->get_stats = vlan_dev_get_stats;
333
334 /* Make this thing known as a VLAN device */
335 new_dev->priv_flags |= IFF_802_1Q_VLAN;
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 /* Set us up to have no queue, as the underlying Hardware device
338 * can do all the queueing we could want.
339 */
340 new_dev->tx_queue_len = 0;
341
342 /* set up method calls */
343 new_dev->change_mtu = vlan_dev_change_mtu;
344 new_dev->open = vlan_dev_open;
345 new_dev->stop = vlan_dev_stop;
346 new_dev->set_mac_address = vlan_dev_set_mac_address;
347 new_dev->set_multicast_list = vlan_dev_set_multicast_list;
348 new_dev->destructor = free_netdev;
349 new_dev->do_ioctl = vlan_dev_ioctl;
350}
351
Stefan Rompfddd7bf92006-03-20 17:11:41 -0800352static void vlan_transfer_operstate(const struct net_device *dev, struct net_device *vlandev)
353{
354 /* Have to respect userspace enforced dormant state
355 * of real device, also must allow supplicant running
356 * on VLAN device
357 */
358 if (dev->operstate == IF_OPER_DORMANT)
359 netif_dormant_on(vlandev);
360 else
361 netif_dormant_off(vlandev);
362
363 if (netif_carrier_ok(dev)) {
364 if (!netif_carrier_ok(vlandev))
365 netif_carrier_on(vlandev);
366 } else {
367 if (netif_carrier_ok(vlandev))
368 netif_carrier_off(vlandev);
369 }
370}
371
Arjan van de Ven5dd8d1e2006-07-03 00:25:33 -0700372/*
373 * vlan network devices have devices nesting below it, and are a special
374 * "super class" of normal network devices; split their locks off into a
375 * separate class since they always nest.
376 */
377static struct lock_class_key vlan_netdev_xmit_lock_key;
378
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380/* Attach a VLAN device to a mac address (ie Ethernet Card).
381 * Returns the device that was created, or NULL if there was
382 * an error of some kind.
383 */
384static struct net_device *register_vlan_device(const char *eth_IF_name,
385 unsigned short VLAN_ID)
386{
387 struct vlan_group *grp;
388 struct net_device *new_dev;
389 struct net_device *real_dev; /* the ethernet device */
390 char name[IFNAMSIZ];
Dan Aloni5c15bde2007-03-02 20:44:51 -0800391 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393#ifdef VLAN_DEBUG
394 printk(VLAN_DBG "%s: if_name -:%s:- vid: %i\n",
395 __FUNCTION__, eth_IF_name, VLAN_ID);
396#endif
397
398 if (VLAN_ID >= VLAN_VID_MASK)
399 goto out_ret_null;
400
401 /* find the device relating to eth_IF_name. */
402 real_dev = dev_get_by_name(eth_IF_name);
403 if (!real_dev)
404 goto out_ret_null;
405
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 goto out_put_dev;
410 }
411
412 if ((real_dev->features & NETIF_F_HW_VLAN_RX) &&
413 (real_dev->vlan_rx_register == NULL ||
414 real_dev->vlan_rx_kill_vid == NULL)) {
415 printk(VLAN_DBG "%s: Device %s has buggy VLAN hw accel.\n",
416 __FUNCTION__, real_dev->name);
417 goto out_put_dev;
418 }
419
420 if ((real_dev->features & NETIF_F_HW_VLAN_FILTER) &&
421 (real_dev->vlan_rx_add_vid == NULL ||
422 real_dev->vlan_rx_kill_vid == NULL)) {
423 printk(VLAN_DBG "%s: Device %s has buggy VLAN hw accel.\n",
424 __FUNCTION__, real_dev->name);
425 goto out_put_dev;
426 }
427
428 /* From this point on, all the data structures must remain
429 * consistent.
430 */
431 rtnl_lock();
432
433 /* The real device must be up and operating in order to
434 * assosciate a VLAN device with it.
435 */
436 if (!(real_dev->flags & IFF_UP))
437 goto out_unlock;
438
439 if (__find_vlan_dev(real_dev, VLAN_ID) != NULL) {
440 /* was already registered. */
441 printk(VLAN_DBG "%s: ALREADY had VLAN registered\n", __FUNCTION__);
442 goto out_unlock;
443 }
444
445 /* Gotta set up the fields for the device. */
446#ifdef VLAN_DEBUG
447 printk(VLAN_DBG "About to allocate name, vlan_name_type: %i\n",
448 vlan_name_type);
449#endif
450 switch (vlan_name_type) {
451 case VLAN_NAME_TYPE_RAW_PLUS_VID:
452 /* name will look like: eth1.0005 */
453 snprintf(name, IFNAMSIZ, "%s.%.4i", real_dev->name, VLAN_ID);
454 break;
455 case VLAN_NAME_TYPE_PLUS_VID_NO_PAD:
456 /* Put our vlan.VID in the name.
457 * Name will look like: vlan5
458 */
459 snprintf(name, IFNAMSIZ, "vlan%i", VLAN_ID);
460 break;
461 case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD:
462 /* Put our vlan.VID in the name.
463 * Name will look like: eth0.5
464 */
465 snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, VLAN_ID);
466 break;
467 case VLAN_NAME_TYPE_PLUS_VID:
468 /* Put our vlan.VID in the name.
469 * Name will look like: vlan0005
470 */
471 default:
472 snprintf(name, IFNAMSIZ, "vlan%.4i", VLAN_ID);
473 };
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 new_dev = alloc_netdev(sizeof(struct vlan_dev_info), name,
476 vlan_setup);
Arjan van de Ven5dd8d1e2006-07-03 00:25:33 -0700477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (new_dev == NULL)
479 goto out_unlock;
480
481#ifdef VLAN_DEBUG
482 printk(VLAN_DBG "Allocated new name -:%s:-\n", new_dev->name);
483#endif
484 /* IFF_BROADCAST|IFF_MULTICAST; ??? */
485 new_dev->flags = real_dev->flags;
486 new_dev->flags &= ~IFF_UP;
487
Stefan Rompf6c753c32006-07-24 13:52:13 -0700488 new_dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
489 (1<<__LINK_STATE_DORMANT))) |
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900490 (1<<__LINK_STATE_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 /* need 4 bytes for extra VLAN header info,
493 * hope the underlying device can handle it.
494 */
495 new_dev->mtu = real_dev->mtu;
496
497 /* TODO: maybe just assign it to be ETHERNET? */
498 new_dev->type = real_dev->type;
499
500 new_dev->hard_header_len = real_dev->hard_header_len;
501 if (!(real_dev->features & NETIF_F_HW_VLAN_TX)) {
502 /* Regular ethernet + 4 bytes (18 total). */
503 new_dev->hard_header_len += VLAN_HLEN;
504 }
505
506 VLAN_MEM_DBG("new_dev->priv malloc, addr: %p size: %i\n",
507 new_dev->priv,
508 sizeof(struct vlan_dev_info));
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 memcpy(new_dev->broadcast, real_dev->broadcast, real_dev->addr_len);
511 memcpy(new_dev->dev_addr, real_dev->dev_addr, real_dev->addr_len);
512 new_dev->addr_len = real_dev->addr_len;
513
514 if (real_dev->features & NETIF_F_HW_VLAN_TX) {
515 new_dev->hard_header = real_dev->hard_header;
516 new_dev->hard_start_xmit = vlan_dev_hwaccel_hard_start_xmit;
517 new_dev->rebuild_header = real_dev->rebuild_header;
518 } else {
519 new_dev->hard_header = vlan_dev_hard_header;
520 new_dev->hard_start_xmit = vlan_dev_hard_start_xmit;
521 new_dev->rebuild_header = vlan_dev_rebuild_header;
522 }
523 new_dev->hard_header_parse = real_dev->hard_header_parse;
524
525 VLAN_DEV_INFO(new_dev)->vlan_id = VLAN_ID; /* 1 through VLAN_VID_MASK */
526 VLAN_DEV_INFO(new_dev)->real_dev = real_dev;
527 VLAN_DEV_INFO(new_dev)->dent = NULL;
528 VLAN_DEV_INFO(new_dev)->flags = 1;
529
530#ifdef VLAN_DEBUG
531 printk(VLAN_DBG "About to go find the group for idx: %i\n",
532 real_dev->ifindex);
533#endif
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (register_netdevice(new_dev))
536 goto out_free_newdev;
537
Arjan van de Ven5dd8d1e2006-07-03 00:25:33 -0700538 lockdep_set_class(&new_dev->_xmit_lock, &vlan_netdev_xmit_lock_key);
539
Stefan Rompfddd7bf92006-03-20 17:11:41 -0800540 new_dev->iflink = real_dev->ifindex;
541 vlan_transfer_operstate(real_dev, new_dev);
542 linkwatch_fire_event(new_dev); /* _MUST_ call rfc2863_policy() */
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /* So, got the sucker initialized, now lets place
545 * it into our local structure.
546 */
547 grp = __vlan_find_group(real_dev->ifindex);
548
549 /* Note, we are running under the RTNL semaphore
550 * so it cannot "appear" on us.
551 */
552 if (!grp) { /* need to add a new group */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700553 grp = kzalloc(sizeof(struct vlan_group), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (!grp)
555 goto out_free_unregister;
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900556
Dan Aloni5c15bde2007-03-02 20:44:51 -0800557 for (i=0; i < VLAN_GROUP_ARRAY_SPLIT_PARTS; i++) {
558 grp->vlan_devices_arrays[i] = kzalloc(
559 sizeof(struct net_device *)*VLAN_GROUP_ARRAY_PART_LEN,
560 GFP_KERNEL);
561
562 if (!grp->vlan_devices_arrays[i])
563 goto out_free_arrays;
564 }
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 /* printk(KERN_ALERT "VLAN REGISTER: Allocated new group.\n"); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 grp->real_dev_ifindex = real_dev->ifindex;
568
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900569 hlist_add_head_rcu(&grp->hlist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 &vlan_group_hash[vlan_grp_hashfn(real_dev->ifindex)]);
571
572 if (real_dev->features & NETIF_F_HW_VLAN_RX)
573 real_dev->vlan_rx_register(real_dev, grp);
574 }
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900575
Dan Aloni5c15bde2007-03-02 20:44:51 -0800576 vlan_group_set_device(grp, VLAN_ID, new_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 if (vlan_proc_add_dev(new_dev)<0)/* create it's proc entry */
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900579 printk(KERN_WARNING "VLAN: failed to add proc entry for %s\n",
580 new_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
583 real_dev->vlan_rx_add_vid(real_dev, VLAN_ID);
584
585 rtnl_unlock();
586
587
588#ifdef VLAN_DEBUG
589 printk(VLAN_DBG "Allocated new device successfully, returning.\n");
590#endif
591 return new_dev;
592
Dan Aloni5c15bde2007-03-02 20:44:51 -0800593out_free_arrays:
594 vlan_group_free(grp);
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596out_free_unregister:
597 unregister_netdev(new_dev);
598 goto out_unlock;
599
600out_free_newdev:
601 free_netdev(new_dev);
602
603out_unlock:
604 rtnl_unlock();
605
606out_put_dev:
607 dev_put(real_dev);
608
609out_ret_null:
610 return NULL;
611}
612
613static int vlan_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
614{
615 struct net_device *dev = ptr;
616 struct vlan_group *grp = __vlan_find_group(dev->ifindex);
617 int i, flgs;
618 struct net_device *vlandev;
619
620 if (!grp)
621 goto out;
622
623 /* It is OK that we do not hold the group lock right now,
624 * as we run under the RTNL lock.
625 */
626
627 switch (event) {
628 case NETDEV_CHANGE:
629 /* Propagate real device state to vlan devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
Dan Aloni5c15bde2007-03-02 20:44:51 -0800631 vlandev = vlan_group_get_device(grp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (!vlandev)
633 continue;
634
Stefan Rompfddd7bf92006-03-20 17:11:41 -0800635 vlan_transfer_operstate(dev, vlandev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637 break;
638
639 case NETDEV_DOWN:
640 /* Put all VLANs for this dev in the down state too. */
641 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
Dan Aloni5c15bde2007-03-02 20:44:51 -0800642 vlandev = vlan_group_get_device(grp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (!vlandev)
644 continue;
645
646 flgs = vlandev->flags;
647 if (!(flgs & IFF_UP))
648 continue;
649
650 dev_change_flags(vlandev, flgs & ~IFF_UP);
651 }
652 break;
653
654 case NETDEV_UP:
655 /* Put all VLANs for this dev in the up 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;
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 flgs = vlandev->flags;
662 if (flgs & IFF_UP)
663 continue;
664
665 dev_change_flags(vlandev, flgs | IFF_UP);
666 }
667 break;
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 case NETDEV_UNREGISTER:
670 /* Delete all VLANs for this dev. */
671 for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
672 int ret;
673
Dan Aloni5c15bde2007-03-02 20:44:51 -0800674 vlandev = vlan_group_get_device(grp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (!vlandev)
676 continue;
677
678 ret = unregister_vlan_dev(dev,
679 VLAN_DEV_INFO(vlandev)->vlan_id);
680
681 unregister_netdevice(vlandev);
682
683 /* Group was destroyed? */
684 if (ret == 1)
685 break;
686 }
687 break;
688 };
689
690out:
691 return NOTIFY_DONE;
692}
693
694/*
695 * VLAN IOCTL handler.
696 * o execute requested action or pass command to the device driver
697 * arg is really a struct vlan_ioctl_args __user *.
698 */
699static int vlan_ioctl_handler(void __user *arg)
700{
701 int err = 0;
702 unsigned short vid = 0;
703 struct vlan_ioctl_args args;
704
705 if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args)))
706 return -EFAULT;
707
708 /* Null terminate this sucker, just in case. */
709 args.device1[23] = 0;
710 args.u.device2[23] = 0;
711
712#ifdef VLAN_DEBUG
713 printk(VLAN_DBG "%s: args.cmd: %x\n", __FUNCTION__, args.cmd);
714#endif
715
716 switch (args.cmd) {
717 case SET_VLAN_INGRESS_PRIORITY_CMD:
718 if (!capable(CAP_NET_ADMIN))
719 return -EPERM;
720 err = vlan_dev_set_ingress_priority(args.device1,
721 args.u.skb_priority,
722 args.vlan_qos);
723 break;
724
725 case SET_VLAN_EGRESS_PRIORITY_CMD:
726 if (!capable(CAP_NET_ADMIN))
727 return -EPERM;
728 err = vlan_dev_set_egress_priority(args.device1,
729 args.u.skb_priority,
730 args.vlan_qos);
731 break;
732
733 case SET_VLAN_FLAG_CMD:
734 if (!capable(CAP_NET_ADMIN))
735 return -EPERM;
736 err = vlan_dev_set_vlan_flag(args.device1,
737 args.u.flag,
738 args.vlan_qos);
739 break;
740
741 case SET_VLAN_NAME_TYPE_CMD:
742 if (!capable(CAP_NET_ADMIN))
743 return -EPERM;
744 if ((args.u.name_type >= 0) &&
745 (args.u.name_type < VLAN_NAME_TYPE_HIGHEST)) {
746 vlan_name_type = args.u.name_type;
747 err = 0;
748 } else {
749 err = -EINVAL;
750 }
751 break;
752
753 case ADD_VLAN_CMD:
754 if (!capable(CAP_NET_ADMIN))
755 return -EPERM;
756 /* we have been given the name of the Ethernet Device we want to
757 * talk to: args.dev1 We also have the
758 * VLAN ID: args.u.VID
759 */
760 if (register_vlan_device(args.device1, args.u.VID)) {
761 err = 0;
762 } else {
763 err = -EINVAL;
764 }
765 break;
766
767 case DEL_VLAN_CMD:
768 if (!capable(CAP_NET_ADMIN))
769 return -EPERM;
770 /* Here, the args.dev1 is the actual VLAN we want
771 * to get rid of.
772 */
773 err = unregister_vlan_device(args.device1);
774 break;
775
776 case GET_VLAN_INGRESS_PRIORITY_CMD:
777 /* TODO: Implement
778 err = vlan_dev_get_ingress_priority(args);
779 if (copy_to_user((void*)arg, &args,
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900780 sizeof(struct vlan_ioctl_args))) {
781 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
783 */
784 err = -EINVAL;
785 break;
786 case GET_VLAN_EGRESS_PRIORITY_CMD:
787 /* TODO: Implement
788 err = vlan_dev_get_egress_priority(args.device1, &(args.args);
789 if (copy_to_user((void*)arg, &args,
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900790 sizeof(struct vlan_ioctl_args))) {
791 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793 */
794 err = -EINVAL;
795 break;
796 case GET_VLAN_REALDEV_NAME_CMD:
797 err = vlan_dev_get_realdev_name(args.device1, args.u.device2);
Mika Kukkonen7eb1b3d2005-12-21 18:39:49 -0800798 if (err)
799 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (copy_to_user(arg, &args,
801 sizeof(struct vlan_ioctl_args))) {
802 err = -EFAULT;
803 }
804 break;
805
806 case GET_VLAN_VID_CMD:
807 err = vlan_dev_get_vid(args.device1, &vid);
Mika Kukkonen7eb1b3d2005-12-21 18:39:49 -0800808 if (err)
809 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 args.u.VID = vid;
811 if (copy_to_user(arg, &args,
812 sizeof(struct vlan_ioctl_args))) {
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900813 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815 break;
816
817 default:
818 /* pass on to underlying device instead?? */
819 printk(VLAN_DBG "%s: Unknown VLAN CMD: %x \n",
820 __FUNCTION__, args.cmd);
821 return -EINVAL;
822 };
Mika Kukkonen7eb1b3d2005-12-21 18:39:49 -0800823out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return err;
825}
826
827MODULE_LICENSE("GPL");
828MODULE_VERSION(DRV_VERSION);