Merge branch 'mips-next-3.10' of git://git.linux-mips.org/pub/scm/john/linux-john...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / 8021q / vlan.c
1 /*
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: netdev@vger.kernel.org
7 * VLAN Home Page: http://www.candelatech.com/~greear/vlan.html
8 *
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>
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
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>
38
39 #include <linux/if_vlan.h>
40 #include "vlan.h"
41 #include "vlanproc.h"
42
43 #define DRV_VERSION "1.8"
44
45 /* Global VLAN variables */
46
47 int vlan_net_id __read_mostly;
48
49 const char vlan_fullname[] = "802.1Q VLAN Support";
50 const char vlan_version[] = DRV_VERSION;
51
52 /* End of global variables definitions. */
53
54 static int vlan_group_prealloc_vid(struct vlan_group *vg, u16 vlan_id)
55 {
56 struct net_device **array;
57 unsigned int size;
58
59 ASSERT_RTNL();
60
61 array = vg->vlan_devices_arrays[vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
62 if (array != NULL)
63 return 0;
64
65 size = sizeof(struct net_device *) * VLAN_GROUP_ARRAY_PART_LEN;
66 array = kzalloc(size, GFP_KERNEL);
67 if (array == NULL)
68 return -ENOBUFS;
69
70 vg->vlan_devices_arrays[vlan_id / VLAN_GROUP_ARRAY_PART_LEN] = array;
71 return 0;
72 }
73
74 void unregister_vlan_dev(struct net_device *dev, struct list_head *head)
75 {
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;
81
82 ASSERT_RTNL();
83
84 vlan_info = rtnl_dereference(real_dev->vlan_info);
85 BUG_ON(!vlan_info);
86
87 grp = &vlan_info->grp;
88
89 grp->nr_vlan_devs--;
90
91 if (vlan->flags & VLAN_FLAG_MVRP)
92 vlan_mvrp_request_leave(dev);
93 if (vlan->flags & VLAN_FLAG_GVRP)
94 vlan_gvrp_request_leave(dev);
95
96 vlan_group_set_device(grp, vlan_id, NULL);
97 /* Because unregister_netdevice_queue() makes sure at least one rcu
98 * grace period is respected before device freeing,
99 * we dont need to call synchronize_net() here.
100 */
101 unregister_netdevice_queue(dev, head);
102
103 netdev_upper_dev_unlink(real_dev, dev);
104
105 if (grp->nr_vlan_devs == 0) {
106 vlan_mvrp_uninit_applicant(real_dev);
107 vlan_gvrp_uninit_applicant(real_dev);
108 }
109
110 /* Take it out of our own structures, but be sure to interlock with
111 * HW accelerating devices or SW vlan input packet processing if
112 * VLAN is not 0 (leave it there for 802.1p).
113 */
114 if (vlan_id)
115 vlan_vid_del(real_dev, vlan_id);
116
117 /* Get rid of the vlan's reference to real_dev */
118 dev_put(real_dev);
119 }
120
121 int vlan_check_real_dev(struct net_device *real_dev, u16 vlan_id)
122 {
123 const char *name = real_dev->name;
124
125 if (real_dev->features & NETIF_F_VLAN_CHALLENGED) {
126 pr_info("VLANs not supported on %s\n", name);
127 return -EOPNOTSUPP;
128 }
129
130 if (vlan_find_dev(real_dev, vlan_id) != NULL)
131 return -EEXIST;
132
133 return 0;
134 }
135
136 int register_vlan_dev(struct net_device *dev)
137 {
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;
144
145 err = vlan_vid_add(real_dev, vlan_id);
146 if (err)
147 return err;
148
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);
152
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;
161 }
162
163 err = vlan_group_prealloc_vid(grp, vlan_id);
164 if (err < 0)
165 goto out_uninit_mvrp;
166
167 err = netdev_upper_dev_link(real_dev, dev);
168 if (err)
169 goto out_uninit_mvrp;
170
171 err = register_netdevice(dev);
172 if (err < 0)
173 goto out_upper_dev_unlink;
174
175 /* Account for reference in struct vlan_dev_priv */
176 dev_hold(real_dev);
177
178 netif_stacked_transfer_operstate(real_dev, dev);
179 linkwatch_fire_event(dev); /* _MUST_ call rfc2863_policy() */
180
181 /* So, got the sucker initialized, now lets place
182 * it into our local structure.
183 */
184 vlan_group_set_device(grp, vlan_id, dev);
185 grp->nr_vlan_devs++;
186
187 return 0;
188
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;
200 }
201
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.
204 */
205 static int register_vlan_device(struct net_device *real_dev, u16 vlan_id)
206 {
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;
212
213 if (vlan_id >= VLAN_VID_MASK)
214 return -ERANGE;
215
216 err = vlan_check_real_dev(real_dev, vlan_id);
217 if (err < 0)
218 return err;
219
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
229 */
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
235 */
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
241 */
242 default:
243 snprintf(name, IFNAMSIZ, "vlan%.4i", vlan_id);
244 }
245
246 new_dev = alloc_netdev(sizeof(struct vlan_dev_priv), name, vlan_setup);
247
248 if (new_dev == NULL)
249 return -ENOBUFS;
250
251 dev_net_set(new_dev, net);
252 /* need 4 bytes for extra VLAN header info,
253 * hope the underlying device can handle it.
254 */
255 new_dev->mtu = real_dev->mtu;
256 new_dev->priv_flags |= (real_dev->priv_flags & IFF_UNICAST_FLT);
257
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;
262
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;
267
268 return 0;
269
270 out_free_newdev:
271 free_netdev(new_dev);
272 return err;
273 }
274
275 static void vlan_sync_address(struct net_device *dev,
276 struct net_device *vlandev)
277 {
278 struct vlan_dev_priv *vlan = vlan_dev_priv(vlandev);
279
280 /* May be called without an actual change */
281 if (ether_addr_equal(vlan->real_dev_addr, dev->dev_addr))
282 return;
283
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);
289
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);
295
296 memcpy(vlan->real_dev_addr, dev->dev_addr, ETH_ALEN);
297 }
298
299 static void vlan_transfer_features(struct net_device *dev,
300 struct net_device *vlandev)
301 {
302 vlandev->gso_max_size = dev->gso_max_size;
303
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;
308
309 #if IS_ENABLED(CONFIG_FCOE)
310 vlandev->fcoe_ddp_xid = dev->fcoe_ddp_xid;
311 #endif
312
313 netdev_update_features(vlandev);
314 }
315
316 static void __vlan_device_event(struct net_device *dev, unsigned long event)
317 {
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;
332 }
333 }
334
335 static int vlan_device_event(struct notifier_block *unused, unsigned long event,
336 void *ptr)
337 {
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);
345
346 if (is_vlan_dev(dev))
347 __vlan_device_event(dev, event);
348
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);
354 }
355
356 vlan_info = rtnl_dereference(dev->vlan_info);
357 if (!vlan_info)
358 goto out;
359 grp = &vlan_info->grp;
360
361 /* It is OK that we do not hold the group lock right now,
362 * as we run under the RTNL lock.
363 */
364
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;
372
373 netif_stacked_transfer_operstate(dev, vlandev);
374 }
375 break;
376
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;
383
384 flgs = vlandev->flags;
385 if (!(flgs & IFF_UP))
386 continue;
387
388 vlan_sync_address(dev, vlandev);
389 }
390 break;
391
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;
397
398 if (vlandev->mtu <= dev->mtu)
399 continue;
400
401 dev_set_mtu(vlandev, dev->mtu);
402 }
403 break;
404
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;
411
412 vlan_transfer_features(dev, vlandev);
413 }
414
415 break;
416
417 case NETDEV_DOWN:
418 if (dev->features & NETIF_F_HW_VLAN_FILTER)
419 vlan_vid_del(dev, 0);
420
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;
426
427 flgs = vlandev->flags;
428 if (!(flgs & IFF_UP))
429 continue;
430
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);
435 }
436 break;
437
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;
444
445 flgs = vlandev->flags;
446 if (flgs & IFF_UP)
447 continue;
448
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);
453 }
454 break;
455
456 case NETDEV_UNREGISTER:
457 /* twiddle thumbs on netns device moves */
458 if (dev->reg_state != NETREG_UNREGISTERING)
459 break;
460
461 for (i = 0; i < VLAN_N_VID; i++) {
462 vlandev = vlan_group_get_device(grp, i);
463 if (!vlandev)
464 continue;
465
466 /* removal of last vid destroys vlan_info, abort
467 * afterwards */
468 if (vlan_info->nr_vids == 1)
469 i = VLAN_N_VID;
470
471 unregister_vlan_dev(vlandev, &list);
472 }
473 unregister_netdevice_many(&list);
474 break;
475
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;
481
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;
489
490 call_netdevice_notifiers(event, vlandev);
491 }
492 break;
493 }
494
495 out:
496 return NOTIFY_DONE;
497 }
498
499 static struct notifier_block vlan_notifier_block __read_mostly = {
500 .notifier_call = vlan_device_event,
501 };
502
503 /*
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 *.
507 */
508 static int vlan_ioctl_handler(struct net *net, void __user *arg)
509 {
510 int err;
511 struct vlan_ioctl_args args;
512 struct net_device *dev = NULL;
513
514 if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args)))
515 return -EFAULT;
516
517 /* Null terminate this sucker, just in case. */
518 args.device1[23] = 0;
519 args.u.device2[23] = 0;
520
521 rtnl_lock();
522
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;
535
536 err = -EINVAL;
537 if (args.cmd != ADD_VLAN_CMD && !is_vlan_dev(dev))
538 goto out;
539 }
540
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;
551
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;
560
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;
569
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;
577
578 vn = net_generic(net, vlan_net_id);
579 vn->name_type = args.u.name_type;
580 err = 0;
581 } else {
582 err = -EINVAL;
583 }
584 break;
585
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;
592
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;
600
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;
608
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;
616
617 default:
618 err = -EOPNOTSUPP;
619 break;
620 }
621 out:
622 rtnl_unlock();
623 return err;
624 }
625
626 static int __net_init vlan_init_net(struct net *net)
627 {
628 struct vlan_net *vn = net_generic(net, vlan_net_id);
629 int err;
630
631 vn->name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;
632
633 err = vlan_proc_init(net);
634
635 return err;
636 }
637
638 static void __net_exit vlan_exit_net(struct net *net)
639 {
640 vlan_proc_cleanup(net);
641 }
642
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),
648 };
649
650 static int __init vlan_proto_init(void)
651 {
652 int err;
653
654 pr_info("%s v%s\n", vlan_fullname, vlan_version);
655
656 err = register_pernet_subsys(&vlan_net_ops);
657 if (err < 0)
658 goto err0;
659
660 err = register_netdevice_notifier(&vlan_notifier_block);
661 if (err < 0)
662 goto err2;
663
664 err = vlan_gvrp_init();
665 if (err < 0)
666 goto err3;
667
668 err = vlan_mvrp_init();
669 if (err < 0)
670 goto err4;
671
672 err = vlan_netlink_init();
673 if (err < 0)
674 goto err5;
675
676 vlan_ioctl_set(vlan_ioctl_handler);
677 return 0;
678
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;
689 }
690
691 static void __exit vlan_cleanup_module(void)
692 {
693 vlan_ioctl_set(NULL);
694 vlan_netlink_fini();
695
696 unregister_netdevice_notifier(&vlan_notifier_block);
697
698 unregister_pernet_subsys(&vlan_net_ops);
699 rcu_barrier(); /* Wait for completion of call_rcu()'s */
700
701 vlan_mvrp_uninit();
702 vlan_gvrp_uninit();
703 }
704
705 module_init(vlan_proto_init);
706 module_exit(vlan_cleanup_module);
707
708 MODULE_LICENSE("GPL");
709 MODULE_VERSION(DRV_VERSION);