vlan: add link to upper device
[platform/adaptation/renesas_rcar/renesas_kernel.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         /* 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).
92          */
93         if (vlan_id)
94                 vlan_vid_del(real_dev, vlan_id);
95
96         grp->nr_vlan_devs--;
97
98         if (vlan->flags & VLAN_FLAG_GVRP)
99                 vlan_gvrp_request_leave(dev);
100
101         vlan_group_set_device(grp, vlan_id, NULL);
102         /* Because unregister_netdevice_queue() makes sure at least one rcu
103          * grace period is respected before device freeing,
104          * we dont need to call synchronize_net() here.
105          */
106         unregister_netdevice_queue(dev, head);
107
108         netdev_upper_dev_unlink(real_dev, dev);
109
110         if (grp->nr_vlan_devs == 0)
111                 vlan_gvrp_uninit_applicant(real_dev);
112
113         /* Get rid of the vlan's reference to real_dev */
114         dev_put(real_dev);
115 }
116
117 int vlan_check_real_dev(struct net_device *real_dev, u16 vlan_id)
118 {
119         const char *name = real_dev->name;
120         const struct net_device_ops *ops = real_dev->netdev_ops;
121
122         if (real_dev->features & NETIF_F_VLAN_CHALLENGED) {
123                 pr_info("VLANs not supported on %s\n", name);
124                 return -EOPNOTSUPP;
125         }
126
127         if ((real_dev->features & NETIF_F_HW_VLAN_FILTER) &&
128             (!ops->ndo_vlan_rx_add_vid || !ops->ndo_vlan_rx_kill_vid)) {
129                 pr_info("Device %s has buggy VLAN hw accel\n", name);
130                 return -EOPNOTSUPP;
131         }
132
133         if (vlan_find_dev(real_dev, vlan_id) != NULL)
134                 return -EEXIST;
135
136         return 0;
137 }
138
139 int register_vlan_dev(struct net_device *dev)
140 {
141         struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
142         struct net_device *real_dev = vlan->real_dev;
143         u16 vlan_id = vlan->vlan_id;
144         struct vlan_info *vlan_info;
145         struct vlan_group *grp;
146         int err;
147
148         err = vlan_vid_add(real_dev, vlan_id);
149         if (err)
150                 return err;
151
152         vlan_info = rtnl_dereference(real_dev->vlan_info);
153         /* vlan_info should be there now. vlan_vid_add took care of it */
154         BUG_ON(!vlan_info);
155
156         grp = &vlan_info->grp;
157         if (grp->nr_vlan_devs == 0) {
158                 err = vlan_gvrp_init_applicant(real_dev);
159                 if (err < 0)
160                         goto out_vid_del;
161         }
162
163         err = vlan_group_prealloc_vid(grp, vlan_id);
164         if (err < 0)
165                 goto out_uninit_applicant;
166
167         err = netdev_upper_dev_link(real_dev, dev);
168         if (err)
169                 goto out_uninit_applicant;
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_applicant:
192         if (grp->nr_vlan_devs == 0)
193                 vlan_gvrp_uninit_applicant(real_dev);
194 out_vid_del:
195         vlan_vid_del(real_dev, vlan_id);
196         return err;
197 }
198
199 /*  Attach a VLAN device to a mac address (ie Ethernet Card).
200  *  Returns 0 if the device was created or a negative error code otherwise.
201  */
202 static int register_vlan_device(struct net_device *real_dev, u16 vlan_id)
203 {
204         struct net_device *new_dev;
205         struct net *net = dev_net(real_dev);
206         struct vlan_net *vn = net_generic(net, vlan_net_id);
207         char name[IFNAMSIZ];
208         int err;
209
210         if (vlan_id >= VLAN_VID_MASK)
211                 return -ERANGE;
212
213         err = vlan_check_real_dev(real_dev, vlan_id);
214         if (err < 0)
215                 return err;
216
217         /* Gotta set up the fields for the device. */
218         switch (vn->name_type) {
219         case VLAN_NAME_TYPE_RAW_PLUS_VID:
220                 /* name will look like:  eth1.0005 */
221                 snprintf(name, IFNAMSIZ, "%s.%.4i", real_dev->name, vlan_id);
222                 break;
223         case VLAN_NAME_TYPE_PLUS_VID_NO_PAD:
224                 /* Put our vlan.VID in the name.
225                  * Name will look like:  vlan5
226                  */
227                 snprintf(name, IFNAMSIZ, "vlan%i", vlan_id);
228                 break;
229         case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD:
230                 /* Put our vlan.VID in the name.
231                  * Name will look like:  eth0.5
232                  */
233                 snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, vlan_id);
234                 break;
235         case VLAN_NAME_TYPE_PLUS_VID:
236                 /* Put our vlan.VID in the name.
237                  * Name will look like:  vlan0005
238                  */
239         default:
240                 snprintf(name, IFNAMSIZ, "vlan%.4i", vlan_id);
241         }
242
243         new_dev = alloc_netdev(sizeof(struct vlan_dev_priv), name, vlan_setup);
244
245         if (new_dev == NULL)
246                 return -ENOBUFS;
247
248         dev_net_set(new_dev, net);
249         /* need 4 bytes for extra VLAN header info,
250          * hope the underlying device can handle it.
251          */
252         new_dev->mtu = real_dev->mtu;
253         new_dev->priv_flags |= (real_dev->priv_flags & IFF_UNICAST_FLT);
254
255         vlan_dev_priv(new_dev)->vlan_id = vlan_id;
256         vlan_dev_priv(new_dev)->real_dev = real_dev;
257         vlan_dev_priv(new_dev)->dent = NULL;
258         vlan_dev_priv(new_dev)->flags = VLAN_FLAG_REORDER_HDR;
259
260         new_dev->rtnl_link_ops = &vlan_link_ops;
261         err = register_vlan_dev(new_dev);
262         if (err < 0)
263                 goto out_free_newdev;
264
265         return 0;
266
267 out_free_newdev:
268         free_netdev(new_dev);
269         return err;
270 }
271
272 static void vlan_sync_address(struct net_device *dev,
273                               struct net_device *vlandev)
274 {
275         struct vlan_dev_priv *vlan = vlan_dev_priv(vlandev);
276
277         /* May be called without an actual change */
278         if (ether_addr_equal(vlan->real_dev_addr, dev->dev_addr))
279                 return;
280
281         /* vlan address was different from the old address and is equal to
282          * the new address */
283         if (!ether_addr_equal(vlandev->dev_addr, vlan->real_dev_addr) &&
284             ether_addr_equal(vlandev->dev_addr, dev->dev_addr))
285                 dev_uc_del(dev, vlandev->dev_addr);
286
287         /* vlan address was equal to the old address and is different from
288          * the new address */
289         if (ether_addr_equal(vlandev->dev_addr, vlan->real_dev_addr) &&
290             !ether_addr_equal(vlandev->dev_addr, dev->dev_addr))
291                 dev_uc_add(dev, vlandev->dev_addr);
292
293         memcpy(vlan->real_dev_addr, dev->dev_addr, ETH_ALEN);
294 }
295
296 static void vlan_transfer_features(struct net_device *dev,
297                                    struct net_device *vlandev)
298 {
299         vlandev->gso_max_size = dev->gso_max_size;
300
301         if (dev->features & NETIF_F_HW_VLAN_TX)
302                 vlandev->hard_header_len = dev->hard_header_len;
303         else
304                 vlandev->hard_header_len = dev->hard_header_len + VLAN_HLEN;
305
306 #if IS_ENABLED(CONFIG_FCOE)
307         vlandev->fcoe_ddp_xid = dev->fcoe_ddp_xid;
308 #endif
309
310         netdev_update_features(vlandev);
311 }
312
313 static void __vlan_device_event(struct net_device *dev, unsigned long event)
314 {
315         switch (event) {
316         case NETDEV_CHANGENAME:
317                 vlan_proc_rem_dev(dev);
318                 if (vlan_proc_add_dev(dev) < 0)
319                         pr_warn("failed to change proc name for %s\n",
320                                 dev->name);
321                 break;
322         case NETDEV_REGISTER:
323                 if (vlan_proc_add_dev(dev) < 0)
324                         pr_warn("failed to add proc entry for %s\n", dev->name);
325                 break;
326         case NETDEV_UNREGISTER:
327                 vlan_proc_rem_dev(dev);
328                 break;
329         }
330 }
331
332 static int vlan_device_event(struct notifier_block *unused, unsigned long event,
333                              void *ptr)
334 {
335         struct net_device *dev = ptr;
336         struct vlan_group *grp;
337         struct vlan_info *vlan_info;
338         int i, flgs;
339         struct net_device *vlandev;
340         struct vlan_dev_priv *vlan;
341         LIST_HEAD(list);
342
343         if (is_vlan_dev(dev))
344                 __vlan_device_event(dev, event);
345
346         if ((event == NETDEV_UP) &&
347             (dev->features & NETIF_F_HW_VLAN_FILTER)) {
348                 pr_info("adding VLAN 0 to HW filter on device %s\n",
349                         dev->name);
350                 vlan_vid_add(dev, 0);
351         }
352
353         vlan_info = rtnl_dereference(dev->vlan_info);
354         if (!vlan_info)
355                 goto out;
356         grp = &vlan_info->grp;
357
358         /* It is OK that we do not hold the group lock right now,
359          * as we run under the RTNL lock.
360          */
361
362         switch (event) {
363         case NETDEV_CHANGE:
364                 /* Propagate real device state to vlan devices */
365                 for (i = 0; i < VLAN_N_VID; i++) {
366                         vlandev = vlan_group_get_device(grp, i);
367                         if (!vlandev)
368                                 continue;
369
370                         netif_stacked_transfer_operstate(dev, vlandev);
371                 }
372                 break;
373
374         case NETDEV_CHANGEADDR:
375                 /* Adjust unicast filters on underlying device */
376                 for (i = 0; i < VLAN_N_VID; i++) {
377                         vlandev = vlan_group_get_device(grp, i);
378                         if (!vlandev)
379                                 continue;
380
381                         flgs = vlandev->flags;
382                         if (!(flgs & IFF_UP))
383                                 continue;
384
385                         vlan_sync_address(dev, vlandev);
386                 }
387                 break;
388
389         case NETDEV_CHANGEMTU:
390                 for (i = 0; i < VLAN_N_VID; i++) {
391                         vlandev = vlan_group_get_device(grp, i);
392                         if (!vlandev)
393                                 continue;
394
395                         if (vlandev->mtu <= dev->mtu)
396                                 continue;
397
398                         dev_set_mtu(vlandev, dev->mtu);
399                 }
400                 break;
401
402         case NETDEV_FEAT_CHANGE:
403                 /* Propagate device features to underlying device */
404                 for (i = 0; i < VLAN_N_VID; i++) {
405                         vlandev = vlan_group_get_device(grp, i);
406                         if (!vlandev)
407                                 continue;
408
409                         vlan_transfer_features(dev, vlandev);
410                 }
411
412                 break;
413
414         case NETDEV_DOWN:
415                 if (dev->features & NETIF_F_HW_VLAN_FILTER)
416                         vlan_vid_del(dev, 0);
417
418                 /* Put all VLANs for this dev in the down state too.  */
419                 for (i = 0; i < VLAN_N_VID; i++) {
420                         vlandev = vlan_group_get_device(grp, i);
421                         if (!vlandev)
422                                 continue;
423
424                         flgs = vlandev->flags;
425                         if (!(flgs & IFF_UP))
426                                 continue;
427
428                         vlan = vlan_dev_priv(vlandev);
429                         if (!(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
430                                 dev_change_flags(vlandev, flgs & ~IFF_UP);
431                         netif_stacked_transfer_operstate(dev, vlandev);
432                 }
433                 break;
434
435         case NETDEV_UP:
436                 /* Put all VLANs for this dev in the up state too.  */
437                 for (i = 0; i < VLAN_N_VID; i++) {
438                         vlandev = vlan_group_get_device(grp, i);
439                         if (!vlandev)
440                                 continue;
441
442                         flgs = vlandev->flags;
443                         if (flgs & IFF_UP)
444                                 continue;
445
446                         vlan = vlan_dev_priv(vlandev);
447                         if (!(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
448                                 dev_change_flags(vlandev, flgs | IFF_UP);
449                         netif_stacked_transfer_operstate(dev, vlandev);
450                 }
451                 break;
452
453         case NETDEV_UNREGISTER:
454                 /* twiddle thumbs on netns device moves */
455                 if (dev->reg_state != NETREG_UNREGISTERING)
456                         break;
457
458                 for (i = 0; i < VLAN_N_VID; i++) {
459                         vlandev = vlan_group_get_device(grp, i);
460                         if (!vlandev)
461                                 continue;
462
463                         /* removal of last vid destroys vlan_info, abort
464                          * afterwards */
465                         if (vlan_info->nr_vids == 1)
466                                 i = VLAN_N_VID;
467
468                         unregister_vlan_dev(vlandev, &list);
469                 }
470                 unregister_netdevice_many(&list);
471                 break;
472
473         case NETDEV_PRE_TYPE_CHANGE:
474                 /* Forbid underlaying device to change its type. */
475                 if (vlan_uses_dev(dev))
476                         return NOTIFY_BAD;
477                 break;
478
479         case NETDEV_NOTIFY_PEERS:
480         case NETDEV_BONDING_FAILOVER:
481                 /* Propagate to vlan devices */
482                 for (i = 0; i < VLAN_N_VID; i++) {
483                         vlandev = vlan_group_get_device(grp, i);
484                         if (!vlandev)
485                                 continue;
486
487                         call_netdevice_notifiers(event, vlandev);
488                 }
489                 break;
490         }
491
492 out:
493         return NOTIFY_DONE;
494 }
495
496 static struct notifier_block vlan_notifier_block __read_mostly = {
497         .notifier_call = vlan_device_event,
498 };
499
500 /*
501  *      VLAN IOCTL handler.
502  *      o execute requested action or pass command to the device driver
503  *   arg is really a struct vlan_ioctl_args __user *.
504  */
505 static int vlan_ioctl_handler(struct net *net, void __user *arg)
506 {
507         int err;
508         struct vlan_ioctl_args args;
509         struct net_device *dev = NULL;
510
511         if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args)))
512                 return -EFAULT;
513
514         /* Null terminate this sucker, just in case. */
515         args.device1[23] = 0;
516         args.u.device2[23] = 0;
517
518         rtnl_lock();
519
520         switch (args.cmd) {
521         case SET_VLAN_INGRESS_PRIORITY_CMD:
522         case SET_VLAN_EGRESS_PRIORITY_CMD:
523         case SET_VLAN_FLAG_CMD:
524         case ADD_VLAN_CMD:
525         case DEL_VLAN_CMD:
526         case GET_VLAN_REALDEV_NAME_CMD:
527         case GET_VLAN_VID_CMD:
528                 err = -ENODEV;
529                 dev = __dev_get_by_name(net, args.device1);
530                 if (!dev)
531                         goto out;
532
533                 err = -EINVAL;
534                 if (args.cmd != ADD_VLAN_CMD && !is_vlan_dev(dev))
535                         goto out;
536         }
537
538         switch (args.cmd) {
539         case SET_VLAN_INGRESS_PRIORITY_CMD:
540                 err = -EPERM;
541                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
542                         break;
543                 vlan_dev_set_ingress_priority(dev,
544                                               args.u.skb_priority,
545                                               args.vlan_qos);
546                 err = 0;
547                 break;
548
549         case SET_VLAN_EGRESS_PRIORITY_CMD:
550                 err = -EPERM;
551                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
552                         break;
553                 err = vlan_dev_set_egress_priority(dev,
554                                                    args.u.skb_priority,
555                                                    args.vlan_qos);
556                 break;
557
558         case SET_VLAN_FLAG_CMD:
559                 err = -EPERM;
560                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
561                         break;
562                 err = vlan_dev_change_flags(dev,
563                                             args.vlan_qos ? args.u.flag : 0,
564                                             args.u.flag);
565                 break;
566
567         case SET_VLAN_NAME_TYPE_CMD:
568                 err = -EPERM;
569                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
570                         break;
571                 if ((args.u.name_type >= 0) &&
572                     (args.u.name_type < VLAN_NAME_TYPE_HIGHEST)) {
573                         struct vlan_net *vn;
574
575                         vn = net_generic(net, vlan_net_id);
576                         vn->name_type = args.u.name_type;
577                         err = 0;
578                 } else {
579                         err = -EINVAL;
580                 }
581                 break;
582
583         case ADD_VLAN_CMD:
584                 err = -EPERM;
585                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
586                         break;
587                 err = register_vlan_device(dev, args.u.VID);
588                 break;
589
590         case DEL_VLAN_CMD:
591                 err = -EPERM;
592                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
593                         break;
594                 unregister_vlan_dev(dev, NULL);
595                 err = 0;
596                 break;
597
598         case GET_VLAN_REALDEV_NAME_CMD:
599                 err = 0;
600                 vlan_dev_get_realdev_name(dev, args.u.device2);
601                 if (copy_to_user(arg, &args,
602                                  sizeof(struct vlan_ioctl_args)))
603                         err = -EFAULT;
604                 break;
605
606         case GET_VLAN_VID_CMD:
607                 err = 0;
608                 args.u.VID = vlan_dev_vlan_id(dev);
609                 if (copy_to_user(arg, &args,
610                                  sizeof(struct vlan_ioctl_args)))
611                       err = -EFAULT;
612                 break;
613
614         default:
615                 err = -EOPNOTSUPP;
616                 break;
617         }
618 out:
619         rtnl_unlock();
620         return err;
621 }
622
623 static int __net_init vlan_init_net(struct net *net)
624 {
625         struct vlan_net *vn = net_generic(net, vlan_net_id);
626         int err;
627
628         vn->name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;
629
630         err = vlan_proc_init(net);
631
632         return err;
633 }
634
635 static void __net_exit vlan_exit_net(struct net *net)
636 {
637         vlan_proc_cleanup(net);
638 }
639
640 static struct pernet_operations vlan_net_ops = {
641         .init = vlan_init_net,
642         .exit = vlan_exit_net,
643         .id   = &vlan_net_id,
644         .size = sizeof(struct vlan_net),
645 };
646
647 static int __init vlan_proto_init(void)
648 {
649         int err;
650
651         pr_info("%s v%s\n", vlan_fullname, vlan_version);
652
653         err = register_pernet_subsys(&vlan_net_ops);
654         if (err < 0)
655                 goto err0;
656
657         err = register_netdevice_notifier(&vlan_notifier_block);
658         if (err < 0)
659                 goto err2;
660
661         err = vlan_gvrp_init();
662         if (err < 0)
663                 goto err3;
664
665         err = vlan_netlink_init();
666         if (err < 0)
667                 goto err4;
668
669         vlan_ioctl_set(vlan_ioctl_handler);
670         return 0;
671
672 err4:
673         vlan_gvrp_uninit();
674 err3:
675         unregister_netdevice_notifier(&vlan_notifier_block);
676 err2:
677         unregister_pernet_subsys(&vlan_net_ops);
678 err0:
679         return err;
680 }
681
682 static void __exit vlan_cleanup_module(void)
683 {
684         vlan_ioctl_set(NULL);
685         vlan_netlink_fini();
686
687         unregister_netdevice_notifier(&vlan_notifier_block);
688
689         unregister_pernet_subsys(&vlan_net_ops);
690         rcu_barrier(); /* Wait for completion of call_rcu()'s */
691
692         vlan_gvrp_uninit();
693 }
694
695 module_init(vlan_proto_init);
696 module_exit(vlan_cleanup_module);
697
698 MODULE_LICENSE("GPL");
699 MODULE_VERSION(DRV_VERSION);