hsi: fix kernel-doc warnings
[linux-2.6.git] / net / 8021q / vlan.c
bloba18714469bf791299f0d60b1a38b9886fdf4dfff
1 /*
2 * INET 802.1Q VLAN
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
9 * 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>
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>
31 #include <net/arp.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>
40 #include "vlan.h"
41 #include "vlanproc.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;
57 unsigned int size;
59 ASSERT_RTNL();
61 array = vg->vlan_devices_arrays[vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
62 if (array != NULL)
63 return 0;
65 size = sizeof(struct net_device *) * VLAN_GROUP_ARRAY_PART_LEN;
66 array = kzalloc(size, GFP_KERNEL);
67 if (array == NULL)
68 return -ENOBUFS;
70 vg->vlan_devices_arrays[vlan_id / VLAN_GROUP_ARRAY_PART_LEN] = array;
71 return 0;
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;
82 ASSERT_RTNL();
84 vlan_info = rtnl_dereference(real_dev->vlan_info);
85 BUG_ON(!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).
93 if (vlan_id)
94 vlan_vid_del(real_dev, vlan_id);
96 grp->nr_vlan_devs--;
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 */
118 dev_put(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);
127 return -EOPNOTSUPP;
130 if (vlan_find_dev(real_dev, vlan_id) != NULL)
131 return -EEXIST;
133 return 0;
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;
143 int err;
145 err = vlan_vid_add(real_dev, vlan_id);
146 if (err)
147 return err;
149 vlan_info = rtnl_dereference(real_dev->vlan_info);
150 /* vlan_info should be there now. vlan_vid_add took care of it */
151 BUG_ON(!vlan_info);
153 grp = &vlan_info->grp;
154 if (grp->nr_vlan_devs == 0) {
155 err = vlan_gvrp_init_applicant(real_dev);
156 if (err < 0)
157 goto out_vid_del;
158 err = vlan_mvrp_init_applicant(real_dev);
159 if (err < 0)
160 goto out_uninit_gvrp;
163 err = vlan_group_prealloc_vid(grp, vlan_id);
164 if (err < 0)
165 goto out_uninit_mvrp;
167 err = netdev_upper_dev_link(real_dev, dev);
168 if (err)
169 goto out_uninit_mvrp;
171 err = register_netdevice(dev);
172 if (err < 0)
173 goto out_upper_dev_unlink;
175 /* Account for reference in struct vlan_dev_priv */
176 dev_hold(real_dev);
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);
185 grp->nr_vlan_devs++;
187 return 0;
189 out_upper_dev_unlink:
190 netdev_upper_dev_unlink(real_dev, dev);
191 out_uninit_mvrp:
192 if (grp->nr_vlan_devs == 0)
193 vlan_mvrp_uninit_applicant(real_dev);
194 out_uninit_gvrp:
195 if (grp->nr_vlan_devs == 0)
196 vlan_gvrp_uninit_applicant(real_dev);
197 out_vid_del:
198 vlan_vid_del(real_dev, vlan_id);
199 return err;
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);
210 char name[IFNAMSIZ];
211 int err;
213 if (vlan_id >= VLAN_VID_MASK)
214 return -ERANGE;
216 err = vlan_check_real_dev(real_dev, vlan_id);
217 if (err < 0)
218 return err;
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);
225 break;
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);
231 break;
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);
237 break;
238 case VLAN_NAME_TYPE_PLUS_VID:
239 /* Put our vlan.VID in the name.
240 * Name will look like: vlan0005
242 default:
243 snprintf(name, IFNAMSIZ, "vlan%.4i", vlan_id);
246 new_dev = alloc_netdev(sizeof(struct vlan_dev_priv), name, vlan_setup);
248 if (new_dev == NULL)
249 return -ENOBUFS;
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);
265 if (err < 0)
266 goto out_free_newdev;
268 return 0;
270 out_free_newdev:
271 free_netdev(new_dev);
272 return err;
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))
282 return;
284 /* vlan address was different from the old address and is equal to
285 * the new address */
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
291 * the new address */
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;
306 else
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;
311 #endif
313 netdev_update_features(vlandev);
316 static void __vlan_device_event(struct net_device *dev, unsigned long event)
318 switch (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",
323 dev->name);
324 break;
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);
328 break;
329 case NETDEV_UNREGISTER:
330 vlan_proc_rem_dev(dev);
331 break;
335 static int vlan_device_event(struct notifier_block *unused, unsigned long event,
336 void *ptr)
338 struct net_device *dev = ptr;
339 struct vlan_group *grp;
340 struct vlan_info *vlan_info;
341 int i, flgs;
342 struct net_device *vlandev;
343 struct vlan_dev_priv *vlan;
344 LIST_HEAD(list);
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",
352 dev->name);
353 vlan_vid_add(dev, 0);
356 vlan_info = rtnl_dereference(dev->vlan_info);
357 if (!vlan_info)
358 goto out;
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.
365 switch (event) {
366 case NETDEV_CHANGE:
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);
370 if (!vlandev)
371 continue;
373 netif_stacked_transfer_operstate(dev, vlandev);
375 break;
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);
381 if (!vlandev)
382 continue;
384 flgs = vlandev->flags;
385 if (!(flgs & IFF_UP))
386 continue;
388 vlan_sync_address(dev, vlandev);
390 break;
392 case NETDEV_CHANGEMTU:
393 for (i = 0; i < VLAN_N_VID; i++) {
394 vlandev = vlan_group_get_device(grp, i);
395 if (!vlandev)
396 continue;
398 if (vlandev->mtu <= dev->mtu)
399 continue;
401 dev_set_mtu(vlandev, dev->mtu);
403 break;
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);
409 if (!vlandev)
410 continue;
412 vlan_transfer_features(dev, vlandev);
415 break;
417 case NETDEV_DOWN:
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);
424 if (!vlandev)
425 continue;
427 flgs = vlandev->flags;
428 if (!(flgs & IFF_UP))
429 continue;
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);
436 break;
438 case NETDEV_UP:
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);
442 if (!vlandev)
443 continue;
445 flgs = vlandev->flags;
446 if (flgs & IFF_UP)
447 continue;
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);
454 break;
456 case NETDEV_UNREGISTER:
457 /* twiddle thumbs on netns device moves */
458 if (dev->reg_state != NETREG_UNREGISTERING)
459 break;
461 for (i = 0; i < VLAN_N_VID; i++) {
462 vlandev = vlan_group_get_device(grp, i);
463 if (!vlandev)
464 continue;
466 /* removal of last vid destroys vlan_info, abort
467 * afterwards */
468 if (vlan_info->nr_vids == 1)
469 i = VLAN_N_VID;
471 unregister_vlan_dev(vlandev, &list);
473 unregister_netdevice_many(&list);
474 break;
476 case NETDEV_PRE_TYPE_CHANGE:
477 /* Forbid underlaying device to change its type. */
478 if (vlan_uses_dev(dev))
479 return NOTIFY_BAD;
480 break;
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);
487 if (!vlandev)
488 continue;
490 call_netdevice_notifiers(event, vlandev);
492 break;
495 out:
496 return NOTIFY_DONE;
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)
510 int err;
511 struct vlan_ioctl_args args;
512 struct net_device *dev = NULL;
514 if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args)))
515 return -EFAULT;
517 /* Null terminate this sucker, just in case. */
518 args.device1[23] = 0;
519 args.u.device2[23] = 0;
521 rtnl_lock();
523 switch (args.cmd) {
524 case SET_VLAN_INGRESS_PRIORITY_CMD:
525 case SET_VLAN_EGRESS_PRIORITY_CMD:
526 case SET_VLAN_FLAG_CMD:
527 case ADD_VLAN_CMD:
528 case DEL_VLAN_CMD:
529 case GET_VLAN_REALDEV_NAME_CMD:
530 case GET_VLAN_VID_CMD:
531 err = -ENODEV;
532 dev = __dev_get_by_name(net, args.device1);
533 if (!dev)
534 goto out;
536 err = -EINVAL;
537 if (args.cmd != ADD_VLAN_CMD && !is_vlan_dev(dev))
538 goto out;
541 switch (args.cmd) {
542 case SET_VLAN_INGRESS_PRIORITY_CMD:
543 err = -EPERM;
544 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
545 break;
546 vlan_dev_set_ingress_priority(dev,
547 args.u.skb_priority,
548 args.vlan_qos);
549 err = 0;
550 break;
552 case SET_VLAN_EGRESS_PRIORITY_CMD:
553 err = -EPERM;
554 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
555 break;
556 err = vlan_dev_set_egress_priority(dev,
557 args.u.skb_priority,
558 args.vlan_qos);
559 break;
561 case SET_VLAN_FLAG_CMD:
562 err = -EPERM;
563 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
564 break;
565 err = vlan_dev_change_flags(dev,
566 args.vlan_qos ? args.u.flag : 0,
567 args.u.flag);
568 break;
570 case SET_VLAN_NAME_TYPE_CMD:
571 err = -EPERM;
572 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
573 break;
574 if ((args.u.name_type >= 0) &&
575 (args.u.name_type < VLAN_NAME_TYPE_HIGHEST)) {
576 struct vlan_net *vn;
578 vn = net_generic(net, vlan_net_id);
579 vn->name_type = args.u.name_type;
580 err = 0;
581 } else {
582 err = -EINVAL;
584 break;
586 case ADD_VLAN_CMD:
587 err = -EPERM;
588 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
589 break;
590 err = register_vlan_device(dev, args.u.VID);
591 break;
593 case DEL_VLAN_CMD:
594 err = -EPERM;
595 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
596 break;
597 unregister_vlan_dev(dev, NULL);
598 err = 0;
599 break;
601 case GET_VLAN_REALDEV_NAME_CMD:
602 err = 0;
603 vlan_dev_get_realdev_name(dev, args.u.device2);
604 if (copy_to_user(arg, &args,
605 sizeof(struct vlan_ioctl_args)))
606 err = -EFAULT;
607 break;
609 case GET_VLAN_VID_CMD:
610 err = 0;
611 args.u.VID = vlan_dev_vlan_id(dev);
612 if (copy_to_user(arg, &args,
613 sizeof(struct vlan_ioctl_args)))
614 err = -EFAULT;
615 break;
617 default:
618 err = -EOPNOTSUPP;
619 break;
621 out:
622 rtnl_unlock();
623 return err;
626 static int __net_init vlan_init_net(struct net *net)
628 struct vlan_net *vn = net_generic(net, vlan_net_id);
629 int err;
631 vn->name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;
633 err = vlan_proc_init(net);
635 return err;
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,
646 .id = &vlan_net_id,
647 .size = sizeof(struct vlan_net),
650 static int __init vlan_proto_init(void)
652 int err;
654 pr_info("%s v%s\n", vlan_fullname, vlan_version);
656 err = register_pernet_subsys(&vlan_net_ops);
657 if (err < 0)
658 goto err0;
660 err = register_netdevice_notifier(&vlan_notifier_block);
661 if (err < 0)
662 goto err2;
664 err = vlan_gvrp_init();
665 if (err < 0)
666 goto err3;
668 err = vlan_mvrp_init();
669 if (err < 0)
670 goto err4;
672 err = vlan_netlink_init();
673 if (err < 0)
674 goto err5;
676 vlan_ioctl_set(vlan_ioctl_handler);
677 return 0;
679 err5:
680 vlan_mvrp_uninit();
681 err4:
682 vlan_gvrp_uninit();
683 err3:
684 unregister_netdevice_notifier(&vlan_notifier_block);
685 err2:
686 unregister_pernet_subsys(&vlan_net_ops);
687 err0:
688 return err;
691 static void __exit vlan_cleanup_module(void)
693 vlan_ioctl_set(NULL);
694 vlan_netlink_fini();
696 unregister_netdevice_notifier(&vlan_notifier_block);
698 unregister_pernet_subsys(&vlan_net_ops);
699 rcu_barrier(); /* Wait for completion of call_rcu()'s */
701 vlan_mvrp_uninit();
702 vlan_gvrp_uninit();
705 module_init(vlan_proto_init);
706 module_exit(vlan_cleanup_module);
708 MODULE_LICENSE("GPL");
709 MODULE_VERSION(DRV_VERSION);