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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/capability.h>
24 #include <linux/module.h>
25 #include <linux/netdevice.h>
26 #include <linux/skbuff.h>
27 #include <linux/slab.h>
28 #include <linux/init.h>
29 #include <linux/rculist.h>
30 #include <net/p8022.h>
32 #include <linux/rtnetlink.h>
33 #include <linux/notifier.h>
34 #include <net/rtnetlink.h>
35 #include <net/net_namespace.h>
36 #include <net/netns/generic.h>
37 #include <asm/uaccess.h>
39 #include <linux/if_vlan.h>
43 #define DRV_VERSION "1.8"
45 /* Global VLAN variables */
47 int vlan_net_id __read_mostly
;
49 const char vlan_fullname
[] = "802.1Q VLAN Support";
50 const char vlan_version
[] = DRV_VERSION
;
52 /* End of global variables definitions. */
54 static int vlan_group_prealloc_vid(struct vlan_group
*vg
, u16 vlan_id
)
56 struct net_device
**array
;
61 array
= vg
->vlan_devices_arrays
[vlan_id
/ VLAN_GROUP_ARRAY_PART_LEN
];
65 size
= sizeof(struct net_device
*) * VLAN_GROUP_ARRAY_PART_LEN
;
66 array
= kzalloc(size
, GFP_KERNEL
);
70 vg
->vlan_devices_arrays
[vlan_id
/ VLAN_GROUP_ARRAY_PART_LEN
] = array
;
74 void unregister_vlan_dev(struct net_device
*dev
, struct list_head
*head
)
76 struct vlan_dev_priv
*vlan
= vlan_dev_priv(dev
);
77 struct net_device
*real_dev
= vlan
->real_dev
;
78 struct vlan_info
*vlan_info
;
79 struct vlan_group
*grp
;
80 u16 vlan_id
= vlan
->vlan_id
;
84 vlan_info
= rtnl_dereference(real_dev
->vlan_info
);
87 grp
= &vlan_info
->grp
;
89 /* Take it out of our own structures, but be sure to interlock with
90 * HW accelerating devices or SW vlan input packet processing if
91 * VLAN is not 0 (leave it there for 802.1p).
94 vlan_vid_del(real_dev
, vlan_id
);
98 if (vlan
->flags
& VLAN_FLAG_MVRP
)
99 vlan_mvrp_request_leave(dev
);
100 if (vlan
->flags
& VLAN_FLAG_GVRP
)
101 vlan_gvrp_request_leave(dev
);
103 vlan_group_set_device(grp
, vlan_id
, NULL
);
104 /* Because unregister_netdevice_queue() makes sure at least one rcu
105 * grace period is respected before device freeing,
106 * we dont need to call synchronize_net() here.
108 unregister_netdevice_queue(dev
, head
);
110 netdev_upper_dev_unlink(real_dev
, dev
);
112 if (grp
->nr_vlan_devs
== 0) {
113 vlan_mvrp_uninit_applicant(real_dev
);
114 vlan_gvrp_uninit_applicant(real_dev
);
117 /* Get rid of the vlan's reference to real_dev */
121 int vlan_check_real_dev(struct net_device
*real_dev
, u16 vlan_id
)
123 const char *name
= real_dev
->name
;
125 if (real_dev
->features
& NETIF_F_VLAN_CHALLENGED
) {
126 pr_info("VLANs not supported on %s\n", name
);
130 if (vlan_find_dev(real_dev
, vlan_id
) != NULL
)
136 int register_vlan_dev(struct net_device
*dev
)
138 struct vlan_dev_priv
*vlan
= vlan_dev_priv(dev
);
139 struct net_device
*real_dev
= vlan
->real_dev
;
140 u16 vlan_id
= vlan
->vlan_id
;
141 struct vlan_info
*vlan_info
;
142 struct vlan_group
*grp
;
145 err
= vlan_vid_add(real_dev
, vlan_id
);
149 vlan_info
= rtnl_dereference(real_dev
->vlan_info
);
150 /* vlan_info should be there now. vlan_vid_add took care of it */
153 grp
= &vlan_info
->grp
;
154 if (grp
->nr_vlan_devs
== 0) {
155 err
= vlan_gvrp_init_applicant(real_dev
);
158 err
= vlan_mvrp_init_applicant(real_dev
);
160 goto out_uninit_gvrp
;
163 err
= vlan_group_prealloc_vid(grp
, vlan_id
);
165 goto out_uninit_mvrp
;
167 err
= netdev_upper_dev_link(real_dev
, dev
);
169 goto out_uninit_mvrp
;
171 err
= register_netdevice(dev
);
173 goto out_upper_dev_unlink
;
175 /* Account for reference in struct vlan_dev_priv */
178 netif_stacked_transfer_operstate(real_dev
, dev
);
179 linkwatch_fire_event(dev
); /* _MUST_ call rfc2863_policy() */
181 /* So, got the sucker initialized, now lets place
182 * it into our local structure.
184 vlan_group_set_device(grp
, vlan_id
, dev
);
189 out_upper_dev_unlink
:
190 netdev_upper_dev_unlink(real_dev
, dev
);
192 if (grp
->nr_vlan_devs
== 0)
193 vlan_mvrp_uninit_applicant(real_dev
);
195 if (grp
->nr_vlan_devs
== 0)
196 vlan_gvrp_uninit_applicant(real_dev
);
198 vlan_vid_del(real_dev
, vlan_id
);
202 /* Attach a VLAN device to a mac address (ie Ethernet Card).
203 * Returns 0 if the device was created or a negative error code otherwise.
205 static int register_vlan_device(struct net_device
*real_dev
, u16 vlan_id
)
207 struct net_device
*new_dev
;
208 struct net
*net
= dev_net(real_dev
);
209 struct vlan_net
*vn
= net_generic(net
, vlan_net_id
);
213 if (vlan_id
>= VLAN_VID_MASK
)
216 err
= vlan_check_real_dev(real_dev
, vlan_id
);
220 /* Gotta set up the fields for the device. */
221 switch (vn
->name_type
) {
222 case VLAN_NAME_TYPE_RAW_PLUS_VID
:
223 /* name will look like: eth1.0005 */
224 snprintf(name
, IFNAMSIZ
, "%s.%.4i", real_dev
->name
, vlan_id
);
226 case VLAN_NAME_TYPE_PLUS_VID_NO_PAD
:
227 /* Put our vlan.VID in the name.
228 * Name will look like: vlan5
230 snprintf(name
, IFNAMSIZ
, "vlan%i", vlan_id
);
232 case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD
:
233 /* Put our vlan.VID in the name.
234 * Name will look like: eth0.5
236 snprintf(name
, IFNAMSIZ
, "%s.%i", real_dev
->name
, vlan_id
);
238 case VLAN_NAME_TYPE_PLUS_VID
:
239 /* Put our vlan.VID in the name.
240 * Name will look like: vlan0005
243 snprintf(name
, IFNAMSIZ
, "vlan%.4i", vlan_id
);
246 new_dev
= alloc_netdev(sizeof(struct vlan_dev_priv
), name
, vlan_setup
);
251 dev_net_set(new_dev
, net
);
252 /* need 4 bytes for extra VLAN header info,
253 * hope the underlying device can handle it.
255 new_dev
->mtu
= real_dev
->mtu
;
256 new_dev
->priv_flags
|= (real_dev
->priv_flags
& IFF_UNICAST_FLT
);
258 vlan_dev_priv(new_dev
)->vlan_id
= vlan_id
;
259 vlan_dev_priv(new_dev
)->real_dev
= real_dev
;
260 vlan_dev_priv(new_dev
)->dent
= NULL
;
261 vlan_dev_priv(new_dev
)->flags
= VLAN_FLAG_REORDER_HDR
;
263 new_dev
->rtnl_link_ops
= &vlan_link_ops
;
264 err
= register_vlan_dev(new_dev
);
266 goto out_free_newdev
;
271 free_netdev(new_dev
);
275 static void vlan_sync_address(struct net_device
*dev
,
276 struct net_device
*vlandev
)
278 struct vlan_dev_priv
*vlan
= vlan_dev_priv(vlandev
);
280 /* May be called without an actual change */
281 if (ether_addr_equal(vlan
->real_dev_addr
, dev
->dev_addr
))
284 /* vlan address was different from the old address and is equal to
286 if (!ether_addr_equal(vlandev
->dev_addr
, vlan
->real_dev_addr
) &&
287 ether_addr_equal(vlandev
->dev_addr
, dev
->dev_addr
))
288 dev_uc_del(dev
, vlandev
->dev_addr
);
290 /* vlan address was equal to the old address and is different from
292 if (ether_addr_equal(vlandev
->dev_addr
, vlan
->real_dev_addr
) &&
293 !ether_addr_equal(vlandev
->dev_addr
, dev
->dev_addr
))
294 dev_uc_add(dev
, vlandev
->dev_addr
);
296 memcpy(vlan
->real_dev_addr
, dev
->dev_addr
, ETH_ALEN
);
299 static void vlan_transfer_features(struct net_device
*dev
,
300 struct net_device
*vlandev
)
302 vlandev
->gso_max_size
= dev
->gso_max_size
;
304 if (dev
->features
& NETIF_F_HW_VLAN_TX
)
305 vlandev
->hard_header_len
= dev
->hard_header_len
;
307 vlandev
->hard_header_len
= dev
->hard_header_len
+ VLAN_HLEN
;
309 #if IS_ENABLED(CONFIG_FCOE)
310 vlandev
->fcoe_ddp_xid
= dev
->fcoe_ddp_xid
;
313 netdev_update_features(vlandev
);
316 static void __vlan_device_event(struct net_device
*dev
, unsigned long event
)
319 case NETDEV_CHANGENAME
:
320 vlan_proc_rem_dev(dev
);
321 if (vlan_proc_add_dev(dev
) < 0)
322 pr_warn("failed to change proc name for %s\n",
325 case NETDEV_REGISTER
:
326 if (vlan_proc_add_dev(dev
) < 0)
327 pr_warn("failed to add proc entry for %s\n", dev
->name
);
329 case NETDEV_UNREGISTER
:
330 vlan_proc_rem_dev(dev
);
335 static int vlan_device_event(struct notifier_block
*unused
, unsigned long event
,
338 struct net_device
*dev
= ptr
;
339 struct vlan_group
*grp
;
340 struct vlan_info
*vlan_info
;
342 struct net_device
*vlandev
;
343 struct vlan_dev_priv
*vlan
;
346 if (is_vlan_dev(dev
))
347 __vlan_device_event(dev
, event
);
349 if ((event
== NETDEV_UP
) &&
350 (dev
->features
& NETIF_F_HW_VLAN_FILTER
)) {
351 pr_info("adding VLAN 0 to HW filter on device %s\n",
353 vlan_vid_add(dev
, 0);
356 vlan_info
= rtnl_dereference(dev
->vlan_info
);
359 grp
= &vlan_info
->grp
;
361 /* It is OK that we do not hold the group lock right now,
362 * as we run under the RTNL lock.
367 /* Propagate real device state to vlan devices */
368 for (i
= 0; i
< VLAN_N_VID
; i
++) {
369 vlandev
= vlan_group_get_device(grp
, i
);
373 netif_stacked_transfer_operstate(dev
, vlandev
);
377 case NETDEV_CHANGEADDR
:
378 /* Adjust unicast filters on underlying device */
379 for (i
= 0; i
< VLAN_N_VID
; i
++) {
380 vlandev
= vlan_group_get_device(grp
, i
);
384 flgs
= vlandev
->flags
;
385 if (!(flgs
& IFF_UP
))
388 vlan_sync_address(dev
, vlandev
);
392 case NETDEV_CHANGEMTU
:
393 for (i
= 0; i
< VLAN_N_VID
; i
++) {
394 vlandev
= vlan_group_get_device(grp
, i
);
398 if (vlandev
->mtu
<= dev
->mtu
)
401 dev_set_mtu(vlandev
, dev
->mtu
);
405 case NETDEV_FEAT_CHANGE
:
406 /* Propagate device features to underlying device */
407 for (i
= 0; i
< VLAN_N_VID
; i
++) {
408 vlandev
= vlan_group_get_device(grp
, i
);
412 vlan_transfer_features(dev
, vlandev
);
418 if (dev
->features
& NETIF_F_HW_VLAN_FILTER
)
419 vlan_vid_del(dev
, 0);
421 /* Put all VLANs for this dev in the down state too. */
422 for (i
= 0; i
< VLAN_N_VID
; i
++) {
423 vlandev
= vlan_group_get_device(grp
, i
);
427 flgs
= vlandev
->flags
;
428 if (!(flgs
& IFF_UP
))
431 vlan
= vlan_dev_priv(vlandev
);
432 if (!(vlan
->flags
& VLAN_FLAG_LOOSE_BINDING
))
433 dev_change_flags(vlandev
, flgs
& ~IFF_UP
);
434 netif_stacked_transfer_operstate(dev
, vlandev
);
439 /* Put all VLANs for this dev in the up state too. */
440 for (i
= 0; i
< VLAN_N_VID
; i
++) {
441 vlandev
= vlan_group_get_device(grp
, i
);
445 flgs
= vlandev
->flags
;
449 vlan
= vlan_dev_priv(vlandev
);
450 if (!(vlan
->flags
& VLAN_FLAG_LOOSE_BINDING
))
451 dev_change_flags(vlandev
, flgs
| IFF_UP
);
452 netif_stacked_transfer_operstate(dev
, vlandev
);
456 case NETDEV_UNREGISTER
:
457 /* twiddle thumbs on netns device moves */
458 if (dev
->reg_state
!= NETREG_UNREGISTERING
)
461 for (i
= 0; i
< VLAN_N_VID
; i
++) {
462 vlandev
= vlan_group_get_device(grp
, i
);
466 /* removal of last vid destroys vlan_info, abort
468 if (vlan_info
->nr_vids
== 1)
471 unregister_vlan_dev(vlandev
, &list
);
473 unregister_netdevice_many(&list
);
476 case NETDEV_PRE_TYPE_CHANGE
:
477 /* Forbid underlaying device to change its type. */
478 if (vlan_uses_dev(dev
))
482 case NETDEV_NOTIFY_PEERS
:
483 case NETDEV_BONDING_FAILOVER
:
484 /* Propagate to vlan devices */
485 for (i
= 0; i
< VLAN_N_VID
; i
++) {
486 vlandev
= vlan_group_get_device(grp
, i
);
490 call_netdevice_notifiers(event
, vlandev
);
499 static struct notifier_block vlan_notifier_block __read_mostly
= {
500 .notifier_call
= vlan_device_event
,
504 * VLAN IOCTL handler.
505 * o execute requested action or pass command to the device driver
506 * arg is really a struct vlan_ioctl_args __user *.
508 static int vlan_ioctl_handler(struct net
*net
, void __user
*arg
)
511 struct vlan_ioctl_args args
;
512 struct net_device
*dev
= NULL
;
514 if (copy_from_user(&args
, arg
, sizeof(struct vlan_ioctl_args
)))
517 /* Null terminate this sucker, just in case. */
518 args
.device1
[23] = 0;
519 args
.u
.device2
[23] = 0;
524 case SET_VLAN_INGRESS_PRIORITY_CMD
:
525 case SET_VLAN_EGRESS_PRIORITY_CMD
:
526 case SET_VLAN_FLAG_CMD
:
529 case GET_VLAN_REALDEV_NAME_CMD
:
530 case GET_VLAN_VID_CMD
:
532 dev
= __dev_get_by_name(net
, args
.device1
);
537 if (args
.cmd
!= ADD_VLAN_CMD
&& !is_vlan_dev(dev
))
542 case SET_VLAN_INGRESS_PRIORITY_CMD
:
544 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
546 vlan_dev_set_ingress_priority(dev
,
552 case SET_VLAN_EGRESS_PRIORITY_CMD
:
554 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
556 err
= vlan_dev_set_egress_priority(dev
,
561 case SET_VLAN_FLAG_CMD
:
563 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
565 err
= vlan_dev_change_flags(dev
,
566 args
.vlan_qos
? args
.u
.flag
: 0,
570 case SET_VLAN_NAME_TYPE_CMD
:
572 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
574 if ((args
.u
.name_type
>= 0) &&
575 (args
.u
.name_type
< VLAN_NAME_TYPE_HIGHEST
)) {
578 vn
= net_generic(net
, vlan_net_id
);
579 vn
->name_type
= args
.u
.name_type
;
588 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
590 err
= register_vlan_device(dev
, args
.u
.VID
);
595 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
597 unregister_vlan_dev(dev
, NULL
);
601 case GET_VLAN_REALDEV_NAME_CMD
:
603 vlan_dev_get_realdev_name(dev
, args
.u
.device2
);
604 if (copy_to_user(arg
, &args
,
605 sizeof(struct vlan_ioctl_args
)))
609 case GET_VLAN_VID_CMD
:
611 args
.u
.VID
= vlan_dev_vlan_id(dev
);
612 if (copy_to_user(arg
, &args
,
613 sizeof(struct vlan_ioctl_args
)))
626 static int __net_init
vlan_init_net(struct net
*net
)
628 struct vlan_net
*vn
= net_generic(net
, vlan_net_id
);
631 vn
->name_type
= VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD
;
633 err
= vlan_proc_init(net
);
638 static void __net_exit
vlan_exit_net(struct net
*net
)
640 vlan_proc_cleanup(net
);
643 static struct pernet_operations vlan_net_ops
= {
644 .init
= vlan_init_net
,
645 .exit
= vlan_exit_net
,
647 .size
= sizeof(struct vlan_net
),
650 static int __init
vlan_proto_init(void)
654 pr_info("%s v%s\n", vlan_fullname
, vlan_version
);
656 err
= register_pernet_subsys(&vlan_net_ops
);
660 err
= register_netdevice_notifier(&vlan_notifier_block
);
664 err
= vlan_gvrp_init();
668 err
= vlan_mvrp_init();
672 err
= vlan_netlink_init();
676 vlan_ioctl_set(vlan_ioctl_handler
);
684 unregister_netdevice_notifier(&vlan_notifier_block
);
686 unregister_pernet_subsys(&vlan_net_ops
);
691 static void __exit
vlan_cleanup_module(void)
693 vlan_ioctl_set(NULL
);
696 unregister_netdevice_notifier(&vlan_notifier_block
);
698 unregister_pernet_subsys(&vlan_net_ops
);
699 rcu_barrier(); /* Wait for completion of call_rcu()'s */
705 module_init(vlan_proto_init
);
706 module_exit(vlan_cleanup_module
);
708 MODULE_LICENSE("GPL");
709 MODULE_VERSION(DRV_VERSION
);