11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * INET 802.1Q VLAN 31da177e4SLinus Torvalds * Ethernet-type device handling. 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Authors: Ben Greear <greearb@candelatech.com> 6ad712087SPatrick McHardy * Please send support related email to: netdev@vger.kernel.org 71da177e4SLinus Torvalds * VLAN Home Page: http://www.candelatech.com/~greear/vlan.html 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Fixes: 101da177e4SLinus Torvalds * Fix for packet capture - Nick Eggleston <nick@dccinc.com>; 111da177e4SLinus Torvalds * Add HW acceleration hooks - David S. Miller <davem@redhat.com>; 121da177e4SLinus Torvalds * Correct all the locking - David S. Miller <davem@redhat.com>; 131da177e4SLinus Torvalds * Use hash table for VLAN groups - David S. Miller <davem@redhat.com> 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 161da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 171da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 181da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 191da177e4SLinus Torvalds */ 201da177e4SLinus Torvalds 21afab2d29SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 22afab2d29SJoe Perches 234fc268d2SRandy Dunlap #include <linux/capability.h> 241da177e4SLinus Torvalds #include <linux/module.h> 251da177e4SLinus Torvalds #include <linux/netdevice.h> 261da177e4SLinus Torvalds #include <linux/skbuff.h> 275a0e3ad6STejun Heo #include <linux/slab.h> 281da177e4SLinus Torvalds #include <linux/init.h> 2982524746SFranck Bui-Huu #include <linux/rculist.h> 301da177e4SLinus Torvalds #include <net/p8022.h> 311da177e4SLinus Torvalds #include <net/arp.h> 321da177e4SLinus Torvalds #include <linux/rtnetlink.h> 331da177e4SLinus Torvalds #include <linux/notifier.h> 3461362766SPatrick McHardy #include <net/rtnetlink.h> 35e9dc8653SEric W. Biederman #include <net/net_namespace.h> 36d9ed0f0eSPavel Emelyanov #include <net/netns/generic.h> 3761362766SPatrick McHardy #include <asm/uaccess.h> 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds #include <linux/if_vlan.h> 401da177e4SLinus Torvalds #include "vlan.h" 411da177e4SLinus Torvalds #include "vlanproc.h" 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds #define DRV_VERSION "1.8" 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds /* Global VLAN variables */ 461da177e4SLinus Torvalds 47f99189b1SEric Dumazet int vlan_net_id __read_mostly; 48d9ed0f0eSPavel Emelyanov 49b3020061SStephen Hemminger const char vlan_fullname[] = "802.1Q VLAN Support"; 50b3020061SStephen Hemminger const char vlan_version[] = DRV_VERSION; 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds /* End of global variables definitions. */ 531da177e4SLinus Torvalds 541fd9b1fcSPatrick McHardy static int vlan_group_prealloc_vid(struct vlan_group *vg, 551fd9b1fcSPatrick McHardy __be16 vlan_proto, u16 vlan_id) 5667727184SPavel Emelyanov { 5767727184SPavel Emelyanov struct net_device **array; 581fd9b1fcSPatrick McHardy unsigned int pidx, vidx; 5967727184SPavel Emelyanov unsigned int size; 6067727184SPavel Emelyanov 6167727184SPavel Emelyanov ASSERT_RTNL(); 6267727184SPavel Emelyanov 631fd9b1fcSPatrick McHardy pidx = vlan_proto_idx(vlan_proto); 641fd9b1fcSPatrick McHardy vidx = vlan_id / VLAN_GROUP_ARRAY_PART_LEN; 651fd9b1fcSPatrick McHardy array = vg->vlan_devices_arrays[pidx][vidx]; 6667727184SPavel Emelyanov if (array != NULL) 6767727184SPavel Emelyanov return 0; 6867727184SPavel Emelyanov 6967727184SPavel Emelyanov size = sizeof(struct net_device *) * VLAN_GROUP_ARRAY_PART_LEN; 7067727184SPavel Emelyanov array = kzalloc(size, GFP_KERNEL); 7167727184SPavel Emelyanov if (array == NULL) 7267727184SPavel Emelyanov return -ENOBUFS; 7367727184SPavel Emelyanov 741fd9b1fcSPatrick McHardy vg->vlan_devices_arrays[pidx][vidx] = array; 7567727184SPavel Emelyanov return 0; 7642429aaeSPatrick McHardy } 7742429aaeSPatrick McHardy 7823289a37SEric Dumazet void unregister_vlan_dev(struct net_device *dev, struct list_head *head) 791da177e4SLinus Torvalds { 807da82c06SJiri Pirko struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 81af301517SPatrick McHardy struct net_device *real_dev = vlan->real_dev; 825b9ea6e0SJiri Pirko struct vlan_info *vlan_info; 831da177e4SLinus Torvalds struct vlan_group *grp; 849bb8582eSPatrick McHardy u16 vlan_id = vlan->vlan_id; 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds ASSERT_RTNL(); 871da177e4SLinus Torvalds 885b9ea6e0SJiri Pirko vlan_info = rtnl_dereference(real_dev->vlan_info); 895b9ea6e0SJiri Pirko BUG_ON(!vlan_info); 905b9ea6e0SJiri Pirko 915b9ea6e0SJiri Pirko grp = &vlan_info->grp; 92acc5efbcSPatrick McHardy 935b9ea6e0SJiri Pirko grp->nr_vlan_devs--; 94af301517SPatrick McHardy 9586fbe9bbSDavid Ward if (vlan->flags & VLAN_FLAG_MVRP) 9686fbe9bbSDavid Ward vlan_mvrp_request_leave(dev); 9755aee10dSEric Dumazet if (vlan->flags & VLAN_FLAG_GVRP) 9855aee10dSEric Dumazet vlan_gvrp_request_leave(dev); 9955aee10dSEric Dumazet 1001fd9b1fcSPatrick McHardy vlan_group_set_device(grp, vlan->vlan_proto, vlan_id, NULL); 10147701a36SVeaceslav Falico 10247701a36SVeaceslav Falico netdev_upper_dev_unlink(real_dev, dev); 10348752e1bSEric Dumazet /* Because unregister_netdevice_queue() makes sure at least one rcu 10448752e1bSEric Dumazet * grace period is respected before device freeing, 10548752e1bSEric Dumazet * we dont need to call synchronize_net() here. 10648752e1bSEric Dumazet */ 10723289a37SEric Dumazet unregister_netdevice_queue(dev, head); 108ce305002SPatrick McHardy 10986fbe9bbSDavid Ward if (grp->nr_vlan_devs == 0) { 11086fbe9bbSDavid Ward vlan_mvrp_uninit_applicant(real_dev); 11170c03b49SPatrick McHardy vlan_gvrp_uninit_applicant(real_dev); 11286fbe9bbSDavid Ward } 11370c03b49SPatrick McHardy 1144a7df340SCong Wang /* Take it out of our own structures, but be sure to interlock with 1154a7df340SCong Wang * HW accelerating devices or SW vlan input packet processing if 1164a7df340SCong Wang * VLAN is not 0 (leave it there for 802.1p). 1174a7df340SCong Wang */ 1184a7df340SCong Wang if (vlan_id) 1191fd9b1fcSPatrick McHardy vlan_vid_del(real_dev, vlan->vlan_proto, vlan_id); 1204a7df340SCong Wang 121af301517SPatrick McHardy /* Get rid of the vlan's reference to real_dev */ 122af301517SPatrick McHardy dev_put(real_dev); 1231da177e4SLinus Torvalds } 1241da177e4SLinus Torvalds 1251fd9b1fcSPatrick McHardy int vlan_check_real_dev(struct net_device *real_dev, 1261fd9b1fcSPatrick McHardy __be16 protocol, u16 vlan_id) 127c1d3ee99SPatrick McHardy { 128656299f7SStephen Hemminger const char *name = real_dev->name; 12940f98e1aSPatrick McHardy 130c1d3ee99SPatrick McHardy if (real_dev->features & NETIF_F_VLAN_CHALLENGED) { 131afab2d29SJoe Perches pr_info("VLANs not supported on %s\n", name); 132c1d3ee99SPatrick McHardy return -EOPNOTSUPP; 133c1d3ee99SPatrick McHardy } 134c1d3ee99SPatrick McHardy 1351fd9b1fcSPatrick McHardy if (vlan_find_dev(real_dev, protocol, vlan_id) != NULL) 136c1d3ee99SPatrick McHardy return -EEXIST; 137c1d3ee99SPatrick McHardy 138c1d3ee99SPatrick McHardy return 0; 139c1d3ee99SPatrick McHardy } 140c1d3ee99SPatrick McHardy 14107b5b17eSPatrick McHardy int register_vlan_dev(struct net_device *dev) 142e89fe42cSPatrick McHardy { 1437da82c06SJiri Pirko struct vlan_dev_priv *vlan = vlan_dev_priv(dev); 144e89fe42cSPatrick McHardy struct net_device *real_dev = vlan->real_dev; 1459bb8582eSPatrick McHardy u16 vlan_id = vlan->vlan_id; 1465b9ea6e0SJiri Pirko struct vlan_info *vlan_info; 1475b9ea6e0SJiri Pirko struct vlan_group *grp; 148e89fe42cSPatrick McHardy int err; 149e89fe42cSPatrick McHardy 1501fd9b1fcSPatrick McHardy err = vlan_vid_add(real_dev, vlan->vlan_proto, vlan_id); 1515b9ea6e0SJiri Pirko if (err) 1525b9ea6e0SJiri Pirko return err; 1535b9ea6e0SJiri Pirko 1545b9ea6e0SJiri Pirko vlan_info = rtnl_dereference(real_dev->vlan_info); 1555b9ea6e0SJiri Pirko /* vlan_info should be there now. vlan_vid_add took care of it */ 1565b9ea6e0SJiri Pirko BUG_ON(!vlan_info); 1575b9ea6e0SJiri Pirko 1585b9ea6e0SJiri Pirko grp = &vlan_info->grp; 1595b9ea6e0SJiri Pirko if (grp->nr_vlan_devs == 0) { 16070c03b49SPatrick McHardy err = vlan_gvrp_init_applicant(real_dev); 16170c03b49SPatrick McHardy if (err < 0) 1625b9ea6e0SJiri Pirko goto out_vid_del; 16386fbe9bbSDavid Ward err = vlan_mvrp_init_applicant(real_dev); 16486fbe9bbSDavid Ward if (err < 0) 16586fbe9bbSDavid Ward goto out_uninit_gvrp; 166e89fe42cSPatrick McHardy } 167e89fe42cSPatrick McHardy 1681fd9b1fcSPatrick McHardy err = vlan_group_prealloc_vid(grp, vlan->vlan_proto, vlan_id); 16967727184SPavel Emelyanov if (err < 0) 17086fbe9bbSDavid Ward goto out_uninit_mvrp; 17167727184SPavel Emelyanov 172d38569abSVlad Yasevich vlan->nest_level = dev_get_nest_level(real_dev, is_vlan_dev) + 1; 173e89fe42cSPatrick McHardy err = register_netdevice(dev); 174e89fe42cSPatrick McHardy if (err < 0) 1755df27e6cSVeaceslav Falico goto out_uninit_mvrp; 1765df27e6cSVeaceslav Falico 1775df27e6cSVeaceslav Falico err = netdev_upper_dev_link(real_dev, dev); 1785df27e6cSVeaceslav Falico if (err) 1795df27e6cSVeaceslav Falico goto out_unregister_netdev; 180e89fe42cSPatrick McHardy 1817da82c06SJiri Pirko /* Account for reference in struct vlan_dev_priv */ 182e89fe42cSPatrick McHardy dev_hold(real_dev); 183e89fe42cSPatrick McHardy 184fc4a7489SPatrick Mullaney netif_stacked_transfer_operstate(real_dev, dev); 185e89fe42cSPatrick McHardy linkwatch_fire_event(dev); /* _MUST_ call rfc2863_policy() */ 186e89fe42cSPatrick McHardy 187e89fe42cSPatrick McHardy /* So, got the sucker initialized, now lets place 188e89fe42cSPatrick McHardy * it into our local structure. 189e89fe42cSPatrick McHardy */ 1901fd9b1fcSPatrick McHardy vlan_group_set_device(grp, vlan->vlan_proto, vlan_id, dev); 1915b9ea6e0SJiri Pirko grp->nr_vlan_devs++; 192e89fe42cSPatrick McHardy 193e89fe42cSPatrick McHardy return 0; 194e89fe42cSPatrick McHardy 1955df27e6cSVeaceslav Falico out_unregister_netdev: 1965df27e6cSVeaceslav Falico unregister_netdevice(dev); 19786fbe9bbSDavid Ward out_uninit_mvrp: 19886fbe9bbSDavid Ward if (grp->nr_vlan_devs == 0) 19986fbe9bbSDavid Ward vlan_mvrp_uninit_applicant(real_dev); 20086fbe9bbSDavid Ward out_uninit_gvrp: 2015b9ea6e0SJiri Pirko if (grp->nr_vlan_devs == 0) 20270c03b49SPatrick McHardy vlan_gvrp_uninit_applicant(real_dev); 2035b9ea6e0SJiri Pirko out_vid_del: 2041fd9b1fcSPatrick McHardy vlan_vid_del(real_dev, vlan->vlan_proto, vlan_id); 205e89fe42cSPatrick McHardy return err; 206e89fe42cSPatrick McHardy } 207e89fe42cSPatrick McHardy 2081da177e4SLinus Torvalds /* Attach a VLAN device to a mac address (ie Ethernet Card). 2092ae0bf69SPatrick McHardy * Returns 0 if the device was created or a negative error code otherwise. 2101da177e4SLinus Torvalds */ 2119bb8582eSPatrick McHardy static int register_vlan_device(struct net_device *real_dev, u16 vlan_id) 2121da177e4SLinus Torvalds { 2131da177e4SLinus Torvalds struct net_device *new_dev; 2140c0667a8SWang Sheng-Hui struct vlan_dev_priv *vlan; 2157a17a2f7SPavel Emelyanov struct net *net = dev_net(real_dev); 2167a17a2f7SPavel Emelyanov struct vlan_net *vn = net_generic(net, vlan_net_id); 2171da177e4SLinus Torvalds char name[IFNAMSIZ]; 2182ae0bf69SPatrick McHardy int err; 2191da177e4SLinus Torvalds 2209bb8582eSPatrick McHardy if (vlan_id >= VLAN_VID_MASK) 2212ae0bf69SPatrick McHardy return -ERANGE; 2221da177e4SLinus Torvalds 2231fd9b1fcSPatrick McHardy err = vlan_check_real_dev(real_dev, htons(ETH_P_8021Q), vlan_id); 2242ae0bf69SPatrick McHardy if (err < 0) 2252ae0bf69SPatrick McHardy return err; 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds /* Gotta set up the fields for the device. */ 2287a17a2f7SPavel Emelyanov switch (vn->name_type) { 2291da177e4SLinus Torvalds case VLAN_NAME_TYPE_RAW_PLUS_VID: 2301da177e4SLinus Torvalds /* name will look like: eth1.0005 */ 2319bb8582eSPatrick McHardy snprintf(name, IFNAMSIZ, "%s.%.4i", real_dev->name, vlan_id); 2321da177e4SLinus Torvalds break; 2331da177e4SLinus Torvalds case VLAN_NAME_TYPE_PLUS_VID_NO_PAD: 2341da177e4SLinus Torvalds /* Put our vlan.VID in the name. 2351da177e4SLinus Torvalds * Name will look like: vlan5 2361da177e4SLinus Torvalds */ 2379bb8582eSPatrick McHardy snprintf(name, IFNAMSIZ, "vlan%i", vlan_id); 2381da177e4SLinus Torvalds break; 2391da177e4SLinus Torvalds case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD: 2401da177e4SLinus Torvalds /* Put our vlan.VID in the name. 2411da177e4SLinus Torvalds * Name will look like: eth0.5 2421da177e4SLinus Torvalds */ 2439bb8582eSPatrick McHardy snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, vlan_id); 2441da177e4SLinus Torvalds break; 2451da177e4SLinus Torvalds case VLAN_NAME_TYPE_PLUS_VID: 2461da177e4SLinus Torvalds /* Put our vlan.VID in the name. 2471da177e4SLinus Torvalds * Name will look like: vlan0005 2481da177e4SLinus Torvalds */ 2491da177e4SLinus Torvalds default: 2509bb8582eSPatrick McHardy snprintf(name, IFNAMSIZ, "vlan%.4i", vlan_id); 2513ff50b79SStephen Hemminger } 2521da177e4SLinus Torvalds 253*c835a677STom Gundersen new_dev = alloc_netdev(sizeof(struct vlan_dev_priv), name, 254*c835a677STom Gundersen NET_NAME_UNKNOWN, vlan_setup); 2555dd8d1e9SArjan van de Ven 2561da177e4SLinus Torvalds if (new_dev == NULL) 2572ae0bf69SPatrick McHardy return -ENOBUFS; 2581da177e4SLinus Torvalds 25965d292a2SPavel Emelyanov dev_net_set(new_dev, net); 2601da177e4SLinus Torvalds /* need 4 bytes for extra VLAN header info, 2611da177e4SLinus Torvalds * hope the underlying device can handle it. 2621da177e4SLinus Torvalds */ 2631da177e4SLinus Torvalds new_dev->mtu = real_dev->mtu; 2646e22ce2cSYi Zou new_dev->priv_flags |= (real_dev->priv_flags & IFF_UNICAST_FLT); 2651da177e4SLinus Torvalds 2660c0667a8SWang Sheng-Hui vlan = vlan_dev_priv(new_dev); 2670c0667a8SWang Sheng-Hui vlan->vlan_proto = htons(ETH_P_8021Q); 2680c0667a8SWang Sheng-Hui vlan->vlan_id = vlan_id; 2690c0667a8SWang Sheng-Hui vlan->real_dev = real_dev; 2700c0667a8SWang Sheng-Hui vlan->dent = NULL; 2710c0667a8SWang Sheng-Hui vlan->flags = VLAN_FLAG_REORDER_HDR; 2721da177e4SLinus Torvalds 27307b5b17eSPatrick McHardy new_dev->rtnl_link_ops = &vlan_link_ops; 2742ae0bf69SPatrick McHardy err = register_vlan_dev(new_dev); 2752ae0bf69SPatrick McHardy if (err < 0) 27642429aaeSPatrick McHardy goto out_free_newdev; 2771da177e4SLinus Torvalds 2782ae0bf69SPatrick McHardy return 0; 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds out_free_newdev: 2811da177e4SLinus Torvalds free_netdev(new_dev); 2822ae0bf69SPatrick McHardy return err; 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds 2858c979c26SPatrick McHardy static void vlan_sync_address(struct net_device *dev, 2868c979c26SPatrick McHardy struct net_device *vlandev) 2878c979c26SPatrick McHardy { 2887da82c06SJiri Pirko struct vlan_dev_priv *vlan = vlan_dev_priv(vlandev); 2898c979c26SPatrick McHardy 2908c979c26SPatrick McHardy /* May be called without an actual change */ 29153a2b3a1SJoe Perches if (ether_addr_equal(vlan->real_dev_addr, dev->dev_addr)) 2928c979c26SPatrick McHardy return; 2938c979c26SPatrick McHardy 2948c979c26SPatrick McHardy /* vlan address was different from the old address and is equal to 2958c979c26SPatrick McHardy * the new address */ 29653a2b3a1SJoe Perches if (!ether_addr_equal(vlandev->dev_addr, vlan->real_dev_addr) && 29753a2b3a1SJoe Perches ether_addr_equal(vlandev->dev_addr, dev->dev_addr)) 298a748ee24SJiri Pirko dev_uc_del(dev, vlandev->dev_addr); 2998c979c26SPatrick McHardy 3008c979c26SPatrick McHardy /* vlan address was equal to the old address and is different from 3018c979c26SPatrick McHardy * the new address */ 30253a2b3a1SJoe Perches if (ether_addr_equal(vlandev->dev_addr, vlan->real_dev_addr) && 30353a2b3a1SJoe Perches !ether_addr_equal(vlandev->dev_addr, dev->dev_addr)) 304a748ee24SJiri Pirko dev_uc_add(dev, vlandev->dev_addr); 3058c979c26SPatrick McHardy 30607fc67beSJoe Perches ether_addr_copy(vlan->real_dev_addr, dev->dev_addr); 3078c979c26SPatrick McHardy } 3088c979c26SPatrick McHardy 3095fb13570SPatrick McHardy static void vlan_transfer_features(struct net_device *dev, 3105fb13570SPatrick McHardy struct net_device *vlandev) 3115fb13570SPatrick McHardy { 312fc0d48b8SVlad Yasevich struct vlan_dev_priv *vlan = vlan_dev_priv(vlandev); 313fc0d48b8SVlad Yasevich 3141ae4be22SAlexander Duyck vlandev->gso_max_size = dev->gso_max_size; 315029f5fc3SJohn Fastabend 316fc0d48b8SVlad Yasevich if (vlan_hw_offload_capable(dev->features, vlan->vlan_proto)) 317029f5fc3SJohn Fastabend vlandev->hard_header_len = dev->hard_header_len; 318029f5fc3SJohn Fastabend else 319029f5fc3SJohn Fastabend vlandev->hard_header_len = dev->hard_header_len + VLAN_HLEN; 320029f5fc3SJohn Fastabend 321f4d5392eSAmerigo Wang #if IS_ENABLED(CONFIG_FCOE) 322b85daa53SVasu Dev vlandev->fcoe_ddp_xid = dev->fcoe_ddp_xid; 323b85daa53SVasu Dev #endif 3248a0427bbSMichał Mirosław 3258a0427bbSMichał Mirosław netdev_update_features(vlandev); 3265fb13570SPatrick McHardy } 3275fb13570SPatrick McHardy 328802fb176SPavel Emelyanov static void __vlan_device_event(struct net_device *dev, unsigned long event) 329802fb176SPavel Emelyanov { 330802fb176SPavel Emelyanov switch (event) { 331802fb176SPavel Emelyanov case NETDEV_CHANGENAME: 332802fb176SPavel Emelyanov vlan_proc_rem_dev(dev); 333802fb176SPavel Emelyanov if (vlan_proc_add_dev(dev) < 0) 334afab2d29SJoe Perches pr_warn("failed to change proc name for %s\n", 335802fb176SPavel Emelyanov dev->name); 336802fb176SPavel Emelyanov break; 33730688a9aSPavel Emelyanov case NETDEV_REGISTER: 33830688a9aSPavel Emelyanov if (vlan_proc_add_dev(dev) < 0) 339afab2d29SJoe Perches pr_warn("failed to add proc entry for %s\n", dev->name); 34030688a9aSPavel Emelyanov break; 34130688a9aSPavel Emelyanov case NETDEV_UNREGISTER: 34230688a9aSPavel Emelyanov vlan_proc_rem_dev(dev); 34330688a9aSPavel Emelyanov break; 344802fb176SPavel Emelyanov } 345802fb176SPavel Emelyanov } 346802fb176SPavel Emelyanov 3472029cc2cSPatrick McHardy static int vlan_device_event(struct notifier_block *unused, unsigned long event, 3482029cc2cSPatrick McHardy void *ptr) 3491da177e4SLinus Torvalds { 350351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 351802fb176SPavel Emelyanov struct vlan_group *grp; 3525b9ea6e0SJiri Pirko struct vlan_info *vlan_info; 3531da177e4SLinus Torvalds int i, flgs; 3541da177e4SLinus Torvalds struct net_device *vlandev; 3557da82c06SJiri Pirko struct vlan_dev_priv *vlan; 3561fd9b1fcSPatrick McHardy bool last = false; 35729906f6aSPatrick McHardy LIST_HEAD(list); 3581da177e4SLinus Torvalds 35981d85346SPatrick McHardy if (is_vlan_dev(dev)) 360802fb176SPavel Emelyanov __vlan_device_event(dev, event); 361802fb176SPavel Emelyanov 362ad1afb00SPedro Garcia if ((event == NETDEV_UP) && 363f646968fSPatrick McHardy (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER)) { 364afab2d29SJoe Perches pr_info("adding VLAN 0 to HW filter on device %s\n", 365ad1afb00SPedro Garcia dev->name); 36680d5c368SPatrick McHardy vlan_vid_add(dev, htons(ETH_P_8021Q), 0); 367ad1afb00SPedro Garcia } 368ad1afb00SPedro Garcia 3695b9ea6e0SJiri Pirko vlan_info = rtnl_dereference(dev->vlan_info); 3705b9ea6e0SJiri Pirko if (!vlan_info) 3711da177e4SLinus Torvalds goto out; 3725b9ea6e0SJiri Pirko grp = &vlan_info->grp; 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds /* It is OK that we do not hold the group lock right now, 3751da177e4SLinus Torvalds * as we run under the RTNL lock. 3761da177e4SLinus Torvalds */ 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds switch (event) { 3791da177e4SLinus Torvalds case NETDEV_CHANGE: 3801da177e4SLinus Torvalds /* Propagate real device state to vlan devices */ 3811fd9b1fcSPatrick McHardy vlan_group_for_each_dev(grp, i, vlandev) 382fc4a7489SPatrick Mullaney netif_stacked_transfer_operstate(dev, vlandev); 3831da177e4SLinus Torvalds break; 3841da177e4SLinus Torvalds 3858c979c26SPatrick McHardy case NETDEV_CHANGEADDR: 3868c979c26SPatrick McHardy /* Adjust unicast filters on underlying device */ 3871fd9b1fcSPatrick McHardy vlan_group_for_each_dev(grp, i, vlandev) { 388d932e04aSPatrick McHardy flgs = vlandev->flags; 389d932e04aSPatrick McHardy if (!(flgs & IFF_UP)) 390d932e04aSPatrick McHardy continue; 391d932e04aSPatrick McHardy 3928c979c26SPatrick McHardy vlan_sync_address(dev, vlandev); 3938c979c26SPatrick McHardy } 3948c979c26SPatrick McHardy break; 3958c979c26SPatrick McHardy 3962e477c9bSHerbert Xu case NETDEV_CHANGEMTU: 3971fd9b1fcSPatrick McHardy vlan_group_for_each_dev(grp, i, vlandev) { 3982e477c9bSHerbert Xu if (vlandev->mtu <= dev->mtu) 3992e477c9bSHerbert Xu continue; 4002e477c9bSHerbert Xu 4012e477c9bSHerbert Xu dev_set_mtu(vlandev, dev->mtu); 4022e477c9bSHerbert Xu } 4032e477c9bSHerbert Xu break; 4042e477c9bSHerbert Xu 4055fb13570SPatrick McHardy case NETDEV_FEAT_CHANGE: 4065fb13570SPatrick McHardy /* Propagate device features to underlying device */ 4071fd9b1fcSPatrick McHardy vlan_group_for_each_dev(grp, i, vlandev) 4085fb13570SPatrick McHardy vlan_transfer_features(dev, vlandev); 4095fb13570SPatrick McHardy break; 4105fb13570SPatrick McHardy 4111da177e4SLinus Torvalds case NETDEV_DOWN: 412f646968fSPatrick McHardy if (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER) 41380d5c368SPatrick McHardy vlan_vid_del(dev, htons(ETH_P_8021Q), 0); 414efc73f4bSAmir Hanania 4151da177e4SLinus Torvalds /* Put all VLANs for this dev in the down state too. */ 4161fd9b1fcSPatrick McHardy vlan_group_for_each_dev(grp, i, vlandev) { 4171da177e4SLinus Torvalds flgs = vlandev->flags; 4181da177e4SLinus Torvalds if (!(flgs & IFF_UP)) 4191da177e4SLinus Torvalds continue; 4201da177e4SLinus Torvalds 4217da82c06SJiri Pirko vlan = vlan_dev_priv(vlandev); 4225e756593SPatrick McHardy if (!(vlan->flags & VLAN_FLAG_LOOSE_BINDING)) 4231da177e4SLinus Torvalds dev_change_flags(vlandev, flgs & ~IFF_UP); 424fc4a7489SPatrick Mullaney netif_stacked_transfer_operstate(dev, vlandev); 4251da177e4SLinus Torvalds } 4261da177e4SLinus Torvalds break; 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds case NETDEV_UP: 4291da177e4SLinus Torvalds /* Put all VLANs for this dev in the up state too. */ 4301fd9b1fcSPatrick McHardy vlan_group_for_each_dev(grp, i, vlandev) { 4311da177e4SLinus Torvalds flgs = vlandev->flags; 4321da177e4SLinus Torvalds if (flgs & IFF_UP) 4331da177e4SLinus Torvalds continue; 4341da177e4SLinus Torvalds 4357da82c06SJiri Pirko vlan = vlan_dev_priv(vlandev); 4365e756593SPatrick McHardy if (!(vlan->flags & VLAN_FLAG_LOOSE_BINDING)) 4371da177e4SLinus Torvalds dev_change_flags(vlandev, flgs | IFF_UP); 438fc4a7489SPatrick Mullaney netif_stacked_transfer_operstate(dev, vlandev); 4391da177e4SLinus Torvalds } 4401da177e4SLinus Torvalds break; 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds case NETDEV_UNREGISTER: 4433b27e105SDavid Lamparter /* twiddle thumbs on netns device moves */ 4443b27e105SDavid Lamparter if (dev->reg_state != NETREG_UNREGISTERING) 4453b27e105SDavid Lamparter break; 4463b27e105SDavid Lamparter 4471fd9b1fcSPatrick McHardy vlan_group_for_each_dev(grp, i, vlandev) { 4485b9ea6e0SJiri Pirko /* removal of last vid destroys vlan_info, abort 44929906f6aSPatrick McHardy * afterwards */ 4505b9ea6e0SJiri Pirko if (vlan_info->nr_vids == 1) 4511fd9b1fcSPatrick McHardy last = true; 45229906f6aSPatrick McHardy 45329906f6aSPatrick McHardy unregister_vlan_dev(vlandev, &list); 4541fd9b1fcSPatrick McHardy if (last) 4551fd9b1fcSPatrick McHardy break; 45629906f6aSPatrick McHardy } 45729906f6aSPatrick McHardy unregister_netdevice_many(&list); 4581da177e4SLinus Torvalds break; 4591c01fe14SJiri Pirko 4601c01fe14SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 4611c01fe14SJiri Pirko /* Forbid underlaying device to change its type. */ 46218c22a03SJiri Pirko if (vlan_uses_dev(dev)) 4631c01fe14SJiri Pirko return NOTIFY_BAD; 46418c22a03SJiri Pirko break; 46599606477SBen Hutchings 46699606477SBen Hutchings case NETDEV_NOTIFY_PEERS: 4677c899432SBen Hutchings case NETDEV_BONDING_FAILOVER: 4684aa5dee4SJiri Pirko case NETDEV_RESEND_IGMP: 46999606477SBen Hutchings /* Propagate to vlan devices */ 4701fd9b1fcSPatrick McHardy vlan_group_for_each_dev(grp, i, vlandev) 4717c899432SBen Hutchings call_netdevice_notifiers(event, vlandev); 47299606477SBen Hutchings break; 4733ff50b79SStephen Hemminger } 4741da177e4SLinus Torvalds 4751da177e4SLinus Torvalds out: 4761da177e4SLinus Torvalds return NOTIFY_DONE; 4771da177e4SLinus Torvalds } 4781da177e4SLinus Torvalds 47969ab4b7dSPatrick McHardy static struct notifier_block vlan_notifier_block __read_mostly = { 48069ab4b7dSPatrick McHardy .notifier_call = vlan_device_event, 48169ab4b7dSPatrick McHardy }; 48269ab4b7dSPatrick McHardy 4831da177e4SLinus Torvalds /* 4841da177e4SLinus Torvalds * VLAN IOCTL handler. 4851da177e4SLinus Torvalds * o execute requested action or pass command to the device driver 4861da177e4SLinus Torvalds * arg is really a struct vlan_ioctl_args __user *. 4871da177e4SLinus Torvalds */ 488881d966bSEric W. Biederman static int vlan_ioctl_handler(struct net *net, void __user *arg) 4891da177e4SLinus Torvalds { 490c17d8874SPatrick McHardy int err; 4911da177e4SLinus Torvalds struct vlan_ioctl_args args; 492c17d8874SPatrick McHardy struct net_device *dev = NULL; 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args))) 4951da177e4SLinus Torvalds return -EFAULT; 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds /* Null terminate this sucker, just in case. */ 4981da177e4SLinus Torvalds args.device1[23] = 0; 4991da177e4SLinus Torvalds args.u.device2[23] = 0; 5001da177e4SLinus Torvalds 501c17d8874SPatrick McHardy rtnl_lock(); 502c17d8874SPatrick McHardy 5031da177e4SLinus Torvalds switch (args.cmd) { 5041da177e4SLinus Torvalds case SET_VLAN_INGRESS_PRIORITY_CMD: 505c17d8874SPatrick McHardy case SET_VLAN_EGRESS_PRIORITY_CMD: 506c17d8874SPatrick McHardy case SET_VLAN_FLAG_CMD: 507c17d8874SPatrick McHardy case ADD_VLAN_CMD: 508c17d8874SPatrick McHardy case DEL_VLAN_CMD: 509c17d8874SPatrick McHardy case GET_VLAN_REALDEV_NAME_CMD: 510c17d8874SPatrick McHardy case GET_VLAN_VID_CMD: 511c17d8874SPatrick McHardy err = -ENODEV; 51265d292a2SPavel Emelyanov dev = __dev_get_by_name(net, args.device1); 513c17d8874SPatrick McHardy if (!dev) 514c17d8874SPatrick McHardy goto out; 515c17d8874SPatrick McHardy 516c17d8874SPatrick McHardy err = -EINVAL; 51726a25239SJoonwoo Park if (args.cmd != ADD_VLAN_CMD && !is_vlan_dev(dev)) 518c17d8874SPatrick McHardy goto out; 519c17d8874SPatrick McHardy } 520c17d8874SPatrick McHardy 521c17d8874SPatrick McHardy switch (args.cmd) { 522c17d8874SPatrick McHardy case SET_VLAN_INGRESS_PRIORITY_CMD: 523c17d8874SPatrick McHardy err = -EPERM; 524276996fdSEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 525c17d8874SPatrick McHardy break; 526c17d8874SPatrick McHardy vlan_dev_set_ingress_priority(dev, 5271da177e4SLinus Torvalds args.u.skb_priority, 5281da177e4SLinus Torvalds args.vlan_qos); 529fffe470aSPatrick McHardy err = 0; 5301da177e4SLinus Torvalds break; 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds case SET_VLAN_EGRESS_PRIORITY_CMD: 533c17d8874SPatrick McHardy err = -EPERM; 534276996fdSEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 535c17d8874SPatrick McHardy break; 536c17d8874SPatrick McHardy err = vlan_dev_set_egress_priority(dev, 5371da177e4SLinus Torvalds args.u.skb_priority, 5381da177e4SLinus Torvalds args.vlan_qos); 5391da177e4SLinus Torvalds break; 5401da177e4SLinus Torvalds 5411da177e4SLinus Torvalds case SET_VLAN_FLAG_CMD: 542c17d8874SPatrick McHardy err = -EPERM; 543276996fdSEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 544c17d8874SPatrick McHardy break; 545b3ce0325SPatrick McHardy err = vlan_dev_change_flags(dev, 546b3ce0325SPatrick McHardy args.vlan_qos ? args.u.flag : 0, 547b3ce0325SPatrick McHardy args.u.flag); 5481da177e4SLinus Torvalds break; 5491da177e4SLinus Torvalds 5501da177e4SLinus Torvalds case SET_VLAN_NAME_TYPE_CMD: 551c17d8874SPatrick McHardy err = -EPERM; 552276996fdSEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 553e35de026SPavel Emelyanov break; 554c17d8874SPatrick McHardy if ((args.u.name_type >= 0) && 555c17d8874SPatrick McHardy (args.u.name_type < VLAN_NAME_TYPE_HIGHEST)) { 5567a17a2f7SPavel Emelyanov struct vlan_net *vn; 5577a17a2f7SPavel Emelyanov 5587a17a2f7SPavel Emelyanov vn = net_generic(net, vlan_net_id); 5597a17a2f7SPavel Emelyanov vn->name_type = args.u.name_type; 5601da177e4SLinus Torvalds err = 0; 5611da177e4SLinus Torvalds } else { 5621da177e4SLinus Torvalds err = -EINVAL; 5631da177e4SLinus Torvalds } 5641da177e4SLinus Torvalds break; 5651da177e4SLinus Torvalds 5661da177e4SLinus Torvalds case ADD_VLAN_CMD: 567c17d8874SPatrick McHardy err = -EPERM; 568276996fdSEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 569c17d8874SPatrick McHardy break; 5702ae0bf69SPatrick McHardy err = register_vlan_device(dev, args.u.VID); 5711da177e4SLinus Torvalds break; 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds case DEL_VLAN_CMD: 574c17d8874SPatrick McHardy err = -EPERM; 575276996fdSEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 576c17d8874SPatrick McHardy break; 57723289a37SEric Dumazet unregister_vlan_dev(dev, NULL); 578af301517SPatrick McHardy err = 0; 5791da177e4SLinus Torvalds break; 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds case GET_VLAN_REALDEV_NAME_CMD: 5823f5f4346SAndrew Morton err = 0; 583c17d8874SPatrick McHardy vlan_dev_get_realdev_name(dev, args.u.device2); 5841da177e4SLinus Torvalds if (copy_to_user(arg, &args, 5852029cc2cSPatrick McHardy sizeof(struct vlan_ioctl_args))) 5861da177e4SLinus Torvalds err = -EFAULT; 5871da177e4SLinus Torvalds break; 5881da177e4SLinus Torvalds 5891da177e4SLinus Torvalds case GET_VLAN_VID_CMD: 5903f5f4346SAndrew Morton err = 0; 59122d1ba74SPatrick McHardy args.u.VID = vlan_dev_vlan_id(dev); 5921da177e4SLinus Torvalds if (copy_to_user(arg, &args, 5932029cc2cSPatrick McHardy sizeof(struct vlan_ioctl_args))) 5941da177e4SLinus Torvalds err = -EFAULT; 5951da177e4SLinus Torvalds break; 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds default: 598198a291cSPatrick McHardy err = -EOPNOTSUPP; 599c17d8874SPatrick McHardy break; 6003ff50b79SStephen Hemminger } 6017eb1b3d3SMika Kukkonen out: 602c17d8874SPatrick McHardy rtnl_unlock(); 6031da177e4SLinus Torvalds return err; 6041da177e4SLinus Torvalds } 6051da177e4SLinus Torvalds 6062c8c1e72SAlexey Dobriyan static int __net_init vlan_init_net(struct net *net) 607d9ed0f0eSPavel Emelyanov { 608946d1a92SEric W. Biederman struct vlan_net *vn = net_generic(net, vlan_net_id); 609d9ed0f0eSPavel Emelyanov int err; 610d9ed0f0eSPavel Emelyanov 6117a17a2f7SPavel Emelyanov vn->name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD; 6127a17a2f7SPavel Emelyanov 613cd1c7014SPavel Emelyanov err = vlan_proc_init(net); 614cd1c7014SPavel Emelyanov 615d9ed0f0eSPavel Emelyanov return err; 616d9ed0f0eSPavel Emelyanov } 617d9ed0f0eSPavel Emelyanov 6182c8c1e72SAlexey Dobriyan static void __net_exit vlan_exit_net(struct net *net) 619d9ed0f0eSPavel Emelyanov { 620cd1c7014SPavel Emelyanov vlan_proc_cleanup(net); 621d9ed0f0eSPavel Emelyanov } 622d9ed0f0eSPavel Emelyanov 623d9ed0f0eSPavel Emelyanov static struct pernet_operations vlan_net_ops = { 624d9ed0f0eSPavel Emelyanov .init = vlan_init_net, 625d9ed0f0eSPavel Emelyanov .exit = vlan_exit_net, 626946d1a92SEric W. Biederman .id = &vlan_net_id, 627946d1a92SEric W. Biederman .size = sizeof(struct vlan_net), 628d9ed0f0eSPavel Emelyanov }; 629d9ed0f0eSPavel Emelyanov 63069ab4b7dSPatrick McHardy static int __init vlan_proto_init(void) 63169ab4b7dSPatrick McHardy { 63269ab4b7dSPatrick McHardy int err; 63369ab4b7dSPatrick McHardy 634da7c06c4SJustin Mattock pr_info("%s v%s\n", vlan_fullname, vlan_version); 63569ab4b7dSPatrick McHardy 63691e2ff35SEric W. Biederman err = register_pernet_subsys(&vlan_net_ops); 637d9ed0f0eSPavel Emelyanov if (err < 0) 638d9ed0f0eSPavel Emelyanov goto err0; 639d9ed0f0eSPavel Emelyanov 64069ab4b7dSPatrick McHardy err = register_netdevice_notifier(&vlan_notifier_block); 64169ab4b7dSPatrick McHardy if (err < 0) 64269ab4b7dSPatrick McHardy goto err2; 64369ab4b7dSPatrick McHardy 64470c03b49SPatrick McHardy err = vlan_gvrp_init(); 64569ab4b7dSPatrick McHardy if (err < 0) 64669ab4b7dSPatrick McHardy goto err3; 64769ab4b7dSPatrick McHardy 64886fbe9bbSDavid Ward err = vlan_mvrp_init(); 64970c03b49SPatrick McHardy if (err < 0) 65070c03b49SPatrick McHardy goto err4; 65170c03b49SPatrick McHardy 65286fbe9bbSDavid Ward err = vlan_netlink_init(); 65386fbe9bbSDavid Ward if (err < 0) 65486fbe9bbSDavid Ward goto err5; 65586fbe9bbSDavid Ward 65669ab4b7dSPatrick McHardy vlan_ioctl_set(vlan_ioctl_handler); 65769ab4b7dSPatrick McHardy return 0; 65869ab4b7dSPatrick McHardy 65986fbe9bbSDavid Ward err5: 66086fbe9bbSDavid Ward vlan_mvrp_uninit(); 66170c03b49SPatrick McHardy err4: 66270c03b49SPatrick McHardy vlan_gvrp_uninit(); 66369ab4b7dSPatrick McHardy err3: 66469ab4b7dSPatrick McHardy unregister_netdevice_notifier(&vlan_notifier_block); 66569ab4b7dSPatrick McHardy err2: 66691e2ff35SEric W. Biederman unregister_pernet_subsys(&vlan_net_ops); 667d9ed0f0eSPavel Emelyanov err0: 66869ab4b7dSPatrick McHardy return err; 66969ab4b7dSPatrick McHardy } 67069ab4b7dSPatrick McHardy 67169ab4b7dSPatrick McHardy static void __exit vlan_cleanup_module(void) 67269ab4b7dSPatrick McHardy { 67369ab4b7dSPatrick McHardy vlan_ioctl_set(NULL); 67469ab4b7dSPatrick McHardy vlan_netlink_fini(); 67569ab4b7dSPatrick McHardy 67669ab4b7dSPatrick McHardy unregister_netdevice_notifier(&vlan_notifier_block); 67769ab4b7dSPatrick McHardy 67891e2ff35SEric W. Biederman unregister_pernet_subsys(&vlan_net_ops); 6796e327c11SJesper Dangaard Brouer rcu_barrier(); /* Wait for completion of call_rcu()'s */ 68070c03b49SPatrick McHardy 68186fbe9bbSDavid Ward vlan_mvrp_uninit(); 68270c03b49SPatrick McHardy vlan_gvrp_uninit(); 68369ab4b7dSPatrick McHardy } 68469ab4b7dSPatrick McHardy 68569ab4b7dSPatrick McHardy module_init(vlan_proto_init); 68669ab4b7dSPatrick McHardy module_exit(vlan_cleanup_module); 68769ab4b7dSPatrick McHardy 6881da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 6891da177e4SLinus Torvalds MODULE_VERSION(DRV_VERSION); 690