3 * Ethernet-type device handling.
5 * Authors: Ben Greear <greearb@candelatech.com>
6 * Please send support related email to: netdev@vger.kernel.org
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 <linux/capability.h>
22 #include <linux/module.h>
23 #include <linux/netdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/slab.h>
26 #include <linux/init.h>
27 #include <linux/rculist.h>
28 #include <net/p8022.h>
30 #include <linux/rtnetlink.h>
31 #include <linux/notifier.h>
32 #include <net/rtnetlink.h>
33 #include <net/net_namespace.h>
34 #include <net/netns/generic.h>
35 #include <asm/uaccess.h>
37 #include <linux/if_vlan.h>
41 #include <ctf/hndctf.h>
44 #define DRV_VERSION "1.8"
46 /* Global VLAN variables */
48 int vlan_net_id __read_mostly
;
50 /* Our listing of VLAN group(s) */
51 static struct hlist_head vlan_group_hash
[VLAN_GRP_HASH_SIZE
];
53 const char vlan_fullname
[] = "802.1Q VLAN Support";
54 const char vlan_version
[] = DRV_VERSION
;
55 static const char vlan_copyright
[] = "Ben Greear <greearb@candelatech.com>";
56 static const char vlan_buggyright
[] = "David S. Miller <davem@redhat.com>";
58 static struct packet_type vlan_packet_type __read_mostly
= {
59 .type
= cpu_to_be16(ETH_P_8021Q
),
60 .func
= vlan_skb_recv
, /* VLAN receive method */
63 /* End of global variables definitions. */
65 static inline unsigned int vlan_grp_hashfn(unsigned int idx
)
67 return ((idx
>> VLAN_GRP_HASH_SHIFT
) ^ idx
) & VLAN_GRP_HASH_MASK
;
70 /* Must be invoked with RCU read lock (no preempt) */
71 static struct vlan_group
*__vlan_find_group(struct net_device
*real_dev
)
73 struct vlan_group
*grp
;
75 int hash
= vlan_grp_hashfn(real_dev
->ifindex
);
77 hlist_for_each_entry_rcu(grp
, n
, &vlan_group_hash
[hash
], hlist
) {
78 if (grp
->real_dev
== real_dev
)
85 /* Find the protocol handler. Assumes VID < VLAN_VID_MASK.
87 * Must be invoked with RCU read lock (no preempt)
89 struct net_device
*__find_vlan_dev(struct net_device
*real_dev
, u16 vlan_id
)
91 struct vlan_group
*grp
= __vlan_find_group(real_dev
);
94 return vlan_group_get_device(grp
, vlan_id
);
99 static void vlan_group_free(struct vlan_group
*grp
)
103 for (i
= 0; i
< VLAN_GROUP_ARRAY_SPLIT_PARTS
; i
++)
104 kfree(grp
->vlan_devices_arrays
[i
]);
108 static struct vlan_group
*vlan_group_alloc(struct net_device
*real_dev
)
110 struct vlan_group
*grp
;
112 grp
= kzalloc(sizeof(struct vlan_group
), GFP_KERNEL
);
116 grp
->real_dev
= real_dev
;
117 hlist_add_head_rcu(&grp
->hlist
,
118 &vlan_group_hash
[vlan_grp_hashfn(real_dev
->ifindex
)]);
122 static int vlan_group_prealloc_vid(struct vlan_group
*vg
, u16 vlan_id
)
124 struct net_device
**array
;
129 array
= vg
->vlan_devices_arrays
[vlan_id
/ VLAN_GROUP_ARRAY_PART_LEN
];
133 size
= sizeof(struct net_device
*) * VLAN_GROUP_ARRAY_PART_LEN
;
134 array
= kzalloc(size
, GFP_KERNEL
);
138 vg
->vlan_devices_arrays
[vlan_id
/ VLAN_GROUP_ARRAY_PART_LEN
] = array
;
142 static void vlan_rcu_free(struct rcu_head
*rcu
)
144 vlan_group_free(container_of(rcu
, struct vlan_group
, rcu
));
147 void unregister_vlan_dev(struct net_device
*dev
, struct list_head
*head
)
149 struct vlan_dev_info
*vlan
= vlan_dev_info(dev
);
150 struct net_device
*real_dev
= vlan
->real_dev
;
151 const struct net_device_ops
*ops
= real_dev
->netdev_ops
;
152 struct vlan_group
*grp
;
153 u16 vlan_id
= vlan
->vlan_id
;
157 grp
= __vlan_find_group(real_dev
);
160 /* Take it out of our own structures, but be sure to interlock with
161 * HW accelerating devices or SW vlan input packet processing if
162 * VLAN is not 0 (leave it there for 802.1p).
164 if (vlan_id
&& (real_dev
->features
& NETIF_F_HW_VLAN_FILTER
))
165 ops
->ndo_vlan_rx_kill_vid(real_dev
, vlan_id
);
169 vlan_group_set_device(grp
, vlan_id
, NULL
);
174 (void)ctf_dev_vlan_delete(kcih
, real_dev
, vlan_id
);
177 unregister_netdevice_queue(dev
, head
);
179 /* If the group is now empty, kill off the group. */
180 if (grp
->nr_vlans
== 0) {
181 vlan_gvrp_uninit_applicant(real_dev
);
183 if (real_dev
->features
& NETIF_F_HW_VLAN_RX
)
184 ops
->ndo_vlan_rx_register(real_dev
, NULL
);
186 hlist_del_rcu(&grp
->hlist
);
188 /* Free the group, after all cpu's are done. */
189 call_rcu(&grp
->rcu
, vlan_rcu_free
);
192 /* Get rid of the vlan's reference to real_dev */
196 int vlan_check_real_dev(struct net_device
*real_dev
, u16 vlan_id
)
198 const char *name
= real_dev
->name
;
199 const struct net_device_ops
*ops
= real_dev
->netdev_ops
;
201 if (real_dev
->features
& NETIF_F_VLAN_CHALLENGED
) {
202 pr_info("8021q: VLANs not supported on %s\n", name
);
206 if ((real_dev
->features
& NETIF_F_HW_VLAN_RX
) && !ops
->ndo_vlan_rx_register
) {
207 pr_info("8021q: device %s has buggy VLAN hw accel\n", name
);
211 if ((real_dev
->features
& NETIF_F_HW_VLAN_FILTER
) &&
212 (!ops
->ndo_vlan_rx_add_vid
|| !ops
->ndo_vlan_rx_kill_vid
)) {
213 pr_info("8021q: Device %s has buggy VLAN hw accel\n", name
);
217 if (__find_vlan_dev(real_dev
, vlan_id
) != NULL
)
223 int register_vlan_dev(struct net_device
*dev
)
225 struct vlan_dev_info
*vlan
= vlan_dev_info(dev
);
226 struct net_device
*real_dev
= vlan
->real_dev
;
227 const struct net_device_ops
*ops
= real_dev
->netdev_ops
;
228 u16 vlan_id
= vlan
->vlan_id
;
229 struct vlan_group
*grp
, *ngrp
= NULL
;
232 grp
= __vlan_find_group(real_dev
);
234 ngrp
= grp
= vlan_group_alloc(real_dev
);
237 err
= vlan_gvrp_init_applicant(real_dev
);
242 err
= vlan_group_prealloc_vid(grp
, vlan_id
);
244 goto out_uninit_applicant
;
246 err
= register_netdevice(dev
);
248 goto out_uninit_applicant
;
250 /* Account for reference in struct vlan_dev_info */
253 netif_stacked_transfer_operstate(real_dev
, dev
);
254 linkwatch_fire_event(dev
); /* _MUST_ call rfc2863_policy() */
256 /* So, got the sucker initialized, now lets place
257 * it into our local structure.
259 vlan_group_set_device(grp
, vlan_id
, dev
);
262 dev
->features
|= (real_dev
->features
& (NETIF_F_SG
| NETIF_F_FRAGLIST
| NETIF_F_ALL_CSUM
));
264 if (ngrp
&& real_dev
->features
& NETIF_F_HW_VLAN_RX
)
265 ops
->ndo_vlan_rx_register(real_dev
, ngrp
);
266 if (real_dev
->features
& NETIF_F_HW_VLAN_FILTER
)
267 ops
->ndo_vlan_rx_add_vid(real_dev
, vlan_id
);
271 out_uninit_applicant
:
273 vlan_gvrp_uninit_applicant(real_dev
);
276 hlist_del_rcu(&ngrp
->hlist
);
277 /* Free the group, after all cpu's are done. */
278 call_rcu(&ngrp
->rcu
, vlan_rcu_free
);
283 /* Attach a VLAN device to a mac address (ie Ethernet Card).
284 * Returns 0 if the device was created or a negative error code otherwise.
286 static int register_vlan_device(struct net_device
*real_dev
, u16 vlan_id
)
288 struct net_device
*new_dev
;
289 struct net
*net
= dev_net(real_dev
);
290 struct vlan_net
*vn
= net_generic(net
, vlan_net_id
);
294 if (vlan_id
>= VLAN_VID_MASK
)
297 err
= vlan_check_real_dev(real_dev
, vlan_id
);
301 /* Gotta set up the fields for the device. */
302 switch (vn
->name_type
) {
303 case VLAN_NAME_TYPE_RAW_PLUS_VID
:
304 /* name will look like: eth1.0005 */
305 snprintf(name
, IFNAMSIZ
, "%s.%.4i", real_dev
->name
, vlan_id
);
307 case VLAN_NAME_TYPE_PLUS_VID_NO_PAD
:
308 /* Put our vlan.VID in the name.
309 * Name will look like: vlan5
311 snprintf(name
, IFNAMSIZ
, "vlan%i", vlan_id
);
313 case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD
:
314 /* Put our vlan.VID in the name.
315 * Name will look like: eth0.5
317 snprintf(name
, IFNAMSIZ
, "%s.%i", real_dev
->name
, vlan_id
);
319 case VLAN_NAME_TYPE_PLUS_VID
:
320 /* Put our vlan.VID in the name.
321 * Name will look like: vlan0005
324 snprintf(name
, IFNAMSIZ
, "vlan%.4i", vlan_id
);
327 new_dev
= alloc_netdev_mq(sizeof(struct vlan_dev_info
), name
,
328 vlan_setup
, real_dev
->num_tx_queues
);
333 new_dev
->real_num_tx_queues
= real_dev
->real_num_tx_queues
;
334 dev_net_set(new_dev
, net
);
335 /* need 4 bytes for extra VLAN header info,
336 * hope the underlying device can handle it.
338 new_dev
->mtu
= real_dev
->mtu
;
340 vlan_dev_info(new_dev
)->vlan_id
= vlan_id
;
341 vlan_dev_info(new_dev
)->real_dev
= real_dev
;
342 vlan_dev_info(new_dev
)->dent
= NULL
;
343 vlan_dev_info(new_dev
)->flags
= VLAN_FLAG_REORDER_HDR
;
345 new_dev
->rtnl_link_ops
= &vlan_link_ops
;
346 err
= register_vlan_dev(new_dev
);
348 goto out_free_newdev
;
351 (void)ctf_dev_vlan_add(kcih
, real_dev
, vlan_id
, new_dev
);
357 free_netdev(new_dev
);
361 static void vlan_sync_address(struct net_device
*dev
,
362 struct net_device
*vlandev
)
364 struct vlan_dev_info
*vlan
= vlan_dev_info(vlandev
);
366 /* May be called without an actual change */
367 if (!compare_ether_addr(vlan
->real_dev_addr
, dev
->dev_addr
))
370 /* vlan address was different from the old address and is equal to
372 if (compare_ether_addr(vlandev
->dev_addr
, vlan
->real_dev_addr
) &&
373 !compare_ether_addr(vlandev
->dev_addr
, dev
->dev_addr
))
374 dev_uc_del(dev
, vlandev
->dev_addr
);
376 /* vlan address was equal to the old address and is different from
378 if (!compare_ether_addr(vlandev
->dev_addr
, vlan
->real_dev_addr
) &&
379 compare_ether_addr(vlandev
->dev_addr
, dev
->dev_addr
))
380 dev_uc_add(dev
, vlandev
->dev_addr
);
382 memcpy(vlan
->real_dev_addr
, dev
->dev_addr
, ETH_ALEN
);
385 static void vlan_transfer_features(struct net_device
*dev
,
386 struct net_device
*vlandev
)
388 unsigned long old_features
= vlandev
->features
;
390 vlandev
->features
&= ~dev
->vlan_features
;
391 vlandev
->features
|= dev
->features
& dev
->vlan_features
;
392 vlandev
->gso_max_size
= dev
->gso_max_size
;
393 #if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
394 vlandev
->fcoe_ddp_xid
= dev
->fcoe_ddp_xid
;
396 vlandev
->real_num_tx_queues
= dev
->real_num_tx_queues
;
397 BUG_ON(vlandev
->real_num_tx_queues
> vlandev
->num_tx_queues
);
399 if (old_features
!= vlandev
->features
)
400 netdev_features_change(vlandev
);
403 static void __vlan_device_event(struct net_device
*dev
, unsigned long event
)
406 case NETDEV_CHANGENAME
:
407 vlan_proc_rem_dev(dev
);
408 if (vlan_proc_add_dev(dev
) < 0)
409 pr_warning("8021q: failed to change proc name for %s\n",
412 case NETDEV_REGISTER
:
413 if (vlan_proc_add_dev(dev
) < 0)
414 pr_warning("8021q: failed to add proc entry for %s\n",
417 case NETDEV_UNREGISTER
:
418 vlan_proc_rem_dev(dev
);
423 static int vlan_device_event(struct notifier_block
*unused
, unsigned long event
,
426 struct net_device
*dev
= ptr
;
427 struct vlan_group
*grp
;
429 struct net_device
*vlandev
;
430 struct vlan_dev_info
*vlan
;
433 if (is_vlan_dev(dev
))
434 __vlan_device_event(dev
, event
);
436 if ((event
== NETDEV_UP
) &&
437 (dev
->features
& NETIF_F_HW_VLAN_FILTER
) &&
438 dev
->netdev_ops
->ndo_vlan_rx_add_vid
) {
439 pr_info("8021q: adding VLAN 0 to HW filter on device %s\n",
441 dev
->netdev_ops
->ndo_vlan_rx_add_vid(dev
, 0);
444 grp
= __vlan_find_group(dev
);
448 /* It is OK that we do not hold the group lock right now,
449 * as we run under the RTNL lock.
454 /* Propagate real device state to vlan devices */
455 for (i
= 0; i
< VLAN_GROUP_ARRAY_LEN
; i
++) {
456 vlandev
= vlan_group_get_device(grp
, i
);
460 netif_stacked_transfer_operstate(dev
, vlandev
);
464 case NETDEV_CHANGEADDR
:
465 /* Adjust unicast filters on underlying device */
466 for (i
= 0; i
< VLAN_GROUP_ARRAY_LEN
; i
++) {
467 vlandev
= vlan_group_get_device(grp
, i
);
471 flgs
= vlandev
->flags
;
472 if (!(flgs
& IFF_UP
))
475 vlan_sync_address(dev
, vlandev
);
479 case NETDEV_CHANGEMTU
:
480 for (i
= 0; i
< VLAN_GROUP_ARRAY_LEN
; i
++) {
481 vlandev
= vlan_group_get_device(grp
, i
);
485 if (vlandev
->mtu
<= dev
->mtu
)
488 dev_set_mtu(vlandev
, dev
->mtu
);
492 case NETDEV_FEAT_CHANGE
:
493 /* Propagate device features to underlying device */
494 for (i
= 0; i
< VLAN_GROUP_ARRAY_LEN
; i
++) {
495 vlandev
= vlan_group_get_device(grp
, i
);
499 vlan_transfer_features(dev
, vlandev
);
505 /* Put all VLANs for this dev in the down state too. */
506 for (i
= 0; i
< VLAN_GROUP_ARRAY_LEN
; i
++) {
507 vlandev
= vlan_group_get_device(grp
, i
);
511 flgs
= vlandev
->flags
;
512 if (!(flgs
& IFF_UP
))
515 vlan
= vlan_dev_info(vlandev
);
516 if (!(vlan
->flags
& VLAN_FLAG_LOOSE_BINDING
))
517 dev_change_flags(vlandev
, flgs
& ~IFF_UP
);
518 netif_stacked_transfer_operstate(dev
, vlandev
);
523 /* Put all VLANs for this dev in the up state too. */
524 for (i
= 0; i
< VLAN_GROUP_ARRAY_LEN
; i
++) {
525 vlandev
= vlan_group_get_device(grp
, i
);
529 flgs
= vlandev
->flags
;
533 vlan
= vlan_dev_info(vlandev
);
534 if (!(vlan
->flags
& VLAN_FLAG_LOOSE_BINDING
))
535 dev_change_flags(vlandev
, flgs
| IFF_UP
);
536 netif_stacked_transfer_operstate(dev
, vlandev
);
540 case NETDEV_UNREGISTER
:
541 /* Delete all VLANs for this dev. */
544 for (i
= 0; i
< VLAN_GROUP_ARRAY_LEN
; i
++) {
545 vlandev
= vlan_group_get_device(grp
, i
);
549 /* unregistration of last vlan destroys group, abort
551 if (grp
->nr_vlans
== 1)
552 i
= VLAN_GROUP_ARRAY_LEN
;
554 unregister_vlan_dev(vlandev
, &list
);
556 unregister_netdevice_many(&list
);
559 case NETDEV_PRE_TYPE_CHANGE
:
560 /* Forbid underlaying device to change its type. */
568 static struct notifier_block vlan_notifier_block __read_mostly
= {
569 .notifier_call
= vlan_device_event
,
573 * VLAN IOCTL handler.
574 * o execute requested action or pass command to the device driver
575 * arg is really a struct vlan_ioctl_args __user *.
577 static int vlan_ioctl_handler(struct net
*net
, void __user
*arg
)
580 struct vlan_ioctl_args args
;
581 struct net_device
*dev
= NULL
;
583 if (copy_from_user(&args
, arg
, sizeof(struct vlan_ioctl_args
)))
586 /* Null terminate this sucker, just in case. */
587 args
.device1
[23] = 0;
588 args
.u
.device2
[23] = 0;
593 case SET_VLAN_INGRESS_PRIORITY_CMD
:
594 case SET_VLAN_EGRESS_PRIORITY_CMD
:
595 case SET_VLAN_FLAG_CMD
:
598 case GET_VLAN_REALDEV_NAME_CMD
:
599 case GET_VLAN_VID_CMD
:
601 dev
= __dev_get_by_name(net
, args
.device1
);
606 if (args
.cmd
!= ADD_VLAN_CMD
&& !is_vlan_dev(dev
))
611 case SET_VLAN_INGRESS_PRIORITY_CMD
:
613 if (!capable(CAP_NET_ADMIN
))
615 vlan_dev_set_ingress_priority(dev
,
621 case SET_VLAN_EGRESS_PRIORITY_CMD
:
623 if (!capable(CAP_NET_ADMIN
))
625 err
= vlan_dev_set_egress_priority(dev
,
630 case SET_VLAN_FLAG_CMD
:
632 if (!capable(CAP_NET_ADMIN
))
634 err
= vlan_dev_change_flags(dev
,
635 args
.vlan_qos
? args
.u
.flag
: 0,
639 case SET_VLAN_NAME_TYPE_CMD
:
641 if (!capable(CAP_NET_ADMIN
))
643 if ((args
.u
.name_type
>= 0) &&
644 (args
.u
.name_type
< VLAN_NAME_TYPE_HIGHEST
)) {
647 vn
= net_generic(net
, vlan_net_id
);
648 vn
->name_type
= args
.u
.name_type
;
657 if (!capable(CAP_NET_ADMIN
))
659 err
= register_vlan_device(dev
, args
.u
.VID
);
664 if (!capable(CAP_NET_ADMIN
))
666 unregister_vlan_dev(dev
, NULL
);
670 case GET_VLAN_REALDEV_NAME_CMD
:
672 vlan_dev_get_realdev_name(dev
, args
.u
.device2
);
673 if (copy_to_user(arg
, &args
,
674 sizeof(struct vlan_ioctl_args
)))
678 case GET_VLAN_VID_CMD
:
680 args
.u
.VID
= vlan_dev_vlan_id(dev
);
681 if (copy_to_user(arg
, &args
,
682 sizeof(struct vlan_ioctl_args
)))
695 static int __net_init
vlan_init_net(struct net
*net
)
697 struct vlan_net
*vn
= net_generic(net
, vlan_net_id
);
700 vn
->name_type
= VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD
;
702 err
= vlan_proc_init(net
);
707 static void __net_exit
vlan_exit_net(struct net
*net
)
709 vlan_proc_cleanup(net
);
712 static struct pernet_operations vlan_net_ops
= {
713 .init
= vlan_init_net
,
714 .exit
= vlan_exit_net
,
716 .size
= sizeof(struct vlan_net
),
719 static int __init
vlan_proto_init(void)
723 pr_info("%s v%s %s\n", vlan_fullname
, vlan_version
, vlan_copyright
);
724 pr_info("All bugs added by %s\n", vlan_buggyright
);
726 err
= register_pernet_subsys(&vlan_net_ops
);
730 err
= register_netdevice_notifier(&vlan_notifier_block
);
734 err
= vlan_gvrp_init();
738 err
= vlan_netlink_init();
742 dev_add_pack(&vlan_packet_type
);
743 vlan_ioctl_set(vlan_ioctl_handler
);
749 unregister_netdevice_notifier(&vlan_notifier_block
);
751 unregister_pernet_subsys(&vlan_net_ops
);
756 static void __exit
vlan_cleanup_module(void)
760 vlan_ioctl_set(NULL
);
763 unregister_netdevice_notifier(&vlan_notifier_block
);
765 dev_remove_pack(&vlan_packet_type
);
767 /* This table must be empty if there are no module references left. */
768 for (i
= 0; i
< VLAN_GRP_HASH_SIZE
; i
++)
769 BUG_ON(!hlist_empty(&vlan_group_hash
[i
]));
771 unregister_pernet_subsys(&vlan_net_ops
);
772 rcu_barrier(); /* Wait for completion of call_rcu()'s */
777 module_init(vlan_proto_init
);
778 module_exit(vlan_cleanup_module
);
780 MODULE_LICENSE("GPL");
781 MODULE_VERSION(DRV_VERSION
);