xref: /openbmc/linux/net/8021q/vlan.c (revision fc0d48b8fb449ca007b2057328abf736cb516168)
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 
172e89fe42cSPatrick McHardy 	err = register_netdevice(dev);
173e89fe42cSPatrick McHardy 	if (err < 0)
1745df27e6cSVeaceslav Falico 		goto out_uninit_mvrp;
1755df27e6cSVeaceslav Falico 
1765df27e6cSVeaceslav Falico 	err = netdev_upper_dev_link(real_dev, dev);
1775df27e6cSVeaceslav Falico 	if (err)
1785df27e6cSVeaceslav Falico 		goto out_unregister_netdev;
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 	 */
1891fd9b1fcSPatrick 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 
1945df27e6cSVeaceslav Falico out_unregister_netdev:
1955df27e6cSVeaceslav Falico 	unregister_netdevice(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:
2031fd9b1fcSPatrick 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;
2130c0667a8SWang Sheng-Hui 	struct vlan_dev_priv *vlan;
2147a17a2f7SPavel Emelyanov 	struct net *net = dev_net(real_dev);
2157a17a2f7SPavel Emelyanov 	struct vlan_net *vn = net_generic(net, vlan_net_id);
2161da177e4SLinus Torvalds 	char name[IFNAMSIZ];
2172ae0bf69SPatrick McHardy 	int err;
2181da177e4SLinus Torvalds 
2199bb8582eSPatrick McHardy 	if (vlan_id >= VLAN_VID_MASK)
2202ae0bf69SPatrick McHardy 		return -ERANGE;
2211da177e4SLinus Torvalds 
2221fd9b1fcSPatrick McHardy 	err = vlan_check_real_dev(real_dev, htons(ETH_P_8021Q), vlan_id);
2232ae0bf69SPatrick McHardy 	if (err < 0)
2242ae0bf69SPatrick McHardy 		return err;
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	/* Gotta set up the fields for the device. */
2277a17a2f7SPavel Emelyanov 	switch (vn->name_type) {
2281da177e4SLinus Torvalds 	case VLAN_NAME_TYPE_RAW_PLUS_VID:
2291da177e4SLinus Torvalds 		/* name will look like:	 eth1.0005 */
2309bb8582eSPatrick McHardy 		snprintf(name, IFNAMSIZ, "%s.%.4i", real_dev->name, vlan_id);
2311da177e4SLinus Torvalds 		break;
2321da177e4SLinus Torvalds 	case VLAN_NAME_TYPE_PLUS_VID_NO_PAD:
2331da177e4SLinus Torvalds 		/* Put our vlan.VID in the name.
2341da177e4SLinus Torvalds 		 * Name will look like:	 vlan5
2351da177e4SLinus Torvalds 		 */
2369bb8582eSPatrick McHardy 		snprintf(name, IFNAMSIZ, "vlan%i", vlan_id);
2371da177e4SLinus Torvalds 		break;
2381da177e4SLinus Torvalds 	case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD:
2391da177e4SLinus Torvalds 		/* Put our vlan.VID in the name.
2401da177e4SLinus Torvalds 		 * Name will look like:	 eth0.5
2411da177e4SLinus Torvalds 		 */
2429bb8582eSPatrick McHardy 		snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, vlan_id);
2431da177e4SLinus Torvalds 		break;
2441da177e4SLinus Torvalds 	case VLAN_NAME_TYPE_PLUS_VID:
2451da177e4SLinus Torvalds 		/* Put our vlan.VID in the name.
2461da177e4SLinus Torvalds 		 * Name will look like:	 vlan0005
2471da177e4SLinus Torvalds 		 */
2481da177e4SLinus Torvalds 	default:
2499bb8582eSPatrick McHardy 		snprintf(name, IFNAMSIZ, "vlan%.4i", vlan_id);
2503ff50b79SStephen Hemminger 	}
2511da177e4SLinus Torvalds 
2527da82c06SJiri Pirko 	new_dev = alloc_netdev(sizeof(struct vlan_dev_priv), name, vlan_setup);
2535dd8d1e9SArjan van de Ven 
2541da177e4SLinus Torvalds 	if (new_dev == NULL)
2552ae0bf69SPatrick McHardy 		return -ENOBUFS;
2561da177e4SLinus Torvalds 
25765d292a2SPavel Emelyanov 	dev_net_set(new_dev, net);
2581da177e4SLinus Torvalds 	/* need 4 bytes for extra VLAN header info,
2591da177e4SLinus Torvalds 	 * hope the underlying device can handle it.
2601da177e4SLinus Torvalds 	 */
2611da177e4SLinus Torvalds 	new_dev->mtu = real_dev->mtu;
2626e22ce2cSYi Zou 	new_dev->priv_flags |= (real_dev->priv_flags & IFF_UNICAST_FLT);
2631da177e4SLinus Torvalds 
2640c0667a8SWang Sheng-Hui 	vlan = vlan_dev_priv(new_dev);
2650c0667a8SWang Sheng-Hui 	vlan->vlan_proto = htons(ETH_P_8021Q);
2660c0667a8SWang Sheng-Hui 	vlan->vlan_id = vlan_id;
2670c0667a8SWang Sheng-Hui 	vlan->real_dev = real_dev;
2680c0667a8SWang Sheng-Hui 	vlan->dent = NULL;
2690c0667a8SWang Sheng-Hui 	vlan->flags = VLAN_FLAG_REORDER_HDR;
2701da177e4SLinus Torvalds 
27107b5b17eSPatrick McHardy 	new_dev->rtnl_link_ops = &vlan_link_ops;
2722ae0bf69SPatrick McHardy 	err = register_vlan_dev(new_dev);
2732ae0bf69SPatrick McHardy 	if (err < 0)
27442429aaeSPatrick McHardy 		goto out_free_newdev;
2751da177e4SLinus Torvalds 
2762ae0bf69SPatrick McHardy 	return 0;
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds out_free_newdev:
2791da177e4SLinus Torvalds 	free_netdev(new_dev);
2802ae0bf69SPatrick McHardy 	return err;
2811da177e4SLinus Torvalds }
2821da177e4SLinus Torvalds 
2838c979c26SPatrick McHardy static void vlan_sync_address(struct net_device *dev,
2848c979c26SPatrick McHardy 			      struct net_device *vlandev)
2858c979c26SPatrick McHardy {
2867da82c06SJiri Pirko 	struct vlan_dev_priv *vlan = vlan_dev_priv(vlandev);
2878c979c26SPatrick McHardy 
2888c979c26SPatrick McHardy 	/* May be called without an actual change */
28953a2b3a1SJoe Perches 	if (ether_addr_equal(vlan->real_dev_addr, dev->dev_addr))
2908c979c26SPatrick McHardy 		return;
2918c979c26SPatrick McHardy 
2928c979c26SPatrick McHardy 	/* vlan address was different from the old address and is equal to
2938c979c26SPatrick McHardy 	 * the new address */
29453a2b3a1SJoe Perches 	if (!ether_addr_equal(vlandev->dev_addr, vlan->real_dev_addr) &&
29553a2b3a1SJoe Perches 	    ether_addr_equal(vlandev->dev_addr, dev->dev_addr))
296a748ee24SJiri Pirko 		dev_uc_del(dev, vlandev->dev_addr);
2978c979c26SPatrick McHardy 
2988c979c26SPatrick McHardy 	/* vlan address was equal to the old address and is different from
2998c979c26SPatrick McHardy 	 * the new address */
30053a2b3a1SJoe Perches 	if (ether_addr_equal(vlandev->dev_addr, vlan->real_dev_addr) &&
30153a2b3a1SJoe Perches 	    !ether_addr_equal(vlandev->dev_addr, dev->dev_addr))
302a748ee24SJiri Pirko 		dev_uc_add(dev, vlandev->dev_addr);
3038c979c26SPatrick McHardy 
30407fc67beSJoe Perches 	ether_addr_copy(vlan->real_dev_addr, dev->dev_addr);
3058c979c26SPatrick McHardy }
3068c979c26SPatrick McHardy 
3075fb13570SPatrick McHardy static void vlan_transfer_features(struct net_device *dev,
3085fb13570SPatrick McHardy 				   struct net_device *vlandev)
3095fb13570SPatrick McHardy {
310*fc0d48b8SVlad Yasevich 	struct vlan_dev_priv *vlan = vlan_dev_priv(vlandev);
311*fc0d48b8SVlad Yasevich 
3121ae4be22SAlexander Duyck 	vlandev->gso_max_size = dev->gso_max_size;
313029f5fc3SJohn Fastabend 
314*fc0d48b8SVlad Yasevich 	if (vlan_hw_offload_capable(dev->features, vlan->vlan_proto))
315029f5fc3SJohn Fastabend 		vlandev->hard_header_len = dev->hard_header_len;
316029f5fc3SJohn Fastabend 	else
317029f5fc3SJohn Fastabend 		vlandev->hard_header_len = dev->hard_header_len + VLAN_HLEN;
318029f5fc3SJohn Fastabend 
319f4d5392eSAmerigo Wang #if IS_ENABLED(CONFIG_FCOE)
320b85daa53SVasu Dev 	vlandev->fcoe_ddp_xid = dev->fcoe_ddp_xid;
321b85daa53SVasu Dev #endif
3228a0427bbSMichał Mirosław 
3238a0427bbSMichał Mirosław 	netdev_update_features(vlandev);
3245fb13570SPatrick McHardy }
3255fb13570SPatrick McHardy 
326802fb176SPavel Emelyanov static void __vlan_device_event(struct net_device *dev, unsigned long event)
327802fb176SPavel Emelyanov {
328802fb176SPavel Emelyanov 	switch (event) {
329802fb176SPavel Emelyanov 	case NETDEV_CHANGENAME:
330802fb176SPavel Emelyanov 		vlan_proc_rem_dev(dev);
331802fb176SPavel Emelyanov 		if (vlan_proc_add_dev(dev) < 0)
332afab2d29SJoe Perches 			pr_warn("failed to change proc name for %s\n",
333802fb176SPavel Emelyanov 				dev->name);
334802fb176SPavel Emelyanov 		break;
33530688a9aSPavel Emelyanov 	case NETDEV_REGISTER:
33630688a9aSPavel Emelyanov 		if (vlan_proc_add_dev(dev) < 0)
337afab2d29SJoe Perches 			pr_warn("failed to add proc entry for %s\n", dev->name);
33830688a9aSPavel Emelyanov 		break;
33930688a9aSPavel Emelyanov 	case NETDEV_UNREGISTER:
34030688a9aSPavel Emelyanov 		vlan_proc_rem_dev(dev);
34130688a9aSPavel Emelyanov 		break;
342802fb176SPavel Emelyanov 	}
343802fb176SPavel Emelyanov }
344802fb176SPavel Emelyanov 
3452029cc2cSPatrick McHardy static int vlan_device_event(struct notifier_block *unused, unsigned long event,
3462029cc2cSPatrick McHardy 			     void *ptr)
3471da177e4SLinus Torvalds {
348351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
349802fb176SPavel Emelyanov 	struct vlan_group *grp;
3505b9ea6e0SJiri Pirko 	struct vlan_info *vlan_info;
3511da177e4SLinus Torvalds 	int i, flgs;
3521da177e4SLinus Torvalds 	struct net_device *vlandev;
3537da82c06SJiri Pirko 	struct vlan_dev_priv *vlan;
3541fd9b1fcSPatrick McHardy 	bool last = false;
35529906f6aSPatrick McHardy 	LIST_HEAD(list);
3561da177e4SLinus Torvalds 
35781d85346SPatrick McHardy 	if (is_vlan_dev(dev))
358802fb176SPavel Emelyanov 		__vlan_device_event(dev, event);
359802fb176SPavel Emelyanov 
360ad1afb00SPedro Garcia 	if ((event == NETDEV_UP) &&
361f646968fSPatrick McHardy 	    (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER)) {
362afab2d29SJoe Perches 		pr_info("adding VLAN 0 to HW filter on device %s\n",
363ad1afb00SPedro Garcia 			dev->name);
36480d5c368SPatrick McHardy 		vlan_vid_add(dev, htons(ETH_P_8021Q), 0);
365ad1afb00SPedro Garcia 	}
366ad1afb00SPedro Garcia 
3675b9ea6e0SJiri Pirko 	vlan_info = rtnl_dereference(dev->vlan_info);
3685b9ea6e0SJiri Pirko 	if (!vlan_info)
3691da177e4SLinus Torvalds 		goto out;
3705b9ea6e0SJiri Pirko 	grp = &vlan_info->grp;
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	/* It is OK that we do not hold the group lock right now,
3731da177e4SLinus Torvalds 	 * as we run under the RTNL lock.
3741da177e4SLinus Torvalds 	 */
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	switch (event) {
3771da177e4SLinus Torvalds 	case NETDEV_CHANGE:
3781da177e4SLinus Torvalds 		/* Propagate real device state to vlan devices */
3791fd9b1fcSPatrick McHardy 		vlan_group_for_each_dev(grp, i, vlandev)
380fc4a7489SPatrick Mullaney 			netif_stacked_transfer_operstate(dev, vlandev);
3811da177e4SLinus Torvalds 		break;
3821da177e4SLinus Torvalds 
3838c979c26SPatrick McHardy 	case NETDEV_CHANGEADDR:
3848c979c26SPatrick McHardy 		/* Adjust unicast filters on underlying device */
3851fd9b1fcSPatrick McHardy 		vlan_group_for_each_dev(grp, i, vlandev) {
386d932e04aSPatrick McHardy 			flgs = vlandev->flags;
387d932e04aSPatrick McHardy 			if (!(flgs & IFF_UP))
388d932e04aSPatrick McHardy 				continue;
389d932e04aSPatrick McHardy 
3908c979c26SPatrick McHardy 			vlan_sync_address(dev, vlandev);
3918c979c26SPatrick McHardy 		}
3928c979c26SPatrick McHardy 		break;
3938c979c26SPatrick McHardy 
3942e477c9bSHerbert Xu 	case NETDEV_CHANGEMTU:
3951fd9b1fcSPatrick McHardy 		vlan_group_for_each_dev(grp, i, vlandev) {
3962e477c9bSHerbert Xu 			if (vlandev->mtu <= dev->mtu)
3972e477c9bSHerbert Xu 				continue;
3982e477c9bSHerbert Xu 
3992e477c9bSHerbert Xu 			dev_set_mtu(vlandev, dev->mtu);
4002e477c9bSHerbert Xu 		}
4012e477c9bSHerbert Xu 		break;
4022e477c9bSHerbert Xu 
4035fb13570SPatrick McHardy 	case NETDEV_FEAT_CHANGE:
4045fb13570SPatrick McHardy 		/* Propagate device features to underlying device */
4051fd9b1fcSPatrick McHardy 		vlan_group_for_each_dev(grp, i, vlandev)
4065fb13570SPatrick McHardy 			vlan_transfer_features(dev, vlandev);
4075fb13570SPatrick McHardy 		break;
4085fb13570SPatrick McHardy 
4091da177e4SLinus Torvalds 	case NETDEV_DOWN:
410f646968fSPatrick McHardy 		if (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
41180d5c368SPatrick McHardy 			vlan_vid_del(dev, htons(ETH_P_8021Q), 0);
412efc73f4bSAmir Hanania 
4131da177e4SLinus Torvalds 		/* Put all VLANs for this dev in the down state too.  */
4141fd9b1fcSPatrick McHardy 		vlan_group_for_each_dev(grp, i, vlandev) {
4151da177e4SLinus Torvalds 			flgs = vlandev->flags;
4161da177e4SLinus Torvalds 			if (!(flgs & IFF_UP))
4171da177e4SLinus Torvalds 				continue;
4181da177e4SLinus Torvalds 
4197da82c06SJiri Pirko 			vlan = vlan_dev_priv(vlandev);
4205e756593SPatrick McHardy 			if (!(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
4211da177e4SLinus Torvalds 				dev_change_flags(vlandev, flgs & ~IFF_UP);
422fc4a7489SPatrick Mullaney 			netif_stacked_transfer_operstate(dev, vlandev);
4231da177e4SLinus Torvalds 		}
4241da177e4SLinus Torvalds 		break;
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	case NETDEV_UP:
4271da177e4SLinus Torvalds 		/* Put all VLANs for this dev in the up state too.  */
4281fd9b1fcSPatrick McHardy 		vlan_group_for_each_dev(grp, i, vlandev) {
4291da177e4SLinus Torvalds 			flgs = vlandev->flags;
4301da177e4SLinus Torvalds 			if (flgs & IFF_UP)
4311da177e4SLinus Torvalds 				continue;
4321da177e4SLinus Torvalds 
4337da82c06SJiri Pirko 			vlan = vlan_dev_priv(vlandev);
4345e756593SPatrick McHardy 			if (!(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
4351da177e4SLinus Torvalds 				dev_change_flags(vlandev, flgs | IFF_UP);
436fc4a7489SPatrick Mullaney 			netif_stacked_transfer_operstate(dev, vlandev);
4371da177e4SLinus Torvalds 		}
4381da177e4SLinus Torvalds 		break;
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
4413b27e105SDavid Lamparter 		/* twiddle thumbs on netns device moves */
4423b27e105SDavid Lamparter 		if (dev->reg_state != NETREG_UNREGISTERING)
4433b27e105SDavid Lamparter 			break;
4443b27e105SDavid Lamparter 
4451fd9b1fcSPatrick McHardy 		vlan_group_for_each_dev(grp, i, vlandev) {
4465b9ea6e0SJiri Pirko 			/* removal of last vid destroys vlan_info, abort
44729906f6aSPatrick McHardy 			 * afterwards */
4485b9ea6e0SJiri Pirko 			if (vlan_info->nr_vids == 1)
4491fd9b1fcSPatrick McHardy 				last = true;
45029906f6aSPatrick McHardy 
45129906f6aSPatrick McHardy 			unregister_vlan_dev(vlandev, &list);
4521fd9b1fcSPatrick McHardy 			if (last)
4531fd9b1fcSPatrick McHardy 				break;
45429906f6aSPatrick McHardy 		}
45529906f6aSPatrick McHardy 		unregister_netdevice_many(&list);
4561da177e4SLinus Torvalds 		break;
4571c01fe14SJiri Pirko 
4581c01fe14SJiri Pirko 	case NETDEV_PRE_TYPE_CHANGE:
4591c01fe14SJiri Pirko 		/* Forbid underlaying device to change its type. */
46018c22a03SJiri Pirko 		if (vlan_uses_dev(dev))
4611c01fe14SJiri Pirko 			return NOTIFY_BAD;
46218c22a03SJiri Pirko 		break;
46399606477SBen Hutchings 
46499606477SBen Hutchings 	case NETDEV_NOTIFY_PEERS:
4657c899432SBen Hutchings 	case NETDEV_BONDING_FAILOVER:
4664aa5dee4SJiri Pirko 	case NETDEV_RESEND_IGMP:
46799606477SBen Hutchings 		/* Propagate to vlan devices */
4681fd9b1fcSPatrick McHardy 		vlan_group_for_each_dev(grp, i, vlandev)
4697c899432SBen Hutchings 			call_netdevice_notifiers(event, vlandev);
47099606477SBen Hutchings 		break;
4713ff50b79SStephen Hemminger 	}
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds out:
4741da177e4SLinus Torvalds 	return NOTIFY_DONE;
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds 
47769ab4b7dSPatrick McHardy static struct notifier_block vlan_notifier_block __read_mostly = {
47869ab4b7dSPatrick McHardy 	.notifier_call = vlan_device_event,
47969ab4b7dSPatrick McHardy };
48069ab4b7dSPatrick McHardy 
4811da177e4SLinus Torvalds /*
4821da177e4SLinus Torvalds  *	VLAN IOCTL handler.
4831da177e4SLinus Torvalds  *	o execute requested action or pass command to the device driver
4841da177e4SLinus Torvalds  *   arg is really a struct vlan_ioctl_args __user *.
4851da177e4SLinus Torvalds  */
486881d966bSEric W. Biederman static int vlan_ioctl_handler(struct net *net, void __user *arg)
4871da177e4SLinus Torvalds {
488c17d8874SPatrick McHardy 	int err;
4891da177e4SLinus Torvalds 	struct vlan_ioctl_args args;
490c17d8874SPatrick McHardy 	struct net_device *dev = NULL;
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args)))
4931da177e4SLinus Torvalds 		return -EFAULT;
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 	/* Null terminate this sucker, just in case. */
4961da177e4SLinus Torvalds 	args.device1[23] = 0;
4971da177e4SLinus Torvalds 	args.u.device2[23] = 0;
4981da177e4SLinus Torvalds 
499c17d8874SPatrick McHardy 	rtnl_lock();
500c17d8874SPatrick McHardy 
5011da177e4SLinus Torvalds 	switch (args.cmd) {
5021da177e4SLinus Torvalds 	case SET_VLAN_INGRESS_PRIORITY_CMD:
503c17d8874SPatrick McHardy 	case SET_VLAN_EGRESS_PRIORITY_CMD:
504c17d8874SPatrick McHardy 	case SET_VLAN_FLAG_CMD:
505c17d8874SPatrick McHardy 	case ADD_VLAN_CMD:
506c17d8874SPatrick McHardy 	case DEL_VLAN_CMD:
507c17d8874SPatrick McHardy 	case GET_VLAN_REALDEV_NAME_CMD:
508c17d8874SPatrick McHardy 	case GET_VLAN_VID_CMD:
509c17d8874SPatrick McHardy 		err = -ENODEV;
51065d292a2SPavel Emelyanov 		dev = __dev_get_by_name(net, args.device1);
511c17d8874SPatrick McHardy 		if (!dev)
512c17d8874SPatrick McHardy 			goto out;
513c17d8874SPatrick McHardy 
514c17d8874SPatrick McHardy 		err = -EINVAL;
51526a25239SJoonwoo Park 		if (args.cmd != ADD_VLAN_CMD && !is_vlan_dev(dev))
516c17d8874SPatrick McHardy 			goto out;
517c17d8874SPatrick McHardy 	}
518c17d8874SPatrick McHardy 
519c17d8874SPatrick McHardy 	switch (args.cmd) {
520c17d8874SPatrick McHardy 	case SET_VLAN_INGRESS_PRIORITY_CMD:
521c17d8874SPatrick McHardy 		err = -EPERM;
522276996fdSEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
523c17d8874SPatrick McHardy 			break;
524c17d8874SPatrick McHardy 		vlan_dev_set_ingress_priority(dev,
5251da177e4SLinus Torvalds 					      args.u.skb_priority,
5261da177e4SLinus Torvalds 					      args.vlan_qos);
527fffe470aSPatrick McHardy 		err = 0;
5281da177e4SLinus Torvalds 		break;
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds 	case SET_VLAN_EGRESS_PRIORITY_CMD:
531c17d8874SPatrick McHardy 		err = -EPERM;
532276996fdSEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
533c17d8874SPatrick McHardy 			break;
534c17d8874SPatrick McHardy 		err = vlan_dev_set_egress_priority(dev,
5351da177e4SLinus Torvalds 						   args.u.skb_priority,
5361da177e4SLinus Torvalds 						   args.vlan_qos);
5371da177e4SLinus Torvalds 		break;
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 	case SET_VLAN_FLAG_CMD:
540c17d8874SPatrick McHardy 		err = -EPERM;
541276996fdSEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
542c17d8874SPatrick McHardy 			break;
543b3ce0325SPatrick McHardy 		err = vlan_dev_change_flags(dev,
544b3ce0325SPatrick McHardy 					    args.vlan_qos ? args.u.flag : 0,
545b3ce0325SPatrick McHardy 					    args.u.flag);
5461da177e4SLinus Torvalds 		break;
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 	case SET_VLAN_NAME_TYPE_CMD:
549c17d8874SPatrick McHardy 		err = -EPERM;
550276996fdSEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
551e35de026SPavel Emelyanov 			break;
552c17d8874SPatrick McHardy 		if ((args.u.name_type >= 0) &&
553c17d8874SPatrick McHardy 		    (args.u.name_type < VLAN_NAME_TYPE_HIGHEST)) {
5547a17a2f7SPavel Emelyanov 			struct vlan_net *vn;
5557a17a2f7SPavel Emelyanov 
5567a17a2f7SPavel Emelyanov 			vn = net_generic(net, vlan_net_id);
5577a17a2f7SPavel Emelyanov 			vn->name_type = args.u.name_type;
5581da177e4SLinus Torvalds 			err = 0;
5591da177e4SLinus Torvalds 		} else {
5601da177e4SLinus Torvalds 			err = -EINVAL;
5611da177e4SLinus Torvalds 		}
5621da177e4SLinus Torvalds 		break;
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 	case ADD_VLAN_CMD:
565c17d8874SPatrick McHardy 		err = -EPERM;
566276996fdSEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
567c17d8874SPatrick McHardy 			break;
5682ae0bf69SPatrick McHardy 		err = register_vlan_device(dev, args.u.VID);
5691da177e4SLinus Torvalds 		break;
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	case DEL_VLAN_CMD:
572c17d8874SPatrick McHardy 		err = -EPERM;
573276996fdSEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
574c17d8874SPatrick McHardy 			break;
57523289a37SEric Dumazet 		unregister_vlan_dev(dev, NULL);
576af301517SPatrick McHardy 		err = 0;
5771da177e4SLinus Torvalds 		break;
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	case GET_VLAN_REALDEV_NAME_CMD:
5803f5f4346SAndrew Morton 		err = 0;
581c17d8874SPatrick McHardy 		vlan_dev_get_realdev_name(dev, args.u.device2);
5821da177e4SLinus Torvalds 		if (copy_to_user(arg, &args,
5832029cc2cSPatrick McHardy 				 sizeof(struct vlan_ioctl_args)))
5841da177e4SLinus Torvalds 			err = -EFAULT;
5851da177e4SLinus Torvalds 		break;
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 	case GET_VLAN_VID_CMD:
5883f5f4346SAndrew Morton 		err = 0;
58922d1ba74SPatrick McHardy 		args.u.VID = vlan_dev_vlan_id(dev);
5901da177e4SLinus Torvalds 		if (copy_to_user(arg, &args,
5912029cc2cSPatrick McHardy 				 sizeof(struct vlan_ioctl_args)))
5921da177e4SLinus Torvalds 		      err = -EFAULT;
5931da177e4SLinus Torvalds 		break;
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 	default:
596198a291cSPatrick McHardy 		err = -EOPNOTSUPP;
597c17d8874SPatrick McHardy 		break;
5983ff50b79SStephen Hemminger 	}
5997eb1b3d3SMika Kukkonen out:
600c17d8874SPatrick McHardy 	rtnl_unlock();
6011da177e4SLinus Torvalds 	return err;
6021da177e4SLinus Torvalds }
6031da177e4SLinus Torvalds 
6042c8c1e72SAlexey Dobriyan static int __net_init vlan_init_net(struct net *net)
605d9ed0f0eSPavel Emelyanov {
606946d1a92SEric W. Biederman 	struct vlan_net *vn = net_generic(net, vlan_net_id);
607d9ed0f0eSPavel Emelyanov 	int err;
608d9ed0f0eSPavel Emelyanov 
6097a17a2f7SPavel Emelyanov 	vn->name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;
6107a17a2f7SPavel Emelyanov 
611cd1c7014SPavel Emelyanov 	err = vlan_proc_init(net);
612cd1c7014SPavel Emelyanov 
613d9ed0f0eSPavel Emelyanov 	return err;
614d9ed0f0eSPavel Emelyanov }
615d9ed0f0eSPavel Emelyanov 
6162c8c1e72SAlexey Dobriyan static void __net_exit vlan_exit_net(struct net *net)
617d9ed0f0eSPavel Emelyanov {
618cd1c7014SPavel Emelyanov 	vlan_proc_cleanup(net);
619d9ed0f0eSPavel Emelyanov }
620d9ed0f0eSPavel Emelyanov 
621d9ed0f0eSPavel Emelyanov static struct pernet_operations vlan_net_ops = {
622d9ed0f0eSPavel Emelyanov 	.init = vlan_init_net,
623d9ed0f0eSPavel Emelyanov 	.exit = vlan_exit_net,
624946d1a92SEric W. Biederman 	.id   = &vlan_net_id,
625946d1a92SEric W. Biederman 	.size = sizeof(struct vlan_net),
626d9ed0f0eSPavel Emelyanov };
627d9ed0f0eSPavel Emelyanov 
62869ab4b7dSPatrick McHardy static int __init vlan_proto_init(void)
62969ab4b7dSPatrick McHardy {
63069ab4b7dSPatrick McHardy 	int err;
63169ab4b7dSPatrick McHardy 
632da7c06c4SJustin Mattock 	pr_info("%s v%s\n", vlan_fullname, vlan_version);
63369ab4b7dSPatrick McHardy 
63491e2ff35SEric W. Biederman 	err = register_pernet_subsys(&vlan_net_ops);
635d9ed0f0eSPavel Emelyanov 	if (err < 0)
636d9ed0f0eSPavel Emelyanov 		goto err0;
637d9ed0f0eSPavel Emelyanov 
63869ab4b7dSPatrick McHardy 	err = register_netdevice_notifier(&vlan_notifier_block);
63969ab4b7dSPatrick McHardy 	if (err < 0)
64069ab4b7dSPatrick McHardy 		goto err2;
64169ab4b7dSPatrick McHardy 
64270c03b49SPatrick McHardy 	err = vlan_gvrp_init();
64369ab4b7dSPatrick McHardy 	if (err < 0)
64469ab4b7dSPatrick McHardy 		goto err3;
64569ab4b7dSPatrick McHardy 
64686fbe9bbSDavid Ward 	err = vlan_mvrp_init();
64770c03b49SPatrick McHardy 	if (err < 0)
64870c03b49SPatrick McHardy 		goto err4;
64970c03b49SPatrick McHardy 
65086fbe9bbSDavid Ward 	err = vlan_netlink_init();
65186fbe9bbSDavid Ward 	if (err < 0)
65286fbe9bbSDavid Ward 		goto err5;
65386fbe9bbSDavid Ward 
65469ab4b7dSPatrick McHardy 	vlan_ioctl_set(vlan_ioctl_handler);
65569ab4b7dSPatrick McHardy 	return 0;
65669ab4b7dSPatrick McHardy 
65786fbe9bbSDavid Ward err5:
65886fbe9bbSDavid Ward 	vlan_mvrp_uninit();
65970c03b49SPatrick McHardy err4:
66070c03b49SPatrick McHardy 	vlan_gvrp_uninit();
66169ab4b7dSPatrick McHardy err3:
66269ab4b7dSPatrick McHardy 	unregister_netdevice_notifier(&vlan_notifier_block);
66369ab4b7dSPatrick McHardy err2:
66491e2ff35SEric W. Biederman 	unregister_pernet_subsys(&vlan_net_ops);
665d9ed0f0eSPavel Emelyanov err0:
66669ab4b7dSPatrick McHardy 	return err;
66769ab4b7dSPatrick McHardy }
66869ab4b7dSPatrick McHardy 
66969ab4b7dSPatrick McHardy static void __exit vlan_cleanup_module(void)
67069ab4b7dSPatrick McHardy {
67169ab4b7dSPatrick McHardy 	vlan_ioctl_set(NULL);
67269ab4b7dSPatrick McHardy 	vlan_netlink_fini();
67369ab4b7dSPatrick McHardy 
67469ab4b7dSPatrick McHardy 	unregister_netdevice_notifier(&vlan_notifier_block);
67569ab4b7dSPatrick McHardy 
67691e2ff35SEric W. Biederman 	unregister_pernet_subsys(&vlan_net_ops);
6776e327c11SJesper Dangaard Brouer 	rcu_barrier(); /* Wait for completion of call_rcu()'s */
67870c03b49SPatrick McHardy 
67986fbe9bbSDavid Ward 	vlan_mvrp_uninit();
68070c03b49SPatrick McHardy 	vlan_gvrp_uninit();
68169ab4b7dSPatrick McHardy }
68269ab4b7dSPatrick McHardy 
68369ab4b7dSPatrick McHardy module_init(vlan_proto_init);
68469ab4b7dSPatrick McHardy module_exit(vlan_cleanup_module);
68569ab4b7dSPatrick McHardy 
6861da177e4SLinus Torvalds MODULE_LICENSE("GPL");
6871da177e4SLinus Torvalds MODULE_VERSION(DRV_VERSION);
688