3 * Ethernet-type device handling.
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
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>
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.
21 #include <asm/uaccess.h> /* for copy_from_user */
22 #include <linux/capability.h>
23 #include <linux/module.h>
24 #include <linux/netdevice.h>
25 #include <linux/skbuff.h>
26 #include <net/datalink.h>
29 #include <linux/init.h>
30 #include <net/p8022.h>
32 #include <linux/rtnetlink.h>
33 #include <linux/notifier.h>
35 #include <linux/if_vlan.h>
39 #define DRV_VERSION "1.8"
41 /* Global VLAN variables */
43 /* Our listing of VLAN group(s) */
44 static 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)
47 static char vlan_fullname
[] = "802.1Q VLAN Support";
48 static char vlan_version
[] = DRV_VERSION
;
49 static char vlan_copyright
[] = "Ben Greear <greearb@candelatech.com>";
50 static char vlan_buggyright
[] = "David S. Miller <davem@redhat.com>";
52 static int vlan_device_event(struct notifier_block
*, unsigned long, void *);
53 static int vlan_ioctl_handler(void __user
*);
54 static int unregister_vlan_dev(struct net_device
*, unsigned short );
56 static struct notifier_block vlan_notifier_block
= {
57 .notifier_call
= vlan_device_event
,
60 /* These may be changed at run-time through IOCTLs */
62 /* Determines interface naming scheme. */
63 unsigned short vlan_name_type
= VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD
;
65 static struct packet_type vlan_packet_type
= {
66 .type
= __constant_htons(ETH_P_8021Q
),
67 .func
= vlan_skb_recv
, /* VLAN receive method */
70 /* End of global variables definitions. */
73 * Function vlan_proto_init (pro)
75 * Initialize VLAN protocol layer,
78 static int __init
vlan_proto_init(void)
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",
87 /* proc file system initialization */
88 err
= vlan_proc_init();
91 "%s %s: can't create entry in proc filesystem!\n",
92 __FUNCTION__
, VLAN_NAME
);
96 dev_add_pack(&vlan_packet_type
);
98 /* Register us to receive netdevice events */
99 err
= register_netdevice_notifier(&vlan_notifier_block
);
103 err
= vlan_netlink_init();
107 vlan_ioctl_set(vlan_ioctl_handler
);
111 unregister_netdevice_notifier(&vlan_notifier_block
);
114 dev_remove_pack(&vlan_packet_type
);
118 /* Cleanup all vlan devices
119 * Note: devices that have been registered that but not
120 * brought up will exist but have no module ref count.
122 static void __exit
vlan_cleanup_devices(void)
124 struct net_device
*dev
, *nxt
;
127 for_each_netdev_safe(dev
, nxt
) {
128 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
);
132 unregister_netdevice(dev
);
139 * Module 'remove' entry point.
140 * o delete /proc/net/router directory and static entries.
142 static void __exit
vlan_cleanup_module(void)
147 vlan_ioctl_set(NULL
);
149 /* Un-register us from receiving netdevice events */
150 unregister_netdevice_notifier(&vlan_notifier_block
);
152 dev_remove_pack(&vlan_packet_type
);
153 vlan_cleanup_devices();
155 /* This table must be empty if there are no module
158 for (i
= 0; i
< VLAN_GRP_HASH_SIZE
; i
++) {
159 BUG_ON(!hlist_empty(&vlan_group_hash
[i
]));
166 module_init(vlan_proto_init
);
167 module_exit(vlan_cleanup_module
);
169 /* Must be invoked with RCU read lock (no preempt) */
170 static struct vlan_group
*__vlan_find_group(int real_dev_ifindex
)
172 struct vlan_group
*grp
;
173 struct hlist_node
*n
;
174 int hash
= vlan_grp_hashfn(real_dev_ifindex
);
176 hlist_for_each_entry_rcu(grp
, n
, &vlan_group_hash
[hash
], hlist
) {
177 if (grp
->real_dev_ifindex
== real_dev_ifindex
)
184 /* Find the protocol handler. Assumes VID < VLAN_VID_MASK.
186 * Must be invoked with RCU read lock (no preempt)
188 struct net_device
*__find_vlan_dev(struct net_device
*real_dev
,
191 struct vlan_group
*grp
= __vlan_find_group(real_dev
->ifindex
);
194 return vlan_group_get_device(grp
, VID
);
199 static void vlan_group_free(struct vlan_group
*grp
)
203 for (i
=0; i
< VLAN_GROUP_ARRAY_SPLIT_PARTS
; i
++)
204 kfree(grp
->vlan_devices_arrays
[i
]);
208 static struct vlan_group
*vlan_group_alloc(int ifindex
)
210 struct vlan_group
*grp
;
214 grp
= kzalloc(sizeof(struct vlan_group
), GFP_KERNEL
);
218 size
= sizeof(struct net_device
*) * VLAN_GROUP_ARRAY_PART_LEN
;
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
])
226 grp
->real_dev_ifindex
= ifindex
;
227 hlist_add_head_rcu(&grp
->hlist
,
228 &vlan_group_hash
[vlan_grp_hashfn(ifindex
)]);
232 vlan_group_free(grp
);
236 static void vlan_rcu_free(struct rcu_head
*rcu
)
238 vlan_group_free(container_of(rcu
, struct vlan_group
, rcu
));
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.
246 * The RTNL lock must be held.
248 static int unregister_vlan_dev(struct net_device
*real_dev
,
249 unsigned short vlan_id
)
251 struct net_device
*dev
= NULL
;
252 int real_dev_ifindex
= real_dev
->ifindex
;
253 struct vlan_group
*grp
;
257 printk(VLAN_DBG
"%s: VID: %i\n", __FUNCTION__
, vlan_id
);
261 if (vlan_id
>= VLAN_VID_MASK
)
265 grp
= __vlan_find_group(real_dev_ifindex
);
270 dev
= vlan_group_get_device(grp
, vlan_id
);
272 /* Remove proc entry */
273 vlan_proc_rem_dev(dev
);
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.
279 if (real_dev
->features
& NETIF_F_HW_VLAN_FILTER
)
280 real_dev
->vlan_rx_kill_vid(real_dev
, vlan_id
);
282 vlan_group_set_device(grp
, vlan_id
, NULL
);
286 /* Caller unregisters (and if necessary, puts)
287 * VLAN device, but we get rid of the reference to
292 /* If the group is now empty, kill off the
295 for (i
= 0; i
< VLAN_VID_MASK
; i
++)
296 if (vlan_group_get_device(grp
, i
))
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
);
303 hlist_del_rcu(&grp
->hlist
);
305 /* Free the group, after all cpu's are done. */
306 call_rcu(&grp
->rcu
, vlan_rcu_free
);
317 int unregister_vlan_device(struct net_device
*dev
)
321 ret
= unregister_vlan_dev(VLAN_DEV_INFO(dev
)->real_dev
,
322 VLAN_DEV_INFO(dev
)->vlan_id
);
323 unregister_netdevice(dev
);
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.
335 static struct lock_class_key vlan_netdev_xmit_lock_key
;
337 static int vlan_dev_init(struct net_device
*dev
)
339 struct net_device
*real_dev
= VLAN_DEV_INFO(dev
)->real_dev
;
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
);
348 /* TODO: maybe just assign it to be ETHERNET? */
349 dev
->type
= real_dev
->type
;
351 memcpy(dev
->broadcast
, real_dev
->broadcast
, real_dev
->addr_len
);
352 memcpy(dev
->dev_addr
, real_dev
->dev_addr
, real_dev
->addr_len
);
353 dev
->addr_len
= real_dev
->addr_len
;
355 if (real_dev
->features
& NETIF_F_HW_VLAN_TX
) {
356 dev
->hard_header
= real_dev
->hard_header
;
357 dev
->hard_header_len
= real_dev
->hard_header_len
;
358 dev
->hard_start_xmit
= vlan_dev_hwaccel_hard_start_xmit
;
359 dev
->rebuild_header
= real_dev
->rebuild_header
;
361 dev
->hard_header
= vlan_dev_hard_header
;
362 dev
->hard_header_len
= real_dev
->hard_header_len
+ VLAN_HLEN
;
363 dev
->hard_start_xmit
= vlan_dev_hard_start_xmit
;
364 dev
->rebuild_header
= vlan_dev_rebuild_header
;
366 dev
->hard_header_parse
= real_dev
->hard_header_parse
;
368 lockdep_set_class(&dev
->_xmit_lock
, &vlan_netdev_xmit_lock_key
);
372 void vlan_setup(struct net_device
*new_dev
)
374 SET_MODULE_OWNER(new_dev
);
376 /* new_dev->ifindex = 0; it will be set when added to
378 * iflink is set as well.
380 new_dev
->get_stats
= vlan_dev_get_stats
;
382 /* Make this thing known as a VLAN device */
383 new_dev
->priv_flags
|= IFF_802_1Q_VLAN
;
385 /* Set us up to have no queue, as the underlying Hardware device
386 * can do all the queueing we could want.
388 new_dev
->tx_queue_len
= 0;
390 /* set up method calls */
391 new_dev
->change_mtu
= vlan_dev_change_mtu
;
392 new_dev
->init
= vlan_dev_init
;
393 new_dev
->open
= vlan_dev_open
;
394 new_dev
->stop
= vlan_dev_stop
;
395 new_dev
->set_mac_address
= vlan_dev_set_mac_address
;
396 new_dev
->set_multicast_list
= vlan_dev_set_multicast_list
;
397 new_dev
->destructor
= free_netdev
;
398 new_dev
->do_ioctl
= vlan_dev_ioctl
;
401 static void vlan_transfer_operstate(const struct net_device
*dev
, struct net_device
*vlandev
)
403 /* Have to respect userspace enforced dormant state
404 * of real device, also must allow supplicant running
407 if (dev
->operstate
== IF_OPER_DORMANT
)
408 netif_dormant_on(vlandev
);
410 netif_dormant_off(vlandev
);
412 if (netif_carrier_ok(dev
)) {
413 if (!netif_carrier_ok(vlandev
))
414 netif_carrier_on(vlandev
);
416 if (netif_carrier_ok(vlandev
))
417 netif_carrier_off(vlandev
);
421 int vlan_check_real_dev(struct net_device
*real_dev
, unsigned short vlan_id
)
423 if (real_dev
->features
& NETIF_F_VLAN_CHALLENGED
) {
424 printk(VLAN_DBG
"%s: VLANs not supported on %s.\n",
425 __FUNCTION__
, real_dev
->name
);
429 if ((real_dev
->features
& NETIF_F_HW_VLAN_RX
) &&
430 !real_dev
->vlan_rx_register
) {
431 printk(VLAN_DBG
"%s: Device %s has buggy VLAN hw accel.\n",
432 __FUNCTION__
, real_dev
->name
);
436 if ((real_dev
->features
& NETIF_F_HW_VLAN_FILTER
) &&
437 (!real_dev
->vlan_rx_add_vid
|| !real_dev
->vlan_rx_kill_vid
)) {
438 printk(VLAN_DBG
"%s: Device %s has buggy VLAN hw accel.\n",
439 __FUNCTION__
, real_dev
->name
);
443 /* The real device must be up and operating in order to
444 * assosciate a VLAN device with it.
446 if (!(real_dev
->flags
& IFF_UP
))
449 if (__find_vlan_dev(real_dev
, vlan_id
) != NULL
) {
450 /* was already registered. */
451 printk(VLAN_DBG
"%s: ALREADY had VLAN registered\n", __FUNCTION__
);
458 int register_vlan_dev(struct net_device
*dev
)
460 struct vlan_dev_info
*vlan
= VLAN_DEV_INFO(dev
);
461 struct net_device
*real_dev
= vlan
->real_dev
;
462 unsigned short vlan_id
= vlan
->vlan_id
;
463 struct vlan_group
*grp
, *ngrp
= NULL
;
466 grp
= __vlan_find_group(real_dev
->ifindex
);
468 ngrp
= grp
= vlan_group_alloc(real_dev
->ifindex
);
473 err
= register_netdevice(dev
);
477 /* Account for reference in struct vlan_dev_info */
480 vlan_transfer_operstate(real_dev
, dev
);
481 linkwatch_fire_event(dev
); /* _MUST_ call rfc2863_policy() */
483 /* So, got the sucker initialized, now lets place
484 * it into our local structure.
486 vlan_group_set_device(grp
, vlan_id
, dev
);
487 if (ngrp
&& real_dev
->features
& NETIF_F_HW_VLAN_RX
)
488 real_dev
->vlan_rx_register(real_dev
, ngrp
);
489 if (real_dev
->features
& NETIF_F_HW_VLAN_FILTER
)
490 real_dev
->vlan_rx_add_vid(real_dev
, vlan_id
);
492 if (vlan_proc_add_dev(dev
) < 0)
493 printk(KERN_WARNING
"VLAN: failed to add proc entry for %s\n",
499 vlan_group_free(ngrp
);
503 /* Attach a VLAN device to a mac address (ie Ethernet Card).
504 * Returns 0 if the device was created or a negative error code otherwise.
506 static int register_vlan_device(struct net_device
*real_dev
,
507 unsigned short VLAN_ID
)
509 struct net_device
*new_dev
;
514 printk(VLAN_DBG
"%s: if_name -:%s:- vid: %i\n",
515 __FUNCTION__
, eth_IF_name
, VLAN_ID
);
518 if (VLAN_ID
>= VLAN_VID_MASK
)
521 err
= vlan_check_real_dev(real_dev
, VLAN_ID
);
525 /* Gotta set up the fields for the device. */
527 printk(VLAN_DBG
"About to allocate name, vlan_name_type: %i\n",
530 switch (vlan_name_type
) {
531 case VLAN_NAME_TYPE_RAW_PLUS_VID
:
532 /* name will look like: eth1.0005 */
533 snprintf(name
, IFNAMSIZ
, "%s.%.4i", real_dev
->name
, VLAN_ID
);
535 case VLAN_NAME_TYPE_PLUS_VID_NO_PAD
:
536 /* Put our vlan.VID in the name.
537 * Name will look like: vlan5
539 snprintf(name
, IFNAMSIZ
, "vlan%i", VLAN_ID
);
541 case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD
:
542 /* Put our vlan.VID in the name.
543 * Name will look like: eth0.5
545 snprintf(name
, IFNAMSIZ
, "%s.%i", real_dev
->name
, VLAN_ID
);
547 case VLAN_NAME_TYPE_PLUS_VID
:
548 /* Put our vlan.VID in the name.
549 * Name will look like: vlan0005
552 snprintf(name
, IFNAMSIZ
, "vlan%.4i", VLAN_ID
);
555 new_dev
= alloc_netdev(sizeof(struct vlan_dev_info
), name
,
561 /* need 4 bytes for extra VLAN header info,
562 * hope the underlying device can handle it.
564 new_dev
->mtu
= real_dev
->mtu
;
567 printk(VLAN_DBG
"Allocated new name -:%s:-\n", new_dev
->name
);
568 VLAN_MEM_DBG("new_dev->priv malloc, addr: %p size: %i\n",
570 sizeof(struct vlan_dev_info
));
573 VLAN_DEV_INFO(new_dev
)->vlan_id
= VLAN_ID
; /* 1 through VLAN_VID_MASK */
574 VLAN_DEV_INFO(new_dev
)->real_dev
= real_dev
;
575 VLAN_DEV_INFO(new_dev
)->dent
= NULL
;
576 VLAN_DEV_INFO(new_dev
)->flags
= VLAN_FLAG_REORDER_HDR
;
578 new_dev
->rtnl_link_ops
= &vlan_link_ops
;
579 err
= register_vlan_dev(new_dev
);
581 goto out_free_newdev
;
583 /* Account for reference in struct vlan_dev_info */
586 printk(VLAN_DBG
"Allocated new device successfully, returning.\n");
591 free_netdev(new_dev
);
595 static int vlan_device_event(struct notifier_block
*unused
, unsigned long event
, void *ptr
)
597 struct net_device
*dev
= ptr
;
598 struct vlan_group
*grp
= __vlan_find_group(dev
->ifindex
);
600 struct net_device
*vlandev
;
605 /* It is OK that we do not hold the group lock right now,
606 * as we run under the RTNL lock.
611 /* Propagate real device state to vlan devices */
612 for (i
= 0; i
< VLAN_GROUP_ARRAY_LEN
; i
++) {
613 vlandev
= vlan_group_get_device(grp
, i
);
617 vlan_transfer_operstate(dev
, vlandev
);
622 /* Put all VLANs for this dev in the down state too. */
623 for (i
= 0; i
< VLAN_GROUP_ARRAY_LEN
; i
++) {
624 vlandev
= vlan_group_get_device(grp
, i
);
628 flgs
= vlandev
->flags
;
629 if (!(flgs
& IFF_UP
))
632 dev_change_flags(vlandev
, flgs
& ~IFF_UP
);
637 /* Put all VLANs for this dev in the up state too. */
638 for (i
= 0; i
< VLAN_GROUP_ARRAY_LEN
; i
++) {
639 vlandev
= vlan_group_get_device(grp
, i
);
643 flgs
= vlandev
->flags
;
647 dev_change_flags(vlandev
, flgs
| IFF_UP
);
651 case NETDEV_UNREGISTER
:
652 /* Delete all VLANs for this dev. */
653 for (i
= 0; i
< VLAN_GROUP_ARRAY_LEN
; i
++) {
656 vlandev
= vlan_group_get_device(grp
, i
);
660 ret
= unregister_vlan_dev(dev
,
661 VLAN_DEV_INFO(vlandev
)->vlan_id
);
663 unregister_netdevice(vlandev
);
665 /* Group was destroyed? */
677 * VLAN IOCTL handler.
678 * o execute requested action or pass command to the device driver
679 * arg is really a struct vlan_ioctl_args __user *.
681 static int vlan_ioctl_handler(void __user
*arg
)
684 unsigned short vid
= 0;
685 struct vlan_ioctl_args args
;
686 struct net_device
*dev
= NULL
;
688 if (copy_from_user(&args
, arg
, sizeof(struct vlan_ioctl_args
)))
691 /* Null terminate this sucker, just in case. */
692 args
.device1
[23] = 0;
693 args
.u
.device2
[23] = 0;
696 printk(VLAN_DBG
"%s: args.cmd: %x\n", __FUNCTION__
, args
.cmd
);
702 case SET_VLAN_INGRESS_PRIORITY_CMD
:
703 case SET_VLAN_EGRESS_PRIORITY_CMD
:
704 case SET_VLAN_FLAG_CMD
:
707 case GET_VLAN_REALDEV_NAME_CMD
:
708 case GET_VLAN_VID_CMD
:
710 dev
= __dev_get_by_name(args
.device1
);
715 if (args
.cmd
!= ADD_VLAN_CMD
&&
716 !(dev
->priv_flags
& IFF_802_1Q_VLAN
))
721 case SET_VLAN_INGRESS_PRIORITY_CMD
:
723 if (!capable(CAP_NET_ADMIN
))
725 vlan_dev_set_ingress_priority(dev
,
730 case SET_VLAN_EGRESS_PRIORITY_CMD
:
732 if (!capable(CAP_NET_ADMIN
))
734 err
= vlan_dev_set_egress_priority(dev
,
739 case SET_VLAN_FLAG_CMD
:
741 if (!capable(CAP_NET_ADMIN
))
743 err
= vlan_dev_set_vlan_flag(dev
,
748 case SET_VLAN_NAME_TYPE_CMD
:
750 if (!capable(CAP_NET_ADMIN
))
752 if ((args
.u
.name_type
>= 0) &&
753 (args
.u
.name_type
< VLAN_NAME_TYPE_HIGHEST
)) {
754 vlan_name_type
= args
.u
.name_type
;
763 if (!capable(CAP_NET_ADMIN
))
765 err
= register_vlan_device(dev
, args
.u
.VID
);
770 if (!capable(CAP_NET_ADMIN
))
772 err
= unregister_vlan_device(dev
);
775 case GET_VLAN_INGRESS_PRIORITY_CMD
:
777 err = vlan_dev_get_ingress_priority(args);
778 if (copy_to_user((void*)arg, &args,
779 sizeof(struct vlan_ioctl_args))) {
785 case GET_VLAN_EGRESS_PRIORITY_CMD
:
787 err = vlan_dev_get_egress_priority(args.device1, &(args.args);
788 if (copy_to_user((void*)arg, &args,
789 sizeof(struct vlan_ioctl_args))) {
795 case GET_VLAN_REALDEV_NAME_CMD
:
796 vlan_dev_get_realdev_name(dev
, args
.u
.device2
);
797 if (copy_to_user(arg
, &args
,
798 sizeof(struct vlan_ioctl_args
))) {
803 case GET_VLAN_VID_CMD
:
804 vlan_dev_get_vid(dev
, &vid
);
806 if (copy_to_user(arg
, &args
,
807 sizeof(struct vlan_ioctl_args
))) {
813 /* pass on to underlying device instead?? */
814 printk(VLAN_DBG
"%s: Unknown VLAN CMD: %x \n",
815 __FUNCTION__
, args
.cmd
);
824 MODULE_LICENSE("GPL");
825 MODULE_VERSION(DRV_VERSION
);