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 54*1fd9b1fcSPatrick McHardy static int vlan_group_prealloc_vid(struct vlan_group *vg, 55*1fd9b1fcSPatrick McHardy __be16 vlan_proto, u16 vlan_id) 5667727184SPavel Emelyanov { 5767727184SPavel Emelyanov struct net_device **array; 58*1fd9b1fcSPatrick McHardy unsigned int pidx, vidx; 5967727184SPavel Emelyanov unsigned int size; 6067727184SPavel Emelyanov 6167727184SPavel Emelyanov ASSERT_RTNL(); 6267727184SPavel Emelyanov 63*1fd9b1fcSPatrick McHardy pidx = vlan_proto_idx(vlan_proto); 64*1fd9b1fcSPatrick McHardy vidx = vlan_id / VLAN_GROUP_ARRAY_PART_LEN; 65*1fd9b1fcSPatrick 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 74*1fd9b1fcSPatrick 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 100*1fd9b1fcSPatrick McHardy vlan_group_set_device(grp, vlan->vlan_proto, vlan_id, NULL); 10148752e1bSEric Dumazet /* Because unregister_netdevice_queue() makes sure at least one rcu 10248752e1bSEric Dumazet * grace period is respected before device freeing, 10348752e1bSEric Dumazet * we dont need to call synchronize_net() here. 10448752e1bSEric Dumazet */ 10523289a37SEric Dumazet unregister_netdevice_queue(dev, head); 106ce305002SPatrick McHardy 107126d6c23SJiri Pirko netdev_upper_dev_unlink(real_dev, dev); 108126d6c23SJiri Pirko 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) 119*1fd9b1fcSPatrick 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 125*1fd9b1fcSPatrick McHardy int vlan_check_real_dev(struct net_device *real_dev, 126*1fd9b1fcSPatrick 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 135*1fd9b1fcSPatrick 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 150*1fd9b1fcSPatrick 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 168*1fd9b1fcSPatrick 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 172126d6c23SJiri Pirko err = netdev_upper_dev_link(real_dev, dev); 173126d6c23SJiri Pirko if (err) 17486fbe9bbSDavid Ward goto out_uninit_mvrp; 175126d6c23SJiri Pirko 176e89fe42cSPatrick McHardy err = register_netdevice(dev); 177e89fe42cSPatrick McHardy if (err < 0) 178126d6c23SJiri Pirko goto out_upper_dev_unlink; 179e89fe42cSPatrick McHardy 1807da82c06SJiri Pirko /* Account for reference in struct vlan_dev_priv */ 181e89fe42cSPatrick McHardy dev_hold(real_dev); 182e89fe42cSPatrick McHardy 183fc4a7489SPatrick Mullaney netif_stacked_transfer_operstate(real_dev, dev); 184e89fe42cSPatrick McHardy linkwatch_fire_event(dev); /* _MUST_ call rfc2863_policy() */ 185e89fe42cSPatrick McHardy 186e89fe42cSPatrick McHardy /* So, got the sucker initialized, now lets place 187e89fe42cSPatrick McHardy * it into our local structure. 188e89fe42cSPatrick McHardy */ 189*1fd9b1fcSPatrick McHardy vlan_group_set_device(grp, vlan->vlan_proto, vlan_id, dev); 1905b9ea6e0SJiri Pirko grp->nr_vlan_devs++; 191e89fe42cSPatrick McHardy 192e89fe42cSPatrick McHardy return 0; 193e89fe42cSPatrick McHardy 194126d6c23SJiri Pirko out_upper_dev_unlink: 195126d6c23SJiri Pirko netdev_upper_dev_unlink(real_dev, dev); 19686fbe9bbSDavid Ward out_uninit_mvrp: 19786fbe9bbSDavid Ward if (grp->nr_vlan_devs == 0) 19886fbe9bbSDavid Ward vlan_mvrp_uninit_applicant(real_dev); 19986fbe9bbSDavid Ward out_uninit_gvrp: 2005b9ea6e0SJiri Pirko if (grp->nr_vlan_devs == 0) 20170c03b49SPatrick McHardy vlan_gvrp_uninit_applicant(real_dev); 2025b9ea6e0SJiri Pirko out_vid_del: 203*1fd9b1fcSPatrick McHardy vlan_vid_del(real_dev, vlan->vlan_proto, vlan_id); 204e89fe42cSPatrick McHardy return err; 205e89fe42cSPatrick McHardy } 206e89fe42cSPatrick McHardy 2071da177e4SLinus Torvalds /* Attach a VLAN device to a mac address (ie Ethernet Card). 2082ae0bf69SPatrick McHardy * Returns 0 if the device was created or a negative error code otherwise. 2091da177e4SLinus Torvalds */ 2109bb8582eSPatrick McHardy static int register_vlan_device(struct net_device *real_dev, u16 vlan_id) 2111da177e4SLinus Torvalds { 2121da177e4SLinus Torvalds struct net_device *new_dev; 2137a17a2f7SPavel Emelyanov struct net *net = dev_net(real_dev); 2147a17a2f7SPavel Emelyanov struct vlan_net *vn = net_generic(net, vlan_net_id); 2151da177e4SLinus Torvalds char name[IFNAMSIZ]; 2162ae0bf69SPatrick McHardy int err; 2171da177e4SLinus Torvalds 2189bb8582eSPatrick McHardy if (vlan_id >= VLAN_VID_MASK) 2192ae0bf69SPatrick McHardy return -ERANGE; 2201da177e4SLinus Torvalds 221*1fd9b1fcSPatrick McHardy err = vlan_check_real_dev(real_dev, htons(ETH_P_8021Q), vlan_id); 2222ae0bf69SPatrick McHardy if (err < 0) 2232ae0bf69SPatrick McHardy return err; 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds /* Gotta set up the fields for the device. */ 2267a17a2f7SPavel Emelyanov switch (vn->name_type) { 2271da177e4SLinus Torvalds case VLAN_NAME_TYPE_RAW_PLUS_VID: 2281da177e4SLinus Torvalds /* name will look like: eth1.0005 */ 2299bb8582eSPatrick McHardy snprintf(name, IFNAMSIZ, "%s.%.4i", real_dev->name, vlan_id); 2301da177e4SLinus Torvalds break; 2311da177e4SLinus Torvalds case VLAN_NAME_TYPE_PLUS_VID_NO_PAD: 2321da177e4SLinus Torvalds /* Put our vlan.VID in the name. 2331da177e4SLinus Torvalds * Name will look like: vlan5 2341da177e4SLinus Torvalds */ 2359bb8582eSPatrick McHardy snprintf(name, IFNAMSIZ, "vlan%i", vlan_id); 2361da177e4SLinus Torvalds break; 2371da177e4SLinus Torvalds case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD: 2381da177e4SLinus Torvalds /* Put our vlan.VID in the name. 2391da177e4SLinus Torvalds * Name will look like: eth0.5 2401da177e4SLinus Torvalds */ 2419bb8582eSPatrick McHardy snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, vlan_id); 2421da177e4SLinus Torvalds break; 2431da177e4SLinus Torvalds case VLAN_NAME_TYPE_PLUS_VID: 2441da177e4SLinus Torvalds /* Put our vlan.VID in the name. 2451da177e4SLinus Torvalds * Name will look like: vlan0005 2461da177e4SLinus Torvalds */ 2471da177e4SLinus Torvalds default: 2489bb8582eSPatrick McHardy snprintf(name, IFNAMSIZ, "vlan%.4i", vlan_id); 2493ff50b79SStephen Hemminger } 2501da177e4SLinus Torvalds 2517da82c06SJiri Pirko new_dev = alloc_netdev(sizeof(struct vlan_dev_priv), name, vlan_setup); 2525dd8d1e9SArjan van de Ven 2531da177e4SLinus Torvalds if (new_dev == NULL) 2542ae0bf69SPatrick McHardy return -ENOBUFS; 2551da177e4SLinus Torvalds 25665d292a2SPavel Emelyanov dev_net_set(new_dev, net); 2571da177e4SLinus Torvalds /* need 4 bytes for extra VLAN header info, 2581da177e4SLinus Torvalds * hope the underlying device can handle it. 2591da177e4SLinus Torvalds */ 2601da177e4SLinus Torvalds new_dev->mtu = real_dev->mtu; 2616e22ce2cSYi Zou new_dev->priv_flags |= (real_dev->priv_flags & IFF_UNICAST_FLT); 2621da177e4SLinus Torvalds 263*1fd9b1fcSPatrick McHardy vlan_dev_priv(new_dev)->vlan_proto = htons(ETH_P_8021Q); 2647da82c06SJiri Pirko vlan_dev_priv(new_dev)->vlan_id = vlan_id; 2657da82c06SJiri Pirko vlan_dev_priv(new_dev)->real_dev = real_dev; 2667da82c06SJiri Pirko vlan_dev_priv(new_dev)->dent = NULL; 2677da82c06SJiri Pirko vlan_dev_priv(new_dev)->flags = VLAN_FLAG_REORDER_HDR; 2681da177e4SLinus Torvalds 26907b5b17eSPatrick McHardy new_dev->rtnl_link_ops = &vlan_link_ops; 2702ae0bf69SPatrick McHardy err = register_vlan_dev(new_dev); 2712ae0bf69SPatrick McHardy if (err < 0) 27242429aaeSPatrick McHardy goto out_free_newdev; 2731da177e4SLinus Torvalds 2742ae0bf69SPatrick McHardy return 0; 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds out_free_newdev: 2771da177e4SLinus Torvalds free_netdev(new_dev); 2782ae0bf69SPatrick McHardy return err; 2791da177e4SLinus Torvalds } 2801da177e4SLinus Torvalds 2818c979c26SPatrick McHardy static void vlan_sync_address(struct net_device *dev, 2828c979c26SPatrick McHardy struct net_device *vlandev) 2838c979c26SPatrick McHardy { 2847da82c06SJiri Pirko struct vlan_dev_priv *vlan = vlan_dev_priv(vlandev); 2858c979c26SPatrick McHardy 2868c979c26SPatrick McHardy /* May be called without an actual change */ 28753a2b3a1SJoe Perches if (ether_addr_equal(vlan->real_dev_addr, dev->dev_addr)) 2888c979c26SPatrick McHardy return; 2898c979c26SPatrick McHardy 2908c979c26SPatrick McHardy /* vlan address was different from the old address and is equal to 2918c979c26SPatrick McHardy * the new address */ 29253a2b3a1SJoe Perches if (!ether_addr_equal(vlandev->dev_addr, vlan->real_dev_addr) && 29353a2b3a1SJoe Perches ether_addr_equal(vlandev->dev_addr, dev->dev_addr)) 294a748ee24SJiri Pirko dev_uc_del(dev, vlandev->dev_addr); 2958c979c26SPatrick McHardy 2968c979c26SPatrick McHardy /* vlan address was equal to the old address and is different from 2978c979c26SPatrick McHardy * the new address */ 29853a2b3a1SJoe Perches if (ether_addr_equal(vlandev->dev_addr, vlan->real_dev_addr) && 29953a2b3a1SJoe Perches !ether_addr_equal(vlandev->dev_addr, dev->dev_addr)) 300a748ee24SJiri Pirko dev_uc_add(dev, vlandev->dev_addr); 3018c979c26SPatrick McHardy 3028c979c26SPatrick McHardy memcpy(vlan->real_dev_addr, dev->dev_addr, ETH_ALEN); 3038c979c26SPatrick McHardy } 3048c979c26SPatrick McHardy 3055fb13570SPatrick McHardy static void vlan_transfer_features(struct net_device *dev, 3065fb13570SPatrick McHardy struct net_device *vlandev) 3075fb13570SPatrick McHardy { 3081ae4be22SAlexander Duyck vlandev->gso_max_size = dev->gso_max_size; 309029f5fc3SJohn Fastabend 310f646968fSPatrick McHardy if (dev->features & NETIF_F_HW_VLAN_CTAG_TX) 311029f5fc3SJohn Fastabend vlandev->hard_header_len = dev->hard_header_len; 312029f5fc3SJohn Fastabend else 313029f5fc3SJohn Fastabend vlandev->hard_header_len = dev->hard_header_len + VLAN_HLEN; 314029f5fc3SJohn Fastabend 315f4d5392eSAmerigo Wang #if IS_ENABLED(CONFIG_FCOE) 316b85daa53SVasu Dev vlandev->fcoe_ddp_xid = dev->fcoe_ddp_xid; 317b85daa53SVasu Dev #endif 3188a0427bbSMichał Mirosław 3198a0427bbSMichał Mirosław netdev_update_features(vlandev); 3205fb13570SPatrick McHardy } 3215fb13570SPatrick McHardy 322802fb176SPavel Emelyanov static void __vlan_device_event(struct net_device *dev, unsigned long event) 323802fb176SPavel Emelyanov { 324802fb176SPavel Emelyanov switch (event) { 325802fb176SPavel Emelyanov case NETDEV_CHANGENAME: 326802fb176SPavel Emelyanov vlan_proc_rem_dev(dev); 327802fb176SPavel Emelyanov if (vlan_proc_add_dev(dev) < 0) 328afab2d29SJoe Perches pr_warn("failed to change proc name for %s\n", 329802fb176SPavel Emelyanov dev->name); 330802fb176SPavel Emelyanov break; 33130688a9aSPavel Emelyanov case NETDEV_REGISTER: 33230688a9aSPavel Emelyanov if (vlan_proc_add_dev(dev) < 0) 333afab2d29SJoe Perches pr_warn("failed to add proc entry for %s\n", dev->name); 33430688a9aSPavel Emelyanov break; 33530688a9aSPavel Emelyanov case NETDEV_UNREGISTER: 33630688a9aSPavel Emelyanov vlan_proc_rem_dev(dev); 33730688a9aSPavel Emelyanov break; 338802fb176SPavel Emelyanov } 339802fb176SPavel Emelyanov } 340802fb176SPavel Emelyanov 3412029cc2cSPatrick McHardy static int vlan_device_event(struct notifier_block *unused, unsigned long event, 3422029cc2cSPatrick McHardy void *ptr) 3431da177e4SLinus Torvalds { 3441da177e4SLinus Torvalds struct net_device *dev = ptr; 345802fb176SPavel Emelyanov struct vlan_group *grp; 3465b9ea6e0SJiri Pirko struct vlan_info *vlan_info; 3471da177e4SLinus Torvalds int i, flgs; 3481da177e4SLinus Torvalds struct net_device *vlandev; 3497da82c06SJiri Pirko struct vlan_dev_priv *vlan; 350*1fd9b1fcSPatrick McHardy bool last = false; 35129906f6aSPatrick McHardy LIST_HEAD(list); 3521da177e4SLinus Torvalds 35381d85346SPatrick McHardy if (is_vlan_dev(dev)) 354802fb176SPavel Emelyanov __vlan_device_event(dev, event); 355802fb176SPavel Emelyanov 356ad1afb00SPedro Garcia if ((event == NETDEV_UP) && 357f646968fSPatrick McHardy (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER)) { 358afab2d29SJoe Perches pr_info("adding VLAN 0 to HW filter on device %s\n", 359ad1afb00SPedro Garcia dev->name); 36080d5c368SPatrick McHardy vlan_vid_add(dev, htons(ETH_P_8021Q), 0); 361ad1afb00SPedro Garcia } 362ad1afb00SPedro Garcia 3635b9ea6e0SJiri Pirko vlan_info = rtnl_dereference(dev->vlan_info); 3645b9ea6e0SJiri Pirko if (!vlan_info) 3651da177e4SLinus Torvalds goto out; 3665b9ea6e0SJiri Pirko grp = &vlan_info->grp; 3671da177e4SLinus Torvalds 3681da177e4SLinus Torvalds /* It is OK that we do not hold the group lock right now, 3691da177e4SLinus Torvalds * as we run under the RTNL lock. 3701da177e4SLinus Torvalds */ 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds switch (event) { 3731da177e4SLinus Torvalds case NETDEV_CHANGE: 3741da177e4SLinus Torvalds /* Propagate real device state to vlan devices */ 375*1fd9b1fcSPatrick McHardy vlan_group_for_each_dev(grp, i, vlandev) 376fc4a7489SPatrick Mullaney netif_stacked_transfer_operstate(dev, vlandev); 3771da177e4SLinus Torvalds break; 3781da177e4SLinus Torvalds 3798c979c26SPatrick McHardy case NETDEV_CHANGEADDR: 3808c979c26SPatrick McHardy /* Adjust unicast filters on underlying device */ 381*1fd9b1fcSPatrick McHardy vlan_group_for_each_dev(grp, i, vlandev) { 382d932e04aSPatrick McHardy flgs = vlandev->flags; 383d932e04aSPatrick McHardy if (!(flgs & IFF_UP)) 384d932e04aSPatrick McHardy continue; 385d932e04aSPatrick McHardy 3868c979c26SPatrick McHardy vlan_sync_address(dev, vlandev); 3878c979c26SPatrick McHardy } 3888c979c26SPatrick McHardy break; 3898c979c26SPatrick McHardy 3902e477c9bSHerbert Xu case NETDEV_CHANGEMTU: 391*1fd9b1fcSPatrick McHardy vlan_group_for_each_dev(grp, i, vlandev) { 3922e477c9bSHerbert Xu if (vlandev->mtu <= dev->mtu) 3932e477c9bSHerbert Xu continue; 3942e477c9bSHerbert Xu 3952e477c9bSHerbert Xu dev_set_mtu(vlandev, dev->mtu); 3962e477c9bSHerbert Xu } 3972e477c9bSHerbert Xu break; 3982e477c9bSHerbert Xu 3995fb13570SPatrick McHardy case NETDEV_FEAT_CHANGE: 4005fb13570SPatrick McHardy /* Propagate device features to underlying device */ 401*1fd9b1fcSPatrick McHardy vlan_group_for_each_dev(grp, i, vlandev) 4025fb13570SPatrick McHardy vlan_transfer_features(dev, vlandev); 4035fb13570SPatrick McHardy break; 4045fb13570SPatrick McHardy 4051da177e4SLinus Torvalds case NETDEV_DOWN: 406f646968fSPatrick McHardy if (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER) 40780d5c368SPatrick McHardy vlan_vid_del(dev, htons(ETH_P_8021Q), 0); 408efc73f4bSAmir Hanania 4091da177e4SLinus Torvalds /* Put all VLANs for this dev in the down state too. */ 410*1fd9b1fcSPatrick McHardy vlan_group_for_each_dev(grp, i, vlandev) { 4111da177e4SLinus Torvalds flgs = vlandev->flags; 4121da177e4SLinus Torvalds if (!(flgs & IFF_UP)) 4131da177e4SLinus Torvalds continue; 4141da177e4SLinus Torvalds 4157da82c06SJiri Pirko vlan = vlan_dev_priv(vlandev); 4165e756593SPatrick McHardy if (!(vlan->flags & VLAN_FLAG_LOOSE_BINDING)) 4171da177e4SLinus Torvalds dev_change_flags(vlandev, flgs & ~IFF_UP); 418fc4a7489SPatrick Mullaney netif_stacked_transfer_operstate(dev, vlandev); 4191da177e4SLinus Torvalds } 4201da177e4SLinus Torvalds break; 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds case NETDEV_UP: 4231da177e4SLinus Torvalds /* Put all VLANs for this dev in the up state too. */ 424*1fd9b1fcSPatrick McHardy vlan_group_for_each_dev(grp, i, vlandev) { 4251da177e4SLinus Torvalds flgs = vlandev->flags; 4261da177e4SLinus Torvalds if (flgs & IFF_UP) 4271da177e4SLinus Torvalds continue; 4281da177e4SLinus Torvalds 4297da82c06SJiri Pirko vlan = vlan_dev_priv(vlandev); 4305e756593SPatrick McHardy if (!(vlan->flags & VLAN_FLAG_LOOSE_BINDING)) 4311da177e4SLinus Torvalds dev_change_flags(vlandev, flgs | IFF_UP); 432fc4a7489SPatrick Mullaney netif_stacked_transfer_operstate(dev, vlandev); 4331da177e4SLinus Torvalds } 4341da177e4SLinus Torvalds break; 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds case NETDEV_UNREGISTER: 4373b27e105SDavid Lamparter /* twiddle thumbs on netns device moves */ 4383b27e105SDavid Lamparter if (dev->reg_state != NETREG_UNREGISTERING) 4393b27e105SDavid Lamparter break; 4403b27e105SDavid Lamparter 441*1fd9b1fcSPatrick McHardy vlan_group_for_each_dev(grp, i, vlandev) { 4425b9ea6e0SJiri Pirko /* removal of last vid destroys vlan_info, abort 44329906f6aSPatrick McHardy * afterwards */ 4445b9ea6e0SJiri Pirko if (vlan_info->nr_vids == 1) 445*1fd9b1fcSPatrick McHardy last = true; 44629906f6aSPatrick McHardy 44729906f6aSPatrick McHardy unregister_vlan_dev(vlandev, &list); 448*1fd9b1fcSPatrick McHardy if (last) 449*1fd9b1fcSPatrick McHardy break; 45029906f6aSPatrick McHardy } 45129906f6aSPatrick McHardy unregister_netdevice_many(&list); 4521da177e4SLinus Torvalds break; 4531c01fe14SJiri Pirko 4541c01fe14SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 4551c01fe14SJiri Pirko /* Forbid underlaying device to change its type. */ 45618c22a03SJiri Pirko if (vlan_uses_dev(dev)) 4571c01fe14SJiri Pirko return NOTIFY_BAD; 45818c22a03SJiri Pirko break; 45999606477SBen Hutchings 46099606477SBen Hutchings case NETDEV_NOTIFY_PEERS: 4617c899432SBen Hutchings case NETDEV_BONDING_FAILOVER: 46299606477SBen Hutchings /* Propagate to vlan devices */ 463*1fd9b1fcSPatrick McHardy vlan_group_for_each_dev(grp, i, vlandev) 4647c899432SBen Hutchings call_netdevice_notifiers(event, vlandev); 46599606477SBen Hutchings break; 4663ff50b79SStephen Hemminger } 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds out: 4691da177e4SLinus Torvalds return NOTIFY_DONE; 4701da177e4SLinus Torvalds } 4711da177e4SLinus Torvalds 47269ab4b7dSPatrick McHardy static struct notifier_block vlan_notifier_block __read_mostly = { 47369ab4b7dSPatrick McHardy .notifier_call = vlan_device_event, 47469ab4b7dSPatrick McHardy }; 47569ab4b7dSPatrick McHardy 4761da177e4SLinus Torvalds /* 4771da177e4SLinus Torvalds * VLAN IOCTL handler. 4781da177e4SLinus Torvalds * o execute requested action or pass command to the device driver 4791da177e4SLinus Torvalds * arg is really a struct vlan_ioctl_args __user *. 4801da177e4SLinus Torvalds */ 481881d966bSEric W. Biederman static int vlan_ioctl_handler(struct net *net, void __user *arg) 4821da177e4SLinus Torvalds { 483c17d8874SPatrick McHardy int err; 4841da177e4SLinus Torvalds struct vlan_ioctl_args args; 485c17d8874SPatrick McHardy struct net_device *dev = NULL; 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args))) 4881da177e4SLinus Torvalds return -EFAULT; 4891da177e4SLinus Torvalds 4901da177e4SLinus Torvalds /* Null terminate this sucker, just in case. */ 4911da177e4SLinus Torvalds args.device1[23] = 0; 4921da177e4SLinus Torvalds args.u.device2[23] = 0; 4931da177e4SLinus Torvalds 494c17d8874SPatrick McHardy rtnl_lock(); 495c17d8874SPatrick McHardy 4961da177e4SLinus Torvalds switch (args.cmd) { 4971da177e4SLinus Torvalds case SET_VLAN_INGRESS_PRIORITY_CMD: 498c17d8874SPatrick McHardy case SET_VLAN_EGRESS_PRIORITY_CMD: 499c17d8874SPatrick McHardy case SET_VLAN_FLAG_CMD: 500c17d8874SPatrick McHardy case ADD_VLAN_CMD: 501c17d8874SPatrick McHardy case DEL_VLAN_CMD: 502c17d8874SPatrick McHardy case GET_VLAN_REALDEV_NAME_CMD: 503c17d8874SPatrick McHardy case GET_VLAN_VID_CMD: 504c17d8874SPatrick McHardy err = -ENODEV; 50565d292a2SPavel Emelyanov dev = __dev_get_by_name(net, args.device1); 506c17d8874SPatrick McHardy if (!dev) 507c17d8874SPatrick McHardy goto out; 508c17d8874SPatrick McHardy 509c17d8874SPatrick McHardy err = -EINVAL; 51026a25239SJoonwoo Park if (args.cmd != ADD_VLAN_CMD && !is_vlan_dev(dev)) 511c17d8874SPatrick McHardy goto out; 512c17d8874SPatrick McHardy } 513c17d8874SPatrick McHardy 514c17d8874SPatrick McHardy switch (args.cmd) { 515c17d8874SPatrick McHardy case SET_VLAN_INGRESS_PRIORITY_CMD: 516c17d8874SPatrick McHardy err = -EPERM; 517276996fdSEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 518c17d8874SPatrick McHardy break; 519c17d8874SPatrick McHardy vlan_dev_set_ingress_priority(dev, 5201da177e4SLinus Torvalds args.u.skb_priority, 5211da177e4SLinus Torvalds args.vlan_qos); 522fffe470aSPatrick McHardy err = 0; 5231da177e4SLinus Torvalds break; 5241da177e4SLinus Torvalds 5251da177e4SLinus Torvalds case SET_VLAN_EGRESS_PRIORITY_CMD: 526c17d8874SPatrick McHardy err = -EPERM; 527276996fdSEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 528c17d8874SPatrick McHardy break; 529c17d8874SPatrick McHardy err = vlan_dev_set_egress_priority(dev, 5301da177e4SLinus Torvalds args.u.skb_priority, 5311da177e4SLinus Torvalds args.vlan_qos); 5321da177e4SLinus Torvalds break; 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds case SET_VLAN_FLAG_CMD: 535c17d8874SPatrick McHardy err = -EPERM; 536276996fdSEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 537c17d8874SPatrick McHardy break; 538b3ce0325SPatrick McHardy err = vlan_dev_change_flags(dev, 539b3ce0325SPatrick McHardy args.vlan_qos ? args.u.flag : 0, 540b3ce0325SPatrick McHardy args.u.flag); 5411da177e4SLinus Torvalds break; 5421da177e4SLinus Torvalds 5431da177e4SLinus Torvalds case SET_VLAN_NAME_TYPE_CMD: 544c17d8874SPatrick McHardy err = -EPERM; 545276996fdSEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 546e35de026SPavel Emelyanov break; 547c17d8874SPatrick McHardy if ((args.u.name_type >= 0) && 548c17d8874SPatrick McHardy (args.u.name_type < VLAN_NAME_TYPE_HIGHEST)) { 5497a17a2f7SPavel Emelyanov struct vlan_net *vn; 5507a17a2f7SPavel Emelyanov 5517a17a2f7SPavel Emelyanov vn = net_generic(net, vlan_net_id); 5527a17a2f7SPavel Emelyanov vn->name_type = args.u.name_type; 5531da177e4SLinus Torvalds err = 0; 5541da177e4SLinus Torvalds } else { 5551da177e4SLinus Torvalds err = -EINVAL; 5561da177e4SLinus Torvalds } 5571da177e4SLinus Torvalds break; 5581da177e4SLinus Torvalds 5591da177e4SLinus Torvalds case ADD_VLAN_CMD: 560c17d8874SPatrick McHardy err = -EPERM; 561276996fdSEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 562c17d8874SPatrick McHardy break; 5632ae0bf69SPatrick McHardy err = register_vlan_device(dev, args.u.VID); 5641da177e4SLinus Torvalds break; 5651da177e4SLinus Torvalds 5661da177e4SLinus Torvalds case DEL_VLAN_CMD: 567c17d8874SPatrick McHardy err = -EPERM; 568276996fdSEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 569c17d8874SPatrick McHardy break; 57023289a37SEric Dumazet unregister_vlan_dev(dev, NULL); 571af301517SPatrick McHardy err = 0; 5721da177e4SLinus Torvalds break; 5731da177e4SLinus Torvalds 5741da177e4SLinus Torvalds case GET_VLAN_REALDEV_NAME_CMD: 5753f5f4346SAndrew Morton err = 0; 576c17d8874SPatrick McHardy vlan_dev_get_realdev_name(dev, args.u.device2); 5771da177e4SLinus Torvalds if (copy_to_user(arg, &args, 5782029cc2cSPatrick McHardy sizeof(struct vlan_ioctl_args))) 5791da177e4SLinus Torvalds err = -EFAULT; 5801da177e4SLinus Torvalds break; 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds case GET_VLAN_VID_CMD: 5833f5f4346SAndrew Morton err = 0; 58422d1ba74SPatrick McHardy args.u.VID = vlan_dev_vlan_id(dev); 5851da177e4SLinus Torvalds if (copy_to_user(arg, &args, 5862029cc2cSPatrick McHardy sizeof(struct vlan_ioctl_args))) 5871da177e4SLinus Torvalds err = -EFAULT; 5881da177e4SLinus Torvalds break; 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds default: 591198a291cSPatrick McHardy err = -EOPNOTSUPP; 592c17d8874SPatrick McHardy break; 5933ff50b79SStephen Hemminger } 5947eb1b3d3SMika Kukkonen out: 595c17d8874SPatrick McHardy rtnl_unlock(); 5961da177e4SLinus Torvalds return err; 5971da177e4SLinus Torvalds } 5981da177e4SLinus Torvalds 5992c8c1e72SAlexey Dobriyan static int __net_init vlan_init_net(struct net *net) 600d9ed0f0eSPavel Emelyanov { 601946d1a92SEric W. Biederman struct vlan_net *vn = net_generic(net, vlan_net_id); 602d9ed0f0eSPavel Emelyanov int err; 603d9ed0f0eSPavel Emelyanov 6047a17a2f7SPavel Emelyanov vn->name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD; 6057a17a2f7SPavel Emelyanov 606cd1c7014SPavel Emelyanov err = vlan_proc_init(net); 607cd1c7014SPavel Emelyanov 608d9ed0f0eSPavel Emelyanov return err; 609d9ed0f0eSPavel Emelyanov } 610d9ed0f0eSPavel Emelyanov 6112c8c1e72SAlexey Dobriyan static void __net_exit vlan_exit_net(struct net *net) 612d9ed0f0eSPavel Emelyanov { 613cd1c7014SPavel Emelyanov vlan_proc_cleanup(net); 614d9ed0f0eSPavel Emelyanov } 615d9ed0f0eSPavel Emelyanov 616d9ed0f0eSPavel Emelyanov static struct pernet_operations vlan_net_ops = { 617d9ed0f0eSPavel Emelyanov .init = vlan_init_net, 618d9ed0f0eSPavel Emelyanov .exit = vlan_exit_net, 619946d1a92SEric W. Biederman .id = &vlan_net_id, 620946d1a92SEric W. Biederman .size = sizeof(struct vlan_net), 621d9ed0f0eSPavel Emelyanov }; 622d9ed0f0eSPavel Emelyanov 62369ab4b7dSPatrick McHardy static int __init vlan_proto_init(void) 62469ab4b7dSPatrick McHardy { 62569ab4b7dSPatrick McHardy int err; 62669ab4b7dSPatrick McHardy 627da7c06c4SJustin Mattock pr_info("%s v%s\n", vlan_fullname, vlan_version); 62869ab4b7dSPatrick McHardy 62991e2ff35SEric W. Biederman err = register_pernet_subsys(&vlan_net_ops); 630d9ed0f0eSPavel Emelyanov if (err < 0) 631d9ed0f0eSPavel Emelyanov goto err0; 632d9ed0f0eSPavel Emelyanov 63369ab4b7dSPatrick McHardy err = register_netdevice_notifier(&vlan_notifier_block); 63469ab4b7dSPatrick McHardy if (err < 0) 63569ab4b7dSPatrick McHardy goto err2; 63669ab4b7dSPatrick McHardy 63770c03b49SPatrick McHardy err = vlan_gvrp_init(); 63869ab4b7dSPatrick McHardy if (err < 0) 63969ab4b7dSPatrick McHardy goto err3; 64069ab4b7dSPatrick McHardy 64186fbe9bbSDavid Ward err = vlan_mvrp_init(); 64270c03b49SPatrick McHardy if (err < 0) 64370c03b49SPatrick McHardy goto err4; 64470c03b49SPatrick McHardy 64586fbe9bbSDavid Ward err = vlan_netlink_init(); 64686fbe9bbSDavid Ward if (err < 0) 64786fbe9bbSDavid Ward goto err5; 64886fbe9bbSDavid Ward 64969ab4b7dSPatrick McHardy vlan_ioctl_set(vlan_ioctl_handler); 65069ab4b7dSPatrick McHardy return 0; 65169ab4b7dSPatrick McHardy 65286fbe9bbSDavid Ward err5: 65386fbe9bbSDavid Ward vlan_mvrp_uninit(); 65470c03b49SPatrick McHardy err4: 65570c03b49SPatrick McHardy vlan_gvrp_uninit(); 65669ab4b7dSPatrick McHardy err3: 65769ab4b7dSPatrick McHardy unregister_netdevice_notifier(&vlan_notifier_block); 65869ab4b7dSPatrick McHardy err2: 65991e2ff35SEric W. Biederman unregister_pernet_subsys(&vlan_net_ops); 660d9ed0f0eSPavel Emelyanov err0: 66169ab4b7dSPatrick McHardy return err; 66269ab4b7dSPatrick McHardy } 66369ab4b7dSPatrick McHardy 66469ab4b7dSPatrick McHardy static void __exit vlan_cleanup_module(void) 66569ab4b7dSPatrick McHardy { 66669ab4b7dSPatrick McHardy vlan_ioctl_set(NULL); 66769ab4b7dSPatrick McHardy vlan_netlink_fini(); 66869ab4b7dSPatrick McHardy 66969ab4b7dSPatrick McHardy unregister_netdevice_notifier(&vlan_notifier_block); 67069ab4b7dSPatrick McHardy 67191e2ff35SEric W. Biederman unregister_pernet_subsys(&vlan_net_ops); 6726e327c11SJesper Dangaard Brouer rcu_barrier(); /* Wait for completion of call_rcu()'s */ 67370c03b49SPatrick McHardy 67486fbe9bbSDavid Ward vlan_mvrp_uninit(); 67570c03b49SPatrick McHardy vlan_gvrp_uninit(); 67669ab4b7dSPatrick McHardy } 67769ab4b7dSPatrick McHardy 67869ab4b7dSPatrick McHardy module_init(vlan_proto_init); 67969ab4b7dSPatrick McHardy module_exit(vlan_cleanup_module); 68069ab4b7dSPatrick McHardy 6811da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 6821da177e4SLinus Torvalds MODULE_VERSION(DRV_VERSION); 683